OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExpressionRange.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ExpressionRange.h"
18 #include <algorithm>
19 #include <cfenv>
20 #include <cmath>
21 #include "DateTimeTranslator.h"
22 #include "DateTimeUtils.h"
23 #include "DateTruncate.h"
25 #include "Execute.h"
26 #include "ExtractFromTime.h"
27 #include "GroupByAndAggregate.h"
29 
30 #define DEF_OPERATOR(fname, op) \
31  ExpressionRange fname(const ExpressionRange& other) const { \
32  if (type_ == ExpressionRangeType::Invalid || \
33  other.type_ == ExpressionRangeType::Invalid) { \
34  return ExpressionRange::makeInvalidRange(); \
35  } \
36  CHECK(type_ == other.type_); \
37  switch (type_) { \
38  case ExpressionRangeType::Integer: \
39  return binOp<int64_t>(other, [](const int64_t x, const int64_t y) { \
40  return int64_t(checked_int64_t(x) op y); \
41  }); \
42  case ExpressionRangeType::Float: \
43  return binOp<float>(other, [](const float x, const float y) { \
44  std::feclearexcept(FE_OVERFLOW); \
45  std::feclearexcept(FE_UNDERFLOW); \
46  auto result = x op y; \
47  if (std::fetestexcept(FE_OVERFLOW) || std::fetestexcept(FE_UNDERFLOW)) { \
48  throw std::runtime_error("overflow / underflow"); \
49  } \
50  return result; \
51  }); \
52  case ExpressionRangeType::Double: \
53  return binOp<double>(other, [](const double x, const double y) { \
54  std::feclearexcept(FE_OVERFLOW); \
55  std::feclearexcept(FE_UNDERFLOW); \
56  auto result = x op y; \
57  if (std::fetestexcept(FE_OVERFLOW) || std::fetestexcept(FE_UNDERFLOW)) { \
58  throw std::runtime_error("overflow / underflow"); \
59  } \
60  return result; \
61  }); \
62  default: \
63  CHECK(false); \
64  } \
65  CHECK(false); \
66  return ExpressionRange::makeInvalidRange(); \
67  }
68 
69 DEF_OPERATOR(ExpressionRange::operator+, +)
70 DEF_OPERATOR(ExpressionRange::operator-, -)
71 DEF_OPERATOR(ExpressionRange::operator*, *)
72 
73 void apply_fp_qual(const Datum const_datum,
74  const SQLTypes const_type,
75  const SQLOps sql_op,
76  ExpressionRange& qual_range) {
77  double const_val = get_value_from_datum<double>(const_datum, const_type);
78  switch (sql_op) {
79  case kGT:
80  case kGE:
81  qual_range.setFpMin(std::max(qual_range.getFpMin(), const_val));
82  break;
83  case kLT:
84  case kLE:
85  qual_range.setFpMax(std::min(qual_range.getFpMax(), const_val));
86  break;
87  case kEQ:
88  qual_range.setFpMin(std::max(qual_range.getFpMin(), const_val));
89  qual_range.setFpMax(std::min(qual_range.getFpMax(), const_val));
90  break;
91  default: // there may be other operators, but don't do anything with them
92  break;
93  }
94 }
95 
96 void apply_int_qual(const Datum const_datum,
97  const SQLTypes const_type,
98  const SQLOps sql_op,
99  ExpressionRange& qual_range) {
100  int64_t const_val = get_value_from_datum<int64_t>(const_datum, const_type);
101  switch (sql_op) {
102  case kGT:
103  qual_range.setIntMin(std::max(qual_range.getIntMin(), const_val + 1));
104  break;
105  case kGE:
106  qual_range.setIntMin(std::max(qual_range.getIntMin(), const_val));
107  break;
108  case kLT:
109  qual_range.setIntMax(std::min(qual_range.getIntMax(), const_val - 1));
110  break;
111  case kLE:
112  qual_range.setIntMax(std::min(qual_range.getIntMax(), const_val));
113  break;
114  case kEQ:
115  qual_range.setIntMin(std::max(qual_range.getIntMin(), const_val));
116  qual_range.setIntMax(std::min(qual_range.getIntMax(), const_val));
117  break;
118  default: // there may be other operators, but don't do anything with them
119  break;
120  }
121 }
122 
123 void apply_hpt_qual(const Datum const_datum,
124  const SQLTypes const_type,
125  const int32_t const_dimen,
126  const int32_t col_dimen,
127  const SQLOps sql_op,
128  ExpressionRange& qual_range) {
129  CHECK(const_dimen != col_dimen);
130  Datum datum{0};
131  if (const_dimen > col_dimen) {
132  datum.bigintval =
133  get_value_from_datum<int64_t>(const_datum, const_type) /
134  DateTimeUtils::get_timestamp_precision_scale(const_dimen - col_dimen);
135  } else {
136  datum.bigintval =
137  get_value_from_datum<int64_t>(const_datum, const_type) *
138  DateTimeUtils::get_timestamp_precision_scale(col_dimen - const_dimen);
139  }
140  apply_int_qual(datum, const_type, sql_op, qual_range);
141 }
142 
144  const Analyzer::ColumnVar* col_expr,
145  const ExpressionRange& col_range,
146  const boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
147  if (!simple_quals) {
148  return col_range;
149  }
150  ExpressionRange qual_range(col_range);
151  for (auto const& itr : simple_quals.get()) {
152  auto qual_bin_oper = dynamic_cast<Analyzer::BinOper*>(itr.get());
153  if (!qual_bin_oper) {
154  continue;
155  }
156  const Analyzer::Expr* left_operand = qual_bin_oper->get_left_operand();
157  auto qual_col = dynamic_cast<const Analyzer::ColumnVar*>(left_operand);
158  if (!qual_col) {
159  // Check for possibility that column is wrapped in a cast
160  // Presumes that only simple casts (i.e. timestamp to timestamp or int to int) have
161  // been passed through by BinOper::normalize_simple_predicate
162  auto u_expr = dynamic_cast<const Analyzer::UOper*>(left_operand);
163  if (!u_expr) {
164  continue;
165  }
166  qual_col = dynamic_cast<const Analyzer::ColumnVar*>(u_expr->get_operand());
167  if (!qual_col) {
168  continue;
169  }
170  }
171  if (qual_col->get_table_id() != col_expr->get_table_id() ||
172  qual_col->get_column_id() != col_expr->get_column_id()) {
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 }
205 
210  }
211  if (other.int_min_ * other.int_max_ <= 0) {
212  // if the other interval contains 0, the rule is more complicated;
213  // punt for now, we can revisit by splitting the other interval and
214  // taking the convex hull of the resulting two intervals
216  }
217  auto div_range = binOp<int64_t>(other, [](const int64_t x, const int64_t y) {
218  return int64_t(checked_int64_t(x) / y);
219  });
220  if (g_null_div_by_zero) {
221  div_range.setHasNulls();
222  }
223  return div_range;
224 }
225 
227  if (type_ != other.type_) {
229  }
231  switch (type_) {
236  result.has_nulls_ = has_nulls_ || other.has_nulls_;
237  result.int_min_ = std::min(int_min_, other.int_min_);
238  result.int_max_ = std::max(int_max_, other.int_max_);
239  result.bucket_ = std::min(bucket_, other.bucket_);
240  break;
241  }
244  result.type_ = type_;
245  result.has_nulls_ = has_nulls_ || other.has_nulls_;
246  result.fp_min_ = std::min(fp_min_, other.fp_min_);
247  result.fp_max_ = std::max(fp_max_, other.fp_max_);
248  break;
249  }
250  default:
251  CHECK(false);
252  }
253  return result;
254 }
255 
257  if (type_ != other.type_) {
258  return false;
259  }
260  switch (type_) {
262  return true;
264  return has_nulls_ == other.has_nulls_ && int_min_ == other.int_min_ &&
265  int_max_ == other.int_max_;
266  }
269  return has_nulls_ == other.has_nulls_ && fp_min_ == other.fp_min_ &&
270  fp_max_ == other.fp_max_;
271  }
272  default:
273  CHECK(false);
274  }
275  return false;
276 }
277 
279  if (ti.is_array()) {
280  return typeSupportsRange(ti.get_elem_type());
281  } else {
282  return (ti.is_number() || ti.is_boolean() || ti.is_time() ||
283  (ti.is_string() && ti.get_compression() == kENCODING_DICT));
284  }
285 }
286 
288  const Analyzer::BinOper* expr,
289  const std::vector<InputTableInfo>& query_infos,
290  const Executor*,
291  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
292 
294 
296  const Analyzer::ColumnVar* col_expr,
297  const std::vector<InputTableInfo>& query_infos,
298  const Executor* executor,
299  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
300 
302 
304  const std::vector<InputTableInfo>& query_infos,
305  const Executor*);
306 
308  const Analyzer::UOper* u_expr,
309  const std::vector<InputTableInfo>& query_infos,
310  const Executor*,
311  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
312 
314  const Analyzer::ExtractExpr* extract_expr,
315  const std::vector<InputTableInfo>& query_infos,
316  const Executor*,
317  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
318 
320  const Analyzer::DatetruncExpr* datetrunc_expr,
321  const std::vector<InputTableInfo>& query_infos,
322  const Executor* executor,
323  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
324 
326  const Analyzer::Expr* expr,
327  const std::vector<InputTableInfo>& query_infos,
328  const Executor* executor,
329  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
330  const auto& expr_ti = expr->get_type_info();
331  if (!ExpressionRange::typeSupportsRange(expr_ti)) {
333  }
334  auto bin_oper_expr = dynamic_cast<const Analyzer::BinOper*>(expr);
335  if (bin_oper_expr) {
336  return getExpressionRange(bin_oper_expr, query_infos, executor, simple_quals);
337  }
338  auto constant_expr = dynamic_cast<const Analyzer::Constant*>(expr);
339  if (constant_expr) {
340  return getExpressionRange(constant_expr);
341  }
342  auto column_var_expr = dynamic_cast<const Analyzer::ColumnVar*>(expr);
343  if (column_var_expr) {
344  return getExpressionRange(column_var_expr, query_infos, executor, simple_quals);
345  }
346  auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr);
347  if (like_expr) {
348  return getExpressionRange(like_expr);
349  }
350  auto case_expr = dynamic_cast<const Analyzer::CaseExpr*>(expr);
351  if (case_expr) {
352  return getExpressionRange(case_expr, query_infos, executor);
353  }
354  auto u_expr = dynamic_cast<const Analyzer::UOper*>(expr);
355  if (u_expr) {
356  return getExpressionRange(u_expr, query_infos, executor, simple_quals);
357  }
358  auto extract_expr = dynamic_cast<const Analyzer::ExtractExpr*>(expr);
359  if (extract_expr) {
360  return getExpressionRange(extract_expr, query_infos, executor, simple_quals);
361  }
362  auto datetrunc_expr = dynamic_cast<const Analyzer::DatetruncExpr*>(expr);
363  if (datetrunc_expr) {
364  return getExpressionRange(datetrunc_expr, query_infos, executor, simple_quals);
365  }
367 }
368 
369 namespace {
370 
371 int64_t scale_up_interval_endpoint(const int64_t endpoint, const SQLTypeInfo& ti) {
372  return endpoint * static_cast<int64_t>(exp_to_scale(ti.get_scale()));
373 }
374 
375 } // namespace
376 
378  const Analyzer::BinOper* expr,
379  const std::vector<InputTableInfo>& query_infos,
380  const Executor* executor,
381  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
382  const auto& lhs =
383  getExpressionRange(expr->get_left_operand(), query_infos, executor, simple_quals);
384  const auto& rhs =
385  getExpressionRange(expr->get_right_operand(), query_infos, executor, simple_quals);
386  switch (expr->get_optype()) {
387  case kPLUS:
388  return lhs + rhs;
389  case kMINUS:
390  return lhs - rhs;
391  case kMULTIPLY:
392  return lhs * rhs;
393  case kDIVIDE: {
394  const auto& lhs_type = expr->get_left_operand()->get_type_info();
395  if (lhs_type.is_decimal() && lhs.getType() != ExpressionRangeType::Invalid) {
396  CHECK(lhs.getType() == ExpressionRangeType::Integer);
397  const auto adjusted_lhs = ExpressionRange::makeIntRange(
398  scale_up_interval_endpoint(lhs.getIntMin(), lhs_type),
399  scale_up_interval_endpoint(lhs.getIntMax(), lhs_type),
400  0,
401  lhs.hasNulls());
402  return adjusted_lhs / rhs;
403  }
404  return lhs / rhs;
405  }
406  default:
407  break;
408  }
410 }
411 
413  if (constant_expr->get_is_null()) {
415  }
416  const auto constant_type = constant_expr->get_type_info().get_type();
417  const auto datum = constant_expr->get_constval();
418  switch (constant_type) {
419  case kTINYINT: {
420  const int64_t v = datum.tinyintval;
421  return ExpressionRange::makeIntRange(v, v, 0, false);
422  }
423  case kSMALLINT: {
424  const int64_t v = datum.smallintval;
425  return ExpressionRange::makeIntRange(v, v, 0, false);
426  }
427  case kINT: {
428  const int64_t v = datum.intval;
429  return ExpressionRange::makeIntRange(v, v, 0, false);
430  }
431  case kBIGINT:
432  case kNUMERIC:
433  case kDECIMAL: {
434  const int64_t v = datum.bigintval;
435  return ExpressionRange::makeIntRange(v, v, 0, false);
436  }
437  case kTIME:
438  case kTIMESTAMP:
439  case kDATE: {
440  const int64_t v = datum.bigintval;
441  return ExpressionRange::makeIntRange(v, v, 0, false);
442  }
443  case kFLOAT: {
444  return ExpressionRange::makeFloatRange(datum.floatval, datum.floatval, false);
445  }
446  case kDOUBLE: {
447  return ExpressionRange::makeDoubleRange(datum.doubleval, datum.doubleval, false);
448  }
449  default:
450  break;
451  }
453 }
454 
455 #define FIND_STAT_FRAG(stat_name) \
456  const auto stat_name##_frag_index = std::stat_name##_element( \
457  nonempty_fragment_indices.begin(), \
458  nonempty_fragment_indices.end(), \
459  [&fragments, &has_nulls, col_id, col_ti](const size_t lhs_idx, \
460  const size_t rhs_idx) { \
461  const auto& lhs = fragments[lhs_idx]; \
462  const auto& rhs = fragments[rhs_idx]; \
463  auto lhs_meta_it = lhs.getChunkMetadataMap().find(col_id); \
464  if (lhs_meta_it == lhs.getChunkMetadataMap().end()) { \
465  return false; \
466  } \
467  auto rhs_meta_it = rhs.getChunkMetadataMap().find(col_id); \
468  CHECK(rhs_meta_it != rhs.getChunkMetadataMap().end()); \
469  if (lhs_meta_it->second->chunkStats.has_nulls || \
470  rhs_meta_it->second->chunkStats.has_nulls) { \
471  has_nulls = true; \
472  } \
473  if (col_ti.is_fp()) { \
474  return extract_##stat_name##_stat_double(lhs_meta_it->second->chunkStats, \
475  col_ti) < \
476  extract_##stat_name##_stat_double(rhs_meta_it->second->chunkStats, \
477  col_ti); \
478  } \
479  return extract_##stat_name##_stat(lhs_meta_it->second->chunkStats, col_ti) < \
480  extract_##stat_name##_stat(rhs_meta_it->second->chunkStats, col_ti); \
481  }); \
482  if (stat_name##_frag_index == nonempty_fragment_indices.end()) { \
483  return ExpressionRange::makeInvalidRange(); \
484  }
485 
486 namespace {
487 
488 double extract_min_stat_double(const ChunkStats& stats, const SQLTypeInfo& col_ti) {
489  return col_ti.get_type() == kDOUBLE ? stats.min.doubleval : stats.min.floatval;
490 }
491 
492 double extract_max_stat_double(const ChunkStats& stats, const SQLTypeInfo& col_ti) {
493  return col_ti.get_type() == kDOUBLE ? stats.max.doubleval : stats.max.floatval;
494 }
495 
496 int64_t get_conservative_datetrunc_bucket(const DatetruncField datetrunc_field) {
497  const int64_t day_seconds{24 * 3600};
498  const int64_t year_days{365};
499  switch (datetrunc_field) {
500  case dtYEAR:
501  return year_days * day_seconds;
502  case dtQUARTER:
503  return 90 * day_seconds; // 90 is least number of days in any quater
504  case dtMONTH:
505  return 28 * day_seconds;
506  case dtDAY:
507  return day_seconds;
508  case dtHOUR:
509  return 3600;
510  case dtMINUTE:
511  return 60;
512  case dtMILLENNIUM:
513  return 1000 * year_days * day_seconds;
514  case dtCENTURY:
515  return 100 * year_days * day_seconds;
516  case dtDECADE:
517  return 10 * year_days * day_seconds;
518  case dtWEEK:
519  case dtWEEK_SUNDAY:
520  case dtWEEK_SATURDAY:
521  return 7 * day_seconds;
522  case dtQUARTERDAY:
523  return 4 * 60 * 50;
524  default:
525  return 0;
526  }
527 }
528 
529 } // namespace
530 
532  const std::vector<InputTableInfo>& query_infos,
533  const Executor* executor,
534  const bool is_outer_join_proj) {
535  bool has_nulls = is_outer_join_proj;
536  int col_id = col_expr->get_column_id();
537  const auto& col_phys_ti = col_expr->get_type_info().is_array()
538  ? col_expr->get_type_info().get_elem_type()
539  : col_expr->get_type_info();
540  const auto col_ti = get_logical_type_info(col_phys_ti);
541  switch (col_ti.get_type()) {
542  case kTEXT:
543  case kCHAR:
544  case kVARCHAR:
545  CHECK_EQ(kENCODING_DICT, col_ti.get_compression());
546  case kBOOLEAN:
547  case kTINYINT:
548  case kSMALLINT:
549  case kINT:
550  case kBIGINT:
551  case kDECIMAL:
552  case kNUMERIC:
553  case kDATE:
554  case kTIMESTAMP:
555  case kTIME:
556  case kFLOAT:
557  case kDOUBLE: {
558  std::optional<size_t> ti_idx;
559  for (size_t i = 0; i < query_infos.size(); ++i) {
560  if (col_expr->get_table_id() == query_infos[i].table_id) {
561  ti_idx = i;
562  break;
563  }
564  }
565  CHECK(ti_idx);
566  const auto& query_info = query_infos[*ti_idx].info;
567  const auto& fragments = query_info.fragments;
568  const auto cd = executor->getColumnDescriptor(col_expr);
569  if (cd && cd->isVirtualCol) {
570  CHECK(cd->columnName == "rowid");
571  CHECK_EQ(kBIGINT, col_ti.get_type());
572  const int64_t num_tuples = query_info.getNumTuples();
574  0, std::max(num_tuples - 1, int64_t(0)), 0, has_nulls);
575  }
576  if (query_info.getNumTuples() == 0) {
577  // The column doesn't contain any values, synthesize an empty range.
578  if (col_ti.is_fp()) {
579  return col_ti.get_type() == kFLOAT
580  ? ExpressionRange::makeFloatRange(0, -1, false)
581  : ExpressionRange::makeDoubleRange(0, -1, false);
582  }
583  return ExpressionRange::makeIntRange(0, -1, 0, false);
584  }
585  std::vector<size_t> nonempty_fragment_indices;
586  for (size_t i = 0; i < fragments.size(); ++i) {
587  const auto& fragment = fragments[i];
588  if (!fragment.isEmptyPhysicalFragment()) {
589  nonempty_fragment_indices.push_back(i);
590  }
591  }
592  FIND_STAT_FRAG(min);
593  FIND_STAT_FRAG(max);
594  const auto& min_frag = fragments[*min_frag_index];
595  const auto min_it = min_frag.getChunkMetadataMap().find(col_id);
596  if (min_it == min_frag.getChunkMetadataMap().end()) {
598  }
599  const auto& max_frag = fragments[*max_frag_index];
600  const auto max_it = max_frag.getChunkMetadataMap().find(col_id);
601  CHECK(max_it != max_frag.getChunkMetadataMap().end());
602  for (const auto& fragment : fragments) {
603  const auto it = fragment.getChunkMetadataMap().find(col_id);
604  if (it != fragment.getChunkMetadataMap().end()) {
605  if (it->second->chunkStats.has_nulls) {
606  has_nulls = true;
607  break;
608  }
609  }
610  }
611  if (col_ti.is_fp()) {
612  const auto min_val = extract_min_stat_double(min_it->second->chunkStats, col_ti);
613  const auto max_val = extract_max_stat_double(max_it->second->chunkStats, col_ti);
614  return col_ti.get_type() == kFLOAT
615  ? ExpressionRange::makeFloatRange(min_val, max_val, has_nulls)
616  : ExpressionRange::makeDoubleRange(min_val, max_val, has_nulls);
617  }
618  const auto min_val = extract_min_stat(min_it->second->chunkStats, col_ti);
619  const auto max_val = extract_max_stat(max_it->second->chunkStats, col_ti);
620  if (max_val < min_val) {
621  // The column doesn't contain any non-null values, synthesize an empty range.
622  CHECK_GT(min_val, 0);
623  return ExpressionRange::makeIntRange(0, -1, 0, has_nulls);
624  }
625  const int64_t bucket =
626  col_ti.get_type() == kDATE ? get_conservative_datetrunc_bucket(dtDAY) : 0;
627  return ExpressionRange::makeIntRange(min_val, max_val, bucket, has_nulls);
628  }
629  default:
630  break;
631  }
633 }
634 
635 #undef FIND_STAT_FRAG
636 
638  const Analyzer::ColumnVar* col_expr,
639  const std::vector<InputTableInfo>& query_infos,
640  const Executor* executor,
641  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
642  const int rte_idx = col_expr->get_rte_idx();
643  CHECK_GE(rte_idx, 0);
644  CHECK_LT(static_cast<size_t>(rte_idx), query_infos.size());
645  bool is_outer_join_proj = rte_idx > 0 && executor->containsLeftDeepOuterJoin();
646  if (col_expr->get_table_id() > 0) {
647  auto col_range = executor->getColRange(
648  PhysicalInput{col_expr->get_column_id(), col_expr->get_table_id()});
649  if (is_outer_join_proj) {
650  col_range.setHasNulls();
651  }
652  return apply_simple_quals(col_expr, col_range, simple_quals);
653  }
654  return getLeafColumnRange(col_expr, query_infos, executor, is_outer_join_proj);
655 }
656 
658  const auto& ti = like_expr->get_type_info();
659  CHECK(ti.is_boolean());
660  const auto& arg_ti = like_expr->get_arg()->get_type_info();
662  arg_ti.get_notnull() ? 0 : inline_int_null_val(ti), 1, 0, false);
663 }
664 
666  const std::vector<InputTableInfo>& query_infos,
667  const Executor* executor) {
668  const auto& expr_pair_list = case_expr->get_expr_pair_list();
669  auto expr_range = ExpressionRange::makeInvalidRange();
670  bool has_nulls = false;
671  for (const auto& expr_pair : expr_pair_list) {
672  CHECK_EQ(expr_pair.first->get_type_info().get_type(), kBOOLEAN);
673  const auto crt_range =
674  getExpressionRange(expr_pair.second.get(), query_infos, executor);
675  if (crt_range.getType() == ExpressionRangeType::Null) {
676  has_nulls = true;
677  continue;
678  }
679  if (crt_range.getType() == ExpressionRangeType::Invalid) {
681  }
682  expr_range = (expr_range.getType() != ExpressionRangeType::Invalid)
683  ? expr_range || crt_range
684  : crt_range;
685  }
686  if (has_nulls && !(expr_range.getType() == ExpressionRangeType::Invalid)) {
687  expr_range.setHasNulls();
688  }
689  const auto else_expr = case_expr->get_else_expr();
690  CHECK(else_expr);
691  const auto else_null_expr = dynamic_cast<const Analyzer::Constant*>(else_expr);
692  if (else_null_expr && else_null_expr->get_is_null()) {
693  expr_range.setHasNulls();
694  return expr_range;
695  }
696  return expr_range || getExpressionRange(else_expr, query_infos, executor);
697 }
698 
699 namespace {
700 
702  const int64_t scale,
703  const SQLTypeInfo& target_ti) {
704  CHECK(target_ti.is_fp());
705  if (target_ti.get_type() == kFLOAT) {
707  static_cast<float>(arg_range.getIntMin()) / scale,
708  static_cast<float>(arg_range.getIntMax()) / scale,
709  arg_range.hasNulls());
710  }
712  static_cast<double>(arg_range.getIntMin()) / scale,
713  static_cast<double>(arg_range.getIntMax()) / scale,
714  arg_range.hasNulls());
715 }
716 
718  const SQLTypeInfo& oper_ti,
719  const SQLTypeInfo& target_ti) {
720  if (oper_ti.is_timestamp() && target_ti.is_date()) {
721  const auto field = dtDAY;
722  const int64_t scale =
725  : 1;
726  const int64_t min_ts = oper_ti.is_high_precision_timestamp()
727  ? DateTruncate(field, arg_range.getIntMin() / scale)
728  : DateTruncate(field, arg_range.getIntMin());
729  const int64_t max_ts = oper_ti.is_high_precision_timestamp()
730  ? DateTruncate(field, arg_range.getIntMax() / scale)
731  : DateTruncate(field, arg_range.getIntMax());
732  const int64_t bucket = get_conservative_datetrunc_bucket(field);
733 
734  return ExpressionRange::makeIntRange(min_ts, max_ts, bucket, arg_range.hasNulls());
735  }
736 
737  const int32_t ti_dimen = target_ti.get_dimension();
738  const int32_t oper_dimen = oper_ti.get_dimension();
739  CHECK(oper_dimen != ti_dimen);
740  const int64_t min_ts =
741  ti_dimen > oper_dimen
743  arg_range.getIntMin(),
744  abs(oper_dimen - ti_dimen))
747  arg_range.getIntMin(),
748  abs(oper_dimen - ti_dimen));
749  const int64_t max_ts =
750  ti_dimen > oper_dimen
752  arg_range.getIntMax(),
753  abs(oper_dimen - ti_dimen))
756  arg_range.getIntMax(),
757  abs(oper_dimen - ti_dimen));
758 
759  return ExpressionRange::makeIntRange(min_ts, max_ts, 0, arg_range.hasNulls());
760 }
761 
762 } // namespace
763 
765  const Analyzer::UOper* u_expr,
766  const std::vector<InputTableInfo>& query_infos,
767  const Executor* executor,
768  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
769  if (u_expr->get_optype() == kUNNEST) {
770  return getExpressionRange(u_expr->get_operand(), query_infos, executor, simple_quals);
771  }
772  if (u_expr->get_optype() != kCAST) {
774  }
775  const auto& ti = u_expr->get_type_info();
776  if (ti.is_string() && ti.get_compression() == kENCODING_DICT) {
777  const auto sdp = executor->getStringDictionaryProxy(
778  ti.get_comp_param(), executor->getRowSetMemoryOwner(), true);
779  CHECK(sdp);
780  const auto const_operand =
781  dynamic_cast<const Analyzer::Constant*>(u_expr->get_operand());
782  CHECK(const_operand);
783 
784  if (const_operand->get_is_null()) {
786  }
787  CHECK(const_operand->get_constval().stringval);
788  const int64_t v = sdp->getIdOfString(*const_operand->get_constval().stringval);
789  return ExpressionRange::makeIntRange(v, v, 0, false);
790  }
791  const auto arg_range =
792  getExpressionRange(u_expr->get_operand(), query_infos, executor, simple_quals);
793  const auto& arg_ti = u_expr->get_operand()->get_type_info();
794  // Timestamp to Date OR Date/Timestamp casts with different precision
795  if ((ti.is_timestamp() && (arg_ti.get_dimension() != ti.get_dimension())) ||
796  ((arg_ti.is_timestamp() && ti.is_date()))) {
797  return getDateTimePrecisionCastRange(arg_range, arg_ti, ti);
798  }
799  switch (arg_range.getType()) {
802  if (ti.is_fp()) {
803  return ti.get_type() == kDOUBLE
805  arg_range.getFpMin(), arg_range.getFpMax(), arg_range.hasNulls())
806  : ExpressionRange::makeFloatRange(arg_range.getFpMin(),
807  arg_range.getFpMax(),
808  arg_range.hasNulls());
809  }
810  if (ti.is_integer()) {
812  arg_range.getFpMin(), arg_range.getFpMax(), 0, arg_range.hasNulls());
813  }
814  break;
815  }
817  if (ti.is_decimal()) {
818  CHECK_EQ(int64_t(0), arg_range.getBucket());
819  const int64_t scale = exp_to_scale(ti.get_scale() - arg_ti.get_scale());
820  return ExpressionRange::makeIntRange(arg_range.getIntMin() * scale,
821  arg_range.getIntMax() * scale,
822  0,
823  arg_range.hasNulls());
824  }
825  if (arg_ti.is_decimal()) {
826  CHECK_EQ(int64_t(0), arg_range.getBucket());
827  const int64_t scale = exp_to_scale(arg_ti.get_scale());
828  const int64_t scale_half = scale / 2;
829  if (ti.is_fp()) {
830  return fpRangeFromDecimal(arg_range, scale, ti);
831  }
832  return ExpressionRange::makeIntRange((arg_range.getIntMin() - scale_half) / scale,
833  (arg_range.getIntMax() + scale_half) / scale,
834  0,
835  arg_range.hasNulls());
836  }
837  if (ti.is_integer() || ti.is_time()) {
838  return arg_range;
839  }
840  if (ti.get_type() == kFLOAT) {
842  arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
843  }
844  if (ti.get_type() == kDOUBLE) {
846  arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
847  }
848  break;
849  }
851  break;
852  default:
853  CHECK(false);
854  }
856 }
857 
859  const Analyzer::ExtractExpr* extract_expr,
860  const std::vector<InputTableInfo>& query_infos,
861  const Executor* executor,
862  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
863  const int32_t extract_field{extract_expr->get_field()};
864  const auto arg_range = getExpressionRange(
865  extract_expr->get_from_expr(), query_infos, executor, simple_quals);
866  const bool has_nulls =
867  arg_range.getType() == ExpressionRangeType::Invalid || arg_range.hasNulls();
868  const auto& extract_expr_ti = extract_expr->get_from_expr()->get_type_info();
869  switch (extract_field) {
870  case kYEAR: {
871  if (arg_range.getType() == ExpressionRangeType::Invalid) {
873  }
874  CHECK(arg_range.getType() == ExpressionRangeType::Integer);
875  const int64_t year_range_min =
876  extract_expr_ti.is_high_precision_timestamp()
877  ? ExtractFromTime(
878  kYEAR,
879  arg_range.getIntMin() /
880  get_timestamp_precision_scale(extract_expr_ti.get_dimension()))
881  : ExtractFromTime(kYEAR, arg_range.getIntMin());
882  const int64_t year_range_max =
883  extract_expr_ti.is_high_precision_timestamp()
884  ? ExtractFromTime(
885  kYEAR,
886  arg_range.getIntMax() /
887  get_timestamp_precision_scale(extract_expr_ti.get_dimension()))
888  : ExtractFromTime(kYEAR, arg_range.getIntMax());
890  year_range_min, year_range_max, 0, arg_range.hasNulls());
891  }
892  case kEPOCH:
893  case kDATEEPOCH:
894  return arg_range;
895  case kQUARTERDAY:
896  case kQUARTER:
897  return ExpressionRange::makeIntRange(1, 4, 0, has_nulls);
898  case kMONTH:
899  return ExpressionRange::makeIntRange(1, 12, 0, has_nulls);
900  case kDAY:
901  return ExpressionRange::makeIntRange(1, 31, 0, has_nulls);
902  case kHOUR:
903  return ExpressionRange::makeIntRange(0, 23, 0, has_nulls);
904  case kMINUTE:
905  return ExpressionRange::makeIntRange(0, 59, 0, has_nulls);
906  case kSECOND:
907  return ExpressionRange::makeIntRange(0, 60, 0, has_nulls);
908  case kMILLISECOND:
909  return ExpressionRange::makeIntRange(0, 999, 0, has_nulls);
910  case kMICROSECOND:
911  return ExpressionRange::makeIntRange(0, 999999, 0, has_nulls);
912  case kNANOSECOND:
913  return ExpressionRange::makeIntRange(0, 999999999, 0, has_nulls);
914  case kDOW:
915  return ExpressionRange::makeIntRange(0, 6, 0, has_nulls);
916  case kISODOW:
917  return ExpressionRange::makeIntRange(1, 7, 0, has_nulls);
918  case kDOY:
919  return ExpressionRange::makeIntRange(1, 366, 0, has_nulls);
920  case kWEEK:
921  case kWEEK_SUNDAY:
922  case kWEEK_SATURDAY:
923  return ExpressionRange::makeIntRange(1, 53, 0, has_nulls);
924  default:
925  CHECK(false);
926  }
928 }
929 
931  const Analyzer::DatetruncExpr* datetrunc_expr,
932  const std::vector<InputTableInfo>& query_infos,
933  const Executor* executor,
934  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
935  const auto arg_range = getExpressionRange(
936  datetrunc_expr->get_from_expr(), query_infos, executor, simple_quals);
937  if (arg_range.getType() == ExpressionRangeType::Invalid) {
939  }
940  const auto& datetrunc_expr_ti = datetrunc_expr->get_from_expr()->get_type_info();
941  const int64_t min_ts = DateTimeTranslator::getDateTruncConstantValue(
942  arg_range.getIntMin(), datetrunc_expr->get_field(), datetrunc_expr_ti);
943  const int64_t max_ts = DateTimeTranslator::getDateTruncConstantValue(
944  arg_range.getIntMax(), datetrunc_expr->get_field(), datetrunc_expr_ti);
945  const int64_t bucket =
946  datetrunc_expr_ti.is_high_precision_timestamp()
947  ? get_conservative_datetrunc_bucket(datetrunc_expr->get_field()) *
949  datetrunc_expr_ti.get_dimension())
950  : get_conservative_datetrunc_bucket(datetrunc_expr->get_field());
951 
952  return ExpressionRange::makeIntRange(min_ts, max_ts, bucket, arg_range.hasNulls());
953 }
int get_table_id() const
Definition: Analyzer.h:194
int64_t getIntMin() const
#define CHECK_EQ(x, y)
Definition: Logger.h:205
#define FIND_STAT_FRAG(stat_name)
const Expr * get_from_expr() const
Definition: Analyzer.h:1197
const Expr * get_else_expr() const
Definition: Analyzer.h:1152
static ExpressionRange makeNullRange()
Definition: sqltypes.h:51
int64_t DateTruncate(DatetruncField field, const int64_t timeval)
SQLTypes
Definition: sqltypes.h:40
bool is_timestamp() const
Definition: sqltypes.h:733
bool operator==(const ExpressionRange &other) const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64, 64, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void >> checked_int64_t
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)
double extract_min_stat_double(const ChunkStats &stats, const SQLTypeInfo &col_ti)
ExtractField get_field() const
Definition: Analyzer.h:1196
bool is_fp() const
Definition: sqltypes.h:491
HOST DEVICE int get_scale() const
Definition: sqltypes.h:335
const Expr * get_right_operand() const
Definition: Analyzer.h:443
SQLOps
Definition: sqldefs.h:29
Definition: sqldefs.h:35
Definition: sqldefs.h:36
#define DEF_OPERATOR(fname, op)
bool get_is_null() const
Definition: Analyzer.h:334
#define CHECK_GE(x, y)
Definition: Logger.h:210
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:899
Definition: sqldefs.h:49
Definition: sqldefs.h:30
DatetruncField get_field() const
Definition: Analyzer.h:1321
Definition: sqldefs.h:41
int64_t scale_up_interval_endpoint(const int64_t endpoint, const SQLTypeInfo &ti)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:330
void setIntMin(const int64_t int_min)
bool is_number() const
Definition: sqltypes.h:492
#define CHECK_GT(x, y)
Definition: Logger.h:209
constexpr int64_t get_datetime_scaled_epoch(const ScalingType direction, const int64_t epoch, const int32_t dimen)
const Expr * get_arg() const
Definition: Analyzer.h:905
bool is_time() const
Definition: sqltypes.h:493
static ExpressionRange makeFloatRange(const float fp_min, const float fp_max, const bool has_nulls)
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)
bool g_null_div_by_zero
Definition: Execute.cpp:79
SQLOps get_optype() const
Definition: Analyzer.h:439
float floatval
Definition: sqltypes.h:210
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
ExpressionRangeType type_
ExpressionRange operator||(const ExpressionRange &other) const
DatetruncField
Definition: DateTruncate.h:27
int64_t get_conservative_datetrunc_bucket(const DatetruncField datetrunc_field)
void apply_fp_qual(const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
int64_t bigintval
Definition: sqltypes.h:209
ExpressionRange getLeafColumnRange(const Analyzer::ColumnVar *col_expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, const bool is_outer_join_proj)
bool is_boolean() const
Definition: sqltypes.h:494
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
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
Definition: sqldefs.h:34
static ExpressionRange makeDoubleRange(const double fp_min, const double fp_max, const bool has_nulls)
#define CHECK_LT(x, y)
Definition: Logger.h:207
Definition: sqltypes.h:54
Definition: sqltypes.h:55
Definition: sqldefs.h:40
const Expr * get_from_expr() const
Definition: Analyzer.h:1322
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:338
int get_rte_idx() const
Definition: Analyzer.h:196
const Expr * get_operand() const
Definition: Analyzer.h:371
Datum get_constval() const
Definition: Analyzer.h:335
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:332
ExpressionRange operator/(const ExpressionRange &other) const
DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval)
Definition: sqltypes.h:43
void setIntMax(const int64_t int_max)
int64_t extract_min_stat(const ChunkStats &stats, const SQLTypeInfo &ti)
ExpressionRangeType getType() const
int64_t getIntMax() const
#define CHECK(condition)
Definition: Logger.h:197
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
bool is_high_precision_timestamp() const
Definition: sqltypes.h:723
uint64_t exp_to_scale(const unsigned exp)
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
Definition: sqldefs.h:33
const Expr * get_left_operand() const
Definition: Analyzer.h:442
static bool typeSupportsRange(const SQLTypeInfo &ti)
Definition: sqltypes.h:47
int get_column_id() const
Definition: Analyzer.h:195
static ExpressionRange makeInvalidRange()
bool is_string() const
Definition: sqltypes.h:487
static int64_t getDateTruncConstantValue(const int64_t &timeval, const DatetruncField &field, const SQLTypeInfo &ti)
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:703
ExpressionRange getDateTimePrecisionCastRange(const ExpressionRange &arg_range, const SQLTypeInfo &oper_ti, const SQLTypeInfo &target_ti)
SQLOps get_optype() const
Definition: Analyzer.h:370
bool is_date() const
Definition: sqltypes.h:721
bool is_array() const
Definition: sqltypes.h:495
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1149
double extract_max_stat_double(const ChunkStats &stats, const SQLTypeInfo &col_ti)
int64_t extract_max_stat(const ChunkStats &stats, const SQLTypeInfo &ti)
double doubleval
Definition: sqltypes.h:211
ExpressionRange fpRangeFromDecimal(const ExpressionRange &arg_range, const int64_t scale, const SQLTypeInfo &target_ti)