OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlTypesLayout.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 
23 #ifndef QUERYENGINE_SQLTYPESLAYOUT_H
24 #define QUERYENGINE_SQLTYPESLAYOUT_H
25 
26 #include "Shared/TargetInfo.h"
27 
28 #include "Logger/Logger.h"
29 
30 #include <limits>
31 
32 class OverflowOrUnderflow : public std::runtime_error {
33  public:
34  OverflowOrUnderflow() : std::runtime_error("Overflow or underflow") {}
35 };
36 
37 inline const SQLTypeInfo get_compact_type(const TargetInfo& target) {
38  if (!target.is_agg) {
39  return 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  if (ti.get_compression() == kENCODING_FIXED) {
94  }
95  case kDATE:
96  case kINTERVAL_DAY_TIME:
98  return inline_int_null_value<int64_t>();
99  case kDECIMAL:
100  case kNUMERIC:
101  return inline_int_null_value<int64_t>();
102  default:
103  abort();
104  }
105 }
106 
107 inline int64_t inline_fixed_encoding_null_val(const SQLTypeInfo& ti) {
108  if (ti.get_compression() == kENCODING_NONE) {
109  return inline_int_null_val(ti);
110  }
112  switch (ti.get_comp_param()) {
113  case 0:
114  case 32:
115  return inline_int_null_value<int32_t>();
116  case 16:
117  return inline_int_null_value<int16_t>();
118  default:
119 #ifndef __CUDACC__
120  CHECK(false) << "Unknown encoding width for date in days: "
121  << ti.get_comp_param();
122 #else
123  CHECK(false);
124 #endif
125  }
126  }
127  if (ti.get_compression() == kENCODING_DICT) {
128  CHECK(ti.is_string());
129  switch (ti.get_size()) {
130  case 1:
131  return inline_int_null_value<uint8_t>();
132  case 2:
133  return inline_int_null_value<uint16_t>();
134  case 4:
135  return inline_int_null_value<int32_t>();
136  default:
137 #ifndef __CUDACC__
138  CHECK(false) << "Unknown size for dictionary encoded type: " << ti.get_size();
139 #else
140  CHECK(false);
141 #endif
142  }
143  }
145  CHECK(ti.is_integer() || ti.is_time() || ti.is_decimal());
146  CHECK_EQ(0, ti.get_comp_param() % 8);
147  return -(1LL << (ti.get_comp_param() - 1));
148 }
149 
150 inline double inline_fp_null_val(const SQLTypeInfo& ti) {
151  CHECK(ti.is_fp());
152  const auto type = ti.get_type();
153  switch (type) {
154  case kFLOAT:
156  case kDOUBLE:
158  default:
159  abort();
160  }
161 }
162 
163 inline uint64_t exp_to_scale(const unsigned exp) {
164  uint64_t res = 1;
165  for (unsigned i = 0; i < exp; ++i) {
166  res *= 10;
167  }
168  return res;
169 }
170 
171 inline size_t get_bit_width(const SQLTypeInfo& ti) {
172  const auto int_type = ti.is_decimal() ? kBIGINT : ti.get_type();
173  switch (int_type) {
174  case kNULLT:
175  throw std::runtime_error(
176  "Untyped NULL values are not supported. Please CAST any NULL "
177  "constants to a type.");
178  case kBOOLEAN:
179  return 8;
180  case kTINYINT:
181  return 8;
182  case kSMALLINT:
183  return 16;
184  case kINT:
185  return 32;
186  case kBIGINT:
187  return 64;
188  case kFLOAT:
189  return 32;
190  case kDOUBLE:
191  return 64;
192  case kTIME:
193  case kTIMESTAMP:
194  case kDATE:
195  case kINTERVAL_DAY_TIME:
197  return sizeof(time_t) * 8;
198  case kTEXT:
199  case kVARCHAR:
200  case kCHAR:
201  return 32;
202  case kARRAY:
203  if (ti.get_size() == -1) {
204  throw std::runtime_error("Projecting on unsized array column not supported.");
205  }
206  return ti.get_size() * 8;
207  case kPOINT:
208  case kMULTIPOINT:
209  case kLINESTRING:
210  case kMULTILINESTRING:
211  case kPOLYGON:
212  case kMULTIPOLYGON:
213  return 32;
214  case kCOLUMN:
215  case kCOLUMN_LIST:
216  return ti.get_elem_type().get_size() * 8;
217  default:
218  break;
219  }
220 #ifdef __CUDACC__
221  UNREACHABLE();
222 #else
223  UNREACHABLE() << "Unhandled int_type: " << int_type;
224 #endif
225  return {};
226 }
227 
228 inline bool is_unsigned_type(const SQLTypeInfo& ti) {
229  return ti.get_compression() == kENCODING_DICT && ti.get_size() < ti.get_logical_size();
230 }
231 
232 #endif // QUERYENGINE_SQLTYPESLAYOUT_H
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:230
HOST DEVICE int get_size() const
Definition: sqltypes.h:414
Definition: sqltypes.h:63
SQLTypeInfo sql_type
Definition: TargetInfo.h:52
bool is_fp() const
Definition: sqltypes.h:604
#define UNREACHABLE()
Definition: Logger.h:266
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:404
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
bool is_time() const
Definition: sqltypes.h:606
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:53
const SQLTypeInfo get_compact_type(const TargetInfo &target)
size_t get_bit_width(const SQLTypeInfo &ti)
bool is_agg
Definition: TargetInfo.h:50
int get_logical_size() const
Definition: sqltypes.h:424
bool is_integer() const
Definition: sqltypes.h:602
SQLAgg agg_kind
Definition: TargetInfo.h:51
Definition: sqltypes.h:66
Definition: sqltypes.h:67
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:412
constexpr float inline_fp_null_value< float >()
Definition: sqldefs.h:77
constexpr double inline_fp_null_value< double >()
Definition: sqltypes.h:55
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:413
void set_notnull(bool n)
Definition: sqltypes.h:521
#define CHECK(condition)
Definition: Logger.h:222
uint64_t exp_to_scale(const unsigned exp)
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)
Definition: sqltypes.h:59
bool is_string() const
Definition: sqltypes.h:600
bool is_distinct
Definition: TargetInfo.h:55
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:981
bool is_decimal() const
Definition: sqltypes.h:603
bool is_unsigned_type(const SQLTypeInfo &ti)
bool is_agg_domain_range_equivalent(const SQLAgg &agg_kind)
Definition: TargetInfo.h:79