OmniSciDB  21ac014ffc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResultSetReduction.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 
25 #include "DynamicWatchdog.h"
26 #include "Execute.h"
27 #include "ResultSet.h"
29 #include "ResultSetReductionJIT.h"
30 #include "RuntimeFunctions.h"
31 #include "Shared/SqlTypesLayout.h"
32 #include "Shared/likely.h"
33 #include "Shared/thread_count.h"
34 
35 #include <llvm/ExecutionEngine/GenericValue.h>
36 
37 #include <algorithm>
38 #include <future>
39 #include <numeric>
40 
41 extern bool g_enable_dynamic_watchdog;
42 
43 namespace {
44 
45 bool use_multithreaded_reduction(const size_t entry_count) {
46  return entry_count > 100000;
47 }
48 
50  const auto row_bytes = get_row_bytes(query_mem_desc);
51  CHECK_EQ(size_t(0), row_bytes % 8);
52  return row_bytes / 8;
53 }
54 
55 std::vector<int64_t> make_key(const int64_t* buff,
56  const size_t entry_count,
57  const size_t key_count) {
58  std::vector<int64_t> key;
59  size_t off = 0;
60  for (size_t i = 0; i < key_count; ++i) {
61  key.push_back(buff[off]);
62  off += entry_count;
63  }
64  return key;
65 }
66 
67 void fill_slots(int64_t* dst_entry,
68  const size_t dst_entry_count,
69  const int64_t* src_buff,
70  const size_t src_entry_idx,
71  const size_t src_entry_count,
73  const auto slot_count = query_mem_desc.getBufferColSlotCount();
74  const auto key_count = query_mem_desc.getGroupbyColCount();
75  if (query_mem_desc.didOutputColumnar()) {
76  for (size_t i = 0, dst_slot_off = 0; i < slot_count;
77  ++i, dst_slot_off += dst_entry_count) {
78  dst_entry[dst_slot_off] =
79  src_buff[slot_offset_colwise(src_entry_idx, i, key_count, src_entry_count)];
80  }
81  } else {
82  const auto row_ptr = src_buff + get_row_qw_count(query_mem_desc) * src_entry_idx;
83  const auto slot_off_quad = get_slot_off_quad(query_mem_desc);
84  for (size_t i = 0; i < slot_count; ++i) {
85  dst_entry[i] = row_ptr[slot_off_quad + i];
86  }
87  }
88 }
89 
91 void fill_empty_key_32(int32_t* key_ptr_i32, const size_t key_count) {
92  for (size_t i = 0; i < key_count; ++i) {
93  key_ptr_i32[i] = EMPTY_KEY_32;
94  }
95 }
96 
98 void fill_empty_key_64(int64_t* key_ptr_i64, const size_t key_count) {
99  for (size_t i = 0; i < key_count; ++i) {
100  key_ptr_i64[i] = EMPTY_KEY_64;
101  }
102 }
103 
104 inline int64_t get_component(const int8_t* group_by_buffer,
105  const size_t comp_sz,
106  const size_t index = 0) {
107  int64_t ret = std::numeric_limits<int64_t>::min();
108  switch (comp_sz) {
109  case 1: {
110  ret = group_by_buffer[index];
111  break;
112  }
113  case 2: {
114  const int16_t* buffer_ptr = reinterpret_cast<const int16_t*>(group_by_buffer);
115  ret = buffer_ptr[index];
116  break;
117  }
118  case 4: {
119  const int32_t* buffer_ptr = reinterpret_cast<const int32_t*>(group_by_buffer);
120  ret = buffer_ptr[index];
121  break;
122  }
123  case 8: {
124  const int64_t* buffer_ptr = reinterpret_cast<const int64_t*>(group_by_buffer);
125  ret = buffer_ptr[index];
126  break;
127  }
128  default:
129  CHECK(false);
130  }
131  return ret;
132 }
133 
134 void run_reduction_code(const ReductionCode& reduction_code,
135  int8_t* this_buff,
136  const int8_t* that_buff,
137  const int32_t start_entry_index,
138  const int32_t end_entry_index,
139  const int32_t that_entry_count,
140  const void* this_qmd,
141  const void* that_qmd,
142  const void* serialized_varlen_buffer) {
143  int err = 0;
144  if (reduction_code.func_ptr) {
145  err = reduction_code.func_ptr(this_buff,
146  that_buff,
147  start_entry_index,
148  end_entry_index,
149  that_entry_count,
150  this_qmd,
151  that_qmd,
152  serialized_varlen_buffer);
153  } else {
154  // Calls LLVM methods that are not thread safe, ensure nothing else compiles while we
155  // run this reduction
156  std::lock_guard<std::mutex> compilation_lock(Executor::compilation_mutex_);
157  auto ret = ReductionInterpreter::run(
158  reduction_code.ir_reduce_loop.get(),
161  ReductionInterpreter::MakeEvalValue(start_entry_index),
162  ReductionInterpreter::MakeEvalValue(end_entry_index),
163  ReductionInterpreter::MakeEvalValue(that_entry_count),
166  ReductionInterpreter::MakeEvalValue(serialized_varlen_buffer)});
167  err = ret.int_val;
168  }
169  if (err) {
171  throw std::runtime_error("Multiple distinct values encountered");
172  }
173  if (err == Executor::ERR_INTERRUPTED) {
174  throw std::runtime_error(
175  "Query execution has interrupted during result set reduction");
176  }
177  throw std::runtime_error(
178  "Query execution has exceeded the time limit or was interrupted during result "
179  "set reduction");
180  }
181 }
182 
183 } // namespace
184 
185 void result_set::fill_empty_key(void* key_ptr,
186  const size_t key_count,
187  const size_t key_width) {
188  switch (key_width) {
189  case 4: {
190  auto key_ptr_i32 = reinterpret_cast<int32_t*>(key_ptr);
191  fill_empty_key_32(key_ptr_i32, key_count);
192  break;
193  }
194  case 8: {
195  auto key_ptr_i64 = reinterpret_cast<int64_t*>(key_ptr);
196  fill_empty_key_64(key_ptr_i64, key_count);
197  break;
198  }
199  default:
200  CHECK(false);
201  }
202 }
203 
204 // Driver method for various buffer layouts, actual work is done by reduceOne* methods.
205 // Reduces the entries of `that` into the buffer of this ResultSetStorage object.
207  const std::vector<std::string>& serialized_varlen_buffer,
208  const ReductionCode& reduction_code) const {
209  auto entry_count = query_mem_desc_.getEntryCount();
210  CHECK_GT(entry_count, size_t(0));
218  }
219  const auto that_entry_count = that.query_mem_desc_.getEntryCount();
222  CHECK_GE(entry_count, that_entry_count);
223  break;
224  default:
225  CHECK_EQ(entry_count, that_entry_count);
226  }
227  auto this_buff = buff_;
228  CHECK(this_buff);
229  auto that_buff = that.buff_;
230  CHECK(that_buff);
233  if (!serialized_varlen_buffer.empty()) {
234  throw std::runtime_error(
235  "Projection of variable length targets with baseline hash group by is not yet "
236  "supported in Distributed mode");
237  }
238  if (use_multithreaded_reduction(that_entry_count)) {
239  const size_t thread_count = cpu_threads();
240  std::vector<std::future<void>> reduction_threads;
241  for (size_t thread_idx = 0; thread_idx < thread_count; ++thread_idx) {
242  const auto thread_entry_count =
243  (that_entry_count + thread_count - 1) / thread_count;
244  const auto start_index = thread_idx * thread_entry_count;
245  const auto end_index =
246  std::min(start_index + thread_entry_count, that_entry_count);
247  reduction_threads.emplace_back(std::async(
248  std::launch::async,
249  [this,
250  this_buff,
251  that_buff,
252  start_index,
253  end_index,
254  that_entry_count,
255  &reduction_code,
256  &that] {
257  if (reduction_code.ir_reduce_loop) {
258  run_reduction_code(reduction_code,
259  this_buff,
260  that_buff,
261  start_index,
262  end_index,
263  that_entry_count,
265  &that.query_mem_desc_,
266  nullptr);
267  } else {
268  for (size_t entry_idx = start_index; entry_idx < end_index; ++entry_idx) {
270  this_buff, that_buff, entry_idx, that_entry_count, that);
271  }
272  }
273  }));
274  }
275  for (auto& reduction_thread : reduction_threads) {
276  reduction_thread.wait();
277  }
278  for (auto& reduction_thread : reduction_threads) {
279  reduction_thread.get();
280  }
281  } else {
282  if (reduction_code.ir_reduce_loop) {
283  run_reduction_code(reduction_code,
284  this_buff,
285  that_buff,
286  0,
287  that_entry_count,
288  that_entry_count,
290  &that.query_mem_desc_,
291  nullptr);
292  } else {
293  for (size_t i = 0; i < that_entry_count; ++i) {
294  reduceOneEntryBaseline(this_buff, that_buff, i, that_entry_count, that);
295  }
296  }
297  }
298  return;
299  }
300  if (use_multithreaded_reduction(entry_count)) {
301  const size_t thread_count = cpu_threads();
302  std::vector<std::future<void>> reduction_threads;
303  for (size_t thread_idx = 0; thread_idx < thread_count; ++thread_idx) {
304  const auto thread_entry_count = (entry_count + thread_count - 1) / thread_count;
305  const auto start_index = thread_idx * thread_entry_count;
306  const auto end_index = std::min(start_index + thread_entry_count, entry_count);
307  if (query_mem_desc_.didOutputColumnar()) {
308  reduction_threads.emplace_back(std::async(std::launch::async,
309  [this,
310  this_buff,
311  that_buff,
312  start_index,
313  end_index,
314  &that,
315  &serialized_varlen_buffer] {
316  reduceEntriesNoCollisionsColWise(
317  this_buff,
318  that_buff,
319  that,
320  start_index,
321  end_index,
322  serialized_varlen_buffer);
323  }));
324  } else {
325  reduction_threads.emplace_back(std::async(std::launch::async,
326  [this,
327  this_buff,
328  that_buff,
329  start_index,
330  end_index,
331  that_entry_count,
332  &reduction_code,
333  &that,
334  &serialized_varlen_buffer] {
335  CHECK(reduction_code.ir_reduce_loop);
337  reduction_code,
338  this_buff,
339  that_buff,
340  start_index,
341  end_index,
342  that_entry_count,
343  &query_mem_desc_,
344  &that.query_mem_desc_,
345  &serialized_varlen_buffer);
346  }));
347  }
348  }
349  for (auto& reduction_thread : reduction_threads) {
350  reduction_thread.wait();
351  }
352  for (auto& reduction_thread : reduction_threads) {
353  reduction_thread.get();
354  }
355  } else {
356  if (query_mem_desc_.didOutputColumnar()) {
357  reduceEntriesNoCollisionsColWise(this_buff,
358  that_buff,
359  that,
360  0,
361  query_mem_desc_.getEntryCount(),
362  serialized_varlen_buffer);
363  } else {
364  CHECK(reduction_code.ir_reduce_loop);
365  run_reduction_code(reduction_code,
366  this_buff,
367  that_buff,
368  0,
369  entry_count,
370  that_entry_count,
371  &query_mem_desc_,
372  &that.query_mem_desc_,
373  &serialized_varlen_buffer);
374  }
375  }
376 }
377 
378 namespace {
379 
381  if (UNLIKELY(dynamic_watchdog())) {
382  // TODO(alex): distinguish between the deadline and interrupt
383  throw std::runtime_error(
384  "Query execution has exceeded the time limit or was interrupted during result "
385  "set reduction");
386  }
387 }
388 
389 ALWAYS_INLINE void check_watchdog_with_seed(const size_t sample_seed) {
390  if (UNLIKELY((sample_seed & 0x3F) == 0 && dynamic_watchdog())) {
391  // TODO(alex): distinguish between the deadline and interrupt
392  throw std::runtime_error(
393  "Query execution has exceeded the time limit or was interrupted during result "
394  "set reduction");
395  }
396 }
397 
398 } // namespace
399 
401  int8_t* this_buff,
402  const int8_t* that_buff,
403  const ResultSetStorage& that,
404  const size_t start_index,
405  const size_t end_index,
406  const std::vector<std::string>& serialized_varlen_buffer) const {
407  // TODO(adb / saman): Support column wise output when serializing distributed agg
408  // functions
409  CHECK(serialized_varlen_buffer.empty());
410 
411  const auto& col_slot_context = query_mem_desc_.getColSlotContext();
412 
413  auto this_crt_col_ptr = get_cols_ptr(this_buff, query_mem_desc_);
414  auto that_crt_col_ptr = get_cols_ptr(that_buff, query_mem_desc_);
415  for (size_t target_idx = 0; target_idx < targets_.size(); ++target_idx) {
416  const auto& agg_info = targets_[target_idx];
417  const auto& slots_for_col = col_slot_context.getSlotsForCol(target_idx);
418 
419  bool two_slot_target{false};
420  if (agg_info.is_agg &&
421  (agg_info.agg_kind == kAVG ||
422  (agg_info.agg_kind == kSAMPLE && agg_info.sql_type.is_varlen()))) {
423  // Note that this assumes if one of the slot pairs in a given target is an array,
424  // all slot pairs are arrays. Currently this is true for all geo targets, but we
425  // should better codify and store this information in the future
426  two_slot_target = true;
427  }
429  throw std::runtime_error(
430  "Query execution was interrupted during result set reduction");
431  }
433  check_watchdog();
434  }
435  for (size_t target_slot_idx = slots_for_col.front();
436  target_slot_idx < slots_for_col.back() + 1;
437  target_slot_idx += 2) {
438  const auto this_next_col_ptr = advance_to_next_columnar_target_buff(
439  this_crt_col_ptr, query_mem_desc_, target_slot_idx);
440  const auto that_next_col_ptr = advance_to_next_columnar_target_buff(
441  that_crt_col_ptr, query_mem_desc_, target_slot_idx);
442  for (size_t entry_idx = start_index; entry_idx < end_index; ++entry_idx) {
443  if (isEmptyEntryColumnar(entry_idx, that_buff)) {
444  continue;
445  }
447  // copy the key from right hand side
448  copyKeyColWise(entry_idx, this_buff, that_buff);
449  }
450  auto this_ptr1 =
451  this_crt_col_ptr +
452  entry_idx * query_mem_desc_.getPaddedSlotWidthBytes(target_slot_idx);
453  auto that_ptr1 =
454  that_crt_col_ptr +
455  entry_idx * query_mem_desc_.getPaddedSlotWidthBytes(target_slot_idx);
456  int8_t* this_ptr2{nullptr};
457  const int8_t* that_ptr2{nullptr};
458  if (UNLIKELY(two_slot_target)) {
459  this_ptr2 =
460  this_next_col_ptr +
461  entry_idx * query_mem_desc_.getPaddedSlotWidthBytes(target_slot_idx + 1);
462  that_ptr2 =
463  that_next_col_ptr +
464  entry_idx * query_mem_desc_.getPaddedSlotWidthBytes(target_slot_idx + 1);
465  }
466  reduceOneSlot(this_ptr1,
467  this_ptr2,
468  that_ptr1,
469  that_ptr2,
470  agg_info,
471  target_idx,
472  target_slot_idx,
473  target_slot_idx,
474  that,
475  slots_for_col.front(),
476  serialized_varlen_buffer);
477  }
478 
479  this_crt_col_ptr = this_next_col_ptr;
480  that_crt_col_ptr = that_next_col_ptr;
481  if (UNLIKELY(two_slot_target)) {
482  this_crt_col_ptr = advance_to_next_columnar_target_buff(
483  this_crt_col_ptr, query_mem_desc_, target_slot_idx + 1);
484  that_crt_col_ptr = advance_to_next_columnar_target_buff(
485  that_crt_col_ptr, query_mem_desc_, target_slot_idx + 1);
486  }
487  }
488  }
489 }
490 
491 /*
492  * copy all keys from the columnar prepended group buffer of "that_buff" into
493  * "this_buff"
494  */
495 void ResultSetStorage::copyKeyColWise(const size_t entry_idx,
496  int8_t* this_buff,
497  const int8_t* that_buff) const {
499  for (size_t group_idx = 0; group_idx < query_mem_desc_.getGroupbyColCount();
500  group_idx++) {
501  // if the column corresponds to a group key
502  const auto column_offset_bytes =
504  auto lhs_key_ptr = this_buff + column_offset_bytes;
505  auto rhs_key_ptr = that_buff + column_offset_bytes;
506  switch (query_mem_desc_.groupColWidth(group_idx)) {
507  case 8:
508  *(reinterpret_cast<int64_t*>(lhs_key_ptr) + entry_idx) =
509  *(reinterpret_cast<const int64_t*>(rhs_key_ptr) + entry_idx);
510  break;
511  case 4:
512  *(reinterpret_cast<int32_t*>(lhs_key_ptr) + entry_idx) =
513  *(reinterpret_cast<const int32_t*>(rhs_key_ptr) + entry_idx);
514  break;
515  case 2:
516  *(reinterpret_cast<int16_t*>(lhs_key_ptr) + entry_idx) =
517  *(reinterpret_cast<const int16_t*>(rhs_key_ptr) + entry_idx);
518  break;
519  case 1:
520  *(reinterpret_cast<int8_t*>(lhs_key_ptr) + entry_idx) =
521  *(reinterpret_cast<const int8_t*>(rhs_key_ptr) + entry_idx);
522  break;
523  default:
524  CHECK(false);
525  break;
526  }
527  }
528 }
529 
530 // Rewrites the entries of this ResultSetStorage object to point directly into the
531 // serialized_varlen_buffer rather than using offsets.
533  const std::vector<std::string>& serialized_varlen_buffer) const {
534  if (serialized_varlen_buffer.empty()) {
535  return;
536  }
537 
539  auto entry_count = query_mem_desc_.getEntryCount();
540  CHECK_GT(entry_count, size_t(0));
541  CHECK(buff_);
542 
543  // Row-wise iteration, consider moving to separate function
544  for (size_t i = 0; i < entry_count; ++i) {
545  if (isEmptyEntry(i, buff_)) {
546  continue;
547  }
548  const auto key_bytes = get_key_bytes_rowwise(query_mem_desc_);
549  const auto key_bytes_with_padding = align_to_int64(key_bytes);
550  auto rowwise_targets_ptr =
551  row_ptr_rowwise(buff_, query_mem_desc_, i) + key_bytes_with_padding;
552  size_t target_slot_idx = 0;
553  for (size_t target_logical_idx = 0; target_logical_idx < targets_.size();
554  ++target_logical_idx) {
555  const auto& target_info = targets_[target_logical_idx];
556  if (target_info.sql_type.is_varlen() && target_info.is_agg) {
557  CHECK(target_info.agg_kind == kSAMPLE);
558  auto ptr1 = rowwise_targets_ptr;
559  auto slot_idx = target_slot_idx;
560  auto ptr2 = ptr1 + query_mem_desc_.getPaddedSlotWidthBytes(slot_idx);
561  auto offset = *reinterpret_cast<const int64_t*>(ptr1);
562 
563  const auto& elem_ti = target_info.sql_type.get_elem_type();
564  size_t length_to_elems =
565  target_info.sql_type.is_string() || target_info.sql_type.is_geometry()
566  ? 1
567  : elem_ti.get_size();
568  if (target_info.sql_type.is_geometry()) {
569  for (int j = 0; j < target_info.sql_type.get_physical_coord_cols(); j++) {
570  if (j > 0) {
571  ptr1 = ptr2 + query_mem_desc_.getPaddedSlotWidthBytes(slot_idx + 1);
572  ptr2 = ptr1 + query_mem_desc_.getPaddedSlotWidthBytes(slot_idx + 2);
573  slot_idx += 2;
574  length_to_elems = 4;
575  }
576  CHECK_LT(static_cast<size_t>(offset), serialized_varlen_buffer.size());
577  const auto& varlen_bytes_str = serialized_varlen_buffer[offset++];
578  const auto str_ptr =
579  reinterpret_cast<const int8_t*>(varlen_bytes_str.c_str());
580  CHECK(ptr1);
581  *reinterpret_cast<int64_t*>(ptr1) = reinterpret_cast<const int64_t>(str_ptr);
582  CHECK(ptr2);
583  *reinterpret_cast<int64_t*>(ptr2) =
584  static_cast<int64_t>(varlen_bytes_str.size() / length_to_elems);
585  }
586  } else {
587  CHECK_LT(static_cast<size_t>(offset), serialized_varlen_buffer.size());
588  const auto& varlen_bytes_str = serialized_varlen_buffer[offset];
589  const auto str_ptr = reinterpret_cast<const int8_t*>(varlen_bytes_str.c_str());
590  CHECK(ptr1);
591  *reinterpret_cast<int64_t*>(ptr1) = reinterpret_cast<const int64_t>(str_ptr);
592  CHECK(ptr2);
593  *reinterpret_cast<int64_t*>(ptr2) =
594  static_cast<int64_t>(varlen_bytes_str.size() / length_to_elems);
595  }
596  }
597 
598  rowwise_targets_ptr = advance_target_ptr_row_wise(
599  rowwise_targets_ptr, target_info, target_slot_idx, query_mem_desc_, false);
600  target_slot_idx = advance_slot(target_slot_idx, target_info, false);
601  }
602  }
603 
604  return;
605 }
606 
607 namespace {
608 
609 #ifdef _MSC_VER
610 #define mapd_cas(address, compare, val) \
611  InterlockedCompareExchange(reinterpret_cast<volatile long*>(address), \
612  static_cast<long>(val), \
613  static_cast<long>(compare))
614 #else
615 #define mapd_cas(address, compare, val) __sync_val_compare_and_swap(address, compare, val)
616 #endif
617 
619  const uint32_t h,
620  const int64_t* key,
621  const uint32_t key_qw_count,
622  const size_t entry_count) {
623  auto off = h;
624  const auto old_key = mapd_cas(&groups_buffer[off], EMPTY_KEY_64, *key);
625  if (old_key == EMPTY_KEY_64) {
626  for (size_t i = 0; i < key_qw_count; ++i) {
627  groups_buffer[off] = key[i];
628  off += entry_count;
629  }
630  return {&groups_buffer[off], true};
631  }
632  off = h;
633  for (size_t i = 0; i < key_qw_count; ++i) {
634  if (groups_buffer[off] != key[i]) {
635  return {nullptr, true};
636  }
637  off += entry_count;
638  }
639  return {&groups_buffer[off], false};
640 }
641 
642 #undef mapd_cas
643 
644 // TODO(alex): fix synchronization when we enable it
646  int64_t* groups_buffer,
647  const uint32_t groups_buffer_entry_count,
648  const int64_t* key,
649  const uint32_t key_qw_count) {
650  uint32_t h = key_hash(key, key_qw_count, sizeof(int64_t)) % groups_buffer_entry_count;
652  groups_buffer, h, key, key_qw_count, groups_buffer_entry_count);
653  if (matching_gvi.first) {
654  return matching_gvi;
655  }
656  uint32_t h_probe = (h + 1) % groups_buffer_entry_count;
657  while (h_probe != h) {
659  groups_buffer, h_probe, key, key_qw_count, groups_buffer_entry_count);
660  if (matching_gvi.first) {
661  return matching_gvi;
662  }
663  h_probe = (h_probe + 1) % groups_buffer_entry_count;
664  }
665  return {nullptr, true};
666 }
667 
668 #ifdef _MSC_VER
669 #define cas_cst(ptr, expected, desired) \
670  (InterlockedCompareExchangePointer(reinterpret_cast<void* volatile*>(ptr), \
671  reinterpret_cast<void*>(&desired), \
672  expected) == expected)
673 #define store_cst(ptr, val) \
674  InterlockedExchangePointer(reinterpret_cast<void* volatile*>(ptr), \
675  reinterpret_cast<void*>(val))
676 #define load_cst(ptr) \
677  InterlockedCompareExchange(reinterpret_cast<volatile long*>(ptr), 0, 0)
678 #else
679 #define cas_cst(ptr, expected, desired) \
680  __atomic_compare_exchange_n( \
681  ptr, expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
682 #define store_cst(ptr, val) __atomic_store_n(ptr, val, __ATOMIC_SEQ_CST)
683 #define load_cst(ptr) __atomic_load_n(ptr, __ATOMIC_SEQ_CST)
684 #endif
685 
686 template <typename T = int64_t>
688  int64_t* groups_buffer,
689  const uint32_t h,
690  const T* key,
691  const uint32_t key_count,
693  const int64_t* that_buff_i64,
694  const size_t that_entry_idx,
695  const size_t that_entry_count,
696  const uint32_t row_size_quad) {
697  auto off = h * row_size_quad;
698  T empty_key = get_empty_key<T>();
699  T write_pending = get_empty_key<T>() - 1;
700  auto row_ptr = reinterpret_cast<T*>(groups_buffer + off);
701  const auto slot_off_quad = get_slot_off_quad(query_mem_desc);
702  const bool success = cas_cst(row_ptr, &empty_key, write_pending);
703  if (success) {
704  fill_slots(groups_buffer + off + slot_off_quad,
705  query_mem_desc.getEntryCount(),
706  that_buff_i64,
707  that_entry_idx,
708  that_entry_count,
710  if (key_count > 1) {
711  memcpy(row_ptr + 1, key + 1, (key_count - 1) * sizeof(T));
712  }
713  store_cst(row_ptr, *key);
714  return {groups_buffer + off + slot_off_quad, true};
715  }
716  while (load_cst(row_ptr) == write_pending) {
717  // spin until the winning thread has finished writing the entire key and the init
718  // value
719  }
720  for (size_t i = 0; i < key_count; ++i) {
721  if (load_cst(row_ptr + i) != key[i]) {
722  return {nullptr, true};
723  }
724  }
725  return {groups_buffer + off + slot_off_quad, false};
726 }
727 
728 #undef load_cst
729 #undef store_cst
730 #undef cas_cst
731 
733  int64_t* groups_buffer,
734  const uint32_t h,
735  const int64_t* key,
736  const uint32_t key_count,
737  const size_t key_width,
739  const int64_t* that_buff_i64,
740  const size_t that_entry_idx,
741  const size_t that_entry_count,
742  const uint32_t row_size_quad) {
743  switch (key_width) {
744  case 4:
745  return get_matching_group_value_reduction(groups_buffer,
746  h,
747  reinterpret_cast<const int32_t*>(key),
748  key_count,
749  query_mem_desc,
750  that_buff_i64,
751  that_entry_idx,
752  that_entry_count,
753  row_size_quad);
754  case 8:
755  return get_matching_group_value_reduction(groups_buffer,
756  h,
757  key,
758  key_count,
759  query_mem_desc,
760  that_buff_i64,
761  that_entry_idx,
762  that_entry_count,
763  row_size_quad);
764  default:
765  CHECK(false);
766  return {nullptr, true};
767  }
768 }
769 
770 } // namespace
771 
773  int64_t* groups_buffer,
774  const uint32_t groups_buffer_entry_count,
775  const int64_t* key,
776  const uint32_t key_count,
777  const size_t key_width,
779  const int64_t* that_buff_i64,
780  const size_t that_entry_idx,
781  const size_t that_entry_count,
782  const uint32_t row_size_quad) {
783  uint32_t h = key_hash(key, key_count, key_width) % groups_buffer_entry_count;
784  auto matching_gvi = get_matching_group_value_reduction(groups_buffer,
785  h,
786  key,
787  key_count,
788  key_width,
789  query_mem_desc,
790  that_buff_i64,
791  that_entry_idx,
792  that_entry_count,
793  row_size_quad);
794  if (matching_gvi.first) {
795  return matching_gvi;
796  }
797  uint32_t h_probe = (h + 1) % groups_buffer_entry_count;
798  while (h_probe != h) {
799  matching_gvi = get_matching_group_value_reduction(groups_buffer,
800  h_probe,
801  key,
802  key_count,
803  key_width,
804  query_mem_desc,
805  that_buff_i64,
806  that_entry_idx,
807  that_entry_count,
808  row_size_quad);
809  if (matching_gvi.first) {
810  return matching_gvi;
811  }
812  h_probe = (h_probe + 1) % groups_buffer_entry_count;
813  }
814  return {nullptr, true};
815 }
816 
817 // Reduces entry at position that_entry_idx in that_buff into this_buff. This is
818 // the baseline layout, so the position in this_buff isn't known to be that_entry_idx.
820  const int8_t* that_buff,
821  const size_t that_entry_idx,
822  const size_t that_entry_count,
823  const ResultSetStorage& that) const {
825  check_watchdog_with_seed(that_entry_idx);
826  }
827  const auto key_count = query_mem_desc_.getGroupbyColCount();
832  const auto key_off =
834  if (isEmptyEntry(that_entry_idx, that_buff)) {
835  return;
836  }
837  auto this_buff_i64 = reinterpret_cast<int64_t*>(this_buff);
838  auto that_buff_i64 = reinterpret_cast<const int64_t*>(that_buff);
839  const auto key = make_key(&that_buff_i64[key_off], that_entry_count, key_count);
840  auto [this_entry_slots, empty_entry] = get_group_value_columnar_reduction(
841  this_buff_i64, query_mem_desc_.getEntryCount(), &key[0], key_count);
842  CHECK(this_entry_slots);
843  if (empty_entry) {
844  fill_slots(this_entry_slots,
846  that_buff_i64,
847  that_entry_idx,
848  that_entry_count,
850  return;
851  }
853  this_entry_slots, that_buff_i64, that_entry_idx, that_entry_count, that);
854 }
855 
856 void ResultSetStorage::reduceOneEntrySlotsBaseline(int64_t* this_entry_slots,
857  const int64_t* that_buff,
858  const size_t that_entry_idx,
859  const size_t that_entry_count,
860  const ResultSetStorage& that) const {
862  const auto key_count = query_mem_desc_.getGroupbyColCount();
863  size_t j = 0;
864  size_t init_agg_val_idx = 0;
865  for (size_t target_logical_idx = 0; target_logical_idx < targets_.size();
866  ++target_logical_idx) {
867  const auto& target_info = targets_[target_logical_idx];
868  const auto that_slot_off = slot_offset_colwise(
869  that_entry_idx, init_agg_val_idx, key_count, that_entry_count);
870  const auto this_slot_off = init_agg_val_idx * query_mem_desc_.getEntryCount();
871  reduceOneSlotBaseline(this_entry_slots,
872  this_slot_off,
873  that_buff,
874  that_entry_count,
875  that_slot_off,
876  target_info,
877  target_logical_idx,
878  j,
879  init_agg_val_idx,
880  that);
882  init_agg_val_idx = advance_slot(init_agg_val_idx, target_info, false);
883  } else {
884  if (query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) < 0) {
885  init_agg_val_idx = advance_slot(init_agg_val_idx, target_info, false);
886  }
887  }
888  j = advance_slot(j, target_info, false);
889  }
890 }
891 
893  const size_t this_slot,
894  const int64_t* that_buff,
895  const size_t that_entry_count,
896  const size_t that_slot,
897  const TargetInfo& target_info,
898  const size_t target_logical_idx,
899  const size_t target_slot_idx,
900  const size_t init_agg_val_idx,
901  const ResultSetStorage& that) const {
903  int8_t* this_ptr2{nullptr};
904  const int8_t* that_ptr2{nullptr};
905  if (target_info.is_agg &&
906  (target_info.agg_kind == kAVG ||
907  (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_varlen()))) {
908  const auto this_count_off = query_mem_desc_.getEntryCount();
909  const auto that_count_off = that_entry_count;
910  this_ptr2 = reinterpret_cast<int8_t*>(&this_buff[this_slot + this_count_off]);
911  that_ptr2 = reinterpret_cast<const int8_t*>(&that_buff[that_slot + that_count_off]);
912  }
913  reduceOneSlot(reinterpret_cast<int8_t*>(&this_buff[this_slot]),
914  this_ptr2,
915  reinterpret_cast<const int8_t*>(&that_buff[that_slot]),
916  that_ptr2,
917  target_info,
918  target_logical_idx,
919  target_slot_idx,
920  init_agg_val_idx,
921  that,
922  target_slot_idx, // dummy, for now
923  {});
924 }
925 
926 // During the reduction of two result sets using the baseline strategy, we first create a
927 // big enough buffer to hold the entries for both and we move the entries from the first
928 // into it before doing the reduction as usual (into the first buffer).
929 template <class KeyType>
931  const size_t new_entry_count) const {
933  CHECK_GT(new_entry_count, query_mem_desc_.getEntryCount());
934  auto new_buff_i64 = reinterpret_cast<int64_t*>(new_buff);
935  const auto key_count = query_mem_desc_.getGroupbyColCount();
938  const auto src_buff = reinterpret_cast<const int64_t*>(buff_);
939  const auto row_qw_count = get_row_qw_count(query_mem_desc_);
940  const auto key_byte_width = query_mem_desc_.getEffectiveKeyWidth();
941 
943  const size_t thread_count = cpu_threads();
944  std::vector<std::future<void>> move_threads;
945 
946  for (size_t thread_idx = 0; thread_idx < thread_count; ++thread_idx) {
947  const auto thread_entry_count =
948  (query_mem_desc_.getEntryCount() + thread_count - 1) / thread_count;
949  const auto start_index = thread_idx * thread_entry_count;
950  const auto end_index =
951  std::min(start_index + thread_entry_count, query_mem_desc_.getEntryCount());
952  move_threads.emplace_back(std::async(
953  std::launch::async,
954  [this,
955  src_buff,
956  new_buff_i64,
957  new_entry_count,
958  start_index,
959  end_index,
960  key_count,
961  row_qw_count,
962  key_byte_width] {
963  for (size_t entry_idx = start_index; entry_idx < end_index; ++entry_idx) {
964  moveOneEntryToBuffer<KeyType>(entry_idx,
965  new_buff_i64,
966  new_entry_count,
967  key_count,
968  row_qw_count,
969  src_buff,
970  key_byte_width);
971  }
972  }));
973  }
974  for (auto& move_thread : move_threads) {
975  move_thread.wait();
976  }
977  for (auto& move_thread : move_threads) {
978  move_thread.get();
979  }
980  } else {
981  for (size_t entry_idx = 0; entry_idx < query_mem_desc_.getEntryCount(); ++entry_idx) {
982  moveOneEntryToBuffer<KeyType>(entry_idx,
983  new_buff_i64,
984  new_entry_count,
985  key_count,
986  row_qw_count,
987  src_buff,
988  key_byte_width);
989  }
990  }
991 }
992 
993 template <class KeyType>
994 void ResultSetStorage::moveOneEntryToBuffer(const size_t entry_index,
995  int64_t* new_buff_i64,
996  const size_t new_entry_count,
997  const size_t key_count,
998  const size_t row_qw_count,
999  const int64_t* src_buff,
1000  const size_t key_byte_width) const {
1001  const auto key_off =
1003  ? key_offset_colwise(entry_index, 0, query_mem_desc_.getEntryCount())
1004  : row_qw_count * entry_index;
1005  const auto key_ptr = reinterpret_cast<const KeyType*>(&src_buff[key_off]);
1006  if (*key_ptr == get_empty_key<KeyType>()) {
1007  return;
1008  }
1009  int64_t* new_entries_ptr{nullptr};
1011  const auto key =
1012  make_key(&src_buff[key_off], query_mem_desc_.getEntryCount(), key_count);
1013  new_entries_ptr =
1014  get_group_value_columnar(new_buff_i64, new_entry_count, &key[0], key_count);
1015  } else {
1016  new_entries_ptr = get_group_value(new_buff_i64,
1017  new_entry_count,
1018  &src_buff[key_off],
1019  key_count,
1020  key_byte_width,
1021  row_qw_count);
1022  }
1023  CHECK(new_entries_ptr);
1024  fill_slots(new_entries_ptr,
1025  new_entry_count,
1026  src_buff,
1027  entry_index,
1029  query_mem_desc_);
1030 }
1031 
1033  if (query_mem_desc_.didOutputColumnar()) {
1034  storage_->initializeColWise();
1035  } else {
1036  storage_->initializeRowWise();
1037  }
1038 }
1039 
1040 // Driver for reductions. Needed because the result of a reduction on the baseline
1041 // layout, which can have collisions, cannot be done in place and something needs
1042 // to take the ownership of the new result set with the bigger underlying buffer.
1043 ResultSet* ResultSetManager::reduce(std::vector<ResultSet*>& result_sets) {
1044  CHECK(!result_sets.empty());
1045  auto result_rs = result_sets.front();
1046  CHECK(result_rs->storage_);
1047  auto& first_result = *result_rs->storage_;
1048  auto result = &first_result;
1049  const auto row_set_mem_owner = result_rs->row_set_mem_owner_;
1050  for (const auto result_set : result_sets) {
1051  CHECK_EQ(row_set_mem_owner, result_set->row_set_mem_owner_);
1052  }
1053  const auto catalog = result_rs->catalog_;
1054  for (const auto result_set : result_sets) {
1055  CHECK_EQ(catalog, result_set->catalog_);
1056  }
1057  if (first_result.query_mem_desc_.getQueryDescriptionType() ==
1059  const auto total_entry_count =
1060  std::accumulate(result_sets.begin(),
1061  result_sets.end(),
1062  size_t(0),
1063  [](const size_t init, const ResultSet* rs) {
1064  return init + rs->query_mem_desc_.getEntryCount();
1065  });
1066  CHECK(total_entry_count);
1067  auto query_mem_desc = first_result.query_mem_desc_;
1068  query_mem_desc.setEntryCount(total_entry_count);
1069  rs_.reset(new ResultSet(first_result.targets_,
1072  row_set_mem_owner,
1073  catalog,
1074  0,
1075  0));
1076  auto result_storage = rs_->allocateStorage(first_result.target_init_vals_);
1077  rs_->initializeStorage();
1078  switch (query_mem_desc.getEffectiveKeyWidth()) {
1079  case 4:
1080  first_result.moveEntriesToBuffer<int32_t>(result_storage->getUnderlyingBuffer(),
1081  query_mem_desc.getEntryCount());
1082  break;
1083  case 8:
1084  first_result.moveEntriesToBuffer<int64_t>(result_storage->getUnderlyingBuffer(),
1085  query_mem_desc.getEntryCount());
1086  break;
1087  default:
1088  CHECK(false);
1089  }
1090  result = rs_->storage_.get();
1091  result_rs = rs_.get();
1092  }
1093 
1094  auto& serialized_varlen_buffer = result_sets.front()->serialized_varlen_buffer_;
1095  if (!serialized_varlen_buffer.empty()) {
1096  result->rewriteAggregateBufferOffsets(serialized_varlen_buffer.front());
1097  for (auto result_it = result_sets.begin() + 1; result_it != result_sets.end();
1098  ++result_it) {
1099  auto& result_serialized_varlen_buffer = (*result_it)->serialized_varlen_buffer_;
1100  CHECK_EQ(result_serialized_varlen_buffer.size(), size_t(1));
1101  serialized_varlen_buffer.emplace_back(
1102  std::move(result_serialized_varlen_buffer.front()));
1103  }
1104  }
1105 
1106  ResultSetReductionJIT reduction_jit(result_rs->getQueryMemDesc(),
1107  result_rs->getTargetInfos(),
1108  result_rs->getTargetInitVals());
1109  auto reduction_code = reduction_jit.codegen();
1110  size_t ctr = 1;
1111  for (auto result_it = result_sets.begin() + 1; result_it != result_sets.end();
1112  ++result_it) {
1113  if (!serialized_varlen_buffer.empty()) {
1114  result->reduce(
1115  *((*result_it)->storage_), serialized_varlen_buffer[ctr++], reduction_code);
1116  } else {
1117  result->reduce(*((*result_it)->storage_), {}, reduction_code);
1118  }
1119  }
1120  return result_rs;
1121 }
1122 
1123 std::shared_ptr<ResultSet> ResultSetManager::getOwnResultSet() {
1124  return rs_;
1125 }
1126 
1127 void ResultSetManager::rewriteVarlenAggregates(ResultSet* result_rs) {
1128  auto& result_storage = result_rs->storage_;
1129  result_storage->rewriteAggregateBufferOffsets(
1130  result_rs->serialized_varlen_buffer_.front());
1131 }
1132 
1133 void ResultSetStorage::fillOneEntryRowWise(const std::vector<int64_t>& entry) {
1134  const auto slot_count = query_mem_desc_.getBufferColSlotCount();
1135  const auto key_count = query_mem_desc_.getGroupbyColCount();
1136  CHECK_EQ(slot_count + key_count, entry.size());
1137  auto this_buff = reinterpret_cast<int64_t*>(buff_);
1139  CHECK_EQ(size_t(1), query_mem_desc_.getEntryCount());
1140  const auto key_off = key_offset_rowwise(0, key_count, slot_count);
1141  CHECK_EQ(query_mem_desc_.getEffectiveKeyWidth(), sizeof(int64_t));
1142  for (size_t i = 0; i < key_count; ++i) {
1143  this_buff[key_off + i] = entry[i];
1144  }
1145  const auto first_slot_off = slot_offset_rowwise(0, 0, key_count, slot_count);
1146  for (size_t i = 0; i < target_init_vals_.size(); ++i) {
1147  this_buff[first_slot_off + i] = entry[key_count + i];
1148  }
1149 }
1150 
1152  const auto key_count = query_mem_desc_.getGroupbyColCount();
1153  const auto row_size = get_row_bytes(query_mem_desc_);
1154  CHECK_EQ(row_size % 8, 0u);
1155  const auto key_bytes_with_padding =
1159  case 4: {
1160  for (size_t i = 0; i < query_mem_desc_.getEntryCount(); ++i) {
1161  auto row_ptr = buff_ + i * row_size;
1162  fill_empty_key_32(reinterpret_cast<int32_t*>(row_ptr), key_count);
1163  auto slot_ptr = reinterpret_cast<int64_t*>(row_ptr + key_bytes_with_padding);
1164  for (size_t j = 0; j < target_init_vals_.size(); ++j) {
1165  slot_ptr[j] = target_init_vals_[j];
1166  }
1167  }
1168  break;
1169  }
1170  case 8: {
1171  for (size_t i = 0; i < query_mem_desc_.getEntryCount(); ++i) {
1172  auto row_ptr = buff_ + i * row_size;
1173  fill_empty_key_64(reinterpret_cast<int64_t*>(row_ptr), key_count);
1174  auto slot_ptr = reinterpret_cast<int64_t*>(row_ptr + key_bytes_with_padding);
1175  for (size_t j = 0; j < target_init_vals_.size(); ++j) {
1176  slot_ptr[j] = target_init_vals_[j];
1177  }
1178  }
1179  break;
1180  }
1181  default:
1182  CHECK(false);
1183  }
1184 }
1185 
1186 void ResultSetStorage::fillOneEntryColWise(const std::vector<int64_t>& entry) {
1188  CHECK_EQ(size_t(1), query_mem_desc_.getEntryCount());
1189  const auto slot_count = query_mem_desc_.getBufferColSlotCount();
1190  const auto key_count = query_mem_desc_.getGroupbyColCount();
1191  CHECK_EQ(slot_count + key_count, entry.size());
1192  auto this_buff = reinterpret_cast<int64_t*>(buff_);
1193 
1194  for (size_t i = 0; i < key_count; i++) {
1195  const auto key_offset = key_offset_colwise(0, i, 1);
1196  this_buff[key_offset] = entry[i];
1197  }
1198 
1199  for (size_t i = 0; i < target_init_vals_.size(); i++) {
1200  const auto slot_offset = slot_offset_colwise(0, i, key_count, 1);
1201  this_buff[slot_offset] = entry[key_count + i];
1202  }
1203 }
1204 
1206  const auto key_count = query_mem_desc_.getGroupbyColCount();
1207  auto this_buff = reinterpret_cast<int64_t*>(buff_);
1209  for (size_t key_idx = 0; key_idx < key_count; ++key_idx) {
1210  const auto first_key_off =
1212  for (size_t i = 0; i < query_mem_desc_.getEntryCount(); ++i) {
1213  this_buff[first_key_off + i] = EMPTY_KEY_64;
1214  }
1215  }
1216  for (size_t target_idx = 0; target_idx < target_init_vals_.size(); ++target_idx) {
1217  const auto first_val_off =
1218  slot_offset_colwise(0, target_idx, key_count, query_mem_desc_.getEntryCount());
1219  for (size_t i = 0; i < query_mem_desc_.getEntryCount(); ++i) {
1220  this_buff[first_val_off + i] = target_init_vals_[target_idx];
1221  }
1222  }
1223 }
1224 
1225 void ResultSetStorage::initializeBaselineValueSlots(int64_t* entry_slots) const {
1226  CHECK(entry_slots);
1228  size_t slot_off = 0;
1229  for (size_t j = 0; j < target_init_vals_.size(); ++j) {
1230  entry_slots[slot_off] = target_init_vals_[j];
1231  slot_off += query_mem_desc_.getEntryCount();
1232  }
1233  } else {
1234  for (size_t j = 0; j < target_init_vals_.size(); ++j) {
1235  entry_slots[j] = target_init_vals_[j];
1236  }
1237  }
1238 }
1239 
1240 #define AGGREGATE_ONE_VALUE( \
1241  agg_kind__, val_ptr__, other_ptr__, chosen_bytes__, agg_info__) \
1242  do { \
1243  const auto sql_type = get_compact_type(agg_info__); \
1244  if (sql_type.is_fp()) { \
1245  if (chosen_bytes__ == sizeof(float)) { \
1246  agg_##agg_kind__##_float(reinterpret_cast<int32_t*>(val_ptr__), \
1247  *reinterpret_cast<const float*>(other_ptr__)); \
1248  } else { \
1249  agg_##agg_kind__##_double(reinterpret_cast<int64_t*>(val_ptr__), \
1250  *reinterpret_cast<const double*>(other_ptr__)); \
1251  } \
1252  } else { \
1253  if (chosen_bytes__ == sizeof(int32_t)) { \
1254  auto val_ptr = reinterpret_cast<int32_t*>(val_ptr__); \
1255  auto other_ptr = reinterpret_cast<const int32_t*>(other_ptr__); \
1256  agg_##agg_kind__##_int32(val_ptr, *other_ptr); \
1257  } else { \
1258  auto val_ptr = reinterpret_cast<int64_t*>(val_ptr__); \
1259  auto other_ptr = reinterpret_cast<const int64_t*>(other_ptr__); \
1260  agg_##agg_kind__(val_ptr, *other_ptr); \
1261  } \
1262  } \
1263  } while (0)
1264 
1265 #define AGGREGATE_ONE_NULLABLE_VALUE( \
1266  agg_kind__, val_ptr__, other_ptr__, init_val__, chosen_bytes__, agg_info__) \
1267  do { \
1268  if (agg_info__.skip_null_val) { \
1269  const auto sql_type = get_compact_type(agg_info__); \
1270  if (sql_type.is_fp()) { \
1271  if (chosen_bytes__ == sizeof(float)) { \
1272  agg_##agg_kind__##_float_skip_val( \
1273  reinterpret_cast<int32_t*>(val_ptr__), \
1274  *reinterpret_cast<const float*>(other_ptr__), \
1275  *reinterpret_cast<const float*>(may_alias_ptr(&init_val__))); \
1276  } else { \
1277  agg_##agg_kind__##_double_skip_val( \
1278  reinterpret_cast<int64_t*>(val_ptr__), \
1279  *reinterpret_cast<const double*>(other_ptr__), \
1280  *reinterpret_cast<const double*>(may_alias_ptr(&init_val__))); \
1281  } \
1282  } else { \
1283  if (chosen_bytes__ == sizeof(int32_t)) { \
1284  int32_t* val_ptr = reinterpret_cast<int32_t*>(val_ptr__); \
1285  const int32_t* other_ptr = reinterpret_cast<const int32_t*>(other_ptr__); \
1286  const auto null_val = static_cast<int32_t>(init_val__); \
1287  agg_##agg_kind__##_int32_skip_val(val_ptr, *other_ptr, null_val); \
1288  } else { \
1289  int64_t* val_ptr = reinterpret_cast<int64_t*>(val_ptr__); \
1290  const int64_t* other_ptr = reinterpret_cast<const int64_t*>(other_ptr__); \
1291  const auto null_val = static_cast<int64_t>(init_val__); \
1292  agg_##agg_kind__##_skip_val(val_ptr, *other_ptr, null_val); \
1293  } \
1294  } \
1295  } else { \
1296  AGGREGATE_ONE_VALUE( \
1297  agg_kind__, val_ptr__, other_ptr__, chosen_bytes__, agg_info__); \
1298  } \
1299  } while (0)
1300 
1301 #define AGGREGATE_ONE_COUNT(val_ptr__, other_ptr__, chosen_bytes__) \
1302  do { \
1303  if (chosen_bytes__ == sizeof(int32_t)) { \
1304  auto val_ptr = reinterpret_cast<int32_t*>(val_ptr__); \
1305  auto other_ptr = reinterpret_cast<const int32_t*>(other_ptr__); \
1306  agg_sum_int32(val_ptr, *other_ptr); \
1307  } else { \
1308  auto val_ptr = reinterpret_cast<int64_t*>(val_ptr__); \
1309  auto other_ptr = reinterpret_cast<const int64_t*>(other_ptr__); \
1310  agg_sum(val_ptr, *other_ptr); \
1311  } \
1312  } while (0)
1313 
1314 #define AGGREGATE_ONE_NULLABLE_COUNT( \
1315  val_ptr__, other_ptr__, init_val__, chosen_bytes__, agg_info__) \
1316  { \
1317  if (agg_info__.skip_null_val) { \
1318  const auto sql_type = get_compact_type(agg_info__); \
1319  if (sql_type.is_fp()) { \
1320  if (chosen_bytes__ == sizeof(float)) { \
1321  agg_sum_float_skip_val( \
1322  reinterpret_cast<int32_t*>(val_ptr__), \
1323  *reinterpret_cast<const float*>(other_ptr__), \
1324  *reinterpret_cast<const float*>(may_alias_ptr(&init_val__))); \
1325  } else { \
1326  agg_sum_double_skip_val( \
1327  reinterpret_cast<int64_t*>(val_ptr__), \
1328  *reinterpret_cast<const double*>(other_ptr__), \
1329  *reinterpret_cast<const double*>(may_alias_ptr(&init_val__))); \
1330  } \
1331  } else { \
1332  if (chosen_bytes__ == sizeof(int32_t)) { \
1333  auto val_ptr = reinterpret_cast<int32_t*>(val_ptr__); \
1334  auto other_ptr = reinterpret_cast<const int32_t*>(other_ptr__); \
1335  const auto null_val = static_cast<int32_t>(init_val__); \
1336  agg_sum_int32_skip_val(val_ptr, *other_ptr, null_val); \
1337  } else { \
1338  auto val_ptr = reinterpret_cast<int64_t*>(val_ptr__); \
1339  auto other_ptr = reinterpret_cast<const int64_t*>(other_ptr__); \
1340  const auto null_val = static_cast<int64_t>(init_val__); \
1341  agg_sum_skip_val(val_ptr, *other_ptr, null_val); \
1342  } \
1343  } \
1344  } else { \
1345  AGGREGATE_ONE_COUNT(val_ptr__, other_ptr__, chosen_bytes__); \
1346  } \
1347  }
1348 
1349 // to be used for 8/16-bit kMIN and kMAX only
1350 #define AGGREGATE_ONE_VALUE_SMALL( \
1351  agg_kind__, val_ptr__, other_ptr__, chosen_bytes__, agg_info__) \
1352  do { \
1353  if (chosen_bytes__ == sizeof(int16_t)) { \
1354  auto val_ptr = reinterpret_cast<int16_t*>(val_ptr__); \
1355  auto other_ptr = reinterpret_cast<const int16_t*>(other_ptr__); \
1356  agg_##agg_kind__##_int16(val_ptr, *other_ptr); \
1357  } else if (chosen_bytes__ == sizeof(int8_t)) { \
1358  auto val_ptr = reinterpret_cast<int8_t*>(val_ptr__); \
1359  auto other_ptr = reinterpret_cast<const int8_t*>(other_ptr__); \
1360  agg_##agg_kind__##_int8(val_ptr, *other_ptr); \
1361  } else { \
1362  UNREACHABLE(); \
1363  } \
1364  } while (0)
1365 
1366 // to be used for 8/16-bit kMIN and kMAX only
1367 #define AGGREGATE_ONE_NULLABLE_VALUE_SMALL( \
1368  agg_kind__, val_ptr__, other_ptr__, init_val__, chosen_bytes__, agg_info__) \
1369  do { \
1370  if (agg_info__.skip_null_val) { \
1371  if (chosen_bytes__ == sizeof(int16_t)) { \
1372  int16_t* val_ptr = reinterpret_cast<int16_t*>(val_ptr__); \
1373  const int16_t* other_ptr = reinterpret_cast<const int16_t*>(other_ptr__); \
1374  const auto null_val = static_cast<int16_t>(init_val__); \
1375  agg_##agg_kind__##_int16_skip_val(val_ptr, *other_ptr, null_val); \
1376  } else if (chosen_bytes == sizeof(int8_t)) { \
1377  int8_t* val_ptr = reinterpret_cast<int8_t*>(val_ptr__); \
1378  const int8_t* other_ptr = reinterpret_cast<const int8_t*>(other_ptr__); \
1379  const auto null_val = static_cast<int8_t>(init_val__); \
1380  agg_##agg_kind__##_int8_skip_val(val_ptr, *other_ptr, null_val); \
1381  } \
1382  } else { \
1383  AGGREGATE_ONE_VALUE_SMALL( \
1384  agg_kind__, val_ptr__, other_ptr__, chosen_bytes__, agg_info__); \
1385  } \
1386  } while (0)
1387 
1388 int8_t result_set::get_width_for_slot(const size_t target_slot_idx,
1389  const bool float_argument_input,
1391  if (float_argument_input) {
1392  return sizeof(float);
1393  }
1394  return query_mem_desc.getPaddedSlotWidthBytes(target_slot_idx);
1395 }
1396 
1398  const TargetInfo& target_info,
1399  const size_t target_slot_idx,
1400  const size_t init_agg_val_idx,
1401  const int8_t* that_ptr1) const {
1402  const bool float_argument_input = takes_float_argument(target_info);
1403  const auto chosen_bytes = result_set::get_width_for_slot(
1404  target_slot_idx, float_argument_input, query_mem_desc_);
1405  auto init_val = target_init_vals_[init_agg_val_idx];
1406 
1407  auto reduce = [&](auto const& size_tag) {
1408  using CastTarget = std::decay_t<decltype(size_tag)>;
1409  const auto lhs_proj_col = *reinterpret_cast<const CastTarget*>(this_ptr1);
1410  const auto rhs_proj_col = *reinterpret_cast<const CastTarget*>(that_ptr1);
1411  if (rhs_proj_col == init_val) {
1412  // ignore
1413  } else if (lhs_proj_col == init_val) {
1414  *reinterpret_cast<CastTarget*>(this_ptr1) = rhs_proj_col;
1415  } else if (lhs_proj_col != rhs_proj_col) {
1416  throw std::runtime_error("Multiple distinct values encountered");
1417  }
1418  };
1419 
1420  switch (chosen_bytes) {
1421  case 1: {
1423  reduce(int8_t());
1424  break;
1425  }
1426  case 2: {
1428  reduce(int16_t());
1429  break;
1430  }
1431  case 4: {
1432  reduce(int32_t());
1433  break;
1434  }
1435  case 8: {
1436  CHECK(!target_info.sql_type.is_varlen());
1437  reduce(int64_t());
1438  break;
1439  }
1440  default:
1441  LOG(FATAL) << "Invalid slot width: " << chosen_bytes;
1442  }
1443 }
1444 
1446  int8_t* this_ptr1,
1447  int8_t* this_ptr2,
1448  const int8_t* that_ptr1,
1449  const int8_t* that_ptr2,
1450  const TargetInfo& target_info,
1451  const size_t target_logical_idx,
1452  const size_t target_slot_idx,
1453  const size_t init_agg_val_idx,
1454  const ResultSetStorage& that,
1455  const size_t first_slot_idx_for_target,
1456  const std::vector<std::string>& serialized_varlen_buffer) const {
1458  if (query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) >= 0) {
1459  return;
1460  }
1461  }
1462  CHECK_LT(init_agg_val_idx, target_init_vals_.size());
1463  const bool float_argument_input = takes_float_argument(target_info);
1464  const auto chosen_bytes = result_set::get_width_for_slot(
1465  target_slot_idx, float_argument_input, query_mem_desc_);
1466  int64_t init_val = target_init_vals_[init_agg_val_idx]; // skip_val for nullable types
1467 
1468  if (target_info.is_agg && target_info.agg_kind == kSINGLE_VALUE) {
1470  this_ptr1, target_info, target_logical_idx, init_agg_val_idx, that_ptr1);
1471  } else if (target_info.is_agg && target_info.agg_kind != kSAMPLE) {
1472  switch (target_info.agg_kind) {
1473  case kCOUNT:
1474  case kAPPROX_COUNT_DISTINCT: {
1475  if (is_distinct_target(target_info)) {
1476  CHECK_EQ(static_cast<size_t>(chosen_bytes), sizeof(int64_t));
1477  reduceOneCountDistinctSlot(this_ptr1, that_ptr1, target_logical_idx, that);
1478  break;
1479  }
1480  CHECK_EQ(int64_t(0), init_val);
1481  AGGREGATE_ONE_COUNT(this_ptr1, that_ptr1, chosen_bytes);
1482  break;
1483  }
1484  case kAVG: {
1485  // Ignore float argument compaction for count component for fear of its overflow
1486  AGGREGATE_ONE_COUNT(this_ptr2,
1487  that_ptr2,
1488  query_mem_desc_.getPaddedSlotWidthBytes(target_slot_idx));
1489  }
1490  // fall thru
1491  case kSUM: {
1493  sum, this_ptr1, that_ptr1, init_val, chosen_bytes, target_info);
1494  break;
1495  }
1496  case kMIN: {
1497  if (static_cast<size_t>(chosen_bytes) <= sizeof(int16_t)) {
1499  min, this_ptr1, that_ptr1, init_val, chosen_bytes, target_info);
1500  } else {
1502  min, this_ptr1, that_ptr1, init_val, chosen_bytes, target_info);
1503  }
1504  break;
1505  }
1506  case kMAX: {
1507  if (static_cast<size_t>(chosen_bytes) <= sizeof(int16_t)) {
1509  max, this_ptr1, that_ptr1, init_val, chosen_bytes, target_info);
1510  } else {
1512  max, this_ptr1, that_ptr1, init_val, chosen_bytes, target_info);
1513  }
1514  break;
1515  }
1516  case kAPPROX_QUANTILE:
1517  CHECK_EQ(static_cast<int8_t>(sizeof(int64_t)), chosen_bytes);
1518  reduceOneApproxQuantileSlot(this_ptr1, that_ptr1, target_logical_idx, that);
1519  break;
1520  default:
1521  UNREACHABLE() << toString(target_info.agg_kind);
1522  }
1523  } else {
1524  switch (chosen_bytes) {
1525  case 1: {
1527  const auto rhs_proj_col = *reinterpret_cast<const int8_t*>(that_ptr1);
1528  if (rhs_proj_col != init_val) {
1529  *reinterpret_cast<int8_t*>(this_ptr1) = rhs_proj_col;
1530  }
1531  break;
1532  }
1533  case 2: {
1535  const auto rhs_proj_col = *reinterpret_cast<const int16_t*>(that_ptr1);
1536  if (rhs_proj_col != init_val) {
1537  *reinterpret_cast<int16_t*>(this_ptr1) = rhs_proj_col;
1538  }
1539  break;
1540  }
1541  case 4: {
1542  CHECK(target_info.agg_kind != kSAMPLE ||
1544  const auto rhs_proj_col = *reinterpret_cast<const int32_t*>(that_ptr1);
1545  if (rhs_proj_col != init_val) {
1546  *reinterpret_cast<int32_t*>(this_ptr1) = rhs_proj_col;
1547  }
1548  break;
1549  }
1550  case 8: {
1551  auto rhs_proj_col = *reinterpret_cast<const int64_t*>(that_ptr1);
1552  if ((target_info.agg_kind == kSAMPLE && target_info.sql_type.is_varlen()) &&
1553  !serialized_varlen_buffer.empty()) {
1554  size_t length_to_elems{0};
1555  if (target_info.sql_type.is_geometry()) {
1556  // TODO: Assumes hard-coded sizes for geometry targets
1557  length_to_elems = target_slot_idx == first_slot_idx_for_target ? 1 : 4;
1558  } else {
1559  const auto& elem_ti = target_info.sql_type.get_elem_type();
1560  length_to_elems = target_info.sql_type.is_string() ? 1 : elem_ti.get_size();
1561  }
1562 
1563  CHECK_LT(static_cast<size_t>(rhs_proj_col), serialized_varlen_buffer.size());
1564  const auto& varlen_bytes_str = serialized_varlen_buffer[rhs_proj_col];
1565  const auto str_ptr = reinterpret_cast<const int8_t*>(varlen_bytes_str.c_str());
1566  *reinterpret_cast<int64_t*>(this_ptr1) =
1567  reinterpret_cast<const int64_t>(str_ptr);
1568  *reinterpret_cast<int64_t*>(this_ptr2) =
1569  static_cast<int64_t>(varlen_bytes_str.size() / length_to_elems);
1570  } else {
1571  if (rhs_proj_col != init_val) {
1572  *reinterpret_cast<int64_t*>(this_ptr1) = rhs_proj_col;
1573  }
1574  if ((target_info.agg_kind == kSAMPLE && target_info.sql_type.is_varlen())) {
1575  CHECK(this_ptr2 && that_ptr2);
1576  *reinterpret_cast<int64_t*>(this_ptr2) =
1577  *reinterpret_cast<const int64_t*>(that_ptr2);
1578  }
1579  }
1580 
1581  break;
1582  }
1583  default:
1584  LOG(FATAL) << "Invalid slot width: " << chosen_bytes;
1585  }
1586  }
1587 }
1588 
1590  const int8_t* that_ptr1,
1591  const size_t target_logical_idx,
1592  const ResultSetStorage& that) const {
1594  static_assert(sizeof(int64_t) == sizeof(quantile::TDigest*));
1595  auto* incoming = *reinterpret_cast<quantile::TDigest* const*>(that_ptr1);
1596  CHECK(incoming) << "this_ptr1=" << (void*)this_ptr1
1597  << ", that_ptr1=" << (void const*)that_ptr1
1598  << ", target_logical_idx=" << target_logical_idx;
1599  if (incoming->centroids().capacity()) {
1600  auto* accumulator = *reinterpret_cast<quantile::TDigest**>(this_ptr1);
1601  CHECK(accumulator) << "this_ptr1=" << (void*)this_ptr1
1602  << ", that_ptr1=" << (void const*)that_ptr1
1603  << ", target_logical_idx=" << target_logical_idx;
1604  accumulator->allocate();
1605  accumulator->mergeTDigest(*incoming);
1606  }
1607 }
1608 
1610  const int8_t* that_ptr1,
1611  const size_t target_logical_idx,
1612  const ResultSetStorage& that) const {
1614  const auto& old_count_distinct_desc =
1615  query_mem_desc_.getCountDistinctDescriptor(target_logical_idx);
1616  CHECK(old_count_distinct_desc.impl_type_ != CountDistinctImplType::Invalid);
1617  const auto& new_count_distinct_desc =
1618  that.query_mem_desc_.getCountDistinctDescriptor(target_logical_idx);
1619  CHECK(old_count_distinct_desc.impl_type_ == new_count_distinct_desc.impl_type_);
1620  CHECK(this_ptr1 && that_ptr1);
1621  auto old_set_ptr = reinterpret_cast<const int64_t*>(this_ptr1);
1622  auto new_set_ptr = reinterpret_cast<const int64_t*>(that_ptr1);
1624  *new_set_ptr, *old_set_ptr, new_count_distinct_desc, old_count_distinct_desc);
1625 }
1626 
1627 bool ResultSetStorage::reduceSingleRow(const int8_t* row_ptr,
1628  const int8_t warp_count,
1629  const bool is_columnar,
1630  const bool replace_bitmap_ptr_with_bitmap_sz,
1631  std::vector<int64_t>& agg_vals,
1633  const std::vector<TargetInfo>& targets,
1634  const std::vector<int64_t>& agg_init_vals) {
1635  const size_t agg_col_count{agg_vals.size()};
1636  const auto row_size = query_mem_desc.getRowSize();
1637  CHECK_EQ(agg_col_count, query_mem_desc.getSlotCount());
1638  CHECK_GE(agg_col_count, targets.size());
1639  CHECK_EQ(is_columnar, query_mem_desc.didOutputColumnar());
1640  CHECK(query_mem_desc.hasKeylessHash());
1641  std::vector<int64_t> partial_agg_vals(agg_col_count, 0);
1642  bool discard_row = true;
1643  for (int8_t warp_idx = 0; warp_idx < warp_count; ++warp_idx) {
1644  bool discard_partial_result = true;
1645  for (size_t target_idx = 0, agg_col_idx = 0;
1646  target_idx < targets.size() && agg_col_idx < agg_col_count;
1647  ++target_idx, ++agg_col_idx) {
1648  const auto& agg_info = targets[target_idx];
1649  const bool float_argument_input = takes_float_argument(agg_info);
1650  const auto chosen_bytes = float_argument_input
1651  ? sizeof(float)
1652  : query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx);
1653  auto partial_bin_val = get_component(
1654  row_ptr + query_mem_desc.getColOnlyOffInBytes(agg_col_idx), chosen_bytes);
1655  partial_agg_vals[agg_col_idx] = partial_bin_val;
1656  if (is_distinct_target(agg_info)) {
1657  CHECK_EQ(int8_t(1), warp_count);
1658  CHECK(agg_info.is_agg && (agg_info.agg_kind == kCOUNT ||
1659  agg_info.agg_kind == kAPPROX_COUNT_DISTINCT));
1660  partial_bin_val = count_distinct_set_size(
1661  partial_bin_val, query_mem_desc.getCountDistinctDescriptor(target_idx));
1662  if (replace_bitmap_ptr_with_bitmap_sz) {
1663  partial_agg_vals[agg_col_idx] = partial_bin_val;
1664  }
1665  }
1666  if (kAVG == agg_info.agg_kind) {
1667  CHECK(agg_info.is_agg && !agg_info.is_distinct);
1668  ++agg_col_idx;
1669  partial_bin_val = partial_agg_vals[agg_col_idx] =
1670  get_component(row_ptr + query_mem_desc.getColOnlyOffInBytes(agg_col_idx),
1671  query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx));
1672  }
1673  if (agg_col_idx == static_cast<size_t>(query_mem_desc.getTargetIdxForKey()) &&
1674  partial_bin_val != agg_init_vals[query_mem_desc.getTargetIdxForKey()]) {
1675  CHECK(agg_info.is_agg);
1676  discard_partial_result = false;
1677  }
1678  }
1679  row_ptr += row_size;
1680  if (discard_partial_result) {
1681  continue;
1682  }
1683  discard_row = false;
1684  for (size_t target_idx = 0, agg_col_idx = 0;
1685  target_idx < targets.size() && agg_col_idx < agg_col_count;
1686  ++target_idx, ++agg_col_idx) {
1687  auto partial_bin_val = partial_agg_vals[agg_col_idx];
1688  const auto& agg_info = targets[target_idx];
1689  const bool float_argument_input = takes_float_argument(agg_info);
1690  const auto chosen_bytes = float_argument_input
1691  ? sizeof(float)
1692  : query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx);
1693  const auto& chosen_type = get_compact_type(agg_info);
1694  if (agg_info.is_agg && agg_info.agg_kind != kSAMPLE) {
1695  try {
1696  switch (agg_info.agg_kind) {
1697  case kCOUNT:
1700  reinterpret_cast<int8_t*>(&agg_vals[agg_col_idx]),
1701  reinterpret_cast<int8_t*>(&partial_agg_vals[agg_col_idx]),
1702  agg_init_vals[agg_col_idx],
1703  chosen_bytes,
1704  agg_info);
1705  break;
1706  case kAVG:
1707  // Ignore float argument compaction for count component for fear of its
1708  // overflow
1710  reinterpret_cast<int8_t*>(&agg_vals[agg_col_idx + 1]),
1711  reinterpret_cast<int8_t*>(&partial_agg_vals[agg_col_idx + 1]),
1712  query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx));
1713  // fall thru
1714  case kSUM:
1716  sum,
1717  reinterpret_cast<int8_t*>(&agg_vals[agg_col_idx]),
1718  reinterpret_cast<int8_t*>(&partial_agg_vals[agg_col_idx]),
1719  agg_init_vals[agg_col_idx],
1720  chosen_bytes,
1721  agg_info);
1722  break;
1723  case kMIN:
1724  if (static_cast<size_t>(chosen_bytes) <= sizeof(int16_t)) {
1726  min,
1727  reinterpret_cast<int8_t*>(&agg_vals[agg_col_idx]),
1728  reinterpret_cast<int8_t*>(&partial_agg_vals[agg_col_idx]),
1729  agg_init_vals[agg_col_idx],
1730  chosen_bytes,
1731  agg_info);
1732  } else {
1734  min,
1735  reinterpret_cast<int8_t*>(&agg_vals[agg_col_idx]),
1736  reinterpret_cast<int8_t*>(&partial_agg_vals[agg_col_idx]),
1737  agg_init_vals[agg_col_idx],
1738  chosen_bytes,
1739  agg_info);
1740  }
1741  break;
1742  case kMAX:
1743  if (static_cast<size_t>(chosen_bytes) <= sizeof(int16_t)) {
1745  max,
1746  reinterpret_cast<int8_t*>(&agg_vals[agg_col_idx]),
1747  reinterpret_cast<int8_t*>(&partial_agg_vals[agg_col_idx]),
1748  agg_init_vals[agg_col_idx],
1749  chosen_bytes,
1750  agg_info);
1751  } else {
1753  max,
1754  reinterpret_cast<int8_t*>(&agg_vals[agg_col_idx]),
1755  reinterpret_cast<int8_t*>(&partial_agg_vals[agg_col_idx]),
1756  agg_init_vals[agg_col_idx],
1757  chosen_bytes,
1758  agg_info);
1759  }
1760  break;
1761  default:
1762  CHECK(false);
1763  break;
1764  }
1765  } catch (std::runtime_error& e) {
1766  // TODO(miyu): handle the case where chosen_bytes < 8
1767  LOG(ERROR) << e.what();
1768  }
1769  if (chosen_type.is_integer() || chosen_type.is_decimal()) {
1770  switch (chosen_bytes) {
1771  case 8:
1772  break;
1773  case 4: {
1774  int32_t ret = *reinterpret_cast<const int32_t*>(&agg_vals[agg_col_idx]);
1775  if (!(agg_info.agg_kind == kCOUNT && ret != agg_init_vals[agg_col_idx])) {
1776  agg_vals[agg_col_idx] = static_cast<int64_t>(ret);
1777  }
1778  break;
1779  }
1780  default:
1781  CHECK(false);
1782  }
1783  }
1784  if (kAVG == agg_info.agg_kind) {
1785  ++agg_col_idx;
1786  }
1787  } else {
1788  if (agg_info.agg_kind == kSAMPLE) {
1789  CHECK(!agg_info.sql_type.is_varlen())
1790  << "Interleaved bins reduction not supported for variable length "
1791  "arguments "
1792  "to SAMPLE";
1793  }
1794  if (agg_vals[agg_col_idx]) {
1795  if (agg_info.agg_kind == kSAMPLE) {
1796  continue;
1797  }
1798  CHECK_EQ(agg_vals[agg_col_idx], partial_bin_val);
1799  } else {
1800  agg_vals[agg_col_idx] = partial_bin_val;
1801  }
1802  }
1803  }
1804  }
1805  return discard_row;
1806 }
GroupValueInfo get_group_value_reduction(int64_t *groups_buffer, const uint32_t groups_buffer_entry_count, const int64_t *key, const uint32_t key_count, const size_t key_width, const QueryMemoryDescriptor &query_mem_desc, const int64_t *that_buff_i64, const size_t that_entry_idx, const size_t that_entry_count, const uint32_t row_size_quad)
size_t slot_offset_rowwise(const size_t entry_idx, const size_t slot_idx, const size_t key_count, const size_t slot_count)
#define CHECK_EQ(x, y)
Definition: Logger.h:214
void count_distinct_set_union(const int64_t new_set_handle, const int64_t old_set_handle, const CountDistinctDescriptor &new_count_distinct_desc, const CountDistinctDescriptor &old_count_distinct_desc)
__device__ bool dynamic_watchdog()
void moveOneEntryToBuffer(const size_t entry_index, int64_t *new_buff_i64, const size_t new_entry_count, const size_t key_count, const size_t row_qw_count, const int64_t *src_buff, const size_t key_byte_width) const
bool isEmptyEntry(const size_t entry_idx, const int8_t *buff) const
void run_reduction_code(const ReductionCode &reduction_code, int8_t *this_buff, const int8_t *that_buff, const int32_t start_entry_index, const int32_t end_entry_index, const int32_t that_entry_count, const void *this_qmd, const void *that_qmd, const void *serialized_varlen_buffer)
#define EMPTY_KEY_64
std::string toString(const ExtArgumentType &sig_type)
static const int32_t ERR_INTERRUPTED
Definition: Execute.h:1142
const std::vector< TargetInfo > targets_
ALWAYS_INLINE void reduceOneSlot(int8_t *this_ptr1, int8_t *this_ptr2, const int8_t *that_ptr1, const int8_t *that_ptr2, const TargetInfo &target_info, const size_t target_logical_idx, const size_t target_slot_idx, const size_t init_agg_val_idx, const ResultSetStorage &that, const size_t first_slot_idx_for_target, const std::vector< std::string > &serialized_varlen_buffer) const
void reduceEntriesNoCollisionsColWise(int8_t *this_buff, const int8_t *that_buff, const ResultSetStorage &that, const size_t start_index, const size_t end_index, const std::vector< std::string > &serialized_varlen_buffer) const
std::vector< int64_t > target_init_vals_
void initializeColWise() const
size_t slot_offset_colwise(const size_t entry_idx, const size_t slot_idx, const size_t key_count, const size_t entry_count)
void reduceOneEntryBaseline(int8_t *this_buff, const int8_t *that_buff, const size_t i, const size_t that_entry_count, const ResultSetStorage &that) const
int64_t getTargetGroupbyIndex(const size_t target_idx) const
bool isEmptyEntryColumnar(const size_t entry_idx, const int8_t *buff) const
int64_t get_component(const int8_t *group_by_buffer, const size_t comp_sz, const size_t index=0)
std::unique_ptr< Function > ir_reduce_loop
bool isLogicalSizedColumnsAllowed() const
T advance_to_next_columnar_target_buff(T target_ptr, const QueryMemoryDescriptor &query_mem_desc, const size_t target_slot_idx)
#define const
SQLTypeInfo sql_type
Definition: TargetInfo.h:51
#define LOG(tag)
Definition: Logger.h:200
std::shared_ptr< ResultSet > rs_
Definition: ResultSet.h:814
GroupValueInfo get_matching_group_value_reduction(int64_t *groups_buffer, const uint32_t h, const T *key, const uint32_t key_count, const QueryMemoryDescriptor &query_mem_desc, const int64_t *that_buff_i64, const size_t that_entry_idx, const size_t that_entry_count, const uint32_t row_size_quad)
ALWAYS_INLINE void check_watchdog_with_seed(const size_t sample_seed)
bool is_varlen() const
Definition: sqltypes.h:523
void initializeStorage() const
#define UNREACHABLE()
Definition: Logger.h:250
#define CHECK_GE(x, y)
Definition: Logger.h:219
void fill_slots(int64_t *dst_entry, const size_t dst_entry_count, const int64_t *src_buff, const size_t src_entry_idx, const size_t src_entry_count, const QueryMemoryDescriptor &query_mem_desc)
void initializeRowWise() const
void reduceOneApproxQuantileSlot(int8_t *this_ptr1, const int8_t *that_ptr1, const size_t target_logical_idx, const ResultSetStorage &that) const
void initializeBaselineValueSlots(int64_t *this_entry_slots) const
GroupValueInfo get_group_value_columnar_reduction(int64_t *groups_buffer, const uint32_t groups_buffer_entry_count, const int64_t *key, const uint32_t key_qw_count)
size_t get_slot_off_quad(const QueryMemoryDescriptor &query_mem_desc)
size_t getEffectiveKeyWidth() const
ALWAYS_INLINE void fill_empty_key_32(int32_t *key_ptr_i32, const size_t key_count)
void reduceOneSlotBaseline(int64_t *this_buff, const size_t this_slot, const int64_t *that_buff, const size_t that_entry_count, const size_t that_slot, const TargetInfo &target_info, const size_t target_logical_idx, const size_t target_slot_idx, const size_t init_agg_val_idx, const ResultSetStorage &that) const
bool g_enable_dynamic_watchdog
Definition: Execute.cpp:76
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:157
bool g_enable_non_kernel_time_query_interrupt
Definition: Execute.cpp:115
std::vector< int64_t > make_key(const int64_t *buff, const size_t entry_count, const size_t key_count)
T advance_target_ptr_row_wise(T target_ptr, const TargetInfo &target_info, const size_t slot_idx, const QueryMemoryDescriptor &query_mem_desc, const bool separate_varlen_storage)
#define CHECK_GT(x, y)
Definition: Logger.h:218
void rewriteVarlenAggregates(ResultSet *)
void reduce(const ResultSetStorage &that, const std::vector< std::string > &serialized_varlen_buffer, const ReductionCode &reduction_code) const
size_t getColOnlyOffInBytes(const size_t col_idx) const
Definition: sqldefs.h:73
const SQLTypeInfo get_compact_type(const TargetInfo &target)
__device__ bool check_interrupt()
int8_t get_width_for_slot(const size_t target_slot_idx, const bool float_argument_input, const QueryMemoryDescriptor &query_mem_desc)
int8_t groupColWidth(const size_t key_idx) const
void reduceOneEntrySlotsBaseline(int64_t *this_entry_slots, const int64_t *that_buff, const size_t that_entry_idx, const size_t that_entry_count, const ResultSetStorage &that) const
static EvalValue run(const Function *function, const std::vector< EvalValue > &inputs)
bool is_agg
Definition: TargetInfo.h:49
size_t advance_slot(const size_t j, const TargetInfo &target_info, const bool separate_varlen_storage)
void copyKeyColWise(const size_t entry_idx, int8_t *this_buff, const int8_t *that_buff) const
int64_t count_distinct_set_size(const int64_t set_handle, const CountDistinctDescriptor &count_distinct_desc)
Definition: CountDistinct.h:75
void init(LogOptions const &log_opts)
Definition: Logger.cpp:282
void reduceOneCountDistinctSlot(int8_t *this_ptr1, const int8_t *that_ptr1, const size_t target_logical_idx, const ResultSetStorage &that) const
size_t getGroupbyColCount() const
void moveEntriesToBuffer(int8_t *new_buff, const size_t new_entry_count) const
void fill_empty_key(void *key_ptr, const size_t key_count, const size_t key_width)
#define store_cst(ptr, val)
size_t targetGroupbyIndicesSize() const
std::pair< int64_t *, bool > GroupValueInfo
ResultSet * reduce(std::vector< ResultSet * > &)
Definition: sqldefs.h:75
DEVICE auto accumulate(ARGS &&...args)
Definition: gpu_enabled.h:42
#define LIKELY(x)
Definition: likely.h:24
std::shared_ptr< ResultSet > getOwnResultSet()
void fillOneEntryColWise(const std::vector< int64_t > &entry)
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:153
static EvalValue MakeEvalValue(const T &val)
GroupValueInfo get_matching_group_value_columnar_reduction(int64_t *groups_buffer, const uint32_t h, const int64_t *key, const uint32_t key_qw_count, const size_t entry_count)
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
static const int32_t ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES
Definition: Execute.h:1147
RUNTIME_EXPORT NEVER_INLINE DEVICE int64_t * get_group_value(int64_t *groups_buffer, const uint32_t groups_buffer_entry_count, const int64_t *key, const uint32_t key_count, const uint32_t key_width, const uint32_t row_size_quad)
T row_ptr_rowwise(T buff, const QueryMemoryDescriptor &query_mem_desc, const size_t entry_idx)
#define mapd_cas(address, compare, val)
SQLAgg agg_kind
Definition: TargetInfo.h:50
size_t getCountDistinctDescriptorsSize() const
QueryDescriptionType getQueryDescriptionType() const
#define cas_cst(ptr, expected, desired)
#define UNLIKELY(x)
Definition: likely.h:25
size_t key_offset_colwise(const size_t entry_idx, const size_t key_idx, const size_t entry_count)
virtual ReductionCode codegen() const
const CountDistinctDescriptor & getCountDistinctDescriptor(const size_t idx) const
#define load_cst(ptr)
#define AGGREGATE_ONE_NULLABLE_VALUE(agg_kind__, val_ptr__, other_ptr__, init_val__, chosen_bytes__, agg_info__)
#define CHECK_LT(x, y)
Definition: Logger.h:216
size_t get_row_bytes(const QueryMemoryDescriptor &query_mem_desc)
ALWAYS_INLINE void reduceOneSlotSingleValue(int8_t *this_ptr1, const TargetInfo &target_info, const size_t target_slot_idx, const size_t init_agg_val_idx, const int8_t *that_ptr1) const
#define AGGREGATE_ONE_COUNT(val_ptr__, other_ptr__, chosen_bytes__)
Definition: sqldefs.h:76
#define AGGREGATE_ONE_NULLABLE_COUNT(val_ptr__, other_ptr__, init_val__, chosen_bytes__, agg_info__)
size_t key_offset_rowwise(const size_t entry_idx, const size_t key_count, const size_t slot_count)
bool use_multithreaded_reduction(const size_t entry_count)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE uint32_t key_hash(const int64_t *key, const uint32_t key_count, const uint32_t key_byte_width)
const ColSlotContext & getColSlotContext() const
#define CHECK(condition)
Definition: Logger.h:206
bool is_geometry() const
Definition: sqltypes.h:510
#define EMPTY_KEY_32
static std::mutex compilation_mutex_
Definition: Execute.h:1150
void fillOneEntryRowWise(const std::vector< int64_t > &entry)
Basic constructors and methods of the row set interface.
size_t get_row_qw_count(const QueryMemoryDescriptor &query_mem_desc)
#define AGGREGATE_ONE_NULLABLE_VALUE_SMALL(agg_kind__, val_ptr__, other_ptr__, init_val__, chosen_bytes__, agg_info__)
void rewriteAggregateBufferOffsets(const std::vector< std::string > &serialized_varlen_buffer) const
ALWAYS_INLINE void fill_empty_key_64(int64_t *key_ptr_i64, const size_t key_count)
bool is_string() const
Definition: sqltypes.h:498
Definition: sqldefs.h:74
#define ALWAYS_INLINE
int cpu_threads()
Definition: thread_count.h:24
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:726
T get_cols_ptr(T buff, const QueryMemoryDescriptor &query_mem_desc)
Definition: sqldefs.h:72
static bool reduceSingleRow(const int8_t *row_ptr, const int8_t warp_count, const bool is_columnar, const bool replace_bitmap_ptr_with_bitmap_sz, std::vector< int64_t > &agg_vals, const QueryMemoryDescriptor &query_mem_desc, const std::vector< TargetInfo > &targets, const std::vector< int64_t > &agg_init_vals)
RUNTIME_EXPORT NEVER_INLINE DEVICE int64_t * get_group_value_columnar(int64_t *groups_buffer, const uint32_t groups_buffer_entry_count, const int64_t *key, const uint32_t key_qw_count)
size_t get_key_bytes_rowwise(const QueryMemoryDescriptor &query_mem_desc)
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)
QueryMemoryDescriptor query_mem_desc_
int32_t getTargetIdxForKey() const
size_t getPrependedGroupColOffInBytes(const size_t group_idx) const