OmniSciDB  a5dc49c757
 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 451 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:37
Definition: sqldefs.h:38
Definition: sqldefs.h:32
Definition: sqldefs.h:36
double getFpMax() const
double getFpMin() const
void setFpMax(const double fp_max)
Definition: sqldefs.h:35
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: Datum.h:76
#define CHECK(condition)
Definition: Logger.h:291
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
Definition: Datum.h:71

+ 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:37
Definition: sqldefs.h:38
Definition: sqldefs.h:32
void setIntMin(const int64_t int_min)
Definition: sqldefs.h:36
void setIntMax(const int64_t int_max)
int64_t getIntMax() const
Definition: sqldefs.h:35

+ 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::getColumnKey(), 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->getColumnKey() != col_expr->getColumnKey()) {
173  continue;
174  }
175  const Analyzer::Expr* right_operand = qual_bin_oper->get_right_operand();
176  auto qual_const = dynamic_cast<const Analyzer::Constant*>(right_operand);
177  if (!qual_const) {
178  continue;
179  }
180  if (qual_range.getType() == ExpressionRangeType::Float ||
181  qual_range.getType() == ExpressionRangeType::Double) {
182  apply_fp_qual(qual_const->get_constval(),
183  qual_const->get_type_info().get_type(),
184  qual_bin_oper->get_optype(),
185  qual_range);
186  } else if ((qual_col->get_type_info().is_timestamp() ||
187  qual_const->get_type_info().is_timestamp()) &&
188  (qual_col->get_type_info().get_dimension() !=
189  qual_const->get_type_info().get_dimension())) {
190  apply_hpt_qual(qual_const->get_constval(),
191  qual_const->get_type_info().get_type(),
192  qual_const->get_type_info().get_dimension(),
193  qual_col->get_type_info().get_dimension(),
194  qual_bin_oper->get_optype(),
195  qual_range);
196  } else {
197  apply_int_qual(qual_const->get_constval(),
198  qual_const->get_type_info().get_type(),
199  qual_bin_oper->get_optype(),
200  qual_range);
201  }
202  }
203  return qual_range;
204 }
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)
const shared::ColumnKey & getColumnKey() const
Definition: Analyzer.h:198

+ 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 373 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().

377  {
378  const auto& lhs =
379  getExpressionRange(expr->get_left_operand(), query_infos, executor, simple_quals);
380  const auto& rhs =
381  getExpressionRange(expr->get_right_operand(), query_infos, executor, simple_quals);
382  switch (expr->get_optype()) {
383  case kPLUS:
384  return lhs + rhs;
385  case kMINUS:
386  return lhs - rhs;
387  case kMULTIPLY:
388  return lhs * rhs;
389  case kDIVIDE: {
390  const auto& lhs_type = expr->get_left_operand()->get_type_info();
391  if (lhs_type.is_decimal() && lhs.getType() != ExpressionRangeType::Invalid) {
392  CHECK(lhs.getType() == ExpressionRangeType::Integer);
393  const auto adjusted_lhs = ExpressionRange::makeIntRange(
394  scale_up_interval_endpoint(lhs.getIntMin(), lhs_type),
395  scale_up_interval_endpoint(lhs.getIntMax(), lhs_type),
396  0,
397  lhs.hasNulls());
398  return adjusted_lhs / rhs;
399  }
400  return lhs / rhs;
401  }
402  default:
403  break;
404  }
406 }
const Expr * get_right_operand() const
Definition: Analyzer.h:456
Definition: sqldefs.h:43
int64_t scale_up_interval_endpoint(const int64_t endpoint, const SQLTypeInfo &ti)
SQLOps get_optype() const
Definition: Analyzer.h:452
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:79
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
Definition: sqldefs.h:42
#define CHECK(condition)
Definition: Logger.h:291
const Expr * get_left_operand() const
Definition: Analyzer.h:455
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 408 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().

