OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OtherTestTableFunctions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 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 "TableFunctionsTesting.h"
18 
19 /*
20  This file contains generic testing compile-time UDTFs. These are
21  table functions that do not belong to other specific classes.
22  Usually these are performance or specific feature tests, such as
23  tests for specific data types or query plan rules.
24  */
25 
26 #ifndef __CUDACC__
27 
28 template <typename T>
30  const T multiplier,
31  Column<T>& out) {
32  const int64_t num_rows = input.size();
33  set_output_row_size(num_rows);
34  for (int64_t r = 0; r < num_rows; ++r) {
35  if (!input.isNull(r)) {
36  out[r] = input[r] * multiplier;
37  } else {
38  out.setNull(r);
39  }
40  }
41  return num_rows;
42 }
43 
44 // explicit instantiations
45 template NEVER_INLINE HOST int32_t
47  const float multiplier,
48  Column<float>& out);
49 template NEVER_INLINE HOST int32_t
51  const double multiplier,
52  Column<double>& out);
53 template NEVER_INLINE HOST int32_t
55  const int32_t multiplier,
56  Column<int32_t>& out);
57 template NEVER_INLINE HOST int32_t
59  const int64_t multiplier,
60  Column<int64_t>& out);
61 
62 #ifndef __CUDACC__
63 
64 #include <algorithm>
65 
66 template <typename T>
67 struct SortAsc {
68  SortAsc(const bool nulls_last)
69  : null_value_(std::numeric_limits<T>::lowest())
70  , null_value_mapped_(map_null_value(nulls_last)) {}
71  static T map_null_value(const bool nulls_last) {
72  return nulls_last ? std::numeric_limits<T>::max() : std::numeric_limits<T>::lowest();
73  }
74  inline T mapValue(const T& val) {
75  return val == null_value_ ? null_value_mapped_ : val;
76  }
77  bool operator()(const T& a, const T& b) { return mapValue(a) < mapValue(b); }
78  const T null_value_;
80 };
81 
82 template <typename T>
83 struct SortDesc {
84  SortDesc(const bool nulls_last)
85  : null_value_(std::numeric_limits<T>::lowest())
86  , null_value_mapped_(map_null_value(nulls_last)) {}
87  static T map_null_value(const bool nulls_last) {
88  return nulls_last ? std::numeric_limits<T>::lowest() : std::numeric_limits<T>::max();
89  }
90 
91  inline T mapValue(const T& val) {
92  return val == null_value_ ? null_value_mapped_ : val;
93  }
94 
95  bool operator()(const T& a, const T& b) { return mapValue(a) > mapValue(b); }
96  const T null_value_;
98 };
99 
100 template <typename T>
102  const int32_t limit,
103  const bool sort_ascending,
104  const bool nulls_last,
105  Column<T>& output) {
106  const int64_t num_rows = input.size();
107  set_output_row_size(num_rows);
108  output = input;
109  if (sort_ascending) {
110  std::sort(output.ptr_, output.ptr_ + num_rows, SortAsc<T>(nulls_last));
111  } else {
112  std::sort(output.ptr_, output.ptr_ + num_rows, SortDesc<T>(nulls_last));
113  }
114  if (limit < 0 || limit > num_rows) {
115  return num_rows;
116  }
117  return limit;
118 }
119 
120 // explicit instantiations
121 template NEVER_INLINE HOST int32_t
123  const int32_t limit,
124  const bool sort_ascending,
125  const bool nulls_last,
126  Column<int8_t>& output);
127 template NEVER_INLINE HOST int32_t
129  const int32_t limit,
130  const bool sort_ascending,
131  const bool nulls_last,
132  Column<int16_t>& output);
133 template NEVER_INLINE HOST int32_t
135  const int32_t limit,
136  const bool sort_ascending,
137  const bool nulls_last,
138  Column<int32_t>& output);
139 template NEVER_INLINE HOST int32_t
141  const int32_t limit,
142  const bool sort_ascending,
143  const bool nulls_last,
144  Column<int64_t>& output);
145 template NEVER_INLINE HOST int32_t
147  const int32_t limit,
148  const bool sort_ascending,
149  const bool nulls_last,
150  Column<float>& output);
151 template NEVER_INLINE HOST int32_t
153  const int32_t limit,
154  const bool sort_ascending,
155  const bool nulls_last,
156  Column<double>& output);
157 
158 #endif
159 
160 template <typename T>
162  if (x >= 0) {
163  if (y > (std::numeric_limits<T>::max() - x)) {
164  throw std::overflow_error("Addition overflow detected");
165  }
166  } else {
167  if (y < (std::numeric_limits<T>::min() - x)) {
168  throw std::underflow_error("Addition underflow detected");
169  }
170  }
171  return x + y;
172 }
173 
174 #ifndef __CUDACC__
175 
176 template <typename T>
177 NEVER_INLINE HOST int32_t
179  int32_t output_num_rows = input.numCols();
180  set_output_row_size(output_num_rows);
181  for (int i = 0; i < output_num_rows; i++) {
182  auto col = input[i];
183  T s = 0;
184  for (int j = 0; j < col.size(); j++) {
185  try {
186  s = safe_addition(s, col[j]);
187  } catch (const std::exception& e) {
188  return TABLE_FUNCTION_ERROR(e.what());
189  } catch (...) {
190  return TABLE_FUNCTION_ERROR("Unknown error");
191  }
192  }
193  out[i] = s;
194  }
195  return output_num_rows;
196 }
197 
198 // explicit instantiations
199 template NEVER_INLINE HOST int32_t
201  Column<int32_t>& out);
202 template NEVER_INLINE HOST int32_t
204  Column<int64_t>& out);
205 template NEVER_INLINE HOST int32_t
207  Column<float>& out);
208 template NEVER_INLINE HOST int32_t
210  Column<double>& out);
211 
212 #endif // #ifndef __CUDACC__
213 
214 EXTENSION_NOINLINE int32_t ct_sleep_worker(int32_t seconds, Column<int32_t>& output) {
215  // save entering time
216  output[0] = std::chrono::duration_cast<std::chrono::milliseconds>(
217  std::chrono::system_clock::now().time_since_epoch())
218  .count() &
219  0xffffff;
220  // store thread id info
221  output[2] = std::hash<std::thread::id>()(std::this_thread::get_id()) & 0xffff;
222  // do "computations" for given seconds
223  std::this_thread::sleep_for(std::chrono::seconds(seconds));
224  // save leaving time
225  output[1] = std::chrono::duration_cast<std::chrono::milliseconds>(
226  std::chrono::system_clock::now().time_since_epoch())
227  .count() &
228  0xffffff;
229  return 3;
230 }
231 
232 EXTENSION_NOINLINE_HOST int32_t ct_sleep1__cpu_(int32_t seconds,
233  int32_t mode,
234  Column<int32_t>& output) {
235  switch (mode) {
236  case 0: {
237  set_output_row_size(3); // uses global singleton of TableFunctionManager
238  break;
239  }
240  case 1: {
242  mgr->set_output_row_size(3);
243  break;
244  }
245  case 2:
246  case 3: {
247  break;
248  }
249  default:
250  return TABLE_FUNCTION_ERROR("unexpected mode");
251  }
252  if (output.size() == 0) {
253  return TABLE_FUNCTION_ERROR("unspecified output columns row size");
254  }
255  return ct_sleep_worker(seconds, output);
256 }
257 
259  int32_t seconds,
260  int32_t mode,
261  Column<int32_t>& output) {
262  switch (mode) {
263  case 0:
264  case 1: {
265  mgr.set_output_row_size(3); // uses thread-safe TableFunctionManager instance
266  break;
267  }
268  case 2: {
269  break;
270  }
271  case 3: {
272  try {
273  auto* mgr0 = TableFunctionManager::get_singleton(); // it may throw "singleton is
274  // not initialized"
275  mgr0->set_output_row_size(3);
276  } catch (std::exception& e) {
277  return mgr.ERROR_MESSAGE(e.what());
278  }
279  break;
280  }
281  default:
282  return mgr.ERROR_MESSAGE("unexpected mode");
283  }
284  if (output.size() == 0) {
285  return mgr.ERROR_MESSAGE("unspecified output columns row size");
286  }
287  return ct_sleep_worker(seconds, output);
288 }
289 
290 template <typename T>
292  const Column<T>& input,
293  Column<T>& output) {
294  int64_t num_rows = input.size();
295  mgr.set_output_row_size(num_rows);
296  for (int64_t r = 0; r < num_rows; ++r) {
297  if (input[r] > 100) {
298  return mgr.ERROR_MESSAGE("Values greater than 100 not allowed");
299  }
300  output[r] = input[r];
301  }
302  return num_rows;
303 }
304 
305 // explicit instantiations
306 template NEVER_INLINE HOST int32_t
308  const Column<float>& input,
309  Column<float>& output);
310 template NEVER_INLINE HOST int32_t
312  const Column<double>& input,
313  Column<double>& output);
314 
316  const Column<int32_t>& input,
317  Column<int32_t>& output) {
318  mgr.set_output_row_size(input.size());
319  for (int32_t i = 0; i < input.size(); i++) {
320  output[i] = input[i] + input.size();
321  }
322  return output.size();
323 }
324 
326  const Column<int32_t>& input1,
327  const Column<int32_t>& input2,
328  int32_t alpha,
329  Column<int32_t>& output1,
330  Column<int32_t>& output2) {
331  auto size = input1.size();
332  mgr.set_output_row_size(size);
333  for (int32_t i = 0; i < size; i++) {
334  output1[i] = input1[i] + size;
335  output2[i] = input2[i] * alpha;
336  }
337  return size;
338 }
339 
340 /*
341  Add two sparse graphs given by pairs of coordinates and the
342  corresponding values and multiply with the size of output
343  columns. Unspecified points are assumed to have the specified fill
344  value.
345 */
346 
348  const Column<int32_t>& x1,
349  const Column<int32_t>& d1,
350  int32_t f1,
351  const Column<int32_t>& x2,
352  const Column<int32_t>& d2,
353  int32_t f2,
354  Column<int32_t>& x,
355  Column<int32_t>& d) {
356  // sorted set of common coordinates:
357  std::set<int32_t, std::less<int32_t>> x12;
358  // inverse map of coordinates and indices, keys are sorted:
359  std::map<int32_t, int32_t, std::less<int32_t>> i1, i2;
360 
361  for (int32_t i = 0; i < x1.size(); i++) {
362  i1[x1[i]] = i;
363  x12.insert(x1[i]);
364  }
365  for (int32_t i = 0; i < x2.size(); i++) {
366  i2[x2[i]] = i;
367  x12.insert(x2[i]);
368  }
369  auto size = x12.size();
370 
371  mgr.set_output_row_size(size);
372  int32_t k = 0;
373  for (auto x_ : x12) {
374  x[k] = x_;
375  auto i1_ = i1.find(x_);
376  auto i2_ = i2.find(x_);
377  if (i1_ != i1.end()) {
378  if (i2_ != i2.end()) {
379  d[k] = d1[i1_->second] + d2[i2_->second];
380  } else {
381  d[k] = d1[i1_->second] + f2;
382  }
383  } else if (i2_ != i2.end()) {
384  d[k] = f1 + d2[i2_->second];
385  } else {
386  d[k] = f1 + f2;
387  }
388  d[k] *= size;
389  k++;
390  }
391  return size;
392 }
393 
394 #endif // #ifndef __CUDACC__
395 
396 #ifdef __CUDACC__
397 
398 EXTENSION_NOINLINE int32_t
399 ct_cuda_enumerate_threads__gpu_(const int32_t output_size,
400  Column<int32_t>& out_local_thread_id,
401  Column<int32_t>& out_block_id,
402  Column<int32_t>& out_global_thread_id) {
403  int32_t local_thread_id = threadIdx.x;
404  int32_t block_id = blockIdx.x;
405  int32_t global_thread_id = threadIdx.x + blockDim.x * blockIdx.x;
406  out_local_thread_id[global_thread_id] = local_thread_id;
407  out_block_id[global_thread_id] = block_id;
408  out_global_thread_id[global_thread_id] = global_thread_id;
409  return output_size;
410 }
411 
412 #endif //__CUDACC__
413 
415  const int32_t i,
416  Column<int32_t>& out) {
417  for (int i = 0; i < input.size(); i++) {
418  if (i % 2 == 0) {
419  out.setNull(i);
420  } else {
421  out[i] = input[i];
422  }
423  }
424  return input.size();
425 }
426 
427 #ifndef __CUDACC__
428 
429 // Test table functions with Timestamp Column inputs
430 // and Timestamp type helper functions
432  const Column<Timestamp>& input,
433  Column<int64_t>& ns,
434  Column<int64_t>& us,
435  Column<int64_t>& ms,
436  Column<int64_t>& s,
437  Column<int64_t>& m,
438  Column<int64_t>& h,
439  Column<int64_t>& d,
440  Column<int64_t>& mo,
441  Column<int64_t>& y) {
442  int size = input.size();
443  mgr.set_output_row_size(size);
444  for (int i = 0; i < size; ++i) {
445  if (input.isNull(i)) {
446  ns.setNull(i);
447  us.setNull(i);
448  ms.setNull(i);
449  s.setNull(i);
450  m.setNull(i);
451  h.setNull(i);
452  d.setNull(i);
453  mo.setNull(i);
454  y.setNull(i);
455  } else {
456  ns[i] = input[i].getNanoseconds();
457  us[i] = input[i].getMicroseconds();
458  ms[i] = input[i].getMilliseconds();
459  s[i] = input[i].getSeconds();
460  m[i] = input[i].getMinutes();
461  h[i] = input[i].getHours();
462  d[i] = input[i].getDay();
463  mo[i] = input[i].getMonth();
464  y[i] = input[i].getYear();
465  }
466  }
467  return size;
468 }
469 
470 // Test table functions with scalar Timestamp inputs
472  const Column<Timestamp>& input,
473  const Timestamp offset,
474  Column<Timestamp>& out) {
475  int size = input.size();
476  mgr.set_output_row_size(size);
477  for (int i = 0; i < size; ++i) {
478  if (input.isNull(i)) {
479  out.setNull(i);
480  } else {
481  out[i] = input[i] + offset;
482  }
483  }
484  return size;
485 }
486 
487 // The following test function ct_timestamp_add_offset_cpu_only is
488 // equivalent interface-wise to ct_timestamp_add_offset but is
489 // different codegen-wise because it will be compiled for both GPU and
490 // CPU execution that is contrary to the ct_timestamp_add_offset
491 // function case that will be compiled for CPU only (this is because
492 // it uses TableFunctionManager argument that will disable compilation
493 // for GPU execution). However, compiling
494 // ct_timestamp_add_offset_cpu_only for GPU execution will fail and
495 // QueryMustRunOnCpu will be thrown which used to lead to a hanging
496 // server due to unclean code cache locking. See QE-374 for more
497 // information.
498 //
499 // A learning point here is that to avoid unnecessary attempts to
500 // compile a UDTF for GPU execution, either its implementation must
501 // use TableFunctionManager argument or the implementation name must
502 // have a suffix containing `__cpu_`.
505  const int32_t multiplier,
506  const Timestamp offset,
507  Column<Timestamp>& out) {
508  out[0] = input[0] + offset;
509  return 1;
510 }
511 
512 // Test table function with sizer argument, and mix of scalar/column inputs.
513 EXTENSION_NOINLINE int32_t
515  const int64_t dummy,
516  const int32_t multiplier,
517  const Column<Timestamp>& input2,
518  Column<Timestamp>& out) {
519  int size = input.size();
520  for (int i = 0; i < size; ++i) {
521  if (input.isNull(i)) {
522  out.setNull(i);
523  } else {
524  out[i] = input[i] + input2[i] + Timestamp(dummy);
525  }
526  }
527  return size;
528 }
529 
530 // Dummy test for ColumnList inputs + Column Timestamp input
533  const ColumnList<int64_t>& input,
534  const Column<Timestamp>& input2,
535  Column<int64_t>& out) {
536  mgr.set_output_row_size(1);
537  out[0] = 1;
538  return 1;
539 }
540 
542  const Column<Timestamp>& input,
544  Column<Timestamp>& mo,
549  Column<Timestamp>& ms,
550  Column<Timestamp>& us) {
551  int size = input.size();
552  mgr.set_output_row_size(size);
553  for (int i = 0; i < size; ++i) {
554  y[i] = input[i].truncateToYear();
555  mo[i] = input[i].truncateToMonth();
556  d[i] = input[i].truncateToDay();
557  h[i] = input[i].truncateToHours();
558  m[i] = input[i].truncateToMinutes();
559  s[i] = input[i].truncateToSeconds();
560  ms[i] = input[i].truncateToMilliseconds();
561  us[i] = input[i].truncateToMicroseconds();
562  }
563 
564  return size;
565 }
566 
567 template <typename T>
568 NEVER_INLINE HOST int32_t
570  const Column<Timestamp>& input,
571  const T inter,
572  Column<Timestamp>& out) {
573  int size = input.size();
574  mgr.set_output_row_size(size);
575  for (int i = 0; i < size; ++i) {
576  out[i] = inter + input[i];
577  }
578  return size;
579 }
580 
581 // explicit instantiations
582 template NEVER_INLINE HOST int32_t
584  const Column<Timestamp>& input,
585  const DayTimeInterval inter,
586  Column<Timestamp>& out);
587 
588 template NEVER_INLINE HOST int32_t
590  const Column<Timestamp>& input,
591  const YearMonthTimeInterval inter,
592  Column<Timestamp>& out);
593 
594 #endif // ifndef __CUDACC__
595 
597  int copy_multiplier,
598  Column<double>& output_col) {
599  int32_t output_row_count = copy_multiplier * input_col.size();
600  if (output_row_count > 100) {
601  // Test failure propagation.
602  return -1;
603  }
604  if (output_col.size() != output_row_count) {
605  return -1;
606  }
607 
608 #ifdef __CUDACC__
609  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
610  int32_t stop = static_cast<int32_t>(input_col.size());
611  int32_t step = blockDim.x * gridDim.x;
612 #else
613  auto start = 0;
614  auto stop = input_col.size();
615  auto step = 1;
616 #endif
617 
618  for (auto i = start; i < stop; i += step) {
619  for (int c = 0; c < copy_multiplier; c++) {
620  output_col[i + (c * input_col.size())] = input_col[i];
621  }
622  }
623 
624  return output_row_count;
625 }
626 
627 #ifndef __CUDACC__
628 
630  int copy_multiplier,
631  Column<double>& output_col,
632  Column<double>& output_col2) {
633  if (copy_multiplier == -1) {
634  // Test UDTF return without allocating output columns, expect
635  // empty output columns.
636  return 0;
637  }
638  if (copy_multiplier == -2) {
639  // Test UDTF return without allocating output columns but
640  // returning positive row size, expect runtime error.
641  return 1;
642  }
643 #ifndef __CUDACC__
644  if (copy_multiplier == -3) {
645  // Test UDTF throw before allocating output columns, expect
646  // runtime error.
647  throw std::runtime_error("row_copier2: throw before calling set_output_row_size");
648  }
649  if (copy_multiplier == -4) {
650  // Test UDTF throw after allocating output columns, expect
651  // runtime error.
653  throw std::runtime_error("row_copier2: throw after calling set_output_row_size");
654  }
655 #endif
656  if (copy_multiplier == -5) {
657  // Test UDTF setting negative row size, expect runtime error.
659  }
660  int32_t output_row_count = copy_multiplier * input_col.size();
661  /* set_output_row_size can be used only when an UDTF is executed on CPU */
662  set_output_row_size(output_row_count);
663  auto result = row_copier(input_col, copy_multiplier, output_col);
664  if (result >= 0) {
665  result = row_copier(input_col, copy_multiplier, output_col2);
666  }
667  return result;
668 }
669 
672  const ColumnList<double>& cols,
673  Column<double>& output_col) {
674  int32_t output_row_count = 0;
675  for (int i = 0; i < cols.numCols(); ++i) {
676  output_row_count += cols[i].size();
677  }
678  mgr.set_output_row_size(output_row_count);
679  int idx = 0;
680  for (int i = 0; i < cols.numCols(); ++i) {
681  for (int j = 0; j < cols[i].size(); ++j) {
682  output_col[idx++] = cols[i][j];
683  }
684  }
685  return output_row_count;
686 }
687 
688 #endif // #ifndef __CUDACC__
689 
691  int copy_multiplier,
692  Column<TextEncodingDict>& output_col) {
693  int32_t output_row_count = copy_multiplier * input_col.size();
694  if (output_row_count > 100) {
695  // Test failure propagation.
696  return -1;
697  }
698  if (output_col.size() != output_row_count) {
699  return -2;
700  }
701 
702 #ifdef __CUDACC__
703  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
704  int32_t stop = static_cast<int32_t>(input_col.size());
705  int32_t step = blockDim.x * gridDim.x;
706 #else
707  auto start = 0;
708  auto stop = input_col.size();
709  auto step = 1;
710 #endif
711 
712  for (auto i = start; i < stop; i += step) {
713  for (int c = 0; c < copy_multiplier; c++) {
714  output_col[i + (c * input_col.size())] = input_col[i];
715  }
716  }
717 
718  return output_row_count;
719 }
720 
721 EXTENSION_NOINLINE int32_t row_adder(const int copy_multiplier,
722  const Column<double>& input_col1,
723  const Column<double>& input_col2,
724  Column<double>& output_col) {
725  int32_t output_row_count = copy_multiplier * input_col1.size();
726  if (output_row_count > 100) {
727  // Test failure propagation.
728  return -1;
729  }
730  if (output_col.size() != output_row_count) {
731  return -1;
732  }
733 
734 #ifdef __CUDACC__
735  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
736  int32_t stop = static_cast<int32_t>(input_col1.size());
737  int32_t step = blockDim.x * gridDim.x;
738 #else
739  auto start = 0;
740  auto stop = input_col1.size();
741  auto step = 1;
742 #endif
743  auto stride = input_col1.size();
744  for (auto i = start; i < stop; i += step) {
745  for (int c = 0; c < copy_multiplier; c++) {
746  if (input_col1.isNull(i) || input_col2.isNull(i)) {
747  output_col.setNull(i + (c * stride));
748  } else {
749  output_col[i + (c * stride)] = input_col1[i] + input_col2[i];
750  }
751  }
752  }
753 
754  return output_row_count;
755 }
756 
757 EXTENSION_NOINLINE int32_t row_addsub(const int copy_multiplier,
758  const Column<double>& input_col1,
759  const Column<double>& input_col2,
760  Column<double>& output_col1,
761  Column<double>& output_col2) {
762  int32_t output_row_count = copy_multiplier * input_col1.size();
763  if (output_row_count > 100) {
764  // Test failure propagation.
765  return -1;
766  }
767  if ((output_col1.size() != output_row_count) ||
768  (output_col2.size() != output_row_count)) {
769  return -1;
770  }
771 
772 #ifdef __CUDACC__
773  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
774  int32_t stop = static_cast<int32_t>(input_col1.size());
775  int32_t step = blockDim.x * gridDim.x;
776 #else
777  auto start = 0;
778  auto stop = input_col1.size();
779  auto step = 1;
780 #endif
781  auto stride = input_col1.size();
782  for (auto i = start; i < stop; i += step) {
783  for (int c = 0; c < copy_multiplier; c++) {
784  output_col1[i + (c * stride)] = input_col1[i] + input_col2[i];
785  output_col2[i + (c * stride)] = input_col1[i] - input_col2[i];
786  }
787  }
788  return output_row_count;
789 }
790 
791 #ifndef __CUDACC__
792 
795  Column<int>& output_max_col,
796  Column<int>& output_max_row_col) {
797  if ((output_max_col.size() != 1) || output_max_row_col.size() != 1) {
798  return -1;
799  }
800  auto start = 0;
801  auto stop = input_col.size();
802  auto step = 1;
803 
804  int curr_max = -2147483648;
805  int curr_max_row = -1;
806  for (auto i = start; i < stop; i += step) {
807  if (input_col[i] > curr_max) {
808  curr_max = input_col[i];
809  curr_max_row = i;
810  }
811  }
812  output_max_col[0] = curr_max;
813  output_max_row_col[0] = curr_max_row;
814  return 1;
815 }
816 
817 #endif // #ifndef __CUDACC__
818 
819 #ifndef __CUDACC__
820 
822  const int index,
823  const int m,
824  Column<double>& col) {
825  col = col_list[index]; // copy the data of col_list item to output column
826  return col.size();
827 }
828 
829 #endif // #ifndef __CUDACC__
830 
832  const int m,
833  Column<double>& col1,
834  Column<double>& col2) {
835  col1 = col_list[0];
836  col2 = col_list[col_list.numCols() - 1];
837  return col1.size();
838 }
839 
840 #ifndef __CUDACC__
841 
844  int32_t output_num_rows = input.numCols();
845  set_output_row_size(output_num_rows);
846  for (int i = 0; i < output_num_rows; i++) {
847  auto col = input[i];
848  int32_t s = 0;
849  for (int j = 0; j < col.size(); j++) {
850  s += col[j];
851  }
852  out[i] = s;
853  }
854  return output_num_rows;
855 }
856 
858  Column<bool>& success) {
859  // set one of each type
860  mgr.set_metadata("test_int8_t", int8_t(1));
861  mgr.set_metadata("test_int16_t", int16_t(2));
862  mgr.set_metadata("test_int32_t", int32_t(3));
863  mgr.set_metadata("test_int64_t", int64_t(4));
864  mgr.set_metadata("test_float", 5.0f);
865  mgr.set_metadata("test_double", 6.0);
866  mgr.set_metadata("test_bool", true);
867 
868  mgr.set_output_row_size(1);
869  success[0] = true;
870  return 1;
871 }
872 
873 NEVER_INLINE HOST int32_t
875  Column<bool>& success) {
876  // set the same name twice
877  mgr.set_metadata("test_int8_t", int8_t(1));
878  mgr.set_metadata("test_int8_t", int8_t(2));
879 
880  mgr.set_output_row_size(1);
881  success[0] = true;
882  return 1;
883 }
884 
885 NEVER_INLINE HOST int32_t
887  Column<bool>& success) {
888  // set the same name twice
889  mgr.set_metadata("test_int8_t", int8_t(1));
890  mgr.set_metadata("test_int8_t", int16_t(2));
891 
892  mgr.set_output_row_size(1);
893  success[0] = true;
894  return 1;
895 }
896 
898  const Column<bool>& input,
899  Column<bool>& success) {
900  // get them all back and check values
901  int8_t i8{};
902  int16_t i16{};
903  int32_t i32{};
904  int64_t i64{};
905  float f{};
906  double d{};
907  bool b{};
908 
909  try {
910  mgr.get_metadata("test_int8_t", i8);
911  mgr.get_metadata("test_int16_t", i16);
912  mgr.get_metadata("test_int32_t", i32);
913  mgr.get_metadata("test_int64_t", i64);
914  mgr.get_metadata("test_float", f);
915  mgr.get_metadata("test_double", d);
916  mgr.get_metadata("test_bool", b);
917  } catch (const std::runtime_error& ex) {
918  return mgr.ERROR_MESSAGE(ex.what());
919  }
920 
921  // return value indicates values were correct
922  // types are implicitly correct by this point, or the above would have thrown
923  bool result = (i8 == 1) && (i16 == 2) && (i32 == 3) && (i64 == 4) && (f == 5.0f) &&
924  (d == 6.0) && b;
925  if (!result) {
926  return mgr.ERROR_MESSAGE("Metadata return values are incorrect");
927  }
928 
929  mgr.set_output_row_size(1);
930  success[0] = true;
931  return 1;
932 }
933 
935  const Column<bool>& input,
936  Column<bool>& success) {
937  // get one back as the wrong type
938  // this should throw
939  float f{};
940  try {
941  mgr.get_metadata("test_double", f);
942  } catch (const std::runtime_error& ex) {
943  return mgr.ERROR_MESSAGE(ex.what());
944  }
945 
946  mgr.set_output_row_size(1);
947  success[0] = true;
948  return 1;
949 }
950 
951 #endif // #ifndef __CUDACC__
952 
954  // output_buffer[0] should always be 0 due to default initialization for GPU
955  return 1;
956 }
957 
959  const int32_t x,
960  const int32_t multiplier,
961  Column<int32_t>& out) {
962  out[0] = 123;
963  return 1;
964 }
965 
967  const Column<int32_t>& input2,
968  const int32_t multiplier,
969  Column<int32_t>& out) {
970  out[0] = 234;
971  return 1;
972 }
973 
974 #ifndef __CUDACC__
975 template <typename T>
977  const Column<T>& input_col,
978  const int reps,
979  Column<T>& output_col) {
980  // Equivalent to row_copier but supports output row sizes larger
981  // than MAX_INT
982  size_t output_row_count = input_col.size() * reps;
983  mgr.set_output_row_size(output_row_count);
984 
985  auto start = 0;
986  auto stop = input_col.size();
987  auto step = 1;
988 
989  for (auto i = start; i < stop; i += step) {
990  for (int c = 0; c < reps; c++) {
991  output_col[i + (stop * c)] = input_col[i];
992  }
993  }
994 
995  return SUCCESS;
996 }
997 
998 template TEMPLATE_NOINLINE int32_t
1000  const Column<int8_t>& input_col,
1001  const int reps,
1002  Column<int8_t>& output_col);
1003 template TEMPLATE_NOINLINE int32_t
1005  const Column<int16_t>& input_col,
1006  const int reps,
1007  Column<int16_t>& output_col);
1008 template TEMPLATE_NOINLINE int32_t
1010  const Column<int32_t>& input_col,
1011  const int reps,
1012  Column<int32_t>& output_col);
1013 template TEMPLATE_NOINLINE int32_t
1015  const Column<int64_t>& input_col,
1016  const int reps,
1017  Column<int64_t>& output_col);
1018 template TEMPLATE_NOINLINE int32_t
1020  const Column<float>& input_col,
1021  const int reps,
1022  Column<float>& output_col);
1023 template TEMPLATE_NOINLINE int32_t
1025  const Column<double>& input_col,
1026  const int reps,
1027  Column<double>& output_col);
1028 template TEMPLATE_NOINLINE int32_t
1030  const Column<bool>& input_col,
1031  const int reps,
1032  Column<bool>& output_col);
1033 
1034 #endif // #ifndef __CUDACC__
EXTENSION_NOINLINE_HOST int32_t ct_copy_and_add_size(TableFunctionManager &mgr, const Column< int32_t > &input, Column< int32_t > &output)
NEVER_INLINE HOST int32_t column_list_safe_row_sum__cpu_template(const ColumnList< T > &input, Column< T > &out)
void set_output_row_size(int64_t num_rows)
Definition: heavydbTypes.h:373
NEVER_INLINE HOST int32_t ct_binding_scalar_multiply__cpu_template(const Column< T > &input, const T multiplier, Column< T > &out)
EXTENSION_NOINLINE_HOST void set_output_row_size(int64_t num_rows)
#define EXTENSION_NOINLINE
Definition: heavydbTypes.h:58
EXTENSION_NOINLINE_HOST int32_t row_copier_columnlist__cpu__(TableFunctionManager &mgr, const ColumnList< double > &cols, Column< double > &output_col)
static T map_null_value(const bool nulls_last)
T mapValue(const T &val)
void get_metadata(const std::string &key, T &value)
Definition: heavydbTypes.h:397
DEVICE int64_t size() const
EXTENSION_NOINLINE int32_t ct_test_nullable(const Column< int32_t > &input, const int32_t i, Column< int32_t > &out)
EXTENSION_NOINLINE int32_t ct_test_func__cpu_1(const Column< int32_t > &input1, const int32_t x, const int32_t multiplier, Column< int32_t > &out)
DEVICE int64_t numCols() const
NEVER_INLINE HOST int32_t tf_metadata_getter_bad__cpu_template(TableFunctionManager &mgr, const Column< bool > &input, Column< bool > &success)
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
EXTENSION_NOINLINE_HOST int32_t ct_add_size_and_mul_alpha(TableFunctionManager &mgr, const Column< int32_t > &input1, const Column< int32_t > &input2, int32_t alpha, Column< int32_t > &output1, Column< int32_t > &output2)
SortAsc(const bool nulls_last)
#define SUCCESS
Definition: heavydbTypes.h:76
SortDesc(const bool nulls_last)
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)
constexpr double a
Definition: Utm.h:32
#define HOST
EXTENSION_NOINLINE int32_t column_list_first_last(const ColumnList< double > &col_list, const int m, Column< double > &col1, Column< double > &col2)
EXTENSION_NOINLINE_HOST int32_t ct_timestamp_add_offset_cpu_only(const Column< Timestamp > &input, const int32_t multiplier, const Timestamp offset, Column< Timestamp > &out)
EXTENSION_NOINLINE int32_t row_copier(const Column< double > &input_col, int copy_multiplier, Column< double > &output_col)
#define EXTENSION_NOINLINE_HOST
Definition: heavydbTypes.h:55
NEVER_INLINE HOST int32_t sort_column_limit__cpu_template(const Column< T > &input, const int32_t limit, const bool sort_ascending, const bool nulls_last, Column< T > &output)
EXTENSION_NOINLINE_HOST int32_t ct_sleep2(TableFunctionManager &mgr, int32_t seconds, int32_t mode, Column< int32_t > &output)
EXTENSION_NOINLINE int32_t row_copier_text(const Column< TextEncodingDict > &input_col, int copy_multiplier, Column< TextEncodingDict > &output_col)
TEMPLATE_NOINLINE int32_t row_repeater__cpu_template(TableFunctionManager &mgr, const Column< T > &input_col, const int reps, Column< T > &output_col)
EXTENSION_NOINLINE_HOST int32_t ct_sleep1__cpu_(int32_t seconds, int32_t mode, Column< int32_t > &output)
T mapValue(const T &val)
NEVER_INLINE HOST int32_t ct_timestamp_add_interval__template(TableFunctionManager &mgr, const Column< Timestamp > &input, const T inter, Column< Timestamp > &out)
EXTENSION_NOINLINE_HOST int32_t column_list_row_sum__cpu_(const ColumnList< int32_t > &input, Column< int32_t > &out)
bool operator()(const T &a, const T &b)
NEVER_INLINE HOST int32_t ct_throw_if_gt_100__cpu_template(TableFunctionManager &mgr, const Column< T > &input, Column< T > &output)
DEVICE bool isNull(int64_t index) const
NEVER_INLINE HOST int32_t tf_metadata_getter__cpu_template(TableFunctionManager &mgr, const Column< bool > &input, Column< bool > &success)
DEVICE void setNull(int64_t index)
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)
const T null_value_mapped_
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
EXTENSION_NOINLINE int32_t ct_sleep_worker(int32_t seconds, Column< int32_t > &output)
EXTENSION_NOINLINE int32_t row_adder(const int copy_multiplier, const Column< double > &input_col1, const Column< double > &input_col2, Column< double > &output_col)
T safe_addition(T x, T y)
EXTENSION_NOINLINE_HOST int32_t ct_timestamp_extract(TableFunctionManager &mgr, const Column< Timestamp > &input, Column< int64_t > &ns, Column< int64_t > &us, Column< int64_t > &ms, Column< int64_t > &s, Column< int64_t > &m, Column< int64_t > &h, Column< int64_t > &d, Column< int64_t > &mo, Column< int64_t > &y)
NEVER_INLINE HOST int32_t tf_metadata_setter__cpu_template(TableFunctionManager &mgr, Column< bool > &success)
#define NEVER_INLINE
static T map_null_value(const bool nulls_last)
void set_metadata(const std::string &key, const T &value)
Definition: heavydbTypes.h:388
bool operator()(const T &a, const T &b)
EXTENSION_NOINLINE int32_t ct_test_func__cpu_2(const Column< int32_t > &input1, const Column< int32_t > &input2, const int32_t multiplier, Column< int32_t > &out)
#define TABLE_FUNCTION_ERROR(MSG)
Definition: heavydbTypes.h:74
NEVER_INLINE HOST int32_t tf_metadata_setter_size_mismatch__cpu_template(TableFunctionManager &mgr, Column< bool > &success)
EXTENSION_NOINLINE int32_t ct_timestamp_test_columns_and_scalars__cpu(const Column< Timestamp > &input, const int64_t dummy, const int32_t multiplier, const Column< Timestamp > &input2, Column< Timestamp > &out)
DEVICE int64_t size() const
DEVICE int64_t size() const
EXTENSION_NOINLINE_HOST int32_t column_list_get__cpu_(const ColumnList< double > &col_list, const int index, const int m, Column< double > &col)
EXTENSION_NOINLINE_HOST int32_t ct_timestamp_add_offset(TableFunctionManager &mgr, const Column< Timestamp > &input, const Timestamp offset, Column< Timestamp > &out)
EXTENSION_NOINLINE_HOST int32_t ct_timestamp_column_list_input(TableFunctionManager &mgr, const ColumnList< int64_t > &input, const Column< Timestamp > &input2, Column< int64_t > &out)
EXTENSION_NOINLINE_HOST int32_t row_copier2__cpu__(const Column< double > &input_col, int copy_multiplier, Column< double > &output_col, Column< double > &output_col2)
NEVER_INLINE HOST int32_t tf_metadata_setter_repeated__cpu_template(TableFunctionManager &mgr, Column< bool > &success)
static TableFunctionManager * get_singleton()
Definition: heavydbTypes.h:357
EXTENSION_NOINLINE_HOST int32_t ct_timestamp_truncate(TableFunctionManager &mgr, const Column< Timestamp > &input, Column< Timestamp > &y, Column< Timestamp > &mo, Column< Timestamp > &d, Column< Timestamp > &h, Column< Timestamp > &m, Column< Timestamp > &s, Column< Timestamp > &ms, Column< Timestamp > &us)
#define TEMPLATE_NOINLINE
Definition: heavydbTypes.h:60
EXTENSION_NOINLINE int32_t ct_gpu_default_init__gpu_(Column< int32_t > &output_buffer)
EXTENSION_NOINLINE_HOST int32_t ct_sparse_add(TableFunctionManager &mgr, const Column< int32_t > &x1, const Column< int32_t > &d1, int32_t f1, const Column< int32_t > &x2, const Column< int32_t > &d2, int32_t f2, Column< int32_t > &x, Column< int32_t > &d)