OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExpressionRange.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 #ifndef QUERYENGINE_EXPRESSIONRANGE_H
18 #define QUERYENGINE_EXPRESSIONRANGE_H
19 
20 #include "../Analyzer/Analyzer.h"
21 
22 #include <boost/multiprecision/cpp_int.hpp> //#include <boost/none.hpp>
23 #include <boost/optional.hpp>
24 
25 using checked_int64_t = boost::multiprecision::number<
26  boost::multiprecision::cpp_int_backend<64,
27  64,
28  boost::multiprecision::signed_magnitude,
29  boost::multiprecision::checked,
30  void>>;
31 
32 // set the min / max bit to 15 instead of 16 (minus sign bit, 15 = 16 - 1)
33 // to check a valid positive int16_t range: 0 ~ 32,767, not 0 ~ 65,535
34 // (similar to check negative int16_t value range: -32,768 ~ 0, not -65,535 ~ 0)
35 using checked_int16_t = boost::multiprecision::number<
36  boost::multiprecision::cpp_int_backend<15,
37  15,
38  boost::multiprecision::signed_magnitude,
39  boost::multiprecision::checked,
40  void>>;
41 
43 
44 class ExpressionRange;
45 
46 class InputColDescriptor;
47 
48 template <typename T>
49 T getMin(const ExpressionRange& other);
50 
51 template <typename T>
52 T getMax(const ExpressionRange& other);
53 
54 template <typename T>
55 T get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept;
56 
58  public:
59  static ExpressionRange makeIntRange(const int64_t int_min,
60  const int64_t int_max,
61  const int64_t bucket,
62  const bool has_nulls) {
63  return ExpressionRange(int_min, int_max, bucket, has_nulls);
64  }
65 
66  static ExpressionRange makeDoubleRange(const double fp_min,
67  const double fp_max,
68  const bool has_nulls) {
69  return ExpressionRange(ExpressionRangeType::Double, fp_min, fp_max, has_nulls);
70  }
71 
72  static ExpressionRange makeFloatRange(const float fp_min,
73  const float fp_max,
74  const bool has_nulls) {
75  return ExpressionRange(ExpressionRangeType::Float, fp_min, fp_max, has_nulls);
76  }
77 
80  }
81 
83 
84  int64_t getIntMin() const {
86  return int_min_;
87  }
88 
89  int64_t getIntMax() const {
91  return int_max_;
92  }
93 
94  double getFpMin() const {
96  return fp_min_;
97  }
98 
99  double getFpMax() const {
101  return fp_max_;
102  }
103 
104  void setIntMin(const int64_t int_min) {
106  int_min_ = int_min;
107  }
108 
109  void setIntMax(const int64_t int_max) {
111  int_max_ = int_max;
112  }
113 
116  int_max_ = -1;
117  int_min_ = 0;
118  }
119 
120  void setFpMin(const double fp_min) {
122  fp_min_ = fp_min;
123  }
124 
125  void setFpMax(const double fp_max) {
127  fp_max_ = fp_max;
128  }
129 
130  ExpressionRangeType getType() const { return type_; }
131 
132  int64_t getBucket() const {
134  return bucket_;
135  }
136 
137  bool hasNulls() const {
139  return has_nulls_;
140  }
141 
142  void setHasNulls() { has_nulls_ = true; }
143 
144  void setNulls(bool n) { has_nulls_ = n; }
145 
146  ExpressionRange operator+(const ExpressionRange& other) const;
147  ExpressionRange operator-(const ExpressionRange& other) const;
148  ExpressionRange operator*(const ExpressionRange& other) const;
149  ExpressionRange operator/(const ExpressionRange& other) const;
150  ExpressionRange operator||(const ExpressionRange& other) const;
151 
152  bool operator==(const ExpressionRange& other) const;
153 
154  static bool typeSupportsRange(const SQLTypeInfo& ti);
155 
156  std::string toString() const {
157  std::ostringstream oss;
158  switch (type_) {
160  oss << has_nulls_ << "|" << std::to_string(int_min_) << "|"
162  break;
165  oss << has_nulls_ << "|" << std::to_string(fp_min_) << "|"
167  break;
168  default:
169  oss << "INVALID";
170  break;
171  }
172  oss << "|" << std::to_string(bucket_);
173  return oss.str();
174  }
175 
176  private:
177  ExpressionRange(const int64_t int_min_in,
178  const int64_t int_max_in,
179  const int64_t bucket,
180  const bool has_nulls_in)
182  , has_nulls_(has_nulls_in)
183  , int_min_(int_min_in)
184  , int_max_(int_max_in)
185  , bucket_(bucket) {}
186 
188  const double fp_min_in,
189  const double fp_max_in,
190  const bool has_nulls_in)
191  : type_(type)
192  , has_nulls_(has_nulls_in)
193  , fp_min_(fp_min_in)
194  , fp_max_(fp_max_in)
195  , bucket_(0) {
197  }
198 
201 
203  : type_(type), has_nulls_(true), bucket_(0) {
205  }
206 
207  template <class T, class BinOp>
208  ExpressionRange binOp(const ExpressionRange& other, const BinOp& bin_op) const {
209  CHECK(type_ == other.type_);
210  try {
211  std::vector<T> limits{bin_op(getMin<T>(*this), getMin<T>(other)),
212  bin_op(getMin<T>(*this), getMax<T>(other)),
213  bin_op(getMax<T>(*this), getMin<T>(other)),
214  bin_op(getMax<T>(*this), getMax<T>(other))};
216  result.type_ = type_;
217  result.has_nulls_ = has_nulls_ || other.has_nulls_;
218  switch (result.type_) {
220  result.int_min_ = *std::min_element(limits.begin(), limits.end());
221  result.int_max_ = *std::max_element(limits.begin(), limits.end());
222  break;
223  }
226  result.fp_min_ = *std::min_element(limits.begin(), limits.end());
227  result.fp_max_ = *std::max_element(limits.begin(), limits.end());
228  break;
229  }
230  default:
231  CHECK(false);
232  }
233  return result;
234  } catch (...) {
236  }
237  }
238 
241  union {
242  int64_t int_min_;
243  double fp_min_;
244  };
245  union {
246  int64_t int_max_;
247  double fp_max_;
248  };
249  int64_t bucket_;
250 };
251 
252 template <>
253 inline int64_t getMin<int64_t>(const ExpressionRange& e) {
254  return e.getIntMin();
255 }
256 
257 template <>
258 inline float getMin<float>(const ExpressionRange& e) {
259  return e.getFpMin();
260 }
261 
262 template <>
263 inline double getMin<double>(const ExpressionRange& e) {
264  return e.getFpMin();
265 }
266 
267 template <>
268 inline int64_t getMax<int64_t>(const ExpressionRange& e) {
269  return e.getIntMax();
270 }
271 
272 template <>
273 inline float getMax<float>(const ExpressionRange& e) {
274  return e.getFpMax();
275 }
276 
277 template <>
278 inline double getMax<double>(const ExpressionRange& e) {
279  return e.getFpMax();
280 }
281 
282 template <>
283 inline int64_t get_value_from_datum(const Datum datum,
284  const SQLTypes type_info) noexcept {
285  switch (type_info) {
286  case kBOOLEAN:
287  return datum.boolval;
288  case kTINYINT:
289  return datum.tinyintval;
290  case kSMALLINT:
291  return datum.smallintval;
292  case kINT:
293  return datum.intval;
294  case kBIGINT:
295  return datum.bigintval;
296  case kTIME:
297  case kTIMESTAMP:
298  case kDATE:
299  return datum.bigintval;
300  case kNUMERIC:
301  case kDECIMAL: {
302  return datum.bigintval;
303  break;
304  }
305  default:
306  UNREACHABLE();
307  }
308  return (int64_t)0;
309 }
310 
311 template <>
312 inline double get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept {
313  switch (type_info) {
314  case kFLOAT:
315  return datum.floatval;
316  case kDOUBLE:
317  return datum.doubleval;
318  default:
319  UNREACHABLE();
320  }
321  return 0.0;
322 }
323 
324 void apply_int_qual(const Datum const_datum,
325  const SQLTypes const_type,
326  const SQLOps sql_op,
327  ExpressionRange& qual_range);
328 void apply_fp_qual(const Datum const_datum,
329  const SQLTypes const_type,
330  const SQLOps sql_op,
331  ExpressionRange& qual_range);
332 void apply_hpt_qual(const Datum const_datum,
333  const SQLTypes const_type,
334  const int32_t const_dimen,
335  const int32_t col_dimen,
336  const SQLOps sql_op,
337  ExpressionRange& qual_range);
338 
340  const Analyzer::ColumnVar*,
341  const ExpressionRange&,
342  const boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> = boost::none);
343 
344 class Executor;
345 struct InputTableInfo;
346 
348  const std::vector<InputTableInfo>&,
349  const Executor*,
350  const bool is_outer_join_proj);
351 
353  const Analyzer::Expr*,
354  const std::vector<InputTableInfo>&,
355  const Executor*,
356  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> = boost::none);
357 
358 #endif // QUERYENGINE_EXPRESSIONRANGE_H
int64_t getIntMin() const
std::string toString() const
ExpressionRange binOp(const ExpressionRange &other, const BinOp &bin_op) const
static ExpressionRange makeNullRange()
Definition: sqltypes.h:76
SQLTypes
Definition: sqltypes.h:65
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)
double getMax< double >(const ExpressionRange &e)
void apply_int_qual(const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
ExpressionRange(const ExpressionRangeType type)
T getMax(const ExpressionRange &other)
SQLOps
Definition: sqldefs.h:31
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 15, 15, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void >> checked_int16_t
ExpressionRange operator-(const ExpressionRange &other) const
#define UNREACHABLE()
Definition: Logger.h:338
T get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept
ExpressionRangeType
void setIntMin(const int64_t int_min)
static ExpressionRange makeFloatRange(const float fp_min, const float fp_max, const bool has_nulls)
std::string to_string(char const *&&v)
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)
T getMin(const ExpressionRange &other)
ExpressionRange(const ExpressionRangeType type, const double fp_min_in, const double fp_max_in, const bool has_nulls_in)
ExpressionRange operator+(const ExpressionRange &other) const
ExpressionRangeType type_
ExpressionRange operator||(const ExpressionRange &other) const
void apply_fp_qual(const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
ExpressionRange getLeafColumnRange(const Analyzer::ColumnVar *col_expr, const std::vector< InputTableInfo > &query_infos, const Executor *executor, const bool is_outer_join_proj)
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
bool g_enable_smem_group_by true
static ExpressionRange makeIntRange(const int64_t int_min, const int64_t int_max, const int64_t bucket, const bool has_nulls)
int64_t getMin< int64_t >(const ExpressionRange &e)
double getMin< double >(const ExpressionRange &e)
static ExpressionRange makeDoubleRange(const double fp_min, const double fp_max, const bool has_nulls)
Definition: sqltypes.h:80
double getFpMax() const
double getFpMin() const
float getMin< float >(const ExpressionRange &e)
ExpressionRange operator/(const ExpressionRange &other) const
void setNulls(bool n)
void setIntMax(const int64_t int_max)
ExpressionRange(const int64_t int_min_in, const int64_t int_max_in, const int64_t bucket, const bool has_nulls_in)
ExpressionRangeType getType() const
int64_t getIntMax() const
bool g_enable_watchdog false
Definition: Execute.cpp:80
int64_t getBucket() const
#define CHECK(condition)
Definition: Logger.h:291
void setFpMax(const double fp_max)
float getMax< float >(const ExpressionRange &e)
static bool typeSupportsRange(const SQLTypeInfo &ti)
Definition: sqltypes.h:72
void setFpMin(const double fp_min)
static ExpressionRange makeInvalidRange()
constexpr double n
Definition: Utm.h:38
Definition: Datum.h:71
int64_t getMax< int64_t >(const ExpressionRange &e)
ExpressionRange operator*(const ExpressionRange &other) const