408  {
409  if (constant_expr->get_is_null()) {
411  }
412  const auto constant_type = constant_expr->get_type_info().get_type();
413  const auto datum = constant_expr->get_constval();
414  switch (constant_type) {
415  case kTINYINT: {
416  const int64_t v = datum.tinyintval;
417  return ExpressionRange::makeIntRange(v, v, 0, false);
418  }
419  case kSMALLINT: {
420  const int64_t v = datum.smallintval;
421  return ExpressionRange::makeIntRange(v, v, 0, false);
422  }
423  case kINT: {
424  const int64_t v = datum.intval;
425  return ExpressionRange::makeIntRange(v, v, 0, false);
426  }
427  case kBIGINT:
428  case kNUMERIC:
429  case kDECIMAL: {
430  const int64_t v = datum.bigintval;
431  return ExpressionRange::makeIntRange(v, v, 0, false);
432  }
433  case kTIME:
434  case kTIMESTAMP:
435  case kDATE: {
436  const int64_t v = datum.bigintval;
437  return ExpressionRange::makeIntRange(v, v, 0, false);
438  }
439  case kFLOAT: {
440  return ExpressionRange::makeFloatRange(datum.floatval, datum.floatval, false);
441  }
442  case kDOUBLE: {
443  return ExpressionRange::makeDoubleRange(datum.doubleval, datum.doubleval, false);
444  }
445  default:
446  break;
447  }
449 }
Definition: sqltypes.h:76
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:80
Definition: sqltypes.h:72
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 634 of file ExpressionRange.cpp.

References apply_simple_quals(), CHECK_GE, CHECK_LT, Analyzer::ColumnVar::get_rte_idx(), Analyzer::ColumnVar::getColumnKey(), getLeafColumnRange(), and PhysicalInput::table_id.

638  {
639  const int rte_idx = col_expr->get_rte_idx();
640  CHECK_GE(rte_idx, 0);
641  CHECK_LT(static_cast<size_t>(rte_idx), query_infos.size());
642  bool is_outer_join_proj = rte_idx > 0 && executor->containsLeftDeepOuterJoin();
643  const auto& column_key = col_expr->getColumnKey();
644  if (column_key.table_id > 0) {
645  auto col_range = executor->getColRange(
646  PhysicalInput{column_key.column_id, column_key.table_id, column_key.db_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 }
#define CHECK_GE(x, y)
Definition: Logger.h:306
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:303
const shared::ColumnKey & getColumnKey() const
Definition: Analyzer.h:198
int32_t get_rte_idx() const
Definition: Analyzer.h:202

+ 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, CHECK_NE, Analyzer::Expr::get_type_info(), Analyzer::StringOper::getChainedStringOpExprs(), ExpressionRange::makeIntRange(), ExpressionRange::makeInvalidRange(), Analyzer::StringOper::requiresPerRowTranslation(), RowSetMemoryOwner::SOURCE_UNION, and TRANSIENT_DICT_ID.

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->get_type_info(),
671  chained_string_op->getLiteralArgs());
672  string_op_infos.emplace_back(string_op_info);
673  }
674 
675  const auto expr_ti = string_oper_expr->get_type_info();
676  if (expr_ti.is_string() && !string_oper_expr->requiresPerRowTranslation()) {
677  // We can only statically know the range if dictionary translation occured (which
678  // means the output type is a dictionary-encoded text col and is not transient, as
679  // transient dictionaries are used as targets for on-the-fly translation
680  CHECK(expr_ti.is_dict_encoded_string());
681 
682  const auto& dict_key = expr_ti.getStringDictKey();
683  CHECK_NE(dict_key.dict_id, TRANSIENT_DICT_ID);
684  const auto translation_map = executor->getStringProxyTranslationMap(
685  dict_key,
686  dict_key,
688  string_op_infos,
689  executor->getRowSetMemoryOwner(),
690  true);
691 
692  // Todo(todd): Track null presence in StringDictionaryProxy::IdMap
693  return ExpressionRange::makeIntRange(translation_map->rangeStart(),
694  translation_map->rangeEnd() - 1,
695  0 /* bucket */,
696  true /* assume has nulls */);
697 
698  } else {
699  // Todo(todd): Get min/max range stats
700  // Below works fine, we just can't take advantage of low-cardinality
701  // group by optimizations
703  }
704 }
bool requiresPerRowTranslation() const
Definition: Analyzer.h:1704
#define TRANSIENT_DICT_ID
Definition: DbObjectKeys.h:24
#define CHECK_NE(x, y)
Definition: Logger.h:302
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
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:1601
#define CHECK(condition)
Definition: Logger.h:291
static ExpressionRange makeInvalidRange()
std::vector< std::shared_ptr< Analyzer::Expr > > getChainedStringOpExprs() const
Definition: Analyzer.h:1700

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::LikeExpr like_expr)

