OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor Class Reference
+ Inheritance diagram for anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor:
+ Collaboration diagram for anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor:

Public Member Functions

 ConstantFoldingVisitor ()
 
int32_t get_num_overflows ()
 
void reset_num_overflows ()
 
- Public Member Functions inherited from ScalarExprVisitor< std::shared_ptr< Analyzer::Expr > >
std::shared_ptr< Analyzer::Exprvisit (const Analyzer::Expr *expr) const
 

Protected Attributes

bool in_string_op_chain_ {false}
 
std::vector< std::shared_ptr
< Analyzer::Expr > > 
chained_string_op_exprs_
 
std::unordered_map< const
Analyzer::Expr *, const
SQLTypeInfo
casts_
 
int32_t num_overflows_
 

Private Member Functions

template<typename T >
bool foldComparison (SQLOps optype, T t1, T t2) const
 
template<typename T >
bool foldLogic (SQLOps optype, T t1, T t2) const
 
template<typename T >
foldArithmetic (SQLOps optype, T t1, T t2) const
 
bool foldOper (SQLOps optype, SQLTypes type, Datum lhs, Datum rhs, Datum &result, SQLTypes &result_type) const
 
std::shared_ptr< Analyzer::ExprvisitUOper (const Analyzer::UOper *uoper) const override
 
std::shared_ptr< Analyzer::ExprvisitBinOper (const Analyzer::BinOper *bin_oper) const override
 
std::shared_ptr< Analyzer::ExprvisitStringOper (const Analyzer::StringOper *string_oper) const override
 

Additional Inherited Members

- Protected Types inherited from DeepCopyVisitor
using RetType = std::shared_ptr< Analyzer::Expr >
 
- Protected Member Functions inherited from DeepCopyVisitor
RetType visitColumnVar (const Analyzer::ColumnVar *col_var) const override
 
RetType visitColumnVarTuple (const Analyzer::ExpressionTuple *col_var_tuple) const override
 
RetType visitVar (const Analyzer::Var *var) const override
 
RetType visitConstant (const Analyzer::Constant *constant) const override
 
RetType visitGeoExpr (const Analyzer::GeoExpr *geo_expr) const override
 
RetType visitInValues (const Analyzer::InValues *in_values) const override
 
RetType visitInIntegerSet (const Analyzer::InIntegerSet *in_integer_set) const override
 
RetType visitCharLength (const Analyzer::CharLengthExpr *char_length) const override
 
RetType visitKeyForString (const Analyzer::KeyForStringExpr *expr) const override
 
RetType visitSampleRatio (const Analyzer::SampleRatioExpr *expr) const override
 
RetType visitCardinality (const Analyzer::CardinalityExpr *cardinality) const override
 
RetType visitLikeExpr (const Analyzer::LikeExpr *like) const override
 
RetType visitRegexpExpr (const Analyzer::RegexpExpr *regexp) const override
 
RetType visitWidthBucket (const Analyzer::WidthBucketExpr *width_bucket_expr) const override
 
RetType visitCaseExpr (const Analyzer::CaseExpr *case_expr) const override
 
RetType visitDatetruncExpr (const Analyzer::DatetruncExpr *datetrunc) const override
 
RetType visitExtractExpr (const Analyzer::ExtractExpr *extract) const override
 
RetType visitArrayOper (const Analyzer::ArrayExpr *array_expr) const override
 
RetType visitGeoUOper (const Analyzer::GeoUOper *geo_expr) const override
 
RetType visitGeoBinOper (const Analyzer::GeoBinOper *geo_expr) const override
 
RetType visitWindowFunction (const Analyzer::WindowFunction *window_func) const override
 
RetType visitFunctionOper (const Analyzer::FunctionOper *func_oper) const override
 
RetType visitDatediffExpr (const Analyzer::DatediffExpr *datediff) const override
 
RetType visitDateaddExpr (const Analyzer::DateaddExpr *dateadd) const override
 
RetType visitFunctionOperWithCustomTypeHandling (const Analyzer::FunctionOperWithCustomTypeHandling *func_oper) const override
 
RetType visitLikelihood (const Analyzer::LikelihoodExpr *likelihood) const override
 
RetType visitAggExpr (const Analyzer::AggExpr *agg) const override
 
RetType visitOffsetInFragment (const Analyzer::OffsetInFragment *) const override
 
- Protected Member Functions inherited from ScalarExprVisitor< std::shared_ptr< Analyzer::Expr > >
virtual std::shared_ptr
< Analyzer::Expr
visitRangeJoinOper (const Analyzer::RangeOper *range_oper) const
 
virtual std::shared_ptr
< Analyzer::Expr
aggregateResult (const std::shared_ptr< Analyzer::Expr > &aggregate, const std::shared_ptr< Analyzer::Expr > &next_result) const
 
virtual void visitBegin () const
 
virtual std::shared_ptr
< Analyzer::Expr
defaultResult () const
 

Detailed Description

Definition at line 214 of file ExpressionRewrite.cpp.

Constructor & Destructor Documentation

anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::ConstantFoldingVisitor ( )
inline

Member Function Documentation

template<typename T >
T anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::foldArithmetic ( SQLOps  optype,
t1,
t2 
) const
inlineprivate

Definition at line 254 of file ExpressionRewrite.cpp.

References kDIVIDE, kMINUS, kMULTIPLY, and kPLUS.

254  {
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  }
Definition: sqldefs.h:40
Definition: sqldefs.h:39
template<typename T >
bool anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::foldComparison ( SQLOps  optype,
t1,
t2 
) const
inlineprivate

Definition at line 216 of file ExpressionRewrite.cpp.

References kEQ, kGE, kGT, kLE, kLT, and kNE.

216  {
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  }
Definition: sqldefs.h:34
Definition: sqldefs.h:35
Definition: sqldefs.h:29
Definition: sqldefs.h:33
Definition: sqldefs.h:31
Definition: sqldefs.h:32
template<typename T >
bool anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::foldLogic ( SQLOps  optype,
t1,
t2 
) const
inlineprivate

Definition at line 238 of file ExpressionRewrite.cpp.

References kAND, kNOT, and kOR.

238  {
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  }
Definition: sqldefs.h:37
Definition: sqldefs.h:36
Definition: sqldefs.h:38
bool anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::foldOper ( SQLOps  optype,
SQLTypes  type,
Datum  lhs,
Datum  rhs,
Datum result,
SQLTypes result_type 
) const
inlineprivate

Definition at line 313 of file ExpressionRewrite.cpp.

References Datum::bigintval, Datum::boolval, CHECK, Datum::doubleval, Datum::floatval, Datum::intval, IS_ARITHMETIC, IS_COMPARISON, IS_LOGIC, kBIGINT, kBOOLEAN, kDOUBLE, kFLOAT, kINT, kSMALLINT, kTINYINT, Datum::smallintval, Datum::tinyintval, and run_benchmark_import::type.

318  {
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  }
int8_t tinyintval
Definition: sqltypes.h:212
#define IS_LOGIC(X)
Definition: sqldefs.h:60
int8_t boolval
Definition: sqltypes.h:211
int32_t intval
Definition: sqltypes.h:214
float floatval
Definition: sqltypes.h:216
int64_t bigintval
Definition: sqltypes.h:215
int16_t smallintval
Definition: sqltypes.h:213
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
#define CHECK(condition)
Definition: Logger.h:222
Definition: sqltypes.h:45
#define IS_COMPARISON(X)
Definition: sqldefs.h:57
double doubleval
Definition: sqltypes.h:217
int32_t anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::get_num_overflows ( )
inline
void anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::reset_num_overflows ( )
inline
std::shared_ptr<Analyzer::Expr> anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::visitBinOper ( const Analyzer::BinOper bin_oper) const
inlineoverrideprivatevirtual

Reimplemented from DeepCopyVisitor.

Definition at line 510 of file ExpressionRewrite.cpp.

References Datum::boolval, CHECK, decimal_to_int_type(), anonymous_namespace{Utm.h}::f, Analyzer::Expr::get_contains_agg(), Analyzer::BinOper::get_left_operand(), Analyzer::BinOper::get_optype(), Analyzer::BinOper::get_own_left_operand(), Analyzer::BinOper::get_own_right_operand(), Analyzer::BinOper::get_qualifier(), Analyzer::Expr::get_type_info(), IS_COMPARISON, SQLTypeInfo::is_decimal(), kAND, kBOOLEAN, kDIVIDE, kDOUBLE, kEQ, kFLOAT, kGE, kGT, kLE, kLT, kMINUS, kMULTIPLY, kNE, kOR, and kPLUS.

