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