Definition at line 706 of file ExpressionRange.cpp.

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

706  {
707  const auto& ti = like_expr->get_type_info();
708  CHECK(ti.is_boolean());
709  const auto& arg_ti = like_expr->get_arg()->get_type_info();
711  arg_ti.get_notnull() ? 0 : inline_int_null_val(ti), 1, 0, false);
712 }
const Expr * get_arg() const
Definition: Analyzer.h:1061
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
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:291
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 714 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.

716  {
717  const auto& expr_pair_list = case_expr->get_expr_pair_list();
718  auto expr_range = ExpressionRange::makeInvalidRange();
719  bool has_nulls = false;
720  for (const auto& expr_pair : expr_pair_list) {
721  CHECK_EQ(expr_pair.first->get_type_info().get_type(), kBOOLEAN);
722  const auto crt_range =
723  getExpressionRange(expr_pair.second.get(), query_infos, executor);
724  if (crt_range.getType() == ExpressionRangeType::Null) {
725  has_nulls = true;
726  continue;
727  }
728  if (crt_range.getType() == ExpressionRangeType::Invalid) {
730  }
731  expr_range = (expr_range.getType() != ExpressionRangeType::Invalid)
732  ? expr_range || crt_range
733  : crt_range;
734  }
735  if (has_nulls && !(expr_range.getType() == ExpressionRangeType::Invalid)) {
736  expr_range.setHasNulls();
737  }
738  const auto else_expr = case_expr->get_else_expr();
739  CHECK(else_expr);
740  const auto else_null_expr = dynamic_cast<const Analyzer::Constant*>(else_expr);
741  if (else_null_expr && else_null_expr->get_is_null()) {
742  expr_range.setHasNulls();
743  return expr_range;
744  }
745  return expr_range || getExpressionRange(else_expr, query_infos, executor);
746 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
const Expr * get_else_expr() const
Definition: Analyzer.h:1387
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:291
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:1384

+ 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 819 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, kDATE, kDOUBLE, kENCODING_DICT, kFLOAT, kTIME, kUNNEST, ExpressionRange::makeDoubleRange(), ExpressionRange::makeFloatRange(), ExpressionRange::makeIntRange(), ExpressionRange::makeInvalidRange(), ExpressionRange::makeNullRange(), Analyzer::Expr::toString(), TRANSIENT_DICT_ID, and VLOG.

823  {
824  if (u_expr->get_optype() == kUNNEST) {
825  return getExpressionRange(u_expr->get_operand(), query_infos, executor, simple_quals);
826  }
827  if (u_expr->get_optype() != kCAST) {
829  }
830  const auto& ti = u_expr->get_type_info();
831  if (ti.is_string() && ti.get_compression() == kENCODING_DICT) {
832  const auto sdp = executor->getStringDictionaryProxy(
833  ti.getStringDictKey(), executor->getRowSetMemoryOwner(), true);
834  CHECK(sdp);
835  const auto colvar_operand =
836  dynamic_cast<const Analyzer::ColumnVar*>(u_expr->get_operand());
837  if (colvar_operand) {
838  const auto& colvar_ti = colvar_operand->get_type_info();
839  if (!(colvar_ti.is_none_encoded_string() &&
840  ti.getStringDictKey().isTransientDict())) {
841  VLOG(1)
842  << "Unable to determine expression range for dictionary encoded expression "
843  << u_expr->get_operand()->toString() << ", proceeding with invalid range.";
845  }
846  CHECK_EQ(ti.getStringDictKey().dict_id, TRANSIENT_DICT_ID);
847  CHECK_EQ(sdp->storageEntryCount(), 0UL);
848  const int64_t transient_entries = static_cast<int64_t>(sdp->transientEntryCount());
849  int64_t const tuples_upper_bound = static_cast<int64_t>(
850  std::accumulate(query_infos.cbegin(),
851  query_infos.cend(),
852  size_t(0),
853  [](auto max, auto const& query_info) {
854  return std::max(max, query_info.info.getNumTuples());
855  }));
856  const int64_t conservative_range_min = -1L - transient_entries - tuples_upper_bound;
857  return ExpressionRange::makeIntRange(conservative_range_min, -2L, 0, true);
858  }
859  const auto const_operand =
860  dynamic_cast<const Analyzer::Constant*>(u_expr->get_operand());
861  if (!const_operand) {
862  // casted subquery result. return invalid for now, but we could attempt to pull the
863  // range from the subquery result in the future
864  CHECK(u_expr->get_operand());
865  VLOG(1) << "Unable to determine expression range for dictionary encoded expression "
866  << u_expr->get_operand()->toString() << ", proceeding with invalid range.";
868  }
869 
870  if (const_operand->get_is_null()) {
872  }
873  CHECK(const_operand->get_constval().stringval);
874  const int64_t v = sdp->getIdOfString(*const_operand->get_constval().stringval);
875  return ExpressionRange::makeIntRange(v, v, 0, false);
876  }
877  const auto arg_range =
878  getExpressionRange(u_expr->get_operand(), query_infos, executor, simple_quals);
879  const auto& arg_ti = u_expr->get_operand()->get_type_info();
880  // Timestamp to Date OR Date/Timestamp casts with different precision
881  if ((ti.is_timestamp() && (arg_ti.get_dimension() != ti.get_dimension())) ||
882  ((arg_ti.is_timestamp() && ti.is_any<kDATE, kTIME>()))) {
883  return getDateTimePrecisionCastRange(arg_range, arg_ti, ti);
884  }
885  switch (arg_range.getType()) {
888  if (ti.is_fp()) {
889  return ti.get_type() == kDOUBLE
891  arg_range.getFpMin(), arg_range.getFpMax(), arg_range.hasNulls())
892  : ExpressionRange::makeFloatRange(arg_range.getFpMin(),
893  arg_range.getFpMax(),
894  arg_range.hasNulls());
895  }
896  if (ti.is_integer()) {
897  return ExpressionRange::makeIntRange(std::floor(arg_range.getFpMin()),
898  std::ceil(arg_range.getFpMax()),
899  0,
900  arg_range.hasNulls());
901  }
902  break;
903  }
905  if (ti.is_decimal()) {
906  CHECK_EQ(int64_t(0), arg_range.getBucket());
907  const int64_t scale = exp_to_scale(ti.get_scale() - arg_ti.get_scale());
908  return ExpressionRange::makeIntRange(arg_range.getIntMin() * scale,
909  arg_range.getIntMax() * scale,
910  0,
911  arg_range.hasNulls());
912  }
913  if (arg_ti.is_decimal()) {
914  CHECK_EQ(int64_t(0), arg_range.getBucket());
915  const int64_t scale = exp_to_scale(arg_ti.get_scale());
916  const int64_t scale_half = scale / 2;
917  if (ti.is_fp()) {
918  return fpRangeFromDecimal(arg_range, scale, ti);
919  }
920  return ExpressionRange::makeIntRange((arg_range.getIntMin() - scale_half) / scale,
921  (arg_range.getIntMax() + scale_half) / scale,
922  0,
923  arg_range.hasNulls());
924  }
925  if (ti.is_integer() || ti.is_time()) {
926  return arg_range;
927  }
928  if (ti.get_type() == kFLOAT) {
930  arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
931  }
932  if (ti.get_type() == kDOUBLE) {
934  arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
935  }
936  break;
937  }
939  break;
940  default:
941  CHECK(false);
942  }
944 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
static ExpressionRange makeNullRange()
Definition: sqltypes.h:76
Definition: sqldefs.h:51
#define TRANSIENT_DICT_ID
Definition: DbObjectKeys.h:24
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:79
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:80
virtual std::string toString() const =0
const Expr * get_operand() const
Definition: Analyzer.h:384
#define CHECK(condition)
Definition: Logger.h:291
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:383
#define VLOG(n)
Definition: Logger.h:388
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 946 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().

