OmniSciDB  dfae7c3b14
SqlTypesLayout.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 
22 #ifndef QUERYENGINE_SQLTYPESLAYOUT_H
23 #define QUERYENGINE_SQLTYPESLAYOUT_H
24 
25 #include "../Shared/TargetInfo.h"
26 
27 #include "Logger/Logger.h"
28 
29 #include <limits>
30 
31 class OverflowOrUnderflow : public std::runtime_error {
32  public:
33  OverflowOrUnderflow() : std::runtime_error("Overflow or underflow") {}
34 };
35 
36 inline const SQLTypeInfo get_compact_type(const TargetInfo& target) {
37  if (!target.is_agg) {
38  return (target.sql_type.is_column() ? target.sql_type.get_elem_type()
39  : target.sql_type);
40  }
41  const auto agg_type = target.agg_kind;
42  const auto& agg_arg = target.agg_arg_type;
43  if (agg_arg.get_type() == kNULLT) {
44  CHECK_EQ(kCOUNT, agg_type);
45  CHECK(!target.is_distinct);
46  return target.sql_type;
47  }
48 
49  if (is_agg_domain_range_equivalent(agg_type)) {
50  return agg_arg;
51  } else {
52  // Nullability of the target needs to match that of the agg for proper initialization
53  // of target (aggregate) values
54  auto modified_target_type = target.sql_type;
55  modified_target_type.set_notnull(agg_arg.get_notnull());
56  return modified_target_type;
57  }
58 }
59 
60 inline void set_compact_type(TargetInfo& target, const SQLTypeInfo& new_type) {
61  if (target.is_agg) {
62  const auto agg_type = target.agg_kind;
63  auto& agg_arg = target.agg_arg_type;
64  if (agg_type != kCOUNT || agg_arg.get_type() != kNULLT) {
65  agg_arg = new_type;
66  return;
67  }
68  }
69  target.sql_type = new_type;
70 }
71 
72 inline int64_t inline_int_null_val(const SQLTypeInfo& ti) {
73  auto type = ti.get_type();
74  if (ti.is_string()) {
76  CHECK_EQ(4, ti.get_logical_size());
77  type = kINT;
78  }
79  switch (type) {
80  case kBOOLEAN:
81  return inline_int_null_value<int8_t>();
82  case kTINYINT:
83  return inline_int_null_value<int8_t>();
84  case kSMALLINT:
85  return inline_int_null_value<int16_t>();
86  case kINT:
87  return inline_int_null_value<int32_t>();
88  case kBIGINT:
89  return inline_int_null_value<int64_t>();
90  case kTIMESTAMP:
91  case kTIME:
92  case kDATE:
93  case kINTERVAL_DAY_TIME:
95  return inline_int_null_value<int64_t>();
96  case kDECIMAL:
97  case kNUMERIC:
98  return inline_int_null_value<int64_t>();
99  default:
100  abort();
101  }
102 }
103 
104 inline int64_t inline_fixed_encoding_null_val(const SQLTypeInfo& ti) {
105  if (ti.get_compression() == kENCODING_NONE) {
106  return inline_int_null_val(ti);
107  }
109  switch (ti.get_comp_param()) {
110  case 0:
111  case 32:
112  return inline_int_null_value<int32_t>();
113  case 16:
114  return inline_int_null_value<int16_t>();
115  default:
116 #ifndef __CUDACC__
117  CHECK(false) << "Unknown encoding width for date in days: "
118  << ti.get_comp_param();
119 #else
120  CHECK(false);
121 #endif
122  }
123  }
124  if (ti.get_compression() == kENCODING_DICT) {
125  CHECK(ti.is_string());
126  switch (ti.get_size()) {
127  case 1:
128  return inline_int_null_value<uint8_t>();
129  case 2:
130  return inline_int_null_value<uint16_t>();
131  case 4:
132  return inline_int_null_value<int32_t>();
133  default:
134 #ifndef __CUDACC__
135  CHECK(false) << "Unknown size for dictionary encoded type: " << ti.get_size();
136 #else
137  CHECK(false);
138 #endif
139  }
140  }
142  CHECK(ti.is_integer() || ti.is_time() || ti.is_decimal());
143  CHECK_EQ(0, ti.get_comp_param() % 8);
144  return -(1L << (ti.get_comp_param() - 1));
145 }
146 
147 inline double inline_fp_null_val(const SQLTypeInfo& ti) {
148  CHECK(ti.is_fp());
149  const auto type = ti.get_type();
150  switch (type) {
151  case kFLOAT:
153  case kDOUBLE:
155  default:
156  abort();
157  }
158 }
159 
160 inline uint64_t exp_to_scale(const unsigned exp) {
161  uint64_t res = 1;
162  for (unsigned i = 0; i < exp; ++i) {
163  res *= 10;
164  }
165  return res;
166 }
167 
168 inline size_t get_bit_width(const SQLTypeInfo& ti) {
169  const auto int_type = ti.is_decimal() ? kBIGINT : ti.get_type();
170  switch (int_type) {
171  case kNULLT:
172  LOG(FATAL) << "Untyped NULL values are not supported. Please CAST any NULL "
173  "constants to a type.";
174  case kBOOLEAN:
175  return 8;
176  case kTINYINT:
177  return 8;
178  case kSMALLINT:
179  return 16;
180  case kINT:
181  return 32;
182  case kBIGINT:
183  return 64;
184  case kFLOAT:
185  return 32;
186  case kDOUBLE:
187  return 64;
188  case kTIME:
189  case kTIMESTAMP:
190  case kDATE:
191  case kINTERVAL_DAY_TIME:
193  return sizeof(time_t) * 8;
194  case kTEXT:
195  case kVARCHAR:
196  case kCHAR:
197  return 32;
198  case kARRAY:
199  if (ti.get_size() == -1) {
200  throw std::runtime_error("Projecting on unsized array column not supported.");
201  }
202  return ti.get_size() * 8;
203  case kPOINT:
204  case kLINESTRING:
205  case kPOLYGON:
206  case kMULTIPOLYGON:
207  return 32;
208  case kCOLUMN:
209  return ti.get_elem_type().get_size() * 8;
210  default:
211  LOG(FATAL) << "Unhandled int_type: " << int_type;
212  return {};
213  }
214 }
215 
216 inline bool is_unsigned_type(const SQLTypeInfo& ti) {
217  return ti.get_compression() == kENCODING_DICT && ti.get_size() < ti.get_logical_size();
218 }
219 
220 #endif // QUERYENGINE_SQLTYPESLAYOUT_H
int64_t inline_int_null_val(const SQLTypeInfo &ti)
void set_compact_type(TargetInfo &target, const SQLTypeInfo &new_type)
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
#define CHECK_EQ(x, y)
Definition: Logger.h:205
bool is_time() const
Definition: sqltypes.h:423
bool is_string() const
Definition: sqltypes.h:417
Definition: sqltypes.h:51
bool is_integer() const
Definition: sqltypes.h:419
SQLTypeInfo sql_type
Definition: TargetInfo.h:42
#define LOG(tag)
Definition: Logger.h:188
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:268
HOST DEVICE int get_size() const
Definition: sqltypes.h:269
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:267
bool is_decimal() const
Definition: sqltypes.h:420
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
int64_t inline_fixed_encoding_null_val(const SQLTypeInfo &ti)
int get_logical_size() const
Definition: sqltypes.h:270
const SQLTypeInfo get_compact_type(const TargetInfo &target)
size_t get_bit_width(const SQLTypeInfo &ti)
bool is_agg
Definition: TargetInfo.h:40
bool is_column() const
Definition: sqltypes.h:430
SQLAgg agg_kind
Definition: TargetInfo.h:41
Definition: sqltypes.h:54
Definition: sqltypes.h:55
constexpr float inline_fp_null_value< float >()
Definition: sqldefs.h:76
constexpr double inline_fp_null_value< double >()
Definition: sqltypes.h:43
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:624
void set_notnull(bool n)
Definition: sqltypes.h:356
#define CHECK(condition)
Definition: Logger.h:197
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
uint64_t exp_to_scale(const unsigned exp)
double inline_fp_null_val(const SQLTypeInfo &ti)
Definition: sqltypes.h:47
bool is_distinct
Definition: TargetInfo.h:45
bool is_fp() const
Definition: sqltypes.h:421
bool is_unsigned_type(const SQLTypeInfo &ti)
bool is_agg_domain_range_equivalent(const SQLAgg &agg_kind)
Definition: TargetInfo.h:64