OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExpressionRange.cpp File Reference
#include "ExpressionRange.h"
#include "DateTimeTranslator.h"
#include "DateTimeUtils.h"
#include "DateTruncate.h"
#include "Descriptors/InputDescriptors.h"
#include "Execute.h"
#include "ExtractFromTime.h"
#include "GroupByAndAggregate.h"
#include "QueryPhysicalInputsCollector.h"
#include <algorithm>
#include <cfenv>
#include <cmath>
+ Include dependency graph for ExpressionRange.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{ExpressionRange.cpp}
 

Macros

#define DEF_OPERATOR(fname, op)
 
#define FIND_STAT_FRAG(stat_name)
 

Functions

void apply_fp_qual (const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
 
void apply_int_qual (const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
 
void apply_hpt_qual (const Datum const_datum, const SQLTypes const_type, const int32_t const_dimen, const int32_t col_dimen, const SQLOps sql_op, ExpressionRange &qual_range)
 
ExpressionRange apply_simple_quals (const Analyzer::ColumnVar *col_expr, const ExpressionRange &col_range, const boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
 
ExpressionRange getExpressionRange (const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
 
ExpressionRange getExpressionRange (const Analyzer::Constant *expr)
 
ExpressionRange getExpressionRange (const Analyzer::ColumnVar *col_expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
 
ExpressionRange getExpressionRange (const Analyzer::StringOper *string_oper_expr, const Executor *executor)
 
ExpressionRange getExpressionRange (const Analyzer::LikeExpr *like_expr)
 
ExpressionRange getExpressionRange (const Analyzer::CaseExpr *case_expr, const std::vector< InputTableInfo > &query_infos, const Executor *)
 
ExpressionRange getExpressionRange (const Analyzer::UOper *u_expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
 
ExpressionRange getExpressionRange (const Analyzer::ExtractExpr *extract_expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
 
ExpressionRange getExpressionRange (const Analyzer::DatetruncExpr *datetrunc_expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
 
ExpressionRange getExpressionRange (const Analyzer::WidthBucketExpr *width_bucket_expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
 
ExpressionRange getExpressionRange (const Analyzer::Expr *expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
 
int64_t anonymous_namespace{ExpressionRange.cpp}::scale_up_interval_endpoint (const int64_t endpoint, const SQLTypeInfo &ti)
 
int64_t anonymous_namespace{ExpressionRange.cpp}::get_conservative_datetrunc_bucket (const DatetruncField datetrunc_field)
 
ExpressionRange getLeafColumnRange (const Analyzer::ColumnVar *col_expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, const bool is_outer_join_proj)
 
ExpressionRange anonymous_namespace{ExpressionRange.cpp}::fpRangeFromDecimal (const ExpressionRange &arg_range, const int64_t scale, const SQLTypeInfo &target_ti)
 
ExpressionRange anonymous_namespace{ExpressionRange.cpp}::getDateTimePrecisionCastRange (const ExpressionRange &arg_range, const SQLTypeInfo &oper_ti, const SQLTypeInfo &target_ti)
 

Macro Definition Documentation

#define DEF_OPERATOR (   fname,
  op 
)

Definition at line 31 of file ExpressionRange.cpp.

#define FIND_STAT_FRAG (   stat_name)

Definition at line 452 of file ExpressionRange.cpp.

Referenced by getLeafColumnRange().

Function Documentation

void apply_fp_qual ( const Datum  const_datum,
const SQLTypes  const_type,
const SQLOps  sql_op,
ExpressionRange qual_range 
)

Definition at line 74 of file ExpressionRange.cpp.

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

Referenced by apply_simple_quals().

77  {
78  double const_val = get_value_from_datum<double>(const_datum, const_type);
79  switch (sql_op) {
80  case kGT:
81  case kGE:
82  qual_range.setFpMin(std::max(qual_range.getFpMin(), const_val));
83  break;
84  case kLT:
85  case kLE:
86  qual_range.setFpMax(std::min(qual_range.getFpMax(), const_val));
87  break;
88  case kEQ:
89  qual_range.setFpMin(std::max(qual_range.getFpMin(), const_val));
90  qual_range.setFpMax(std::min(qual_range.getFpMax(), const_val));
91  break;
92  default: // there may be other operators, but don't do anything with them
93  break;
94  }
95 }
Definition: sqldefs.h:34
Definition: sqldefs.h:35
Definition: sqldefs.h:29
Definition: sqldefs.h:33
double getFpMax() const
double getFpMin() const
void setFpMax(const double fp_max)
Definition: sqldefs.h:32
void setFpMin(const double fp_min)

+ Here is the caller graph for this function:

void apply_hpt_qual ( const Datum  const_datum,
const SQLTypes  const_type,
const int32_t  const_dimen,
const int32_t  col_dimen,
const SQLOps  sql_op,
ExpressionRange qual_range 
)

Definition at line 124 of file ExpressionRange.cpp.

References apply_int_qual(), Datum::bigintval, CHECK, and DateTimeUtils::get_timestamp_precision_scale().

Referenced by apply_simple_quals().

129  {
130  CHECK(const_dimen != col_dimen);
131  Datum datum{0};
132  if (const_dimen > col_dimen) {
133  datum.bigintval =
134  get_value_from_datum<int64_t>(const_datum, const_type) /
135  DateTimeUtils::get_timestamp_precision_scale(const_dimen - col_dimen);
136  } else {
137  datum.bigintval =
138  get_value_from_datum<int64_t>(const_datum, const_type) *
139  DateTimeUtils::get_timestamp_precision_scale(col_dimen - const_dimen);
140  }
141  apply_int_qual(datum, const_type, sql_op, qual_range);
142 }
void apply_int_qual(const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
int64_t bigintval
Definition: sqltypes.h:215
#define CHECK(condition)
Definition: Logger.h:222
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void apply_int_qual ( const Datum  const_datum,
const SQLTypes  const_type,
const SQLOps  sql_op,
ExpressionRange qual_range 
)

Definition at line 97 of file ExpressionRange.cpp.

References ExpressionRange::getIntMax(), ExpressionRange::getIntMin(), kEQ, kGE, kGT, kLE, kLT, ExpressionRange::setIntMax(), and ExpressionRange::setIntMin().

Referenced by apply_hpt_qual(), and apply_simple_quals().

100  {
101  int64_t const_val = get_value_from_datum<int64_t>(const_datum, const_type);
102  switch (sql_op) {
103  case kGT:
104  qual_range.setIntMin(std::max(qual_range.getIntMin(), const_val + 1));
105  break;
106  case kGE:
107  qual_range.setIntMin(std::max(qual_range.getIntMin(), const_val));
108  break;
109  case kLT:
110  qual_range.setIntMax(std::min(qual_range.getIntMax(), const_val - 1));
111  break;
112  case kLE:
113  qual_range.setIntMax(std::min(qual_range.getIntMax(), const_val));
114  break;
115  case kEQ:
116  qual_range.setIntMin(std::max(qual_range.getIntMin(), const_val));
117  qual_range.setIntMax(std::min(qual_range.getIntMax(), const_val));
118  break;
119  default: // there may be other operators, but don't do anything with them
120  break;
121  }
122 }
int64_t getIntMin() const
Definition: sqldefs.h:34
Definition: sqldefs.h:35
Definition: sqldefs.h:29
void setIntMin(const int64_t int_min)
Definition: sqldefs.h:33
void setIntMax(const int64_t int_max)
int64_t getIntMax() const
Definition: sqldefs.h:32

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ExpressionRange apply_simple_quals ( const Analyzer::ColumnVar col_expr,
const ExpressionRange col_range,
const boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>  simple_quals 
)

Definition at line 144 of file ExpressionRange.cpp.

References apply_fp_qual(), apply_hpt_qual(), apply_int_qual(), Double, Float, Analyzer::ColumnVar::get_column_id(), Analyzer::ColumnVar::get_table_id(), and ExpressionRange::getType().

Referenced by getExpressionRange().

147  {
148  if (!simple_quals) {
149  return col_range;
150  }
151  ExpressionRange qual_range(col_range);
152  for (auto const& itr : simple_quals.get()) {
153  auto qual_bin_oper = dynamic_cast<Analyzer::BinOper*>(itr.get());
154  if (!qual_bin_oper) {
155  continue;
156  }
157  const Analyzer::Expr* left_operand = qual_bin_oper->get_left_operand();
158  auto qual_col = dynamic_cast<const Analyzer::ColumnVar*>(left_operand);
159  if (!qual_col) {
160  // Check for possibility that column is wrapped in a cast
161  // Presumes that only simple casts (i.e. timestamp to timestamp or int to int) have
162  // been passed through by BinOper::normalize_simple_predicate
163  auto u_expr = dynamic_cast<const Analyzer::UOper*>(left_operand);
164  if (!u_expr) {
165  continue;
166  }
167  qual_col = dynamic_cast<const Analyzer::ColumnVar*>(u_expr->get_operand());
168  if (!qual_col) {
169  continue;
170  }
171  }
172  if (qual_col->get_table_id() != col_expr->get_table_id() ||
173  qual_col->get_column_id() != col_expr->get_column_id()) {
174  continue;
175  }
176  const Analyzer::Expr* right_operand = qual_bin_oper->get_right_operand();
177  auto qual_const = dynamic_cast<const Analyzer::Constant*>(right_operand);
178  if (!qual_const) {
179  continue;
180  }
181  if (qual_range.getType() == ExpressionRangeType::Float ||
182  qual_range.getType() == ExpressionRangeType::Double) {
183  apply_fp_qual(qual_const->get_constval(),
184  qual_const->get_type_info().get_type(),
185  qual_bin_oper->get_optype(),
186  qual_range);
187  } else if ((qual_col->get_type_info().is_timestamp() ||
188  qual_const->get_type_info().is_timestamp()) &&
189  (qual_col->get_type_info().get_dimension() !=
190  qual_const->get_type_info().get_dimension())) {
191  apply_hpt_qual(qual_const->get_constval(),
192  qual_const->get_type_info().get_type(),
193  qual_const->get_type_info().get_dimension(),
194  qual_col->get_type_info().get_dimension(),
195  qual_bin_oper->get_optype(),
196  qual_range);
197  } else {
198  apply_int_qual(qual_const->get_constval(),
199  qual_const->get_type_info().get_type(),
200  qual_bin_oper->get_optype(),
201  qual_range);
202  }
203  }
204  return qual_range;
205 }
int get_table_id() const
Definition: Analyzer.h:200
void apply_hpt_qual(const Datum const_datum, const SQLTypes const_type, const int32_t const_dimen, const int32_t col_dimen, const SQLOps sql_op, ExpressionRange &qual_range)
void apply_int_qual(const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
void apply_fp_qual(const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
int get_column_id() const
Definition: Analyzer.h:201

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::BinOper expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor,
boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>  simple_quals 
)

Definition at line 374 of file ExpressionRange.cpp.

References CHECK, Analyzer::BinOper::get_left_operand(), Analyzer::BinOper::get_optype(), Analyzer::BinOper::get_right_operand(), Analyzer::Expr::get_type_info(), Integer, Invalid, kDIVIDE, kMINUS, kMULTIPLY, kPLUS, ExpressionRange::makeIntRange(), ExpressionRange::makeInvalidRange(), and anonymous_namespace{ExpressionRange.cpp}::scale_up_interval_endpoint().

Referenced by CodeGenerator::checkExpressionRanges(), CodeGenerator::codegen(), anonymous_namespace{RelAlgExecutor.cpp}::decide_approx_count_distinct_implementation(), anonymous_namespace{GroupByAndAggregate.cpp}::get_expr_range_info(), anonymous_namespace{GroupByAndAggregate.cpp}::get_keyless_info(), getExpressionRange(), PerfectJoinHashTable::getInstance(), anonymous_namespace{QueryMemoryDescriptor.cpp}::pick_baseline_key_width(), and QueryRewriter::rewriteConstrainedByInImpl().

378  {
379  const auto& lhs =
380  getExpressionRange(expr->get_left_operand(), query_infos, executor, simple_quals);
381  const auto& rhs =
382  getExpressionRange(expr->get_right_operand(), query_infos, executor, simple_quals);
383  switch (expr->get_optype()) {
384  case kPLUS:
385  return lhs + rhs;
386  case kMINUS:
387  return lhs - rhs;
388  case kMULTIPLY:
389  return lhs * rhs;
390  case kDIVIDE: {
391  const auto& lhs_type = expr->get_left_operand()->get_type_info();
392  if (lhs_type.is_decimal() && lhs.getType() != ExpressionRangeType::Invalid) {
393  CHECK(lhs.getType() == ExpressionRangeType::Integer);
394  const auto adjusted_lhs = ExpressionRange::makeIntRange(
395  scale_up_interval_endpoint(lhs.getIntMin(), lhs_type),
396  scale_up_interval_endpoint(lhs.getIntMax(), lhs_type),
397  0,
398  lhs.hasNulls());
399  return adjusted_lhs / rhs;
400  }
401  return lhs / rhs;
402  }
403  default:
404  break;
405  }
407 }
const Expr * get_right_operand() const
Definition: Analyzer.h:450
Definition: sqldefs.h:40
int64_t scale_up_interval_endpoint(const int64_t endpoint, const SQLTypeInfo &ti)
SQLOps get_optype() const
Definition: Analyzer.h:446
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
Definition: sqldefs.h:39
#define CHECK(condition)
Definition: Logger.h:222
const Expr * get_left_operand() const
Definition: Analyzer.h:449
static ExpressionRange makeInvalidRange()

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::Constant expr)

Definition at line 409 of file ExpressionRange.cpp.

References Analyzer::Constant::get_constval(), Analyzer::Constant::get_is_null(), SQLTypeInfo::get_type(), Analyzer::Expr::get_type_info(), kBIGINT, kDATE, kDECIMAL, kDOUBLE, kFLOAT, kINT, kNUMERIC, kSMALLINT, kTIME, kTIMESTAMP, kTINYINT, ExpressionRange::makeDoubleRange(), ExpressionRange::makeFloatRange(), ExpressionRange::makeIntRange(), and ExpressionRange::makeInvalidRange().

409  {
410  if (constant_expr->get_is_null()) {
412  }
413  const auto constant_type = constant_expr->get_type_info().get_type();
414  const auto datum = constant_expr->get_constval();
415  switch (constant_type) {
416  case kTINYINT: {
417  const int64_t v = datum.tinyintval;
418  return ExpressionRange::makeIntRange(v, v, 0, false);
419  }
420  case kSMALLINT: {
421  const int64_t v = datum.smallintval;
422  return ExpressionRange::makeIntRange(v, v, 0, false);
423  }
424  case kINT: {
425  const int64_t v = datum.intval;
426  return ExpressionRange::makeIntRange(v, v, 0, false);
427  }
428  case kBIGINT:
429  case kNUMERIC:
430  case kDECIMAL: {
431  const int64_t v = datum.bigintval;
432  return ExpressionRange::makeIntRange(v, v, 0, false);
433  }
434  case kTIME:
435  case kTIMESTAMP:
436  case kDATE: {
437  const int64_t v = datum.bigintval;
438  return ExpressionRange::makeIntRange(v, v, 0, false);
439  }
440  case kFLOAT: {
441  return ExpressionRange::makeFloatRange(datum.floatval, datum.floatval, false);
442  }
443  case kDOUBLE: {
444  return ExpressionRange::makeDoubleRange(datum.doubleval, datum.doubleval, false);
445  }
446  default:
447  break;
448  }
450 }
Definition: sqltypes.h:49
static ExpressionRange makeFloatRange(const float fp_min, const float fp_max, const bool has_nulls)
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
static ExpressionRange makeDoubleRange(const double fp_min, const double fp_max, const bool has_nulls)
Definition: sqltypes.h:53
Definition: sqltypes.h:45
static ExpressionRange makeInvalidRange()

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::ColumnVar col_expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor,
boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>  simple_quals 
)

Definition at line 635 of file ExpressionRange.cpp.

References apply_simple_quals(), CHECK_GE, CHECK_LT, Analyzer::ColumnVar::get_column_id(), Analyzer::ColumnVar::get_rte_idx(), Analyzer::ColumnVar::get_table_id(), and getLeafColumnRange().

639  {
640  const int rte_idx = col_expr->get_rte_idx();
641  CHECK_GE(rte_idx, 0);
642  CHECK_LT(static_cast<size_t>(rte_idx), query_infos.size());
643  bool is_outer_join_proj = rte_idx > 0 && executor->containsLeftDeepOuterJoin();
644  if (col_expr->get_table_id() > 0) {
645  auto col_range = executor->getColRange(
646  PhysicalInput{col_expr->get_column_id(), col_expr->get_table_id()});
647  if (is_outer_join_proj) {
648  col_range.setHasNulls();
649  }
650  return apply_simple_quals(col_expr, col_range, simple_quals);
651  }
652  return getLeafColumnRange(col_expr, query_infos, executor, is_outer_join_proj);
653 }
int get_table_id() const
Definition: Analyzer.h:200
#define CHECK_GE(x, y)
Definition: Logger.h:235
ExpressionRange apply_simple_quals(const Analyzer::ColumnVar *col_expr, const ExpressionRange &col_range, const boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
ExpressionRange getLeafColumnRange(const Analyzer::ColumnVar *col_expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, const bool is_outer_join_proj)
#define CHECK_LT(x, y)
Definition: Logger.h:232
int get_rte_idx() const
Definition: Analyzer.h:202
int get_column_id() const
Definition: Analyzer.h:201

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::StringOper string_oper_expr,
const Executor executor 
)

Definition at line 655 of file ExpressionRange.cpp.

References CHECK, Analyzer::Expr::get_type_info(), Analyzer::StringOper::getArg(), Analyzer::StringOper::getChainedStringOpExprs(), ExpressionRange::makeIntRange(), ExpressionRange::makeInvalidRange(), remove_cast(), and RowSetMemoryOwner::SOURCE_UNION.

656  {
657  auto chained_string_op_exprs = string_oper_expr->getChainedStringOpExprs();
658  if (chained_string_op_exprs.empty()) {
659  throw std::runtime_error(
660  "StringOper getExpressionRange() Expected folded string operator but found "
661  "operator unfolded.");
662  }
663  // Consider encapsulating below in an Analyzer::StringOper method to dedup
664  std::vector<StringOps_Namespace::StringOpInfo> string_op_infos;
665  for (const auto& chained_string_op_expr : chained_string_op_exprs) {
666  auto chained_string_op =
667  dynamic_cast<const Analyzer::StringOper*>(chained_string_op_expr.get());
668  CHECK(chained_string_op);
669  StringOps_Namespace::StringOpInfo string_op_info(chained_string_op->get_kind(),
670  chained_string_op->getLiteralArgs());
671  string_op_infos.emplace_back(string_op_info);
672  }
673 
674  const auto string_oper_col_var_expr = remove_cast(string_oper_expr->getArg(0));
675  CHECK(string_oper_col_var_expr);
676  const auto string_oper_col_var =
677  dynamic_cast<const Analyzer::ColumnVar*>(string_oper_col_var_expr);
678  CHECK(string_oper_col_var);
679  const auto string_oper_col_var_ti = string_oper_col_var->get_type_info();
680  if (string_oper_col_var_ti.is_none_encoded_string()) {
681  // If here the translation is done on the fly so unless this
682  // was the result of a previous step, we won't have the translated
683  // dictionary yet. Just throw an invalid range.
685  }
686  CHECK(string_oper_col_var_ti.is_dict_encoded_string());
687 
688  const auto expr_ti = string_oper_expr->get_type_info();
689  CHECK(expr_ti.is_string());
690  const auto dict_id = expr_ti.get_comp_param();
691 
692  const auto translation_map = executor->getStringProxyTranslationMap(
693  dict_id,
694  dict_id,
696  string_op_infos,
697  executor->getRowSetMemoryOwner(),
698  true);
699 
700  // Todo(todd): Track null presence in StringDictionaryProxy::IdMap
701  return ExpressionRange::makeIntRange(translation_map->rangeStart(),
702  translation_map->rangeEnd() - 1,
703  0 /* bucket */,
704  true /* assume has nulls */);
705 }
std::shared_ptr< Analyzer::Expr > remove_cast(const std::shared_ptr< Analyzer::Expr > &expr)
Definition: Analyzer.cpp:4196
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
Expression class for string functions The &quot;arg&quot; constructor parameter must be an expression that reso...
Definition: Analyzer.h:1463
#define CHECK(condition)
Definition: Logger.h:222
static ExpressionRange makeInvalidRange()
std::vector< std::shared_ptr< Analyzer::Expr > > getChainedStringOpExprs() const
Definition: Analyzer.h:1530
const Expr * getArg(const size_t i) const
Definition: Analyzer.h:1518

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::LikeExpr like_expr)

Definition at line 707 of file ExpressionRange.cpp.

References CHECK, Analyzer::LikeExpr::get_arg(), Analyzer::Expr::get_type_info(), inline_int_null_val(), and ExpressionRange::makeIntRange().

707  {
708  const auto& ti = like_expr->get_type_info();
709  CHECK(ti.is_boolean());
710  const auto& arg_ti = like_expr->get_arg()->get_type_info();
712  arg_ti.get_notnull() ? 0 : inline_int_null_val(ti), 1, 0, false);
713 }
const Expr * get_arg() const
Definition: Analyzer.h:903
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
#define CHECK(condition)
Definition: Logger.h:222
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::CaseExpr case_expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor 
)

Definition at line 715 of file ExpressionRange.cpp.

References CHECK, CHECK_EQ, Analyzer::CaseExpr::get_else_expr(), Analyzer::CaseExpr::get_expr_pair_list(), getExpressionRange(), Invalid, kBOOLEAN, ExpressionRange::makeInvalidRange(), and Null.

717  {
718  const auto& expr_pair_list = case_expr->get_expr_pair_list();
719  auto expr_range = ExpressionRange::makeInvalidRange();
720  bool has_nulls = false;
721  for (const auto& expr_pair : expr_pair_list) {
722  CHECK_EQ(expr_pair.first->get_type_info().get_type(), kBOOLEAN);
723  const auto crt_range =
724  getExpressionRange(expr_pair.second.get(), query_infos, executor);
725  if (crt_range.getType() == ExpressionRangeType::Null) {
726  has_nulls = true;
727  continue;
728  }
729  if (crt_range.getType() == ExpressionRangeType::Invalid) {
731  }
732  expr_range = (expr_range.getType() != ExpressionRangeType::Invalid)
733  ? expr_range || crt_range
734  : crt_range;
735  }
736  if (has_nulls && !(expr_range.getType() == ExpressionRangeType::Invalid)) {
737  expr_range.setHasNulls();
738  }
739  const auto else_expr = case_expr->get_else_expr();
740  CHECK(else_expr);
741  const auto else_null_expr = dynamic_cast<const Analyzer::Constant*>(else_expr);
742  if (else_null_expr && else_null_expr->get_is_null()) {
743  expr_range.setHasNulls();
744  return expr_range;
745  }
746  return expr_range || getExpressionRange(else_expr, query_infos, executor);
747 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
const Expr * get_else_expr() const
Definition: Analyzer.h:1259
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
#define CHECK(condition)
Definition: Logger.h:222
static ExpressionRange makeInvalidRange()
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1256

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::UOper u_expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor,
boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>  simple_quals 
)

Definition at line 814 of file ExpressionRange.cpp.

References gpu_enabled::accumulate(), CHECK, CHECK_EQ, Double, exp_to_scale(), Float, anonymous_namespace{ExpressionRange.cpp}::fpRangeFromDecimal(), Analyzer::UOper::get_operand(), Analyzer::UOper::get_optype(), Analyzer::Expr::get_type_info(), anonymous_namespace{ExpressionRange.cpp}::getDateTimePrecisionCastRange(), getExpressionRange(), Integer, Invalid, kCAST, kDOUBLE, kENCODING_DICT, kFLOAT, kUNNEST, ExpressionRange::makeDoubleRange(), ExpressionRange::makeFloatRange(), ExpressionRange::makeIntRange(), ExpressionRange::makeInvalidRange(), ExpressionRange::makeNullRange(), Analyzer::Expr::toString(), TRANSIENT_DICT_ID, and VLOG.

818  {
819  if (u_expr->get_optype() == kUNNEST) {
820  return getExpressionRange(u_expr->get_operand(), query_infos, executor, simple_quals);
821  }
822  if (u_expr->get_optype() != kCAST) {
824  }
825  const auto& ti = u_expr->get_type_info();
826  if (ti.is_string() && ti.get_compression() == kENCODING_DICT) {
827  const auto sdp = executor->getStringDictionaryProxy(
828  ti.get_comp_param(), executor->getRowSetMemoryOwner(), true);
829  CHECK(sdp);
830  const auto colvar_operand =
831  dynamic_cast<const Analyzer::ColumnVar*>(u_expr->get_operand());
832  if (colvar_operand) {
833  const auto& colvar_ti = colvar_operand->get_type_info();
834  if (!(colvar_ti.is_none_encoded_string() &&
835  ti.get_comp_param() == TRANSIENT_DICT_ID)) {
836  VLOG(1)
837  << "Unable to determine expression range for dictionary encoded expression "
838  << u_expr->get_operand()->toString() << ", proceeding with invalid range.";
840  }
841  CHECK_EQ(ti.get_comp_param(), TRANSIENT_DICT_ID);
842  CHECK_EQ(sdp->storageEntryCount(), 0UL);
843  const int64_t transient_entries = static_cast<int64_t>(sdp->transientEntryCount());
844  int64_t const tuples_upper_bound = static_cast<int64_t>(
845  std::accumulate(query_infos.cbegin(),
846  query_infos.cend(),
847  size_t(0),
848  [](auto max, auto const& query_info) {
849  return std::max(max, query_info.info.getNumTuples());
850  }));
851  const int64_t conservative_range_min = -1L - transient_entries - tuples_upper_bound;
852  return ExpressionRange::makeIntRange(conservative_range_min, -2L, 0, true);
853  }
854  const auto const_operand =
855  dynamic_cast<const Analyzer::Constant*>(u_expr->get_operand());
856  if (!const_operand) {
857  // casted subquery result. return invalid for now, but we could attempt to pull the
858  // range from the subquery result in the future
859  CHECK(u_expr->get_operand());
860  VLOG(1) << "Unable to determine expression range for dictionary encoded expression "
861  << u_expr->get_operand()->toString() << ", proceeding with invalid range.";
863  }
864 
865  if (const_operand->get_is_null()) {
867  }
868  CHECK(const_operand->get_constval().stringval);
869  const int64_t v = sdp->getIdOfString(*const_operand->get_constval().stringval);
870  return ExpressionRange::makeIntRange(v, v, 0, false);
871  }
872  const auto arg_range =
873  getExpressionRange(u_expr->get_operand(), query_infos, executor, simple_quals);
874  const auto& arg_ti = u_expr->get_operand()->get_type_info();
875  // Timestamp to Date OR Date/Timestamp casts with different precision
876  if ((ti.is_timestamp() && (arg_ti.get_dimension() != ti.get_dimension())) ||
877  ((arg_ti.is_timestamp() && ti.is_date()))) {
878  return getDateTimePrecisionCastRange(arg_range, arg_ti, ti);
879  }
880  switch (arg_range.getType()) {
883  if (ti.is_fp()) {
884  return ti.get_type() == kDOUBLE
886  arg_range.getFpMin(), arg_range.getFpMax(), arg_range.hasNulls())
887  : ExpressionRange::makeFloatRange(arg_range.getFpMin(),
888  arg_range.getFpMax(),
889  arg_range.hasNulls());
890  }
891  if (ti.is_integer()) {
892  return ExpressionRange::makeIntRange(std::floor(arg_range.getFpMin()),
893  std::ceil(arg_range.getFpMax()),
894  0,
895  arg_range.hasNulls());
896  }
897  break;
898  }
900  if (ti.is_decimal()) {
901  CHECK_EQ(int64_t(0), arg_range.getBucket());
902  const int64_t scale = exp_to_scale(ti.get_scale() - arg_ti.get_scale());
903  return ExpressionRange::makeIntRange(arg_range.getIntMin() * scale,
904  arg_range.getIntMax() * scale,
905  0,
906  arg_range.hasNulls());
907  }
908  if (arg_ti.is_decimal()) {
909  CHECK_EQ(int64_t(0), arg_range.getBucket());
910  const int64_t scale = exp_to_scale(arg_ti.get_scale());
911  const int64_t scale_half = scale / 2;
912  if (ti.is_fp()) {
913  return fpRangeFromDecimal(arg_range, scale, ti);
914  }
915  return ExpressionRange::makeIntRange((arg_range.getIntMin() - scale_half) / scale,
916  (arg_range.getIntMax() + scale_half) / scale,
917  0,
918  arg_range.hasNulls());
919  }
920  if (ti.is_integer() || ti.is_time()) {
921  return arg_range;
922  }
923  if (ti.get_type() == kFLOAT) {
925  arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
926  }
927  if (ti.get_type() == kDOUBLE) {
929  arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
930  }
931  break;
932  }
934  break;
935  default:
936  CHECK(false);
937  }
939 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
static ExpressionRange makeNullRange()
Definition: sqldefs.h:48
static ExpressionRange makeFloatRange(const float fp_min, const float fp_max, const bool has_nulls)
DEVICE auto accumulate(ARGS &&...args)
Definition: gpu_enabled.h:42
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
static ExpressionRange makeDoubleRange(const double fp_min, const double fp_max, const bool has_nulls)
virtual std::string toString() const =0
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:259
const Expr * get_operand() const
Definition: Analyzer.h:378
#define CHECK(condition)
Definition: Logger.h:222
uint64_t exp_to_scale(const unsigned exp)
static ExpressionRange makeInvalidRange()
ExpressionRange getDateTimePrecisionCastRange(const ExpressionRange &arg_range, const SQLTypeInfo &oper_ti, const SQLTypeInfo &target_ti)
SQLOps get_optype() const
Definition: Analyzer.h:377
#define VLOG(n)
Definition: Logger.h:316
ExpressionRange fpRangeFromDecimal(const ExpressionRange &arg_range, const int64_t scale, const SQLTypeInfo &target_ti)

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::ExtractExpr extract_expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor,
boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>  simple_quals 
)

Definition at line 941 of file ExpressionRange.cpp.

References CHECK, ExtractFromTime(), Analyzer::ExtractExpr::get_field(), Analyzer::ExtractExpr::get_from_expr(), DateTimeUtils::get_timestamp_precision_scale(), Analyzer::Expr::get_type_info(), getExpressionRange(), ExpressionRange::getType(), Integer, Invalid, kDATEEPOCH, kDAY, kDOW, kDOY, kEPOCH, kHOUR, kISODOW, kMICROSECOND, kMILLISECOND, kMINUTE, kMONTH, kNANOSECOND, kQUARTER, kQUARTERDAY, kSECOND, kWEEK, kWEEK_SATURDAY, kWEEK_SUNDAY, kYEAR, ExpressionRange::makeIntRange(), and ExpressionRange::makeInvalidRange().

945  {
946  const int32_t extract_field{extract_expr->get_field()};
947  const auto arg_range = getExpressionRange(
948  extract_expr->get_from_expr(), query_infos, executor, simple_quals);
949  const bool has_nulls =
950  arg_range.getType() == ExpressionRangeType::Invalid || arg_range.hasNulls();
951  const auto& extract_expr_ti = extract_expr->get_from_expr()->get_type_info();
952  switch (extract_field) {
953  case kYEAR: {
954  if (arg_range.getType() == ExpressionRangeType::Invalid) {
956  }
957  CHECK(arg_range.getType() == ExpressionRangeType::Integer);
958  const int64_t year_range_min =
959  extract_expr_ti.is_high_precision_timestamp()
960  ? ExtractFromTime(
961  kYEAR,
962  arg_range.getIntMin() /
963  get_timestamp_precision_scale(extract_expr_ti.get_dimension()))
964  : ExtractFromTime(kYEAR, arg_range.getIntMin());
965  const int64_t year_range_max =
966  extract_expr_ti.is_high_precision_timestamp()
967  ? ExtractFromTime(
968  kYEAR,
969  arg_range.getIntMax() /
970  get_timestamp_precision_scale(extract_expr_ti.get_dimension()))
971  : ExtractFromTime(kYEAR, arg_range.getIntMax());
973  year_range_min, year_range_max, 0, arg_range.hasNulls());
974  }
975  case kEPOCH:
976  case kDATEEPOCH:
977  return arg_range;
978  case kQUARTERDAY:
979  case kQUARTER:
980  return ExpressionRange::makeIntRange(1, 4, 0, has_nulls);
981  case kMONTH:
982  return ExpressionRange::makeIntRange(1, 12, 0, has_nulls);
983  case kDAY:
984  return ExpressionRange::makeIntRange(1, 31, 0, has_nulls);
985  case kHOUR:
986  return ExpressionRange::makeIntRange(0, 23, 0, has_nulls);
987  case kMINUTE:
988  return ExpressionRange::makeIntRange(0, 59, 0, has_nulls);
989  case kSECOND:
990  return ExpressionRange::makeIntRange(0, 60, 0, has_nulls);
991  case kMILLISECOND:
992  return ExpressionRange::makeIntRange(0, 999, 0, has_nulls);
993  case kMICROSECOND:
994  return ExpressionRange::makeIntRange(0, 999999, 0, has_nulls);
995  case kNANOSECOND:
996  return ExpressionRange::makeIntRange(0, 999999999, 0, has_nulls);
997  case kDOW:
998  return ExpressionRange::makeIntRange(0, 6, 0, has_nulls);
999  case kISODOW:
1000  return ExpressionRange::makeIntRange(1, 7, 0, has_nulls);
1001  case kDOY:
1002  return ExpressionRange::makeIntRange(1, 366, 0, has_nulls);
1003  case kWEEK:
1004  case kWEEK_SUNDAY:
1005  case kWEEK_SATURDAY:
1006  return ExpressionRange::makeIntRange(1, 53, 0, has_nulls);
1007  default:
1008  CHECK(false);
1009  }
1011 }
const Expr * get_from_expr() const
Definition: Analyzer.h:1304
ExtractField get_field() const
Definition: Analyzer.h:1303
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval)
ExpressionRangeType getType() const
#define CHECK(condition)
Definition: Logger.h:222
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
static ExpressionRange makeInvalidRange()

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::DatetruncExpr datetrunc_expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor,
boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>  simple_quals 
)

