OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InlineNullValues.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, 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 
17 #ifndef INLINENULLVALUES_H
18 #define INLINENULLVALUES_H
19 
20 #include "Logger/Logger.h"
21 
22 template <typename T>
23 constexpr inline T inline_fp_null_value() {
24  LOG(FATAL) << "Only float or double overloads should be called.";
25  return T{};
26 }
27 
28 template <>
29 constexpr inline float inline_fp_null_value<float>() {
30  return NULL_FLOAT;
31 }
32 
33 template <>
34 constexpr inline double inline_fp_null_value<double>() {
35  return NULL_DOUBLE;
36 }
37 
38 template <typename T>
40  LOG(FATAL) << "Only float or double overloads should be called.";
41  return T{};
42 }
43 
44 template <>
45 constexpr inline float inline_fp_null_array_value<float>() {
46  return NULL_ARRAY_FLOAT;
47 }
48 
49 template <>
50 constexpr inline double inline_fp_null_array_value<double>() {
51  return NULL_ARRAY_DOUBLE;
52 }
53 
54 template <typename SQL_TYPE_INFO>
55 inline int64_t inline_int_null_val(const SQL_TYPE_INFO& ti) {
56  auto type = ti.get_type();
57  if (ti.is_string()) {
58  CHECK_EQ(kENCODING_DICT, ti.get_compression());
59  CHECK_EQ(4, ti.get_logical_size());
60  type = kINT;
61  } else {
62  CHECK_EQ(kENCODING_NONE, ti.get_compression());
63  }
64  switch (type) {
65  case kBOOLEAN:
66  return inline_int_null_value<int8_t>();
67  case kTINYINT:
68  return inline_int_null_value<int8_t>();
69  case kSMALLINT:
70  return inline_int_null_value<int16_t>();
71  case kINT:
72  return inline_int_null_value<int32_t>();
73  case kBIGINT:
74  return inline_int_null_value<int64_t>();
75  case kTIMESTAMP:
76  case kTIME:
77  case kDATE:
78  case kINTERVAL_DAY_TIME:
80  return inline_int_null_value<int64_t>();
81  case kDECIMAL:
82  case kNUMERIC:
83  return inline_int_null_value<int64_t>();
84  default:
85  abort();
86  }
87 }
88 
89 template <typename SQL_TYPE_INFO>
90 inline int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO& ti) {
91  if (ti.get_compression() == kENCODING_NONE) {
92  return inline_int_null_val(ti);
93  }
94  if (ti.get_compression() == kENCODING_DATE_IN_DAYS) {
95  switch (ti.get_comp_param()) {
96  case 0:
97  case 32:
98  return inline_int_null_value<int32_t>();
99  case 16:
100  return inline_int_null_value<int16_t>();
101  default:
102 #ifndef __CUDACC__
103  CHECK(false) << "Unknown encoding width for date in days: "
104  << ti.get_comp_param();
105 #else
106  CHECK(false);
107 #endif
108  }
109  }
110  if (ti.get_compression() == kENCODING_DICT) {
111  CHECK(ti.is_string());
112  switch (ti.get_size()) {
113  case 1:
114  return inline_int_null_value<uint8_t>();
115  case 2:
116  return inline_int_null_value<uint16_t>();
117  case 4:
118  return inline_int_null_value<int32_t>();
119  default:
120 #ifndef __CUDACC__
121  CHECK(false) << "Unknown size for dictionary encoded type: " << ti.get_size();
122 #else
123  CHECK(false);
124 #endif
125  }
126  }
127  CHECK_EQ(kENCODING_FIXED, ti.get_compression());
128  CHECK(ti.is_integer() || ti.is_time() || ti.is_decimal());
129  CHECK_EQ(0, ti.get_comp_param() % 8);
130  return -(1L << (ti.get_comp_param() - 1));
131 }
132 
133 template <typename SQL_TYPE_INFO>
134 inline double inline_fp_null_val(const SQL_TYPE_INFO& ti) {
135  CHECK(ti.is_fp());
136  const auto type = ti.get_type();
137  switch (type) {
138  case kFLOAT:
140  case kDOUBLE:
142  default:
143  abort();
144  }
145 }
146 
147 // NULL_ARRAY sentinels
148 template <typename SQL_TYPE_INFO>
149 inline int64_t inline_int_null_array_val(const SQL_TYPE_INFO& ti) {
150  auto type = ti.get_type();
151  if (ti.is_string()) {
152  CHECK_EQ(kENCODING_DICT, ti.get_compression());
153  CHECK_EQ(4, ti.get_logical_size());
154  type = kINT;
155  } else {
156  CHECK_EQ(kENCODING_NONE, ti.get_compression());
157  }
158  // For all of the types below NULL sentinel is min of the range,
159  // the value right above it is the NULL_ARRAY sentinel
160  switch (type) {
161  case kBOOLEAN:
162  return inline_int_null_array_value<int8_t>();
163  case kTINYINT:
164  return inline_int_null_array_value<int8_t>();
165  case kSMALLINT:
166  return inline_int_null_array_value<int16_t>();
167  case kINT:
168  return inline_int_null_array_value<int32_t>();
169  case kBIGINT:
170  return inline_int_null_array_value<int64_t>();
171  case kTIMESTAMP:
172  case kTIME:
173  case kDATE:
174  case kINTERVAL_DAY_TIME:
176  return inline_int_null_array_value<int64_t>();
177  case kDECIMAL:
178  case kNUMERIC:
179  return inline_int_null_array_value<int64_t>();
180  default:
181  abort();
182  }
183 }
184 
185 template <typename SQL_TYPE_INFO>
186 inline int64_t inline_fixed_encoding_null_array_val(const SQL_TYPE_INFO& ti) {
187  if (ti.get_compression() == kENCODING_NONE) {
188  return inline_int_null_array_val(ti);
189  }
190  if (ti.get_compression() == kENCODING_DATE_IN_DAYS) {
191  switch (ti.get_comp_param()) {
192  case 0:
193  case 32:
194  return inline_int_null_array_value<int32_t>();
195  case 16:
196  return inline_int_null_array_value<int16_t>();
197  default:
198 #ifndef __CUDACC__
199  CHECK(false) << "Unknown encoding width for date in days: "
200  << ti.get_comp_param();
201 #else
202  CHECK(false);
203 #endif
204  }
205  }
206  if (ti.get_compression() == kENCODING_DICT) {
207  CHECK(ti.is_string());
208 #ifndef __CUDACC__
209  CHECK(false) << "Currently don't support fixed length arrays of dict encoded strings";
210 #else
211  CHECK(false);
212 #endif
213  switch (ti.get_size()) {
214  case 1:
215  return inline_int_null_array_value<uint8_t>();
216  case 2:
217  return inline_int_null_array_value<uint16_t>();
218  case 4:
219  return inline_int_null_array_value<int32_t>();
220  default:
221 #ifndef __CUDACC__
222  CHECK(false) << "Unknown size for dictionary encoded type: " << ti.get_size();
223 #else
224  CHECK(false);
225 #endif
226  }
227  }
228 #ifndef __CUDACC__
229  CHECK(false) << "Currently don't support fixed length arrays with fixed encoding";
230 #else
231  CHECK(false);
232 #endif
233  CHECK_EQ(kENCODING_FIXED, ti.get_compression());
234  CHECK(ti.is_integer() || ti.is_time() || ti.is_decimal());
235  CHECK_EQ(0, ti.get_comp_param() % 8);
236  // The value of the NULL sentinel for fixed encoding is:
237  // -(1L << (ti.get_comp_param() - 1))
238  // NULL_ARRAY sentinel would have to be the value just above NULL:
239  return -(1L << (ti.get_comp_param() - 1)) + 1;
240 }
241 
242 #endif
#define CHECK_EQ(x, y)
Definition: Logger.h:205
#define NULL_DOUBLE
Definition: sqltypes.h:257
Definition: sqltypes.h:51
constexpr T inline_fp_null_value()
#define NULL_ARRAY_DOUBLE
Definition: sqltypes.h:265
#define LOG(tag)
Definition: Logger.h:188
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
#define NULL_FLOAT
Definition: sqltypes.h:256
T inline_fp_null_array_value()
int64_t inline_int_null_array_val(const SQL_TYPE_INFO &ti)
Definition: sqltypes.h:55
constexpr double inline_fp_null_array_value< double >()
constexpr float inline_fp_null_value< float >()
constexpr double inline_fp_null_value< double >()
int64_t inline_fixed_encoding_null_array_val(const SQL_TYPE_INFO &ti)
#define CHECK(condition)
Definition: Logger.h:197
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:47
constexpr float inline_fp_null_array_value< float >()
#define NULL_ARRAY_FLOAT
Definition: sqltypes.h:264