OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  visitBegin();
28  const auto var = dynamic_cast<const Analyzer::Var*>(expr);
29  if (var) {
30  return visitVar(var);
31  }
32  const auto column_var = dynamic_cast<const Analyzer::ColumnVar*>(expr);
33  if (column_var) {
34  return visitColumnVar(column_var);
35  }
36  const auto column_var_tuple = dynamic_cast<const Analyzer::ExpressionTuple*>(expr);
37  if (column_var_tuple) {
38  return visitColumnVarTuple(column_var_tuple);
39  }
40  const auto constant = dynamic_cast<const Analyzer::Constant*>(expr);
41  if (constant) {
42  return visitConstant(constant);
43  }
44  const auto uoper = dynamic_cast<const Analyzer::UOper*>(expr);
45  if (uoper) {
46  return visitUOper(uoper);
47  }
48  const auto bin_oper = dynamic_cast<const Analyzer::BinOper*>(expr);
49  if (bin_oper) {
50  return visitBinOper(bin_oper);
51  }
52  const auto geo_expr = dynamic_cast<const Analyzer::GeoExpr*>(expr);
53  if (geo_expr) {
54  return visitGeoExpr(geo_expr);
55  }
56  const auto in_values = dynamic_cast<const Analyzer::InValues*>(expr);
57  if (in_values) {
58  return visitInValues(in_values);
59  }
60  const auto in_integer_set = dynamic_cast<const Analyzer::InIntegerSet*>(expr);
61  if (in_integer_set) {
62  return visitInIntegerSet(in_integer_set);
63  }
64  const auto char_length = dynamic_cast<const Analyzer::CharLengthExpr*>(expr);
65  if (char_length) {
67  }
68  const auto key_for_string = dynamic_cast<const Analyzer::KeyForStringExpr*>(expr);
69  if (key_for_string) {
70  return visitKeyForString(key_for_string);
71  }
72  const auto sample_ratio = dynamic_cast<const Analyzer::SampleRatioExpr*>(expr);
73  if (sample_ratio) {
75  }
76  const auto width_bucket = dynamic_cast<const Analyzer::WidthBucketExpr*>(expr);
77  if (width_bucket) {
79  }
80  const auto string_oper = dynamic_cast<const Analyzer::StringOper*>(expr);
81  if (string_oper) {
82  return visitStringOper(string_oper);
83  }
84  const auto cardinality = dynamic_cast<const Analyzer::CardinalityExpr*>(expr);
85  if (cardinality) {
86  return visitCardinality(cardinality);
87  }
88  const auto width_bucket_expr = dynamic_cast<const Analyzer::WidthBucketExpr*>(expr);
89  if (width_bucket_expr) {
91  }
92  const auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr);
93  if (like_expr) {
94  return visitLikeExpr(like_expr);
95  }
96  const auto regexp_expr = dynamic_cast<const Analyzer::RegexpExpr*>(expr);
97  if (regexp_expr) {
98  return visitRegexpExpr(regexp_expr);
99  }
100  const auto case_ = dynamic_cast<const Analyzer::CaseExpr*>(expr);
101  if (case_) {
102  return visitCaseExpr(case_);
103  }
104  const auto datetrunc = dynamic_cast<const Analyzer::DatetruncExpr*>(expr);
105  if (datetrunc) {
106  return visitDatetruncExpr(datetrunc);
107  }
108  const auto extract = dynamic_cast<const Analyzer::ExtractExpr*>(expr);
109  if (extract) {
110  return visitExtractExpr(extract);
111  }
112  const auto window_func = dynamic_cast<const Analyzer::WindowFunction*>(expr);
113  if (window_func) {
114  return visitWindowFunction(window_func);
115  }
116  const auto func_with_custom_type_handling =
117  dynamic_cast<const Analyzer::FunctionOperWithCustomTypeHandling*>(expr);
118  if (func_with_custom_type_handling) {
119  return visitFunctionOperWithCustomTypeHandling(func_with_custom_type_handling);
120  }
121  const auto func = dynamic_cast<const Analyzer::FunctionOper*>(expr);
122  if (func) {
123  return visitFunctionOper(func);
124  }
125  const auto array = dynamic_cast<const Analyzer::ArrayExpr*>(expr);
126  if (array) {
127  return visitArrayOper(array);
128  }
129  const auto geo_uop = dynamic_cast<const Analyzer::GeoUOper*>(expr);
130  if (geo_uop) {
131  return visitGeoUOper(geo_uop);
132  }
133  const auto geo_binop = dynamic_cast<const Analyzer::GeoBinOper*>(expr);
134  if (geo_binop) {
135  return visitGeoBinOper(geo_binop);
136  }
137  const auto datediff = dynamic_cast<const Analyzer::DatediffExpr*>(expr);
138  if (datediff) {
139  return visitDatediffExpr(datediff);
140  }
141  const auto dateadd = dynamic_cast<const Analyzer::DateaddExpr*>(expr);
142  if (dateadd) {
143  return visitDateaddExpr(dateadd);
144  }
145  const auto likelihood = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
146  if (likelihood) {
147  return visitLikelihood(likelihood);
148  }
149  const auto offset_in_fragment = dynamic_cast<const Analyzer::OffsetInFragment*>(expr);
150  if (offset_in_fragment) {
151  return visitOffsetInFragment(offset_in_fragment);
152  }
153  const auto agg = dynamic_cast<const Analyzer::AggExpr*>(expr);
154  if (agg) {
155  return visitAggExpr(agg);
156  }
157  const auto range_join_oper = dynamic_cast<const Analyzer::RangeOper*>(expr);
158  if (range_join_oper) {
159  return visitRangeJoinOper(range_join_oper);
160  }
161  return defaultResult();
162  }
163 
164  protected:
165  virtual T visitVar(const Analyzer::Var*) const { return defaultResult(); }
166 
167  virtual T visitColumnVar(const Analyzer::ColumnVar*) const { return defaultResult(); }
168 
170  return defaultResult();
171  }
172 
173  virtual T visitConstant(const Analyzer::Constant*) const { return defaultResult(); }
174 
175  virtual T visitUOper(const Analyzer::UOper* uoper) const {
176  T result = defaultResult();
177  result = aggregateResult(result, visit(uoper->get_operand()));
178  return result;
179  }
180 
181  virtual T visitBinOper(const Analyzer::BinOper* bin_oper) const {
182  T result = defaultResult();
183  result = aggregateResult(result, visit(bin_oper->get_left_operand()));
184  result = aggregateResult(result, visit(bin_oper->get_right_operand()));
185  return result;
186  }
187 
188  virtual T visitGeoExpr(const Analyzer::GeoExpr* geo_expr) const {
189  T result = defaultResult();
190  const auto geo_expr_children = geo_expr->getChildExprs();
191  for (const auto expr : geo_expr_children) {
192  result = aggregateResult(result, visit(expr));
193  }
194  return result;
195  }
196 
197  virtual T visitInValues(const Analyzer::InValues* in_values) const {
198  T result = visit(in_values->get_arg());
199  const auto& value_list = in_values->get_value_list();
200  for (const auto& in_value : value_list) {
201  result = aggregateResult(result, visit(in_value.get()));
202  }
203  return result;
204  }
205 
206  virtual T visitInIntegerSet(const Analyzer::InIntegerSet* in_integer_set) const {
207  return visit(in_integer_set->get_arg());
208  }
209 
211  T result = defaultResult();
212  result = aggregateResult(result, visit(char_length->get_arg()));
213  return result;
214  }
215 
216  virtual T visitKeyForString(const Analyzer::KeyForStringExpr* key_for_string) const {
217  T result = defaultResult();
218  result = aggregateResult(result, visit(key_for_string->get_arg()));
219  return result;
220  }
221 
223  T result = defaultResult();
224  result = aggregateResult(result, visit(sample_ratio->get_arg()));
225  return result;
226  }
227 
228  virtual T visitStringOper(const Analyzer::StringOper* string_oper) const {
229  T result = defaultResult();
230  for (const auto& arg : string_oper->getOwnArgs()) {
231  result = aggregateResult(result, visit(arg.get()));
232  }
233  return result;
234  }
235 
236  virtual T visitCardinality(const Analyzer::CardinalityExpr* cardinality) const {
237  T result = defaultResult();
238  result = aggregateResult(result, visit(cardinality->get_arg()));
239  return result;
240  }
241 
242  virtual T visitLikeExpr(const Analyzer::LikeExpr* like) const {
243  T result = defaultResult();
244  result = aggregateResult(result, visit(like->get_arg()));
245  result = aggregateResult(result, visit(like->get_like_expr()));
246  if (like->get_escape_expr()) {
247  result = aggregateResult(result, visit(like->get_escape_expr()));
248  }
249  return result;
250  }
251 
252  virtual T visitRegexpExpr(const Analyzer::RegexpExpr* regexp) const {
253  T result = defaultResult();
254  result = aggregateResult(result, visit(regexp->get_arg()));
255  result = aggregateResult(result, visit(regexp->get_pattern_expr()));
256  if (regexp->get_escape_expr()) {
257  result = aggregateResult(result, visit(regexp->get_escape_expr()));
258  }
259  return result;
260  }
261 
263  T result = defaultResult();
264  result = aggregateResult(result, visit(width_bucket_expr->get_target_value()));
265  result = aggregateResult(result, visit(width_bucket_expr->get_lower_bound()));
266  result = aggregateResult(result, visit(width_bucket_expr->get_upper_bound()));
267  result = aggregateResult(result, visit(width_bucket_expr->get_partition_count()));
268  return result;
269  }
270 
271  virtual T visitCaseExpr(const Analyzer::CaseExpr* case_) const {
272  T result = defaultResult();
273  const auto& expr_pair_list = case_->get_expr_pair_list();
274  for (const auto& expr_pair : expr_pair_list) {
275  result = aggregateResult(result, visit(expr_pair.first.get()));
276  result = aggregateResult(result, visit(expr_pair.second.get()));
277  }
278  result = aggregateResult(result, visit(case_->get_else_expr()));
279  return result;
280  }
281 
282  virtual T visitDatetruncExpr(const Analyzer::DatetruncExpr* datetrunc) const {
283  T result = defaultResult();
284  result = aggregateResult(result, visit(datetrunc->get_from_expr()));
285  return result;
286  }
287 
288  virtual T visitExtractExpr(const Analyzer::ExtractExpr* extract) const {
289  T result = defaultResult();
290  result = aggregateResult(result, visit(extract->get_from_expr()));
291  return result;
292  }
293 
295  const Analyzer::FunctionOperWithCustomTypeHandling* func_oper) const {
296  return visitFunctionOper(func_oper);
297  }
298 
299  virtual T visitArrayOper(Analyzer::ArrayExpr const* array_expr) const {
300  T result = defaultResult();
301  for (size_t i = 0; i < array_expr->getElementCount(); ++i) {
302  result = aggregateResult(result, visit(array_expr->getElement(i)));
303  }
304  return result;
305  }
306 
307  virtual T visitGeoUOper(const Analyzer::GeoUOper* geo_expr) const {
308  T result = defaultResult();
309  for (const auto& arg : geo_expr->getArgs0()) {
310  result = aggregateResult(result, visit(arg.get()));
311  }
312  return result;
313  }
314 
315  virtual T visitGeoBinOper(const Analyzer::GeoBinOper* geo_expr) const {
316  T result = defaultResult();
317  for (const auto& arg : geo_expr->getArgs0()) {
318  result = aggregateResult(result, visit(arg.get()));
319  }
320  for (const auto& arg : geo_expr->getArgs1()) {
321  result = aggregateResult(result, visit(arg.get()));
322  }
323  return result;
324  }
325 
326  virtual T visitFunctionOper(const Analyzer::FunctionOper* func_oper) const {
327  T result = defaultResult();
328  for (size_t i = 0; i < func_oper->getArity(); ++i) {
329  result = aggregateResult(result, visit(func_oper->getArg(i)));
330  }
331  return result;
332  }
333 
334  virtual T visitWindowFunction(const Analyzer::WindowFunction* window_func) const {
335  T result = defaultResult();
336  for (const auto& arg : window_func->getArgs()) {
337  result = aggregateResult(result, visit(arg.get()));
338  }
339  for (const auto& partition_key : window_func->getPartitionKeys()) {
340  result = aggregateResult(result, visit(partition_key.get()));
341  }
342  for (const auto& order_key : window_func->getOrderKeys()) {
343  result = aggregateResult(result, visit(order_key.get()));
344  }
345  return result;
346  }
347 
348  virtual T visitDatediffExpr(const Analyzer::DatediffExpr* datediff) const {
349  T result = defaultResult();
350  result = aggregateResult(result, visit(datediff->get_start_expr()));
351  result = aggregateResult(result, visit(datediff->get_end_expr()));
352  return result;
353  }
354 
355  virtual T visitDateaddExpr(const Analyzer::DateaddExpr* dateadd) const {
356  T result = defaultResult();
357  result = aggregateResult(result, visit(dateadd->get_number_expr()));
358  result = aggregateResult(result, visit(dateadd->get_datetime_expr()));
359  return result;
360  }
361 
362  virtual T visitLikelihood(const Analyzer::LikelihoodExpr* likelihood) const {
363  return visit(likelihood->get_arg());
364  }
365 
367  return defaultResult();
368  }
369 
370  virtual T visitAggExpr(const Analyzer::AggExpr* agg) const {
371  T result = defaultResult();
372  if (agg->get_arg()) {
373  return aggregateResult(result, visit(agg->get_arg()));
374  }
375  return defaultResult();
376  }
377 
378  virtual T visitRangeJoinOper(const Analyzer::RangeOper* range_oper) const {
379  T result = defaultResult();
380  result = aggregateResult(result, visit(range_oper->get_left_operand()));
381  result = aggregateResult(result, visit(range_oper->get_right_operand()));
382  return result;
383  }
384 
385  protected:
386  virtual T aggregateResult(const T& aggregate, const T& next_result) const {
387  return next_result;
388  }
389 
390  virtual void visitBegin() const {}
391 
392  virtual T defaultResult() const { return T{}; }
393 };
394 
395 #endif // QUERYENGINE_SCALAREXPRVISITOR_H
virtual T visitAggExpr(const Analyzer::AggExpr *agg) const
const Expr * get_from_expr() const
Definition: Analyzer.h:1304
const Expr * get_partition_count() const
Definition: Analyzer.h:1043
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:1259
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:2372
const Expr * get_escape_expr() const
Definition: Analyzer.h:906
size_t getArity() const
Definition: Analyzer.h:2169
const Expr * get_escape_expr() const
Definition: Analyzer.h:978
const Expr * get_right_operand() const
Definition: Analyzer.h:450
virtual T visitGeoBinOper(const Analyzer::GeoBinOper *geo_expr) const
const Expr * get_arg() const
Definition: Analyzer.h:975
Expr * get_arg() const
Definition: Analyzer.h:1202
const Expr * get_arg() const
Definition: Analyzer.h:1139
T visit(const Analyzer::Expr *expr) const
virtual T visitCardinality(const Analyzer::CardinalityExpr *cardinality) const
virtual T visitGeoUOper(const Analyzer::GeoUOper *geo_expr) const
virtual std::vector< Analyzer::Expr * > getChildExprs() const
Definition: Analyzer.h:2501
const std::vector< std::shared_ptr< Analyzer::Expr > > & getOrderKeys() const
Definition: Analyzer.h:2269
virtual T visitExtractExpr(const Analyzer::ExtractExpr *extract) const
const Expr * get_left_operand() const
Definition: Analyzer.h:546
const Expr * get_arg() const
Definition: Analyzer.h:903
virtual T visitLikelihood(const Analyzer::LikelihoodExpr *likelihood) const
virtual T visitCharLength(const Analyzer::CharLengthExpr *char_length) const
virtual T visitVar(const Analyzer::Var *) const
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int32_t width_bucket(const double target_value, const double lower_bound, const double upper_bound, const double scale_factor, const int32_t partition_count)
virtual T visitGeoExpr(const Analyzer::GeoExpr *geo_expr) const
virtual T visitLikeExpr(const Analyzer::LikeExpr *like) const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:2263
virtual void visitBegin() const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:2342
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:1387
const Expr * get_right_operand() const
Definition: Analyzer.h:547
size_t getElementCount() const
Definition: Analyzer.h:2302
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int32_t char_length(const char *str, const int32_t str_len)
virtual T visitDatediffExpr(const Analyzer::DatediffExpr *datediff) const
const Expr * get_pattern_expr() const
Definition: Analyzer.h:977
virtual T visitDateaddExpr(const Analyzer::DateaddExpr *dateadd) const
Expression class for string functions The &quot;arg&quot; constructor parameter must be an expression that reso...
Definition: Analyzer.h:1463
const Expr * get_from_expr() const
Definition: Analyzer.h:1429
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1346
const Expr * get_like_expr() const
Definition: Analyzer.h:905
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:2171
virtual T visitRangeJoinOper(const Analyzer::RangeOper *range_oper) const
const Expr * get_operand() const
Definition: Analyzer.h:378
const Expr * get_arg() const
Definition: Analyzer.h:804
const Expr * get_arg() const
Definition: Analyzer.h:710
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:640
virtual T visitConstant(const Analyzer::Constant *) const
virtual T visitKeyForString(const Analyzer::KeyForStringExpr *key_for_string) const
virtual T visitCaseExpr(const Analyzer::CaseExpr *case_) const
virtual T visitBinOper(const Analyzer::BinOper *bin_oper) const
const Expr * get_target_value() const
Definition: Analyzer.h:1040
const Expr * get_arg() const
Definition: Analyzer.h:759
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:1388
#define CHECK(condition)
Definition: Logger.h:223
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:449
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
const Expr * get_lower_bound() const
Definition: Analyzer.h:1041
virtual T visitColumnVarTuple(const Analyzer::ExpressionTuple *) const
const Expr * get_arg() const
Definition: Analyzer.h:687
virtual T visitStringOper(const Analyzer::StringOper *string_oper) const
virtual T visitDatetruncExpr(const Analyzer::DatetruncExpr *datetrunc) const
const Expr * get_upper_bound() const
Definition: Analyzer.h:1042
const std::vector< std::shared_ptr< Analyzer::Expr > > & getPartitionKeys() const
Definition: Analyzer.h:2265
virtual T visitWidthBucket(const Analyzer::WidthBucketExpr *width_bucket_expr) const
const Expr * get_arg() const
Definition: Analyzer.h:849
RUNTIME_EXPORT ALWAYS_INLINE DEVICE bool sample_ratio(const double proportion, const int64_t row_offset)
const Expr * get_arg() const
Definition: Analyzer.h:638
std::vector< std::shared_ptr< Analyzer::Expr > > getOwnArgs() const
Definition: Analyzer.h:1528
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs1() const
Definition: Analyzer.h:2373
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1256
const Expr * get_number_expr() const
Definition: Analyzer.h:1345
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:2306
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int32_t width_bucket_expr(const double target_value, const bool reversed, const double lower_bound, const double upper_bound, const int32_t partition_count)