Definition at line 1013 of file ExpressionRange.cpp.

References anonymous_namespace{ExpressionRange.cpp}::get_conservative_datetrunc_bucket(), Analyzer::DatetruncExpr::get_field(), Analyzer::DatetruncExpr::get_from_expr(), DateTimeUtils::get_timestamp_precision_scale(), Analyzer::Expr::get_type_info(), DateTimeTranslator::getDateTruncConstantValue(), getExpressionRange(), Invalid, ExpressionRange::makeIntRange(), and ExpressionRange::makeInvalidRange().

1017  {
1018  const auto arg_range = getExpressionRange(
1019  datetrunc_expr->get_from_expr(), query_infos, executor, simple_quals);
1020  if (arg_range.getType() == ExpressionRangeType::Invalid) {
1022  }
1023  const auto& datetrunc_expr_ti = datetrunc_expr->get_from_expr()->get_type_info();
1024  const int64_t min_ts = DateTimeTranslator::getDateTruncConstantValue(
1025  arg_range.getIntMin(), datetrunc_expr->get_field(), datetrunc_expr_ti);
1026  const int64_t max_ts = DateTimeTranslator::getDateTruncConstantValue(
1027  arg_range.getIntMax(), datetrunc_expr->get_field(), datetrunc_expr_ti);
1028  const int64_t bucket =
1029  datetrunc_expr_ti.is_high_precision_timestamp()
1030  ? get_conservative_datetrunc_bucket(datetrunc_expr->get_field()) *
1032  datetrunc_expr_ti.get_dimension())
1033  : get_conservative_datetrunc_bucket(datetrunc_expr->get_field());
1034 
1035  return ExpressionRange::makeIntRange(min_ts, max_ts, bucket, arg_range.hasNulls());
1036 }
DatetruncField get_field() const
Definition: Analyzer.h:1428
int64_t get_conservative_datetrunc_bucket(const DatetruncField datetrunc_field)
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
const Expr * get_from_expr() const
Definition: Analyzer.h:1429
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
static ExpressionRange makeInvalidRange()
static int64_t getDateTruncConstantValue(const int64_t &timeval, const DatetruncField &field, const SQLTypeInfo &ti)

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::WidthBucketExpr width_bucket_expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor,
boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>  simple_quals 
)

