OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sqltypes.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 
24 #ifndef SQLTYPES_H
25 #define SQLTYPES_H
26 
27 #include "ConfigResolve.h"
28 #include "StringTransform.h"
29 
30 #include <cassert>
31 #include <cfloat>
32 #include <cstdint>
33 #include <ctime>
34 #include <limits>
35 #include <memory>
36 #include <string>
37 #include <type_traits>
38 #include <vector>
39 
40 // must not change because these values persist in catalogs.
41 enum SQLTypes {
42  kNULLT = 0, // type for null values
43  kBOOLEAN = 1,
44  kCHAR = 2,
45  kVARCHAR = 3,
46  kNUMERIC = 4,
47  kDECIMAL = 5,
48  kINT = 6,
49  kSMALLINT = 7,
50  kFLOAT = 8,
51  kDOUBLE = 9,
52  kTIME = 10,
53  kTIMESTAMP = 11,
54  kBIGINT = 12,
55  kTEXT = 13,
56  kDATE = 14,
57  kARRAY = 15,
60  kPOINT = 18,
62  kPOLYGON = 20,
64  kTINYINT = 22,
65  kGEOMETRY = 23,
66  kGEOGRAPHY = 24,
67  kEVAL_CONTEXT_TYPE = 25, // Placeholder Type for ANY
68  kVOID = 26,
69  kCURSOR = 27,
71 };
72 
73 struct VarlenDatum {
74  size_t length;
75  int8_t* pointer;
76  bool is_null;
77 
79  DEVICE virtual ~VarlenDatum() {}
80 
81  VarlenDatum(const size_t l, int8_t* p, const bool n)
82  : length(l), pointer(p), is_null(n) {}
83 };
84 
86  void operator()(int8_t*) {}
87 };
88 struct FreeDeleter {
89  void operator()(int8_t* p) { free(p); }
90 };
91 
92 struct HostArrayDatum : public VarlenDatum {
93  using ManagedPtr = std::shared_ptr<int8_t>;
94 
95  HostArrayDatum() = default;
96 
97  HostArrayDatum(size_t const l, ManagedPtr p, bool const n)
98  : VarlenDatum(l, p.get(), n), data_ptr(p) {}
99 
100  HostArrayDatum(size_t const l, int8_t* p, bool const n)
101  : VarlenDatum(l, p, n), data_ptr(p, FreeDeleter()){};
102 
103  template <typename CUSTOM_DELETER,
104  typename = std::enable_if_t<
105  std::is_void<std::result_of_t<CUSTOM_DELETER(int8_t*)> >::value> >
106  HostArrayDatum(size_t const l, int8_t* p, CUSTOM_DELETER custom_deleter)
107  : VarlenDatum(l, p, 0 == l), data_ptr(p, custom_deleter) {}
108 
109  template <typename CUSTOM_DELETER,
110  typename = std::enable_if_t<
111  std::is_void<std::result_of_t<CUSTOM_DELETER(int8_t*)> >::value> >
112  HostArrayDatum(size_t const l, int8_t* p, bool const n, CUSTOM_DELETER custom_deleter)
113  : VarlenDatum(l, p, n), data_ptr(p, custom_deleter) {}
114 
116 };
117 
118 struct DeviceArrayDatum : public VarlenDatum {
120 };
121 
122 using ArrayDatum = std::conditional_t<isCudaCC(), DeviceArrayDatum, HostArrayDatum>;
123 
124 typedef union {
125  bool boolval;
126  int8_t tinyintval;
127  int16_t smallintval;
128  int32_t intval;
129  int64_t bigintval;
130  float floatval;
131  double doubleval;
133 #ifndef __CUDACC__
134  std::string* stringval; // string value
135 #endif
136 } Datum;
137 
138 #ifndef __CUDACC__
140  int8_t* numbersPtr;
141  std::vector<std::string>* stringsPtr;
142  std::vector<ArrayDatum>* arraysPtr;
143 };
144 #endif
145 
146 // must not change because these values persist in catalogs.
148  kENCODING_NONE = 0, // no encoding
149  kENCODING_FIXED = 1, // Fixed-bit encoding
150  kENCODING_RL = 2, // Run Length encoding
151  kENCODING_DIFF = 3, // Differential encoding
152  kENCODING_DICT = 4, // Dictionary encoding
153  kENCODING_SPARSE = 5, // Null encoding for sparse columns
154  kENCODING_GEOINT = 6, // Encoding coordinates as intergers
155  kENCODING_DATE_IN_DAYS = 7, // Date encoding in days
157 };
158 
159 #include "SQLTypeUtilities.h"
160 
161 #define IS_INTEGER(T) \
162  (((T) == kINT) || ((T) == kSMALLINT) || ((T) == kBIGINT) || ((T) == kTINYINT))
163 #define IS_NUMBER(T) \
164  (((T) == kINT) || ((T) == kSMALLINT) || ((T) == kDOUBLE) || ((T) == kFLOAT) || \
165  ((T) == kBIGINT) || ((T) == kNUMERIC) || ((T) == kDECIMAL) || ((T) == kTINYINT))
166 #define IS_STRING(T) (((T) == kTEXT) || ((T) == kVARCHAR) || ((T) == kCHAR))
167 #define IS_GEO(T) \
168  (((T) == kPOINT) || ((T) == kLINESTRING) || ((T) == kPOLYGON) || ((T) == kMULTIPOLYGON))
169 #define IS_INTERVAL(T) ((T) == kINTERVAL_DAY_TIME || (T) == kINTERVAL_YEAR_MONTH)
170 #define IS_DECIMAL(T) ((T) == kNUMERIC || (T) == kDECIMAL)
171 #define IS_GEO_POLY(T) (((T) == kPOLYGON) || ((T) == kMULTIPOLYGON))
172 
173 #define NULL_BOOLEAN INT8_MIN
174 #define NULL_TINYINT INT8_MIN
175 #define NULL_SMALLINT INT16_MIN
176 #define NULL_INT INT32_MIN
177 #define NULL_BIGINT INT64_MIN
178 #define NULL_FLOAT FLT_MIN
179 #define NULL_DOUBLE DBL_MIN
180 
181 #define NULL_ARRAY_BOOLEAN (INT8_MIN + 1)
182 #define NULL_ARRAY_TINYINT (INT8_MIN + 1)
183 #define NULL_ARRAY_SMALLINT (INT16_MIN + 1)
184 #define NULL_ARRAY_INT (INT32_MIN + 1)
185 #define NULL_ARRAY_BIGINT (INT64_MIN + 1)
186 #define NULL_ARRAY_FLOAT (FLT_MIN * 2.0)
187 #define NULL_ARRAY_DOUBLE (DBL_MIN * 2.0)
188 
189 #define TRANSIENT_DICT_ID 0
190 #define TRANSIENT_DICT(ID) (-(ID))
191 #define REGULAR_DICT(TRANSIENTID) (-(TRANSIENTID))
192 
193 template <typename T>
194 constexpr auto is_datetime(T sql_type) {
195  return sql_type == kTIME || sql_type == kTIMESTAMP || sql_type == kDATE;
196 }
197 
198 template <typename CORE_TYPE>
200  public:
202 
204 
206  bool isChunkIteratorPackaging() const { return packaging_type_ == Chunk; }
209 
210  private:
212 };
213 
214 template <typename CORE_TYPE>
216  public:
217  inline int get_array_context_logical_size() const {
218  CORE_TYPE const* derived(static_cast<CORE_TYPE const*>(this));
219  if (is_member_of_typeset<kCHAR, kTEXT, kVARCHAR>(*derived)) {
220  auto comp_type(derived->get_compression());
221  if (comp_type == kENCODING_DICT || comp_type == kENCODING_FIXED ||
222  comp_type == kENCODING_NONE) {
223  return sizeof(int32_t);
224  }
225  }
226  return derived->get_logical_size();
227  }
228 };
229 
230 template <typename CORE_TYPE>
232  public:
233  constexpr auto is_date_in_days() const {
234  CORE_TYPE const* derived(static_cast<CORE_TYPE const*>(this));
235  if (is_member_of_typeset<kDATE>(*derived)) {
236  auto comp_type(derived->get_compression());
237  if (comp_type == kENCODING_DATE_IN_DAYS) {
238  return true;
239  }
240  }
241  return false;
242  }
243 
244  constexpr auto is_date() const {
245  CORE_TYPE const* derived(static_cast<CORE_TYPE const*>(this));
246  if (is_member_of_typeset<kDATE>(*derived)) {
247  return true;
248  }
249  return false;
250  }
251 
252  constexpr auto is_high_precision_timestamp() const {
253  CORE_TYPE const* derived(static_cast<CORE_TYPE const*>(this));
254  if (is_member_of_typeset<kTIMESTAMP>(*derived)) {
255  auto dimension(derived->get_dimension());
256  if (dimension > 0) {
257  return true;
258  }
259  }
260  return false;
261  }
262 
263  constexpr auto is_timestamp() const {
264  CORE_TYPE const* derived(static_cast<CORE_TYPE const*>(this));
265  if (is_member_of_typeset<kTIMESTAMP>(*derived)) {
266  return true;
267  }
268  return false;
269  }
270 };
271 
272 // @type SQLTypeInfo
273 // @brief a structure to capture all type information including
274 // length, precision, scale, etc.
275 template <template <class> class... TYPE_FACET_PACK>
276 class SQLTypeInfoCore : public TYPE_FACET_PACK<SQLTypeInfoCore<TYPE_FACET_PACK...> >... {
277  public:
278  SQLTypeInfoCore(SQLTypes t, int d, int s, bool n, EncodingType c, int p, SQLTypes st)
279  : type(t)
280  , subtype(st)
281  , dimension(d)
282  , scale(s)
283  , notnull(n)
284  , compression(c)
285  , comp_param(p)
286  , size(get_storage_size()) {}
287  SQLTypeInfoCore(SQLTypes t, int d, int s, bool n)
288  : type(t)
289  , subtype(kNULLT)
290  , dimension(d)
291  , scale(s)
292  , notnull(n)
294  , comp_param(0)
295  , size(get_storage_size()) {}
296  SQLTypeInfoCore(SQLTypes t, int d, int s) : SQLTypeInfoCore(t, d, s, false) {}
298  : type(t)
299  , subtype(kNULLT)
300  , dimension(0)
301  , scale(0)
302  , notnull(n)
304  , comp_param(0)
305  , size(get_storage_size()) {}
308  : type(t)
309  , subtype(kNULLT)
310  , dimension(0)
311  , scale(0)
312  , notnull(n)
313  , compression(c)
314  , comp_param(0)
315  , size(get_storage_size()) {}
317  : type(kNULLT)
318  , subtype(kNULLT)
319  , dimension(0)
320  , scale(0)
321  , notnull(false)
323  , comp_param(0)
324  , size(0) {}
325 
326  HOST DEVICE inline SQLTypes get_type() const { return type; }
327  HOST DEVICE inline SQLTypes get_subtype() const { return subtype; }
328  HOST DEVICE inline int get_dimension() const { return dimension; }
329  inline int get_precision() const { return dimension; }
330  HOST DEVICE inline int get_input_srid() const { return dimension; }
331  HOST DEVICE inline int get_scale() const { return scale; }
332  HOST DEVICE inline int get_output_srid() const { return scale; }
333  HOST DEVICE inline bool get_notnull() const { return notnull; }
335  HOST DEVICE inline int get_comp_param() const { return comp_param; }
336  HOST DEVICE inline int get_size() const { return size; }
337  inline int get_logical_size() const {
340  return ti.get_size();
341  }
342  if (compression == kENCODING_DICT) {
343  return 4;
344  }
345  return get_size();
346  }
347  inline int get_physical_cols() const {
348  switch (type) {
349  case kPOINT:
350  return 1; // coords
351  case kLINESTRING:
352  return 2; // coords, bounds
353  case kPOLYGON:
354  return 4; // coords, ring_sizes, bounds, render_group
355  case kMULTIPOLYGON:
356  return 5; // coords, ring_sizes, poly_rings, bounds, render_group
357  default:
358  break;
359  }
360  return 0;
361  }
362  inline int get_physical_coord_cols() const {
363  // @TODO dmitri/simon rename this function?
364  // It needs to return the number of extra columns
365  // which need to go through the executor, as opposed
366  // to those which are only needed by CPU for poly
367  // cache building or what-not. For now, we just omit
368  // the Render Group column. If we add Bounding Box
369  // or something this may require rethinking. Perhaps
370  // these two functions need to return an array of
371  // offsets rather than just a number to loop over,
372  // so that executor and non-executor columns can
373  // be mixed.
374  // NOTE(adb): In binding to extension functions, we need to know some pretty specific
375  // type info about each of the physical coords cols for each geo type. I added checks
376  // there to ensure the physical coords col for the geo type match what we expect. If
377  // these values are ever changed, corresponding values in
378  // ExtensionFunctionsBinding.cpp::compute_narrowing_conv_scores and
379  // ExtensionFunctionsBinding.cpp::compute_widening_conv_scores will also need to be
380  // changed.
381  switch (type) {
382  case kPOINT:
383  return 1;
384  case kLINESTRING:
385  return 1; // omit bounds
386  case kPOLYGON:
387  return 2; // omit bounds, render group
388  case kMULTIPOLYGON:
389  return 3; // omit bounds, render group
390  default:
391  break;
392  }
393  return 0;
394  }
395  inline bool has_bounds() const {
396  switch (type) {
397  case kLINESTRING:
398  case kPOLYGON:
399  case kMULTIPOLYGON:
400  return true;
401  default:
402  break;
403  }
404  return false;
405  }
406  inline bool has_render_group() const {
407  switch (type) {
408  case kPOLYGON:
409  case kMULTIPOLYGON:
410  return true;
411  default:
412  break;
413  }
414  return false;
415  }
416  HOST DEVICE inline void set_type(SQLTypes t) { type = t; }
417  HOST DEVICE inline void set_subtype(SQLTypes st) { subtype = st; }
418  inline void set_dimension(int d) { dimension = d; }
419  inline void set_precision(int d) { dimension = d; }
420  inline void set_input_srid(int d) { dimension = d; }
421  inline void set_scale(int s) { scale = s; }
422  inline void set_output_srid(int s) { scale = s; }
423  inline void set_notnull(bool n) { notnull = n; }
424  inline void set_size(int s) { size = s; }
425  inline void set_fixed_size() { size = get_storage_size(); }
426  inline void set_compression(EncodingType c) { compression = c; }
427  inline void set_comp_param(int p) { comp_param = p; }
428 #ifndef __CUDACC__
429  inline std::string get_type_name() const {
430  if (IS_GEO(type)) {
431  std::string srid_string = "";
432  if (get_output_srid() > 0) {
433  srid_string = ", " + std::to_string(get_output_srid());
434  }
435  CHECK_LT(static_cast<int>(subtype), kSQLTYPE_LAST);
436  return type_name[static_cast<int>(subtype)] + "(" +
437  type_name[static_cast<int>(type)] + srid_string + ")";
438  }
439  std::string ps = "";
440  if (type == kDECIMAL || type == kNUMERIC || subtype == kDECIMAL ||
441  subtype == kNUMERIC) {
442  ps = "(" + std::to_string(dimension) + "," + std::to_string(scale) + ")";
443  } else if (type == kTIMESTAMP) {
444  ps = "(" + std::to_string(dimension) + ")";
445  }
446  if (type == kARRAY) {
447  auto elem_ti = get_elem_type();
448  auto num_elems = (size > 0) ? std::to_string(size / elem_ti.get_size()) : "";
449  CHECK_LT(static_cast<int>(subtype), kSQLTYPE_LAST);
450  return type_name[static_cast<int>(subtype)] + ps + "[" + num_elems + "]";
451  }
452  return type_name[static_cast<int>(type)] + ps;
453  }
454  inline std::string get_compression_name() const { return comp_name[(int)compression]; }
455  inline std::string to_string() const {
456  return concat("(",
457  type_name[static_cast<int>(type)],
458  ", ",
459  get_dimension(),
460  ", ",
461  get_scale(),
462  ", ",
463  get_notnull() ? "not nullable" : "nullable",
464  ", ",
466  ", ",
467  get_comp_param(),
468  ", ",
469  type_name[static_cast<int>(subtype)],
470  ": ",
471  get_size(),
472  ": ",
474  ")");
475  }
476 #endif
477  inline bool is_string() const { return IS_STRING(type); }
478  inline bool is_string_array() const { return (type == kARRAY) && IS_STRING(subtype); }
479  inline bool is_integer() const { return IS_INTEGER(type); }
480  inline bool is_decimal() const { return type == kDECIMAL || type == kNUMERIC; }
481  inline bool is_fp() const { return type == kFLOAT || type == kDOUBLE; }
482  inline bool is_number() const { return IS_NUMBER(type); }
483  inline bool is_time() const { return is_datetime(type); }
484  inline bool is_boolean() const { return type == kBOOLEAN; }
485  inline bool is_array() const { return type == kARRAY; }
486  inline bool is_varlen_array() const { return type == kARRAY && size <= 0; }
487  inline bool is_fixlen_array() const { return type == kARRAY && size > 0; }
488  inline bool is_timeinterval() const { return IS_INTERVAL(type); }
489  inline bool is_geometry() const { return IS_GEO(type); }
490 
491  inline bool is_varlen() const { // TODO: logically this should ignore fixlen arrays
492  return (IS_STRING(type) && compression != kENCODING_DICT) || type == kARRAY ||
493  IS_GEO(type);
494  }
495 
496  // need this here till is_varlen can be fixed w/o negative impact to existing code
497  inline bool is_varlen_indeed() const {
498  // SQLTypeInfo.is_varlen() is broken with fixedlen array now
499  // and seems left broken for some concern, so fix it locally
500  return is_varlen() && !is_fixlen_array();
501  }
502 
503  inline bool is_dict_encoded_string() const {
504  return is_string() && compression == kENCODING_DICT;
505  }
506 
507  HOST DEVICE inline bool operator!=(const SQLTypeInfoCore& rhs) const {
508  return type != rhs.get_type() || subtype != rhs.get_subtype() ||
509  dimension != rhs.get_dimension() || scale != rhs.get_scale() ||
510  compression != rhs.get_compression() ||
513  notnull != rhs.get_notnull();
514  }
515  HOST DEVICE inline bool operator==(const SQLTypeInfoCore& rhs) const {
516  return type == rhs.get_type() && subtype == rhs.get_subtype() &&
517  dimension == rhs.get_dimension() && scale == rhs.get_scale() &&
518  compression == rhs.get_compression() &&
521  notnull == rhs.get_notnull();
522  }
523 
524  // FIX-ME: Work through variadic base classes
526  type = rhs.get_type();
527  subtype = rhs.get_subtype();
528  dimension = rhs.get_dimension();
529  scale = rhs.get_scale();
530  notnull = rhs.get_notnull();
532  comp_param = rhs.get_comp_param();
533  size = rhs.get_size();
534  return (*this);
535  }
536 
537  inline bool is_castable(const SQLTypeInfoCore& new_type_info) const {
538  // can always cast between the same type but different precision/scale/encodings
539  if (type == new_type_info.get_type()) {
540  return true;
541  // can always cast from or to string
542  } else if (is_string() || new_type_info.is_string()) {
543  return true;
544  // can cast between numbers
545  } else if (is_number() && new_type_info.is_number()) {
546  return true;
547  // can cast from timestamp or date to number (epoch)
548  } else if ((type == kTIMESTAMP || type == kDATE) && new_type_info.is_number()) {
549  return true;
550  // can cast from date to timestamp
551  } else if (type == kDATE && new_type_info.get_type() == kTIMESTAMP) {
552  return true;
553  } else if (type == kTIMESTAMP && new_type_info.get_type() == kDATE) {
554  return true;
555  } else if (type == kBOOLEAN && new_type_info.is_number()) {
556  return true;
557  } else if (type == kARRAY && new_type_info.get_type() == kARRAY) {
558  return get_elem_type().is_castable(new_type_info.get_elem_type());
559  } else {
560  return false;
561  }
562  }
563 
564  HOST DEVICE inline bool is_null(const Datum& d) const {
565  // assuming Datum is always uncompressed
566  switch (type) {
567  case kBOOLEAN:
568  return (int8_t)d.boolval == NULL_BOOLEAN;
569  case kTINYINT:
570  return d.tinyintval == NULL_TINYINT;
571  case kSMALLINT:
572  return d.smallintval == NULL_SMALLINT;
573  case kINT:
574  return d.intval == NULL_INT;
575  case kBIGINT:
576  case kNUMERIC:
577  case kDECIMAL:
578  return d.bigintval == NULL_BIGINT;
579  case kFLOAT:
580  return d.floatval == NULL_FLOAT;
581  case kDOUBLE:
582  return d.doubleval == NULL_DOUBLE;
583  case kTIME:
584  case kTIMESTAMP:
585  case kDATE:
586  return d.bigintval == NULL_BIGINT;
587  case kTEXT:
588  case kVARCHAR:
589  case kCHAR:
590  // @TODO handle null strings
591  break;
592  case kNULLT:
593  return true;
594  case kARRAY:
595  return d.arrayval == NULL || d.arrayval->is_null;
596  default:
597  break;
598  }
599  return false;
600  }
601  HOST DEVICE inline bool is_null(const int8_t* val) const {
602  if (type == kFLOAT) {
603  return *(float*)val == NULL_FLOAT;
604  }
605  if (type == kDOUBLE) {
606  return *(double*)val == NULL_DOUBLE;
607  }
608  // val can be either compressed or uncompressed
609  switch (size) {
610  case 1:
611  return *val == NULL_TINYINT;
612  case 2:
613  return *(int16_t*)val == NULL_SMALLINT;
614  case 4:
615  return *(int32_t*)val == NULL_INT;
616  case 8:
617  return *(int64_t*)val == NULL_BIGINT;
618  case kNULLT:
619  return true;
620  default:
621  // @TODO(wei) handle null strings
622  break;
623  }
624  return false;
625  }
626  HOST DEVICE inline bool is_null_fixlen_array(const int8_t* val, int array_size) const {
627  // Check if fixed length array has a NULL_ARRAY sentinel as the first element
628  if (type == kARRAY && val && array_size > 0 && array_size == size) {
629  // Need to create element type to get the size, but can't call get_elem_type()
630  // since this is a HOST DEVICE function. Going through copy constructor instead.
631  auto elem_ti{*this};
632  elem_ti.set_type(subtype);
633  elem_ti.set_subtype(kNULLT);
634  auto elem_size = elem_ti.get_storage_size();
635  if (elem_size < 1) {
636  return false;
637  }
638  if (subtype == kFLOAT) {
639  return *(float*)val == NULL_ARRAY_FLOAT;
640  }
641  if (subtype == kDOUBLE) {
642  return *(double*)val == NULL_ARRAY_DOUBLE;
643  }
644  switch (elem_size) {
645  case 1:
646  return *val == NULL_ARRAY_TINYINT;
647  case 2:
648  return *(int16_t*)val == NULL_ARRAY_SMALLINT;
649  case 4:
650  return *(int32_t*)val == NULL_ARRAY_INT;
651  case 8:
652  return *(int64_t*)val == NULL_ARRAY_BIGINT;
653  default:
654  return false;
655  }
656  }
657  return false;
658  }
660  return SQLTypeInfoCore(
662  }
664  return SQLTypeInfoCore(
666  }
667 
668  private:
669  SQLTypes type; // type id
670  SQLTypes subtype; // element type of arrays
671  int dimension; // VARCHAR/CHAR length or NUMERIC/DECIMAL precision
672  int scale; // NUMERIC/DECIMAL scale
673  bool notnull; // nullable? a hint, not used for type checking
674  EncodingType compression; // compression scheme
675  int comp_param; // compression parameter when applicable for certain schemes
676  int size; // size of the type in bytes. -1 for variable size
677 #ifndef __CUDACC__
678  static std::string type_name[kSQLTYPE_LAST];
679  static std::string comp_name[kENCODING_LAST];
680 #endif
681  HOST DEVICE inline int get_storage_size() const {
682  switch (type) {
683  case kBOOLEAN:
684  return sizeof(int8_t);
685  case kTINYINT:
686  return sizeof(int8_t);
687  case kSMALLINT:
688  switch (compression) {
689  case kENCODING_NONE:
690  return sizeof(int16_t);
691  case kENCODING_FIXED:
692  case kENCODING_SPARSE:
693  return comp_param / 8;
694  case kENCODING_RL:
695  case kENCODING_DIFF:
696  break;
697  default:
698  assert(false);
699  }
700  break;
701  case kINT:
702  switch (compression) {
703  case kENCODING_NONE:
704  return sizeof(int32_t);
705  case kENCODING_FIXED:
706  case kENCODING_SPARSE:
707  return comp_param / 8;
708  case kENCODING_RL:
709  case kENCODING_DIFF:
710  break;
711  default:
712  assert(false);
713  }
714  break;
715  case kBIGINT:
716  case kNUMERIC:
717  case kDECIMAL:
718  switch (compression) {
719  case kENCODING_NONE:
720  return sizeof(int64_t);
721  case kENCODING_FIXED:
722  case kENCODING_SPARSE:
723  return comp_param / 8;
724  case kENCODING_RL:
725  case kENCODING_DIFF:
726  break;
727  default:
728  assert(false);
729  }
730  break;
731  case kFLOAT:
732  switch (compression) {
733  case kENCODING_NONE:
734  return sizeof(float);
735  case kENCODING_FIXED:
736  case kENCODING_RL:
737  case kENCODING_DIFF:
738  case kENCODING_SPARSE:
739  assert(false);
740  break;
741  default:
742  assert(false);
743  }
744  break;
745  case kDOUBLE:
746  switch (compression) {
747  case kENCODING_NONE:
748  return sizeof(double);
749  case kENCODING_FIXED:
750  case kENCODING_RL:
751  case kENCODING_DIFF:
752  case kENCODING_SPARSE:
753  assert(false);
754  break;
755  default:
756  assert(false);
757  }
758  break;
759  case kTIMESTAMP:
760  case kTIME:
761  case kINTERVAL_DAY_TIME:
763  case kDATE:
764  switch (compression) {
765  case kENCODING_NONE:
766  return sizeof(int64_t);
767  case kENCODING_FIXED:
768  if (type == kTIMESTAMP && dimension > 0) {
769  assert(false); // disable compression for timestamp precisions
770  }
771  return comp_param / 8;
772  case kENCODING_RL:
773  case kENCODING_DIFF:
774  case kENCODING_SPARSE:
775  assert(false);
776  break;
778  switch (comp_param) {
779  case 0:
780  return 4; // Default date encoded in days is 32 bits
781  case 16:
782  case 32:
783  return comp_param / 8;
784  default:
785  assert(false);
786  break;
787  }
788  default:
789  assert(false);
790  }
791  break;
792  case kTEXT:
793  case kVARCHAR:
794  case kCHAR:
795  if (compression == kENCODING_DICT) {
796  return sizeof(int32_t); // @TODO(wei) must check DictDescriptor
797  }
798  break;
799  case kARRAY:
800  // TODO: return size for fixlen arrays?
801  break;
802  case kPOINT:
803  case kLINESTRING:
804  case kPOLYGON:
805  case kMULTIPOLYGON:
806  break;
807  default:
808  break;
809  }
810  return -1;
811  }
812 };
813 
814 #ifndef __CUDACC__
815 // todo: Get rid of preprocessor definition and move into Cuda Type Concept
816 template <template <class> class... TYPE_FACET_PACK>
817 std::string SQLTypeInfoCore<TYPE_FACET_PACK...>::type_name[kSQLTYPE_LAST] = {
818  "NULL",
819  "BOOLEAN",
820  "CHAR",
821  "VARCHAR",
822  "NUMERIC",
823  "DECIMAL",
824  "INTEGER",
825  "SMALLINT",
826  "FLOAT",
827  "DOUBLE",
828  "TIME",
829  "TIMESTAMP",
830  "BIGINT",
831  "TEXT",
832  "DATE",
833  "ARRAY",
834  "INTERVAL_DAY_TIME",
835  "INTERVAL_YEAR_MONTH",
836  "POINT",
837  "LINESTRING",
838  "POLYGON",
839  "MULTIPOLYGON",
840  "TINYINT",
841  "GEOMETRY",
842  "GEOGRAPHY",
843  "EVAL_CONTEXT_TYPE",
844  "VOID",
845  "CURSOR"};
846 template <template <class> class... TYPE_FACET_PACK>
847 std::string SQLTypeInfoCore<TYPE_FACET_PACK...>::comp_name[kENCODING_LAST] =
848  {"NONE", "FIXED", "RL", "DIFF", "DICT", "SPARSE", "COMPRESSED", "DAYS"};
849 #endif
850 
851 using SQLTypeInfo =
853 
855 
856 #ifndef __CUDACC__
857 Datum StringToDatum(const std::string& s, SQLTypeInfo& ti);
858 std::string DatumToString(Datum d, const SQLTypeInfo& ti);
859 bool DatumEqual(const Datum, const Datum, const SQLTypeInfo& ti);
860 int64_t convert_decimal_value_to_scale(const int64_t decimal_value,
861  const SQLTypeInfo& type_info,
862  const SQLTypeInfo& new_type_info);
863 #endif
864 
865 #include "../QueryEngine/DateAdd.h"
866 #include "../QueryEngine/DateTruncate.h"
867 #include "../QueryEngine/ExtractFromTime.h"
868 
870  EncodingType encoding = type_info.get_compression();
871  if (encoding == kENCODING_DATE_IN_DAYS ||
872  (encoding == kENCODING_FIXED && type_info.get_type() != kARRAY)) {
873  encoding = kENCODING_NONE;
874  }
875  return SQLTypeInfo(type_info.get_type(),
876  type_info.get_dimension(),
877  type_info.get_scale(),
878  type_info.get_notnull(),
879  encoding,
880  type_info.get_comp_param(),
881  type_info.get_subtype());
882 }
883 
885  SQLTypeInfo nullable_type_info = get_logical_type_info(type_info);
886  nullable_type_info.set_notnull(false);
887  return nullable_type_info;
888 }
889 
890 template <class T>
891 constexpr inline int64_t inline_int_null_value() {
892  return std::is_signed<T>::value ? std::numeric_limits<T>::min()
893  : std::numeric_limits<T>::max();
894 }
895 
896 template <class T>
897 constexpr inline int64_t inline_int_null_array_value() {
898  return std::is_signed<T>::value ? std::numeric_limits<T>::min() + 1
899  : std::numeric_limits<T>::max() - 1;
900  // TODO: null_array values in signed types would step on max valid value
901  // in fixlen unsigned arrays, the max valid value may need to be lowered.
902 }
903 
904 template <class T>
905 constexpr inline int64_t max_valid_int_value() {
906  return std::is_signed<T>::value ? std::numeric_limits<T>::max()
907  : std::numeric_limits<T>::max() - 1;
908 }
909 
910 #include "InlineNullValues.h"
911 
912 using StringOffsetT = int32_t;
913 using ArrayOffsetT = int32_t;
914 
915 #endif // SQLTYPES_H
int8_t tinyintval
Definition: sqltypes.h:126
bool is_fp() const
Definition: sqltypes.h:481
bool is_boolean() const
Definition: sqltypes.h:484
bool is_castable(const SQLTypeInfoCore &new_type_info) const
Definition: sqltypes.h:537
HOST DEVICE bool operator!=(const SQLTypeInfoCore &rhs) const
Definition: sqltypes.h:507
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:332
void setStandardBufferPackaging()
Definition: sqltypes.h:207
#define NULL_DOUBLE
Definition: sqltypes.h:179
void setChunkIteratorPackaging()
Definition: sqltypes.h:208
HOST DEVICE SQLTypeInfoCore & operator=(const SQLTypeInfoCore &rhs)
Definition: sqltypes.h:525
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:193
DEVICE VarlenDatum()
Definition: sqltypes.h:78
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
SQLTypes
Definition: sqltypes.h:41
std::vector< std::string > * stringsPtr
Definition: sqltypes.h:141
std::vector< ArrayDatum > * arraysPtr
Definition: sqltypes.h:142
EncodingType
Definition: encodetypes.h:22
bool is_null
Definition: sqltypes.h:76
#define NULL_BIGINT
Definition: sqltypes.h:177
SQLTypeInfoCore(SQLTypes t, bool n)
Definition: sqltypes.h:297
#define NULL_ARRAY_DOUBLE
Definition: sqltypes.h:187
SQLTypeInfo get_nullable_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:884
bool boolval
Definition: sqltypes.h:125
bool has_bounds() const
Definition: sqltypes.h:395
constexpr int64_t inline_int_null_value()
Definition: sqltypes.h:891
void set_size(int s)
Definition: sqltypes.h:424
HOST DEVICE int get_scale() const
Definition: sqltypes.h:331
bool isChunkIteratorPackaging() const
Definition: sqltypes.h:206
VarlenDatum * arrayval
Definition: sqltypes.h:132
void set_input_srid(int d)
Definition: sqltypes.h:420
constexpr auto is_datetime(T sql_type)
Definition: sqltypes.h:194
HOST DEVICE int get_size() const
Definition: sqltypes.h:336
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:869
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:416
void set_fixed_size()
Definition: sqltypes.h:425
constexpr auto is_date() const
Definition: sqltypes.h:244
#define NULL_ARRAY_SMALLINT
Definition: sqltypes.h:183
#define NULL_ARRAY_TINYINT
Definition: sqltypes.h:182
bool is_varlen() const
Definition: sqltypes.h:491
Definition: sqltypes.h:68
void set_dimension(int d)
Definition: sqltypes.h:418
bool is_varlen_array() const
Definition: sqltypes.h:486
SQLTypeInfoCore(SQLTypes t, int d, int s)
Definition: sqltypes.h:296
void set_scale(int s)
Definition: sqltypes.h:421
void set_compression(EncodingType c)
Definition: sqltypes.h:426
int32_t intval
Definition: sqltypes.h:128
std::string to_string() const
Definition: sqltypes.h:455
std::string to_string(char const *&&v)
HostArrayDatum(size_t const l, int8_t *p, bool const n, CUSTOM_DELETER custom_deleter)
Definition: sqltypes.h:112
void set_notnull(bool n)
Definition: sqltypes.h:423
void set_output_srid(int s)
Definition: sqltypes.h:422
int8_t * pointer
Definition: sqltypes.h:75
bool is_number() const
Definition: sqltypes.h:482
int32_t StringOffsetT
Definition: sqltypes.h:912
#define DEVICE
std::string get_type_name() const
Definition: sqltypes.h:429
constexpr int64_t max_valid_int_value()
Definition: sqltypes.h:905
#define HOST
float floatval
Definition: sqltypes.h:130
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:417
void set_precision(int d)
Definition: sqltypes.h:419
bool is_array() const
Definition: sqltypes.h:485
#define IS_INTERVAL(T)
Definition: sqltypes.h:169
bool is_time() const
Definition: sqltypes.h:483
int get_logical_size() const
Definition: sqltypes.h:337
std::shared_ptr< int8_t > ManagedPtr
Definition: sqltypes.h:93
bool DatumEqual(const Datum a, const Datum b, const SQLTypeInfo &ti)
Definition: Datum.cpp:153
#define NULL_TINYINT
Definition: sqltypes.h:174
VarlenDatum(const size_t l, int8_t *p, const bool n)
Definition: sqltypes.h:81
static std::string type_name[kSQLTYPE_LAST]
Definition: sqltypes.h:678
HOST DEVICE int get_storage_size() const
Definition: sqltypes.h:681
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
int get_physical_coord_cols() const
Definition: sqltypes.h:362
int64_t bigintval
Definition: sqltypes.h:129
SQLTypeInfoCore(SQLTypes t, bool n, EncodingType c)
Definition: sqltypes.h:307
#define NULL_FLOAT
Definition: sqltypes.h:178
HostArrayDatum(size_t const l, int8_t *p, CUSTOM_DELETER custom_deleter)
Definition: sqltypes.h:106
HOST DEVICE bool is_null(const int8_t *val) const
Definition: sqltypes.h:601
constexpr int64_t inline_int_null_array_value()
Definition: sqltypes.h:897
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:333
ManagedPtr data_ptr
Definition: sqltypes.h:115
HostArrayDatum()=default
int16_t smallintval
Definition: sqltypes.h:127
static std::string comp_name[kENCODING_LAST]
Definition: sqltypes.h:679
#define NULL_ARRAY_INT
Definition: sqltypes.h:184
PackagingType packaging_type_
Definition: sqltypes.h:211
#define NULL_INT
Definition: sqltypes.h:176
HostArrayDatum(size_t const l, int8_t *p, bool const n)
Definition: sqltypes.h:100
bool g_enable_smem_group_by true
void operator()(int8_t *p)
Definition: sqltypes.h:89
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:852
bool is_varlen_indeed() const
Definition: sqltypes.h:497
bool is_geometry() const
Definition: sqltypes.h:489
std::string * stringval
Definition: sqltypes.h:134
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:268
bool is_fixlen_array() const
Definition: sqltypes.h:487
DEVICE DeviceArrayDatum()
Definition: sqltypes.h:119
SQLTypes subtype
Definition: sqltypes.h:670
#define CHECK_LT(x, y)
Definition: Logger.h:200
Definition: sqltypes.h:55
Definition: sqltypes.h:56
HOST DEVICE bool is_null(const Datum &d) const
Definition: sqltypes.h:564
int get_array_context_logical_size() const
Definition: sqltypes.h:217
int64_t const int32_t sz assert(dest)
constexpr auto is_high_precision_timestamp() const
Definition: sqltypes.h:252
int64_t convert_decimal_value_to_scale(const int64_t decimal_value, const SQLTypeInfo &type_info, const SQLTypeInfo &new_type_info)
Definition: Datum.cpp:284
HOST DEVICE bool operator==(const SQLTypeInfoCore &rhs) const
Definition: sqltypes.h:515
int32_t ArrayOffsetT
Definition: sqltypes.h:913
#define NULL_ARRAY_BIGINT
Definition: sqltypes.h:185
bool is_integer() const
Definition: sqltypes.h:479
Datum StringToDatum(const std::string &s, SQLTypeInfo &ti)
Definition: Datum.cpp:90
constexpr auto is_date_in_days() const
Definition: sqltypes.h:233
SQLTypeInfoCore get_array_type() const
Definition: sqltypes.h:663
bool is_timeinterval() const
Definition: sqltypes.h:488
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:328
#define IS_INTEGER(T)
Definition: sqltypes.h:161
Definition: sqltypes.h:44
bool has_render_group() const
Definition: sqltypes.h:406
#define IS_STRING(T)
Definition: sqltypes.h:166
SQLTypeInfoCore(SQLTypes t)
Definition: sqltypes.h:306
void set_comp_param(int p)
Definition: sqltypes.h:427
virtual DEVICE ~VarlenDatum()
Definition: sqltypes.h:79
#define NULL_SMALLINT
Definition: sqltypes.h:175
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:327
bool is_string() const
Definition: sqltypes.h:477
bool g_enable_watchdog false
Definition: Execute.cpp:71
int get_physical_cols() const
Definition: sqltypes.h:347
HostArrayDatum(size_t const l, ManagedPtr p, bool const n)
Definition: sqltypes.h:97
int get_precision() const
Definition: sqltypes.h:329
Definition: sqltypes.h:48
SQLTypeInfoCore get_elem_type() const
Definition: sqltypes.h:659
bool is_dict_encoded_string() const
Definition: sqltypes.h:503
bool isStandardBufferPackaging() const
Definition: sqltypes.h:205
HOST DEVICE bool is_null_fixlen_array(const int8_t *val, int array_size) const
Definition: sqltypes.h:626
int8_t * numbersPtr
Definition: sqltypes.h:140
bool is_decimal() const
Definition: sqltypes.h:480
SQLTypes type
Definition: sqltypes.h:669
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:330
#define IS_NUMBER(T)
Definition: sqltypes.h:163
void operator()(int8_t *)
Definition: sqltypes.h:86
#define IS_GEO(T)
Definition: sqltypes.h:167
constexpr auto is_timestamp() const
Definition: sqltypes.h:263
#define TRANSIENT_DICT(ID)
Definition: sqltypes.h:190
EncodingType compression
Definition: sqltypes.h:674
#define NULL_BOOLEAN
Definition: sqltypes.h:173
SQLTypeInfoCore(SQLTypes t, int d, int s, bool n)
Definition: sqltypes.h:287
std::conditional_t< isCudaCC(), DeviceArrayDatum, HostArrayDatum > ArrayDatum
Definition: sqltypes.h:122
SQLTypeInfoCore(SQLTypes t, int d, int s, bool n, EncodingType c, int p, SQLTypes st)
Definition: sqltypes.h:278
bool is_string_array() const
Definition: sqltypes.h:478
double doubleval
Definition: sqltypes.h:131
std::string get_compression_name() const
Definition: sqltypes.h:454
size_t length
Definition: sqltypes.h:74
#define NULL_ARRAY_FLOAT
Definition: sqltypes.h:186