OmniSciDB  5ade3759e0
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 "../QueryEngine/DateTimeUtils.h"
28 #include "../Shared/DateConverters.h"
29 #include "../Shared/sql_type_to_string.h"
30 #include "../Shared/sql_window_function_to_string.h"
31 #include "../Shared/sqltypes.h"
32 #include "RangeTableEntry.h"
33 
34 #include <algorithm>
35 #include <cstring>
36 #include <iostream>
37 #include <stdexcept>
38 
39 namespace Analyzer {
40 
42  if ((type_info.is_string() || type_info.is_geometry()) && !is_null) {
43  delete constval.stringval;
44  }
45 }
46 
48  delete parsetree;
49 }
50 
52  for (auto p : rangetable) {
53  delete p;
54  }
55  delete order_by;
56  delete next_query;
57 }
58 
59 std::shared_ptr<Analyzer::Expr> ColumnVar::deep_copy() const {
60  return makeExpr<ColumnVar>(type_info, table_id, column_id, rte_idx);
61 }
62 
63 void ExpressionTuple::collect_rte_idx(std::set<int>& rte_idx_set) const {
64  for (const auto column : tuple_) {
65  column->collect_rte_idx(rte_idx_set);
66  }
67 }
68 
69 std::shared_ptr<Analyzer::Expr> ExpressionTuple::deep_copy() const {
70  std::vector<std::shared_ptr<Expr>> tuple_deep_copy;
71  for (const auto& column : tuple_) {
72  const auto column_deep_copy =
73  std::dynamic_pointer_cast<Analyzer::ColumnVar>(column->deep_copy());
74  CHECK(column_deep_copy);
75  tuple_deep_copy.push_back(column_deep_copy);
76  }
77  return makeExpr<ExpressionTuple>(tuple_deep_copy);
78 }
79 
80 std::shared_ptr<Analyzer::Expr> Var::deep_copy() const {
81  return makeExpr<Var>(type_info, table_id, column_id, rte_idx, which_row, varno);
82 }
83 
84 std::shared_ptr<Analyzer::Expr> Constant::deep_copy() const {
85  Datum d = constval;
86  if ((type_info.is_string() || type_info.is_geometry()) && !is_null) {
87  d.stringval = new std::string(*constval.stringval);
88  }
89  if (type_info.get_type() == kARRAY) {
90  return makeExpr<Constant>(type_info, is_null, value_list);
91  }
92  return makeExpr<Constant>(type_info, is_null, d);
93 }
94 
95 std::shared_ptr<Analyzer::Expr> UOper::deep_copy() const {
96  return makeExpr<UOper>(type_info, contains_agg, optype, operand->deep_copy());
97 }
98 
99 std::shared_ptr<Analyzer::Expr> BinOper::deep_copy() const {
100  return makeExpr<BinOper>(type_info,
101  contains_agg,
102  optype,
103  qualifier,
104  left_operand->deep_copy(),
105  right_operand->deep_copy());
106 }
107 
108 std::shared_ptr<Analyzer::Expr> Subquery::deep_copy() const {
109  // not supported yet.
110  CHECK(false);
111  return nullptr;
112 }
113 
114 std::shared_ptr<Analyzer::Expr> InValues::deep_copy() const {
115  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
116  for (auto p : value_list) {
117  new_value_list.push_back(p->deep_copy());
118  }
119  return makeExpr<InValues>(arg->deep_copy(), new_value_list);
120 }
121 
122 std::shared_ptr<Analyzer::Expr> CharLengthExpr::deep_copy() const {
123  return makeExpr<CharLengthExpr>(arg->deep_copy(), calc_encoded_length);
124 }
125 
126 std::shared_ptr<Analyzer::Expr> KeyForStringExpr::deep_copy() const {
127  return makeExpr<KeyForStringExpr>(arg->deep_copy());
128 }
129 
130 std::shared_ptr<Analyzer::Expr> CardinalityExpr::deep_copy() const {
131  return makeExpr<CardinalityExpr>(arg->deep_copy());
132 }
133 
134 std::shared_ptr<Analyzer::Expr> LikeExpr::deep_copy() const {
135  return makeExpr<LikeExpr>(arg->deep_copy(),
136  like_expr->deep_copy(),
137  escape_expr ? escape_expr->deep_copy() : nullptr,
138  is_ilike,
139  is_simple);
140 }
141 
142 std::shared_ptr<Analyzer::Expr> RegexpExpr::deep_copy() const {
143  return makeExpr<RegexpExpr>(arg->deep_copy(),
144  pattern_expr->deep_copy(),
145  escape_expr ? escape_expr->deep_copy() : nullptr);
146 }
147 
148 std::shared_ptr<Analyzer::Expr> LikelihoodExpr::deep_copy() const {
149  return makeExpr<LikelihoodExpr>(arg->deep_copy(), likelihood);
150 }
151 std::shared_ptr<Analyzer::Expr> AggExpr::deep_copy() const {
152  return makeExpr<AggExpr>(type_info,
153  aggtype,
154  arg == nullptr ? nullptr : arg->deep_copy(),
155  is_distinct,
156  error_rate);
157 }
158 
159 std::shared_ptr<Analyzer::Expr> CaseExpr::deep_copy() const {
160  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
161  new_list;
162  for (auto p : expr_pair_list) {
163  new_list.emplace_back(p.first->deep_copy(), p.second->deep_copy());
164  }
165  return makeExpr<CaseExpr>(type_info,
166  contains_agg,
167  new_list,
168  else_expr == nullptr ? nullptr : else_expr->deep_copy());
169 }
170 
171 std::shared_ptr<Analyzer::Expr> ExtractExpr::deep_copy() const {
172  return makeExpr<ExtractExpr>(type_info, contains_agg, field_, from_expr_->deep_copy());
173 }
174 
175 std::shared_ptr<Analyzer::Expr> DateaddExpr::deep_copy() const {
176  return makeExpr<DateaddExpr>(
177  type_info, field_, number_->deep_copy(), datetime_->deep_copy());
178 }
179 
180 std::shared_ptr<Analyzer::Expr> DatediffExpr::deep_copy() const {
181  return makeExpr<DatediffExpr>(
182  type_info, field_, start_->deep_copy(), end_->deep_copy());
183 }
184 
185 std::shared_ptr<Analyzer::Expr> DatetruncExpr::deep_copy() const {
186  return makeExpr<DatetruncExpr>(
187  type_info, contains_agg, field_, from_expr_->deep_copy());
188 }
189 
190 std::shared_ptr<Analyzer::Expr> OffsetInFragment::deep_copy() const {
191  return makeExpr<OffsetInFragment>();
192 }
193 
194 std::shared_ptr<Analyzer::Expr> WindowFunction::deep_copy() const {
195  return makeExpr<WindowFunction>(
196  type_info, kind_, args_, partition_keys_, order_keys_, collation_);
197 }
198 
200  return makeExpr<Analyzer::ArrayExpr>(
201  type_info, contained_expressions_, expr_index_, local_alloc_);
202 }
203 
205  const SQLTypeInfo& left_type,
206  const SQLTypeInfo& right_type,
207  SQLTypeInfo* new_left_type,
208  SQLTypeInfo* new_right_type) {
209  SQLTypeInfo result_type;
210  SQLTypeInfo common_type;
211  *new_left_type = left_type;
212  *new_right_type = right_type;
213  if (IS_LOGIC(op)) {
214  if (left_type.get_type() != kBOOLEAN || right_type.get_type() != kBOOLEAN) {
215  throw std::runtime_error(
216  "non-boolean operands cannot be used in logic operations.");
217  }
218  result_type = SQLTypeInfo(kBOOLEAN, false);
219  } else if (IS_COMPARISON(op)) {
220  if (left_type != right_type) {
221  if (left_type.is_number() && right_type.is_number()) {
222  common_type = common_numeric_type(left_type, right_type);
223  *new_left_type = common_type;
224  new_left_type->set_notnull(left_type.get_notnull());
225  *new_right_type = common_type;
226  new_right_type->set_notnull(right_type.get_notnull());
227  } else if (left_type.is_time() && right_type.is_time()) {
228  switch (left_type.get_type()) {
229  case kTIMESTAMP:
230  switch (right_type.get_type()) {
231  case kTIME:
232  throw std::runtime_error("Cannont compare between TIMESTAMP and TIME.");
233  break;
234  case kDATE:
235  *new_left_type = SQLTypeInfo(left_type.get_type(),
236  left_type.get_dimension(),
237  0,
238  left_type.get_notnull());
239  *new_right_type = *new_left_type;
240  new_right_type->set_notnull(right_type.get_notnull());
241  break;
242  case kTIMESTAMP:
243  *new_left_type = SQLTypeInfo(
244  kTIMESTAMP,
245  std::max(left_type.get_dimension(), right_type.get_dimension()),
246  0,
247  left_type.get_notnull());
248  *new_right_type = SQLTypeInfo(
249  kTIMESTAMP,
250  std::max(left_type.get_dimension(), right_type.get_dimension()),
251  0,
252  right_type.get_notnull());
253  break;
254  default:
255  CHECK(false);
256  }
257  break;
258  case kTIME:
259  switch (right_type.get_type()) {
260  case kTIMESTAMP:
261  throw std::runtime_error("Cannont compare between TIME and TIMESTAMP.");
262  break;
263  case kDATE:
264  throw std::runtime_error("Cannont compare between TIME and DATE.");
265  break;
266  case kTIME:
267  *new_left_type = SQLTypeInfo(
268  kTIME,
269  std::max(left_type.get_dimension(), right_type.get_dimension()),
270  0,
271  left_type.get_notnull());
272  *new_right_type = SQLTypeInfo(
273  kTIME,
274  std::max(left_type.get_dimension(), right_type.get_dimension()),
275  0,
276  right_type.get_notnull());
277  break;
278  default:
279  CHECK(false);
280  }
281  break;
282  case kDATE:
283  switch (right_type.get_type()) {
284  case kTIMESTAMP:
285  *new_left_type = SQLTypeInfo(right_type.get_type(),
286  right_type.get_dimension(),
287  0,
288  left_type.get_notnull());
289  *new_right_type = *new_left_type;
290  new_right_type->set_notnull(right_type.get_notnull());
291  break;
292  case kDATE:
293  *new_left_type = SQLTypeInfo(left_type.get_type(),
294  left_type.get_dimension(),
295  0,
296  left_type.get_notnull());
297  *new_right_type = *new_left_type;
298  new_right_type->set_notnull(right_type.get_notnull());
299  break;
300  case kTIME:
301  throw std::runtime_error("Cannont compare between DATE and TIME.");
302  break;
303  default:
304  CHECK(false);
305  }
306  break;
307  default:
308  CHECK(false);
309  }
310  } else if (left_type.is_string() && right_type.is_time()) {
311  *new_left_type = right_type;
312  new_left_type->set_notnull(left_type.get_notnull());
313  *new_right_type = right_type;
314  } else if (left_type.is_time() && right_type.is_string()) {
315  *new_left_type = left_type;
316  *new_right_type = left_type;
317  new_right_type->set_notnull(right_type.get_notnull());
318  } else if (left_type.is_string() && right_type.is_string()) {
319  *new_left_type = left_type;
320  *new_right_type = right_type;
321  } else if (left_type.is_boolean() && right_type.is_boolean()) {
322  const bool notnull = left_type.get_notnull() && right_type.get_notnull();
323  common_type = SQLTypeInfo(kBOOLEAN, notnull);
324  *new_left_type = common_type;
325  *new_right_type = common_type;
326  } else {
327  throw std::runtime_error("Cannot compare between " + left_type.get_type_name() +
328  " and " + right_type.get_type_name());
329  }
330  }
331  result_type = SQLTypeInfo(kBOOLEAN, false);
332  } else if (op == kMINUS &&
333  (left_type.get_type() == kDATE || left_type.get_type() == kTIMESTAMP) &&
334  right_type.is_timeinterval()) {
335  *new_left_type = left_type;
336  *new_right_type = right_type;
337  result_type = left_type;
338  } else if (IS_ARITHMETIC(op)) {
339  if (!(left_type.is_number() || left_type.is_timeinterval()) ||
340  !(right_type.is_number() || right_type.is_timeinterval())) {
341  throw std::runtime_error("non-numeric operands in arithmetic operations.");
342  }
343  if (op == kMODULO && (!left_type.is_integer() || !right_type.is_integer())) {
344  throw std::runtime_error("non-integer operands in modulo operation.");
345  }
346  common_type = common_numeric_type(left_type, right_type);
347  if (common_type.is_decimal()) {
348  if (op == kMULTIPLY) {
349  // Decimal multiplication requires common_type adjustment:
350  // dimension and scale of the result should be increased.
351  auto new_dimension = left_type.get_dimension() + right_type.get_dimension();
352  // If new dimension is over 20 digits, the result may overflow, or it may not.
353  // Rely on the runtime overflow detection rather than a static check here.
354  if (common_type.get_dimension() < new_dimension) {
355  common_type.set_dimension(new_dimension);
356  }
357  common_type.set_scale(left_type.get_scale() + right_type.get_scale());
358  } else if (op == kPLUS || op == kMINUS) {
359  // Scale should remain the same but dimension could actually go up
360  common_type.set_dimension(common_type.get_dimension() + 1);
361  }
362  }
363  *new_left_type = common_type;
364  new_left_type->set_notnull(left_type.get_notnull());
365  *new_right_type = common_type;
366  new_right_type->set_notnull(right_type.get_notnull());
367  if (op == kMULTIPLY) {
368  new_left_type->set_scale(left_type.get_scale());
369  new_right_type->set_scale(right_type.get_scale());
370  }
371  result_type = common_type;
372  } else {
373  throw std::runtime_error("invalid binary operator type.");
374  }
375  result_type.set_notnull(left_type.get_notnull() && right_type.get_notnull());
376  return result_type;
377 }
378 
380  const SQLTypeInfo& type2) {
381  SQLTypeInfo common_type;
383  int comp_param = 0;
384  CHECK(type1.is_string() && type2.is_string());
385  // if type1 and type2 have the same DICT encoding then keep it
386  // otherwise, they must be decompressed
387  if (type1.get_compression() == kENCODING_DICT &&
388  type2.get_compression() == kENCODING_DICT) {
389  if (type1.get_comp_param() == type2.get_comp_param() ||
390  type1.get_comp_param() == TRANSIENT_DICT(type2.get_comp_param())) {
391  comp = kENCODING_DICT;
392  comp_param = std::min(type1.get_comp_param(), type2.get_comp_param());
393  }
394  } else if (type1.get_compression() == kENCODING_DICT &&
395  type2.get_compression() == kENCODING_NONE) {
396  comp_param = type1.get_comp_param();
397  } else if (type1.get_compression() == kENCODING_NONE &&
398  type2.get_compression() == kENCODING_DICT) {
399  comp_param = type2.get_comp_param();
400  } else {
401  comp_param = std::max(type1.get_comp_param(),
402  type2.get_comp_param()); // preserve previous comp_param if set
403  }
404  const bool notnull = type1.get_notnull() && type2.get_notnull();
405  if (type1.get_type() == kTEXT || type2.get_type() == kTEXT) {
406  common_type = SQLTypeInfo(kTEXT, 0, 0, notnull, comp, comp_param, kNULLT);
407  return common_type;
408  }
409  common_type = SQLTypeInfo(kVARCHAR,
410  std::max(type1.get_dimension(), type2.get_dimension()),
411  0,
412  notnull,
413  comp,
414  comp_param,
415  kNULLT);
416  return common_type;
417 }
418 
420  const SQLTypeInfo& type2) {
421  SQLTypeInfo common_type;
422  const bool notnull = type1.get_notnull() && type2.get_notnull();
423  if (type1.get_type() == type2.get_type()) {
424  CHECK(((type1.is_number() || type1.is_timeinterval()) &&
425  (type2.is_number() || type2.is_timeinterval())) ||
426  (type1.is_boolean() && type2.is_boolean()));
427  common_type = SQLTypeInfo(type1.get_type(),
428  std::max(type1.get_dimension(), type2.get_dimension()),
429  std::max(type1.get_scale(), type2.get_scale()),
430  notnull);
431  return common_type;
432  }
433  std::string timeinterval_op_error{
434  "Operator type not supported for time interval arithmetic: "};
435  if (type1.is_timeinterval()) {
436  if (!type2.is_integer()) {
437  throw std::runtime_error(timeinterval_op_error + type2.get_type_name());
438  }
439  return type1;
440  }
441  if (type2.is_timeinterval()) {
442  if (!type1.is_integer()) {
443  throw std::runtime_error(timeinterval_op_error + type1.get_type_name());
444  }
445  return type2;
446  }
447  CHECK(type1.is_number() && type2.is_number());
448  switch (type1.get_type()) {
449  case kTINYINT:
450  switch (type2.get_type()) {
451  case kSMALLINT:
452  common_type = SQLTypeInfo(kSMALLINT, notnull);
453  break;
454  case kINT:
455  common_type = SQLTypeInfo(kINT, notnull);
456  break;
457  case kBIGINT:
458  common_type = SQLTypeInfo(kBIGINT, notnull);
459  break;
460  case kFLOAT:
461  common_type = SQLTypeInfo(kFLOAT, notnull);
462  break;
463  case kDOUBLE:
464  common_type = SQLTypeInfo(kDOUBLE, notnull);
465  break;
466  case kNUMERIC:
467  case kDECIMAL:
468  common_type =
470  std::max(5 + type2.get_scale(), type2.get_dimension()),
471  type2.get_scale(),
472  notnull);
473  break;
474  default:
475  CHECK(false);
476  }
477  break;
478  case kSMALLINT:
479  switch (type2.get_type()) {
480  case kTINYINT:
481  common_type = SQLTypeInfo(kSMALLINT, notnull);
482  break;
483  case kINT:
484  common_type = SQLTypeInfo(kINT, notnull);
485  break;
486  case kBIGINT:
487  common_type = SQLTypeInfo(kBIGINT, notnull);
488  break;
489  case kFLOAT:
490  common_type = SQLTypeInfo(kFLOAT, notnull);
491  break;
492  case kDOUBLE:
493  common_type = SQLTypeInfo(kDOUBLE, notnull);
494  break;
495  case kNUMERIC:
496  case kDECIMAL:
497  common_type =
499  std::max(5 + type2.get_scale(), type2.get_dimension()),
500  type2.get_scale(),
501  notnull);
502  break;
503  default:
504  CHECK(false);
505  }
506  break;
507  case kINT:
508  switch (type2.get_type()) {
509  case kTINYINT:
510  common_type = SQLTypeInfo(kINT, notnull);
511  break;
512  case kSMALLINT:
513  common_type = SQLTypeInfo(kINT, notnull);
514  break;
515  case kBIGINT:
516  common_type = SQLTypeInfo(kBIGINT, notnull);
517  break;
518  case kFLOAT:
519  common_type = SQLTypeInfo(kFLOAT, notnull);
520  break;
521  case kDOUBLE:
522  common_type = SQLTypeInfo(kDOUBLE, notnull);
523  break;
524  case kNUMERIC:
525  case kDECIMAL:
526  common_type = SQLTypeInfo(
527  kDECIMAL,
528  std::max(std::min(19, 10 + type2.get_scale()), type2.get_dimension()),
529  type2.get_scale(),
530  notnull);
531  break;
532  default:
533  CHECK(false);
534  }
535  break;
536  case kBIGINT:
537  switch (type2.get_type()) {
538  case kTINYINT:
539  common_type = SQLTypeInfo(kBIGINT, notnull);
540  break;
541  case kSMALLINT:
542  common_type = SQLTypeInfo(kBIGINT, notnull);
543  break;
544  case kINT:
545  common_type = SQLTypeInfo(kBIGINT, notnull);
546  break;
547  case kFLOAT:
548  common_type = SQLTypeInfo(kFLOAT, notnull);
549  break;
550  case kDOUBLE:
551  common_type = SQLTypeInfo(kDOUBLE, notnull);
552  break;
553  case kNUMERIC:
554  case kDECIMAL:
555  common_type = SQLTypeInfo(kDECIMAL, 19, type2.get_scale(), notnull);
556  break;
557  default:
558  CHECK(false);
559  }
560  break;
561  case kFLOAT:
562  switch (type2.get_type()) {
563  case kTINYINT:
564  common_type = SQLTypeInfo(kFLOAT, notnull);
565  break;
566  case kSMALLINT:
567  common_type = SQLTypeInfo(kFLOAT, notnull);
568  break;
569  case kINT:
570  common_type = SQLTypeInfo(kFLOAT, notnull);
571  break;
572  case kBIGINT:
573  common_type = SQLTypeInfo(kFLOAT, notnull);
574  break;
575  case kDOUBLE:
576  common_type = SQLTypeInfo(kDOUBLE, notnull);
577  break;
578  case kNUMERIC:
579  case kDECIMAL:
580  common_type = SQLTypeInfo(kFLOAT, notnull);
581  break;
582  default:
583  CHECK(false);
584  }
585  break;
586  case kDOUBLE:
587  switch (type2.get_type()) {
588  case kTINYINT:
589  case kSMALLINT:
590  case kINT:
591  case kBIGINT:
592  case kFLOAT:
593  case kNUMERIC:
594  case kDECIMAL:
595  common_type = SQLTypeInfo(kDOUBLE, notnull);
596  break;
597  default:
598  CHECK(false);
599  }
600  break;
601  case kNUMERIC:
602  case kDECIMAL:
603  switch (type2.get_type()) {
604  case kSMALLINT:
605  common_type =
607  std::max(5 + type1.get_scale(), type1.get_dimension()),
608  type1.get_scale(),
609  notnull);
610  break;
611  case kINT:
612  common_type = SQLTypeInfo(
613  kDECIMAL,
614  std::max(std::min(19, 10 + type1.get_scale()), type2.get_dimension()),
615  type1.get_scale(),
616  notnull);
617  break;
618  case kBIGINT:
619  common_type = SQLTypeInfo(kDECIMAL, 19, type1.get_scale(), notnull);
620  break;
621  case kFLOAT:
622  common_type = SQLTypeInfo(kFLOAT, notnull);
623  break;
624  case kDOUBLE:
625  common_type = SQLTypeInfo(kDOUBLE, notnull);
626  break;
627  case kNUMERIC:
628  case kDECIMAL: {
629  int common_scale = std::max(type1.get_scale(), type2.get_scale());
630  common_type = SQLTypeInfo(kDECIMAL,
631  std::max(type1.get_dimension() - type1.get_scale(),
632  type2.get_dimension() - type2.get_scale()) +
633  common_scale,
634  common_scale,
635  notnull);
636  break;
637  }
638  default:
639  CHECK(false);
640  }
641  break;
642  default:
643  CHECK(false);
644  }
645  common_type.set_fixed_size();
646  return common_type;
647 }
648 
649 std::shared_ptr<Analyzer::Expr> Expr::decompress() {
651  return shared_from_this();
652  }
653  SQLTypeInfo new_type_info(type_info.get_type(),
658  0,
660  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
661 }
662 
663 std::shared_ptr<Analyzer::Expr> Expr::add_cast(const SQLTypeInfo& new_type_info) {
664  if (new_type_info == type_info) {
665  return shared_from_this();
666  }
667  if (new_type_info.is_string() && type_info.is_string() &&
668  new_type_info.get_compression() == kENCODING_DICT &&
670  (new_type_info.get_comp_param() == type_info.get_comp_param() ||
671  new_type_info.get_comp_param() == TRANSIENT_DICT(type_info.get_comp_param()))) {
672  return shared_from_this();
673  }
674  if (!type_info.is_castable(new_type_info)) {
675  throw std::runtime_error("Cannot CAST from " + type_info.get_type_name() + " to " +
676  new_type_info.get_type_name());
677  }
678  // @TODO(wei) temporary restriction until executor can support this.
679  if (typeid(*this) != typeid(Constant) && new_type_info.is_string() &&
680  new_type_info.get_compression() == kENCODING_DICT &&
681  new_type_info.get_comp_param() <= TRANSIENT_DICT_ID) {
683  throw std::runtime_error(
684  "Cannot group by string columns which are not dictionary encoded.");
685  }
686  throw std::runtime_error(
687  "Internal error: Cannot apply transient dictionary encoding to non-literal "
688  "expression "
689  "yet.");
690  }
691  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
692 }
693 
694 namespace {
695 
696 struct IntFracRepr {
697  const int64_t integral;
698  const int64_t fractional;
699  const int64_t scale;
700 };
701 
702 IntFracRepr decimal_to_int_frac(const int64_t dec, const SQLTypeInfo& ti) {
703  if (ti.get_scale() > 18) {
704  auto truncated_ti = ti;
705  truncated_ti.set_scale(18);
706  auto truncated_dec = dec;
707  for (int i = 0; i < ti.get_scale() - 18; ++i) {
708  truncated_dec /= 10;
709  }
710  return decimal_to_int_frac(truncated_dec, truncated_ti);
711  }
712  int64_t integral_part = dec;
713  int64_t scale = 1;
714  for (int i = 0; i < ti.get_scale(); i++) {
715  integral_part /= 10;
716  scale *= 10;
717  }
718  return {integral_part, dec - integral_part * scale, scale};
719 }
720 
721 template <class T>
722 T safe_narrow(const int64_t v) {
723  if (v > std::numeric_limits<T>::max() || v < std::numeric_limits<T>::min()) {
724  throw std::runtime_error("Overflow or underflow");
725  }
726  return static_cast<T>(v);
727 }
728 
729 } // namespace
730 
731 void Constant::cast_number(const SQLTypeInfo& new_type_info) {
732  switch (type_info.get_type()) {
733  case kTINYINT:
734  switch (new_type_info.get_type()) {
735  case kTINYINT:
736  break;
737  case kINT:
738  constval.intval = (int32_t)constval.tinyintval;
739  break;
740  case kSMALLINT:
742  break;
743  case kBIGINT:
745  break;
746  case kDOUBLE:
748  break;
749  case kFLOAT:
751  break;
752  case kNUMERIC:
753  case kDECIMAL:
755  for (int i = 0; i < new_type_info.get_scale(); i++) {
756  constval.bigintval *= 10;
757  }
758  break;
759  default:
760  CHECK(false);
761  }
762  break;
763  case kINT:
764  switch (new_type_info.get_type()) {
765  case kTINYINT:
766  constval.tinyintval = safe_narrow<int8_t>(constval.intval);
767  break;
768  case kINT:
769  break;
770  case kSMALLINT:
771  constval.smallintval = safe_narrow<int16_t>(constval.intval);
772  break;
773  case kBIGINT:
774  constval.bigintval = (int64_t)constval.intval;
775  break;
776  case kDOUBLE:
777  constval.doubleval = (double)constval.intval;
778  break;
779  case kFLOAT:
780  constval.floatval = (float)constval.intval;
781  break;
782  case kNUMERIC:
783  case kDECIMAL:
784  constval.bigintval = (int64_t)constval.intval;
785  for (int i = 0; i < new_type_info.get_scale(); i++) {
786  constval.bigintval *= 10;
787  }
788  break;
789  default:
790  CHECK(false);
791  }
792  break;
793  case kSMALLINT:
794  switch (new_type_info.get_type()) {
795  case kTINYINT:
796  constval.tinyintval = safe_narrow<int8_t>(constval.smallintval);
797  break;
798  case kINT:
799  constval.intval = (int32_t)constval.smallintval;
800  break;
801  case kSMALLINT:
802  break;
803  case kBIGINT:
805  break;
806  case kDOUBLE:
808  break;
809  case kFLOAT:
811  break;
812  case kNUMERIC:
813  case kDECIMAL:
815  for (int i = 0; i < new_type_info.get_scale(); i++) {
816  constval.bigintval *= 10;
817  }
818  break;
819  default:
820  CHECK(false);
821  }
822  break;
823  case kBIGINT:
824  switch (new_type_info.get_type()) {
825  case kTINYINT:
826  constval.tinyintval = safe_narrow<int8_t>(constval.bigintval);
827  break;
828  case kINT:
829  constval.intval = safe_narrow<int32_t>(constval.bigintval);
830  break;
831  case kSMALLINT:
832  constval.smallintval = safe_narrow<int16_t>(constval.bigintval);
833  break;
834  case kBIGINT:
835  break;
836  case kDOUBLE:
838  break;
839  case kFLOAT:
841  break;
842  case kNUMERIC:
843  case kDECIMAL:
844  for (int i = 0; i < new_type_info.get_scale(); i++) {
845  constval.bigintval *= 10;
846  }
847  break;
848  default:
849  CHECK(false);
850  }
851  break;
852  case kDOUBLE:
853  switch (new_type_info.get_type()) {
854  case kTINYINT:
856  break;
857  case kINT:
858  constval.intval = (int32_t)constval.doubleval;
859  break;
860  case kSMALLINT:
862  break;
863  case kBIGINT:
865  break;
866  case kDOUBLE:
867  break;
868  case kFLOAT:
870  break;
871  case kNUMERIC:
872  case kDECIMAL:
873  for (int i = 0; i < new_type_info.get_scale(); i++) {
874  constval.doubleval *= 10;
875  }
877  break;
878  default:
879  CHECK(false);
880  }
881  break;
882  case kFLOAT:
883  switch (new_type_info.get_type()) {
884  case kTINYINT:
886  break;
887  case kINT:
888  constval.intval = (int32_t)constval.floatval;
889  break;
890  case kSMALLINT:
892  break;
893  case kBIGINT:
894  constval.bigintval = (int64_t)constval.floatval;
895  break;
896  case kDOUBLE:
898  break;
899  case kFLOAT:
900  break;
901  case kNUMERIC:
902  case kDECIMAL:
903  for (int i = 0; i < new_type_info.get_scale(); i++) {
904  constval.floatval *= 10;
905  }
906  constval.bigintval = (int64_t)constval.floatval;
907  break;
908  default:
909  CHECK(false);
910  }
911  break;
912  case kNUMERIC:
913  case kDECIMAL:
914  switch (new_type_info.get_type()) {
915  case kTINYINT:
916  for (int i = 0; i < type_info.get_scale(); i++) {
917  constval.bigintval /= 10;
918  }
919  constval.tinyintval = safe_narrow<int8_t>(constval.bigintval);
920  break;
921  case kINT:
922  for (int i = 0; i < type_info.get_scale(); i++) {
923  constval.bigintval /= 10;
924  }
925  constval.intval = safe_narrow<int32_t>(constval.bigintval);
926  break;
927  case kSMALLINT:
928  for (int i = 0; i < type_info.get_scale(); i++) {
929  constval.bigintval /= 10;
930  }
932  break;
933  case kBIGINT:
934  for (int i = 0; i < type_info.get_scale(); i++) {
935  constval.bigintval /= 10;
936  }
937  break;
938  case kDOUBLE: {
939  const auto int_frac = decimal_to_int_frac(constval.bigintval, type_info);
940  constval.doubleval = int_frac.integral +
941  static_cast<double>(int_frac.fractional) / int_frac.scale;
942  break;
943  }
944  case kFLOAT: {
945  const auto int_frac = decimal_to_int_frac(constval.bigintval, type_info);
946  constval.floatval = int_frac.integral +
947  static_cast<double>(int_frac.fractional) / int_frac.scale;
948  break;
949  }
950  case kNUMERIC:
951  case kDECIMAL:
953  constval.bigintval, type_info, new_type_info);
954  break;
955  default:
956  CHECK(false);
957  }
958  break;
959  case kTIMESTAMP:
960  switch (new_type_info.get_type()) {
961  case kTINYINT:
962  constval.tinyintval = static_cast<int8_t>(constval.bigintval);
963  break;
964  case kINT:
965  constval.intval = static_cast<int32_t>(constval.bigintval);
966  break;
967  case kSMALLINT:
968  constval.smallintval = static_cast<int16_t>(constval.bigintval);
969  break;
970  case kBIGINT:
971  constval.bigintval = static_cast<int64_t>(constval.bigintval);
972  break;
973  case kDOUBLE:
974  constval.doubleval = static_cast<double>(constval.bigintval);
975  break;
976  case kFLOAT:
977  constval.floatval = static_cast<float>(constval.bigintval);
978  break;
979  case kNUMERIC:
980  case kDECIMAL:
981  for (int i = 0; i < new_type_info.get_scale(); i++) {
982  constval.bigintval *= 10;
983  }
984  break;
985  default:
986  CHECK(false);
987  }
988  break;
989  case kBOOLEAN:
990  switch (new_type_info.get_type()) {
991  case kTINYINT:
993  break;
994  case kINT:
995  constval.intval = constval.boolval ? 1 : 0;
996  break;
997  case kSMALLINT:
999  break;
1000  case kBIGINT:
1001  constval.bigintval = constval.boolval ? 1 : 0;
1002  break;
1003  case kDOUBLE:
1004  constval.doubleval = constval.boolval ? 1 : 0;
1005  break;
1006  case kFLOAT:
1007  constval.floatval = constval.boolval ? 1 : 0;
1008  break;
1009  case kNUMERIC:
1010  case kDECIMAL:
1011  constval.bigintval = constval.boolval ? 1 : 0;
1012  for (int i = 0; i < new_type_info.get_scale(); i++) {
1013  constval.bigintval *= 10;
1014  }
1015  break;
1016  default:
1017  CHECK(false);
1018  }
1019  break;
1020  default:
1021  CHECK(false);
1022  }
1023  type_info = new_type_info;
1024 }
1025 
1026 void Constant::cast_string(const SQLTypeInfo& new_type_info) {
1027  std::string* s = constval.stringval;
1028  if (s != nullptr && new_type_info.get_type() != kTEXT &&
1029  static_cast<size_t>(new_type_info.get_dimension()) < s->length()) {
1030  // truncate string
1031  constval.stringval = new std::string(s->substr(0, new_type_info.get_dimension()));
1032  delete s;
1033  }
1034  type_info = new_type_info;
1035 }
1036 
1037 void Constant::cast_from_string(const SQLTypeInfo& new_type_info) {
1038  std::string* s = constval.stringval;
1039  SQLTypeInfo ti = new_type_info;
1040  constval = StringToDatum(*s, ti);
1041  delete s;
1042  type_info = new_type_info;
1043 }
1044 
1045 void Constant::cast_to_string(const SQLTypeInfo& str_type_info) {
1046  const auto str_val = DatumToString(constval, type_info);
1047  constval.stringval = new std::string(str_val);
1048  if (str_type_info.get_type() != kTEXT &&
1049  constval.stringval->length() > static_cast<size_t>(str_type_info.get_dimension())) {
1050  // truncate the string
1051  *constval.stringval = constval.stringval->substr(0, str_type_info.get_dimension());
1052  }
1053  type_info = str_type_info;
1054 }
1055 
1056 void Constant::do_cast(const SQLTypeInfo& new_type_info) {
1057  if (type_info == new_type_info) {
1058  return;
1059  }
1060  if (is_null) {
1061  type_info = new_type_info;
1062  set_null_value();
1063  return;
1064  }
1065  if (new_type_info.is_number() &&
1067  type_info.get_type() == kBOOLEAN)) {
1068  cast_number(new_type_info);
1069  } else if (new_type_info.is_geometry() && type_info.is_string()) {
1070  type_info = new_type_info;
1071  } else if (new_type_info.is_geometry() &&
1072  type_info.get_type() == new_type_info.get_type()) {
1073  type_info = new_type_info;
1074  } else if (new_type_info.is_boolean() && type_info.is_boolean()) {
1075  type_info = new_type_info;
1076  } else if (new_type_info.is_string() && type_info.is_string()) {
1077  cast_string(new_type_info);
1078  } else if (type_info.is_string() || type_info.get_type() == kVARCHAR) {
1079  cast_from_string(new_type_info);
1080  } else if (new_type_info.is_string()) {
1081  cast_to_string(new_type_info);
1082  } else if (new_type_info.get_type() == kDATE && type_info.get_type() == kDATE) {
1083  type_info = new_type_info;
1084  } else if (new_type_info.get_type() == kDATE && type_info.get_type() == kTIMESTAMP) {
1086  type_info.is_high_precision_timestamp()
1091  type_info = new_type_info;
1092  } else if ((type_info.get_type() == kTIMESTAMP || type_info.get_type() == kDATE) &&
1093  new_type_info.get_type() == kTIMESTAMP) {
1094  const auto dimen = (type_info.get_type() == kDATE) ? 0 : type_info.get_dimension();
1095  if (dimen != new_type_info.get_dimension()) {
1096  constval.bigintval = dimen < new_type_info.get_dimension()
1100  new_type_info.get_dimension() - dimen))
1104  dimen - new_type_info.get_dimension()));
1105  }
1106  type_info = new_type_info;
1107  } else if (new_type_info.is_array() && type_info.is_array()) {
1108  auto new_sub_ti = new_type_info.get_elem_type();
1109  for (auto& v : value_list) {
1110  auto c = std::dynamic_pointer_cast<Analyzer::Constant>(v);
1111  if (!c) {
1112  throw std::runtime_error("Invalid array cast.");
1113  }
1114  c->do_cast(new_sub_ti);
1115  }
1116  type_info = new_type_info;
1117  } else if (get_is_null() && (new_type_info.is_number() || new_type_info.is_time() ||
1118  new_type_info.is_string())) {
1119  type_info = new_type_info;
1120  set_null_value();
1121  } else {
1122  throw std::runtime_error("Cast from " + type_info.get_type_name() + " to " +
1123  new_type_info.get_type_name() + " not supported");
1124  }
1125 }
1126 
1128  switch (type_info.get_type()) {
1129  case kBOOLEAN:
1131  break;
1132  case kTINYINT:
1134  break;
1135  case kINT:
1137  break;
1138  case kSMALLINT:
1140  break;
1141  case kBIGINT:
1142  case kNUMERIC:
1143  case kDECIMAL:
1145  break;
1146  case kTIME:
1147  case kTIMESTAMP:
1148  case kDATE:
1150  break;
1151  case kVARCHAR:
1152  case kCHAR:
1153  case kTEXT:
1154  constval.stringval = nullptr;
1155  break;
1156  case kPOINT:
1157  case kLINESTRING:
1158  case kPOLYGON:
1159  case kMULTIPOLYGON:
1160  constval.stringval = nullptr;
1161  break;
1162  case kFLOAT:
1164  break;
1165  case kDOUBLE:
1167  break;
1168  case kNULLT:
1169  constval.bigintval = 0;
1170  break;
1171  case kARRAY:
1172  constval.arrayval = nullptr;
1173  break;
1174  default:
1175  CHECK(false);
1176  }
1177 }
1178 
1179 std::shared_ptr<Analyzer::Expr> Constant::add_cast(const SQLTypeInfo& new_type_info) {
1180  if (is_null) {
1181  type_info = new_type_info;
1182  set_null_value();
1183  return shared_from_this();
1184  }
1185  if (new_type_info.get_compression() != type_info.get_compression()) {
1186  if (new_type_info.get_compression() != kENCODING_NONE) {
1187  SQLTypeInfo new_ti = new_type_info;
1188  if (new_ti.get_compression() != kENCODING_DATE_IN_DAYS) {
1190  }
1191  do_cast(new_ti);
1192  }
1193  return Expr::add_cast(new_type_info);
1194  }
1195  if (is_member_of_typeset<kINT, kDECIMAL, kFLOAT, kDOUBLE>(new_type_info) &&
1196  is_member_of_typeset<kTIME, kDATE>(type_info)) {
1197  // Let the codegen phase deal with casts from date/time to a number.
1198  return makeExpr<UOper>(new_type_info, contains_agg, kCAST, shared_from_this());
1199  }
1200  do_cast(new_type_info);
1201  return shared_from_this();
1202 }
1203 
1204 std::shared_ptr<Analyzer::Expr> UOper::add_cast(const SQLTypeInfo& new_type_info) {
1205  if (optype != kCAST) {
1206  return Expr::add_cast(new_type_info);
1207  }
1208  if (type_info.is_string() && new_type_info.is_string() &&
1209  new_type_info.get_compression() == kENCODING_DICT &&
1211  const SQLTypeInfo oti = operand->get_type_info();
1212  if (oti.is_string() && oti.get_compression() == kENCODING_DICT &&
1213  (oti.get_comp_param() == new_type_info.get_comp_param() ||
1214  oti.get_comp_param() == TRANSIENT_DICT(new_type_info.get_comp_param()))) {
1215  auto result = operand;
1216  operand = nullptr;
1217  return result;
1218  }
1219  }
1220  return Expr::add_cast(new_type_info);
1221 }
1222 
1223 std::shared_ptr<Analyzer::Expr> CaseExpr::add_cast(const SQLTypeInfo& new_type_info) {
1224  SQLTypeInfo ti = new_type_info;
1225  if (new_type_info.is_string() && new_type_info.get_compression() == kENCODING_DICT &&
1226  new_type_info.get_comp_param() == TRANSIENT_DICT_ID && type_info.is_string() &&
1230  }
1231 
1232  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1233  new_expr_pair_list;
1234  for (auto& p : expr_pair_list) {
1235  new_expr_pair_list.emplace_back(
1236  std::make_pair(p.first, p.second->deep_copy()->add_cast(ti)));
1237  }
1238 
1239  if (else_expr != nullptr) {
1240  else_expr = else_expr->add_cast(ti);
1241  }
1242  // Replace the current WHEN THEN pair list once we are sure all casts have succeeded
1243  expr_pair_list = new_expr_pair_list;
1244 
1245  type_info = ti;
1246  return shared_from_this();
1247 }
1248 
1249 std::shared_ptr<Analyzer::Expr> Subquery::add_cast(const SQLTypeInfo& new_type_info) {
1250  // not supported yet.
1251  CHECK(false);
1252  return nullptr;
1253 }
1254 
1255 int Query::get_rte_idx(const std::string& name) const {
1256  int rte_idx = 0;
1257  for (auto rte : rangetable) {
1258  if (rte->get_rangevar() == name) {
1259  return rte_idx;
1260  }
1261  rte_idx++;
1262  }
1263  return -1;
1264 }
1265 
1267  rangetable.push_back(rte);
1268 }
1269 
1271  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1272  if (!groupby.empty()) {
1273  for (auto e : groupby) {
1274  auto c = std::dynamic_pointer_cast<ColumnVar>(e);
1275  if (c && table_id == c->get_table_id() && column_id == c->get_column_id()) {
1276  return;
1277  }
1278  }
1279  }
1280  throw std::runtime_error(
1281  "expressions in the SELECT or HAVING clause must be an aggregate function or an "
1282  "expression "
1283  "over GROUP BY columns.");
1284 }
1285 
1287  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1288  if (which_row != kGROUPBY) {
1289  throw std::runtime_error("Internal error: invalid VAR in GROUP BY or HAVING.");
1290  }
1291 }
1292 
1294  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1295  operand->check_group_by(groupby);
1296 }
1297 
1299  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
1300  left_operand->check_group_by(groupby);
1301  right_operand->check_group_by(groupby);
1302 }
1303 
1304 namespace {
1305 
1306 template <class T>
1307 bool expr_is(const std::shared_ptr<Analyzer::Expr>& expr) {
1308  return std::dynamic_pointer_cast<T>(expr) != nullptr;
1309 }
1310 
1311 } // namespace
1312 
1314  const std::shared_ptr<Analyzer::Expr> cast_operand,
1315  const std::shared_ptr<Analyzer::Expr> const_operand) {
1316  if (expr_is<UOper>(cast_operand) && expr_is<Constant>(const_operand)) {
1317  auto u_expr = std::dynamic_pointer_cast<UOper>(cast_operand);
1318  if (u_expr->get_optype() != kCAST) {
1319  return false;
1320  }
1321  if (!(expr_is<Analyzer::ColumnVar>(u_expr->get_own_operand()) &&
1322  !expr_is<Analyzer::Var>(u_expr->get_own_operand()))) {
1323  return false;
1324  }
1325  const auto& ti = u_expr->get_type_info();
1326  if (ti.is_time() && u_expr->get_operand()->get_type_info().is_time()) {
1327  // Allow casts between time types to pass through
1328  return true;
1329  } else if (ti.is_integer() && u_expr->get_operand()->get_type_info().is_integer()) {
1330  // Allow casts between integer types to pass through
1331  return true;
1332  }
1333  }
1334  return false;
1335 }
1336 
1337 std::shared_ptr<Analyzer::Expr> BinOper::normalize_simple_predicate(int& rte_idx) const {
1338  rte_idx = -1;
1339  if (!IS_COMPARISON(optype) || qualifier != kONE) {
1340  return nullptr;
1341  }
1342  if (expr_is<UOper>(left_operand)) {
1343  if (BinOper::simple_predicate_has_simple_cast(left_operand, right_operand)) {
1344  auto uo = std::dynamic_pointer_cast<UOper>(left_operand);
1345  auto cv = std::dynamic_pointer_cast<ColumnVar>(uo->get_own_operand());
1346  rte_idx = cv->get_rte_idx();
1347  return this->deep_copy();
1348  }
1349  } else if (expr_is<UOper>(right_operand)) {
1350  if (BinOper::simple_predicate_has_simple_cast(right_operand, left_operand)) {
1351  auto uo = std::dynamic_pointer_cast<UOper>(right_operand);
1352  auto cv = std::dynamic_pointer_cast<ColumnVar>(uo->get_own_operand());
1353  rte_idx = cv->get_rte_idx();
1354  return makeExpr<BinOper>(type_info,
1355  contains_agg,
1356  COMMUTE_COMPARISON(optype),
1357  qualifier,
1358  right_operand->deep_copy(),
1359  left_operand->deep_copy());
1360  }
1361  } else if (expr_is<ColumnVar>(left_operand) && !expr_is<Var>(left_operand) &&
1362  expr_is<Constant>(right_operand)) {
1363  auto cv = std::dynamic_pointer_cast<ColumnVar>(left_operand);
1364  rte_idx = cv->get_rte_idx();
1365  return this->deep_copy();
1366  } else if (expr_is<Constant>(left_operand) && expr_is<ColumnVar>(right_operand) &&
1367  !expr_is<Var>(right_operand)) {
1368  auto cv = std::dynamic_pointer_cast<ColumnVar>(right_operand);
1369  rte_idx = cv->get_rte_idx();
1370  return makeExpr<BinOper>(type_info,
1371  contains_agg,
1372  COMMUTE_COMPARISON(optype),
1373  qualifier,
1374  right_operand->deep_copy(),
1375  left_operand->deep_copy());
1376  }
1377  return nullptr;
1378 }
1379 
1380 void ColumnVar::group_predicates(std::list<const Expr*>& scan_predicates,
1381  std::list<const Expr*>& join_predicates,
1382  std::list<const Expr*>& const_predicates) const {
1383  if (type_info.get_type() == kBOOLEAN) {
1384  scan_predicates.push_back(this);
1385  }
1386 }
1387 
1388 void UOper::group_predicates(std::list<const Expr*>& scan_predicates,
1389  std::list<const Expr*>& join_predicates,
1390  std::list<const Expr*>& const_predicates) const {
1391  std::set<int> rte_idx_set;
1392  operand->collect_rte_idx(rte_idx_set);
1393  if (rte_idx_set.size() > 1) {
1394  join_predicates.push_back(this);
1395  } else if (rte_idx_set.size() == 1) {
1396  scan_predicates.push_back(this);
1397  } else {
1398  const_predicates.push_back(this);
1399  }
1400 }
1401 
1402 void BinOper::group_predicates(std::list<const Expr*>& scan_predicates,
1403  std::list<const Expr*>& join_predicates,
1404  std::list<const Expr*>& const_predicates) const {
1405  if (optype == kAND) {
1406  left_operand->group_predicates(scan_predicates, join_predicates, const_predicates);
1407  right_operand->group_predicates(scan_predicates, join_predicates, const_predicates);
1408  return;
1409  }
1410  std::set<int> rte_idx_set;
1411  left_operand->collect_rte_idx(rte_idx_set);
1412  right_operand->collect_rte_idx(rte_idx_set);
1413  if (rte_idx_set.size() > 1) {
1414  join_predicates.push_back(this);
1415  } else if (rte_idx_set.size() == 1) {
1416  scan_predicates.push_back(this);
1417  } else {
1418  const_predicates.push_back(this);
1419  }
1420 }
1421 
1422 namespace {
1423 
1425  const auto const_expr = dynamic_cast<const Analyzer::Constant*>(expr);
1426  if (const_expr) {
1427  return const_expr->get_is_null();
1428  }
1429  const auto& expr_ti = expr->get_type_info();
1430  return !expr_ti.get_notnull();
1431 }
1432 
1433 bool is_in_values_nullable(const std::shared_ptr<Analyzer::Expr>& a,
1434  const std::list<std::shared_ptr<Analyzer::Expr>>& l) {
1435  if (is_expr_nullable(a.get())) {
1436  return true;
1437  }
1438  for (const auto& v : l) {
1439  if (is_expr_nullable(v.get())) {
1440  return true;
1441  }
1442  }
1443  return false;
1444 }
1445 
1446 } // namespace
1447 
1448 InValues::InValues(std::shared_ptr<Analyzer::Expr> a,
1449  const std::list<std::shared_ptr<Analyzer::Expr>>& l)
1450  : Expr(kBOOLEAN, !is_in_values_nullable(a, l)), arg(a), value_list(l) {}
1451 
1452 void InValues::group_predicates(std::list<const Expr*>& scan_predicates,
1453  std::list<const Expr*>& join_predicates,
1454  std::list<const Expr*>& const_predicates) const {
1455  std::set<int> rte_idx_set;
1456  arg->collect_rte_idx(rte_idx_set);
1457  if (rte_idx_set.size() > 1) {
1458  join_predicates.push_back(this);
1459  } else if (rte_idx_set.size() == 1) {
1460  scan_predicates.push_back(this);
1461  } else {
1462  const_predicates.push_back(this);
1463  }
1464 }
1465 
1466 InIntegerSet::InIntegerSet(const std::shared_ptr<const Analyzer::Expr> a,
1467  const std::vector<int64_t>& l,
1468  const bool not_null)
1469  : Expr(kBOOLEAN, not_null), arg(a), value_list(l) {}
1470 
1471 void CharLengthExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1472  std::list<const Expr*>& join_predicates,
1473  std::list<const Expr*>& const_predicates) const {
1474  std::set<int> rte_idx_set;
1475  arg->collect_rte_idx(rte_idx_set);
1476  if (rte_idx_set.size() > 1) {
1477  join_predicates.push_back(this);
1478  } else if (rte_idx_set.size() == 1) {
1479  scan_predicates.push_back(this);
1480  } else {
1481  const_predicates.push_back(this);
1482  }
1483 }
1484 
1485 void KeyForStringExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1486  std::list<const Expr*>& join_predicates,
1487  std::list<const Expr*>& const_predicates) const {
1488  std::set<int> rte_idx_set;
1489  arg->collect_rte_idx(rte_idx_set);
1490  if (rte_idx_set.size() > 1) {
1491  join_predicates.push_back(this);
1492  } else if (rte_idx_set.size() == 1) {
1493  scan_predicates.push_back(this);
1494  } else {
1495  const_predicates.push_back(this);
1496  }
1497 }
1498 
1499 void CardinalityExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1500  std::list<const Expr*>& join_predicates,
1501  std::list<const Expr*>& const_predicates) const {
1502  std::set<int> rte_idx_set;
1503  arg->collect_rte_idx(rte_idx_set);
1504  if (rte_idx_set.size() > 1) {
1505  join_predicates.push_back(this);
1506  } else if (rte_idx_set.size() == 1) {
1507  scan_predicates.push_back(this);
1508  } else {
1509  const_predicates.push_back(this);
1510  }
1511 }
1512 
1513 void LikeExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1514  std::list<const Expr*>& join_predicates,
1515  std::list<const Expr*>& const_predicates) const {
1516  std::set<int> rte_idx_set;
1517  arg->collect_rte_idx(rte_idx_set);
1518  if (rte_idx_set.size() > 1) {
1519  join_predicates.push_back(this);
1520  } else if (rte_idx_set.size() == 1) {
1521  scan_predicates.push_back(this);
1522  } else {
1523  const_predicates.push_back(this);
1524  }
1525 }
1526 
1527 void RegexpExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1528  std::list<const Expr*>& join_predicates,
1529  std::list<const Expr*>& const_predicates) const {
1530  std::set<int> rte_idx_set;
1531  arg->collect_rte_idx(rte_idx_set);
1532  if (rte_idx_set.size() > 1) {
1533  join_predicates.push_back(this);
1534  } else if (rte_idx_set.size() == 1) {
1535  scan_predicates.push_back(this);
1536  } else {
1537  const_predicates.push_back(this);
1538  }
1539 }
1540 
1541 void LikelihoodExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1542  std::list<const Expr*>& join_predicates,
1543  std::list<const Expr*>& const_predicates) const {
1544  std::set<int> rte_idx_set;
1545  arg->collect_rte_idx(rte_idx_set);
1546  if (rte_idx_set.size() > 1) {
1547  join_predicates.push_back(this);
1548  } else if (rte_idx_set.size() == 1) {
1549  scan_predicates.push_back(this);
1550  } else {
1551  const_predicates.push_back(this);
1552  }
1553 }
1554 
1555 void AggExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1556  std::list<const Expr*>& join_predicates,
1557  std::list<const Expr*>& const_predicates) const {
1558  std::set<int> rte_idx_set;
1559  arg->collect_rte_idx(rte_idx_set);
1560  if (rte_idx_set.size() > 1) {
1561  join_predicates.push_back(this);
1562  } else if (rte_idx_set.size() == 1) {
1563  scan_predicates.push_back(this);
1564  } else {
1565  const_predicates.push_back(this);
1566  }
1567 }
1568 
1569 void CaseExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1570  std::list<const Expr*>& join_predicates,
1571  std::list<const Expr*>& const_predicates) const {
1572  std::set<int> rte_idx_set;
1573  for (auto p : expr_pair_list) {
1574  p.first->collect_rte_idx(rte_idx_set);
1575  p.second->collect_rte_idx(rte_idx_set);
1576  }
1577  if (else_expr != nullptr) {
1578  else_expr->collect_rte_idx(rte_idx_set);
1579  }
1580  if (rte_idx_set.size() > 1) {
1581  join_predicates.push_back(this);
1582  } else if (rte_idx_set.size() == 1) {
1583  scan_predicates.push_back(this);
1584  } else {
1585  const_predicates.push_back(this);
1586  }
1587 }
1588 
1589 void ExtractExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1590  std::list<const Expr*>& join_predicates,
1591  std::list<const Expr*>& const_predicates) const {
1592  std::set<int> rte_idx_set;
1593  from_expr_->collect_rte_idx(rte_idx_set);
1594  if (rte_idx_set.size() > 1) {
1595  join_predicates.push_back(this);
1596  } else if (rte_idx_set.size() == 1) {
1597  scan_predicates.push_back(this);
1598  } else {
1599  const_predicates.push_back(this);
1600  }
1601 }
1602 
1603 void DateaddExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1604  std::list<const Expr*>& join_predicates,
1605  std::list<const Expr*>& const_predicates) const {
1606  std::set<int> rte_idx_set;
1607  number_->collect_rte_idx(rte_idx_set);
1608  datetime_->collect_rte_idx(rte_idx_set);
1609  if (rte_idx_set.size() > 1) {
1610  join_predicates.push_back(this);
1611  } else if (rte_idx_set.size() == 1) {
1612  scan_predicates.push_back(this);
1613  } else {
1614  const_predicates.push_back(this);
1615  }
1616 }
1617 
1618 void DatediffExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1619  std::list<const Expr*>& join_predicates,
1620  std::list<const Expr*>& const_predicates) const {
1621  std::set<int> rte_idx_set;
1622  start_->collect_rte_idx(rte_idx_set);
1623  end_->collect_rte_idx(rte_idx_set);
1624  if (rte_idx_set.size() > 1) {
1625  join_predicates.push_back(this);
1626  } else if (rte_idx_set.size() == 1) {
1627  scan_predicates.push_back(this);
1628  } else {
1629  const_predicates.push_back(this);
1630  }
1631 }
1632 
1633 void DatetruncExpr::group_predicates(std::list<const Expr*>& scan_predicates,
1634  std::list<const Expr*>& join_predicates,
1635  std::list<const Expr*>& const_predicates) const {
1636  std::set<int> rte_idx_set;
1637  from_expr_->collect_rte_idx(rte_idx_set);
1638  if (rte_idx_set.size() > 1) {
1639  join_predicates.push_back(this);
1640  } else if (rte_idx_set.size() == 1) {
1641  scan_predicates.push_back(this);
1642  } else {
1643  const_predicates.push_back(this);
1644  }
1645 }
1646 
1647 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_with_targetlist(
1648  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1649  for (auto tle : tlist) {
1650  const Expr* e = tle->get_expr();
1651  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
1652  if (colvar != nullptr) {
1653  if (table_id == colvar->get_table_id() && column_id == colvar->get_column_id()) {
1654  return colvar->deep_copy();
1655  }
1656  }
1657  }
1658  throw std::runtime_error("Internal error: cannot find ColumnVar in targetlist.");
1659 }
1660 
1661 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_with_child_targetlist(
1662  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1663  int varno = 1;
1664  for (auto tle : tlist) {
1665  const Expr* e = tle->get_expr();
1666  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
1667  if (colvar == nullptr) {
1668  throw std::runtime_error(
1669  "Internal Error: targetlist in rewrite_with_child_targetlist is not all "
1670  "columns.");
1671  }
1672  if (table_id == colvar->get_table_id() && column_id == colvar->get_column_id()) {
1673  return makeExpr<Var>(colvar->get_type_info(),
1674  colvar->get_table_id(),
1675  colvar->get_column_id(),
1676  colvar->get_rte_idx(),
1678  varno);
1679  }
1680  varno++;
1681  }
1682  throw std::runtime_error("Internal error: cannot find ColumnVar in child targetlist.");
1683 }
1684 
1685 std::shared_ptr<Analyzer::Expr> ColumnVar::rewrite_agg_to_var(
1686  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1687  int varno = 1;
1688  for (auto tle : tlist) {
1689  const Expr* e = tle->get_expr();
1690  if (typeid(*e) != typeid(AggExpr)) {
1691  const ColumnVar* colvar = dynamic_cast<const ColumnVar*>(e);
1692  if (colvar == nullptr) {
1693  throw std::runtime_error(
1694  "Internal Error: targetlist in rewrite_agg_to_var is not all columns and "
1695  "aggregates.");
1696  }
1697  if (table_id == colvar->get_table_id() && column_id == colvar->get_column_id()) {
1698  return makeExpr<Var>(colvar->get_type_info(),
1699  colvar->get_table_id(),
1700  colvar->get_column_id(),
1701  colvar->get_rte_idx(),
1703  varno);
1704  }
1705  }
1706  varno++;
1707  }
1708  throw std::runtime_error(
1709  "Internal error: cannot find ColumnVar from having clause in targetlist.");
1710 }
1711 
1712 std::shared_ptr<Analyzer::Expr> Var::rewrite_agg_to_var(
1713  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1714  int varno = 1;
1715  for (auto tle : tlist) {
1716  const Expr* e = tle->get_expr();
1717  if (*e == *this) {
1718  return makeExpr<Var>(e->get_type_info(), Var::kINPUT_OUTER, varno);
1719  }
1720  varno++;
1721  }
1722  throw std::runtime_error(
1723  "Internal error: cannot find Var from having clause in targetlist.");
1724 }
1725 
1726 std::shared_ptr<Analyzer::Expr> InValues::rewrite_with_targetlist(
1727  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1728  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
1729  for (auto v : value_list) {
1730  new_value_list.push_back(v->deep_copy());
1731  }
1732  return makeExpr<InValues>(arg->rewrite_with_targetlist(tlist), new_value_list);
1733 }
1734 
1735 std::shared_ptr<Analyzer::Expr> InValues::rewrite_with_child_targetlist(
1736  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1737  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
1738  for (auto v : value_list) {
1739  new_value_list.push_back(v->deep_copy());
1740  }
1741  return makeExpr<InValues>(arg->rewrite_with_child_targetlist(tlist), new_value_list);
1742 }
1743 
1744 std::shared_ptr<Analyzer::Expr> InValues::rewrite_agg_to_var(
1745  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1746  std::list<std::shared_ptr<Analyzer::Expr>> new_value_list;
1747  for (auto v : value_list) {
1748  new_value_list.push_back(v->rewrite_agg_to_var(tlist));
1749  }
1750  return makeExpr<InValues>(arg->rewrite_agg_to_var(tlist), new_value_list);
1751 }
1752 
1753 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_with_targetlist(
1754  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1755  for (auto tle : tlist) {
1756  const Expr* e = tle->get_expr();
1757  if (typeid(*e) == typeid(AggExpr)) {
1758  const AggExpr* agg = dynamic_cast<const AggExpr*>(e);
1759  if (*this == *agg) {
1760  return agg->deep_copy();
1761  }
1762  }
1763  }
1764  throw std::runtime_error("Internal error: cannot find AggExpr in targetlist.");
1765 }
1766 
1767 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_with_child_targetlist(
1768  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1769  return makeExpr<AggExpr>(type_info,
1770  aggtype,
1771  arg ? arg->rewrite_with_child_targetlist(tlist) : nullptr,
1772  is_distinct,
1773  error_rate);
1774 }
1775 
1776 std::shared_ptr<Analyzer::Expr> AggExpr::rewrite_agg_to_var(
1777  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1778  int varno = 1;
1779  for (auto tle : tlist) {
1780  const Expr* e = tle->get_expr();
1781  if (typeid(*e) == typeid(AggExpr)) {
1782  const AggExpr* agg_expr = dynamic_cast<const AggExpr*>(e);
1783  if (*this == *agg_expr) {
1784  return makeExpr<Var>(agg_expr->get_type_info(), Var::kINPUT_OUTER, varno);
1785  }
1786  }
1787  varno++;
1788  }
1789  throw std::runtime_error(
1790  "Internal error: cannot find AggExpr from having clause in targetlist.");
1791 }
1792 
1793 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_with_targetlist(
1794  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1795  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1796  epair_list;
1797  for (auto p : expr_pair_list) {
1798  epair_list.emplace_back(p.first->rewrite_with_targetlist(tlist),
1799  p.second->rewrite_with_targetlist(tlist));
1800  }
1801  return makeExpr<CaseExpr>(
1802  type_info,
1803  contains_agg,
1804  epair_list,
1805  else_expr ? else_expr->rewrite_with_targetlist(tlist) : nullptr);
1806 }
1807 
1808 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_with_targetlist(
1809  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1810  return makeExpr<ExtractExpr>(
1811  type_info, contains_agg, field_, from_expr_->rewrite_with_targetlist(tlist));
1812 }
1813 
1814 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_with_targetlist(
1815  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1816  return makeExpr<DateaddExpr>(type_info,
1817  field_,
1818  number_->rewrite_with_targetlist(tlist),
1819  datetime_->rewrite_with_targetlist(tlist));
1820 }
1821 
1822 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_with_targetlist(
1823  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1824  return makeExpr<DatediffExpr>(type_info,
1825  field_,
1826  start_->rewrite_with_targetlist(tlist),
1827  end_->rewrite_with_targetlist(tlist));
1828 }
1829 
1830 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_with_targetlist(
1831  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1832  return makeExpr<DatetruncExpr>(
1833  type_info, contains_agg, field_, from_expr_->rewrite_with_targetlist(tlist));
1834 }
1835 
1836 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_with_child_targetlist(
1837  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1838  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1839  epair_list;
1840  for (auto p : expr_pair_list) {
1841  epair_list.emplace_back(p.first->rewrite_with_child_targetlist(tlist),
1842  p.second->rewrite_with_child_targetlist(tlist));
1843  }
1844  return makeExpr<CaseExpr>(
1845  type_info,
1846  contains_agg,
1847  epair_list,
1848  else_expr ? else_expr->rewrite_with_child_targetlist(tlist) : nullptr);
1849 }
1850 
1851 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_with_child_targetlist(
1852  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1853  return makeExpr<ExtractExpr>(
1854  type_info, contains_agg, field_, from_expr_->rewrite_with_child_targetlist(tlist));
1855 }
1856 
1857 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_with_child_targetlist(
1858  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1859  return makeExpr<DateaddExpr>(type_info,
1860  field_,
1861  number_->rewrite_with_child_targetlist(tlist),
1862  datetime_->rewrite_with_child_targetlist(tlist));
1863 }
1864 
1865 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_with_child_targetlist(
1866  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1867  return makeExpr<DatediffExpr>(type_info,
1868  field_,
1869  start_->rewrite_with_child_targetlist(tlist),
1870  end_->rewrite_with_child_targetlist(tlist));
1871 }
1872 
1873 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_with_child_targetlist(
1874  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1875  return makeExpr<DatetruncExpr>(
1876  type_info, contains_agg, field_, from_expr_->rewrite_with_child_targetlist(tlist));
1877 }
1878 
1879 std::shared_ptr<Analyzer::Expr> CaseExpr::rewrite_agg_to_var(
1880  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1881  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
1882  epair_list;
1883  for (auto p : expr_pair_list) {
1884  epair_list.emplace_back(p.first->rewrite_agg_to_var(tlist),
1885  p.second->rewrite_agg_to_var(tlist));
1886  }
1887  return makeExpr<CaseExpr>(type_info,
1888  contains_agg,
1889  epair_list,
1890  else_expr ? else_expr->rewrite_agg_to_var(tlist) : nullptr);
1891 }
1892 
1893 std::shared_ptr<Analyzer::Expr> ExtractExpr::rewrite_agg_to_var(
1894  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1895  return makeExpr<ExtractExpr>(
1896  type_info, contains_agg, field_, from_expr_->rewrite_agg_to_var(tlist));
1897 }
1898 
1899 std::shared_ptr<Analyzer::Expr> DateaddExpr::rewrite_agg_to_var(
1900  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1901  return makeExpr<DateaddExpr>(type_info,
1902  field_,
1903  number_->rewrite_agg_to_var(tlist),
1904  datetime_->rewrite_agg_to_var(tlist));
1905 }
1906 
1907 std::shared_ptr<Analyzer::Expr> DatediffExpr::rewrite_agg_to_var(
1908  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1909  return makeExpr<DatediffExpr>(type_info,
1910  field_,
1911  start_->rewrite_agg_to_var(tlist),
1912  end_->rewrite_agg_to_var(tlist));
1913 }
1914 
1915 std::shared_ptr<Analyzer::Expr> DatetruncExpr::rewrite_agg_to_var(
1916  const std::vector<std::shared_ptr<TargetEntry>>& tlist) const {
1917  return makeExpr<DatetruncExpr>(
1918  type_info, contains_agg, field_, from_expr_->rewrite_agg_to_var(tlist));
1919 }
1920 
1921 bool ColumnVar::operator==(const Expr& rhs) const {
1922  if (typeid(rhs) != typeid(ColumnVar) && typeid(rhs) != typeid(Var)) {
1923  return false;
1924  }
1925  const ColumnVar& rhs_cv = dynamic_cast<const ColumnVar&>(rhs);
1926  if (rte_idx != -1) {
1927  return (table_id == rhs_cv.get_table_id()) && (column_id == rhs_cv.get_column_id()) &&
1928  (rte_idx == rhs_cv.get_rte_idx());
1929  }
1930  const Var* v = dynamic_cast<const Var*>(this);
1931  if (v == nullptr) {
1932  return false;
1933  }
1934  const Var* rv = dynamic_cast<const Var*>(&rhs);
1935  if (rv == nullptr) {
1936  return false;
1937  }
1938  return (v->get_which_row() == rv->get_which_row()) &&
1939  (v->get_varno() == rv->get_varno());
1940 }
1941 
1942 bool ExpressionTuple::operator==(const Expr& rhs) const {
1943  const auto rhs_tuple = dynamic_cast<const ExpressionTuple*>(&rhs);
1944  if (!rhs_tuple) {
1945  return false;
1946  }
1947  const auto& rhs_tuple_cols = rhs_tuple->getTuple();
1948  return expr_list_match(tuple_, rhs_tuple_cols);
1949 }
1950 
1951 bool Datum_equal(const SQLTypeInfo& ti, Datum val1, Datum val2) {
1952  switch (ti.get_type()) {
1953  case kBOOLEAN:
1954  return val1.boolval == val2.boolval;
1955  case kCHAR:
1956  case kVARCHAR:
1957  case kTEXT:
1958  return *val1.stringval == *val2.stringval;
1959  case kNUMERIC:
1960  case kDECIMAL:
1961  case kBIGINT:
1962  return val1.bigintval == val2.bigintval;
1963  case kINT:
1964  return val1.intval == val2.intval;
1965  case kSMALLINT:
1966  return val1.smallintval == val2.smallintval;
1967  case kTINYINT:
1968  return val1.tinyintval == val2.tinyintval;
1969  case kFLOAT:
1970  return val1.floatval == val2.floatval;
1971  case kDOUBLE:
1972  return val1.doubleval == val2.doubleval;
1973  case kTIME:
1974  case kTIMESTAMP:
1975  case kDATE:
1976  case kINTERVAL_DAY_TIME:
1977  case kINTERVAL_YEAR_MONTH:
1978  return val1.bigintval == val2.bigintval;
1979  case kPOINT:
1980  case kLINESTRING:
1981  case kPOLYGON:
1982  case kMULTIPOLYGON:
1983  return *val1.stringval == *val2.stringval;
1984  default:
1985  throw std::runtime_error("Unrecognized type for Constant Datum equality: " +
1986  ti.get_type_name());
1987  }
1988  UNREACHABLE();
1989  return false;
1990 }
1991 
1992 bool Constant::operator==(const Expr& rhs) const {
1993  if (typeid(rhs) != typeid(Constant)) {
1994  return false;
1995  }
1996  const Constant& rhs_c = dynamic_cast<const Constant&>(rhs);
1997  if (type_info != rhs_c.get_type_info() || is_null != rhs_c.get_is_null()) {
1998  return false;
1999  }
2000  if (is_null && rhs_c.get_is_null()) {
2001  return true;
2002  }
2003  if (type_info.is_array()) {
2004  return false;
2005  }
2006  return Datum_equal(type_info, constval, rhs_c.get_constval());
2007 }
2008 
2009 bool UOper::operator==(const Expr& rhs) const {
2010  if (typeid(rhs) != typeid(UOper)) {
2011  return false;
2012  }
2013  const UOper& rhs_uo = dynamic_cast<const UOper&>(rhs);
2014  return optype == rhs_uo.get_optype() && *operand == *rhs_uo.get_operand();
2015 }
2016 
2017 bool BinOper::operator==(const Expr& rhs) const {
2018  if (typeid(rhs) != typeid(BinOper)) {
2019  return false;
2020  }
2021  const BinOper& rhs_bo = dynamic_cast<const BinOper&>(rhs);
2022  return optype == rhs_bo.get_optype() && *left_operand == *rhs_bo.get_left_operand() &&
2023  *right_operand == *rhs_bo.get_right_operand();
2024 }
2025 
2026 bool CharLengthExpr::operator==(const Expr& rhs) const {
2027  if (typeid(rhs) != typeid(CharLengthExpr)) {
2028  return false;
2029  }
2030  const CharLengthExpr& rhs_cl = dynamic_cast<const CharLengthExpr&>(rhs);
2031  if (!(*arg == *rhs_cl.get_arg()) ||
2032  calc_encoded_length != rhs_cl.get_calc_encoded_length()) {
2033  return false;
2034  }
2035  return true;
2036 }
2037 
2038 bool KeyForStringExpr::operator==(const Expr& rhs) const {
2039  if (typeid(rhs) != typeid(KeyForStringExpr)) {
2040  return false;
2041  }
2042  const KeyForStringExpr& rhs_cl = dynamic_cast<const KeyForStringExpr&>(rhs);
2043  if (!(*arg == *rhs_cl.get_arg())) {
2044  return false;
2045  }
2046  return true;
2047 }
2048 
2049 bool CardinalityExpr::operator==(const Expr& rhs) const {
2050  if (typeid(rhs) != typeid(CardinalityExpr)) {
2051  return false;
2052  }
2053  const CardinalityExpr& rhs_ca = dynamic_cast<const CardinalityExpr&>(rhs);
2054  if (!(*arg == *rhs_ca.get_arg())) {
2055  return false;
2056  }
2057  return true;
2058 }
2059 
2060 bool LikeExpr::operator==(const Expr& rhs) const {
2061  if (typeid(rhs) != typeid(LikeExpr)) {
2062  return false;
2063  }
2064  const LikeExpr& rhs_lk = dynamic_cast<const LikeExpr&>(rhs);
2065  if (!(*arg == *rhs_lk.get_arg()) || !(*like_expr == *rhs_lk.get_like_expr()) ||
2066  is_ilike != rhs_lk.get_is_ilike()) {
2067  return false;
2068  }
2069  if (escape_expr.get() == rhs_lk.get_escape_expr()) {
2070  return true;
2071  }
2072  if (escape_expr != nullptr && rhs_lk.get_escape_expr() != nullptr &&
2073  *escape_expr == *rhs_lk.get_escape_expr()) {
2074  return true;
2075  }
2076  return false;
2077 }
2078 
2079 bool RegexpExpr::operator==(const Expr& rhs) const {
2080  if (typeid(rhs) != typeid(RegexpExpr)) {
2081  return false;
2082  }
2083  const RegexpExpr& rhs_re = dynamic_cast<const RegexpExpr&>(rhs);
2084  if (!(*arg == *rhs_re.get_arg()) || !(*pattern_expr == *rhs_re.get_pattern_expr())) {
2085  return false;
2086  }
2087  if (escape_expr.get() == rhs_re.get_escape_expr()) {
2088  return true;
2089  }
2090  if (escape_expr != nullptr && rhs_re.get_escape_expr() != nullptr &&
2091  *escape_expr == *rhs_re.get_escape_expr()) {
2092  return true;
2093  }
2094  return false;
2095 }
2096 
2097 bool LikelihoodExpr::operator==(const Expr& rhs) const {
2098  if (typeid(rhs) != typeid(LikelihoodExpr)) {
2099  return false;
2100  }
2101  const LikelihoodExpr& rhs_l = dynamic_cast<const LikelihoodExpr&>(rhs);
2102  if (!(*arg == *rhs_l.get_arg())) {
2103  return false;
2104  }
2105  if (likelihood != rhs_l.get_likelihood()) {
2106  return false;
2107  }
2108  return true;
2109 }
2110 
2111 bool InValues::operator==(const Expr& rhs) const {
2112  if (typeid(rhs) != typeid(InValues)) {
2113  return false;
2114  }
2115  const InValues& rhs_iv = dynamic_cast<const InValues&>(rhs);
2116  if (!(*arg == *rhs_iv.get_arg())) {
2117  return false;
2118  }
2119  if (value_list.size() != rhs_iv.get_value_list().size()) {
2120  return false;
2121  }
2122  auto q = rhs_iv.get_value_list().begin();
2123  for (auto p : value_list) {
2124  if (!(*p == **q)) {
2125  return false;
2126  }
2127  q++;
2128  }
2129  return true;
2130 }
2131 
2132 bool AggExpr::operator==(const Expr& rhs) const {
2133  if (typeid(rhs) != typeid(AggExpr)) {
2134  return false;
2135  }
2136  const AggExpr& rhs_ae = dynamic_cast<const AggExpr&>(rhs);
2137  if (aggtype != rhs_ae.get_aggtype() || is_distinct != rhs_ae.get_is_distinct()) {
2138  return false;
2139  }
2140  if (arg.get() == rhs_ae.get_arg()) {
2141  return true;
2142  }
2143  if (arg == nullptr || rhs_ae.get_arg() == nullptr) {
2144  return false;
2145  }
2146  return *arg == *rhs_ae.get_arg();
2147 }
2148 
2149 bool CaseExpr::operator==(const Expr& rhs) const {
2150  if (typeid(rhs) != typeid(CaseExpr)) {
2151  return false;
2152  }
2153  const CaseExpr& rhs_ce = dynamic_cast<const CaseExpr&>(rhs);
2154  if (expr_pair_list.size() != rhs_ce.get_expr_pair_list().size()) {
2155  return false;
2156  }
2157  if ((else_expr == nullptr && rhs_ce.get_else_expr() != nullptr) ||
2158  (else_expr != nullptr && rhs_ce.get_else_expr() == nullptr)) {
2159  return false;
2160  }
2161  auto it = rhs_ce.get_expr_pair_list().cbegin();
2162  for (auto p : expr_pair_list) {
2163  if (!(*p.first == *it->first) || !(*p.second == *it->second)) {
2164  return false;
2165  }
2166  ++it;
2167  }
2168  return else_expr == nullptr ||
2169  (else_expr != nullptr && *else_expr == *rhs_ce.get_else_expr());
2170 }
2171 
2172 bool ExtractExpr::operator==(const Expr& rhs) const {
2173  if (typeid(rhs) != typeid(ExtractExpr)) {
2174  return false;
2175  }
2176  const ExtractExpr& rhs_ee = dynamic_cast<const ExtractExpr&>(rhs);
2177  return field_ == rhs_ee.get_field() && *from_expr_ == *rhs_ee.get_from_expr();
2178 }
2179 
2180 bool DateaddExpr::operator==(const Expr& rhs) const {
2181  if (typeid(rhs) != typeid(DateaddExpr)) {
2182  return false;
2183  }
2184  const DateaddExpr& rhs_ee = dynamic_cast<const DateaddExpr&>(rhs);
2185  return field_ == rhs_ee.get_field() && *number_ == *rhs_ee.get_number_expr() &&
2186  *datetime_ == *rhs_ee.get_datetime_expr();
2187 }
2188 
2189 bool DatediffExpr::operator==(const Expr& rhs) const {
2190  if (typeid(rhs) != typeid(DatediffExpr)) {
2191  return false;
2192  }
2193  const DatediffExpr& rhs_ee = dynamic_cast<const DatediffExpr&>(rhs);
2194  return field_ == rhs_ee.get_field() && *start_ == *rhs_ee.get_start_expr() &&
2195  *end_ == *rhs_ee.get_end_expr();
2196 }
2197 
2198 bool DatetruncExpr::operator==(const Expr& rhs) const {
2199  if (typeid(rhs) != typeid(DatetruncExpr)) {
2200  return false;
2201  }
2202  const DatetruncExpr& rhs_ee = dynamic_cast<const DatetruncExpr&>(rhs);
2203  return field_ == rhs_ee.get_field() && *from_expr_ == *rhs_ee.get_from_expr();
2204 }
2205 
2206 bool OffsetInFragment::operator==(const Expr& rhs) const {
2207  return typeid(rhs) == typeid(OffsetInFragment);
2208 }
2209 
2210 bool WindowFunction::operator==(const Expr& rhs) const {
2211  const auto rhs_window = dynamic_cast<const WindowFunction*>(&rhs);
2212  if (!rhs_window) {
2213  return false;
2214  }
2215  if (kind_ != rhs_window->kind_ || args_.size() != rhs_window->args_.size() ||
2216  partition_keys_.size() != rhs_window->partition_keys_.size() ||
2217  order_keys_.size() != rhs_window->order_keys_.size()) {
2218  return false;
2219  }
2220  return expr_list_match(args_, rhs_window->args_) &&
2221  expr_list_match(partition_keys_, rhs_window->partition_keys_) &&
2222  expr_list_match(order_keys_, rhs_window->order_keys_);
2223 }
2224 
2225 bool ArrayExpr::operator==(Expr const& rhs) const {
2226  if (typeid(rhs) != typeid(ArrayExpr)) {
2227  return false;
2228  }
2229  ArrayExpr const& casted_rhs = static_cast<ArrayExpr const&>(rhs);
2230  for (unsigned i = 0; i < contained_expressions_.size(); i++) {
2231  auto& lhs_expr = contained_expressions_[i];
2232  auto& rhs_expr = casted_rhs.contained_expressions_[i];
2233  if (!(lhs_expr == rhs_expr)) {
2234  return false;
2235  }
2236  }
2237  return true;
2238 }
2239 
2240 std::string ColumnVar::toString() const {
2241  return "(ColumnVar table: " + std::to_string(table_id) +
2242  " column: " + std::to_string(column_id) + " rte: " + std::to_string(rte_idx) +
2243  " " + get_type_info().get_type_name() + ") ";
2244 }
2245 
2246 std::string ExpressionTuple::toString() const {
2247  std::string str{"< "};
2248  for (const auto& column : tuple_) {
2249  str += column->toString();
2250  }
2251  str += "> ";
2252  return str;
2253 }
2254 
2255 std::string Var::toString() const {
2256  return "(Var table: " + std::to_string(table_id) +
2257  " column: " + std::to_string(column_id) + " rte: " + std::to_string(rte_idx) +
2258  " which_row: " + std::to_string(which_row) + " varno: " + std::to_string(varno) +
2259  ") ";
2260 }
2261 
2262 std::string Constant::toString() const {
2263  std::string str{"(Const "};
2264  if (is_null) {
2265  str += "NULL";
2266  } else if (type_info.is_array()) {
2267  const auto& elem_ti = type_info.get_elem_type();
2268  str += sql_type_to_str(type_info.get_type()) + ": " +
2269  sql_type_to_str(elem_ti.get_type());
2270  } else {
2271  str += DatumToString(constval, type_info);
2272  }
2273  str += ") ";
2274  return str;
2275 }
2276 
2277 std::string UOper::toString() const {
2278  std::string op;
2279  switch (optype) {
2280  case kNOT:
2281  op = "NOT ";
2282  break;
2283  case kUMINUS:
2284  op = "- ";
2285  break;
2286  case kISNULL:
2287  op = "IS NULL ";
2288  break;
2289  case kEXISTS:
2290  op = "EXISTS ";
2291  break;
2292  case kCAST:
2293  op = "CAST " + type_info.get_type_name() + "(" +
2295  std::to_string(type_info.get_scale()) + ") " +
2298  break;
2299  case kUNNEST:
2300  op = "UNNEST ";
2301  break;
2302  default:
2303  break;
2304  }
2305  return "(" + op + operand->toString() + ") ";
2306 }
2307 
2308 std::string BinOper::toString() const {
2309  std::string op;
2310  switch (optype) {
2311  case kEQ:
2312  op = "= ";
2313  break;
2314  case kNE:
2315  op = "<> ";
2316  break;
2317  case kLT:
2318  op = "< ";
2319  break;
2320  case kLE:
2321  op = "<= ";
2322  break;
2323  case kGT:
2324  op = "> ";
2325  break;
2326  case kGE:
2327  op = ">= ";
2328  break;
2329  case kAND:
2330  op = "AND ";
2331  break;
2332  case kOR:
2333  op = "OR ";
2334  break;
2335  case kMINUS:
2336  op = "- ";
2337  break;
2338  case kPLUS:
2339  op = "+ ";
2340  break;
2341  case kMULTIPLY:
2342  op = "* ";
2343  break;
2344  case kDIVIDE:
2345  op = "/ ";
2346  break;
2347  case kMODULO:
2348  op = "% ";
2349  break;
2350  case kARRAY_AT:
2351  op = "[] ";
2352  break;
2353  case kOVERLAPS:
2354  op = "OVERLAPS ";
2355  break;
2356  default:
2357  break;
2358  }
2359  std::string str{"("};
2360  str += op;
2361  if (qualifier == kANY) {
2362  str += "ANY ";
2363  } else if (qualifier == kALL) {
2364  str += "ALL ";
2365  }
2366  str += left_operand->toString();
2367  str += right_operand->toString();
2368  str += ") ";
2369  return str;
2370 }
2371 
2372 std::string Subquery::toString() const {
2373  return "(Subquery ) ";
2374 }
2375 
2376 std::string InValues::toString() const {
2377  std::string str{"(IN "};
2378  str += arg->toString();
2379  str += "(";
2380  for (auto e : value_list) {
2381  str += e->toString();
2382  }
2383  str += ") ";
2384  return str;
2385 }
2386 
2387 std::shared_ptr<Analyzer::Expr> InIntegerSet::deep_copy() const {
2388  return std::make_shared<InIntegerSet>(arg, value_list, get_type_info().get_notnull());
2389 }
2390 
2391 bool InIntegerSet::operator==(const Expr& rhs) const {
2392  if (!dynamic_cast<const InIntegerSet*>(&rhs)) {
2393  return false;
2394  }
2395  const auto& rhs_in_integer_set = static_cast<const InIntegerSet&>(rhs);
2396  return *arg == *rhs_in_integer_set.arg && value_list == rhs_in_integer_set.value_list;
2397 }
2398 
2399 std::string InIntegerSet::toString() const {
2400  std::string str{"(IN_INTEGER_SET "};
2401  str += arg->toString();
2402  str += "( ";
2403  for (const auto e : value_list) {
2404  str += std::to_string(e) + " ";
2405  }
2406  str += ") ";
2407  return str;
2408 }
2409 
2410 std::string CharLengthExpr::toString() const {
2411  std::string str;
2412  if (calc_encoded_length) {
2413  str += "CHAR_LENGTH(";
2414  } else {
2415  str += "LENGTH(";
2416  }
2417  str += arg->toString();
2418  str += ") ";
2419  return str;
2420 }
2421 
2422 std::string KeyForStringExpr::toString() const {
2423  std::string str{"KEY_FOR_STRING("};
2424  str += arg->toString();
2425  str += ") ";
2426  return str;
2427 }
2428 
2429 std::string CardinalityExpr::toString() const {
2430  std::string str{"CARDINALITY("};
2431  str += arg->toString();
2432  str += ") ";
2433  return str;
2434 }
2435 
2436 std::string LikeExpr::toString() const {
2437  std::string str{"(LIKE "};
2438  str += arg->toString();
2439  str += like_expr->toString();
2440  if (escape_expr) {
2441  str += escape_expr->toString();
2442  }
2443  str += ") ";
2444  return str;
2445 }
2446 
2447 std::string RegexpExpr::toString() const {
2448  std::string str{"(REGEXP "};
2449  str += arg->toString();
2450  str += pattern_expr->toString();
2451  if (escape_expr) {
2452  str += escape_expr->toString();
2453  }
2454  str += ") ";
2455  return str;
2456 }
2457 
2458 std::string LikelihoodExpr::toString() const {
2459  std::string str{"(LIKELIHOOD "};
2460  str += arg->toString();
2461  return str + " " + std::to_string(likelihood) + ") ";
2462 }
2463 
2464 std::string AggExpr::toString() const {
2465  std::string agg;
2466  switch (aggtype) {
2467  case kAVG:
2468  agg = "AVG ";
2469  break;
2470  case kMIN:
2471  agg = "MIN ";
2472  break;
2473  case kMAX:
2474  agg = "MAX ";
2475  break;
2476  case kSUM:
2477  agg = "SUM ";
2478  break;
2479  case kCOUNT:
2480  agg = "COUNT ";
2481  break;
2483  agg = "APPROX_COUNT_DISTINCT";
2484  break;
2485  case kSAMPLE:
2486  agg = "SAMPLE";
2487  break;
2488  }
2489  std::string str{"(" + agg};
2490  if (is_distinct) {
2491  str += "DISTINCT ";
2492  }
2493  if (arg) {
2494  str += arg->toString();
2495  } else {
2496  str += "*";
2497  }
2498  return str + ") ";
2499 }
2500 
2501 std::string CaseExpr::toString() const {
2502  std::string str{"CASE "};
2503  for (auto p : expr_pair_list) {
2504  str += "(";
2505  str += p.first->toString();
2506  str += ", ";
2507  str += p.second->toString();
2508  str += ") ";
2509  }
2510  if (else_expr) {
2511  str += "ELSE ";
2512  str += else_expr->toString();
2513  }
2514  str += " END ";
2515  return str;
2516 }
2517 
2518 std::string ExtractExpr::toString() const {
2519  return "EXTRACT(" + std::to_string(field_) + " FROM " + from_expr_->toString() + ") ";
2520 }
2521 
2522 std::string DateaddExpr::toString() const {
2523  return "DATEADD(" + std::to_string(field_) + " NUMBER " + number_->toString() +
2524  " DATETIME " + datetime_->toString() + ") ";
2525 }
2526 
2527 std::string DatediffExpr::toString() const {
2528  return "DATEDIFF(" + std::to_string(field_) + " START " + start_->toString() + " END " +
2529  end_->toString() + ") ";
2530 }
2531 
2532 std::string DatetruncExpr::toString() const {
2533  return "DATE_TRUNC(" + std::to_string(field_) + " , " + from_expr_->toString() + ") ";
2534 }
2535 
2536 std::string OffsetInFragment::toString() const {
2537  return "(OffsetInFragment) ";
2538 }
2539 
2540 std::string WindowFunction::toString() const {
2541  std::string result = "WindowFunction(" + sql_window_function_to_str(kind_);
2542  for (const auto& arg : args_) {
2543  result += " " + arg->toString();
2544  }
2545  return result + ") ";
2546 }
2547 
2548 std::string ArrayExpr::toString() const {
2549  std::string str{"ARRAY["};
2550 
2551  auto iter(contained_expressions_.begin());
2552  while (iter != contained_expressions_.end()) {
2553  str += (*iter)->toString();
2554  if (iter + 1 != contained_expressions_.end()) {
2555  str += ", ";
2556  }
2557  iter++;
2558  }
2559  str += "]";
2560  return str;
2561 }
2562 
2563 std::string TargetEntry::toString() const {
2564  std::string str{"(" + resname + " "};
2565  str += expr->toString();
2566  if (unnest) {
2567  str += " UNNEST";
2568  }
2569  str += ") ";
2570  return str;
2571 }
2572 
2573 std::string OrderEntry::toString() const {
2574  std::string str{std::to_string(tle_no)};
2575  if (is_desc) {
2576  str += " desc";
2577  }
2578  if (nulls_first) {
2579  str += " nulls first";
2580  }
2581  str += " ";
2582  return str;
2583 }
2584 
2585 void Expr::add_unique(std::list<const Expr*>& expr_list) const {
2586  // only add unique instances to the list
2587  for (auto e : expr_list) {
2588  if (*e == *this) {
2589  return;
2590  }
2591  }
2592  expr_list.push_back(this);
2593 }
2594 
2595 void BinOper::find_expr(bool (*f)(const Expr*), std::list<const Expr*>& expr_list) const {
2596  if (f(this)) {
2597  add_unique(expr_list);
2598  return;
2599  }
2600  left_operand->find_expr(f, expr_list);
2601  right_operand->find_expr(f, expr_list);
2602 }
2603 
2604 void UOper::find_expr(bool (*f)(const Expr*), std::list<const Expr*>& expr_list) const {
2605  if (f(this)) {
2606  add_unique(expr_list);
2607  return;
2608  }
2609  operand->find_expr(f, expr_list);
2610 }
2611 
2612 void InValues::find_expr(bool (*f)(const Expr*),
2613  std::list<const Expr*>& expr_list) const {
2614  if (f(this)) {
2615  add_unique(expr_list);
2616  return;
2617  }
2618  arg->find_expr(f, expr_list);
2619  for (auto e : value_list) {
2620  e->find_expr(f, expr_list);
2621  }
2622 }
2623 
2624 void CharLengthExpr::find_expr(bool (*f)(const Expr*),
2625  std::list<const Expr*>& expr_list) const {
2626  if (f(this)) {
2627  add_unique(expr_list);
2628  return;
2629  }
2630  arg->find_expr(f, expr_list);
2631 }
2632 
2633 void KeyForStringExpr::find_expr(bool (*f)(const Expr*),
2634  std::list<const Expr*>& expr_list) const {
2635  if (f(this)) {
2636  add_unique(expr_list);
2637  return;
2638  }
2639  arg->find_expr(f, expr_list);
2640 }
2641 
2642 void CardinalityExpr::find_expr(bool (*f)(const Expr*),
2643  std::list<const Expr*>& expr_list) const {
2644  if (f(this)) {
2645  add_unique(expr_list);
2646  return;
2647  }
2648  arg->find_expr(f, expr_list);
2649 }
2650 
2651 void LikeExpr::find_expr(bool (*f)(const Expr*),
2652  std::list<const Expr*>& expr_list) const {
2653  if (f(this)) {
2654  add_unique(expr_list);
2655  return;
2656  }
2657  arg->find_expr(f, expr_list);
2658  like_expr->find_expr(f, expr_list);
2659  if (escape_expr != nullptr) {
2660  escape_expr->find_expr(f, expr_list);
2661  }
2662 }
2663 
2664 void RegexpExpr::find_expr(bool (*f)(const Expr*),
2665  std::list<const Expr*>& expr_list) const {
2666  if (f(this)) {
2667  add_unique(expr_list);
2668  return;
2669  }
2670  arg->find_expr(f, expr_list);
2671  pattern_expr->find_expr(f, expr_list);
2672  if (escape_expr != nullptr) {
2673  escape_expr->find_expr(f, expr_list);
2674  }
2675 }
2676 
2677 void LikelihoodExpr::find_expr(bool (*f)(const Expr*),
2678  std::list<const Expr*>& expr_list) const {
2679  if (f(this)) {
2680  add_unique(expr_list);
2681  return;
2682  }
2683  arg->find_expr(f, expr_list);
2684 }
2685 
2686 void AggExpr::find_expr(bool (*f)(const Expr*), std::list<const Expr*>& expr_list) const {
2687  if (f(this)) {
2688  add_unique(expr_list);
2689  return;
2690  }
2691  if (arg != nullptr) {
2692  arg->find_expr(f, expr_list);
2693  }
2694 }
2695 
2696 void CaseExpr::find_expr(bool (*f)(const Expr*),
2697  std::list<const Expr*>& expr_list) const {
2698  if (f(this)) {
2699  add_unique(expr_list);
2700  return;
2701  }
2702  for (auto p : expr_pair_list) {
2703  p.first->find_expr(f, expr_list);
2704  p.second->find_expr(f, expr_list);
2705  }
2706  if (else_expr != nullptr) {
2707  else_expr->find_expr(f, expr_list);
2708  }
2709 }
2710 
2711 void ExtractExpr::find_expr(bool (*f)(const Expr*),
2712  std::list<const Expr*>& expr_list) const {
2713  if (f(this)) {
2714  add_unique(expr_list);
2715  return;
2716  }
2717  from_expr_->find_expr(f, expr_list);
2718 }
2719 
2720 void DateaddExpr::find_expr(bool (*f)(const Expr*),
2721  std::list<const Expr*>& expr_list) const {
2722  if (f(this)) {
2723  add_unique(expr_list);
2724  return;
2725  }
2726  number_->find_expr(f, expr_list);
2727  datetime_->find_expr(f, expr_list);
2728 }
2729 
2730 void DatediffExpr::find_expr(bool (*f)(const Expr*),
2731  std::list<const Expr*>& expr_list) const {
2732  if (f(this)) {
2733  add_unique(expr_list);
2734  return;
2735  }
2736  start_->find_expr(f, expr_list);
2737  end_->find_expr(f, expr_list);
2738 }
2739 
2740 void DatetruncExpr::find_expr(bool (*f)(const Expr*),
2741  std::list<const Expr*>& expr_list) const {
2742  if (f(this)) {
2743  add_unique(expr_list);
2744  return;
2745  }
2746  from_expr_->find_expr(f, expr_list);
2747 }
2748 
2749 void CaseExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
2750  for (auto p : expr_pair_list) {
2751  p.first->collect_rte_idx(rte_idx_set);
2752  p.second->collect_rte_idx(rte_idx_set);
2753  }
2754  if (else_expr != nullptr) {
2755  else_expr->collect_rte_idx(rte_idx_set);
2756  }
2757 }
2758 
2759 void ExtractExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
2760  from_expr_->collect_rte_idx(rte_idx_set);
2761 }
2762 
2763 void DateaddExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
2764  number_->collect_rte_idx(rte_idx_set);
2765  datetime_->collect_rte_idx(rte_idx_set);
2766 }
2767 
2768 void DatediffExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
2769  start_->collect_rte_idx(rte_idx_set);
2770  end_->collect_rte_idx(rte_idx_set);
2771 }
2772 
2773 void DatetruncExpr::collect_rte_idx(std::set<int>& rte_idx_set) const {
2774  from_expr_->collect_rte_idx(rte_idx_set);
2775 }
2776 
2778  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
2779  bool include_agg) const {
2780  for (auto p : expr_pair_list) {
2781  p.first->collect_column_var(colvar_set, include_agg);
2782  p.second->collect_column_var(colvar_set, include_agg);
2783  }
2784  if (else_expr != nullptr) {
2785  else_expr->collect_column_var(colvar_set, include_agg);
2786  }
2787 }
2788 
2790  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
2791  bool include_agg) const {
2792  from_expr_->collect_column_var(colvar_set, include_agg);
2793 }
2794 
2796  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
2797  bool include_agg) const {
2798  number_->collect_column_var(colvar_set, include_agg);
2799  datetime_->collect_column_var(colvar_set, include_agg);
2800 }
2801 
2803  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
2804  bool include_agg) const {
2805  start_->collect_column_var(colvar_set, include_agg);
2806  end_->collect_column_var(colvar_set, include_agg);
2807 }
2808 
2810  std::set<const ColumnVar*, bool (*)(const ColumnVar*, const ColumnVar*)>& colvar_set,
2811  bool include_agg) const {
2812  from_expr_->collect_column_var(colvar_set, include_agg);
2813 }
2814 
2816  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
2817  for (auto p : expr_pair_list) {
2818  p.first->check_group_by(groupby);
2819  p.second->check_group_by(groupby);
2820  }
2821  if (else_expr != nullptr) {
2822  else_expr->check_group_by(groupby);
2823  }
2824 }
2825 
2827  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
2828  from_expr_->check_group_by(groupby);
2829 }
2830 
2832  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
2833  number_->check_group_by(groupby);
2834  datetime_->check_group_by(groupby);
2835 }
2836 
2838  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
2839  start_->check_group_by(groupby);
2840  end_->check_group_by(groupby);
2841 }
2842 
2844  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby) const {
2845  from_expr_->check_group_by(groupby);
2846 }
2847 
2848 void CaseExpr::get_domain(DomainSet& domain_set) const {
2849  for (const auto& p : expr_pair_list) {
2850  const auto c = std::dynamic_pointer_cast<const Constant>(p.second);
2851  if (c != nullptr) {
2852  c->add_unique(domain_set);
2853  } else {
2854  const auto v = std::dynamic_pointer_cast<const ColumnVar>(p.second);
2855  if (v != nullptr) {
2856  v->add_unique(domain_set);
2857  } else {
2858  const auto cast = std::dynamic_pointer_cast<const UOper>(p.second);
2859  if (cast != nullptr && cast->get_optype() == kCAST) {
2860  const Constant* c = dynamic_cast<const Constant*>(cast->get_operand());
2861  if (c != nullptr) {
2862  cast->add_unique(domain_set);
2863  continue;
2864  } else {
2865  const auto v = std::dynamic_pointer_cast<const ColumnVar>(p.second);
2866  if (v != nullptr) {
2867  v->add_unique(domain_set);
2868  continue;
2869  }
2870  }
2871  }
2872  p.second->get_domain(domain_set);
2873  if (domain_set.empty()) {
2874  return;
2875  }
2876  }
2877  }
2878  }
2879  if (else_expr != nullptr) {
2880  const auto c = std::dynamic_pointer_cast<const Constant>(else_expr);
2881  if (c != nullptr) {
2882  c->add_unique(domain_set);
2883  } else {
2884  const auto v = std::dynamic_pointer_cast<const ColumnVar>(else_expr);
2885  if (v != nullptr) {
2886  v->add_unique(domain_set);
2887  } else {
2888  const auto cast = std::dynamic_pointer_cast<const UOper>(else_expr);
2889  if (cast != nullptr && cast->get_optype() == kCAST) {
2890  const Constant* c = dynamic_cast<const Constant*>(cast->get_operand());
2891  if (c != nullptr) {
2892  c->add_unique(domain_set);
2893  } else {
2894  const auto v = std::dynamic_pointer_cast<const ColumnVar>(else_expr);
2895  if (v != nullptr) {
2896  v->add_unique(domain_set);
2897  }
2898  }
2899  } else {
2900  else_expr->get_domain(domain_set);
2901  }
2902  }
2903  }
2904  }
2905 }
2906 
2907 std::shared_ptr<Analyzer::Expr> FunctionOper::deep_copy() const {
2908  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
2909  for (size_t i = 0; i < getArity(); ++i) {
2910  args_copy.push_back(getArg(i)->deep_copy());
2911  }
2912  return makeExpr<Analyzer::FunctionOper>(type_info, getName(), args_copy);
2913 }
2914 
2915 bool FunctionOper::operator==(const Expr& rhs) const {
2916  if (type_info != rhs.get_type_info()) {
2917  return false;
2918  }
2919  const auto rhs_func_oper = dynamic_cast<const FunctionOper*>(&rhs);
2920  if (!rhs_func_oper) {
2921  return false;
2922  }
2923  if (getName() != rhs_func_oper->getName()) {
2924  return false;
2925  }
2926  if (getArity() != rhs_func_oper->getArity()) {
2927  return false;
2928  }
2929  for (size_t i = 0; i < getArity(); ++i) {
2930  if (!(*getArg(i) == *(rhs_func_oper->getArg(i)))) {
2931  return false;
2932  }
2933  }
2934  return true;
2935 }
2936 
2937 std::string FunctionOper::toString() const {
2938  std::string str{"(" + name_ + " "};
2939  for (const auto arg : args_) {
2940  str += arg->toString();
2941  }
2942  str += ")";
2943  return str;
2944 }
2945 
2946 std::shared_ptr<Analyzer::Expr> FunctionOperWithCustomTypeHandling::deep_copy() const {
2947  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
2948  for (size_t i = 0; i < getArity(); ++i) {
2949  args_copy.push_back(getArg(i)->deep_copy());
2950  }
2951  return makeExpr<Analyzer::FunctionOperWithCustomTypeHandling>(
2952  type_info, getName(), args_copy);
2953 }
2954 
2956  if (type_info != rhs.get_type_info()) {
2957  return false;
2958  }
2959  const auto rhs_func_oper =
2960  dynamic_cast<const FunctionOperWithCustomTypeHandling*>(&rhs);
2961  if (!rhs_func_oper) {
2962  return false;
2963  }
2964  if (getName() != rhs_func_oper->getName()) {
2965  return false;
2966  }
2967  if (getArity() != rhs_func_oper->getArity()) {
2968  return false;
2969  }
2970  for (size_t i = 0; i < getArity(); ++i) {
2971  if (!(*getArg(i) == *(rhs_func_oper->getArg(i)))) {
2972  return false;
2973  }
2974  }
2975  return true;
2976 }
2977 
2978 } // namespace Analyzer
2979 
2980 bool expr_list_match(const std::vector<std::shared_ptr<Analyzer::Expr>>& lhs,
2981  const std::vector<std::shared_ptr<Analyzer::Expr>>& rhs) {
2982  if (lhs.size() != rhs.size()) {
2983  return false;
2984  }
2985  for (size_t i = 0; i < lhs.size(); ++i) {
2986  if (!(*lhs[i] == *rhs[i])) {
2987  return false;
2988  }
2989  }
2990  return true;
2991 }
2992 
2993 std::shared_ptr<Analyzer::Expr> remove_cast(const std::shared_ptr<Analyzer::Expr>& expr) {
2994  const auto uoper = dynamic_cast<const Analyzer::UOper*>(expr.get());
2995  if (!uoper || uoper->get_optype() != kCAST) {
2996  return expr;
2997  }
2998  return uoper->get_own_operand();
2999 }
int8_t tinyintval
Definition: sqltypes.h:123
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:2946
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:1466
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:1388
Definition: sqldefs.h:69
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1179
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1744
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:1527
NEVER_INLINE DEVICE int64_t DateTruncate(DatetruncField field, const int64_t timeval)
void group_predicates(std::list< const Expr *> &scan_predicates, std::list< const Expr *> &join_predicates, std::list< const Expr *> &const_predicates) const override
Definition: Analyzer.cpp:1633
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2612
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:1471
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2686
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1893
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2642
std::string toString() const override
Definition: Analyzer.cpp:2399
const Expr * get_arg() const
Definition: Analyzer.h:577
InValues(std::shared_ptr< Analyzer::Expr > a, const std::list< std::shared_ptr< Analyzer::Expr >> &l)
Definition: Analyzer.cpp:1448
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:1313
#define IS_LOGIC(X)
Definition: sqldefs.h:60
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2189
#define NULL_DOUBLE
Definition: sqltypes.h:176
std::string toString() const override
Definition: Analyzer.cpp:2246
void get_domain(DomainSet &domain_set) const override
Definition: Analyzer.cpp:2848
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1915
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2017
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2696
void d(const SQLTypes expected_type, const std::string &str)
Definition: ImportTest.cpp:268
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2651
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2915
bool Datum_equal(const SQLTypeInfo &ti, Datum val1, Datum val2)
Definition: Analyzer.cpp:1951
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:193
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2009
bool is_time() const
Definition: sqltypes.h:456
int get_precision() const
Definition: sqltypes.h:326
bool get_calc_encoded_length() const
Definition: Analyzer.h:651
int get_column_id() const
Definition: Analyzer.h:194
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1822
std::shared_ptr< Analyzer::Expr > decompress()
Definition: Analyzer.cpp:649
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:325
Definition: sqltypes.h:51
std::shared_ptr< Analyzer::Expr > remove_cast(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:2993
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:1603
void add_rte(RangeTableEntry *rte)
Definition: Analyzer.cpp:1266
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1131
EncodingType
Definition: encodetypes.h:22
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1879
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2206
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2595
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1865
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2049
#define NULL_BIGINT
Definition: sqltypes.h:174
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:330
bool boolval
Definition: sqltypes.h:122
bool operator==(Expr const &rhs) const override
Definition: Analyzer.cpp:2225
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1712
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:126
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:95
SQLOps
Definition: sqldefs.h:29
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:2763
Definition: sqldefs.h:35
void c(const std::string &query_string, const ExecutorDeviceType device_type)
HOST DEVICE int get_scale() const
Definition: sqltypes.h:328
std::shared_ptr< Analyzer::Expr > arg
Definition: Analyzer.h:607
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:80
Definition: sqldefs.h:36
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:323
Definition: sqldefs.h:38
std::string toString() const override
Definition: Analyzer.cpp:2937
VarlenDatum * arrayval
Definition: sqltypes.h:129
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:2802
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:130
const std::shared_ptr< const Analyzer::Expr > arg
Definition: Analyzer.h:636
#define UNREACHABLE()
Definition: Logger.h:231
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:190
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:194
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:2843
Definition: sqldefs.h:49
Definition: sqldefs.h:30
const Expr * get_start_expr() const
Definition: Analyzer.h:1172
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1223
DateaddField get_field() const
Definition: Analyzer.h:1129
std::shared_ptr< Analyzer::Expr > ExpressionPtr
Definition: Analyzer.h:179
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1857
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2132
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:1402
void set_fixed_size()
Definition: sqltypes.h:422
Definition: sqldefs.h:41
std::string toString() const
Definition: Analyzer.cpp:2573
bool is_expr_nullable(const Analyzer::Expr *expr)
Definition: Analyzer.cpp:1424
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:1541
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1204
DEVICE int64_t DateTruncateAlterPrecisionScaleUp(const int64_t timeval, const int64_t scale)
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2955
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:59
std::string get_compression_name() const
Definition: sqltypes.h:448
bool is_castable(const SQLTypeInfoCore &new_type_info) const
Definition: sqltypes.h:510
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:1589
void set_dimension(int d)
Definition: sqltypes.h:415
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2111
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:1513
const Expr * get_from_expr() const
Definition: Analyzer.h:1089
WhichRow get_which_row() const
Definition: Analyzer.h:272
void set_scale(int s)
Definition: sqltypes.h:418
std::string toString() const override
Definition: Analyzer.cpp:2522
void cast_number(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:731
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:331
void set_compression(EncodingType c)
Definition: sqltypes.h:423
std::string toString() const override
Definition: Analyzer.cpp:2308
SQLTypeInfo type_info
Definition: Analyzer.h:175
std::list< const Expr * > DomainSet
Definition: Analyzer.h:61
int32_t intval
Definition: sqltypes.h:125
std::string toString() const override
Definition: Analyzer.cpp:2255
const std::shared_ptr< Analyzer::Expr > get_own_operand() const
Definition: Analyzer.h:365
const Expr * get_arg() const
Definition: Analyzer.h:743
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1298
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1851
std::string to_string(char const *&&v)
void set_notnull(bool n)
Definition: sqltypes.h:420
~Subquery() override
Definition: Analyzer.cpp:47
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2149
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:2826
std::string toString() const override
Definition: Analyzer.cpp:2277
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:69
std::string toString() const override
Definition: Analyzer.cpp:2376
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:1618
SQLOps get_optype() const
Definition: Analyzer.h:432
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2038
Expr * get_arg() const
Definition: Analyzer.h:988
~Constant() override
Definition: Analyzer.cpp:41
std::string toString() const override
Definition: Analyzer.cpp:2436
DatetruncField get_field() const
Definition: Analyzer.h:1213
std::string toString() const override
Definition: Analyzer.cpp:2458
const Expr * get_arg() const
Definition: Analyzer.h:698
std::string toString() const override
Definition: Analyzer.cpp:2410
Definition: sqldefs.h:71
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1735
void cast_from_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1037
const Expr * get_arg() const
Definition: Analyzer.h:797
std::string toString() const override
Definition: Analyzer.cpp:2540
std::string get_type_name() const
Definition: sqltypes.h:426
const Expr * get_else_expr() const
Definition: Analyzer.h:1044
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2210
float floatval
Definition: sqltypes.h:127
Datum get_constval() const
Definition: Analyzer.h:328
std::shared_ptr< Analyzer::Expr > normalize_simple_predicate(int &rte_idx) const override
Definition: Analyzer.cpp:1337
const std::list< std::shared_ptr< Analyzer::Expr > > value_list
Definition: Analyzer.h:609
bool is_array() const
Definition: sqltypes.h:458
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:148
static SQLTypeInfo common_string_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:379
bool is_timeinterval() const
Definition: sqltypes.h:461
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:159
bool is_integer() const
Definition: sqltypes.h:452
std::string toString() const
Definition: Analyzer.cpp:2563
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1776
void group_predicates(std::list< const Expr *> &scan_predicates, std::list< const Expr *> &join_predicates, std::list< const Expr *> &const_predicates) const override
Definition: Analyzer.cpp:1569
T v(const TargetValue &r)
DEVICE int64_t DateTruncateAlterPrecisionScaleDown(const int64_t timeval, const int64_t scale)
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:2768
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1899
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:2759
void cast_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1026
std::string toString() const override
Definition: Analyzer.cpp:2422
#define NULL_TINYINT
Definition: sqltypes.h:171
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:108
const Expr * get_from_expr() const
Definition: Analyzer.h:1214
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:2980
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2391
const Expr * get_escape_expr() const
Definition: Analyzer.h:800
bool is_decimal() const
Definition: sqltypes.h:453
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:175
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:324
std::string toString() const override
Definition: Analyzer.cpp:2240
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:1499
int64_t bigintval
Definition: sqltypes.h:126
const std::vector< std::shared_ptr< Analyzer::Expr > > & getTuple() const
Definition: Analyzer.h:243
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1286
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2720
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2172
std::string toString() const override
Definition: Analyzer.cpp:2447
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:2831
#define NULL_FLOAT
Definition: sqltypes.h:175
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2097
const std::list< std::shared_ptr< Analyzer::Expr > > value_list
Definition: Analyzer.h:341
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:2387
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2180
std::string toString() const override
Definition: Analyzer.cpp:2532
std::string sql_window_function_to_str(const SqlWindowFunctionKind kind)
static SQLTypeInfo common_numeric_type(const SQLTypeInfo &type1, const SQLTypeInfo &type2)
Definition: Analyzer.cpp:419
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:2773
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1753
Definition: sqldefs.h:37
Definition: sqldefs.h:71
float get_likelihood() const
Definition: Analyzer.h:927
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:1921
Definition: sqldefs.h:69
int16_t smallintval
Definition: sqltypes.h:124
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1041
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2633
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:2777
SQLTypeInfoCore get_elem_type() const
Definition: sqltypes.h:632
#define NULL_INT
Definition: sqltypes.h:173
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2026
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2664
IntFracRepr decimal_to_int_frac(const int64_t dec, const SQLTypeInfo &ti)
Definition: Analyzer.cpp:702
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:823
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1793
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:2809
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2677
std::string * stringval
Definition: sqltypes.h:131
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:2907
DatetruncField get_field() const
Definition: Analyzer.h:1171
Definition: sqldefs.h:34
bool is_boolean() const
Definition: sqltypes.h:457
const std::vector< int64_t > value_list
Definition: Analyzer.h:637
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:2749
bool get_is_null() const
Definition: Analyzer.h:327
Definition: sqltypes.h:54
Definition: sqltypes.h:55
Definition: sqldefs.h:40
ExpressionPtrVector contained_expressions_
Definition: Analyzer.h:1397
virtual void add_unique(std::list< const Expr *> &expr_list) const
Definition: Analyzer.cpp:2585
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1767
Definition: sqldefs.h:69
void do_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1056
virtual ~Query()
Definition: Analyzer.cpp:51
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:186
DEVICE int64_t DateTruncateHighPrecisionToDate(const int64_t timeval, const int64_t scale)
bool is_in_values_nullable(const std::shared_ptr< Analyzer::Expr > &a, const std::list< std::shared_ptr< Analyzer::Expr >> &l)
Definition: Analyzer.cpp:1433
const Expr * get_arg() const
Definition: Analyzer.h:869
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:171
bool is_null(const T &v, const SQLTypeInfo &t)
std::string toString() const override
Definition: Analyzer.cpp:2518
const Expr * get_pattern_expr() const
Definition: Analyzer.h:871
int64_t convert_decimal_value_to_scale(const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
Definition: Datum.cpp:284
const Expr * get_escape_expr() const
Definition: Analyzer.h:872
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1647
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
int get_rte_idx() const
Definition: Analyzer.h:195
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2740
std::string toString() const override
Definition: Analyzer.cpp:2536
Analyzer::ExpressionPtr deep_copy() const override
Definition: Analyzer.cpp:199
Datum StringToDatum(const std::string &s, SQLTypeInfo &ti)
Definition: Datum.cpp:90
SQLAgg get_aggtype() const
Definition: Analyzer.h:987
int get_table_id() const
Definition: Analyzer.h:193
Definition: sqldefs.h:32
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:1942
const Expr * get_arg() const
Definition: Analyzer.h:925
Definition: sqldefs.h:71
int get_rte_idx(const std::string &range_var_name) const
Definition: Analyzer.cpp:1255
bool get_is_distinct() const
Definition: Analyzer.h:990
std::string toString() const override
Definition: Analyzer.cpp:2527
std::string toString() const override
Definition: Analyzer.cpp:2464
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1907
Definition: sqltypes.h:43
virtual std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:663
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1293
void collect_rte_idx(std::set< int > &rte_idx_set) const override
Definition: Analyzer.cpp:63
void set_comp_param(int p)
Definition: sqltypes.h:424
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:332
std::string toString() const override
Definition: Analyzer.cpp:2262
std::string toString() const override
Definition: Analyzer.cpp:2548
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:1380
int get_varno() const
Definition: Analyzer.h:274
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:142
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
bool is_number() const
Definition: sqltypes.h:455
Constant(SQLTypes t, bool n)
Definition: Analyzer.h:307
#define NULL_SMALLINT
Definition: sqltypes.h:172
std::string sql_type_to_str(const SQLTypes &type)
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1726
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:1992
#define CHECK(condition)
Definition: Logger.h:187
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2060
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1873
std::shared_ptr< Analyzer::Expr > add_cast(const SQLTypeInfo &new_type_info) override
Definition: Analyzer.cpp:1249
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:48
#define COMMUTE_COMPARISON(X)
Definition: sqldefs.h:63
bool is_geometry() const
Definition: sqltypes.h:462
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:122
const Expr * get_arg() const
Definition: Analyzer.h:649
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:114
Definition: sqldefs.h:33
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:84
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2624
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:185
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:99
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:2789
Definition: sqltypes.h:47
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1836
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:1270
std::string toString() const override
Definition: Analyzer.cpp:2372
const Expr * get_like_expr() const
Definition: Analyzer.h:799
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2198
bool expr_is(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:1307
ExtractField get_field() const
Definition: Analyzer.h:1088
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:1485
std::string toString() const override
Definition: Analyzer.cpp:2429
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:579
Definition: sqldefs.h:71
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:1555
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:134
const Expr * get_end_expr() const
Definition: Analyzer.h:1173
bool contains_agg
Definition: Analyzer.h:176
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2604
Definition: sqldefs.h:71
const Expr * get_number_expr() const
Definition: Analyzer.h:1130
bool is_string() const
Definition: sqltypes.h:450
bool is_distinct(const size_t input_idx, const RelAlgNode *node)
Definition: sqldefs.h:39
#define TRANSIENT_DICT(ID)
Definition: sqltypes.h:187
std::string toString() const override
Definition: Analyzer.cpp:2501
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:2837
bool get_is_ilike() const
Definition: Analyzer.h:801
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:151
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:204
#define NULL_BOOLEAN
Definition: sqltypes.h:170
void cast_to_string(const SQLTypeInfo &new_type_info)
Definition: Analyzer.cpp:1045
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const override
Definition: Analyzer.cpp:2795
void check_group_by(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby) const override
Definition: Analyzer.cpp:2815
SQLOps get_optype() const
Definition: Analyzer.h:363
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1830
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:180
bool operator==(const Expr &rhs) const override
Definition: Analyzer.cpp:2079
#define IS_COMPARISON(X)
Definition: sqldefs.h:57
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:1452
std::shared_ptr< Analyzer::Expr > rewrite_agg_to_var(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1685
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2730
double doubleval
Definition: sqltypes.h:128
void find_expr(bool(*f)(const Expr *), std::list< const Expr *> &expr_list) const override
Definition: Analyzer.cpp:2711
const Expr * get_right_operand() const
Definition: Analyzer.h:436
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1808
std::shared_ptr< Analyzer::Expr > rewrite_with_child_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1661
std::shared_ptr< Analyzer::Expr > rewrite_with_targetlist(const std::vector< std::shared_ptr< TargetEntry >> &tlist) const override
Definition: Analyzer.cpp:1814
const Expr * get_left_operand() const
Definition: Analyzer.h:435
const Expr * get_operand() const
Definition: Analyzer.h:364