OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
heavydbTypes.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 
17 #pragma once
18 
19 #include <cmath>
20 #include <cstring>
21 #include <limits>
22 #include <stdexcept>
23 #include <type_traits>
24 #include <vector>
25 
26 #include "DateTruncate.h"
27 #include "ExtractFromTime.h"
29 #include "Utils/FlatBuffer.h"
30 
31 #include "DateAdd.h"
32 
33 #include "../Shared/sqltypes_lite.h"
34 
35 #if !(defined(__CUDACC__) || defined(NO_BOOST))
36 #include "../Shared/DateTimeParser.h"
37 #endif
38 
39 /* `../` is required for UDFCompiler */
40 #include "../Shared/InlineNullValues.h"
41 #include "../Shared/funcannotations.h"
42 
43 #ifndef __CUDACC__
44 #ifndef UDF_COMPILED
45 #include "Shared/toString.h"
47 #endif // #ifndef UDF_COMPILED
48 #endif // #ifndef __CUDACC__
49 
50 #include "../Geospatial/CompressionRuntime.h"
51 
52 // declaring CPU functions as __host__ can help catch erroneous compilation of
53 // these being done by the CUDA compiler at build time
54 #define EXTENSION_INLINE_HOST extern "C" RUNTIME_EXPORT ALWAYS_INLINE HOST
55 #define EXTENSION_NOINLINE_HOST extern "C" RUNTIME_EXPORT NEVER_INLINE HOST
56 
57 #define EXTENSION_INLINE extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE
58 #define EXTENSION_NOINLINE extern "C" RUNTIME_EXPORT NEVER_INLINE DEVICE
59 #define TEMPLATE_INLINE ALWAYS_INLINE DEVICE
60 #define TEMPLATE_NOINLINE NEVER_INLINE DEVICE
61 
62 EXTENSION_NOINLINE int8_t* allocate_varlen_buffer(int64_t element_count,
63  int64_t element_size);
64 
65 /*
66  Table function management functions and macros:
67  */
68 #define FUNC_NAME (std::string(__func__).substr(0, std::string(__func__).find("__")))
69 // TODO: support windows path format
70 #define ERROR_STRING(MSG) \
71  (std::string(__FILE__).substr(std::string(__FILE__).rfind("/") + 1) + ":" + \
72  std::to_string(__LINE__) + " " + FUNC_NAME + ": " + MSG) \
73  .c_str()
74 #define TABLE_FUNCTION_ERROR(MSG) table_function_error(ERROR_STRING(MSG))
75 #define ERROR_MESSAGE(MSG) error_message(ERROR_STRING(MSG))
76 #define SUCCESS table_function_success_code()
77 
79  int32_t index,
80  int64_t output_item_values_total_number);
82  int32_t index,
83  int64_t output_array_values_total_number);
84 EXTENSION_NOINLINE_HOST void set_output_row_size(int64_t num_rows);
86  int8_t* mgr_ptr,
87  int32_t index,
88  int64_t output_array_values_total_number);
90  int8_t* mgr_ptr,
91  int32_t index,
92  int64_t output_item_values_total_number);
94  int64_t num_rows);
96 EXTENSION_NOINLINE_HOST int32_t table_function_error(const char* message);
99  const char* message);
101  int8_t* mgr_ptr,
102  const char* key,
103  const uint8_t* raw_bytes,
104  const size_t num_bytes,
105  const TableFunctionMetadataType value_type);
107  int8_t* mgr_ptr,
108  const char* key,
109  const uint8_t*& raw_bytes,
110  size_t& num_bytes,
111  TableFunctionMetadataType& value_type);
112 
114 
116 
118  int8_t* mgr_ptr,
119  int32_t db_id,
120  int32_t dict_id);
121 
122 std::string TableFunctionManager_getString(int8_t* mgr_ptr,
123  int32_t db_id,
124  int32_t dict_id,
125  int32_t string_id);
126 
128  int32_t db_id,
129  int32_t dict_id,
130  std::string str);
131 
133  int64_t count,
134  int64_t size);
135 /*
136  Row function management functions and macros:
137  */
138 
139 #define GET_DICT_DB_ID(mgr, arg_idx) (mgr.getDictDbId(__func__, arg_idx))
140 
141 #define GET_DICT_ID(mgr, arg_idx) (mgr.getDictId(__func__, arg_idx))
142 
144  int8_t* mgr_ptr,
145  int32_t db_id,
146  int32_t dict_id,
147  int32_t string_id);
148 
150  const char* func_name,
151  size_t arg_idx);
152 
154  const char* func_name,
155  size_t arg_idx);
156 
158  int32_t db_id,
159  int32_t dict_id,
160  std::string str);
162  int64_t count,
163  int64_t size);
164 /*
165  Column<Array<T>> methods
166 */
167 
168 EXTENSION_NOINLINE_HOST void ColumnArray_getArray(int8_t* flatbuffer,
169  const int64_t index,
170  const int64_t expected_numel,
171  int8_t*& ptr,
172  int64_t& size, // items count
173  bool& is_null);
174 
175 EXTENSION_NOINLINE_HOST bool ColumnArray_isNull(int8_t* flatbuffer, int64_t index);
176 
177 EXTENSION_NOINLINE_HOST void ColumnArray_setNull(int8_t* flatbuffer, int64_t index);
178 
179 EXTENSION_NOINLINE_HOST void ColumnArray_setArray(int8_t* flatbuffer,
180  int64_t index,
181  const int8_t* ptr,
182  int64_t size,
183  bool is_null);
184 
185 EXTENSION_NOINLINE_HOST void ColumnArray_concatArray(int8_t* flatbuffer,
186  int64_t index,
187  const int8_t* ptr,
188  int64_t size,
189  bool is_null);
190 
191 // For BC:
192 EXTENSION_NOINLINE_HOST void ColumnArray_getItem(int8_t* flatbuffer,
193  const int64_t index,
194  const int64_t expected_numel,
195  int8_t*& ptr,
196  int64_t& size, // in bytes
197  bool& is_null,
198  int64_t sizeof_T);
199 
200 EXTENSION_NOINLINE_HOST void ColumnArray_setItem(int8_t* flatbuffer,
201  int64_t index,
202  const int8_t* ptr,
203  int64_t size,
204  bool is_null,
205  int64_t sizeof_T);
206 
207 EXTENSION_NOINLINE_HOST void ColumnArray_concatItem(int8_t* flatbuffer,
208  int64_t index,
209  const int8_t* ptr,
210  int64_t size,
211  bool is_null,
212  int64_t sizeof_T);
213 
214 // https://www.fluentcpp.com/2018/04/06/strong-types-by-struct/
216  int32_t value;
217 
218 #ifndef __CUDACC__
219  TextEncodingDict(const int32_t other) : value(other) {}
221 #endif
222 
223  operator int32_t() const {
224  return value;
225  }
226 
227  TextEncodingDict operator=(const int32_t other) {
228  value = other;
229  return *this;
230  }
231 
232  DEVICE ALWAYS_INLINE bool isNull() const {
233  return value == inline_int_null_value<int32_t>();
234  }
235 
236  DEVICE ALWAYS_INLINE bool operator==(const TextEncodingDict& other) const {
237  return value == other.value;
238  }
239 
240  DEVICE ALWAYS_INLINE bool operator==(const int32_t& other) const {
241  return value == other;
242  }
243 
244  DEVICE ALWAYS_INLINE bool operator==(const int64_t& other) const {
245  return value == other;
246  }
247 
248  DEVICE ALWAYS_INLINE bool operator!=(const TextEncodingDict& other) const {
249  return !operator==(other);
250  }
251  DEVICE ALWAYS_INLINE bool operator!=(const int32_t& other) const {
252  return !operator==(other);
253  }
254 
255  DEVICE ALWAYS_INLINE bool operator!=(const int64_t& other) const {
256  return !operator==(other);
257  }
258 
259  DEVICE ALWAYS_INLINE bool operator<(const TextEncodingDict& other) const {
260  return value < other.value;
261  }
262 
263  DEVICE ALWAYS_INLINE bool operator<(const int32_t& other) const {
264  return value < other;
265  }
266 
267  DEVICE ALWAYS_INLINE bool operator<(const int64_t& other) const {
268  return value < other;
269  }
270 
271 #ifdef HAVE_TOSTRING
272  std::string toString() const {
273  return ::typeName(this) + "(value=" + ::toString(value) + ")";
274  }
275 #endif
276 };
277 
278 template <>
280 #ifndef __CUDACC__
281  return TextEncodingDict(inline_int_null_value<int32_t>());
282 #else
283  TextEncodingDict null_val;
284  null_val.value = inline_int_null_value<int32_t>();
285  return null_val;
286 #endif
287 }
288 
289 #ifndef __CUDACC__
290 
291 /*
292  This RowFunctionManager struct is a minimal proxy to the
293  RowFunctionManager defined in RowFunctionManager.h. The
294  corresponding instances share `this` but have different virtual
295  tables for methods.
296 */
297 
299  std::string getString(int32_t db_id, int32_t dict_id, int32_t string_id) {
301  reinterpret_cast<int8_t*>(this), db_id, dict_id, string_id);
302  }
303 
304  int32_t getDictDbId(const char* func_name, size_t arg_idx) {
306  reinterpret_cast<int8_t*>(this), func_name, arg_idx);
307  }
308 
309  int32_t getDictId(const char* func_name, size_t arg_idx) {
311  reinterpret_cast<int8_t*>(this), func_name, arg_idx);
312  }
313 
314  int32_t getOrAddTransient(int32_t db_id, int32_t dict_id, std::string str) {
316  reinterpret_cast<int8_t*>(this), db_id, dict_id, str);
317  }
318 
319  int8_t* makeBuffer(int64_t element_count, int64_t element_size) {
321  reinterpret_cast<int8_t*>(this), element_count, element_size);
322  }
323 };
324 
325 #ifndef UDF_COMPILED
326 
327 namespace {
328 template <typename T>
330  if constexpr (std::is_same<T, int8_t>::value) {
332  } else if constexpr (std::is_same<T, int16_t>::value) {
334  } else if constexpr (std::is_same<T, int32_t>::value) {
336  } else if constexpr (std::is_same<T, int64_t>::value) {
338  } else if constexpr (std::is_same<T, float>::value) {
340  } else if constexpr (std::is_same<T, double>::value) {
342  } else if constexpr (std::is_same<T, bool>::value) {
344  }
345  throw std::runtime_error("Unsupported TableFunctionMetadataType");
346 }
347 } // namespace
348 
349 /*
350  This TableFunctionManager struct is a minimal proxy to the
351  TableFunctionManager defined in TableFunctionManager.h. The
352  corresponding instances share `this` but have different virtual
353  tables for methods.
354 */
355 
358  return reinterpret_cast<TableFunctionManager*>(TableFunctionManager_get_singleton());
359  }
360 
362  int64_t output_array_values_total_number) {
364  reinterpret_cast<int8_t*>(this), index, output_array_values_total_number);
365  }
366 
368  int64_t output_item_values_total_number) {
370  reinterpret_cast<int8_t*>(this), index, output_item_values_total_number);
371  }
372 
373  void set_output_row_size(int64_t num_rows) {
374  if (!output_allocations_disabled) {
375  TableFunctionManager_set_output_row_size(reinterpret_cast<int8_t*>(this), num_rows);
376  }
377  }
378 
379  void disable_output_allocations() { output_allocations_disabled = true; }
380 
381  void enable_output_allocations() { output_allocations_disabled = false; }
382 
383  int32_t error_message(const char* message) {
384  return TableFunctionManager_error_message(reinterpret_cast<int8_t*>(this), message);
385  }
386 
387  template <typename T>
388  void set_metadata(const std::string& key, const T& value) {
389  TableFunctionManager_set_metadata(reinterpret_cast<int8_t*>(this),
390  key.c_str(),
391  reinterpret_cast<const uint8_t*>(&value),
392  sizeof(value),
393  get_metadata_type<T>());
394  }
395 
396  template <typename T>
397  void get_metadata(const std::string& key, T& value) {
398  const uint8_t* raw_data{};
399  size_t num_bytes{};
400  TableFunctionMetadataType value_type;
402  reinterpret_cast<int8_t*>(this), key.c_str(), raw_data, num_bytes, value_type);
403  if (sizeof(T) != num_bytes) {
404  throw std::runtime_error("Size mismatch for Table Function Metadata '" + key + "'");
405  }
406  if (get_metadata_type<T>() != value_type) {
407  throw std::runtime_error("Type mismatch for Table Function Metadata '" + key + "'");
408  }
409  std::memcpy(&value, raw_data, num_bytes);
410  }
411  int32_t getNewDictDbId() {
412  return TableFunctionManager_getNewDictDbId(reinterpret_cast<int8_t*>(this));
413  }
414  int32_t getNewDictId() {
415  return TableFunctionManager_getNewDictId(reinterpret_cast<int8_t*>(this));
416  }
417  StringDictionaryProxy* getStringDictionaryProxy(int32_t db_id, int32_t dict_id) {
418  return reinterpret_cast<StringDictionaryProxy*>(
420  reinterpret_cast<int8_t*>(this), db_id, dict_id));
421  }
422  std::string getString(int32_t db_id, int32_t dict_id, int32_t string_id) {
424  reinterpret_cast<int8_t*>(this), db_id, dict_id, string_id);
425  }
426  int32_t getOrAddTransient(int32_t db_id, int32_t dict_id, std::string str) {
428  reinterpret_cast<int8_t*>(this), db_id, dict_id, str);
429  }
430  int8_t* makeBuffer(int64_t element_count, int64_t element_size) {
432  reinterpret_cast<int8_t*>(this), element_count, element_size);
433  }
434 
435 #ifdef HAVE_TOSTRING
436  std::string toString() const {
437  std::string result = ::typeName(this) + "(";
438  if (!(void*)this) { // cast to void* to avoid warnings
439  result += "UNINITIALIZED";
440  }
441  result += ")";
442  return result;
443  }
444 #endif // HAVE_TOSTRING
445  bool output_allocations_disabled{false};
446 };
447 
448 #endif // #ifndef UDF_COMPILED
449 #endif // #ifndef __CUDACC__
450 
451 // Defines the maximal dimensionality of nested array objects.
452 #define NESTED_ARRAY_NDIM 4
453 
454 #define IS_VALUE_TYPE(T) \
455  (std::is_scalar<T>::value || std::is_same<T, TextEncodingDict>::value || \
456  std::is_same<T, Geo::Point2D>::value)
457 
458 namespace flatbuffer {
459 template <typename T>
460 struct Array;
461 }
462 
463 namespace Geo {
464 struct Point2D;
465 }
466 
467 struct TextEncodingNone;
468 
469 template <typename T>
470 struct Array {
471  T* ptr_;
472  int64_t size_;
473  int8_t is_null_;
474 
475  DEVICE Array(T* ptr, const int64_t size, const bool is_null = false)
476  : ptr_(is_null ? nullptr : ptr), size_(size), is_null_(is_null) {}
477  DEVICE Array() : ptr_(nullptr), size_(0), is_null_(true) {}
478 
479 #ifndef __CUDACC__
480  template <typename M>
481  HOST Array(M& mgr, const int64_t size, const bool is_null = false)
482  : size_(size), is_null_(is_null) {
483  static_assert(
484 #ifndef UDF_COMPILED
485  (std::is_same<TableFunctionManager, M>::value) ||
486 #endif // #ifndef UDF_COMPILED
487  (std::is_same<RowFunctionManager, M>::value),
488  "M must be a TableFunctionManager or RowFunctionManager");
489  if (!is_null_) {
490  ptr_ = reinterpret_cast<T*>(mgr.makeBuffer(size, static_cast<int64_t>(sizeof(T))));
491  } else {
492  ptr_ = nullptr;
493  }
494  }
495 #endif // #ifndef __CUDACC__
496 
497  DEVICE Array(const int64_t size, const bool is_null = false)
498  : size_(size), is_null_(is_null) {
499  if (!is_null) {
500  // Memory must be manually released, otherwise leaks can happen.
501  // On UDFs, if it is the return argument, memory is released with
502  // register_buffer_with_executor_rsm
503  ptr_ = reinterpret_cast<T*>(
504  allocate_varlen_buffer(size, static_cast<int64_t>(sizeof(T))));
505  } else {
506  ptr_ = nullptr;
507  }
508  }
509 
511  bool _is_null;
512  arr.getValuesBuffer(ptr_, size_, _is_null);
513  is_null_ = _is_null;
514  }
515 
517  return ptr_;
518  }
519 
520  DEVICE ALWAYS_INLINE size_t size() const {
521  return size_;
522  }
523 
524  DEVICE T operator()(const unsigned int index) const {
525 #ifndef UDF_COMPILED
526  // TODO: implement Array<TextEncodingNone> indexing support
527  static_assert(!std::is_same<T, TextEncodingNone>::value);
528 #endif
529  if (index < static_cast<unsigned int>(size_)) {
530  return ptr_[index];
531  } else {
532  return 0; // see array_at
533  }
534  }
535 
536  DEVICE T& operator[](const unsigned int index) {
537 #ifndef UDF_COMPILED
538  // TODO: implement Array<TextEncodingNone> indexing support
539  static_assert(!std::is_same<T, TextEncodingNone>::value);
540 #endif
541  return ptr_[index];
542  }
543 
544  DEVICE const T& operator[](const unsigned int index) const {
545 #ifndef UDF_COMPILED
546  // TODO: implement Array<TextEncodingNone> indexing support
547  static_assert(!std::is_same<T, TextEncodingNone>::value);
548 #endif
549  return ptr_[index];
550  }
551 
552  DEVICE int64_t getSize() const {
553  return size_;
554  }
555 
556  DEVICE bool isNull() const {
557  return is_null_;
558  }
559 
560  DEVICE constexpr inline T null_value() const {
561  if constexpr (std::is_same<T, TextEncodingNone>::value) {
562  return {};
563  } else {
564  return inline_null_value<T>();
565  }
566  }
567 
568  DEVICE bool isNull(const unsigned int index) const {
569  return (is_null_ ? false : ptr_[index] == null_value());
570  }
571 
572  DEVICE bool operator==(const Array& other) const {
573  if (isNull() || other.isNull() || size() != other.size()) {
574  return false;
575  }
576  for (int64_t i = 0; i < size_; i++) {
577  if ((*this)[i] != other[i]) {
578  return false;
579  }
580  }
581  return true;
582  }
583 
584 #ifdef HAVE_TOSTRING
585  std::string toString() const {
586  std::string result = ::typeName(this) +
587  "(ptr=" + ::toString(reinterpret_cast<void*>(ptr_)) +
588  ", size=" + std::to_string(size_) +
589  ", is_null=" + std::to_string(is_null_) + ")[";
590  for (int64_t i = 0; i < size_; i++) {
591  if (size_ > 10) {
592  // show the first 8 and the last 2 values in the array:
593  if (i == 8) {
594  result += "..., ";
595  } else if (i > 8 && i < size_ - 2) {
596  continue;
597  }
598  }
599  result += ::toString((*this)[i]) + ", ";
600  }
601  result += "]";
602  return result;
603  }
604 #endif
605 };
606 
608  char* ptr_;
609  int64_t size_;
610  // padding is required to prevent clang/gcc from expanding arguments
611  // https://stackoverflow.com/questions/27386912/prevent-clang-from-expanding-arguments-that-are-aggregate-types/27387908#27387908
612  int8_t padding;
613 
614 #ifndef __CUDACC__
615  TextEncodingNone() = default;
616  // Requires external ownership.
617  explicit TextEncodingNone(char const* const c_str)
618  : ptr_(const_cast<char*>(c_str)), size_(strlen(c_str)) {}
619 
620  template <typename M>
621  explicit DEVICE ALWAYS_INLINE TextEncodingNone(M& mgr, const std::string& str) {
622  static_assert(
623 #ifndef UDF_COMPILED
624  (std::is_same<TableFunctionManager, M>::value) ||
625 #endif // #ifndef UDF_COMPILED
626  (std::is_same<RowFunctionManager, M>::value),
627  "M must be a TableFunctionManager or RowFunctionManager");
628  size_ = str.length();
629  if (str.empty()) {
630  ptr_ = nullptr;
631  } else {
632  int8_t* buffer = mgr.makeBuffer(size_, static_cast<int64_t>(sizeof(char)));
633  ptr_ = reinterpret_cast<char*>(buffer);
634  strcpy(ptr_, str.c_str());
635  }
636  }
637 
638  operator std::string() const {
639  return std::string(ptr_, size_);
640  }
641  std::string getString() const {
642  return std::string(ptr_, size_);
643  }
644 #endif
645 
646  DEVICE ALWAYS_INLINE char& operator[](const unsigned int index) {
647  return index < size_ ? ptr_[index] : ptr_[size_ - 1];
648  }
649  DEVICE ALWAYS_INLINE bool operator==(const char* rhs) const {
650 #ifdef __CUDACC__
651  for (int i = 0; i < size_; i++) {
652  if (rhs[i] == '\0' || ptr_[i] != rhs[i]) {
653  return false;
654  }
655  }
656  return rhs[size_] == '\0';
657 #else
658  return strcmp(ptr_, rhs) == 0;
659 #endif
660  }
662  if (isNull() || rhs.isNull() || size_ != rhs.size()) {
663  return false;
664  }
665 #ifdef __CUDACC__
666  for (int i = 0; i < size_; i++) {
667  if ((*this)[i] != rhs[i]) {
668  return false;
669  }
670  }
671  return true;
672 #else
673  return strncmp(ptr_, rhs.data(), size_) == 0;
674 #endif
675  }
676  DEVICE ALWAYS_INLINE bool operator!=(const char* rhs) const {
677  return !(this->operator==(rhs));
678  }
680  return !(this->operator==(rhs));
681  }
682  DEVICE ALWAYS_INLINE operator char*() const {
683  return ptr_;
684  }
685  DEVICE ALWAYS_INLINE char* data() const {
686  return ptr_;
687  }
688  DEVICE ALWAYS_INLINE int64_t size() const {
689  return size_;
690  }
691  DEVICE ALWAYS_INLINE bool isNull() const {
692  return size_ == 0;
693  }
694 };
695 
696 struct DayTimeInterval;
697 struct YearMonthTimeInterval;
698 struct Timestamp {
699  int64_t time;
700 
701  Timestamp() = default;
702 
703  DEVICE Timestamp(int64_t timeval) : time(timeval) {}
704 
705  DEVICE ALWAYS_INLINE Timestamp operator+(const DayTimeInterval& interval) const;
707 
708 #if !(defined(__CUDACC__) || defined(NO_BOOST))
709  DEVICE Timestamp(std::string_view const str) {
710  time = dateTimeParse<kTIMESTAMP>(str, 9);
711  }
712 #endif
713 
714  DEVICE ALWAYS_INLINE const Timestamp operator+(const Timestamp& other) const {
715 #ifndef __CUDACC__
716  if (other.time > 0) {
717  if (time > (std::numeric_limits<int64_t>::max() - other.time)) {
718  throw std::underflow_error("Underflow in Timestamp addition!");
719  }
720  } else {
721  if (time < (std::numeric_limits<int64_t>::min() - other.time)) {
722  throw std::overflow_error("Overflow in Timestamp addition!");
723  }
724  }
725 #endif
726  return Timestamp(time + other.time);
727  }
728 
729  DEVICE ALWAYS_INLINE const Timestamp operator-(const Timestamp& other) const {
730 #ifndef __CUDACC__
731  if (other.time > 0) {
732  if (time < (std::numeric_limits<int64_t>::min() + other.time)) {
733  throw std::underflow_error("Underflow in Timestamp substraction!");
734  }
735  } else {
736  if (time > (std::numeric_limits<int64_t>::max() + other.time)) {
737  throw std::overflow_error("Overflow in Timestamp substraction!");
738  }
739  }
740 #endif
741  return Timestamp(time - other.time);
742  }
743 
744  DEVICE ALWAYS_INLINE int64_t operator/(const Timestamp& other) const {
745 #ifndef __CUDACC__
746  if (other.time == 0) {
747  throw std::runtime_error("Timestamp division by zero!");
748  }
749 #endif
750  return time / other.time;
751  }
752 
753  DEVICE ALWAYS_INLINE const Timestamp operator*(const int64_t multiplier) const {
754 #ifndef __CUDACC__
755  int64_t overflow_test = static_cast<int64_t>(time) * static_cast<int64_t>(multiplier);
756  if (time != 0 && overflow_test / time != static_cast<int64_t>(multiplier)) {
757  throw std::overflow_error("Overflow in Timestamp multiplication!");
758  }
759 #endif
760  return Timestamp(time * multiplier);
761  }
762 
763  DEVICE ALWAYS_INLINE bool operator==(const Timestamp& other) const {
764  return time == other.time;
765  }
766 
767  DEVICE ALWAYS_INLINE bool operator!=(const Timestamp& other) const {
768  return !operator==(other);
769  }
770 
771  DEVICE ALWAYS_INLINE bool operator<(const Timestamp& other) const {
772  return time < other.time;
773  }
774 
777  }
778 
781  }
782 
785  }
786 
789  }
790 
793  }
794 
797  }
798 
801  }
802 
805  }
806 
809  }
810  // Should always be safe as we're downcasting to lower precisions
813  }
814  // Should always be safe as we're downcasting to lower precisions
817  }
818  // Should always be safe as we're downcasting to lower precisions
819  DEVICE ALWAYS_INLINE int64_t getSeconds() const {
821  }
822  DEVICE ALWAYS_INLINE int64_t getMinutes() const {
824  }
825  DEVICE ALWAYS_INLINE int64_t getHours() const {
827  }
828  DEVICE ALWAYS_INLINE int64_t getDay() const {
830  }
831  DEVICE ALWAYS_INLINE int64_t getMonth() const {
833  }
834  DEVICE ALWAYS_INLINE int64_t getYear() const {
836  }
837 #ifdef HAVE_TOSTRING
838  std::string toString() const {
839  Datum d;
840  d.bigintval = time;
841  return ::typeName(this) + "(time=" + ::toString(time) + ")" +
842  ", (formatted= " + DatumToString(d, SQLTypeInfo(kTIMESTAMP, 9, 0, false)) +
843  ")";
844  }
845 #endif
846 };
847 
848 template <>
850  return Timestamp(inline_int_null_value<int64_t>());
851 }
852 
854  int64_t timeval;
855 
856  DEVICE DayTimeInterval(int64_t init) : timeval(init) {}
857 
858  DEVICE ALWAYS_INLINE bool operator==(const DayTimeInterval& other) const {
859  return timeval == other.timeval;
860  }
861 
862  DEVICE ALWAYS_INLINE bool operator!=(const DayTimeInterval& other) const {
863  return !operator==(other);
864  }
865 
868  daMILLISECOND, timeval, t.time, 9, inline_int_null_value<int64_t>()));
869  }
870 
871  DEVICE ALWAYS_INLINE DayTimeInterval operator*(const int64_t multiplier) const {
872  return DayTimeInterval(timeval * multiplier);
873  }
874 
876  const Timestamp& end) const {
877 #ifndef __CUDACC__
878  if (timeval == 0) {
879  throw std::runtime_error("Timestamp division by zero!");
880  }
881 #endif
882 
883  if ((timeval > 0 && end.time < begin.time) ||
884  (timeval < 0 && end.time > begin.time)) {
885  return -1;
886  }
887 
888  Timestamp diff = end.time - begin.time;
889  int64_t asNanoSecs =
890  static_cast<int64_t>(timeval) * static_cast<int64_t>(kMicroSecsPerSec);
891 #ifndef __CUDACC__
892  if (timeval != 0 && asNanoSecs / timeval != static_cast<int64_t>(kMicroSecsPerSec)) {
893  throw std::overflow_error("Overflow in INTERVAL precision conversion!");
894  }
895 #endif
896 
897  return diff / asNanoSecs;
898  }
899 };
900 
902  int64_t timeval;
903 
905 
907  return timeval == other.timeval;
908  }
909 
911  return !operator==(other);
912  }
913 
916  daMONTH, timeval, t.time, 9, inline_int_null_value<int64_t>()));
917  }
918 
919  DEVICE ALWAYS_INLINE YearMonthTimeInterval operator*(const int64_t multiplier) const {
920  return YearMonthTimeInterval(timeval * multiplier);
921  }
922 
924  const Timestamp& end) const {
925  if ((timeval > 0 && end.time < begin.time) ||
926  (timeval < 0 && end.time > begin.time)) {
927  return -1;
928  }
929 
930  int64_t ret = ((end.getYear() * 12 + end.getMonth()) -
931  (begin.getYear() * 12 + begin.getMonth())) /
932  timeval;
933  return ret;
934  }
935 };
936 
938  const DayTimeInterval& interval) const {
939  return interval + *this;
940 }
941 
943  const YearMonthTimeInterval& interval) const {
944  return interval + *this;
945 }
946 
948  int8_t* ptr;
949  int32_t sz;
950  int32_t compression;
951  int32_t input_srid;
952  int32_t output_srid;
953 
954  DEVICE int64_t getSize() const { return sz; }
955 
956  DEVICE int32_t getCompression() const { return compression; }
957 
958  DEVICE int32_t getInputSrid() const { return input_srid; }
959 
960  DEVICE int32_t getOutputSrid() const { return output_srid; }
961 };
962 
963 typedef struct GeoPointStruct GeoPoint;
964 
966  int8_t* ptr;
967  int32_t sz;
968  int32_t compression;
969  int32_t input_srid;
970  int32_t output_srid;
971 
972  DEVICE int32_t getSize() const { return sz; }
973 
974  DEVICE int32_t getCompression() const { return compression; }
975 
976  DEVICE int32_t getInputSrid() const { return input_srid; }
977 
978  DEVICE int32_t getOutputSrid() const { return output_srid; }
979 };
980 
982 
984  int8_t* ptr;
985  int32_t sz;
986  int32_t compression;
987  int32_t input_srid;
988  int32_t output_srid;
989 
990  DEVICE int32_t getSize() const { return sz; }
991 
992  DEVICE int32_t getCompression() const { return compression; }
993 
994  DEVICE int32_t getInputSrid() const { return input_srid; }
995 
996  DEVICE int32_t getOutputSrid() const { return output_srid; }
997 };
998 
1000 
1002  int8_t* ptr;
1003  int32_t sz;
1006  int32_t compression;
1007  int32_t input_srid;
1008  int32_t output_srid;
1009 
1010  DEVICE int8_t* getCoords() const { return ptr; }
1011 
1012  DEVICE int32_t getCoordsSize() const { return sz; }
1013 
1015 
1016  DEVICE int32_t getNumLineStrings() const { return num_linestrings; }
1017 
1018  DEVICE int32_t getCompression() const { return compression; }
1019 
1020  DEVICE int32_t getInputSrid() const { return input_srid; }
1021 
1022  DEVICE int32_t getOutputSrid() const { return output_srid; }
1023 };
1024 
1026 
1028  int8_t* ptr_coords;
1029  int32_t coords_size;
1030  int8_t* ring_sizes;
1031  int32_t num_rings;
1032  int32_t compression;
1033  int32_t input_srid;
1034  int32_t output_srid;
1035 
1036  DEVICE int8_t* getRingSizes() { return ring_sizes; }
1037  DEVICE int32_t getCoordsSize() const { return coords_size; }
1038 
1039  DEVICE int32_t getNumRings() const { return num_rings; }
1040 
1041  DEVICE int32_t getCompression() const { return compression; }
1042 
1043  DEVICE int32_t getInputSrid() const { return input_srid; }
1044 
1045  DEVICE int32_t getOutputSrid() const { return output_srid; }
1046 };
1047 
1049 
1051  int8_t* ptr_coords;
1052  int32_t coords_size;
1053  int8_t* ring_sizes;
1054  int32_t num_rings;
1055  int8_t* poly_sizes;
1056  int32_t num_polys;
1057  int32_t compression;
1058  int32_t input_srid;
1059  int32_t output_srid;
1060 
1061  DEVICE int8_t* getRingSizes() { return ring_sizes; }
1062  DEVICE int32_t getCoordsSize() const { return coords_size; }
1063 
1064  DEVICE int32_t getNumRings() const { return num_rings; }
1065 
1066  DEVICE int8_t* getPolygonSizes() { return poly_sizes; }
1067 
1068  DEVICE int32_t getNumPolygons() const { return num_polys; }
1069 
1070  DEVICE int32_t getCompression() const { return compression; }
1071 
1072  DEVICE int32_t getInputSrid() const { return input_srid; }
1073 
1074  DEVICE int32_t getOutputSrid() const { return output_srid; }
1075 };
1076 
1078 
1079 // There are redundant #ifndef UDF_COMPILED inside
1080 // ifguard for StringDictionaryProxy to flag that
1081 // if we decide to adapt C++ UDF Compiler for table
1082 // functions, the linking issue we encountered with
1083 // the shared_mutex include in StringDicitonaryProxy
1084 // will need to be resolved separately.
1085 
1086 #ifndef UDF_COMPILED
1087 
1088 #ifdef __CUDACC__
1089 template <typename T>
1090 static DEVICE __constant__ T Column_null_value;
1091 #endif
1092 
1093 template <typename T>
1094 struct Column {
1095  T* ptr_; // row data
1096  int64_t num_rows_; // row count
1097 
1098  DEVICE Column(T* ptr, const int64_t num_rows) : ptr_(ptr), num_rows_(num_rows) {}
1099 
1100 #ifndef __CUDACC__
1101 #ifndef UDF_COMPILED
1102  DEVICE Column(const Column& other) : ptr_(other.ptr_), num_rows_(other.num_rows_) {}
1103  DEVICE Column(std::vector<T>& input_vec)
1104  : ptr_(input_vec.data()), num_rows_(static_cast<int64_t>(input_vec.size())) {}
1105 #endif // #ifndef UDF_COMPILED
1106 #endif // #ifndef __CUDACC__
1107 
1108  DEVICE T& operator[](const unsigned int index) const {
1109  if (index >= num_rows_) {
1110 #ifndef __CUDACC__
1111  throw std::runtime_error("column buffer index is out of range");
1112 #else
1113  auto& null_value = Column_null_value<T>;
1114  set_null(null_value);
1115  return null_value;
1116 #endif
1117  }
1118  return ptr_[index];
1119  }
1120  DEVICE inline T* getPtr() const {
1121  return ptr_;
1122  }
1123  DEVICE inline int64_t size() const {
1124  return num_rows_;
1125  }
1126  DEVICE inline void setSize(int64_t num_rows) {
1127  num_rows_ = num_rows;
1128  }
1129 
1130  DEVICE inline bool isNull(int64_t index) const {
1131  return is_null(ptr_[index]);
1132  }
1133  DEVICE inline void setNull(int64_t index) {
1134  set_null(ptr_[index]);
1135  }
1137 #ifndef __CUDACC__
1138  if (size() == other.size()) {
1139  memcpy(ptr_, &other[0], other.size() * sizeof(T));
1140  } else {
1141  throw std::runtime_error("cannot copy assign columns with different sizes");
1142  }
1143 #else
1144  if (size() == other.size()) {
1145  for (unsigned int i = 0; i < size(); i++) {
1146  ptr_[i] = other[i];
1147  }
1148  } else {
1149  // TODO: set error
1150  }
1151 #endif
1152  return *this;
1153  }
1154 
1155 #ifdef HAVE_TOSTRING
1156  std::string toString() const {
1157  return ::typeName(this) + "(ptr=" + ::toString(reinterpret_cast<void*>(ptr_)) +
1158  ", num_rows=" + std::to_string(num_rows_) + ")";
1159  }
1160 #endif
1161 };
1162 
1163 namespace flatbuffer {
1164 
1165 // clang-format off
1166 /*
1167  flatbuffer::Column<RowType, RowStruct> is a base class for various Column types
1168  that use FlatBuffer storage. It provides an API to access column
1169  rows:
1170 
1171  bool isNull(int64_t index) --- check if index-th row is a NULL row
1172  int64_t size() --- return the total number of rows
1173  RowType getItem(int64_t index) --- get the index-th row
1174  RowType operator[](unsigned int index) --- same as getItem
1175  RowStruct operator()(unsigned int index) --- getItem result returned as RowStruct
1176  void setNull(int64_t index) --- set the index-th row to NULL
1177  voit setItem(int64_t index, const RowType& item) --- set the index-th row to item
1178 
1179  RowType class is a substruct of NestedArray<ItemType>. If the row
1180  item is a some sort of a sequence of, say, an array of scalar
1181  values, geo points, etc., then ItemType itself can be a subclass of
1182  NestedArray<SubItemType>. The same applies to SubItemType etc.
1183 
1184  RowStruct is a struct capturing the data of a row as a buffer and
1185  its size.
1186 
1187  NestedArray<ItemType, ...> provides an API for accessing its items:
1188 
1189  size_t size() --- return the number of items in the NestedArray
1190  instance
1191  size_t size(int64_t index) --- return the number of subitems in an item
1192  ItemType getItem(int64_t index) --- return the index-th item
1193  ItemType operator[](unsigned int index) --- same as getItem
1194  bool isNull() --- check if NestedArray instance itself is NULL
1195  NestedArray<ItemType> operator=(NestedArray<ItemType>& other)
1196  --- copy other items to self
1197 
1198  If ItemType class is not a subclass of NestedArray, then classes
1199  derived from NestedArray<ItemType> must
1200  implement the following methods:
1201 
1202  ItemType getItem(const int64_t index);
1203  ItemType getItem(const int64_t index) const;
1204  ItemType operator[](unsigned int index);
1205 
1206  Notice that classes derived from NestedArray do not implement
1207  setItem nor setNull methods. Only flatbuffer::Column<RowType>::setItem
1208  method can be used to allocate and initialize items. The setItem can
1209  be called exactly once per index value.
1210 
1211  To change the content of existing NestedArray item, the operator=
1212  interface can be used. However, then the NestedArray item must have
1213  exactly the same shape as in the other NestedArray item has.
1214 */
1215 // clang-format on
1216 
1217 template <typename RowType, typename RowStruct>
1218 struct Column {
1219  int8_t* flatbuffer_;
1220  int64_t num_rows_;
1221 
1222  Column<RowType, RowStruct>(int8_t* flatbuffer, int64_t num_rows)
1223  : flatbuffer_(flatbuffer), num_rows_(num_rows) {}
1224 
1225  // Return true if index-th row is NULL.
1226  DEVICE inline bool isNull(int64_t index) const {
1228  bool is_null = false;
1229  auto status = m.isNull(index, is_null);
1230 #ifndef __CUDACC__
1231  if (status != FlatBufferManager::Status::Success) {
1232  throw std::runtime_error("isNull failed: " + ::toString(status));
1233  }
1234 #endif
1235  return is_null;
1236  }
1237 
1238  // Return the number of rows.
1239  DEVICE int64_t size() const {
1240  return num_rows_;
1241  }
1242 
1243  // Set the index-th row to NULL. Can be called once per row.
1244  DEVICE inline void setNull(int64_t index) {
1246  auto status = m.setNull(index);
1247 #ifndef __CUDACC__
1248  if (status != FlatBufferManager::Status::Success) {
1249  throw std::runtime_error("setNull failed: " + ::toString(status));
1250  }
1251 #endif
1252  }
1253 
1254  // Return row object.
1255  DEVICE inline RowType getItem(const int64_t index) const {
1256  RowType row{{flatbuffer_, {index}, 1}};
1257  return row;
1258  }
1259 
1260  // Return row object via indexing.
1261  DEVICE inline RowType operator[](const unsigned int index) const {
1262  return getItem(static_cast<int64_t>(index));
1263  }
1264 
1265  // Return row buffer object.
1266  DEVICE inline RowStruct operator()(const unsigned int index) const {
1267  return (*this)[index];
1268  }
1269 
1270  // Copy item into the index-th row.
1271  DEVICE inline void setItem(int64_t index, const RowType& item) {
1272  RowType this_item = getItem(index);
1273  this_item = item;
1274  }
1275 
1276  // Copy item into the index-th row.
1277  DEVICE inline void setItem(int64_t index, const RowStruct& item) {
1278  RowType this_item = getItem(index);
1279  this_item = item;
1280  }
1281 
1282  // Return the total number of values that the flatbuffer instance
1283  // holds.
1284  inline int64_t getNofValues() const {
1286  return m.getValuesCount();
1287  }
1288 
1289  DEVICE inline void concatItem(int64_t index, const RowType& item) {
1290  RowType this_item = getItem(index);
1291  this_item += item;
1292  }
1293 
1294  DEVICE inline void concatItem(int64_t index, const RowStruct& item) {
1295  RowType this_item = getItem(index);
1296  this_item += item;
1297  }
1298 
1299  // Return row object with the specified extra number of elements.
1300  DEVICE RowType getItem(const int64_t index, const int64_t extra_numel) const {
1301  auto result = (*this)[index];
1302  if (extra_numel >= 0) {
1303  result.extend(nullptr, extra_numel, /*assing=*/false);
1304  }
1305  return result;
1306  }
1307 
1308  const SQLTypeInfoLite* getTypeInfo() const {
1310  const auto* ti = reinterpret_cast<const SQLTypeInfoLite*>(m.get_user_data_buffer());
1311  if (ti == nullptr) {
1312 #ifndef __CUDACC__
1313  throw std::runtime_error(::typeName(this) +
1314  " getTypeInfo failed: no user data buffer");
1315 #endif
1316  }
1317  return ti;
1318  }
1319 
1320 #ifdef HAVE_FLATBUFFER_TOSTRING
1321  std::string toString() const {
1323  return ::typeName(this) + "(" + m.toString() +
1324  ", num_rows=" + std::to_string(num_rows_) + ")";
1325  }
1326 #endif
1327 };
1328 
1329 template <typename ItemType>
1330 struct NestedArray {
1331  /*
1332  flatbuffer_ contains a NestedArray with dimensionality up to NESTED_ARRAY_NDIM.
1333 
1334  index_ contains indices used to index a NestedArray
1335 
1336  n_ defines how many indices in index_ are used in the indexing operation
1337  */
1338 
1339  int8_t* flatbuffer_;
1341  size_t n_{0};
1342 
1343  size_t size() const {
1345  size_t length;
1346  FlatBufferManager::Status status = FlatBufferManager::Status::NotImplementedError;
1347  status = m.getLength<NESTED_ARRAY_NDIM>(index_, n_, length);
1348  if (status != FlatBufferManager::Status::Success) {
1349 #ifndef __CUDACC__
1350  throw std::runtime_error(::typeName(this) + " size failed: getLength failed with " +
1351  ::toString(status));
1352 #endif
1353  return 0;
1354  }
1355  return length;
1356  }
1357 
1358  size_t size(const int64_t index) const {
1359  return getItem(index).size();
1360  }
1361 
1362  void getValuesBuffer(ItemType*& values,
1363  int64_t& nof_values,
1364  bool& is_null,
1365  bool require_1d_item = true) const {
1366  int8_t* _values;
1367  size_t value_size;
1368  getRawBuffer(_values, nof_values, value_size, is_null, require_1d_item);
1369  values = reinterpret_cast<ItemType*>(_values);
1370  }
1371 
1372  void getRawBuffer(int8_t*& values,
1373  int64_t& nof_values,
1374  size_t& value_size,
1375  bool& is_null,
1376  bool require_1d_item = true) const {
1377  values = nullptr;
1378  nof_values = 0;
1379  is_null = true;
1380  value_size = 0;
1381  if constexpr (!IS_VALUE_TYPE(ItemType)) {
1382 #ifndef __CUDACC__
1383  ItemType item{};
1384  throw std::runtime_error(::typeName(this) +
1385  " getValuesBuffer failed: expected scalar type but got " +
1386  ::typeName(&item));
1387 #endif
1388  } else {
1389  FlatBufferManager::Status status{};
1392  status = m.getItem<NESTED_ARRAY_NDIM>(index_, n_, item);
1393  if (status != FlatBufferManager::Status::Success) {
1394 #ifndef __CUDACC__
1395  throw std::runtime_error(::typeName(this) +
1396  " getValuesBuffer failed: getItem returned " +
1397  ::toString(status));
1398 #endif
1399  }
1400  if (require_1d_item && item.nof_sizes != 0) {
1401 #ifndef __CUDACC__
1402  throw std::runtime_error(::typeName(this) +
1403  " &getValuesBuffer failed: expected 1-D item");
1404 #endif
1405  }
1406  values = item.values;
1407  nof_values = item.nof_values;
1408  is_null = item.is_null;
1409  value_size = m.getValueSize();
1410  }
1411  }
1412 
1413  // Return reference to index-th item that is of scalar type.
1414  // For the direct assess to the array buffer, one can use `&getValue(0)`
1415  ItemType& getValue(const int64_t index) const {
1416  if constexpr (!IS_VALUE_TYPE(ItemType)) {
1417 #ifndef __CUDACC__
1418  ItemType item{};
1419  throw std::runtime_error(::typeName(this) +
1420  " &getValue failed: expected scalar type but got " +
1421  ::typeName(&item));
1422 #endif
1423  }
1424  ItemType* values;
1425  int64_t nof_values;
1426  bool is_null;
1427  getValuesBuffer(values, nof_values, is_null);
1428  if (is_null) {
1429 #ifndef __CUDACC__
1430  throw std::runtime_error(::typeName(this) +
1431  " &getValue failed: expected non-null item ");
1432 #endif
1433  }
1434  if (index < 0 || index >= nof_values) {
1435 #ifndef __CUDACC__
1436  throw std::runtime_error(
1437  ::typeName(this) + " &getValue failed: index (=" + std::to_string(index) +
1438  ") is out of range, expected 0 <= index < " + std::to_string(nof_values));
1439 #endif
1440  }
1441  return values[index];
1442  }
1443 
1444  // same as getItem
1445  const ItemType operator[](const int64_t index) const {
1446  return getItem(index);
1447  }
1448 
1449  // Return index-th item,
1450  const ItemType getItem(const int64_t index) const {
1451  if constexpr (IS_VALUE_TYPE(ItemType)) {
1452  return getValue(index);
1453  } else {
1454  ItemType result{flatbuffer_, {}, n_ + 1};
1455  for (size_t i = 0; i < n_; i++) {
1456  result.index_[i] = index_[i];
1457  }
1458  result.index_[n_] = index;
1459  return result;
1460  }
1461  }
1462 
1463  // Check if the parent is NULL
1464  inline bool isNull() const {
1466  bool is_null = false;
1467  auto status = m.isNull(index_[0], is_null);
1468 #ifndef __CUDACC__
1469  if (status != FlatBufferManager::Status::Success) {
1470  throw std::runtime_error(::typeName(this) + "isNull failed: isNull returned " +
1471  ::toString(status));
1472  }
1473 #endif
1474  return is_null;
1475  }
1476 
1477  // Check if the index-th item has NULL value
1478  inline bool isNull(int64_t index) const {
1480  bool is_null = false;
1481  int64_t tmp_index[NESTED_ARRAY_NDIM];
1482  for (size_t i = 0; i < n_; i++) {
1483  tmp_index[i] = index_[i];
1484  }
1485  tmp_index[n_] = index;
1486  auto status =
1487  m.isNull<NESTED_ARRAY_NDIM>(tmp_index, static_cast<size_t>(n_ + 1), is_null);
1488 #ifndef __CUDACC__
1489  if (status != FlatBufferManager::Status::Success) {
1490  throw std::runtime_error(::typeName(this) +
1491  "isNull(index) failed: isNull returned " +
1492  ::toString(status));
1493  }
1494 #endif
1495  return is_null;
1496  }
1497 
1498  // Extends or assigns other item to item
1499  DEVICE void extend(const int8_t* data, const int32_t size, bool assign = false) {
1500  if (n_ != 1) {
1501 #ifndef __CUDACC__
1502  throw std::runtime_error(::typeName(this) +
1503  " extend failed: expected single index, got " +
1504  ::toString(n_));
1505 #endif
1506  }
1507  FlatBufferManager::Status status{};
1509  if (assign) {
1510  status = m.setItem(index_[0], data, size);
1511  if (status != FlatBufferManager::Status::Success) {
1512 #ifndef __CUDACC__
1513  throw std::runtime_error(
1514  ::typeName(this) +
1515  " extend failed: setItem failed with: " + ::toString(status));
1516 #endif
1517  }
1518  } else {
1519  status = m.concatItem(index_[0], data, size);
1520  if (status != FlatBufferManager::Status::Success) {
1521 #ifndef __CUDACC__
1522  throw std::runtime_error(
1523  ::typeName(this) +
1524  " extend failed: concatItem failed with: " + ::toString(status));
1525 #endif
1526  }
1527  }
1528  }
1529 
1530  void extend(const NestedArray<ItemType>& other, bool assign = false) {
1531  // TODO: check flatbuffer_ equality
1532  if (n_ != 1) {
1533 #ifndef __CUDACC__
1534  throw std::runtime_error(::typeName(this) +
1535  "extend failed: expected single index, got " +
1536  ::toString(n_));
1537 #endif
1538  }
1539  FlatBufferManager other_m{other.flatbuffer_};
1541  auto status = other_m.getItem<NESTED_ARRAY_NDIM>(other.index_, other.n_, item);
1542  if (status != FlatBufferManager::Status::Success) {
1543 #ifndef __CUDACC__
1544  throw std::runtime_error(::typeName(this) + " extend failed: getItem raised " +
1545  ::toString(status));
1546 #endif
1547  } else {
1549  if (assign) {
1550  status = this_m.setItem<NESTED_ARRAY_NDIM>(index_[0], item);
1551  if (status != FlatBufferManager::Status::Success) {
1552 #ifndef __CUDACC__
1553  throw std::runtime_error(::typeName(this) + " extend failed: setItem raised " +
1554  ::toString(status));
1555 #endif
1556  }
1557  } else {
1558  status = this_m.concatItem<NESTED_ARRAY_NDIM>(index_[0], item);
1559  if (status != FlatBufferManager::Status::Success) {
1560 #ifndef __CUDACC__
1561  throw std::runtime_error(::typeName(this) +
1562  " extend failed: concatItem raised " +
1563  ::toString(status));
1564 #endif
1565  }
1566  }
1567  }
1568  }
1569 
1570  // copy other into self, can be called exactly once
1572  extend(other, /*assign=*/true);
1573  return *this;
1574  }
1575 
1576  // extend self with other, can be called multiple times provided
1577  // that self is the last specified item in its container
1579  extend(other, /*assign=*/false);
1580  return *this;
1581  }
1582 
1583  const SQLTypeInfoLite* getTypeInfo() const {
1585  const auto* ti = reinterpret_cast<const SQLTypeInfoLite*>(m.get_user_data_buffer());
1586  if (ti == nullptr) {
1587 #ifndef __CUDACC__
1588  throw std::runtime_error(::typeName(this) +
1589  " getTypeInfo failed: user data buffer is null");
1590 #endif
1591  }
1592  return ti;
1593  }
1594 
1595 #ifdef HAVE_TOSTRING
1596  std::string toString() const {
1597  std::string result = ::typeName(this) + "(..., {";
1598  for (size_t i = 0; i < n_; i++) {
1599  result += std::to_string(index_[i]) + ", ";
1600  }
1601  result += "}, " + std::to_string(n_) + ")";
1602  return result;
1603  }
1604 #endif
1605 };
1606 
1610 
1611  char& operator[](const int64_t index) { return getValue(index); }
1612 
1613  DEVICE TextEncodingNone& operator=(const ::TextEncodingNone& other) {
1614  extend(reinterpret_cast<const int8_t*>(other.data()),
1615  static_cast<int32_t>(other.size()),
1616  true);
1617  return *this;
1618  }
1619 
1620  DEVICE TextEncodingNone& operator+=(const ::TextEncodingNone& other) {
1621  extend(reinterpret_cast<const int8_t*>(other.data()),
1622  static_cast<int32_t>(other.size()),
1623  false);
1624  return *this;
1625  }
1626 
1627 #ifndef __CUDACC__
1628  // std::string supported API
1629 
1630  std::string str() const {
1631  char* values;
1632  int64_t nof_values;
1633  bool is_null;
1634  getValuesBuffer(values, nof_values, is_null);
1635  if (is_null) {
1636  return {};
1637  }
1638  std::string result;
1639  result.assign(reinterpret_cast<const char*>(values), static_cast<size_t>(nof_values));
1640  return result;
1641  }
1642 
1643  DEVICE TextEncodingNone& operator=(const std::string& s) {
1644  extend(
1645  reinterpret_cast<const int8_t*>(s.data()), static_cast<int32_t>(s.size()), true);
1646  return *this;
1647  }
1648 
1649  DEVICE TextEncodingNone& operator+=(const std::string& s) {
1650  extend(
1651  reinterpret_cast<const int8_t*>(s.data()), static_cast<int32_t>(s.size()), false);
1652  return *this;
1653  }
1654 
1655  std::string getString() const {
1656  return str();
1657  }
1658 #endif
1659 };
1660 
1661 template <typename T> // T is scalar type
1662 struct Array : public NestedArray<T> {
1663  using NestedArray<T>::size;
1666  using NestedArray<T>::extend;
1667 
1668  T& operator[](const int64_t index) { return getValue(index); }
1669 
1670  DEVICE Array<T>& operator=(const ::Array<T>& s) {
1671  extend(
1672  reinterpret_cast<const int8_t*>(s.data()), static_cast<int32_t>(s.size()), true);
1673  return *this;
1674  }
1675 
1676  DEVICE Array<T>& operator+=(const ::Array<T>& s) {
1677  extend(
1678  reinterpret_cast<const int8_t*>(s.data()), static_cast<int32_t>(s.size()), false);
1679  return *this;
1680  }
1681 
1682  // For BC:
1683  DEVICE int64_t getSize() const { return size(); }
1684  DEVICE T operator()(const unsigned int index) const {
1685  return getItem(static_cast<int64_t>(index));
1686  }
1687  DEVICE T operator()(const int64_t index) const { return getItem(index); }
1688 };
1689 
1690 }; // namespace flatbuffer
1691 
1692 namespace Geo {
1693 
1694 struct Point2D {
1695  double x{std::numeric_limits<double>::quiet_NaN()};
1696  double y{std::numeric_limits<double>::quiet_NaN()};
1697 
1698 #ifdef HAVE_TOSTRING
1699  std::string toString() const {
1700  return ::typeName(this) + "(x=" + ::toString(x) + ", y=" + std::to_string(y) + ")";
1701  }
1702 #endif
1703 };
1704 
1705 DEVICE inline int32_t compress_x_coord(const double* x, const int64_t index) {
1706  return static_cast<int32_t>(Geospatial::compress_longitude_coord_geoint32(x[index]));
1707 }
1708 
1709 DEVICE inline int32_t compress_y_coord(const double* x, const int64_t index) {
1710  return static_cast<int32_t>(Geospatial::compress_latitude_coord_geoint32(x[index + 1]));
1711 }
1712 
1713 DEVICE inline double decompress_x_coord(const int8_t* data,
1714  const int64_t index,
1715  const bool is_geoint) {
1716  if (is_geoint) {
1718  reinterpret_cast<const int32_t*>(data)[index]);
1719  } else {
1720  return reinterpret_cast<const double*>(data)[index];
1721  }
1722 }
1723 
1724 DEVICE inline double decompress_y_coord(const int8_t* data,
1725  const int64_t index,
1726  const bool is_geoint) {
1727  if (is_geoint) {
1729  reinterpret_cast<const int32_t*>(data)[index + 1]);
1730  } else {
1731  return reinterpret_cast<const double*>(data)[index + 1];
1732  }
1733 }
1734 
1735 DEVICE inline double decompress_x_coord(const int8_t* data,
1736  const int64_t index,
1737  const bool is_geoint,
1738  const int32_t input_srid,
1739  const int32_t output_srid) {
1740  double x = decompress_x_coord(data, index, is_geoint);
1741  if (input_srid == output_srid || output_srid == 0) {
1742  return x;
1743  } else if (input_srid == 4326 && output_srid == 900913) {
1744  // WGS 84 --> Web Mercator
1745  x *= 111319.490778;
1746  } else {
1747 #ifndef __CUDACC__
1748  throw std::runtime_error("decompress_x_coord: unhandled geo transformation from " +
1749  std::to_string(input_srid) + " to " +
1750  std::to_string(output_srid) + '.');
1751 #endif
1752  }
1753  return x;
1754 }
1755 
1756 DEVICE inline double decompress_y_coord(const int8_t* data,
1757  const int64_t index,
1758  const bool is_geoint,
1759  const int32_t input_srid,
1760  const int32_t output_srid) {
1761  double y = decompress_y_coord(data, index, is_geoint);
1762  if (input_srid == output_srid || output_srid == 0) {
1763  return y;
1764  } else if (input_srid == 4326 && output_srid == 900913) {
1765  // WGS 84 --> Web Mercator
1766  y = 6378136.99911 * log(tan(.00872664626 * y + .785398163397));
1767  } else {
1768 #ifndef __CUDACC__
1769  throw std::runtime_error("decompress_y_coord: unhandled geo transformation from " +
1770  std::to_string(input_srid) + " to " +
1771  std::to_string(output_srid) + '.');
1772 #endif
1773  }
1774  return y;
1775 }
1776 
1777 DEVICE inline Point2D get_point(const int8_t* data,
1778  const int64_t index,
1779  const int32_t input_srid,
1780  const int32_t output_srid,
1781  const bool is_geoint) {
1782  Point2D point{decompress_x_coord(data, index, is_geoint, input_srid, output_srid),
1783  decompress_y_coord(data, index, is_geoint, input_srid, output_srid)};
1784  return point;
1785 }
1786 
1787 #ifndef __CUDACC__
1788 
1789 template <typename CT>
1790 inline void points_to_vector(const int8_t* points_buf,
1791  const int64_t nof_points,
1792  const bool is_geoint,
1793  std::vector<CT>& result) {
1794  result.reserve(2 * nof_points);
1795  if (is_geoint) {
1796  if constexpr (std::is_same<CT, double>::value) {
1797  for (int64_t i = 0; i < nof_points; i++) {
1798  result.push_back(decompress_x_coord(points_buf, 2 * i, is_geoint));
1799  result.push_back(decompress_y_coord(points_buf, 2 * i, is_geoint));
1800  }
1801  } else {
1802  const int32_t* buf = reinterpret_cast<const int32_t*>(points_buf);
1803  result.assign(buf, buf + 2 * nof_points);
1804  }
1805  } else {
1806  const double* buf = reinterpret_cast<const double*>(points_buf);
1807  if constexpr (std::is_same<CT, double>::value) {
1808  result.assign(buf, buf + 2 * nof_points);
1809  } else {
1810  for (int64_t i = 0; i < nof_points; i++) {
1811  result.push_back(compress_x_coord(buf, 2 * i));
1812  result.push_back(compress_y_coord(buf, 2 * i));
1813  }
1814  }
1815  }
1816 }
1817 
1818 inline std::vector<int32_t> compress_coords(const int8_t* data,
1819  const int64_t size,
1820  const bool is_geoint) {
1821  int64_t nofpoints = size / (is_geoint ? sizeof(int32_t) * 2 : sizeof(double) * 2);
1822  std::vector<int32_t> result;
1823  result.reserve(2 * nofpoints);
1824  if (is_geoint) {
1825  const int32_t* buf = reinterpret_cast<const int32_t*>(data);
1826  result.assign(buf, buf + 2 * nofpoints);
1827  } else {
1828  const double* buf = reinterpret_cast<const double*>(data);
1829  for (int64_t i = 0; i < nofpoints; i++) {
1830  result.push_back(compress_x_coord(buf, 2 * i));
1831  result.push_back(compress_y_coord(buf, 2 * i));
1832  }
1833  }
1834  return result;
1835 }
1836 
1837 inline std::vector<double> decompress_coords(const int8_t* data,
1838  const int64_t size,
1839  const bool is_geoint) {
1840  int64_t nofpoints = size / (is_geoint ? sizeof(int32_t) * 2 : sizeof(double) * 2);
1841  std::vector<double> result;
1842  result.reserve(2 * nofpoints);
1843  for (int64_t i = 0; i < nofpoints; i++) {
1844  result.push_back(decompress_x_coord(data, 2 * i, is_geoint));
1845  result.push_back(decompress_y_coord(data, 2 * i, is_geoint));
1846  }
1847  return result;
1848 }
1849 
1850 inline std::vector<int32_t> compress_coords(const std::vector<double>& coords) {
1851  std::vector<int32_t> result;
1852  const size_t nofpoints = coords.size() / 2;
1853  result.reserve(coords.size());
1854  const double* buf = coords.data();
1855  for (size_t i = 0; i < nofpoints; i++) {
1856  result.push_back(compress_x_coord(buf, 2 * i));
1857  result.push_back(compress_y_coord(buf, 2 * i));
1858  }
1859  return result;
1860 }
1861 
1862 inline std::vector<double> decompress_coords(const std::vector<int32_t>& coords) {
1863  std::vector<double> result;
1864  const size_t nofpoints = coords.size() / 2;
1865  result.reserve(coords.size());
1866  const int8_t* buf = reinterpret_cast<const int8_t*>(coords.data());
1867  for (size_t i = 0; i < nofpoints; i++) {
1868  result.push_back(decompress_x_coord(buf, 2 * i, true));
1869  result.push_back(decompress_y_coord(buf, 2 * i, true));
1870  }
1871  return result;
1872 }
1873 
1874 inline std::vector<std::vector<int32_t>> compress_coords(
1875  const std::vector<std::vector<double>>& coords) {
1876  std::vector<std::vector<int32_t>> result;
1877  result.reserve(coords.size());
1878  for (size_t i = 0; i < coords.size(); i++) {
1879  result.push_back(compress_coords(coords[i]));
1880  }
1881  return result;
1882 }
1883 
1884 inline std::vector<std::vector<double>> decompress_coords(
1885  const std::vector<std::vector<int32_t>>& coords) {
1886  std::vector<std::vector<double>> result;
1887  result.reserve(coords.size());
1888  for (size_t i = 0; i < coords.size(); i++) {
1889  result.push_back(decompress_coords(coords[i]));
1890  }
1891  return result;
1892 }
1893 
1894 inline std::vector<std::vector<std::vector<int32_t>>> compress_coords(
1895  const std::vector<std::vector<std::vector<double>>>& coords) {
1896  std::vector<std::vector<std::vector<int32_t>>> result;
1897  result.reserve(coords.size());
1898  for (size_t i = 0; i < coords.size(); i++) {
1899  result.push_back(compress_coords(coords[i]));
1900  }
1901  return result;
1902 }
1903 
1904 inline std::vector<std::vector<std::vector<double>>> decompress_coords(
1905  const std::vector<std::vector<std::vector<int32_t>>>& coords) {
1906  std::vector<std::vector<std::vector<double>>> result;
1907  result.reserve(coords.size());
1908  for (size_t i = 0; i < coords.size(); i++) {
1909  result.push_back(decompress_coords(coords[i]));
1910  }
1911  return result;
1912 }
1913 
1914 #endif
1915 
1916 // to be deprecated
1917 inline bool get_is_geoint(const int8_t* flatbuffer) {
1918  FlatBufferManager m{const_cast<int8_t*>(flatbuffer)};
1919  switch (m.format()) {
1920  case GeoPointFormatId:
1921  return m.getGeoPointMetadata()->is_geoint;
1922  default:
1923 #ifndef __CUDACC__
1924  throw std::runtime_error("get_is_geoint: unexpected format " +
1925  ::toString(m.format()));
1926 #else
1927  return false;
1928 #endif
1929  }
1930 }
1931 
1932 // to be deprecated
1933 inline int32_t get_input_srid(const int8_t* flatbuffer) {
1934  FlatBufferManager m{const_cast<int8_t*>(flatbuffer)};
1935  switch (m.format()) {
1936  case GeoPointFormatId:
1937  return m.getGeoPointMetadata()->input_srid;
1938  default:
1939 #ifndef __CUDACC__
1940  throw std::runtime_error("get_input_srid: unexpected format " +
1941  ::toString(m.format()));
1942 #else
1943  return -1;
1944 #endif
1945  }
1946 }
1947 
1948 // to be deprecated
1949 inline int32_t get_output_srid(const int8_t* flatbuffer) {
1950  FlatBufferManager m{const_cast<int8_t*>(flatbuffer)};
1951  switch (m.format()) {
1952  case GeoPointFormatId:
1953  return m.getGeoPointMetadata()->output_srid;
1954  default:
1955 #ifndef __CUDACC__
1956  throw std::runtime_error("get_output_srid: unexpected format " +
1957  ::toString(m.format()));
1958 #else
1959  return -1;
1960 #endif
1961  }
1962 }
1963 
1964 template <typename ItemType>
1965 struct GeoNestedArray : public flatbuffer::NestedArray<ItemType> {
1973 
1974  DEVICE Point2D getPoint(const int64_t index) const {
1975  int8_t* values;
1976  int64_t nof_values;
1977  bool is_null;
1978  size_t value_size;
1979  getRawBuffer(values, nof_values, value_size, is_null);
1980  if (is_null) {
1981  return {};
1982  }
1983  const auto* ti = getTypeInfo();
1984  return get_point(
1985  values, 2 * index, ti->get_input_srid(), ti->get_output_srid(), ti->is_geoint());
1986  }
1987 
1988 #ifndef __CUDACC__
1989 
1990  template <typename CT, typename VT>
1992  if constexpr (std::is_same<CT, VT>::value) {
1993  int8_t* values;
1994  int64_t nof_values;
1995  bool is_null;
1996  size_t value_size;
1997  getRawBuffer(values, nof_values, value_size, is_null);
1998  const auto* ti = getTypeInfo();
1999  points_to_vector(values, nof_values, ti->is_geoint(), result);
2000  return FlatBufferManager::Status::Success;
2001  } else {
2002  auto sz = size();
2003  result.reserve(sz);
2004  for (size_t i = 0; i < sz; i++) {
2005  auto item = getItem(i);
2006  VT ritem;
2007  auto status = item.toCoords(ritem);
2008  if (status != FlatBufferManager::Status::Success) {
2009  return status;
2010  }
2011  result.push_back(ritem);
2012  }
2013  return FlatBufferManager::Status::Success;
2014  }
2015  }
2016 
2017  // Return coordinates as a vector of double or int32_t type
2018  template <typename CT>
2019  FlatBufferManager::Status toCoords(std::vector<CT>& result) const {
2020  return toCoordsWorker<CT, CT>(result);
2021  }
2022 
2023  template <typename CT>
2024  FlatBufferManager::Status toCoords(std::vector<std::vector<CT>>& result) const {
2025  return toCoordsWorker<CT, std::vector<CT>>(result);
2026  }
2027 
2028  template <typename CT>
2030  std::vector<std::vector<std::vector<CT>>>& result) const {
2031  return toCoordsWorker<CT, std::vector<std::vector<CT>>>(result);
2032  }
2033 
2034  template <typename CT, typename VT>
2037  const auto* ti = getTypeInfo();
2038  if (ti == nullptr) {
2040  }
2041  if (n_ != 1) {
2042  throw std::runtime_error(
2043  "NestedArray fromCoords failed: expected single index but got " +
2044  ::toString(n_));
2045  }
2046  if (ti->is_geoint()) {
2047  if constexpr (std::is_same<CT, double>::value) {
2048  const auto ccoords = compress_coords(coords);
2049  return m.setItem(index_[0], ccoords);
2050  } else {
2051  return m.setItem(index_[0], coords);
2052  }
2053  } else {
2054  if constexpr (std::is_same<CT, double>::value) {
2055  return m.setItem(index_[0], coords);
2056  } else {
2057  const auto dcoords = decompress_coords(coords);
2058  return m.setItem(index_[0], dcoords);
2059  }
2060  }
2061  }
2062 
2063  // Create row from a nested vector of coordinates either in double or int32_t type
2064  template <typename CT>
2065  FlatBufferManager::Status fromCoords(const std::vector<CT>& coords) {
2066  return fromCoordsWorker<CT, std::vector<CT>>(coords);
2067  }
2068 
2069  template <typename CT>
2070  FlatBufferManager::Status fromCoords(const std::vector<std::vector<CT>>& coords) {
2071  return fromCoordsWorker<CT, std::vector<std::vector<CT>>>(coords);
2072  }
2073 
2074  template <typename CT>
2076  const std::vector<std::vector<std::vector<CT>>>& coords) {
2077  return fromCoordsWorker<CT, std::vector<std::vector<std::vector<CT>>>>(coords);
2078  }
2079 
2080 #endif
2081 };
2082 
2083 struct LineString : public GeoNestedArray<Point2D> {
2084  DEVICE inline Point2D operator[](const unsigned int index) const {
2085  return getPoint(static_cast<int64_t>(index));
2086  }
2087 
2088 #ifndef __CUDACC__
2089 
2091 
2092  template <typename CT>
2093  std::vector<CT> toCoords() const {
2094  std::vector<CT> result;
2095  auto status = toCoords(result);
2096  if (status != FlatBufferManager::Status::Success) {
2097 #ifndef __CUDACC__
2098  throw std::runtime_error(::typeName(this) +
2099  " toCoords failed: " + ::toString(status));
2100 #endif
2101  }
2102  return result;
2103  }
2104 
2105  std::vector<double> toCoords() const {
2106  return toCoords<double>();
2107  }
2108 
2109 #endif
2110 };
2111 
2112 struct MultiPoint : public GeoNestedArray<Point2D> {
2113  DEVICE inline Point2D operator[](const unsigned int index) const {
2114  return getPoint(static_cast<int64_t>(index));
2115  }
2116 
2117 #ifndef __CUDACC__
2118 
2120 
2121  template <typename CT>
2122  std::vector<CT> toCoords() const {
2123  std::vector<CT> result;
2124  auto status = toCoords(result);
2125  if (status != FlatBufferManager::Status::Success) {
2126 #ifndef __CUDACC__
2127  throw std::runtime_error(::typeName(this) +
2128  " toCoords failed: " + ::toString(status));
2129 #endif
2130  }
2131  return result;
2132  }
2133 
2134  std::vector<double> toCoords() const {
2135  return toCoords<double>();
2136  }
2137 
2138 #endif
2139 };
2140 
2141 struct MultiLineString : public GeoNestedArray<LineString> {
2142 #ifndef __CUDACC__
2143 
2145 
2146  template <typename CT>
2147  std::vector<std::vector<CT>> toCoords() const {
2148  std::vector<std::vector<CT>> result;
2149  auto status = toCoords(result);
2150  if (status != FlatBufferManager::Status::Success) {
2151  throw std::runtime_error(::typeName(this) +
2152  " toCoords failed: " + ::toString(status));
2153  }
2154  return result;
2155  }
2156 
2157  std::vector<std::vector<double>> toCoords() const {
2158  return toCoords<double>();
2159  }
2160 
2161 #endif
2162 };
2163 
2164 struct Polygon : public GeoNestedArray<LineString> {
2165 #ifndef __CUDACC__
2166 
2168 
2169  template <typename CT>
2170  std::vector<std::vector<CT>> toCoords() const {
2171  std::vector<std::vector<CT>> result;
2172  auto status = toCoords(result);
2173  if (status != FlatBufferManager::Status::Success) {
2174  throw std::runtime_error(::typeName(this) +
2175  " toCoords failed: " + ::toString(status));
2176  }
2177  return result;
2178  }
2179 
2180  std::vector<std::vector<double>> toCoords() const {
2181  return toCoords<double>();
2182  }
2183 
2184 #endif
2185 };
2186 
2187 struct MultiPolygon : public GeoNestedArray<Polygon> {
2188 #ifndef __CUDACC__
2189 
2191 
2192  template <typename CT>
2193  std::vector<std::vector<std::vector<CT>>> toCoords() const {
2194  std::vector<std::vector<std::vector<CT>>> result;
2195  auto status = toCoords(result);
2196  if (status != FlatBufferManager::Status::Success) {
2197  throw std::runtime_error(::typeName(this) +
2198  " toCoords failed: " + ::toString(status));
2199  }
2200  return result;
2201  }
2202 
2203  std::vector<std::vector<std::vector<double>>> toCoords() const {
2204  return toCoords<double>();
2205  }
2206 
2207 #endif
2208 };
2209 
2210 } // namespace Geo
2211 
2212 template <>
2213 struct Column<GeoLineString> : public flatbuffer::Column<Geo::LineString, GeoLineString> {
2214 };
2215 
2216 template <>
2217 struct Column<GeoMultiPoint> : public flatbuffer::Column<Geo::MultiPoint, GeoMultiPoint> {
2218 };
2219 
2220 template <>
2221 struct Column<GeoPolygon> : public flatbuffer::Column<Geo::Polygon, GeoPolygon> {};
2222 
2223 template <>
2225  : public flatbuffer::Column<Geo::MultiLineString, GeoMultiLineString> {};
2226 
2227 template <>
2229  : public flatbuffer::Column<Geo::MultiPolygon, GeoMultiPolygon> {};
2230 
2231 template <>
2232 struct Column<GeoPoint> {
2233  int8_t* flatbuffer_; // contains a flat buffer of the storage, use FlatBufferManager
2234  // to access it.
2235  int64_t num_rows_; // total row count, the number of all varlen arrays
2236 
2237  DEVICE Geo::Point2D getItem(const int64_t index, const int32_t output_srid = 0) const {
2238  /*
2239  output_srid != 0 enables transformation from input_srid to the
2240  specified output_srid. If output_srid < 0, the column's
2241  output_srid will be used, otherwise, the user specified
2242  output_srid is used.
2243  */
2244  FlatBufferManager m{flatbuffer_};
2245  int8_t* ptr;
2246  int64_t size;
2247  bool is_null;
2248  auto status = m.getItemOld(index, size, ptr, is_null);
2249  if (status != FlatBufferManager::Status::Success) {
2250 #ifndef __CUDACC__
2251  throw std::runtime_error("getItem failed: " + ::toString(status));
2252 #endif
2253  }
2254  bool is_geoint = Geo::get_is_geoint(flatbuffer_);
2255  int32_t this_input_srid = Geo::get_input_srid(flatbuffer_);
2256  int32_t this_output_srid = Geo::get_output_srid(flatbuffer_);
2257  return Geo::get_point(ptr,
2258  0,
2259  this_input_srid,
2260  (output_srid < 0 ? this_output_srid : output_srid),
2261  is_geoint);
2262  }
2263 
2264  DEVICE inline Geo::Point2D operator[](const unsigned int index) const {
2265  /* Use getItem(index, output_srid) to enable user-specified
2266  transformation. */
2267  return getItem(static_cast<int64_t>(index), /*output_srid=*/0);
2268  }
2269 
2270  DEVICE int64_t size() const {
2271  return num_rows_;
2272  }
2273 
2274  DEVICE inline bool isNull(int64_t index) const {
2275  FlatBufferManager m{flatbuffer_};
2276  bool is_null = false;
2277  auto status = m.isNull(index, is_null);
2278 #ifndef __CUDACC__
2279  if (status != FlatBufferManager::Status::Success) {
2280  throw std::runtime_error("isNull failed: " + ::toString(status));
2281  }
2282 #endif
2283  return is_null;
2284  }
2285 
2286  DEVICE inline void setNull(int64_t index) {
2287  FlatBufferManager m{flatbuffer_};
2288  auto status = m.setNull(index);
2289 #ifndef __CUDACC__
2290  if (status != FlatBufferManager::Status::Success) {
2291  throw std::runtime_error("setNull failed: " + ::toString(status));
2292  }
2293 #endif
2294  }
2295 
2296  DEVICE inline void setItem(int64_t index, const Geo::Point2D& other) {
2297  FlatBufferManager m{flatbuffer_};
2298  const auto* metadata = m.getGeoPointMetadata();
2299  int8_t* dest = nullptr;
2300  int64_t sz = 2 * (metadata->is_geoint ? sizeof(int32_t) : sizeof(double));
2301  FlatBufferManager::Status status = m.setItemOld(index, nullptr, sz, &dest);
2302  if (status != FlatBufferManager::Status::Success) {
2303 #ifndef __CUDACC__
2304  throw std::runtime_error("setItem failed: " + ::toString(status));
2305 #endif
2306  }
2307  if (dest == nullptr) {
2308 #ifndef __CUDACC__
2309  throw std::runtime_error("setItem failed: dest is not set?!");
2310 #endif
2311  }
2312  if (metadata->is_geoint) {
2313  // TODO: check other.x/y ranges
2314  int32_t* ptr = reinterpret_cast<int32_t*>(dest);
2317  } else {
2318  double* ptr = reinterpret_cast<double*>(dest);
2319  ptr[0] = other.x;
2320  ptr[1] = other.y;
2321  }
2322  }
2323 
2324 #ifdef HAVE_FLATBUFFER_TOSTRING
2325  std::string toString() const {
2326  FlatBufferManager m{flatbuffer_};
2327  return ::typeName(this) + "(" + m.toString() +
2328  ", num_rows=" + std::to_string(num_rows_) + ")";
2329  }
2330 #endif
2331 };
2332 
2333 template <>
2335  : public flatbuffer::Column<flatbuffer::TextEncodingNone, TextEncodingNone> {};
2336 
2337 template <typename T>
2338 struct Column<Array<T>> : public flatbuffer::Column<flatbuffer::Array<T>, Array<T>> {
2339  Column<Array<T>>(int8_t* flatbuffer, int64_t num_rows)
2340  : flatbuffer::Column<flatbuffer::Array<T>, Array<T>>(flatbuffer, num_rows) {}
2341 };
2342 
2343 template <>
2345  : public flatbuffer::Column<flatbuffer::Array<TextEncodingDict>,
2346  Array<TextEncodingDict>> {
2347  Column<Array<TextEncodingDict>>(int8_t* flatbuffer, int64_t num_rows)
2349  flatbuffer,
2350  num_rows) {}
2351 
2352  DEVICE inline int32_t getDictDbId() const {
2353  const auto* ti = getTypeInfo();
2354  return (ti ? ti->db_id : 0);
2355  }
2356 
2357  DEVICE inline int32_t getDictId() const {
2358  const auto* ti = getTypeInfo();
2359  return (ti ? ti->dict_id : 0);
2360  }
2361 };
2362 
2363 template <>
2365  TextEncodingDict* ptr_; // row data
2366  int64_t num_rows_; // row count
2367 #ifndef __CUDACC__
2368 #ifndef UDF_COMPILED
2370  DEVICE Column(const Column& other)
2371  : ptr_(other.ptr_)
2372  , num_rows_(other.num_rows_)
2373  , string_dict_proxy_(other.string_dict_proxy_) {}
2375  const int64_t num_rows,
2376  StringDictionaryProxy* string_dict_proxy)
2377  : ptr_(ptr), num_rows_(num_rows), string_dict_proxy_(string_dict_proxy) {}
2378  DEVICE Column(std::vector<TextEncodingDict>& input_vec)
2379  : ptr_(input_vec.data())
2380  , num_rows_(static_cast<int64_t>(input_vec.size()))
2381  , string_dict_proxy_(nullptr) {}
2382 #else
2383  DEVICE Column(TextEncodingDict* ptr, const int64_t num_rows)
2384  : ptr_(ptr), num_rows_(num_rows) {}
2385 #endif // #ifndef UDF_COMPILED
2386 #else
2387  DEVICE Column(TextEncodingDict* ptr, const int64_t num_rows)
2388  : ptr_(ptr), num_rows_(num_rows) {}
2389 #endif // #ifndef __CUDACC__
2390 
2391  DEVICE TextEncodingDict& operator[](const unsigned int index) const {
2392  if (index >= num_rows_) {
2393 #ifndef __CUDACC__
2394  throw std::runtime_error("column buffer index is out of range");
2395 #else
2396  static DEVICE TextEncodingDict null_value;
2397  set_null(null_value.value);
2398  return null_value;
2399 #endif
2400  }
2401  return ptr_[index];
2402  }
2403  DEVICE inline TextEncodingDict* getPtr() const {
2404  return ptr_;
2405  }
2406  DEVICE inline int64_t size() const {
2407  return num_rows_;
2408  }
2409  DEVICE inline void setSize(int64_t num_rows) {
2410  num_rows_ = num_rows;
2411  }
2412 
2413  DEVICE inline bool isNull(int64_t index) const {
2414  return is_null(ptr_[index].value);
2415  }
2416 
2417  DEVICE inline void setNull(int64_t index) {
2418  set_null(ptr_[index].value);
2419  }
2420 
2421 #ifndef __CUDACC__
2422 #ifndef UDF_COMPILED
2423  DEVICE inline int32_t getDictDbId() const {
2424  return string_dict_proxy_->getDictKey().db_id;
2425  }
2426  DEVICE inline int32_t getDictId() const {
2427  return string_dict_proxy_->getDictKey().dict_id;
2428  }
2429  DEVICE inline const std::string getString(int64_t index) const {
2430  return isNull(index) ? "" : string_dict_proxy_->getString(ptr_[index].value);
2431  }
2432  DEVICE inline const TextEncodingDict getOrAddTransient(const std::string& str) {
2433  return string_dict_proxy_->getOrAddTransient(str);
2434  }
2435 #endif // #ifndef UDF_COMPILED
2436 #endif // #ifndef __CUDACC__
2437 
2439 #ifndef __CUDACC__
2440  if (size() == other.size()) {
2441  memcpy(ptr_, other.ptr_, other.size() * sizeof(TextEncodingDict));
2442  } else {
2443  throw std::runtime_error("cannot copy assign columns with different sizes");
2444  }
2445 #else
2446  if (size() == other.size()) {
2447  for (unsigned int i = 0; i < size(); i++) {
2448  ptr_[i] = other[i];
2449  }
2450  } else {
2451  // TODO: set error
2452  }
2453 #endif
2454  return *this;
2455  }
2456 
2457 #ifdef HAVE_TOSTRING
2458  std::string toString() const {
2459  return ::typeName(this) + "(ptr=" + ::toString(reinterpret_cast<void*>(ptr_)) +
2460  ", num_rows=" + std::to_string(num_rows_) + ")";
2461  }
2462 #endif
2463 };
2464 
2465 template <>
2466 DEVICE inline bool Column<Timestamp>::isNull(int64_t index) const {
2467  return is_null(ptr_[index].time);
2468 }
2469 
2470 template <>
2471 DEVICE inline void Column<Timestamp>::setNull(int64_t index) {
2472  set_null(ptr_[index].time);
2473 }
2474 
2475 template <>
2477  set_null(t.time);
2478 }
2479 
2480 /*
2481  ColumnList is an ordered list of Columns.
2482 */
2483 template <typename T>
2484 struct ColumnList {
2485  int8_t** ptrs_; // ptrs to columns data
2486  int64_t num_cols_; // the length of columns list
2487  int64_t num_rows_; // the number of rows of columns
2488 
2489  DEVICE ColumnList(int8_t** ptrs, const int64_t num_cols, const int64_t num_rows)
2490  : ptrs_(ptrs), num_cols_(num_cols), num_rows_(num_rows) {}
2491 
2492  DEVICE int64_t size() const { return num_rows_; }
2493  DEVICE int64_t numCols() const { return num_cols_; }
2494  DEVICE Column<T> operator[](const int index) const {
2495  if (index >= 0 && index < num_cols_)
2496  return {reinterpret_cast<T*>(ptrs_[index]), num_rows_};
2497  else
2498  return {nullptr, -1};
2499  }
2500 
2501 #ifdef HAVE_TOSTRING
2502 
2503  std::string toString() const {
2504  std::string result = ::typeName(this) + "(ptrs=[";
2505  for (int64_t index = 0; index < num_cols_; index++) {
2506  result += ::toString(reinterpret_cast<void*>(ptrs_[index])) +
2507  (index < num_cols_ - 1 ? ", " : "");
2508  }
2509  result += "], num_cols=" + std::to_string(num_cols_) +
2510  ", num_rows=" + std::to_string(num_rows_) + ")";
2511  return result;
2512  }
2513 
2514 #endif
2515 };
2516 
2517 template <typename T>
2518 struct ColumnList<Array<T>> {
2519  int8_t** ptrs_; // ptrs to columns data in FlatBuffer format
2520  int64_t num_cols_; // the length of columns list
2521  int64_t num_rows_; // the size of columns
2522 
2523  DEVICE int64_t size() const { return num_rows_; }
2524  DEVICE int64_t numCols() const { return num_cols_; }
2525  DEVICE Column<Array<T>> operator[](const int index) const {
2526  int8_t* ptr = ((index >= 0 && index < num_cols_) ? ptrs_[index] : nullptr);
2527  int64_t num_rows = ((index >= 0 && index < num_cols_) ? num_rows_ : -1);
2528  Column<Array<T>> result(ptr, num_rows);
2529  return result;
2530  }
2531 
2532 #ifdef HAVE_TOSTRING
2533 
2534  std::string toString() const {
2535  std::string result = ::typeName(this) + "(ptrs=[";
2536  for (int64_t index = 0; index < num_cols_; index++) {
2537  result += ::toString(reinterpret_cast<void*>(ptrs_[index])) +
2538  (index < num_cols_ - 1 ? ", " : "");
2539  }
2540  result += "], num_cols=" + std::to_string(num_cols_) +
2541  ", num_rows=" + std::to_string(num_rows_) + ")";
2542  return result;
2543  }
2544 
2545 #endif
2546 };
2547 
2548 template <>
2550  int8_t** ptrs_; // ptrs to columns data
2551  int64_t num_cols_; // the length of columns list
2552  int64_t num_rows_; // the size of columns
2553 #ifndef __CUDACC__
2554 #ifndef UDF_COMPILED
2556  DEVICE ColumnList(int8_t** ptrs,
2557  const int64_t num_cols,
2558  const int64_t num_rows,
2559  StringDictionaryProxy** string_dict_proxies)
2560  : ptrs_(ptrs)
2561  , num_cols_(num_cols)
2562  , num_rows_(num_rows)
2563  , string_dict_proxies_(string_dict_proxies) {}
2564 #else
2565  DEVICE ColumnList(int8_t** ptrs, const int64_t num_cols, const int64_t num_rows)
2566  : ptrs_(ptrs), num_cols_(num_cols), num_rows_(num_rows) {}
2567 #endif // #ifndef UDF_COMPILED
2568 #else
2569  DEVICE ColumnList(int8_t** ptrs, const int64_t num_cols, const int64_t num_rows)
2570  : ptrs_(ptrs), num_cols_(num_cols), num_rows_(num_rows) {}
2571 #endif // #ifndef __CUDACC__
2572 
2573  DEVICE int64_t size() const {
2574  return num_rows_;
2575  }
2576  DEVICE int64_t numCols() const {
2577  return num_cols_;
2578  }
2579  DEVICE Column<TextEncodingDict> operator[](const int index) const {
2580  if (index >= 0 && index < num_cols_) {
2581  Column<TextEncodingDict> result(reinterpret_cast<TextEncodingDict*>(ptrs_[index]),
2582  num_rows_
2583 #ifndef __CUDACC__
2584 #ifndef UDF_COMPILED
2585  ,
2586  string_dict_proxies_[index]
2587 #endif // #ifndef UDF_COMPILED
2588 #endif // #ifndef __CUDACC__
2589  );
2590  return result;
2591  } else {
2593  -1
2594 #ifndef __CUDACC__
2595 #ifndef UDF_COMPILED
2596  ,
2597  nullptr
2598 #endif // #ifndef UDF_COMPILED
2599 #endif // #ifndef__CUDACC__
2600  );
2601  return result;
2602  }
2603  }
2604 
2605 #ifdef HAVE_TOSTRING
2606 
2607  std::string toString() const {
2608  std::string result = ::typeName(this) + "(ptrs=[";
2609  for (int64_t index = 0; index < num_cols_; index++) {
2610  result += ::toString(reinterpret_cast<void*>(ptrs_[index])) +
2611  (index < num_cols_ - 1 ? ", " : "");
2612  }
2613  result += "], num_cols=" + std::to_string(num_cols_) +
2614  ", num_rows=" + std::to_string(num_rows_) + ")";
2615  return result;
2616  }
2617 
2618 #endif
2619 };
2620 
2621 #endif // #ifndef UDF_COMPILED
DEVICE RowStruct operator()(const unsigned int index) const
std::string toString() const
DEVICE const std::string getString(int64_t index) const
FlatBufferManager::Status toCoordsWorker(std::vector< VT > &result) const
EXTENSION_NOINLINE_HOST int32_t RowFunctionManager_getDictDbId(int8_t *mgr_ptr, const char *func_name, size_t arg_idx)
void set_output_row_size(int64_t num_rows)
Definition: heavydbTypes.h:373
DEVICE int64_t getSize() const
Definition: heavydbTypes.h:954
std::string toString() const
std::string getString(int32_t db_id, int32_t dict_id, int32_t string_id)
Definition: heavydbTypes.h:422
DEVICE Array(const flatbuffer::Array< T > &arr)
Definition: heavydbTypes.h:510
DEVICE ALWAYS_INLINE Timestamp truncateToSeconds() const
Definition: heavydbTypes.h:783
DEVICE int32_t getDictDbId() const
FlatBufferManager::Status fromCoords(const std::vector< std::vector< std::vector< CT >>> &coords)
DEVICE int32_t getCoordsSize() const
DEVICE ALWAYS_INLINE Timestamp truncateToDay() const
Definition: heavydbTypes.h:795
std::vector< double > decompress_coords(const int8_t *data, const int64_t size, const bool is_geoint)
const ItemType getItem(const int64_t index) const
const ItemType operator[](const int64_t index) const
void set_output_array_values_total_number(int32_t index, int64_t output_array_values_total_number)
Definition: heavydbTypes.h:361
DEVICE int64_t getSize() const
bool isNull(int64_t index) const
#define IS_VALUE_TYPE(T)
Definition: heavydbTypes.h:454
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:460
DEVICE double decompress_x_coord(const int8_t *data, const int64_t index, const bool is_geoint)
EXTENSION_NOINLINE_HOST void set_output_array_values_total_number(int32_t index, int64_t output_array_values_total_number)
FlatBufferManager::Status fromCoords(const std::vector< std::vector< CT >> &coords)
int64_t index_[NESTED_ARRAY_NDIM]
DEVICE Column(std::vector< T > &input_vec)
DEVICE ALWAYS_INLINE bool operator!=(const int64_t &other) const
Definition: heavydbTypes.h:255
EXTENSION_NOINLINE_HOST void set_output_row_size(int64_t num_rows)
DEVICE RowType getItem(const int64_t index) const
#define EXTENSION_NOINLINE
Definition: heavydbTypes.h:58
DEVICE ALWAYS_INLINE const Timestamp operator+(const Timestamp &other) const
Definition: heavydbTypes.h:714
DEVICE double decompress_latitude_coord_geoint32(const int32_t compressed)
DEVICE uint64_t compress_longitude_coord_geoint32(const double coord)
DEVICE int32_t getCompression() const
static constexpr int64_t kNanoSecsPerSec
DEVICE int32_t getInputSrid() const
Definition: heavydbTypes.h:958
int32_t compression
Definition: heavydbTypes.h:950
DEVICE int32_t getInputSrid() const
void get_metadata(const std::string &key, T &value)
Definition: heavydbTypes.h:397
const SQLTypeInfoLite * getTypeInfo() const
FlatBufferManager::Status toCoords(std::vector< std::vector< CT >> &result) const
EXTENSION_NOINLINE_HOST int32_t RowFunctionManager_getDictId(int8_t *mgr_ptr, const char *func_name, size_t arg_idx)
EXTENSION_NOINLINE_HOST int32_t table_function_success_code()
DEVICE ALWAYS_INLINE bool operator==(const YearMonthTimeInterval &other) const
Definition: heavydbTypes.h:906
EXTENSION_NOINLINE_HOST int8_t * TableFunctionManager_get_singleton()
EXTENSION_NOINLINE_HOST void ColumnArray_setNull(int8_t *flatbuffer, int64_t index)
DEVICE int8_t * getRingSizes()
DEVICE double decompress_y_coord(const int8_t *data, const int64_t index, const bool is_geoint)
std::string getString() const
Definition: heavydbTypes.h:641
EXTENSION_NOINLINE_HOST void ColumnArray_getArray(int8_t *flatbuffer, const int64_t index, const int64_t expected_numel, int8_t *&ptr, int64_t &size, bool &is_null)
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_getNewDictDbId(int8_t *mgr_ptr)
DEVICE ALWAYS_INLINE bool operator<(const TextEncodingDict &other) const
Definition: heavydbTypes.h:259
int32_t getOrAddTransient(int32_t db_id, int32_t dict_id, std::string str)
Definition: heavydbTypes.h:426
Simplified core of GeoJSON Polygon coordinates definition.
std::vector< std::vector< std::vector< CT > > > toCoords() const
DEVICE ALWAYS_INLINE int64_t getMicroseconds() const
Definition: heavydbTypes.h:811
DEVICE Point2D getPoint(const int64_t index) const
DEVICE ALWAYS_INLINE char * data() const
Definition: heavydbTypes.h:685
DEVICE YearMonthTimeInterval(int64_t init)
Definition: heavydbTypes.h:904
TextEncodingDict operator=(const int32_t other)
Definition: heavydbTypes.h:227
DEVICE int64_t size() const
int32_t getNewDictDbId()
Definition: heavydbTypes.h:411
EXTENSION_NOINLINE_HOST int32_t RowFunctionManager_getOrAddTransient(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id, std::string str)
DEVICE int32_t getSize() const
Definition: heavydbTypes.h:990
CONSTEXPR DEVICE void set_null< Timestamp >(Timestamp &t)
std::string toString() const
DEVICE int32_t getNumLineStrings() const
std::vector< std::vector< double > > toCoords() const
EXTENSION_NOINLINE_HOST void ColumnArray_concatItem(int8_t *flatbuffer, int64_t index, const int8_t *ptr, int64_t size, bool is_null, int64_t sizeof_T)
std::vector< CT > toCoords() const
int64_t num_rows_
std::vector< std::vector< CT > > toCoords() const
EXTENSION_NOINLINE_HOST void TableFunctionManager_set_output_item_values_total_number(int8_t *mgr_ptr, int32_t index, int64_t output_item_values_total_number)
std::string toString() const
Definition: heavydbTypes.h:585
DEVICE void concatItem(int64_t index, const RowType &item)
DEVICE int64_t DateTruncate(DatetruncField field, const int64_t timeval)
int32_t get_output_srid(const int8_t *flatbuffer)
DEVICE int64_t size() const
DEVICE Timestamp(std::string_view const str)
Definition: heavydbTypes.h:709
DEVICE int64_t numCols() const
DEVICE T operator()(const int64_t index) const
DEVICE ALWAYS_INLINE int64_t getYear() const
Definition: heavydbTypes.h:834
EXTENSION_NOINLINE_HOST void ColumnArray_setItem(int8_t *flatbuffer, int64_t index, const int8_t *ptr, int64_t size, bool is_null, int64_t sizeof_T)
int32_t getDictId(const char *func_name, size_t arg_idx)
Definition: heavydbTypes.h:309
int64_t time
Definition: heavydbTypes.h:699
DEVICE ColumnList(int8_t **ptrs, const int64_t num_cols, const int64_t num_rows)
DEVICE int32_t getOutputSrid() const
Definition: heavydbTypes.h:996
DEVICE void setItem(int64_t index, const RowStruct &item)
DEVICE T * getPtr() const
void getRawBuffer(int8_t *&values, int64_t &nof_values, size_t &value_size, bool &is_null, bool require_1d_item=true) const
DEVICE Column< T > & operator=(const Column< T > &other)
size_t size() const
DEVICE Column< T > operator[](const int index) const
const SQLTypeInfoLite * getTypeInfo() const
DEVICE ALWAYS_INLINE bool operator!=(const DayTimeInterval &other) const
Definition: heavydbTypes.h:862
DEVICE int64_t size() const
int8_t is_null_
Definition: heavydbTypes.h:473
EXTENSION_NOINLINE_HOST void ColumnArray_concatArray(int8_t *flatbuffer, int64_t index, const int8_t *ptr, int64_t size, bool is_null)
DEVICE int32_t getDictDbId() const
int32_t get_input_srid(const int8_t *flatbuffer)
RUNTIME_EXPORT DEVICE int64_t DateAddHighPrecisionNullable(const DateaddField field, const int64_t number, const int64_t timeval, const int32_t dim, const int64_t null_val)
DEVICE int32_t getCompression() const
Definition: heavydbTypes.h:956
std::vector< std::vector< double > > toCoords() const
DEVICE void setNull(int64_t index)
DEVICE ALWAYS_INLINE Timestamp truncateToHours() const
Definition: heavydbTypes.h:791
DEVICE ALWAYS_INLINE int64_t getDay() const
Definition: heavydbTypes.h:828
DEVICE ALWAYS_INLINE Timestamp operator+(const Timestamp &t) const
Definition: heavydbTypes.h:914
EXTENSION_NOINLINE_HOST void TableFunctionManager_get_metadata(int8_t *mgr_ptr, const char *key, const uint8_t *&raw_bytes, size_t &num_bytes, TableFunctionMetadataType &value_type)
DEVICE void concatItem(int64_t index, const RowStruct &item)
DEVICE int8_t * getPolygonSizes()
EXTENSION_NOINLINE_HOST int8_t * RowFunctionManager_makeBuffer(int8_t *mgr_ptr, int64_t count, int64_t size)
DEVICE ALWAYS_INLINE bool operator==(const Timestamp &other) const
Definition: heavydbTypes.h:763
DEVICE ALWAYS_INLINE bool operator!=(const YearMonthTimeInterval &other) const
Definition: heavydbTypes.h:910
std::vector< CT > toCoords() const
DEVICE int64_t size() const
DEVICE Point2D get_point(const int8_t *data, const int64_t index, const int32_t input_srid, const int32_t output_srid, const bool is_geoint)
std::string toString() const
Definition: heavydbTypes.h:838
DEVICE int8_t * getLineStringSizes()
std::string to_string(char const *&&v)
DEVICE void setNull(int64_t index)
DEVICE ALWAYS_INLINE int64_t size() const
Definition: heavydbTypes.h:688
Simplified core of GeoJSON MultiPolygon coordinates definition.
#define DEVICE
DEVICE ALWAYS_INLINE Timestamp truncateToYear() const
Definition: heavydbTypes.h:803
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_error_message(int8_t *mgr_ptr, const char *message)
DEVICE ALWAYS_INLINE bool operator==(const TextEncodingDict &other) const
Definition: heavydbTypes.h:236
static constexpr int64_t kMilliSecsPerSec
EXTENSION_NOINLINE_HOST int8_t * TableFunctionManager_getStringDictionaryProxy(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id)
#define HOST
size_t size(const int64_t index) const
DEVICE TextEncodingNone & operator+=(const std::string &s)
std::vector< std::vector< std::vector< double > > > toCoords() const
DEVICE Geo::Point2D getItem(const int64_t index, const int32_t output_srid=0) const
HOST Array(M &mgr, const int64_t size, const bool is_null=false)
Definition: heavydbTypes.h:481
DEVICE int32_t getDictId() const
int32_t error_message(const char *message)
Definition: heavydbTypes.h:383
DEVICE int32_t compress_x_coord(const double *x, const int64_t index)
DEVICE Array< T > & operator+=(const ::Array< T > &s)
EXTENSION_NOINLINE int8_t * allocate_varlen_buffer(int64_t element_count, int64_t element_size)
DEVICE T & operator[](const unsigned int index) const
TableFunctionMetadataType get_metadata_type()
Definition: heavydbTypes.h:329
DEVICE uint64_t compress_latitude_coord_geoint32(const double coord)
CONSTEXPR DEVICE bool is_null(const T &value)
std::string TableFunctionManager_getString(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id, int32_t string_id)
EXTENSION_NOINLINE_HOST int8_t * TableFunctionManager_makeBuffer(int8_t *mgr_ptr, int64_t count, int64_t size)
StringDictionaryProxy ** string_dict_proxies_
#define CONSTEXPR
DEVICE Array(const int64_t size, const bool is_null=false)
Definition: heavydbTypes.h:497
DEVICE TextEncodingDict inline_null_value()
Definition: heavydbTypes.h:279
DEVICE ALWAYS_INLINE bool operator<(const int32_t &other) const
Definition: heavydbTypes.h:263
DEVICE Column< Array< T > > operator[](const int index) const
void init(LogOptions const &log_opts)
Definition: Logger.cpp:364
DEVICE int32_t getDictId() const
DEVICE ALWAYS_INLINE bool operator!=(const int32_t &other) const
Definition: heavydbTypes.h:251
std::string toString() const
Definition: heavydbTypes.h:272
DEVICE int32_t compress_y_coord(const double *x, const int64_t index)
std::vector< double > toCoords() const
DEVICE ALWAYS_INLINE Timestamp operator+(const Timestamp &t) const
Definition: heavydbTypes.h:866
DEVICE ALWAYS_INLINE bool isNull() const
Definition: heavydbTypes.h:232
DEVICE T operator()(const unsigned int index) const
Definition: heavydbTypes.h:524
int64_t bigintval
Definition: Datum.h:76
FlatBufferManager::Status toCoords(std::vector< std::vector< std::vector< CT >>> &result) const
int64_t getNofValues() const
std::string toString() const
DEVICE int32_t getCoordsSize() const
StringDictionaryProxy * string_dict_proxy_
std::string getString() const
CONSTEXPR DEVICE void set_null(T &value)
std::vector< double > toCoords() const
DEVICE ALWAYS_INLINE int64_t numStepsBetween(const Timestamp &begin, const Timestamp &end) const
Definition: heavydbTypes.h:875
DEVICE ALWAYS_INLINE T * data() const
Definition: heavydbTypes.h:516
DEVICE int64_t getSize() const
Definition: heavydbTypes.h:552
#define EXTENSION_NOINLINE_HOST
Definition: heavydbTypes.h:55
DEVICE int32_t getInputSrid() const
DEVICE ALWAYS_INLINE bool operator==(const char *rhs) const
Definition: heavydbTypes.h:649
DEVICE int32_t getCompression() const
DEVICE ALWAYS_INLINE int64_t operator/(const Timestamp &other) const
Definition: heavydbTypes.h:744
DEVICE T operator()(const unsigned int index) const
int64_t num_rows_
std::string toString() const
DEVICE int32_t getSize() const
Definition: heavydbTypes.h:972
std::vector< std::vector< CT > > toCoords() const
DEVICE Array()
Definition: heavydbTypes.h:477
DEVICE ALWAYS_INLINE DayTimeInterval operator*(const int64_t multiplier) const
Definition: heavydbTypes.h:871
DEVICE TextEncodingDict & operator[](const unsigned int index) const
DEVICE int8_t * getRingSizes()
bool get_is_geoint(const int8_t *flatbuffer)
DEVICE ALWAYS_INLINE bool operator!=(const TextEncodingNone &rhs) const
Definition: heavydbTypes.h:679
DEVICE ALWAYS_INLINE int64_t getMinutes() const
Definition: heavydbTypes.h:822
DEVICE ALWAYS_INLINE int64_t getSeconds() const
Definition: heavydbTypes.h:819
bool g_enable_smem_group_by true
FlatBufferManager::Status toCoords(std::vector< CT > &result) const
std::string toString(const Executor::ExtModuleKinds &kind)
Definition: Execute.h:1703
DEVICE ALWAYS_INLINE bool operator!=(const TextEncodingDict &other) const
Definition: heavydbTypes.h:248
int32_t input_srid
Definition: heavydbTypes.h:951
void disable_output_allocations()
Definition: heavydbTypes.h:379
std::vector< int32_t > compress_coords(const int8_t *data, const int64_t size, const bool is_geoint)
TableFunctionMetadataType
DEVICE const T & operator[](const unsigned int index) const
Definition: heavydbTypes.h:544
DEVICE int32_t getNumRings() const
DEVICE ALWAYS_INLINE const Timestamp operator-(const Timestamp &other) const
Definition: heavydbTypes.h:729
DEVICE bool operator==(const Array &other) const
Definition: heavydbTypes.h:572
DEVICE int32_t getOutputSrid() const
std::string toString() const
DEVICE int32_t getNumPolygons() const
EXTENSION_NOINLINE_HOST int32_t table_function_error(const char *message)
DEVICE TextEncodingDict * getPtr() const
#define RUNTIME_EXPORT
DEVICE int64_t numCols() const
DEVICE int32_t getCompression() const
StringDictionaryProxy * getStringDictionaryProxy(int32_t db_id, int32_t dict_id)
Definition: heavydbTypes.h:417
EXTENSION_NOINLINE_HOST bool ColumnArray_isNull(int8_t *flatbuffer, int64_t index)
DEVICE int32_t getCompression() const
Definition: heavydbTypes.h:974
int8_t ** ptrs_
int64_t num_cols_
DEVICE ALWAYS_INLINE Timestamp truncateToMilliseconds() const
Definition: heavydbTypes.h:779
TextEncodingDict(const int32_t other)
Definition: heavydbTypes.h:219
int8_t * makeBuffer(int64_t element_count, int64_t element_size)
Definition: heavydbTypes.h:430
DEVICE ALWAYS_INLINE bool operator!=(const Timestamp &other) const
Definition: heavydbTypes.h:767
DEVICE bool isNull(int64_t index) const
DEVICE int32_t getOutputSrid() const
Definition: heavydbTypes.h:978
EXTENSION_NOINLINE_HOST void ColumnArray_getItem(int8_t *flatbuffer, const int64_t index, const int64_t expected_numel, int8_t *&ptr, int64_t &size, bool &is_null, int64_t sizeof_T)
DEVICE constexpr T null_value() const
Definition: heavydbTypes.h:560
int8_t * makeBuffer(int64_t element_count, int64_t element_size)
Definition: heavydbTypes.h:319
void extend(const NestedArray< ItemType > &other, bool assign=false)
DEVICE RowType getItem(const int64_t index, const int64_t extra_numel) const
FlatBufferManager::Status fromCoordsWorker(const VT &coords)
DEVICE int32_t getCoordsSize() const
DEVICE TextEncodingNone & operator=(const ::TextEncodingNone &other)
DEVICE Timestamp(int64_t timeval)
Definition: heavydbTypes.h:703
DEVICE ALWAYS_INLINE char & operator[](const unsigned int index)
Definition: heavydbTypes.h:646
EXTENSION_NOINLINE_HOST void TableFunctionManager_set_metadata(int8_t *mgr_ptr, const char *key, const uint8_t *raw_bytes, const size_t num_bytes, const TableFunctionMetadataType value_type)
TextEncodingDict * ptr_
DEVICE bool isNull(int64_t index) const
int32_t getOrAddTransient(int32_t db_id, int32_t dict_id, std::string str)
Definition: heavydbTypes.h:314
DEVICE ALWAYS_INLINE Timestamp truncateToMicroseconds() const
Definition: heavydbTypes.h:775
DEVICE void setNull(int64_t index)
FlatBufferManager::Status fromCoords(const std::vector< CT > &coords)
DEVICE double decompress_longitude_coord_geoint32(const int32_t compressed)
DEVICE Column(const Column &other)
DEVICE void extend(const int8_t *data, const int32_t size, bool assign=false)
DEVICE int32_t getInputSrid() const
Definition: heavydbTypes.h:976
DEVICE Column< TextEncodingDict > operator[](const int index) const
void points_to_vector(const int8_t *points_buf, const int64_t nof_points, const bool is_geoint, std::vector< CT > &result)
DEVICE ALWAYS_INLINE int64_t getMonth() const
Definition: heavydbTypes.h:831
DEVICE Column(TextEncodingDict *ptr, const int64_t num_rows, StringDictionaryProxy *string_dict_proxy)
DEVICE int32_t getOutputSrid() const
NestedArray< ItemType > & operator+=(const NestedArray< ItemType > &other)
void set_output_item_values_total_number(int32_t index, int64_t output_item_values_total_number)
Definition: heavydbTypes.h:367
DEVICE int64_t ExtractFromTime(ExtractField field, const int64_t timeval)
DEVICE int64_t size() const
DEVICE bool isNull(int64_t index) const
EXTENSION_NOINLINE_HOST void set_output_item_values_total_number(int32_t index, int64_t output_item_values_total_number)
RUNTIME_EXPORT NEVER_INLINE HOST std::string RowFunctionManager_getString(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id, int32_t string_id)
std::string typeName(const T *v)
Definition: toString.h:106
T * ptr_
Definition: heavydbTypes.h:471
NestedArray< ItemType > & operator=(const NestedArray< ItemType > &other)
DEVICE Column< TextEncodingDict > & operator=(const Column< TextEncodingDict > &other)
DEVICE void setSize(int64_t num_rows)
DEVICE int32_t getCompression() const
Definition: heavydbTypes.h:992
DEVICE ALWAYS_INLINE bool operator==(const DayTimeInterval &other) const
Definition: heavydbTypes.h:858
DEVICE ALWAYS_INLINE Timestamp operator+(const DayTimeInterval &interval) const
Definition: heavydbTypes.h:937
DEVICE ALWAYS_INLINE int64_t getHours() const
Definition: heavydbTypes.h:825
DEVICE ALWAYS_INLINE bool isNull() const
Definition: heavydbTypes.h:691
DEVICE ALWAYS_INLINE size_t size() const
Definition: heavydbTypes.h:520
int64_t size_
Definition: heavydbTypes.h:472
#define NEVER_INLINE
EXTENSION_NOINLINE_HOST void TableFunctionManager_set_output_row_size(int8_t *mgr_ptr, int64_t num_rows)
void getValuesBuffer(T *&values, int64_t &nof_values, bool &is_null, bool require_1d_item=true) const
DEVICE int8_t * getCoords() const
std::string str() const
DEVICE TextEncodingNone & operator+=(const ::TextEncodingNone &other)
TextEncodingNone()=default
DEVICE int32_t getNumRings() const
void set_metadata(const std::string &key, const T &value)
Definition: heavydbTypes.h:388
DEVICE Array(T *ptr, const int64_t size, const bool is_null=false)
Definition: heavydbTypes.h:475
DEVICE ALWAYS_INLINE Timestamp truncateToMonth() const
Definition: heavydbTypes.h:799
DEVICE ALWAYS_INLINE bool operator<(const Timestamp &other) const
Definition: heavydbTypes.h:771
std::string getString(int32_t db_id, int32_t dict_id, int32_t string_id)
Definition: heavydbTypes.h:299
DEVICE ALWAYS_INLINE int64_t numStepsBetween(const Timestamp &begin, const Timestamp &end) const
Definition: heavydbTypes.h:923
DEVICE ALWAYS_INLINE TextEncodingNone(M &mgr, const std::string &str)
Definition: heavydbTypes.h:621
DEVICE int64_t numCols() const
DEVICE RowType operator[](const unsigned int index) const
DEVICE Geo::Point2D operator[](const unsigned int index) const
DEVICE void setItem(int64_t index, const Geo::Point2D &other)
ItemType & getValue(const int64_t index) const
int32_t output_srid
Definition: heavydbTypes.h:952
DEVICE Point2D operator[](const unsigned int index) const
DEVICE ALWAYS_INLINE int64_t getMilliseconds() const
Definition: heavydbTypes.h:815
std::string toString() const
DEVICE T & operator[](const unsigned int index)
Definition: heavydbTypes.h:536
DEVICE void setNull(int64_t index)
DEVICE bool isNull(int64_t index) const
DEVICE int64_t size() const
Timestamp()=default
DEVICE int32_t getInputSrid() const
Definition: heavydbTypes.h:994
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_getNewDictId(int8_t *mgr_ptr)
std::string toString() const
DEVICE int64_t size() const
DEVICE Column(const Column &other)
DEVICE ALWAYS_INLINE const Timestamp operator*(const int64_t multiplier) const
Definition: heavydbTypes.h:753
DEVICE ALWAYS_INLINE YearMonthTimeInterval operator*(const int64_t multiplier) const
Definition: heavydbTypes.h:919
char & operator[](const int64_t index)
DEVICE Column(T *ptr, const int64_t num_rows)
DEVICE ALWAYS_INLINE bool operator==(const TextEncodingNone &rhs) const
Definition: heavydbTypes.h:661
DEVICE const TextEncodingDict getOrAddTransient(const std::string &str)
DEVICE ColumnList(int8_t **ptrs, const int64_t num_cols, const int64_t num_rows, StringDictionaryProxy **string_dict_proxies)
DEVICE ALWAYS_INLINE int64_t getNanoseconds() const
Definition: heavydbTypes.h:807
#define ALWAYS_INLINE
TextEncodingNone(char const *const c_str)
Definition: heavydbTypes.h:617
#define NESTED_ARRAY_NDIM
Definition: heavydbTypes.h:452
Definition: Datum.h:71
DEVICE DayTimeInterval(int64_t init)
Definition: heavydbTypes.h:856
DEVICE ALWAYS_INLINE Timestamp truncateToMinutes() const
Definition: heavydbTypes.h:787
DEVICE bool isNull(const unsigned int index) const
Definition: heavydbTypes.h:568
DEVICE int32_t getInputSrid() const
void enable_output_allocations()
Definition: heavydbTypes.h:381
DEVICE int32_t getOutputSrid() const
DEVICE ALWAYS_INLINE bool operator==(const int32_t &other) const
Definition: heavydbTypes.h:240
T & operator[](const int64_t index)
static TableFunctionManager * get_singleton()
Definition: heavydbTypes.h:357
std::string toString() const
DEVICE void setItem(int64_t index, const RowType &item)
EXTENSION_NOINLINE_HOST void TableFunctionManager_set_output_array_values_total_number(int8_t *mgr_ptr, int32_t index, int64_t output_array_values_total_number)
DEVICE bool isNull() const
Definition: heavydbTypes.h:556
DEVICE ALWAYS_INLINE bool operator<(const int64_t &other) const
Definition: heavydbTypes.h:267
EXTENSION_NOINLINE_HOST void ColumnArray_setArray(int8_t *flatbuffer, int64_t index, const int8_t *ptr, int64_t size, bool is_null)
static constexpr int64_t kMicroSecsPerSec
std::string toString() const
Definition: heavydbTypes.h:436
DEVICE ALWAYS_INLINE bool operator!=(const char *rhs) const
Definition: heavydbTypes.h:676
DEVICE void setSize(int64_t num_rows)
int32_t getDictDbId(const char *func_name, size_t arg_idx)
Definition: heavydbTypes.h:304
HOST DEVICE Status setNull(int64_t index)
Definition: FlatBuffer.h:2029
DEVICE int32_t getOutputSrid() const
Definition: heavydbTypes.h:960
DEVICE ALWAYS_INLINE bool operator==(const int64_t &other) const
Definition: heavydbTypes.h:244
DEVICE Point2D operator[](const unsigned int index) const
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_getOrAddTransient(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id, std::string str)
DEVICE TextEncodingNone & operator=(const std::string &s)
DEVICE Column(std::vector< TextEncodingDict > &input_vec)
DEVICE Array< T > & operator=(const ::Array< T > &s)