OmniSciDB  21ac014ffc
 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 <limits>
20 #include <stdexcept>
21 #include <type_traits>
22 
23 /* `../` is required for UDFCompiler */
24 #include "../Shared/InlineNullValues.h"
25 #include "../Shared/funcannotations.h"
26 
27 #define EXTENSION_INLINE extern "C" RUNTIME_EXPORT ALWAYS_INLINE DEVICE
28 #define EXTENSION_NOINLINE extern "C" RUNTIME_EXPORT NEVER_INLINE DEVICE
29 
30 EXTENSION_NOINLINE int8_t* allocate_varlen_buffer(int64_t element_count,
31  int64_t element_size);
32 
33 EXTENSION_NOINLINE void set_output_row_size(int64_t num_rows);
34 
35 // https://www.fluentcpp.com/2018/04/06/strong-types-by-struct/
37  int32_t value;
38 };
39 
40 template <typename T>
41 struct Array {
42  T* ptr;
43  int64_t size;
44  int8_t is_null;
45 
46  DEVICE Array(const int64_t size, const bool is_null = false)
47  : size(size), is_null(is_null) {
48  if (!is_null) {
49  ptr = reinterpret_cast<T*>(
50  allocate_varlen_buffer(size, static_cast<int64_t>(sizeof(T))));
51  } else {
52  ptr = nullptr;
53  }
54  }
55 
56  DEVICE T operator()(const unsigned int index) const {
57  if (index < static_cast<unsigned int>(size)) {
58  return ptr[index];
59  } else {
60  return 0; // see array_at
61  }
62  }
63 
64  DEVICE T& operator[](const unsigned int index) { return ptr[index]; }
65 
66  DEVICE int64_t getSize() const { return size; }
67 
68  DEVICE bool isNull() const { return is_null; }
69 
70  DEVICE constexpr inline T null_value() const {
71  return std::is_signed<T>::value ? std::numeric_limits<T>::min()
72  : std::numeric_limits<T>::max();
73  }
74 };
75 
76 struct GeoPoint {
77  int8_t* ptr;
78  int32_t sz;
79  int32_t compression;
80  int32_t input_srid;
81  int32_t output_srid;
82 
83  DEVICE int64_t getSize() const { return sz; }
84 
85  DEVICE int32_t getCompression() const { return compression; }
86 
87  DEVICE int32_t getInputSrid() const { return input_srid; }
88 
89  DEVICE int32_t getOutputSrid() const { return output_srid; }
90 };
91 
92 struct GeoLineString {
93  int8_t* ptr;
94  int32_t sz;
95  int32_t compression;
96  int32_t input_srid;
97  int32_t output_srid;
98 
99  DEVICE int32_t getSize() const { return sz; }
100 
101  DEVICE int32_t getCompression() const { return compression; }
102 
103  DEVICE int32_t getInputSrid() const { return input_srid; }
104 
105  DEVICE int32_t getOutputSrid() const { return output_srid; }
106 };
107 
108 struct GeoPolygon {
109  int8_t* ptr_coords;
110  int32_t coords_size;
111  int8_t* ring_sizes;
112  int32_t num_rings;
113  int32_t compression;
114  int32_t input_srid;
115  int32_t output_srid;
116 
117  DEVICE int8_t* getRingSizes() { return ring_sizes; }
118  DEVICE int32_t getCoordsSize() const { return coords_size; }
119 
120  DEVICE int32_t getNumRings() const { return num_rings; }
121 
122  DEVICE int32_t getCompression() const { return compression; }
123 
124  DEVICE int32_t getInputSrid() const { return input_srid; }
125 
126  DEVICE int32_t getOutputSrid() const { return output_srid; }
127 };
128 
130  int8_t* ptr_coords;
131  int32_t coords_size;
132  int8_t* ring_sizes;
133  int32_t num_rings;
134  int8_t* poly_sizes;
135  int32_t num_polys;
136  int32_t compression;
137  int32_t input_srid;
138  int32_t output_srid;
139 
140  DEVICE int8_t* getRingSizes() { return ring_sizes; }
141  DEVICE int32_t getCoordsSize() const { return coords_size; }
142 
143  DEVICE int32_t getNumRings() const { return num_rings; }
144 
145  DEVICE int8_t* getPolygonSizes() { return poly_sizes; }
146 
147  DEVICE int32_t getNumPolygons() const { return num_polys; }
148 
149  DEVICE int32_t getCompression() const { return compression; }
150 
151  DEVICE int32_t getInputSrid() const { return input_srid; }
152 
153  DEVICE int32_t getOutputSrid() const { return output_srid; }
154 };
155 
156 template <typename T>
157 struct Column {
158  T* ptr_; // row data
159  int64_t size_; // row count
160 
161  DEVICE T& operator[](const unsigned int index) const {
162  if (index >= size_) {
163 #ifndef __CUDACC__
164  throw std::runtime_error("column buffer index is out of range");
165 #else
166  static DEVICE T null_value;
167  if constexpr (std::is_same_v<T, TextEncodingDict>) {
168  set_null(null_value.value);
169  } else {
170  set_null(null_value);
171  }
172  return null_value;
173 #endif
174  }
175  return ptr_[index];
176  }
177  DEVICE int64_t size() const { return size_; }
178 
179  DEVICE bool isNull(int64_t index) const { return is_null(ptr_[index]); }
180  DEVICE void setNull(int64_t index) {
181  if constexpr (std::is_same<T, TextEncodingDict>::value) {
182  set_null(ptr_[index].value);
183  } else {
184  set_null(ptr_[index]);
185  }
186  }
188 #ifndef __CUDACC__
189  if (size() == other.size()) {
190  memcpy(ptr_, &other[0], other.size() * sizeof(T));
191  } else {
192  throw std::runtime_error("cannot copy assign columns with different sizes");
193  }
194 #else
195  if (size() == other.size()) {
196  for (unsigned int i = 0; i < size(); i++) {
197  ptr_[i] = other[i];
198  }
199  } else {
200  // TODO: set error
201  }
202 #endif
203  return *this;
204  }
205 
206 #ifdef HAVE_TOSTRING
207  std::string toString() const {
208  return ::typeName(this) + "(ptr=" + ::toString(reinterpret_cast<void*>(ptr_)) +
209  ", size=" + std::to_string(size_) + ")";
210  }
211 #endif
212 };
213 
214 /*
215  ColumnList is an ordered list of Columns.
216 */
217 template <typename T>
218 struct ColumnList {
219  int8_t** ptrs_; // ptrs to columns data
220  int64_t num_cols_; // the length of columns list
221  int64_t size_; // the size of columns
222 
223  DEVICE int64_t size() const { return size_; }
224  DEVICE int64_t numCols() const { return num_cols_; }
225  DEVICE Column<T> operator[](const int index) const {
226  if (index >= 0 && index < num_cols_)
227  return {reinterpret_cast<T*>(ptrs_[index]), size_};
228  else
229  return {nullptr, -1};
230  }
231 
232 #ifdef HAVE_TOSTRING
233 
234  std::string toString() const {
235  std::string result = ::typeName(this) + "(ptrs=[";
236  for (int64_t index = 0; index < num_cols_; index++) {
237  result += ::toString(reinterpret_cast<void*>(ptrs_[index])) +
238  (index < num_cols_ - 1 ? ", " : "");
239  }
240  result += "], num_cols=" + std::to_string(num_cols_) +
241  ", size=" + std::to_string(size_) + ")";
242  return result;
243  }
244 
245 #endif
246 };
int32_t compression
Definition: OmniSciTypes.h:113
DEVICE int32_t getCompression() const
Definition: OmniSciTypes.h:149
int8_t * ptr_coords
Definition: OmniSciTypes.h:109
std::string toString(const ExtArgumentType &sig_type)
DEVICE int32_t getInputSrid() const
Definition: OmniSciTypes.h:124
int32_t output_srid
Definition: OmniSciTypes.h:115
int32_t input_srid
Definition: OmniSciTypes.h:80
EXTENSION_NOINLINE int8_t * allocate_varlen_buffer(int64_t element_count, int64_t element_size)
Simplified core of GeoJSON Polygon coordinates definition.
Definition: OmniSciTypes.h:108
int64_t size
Definition: OmniSciTypes.h:43
DEVICE int64_t size() const
Definition: OmniSciTypes.h:177
int32_t coords_size
Definition: OmniSciTypes.h:110
DEVICE int64_t numCols() const
Definition: OmniSciTypes.h:224
DEVICE int8_t * getPolygonSizes()
Definition: OmniSciTypes.h:145
T * ptr_
Definition: OmniSciTypes.h:158
DEVICE Column< T > & operator=(const Column< T > &other)
Definition: OmniSciTypes.h:187
DEVICE Column< T > operator[](const int index) const
Definition: OmniSciTypes.h:225
DEVICE int8_t * getRingSizes()
Definition: OmniSciTypes.h:140
int8_t * ptr
Definition: OmniSciTypes.h:93
std::string to_string(char const *&&v)
int32_t compression
Definition: OmniSciTypes.h:136
int32_t output_srid
Definition: OmniSciTypes.h:81
Simplified core of GeoJSON MultiPolygon coordinates definition.
Definition: OmniSciTypes.h:129
DEVICE int32_t getNumRings() const
Definition: OmniSciTypes.h:143
#define DEVICE
#define EXTENSION_NOINLINE
Definition: OmniSciTypes.h:28
DEVICE int32_t getInputSrid() const
Definition: OmniSciTypes.h:87
int32_t output_srid
Definition: OmniSciTypes.h:97
DEVICE int32_t getInputSrid() const
Definition: OmniSciTypes.h:103
DEVICE T & operator[](const unsigned int index) const
Definition: OmniSciTypes.h:161
int32_t output_srid
Definition: OmniSciTypes.h:138
CONSTEXPR DEVICE bool is_null(const T &value)
DEVICE Array(const int64_t size, const bool is_null=false)
Definition: OmniSciTypes.h:46
int64_t size_
Definition: OmniSciTypes.h:221
DEVICE T operator()(const unsigned int index) const
Definition: OmniSciTypes.h:56
int8_t * ring_sizes
Definition: OmniSciTypes.h:132
DEVICE int32_t getCoordsSize() const
Definition: OmniSciTypes.h:118
int32_t coords_size
Definition: OmniSciTypes.h:131
CONSTEXPR DEVICE void set_null(T &value)
int32_t input_srid
Definition: OmniSciTypes.h:137
DEVICE int32_t getNumPolygons() const
Definition: OmniSciTypes.h:147
DEVICE int64_t getSize() const
Definition: OmniSciTypes.h:66
DEVICE int32_t getCompression() const
Definition: OmniSciTypes.h:122
DEVICE int32_t getCoordsSize() const
Definition: OmniSciTypes.h:141
DEVICE int32_t getInputSrid() const
Definition: OmniSciTypes.h:151
int32_t compression
Definition: OmniSciTypes.h:79
int8_t is_null
Definition: OmniSciTypes.h:44
int32_t num_rings
Definition: OmniSciTypes.h:112
int8_t * ring_sizes
Definition: OmniSciTypes.h:111
EXTENSION_NOINLINE void set_output_row_size(int64_t num_rows)
int32_t sz
Definition: OmniSciTypes.h:78
int8_t * poly_sizes
Definition: OmniSciTypes.h:134
DEVICE int32_t getNumRings() const
Definition: OmniSciTypes.h:120
int8_t ** ptrs_
Definition: OmniSciTypes.h:219
int64_t num_cols_
Definition: OmniSciTypes.h:220
DEVICE int32_t getSize() const
Definition: OmniSciTypes.h:99
DEVICE int32_t getCompression() const
Definition: OmniSciTypes.h:101
DEVICE constexpr T null_value() const
Definition: OmniSciTypes.h:70
DEVICE int32_t getOutputSrid() const
Definition: OmniSciTypes.h:126
DEVICE bool isNull(int64_t index) const
Definition: OmniSciTypes.h:179
int64_t size_
Definition: OmniSciTypes.h:159
DEVICE void setNull(int64_t index)
Definition: OmniSciTypes.h:180
int8_t * ptr
Definition: OmniSciTypes.h:77
DEVICE int32_t getOutputSrid() const
Definition: OmniSciTypes.h:89
int32_t compression
Definition: OmniSciTypes.h:95
DEVICE int8_t * getRingSizes()
Definition: OmniSciTypes.h:117
std::string typeName(const T *v)
Definition: toString.h:85
DEVICE int64_t getSize() const
Definition: OmniSciTypes.h:83
DEVICE int32_t getCompression() const
Definition: OmniSciTypes.h:85
T * ptr
Definition: OmniSciTypes.h:42
DEVICE T & operator[](const unsigned int index)
Definition: OmniSciTypes.h:64
int8_t * ptr_coords
Definition: OmniSciTypes.h:130
DEVICE int64_t size() const
Definition: OmniSciTypes.h:223
DEVICE int32_t getOutputSrid() const
Definition: OmniSciTypes.h:153
DEVICE bool isNull() const
Definition: OmniSciTypes.h:68
int32_t input_srid
Definition: OmniSciTypes.h:114
int32_t input_srid
Definition: OmniSciTypes.h:96
DEVICE int32_t getOutputSrid() const
Definition: OmniSciTypes.h:105