OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExpressionRewrite.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <algorithm>
20 #include <boost/locale/conversion.hpp>
21 #include <unordered_set>
22 
23 #include "Analyzer/Analyzer.h"
24 #include "Logger/Logger.h"
25 #include "Parser/ParserNode.h"
27 #include "QueryEngine/Execute.h"
31 #include "Shared/sqldefs.h"
32 #include "StringOps/StringOps.h"
33 
34 namespace {
35 
36 class OrToInVisitor : public ScalarExprVisitor<std::shared_ptr<Analyzer::InValues>> {
37  protected:
38  std::shared_ptr<Analyzer::InValues> visitBinOper(
39  const Analyzer::BinOper* bin_oper) const override {
40  switch (bin_oper->get_optype()) {
41  case kEQ: {
42  const auto rhs_owned = bin_oper->get_own_right_operand();
43  auto rhs_no_cast = extract_cast_arg(rhs_owned.get());
44  if (!dynamic_cast<const Analyzer::Constant*>(rhs_no_cast)) {
45  return nullptr;
46  }
47  const auto arg = bin_oper->get_own_left_operand();
48  const auto& arg_ti = arg->get_type_info();
49  auto rhs = rhs_no_cast->deep_copy()->add_cast(arg_ti);
50  return makeExpr<Analyzer::InValues>(
51  arg, std::list<std::shared_ptr<Analyzer::Expr>>{rhs});
52  }
53  case kOR: {
54  return aggregateResult(visit(bin_oper->get_left_operand()),
55  visit(bin_oper->get_right_operand()));
56  }
57  default:
58  break;
59  }
60  return nullptr;
61  }
62 
63  std::shared_ptr<Analyzer::InValues> visitUOper(
64  const Analyzer::UOper* uoper) const override {
65  return nullptr;
66  }
67 
68  std::shared_ptr<Analyzer::InValues> visitInValues(
69  const Analyzer::InValues*) const override {
70  return nullptr;
71  }
72 
73  std::shared_ptr<Analyzer::InValues> visitInIntegerSet(
74  const Analyzer::InIntegerSet*) const override {
75  return nullptr;
76  }
77 
78  std::shared_ptr<Analyzer::InValues> visitCharLength(
79  const Analyzer::CharLengthExpr*) const override {
80  return nullptr;
81  }
82 
83  std::shared_ptr<Analyzer::InValues> visitKeyForString(
84  const Analyzer::KeyForStringExpr*) const override {
85  return nullptr;
86  }
87 
88  std::shared_ptr<Analyzer::InValues> visitSampleRatio(
89  const Analyzer::SampleRatioExpr*) const override {
90  return nullptr;
91  }
92 
93  std::shared_ptr<Analyzer::InValues> visitCardinality(
94  const Analyzer::CardinalityExpr*) const override {
95  return nullptr;
96  }
97 
98  std::shared_ptr<Analyzer::InValues> visitLikeExpr(
99  const Analyzer::LikeExpr*) const override {
100  return nullptr;
101  }
102 
103  std::shared_ptr<Analyzer::InValues> visitRegexpExpr(
104  const Analyzer::RegexpExpr*) const override {
105  return nullptr;
106  }
107 
108  std::shared_ptr<Analyzer::InValues> visitCaseExpr(
109  const Analyzer::CaseExpr*) const override {
110  return nullptr;
111  }
112 
113  std::shared_ptr<Analyzer::InValues> visitDatetruncExpr(
114  const Analyzer::DatetruncExpr*) const override {
115  return nullptr;
116  }
117 
118  std::shared_ptr<Analyzer::InValues> visitDatediffExpr(
119  const Analyzer::DatediffExpr*) const override {
120  return nullptr;
121  }
122 
123  std::shared_ptr<Analyzer::InValues> visitDateaddExpr(
124  const Analyzer::DateaddExpr*) const override {
125  return nullptr;
126  }
127 
128  std::shared_ptr<Analyzer::InValues> visitExtractExpr(
129  const Analyzer::ExtractExpr*) const override {
130  return nullptr;
131  }
132 
133  std::shared_ptr<Analyzer::InValues> visitLikelihood(
134  const Analyzer::LikelihoodExpr*) const override {
135  return nullptr;
136  }
137 
138  std::shared_ptr<Analyzer::InValues> visitAggExpr(
139  const Analyzer::AggExpr*) const override {
140  return nullptr;
141  }
142 
143  std::shared_ptr<Analyzer::InValues> aggregateResult(
144  const std::shared_ptr<Analyzer::InValues>& lhs,
145  const std::shared_ptr<Analyzer::InValues>& rhs) const override {
146  if (!lhs || !rhs) {
147  return nullptr;
148  }
149 
150  if (lhs->get_arg()->get_type_info() == rhs->get_arg()->get_type_info() &&
151  (*lhs->get_arg() == *rhs->get_arg())) {
152  auto union_values = lhs->get_value_list();
153  const auto& rhs_values = rhs->get_value_list();
154  union_values.insert(union_values.end(), rhs_values.begin(), rhs_values.end());
155  return makeExpr<Analyzer::InValues>(lhs->get_own_arg(), union_values);
156  }
157  return nullptr;
158  }
159 };
160 
162  protected:
163  std::shared_ptr<Analyzer::Expr> visitBinOper(
164  const Analyzer::BinOper* bin_oper) const override {
165  OrToInVisitor simple_visitor;
166  if (bin_oper->get_optype() == kOR) {
167  auto rewritten = simple_visitor.visit(bin_oper);
168  if (rewritten) {
169  return rewritten;
170  }
171  }
172  auto lhs = bin_oper->get_own_left_operand();
173  auto rhs = bin_oper->get_own_right_operand();
174  auto rewritten_lhs = visit(lhs.get());
175  auto rewritten_rhs = visit(rhs.get());
176  return makeExpr<Analyzer::BinOper>(bin_oper->get_type_info(),
177  bin_oper->get_contains_agg(),
178  bin_oper->get_optype(),
179  bin_oper->get_qualifier(),
180  rewritten_lhs ? rewritten_lhs : lhs,
181  rewritten_rhs ? rewritten_rhs : rhs);
182  }
183 };
184 
186  protected:
188 
189  RetType visitArrayOper(const Analyzer::ArrayExpr* array_expr) const override {
190  std::vector<std::shared_ptr<Analyzer::Expr>> args_copy;
191  for (size_t i = 0; i < array_expr->getElementCount(); ++i) {
192  auto const element_expr_ptr = visit(array_expr->getElement(i));
193  auto const& element_expr_type_info = element_expr_ptr->get_type_info();
194 
195  if (!element_expr_type_info.is_string() ||
196  element_expr_type_info.get_compression() != kENCODING_NONE) {
197  args_copy.push_back(element_expr_ptr);
198  } else {
199  auto transient_dict_type_info = element_expr_type_info;
200 
201  transient_dict_type_info.set_compression(kENCODING_DICT);
202  transient_dict_type_info.set_comp_param(TRANSIENT_DICT_ID);
203  transient_dict_type_info.set_fixed_size();
204  args_copy.push_back(element_expr_ptr->add_cast(transient_dict_type_info));
205  }
206  }
207 
208  const auto& type_info = array_expr->get_type_info();
209  return makeExpr<Analyzer::ArrayExpr>(
210  type_info, args_copy, array_expr->isNull(), array_expr->isLocalAlloc());
211  }
212 };
213 
215  template <typename T>
216  bool foldComparison(SQLOps optype, T t1, T t2) const {
217  switch (optype) {
218  case kEQ:
219  return t1 == t2;
220  case kNE:
221  return t1 != t2;
222  case kLT:
223  return t1 < t2;
224  case kLE:
225  return t1 <= t2;
226  case kGT:
227  return t1 > t2;
228  case kGE:
229  return t1 >= t2;
230  default:
231  break;
232  }
233  throw std::runtime_error("Unable to fold");
234  return false;
235  }
236 
237  template <typename T>
238  bool foldLogic(SQLOps optype, T t1, T t2) const {
239  switch (optype) {
240  case kAND:
241  return t1 && t2;
242  case kOR:
243  return t1 || t2;
244  case kNOT:
245  return !t1;
246  default:
247  break;
248  }
249  throw std::runtime_error("Unable to fold");
250  return false;
251  }
252 
253  template <typename T>
254  T foldArithmetic(SQLOps optype, T t1, T t2) const {
255  bool t2_is_zero = (t2 == (t2 - t2));
256  bool t2_is_negative = (t2 < (t2 - t2));
257  switch (optype) {
258  case kPLUS:
259  // The MIN limit for float and double is the smallest representable value,
260  // not the lowest negative value! Switching to C++11 lowest.
261  if ((t2_is_negative && t1 < std::numeric_limits<T>::lowest() - t2) ||
262  (!t2_is_negative && t1 > std::numeric_limits<T>::max() - t2)) {
263  num_overflows_++;
264  throw std::runtime_error("Plus overflow");
265  }
266  return t1 + t2;
267  case kMINUS:
268  if ((t2_is_negative && t1 > std::numeric_limits<T>::max() + t2) ||
269  (!t2_is_negative && t1 < std::numeric_limits<T>::lowest() + t2)) {
270  num_overflows_++;
271  throw std::runtime_error("Minus overflow");
272  }
273  return t1 - t2;
274  case kMULTIPLY: {
275  if (t2_is_zero) {
276  return t2;
277  }
278  auto ct1 = t1;
279  auto ct2 = t2;
280  // Need to keep t2's sign on the left
281  if (t2_is_negative) {
282  if (t1 == std::numeric_limits<T>::lowest() ||
283  t2 == std::numeric_limits<T>::lowest()) {
284  // negation could overflow - bail
285  num_overflows_++;
286  throw std::runtime_error("Mul neg overflow");
287  }
288  ct1 = -t1; // ct1 gets t2's negativity
289  ct2 = -t2; // ct2 is now positive
290  }
291  // Don't check overlow if we are folding FP mul by a fraction
292  bool ct2_is_fraction = (ct2 < (ct2 / ct2));
293  if (!ct2_is_fraction) {
294  if (ct1 > std::numeric_limits<T>::max() / ct2 ||
295  ct1 < std::numeric_limits<T>::lowest() / ct2) {
296  num_overflows_++;
297  throw std::runtime_error("Mul overflow");
298  }
299  }
300  return t1 * t2;
301  }
302  case kDIVIDE:
303  if (t2_is_zero) {
304  throw std::runtime_error("Will not fold division by zero");
305  }
306  return t1 / t2;
307  default:
308  break;
309  }
310  throw std::runtime_error("Unable to fold");
311  }
312 
313  bool foldOper(SQLOps optype,
314  SQLTypes type,
315  Datum lhs,
316  Datum rhs,
317  Datum& result,
318  SQLTypes& result_type) const {
319  result_type = type;
320 
321  try {
322  switch (type) {
323  case kBOOLEAN:
324  if (IS_COMPARISON(optype)) {
325  result.boolval = foldComparison<bool>(optype, lhs.boolval, rhs.boolval);
326  result_type = kBOOLEAN;
327  return true;
328  }
329  if (IS_LOGIC(optype)) {
330  result.boolval = foldLogic<bool>(optype, lhs.boolval, rhs.boolval);
331  result_type = kBOOLEAN;
332  return true;
333  }
334  CHECK(!IS_ARITHMETIC(optype));
335  break;
336  case kTINYINT:
337  if (IS_COMPARISON(optype)) {
338  result.boolval =
339  foldComparison<int8_t>(optype, lhs.tinyintval, rhs.tinyintval);
340  result_type = kBOOLEAN;
341  return true;
342  }
343  if (IS_ARITHMETIC(optype)) {
344  result.tinyintval =
345  foldArithmetic<int8_t>(optype, lhs.tinyintval, rhs.tinyintval);
346  result_type = kTINYINT;
347  return true;
348  }
349  CHECK(!IS_LOGIC(optype));
350  break;
351  case kSMALLINT:
352  if (IS_COMPARISON(optype)) {
353  result.boolval =
354  foldComparison<int16_t>(optype, lhs.smallintval, rhs.smallintval);
355  result_type = kBOOLEAN;
356  return true;
357  }
358  if (IS_ARITHMETIC(optype)) {
359  result.smallintval =
360  foldArithmetic<int16_t>(optype, lhs.smallintval, rhs.smallintval);
361  result_type = kSMALLINT;
362  return true;
363  }
364  CHECK(!IS_LOGIC(optype));
365  break;
366  case kINT:
367  if (IS_COMPARISON(optype)) {
368  result.boolval = foldComparison<int32_t>(optype, lhs.intval, rhs.intval);
369  result_type = kBOOLEAN;
370  return true;
371  }
372  if (IS_ARITHMETIC(optype)) {
373  result.intval = foldArithmetic<int32_t>(optype, lhs.intval, rhs.intval);
374  result_type = kINT;
375  return true;
376  }
377  CHECK(!IS_LOGIC(optype));
378  break;
379  case kBIGINT:
380  if (IS_COMPARISON(optype)) {
381  result.boolval =
382  foldComparison<int64_t>(optype, lhs.bigintval, rhs.bigintval);
383  result_type = kBOOLEAN;
384  return true;
385  }
386  if (IS_ARITHMETIC(optype)) {
387  result.bigintval =
388  foldArithmetic<int64_t>(optype, lhs.bigintval, rhs.bigintval);
389  result_type = kBIGINT;
390  return true;
391  }
392  CHECK(!IS_LOGIC(optype));
393  break;
394  case kFLOAT:
395  if (IS_COMPARISON(optype)) {
396  result.boolval = foldComparison<float>(optype, lhs.floatval, rhs.floatval);
397  result_type = kBOOLEAN;
398  return true;
399  }
400  if (IS_ARITHMETIC(optype)) {
401  result.floatval = foldArithmetic<float>(optype, lhs.floatval, rhs.floatval);
402  result_type = kFLOAT;
403  return true;
404  }
405  CHECK(!IS_LOGIC(optype));
406  break;
407  case kDOUBLE:
408  if (IS_COMPARISON(optype)) {
409  result.boolval = foldComparison<double>(optype, lhs.doubleval, rhs.doubleval);
410  result_type = kBOOLEAN;
411  return true;
412  }
413  if (IS_ARITHMETIC(optype)) {
414  result.doubleval =
415  foldArithmetic<double>(optype, lhs.doubleval, rhs.doubleval);
416  result_type = kDOUBLE;
417  return true;
418  }
419  CHECK(!IS_LOGIC(optype));
420  break;
421  default:
422  break;
423  }
424  } catch (...) {
425  return false;
426  }
427  return false;
428  }
429 
430  std::shared_ptr<Analyzer::Expr> visitUOper(
431  const Analyzer::UOper* uoper) const override {
432  const auto unvisited_operand = uoper->get_operand();
433  const auto optype = uoper->get_optype();
434  const auto& ti = uoper->get_type_info();
435  if (optype == kCAST) {
436  // Cache the cast type so it could be used in operand rewriting/folding
437  casts_.insert({unvisited_operand, ti});
438  }
439  const auto operand = visit(unvisited_operand);
440 
441  const auto& operand_ti = operand->get_type_info();
442  const auto operand_type =
443  operand_ti.is_decimal() ? decimal_to_int_type(operand_ti) : operand_ti.get_type();
444  const auto const_operand =
445  std::dynamic_pointer_cast<const Analyzer::Constant>(operand);
446 
447  if (const_operand) {
448  const auto operand_datum = const_operand->get_constval();
449  Datum zero_datum = {};
450  Datum result_datum = {};
451  SQLTypes result_type;
452  switch (optype) {
453  case kNOT: {
454  if (foldOper(kEQ,
455  operand_type,
456  zero_datum,
457  operand_datum,
458  result_datum,
459  result_type)) {
460  CHECK_EQ(result_type, kBOOLEAN);
461  return makeExpr<Analyzer::Constant>(result_type, false, result_datum);
462  }
463  break;
464  }
465  case kUMINUS: {
466  if (foldOper(kMINUS,
467  operand_type,
468  zero_datum,
469  operand_datum,
470  result_datum,
471  result_type)) {
472  if (!operand_ti.is_decimal()) {
473  return makeExpr<Analyzer::Constant>(result_type, false, result_datum);
474  }
475  return makeExpr<Analyzer::Constant>(ti, false, result_datum);
476  }
477  break;
478  }
479  case kCAST: {
480  // Trying to fold number to number casts only
481  if (!ti.is_number() || !operand_ti.is_number()) {
482  break;
483  }
484  // Disallowing folding of FP to DECIMAL casts for now:
485  // allowing them would make this test pass:
486  // update dectest set d=cast( 1234.0 as float );
487  // which is expected to throw in Update.ImplicitCastToNumericTypes
488  // due to cast codegen currently not supporting these casts
489  if (ti.is_decimal() && operand_ti.is_fp()) {
490  break;
491  }
492  auto operand_copy = const_operand->deep_copy();
493  auto cast_operand = operand_copy->add_cast(ti);
494  auto const_cast_operand =
495  std::dynamic_pointer_cast<const Analyzer::Constant>(cast_operand);
496  if (const_cast_operand) {
497  auto const_cast_datum = const_cast_operand->get_constval();
498  return makeExpr<Analyzer::Constant>(ti, false, const_cast_datum);
499  }
500  }
501  default:
502  break;
503  }
504  }
505 
506  return makeExpr<Analyzer::UOper>(
507  uoper->get_type_info(), uoper->get_contains_agg(), optype, operand);
508  }
509 
510  std::shared_ptr<Analyzer::Expr> visitBinOper(
511  const Analyzer::BinOper* bin_oper) const override {
512  const auto optype = bin_oper->get_optype();
513  auto ti = bin_oper->get_type_info();
514  auto left_operand = bin_oper->get_own_left_operand();
515  auto right_operand = bin_oper->get_own_right_operand();
516 
517  // Check if bin_oper result is cast to a larger int or fp type
518  if (casts_.find(bin_oper) != casts_.end()) {
519  const auto cast_ti = casts_[bin_oper];
520  const auto& lhs_ti = bin_oper->get_left_operand()->get_type_info();
521  // Propagate cast down to the operands for folding
522  if ((cast_ti.is_integer() || cast_ti.is_fp()) && lhs_ti.is_integer() &&
523  cast_ti.get_size() > lhs_ti.get_size() &&
524  (optype == kMINUS || optype == kPLUS || optype == kMULTIPLY)) {
525  // Before folding, cast the operands to the bigger type to avoid overflows.
526  // Currently upcasting smaller integer types to larger integers or double.
527  left_operand = left_operand->deep_copy()->add_cast(cast_ti);
528  right_operand = right_operand->deep_copy()->add_cast(cast_ti);
529  ti = cast_ti;
530  }
531  }
532 
533  const auto lhs = visit(left_operand.get());
534  const auto rhs = visit(right_operand.get());
535 
536  auto const_lhs = std::dynamic_pointer_cast<Analyzer::Constant>(lhs);
537  auto const_rhs = std::dynamic_pointer_cast<Analyzer::Constant>(rhs);
538  const auto& lhs_ti = lhs->get_type_info();
539  const auto& rhs_ti = rhs->get_type_info();
540  auto lhs_type = lhs_ti.is_decimal() ? decimal_to_int_type(lhs_ti) : lhs_ti.get_type();
541  auto rhs_type = rhs_ti.is_decimal() ? decimal_to_int_type(rhs_ti) : rhs_ti.get_type();
542 
543  if (const_lhs && const_rhs && lhs_type == rhs_type) {
544  auto lhs_datum = const_lhs->get_constval();
545  auto rhs_datum = const_rhs->get_constval();
546  Datum result_datum = {};
547  SQLTypes result_type;
548  if (foldOper(optype, lhs_type, lhs_datum, rhs_datum, result_datum, result_type)) {
549  // Fold all ops that don't take in decimal operands, and also decimal comparisons
550  if (!lhs_ti.is_decimal() || IS_COMPARISON(optype)) {
551  return makeExpr<Analyzer::Constant>(result_type, false, result_datum);
552  }
553  // Decimal arithmetic has been done as kBIGINT. Selectively fold some decimal ops,
554  // using result_datum and BinOper expr typeinfo which was adjusted for these ops.
555  if (optype == kMINUS || optype == kPLUS || optype == kMULTIPLY) {
556  return makeExpr<Analyzer::Constant>(ti, false, result_datum);
557  }
558  }
559  }
560 
561  if (optype == kAND && lhs_type == rhs_type && lhs_type == kBOOLEAN) {
562  if (const_rhs && !const_rhs->get_is_null()) {
563  auto rhs_datum = const_rhs->get_constval();
564  if (rhs_datum.boolval == false) {
565  Datum d;
566  d.boolval = false;
567  // lhs && false --> false
568  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
569  }
570  // lhs && true --> lhs
571  return lhs;
572  }
573  if (const_lhs && !const_lhs->get_is_null()) {
574  auto lhs_datum = const_lhs->get_constval();
575  if (lhs_datum.boolval == false) {
576  Datum d;
577  d.boolval = false;
578  // false && rhs --> false
579  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
580  }
581  // true && rhs --> rhs
582  return rhs;
583  }
584  }
585  if (optype == kOR && lhs_type == rhs_type && lhs_type == kBOOLEAN) {
586  if (const_rhs && !const_rhs->get_is_null()) {
587  auto rhs_datum = const_rhs->get_constval();
588  if (rhs_datum.boolval == true) {
589  Datum d;
590  d.boolval = true;
591  // lhs || true --> true
592  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
593  }
594  // lhs || false --> lhs
595  return lhs;
596  }
597  if (const_lhs && !const_lhs->get_is_null()) {
598  auto lhs_datum = const_lhs->get_constval();
599  if (lhs_datum.boolval == true) {
600  Datum d;
601  d.boolval = true;
602  // true || rhs --> true
603  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
604  }
605  // false || rhs --> rhs
606  return rhs;
607  }
608  }
609  if (*lhs == *rhs) {
610  if (!lhs_ti.get_notnull()) {
611  CHECK(!rhs_ti.get_notnull());
612  // We can't fold the ostensible tautaulogy
613  // for nullable lhs and rhs types, as
614  // lhs <> rhs when they are null
615 
616  // We likely could turn this into a lhs is not null
617  // operatation, but is it worth it?
618  return makeExpr<Analyzer::BinOper>(ti,
619  bin_oper->get_contains_agg(),
620  bin_oper->get_optype(),
621  bin_oper->get_qualifier(),
622  lhs,
623  rhs);
624  }
625  CHECK(rhs_ti.get_notnull());
626  // Tautologies: v=v; v<=v; v>=v
627  if (optype == kEQ || optype == kLE || optype == kGE) {
628  Datum d;
629  d.boolval = true;
630  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
631  }
632  // Contradictions: v!=v; v<v; v>v
633  if (optype == kNE || optype == kLT || optype == kGT) {
634  Datum d;
635  d.boolval = false;
636  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
637  }
638  // v-v
639  if (optype == kMINUS) {
640  Datum d = {};
641  return makeExpr<Analyzer::Constant>(lhs_type, false, d);
642  }
643  }
644  // Convert fp division by a constant to multiplication by 1/constant
645  if (optype == kDIVIDE && const_rhs && rhs_ti.is_fp()) {
646  auto rhs_datum = const_rhs->get_constval();
647  std::shared_ptr<Analyzer::Expr> recip_rhs = nullptr;
648  if (rhs_ti.get_type() == kFLOAT) {
649  if (rhs_datum.floatval == 1.0) {
650  return lhs;
651  }
652  auto f = std::fabs(rhs_datum.floatval);
653  if (f > 1.0 || (f != 0.0 && 1.0 < f * std::numeric_limits<float>::max())) {
654  rhs_datum.floatval = 1.0 / rhs_datum.floatval;
655  recip_rhs = makeExpr<Analyzer::Constant>(rhs_type, false, rhs_datum);
656  }
657  } else if (rhs_ti.get_type() == kDOUBLE) {
658  if (rhs_datum.doubleval == 1.0) {
659  return lhs;
660  }
661  auto d = std::fabs(rhs_datum.doubleval);
662  if (d > 1.0 || (d != 0.0 && 1.0 < d * std::numeric_limits<double>::max())) {
663  rhs_datum.doubleval = 1.0 / rhs_datum.doubleval;
664  recip_rhs = makeExpr<Analyzer::Constant>(rhs_type, false, rhs_datum);
665  }
666  }
667  if (recip_rhs) {
668  return makeExpr<Analyzer::BinOper>(ti,
669  bin_oper->get_contains_agg(),
670  kMULTIPLY,
671  bin_oper->get_qualifier(),
672  lhs,
673  recip_rhs);
674  }
675  }
676 
677  return makeExpr<Analyzer::BinOper>(ti,
678  bin_oper->get_contains_agg(),
679  bin_oper->get_optype(),
680  bin_oper->get_qualifier(),
681  lhs,
682  rhs);
683  }
684 
685  std::shared_ptr<Analyzer::Expr> visitStringOper(
686  const Analyzer::StringOper* string_oper) const override {
687  // Todo(todd): For clarity and modularity we should move string
688  // operator rewrites into their own visitor class.
689  // String operation rewrites were originally put here as they only
690  // handled string operators on rewrite, but now handle variable
691  // inputs as well.
692  const auto original_args = string_oper->getOwnArgs();
693  std::vector<std::shared_ptr<Analyzer::Expr>> rewritten_args;
694  const bool parent_in_string_op_chain = in_string_op_chain_;
695  if (!parent_in_string_op_chain) {
696  chained_string_op_exprs_.clear();
697  in_string_op_chain_ = true;
698  }
699  size_t rewritten_arg_literal_arity = 0;
700  for (auto original_arg : original_args) {
701  rewritten_args.emplace_back(visit(original_arg.get()));
702  if (dynamic_cast<const Analyzer::Constant*>(rewritten_args.back().get())) {
703  rewritten_arg_literal_arity++;
704  }
705  }
706  if (!parent_in_string_op_chain) {
707  in_string_op_chain_ = false;
708  }
709  const auto kind = string_oper->get_kind();
710 
711  if (string_oper->getArity() == rewritten_arg_literal_arity) {
712  Analyzer::StringOper literal_string_oper(kind, rewritten_args);
713  const auto literal_args = literal_string_oper.getLiteralArgs();
714  const auto string_op_info = StringOps_Namespace::StringOpInfo(kind, literal_args);
715  const auto literal_result =
717  return Parser::StringLiteral::analyzeValue(literal_result.first,
718  literal_result.second /* is null */);
719  }
720  chained_string_op_exprs_.emplace_back(
721  makeExpr<Analyzer::StringOper>(kind, rewritten_args));
722  if (parent_in_string_op_chain) {
723  CHECK(in_string_op_chain_);
724  CHECK(rewritten_args[0]->get_type_info().is_string());
725  CHECK(
726  dynamic_cast<const Analyzer::ColumnVar*>(remove_cast(rewritten_args[0].get())));
727  return rewritten_args[0]->deep_copy();
728  } else {
729  CHECK(!in_string_op_chain_);
730  return makeExpr<Analyzer::StringOper>(
731  kind, rewritten_args, chained_string_op_exprs_);
732  }
733  }
734 
735  protected:
736  mutable bool in_string_op_chain_{false};
737  mutable std::vector<std::shared_ptr<Analyzer::Expr>> chained_string_op_exprs_;
738  mutable std::unordered_map<const Analyzer::Expr*, const SQLTypeInfo> casts_;
739  mutable int32_t num_overflows_;
740 
741  public:
742  ConstantFoldingVisitor() : num_overflows_(0) {}
743  int32_t get_num_overflows() { return num_overflows_; }
744  void reset_num_overflows() { num_overflows_ = 0; }
745 };
746 
748  const auto with_likelihood = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
749  if (!with_likelihood) {
750  return expr;
751  }
752  return with_likelihood->get_arg();
753 }
754 
755 } // namespace
756 
758  return ArrayElementStringLiteralEncodingVisitor().visit(expr);
759 }
760 
762  const auto sum_window = rewrite_sum_window(expr);
763  if (sum_window) {
764  return sum_window;
765  }
766  const auto avg_window = rewrite_avg_window(expr);
767  if (avg_window) {
768  return avg_window;
769  }
770  const auto expr_no_likelihood = strip_likelihood(expr);
771  // The following check is not strictly needed, but seems silly to transform a
772  // simple string comparison to an IN just to codegen the same thing anyway.
773 
774  RecursiveOrToInVisitor visitor;
775  auto rewritten_expr = visitor.visit(expr_no_likelihood);
776  const auto expr_with_likelihood =
777  std::dynamic_pointer_cast<const Analyzer::LikelihoodExpr>(rewritten_expr);
778  if (expr_with_likelihood) {
779  // Add back likelihood
780  return std::make_shared<Analyzer::LikelihoodExpr>(
781  rewritten_expr, expr_with_likelihood->get_likelihood());
782  }
783  return rewritten_expr;
784 }
785 
786 namespace {
787 
788 static const std::unordered_set<std::string> overlaps_supported_functions = {
789  "ST_Contains_MultiPolygon_Point",
790  "ST_Contains_Polygon_Point",
791  "ST_cContains_MultiPolygon_Point", // compressed coords version
792  "ST_cContains_Polygon_Point",
793  "ST_Contains_Polygon_Polygon",
794  "ST_Contains_Polygon_MultiPolygon",
795  "ST_Contains_MultiPolygon_MultiPolygon",
796  "ST_Contains_MultiPolygon_Polygon",
797  "ST_Intersects_Polygon_Point",
798  "ST_cIntersects_Polygon_Point",
799  "ST_Intersects_Polygon_Polygon",
800  "ST_Intersects_Polygon_MultiPolygon",
801  "ST_Intersects_MultiPolygon_MultiPolygon",
802  "ST_Intersects_MultiPolygon_Polygon",
803  "ST_Intersects_MultiPolygon_Point",
804  "ST_cIntersects_MultiPolygon_Point",
805  "ST_Approx_Overlaps_MultiPolygon_Point",
806  "ST_Overlaps"};
807 
808 static const std::unordered_set<std::string> requires_many_to_many = {
809  "ST_Contains_Polygon_Polygon",
810  "ST_Contains_Polygon_MultiPolygon",
811  "ST_Contains_MultiPolygon_MultiPolygon",
812  "ST_Contains_MultiPolygon_Polygon",
813  "ST_Intersects_Polygon_Polygon",
814  "ST_Intersects_Polygon_MultiPolygon",
815  "ST_Intersects_MultiPolygon_MultiPolygon",
816  "ST_Intersects_MultiPolygon_Polygon"};
817 
818 } // namespace
819 
820 boost::optional<OverlapsJoinConjunction> rewrite_overlaps_conjunction(
821  const std::shared_ptr<Analyzer::Expr> expr) {
822  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(expr.get());
823  if (func_oper) {
824  const auto needs_many_many = [func_oper]() {
825  return requires_many_to_many.find(func_oper->getName()) !=
826  requires_many_to_many.end();
827  };
828  // TODO(adb): consider converting unordered set to an unordered map, potentially
829  // storing the rewrite function we want to apply in the map
830  if (overlaps_supported_functions.find(func_oper->getName()) !=
832  if (!g_enable_hashjoin_many_to_many && needs_many_many()) {
833  LOG(WARNING) << "Many-to-many hashjoin support is disabled, unable to rewrite "
834  << func_oper->toString() << " to use accelerated geo join.";
835  return boost::none;
836  }
837 
838  DeepCopyVisitor deep_copy_visitor;
839  if (func_oper->getName() == "ST_Overlaps") {
840  CHECK_GE(func_oper->getArity(), size_t(2));
841  // return empty quals, overlaps join quals
842  // TODO(adb): we will likely want to actually check for true overlaps, but this
843  // works for now
844 
845  auto lhs = func_oper->getOwnArg(0);
846  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
847  CHECK(rewritten_lhs);
848 
849  auto rhs = func_oper->getOwnArg(1);
850  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
851  CHECK(rewritten_rhs);
852 
853  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
854  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
855  return OverlapsJoinConjunction{{}, {overlaps_oper}};
856  }
857 
858  // TODO(jclay): This will work for Poly_Poly,but needs to change for others.
859  CHECK_GE(func_oper->getArity(), size_t(4));
860  if (func_oper->getName() == "ST_Contains_Polygon_Polygon" ||
861  func_oper->getName() == "ST_Intersects_Polygon_Polygon" ||
862  func_oper->getName() == "ST_Intersects_MultiPolygon_MultiPolygon" ||
863  func_oper->getName() == "ST_Intersects_MultiPolygon_Polygon" ||
864  func_oper->getName() == "ST_Intersects_Polygon_MultiPolygon") {
865  auto lhs = func_oper->getOwnArg(3);
866  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
867  CHECK(rewritten_lhs);
868  auto rhs = func_oper->getOwnArg(1);
869  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
870  CHECK(rewritten_rhs);
871 
872  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
873  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
874 
875  VLOG(1) << "Successfully converted to overlaps join";
876  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
877  }
878 
879  auto lhs = func_oper->getOwnArg(2);
880  auto rewritten_lhs = deep_copy_visitor.visit(lhs.get());
881  CHECK(rewritten_lhs);
882  const auto& lhs_ti = rewritten_lhs->get_type_info();
883 
884  if (!lhs_ti.is_geometry() && !is_constructed_point(rewritten_lhs.get())) {
885  // TODO(adb): If ST_Contains is passed geospatial literals instead of columns, the
886  // function will be expanded during translation rather than during code
887  // generation. While this scenario does not make sense for the overlaps join, we
888  // need to detect and abort the overlaps rewrite. Adding a GeospatialConstant
889  // dervied class to the Analyzer may prove to be a better way to handle geo
890  // literals, but for now we ensure the LHS type is a geospatial type, which would
891  // mean the function has not been expanded to the physical types, yet.
892 
893  LOG(INFO) << "Unable to rewrite " << func_oper->getName()
894  << " to overlaps conjunction. LHS input type is neither a geospatial "
895  "column nor a constructed point\n"
896  << func_oper->toString();
897 
898  return boost::none;
899  }
900 
901  // Read the bounds arg from the ST_Contains FuncOper (second argument)instead of the
902  // poly column (first argument)
903  auto rhs = func_oper->getOwnArg(1);
904  auto rewritten_rhs = deep_copy_visitor.visit(rhs.get());
905  CHECK(rewritten_rhs);
906 
907  // Check for compatible join ordering. If the join ordering does not match expected
908  // ordering for overlaps, the join builder will fail.
909  std::set<int> lhs_rte_idx;
910  lhs->collect_rte_idx(lhs_rte_idx);
911  CHECK(!lhs_rte_idx.empty());
912  std::set<int> rhs_rte_idx;
913  rhs->collect_rte_idx(rhs_rte_idx);
914  CHECK(!rhs_rte_idx.empty());
915 
916  if (lhs_rte_idx.size() > 1 || rhs_rte_idx.size() > 1 || lhs_rte_idx > rhs_rte_idx) {
917  LOG(INFO) << "Unable to rewrite " << func_oper->getName()
918  << " to overlaps conjunction. Cannot build hash table over LHS type. "
919  "Check join order.\n"
920  << func_oper->toString();
921  return boost::none;
922  }
923 
924  VLOG(1) << "Rewritten to use overlaps join with lhs as "
925  << rewritten_lhs->toString() << " and rhs as " << rewritten_rhs->toString();
926 
927  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
928  kBOOLEAN, kOVERLAPS, kONE, rewritten_lhs, rewritten_rhs);
929 
930  VLOG(1) << "Successfully converted to overlaps join";
931  if (func_oper->getName() == "ST_Approx_Overlaps_MultiPolygon_Point"sv) {
932  return OverlapsJoinConjunction{{}, {overlaps_oper}};
933  } else {
934  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
935  }
936  } else {
937  VLOG(1) << "Overlaps join not enabled for " << func_oper->getName();
938  }
939  return boost::none;
940  }
941  auto bin_oper = dynamic_cast<Analyzer::BinOper*>(expr.get());
942  if (g_enable_distance_rangejoin && bin_oper &&
943  (bin_oper->get_optype() == kLE || bin_oper->get_optype() == kLT)) {
944  auto lhs = dynamic_cast<const Analyzer::GeoOperator*>(bin_oper->get_left_operand());
945  auto rhs = dynamic_cast<const Analyzer::Constant*>(bin_oper->get_right_operand());
946  if (lhs && rhs && lhs->getName() == "ST_Distance") {
947  CHECK_EQ(lhs->size(), size_t(2));
948  auto l_arg = lhs->getOperand(0);
949  auto r_arg = lhs->getOperand(1);
950  const bool is_geography = l_arg->get_type_info().get_subtype() == kGEOGRAPHY ||
951  r_arg->get_type_info().get_subtype() == kGEOGRAPHY;
952  if (is_geography) {
953  VLOG(1) << "Range join not yet supported for geodesic distance "
954  << bin_oper->toString();
955  return boost::none;
956  }
957 
958  // Check for compatible join ordering. If the join ordering does not match expected
959  // ordering for overlaps, the join builder will fail.
960  std::set<int> lhs_rte_idx;
961  l_arg->collect_rte_idx(lhs_rte_idx);
962  CHECK(!lhs_rte_idx.empty());
963  std::set<int> rhs_rte_idx;
964  r_arg->collect_rte_idx(rhs_rte_idx);
965  CHECK(!rhs_rte_idx.empty());
966 
967  if (lhs_rte_idx.size() > 1 || rhs_rte_idx.size() > 1 || lhs_rte_idx > rhs_rte_idx) {
968  LOG(INFO) << "Unable to rewrite " << lhs->getName()
969  << " to overlaps conjunction. Cannot build hash table over LHS type. "
970  "Check join order.\n"
971  << bin_oper->toString();
972  return boost::none;
973  }
974 
975  const bool inclusive = bin_oper->get_optype() == kLE;
976  auto range_expr = makeExpr<Analyzer::RangeOper>(
977  inclusive, inclusive, r_arg->deep_copy(), rhs->deep_copy());
978  auto overlaps_oper = makeExpr<Analyzer::BinOper>(
979  kBOOLEAN, kOVERLAPS, kONE, l_arg->deep_copy(), range_expr);
980  return OverlapsJoinConjunction{{expr}, {overlaps_oper}};
981  }
982  }
983  return boost::none;
984 }
985 
995  public:
997  for (const auto& join_condition : join_quals) {
998  for (const auto& qual : join_condition.quals) {
999  auto qual_bin_oper = dynamic_cast<Analyzer::BinOper*>(qual.get());
1000  if (qual_bin_oper) {
1001  join_qual_pairs.emplace_back(qual_bin_oper->get_left_operand(),
1002  qual_bin_oper->get_right_operand());
1003  }
1004  }
1005  }
1006  }
1007 
1008  bool visitFunctionOper(const Analyzer::FunctionOper* func_oper) const override {
1009  if (overlaps_supported_functions.find(func_oper->getName()) !=
1011  const auto lhs = func_oper->getArg(2);
1012  const auto rhs = func_oper->getArg(1);
1013  for (const auto& qual_pair : join_qual_pairs) {
1014  if (*lhs == *qual_pair.first && *rhs == *qual_pair.second) {
1015  return true;
1016  }
1017  }
1018  }
1019  return false;
1020  }
1021 
1022  bool defaultResult() const override { return false; }
1023 
1024  private:
1025  std::vector<std::pair<const Analyzer::Expr*, const Analyzer::Expr*>> join_qual_pairs;
1026 };
1027 
1028 std::list<std::shared_ptr<Analyzer::Expr>> strip_join_covered_filter_quals(
1029  const std::list<std::shared_ptr<Analyzer::Expr>>& quals,
1030  const JoinQualsPerNestingLevel& join_quals) {
1032  return quals;
1033  }
1034 
1035  if (join_quals.empty()) {
1036  return quals;
1037  }
1038 
1039  std::list<std::shared_ptr<Analyzer::Expr>> quals_to_return;
1040 
1041  JoinCoveredQualVisitor visitor(join_quals);
1042  for (const auto& qual : quals) {
1043  if (!visitor.visit(qual.get())) {
1044  // Not a covered qual, don't elide it from the filtered count
1045  quals_to_return.push_back(qual);
1046  }
1047  }
1048 
1049  return quals_to_return;
1050 }
1051 
1052 std::shared_ptr<Analyzer::Expr> fold_expr(const Analyzer::Expr* expr) {
1053  if (!expr) {
1054  return nullptr;
1055  }
1056  const auto expr_no_likelihood = strip_likelihood(expr);
1057  ConstantFoldingVisitor visitor;
1058  auto rewritten_expr = visitor.visit(expr_no_likelihood);
1059  if (visitor.get_num_overflows() > 0 && rewritten_expr->get_type_info().is_integer() &&
1060  rewritten_expr->get_type_info().get_type() != kBIGINT) {
1061  auto rewritten_expr_const =
1062  std::dynamic_pointer_cast<const Analyzer::Constant>(rewritten_expr);
1063  if (!rewritten_expr_const) {
1064  // Integer expression didn't fold completely the first time due to
1065  // overflows in smaller type subexpressions, trying again with a cast
1066  const auto& ti = SQLTypeInfo(kBIGINT, false);
1067  auto bigint_expr_no_likelihood = expr_no_likelihood->deep_copy()->add_cast(ti);
1068  auto rewritten_expr_take2 = visitor.visit(bigint_expr_no_likelihood.get());
1069  auto rewritten_expr_take2_const =
1070  std::dynamic_pointer_cast<Analyzer::Constant>(rewritten_expr_take2);
1071  if (rewritten_expr_take2_const) {
1072  // Managed to fold, switch to the new constant
1073  rewritten_expr = rewritten_expr_take2_const;
1074  }
1075  }
1076  }
1077  const auto expr_with_likelihood = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
1078  if (expr_with_likelihood) {
1079  // Add back likelihood
1080  return std::make_shared<Analyzer::LikelihoodExpr>(
1081  rewritten_expr, expr_with_likelihood->get_likelihood());
1082  }
1083  return rewritten_expr;
1084 }
1085 
1087  const Analyzer::ColumnVar* val_side,
1088  const int max_rte_covered) {
1089  if (key_side->get_table_id() == val_side->get_table_id() &&
1090  key_side->get_rte_idx() == val_side->get_rte_idx() &&
1091  key_side->get_rte_idx() > max_rte_covered) {
1092  return true;
1093  }
1094  return false;
1095 }
1096 
1098  std::unordered_map<int, llvm::Value*>& scan_idx_to_hash_pos) {
1099  int ret = INT32_MIN;
1100  for (auto& kv : scan_idx_to_hash_pos) {
1101  if (kv.first > ret) {
1102  ret = kv.first;
1103  }
1104  }
1105  return ret;
1106 }
int8_t tinyintval
Definition: sqltypes.h:212
int get_table_id() const
Definition: Analyzer.h:200
Defines data structures for the semantic analysis phase of query processing.
Analyzer::ExpressionPtr rewrite_array_elements(Analyzer::Expr const *expr)
#define CHECK_EQ(x, y)
Definition: Logger.h:230
#define IS_LOGIC(X)
Definition: sqldefs.h:60
std::shared_ptr< Analyzer::InValues > visitLikeExpr(const Analyzer::LikeExpr *) const override
std::shared_ptr< Analyzer::InValues > visitDateaddExpr(const Analyzer::DateaddExpr *) const override
bool self_join_not_covered_by_left_deep_tree(const Analyzer::ColumnVar *key_side, const Analyzer::ColumnVar *val_side, const int max_rte_covered)
std::shared_ptr< Analyzer::Expr > remove_cast(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:4196
SQLTypes
Definition: sqltypes.h:38
std::shared_ptr< Analyzer::WindowFunction > rewrite_avg_window(const Analyzer::Expr *expr)
bool g_strip_join_covered_quals
Definition: Execute.cpp:107
std::shared_ptr< Analyzer::InValues > visitKeyForString(const Analyzer::KeyForStringExpr *) const override
std::shared_ptr< Analyzer::WindowFunction > rewrite_sum_window(const Analyzer::Expr *expr)
std::shared_ptr< Analyzer::InValues > visitBinOper(const Analyzer::BinOper *bin_oper) const override
#define LOG(tag)
Definition: Logger.h:216
const Expr * get_right_operand() const
Definition: Analyzer.h:450
bool is_constructed_point(const Analyzer::Expr *expr)
Definition: Execute.h:1427
SQLOps
Definition: sqldefs.h:28
static const std::unordered_set< std::string > requires_many_to_many
Definition: sqldefs.h:34
int8_t boolval
Definition: sqltypes.h:211
Definition: sqldefs.h:35
Definition: sqldefs.h:37
#define CHECK_GE(x, y)
Definition: Logger.h:235
bool get_contains_agg() const
Definition: Analyzer.h:83
Definition: sqldefs.h:48
Definition: sqldefs.h:29
std::shared_ptr< Analyzer::Expr > ExpressionPtr
Definition: Analyzer.h:186
size_t getArity() const
Definition: Analyzer.h:1506
const Analyzer::Expr * extract_cast_arg(const Analyzer::Expr *expr)
Definition: Execute.h:201
Definition: sqldefs.h:40
std::vector< JoinCondition > JoinQualsPerNestingLevel
T visit(const Analyzer::Expr *expr) const
Analyzer::ExpressionPtr rewrite_expr(const Analyzer::Expr *expr)
static const std::unordered_set< std::string > overlaps_supported_functions
bool isNull() const
Definition: Analyzer.h:2367
std::shared_ptr< Analyzer::InValues > aggregateResult(const std::shared_ptr< Analyzer::InValues > &lhs, const std::shared_ptr< Analyzer::InValues > &rhs) const override
std::list< std::shared_ptr< Analyzer::Expr > > strip_join_covered_filter_quals(const std::list< std::shared_ptr< Analyzer::Expr >> &quals, const JoinQualsPerNestingLevel &join_quals)
std::shared_ptr< Analyzer::InValues > visitUOper(const Analyzer::UOper *uoper) const override
const int get_max_rte_scan_table(std::unordered_map< int, llvm::Value * > &scan_idx_to_hash_pos)
std::shared_ptr< Analyzer::Expr > visitBinOper(const Analyzer::BinOper *bin_oper) const override
std::shared_ptr< Analyzer::Expr > RetType
int32_t intval
Definition: sqltypes.h:214
constexpr double f
Definition: Utm.h:31
std::shared_ptr< Analyzer::InValues > visitCharLength(const Analyzer::CharLengthExpr *) const override
std::shared_ptr< Analyzer::InValues > visitInIntegerSet(const Analyzer::InIntegerSet *) const override
SQLOps get_optype() const
Definition: Analyzer.h:446
float floatval
Definition: sqltypes.h:216
std::shared_ptr< Analyzer::Expr > visitUOper(const Analyzer::UOper *uoper) const override
LiteralArgMap getLiteralArgs() const
Definition: Analyzer.cpp:3960
Classes representing a parse tree.
bool g_enable_hashjoin_many_to_many
Definition: Execute.cpp:104
std::shared_ptr< Analyzer::InValues > visitRegexpExpr(const Analyzer::RegexpExpr *) const override
int64_t bigintval
Definition: sqltypes.h:215
bool g_enable_distance_rangejoin
Definition: Execute.cpp:103
std::shared_ptr< Analyzer::InValues > visitAggExpr(const Analyzer::AggExpr *) const override
bool visitFunctionOper(const Analyzer::FunctionOper *func_oper) const override
Definition: sqldefs.h:36
bool foldOper(SQLOps optype, SQLTypes type, Datum lhs, Datum rhs, Datum &result, SQLTypes &result_type) const
int16_t smallintval
Definition: sqltypes.h:213
std::pair< std::string, bool > apply_string_op_to_literals(const StringOpInfo &string_op_info)
Definition: StringOps.cpp:617
bool defaultResult() const override
boost::optional< OverlapsJoinConjunction > rewrite_overlaps_conjunction(const std::shared_ptr< Analyzer::Expr > expr)
std::shared_ptr< Analyzer::Expr > visitStringOper(const Analyzer::StringOper *string_oper) const override
RetType visitArrayOper(const Analyzer::ArrayExpr *array_expr) const override
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
std::shared_ptr< Analyzer::Expr > visitBinOper(const Analyzer::BinOper *bin_oper) const override
size_t getElementCount() const
Definition: Analyzer.h:2365
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:493
Definition: sqldefs.h:33
std::vector< std::shared_ptr< Analyzer::Expr > > chained_string_op_exprs_
Definition: sqldefs.h:39
std::vector< std::pair< const Analyzer::Expr *, const Analyzer::Expr * > > join_qual_pairs
Definition: sqldefs.h:70
Expression class for string functions The &quot;arg&quot; constructor parameter must be an expression that reso...
Definition: Analyzer.h:1463
static std::shared_ptr< Analyzer::Expr > analyzeValue(const std::string &stringval, const bool is_null)
Definition: ParserNode.cpp:142
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:259
bool isLocalAlloc() const
Definition: Analyzer.h:2366
SqlStringOpKind get_kind() const
Definition: Analyzer.h:1504
const Analyzer::Expr * getArg(const size_t i) const
Definition: Analyzer.h:2171
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
int get_rte_idx() const
Definition: Analyzer.h:202
const Expr * get_operand() const
Definition: Analyzer.h:378
Datum get_constval() const
Definition: Analyzer.h:342
Definition: sqldefs.h:31
std::unordered_map< const Analyzer::Expr *, const SQLTypeInfo > casts_
std::shared_ptr< Analyzer::InValues > visitDatediffExpr(const Analyzer::DatediffExpr *) const override
std::shared_ptr< Analyzer::InValues > visitLikelihood(const Analyzer::LikelihoodExpr *) const override
std::shared_ptr< Analyzer::InValues > visitInValues(const Analyzer::InValues *) const override
#define CHECK(condition)
Definition: Logger.h:222
Definition: sqldefs.h:32
const Expr * get_left_operand() const
Definition: Analyzer.h:449
Common Enum definitions for SQL processing.
std::shared_ptr< Analyzer::InValues > visitCaseExpr(const Analyzer::CaseExpr *) const override
Definition: sqltypes.h:45
JoinCoveredQualVisitor(const JoinQualsPerNestingLevel &join_quals)
const std::shared_ptr< Analyzer::Expr > get_own_right_operand() const
Definition: Analyzer.h:454
std::string getName() const
Definition: Analyzer.h:2167
bool is_decimal() const
Definition: sqltypes.h:513
std::shared_ptr< Analyzer::InValues > visitDatetruncExpr(const Analyzer::DatetruncExpr *) const override
Definition: sqldefs.h:38
const std::shared_ptr< Analyzer::Expr > get_own_left_operand() const
Definition: Analyzer.h:451
std::vector< std::shared_ptr< Analyzer::Expr > > getOwnArgs() const
Definition: Analyzer.h:1528
SQLOps get_optype() const
Definition: Analyzer.h:377
#define VLOG(n)
Definition: Logger.h:316
std::shared_ptr< Analyzer::Expr > fold_expr(const Analyzer::Expr *expr)
#define IS_COMPARISON(X)
Definition: sqldefs.h:57
double doubleval
Definition: sqltypes.h:217
std::shared_ptr< Analyzer::InValues > visitExtractExpr(const Analyzer::ExtractExpr *) const override
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:2369
SQLQualifier get_qualifier() const
Definition: Analyzer.h:448
const Analyzer::Expr * strip_likelihood(const Analyzer::Expr *expr)
std::shared_ptr< Analyzer::InValues > visitSampleRatio(const Analyzer::SampleRatioExpr *) const override
std::shared_ptr< Analyzer::InValues > visitCardinality(const Analyzer::CardinalityExpr *) const override