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