950  {
951  const int32_t extract_field{extract_expr->get_field()};
952  const auto arg_range = getExpressionRange(
953  extract_expr->get_from_expr(), query_infos, executor, simple_quals);
954  const bool has_nulls =
955  arg_range.getType() == ExpressionRangeType::Invalid || arg_range.hasNulls();
956  const auto& extract_expr_ti = extract_expr->get_from_expr()->get_type_info();
957  switch (extract_field) {
958  case kYEAR: {
959  if (arg_range.getType() == ExpressionRangeType::Invalid) {
961  }
962  CHECK(arg_range.getType() == ExpressionRangeType::Integer);
963  const int64_t year_range_min =
964  extract_expr_ti.is_high_precision_timestamp()
965  ? ExtractFromTime(
966  kYEAR,
967  arg_range.getIntMin() /
968  get_timestamp_precision_scale(extract_expr_ti.get_dimension()))
969  : ExtractFromTime(kYEAR, arg_range.getIntMin());
970  const int64_t year_range_max =
971  extract_expr_ti.is_high_precision_timestamp()
972  ? ExtractFromTime(
973  kYEAR,
974  arg_range.getIntMax() /
975  get_timestamp_precision_scale(extract_expr_ti.get_dimension()))
976  : ExtractFromTime(kYEAR, arg_range.getIntMax());
978  year_range_min, year_range_max, 0, arg_range.hasNulls());
979  }
980  case kEPOCH:
981  case kDATEEPOCH:
982  return arg_range;
983  case kQUARTERDAY:
984  case kQUARTER:
985  return ExpressionRange::makeIntRange(1, 4, 0, has_nulls);
986  case kMONTH:
987  return ExpressionRange::makeIntRange(1, 12, 0, has_nulls);
988  case kDAY:
989  return ExpressionRange::makeIntRange(1, 31, 0, has_nulls);
990  case kHOUR:
991  return ExpressionRange::makeIntRange(0, 23, 0, has_nulls);
992  case kMINUTE:
993  return ExpressionRange::makeIntRange(0, 59, 0, has_nulls);
994  case kSECOND:
995  return ExpressionRange::makeIntRange(0, 60, 0, has_nulls);
996  case kMILLISECOND:
997  return ExpressionRange::makeIntRange(0, 999, 0, has_nulls);
998  case kMICROSECOND:
999  return ExpressionRange::makeIntRange(0, 999999, 0, has_nulls);
1000  case kNANOSECOND:
1001  return ExpressionRange::makeIntRange(0, 999999999, 0, has_nulls);
1002  case kDOW:
1003  return ExpressionRange::makeIntRange(0, 6, 0, has_nulls);
1004  case kISODOW:
1005  return ExpressionRange::makeIntRange(1, 7, 0, has_nulls);
1006  case kDOY:
1007  return ExpressionRange::makeIntRange(1, 366, 0, has_nulls);
1008  case kWEEK:
1009  case kWEEK_SUNDAY:
1010  case kWEEK_SATURDAY:
1011  return ExpressionRange::makeIntRange(1, 53, 0, has_nulls);
1012  default:
1013  CHECK(false);
1014  }
1016 }
const Expr * get_from_expr() const
Definition: Analyzer.h:1432
ExtractField get_field() const
Definition: Analyzer.h:1431
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:79
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:291
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 1018 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().

