OmniSciDB  471d68cefb
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExpressionRange.h
Go to the documentation of this file.
1 /* * Copyright 2017 MapD Technologies, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef QUERYENGINE_EXPRESSIONRANGE_H
17 #define QUERYENGINE_EXPRESSIONRANGE_H
18 
19 #include "../Analyzer/Analyzer.h"
20 
21 #include <boost/multiprecision/cpp_int.hpp> //#include <boost/none.hpp>
22 #include <boost/optional.hpp>
23 
24 using checked_int64_t = boost::multiprecision::number<
25  boost::multiprecision::cpp_int_backend<64,
26  64,
27  boost::multiprecision::signed_magnitude,
28  boost::multiprecision::checked,
29  void>>;
30 
31 // set the min / max bit to 15 instead of 16 (minus sign bit, 15 = 16 - 1)
32 // to check a valid positive int16_t range: 0 ~ 32,767, not 0 ~ 65,535
33 // (similar to check negative int16_t value range: -32,768 ~ 0, not -65,535 ~ 0)
34 using checked_int16_t = boost::multiprecision::number<
35  boost::multiprecision::cpp_int_backend<15,
36  15,
37  boost::multiprecision::signed_magnitude,
38  boost::multiprecision::checked,
39  void>>;
40 
42 
43 class ExpressionRange;
44 
45 class InputColDescriptor;
46 
47 template <typename T>
48 T getMin(const ExpressionRange& other);
49 
50 template <typename T>
51 T getMax(const ExpressionRange& other);
52 
53 template <typename T>
54 T get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept;
55 
57  public:
58  static ExpressionRange makeIntRange(const int64_t int_min,
59  const int64_t int_max,
60  const int64_t bucket,
61  const bool has_nulls) {
62  return ExpressionRange(int_min, int_max, bucket, has_nulls);
63  }
64 
65  static ExpressionRange makeDoubleRange(const double fp_min,
66  const double fp_max,
67  const bool has_nulls) {
68  return ExpressionRange(ExpressionRangeType::Double, fp_min, fp_max, has_nulls);
69  }
70 
71  static ExpressionRange makeFloatRange(const float fp_min,
72  const float fp_max,
73  const bool has_nulls) {
74  return ExpressionRange(ExpressionRangeType::Float, fp_min, fp_max, has_nulls);
75  }
76 
79  }
80 
82 
83  int64_t getIntMin() const {
85  return int_min_;
86  }
87 
88  int64_t getIntMax() const {
90  return int_max_;
91  }
92 
93  double getFpMin() const {
95  return fp_min_;
96  }
97 
98  double getFpMax() const {
100  return fp_max_;
101  }
102 
103  void setIntMin(const int64_t int_min) {
105  int_min_ = int_min;
106  }
107 
108  void setIntMax(const int64_t int_max) {
110  int_max_ = int_max;
111  }
112 
115  int_max_ = -1;
116  int_min_ = 0;
117  }
118 
119  void setFpMin(const double fp_min) {
121  fp_min_ = fp_min;
122  }
123 
124  void setFpMax(const double fp_max) {
126  fp_max_ = fp_max;
127  }
128 
129  ExpressionRangeType getType() const { return type_; }
130 
131  int64_t getBucket() const {
133  return bucket_;
134  }
135 
136  bool hasNulls() const {
138  return has_nulls_;
139  }
140 
141  void setHasNulls() { has_nulls_ = true; }
142 
143  void setNulls(bool n) { has_nulls_ = n; }
144 
145  ExpressionRange operator+(const ExpressionRange& other) const;
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 
151  bool operator==(const ExpressionRange& other) const;
152 
153  static bool typeSupportsRange(const SQLTypeInfo& ti);
154 
155  std::string toString() const {
156  std::ostringstream oss;
157  switch (type_) {
159  oss << has_nulls_ << "|" << std::to_string(int_min_) << "|"
161  break;
164  oss << has_nulls_ << "|" << std::to_string(fp_min_) << "|"
166  break;
167  default:
168  oss << "INVALID";
169  break;
170  }
171  oss << "|" << std::to_string(bucket_);
172  return oss.str();
173  }
174 
175  private:
176  ExpressionRange(const int64_t int_min_in,
177  const int64_t int_max_in,
178  const int64_t bucket,
179  const bool has_nulls_in)
181  , has_nulls_(has_nulls_in)
182  , int_min_(int_min_in)
183  , int_max_(int_max_in)
184  , bucket_(bucket) {}
185 
187  const double fp_min_in,
188  const double fp_max_in,
189  const bool has_nulls_in)
190  : type_(type)
191  , has_nulls_(has_nulls_in)
192  , fp_min_(fp_min_in)
193  , fp_max_(fp_max_in)
194  , bucket_(0) {
196  }
197 
200 
202  : type_(type), has_nulls_(true), bucket_(0) {
204  }
205 
206  template <class T, class BinOp>
207  ExpressionRange binOp(const ExpressionRange& other, const BinOp& bin_op) const {
208  CHECK(type_ == other.type_);
209  try {
210  std::vector<T> limits{bin_op(getMin<T>(*this), getMin<T>(other)),
211  bin_op(getMin<T>(*this), getMax<T>(other)),
212  bin_op(getMax<T>(*this), getMin<T>(other)),
213  bin_op(getMax<T>(*this), getMax<T>(other))};
215  result.type_ = type_;
216  result.has_nulls_ = has_nulls_ || other.has_nulls_;
217  switch (result.type_) {
219  result.int_min_ = *std::min_element(limits.begin(), limits.end());
220  result.int_max_ = *std::max_element(limits.begin(), limits.end());
221  break;
222  }
225  result.fp_min_ = *std::min_element(limits.begin(), limits.end());
226  result.fp_max_ = *std::max_element(limits.begin(), limits.end());
227  break;
228  }
229  default:
230  CHECK(false);
231  }
232  return result;
233  } catch (...) {
235  }
236  }
237 
240  union {
241  int64_t int_min_;
242  double fp_min_;
243  };
244  union {
245  int64_t int_max_;
246  double fp_max_;
247  };
248  int64_t bucket_;
249 };
250 
251 template <>
252 inline int64_t getMin<int64_t>(const ExpressionRange& e) {
253  return e.getIntMin();
254 }
255 
256 template <>
257 inline float getMin<float>(const ExpressionRange& e) {
258  return e.getFpMin();
259 }
260 
261 template <>
262 inline double getMin<double>(const ExpressionRange& e) {
263  return e.getFpMin();
264 }
265 
266 template <>
267 inline int64_t getMax<int64_t>(const ExpressionRange& e) {
268  return e.getIntMax();
269 }
270 
271 template <>
272 inline float getMax<float>(const ExpressionRange& e) {
273  return e.getFpMax();
274 }
275 
276 template <>
277 inline double getMax<double>(const ExpressionRange& e) {
278  return e.getFpMax();
279 }
280 
281 template <>
282 inline int64_t get_value_from_datum(const Datum datum,
283  const SQLTypes type_info) noexcept {
284  switch (type_info) {
285  case kBOOLEAN:
286  return datum.boolval;
287  case kTINYINT:
288  return datum.tinyintval;
289  case kSMALLINT:
290  return datum.smallintval;
291  case kINT:
292  return datum.intval;
293  case kBIGINT:
294  return datum.bigintval;
295  case kTIME:
296  case kTIMESTAMP:
297  case kDATE:
298  return datum.bigintval;
299  case kNUMERIC:
300  case kDECIMAL: {
301  return datum.bigintval;
302  break;
303  }
304  default:
305  UNREACHABLE();
306  }
307  return (int64_t)0;
308 }
309 
310 template <>
311 inline double get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept {
312  switch (type_info) {
313  case kFLOAT:
314  return datum.floatval;
315  case kDOUBLE:
316  return datum.doubleval;
317  default:
318  UNREACHABLE();
319  }
320  return 0.0;
321 }
322 
323 void apply_int_qual(const Datum const_datum,
324  const SQLTypes const_type,
325  const SQLOps sql_op,
326  ExpressionRange& qual_range);
327 void apply_fp_qual(const Datum const_datum,
328  const SQLTypes const_type,
329  const SQLOps sql_op,
330  ExpressionRange& qual_range);
331 void apply_hpt_qual(const Datum const_datum,
332  const SQLTypes const_type,
333  const int32_t const_dimen,
334  const int32_t col_dimen,
335  const SQLOps sql_op,
336  ExpressionRange& qual_range);
337 
339  const Analyzer::ColumnVar*,
340  const ExpressionRange&,
341  const boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> = boost::none);
342 
343 class Executor;
344 struct InputTableInfo;
345 
347  const std::vector<InputTableInfo>&,
348  const Executor*,
349  const bool is_outer_join_proj);
350 
352  const Analyzer::Expr*,
353  const std::vector<InputTableInfo>&,
354  const Executor*,
355  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> = boost::none);
356 
357 #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:49
SQLTypes
Definition: sqltypes.h:38
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:29
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:253
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:53
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:76
int64_t getBucket() const
#define CHECK(condition)
Definition: Logger.h:209
void setFpMax(const double fp_max)
float getMax< float >(const ExpressionRange &e)
static bool typeSupportsRange(const SQLTypeInfo &ti)
Definition: sqltypes.h:45
void setFpMin(const double fp_min)
static ExpressionRange makeInvalidRange()
constexpr double n
Definition: Utm.h:46
int64_t getMax< int64_t >(const ExpressionRange &e)
ExpressionRange operator*(const ExpressionRange &other) const