OmniSciDB  29e35f4d58
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

std::unordered_map< const Analyzer::Expr *, const SQLTypeInfocasts_
 
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::ExprvisitLower (const Analyzer::LowerExpr *lower_expr) 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 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 visitCardinality (const Analyzer::CardinalityExpr *cardinality) const override
 
RetType visitLikeExpr (const Analyzer::LikeExpr *like) const override
 
RetType visitRegexpExpr (const Analyzer::RegexpExpr *regexp) 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 visitGeoExpr (const Analyzer::GeoExpr *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::ExpraggregateResult (const std::shared_ptr< Analyzer::Expr > &aggregate, const std::shared_ptr< Analyzer::Expr > &next_result) const
 
virtual std::shared_ptr< Analyzer::ExprdefaultResult () const
 

Detailed Description

Definition at line 207 of file ExpressionRewrite.cpp.

Constructor & Destructor Documentation

◆ ConstantFoldingVisitor()

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

Member Function Documentation

◆ foldArithmetic()

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

Definition at line 247 of file ExpressionRewrite.cpp.

References kDIVIDE, kMINUS, kMULTIPLY, and kPLUS.

247  {
248  bool t2_is_zero = (t2 == (t2 - t2));
249  bool t2_is_negative = (t2 < (t2 - t2));
250  switch (optype) {
251  case kPLUS:
252  // The MIN limit for float and double is the smallest representable value,
253  // not the lowest negative value! Switching to C++11 lowest.
254  if ((t2_is_negative && t1 < std::numeric_limits<T>::lowest() - t2) ||
255  (!t2_is_negative && t1 > std::numeric_limits<T>::max() - t2)) {
256  num_overflows_++;
257  throw std::runtime_error("Plus overflow");
258  }
259  return t1 + t2;
260  case kMINUS:
261  if ((t2_is_negative && t1 > std::numeric_limits<T>::max() + t2) ||
262  (!t2_is_negative && t1 < std::numeric_limits<T>::lowest() + t2)) {
263  num_overflows_++;
264  throw std::runtime_error("Minus overflow");
265  }
266  return t1 - t2;
267  case kMULTIPLY: {
268  if (t2_is_zero) {
269  return t2;
270  }
271  auto ct1 = t1;
272  auto ct2 = t2;
273  // Need to keep t2's sign on the left
274  if (t2_is_negative) {
275  if (t1 == std::numeric_limits<T>::lowest() ||
276  t2 == std::numeric_limits<T>::lowest()) {
277  // negation could overflow - bail
278  num_overflows_++;
279  throw std::runtime_error("Mul neg overflow");
280  }
281  ct1 = -t1; // ct1 gets t2's negativity
282  ct2 = -t2; // ct2 is now positive
283  }
284  // Don't check overlow if we are folding FP mul by a fraction
285  bool ct2_is_fraction = (ct2 < (ct2 / ct2));
286  if (!ct2_is_fraction) {
287  if (ct1 > std::numeric_limits<T>::max() / ct2 ||
288  ct1 < std::numeric_limits<T>::lowest() / ct2) {
289  num_overflows_++;
290  throw std::runtime_error("Mul overflow");
291  }
292  }
293  return t1 * t2;
294  }
295  case kDIVIDE:
296  if (t2_is_zero) {
297  throw std::runtime_error("Will not fold division by zero");
298  }
299  return t1 / t2;
300  default:
301  break;
302  }
303  throw std::runtime_error("Unable to fold");
304  }
Definition: sqldefs.h:41
Definition: sqldefs.h:40

◆ foldComparison()

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

Definition at line 209 of file ExpressionRewrite.cpp.

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

209  {
210  switch (optype) {
211  case kEQ:
212  return t1 == t2;
213  case kNE:
214  return t1 != t2;
215  case kLT:
216  return t1 < t2;
217  case kLE:
218  return t1 <= t2;
219  case kGT:
220  return t1 > t2;
221  case kGE:
222  return t1 >= t2;
223  default:
224  break;
225  }
226  throw std::runtime_error("Unable to fold");
227  return false;
228  }
Definition: sqldefs.h:35
Definition: sqldefs.h:36
Definition: sqldefs.h:30
Definition: sqldefs.h:34
Definition: sqldefs.h:32
Definition: sqldefs.h:33

◆ foldLogic()

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

Definition at line 231 of file ExpressionRewrite.cpp.

References kAND, kNOT, and kOR.

231  {
232  switch (optype) {
233  case kAND:
234  return t1 && t2;
235  case kOR:
236  return t1 || t2;
237  case kNOT:
238  return !t1;
239  default:
240  break;
241  }
242  throw std::runtime_error("Unable to fold");
243  return false;
244  }
Definition: sqldefs.h:38
Definition: sqldefs.h:37
Definition: sqldefs.h:39

◆ foldOper()

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 306 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.

311  {
312  result_type = type;
313 
314  try {
315  switch (type) {
316  case kBOOLEAN:
317  if (IS_COMPARISON(optype)) {
318  result.boolval = foldComparison<bool>(optype, lhs.boolval, rhs.boolval);
319  result_type = kBOOLEAN;
320  return true;
321  }
322  if (IS_LOGIC(optype)) {
323  result.boolval = foldLogic<bool>(optype, lhs.boolval, rhs.boolval);
324  result_type = kBOOLEAN;
325  return true;
326  }
327  CHECK(!IS_ARITHMETIC(optype));
328  break;
329  case kTINYINT:
330  if (IS_COMPARISON(optype)) {
331  result.boolval =
332  foldComparison<int8_t>(optype, lhs.tinyintval, rhs.tinyintval);
333  result_type = kBOOLEAN;
334  return true;
335  }
336  if (IS_ARITHMETIC(optype)) {
337  result.tinyintval =
338  foldArithmetic<int8_t>(optype, lhs.tinyintval, rhs.tinyintval);
339  result_type = kTINYINT;
340  return true;
341  }
342  CHECK(!IS_LOGIC(optype));
343  break;
344  case kSMALLINT:
345  if (IS_COMPARISON(optype)) {
346  result.boolval =
347  foldComparison<int16_t>(optype, lhs.smallintval, rhs.smallintval);
348  result_type = kBOOLEAN;
349  return true;
350  }
351  if (IS_ARITHMETIC(optype)) {
352  result.smallintval =
353  foldArithmetic<int16_t>(optype, lhs.smallintval, rhs.smallintval);
354  result_type = kSMALLINT;
355  return true;
356  }
357  CHECK(!IS_LOGIC(optype));
358  break;
359  case kINT:
360  if (IS_COMPARISON(optype)) {
361  result.boolval = foldComparison<int32_t>(optype, lhs.intval, rhs.intval);
362  result_type = kBOOLEAN;
363  return true;
364  }
365  if (IS_ARITHMETIC(optype)) {
366  result.intval = foldArithmetic<int32_t>(optype, lhs.intval, rhs.intval);
367  result_type = kINT;
368  return true;
369  }
370  CHECK(!IS_LOGIC(optype));
371  break;
372  case kBIGINT:
373  if (IS_COMPARISON(optype)) {
374  result.boolval =
375  foldComparison<int64_t>(optype, lhs.bigintval, rhs.bigintval);
376  result_type = kBOOLEAN;
377  return true;
378  }
379  if (IS_ARITHMETIC(optype)) {
380  result.bigintval =
381  foldArithmetic<int64_t>(optype, lhs.bigintval, rhs.bigintval);
382  result_type = kBIGINT;
383  return true;
384  }
385  CHECK(!IS_LOGIC(optype));
386  break;
387  case kFLOAT:
388  if (IS_COMPARISON(optype)) {
389  result.boolval = foldComparison<float>(optype, lhs.floatval, rhs.floatval);
390  result_type = kBOOLEAN;
391  return true;
392  }
393  if (IS_ARITHMETIC(optype)) {
394  result.floatval = foldArithmetic<float>(optype, lhs.floatval, rhs.floatval);
395  result_type = kFLOAT;
396  return true;
397  }
398  CHECK(!IS_LOGIC(optype));
399  break;
400  case kDOUBLE:
401  if (IS_COMPARISON(optype)) {
402  result.boolval = foldComparison<double>(optype, lhs.doubleval, rhs.doubleval);
403  result_type = kBOOLEAN;
404  return true;
405  }
406  if (IS_ARITHMETIC(optype)) {
407  result.doubleval =
408  foldArithmetic<double>(optype, lhs.doubleval, rhs.doubleval);
409  result_type = kDOUBLE;
410  return true;
411  }
412  CHECK(!IS_LOGIC(optype));
413  break;
414  default:
415  break;
416  }
417  } catch (...) {
418  return false;
419  }
420  return false;
421  }
int8_t tinyintval
Definition: sqltypes.h:126
#define IS_LOGIC(X)
Definition: sqldefs.h:60
bool boolval
Definition: sqltypes.h:125
int32_t intval
Definition: sqltypes.h:128
float floatval
Definition: sqltypes.h:130
int64_t bigintval
Definition: sqltypes.h:129
int16_t smallintval
Definition: sqltypes.h:127
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
#define CHECK(condition)
Definition: Logger.h:193
Definition: sqltypes.h:48
#define IS_COMPARISON(X)
Definition: sqldefs.h:57
double doubleval
Definition: sqltypes.h:131

◆ get_num_overflows()

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

◆ reset_num_overflows()

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

◆ visitBinOper()

std::shared_ptr<Analyzer::Expr> anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::visitBinOper ( const Analyzer::BinOper bin_oper) const
inlineoverrideprivatevirtual

Reimplemented from DeepCopyVisitor.

Definition at line 502 of file ExpressionRewrite.cpp.

References Datum::boolval, decimal_to_int_type(), 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, SQLTypeInfoCore< TYPE_FACET_PACK >::is_decimal(), kAND, kBOOLEAN, kDIVIDE, kDOUBLE, kEQ, kFLOAT, kGE, kGT, kLE, kLT, kMINUS, kMULTIPLY, kNE, kOR, and kPLUS.

503  {
504  const auto optype = bin_oper->get_optype();
505  auto ti = bin_oper->get_type_info();
506  auto left_operand = bin_oper->get_own_left_operand();
507  auto right_operand = bin_oper->get_own_right_operand();
508 
509  // Check if bin_oper result is cast to a larger int or fp type
510  if (casts_.find(bin_oper) != casts_.end()) {
511  const auto cast_ti = casts_[bin_oper];
512  const auto& lhs_ti = bin_oper->get_left_operand()->get_type_info();
513  // Propagate cast down to the operands for folding
514  if ((cast_ti.is_integer() || cast_ti.is_fp()) && lhs_ti.is_integer() &&
515  cast_ti.get_size() > lhs_ti.get_size() &&
516  (optype == kMINUS || optype == kPLUS || optype == kMULTIPLY)) {
517  // Before folding, cast the operands to the bigger type to avoid overflows.
518  // Currently upcasting smaller integer types to larger integers or double.
519  left_operand = left_operand->deep_copy()->add_cast(cast_ti);
520  right_operand = right_operand->deep_copy()->add_cast(cast_ti);
521  ti = cast_ti;
522  }
523  }
524 
525  const auto lhs = visit(left_operand.get());
526  const auto rhs = visit(right_operand.get());
527 
528  auto const_lhs = std::dynamic_pointer_cast<Analyzer::Constant>(lhs);
529  auto const_rhs = std::dynamic_pointer_cast<Analyzer::Constant>(rhs);
530  const auto& lhs_ti = lhs->get_type_info();
531  const auto& rhs_ti = rhs->get_type_info();
532  auto lhs_type = lhs_ti.is_decimal() ? decimal_to_int_type(lhs_ti) : lhs_ti.get_type();
533  auto rhs_type = rhs_ti.is_decimal() ? decimal_to_int_type(rhs_ti) : rhs_ti.get_type();
534 
535  if (const_lhs && const_rhs && lhs_type == rhs_type) {
536  auto lhs_datum = const_lhs->get_constval();
537  auto rhs_datum = const_rhs->get_constval();
538  Datum result_datum = {};
539  SQLTypes result_type;
540  if (foldOper(optype, lhs_type, lhs_datum, rhs_datum, result_datum, result_type)) {
541  // Fold all ops that don't take in decimal operands, and also decimal comparisons
542  if (!lhs_ti.is_decimal() || IS_COMPARISON(optype)) {
543  return makeExpr<Analyzer::Constant>(result_type, false, result_datum);
544  }
545  // Decimal arithmetic has been done as kBIGINT. Selectively fold some decimal ops,
546  // using result_datum and BinOper expr typeinfo which was adjusted for these ops.
547  if (optype == kMINUS || optype == kPLUS || optype == kMULTIPLY) {
548  return makeExpr<Analyzer::Constant>(ti, false, result_datum);
549  }
550  }
551  }
552 
553  if (optype == kAND && lhs_type == rhs_type && lhs_type == kBOOLEAN) {
554  if (const_rhs && !const_rhs->get_is_null()) {
555  auto rhs_datum = const_rhs->get_constval();
556  if (rhs_datum.boolval == false) {
557  Datum d;
558  d.boolval = false;
559  // lhs && false --> false
560  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
561  }
562  // lhs && true --> lhs
563  return lhs;
564  }
565  if (const_lhs && !const_lhs->get_is_null()) {
566  auto lhs_datum = const_lhs->get_constval();
567  if (lhs_datum.boolval == false) {
568  Datum d;
569  d.boolval = false;
570  // false && rhs --> false
571  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
572  }
573  // true && rhs --> rhs
574  return rhs;
575  }
576  }
577  if (optype == kOR && lhs_type == rhs_type && lhs_type == kBOOLEAN) {
578  if (const_rhs && !const_rhs->get_is_null()) {
579  auto rhs_datum = const_rhs->get_constval();
580  if (rhs_datum.boolval == true) {
581  Datum d;
582  d.boolval = true;
583  // lhs || true --> true
584  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
585  }
586  // lhs || false --> lhs
587  return lhs;
588  }
589  if (const_lhs && !const_lhs->get_is_null()) {
590  auto lhs_datum = const_lhs->get_constval();
591  if (lhs_datum.boolval == true) {
592  Datum d;
593  d.boolval = true;
594  // true || rhs --> true
595  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
596  }
597  // false || rhs --> rhs
598  return rhs;
599  }
600  }
601  if (*lhs == *rhs) {
602  // Tautologies: v=v; v<=v; v>=v
603  if (optype == kEQ || optype == kLE || optype == kGE) {
604  Datum d;
605  d.boolval = true;
606  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
607  }
608  // Contradictions: v!=v; v<v; v>v
609  if (optype == kNE || optype == kLT || optype == kGT) {
610  Datum d;
611  d.boolval = false;
612  return makeExpr<Analyzer::Constant>(kBOOLEAN, false, d);
613  }
614  // v-v
615  if (optype == kMINUS) {
616  Datum d = {};
617  return makeExpr<Analyzer::Constant>(lhs_type, false, d);
618  }
619  }
620  // Convert fp division by a constant to multiplication by 1/constant
621  if (optype == kDIVIDE && const_rhs && rhs_ti.is_fp()) {
622  auto rhs_datum = const_rhs->get_constval();
623  std::shared_ptr<Analyzer::Expr> recip_rhs = nullptr;
624  if (rhs_ti.get_type() == kFLOAT) {
625  if (rhs_datum.floatval == 1.0) {
626  return lhs;
627  }
628  auto f = std::fabs(rhs_datum.floatval);
629  if (f > 1.0 || (f != 0.0 && 1.0 < f * std::numeric_limits<float>::max())) {
630  rhs_datum.floatval = 1.0 / rhs_datum.floatval;
631  recip_rhs = makeExpr<Analyzer::Constant>(rhs_type, false, rhs_datum);
632  }
633  } else if (rhs_ti.get_type() == kDOUBLE) {
634  if (rhs_datum.doubleval == 1.0) {
635  return lhs;
636  }
637  auto d = std::fabs(rhs_datum.doubleval);
638  if (d > 1.0 || (d != 0.0 && 1.0 < d * std::numeric_limits<double>::max())) {
639  rhs_datum.doubleval = 1.0 / rhs_datum.doubleval;
640  recip_rhs = makeExpr<Analyzer::Constant>(rhs_type, false, rhs_datum);
641  }
642  }
643  if (recip_rhs) {
644  return makeExpr<Analyzer::BinOper>(ti,
645  bin_oper->get_contains_agg(),
646  kMULTIPLY,
647  bin_oper->get_qualifier(),
648  lhs,
649  recip_rhs);
650  }
651  }
652 
653  return makeExpr<Analyzer::BinOper>(ti,
654  bin_oper->get_contains_agg(),
655  bin_oper->get_optype(),
656  bin_oper->get_qualifier(),
657  lhs,
658  rhs);
659  }
SQLQualifier get_qualifier() const
Definition: Analyzer.h:435
bool foldOper(SQLOps optype, SQLTypes type, Datum lhs, Datum rhs, Datum &result, SQLTypes &result_type) const
SQLTypes
Definition: sqltypes.h:41
bool boolval
Definition: sqltypes.h:125
Definition: sqldefs.h:35
Definition: sqldefs.h:36
Definition: sqldefs.h:38
Definition: sqldefs.h:30
Definition: sqldefs.h:41
SQLOps get_optype() const
Definition: Analyzer.h:433
bool is_decimal() const
Definition: sqltypes.h:480
Definition: sqldefs.h:37
const std::shared_ptr< Analyzer::Expr > get_own_left_operand() const
Definition: Analyzer.h:438
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:268
Definition: sqldefs.h:34
const std::shared_ptr< Analyzer::Expr > get_own_right_operand() const
Definition: Analyzer.h:441
Definition: sqldefs.h:40
Definition: sqldefs.h:32
std::shared_ptr< Analyzer::Expr > visit(const Analyzer::Expr *expr) const
std::unordered_map< const Analyzer::Expr *, const SQLTypeInfo > casts_
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
Definition: sqldefs.h:33
bool get_contains_agg() const
Definition: Analyzer.h:80
#define IS_COMPARISON(X)
Definition: sqldefs.h:57
const Expr * get_left_operand() const
Definition: Analyzer.h:436
+ Here is the call graph for this function:

◆ visitLower()

std::shared_ptr<Analyzer::Expr> anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::visitLower ( const Analyzer::LowerExpr lower_expr) const
inlineoverrideprivatevirtual

Reimplemented from DeepCopyVisitor.

Definition at line 661 of file ExpressionRewrite.cpp.

References Parser::StringLiteral::analyzeValue(), Analyzer::LowerExpr::get_arg(), and Analyzer::LowerExpr::get_own_arg().

662  {
663  const auto constant_arg_expr =
664  dynamic_cast<const Analyzer::Constant*>(lower_expr->get_arg());
665  if (constant_arg_expr) {
667  boost::locale::to_lower(*constant_arg_expr->get_constval().stringval));
668  }
669  return makeExpr<Analyzer::LowerExpr>(lower_expr->get_own_arg());
670  }
const std::shared_ptr< Analyzer::Expr > get_own_arg() const
Definition: Analyzer.h:746
static std::shared_ptr< Analyzer::Expr > analyzeValue(const std::string &)
Definition: ParserNode.cpp:97
const Expr * get_arg() const
Definition: Analyzer.h:744
+ Here is the call graph for this function:

