MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
expression.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12// Created by Wangyunlai on 2022/07/05.
13//
14
15#pragma once
16
17#include "common/lang/string.h"
18#include "common/lang/memory.h"
19#include "common/lang/unordered_set.h"
20#include "common/value.h"
21#include "storage/field/field.h"
22#include "sql/expr/aggregator.h"
23#include "storage/common/chunk.h"
24
25class Tuple;
26
36enum class ExprType
37{
38 NONE,
39 STAR,
42
43 FIELD,
44 VALUE,
45 CAST,
50};
51
66{
67public:
68 Expression() = default;
69
70 virtual ~Expression() = default;
71
75 virtual unique_ptr<Expression> copy() const = 0;
76
80 virtual bool equal(const Expression &other) const { return false; }
84 virtual RC get_value(const Tuple &tuple, Value &value) const = 0;
85
90 virtual RC try_get_value(Value &value) const { return RC::UNIMPLEMENTED; }
91
95 virtual RC get_column(Chunk &chunk, Column &column) { return RC::UNIMPLEMENTED; }
96
101 virtual ExprType type() const = 0;
102
107 virtual AttrType value_type() const = 0;
108
112 virtual int value_length() const { return -1; }
113
117 virtual const char *name() const { return name_.c_str(); }
118 virtual void set_name(string name) { name_ = name; }
119
123 virtual int pos() const { return pos_; }
124 virtual void set_pos(int pos) { pos_ = pos; }
125
129 virtual RC eval(Chunk &chunk, vector<uint8_t> &select) { return RC::UNIMPLEMENTED; }
130
131protected:
138 int pos_ = -1;
139
140private:
141 string name_;
142};
143
144class StarExpr : public Expression
145{
146public:
147 StarExpr() : table_name_() {}
148 StarExpr(const char *table_name) : table_name_(table_name) {}
149 virtual ~StarExpr() = default;
150
151 unique_ptr<Expression> copy() const override { return make_unique<StarExpr>(table_name_.c_str()); }
152
153 ExprType type() const override { return ExprType::STAR; }
154 AttrType value_type() const override { return AttrType::UNDEFINED; }
155
156 RC get_value(const Tuple &tuple, Value &value) const override { return RC::UNIMPLEMENTED; } // 不需要实现
157
158 const char *table_name() const { return table_name_.c_str(); }
159
160private:
161 string table_name_;
162};
163
165{
166public:
167 UnboundFieldExpr(const string &table_name, const string &field_name)
168 : table_name_(table_name), field_name_(field_name)
169 {}
170
171 virtual ~UnboundFieldExpr() = default;
172
173 unique_ptr<Expression> copy() const override { return make_unique<UnboundFieldExpr>(table_name_, field_name_); }
174
175 ExprType type() const override { return ExprType::UNBOUND_FIELD; }
176 AttrType value_type() const override { return AttrType::UNDEFINED; }
177
178 RC get_value(const Tuple &tuple, Value &value) const override { return RC::INTERNAL; }
179
180 const char *table_name() const { return table_name_.c_str(); }
181 const char *field_name() const { return field_name_.c_str(); }
182
183private:
184 string table_name_;
185 string field_name_;
186};
187
192class FieldExpr : public Expression
193{
194public:
195 FieldExpr() = default;
196 FieldExpr(const Table *table, const FieldMeta *field) : field_(table, field) {}
197 FieldExpr(const Field &field) : field_(field) {}
198
199 virtual ~FieldExpr() = default;
200
201 bool equal(const Expression &other) const override;
202
203 unique_ptr<Expression> copy() const override { return make_unique<FieldExpr>(field_); }
204
205 ExprType type() const override { return ExprType::FIELD; }
206 AttrType value_type() const override { return field_.attr_type(); }
207 int value_length() const override { return field_.meta()->len(); }
208
209 Field &field() { return field_; }
210
211 const Field &field() const { return field_; }
212
213 const char *table_name() const { return field_.table_name(); }
214 const char *field_name() const { return field_.field_name(); }
215
216 RC get_column(Chunk &chunk, Column &column) override;
217
218 RC get_value(const Tuple &tuple, Value &value) const override;
219
220private:
221 Field field_;
222};
223
228class ValueExpr : public Expression
229{
230public:
231 ValueExpr() = default;
232 explicit ValueExpr(const Value &value) : value_(value) {}
233
234 virtual ~ValueExpr() = default;
235
236 bool equal(const Expression &other) const override;
237
238 unique_ptr<Expression> copy() const override { return make_unique<ValueExpr>(value_); }
239
240 RC get_value(const Tuple &tuple, Value &value) const override;
241 RC get_column(Chunk &chunk, Column &column) override;
242 RC try_get_value(Value &value) const override
243 {
244 value = value_;
245 return RC::SUCCESS;
246 }
247
248 ExprType type() const override { return ExprType::VALUE; }
249 AttrType value_type() const override { return value_.attr_type(); }
250 int value_length() const override { return value_.length(); }
251
252 void get_value(Value &value) const { value = value_; }
253 const Value &get_value() const { return value_; }
254
255private:
256 Value value_;
257};
258
263class CastExpr : public Expression
264{
265public:
266 CastExpr(unique_ptr<Expression> child, AttrType cast_type);
267 virtual ~CastExpr();
268
269 unique_ptr<Expression> copy() const override { return make_unique<CastExpr>(child_->copy(), cast_type_); }
270
271 ExprType type() const override { return ExprType::CAST; }
272
273 RC get_value(const Tuple &tuple, Value &value) const override;
274 RC get_column(Chunk &chunk, Column &column) override;
275
276 RC try_get_value(Value &value) const override;
277
278 AttrType value_type() const override { return cast_type_; }
279
280 unique_ptr<Expression> &child() { return child_; }
281
282private:
283 RC cast(const Value &value, Value &cast_value) const;
284
285private:
286 unique_ptr<Expression> child_;
287 AttrType cast_type_;
288};
289
295{
296public:
297 ComparisonExpr(CompOp comp, unique_ptr<Expression> left, unique_ptr<Expression> right);
298 virtual ~ComparisonExpr();
299
300 ExprType type() const override { return ExprType::COMPARISON; }
301 RC get_value(const Tuple &tuple, Value &value) const override;
302 AttrType value_type() const override { return AttrType::BOOLEANS; }
303 CompOp comp() const { return comp_; }
304
305 unique_ptr<Expression> copy() const override
306 {
307 return make_unique<ComparisonExpr>(comp_, left_->copy(), right_->copy());
308 }
309
314 RC eval(Chunk &chunk, vector<uint8_t> &select) override;
315
316 unique_ptr<Expression> &left() { return left_; }
317 unique_ptr<Expression> &right() { return right_; }
318
323 RC try_get_value(Value &value) const override;
324
329 RC compare_value(const Value &left, const Value &right, bool &value) const;
330
331 template <typename T>
332 RC compare_column(const Column &left, const Column &right, vector<uint8_t> &result) const;
333
334private:
335 CompOp comp_;
336 unique_ptr<Expression> left_;
337 unique_ptr<Expression> right_;
338};
339
347{
348public:
349 enum class Type
350 {
351 AND,
352 OR
353 };
354
355public:
356 ConjunctionExpr(Type type, vector<unique_ptr<Expression>> &children);
357 virtual ~ConjunctionExpr() = default;
358
359 unique_ptr<Expression> copy() const override
360 {
361 vector<unique_ptr<Expression>> children;
362 for (auto &child : children_) {
363 children.emplace_back(child->copy());
364 }
365 return make_unique<ConjunctionExpr>(conjunction_type_, children);
366 }
367
368 ExprType type() const override { return ExprType::CONJUNCTION; }
369 AttrType value_type() const override { return AttrType::BOOLEANS; }
370 RC get_value(const Tuple &tuple, Value &value) const override;
371
372 Type conjunction_type() const { return conjunction_type_; }
373
374 vector<unique_ptr<Expression>> &children() { return children_; }
375
376private:
377 Type conjunction_type_;
378 vector<unique_ptr<Expression>> children_;
379};
380
386{
387public:
388 enum class Type
389 {
390 ADD,
391 SUB,
392 MUL,
393 DIV,
394 NEGATIVE,
395 };
396
397public:
398 ArithmeticExpr(Type type, Expression *left, Expression *right);
399 ArithmeticExpr(Type type, unique_ptr<Expression> left, unique_ptr<Expression> right);
400 virtual ~ArithmeticExpr() = default;
401
402 unique_ptr<Expression> copy() const override
403 {
404 if (right_) {
405 return make_unique<ArithmeticExpr>(arithmetic_type_, left_->copy(), right_->copy());
406 } else {
407 return make_unique<ArithmeticExpr>(arithmetic_type_, left_->copy(), nullptr);
408 }
409 }
410
411 bool equal(const Expression &other) const override;
412 ExprType type() const override { return ExprType::ARITHMETIC; }
413
414 AttrType value_type() const override;
415 int value_length() const override { return std::max(left_->value_length(), right_ ? right_->value_length() : 0); };
416
417 RC get_value(const Tuple &tuple, Value &value) const override;
418
419 RC get_column(Chunk &chunk, Column &column) override;
420
421 RC try_get_value(Value &value) const override;
422
423 Type arithmetic_type() const { return arithmetic_type_; }
424
425 unique_ptr<Expression> &left() { return left_; }
426 unique_ptr<Expression> &right() { return right_; }
427
428private:
429 RC calc_value(const Value &left_value, const Value &right_value, Value &value) const;
430
431 RC calc_column(const Column &left_column, const Column &right_column, Column &column) const;
432
433 template <bool LEFT_CONSTANT, bool RIGHT_CONSTANT>
434 RC execute_calc(const Column &left, const Column &right, Column &result, Type type, AttrType attr_type) const;
435
436private:
437 Type arithmetic_type_;
438 unique_ptr<Expression> left_;
439 unique_ptr<Expression> right_;
440};
441
443{
444public:
445 UnboundAggregateExpr(const char *aggregate_name, Expression *child);
446 UnboundAggregateExpr(const char *aggregate_name, unique_ptr<Expression> child);
447 virtual ~UnboundAggregateExpr() = default;
448
449 ExprType type() const override { return ExprType::UNBOUND_AGGREGATION; }
450
451 unique_ptr<Expression> copy() const override
452 {
453 return make_unique<UnboundAggregateExpr>(aggregate_name_.c_str(), child_->copy());
454 }
455
456 const char *aggregate_name() const { return aggregate_name_.c_str(); }
457
458 unique_ptr<Expression> &child() { return child_; }
459
460 RC get_value(const Tuple &tuple, Value &value) const override { return RC::INTERNAL; }
461 AttrType value_type() const override { return child_->value_type(); }
462
463private:
464 string aggregate_name_;
465 unique_ptr<Expression> child_;
466};
467
469{
470public:
471 enum class Type
472 {
473 COUNT,
474 SUM,
475 AVG,
476 MAX,
477 MIN,
478 };
479
480public:
481 AggregateExpr(Type type, Expression *child);
482 AggregateExpr(Type type, unique_ptr<Expression> child);
483 virtual ~AggregateExpr() = default;
484
485 bool equal(const Expression &other) const override;
486
487 unique_ptr<Expression> copy() const override { return make_unique<AggregateExpr>(aggregate_type_, child_->copy()); }
488
489 ExprType type() const override { return ExprType::AGGREGATION; }
490
491 AttrType value_type() const override
492 {
493 if (aggregate_type_ == Type::COUNT) {
494 return AttrType::INTS;
495 } else if (aggregate_type_ == Type::AVG) {
496 return AttrType::FLOATS;
497 } else {
498 return child_->value_type();
499 }
500 }
501 int value_length() const override
502 {
503 if (aggregate_type_ == Type::COUNT) {
504 return sizeof(int);
505 } else if (aggregate_type_ == Type::AVG) {
506 return sizeof(float);
507 } else {
508 return child_->value_length();
509 }
510 }
511
512 RC get_value(const Tuple &tuple, Value &value) const override;
513
514 RC get_column(Chunk &chunk, Column &column) override;
515
516 Type aggregate_type() const { return aggregate_type_; }
517
518 unique_ptr<Expression> &child() { return child_; }
519
520 const unique_ptr<Expression> &child() const { return child_; }
521
522 unique_ptr<Aggregator> create_aggregator() const;
523
524public:
525 static RC type_from_string(const char *type_str, Type &type);
526
527private:
528 Type aggregate_type_;
529 unique_ptr<Expression> child_;
530};
Definition: expression.h:469
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:489
int value_length() const override
表达式值的长度
Definition: expression.h:501
RC get_column(Chunk &chunk, Column &column) override
从 chunk 中获取表达式的计算结果 column
Definition: expression.cpp:569
bool equal(const Expression &other) const override
判断两个表达式是否相等
Definition: expression.cpp:580
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:487
AttrType value_type() const override
表达式值的类型
Definition: expression.h:491
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:608
算术表达式
Definition: expression.h:386
RC get_column(Chunk &chunk, Column &column) override
从 chunk 中获取表达式的计算结果 column
Definition: expression.cpp:482
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:462
RC try_get_value(Value &value) const override
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.cpp:529
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:412
int value_length() const override
表达式值的长度
Definition: expression.h:415
AttrType value_type() const override
表达式值的类型
Definition: expression.cpp:343
bool equal(const Expression &other) const override
判断两个表达式是否相等
Definition: expression.cpp:331
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:402
类型转换表达式
Definition: expression.h:264
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:271
unique_ptr< Expression > child_
从这个表达式转换
Definition: expression.h:286
RC get_column(Chunk &chunk, Column &column) override
从 chunk 中获取表达式的计算结果 column
Definition: expression.cpp:102
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:269
AttrType value_type() const override
表达式值的类型
Definition: expression.h:278
AttrType cast_type_
想要转换成这个类型
Definition: expression.h:287
RC try_get_value(Value &value) const override
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.cpp:122
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:91
A Chunk represents a set of columns.
Definition: chunk.h:23
A column contains multiple values in contiguous memory with a specified type.
Definition: column.h:23
比较表达式
Definition: expression.h:295
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:196
AttrType value_type() const override
表达式值的类型
Definition: expression.h:302
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:305
RC eval(Chunk &chunk, vector< uint8_t > &select) override
根据 ComparisonExpr 获得 select 结果。 select 的长度与chunk 的行数相同,表示每一行在ComparisonExpr 计算后是否会被输出。
Definition: expression.cpp:221
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:300
RC compare_value(const Value &left, const Value &right, bool &value) const
Definition: expression.cpp:142
RC try_get_value(Value &value) const override
Definition: expression.cpp:175
联结表达式多个表达式使用同一种关系(AND或OR)来联结 当前miniob仅有AND操作
Definition: expression.h:347
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:295
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:359
AttrType value_type() const override
表达式值的类型
Definition: expression.h:369
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:368
表达式的抽象描述
Definition: expression.h:66
virtual const char * name() const
表达式的名字,比如是字段名称,或者用户在执行SQL语句时输入的内容
Definition: expression.h:117
virtual bool equal(const Expression &other) const
判断两个表达式是否相等
Definition: expression.h:80
int pos_
表达式在下层算子返回的 chunk 中的位置
Definition: expression.h:138
virtual AttrType value_type() const =0
表达式值的类型
virtual int pos() const
表达式在下层算子返回的 chunk 中的位置
Definition: expression.h:123
virtual int value_length() const
表达式值的长度
Definition: expression.h:112
virtual RC get_value(const Tuple &tuple, Value &value) const =0
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
virtual RC get_column(Chunk &chunk, Column &column)
从 chunk 中获取表达式的计算结果 column
Definition: expression.h:95
virtual unique_ptr< Expression > copy() const =0
复制表达式
virtual ExprType type() const =0
表达式的类型 可以根据表达式类型来转换为具体的子类
virtual RC eval(Chunk &chunk, vector< uint8_t > &select)
用于 ComparisonExpr 获得比较结果 select。
Definition: expression.h:129
virtual RC try_get_value(Value &value) const
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.h:90
字段表达式
Definition: expression.h:193
bool equal(const Expression &other) const override
判断两个表达式是否相等
Definition: expression.cpp:26
int value_length() const override
表达式值的长度
Definition: expression.h:207
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:21
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:203
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:205
AttrType value_type() const override
表达式值的类型
Definition: expression.h:206
RC get_column(Chunk &chunk, Column &column) override
从 chunk 中获取表达式的计算结果 column
Definition: expression.cpp:40
字段元数据
Definition: field_meta.h:30
字段
Definition: field.h:25
Definition: expression.h:145
AttrType value_type() const override
表达式值的类型
Definition: expression.h:154
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.h:156
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:153
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:151
Definition: table.h:44
元组的抽象描述
Definition: tuple.h:66
Definition: expression.h:443
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.h:460
AttrType value_type() const override
表达式值的类型
Definition: expression.h:461
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:451
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:449
Definition: expression.h:165
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.h:178
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:173
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:175
AttrType value_type() const override
表达式值的类型
Definition: expression.h:176
常量值表达式
Definition: expression.h:229
AttrType value_type() const override
表达式值的类型
Definition: expression.h:249
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:248
RC get_column(Chunk &chunk, Column &column) override
从 chunk 中获取表达式的计算结果 column
Definition: expression.cpp:68
int value_length() const override
表达式值的长度
Definition: expression.h:250
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:238
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:62
RC try_get_value(Value &value) const override
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.h:242
bool equal(const Expression &other) const override
判断两个表达式是否相等
Definition: expression.cpp:50
属性的值
Definition: value.h:31
ExprType
表达式类型
Definition: expression.h:37
@ CAST
需要做类型转换的表达式
@ COMPARISON
需要做比较的表达式
@ UNBOUND_AGGREGATION
未绑定的聚合函数,需要在resolver阶段解析为AggregateExpr
@ FIELD
字段。在实际执行时,根据行数据内容提取对应字段的值
@ ARITHMETIC
算术运算
@ STAR
星号,表示所有字段
@ UNBOUND_FIELD
未绑定的字段,需要在resolver阶段解析为FieldExpr
@ CONJUNCTION
多个表达式使用同一种关系(AND或OR)来联结
@ VALUE
常量值
@ AGGREGATION
聚合运算
CompOp
描述比较运算符
Definition: parse_defs.h:47