OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Analyzer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 
23 #include "Analyzer.h"
24 #include "Catalog/Catalog.h"
25 #include "Geospatial/Conversion.h"
26 #include "Geospatial/Types.h"
28 #include "RangeTableEntry.h"
29 #include "Shared/DateConverters.h"
30 #include "Shared/misc.h"
31 #include "Shared/sqltypes.h"
32 
33 #include <algorithm>
34 #include <cstring>
35 #include <iostream>
36 #include <stdexcept>
37 
38 namespace Analyzer {
39 
41  if ((type_info.is_string() || type_info.is_geometry()) && !is_null) {
42  delete constval.stringval;
43  }
44 }
45 
47  delete parsetree;
48 }
49 
51  for (auto p : rangetable) {
52  delete p;
53  }
54  delete order_by;
55  delete next_query;
56 }
57 
58 size_t Expr::get_num_column_vars(const bool include_agg) const {
59  std::set<const Analyzer::ColumnVar*,
60  bool (*)(const Analyzer::ColumnVar*, const Analyzer::ColumnVar*)>
62  collect_column_var(colvar_set, include_agg);
63  return colvar_set.size();
64 }
65 
66 std::shared_ptr<Analyzer::Expr> ColumnVar::deep_copy() const {
67  return makeExpr<ColumnVar>(type_info, column_key_, rte_idx_);
68 }
69 
70 void ExpressionTuple::collect_rte_idx(std::set<int>& rte_idx_set) const {
71  for (const auto& column : tuple_) {
72  column->collect_rte_idx(rte_idx_set);
73  }
74 }
75 
76 std::shared_ptr<Analyzer::Expr> ExpressionTuple::deep_copy() const {
77  std::vector<std::shared_ptr<Expr>> tuple_deep_copy;
78  for (const auto& column : tuple_) {
79  const auto column_deep_copy =
80  std::dynamic_pointer_cast<Analyzer::ColumnVar>(column->deep_copy());
81  CHECK(column_deep_copy);
82  tuple_deep_copy.push_back(column_deep_copy);
83  }
84  return makeExpr<ExpressionTuple>(tuple_deep_copy);
85 }
86 
87 std::shared_ptr<Analyzer::Expr> Var::deep_copy() const {
88  return makeExpr<Var>(type_info, column_key_, rte_idx_, which_row, varno);
89 }
90 
91 std::shared_ptr<Analyzer::Expr> Constant::deep_copy() const {
92  Datum d = constval;
93  if ((type_info.is_string() || type_info.is_geometry()) && !is_null) {
94  d.stringval = new std::string(*constval.stringval);
95  }
96  if (type_info.get_type() == kARRAY) {
97  return makeExpr<Constant>(type_info, is_null, value_list);
98  }
99  return makeExpr<Constant>(type_info, is_null, d);
100 }
101 
102 std::shared_ptr<Analyzer::Expr> UOper::deep_copy() const {
103  return makeExpr<UOper>(type_info, contains_agg, optype, operand->deep_copy());
104 }
105 
106 std::shared_ptr<Analyzer::Expr> BinOper::deep_copy() const {
107  return makeExpr<BinOper>(type_info,
108  contains_agg,
109  optype,
110  qualifier,
111  left_operand->deep_copy(),
112  right_operand->deep_copy());
113 }
114 
115 std::shared_ptr<Analyzer::Expr> RangeOper::deep_copy() const {
116  return makeExpr<RangeOper>(left_inclusive_,
118  left_operand_->deep_copy(),
119  right_operand_->deep_copy());
120 }
121 
122 std::shared_ptr<Analyzer::Expr> Subquery::deep_copy() const {
123  // not supported yet.
124  CHECK(false);
125  return nullptr;
126 }
127 
128 std::shared_ptr<Analyzer::Expr> InValues::deep_copy() const {
129  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
130  for (auto p : value_list) {
131  new_value_list.push_back(p->deep_copy());
132  }
133  return makeExpr<InValues>(arg->deep_copy(), new_value_list);
134 }
135 
136 std::shared_ptr<Analyzer::Expr> MLPredictExpr::deep_copy() const {
137  std::vector<std::shared_ptr<Analyzer::Expr>> regressors_copy;
138  for (auto r : regressor_values_) {
139  regressors_copy.emplace_back(r->deep_copy());
140  }
141  return makeExpr<MLPredictExpr>(model_value_->deep_copy(), regressors_copy);
142 }
143 
144 std::shared_ptr<Analyzer::Expr> PCAProjectExpr::deep_copy() const {
145  std::vector<std::shared_ptr<Analyzer::Expr>> features_copy;
146  for (auto feature_value : feature_values_) {
147  features_copy.emplace_back(feature_value->deep_copy());
148  }
149  return makeExpr<PCAProjectExpr>(
150  model_value_->deep_copy(), features_copy, pc_dimension_value_->deep_copy());
151 }
152 
153 std::shared_ptr<Analyzer::Expr> CharLengthExpr::deep_copy() const {
154  return makeExpr<CharLengthExpr>(arg->deep_copy(), calc_encoded_length);
155 }
156 
157 std::shared_ptr<Analyzer::Expr> KeyForStringExpr::deep_copy() const {
158  return makeExpr<KeyForStringExpr>(arg->deep_copy());
159 }
160 
161 std::shared_ptr<Analyzer::Expr> SampleRatioExpr::deep_copy() const {
162  return makeExpr<SampleRatioExpr>(arg->deep_copy());
163 }
164 
165 std::shared_ptr<Analyzer::Expr> CardinalityExpr::deep_copy() const {
166  return makeExpr<CardinalityExpr>(arg->deep_copy());
167 }
168 
169 std::shared_ptr<Analyzer::Expr> LikeExpr::deep_copy() const {
170  return makeExpr<LikeExpr>(arg->deep_copy(),
171  like_expr->deep_copy(),
172  escape_expr ? escape_expr->deep_copy() : nullptr,
173  is_ilike,
174  is_simple);
175 }
176 
177 std::shared_ptr<Analyzer::Expr> RegexpExpr::deep_copy() const {
178  return makeExpr<RegexpExpr>(arg->deep_copy(),
179  pattern_expr->deep_copy(),
180  escape_expr ? escape_expr->deep_copy() : nullptr);
181 }
182 
183 std::shared_ptr<Analyzer::Expr> WidthBucketExpr::deep_copy() const {
184  return makeExpr<WidthBucketExpr>(target_value_->deep_copy(),
185  lower_bound_->deep_copy(),
186  upper_bound_->deep_copy(),
187  partition_count_->deep_copy());
188 }
189 
190 std::shared_ptr<Analyzer::Expr> LikelihoodExpr::deep_copy() const {
191  return makeExpr<LikelihoodExpr>(arg->deep_copy(), likelihood);
192 }
193 
194 std::shared_ptr<Analyzer::Expr> AggExpr::deep_copy() const {
195  return makeExpr<AggExpr>(
196  type_info, aggtype, arg ? arg->deep_copy() : nullptr, is_distinct, arg1);
197 }
198 
199 std::shared_ptr<Analyzer::Expr> CaseExpr::deep_copy() const {
200  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
201  new_list;
202  for (auto p : expr_pair_list) {
203  new_list.emplace_back(p.first->deep_copy(), p.second->deep_copy());
204  }
205  return makeExpr<CaseExpr>(type_info,
206  contains_agg,
207  new_list,
208  else_expr == nullptr ? nullptr : else_expr->deep_copy());
209 }
210 
211 std::shared_ptr<Analyzer::Expr> ExtractExpr::deep_copy() const {
212  return makeExpr<ExtractExpr>(type_info, contains_agg, field_, from_expr_->deep_copy());
213 }
214 
215 std::shared_ptr<Analyzer::Expr> DateaddExpr::deep_copy() const {
216  return makeExpr<DateaddExpr>(
217  type_info, field_, number_->deep_copy(), datetime_->deep_copy());
218 }
219 
220 std::shared_ptr<Analyzer::Expr> DatediffExpr::deep_copy() const {
221  return makeExpr<DatediffExpr>(
222  type_info, field_, start_->deep_copy(), end_->deep_copy());
223 }
224 
225 std::shared_ptr<Analyzer::Expr> DatetruncExpr::deep_copy() const {
226  return makeExpr<DatetruncExpr>(
227  type_info, contains_agg, field_, from_expr_->deep_copy());
228 }
229 
230 std::shared_ptr<Analyzer::Expr> OffsetInFragment::deep_copy() const {
231  return makeExpr<OffsetInFragment>();
232 }
233 
234 std::shared_ptr<Analyzer::Expr> WindowFrame::deep_copy() const {
235  return makeExpr<WindowFrame>(bound_type_,
236  bound_expr_ ? bound_expr_->deep_copy() : nullptr);
237 }
238 
239 std::shared_ptr<Analyzer::Expr> WindowFunction::deep_copy() const {
240  return makeExpr<WindowFunction>(type_info,
241  kind_,
242  args_,
244  order_keys_,
246  frame_start_bound_->deep_copy(),
247  frame_end_bound_->deep_copy(),
248  collation_);
249 }
250 
252  return makeExpr<Analyzer::ArrayExpr>(
254 }
255 
256 std::shared_ptr<Analyzer::Expr> GeoUOper::deep_copy() const {
258  return makeExpr<GeoUOper>(op_, type_info, type_info, args0_);
259  }
260  return makeExpr<GeoUOper>(op_, type_info, ti0_, args0_);
261 }
262 
263 std::shared_ptr<Analyzer::Expr> GeoBinOper::deep_copy() const {
264  return makeExpr<GeoBinOper>(op_, type_info, ti0_, ti1_, args0_, args1_);
265 }
266 
268  const SQLTypeInfo& left_type,
269  const SQLTypeInfo& right_type,
270  SQLTypeInfo* new_left_type,
271  SQLTypeInfo* new_right_type) {
272  SQLTypeInfo result_type;
273  SQLTypeInfo common_type;
274  *new_left_type = left_type;
275  *new_right_type = right_type;
276  if (IS_LOGIC(op)) {
277  if (left_type.get_type() != kBOOLEAN || right_type.get_type() != kBOOLEAN) {
278  throw std::runtime_error(
279  "non-boolean operands cannot be used in logic operations.");
280  }
281  result_type = SQLTypeInfo(kBOOLEAN, false);
282  } else if (IS_COMPARISON(op)) {
283  if (left_type != right_type) {
284  if (left_type.is_number() && right_type.is_number()) {
285  common_type = common_numeric_type(left_type, right_type);
286  *new_left_type = common_type;
287  new_left_type->set_notnull(left_type.get_notnull());
288  *new_right_type = common_type;
289  new_right_type->set_notnull(right_type.get_notnull());
290  } else if (left_type.is_time() && right_type.is_time()) {
291  switch (left_type.get_type()) {
292  case kTIMESTAMP:
293  switch (right_type.get_type()) {
294  case kTIME:
295  throw std::runtime_error("Cannont compare between TIMESTAMP and TIME.");
296  break;
297  case kDATE:
298  *new_left_type = SQLTypeInfo(left_type.get_type(),
299  left_type.get_dimension(),
300  0,
301  left_type.get_notnull());
302  *new_right_type = *new_left_type;
303  new_right_type->set_notnull(right_type.get_notnull());
304  break;
305  case kTIMESTAMP:
306  *new_left_type = SQLTypeInfo(
307  kTIMESTAMP,
308  std::max(left_type.get_dimension(), right_type.get_dimension()),
309  0,
310  left_type.get_notnull());
311  *new_right_type = SQLTypeInfo(
312  kTIMESTAMP,
313  std::max(left_type.get_dimension(), right_type.get_dimension()),
314  0,
315  right_type.get_notnull());
316  break;
317  default:
318  CHECK(false);
319  }
320  break;
321  case kTIME:
322  switch (right_type.get_type()) {
323  case kTIMESTAMP:
324  throw std::runtime_error("Cannont compare between TIME and TIMESTAMP.");
325  break;
326  case kDATE:
327  throw std::runtime_error("Cannont compare between TIME and DATE.");
328  break;
329  case kTIME:
330  *new_left_type = SQLTypeInfo(
331  kTIME,
332  std::max(left_type.get_dimension(), right_type.get_dimension()),
333  0,
334  left_type.get_notnull());
335  *new_right_type = SQLTypeInfo(
336  kTIME,
337  std::max(left_type.get_dimension(), right_type.get_dimension()),
338  0,
339  right_type.get_notnull());
340  break;
341  default:
342  CHECK(false);
343  }
344  break;
345  case kDATE:
346  switch (right_type.get_type()) {
347  case kTIMESTAMP:
348  *new_left_type = SQLTypeInfo(right_type.get_type(),
349  right_type.get_dimension(),
350  0,
351  left_type.get_notnull());
352  *new_right_type = *new_left_type;
353  new_right_type->set_notnull(right_type.get_notnull());
354  break;
355  case kDATE:
356  *new_left_type = SQLTypeInfo(left_type.get_type(),
357  left_type.get_dimension(),
358  0,
359  left_type.get_notnull());
360  *new_right_type = *new_left_type;
361  new_right_type->set_notnull(right_type.get_notnull());
362  break;
363  case kTIME:
364  throw std::runtime_error("Cannont compare between DATE and TIME.");
365  break;
366  default:
367  CHECK(false);
368  }
369  break;
370  default:
371  CHECK(false);
372  }
373  } else if (left_type.is_string() && right_type.is_time()) {
374  *new_left_type = right_type;
375  new_left_type->set_notnull(left_type.get_notnull());
376  *new_right_type = right_type;
377  } else if (left_type.is_time() && right_type.is_string()) {
378  *new_left_type = left_type;
379  *new_right_type = left_type;
380  new_right_type->set_notnull(right_type.get_notnull());
381  } else if (left_type.is_string() && right_type.is_string()) {
382  *new_left_type = left_type;
383  *new_right_type = right_type;
384  } else if (left_type.is_boolean() && right_type.is_boolean()) {
385  const bool notnull = left_type.get_notnull() && right_type.get_notnull();
386  common_type = SQLTypeInfo(kBOOLEAN, notnull);
387  *new_left_type = common_type;
388  *new_right_type = common_type;
389  } else {
390  throw std::runtime_error("Cannot compare between " + left_type.get_type_name() +
391  " and " + right_type.get_type_name());
392  }
393  }
394  result_type = SQLTypeInfo(kBOOLEAN, false);
395  } else if (op == kMINUS &&
396  (left_type.get_type() == kDATE || left_type.get_type() == kTIMESTAMP) &&
397  right_type.is_timeinterval()) {
398  *new_left_type = left_type;
399  *new_right_type = right_type;
400  result_type = left_type;
401  } else if (IS_ARITHMETIC(op)) {
402  if (!(left_type.is_number() || left_type.is_timeinterval()) ||
403  !(right_type.is_number() || right_type.is_timeinterval())) {
404  throw std::runtime_error("non-numeric operands in arithmetic operations.");
405  }
406  if (op == kMODULO && (!left_type.is_integer() || !right_type.is_integer())) {
407  throw std::runtime_error("non-integer operands in modulo operation.");
408  }
409  common_type = common_numeric_type(left_type, right_type);
410  if (common_type.is_decimal()) {
411  if (op == kMULTIPLY) {
412  // Decimal multiplication requires common_type adjustment:
413  // dimension and scale of the result should be increased.
414  auto new_dimension = left_type.get_dimension() + right_type.get_dimension();
415  // If new dimension is over 20 digits, the result may overflow, or it may not.
416  // Rely on the runtime overflow detection rather than a static check here.
417  if (common_type.get_dimension() < new_dimension) {
418  common_type.set_dimension(new_dimension);
419  }
420  common_type.set_scale(left_type.get_scale() + right_type.get_scale());
421  } else if (op == kPLUS || op == kMINUS) {
422  // Scale should remain the same but dimension could actually go up
423  common_type.set_dimension(common_type.get_dimension() + 1);
424  }
425  }
426  *new_left_type = common_type;
427  new_left_type->set_notnull(left_type.get_notnull());
428  *new_right_type = common_type;
429  new_right_type->set_notnull(right_type.get_notnull());
430  if (op == kMULTIPLY) {
431  new_left_type->set_scale(left_type.get_scale());
432  new_right_type->set_scale(right_type.get_scale());
433  }
434  result_type = common_type;
435  } else {
436  throw std::runtime_error("invalid binary operator type.");
437  }
438  result_type.set_notnull(left_type.get_notnull() && right_type.get_notnull());
439  return result_type;
440 }
441 
442 namespace {
443 bool has_same_dict(const SQLTypeInfo& type1, const SQLTypeInfo& type2) {
444  const auto& type1_dict_key = type1.getStringDictKey();
445  const auto& type2_dict_key = type2.getStringDictKey();
446  return (type1_dict_key == type2_dict_key ||
447  (type1_dict_key.db_id == type2_dict_key.db_id &&
448  type1_dict_key.dict_id == TRANSIENT_DICT(type2_dict_key.dict_id)));
449 }
450 } // namespace
451 
453  const SQLTypeInfo& type2) {
454  SQLTypeInfo common_type;
456  shared::StringDictKey dict_key;
457  CHECK(type1.is_string() && type2.is_string());
458  // if type1 and type2 have the same DICT encoding then keep it
459  // otherwise, they must be decompressed
460  if (type1.get_compression() == kENCODING_DICT &&
461  type2.get_compression() == kENCODING_DICT) {
462  if (has_same_dict(type1, type2)) {
463  comp = kENCODING_DICT;
464  if (type1.getStringDictKey().dict_id < type2.getStringDictKey().dict_id) {
465  dict_key = type1.getStringDictKey();
466  } else {
467  dict_key = type2.getStringDictKey();
468  }
469  }
470  } else if (type1.get_compression() == kENCODING_DICT &&
471  type2.get_compression() == kENCODING_NONE) {
472  dict_key = type1.getStringDictKey();
473  } else if (type1.get_compression() == kENCODING_NONE &&
474  type2.get_compression() == kENCODING_DICT) {
475  dict_key = type2.getStringDictKey();
476  } else {
477  dict_key.dict_id =
478  std::max(type1.get_comp_param(),
479  type2.get_comp_param()); // preserve previous comp_param if set
480  }
481  const bool notnull = type1.get_notnull() && type2.get_notnull();
482  if (type1.get_type() == kTEXT || type2.get_type() == kTEXT) {
483  common_type = SQLTypeInfo(kTEXT, 0, 0, notnull, comp, dict_key.dict_id, kNULLT);
484  } else {
485  common_type = SQLTypeInfo(kVARCHAR,
486  std::max(type1.get_dimension(), type2.get_dimension()),
487  0,
488  notnull,
489  comp,
490  dict_key.dict_id,
491  kNULLT);
492  }
493 
494  if (common_type.is_dict_encoded_string()) {
495  common_type.setStringDictKey(dict_key);
496  }
497  return common_type;
498 }
499 
501  const SQLTypeInfo& type2) {
502  SQLTypeInfo common_type;
503  const bool notnull = type1.get_notnull() && type2.get_notnull();
504  if (type1.get_type() == type2.get_type()) {
505  CHECK(((type1.is_number() || type1.is_timeinterval()) &&
506  (type2.is_number() || type2.is_timeinterval())) ||
507  (type1.is_boolean() && type2.is_boolean()));
508  common_type = SQLTypeInfo(type1.get_type(),
509  std::max(type1.get_dimension(), type2.get_dimension()),
510  std::max(type1.get_scale(), type2.get_scale()),
511  notnull);
512  return common_type;
513  }
514  std::string timeinterval_op_error{
515  "Operator type not supported for time interval arithmetic: "};
516  if (type1.is_timeinterval()) {
517  if (!type2.is_number()) {
518  // allow `number` types to interpret millisecond / microsecond / nanosecond b/c it
519  // may require double and decimal types to represent their time value correctly
520  throw std::runtime_error(timeinterval_op_error + type2.get_type_name());
521  }
522  return type1;
523  }
524  if (type2.is_timeinterval()) {
525  if (!type1.is_number()) {
526  throw std::runtime_error(timeinterval_op_error + type1.get_type_name());
527  }
528  return type2;
529  }
530  CHECK(type1.is_number() && type2.is_number());
531  switch (type1.get_type()) {
532  case kTINYINT:
533  switch (type2.get_type()) {
534  case kSMALLINT:
535  common_type = SQLTypeInfo(kSMALLINT, notnull);
536  break;
537  case kINT:
538  common_type = SQLTypeInfo(kINT, notnull);
539  break;
540  case kBIGINT:
541  common_type = SQLTypeInfo(kBIGINT, notnull);
542  break;
543  case kFLOAT:
544  common_type = SQLTypeInfo(kFLOAT, notnull);
545  break;
546  case kDOUBLE:
547  common_type = SQLTypeInfo(kDOUBLE, notnull);
548  break;
549  case kNUMERIC:
550  case kDECIMAL:
551  common_type =
553  std::max(5 + type2.get_scale(), type2.get_dimension()),
554  type2.get_scale(),
555  notnull);
556  break;
557  default:
558  CHECK(false);
559  }
560  break;
561  case kSMALLINT:
562  switch (type2.get_type()) {
563  case kTINYINT:
564  common_type = SQLTypeInfo(kSMALLINT, notnull);
565  break;
566  case kINT:
567  common_type = SQLTypeInfo(kINT, notnull);
568  break;
569  case kBIGINT:
570  common_type = SQLTypeInfo(kBIGINT, notnull);
571  break;
572  case kFLOAT:
573  common_type = SQLTypeInfo(kFLOAT, notnull);
574  break;
575  case kDOUBLE:
576  common_type = SQLTypeInfo(kDOUBLE, notnull);
577  break;
578  case kNUMERIC:
579  case kDECIMAL:
580  common_type =
582  std::max(5 + type2.get_scale(), type2.get_dimension()),
583  type2.get_scale(),
584  notnull);
585  break;
586  default:
587  CHECK(false);
588  }
589  break;
590  case kINT:
591  switch (type2.get_type()) {
592  case kTINYINT:
593  common_type = SQLTypeInfo(kINT, notnull);
594  break;
595  case kSMALLINT:
596  common_type = SQLTypeInfo(kINT, notnull);
597  break;
598  case kBIGINT:
599  common_type = SQLTypeInfo(kBIGINT, notnull);
600  break;
601  case kFLOAT:
602  common_type = SQLTypeInfo(kFLOAT, notnull);
603  break;
604  case kDOUBLE:
605  common_type = SQLTypeInfo(kDOUBLE, notnull);
606  break;
607  case kNUMERIC:
608  case kDECIMAL:
609  common_type = SQLTypeInfo(
610  kDECIMAL,
612  10 + type2.get_scale()),
613  type2.get_dimension()),
614  type2.get_scale(),
615  notnull);
616  break;
617  default:
618  CHECK(false);
619  }
620  break;
621  case kBIGINT:
622  switch (type2.get_type()) {
623  case kTINYINT:
624  common_type = SQLTypeInfo(kBIGINT, notnull);
625  break;
626  case kSMALLINT:
627  common_type = SQLTypeInfo(kBIGINT, notnull);
628  break;
629  case kINT:
630  common_type = SQLTypeInfo(kBIGINT, notnull);
631  break;
632  case kFLOAT:
633  common_type = SQLTypeInfo(kFLOAT, notnull);
634  break;
635  case kDOUBLE:
636  common_type = SQLTypeInfo(kDOUBLE, notnull);
637  break;
638  case kNUMERIC:
639  case kDECIMAL:
640  common_type = SQLTypeInfo(kDECIMAL,
642  type2.get_scale(),
643  notnull);
644  break;
645  default:
646  CHECK(false);
647  }
648  break;
649  case kFLOAT:
650  switch (type2.get_type()) {
651  case kTINYINT:
652  common_type = SQLTypeInfo(kFLOAT, notnull);
653  break;
654  case kSMALLINT:
655  common_type = SQLTypeInfo(kFLOAT, notnull);
656  break;
657  case kINT:
658  common_type = SQLTypeInfo(kFLOAT, notnull);
659  break;
660  case kBIGINT:
661  common_type = SQLTypeInfo(kFLOAT, notnull);
662  break;
663  case kDOUBLE:
664  common_type = SQLTypeInfo(kDOUBLE, notnull);
665  break;
666  case kNUMERIC:
667  case kDECIMAL:
668  common_type = SQLTypeInfo(kFLOAT, notnull);
669  break;
670  default:
671  CHECK(false);
672  }
673  break;
674  case kDOUBLE:
675  switch (type2.get_type()) {
676  case kTINYINT:
677  case kSMALLINT:
678  case kINT:
679  case kBIGINT:
680  case kFLOAT:
681  case kNUMERIC:
682  case kDECIMAL:
683  common_type = SQLTypeInfo(kDOUBLE, notnull);
684  break;
685  default:
686  CHECK(false);
687  }
688  break;
689  case kNUMERIC:
690  case kDECIMAL:
691  switch (type2.get_type()) {
692  case kTINYINT:
693  common_type =
695  std::max(3 + type1.get_scale(), type1.get_dimension()),
696  type1.get_scale(),
697  notnull);
698  break;
699  case kSMALLINT:
700  common_type =
702  std::max(5 + type1.get_scale(), type1.get_dimension()),
703  type1.get_scale(),
704  notnull);
705  break;
706  case kINT:
707  common_type = SQLTypeInfo(
708  kDECIMAL,
710  10 + type1.get_scale()),
711  type2.get_dimension()),
712  type1.get_scale(),
713  notnull);
714  break;
715  case kBIGINT:
716  common_type = SQLTypeInfo(kDECIMAL,
718  type1.get_scale(),
719  notnull);
720  break;
721  case kFLOAT:
722  common_type = SQLTypeInfo(kFLOAT, notnull);
723  break;
724  case kDOUBLE:
725  common_type = SQLTypeInfo(kDOUBLE, notnull);
726  break;
727  case kNUMERIC:
728  case kDECIMAL: {
729  int common_scale = std::max(type1.get_scale(), type2.get_scale());
730  common_type = SQLTypeInfo(kDECIMAL,
731  std::max(type1.get_dimension() - type1.get_scale(),
732  type2.get_dimension() - type2.get_scale()) +
733  common_scale,
734  common_scale,
735  notnull);
736  break;
737  }
738  default:
739  CHECK(false);
740  }
741  break;
742  default:
743  CHECK(false);
744  }
745  common_type.set_fixed_size();
746  return common_type;
747 }
748 
749 std::shared_ptr<Analyzer::Expr> Expr::decompress() {
751  return shared_from_this();
752  }
753  SQLTypeInfo new_type_info(type_info.get_type(),
758  0,
760  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
761 }
762 
763 namespace {
765  sql_type_info_copy.set_type(kTEXT);
766  sql_type_info_copy.set_compression(kENCODING_DICT);
767  sql_type_info_copy.set_comp_param(TRANSIENT_DICT_ID);
769  sql_type_info_copy.set_fixed_size();
770  return sql_type_info_copy;
771 }
772 } // namespace
773 
774 std::shared_ptr<Analyzer::Expr> Expr::add_cast(const SQLTypeInfo& new_type_info) {
775  if (new_type_info == type_info) {
776  return shared_from_this();
777  }
778  if (new_type_info.is_string() && type_info.is_string() &&
779  new_type_info.get_compression() == kENCODING_DICT &&
781  has_same_dict(new_type_info, type_info)) {
782  return shared_from_this();
783  }
784  if (!type_info.is_castable(new_type_info)) {
785  if (type_info.is_string() && (new_type_info.is_number() || new_type_info.is_time())) {
786  throw std::runtime_error("Cannot CAST from " + type_info.get_type_name() + " to " +
787  new_type_info.get_type_name() +
788  ". Consider using TRY_CAST instead.");
789  }
790  throw std::runtime_error("Cannot CAST from " + type_info.get_type_name() + " to " +
791  new_type_info.get_type_name());
792  }
793  // @TODO(wei) temporary restriction until executor can support this.
794  const bool has_non_literal_operands = get_num_column_vars(true) > 0UL;
795  if (has_non_literal_operands && new_type_info.is_string() &&
796  new_type_info.get_compression() == kENCODING_DICT &&
797  new_type_info.getStringDictKey().dict_id <= TRANSIENT_DICT_ID) {
799  throw std::runtime_error(
800  "Implicit casts of TEXT ENCODING NONE to TEXT ENCODED DICT are not allowed "
801  "for non-literal arguments. Consider adding an explicit conversion to a "
802  "dictionary-encoded text type with ENCODE_TEXT(<none-encoded text arg>).");
803  }
804  throw std::runtime_error(
805  "Internal error: Cannot apply transient dictionary encoding "
806  "to non-literal expression.");
807  }
808  if (!type_info.is_string() && new_type_info.is_string() &&
809  !new_type_info.is_text_encoding_dict()) {
810  return makeExpr<UOper>(
811  make_transient_dict_type(new_type_info), contains_agg, kCAST, shared_from_this());
812  }
813  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
814 }
815 
816 namespace {
817 
818 // Return dec * 10^-scale
819 template <typename T>
820 T floatFromDecimal(int64_t const dec, unsigned const scale) {
821  static_assert(std::is_floating_point_v<T>);
822  return static_cast<T>(dec) / shared::power10(scale);
823 }
824 
825 // Q: Why is there a maxRound() but no minRound()?
826 // A: The numerical value of std::numeric_limits<int64_t>::min() is unchanged when cast
827 // to either float or double, but std::numeric_limits<intXX_t>::max() is incremented to
828 // 2^(XX-1) when cast to float/double for XX in {32,64}, which is an invalid intXX_t
829 // value. Thus the maximum float/double that can be cast to a valid integer type must be
830 // calculated directly, and not just compared to std::numeric_limits<intXX_t>::max().
831 template <typename FLOAT_TYPE, typename INT_TYPE>
832 constexpr FLOAT_TYPE maxRound() {
833  static_assert(std::is_integral_v<INT_TYPE> && std::is_floating_point_v<FLOAT_TYPE>);
834  constexpr int dd =
835  std::numeric_limits<INT_TYPE>::digits - std::numeric_limits<FLOAT_TYPE>::digits;
836  if constexpr (0 < dd) { // NOLINT
837  return static_cast<FLOAT_TYPE>(std::numeric_limits<INT_TYPE>::max() - (1ll << dd));
838  } else {
839  return static_cast<FLOAT_TYPE>(std::numeric_limits<INT_TYPE>::max());
840  }
841 }
842 
843 template <typename TO, typename FROM>
844 TO safeNarrow(FROM const from) {
845  static_assert(std::is_integral_v<TO> && std::is_integral_v<FROM>);
846  static_assert(sizeof(TO) < sizeof(FROM));
847  if (from < static_cast<FROM>(std::numeric_limits<TO>::min()) ||
848  static_cast<FROM>(std::numeric_limits<TO>::max()) < from) {
849  throw std::runtime_error("Overflow or underflow");
850  }
851  return static_cast<TO>(from);
852 }
853 
854 template <typename T>
855 T roundDecimal(int64_t n, unsigned scale) {
856  static_assert(std::is_integral_v<T>);
857  constexpr size_t max_scale = std::numeric_limits<uint64_t>::digits10; // 19
858  constexpr auto pow10 = shared::powersOf<uint64_t, max_scale + 1>(10);
859  if (scale == 0) {
860  if constexpr (sizeof(T) < sizeof(int64_t)) { // NOLINT
861  return safeNarrow<T>(n);
862  } else {
863  return n;
864  }
865  } else if (max_scale < scale) {
866  return 0; // 0.09223372036854775807 rounds to 0
867  }
868  uint64_t const u = std::abs(n);
869  uint64_t const pow = pow10[scale];
870  uint64_t div = u / pow;
871  uint64_t rem = u % pow;
872  div += pow / 2 <= rem;
873  if constexpr (sizeof(T) < sizeof(int64_t)) { // NOLINT
874  return safeNarrow<T>(static_cast<int64_t>(n < 0 ? -div : div));
875  } else {
876  return n < 0 ? -div : div;
877  }
878 }
879 
880 template <typename TO, typename FROM>
881 TO safeRound(FROM const from) {
882  static_assert(std::is_integral_v<TO> && std::is_floating_point_v<FROM>);
883  constexpr FROM max_float = maxRound<FROM, TO>();
884  FROM const n = std::round(from);
885  if (n < static_cast<FROM>(std::numeric_limits<TO>::min()) || max_float < n) {
886  throw std::runtime_error("Overflow or underflow");
887  }
888  return static_cast<TO>(n);
889 }
890 
891 // Return numeric/decimal representation of from with given scale.
892 template <typename T>
893 int64_t safeScale(T from, unsigned const scale) {
894  static_assert(std::is_arithmetic_v<T>);
895  constexpr size_t max_scale = std::numeric_limits<int64_t>::digits10; // 18
896  constexpr auto pow10 = shared::powersOf<int64_t, max_scale + 1>(10);
897  if constexpr (std::is_integral_v<T>) { // NOLINT
898  int64_t retval;
899  if (scale < pow10.size()) {
900 #ifdef __linux__
901  if (!__builtin_mul_overflow(from, pow10[scale], &retval)) {
902  return retval;
903  }
904  // Not over flow safe.
905 #else
906  return from * pow10[scale];
907 #endif
908  }
909  } else if constexpr (std::is_floating_point_v<T>) {
910  if (scale < pow10.size()) {
911  return safeRound<int64_t>(from * pow10[scale]);
912  }
913  }
914  if (from == 0) {
915  return 0;
916  }
917  throw std::runtime_error("Overflow or underflow");
918 }
919 
920 } // namespace
921 
922 void Constant::cast_number(const SQLTypeInfo& new_type_info) {
923  switch (type_info.get_type()) {
924  case kTINYINT:
925  switch (new_type_info.get_type()) {
926  case kTINYINT:
927  break;
928  case kINT:
929  constval.intval = (int32_t)constval.tinyintval;
930  break;
931  case kSMALLINT:
933  break;
934  case kBIGINT:
935  case kTIMESTAMP:
937  break;
938  case kDOUBLE:
940  break;
941  case kFLOAT:
943  break;
944  case kNUMERIC:
945  case kDECIMAL:
947  break;
948  default:
949  CHECK(false);
950  }
951  break;
952  case kINT:
953  switch (new_type_info.get_type()) {
954  case kTINYINT:
955  constval.tinyintval = safeNarrow<int8_t>(constval.intval);
956  break;
957  case kINT:
958  break;
959  case kSMALLINT:
960  constval.smallintval = safeNarrow<int16_t>(constval.intval);
961  break;
962  case kBIGINT:
963  case kTIMESTAMP:
964  constval.bigintval = (int64_t)constval.intval;
965  break;
966  case kDOUBLE:
967  constval.doubleval = (double)constval.intval;
968  break;
969  case kFLOAT:
970  constval.floatval = (float)constval.intval;
971  break;
972  case kNUMERIC:
973  case kDECIMAL:
974  constval.bigintval = safeScale(constval.intval, new_type_info.get_scale());
975  break;
976  default:
977  CHECK(false);
978  }
979  break;
980  case kSMALLINT:
981  switch (new_type_info.get_type()) {
982  case kTINYINT:
983  constval.tinyintval = safeNarrow<int8_t>(constval.smallintval);
984  break;
985  case kINT:
986  constval.intval = (int32_t)constval.smallintval;
987  break;
988  case kSMALLINT:
989  break;
990  case kBIGINT:
991  case kTIMESTAMP:
993  break;
994  case kDOUBLE:
996  break;
997  case kFLOAT:
999  break;
1000  case kNUMERIC:
1001  case kDECIMAL:
1003  break;
1004  default:
1005  CHECK(false);
1006  }
1007  break;
1008  case kBIGINT:
1009  switch (new_type_info.get_type()) {
1010  case kTINYINT:
1011  constval.tinyintval = safeNarrow<int8_t>(constval.bigintval);
1012  break;
1013  case kINT:
1014  constval.intval = safeNarrow<int32_t>(constval.bigintval);
1015  break;
1016  case kSMALLINT:
1017  constval.smallintval = safeNarrow<int16_t>(constval.bigintval);
1018  break;
1019  case kBIGINT:
1020  case kTIMESTAMP:
1021  break;
1022  case kDOUBLE:
1024  break;
1025  case kFLOAT:
1027  break;
1028  case kNUMERIC:
1029  case kDECIMAL:
1031  break;
1032  default:
1033  CHECK(false);
1034  }
1035  break;
1036  case kDOUBLE:
1037  switch (new_type_info.get_type()) {
1038  case kTINYINT:
1039  constval.tinyintval = safeRound<int8_t>(constval.doubleval);
1040  break;
1041  case kINT:
1042  constval.intval = safeRound<int32_t>(constval.doubleval);
1043  break;
1044  case kSMALLINT:
1045  constval.smallintval = safeRound<int16_t>(constval.doubleval);
1046  break;
1047  case kBIGINT:
1048  case kTIMESTAMP:
1049  constval.bigintval = safeRound<int64_t>(constval.doubleval);
1050  break;
1051  case kDOUBLE:
1052  break;
1053  case kFLOAT:
1055  break;
1056  case kNUMERIC:
1057  case kDECIMAL:
1059  break;
1060  default:
1061  CHECK(false);
1062  }
1063  break;
1064  case kFLOAT:
1065  switch (new_type_info.get_type()) {
1066  case kTINYINT:
1067  constval.tinyintval = safeRound<int8_t>(constval.floatval);
1068  break;
1069  case kINT:
1070  constval.intval = safeRound<int32_t>(constval.floatval);
1071  break;
1072  case kSMALLINT:
1073  constval.smallintval = safeRound<int16_t>(constval.floatval);
1074  break;
1075  case kBIGINT:
1076  case kTIMESTAMP:
1077  constval.bigintval = safeRound<int64_t>(constval.floatval);
1078  break;
1079  case kDOUBLE:
1080  constval.doubleval = (double)constval.floatval;
1081  break;
1082  case kFLOAT:
1083  break;
1084  case kNUMERIC:
1085  case kDECIMAL:
1086  constval.bigintval = safeScale(constval.floatval, new_type_info.get_scale());
1087  break;
1088  default:
1089  CHECK(false);
1090  }
1091  break;
1092  case kNUMERIC:
1093  case kDECIMAL:
1094  switch (new_type_info.get_type()) {
1095  case kTINYINT:
1097  roundDecimal<int8_t>(constval.bigintval, type_info.get_scale());
1098  break;
1099  case kINT:
1100  constval.intval =
1101  roundDecimal<int32_t>(constval.bigintval, type_info.get_scale());
1102  break;
1103  case kSMALLINT:
1105  roundDecimal<int16_t>(constval.bigintval, type_info.get_scale());
1106  break;
1107  case kBIGINT:
1108  case kTIMESTAMP:
1110  roundDecimal<int64_t>(constval.bigintval, type_info.get_scale());
1111  break;
1112  case kDOUBLE:
1114  floatFromDecimal<double>(constval.bigintval, type_info.get_scale());
1115  break;
1116  case kFLOAT:
1117  constval.floatval =
1118  floatFromDecimal<float>(constval.bigintval, type_info.get_scale());
1119  break;
1120  case kNUMERIC:
1121  case kDECIMAL:
1123  constval.bigintval, type_info, new_type_info);
1124  break;
1125  default:
1126  CHECK(false);
1127  }
1128  break;
1129  case kTIMESTAMP:
1130  switch (new_type_info.get_type()) {
1131  case kTINYINT:
1132  constval.tinyintval = safeNarrow<int8_t>(constval.bigintval);
1133  break;
1134  case kINT:
1135  constval.intval = safeNarrow<int32_t>(constval.bigintval);
1136  break;
1137  case kSMALLINT:
1138  constval.smallintval = safeNarrow<int16_t>(constval.bigintval);
1139  break;
1140  case kBIGINT:
1141  case kTIMESTAMP:
1142  break;
1143  case kDOUBLE:
1144  constval.doubleval = static_cast<double>(constval.bigintval);
1145  break;
1146  case kFLOAT:
1147  constval.floatval = static_cast<float>(constval.bigintval);
1148  break;
1149  case kNUMERIC:
1150  case kDECIMAL:
1151  for (int i = 0; i < new_type_info.get_scale(); i++) {
1152  constval.bigintval *= 10;
1153  }
1154  break;
1155  default:
1156  CHECK(false);
1157  }
1158  break;
1159  case kBOOLEAN:
1160  switch (new_type_info.get_type()) {
1161  case kTINYINT:
1162  constval.tinyintval = constval.boolval ? 1 : 0;
1163  break;
1164  case kINT:
1165  constval.intval = constval.boolval ? 1 : 0;
1166  break;
1167  case kSMALLINT:
1169  break;
1170  case kBIGINT:
1171  case kTIMESTAMP:
1172  constval.bigintval = constval.boolval ? 1 : 0;
1173  break;
1174  case kDOUBLE:
1175  constval.doubleval = constval.boolval ? 1 : 0;
1176  break;
1177  case kFLOAT:
1178  constval.floatval = constval.boolval ? 1 : 0;
1179  break;
1180  case kNUMERIC:
1181  case kDECIMAL:
1182  constval.bigintval = constval.boolval ? 1 : 0;
1183  for (int i = 0; i < new_type_info.get_scale(); i++) {
1184  constval.bigintval *= 10;
1185  }
1186  break;
1187  default:
1188  CHECK(false);
1189  }
1190  break;
1191  default:
1192  CHECK(false);
1193  }
1194  type_info = new_type_info;
1195 }
1196 
1197 void Constant::cast_string(const SQLTypeInfo& new_type_info) {
1198  std::string* s = constval.stringval;
1199  if (s != nullptr && new_type_info.get_type() != kTEXT &&
1200  static_cast<size_t>(new_type_info.get_dimension()) < s->length()) {
1201  // truncate string
1202  constval.stringval = new std::string(s->substr(0, new_type_info.get_dimension()));
1203  delete s;
1204  }
1205  type_info = new_type_info;
1206 }
1207 
1208 void Constant::cast_from_string(const SQLTypeInfo& new_type_info) {
1209  std::string* s = constval.stringval;
1210  SQLTypeInfo ti = new_type_info;
1211  constval = StringToDatum(*s, ti);
1212  delete s;
1213  type_info = new_type_info;
1214 }
1215 
1216 void Constant::cast_to_string(const SQLTypeInfo& str_type_info) {
1217  const auto str_val = DatumToString(constval, type_info);
1218  constval.stringval = new std::string(str_val);
1219  if (str_type_info.get_type() != kTEXT &&
1220  constval.stringval->length() > static_cast<size_t>(str_type_info.get_dimension())) {
1221  // truncate the string
1222  *constval.stringval = constval.stringval->substr(0, str_type_info.get_dimension());
1223  }
1224  type_info = str_type_info;
1225 }
1226 
1227 namespace {
1228 
1229 // TODO(adb): we should revisit this, as one could argue a Datum should never contain
1230 // a null sentinel. In fact, if we bundle Datum with a null boolean ("NullableDatum"),
1231 // the logic becomes more explicit. There are likely other bugs associated with the
1232 // current logic -- for example, boolean is set to -128 which is likely UB
1233 inline bool is_null_value(const SQLTypeInfo& ti, const Datum& constval) {
1234  switch (ti.get_type()) {
1235  case kBOOLEAN:
1236  return constval.tinyintval == NULL_BOOLEAN;
1237  case kTINYINT:
1238  return constval.tinyintval == NULL_TINYINT;
1239  case kINT:
1240  return constval.intval == NULL_INT;
1241  case kSMALLINT:
1242  return constval.smallintval == NULL_SMALLINT;
1243  case kBIGINT:
1244  case kNUMERIC:
1245  case kDECIMAL:
1246  return constval.bigintval == NULL_BIGINT;
1247  case kTIME:
1248  case kTIMESTAMP:
1249  case kDATE:
1250  case kINTERVAL_DAY_TIME:
1251  case kINTERVAL_YEAR_MONTH:
1252  return constval.bigintval == NULL_BIGINT;
1253  case kVARCHAR:
1254  case kCHAR:
1255  case kTEXT:
1256  return constval.stringval == nullptr;
1257  case kPOINT:
1258  case kMULTIPOINT:
1259  case kLINESTRING:
1260  case kMULTILINESTRING:
1261  case kPOLYGON:
1262  case kMULTIPOLYGON:
1263  return constval.stringval == nullptr;
1264  case kFLOAT:
1265  return constval.floatval == NULL_FLOAT;
1266  case kDOUBLE:
1267  return constval.doubleval == NULL_DOUBLE;
1268  case kNULLT:
1269  return constval.bigintval == 0;
1270  case kARRAY:
1271  return constval.arrayval == nullptr;
1272  default:
1273  UNREACHABLE();
1274  }
1275  UNREACHABLE();
1276  return false;
1277 }
1278 
1279 } // namespace
1280 
1281 void Constant::do_cast(const SQLTypeInfo& new_type_info) {
1282  if (type_info == new_type_info) {
1283  return;
1284  }
1285  if (is_null && !new_type_info.get_notnull()) {
1286  type_info = new_type_info;
1287  set_null_value();
1288  return;
1289  }
1290  if ((new_type_info.is_number() || new_type_info.get_type() == kTIMESTAMP) &&
1291  (new_type_info.get_type() != kTIMESTAMP || type_info.get_type() != kTIMESTAMP) &&
1293  type_info.get_type() == kBOOLEAN)) {
1294  cast_number(new_type_info);
1295  } else if (new_type_info.is_geometry() && type_info.is_string()) {
1296  type_info = new_type_info;
1297  } else if (new_type_info.is_geometry() &&
1298  type_info.get_type() == new_type_info.get_type()) {
1299  type_info = new_type_info;
1300  } else if (new_type_info.is_boolean() && type_info.is_boolean()) {
1301  type_info = new_type_info;
1302  } else if (new_type_info.is_string() && type_info.is_string()) {
1303  cast_string(new_type_info);
1304  } else if (type_info.is_string() || type_info.get_type() == kVARCHAR) {
1305  cast_from_string(new_type_info);
1306  } else if (new_type_info.is_string()) {
1307  cast_to_string(new_type_info);
1308  } else if (new_type_info.get_type() == kDATE && type_info.get_type() == kDATE) {
1309  type_info = new_type_info;
1310  } else if (new_type_info.get_type() == kDATE && type_info.get_type() == kTIMESTAMP) {
1317  type_info = new_type_info;
1318  } else if ((type_info.get_type() == kTIMESTAMP || type_info.get_type() == kDATE) &&
1319  new_type_info.get_type() == kTIMESTAMP) {
1320  const auto dimen = (type_info.get_type() == kDATE) ? 0 : type_info.get_dimension();
1321  if (dimen != new_type_info.get_dimension()) {
1322  constval.bigintval = dimen < new_type_info.get_dimension()
1326  new_type_info.get_dimension() - dimen)
1330  dimen - new_type_info.get_dimension());
1331  }
1332  type_info = new_type_info;
1333  } else if (new_type_info.is_array() && type_info.is_array()) {
1334  auto new_sub_ti = new_type_info.get_elem_type();
1335  for (auto& v : value_list) {
1336  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(v);
1337  if (!c) {
1338  throw std::runtime_error("Invalid array cast.");
1339  }
1340  c->do_cast(new_sub_ti);
1341  }
1342  type_info = new_type_info;
1343  } else if (get_is_null() && (new_type_info.is_number() || new_type_info.is_time() ||
1344  new_type_info.is_string() || new_type_info.is_boolean())) {
1345  type_info = new_type_info;
1346  set_null_value();
1347  } else if (!is_null_value(type_info, constval) &&
1348  get_nullable_type_info(type_info) == new_type_info) {
1349  CHECK(!is_null);
1350  // relax nullability
1351  type_info = new_type_info;
1352  return;
1353  } else if (type_info.is_timestamp() && new_type_info.is_any<kTIME>()) {
1354  type_info = new_type_info;
1355  return;
1356  } else {
1357  throw std::runtime_error("Cast from " + type_info.get_type_name() + " to " +
1358  new_type_info.get_type_name() + " not supported");
1359  }
1360 }
1361 
1363  switch (type_info.get_type()) {
1364  case kBOOLEAN:
1366  break;
1367  case kTINYINT:
1369  break;
1370  case kINT:
1372  break;
1373  case kSMALLINT:
1375  break;
1376  case kBIGINT:
1377  case kNUMERIC:
1378  case kDECIMAL:
1380  break;
1381  case kTIME:
1382  case kTIMESTAMP:
1383  case kDATE:
1385  break;
1386  case kVARCHAR:
1387  case kCHAR:
1388  case kTEXT:
1389  constval.stringval = nullptr;
1390  break;
1391  case kPOINT:
1392  case kMULTIPOINT:
1393  case kLINESTRING:
1394  case kMULTILINESTRING:
1395  case kPOLYGON:
1396  case kMULTIPOLYGON:
1397  constval.stringval = nullptr;
1398  break;
1399  case kFLOAT:
1401  break;
1402  case kDOUBLE:
1404  break;
1405  case kNULLT:
1406  constval.bigintval = 0;
1407  break;
1408  case kARRAY:
1409  constval.arrayval = nullptr;
1410  break;
1411  default:
1412  CHECK(false);
1413  }
1414 }
1415 
1416 std::shared_ptr<Analyzer::Expr> Constant::add_cast(const SQLTypeInfo& new_type_info) {
1417  if (is_null) {
1418  type_info = new_type_info;
1419  set_null_value();
1420  return shared_from_this();
1421  }
1422  if (new_type_info.get_compression() != type_info.get_compression()) {
1423  if (new_type_info.get_compression() != kENCODING_NONE) {
1424  SQLTypeInfo new_ti = new_type_info;
1425  if (new_ti.get_compression() != kENCODING_DATE_IN_DAYS) {
1427  }
1428  do_cast(new_ti);
1429  }
1430  return Expr::add_cast(new_type_info);
1431  }
1432  const auto is_integral_type =
1433  new_type_info.is_integer() || new_type_info.is_decimal() || new_type_info.is_fp();
1434  if (is_integral_type && (type_info.is_time() || type_info.is_date())) {
1435  // Let the codegen phase deal with casts from date/time to a number.
1436  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
1437  }
1438  do_cast(new_type_info);
1439  return shared_from_this();
1440 }
1441 
1442 std::shared_ptr<Analyzer::Expr> UOper::add_cast(const SQLTypeInfo& new_type_info) {
1443  if (optype != kCAST) {
1444  return Expr::add_cast(new_type_info);
1445  }
1446  if (type_info.is_string() && new_type_info.is_string() &&
1447  new_type_info.get_compression() == kENCODING_DICT &&
1449  const SQLTypeInfo oti = operand->get_type_info();
1450  if (oti.is_string() && oti.get_compression() == kENCODING_DICT &&
1451  has_same_dict(oti, new_type_info)) {
1452  auto result = operand;
1453  operand = nullptr;
1454  return result;
1455  }
1456  }
1457  return Expr::add_cast(new_type_info);
1458 }
1459 
1460 std::shared_ptr<Analyzer::Expr> CaseExpr::add_cast(const SQLTypeInfo& new_type_info) {
1461  SQLTypeInfo ti = new_type_info;
1462  if (new_type_info.is_string() && new_type_info.get_compression() == kENCODING_DICT &&
1463  new_type_info.getStringDictKey().isTransientDict() && type_info.is_string() &&
1467  auto dict_key = type_info.getStringDictKey();
1468  dict_key.dict_id = TRANSIENT_DICT(dict_key.dict_id);
1469  ti.setStringDictKey(dict_key);
1470  }
1471 
1472  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1473  new_expr_pair_list;
1474  for (auto& p : expr_pair_list) {
1475  new_expr_pair_list.emplace_back(
1476  std::make_pair(p.first, p.second->deep_copy()->add_cast(ti)));
1477  }
1478 
1479  if (else_expr != nullptr) {
1480  else_expr = else_expr->add_cast(ti);
1481  }
1482  // Replace the current WHEN THEN pair list once we are sure all casts have succeeded
1483  expr_pair_list = new_expr_pair_list;
1484 
1485  type_info = ti;
1486  return shared_from_this();
1487 }
1488 
1489 std::shared_ptr<Analyzer::Expr> Subquery::add_cast(const SQLTypeInfo& new_type_info) {
1490  // not supported yet.
1491  CHECK(false);
1492  return nullptr;
1493 }
1494 
1495 int Query::get_rte_idx(const std::string& name) const {
1496  int rte_idx = 0;
1497  for (auto rte : rangetable) {
1498  if (rte->get_rangevar() == name) {
1499  return rte_idx;
1500  }
1501  rte_idx++;
1502  }
1503  return -1;
1504 }
1505 
1507  rangetable.push_back(rte);
1508 }
1509 
1511  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1512  if (!groupby.empty()) {
1513  for (auto e : groupby) {
1514  auto c = std::dynamic_pointer_cast<ColumnVar>(e);
1515  if (c && column_key_ == c->getColumnKey()) {
1516  return;
1517  }
1518  }
1519  }
1520  throw std::runtime_error(
1521  "expressions in the SELECT or HAVING clause must be an aggregate function or an "
1522  "expression "
1523  "over GROUP BY columns.");
1524 }
1525 
1527  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1528  if (which_row != kGROUPBY) {
1529  throw std::runtime_error("Internal error: invalid VAR in GROUP BY or HAVING.");
1530  }
1531 }
1532 
1534  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1535  operand->check_group_by(groupby);
1536 }
1537 
1539  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1540  left_operand->check_group_by(groupby);
1541  right_operand->check_group_by(groupby);
1542 }
1543 
1544 namespace {
1545 
1546 template <class T>
1547 bool expr_is(const std::shared_ptr<Analyzer::Expr>& expr) {
1548  return std::dynamic_pointer_cast<T>(expr) != nullptr;
1549 }
1550 
1551 } // namespace
1552 
1554  const std::shared_ptr<Analyzer::Expr> cast_operand,
1555  const std::shared_ptr<Analyzer::Expr> const_operand) {
1556  if (expr_is<UOper>(cast_operand) && expr_is<Constant>(const_operand)) {
1557  auto u_expr = std::dynamic_pointer_cast<UOper>(cast_operand);
1558  if (u_expr->get_optype() != kCAST) {
1559  return false;
1560  }
1561  if (!(expr_is<Analyzer::ColumnVar>(u_expr->get_own_operand()) &&
1562  !expr_is<Analyzer::Var>(u_expr->get_own_operand()))) {
1563  return false;
1564  }
1565  const auto& ti = u_expr->get_type_info();
1566  if (ti.is_time() && u_expr->get_operand()->get_type_info().is_time()) {
1567  // Allow casts between time types to pass through
1568  return true;
1569  } else if (ti.is_integer() && u_expr->get_operand()->get_type_info().is_integer()) {
1570  // Allow casts between integer types to pass through
1571  return true;
1572  }
1573  }
1574  return false;
1575 }
1576 
1577 std::shared_ptr<Analyzer::Expr> BinOper::normalize_simple_predicate(int& rte_idx) const {
1578  rte_idx = -1;
1579  if (!IS_COMPARISON(optype) || qualifier != kONE) {
1580  return nullptr;
1581  }
1582  if (expr_is<UOper>(left_operand)) {
1584  auto uo = std::dynamic_pointer_cast<UOper>(left_operand);
1585  auto cv = std::dynamic_pointer_cast<ColumnVar>(uo->get_own_operand());
1586  rte_idx = cv->get_rte_idx();
1587  return this->deep_copy();
1588  }
1589  } else if (expr_is<UOper>(right_operand)) {
1591  auto uo = std::dynamic_pointer_cast<UOper>(right_operand);
1592  auto cv = std::dynamic_pointer_cast<ColumnVar>(uo->get_own_operand());
1593  rte_idx = cv->get_rte_idx();
1594  return makeExpr<BinOper>(type_info,
1595  contains_agg,
1597  qualifier,
1598  right_operand->deep_copy(),
1599  left_operand->deep_copy());
1600  }
1601  } else if (expr_is<ColumnVar>(left_operand) && !expr_is<Var>(left_operand) &&
1602  expr_is<Constant>(right_operand)) {
1603  auto cv = std::dynamic_pointer_cast<ColumnVar>(left_operand);
1604  rte_idx = cv->get_rte_idx();
1605  return this->deep_copy();
1606  } else if (expr_is<Constant>(left_operand) && expr_is<ColumnVar>(right_operand) &&
1607  !expr_is<Var>(right_operand)) {
1608  auto cv = std::dynamic_pointer_cast<ColumnVar>(right_operand);
1609  rte_idx = cv->get_rte_idx();
1610  return makeExpr<BinOper>(type_info,
1611  contains_agg,
1613  qualifier,
1614  right_operand->deep_copy(),
1615  left_operand->deep_copy());
1616  }
1617  return nullptr;
1618 }
1619 
1620 void ColumnVar::group_predicates(std::list<const Expr*>& scan_predicates,
1621  std::list<const Expr*>& join_predicates,
1622  std::list<const Expr*>& const_predicates) const {
1623  if (type_info.get_type() == kBOOLEAN) {
1624  scan_predicates.push_back(this);
1625  }
1626 }
1627 
1628 void UOper::group_predicates(std::list<const Expr*>& scan_predicates,
1629  std::list<const Expr*>& join_predicates,
1630  std::list<const Expr*>& const_predicates) const {
1631  std::set<int> rte_idx_set;
1632  operand->collect_rte_idx(rte_idx_set);
1633  if (rte_idx_set.size() > 1) {
1634  join_predicates.push_back(this);
1635  } else if (rte_idx_set.size() == 1) {
1636  scan_predicates.push_back(this);
1637  } else {
1638  const_predicates.push_back(this);
1639  }
1640 }
1641 
1642 void BinOper::group_predicates(std::list<const Expr*>& scan_predicates,
1643  std::list<const Expr*>& join_predicates,
1644  std::list<const Expr*>& const_predicates) const {
1645  if (optype == kAND) {
1646  left_operand->group_predicates(scan_predicates, join_predicates, const_predicates);
1647  right_operand->group_predicates(scan_predicates, join_predicates, const_predicates);
1648  return;
1649  }
1650  std::set<int> rte_idx_set;
1651  left_operand->collect_rte_idx(rte_idx_set);
1652  right_operand->collect_rte_idx(rte_idx_set);
1653  if (rte_idx_set.size() > 1) {
1654  join_predicates.push_back(this);
1655  } else if (rte_idx_set.size() == 1) {
1656  scan_predicates.push_back(this);
1657  } else {
1658  const_predicates.push_back(this);
1659  }
1660 }
1661 
1662 namespace {
1663 
1665  const auto const_expr = dynamic_cast<const Analyzer::Constant*>(expr);
1666  if (const_expr) {
1667  return const_expr->get_is_null();
1668  }
1669  const auto& expr_ti = expr->get_type_info();
1670  return !expr_ti.get_notnull();
1671 }
1672 
1673 bool is_in_values_nullable(const std::shared_ptr<Analyzer::Expr>& a,
1674  const std::list<std::shared_ptr<Analyzer::Expr>>& l) {
1675  if (is_expr_nullable(a.get())) {
1676  return true;
1677  }
1678  for (const auto& v : l) {
1679  if (is_expr_nullable(v.get())) {
1680  return true;
1681  }
1682  }
1683  return false;
1684 }
1685 
1686 } // namespace
1687 
1688 InValues::InValues(std::shared_ptr<Analyzer::Expr> a,
1689  const std::list<std::shared_ptr<Analyzer::Expr>>& l)
1690  : Expr(kBOOLEAN, !is_in_values_nullable(a, l)), arg(a), value_list(l) {}
1691 
1692 void InValues::group_predicates(std::list<const Expr*>& scan_predicates,
1693  std::list<const Expr*>& join_predicates,
1694  std::list<const Expr*>& const_predicates) const {
1695  std::set<int> rte_idx_set;
1696  arg->collect_rte_idx(rte_idx_set);
1697  if (rte_idx_set.size() > 1) {
1698  join_predicates.push_back(this);
1699  } else if (rte_idx_set.size() == 1) {
1700  scan_predicates.push_back(this);
1701  } else {
1702  const_predicates.push_back(this);
1703  }
1704 }
1705 
1706 InIntegerSet::InIntegerSet(const std::shared_ptr<const Analyzer::Expr> a,
1707  const std::vector<int64_t>& l,
1708  const bool not_null)
1709  : Expr(kBOOLEAN, not_null), arg(a), value_list(l) {}
1710 
1711 void CharLengthExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1712  std::list<const Expr*>& join_predicates,
1713  std::list<const Expr*>& const_predicates) const {
1714  std::set<int> rte_idx_set;
1715  arg->collect_rte_idx(rte_idx_set);
1716  if (rte_idx_set.size() > 1) {
1717  join_predicates.push_back(this);
1718  } else if (rte_idx_set.size() == 1) {
1719  scan_predicates.push_back(this);
1720  } else {
1721  const_predicates.push_back(this);
1722  }
1723 }
1724 
1725 void KeyForStringExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1726  std::list<const Expr*>& join_predicates,
1727  std::list<const Expr*>& const_predicates) const {
1728  std::set<int> rte_idx_set;
1729  arg->collect_rte_idx(rte_idx_set);
1730  if (rte_idx_set.size() > 1) {
1731  join_predicates.push_back(this);
1732  } else if (rte_idx_set.size() == 1) {
1733  scan_predicates.push_back(this);
1734  } else {
1735  const_predicates.push_back(this);
1736  }
1737 }
1738 
1739 void MLPredictExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1740  std::list<const Expr*>& join_predicates,
1741  std::list<const Expr*>& const_predicates) const {
1742  std::set<int> rte_idx_set;
1743  for (const auto& regressor_value : regressor_values_) {
1744  regressor_value->collect_rte_idx(rte_idx_set);
1745  }
1746  if (rte_idx_set.size() > 1) {
1747  join_predicates.push_back(this);
1748  } else if (rte_idx_set.size() == 1) {
1749  scan_predicates.push_back(this);
1750  } else {
1751  const_predicates.push_back(this);
1752  }
1753 }
1754 
1755 void PCAProjectExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1756  std::list<const Expr*>& join_predicates,
1757  std::list<const Expr*>& const_predicates) const {
1758  std::set<int> rte_idx_set;
1759  for (const auto& feature_value : feature_values_) {
1760  feature_value->collect_rte_idx(rte_idx_set);
1761  }
1762  if (rte_idx_set.size() > 1) {
1763  join_predicates.push_back(this);
1764  } else if (rte_idx_set.size() == 1) {
1765  scan_predicates.push_back(this);
1766  } else {
1767  const_predicates.push_back(this);
1768  }
1769 }
1770 
1771 void SampleRatioExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1772  std::list<const Expr*>& join_predicates,
1773  std::list<const Expr*>& const_predicates) const {
1774  std::set<int> rte_idx_set;
1775  arg->collect_rte_idx(rte_idx_set);
1776  if (rte_idx_set.size() > 1) {
1777  join_predicates.push_back(this);
1778  } else if (rte_idx_set.size() == 1) {
1779  scan_predicates.push_back(this);
1780  } else {
1781  const_predicates.push_back(this);
1782  }
1783 }
1784 
1785 void StringOper::group_predicates(std::list<const Expr*>& scan_predicates,
1786  std::list<const Expr*>& join_predicates,
1787  std::list<const Expr*>& const_predicates) const {
1788  std::set<int> rte_idx_set;
1789  for (const auto& arg : args_) {
1790  arg->collect_rte_idx(rte_idx_set);
1791  }
1792  if (rte_idx_set.size() > 1) {
1793  join_predicates.push_back(this);
1794  } else if (rte_idx_set.size() == 1) {
1795  scan_predicates.push_back(this);
1796  } else {
1797  const_predicates.push_back(this);
1798  }
1799 }
1800 
1801 void CardinalityExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1802  std::list<const Expr*>& join_predicates,
1803  std::list<const Expr*>& const_predicates) const {
1804  std::set<int> rte_idx_set;
1805  arg->collect_rte_idx(rte_idx_set);
1806  if (rte_idx_set.size() > 1) {
1807  join_predicates.push_back(this);
1808  } else if (rte_idx_set.size() == 1) {
1809  scan_predicates.push_back(this);
1810  } else {
1811  const_predicates.push_back(this);
1812  }
1813 }
1814 
1815 void LikeExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1816  std::list<const Expr*>& join_predicates,
1817  std::list<const Expr*>& const_predicates) const {
1818  std::set<int> rte_idx_set;
1819  arg->collect_rte_idx(rte_idx_set);
1820  if (rte_idx_set.size() > 1) {
1821  join_predicates.push_back(this);
1822  } else if (rte_idx_set.size() == 1) {
1823  scan_predicates.push_back(this);
1824  } else {
1825  const_predicates.push_back(this);
1826  }
1827 }
1828 
1829 void RegexpExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1830  std::list<const Expr*>& join_predicates,
1831  std::list<const Expr*>& const_predicates) const {
1832  std::set<int> rte_idx_set;
1833  arg->collect_rte_idx(rte_idx_set);
1834  if (rte_idx_set.size() > 1) {
1835  join_predicates.push_back(this);
1836  } else if (rte_idx_set.size() == 1) {
1837  scan_predicates.push_back(this);
1838  } else {
1839  const_predicates.push_back(this);
1840  }
1841 }
1842 
1843 void WidthBucketExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1844  std::list<const Expr*>& join_predicates,
1845  std::list<const Expr*>& const_predicates) const {
1846  std::set<int> rte_idx_set;
1847  target_value_->collect_rte_idx(rte_idx_set);
1848  if (rte_idx_set.size() > 1) {
1849  join_predicates.push_back(this);
1850  } else if (rte_idx_set.size() == 1) {
1851  scan_predicates.push_back(this);
1852  } else {
1853  const_predicates.push_back(this);
1854  }
1855 }
1856 
1857 void LikelihoodExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1858  std::list<const Expr*>& join_predicates,
1859  std::list<const Expr*>& const_predicates) const {
1860  std::set<int> rte_idx_set;
1861  arg->collect_rte_idx(rte_idx_set);
1862  if (rte_idx_set.size() > 1) {
1863  join_predicates.push_back(this);
1864  } else if (rte_idx_set.size() == 1) {
1865  scan_predicates.push_back(this);
1866  } else {
1867  const_predicates.push_back(this);
1868  }
1869 }
1870 
1871 void AggExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1872  std::list<const Expr*>& join_predicates,
1873  std::list<const Expr*>& const_predicates) const {
1874  std::set<int> rte_idx_set;
1875  arg->collect_rte_idx(rte_idx_set);
1876  if (rte_idx_set.size() > 1) {
1877  join_predicates.push_back(this);
1878  } else if (rte_idx_set.size() == 1) {
1879  scan_predicates.push_back(this);
1880  } else {
1881  const_predicates.push_back(this);
1882  }
1883 }
1884 
1885 void CaseExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1886  std::list<const Expr*>& join_predicates,
1887  std::list<const Expr*>& const_predicates) const {
1888  std::set<int> rte_idx_set;
1889  for (auto p : expr_pair_list) {
1890  p.first->collect_rte_idx(rte_idx_set);
1891  p.second->collect_rte_idx(rte_idx_set);
1892  }
1893  if (else_expr != nullptr) {
1894  else_expr->collect_rte_idx(rte_idx_set);
1895  }
1896  if (rte_idx_set.size() > 1) {
1897  join_predicates.push_back(this);
1898  } else if (rte_idx_set.size() == 1) {
1899  scan_predicates.push_back(this);
1900  } else {
1901  const_predicates.push_back(this);
1902  }
1903 }
1904 
1905 void ExtractExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1906  std::list<const Expr*>& join_predicates,
1907  std::list<const Expr*>& const_predicates) const {
1908  std::set<int> rte_idx_set;
1909  from_expr_->collect_rte_idx(rte_idx_set);
1910  if (rte_idx_set.size() > 1) {
1911  join_predicates.push_back(this);
1912  } else if (rte_idx_set.size() == 1) {
1913  scan_predicates.push_back(this);
1914  } else {
1915  const_predicates.push_back(this);
1916  }
1917 }
1918 
1919 void DateaddExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1920  std::list<const Expr*>& join_predicates,
1921  std::list<const Expr*>& const_predicates) const {
1922  std::set<int> rte_idx_set;
1923  number_->collect_rte_idx(rte_idx_set);
1924  datetime_->collect_rte_idx(rte_idx_set);
1925  if (rte_idx_set.size() > 1) {
1926  join_predicates.push_back(this);
1927  } else if (rte_idx_set.size() == 1) {
1928  scan_predicates.push_back(this);
1929  } else {
1930  const_predicates.push_back(this);
1931  }
1932 }
1933 
1934 void DatediffExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1935  std::list<const Expr*>& join_predicates,
1936  std::list<const Expr*>& const_predicates) const {
1937  std::set<int> rte_idx_set;
1938  start_->collect_rte_idx(rte_idx_set);
1939  end_->collect_rte_idx(rte_idx_set);
1940  if (rte_idx_set.size() > 1) {
1941  join_predicates.push_back(this);
1942  } else if (rte_idx_set.size() == 1) {
1943  scan_predicates.push_back(this);
1944  } else {
1945  const_predicates.push_back(this);
1946  }
1947 }
1948 
1949 void DatetruncExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1950  std::list<const Expr*>& join_predicates,
1951  std::list<const Expr*>& const_predicates) const {
1952  std::set<int> rte_idx_set;
1953  from_expr_->collect_rte_idx(rte_idx_set);
1954  if (rte_idx_set.size() > 1) {
1955  join_predicates.push_back(this);
1956  } else if (rte_idx_set.size() == 1) {
1957  scan_predicates.push_back(this);
1958  } else {
1959  const_predicates.push_back(this);
1960  }
1961 }
1962 
1963 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_with_targetlist(
1964  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1965  for (auto tle : tlist) {
1966  const Expr* e = tle->get_expr();
1967  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
1968  if (colvar != nullptr) {
1969  if (column_key_ == colvar->getColumnKey()) {
1970  return colvar->deep_copy();
1971  }
1972  }
1973  }
1974  throw std::runtime_error("Internal error: cannot find ColumnVar in targetlist.");
1975 }
1976 
1977 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_with_child_targetlist(
1978  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1979  int varno = 1;
1980  for (auto tle : tlist) {
1981  const Expr* e = tle->get_expr();
1982  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
1983  if (colvar == nullptr) {
1984  throw std::runtime_error(
1985  "Internal Error: targetlist in rewrite_with_child_targetlist is not all "
1986  "columns.");
1987  }
1988  if (column_key_ == colvar->getColumnKey()) {
1989  return makeExpr<Var>(colvar->get_type_info(),
1990  colvar->getColumnKey(),
1991  colvar->get_rte_idx(),
1993  varno);
1994  }
1995  varno++;
1996  }
1997  throw std::runtime_error("Internal error: cannot find ColumnVar in child targetlist.");
1998 }
1999 
2000 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_agg_to_var(
2001  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2002  int varno = 1;
2003  for (auto tle : tlist) {
2004  const Expr* e = tle->get_expr();
2005  if (typeid(*e) != typeid(AggExpr)) {
2006  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
2007  if (colvar == nullptr) {
2008  throw std::runtime_error(
2009  "Internal Error: targetlist in rewrite_agg_to_var is not all columns and "
2010  "aggregates.");
2011  }
2012  if (column_key_ == colvar->getColumnKey()) {
2013  return makeExpr<Var>(colvar->get_type_info(),
2014  colvar->getColumnKey(),
2015  colvar->get_rte_idx(),
2017  varno);
2018  }
2019  }
2020  varno++;
2021  }
2022  throw std::runtime_error(
2023  "Internal error: cannot find ColumnVar from having clause in targetlist.");
2024 }
2025 
2026 std::shared_ptr<Analyzer::Expr> Var::rewrite_agg_to_var(
2027  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2028  int varno = 1;
2029  for (auto tle : tlist) {
2030  const Expr* e = tle->get_expr();
2031  if (*e == *this) {
2032  return makeExpr<Var>(e->get_type_info(), Var::kINPUT_OUTER, varno);
2033  }
2034  varno++;
2035  }
2036  throw std::runtime_error(
2037  "Internal error: cannot find Var from having clause in targetlist.");
2038 }
2039 
2040 std::shared_ptr<Analyzer::Expr> StringOper::rewrite_with_targetlist(
2041  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2042  std::vector<std::shared_ptr<Analyzer::Expr>> rewritten_args;
2043  for (const auto& arg : args_) {
2044  rewritten_args.emplace_back(arg->rewrite_with_targetlist(tlist));
2045  }
2046  return makeExpr<StringOper>(kind_, rewritten_args);
2047 }
2048 
2049 std::shared_ptr<Analyzer::Expr> StringOper::rewrite_with_child_targetlist(
2050  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2051  std::vector<std::shared_ptr<Analyzer::Expr>> rewritten_args;
2052  for (const auto& arg : args_) {
2053  rewritten_args.emplace_back(arg->rewrite_with_child_targetlist(tlist));
2054  }
2055  return makeExpr<StringOper>(kind_, rewritten_args);
2056 }
2057 
2058 std::shared_ptr<Analyzer::Expr> StringOper::rewrite_agg_to_var(
2059  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2060  std::vector<std::shared_ptr<Analyzer::Expr>> rewritten_args;
2061  for (const auto& arg : args_) {
2062  rewritten_args.emplace_back(arg->rewrite_agg_to_var(tlist));
2063  }
2064  return makeExpr<StringOper>(kind_, rewritten_args);
2065 }
2066 
2067 std::shared_ptr<Analyzer::Expr> InValues::rewrite_with_targetlist(
2068  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2069  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
2070  for (auto v : value_list) {
2071  new_value_list.push_back(v->deep_copy());
2072  }
2073  return makeExpr<InValues>(arg->rewrite_with_targetlist(tlist), new_value_list);
2074 }
2075 
2076 std::shared_ptr<Analyzer::Expr> InValues::rewrite_with_child_targetlist(
2077  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2078  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
2079  for (auto v : value_list) {
2080  new_value_list.push_back(v->deep_copy());
2081  }
2082  return makeExpr<InValues>(arg->rewrite_with_child_targetlist(tlist), new_value_list);
2083 }
2084 
2085 std::shared_ptr<Analyzer::Expr> InValues::rewrite_agg_to_var(
2086  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2087  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
2088  for (auto v : value_list) {
2089  new_value_list.push_back(v->rewrite_agg_to_var(tlist));
2090  }
2091  return makeExpr<InValues>(arg->rewrite_agg_to_var(tlist), new_value_list);
2092 }
2093 
2094 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_with_targetlist(
2095  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2096  for (auto tle : tlist) {
2097  const Expr* e = tle->get_expr();
2098  if (typeid(*e) == typeid(AggExpr)) {
2099  const AggExpr* agg = dynamic_cast<const AggExpr*>(e);
2100  if (*this == *agg) {
2101  return agg->deep_copy();
2102  }
2103  }
2104  }
2105  throw std::runtime_error("Internal error: cannot find AggExpr in targetlist.");
2106 }
2107 
2108 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_with_child_targetlist(
2109  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2110  return makeExpr<AggExpr>(type_info,
2111  aggtype,
2112  arg ? arg->rewrite_with_child_targetlist(tlist) : nullptr,
2113  is_distinct,
2114  arg1);
2115 }
2116 
2117 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_agg_to_var(
2118  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2119  int varno = 1;
2120  for (auto tle : tlist) {
2121  const Expr* e = tle->get_expr();
2122  if (typeid(*e) == typeid(AggExpr)) {
2123  const AggExpr* agg_expr = dynamic_cast<const AggExpr*>(e);
2124  if (*this == *agg_expr) {
2125  return makeExpr<Var>(agg_expr->get_type_info(), Var::kINPUT_OUTER, varno);
2126  }
2127  }
2128  varno++;
2129  }
2130  throw std::runtime_error(
2131  "Internal error: cannot find AggExpr from having clause in targetlist.");
2132 }
2133 
2134 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_with_targetlist(
2135  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2136  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
2137  epair_list;
2138  for (auto p : expr_pair_list) {
2139  epair_list.emplace_back(p.first->rewrite_with_targetlist(tlist),
2140  p.second->rewrite_with_targetlist(tlist));
2141  }
2142  return makeExpr<CaseExpr>(
2143  type_info,
2144  contains_agg,
2145  epair_list,
2146  else_expr ? else_expr->rewrite_with_targetlist(tlist) : nullptr);
2147 }
2148 
2149 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_with_targetlist(
2150  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2151  return makeExpr<ExtractExpr>(
2152  type_info, contains_agg, field_, from_expr_->rewrite_with_targetlist(tlist));
2153 }
2154 
2155 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_with_targetlist(
2156  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2157  return makeExpr<DateaddExpr>(type_info,
2158  field_,
2159  number_->rewrite_with_targetlist(tlist),
2160  datetime_->rewrite_with_targetlist(tlist));
2161 }
2162 
2163 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_with_targetlist(
2164  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2165  return makeExpr<DatediffExpr>(type_info,
2166  field_,
2167  start_->rewrite_with_targetlist(tlist),
2168  end_->rewrite_with_targetlist(tlist));
2169 }
2170 
2171 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_with_targetlist(
2172  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2173  return makeExpr<DatetruncExpr>(
2174  type_info, contains_agg, field_, from_expr_->rewrite_with_targetlist(tlist));
2175 }
2176 
2177 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_with_child_targetlist(
2178  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2179  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
2180  epair_list;
2181  for (auto p : expr_pair_list) {
2182  epair_list.emplace_back(p.first->rewrite_with_child_targetlist(tlist),
2183  p.second->rewrite_with_child_targetlist(tlist));
2184  }
2185  return makeExpr<CaseExpr>(
2186  type_info,
2187  contains_agg,
2188  epair_list,
2189  else_expr ? else_expr->rewrite_with_child_targetlist(tlist) : nullptr);
2190 }
2191 
2192 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_with_child_targetlist(
2193  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2194  return makeExpr<ExtractExpr>(
2195  type_info, contains_agg, field_, from_expr_->rewrite_with_child_targetlist(tlist));
2196 }
2197 
2198 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_with_child_targetlist(
2199  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2200  return makeExpr<DateaddExpr>(type_info,
2201  field_,
2202  number_->rewrite_with_child_targetlist(tlist),
2203  datetime_->rewrite_with_child_targetlist(tlist));
2204 }
2205 
2206 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_with_child_targetlist(
2207  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2208  return makeExpr<DatediffExpr>(type_info,
2209  field_,
2210  start_->rewrite_with_child_targetlist(tlist),
2211  end_->rewrite_with_child_targetlist(tlist));
2212 }
2213 
2214 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_with_child_targetlist(
2215  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2216  return makeExpr<DatetruncExpr>(
2217  type_info, contains_agg, field_, from_expr_->rewrite_with_child_targetlist(tlist));
2218 }
2219 
2220 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_agg_to_var(
2221  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2222  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
2223  epair_list;
2224  for (auto p : expr_pair_list) {
2225  epair_list.emplace_back(p.first->rewrite_agg_to_var(tlist),
2226  p.second->rewrite_agg_to_var(tlist));
2227  }
2228  return makeExpr<CaseExpr>(type_info,
2229  contains_agg,
2230  epair_list,
2231  else_expr ? else_expr->rewrite_agg_to_var(tlist) : nullptr);
2232 }
2233 
2234 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_agg_to_var(
2235  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2236  return makeExpr<ExtractExpr>(
2237  type_info, contains_agg, field_, from_expr_->rewrite_agg_to_var(tlist));
2238 }
2239 
2240 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_agg_to_var(
2241  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2242  return makeExpr<DateaddExpr>(type_info,
2243  field_,
2244  number_->rewrite_agg_to_var(tlist),
2245  datetime_->rewrite_agg_to_var(tlist));
2246 }
2247 
2248 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_agg_to_var(
2249  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2250  return makeExpr<DatediffExpr>(type_info,
2251  field_,
2252  start_->rewrite_agg_to_var(tlist),
2253  end_->rewrite_agg_to_var(tlist));
2254 }
2255 
2256 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_agg_to_var(
2257  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2258  return makeExpr<DatetruncExpr>(
2259  type_info, contains_agg, field_, from_expr_->rewrite_agg_to_var(tlist));
2260 }
2261 
2262 bool ColumnVar::operator==(const Expr& rhs) const {
2263  if (typeid(rhs) != typeid(ColumnVar) && typeid(rhs) != typeid(Var)) {
2264  return false;
2265  }
2266  const ColumnVar& rhs_cv = dynamic_cast<const ColumnVar&>(rhs);
2267  if (rte_idx_ != -1) {
2268  return (column_key_ == rhs_cv.getColumnKey()) && (rte_idx_ == rhs_cv.get_rte_idx());
2269  }
2270  const Var* v = dynamic_cast<const Var*>(this);
2271  if (v == nullptr) {
2272  return false;
2273  }
2274  const Var* rv = dynamic_cast<const Var*>(&rhs);
2275  if (rv == nullptr) {
2276  return false;
2277  }
2278  return (v->get_which_row() == rv->get_which_row()) &&
2279  (v->get_varno() == rv->get_varno());
2280 }
2281 
2282 bool ExpressionTuple::operator==(const Expr& rhs) const {
2283  const auto rhs_tuple = dynamic_cast<const ExpressionTuple*>(&rhs);
2284  if (!rhs_tuple) {
2285  return false;
2286  }
2287  const auto& rhs_tuple_cols = rhs_tuple->getTuple();
2288  return expr_list_match(tuple_, rhs_tuple_cols);
2289 }
2290 
2291 bool Datum_equal(const SQLTypeInfo& ti, Datum val1, Datum val2) {
2292  switch (ti.get_type()) {
2293  case kBOOLEAN:
2294  return val1.boolval == val2.boolval;
2295  case kCHAR:
2296  case kVARCHAR:
2297  case kTEXT:
2298  return *val1.stringval == *val2.stringval;
2299  case kNUMERIC:
2300  case kDECIMAL:
2301  case kBIGINT:
2302  return val1.bigintval == val2.bigintval;
2303  case kINT:
2304  return val1.intval == val2.intval;
2305  case kSMALLINT:
2306  return val1.smallintval == val2.smallintval;
2307  case kTINYINT:
2308  return val1.tinyintval == val2.tinyintval;
2309  case kFLOAT:
2310  return val1.floatval == val2.floatval;
2311  case kDOUBLE:
2312  return val1.doubleval == val2.doubleval;
2313  case kTIME:
2314  case kTIMESTAMP:
2315  case kDATE:
2316  case kINTERVAL_DAY_TIME:
2317  case kINTERVAL_YEAR_MONTH:
2318  return val1.bigintval == val2.bigintval;
2319  case kPOINT:
2320  case kMULTIPOINT:
2321  case kLINESTRING:
2322  case kMULTILINESTRING:
2323  case kPOLYGON:
2324  case kMULTIPOLYGON:
2325  return *val1.stringval == *val2.stringval;
2326  default:
2327  throw std::runtime_error("Unrecognized type for Constant Datum equality: " +
2328  ti.get_type_name());
2329  }
2330  UNREACHABLE();
2331  return false;
2332 }
2333 
2334 bool Constant::operator==(const Expr& rhs) const {
2335  if (typeid(rhs) != typeid(Constant)) {
2336  return false;
2337  }
2338  const Constant& rhs_c = dynamic_cast<const Constant&>(rhs);
2339  if (type_info != rhs_c.get_type_info() || is_null != rhs_c.get_is_null()) {
2340  return false;
2341  }
2342  if (is_null && rhs_c.get_is_null()) {
2343  return true;
2344  }
2345  if (type_info.is_array()) {
2346  return false;
2347  }
2348  return Datum_equal(type_info, constval, rhs_c.get_constval());
2349 }
2350 
2351 bool UOper::operator==(const Expr& rhs) const {
2352  if (typeid(rhs) != typeid(UOper)) {
2353  return false;
2354  }
2355  const UOper& rhs_uo = dynamic_cast<const UOper&>(rhs);
2356  return optype == rhs_uo.get_optype() && *operand == *rhs_uo.get_operand();
2357 }
2358 
2359 bool BinOper::operator==(const Expr& rhs) const {
2360  if (typeid(rhs) != typeid(BinOper)) {
2361  return false;
2362  }
2363  const BinOper& rhs_bo = dynamic_cast<const BinOper&>(rhs);
2364  return optype == rhs_bo.get_optype() && *left_operand == *rhs_bo.get_left_operand() &&
2365  *right_operand == *rhs_bo.get_right_operand();
2366 }
2367 
2368 bool RangeOper::operator==(const Expr& rhs) const {
2369  if (typeid(rhs) != typeid(RangeOper)) {
2370  return false;
2371  }
2372  const RangeOper& rhs_rg = dynamic_cast<const RangeOper&>(rhs);
2373  return left_inclusive_ == rhs_rg.left_inclusive_ &&
2374  right_inclusive_ == rhs_rg.right_inclusive_ &&
2375  *left_operand_ == *rhs_rg.left_operand_ &&
2376  *right_operand_ == *rhs_rg.right_operand_;
2377 }
2378 
2379 bool CharLengthExpr::operator==(const Expr& rhs) const {
2380  if (typeid(rhs) != typeid(CharLengthExpr)) {
2381  return false;
2382  }
2383  const CharLengthExpr& rhs_cl = dynamic_cast<const CharLengthExpr&>(rhs);
2384  if (!(*arg == *rhs_cl.get_arg()) ||
2386  return false;
2387  }
2388  return true;
2389 }
2390 
2391 bool KeyForStringExpr::operator==(const Expr& rhs) const {
2392  if (typeid(rhs) != typeid(KeyForStringExpr)) {
2393  return false;
2394  }
2395  const KeyForStringExpr& rhs_cl = dynamic_cast<const KeyForStringExpr&>(rhs);
2396  if (!(*arg == *rhs_cl.get_arg())) {
2397  return false;
2398  }
2399  return true;
2400 }
2401 
2402 bool SampleRatioExpr::operator==(const Expr& rhs) const {
2403  if (typeid(rhs) != typeid(SampleRatioExpr)) {
2404  return false;
2405  }
2406  const SampleRatioExpr& rhs_cl = dynamic_cast<const SampleRatioExpr&>(rhs);
2407  if (!(*arg == *rhs_cl.get_arg())) {
2408  return false;
2409  }
2410  return true;
2411 }
2412 
2413 bool MLPredictExpr::operator==(const Expr& rhs) const {
2414  if (typeid(rhs) != typeid(MLPredictExpr)) {
2415  return false;
2416  }
2417  const MLPredictExpr& rhs_cl = dynamic_cast<const MLPredictExpr&>(rhs);
2418  if (!(*model_value_ == *rhs_cl.get_model_value())) {
2419  return false;
2420  }
2421  auto rhs_regressor_values = rhs_cl.get_regressor_values();
2422  if (regressor_values_.size() != rhs_regressor_values.size()) {
2423  return false;
2424  }
2425  for (size_t regressor_idx = 0; regressor_idx < regressor_values_.size();
2426  ++regressor_idx) {
2427  if (!(*regressor_values_[regressor_idx] == *rhs_regressor_values[regressor_idx])) {
2428  return false;
2429  }
2430  }
2431  return true;
2432 }
2433 
2434 bool PCAProjectExpr::operator==(const Expr& rhs) const {
2435  if (typeid(rhs) != typeid(PCAProjectExpr)) {
2436  return false;
2437  }
2438  const PCAProjectExpr& rhs_cl = dynamic_cast<const PCAProjectExpr&>(rhs);
2439  if (!(*model_value_ == *rhs_cl.get_model_value())) {
2440  return false;
2441  }
2442  if (!(*pc_dimension_value_ == *rhs_cl.get_pc_dimension_value())) {
2443  return false;
2444  }
2445  auto rhs_feature_values = rhs_cl.get_feature_values();
2446  if (feature_values_.size() != rhs_feature_values.size()) {
2447  return false;
2448  }
2449  for (size_t feature_idx = 0; feature_idx < feature_values_.size(); ++feature_idx) {
2450  if (!(*feature_values_[feature_idx] == *rhs_feature_values[feature_idx])) {
2451  return false;
2452  }
2453  }
2454  return true;
2455 }
2456 
2457 bool CardinalityExpr::operator==(const Expr& rhs) const {
2458  if (typeid(rhs) != typeid(CardinalityExpr)) {
2459  return false;
2460  }
2461  const CardinalityExpr& rhs_ca = dynamic_cast<const CardinalityExpr&>(rhs);
2462  if (!(*arg == *rhs_ca.get_arg())) {
2463  return false;
2464  }
2465  return true;
2466 }
2467 
2468 bool LikeExpr::operator==(const Expr& rhs) const {
2469  if (typeid(rhs) != typeid(LikeExpr)) {
2470  return false;
2471  }
2472  const LikeExpr& rhs_lk = dynamic_cast<const LikeExpr&>(rhs);
2473  if (!(*arg == *rhs_lk.get_arg()) || !(*like_expr == *rhs_lk.get_like_expr()) ||
2474  is_ilike != rhs_lk.get_is_ilike()) {
2475  return false;
2476  }
2477  if (escape_expr.get() == rhs_lk.get_escape_expr()) {
2478  return true;
2479  }
2480  if (escape_expr != nullptr && rhs_lk.get_escape_expr() != nullptr &&
2481  *escape_expr == *rhs_lk.get_escape_expr()) {
2482  return true;
2483  }
2484  return false;
2485 }
2486 
2487 bool RegexpExpr::operator==(const Expr& rhs) const {
2488  if (typeid(rhs) != typeid(RegexpExpr)) {
2489  return false;
2490  }
2491  const RegexpExpr& rhs_re = dynamic_cast<const RegexpExpr&>(rhs);
2492  if (!(*arg == *rhs_re.get_arg()) || !(*pattern_expr == *rhs_re.get_pattern_expr())) {
2493  return false;
2494  }
2495  if (escape_expr.get() == rhs_re.get_escape_expr()) {
2496  return true;
2497  }
2498  if (escape_expr != nullptr && rhs_re.get_escape_expr() != nullptr &&
2499  *escape_expr == *rhs_re.get_escape_expr()) {
2500  return true;
2501  }
2502  return false;
2503 }
2504 
2505 bool WidthBucketExpr::operator==(const Expr& rhs) const {
2506  if (typeid(rhs) != typeid(WidthBucketExpr)) {
2507  return false;
2508  }
2509  const WidthBucketExpr& rhs_l = dynamic_cast<const WidthBucketExpr&>(rhs);
2510  if (!(*target_value_ == *rhs_l.get_target_value())) {
2511  return false;
2512  }
2513  if (!(*lower_bound_ == *rhs_l.get_lower_bound())) {
2514  return false;
2515  }
2516  if (!(*upper_bound_ == *rhs_l.get_upper_bound())) {
2517  return false;
2518  }
2519  if (!(*partition_count_ == *rhs_l.get_partition_count())) {
2520  return false;
2521  }
2522  return true;
2523 }
2524 
2525 bool LikelihoodExpr::operator==(const Expr& rhs) const {
2526  if (typeid(rhs) != typeid(LikelihoodExpr)) {
2527  return false;
2528  }
2529  const LikelihoodExpr& rhs_l = dynamic_cast<const LikelihoodExpr&>(rhs);
2530  if (!(*arg == *rhs_l.get_arg())) {
2531  return false;
2532  }
2533  if (likelihood != rhs_l.get_likelihood()) {
2534  return false;
2535  }
2536  return true;
2537 }
2538 
2539 bool InValues::operator==(const Expr& rhs) const {
2540  if (typeid(rhs) != typeid(InValues)) {
2541  return false;
2542  }
2543  const InValues& rhs_iv = dynamic_cast<const InValues&>(rhs);
2544  if (!(*arg == *rhs_iv.get_arg())) {
2545  return false;
2546  }
2547  if (value_list.size() != rhs_iv.get_value_list().size()) {
2548  return false;
2549  }
2550  auto q = rhs_iv.get_value_list().begin();
2551  for (auto p : value_list) {
2552  if (!(*p == **q)) {
2553  return false;
2554  }
2555  q++;
2556  }
2557  return true;
2558 }
2559 
2560 bool AggExpr::operator==(const Expr& rhs) const {
2561  if (typeid(rhs) != typeid(AggExpr)) {
2562  return false;
2563  }
2564  const AggExpr& rhs_ae = dynamic_cast<const AggExpr&>(rhs);
2565  if (aggtype != rhs_ae.get_aggtype() || is_distinct != rhs_ae.get_is_distinct()) {
2566  return false;
2567  }
2568  if (arg.get() == rhs_ae.get_arg()) {
2569  return true;
2570  }
2571  if (arg == nullptr || rhs_ae.get_arg() == nullptr) {
2572  return false;
2573  }
2574  return *arg == *rhs_ae.get_arg();
2575 }
2576 
2577 bool CaseExpr::operator==(const Expr& rhs) const {
2578  if (typeid(rhs) != typeid(CaseExpr)) {
2579  return false;
2580  }
2581  const CaseExpr& rhs_ce = dynamic_cast<const CaseExpr&>(rhs);
2582  if (expr_pair_list.size() != rhs_ce.get_expr_pair_list().size()) {
2583  return false;
2584  }
2585  if ((else_expr == nullptr && rhs_ce.get_else_expr() != nullptr) ||
2586  (else_expr != nullptr && rhs_ce.get_else_expr() == nullptr)) {
2587  return false;
2588  }
2589  auto it = rhs_ce.get_expr_pair_list().cbegin();
2590  for (auto p : expr_pair_list) {
2591  if (!(*p.first == *it->first) || !(*p.second == *it->second)) {
2592  return false;
2593  }
2594  ++it;
2595  }
2596  return else_expr == nullptr ||
2597  (else_expr != nullptr && *else_expr == *rhs_ce.get_else_expr());
2598 }
2599 
2600 bool ExtractExpr::operator==(const Expr& rhs) const {
2601  if (typeid(rhs) != typeid(ExtractExpr)) {
2602  return false;
2603  }
2604  const ExtractExpr& rhs_ee = dynamic_cast<const ExtractExpr&>(rhs);
2605  return field_ == rhs_ee.get_field() && *from_expr_ == *rhs_ee.get_from_expr();
2606 }
2607 
2608 bool DateaddExpr::operator==(const Expr& rhs) const {
2609  if (typeid(rhs) != typeid(DateaddExpr)) {
2610  return false;
2611  }
2612  const DateaddExpr& rhs_ee = dynamic_cast<const DateaddExpr&>(rhs);
2613  return field_ == rhs_ee.get_field() && *number_ == *rhs_ee.get_number_expr() &&
2614  *datetime_ == *rhs_ee.get_datetime_expr();
2615 }
2616 
2617 bool DatediffExpr::operator==(const Expr& rhs) const {
2618  if (typeid(rhs) != typeid(DatediffExpr)) {
2619  return false;
2620  }
2621  const DatediffExpr& rhs_ee = dynamic_cast<const DatediffExpr&>(rhs);
2622  return field_ == rhs_ee.get_field() && *start_ == *rhs_ee.get_start_expr() &&
2623  *end_ == *rhs_ee.get_end_expr();
2624 }
2625 
2626 bool DatetruncExpr::operator==(const Expr& rhs) const {
2627  if (typeid(rhs) != typeid(DatetruncExpr)) {
2628  return false;
2629  }
2630  const DatetruncExpr& rhs_ee = dynamic_cast<const DatetruncExpr&>(rhs);
2631  return field_ == rhs_ee.get_field() && *from_expr_ == *rhs_ee.get_from_expr();
2632 }
2633 
2634 bool OffsetInFragment::operator==(const Expr& rhs) const {
2635  return typeid(rhs) == typeid(OffsetInFragment);
2636 }
2637 
2638 bool WindowFrame::operator==(const Expr& rhs) const {
2639  const WindowFrame& rhs_window_frame = dynamic_cast<const WindowFrame&>(rhs);
2640  if (bound_type_ == rhs_window_frame.bound_type_) {
2641  if (bound_expr_) {
2642  return rhs_window_frame.bound_expr_ &&
2643  *bound_expr_.get() == *rhs_window_frame.getBoundExpr();
2644  } else {
2645  return !rhs_window_frame.bound_expr_;
2646  }
2647  }
2648  return false;
2649 }
2650 
2651 bool WindowFunction::operator==(const Expr& rhs) const {
2652  const auto rhs_window = dynamic_cast<const WindowFunction*>(&rhs);
2653  if (!rhs_window) {
2654  return false;
2655  }
2656  if (kind_ != rhs_window->kind_ || args_.size() != rhs_window->args_.size() ||
2657  partition_keys_.size() != rhs_window->partition_keys_.size() ||
2658  order_keys_.size() != rhs_window->order_keys_.size() ||
2659  frame_bound_type_ != rhs_window->frame_bound_type_ ||
2660  frame_start_bound_.get() != rhs_window->frame_start_bound_.get() ||
2661  frame_end_bound_.get() != rhs_window->frame_end_bound_.get()) {
2662  return false;
2663  }
2664  return expr_list_match(args_, rhs_window->args_) &&
2665  expr_list_match(partition_keys_, rhs_window->partition_keys_) &&
2666  expr_list_match(order_keys_, rhs_window->order_keys_);
2667 }
2668 
2669 bool ArrayExpr::operator==(Expr const& rhs) const {
2670  if (typeid(rhs) != typeid(ArrayExpr)) {
2671  return false;
2672  }
2673  ArrayExpr const& casted_rhs = static_cast<ArrayExpr const&>(rhs);
2674  for (unsigned i = 0; i < contained_expressions_.size(); i++) {
2675  auto& lhs_expr = contained_expressions_[i];
2676  auto& rhs_expr = casted_rhs.contained_expressions_[i];
2677  if (!(lhs_expr == rhs_expr)) {
2678  return false;
2679  }
2680  }
2681  if (isNull() != casted_rhs.isNull()) {
2682  return false;
2683  }
2684 
2685  return true;
2686  ;
2687 }
2688 
2689 bool GeoUOper::operator==(const Expr& rhs) const {
2690  const auto rhs_geo = dynamic_cast<const GeoUOper*>(&rhs);
2691  if (!rhs_geo) {
2692  return false;
2693  }
2694  if (op_ != rhs_geo->getOp() || ti0_ != rhs_geo->getTypeInfo0() ||
2695  args0_.size() != rhs_geo->getArgs0().size()) {
2696  return false;
2697  }
2698  return expr_list_match(args0_, rhs_geo->getArgs0());
2699 }
2700 
2701 bool GeoBinOper::operator==(const Expr& rhs) const {
2702  const auto rhs_geo = dynamic_cast<const GeoBinOper*>(&rhs);
2703  if (!rhs_geo) {
2704  return false;
2705  }
2706  if (op_ != rhs_geo->getOp() || ti0_ != rhs_geo->getTypeInfo0() ||
2707  args0_.size() != rhs_geo->getArgs0().size()) {
2708  return false;
2709  }
2710  if (ti1_ != rhs_geo->getTypeInfo1() || args1_.size() != rhs_geo->getArgs1().size()) {
2711  return false;
2712  }
2713  return expr_list_match(args0_, rhs_geo->getArgs0()) ||
2714  expr_list_match(args1_, rhs_geo->getArgs1());
2715 }
2716 
2717 std::string ColumnVar::toString() const {
2718  std::stringstream ss;
2719  ss << "(ColumnVar " << column_key_ << ", rte: " << std::to_string(rte_idx_) << " "
2720  << get_type_info().get_type_name() << ", type: " << type_info.toString() << ") ";
2721  return ss.str();
2722 }
2723 
2724 std::string ExpressionTuple::toString() const {
2725  std::string str{"< "};
2726  for (const auto& column : tuple_) {
2727  str += column->toString();
2728  }
2729  str += "> ";
2730  return str;
2731 }
2732 
2733 std::string Var::toString() const {
2734  std::stringstream ss;
2735  ss << "(Var " << column_key_ << ", rte: " << std::to_string(rte_idx_)
2736  << ", which_row: " << std::to_string(which_row)
2737  << ", varno: " << std::to_string(varno) + ") ";
2738  return ss.str();
2739 }
2740 
2741 std::string Constant::toString() const {
2742  std::string str{"(Const "};
2743  if (is_null) {
2744  str += "NULL";
2745  } else if (type_info.is_array()) {
2746  const auto& elem_ti = type_info.get_elem_type();
2747  str += ::toString(type_info.get_type()) + ": " + ::toString(elem_ti.get_type());
2748  } else {
2749  str += DatumToString(constval, type_info);
2750  }
2751  str += ") ";
2752  return str;
2753 }
2754 
2755 std::string UOper::toString() const {
2756  std::string op;
2757  switch (optype) {
2758  case kNOT:
2759  op = "NOT ";
2760  break;
2761  case kUMINUS:
2762  op = "- ";
2763  break;
2764  case kISNULL:
2765  op = "IS NULL ";
2766  break;
2767  case kEXISTS:
2768  op = "EXISTS ";
2769  break;
2770  case kCAST:
2771  op = "CAST " + type_info.get_type_name() + "(" +
2773  std::to_string(type_info.get_scale()) + ") " +
2776  break;
2777  case kUNNEST:
2778  op = "UNNEST ";
2779  break;
2780  default:
2781  break;
2782  }
2783  return "(" + op + operand->toString() + ") ";
2784 }
2785 
2786 std::string BinOper::toString() const {
2787  std::string op;
2788  switch (optype) {
2789  case kEQ:
2790  op = "= ";
2791  break;
2792  case kNE:
2793  op = "<> ";
2794  break;
2795  case kLT:
2796  op = "< ";
2797  break;
2798  case kLE:
2799  op = "<= ";
2800  break;
2801  case kGT:
2802  op = "> ";
2803  break;
2804  case kGE:
2805  op = ">= ";
2806  break;
2807  case kAND:
2808  op = "AND ";
2809  break;
2810  case kOR:
2811  op = "OR ";
2812  break;
2813  case kMINUS:
2814  op = "- ";
2815  break;
2816  case kPLUS:
2817  op = "+ ";
2818  break;
2819  case kMULTIPLY:
2820  op = "* ";
2821  break;
2822  case kDIVIDE:
2823  op = "/ ";
2824  break;
2825  case kMODULO:
2826  op = "% ";
2827  break;
2828  case kARRAY_AT:
2829  op = "[] ";
2830  break;
2831  case kBBOX_INTERSECT:
2832  op = "BBOX_INTERSECT ";
2833  break;
2834  default:
2835  break;
2836  }
2837  std::string str{"("};
2838  str += op;
2839  if (qualifier == kANY) {
2840  str += "ANY ";
2841  } else if (qualifier == kALL) {
2842  str += "ALL ";
2843  }
2844  str += left_operand->toString();
2845  str += right_operand->toString();
2846  str += ") ";
2847  return str;
2848 }
2849 
2850 std::string RangeOper::toString() const {
2851  const std::string lhs = left_inclusive_ ? "[" : "(";
2852  const std::string rhs = right_inclusive_ ? "]" : ")";
2853  return "(RangeOper " + lhs + " " + left_operand_->toString() + " , " +
2854  right_operand_->toString() + " " + rhs + " )";
2855 }
2856 
2857 std::string MLPredictExpr::toString() const {
2858  std::stringstream ss;
2859  ss << "ML_PREDICT(Model: ";
2860  ss << model_value_->toString();
2861  ss << " Regressors: ";
2862  for (const auto& regressor_value : regressor_values_) {
2863  ss << regressor_value->toString() << " ";
2864  }
2865  ss << ") ";
2866  return ss.str();
2867 }
2868 
2869 std::string PCAProjectExpr::toString() const {
2870  std::stringstream ss;
2871  ss << "PCA_PROJECT(Model: ";
2872  ss << model_value_->toString();
2873  ss << " Features: ";
2874  for (const auto& feature_value : feature_values_) {
2875  ss << feature_value->toString() << " ";
2876  }
2877  ss << " PC Dimension: ";
2878  ss << pc_dimension_value_->toString() << " ";
2879  ss << ") ";
2880  return ss.str();
2881 }
2882 
2883 std::string Subquery::toString() const {
2884  return "(Subquery ) ";
2885 }
2886 
2887 std::string InValues::toString() const {
2888  std::string str{"(IN "};
2889  str += arg->toString();
2890  str += "(";
2891  int cnt = 0;
2892  bool shorted_value_list_str = false;
2893  for (auto e : value_list) {
2894  str += e->toString();
2895  cnt++;
2896  if (cnt > 4) {
2897  shorted_value_list_str = true;
2898  break;
2899  }
2900  }
2901  if (shorted_value_list_str) {
2902  str += "... | ";
2903  str += "Total # values: ";
2904  str += std::to_string(value_list.size());
2905  }
2906  str += ") ";
2907  return str;
2908 }
2909 
2910 std::shared_ptr<Analyzer::Expr> InIntegerSet::deep_copy() const {
2911  return std::make_shared<InIntegerSet>(arg, value_list, get_type_info().get_notnull());
2912 }
2913 
2914 bool InIntegerSet::operator==(const Expr& rhs) const {
2915  if (!dynamic_cast<const InIntegerSet*>(&rhs)) {
2916  return false;
2917  }
2918  const auto& rhs_in_integer_set = static_cast<const InIntegerSet&>(rhs);
2919  return *arg == *rhs_in_integer_set.arg && value_list == rhs_in_integer_set.value_list;
2920 }
2921 
2922 std::string InIntegerSet::toString() const {
2923  std::string str{"(IN_INTEGER_SET "};
2924  str += arg->toString();
2925  str += "( ";
2926  int cnt = 0;
2927  bool shorted_value_list_str = false;
2928  for (const auto e : value_list) {
2929  str += std::to_string(e) + " ";
2930  cnt++;
2931  if (cnt > 4) {
2932  shorted_value_list_str = true;
2933  break;
2934  }
2935  }
2936  if (shorted_value_list_str) {
2937  str += "... | ";
2938  str += "Total # values: ";
2939  str += std::to_string(value_list.size());
2940  }
2941  str += ") ";
2942  return str;
2943 }
2944 
2945 std::string CharLengthExpr::toString() const {
2946  std::string str;
2947  if (calc_encoded_length) {
2948  str += "CHAR_LENGTH(";
2949  } else {
2950  str += "LENGTH(";
2951  }
2952  str += arg->toString();
2953  str += ") ";
2954  return str;
2955 }
2956 
2957 std::string KeyForStringExpr::toString() const {
2958  std::string str{"KEY_FOR_STRING("};
2959  str += arg->toString();
2960  str += ") ";
2961  return str;
2962 }
2963 
2964 std::string SampleRatioExpr::toString() const {
2965  std::string str{"SAMPLE_RATIO("};
2966  str += arg->toString();
2967  str += ") ";
2968  return str;
2969 }
2970 
2971 std::string CardinalityExpr::toString() const {
2972  std::string str{"CARDINALITY("};
2973  str += arg->toString();
2974  str += ") ";
2975  return str;
2976 }
2977 
2978 std::string LikeExpr::toString() const {
2979  std::string str{"(LIKE "};
2980  str += arg->toString();
2981  str += like_expr->toString();
2982  if (escape_expr) {
2983  str += escape_expr->toString();
2984  }
2985  str += ") ";
2986  return str;
2987 }
2988 
2989 std::string RegexpExpr::toString() const {
2990  std::string str{"(REGEXP "};
2991  str += arg->toString();
2992  str += pattern_expr->toString();
2993  if (escape_expr) {
2994  str += escape_expr->toString();
2995  }
2996  str += ") ";
2997  return str;
2998 }
2999 
3000 std::string WidthBucketExpr::toString() const {
3001  std::string str{"(WIDTH_BUCKET "};
3002  str += target_value_->toString();
3003  str += lower_bound_->toString();
3004  str += upper_bound_->toString();
3005  str += partition_count_->toString();
3006  return str + ") ";
3007 }
3008 
3009 std::string LikelihoodExpr::toString() const {
3010  std::string str{"(LIKELIHOOD "};
3011  str += arg->toString();
3012  return str + " " + std::to_string(likelihood) + ") ";
3013 }
3014 
3015 std::string AggExpr::toString() const {
3016  std::string agg;
3017  switch (aggtype) {
3018  case kAVG:
3019  agg = "AVG ";
3020  break;
3021  case kMIN:
3022  agg = "MIN ";
3023  break;
3024  case kMAX:
3025  agg = "MAX ";
3026  break;
3027  case kSUM:
3028  agg = "SUM ";
3029  break;
3030  case kCOUNT:
3031  agg = "COUNT ";
3032  break;
3034  agg = "APPROX_COUNT_DISTINCT";
3035  break;
3036  case kAPPROX_QUANTILE:
3037  agg = "APPROX_PERCENTILE";
3038  break;
3039  case kSINGLE_VALUE:
3040  agg = "SINGLE_VALUE";
3041  break;
3042  case kSAMPLE:
3043  agg = "SAMPLE";
3044  break;
3045  case kMODE:
3046  agg = "MODE";
3047  break;
3048  case kCOUNT_IF:
3049  agg = "COUNT_IF";
3050  break;
3051  case kSUM_IF:
3052  agg = "SUM_IF";
3053  break;
3054  default:
3055  UNREACHABLE() << "Unhandled aggtype: " << aggtype;
3056  break;
3057  }
3058  std::string str{"(" + agg};
3059  if (is_distinct) {
3060  str += "DISTINCT ";
3061  }
3062  if (arg) {
3063  str += arg->toString();
3064  } else {
3065  str += "*";
3066  }
3067  return str + ") ";
3068 }
3069 
3070 std::string CaseExpr::toString() const {
3071  std::string str{"CASE "};
3072  for (auto p : expr_pair_list) {
3073  str += "(";
3074  str += p.first->toString();
3075  str += ", ";
3076  str += p.second->toString();
3077  str += ") ";
3078  }
3079  if (else_expr) {
3080  str += "ELSE ";
3081  str += else_expr->toString();
3082  }
3083  str += " END ";
3084  return str;
3085 }
3086 
3087 std::string ExtractExpr::toString() const {
3088  return "EXTRACT(" + std::to_string(field_) + " FROM " + from_expr_->toString() + ") ";
3089 }
3090 
3091 std::string DateaddExpr::toString() const {
3092  return "DATEADD(" + std::to_string(field_) + " NUMBER " + number_->toString() +
3093  " DATETIME " + datetime_->toString() + ") ";
3094 }
3095 
3096 std::string DatediffExpr::toString() const {
3097  return "DATEDIFF(" + std::to_string(field_) + " START " + start_->toString() + " END " +
3098  end_->toString() + ") ";
3099 }
3100 
3101 std::string DatetruncExpr::toString() const {
3102  return "DATE_TRUNC(" + std::to_string(field_) + " , " + from_expr_->toString() + ") ";
3103 }
3104 
3105 std::string OffsetInFragment::toString() const {
3106  return "(OffsetInFragment) ";
3107 }
3108 
3109 std::string WindowFrame::toString() const {
3110  auto bound_str = bound_expr_ ? bound_expr_->toString() : "None";
3111  return ::toString(bound_type_) + " " + bound_str;
3112 }
3113 
3114 std::string WindowFunction::toString() const {
3115  std::string result = "WindowFunction(" + ::toString(kind_);
3116  for (const auto& arg : args_) {
3117  result += " " + arg->toString();
3118  }
3119  if (hasFraming()) {
3120  result += " Frame{";
3121  switch (frame_bound_type_) {
3122  case FrameBoundType::ROW: {
3123  result += "ROW";
3124  break;
3125  }
3126  case FrameBoundType::RANGE: {
3127  result += "RANGE";
3128  break;
3129  }
3130  default: {
3131  UNREACHABLE()
3132  << "Two bound types are supported for window framing: ROW and RANGE.";
3133  break;
3134  }
3135  }
3136  result += " BETWEEN : " + frame_start_bound_->toString();
3137  result += " AND : " + frame_end_bound_->toString();
3138  } else {
3139  if (!order_keys_.empty()) {
3140  result += " (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)";
3141  } else {
3142  result += " (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)";
3143  }
3144  }
3145  result += "} ";
3146  return result + ") ";
3147 }
3148 
3149 std::string ArrayExpr::toString() const {
3150  std::string str{"ARRAY["};
3151 
3152  auto iter(contained_expressions_.begin());
3153  while (iter != contained_expressions_.end()) {
3154  str += (*iter)->toString();
3155  if (iter + 1 != contained_expressions_.end()) {
3156  str += ", ";
3157  }
3158  iter++;
3159  }
3160  str += "]";
3161  return str;
3162 }
3163 
3164 std::string GeoUOper::toString() const {
3165  std::string fn;
3166  switch (op_) {
3168  fn = "Geo";
3169  break;
3171  fn = "ST_IsEmpty";
3172  break;
3174  fn = "ST_IsValid";
3175  break;
3177  fn = "ST_IsConcaveHull";
3178  break;
3180  fn = "ST_IsConvexHull";
3181  break;
3182  default:
3183  fn = "Geo_UNKNOWN";
3184  break;
3185  }
3186  std::string result = fn + "(";
3187  for (const auto& arg : args0_) {
3188  result += " " + arg->toString();
3189  }
3190  return result + " ) ";
3191 }
3192 
3193 std::string GeoBinOper::toString() const {
3194  std::string fn;
3195  switch (op_) {
3197  fn = "ST_Intersection";
3198  break;
3200  fn = "ST_Difference";
3201  break;
3203  fn = "ST_Union";
3204  break;
3206  fn = "ST_Buffer";
3207  break;
3208  default:
3209  fn = "Geo_UNKNOWN";
3210  break;
3211  }
3212  std::string result = fn + "(";
3213  // TODO: generate wkt
3214  for (const auto& arg : args0_) {
3215  result += " " + arg->toString();
3216  }
3217  for (const auto& arg : args1_) {
3218  result += " " + arg->toString();
3219  }
3220  return result + " ) ";
3221 }
3222 
3223 std::string TargetEntry::toString() const {
3224  std::string str{"(" + resname + " "};
3225  str += expr->toString();
3226  if (unnest) {
3227  str += " UNNEST";
3228  }
3229  str += ") ";
3230  return str;
3231 }
3232 
3233 std::string OrderEntry::toString() const {
3234  std::string str{std::to_string(tle_no)};
3235  if (is_desc) {
3236  str += " desc";
3237  }
3238  if (nulls_first) {
3239  str += " nulls first";
3240  }
3241  str += " ";
3242  return str;
3243 }
3244 
3245 void Expr::add_unique(std::list<const Expr*>& expr_list) const {
3246  // only add unique instances to the list
3247  for (auto e : expr_list) {
3248  if (*e == *this) {
3249  return;
3250  }
3251  }
3252  expr_list.push_back(this);
3253 }
3254 
3255 void BinOper::find_expr(std::function<bool(const Expr*)> f,
3256  std::list<const Expr*>& expr_list) const {
3257  if (f(this)) {
3258  add_unique(expr_list);
3259  return;
3260  }
3261  left_operand->find_expr(f, expr_list);
3262  right_operand->find_expr(f, expr_list);
3263 }
3264 
3265 void UOper::find_expr(std::function<bool(const Expr*)> f,
3266  std::list<const Expr*>& expr_list) const {
3267  if (f(this)) {
3268  add_unique(expr_list);
3269  return;
3270  }
3271  operand->find_expr(f, expr_list);
3272 }
3273 
3274 void InValues::find_expr(std::function<bool(const Expr*)> f,
3275  std::list<const Expr*>& expr_list) const {
3276  if (f(this)) {
3277  add_unique(expr_list);
3278  return;
3279  }
3280  arg->find_expr(f, expr_list);
3281  for (auto e : value_list) {
3282  e->find_expr(f, expr_list);
3283  }
3284 }
3285 
3286 void MLPredictExpr::find_expr(std::function<bool(const Expr*)> f,
3287  std::list<const Expr*>& expr_list) const {
3288  if (f(this)) {
3289  add_unique(expr_list);
3290  return;
3291  }
3292  for (auto& r : regressor_values_) {
3293  r->find_expr(f, expr_list);
3294  }
3295 }
3296 
3297 void PCAProjectExpr::find_expr(std::function<bool(const Expr*)> f,
3298  std::list<const Expr*>& expr_list) const {
3299  if (f(this)) {
3300  add_unique(expr_list);
3301  return;
3302  }
3303  for (auto& feature_value : feature_values_) {
3304  feature_value->find_expr(f, expr_list);
3305  }
3306 }
3307 
3308 void CharLengthExpr::find_expr(std::function<bool(const Expr*)> f,
3309  std::list<const Expr*>& expr_list) const {
3310  if (f(this)) {
3311  add_unique(expr_list);
3312  return;
3313  }
3314  arg->find_expr(f, expr_list);
3315 }
3316 
3317 void KeyForStringExpr::find_expr(std::function<bool(const Expr*)> f,
3318  std::list<const Expr*>& expr_list) const {
3319  if (f(this)) {
3320  add_unique(expr_list);
3321  return;
3322  }
3323  arg->find_expr(f, expr_list);
3324 }
3325 
3326 void SampleRatioExpr::find_expr(std::function<bool(const Expr*)> f,
3327  std::list<const Expr*>& expr_list) const {
3328  if (f(this)) {
3329  add_unique(expr_list);
3330  return;
3331  }
3332  arg->find_expr(f, expr_list);
3333 }
3334 
3335 void StringOper::find_expr(std::function<bool(const Expr*)> f,
3336  std::list<const Expr*>& expr_list) const {
3337  if (f(this)) {
3338  add_unique(expr_list);
3339  return;
3340  }
3341  for (const auto& arg : args_) {
3342  arg->find_expr(f, expr_list);
3343  }
3344 }
3345 
3346 void CardinalityExpr::find_expr(std::function<bool(const Expr*)> f,
3347  std::list<const Expr*>& expr_list) const {
3348  if (f(this)) {
3349  add_unique(expr_list);
3350  return;
3351  }
3352  arg->find_expr(f, expr_list);
3353 }
3354 
3355 void LikeExpr::find_expr(std::function<bool(const Expr*)> f,
3356  std::list<const Expr*>& expr_list) const {
3357  if (f(this)) {
3358  add_unique(expr_list);
3359  return;
3360  }
3361  arg->find_expr(f, expr_list);
3362  like_expr->find_expr(f, expr_list);
3363  if (escape_expr != nullptr) {
3364  escape_expr->find_expr(f, expr_list);
3365  }
3366 }
3367 
3368 void RegexpExpr::find_expr(std::function<bool(const Expr*)> f,
3369  std::list<const Expr*>& expr_list) const {
3370  if (f(this)) {
3371  add_unique(expr_list);
3372  return;
3373  }
3374  arg->find_expr(f, expr_list);
3375  pattern_expr->find_expr(f, expr_list);
3376  if (escape_expr != nullptr) {
3377  escape_expr->find_expr(f, expr_list);
3378  }
3379 }
3380 
3381 void WidthBucketExpr::find_expr(std::function<bool(const Expr*)> f,
3382  std::list<const Expr*>& expr_list) const {
3383  if (f(this)) {
3384  add_unique(expr_list);
3385  return;
3386  }
3387  target_value_->find_expr(f, expr_list);
3388  lower_bound_->find_expr(f, expr_list);
3389  upper_bound_->find_expr(f, expr_list);
3390  partition_count_->find_expr(f, expr_list);
3391 }
3392 
3393 void LikelihoodExpr::find_expr(std::function<bool(const Expr*)> f,
3394  std::list<const Expr*>& expr_list) const {
3395  if (f(this)) {
3396  add_unique(expr_list);
3397  return;
3398  }
3399  arg->find_expr(f, expr_list);
3400 }
3401 
3402 void AggExpr::find_expr(std::function<bool(const Expr*)> f,
3403  std::list<const Expr*>& expr_list) const {
3404  if (f(this)) {
3405  add_unique(expr_list);
3406  return;
3407  }
3408  if (arg != nullptr) {
3409  arg->find_expr(f, expr_list);
3410  }
3411 }
3412 
3413 void CaseExpr::find_expr(std::function<bool(const Expr*)> f,
3414  std::list<const Expr*>& expr_list) const {
3415  if (f(this)) {
3416  add_unique(expr_list);
3417  return;
3418  }
3419  for (auto p : expr_pair_list) {
3420  p.first->find_expr(f, expr_list);
3421  p.second->find_expr(f, expr_list);
3422  }
3423  if (else_expr != nullptr) {
3424  else_expr->find_expr(f, expr_list);
3425  }
3426 }
3427 
3428 void ExtractExpr::find_expr(std::function<bool(const Expr*)> f,
3429  std::list<const Expr*>& expr_list) const {
3430  if (f(this)) {
3431  add_unique(expr_list);
3432  return;
3433  }
3434  from_expr_->find_expr(f, expr_list);
3435 }
3436 
3437 void DateaddExpr::find_expr(std::function<bool(const Expr*)> f,
3438  std::list<const Expr*>& expr_list) const {
3439  if (f(this)) {
3440  add_unique(expr_list);
3441  return;
3442  }
3443  number_->find_expr(f, expr_list);
3444  datetime_->find_expr(f, expr_list);
3445 }
3446 
3447 void DatediffExpr::find_expr(std::function<bool(const Expr*)> f,
3448  std::list<const Expr*>& expr_list) const {
3449  if (f(this)) {
3450  add_unique(expr_list);
3451  return;
3452  }
3453  start_->find_expr(f, expr_list);
3454  end_->find_expr(f, expr_list);
3455 }
3456 
3457 void DatetruncExpr::find_expr(std::function<bool(const Expr*)> f,
3458  std::list<const Expr*>& expr_list) const {
3459  if (f(this)) {
3460  add_unique(expr_list);
3461  return;
3462  }
3463  from_expr_->find_expr(f, expr_list);
3464 }
3465 
3466 void FunctionOper::find_expr(std::function<bool(const Expr*)> f,
3467  std::list<const Expr*>& expr_list) const {
3468  if (f(this)) {
3469  add_unique(expr_list);
3470  return;
3471  }
3472  for (const auto& arg : args_) {
3473  arg->find_expr(f, expr_list);
3474  }
3475 }
3476 
3477 void CaseExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3478  for (auto p : expr_pair_list) {
3479  p.first->collect_rte_idx(rte_idx_set);
3480  p.second->collect_rte_idx(rte_idx_set);
3481  }
3482  if (else_expr != nullptr) {
3483  else_expr->collect_rte_idx(rte_idx_set);
3484  }
3485 }
3486 
3487 void ExtractExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3488  from_expr_->collect_rte_idx(rte_idx_set);
3489 }
3490 
3491 void DateaddExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3492  number_->collect_rte_idx(rte_idx_set);
3493  datetime_->collect_rte_idx(rte_idx_set);
3494 }
3495 
3496 void DatediffExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3497  start_->collect_rte_idx(rte_idx_set);
3498  end_->collect_rte_idx(rte_idx_set);
3499 }
3500 
3501 void DatetruncExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3502  from_expr_->collect_rte_idx(rte_idx_set);
3503 }
3504 
3505 void ArrayExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3506  for (unsigned i = 0; i < getElementCount(); i++) {
3507  const auto expr = getElement(i);
3508  expr->collect_rte_idx(rte_idx_set);
3509  }
3510 }
3511 
3512 void StringOper::collect_rte_idx(std::set<int>& rte_idx_set) const {
3513  for (const auto& arg : args_) {
3514  arg->collect_rte_idx(rte_idx_set);
3515  }
3516 }
3517 
3518 void FunctionOper::collect_rte_idx(std::set<int>& rte_idx_set) const {
3519  for (unsigned i = 0; i < getArity(); i++) {
3520  const auto expr = getArg(i);
3521  expr->collect_rte_idx(rte_idx_set);
3522  }
3523 }
3524 
3526  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3527  bool include_agg) const {
3528  for (auto p : expr_pair_list) {
3529  p.first->collect_column_var(colvar_set, include_agg);
3530  p.second->collect_column_var(colvar_set, include_agg);
3531  }
3532  if (else_expr != nullptr) {
3533  else_expr->collect_column_var(colvar_set, include_agg);
3534  }
3535 }
3536 
3538  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3539  bool include_agg) const {
3540  from_expr_->collect_column_var(colvar_set, include_agg);
3541 }
3542 
3544  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3545  bool include_agg) const {
3546  number_->collect_column_var(colvar_set, include_agg);
3547  datetime_->collect_column_var(colvar_set, include_agg);
3548 }
3549 
3551  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3552  bool include_agg) const {
3553  start_->collect_column_var(colvar_set, include_agg);
3554  end_->collect_column_var(colvar_set, include_agg);
3555 }
3556 
3558  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3559  bool include_agg) const {
3560  from_expr_->collect_column_var(colvar_set, include_agg);
3561 }
3562 
3564  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3565  bool include_agg) const {
3566  for (unsigned i = 0; i < getElementCount(); i++) {
3567  const auto expr = getElement(i);
3568  expr->collect_column_var(colvar_set, include_agg);
3569  }
3570 }
3571 
3573  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3574  bool include_agg) const {
3575  for (const auto& arg : args_) {
3576  arg->collect_column_var(colvar_set, include_agg);
3577  }
3578 }
3579 
3581  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3582  bool include_agg) const {
3583  for (unsigned i = 0; i < getArity(); i++) {
3584  const auto expr = getArg(i);
3585  expr->collect_column_var(colvar_set, include_agg);
3586  }
3587 }
3588 
3590  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3591  for (auto p : expr_pair_list) {
3592  p.first->check_group_by(groupby);
3593  p.second->check_group_by(groupby);
3594  }
3595  if (else_expr != nullptr) {
3596  else_expr->check_group_by(groupby);
3597  }
3598 }
3599 
3601  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3602  from_expr_->check_group_by(groupby);
3603 }
3604 
3606  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3607  number_->check_group_by(groupby);
3608  datetime_->check_group_by(groupby);
3609 }
3610 
3612  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3613  start_->check_group_by(groupby);
3614  end_->check_group_by(groupby);
3615 }
3616 
3618  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3619  from_expr_->check_group_by(groupby);
3620 }
3621 
3622 void CaseExpr::get_domain(DomainSet& domain_set) const {
3623  for (const auto& p : expr_pair_list) {
3624  const auto c = std::dynamic_pointer_cast<const Constant>(p.second);
3625  if (c != nullptr) {
3626  c->add_unique(domain_set);
3627  } else {
3628  const auto v = std::dynamic_pointer_cast<const ColumnVar>(p.second);
3629  if (v != nullptr) {
3630  v->add_unique(domain_set);
3631  } else {
3632  const auto cast = std::dynamic_pointer_cast<const UOper>(p.second);
3633  if (cast != nullptr && cast->get_optype() == kCAST) {
3634  const Constant* c = dynamic_cast<const Constant*>(cast->get_operand());
3635  if (c != nullptr) {
3636  cast->add_unique(domain_set);
3637  continue;
3638  } else {
3639  const auto v = std::dynamic_pointer_cast<const ColumnVar>(p.second);
3640  if (v != nullptr) {
3641  v->add_unique(domain_set);
3642  continue;
3643  }
3644  }
3645  }
3646  p.second->get_domain(domain_set);
3647  if (domain_set.empty()) {
3648  return;
3649  }
3650  }
3651  }
3652  }
3653  if (else_expr != nullptr) {
3654  const auto c = std::dynamic_pointer_cast<const Constant>(else_expr);
3655  if (c != nullptr) {
3656  c->add_unique(domain_set);
3657  } else {
3658  const auto v = std::dynamic_pointer_cast<const ColumnVar>(else_expr);
3659  if (v != nullptr) {
3660  v->add_unique(domain_set);
3661  } else {
3662  const auto cast = std::dynamic_pointer_cast<const UOper>(else_expr);
3663  if (cast != nullptr && cast->get_optype() == kCAST) {
3664  const Constant* c = dynamic_cast<const Constant*>(cast->get_operand());
3665  if (c != nullptr) {
3666  c->add_unique(domain_set);
3667  } else {
3668  const auto v = std::dynamic_pointer_cast<const ColumnVar>(else_expr);
3669  if (v != nullptr) {
3670  v->add_unique(domain_set);
3671  }
3672  }
3673  } else {
3674  else_expr->get_domain(domain_set);
3675  }
3676  }
3677  }
3678  }
3679 }
3680 
3681 std::shared_ptr<Analyzer::Expr> StringOper::deep_copy() const {
3682  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
3683  for (const auto& arg : args_) {
3684  args_copy.emplace_back(arg->deep_copy());
3685  }
3686  std::vector<std::shared_ptr<Analyzer::Expr>> chained_string_op_exprs_copy;
3687  for (const auto& chained_string_op_expr : chained_string_op_exprs_) {
3688  chained_string_op_exprs_copy.emplace_back(chained_string_op_expr->deep_copy());
3689  }
3690  return makeExpr<Analyzer::StringOper>(kind_,
3691  get_type_info(),
3692  std::move(args_copy),
3693  std::move(chained_string_op_exprs_copy));
3694 }
3695 
3696 std::shared_ptr<Analyzer::Expr> LowerStringOper::deep_copy() const {
3697  return makeExpr<Analyzer::LowerStringOper>(
3698  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3699 }
3700 
3701 std::shared_ptr<Analyzer::Expr> UpperStringOper::deep_copy() const {
3702  return makeExpr<Analyzer::UpperStringOper>(
3703  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3704 }
3705 
3706 std::shared_ptr<Analyzer::Expr> InitCapStringOper::deep_copy() const {
3707  return makeExpr<Analyzer::InitCapStringOper>(
3708  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3709 }
3710 
3711 std::shared_ptr<Analyzer::Expr> ReverseStringOper::deep_copy() const {
3712  return makeExpr<Analyzer::ReverseStringOper>(
3713  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3714 }
3715 
3716 std::shared_ptr<Analyzer::Expr> RepeatStringOper::deep_copy() const {
3717  return makeExpr<Analyzer::RepeatStringOper>(
3718  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3719 }
3720 
3721 std::shared_ptr<Analyzer::Expr> PadStringOper::deep_copy() const {
3722  return makeExpr<Analyzer::PadStringOper>(
3723  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3724 }
3725 
3726 std::shared_ptr<Analyzer::Expr> TrimStringOper::deep_copy() const {
3727  return makeExpr<Analyzer::TrimStringOper>(
3728  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3729 }
3730 
3731 std::shared_ptr<Analyzer::Expr> SubstringStringOper::deep_copy() const {
3732  return makeExpr<Analyzer::SubstringStringOper>(
3733  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3734 }
3735 
3736 std::shared_ptr<Analyzer::Expr> ReplaceStringOper::deep_copy() const {
3737  return makeExpr<Analyzer::ReplaceStringOper>(
3738  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3739 }
3740 
3741 std::shared_ptr<Analyzer::Expr> OverlayStringOper::deep_copy() const {
3742  return makeExpr<Analyzer::OverlayStringOper>(
3743  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3744 }
3745 
3746 std::shared_ptr<Analyzer::Expr> ConcatStringOper::deep_copy() const {
3747  return makeExpr<Analyzer::ConcatStringOper>(
3748  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3749 }
3750 
3751 std::shared_ptr<Analyzer::Expr> SplitPartStringOper::deep_copy() const {
3752  return makeExpr<Analyzer::SplitPartStringOper>(
3753  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3754 }
3755 
3756 std::shared_ptr<Analyzer::Expr> RegexpReplaceStringOper::deep_copy() const {
3757  return makeExpr<Analyzer::RegexpReplaceStringOper>(
3758  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3759 }
3760 
3761 std::shared_ptr<Analyzer::Expr> RegexpSubstrStringOper::deep_copy() const {
3762  return makeExpr<Analyzer::RegexpSubstrStringOper>(
3763  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3764 }
3765 
3766 std::shared_ptr<Analyzer::Expr> JsonValueStringOper::deep_copy() const {
3767  return makeExpr<Analyzer::JsonValueStringOper>(
3768  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3769 }
3770 
3771 std::shared_ptr<Analyzer::Expr> Base64EncodeStringOper::deep_copy() const {
3772  return makeExpr<Analyzer::Base64EncodeStringOper>(
3773  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3774 }
3775 
3776 std::shared_ptr<Analyzer::Expr> Base64DecodeStringOper::deep_copy() const {
3777  return makeExpr<Analyzer::Base64DecodeStringOper>(
3778  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3779 }
3780 
3781 std::shared_ptr<Analyzer::Expr> TryStringCastOper::deep_copy() const {
3782  return makeExpr<Analyzer::TryStringCastOper>(
3783  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3784 }
3785 
3786 std::shared_ptr<Analyzer::Expr> PositionStringOper::deep_copy() const {
3787  return makeExpr<Analyzer::PositionStringOper>(
3788  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3789 }
3790 
3791 std::shared_ptr<Analyzer::Expr> JarowinklerSimilarityStringOper::deep_copy() const {
3792  return makeExpr<Analyzer::JarowinklerSimilarityStringOper>(
3793  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3794 }
3795 
3796 std::shared_ptr<Analyzer::Expr> LevenshteinDistanceStringOper::deep_copy() const {
3797  return makeExpr<Analyzer::LevenshteinDistanceStringOper>(
3798  std::dynamic_pointer_cast<Analyzer::StringOper>(StringOper::deep_copy()));
3799 }
3800 
3801 std::shared_ptr<Analyzer::Expr> FunctionOper::deep_copy() const {
3802  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
3803  for (size_t i = 0; i < getArity(); ++i) {
3804  args_copy.push_back(getArg(i)->deep_copy());
3805  }
3806  return makeExpr<Analyzer::FunctionOper>(type_info, getName(), args_copy);
3807 }
3808 
3809 bool StringOper::operator==(const Expr& rhs) const {
3810  const auto rhs_string_oper = dynamic_cast<const StringOper*>(&rhs);
3811 
3812  if (!rhs_string_oper) {
3813  return false;
3814  }
3815 
3816  if (get_kind() != rhs_string_oper->get_kind()) {
3817  return false;
3818  }
3819  if (getArity() != rhs_string_oper->getArity()) {
3820  return false;
3821  }
3822 
3823  for (size_t i = 0; i < getArity(); ++i) {
3824  if (!(*getArg(i) == *(rhs_string_oper->getArg(i)))) {
3825  return false;
3826  }
3827  }
3828  if (chained_string_op_exprs_.size() !=
3829  rhs_string_oper->chained_string_op_exprs_.size()) {
3830  return false;
3831  }
3832  for (size_t i = 0; i < chained_string_op_exprs_.size(); ++i) {
3833  if (!(*(chained_string_op_exprs_[i]) ==
3834  *(rhs_string_oper->chained_string_op_exprs_[i]))) {
3835  return false;
3836  }
3837  }
3838  return true;
3839 }
3840 
3841 bool FunctionOper::operator==(const Expr& rhs) const {
3842  if (type_info != rhs.get_type_info()) {
3843  return false;
3844  }
3845  const auto rhs_func_oper = dynamic_cast<const FunctionOper*>(&rhs);
3846  if (!rhs_func_oper) {
3847  return false;
3848  }
3849  if (getName() != rhs_func_oper->getName()) {
3850  return false;
3851  }
3852  if (getArity() != rhs_func_oper->getArity()) {
3853  return false;
3854  }
3855  for (size_t i = 0; i < getArity(); ++i) {
3856  if (!(*getArg(i) == *(rhs_func_oper->getArg(i)))) {
3857  return false;
3858  }
3859  }
3860  return true;
3861 }
3862 
3863 std::string StringOper::toString() const {
3864  std::string str{"(" + ::toString(kind_) + " "};
3865  for (const auto& arg : args_) {
3866  str += arg->toString();
3867  }
3868  str += ")";
3869  return str;
3870 }
3871 
3872 std::string FunctionOper::toString() const {
3873  std::string str{"(" + name_ + " "};
3874  for (const auto& arg : args_) {
3875  str += arg->toString();
3876  }
3877  str += ")";
3878  return str;
3879 }
3880 
3881 std::shared_ptr<Analyzer::Expr> FunctionOperWithCustomTypeHandling::deep_copy() const {
3882  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
3883  for (size_t i = 0; i < getArity(); ++i) {
3884  args_copy.push_back(getArg(i)->deep_copy());
3885  }
3886  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
3887  type_info, getName(), args_copy);
3888 }
3889 
3891  if (type_info != rhs.get_type_info()) {
3892  return false;
3893  }
3894  const auto rhs_func_oper =
3895  dynamic_cast<const FunctionOperWithCustomTypeHandling*>(&rhs);
3896  if (!rhs_func_oper) {
3897  return false;
3898  }
3899  if (getName() != rhs_func_oper->getName()) {
3900  return false;
3901  }
3902  if (getArity() != rhs_func_oper->getArity()) {
3903  return false;
3904  }
3905  for (size_t i = 0; i < getArity(); ++i) {
3906  if (!(*getArg(i) == *(rhs_func_oper->getArg(i)))) {
3907  return false;
3908  }
3909  }
3910  return true;
3911 }
3912 
3913 double WidthBucketExpr::get_bound_val(const Analyzer::Expr* bound_expr) const {
3914  CHECK(bound_expr);
3915  auto copied_expr = bound_expr->deep_copy();
3916  auto casted_expr = copied_expr->add_cast(SQLTypeInfo(kDOUBLE, false));
3917  CHECK(casted_expr);
3918  auto casted_constant = std::dynamic_pointer_cast<const Analyzer::Constant>(casted_expr);
3919  CHECK(casted_constant);
3920  return casted_constant->get_constval().doubleval;
3921 }
3922 
3924  auto const_partition_count_expr =
3925  dynamic_cast<const Analyzer::Constant*>(partition_count_.get());
3926  if (!const_partition_count_expr) {
3927  return -1;
3928  }
3929  auto d = const_partition_count_expr->get_constval();
3930  switch (const_partition_count_expr->get_type_info().get_type()) {
3931  case kTINYINT:
3932  return d.tinyintval;
3933  case kSMALLINT:
3934  return d.smallintval;
3935  case kINT:
3936  return d.intval;
3937  case kBIGINT: {
3938  auto bi = d.bigintval;
3939  if (bi < 1 || bi > INT32_MAX) {
3940  return -1;
3941  }
3942  return bi;
3943  }
3944  default:
3945  return -1;
3946  }
3947 }
3948 
3949 namespace {
3950 // Assumes lower_bound <= upper_bound
3951 int32_t ordered_bucket(double const lower_bound,
3952  double const upper_bound,
3953  int32_t const partition_count,
3954  double const value) {
3955  if (value < lower_bound) {
3956  return 0;
3957  } else if (upper_bound <= value) {
3958  return partition_count + 1;
3959  } else { // There is no division by 0 since a previous return would have occurred.
3960  double const width = upper_bound - lower_bound;
3961  return static_cast<int32_t>(partition_count * (value - lower_bound) / width) + 1;
3962  }
3963 }
3964 } // namespace
3965 
3966 // this utility function is useful for optimizing expression range decision
3967 // for an expression depending on width_bucket expr
3968 int32_t WidthBucketExpr::compute_bucket(double target_const) const {
3969  if (target_const == inline_fp_null_val(SQLTypeInfo(kDOUBLE))) {
3970  return INT32_MIN;
3971  }
3972  double const lower_bound = get_bound_val(lower_bound_.get());
3973  double const upper_bound = get_bound_val(upper_bound_.get());
3974  int32_t const partition_count = get_partition_count_val();
3975  if (lower_bound <= upper_bound) {
3976  return ordered_bucket(lower_bound, upper_bound, partition_count, target_const);
3977  } else {
3978  return ordered_bucket(-lower_bound, -upper_bound, partition_count, -target_const);
3979  }
3980 }
3981 
3982 namespace {
3983 
3985  CHECK(geo);
3986  switch (geo->getType()) {
3988  return kPOINT;
3989  }
3991  return kMULTIPOINT;
3992  }
3994  return kLINESTRING;
3995  }
3997  return kMULTILINESTRING;
3998  }
4000  return kPOLYGON;
4001  }
4003  return kMULTIPOLYGON;
4004  }
4005  default:
4006  UNREACHABLE();
4007  return kNULLT;
4008  }
4009 }
4010 
4011 } // namespace
4012 
4013 // TODO: fixup null
4014 GeoConstant::GeoConstant(std::unique_ptr<Geospatial::GeoBase>&& geo,
4015  const SQLTypeInfo& ti)
4016  : GeoExpr(ti), geo_(std::move(geo)) {
4017  CHECK(geo_);
4018  if (get_ti_from_geo(geo_.get()) != ti.get_type()) {
4019  throw std::runtime_error("Conflicting types for geo data " + geo_->getWktString() +
4020  " (type provided: " + ti.get_type_name() + ")");
4021  }
4022 }
4023 
4024 std::shared_ptr<Analyzer::Expr> GeoConstant::deep_copy() const {
4025  CHECK(geo_);
4026  return makeExpr<GeoConstant>(geo_->clone(), type_info);
4027 }
4028 
4029 std::string GeoConstant::toString() const {
4030  std::string str{"(GeoConstant "};
4031  CHECK(geo_);
4032  str += geo_->getWktString();
4033  str += ") ";
4034  return str;
4035 }
4036 
4037 std::string GeoConstant::getWKTString() const {
4038  CHECK(geo_);
4039  return geo_->getWktString();
4040 }
4041 
4042 bool GeoConstant::operator==(const Expr& rhs) const {
4043  if (typeid(rhs) != typeid(GeoConstant)) {
4044  return false;
4045  }
4046  const GeoConstant& rhs_c = dynamic_cast<const GeoConstant&>(rhs);
4047  if (type_info != rhs_c.get_type_info() /*|| is_null != rhs_c.get_is_null()*/) {
4048  return false;
4049  }
4050  /* TODO: constant nulls
4051  if (is_null && rhs_c.get_is_null()) {
4052  return true;
4053  }
4054 
4055  */
4056  return *geo_ == *rhs_c.geo_;
4057 }
4058 
4062 }
4063 
4064 std::shared_ptr<Analyzer::Constant> GeoConstant::makePhysicalConstant(
4065  const size_t index) const {
4066  // TODO: handle bounds, etc
4067  const auto num_phys_coords = type_info.get_physical_coord_cols();
4068  CHECK_GE(num_phys_coords, 0);
4069  CHECK_LE(index, size_t(num_phys_coords));
4070  SQLTypeInfo ti = type_info;
4071 
4072  std::vector<double> coords;
4073  std::vector<double> bounds;
4074  std::vector<int> ring_sizes; // also linestring_sizes
4075  std::vector<int> poly_rings;
4076 
4077  const bool validate_with_geos_if_available = false;
4079  ti,
4080  coords,
4081  bounds,
4082  ring_sizes,
4083  poly_rings,
4084  validate_with_geos_if_available);
4085 
4086  switch (index) {
4087  case 0: // coords
4088  return Geospatial::convert_coords(coords, ti);
4089  case 1: // ring sizes
4090  return Geospatial::convert_rings(ring_sizes);
4091  case 2: // poly rings
4092  return Geospatial::convert_rings(poly_rings);
4093  default:
4094  UNREACHABLE();
4095  }
4096 
4097  UNREACHABLE();
4098  return nullptr;
4099 }
4100 
4101 std::shared_ptr<Analyzer::Expr> GeoConstant::add_cast(const SQLTypeInfo& new_type_info) {
4102  // TODO: we should eliminate the notion of input and output SRIDs on a type. A type can
4103  // only have 1 SRID. A cast or transforms changes the SRID of the type.
4104  // NOTE: SRID 0 indicates set srid, skip cast
4105  SQLTypeInfo cast_type_info = new_type_info;
4106  if (!(get_type_info().get_input_srid() == 0) &&
4107  (get_type_info().get_input_srid() != new_type_info.get_output_srid())) {
4108  // run cast
4109  CHECK(geo_);
4110  if (!geo_->transform(get_type_info().get_input_srid(),
4111  new_type_info.get_output_srid())) {
4112  throw std::runtime_error("Failed to transform constant geometry: " + toString());
4113  }
4114  // The geo constant has been transformed but the new type info still encodes
4115  // a transform. Need to reset it and eliminate srid transition.
4116  cast_type_info.set_input_srid(new_type_info.get_output_srid());
4117  }
4118  return makeExpr<GeoConstant>(std::move(geo_), cast_type_info);
4119 }
4120 
4122  const std::string& name,
4123  const std::vector<std::shared_ptr<Analyzer::Expr>>& args,
4124  const std::optional<int>& output_srid_override)
4125  : GeoExpr(ti)
4126  , name_(name)
4127  , args_(args)
4128  , output_srid_override_(output_srid_override) {}
4129 
4130 std::shared_ptr<Analyzer::Expr> GeoOperator::deep_copy() const {
4131  std::vector<std::shared_ptr<Analyzer::Expr>> args;
4132  for (size_t i = 0; i < args_.size(); i++) {
4133  args.push_back(args_[i]->deep_copy());
4134  }
4135  return makeExpr<GeoOperator>(type_info, name_, args, output_srid_override_);
4136 }
4137 
4138 void GeoOperator::collect_rte_idx(std::set<int>& rte_idx_set) const {
4139  for (size_t i = 0; i < size(); i++) {
4140  const auto expr = getOperand(i);
4141  expr->collect_rte_idx(rte_idx_set);
4142  }
4143 }
4144 
4146  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
4147  bool include_agg) const {
4148  for (size_t i = 0; i < size(); i++) {
4149  const auto expr = getOperand(i);
4150  expr->collect_column_var(colvar_set, include_agg);
4151  }
4152 }
4153 
4154 std::string GeoOperator::toString() const {
4155  std::string str{"(" + name_ + " "};
4156  for (const auto& arg : args_) {
4157  str += arg->toString();
4158  }
4159  str += ")";
4160  return str;
4161 }
4162 
4163 bool GeoOperator::operator==(const Expr& rhs) const {
4164  if (typeid(rhs) != typeid(GeoOperator)) {
4165  return false;
4166  }
4167  const GeoOperator& rhs_go = dynamic_cast<const GeoOperator&>(rhs);
4168  if (getName() != rhs_go.getName()) {
4169  return false;
4170  }
4171  if (rhs_go.size() != size()) {
4172  return false;
4173  }
4174  for (size_t i = 0; i < size(); i++) {
4175  if (args_[i].get() != rhs_go.getOperand(i)) {
4176  return false;
4177  }
4178  }
4179  return true;
4180 }
4181 
4182 size_t GeoOperator::size() const {
4183  return args_.size();
4184 }
4185 
4186 Analyzer::Expr* GeoOperator::getOperand(const size_t index) const {
4187  CHECK_LT(index, args_.size());
4188  return args_[index].get();
4189 }
4190 
4191 std::shared_ptr<Analyzer::Expr> GeoOperator::add_cast(const SQLTypeInfo& new_type_info) {
4192  constexpr bool geo_contains_agg = false;
4193  if (get_type_info().is_geometry()) {
4194  std::vector<std::shared_ptr<Analyzer::Expr>> args;
4195  for (size_t i = 0; i < args_.size(); i++) {
4196  args.push_back(args_[i]->deep_copy());
4197  }
4198  CHECK(new_type_info.is_geometry());
4199  return makeExpr<GeoOperator>(new_type_info, name_, args, output_srid_override_);
4200  } else if (!get_type_info().is_string() && new_type_info.is_string() &&
4201  !new_type_info.is_text_encoding_dict()) {
4202  auto const trans_dict_type = make_transient_dict_type(new_type_info);
4203  return makeExpr<UOper>(trans_dict_type, geo_contains_agg, kCAST, deep_copy());
4204  } else {
4205  return makeExpr<UOper>(new_type_info, geo_contains_agg, kCAST, deep_copy());
4206  }
4207 }
4208 
4209 std::shared_ptr<Analyzer::Expr> GeoTransformOperator::deep_copy() const {
4210  std::vector<std::shared_ptr<Analyzer::Expr>> args;
4211  for (size_t i = 0; i < args_.size(); i++) {
4212  args.push_back(args_[i]->deep_copy());
4213  }
4214  return makeExpr<GeoTransformOperator>(
4216 }
4217 
4218 std::string GeoTransformOperator::toString() const {
4219  std::string str{"(" + name_ + " "};
4220  for (const auto& arg : args_) {
4221  str += arg->toString();
4222  }
4223  str +=
4224  " : " + std::to_string(input_srid_) + " -> " + std::to_string(output_srid_) + " ";
4225  str += ")";
4226  return str;
4227 }
4228 
4229 bool GeoTransformOperator::operator==(const Expr& rhs) const {
4230  if (typeid(rhs) != typeid(GeoTransformOperator)) {
4231  return false;
4232  }
4233  const GeoTransformOperator& rhs_gto = dynamic_cast<const GeoTransformOperator&>(rhs);
4234  if (getName() != rhs_gto.getName()) {
4235  return false;
4236  }
4237  if (rhs_gto.size() != size()) {
4238  return false;
4239  }
4240  for (size_t i = 0; i < size(); i++) {
4241  if (args_[i].get() != rhs_gto.getOperand(i)) {
4242  return false;
4243  }
4244  }
4245  if (input_srid_ != rhs_gto.input_srid_) {
4246  return false;
4247  }
4248  if (output_srid_ != rhs_gto.output_srid_) {
4249  return false;
4250  }
4251  return true;
4252 }
4253 
4255  auto comparator = Analyzer::ColumnVar::colvar_comp;
4256  std::set<const Analyzer::ColumnVar*,
4257  bool (*)(const Analyzer::ColumnVar*, const Analyzer::ColumnVar*)>
4258  colvar_set(comparator);
4259  collect_column_var(colvar_set, true);
4260  if (colvar_set.size() != 1UL) {
4261  return false;
4262  }
4263  auto col_expr_ptr = *colvar_set.begin();
4264  CHECK(col_expr_ptr);
4265  return col_expr_ptr->get_type_info().is_dict_encoded_string();
4266 }
4267 
4268 std::vector<size_t> StringOper::getLiteralArgIndexes() const {
4269  std::vector<size_t> literal_arg_indexes;
4270  const auto num_args = args_.size();
4271  for (size_t idx = 0; idx < num_args; ++idx) {
4272  if (dynamic_cast<const Constant*>(args_[idx].get())) {
4273  literal_arg_indexes.emplace_back(idx);
4274  }
4275  }
4276  return literal_arg_indexes;
4277 }
4278 
4279 using LiteralArgMap = std::map<size_t, std::pair<SQLTypes, Datum>>;
4280 
4282  LiteralArgMap literal_arg_map;
4283  const auto num_args = getArity();
4284  for (size_t idx = 0; idx < num_args; ++idx) {
4285  const auto constant_arg_expr = dynamic_cast<const Analyzer::Constant*>(getArg(idx));
4286  if (constant_arg_expr) {
4287  literal_arg_map.emplace(
4288  std::make_pair(idx,
4289  std::make_pair(constant_arg_expr->get_type_info().get_type(),
4290  constant_arg_expr->get_constval())));
4291  }
4292  }
4293  return literal_arg_map;
4294 }
4295 
4297  const SqlStringOpKind kind,
4298  const std::vector<std::shared_ptr<Analyzer::Expr>>& args) {
4300  << "get_return_type for TRY_STRING_CAST disallowed.";
4301  SQLTypeInfo return_ti(kNULLT);
4302  size_t num_var_string_inputs{0};
4303  for (const auto& arg : args) {
4304  const auto raw_arg = arg.get();
4305  const auto& arg_ti = arg->get_type_info();
4306  if (dynamic_cast<const Analyzer::CaseExpr*>(remove_cast(raw_arg))) {
4307  // Currently we disallow case statement inputs to string functions
4308  // pending a full resolution to QE-359, but the error is thrown
4309  // downstream in StringOper::check_operand_types
4310  return SQLTypeInfo(kTEXT, kENCODING_DICT, 0, kNULLT);
4311  } else if (arg_ti.is_string() && dynamic_cast<const Analyzer::Constant*>(raw_arg)) {
4312  if (return_ti == SQLTypeInfo(kNULLT)) {
4313  return_ti = arg_ti;
4314  }
4315  } else if (arg_ti.is_none_encoded_string()) {
4316  return SQLTypeInfo(kTEXT, kENCODING_DICT, 0, kNULLT);
4317  } else if (arg_ti.is_dict_encoded_string()) {
4318  if (arg_ti.getStringDictKey().isTransientDict()) {
4319  return SQLTypeInfo(kTEXT, kENCODING_DICT, 0, kNULLT);
4320  } else {
4321  num_var_string_inputs++;
4322  return_ti = arg_ti;
4323  }
4324  }
4325  }
4326  if (num_var_string_inputs > 1UL) {
4327  return SQLTypeInfo(kTEXT, kENCODING_DICT, 0, kNULLT);
4328  }
4329  return return_ti;
4330 }
4331 
4333  const size_t min_args,
4334  const std::vector<OperandTypeFamily>& expected_type_families,
4335  const std::vector<std::string>& arg_names,
4336  const bool dict_encoded_cols_only,
4337  const bool cols_first_arg_only) const {
4338  std::ostringstream oss;
4340  oss << "Function " << ::toString(get_kind()) << " not supported.";
4341  throw std::runtime_error(oss.str());
4342  }
4343  const size_t num_args = args_.size();
4344  CHECK_EQ(expected_type_families.size(), arg_names.size());
4345  if (num_args < min_args || num_args > expected_type_families.size()) {
4346  oss << "Error instantiating " << ::toString(get_kind()) << " operator. ";
4347  oss << "Expected " << expected_type_families.size() << " arguments, but received "
4348  << num_args << ".";
4349  }
4350  for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx) {
4351  const auto& expected_type_family = expected_type_families[arg_idx];
4352  // We need to remove any casts that Calcite may add to try the right operand type,
4353  // even if we don't support them. Need to check how this works with casts we do
4354  // support.
4355  auto arg_ti = args_[arg_idx]->get_type_info();
4356  const auto decasted_arg = remove_cast(args_[arg_idx]);
4357  const bool is_arg_case =
4358  dynamic_cast<const Analyzer::CaseExpr*>(decasted_arg.get()) != nullptr;
4359  if (is_arg_case) {
4360  oss << "Error instantiating " << ::toString(get_kind()) << " operator. "
4361  << "Currently string operations cannot be run on output of a case "
4362  << "statement.";
4363  throw std::runtime_error(oss.str());
4364  }
4365  const bool is_arg_constant =
4366  dynamic_cast<const Analyzer::Constant*>(decasted_arg.get()) != nullptr;
4367  auto decasted_arg_ti = decasted_arg->get_type_info();
4368  // We need to prevent any non-string type from being casted to a string, but can
4369  // permit non-integer types being casted to integers Todo: Find a cleaner way to
4370  // handle this (we haven't validated any of the casts that calcite has given us at
4371  // the point of RelAlgTranslation)
4372  if (arg_ti != decasted_arg_ti &&
4373  (arg_ti.is_integer() && decasted_arg_ti.is_string())) {
4374  arg_ti = decasted_arg_ti;
4375  }
4376 
4377  if (cols_first_arg_only && !is_arg_constant && arg_idx >= 1UL) {
4378  oss << "Error instantiating " << ::toString(get_kind()) << " operator. "
4379  << "Currently only column inputs are allowed for argument '" << arg_names[0]
4380  << "', but a column input was received for argument '" << arg_names[arg_idx]
4381  << "'.";
4382  throw std::runtime_error(oss.str());
4383  }
4384  switch (expected_type_family) {
4386  if (!arg_ti.is_string()) {
4387  oss << "Error instantiating " << ::toString(get_kind()) << " operator. "
4388  << "Expected text type for argument '" << arg_names[arg_idx] << "'";
4389  throw std::runtime_error(oss.str());
4390  break;
4391  }
4392  if (dict_encoded_cols_only &&
4393  (!is_arg_constant && !arg_ti.is_dict_encoded_string())) {
4394  oss << "Error instantiating " << ::toString(get_kind()) << " operator. "
4395  << "Currently only text-encoded dictionary-encoded column inputs are "
4396  << "allowed, but a none-encoded text column argument was received.";
4397  throw std::runtime_error(oss.str());
4398  break;
4399  }
4400  break;
4401  }
4403  if (!IS_INTEGER(arg_ti.get_type())) {
4404  oss << "Error instantiating " << ::toString(get_kind()) << " operator. "
4405  << "Expected integer type for argument " << arg_idx + 1 << " ('"
4406  << arg_names[arg_idx] << "').";
4407  throw std::runtime_error(oss.str());
4408  break;
4409  }
4410  if (!is_arg_constant) {
4411  oss << "Error instantiating " << ::toString(get_kind()) << " operator. "
4412  << "Currently only text-encoded dictionary column inputs are "
4413  << "allowed, but an integer-type column was provided.";
4414  throw std::runtime_error(oss.str());
4415  break;
4416  }
4417  break;
4418  }
4419  }
4420  }
4421 }
4422 
4424  const std::vector<std::shared_ptr<Analyzer::Expr>>& operands) {
4425  if (operands.size() != 2UL) {
4426  std::ostringstream oss;
4427  oss << "Concat operator expects two arguments, but was provided " << operands.size()
4428  << ".";
4429  throw std::runtime_error(oss.str());
4430  }
4431  const auto operand0_is_literal =
4432  dynamic_cast<const Analyzer::Constant*>(operands[0].get());
4433  const auto operand1_is_literal =
4434  dynamic_cast<const Analyzer::Constant*>(operands[1].get());
4435  if (operand0_is_literal && !operand1_is_literal) {
4436  return SqlStringOpKind::RCONCAT;
4437  }
4438  return SqlStringOpKind::CONCAT;
4439 }
4440 
4441 std::vector<std::shared_ptr<Analyzer::Expr>> ConcatStringOper::normalize_operands(
4442  const std::vector<std::shared_ptr<Analyzer::Expr>>& operands) {
4444  CHECK_EQ(operands.size(), 2UL); // Size should be 2 per get_concat_ordered_kind
4445  return {operands[1], operands[0]};
4446  }
4447  return operands;
4448 }
4449 
4451  const SqlStringOpKind pad_op_kind) {
4452  if (!(pad_op_kind == SqlStringOpKind::LPAD || pad_op_kind == SqlStringOpKind::RPAD)) {
4453  // Arguably should CHECK here
4454  throw std::runtime_error("Invalid pad type supplied to PAD operator");
4455  }
4456  return pad_op_kind;
4457 }
4458 
4460  const SqlStringOpKind trim_op_kind_maybe,
4461  const std::vector<std::shared_ptr<Analyzer::Expr>>& args) {
4462  auto get_trim_type_if_exists = [&args]() {
4463  if (args.empty()) {
4464  return SqlStringOpKind::INVALID;
4465  }
4466  const auto trim_type_arg = dynamic_cast<const Analyzer::Constant*>(args[0].get());
4467  if (!trim_type_arg) {
4468  return SqlStringOpKind::INVALID;
4469  }
4470  const auto trim_type_str = to_upper(*trim_type_arg->get_constval().stringval);
4471  if (trim_type_str == "BOTH") {
4472  return SqlStringOpKind::TRIM;
4473  }
4474  if (trim_type_str == "LEADING") {
4475  return SqlStringOpKind::LTRIM;
4476  }
4477  if (trim_type_str == "TRAILING") {
4478  return SqlStringOpKind::RTRIM;
4479  }
4480  return SqlStringOpKind::INVALID;
4481  };
4482 
4483  const auto trim_op_kind = trim_op_kind_maybe == SqlStringOpKind::TRIM
4484  ? get_trim_type_if_exists()
4485  : trim_op_kind_maybe;
4487  return trim_op_kind;
4488 }
4489 
4490 std::vector<std::shared_ptr<Analyzer::Expr>> TrimStringOper::normalize_operands(
4491  const std::vector<std::shared_ptr<Analyzer::Expr>>& operands,
4492  const SqlStringOpKind string_op_kind) {
4493  if (operands.size() < 2UL) {
4494  throw std::runtime_error("Trim operator expects 2 arguments.");
4495  }
4496 
4497  // Calcite is returning LTRIM/RTRIM(string_literal1, string_literal2) with the
4498  // arguments in that order, but LTRIM/TRIM(string_var, string_literal) in the
4499  // reverse order that the main TRIM operator uses as well
4500  // Until we can look into that more, reverse order only if we have a const then
4501  // non-const input order
4502 
4503  if (string_op_kind == SqlStringOpKind::LTRIM ||
4504  string_op_kind == SqlStringOpKind::RTRIM) {
4505  CHECK_EQ(operands.size(), 2UL);
4506  if (dynamic_cast<const Analyzer::Constant*>(operands[0].get()) &&
4507  !dynamic_cast<const Analyzer::Constant*>(operands[1].get())) {
4508  return {operands[1], operands[0]};
4509  }
4510  return operands;
4511  }
4512  CHECK_EQ(operands.size(), 3UL);
4513  return {operands[2], operands[1]};
4514 }
4515 
4516 std::vector<std::shared_ptr<Analyzer::Expr>> PositionStringOper::normalize_operands(
4517  const std::vector<std::shared_ptr<Analyzer::Expr>>& operands) {
4518  CHECK_GE(operands.size(), 2UL);
4519  CHECK_LE(operands.size(), 3UL);
4520  std::vector<std::shared_ptr<Analyzer::Expr>> normalized_operands;
4521  normalized_operands.emplace_back(operands[1]);
4522  normalized_operands.emplace_back(operands[0]);
4523  if (operands.size() == 3UL) {
4524  normalized_operands.emplace_back(operands[2]);
4525  }
4526  return normalized_operands;
4527 }
4528 
4529 std::vector<std::shared_ptr<Analyzer::Expr>>
4531  const std::vector<std::shared_ptr<Analyzer::Expr>>& operands) {
4532  if (operands.size() != 2UL) {
4533  std::ostringstream oss;
4534  oss << "JAROWINKLER_SIMILARITY operator expects two arguments, but was provided "
4535  << operands.size() << ".";
4536  throw std::runtime_error(oss.str());
4537  }
4538  const auto operand0_is_literal =
4539  dynamic_cast<const Analyzer::Constant*>(operands[0].get());
4540  const auto operand1_is_literal =
4541  dynamic_cast<const Analyzer::Constant*>(operands[1].get());
4542  if (operand0_is_literal && !operand1_is_literal) {
4543  return {operands[1], operands[0]};
4544  }
4545  return operands;
4546 }
4547 
4548 std::vector<std::shared_ptr<Analyzer::Expr>>
4550  const std::vector<std::shared_ptr<Analyzer::Expr>>& operands) {
4551  if (operands.size() != 2UL) {
4552  std::ostringstream oss;
4553  oss << "LEVENSHTEIN_DISTANCE operator expects two arguments, but was provided "
4554  << operands.size() << ".";
4555  throw std::runtime_error(oss.str());
4556  }
4557  const auto operand0_is_literal =
4558  dynamic_cast<const Analyzer::Constant*>(operands[0].get());
4559  const auto operand1_is_literal =
4560  dynamic_cast<const Analyzer::Constant*>(operands[1].get());
4561  if (operand0_is_literal && !operand1_is_literal) {
4562  return {operands[1], operands[0]};
4563  }
4564  return operands;
4565 }
4566 
4567 } // namespace Analyzer
4568 
4569 bool expr_list_match(const std::vector<std::shared_ptr<Analyzer::Expr>>& lhs,
4570  const std::vector<std::shared_ptr<Analyzer::Expr>>& rhs) {
4571  if (lhs.size() != rhs.size()) {
4572  return false;
4573  }
4574  for (size_t i = 0; i < lhs.size(); ++i) {
4575  if (!(*lhs[i] == *rhs[i])) {
4576  return false;
4577  }
4578  }
4579  return true;
4580 }
4581 
4582 std::shared_ptr<Analyzer::Expr> remove_cast(const std::shared_ptr<Analyzer::Expr>& expr) {
4583  const auto uoper = dynamic_cast<const Analyzer::UOper*>(expr.get());
4584  if (!uoper || uoper->get_optype() != kCAST) {
4585  return expr;
4586  }
4587  return uoper->get_own_operand();
4588 }
4589 
4591  const auto uoper = dynamic_cast<const Analyzer::UOper*>(expr);
4592  if (!uoper || uoper->get_optype() != kCAST) {
4593  return expr;
4594  }
4595  return uoper->get_operand();
4596 }
DEVICE auto upper_bound(ARGS &&...args)
Definition: gpu_enabled.h:123
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1302
int8_t tinyintval
Definition: Datum.h:71
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3881
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1174
Query * next_query
Definition: Analyzer.h:3074
Defines data structures for the semantic analysis phase of query processing.
InIntegerSet(const std::shared_ptr< const Analyzer::Expr > a, const std::vector< int64_t > &values, const bool not_null)
Definition: Analyzer.cpp:1706
Definition: sqldefs.h:71
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2085
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:392
void set_compression(EncodingType c)
Definition: sqltypes.h:479
T roundDecimal(int64_t n, unsigned scale)
Definition: Analyzer.cpp:855
std::string toString() const override
Definition: Analyzer.cpp:3000
Query * parsetree
Definition: Analyzer.h:632
double power10(unsigned const x)
Definition: misc.h:275
float get_likelihood() const
Definition: Analyzer.h:1269
static constexpr int32_t kMaxRepresentableNumericPrecision
Definition: sqltypes.h:60
#define CHECK_EQ(x, y)
Definition: Logger.h:301
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2234
std::shared_ptr< Analyzer::Expr > expr
Definition: Analyzer.h:2993
std::string toString() const override
Definition: Analyzer.cpp:2922
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3297
bool unnest
Definition: Analyzer.h:2994
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1692
InValues(std::shared_ptr< Analyzer::Expr > a, const std::list< std::shared_ptr< Analyzer::Expr >> &l)
Definition: Analyzer.cpp:1688
const Expr * get_from_expr() const
Definition: Analyzer.h:1432
static bool simple_predicate_has_simple_cast(const std::shared_ptr< Analyzer::Expr > cast_operand, const std::shared_ptr< Analyzer::Expr > const_operand)
Definition: Analyzer.cpp:1553
#define IS_LOGIC(X)
Definition: sqldefs.h:61
const Expr * get_partition_count() const
Definition: Analyzer.h:1201
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2617
std::string toString() const override
Definition: Analyzer.cpp:2724
#define NULL_DOUBLE
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3781
GeoConstant(std::unique_ptr< Geospatial::GeoBase > &&geo, const SQLTypeInfo &ti)
Definition: Analyzer.cpp:4014
void get_domain(DomainSet &domain_set) const override
Definition: Analyzer.cpp:3622
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2256
std::string toString() const final
Definition: Analyzer.cpp:4029
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2359
const Expr * get_else_expr() const
Definition: Analyzer.h:1387
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3696
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2505
static std::vector< std::shared_ptr< Analyzer::Expr > > normalize_operands(const std::vector< std::shared_ptr< Analyzer::Expr >> &operands)
Definition: Analyzer.cpp:4516
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:256
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:3841
bool Datum_equal(const SQLTypeInfo &ti, Datum val1, Datum val2)
Definition: Analyzer.cpp:2291
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:460
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2351
void check_operand_types(const size_t min_args, const std::vector< OperandTypeFamily > &expected_type_families, const std::vector< std::string > &arg_names, const bool dict_encoded_cols_only=false, const bool cols_first_arg_only=true) const
Definition: Analyzer.cpp:4332
const Expr * get_escape_expr() const
Definition: Analyzer.h:1064
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:950
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3308
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2163
std::shared_ptr< Analyzer::Expr > decompress()
Definition: Analyzer.cpp:749
size_t getArity() const
Definition: Analyzer.h:2615
std::optional< int > output_srid_override_
Definition: Analyzer.h:3189
Definition: sqltypes.h:76
std::shared_ptr< Analyzer::Expr > remove_cast(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:4582
bool is_null_value(const SQLTypeInfo &ti, const Datum &constval)
Definition: Analyzer.cpp:1233
static bool colvar_comp(const ColumnVar *l, const ColumnVar *r)
Definition: Analyzer.h:215
SQLTypes
Definition: sqltypes.h:65
const std::shared_ptr< Analyzer::Expr > frame_end_bound_
Definition: Analyzer.h:2869
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3776
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1506
bool is_timestamp() const
Definition: sqltypes.h:1044
std::shared_ptr< Analyzer::Expr > operand
Definition: Analyzer.h:424
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3731
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2413
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:904
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3791
std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > expr_pair_list
Definition: Analyzer.h:1414
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1905
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2220
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2634
#define NULL_FLOAT
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3681
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3317
void collect_rte_idx(std::set< int > &rte_idx_set) const final
Definition: Analyzer.cpp:4138
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2206
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2457
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1628
#define NULL_BIGINT
virtual void add_unique(std::list< const Expr * > &expr_list) const
Definition: Analyzer.cpp:3245
std::shared_ptr< Analyzer::Expr > right_operand
Definition: Analyzer.h:530
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3346
std::shared_ptr< Analyzer::Expr > escape_expr
Definition: Analyzer.h:1178
bool operator==(Expr const &rhs) const override
Definition: Analyzer.cpp:2669
ExtractField get_field() const
Definition: Analyzer.h:1431
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2026
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1725
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3512
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:157
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:102
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3580
bool is_fp() const
Definition: sqltypes.h:571
const Expr * get_escape_expr() const
Definition: Analyzer.h:1136
HOST DEVICE int get_scale() const
Definition: sqltypes.h:396
const Expr * get_right_operand() const
Definition: Analyzer.h:456
SQLOps
Definition: sqldefs.h:28
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1871
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3491
std::string toString() const override
Definition: Analyzer.cpp:3193
shared::ColumnKey column_key_
Definition: Analyzer.h:239
Definition: sqldefs.h:34
std::map< size_t, std::pair< SQLTypes, Datum >> LiteralArgMap
Definition: Analyzer.h:1769
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3746
SqlWindowFrameBoundType bound_type_
Definition: Analyzer.h:2721
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3447
int8_t boolval
Definition: Datum.h:70
std::string get_compression_name() const
Definition: sqltypes.h:520
DEVICE int64_t DateTruncate(DatetruncField field, const int64_t timeval)
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:674
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:87
Definition: sqldefs.h:35
const std::vector< std::shared_ptr< Analyzer::Expr > > args0_
Definition: Analyzer.h:2937
SQLTypeInfo ti0_
Definition: Analyzer.h:2967
std::shared_ptr< Analyzer::Expr > like_expr
Definition: Analyzer.h:1111
bool get_is_null() const
Definition: Analyzer.h:347
Definition: sqldefs.h:37
const std::vector< std::shared_ptr< Analyzer::Expr > > args0_
Definition: Analyzer.h:2969
std::string toString() const override
Definition: Analyzer.cpp:3872
VarlenDatum * arrayval
Definition: Datum.h:77
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3550
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3265
int tle_no
Definition: Analyzer.h:2680
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3274
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:165
const std::shared_ptr< const Analyzer::Expr > arg
Definition: Analyzer.h:703
#define UNREACHABLE()
Definition: Logger.h:338
const std::shared_ptr< Analyzer::Expr > get_own_operand() const
Definition: Analyzer.h:385
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:230
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:239
#define CHECK_GE(x, y)
Definition: Logger.h:306
void cast_from_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1208
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3701
SqlStringOpKind
Definition: sqldefs.h:89
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:3617
std::string toString() const override
Definition: Analyzer.cpp:2964
Definition: sqldefs.h:48
Definition: sqldefs.h:29
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1460
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3766
std::shared_ptr< Analyzer::Expr > ExpressionPtr
Definition: Analyzer.h:184
const Geospatial::GeoBase::GeoOp op_
Definition: Analyzer.h:2966
const Expr * get_arg() const
Definition: Analyzer.h:1133
size_t getArity() const
Definition: Analyzer.h:1674
const std::vector< std::shared_ptr< Analyzer::Expr > > order_keys_
Definition: Analyzer.h:2866
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2198
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:3809
Expr * get_arg() const
Definition: Analyzer.h:1330
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2560
DatetruncField get_field() const
Definition: Analyzer.h:1566
std::shared_ptr< Analyzer::Expr > model_value_
Definition: Analyzer.h:774
static SqlStringOpKind get_and_validate_trim_op_kind(const SqlStringOpKind trim_op_kind_maybe, const std::vector< std::shared_ptr< Analyzer::Expr >> &args)
Definition: Analyzer.cpp:4459
Definition: sqldefs.h:40
const Expr * get_arg() const
Definition: Analyzer.h:1267
bool is_expr_nullable(const Analyzer::Expr *expr)
Definition: Analyzer.cpp:1664
std::shared_ptr< Analyzer::Expr > upper_bound_
Definition: Analyzer.h:1251
Constants for Builtin SQL Types supported by HEAVY.AI.
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1442
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1364
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3368
std::string toString(const QueryDescriptionType &type)
Definition: Types.h:64
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:3890
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:4130
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:66
const std::vector< std::shared_ptr< Analyzer::Expr > > args_
Definition: Analyzer.h:2641
bool has_same_dict(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:443
const std::vector< std::shared_ptr< Analyzer::Expr > > partition_keys_
Definition: Analyzer.h:2865
int32_t get_input_srid(const int8_t *flatbuffer)
static std::vector< std::shared_ptr< Analyzer::Expr > > normalize_operands(const std::vector< std::shared_ptr< Analyzer::Expr >> &operands)
Definition: Analyzer.cpp:4441
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3741
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3505
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2539
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
~Constant() override
Definition: Analyzer.cpp:40
std::unique_ptr< Geospatial::GeoBase > geo_
Definition: Analyzer.h:3136
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3428
SQLQualifier qualifier
Definition: Analyzer.h:527
bool isNull() const
Definition: Analyzer.h:2894
#define TRANSIENT_DICT_ID
Definition: DbObjectKeys.h:24
std::shared_ptr< Analyzer::Expr > partition_count_
Definition: Analyzer.h:1252
std::string toString() const override
Definition: Analyzer.cpp:3091
bool is_number() const
Definition: sqltypes.h:574
constexpr int64_t get_datetime_scaled_epoch(const ScalingType direction, const int64_t epoch, const int32_t dimen)
std::string toString() const override
Definition: Analyzer.cpp:2786
SQLTypeInfo type_info
Definition: Analyzer.h:180
std::list< const Expr * > DomainSet
Definition: Analyzer.h:62
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
virtual void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const
Definition: Analyzer.h:117
int32_t intval
Definition: Datum.h:73
const Expr * get_arg() const
Definition: Analyzer.h:1061
bool is_time() const
Definition: sqltypes.h:577
std::string toString() const override
Definition: Analyzer.cpp:2733
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1538
double get_bound_val(const Analyzer::Expr *bound_expr) const
Definition: Analyzer.cpp:3913
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2192
std::string to_string(char const *&&v)
~Subquery() override
Definition: Analyzer.cpp:46
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2577
bool operator==(const Expr &) const override
Definition: Analyzer.cpp:4229
int get_rte_idx(const std::string &range_var_name) const
Definition: Analyzer.cpp:1495
void cast_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1197
const std::vector< std::shared_ptr< Analyzer::Expr > > args_
Definition: Analyzer.h:2864
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:3600
std::string toString() const override
Definition: Analyzer.cpp:2755
std::vector< std::shared_ptr< Analyzer::Expr > > chained_string_op_exprs_
Definition: Analyzer.h:1821
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:76
#define NULL_INT
std::string toString() const override
Definition: Analyzer.cpp:2887
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3751
constexpr double a
Definition: Utm.h:32
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2391
bool g_enable_string_functions
std::string toString() const override
Definition: Analyzer.cpp:2978
std::vector< size_t > getLiteralArgIndexes() const
Definition: Analyzer.cpp:4268
std::string toString() const override
Definition: Analyzer.cpp:3009
std::string toString() const override
Definition: Analyzer.cpp:2945
Definition: sqldefs.h:75
bool get_calc_encoded_length() const
Definition: Analyzer.h:870
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2368
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2076
std::string toString() const override
Definition: Analyzer.cpp:3164
DatetruncField field_
Definition: Analyzer.h:1592
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2040
const FrameBoundType frame_bound_type_
Definition: Analyzer.h:2867
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3786
const Geospatial::GeoBase::GeoOp op_
Definition: Analyzer.h:2935
std::string toString() const override
Definition: Analyzer.cpp:3114
SQLOps get_optype() const
Definition: Analyzer.h:452
std::shared_ptr< Analyzer::Expr > left_operand
Definition: Analyzer.h:529
This file contains the class specification and related data structures for Catalog.
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2651
void set_input_srid(int d)
Definition: sqltypes.h:472
const std::vector< std::shared_ptr< Analyzer::Expr > > args1_
Definition: Analyzer.h:2970
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3437
float floatval
Definition: Datum.h:75
std::string toString() const override
Definition: Analyzer.cpp:2869
std::string toString() const
Definition: Analyzer.cpp:3223
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3796
const std::shared_ptr< Analyzer::Expr > frame_start_bound_
Definition: Analyzer.h:2868
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:115
std::shared_ptr< Analyzer::Expr > normalize_simple_predicate(int &rte_idx) const override
Definition: Analyzer.cpp:1577
const std::list< std::shared_ptr< Analyzer::Expr > > value_list
Definition: Analyzer.h:676
EncodingType
Definition: sqltypes.h:240
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:190
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:183
static SQLTypeInfo common_string_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:452
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:234
std::shared_ptr< Analyzer::Expr > right_operand_
Definition: Analyzer.h:577
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1620
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:199
static SqlStringOpKind validate_trim_op_kind(const SqlStringOpKind trim_op_kind)
Definition: Analyzer.h:2087
bool hasSingleDictEncodedColInput() const
returns whether we have one and only one column involved in this StringOper and all its descendents...
Definition: Analyzer.cpp:4254
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3335
std::shared_ptr< Analyzer::Expr > deep_copy() const final
Definition: Analyzer.cpp:4024
LiteralArgMap getLiteralArgs() const
Definition: Analyzer.cpp:4281
std::string toString() const override
Definition: Analyzer.cpp:2850
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2117
const DatetruncField field_
Definition: Analyzer.h:1550
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2638
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1801
const std::vector< std::shared_ptr< Analyzer::Expr > > & getTuple() const
Definition: Analyzer.h:253
const Expr * get_pc_dimension_value() const
Definition: Analyzer.h:792
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3496
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2240
bool is_castable(const SQLTypeInfo &new_type_info) const
Definition: sqltypes.h:713
void set_fixed_size()
Definition: sqltypes.h:477
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3487
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3518
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:136
std::string toString() const override
Definition: Analyzer.cpp:2857
std::shared_ptr< Analyzer::Expr > lower_bound_
Definition: Analyzer.h:1250
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:144
bool is_integer() const
Definition: sqltypes.h:565
std::string toString() const override
Definition: Analyzer.cpp:2957
const DateaddField field_
Definition: Analyzer.h:1504
std::shared_ptr< Analyzer::Expr > arg1
Definition: Analyzer.h:1367
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:122
#define CHECK_NE(x, y)
Definition: Logger.h:302
std::shared_ptr< Analyzer::Expr > from_expr_
Definition: Analyzer.h:1593
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3393
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1739
bool expr_list_match(const std::vector< std::shared_ptr< Analyzer::Expr >> &lhs, const std::vector< std::shared_ptr< Analyzer::Expr >> &rhs)
Definition: Analyzer.cpp:4569
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3563
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2914
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2434
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:215
std::string toString() const override
Definition: Analyzer.cpp:2717
bool is_text_encoding_dict() const
Definition: sqltypes.h:615
void set_scale(int s)
Definition: sqltypes.h:473
int64_t bigintval
Definition: Datum.h:74
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1526
const std::shared_ptr< Analyzer::Expr > end_
Definition: Analyzer.h:1552
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2600
std::string toString() const override
Definition: Analyzer.cpp:2989
WhichRow get_which_row() const
Definition: Analyzer.h:286
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:3605
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2525
int32_t compute_bucket(double target_const_val) const
Definition: Analyzer.cpp:3968
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3402
const std::shared_ptr< Analyzer::Expr > number_
Definition: Analyzer.h:1505
const std::list< std::shared_ptr< Analyzer::Expr > > value_list
Definition: Analyzer.h:361
bool is_timeinterval() const
Definition: sqltypes.h:592
std::string resname
Definition: Analyzer.h:2992
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3716
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:2910
const Analyzer::Expr * getBoundExpr() const
Definition: Analyzer.h:2697
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2608
const std::string & getName() const
Definition: Analyzer.h:3167
std::string toString() const override
Definition: Analyzer.cpp:3101
int32_t ordered_bucket(double const lower_bound, double const upper_bound, int32_t const partition_count, double const value)
Definition: Analyzer.cpp:3951
static SQLTypeInfo common_numeric_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:500
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3501
SQLTypeInfo ti1_
Definition: Analyzer.h:2968
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2094
Definition: sqldefs.h:36
Definition: sqldefs.h:77
int32_t get_varno() const
Definition: Analyzer.h:288
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1815
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2262
Definition: sqldefs.h:71
static bool getGeoColumns(const std::string &wkt_or_wkb_hex, SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool validate_with_geos_if_available)
Definition: Types.cpp:1121
int16_t smallintval
Definition: Datum.h:72
const std::shared_ptr< Analyzer::Expr > start_
Definition: Analyzer.h:1551
std::string toString() const
Definition: Analyzer.cpp:3233
Datum StringToDatum(const std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:339
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1771
static std::vector< std::shared_ptr< Analyzer::Expr > > normalize_operands(const std::vector< std::shared_ptr< Analyzer::Expr >> &operands)
Definition: Analyzer.cpp:4549
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1829
bool nulls_first
Definition: Analyzer.h:2682
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2058
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3525
SQLTypeInfo make_transient_dict_type(SQLTypeInfo sql_type_info_copy)
Definition: Analyzer.cpp:764
std::string toString() const
Definition: sqltypes.h:523
bool get_is_distinct() const
Definition: Analyzer.h:1332
std::shared_ptr< Analyzer::Constant > convert_rings(const std::vector< int > &rings)
Definition: Conversion.h:44
bool is_boolean() const
Definition: sqltypes.h:580
static std::vector< std::shared_ptr< Analyzer::Expr > > normalize_operands(const std::vector< std::shared_ptr< Analyzer::Expr >> &operands, const SqlStringOpKind string_op_kind)
Definition: Analyzer.cpp:4490
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2379
const std::vector< OrderEntry > collation_
Definition: Analyzer.h:2870
const Expr * get_start_expr() const
Definition: Analyzer.h:1525
void do_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1281
std::string toString() const override
Definition: Analyzer.cpp:2741
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1949
#define NULL_BOOLEAN
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2701
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
WhichRow which_row
Definition: Analyzer.h:309
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2134
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3557
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1919
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3706
static SQLTypeInfo get_return_type(const SqlStringOpKind kind, const std::vector< std::shared_ptr< Analyzer::Expr >> &args)
Definition: Analyzer.cpp:4296
int get_precision() const
Definition: sqltypes.h:394
std::string * stringval
Definition: Datum.h:79
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) final
Definition: Analyzer.cpp:4101
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3801
size_t getElementCount() const
Definition: Analyzer.h:2892
std::string to_upper(const std::string &str)
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1110
const std::vector< std::shared_ptr< Analyzer::Expr > > tuple_
Definition: Analyzer.h:263
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:4209
std::vector< std::shared_ptr< Analyzer::Expr > > args_
Definition: Analyzer.h:1820
Definition: sqldefs.h:33
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3457
void set_comp_param(int p)
Definition: sqltypes.h:480
const std::vector< int64_t > value_list
Definition: Analyzer.h:704
std::map< size_t, std::pair< SQLTypes, Datum >> LiteralArgMap
Definition: Analyzer.cpp:4279
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3477
const Expr * get_pattern_expr() const
Definition: Analyzer.h:1135
#define CHECK_LT(x, y)
Definition: Logger.h:303
Definition: sqltypes.h:79
Definition: sqltypes.h:80
Definition: sqldefs.h:39
ExpressionPtrVector contained_expressions_
Definition: Analyzer.h:2908
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2108
SqlStringOpKind kind_
Definition: Analyzer.h:1819
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3572
Definition: sqldefs.h:71
Expression class for string functions The &quot;arg&quot; constructor parameter must be an expression that reso...
Definition: Analyzer.h:1601
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1885
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:161
const Expr * get_from_expr() const
Definition: Analyzer.h:1567
virtual ~Query()
Definition: Analyzer.cpp:50
const shared::ColumnKey & getColumnKey() const
Definition: Analyzer.h:198
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3413
bool is_in_values_nullable(const std::shared_ptr< Analyzer::Expr > &a, const std::list< std::shared_ptr< Analyzer::Expr >> &l)
Definition: Analyzer.cpp:1673
DEVICE auto lower_bound(ARGS &&...args)
Definition: gpu_enabled.h:78
#define CHECK_LE(x, y)
Definition: Logger.h:304
const std::shared_ptr< Analyzer::Expr > bound_expr_
Definition: Analyzer.h:2722
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1478
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:263
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:211
SqlStringOpKind get_kind() const
Definition: Analyzer.h:1672
std::string toString() const override
Definition: Analyzer.cpp:3087
const Expr * get_like_expr() const
Definition: Analyzer.h:1063
int64_t convert_decimal_value_to_scale(const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
Definition: Datum.cpp:624
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1963
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:2617
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:62
void set_dimension(int d)
Definition: sqltypes.h:470
void setStringDictKey(const shared::StringDictKey &dict_key)
Definition: sqltypes.h:1061
std::string toString() const override
Definition: Analyzer.cpp:3105
const Expr * get_operand() const
Definition: Analyzer.h:384
std::shared_ptr< Analyzer::Expr > pc_dimension_value_
Definition: Analyzer.h:856
Analyzer::ExpressionPtr deep_copy() const override
Definition: Analyzer.cpp:251
Datum get_constval() const
Definition: Analyzer.h:348
std::vector< std::shared_ptr< Analyzer::Expr > > regressor_values_
Definition: Analyzer.h:775
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3381
Definition: sqldefs.h:31
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2282
const Expr * get_arg() const
Definition: Analyzer.h:962
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) final
Definition: Analyzer.cpp:4191
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1857
Definition: sqldefs.h:78
const Expr * get_model_value() const
Definition: Analyzer.h:788
bool isTransientDict() const
int64_t safeScale(T from, unsigned const scale)
Definition: Analyzer.cpp:893
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1416
const Expr * get_arg() const
Definition: Analyzer.h:868
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:393
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:646
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3326
int32_t get_partition_count_val() const
Definition: Analyzer.cpp:3923
std::string toString() const override
Definition: Analyzer.cpp:3096
std::vector< RangeTableEntry * > rangetable
Definition: Analyzer.h:3067
GeoOperator(const SQLTypeInfo &ti, const std::string &name, const std::vector< std::shared_ptr< Analyzer::Expr >> &args, const std::optional< int > &output_srid_override=std::nullopt)
Definition: Analyzer.cpp:4121
std::shared_ptr< Analyzer::Expr > from_expr_
Definition: Analyzer.h:1458
#define IS_INTEGER(T)
Definition: sqltypes.h:304
std::string get_type_name() const
Definition: sqltypes.h:482
std::string toString() const override
Definition: Analyzer.cpp:3015
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2248
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1785
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1843
Definition: sqltypes.h:68
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:774
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1533
void cast_number(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:922
virtual std::shared_ptr< Analyzer::Expr > deep_copy() const =0
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:995
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDate(const int64_t timeval, const int64_t scale)
size_t size() const
Definition: Analyzer.cpp:4182
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:70
SQLTypeInfo ti0_
Definition: Analyzer.h:2936
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:402
static const StringDictKey kTransientDictKey
Definition: DbObjectKeys.h:45
DateaddField get_field() const
Definition: Analyzer.h:1476
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1755
SQLAgg get_aggtype() const
Definition: Analyzer.h:1329
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3286
#define NULL_TINYINT
std::string toString() const override
Definition: Analyzer.cpp:3149
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:177
const Expr * get_target_value() const
Definition: Analyzer.h:1198
const std::shared_ptr< Analyzer::Expr > datetime_
Definition: Analyzer.h:1506
const std::vector< std::shared_ptr< Analyzer::Expr > > & get_feature_values() const
Definition: Analyzer.h:789
std::string toString() const override
Definition: Analyzer.cpp:3863
const Expr * get_arg() const
Definition: Analyzer.h:917
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3736
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3756
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1040
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2067
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3721
std::shared_ptr< Analyzer::Constant > convert_coords(const std::vector< double > &coords, const SQLTypeInfo &ti)
Definition: Conversion.h:26
void set_notnull(bool n)
Definition: sqltypes.h:475
T floatFromDecimal(int64_t const dec, unsigned const scale)
Definition: Analyzer.cpp:820
const Expr * get_end_expr() const
Definition: Analyzer.h:1526
#define CHECK(condition)
Definition: Logger.h:291
bool is_geometry() const
Definition: sqltypes.h:595
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2468
ExtractField field_
Definition: Analyzer.h:1457
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2214
bool is_desc
Definition: Analyzer.h:2681
const Expr * get_model_value() const
Definition: Analyzer.h:713
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1489
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
bool is_high_precision_timestamp() const
Definition: sqltypes.h:1034
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2402
void cast_to_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1216
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3711
#define NULL_SMALLINT
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2049
#define COMMUTE_COMPARISON(X)
Definition: sqldefs.h:64
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:153
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3466
static SqlStringOpKind get_and_validate_pad_op_kind(const SqlStringOpKind pad_op_kind)
Definition: Analyzer.cpp:4450
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1642
std::shared_ptr< Analyzer::Expr > left_operand_
Definition: Analyzer.h:576
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3255
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:128
std::string toString() const override
Definition: Analyzer.cpp:4218
Definition: sqldefs.h:32
Analyzer::Expr * getOperand(const size_t index) const
Definition: Analyzer.cpp:4186
bool operator==(const Expr &) const final
Definition: Analyzer.cpp:4042
std::string toString() const override
Definition: Analyzer.cpp:3109
std::shared_ptr< Analyzer::Expr > escape_expr
Definition: Analyzer.h:1113
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1711
bool is_any() const
Definition: sqltypes.h:556
const Expr * get_left_operand() const
Definition: Analyzer.h:455
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:225
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:106
static std::vector< std::shared_ptr< Analyzer::Expr > > normalize_operands(const std::vector< std::shared_ptr< Analyzer::Expr >> &operands)
Definition: Analyzer.cpp:4530
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3537
bool is_dict_encoded_string() const
Definition: sqltypes.h:641
Definition: sqltypes.h:72
int32_t varno
Definition: Analyzer.h:312
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2177
const SqlWindowFunctionKind kind_
Definition: Analyzer.h:2863
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1510
std::shared_ptr< Analyzer::Constant > makePhysicalConstant(const size_t index) const
Definition: Analyzer.cpp:4064
const Expr * get_lower_bound() const
Definition: Analyzer.h:1199
std::string toString() const override
Definition: Analyzer.cpp:2883
bool is_string() const
Definition: sqltypes.h:559
void find_expr(std::function< bool(const Expr *)> f, std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3355
std::string getName() const
Definition: Analyzer.h:2613
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const final
Definition: Analyzer.cpp:4145
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2626
bool expr_is(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:1547
string name
Definition: setup.in.py:72
constexpr double n
Definition: Utm.h:38
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:398
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2334
std::string getWKTString() const
Definition: Analyzer.cpp:4037
virtual size_t get_num_column_vars(const bool include_agg) const
Definition: Analyzer.cpp:58
std::string toString() const override
Definition: Analyzer.cpp:2971
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1934
const std::vector< std::shared_ptr< Analyzer::Expr > > & get_regressor_values() const
Definition: Analyzer.h:714
std::string toString() const override
Definition: Analyzer.cpp:4154
Definition: sqldefs.h:76
const Expr * get_upper_bound() const
Definition: Analyzer.h:1200
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:169
int32_t get_rte_idx() const
Definition: Analyzer.h:202
Definition: Datum.h:69
bool operator==(const Expr &) const override
Definition: Analyzer.cpp:4163
bool contains_agg
Definition: Analyzer.h:181
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:975
bool is_decimal() const
Definition: sqltypes.h:568
const Expr * get_arg() const
Definition: Analyzer.h:1007
DatetruncField get_field() const
Definition: Analyzer.h:1524
int get_physical_coord_cols() const
Definition: sqltypes.h:449
Definition: sqldefs.h:74
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3771
std::list< OrderEntry > * order_by
Definition: Analyzer.h:3073
size_t physicalCols() const
Definition: Analyzer.cpp:4059
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:91
std::shared_ptr< Analyzer::Expr > pattern_expr
Definition: Analyzer.h:1176
const std::vector< std::shared_ptr< Analyzer::Expr > > args_
Definition: Analyzer.h:3186
Definition: sqldefs.h:38
SQLTypes get_ti_from_geo(const Geospatial::GeoBase *geo)
Definition: Analyzer.cpp:3984
const Expr * getArg(const size_t i) const
Definition: Analyzer.h:1688
bool hasFraming() const
Definition: Analyzer.h:2830
#define TRANSIENT_DICT(ID)
Definition: sqltypes.h:322
Definition: sqldefs.h:83
std::shared_ptr< Analyzer::Expr > model_value_
Definition: Analyzer.h:854
std::string toString() const override
Definition: Analyzer.cpp:3070
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:3611
std::shared_ptr< Analyzer::Expr > else_expr
Definition: Analyzer.h:1417
const Expr * get_arg() const
Definition: Analyzer.h:644
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:194
static SQLTypeInfo analyze_type_info(SQLOps op, const SQLTypeInfo &left_type, const SQLTypeInfo &right_type, SQLTypeInfo *new_left_type, SQLTypeInfo *new_right_type)
Definition: Analyzer.cpp:267
SQLOps get_optype() const
Definition: Analyzer.h:383
bool is_date() const
Definition: sqltypes.h:1026
bool is_array() const
Definition: sqltypes.h:583
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1384
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3761
static SqlStringOpKind get_concat_ordered_kind(const std::vector< std::shared_ptr< Analyzer::Expr >> &operands)
Definition: Analyzer.cpp:4423
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3543
std::shared_ptr< Analyzer::Expr > target_value_
Definition: Analyzer.h:1249
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:3589
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2171
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:220
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2487
#define IS_COMPARISON(X)
Definition: sqldefs.h:58
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2689
SQLTypeInfo get_nullable_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:1482
const std::string name_
Definition: Analyzer.h:3185
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2000
double doubleval
Definition: Datum.h:76
SQLOps optype
Definition: Analyzer.h:423
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:397
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3726
const Expr * get_number_expr() const
Definition: Analyzer.h:1477
const std::string name_
Definition: Analyzer.h:2640
bool get_is_ilike() const
Definition: Analyzer.h:1065
virtual GeoType getType() const =0
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:2896
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2149
const shared::StringDictKey & getStringDictKey() const
Definition: sqltypes.h:1055
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1977
std::vector< std::shared_ptr< Analyzer::Expr > > feature_values_
Definition: Analyzer.h:855
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2155
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:468