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