511  {
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  }
SQLTypes
Definition: sqltypes.h:38
Definition: sqldefs.h:34
int8_t boolval
Definition: sqltypes.h:211
Definition: sqldefs.h:35
Definition: sqldefs.h:37
bool get_contains_agg() const
Definition: Analyzer.h:83
Definition: sqldefs.h:29
Definition: sqldefs.h:40
std::shared_ptr< Analyzer::Expr > visit(const Analyzer::Expr *expr) const
constexpr double f
Definition: Utm.h:31
SQLOps get_optype() const
Definition: Analyzer.h:446
Definition: sqldefs.h:36
bool foldOper(SQLOps optype, SQLTypes type, Datum lhs, Datum rhs, Datum &result, SQLTypes &result_type) const
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:493
Definition: sqldefs.h:33
Definition: sqldefs.h:39
Definition: sqldefs.h:31
std::unordered_map< const Analyzer::Expr *, const SQLTypeInfo > casts_
#define CHECK(condition)
Definition: Logger.h:222
Definition: sqldefs.h:32
const Expr * get_left_operand() const
Definition: Analyzer.h:449
const std::shared_ptr< Analyzer::Expr > get_own_right_operand() const
Definition: Analyzer.h:454
bool is_decimal() const
Definition: sqltypes.h:513
const std::shared_ptr< Analyzer::Expr > get_own_left_operand() const
Definition: Analyzer.h:451
#define IS_COMPARISON(X)
Definition: sqldefs.h:57
SQLQualifier get_qualifier() const
Definition: Analyzer.h:448

+ Here is the call graph for this function:

std::shared_ptr<Analyzer::Expr> anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::visitStringOper ( const Analyzer::StringOper string_oper) const
inlineoverrideprivatevirtual

Reimplemented from DeepCopyVisitor.

Definition at line 685 of file ExpressionRewrite.cpp.

References Parser::StringLiteral::analyzeValue(), StringOps_Namespace::apply_string_op_to_literals(), CHECK, Analyzer::StringOper::get_kind(), Analyzer::StringOper::getArity(), Analyzer::StringOper::getLiteralArgs(), Analyzer::StringOper::getOwnArgs(), and remove_cast().

686  {
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) {
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 {
730  return makeExpr<Analyzer::StringOper>(
731  kind, rewritten_args, chained_string_op_exprs_);
732  }
733  }
std::shared_ptr< Analyzer::Expr > remove_cast(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:4196
size_t getArity() const
Definition: Analyzer.h:1506
std::shared_ptr< Analyzer::Expr > visit(const Analyzer::Expr *expr) const
std::pair< std::string, bool > apply_string_op_to_literals(const StringOpInfo &string_op_info)
Definition: StringOps.cpp:617
std::vector< std::shared_ptr< Analyzer::Expr > > chained_string_op_exprs_
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
SqlStringOpKind get_kind() const
Definition: Analyzer.h:1504
#define CHECK(condition)
Definition: Logger.h:222
std::vector< std::shared_ptr< Analyzer::Expr > > getOwnArgs() const
Definition: Analyzer.h:1528

+ Here is the call graph for this function:

std::shared_ptr<Analyzer::Expr> anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::visitUOper ( const Analyzer::UOper uoper) const
inlineoverrideprivatevirtual

Reimplemented from DeepCopyVisitor.

Definition at line 430 of file ExpressionRewrite.cpp.

References CHECK_EQ, decimal_to_int_type(), Analyzer::Constant::get_constval(), Analyzer::Expr::get_contains_agg(), Analyzer::UOper::get_operand(), Analyzer::UOper::get_optype(), Analyzer::Expr::get_type_info(), kBOOLEAN, kCAST, kEQ, kMINUS, kNOT, and kUMINUS.

431  {
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  }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
SQLTypes
Definition: sqltypes.h:38
bool get_contains_agg() const
Definition: Analyzer.h:83
Definition: sqldefs.h:48
Definition: sqldefs.h:29
std::shared_ptr< Analyzer::Expr > visit(const Analyzer::Expr *expr) const
bool foldOper(SQLOps optype, SQLTypes type, Datum lhs, Datum rhs, Datum &result, SQLTypes &result_type) const
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:493
Definition: sqldefs.h:39
const Expr * get_operand() const
Definition: Analyzer.h:378
Datum get_constval() const
Definition: Analyzer.h:342
std::unordered_map< const Analyzer::Expr *, const SQLTypeInfo > casts_
Definition: sqldefs.h:38
SQLOps get_optype() const
Definition: Analyzer.h:377

+ Here is the call graph for this function:

Member Data Documentation

std::unordered_map<const Analyzer::Expr*, const SQLTypeInfo> anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::casts_
mutableprotected

Definition at line 738 of file ExpressionRewrite.cpp.

std::vector<std::shared_ptr<Analyzer::Expr> > anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::chained_string_op_exprs_
mutableprotected

Definition at line 737 of file ExpressionRewrite.cpp.

bool anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::in_string_op_chain_ {false}
mutableprotected

Definition at line 736 of file ExpressionRewrite.cpp.

int32_t anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::num_overflows_
mutableprotected

Definition at line 739 of file ExpressionRewrite.cpp.


The documentation for this class was generated from the following file: