OmniSciDB  6686921089
 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 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
25 #include "Analyzer.h"
26 #include "Catalog/Catalog.h"
27 #include "Geospatial/Conversion.h"
28 #include "Geospatial/Types.h"
30 #include "RangeTableEntry.h"
31 #include "Shared/DateConverters.h"
32 #include "Shared/misc.h"
33 #include "Shared/sqltypes.h"
34 
35 #include <algorithm>
36 #include <cstring>
37 #include <iostream>
38 #include <stdexcept>
39 
40 namespace Analyzer {
41 
43  if ((type_info.is_string() || type_info.is_geometry()) && !is_null) {
44  delete constval.stringval;
45  }
46 }
47 
49  delete parsetree;
50 }
51 
53  for (auto p : rangetable) {
54  delete p;
55  }
56  delete order_by;
57  delete next_query;
58 }
59 
60 std::shared_ptr<Analyzer::Expr> ColumnVar::deep_copy() const {
61  return makeExpr<ColumnVar>(type_info, table_id, column_id, rte_idx);
62 }
63 
64 void ExpressionTuple::collect_rte_idx(std::set<int>& rte_idx_set) const {
65  for (const auto& column : tuple_) {
66  column->collect_rte_idx(rte_idx_set);
67  }
68 }
69 
70 std::shared_ptr<Analyzer::Expr> ExpressionTuple::deep_copy() const {
71  std::vector<std::shared_ptr<Expr>> tuple_deep_copy;
72  for (const auto& column : tuple_) {
73  const auto column_deep_copy =
74  std::dynamic_pointer_cast<Analyzer::ColumnVar>(column->deep_copy());
75  CHECK(column_deep_copy);
76  tuple_deep_copy.push_back(column_deep_copy);
77  }
78  return makeExpr<ExpressionTuple>(tuple_deep_copy);
79 }
80 
81 std::shared_ptr<Analyzer::Expr> Var::deep_copy() const {
82  return makeExpr<Var>(type_info, table_id, column_id, rte_idx, which_row, varno);
83 }
84 
85 std::shared_ptr<Analyzer::Expr> Constant::deep_copy() const {
86  Datum d = constval;
87  if ((type_info.is_string() || type_info.is_geometry()) && !is_null) {
88  d.stringval = new std::string(*constval.stringval);
89  }
90  if (type_info.get_type() == kARRAY) {
91  return makeExpr<Constant>(type_info, is_null, value_list);
92  }
93  return makeExpr<Constant>(type_info, is_null, d);
94 }
95 
96 std::shared_ptr<Analyzer::Expr> UOper::deep_copy() const {
97  return makeExpr<UOper>(type_info, contains_agg, optype, operand->deep_copy());
98 }
99 
100 std::shared_ptr<Analyzer::Expr> BinOper::deep_copy() const {
101  return makeExpr<BinOper>(type_info,
102  contains_agg,
103  optype,
104  qualifier,
105  left_operand->deep_copy(),
106  right_operand->deep_copy());
107 }
108 
109 std::shared_ptr<Analyzer::Expr> RangeOper::deep_copy() const {
110  return makeExpr<RangeOper>(left_inclusive_,
112  left_operand_->deep_copy(),
113  right_operand_->deep_copy());
114 }
115 
116 std::shared_ptr<Analyzer::Expr> Subquery::deep_copy() const {
117  // not supported yet.
118  CHECK(false);
119  return nullptr;
120 }
121 
122 std::shared_ptr<Analyzer::Expr> InValues::deep_copy() const {
123  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
124  for (auto p : value_list) {
125  new_value_list.push_back(p->deep_copy());
126  }
127  return makeExpr<InValues>(arg->deep_copy(), new_value_list);
128 }
129 
130 std::shared_ptr<Analyzer::Expr> CharLengthExpr::deep_copy() const {
131  return makeExpr<CharLengthExpr>(arg->deep_copy(), calc_encoded_length);
132 }
133 
134 std::shared_ptr<Analyzer::Expr> KeyForStringExpr::deep_copy() const {
135  return makeExpr<KeyForStringExpr>(arg->deep_copy());
136 }
137 
138 std::shared_ptr<Analyzer::Expr> SampleRatioExpr::deep_copy() const {
139  return makeExpr<SampleRatioExpr>(arg->deep_copy());
140 }
141 
142 std::shared_ptr<Analyzer::Expr> LowerExpr::deep_copy() const {
143  return makeExpr<LowerExpr>(arg->deep_copy());
144 }
145 
146 std::shared_ptr<Analyzer::Expr> CardinalityExpr::deep_copy() const {
147  return makeExpr<CardinalityExpr>(arg->deep_copy());
148 }
149 
150 std::shared_ptr<Analyzer::Expr> LikeExpr::deep_copy() const {
151  return makeExpr<LikeExpr>(arg->deep_copy(),
152  like_expr->deep_copy(),
153  escape_expr ? escape_expr->deep_copy() : nullptr,
154  is_ilike,
155  is_simple);
156 }
157 
158 std::shared_ptr<Analyzer::Expr> RegexpExpr::deep_copy() const {
159  return makeExpr<RegexpExpr>(arg->deep_copy(),
160  pattern_expr->deep_copy(),
161  escape_expr ? escape_expr->deep_copy() : nullptr);
162 }
163 
164 std::shared_ptr<Analyzer::Expr> WidthBucketExpr::deep_copy() const {
165  return makeExpr<WidthBucketExpr>(target_value_->deep_copy(),
166  lower_bound_->deep_copy(),
167  upper_bound_->deep_copy(),
168  partition_count_->deep_copy());
169 }
170 
171 std::shared_ptr<Analyzer::Expr> LikelihoodExpr::deep_copy() const {
172  return makeExpr<LikelihoodExpr>(arg->deep_copy(), likelihood);
173 }
174 
175 std::shared_ptr<Analyzer::Expr> AggExpr::deep_copy() const {
176  return makeExpr<AggExpr>(
177  type_info, aggtype, arg ? arg->deep_copy() : nullptr, is_distinct, arg1);
178 }
179 
180 std::shared_ptr<Analyzer::Expr> CaseExpr::deep_copy() const {
181  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
182  new_list;
183  for (auto p : expr_pair_list) {
184  new_list.emplace_back(p.first->deep_copy(), p.second->deep_copy());
185  }
186  return makeExpr<CaseExpr>(type_info,
187  contains_agg,
188  new_list,
189  else_expr == nullptr ? nullptr : else_expr->deep_copy());
190 }
191 
192 std::shared_ptr<Analyzer::Expr> ExtractExpr::deep_copy() const {
193  return makeExpr<ExtractExpr>(type_info, contains_agg, field_, from_expr_->deep_copy());
194 }
195 
196 std::shared_ptr<Analyzer::Expr> DateaddExpr::deep_copy() const {
197  return makeExpr<DateaddExpr>(
198  type_info, field_, number_->deep_copy(), datetime_->deep_copy());
199 }
200 
201 std::shared_ptr<Analyzer::Expr> DatediffExpr::deep_copy() const {
202  return makeExpr<DatediffExpr>(
203  type_info, field_, start_->deep_copy(), end_->deep_copy());
204 }
205 
206 std::shared_ptr<Analyzer::Expr> DatetruncExpr::deep_copy() const {
207  return makeExpr<DatetruncExpr>(
208  type_info, contains_agg, field_, from_expr_->deep_copy());
209 }
210 
211 std::shared_ptr<Analyzer::Expr> OffsetInFragment::deep_copy() const {
212  return makeExpr<OffsetInFragment>();
213 }
214 
215 std::shared_ptr<Analyzer::Expr> WindowFunction::deep_copy() const {
216  return makeExpr<WindowFunction>(
218 }
219 
221  return makeExpr<Analyzer::ArrayExpr>(
223 }
224 
225 std::shared_ptr<Analyzer::Expr> GeoUOper::deep_copy() const {
227  return makeExpr<GeoUOper>(op_, type_info, type_info, args0_);
228  }
229  return makeExpr<GeoUOper>(op_, type_info, ti0_, args0_);
230 }
231 
232 std::shared_ptr<Analyzer::Expr> GeoBinOper::deep_copy() const {
233  return makeExpr<GeoBinOper>(op_, type_info, ti0_, ti1_, args0_, args1_);
234 }
235 
237  const SQLTypeInfo& left_type,
238  const SQLTypeInfo& right_type,
239  SQLTypeInfo* new_left_type,
240  SQLTypeInfo* new_right_type) {
241  SQLTypeInfo result_type;
242  SQLTypeInfo common_type;
243  *new_left_type = left_type;
244  *new_right_type = right_type;
245  if (IS_LOGIC(op)) {
246  if (left_type.get_type() != kBOOLEAN || right_type.get_type() != kBOOLEAN) {
247  throw std::runtime_error(
248  "non-boolean operands cannot be used in logic operations.");
249  }
250  result_type = SQLTypeInfo(kBOOLEAN, false);
251  } else if (IS_COMPARISON(op)) {
252  if (left_type != right_type) {
253  if (left_type.is_number() && right_type.is_number()) {
254  common_type = common_numeric_type(left_type, right_type);
255  *new_left_type = common_type;
256  new_left_type->set_notnull(left_type.get_notnull());
257  *new_right_type = common_type;
258  new_right_type->set_notnull(right_type.get_notnull());
259  } else if (left_type.is_time() && right_type.is_time()) {
260  switch (left_type.get_type()) {
261  case kTIMESTAMP:
262  switch (right_type.get_type()) {
263  case kTIME:
264  throw std::runtime_error("Cannont compare between TIMESTAMP and TIME.");
265  break;
266  case kDATE:
267  *new_left_type = SQLTypeInfo(left_type.get_type(),
268  left_type.get_dimension(),
269  0,
270  left_type.get_notnull());
271  *new_right_type = *new_left_type;
272  new_right_type->set_notnull(right_type.get_notnull());
273  break;
274  case kTIMESTAMP:
275  *new_left_type = SQLTypeInfo(
276  kTIMESTAMP,
277  std::max(left_type.get_dimension(), right_type.get_dimension()),
278  0,
279  left_type.get_notnull());
280  *new_right_type = SQLTypeInfo(
281  kTIMESTAMP,
282  std::max(left_type.get_dimension(), right_type.get_dimension()),
283  0,
284  right_type.get_notnull());
285  break;
286  default:
287  CHECK(false);
288  }
289  break;
290  case kTIME:
291  switch (right_type.get_type()) {
292  case kTIMESTAMP:
293  throw std::runtime_error("Cannont compare between TIME and TIMESTAMP.");
294  break;
295  case kDATE:
296  throw std::runtime_error("Cannont compare between TIME and DATE.");
297  break;
298  case kTIME:
299  *new_left_type = SQLTypeInfo(
300  kTIME,
301  std::max(left_type.get_dimension(), right_type.get_dimension()),
302  0,
303  left_type.get_notnull());
304  *new_right_type = SQLTypeInfo(
305  kTIME,
306  std::max(left_type.get_dimension(), right_type.get_dimension()),
307  0,
308  right_type.get_notnull());
309  break;
310  default:
311  CHECK(false);
312  }
313  break;
314  case kDATE:
315  switch (right_type.get_type()) {
316  case kTIMESTAMP:
317  *new_left_type = SQLTypeInfo(right_type.get_type(),
318  right_type.get_dimension(),
319  0,
320  left_type.get_notnull());
321  *new_right_type = *new_left_type;
322  new_right_type->set_notnull(right_type.get_notnull());
323  break;
324  case kDATE:
325  *new_left_type = SQLTypeInfo(left_type.get_type(),
326  left_type.get_dimension(),
327  0,
328  left_type.get_notnull());
329  *new_right_type = *new_left_type;
330  new_right_type->set_notnull(right_type.get_notnull());
331  break;
332  case kTIME:
333  throw std::runtime_error("Cannont compare between DATE and TIME.");
334  break;
335  default:
336  CHECK(false);
337  }
338  break;
339  default:
340  CHECK(false);
341  }
342  } else if (left_type.is_string() && right_type.is_time()) {
343  *new_left_type = right_type;
344  new_left_type->set_notnull(left_type.get_notnull());
345  *new_right_type = right_type;
346  } else if (left_type.is_time() && right_type.is_string()) {
347  *new_left_type = left_type;
348  *new_right_type = left_type;
349  new_right_type->set_notnull(right_type.get_notnull());
350  } else if (left_type.is_string() && right_type.is_string()) {
351  *new_left_type = left_type;
352  *new_right_type = right_type;
353  } else if (left_type.is_boolean() && right_type.is_boolean()) {
354  const bool notnull = left_type.get_notnull() && right_type.get_notnull();
355  common_type = SQLTypeInfo(kBOOLEAN, notnull);
356  *new_left_type = common_type;
357  *new_right_type = common_type;
358  } else {
359  throw std::runtime_error("Cannot compare between " + left_type.get_type_name() +
360  " and " + right_type.get_type_name());
361  }
362  }
363  result_type = SQLTypeInfo(kBOOLEAN, false);
364  } else if (op == kMINUS &&
365  (left_type.get_type() == kDATE || left_type.get_type() == kTIMESTAMP) &&
366  right_type.is_timeinterval()) {
367  *new_left_type = left_type;
368  *new_right_type = right_type;
369  result_type = left_type;
370  } else if (IS_ARITHMETIC(op)) {
371  if (!(left_type.is_number() || left_type.is_timeinterval()) ||
372  !(right_type.is_number() || right_type.is_timeinterval())) {
373  throw std::runtime_error("non-numeric operands in arithmetic operations.");
374  }
375  if (op == kMODULO && (!left_type.is_integer() || !right_type.is_integer())) {
376  throw std::runtime_error("non-integer operands in modulo operation.");
377  }
378  common_type = common_numeric_type(left_type, right_type);
379  if (common_type.is_decimal()) {
380  if (op == kMULTIPLY) {
381  // Decimal multiplication requires common_type adjustment:
382  // dimension and scale of the result should be increased.
383  auto new_dimension = left_type.get_dimension() + right_type.get_dimension();
384  // If new dimension is over 20 digits, the result may overflow, or it may not.
385  // Rely on the runtime overflow detection rather than a static check here.
386  if (common_type.get_dimension() < new_dimension) {
387  common_type.set_dimension(new_dimension);
388  }
389  common_type.set_scale(left_type.get_scale() + right_type.get_scale());
390  } else if (op == kPLUS || op == kMINUS) {
391  // Scale should remain the same but dimension could actually go up
392  common_type.set_dimension(common_type.get_dimension() + 1);
393  }
394  }
395  *new_left_type = common_type;
396  new_left_type->set_notnull(left_type.get_notnull());
397  *new_right_type = common_type;
398  new_right_type->set_notnull(right_type.get_notnull());
399  if (op == kMULTIPLY) {
400  new_left_type->set_scale(left_type.get_scale());
401  new_right_type->set_scale(right_type.get_scale());
402  }
403  result_type = common_type;
404  } else {
405  throw std::runtime_error("invalid binary operator type.");
406  }
407  result_type.set_notnull(left_type.get_notnull() && right_type.get_notnull());
408  return result_type;
409 }
410 
412  const SQLTypeInfo& type2) {
413  SQLTypeInfo common_type;
415  int comp_param = 0;
416  CHECK(type1.is_string() && type2.is_string());
417  // if type1 and type2 have the same DICT encoding then keep it
418  // otherwise, they must be decompressed
419  if (type1.get_compression() == kENCODING_DICT &&
420  type2.get_compression() == kENCODING_DICT) {
421  if (type1.get_comp_param() == type2.get_comp_param() ||
422  type1.get_comp_param() == TRANSIENT_DICT(type2.get_comp_param())) {
423  comp = kENCODING_DICT;
424  comp_param = std::min(type1.get_comp_param(), type2.get_comp_param());
425  }
426  } else if (type1.get_compression() == kENCODING_DICT &&
427  type2.get_compression() == kENCODING_NONE) {
428  comp_param = type1.get_comp_param();
429  } else if (type1.get_compression() == kENCODING_NONE &&
430  type2.get_compression() == kENCODING_DICT) {
431  comp_param = type2.get_comp_param();
432  } else {
433  comp_param = std::max(type1.get_comp_param(),
434  type2.get_comp_param()); // preserve previous comp_param if set
435  }
436  const bool notnull = type1.get_notnull() && type2.get_notnull();
437  if (type1.get_type() == kTEXT || type2.get_type() == kTEXT) {
438  common_type = SQLTypeInfo(kTEXT, 0, 0, notnull, comp, comp_param, kNULLT);
439  return common_type;
440  }
441  common_type = SQLTypeInfo(kVARCHAR,
442  std::max(type1.get_dimension(), type2.get_dimension()),
443  0,
444  notnull,
445  comp,
446  comp_param,
447  kNULLT);
448  return common_type;
449 }
450 
452  const SQLTypeInfo& type2) {
453  SQLTypeInfo common_type;
454  const bool notnull = type1.get_notnull() && type2.get_notnull();
455  if (type1.get_type() == type2.get_type()) {
456  CHECK(((type1.is_number() || type1.is_timeinterval()) &&
457  (type2.is_number() || type2.is_timeinterval())) ||
458  (type1.is_boolean() && type2.is_boolean()));
459  common_type = SQLTypeInfo(type1.get_type(),
460  std::max(type1.get_dimension(), type2.get_dimension()),
461  std::max(type1.get_scale(), type2.get_scale()),
462  notnull);
463  return common_type;
464  }
465  std::string timeinterval_op_error{
466  "Operator type not supported for time interval arithmetic: "};
467  if (type1.is_timeinterval()) {
468  if (!type2.is_integer()) {
469  throw std::runtime_error(timeinterval_op_error + type2.get_type_name());
470  }
471  return type1;
472  }
473  if (type2.is_timeinterval()) {
474  if (!type1.is_integer()) {
475  throw std::runtime_error(timeinterval_op_error + type1.get_type_name());
476  }
477  return type2;
478  }
479  CHECK(type1.is_number() && type2.is_number());
480  switch (type1.get_type()) {
481  case kTINYINT:
482  switch (type2.get_type()) {
483  case kSMALLINT:
484  common_type = SQLTypeInfo(kSMALLINT, notnull);
485  break;
486  case kINT:
487  common_type = SQLTypeInfo(kINT, notnull);
488  break;
489  case kBIGINT:
490  common_type = SQLTypeInfo(kBIGINT, notnull);
491  break;
492  case kFLOAT:
493  common_type = SQLTypeInfo(kFLOAT, notnull);
494  break;
495  case kDOUBLE:
496  common_type = SQLTypeInfo(kDOUBLE, notnull);
497  break;
498  case kNUMERIC:
499  case kDECIMAL:
500  common_type =
502  std::max(5 + type2.get_scale(), type2.get_dimension()),
503  type2.get_scale(),
504  notnull);
505  break;
506  default:
507  CHECK(false);
508  }
509  break;
510  case kSMALLINT:
511  switch (type2.get_type()) {
512  case kTINYINT:
513  common_type = SQLTypeInfo(kSMALLINT, notnull);
514  break;
515  case kINT:
516  common_type = SQLTypeInfo(kINT, notnull);
517  break;
518  case kBIGINT:
519  common_type = SQLTypeInfo(kBIGINT, notnull);
520  break;
521  case kFLOAT:
522  common_type = SQLTypeInfo(kFLOAT, notnull);
523  break;
524  case kDOUBLE:
525  common_type = SQLTypeInfo(kDOUBLE, notnull);
526  break;
527  case kNUMERIC:
528  case kDECIMAL:
529  common_type =
531  std::max(5 + type2.get_scale(), type2.get_dimension()),
532  type2.get_scale(),
533  notnull);
534  break;
535  default:
536  CHECK(false);
537  }
538  break;
539  case kINT:
540  switch (type2.get_type()) {
541  case kTINYINT:
542  common_type = SQLTypeInfo(kINT, notnull);
543  break;
544  case kSMALLINT:
545  common_type = SQLTypeInfo(kINT, notnull);
546  break;
547  case kBIGINT:
548  common_type = SQLTypeInfo(kBIGINT, notnull);
549  break;
550  case kFLOAT:
551  common_type = SQLTypeInfo(kFLOAT, notnull);
552  break;
553  case kDOUBLE:
554  common_type = SQLTypeInfo(kDOUBLE, notnull);
555  break;
556  case kNUMERIC:
557  case kDECIMAL:
558  common_type = SQLTypeInfo(
559  kDECIMAL,
560  std::max(std::min(19, 10 + type2.get_scale()), type2.get_dimension()),
561  type2.get_scale(),
562  notnull);
563  break;
564  default:
565  CHECK(false);
566  }
567  break;
568  case kBIGINT:
569  switch (type2.get_type()) {
570  case kTINYINT:
571  common_type = SQLTypeInfo(kBIGINT, notnull);
572  break;
573  case kSMALLINT:
574  common_type = SQLTypeInfo(kBIGINT, notnull);
575  break;
576  case kINT:
577  common_type = SQLTypeInfo(kBIGINT, notnull);
578  break;
579  case kFLOAT:
580  common_type = SQLTypeInfo(kFLOAT, notnull);
581  break;
582  case kDOUBLE:
583  common_type = SQLTypeInfo(kDOUBLE, notnull);
584  break;
585  case kNUMERIC:
586  case kDECIMAL:
587  common_type = SQLTypeInfo(kDECIMAL, 19, type2.get_scale(), notnull);
588  break;
589  default:
590  CHECK(false);
591  }
592  break;
593  case kFLOAT:
594  switch (type2.get_type()) {
595  case kTINYINT:
596  common_type = SQLTypeInfo(kFLOAT, notnull);
597  break;
598  case kSMALLINT:
599  common_type = SQLTypeInfo(kFLOAT, notnull);
600  break;
601  case kINT:
602  common_type = SQLTypeInfo(kFLOAT, notnull);
603  break;
604  case kBIGINT:
605  common_type = SQLTypeInfo(kFLOAT, notnull);
606  break;
607  case kDOUBLE:
608  common_type = SQLTypeInfo(kDOUBLE, notnull);
609  break;
610  case kNUMERIC:
611  case kDECIMAL:
612  common_type = SQLTypeInfo(kFLOAT, notnull);
613  break;
614  default:
615  CHECK(false);
616  }
617  break;
618  case kDOUBLE:
619  switch (type2.get_type()) {
620  case kTINYINT:
621  case kSMALLINT:
622  case kINT:
623  case kBIGINT:
624  case kFLOAT:
625  case kNUMERIC:
626  case kDECIMAL:
627  common_type = SQLTypeInfo(kDOUBLE, notnull);
628  break;
629  default:
630  CHECK(false);
631  }
632  break;
633  case kNUMERIC:
634  case kDECIMAL:
635  switch (type2.get_type()) {
636  case kTINYINT:
637  common_type =
639  std::max(3 + type1.get_scale(), type1.get_dimension()),
640  type1.get_scale(),
641  notnull);
642  break;
643  case kSMALLINT:
644  common_type =
646  std::max(5 + type1.get_scale(), type1.get_dimension()),
647  type1.get_scale(),
648  notnull);
649  break;
650  case kINT:
651  common_type = SQLTypeInfo(
652  kDECIMAL,
653  std::max(std::min(19, 10 + type1.get_scale()), type2.get_dimension()),
654  type1.get_scale(),
655  notnull);
656  break;
657  case kBIGINT:
658  common_type = SQLTypeInfo(kDECIMAL, 19, type1.get_scale(), notnull);
659  break;
660  case kFLOAT:
661  common_type = SQLTypeInfo(kFLOAT, notnull);
662  break;
663  case kDOUBLE:
664  common_type = SQLTypeInfo(kDOUBLE, notnull);
665  break;
666  case kNUMERIC:
667  case kDECIMAL: {
668  int common_scale = std::max(type1.get_scale(), type2.get_scale());
669  common_type = SQLTypeInfo(kDECIMAL,
670  std::max(type1.get_dimension() - type1.get_scale(),
671  type2.get_dimension() - type2.get_scale()) +
672  common_scale,
673  common_scale,
674  notnull);
675  break;
676  }
677  default:
678  CHECK(false);
679  }
680  break;
681  default:
682  CHECK(false);
683  }
684  common_type.set_fixed_size();
685  return common_type;
686 }
687 
688 std::shared_ptr<Analyzer::Expr> Expr::decompress() {
690  return shared_from_this();
691  }
692  SQLTypeInfo new_type_info(type_info.get_type(),
697  0,
699  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
700 }
701 
702 std::shared_ptr<Analyzer::Expr> Expr::add_cast(const SQLTypeInfo& new_type_info) {
703  if (new_type_info == type_info) {
704  return shared_from_this();
705  }
706  if (new_type_info.is_string() && type_info.is_string() &&
707  new_type_info.get_compression() == kENCODING_DICT &&
709  (new_type_info.get_comp_param() == type_info.get_comp_param() ||
710  new_type_info.get_comp_param() == TRANSIENT_DICT(type_info.get_comp_param()))) {
711  return shared_from_this();
712  }
713  if (!type_info.is_castable(new_type_info)) {
714  throw std::runtime_error("Cannot CAST from " + type_info.get_type_name() + " to " +
715  new_type_info.get_type_name());
716  }
717  // @TODO(wei) temporary restriction until executor can support this.
718  if (typeid(*this) != typeid(Constant) && new_type_info.is_string() &&
719  new_type_info.get_compression() == kENCODING_DICT &&
720  new_type_info.get_comp_param() <= TRANSIENT_DICT_ID) {
722  throw std::runtime_error(
723  "Cannot group by string columns which are not dictionary encoded.");
724  }
725  throw std::runtime_error(
726  "Internal error: Cannot apply transient dictionary encoding to non-literal "
727  "expression "
728  "yet.");
729  }
730  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
731 }
732 
733 namespace {
734 
735 // Return dec * 10^-scale
736 template <typename T>
737 T floatFromDecimal(int64_t const dec, unsigned const scale) {
738  static_assert(std::is_floating_point_v<T>);
739  return static_cast<T>(dec) / shared::power10(scale);
740 }
741 
742 // Q: Why is there a maxRound() but no minRound()?
743 // A: The numerical value of std::numeric_limits<int64_t>::min() is unchanged when cast
744 // to either float or double, but std::numeric_limits<intXX_t>::max() is incremented to
745 // 2^(XX-1) when cast to float/double for XX in {32,64}, which is an invalid intXX_t
746 // value. Thus the maximum float/double that can be cast to a valid integer type must be
747 // calculated directly, and not just compared to std::numeric_limits<intXX_t>::max().
748 template <typename FLOAT_TYPE, typename INT_TYPE>
749 constexpr FLOAT_TYPE maxRound() {
750  static_assert(std::is_integral_v<INT_TYPE> && std::is_floating_point_v<FLOAT_TYPE>);
751  constexpr int dd =
752  std::numeric_limits<INT_TYPE>::digits - std::numeric_limits<FLOAT_TYPE>::digits;
753  if constexpr (0 < dd) { // NOLINT
754  return static_cast<FLOAT_TYPE>(std::numeric_limits<INT_TYPE>::max() - (1ll << dd));
755  } else {
756  return static_cast<FLOAT_TYPE>(std::numeric_limits<INT_TYPE>::max());
757  }
758 }
759 
760 template <typename TO, typename FROM>
761 TO safeNarrow(FROM const from) {
762  static_assert(std::is_integral_v<TO> && std::is_integral_v<FROM>);
763  static_assert(sizeof(TO) < sizeof(FROM));
764  if (from < static_cast<FROM>(std::numeric_limits<TO>::min()) ||
765  static_cast<FROM>(std::numeric_limits<TO>::max()) < from) {
766  throw std::runtime_error("Overflow or underflow");
767  }
768  return static_cast<TO>(from);
769 }
770 
771 template <typename T>
772 T roundDecimal(int64_t n, unsigned scale) {
773  static_assert(std::is_integral_v<T>);
774  constexpr size_t max_scale = std::numeric_limits<uint64_t>::digits10; // 19
775  constexpr auto pow10 = shared::powersOf<uint64_t, max_scale + 1>(10);
776  if (scale == 0) {
777  if constexpr (sizeof(T) < sizeof(int64_t)) { // NOLINT
778  return safeNarrow<T>(n);
779  } else {
780  return n;
781  }
782  } else if (max_scale < scale) {
783  return 0; // 0.09223372036854775807 rounds to 0
784  }
785  uint64_t const u = std::abs(n);
786  uint64_t const pow = pow10[scale];
787  uint64_t div = u / pow;
788  uint64_t rem = u % pow;
789  div += pow / 2 <= rem;
790  if constexpr (sizeof(T) < sizeof(int64_t)) { // NOLINT
791  return safeNarrow<T>(static_cast<int64_t>(n < 0 ? -div : div));
792  } else {
793  return n < 0 ? -div : div;
794  }
795 }
796 
797 template <typename TO, typename FROM>
798 TO safeRound(FROM const from) {
799  static_assert(std::is_integral_v<TO> && std::is_floating_point_v<FROM>);
800  constexpr FROM max_float = maxRound<FROM, TO>();
801  FROM const n = std::round(from);
802  if (n < static_cast<FROM>(std::numeric_limits<TO>::min()) || max_float < n) {
803  throw std::runtime_error("Overflow or underflow");
804  }
805  return static_cast<TO>(n);
806 }
807 
808 // Return numeric/decimal representation of from with given scale.
809 template <typename T>
810 int64_t safeScale(T from, unsigned const scale) {
811  static_assert(std::is_arithmetic_v<T>);
812  constexpr size_t max_scale = std::numeric_limits<int64_t>::digits10; // 18
813  constexpr auto pow10 = shared::powersOf<int64_t, max_scale + 1>(10);
814  if constexpr (std::is_integral_v<T>) { // NOLINT
815  int64_t retval;
816  if (scale < pow10.size()) {
817 #ifdef __linux__
818  if (!__builtin_mul_overflow(from, pow10[scale], &retval)) {
819  return retval;
820  }
821  // Not over flow safe.
822 #else
823  return from * pow10[scale];
824 #endif
825  }
826  } else if constexpr (std::is_floating_point_v<T>) {
827  if (scale < pow10.size()) {
828  return safeRound<int64_t>(from * pow10[scale]);
829  }
830  }
831  if (from == 0) {
832  return 0;
833  }
834  throw std::runtime_error("Overflow or underflow");
835 }
836 
837 } // namespace
838 
839 void Constant::cast_number(const SQLTypeInfo& new_type_info) {
840  switch (type_info.get_type()) {
841  case kTINYINT:
842  switch (new_type_info.get_type()) {
843  case kTINYINT:
844  break;
845  case kINT:
846  constval.intval = (int32_t)constval.tinyintval;
847  break;
848  case kSMALLINT:
850  break;
851  case kBIGINT:
852  case kTIMESTAMP:
854  break;
855  case kDOUBLE:
857  break;
858  case kFLOAT:
860  break;
861  case kNUMERIC:
862  case kDECIMAL:
864  break;
865  default:
866  CHECK(false);
867  }
868  break;
869  case kINT:
870  switch (new_type_info.get_type()) {
871  case kTINYINT:
872  constval.tinyintval = safeNarrow<int8_t>(constval.intval);
873  break;
874  case kINT:
875  break;
876  case kSMALLINT:
877  constval.smallintval = safeNarrow<int16_t>(constval.intval);
878  break;
879  case kBIGINT:
880  case kTIMESTAMP:
881  constval.bigintval = (int64_t)constval.intval;
882  break;
883  case kDOUBLE:
884  constval.doubleval = (double)constval.intval;
885  break;
886  case kFLOAT:
887  constval.floatval = (float)constval.intval;
888  break;
889  case kNUMERIC:
890  case kDECIMAL:
891  constval.bigintval = safeScale(constval.intval, new_type_info.get_scale());
892  break;
893  default:
894  CHECK(false);
895  }
896  break;
897  case kSMALLINT:
898  switch (new_type_info.get_type()) {
899  case kTINYINT:
900  constval.tinyintval = safeNarrow<int8_t>(constval.smallintval);
901  break;
902  case kINT:
903  constval.intval = (int32_t)constval.smallintval;
904  break;
905  case kSMALLINT:
906  break;
907  case kBIGINT:
908  case kTIMESTAMP:
910  break;
911  case kDOUBLE:
913  break;
914  case kFLOAT:
916  break;
917  case kNUMERIC:
918  case kDECIMAL:
920  break;
921  default:
922  CHECK(false);
923  }
924  break;
925  case kBIGINT:
926  switch (new_type_info.get_type()) {
927  case kTINYINT:
928  constval.tinyintval = safeNarrow<int8_t>(constval.bigintval);
929  break;
930  case kINT:
931  constval.intval = safeNarrow<int32_t>(constval.bigintval);
932  break;
933  case kSMALLINT:
934  constval.smallintval = safeNarrow<int16_t>(constval.bigintval);
935  break;
936  case kBIGINT:
937  case kTIMESTAMP:
938  break;
939  case kDOUBLE:
941  break;
942  case kFLOAT:
944  break;
945  case kNUMERIC:
946  case kDECIMAL:
948  break;
949  default:
950  CHECK(false);
951  }
952  break;
953  case kDOUBLE:
954  switch (new_type_info.get_type()) {
955  case kTINYINT:
956  constval.tinyintval = safeRound<int8_t>(constval.doubleval);
957  break;
958  case kINT:
959  constval.intval = safeRound<int32_t>(constval.doubleval);
960  break;
961  case kSMALLINT:
962  constval.smallintval = safeRound<int16_t>(constval.doubleval);
963  break;
964  case kBIGINT:
965  case kTIMESTAMP:
966  constval.bigintval = safeRound<int64_t>(constval.doubleval);
967  break;
968  case kDOUBLE:
969  break;
970  case kFLOAT:
972  break;
973  case kNUMERIC:
974  case kDECIMAL:
976  break;
977  default:
978  CHECK(false);
979  }
980  break;
981  case kFLOAT:
982  switch (new_type_info.get_type()) {
983  case kTINYINT:
984  constval.tinyintval = safeRound<int8_t>(constval.floatval);
985  break;
986  case kINT:
987  constval.intval = safeRound<int32_t>(constval.floatval);
988  break;
989  case kSMALLINT:
990  constval.smallintval = safeRound<int16_t>(constval.floatval);
991  break;
992  case kBIGINT:
993  case kTIMESTAMP:
994  constval.bigintval = safeRound<int64_t>(constval.floatval);
995  break;
996  case kDOUBLE:
998  break;
999  case kFLOAT:
1000  break;
1001  case kNUMERIC:
1002  case kDECIMAL:
1003  constval.bigintval = safeScale(constval.floatval, new_type_info.get_scale());
1004  break;
1005  default:
1006  CHECK(false);
1007  }
1008  break;
1009  case kNUMERIC:
1010  case kDECIMAL:
1011  switch (new_type_info.get_type()) {
1012  case kTINYINT:
1014  roundDecimal<int8_t>(constval.bigintval, type_info.get_scale());
1015  break;
1016  case kINT:
1017  constval.intval =
1018  roundDecimal<int32_t>(constval.bigintval, type_info.get_scale());
1019  break;
1020  case kSMALLINT:
1022  roundDecimal<int16_t>(constval.bigintval, type_info.get_scale());
1023  break;
1024  case kBIGINT:
1025  case kTIMESTAMP:
1027  roundDecimal<int64_t>(constval.bigintval, type_info.get_scale());
1028  break;
1029  case kDOUBLE:
1031  floatFromDecimal<double>(constval.bigintval, type_info.get_scale());
1032  break;
1033  case kFLOAT:
1034  constval.floatval =
1035  floatFromDecimal<float>(constval.bigintval, type_info.get_scale());
1036  break;
1037  case kNUMERIC:
1038  case kDECIMAL:
1040  constval.bigintval, type_info, new_type_info);
1041  break;
1042  default:
1043  CHECK(false);
1044  }
1045  break;
1046  case kTIMESTAMP:
1047  switch (new_type_info.get_type()) {
1048  case kTINYINT:
1049  constval.tinyintval = safeNarrow<int8_t>(constval.bigintval);
1050  break;
1051  case kINT:
1052  constval.intval = safeNarrow<int32_t>(constval.bigintval);
1053  break;
1054  case kSMALLINT:
1055  constval.smallintval = safeNarrow<int16_t>(constval.bigintval);
1056  break;
1057  case kBIGINT:
1058  case kTIMESTAMP:
1059  break;
1060  case kDOUBLE:
1061  constval.doubleval = static_cast<double>(constval.bigintval);
1062  break;
1063  case kFLOAT:
1064  constval.floatval = static_cast<float>(constval.bigintval);
1065  break;
1066  case kNUMERIC:
1067  case kDECIMAL:
1068  for (int i = 0; i < new_type_info.get_scale(); i++) {
1069  constval.bigintval *= 10;
1070  }
1071  break;
1072  default:
1073  CHECK(false);
1074  }
1075  break;
1076  case kBOOLEAN:
1077  switch (new_type_info.get_type()) {
1078  case kTINYINT:
1079  constval.tinyintval = constval.boolval ? 1 : 0;
1080  break;
1081  case kINT:
1082  constval.intval = constval.boolval ? 1 : 0;
1083  break;
1084  case kSMALLINT:
1086  break;
1087  case kBIGINT:
1088  case kTIMESTAMP:
1089  constval.bigintval = constval.boolval ? 1 : 0;
1090  break;
1091  case kDOUBLE:
1092  constval.doubleval = constval.boolval ? 1 : 0;
1093  break;
1094  case kFLOAT:
1095  constval.floatval = constval.boolval ? 1 : 0;
1096  break;
1097  case kNUMERIC:
1098  case kDECIMAL:
1099  constval.bigintval = constval.boolval ? 1 : 0;
1100  for (int i = 0; i < new_type_info.get_scale(); i++) {
1101  constval.bigintval *= 10;
1102  }
1103  break;
1104  default:
1105  CHECK(false);
1106  }
1107  break;
1108  default:
1109  CHECK(false);
1110  }
1111  type_info = new_type_info;
1112 }
1113 
1114 void Constant::cast_string(const SQLTypeInfo& new_type_info) {
1115  std::string* s = constval.stringval;
1116  if (s != nullptr && new_type_info.get_type() != kTEXT &&
1117  static_cast<size_t>(new_type_info.get_dimension()) < s->length()) {
1118  // truncate string
1119  constval.stringval = new std::string(s->substr(0, new_type_info.get_dimension()));
1120  delete s;
1121  }
1122  type_info = new_type_info;
1123 }
1124 
1125 void Constant::cast_from_string(const SQLTypeInfo& new_type_info) {
1126  std::string* s = constval.stringval;
1127  SQLTypeInfo ti = new_type_info;
1128  constval = StringToDatum(*s, ti);
1129  delete s;
1130  type_info = new_type_info;
1131 }
1132 
1133 void Constant::cast_to_string(const SQLTypeInfo& str_type_info) {
1134  const auto str_val = DatumToString(constval, type_info);
1135  constval.stringval = new std::string(str_val);
1136  if (str_type_info.get_type() != kTEXT &&
1137  constval.stringval->length() > static_cast<size_t>(str_type_info.get_dimension())) {
1138  // truncate the string
1139  *constval.stringval = constval.stringval->substr(0, str_type_info.get_dimension());
1140  }
1141  type_info = str_type_info;
1142 }
1143 
1144 namespace {
1145 
1146 // TODO(adb): we should revisit this, as one could argue a Datum should never contain
1147 // a null sentinel. In fact, if we bundle Datum with a null boolean ("NullableDatum"),
1148 // the logic becomes more explicit. There are likely other bugs associated with the
1149 // current logic -- for example, boolean is set to -128 which is likely UB
1150 inline bool is_null_value(const SQLTypeInfo& ti, const Datum& constval) {
1151  switch (ti.get_type()) {
1152  case kBOOLEAN:
1153  return constval.tinyintval == NULL_BOOLEAN;
1154  case kTINYINT:
1155  return constval.tinyintval == NULL_TINYINT;
1156  case kINT:
1157  return constval.intval == NULL_INT;
1158  case kSMALLINT:
1159  return constval.smallintval == NULL_SMALLINT;
1160  case kBIGINT:
1161  case kNUMERIC:
1162  case kDECIMAL:
1163  return constval.bigintval == NULL_BIGINT;
1164  case kTIME:
1165  case kTIMESTAMP:
1166  case kDATE:
1167  return constval.bigintval == NULL_BIGINT;
1168  case kVARCHAR:
1169  case kCHAR:
1170  case kTEXT:
1171  return constval.stringval == nullptr;
1172  case kPOINT:
1173  case kLINESTRING:
1174  case kPOLYGON:
1175  case kMULTIPOLYGON:
1176  return constval.stringval == nullptr;
1177  case kFLOAT:
1178  return constval.floatval == NULL_FLOAT;
1179  case kDOUBLE:
1180  return constval.doubleval == NULL_DOUBLE;
1181  case kNULLT:
1182  return constval.bigintval == 0;
1183  case kARRAY:
1184  return constval.arrayval == nullptr;
1185  default:
1186  UNREACHABLE();
1187  }
1188  UNREACHABLE();
1189  return false;
1190 }
1191 
1192 } // namespace
1193 
1194 void Constant::do_cast(const SQLTypeInfo& new_type_info) {
1195  if (type_info == new_type_info) {
1196  return;
1197  }
1198  if (is_null && !new_type_info.get_notnull()) {
1199  type_info = new_type_info;
1200  set_null_value();
1201  return;
1202  }
1203  if ((new_type_info.is_number() || new_type_info.get_type() == kTIMESTAMP) &&
1204  (new_type_info.get_type() != kTIMESTAMP || type_info.get_type() != kTIMESTAMP) &&
1206  type_info.get_type() == kBOOLEAN)) {
1207  cast_number(new_type_info);
1208  } else if (new_type_info.is_geometry() && type_info.is_string()) {
1209  type_info = new_type_info;
1210  } else if (new_type_info.is_geometry() &&
1211  type_info.get_type() == new_type_info.get_type()) {
1212  type_info = new_type_info;
1213  } else if (new_type_info.is_boolean() && type_info.is_boolean()) {
1214  type_info = new_type_info;
1215  } else if (new_type_info.is_string() && type_info.is_string()) {
1216  cast_string(new_type_info);
1217  } else if (type_info.is_string() || type_info.get_type() == kVARCHAR) {
1218  cast_from_string(new_type_info);
1219  } else if (new_type_info.is_string()) {
1220  cast_to_string(new_type_info);
1221  } else if (new_type_info.get_type() == kDATE && type_info.get_type() == kDATE) {
1222  type_info = new_type_info;
1223  } else if (new_type_info.get_type() == kDATE && type_info.get_type() == kTIMESTAMP) {
1230  type_info = new_type_info;
1231  } else if ((type_info.get_type() == kTIMESTAMP || type_info.get_type() == kDATE) &&
1232  new_type_info.get_type() == kTIMESTAMP) {
1233  const auto dimen = (type_info.get_type() == kDATE) ? 0 : type_info.get_dimension();
1234  if (dimen != new_type_info.get_dimension()) {
1235  constval.bigintval = dimen < new_type_info.get_dimension()
1239  new_type_info.get_dimension() - dimen)
1243  dimen - new_type_info.get_dimension());
1244  }
1245  type_info = new_type_info;
1246  } else if (new_type_info.is_array() && type_info.is_array()) {
1247  auto new_sub_ti = new_type_info.get_elem_type();
1248  for (auto& v : value_list) {
1249  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(v);
1250  if (!c) {
1251  throw std::runtime_error("Invalid array cast.");
1252  }
1253  c->do_cast(new_sub_ti);
1254  }
1255  type_info = new_type_info;
1256  } else if (get_is_null() && (new_type_info.is_number() || new_type_info.is_time() ||
1257  new_type_info.is_string() || new_type_info.is_boolean())) {
1258  type_info = new_type_info;
1259  set_null_value();
1260  } else if (!is_null_value(type_info, constval) &&
1261  get_nullable_type_info(type_info) == new_type_info) {
1262  CHECK(!is_null);
1263  // relax nullability
1264  type_info = new_type_info;
1265  return;
1266  } else {
1267  throw std::runtime_error("Cast from " + type_info.get_type_name() + " to " +
1268  new_type_info.get_type_name() + " not supported");
1269  }
1270 }
1271 
1273  switch (type_info.get_type()) {
1274  case kBOOLEAN:
1276  break;
1277  case kTINYINT:
1279  break;
1280  case kINT:
1282  break;
1283  case kSMALLINT:
1285  break;
1286  case kBIGINT:
1287  case kNUMERIC:
1288  case kDECIMAL:
1290  break;
1291  case kTIME:
1292  case kTIMESTAMP:
1293  case kDATE:
1295  break;
1296  case kVARCHAR:
1297  case kCHAR:
1298  case kTEXT:
1299  constval.stringval = nullptr;
1300  break;
1301  case kPOINT:
1302  case kLINESTRING:
1303  case kPOLYGON:
1304  case kMULTIPOLYGON:
1305  constval.stringval = nullptr;
1306  break;
1307  case kFLOAT:
1309  break;
1310  case kDOUBLE:
1312  break;
1313  case kNULLT:
1314  constval.bigintval = 0;
1315  break;
1316  case kARRAY:
1317  constval.arrayval = nullptr;
1318  break;
1319  default:
1320  CHECK(false);
1321  }
1322 }
1323 
1324 std::shared_ptr<Analyzer::Expr> Constant::add_cast(const SQLTypeInfo& new_type_info) {
1325  if (is_null) {
1326  type_info = new_type_info;
1327  set_null_value();
1328  return shared_from_this();
1329  }
1330  if (new_type_info.get_compression() != type_info.get_compression()) {
1331  if (new_type_info.get_compression() != kENCODING_NONE) {
1332  SQLTypeInfo new_ti = new_type_info;
1333  if (new_ti.get_compression() != kENCODING_DATE_IN_DAYS) {
1335  }
1336  do_cast(new_ti);
1337  }
1338  return Expr::add_cast(new_type_info);
1339  }
1340  const auto is_integral_type =
1341  new_type_info.is_integer() || new_type_info.is_decimal() || new_type_info.is_fp();
1342  if (is_integral_type && (type_info.is_time() || type_info.is_date())) {
1343  // Let the codegen phase deal with casts from date/time to a number.
1344  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
1345  }
1346  do_cast(new_type_info);
1347  return shared_from_this();
1348 }
1349 
1350 std::shared_ptr<Analyzer::Expr> UOper::add_cast(const SQLTypeInfo& new_type_info) {
1351  if (optype != kCAST) {
1352  return Expr::add_cast(new_type_info);
1353  }
1354  if (type_info.is_string() && new_type_info.is_string() &&
1355  new_type_info.get_compression() == kENCODING_DICT &&
1357  const SQLTypeInfo oti = operand->get_type_info();
1358  if (oti.is_string() && oti.get_compression() == kENCODING_DICT &&
1359  (oti.get_comp_param() == new_type_info.get_comp_param() ||
1360  oti.get_comp_param() == TRANSIENT_DICT(new_type_info.get_comp_param()))) {
1361  auto result = operand;
1362  operand = nullptr;
1363  return result;
1364  }
1365  }
1366  return Expr::add_cast(new_type_info);
1367 }
1368 
1369 std::shared_ptr<Analyzer::Expr> CaseExpr::add_cast(const SQLTypeInfo& new_type_info) {
1370  SQLTypeInfo ti = new_type_info;
1371  if (new_type_info.is_string() && new_type_info.get_compression() == kENCODING_DICT &&
1372  new_type_info.get_comp_param() == TRANSIENT_DICT_ID && type_info.is_string() &&
1376  }
1377 
1378  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1379  new_expr_pair_list;
1380  for (auto& p : expr_pair_list) {
1381  new_expr_pair_list.emplace_back(
1382  std::make_pair(p.first, p.second->deep_copy()->add_cast(ti)));
1383  }
1384 
1385  if (else_expr != nullptr) {
1386  else_expr = else_expr->add_cast(ti);
1387  }
1388  // Replace the current WHEN THEN pair list once we are sure all casts have succeeded
1389  expr_pair_list = new_expr_pair_list;
1390 
1391  type_info = ti;
1392  return shared_from_this();
1393 }
1394 
1395 std::shared_ptr<Analyzer::Expr> Subquery::add_cast(const SQLTypeInfo& new_type_info) {
1396  // not supported yet.
1397  CHECK(false);
1398  return nullptr;
1399 }
1400 
1401 int Query::get_rte_idx(const std::string& name) const {
1402  int rte_idx = 0;
1403  for (auto rte : rangetable) {
1404  if (rte->get_rangevar() == name) {
1405  return rte_idx;
1406  }
1407  rte_idx++;
1408  }
1409  return -1;
1410 }
1411 
1413  rangetable.push_back(rte);
1414 }
1415 
1417  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1418  if (!groupby.empty()) {
1419  for (auto e : groupby) {
1420  auto c = std::dynamic_pointer_cast<ColumnVar>(e);
1421  if (c && table_id == c->get_table_id() && column_id == c->get_column_id()) {
1422  return;
1423  }
1424  }
1425  }
1426  throw std::runtime_error(
1427  "expressions in the SELECT or HAVING clause must be an aggregate function or an "
1428  "expression "
1429  "over GROUP BY columns.");
1430 }
1431 
1433  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1434  if (which_row != kGROUPBY) {
1435  throw std::runtime_error("Internal error: invalid VAR in GROUP BY or HAVING.");
1436  }
1437 }
1438 
1440  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1441  operand->check_group_by(groupby);
1442 }
1443 
1445  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1446  left_operand->check_group_by(groupby);
1447  right_operand->check_group_by(groupby);
1448 }
1449 
1450 namespace {
1451 
1452 template <class T>
1453 bool expr_is(const std::shared_ptr<Analyzer::Expr>& expr) {
1454  return std::dynamic_pointer_cast<T>(expr) != nullptr;
1455 }
1456 
1457 } // namespace
1458 
1460  const std::shared_ptr<Analyzer::Expr> cast_operand,
1461  const std::shared_ptr<Analyzer::Expr> const_operand) {
1462  if (expr_is<UOper>(cast_operand) && expr_is<Constant>(const_operand)) {
1463  auto u_expr = std::dynamic_pointer_cast<UOper>(cast_operand);
1464  if (u_expr->get_optype() != kCAST) {
1465  return false;
1466  }
1467  if (!(expr_is<Analyzer::ColumnVar>(u_expr->get_own_operand()) &&
1468  !expr_is<Analyzer::Var>(u_expr->get_own_operand()))) {
1469  return false;
1470  }
1471  const auto& ti = u_expr->get_type_info();
1472  if (ti.is_time() && u_expr->get_operand()->get_type_info().is_time()) {
1473  // Allow casts between time types to pass through
1474  return true;
1475  } else if (ti.is_integer() && u_expr->get_operand()->get_type_info().is_integer()) {
1476  // Allow casts between integer types to pass through
1477  return true;
1478  }
1479  }
1480  return false;
1481 }
1482 
1483 std::shared_ptr<Analyzer::Expr> BinOper::normalize_simple_predicate(int& rte_idx) const {
1484  rte_idx = -1;
1485  if (!IS_COMPARISON(optype) || qualifier != kONE) {
1486  return nullptr;
1487  }
1488  if (expr_is<UOper>(left_operand)) {
1490  auto uo = std::dynamic_pointer_cast<UOper>(left_operand);
1491  auto cv = std::dynamic_pointer_cast<ColumnVar>(uo->get_own_operand());
1492  rte_idx = cv->get_rte_idx();
1493  return this->deep_copy();
1494  }
1495  } else if (expr_is<UOper>(right_operand)) {
1497  auto uo = std::dynamic_pointer_cast<UOper>(right_operand);
1498  auto cv = std::dynamic_pointer_cast<ColumnVar>(uo->get_own_operand());
1499  rte_idx = cv->get_rte_idx();
1500  return makeExpr<BinOper>(type_info,
1501  contains_agg,
1503  qualifier,
1504  right_operand->deep_copy(),
1505  left_operand->deep_copy());
1506  }
1507  } else if (expr_is<ColumnVar>(left_operand) && !expr_is<Var>(left_operand) &&
1508  expr_is<Constant>(right_operand)) {
1509  auto cv = std::dynamic_pointer_cast<ColumnVar>(left_operand);
1510  rte_idx = cv->get_rte_idx();
1511  return this->deep_copy();
1512  } else if (expr_is<Constant>(left_operand) && expr_is<ColumnVar>(right_operand) &&
1513  !expr_is<Var>(right_operand)) {
1514  auto cv = std::dynamic_pointer_cast<ColumnVar>(right_operand);
1515  rte_idx = cv->get_rte_idx();
1516  return makeExpr<BinOper>(type_info,
1517  contains_agg,
1519  qualifier,
1520  right_operand->deep_copy(),
1521  left_operand->deep_copy());
1522  }
1523  return nullptr;
1524 }
1525 
1526 void ColumnVar::group_predicates(std::list<const Expr*>& scan_predicates,
1527  std::list<const Expr*>& join_predicates,
1528  std::list<const Expr*>& const_predicates) const {
1529  if (type_info.get_type() == kBOOLEAN) {
1530  scan_predicates.push_back(this);
1531  }
1532 }
1533 
1534 void UOper::group_predicates(std::list<const Expr*>& scan_predicates,
1535  std::list<const Expr*>& join_predicates,
1536  std::list<const Expr*>& const_predicates) const {
1537  std::set<int> rte_idx_set;
1538  operand->collect_rte_idx(rte_idx_set);
1539  if (rte_idx_set.size() > 1) {
1540  join_predicates.push_back(this);
1541  } else if (rte_idx_set.size() == 1) {
1542  scan_predicates.push_back(this);
1543  } else {
1544  const_predicates.push_back(this);
1545  }
1546 }
1547 
1548 void BinOper::group_predicates(std::list<const Expr*>& scan_predicates,
1549  std::list<const Expr*>& join_predicates,
1550  std::list<const Expr*>& const_predicates) const {
1551  if (optype == kAND) {
1552  left_operand->group_predicates(scan_predicates, join_predicates, const_predicates);
1553  right_operand->group_predicates(scan_predicates, join_predicates, const_predicates);
1554  return;
1555  }
1556  std::set<int> rte_idx_set;
1557  left_operand->collect_rte_idx(rte_idx_set);
1558  right_operand->collect_rte_idx(rte_idx_set);
1559  if (rte_idx_set.size() > 1) {
1560  join_predicates.push_back(this);
1561  } else if (rte_idx_set.size() == 1) {
1562  scan_predicates.push_back(this);
1563  } else {
1564  const_predicates.push_back(this);
1565  }
1566 }
1567 
1568 namespace {
1569 
1571  const auto const_expr = dynamic_cast<const Analyzer::Constant*>(expr);
1572  if (const_expr) {
1573  return const_expr->get_is_null();
1574  }
1575  const auto& expr_ti = expr->get_type_info();
1576  return !expr_ti.get_notnull();
1577 }
1578 
1579 bool is_in_values_nullable(const std::shared_ptr<Analyzer::Expr>& a,
1580  const std::list<std::shared_ptr<Analyzer::Expr>>& l) {
1581  if (is_expr_nullable(a.get())) {
1582  return true;
1583  }
1584  for (const auto& v : l) {
1585  if (is_expr_nullable(v.get())) {
1586  return true;
1587  }
1588  }
1589  return false;
1590 }
1591 
1592 } // namespace
1593 
1594 InValues::InValues(std::shared_ptr<Analyzer::Expr> a,
1595  const std::list<std::shared_ptr<Analyzer::Expr>>& l)
1596  : Expr(kBOOLEAN, !is_in_values_nullable(a, l)), arg(a), value_list(l) {}
1597 
1598 void InValues::group_predicates(std::list<const Expr*>& scan_predicates,
1599  std::list<const Expr*>& join_predicates,
1600  std::list<const Expr*>& const_predicates) const {
1601  std::set<int> rte_idx_set;
1602  arg->collect_rte_idx(rte_idx_set);
1603  if (rte_idx_set.size() > 1) {
1604  join_predicates.push_back(this);
1605  } else if (rte_idx_set.size() == 1) {
1606  scan_predicates.push_back(this);
1607  } else {
1608  const_predicates.push_back(this);
1609  }
1610 }
1611 
1612 InIntegerSet::InIntegerSet(const std::shared_ptr<const Analyzer::Expr> a,
1613  const std::vector<int64_t>& l,
1614  const bool not_null)
1615  : Expr(kBOOLEAN, not_null), arg(a), value_list(l) {}
1616 
1617 void CharLengthExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1618  std::list<const Expr*>& join_predicates,
1619  std::list<const Expr*>& const_predicates) const {
1620  std::set<int> rte_idx_set;
1621  arg->collect_rte_idx(rte_idx_set);
1622  if (rte_idx_set.size() > 1) {
1623  join_predicates.push_back(this);
1624  } else if (rte_idx_set.size() == 1) {
1625  scan_predicates.push_back(this);
1626  } else {
1627  const_predicates.push_back(this);
1628  }
1629 }
1630 
1631 void KeyForStringExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1632  std::list<const Expr*>& join_predicates,
1633  std::list<const Expr*>& const_predicates) const {
1634  std::set<int> rte_idx_set;
1635  arg->collect_rte_idx(rte_idx_set);
1636  if (rte_idx_set.size() > 1) {
1637  join_predicates.push_back(this);
1638  } else if (rte_idx_set.size() == 1) {
1639  scan_predicates.push_back(this);
1640  } else {
1641  const_predicates.push_back(this);
1642  }
1643 }
1644 
1645 void SampleRatioExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1646  std::list<const Expr*>& join_predicates,
1647  std::list<const Expr*>& const_predicates) const {
1648  std::set<int> rte_idx_set;
1649  arg->collect_rte_idx(rte_idx_set);
1650  if (rte_idx_set.size() > 1) {
1651  join_predicates.push_back(this);
1652  } else if (rte_idx_set.size() == 1) {
1653  scan_predicates.push_back(this);
1654  } else {
1655  const_predicates.push_back(this);
1656  }
1657 }
1658 
1659 void LowerExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1660  std::list<const Expr*>& join_predicates,
1661  std::list<const Expr*>& const_predicates) const {
1662  std::set<int> rte_idx_set;
1663  arg->collect_rte_idx(rte_idx_set);
1664  if (rte_idx_set.size() > 1) {
1665  join_predicates.push_back(this);
1666  } else if (rte_idx_set.size() == 1) {
1667  scan_predicates.push_back(this);
1668  } else {
1669  const_predicates.push_back(this);
1670  }
1671 }
1672 
1673 void CardinalityExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1674  std::list<const Expr*>& join_predicates,
1675  std::list<const Expr*>& const_predicates) const {
1676  std::set<int> rte_idx_set;
1677  arg->collect_rte_idx(rte_idx_set);
1678  if (rte_idx_set.size() > 1) {
1679  join_predicates.push_back(this);
1680  } else if (rte_idx_set.size() == 1) {
1681  scan_predicates.push_back(this);
1682  } else {
1683  const_predicates.push_back(this);
1684  }
1685 }
1686 
1687 void LikeExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1688  std::list<const Expr*>& join_predicates,
1689  std::list<const Expr*>& const_predicates) const {
1690  std::set<int> rte_idx_set;
1691  arg->collect_rte_idx(rte_idx_set);
1692  if (rte_idx_set.size() > 1) {
1693  join_predicates.push_back(this);
1694  } else if (rte_idx_set.size() == 1) {
1695  scan_predicates.push_back(this);
1696  } else {
1697  const_predicates.push_back(this);
1698  }
1699 }
1700 
1701 void RegexpExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1702  std::list<const Expr*>& join_predicates,
1703  std::list<const Expr*>& const_predicates) const {
1704  std::set<int> rte_idx_set;
1705  arg->collect_rte_idx(rte_idx_set);
1706  if (rte_idx_set.size() > 1) {
1707  join_predicates.push_back(this);
1708  } else if (rte_idx_set.size() == 1) {
1709  scan_predicates.push_back(this);
1710  } else {
1711  const_predicates.push_back(this);
1712  }
1713 }
1714 
1715 void WidthBucketExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1716  std::list<const Expr*>& join_predicates,
1717  std::list<const Expr*>& const_predicates) const {
1718  std::set<int> rte_idx_set;
1719  target_value_->collect_rte_idx(rte_idx_set);
1720  if (rte_idx_set.size() > 1) {
1721  join_predicates.push_back(this);
1722  } else if (rte_idx_set.size() == 1) {
1723  scan_predicates.push_back(this);
1724  } else {
1725  const_predicates.push_back(this);
1726  }
1727 }
1728 
1729 void LikelihoodExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1730  std::list<const Expr*>& join_predicates,
1731  std::list<const Expr*>& const_predicates) const {
1732  std::set<int> rte_idx_set;
1733  arg->collect_rte_idx(rte_idx_set);
1734  if (rte_idx_set.size() > 1) {
1735  join_predicates.push_back(this);
1736  } else if (rte_idx_set.size() == 1) {
1737  scan_predicates.push_back(this);
1738  } else {
1739  const_predicates.push_back(this);
1740  }
1741 }
1742 
1743 void AggExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1744  std::list<const Expr*>& join_predicates,
1745  std::list<const Expr*>& const_predicates) const {
1746  std::set<int> rte_idx_set;
1747  arg->collect_rte_idx(rte_idx_set);
1748  if (rte_idx_set.size() > 1) {
1749  join_predicates.push_back(this);
1750  } else if (rte_idx_set.size() == 1) {
1751  scan_predicates.push_back(this);
1752  } else {
1753  const_predicates.push_back(this);
1754  }
1755 }
1756 
1757 void CaseExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1758  std::list<const Expr*>& join_predicates,
1759  std::list<const Expr*>& const_predicates) const {
1760  std::set<int> rte_idx_set;
1761  for (auto p : expr_pair_list) {
1762  p.first->collect_rte_idx(rte_idx_set);
1763  p.second->collect_rte_idx(rte_idx_set);
1764  }
1765  if (else_expr != nullptr) {
1766  else_expr->collect_rte_idx(rte_idx_set);
1767  }
1768  if (rte_idx_set.size() > 1) {
1769  join_predicates.push_back(this);
1770  } else if (rte_idx_set.size() == 1) {
1771  scan_predicates.push_back(this);
1772  } else {
1773  const_predicates.push_back(this);
1774  }
1775 }
1776 
1777 void ExtractExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1778  std::list<const Expr*>& join_predicates,
1779  std::list<const Expr*>& const_predicates) const {
1780  std::set<int> rte_idx_set;
1781  from_expr_->collect_rte_idx(rte_idx_set);
1782  if (rte_idx_set.size() > 1) {
1783  join_predicates.push_back(this);
1784  } else if (rte_idx_set.size() == 1) {
1785  scan_predicates.push_back(this);
1786  } else {
1787  const_predicates.push_back(this);
1788  }
1789 }
1790 
1791 void DateaddExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1792  std::list<const Expr*>& join_predicates,
1793  std::list<const Expr*>& const_predicates) const {
1794  std::set<int> rte_idx_set;
1795  number_->collect_rte_idx(rte_idx_set);
1796  datetime_->collect_rte_idx(rte_idx_set);
1797  if (rte_idx_set.size() > 1) {
1798  join_predicates.push_back(this);
1799  } else if (rte_idx_set.size() == 1) {
1800  scan_predicates.push_back(this);
1801  } else {
1802  const_predicates.push_back(this);
1803  }
1804 }
1805 
1806 void DatediffExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1807  std::list<const Expr*>& join_predicates,
1808  std::list<const Expr*>& const_predicates) const {
1809  std::set<int> rte_idx_set;
1810  start_->collect_rte_idx(rte_idx_set);
1811  end_->collect_rte_idx(rte_idx_set);
1812  if (rte_idx_set.size() > 1) {
1813  join_predicates.push_back(this);
1814  } else if (rte_idx_set.size() == 1) {
1815  scan_predicates.push_back(this);
1816  } else {
1817  const_predicates.push_back(this);
1818  }
1819 }
1820 
1821 void DatetruncExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1822  std::list<const Expr*>& join_predicates,
1823  std::list<const Expr*>& const_predicates) const {
1824  std::set<int> rte_idx_set;
1825  from_expr_->collect_rte_idx(rte_idx_set);
1826  if (rte_idx_set.size() > 1) {
1827  join_predicates.push_back(this);
1828  } else if (rte_idx_set.size() == 1) {
1829  scan_predicates.push_back(this);
1830  } else {
1831  const_predicates.push_back(this);
1832  }
1833 }
1834 
1835 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_with_targetlist(
1836  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1837  for (auto tle : tlist) {
1838  const Expr* e = tle->get_expr();
1839  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
1840  if (colvar != nullptr) {
1841  if (table_id == colvar->get_table_id() && column_id == colvar->get_column_id()) {
1842  return colvar->deep_copy();
1843  }
1844  }
1845  }
1846  throw std::runtime_error("Internal error: cannot find ColumnVar in targetlist.");
1847 }
1848 
1849 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_with_child_targetlist(
1850  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1851  int varno = 1;
1852  for (auto tle : tlist) {
1853  const Expr* e = tle->get_expr();
1854  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
1855  if (colvar == nullptr) {
1856  throw std::runtime_error(
1857  "Internal Error: targetlist in rewrite_with_child_targetlist is not all "
1858  "columns.");
1859  }
1860  if (table_id == colvar->get_table_id() && column_id == colvar->get_column_id()) {
1861  return makeExpr<Var>(colvar->get_type_info(),
1862  colvar->get_table_id(),
1863  colvar->get_column_id(),
1864  colvar->get_rte_idx(),
1866  varno);
1867  }
1868  varno++;
1869  }
1870  throw std::runtime_error("Internal error: cannot find ColumnVar in child targetlist.");
1871 }
1872 
1873 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_agg_to_var(
1874  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1875  int varno = 1;
1876  for (auto tle : tlist) {
1877  const Expr* e = tle->get_expr();
1878  if (typeid(*e) != typeid(AggExpr)) {
1879  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
1880  if (colvar == nullptr) {
1881  throw std::runtime_error(
1882  "Internal Error: targetlist in rewrite_agg_to_var is not all columns and "
1883  "aggregates.");
1884  }
1885  if (table_id == colvar->get_table_id() && column_id == colvar->get_column_id()) {
1886  return makeExpr<Var>(colvar->get_type_info(),
1887  colvar->get_table_id(),
1888  colvar->get_column_id(),
1889  colvar->get_rte_idx(),
1891  varno);
1892  }
1893  }
1894  varno++;
1895  }
1896  throw std::runtime_error(
1897  "Internal error: cannot find ColumnVar from having clause in targetlist.");
1898 }
1899 
1900 std::shared_ptr<Analyzer::Expr> Var::rewrite_agg_to_var(
1901  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1902  int varno = 1;
1903  for (auto tle : tlist) {
1904  const Expr* e = tle->get_expr();
1905  if (*e == *this) {
1906  return makeExpr<Var>(e->get_type_info(), Var::kINPUT_OUTER, varno);
1907  }
1908  varno++;
1909  }
1910  throw std::runtime_error(
1911  "Internal error: cannot find Var from having clause in targetlist.");
1912 }
1913 
1914 std::shared_ptr<Analyzer::Expr> InValues::rewrite_with_targetlist(
1915  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1916  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
1917  for (auto v : value_list) {
1918  new_value_list.push_back(v->deep_copy());
1919  }
1920  return makeExpr<InValues>(arg->rewrite_with_targetlist(tlist), new_value_list);
1921 }
1922 
1923 std::shared_ptr<Analyzer::Expr> InValues::rewrite_with_child_targetlist(
1924  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1925  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
1926  for (auto v : value_list) {
1927  new_value_list.push_back(v->deep_copy());
1928  }
1929  return makeExpr<InValues>(arg->rewrite_with_child_targetlist(tlist), new_value_list);
1930 }
1931 
1932 std::shared_ptr<Analyzer::Expr> InValues::rewrite_agg_to_var(
1933  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1934  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
1935  for (auto v : value_list) {
1936  new_value_list.push_back(v->rewrite_agg_to_var(tlist));
1937  }
1938  return makeExpr<InValues>(arg->rewrite_agg_to_var(tlist), new_value_list);
1939 }
1940 
1941 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_with_targetlist(
1942  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1943  for (auto tle : tlist) {
1944  const Expr* e = tle->get_expr();
1945  if (typeid(*e) == typeid(AggExpr)) {
1946  const AggExpr* agg = dynamic_cast<const AggExpr*>(e);
1947  if (*this == *agg) {
1948  return agg->deep_copy();
1949  }
1950  }
1951  }
1952  throw std::runtime_error("Internal error: cannot find AggExpr in targetlist.");
1953 }
1954 
1955 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_with_child_targetlist(
1956  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1957  return makeExpr<AggExpr>(type_info,
1958  aggtype,
1959  arg ? arg->rewrite_with_child_targetlist(tlist) : nullptr,
1960  is_distinct,
1961  arg1);
1962 }
1963 
1964 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_agg_to_var(
1965  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1966  int varno = 1;
1967  for (auto tle : tlist) {
1968  const Expr* e = tle->get_expr();
1969  if (typeid(*e) == typeid(AggExpr)) {
1970  const AggExpr* agg_expr = dynamic_cast<const AggExpr*>(e);
1971  if (*this == *agg_expr) {
1972  return makeExpr<Var>(agg_expr->get_type_info(), Var::kINPUT_OUTER, varno);
1973  }
1974  }
1975  varno++;
1976  }
1977  throw std::runtime_error(
1978  "Internal error: cannot find AggExpr from having clause in targetlist.");
1979 }
1980 
1981 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_with_targetlist(
1982  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1983  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1984  epair_list;
1985  for (auto p : expr_pair_list) {
1986  epair_list.emplace_back(p.first->rewrite_with_targetlist(tlist),
1987  p.second->rewrite_with_targetlist(tlist));
1988  }
1989  return makeExpr<CaseExpr>(
1990  type_info,
1991  contains_agg,
1992  epair_list,
1993  else_expr ? else_expr->rewrite_with_targetlist(tlist) : nullptr);
1994 }
1995 
1996 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_with_targetlist(
1997  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1998  return makeExpr<ExtractExpr>(
1999  type_info, contains_agg, field_, from_expr_->rewrite_with_targetlist(tlist));
2000 }
2001 
2002 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_with_targetlist(
2003  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2004  return makeExpr<DateaddExpr>(type_info,
2005  field_,
2006  number_->rewrite_with_targetlist(tlist),
2007  datetime_->rewrite_with_targetlist(tlist));
2008 }
2009 
2010 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_with_targetlist(
2011  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2012  return makeExpr<DatediffExpr>(type_info,
2013  field_,
2014  start_->rewrite_with_targetlist(tlist),
2015  end_->rewrite_with_targetlist(tlist));
2016 }
2017 
2018 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_with_targetlist(
2019  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2020  return makeExpr<DatetruncExpr>(
2021  type_info, contains_agg, field_, from_expr_->rewrite_with_targetlist(tlist));
2022 }
2023 
2024 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_with_child_targetlist(
2025  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2026  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
2027  epair_list;
2028  for (auto p : expr_pair_list) {
2029  epair_list.emplace_back(p.first->rewrite_with_child_targetlist(tlist),
2030  p.second->rewrite_with_child_targetlist(tlist));
2031  }
2032  return makeExpr<CaseExpr>(
2033  type_info,
2034  contains_agg,
2035  epair_list,
2036  else_expr ? else_expr->rewrite_with_child_targetlist(tlist) : nullptr);
2037 }
2038 
2039 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_with_child_targetlist(
2040  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2041  return makeExpr<ExtractExpr>(
2042  type_info, contains_agg, field_, from_expr_->rewrite_with_child_targetlist(tlist));
2043 }
2044 
2045 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_with_child_targetlist(
2046  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2047  return makeExpr<DateaddExpr>(type_info,
2048  field_,
2049  number_->rewrite_with_child_targetlist(tlist),
2050  datetime_->rewrite_with_child_targetlist(tlist));
2051 }
2052 
2053 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_with_child_targetlist(
2054  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2055  return makeExpr<DatediffExpr>(type_info,
2056  field_,
2057  start_->rewrite_with_child_targetlist(tlist),
2058  end_->rewrite_with_child_targetlist(tlist));
2059 }
2060 
2061 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_with_child_targetlist(
2062  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2063  return makeExpr<DatetruncExpr>(
2064  type_info, contains_agg, field_, from_expr_->rewrite_with_child_targetlist(tlist));
2065 }
2066 
2067 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_agg_to_var(
2068  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2069  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
2070  epair_list;
2071  for (auto p : expr_pair_list) {
2072  epair_list.emplace_back(p.first->rewrite_agg_to_var(tlist),
2073  p.second->rewrite_agg_to_var(tlist));
2074  }
2075  return makeExpr<CaseExpr>(type_info,
2076  contains_agg,
2077  epair_list,
2078  else_expr ? else_expr->rewrite_agg_to_var(tlist) : nullptr);
2079 }
2080 
2081 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_agg_to_var(
2082  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2083  return makeExpr<ExtractExpr>(
2084  type_info, contains_agg, field_, from_expr_->rewrite_agg_to_var(tlist));
2085 }
2086 
2087 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_agg_to_var(
2088  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2089  return makeExpr<DateaddExpr>(type_info,
2090  field_,
2091  number_->rewrite_agg_to_var(tlist),
2092  datetime_->rewrite_agg_to_var(tlist));
2093 }
2094 
2095 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_agg_to_var(
2096  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2097  return makeExpr<DatediffExpr>(type_info,
2098  field_,
2099  start_->rewrite_agg_to_var(tlist),
2100  end_->rewrite_agg_to_var(tlist));
2101 }
2102 
2103 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_agg_to_var(
2104  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
2105  return makeExpr<DatetruncExpr>(
2106  type_info, contains_agg, field_, from_expr_->rewrite_agg_to_var(tlist));
2107 }
2108 
2109 bool ColumnVar::operator==(const Expr& rhs) const {
2110  if (typeid(rhs) != typeid(ColumnVar) && typeid(rhs) != typeid(Var)) {
2111  return false;
2112  }
2113  const ColumnVar& rhs_cv = dynamic_cast<const ColumnVar&>(rhs);
2114  if (rte_idx != -1) {
2115  return (table_id == rhs_cv.get_table_id()) && (column_id == rhs_cv.get_column_id()) &&
2116  (rte_idx == rhs_cv.get_rte_idx());
2117  }
2118  const Var* v = dynamic_cast<const Var*>(this);
2119  if (v == nullptr) {
2120  return false;
2121  }
2122  const Var* rv = dynamic_cast<const Var*>(&rhs);
2123  if (rv == nullptr) {
2124  return false;
2125  }
2126  return (v->get_which_row() == rv->get_which_row()) &&
2127  (v->get_varno() == rv->get_varno());
2128 }
2129 
2130 bool ExpressionTuple::operator==(const Expr& rhs) const {
2131  const auto rhs_tuple = dynamic_cast<const ExpressionTuple*>(&rhs);
2132  if (!rhs_tuple) {
2133  return false;
2134  }
2135  const auto& rhs_tuple_cols = rhs_tuple->getTuple();
2136  return expr_list_match(tuple_, rhs_tuple_cols);
2137 }
2138 
2139 bool Datum_equal(const SQLTypeInfo& ti, Datum val1, Datum val2) {
2140  switch (ti.get_type()) {
2141  case kBOOLEAN:
2142  return val1.boolval == val2.boolval;
2143  case kCHAR:
2144  case kVARCHAR:
2145  case kTEXT:
2146  return *val1.stringval == *val2.stringval;
2147  case kNUMERIC:
2148  case kDECIMAL:
2149  case kBIGINT:
2150  return val1.bigintval == val2.bigintval;
2151  case kINT:
2152  return val1.intval == val2.intval;
2153  case kSMALLINT:
2154  return val1.smallintval == val2.smallintval;
2155  case kTINYINT:
2156  return val1.tinyintval == val2.tinyintval;
2157  case kFLOAT:
2158  return val1.floatval == val2.floatval;
2159  case kDOUBLE:
2160  return val1.doubleval == val2.doubleval;
2161  case kTIME:
2162  case kTIMESTAMP:
2163  case kDATE:
2164  case kINTERVAL_DAY_TIME:
2165  case kINTERVAL_YEAR_MONTH:
2166  return val1.bigintval == val2.bigintval;
2167  case kPOINT:
2168  case kLINESTRING:
2169  case kPOLYGON:
2170  case kMULTIPOLYGON:
2171  return *val1.stringval == *val2.stringval;
2172  default:
2173  throw std::runtime_error("Unrecognized type for Constant Datum equality: " +
2174  ti.get_type_name());
2175  }
2176  UNREACHABLE();
2177  return false;
2178 }
2179 
2180 bool Constant::operator==(const Expr& rhs) const {
2181  if (typeid(rhs) != typeid(Constant)) {
2182  return false;
2183  }
2184  const Constant& rhs_c = dynamic_cast<const Constant&>(rhs);
2185  if (type_info != rhs_c.get_type_info() || is_null != rhs_c.get_is_null()) {
2186  return false;
2187  }
2188  if (is_null && rhs_c.get_is_null()) {
2189  return true;
2190  }
2191  if (type_info.is_array()) {
2192  return false;
2193  }
2194  return Datum_equal(type_info, constval, rhs_c.get_constval());
2195 }
2196 
2197 bool UOper::operator==(const Expr& rhs) const {
2198  if (typeid(rhs) != typeid(UOper)) {
2199  return false;
2200  }
2201  const UOper& rhs_uo = dynamic_cast<const UOper&>(rhs);
2202  return optype == rhs_uo.get_optype() && *operand == *rhs_uo.get_operand();
2203 }
2204 
2205 bool BinOper::operator==(const Expr& rhs) const {
2206  if (typeid(rhs) != typeid(BinOper)) {
2207  return false;
2208  }
2209  const BinOper& rhs_bo = dynamic_cast<const BinOper&>(rhs);
2210  return optype == rhs_bo.get_optype() && *left_operand == *rhs_bo.get_left_operand() &&
2211  *right_operand == *rhs_bo.get_right_operand();
2212 }
2213 
2214 bool RangeOper::operator==(const Expr& rhs) const {
2215  if (typeid(rhs) != typeid(RangeOper)) {
2216  return false;
2217  }
2218  const RangeOper& rhs_rg = dynamic_cast<const RangeOper&>(rhs);
2219  return left_inclusive_ == rhs_rg.left_inclusive_ &&
2220  right_inclusive_ == rhs_rg.right_inclusive_ &&
2221  *left_operand_ == *rhs_rg.left_operand_ &&
2222  *right_operand_ == *rhs_rg.right_operand_;
2223 }
2224 
2225 bool CharLengthExpr::operator==(const Expr& rhs) const {
2226  if (typeid(rhs) != typeid(CharLengthExpr)) {
2227  return false;
2228  }
2229  const CharLengthExpr& rhs_cl = dynamic_cast<const CharLengthExpr&>(rhs);
2230  if (!(*arg == *rhs_cl.get_arg()) ||
2232  return false;
2233  }
2234  return true;
2235 }
2236 
2237 bool KeyForStringExpr::operator==(const Expr& rhs) const {
2238  if (typeid(rhs) != typeid(KeyForStringExpr)) {
2239  return false;
2240  }
2241  const KeyForStringExpr& rhs_cl = dynamic_cast<const KeyForStringExpr&>(rhs);
2242  if (!(*arg == *rhs_cl.get_arg())) {
2243  return false;
2244  }
2245  return true;
2246 }
2247 
2248 bool SampleRatioExpr::operator==(const Expr& rhs) const {
2249  if (typeid(rhs) != typeid(SampleRatioExpr)) {
2250  return false;
2251  }
2252  const SampleRatioExpr& rhs_cl = dynamic_cast<const SampleRatioExpr&>(rhs);
2253  if (!(*arg == *rhs_cl.get_arg())) {
2254  return false;
2255  }
2256  return true;
2257 }
2258 
2259 bool LowerExpr::operator==(const Expr& rhs) const {
2260  if (typeid(rhs) != typeid(LowerExpr)) {
2261  return false;
2262  }
2263 
2264  return *arg == *dynamic_cast<const LowerExpr&>(rhs).get_arg();
2265 }
2266 
2267 bool CardinalityExpr::operator==(const Expr& rhs) const {
2268  if (typeid(rhs) != typeid(CardinalityExpr)) {
2269  return false;
2270  }
2271  const CardinalityExpr& rhs_ca = dynamic_cast<const CardinalityExpr&>(rhs);
2272  if (!(*arg == *rhs_ca.get_arg())) {
2273  return false;
2274  }
2275  return true;
2276 }
2277 
2278 bool LikeExpr::operator==(const Expr& rhs) const {
2279  if (typeid(rhs) != typeid(LikeExpr)) {
2280  return false;
2281  }
2282  const LikeExpr& rhs_lk = dynamic_cast<const LikeExpr&>(rhs);
2283  if (!(*arg == *rhs_lk.get_arg()) || !(*like_expr == *rhs_lk.get_like_expr()) ||
2284  is_ilike != rhs_lk.get_is_ilike()) {
2285  return false;
2286  }
2287  if (escape_expr.get() == rhs_lk.get_escape_expr()) {
2288  return true;
2289  }
2290  if (escape_expr != nullptr && rhs_lk.get_escape_expr() != nullptr &&
2291  *escape_expr == *rhs_lk.get_escape_expr()) {
2292  return true;
2293  }
2294  return false;
2295 }
2296 
2297 bool RegexpExpr::operator==(const Expr& rhs) const {
2298  if (typeid(rhs) != typeid(RegexpExpr)) {
2299  return false;
2300  }
2301  const RegexpExpr& rhs_re = dynamic_cast<const RegexpExpr&>(rhs);
2302  if (!(*arg == *rhs_re.get_arg()) || !(*pattern_expr == *rhs_re.get_pattern_expr())) {
2303  return false;
2304  }
2305  if (escape_expr.get() == rhs_re.get_escape_expr()) {
2306  return true;
2307  }
2308  if (escape_expr != nullptr && rhs_re.get_escape_expr() != nullptr &&
2309  *escape_expr == *rhs_re.get_escape_expr()) {
2310  return true;
2311  }
2312  return false;
2313 }
2314 
2315 bool WidthBucketExpr::operator==(const Expr& rhs) const {
2316  if (typeid(rhs) != typeid(WidthBucketExpr)) {
2317  return false;
2318  }
2319  const WidthBucketExpr& rhs_l = dynamic_cast<const WidthBucketExpr&>(rhs);
2320  if (!(*target_value_ == *rhs_l.get_target_value())) {
2321  return false;
2322  }
2323  if (!(*lower_bound_ == *rhs_l.get_lower_bound())) {
2324  return false;
2325  }
2326  if (!(*upper_bound_ == *rhs_l.get_upper_bound())) {
2327  return false;
2328  }
2329  if (!(*partition_count_ == *rhs_l.get_partition_count())) {
2330  return false;
2331  }
2332  return true;
2333 }
2334 
2335 bool LikelihoodExpr::operator==(const Expr& rhs) const {
2336  if (typeid(rhs) != typeid(LikelihoodExpr)) {
2337  return false;
2338  }
2339  const LikelihoodExpr& rhs_l = dynamic_cast<const LikelihoodExpr&>(rhs);
2340  if (!(*arg == *rhs_l.get_arg())) {
2341  return false;
2342  }
2343  if (likelihood != rhs_l.get_likelihood()) {
2344  return false;
2345  }
2346  return true;
2347 }
2348 
2349 bool InValues::operator==(const Expr& rhs) const {
2350  if (typeid(rhs) != typeid(InValues)) {
2351  return false;
2352  }
2353  const InValues& rhs_iv = dynamic_cast<const InValues&>(rhs);
2354  if (!(*arg == *rhs_iv.get_arg())) {
2355  return false;
2356  }
2357  if (value_list.size() != rhs_iv.get_value_list().size()) {
2358  return false;
2359  }
2360  auto q = rhs_iv.get_value_list().begin();
2361  for (auto p : value_list) {
2362  if (!(*p == **q)) {
2363  return false;
2364  }
2365  q++;
2366  }
2367  return true;
2368 }
2369 
2370 bool AggExpr::operator==(const Expr& rhs) const {
2371  if (typeid(rhs) != typeid(AggExpr)) {
2372  return false;
2373  }
2374  const AggExpr& rhs_ae = dynamic_cast<const AggExpr&>(rhs);
2375  if (aggtype != rhs_ae.get_aggtype() || is_distinct != rhs_ae.get_is_distinct()) {
2376  return false;
2377  }
2378  if (arg.get() == rhs_ae.get_arg()) {
2379  return true;
2380  }
2381  if (arg == nullptr || rhs_ae.get_arg() == nullptr) {
2382  return false;
2383  }
2384  return *arg == *rhs_ae.get_arg();
2385 }
2386 
2387 bool CaseExpr::operator==(const Expr& rhs) const {
2388  if (typeid(rhs) != typeid(CaseExpr)) {
2389  return false;
2390  }
2391  const CaseExpr& rhs_ce = dynamic_cast<const CaseExpr&>(rhs);
2392  if (expr_pair_list.size() != rhs_ce.get_expr_pair_list().size()) {
2393  return false;
2394  }
2395  if ((else_expr == nullptr && rhs_ce.get_else_expr() != nullptr) ||
2396  (else_expr != nullptr && rhs_ce.get_else_expr() == nullptr)) {
2397  return false;
2398  }
2399  auto it = rhs_ce.get_expr_pair_list().cbegin();
2400  for (auto p : expr_pair_list) {
2401  if (!(*p.first == *it->first) || !(*p.second == *it->second)) {
2402  return false;
2403  }
2404  ++it;
2405  }
2406  return else_expr == nullptr ||
2407  (else_expr != nullptr && *else_expr == *rhs_ce.get_else_expr());
2408 }
2409 
2410 bool ExtractExpr::operator==(const Expr& rhs) const {
2411  if (typeid(rhs) != typeid(ExtractExpr)) {
2412  return false;
2413  }
2414  const ExtractExpr& rhs_ee = dynamic_cast<const ExtractExpr&>(rhs);
2415  return field_ == rhs_ee.get_field() && *from_expr_ == *rhs_ee.get_from_expr();
2416 }
2417 
2418 bool DateaddExpr::operator==(const Expr& rhs) const {
2419  if (typeid(rhs) != typeid(DateaddExpr)) {
2420  return false;
2421  }
2422  const DateaddExpr& rhs_ee = dynamic_cast<const DateaddExpr&>(rhs);
2423  return field_ == rhs_ee.get_field() && *number_ == *rhs_ee.get_number_expr() &&
2424  *datetime_ == *rhs_ee.get_datetime_expr();
2425 }
2426 
2427 bool DatediffExpr::operator==(const Expr& rhs) const {
2428  if (typeid(rhs) != typeid(DatediffExpr)) {
2429  return false;
2430  }
2431  const DatediffExpr& rhs_ee = dynamic_cast<const DatediffExpr&>(rhs);
2432  return field_ == rhs_ee.get_field() && *start_ == *rhs_ee.get_start_expr() &&
2433  *end_ == *rhs_ee.get_end_expr();
2434 }
2435 
2436 bool DatetruncExpr::operator==(const Expr& rhs) const {
2437  if (typeid(rhs) != typeid(DatetruncExpr)) {
2438  return false;
2439  }
2440  const DatetruncExpr& rhs_ee = dynamic_cast<const DatetruncExpr&>(rhs);
2441  return field_ == rhs_ee.get_field() && *from_expr_ == *rhs_ee.get_from_expr();
2442 }
2443 
2444 bool OffsetInFragment::operator==(const Expr& rhs) const {
2445  return typeid(rhs) == typeid(OffsetInFragment);
2446 }
2447 
2448 bool WindowFunction::operator==(const Expr& rhs) const {
2449  const auto rhs_window = dynamic_cast<const WindowFunction*>(&rhs);
2450  if (!rhs_window) {
2451  return false;
2452  }
2453  if (kind_ != rhs_window->kind_ || args_.size() != rhs_window->args_.size() ||
2454  partition_keys_.size() != rhs_window->partition_keys_.size() ||
2455  order_keys_.size() != rhs_window->order_keys_.size()) {
2456  return false;
2457  }
2458  return expr_list_match(args_, rhs_window->args_) &&
2459  expr_list_match(partition_keys_, rhs_window->partition_keys_) &&
2460  expr_list_match(order_keys_, rhs_window->order_keys_);
2461 }
2462 
2463 bool ArrayExpr::operator==(Expr const& rhs) const {
2464  if (typeid(rhs) != typeid(ArrayExpr)) {
2465  return false;
2466  }
2467  ArrayExpr const& casted_rhs = static_cast<ArrayExpr const&>(rhs);
2468  for (unsigned i = 0; i < contained_expressions_.size(); i++) {
2469  auto& lhs_expr = contained_expressions_[i];
2470  auto& rhs_expr = casted_rhs.contained_expressions_[i];
2471  if (!(lhs_expr == rhs_expr)) {
2472  return false;
2473  }
2474  }
2475  if (isNull() != casted_rhs.isNull()) {
2476  return false;
2477  }
2478 
2479  return true;
2480  ;
2481 }
2482 
2483 bool GeoUOper::operator==(const Expr& rhs) const {
2484  const auto rhs_geo = dynamic_cast<const GeoUOper*>(&rhs);
2485  if (!rhs_geo) {
2486  return false;
2487  }
2488  if (op_ != rhs_geo->getOp() || ti0_ != rhs_geo->getTypeInfo0() ||
2489  args0_.size() != rhs_geo->getArgs0().size()) {
2490  return false;
2491  }
2492  return expr_list_match(args0_, rhs_geo->getArgs0());
2493 }
2494 
2495 bool GeoBinOper::operator==(const Expr& rhs) const {
2496  const auto rhs_geo = dynamic_cast<const GeoBinOper*>(&rhs);
2497  if (!rhs_geo) {
2498  return false;
2499  }
2500  if (op_ != rhs_geo->getOp() || ti0_ != rhs_geo->getTypeInfo0() ||
2501  args0_.size() != rhs_geo->getArgs0().size()) {
2502  return false;
2503  }
2504  if (ti1_ != rhs_geo->getTypeInfo1() || args1_.size() != rhs_geo->getArgs1().size()) {
2505  return false;
2506  }
2507  return expr_list_match(args0_, rhs_geo->getArgs0()) ||
2508  expr_list_match(args1_, rhs_geo->getArgs1());
2509 }
2510 
2511 std::string ColumnVar::toString() const {
2512  return "(ColumnVar table: " + std::to_string(table_id) +
2513  " column: " + std::to_string(column_id) + " rte: " + std::to_string(rte_idx) +
2514  " " + get_type_info().get_type_name() + ") ";
2515 }
2516 
2517 std::string ExpressionTuple::toString() const {
2518  std::string str{"< "};
2519  for (const auto& column : tuple_) {
2520  str += column->toString();
2521  }
2522  str += "> ";
2523  return str;
2524 }
2525 
2526 std::string Var::toString() const {
2527  return "(Var table: " + std::to_string(table_id) +
2528  " column: " + std::to_string(column_id) + " rte: " + std::to_string(rte_idx) +
2529  " which_row: " + std::to_string(which_row) + " varno: " + std::to_string(varno) +
2530  ") ";
2531 }
2532 
2533 std::string Constant::toString() const {
2534  std::string str{"(Const "};
2535  if (is_null) {
2536  str += "NULL";
2537  } else if (type_info.is_array()) {
2538  const auto& elem_ti = type_info.get_elem_type();
2539  str += ::toString(type_info.get_type()) + ": " + ::toString(elem_ti.get_type());
2540  } else {
2541  str += DatumToString(constval, type_info);
2542  }
2543  str += ") ";
2544  return str;
2545 }
2546 
2547 std::string UOper::toString() const {
2548  std::string op;
2549  switch (optype) {
2550  case kNOT:
2551  op = "NOT ";
2552  break;
2553  case kUMINUS:
2554  op = "- ";
2555  break;
2556  case kISNULL:
2557  op = "IS NULL ";
2558  break;
2559  case kEXISTS:
2560  op = "EXISTS ";
2561  break;
2562  case kCAST:
2563  op = "CAST " + type_info.get_type_name() + "(" +
2565  std::to_string(type_info.get_scale()) + ") " +
2568  break;
2569  case kUNNEST:
2570  op = "UNNEST ";
2571  break;
2572  default:
2573  break;
2574  }
2575  return "(" + op + operand->toString() + ") ";
2576 }
2577 
2578 std::string BinOper::toString() const {
2579  std::string op;
2580  switch (optype) {
2581  case kEQ:
2582  op = "= ";
2583  break;
2584  case kNE:
2585  op = "<> ";
2586  break;
2587  case kLT:
2588  op = "< ";
2589  break;
2590  case kLE:
2591  op = "<= ";
2592  break;
2593  case kGT:
2594  op = "> ";
2595  break;
2596  case kGE:
2597  op = ">= ";
2598  break;
2599  case kAND:
2600  op = "AND ";
2601  break;
2602  case kOR:
2603  op = "OR ";
2604  break;
2605  case kMINUS:
2606  op = "- ";
2607  break;
2608  case kPLUS:
2609  op = "+ ";
2610  break;
2611  case kMULTIPLY:
2612  op = "* ";
2613  break;
2614  case kDIVIDE:
2615  op = "/ ";
2616  break;
2617  case kMODULO:
2618  op = "% ";
2619  break;
2620  case kARRAY_AT:
2621  op = "[] ";
2622  break;
2623  case kOVERLAPS:
2624  op = "OVERLAPS ";
2625  break;
2626  default:
2627  break;
2628  }
2629  std::string str{"("};
2630  str += op;
2631  if (qualifier == kANY) {
2632  str += "ANY ";
2633  } else if (qualifier == kALL) {
2634  str += "ALL ";
2635  }
2636  str += left_operand->toString();
2637  str += right_operand->toString();
2638  str += ") ";
2639  return str;
2640 }
2641 
2642 std::string RangeOper::toString() const {
2643  const std::string lhs = left_inclusive_ ? "[" : "(";
2644  const std::string rhs = right_inclusive_ ? "]" : ")";
2645  return "(RangeOper " + lhs + " " + left_operand_->toString() + " , " +
2646  right_operand_->toString() + " " + rhs + " )";
2647 }
2648 
2649 std::string Subquery::toString() const {
2650  return "(Subquery ) ";
2651 }
2652 
2653 std::string InValues::toString() const {
2654  std::string str{"(IN "};
2655  str += arg->toString();
2656  str += "(";
2657  int cnt = 0;
2658  bool shorted_value_list_str = false;
2659  for (auto e : value_list) {
2660  str += e->toString();
2661  cnt++;
2662  if (cnt > 4) {
2663  shorted_value_list_str = true;
2664  break;
2665  }
2666  }
2667  if (shorted_value_list_str) {
2668  str += "... | ";
2669  str += "Total # values: ";
2670  str += std::to_string(value_list.size());
2671  }
2672  str += ") ";
2673  return str;
2674 }
2675 
2676 std::shared_ptr<Analyzer::Expr> InIntegerSet::deep_copy() const {
2677  return std::make_shared<InIntegerSet>(arg, value_list, get_type_info().get_notnull());
2678 }
2679 
2680 bool InIntegerSet::operator==(const Expr& rhs) const {
2681  if (!dynamic_cast<const InIntegerSet*>(&rhs)) {
2682  return false;
2683  }
2684  const auto& rhs_in_integer_set = static_cast<const InIntegerSet&>(rhs);
2685  return *arg == *rhs_in_integer_set.arg && value_list == rhs_in_integer_set.value_list;
2686 }
2687 
2688 std::string InIntegerSet::toString() const {
2689  std::string str{"(IN_INTEGER_SET "};
2690  str += arg->toString();
2691  str += "( ";
2692  int cnt = 0;
2693  bool shorted_value_list_str = false;
2694  for (const auto e : value_list) {
2695  str += std::to_string(e) + " ";
2696  cnt++;
2697  if (cnt > 4) {
2698  shorted_value_list_str = true;
2699  break;
2700  }
2701  }
2702  if (shorted_value_list_str) {
2703  str += "... | ";
2704  str += "Total # values: ";
2705  str += std::to_string(value_list.size());
2706  }
2707  str += ") ";
2708  return str;
2709 }
2710 
2711 std::string CharLengthExpr::toString() const {
2712  std::string str;
2713  if (calc_encoded_length) {
2714  str += "CHAR_LENGTH(";
2715  } else {
2716  str += "LENGTH(";
2717  }
2718  str += arg->toString();
2719  str += ") ";
2720  return str;
2721 }
2722 
2723 std::string KeyForStringExpr::toString() const {
2724  std::string str{"KEY_FOR_STRING("};
2725  str += arg->toString();
2726  str += ") ";
2727  return str;
2728 }
2729 
2730 std::string SampleRatioExpr::toString() const {
2731  std::string str{"SAMPLE_RATIO("};
2732  str += arg->toString();
2733  str += ") ";
2734  return str;
2735 }
2736 
2737 std::string LowerExpr::toString() const {
2738  return "LOWER(" + arg->toString() + ") ";
2739 }
2740 
2741 std::string CardinalityExpr::toString() const {
2742  std::string str{"CARDINALITY("};
2743  str += arg->toString();
2744  str += ") ";
2745  return str;
2746 }
2747 
2748 std::string LikeExpr::toString() const {
2749  std::string str{"(LIKE "};
2750  str += arg->toString();
2751  str += like_expr->toString();
2752  if (escape_expr) {
2753  str += escape_expr->toString();
2754  }
2755  str += ") ";
2756  return str;
2757 }
2758 
2759 std::string RegexpExpr::toString() const {
2760  std::string str{"(REGEXP "};
2761  str += arg->toString();
2762  str += pattern_expr->toString();
2763  if (escape_expr) {
2764  str += escape_expr->toString();
2765  }
2766  str += ") ";
2767  return str;
2768 }
2769 
2770 std::string WidthBucketExpr::toString() const {
2771  std::string str{"(WIDTH_BUCKET "};
2772  str += target_value_->toString();
2773  str += lower_bound_->toString();
2774  str += upper_bound_->toString();
2775  str += partition_count_->toString();
2776  return str + ") ";
2777 }
2778 
2779 std::string LikelihoodExpr::toString() const {
2780  std::string str{"(LIKELIHOOD "};
2781  str += arg->toString();
2782  return str + " " + std::to_string(likelihood) + ") ";
2783 }
2784 
2785 std::string AggExpr::toString() const {
2786  std::string agg;
2787  switch (aggtype) {
2788  case kAVG:
2789  agg = "AVG ";
2790  break;
2791  case kMIN:
2792  agg = "MIN ";
2793  break;
2794  case kMAX:
2795  agg = "MAX ";
2796  break;
2797  case kSUM:
2798  agg = "SUM ";
2799  break;
2800  case kCOUNT:
2801  agg = "COUNT ";
2802  break;
2804  agg = "APPROX_COUNT_DISTINCT";
2805  break;
2806  case kAPPROX_QUANTILE:
2807  agg = "APPROX_PERCENTILE";
2808  break;
2809  case kSINGLE_VALUE:
2810  agg = "SINGLE_VALUE";
2811  break;
2812  case kSAMPLE:
2813  agg = "SAMPLE";
2814  break;
2815  }
2816  std::string str{"(" + agg};
2817  if (is_distinct) {
2818  str += "DISTINCT ";
2819  }
2820  if (arg) {
2821  str += arg->toString();
2822  } else {
2823  str += "*";
2824  }
2825  return str + ") ";
2826 }
2827 
2828 std::string CaseExpr::toString() const {
2829  std::string str{"CASE "};
2830  for (auto p : expr_pair_list) {
2831  str += "(";
2832  str += p.first->toString();
2833  str += ", ";
2834  str += p.second->toString();
2835  str += ") ";
2836  }
2837  if (else_expr) {
2838  str += "ELSE ";
2839  str += else_expr->toString();
2840  }
2841  str += " END ";
2842  return str;
2843 }
2844 
2845 std::string ExtractExpr::toString() const {
2846  return "EXTRACT(" + std::to_string(field_) + " FROM " + from_expr_->toString() + ") ";
2847 }
2848 
2849 std::string DateaddExpr::toString() const {
2850  return "DATEADD(" + std::to_string(field_) + " NUMBER " + number_->toString() +
2851  " DATETIME " + datetime_->toString() + ") ";
2852 }
2853 
2854 std::string DatediffExpr::toString() const {
2855  return "DATEDIFF(" + std::to_string(field_) + " START " + start_->toString() + " END " +
2856  end_->toString() + ") ";
2857 }
2858 
2859 std::string DatetruncExpr::toString() const {
2860  return "DATE_TRUNC(" + std::to_string(field_) + " , " + from_expr_->toString() + ") ";
2861 }
2862 
2863 std::string OffsetInFragment::toString() const {
2864  return "(OffsetInFragment) ";
2865 }
2866 
2867 std::string WindowFunction::toString() const {
2868  std::string result = "WindowFunction(" + ::toString(kind_);
2869  for (const auto& arg : args_) {
2870  result += " " + arg->toString();
2871  }
2872  return result + ") ";
2873 }
2874 
2875 std::string ArrayExpr::toString() const {
2876  std::string str{"ARRAY["};
2877 
2878  auto iter(contained_expressions_.begin());
2879  while (iter != contained_expressions_.end()) {
2880  str += (*iter)->toString();
2881  if (iter + 1 != contained_expressions_.end()) {
2882  str += ", ";
2883  }
2884  iter++;
2885  }
2886  str += "]";
2887  return str;
2888 }
2889 
2890 std::string GeoUOper::toString() const {
2891  std::string fn;
2892  switch (op_) {
2894  fn = "Geo";
2895  break;
2897  fn = "ST_IsEmpty";
2898  break;
2900  fn = "ST_IsValid";
2901  break;
2902  default:
2903  fn = "Geo_UNKNOWN";
2904  break;
2905  }
2906  std::string result = fn + "(";
2907  for (const auto& arg : args0_) {
2908  result += " " + arg->toString();
2909  }
2910  return result + " ) ";
2911 }
2912 
2913 std::string GeoBinOper::toString() const {
2914  std::string fn;
2915  switch (op_) {
2917  fn = "ST_Intersection";
2918  break;
2920  fn = "ST_Difference";
2921  break;
2923  fn = "ST_Union";
2924  break;
2926  fn = "ST_Buffer";
2927  break;
2928  default:
2929  fn = "Geo_UNKNOWN";
2930  break;
2931  }
2932  std::string result = fn + "(";
2933  // TODO: generate wkt
2934  for (const auto& arg : args0_) {
2935  result += " " + arg->toString();
2936  }
2937  for (const auto& arg : args1_) {
2938  result += " " + arg->toString();
2939  }
2940  return result + " ) ";
2941 }
2942 
2943 std::string TargetEntry::toString() const {
2944  std::string str{"(" + resname + " "};
2945  str += expr->toString();
2946  if (unnest) {
2947  str += " UNNEST";
2948  }
2949  str += ") ";
2950  return str;
2951 }
2952 
2953 std::string OrderEntry::toString() const {
2954  std::string str{std::to_string(tle_no)};
2955  if (is_desc) {
2956  str += " desc";
2957  }
2958  if (nulls_first) {
2959  str += " nulls first";
2960  }
2961  str += " ";
2962  return str;
2963 }
2964 
2965 void Expr::add_unique(std::list<const Expr*>& expr_list) const {
2966  // only add unique instances to the list
2967  for (auto e : expr_list) {
2968  if (*e == *this) {
2969  return;
2970  }
2971  }
2972  expr_list.push_back(this);
2973 }
2974 
2975 void BinOper::find_expr(bool (*f)(const Expr*), std::list<const Expr*>& expr_list) const {
2976  if (f(this)) {
2977  add_unique(expr_list);
2978  return;
2979  }
2980  left_operand->find_expr(f, expr_list);
2981  right_operand->find_expr(f, expr_list);
2982 }
2983 
2984 void UOper::find_expr(bool (*f)(const Expr*), std::list<const Expr*>& expr_list) const {
2985  if (f(this)) {
2986  add_unique(expr_list);
2987  return;
2988  }
2989  operand->find_expr(f, expr_list);
2990 }
2991 
2992 void InValues::find_expr(bool (*f)(const Expr*),
2993  std::list<const Expr*>& expr_list) const {
2994  if (f(this)) {
2995  add_unique(expr_list);
2996  return;
2997  }
2998  arg->find_expr(f, expr_list);
2999  for (auto e : value_list) {
3000  e->find_expr(f, expr_list);
3001  }
3002 }
3003 
3004 void CharLengthExpr::find_expr(bool (*f)(const Expr*),
3005  std::list<const Expr*>& expr_list) const {
3006  if (f(this)) {
3007  add_unique(expr_list);
3008  return;
3009  }
3010  arg->find_expr(f, expr_list);
3011 }
3012 
3013 void KeyForStringExpr::find_expr(bool (*f)(const Expr*),
3014  std::list<const Expr*>& expr_list) const {
3015  if (f(this)) {
3016  add_unique(expr_list);
3017  return;
3018  }
3019  arg->find_expr(f, expr_list);
3020 }
3021 
3022 void SampleRatioExpr::find_expr(bool (*f)(const Expr*),
3023  std::list<const Expr*>& expr_list) const {
3024  if (f(this)) {
3025  add_unique(expr_list);
3026  return;
3027  }
3028  arg->find_expr(f, expr_list);
3029 }
3030 
3031 void LowerExpr::find_expr(bool (*f)(const Expr*),
3032  std::list<const Expr*>& expr_list) const {
3033  if (f(this)) {
3034  add_unique(expr_list);
3035  } else {
3036  arg->find_expr(f, expr_list);
3037  }
3038 }
3039 
3040 void CardinalityExpr::find_expr(bool (*f)(const Expr*),
3041  std::list<const Expr*>& expr_list) const {
3042  if (f(this)) {
3043  add_unique(expr_list);
3044  return;
3045  }
3046  arg->find_expr(f, expr_list);
3047 }
3048 
3049 void LikeExpr::find_expr(bool (*f)(const Expr*),
3050  std::list<const Expr*>& expr_list) const {
3051  if (f(this)) {
3052  add_unique(expr_list);
3053  return;
3054  }
3055  arg->find_expr(f, expr_list);
3056  like_expr->find_expr(f, expr_list);
3057  if (escape_expr != nullptr) {
3058  escape_expr->find_expr(f, expr_list);
3059  }
3060 }
3061 
3062 void RegexpExpr::find_expr(bool (*f)(const Expr*),
3063  std::list<const Expr*>& expr_list) const {
3064  if (f(this)) {
3065  add_unique(expr_list);
3066  return;
3067  }
3068  arg->find_expr(f, expr_list);
3069  pattern_expr->find_expr(f, expr_list);
3070  if (escape_expr != nullptr) {
3071  escape_expr->find_expr(f, expr_list);
3072  }
3073 }
3074 
3075 void WidthBucketExpr::find_expr(bool (*f)(const Expr*),
3076  std::list<const Expr*>& expr_list) const {
3077  if (f(this)) {
3078  add_unique(expr_list);
3079  return;
3080  }
3081  target_value_->find_expr(f, expr_list);
3082  lower_bound_->find_expr(f, expr_list);
3083  upper_bound_->find_expr(f, expr_list);
3084  partition_count_->find_expr(f, expr_list);
3085 }
3086 
3087 void LikelihoodExpr::find_expr(bool (*f)(const Expr*),
3088  std::list<const Expr*>& expr_list) const {
3089  if (f(this)) {
3090  add_unique(expr_list);
3091  return;
3092  }
3093  arg->find_expr(f, expr_list);
3094 }
3095 
3096 void AggExpr::find_expr(bool (*f)(const Expr*), std::list<const Expr*>& expr_list) const {
3097  if (f(this)) {
3098  add_unique(expr_list);
3099  return;
3100  }
3101  if (arg != nullptr) {
3102  arg->find_expr(f, expr_list);
3103  }
3104 }
3105 
3106 void CaseExpr::find_expr(bool (*f)(const Expr*),
3107  std::list<const Expr*>& expr_list) const {
3108  if (f(this)) {
3109  add_unique(expr_list);
3110  return;
3111  }
3112  for (auto p : expr_pair_list) {
3113  p.first->find_expr(f, expr_list);
3114  p.second->find_expr(f, expr_list);
3115  }
3116  if (else_expr != nullptr) {
3117  else_expr->find_expr(f, expr_list);
3118  }
3119 }
3120 
3121 void ExtractExpr::find_expr(bool (*f)(const Expr*),
3122  std::list<const Expr*>& expr_list) const {
3123  if (f(this)) {
3124  add_unique(expr_list);
3125  return;
3126  }
3127  from_expr_->find_expr(f, expr_list);
3128 }
3129 
3130 void DateaddExpr::find_expr(bool (*f)(const Expr*),
3131  std::list<const Expr*>& expr_list) const {
3132  if (f(this)) {
3133  add_unique(expr_list);
3134  return;
3135  }
3136  number_->find_expr(f, expr_list);
3137  datetime_->find_expr(f, expr_list);
3138 }
3139 
3140 void DatediffExpr::find_expr(bool (*f)(const Expr*),
3141  std::list<const Expr*>& expr_list) const {
3142  if (f(this)) {
3143  add_unique(expr_list);
3144  return;
3145  }
3146  start_->find_expr(f, expr_list);
3147  end_->find_expr(f, expr_list);
3148 }
3149 
3150 void DatetruncExpr::find_expr(bool (*f)(const Expr*),
3151  std::list<const Expr*>& expr_list) const {
3152  if (f(this)) {
3153  add_unique(expr_list);
3154  return;
3155  }
3156  from_expr_->find_expr(f, expr_list);
3157 }
3158 
3159 void CaseExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3160  for (auto p : expr_pair_list) {
3161  p.first->collect_rte_idx(rte_idx_set);
3162  p.second->collect_rte_idx(rte_idx_set);
3163  }
3164  if (else_expr != nullptr) {
3165  else_expr->collect_rte_idx(rte_idx_set);
3166  }
3167 }
3168 
3169 void ExtractExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3170  from_expr_->collect_rte_idx(rte_idx_set);
3171 }
3172 
3173 void DateaddExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3174  number_->collect_rte_idx(rte_idx_set);
3175  datetime_->collect_rte_idx(rte_idx_set);
3176 }
3177 
3178 void DatediffExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3179  start_->collect_rte_idx(rte_idx_set);
3180  end_->collect_rte_idx(rte_idx_set);
3181 }
3182 
3183 void DatetruncExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3184  from_expr_->collect_rte_idx(rte_idx_set);
3185 }
3186 
3187 void ArrayExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
3188  for (unsigned i = 0; i < getElementCount(); i++) {
3189  const auto expr = getElement(i);
3190  expr->collect_rte_idx(rte_idx_set);
3191  }
3192 }
3193 
3194 void FunctionOper::collect_rte_idx(std::set<int>& rte_idx_set) const {
3195  for (unsigned i = 0; i < getArity(); i++) {
3196  const auto expr = getArg(i);
3197  expr->collect_rte_idx(rte_idx_set);
3198  }
3199 }
3200 
3202  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3203  bool include_agg) const {
3204  for (auto p : expr_pair_list) {
3205  p.first->collect_column_var(colvar_set, include_agg);
3206  p.second->collect_column_var(colvar_set, include_agg);
3207  }
3208  if (else_expr != nullptr) {
3209  else_expr->collect_column_var(colvar_set, include_agg);
3210  }
3211 }
3212 
3214  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3215  bool include_agg) const {
3216  from_expr_->collect_column_var(colvar_set, include_agg);
3217 }
3218 
3220  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3221  bool include_agg) const {
3222  number_->collect_column_var(colvar_set, include_agg);
3223  datetime_->collect_column_var(colvar_set, include_agg);
3224 }
3225 
3227  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3228  bool include_agg) const {
3229  start_->collect_column_var(colvar_set, include_agg);
3230  end_->collect_column_var(colvar_set, include_agg);
3231 }
3232 
3234  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3235  bool include_agg) const {
3236  from_expr_->collect_column_var(colvar_set, include_agg);
3237 }
3238 
3240  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3241  bool include_agg) const {
3242  for (unsigned i = 0; i < getElementCount(); i++) {
3243  const auto expr = getElement(i);
3244  expr->collect_column_var(colvar_set, include_agg);
3245  }
3246 }
3247 
3249  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3250  bool include_agg) const {
3251  for (unsigned i = 0; i < getArity(); i++) {
3252  const auto expr = getArg(i);
3253  expr->collect_column_var(colvar_set, include_agg);
3254  }
3255 }
3256 
3258  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3259  for (auto p : expr_pair_list) {
3260  p.first->check_group_by(groupby);
3261  p.second->check_group_by(groupby);
3262  }
3263  if (else_expr != nullptr) {
3264  else_expr->check_group_by(groupby);
3265  }
3266 }
3267 
3269  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3270  from_expr_->check_group_by(groupby);
3271 }
3272 
3274  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3275  number_->check_group_by(groupby);
3276  datetime_->check_group_by(groupby);
3277 }
3278 
3280  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3281  start_->check_group_by(groupby);
3282  end_->check_group_by(groupby);
3283 }
3284 
3286  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
3287  from_expr_->check_group_by(groupby);
3288 }
3289 
3290 void CaseExpr::get_domain(DomainSet& domain_set) const {
3291  for (const auto& p : expr_pair_list) {
3292  const auto c = std::dynamic_pointer_cast<const Constant>(p.second);
3293  if (c != nullptr) {
3294  c->add_unique(domain_set);
3295  } else {
3296  const auto v = std::dynamic_pointer_cast<const ColumnVar>(p.second);
3297  if (v != nullptr) {
3298  v->add_unique(domain_set);
3299  } else {
3300  const auto cast = std::dynamic_pointer_cast<const UOper>(p.second);
3301  if (cast != nullptr && cast->get_optype() == kCAST) {
3302  const Constant* c = dynamic_cast<const Constant*>(cast->get_operand());
3303  if (c != nullptr) {
3304  cast->add_unique(domain_set);
3305  continue;
3306  } else {
3307  const auto v = std::dynamic_pointer_cast<const ColumnVar>(p.second);
3308  if (v != nullptr) {
3309  v->add_unique(domain_set);
3310  continue;
3311  }
3312  }
3313  }
3314  p.second->get_domain(domain_set);
3315  if (domain_set.empty()) {
3316  return;
3317  }
3318  }
3319  }
3320  }
3321  if (else_expr != nullptr) {
3322  const auto c = std::dynamic_pointer_cast<const Constant>(else_expr);
3323  if (c != nullptr) {
3324  c->add_unique(domain_set);
3325  } else {
3326  const auto v = std::dynamic_pointer_cast<const ColumnVar>(else_expr);
3327  if (v != nullptr) {
3328  v->add_unique(domain_set);
3329  } else {
3330  const auto cast = std::dynamic_pointer_cast<const UOper>(else_expr);
3331  if (cast != nullptr && cast->get_optype() == kCAST) {
3332  const Constant* c = dynamic_cast<const Constant*>(cast->get_operand());
3333  if (c != nullptr) {
3334  c->add_unique(domain_set);
3335  } else {
3336  const auto v = std::dynamic_pointer_cast<const ColumnVar>(else_expr);
3337  if (v != nullptr) {
3338  v->add_unique(domain_set);
3339  }
3340  }
3341  } else {
3342  else_expr->get_domain(domain_set);
3343  }
3344  }
3345  }
3346  }
3347 }
3348 
3349 std::shared_ptr<Analyzer::Expr> FunctionOper::deep_copy() const {
3350  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
3351  for (size_t i = 0; i < getArity(); ++i) {
3352  args_copy.push_back(getArg(i)->deep_copy());
3353  }
3354  return makeExpr<Analyzer::FunctionOper>(type_info, getName(), args_copy);
3355 }
3356 
3357 bool FunctionOper::operator==(const Expr& rhs) const {
3358  if (type_info != rhs.get_type_info()) {
3359  return false;
3360  }
3361  const auto rhs_func_oper = dynamic_cast<const FunctionOper*>(&rhs);
3362  if (!rhs_func_oper) {
3363  return false;
3364  }
3365  if (getName() != rhs_func_oper->getName()) {
3366  return false;
3367  }
3368  if (getArity() != rhs_func_oper->getArity()) {
3369  return false;
3370  }
3371  for (size_t i = 0; i < getArity(); ++i) {
3372  if (!(*getArg(i) == *(rhs_func_oper->getArg(i)))) {
3373  return false;
3374  }
3375  }
3376  return true;
3377 }
3378 
3379 std::string FunctionOper::toString() const {
3380  std::string str{"(" + name_ + " "};
3381  for (const auto& arg : args_) {
3382  str += arg->toString();
3383  }
3384  str += ")";
3385  return str;
3386 }
3387 
3388 std::shared_ptr<Analyzer::Expr> FunctionOperWithCustomTypeHandling::deep_copy() const {
3389  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
3390  for (size_t i = 0; i < getArity(); ++i) {
3391  args_copy.push_back(getArg(i)->deep_copy());
3392  }
3393  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
3394  type_info, getName(), args_copy);
3395 }
3396 
3398  if (type_info != rhs.get_type_info()) {
3399  return false;
3400  }
3401  const auto rhs_func_oper =
3402  dynamic_cast<const FunctionOperWithCustomTypeHandling*>(&rhs);
3403  if (!rhs_func_oper) {
3404  return false;
3405  }
3406  if (getName() != rhs_func_oper->getName()) {
3407  return false;
3408  }
3409  if (getArity() != rhs_func_oper->getArity()) {
3410  return false;
3411  }
3412  for (size_t i = 0; i < getArity(); ++i) {
3413  if (!(*getArg(i) == *(rhs_func_oper->getArg(i)))) {
3414  return false;
3415  }
3416  }
3417  return true;
3418 }
3419 
3420 double WidthBucketExpr::get_bound_val(const Analyzer::Expr* bound_expr) const {
3421  CHECK(bound_expr);
3422  auto copied_expr = bound_expr->deep_copy();
3423  auto casted_expr = copied_expr->add_cast(SQLTypeInfo(kDOUBLE, false));
3424  CHECK(casted_expr);
3425  auto casted_constant = std::dynamic_pointer_cast<const Analyzer::Constant>(casted_expr);
3426  CHECK(casted_constant);
3427  return casted_constant->get_constval().doubleval;
3428 }
3429 
3431  auto const_partition_count_expr =
3432  dynamic_cast<const Analyzer::Constant*>(partition_count_.get());
3433  if (!const_partition_count_expr) {
3434  return -1;
3435  }
3436  auto d = const_partition_count_expr->get_constval();
3437  switch (const_partition_count_expr->get_type_info().get_type()) {
3438  case kTINYINT:
3439  return d.tinyintval;
3440  case kSMALLINT:
3441  return d.smallintval;
3442  case kINT:
3443  return d.intval;
3444  case kBIGINT: {
3445  auto bi = d.bigintval;
3446  if (bi < 1 || bi > INT32_MAX) {
3447  return -1;
3448  }
3449  return bi;
3450  }
3451  default:
3452  return -1;
3453  }
3454 }
3455 
3456 namespace {
3457 
3459  CHECK(geo);
3460  switch (geo->getType()) {
3462  return kPOINT;
3463  }
3465  return kLINESTRING;
3466  }
3468  return kPOLYGON;
3469  }
3471  return kMULTIPOLYGON;
3472  }
3473  default:
3474  UNREACHABLE();
3475  return kNULLT;
3476  }
3477 }
3478 
3479 } // namespace
3480 
3481 // TODO: fixup null
3482 GeoConstant::GeoConstant(std::unique_ptr<Geospatial::GeoBase>&& geo,
3483  const SQLTypeInfo& ti)
3484  : GeoExpr(ti), geo_(std::move(geo)) {
3485  CHECK(geo_);
3486  if (get_ti_from_geo(geo_.get()) != ti.get_type()) {
3487  throw std::runtime_error("Conflicting types for geo data " + geo_->getWktString() +
3488  " (type provided: " + ti.get_type_name() + ")");
3489  }
3490 }
3491 
3492 std::shared_ptr<Analyzer::Expr> GeoConstant::deep_copy() const {
3493  CHECK(geo_);
3494  return makeExpr<GeoConstant>(geo_->clone(), type_info);
3495 }
3496 
3497 std::string GeoConstant::toString() const {
3498  std::string str{"(GeoConstant "};
3499  CHECK(geo_);
3500  str += geo_->getWktString();
3501  str += ") ";
3502  return str;
3503 }
3504 
3505 std::string GeoConstant::getWKTString() const {
3506  CHECK(geo_);
3507  return geo_->getWktString();
3508 }
3509 
3510 bool GeoConstant::operator==(const Expr& rhs) const {
3511  if (typeid(rhs) != typeid(GeoConstant)) {
3512  return false;
3513  }
3514  const GeoConstant& rhs_c = dynamic_cast<const GeoConstant&>(rhs);
3515  if (type_info != rhs_c.get_type_info() /*|| is_null != rhs_c.get_is_null()*/) {
3516  return false;
3517  }
3518  /* TODO: constant nulls
3519  if (is_null && rhs_c.get_is_null()) {
3520  return true;
3521  }
3522 
3523  */
3524  return *geo_ == *rhs_c.geo_;
3525 }
3526 
3530 }
3531 
3532 std::shared_ptr<Analyzer::Constant> GeoConstant::makePhysicalConstant(
3533  const size_t index) const {
3534  // TODO: handle bounds, etc
3535  const auto num_phys_coords = type_info.get_physical_coord_cols();
3536  CHECK_GE(num_phys_coords, 0);
3537  CHECK_LE(index, size_t(num_phys_coords));
3538  SQLTypeInfo ti = type_info;
3539 
3540  std::vector<double> coords;
3541  std::vector<double> bounds;
3542  std::vector<int> ring_sizes;
3543  std::vector<int> poly_rings;
3544 
3546  geo_->getWktString(), ti, coords, bounds, ring_sizes, poly_rings);
3547 
3548  switch (index) {
3549  case 0: // coords
3550  return Geospatial::convert_coords(coords, ti);
3551  case 1: // ring sizes
3552  return Geospatial::convert_rings(ring_sizes);
3553  case 2: // poly rings
3554  return Geospatial::convert_rings(poly_rings);
3555  default:
3556  UNREACHABLE();
3557  }
3558 
3559  UNREACHABLE();
3560  return nullptr;
3561 }
3562 
3563 std::shared_ptr<Analyzer::Expr> GeoConstant::add_cast(const SQLTypeInfo& new_type_info) {
3564  // TODO: we should eliminate the notion of input and output SRIDs on a type. A type can
3565  // only have 1 SRID. A cast or transforms changes the SRID of the type.
3566  // NOTE: SRID 0 indicates set srid, skip cast
3567  if (!(get_type_info().get_input_srid() == 0) &&
3568  (get_type_info().get_input_srid() != new_type_info.get_output_srid())) {
3569  // run cast
3570  CHECK(geo_);
3571  if (!geo_->transform(get_type_info().get_input_srid(),
3572  new_type_info.get_output_srid())) {
3573  throw std::runtime_error("Failed to transform constant geometry: " + toString());
3574  }
3575  }
3576  return makeExpr<GeoConstant>(std::move(geo_), new_type_info);
3577 }
3578 
3580  const std::string& name,
3581  const std::vector<std::shared_ptr<Analyzer::Expr>>& args,
3582  const std::optional<int>& output_srid_override)
3583  : GeoExpr(ti)
3584  , name_(name)
3585  , args_(args)
3586  , output_srid_override_(output_srid_override) {}
3587 
3588 std::shared_ptr<Analyzer::Expr> GeoOperator::deep_copy() const {
3589  std::vector<std::shared_ptr<Analyzer::Expr>> args;
3590  for (size_t i = 0; i < args_.size(); i++) {
3591  args.push_back(args_[i]->deep_copy());
3592  }
3593  return makeExpr<GeoOperator>(type_info, name_, args, output_srid_override_);
3594 }
3595 
3596 void GeoOperator::collect_rte_idx(std::set<int>& rte_idx_set) const {
3597  for (size_t i = 0; i < size(); i++) {
3598  const auto expr = getOperand(i);
3599  expr->collect_rte_idx(rte_idx_set);
3600  }
3601 }
3602 
3604  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
3605  bool include_agg) const {
3606  for (size_t i = 0; i < size(); i++) {
3607  const auto expr = getOperand(i);
3608  expr->collect_column_var(colvar_set, include_agg);
3609  }
3610 }
3611 
3612 std::string GeoOperator::toString() const {
3613  std::string str{"(" + name_ + " "};
3614  for (const auto& arg : args_) {
3615  str += arg->toString();
3616  }
3617  str += ")";
3618  return str;
3619 }
3620 
3621 bool GeoOperator::operator==(const Expr& rhs) const {
3622  if (typeid(rhs) != typeid(GeoOperator)) {
3623  return false;
3624  }
3625  const GeoOperator& rhs_go = dynamic_cast<const GeoOperator&>(rhs);
3626  if (getName() != rhs_go.getName()) {
3627  return false;
3628  }
3629  if (rhs_go.size() != size()) {
3630  return false;
3631  }
3632  for (size_t i = 0; i < size(); i++) {
3633  if (args_[i].get() != rhs_go.getOperand(i)) {
3634  return false;
3635  }
3636  }
3637  return true;
3638 }
3639 
3640 size_t GeoOperator::size() const {
3641  return args_.size();
3642 }
3643 
3644 Analyzer::Expr* GeoOperator::getOperand(const size_t index) const {
3645  CHECK_LT(index, args_.size());
3646  return args_[index].get();
3647 }
3648 
3649 std::shared_ptr<Analyzer::Expr> GeoOperator::add_cast(const SQLTypeInfo& new_type_info) {
3650  if (get_type_info().is_geometry()) {
3651  std::vector<std::shared_ptr<Analyzer::Expr>> args;
3652  for (size_t i = 0; i < args_.size(); i++) {
3653  args.push_back(args_[i]->deep_copy());
3654  }
3655  CHECK(new_type_info.is_geometry());
3656  return makeExpr<GeoOperator>(new_type_info, name_, args, output_srid_override_);
3657  } else {
3658  auto new_expr = deep_copy();
3659  return makeExpr<UOper>(new_type_info, /*contains_agg=*/false, kCAST, new_expr);
3660  }
3661 }
3662 
3663 std::shared_ptr<Analyzer::Expr> GeoTransformOperator::deep_copy() const {
3664  std::vector<std::shared_ptr<Analyzer::Expr>> args;
3665  for (size_t i = 0; i < args_.size(); i++) {
3666  args.push_back(args_[i]->deep_copy());
3667  }
3668  return makeExpr<GeoTransformOperator>(
3670 }
3671 
3672 std::string GeoTransformOperator::toString() const {
3673  std::string str{"(" + name_ + " "};
3674  for (const auto& arg : args_) {
3675  str += arg->toString();
3676  }
3677  str +=
3678  " : " + std::to_string(input_srid_) + " -> " + std::to_string(output_srid_) + " ";
3679  str += ")";
3680  return str;
3681 }
3682 
3683 bool GeoTransformOperator::operator==(const Expr& rhs) const {
3684  if (typeid(rhs) != typeid(GeoTransformOperator)) {
3685  return false;
3686  }
3687  const GeoTransformOperator& rhs_gto = dynamic_cast<const GeoTransformOperator&>(rhs);
3688  if (getName() != rhs_gto.getName()) {
3689  return false;
3690  }
3691  if (rhs_gto.size() != size()) {
3692  return false;
3693  }
3694  for (size_t i = 0; i < size(); i++) {
3695  if (args_[i].get() != rhs_gto.getOperand(i)) {
3696  return false;
3697  }
3698  }
3699  if (input_srid_ != rhs_gto.input_srid_) {
3700  return false;
3701  }
3702  if (output_srid_ != rhs_gto.output_srid_) {
3703  return false;
3704  }
3705  return true;
3706 }
3707 
3708 } // namespace Analyzer
3709 
3710 bool expr_list_match(const std::vector<std::shared_ptr<Analyzer::Expr>>& lhs,
3711  const std::vector<std::shared_ptr<Analyzer::Expr>>& rhs) {
3712  if (lhs.size() != rhs.size()) {
3713  return false;
3714  }
3715  for (size_t i = 0; i < lhs.size(); ++i) {
3716  if (!(*lhs[i] == *rhs[i])) {
3717  return false;
3718  }
3719  }
3720  return true;
3721 }
3722 
3723 std::shared_ptr<Analyzer::Expr> remove_cast(const std::shared_ptr<Analyzer::Expr>& expr) {
3724  const auto uoper = dynamic_cast<const Analyzer::UOper*>(expr.get());
3725  if (!uoper || uoper->get_optype() != kCAST) {
3726  return expr;
3727  }
3728  return uoper->get_own_operand();
3729 }
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3121
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1222
int8_t tinyintval
Definition: sqltypes.h:212
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3388
int get_table_id() const
Definition: Analyzer.h:193
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1064
Query * next_query
Definition: Analyzer.h:1824
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:1612
Definition: sqldefs.h:69
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1932
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:330
void set_compression(EncodingType c)
Definition: sqltypes.h:429
T roundDecimal(int64_t n, unsigned scale)
Definition: Analyzer.cpp:772
std::string toString() const override
Definition: Analyzer.cpp:2770
Query * parsetree
Definition: Analyzer.h:618
double power10(unsigned const x)
Definition: misc.h:262
float get_likelihood() const
Definition: Analyzer.h:1189
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2081
std::shared_ptr< Analyzer::Expr > expr
Definition: Analyzer.h:1749
std::string toString() const override
Definition: Analyzer.cpp:2688
bool unnest
Definition: Analyzer.h:1750
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:1598
InValues(std::shared_ptr< Analyzer::Expr > a, const std::list< std::shared_ptr< Analyzer::Expr >> &l)
Definition: Analyzer.cpp:1594
const Expr * get_from_expr() const
Definition: Analyzer.h:1352
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:1459
#define IS_LOGIC(X)
Definition: sqldefs.h:60
const Expr * get_partition_count() const
Definition: Analyzer.h:1091
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2427
std::string toString() const override
Definition: Analyzer.cpp:2517
#define NULL_DOUBLE
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3096
GeoConstant(std::unique_ptr< Geospatial::GeoBase > &&geo, const SQLTypeInfo &ti)
Definition: Analyzer.cpp:3482
void get_domain(DomainSet &domain_set) const override
Definition: Analyzer.cpp:3290
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2103
std::string toString() const final
Definition: Analyzer.cpp:3497
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2205
const Expr * get_else_expr() const
Definition: Analyzer.h:1307
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2315
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:225
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:3357
bool Datum_equal(const SQLTypeInfo &ti, Datum val1, Datum val2)
Definition: Analyzer.cpp:2139
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:392
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:2992
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2197
const Expr * get_escape_expr() const
Definition: Analyzer.h:954
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:784
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2010
std::shared_ptr< Analyzer::Expr > decompress()
Definition: Analyzer.cpp:688
size_t getArity() const
Definition: Analyzer.h:1515
std::optional< int > output_srid_override_
Definition: Analyzer.h:1947
Definition: sqltypes.h:49
std::shared_ptr< Analyzer::Expr > remove_cast(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:3723
int64_t DateTruncate(DatetruncField field, const int64_t timeval)
bool is_null_value(const SQLTypeInfo &ti, const Datum &constval)
Definition: Analyzer.cpp:1150
SQLTypes
Definition: sqltypes.h:38
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1412
std::shared_ptr< Analyzer::Expr > operand
Definition: Analyzer.h:410
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:738
std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > expr_pair_list
Definition: Analyzer.h:1334
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:1777
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2067
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2444
#define NULL_FLOAT
void collect_rte_idx(std::set< int > &rte_idx_set) const final
Definition: Analyzer.cpp:3596
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2053
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2267
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:1534
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3031
#define NULL_BIGINT
virtual void add_unique(std::list< const Expr * > &expr_list) const
Definition: Analyzer.cpp:2965
std::shared_ptr< Analyzer::Expr > right_operand
Definition: Analyzer.h:516
#define const
std::shared_ptr< Analyzer::Expr > escape_expr
Definition: Analyzer.h:1068
bool operator==(Expr const &rhs) const override
Definition: Analyzer.cpp:2463
ExtractField get_field() const
Definition: Analyzer.h:1351
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1900
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:1631
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:142
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:134
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:96
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3248
bool is_fp() const
Definition: sqltypes.h:513
const Expr * get_escape_expr() const
Definition: Analyzer.h:1026
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
const Expr * get_right_operand() const
Definition: Analyzer.h:442
SQLOps
Definition: sqldefs.h:29
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:1743
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3173
std::string toString() const override
Definition: Analyzer.cpp:2913
Definition: sqldefs.h:35
int8_t boolval
Definition: sqltypes.h:211
std::string get_compression_name() const
Definition: sqltypes.h:470
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:660
string name
Definition: setup.in.py:72
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:81
Definition: sqldefs.h:36
const std::vector< std::shared_ptr< Analyzer::Expr > > args0_
Definition: Analyzer.h:1693
SQLTypeInfo ti0_
Definition: Analyzer.h:1723
std::shared_ptr< Analyzer::Expr > like_expr
Definition: Analyzer.h:1001
bool get_is_null() const
Definition: Analyzer.h:333
Definition: sqldefs.h:38
const std::vector< std::shared_ptr< Analyzer::Expr > > args0_
Definition: Analyzer.h:1725
std::string toString() const override
Definition: Analyzer.cpp:3379
VarlenDatum * arrayval
Definition: sqltypes.h:218
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3226
int tle_no
Definition: Analyzer.h:1578
#define TO
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:146
const std::shared_ptr< const Analyzer::Expr > arg
Definition: Analyzer.h:689
#define UNREACHABLE()
Definition: Logger.h:253
const std::shared_ptr< Analyzer::Expr > get_own_operand() const
Definition: Analyzer.h:371
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:211
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:215
#define CHECK_GE(x, y)
Definition: Logger.h:222
void cast_from_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1125
#define INT32_MAX
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:3285
std::string toString() const override
Definition: Analyzer.cpp:2730
Definition: sqldefs.h:49
Definition: sqldefs.h:30
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1369
std::shared_ptr< Analyzer::Expr > ExpressionPtr
Definition: Analyzer.h:179
const Geospatial::GeoBase::GeoOp op_
Definition: Analyzer.h:1722
const Expr * get_arg() const
Definition: Analyzer.h:1023
const std::vector< std::shared_ptr< Analyzer::Expr > > order_keys_
Definition: Analyzer.h:1625
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2045
Expr * get_arg() const
Definition: Analyzer.h:1250
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2370
DatetruncField get_field() const
Definition: Analyzer.h:1476
Definition: sqldefs.h:41
const Expr * get_arg() const
Definition: Analyzer.h:1187
bool is_expr_nullable(const Analyzer::Expr *expr)
Definition: Analyzer.cpp:1570
std::shared_ptr< Analyzer::Expr > upper_bound_
Definition: Analyzer.h:1171
Constants for Builtin SQL Types supported by OmniSci.
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1350
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1284
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:3397
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3588
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:60
const std::vector< std::shared_ptr< Analyzer::Expr > > args_
Definition: Analyzer.h:1539
const std::vector< std::shared_ptr< Analyzer::Expr > > partition_keys_
Definition: Analyzer.h:1624
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3187
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2349
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
~Constant() override
Definition: Analyzer.cpp:42
std::unique_ptr< Geospatial::GeoBase > geo_
Definition: Analyzer.h:1894
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2259
SQLQualifier qualifier
Definition: Analyzer.h:513
bool isNull() const
Definition: Analyzer.h:1650
std::shared_ptr< Analyzer::Expr > partition_count_
Definition: Analyzer.h:1172
std::string toString() const override
Definition: Analyzer.cpp:2849
bool is_number() const
Definition: sqltypes.h:514
constexpr int64_t get_datetime_scaled_epoch(const ScalingType direction, const int64_t epoch, const int32_t dimen)
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:1659
std::string toString() const override
Definition: Analyzer.cpp:2578
SQLTypeInfo type_info
Definition: Analyzer.h:175
std::list< const Expr * > DomainSet
Definition: Analyzer.h:60
int32_t intval
Definition: sqltypes.h:214
const Expr * get_arg() const
Definition: Analyzer.h:951
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3062
bool is_time() const
Definition: sqltypes.h:515
std::string toString() const override
Definition: Analyzer.cpp:2526
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1444
double get_bound_val(const Analyzer::Expr *bound_expr) const
Definition: Analyzer.cpp:3420
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2039
std::string to_string(char const *&&v)
~Subquery() override
Definition: Analyzer.cpp:48
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2387
bool operator==(const Expr &) const override
Definition: Analyzer.cpp:3683
int get_rte_idx(const std::string &range_var_name) const
Definition: Analyzer.cpp:1401
void cast_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1114
const std::vector< std::shared_ptr< Analyzer::Expr > > args_
Definition: Analyzer.h:1623
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:3268
std::string toString() const override
Definition: Analyzer.cpp:2547
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:70
#define NULL_INT
std::string toString() const override
Definition: Analyzer.cpp:2653
constexpr double a
Definition: Utm.h:38
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:2975
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2237
std::string toString() const override
Definition: Analyzer.cpp:2748
std::string toString() const override
Definition: Analyzer.cpp:2779
std::string toString() const override
Definition: Analyzer.cpp:2711
Definition: sqldefs.h:73
bool get_calc_encoded_length() const
Definition: Analyzer.h:704
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2214
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1923
std::string toString() const override
Definition: Analyzer.cpp:2890
DatetruncField field_
Definition: Analyzer.h:1502
const Geospatial::GeoBase::GeoOp op_
Definition: Analyzer.h:1691
std::string toString() const override
Definition: Analyzer.cpp:2867
SQLOps get_optype() const
Definition: Analyzer.h:438
std::shared_ptr< Analyzer::Expr > left_operand
Definition: Analyzer.h:515
This file contains the class specification and related data structures for Catalog.
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2448
const std::vector< std::shared_ptr< Analyzer::Expr > > args1_
Definition: Analyzer.h:1726
float floatval
Definition: sqltypes.h:216
std::shared_ptr< Analyzer::Constant > arg1
Definition: Analyzer.h:1287
std::string toString() const
Definition: Analyzer.cpp:2943
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:109
std::shared_ptr< Analyzer::Expr > normalize_simple_predicate(int &rte_idx) const override
Definition: Analyzer.cpp:1483
const std::list< std::shared_ptr< Analyzer::Expr > > value_list
Definition: Analyzer.h:662
EncodingType
Definition: sqltypes.h:233
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:171
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:164
static SQLTypeInfo common_string_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:411
std::shared_ptr< Analyzer::Expr > right_operand_
Definition: Analyzer.h:563
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:1526
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:180
std::shared_ptr< Analyzer::Expr > deep_copy() const final
Definition: Analyzer.cpp:3492
std::string toString() const override
Definition: Analyzer.cpp:2642
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1964
const DatetruncField field_
Definition: Analyzer.h:1460
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:1673
const std::vector< std::shared_ptr< Analyzer::Expr > > & getTuple() const
Definition: Analyzer.h:243
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3178
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2087
bool is_castable(const SQLTypeInfo &new_type_info) const
Definition: sqltypes.h:598
void set_fixed_size()
Definition: sqltypes.h:428
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3169
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3194
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3049
std::shared_ptr< Analyzer::Expr > lower_bound_
Definition: Analyzer.h:1170
bool is_integer() const
Definition: sqltypes.h:511
std::string toString() const override
Definition: Analyzer.cpp:2723
const DateaddField field_
Definition: Analyzer.h:1418
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:116
std::shared_ptr< Analyzer::Expr > from_expr_
Definition: Analyzer.h:1503
bool expr_list_match(const std::vector< std::shared_ptr< Analyzer::Expr >> &lhs, const std::vector< std::shared_ptr< Analyzer::Expr >> &rhs)
Definition: Analyzer.cpp:3710
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:885
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3239
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2680
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:196
std::string toString() const override
Definition: Analyzer.cpp:2511
void set_scale(int s)
Definition: sqltypes.h:424
int64_t bigintval
Definition: sqltypes.h:215
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1432
const std::shared_ptr< Analyzer::Expr > end_
Definition: Analyzer.h:1462
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2410
std::string toString() const override
Definition: Analyzer.cpp:2759
WhichRow get_which_row() const
Definition: Analyzer.h:272
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:3273
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2335
const std::shared_ptr< Analyzer::Expr > number_
Definition: Analyzer.h:1419
const std::list< std::shared_ptr< Analyzer::Expr > > value_list
Definition: Analyzer.h:347
bool is_timeinterval() const
Definition: sqltypes.h:520
std::string resname
Definition: Analyzer.h:1748
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:2676
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2418
#define FROM
const std::string & getName() const
Definition: Analyzer.h:1925
std::string toString() const override
Definition: Analyzer.cpp:2859
static SQLTypeInfo common_numeric_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:451
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3183
SQLTypeInfo ti1_
Definition: Analyzer.h:1724
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1941
Definition: sqldefs.h:37
Definition: sqldefs.h:75
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:1687
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2109
Definition: sqldefs.h:69
int16_t smallintval
Definition: sqltypes.h:213
const std::shared_ptr< Analyzer::Expr > start_
Definition: Analyzer.h:1461
int get_varno() const
Definition: Analyzer.h:274
std::string toString() const
Definition: Analyzer.cpp:2953
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:1645
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:1701
bool nulls_first
Definition: Analyzer.h:1580
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3201
Datum StringToDatum(std::string_view s, SQLTypeInfo &ti)
Definition: Datum.cpp:275
bool get_is_distinct() const
Definition: Analyzer.h:1252
std::shared_ptr< Analyzer::Constant > convert_rings(const std::vector< int > &rings)
Definition: Conversion.h:44
bool is_boolean() const
Definition: sqltypes.h:516
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2225
const std::vector< OrderEntry > collation_
Definition: Analyzer.h:1626
const Expr * get_start_expr() const
Definition: Analyzer.h:1435
void do_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1194
std::string toString() const override
Definition: Analyzer.cpp:2533
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:1821
#define NULL_BOOLEAN
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2495
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3140
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3022
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
WhichRow which_row
Definition: Analyzer.h:295
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1981
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3233
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:1791
int get_precision() const
Definition: sqltypes.h:332
std::string * stringval
Definition: sqltypes.h:220
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) final
Definition: Analyzer.cpp:3563
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3349
size_t getElementCount() const
Definition: Analyzer.h:1648
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:1000
const std::vector< std::shared_ptr< Analyzer::Expr > > tuple_
Definition: Analyzer.h:253
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:3663
Definition: sqldefs.h:34
void set_comp_param(int p)
Definition: sqltypes.h:430
const std::vector< int64_t > value_list
Definition: Analyzer.h:690
static bool getGeoColumns(const std::string &wkt_or_wkb_hex, SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool promote_poly_to_mpoly=false)
Definition: Types.cpp:937
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:3159
const Expr * get_pattern_expr() const
Definition: Analyzer.h:1025
#define CHECK_LT(x, y)
Definition: Logger.h:219
Definition: sqltypes.h:52
Definition: sqltypes.h:53
Definition: sqldefs.h:40
ExpressionPtrVector contained_expressions_
Definition: Analyzer.h:1664
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1955
Definition: sqldefs.h:69
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:1757
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:138
const Expr * get_from_expr() const
Definition: Analyzer.h:1477
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3150
virtual ~Query()
Definition: Analyzer.cpp:52
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:259
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3040
bool is_in_values_nullable(const std::shared_ptr< Analyzer::Expr > &a, const std::list< std::shared_ptr< Analyzer::Expr >> &l)
Definition: Analyzer.cpp:1579
#define CHECK_LE(x, y)
Definition: Logger.h:220
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1394
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:337
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:232
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:192
std::string toString() const override
Definition: Analyzer.cpp:2845
const Expr * get_like_expr() const
Definition: Analyzer.h:953
int64_t convert_decimal_value_to_scale(const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
Definition: Datum.cpp:473
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:2984
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1835
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:1517
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
void set_dimension(int d)
Definition: sqltypes.h:421
std::string toString() const override
Definition: Analyzer.cpp:2863
int get_rte_idx() const
Definition: Analyzer.h:195
const Expr * get_operand() const
Definition: Analyzer.h:370
Analyzer::ExpressionPtr deep_copy() const override
Definition: Analyzer.cpp:220
Datum get_constval() const
Definition: Analyzer.h:334
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3004
Definition: sqldefs.h:32
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2130
const Expr * get_arg() const
Definition: Analyzer.h:796
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) final
Definition: Analyzer.cpp:3649
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:1729
Definition: sqldefs.h:76
int64_t safeScale(T from, unsigned const scale)
Definition: Analyzer.cpp:810
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1324
const Expr * get_arg() const
Definition: Analyzer.h:702
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:331
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:632
int32_t get_partition_count_val() const
Definition: Analyzer.cpp:3430
std::string toString() const override
Definition: Analyzer.cpp:2854
std::vector< RangeTableEntry * > rangetable
Definition: Analyzer.h:1817
GeoOperator(const SQLTypeInfo &ti, const std::string &name, const std::vector< std::shared_ptr< Analyzer::Expr >> &args, const std::optional< int > &output_srid_override=std::nullopt)
Definition: Analyzer.cpp:3579
std::shared_ptr< Analyzer::Expr > from_expr_
Definition: Analyzer.h:1378
std::string get_type_name() const
Definition: sqltypes.h:432
std::string toString() const override
Definition: Analyzer.cpp:2785
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2095
Expression class for the LOWER (lowercase) string function. The &quot;arg&quot; constructor parameter must be a...
Definition: Analyzer.h:837
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1715
Definition: sqltypes.h:41
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:702
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1439
void cast_number(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:839
virtual std::shared_ptr< Analyzer::Expr > deep_copy() const =0
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:829
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int64_t DateTruncateHighPrecisionToDate(const int64_t timeval, const int64_t scale)
size_t size() const
Definition: Analyzer.cpp:3640
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:64
SQLTypeInfo ti0_
Definition: Analyzer.h:1692
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:338
DateaddField get_field() const
Definition: Analyzer.h:1392
SQLAgg get_aggtype() const
Definition: Analyzer.h:1249
#define NULL_TINYINT
std::string toString() const override
Definition: Analyzer.cpp:2875
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:158
const Expr * get_target_value() const
Definition: Analyzer.h:1088
const std::shared_ptr< Analyzer::Expr > datetime_
Definition: Analyzer.h:1420
const Expr * get_arg() const
Definition: Analyzer.h:751
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3130
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:930
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1914
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3106
std::shared_ptr< Analyzer::Constant > convert_coords(const std::vector< double > &coords, const SQLTypeInfo &ti)
Definition: Conversion.h:26
void set_notnull(bool n)
Definition: sqltypes.h:426
T floatFromDecimal(int64_t const dec, unsigned const scale)
Definition: Analyzer.cpp:737
const Expr * get_end_expr() const
Definition: Analyzer.h:1436
#define CHECK(condition)
Definition: Logger.h:209
bool is_geometry() const
Definition: sqltypes.h:521
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2278
ExtractField field_
Definition: Analyzer.h:1377
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2061
bool is_desc
Definition: Analyzer.h:1579
std::string toString() const override
Definition: Analyzer.cpp:2737
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1395
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
bool is_high_precision_timestamp() const
Definition: sqltypes.h:870
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2248
void cast_to_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1133
#define NULL_SMALLINT
#define COMMUTE_COMPARISON(X)
Definition: sqldefs.h:63
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:130
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:1548
std::shared_ptr< Analyzer::Expr > left_operand_
Definition: Analyzer.h:562
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:122
std::string toString() const override
Definition: Analyzer.cpp:3672
Definition: sqldefs.h:33
char * f
Analyzer::Expr * getOperand(const size_t index) const
Definition: Analyzer.cpp:3644
bool operator==(const Expr &) const final
Definition: Analyzer.cpp:3510
std::shared_ptr< Analyzer::Expr > escape_expr
Definition: Analyzer.h:1003
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:1617
const Expr * get_left_operand() const
Definition: Analyzer.h:441
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:206
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:100
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:3213
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3087
Definition: sqltypes.h:45
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:2024
const SqlWindowFunctionKind kind_
Definition: Analyzer.h:1622
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1416
std::shared_ptr< Analyzer::Constant > makePhysicalConstant(const size_t index) const
Definition: Analyzer.cpp:3532
const Expr * get_lower_bound() const
Definition: Analyzer.h:1089
int get_column_id() const
Definition: Analyzer.h:194
std::string toString() const override
Definition: Analyzer.cpp:2649
bool is_string() const
Definition: sqltypes.h:509
std::string getName() const
Definition: Analyzer.h:1513
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const final
Definition: Analyzer.cpp:3603
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2436
bool expr_is(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:1453
constexpr double n
Definition: Utm.h:46
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2180
std::string getWKTString() const
Definition: Analyzer.cpp:3505
std::string toString() const override
Definition: Analyzer.cpp:2741
void group_predicates(std::list< const Expr * > &scan_predicates, std::list< const Expr * > &join_predicates, std::list< const Expr * > &const_predicates) const override
Definition: Analyzer.cpp:1806
std::string toString() const override
Definition: Analyzer.cpp:3612
Definition: sqldefs.h:74
const Expr * get_upper_bound() const
Definition: Analyzer.h:1090
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:150
bool operator==(const Expr &) const override
Definition: Analyzer.cpp:3621
bool contains_agg
Definition: Analyzer.h:176
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:850
bool is_decimal() const
Definition: sqltypes.h:512
const Expr * get_arg() const
Definition: Analyzer.h:897
DatetruncField get_field() const
Definition: Analyzer.h:1434
int get_physical_coord_cols() const
Definition: sqltypes.h:365
Definition: sqldefs.h:72
void find_expr(bool(*f)(const Expr *), std::list< const Expr * > &expr_list) const override
Definition: Analyzer.cpp:3013
std::list< OrderEntry > * order_by
Definition: Analyzer.h:1823
size_t physicalCols() const
Definition: Analyzer.cpp:3527
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:85
std::shared_ptr< Analyzer::Expr > pattern_expr
Definition: Analyzer.h:1066
const std::vector< std::shared_ptr< Analyzer::Expr > > args_
Definition: Analyzer.h:1944