OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #include "funcannotations.h"
22 
23 #include <cassert>
24 #include <cfloat>
25 #include <cstdint>
26 #include <cstdlib>
27 #include <limits>
28 
29 #define NULL_BOOLEAN INT8_MIN
30 #define NULL_TINYINT INT8_MIN
31 #define NULL_SMALLINT INT16_MIN
32 #define NULL_INT INT32_MIN
33 #define NULL_BIGINT INT64_MIN
34 #define NULL_FLOAT FLT_MIN
35 #define NULL_DOUBLE DBL_MIN
36 
37 #define NULL_ARRAY_BOOLEAN (INT8_MIN + 1)
38 #define NULL_ARRAY_TINYINT (INT8_MIN + 1)
39 #define NULL_ARRAY_SMALLINT (INT16_MIN + 1)
40 #define NULL_ARRAY_INT (INT32_MIN + 1)
41 #define NULL_ARRAY_BIGINT (INT64_MIN + 1)
42 #define NULL_ARRAY_FLOAT (FLT_MIN * 2.0)
43 #define NULL_ARRAY_DOUBLE (DBL_MIN * 2.0)
44 
45 #define NULL_ARRAY_COMPRESSED_32 0x80000000U
46 
47 #if !(defined(__CUDACC__) || defined(NO_BOOST))
48 #define CONSTEXPR constexpr
49 #else
50 #define CONSTEXPR
51 #endif
52 
53 template <class T>
54 constexpr inline int64_t inline_int_null_value() {
55  return std::is_signed<T>::value ? std::numeric_limits<T>::min()
56  : std::numeric_limits<T>::max();
57 }
58 
59 template <class T>
60 constexpr inline int64_t inline_int_null_array_value() {
61  return std::is_signed<T>::value ? std::numeric_limits<T>::min() + 1
62  : std::numeric_limits<T>::max() - 1;
63  // TODO: null_array values in signed types would step on max valid value
64  // in fixlen unsigned arrays, the max valid value may need to be lowered.
65 }
66 
67 template <class T>
68 constexpr inline int64_t max_valid_int_value() {
69  return std::is_signed<T>::value ? std::numeric_limits<T>::max()
70  : std::numeric_limits<T>::max() - 1;
71 }
72 
73 template <typename T>
74 constexpr inline T inline_fp_null_value() {
75 #if !(defined(__CUDACC__) || defined(NO_BOOST))
76  LOG(FATAL) << "Only float or double overloads should be called.";
77 #else
78  LOG(FATAL);
79 #endif
80  return T{};
81 }
82 
83 template <>
84 constexpr inline float inline_fp_null_value<float>() {
85  return NULL_FLOAT;
86 }
87 
88 template <>
89 constexpr inline double inline_fp_null_value<double>() {
90  return NULL_DOUBLE;
91 }
92 
93 template <typename T>
95 #if !(defined(__CUDACC__) || defined(NO_BOOST))
96  LOG(FATAL) << "Only float or double overloads should be called.";
97 #else
98  assert(false);
99 #endif
100  return T{};
101 }
102 
103 template <>
105  return NULL_ARRAY_FLOAT;
106 }
107 
108 template <>
110  return NULL_ARRAY_DOUBLE;
111 }
112 
113 #ifndef NO_BOOST
114 template <typename SQL_TYPE_INFO>
115 inline int64_t inline_int_null_val(const SQL_TYPE_INFO& ti) {
116  auto type = ti.get_type();
117  if (ti.is_string()) {
118  CHECK_EQ(kENCODING_DICT, ti.get_compression());
119  CHECK_EQ(4, ti.get_logical_size());
120  type = kINT;
121  } else {
122  CHECK_EQ(kENCODING_NONE, ti.get_compression());
123  }
124  switch (type) {
125  case kBOOLEAN:
126  return inline_int_null_value<int8_t>();
127  case kTINYINT:
128  return inline_int_null_value<int8_t>();
129  case kSMALLINT:
130  return inline_int_null_value<int16_t>();
131  case kINT:
132  return inline_int_null_value<int32_t>();
133  case kBIGINT:
134  return inline_int_null_value<int64_t>();
135  case kTIMESTAMP:
136  case kTIME:
137  case kDATE:
138  case kINTERVAL_DAY_TIME:
140  return inline_int_null_value<int64_t>();
141  case kDECIMAL:
142  case kNUMERIC:
143  return inline_int_null_value<int64_t>();
144  default:
145  abort();
146  }
147 }
148 
149 template <typename SQL_TYPE_INFO>
150 inline int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO& ti) {
151  if (ti.get_compression() == kENCODING_NONE) {
152  return inline_int_null_val(ti);
153  }
154  if (ti.get_compression() == kENCODING_DATE_IN_DAYS) {
155  switch (ti.get_comp_param()) {
156  case 0:
157  case 32:
158  return inline_int_null_value<int32_t>();
159  case 16:
160  return inline_int_null_value<int16_t>();
161  default:
162 #ifndef __CUDACC__
163  CHECK(false) << "Unknown encoding width for date in days: "
164  << ti.get_comp_param();
165 #else
166  CHECK(false);
167 #endif
168  }
169  }
170  if (ti.get_compression() == kENCODING_DICT) {
171  CHECK(ti.is_string());
172  switch (ti.get_size()) {
173  case 1:
174  return inline_int_null_value<uint8_t>();
175  case 2:
176  return inline_int_null_value<uint16_t>();
177  case 4:
178  return inline_int_null_value<int32_t>();
179  default:
180 #ifndef __CUDACC__
181  CHECK(false) << "Unknown size for dictionary encoded type: " << ti.get_size();
182 #else
183  CHECK(false);
184 #endif
185  }
186  }
187  CHECK_EQ(kENCODING_FIXED, ti.get_compression());
188  CHECK(ti.is_integer() || ti.is_time() || ti.is_decimal());
189  CHECK_EQ(0, ti.get_comp_param() % 8);
190  return -(1LL << (ti.get_comp_param() - 1));
191 }
192 
193 template <typename SQL_TYPE_INFO>
194 inline double inline_fp_null_val(const SQL_TYPE_INFO& ti) {
195  CHECK(ti.is_fp());
196  const auto type = ti.get_type();
197  switch (type) {
198  case kFLOAT:
200  case kDOUBLE:
202  default:
203  abort();
204  }
205 }
206 
207 // NULL_ARRAY sentinels
208 template <typename SQL_TYPE_INFO>
209 inline int64_t inline_int_null_array_val(const SQL_TYPE_INFO& ti) {
210  auto type = ti.get_type();
211  if (ti.is_string()) {
212  CHECK_EQ(kENCODING_DICT, ti.get_compression());
213  CHECK_EQ(4, ti.get_logical_size());
214  type = kINT;
215  } else {
216  CHECK_EQ(kENCODING_NONE, ti.get_compression());
217  }
218  // For all of the types below NULL sentinel is min of the range,
219  // the value right above it is the NULL_ARRAY sentinel
220  switch (type) {
221  case kBOOLEAN:
222  return inline_int_null_array_value<int8_t>();
223  case kTINYINT:
224  return inline_int_null_array_value<int8_t>();
225  case kSMALLINT:
226  return inline_int_null_array_value<int16_t>();
227  case kINT:
228  return inline_int_null_array_value<int32_t>();
229  case kBIGINT:
230  return inline_int_null_array_value<int64_t>();
231  case kTIMESTAMP:
232  case kTIME:
233  case kDATE:
234  case kINTERVAL_DAY_TIME:
236  return inline_int_null_array_value<int64_t>();
237  case kDECIMAL:
238  case kNUMERIC:
239  return inline_int_null_array_value<int64_t>();
240  default:
241  abort();
242  }
243 }
244 
245 template <typename SQL_TYPE_INFO>
246 inline int64_t inline_fixed_encoding_null_array_val(const SQL_TYPE_INFO& ti) {
247  if (ti.get_compression() == kENCODING_NONE) {
248  return inline_int_null_array_val(ti);
249  }
250  if (ti.get_compression() == kENCODING_DATE_IN_DAYS) {
251  switch (ti.get_comp_param()) {
252  case 0:
253  case 32:
254  return inline_int_null_array_value<int32_t>();
255  case 16:
256  return inline_int_null_array_value<int16_t>();
257  default:
258 #ifndef __CUDACC__
259  CHECK(false) << "Unknown encoding width for date in days: "
260  << ti.get_comp_param();
261 #else
262  CHECK(false);
263 #endif
264  }
265  }
266  if (ti.get_compression() == kENCODING_DICT) {
267  CHECK(ti.is_string());
268 #ifndef __CUDACC__
269  CHECK(false) << "Currently don't support fixed length arrays of dict encoded strings";
270 #else
271  CHECK(false);
272 #endif
273  switch (ti.get_size()) {
274  case 1:
275  return inline_int_null_array_value<uint8_t>();
276  case 2:
277  return inline_int_null_array_value<uint16_t>();
278  case 4:
279  return inline_int_null_array_value<int32_t>();
280  default:
281 #ifndef __CUDACC__
282  CHECK(false) << "Unknown size for dictionary encoded type: " << ti.get_size();
283 #else
284  CHECK(false);
285 #endif
286  }
287  }
288 #ifndef __CUDACC__
289  CHECK(false) << "Currently don't support fixed length arrays with fixed encoding";
290 #else
291  CHECK(false);
292 #endif
293  CHECK_EQ(kENCODING_FIXED, ti.get_compression());
294  CHECK(ti.is_integer() || ti.is_time() || ti.is_decimal());
295  CHECK_EQ(0, ti.get_comp_param() % 8);
296  // The value of the NULL sentinel for fixed encoding is:
297  // -(1LL << (ti.get_comp_param() - 1))
298  // NULL_ARRAY sentinel would have to be the value just above NULL:
299  return -(1LL << (ti.get_comp_param() - 1)) + 1;
300 }
301 
302 #endif // NO_BOOST
303 
304 #include <type_traits>
305 
306 namespace serialize_detail {
307 template <int overload>
308 struct IntType;
309 template <>
310 struct IntType<1> {
311  using type = uint8_t;
312 };
313 template <>
314 struct IntType<2> {
315  using type = uint16_t;
316 };
317 template <>
318 struct IntType<4> {
319  using type = uint32_t;
320 };
321 template <>
322 struct IntType<8> {
323  using type = uint64_t;
324 };
325 } // namespace serialize_detail
326 
327 template <typename T, bool array = false>
330  using TT = typename serialize_detail::IntType<sizeof(T)>::type;
331  T nv = 0;
332  if
333  CONSTEXPR(std::is_floating_point<T>::value) {
334  if
335  CONSTEXPR(array) { nv = inline_fp_null_array_value<T>(); }
336  else {
337  nv = inline_fp_null_value<T>();
338  }
339  }
340  else if
341  CONSTEXPR(std::is_integral<T>::value) {
342  if
343  CONSTEXPR(array) { nv = inline_int_null_array_value<T>(); }
344  else {
345  nv = inline_int_null_value<T>();
346  }
347  }
348 #if !(defined(__CUDACC__) || defined(NO_BOOST))
349  else {
350  CHECK(false) << "Serializing null values of floating point or integral types only is "
351  "supported.";
352  }
353 #endif
354  return *(TT*)(&nv);
355 }
356 
357 template <typename T, bool array = false>
358 CONSTEXPR DEVICE inline bool is_null(const T& value) {
359  using TT = typename serialize_detail::IntType<sizeof(T)>::type;
360  return serialized_null_value<T, array>() == *(TT*)(&value);
361 }
362 
363 template <typename T, bool array = false>
364 CONSTEXPR DEVICE inline void set_null(T& value) {
365  using TT = typename serialize_detail::IntType<sizeof(T)>::type;
366  *(TT*)(&value) = serialized_null_value<T, array>();
367 }
368 
369 #endif
#define CHECK_EQ(x, y)
Definition: Logger.h:217
#define NULL_DOUBLE
Definition: sqltypes.h:49
constexpr T inline_fp_null_value()
CONSTEXPR DEVICE serialize_detail::IntType< sizeof(T)>::type serialized_null_value()
#define NULL_FLOAT
#define LOG(tag)
Definition: Logger.h:203
DEVICE T inline_fp_null_array_value()
DEVICE float inline_fp_null_array_value< float >()
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
#define DEVICE
CONSTEXPR DEVICE bool is_null(const T &value)
#define CONSTEXPR
DEVICE double inline_fp_null_array_value< double >()
CONSTEXPR DEVICE void set_null(T &value)
#define NULL_ARRAY_FLOAT
int64_t inline_int_null_array_val(const SQL_TYPE_INFO &ti)
Definition: sqltypes.h:53
constexpr int64_t inline_int_null_value()
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 NULL_ARRAY_DOUBLE
#define CHECK(condition)
Definition: Logger.h:209
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:45
constexpr int64_t inline_int_null_array_value()
constexpr int64_t max_valid_int_value()