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 <string.h>
18#include <memory>
19#include <string>
20
21#include "storage/field/field.h"
22#include "sql/parser/value.h"
23#include "common/log/log.h"
24
25class Tuple;
26
36enum class ExprType
37{
38 NONE,
39 STAR,
40 FIELD,
41 VALUE,
42 CAST,
46};
47
60{
61public:
62 Expression() = default;
63 virtual ~Expression() = default;
64
68 virtual RC get_value(const Tuple &tuple, Value &value) const = 0;
69
74 virtual RC try_get_value(Value &value) const
75 {
76 return RC::UNIMPLENMENT;
77 }
78
83 virtual ExprType type() const = 0;
84
89 virtual AttrType value_type() const = 0;
90
94 virtual std::string name() const { return name_; }
95 virtual void set_name(std::string name) { name_ = name; }
96
97private:
98 std::string name_;
99};
100
105class FieldExpr : public Expression
106{
107public:
108 FieldExpr() = default;
109 FieldExpr(const Table *table, const FieldMeta *field) : field_(table, field)
110 {}
111 FieldExpr(const Field &field) : field_(field)
112 {}
113
114 virtual ~FieldExpr() = default;
115
116 ExprType type() const override { return ExprType::FIELD; }
117 AttrType value_type() const override { return field_.attr_type(); }
118
119 Field &field() { return field_; }
120
121 const Field &field() const { return field_; }
122
123 const char *table_name() const { return field_.table_name(); }
124
125 const char *field_name() const { return field_.field_name(); }
126
127 RC get_value(const Tuple &tuple, Value &value) const override;
128
129private:
130 Field field_;
131};
132
137class ValueExpr : public Expression
138{
139public:
140 ValueExpr() = default;
141 explicit ValueExpr(const Value &value) : value_(value)
142 {}
143
144 virtual ~ValueExpr() = default;
145
146 RC get_value(const Tuple &tuple, Value &value) const override;
147 RC try_get_value(Value &value) const override { value = value_; return RC::SUCCESS; }
148
149 ExprType type() const override { return ExprType::VALUE; }
150
151 AttrType value_type() const override { return value_.attr_type(); }
152
153 void get_value(Value &value) const { value = value_; }
154
155 const Value &get_value() const { return value_; }
156
157private:
158 Value value_;
159};
160
165class CastExpr : public Expression
166{
167public:
168 CastExpr(std::unique_ptr<Expression> child, AttrType cast_type);
169 virtual ~CastExpr();
170
171 ExprType type() const override
172 {
173 return ExprType::CAST;
174 }
175 RC get_value(const Tuple &tuple, Value &value) const override;
176
177 RC try_get_value(Value &value) const override;
178
179 AttrType value_type() const override { return cast_type_; }
180
181 std::unique_ptr<Expression> &child() { return child_; }
182
183private:
184 RC cast(const Value &value, Value &cast_value) const;
185
186private:
187 std::unique_ptr<Expression> child_;
188 AttrType cast_type_;
189};
190
196{
197public:
198 ComparisonExpr(CompOp comp, std::unique_ptr<Expression> left, std::unique_ptr<Expression> right);
199 virtual ~ComparisonExpr();
200
201 ExprType type() const override { return ExprType::COMPARISON; }
202
203 RC get_value(const Tuple &tuple, Value &value) const override;
204
205 AttrType value_type() const override { return BOOLEANS; }
206
207 CompOp comp() const { return comp_; }
208
209 std::unique_ptr<Expression> &left() { return left_; }
210 std::unique_ptr<Expression> &right() { return right_; }
211
216 RC try_get_value(Value &value) const override;
217
222 RC compare_value(const Value &left, const Value &right, bool &value) const;
223
224private:
225 CompOp comp_;
226 std::unique_ptr<Expression> left_;
227 std::unique_ptr<Expression> right_;
228};
229
237{
238public:
239 enum class Type {
240 AND,
241 OR,
242 };
243
244public:
245 ConjunctionExpr(Type type, std::vector<std::unique_ptr<Expression>> &children);
246 virtual ~ConjunctionExpr() = default;
247
248 ExprType type() const override { return ExprType::CONJUNCTION; }
249
250 AttrType value_type() const override { return BOOLEANS; }
251
252 RC get_value(const Tuple &tuple, Value &value) const override;
253
254 Type conjunction_type() const { return conjunction_type_; }
255
256 std::vector<std::unique_ptr<Expression>> &children() { return children_; }
257
258private:
259 Type conjunction_type_;
260 std::vector<std::unique_ptr<Expression>> children_;
261};
262
268{
269public:
270 enum class Type {
271 ADD,
272 SUB,
273 MUL,
274 DIV,
275 NEGATIVE,
276 };
277
278public:
279 ArithmeticExpr(Type type, Expression *left, Expression *right);
280 ArithmeticExpr(Type type, std::unique_ptr<Expression> left, std::unique_ptr<Expression> right);
281 virtual ~ArithmeticExpr() = default;
282
283 ExprType type() const override { return ExprType::ARITHMETIC; }
284
285 AttrType value_type() const override;
286
287 RC get_value(const Tuple &tuple, Value &value) const override;
288 RC try_get_value(Value &value) const override;
289
290 Type arithmetic_type() const { return arithmetic_type_; }
291
292 std::unique_ptr<Expression> &left() { return left_; }
293 std::unique_ptr<Expression> &right() { return right_; }
294
295private:
296 RC calc_value(const Value &left_value, const Value &right_value, Value &value) const;
297
298private:
299 Type arithmetic_type_;
300 std::unique_ptr<Expression> left_;
301 std::unique_ptr<Expression> right_;
302};
算术表达式
Definition: expression.h:268
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:288
RC try_get_value(Value &value) const override
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.cpp:308
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:283
AttrType value_type() const override
表达式值的类型
Definition: expression.cpp:208
类型转换表达式
Definition: expression.h:166
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:171
AttrType value_type() const override
表达式值的类型
Definition: expression.h:179
AttrType cast_type_
想要转换成这个类型
Definition: expression.h:188
RC try_get_value(Value &value) const override
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.cpp:70
std::unique_ptr< Expression > child_
从这个表达式转换
Definition: expression.h:187
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:60
比较表达式
Definition: expression.h:196
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:143
AttrType value_type() const override
表达式值的类型
Definition: expression.h:205
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:201
RC compare_value(const Value &left, const Value &right, bool &value) const
Definition: expression.cpp:89
RC try_get_value(Value &value) const override
Definition: expression.cpp:122
联结表达式多个表达式使用同一种关系(AND或OR)来联结 当前miniob仅有AND操作
Definition: expression.h:237
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:172
AttrType value_type() const override
表达式值的类型
Definition: expression.h:250
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:248
表达式的抽象描述
Definition: expression.h:60
virtual AttrType value_type() const =0
表达式值的类型
virtual std::string name() const
表达式的名字,比如是字段名称,或者用户在执行SQL语句时输入的内容
Definition: expression.h:94
virtual RC get_value(const Tuple &tuple, Value &value) const =0
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
virtual ExprType type() const =0
表达式的类型 可以根据表达式类型来转换为具体的子类
virtual RC try_get_value(Value &value) const
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.h:74
字段表达式
Definition: expression.h:106
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:20
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:116
AttrType value_type() const override
表达式值的类型
Definition: expression.h:117
字段元数据
Definition: field_meta.h:31
字段
Definition: field.h:25
Definition: table.h:37
元组的抽象描述
Definition: tuple.h:84
常量值表达式
Definition: expression.h:138
AttrType value_type() const override
表达式值的类型
Definition: expression.h:151
ExprType type() const override
表达式的类型 可以根据表达式类型来转换为具体的子类
Definition: expression.h:149
RC get_value(const Tuple &tuple, Value &value) const override
根据具体的tuple,来计算当前表达式的值。tuple有可能是一个具体某个表的行数据
Definition: expression.cpp:25
RC try_get_value(Value &value) const override
在没有实际运行的情况下,也就是无法获取tuple的情况下,尝试获取表达式的值
Definition: expression.h:147
属性的值
Definition: value.h:40
ExprType
表达式类型
Definition: expression.h:37
@ CAST
需要做类型转换的表达式
@ COMPARISON
需要做比较的表达式
@ FIELD
字段。在实际执行时,根据行数据内容提取对应字段的值
@ ARITHMETIC
算术运算
@ STAR
星号,表示所有字段
@ CONJUNCTION
多个表达式使用同一种关系(AND或OR)来联结
@ VALUE
常量值
CompOp
描述比较运算符
Definition: parse_defs.h:48