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