1022  {
1023  const auto arg_range = getExpressionRange(
1024  datetrunc_expr->get_from_expr(), query_infos, executor, simple_quals);
1025  if (arg_range.getType() == ExpressionRangeType::Invalid) {
1027  }
1028  const auto& datetrunc_expr_ti = datetrunc_expr->get_from_expr()->get_type_info();
1029  const int64_t min_ts = DateTimeTranslator::getDateTruncConstantValue(
1030  arg_range.getIntMin(), datetrunc_expr->get_field(), datetrunc_expr_ti);
1031  const int64_t max_ts = DateTimeTranslator::getDateTruncConstantValue(
1032  arg_range.getIntMax(), datetrunc_expr->get_field(), datetrunc_expr_ti);
1033  const int64_t bucket =
1034  datetrunc_expr_ti.is_high_precision_timestamp()
1035  ? get_conservative_datetrunc_bucket(datetrunc_expr->get_field()) *
1037  datetrunc_expr_ti.get_dimension())
1038  : get_conservative_datetrunc_bucket(datetrunc_expr->get_field());
1039 
1040  return ExpressionRange::makeIntRange(min_ts, max_ts, bucket, arg_range.hasNulls());
1041 }
DatetruncField get_field() const
Definition: Analyzer.h:1566
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:79
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:1567
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 1043 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().

1047  {
1048  auto target_value_expr = width_bucket_expr->get_target_value();
1049  auto target_value_range = getExpressionRange(target_value_expr, query_infos, executor);
1050  auto target_ti = target_value_expr->get_type_info();
1051  if (width_bucket_expr->is_constant_expr() &&
1052  target_value_range.getType() != ExpressionRangeType::Invalid) {
1053  auto const_target_value = dynamic_cast<const Analyzer::Constant*>(target_value_expr);
1054  if (const_target_value) {
1055  if (const_target_value->get_is_null()) {
1056  // null constant, return default width_bucket range
1058  0, width_bucket_expr->get_partition_count_val(), 0, true);
1059  } else {
1060  CHECK(target_value_range.getFpMax() == target_value_range.getFpMin());
1061  auto target_value_bucket =
1062  width_bucket_expr->compute_bucket(target_value_range.getFpMax());
1064  target_value_bucket, target_value_bucket, 0, target_value_range.hasNulls());
1065  }
1066  }
1067  // compute possible bucket range based on lower and upper bound constants
1068  // to elucidate a target bucket range
1069  const auto target_value_range_with_qual =
1070  getExpressionRange(target_value_expr, query_infos, executor, simple_quals);
1071  auto compute_bucket_range = [&width_bucket_expr](const ExpressionRange& target_range,
1072  SQLTypeInfo ti) {
1073  // we casted bucket bound exprs to double
1074  auto lower_bound_bucket =
1075  width_bucket_expr->compute_bucket(target_range.getFpMin());
1076  auto upper_bound_bucket =
1077  width_bucket_expr->compute_bucket(target_range.getFpMax());
1079  lower_bound_bucket, upper_bound_bucket, 0, target_range.hasNulls());
1080  };
1081  auto res_range = compute_bucket_range(target_value_range_with_qual, target_ti);
1082  // check target_value expression's col range to be not nullable iff it has its filter
1083  // expression i.e., in simple_quals
1084  // todo (yoonmin) : need to search simple_quals to cover more cases?
1085  if (target_value_range.getFpMin() < target_value_range_with_qual.getFpMin() ||
1086  target_value_range.getFpMax() > target_value_range_with_qual.getFpMax()) {
1087  res_range.setNulls(false);
1088  }
1089  return res_range;
1090  } else {
1091  // we cannot determine a possibility of skipping oob check safely
1092  const bool has_nulls = target_value_range.getType() == ExpressionRangeType::Invalid ||
1093  target_value_range.hasNulls();
1094  auto partition_expr_range = getExpressionRange(
1095  width_bucket_expr->get_partition_count(), query_infos, executor, simple_quals);
1096  auto res = ExpressionRange::makeIntRange(0, INT32_MAX, 0, has_nulls);
1097  switch (partition_expr_range.getType()) {
1099  res.setIntMax(partition_expr_range.getIntMax() + 1);
1100  break;
1101  }
1104  res.setIntMax(static_cast<int64_t>(partition_expr_range.getFpMax()) + 1);
1105  break;
1106  }
1107  default:
1108  break;
1109  }
1110  return res;
1111  }
1112 }
const Expr * get_partition_count() const
Definition: Analyzer.h:1201
bool is_constant_expr() const
Definition: Analyzer.h:1246
int32_t compute_bucket(double target_const_val) const
Definition: Analyzer.cpp:3991
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:3946
const Expr * get_target_value() const
Definition: Analyzer.h:1198
#define CHECK(condition)
Definition: Logger.h:291
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 334 of file ExpressionRange.cpp.

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

338  {
341  } else if (auto bin_oper_expr = dynamic_cast<const Analyzer::BinOper*>(expr)) {
342  return getExpressionRange(bin_oper_expr, query_infos, executor, simple_quals);
343  } else if (auto constant_expr = dynamic_cast<const Analyzer::Constant*>(expr)) {
344  return getExpressionRange(constant_expr);
345  } else if (auto column_var_expr = dynamic_cast<const Analyzer::ColumnVar*>(expr)) {
346  return getExpressionRange(column_var_expr, query_infos, executor, simple_quals);
347  } else if (auto string_oper_expr = dynamic_cast<const Analyzer::StringOper*>(expr)) {
348  return getExpressionRange(string_oper_expr, executor);
349  } else if (auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr)) {
350  return getExpressionRange(like_expr);
351  } else if (auto case_expr = dynamic_cast<const Analyzer::CaseExpr*>(expr)) {
352  return getExpressionRange(case_expr, query_infos, executor);
353  } else if (auto u_expr = dynamic_cast<const Analyzer::UOper*>(expr)) {
354  return getExpressionRange(u_expr, query_infos, executor, simple_quals);
355  } else if (auto extract_expr = dynamic_cast<const Analyzer::ExtractExpr*>(expr)) {
356  return getExpressionRange(extract_expr, query_infos, executor, simple_quals);
357  } else if (auto datetrunc_expr = dynamic_cast<const Analyzer::DatetruncExpr*>(expr)) {
358  return getExpressionRange(datetrunc_expr, query_infos, executor, simple_quals);
359  } else if (auto width_expr = dynamic_cast<const Analyzer::WidthBucketExpr*>(expr)) {
360  return getExpressionRange(width_expr, query_infos, executor, simple_quals);
361  }
363 }
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:79
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 521 of file ExpressionRange.cpp.

References CHECK, CHECK_EQ, CHECK_GT, shared::ColumnKey::column_id, dtDAY, extract_max_stat_fp_type(), extract_max_stat_int_type(), extract_min_stat_fp_type(), extract_min_stat_int_type(), FIND_STAT_FRAG, anonymous_namespace{ExpressionRange.cpp}::get_conservative_datetrunc_bucket(), SQLTypeInfo::get_elem_type(), get_logical_type_info(), Analyzer::Expr::get_type_info(), Analyzer::ColumnVar::getColumnKey(), Analyzer::ColumnVar::getTableKey(), 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().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function: