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
275 RC try_get_value(Value &value) const override;
276
277 AttrType value_type() const override { return cast_type_; }
278
279 unique_ptr<Expression> &child() { return child_; }
280
281private:
282 RC cast(const Value &value, Value &cast_value) const;
283
284private:
285 unique_ptr<Expression> child_;
286 AttrType cast_type_;
287};
288
294{
295public:
296 ComparisonExpr(CompOp comp, unique_ptr<Expression> left, unique_ptr<Expression> right);
297 virtual ~ComparisonExpr();
298
299 ExprType type() const override { return ExprType::COMPARISON; }
300 RC get_value(const Tuple &tuple, Value &value) const override;
301 AttrType value_type() const override { return AttrType::BOOLEANS; }
302 CompOp comp() const { return comp_; }
303
304 unique_ptr<Expression> copy() const override
305 {
306 return make_unique<ComparisonExpr>(comp_, left_->copy(), right_->copy());
307 }
308
313 RC eval(Chunk &chunk, vector<uint8_t> &select) override;
314
315 unique_ptr<Expression> &left() { return left_; }
316 unique_ptr<Expression> &right() { return right_; }
317
322 RC try_get_value(Value &value) const override;
323
328 RC compare_value(const Value &left, const Value &right, bool &value) const;
329
330 template <typename T>
331 RC compare_column(const Column &left, const Column &right, vector<uint8_t> &result) const;
332
333private:
334 CompOp comp_;
335 unique_ptr<Expression> left_;
336 unique_ptr<Expression> right_;
337};
338
346{
347public:
348 enum class Type
349 {
350 AND,
351 OR
352 };
353
354public:
355 ConjunctionExpr(Type type, vector<unique_ptr<Expression>> &children);
356 virtual ~ConjunctionExpr() = default;
357
358 unique_ptr<Expression> copy() const override
359 {
360 vector<unique_ptr<Expression>> children;
361 for (auto &child : children_) {
362 children.emplace_back(child->copy());
363 }
364 return make_unique<ConjunctionExpr>(conjunction_type_, children);
365 }
366
367 ExprType type() const override { return ExprType::CONJUNCTION; }
368 AttrType value_type() const override { return AttrType::BOOLEANS; }
369 RC get_value(const Tuple &tuple, Value &value) const override;
370
371 Type conjunction_type() const { return conjunction_type_; }
372
373 vector<unique_ptr<Expression>> &children() { return children_; }
374
375private:
376 Type conjunction_type_;
377 vector<unique_ptr<Expression>> children_;
378};
379
385{
386public:
387 enum class Type
388 {
389 ADD,
390 SUB,
391 MUL,
392 DIV,
393 NEGATIVE,
394 };
395
396public:
397 ArithmeticExpr(Type type, Expression *left, Expression *right);
398 ArithmeticExpr(Type type, unique_ptr<Expression> left, unique_ptr<Expression> right);
399 virtual ~ArithmeticExpr() = default;
400
401 unique_ptr<Expression> copy() const override
402 {
403 if (right_) {
404 return make_unique<ArithmeticExpr>(arithmetic_type_, left_->copy(), right_->copy());
405 } else {
406 return make_unique<ArithmeticExpr>(arithmetic_type_, left_->copy(), nullptr);
407 }
408 }
409
410 bool equal(const Expression &other) const override;
411 ExprType type() const override { return ExprType::ARITHMETIC; }
412
413 AttrType value_type() const override;
414 int value_length() const override
415 {
416 if (!right_) {
417 return left_->value_length();
418 }
419 return 4; // sizeof(float) or sizeof(int)
420 };
421
422 RC get_value(const Tuple &tuple, Value &value) const override;
423
424 RC get_column(Chunk &chunk, Column &column) override;
425
426 RC try_get_value(Value &value) const override;
427
428 Type arithmetic_type() const { return arithmetic_type_; }
429
430 unique_ptr<Expression> &left() { return left_; }
431 unique_ptr<Expression> &right() { return right_; }
432
433private:
434 RC calc_value(const Value &left_value, const Value &right_value, Value &value) const;
435
436 RC calc_column(const Column &left_column, const Column &right_column, Column &column) const;
437
438 template <bool LEFT_CONSTANT, bool RIGHT_CONSTANT>
439 RC execute_calc(const Column &left, const Column &right, Column &result, Type type, AttrType attr_type) const;
440
441private:
442 Type arithmetic_type_;
443 unique_ptr<Expression> left_;
444 unique_ptr<Expression> right_;
445};
446
448{
449public:
450 UnboundAggregateExpr(const char *aggregate_name, Expression *child);
451 UnboundAggregateExpr(const char *aggregate_name, unique_ptr<Expression> child);
452 virtual ~UnboundAggregateExpr() = default;
453
454 ExprType type() const override { return ExprType::UNBOUND_AGGREGATION; }
455
456 unique_ptr<Expression> copy() const override
457 {
458 return make_unique<UnboundAggregateExpr>(aggregate_name_.c_str(), child_->copy());
459 }
460
461 const char *aggregate_name() const { return aggregate_name_.c_str(); }
462
463 unique_ptr<Expression> &child() { return child_; }
464
465 RC get_value(const Tuple &tuple, Value &value) const override { return RC::INTERNAL; }
466 AttrType value_type() const override { return child_->value_type(); }
467
468private:
469 string aggregate_name_;
470 unique_ptr<Expression> child_;
471};
472
474{
475public:
476 enum class Type
477 {
478 COUNT,
479 SUM,
480 AVG,
481 MAX,
482 MIN,
483 };
484
485public:
486 AggregateExpr(Type type, Expression *child);
487 AggregateExpr(Type type, unique_ptr<Expression> child);
488 virtual ~AggregateExpr() = default;
489
490 bool equal(const Expression &other) const override;
491
492 unique_ptr<Expression> copy() const override { return make_unique<AggregateExpr>(aggregate_type_, child_->copy()); }
493
494 ExprType type() const override { return ExprType::AGGREGATION; }
495
496 AttrType value_type() const override { return child_->value_type(); }
497 int value_length() const override { return child_->value_length(); }
498
499 RC get_value(const Tuple &tuple, Value &value) const override;
500
501 RC get_column(Chunk &chunk, Column &column) override;
502
503 Type aggregate_type() const { return aggregate_type_; }
504
505 unique_ptr<Expression> &child() { return child_; }
506
507 const unique_ptr<Expression> &child() const { return child_; }
508
509 unique_ptr<Aggregator> create_aggregator() const;
510
511public:
512 static RC type_from_string(const char *type_str, Type &type);
513
514private:
515 Type aggregate_type_;
516 unique_ptr<Expression> child_;
517};
Definition: expression.h:474
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:494
int value_length() const override
表达式值的长度
Definition: expression.h:497
RC get_column(Chunk &chunk, Column &column) override
从 chunk 中获取表达式的计算结果 column
Definition: expression.cpp:530
bool equal(const Expression &other) const override
判断两个表达式是否相等
Definition: expression.cpp:541
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:492
AttrType value_type() const override
表达式值的类型
Definition: expression.h:496
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:569
算术表达式
Definition: expression.h:385
RC get_column(Chunk &chunk, Column &column) override
从 chunk 中获取表达式的计算结果 column
Definition: expression.cpp:443
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:423
RC try_get_value(Value &value) const override
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.cpp:490
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:411
int value_length() const override
表达式值的长度
Definition: expression.h:414
AttrType value_type() const override
表达式值的类型
Definition: expression.cpp:305
bool equal(const Expression &other) const override
判断两个表达式是否相等
Definition: expression.cpp:293
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:401
类型转换表达式
Definition: expression.h:264
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:271
unique_ptr< Expression > child_
从这个表达式转换
Definition: expression.h:285
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:269
AttrType value_type() const override
表达式值的类型
Definition: expression.h:277
AttrType cast_type_
想要转换成这个类型
Definition: expression.h:286
RC try_get_value(Value &value) const override
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.cpp:102
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:22
比较表达式
Definition: expression.h:294
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:176
AttrType value_type() const override
表达式值的类型
Definition: expression.h:301
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:304
RC eval(Chunk &chunk, vector< uint8_t > &select) override
根据 ComparisonExpr 获得 select 结果。 select 的长度与chunk 的行数相同,表示每一行在ComparisonExpr 计算后是否会被输出。
Definition: expression.cpp:201
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:299
RC compare_value(const Value &left, const Value &right, bool &value) const
Definition: expression.cpp:122
RC try_get_value(Value &value) const override
Definition: expression.cpp:155
联结表达式多个表达式使用同一种关系(AND或OR)来联结 当前miniob仅有AND操作
Definition: expression.h:346
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:257
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:358
AttrType value_type() const override
表达式值的类型
Definition: expression.h:368
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:367
表达式的抽象描述
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:42
元组的抽象描述
Definition: tuple.h:66
Definition: expression.h:448
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.h:465
AttrType value_type() const override
表达式值的类型
Definition: expression.h:466
unique_ptr< Expression > copy() const override
复制表达式
Definition: expression.h:456
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:454
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:30
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:46