OmniSciDB  a5dc49c757
 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 #ifdef __CUDACC__
45  CHECK((shared::is_any<kCOUNT, kCOUNT_IF, kAPPROX_QUANTILE, kMODE>(agg_type)));
46 #else
47  CHECK((shared::is_any<kCOUNT, kCOUNT_IF, kAPPROX_QUANTILE, kMODE>(agg_type)))
48  << agg_type;
49 #endif
50  CHECK(!target.is_distinct);
51  return target.sql_type;
52  }
53 
54  if (is_agg_domain_range_equivalent(agg_type)) {
55  return agg_arg;
56  } else {
57  // Nullability of the target needs to match that of the agg for proper initialization
58  // of target (aggregate) values
59  auto modified_target_type = target.sql_type;
60  modified_target_type.set_notnull(agg_arg.get_notnull());
61  return modified_target_type;
62  }
63 }
64 
65 inline void set_compact_type(TargetInfo& target, const SQLTypeInfo& new_type) {
66  if (target.is_agg) {
67  const auto agg_type = target.agg_kind;
68  auto& agg_arg = target.agg_arg_type;
69  if (!shared::is_any<kCOUNT, kCOUNT_IF>(agg_type) || agg_arg.get_type() != kNULLT) {
70  agg_arg = new_type;
71  return;
72  }
73  }
74  target.sql_type = new_type;
75 }
76 
77 // TODO: this function appears to be out of sync with
78 // inline_int_null_val in InlineNullValues.h, or vice-versa??
79 inline int64_t inline_int_null_val(const SQLTypeInfo& ti) {
80  auto type = ti.get_type();
81  if (ti.is_string()) {
83  CHECK_EQ(4, ti.get_logical_size());
84  type = kINT;
85  }
86  switch (type) {
87  case kBOOLEAN:
88  return inline_int_null_value<int8_t>();
89  case kTINYINT:
90  return inline_int_null_value<int8_t>();
91  case kSMALLINT:
92  return inline_int_null_value<int16_t>();
93  case kINT:
94  return inline_int_null_value<int32_t>();
95  case kBIGINT:
96  return inline_int_null_value<int64_t>();
97  case kTIMESTAMP:
98  case kTIME:
99  case kDATE:
100  case kINTERVAL_DAY_TIME:
102  return inline_int_null_value<int64_t>();
103  case kDECIMAL:
104  case kNUMERIC:
105  return inline_int_null_value<int64_t>();
106  default:
107  abort();
108  }
109 }
110 
111 inline int64_t inline_fixed_encoding_null_val(const SQLTypeInfo& ti) {
112  if (ti.get_compression() == kENCODING_NONE) {
113  return inline_int_null_val(ti);
114  }
116  switch (ti.get_comp_param()) {
117  case 0:
118  case 32:
119  return inline_int_null_value<int32_t>();
120  case 16:
121  return inline_int_null_value<int16_t>();
122  default:
123 #ifndef __CUDACC__
124  CHECK(false) << "Unknown encoding width for date in days: "
125  << ti.get_comp_param();
126 #else
127  CHECK(false);
128 #endif
129  }
130  }
131  if (ti.get_compression() == kENCODING_DICT) {
132  CHECK(ti.is_string());
133  switch (ti.get_size()) {
134  case 1:
135  return inline_int_null_value<uint8_t>();
136  case 2:
137  return inline_int_null_value<uint16_t>();
138  case 4:
139  return inline_int_null_value<int32_t>();
140  default:
141 #ifndef __CUDACC__
142  CHECK(false) << "Unknown size for dictionary encoded type: " << ti.get_size();
143 #else
144  CHECK(false);
145 #endif
146  }
147  }
149  CHECK(ti.is_integer() || ti.is_time() || ti.is_decimal());
150  CHECK_EQ(0, ti.get_comp_param() % 8);
151  return -(1LL << (ti.get_comp_param() - 1));
152 }
153 
154 inline double inline_fp_null_val(const SQLTypeInfo& ti) {
155  CHECK(ti.is_fp());
156  const auto type = ti.get_type();
157  switch (type) {
158  case kFLOAT:
160  case kDOUBLE:
162  default:
163  abort();
164  }
165 }
166 
167 inline uint64_t exp_to_scale(const unsigned exp) {
168  uint64_t res = 1;
169  for (unsigned i = 0; i < exp; ++i) {
170  res *= 10;
171  }
172  return res;
173 }
174 
175 inline size_t get_bit_width(const SQLTypeInfo& ti) {
176  const auto int_type = ti.is_decimal() ? kBIGINT : ti.get_type();
177  switch (int_type) {
178  case kNULLT:
179  throw std::runtime_error(
180  "Untyped NULL values are not supported. Please CAST any NULL "
181  "constants to a type.");
182  case kBOOLEAN:
183  return 8;
184  case kTINYINT:
185  return 8;
186  case kSMALLINT:
187  return 16;
188  case kINT:
189  return 32;
190  case kBIGINT:
191  return 64;
192  case kFLOAT:
193  return 32;
194  case kDOUBLE:
195  return 64;
196  case kTIME:
197  case kTIMESTAMP:
198  case kDATE:
199  case kINTERVAL_DAY_TIME:
201  return sizeof(time_t) * 8;
202  case kTEXT:
203  case kVARCHAR:
204  case kCHAR:
205  return 32;
206  case kARRAY:
207  if (ti.get_size() == -1) {
208  throw std::runtime_error("Projecting on unsized array column not supported.");
209  }
210  return ti.get_size() * 8;
211  case kPOINT:
212  case kMULTIPOINT:
213  case kLINESTRING:
214  case kMULTILINESTRING:
215  case kPOLYGON:
216  case kMULTIPOLYGON:
217  return 32;
218  case kCOLUMN:
219  case kCOLUMN_LIST:
220  return ti.get_elem_type().get_size() * 8;
221  default:
222  break;
223  }
224 #ifdef __CUDACC__
225  UNREACHABLE();
226 #else
227  UNREACHABLE() << "Unhandled int_type: " << int_type;
228 #endif
229  return {};
230 }
231 
232 inline bool is_unsigned_type(const SQLTypeInfo& ti) {
233  return ti.get_compression() == kENCODING_DICT && ti.get_size() < ti.get_logical_size();
234 }
235 
236 #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:301
HOST DEVICE int get_size() const
Definition: sqltypes.h:403
Definition: sqltypes.h:76
SQLTypeInfo sql_type
Definition: TargetInfo.h:52
bool is_fp() const
Definition: sqltypes.h:573
bool is_agg_domain_range_equivalent(const SQLAgg agg_kind)
Definition: TargetInfo.h:83
#define UNREACHABLE()
Definition: Logger.h:338
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
bool is_time() const
Definition: sqltypes.h:579
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:421
bool is_integer() const
Definition: sqltypes.h:567
SQLAgg agg_kind
Definition: TargetInfo.h:51
Definition: sqltypes.h:79
Definition: sqltypes.h:80
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
constexpr float inline_fp_null_value< float >()
constexpr double inline_fp_null_value< double >()
Definition: sqltypes.h:68
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:402
void set_notnull(bool n)
Definition: sqltypes.h:477
#define CHECK(condition)
Definition: Logger.h:291
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:72
bool is_string() const
Definition: sqltypes.h:561
bool is_distinct
Definition: TargetInfo.h:55
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:977
bool is_decimal() const
Definition: sqltypes.h:570
bool is_unsigned_type(const SQLTypeInfo &ti)