OmniSciDB  21ac014ffc
 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 #include "../../QueryEngine/OmniSciTypes.h"
18 #include "../../Shared/funcannotations.h"
19 
20 // clang-format off
21 /*
22  UDTF: row_copier(Column<double>, RowMultiplier) -> Column<double>
23  UDTF: row_copier_text(Column<TextEncodingDict>, RowMultiplier) -> Column<TextEncodingDict> | input_id=args<0>
24 */
25 // clang-format on
26 EXTENSION_NOINLINE int32_t row_copier(const Column<double>& input_col,
27  int copy_multiplier,
28  Column<double>& output_col) {
29  int32_t output_row_count = copy_multiplier * input_col.size();
30  if (output_row_count > 100) {
31  // Test failure propagation.
32  return -1;
33  }
34  if (output_col.size() != output_row_count) {
35  return -1;
36  }
37 
38 #ifdef __CUDACC__
39  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
40  int32_t stop = static_cast<int32_t>(input_col.size());
41  int32_t step = blockDim.x * gridDim.x;
42 #else
43  auto start = 0;
44  auto stop = input_col.size();
45  auto step = 1;
46 #endif
47 
48  for (auto i = start; i < stop; i += step) {
49  for (int c = 0; c < copy_multiplier; c++) {
50  output_col[i + (c * input_col.size())] = input_col[i];
51  }
52  }
53 
54  return output_row_count;
55 }
56 
58  int copy_multiplier,
59  Column<TextEncodingDict>& output_col) {
60  int32_t output_row_count = copy_multiplier * input_col.size();
61  if (output_row_count > 100) {
62  // Test failure propagation.
63  return -1;
64  }
65  if (output_col.size() != output_row_count) {
66  return -2;
67  }
68 
69 #ifdef __CUDACC__
70  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
71  int32_t stop = static_cast<int32_t>(input_col.size());
72  int32_t step = blockDim.x * gridDim.x;
73 #else
74  auto start = 0;
75  auto stop = input_col.size();
76  auto step = 1;
77 #endif
78 
79  for (auto i = start; i < stop; i += step) {
80  for (int c = 0; c < copy_multiplier; c++) {
81  output_col[i + (c * input_col.size())] = input_col[i];
82  }
83  }
84 
85  return output_row_count;
86 }
87 
88 /*
89  UDTF: row_adder(RowMultiplier<1>, Cursor<ColumnDouble, ColumnDouble>) -> ColumnDouble
90 */
91 EXTENSION_NOINLINE int32_t row_adder(const int copy_multiplier,
92  const Column<double>& input_col1,
93  const Column<double>& input_col2,
94  Column<double>& output_col) {
95  int32_t output_row_count = copy_multiplier * input_col1.size();
96  if (output_row_count > 100) {
97  // Test failure propagation.
98  return -1;
99  }
100  if (output_col.size() != output_row_count) {
101  return -1;
102  }
103 
104 #ifdef __CUDACC__
105  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
106  int32_t stop = static_cast<int32_t>(input_col1.size());
107  int32_t step = blockDim.x * gridDim.x;
108 #else
109  auto start = 0;
110  auto stop = input_col1.size();
111  auto step = 1;
112 #endif
113  auto stride = input_col1.size();
114  for (auto i = start; i < stop; i += step) {
115  for (int c = 0; c < copy_multiplier; c++) {
116  if (input_col1.isNull(i) || input_col2.isNull(i)) {
117  output_col.setNull(i + (c * stride));
118  } else {
119  output_col[i + (c * stride)] = input_col1[i] + input_col2[i];
120  }
121  }
122  }
123 
124  return output_row_count;
125 }
126 
127 // clang-format off
128 /*
129  UDTF: row_addsub(RowMultiplier, Cursor<double, double>) -> Column<double>, Column<double>
130 */
131 // clang-format on
132 EXTENSION_NOINLINE int32_t row_addsub(const int copy_multiplier,
133  const Column<double>& input_col1,
134  const Column<double>& input_col2,
135  Column<double>& output_col1,
136  Column<double>& output_col2) {
137  int32_t output_row_count = copy_multiplier * input_col1.size();
138  if (output_row_count > 100) {
139  // Test failure propagation.
140  return -1;
141  }
142  if ((output_col1.size() != output_row_count) ||
143  (output_col2.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  output_col1[i + (c * stride)] = input_col1[i] + input_col2[i];
160  output_col2[i + (c * stride)] = input_col1[i] - input_col2[i];
161  }
162  }
163  return output_row_count;
164 }
165 
166 // clang-format off
167 /*
168  UDTF: get_max_with_row_offset(Cursor<int>, Constant<1>) -> Column<int>, Column<int>
169 */
170 // clang-format on
172  Column<int>& output_max_col,
173  Column<int>& output_max_row_col) {
174  if ((output_max_col.size() != 1) || output_max_row_col.size() != 1) {
175  return -1;
176  }
177 #ifdef __CUDACC__
178  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
179  int32_t stop = static_cast<int32_t>(input_col.size());
180  int32_t step = blockDim.x * gridDim.x;
181 #else
182  auto start = 0;
183  auto stop = input_col.size();
184  auto step = 1;
185 #endif
186 
187  int curr_max = -2147483648;
188  int curr_max_row = -1;
189  for (auto i = start; i < stop; i += step) {
190  if (input_col[i] > curr_max) {
191  curr_max = input_col[i];
192  curr_max_row = i;
193  }
194  }
195  output_max_col[0] = curr_max;
196  output_max_row_col[0] = curr_max_row;
197  return 1;
198 }
199 
200 #include "TableFunctionsTesting.hpp"
201 
202 // clang-format off
203 /*
204  UDTF: column_list_get__cpu_(ColumnList<double>, int, RowMultiplier) -> Column<double>
205 */
206 // clang-format on
208  const int index,
209  const int m,
210  Column<double>& col) {
211  col = col_list[index]; // copy the data of col_list item to output column
212  return col.size();
213 }
214 
215 // clang-format off
216 /*
217  UDTF: column_list_first_last(ColumnList<double>, RowMultiplier) -> Column<double>,
218  Column<double>
219 */
220 // clang-format on
222  const int m,
223  Column<double>& col1,
224  Column<double>& col2) {
225  col1 = col_list[0];
226  col2 = col_list[col_list.numCols() - 1];
227  return col1.size();
228 }
229 
230 // clang-format off
231 /*
232  UDTF: column_list_row_sum__cpu_(Cursor<ColumnList<int32_t>>) -> Column<int32_t>
233  */
234 // clang-format on
235 
237  Column<int32_t>& out) {
238  int32_t output_num_rows = input.numCols();
239  set_output_row_size(output_num_rows);
240  for (int i = 0; i < output_num_rows; i++) {
241  auto col = input[i];
242  int32_t s = 0;
243  for (int j = 0; j < col.size(); j++) {
244  s += col[j];
245  }
246  out[i] = s;
247  }
248  return output_num_rows;
249 }
250 
251 #include "MLFunctions.hpp"
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:177
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:224
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 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:179
DEVICE void setNull(int64_t index)
Definition: OmniSciTypes.h:180
EXTENSION_NOINLINE int32_t column_list_row_sum__cpu_(const ColumnList< int32_t > &input, Column< int32_t > &out)