OmniSciDB  6686921089
 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/OmniSciTypes.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 
60  int copy_multiplier,
61  Column<double>& output_col,
62  Column<double>& output_col2) {
63  if (copy_multiplier == -1) {
64  // Test UDTF return without allocating output columns, expect
65  // empty output columns.
66  return 0;
67  }
68  if (copy_multiplier == -2) {
69  // Test UDTF return without allocating output columns but
70  // returning positive row size, expect runtime error.
71  return 1;
72  }
73 #ifndef __CUDACC__
74  if (copy_multiplier == -3) {
75  // Test UDTF throw before allocating output columns, expect
76  // runtime error.
77  throw std::runtime_error("row_copier2: throw before calling set_output_row_size");
78  }
79  if (copy_multiplier == -4) {
80  // Test UDTF throw after allocating output columns, expect
81  // runtime error.
83  throw std::runtime_error("row_copier2: throw after calling set_output_row_size");
84  }
85 #endif
86  if (copy_multiplier == -5) {
87  // Test UDTF setting negative row size, expect runtime error.
89  }
90  int32_t output_row_count = copy_multiplier * input_col.size();
91  /* set_output_row_size can be used only when an UDTF is executed on CPU */
92  set_output_row_size(output_row_count);
93  auto result = row_copier(input_col, copy_multiplier, output_col);
94  if (result >= 0) {
95  result = row_copier(input_col, copy_multiplier, output_col2);
96  }
97  return result;
98 }
99 
101  int copy_multiplier,
102  Column<TextEncodingDict>& output_col) {
103  int32_t output_row_count = copy_multiplier * input_col.size();
104  if (output_row_count > 100) {
105  // Test failure propagation.
106  return -1;
107  }
108  if (output_col.size() != output_row_count) {
109  return -2;
110  }
111 
112 #ifdef __CUDACC__
113  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
114  int32_t stop = static_cast<int32_t>(input_col.size());
115  int32_t step = blockDim.x * gridDim.x;
116 #else
117  auto start = 0;
118  auto stop = input_col.size();
119  auto step = 1;
120 #endif
121 
122  for (auto i = start; i < stop; i += step) {
123  for (int c = 0; c < copy_multiplier; c++) {
124  output_col[i + (c * input_col.size())] = input_col[i];
125  }
126  }
127 
128  return output_row_count;
129 }
130 
131 /*
132  UDTF: row_adder(RowMultiplier<1>, Cursor<ColumnDouble, ColumnDouble>) -> ColumnDouble
133 */
134 EXTENSION_NOINLINE int32_t row_adder(const int copy_multiplier,
135  const Column<double>& input_col1,
136  const Column<double>& input_col2,
137  Column<double>& output_col) {
138  int32_t output_row_count = copy_multiplier * input_col1.size();
139  if (output_row_count > 100) {
140  // Test failure propagation.
141  return -1;
142  }
143  if (output_col.size() != output_row_count) {
144  return -1;
145  }
146 
147 #ifdef __CUDACC__
148  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
149  int32_t stop = static_cast<int32_t>(input_col1.size());
150  int32_t step = blockDim.x * gridDim.x;
151 #else
152  auto start = 0;
153  auto stop = input_col1.size();
154  auto step = 1;
155 #endif
156  auto stride = input_col1.size();
157  for (auto i = start; i < stop; i += step) {
158  for (int c = 0; c < copy_multiplier; c++) {
159  if (input_col1.isNull(i) || input_col2.isNull(i)) {
160  output_col.setNull(i + (c * stride));
161  } else {
162  output_col[i + (c * stride)] = input_col1[i] + input_col2[i];
163  }
164  }
165  }
166 
167  return output_row_count;
168 }
169 
170 // clang-format off
171 /*
172  UDTF: row_addsub(RowMultiplier, Cursor<double, double>) -> Column<double>, Column<double>
173 */
174 // clang-format on
175 EXTENSION_NOINLINE int32_t row_addsub(const int copy_multiplier,
176  const Column<double>& input_col1,
177  const Column<double>& input_col2,
178  Column<double>& output_col1,
179  Column<double>& output_col2) {
180  int32_t output_row_count = copy_multiplier * input_col1.size();
181  if (output_row_count > 100) {
182  // Test failure propagation.
183  return -1;
184  }
185  if ((output_col1.size() != output_row_count) ||
186  (output_col2.size() != output_row_count)) {
187  return -1;
188  }
189 
190 #ifdef __CUDACC__
191  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
192  int32_t stop = static_cast<int32_t>(input_col1.size());
193  int32_t step = blockDim.x * gridDim.x;
194 #else
195  auto start = 0;
196  auto stop = input_col1.size();
197  auto step = 1;
198 #endif
199  auto stride = input_col1.size();
200  for (auto i = start; i < stop; i += step) {
201  for (int c = 0; c < copy_multiplier; c++) {
202  output_col1[i + (c * stride)] = input_col1[i] + input_col2[i];
203  output_col2[i + (c * stride)] = input_col1[i] - input_col2[i];
204  }
205  }
206  return output_row_count;
207 }
208 
209 // clang-format off
210 /*
211  UDTF: get_max_with_row_offset(Cursor<int>, Constant<1>) -> Column<int>, Column<int>
212 */
213 // clang-format on
215  Column<int>& output_max_col,
216  Column<int>& output_max_row_col) {
217  if ((output_max_col.size() != 1) || output_max_row_col.size() != 1) {
218  return -1;
219  }
220 #ifdef __CUDACC__
221  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
222  int32_t stop = static_cast<int32_t>(input_col.size());
223  int32_t step = blockDim.x * gridDim.x;
224 #else
225  auto start = 0;
226  auto stop = input_col.size();
227  auto step = 1;
228 #endif
229 
230  int curr_max = -2147483648;
231  int curr_max_row = -1;
232  for (auto i = start; i < stop; i += step) {
233  if (input_col[i] > curr_max) {
234  curr_max = input_col[i];
235  curr_max_row = i;
236  }
237  }
238  output_max_col[0] = curr_max;
239  output_max_row_col[0] = curr_max_row;
240  return 1;
241 }
242 
243 // clang-format off
244 /*
245  UDTF: column_list_get__cpu_(ColumnList<double>, int, RowMultiplier) -> Column<double>
246 */
247 // clang-format on
249  const int index,
250  const int m,
251  Column<double>& col) {
252  col = col_list[index]; // copy the data of col_list item to output column
253  return col.size();
254 }
255 
256 // clang-format off
257 /*
258  UDTF: column_list_first_last(ColumnList<double>, RowMultiplier) -> Column<double>,
259  Column<double>
260 */
261 // clang-format on
263  const int m,
264  Column<double>& col1,
265  Column<double>& col2) {
266  col1 = col_list[0];
267  col2 = col_list[col_list.numCols() - 1];
268  return col1.size();
269 }
270 
271 // clang-format off
272 /*
273  UDTF: column_list_row_sum__cpu_(Cursor<ColumnList<int32_t>>) -> Column<int32_t>
274  */
275 // clang-format on
276 
278  Column<int32_t>& out) {
279  int32_t output_num_rows = input.numCols();
280  set_output_row_size(output_num_rows);
281  for (int i = 0; i < output_num_rows; i++) {
282  auto col = input[i];
283  int32_t s = 0;
284  for (int j = 0; j < col.size(); j++) {
285  s += col[j];
286  }
287  out[i] = s;
288  }
289  return output_num_rows;
290 }
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)
DEVICE int64_t size() const
Definition: OmniSciTypes.h:218
EXTENSION_NOINLINE int32_t row_copier_text(const Column< TextEncodingDict > &input_col, int copy_multiplier, Column< TextEncodingDict > &output_col)
DEVICE int64_t numCols() const
Definition: OmniSciTypes.h:269
EXTENSION_NOINLINE int32_t get_max_with_row_offset(const Column< int > &input_col, Column< int > &output_max_col, Column< int > &output_max_row_col)
#define EXTENSION_NOINLINE
Definition: OmniSciTypes.h:28
EXTENSION_NOINLINE int32_t row_copier(const Column< double > &input_col, int copy_multiplier, Column< double > &output_col)
EXTENSION_NOINLINE int32_t column_list_first_last(const ColumnList< double > &col_list, const int m, Column< double > &col1, Column< double > &col2)
EXTENSION_NOINLINE 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_get__cpu_(const ColumnList< double > &col_list, const int index, const int m, Column< double > &col)
EXTENSION_NOINLINE void set_output_row_size(int64_t num_rows)
DEVICE bool isNull(int64_t index) const
Definition: OmniSciTypes.h:220
DEVICE void setNull(int64_t index)
Definition: OmniSciTypes.h:221
EXTENSION_NOINLINE int32_t column_list_row_sum__cpu_(const ColumnList< int32_t > &input, Column< int32_t > &out)