OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 geo_expr = dynamic_cast<const Analyzer::GeoExpr*>(expr);
52  if (geo_expr) {
53  return visitGeoExpr(geo_expr);
54  }
55  const auto in_values = dynamic_cast<const Analyzer::InValues*>(expr);
56  if (in_values) {
57  return visitInValues(in_values);
58  }
59  const auto in_integer_set = dynamic_cast<const Analyzer::InIntegerSet*>(expr);
60  if (in_integer_set) {
61  return visitInIntegerSet(in_integer_set);
62  }
63  const auto char_length = dynamic_cast<const Analyzer::CharLengthExpr*>(expr);
64  if (char_length) {
66  }
67  const auto key_for_string = dynamic_cast<const Analyzer::KeyForStringExpr*>(expr);
68  if (key_for_string) {
69  return visitKeyForString(key_for_string);
70  }
71  const auto sample_ratio = dynamic_cast<const Analyzer::SampleRatioExpr*>(expr);
72  if (sample_ratio) {
74  }
75  const auto lower = dynamic_cast<const Analyzer::LowerExpr*>(expr);
76  if (lower) {
77  return visitLower(lower);
78  }
79  const auto cardinality = dynamic_cast<const Analyzer::CardinalityExpr*>(expr);
80  if (cardinality) {
81  return visitCardinality(cardinality);
82  }
83  const auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr);
84  if (like_expr) {
85  return visitLikeExpr(like_expr);
86  }
87  const auto regexp_expr = dynamic_cast<const Analyzer::RegexpExpr*>(expr);
88  if (regexp_expr) {
89  return visitRegexpExpr(regexp_expr);
90  }
91  const auto case_ = dynamic_cast<const Analyzer::CaseExpr*>(expr);
92  if (case_) {
93  return visitCaseExpr(case_);
94  }
95  const auto datetrunc = dynamic_cast<const Analyzer::DatetruncExpr*>(expr);
96  if (datetrunc) {
97  return visitDatetruncExpr(datetrunc);
98  }
99  const auto extract = dynamic_cast<const Analyzer::ExtractExpr*>(expr);
100  if (extract) {
101  return visitExtractExpr(extract);
102  }
103  const auto window_func = dynamic_cast<const Analyzer::WindowFunction*>(expr);
104  if (window_func) {
105  return visitWindowFunction(window_func);
106  }
107  const auto func_with_custom_type_handling =
108  dynamic_cast<const Analyzer::FunctionOperWithCustomTypeHandling*>(expr);
109  if (func_with_custom_type_handling) {
110  return visitFunctionOperWithCustomTypeHandling(func_with_custom_type_handling);
111  }
112  const auto func = dynamic_cast<const Analyzer::FunctionOper*>(expr);
113  if (func) {
114  return visitFunctionOper(func);
115  }
116  const auto array = dynamic_cast<const Analyzer::ArrayExpr*>(expr);
117  if (array) {
118  return visitArrayOper(array);
119  }
120  const auto geo_uop = dynamic_cast<const Analyzer::GeoUOper*>(expr);
121  if (geo_uop) {
122  return visitGeoUOper(geo_uop);
123  }
124  const auto geo_binop = dynamic_cast<const Analyzer::GeoBinOper*>(expr);
125  if (geo_binop) {
126  return visitGeoBinOper(geo_binop);
127  }
128  const auto datediff = dynamic_cast<const Analyzer::DatediffExpr*>(expr);
129  if (datediff) {
130  return visitDatediffExpr(datediff);
131  }
132  const auto dateadd = dynamic_cast<const Analyzer::DateaddExpr*>(expr);
133  if (dateadd) {
134  return visitDateaddExpr(dateadd);
135  }
136  const auto likelihood = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
137  if (likelihood) {
138  return visitLikelihood(likelihood);
139  }
140  const auto offset_in_fragment = dynamic_cast<const Analyzer::OffsetInFragment*>(expr);
141  if (offset_in_fragment) {
142  return visitOffsetInFragment(offset_in_fragment);
143  }
144  const auto agg = dynamic_cast<const Analyzer::AggExpr*>(expr);
145  if (agg) {
146  return visitAggExpr(agg);
147  }
148  return defaultResult();
149  }
150 
151  protected:
152  virtual T visitVar(const Analyzer::Var*) const { return defaultResult(); }
153 
154  virtual T visitColumnVar(const Analyzer::ColumnVar*) const { return defaultResult(); }
155 
157  return defaultResult();
158  }
159 
160  virtual T visitConstant(const Analyzer::Constant*) const { return defaultResult(); }
161 
162  virtual T visitUOper(const Analyzer::UOper* uoper) const {
163  T result = defaultResult();
164  result = aggregateResult(result, visit(uoper->get_operand()));
165  return result;
166  }
167 
168  virtual T visitBinOper(const Analyzer::BinOper* bin_oper) const {
169  T result = defaultResult();
170  result = aggregateResult(result, visit(bin_oper->get_left_operand()));
171  result = aggregateResult(result, visit(bin_oper->get_right_operand()));
172  return result;
173  }
174 
175  virtual T visitGeoExpr(const Analyzer::GeoExpr* geo_expr) const {
176  return defaultResult();
177  }
178 
179  virtual T visitInValues(const Analyzer::InValues* in_values) const {
180  T result = visit(in_values->get_arg());
181  const auto& value_list = in_values->get_value_list();
182  for (const auto& in_value : value_list) {
183  result = aggregateResult(result, visit(in_value.get()));
184  }
185  return result;
186  }
187 
188  virtual T visitInIntegerSet(const Analyzer::InIntegerSet* in_integer_set) const {
189  return visit(in_integer_set->get_arg());
190  }
191 
193  T result = defaultResult();
194  result = aggregateResult(result, visit(char_length->get_arg()));
195  return result;
196  }
197 
198  virtual T visitKeyForString(const Analyzer::KeyForStringExpr* key_for_string) const {
199  T result = defaultResult();
200  result = aggregateResult(result, visit(key_for_string->get_arg()));
201  return result;
202  }
203 
205  T result = defaultResult();
206  result = aggregateResult(result, visit(sample_ratio->get_arg()));
207  return result;
208  }
209 
210  virtual T visitLower(const Analyzer::LowerExpr* lower_expr) const {
211  return visit(lower_expr->get_arg());
212  }
213 
214  virtual T visitCardinality(const Analyzer::CardinalityExpr* cardinality) const {
215  T result = defaultResult();
216  result = aggregateResult(result, visit(cardinality->get_arg()));
217  return result;
218  }
219 
220  virtual T visitLikeExpr(const Analyzer::LikeExpr* like) const {
221  T result = defaultResult();
222  result = aggregateResult(result, visit(like->get_arg()));
223  result = aggregateResult(result, visit(like->get_like_expr()));
224  if (like->get_escape_expr()) {
225  result = aggregateResult(result, visit(like->get_escape_expr()));
226  }
227  return result;
228  }
229 
230  virtual T visitRegexpExpr(const Analyzer::RegexpExpr* regexp) const {
231  T result = defaultResult();
232  result = aggregateResult(result, visit(regexp->get_arg()));
233  result = aggregateResult(result, visit(regexp->get_pattern_expr()));
234  if (regexp->get_escape_expr()) {
235  result = aggregateResult(result, visit(regexp->get_escape_expr()));
236  }
237  return result;
238  }
239 
240  virtual T visitCaseExpr(const Analyzer::CaseExpr* case_) const {
241  T result = defaultResult();
242  const auto& expr_pair_list = case_->get_expr_pair_list();
243  for (const auto& expr_pair : expr_pair_list) {
244  result = aggregateResult(result, visit(expr_pair.first.get()));
245  result = aggregateResult(result, visit(expr_pair.second.get()));
246  }
247  result = aggregateResult(result, visit(case_->get_else_expr()));
248  return result;
249  }
250 
251  virtual T visitDatetruncExpr(const Analyzer::DatetruncExpr* datetrunc) const {
252  T result = defaultResult();
253  result = aggregateResult(result, visit(datetrunc->get_from_expr()));
254  return result;
255  }
256 
257  virtual T visitExtractExpr(const Analyzer::ExtractExpr* extract) const {
258  T result = defaultResult();
259  result = aggregateResult(result, visit(extract->get_from_expr()));
260  return result;
261  }
262 
264  const Analyzer::FunctionOperWithCustomTypeHandling* func_oper) const {
265  return visitFunctionOper(func_oper);
266  }
267 
268  virtual T visitArrayOper(Analyzer::ArrayExpr const* array_expr) const {
269  T result = defaultResult();
270  for (size_t i = 0; i < array_expr->getElementCount(); ++i) {
271  result = aggregateResult(result, visit(array_expr->getElement(i)));
272  }
273  return result;
274  }
275 
276  virtual T visitGeoUOper(const Analyzer::GeoUOper* geo_expr) const {
277  T result = defaultResult();
278  for (const auto& arg : geo_expr->getArgs0()) {
279  result = aggregateResult(result, visit(arg.get()));
280  }
281  return result;
282  }
283 
284  virtual T visitGeoBinOper(const Analyzer::GeoBinOper* geo_expr) const {
285  T result = defaultResult();
286  for (const auto& arg : geo_expr->getArgs0()) {
287  result = aggregateResult(result, visit(arg.get()));
288  }
289  for (const auto& arg : geo_expr->getArgs1()) {
290  result = aggregateResult(result, visit(arg.get()));
291  }
292  return result;
293  }
294 
295  virtual T visitFunctionOper(const Analyzer::FunctionOper* func_oper) const {
296  T result = defaultResult();
297  for (size_t i = 0; i < func_oper->getArity(); ++i) {
298  result = aggregateResult(result, visit(func_oper->getArg(i)));
299  }
300  return result;
301  }
302 
303  virtual T visitWindowFunction(const Analyzer::WindowFunction* window_func) const {
304  T result = defaultResult();
305  for (const auto& arg : window_func->getArgs()) {
306  result = aggregateResult(result, visit(arg.get()));
307  }
308  for (const auto& partition_key : window_func->getPartitionKeys()) {
309  result = aggregateResult(result, visit(partition_key.get()));
310  }
311  for (const auto& order_key : window_func->getOrderKeys()) {
312  result = aggregateResult(result, visit(order_key.get()));
313  }
314  return result;
315  }
316 
317  virtual T visitDatediffExpr(const Analyzer::DatediffExpr* datediff) const {
318  T result = defaultResult();
319  result = aggregateResult(result, visit(datediff->get_start_expr()));
320  result = aggregateResult(result, visit(datediff->get_end_expr()));
321  return result;
322  }
323 
324  virtual T visitDateaddExpr(const Analyzer::DateaddExpr* dateadd) const {
325  T result = defaultResult();
326  result = aggregateResult(result, visit(dateadd->get_number_expr()));
327  result = aggregateResult(result, visit(dateadd->get_datetime_expr()));
328  return result;
329  }
330 
331  virtual T visitLikelihood(const Analyzer::LikelihoodExpr* likelihood) const {
332  return visit(likelihood->get_arg());
333  }
334 
336  return defaultResult();
337  }
338 
339  virtual T visitAggExpr(const Analyzer::AggExpr* agg) const {
340  T result = defaultResult();
341  return aggregateResult(result, visit(agg->get_arg()));
342  }
343 
344  protected:
345  virtual T aggregateResult(const T& aggregate, const T& next_result) const {
346  return next_result;
347  }
348 
349  virtual T defaultResult() const { return T{}; }
350 };
351 
352 #endif // QUERYENGINE_SCALAREXPRVISITOR_H
virtual T visitAggExpr(const Analyzer::AggExpr *agg) const
const Expr * get_from_expr() const
Definition: Analyzer.h:1197
virtual T aggregateResult(const T &aggregate, const T &next_result) const
virtual T visitOffsetInFragment(const Analyzer::OffsetInFragment *) const
const Expr * get_else_expr() const
Definition: Analyzer.h:1152
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:1558
const Expr * get_escape_expr() const
Definition: Analyzer.h:908
size_t getArity() const
Definition: Analyzer.h:1360
const Expr * get_escape_expr() const
Definition: Analyzer.h:980
const Expr * get_right_operand() const
Definition: Analyzer.h:443
virtual T visitGeoBinOper(const Analyzer::GeoBinOper *geo_expr) const
const Expr * get_arg() const
Definition: Analyzer.h:977
Expr * get_arg() const
Definition: Analyzer.h:1096
const Expr * get_arg() const
Definition: Analyzer.h:1033
T visit(const Analyzer::Expr *expr) const
virtual T visitCardinality(const Analyzer::CardinalityExpr *cardinality) const
virtual T visitGeoUOper(const Analyzer::GeoUOper *geo_expr) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getOrderKeys() const
Definition: Analyzer.h:1455
virtual T visitExtractExpr(const Analyzer::ExtractExpr *extract) const
const Expr * get_arg() const
Definition: Analyzer.h:905
virtual T visitLikelihood(const Analyzer::LikelihoodExpr *likelihood) const
virtual T visitCharLength(const Analyzer::CharLengthExpr *char_length) const
virtual T visitVar(const Analyzer::Var *) const
virtual T visitGeoExpr(const Analyzer::GeoExpr *geo_expr) const
virtual T visitLikeExpr(const Analyzer::LikeExpr *like) const
ALWAYS_INLINE DEVICE int32_t char_length(const char *str, const int32_t str_len)
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1449
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:1528
virtual T visitLower(const Analyzer::LowerExpr *lower_expr) const
virtual T visitColumnVar(const Analyzer::ColumnVar *) const
virtual T visitWindowFunction(const Analyzer::WindowFunction *window_func) const
const Expr * get_start_expr() const
Definition: Analyzer.h:1280
size_t getElementCount() const
Definition: Analyzer.h:1488
virtual T visitDatediffExpr(const Analyzer::DatediffExpr *datediff) const
const Expr * get_pattern_expr() const
Definition: Analyzer.h:979
virtual T visitDateaddExpr(const Analyzer::DateaddExpr *dateadd) const
const Expr * get_from_expr() const
Definition: Analyzer.h:1322
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1239
const Expr * get_like_expr() const
Definition: Analyzer.h:907
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:1362
const Expr * get_operand() const
Definition: Analyzer.h:371
const Expr * get_arg() const
Definition: Analyzer.h:750
const Expr * get_arg() const
Definition: Analyzer.h:656
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:586
virtual T visitConstant(const Analyzer::Constant *) const
virtual T visitKeyForString(const Analyzer::KeyForStringExpr *key_for_string) const
Expression class for the LOWER (lowercase) string function. The &quot;arg&quot; constructor parameter must be a...
Definition: Analyzer.h:791
virtual T visitCaseExpr(const Analyzer::CaseExpr *case_) const
virtual T visitBinOper(const Analyzer::BinOper *bin_oper) const
const Expr * get_arg() const
Definition: Analyzer.h:705
virtual T visitInValues(const Analyzer::InValues *in_values) const
virtual T visitInIntegerSet(const Analyzer::InIntegerSet *in_integer_set) const
const Expr * get_end_expr() const
Definition: Analyzer.h:1281
#define CHECK(condition)
Definition: Logger.h:203
virtual T visitFunctionOperWithCustomTypeHandling(const Analyzer::FunctionOperWithCustomTypeHandling *func_oper) const
virtual T visitFunctionOper(const Analyzer::FunctionOper *func_oper) const
virtual T visitArrayOper(Analyzer::ArrayExpr const *array_expr) const
virtual T defaultResult() const
const Expr * get_left_operand() const
Definition: Analyzer.h:442
virtual T visitRegexpExpr(const Analyzer::RegexpExpr *regexp) const
virtual T visitSampleRatio(const Analyzer::SampleRatioExpr *sample_ratio) const
virtual T visitUOper(const Analyzer::UOper *uoper) const
virtual T visitColumnVarTuple(const Analyzer::ExpressionTuple *) const
ALWAYS_INLINE DEVICE bool sample_ratio(const double proportion, const int64_t row_offset)
const Expr * get_arg() const
Definition: Analyzer.h:633
virtual T visitDatetruncExpr(const Analyzer::DatetruncExpr *datetrunc) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:1451
const Expr * get_arg() const
Definition: Analyzer.h:851
const Expr * get_arg() const
Definition: Analyzer.h:584
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs1() const
Definition: Analyzer.h:1559
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1149
const Expr * get_arg() const
Definition: Analyzer.h:795
const Expr * get_number_expr() const
Definition: Analyzer.h:1238
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:1492