-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.cpp
More file actions
463 lines (413 loc) · 15.8 KB
/
expression.cpp
File metadata and controls
463 lines (413 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
* @file expression.cpp
* @brief Expression evaluator implementation
*/
#include "parser/expression.hpp"
#include <cstddef>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "common/value.hpp"
#include "executor/types.hpp"
#include "parser/token.hpp"
namespace cloudsql::parser {
/**
* @brief Evaluate binary expression
*/
common::Value BinaryExpr::evaluate(const executor::Tuple* tuple,
const executor::Schema* schema) const {
const common::Value left_val = left_->evaluate(tuple, schema);
const common::Value right_val = right_->evaluate(tuple, schema);
switch (op_) {
case TokenType::Plus:
if (left_val.type() == common::ValueType::TYPE_FLOAT64 ||
right_val.type() == common::ValueType::TYPE_FLOAT64) {
return common::Value::make_float64(left_val.to_float64() + right_val.to_float64());
}
return common::Value::make_int64(left_val.to_int64() + right_val.to_int64());
case TokenType::Minus:
if (left_val.type() == common::ValueType::TYPE_FLOAT64 ||
right_val.type() == common::ValueType::TYPE_FLOAT64) {
return common::Value::make_float64(left_val.to_float64() - right_val.to_float64());
}
return common::Value::make_int64(left_val.to_int64() - right_val.to_int64());
case TokenType::Star:
if (left_val.type() == common::ValueType::TYPE_FLOAT64 ||
right_val.type() == common::ValueType::TYPE_FLOAT64) {
return common::Value::make_float64(left_val.to_float64() * right_val.to_float64());
}
return common::Value::make_int64(left_val.to_int64() * right_val.to_int64());
case TokenType::Slash:
return common::Value::make_float64(left_val.to_float64() / right_val.to_float64());
case TokenType::Eq:
return common::Value(left_val == right_val);
case TokenType::Ne:
return common::Value(left_val != right_val);
case TokenType::Lt:
return common::Value(left_val < right_val);
case TokenType::Le:
return common::Value(left_val <= right_val);
case TokenType::Gt:
return common::Value(left_val > right_val);
case TokenType::Ge:
return common::Value(left_val >= right_val);
case TokenType::And:
return common::Value(left_val.as_bool() && right_val.as_bool());
case TokenType::Or:
return common::Value(left_val.as_bool() || right_val.as_bool());
default:
return common::Value::make_null();
}
}
void BinaryExpr::evaluate_vectorized(const executor::VectorBatch& batch,
const executor::Schema& schema,
executor::ColumnVector& result) const {
const size_t row_count = batch.row_count();
result.clear();
// Try to optimize: Column op Constant
if (left_->type() == ExprType::Column && right_->type() == ExprType::Constant) {
const auto& col_expr = static_cast<const ColumnExpr&>(*left_);
const auto& const_expr = static_cast<const ConstantExpr&>(*right_);
const size_t col_idx = schema.find_column(col_expr.name());
if (col_idx != static_cast<size_t>(-1)) {
auto& src_col = const_cast<executor::VectorBatch&>(batch).get_column(col_idx);
// INT64 optimize
if (src_col.type() == common::ValueType::TYPE_INT64 &&
const_expr.value().type() == common::ValueType::TYPE_INT64) {
auto& num_src = dynamic_cast<executor::NumericVector<int64_t>&>(src_col);
const int64_t* src_data = num_src.raw_data();
const int64_t const_val = const_expr.value().as_int64();
if (op_ == TokenType::Gt) {
auto& bool_res = dynamic_cast<executor::NumericVector<bool>&>(result);
bool_res.resize(row_count);
uint8_t* res_data = bool_res.raw_data_mut();
for (size_t i = 0; i < row_count; ++i) {
if (num_src.is_null(i)) {
bool_res.set_null(i, true);
} else {
res_data[i] = static_cast<uint8_t>(src_data[i] > const_val);
bool_res.set_null(i, false);
}
}
return;
}
if (op_ == TokenType::Eq) {
auto& bool_res = dynamic_cast<executor::NumericVector<bool>&>(result);
bool_res.resize(row_count);
uint8_t* res_data = bool_res.raw_data_mut();
for (size_t i = 0; i < row_count; ++i) {
if (num_src.is_null(i)) {
bool_res.set_null(i, true);
} else {
res_data[i] = static_cast<uint8_t>(src_data[i] == const_val);
bool_res.set_null(i, false);
}
}
return;
}
}
}
}
// Fallback to row-by-row if not optimized
for (size_t i = 0; i < row_count; ++i) {
std::vector<common::Value> row_vals;
for (size_t c = 0; c < batch.column_count(); ++c) {
row_vals.push_back(const_cast<executor::VectorBatch&>(batch).get_column(c).get(i));
}
executor::Tuple t(std::move(row_vals));
result.append(evaluate(&t, &schema));
}
}
std::string BinaryExpr::to_string() const {
std::string op_str;
switch (op_) {
case TokenType::Plus:
op_str = " + ";
break;
case TokenType::Minus:
op_str = " - ";
break;
case TokenType::Star:
op_str = " * ";
break;
case TokenType::Slash:
op_str = " / ";
break;
case TokenType::Eq:
op_str = " = ";
break;
case TokenType::Ne:
op_str = " <> ";
break;
case TokenType::Lt:
op_str = " < ";
break;
case TokenType::Le:
op_str = " <= ";
break;
case TokenType::Gt:
op_str = " > ";
break;
case TokenType::Ge:
op_str = " >= ";
break;
case TokenType::And:
op_str = " AND ";
break;
case TokenType::Or:
op_str = " OR ";
break;
default:
op_str = " ";
break;
}
return left_->to_string() + op_str + right_->to_string();
}
std::unique_ptr<Expression> BinaryExpr::clone() const {
return std::make_unique<BinaryExpr>(left_->clone(), op_, right_->clone());
}
/**
* @brief Evaluate unary expression
*/
common::Value UnaryExpr::evaluate(const executor::Tuple* tuple,
const executor::Schema* schema) const {
const common::Value val = expr_->evaluate(tuple, schema);
switch (op_) {
case TokenType::Minus:
if (val.is_numeric()) {
return common::Value(-val.to_float64());
}
break;
case TokenType::Not:
return common::Value(!val.as_bool());
default:
break;
}
return common::Value::make_null();
}
void UnaryExpr::evaluate_vectorized(const executor::VectorBatch& batch,
const executor::Schema& schema,
executor::ColumnVector& result) const {
const size_t row_count = batch.row_count();
result.clear();
for (size_t i = 0; i < row_count; ++i) {
std::vector<common::Value> row_vals;
for (size_t c = 0; c < batch.column_count(); ++c) {
row_vals.push_back(const_cast<executor::VectorBatch&>(batch).get_column(c).get(i));
}
executor::Tuple t(std::move(row_vals));
result.append(evaluate(&t, &schema));
}
}
std::string UnaryExpr::to_string() const {
return (op_ == TokenType::Minus ? "-" : "NOT ") + expr_->to_string();
}
std::unique_ptr<Expression> UnaryExpr::clone() const {
return std::make_unique<UnaryExpr>(op_, expr_->clone());
}
/**
* @brief Evaluate column expression using tuple and schema
*/
common::Value ColumnExpr::evaluate(const executor::Tuple* tuple,
const executor::Schema* schema) const {
if (tuple == nullptr || schema == nullptr) {
return common::Value::make_null();
}
size_t index = static_cast<size_t>(-1);
/* 1. Try exact match (either fully qualified or just name) */
index = schema->find_column(this->to_string());
/* 2. If not found and it's qualified, try just the column name */
if (index == static_cast<size_t>(-1) && has_table()) {
index = schema->find_column(name_);
}
if (index == static_cast<size_t>(-1)) {
return common::Value::make_null();
}
return tuple->get(index);
}
void ColumnExpr::evaluate_vectorized(const executor::VectorBatch& batch,
const executor::Schema& schema,
executor::ColumnVector& result) const {
size_t index = static_cast<size_t>(-1);
/* 1. Try exact match (either fully qualified or just name) */
index = schema.find_column(this->to_string());
/* 2. If not found and it's qualified, try just the column name */
if (index == static_cast<size_t>(-1) && has_table()) {
index = schema.find_column(name_);
}
result.clear();
if (index == static_cast<size_t>(-1)) {
for (size_t i = 0; i < batch.row_count(); ++i) {
result.append(common::Value::make_null());
}
return;
}
auto& src_col = const_cast<executor::VectorBatch&>(batch).get_column(index);
for (size_t i = 0; i < batch.row_count(); ++i) {
result.append(src_col.get(i));
}
}
std::string ColumnExpr::to_string() const {
return has_table() ? table_name_ + "." + name_ : name_;
}
std::unique_ptr<Expression> ColumnExpr::clone() const {
return has_table() ? std::make_unique<ColumnExpr>(table_name_, name_)
: std::make_unique<ColumnExpr>(name_);
}
common::Value ConstantExpr::evaluate(const executor::Tuple* tuple,
const executor::Schema* schema) const {
(void)tuple;
(void)schema;
return value_;
}
void ConstantExpr::evaluate_vectorized(const executor::VectorBatch& batch,
const executor::Schema& schema,
executor::ColumnVector& result) const {
(void)schema;
result.clear();
for (size_t i = 0; i < batch.row_count(); ++i) {
result.append(value_);
}
}
std::string ConstantExpr::to_string() const {
if (value_.type() == common::ValueType::TYPE_TEXT) {
return "'" + value_.to_string() + "'";
}
return value_.to_string();
}
std::unique_ptr<Expression> ConstantExpr::clone() const {
return std::make_unique<ConstantExpr>(value_);
}
/**
* @brief Evaluate function expression
*/
common::Value FunctionExpr::evaluate(const executor::Tuple* tuple,
const executor::Schema* schema) const {
if (tuple == nullptr || schema == nullptr) {
return common::Value::make_null();
}
/* Attempt to look up the function result in the schema (e.g. for aggregates) */
const size_t index = schema->find_column(this->to_string());
if (index != static_cast<size_t>(-1)) {
return tuple->get(index);
}
return common::Value::make_null();
}
void FunctionExpr::evaluate_vectorized(const executor::VectorBatch& batch,
const executor::Schema& schema,
executor::ColumnVector& result) const {
const size_t row_count = batch.row_count();
result.clear();
for (size_t i = 0; i < row_count; ++i) {
std::vector<common::Value> row_vals;
for (size_t c = 0; c < batch.column_count(); ++c) {
row_vals.push_back(const_cast<executor::VectorBatch&>(batch).get_column(c).get(i));
}
executor::Tuple t(std::move(row_vals));
result.append(evaluate(&t, &schema));
}
}
std::string FunctionExpr::to_string() const {
std::string result = func_name_ + "(";
if (distinct_) {
result += "DISTINCT ";
}
bool first = true;
for (const auto& arg : args_) {
if (!first) {
result += ", ";
}
result += arg->to_string();
first = false;
}
if (args_.empty() && func_name_ == "COUNT") {
result += "*";
}
result += ")";
return result;
}
std::unique_ptr<Expression> FunctionExpr::clone() const {
auto result = std::make_unique<FunctionExpr>(func_name_);
result->set_distinct(distinct_);
for (const auto& arg : args_) {
result->add_arg(arg->clone());
}
return result;
}
/**
* @brief Evaluate IN expression
*/
common::Value InExpr::evaluate(const executor::Tuple* tuple, const executor::Schema* schema) const {
const common::Value col_val = column_->evaluate(tuple, schema);
for (const auto& val : values_) {
if (col_val == val->evaluate(tuple, schema)) {
return common::Value(!not_flag_);
}
}
return common::Value(not_flag_);
}
void InExpr::evaluate_vectorized(const executor::VectorBatch& batch, const executor::Schema& schema,
executor::ColumnVector& result) const {
const size_t row_count = batch.row_count();
result.clear();
for (size_t i = 0; i < row_count; ++i) {
std::vector<common::Value> row_vals;
for (size_t c = 0; c < batch.column_count(); ++c) {
row_vals.push_back(const_cast<executor::VectorBatch&>(batch).get_column(c).get(i));
}
executor::Tuple t(std::move(row_vals));
result.append(evaluate(&t, &schema));
}
}
std::string InExpr::to_string() const {
std::string result = column_->to_string() + (not_flag_ ? " NOT IN (" : " IN (");
bool first = true;
for (const auto& val : values_) {
if (!first) {
result += ", ";
}
result += val->to_string();
first = false;
}
result += ")";
return result;
}
std::unique_ptr<Expression> InExpr::clone() const {
std::vector<std::unique_ptr<Expression>> cloned_vals;
cloned_vals.reserve(values_.size());
for (const auto& val : values_) {
cloned_vals.push_back(val->clone());
}
return std::make_unique<InExpr>(column_->clone(), std::move(cloned_vals), not_flag_);
}
/**
* @brief Evaluate IS NULL expression
*/
common::Value IsNullExpr::evaluate(const executor::Tuple* tuple,
const executor::Schema* schema) const {
const common::Value val = expr_->evaluate(tuple, schema);
const bool result = val.is_null();
return common::Value(not_flag_ ? !result : result);
}
void IsNullExpr::evaluate_vectorized(const executor::VectorBatch& batch,
const executor::Schema& schema,
executor::ColumnVector& result) const {
const size_t row_count = batch.row_count();
result.clear();
for (size_t i = 0; i < row_count; ++i) {
std::vector<common::Value> row_vals;
for (size_t c = 0; c < batch.column_count(); ++c) {
row_vals.push_back(const_cast<executor::VectorBatch&>(batch).get_column(c).get(i));
}
executor::Tuple t(std::move(row_vals));
result.append(evaluate(&t, &schema));
}
}
std::string IsNullExpr::to_string() const {
return expr_->to_string() + (not_flag_ ? " IS NOT NULL" : " IS NULL");
}
std::unique_ptr<Expression> IsNullExpr::clone() const {
return std::make_unique<IsNullExpr>(expr_->clone(), not_flag_);
}
} // namespace cloudsql::parser