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