|
| 1 | +// constant_folding.h — Evaluate sub-expressions that have no column references |
| 2 | +// at plan time, replacing them with literal AST nodes. |
| 3 | +// |
| 4 | +// Walks all expression ASTs in Filter, Project, Sort conditions. For each |
| 5 | +// sub-expression with zero column references, evaluates it using |
| 6 | +// evaluate_expression with a null resolver and replaces the node in-place. |
| 7 | + |
| 8 | +#ifndef SQL_ENGINE_RULES_CONSTANT_FOLDING_H |
| 9 | +#define SQL_ENGINE_RULES_CONSTANT_FOLDING_H |
| 10 | + |
| 11 | +#include "sql_engine/plan_node.h" |
| 12 | +#include "sql_engine/catalog.h" |
| 13 | +#include "sql_engine/expression_eval.h" |
| 14 | +#include "sql_engine/function_registry.h" |
| 15 | +#include "sql_parser/ast.h" |
| 16 | +#include "sql_parser/arena.h" |
| 17 | +#include "sql_parser/common.h" |
| 18 | +#include <cstdio> |
| 19 | +#include <cstring> |
| 20 | + |
| 21 | +namespace sql_engine { |
| 22 | +namespace rules { |
| 23 | + |
| 24 | +namespace detail_cf { |
| 25 | + |
| 26 | +// Check if an expression has any column references |
| 27 | +inline bool has_column_ref(const sql_parser::AstNode* expr) { |
| 28 | + if (!expr) return false; |
| 29 | + switch (expr->type) { |
| 30 | + case sql_parser::NodeType::NODE_COLUMN_REF: |
| 31 | + case sql_parser::NodeType::NODE_QUALIFIED_NAME: |
| 32 | + return true; |
| 33 | + case sql_parser::NodeType::NODE_IDENTIFIER: |
| 34 | + // Identifiers in expression context are column references |
| 35 | + return true; |
| 36 | + default: |
| 37 | + break; |
| 38 | + } |
| 39 | + for (const sql_parser::AstNode* c = expr->first_child; c; c = c->next_sibling) { |
| 40 | + if (has_column_ref(c)) return true; |
| 41 | + } |
| 42 | + return false; |
| 43 | +} |
| 44 | + |
| 45 | +// Try to fold a constant sub-expression. Returns true if folded. |
| 46 | +// Modifies the node in-place (arena-allocated, safe to mutate). |
| 47 | +template <sql_parser::Dialect D> |
| 48 | +inline bool try_fold(sql_parser::AstNode* expr, |
| 49 | + FunctionRegistry<D>& functions, |
| 50 | + sql_parser::Arena& arena) { |
| 51 | + if (!expr) return false; |
| 52 | + |
| 53 | + // Don't fold leaf literals — already constant |
| 54 | + switch (expr->type) { |
| 55 | + case sql_parser::NodeType::NODE_LITERAL_INT: |
| 56 | + case sql_parser::NodeType::NODE_LITERAL_FLOAT: |
| 57 | + case sql_parser::NodeType::NODE_LITERAL_STRING: |
| 58 | + case sql_parser::NodeType::NODE_LITERAL_NULL: |
| 59 | + return false; |
| 60 | + default: |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + // If this expression has no column refs, try to evaluate it |
| 65 | + if (!has_column_ref(expr)) { |
| 66 | + // Null resolver — any column reference attempt returns null |
| 67 | + auto null_resolve = [](sql_parser::StringRef) -> Value { |
| 68 | + return value_null(); |
| 69 | + }; |
| 70 | + |
| 71 | + Value result = evaluate_expression<D>(expr, null_resolve, functions, arena); |
| 72 | + |
| 73 | + if (result.is_null()) { |
| 74 | + expr->type = sql_parser::NodeType::NODE_LITERAL_NULL; |
| 75 | + expr->first_child = nullptr; |
| 76 | + expr->value_ptr = nullptr; |
| 77 | + expr->value_len = 0; |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + // Replace node with appropriate literal type |
| 82 | + switch (result.tag) { |
| 83 | + case Value::TAG_INT64: { |
| 84 | + // Format integer as string in arena |
| 85 | + char tmp[32]; |
| 86 | + int n = std::snprintf(tmp, sizeof(tmp), "%lld", |
| 87 | + static_cast<long long>(result.int_val)); |
| 88 | + if (n > 0) { |
| 89 | + char* buf = static_cast<char*>(arena.allocate(static_cast<uint32_t>(n))); |
| 90 | + std::memcpy(buf, tmp, static_cast<size_t>(n)); |
| 91 | + expr->type = sql_parser::NodeType::NODE_LITERAL_INT; |
| 92 | + expr->value_ptr = buf; |
| 93 | + expr->value_len = static_cast<uint32_t>(n); |
| 94 | + expr->first_child = nullptr; |
| 95 | + return true; |
| 96 | + } |
| 97 | + break; |
| 98 | + } |
| 99 | + case Value::TAG_DOUBLE: { |
| 100 | + char tmp[64]; |
| 101 | + int n = std::snprintf(tmp, sizeof(tmp), "%g", result.double_val); |
| 102 | + if (n > 0) { |
| 103 | + char* buf = static_cast<char*>(arena.allocate(static_cast<uint32_t>(n))); |
| 104 | + std::memcpy(buf, tmp, static_cast<size_t>(n)); |
| 105 | + expr->type = sql_parser::NodeType::NODE_LITERAL_FLOAT; |
| 106 | + expr->value_ptr = buf; |
| 107 | + expr->value_len = static_cast<uint32_t>(n); |
| 108 | + expr->first_child = nullptr; |
| 109 | + return true; |
| 110 | + } |
| 111 | + break; |
| 112 | + } |
| 113 | + case Value::TAG_STRING: { |
| 114 | + expr->type = sql_parser::NodeType::NODE_LITERAL_STRING; |
| 115 | + expr->value_ptr = result.str_val.ptr; |
| 116 | + expr->value_len = result.str_val.len; |
| 117 | + expr->first_child = nullptr; |
| 118 | + return true; |
| 119 | + } |
| 120 | + case Value::TAG_BOOL: { |
| 121 | + const char* s = result.bool_val ? "TRUE" : "FALSE"; |
| 122 | + uint32_t len = result.bool_val ? 4 : 5; |
| 123 | + char* buf = static_cast<char*>(arena.allocate(len)); |
| 124 | + std::memcpy(buf, s, len); |
| 125 | + expr->type = sql_parser::NodeType::NODE_LITERAL_INT; |
| 126 | + expr->value_ptr = buf; |
| 127 | + expr->value_len = len; |
| 128 | + expr->first_child = nullptr; |
| 129 | + return true; |
| 130 | + } |
| 131 | + default: |
| 132 | + break; |
| 133 | + } |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + // Has column refs — try to fold children (partial folding) |
| 138 | + bool any_folded = false; |
| 139 | + for (sql_parser::AstNode* c = expr->first_child; c; c = c->next_sibling) { |
| 140 | + if (try_fold<D>(c, functions, arena)) { |
| 141 | + any_folded = true; |
| 142 | + } |
| 143 | + } |
| 144 | + return any_folded; |
| 145 | +} |
| 146 | + |
| 147 | +// Fold all expressions in a plan node |
| 148 | +template <sql_parser::Dialect D> |
| 149 | +inline void fold_plan_exprs(PlanNode* node, |
| 150 | + FunctionRegistry<D>& functions, |
| 151 | + sql_parser::Arena& arena) { |
| 152 | + if (!node) return; |
| 153 | + |
| 154 | + switch (node->type) { |
| 155 | + case PlanNodeType::FILTER: |
| 156 | + if (node->filter.expr) { |
| 157 | + try_fold<D>(const_cast<sql_parser::AstNode*>(node->filter.expr), |
| 158 | + functions, arena); |
| 159 | + } |
| 160 | + break; |
| 161 | + case PlanNodeType::PROJECT: |
| 162 | + for (uint16_t i = 0; i < node->project.count; ++i) { |
| 163 | + if (node->project.exprs[i]) { |
| 164 | + try_fold<D>(const_cast<sql_parser::AstNode*>(node->project.exprs[i]), |
| 165 | + functions, arena); |
| 166 | + } |
| 167 | + } |
| 168 | + break; |
| 169 | + case PlanNodeType::SORT: |
| 170 | + for (uint16_t i = 0; i < node->sort.count; ++i) { |
| 171 | + if (node->sort.keys[i]) { |
| 172 | + try_fold<D>(const_cast<sql_parser::AstNode*>(node->sort.keys[i]), |
| 173 | + functions, arena); |
| 174 | + } |
| 175 | + } |
| 176 | + break; |
| 177 | + case PlanNodeType::JOIN: |
| 178 | + if (node->join.condition) { |
| 179 | + try_fold<D>(const_cast<sql_parser::AstNode*>(node->join.condition), |
| 180 | + functions, arena); |
| 181 | + } |
| 182 | + break; |
| 183 | + case PlanNodeType::AGGREGATE: |
| 184 | + for (uint16_t i = 0; i < node->aggregate.group_count; ++i) { |
| 185 | + if (node->aggregate.group_by[i]) { |
| 186 | + try_fold<D>(const_cast<sql_parser::AstNode*>(node->aggregate.group_by[i]), |
| 187 | + functions, arena); |
| 188 | + } |
| 189 | + } |
| 190 | + for (uint16_t i = 0; i < node->aggregate.agg_count; ++i) { |
| 191 | + if (node->aggregate.agg_exprs[i]) { |
| 192 | + try_fold<D>(const_cast<sql_parser::AstNode*>(node->aggregate.agg_exprs[i]), |
| 193 | + functions, arena); |
| 194 | + } |
| 195 | + } |
| 196 | + break; |
| 197 | + default: |
| 198 | + break; |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +} // namespace detail_cf |
| 203 | + |
| 204 | +template <sql_parser::Dialect D> |
| 205 | +inline PlanNode* constant_folding(PlanNode* node, const Catalog& catalog, |
| 206 | + FunctionRegistry<D>& functions, |
| 207 | + sql_parser::Arena& arena) { |
| 208 | + if (!node) return nullptr; |
| 209 | + |
| 210 | + // Fold expressions in this node |
| 211 | + detail_cf::fold_plan_exprs<D>(node, functions, arena); |
| 212 | + |
| 213 | + // Recurse into children |
| 214 | + node->left = constant_folding<D>(node->left, catalog, functions, arena); |
| 215 | + node->right = constant_folding<D>(node->right, catalog, functions, arena); |
| 216 | + |
| 217 | + return node; |
| 218 | +} |
| 219 | + |
| 220 | +} // namespace rules |
| 221 | +} // namespace sql_engine |
| 222 | + |
| 223 | +#endif // SQL_ENGINE_RULES_CONSTANT_FOLDING_H |
0 commit comments