OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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

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::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::Expr
aggregateResult (const std::shared_ptr< Analyzer::Expr > &aggregate, const std::shared_ptr< Analyzer::Expr > &next_result) const
 
virtual std::shared_ptr
< Analyzer::Expr
defaultResult () const
 

Detailed Description

Definition at line 204 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 244 of file ExpressionRewrite.cpp.

References kDIVIDE, kMINUS, kMULTIPLY, and kPLUS.

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

Definition at line 206 of file ExpressionRewrite.cpp.

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

206  {
207  switch (optype) {
208  case kEQ:
209  return t1 == t2;
210  case kNE:
211  return t1 != t2;
212  case kLT:
213  return t1 < t2;
214  case kLE:
215  return t1 <= t2;
216  case kGT:
217  return t1 > t2;
218  case kGE:
219  return t1 >= t2;
220  default:
221  break;
222  }
223  throw std::runtime_error("Unable to fold");
224  return false;
225  }
Definition: sqldefs.h:35
Definition: sqldefs.h:36
Definition: sqldefs.h:30
Definition: sqldefs.h:34
Definition: sqldefs.h:32
Definition: sqldefs.h:33
template<typename T >
bool anonymous_namespace{ExpressionRewrite.cpp}::ConstantFoldingVisitor::foldLogic ( SQLOps  optype,
t1,
t2 
) const
inlineprivate

Definition at line 228 of file ExpressionRewrite.cpp.

References kAND, kNOT, and kOR.

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

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

+ Here is the call graph for this function:

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

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

+ Here is the call graph for this function:

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

Reimplemented from DeepCopyVisitor.

Definition at line 658 of file ExpressionRewrite.cpp.

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

659  {
660  const auto constant_arg_expr =
661  dynamic_cast<const Analyzer::Constant*>(lower_expr->get_arg());
662  if (constant_arg_expr) {
664  boost::locale::to_lower(*constant_arg_expr->get_constval().stringval));
665  }
666  return makeExpr<Analyzer::LowerExpr>(lower_expr->get_own_arg());
667  }
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:

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

Reimplemented from DeepCopyVisitor.

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

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

+ 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 670 of file ExpressionRewrite.cpp.

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

Definition at line 671 of file ExpressionRewrite.cpp.


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