Definition at line 1038 of file ExpressionRange.cpp.

References CHECK, Analyzer::WidthBucketExpr::compute_bucket(), Double, Float, Analyzer::WidthBucketExpr::get_partition_count(), Analyzer::WidthBucketExpr::get_partition_count_val(), Analyzer::WidthBucketExpr::get_target_value(), getExpressionRange(), ExpressionRange::getFpMax(), ExpressionRange::getFpMin(), ExpressionRange::hasNulls(), Integer, Invalid, Analyzer::WidthBucketExpr::is_constant_expr(), ExpressionRange::makeIntRange(), run_benchmark_import::res, and width_bucket_expr().

1042  {
1043  auto target_value_expr = width_bucket_expr->get_target_value();
1044  auto target_value_range = getExpressionRange(target_value_expr, query_infos, executor);
1045  auto target_ti = target_value_expr->get_type_info();
1046  if (width_bucket_expr->is_constant_expr() &&
1047  target_value_range.getType() != ExpressionRangeType::Invalid) {
1048  auto const_target_value = dynamic_cast<const Analyzer::Constant*>(target_value_expr);
1049  if (const_target_value) {
1050  if (const_target_value->get_is_null()) {
1051  // null constant, return default width_bucket range
1053  0, width_bucket_expr->get_partition_count_val(), 0, true);
1054  } else {
1055  CHECK(target_value_range.getFpMax() == target_value_range.getFpMin());
1056  auto target_value_bucket =
1057  width_bucket_expr->compute_bucket(target_value_range.getFpMax(), target_ti);
1059  target_value_bucket, target_value_bucket, 0, target_value_range.hasNulls());
1060  }
1061  }
1062  // compute possible bucket range based on lower and upper bound constants
1063  // to elucidate a target bucket range
1064  const auto target_value_range_with_qual =
1065  getExpressionRange(target_value_expr, query_infos, executor, simple_quals);
1066  auto compute_bucket_range = [&width_bucket_expr](const ExpressionRange& target_range,
1067  SQLTypeInfo ti) {
1068  // we casted bucket bound exprs to double
1069  auto lower_bound_bucket =
1070  width_bucket_expr->compute_bucket<double>(target_range.getFpMin(), ti);
1071  auto upper_bound_bucket =
1072  width_bucket_expr->compute_bucket<double>(target_range.getFpMax(), ti);
1074  lower_bound_bucket, upper_bound_bucket, 0, target_range.hasNulls());
1075  };
1076  auto res_range = compute_bucket_range(target_value_range_with_qual, target_ti);
1077  // check target_value expression's col range to be not nullable iff it has its filter
1078  // expression i.e., in simple_quals
1079  // todo (yoonmin) : need to search simple_quals to cover more cases?
1080  if (target_value_range.getFpMin() < target_value_range_with_qual.getFpMin() ||
1081  target_value_range.getFpMax() > target_value_range_with_qual.getFpMax()) {
1082  res_range.setNulls(false);
1083  }
1084  return res_range;
1085  } else {
1086  // we cannot determine a possibility of skipping oob check safely
1087  const bool has_nulls = target_value_range.getType() == ExpressionRangeType::Invalid ||
1088  target_value_range.hasNulls();
1089  auto partition_expr_range = getExpressionRange(
1090  width_bucket_expr->get_partition_count(), query_infos, executor, simple_quals);
1091  auto res = ExpressionRange::makeIntRange(0, INT32_MAX, 0, has_nulls);
1092  switch (partition_expr_range.getType()) {
1094  res.setIntMax(partition_expr_range.getIntMax() + 1);
1095  break;
1096  }
1099  res.setIntMax(static_cast<int64_t>(partition_expr_range.getFpMax()) + 1);
1100  break;
1101  }
1102  default:
1103  break;
1104  }
1105  return res;
1106  }
1107 }
const Expr * get_partition_count() const
Definition: Analyzer.h:1043
bool is_constant_expr() const
Definition: Analyzer.h:1118
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
bool hasNulls() const
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
double getFpMax() const
double getFpMin() const
int32_t get_partition_count_val() const
Definition: Analyzer.cpp:3651
const Expr * get_target_value() const
Definition: Analyzer.h:1040
#define CHECK(condition)
Definition: Logger.h:222
int32_t compute_bucket(T target_const_val, SQLTypeInfo &ti) const
Definition: Analyzer.h:1081
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int32_t width_bucket_expr(const double target_value, const bool reversed, const double lower_bound, const double upper_bound, const int32_t partition_count)

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::Expr expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor,
boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>  simple_quals 
)

Definition at line 335 of file ExpressionRange.cpp.

References Analyzer::Expr::get_type_info(), getExpressionRange(), ExpressionRange::makeInvalidRange(), and ExpressionRange::typeSupportsRange().

339  {
342  } else if (auto bin_oper_expr = dynamic_cast<const Analyzer::BinOper*>(expr)) {
343  return getExpressionRange(bin_oper_expr, query_infos, executor, simple_quals);
344  } else if (auto constant_expr = dynamic_cast<const Analyzer::Constant*>(expr)) {
345  return getExpressionRange(constant_expr);
346  } else if (auto column_var_expr = dynamic_cast<const Analyzer::ColumnVar*>(expr)) {
347  return getExpressionRange(column_var_expr, query_infos, executor, simple_quals);
348  } else if (auto string_oper_expr = dynamic_cast<const Analyzer::StringOper*>(expr)) {
349  return getExpressionRange(string_oper_expr, executor);
350  } else if (auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr)) {
351  return getExpressionRange(like_expr);
352  } else if (auto case_expr = dynamic_cast<const Analyzer::CaseExpr*>(expr)) {
353  return getExpressionRange(case_expr, query_infos, executor);
354  } else if (auto u_expr = dynamic_cast<const Analyzer::UOper*>(expr)) {
355  return getExpressionRange(u_expr, query_infos, executor, simple_quals);
356  } else if (auto extract_expr = dynamic_cast<const Analyzer::ExtractExpr*>(expr)) {
357  return getExpressionRange(extract_expr, query_infos, executor, simple_quals);
358  } else if (auto datetrunc_expr = dynamic_cast<const Analyzer::DatetruncExpr*>(expr)) {
359  return getExpressionRange(datetrunc_expr, query_infos, executor, simple_quals);
360  } else if (auto width_expr = dynamic_cast<const Analyzer::WidthBucketExpr*>(expr)) {
361  return getExpressionRange(width_expr, query_infos, executor, simple_quals);
362  }
364 }
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
static bool typeSupportsRange(const SQLTypeInfo &ti)
static ExpressionRange makeInvalidRange()

