OmniSciDB  c1a53651b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TableFunctions.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 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 "../../QueryEngine/heavydbTypes.h"
20 
21 // clang-format off
22 /*
23  UDTF: row_copier(Column<double>, RowMultiplier) -> Column<double>
24  UDTF: row_copier_text(Column<TextEncodingDict>, RowMultiplier) -> Column<TextEncodingDict> | input_id=args<0>
25  UDTF: row_copier2__cpu__(Column<double>, int) -> Column<double>, Column<double>
26 */
27 // clang-format on
28 EXTENSION_NOINLINE int32_t row_copier(const Column<double>& input_col,
29  int copy_multiplier,
30  Column<double>& output_col) {
31  int32_t output_row_count = copy_multiplier * input_col.size();
32  if (output_row_count > 100) {
33  // Test failure propagation.
34  return -1;
35  }
36  if (output_col.size() != output_row_count) {
37  return -1;
38  }
39 
40 #ifdef __CUDACC__
41  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
42  int32_t stop = static_cast<int32_t>(input_col.size());
43  int32_t step = blockDim.x * gridDim.x;
44 #else
45  auto start = 0;
46  auto stop = input_col.size();
47  auto step = 1;
48 #endif
49 
50  for (auto i = start; i < stop; i += step) {
51  for (int c = 0; c < copy_multiplier; c++) {
52  output_col[i + (c * input_col.size())] = input_col[i];
53  }
54  }
55 
56  return output_row_count;
57 }
58 
59 #ifndef __CUDACC__
60 
62  int copy_multiplier,
63  Column<double>& output_col,
64  Column<double>& output_col2) {
65  if (copy_multiplier == -1) {
66  // Test UDTF return without allocating output columns, expect
67  // empty output columns.
68  return 0;
69  }
70  if (copy_multiplier == -2) {
71  // Test UDTF return without allocating output columns but
72  // returning positive row size, expect runtime error.
73  return 1;
74  }
75 #ifndef __CUDACC__
76  if (copy_multiplier == -3) {
77  // Test UDTF throw before allocating output columns, expect
78  // runtime error.
79  throw std::runtime_error("row_copier2: throw before calling set_output_row_size");
80  }
81  if (copy_multiplier == -4) {
82  // Test UDTF throw after allocating output columns, expect
83  // runtime error.
85  throw std::runtime_error("row_copier2: throw after calling set_output_row_size");
86  }
87 #endif
88  if (copy_multiplier == -5) {
89  // Test UDTF setting negative row size, expect runtime error.
91  }
92  int32_t output_row_count = copy_multiplier * input_col.size();
93  /* set_output_row_size can be used only when an UDTF is executed on CPU */
94  set_output_row_size(output_row_count);
95  auto result = row_copier(input_col, copy_multiplier, output_col);
96  if (result >= 0) {
97  result = row_copier(input_col, copy_multiplier, output_col2);
98  }
99  return result;
100 }
101 
102 #endif // #ifndef __CUDACC__
103 
105  int copy_multiplier,
106  Column<TextEncodingDict>& output_col) {
107  int32_t output_row_count = copy_multiplier * input_col.size();
108  if (output_row_count > 100) {
109  // Test failure propagation.
110  return -1;
111  }
112  if (output_col.size() != output_row_count) {
113  return -2;
114  }
115 
116 #ifdef __CUDACC__
117  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
118  int32_t stop = static_cast<int32_t>(input_col.size());
119  int32_t step = blockDim.x * gridDim.x;
120 #else
121  auto start = 0;
122  auto stop = input_col.size();
123  auto step = 1;
124 #endif
125 
126  for (auto i = start; i < stop; i += step) {
127  for (int c = 0; c < copy_multiplier; c++) {
128  output_col[i + (c * input_col.size())] = input_col[i];
129  }
130  }
131 
132  return output_row_count;
133 }
134 
135 /*
136  UDTF: row_adder(RowMultiplier<1>, Cursor<ColumnDouble, ColumnDouble>) -> ColumnDouble
137 */
138 EXTENSION_NOINLINE int32_t row_adder(const int copy_multiplier,
139  const Column<double>& input_col1,
140  const Column<double>& input_col2,
141  Column<double>& output_col) {
142  int32_t output_row_count = copy_multiplier * input_col1.size();
143  if (output_row_count > 100) {
144  // Test failure propagation.
145  return -1;
146  }
147  if (output_col.size() != output_row_count) {
148  return -1;
149  }
150 
151 #ifdef __CUDACC__
152  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
153  int32_t stop = static_cast<int32_t>(input_col1.size());
154  int32_t step = blockDim.x * gridDim.x;
155 #else
156  auto start = 0;
157  auto stop = input_col1.size();
158  auto step = 1;
159 #endif
160  auto stride = input_col1.size();
161  for (auto i = start; i < stop; i += step) {
162  for (int c = 0; c < copy_multiplier; c++) {
163  if (input_col1.isNull(i) || input_col2.isNull(i)) {
164  output_col.setNull(i + (c * stride));
165  } else {
166  output_col[i + (c * stride)] = input_col1[i] + input_col2[i];
167  }
168  }
169  }
170 
171  return output_row_count;
172 }
173 
174 // clang-format off
175 /*
176  UDTF: row_addsub(RowMultiplier, Cursor<double, double>) -> Column<double>, Column<double>
177 */
178 // clang-format on
179 EXTENSION_NOINLINE int32_t row_addsub(const int copy_multiplier,
180  const Column<double>& input_col1,
181  const Column<double>& input_col2,
182  Column<double>& output_col1,
183  Column<double>& output_col2) {
184  int32_t output_row_count = copy_multiplier * input_col1.size();
185  if (output_row_count > 100) {
186  // Test failure propagation.
187  return -1;
188  }
189  if ((output_col1.size() != output_row_count) ||
190  (output_col2.size() != output_row_count)) {
191  return -1;
192  }
193 
194 #ifdef __CUDACC__
195  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
196  int32_t stop = static_cast<int32_t>(input_col1.size());
197  int32_t step = blockDim.x * gridDim.x;
198 #else
199  auto start = 0;
200  auto stop = input_col1.size();
201  auto step = 1;
202 #endif
203  auto stride = input_col1.size();
204  for (auto i = start; i < stop; i += step) {
205  for (int c = 0; c < copy_multiplier; c++) {
206  output_col1[i + (c * stride)] = input_col1[i] + input_col2[i];
207  output_col2[i + (c * stride)] = input_col1[i] - input_col2[i];
208  }
209  }
210  return output_row_count;
211 }
212 
213 // clang-format off
214 /*
215  UDTF: get_max_with_row_offset__cpu_(Cursor<int>, Constant<1>) -> Column<int>, Column<int>
216 */
217 // clang-format on
218 
219 #ifndef __CUDACC__
220 
223  Column<int>& output_max_col,
224  Column<int>& output_max_row_col) {
225  if ((output_max_col.size() != 1) || output_max_row_col.size() != 1) {
226  return -1;
227  }
228  auto start = 0;
229  auto stop = input_col.size();
230  auto step = 1;
231 
232  int curr_max = -2147483648;
233  int curr_max_row = -1;
234  for (auto i = start; i < stop; i += step) {
235  if (input_col[i] > curr_max) {
236  curr_max = input_col[i];
237  curr_max_row = i;
238  }
239  }
240  output_max_col[0] = curr_max;
241  output_max_row_col[0] = curr_max_row;
242  return 1;
243 }
244 
245 #endif // #ifndef __CUDACC__
246 
247 // clang-format off
248 /*
249  UDTF: column_list_get__cpu_(ColumnList<double>, int, RowMultiplier) -> Column<double>
250 */
251 // clang-format on
252 
253 #ifndef __CUDACC__
254 
256  const int index,
257  const int m,
258  Column<double>& col) {
259  col = col_list[index]; // copy the data of col_list item to output column
260  return col.size();
261 }
262 
263 #endif // #ifndef __CUDACC__
264 
265 // clang-format off
266 /*
267  UDTF: column_list_first_last(ColumnList<double>, RowMultiplier) -> Column<double>,
268  Column<double>
269 */
270 // clang-format on
272  const int m,
273  Column<double>& col1,
274  Column<double>& col2) {
275  col1 = col_list[0];
276  col2 = col_list[col_list.numCols() - 1];
277  return col1.size();
278 }
279 
280 // clang-format off
281 /*
282  UDTF: column_list_row_sum__cpu_(Cursor<ColumnList<int32_t>>) -> Column<int32_t>
283  */
284 // clang-format on
285 
286 #ifndef __CUDACC__
287 
290  int32_t output_num_rows = input.numCols();
291  set_output_row_size(output_num_rows);
292  for (int i = 0; i < output_num_rows; i++) {
293  auto col = input[i];
294  int32_t s = 0;
295  for (int j = 0; j < col.size(); j++) {
296  s += col[j];
297  }
298  out[i] = s;
299  }
300  return output_num_rows;
301 }
302 
303 #endif // #ifndef __CUDACC__
EXTENSION_NOINLINE_HOST int32_t get_max_with_row_offset__cpu_(const Column< int > &input_col, Column< int > &output_max_col, Column< int > &output_max_row_col)
EXTENSION_NOINLINE int32_t row_adder(const int copy_multiplier, const Column< double > &input_col1, const Column< double > &input_col2, Column< double > &output_col)
EXTENSION_NOINLINE int32_t row_addsub(const int copy_multiplier, const Column< double > &input_col1, const Column< double > &input_col2, Column< double > &output_col1, Column< double > &output_col2)
EXTENSION_NOINLINE_HOST void set_output_row_size(int64_t num_rows)
#define EXTENSION_NOINLINE
Definition: heavydbTypes.h:52
EXTENSION_NOINLINE_HOST int32_t column_list_row_sum__cpu_(const ColumnList< int32_t > &input, Column< int32_t > &out)
DEVICE int64_t size() const
Definition: heavydbTypes.h:751
EXTENSION_NOINLINE int32_t row_copier_text(const Column< TextEncodingDict > &input_col, int copy_multiplier, Column< TextEncodingDict > &output_col)
DEVICE int64_t numCols() const
EXTENSION_NOINLINE int32_t row_copier(const Column< double > &input_col, int copy_multiplier, Column< double > &output_col)
EXTENSION_NOINLINE_HOST int32_t row_copier2__cpu__(const Column< double > &input_col, int copy_multiplier, Column< double > &output_col, Column< double > &output_col2)
EXTENSION_NOINLINE int32_t column_list_first_last(const ColumnList< double > &col_list, const int m, Column< double > &col1, Column< double > &col2)
#define EXTENSION_NOINLINE_HOST
Definition: heavydbTypes.h:49
EXTENSION_NOINLINE_HOST int32_t column_list_get__cpu_(const ColumnList< double > &col_list, const int index, const int m, Column< double > &col)
DEVICE bool isNull(int64_t index) const
Definition: heavydbTypes.h:754
DEVICE void setNull(int64_t index)
Definition: heavydbTypes.h:755
DEVICE int64_t size() const
Definition: heavydbTypes.h:950