OmniSciDB  dfae7c3b14
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 
32 
33 class ExpressionRange;
34 
35 class InputColDescriptor;
36 
37 template <typename T>
38 T getMin(const ExpressionRange& other);
39 
40 template <typename T>
41 T getMax(const ExpressionRange& other);
42 
43 template <typename T>
44 T get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept;
45 
47  public:
48  static ExpressionRange makeIntRange(const int64_t int_min,
49  const int64_t int_max,
50  const int64_t bucket,
51  const bool has_nulls) {
52  return ExpressionRange(int_min, int_max, bucket, has_nulls);
53  }
54 
55  static ExpressionRange makeDoubleRange(const double fp_min,
56  const double fp_max,
57  const bool has_nulls) {
58  return ExpressionRange(ExpressionRangeType::Double, fp_min, fp_max, has_nulls);
59  }
60 
61  static ExpressionRange makeFloatRange(const float fp_min,
62  const float fp_max,
63  const bool has_nulls) {
64  return ExpressionRange(ExpressionRangeType::Float, fp_min, fp_max, has_nulls);
65  }
66 
69  }
70 
72 
73  int64_t getIntMin() const {
75  return int_min_;
76  }
77 
78  int64_t getIntMax() const {
80  return int_max_;
81  }
82 
83  double getFpMin() const {
85  return fp_min_;
86  }
87 
88  double getFpMax() const {
90  return fp_max_;
91  }
92 
93  void setIntMin(const int64_t int_min) {
95  int_min_ = int_min;
96  }
97 
98  void setIntMax(const int64_t int_max) {
100  int_max_ = int_max;
101  }
102 
105  int_max_ = -1;
106  int_min_ = 0;
107  }
108 
109  void setFpMin(const double fp_min) {
111  fp_min_ = fp_min;
112  }
113 
114  void setFpMax(const double fp_max) {
116  fp_max_ = fp_max;
117  }
118 
119  ExpressionRangeType getType() const { return type_; }
120 
121  int64_t getBucket() const {
123  return bucket_;
124  }
125 
126  bool hasNulls() const {
128  return has_nulls_;
129  }
130 
131  void setHasNulls() { has_nulls_ = true; }
132 
133  ExpressionRange operator+(const ExpressionRange& other) const;
134  ExpressionRange operator-(const ExpressionRange& other) const;
135  ExpressionRange operator*(const ExpressionRange& other) const;
136  ExpressionRange operator/(const ExpressionRange& other) const;
137  ExpressionRange operator||(const ExpressionRange& other) const;
138 
139  bool operator==(const ExpressionRange& other) const;
140 
141  static bool typeSupportsRange(const SQLTypeInfo& ti);
142 
143  private:
144  ExpressionRange(const int64_t int_min_in,
145  const int64_t int_max_in,
146  const int64_t bucket,
147  const bool has_nulls_in)
148  : type_(ExpressionRangeType::Integer)
149  , has_nulls_(has_nulls_in)
150  , int_min_(int_min_in)
151  , int_max_(int_max_in)
152  , bucket_(bucket) {}
153 
155  const double fp_min_in,
156  const double fp_max_in,
157  const bool has_nulls_in)
158  : type_(type)
159  , has_nulls_(has_nulls_in)
160  , fp_min_(fp_min_in)
161  , fp_max_(fp_max_in)
162  , bucket_(0) {
164  }
165 
167  : type_(ExpressionRangeType::Invalid), has_nulls_(false), bucket_(0) {}
168 
170  : type_(type), has_nulls_(true), bucket_(0) {
172  }
173 
174  template <class T, class BinOp>
175  ExpressionRange binOp(const ExpressionRange& other, const BinOp& bin_op) const {
176  CHECK(type_ == other.type_);
177  try {
178  std::vector<T> limits{bin_op(getMin<T>(*this), getMin<T>(other)),
179  bin_op(getMin<T>(*this), getMax<T>(other)),
180  bin_op(getMax<T>(*this), getMin<T>(other)),
181  bin_op(getMax<T>(*this), getMax<T>(other))};
183  result.type_ = type_;
184  result.has_nulls_ = has_nulls_ || other.has_nulls_;
185  switch (result.type_) {
187  result.int_min_ = *std::min_element(limits.begin(), limits.end());
188  result.int_max_ = *std::max_element(limits.begin(), limits.end());
189  break;
190  }
193  result.fp_min_ = *std::min_element(limits.begin(), limits.end());
194  result.fp_max_ = *std::max_element(limits.begin(), limits.end());
195  break;
196  }
197  default:
198  CHECK(false);
199  }
200  return result;
201  } catch (...) {
203  }
204  }
205 
208  union {
209  int64_t int_min_;
210  double fp_min_;
211  };
212  union {
213  int64_t int_max_;
214  double fp_max_;
215  };
216  int64_t bucket_;
217 };
218 
219 template <>
220 inline int64_t getMin<int64_t>(const ExpressionRange& e) {
221  return e.getIntMin();
222 }
223 
224 template <>
225 inline float getMin<float>(const ExpressionRange& e) {
226  return e.getFpMin();
227 }
228 
229 template <>
230 inline double getMin<double>(const ExpressionRange& e) {
231  return e.getFpMin();
232 }
233 
234 template <>
235 inline int64_t getMax<int64_t>(const ExpressionRange& e) {
236  return e.getIntMax();
237 }
238 
239 template <>
240 inline float getMax<float>(const ExpressionRange& e) {
241  return e.getFpMax();
242 }
243 
244 template <>
245 inline double getMax<double>(const ExpressionRange& e) {
246  return e.getFpMax();
247 }
248 
249 template <>
250 inline int64_t get_value_from_datum(const Datum datum,
251  const SQLTypes type_info) noexcept {
252  switch (type_info) {
253  case kBOOLEAN:
254  return datum.boolval;
255  case kTINYINT:
256  return datum.tinyintval;
257  case kSMALLINT:
258  return datum.smallintval;
259  case kINT:
260  return datum.intval;
261  case kBIGINT:
262  return datum.bigintval;
263  case kTIME:
264  case kTIMESTAMP:
265  case kDATE:
266  return datum.bigintval;
267  case kNUMERIC:
268  case kDECIMAL: {
269  return datum.bigintval;
270  break;
271  }
272  default:
273  UNREACHABLE();
274  }
275  return (int64_t)0;
276 }
277 
278 template <>
279 inline double get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept {
280  switch (type_info) {
281  case kFLOAT:
282  return datum.floatval;
283  case kDOUBLE:
284  return datum.doubleval;
285  default:
286  UNREACHABLE();
287  }
288  return 0.0;
289 }
290 
291 void apply_int_qual(const Datum const_datum,
292  const SQLTypes const_type,
293  const SQLOps sql_op,
294  ExpressionRange& qual_range);
295 void apply_fp_qual(const Datum const_datum,
296  const SQLTypes const_type,
297  const SQLOps sql_op,
298  ExpressionRange& qual_range);
299 void apply_hpt_qual(const Datum const_datum,
300  const SQLTypes const_type,
301  const int32_t const_dimen,
302  const int32_t col_dimen,
303  const SQLOps sql_op,
304  ExpressionRange& qual_range);
305 
307  const Analyzer::ColumnVar*,
308  const ExpressionRange&,
309  const boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> = boost::none);
310 
311 class Executor;
312 struct InputTableInfo;
313 
315  const std::vector<InputTableInfo>&,
316  const Executor*,
317  const bool is_outer_join_proj);
318 
320  const Analyzer::Expr*,
321  const std::vector<InputTableInfo>&,
322  const Executor*,
323  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> = boost::none);
324 
325 #endif // QUERYENGINE_EXPRESSIONRANGE_H
int8_t tinyintval
Definition: sqltypes.h:135
static ExpressionRange makeNullRange()
Definition: sqltypes.h:51
SQLTypes
Definition: sqltypes.h:40
int64_t getBucket() const
double getMax< double >(const ExpressionRange &e)
ExpressionRange(const ExpressionRangeType type)
bool boolval
Definition: sqltypes.h:134
T getMax(const ExpressionRange &other)
SQLOps
Definition: sqldefs.h:29
double getFpMin() const
#define UNREACHABLE()
Definition: Logger.h:241
T get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept
ExpressionRange getLeafColumnRange(const Analyzer::ColumnVar *, const std::vector< InputTableInfo > &, const Executor *, const bool is_outer_join_proj)
ExpressionRange binOp(const ExpressionRange &other, const BinOp &bin_op) const
ExpressionRangeType
void setIntMin(const int64_t int_min)
int64_t getIntMax() const
ExpressionRangeType getType() const
int32_t intval
Definition: sqltypes.h:137
static ExpressionRange makeFloatRange(const float fp_min, const float fp_max, const bool has_nulls)
T getMin(const ExpressionRange &other)
ExpressionRange(const ExpressionRangeType type, const double fp_min_in, const double fp_max_in, const bool has_nulls_in)
float floatval
Definition: sqltypes.h:139
ExpressionRangeType type_
int64_t getIntMin() const
int64_t bigintval
Definition: sqltypes.h:138
int16_t smallintval
Definition: sqltypes.h:136
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:55
ExpressionRange getExpressionRange(const Analyzer::Expr *, const std::vector< InputTableInfo > &, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>=boost::none)
bool hasNulls() const
float getMin< float >(const ExpressionRange &e)
ExpressionRange apply_simple_quals(const Analyzer::ColumnVar *, const ExpressionRange &, const boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>>=boost::none)
bool operator==(const SlotSize &lhs, const SlotSize &rhs)
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)
#define CHECK(condition)
Definition: Logger.h:197
void setFpMax(const double fp_max)
float getMax< float >(const ExpressionRange &e)
double getFpMax() 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)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64, 64, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void > > checked_int64_t
Definition: sqltypes.h:47
void setFpMin(const double fp_min)
static ExpressionRange makeInvalidRange()
void apply_int_qual(const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
void apply_fp_qual(const Datum const_datum, const SQLTypes const_type, const SQLOps sql_op, ExpressionRange &qual_range)
int64_t getMax< int64_t >(const ExpressionRange &e)
double doubleval
Definition: sqltypes.h:140