+ Here is the call graph for this function:

ExpressionRange getLeafColumnRange ( const Analyzer::ColumnVar col_expr,
const std::vector< InputTableInfo > &  query_infos,
const Executor executor,
const bool  is_outer_join_proj 
)

Definition at line 522 of file ExpressionRange.cpp.

References CHECK, CHECK_EQ, CHECK_GT, dtDAY, extract_max_stat_fp_type(), extract_max_stat_int_type(), extract_min_stat_fp_type(), extract_min_stat_int_type(), FIND_STAT_FRAG, Analyzer::ColumnVar::get_column_id(), anonymous_namespace{ExpressionRange.cpp}::get_conservative_datetrunc_bucket(), SQLTypeInfo::get_elem_type(), get_logical_type_info(), Analyzer::ColumnVar::get_table_id(), Analyzer::Expr::get_type_info(), SQLTypeInfo::is_array(), kBIGINT, kBOOLEAN, kCHAR, kDATE, kDECIMAL, kDOUBLE, kENCODING_DICT, kFLOAT, kINT, kNUMERIC, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, ExpressionRange::makeDoubleRange(), ExpressionRange::makeFloatRange(), ExpressionRange::makeIntRange(), and ExpressionRange::makeInvalidRange().

Referenced by Executor::computeColRangesCache(), and getExpressionRange().

