OmniSciDB  c1a53651b2
 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_array_values_total_number sets the total number of array
54  values in the index-th output Column of arrays.
55 
56  set_output_array_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_array_values_total_number) {
64  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
65  if (output_array_values_total_number < 0) {
66  throw TableFunctionError(
67  "set_output_array_values_total_number: expected non-negative array size but "
68  "got " +
69  std::to_string(output_array_values_total_number));
70  }
71  mgr->set_output_array_values_total_number(index, output_array_values_total_number);
72 }
73 
75  int32_t index,
76  int64_t output_array_values_total_number) {
79  reinterpret_cast<int8_t*>(mgr), index, output_array_values_total_number);
80 }
81 
82 /*
83  TableFunctionManager_register_output_column stores the pointer of
84  output Column instance so that when the buffers of output columns
85  are allocated, the Column instance members ptr_ and size_ can be
86  updated.
87 
88  TableFunctionManager_register_output_column is used internally when
89  creating output Column instances prior entering the table function.
90  */
91 
92 extern "C" DEVICE RUNTIME_EXPORT void
93 TableFunctionManager_register_output_column(int8_t* mgr_ptr, int32_t index, int8_t* ptr) {
94  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
95  CHECK(mgr);
96  mgr->set_output_column(index, ptr);
97 }
98 
99 /*
100  table_function_error allows code from within a table function to
101  set an error message that can be accessed from the execution context.
102  This allows the error message to be propagated as an exception message.
103  */
104 extern "C" DEVICE RUNTIME_EXPORT int32_t
105 TableFunctionManager_error_message(int8_t* mgr_ptr, const char* message) {
106  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
107  CHECK(mgr);
108  if (message != nullptr) {
109  mgr->set_error_message(message);
110  } else {
111  mgr->set_error_message("no error message set");
112  }
114 }
115 
116 extern "C" DEVICE RUNTIME_EXPORT int32_t table_function_error(const char* message) {
118  return TableFunctionManager_error_message(reinterpret_cast<int8_t*>(mgr), message);
119 }
120 /*
121  TableFunctionManager_get_singleton is used internally to get the
122  pointer to global singleton of TableFunctionManager, if initialized,
123  otherwise throws runtime error.
124 */
127  if (!mgr) {
128  throw TableFunctionError("uninitialized TableFunctionManager singleton");
129  }
130  return reinterpret_cast<int8_t*>(mgr);
131 }
132 
134  int8_t* mgr_ptr,
135  const char* key,
136  const uint8_t* raw_bytes,
137  const size_t num_bytes,
138  const TableFunctionMetadataType value_type) {
139  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
140  CHECK(mgr);
141  mgr->set_metadata(key, raw_bytes, num_bytes, value_type);
142 }
143 
145  int8_t* mgr_ptr,
146  const char* key,
147  const uint8_t*& raw_bytes,
148  size_t& num_bytes,
149  TableFunctionMetadataType& value_type) {
150  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
151  CHECK(mgr);
152  mgr->get_metadata(key, raw_bytes, num_bytes, value_type);
153 }
154 
155 extern "C" DEVICE RUNTIME_EXPORT int32_t
156 TableFunctionManager_getNewDictDbId(int8_t* mgr_ptr) {
157  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
158  CHECK(mgr);
159  return mgr->getNewDictDbId();
160 }
161 
162 extern "C" DEVICE RUNTIME_EXPORT int32_t
163 TableFunctionManager_getNewDictId(int8_t* mgr_ptr) {
164  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
165  CHECK(mgr);
166  return mgr->getNewDictId();
167 }
168 
170  int8_t* mgr_ptr,
171  int32_t db_id,
172  int32_t dict_id) {
173  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
174  CHECK(mgr);
175  return mgr->getStringDictionaryProxy(db_id, dict_id);
176 }
177 
178 DEVICE RUNTIME_EXPORT std::string TableFunctionManager_getString(int8_t* mgr_ptr,
179  int32_t db_id,
180  int32_t dict_id,
181  int32_t string_id) {
182  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
183  CHECK(mgr);
184  return mgr->getString(db_id, dict_id, string_id);
185 }
186 
188  int8_t* mgr_ptr,
189  int32_t db_id,
190  int32_t dict_id,
191  int32_t string_id) {
192  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
193  CHECK(mgr);
194  return mgr->getString(db_id, dict_id, string_id).c_str();
195 }
196 
197 extern "C" DEVICE RUNTIME_EXPORT int32_t
199  int32_t db_id,
200  int32_t dict_id,
201  std::string str) {
202  auto mgr = reinterpret_cast<TableFunctionManager*>(mgr_ptr);
203  CHECK(mgr);
204  return mgr->getOrAddTransient(db_id, dict_id, str);
205 }
206 
207 #endif // EXECUTE_INCLUDE
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)
EXTENSION_NOINLINE_HOST int8_t * TableFunctionManager_get_singleton()
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_getNewDictDbId(int8_t *mgr_ptr)
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)
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)
std::string TableFunctionManager_getString(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id, int32_t string_id)
TableFunctionMetadataType
EXTENSION_NOINLINE_HOST int32_t table_function_error(const char *message)
#define RUNTIME_EXPORT
EXTENSION_NOINLINE_HOST const char * TableFunctionManager_getCString(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id, int32_t string_id)
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)
EXTENSION_NOINLINE_HOST void TableFunctionManager_set_output_row_size(int8_t *mgr_ptr, int64_t num_rows)
#define CHECK(condition)
Definition: Logger.h:291
EXTENSION_NOINLINE_HOST int32_t TableFunctionManager_getNewDictId(int8_t *mgr_ptr)
static TableFunctionManager * get_singleton()
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 int32_t TableFunctionManager_getOrAddTransient(int8_t *mgr_ptr, int32_t db_id, int32_t dict_id, std::string str)