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