OmniSciDB  c07336695a
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
 

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 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 202 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 242 of file ExpressionRewrite.cpp.

References kDIVIDE, kMINUS, kMULTIPLY, and kPLUS.

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

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

204  {
205  switch (optype) {
206  case kEQ:
207  return t1 == t2;
208  case kNE:
209  return t1 != t2;
210  case kLT:
211  return t1 < t2;
212  case kLE:
213  return t1 <= t2;
214  case kGT:
215  return t1 > t2;
216  case kGE:
217  return t1 >= t2;
218  default:
219  break;
220  }
221  throw std::runtime_error("Unable to fold");
222  return false;
223  }
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 226 of file ExpressionRewrite.cpp.

References kAND, kNOT, and kOR.

226  {
227  switch (optype) {
228  case kAND:
229  return t1 && t2;
230  case kOR:
231  return t1 || t2;
232  case kNOT:
233  return !t1;
234  default:
235  break;
236  }
237  throw std::runtime_error("Unable to fold");
238  return false;
239  }
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 301 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.

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

◆ 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 497 of file ExpressionRewrite.cpp.

References Datum::boolval, anonymous_namespace{ImportTest.cpp}::d(), 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.

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

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

◆ num_overflows_

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

Definition at line 658 of file ExpressionRewrite.cpp.


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