OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 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  static bool typeSupportsRange(const SQLTypeInfo& ti);
143 
144  private:
145  ExpressionRange(const int64_t int_min_in,
146  const int64_t int_max_in,
147  const int64_t bucket,
148  const bool has_nulls_in)
150  , has_nulls_(has_nulls_in)
151  , int_min_(int_min_in)
152  , int_max_(int_max_in)
153  , bucket_(bucket) {}
154 
156  const double fp_min_in,
157  const double fp_max_in,
158  const bool has_nulls_in)
159  : type_(type)
160  , has_nulls_(has_nulls_in)
161  , fp_min_(fp_min_in)
162  , fp_max_(fp_max_in)
163  , bucket_(0) {
165  }
166 
169 
171  : type_(type), has_nulls_(true), bucket_(0) {
173  }
174 
175  template <class T, class BinOp>
176  ExpressionRange binOp(const ExpressionRange& other, const BinOp& bin_op) const {
177  CHECK(type_ == other.type_);
178  try {
179  std::vector<T> limits{bin_op(getMin<T>(*this), getMin<T>(other)),
180  bin_op(getMin<T>(*this), getMax<T>(other)),
181  bin_op(getMax<T>(*this), getMin<T>(other)),
182  bin_op(getMax<T>(*this), getMax<T>(other))};
184  result.type_ = type_;
185  result.has_nulls_ = has_nulls_ || other.has_nulls_;
186  switch (result.type_) {
188  result.int_min_ = *std::min_element(limits.begin(), limits.end());
189  result.int_max_ = *std::max_element(limits.begin(), limits.end());
190  break;
191  }
194  result.fp_min_ = *std::min_element(limits.begin(), limits.end());
195  result.fp_max_ = *std::max_element(limits.begin(), limits.end());
196  break;
197  }
198  default:
199  CHECK(false);
200  }
201  return result;
202  } catch (...) {
204  }
205  }
206 
209  union {
210  int64_t int_min_;
211  double fp_min_;
212  };
213  union {
214  int64_t int_max_;
215  double fp_max_;
216  };
217  int64_t bucket_;
218 };
219 
220 template <>
221 inline int64_t getMin<int64_t>(const ExpressionRange& e) {
222  return e.getIntMin();
223 }
224 
225 template <>
226 inline float getMin<float>(const ExpressionRange& e) {
227  return e.getFpMin();
228 }
229 
230 template <>
231 inline double getMin<double>(const ExpressionRange& e) {
232  return e.getFpMin();
233 }
234 
235 template <>
236 inline int64_t getMax<int64_t>(const ExpressionRange& e) {
237  return e.getIntMax();
238 }
239 
240 template <>
241 inline float getMax<float>(const ExpressionRange& e) {
242  return e.getFpMax();
243 }
244 
245 template <>
246 inline double getMax<double>(const ExpressionRange& e) {
247  return e.getFpMax();
248 }
249 
250 template <>
251 inline int64_t get_value_from_datum(const Datum datum,
252  const SQLTypes type_info) noexcept {
253  switch (type_info) {
254  case kBOOLEAN:
255  return datum.boolval;
256  case kTINYINT:
257  return datum.tinyintval;
258  case kSMALLINT:
259  return datum.smallintval;
260  case kINT:
261  return datum.intval;
262  case kBIGINT:
263  return datum.bigintval;
264  case kTIME:
265  case kTIMESTAMP:
266  case kDATE:
267  return datum.bigintval;
268  case kNUMERIC:
269  case kDECIMAL: {
270  return datum.bigintval;
271  break;
272  }
273  default:
274  UNREACHABLE();
275  }
276  return (int64_t)0;
277 }
278 
279 template <>
280 inline double get_value_from_datum(const Datum datum, const SQLTypes type_info) noexcept {
281  switch (type_info) {
282  case kFLOAT:
283  return datum.floatval;
284  case kDOUBLE:
285  return datum.doubleval;
286  default:
287  UNREACHABLE();
288  }
289  return 0.0;
290 }
291 
292 void apply_int_qual(const Datum const_datum,
293  const SQLTypes const_type,
294  const SQLOps sql_op,
295  ExpressionRange& qual_range);
296 void apply_fp_qual(const Datum const_datum,
297  const SQLTypes const_type,
298  const SQLOps sql_op,
299  ExpressionRange& qual_range);
300 void apply_hpt_qual(const Datum const_datum,
301  const SQLTypes const_type,
302  const int32_t const_dimen,
303  const int32_t col_dimen,
304  const SQLOps sql_op,
305  ExpressionRange& qual_range);
306 
308  const Analyzer::ColumnVar*,
309  const ExpressionRange&,
310  const boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> = boost::none);
311 
312 class Executor;
313 struct InputTableInfo;
314 
316  const std::vector<InputTableInfo>&,
317  const Executor*,
318  const bool is_outer_join_proj);
319 
321  const Analyzer::Expr*,
322  const std::vector<InputTableInfo>&,
323  const Executor*,
324  boost::optional<std::list<std::shared_ptr<Analyzer::Expr>>> = boost::none);
325 
326 #endif // QUERYENGINE_EXPRESSIONRANGE_H
int64_t getIntMin() const
ExpressionRange binOp(const ExpressionRange &other, const BinOp &bin_op) const
static ExpressionRange makeNullRange()
Definition: sqltypes.h:52
SQLTypes
Definition: sqltypes.h:41
bool 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)
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
ExpressionRange operator-(const ExpressionRange &other) const
#define UNREACHABLE()
Definition: Logger.h:234
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)
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_
CHECK(cgen_state)
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)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64, 64, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void > > checked_int64_t
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:56
double getFpMax() const
double getFpMin() const
float getMin< float >(const ExpressionRange &e)
ExpressionRange operator/(const ExpressionRange &other) const
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:71
int64_t getBucket() const
void setFpMax(const double fp_max)
float getMax< float >(const ExpressionRange &e)
static bool typeSupportsRange(const SQLTypeInfo &ti)
Definition: sqltypes.h:48
void setFpMin(const double fp_min)
static ExpressionRange makeInvalidRange()
int64_t getMax< int64_t >(const ExpressionRange &e)
ExpressionRange operator*(const ExpressionRange &other) const