OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
heavydbColumnOps.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 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 #include "heavydbTypes.h"
18 
19 // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
20 // expose Column<GeoPoint> methods
21 
23  int64_t index,
24  int32_t output_srid,
26  result = col.getItem(index, output_srid);
27 }
28 
30  int64_t index) {
31  return col.isNull(index);
32 }
33 
35  int64_t index) {
36  col.setNull(index);
37 }
38 
40  int64_t index,
41  const Geo::Point2D& other) {
42  col.setItem(index, other);
43 }
44 
45 // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
46 // Column<Geo*> methods
47 
48 #define EXPOSE_isNull(RowType) \
49  extern "C" DEVICE RUNTIME_EXPORT bool Column##RowType##_isNull(Column<RowType>& col, \
50  int64_t index) { \
51  return col.isNull(index); \
52  }
53 
54 #define EXPOSE_setNull(RowType) \
55  extern "C" DEVICE RUNTIME_EXPORT void Column##RowType##_setNull(Column<RowType>& col, \
56  int64_t index) { \
57  col.setNull(index); \
58  }
59 
60 // copy elements by hand as operator= expects a valid Geo type as rhs
61 #define EXPOSE_getItem(RowType, TypeName) \
62  extern "C" DEVICE RUNTIME_EXPORT void Column##RowType##_getItem( \
63  Column<RowType>& col, int64_t index, TypeName& rhs) { \
64  TypeName out = col.getItem(index); \
65  rhs.flatbuffer_ = out.flatbuffer_; \
66  rhs.n_ = out.n_; \
67  for (int i = 0; i < NESTED_ARRAY_NDIM; i++) \
68  rhs.index_[i] = out.index_[i]; \
69  }
70 
71 #define EXPOSE_setItem(RowType, TypeName) \
72  extern "C" DEVICE RUNTIME_EXPORT void Column##RowType##_setItem( \
73  Column<RowType>& col, int64_t index, TypeName& rhs) { \
74  col.setItem(index, rhs); \
75  }
76 
77 #define EXPOSE_getNofValues(RowType) \
78  extern "C" DEVICE RUNTIME_EXPORT int64_t Column##RowType##_getNofValues( \
79  Column<RowType>& col) { \
80  return col.getNofValues(); \
81  }
82 
83 #define EXPOSE(RowType, TypeName) \
84  EXPOSE_isNull(RowType); \
85  EXPOSE_setNull(RowType); \
86  EXPOSE_getNofValues(RowType); \
87  EXPOSE_getItem(RowType, TypeName); \
88  EXPOSE_setItem(RowType, TypeName);
89 
96 
97 // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
98 // Geo* methods
99 
100 #define EXPOSE_Geo_isNull(BaseType) \
101  extern "C" DEVICE RUNTIME_EXPORT bool Geo##BaseType##_isNull(Geo::BaseType& base) { \
102  return base.isNull(); \
103  }
104 
105 #define EXPOSE_Geo_size(BaseType) \
106  extern "C" DEVICE RUNTIME_EXPORT size_t Geo##BaseType##_size(Geo::BaseType& base) { \
107  return base.size(); \
108  }
109 
111  double* data,
112  int64_t* dim_x,
113  int64_t* dim_y,
114  int64_t size_dim_x,
115  int64_t size_dim_y) {
116  std::vector<std::vector<std::vector<double>>> coords(size_dim_x);
117  int l = 0;
118  for (int i = 0; i < size_dim_x; i++) {
119  coords[i].resize(size_dim_y);
120  for (int j = 0; j < size_dim_y; j++) {
121  coords[i][j].resize(dim_y[j]);
122  for (int k = 0; k < dim_y[j]; k++) {
123  coords[i][j][k] = data[l++];
124  }
125  }
126  }
127  base.fromCoords(coords);
128 }
129 
130 #define EXPOSE_Geo_fromCoords_vec2(BaseType) \
131  extern "C" DEVICE RUNTIME_EXPORT void Geo##BaseType##_fromCoords( \
132  Geo::BaseType& base, double* data, int64_t* indices, int64_t nrows) { \
133  std::vector<std::vector<double>> coords; \
134  int64_t offset = 0; \
135  for (int64_t i = 0; i < nrows; i++) { \
136  int64_t size = indices[i]; \
137  std::vector<double> row(data + offset, data + offset + size); \
138  coords.push_back(row); \
139  offset += size; \
140  } \
141  base.fromCoords(coords); \
142  }
143 
144 // Can this function receive anything other than Array<double>?
145 #define EXPOSE_Geo_fromCoords_vec(BaseType) \
146  extern "C" DEVICE RUNTIME_EXPORT void Geo##BaseType##_fromCoords( \
147  Geo::BaseType& base, double* lst, int64_t size) { \
148  std::vector<double> coords(lst, lst + size); \
149  base.fromCoords(coords); \
150  }
151 
152 #define EXPOSE_Geo_toCoords_vec(BaseType) \
153  extern "C" DEVICE RUNTIME_EXPORT double Geo##BaseType##_toCoords_get_value( \
154  Geo::BaseType& base, int64_t index) { \
155  Geo::Point2D p = base[index / 2]; \
156  return (index % 2 == 0) ? p.x : p.y; \
157  }
158 
159 #define EXPOSE_Geo_toCoords_vec2(BaseType) \
160  extern "C" DEVICE RUNTIME_EXPORT double Geo##BaseType##_toCoords_get_value( \
161  const Geo::BaseType& base, int64_t ring_index, int64_t coord_index) { \
162  Geo::Point2D p = base[ring_index][coord_index / 2]; \
163  return (coord_index % 2 == 0) ? p.x : p.y; \
164  }
165 
166 #define EXPOSE_Geo_getItem_Point2D(BaseType, ItemType) \
167  extern "C" DEVICE RUNTIME_EXPORT void Geo##BaseType##_getItem( \
168  Geo::BaseType& base, int64_t index, Geo::ItemType& result) { \
169  Geo::ItemType out = base.getItem(index); \
170  result.x = out.x; \
171  result.y = out.y; \
172  }
173 
174 #define EXPOSE_Geo_getItem(BaseType, ItemType) \
175  extern "C" DEVICE RUNTIME_EXPORT void Geo##BaseType##_getItem( \
176  Geo::BaseType& base, int64_t index, Geo::ItemType& result) { \
177  Geo::ItemType out = base.getItem(index); \
178  result.flatbuffer_ = out.flatbuffer_; \
179  result.n_ = out.n_; \
180  for (int i = 0; i < NESTED_ARRAY_NDIM; i++) \
181  result.index_[i] = out.index_[i]; \
182  }
183 
184 #define EXPOSE_Geo_Point2D(BaseType, ItemType) \
185  EXPOSE_Geo_isNull(BaseType); \
186  EXPOSE_Geo_size(BaseType); \
187  EXPOSE_Geo_getItem_Point2D(BaseType, ItemType);
188 
189 #define EXPOSE_Geo(BaseType, ItemType) \
190  EXPOSE_Geo_isNull(BaseType); \
191  EXPOSE_Geo_size(BaseType); \
192  EXPOSE_Geo_getItem(BaseType, ItemType);
193 
194 EXPOSE_Geo_Point2D(LineString, Point2D);
195 EXPOSE_Geo_Point2D(MultiPoint, Point2D);
196 EXPOSE_Geo(MultiLineString, LineString);
197 EXPOSE_Geo(Polygon, LineString);
198 EXPOSE_Geo(MultiPolygon, Polygon);
199 
200 // Expose fromCoords
201 EXPOSE_Geo_fromCoords_vec(LineString);
202 EXPOSE_Geo_fromCoords_vec(MultiPoint);
204 EXPOSE_Geo_fromCoords_vec2(MultiLineString);
205 
206 // Expose toCoords
207 EXPOSE_Geo_toCoords_vec(MultiPoint);
208 EXPOSE_Geo_toCoords_vec(LineString);
209 EXPOSE_Geo_toCoords_vec2(Polygon);
210 EXPOSE_Geo_toCoords_vec2(MultiLineString);
211 // Missing vec3 for MultiPolygon
212 
213 // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
214 
217  int64_t index,
218  int8_t* rhs) {
219  col.setItem(index, *(reinterpret_cast<TextEncodingNone*>(rhs)));
220 }
221 
224  int64_t index,
225  int8_t* rhs) {
226  col[index] += *(reinterpret_cast<TextEncodingNone*>(rhs));
227 }
228 
231  int64_t index,
232  int8_t* rhs) {
233  col[index] += *(reinterpret_cast<flatbuffer::TextEncodingNone*>(rhs));
234 }
235 
236 // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
237 
238 volatile bool avoid_opt_address_geo(void* address) {
239  return address != nullptr;
240 }
241 
242 #define LIST_FUNCTION_W_SUFFIX(RowType, Suffix) \
243  avoid_opt_address_geo(reinterpret_cast<void*>(Column##RowType##_##Suffix))
244 
245 #define LIST_GEO_fromCoords(RowType) \
246  avoid_opt_address_geo(reinterpret_cast<void*>(RowType##_fromCoords))
247 
248 #define LIST_Geo_toCoords_vec(RowType) \
249  avoid_opt_address_geo(reinterpret_cast<void*>(RowType##_toCoords_get_value))
250 
251 #define LIST_Geo_toCoords_vec2(RowType) \
252  avoid_opt_address_geo(reinterpret_cast<void*>(RowType##_toCoords_get_value))
253 
254 #define LIST_FUNCTIONS(RowType) \
255  LIST_FUNCTION_W_SUFFIX(RowType, getItem) && \
256  LIST_FUNCTION_W_SUFFIX(RowType, setItem) && \
257  LIST_FUNCTION_W_SUFFIX(RowType, isNull) && \
258  LIST_FUNCTION_W_SUFFIX(RowType, setNull) && \
259  LIST_FUNCTION_W_SUFFIX(RowType, getNofValues)
260 
262  bool ret = true;
263  ret &=
264  (avoid_opt_address_geo(reinterpret_cast<void*>(ColumnGeoPoint_getItem)) &&
265  avoid_opt_address_geo(reinterpret_cast<void*>(ColumnGeoPoint_isNull)) &&
266  avoid_opt_address_geo(reinterpret_cast<void*>(ColumnGeoPoint_setItem)) &&
267  avoid_opt_address_geo(reinterpret_cast<void*>(ColumnGeoPoint_setNull)) &&
268  avoid_opt_address_geo(reinterpret_cast<void*>(GeoLineString_toCoords_get_value)) &&
269  avoid_opt_address_geo(reinterpret_cast<void*>(ColumnGeoPoint_setNull)));
271  ret &= LIST_FUNCTIONS(GeoPolygon);
274 
275  // Geo fromCoords
281 
282  // Geo toCoords
287 
288  return ret;
289 }
DEVICE RUNTIME_EXPORT bool ColumnGeoPoint_isNull(const Column< GeoPoint > &col, int64_t index)
#define EXPOSE_Geo(BaseType, ItemType)
Simplified core of GeoJSON Polygon coordinates definition.
#define LIST_Geo_toCoords_vec(RowType)
#define LIST_GEO_fromCoords(RowType)
DEVICE void setNull(int64_t index)
Simplified core of GeoJSON MultiPolygon coordinates definition.
#define DEVICE
#define LIST_Geo_toCoords_vec2(RowType)
DEVICE Geo::Point2D getItem(const int64_t index, const int32_t output_srid=0) const
#define LIST_FUNCTIONS(RowType)
#define EXPOSE_Geo_fromCoords_vec(BaseType)
DEVICE RUNTIME_EXPORT void ColumnGeoPoint_setItem(Column< GeoPoint > &col, int64_t index, const Geo::Point2D &other)
DEVICE RUNTIME_EXPORT void ColumnTextEncodingNone_concatItem(Column< TextEncodingNone > &col, int64_t index, int8_t *rhs)
DEVICE RUNTIME_EXPORT void ColumnTextEncodingNone_setItem_fromBuffer(Column< TextEncodingNone > &col, int64_t index, int8_t *rhs)
DEVICE RUNTIME_EXPORT void ColumnGeoPoint_setNull(Column< GeoPoint > &col, int64_t index)
#define RUNTIME_EXPORT
DEVICE RUNTIME_EXPORT void GeoMultiPolygon_fromCoords(Geo::MultiPolygon &base, double *data, int64_t *dim_x, int64_t *dim_y, int64_t size_dim_x, int64_t size_dim_y)
DEVICE bool isNull(int64_t index) const
DEVICE RUNTIME_EXPORT void ColumnTextEncodingNone_concatItem_fromBuffer(Column< TextEncodingNone > &col, int64_t index, int8_t *rhs)
DEVICE RUNTIME_EXPORT void ColumnGeoPoint_getItem(const Column< GeoPoint > &col, int64_t index, int32_t output_srid, Geo::Point2D &result)
FlatBufferManager::Status fromCoords(const std::vector< CT > &coords)
volatile bool avoid_opt_address_geo(void *address)
#define EXPOSE_Geo_toCoords_vec(BaseType)
DEVICE void setItem(int64_t index, const Geo::Point2D &other)
#define EXPOSE_Geo_fromCoords_vec2(BaseType)
#define EXPOSE_Geo_Point2D(BaseType, ItemType)
bool functions_exist_geo_column()
DEVICE void setItem(int64_t index, const RowType &item)
#define EXPOSE(RowType, TypeName)
#define EXPOSE_Geo_toCoords_vec2(BaseType)