OmniSciDB  04ee39c94c
ScalarExprVisitor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef QUERYENGINE_SCALAREXPRVISITOR_H
18 #define QUERYENGINE_SCALAREXPRVISITOR_H
19 
20 #include "../Analyzer/Analyzer.h"
21 
22 template <class T>
24  public:
25  T visit(const Analyzer::Expr* expr) const {
26  CHECK(expr);
27  const auto var = dynamic_cast<const Analyzer::Var*>(expr);
28  if (var) {
29  return visitVar(var);
30  }
31  const auto column_var = dynamic_cast<const Analyzer::ColumnVar*>(expr);
32  if (column_var) {
33  return visitColumnVar(column_var);
34  }
35  const auto column_var_tuple = dynamic_cast<const Analyzer::ExpressionTuple*>(expr);
36  if (column_var_tuple) {
37  return visitColumnVarTuple(column_var_tuple);
38  }
39  const auto constant = dynamic_cast<const Analyzer::Constant*>(expr);
40  if (constant) {
41  return visitConstant(constant);
42  }
43  const auto uoper = dynamic_cast<const Analyzer::UOper*>(expr);
44  if (uoper) {
45  return visitUOper(uoper);
46  }
47  const auto bin_oper = dynamic_cast<const Analyzer::BinOper*>(expr);
48  if (bin_oper) {
49  return visitBinOper(bin_oper);
50  }
51  const auto in_values = dynamic_cast<const Analyzer::InValues*>(expr);
52  if (in_values) {
53  return visitInValues(in_values);
54  }
55  const auto in_integer_set = dynamic_cast<const Analyzer::InIntegerSet*>(expr);
56  if (in_integer_set) {
57  return visitInIntegerSet(in_integer_set);
58  }
59  const auto char_length = dynamic_cast<const Analyzer::CharLengthExpr*>(expr);
60  if (char_length) {
62  }
63  const auto key_for_string = dynamic_cast<const Analyzer::KeyForStringExpr*>(expr);
64  if (key_for_string) {
65  return visitKeyForString(key_for_string);
66  }
67  const auto cardinality = dynamic_cast<const Analyzer::CardinalityExpr*>(expr);
68  if (cardinality) {
69  return visitCardinality(cardinality);
70  }
71  const auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr);
72  if (like_expr) {
73  return visitLikeExpr(like_expr);
74  }
75  const auto regexp_expr = dynamic_cast<const Analyzer::RegexpExpr*>(expr);
76  if (regexp_expr) {
77  return visitRegexpExpr(regexp_expr);
78  }
79  const auto case_ = dynamic_cast<const Analyzer::CaseExpr*>(expr);
80  if (case_) {
81  return visitCaseExpr(case_);
82  }
83  const auto datetrunc = dynamic_cast<const Analyzer::DatetruncExpr*>(expr);
84  if (datetrunc) {
85  return visitDatetruncExpr(datetrunc);
86  }
87  const auto extract = dynamic_cast<const Analyzer::ExtractExpr*>(expr);
88  if (extract) {
89  return visitExtractExpr(extract);
90  }
91  const auto window_func = dynamic_cast<const Analyzer::WindowFunction*>(expr);
92  if (window_func) {
93  return visitWindowFunction(window_func);
94  }
95  const auto func_with_custom_type_handling =
96  dynamic_cast<const Analyzer::FunctionOperWithCustomTypeHandling*>(expr);
97  if (func_with_custom_type_handling) {
98  return visitFunctionOperWithCustomTypeHandling(func_with_custom_type_handling);
99  }
100  const auto func = dynamic_cast<const Analyzer::FunctionOper*>(expr);
101  if (func) {
102  return visitFunctionOper(func);
103  }
104  const auto array = dynamic_cast<const Analyzer::ArrayExpr*>(expr);
105  if (array) {
106  return visitArrayOper(array);
107  }
108  const auto datediff = dynamic_cast<const Analyzer::DatediffExpr*>(expr);
109  if (datediff) {
110  return visitDatediffExpr(datediff);
111  }
112  const auto dateadd = dynamic_cast<const Analyzer::DateaddExpr*>(expr);
113  if (dateadd) {
114  return visitDateaddExpr(dateadd);
115  }
116  const auto likelihood = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
117  if (likelihood) {
118  return visitLikelihood(likelihood);
119  }
120  const auto offset_in_fragment = dynamic_cast<const Analyzer::OffsetInFragment*>(expr);
121  if (offset_in_fragment) {
122  return visitOffsetInFragment(offset_in_fragment);
123  }
124  const auto agg = dynamic_cast<const Analyzer::AggExpr*>(expr);
125  if (agg) {
126  return visitAggExpr(agg);
127  }
128  return defaultResult();
129  }
130 
131  protected:
132  virtual T visitVar(const Analyzer::Var*) const { return defaultResult(); }
133 
134  virtual T visitColumnVar(const Analyzer::ColumnVar*) const { return defaultResult(); }
135 
137  return defaultResult();
138  }
139 
140  virtual T visitConstant(const Analyzer::Constant*) const { return defaultResult(); }
141 
142  virtual T visitUOper(const Analyzer::UOper* uoper) const {
143  T result = defaultResult();
144  result = aggregateResult(result, visit(uoper->get_operand()));
145  return result;
146  }
147 
148  virtual T visitBinOper(const Analyzer::BinOper* bin_oper) const {
149  T result = defaultResult();
150  result = aggregateResult(result, visit(bin_oper->get_left_operand()));
151  result = aggregateResult(result, visit(bin_oper->get_right_operand()));
152  return result;
153  }
154 
155  virtual T visitInValues(const Analyzer::InValues* in_values) const {
156  T result = visit(in_values->get_arg());
157  const auto& value_list = in_values->get_value_list();
158  for (const auto in_value : value_list) {
159  result = aggregateResult(result, visit(in_value.get()));
160  }
161  return result;
162  }
163 
164  virtual T visitInIntegerSet(const Analyzer::InIntegerSet* in_integer_set) const {
165  return visit(in_integer_set->get_arg());
166  }
167 
169  T result = defaultResult();
170  result = aggregateResult(result, visit(char_length->get_arg()));
171  return result;
172  }
173 
174  virtual T visitKeyForString(const Analyzer::KeyForStringExpr* key_for_string) const {
175  T result = defaultResult();
176  result = aggregateResult(result, visit(key_for_string->get_arg()));
177  return result;
178  }
179 
180  virtual T visitCardinality(const Analyzer::CardinalityExpr* cardinality) const {
181  T result = defaultResult();
182  result = aggregateResult(result, visit(cardinality->get_arg()));
183  return result;
184  }
185 
186  virtual T visitLikeExpr(const Analyzer::LikeExpr* like) const {
187  T result = defaultResult();
188  result = aggregateResult(result, visit(like->get_arg()));
189  result = aggregateResult(result, visit(like->get_like_expr()));
190  if (like->get_escape_expr()) {
191  result = aggregateResult(result, visit(like->get_escape_expr()));
192  }
193  return result;
194  }
195 
196  virtual T visitRegexpExpr(const Analyzer::RegexpExpr* regexp) const {
197  T result = defaultResult();
198  result = aggregateResult(result, visit(regexp->get_arg()));
199  result = aggregateResult(result, visit(regexp->get_pattern_expr()));
200  if (regexp->get_escape_expr()) {
201  result = aggregateResult(result, visit(regexp->get_escape_expr()));
202  }
203  return result;
204  }
205 
206  virtual T visitCaseExpr(const Analyzer::CaseExpr* case_) const {
207  T result = defaultResult();
208  const auto& expr_pair_list = case_->get_expr_pair_list();
209  for (const auto& expr_pair : expr_pair_list) {
210  result = aggregateResult(result, visit(expr_pair.first.get()));
211  result = aggregateResult(result, visit(expr_pair.second.get()));
212  }
213  result = aggregateResult(result, visit(case_->get_else_expr()));
214  return result;
215  }
216 
217  virtual T visitDatetruncExpr(const Analyzer::DatetruncExpr* datetrunc) const {
218  T result = defaultResult();
219  result = aggregateResult(result, visit(datetrunc->get_from_expr()));
220  return result;
221  }
222 
223  virtual T visitExtractExpr(const Analyzer::ExtractExpr* extract) const {
224  T result = defaultResult();
225  result = aggregateResult(result, visit(extract->get_from_expr()));
226  return result;
227  }
228 
230  const Analyzer::FunctionOperWithCustomTypeHandling* func_oper) const {
231  return visitFunctionOper(func_oper);
232  }
233 
234  virtual T visitArrayOper(Analyzer::ArrayExpr const* array_expr) const {
235  T result = defaultResult();
236  for (size_t i = 0; i < array_expr->getElementCount(); ++i) {
237  result = aggregateResult(result, visit(array_expr->getElement(i)));
238  }
239  return result;
240  }
241 
242  virtual T visitFunctionOper(const Analyzer::FunctionOper* func_oper) const {
243  T result = defaultResult();
244  for (size_t i = 0; i < func_oper->getArity(); ++i) {
245  result = aggregateResult(result, visit(func_oper->getArg(i)));
246  }
247  return result;
248  }
249 
250  virtual T visitWindowFunction(const Analyzer::WindowFunction* window_func) const {
251  T result = defaultResult();
252  for (const auto& arg : window_func->getArgs()) {
253  result = aggregateResult(result, visit(arg.get()));
254  }
255  for (const auto& partition_key : window_func->getPartitionKeys()) {
256  result = aggregateResult(result, visit(partition_key.get()));
257  }
258  for (const auto& order_key : window_func->getOrderKeys()) {
259  result = aggregateResult(result, visit(order_key.get()));
260  }
261  return result;
262  }
263 
264  virtual T visitDatediffExpr(const Analyzer::DatediffExpr* datediff) const {
265  T result = defaultResult();
266  result = aggregateResult(result, visit(datediff->get_start_expr()));
267  result = aggregateResult(result, visit(datediff->get_end_expr()));
268  return result;
269  }
270 
271  virtual T visitDateaddExpr(const Analyzer::DateaddExpr* dateadd) const {
272  T result = defaultResult();
273  result = aggregateResult(result, visit(dateadd->get_number_expr()));
274  result = aggregateResult(result, visit(dateadd->get_datetime_expr()));
275  return result;
276  }
277 
278  virtual T visitLikelihood(const Analyzer::LikelihoodExpr* likelihood) const {
279  return visit(likelihood->get_arg());
280  }
281 
283  return defaultResult();
284  }
285 
286  virtual T visitAggExpr(const Analyzer::AggExpr* agg) const {
287  T result = defaultResult();
288  return aggregateResult(result, visit(agg->get_arg()));
289  }
290 
291  protected:
292  virtual T aggregateResult(const T& aggregate, const T& next_result) const {
293  return next_result;
294  }
295 
296  virtual T defaultResult() const { return T{}; }
297 };
298 
299 #endif // QUERYENGINE_SCALAREXPRVISITOR_H
const Expr * get_arg() const
Definition: Analyzer.h:577
virtual T visitInValues(const Analyzer::InValues *in_values) const
size_t getElementCount() const
Definition: Analyzer.h:1380
virtual T visitInIntegerSet(const Analyzer::InIntegerSet *in_integer_set) const
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1131
const Expr * get_arg() const
Definition: Analyzer.h:626
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:1254
virtual T visitOffsetInFragment(const Analyzer::OffsetInFragment *) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getOrderKeys() const
Definition: Analyzer.h:1347
virtual T defaultResult() const
const Expr * get_start_expr() const
Definition: Analyzer.h:1172
virtual T visitCaseExpr(const Analyzer::CaseExpr *case_) const
virtual T visitFunctionOper(const Analyzer::FunctionOper *func_oper) const
virtual T visitCardinality(const Analyzer::CardinalityExpr *cardinality) const
size_t getArity() const
Definition: Analyzer.h:1252
const Expr * get_from_expr() const
Definition: Analyzer.h:1089
virtual T visitDateaddExpr(const Analyzer::DateaddExpr *dateadd) const
virtual T visitAggExpr(const Analyzer::AggExpr *agg) const
const Expr * get_arg() const
Definition: Analyzer.h:743
Expr * get_arg() const
Definition: Analyzer.h:988
const Expr * get_arg() const
Definition: Analyzer.h:698
const Expr * get_arg() const
Definition: Analyzer.h:797
const Expr * get_else_expr() const
Definition: Analyzer.h:1044
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:1384
ALWAYS_INLINE DEVICE int32_t char_length(const char *str, const int32_t str_len)
virtual T visitFunctionOperWithCustomTypeHandling(const Analyzer::FunctionOperWithCustomTypeHandling *func_oper) const
const Expr * get_from_expr() const
Definition: Analyzer.h:1214
virtual T visitDatetruncExpr(const Analyzer::DatetruncExpr *datetrunc) const
const Expr * get_escape_expr() const
Definition: Analyzer.h:800
virtual T visitKeyForString(const Analyzer::KeyForStringExpr *key_for_string) const
virtual T visitVar(const Analyzer::Var *) const
virtual T visitRegexpExpr(const Analyzer::RegexpExpr *regexp) const
virtual T visitDatediffExpr(const Analyzer::DatediffExpr *datediff) const
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1041
virtual T visitColumnVar(const Analyzer::ColumnVar *) const
virtual T visitLikelihood(const Analyzer::LikelihoodExpr *likelihood) const
virtual T visitCharLength(const Analyzer::CharLengthExpr *char_length) const
virtual T visitArrayOper(Analyzer::ArrayExpr const *array_expr) const
virtual T visitColumnVarTuple(const Analyzer::ExpressionTuple *) const
virtual T visitUOper(const Analyzer::UOper *uoper) const
virtual T visitWindowFunction(const Analyzer::WindowFunction *window_func) const
const Expr * get_arg() const
Definition: Analyzer.h:869
const Expr * get_pattern_expr() const
Definition: Analyzer.h:871
const Expr * get_escape_expr() const
Definition: Analyzer.h:872
virtual T visitConstant(const Analyzer::Constant *) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:1343
const Expr * get_arg() const
Definition: Analyzer.h:925
T visit(const Analyzer::Expr *expr) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1341
virtual T visitExtractExpr(const Analyzer::ExtractExpr *extract) const
virtual T visitBinOper(const Analyzer::BinOper *bin_oper) const
#define CHECK(condition)
Definition: Logger.h:187
const Expr * get_arg() const
Definition: Analyzer.h:649
virtual T aggregateResult(const T &aggregate, const T &next_result) const
const Expr * get_like_expr() const
Definition: Analyzer.h:799
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:579
virtual T visitLikeExpr(const Analyzer::LikeExpr *like) const
const Expr * get_end_expr() const
Definition: Analyzer.h:1173
const Expr * get_number_expr() const
Definition: Analyzer.h:1130
const Expr * get_right_operand() const
Definition: Analyzer.h:436
const Expr * get_left_operand() const
Definition: Analyzer.h:435
const Expr * get_operand() const
Definition: Analyzer.h:364