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