OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TableFunctionOps.cpp
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 #ifdef EXECUTE_INCLUDE
18 
20 
21 /*
22  set_output_row_size sets the row size of output Columns and
23  allocates the corresponding column buffers.
24 
25  `set_output_row_size` can be called exactly one time when entering a
26  table function (when not using TableFunctionSpecifiedParameter
27  sizer) or within a table function (when using
28  TableFunctionSpecifiedParameter sizer).
29 
30  For thread-safety, it is recommended to have `TableFunctionManager
31  mgr` as a first argument in a UDTF definition and then use
32  `mgr.set_output_row_size(..)` to set the row size of output columns,
33 */
34 
36  int8_t* mgr_ptr,
37  int64_t num_rows) {
38  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
39  if (num_rows < 0) {
40  throw TableFunctionError(
41  "set_output_row_size: expected non-negative row size but got " +
42  std::to_string(num_rows));
43  }
44  mgr->allocate_output_buffers(num_rows);
45 }
46 
47 extern "C" DEVICE RUNTIME_EXPORT void set_output_row_size(int64_t num_rows) {
49  TableFunctionManager_set_output_row_size(reinterpret_cast<int8_t*>(mgr), num_rows);
50 }
51 
52 /*
53  set_output_item_values_total_number sets the total number of items
54  values in the index-th output Column of non-scalars.
55 
56  set_output_item_values_total_number must be called before
57  set_output_row_size.
58 */
59 extern "C" DEVICE RUNTIME_EXPORT void
61  int8_t* mgr_ptr,
62  int32_t index,
63  int64_t output_item_values_total_number) {
64  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
65  if (output_item_values_total_number < 0) {
66  throw TableFunctionError(
67  "set_output_item_values_total_number: expected non-negative size but "
68  "got " +
69  std::to_string(output_item_values_total_number));
70  }
71  mgr->set_output_item_values_total_number(index, output_item_values_total_number);
72 }
73 
75  int32_t index,
76  int64_t output_item_values_total_number) {
79  reinterpret_cast<int8_t*>(mgr), index, output_item_values_total_number);
80 }
81 
82 /*
83  NOTE: use set_output_item_values_total_number instead!
84 
85  set_output_array_values_total_number sets the total number of array
86  values in the index-th output Column of arrays.
87 
88  set_output_array_values_total_number must be called before
89  set_output_row_size.
90 */
91 extern "C" DEVICE RUNTIME_EXPORT void
93  int8_t* mgr_ptr,
94  int32_t index,
95  int64_t output_array_values_total_number) {
96  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
97  if (output_array_values_total_number < 0) {
98  throw TableFunctionError(
99  "set_output_array_values_total_number: expected non-negative size but "
100  "got " +
101  std::to_string(output_array_values_total_number));
102  }
103  mgr->set_output_array_values_total_number(index, output_array_values_total_number);
104 }
105 
107  int32_t index,
108  int64_t output_array_values_total_number) {
111  reinterpret_cast<int8_t*>(mgr), index, output_array_values_total_number);
112 }
113 
114 /*
115  TableFunctionManager_register_output_column stores the pointer of
116  output Column instance so that when the buffers of output columns
117  are allocated, the Column instance members ptr_ and size_ can be
118  updated.
119 
120  TableFunctionManager_register_output_column is used internally when
121  creating output Column instances prior entering the table function.
122  */
123 
124 extern "C" DEVICE RUNTIME_EXPORT void
125 TableFunctionManager_register_output_column(int8_t* mgr_ptr, int32_t index, int8_t* ptr) {
126  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
127  CHECK(mgr);
128  mgr->set_output_column(index, ptr);
129 }
130 
131 /*
132  table_function_error allows code from within a table function to
133  set an error message that can be accessed from the execution context.
134  This allows the error message to be propagated as an exception message.
135  */
136 extern "C" DEVICE RUNTIME_EXPORT int32_t
137 TableFunctionManager_error_message(int8_t* mgr_ptr, const char* message) {
138  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
139  CHECK(mgr);
140  if (message != nullptr) {
141  mgr->set_error_message(message);
142  } else {
143  mgr->set_error_message("no error message set");
144  }
146 }
147 
148 extern "C" DEVICE RUNTIME_EXPORT int32_t table_function_error(const char* message) {
150  return TableFunctionManager_error_message(reinterpret_cast<int8_t*>(mgr), message);
151 }
152 
155 }
156 
157 /*
158  TableFunctionManager_get_singleton is used internally to get the
159  pointer to global singleton of TableFunctionManager, if initialized,
160  otherwise throws runtime error.
161 */
164  if (!mgr) {
165  throw TableFunctionError("uninitialized TableFunctionManager singleton");
166  }
167  return reinterpret_cast<int8_t*>(mgr);
168 }
169 
171  int8_t* mgr_ptr,
172  const char* key,
173  const uint8_t* raw_bytes,
174  const size_t num_bytes,
175  const TableFunctionMetadataType value_type) {
176  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
177  CHECK(mgr);
178  mgr->set_metadata(key, raw_bytes, num_bytes, value_type);
179 }
180 
182  int8_t* mgr_ptr,
183  const char* key,
184  const uint8_t*& raw_bytes,
185  size_t& num_bytes,
186  TableFunctionMetadataType& value_type) {
187  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
188  CHECK(mgr);
189  mgr->get_metadata(key, raw_bytes, num_bytes, value_type);
190 }
191 
192 extern "C" DEVICE RUNTIME_EXPORT int32_t
193 TableFunctionManager_getNewDictDbId(int8_t* mgr_ptr) {
194  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
195  CHECK(mgr);
196  return mgr->getNewDictDbId();
197 }
198 
199 extern "C" DEVICE RUNTIME_EXPORT int32_t
200 TableFunctionManager_getNewDictId(int8_t* mgr_ptr) {
201  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
202  CHECK(mgr);
203  return mgr->getNewDictId();
204 }
205 
207  int8_t* mgr_ptr,
208  int32_t db_id,
209  int32_t dict_id) {
210  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
211  CHECK(mgr);
212  return mgr->getStringDictionaryProxy(db_id, dict_id);
213 }
214 
215 DEVICE RUNTIME_EXPORT std::string TableFunctionManager_getString(int8_t* mgr_ptr,
216  int32_t db_id,
217  int32_t dict_id,
218  int32_t string_id) {
219  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
220  CHECK(mgr);
221  return mgr->getString(db_id, dict_id, string_id);
222 }
223 
225  int8_t* mgr_ptr,
226  int64_t element_count,
227  int64_t element_size) {
228  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
229  CHECK(mgr);
230  return mgr->makeBuffer(element_count, element_size);
231 }
232 
233 extern "C" DEVICE RUNTIME_EXPORT int32_t
235  int32_t db_id,
236  int32_t dict_id,
237  std::string str) {
238  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
239  CHECK(mgr);
240  return mgr->getOrAddTransient(db_id, dict_id, str);
241 }
242 
243 extern "C" DEVICE RUNTIME_EXPORT void ColumnArray_getArray(int8_t* flatbuffer,
244  const int64_t index,
245  const int64_t expected_numel,
246  int8_t*& ptr,
247  int64_t& size,
248  bool& is_null) {
249  FlatBufferManager m{flatbuffer};
250  FlatBufferManager::Status status{};
251  if (m.isNestedArray()) {
253  if (!m.isSpecified(index)) {
254  if (expected_numel < 0) {
255 #ifndef __CUDACC__
256  throw std::runtime_error("getArray failed: " + ::toString(status));
257 #endif
258  }
259  status = m.setItem(index, nullptr, expected_numel);
260  if (status != FlatBufferManager::Status::Success) {
261 #ifndef __CUDACC__
262  throw std::runtime_error("getArray failed[setItem]: " + ::toString(status));
263 #endif
264  }
265  }
266  status = m.getItem(index, item);
267  if (status == FlatBufferManager::Status::Success) {
268 #ifndef __CUDACC__
269  CHECK_EQ(item.nof_sizes, 0);
270 #endif
271  ptr = item.values;
272  size = item.nof_values;
273  is_null = item.is_null;
274  if (expected_numel >= 0 && expected_numel != item.nof_values) {
275 #ifndef __CUDACC__
276  throw std::runtime_error("getArray failed: unexpected size " +
277  ::toString(item.nof_values) + ", expected " +
278  ::toString(expected_numel));
279 #endif
280  }
281 
282  } else {
283 #ifndef __CUDACC__
284  throw std::runtime_error("getArray failed: " + ::toString(status));
285 #endif
286  }
287  }
288 }
289 
290 // For BC (rbc <= 0.9):
291 extern "C" DEVICE RUNTIME_EXPORT void ColumnArray_getItem(int8_t* flatbuffer,
292  const int64_t index,
293  const int64_t expected_numel,
294  int8_t*& ptr,
295  int64_t& size,
296  bool& is_null,
297  int64_t sizeof_T) {
298  ColumnArray_getArray(flatbuffer, index, expected_numel, ptr, size, is_null);
299  size *= sizeof_T;
300 }
301 
302 extern "C" DEVICE RUNTIME_EXPORT bool ColumnArray_isNull(int8_t* flatbuffer,
303  int64_t index) {
304  FlatBufferManager m{flatbuffer};
305  bool is_null = false;
306  auto status = m.isNull(index, is_null);
307 #ifndef __CUDACC__
308  if (status != FlatBufferManager::Status::Success) {
309  throw std::runtime_error("isNull failed: " + ::toString(status));
310  }
311 #endif
312  return is_null;
313 }
314 
315 extern "C" DEVICE RUNTIME_EXPORT void ColumnArray_setNull(int8_t* flatbuffer,
316  int64_t index) {
317  FlatBufferManager m{flatbuffer};
318  auto status = m.setNull(index);
319 #ifndef __CUDACC__
320  if (status != FlatBufferManager::Status::Success) {
321  throw std::runtime_error("setNull failed: " + ::toString(status));
322  }
323 #endif
324 }
325 
326 extern "C" DEVICE RUNTIME_EXPORT void ColumnArray_setArray(int8_t* flatbuffer,
327  int64_t index,
328  const int8_t* ptr,
329  int64_t size,
330  bool is_null) {
331  FlatBufferManager m{flatbuffer};
333  if (is_null) {
334  status = m.setNull(index);
335  } else {
336  status = m.setItem(index, ptr, size);
337  }
338 #ifndef __CUDACC__
339  if (status != FlatBufferManager::Status::Success) {
340  throw std::runtime_error("setItem failed: " + ::toString(status));
341  }
342 #endif
343 }
344 
345 // For BC (rbc <= 0.9):
346 extern "C" DEVICE RUNTIME_EXPORT void ColumnArray_setItem(int8_t* flatbuffer,
347  int64_t index,
348  const int8_t* ptr,
349  int64_t size,
350  bool is_null,
351  int64_t sizeof_T) {
352  FlatBufferManager m{flatbuffer};
353  CHECK_EQ(sizeof_T, m.getValueSize());
354  ColumnArray_setArray(flatbuffer, index, ptr, size, is_null);
355 }
356 
357 extern "C" DEVICE RUNTIME_EXPORT void ColumnArray_concatArray(int8_t* flatbuffer,
358  int64_t index,
359  const int8_t* ptr,
360  int64_t size,
361  bool is_null) {
362  FlatBufferManager m{flatbuffer};
364  if (is_null) {
365  if (m.isSpecified(index)) {
366  // concat(item, null) -> item
367  status = FlatBufferManager::Status::Success;
368  } else {
369  // concat(unspecified, null) -> null
370  status = m.setNull(index);
371  }
372  } else {
373  status = m.concatItem(index, ptr, size);
374  }
375 #ifndef __CUDACC__
376  if (status != FlatBufferManager::Status::Success) {
377  throw std::runtime_error("concatItem failed: " + ::toString(status));
378  }
379 #endif
380 }
381 
382 // For BC (rbc <= 0.9):
383 extern "C" DEVICE RUNTIME_EXPORT void ColumnArray_concatItem(int8_t* flatbuffer,
384  int64_t index,
385  const int8_t* ptr,
386  int64_t size,
387  bool is_null,
388  int64_t sizeof_T) {
389  FlatBufferManager m{flatbuffer};
390  CHECK_EQ(sizeof_T, m.getValueSize());
391  ColumnArray_concatArray(flatbuffer, index, ptr, size, is_null);
392 }
393 
394 #endif // EXECUTE_INCLUDE
void set_output_column(int32_t index, int8_t *ptr)
#define CHECK_EQ(x, y)
Definition: Logger.h:301
std::string getString(int32_t db_id, int32_t dict_id, int32_t string_id)
Definition: heavydbTypes.h:422
void set_output_array_values_total_number(int32_t index, int64_t output_array_values_total_number)
Definition: heavydbTypes.h:361
EXTENSION_NOINLINE_HOST void set_output_array_values_total_number(int32_t index, int64_t output_array_values_total_number)
EXTENSION_NOINLINE_HOST void set_output_row_size(int64_t num_rows)
void get_metadata(const std::string &key, T &value)
Definition: heavydbTypes.h:397
EXTENSION_NOINLINE_HOST int32_t table_function_success_code()
EXTENSION_NOINLINE_HOST int8_t * TableFunctionManager_get_singleton()
EXTENSION_NOINLINE_HOST void ColumnArray_setNull(int8_t *flatbuffer, int64_t index)
void set_error_message(const char *msg)
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)
int32_t getOrAddTransient(int32_t db_id, int32_t dict_id, std::string str)
Definition: heavydbTypes.h:426
int32_t getNewDictDbId()
Definition: heavydbTypes.h:411
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)
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)
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)
std::string toString(const QueryDescriptionType &type)
Definition: Types.h:64
EXTENSION_NOINLINE_HOST void ColumnArray_concatArray(int8_t *flatbuffer, int64_t index, const int8_t *ptr, int64_t size, bool is_null)
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)
static TableFunctionManager *& get_singleton_internal()
std::string to_string(char const *&&v)
#define DEVICE
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_error_message(int8_t *mgr_ptr, const char *message)
EXTENSION_NOINLINE_HOST int8_t * TableFunctionManager_getStringDictionaryProxy(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id)
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)
TableFunctionMetadataType
EXTENSION_NOINLINE_HOST int32_t table_function_error(const char *message)
#define RUNTIME_EXPORT
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)
int8_t * makeBuffer(int64_t element_count, int64_t element_size)
Definition: heavydbTypes.h:430
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)
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)
void set_output_item_values_total_number(int32_t index, int64_t output_item_values_total_number)
Definition: heavydbTypes.h:367
EXTENSION_NOINLINE_HOST void set_output_item_values_total_number(int32_t index, int64_t output_item_values_total_number)
EXTENSION_NOINLINE_HOST void TableFunctionManager_set_output_row_size(int8_t *mgr_ptr, int64_t num_rows)
void set_metadata(const std::string &key, const T &value)
Definition: heavydbTypes.h:388
#define CHECK(condition)
Definition: Logger.h:291
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_getNewDictId(int8_t *mgr_ptr)
HOST DEVICE Status isNull(const int64_t index[NDIM], const size_t n, bool &is_null)
Definition: FlatBuffer.h:1052
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)
EXTENSION_NOINLINE_HOST void ColumnArray_setArray(int8_t *flatbuffer, int64_t index, const int8_t *ptr, int64_t size, bool is_null)
HOST DEVICE Status setNull(int64_t index)
Definition: FlatBuffer.h:2029
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_getOrAddTransient(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id, std::string str)