This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathtuple_value_expression.h
214 lines (169 loc) · 6.52 KB
/
tuple_value_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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// tuple_value_expression.h
//
// Identification: src/include/expression/tuple_value_expression.h
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#pragma once
#include "expression/abstract_expression.h"
#include "common/internal_types.h"
#include "common/logger.h"
#include "common/sql_node_visitor.h"
#include "planner/binding_context.h"
#include "util/string_util.h"
namespace peloton {
namespace expression {
//===----------------------------------------------------------------------===//
// TupleValueExpression
//===----------------------------------------------------------------------===//
class TupleValueExpression : public AbstractExpression {
public:
TupleValueExpression(std::string &&col_name)
: AbstractExpression(ExpressionType::VALUE_TUPLE, type::TypeId::INVALID),
value_idx_(-1),
tuple_idx_(-1),
col_name_(StringUtil::Lower(col_name)),
ai_(nullptr) {}
TupleValueExpression(std::string &&col_name, std::string &&table_name)
: AbstractExpression(ExpressionType::VALUE_TUPLE, type::TypeId::INVALID),
value_idx_(-1),
tuple_idx_(-1),
table_name_(StringUtil::Lower(table_name)),
col_name_(StringUtil::Lower(col_name)),
ai_(nullptr) {}
TupleValueExpression(type::TypeId type_id, const int tuple_idx,
const int value_idx)
: AbstractExpression(ExpressionType::VALUE_TUPLE, type_id),
value_idx_(value_idx),
tuple_idx_(tuple_idx),
ai_(nullptr) {}
~TupleValueExpression() {}
virtual type::Value Evaluate(
const AbstractTuple *tuple1, const AbstractTuple *tuple2,
executor::ExecutorContext *context) const override;
virtual void DeduceExpressionName() override {
if (!alias.empty()) return;
expr_name_ = col_name_;
}
// TODO: Delete this when TransformExpression is completely depracated
void SetTupleValueExpressionParams(type::TypeId type_id, int value_idx,
int tuple_idx) {
return_value_type_ = type_id;
value_idx_ = value_idx;
tuple_idx_ = tuple_idx;
}
inline void SetValueType(type::TypeId type_id) {
return_value_type_ = type_id;
}
inline void SetValueIdx(int value_idx, int tuple_idx = 0) {
value_idx_ = value_idx;
tuple_idx_ = tuple_idx;
}
inline void SetIsNotNull(bool is_not_null) {
is_not_null_ = is_not_null;
}
/**
* @brief Attribute binding
* @param binding_contexts
*/
void PerformBinding(const std::vector<const planner::BindingContext *> &
binding_contexts) override;
// Return the attributes this expression uses
void GetUsedAttributes(std::unordered_set<const planner::AttributeInfo *> &
attributes) const override {
PELOTON_ASSERT(GetAttributeRef() != nullptr);
attributes.insert(GetAttributeRef());
}
AbstractExpression *Copy() const override {
return new TupleValueExpression(*this);
}
virtual bool operator==(const AbstractExpression &rhs) const override {
return ExactlyEquals(rhs);
}
virtual bool operator!=(const AbstractExpression &rhs) const override {
return !(*this == rhs);
}
virtual bool ExactlyEquals(const AbstractExpression &rhs) const override {
if (exp_type_ != rhs.GetExpressionType()) return false;
auto &other = static_cast<const TupleValueExpression &>(rhs);
// For query like SELECT A.id, B.id FROM test AS A, test AS B;
// we need to know whether A.id is from A.id or B.id. In this case,
// A.id and B.id have the same bound oids since they refer to the same table
// but they have different table alias.
if ((table_name_.empty() xor other.table_name_.empty()) ||
col_name_.empty() xor other.col_name_.empty())
return false;
if (GetIsNotNull() != other.GetIsNotNull())
return false;
bool res = bound_obj_id_ == other.bound_obj_id_;
if (!table_name_.empty() && !other.table_name_.empty())
res = table_name_ == other.table_name_ && res;
if (!col_name_.empty() && !other.col_name_.empty())
res = col_name_ == other.col_name_ && res;
return res;
}
virtual hash_t Hash() const override { return HashForExactMatch(); }
virtual hash_t HashForExactMatch() const override;
const planner::AttributeInfo *GetAttributeRef() const { return ai_; }
int GetColumnId() const { return value_idx_; }
int GetTupleId() const { return tuple_idx_; }
std::string GetTableName() const { return table_name_; }
std::string GetColumnName() const { return col_name_; }
std::string GetColFullName() const {
if (!table_name_.empty()) {
return table_name_ + "." + col_name_;
}
return col_name_;
}
void SetTableName(std::string table_alias) { table_name_ = table_alias; }
void SetColName(std::string col_name) { col_name_ = col_name; }
bool GetIsBound() const { return is_bound_; }
bool GetIsNotNull() const { return is_not_null_; }
const std::tuple<oid_t, oid_t, oid_t> &GetBoundOid() const {
return bound_obj_id_;
}
void SetBoundOid(oid_t db_id, oid_t table_id, oid_t col_id) {
bound_obj_id_ = std::make_tuple(db_id, table_id, col_id);
is_bound_ = true;
}
void SetBoundOid(std::tuple<oid_t, oid_t, oid_t> &bound_oid) {
bound_obj_id_ = bound_oid;
is_bound_ = true;
}
std::tuple<oid_t, oid_t, oid_t> GetBoundOid() {
PELOTON_ASSERT(is_bound_);
return bound_obj_id_;
}
virtual void Accept(SqlNodeVisitor *v) override { v->Visit(this); }
const std::string GetInfo(int num_indent) const override;
const std::string GetInfo() const override;
bool IsNullable() const override;
protected:
TupleValueExpression(const TupleValueExpression &other)
: AbstractExpression(other),
is_bound_(other.is_bound_),
bound_obj_id_(other.bound_obj_id_),
value_idx_(other.value_idx_),
tuple_idx_(other.tuple_idx_),
table_name_(other.table_name_),
col_name_(other.col_name_),
is_not_null_(other.is_not_null_) {}
// Bound flag
bool is_bound_ = false;
// Bound ids. Init to INVALID_OID
std::tuple<oid_t, oid_t, oid_t> bound_obj_id_ =
std::make_tuple(INVALID_OID, INVALID_OID, INVALID_OID);
int value_idx_;
int tuple_idx_;
std::string table_name_;
std::string col_name_;
bool is_not_null_ = false;
const planner::AttributeInfo *ai_;
};
} // namespace expression
} // namespace peloton