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