525  {
526  bool has_nulls = is_outer_join_proj;
527  int col_id = col_expr->get_column_id();
528  const auto& col_phys_ti = col_expr->get_type_info().is_array()
529  ? col_expr->get_type_info().get_elem_type()
530  : col_expr->get_type_info();
531  const auto col_ti = get_logical_type_info(col_phys_ti);
532  switch (col_ti.get_type()) {
533  case kTEXT:
534  case kCHAR:
535  case kVARCHAR:
536  CHECK_EQ(kENCODING_DICT, col_ti.get_compression());
537  case kBOOLEAN:
538  case kTINYINT:
539  case kSMALLINT:
540  case kINT:
541  case kBIGINT:
542  case kDECIMAL:
543  case kNUMERIC:
544  case kDATE:
545  case kTIMESTAMP:
546  case kTIME:
547  case kFLOAT:
548  case kDOUBLE: {
549  std::optional<size_t> ti_idx;
550  for (size_t i = 0; i < query_infos.size(); ++i) {
551  if (col_expr->get_table_id() == query_infos[i].table_id) {
552  ti_idx = i;
553  break;
554  }
555  }
556  CHECK(ti_idx);
557  const auto& query_info = query_infos[*ti_idx].info;
558  const auto& fragments = query_info.fragments;
559  const auto cd = executor->getColumnDescriptor(col_expr);
560  if (cd && cd->isVirtualCol) {
561  CHECK(cd->columnName == "rowid");
562  CHECK_EQ(kBIGINT, col_ti.get_type());
563  const int64_t num_tuples = query_info.getNumTuples();
565  0, std::max(num_tuples - 1, int64_t(0)), 0, has_nulls);
566  }
567  if (query_info.getNumTuples() == 0) {
568  // The column doesn't contain any values, synthesize an empty range.
569  if (col_ti.is_fp()) {
570  return col_ti.get_type() == kFLOAT
571  ? ExpressionRange::makeFloatRange(0, -1, false)
572  : ExpressionRange::makeDoubleRange(0, -1, false);
573  }
574  return ExpressionRange::makeIntRange(0, -1, 0, false);
575  }
576  std::vector<size_t> nonempty_fragment_indices;
577  for (size_t i = 0; i < fragments.size(); ++i) {
578  const auto& fragment = fragments[i];
579  if (!fragment.isEmptyPhysicalFragment()) {
580  nonempty_fragment_indices.push_back(i);
581  }
582  }
583  FIND_STAT_FRAG(min);
584  FIND_STAT_FRAG(max);
585  const auto& min_frag = fragments[*min_frag_index];
586  const auto min_it = min_frag.getChunkMetadataMap().find(col_id);
587  if (min_it == min_frag.getChunkMetadataMap().end()) {
589  }
590  const auto& max_frag = fragments[*max_frag_index];
591  const auto max_it = max_frag.getChunkMetadataMap().find(col_id);
592  CHECK(max_it != max_frag.getChunkMetadataMap().end());
593  for (const auto& fragment : fragments) {
594  const auto it = fragment.getChunkMetadataMap().find(col_id);
595  if (it != fragment.getChunkMetadataMap().end()) {
596  if (it->second->chunkStats.has_nulls) {
597  has_nulls = true;
598  break;
599  }
600  }
601  }
602 
603  // Detect FSI placeholder metadata. If we let this through it will be treated as an
604  // empty range, when it really implies an unknown range.
605  if (min_it->second->isPlaceholder() || max_it->second->isPlaceholder()) {
607  }
608 
609  if (col_ti.is_fp()) {
610  const auto min_val = extract_min_stat_fp_type(min_it->second->chunkStats, col_ti);
611  const auto max_val = extract_max_stat_fp_type(max_it->second->chunkStats, col_ti);
612  return col_ti.get_type() == kFLOAT
613  ? ExpressionRange::makeFloatRange(min_val, max_val, has_nulls)
614  : ExpressionRange::makeDoubleRange(min_val, max_val, has_nulls);
615  }
616  const auto min_val = extract_min_stat_int_type(min_it->second->chunkStats, col_ti);
617  const auto max_val = extract_max_stat_int_type(max_it->second->chunkStats, col_ti);
618  if (max_val < min_val) {
619  // The column doesn't contain any non-null values, synthesize an empty range.
620  CHECK_GT(min_val, 0);
621  return ExpressionRange::makeIntRange(0, -1, 0, has_nulls);
622  }
623  const int64_t bucket =
624  col_ti.get_type() == kDATE ? get_conservative_datetrunc_bucket(dtDAY) : 0;
625  return ExpressionRange::makeIntRange(min_val, max_val, bucket, has_nulls);
626  }
627  default:
628  break;
629  }
631 }
int get_table_id() const
Definition: Analyzer.h:200
#define CHECK_EQ(x, y)
Definition: Logger.h:230
#define FIND_STAT_FRAG(stat_name)
Definition: sqltypes.h:49
double extract_max_stat_fp_type(const ChunkStats &stats, const SQLTypeInfo &ti)
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:1087
#define CHECK_GT(x, y)
Definition: Logger.h:234
int64_t extract_max_stat_int_type(const ChunkStats &stats, const SQLTypeInfo &ti)
static ExpressionRange makeFloatRange(const float fp_min, const float fp_max, const bool has_nulls)
int64_t get_conservative_datetrunc_bucket(const DatetruncField datetrunc_field)
double extract_min_stat_fp_type(const ChunkStats &stats, const SQLTypeInfo &ti)
int64_t extract_min_stat_int_type(const ChunkStats &stats, const SQLTypeInfo &ti)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
Definition: sqltypes.h:52
Definition: sqltypes.h:53
Definition: sqltypes.h:41
bool g_enable_watchdog false
Definition: Execute.cpp:79
#define CHECK(condition)
Definition: Logger.h:222
Definition: sqltypes.h:45
int get_column_id() const
Definition: Analyzer.h:201
static ExpressionRange makeInvalidRange()
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:865
bool is_array() const
Definition: sqltypes.h:518

+ Here is the call graph for this function:

+ Here is the caller graph for this function: