OmniSciDB  a987f07e93
 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: Datum.h:72
#define CHECK(condition)
Definition: Logger.h:289
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
Definition: Datum.h:67

+ 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:202
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:203

+ 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:452
Definition: sqldefs.h:40
int64_t scale_up_interval_endpoint(const int64_t endpoint, const SQLTypeInfo &ti)
SQLOps get_optype() const
Definition: Analyzer.h:448
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:83
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:289
const Expr * get_left_operand() const
Definition: Analyzer.h:451
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:64
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:68
Definition: sqltypes.h:60
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:202
#define CHECK_GE(x, y)
Definition: Logger.h:302
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:299
int get_rte_idx() const
Definition: Analyzer.h:204
int get_column_id() const
Definition: Analyzer.h:203

+ 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  const auto dict_id = expr_ti.get_comp_param();
682  CHECK_NE(dict_id, TRANSIENT_DICT_ID);
683  const auto translation_map = executor->getStringProxyTranslationMap(
684  dict_id,
685  dict_id,
687  string_op_infos,
688  executor->getRowSetMemoryOwner(),
689  true);
690 
691  // Todo(todd): Track null presence in StringDictionaryProxy::IdMap
692  return ExpressionRange::makeIntRange(translation_map->rangeStart(),
693  translation_map->rangeEnd() - 1,
694  0 /* bucket */,
695  true /* assume has nulls */);
696 
697  } else {
698  // Todo(todd): Get min/max range stats
699  // Below works fine, we just can't take advantage of low-cardinality
700  // group by optimizations
702  }
703 }
bool requiresPerRowTranslation() const
Definition: Analyzer.h:1574
#define CHECK_NE(x, y)
Definition: Logger.h:298
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:83
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:1475
#define TRANSIENT_DICT_ID
Definition: sqltypes.h:310
#define CHECK(condition)
Definition: Logger.h:289
static ExpressionRange makeInvalidRange()
std::vector< std::shared_ptr< Analyzer::Expr > > getChainedStringOpExprs() const
Definition: Analyzer.h:1570

+ Here is the call graph for this function:

ExpressionRange getExpressionRange ( const Analyzer::LikeExpr like_expr)

Definition at line 705 of file ExpressionRange.cpp.

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

705  {
706  const auto& ti = like_expr->get_type_info();
707  CHECK(ti.is_boolean());
708  const auto& arg_ti = like_expr->get_arg()->get_type_info();
710  arg_ti.get_notnull() ? 0 : inline_int_null_val(ti), 1, 0, false);
711 }
const Expr * get_arg() const
Definition: Analyzer.h:905
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:83
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:289
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 713 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.

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

+ 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 818 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.

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

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

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

1046  {
1047  auto target_value_expr = width_bucket_expr->get_target_value();
1048  auto target_value_range = getExpressionRange(target_value_expr, query_infos, executor);
1049  auto target_ti = target_value_expr->get_type_info();
1050  if (width_bucket_expr->is_constant_expr() &&
1051  target_value_range.getType() != ExpressionRangeType::Invalid) {
1052  auto const_target_value = dynamic_cast<const Analyzer::Constant*>(target_value_expr);
1053  if (const_target_value) {
1054  if (const_target_value->get_is_null()) {
1055  // null constant, return default width_bucket range
1057  0, width_bucket_expr->get_partition_count_val(), 0, true);
1058  } else {
1059  CHECK(target_value_range.getFpMax() == target_value_range.getFpMin());
1060  auto target_value_bucket =
1061  width_bucket_expr->compute_bucket(target_value_range.getFpMax(), target_ti);
1063  target_value_bucket, target_value_bucket, 0, target_value_range.hasNulls());
1064  }
1065  }
1066  // compute possible bucket range based on lower and upper bound constants
1067  // to elucidate a target bucket range
1068  const auto target_value_range_with_qual =
1069  getExpressionRange(target_value_expr, query_infos, executor, simple_quals);
1070  auto compute_bucket_range = [&width_bucket_expr](const ExpressionRange& target_range,
1071  SQLTypeInfo ti) {
1072  // we casted bucket bound exprs to double
1073  auto lower_bound_bucket =
1074  width_bucket_expr->compute_bucket<double>(target_range.getFpMin(), ti);
1075  auto upper_bound_bucket =
1076  width_bucket_expr->compute_bucket<double>(target_range.getFpMax(), ti);
1078  lower_bound_bucket, upper_bound_bucket, 0, target_range.hasNulls());
1079  };
1080  auto res_range = compute_bucket_range(target_value_range_with_qual, target_ti);
1081  // check target_value expression's col range to be not nullable iff it has its filter
1082  // expression i.e., in simple_quals
1083  // todo (yoonmin) : need to search simple_quals to cover more cases?
1084  if (target_value_range.getFpMin() < target_value_range_with_qual.getFpMin() ||
1085  target_value_range.getFpMax() > target_value_range_with_qual.getFpMax()) {
1086  res_range.setNulls(false);
1087  }
1088  return res_range;
1089  } else {
1090  // we cannot determine a possibility of skipping oob check safely
1091  const bool has_nulls = target_value_range.getType() == ExpressionRangeType::Invalid ||
1092  target_value_range.hasNulls();
1093  auto partition_expr_range = getExpressionRange(
1094  width_bucket_expr->get_partition_count(), query_infos, executor, simple_quals);
1095  auto res = ExpressionRange::makeIntRange(0, INT32_MAX, 0, has_nulls);
1096  switch (partition_expr_range.getType()) {
1098  res.setIntMax(partition_expr_range.getIntMax() + 1);
1099  break;
1100  }
1103  res.setIntMax(static_cast<int64_t>(partition_expr_range.getFpMax()) + 1);
1104  break;
1105  }
1106  default:
1107  break;
1108  }
1109  return res;
1110  }
1111 }
const Expr * get_partition_count() const
Definition: Analyzer.h:1045
bool is_constant_expr() const
Definition: Analyzer.h:1120
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:3747
const Expr * get_target_value() const
Definition: Analyzer.h:1042
#define CHECK(condition)
Definition: Logger.h:289
int32_t compute_bucket(T target_const_val, SQLTypeInfo &ti) const
Definition: Analyzer.h:1083
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:83
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:202
#define CHECK_EQ(x, y)
Definition: Logger.h:297
#define FIND_STAT_FRAG(stat_name)
Definition: sqltypes.h:64
double extract_max_stat_fp_type(const ChunkStats &stats, const SQLTypeInfo &ti)
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:1206
#define CHECK_GT(x, y)
Definition: Logger.h:301
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:83
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
Definition: sqltypes.h:67
Definition: sqltypes.h:68
Definition: sqltypes.h:56
bool g_enable_watchdog false
Definition: Execute.cpp:79
#define CHECK(condition)
Definition: Logger.h:289
Definition: sqltypes.h:60
int get_column_id() const
Definition: Analyzer.h:203
static ExpressionRange makeInvalidRange()
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:957
bool is_array() const
Definition: sqltypes.h:584

+ Here is the call graph for this function:

+ Here is the caller graph for this function: