OmniSciDB  5ade3759e0
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  if (qual_range.getType() == ExpressionRangeType::Integer) {
204  if (qual_range.getIntMin() > qual_range.getIntMax()) {
205  return ExpressionRange::makeIntRange(0, -1, 0, qual_range.hasNulls());
206  }
207  }
208  return qual_range;
209 }
210 
215  }
216  if (other.int_min_ * other.int_max_ <= 0) {
217  // if the other interval contains 0, the rule is more complicated;
218  // punt for now, we can revisit by splitting the other interval and
219  // taking the convex hull of the resulting two intervals
221  }
222  auto div_range = binOp<int64_t>(other, [](const int64_t x, const int64_t y) {
223  return int64_t(checked_int64_t(x) / y);
224  });
225  if (g_null_div_by_zero) {
226  div_range.setHasNulls();
227  }
228  return div_range;
229 }
230 
232  if (type_ != other.type_) {
234  }
236  switch (type_) {
241  result.has_nulls_ = has_nulls_ || other.has_nulls_;
242  result.int_min_ = std::min(int_min_, other.int_min_);
243  result.int_max_ = std::max(int_max_, other.int_max_);
244  result.bucket_ = std::min(bucket_, other.bucket_);
245  break;
246  }
249  result.type_ = type_;
250  result.has_nulls_ = has_nulls_ || other.has_nulls_;
251  result.fp_min_ = std::min(fp_min_, other.fp_min_);
252  result.fp_max_ = std::max(fp_max_, other.fp_max_);
253  break;
254  }
255  default:
256  CHECK(false);
257  }
258  return result;
259 }
260 
262  if (type_ != other.type_) {
263  return false;
264  }
265  switch (type_) {
267  return true;
269  return has_nulls_ == other.has_nulls_ && int_min_ == other.int_min_ &&
270  int_max_ == other.int_max_;
271  }
274  return has_nulls_ == other.has_nulls_ && fp_min_ == other.fp_min_ &&
275  fp_max_ == other.fp_max_;
276  }
277  default:
278  CHECK(false);
279  }
280  return false;
281 }
282 
284  const Analyzer::BinOper* expr,
285  const std::vector<InputTableInfo>& query_infos,
286  const Executor*,
287  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
288 
290 
292  const Analyzer::ColumnVar* col_expr,
293  const std::vector<InputTableInfo>& query_infos,
294  const Executor* executor,
295  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
296 
298 
300  const std::vector<InputTableInfo>& query_infos,
301  const Executor*);
302 
304  const Analyzer::UOper* u_expr,
305  const std::vector<InputTableInfo>& query_infos,
306  const Executor*,
307  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
308 
310  const Analyzer::ExtractExpr* extract_expr,
311  const std::vector<InputTableInfo>& query_infos,
312  const Executor*,
313  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
314 
316  const Analyzer::DatetruncExpr* datetrunc_expr,
317  const std::vector<InputTableInfo>& query_infos,
318  const Executor* executor,
319  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals);
320 
322  const Analyzer::Expr* expr,
323  const std::vector<InputTableInfo>& query_infos,
324  const Executor* executor,
325  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
326  auto bin_oper_expr = dynamic_cast<const Analyzer::BinOper*>(expr);
327  if (bin_oper_expr) {
328  return getExpressionRange(bin_oper_expr, query_infos, executor, simple_quals);
329  }
330  auto constant_expr = dynamic_cast<const Analyzer::Constant*>(expr);
331  if (constant_expr) {
332  return getExpressionRange(constant_expr);
333  }
334  auto column_var_expr = dynamic_cast<const Analyzer::ColumnVar*>(expr);
335  if (column_var_expr) {
336  return getExpressionRange(column_var_expr, query_infos, executor, simple_quals);
337  }
338  auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr);
339  if (like_expr) {
340  return getExpressionRange(like_expr);
341  }
342  auto case_expr = dynamic_cast<const Analyzer::CaseExpr*>(expr);
343  if (case_expr) {
344  return getExpressionRange(case_expr, query_infos, executor);
345  }
346  auto u_expr = dynamic_cast<const Analyzer::UOper*>(expr);
347  if (u_expr) {
348  return getExpressionRange(u_expr, query_infos, executor, simple_quals);
349  }
350  auto extract_expr = dynamic_cast<const Analyzer::ExtractExpr*>(expr);
351  if (extract_expr) {
352  return getExpressionRange(extract_expr, query_infos, executor, simple_quals);
353  }
354  auto datetrunc_expr = dynamic_cast<const Analyzer::DatetruncExpr*>(expr);
355  if (datetrunc_expr) {
356  return getExpressionRange(datetrunc_expr, query_infos, executor, simple_quals);
357  }
359 }
360 
361 namespace {
362 
363 int64_t scale_up_interval_endpoint(const int64_t endpoint, const SQLTypeInfo& ti) {
364  return endpoint * static_cast<int64_t>(exp_to_scale(ti.get_scale()));
365 }
366 
367 } // namespace
368 
370  const Analyzer::BinOper* expr,
371  const std::vector<InputTableInfo>& query_infos,
372  const Executor* executor,
373  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
374  const auto& lhs =
375  getExpressionRange(expr->get_left_operand(), query_infos, executor, simple_quals);
376  const auto& rhs =
377  getExpressionRange(expr->get_right_operand(), query_infos, executor, simple_quals);
378  switch (expr->get_optype()) {
379  case kPLUS:
380  return lhs + rhs;
381  case kMINUS:
382  return lhs - rhs;
383  case kMULTIPLY:
384  return lhs * rhs;
385  case kDIVIDE: {
386  const auto& lhs_type = expr->get_left_operand()->get_type_info();
387  if (lhs_type.is_decimal() && lhs.getType() != ExpressionRangeType::Invalid) {
388  CHECK(lhs.getType() == ExpressionRangeType::Integer);
389  const auto adjusted_lhs = ExpressionRange::makeIntRange(
390  scale_up_interval_endpoint(lhs.getIntMin(), lhs_type),
391  scale_up_interval_endpoint(lhs.getIntMax(), lhs_type),
392  0,
393  lhs.hasNulls());
394  return adjusted_lhs / rhs;
395  }
396  return lhs / rhs;
397  }
398  default:
399  break;
400  }
402 }
403 
405  if (constant_expr->get_is_null()) {
407  }
408  const auto constant_type = constant_expr->get_type_info().get_type();
409  const auto datum = constant_expr->get_constval();
410  switch (constant_type) {
411  case kTINYINT: {
412  const int64_t v = datum.tinyintval;
413  return ExpressionRange::makeIntRange(v, v, 0, false);
414  }
415  case kSMALLINT: {
416  const int64_t v = datum.smallintval;
417  return ExpressionRange::makeIntRange(v, v, 0, false);
418  }
419  case kINT: {
420  const int64_t v = datum.intval;
421  return ExpressionRange::makeIntRange(v, v, 0, false);
422  }
423  case kBIGINT:
424  case kNUMERIC:
425  case kDECIMAL: {
426  const int64_t v = datum.bigintval;
427  return ExpressionRange::makeIntRange(v, v, 0, false);
428  }
429  case kTIME:
430  case kTIMESTAMP:
431  case kDATE: {
432  const int64_t v = datum.bigintval;
433  return ExpressionRange::makeIntRange(v, v, 0, false);
434  }
435  case kFLOAT: {
436  return ExpressionRange::makeFloatRange(datum.floatval, datum.floatval, false);
437  }
438  case kDOUBLE: {
439  return ExpressionRange::makeDoubleRange(datum.doubleval, datum.doubleval, false);
440  }
441  default:
442  break;
443  }
445 }
446 
447 #define FIND_STAT_FRAG(stat_name) \
448  const auto stat_name##_frag_index = std::stat_name##_element( \
449  nonempty_fragment_indices.begin(), \
450  nonempty_fragment_indices.end(), \
451  [&fragments, &has_nulls, col_id, col_ti](const size_t lhs_idx, \
452  const size_t rhs_idx) { \
453  const auto& lhs = fragments[lhs_idx]; \
454  const auto& rhs = fragments[rhs_idx]; \
455  auto lhs_meta_it = lhs.getChunkMetadataMap().find(col_id); \
456  if (lhs_meta_it == lhs.getChunkMetadataMap().end()) { \
457  return false; \
458  } \
459  auto rhs_meta_it = rhs.getChunkMetadataMap().find(col_id); \
460  CHECK(rhs_meta_it != rhs.getChunkMetadataMap().end()); \
461  if (lhs_meta_it->second.chunkStats.has_nulls || \
462  rhs_meta_it->second.chunkStats.has_nulls) { \
463  has_nulls = true; \
464  } \
465  if (col_ti.is_fp()) { \
466  return extract_##stat_name##_stat_double(lhs_meta_it->second.chunkStats, \
467  col_ti) < \
468  extract_##stat_name##_stat_double(rhs_meta_it->second.chunkStats, \
469  col_ti); \
470  } \
471  return extract_##stat_name##_stat(lhs_meta_it->second.chunkStats, col_ti) < \
472  extract_##stat_name##_stat(rhs_meta_it->second.chunkStats, col_ti); \
473  }); \
474  if (stat_name##_frag_index == nonempty_fragment_indices.end()) { \
475  return ExpressionRange::makeInvalidRange(); \
476  }
477 
478 namespace {
479 
480 double extract_min_stat_double(const ChunkStats& stats, const SQLTypeInfo& col_ti) {
481  return col_ti.get_type() == kDOUBLE ? stats.min.doubleval : stats.min.floatval;
482 }
483 
484 double extract_max_stat_double(const ChunkStats& stats, const SQLTypeInfo& col_ti) {
485  return col_ti.get_type() == kDOUBLE ? stats.max.doubleval : stats.max.floatval;
486 }
487 
488 int64_t get_conservative_datetrunc_bucket(const DatetruncField datetrunc_field) {
489  const int64_t day_seconds{24 * 3600};
490  const int64_t year_days{365};
491  switch (datetrunc_field) {
492  case dtYEAR:
493  return year_days * day_seconds;
494  case dtQUARTER:
495  return 90 * day_seconds; // 90 is least number of days in any quater
496  case dtMONTH:
497  return 28 * day_seconds;
498  case dtDAY:
499  return day_seconds;
500  case dtHOUR:
501  return 3600;
502  case dtMINUTE:
503  return 60;
504  case dtMILLENNIUM:
505  return 1000 * year_days * day_seconds;
506  case dtCENTURY:
507  return 100 * year_days * day_seconds;
508  case dtDECADE:
509  return 10 * year_days * day_seconds;
510  case dtWEEK:
511  return 7 * day_seconds;
512  case dtQUARTERDAY:
513  return 4 * 60 * 50;
514  default:
515  return 0;
516  }
517 }
518 
519 } // namespace
520 
522  const std::vector<InputTableInfo>& query_infos,
523  const Executor* executor,
524  const bool is_outer_join_proj) {
525  bool has_nulls = is_outer_join_proj;
526  int col_id = col_expr->get_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  ssize_t ti_idx = -1;
549  for (size_t i = 0; i < query_infos.size(); ++i) {
550  if (col_expr->get_table_id() == query_infos[i].table_id) {
551  ti_idx = i;
552  break;
553  }
554  }
555  CHECK_NE(ssize_t(-1), 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  if (col_ti.is_fp()) {
602  const auto min_val = extract_min_stat_double(min_it->second.chunkStats, col_ti);
603  const auto max_val = extract_max_stat_double(max_it->second.chunkStats, col_ti);
604  return col_ti.get_type() == kFLOAT
605  ? ExpressionRange::makeFloatRange(min_val, max_val, has_nulls)
606  : ExpressionRange::makeDoubleRange(min_val, max_val, has_nulls);
607  }
608  const auto min_val = extract_min_stat(min_it->second.chunkStats, col_ti);
609  const auto max_val = extract_max_stat(max_it->second.chunkStats, col_ti);
610  if (max_val < min_val) {
611  // The column doesn't contain any non-null values, synthesize an empty range.
612  CHECK_GT(min_val, 0);
613  return ExpressionRange::makeIntRange(0, -1, 0, has_nulls);
614  }
615  const int64_t bucket =
616  col_ti.get_type() == kDATE ? get_conservative_datetrunc_bucket(dtDAY) : 0;
617  return ExpressionRange::makeIntRange(min_val, max_val, bucket, has_nulls);
618  }
619  default:
620  break;
621  }
623 }
624 
625 #undef FIND_STAT_FRAG
626 
628  const Analyzer::ColumnVar* col_expr,
629  const std::vector<InputTableInfo>& query_infos,
630  const Executor* executor,
631  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
632  const int rte_idx = col_expr->get_rte_idx();
633  CHECK_GE(rte_idx, 0);
634  CHECK_LT(static_cast<size_t>(rte_idx), query_infos.size());
635  bool is_outer_join_proj = rte_idx > 0 && executor->containsLeftDeepOuterJoin();
636  if (col_expr->get_table_id() > 0) {
637  auto col_range = executor->getColRange(
638  PhysicalInput{col_expr->get_column_id(), col_expr->get_table_id()});
639  if (is_outer_join_proj) {
640  col_range.setHasNulls();
641  }
642  return apply_simple_quals(col_expr, col_range, simple_quals);
643  }
644  return getLeafColumnRange(col_expr, query_infos, executor, is_outer_join_proj);
645 }
646 
648  const auto& ti = like_expr->get_type_info();
649  CHECK(ti.is_boolean());
650  const auto& arg_ti = like_expr->get_arg()->get_type_info();
652  arg_ti.get_notnull() ? 0 : inline_int_null_val(ti), 1, 0, false);
653 }
654 
656  const std::vector<InputTableInfo>& query_infos,
657  const Executor* executor) {
658  const auto& expr_pair_list = case_expr->get_expr_pair_list();
659  auto expr_range = ExpressionRange::makeInvalidRange();
660  bool has_nulls = false;
661  for (const auto& expr_pair : expr_pair_list) {
662  CHECK_EQ(expr_pair.first->get_type_info().get_type(), kBOOLEAN);
663  const auto crt_range =
664  getExpressionRange(expr_pair.second.get(), query_infos, executor);
665  if (crt_range.getType() == ExpressionRangeType::Null) {
666  has_nulls = true;
667  continue;
668  }
669  if (crt_range.getType() == ExpressionRangeType::Invalid) {
671  }
672  expr_range = (expr_range.getType() != ExpressionRangeType::Invalid)
673  ? expr_range || crt_range
674  : crt_range;
675  }
676  if (has_nulls && !(expr_range.getType() == ExpressionRangeType::Invalid)) {
677  expr_range.setHasNulls();
678  }
679  const auto else_expr = case_expr->get_else_expr();
680  CHECK(else_expr);
681  const auto else_null_expr = dynamic_cast<const Analyzer::Constant*>(else_expr);
682  if (else_null_expr && else_null_expr->get_is_null()) {
683  expr_range.setHasNulls();
684  return expr_range;
685  }
686  return expr_range || getExpressionRange(else_expr, query_infos, executor);
687 }
688 
689 namespace {
690 
692  const int64_t scale,
693  const SQLTypeInfo& target_ti) {
694  CHECK(target_ti.is_fp());
695  if (target_ti.get_type() == kFLOAT) {
697  static_cast<float>(arg_range.getIntMin()) / scale,
698  static_cast<float>(arg_range.getIntMax()) / scale,
699  arg_range.hasNulls());
700  }
702  static_cast<double>(arg_range.getIntMin()) / scale,
703  static_cast<double>(arg_range.getIntMax()) / scale,
704  arg_range.hasNulls());
705 }
706 
708  const SQLTypeInfo& oper_ti,
709  const SQLTypeInfo& target_ti) {
710  if (oper_ti.is_timestamp() && target_ti.is_date()) {
711  const auto field = dtDAY;
712  const int64_t scale =
713  oper_ti.is_high_precision_timestamp()
715  : 1;
716  const int64_t min_ts = oper_ti.is_high_precision_timestamp()
717  ? DateTruncate(field, arg_range.getIntMin() / scale)
718  : DateTruncate(field, arg_range.getIntMin());
719  const int64_t max_ts = oper_ti.is_high_precision_timestamp()
720  ? DateTruncate(field, arg_range.getIntMax() / scale)
721  : DateTruncate(field, arg_range.getIntMax());
722  const int64_t bucket = get_conservative_datetrunc_bucket(field);
723 
724  return ExpressionRange::makeIntRange(min_ts, max_ts, bucket, arg_range.hasNulls());
725  }
726 
727  const int32_t ti_dimen = target_ti.get_dimension();
728  const int32_t oper_dimen = oper_ti.get_dimension();
729  CHECK(oper_dimen != ti_dimen);
730  const int64_t scale =
731  DateTimeUtils::get_timestamp_precision_scale(abs(oper_dimen - ti_dimen));
732  const int64_t min_ts =
733  ti_dimen > oper_dimen
734  ? DateTruncateAlterPrecisionScaleUp(arg_range.getIntMin(), scale)
735  : DateTruncateAlterPrecisionScaleDown(arg_range.getIntMin(), scale);
736  const int64_t max_ts =
737  ti_dimen > oper_dimen
738  ? DateTruncateAlterPrecisionScaleUp(arg_range.getIntMax(), scale)
739  : DateTruncateAlterPrecisionScaleDown(arg_range.getIntMax(), scale);
740 
741  return ExpressionRange::makeIntRange(min_ts, max_ts, 0, arg_range.hasNulls());
742 }
743 
744 } // namespace
745 
747  const Analyzer::UOper* u_expr,
748  const std::vector<InputTableInfo>& query_infos,
749  const Executor* executor,
750  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
751  if (u_expr->get_optype() == kUNNEST) {
752  return getExpressionRange(u_expr->get_operand(), query_infos, executor, simple_quals);
753  }
754  if (u_expr->get_optype() != kCAST) {
756  }
757  const auto& ti = u_expr->get_type_info();
758  if (ti.is_string() && ti.get_compression() == kENCODING_DICT) {
759  const auto sdp = executor->getStringDictionaryProxy(
760  ti.get_comp_param(), executor->getRowSetMemoryOwner(), true);
761  CHECK(sdp);
762  const auto const_operand =
763  dynamic_cast<const Analyzer::Constant*>(u_expr->get_operand());
764  CHECK(const_operand);
765 
766  if (const_operand->get_is_null()) {
768  }
769  CHECK(const_operand->get_constval().stringval);
770  const int64_t v = sdp->getIdOfString(*const_operand->get_constval().stringval);
771  return ExpressionRange::makeIntRange(v, v, 0, false);
772  }
773  const auto arg_range =
774  getExpressionRange(u_expr->get_operand(), query_infos, executor, simple_quals);
775  const auto& arg_ti = u_expr->get_operand()->get_type_info();
776  // Timestamp to Date OR Date/Timestamp casts with different precision
777  if ((ti.is_timestamp() && (arg_ti.get_dimension() != ti.get_dimension())) ||
778  ((arg_ti.is_timestamp() && ti.is_date()))) {
779  return getDateTimePrecisionCastRange(arg_range, arg_ti, ti);
780  }
781  switch (arg_range.getType()) {
784  if (ti.is_fp()) {
785  return ti.get_type() == kDOUBLE
787  arg_range.getFpMin(), arg_range.getFpMax(), arg_range.hasNulls())
788  : ExpressionRange::makeFloatRange(arg_range.getFpMin(),
789  arg_range.getFpMax(),
790  arg_range.hasNulls());
791  }
792  if (ti.is_integer()) {
794  arg_range.getFpMin(), arg_range.getFpMax(), 0, arg_range.hasNulls());
795  }
796  break;
797  }
799  if (ti.is_decimal()) {
800  CHECK_EQ(int64_t(0), arg_range.getBucket());
801  const int64_t scale = exp_to_scale(ti.get_scale() - arg_ti.get_scale());
802  return ExpressionRange::makeIntRange(arg_range.getIntMin() * scale,
803  arg_range.getIntMax() * scale,
804  0,
805  arg_range.hasNulls());
806  }
807  if (arg_ti.is_decimal()) {
808  CHECK_EQ(int64_t(0), arg_range.getBucket());
809  const int64_t scale = exp_to_scale(arg_ti.get_scale());
810  const int64_t scale_half = scale / 2;
811  if (ti.is_fp()) {
812  return fpRangeFromDecimal(arg_range, scale, ti);
813  }
814  return ExpressionRange::makeIntRange((arg_range.getIntMin() - scale_half) / scale,
815  (arg_range.getIntMax() + scale_half) / scale,
816  0,
817  arg_range.hasNulls());
818  }
819  if (ti.is_integer() || ti.is_time()) {
820  return arg_range;
821  }
822  if (ti.get_type() == kFLOAT) {
824  arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
825  }
826  if (ti.get_type() == kDOUBLE) {
828  arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
829  }
830  break;
831  }
833  break;
834  default:
835  CHECK(false);
836  }
838 }
839 
841  const Analyzer::ExtractExpr* extract_expr,
842  const std::vector<InputTableInfo>& query_infos,
843  const Executor* executor,
844  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
845  const int32_t extract_field{extract_expr->get_field()};
846  const auto arg_range = getExpressionRange(
847  extract_expr->get_from_expr(), query_infos, executor, simple_quals);
848  const bool has_nulls =
849  arg_range.getType() == ExpressionRangeType::Invalid || arg_range.hasNulls();
850  const auto& extract_expr_ti = extract_expr->get_from_expr()->get_type_info();
851  switch (extract_field) {
852  case kYEAR: {
853  if (arg_range.getType() == ExpressionRangeType::Invalid) {
855  }
856  CHECK(arg_range.getType() == ExpressionRangeType::Integer);
857  const int64_t year_range_min =
858  extract_expr_ti.is_high_precision_timestamp()
859  ? ExtractFromTime(
860  kYEAR,
861  arg_range.getIntMin() /
862  get_timestamp_precision_scale(extract_expr_ti.get_dimension()))
863  : ExtractFromTime(kYEAR, arg_range.getIntMin());
864  const int64_t year_range_max =
865  extract_expr_ti.is_high_precision_timestamp()
866  ? ExtractFromTime(
867  kYEAR,
868  arg_range.getIntMax() /
869  get_timestamp_precision_scale(extract_expr_ti.get_dimension()))
870  : ExtractFromTime(kYEAR, arg_range.getIntMax());
872  year_range_min, year_range_max, 0, arg_range.hasNulls());
873  }
874  case kEPOCH:
875  return arg_range;
876  case kQUARTERDAY:
877  case kQUARTER:
878  return ExpressionRange::makeIntRange(1, 4, 0, has_nulls);
879  case kMONTH:
880  return ExpressionRange::makeIntRange(1, 12, 0, has_nulls);
881  case kDAY:
882  return ExpressionRange::makeIntRange(1, 31, 0, has_nulls);
883  case kHOUR:
884  return ExpressionRange::makeIntRange(0, 23, 0, has_nulls);
885  case kMINUTE:
886  return ExpressionRange::makeIntRange(0, 59, 0, has_nulls);
887  case kSECOND:
888  return ExpressionRange::makeIntRange(0, 60, 0, has_nulls);
889  case kMILLISECOND:
890  return ExpressionRange::makeIntRange(0, 999, 0, has_nulls);
891  case kMICROSECOND:
892  return ExpressionRange::makeIntRange(0, 999999, 0, has_nulls);
893  case kNANOSECOND:
894  return ExpressionRange::makeIntRange(0, 999999999, 0, has_nulls);
895  case kDOW:
896  return ExpressionRange::makeIntRange(0, 6, 0, has_nulls);
897  case kISODOW:
898  return ExpressionRange::makeIntRange(1, 7, 0, has_nulls);
899  case kDOY:
900  return ExpressionRange::makeIntRange(1, 366, 0, has_nulls);
901  case kWEEK:
902  return ExpressionRange::makeIntRange(1, 53, 0, has_nulls);
903  default:
904  CHECK(false);
905  }
907 }
908 
910  const Analyzer::DatetruncExpr* datetrunc_expr,
911  const std::vector<InputTableInfo>& query_infos,
912  const Executor* executor,
913  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> simple_quals) {
914  const auto arg_range = getExpressionRange(
915  datetrunc_expr->get_from_expr(), query_infos, executor, simple_quals);
916  if (arg_range.getType() == ExpressionRangeType::Invalid) {
918  }
919  const auto& datetrunc_expr_ti = datetrunc_expr->get_from_expr()->get_type_info();
920  const int64_t min_ts = DateTimeTranslator::getDateTruncConstantValue(
921  arg_range.getIntMin(), datetrunc_expr->get_field(), datetrunc_expr_ti);
922  const int64_t max_ts = DateTimeTranslator::getDateTruncConstantValue(
923  arg_range.getIntMax(), datetrunc_expr->get_field(), datetrunc_expr_ti);
924  const int64_t bucket =
925  datetrunc_expr_ti.is_high_precision_timestamp()
926  ? get_conservative_datetrunc_bucket(datetrunc_expr->get_field()) *
928  datetrunc_expr_ti.get_dimension())
929  : get_conservative_datetrunc_bucket(datetrunc_expr->get_field());
930 
931  return ExpressionRange::makeIntRange(min_ts, max_ts, bucket, arg_range.hasNulls());
932 }
NEVER_INLINE DEVICE int64_t DateTruncate(DatetruncField field, const int64_t timeval)
#define CHECK_EQ(x, y)
Definition: Logger.h:195
#define FIND_STAT_FRAG(stat_name)
int get_column_id() const
Definition: Analyzer.h:194
static ExpressionRange makeNullRange()
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:325
Definition: sqltypes.h:51
bool is_fp() const
Definition: sqltypes.h:454
SQLTypes
Definition: sqltypes.h:40
ExpressionRange operator/(const ExpressionRange &other) const
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)
SQLOps
Definition: sqldefs.h:29
Definition: sqldefs.h:35
HOST DEVICE int get_scale() const
Definition: sqltypes.h:328
Definition: sqldefs.h:36
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:323
#define DEF_OPERATOR(fname, op)
#define CHECK_GE(x, y)
Definition: Logger.h:200
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:840
Definition: sqldefs.h:49
Definition: sqldefs.h:30
Definition: sqldefs.h:41
DEVICE int64_t DateTruncateAlterPrecisionScaleUp(const int64_t timeval, const int64_t scale)
ExpressionRange operator||(const ExpressionRange &other) const
int64_t scale_up_interval_endpoint(const int64_t endpoint, const SQLTypeInfo &ti)
const Expr * get_from_expr() const
Definition: Analyzer.h:1089
void setIntMin(const int64_t int_min)
int64_t getIntMax() const
#define CHECK_GT(x, y)
Definition: Logger.h:199
ExpressionRangeType getType() const
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)
SQLOps get_optype() const
Definition: Analyzer.h:432
DatetruncField get_field() const
Definition: Analyzer.h:1213
const Expr * get_arg() const
Definition: Analyzer.h:797
bool g_null_div_by_zero
Definition: Execute.cpp:73
const Expr * get_else_expr() const
Definition: Analyzer.h:1044
float floatval
Definition: sqltypes.h:127
Datum get_constval() const
Definition: Analyzer.h:328
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
ExpressionRangeType type_
bool is_array() const
Definition: sqltypes.h:458
T v(const TargetValue &r)
DEVICE int64_t DateTruncateAlterPrecisionScaleDown(const int64_t timeval, const int64_t scale)
int64_t getIntMin() const
DatetruncField
Definition: DateTruncate.h:42
int64_t get_conservative_datetrunc_bucket(const DatetruncField datetrunc_field)
#define CHECK_NE(x, y)
Definition: Logger.h:196
const Expr * get_from_expr() const
Definition: Analyzer.h:1214
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:126
NEVER_INLINE DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval)
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1041
ExpressionRange getLeafColumnRange(const Analyzer::ColumnVar *col_expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, const bool is_outer_join_proj)
SQLTypeInfoCore get_elem_type() const
Definition: sqltypes.h:632
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)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64, 64, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void > > checked_int64_t
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)
bool get_is_null() const
Definition: Analyzer.h:327
#define CHECK_LT(x, y)
Definition: Logger.h:197
Definition: sqltypes.h:54
Definition: sqltypes.h:55
Definition: sqldefs.h:40
bool operator==(const ExpressionRange &other) const
int get_rte_idx() const
Definition: Analyzer.h:195
bool hasNulls() const
int get_table_id() const
Definition: Analyzer.h:193
Definition: sqltypes.h:43
void setIntMax(const int64_t int_max)
int64_t extract_min_stat(const ChunkStats &stats, const SQLTypeInfo &ti)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
#define CHECK(condition)
Definition: Logger.h:187
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:48
uint64_t exp_to_scale(const unsigned exp)
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
Definition: sqldefs.h:33
Definition: sqltypes.h:47
static ExpressionRange makeInvalidRange()
static int64_t getDateTruncConstantValue(const int64_t &timeval, const DatetruncField &field, const SQLTypeInfo &ti)
ExtractField get_field() const
Definition: Analyzer.h:1088
ExpressionRange getDateTimePrecisionCastRange(const ExpressionRange &arg_range, const SQLTypeInfo &oper_ti, const SQLTypeInfo &target_ti)
double extract_max_stat_double(const ChunkStats &stats, const SQLTypeInfo &col_ti)
int64_t extract_max_stat(const ChunkStats &stats, const SQLTypeInfo &ti)
SQLOps get_optype() const
Definition: Analyzer.h:363
double doubleval
Definition: sqltypes.h:128
const Expr * get_right_operand() const
Definition: Analyzer.h:436
ExpressionRange fpRangeFromDecimal(const ExpressionRange &arg_range, const int64_t scale, const SQLTypeInfo &target_ti)
const Expr * get_left_operand() const
Definition: Analyzer.h:435
const Expr * get_operand() const
Definition: Analyzer.h:364