This repository has been archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 624
/
abstract_expression.h
344 lines (279 loc) · 9.66 KB
/
abstract_expression.h
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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// abstract_expression.h
//
// Identification: src/include/expression/abstract_expression.h
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#pragma once
#include <string>
#include "common/printable.h"
#include "planner/attribute_info.h"
#include "expression/parameter.h"
#include "codegen/query_parameters_map.h"
#include "common/internal_types.h"
#include "storage/zone_map_manager.h"
namespace peloton {
// Forward Declaration
class Printable;
class AbstractTuple;
class SqlNodeVisitor;
enum class ExpressionType;
namespace codegen {
namespace type {
class Type;
} // namespace type
} // namespace codegen
namespace executor {
class ExecutorContext;
} // namespace executor
namespace planner {
class BindingContext;
} // namespace planner
namespace type {
class Value;
} // namespace type
namespace expression {
//===----------------------------------------------------------------------===//
// AbstractExpression
//
// Predicate objects for filtering tuples during query execution.
// These objects are stored in query plans and passed to Storage Access Manager.
//
// An expression usually has a longer life cycle than an execution, because,
// for example, it can be cached and reused for several executions of the same
// query template. Moreover, those executions can run simultaneously.
// So, an expression should not store per-execution information in its states.
// An expression tree (along with the plan node tree containing it) should
// remain constant and read-only during an execution.
//===----------------------------------------------------------------------===//
class AbstractExpression : public Printable {
public:
/**
* @brief Apply the operator to the inputs and produce ouput
*
* This will be removed in the future and replaced by the
* the LLVM engine. You should not modify any Evaluate methods
* nor should you add new ones.
*
* @param tuple1
* @param tuple2
* @param context
* @return
* @deprecated
*/
virtual type::Value Evaluate(const AbstractTuple *tuple1,
const AbstractTuple *tuple2,
executor::ExecutorContext *context) const = 0;
/**
* Return true if this expression or any descendent has a value that should be
* substituted with a parameter.
*/
virtual bool HasParameter() const;
virtual bool IsNullable() const;
const AbstractExpression *GetChild(int index) const {
return GetModifiableChild(index);
}
size_t GetChildrenSize() const { return children_.size(); }
AbstractExpression *GetModifiableChild(int index) const {
if (index < 0 || index >= (int)children_.size()) {
return nullptr;
}
return children_[index].get();
}
void SetChild(int index, AbstractExpression *expr) {
if (index >= (int)children_.size()) {
children_.resize(index + 1);
}
children_[index].reset(expr);
}
void SetExpressionType(ExpressionType type) { exp_type_ = type; }
//////////////////////////////////////////////////////////////////////////////
///
/// Utilities and members for Zone Map consumption
///
//////////////////////////////////////////////////////////////////////////////
bool IsZoneMappable();
size_t GetNumberofParsedPredicates() const {
return parsed_predicates.size();
}
const std::vector<storage::PredicateInfo> *GetParsedPredicates() const {
return &parsed_predicates;
}
void ClearParsedPredicates() { parsed_predicates.clear(); }
/** accessors */
ExpressionType GetExpressionType() const { return exp_type_; }
type::TypeId GetValueType() const { return return_value_type_; }
codegen::type::Type ResultType() const;
// Attribute binding
virtual void PerformBinding(
const std::vector<const planner::BindingContext *> &binding_contexts);
// Is this expression computable using SIMD instructions?
virtual bool IsSIMDable() const {
for (uint32_t i = 0; i < GetChildrenSize(); i++) {
if (!children_[i]->IsSIMDable()) {
return false;
}
}
return true;
}
// Get all the attributes this expression uses
virtual void GetUsedAttributes(
std::unordered_set<const planner::AttributeInfo *> &attributes) const;
virtual void DeduceExpressionType() {}
// Walks the expressoin trees and generate the correct expression name
virtual void DeduceExpressionName();
virtual const std::string GetInfo(int num_indent) const;
virtual const std::string GetInfo() const;
// Equlity checks without actual values
virtual bool operator==(const AbstractExpression &rhs) const;
virtual bool operator!=(const AbstractExpression &rhs) const {
return !(*this == rhs);
}
virtual hash_t Hash() const;
// Exact match including value equality
virtual bool ExactlyEquals(const AbstractExpression &other) const;
virtual hash_t HashForExactMatch() const;
virtual void VisitParameters(
codegen::QueryParametersMap &map,
std::vector<peloton::type::Value> &values,
const std::vector<peloton::type::Value> &values_from_user) {
for (auto &child : children_) {
child->VisitParameters(map, values, values_from_user);
}
};
virtual AbstractExpression *Copy() const = 0;
//===--------------------------------------------------------------------===//
// Serialization/Deserialization
// Each sub-class will have to implement this function
//===--------------------------------------------------------------------===//
// virtual bool SerializeTo(SerializeOutput &output) const {}
// virtual bool DeserializeFrom(SerializeInput &input) const {
virtual int SerializeSize() const { return 0; }
const char *GetExpressionName() const { return expr_name_.c_str(); }
// Parser stuff
int ival_ = 0;
std::string expr_name_;
std::string alias;
bool distinct_ = false;
virtual void Accept(SqlNodeVisitor *) = 0;
virtual void AcceptChildren(SqlNodeVisitor *v) {
for (auto &child : children_) {
child->Accept(v);
}
}
/**
* @brief Derive the sub-query depth level of the current expression
*
* @return the derived depth
*/
virtual int DeriveDepth() {
if (depth_ < 0) {
for (auto &child : children_) {
auto child_depth = child->DeriveDepth();
if (child_depth >= 0 && (depth_ == -1 || child_depth < depth_))
depth_ = child_depth;
}
}
return depth_;
}
/**
* @brief Set the sub-query depth level of the current expression
*
* @param depth The depth to set
*/
void SetDepth(int depth) { depth_ = depth; }
/**
* @brief Get the sub-query depth level of the current expression
*
* @return The sub-query depth level
*/
int GetDepth() const { return depth_; }
/**
* @brief Tell if the current expression contain a sub-query
*
* @return The sub-query flag
*/
bool HasSubquery() const { return has_subquery_; }
/**
* @brief Derive if there's sub-query in the current expression
*
* @return If there is sub-query, then return true, otherwise return false
*/
virtual bool DeriveSubqueryFlag() {
if (exp_type_ == ExpressionType::ROW_SUBQUERY ||
exp_type_ == ExpressionType::SELECT_SUBQUERY) {
has_subquery_ = true;
} else {
for (auto &child : children_) {
if (child->DeriveSubqueryFlag()) {
has_subquery_ = true;
break;
}
}
}
return has_subquery_;
}
protected:
AbstractExpression(ExpressionType type) : exp_type_(type) {}
AbstractExpression(ExpressionType exp_type, type::TypeId return_value_type)
: exp_type_(exp_type), return_value_type_(return_value_type) {}
AbstractExpression(ExpressionType exp_type, type::TypeId return_value_type,
AbstractExpression *left, AbstractExpression *right)
: exp_type_(exp_type), return_value_type_(return_value_type) {
// Order of these is important!
if (left != nullptr)
children_.push_back(std::unique_ptr<AbstractExpression>(left));
// Sometimes there's no right child. E.g.: OperatorUnaryMinusExpression.
if (right != nullptr)
children_.push_back(std::unique_ptr<AbstractExpression>(right));
}
AbstractExpression(const AbstractExpression &other)
: ival_(other.ival_),
expr_name_(other.expr_name_),
distinct_(other.distinct_),
exp_type_(other.exp_type_),
return_value_type_(other.return_value_type_),
has_parameter_(other.has_parameter_),
depth_(other.depth_) {
for (auto &child : other.children_) {
children_.push_back(std::unique_ptr<AbstractExpression>(child->Copy()));
}
}
ExpressionType exp_type_ = ExpressionType::INVALID;
type::TypeId return_value_type_ = type::TypeId::INVALID;
std::vector<std::unique_ptr<AbstractExpression>> children_;
bool has_parameter_ = false;
/**
* @brief The current sub-query depth level in the current expression, -1
* stands for not derived
*/
int depth_ = -1;
/**
* @brief The flag indicating if there's sub-query in the current expression
*/
bool has_subquery_ = false;
///
std::vector<storage::PredicateInfo> parsed_predicates;
};
// Equality Comparator class for Abstract Expression
class ExprEqualCmp {
public:
inline bool operator()(AbstractExpression *expr1,
AbstractExpression *expr2) const {
return expr1->ExactlyEquals(*expr2);
}
};
// Hasher class for Abstract Expression
class ExprHasher {
public:
inline size_t operator()(AbstractExpression *expr) const {
return expr->Hash();
}
};
} // namespace expression
} // namespace peloton