OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResultSetIteration.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 "Execute.h"
26 #include "Geospatial/Compression.h"
27 #include "Geospatial/Types.h"
28 #include "ParserNode.h"
30 #include "ResultSet.h"
32 #include "RuntimeFunctions.h"
33 #include "Shared/SqlTypesLayout.h"
34 #include "Shared/likely.h"
35 #include "Shared/sqltypes.h"
36 #include "TypePunning.h"
37 
38 #include <boost/math/special_functions/fpclassify.hpp>
39 
40 #include <memory>
41 #include <utility>
42 
43 namespace {
44 
45 // Interprets ptr1, ptr2 as the sum and count pair used for AVG.
47  const int8_t compact_sz1,
48  const int8_t* ptr2,
49  const int8_t compact_sz2,
50  const TargetInfo& target_info) {
51  int64_t sum{0};
52  CHECK(target_info.agg_kind == kAVG);
53  const bool float_argument_input = takes_float_argument(target_info);
54  const auto actual_compact_sz1 = float_argument_input ? sizeof(float) : compact_sz1;
55  const auto& agg_ti = target_info.agg_arg_type;
56  if (agg_ti.is_integer() || agg_ti.is_decimal()) {
57  sum = read_int_from_buff(ptr1, actual_compact_sz1);
58  } else if (agg_ti.is_fp()) {
59  switch (actual_compact_sz1) {
60  case 8: {
61  double d = *reinterpret_cast<const double*>(ptr1);
62  sum = *reinterpret_cast<const int64_t*>(may_alias_ptr(&d));
63  break;
64  }
65  case 4: {
66  double d = *reinterpret_cast<const float*>(ptr1);
67  sum = *reinterpret_cast<const int64_t*>(may_alias_ptr(&d));
68  break;
69  }
70  default:
71  CHECK(false);
72  }
73  } else {
74  CHECK(false);
75  }
76  const auto count = read_int_from_buff(ptr2, compact_sz2);
77  return pair_to_double({sum, count}, target_info.sql_type, false);
78 }
79 
80 // Given the entire buffer for the result set, buff, finds the beginning of the
81 // column for slot_idx. Only makes sense for column-wise representation.
82 const int8_t* advance_col_buff_to_slot(const int8_t* buff,
84  const std::vector<TargetInfo>& targets,
85  const size_t slot_idx,
86  const bool separate_varlen_storage) {
87  auto crt_col_ptr = get_cols_ptr(buff, query_mem_desc);
88  const auto buffer_col_count = query_mem_desc.getBufferColSlotCount();
89  size_t agg_col_idx{0};
90  for (size_t target_idx = 0; target_idx < targets.size(); ++target_idx) {
91  if (agg_col_idx == slot_idx) {
92  return crt_col_ptr;
93  }
94  CHECK_LT(agg_col_idx, buffer_col_count);
95  const auto& agg_info = targets[target_idx];
96  crt_col_ptr =
97  advance_to_next_columnar_target_buff(crt_col_ptr, query_mem_desc, agg_col_idx);
98  if (agg_info.is_agg && agg_info.agg_kind == kAVG) {
99  if (agg_col_idx + 1 == slot_idx) {
100  return crt_col_ptr;
101  }
103  crt_col_ptr, query_mem_desc, agg_col_idx + 1);
104  }
105  agg_col_idx = advance_slot(agg_col_idx, agg_info, separate_varlen_storage);
106  }
107  CHECK(false);
108  return nullptr;
109 }
110 } // namespace
111 
112 // Gets the byte offset, starting from the beginning of the row targets buffer, of
113 // the value in position slot_idx (only makes sense for row-wise representation).
114 size_t result_set::get_byteoff_of_slot(const size_t slot_idx,
116  return query_mem_desc.getPaddedColWidthForRange(0, slot_idx);
117 }
118 
119 std::vector<TargetValue> ResultSet::getRowAt(
120  const size_t global_entry_idx,
121  const bool translate_strings,
122  const bool decimal_to_double,
123  const bool fixup_count_distinct_pointers,
124  const std::vector<bool>& targets_to_skip /* = {}*/) const {
125  const auto storage_lookup_result =
126  fixup_count_distinct_pointers
127  ? StorageLookupResult{storage_.get(), global_entry_idx, 0}
128  : findStorage(global_entry_idx);
129  const auto storage = storage_lookup_result.storage_ptr;
130  const auto local_entry_idx = storage_lookup_result.fixedup_entry_idx;
131  if (!fixup_count_distinct_pointers && storage->isEmptyEntry(local_entry_idx)) {
132  return {};
133  }
134 
135  const auto buff = storage->buff_;
136  CHECK(buff);
137  std::vector<TargetValue> row;
138  size_t agg_col_idx = 0;
139  int8_t* rowwise_target_ptr{nullptr};
140  int8_t* keys_ptr{nullptr};
141  const int8_t* crt_col_ptr{nullptr};
142  if (query_mem_desc_.didOutputColumnar()) {
143  keys_ptr = buff;
144  crt_col_ptr = get_cols_ptr(buff, storage->query_mem_desc_);
145  } else {
146  keys_ptr = row_ptr_rowwise(buff, query_mem_desc_, local_entry_idx);
147  const auto key_bytes_with_padding =
148  align_to_int64(get_key_bytes_rowwise(query_mem_desc_));
149  rowwise_target_ptr = keys_ptr + key_bytes_with_padding;
150  }
151  for (size_t target_idx = 0; target_idx < storage->targets_.size(); ++target_idx) {
152  const auto& agg_info = storage->targets_[target_idx];
153  if (query_mem_desc_.didOutputColumnar()) {
154  if (UNLIKELY(!targets_to_skip.empty())) {
155  row.push_back(!targets_to_skip[target_idx]
156  ? getTargetValueFromBufferColwise(crt_col_ptr,
157  keys_ptr,
158  storage->query_mem_desc_,
159  local_entry_idx,
160  global_entry_idx,
161  agg_info,
162  target_idx,
163  agg_col_idx,
164  translate_strings,
165  decimal_to_double)
166  : nullptr);
167  } else {
168  row.push_back(getTargetValueFromBufferColwise(crt_col_ptr,
169  keys_ptr,
170  storage->query_mem_desc_,
171  local_entry_idx,
172  global_entry_idx,
173  agg_info,
174  target_idx,
175  agg_col_idx,
176  translate_strings,
177  decimal_to_double));
178  }
179  crt_col_ptr = advance_target_ptr_col_wise(crt_col_ptr,
180  agg_info,
181  agg_col_idx,
182  storage->query_mem_desc_,
183  separate_varlen_storage_valid_);
184  } else {
185  if (UNLIKELY(!targets_to_skip.empty())) {
186  row.push_back(!targets_to_skip[target_idx]
187  ? getTargetValueFromBufferRowwise(rowwise_target_ptr,
188  keys_ptr,
189  global_entry_idx,
190  agg_info,
191  target_idx,
192  agg_col_idx,
193  translate_strings,
194  decimal_to_double,
195  fixup_count_distinct_pointers)
196  : nullptr);
197  } else {
198  row.push_back(getTargetValueFromBufferRowwise(rowwise_target_ptr,
199  keys_ptr,
200  global_entry_idx,
201  agg_info,
202  target_idx,
203  agg_col_idx,
204  translate_strings,
205  decimal_to_double,
206  fixup_count_distinct_pointers));
207  }
208  rowwise_target_ptr = advance_target_ptr_row_wise(rowwise_target_ptr,
209  agg_info,
210  agg_col_idx,
211  query_mem_desc_,
212  separate_varlen_storage_valid_);
213  }
214  agg_col_idx = advance_slot(agg_col_idx, agg_info, separate_varlen_storage_valid_);
215  }
216 
217  return row;
218 }
219 
220 TargetValue ResultSet::getRowAt(const size_t row_idx,
221  const size_t col_idx,
222  const bool translate_strings,
223  const bool decimal_to_double /* = true */) const {
224  std::lock_guard<std::mutex> lock(row_iteration_mutex_);
225  moveToBegin();
226  for (size_t i = 0; i < row_idx; ++i) {
227  auto crt_row = getNextRowUnlocked(translate_strings, decimal_to_double);
228  CHECK(!crt_row.empty());
229  }
230  auto crt_row = getNextRowUnlocked(translate_strings, decimal_to_double);
231  CHECK(!crt_row.empty());
232  return crt_row[col_idx];
233 }
234 
235 OneIntegerColumnRow ResultSet::getOneColRow(const size_t global_entry_idx) const {
236  const auto storage_lookup_result = findStorage(global_entry_idx);
237  const auto storage = storage_lookup_result.storage_ptr;
238  const auto local_entry_idx = storage_lookup_result.fixedup_entry_idx;
239  if (storage->isEmptyEntry(local_entry_idx)) {
240  return {0, false};
241  }
242  const auto buff = storage->buff_;
243  CHECK(buff);
244  CHECK(!query_mem_desc_.didOutputColumnar());
245  const auto keys_ptr = row_ptr_rowwise(buff, query_mem_desc_, local_entry_idx);
246  const auto key_bytes_with_padding =
247  align_to_int64(get_key_bytes_rowwise(query_mem_desc_));
248  const auto rowwise_target_ptr = keys_ptr + key_bytes_with_padding;
249  const auto tv = getTargetValueFromBufferRowwise(rowwise_target_ptr,
250  keys_ptr,
251  global_entry_idx,
252  targets_.front(),
253  0,
254  0,
255  false,
256  false,
257  false);
258  const auto scalar_tv = boost::get<ScalarTargetValue>(&tv);
259  CHECK(scalar_tv);
260  const auto ival_ptr = boost::get<int64_t>(scalar_tv);
261  CHECK(ival_ptr);
262  return {*ival_ptr, true};
263 }
264 
265 std::vector<TargetValue> ResultSet::getRowAt(const size_t logical_index) const {
266  if (logical_index >= entryCount()) {
267  return {};
268  }
269  const auto entry_idx =
270  permutation_.empty() ? logical_index : permutation_[logical_index];
271  return getRowAt(entry_idx, true, false, false);
272 }
273 
274 std::vector<TargetValue> ResultSet::getRowAtNoTranslations(
275  const size_t logical_index,
276  const std::vector<bool>& targets_to_skip /* = {}*/) const {
277  if (logical_index >= entryCount()) {
278  return {};
279  }
280  const auto entry_idx =
281  permutation_.empty() ? logical_index : permutation_[logical_index];
282  return getRowAt(entry_idx, false, false, false, targets_to_skip);
283 }
284 
285 bool ResultSet::isRowAtEmpty(const size_t logical_index) const {
286  if (logical_index >= entryCount()) {
287  return true;
288  }
289  const auto entry_idx =
290  permutation_.empty() ? logical_index : permutation_[logical_index];
291  const auto storage_lookup_result = findStorage(entry_idx);
292  const auto storage = storage_lookup_result.storage_ptr;
293  const auto local_entry_idx = storage_lookup_result.fixedup_entry_idx;
294  return storage->isEmptyEntry(local_entry_idx);
295 }
296 
297 std::vector<TargetValue> ResultSet::getNextRow(const bool translate_strings,
298  const bool decimal_to_double) const {
299  std::lock_guard<std::mutex> lock(row_iteration_mutex_);
300  if (!storage_ && !just_explain_) {
301  return {};
302  }
303  return getNextRowUnlocked(translate_strings, decimal_to_double);
304 }
305 
306 std::vector<TargetValue> ResultSet::getNextRowUnlocked(
307  const bool translate_strings,
308  const bool decimal_to_double) const {
309  if (just_explain_) {
310  if (fetched_so_far_) {
311  return {};
312  }
313  fetched_so_far_ = 1;
314  return {explanation_};
315  }
316  return getNextRowImpl(translate_strings, decimal_to_double);
317 }
318 
319 std::vector<TargetValue> ResultSet::getNextRowImpl(const bool translate_strings,
320  const bool decimal_to_double) const {
321  size_t entry_buff_idx = 0;
322  do {
323  if (keep_first_ && fetched_so_far_ >= drop_first_ + keep_first_) {
324  return {};
325  }
326 
327  entry_buff_idx = advanceCursorToNextEntry();
328 
329  if (crt_row_buff_idx_ >= entryCount()) {
330  CHECK_EQ(entryCount(), crt_row_buff_idx_);
331  return {};
332  }
333  ++crt_row_buff_idx_;
334  ++fetched_so_far_;
335 
336  } while (drop_first_ && fetched_so_far_ <= drop_first_);
337 
338  auto row = getRowAt(entry_buff_idx, translate_strings, decimal_to_double, false);
339  CHECK(!row.empty());
340 
341  return row;
342 }
343 
344 namespace {
345 
346 const int8_t* columnar_elem_ptr(const size_t entry_idx,
347  const int8_t* col1_ptr,
348  const int8_t compact_sz1) {
349  return col1_ptr + compact_sz1 * entry_idx;
350 }
351 
352 int64_t int_resize_cast(const int64_t ival, const size_t sz) {
353  switch (sz) {
354  case 8:
355  return ival;
356  case 4:
357  return static_cast<int32_t>(ival);
358  case 2:
359  return static_cast<int16_t>(ival);
360  case 1:
361  return static_cast<int8_t>(ival);
362  default:
363  UNREACHABLE();
364  }
365  UNREACHABLE();
366  return 0;
367 }
368 
369 } // namespace
370 
372  // Compute offsets for base storage and all appended storage
373  for (size_t storage_idx = 0; storage_idx < result_set_->appended_storage_.size() + 1;
374  ++storage_idx) {
375  offsets_for_storage_.emplace_back();
376 
377  const int8_t* rowwise_target_ptr{0};
378 
379  size_t agg_col_idx = 0;
380  for (size_t target_idx = 0; target_idx < result_set_->storage_->targets_.size();
381  ++target_idx) {
382  const auto& agg_info = result_set_->storage_->targets_[target_idx];
383 
384  auto ptr1 = rowwise_target_ptr;
385  const auto compact_sz1 =
386  result_set_->query_mem_desc_.getPaddedSlotWidthBytes(agg_col_idx)
387  ? result_set_->query_mem_desc_.getPaddedSlotWidthBytes(agg_col_idx)
388  : key_width_;
389 
390  const int8_t* ptr2{nullptr};
391  int8_t compact_sz2{0};
392  if ((agg_info.is_agg && agg_info.agg_kind == kAVG)) {
393  ptr2 = ptr1 + compact_sz1;
394  compact_sz2 =
395  result_set_->query_mem_desc_.getPaddedSlotWidthBytes(agg_col_idx + 1);
396  } else if (is_real_str_or_array(agg_info)) {
397  ptr2 = ptr1 + compact_sz1;
398  if (!result_set_->separate_varlen_storage_valid_) {
399  // None encoded strings explicitly attached to ResultSetStorage do not have a
400  // second slot in the QueryMemoryDescriptor col width vector
401  compact_sz2 =
402  result_set_->query_mem_desc_.getPaddedSlotWidthBytes(agg_col_idx + 1);
403  }
404  }
405  offsets_for_storage_[storage_idx].push_back(
406  TargetOffsets{ptr1,
407  static_cast<size_t>(compact_sz1),
408  ptr2,
409  static_cast<size_t>(compact_sz2)});
410  rowwise_target_ptr =
411  advance_target_ptr_row_wise(rowwise_target_ptr,
412  agg_info,
413  agg_col_idx,
414  result_set_->query_mem_desc_,
415  result_set_->separate_varlen_storage_valid_);
416 
417  agg_col_idx = advance_slot(
418  agg_col_idx, agg_info, result_set_->separate_varlen_storage_valid_);
419  }
420  CHECK_EQ(offsets_for_storage_[storage_idx].size(),
421  result_set_->storage_->targets_.size());
422  }
423 }
424 
426  const int8_t* buff,
427  const size_t entry_idx,
428  const size_t target_logical_idx,
429  const StorageLookupResult& storage_lookup_result) const {
430  CHECK(buff);
431  const int8_t* rowwise_target_ptr{nullptr};
432  const int8_t* keys_ptr{nullptr};
433 
434  const size_t storage_idx = storage_lookup_result.storage_idx;
435 
436  CHECK_LT(storage_idx, offsets_for_storage_.size());
437  CHECK_LT(target_logical_idx, offsets_for_storage_[storage_idx].size());
438 
439  const auto& offsets_for_target = offsets_for_storage_[storage_idx][target_logical_idx];
440  const auto& agg_info = result_set_->storage_->targets_[target_logical_idx];
441  const auto& type_info = agg_info.sql_type;
442 
443  keys_ptr = get_rowwise_ptr(buff, entry_idx);
444  rowwise_target_ptr = keys_ptr + key_bytes_with_padding_;
445  auto ptr1 = rowwise_target_ptr + reinterpret_cast<size_t>(offsets_for_target.ptr1);
446  if (result_set_->query_mem_desc_.targetGroupbyIndicesSize() > 0) {
447  if (result_set_->query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) >= 0) {
448  ptr1 = keys_ptr +
449  result_set_->query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) *
450  key_width_;
451  }
452  }
453  const auto i1 =
454  result_set_->lazyReadInt(read_int_from_buff(ptr1, offsets_for_target.compact_sz1),
455  target_logical_idx,
456  storage_lookup_result);
457  if (agg_info.is_agg && agg_info.agg_kind == kAVG) {
458  CHECK(offsets_for_target.ptr2);
459  const auto ptr2 =
460  rowwise_target_ptr + reinterpret_cast<size_t>(offsets_for_target.ptr2);
461  const auto i2 = read_int_from_buff(ptr2, offsets_for_target.compact_sz2);
462  return InternalTargetValue(i1, i2);
463  } else {
464  if (type_info.is_string() && type_info.get_compression() == kENCODING_NONE) {
465  CHECK(!agg_info.is_agg);
466  if (!result_set_->lazy_fetch_info_.empty()) {
467  CHECK_LT(target_logical_idx, result_set_->lazy_fetch_info_.size());
468  const auto& col_lazy_fetch = result_set_->lazy_fetch_info_[target_logical_idx];
469  if (col_lazy_fetch.is_lazily_fetched) {
470  return InternalTargetValue(reinterpret_cast<const std::string*>(i1));
471  }
472  }
473  if (result_set_->separate_varlen_storage_valid_) {
474  if (i1 < 0) {
475  CHECK_EQ(-1, i1);
476  return InternalTargetValue(static_cast<const std::string*>(nullptr));
477  }
478  CHECK_LT(storage_lookup_result.storage_idx,
479  result_set_->serialized_varlen_buffer_.size());
480  const auto& varlen_buffer_for_fragment =
481  result_set_->serialized_varlen_buffer_[storage_lookup_result.storage_idx];
482  CHECK_LT(static_cast<size_t>(i1), varlen_buffer_for_fragment.size());
483  return InternalTargetValue(&varlen_buffer_for_fragment[i1]);
484  }
485  CHECK(offsets_for_target.ptr2);
486  const auto ptr2 =
487  rowwise_target_ptr + reinterpret_cast<size_t>(offsets_for_target.ptr2);
488  const auto str_len = read_int_from_buff(ptr2, offsets_for_target.compact_sz2);
489  CHECK_GE(str_len, 0);
490  return result_set_->getVarlenOrderEntry(i1, str_len);
491  }
492  return InternalTargetValue(
493  type_info.is_fp() ? i1 : int_resize_cast(i1, type_info.get_logical_size()));
494  }
495 }
496 
498  // Compute offsets for base storage and all appended storage
499  const auto key_width = result_set_->query_mem_desc_.getEffectiveKeyWidth();
500  for (size_t storage_idx = 0; storage_idx < result_set_->appended_storage_.size() + 1;
501  ++storage_idx) {
502  offsets_for_storage_.emplace_back();
503 
504  const int8_t* buff = storage_idx == 0
505  ? result_set_->storage_->buff_
506  : result_set_->appended_storage_[storage_idx - 1]->buff_;
507  CHECK(buff);
508 
509  const auto& crt_query_mem_desc =
510  storage_idx == 0
511  ? result_set_->storage_->query_mem_desc_
512  : result_set_->appended_storage_[storage_idx - 1]->query_mem_desc_;
513  const int8_t* crt_col_ptr = get_cols_ptr(buff, crt_query_mem_desc);
514 
515  size_t agg_col_idx = 0;
516  for (size_t target_idx = 0; target_idx < result_set_->storage_->targets_.size();
517  ++target_idx) {
518  const auto& agg_info = result_set_->storage_->targets_[target_idx];
519 
520  const auto compact_sz1 =
521  crt_query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx)
522  ? crt_query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx)
523  : key_width;
524 
525  const auto next_col_ptr = advance_to_next_columnar_target_buff(
526  crt_col_ptr, crt_query_mem_desc, agg_col_idx);
527  const bool uses_two_slots = (agg_info.is_agg && agg_info.agg_kind == kAVG) ||
528  is_real_str_or_array(agg_info);
529  const auto col2_ptr = uses_two_slots ? next_col_ptr : nullptr;
530  const auto compact_sz2 =
531  (agg_info.is_agg && agg_info.agg_kind == kAVG) || is_real_str_or_array(agg_info)
532  ? crt_query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx + 1)
533  : 0;
534 
535  offsets_for_storage_[storage_idx].push_back(
536  TargetOffsets{crt_col_ptr,
537  static_cast<size_t>(compact_sz1),
538  col2_ptr,
539  static_cast<size_t>(compact_sz2)});
540 
541  crt_col_ptr = next_col_ptr;
542  if (uses_two_slots) {
544  crt_col_ptr, crt_query_mem_desc, agg_col_idx + 1);
545  }
546  agg_col_idx = advance_slot(
547  agg_col_idx, agg_info, result_set_->separate_varlen_storage_valid_);
548  }
549  CHECK_EQ(offsets_for_storage_[storage_idx].size(),
550  result_set_->storage_->targets_.size());
551  }
552 }
553 
555  const int8_t* buff,
556  const size_t entry_idx,
557  const size_t target_logical_idx,
558  const StorageLookupResult& storage_lookup_result) const {
559  const size_t storage_idx = storage_lookup_result.storage_idx;
560 
561  CHECK_LT(storage_idx, offsets_for_storage_.size());
562  CHECK_LT(target_logical_idx, offsets_for_storage_[storage_idx].size());
563 
564  const auto& offsets_for_target = offsets_for_storage_[storage_idx][target_logical_idx];
565  const auto& agg_info = result_set_->storage_->targets_[target_logical_idx];
566  const auto& type_info = agg_info.sql_type;
567  auto ptr1 = offsets_for_target.ptr1;
568  if (result_set_->query_mem_desc_.targetGroupbyIndicesSize() > 0) {
569  if (result_set_->query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) >= 0) {
570  ptr1 =
571  buff + result_set_->query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) *
572  result_set_->query_mem_desc_.getEffectiveKeyWidth() *
573  result_set_->query_mem_desc_.entry_count_;
574  }
575  }
576 
577  const auto i1 = result_set_->lazyReadInt(
579  columnar_elem_ptr(entry_idx, ptr1, offsets_for_target.compact_sz1),
580  offsets_for_target.compact_sz1),
581  target_logical_idx,
582  storage_lookup_result);
583  if (agg_info.is_agg && agg_info.agg_kind == kAVG) {
584  CHECK(offsets_for_target.ptr2);
585  const auto i2 = read_int_from_buff(
587  entry_idx, offsets_for_target.ptr2, offsets_for_target.compact_sz2),
588  offsets_for_target.compact_sz2);
589  return InternalTargetValue(i1, i2);
590  } else {
591  // for TEXT ENCODING NONE:
592  if (type_info.is_string() && type_info.get_compression() == kENCODING_NONE) {
593  CHECK(!agg_info.is_agg);
594  if (!result_set_->lazy_fetch_info_.empty()) {
595  CHECK_LT(target_logical_idx, result_set_->lazy_fetch_info_.size());
596  const auto& col_lazy_fetch = result_set_->lazy_fetch_info_[target_logical_idx];
597  if (col_lazy_fetch.is_lazily_fetched) {
598  return InternalTargetValue(reinterpret_cast<const std::string*>(i1));
599  }
600  }
601  if (result_set_->separate_varlen_storage_valid_) {
602  if (i1 < 0) {
603  CHECK_EQ(-1, i1);
604  return InternalTargetValue(static_cast<const std::string*>(nullptr));
605  }
606  CHECK_LT(storage_lookup_result.storage_idx,
607  result_set_->serialized_varlen_buffer_.size());
608  const auto& varlen_buffer_for_fragment =
609  result_set_->serialized_varlen_buffer_[storage_lookup_result.storage_idx];
610  CHECK_LT(static_cast<size_t>(i1), varlen_buffer_for_fragment.size());
611  return InternalTargetValue(&varlen_buffer_for_fragment[i1]);
612  }
613  CHECK(offsets_for_target.ptr2);
614  const auto i2 = read_int_from_buff(
616  entry_idx, offsets_for_target.ptr2, offsets_for_target.compact_sz2),
617  offsets_for_target.compact_sz2);
618  CHECK_GE(i2, 0);
619  return result_set_->getVarlenOrderEntry(i1, i2);
620  }
621  return InternalTargetValue(
622  type_info.is_fp() ? i1 : int_resize_cast(i1, type_info.get_logical_size()));
623  }
624 }
625 
627  const size_t str_len) const {
628  char* host_str_ptr{nullptr};
629  std::vector<int8_t> cpu_buffer;
631  cpu_buffer.resize(str_len);
632  const auto executor = query_mem_desc_.getExecutor();
633  CHECK(executor);
634  auto& data_mgr = executor->catalog_->getDataMgr();
635  copy_from_gpu(&data_mgr,
636  &cpu_buffer[0],
637  static_cast<CUdeviceptr>(str_ptr),
638  str_len,
639  device_id_);
640  host_str_ptr = reinterpret_cast<char*>(&cpu_buffer[0]);
641  } else {
643  host_str_ptr = reinterpret_cast<char*>(str_ptr);
644  }
645  std::string str(host_str_ptr, str_len);
646  return InternalTargetValue(row_set_mem_owner_->addString(str));
647 }
648 
649 int64_t ResultSet::lazyReadInt(const int64_t ival,
650  const size_t target_logical_idx,
651  const StorageLookupResult& storage_lookup_result) const {
652  if (!lazy_fetch_info_.empty()) {
653  CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
654  const auto& col_lazy_fetch = lazy_fetch_info_[target_logical_idx];
655  if (col_lazy_fetch.is_lazily_fetched) {
656  CHECK_LT(static_cast<size_t>(storage_lookup_result.storage_idx),
657  col_buffers_.size());
658  int64_t ival_copy = ival;
659  auto& frag_col_buffers =
660  getColumnFrag(static_cast<size_t>(storage_lookup_result.storage_idx),
661  target_logical_idx,
662  ival_copy);
663  auto& frag_col_buffer = frag_col_buffers[col_lazy_fetch.local_col_id];
664  CHECK_LT(target_logical_idx, targets_.size());
665  const TargetInfo& target_info = targets_[target_logical_idx];
666  CHECK(!target_info.is_agg);
667  if (target_info.sql_type.is_string() &&
668  target_info.sql_type.get_compression() == kENCODING_NONE) {
669  VarlenDatum vd;
670  bool is_end{false};
672  reinterpret_cast<ChunkIter*>(const_cast<int8_t*>(frag_col_buffer)),
673  storage_lookup_result.fixedup_entry_idx,
674  false,
675  &vd,
676  &is_end);
677  CHECK(!is_end);
678  if (vd.is_null) {
679  return 0;
680  }
681  std::string fetched_str(reinterpret_cast<char*>(vd.pointer), vd.length);
682  return reinterpret_cast<int64_t>(row_set_mem_owner_->addString(fetched_str));
683  }
684  return result_set::lazy_decode(col_lazy_fetch, frag_col_buffer, ival_copy);
685  }
686  }
687  return ival;
688 }
689 
690 // Not all entries in the buffer represent a valid row. Advance the internal cursor
691 // used for the getNextRow method to the next row which is valid.
694  iter.global_entry_idx_valid_ = false;
695  return;
696  }
697 
698  while (iter.crt_row_buff_idx_ < entryCount()) {
699  const auto entry_idx = permutation_.empty() ? iter.crt_row_buff_idx_
701  const auto storage_lookup_result = findStorage(entry_idx);
702  const auto storage = storage_lookup_result.storage_ptr;
703  const auto fixedup_entry_idx = storage_lookup_result.fixedup_entry_idx;
704  if (!storage->isEmptyEntry(fixedup_entry_idx)) {
705  if (iter.fetched_so_far_ < drop_first_) {
706  ++iter.fetched_so_far_;
707  } else {
708  break;
709  }
710  }
711  ++iter.crt_row_buff_idx_;
712  }
713  if (permutation_.empty()) {
715  } else {
717  iter.global_entry_idx_ = iter.crt_row_buff_idx_ == permutation_.size()
718  ? iter.crt_row_buff_idx_
720  }
721 
723 
724  if (iter.global_entry_idx_valid_) {
725  ++iter.crt_row_buff_idx_;
726  ++iter.fetched_so_far_;
727  }
728 }
729 
730 // Not all entries in the buffer represent a valid row. Advance the internal cursor
731 // used for the getNextRow method to the next row which is valid.
733  while (crt_row_buff_idx_ < entryCount()) {
734  const auto entry_idx =
736  const auto storage_lookup_result = findStorage(entry_idx);
737  const auto storage = storage_lookup_result.storage_ptr;
738  const auto fixedup_entry_idx = storage_lookup_result.fixedup_entry_idx;
739  if (!storage->isEmptyEntry(fixedup_entry_idx)) {
740  break;
741  }
743  }
744  if (permutation_.empty()) {
745  return crt_row_buff_idx_;
746  }
748  return crt_row_buff_idx_ == permutation_.size() ? crt_row_buff_idx_
750 }
751 
752 size_t ResultSet::entryCount() const {
753  return permutation_.empty() ? query_mem_desc_.getEntryCount() : permutation_.size();
754 }
755 
756 size_t ResultSet::getBufferSizeBytes(const ExecutorDeviceType device_type) const {
757  CHECK(storage_);
758  return storage_->query_mem_desc_.getBufferSizeBytes(device_type);
759 }
760 
761 namespace {
762 
763 template <class T>
765  return ScalarTargetValue(static_cast<int64_t>(val));
766 }
767 
768 template <>
770  return ScalarTargetValue(val);
771 }
772 
773 template <>
775  return ScalarTargetValue(val);
776 }
777 
778 template <class T>
780  const int8_t* buff,
781  const size_t buff_sz,
782  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner) {
783  std::vector<ScalarTargetValue> values;
784  auto buff_elems = reinterpret_cast<const T*>(buff);
785  CHECK_EQ(size_t(0), buff_sz % sizeof(T));
786  const size_t num_elems = buff_sz / sizeof(T);
787  for (size_t i = 0; i < num_elems; ++i) {
788  values.push_back(make_scalar_tv<T>(buff_elems[i]));
789  }
790  return ArrayTargetValue(values);
791 }
792 
794  const int32_t* buff,
795  const size_t buff_sz,
796  const int dict_id,
797  const bool translate_strings,
798  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
799  const Catalog_Namespace::Catalog* catalog) {
800  std::vector<ScalarTargetValue> values;
801  CHECK_EQ(size_t(0), buff_sz % sizeof(int32_t));
802  const size_t num_elems = buff_sz / sizeof(int32_t);
803  if (translate_strings) {
804  for (size_t i = 0; i < num_elems; ++i) {
805  const auto string_id = buff[i];
806 
807  if (string_id == NULL_INT) {
808  values.emplace_back(NullableString(nullptr));
809  } else {
810  if (dict_id == 0) {
811  StringDictionaryProxy* sdp = row_set_mem_owner->getLiteralStringDictProxy();
812  values.emplace_back(sdp->getString(string_id));
813  } else {
814  values.emplace_back(NullableString(
815  row_set_mem_owner
816  ->getOrAddStringDictProxy(dict_id, /*with_generation=*/false, catalog)
817  ->getString(string_id)));
818  }
819  }
820  }
821  } else {
822  for (size_t i = 0; i < num_elems; i++) {
823  values.emplace_back(static_cast<int64_t>(buff[i]));
824  }
825  }
826  return ArrayTargetValue(values);
827 }
828 
830  const int8_t* buff,
831  const size_t buff_sz,
832  const bool translate_strings,
833  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
834  const Catalog_Namespace::Catalog* catalog) {
835  CHECK(array_ti.is_array());
836  const auto& elem_ti = array_ti.get_elem_type();
837  if (elem_ti.is_string()) {
838  return build_string_array_target_value(reinterpret_cast<const int32_t*>(buff),
839  buff_sz,
840  elem_ti.get_comp_param(),
841  translate_strings,
842  row_set_mem_owner,
843  catalog);
844  }
845  switch (elem_ti.get_size()) {
846  case 1:
847  return build_array_target_value<int8_t>(buff, buff_sz, row_set_mem_owner);
848  case 2:
849  return build_array_target_value<int16_t>(buff, buff_sz, row_set_mem_owner);
850  case 4:
851  if (elem_ti.is_fp()) {
852  return build_array_target_value<float>(buff, buff_sz, row_set_mem_owner);
853  } else {
854  return build_array_target_value<int32_t>(buff, buff_sz, row_set_mem_owner);
855  }
856  case 8:
857  if (elem_ti.is_fp()) {
858  return build_array_target_value<double>(buff, buff_sz, row_set_mem_owner);
859  } else {
860  return build_array_target_value<int64_t>(buff, buff_sz, row_set_mem_owner);
861  }
862  default:
863  CHECK(false);
864  }
865  CHECK(false);
866  return TargetValue(nullptr);
867 }
868 
869 template <class Tuple, size_t... indices>
870 inline std::vector<std::pair<const int8_t*, const int64_t>> make_vals_vector(
871  std::index_sequence<indices...>,
872  const Tuple& tuple) {
873  return std::vector<std::pair<const int8_t*, const int64_t>>{
874  std::make_pair(std::get<2 * indices>(tuple), std::get<2 * indices + 1>(tuple))...};
875 }
876 
877 inline std::unique_ptr<ArrayDatum> lazy_fetch_chunk(const int8_t* ptr,
878  const int64_t varlen_ptr) {
879  auto ad = std::make_unique<ArrayDatum>();
880  bool is_end;
881  ChunkIter_get_nth(reinterpret_cast<ChunkIter*>(const_cast<int8_t*>(ptr)),
882  varlen_ptr,
883  ad.get(),
884  &is_end);
885  CHECK(!is_end);
886  return ad;
887 }
888 
890  template <typename... T>
891  static inline auto fetch(const SQLTypeInfo& geo_ti,
892  const ResultSet::GeoReturnType return_type,
893  T&&... vals) {
894  constexpr int num_vals = sizeof...(vals);
895  static_assert(
896  num_vals % 2 == 0,
897  "Must have consistent pointer/size pairs for lazy fetch of geo target values.");
898  const auto vals_vector = make_vals_vector(std::make_index_sequence<num_vals / 2>{},
899  std::make_tuple(vals...));
900  std::array<VarlenDatumPtr, num_vals / 2> ad_arr;
901  size_t ctr = 0;
902  for (const auto& col_pair : vals_vector) {
903  ad_arr[ctr] = lazy_fetch_chunk(col_pair.first, col_pair.second);
904  // Regular chunk iterator used to fetch this datum sets the right nullness.
905  // That includes the fixlen bounds array.
906  // However it may incorrectly set it for the POINT coord array datum
907  // if 1st byte happened to hold NULL_ARRAY_TINYINT. One should either use
908  // the specialized iterator for POINT coords or rely on regular iterator +
909  // reset + recheck, which is what is done below.
910  auto is_point = (geo_ti.get_type() == kPOINT && ctr == 0);
911  if (is_point) {
912  // Resetting POINT coords array nullness here
913  ad_arr[ctr]->is_null = false;
914  }
915  if (!geo_ti.get_notnull()) {
916  // Recheck and set nullness
917  if (ad_arr[ctr]->length == 0 || ad_arr[ctr]->pointer == NULL ||
918  (is_point &&
919  is_null_point(geo_ti, ad_arr[ctr]->pointer, ad_arr[ctr]->length))) {
920  ad_arr[ctr]->is_null = true;
921  }
922  }
923  ctr++;
924  }
925  return ad_arr;
926  }
927 };
928 
929 inline std::unique_ptr<ArrayDatum> fetch_data_from_gpu(int64_t varlen_ptr,
930  const int64_t length,
931  Data_Namespace::DataMgr* data_mgr,
932  const int device_id) {
933  auto cpu_buf = std::shared_ptr<int8_t>(new int8_t[length], FreeDeleter());
935  data_mgr, cpu_buf.get(), static_cast<CUdeviceptr>(varlen_ptr), length, device_id);
936  // Just fetching the data from gpu, not checking geo nullness
937  return std::make_unique<ArrayDatum>(length, cpu_buf, false);
938 }
939 
941  static inline auto yieldGpuPtrFetcher() {
942  return [](const int64_t ptr, const int64_t length) -> VarlenDatumPtr {
943  // Just fetching the data from gpu, not checking geo nullness
944  return std::make_unique<VarlenDatum>(length, reinterpret_cast<int8_t*>(ptr), false);
945  };
946  }
947 
948  static inline auto yieldGpuDatumFetcher(Data_Namespace::DataMgr* data_mgr_ptr,
949  const int device_id) {
950  return [data_mgr_ptr, device_id](const int64_t ptr,
951  const int64_t length) -> VarlenDatumPtr {
952  return fetch_data_from_gpu(ptr, length, data_mgr_ptr, device_id);
953  };
954  }
955 
956  static inline auto yieldCpuDatumFetcher() {
957  return [](const int64_t ptr, const int64_t length) -> VarlenDatumPtr {
958  // Just fetching the data from gpu, not checking geo nullness
959  return std::make_unique<VarlenDatum>(length, reinterpret_cast<int8_t*>(ptr), false);
960  };
961  }
962 
963  template <typename... T>
964  static inline auto fetch(const SQLTypeInfo& geo_ti,
965  const ResultSet::GeoReturnType return_type,
966  Data_Namespace::DataMgr* data_mgr,
967  const bool fetch_data_from_gpu,
968  const int device_id,
969  T&&... vals) {
970  auto ad_arr_generator = [&](auto datum_fetcher) {
971  constexpr int num_vals = sizeof...(vals);
972  static_assert(
973  num_vals % 2 == 0,
974  "Must have consistent pointer/size pairs for lazy fetch of geo target values.");
975  const auto vals_vector = std::vector<int64_t>{vals...};
976 
977  std::array<VarlenDatumPtr, num_vals / 2> ad_arr;
978  size_t ctr = 0;
979  for (size_t i = 0; i < vals_vector.size(); i += 2) {
980  ad_arr[ctr] = datum_fetcher(vals_vector[i], vals_vector[i + 1]);
981  // All fetched datums come in with is_null set to false
982  if (!geo_ti.get_notnull()) {
983  bool is_null = false;
984  // Now need to set the nullness
985  if (ad_arr[ctr]->length == 0 || ad_arr[ctr]->pointer == NULL) {
986  is_null = true;
987  } else if (geo_ti.get_type() == kPOINT && ctr == 0 &&
988  is_null_point(geo_ti, ad_arr[ctr]->pointer, ad_arr[ctr]->length)) {
989  is_null = true; // recognizes compressed and uncompressed points
990  } else if (ad_arr[ctr]->length == 4 * sizeof(double)) {
991  // Bounds
992  auto dti = SQLTypeInfo(kARRAY, 0, 0, false, kENCODING_NONE, 0, kDOUBLE);
993  is_null = dti.is_null_fixlen_array(ad_arr[ctr]->pointer, ad_arr[ctr]->length);
994  }
995  ad_arr[ctr]->is_null = is_null;
996  }
997  ctr++;
998  }
999  return ad_arr;
1000  };
1001 
1002  if (fetch_data_from_gpu) {
1004  return ad_arr_generator(yieldGpuPtrFetcher());
1005  } else {
1006  return ad_arr_generator(yieldGpuDatumFetcher(data_mgr, device_id));
1007  }
1008  } else {
1009  return ad_arr_generator(yieldCpuDatumFetcher());
1010  }
1011  }
1012 };
1013 
1014 template <SQLTypes GEO_SOURCE_TYPE, typename GeoTargetFetcher>
1016  template <typename... T>
1017  static inline TargetValue build(const SQLTypeInfo& geo_ti,
1018  const ResultSet::GeoReturnType return_type,
1019  T&&... vals) {
1020  auto ad_arr = GeoTargetFetcher::fetch(geo_ti, return_type, std::forward<T>(vals)...);
1021  static_assert(std::tuple_size<decltype(ad_arr)>::value > 0,
1022  "ArrayDatum array for Geo Target must contain at least one value.");
1023 
1024  // Fetcher sets the geo nullness based on geo typeinfo's notnull, type and
1025  // compression. Serializers will generate appropriate NULL geo where necessary.
1026  switch (return_type) {
1028  if (!geo_ti.get_notnull() && ad_arr[0]->is_null) {
1029  return GeoTargetValue();
1030  }
1032  GEO_SOURCE_TYPE>::GeoSerializerType::serialize(geo_ti,
1033  ad_arr);
1034  }
1036  if (!geo_ti.get_notnull() && ad_arr[0]->is_null) {
1037  // Generating NULL wkt string to represent NULL geo
1038  return NullableString(nullptr);
1039  }
1041  GEO_SOURCE_TYPE>::GeoSerializerType::serialize(geo_ti,
1042  ad_arr);
1043  }
1046  if (!geo_ti.get_notnull() && ad_arr[0]->is_null) {
1047  // NULL geo
1048  // Pass along null datum, instead of an empty/null GeoTargetValuePtr
1049  // return GeoTargetValuePtr();
1050  }
1052  GEO_SOURCE_TYPE>::GeoSerializerType::serialize(geo_ti,
1053  ad_arr);
1054  }
1055  default: {
1056  UNREACHABLE();
1057  return TargetValue(nullptr);
1058  }
1059  }
1060  }
1061 };
1062 
1063 template <typename T>
1064 inline std::pair<int64_t, int64_t> get_frag_id_and_local_idx(
1065  const std::vector<std::vector<T>>& frag_offsets,
1066  const size_t tab_or_col_idx,
1067  const int64_t global_idx) {
1068  CHECK_GE(global_idx, int64_t(0));
1069  for (int64_t frag_id = frag_offsets.size() - 1; frag_id > 0; --frag_id) {
1070  CHECK_LT(tab_or_col_idx, frag_offsets[frag_id].size());
1071  const auto frag_off = static_cast<int64_t>(frag_offsets[frag_id][tab_or_col_idx]);
1072  if (frag_off < global_idx) {
1073  return {frag_id, global_idx - frag_off};
1074  }
1075  }
1076  return {-1, -1};
1077 }
1078 
1079 } // namespace
1080 
1081 const std::vector<const int8_t*>& ResultSet::getColumnFrag(const size_t storage_idx,
1082  const size_t col_logical_idx,
1083  int64_t& global_idx) const {
1084  CHECK_LT(static_cast<size_t>(storage_idx), col_buffers_.size());
1085  if (col_buffers_[storage_idx].size() > 1) {
1086  int64_t frag_id = 0;
1087  int64_t local_idx = global_idx;
1088  if (consistent_frag_sizes_[storage_idx][col_logical_idx] != -1) {
1089  frag_id = global_idx / consistent_frag_sizes_[storage_idx][col_logical_idx];
1090  local_idx = global_idx % consistent_frag_sizes_[storage_idx][col_logical_idx];
1091  } else {
1092  std::tie(frag_id, local_idx) = get_frag_id_and_local_idx(
1093  frag_offsets_[storage_idx], col_logical_idx, global_idx);
1094  CHECK_LE(local_idx, global_idx);
1095  }
1096  CHECK_GE(frag_id, int64_t(0));
1097  CHECK_LT(static_cast<size_t>(frag_id), col_buffers_[storage_idx].size());
1098  global_idx = local_idx;
1099  return col_buffers_[storage_idx][frag_id];
1100  } else {
1101  CHECK_EQ(size_t(1), col_buffers_[storage_idx].size());
1102  return col_buffers_[storage_idx][0];
1103  }
1104 }
1105 
1110 void ResultSet::copyColumnIntoBuffer(const size_t column_idx,
1111  int8_t* output_buffer,
1112  const size_t output_buffer_size) const {
1114  CHECK_LT(column_idx, query_mem_desc_.getSlotCount());
1115  CHECK(output_buffer_size > 0);
1116  CHECK(output_buffer);
1117  const auto column_width_size = query_mem_desc_.getPaddedSlotWidthBytes(column_idx);
1118  size_t out_buff_offset = 0;
1119 
1120  // the main storage:
1121  const size_t crt_storage_row_count = storage_->query_mem_desc_.getEntryCount();
1122  const size_t crt_buffer_size = crt_storage_row_count * column_width_size;
1123  const size_t column_offset = storage_->query_mem_desc_.getColOffInBytes(column_idx);
1124  const int8_t* storage_buffer = storage_->getUnderlyingBuffer() + column_offset;
1125  CHECK(crt_buffer_size <= output_buffer_size);
1126  std::memcpy(output_buffer, storage_buffer, crt_buffer_size);
1127 
1128  out_buff_offset += crt_buffer_size;
1129 
1130  // the appended storages:
1131  for (size_t i = 0; i < appended_storage_.size(); i++) {
1132  const size_t crt_storage_row_count =
1133  appended_storage_[i]->query_mem_desc_.getEntryCount();
1134  if (crt_storage_row_count == 0) {
1135  // skip an empty appended storage
1136  continue;
1137  }
1138  CHECK_LT(out_buff_offset, output_buffer_size);
1139  const size_t crt_buffer_size = crt_storage_row_count * column_width_size;
1140  const size_t column_offset =
1141  appended_storage_[i]->query_mem_desc_.getColOffInBytes(column_idx);
1142  const int8_t* storage_buffer =
1143  appended_storage_[i]->getUnderlyingBuffer() + column_offset;
1144  CHECK(out_buff_offset + crt_buffer_size <= output_buffer_size);
1145  std::memcpy(output_buffer + out_buff_offset, storage_buffer, crt_buffer_size);
1146 
1147  out_buff_offset += crt_buffer_size;
1148  }
1149 }
1150 
1151 template <typename ENTRY_TYPE, QueryDescriptionType QUERY_TYPE, bool COLUMNAR_FORMAT>
1152 ENTRY_TYPE ResultSet::getEntryAt(const size_t row_idx,
1153  const size_t target_idx,
1154  const size_t slot_idx) const {
1155  if constexpr (QUERY_TYPE == QueryDescriptionType::GroupByPerfectHash) { // NOLINT
1156  if constexpr (COLUMNAR_FORMAT) { // NOLINT
1157  return getColumnarPerfectHashEntryAt<ENTRY_TYPE>(row_idx, target_idx, slot_idx);
1158  } else {
1159  return getRowWisePerfectHashEntryAt<ENTRY_TYPE>(row_idx, target_idx, slot_idx);
1160  }
1161  } else if constexpr (QUERY_TYPE == QueryDescriptionType::GroupByBaselineHash) {
1162  if constexpr (COLUMNAR_FORMAT) { // NOLINT
1163  return getColumnarBaselineEntryAt<ENTRY_TYPE>(row_idx, target_idx, slot_idx);
1164  } else {
1165  return getRowWiseBaselineEntryAt<ENTRY_TYPE>(row_idx, target_idx, slot_idx);
1166  }
1167  } else {
1168  UNREACHABLE() << "Invalid query type is used";
1169  return 0;
1170  }
1171 }
1172 
1173 #define DEF_GET_ENTRY_AT(query_type, columnar_output) \
1174  template DATA_T ResultSet::getEntryAt<DATA_T, query_type, columnar_output>( \
1175  const size_t row_idx, const size_t target_idx, const size_t slot_idx) const;
1176 
1177 #define DATA_T int64_t
1181 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1182 #undef DATA_T
1183 
1184 #define DATA_T int32_t
1186 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1187 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1188 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1189 #undef DATA_T
1190 
1191 #define DATA_T int16_t
1193 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1194 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1195 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1196 #undef DATA_T
1197 
1198 #define DATA_T int8_t
1200 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1201 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1202 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1203 #undef DATA_T
1204 
1205 #define DATA_T float
1207 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1208 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1209 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1210 #undef DATA_T
1211 
1212 #define DATA_T double
1214 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1215 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1216 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1217 #undef DATA_T
1218 
1219 #undef DEF_GET_ENTRY_AT
1220 
1227 template <typename ENTRY_TYPE>
1228 ENTRY_TYPE ResultSet::getColumnarPerfectHashEntryAt(const size_t row_idx,
1229  const size_t target_idx,
1230  const size_t slot_idx) const {
1231  const size_t column_offset = storage_->query_mem_desc_.getColOffInBytes(slot_idx);
1232  const int8_t* storage_buffer = storage_->getUnderlyingBuffer() + column_offset;
1233  return reinterpret_cast<const ENTRY_TYPE*>(storage_buffer)[row_idx];
1234 }
1235 
1242 template <typename ENTRY_TYPE>
1243 ENTRY_TYPE ResultSet::getRowWisePerfectHashEntryAt(const size_t row_idx,
1244  const size_t target_idx,
1245  const size_t slot_idx) const {
1246  const size_t row_offset = storage_->query_mem_desc_.getRowSize() * row_idx;
1247  const size_t column_offset = storage_->query_mem_desc_.getColOffInBytes(slot_idx);
1248  const int8_t* storage_buffer =
1249  storage_->getUnderlyingBuffer() + row_offset + column_offset;
1250  return *reinterpret_cast<const ENTRY_TYPE*>(storage_buffer);
1251 }
1252 
1259 template <typename ENTRY_TYPE>
1260 ENTRY_TYPE ResultSet::getRowWiseBaselineEntryAt(const size_t row_idx,
1261  const size_t target_idx,
1262  const size_t slot_idx) const {
1263  CHECK_NE(storage_->query_mem_desc_.targetGroupbyIndicesSize(), size_t(0));
1264  const auto key_width = storage_->query_mem_desc_.getEffectiveKeyWidth();
1265  auto keys_ptr = row_ptr_rowwise(
1266  storage_->getUnderlyingBuffer(), storage_->query_mem_desc_, row_idx);
1267  const auto column_offset =
1268  (storage_->query_mem_desc_.getTargetGroupbyIndex(target_idx) < 0)
1269  ? storage_->query_mem_desc_.getColOffInBytes(slot_idx)
1270  : storage_->query_mem_desc_.getTargetGroupbyIndex(target_idx) * key_width;
1271  const auto storage_buffer = keys_ptr + column_offset;
1272  return *reinterpret_cast<const ENTRY_TYPE*>(storage_buffer);
1273 }
1274 
1281 template <typename ENTRY_TYPE>
1282 ENTRY_TYPE ResultSet::getColumnarBaselineEntryAt(const size_t row_idx,
1283  const size_t target_idx,
1284  const size_t slot_idx) const {
1285  CHECK_NE(storage_->query_mem_desc_.targetGroupbyIndicesSize(), size_t(0));
1286  const auto key_width = storage_->query_mem_desc_.getEffectiveKeyWidth();
1287  const auto column_offset =
1288  (storage_->query_mem_desc_.getTargetGroupbyIndex(target_idx) < 0)
1289  ? storage_->query_mem_desc_.getColOffInBytes(slot_idx)
1290  : storage_->query_mem_desc_.getTargetGroupbyIndex(target_idx) * key_width *
1291  storage_->query_mem_desc_.getEntryCount();
1292  const auto column_buffer = storage_->getUnderlyingBuffer() + column_offset;
1293  return reinterpret_cast<const ENTRY_TYPE*>(column_buffer)[row_idx];
1294 }
1295 
1296 // Interprets ptr1, ptr2 as the ptr and len pair used for variable length data.
1298  const int8_t compact_sz1,
1299  const int8_t* ptr2,
1300  const int8_t compact_sz2,
1301  const TargetInfo& target_info,
1302  const size_t target_logical_idx,
1303  const bool translate_strings,
1304  const size_t entry_buff_idx) const {
1305  auto varlen_ptr = read_int_from_buff(ptr1, compact_sz1);
1306  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1307  if (varlen_ptr < 0) {
1308  CHECK_EQ(-1, varlen_ptr);
1309  if (target_info.sql_type.get_type() == kARRAY) {
1310  return ArrayTargetValue(boost::optional<std::vector<ScalarTargetValue>>{});
1311  }
1312  return TargetValue(nullptr);
1313  }
1314  const auto storage_idx = getStorageIndex(entry_buff_idx);
1315  if (target_info.sql_type.is_string()) {
1316  CHECK(target_info.sql_type.get_compression() == kENCODING_NONE);
1317  CHECK_LT(storage_idx.first, serialized_varlen_buffer_.size());
1318  const auto& varlen_buffer_for_storage =
1319  serialized_varlen_buffer_[storage_idx.first];
1320  CHECK_LT(static_cast<size_t>(varlen_ptr), varlen_buffer_for_storage.size());
1321  return varlen_buffer_for_storage[varlen_ptr];
1322  } else if (target_info.sql_type.get_type() == kARRAY) {
1323  CHECK_LT(storage_idx.first, serialized_varlen_buffer_.size());
1324  const auto& varlen_buffer = serialized_varlen_buffer_[storage_idx.first];
1325  CHECK_LT(static_cast<size_t>(varlen_ptr), varlen_buffer.size());
1326 
1327  return build_array_target_value(
1328  target_info.sql_type,
1329  reinterpret_cast<const int8_t*>(varlen_buffer[varlen_ptr].data()),
1330  varlen_buffer[varlen_ptr].size(),
1331  translate_strings,
1333  catalog_);
1334  } else {
1335  CHECK(false);
1336  }
1337  }
1338  if (!lazy_fetch_info_.empty()) {
1339  CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
1340  const auto& col_lazy_fetch = lazy_fetch_info_[target_logical_idx];
1341  if (col_lazy_fetch.is_lazily_fetched) {
1342  const auto storage_idx = getStorageIndex(entry_buff_idx);
1343  CHECK_LT(storage_idx.first, col_buffers_.size());
1344  auto& frag_col_buffers =
1345  getColumnFrag(storage_idx.first, target_logical_idx, varlen_ptr);
1346  bool is_end{false};
1347  if (target_info.sql_type.is_string()) {
1348  VarlenDatum vd;
1349  ChunkIter_get_nth(reinterpret_cast<ChunkIter*>(const_cast<int8_t*>(
1350  frag_col_buffers[col_lazy_fetch.local_col_id])),
1351  varlen_ptr,
1352  false,
1353  &vd,
1354  &is_end);
1355  CHECK(!is_end);
1356  if (vd.is_null) {
1357  return TargetValue(nullptr);
1358  }
1359  CHECK(vd.pointer);
1360  CHECK_GT(vd.length, 0u);
1361  std::string fetched_str(reinterpret_cast<char*>(vd.pointer), vd.length);
1362  return fetched_str;
1363  } else {
1364  CHECK(target_info.sql_type.is_array());
1365  ArrayDatum ad;
1366  ChunkIter_get_nth(reinterpret_cast<ChunkIter*>(const_cast<int8_t*>(
1367  frag_col_buffers[col_lazy_fetch.local_col_id])),
1368  varlen_ptr,
1369  &ad,
1370  &is_end);
1371  CHECK(!is_end);
1372  if (ad.is_null) {
1373  return ArrayTargetValue(boost::optional<std::vector<ScalarTargetValue>>{});
1374  }
1375  CHECK_GE(ad.length, 0u);
1376  if (ad.length > 0) {
1377  CHECK(ad.pointer);
1378  }
1379  return build_array_target_value(target_info.sql_type,
1380  ad.pointer,
1381  ad.length,
1382  translate_strings,
1384  catalog_);
1385  }
1386  }
1387  }
1388  if (!varlen_ptr) {
1389  if (target_info.sql_type.is_array()) {
1390  return ArrayTargetValue(boost::optional<std::vector<ScalarTargetValue>>{});
1391  }
1392  return TargetValue(nullptr);
1393  }
1394  auto length = read_int_from_buff(ptr2, compact_sz2);
1395  if (target_info.sql_type.is_array()) {
1396  const auto& elem_ti = target_info.sql_type.get_elem_type();
1397  length *= elem_ti.get_array_context_logical_size();
1398  }
1399  std::vector<int8_t> cpu_buffer;
1400  if (varlen_ptr && device_type_ == ExecutorDeviceType::GPU) {
1401  cpu_buffer.resize(length);
1402  const auto executor = query_mem_desc_.getExecutor();
1403  CHECK(executor);
1404  auto& data_mgr = executor->catalog_->getDataMgr();
1405  copy_from_gpu(&data_mgr,
1406  &cpu_buffer[0],
1407  static_cast<CUdeviceptr>(varlen_ptr),
1408  length,
1409  device_id_);
1410  varlen_ptr = reinterpret_cast<int64_t>(&cpu_buffer[0]);
1411  }
1412  if (target_info.sql_type.is_array()) {
1413  return build_array_target_value(target_info.sql_type,
1414  reinterpret_cast<const int8_t*>(varlen_ptr),
1415  length,
1416  translate_strings,
1418  catalog_);
1419  }
1420  return std::string(reinterpret_cast<char*>(varlen_ptr), length);
1421 }
1422 
1423 bool ResultSet::isGeoColOnGpu(const size_t col_idx) const {
1424  // This should match the logic in makeGeoTargetValue which ultimately calls
1425  // fetch_data_from_gpu when the geo column is on the device.
1426  // TODO(croot): somehow find a way to refactor this and makeGeoTargetValue to use a
1427  // utility function that handles this logic in one place
1428  CHECK_LT(col_idx, targets_.size());
1429  if (!IS_GEO(targets_[col_idx].sql_type.get_type())) {
1430  throw std::runtime_error("Column target at index " + std::to_string(col_idx) +
1431  " is not a geo column. It is of type " +
1432  targets_[col_idx].sql_type.get_type_name() + ".");
1433  }
1434 
1435  const auto& target_info = targets_[col_idx];
1436  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1437  return false;
1438  }
1439 
1440  if (!lazy_fetch_info_.empty()) {
1441  CHECK_LT(col_idx, lazy_fetch_info_.size());
1442  if (lazy_fetch_info_[col_idx].is_lazily_fetched) {
1443  return false;
1444  }
1445  }
1446 
1448 }
1449 
1450 // Reads a geo value from a series of ptrs to var len types
1451 // In Columnar format, geo_target_ptr is the geo column ptr (a pointer to the beginning
1452 // of that specific geo column) and should be appropriately adjusted with the
1453 // entry_buff_idx
1454 TargetValue ResultSet::makeGeoTargetValue(const int8_t* geo_target_ptr,
1455  const size_t slot_idx,
1456  const TargetInfo& target_info,
1457  const size_t target_logical_idx,
1458  const size_t entry_buff_idx) const {
1459  CHECK(target_info.sql_type.is_geometry());
1460 
1461  auto getNextTargetBufferRowWise = [&](const size_t slot_idx, const size_t range) {
1462  return geo_target_ptr + query_mem_desc_.getPaddedColWidthForRange(slot_idx, range);
1463  };
1464 
1465  auto getNextTargetBufferColWise = [&](const size_t slot_idx, const size_t range) {
1466  const auto storage_info = findStorage(entry_buff_idx);
1467  auto crt_geo_col_ptr = geo_target_ptr;
1468  for (size_t i = slot_idx; i < slot_idx + range; i++) {
1469  crt_geo_col_ptr = advance_to_next_columnar_target_buff(
1470  crt_geo_col_ptr, storage_info.storage_ptr->query_mem_desc_, i);
1471  }
1472  // adjusting the column pointer to represent a pointer to the geo target value
1473  return crt_geo_col_ptr +
1474  storage_info.fixedup_entry_idx *
1475  storage_info.storage_ptr->query_mem_desc_.getPaddedSlotWidthBytes(
1476  slot_idx + range);
1477  };
1478 
1479  auto getNextTargetBuffer = [&](const size_t slot_idx, const size_t range) {
1481  ? getNextTargetBufferColWise(slot_idx, range)
1482  : getNextTargetBufferRowWise(slot_idx, range);
1483  };
1484 
1485  auto getCoordsDataPtr = [&](const int8_t* geo_target_ptr) {
1486  return read_int_from_buff(getNextTargetBuffer(slot_idx, 0),
1488  };
1489 
1490  auto getCoordsLength = [&](const int8_t* geo_target_ptr) {
1491  return read_int_from_buff(getNextTargetBuffer(slot_idx, 1),
1493  };
1494 
1495  auto getRingSizesPtr = [&](const int8_t* geo_target_ptr) {
1496  return read_int_from_buff(getNextTargetBuffer(slot_idx, 2),
1498  };
1499 
1500  auto getRingSizesLength = [&](const int8_t* geo_target_ptr) {
1501  return read_int_from_buff(getNextTargetBuffer(slot_idx, 3),
1503  };
1504 
1505  auto getPolyRingsPtr = [&](const int8_t* geo_target_ptr) {
1506  return read_int_from_buff(getNextTargetBuffer(slot_idx, 4),
1508  };
1509 
1510  auto getPolyRingsLength = [&](const int8_t* geo_target_ptr) {
1511  return read_int_from_buff(getNextTargetBuffer(slot_idx, 5),
1513  };
1514 
1515  auto getFragColBuffers = [&]() -> decltype(auto) {
1516  const auto storage_idx = getStorageIndex(entry_buff_idx);
1517  CHECK_LT(storage_idx.first, col_buffers_.size());
1518  auto global_idx = getCoordsDataPtr(geo_target_ptr);
1519  return getColumnFrag(storage_idx.first, target_logical_idx, global_idx);
1520  };
1521 
1522  const bool is_gpu_fetch = device_type_ == ExecutorDeviceType::GPU;
1523 
1524  auto getDataMgr = [&]() {
1525  auto executor = query_mem_desc_.getExecutor();
1526  CHECK(executor);
1527  auto& data_mgr = executor->catalog_->getDataMgr();
1528  return &data_mgr;
1529  };
1530 
1531  auto getSeparateVarlenStorage = [&]() -> decltype(auto) {
1532  const auto storage_idx = getStorageIndex(entry_buff_idx);
1533  CHECK_LT(storage_idx.first, serialized_varlen_buffer_.size());
1534  const auto& varlen_buffer = serialized_varlen_buffer_[storage_idx.first];
1535  return varlen_buffer;
1536  };
1537 
1538  if (separate_varlen_storage_valid_ && getCoordsDataPtr(geo_target_ptr) < 0) {
1539  CHECK_EQ(-1, getCoordsDataPtr(geo_target_ptr));
1540  return TargetValue(nullptr);
1541  }
1542 
1543  const ColumnLazyFetchInfo* col_lazy_fetch = nullptr;
1544  if (!lazy_fetch_info_.empty()) {
1545  CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
1546  col_lazy_fetch = &lazy_fetch_info_[target_logical_idx];
1547  }
1548 
1549  switch (target_info.sql_type.get_type()) {
1550  case kPOINT: {
1551  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1552  const auto& varlen_buffer = getSeparateVarlenStorage();
1553  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr)),
1554  varlen_buffer.size());
1555 
1556  return GeoTargetValueBuilder<kPOINT, GeoQueryOutputFetchHandler>::build(
1557  target_info.sql_type,
1559  nullptr,
1560  false,
1561  device_id_,
1562  reinterpret_cast<int64_t>(
1563  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1564  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()));
1565  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1566  const auto& frag_col_buffers = getFragColBuffers();
1567  return GeoTargetValueBuilder<kPOINT, GeoLazyFetchHandler>::build(
1568  target_info.sql_type,
1570  frag_col_buffers[col_lazy_fetch->local_col_id],
1571  getCoordsDataPtr(geo_target_ptr));
1572  } else {
1573  return GeoTargetValueBuilder<kPOINT, GeoQueryOutputFetchHandler>::build(
1574  target_info.sql_type,
1576  is_gpu_fetch ? getDataMgr() : nullptr,
1577  is_gpu_fetch,
1578  device_id_,
1579  getCoordsDataPtr(geo_target_ptr),
1580  getCoordsLength(geo_target_ptr));
1581  }
1582  break;
1583  }
1584  case kLINESTRING: {
1585  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1586  const auto& varlen_buffer = getSeparateVarlenStorage();
1587  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr)),
1588  varlen_buffer.size());
1589 
1590  return GeoTargetValueBuilder<kLINESTRING, GeoQueryOutputFetchHandler>::build(
1591  target_info.sql_type,
1593  nullptr,
1594  false,
1595  device_id_,
1596  reinterpret_cast<int64_t>(
1597  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1598  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()));
1599  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1600  const auto& frag_col_buffers = getFragColBuffers();
1601  return GeoTargetValueBuilder<kLINESTRING, GeoLazyFetchHandler>::build(
1602  target_info.sql_type,
1604  frag_col_buffers[col_lazy_fetch->local_col_id],
1605  getCoordsDataPtr(geo_target_ptr));
1606  } else {
1607  return GeoTargetValueBuilder<kLINESTRING, GeoQueryOutputFetchHandler>::build(
1608  target_info.sql_type,
1610  is_gpu_fetch ? getDataMgr() : nullptr,
1611  is_gpu_fetch,
1612  device_id_,
1613  getCoordsDataPtr(geo_target_ptr),
1614  getCoordsLength(geo_target_ptr));
1615  }
1616  break;
1617  }
1618  case kPOLYGON: {
1619  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1620  const auto& varlen_buffer = getSeparateVarlenStorage();
1621  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr) + 1),
1622  varlen_buffer.size());
1623 
1624  return GeoTargetValueBuilder<kPOLYGON, GeoQueryOutputFetchHandler>::build(
1625  target_info.sql_type,
1627  nullptr,
1628  false,
1629  device_id_,
1630  reinterpret_cast<int64_t>(
1631  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1632  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()),
1633  reinterpret_cast<int64_t>(
1634  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].data()),
1635  static_cast<int64_t>(
1636  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].size()));
1637  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1638  const auto& frag_col_buffers = getFragColBuffers();
1639 
1640  return GeoTargetValueBuilder<kPOLYGON, GeoLazyFetchHandler>::build(
1641  target_info.sql_type,
1643  frag_col_buffers[col_lazy_fetch->local_col_id],
1644  getCoordsDataPtr(geo_target_ptr),
1645  frag_col_buffers[col_lazy_fetch->local_col_id + 1],
1646  getCoordsDataPtr(geo_target_ptr));
1647  } else {
1648  return GeoTargetValueBuilder<kPOLYGON, GeoQueryOutputFetchHandler>::build(
1649  target_info.sql_type,
1651  is_gpu_fetch ? getDataMgr() : nullptr,
1652  is_gpu_fetch,
1653  device_id_,
1654  getCoordsDataPtr(geo_target_ptr),
1655  getCoordsLength(geo_target_ptr),
1656  getRingSizesPtr(geo_target_ptr),
1657  getRingSizesLength(geo_target_ptr) * 4);
1658  }
1659  break;
1660  }
1661  case kMULTIPOLYGON: {
1662  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1663  const auto& varlen_buffer = getSeparateVarlenStorage();
1664  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr) + 2),
1665  varlen_buffer.size());
1666 
1667  return GeoTargetValueBuilder<kMULTIPOLYGON, GeoQueryOutputFetchHandler>::build(
1668  target_info.sql_type,
1670  nullptr,
1671  false,
1672  device_id_,
1673  reinterpret_cast<int64_t>(
1674  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1675  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()),
1676  reinterpret_cast<int64_t>(
1677  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].data()),
1678  static_cast<int64_t>(
1679  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].size()),
1680  reinterpret_cast<int64_t>(
1681  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 2].data()),
1682  static_cast<int64_t>(
1683  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 2].size()));
1684  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1685  const auto& frag_col_buffers = getFragColBuffers();
1686 
1687  return GeoTargetValueBuilder<kMULTIPOLYGON, GeoLazyFetchHandler>::build(
1688  target_info.sql_type,
1690  frag_col_buffers[col_lazy_fetch->local_col_id],
1691  getCoordsDataPtr(geo_target_ptr),
1692  frag_col_buffers[col_lazy_fetch->local_col_id + 1],
1693  getCoordsDataPtr(geo_target_ptr),
1694  frag_col_buffers[col_lazy_fetch->local_col_id + 2],
1695  getCoordsDataPtr(geo_target_ptr));
1696  } else {
1697  return GeoTargetValueBuilder<kMULTIPOLYGON, GeoQueryOutputFetchHandler>::build(
1698  target_info.sql_type,
1700  is_gpu_fetch ? getDataMgr() : nullptr,
1701  is_gpu_fetch,
1702  device_id_,
1703  getCoordsDataPtr(geo_target_ptr),
1704  getCoordsLength(geo_target_ptr),
1705  getRingSizesPtr(geo_target_ptr),
1706  getRingSizesLength(geo_target_ptr) * 4,
1707  getPolyRingsPtr(geo_target_ptr),
1708  getPolyRingsLength(geo_target_ptr) * 4);
1709  }
1710  break;
1711  }
1712  default:
1713  throw std::runtime_error("Unknown Geometry type encountered: " +
1714  target_info.sql_type.get_type_name());
1715  }
1716  UNREACHABLE();
1717  return TargetValue(nullptr);
1718 }
1719 
1720 // Reads an integer or a float from ptr based on the type and the byte width.
1722  const int8_t compact_sz,
1723  const TargetInfo& target_info,
1724  const size_t target_logical_idx,
1725  const bool translate_strings,
1726  const bool decimal_to_double,
1727  const size_t entry_buff_idx) const {
1728  auto actual_compact_sz = compact_sz;
1729  const auto& type_info = target_info.sql_type;
1730  if (type_info.get_type() == kFLOAT && !query_mem_desc_.forceFourByteFloat()) {
1732  actual_compact_sz = sizeof(float);
1733  } else {
1734  actual_compact_sz = sizeof(double);
1735  }
1736  if (target_info.is_agg &&
1737  (target_info.agg_kind == kAVG || target_info.agg_kind == kSUM ||
1738  target_info.agg_kind == kMIN || target_info.agg_kind == kMAX ||
1739  target_info.agg_kind == kSINGLE_VALUE)) {
1740  // The above listed aggregates use two floats in a single 8-byte slot. Set the
1741  // padded size to 4 bytes to properly read each value.
1742  actual_compact_sz = sizeof(float);
1743  }
1744  }
1745  if (get_compact_type(target_info).is_date_in_days()) {
1746  // Dates encoded in days are converted to 8 byte values on read.
1747  actual_compact_sz = sizeof(int64_t);
1748  }
1749 
1750  // String dictionary keys are read as 32-bit values regardless of encoding
1751  if (type_info.is_string() && type_info.get_compression() == kENCODING_DICT &&
1752  type_info.get_comp_param()) {
1753  actual_compact_sz = sizeof(int32_t);
1754  }
1755 
1756  auto ival = read_int_from_buff(ptr, actual_compact_sz);
1757  const auto& chosen_type = get_compact_type(target_info);
1758  if (!lazy_fetch_info_.empty()) {
1759  CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
1760  const auto& col_lazy_fetch = lazy_fetch_info_[target_logical_idx];
1761  if (col_lazy_fetch.is_lazily_fetched) {
1762  CHECK_GE(ival, 0);
1763  const auto storage_idx = getStorageIndex(entry_buff_idx);
1764  CHECK_LT(storage_idx.first, col_buffers_.size());
1765  auto& frag_col_buffers = getColumnFrag(storage_idx.first, target_logical_idx, ival);
1766  CHECK_LT(size_t(col_lazy_fetch.local_col_id), frag_col_buffers.size());
1767  ival = result_set::lazy_decode(
1768  col_lazy_fetch, frag_col_buffers[col_lazy_fetch.local_col_id], ival);
1769  if (chosen_type.is_fp()) {
1770  const auto dval = *reinterpret_cast<const double*>(may_alias_ptr(&ival));
1771  if (chosen_type.get_type() == kFLOAT) {
1772  return ScalarTargetValue(static_cast<float>(dval));
1773  } else {
1774  return ScalarTargetValue(dval);
1775  }
1776  }
1777  }
1778  }
1779  if (chosen_type.is_fp()) {
1780  if (target_info.agg_kind == kAPPROX_MEDIAN) {
1781  return *reinterpret_cast<double const*>(ptr) == NULL_DOUBLE
1782  ? NULL_DOUBLE // sql_validate / just_validate
1783  : calculateQuantile(*reinterpret_cast<quantile::TDigest* const*>(ptr),
1784  0.5);
1785  }
1786  switch (actual_compact_sz) {
1787  case 8: {
1788  const auto dval = *reinterpret_cast<const double*>(ptr);
1789  return chosen_type.get_type() == kFLOAT
1790  ? ScalarTargetValue(static_cast<const float>(dval))
1791  : ScalarTargetValue(dval);
1792  }
1793  case 4: {
1794  CHECK_EQ(kFLOAT, chosen_type.get_type());
1795  return *reinterpret_cast<const float*>(ptr);
1796  }
1797  default:
1798  CHECK(false);
1799  }
1800  }
1801  if (chosen_type.is_integer() | chosen_type.is_boolean() || chosen_type.is_time() ||
1802  chosen_type.is_timeinterval()) {
1803  if (is_distinct_target(target_info)) {
1805  ival, query_mem_desc_.getCountDistinctDescriptor(target_logical_idx)));
1806  }
1807  // TODO(alex): remove int_resize_cast, make read_int_from_buff return the
1808  // right type instead
1809  if (inline_int_null_val(chosen_type) ==
1810  int_resize_cast(ival, chosen_type.get_logical_size())) {
1811  return inline_int_null_val(type_info);
1812  }
1813  return ival;
1814  }
1815  if (chosen_type.is_string() && chosen_type.get_compression() == kENCODING_DICT) {
1816  if (translate_strings) {
1817  if (static_cast<int32_t>(ival) ==
1818  NULL_INT) { // TODO(alex): this isn't nice, fix it
1819  return NullableString(nullptr);
1820  }
1821  StringDictionaryProxy* sdp{nullptr};
1822  if (!chosen_type.get_comp_param()) {
1823  sdp = row_set_mem_owner_->getLiteralStringDictProxy();
1824  } else {
1825  sdp = catalog_
1826  ? row_set_mem_owner_->getOrAddStringDictProxy(
1827  chosen_type.get_comp_param(), /*with_generation=*/false, catalog_)
1828  : row_set_mem_owner_->getStringDictProxy(
1829  chosen_type.get_comp_param()); // unit tests bypass the catalog
1830  }
1831  return NullableString(sdp->getString(ival));
1832  } else {
1833  return static_cast<int64_t>(static_cast<int32_t>(ival));
1834  }
1835  }
1836  if (chosen_type.is_decimal()) {
1837  if (decimal_to_double) {
1838  if (target_info.is_agg &&
1839  (target_info.agg_kind == kAVG || target_info.agg_kind == kSUM ||
1840  target_info.agg_kind == kMIN || target_info.agg_kind == kMAX) &&
1841  ival == inline_int_null_val(SQLTypeInfo(kBIGINT, false))) {
1842  return NULL_DOUBLE;
1843  }
1844  if (ival ==
1845  inline_int_null_val(SQLTypeInfo(decimal_to_int_type(chosen_type), false))) {
1846  return NULL_DOUBLE;
1847  }
1848  return static_cast<double>(ival) / exp_to_scale(chosen_type.get_scale());
1849  }
1850  return ival;
1851  }
1852  CHECK(false);
1853  return TargetValue(int64_t(0));
1854 }
1855 
1856 // Gets the TargetValue stored at position local_entry_idx in the col1_ptr and col2_ptr
1857 // column buffers. The second column is only used for AVG.
1858 // the global_entry_idx is passed to makeTargetValue to be used for
1859 // final lazy fetch (if there's any).
1861  const int8_t* col_ptr,
1862  const int8_t* keys_ptr,
1864  const size_t local_entry_idx,
1865  const size_t global_entry_idx,
1866  const TargetInfo& target_info,
1867  const size_t target_logical_idx,
1868  const size_t slot_idx,
1869  const bool translate_strings,
1870  const bool decimal_to_double) const {
1872  const auto col1_ptr = col_ptr;
1873  const auto compact_sz1 = query_mem_desc.getPaddedSlotWidthBytes(slot_idx);
1874  const auto next_col_ptr =
1875  advance_to_next_columnar_target_buff(col1_ptr, query_mem_desc, slot_idx);
1876  const auto col2_ptr = ((target_info.is_agg && target_info.agg_kind == kAVG) ||
1877  is_real_str_or_array(target_info))
1878  ? next_col_ptr
1879  : nullptr;
1880  const auto compact_sz2 = ((target_info.is_agg && target_info.agg_kind == kAVG) ||
1881  is_real_str_or_array(target_info))
1882  ? query_mem_desc.getPaddedSlotWidthBytes(slot_idx + 1)
1883  : 0;
1884 
1885  // TODO(Saman): add required logics for count distinct
1886  // geospatial target values:
1887  if (target_info.sql_type.is_geometry()) {
1888  return makeGeoTargetValue(
1889  col1_ptr, slot_idx, target_info, target_logical_idx, global_entry_idx);
1890  }
1891 
1892  const auto ptr1 = columnar_elem_ptr(local_entry_idx, col1_ptr, compact_sz1);
1893  if (target_info.agg_kind == kAVG || is_real_str_or_array(target_info)) {
1894  CHECK(col2_ptr);
1895  CHECK(compact_sz2);
1896  const auto ptr2 = columnar_elem_ptr(local_entry_idx, col2_ptr, compact_sz2);
1897  return target_info.agg_kind == kAVG
1898  ? make_avg_target_value(ptr1, compact_sz1, ptr2, compact_sz2, target_info)
1899  : makeVarlenTargetValue(ptr1,
1900  compact_sz1,
1901  ptr2,
1902  compact_sz2,
1903  target_info,
1904  target_logical_idx,
1905  translate_strings,
1906  global_entry_idx);
1907  }
1909  query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) < 0) {
1910  return makeTargetValue(ptr1,
1911  compact_sz1,
1912  target_info,
1913  target_logical_idx,
1914  translate_strings,
1915  decimal_to_double,
1916  global_entry_idx);
1917  }
1918  const auto key_width = query_mem_desc_.getEffectiveKeyWidth();
1919  const auto key_idx = query_mem_desc_.getTargetGroupbyIndex(target_logical_idx);
1920  CHECK_GE(key_idx, 0);
1921  auto key_col_ptr = keys_ptr + key_idx * query_mem_desc_.getEntryCount() * key_width;
1922  return makeTargetValue(columnar_elem_ptr(local_entry_idx, key_col_ptr, key_width),
1923  key_width,
1924  target_info,
1925  target_logical_idx,
1926  translate_strings,
1927  decimal_to_double,
1928  global_entry_idx);
1929 }
1930 
1931 // Gets the TargetValue stored in slot_idx (and slot_idx for AVG) of
1932 // rowwise_target_ptr.
1934  int8_t* rowwise_target_ptr,
1935  int8_t* keys_ptr,
1936  const size_t entry_buff_idx,
1937  const TargetInfo& target_info,
1938  const size_t target_logical_idx,
1939  const size_t slot_idx,
1940  const bool translate_strings,
1941  const bool decimal_to_double,
1942  const bool fixup_count_distinct_pointers) const {
1943  if (UNLIKELY(fixup_count_distinct_pointers)) {
1944  if (is_distinct_target(target_info)) {
1945  auto count_distinct_ptr_ptr = reinterpret_cast<int64_t*>(rowwise_target_ptr);
1946  const auto remote_ptr = *count_distinct_ptr_ptr;
1947  if (remote_ptr) {
1948  const auto ptr = storage_->mappedPtr(remote_ptr);
1949  if (ptr) {
1950  *count_distinct_ptr_ptr = ptr;
1951  } else {
1952  // need to create a zero filled buffer for this remote_ptr
1953  const auto& count_distinct_desc =
1954  query_mem_desc_.count_distinct_descriptors_[target_logical_idx];
1955  const auto bitmap_byte_sz = count_distinct_desc.sub_bitmap_count == 1
1956  ? count_distinct_desc.bitmapSizeBytes()
1957  : count_distinct_desc.bitmapPaddedSizeBytes();
1958  auto count_distinct_buffer = row_set_mem_owner_->allocateCountDistinctBuffer(
1959  bitmap_byte_sz, /*thread_idx=*/0);
1960  *count_distinct_ptr_ptr = reinterpret_cast<int64_t>(count_distinct_buffer);
1961  }
1962  }
1963  }
1964  return int64_t(0);
1965  }
1966  if (target_info.sql_type.is_geometry()) {
1967  return makeGeoTargetValue(
1968  rowwise_target_ptr, slot_idx, target_info, target_logical_idx, entry_buff_idx);
1969  }
1970 
1971  auto ptr1 = rowwise_target_ptr;
1972  int8_t compact_sz1 = query_mem_desc_.getPaddedSlotWidthBytes(slot_idx);
1974  !query_mem_desc_.hasKeylessHash() && !target_info.is_agg) {
1975  // Single column perfect hash group by can utilize one slot for both the key and the
1976  // target value if both values fit in 8 bytes. Use the target value actual size for
1977  // this case. If they don't, the target value should be 8 bytes, so we can still use
1978  // the actual size rather than the compact size.
1979  compact_sz1 = query_mem_desc_.getLogicalSlotWidthBytes(slot_idx);
1980  }
1981 
1982  // logic for deciding width of column
1983  if (target_info.agg_kind == kAVG || is_real_str_or_array(target_info)) {
1984  const auto ptr2 =
1985  rowwise_target_ptr + query_mem_desc_.getPaddedSlotWidthBytes(slot_idx);
1986  int8_t compact_sz2 = 0;
1987  // Skip reading the second slot if we have a none encoded string and are using
1988  // the none encoded strings buffer attached to ResultSetStorage
1990  (target_info.sql_type.is_array() ||
1991  (target_info.sql_type.is_string() &&
1992  target_info.sql_type.get_compression() == kENCODING_NONE)))) {
1993  compact_sz2 = query_mem_desc_.getPaddedSlotWidthBytes(slot_idx + 1);
1994  }
1995  if (separate_varlen_storage_valid_ && target_info.is_agg) {
1996  compact_sz2 = 8; // TODO(adb): is there a better way to do this?
1997  }
1998  CHECK(ptr2);
1999  return target_info.agg_kind == kAVG
2000  ? make_avg_target_value(ptr1, compact_sz1, ptr2, compact_sz2, target_info)
2001  : makeVarlenTargetValue(ptr1,
2002  compact_sz1,
2003  ptr2,
2004  compact_sz2,
2005  target_info,
2006  target_logical_idx,
2007  translate_strings,
2008  entry_buff_idx);
2009  }
2011  query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) < 0) {
2012  return makeTargetValue(ptr1,
2013  compact_sz1,
2014  target_info,
2015  target_logical_idx,
2016  translate_strings,
2017  decimal_to_double,
2018  entry_buff_idx);
2019  }
2020  const auto key_width = query_mem_desc_.getEffectiveKeyWidth();
2021  ptr1 = keys_ptr + query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) * key_width;
2022  return makeTargetValue(ptr1,
2023  key_width,
2024  target_info,
2025  target_logical_idx,
2026  translate_strings,
2027  decimal_to_double,
2028  entry_buff_idx);
2029 }
2030 
2031 // Returns true iff the entry at position entry_idx in buff contains a valid row.
2032 bool ResultSetStorage::isEmptyEntry(const size_t entry_idx, const int8_t* buff) const {
2035  return false;
2036  }
2038  return isEmptyEntryColumnar(entry_idx, buff);
2039  }
2044  CHECK_LT(static_cast<size_t>(query_mem_desc_.getTargetIdxForKey()),
2045  target_init_vals_.size());
2046  const auto rowwise_target_ptr = row_ptr_rowwise(buff, query_mem_desc_, entry_idx);
2047  const auto target_slot_off = result_set::get_byteoff_of_slot(
2049  return read_int_from_buff(rowwise_target_ptr + target_slot_off,
2052  target_init_vals_[query_mem_desc_.getTargetIdxForKey()];
2053  } else {
2054  const auto keys_ptr = row_ptr_rowwise(buff, query_mem_desc_, entry_idx);
2056  case 4:
2059  return *reinterpret_cast<const int32_t*>(keys_ptr) == EMPTY_KEY_32;
2060  case 8:
2061  return *reinterpret_cast<const int64_t*>(keys_ptr) == EMPTY_KEY_64;
2062  default:
2063  CHECK(false);
2064  return true;
2065  }
2066  }
2067 }
2068 
2069 /*
2070  * Returns true if the entry contain empty keys
2071  * This function should only be used with columanr format.
2072  */
2073 bool ResultSetStorage::isEmptyEntryColumnar(const size_t entry_idx,
2074  const int8_t* buff) const {
2078  return false;
2079  }
2084  CHECK_LT(static_cast<size_t>(query_mem_desc_.getTargetIdxForKey()),
2085  target_init_vals_.size());
2086  const auto col_buff = advance_col_buff_to_slot(
2088  const auto entry_buff =
2089  col_buff + entry_idx * query_mem_desc_.getPaddedSlotWidthBytes(
2091  return read_int_from_buff(entry_buff,
2094  target_init_vals_[query_mem_desc_.getTargetIdxForKey()];
2095  } else {
2096  // it's enough to find the first group key which is empty
2098  return reinterpret_cast<const int64_t*>(buff)[entry_idx] == EMPTY_KEY_64;
2099  } else {
2101  const auto target_buff = buff + query_mem_desc_.getPrependedGroupColOffInBytes(0);
2102  switch (query_mem_desc_.groupColWidth(0)) {
2103  case 8:
2104  return reinterpret_cast<const int64_t*>(target_buff)[entry_idx] == EMPTY_KEY_64;
2105  case 4:
2106  return reinterpret_cast<const int32_t*>(target_buff)[entry_idx] == EMPTY_KEY_32;
2107  case 2:
2108  return reinterpret_cast<const int16_t*>(target_buff)[entry_idx] == EMPTY_KEY_16;
2109  case 1:
2110  return reinterpret_cast<const int8_t*>(target_buff)[entry_idx] == EMPTY_KEY_8;
2111  default:
2112  CHECK(false);
2113  }
2114  }
2115  return false;
2116  }
2117  return false;
2118 }
2119 
2120 namespace {
2121 
2122 template <typename T>
2123 inline size_t make_bin_search(size_t l, size_t r, T&& is_empty_fn) {
2124  // Avoid search if there are no empty keys.
2125  if (!is_empty_fn(r - 1)) {
2126  return r;
2127  }
2128 
2129  --r;
2130  while (l != r) {
2131  size_t c = (l + r) / 2;
2132  if (is_empty_fn(c)) {
2133  r = c;
2134  } else {
2135  l = c + 1;
2136  }
2137  }
2138 
2139  return r;
2140 }
2141 
2142 } // namespace
2143 
2147 
2148  if (!query_mem_desc_.getEntryCount()) {
2149  return 0;
2150  }
2151 
2153  return make_bin_search(0, query_mem_desc_.getEntryCount(), [this](size_t idx) {
2154  return reinterpret_cast<const int64_t*>(buff_)[idx] == EMPTY_KEY_64;
2155  });
2156  } else {
2157  return make_bin_search(0, query_mem_desc_.getEntryCount(), [this](size_t idx) {
2158  const auto keys_ptr = row_ptr_rowwise(buff_, query_mem_desc_, idx);
2159  return *reinterpret_cast<const int64_t*>(keys_ptr) == EMPTY_KEY_64;
2160  });
2161  }
2162 }
2163 
2164 bool ResultSetStorage::isEmptyEntry(const size_t entry_idx) const {
2165  return isEmptyEntry(entry_idx, buff_);
2166 }
2167 
2169  const InternalTargetValue& val,
2170  const bool float_argument_input) {
2171  if (ti.get_notnull()) {
2172  return false;
2173  }
2174  if (val.isInt()) {
2175  return val.i1 == null_val_bit_pattern(ti, float_argument_input);
2176  }
2177  if (val.isPair()) {
2178  return !val.i2 ||
2179  pair_to_double({val.i1, val.i2}, ti, float_argument_input) == NULL_DOUBLE;
2180  }
2181  if (val.isStr()) {
2182  return !val.i1;
2183  }
2184  CHECK(val.isNull());
2185  return true;
2186 }
InternalTargetValue getColumnInternal(const int8_t *buff, const size_t entry_idx, const size_t target_logical_idx, const StorageLookupResult &storage_lookup_result) const
#define CHECK_EQ(x, y)
Definition: Logger.h:205
std::pair< size_t, size_t > getStorageIndex(const size_t entry_idx) const
Definition: ResultSet.cpp:638
#define NULL_DOUBLE
Permutation permutation_
Definition: ResultSet.h:740
bool isEmptyEntry(const size_t entry_idx, const int8_t *buff) const
#define EMPTY_KEY_64
ENTRY_TYPE getRowWisePerfectHashEntryAt(const size_t row_idx, const size_t target_idx, const size_t slot_idx) const
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:101
double decimal_to_double(const SQLTypeInfo &otype, int64_t oval)
bool isPair() const
Definition: TargetValue.h:67
AppendedStorage appended_storage_
Definition: ResultSet.h:734
tuple d
Definition: test_fsi.py:9
ENTRY_TYPE getColumnarPerfectHashEntryAt(const size_t row_idx, const size_t target_idx, const size_t slot_idx) const
int64_t getTargetGroupbyIndex(const size_t target_idx) const
GeoReturnType geo_return_type_
Definition: ResultSet.h:774
bool isEmptyEntryColumnar(const size_t entry_idx, const int8_t *buff) const
ExecutorDeviceType
bool isStr() const
Definition: TargetValue.h:71
bool is_null
Definition: sqltypes.h:147
bool isLogicalSizedColumnsAllowed() const
T advance_to_next_columnar_target_buff(T target_ptr, const QueryMemoryDescriptor &query_mem_desc, const size_t target_slot_idx)
#define const
SQLTypeInfo sql_type
Definition: TargetInfo.h:42
TargetValue build_string_array_target_value(const int32_t *buff, const size_t buff_sz, const int dict_id, const bool translate_strings, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner, const Catalog_Namespace::Catalog *catalog)
size_t make_bin_search(size_t l, size_t r, T &&is_empty_fn)
std::unique_ptr< ArrayDatum > lazy_fetch_chunk(const int8_t *ptr, const int64_t varlen_ptr)
const Catalog_Namespace::Catalog * catalog_
Definition: ResultSet.h:742
unsigned long long CUdeviceptr
Definition: nocuda.h:27
tuple r
Definition: test_fsi.py:16
std::vector< TargetValue > getNextRow(const bool translate_strings, const bool decimal_to_double) const
static bool isNull(const SQLTypeInfo &ti, const InternalTargetValue &val, const bool float_argument_input)
QueryMemoryDescriptor query_mem_desc_
Definition: ResultSet.h:732
#define UNREACHABLE()
Definition: Logger.h:241
#define CHECK_GE(x, y)
Definition: Logger.h:210
std::unique_ptr< ResultSetStorage > storage_
Definition: ResultSet.h:733
bool is_null_point(const SQLTypeInfo &geo_ti, const int8_t *coords, const size_t coords_sz)
std::string getString(int32_t string_id) const
ENTRY_TYPE getEntryAt(const size_t row_idx, const size_t target_idx, const size_t slot_idx) const
size_t getEffectiveKeyWidth() const
Constants for Builtin SQL Types supported by OmniSci.
TargetValue makeGeoTargetValue(const int8_t *geo_target_ptr, const size_t slot_idx, const TargetInfo &target_info, const size_t target_logical_idx, const size_t entry_buff_idx) const
TargetValue getTargetValueFromBufferRowwise(int8_t *rowwise_target_ptr, int8_t *keys_ptr, const size_t entry_buff_idx, const TargetInfo &target_info, const size_t target_logical_idx, const size_t slot_idx, const bool translate_strings, const bool decimal_to_double, const bool fixup_count_distinct_pointers) const
int64_t read_int_from_buff(const int8_t *ptr, const int8_t compact_sz)
size_t keep_first_
Definition: ResultSet.h:738
TargetValue make_avg_target_value(const int8_t *ptr1, const int8_t compact_sz1, const int8_t *ptr2, const int8_t compact_sz2, const TargetInfo &target_info)
double pair_to_double(const std::pair< int64_t, int64_t > &fp_pair, const SQLTypeInfo &ti, const bool float_argument_input)
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:134
std::vector< SerializedVarlenBufferStorage > serialized_varlen_buffer_
Definition: ResultSet.h:765
int64_t lazyReadInt(const int64_t ival, const size_t target_logical_idx, const StorageLookupResult &storage_lookup_result) const
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:314
OneIntegerColumnRow getOneColRow(const size_t index) const
TargetValue getTargetValueFromBufferColwise(const int8_t *col_ptr, const int8_t *keys_ptr, const QueryMemoryDescriptor &query_mem_desc, const size_t local_entry_idx, const size_t global_entry_idx, const TargetInfo &target_info, const size_t target_logical_idx, const size_t slot_idx, const bool translate_strings, const bool decimal_to_double) 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)
#define CHECK_GT(x, y)
Definition: Logger.h:209
int64_t null_val_bit_pattern(const SQLTypeInfo &ti, const bool float_argument_input)
DEVICE void ChunkIter_get_nth(ChunkIter *it, int n, bool uncompress, VarlenDatum *result, bool *is_end)
Definition: ChunkIter.cpp:181
TargetValue build_array_target_value(const int8_t *buff, const size_t buff_sz, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner)
std::string to_string(char const *&&v)
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
int8_t * pointer
Definition: sqltypes.h:146
#define NULL_INT
const ResultSet * result_set_
Definition: ResultSet.h:598
std::vector< TargetValue > getRowAtNoTranslations(const size_t index, const std::vector< bool > &targets_to_skip={}) const
const int8_t * advance_col_buff_to_slot(const int8_t *buff, const QueryMemoryDescriptor &query_mem_desc, const std::vector< TargetInfo > &targets, const size_t slot_idx, const bool separate_varlen_storage)
Definition: sqldefs.h:73
Serialization routines for geospatial types.
std::conditional_t< is_cuda_compiler(), DeviceArrayDatum, HostArrayDatum > ArrayDatum
Definition: sqltypes.h:202
const SQLTypeInfo get_compact_type(const TargetInfo &target)
size_t global_entry_idx_
Definition: ResultSet.h:126
InternalTargetValue getVarlenOrderEntry(const int64_t str_ptr, const size_t str_len) const
const std::vector< TargetInfo > targets_
Definition: ResultSet.h:729
int8_t groupColWidth(const size_t key_idx) const
std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner_
Definition: ResultSet.h:739
size_t get_byteoff_of_slot(const size_t slot_idx, const QueryMemoryDescriptor &query_mem_desc)
size_t drop_first_
Definition: ResultSet.h:737
bool is_agg
Definition: TargetInfo.h:40
size_t advance_slot(const size_t j, const TargetInfo &target_info, const bool separate_varlen_storage)
CONSTEXPR DEVICE bool is_null(const T &value)
static TargetValue build(const SQLTypeInfo &geo_ti, const ResultSet::GeoReturnType return_type, T &&...vals)
Classes representing a parse tree.
int64_t count_distinct_set_size(const int64_t set_handle, const CountDistinctDescriptor &count_distinct_desc)
Definition: CountDistinct.h:75
size_t getGroupbyColCount() const
#define CHECK_NE(x, y)
Definition: Logger.h:206
int count
size_t targetGroupbyIndicesSize() const
size_t binSearchRowCount() const
void copy_from_gpu(Data_Namespace::DataMgr *data_mgr, void *dst, const CUdeviceptr src, const size_t num_bytes, const int device_id)
boost::optional< std::vector< ScalarTargetValue >> ArrayTargetValue
Definition: TargetValue.h:157
int64_t lazy_decode(const ColumnLazyFetchInfo &col_lazy_fetch, const int8_t *byte_stream, const int64_t pos)
CountDistinctDescriptors count_distinct_descriptors_
Definition: sqldefs.h:75
size_t getPaddedColWidthForRange(const size_t offset, const size_t range) const
StorageLookupResult findStorage(const size_t entry_idx) const
Definition: ResultSet.cpp:663
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:130
bool isNull() const
Definition: TargetValue.h:69
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
#define EMPTY_KEY_8
const std::vector< ColumnLazyFetchInfo > lazy_fetch_info_
Definition: ResultSet.h:752
void copyColumnIntoBuffer(const size_t column_idx, int8_t *output_buffer, const size_t output_buffer_size) const
bool g_enable_smem_group_by true
T row_ptr_rowwise(T buff, const QueryMemoryDescriptor &query_mem_desc, const size_t entry_idx)
SQLAgg agg_kind
Definition: TargetInfo.h:41
QueryDescriptionType getQueryDescriptionType() const
static double calculateQuantile(quantile::TDigest *const t_digest, double const q)
Definition: ResultSet.cpp:729
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:419
#define UNLIKELY(x)
Definition: likely.h:25
std::vector< TargetValue > getRowAt(const size_t index) const
const CountDistinctDescriptor & getCountDistinctDescriptor(const size_t idx) const
#define CHECK_LT(x, y)
Definition: Logger.h:207
bool is_real_str_or_array(const TargetInfo &target_info)
bool isSingleColumnGroupByWithPerfectHash() const
#define CHECK_LE(x, y)
Definition: Logger.h:208
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:322
bool is_date_in_days() const
Definition: sqltypes.h:720
int64_t int_resize_cast(const int64_t ival, const size_t sz)
boost::optional< boost::variant< GeoPointTargetValue, GeoLineStringTargetValue, GeoPolyTargetValue, GeoMultiPolyTargetValue >> GeoTargetValue
Definition: TargetValue.h:161
int get_array_context_logical_size() const
Definition: sqltypes.h:551
bool isGeoColOnGpu(const size_t col_idx) const
#define EMPTY_KEY_16
std::vector< std::vector< std::vector< const int8_t * > > > col_buffers_
Definition: ResultSet.h:753
#define DEF_GET_ENTRY_AT(query_type, columnar_output)
bool isRowAtEmpty(const size_t index) const
size_t entryCount() const
static auto fetch(const SQLTypeInfo &geo_ti, const ResultSet::GeoReturnType return_type, Data_Namespace::DataMgr *data_mgr, const bool fetch_data_from_gpu, const int device_id, T &&...vals)
std::string get_type_name() const
Definition: sqltypes.h:417
boost::variant< std::string, void * > NullableString
Definition: TargetValue.h:155
TargetValue makeTargetValue(const int8_t *ptr, const int8_t compact_sz, const TargetInfo &target_info, const size_t target_logical_idx, const bool translate_strings, const bool decimal_to_double, const size_t entry_buff_idx) const
static auto yieldGpuDatumFetcher(Data_Namespace::DataMgr *data_mgr_ptr, const int device_id)
const bool is_lazily_fetched
std::vector< std::vector< int64_t > > consistent_frag_sizes_
Definition: ResultSet.h:755
const ExecutorDeviceType device_type_
Definition: ResultSet.h:730
std::vector< TargetValue > getNextRowImpl(const bool translate_strings, const bool decimal_to_double) const
bool isInt() const
Definition: TargetValue.h:65
bool g_enable_watchdog false
Definition: Execute.cpp:76
static auto fetch(const SQLTypeInfo &geo_ti, const ResultSet::GeoReturnType return_type, T &&...vals)
#define CHECK(condition)
Definition: Logger.h:197
bool is_geometry() const
Definition: sqltypes.h:500
ScalarTargetValue make_scalar_tv(const T val)
size_t getBufferSizeBytes(const ExecutorDeviceType device_type) const
#define EMPTY_KEY_32
uint64_t exp_to_scale(const unsigned exp)
size_t crt_row_buff_idx_
Definition: ResultSet.h:735
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
QueryDescriptionType
Definition: Types.h:26
std::vector< std::vector< std::vector< int64_t > > > frag_offsets_
Definition: ResultSet.h:754
Basic constructors and methods of the row set interface.
bool separate_varlen_storage_valid_
Definition: ResultSet.h:766
boost::variant< ScalarTargetValue, ArrayTargetValue, GeoTargetValue, GeoTargetValuePtr > TargetValue
Definition: TargetValue.h:167
std::vector< TargetValue > getNextRowUnlocked(const bool translate_strings, const bool decimal_to_double) const
std::vector< std::pair< const int8_t *, const int64_t > > make_vals_vector(std::index_sequence< indices...>, const Tuple &tuple)
T advance_target_ptr_col_wise(T target_ptr, const TargetInfo &target_info, const size_t slot_idx, const QueryMemoryDescriptor &query_mem_desc, const bool separate_varlen_storage)
size_t advanceCursorToNextEntry() const
bool is_string() const
Definition: sqltypes.h:488
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:321
ENTRY_TYPE getColumnarBaselineEntryAt(const size_t row_idx, const size_t target_idx, const size_t slot_idx) const
Definition: sqldefs.h:74
size_t crt_row_buff_idx_
Definition: ResultSet.h:125
InternalTargetValue getColumnInternal(const int8_t *buff, const size_t entry_idx, const size_t target_logical_idx, const StorageLookupResult &storage_lookup_result) const
std::vector< std::vector< TargetOffsets > > offsets_for_storage_
Definition: ResultSet.h:596
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:712
T get_cols_ptr(T buff, const QueryMemoryDescriptor &query_mem_desc)
const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const
Definition: sqldefs.h:72
bool global_entry_idx_valid_
Definition: ResultSet.h:127
std::unique_ptr< VarlenDatum > VarlenDatumPtr
#define IS_GEO(T)
Definition: sqltypes.h:245
bool isDirectColumnarConversionPossible() const
Definition: ResultSet.cpp:1032
size_t get_key_bytes_rowwise(const QueryMemoryDescriptor &query_mem_desc)
const int8_t * columnar_elem_ptr(const size_t entry_idx, const int8_t *col1_ptr, const int8_t compact_sz1)
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)
bool is_array() const
Definition: sqltypes.h:496
TargetValue makeVarlenTargetValue(const int8_t *ptr1, const int8_t compact_sz1, const int8_t *ptr2, const int8_t compact_sz2, const TargetInfo &target_info, const size_t target_logical_idx, const bool translate_strings, const size_t entry_buff_idx) const
std::pair< int64_t, int64_t > get_frag_id_and_local_idx(const std::vector< std::vector< T >> &frag_offsets, const size_t tab_or_col_idx, const int64_t global_idx)
const std::vector< const int8_t * > & getColumnFrag(const size_t storge_idx, const size_t col_logical_idx, int64_t &global_idx) const
const Executor * getExecutor() const
std::unique_ptr< ArrayDatum > fetch_data_from_gpu(int64_t varlen_ptr, const int64_t length, Data_Namespace::DataMgr *data_mgr, const int device_id)
ENTRY_TYPE getRowWiseBaselineEntryAt(const size_t row_idx, const size_t target_idx, const size_t slot_idx) const
boost::variant< int64_t, double, float, NullableString > ScalarTargetValue
Definition: TargetValue.h:156
int32_t getTargetIdxForKey() const
size_t length
Definition: sqltypes.h:145
size_t getPrependedGroupColOffInBytes(const size_t group_idx) const
const int device_id_
Definition: ResultSet.h:731