OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 /*
21  UDTF: row_copier(Column<double>, RowMultiplier) -> Column<double>
22 */
23 EXTENSION_NOINLINE int32_t row_copier(const Column<double>& input_col,
24  int copy_multiplier,
25  Column<double>& output_col) {
26  int32_t output_row_count = copy_multiplier * input_col.getSize();
27  if (output_row_count > 100) {
28  // Test failure propagation.
29  return -1;
30  }
31  if (output_col.getSize() != output_row_count) {
32  return -1;
33  }
34 
35 #ifdef __CUDACC__
36  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
37  int32_t stop = static_cast<int32_t>(input_col.getSize());
38  int32_t step = blockDim.x * gridDim.x;
39 #else
40  auto start = 0;
41  auto stop = input_col.getSize();
42  auto step = 1;
43 #endif
44 
45  for (auto i = start; i < stop; i += step) {
46  for (int c = 0; c < copy_multiplier; c++) {
47  output_col[i + (c * input_col.getSize())] = input_col[i];
48  }
49  }
50 
51  return output_row_count;
52 }
53 
54 /*
55  UDTF: row_adder(RowMultiplier<1>, Cursor<ColumnDouble, ColumnDouble>) -> ColumnDouble
56 */
57 EXTENSION_NOINLINE int32_t row_adder(const int copy_multiplier,
58  const Column<double>& input_col1,
59  const Column<double>& input_col2,
60  Column<double>& output_col) {
61  int32_t output_row_count = copy_multiplier * input_col1.getSize();
62  if (output_row_count > 100) {
63  // Test failure propagation.
64  return -1;
65  }
66  if (output_col.getSize() != output_row_count) {
67  return -1;
68  }
69 
70 #ifdef __CUDACC__
71  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
72  int32_t stop = static_cast<int32_t>(input_col1.getSize());
73  int32_t step = blockDim.x * gridDim.x;
74 #else
75  auto start = 0;
76  auto stop = input_col1.getSize();
77  auto step = 1;
78 #endif
79  auto stride = input_col1.getSize();
80  for (auto i = start; i < stop; i += step) {
81  for (int c = 0; c < copy_multiplier; c++) {
82  if (input_col1.isNull(i) || input_col2.isNull(i)) {
83  output_col.setNull(i + (c * stride));
84  } else {
85  output_col[i + (c * stride)] = input_col1[i] + input_col2[i];
86  }
87  }
88  }
89 
90  return output_row_count;
91 }
92 
93 // clang-format off
94 /*
95  UDTF: row_addsub(RowMultiplier, Cursor<double, double>) -> Column<double>, Column<double>
96 */
97 // clang-format on
98 EXTENSION_NOINLINE int32_t row_addsub(const int copy_multiplier,
99  const Column<double>& input_col1,
100  const Column<double>& input_col2,
101  Column<double>& output_col1,
102  Column<double>& output_col2) {
103  int32_t output_row_count = copy_multiplier * input_col1.getSize();
104  if (output_row_count > 100) {
105  // Test failure propagation.
106  return -1;
107  }
108  if ((output_col1.getSize() != output_row_count) ||
109  (output_col2.getSize() != output_row_count)) {
110  return -1;
111  }
112 
113 #ifdef __CUDACC__
114  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
115  int32_t stop = static_cast<int32_t>(input_col1.getSize());
116  int32_t step = blockDim.x * gridDim.x;
117 #else
118  auto start = 0;
119  auto stop = input_col1.getSize();
120  auto step = 1;
121 #endif
122  auto stride = input_col1.getSize();
123  for (auto i = start; i < stop; i += step) {
124  for (int c = 0; c < copy_multiplier; c++) {
125  output_col1[i + (c * stride)] = input_col1[i] + input_col2[i];
126  output_col2[i + (c * stride)] = input_col1[i] - input_col2[i];
127  }
128  }
129  return output_row_count;
130 }
131 
132 /*
133  UDTF: get_max_with_row_offset(Cursor<int>) -> Column<int>, Column<int>
134 */
136  Column<int>& output_max_col,
137  Column<int>& output_max_row_col) {
138  if ((output_max_col.getSize() != 1) || output_max_row_col.getSize() != 1) {
139  return -1;
140  }
141 #ifdef __CUDACC__
142  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
143  int32_t stop = static_cast<int32_t>(input_col.getSize());
144  int32_t step = blockDim.x * gridDim.x;
145 #else
146  auto start = 0;
147  auto stop = input_col.getSize();
148  auto step = 1;
149 #endif
150 
151  int curr_max = -2147483648;
152  int curr_max_row = -1;
153  for (auto i = start; i < stop; i += step) {
154  if (input_col[i] > curr_max) {
155  curr_max = input_col[i];
156  curr_max_row = i;
157  }
158  }
159  output_max_col[0] = curr_max;
160  output_max_row_col[0] = curr_max_row;
161  return 1;
162 }
163 
164 #include "TableFunctionsTesting.hpp"
165 
166 /*
167  UDTF: column_list_get__cpu_(ColumnList<double>, int, RowMultiplier) -> Column<double>
168 */
170  const int index,
171  const int m,
172  Column<double>& col) {
173  col = col_list(index); // copy the data of col_list item to output column
174  return col.getSize();
175 }
176 
177 // clang-format off
178 /*
179  UDTF: column_list_first_last(ColumnList<double>, RowMultiplier) -> Column<double>,
180  Column<double>
181 */
182 // clang-format on
184  const int m,
185  Column<double>& col1,
186  Column<double>& col2) {
187  col1 = col_list(0);
188  col2 = col_list(col_list.getLength() - 1);
189  return col1.getSize();
190 }
191 
192 #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)
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:27
EXTENSION_NOINLINE int32_t row_copier(const Column< double > &input_col, int copy_multiplier, Column< double > &output_col)
DEVICE int64_t getLength() const
Definition: OmniSciTypes.h:201
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)
DEVICE int64_t getSize() const
Definition: OmniSciTypes.h:154
DEVICE bool isNull(int64_t index) const
Definition: OmniSciTypes.h:157
DEVICE void setNull(int64_t index)
Definition: OmniSciTypes.h:158