OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OmniSciTypes.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cstring>
20 #include <limits>
21 #include <stdexcept>
22 #include <type_traits>
23 
24 /* `../` is required for UDFCompiler */
25 #include "../Shared/InlineNullValues.h"
26 #include "../Shared/funcannotations.h"
27 
28 #define EXTENSION_INLINE extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE
29 #define EXTENSION_NOINLINE extern "C" RUNTIME_EXPORT NEVER_INLINE DEVICE
30 #define TEMPLATE_INLINE ALWAYS_INLINE DEVICE
31 #define TEMPLATE_NOINLINE NEVER_INLINE DEVICE
32 
33 EXTENSION_NOINLINE int8_t* allocate_varlen_buffer(int64_t element_count,
34  int64_t element_size);
35 
36 /*
37  Table function management functions and macros:
38  */
39 #define FUNC_NAME (std::string(__func__).substr(0, std::string(__func__).find("__")))
40 // TODO: support windows path format
41 #define ERROR_STRING(MSG) \
42  (std::string(__FILE__).substr(std::string(__FILE__).rfind("/") + 1) + ":" + \
43  std::to_string(__LINE__) + " " + FUNC_NAME + ": " + MSG) \
44  .c_str()
45 #define TABLE_FUNCTION_ERROR(MSG) table_function_error(ERROR_STRING(MSG))
46 #define ERROR_MESSAGE(MSG) error_message(ERROR_STRING(MSG))
47 
48 EXTENSION_NOINLINE void set_output_row_size(int64_t num_rows);
50  int64_t num_rows);
52 EXTENSION_NOINLINE int32_t table_function_error(const char* message);
54  const char* message);
55 
56 // https://www.fluentcpp.com/2018/04/06/strong-types-by-struct/
58  int32_t value;
59  operator int32_t() const { return value; }
60  TextEncodingDict operator=(const int32_t other) {
61  value = other;
62  return *this;
63  }
64 #ifndef __CUDACC__
65  TextEncodingDict(const int32_t other) : value(other) {}
67 #endif
68 };
69 
70 template <typename T>
71 struct Array {
72  T* ptr;
73  int64_t size;
74  int8_t is_null;
75 
76  DEVICE Array(const int64_t size, const bool is_null = false)
77  : size(size), is_null(is_null) {
78  if (!is_null) {
79  ptr = reinterpret_cast<T*>(
80  allocate_varlen_buffer(size, static_cast<int64_t>(sizeof(T))));
81  } else {
82  ptr = nullptr;
83  }
84  }
85 
86  DEVICE T operator()(const unsigned int index) const {
87  if (index < static_cast<unsigned int>(size)) {
88  return ptr[index];
89  } else {
90  return 0; // see array_at
91  }
92  }
93 
94  DEVICE T& operator[](const unsigned int index) { return ptr[index]; }
95 
96  DEVICE int64_t getSize() const { return size; }
97 
98  DEVICE bool isNull() const { return is_null; }
99 
100  DEVICE constexpr inline T null_value() const {
101  return std::is_signed<T>::value ? std::numeric_limits<T>::min()
102  : std::numeric_limits<T>::max();
103  }
104 };
105 
107  char* ptr_;
108  int64_t size_;
109 
110 #ifndef __CUDACC__
111  std::string getString() const { return std::string(ptr_, size_); }
112 #endif
113 
114  DEVICE ALWAYS_INLINE char& operator[](const unsigned int index) {
115  return index < size_ ? ptr_[index] : ptr_[size_ - 1];
116  }
117  DEVICE ALWAYS_INLINE bool operator==(const char* rhs) const {
118 #ifdef __CUDACC__
119  for (int i = 0; i < size_; i++) {
120  if (rhs[i] == '\0' || ptr_[i] != rhs[i]) {
121  return false;
122  }
123  }
124  return rhs[size_] == '\0';
125 #else
126  return strcmp(ptr_, rhs) == 0;
127 #endif
128  }
129  DEVICE ALWAYS_INLINE bool operator!=(const char* rhs) const {
130  return !(this->operator==(rhs));
131  }
132  DEVICE ALWAYS_INLINE operator char*() const { return ptr_; }
133  DEVICE ALWAYS_INLINE int64_t size() const { return size_; }
134  DEVICE ALWAYS_INLINE bool isNull() const { return size_ == 0; }
135 };
136 
137 struct GeoPoint {
138  int8_t* ptr;
139  int32_t sz;
140  int32_t compression;
141  int32_t input_srid;
142  int32_t output_srid;
143 
144  DEVICE int64_t getSize() const { return sz; }
145 
146  DEVICE int32_t getCompression() const { return compression; }
147 
148  DEVICE int32_t getInputSrid() const { return input_srid; }
149 
150  DEVICE int32_t getOutputSrid() const { return output_srid; }
151 };
152 
154  int8_t* ptr;
155  int32_t sz;
156  int32_t compression;
157  int32_t input_srid;
158  int32_t output_srid;
159 
160  DEVICE int32_t getSize() const { return sz; }
161 
162  DEVICE int32_t getCompression() const { return compression; }
163 
164  DEVICE int32_t getInputSrid() const { return input_srid; }
165 
166  DEVICE int32_t getOutputSrid() const { return output_srid; }
167 };
168 
169 struct GeoPolygon {
170  int8_t* ptr_coords;
171  int32_t coords_size;
172  int8_t* ring_sizes;
173  int32_t num_rings;
174  int32_t compression;
175  int32_t input_srid;
176  int32_t output_srid;
177 
178  DEVICE int8_t* getRingSizes() { return ring_sizes; }
179  DEVICE int32_t getCoordsSize() const { return coords_size; }
180 
181  DEVICE int32_t getNumRings() const { return num_rings; }
182 
183  DEVICE int32_t getCompression() const { return compression; }
184 
185  DEVICE int32_t getInputSrid() const { return input_srid; }
186 
187  DEVICE int32_t getOutputSrid() const { return output_srid; }
188 };
189 
191  int8_t* ptr_coords;
192  int32_t coords_size;
193  int8_t* ring_sizes;
194  int32_t num_rings;
195  int8_t* poly_sizes;
196  int32_t num_polys;
197  int32_t compression;
198  int32_t input_srid;
199  int32_t output_srid;
200 
201  DEVICE int8_t* getRingSizes() { return ring_sizes; }
202  DEVICE int32_t getCoordsSize() const { return coords_size; }
203 
204  DEVICE int32_t getNumRings() const { return num_rings; }
205 
206  DEVICE int8_t* getPolygonSizes() { return poly_sizes; }
207 
208  DEVICE int32_t getNumPolygons() const { return num_polys; }
209 
210  DEVICE int32_t getCompression() const { return compression; }
211 
212  DEVICE int32_t getInputSrid() const { return input_srid; }
213 
214  DEVICE int32_t getOutputSrid() const { return output_srid; }
215 };
216 
217 template <typename T>
218 struct Column {
219  T* ptr_; // row data
220  int64_t size_; // row count
221 
222  DEVICE T& operator[](const unsigned int index) const {
223  if (index >= size_) {
224 #ifndef __CUDACC__
225  throw std::runtime_error("column buffer index is out of range");
226 #else
227  static DEVICE T null_value;
228  if constexpr (std::is_same_v<T, TextEncodingDict>) {
229  set_null(null_value.value);
230  } else {
231  set_null(null_value);
232  }
233  return null_value;
234 #endif
235  }
236  return ptr_[index];
237  }
238  DEVICE int64_t size() const { return size_; }
239 
240  DEVICE inline bool isNull(int64_t index) const { return is_null(ptr_[index]); }
241  DEVICE inline void setNull(int64_t index) { set_null(ptr_[index]); }
243 #ifndef __CUDACC__
244  if (size() == other.size()) {
245  memcpy(ptr_, &other[0], other.size() * sizeof(T));
246  } else {
247  throw std::runtime_error("cannot copy assign columns with different sizes");
248  }
249 #else
250  if (size() == other.size()) {
251  for (unsigned int i = 0; i < size(); i++) {
252  ptr_[i] = other[i];
253  }
254  } else {
255  // TODO: set error
256  }
257 #endif
258  return *this;
259  }
260 
261 #ifdef HAVE_TOSTRING
262  std::string toString() const {
263  return ::typeName(this) + "(ptr=" + ::toString(reinterpret_cast<void*>(ptr_)) +
264  ", size=" + std::to_string(size_) + ")";
265  }
266 #endif
267 };
268 
269 template <>
270 DEVICE inline bool Column<TextEncodingDict>::isNull(int64_t index) const {
271  return is_null(ptr_[index].value);
272 }
273 
274 template <>
275 DEVICE inline void Column<TextEncodingDict>::setNull(int64_t index) {
276  set_null(ptr_[index].value);
277 }
278 
279 /*
280  ColumnList is an ordered list of Columns.
281 */
282 template <typename T>
283 struct ColumnList {
284  int8_t** ptrs_; // ptrs to columns data
285  int64_t num_cols_; // the length of columns list
286  int64_t size_; // the size of columns
287 
288  DEVICE int64_t size() const { return size_; }
289  DEVICE int64_t numCols() const { return num_cols_; }
290  DEVICE Column<T> operator[](const int index) const {
291  if (index >= 0 && index < num_cols_)
292  return {reinterpret_cast<T*>(ptrs_[index]), size_};
293  else
294  return {nullptr, -1};
295  }
296 
297 #ifdef HAVE_TOSTRING
298 
299  std::string toString() const {
300  std::string result = ::typeName(this) + "(ptrs=[";
301  for (int64_t index = 0; index < num_cols_; index++) {
302  result += ::toString(reinterpret_cast<void*>(ptrs_[index])) +
303  (index < num_cols_ - 1 ? ", " : "");
304  }
305  result += "], num_cols=" + std::to_string(num_cols_) +
306  ", size=" + std::to_string(size_) + ")";
307  return result;
308  }
309 
310 #endif
311 };
312 
313 /*
314  This TableFunctionManager struct is a minimal proxy to the
315  TableFunctionManager defined in TableFunctionManager.h. The
316  corresponding instances share `this` but have different virtual
317  tables for methods.
318 */
321  return reinterpret_cast<TableFunctionManager*>(TableFunctionManager_get_singleton());
322  }
323 
324  void set_output_row_size(int64_t num_rows) {
325  TableFunctionManager_set_output_row_size(reinterpret_cast<int8_t*>(this), num_rows);
326  }
327 
328  int32_t error_message(const char* message) {
329  return TableFunctionManager_error_message(reinterpret_cast<int8_t*>(this), message);
330  }
331 
332 #ifdef HAVE_TOSTRING
333 
334  std::string toString() const {
335  std::string result = ::typeName(this) + "(";
336  if (!this) {
337  result += "UNINITIALIZED";
338  }
339  result += ")";
340  return result;
341  }
342 
343 #endif
344 };
void set_output_row_size(int64_t num_rows)
Definition: OmniSciTypes.h:324
int32_t compression
Definition: OmniSciTypes.h:174
DEVICE int32_t getCompression() const
Definition: OmniSciTypes.h:210
int8_t * ptr_coords
Definition: OmniSciTypes.h:170
std::string toString(const ExtArgumentType &sig_type)
DEVICE int32_t getInputSrid() const
Definition: OmniSciTypes.h:185
int32_t output_srid
Definition: OmniSciTypes.h:176
int32_t input_srid
Definition: OmniSciTypes.h:141
EXTENSION_NOINLINE int8_t * allocate_varlen_buffer(int64_t element_count, int64_t element_size)
std::string getString() const
Definition: OmniSciTypes.h:111
Simplified core of GeoJSON Polygon coordinates definition.
Definition: OmniSciTypes.h:169
int64_t size
Definition: OmniSciTypes.h:73
TextEncodingDict operator=(const int32_t other)
Definition: OmniSciTypes.h:60
DEVICE int64_t size() const
Definition: OmniSciTypes.h:238
EXTENSION_NOINLINE int32_t table_function_error(const char *message)
int32_t coords_size
Definition: OmniSciTypes.h:171
DEVICE int64_t numCols() const
Definition: OmniSciTypes.h:289
DEVICE int8_t * getPolygonSizes()
Definition: OmniSciTypes.h:206
T * ptr_
Definition: OmniSciTypes.h:219
DEVICE Column< T > & operator=(const Column< T > &other)
Definition: OmniSciTypes.h:242
DEVICE Column< T > operator[](const int index) const
Definition: OmniSciTypes.h:290
DEVICE int8_t * getRingSizes()
Definition: OmniSciTypes.h:201
int8_t * ptr
Definition: OmniSciTypes.h:154
std::string to_string(char const *&&v)
int32_t compression
Definition: OmniSciTypes.h:197
int32_t output_srid
Definition: OmniSciTypes.h:142
DEVICE ALWAYS_INLINE int64_t size() const
Definition: OmniSciTypes.h:133
Simplified core of GeoJSON MultiPolygon coordinates definition.
Definition: OmniSciTypes.h:190
DEVICE int32_t getNumRings() const
Definition: OmniSciTypes.h:204
#define DEVICE
#define EXTENSION_NOINLINE
Definition: OmniSciTypes.h:29
DEVICE int32_t getInputSrid() const
Definition: OmniSciTypes.h:148
int32_t output_srid
Definition: OmniSciTypes.h:158
DEVICE int32_t getInputSrid() const
Definition: OmniSciTypes.h:164
EXTENSION_NOINLINE void TableFunctionManager_set_output_row_size(int8_t *mgr_ptr, int64_t num_rows)
int32_t error_message(const char *message)
Definition: OmniSciTypes.h:328
DEVICE T & operator[](const unsigned int index) const
Definition: OmniSciTypes.h:222
int32_t output_srid
Definition: OmniSciTypes.h:199
CONSTEXPR DEVICE bool is_null(const T &value)
DEVICE Array(const int64_t size, const bool is_null=false)
Definition: OmniSciTypes.h:76
int64_t size_
Definition: OmniSciTypes.h:286
DEVICE T operator()(const unsigned int index) const
Definition: OmniSciTypes.h:86
int8_t * ring_sizes
Definition: OmniSciTypes.h:193
DEVICE int32_t getCoordsSize() const
Definition: OmniSciTypes.h:179
int32_t coords_size
Definition: OmniSciTypes.h:192
CONSTEXPR DEVICE void set_null(T &value)
int32_t input_srid
Definition: OmniSciTypes.h:198
DEVICE int32_t getNumPolygons() const
Definition: OmniSciTypes.h:208
DEVICE int64_t getSize() const
Definition: OmniSciTypes.h:96
DEVICE int32_t getCompression() const
Definition: OmniSciTypes.h:183
DEVICE ALWAYS_INLINE bool operator==(const char *rhs) const
Definition: OmniSciTypes.h:117
DEVICE int32_t getCoordsSize() const
Definition: OmniSciTypes.h:202
DEVICE int32_t getInputSrid() const
Definition: OmniSciTypes.h:212
EXTENSION_NOINLINE int8_t * TableFunctionManager_get_singleton()
int32_t compression
Definition: OmniSciTypes.h:140
int8_t is_null
Definition: OmniSciTypes.h:74
int32_t num_rings
Definition: OmniSciTypes.h:173
int8_t * ring_sizes
Definition: OmniSciTypes.h:172
EXTENSION_NOINLINE void set_output_row_size(int64_t num_rows)
int32_t sz
Definition: OmniSciTypes.h:139
int8_t * poly_sizes
Definition: OmniSciTypes.h:195
DEVICE int32_t getNumRings() const
Definition: OmniSciTypes.h:181
int8_t ** ptrs_
Definition: OmniSciTypes.h:284
int64_t num_cols_
Definition: OmniSciTypes.h:285
TextEncodingDict(const int32_t other)
Definition: OmniSciTypes.h:65
DEVICE int32_t getSize() const
Definition: OmniSciTypes.h:160
DEVICE int32_t getCompression() const
Definition: OmniSciTypes.h:162
DEVICE constexpr T null_value() const
Definition: OmniSciTypes.h:100
DEVICE int32_t getOutputSrid() const
Definition: OmniSciTypes.h:187
DEVICE ALWAYS_INLINE char & operator[](const unsigned int index)
Definition: OmniSciTypes.h:114
DEVICE bool isNull(int64_t index) const
Definition: OmniSciTypes.h:240
int64_t size_
Definition: OmniSciTypes.h:220
DEVICE void setNull(int64_t index)
Definition: OmniSciTypes.h:241
int8_t * ptr
Definition: OmniSciTypes.h:138
DEVICE int32_t getOutputSrid() const
Definition: OmniSciTypes.h:150
int32_t compression
Definition: OmniSciTypes.h:156
DEVICE int8_t * getRingSizes()
Definition: OmniSciTypes.h:178
std::string typeName(const T *v)
Definition: toString.h:93
DEVICE int64_t getSize() const
Definition: OmniSciTypes.h:144
DEVICE ALWAYS_INLINE bool isNull() const
Definition: OmniSciTypes.h:134
DEVICE int32_t getCompression() const
Definition: OmniSciTypes.h:146
T * ptr
Definition: OmniSciTypes.h:72
DEVICE T & operator[](const unsigned int index)
Definition: OmniSciTypes.h:94
int8_t * ptr_coords
Definition: OmniSciTypes.h:191
DEVICE int64_t size() const
Definition: OmniSciTypes.h:288
DEVICE int32_t getOutputSrid() const
Definition: OmniSciTypes.h:214
#define ALWAYS_INLINE
static TableFunctionManager * get_singleton()
Definition: OmniSciTypes.h:320
DEVICE bool isNull() const
Definition: OmniSciTypes.h:98
int32_t input_srid
Definition: OmniSciTypes.h:175
DEVICE ALWAYS_INLINE bool operator!=(const char *rhs) const
Definition: OmniSciTypes.h:129
int32_t input_srid
Definition: OmniSciTypes.h:157
DEVICE int32_t getOutputSrid() const
Definition: OmniSciTypes.h:166
EXTENSION_NOINLINE int32_t TableFunctionManager_error_message(int8_t *mgr_ptr, const char *message)