◆ visitUOper()

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

Reimplemented from DeepCopyVisitor.

Definition at line 423 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.

424  {
425  const auto unvisited_operand = uoper->get_operand();
426  const auto optype = uoper->get_optype();
427  const auto& ti = uoper->get_type_info();
428  if (optype == kCAST) {
429  // Cache the cast type so it could be used in operand rewriting/folding
430  casts_.insert({unvisited_operand, ti});
431  }
432  const auto operand = visit(unvisited_operand);
433  const auto& operand_ti = operand->get_type_info();
434  const auto operand_type =
435  operand_ti.is_decimal() ? decimal_to_int_type(operand_ti) : operand_ti.get_type();
436  const auto const_operand =
437  std::dynamic_pointer_cast<const Analyzer::Constant>(operand);
438 
439  if (const_operand) {
440  const auto operand_datum = const_operand->get_constval();
441  Datum zero_datum = {};
442  Datum result_datum = {};
443  SQLTypes result_type;
444  switch (optype) {
445  case kNOT: {
446  if (foldOper(kEQ,
447  operand_type,
448  zero_datum,
449  operand_datum,
450  result_datum,
451  result_type)) {
452  CHECK_EQ(result_type, kBOOLEAN);
453  return makeExpr<Analyzer::Constant>(result_type, false, result_datum);
454  }
455  break;
456  }
457  case kUMINUS: {
458  if (foldOper(kMINUS,
459  operand_type,
460  zero_datum,
461  operand_datum,
462  result_datum,
463  result_type)) {
464  if (!operand_ti.is_decimal()) {
465  return makeExpr<Analyzer::Constant>(result_type, false, result_datum);
466  }
467  return makeExpr<Analyzer::Constant>(ti, false, result_datum);
468  }
469  break;
470  }
471  case kCAST: {
472  // Trying to fold number to number casts only
473  if (!ti.is_number() || !operand_ti.is_number()) {
474  break;
475  }
476  // Disallowing folding of FP to DECIMAL casts for now:
477  // allowing them would make this test pass:
478  // update dectest set d=cast( 1234.0 as float );
479  // which is expected to throw in Update.ImplicitCastToNumericTypes
480  // due to cast codegen currently not supporting these casts
481  if (ti.is_decimal() && operand_ti.is_fp()) {
482  break;
483  }
484  auto operand_copy = const_operand->deep_copy();
485  auto cast_operand = operand_copy->add_cast(ti);
486  auto const_cast_operand =
487  std::dynamic_pointer_cast<const Analyzer::Constant>(cast_operand);
488  if (const_cast_operand) {
489  auto const_cast_datum = const_cast_operand->get_constval();
490  return makeExpr<Analyzer::Constant>(ti, false, const_cast_datum);
491  }
492  }
493  default:
494  break;
495  }
496  }
497 
498  return makeExpr<Analyzer::UOper>(
499  uoper->get_type_info(), uoper->get_contains_agg(), optype, operand);
500  }
#define CHECK_EQ(x, y)
Definition: Logger.h:201
bool foldOper(SQLOps optype, SQLTypes type, Datum lhs, Datum rhs, Datum &result, SQLTypes &result_type) const
SQLTypes
Definition: sqltypes.h:41
Definition: sqldefs.h:49
Definition: sqldefs.h:30
Datum get_constval() const
Definition: Analyzer.h:329
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:268
Definition: sqldefs.h:40
std::shared_ptr< Analyzer::Expr > visit(const Analyzer::Expr *expr) const
std::unordered_map< const Analyzer::Expr *, const SQLTypeInfo > casts_
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
bool get_contains_agg() const
Definition: Analyzer.h:80
Definition: sqldefs.h:39
SQLOps get_optype() const
Definition: Analyzer.h:364
const Expr * get_operand() const
Definition: Analyzer.h:365
+ Here is the call graph for this function:

Member Data Documentation

◆ casts_

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

Definition at line 673 of file ExpressionRewrite.cpp.

◆ num_overflows_

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

Definition at line 674 of file ExpressionRewrite.cpp.


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