OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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.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;
39  }
40  const auto agg_type = target.agg_kind;
41  const auto& agg_arg = target.agg_arg_type;
42  if (agg_arg.get_type() == kNULLT) {
43  CHECK_EQ(kCOUNT, agg_type);
44  CHECK(!target.is_distinct);
45  return target.sql_type;
46  }
47 
48  if (is_agg_domain_range_equivalent(agg_type)) {
49  return agg_arg;
50  } else {
51  // Nullability of the target needs to match that of the agg for proper initialization
52  // of target (aggregate) values
53  auto modified_target_type = target.sql_type;
54  modified_target_type.set_notnull(agg_arg.get_notnull());
55  return modified_target_type;
56  }
57 }
58 
59 inline void set_compact_type(TargetInfo& target, const SQLTypeInfo& new_type) {
60  if (target.is_agg) {
61  const auto agg_type = target.agg_kind;
62  auto& agg_arg = target.agg_arg_type;
63  if (agg_type != kCOUNT || agg_arg.get_type() != kNULLT) {
64  agg_arg = new_type;
65  return;
66  }
67  }
68  target.sql_type = new_type;
69 }
70 
71 inline int64_t inline_int_null_val(const SQLTypeInfo& ti) {
72  auto type = ti.get_type();
73  if (ti.is_string()) {
75  CHECK_EQ(4, ti.get_logical_size());
76  type = kINT;
77  }
78  switch (type) {
79  case kBOOLEAN:
80  return inline_int_null_value<int8_t>();
81  case kTINYINT:
82  return inline_int_null_value<int8_t>();
83  case kSMALLINT:
84  return inline_int_null_value<int16_t>();
85  case kINT:
86  return inline_int_null_value<int32_t>();
87  case kBIGINT:
88  return inline_int_null_value<int64_t>();
89  case kTIMESTAMP:
90  case kTIME:
91  case kDATE:
92  case kINTERVAL_DAY_TIME:
94  return inline_int_null_value<int64_t>();
95  case kDECIMAL:
96  case kNUMERIC:
97  return inline_int_null_value<int64_t>();
98  default:
99  abort();
100  }
101 }
102 
103 inline int64_t inline_fixed_encoding_null_val(const SQLTypeInfo& ti) {
104  if (ti.get_compression() == kENCODING_NONE) {
105  return inline_int_null_val(ti);
106  }
108  switch (ti.get_comp_param()) {
109  case 0:
110  case 32:
111  return inline_int_null_value<int32_t>();
112  case 16:
113  return inline_int_null_value<int16_t>();
114  default:
115 #ifndef __CUDACC__
116  CHECK(false) << "Unknown encoding width for date in days: "
117  << ti.get_comp_param();
118 #else
119  CHECK(false);
120 #endif
121  }
122  }
123  if (ti.get_compression() == kENCODING_DICT) {
124  CHECK(ti.is_string());
125  switch (ti.get_size()) {
126  case 1:
127  return inline_int_null_value<uint8_t>();
128  case 2:
129  return inline_int_null_value<uint16_t>();
130  case 4:
131  return inline_int_null_value<int32_t>();
132  default:
133 #ifndef __CUDACC__
134  CHECK(false) << "Unknown size for dictionary encoded type: " << ti.get_size();
135 #else
136  CHECK(false);
137 #endif
138  }
139  }
141  CHECK(ti.is_integer() || ti.is_time() || ti.is_decimal());
142  CHECK_EQ(0, ti.get_comp_param() % 8);
143  return -(1L << (ti.get_comp_param() - 1));
144 }
145 
146 inline double inline_fp_null_val(const SQLTypeInfo& ti) {
147  CHECK(ti.is_fp());
148  const auto type = ti.get_type();
149  switch (type) {
150  case kFLOAT:
152  case kDOUBLE:
154  default:
155  abort();
156  }
157 }
158 
159 inline uint64_t exp_to_scale(const unsigned exp) {
160  uint64_t res = 1;
161  for (unsigned i = 0; i < exp; ++i) {
162  res *= 10;
163  }
164  return res;
165 }
166 
167 inline size_t get_bit_width(const SQLTypeInfo& ti) {
168  const auto int_type = ti.is_decimal() ? kBIGINT : ti.get_type();
169  switch (int_type) {
170  case kBOOLEAN:
171  return 8;
172  case kTINYINT:
173  return 8;
174  case kSMALLINT:
175  return 16;
176  case kINT:
177  return 32;
178  case kBIGINT:
179  return 64;
180  case kFLOAT:
181  return 32;
182  case kDOUBLE:
183  return 64;
184  case kTIME:
185  case kTIMESTAMP:
186  case kDATE:
187  case kINTERVAL_DAY_TIME:
189  return sizeof(time_t) * 8;
190  case kTEXT:
191  case kVARCHAR:
192  case kCHAR:
193  return 32;
194  case kARRAY:
195  if (ti.get_size() == -1) {
196  throw std::runtime_error("Projecting on unsized array column not supported.");
197  }
198  return ti.get_size() * 8;
199  case kPOINT:
200  case kLINESTRING:
201  case kPOLYGON:
202  case kMULTIPOLYGON:
203  return 32;
204  default:
205  abort();
206  }
207 }
208 
209 inline bool is_unsigned_type(const SQLTypeInfo& ti) {
210  return ti.get_compression() == kENCODING_DICT && ti.get_size() < ti.get_logical_size();
211 }
212 
213 #endif // QUERYENGINE_SQLTYPESLAYOUT_H
void set_compact_type(TargetInfo &target, const SQLTypeInfo &new_type)
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
bool is_fp() const
Definition: sqltypes.h:481
#define CHECK_EQ(x, y)
Definition: Logger.h:198
Definition: sqltypes.h:52
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:335
SQLTypeInfo sql_type
Definition: TargetInfo.h:42
HOST DEVICE int get_size() const
Definition: sqltypes.h:336
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
void set_notnull(bool n)
Definition: sqltypes.h:423
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
const SQLTypeInfo get_compact_type(const TargetInfo &target)
size_t get_bit_width(const SQLTypeInfo &ti)
bool is_agg
Definition: TargetInfo.h:40
CHECK(cgen_state)
bool is_time() const
Definition: sqltypes.h:483
int get_logical_size() const
Definition: sqltypes.h:337
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
SQLAgg agg_kind
Definition: TargetInfo.h:41
Definition: sqltypes.h:55
Definition: sqltypes.h:56
bool is_integer() const
Definition: sqltypes.h:479
constexpr float inline_fp_null_value< float >()
Definition: sqldefs.h:71
constexpr double inline_fp_null_value< double >()
Definition: sqltypes.h:44
bool is_string() const
Definition: sqltypes.h:477
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:48
bool is_distinct
Definition: TargetInfo.h:45
bool is_decimal() const
Definition: sqltypes.h:480
bool is_unsigned_type(const SQLTypeInfo &ti)
bool is_agg_domain_range_equivalent(const SQLAgg &agg_kind)
Definition: TargetInfo.h:52