OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResultSetIteration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 
23 #include "Execute.h"
24 #include "Geospatial/Compression.h"
25 #include "Geospatial/Types.h"
26 #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  const auto buff = storage->buff_;
135  CHECK(buff);
136  std::vector<TargetValue> row;
137  size_t agg_col_idx = 0;
138  int8_t* rowwise_target_ptr{nullptr};
139  int8_t* keys_ptr{nullptr};
140  const int8_t* crt_col_ptr{nullptr};
141  if (query_mem_desc_.didOutputColumnar()) {
142  keys_ptr = buff;
143  crt_col_ptr = get_cols_ptr(buff, storage->query_mem_desc_);
144  } else {
145  keys_ptr = row_ptr_rowwise(buff, query_mem_desc_, local_entry_idx);
146  const auto key_bytes_with_padding =
147  align_to_int64(get_key_bytes_rowwise(query_mem_desc_));
148  rowwise_target_ptr = keys_ptr + key_bytes_with_padding;
149  }
150  for (size_t target_idx = 0; target_idx < storage->targets_.size(); ++target_idx) {
151  const auto& agg_info = storage->targets_[target_idx];
152  if (query_mem_desc_.didOutputColumnar()) {
153  if (UNLIKELY(!targets_to_skip.empty())) {
154  row.push_back(!targets_to_skip[target_idx]
155  ? getTargetValueFromBufferColwise(crt_col_ptr,
156  keys_ptr,
157  storage->query_mem_desc_,
158  local_entry_idx,
159  global_entry_idx,
160  agg_info,
161  target_idx,
162  agg_col_idx,
163  translate_strings,
164  decimal_to_double)
165  : nullptr);
166  } else {
167  row.push_back(getTargetValueFromBufferColwise(crt_col_ptr,
168  keys_ptr,
169  storage->query_mem_desc_,
170  local_entry_idx,
171  global_entry_idx,
172  agg_info,
173  target_idx,
174  agg_col_idx,
175  translate_strings,
176  decimal_to_double));
177  }
178  crt_col_ptr = advance_target_ptr_col_wise(crt_col_ptr,
179  agg_info,
180  agg_col_idx,
181  storage->query_mem_desc_,
182  separate_varlen_storage_valid_);
183  } else {
184  if (UNLIKELY(!targets_to_skip.empty())) {
185  row.push_back(!targets_to_skip[target_idx]
186  ? getTargetValueFromBufferRowwise(rowwise_target_ptr,
187  keys_ptr,
188  global_entry_idx,
189  agg_info,
190  target_idx,
191  agg_col_idx,
192  translate_strings,
193  decimal_to_double,
194  fixup_count_distinct_pointers)
195  : nullptr);
196  } else {
197  row.push_back(getTargetValueFromBufferRowwise(rowwise_target_ptr,
198  keys_ptr,
199  global_entry_idx,
200  agg_info,
201  target_idx,
202  agg_col_idx,
203  translate_strings,
204  decimal_to_double,
205  fixup_count_distinct_pointers));
206  }
207  rowwise_target_ptr = advance_target_ptr_row_wise(rowwise_target_ptr,
208  agg_info,
209  agg_col_idx,
210  query_mem_desc_,
211  separate_varlen_storage_valid_);
212  }
213  agg_col_idx = advance_slot(agg_col_idx, agg_info, separate_varlen_storage_valid_);
214  }
215 
216  return row;
217 }
218 
219 TargetValue ResultSet::getRowAt(const size_t row_idx,
220  const size_t col_idx,
221  const bool translate_strings,
222  const bool decimal_to_double /* = true */) const {
223  std::lock_guard<std::mutex> lock(row_iteration_mutex_);
224  moveToBegin();
225  for (size_t i = 0; i < row_idx; ++i) {
226  auto crt_row = getNextRowUnlocked(translate_strings, decimal_to_double);
227  CHECK(!crt_row.empty());
228  }
229  auto crt_row = getNextRowUnlocked(translate_strings, decimal_to_double);
230  CHECK(!crt_row.empty());
231  return crt_row[col_idx];
232 }
233 
234 OneIntegerColumnRow ResultSet::getOneColRow(const size_t global_entry_idx) const {
235  const auto storage_lookup_result = findStorage(global_entry_idx);
236  const auto storage = storage_lookup_result.storage_ptr;
237  const auto local_entry_idx = storage_lookup_result.fixedup_entry_idx;
238  if (storage->isEmptyEntry(local_entry_idx)) {
239  return {0, false};
240  }
241  const auto buff = storage->buff_;
242  CHECK(buff);
243  CHECK(!query_mem_desc_.didOutputColumnar());
244  const auto keys_ptr = row_ptr_rowwise(buff, query_mem_desc_, local_entry_idx);
245  const auto key_bytes_with_padding =
246  align_to_int64(get_key_bytes_rowwise(query_mem_desc_));
247  const auto rowwise_target_ptr = keys_ptr + key_bytes_with_padding;
248  const auto tv = getTargetValueFromBufferRowwise(rowwise_target_ptr,
249  keys_ptr,
250  global_entry_idx,
251  targets_.front(),
252  0,
253  0,
254  false,
255  false,
256  false);
257  const auto scalar_tv = boost::get<ScalarTargetValue>(&tv);
258  CHECK(scalar_tv);
259  const auto ival_ptr = boost::get<int64_t>(scalar_tv);
260  CHECK(ival_ptr);
261  return {*ival_ptr, true};
262 }
263 
264 std::vector<TargetValue> ResultSet::getRowAt(const size_t logical_index) const {
265  if (logical_index >= entryCount()) {
266  return {};
267  }
268  const auto entry_idx =
269  permutation_.empty() ? logical_index : permutation_[logical_index];
270  return getRowAt(entry_idx, true, false, false);
271 }
272 
273 std::vector<TargetValue> ResultSet::getRowAtNoTranslations(
274  const size_t logical_index,
275  const std::vector<bool>& targets_to_skip /* = {}*/) const {
276  if (logical_index >= entryCount()) {
277  return {};
278  }
279  const auto entry_idx =
280  permutation_.empty() ? logical_index : permutation_[logical_index];
281  return getRowAt(entry_idx, false, false, false, targets_to_skip);
282 }
283 
284 bool ResultSet::isRowAtEmpty(const size_t logical_index) const {
285  if (logical_index >= entryCount()) {
286  return true;
287  }
288  const auto entry_idx =
289  permutation_.empty() ? logical_index : permutation_[logical_index];
290  const auto storage_lookup_result = findStorage(entry_idx);
291  const auto storage = storage_lookup_result.storage_ptr;
292  const auto local_entry_idx = storage_lookup_result.fixedup_entry_idx;
293  return storage->isEmptyEntry(local_entry_idx);
294 }
295 
296 std::vector<TargetValue> ResultSet::getNextRow(const bool translate_strings,
297  const bool decimal_to_double) const {
298  std::lock_guard<std::mutex> lock(row_iteration_mutex_);
299  if (!storage_ && !just_explain_) {
300  return {};
301  }
302  return getNextRowUnlocked(translate_strings, decimal_to_double);
303 }
304 
305 std::vector<TargetValue> ResultSet::getNextRowUnlocked(
306  const bool translate_strings,
307  const bool decimal_to_double) const {
308  if (just_explain_) {
309  if (fetched_so_far_) {
310  return {};
311  }
312  fetched_so_far_ = 1;
313  return {explanation_};
314  }
315  return getNextRowImpl(translate_strings, decimal_to_double);
316 }
317 
318 std::vector<TargetValue> ResultSet::getNextRowImpl(const bool translate_strings,
319  const bool decimal_to_double) const {
320  size_t entry_buff_idx = 0;
321  do {
322  if (keep_first_ && fetched_so_far_ >= drop_first_ + keep_first_) {
323  return {};
324  }
325 
326  entry_buff_idx = advanceCursorToNextEntry();
327 
328  if (crt_row_buff_idx_ >= entryCount()) {
329  CHECK_EQ(entryCount(), crt_row_buff_idx_);
330  return {};
331  }
332  ++crt_row_buff_idx_;
333  ++fetched_so_far_;
334 
335  } while (drop_first_ && fetched_so_far_ <= drop_first_);
336 
337  auto row = getRowAt(entry_buff_idx, translate_strings, decimal_to_double, false);
338  CHECK(!row.empty());
339 
340  return row;
341 }
342 
343 namespace {
344 
345 const int8_t* columnar_elem_ptr(const size_t entry_idx,
346  const int8_t* col1_ptr,
347  const int8_t compact_sz1) {
348  return col1_ptr + compact_sz1 * entry_idx;
349 }
350 
351 int64_t int_resize_cast(const int64_t ival, const size_t sz) {
352  switch (sz) {
353  case 8:
354  return ival;
355  case 4:
356  return static_cast<int32_t>(ival);
357  case 2:
358  return static_cast<int16_t>(ival);
359  case 1:
360  return static_cast<int8_t>(ival);
361  default:
362  UNREACHABLE();
363  }
364  UNREACHABLE();
365  return 0;
366 }
367 
368 } // namespace
369 
371  // Compute offsets for base storage and all appended storage
372  for (size_t storage_idx = 0; storage_idx < result_set_->appended_storage_.size() + 1;
373  ++storage_idx) {
374  offsets_for_storage_.emplace_back();
375 
376  const int8_t* rowwise_target_ptr{0};
377 
378  size_t agg_col_idx = 0;
379  for (size_t target_idx = 0; target_idx < result_set_->storage_->targets_.size();
380  ++target_idx) {
381  const auto& agg_info = result_set_->storage_->targets_[target_idx];
382 
383  auto ptr1 = rowwise_target_ptr;
384  const auto compact_sz1 =
385  result_set_->query_mem_desc_.getPaddedSlotWidthBytes(agg_col_idx)
386  ? result_set_->query_mem_desc_.getPaddedSlotWidthBytes(agg_col_idx)
387  : key_width_;
388 
389  const int8_t* ptr2{nullptr};
390  int8_t compact_sz2{0};
391  if ((agg_info.is_agg && agg_info.agg_kind == kAVG)) {
392  ptr2 = ptr1 + compact_sz1;
393  compact_sz2 =
394  result_set_->query_mem_desc_.getPaddedSlotWidthBytes(agg_col_idx + 1);
395  } else if (is_real_str_or_array(agg_info)) {
396  ptr2 = ptr1 + compact_sz1;
397  if (!result_set_->separate_varlen_storage_valid_) {
398  // None encoded strings explicitly attached to ResultSetStorage do not have a
399  // second slot in the QueryMemoryDescriptor col width vector
400  compact_sz2 =
401  result_set_->query_mem_desc_.getPaddedSlotWidthBytes(agg_col_idx + 1);
402  }
403  }
404  offsets_for_storage_[storage_idx].push_back(
405  TargetOffsets{ptr1,
406  static_cast<size_t>(compact_sz1),
407  ptr2,
408  static_cast<size_t>(compact_sz2)});
409  rowwise_target_ptr =
410  advance_target_ptr_row_wise(rowwise_target_ptr,
411  agg_info,
412  agg_col_idx,
413  result_set_->query_mem_desc_,
414  result_set_->separate_varlen_storage_valid_);
415 
416  agg_col_idx = advance_slot(
417  agg_col_idx, agg_info, result_set_->separate_varlen_storage_valid_);
418  }
419  CHECK_EQ(offsets_for_storage_[storage_idx].size(),
420  result_set_->storage_->targets_.size());
421  }
422 }
423 
425  const int8_t* buff,
426  const size_t entry_idx,
427  const size_t target_logical_idx,
428  const StorageLookupResult& storage_lookup_result) const {
429  CHECK(buff);
430  const int8_t* rowwise_target_ptr{nullptr};
431  const int8_t* keys_ptr{nullptr};
432 
433  const size_t storage_idx = storage_lookup_result.storage_idx;
434 
435  CHECK_LT(storage_idx, offsets_for_storage_.size());
436  CHECK_LT(target_logical_idx, offsets_for_storage_[storage_idx].size());
437 
438  const auto& offsets_for_target = offsets_for_storage_[storage_idx][target_logical_idx];
439  const auto& agg_info = result_set_->storage_->targets_[target_logical_idx];
440  const auto& type_info = agg_info.sql_type;
441 
442  keys_ptr = get_rowwise_ptr(buff, entry_idx);
443  rowwise_target_ptr = keys_ptr + key_bytes_with_padding_;
444  auto ptr1 = rowwise_target_ptr + reinterpret_cast<size_t>(offsets_for_target.ptr1);
445  if (result_set_->query_mem_desc_.targetGroupbyIndicesSize() > 0) {
446  if (result_set_->query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) >= 0) {
447  ptr1 = keys_ptr +
448  result_set_->query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) *
449  key_width_;
450  }
451  }
452  const auto i1 =
453  result_set_->lazyReadInt(read_int_from_buff(ptr1, offsets_for_target.compact_sz1),
454  target_logical_idx,
455  storage_lookup_result);
456  if (agg_info.is_agg && agg_info.agg_kind == kAVG) {
457  CHECK(offsets_for_target.ptr2);
458  const auto ptr2 =
459  rowwise_target_ptr + reinterpret_cast<size_t>(offsets_for_target.ptr2);
460  const auto i2 = read_int_from_buff(ptr2, offsets_for_target.compact_sz2);
461  return InternalTargetValue(i1, i2);
462  } else {
463  if (type_info.is_string() && type_info.get_compression() == kENCODING_NONE) {
464  CHECK(!agg_info.is_agg);
465  if (!result_set_->lazy_fetch_info_.empty()) {
466  CHECK_LT(target_logical_idx, result_set_->lazy_fetch_info_.size());
467  const auto& col_lazy_fetch = result_set_->lazy_fetch_info_[target_logical_idx];
468  if (col_lazy_fetch.is_lazily_fetched) {
469  return InternalTargetValue(reinterpret_cast<const std::string*>(i1));
470  }
471  }
472  if (result_set_->separate_varlen_storage_valid_) {
473  if (i1 < 0) {
474  CHECK_EQ(-1, i1);
475  return InternalTargetValue(static_cast<const std::string*>(nullptr));
476  }
477  CHECK_LT(storage_lookup_result.storage_idx,
478  result_set_->serialized_varlen_buffer_.size());
479  const auto& varlen_buffer_for_fragment =
480  result_set_->serialized_varlen_buffer_[storage_lookup_result.storage_idx];
481  CHECK_LT(static_cast<size_t>(i1), varlen_buffer_for_fragment.size());
482  return InternalTargetValue(&varlen_buffer_for_fragment[i1]);
483  }
484  CHECK(offsets_for_target.ptr2);
485  const auto ptr2 =
486  rowwise_target_ptr + reinterpret_cast<size_t>(offsets_for_target.ptr2);
487  const auto str_len = read_int_from_buff(ptr2, offsets_for_target.compact_sz2);
488  CHECK_GE(str_len, 0);
489  return result_set_->getVarlenOrderEntry(i1, str_len);
490  } else if (agg_info.is_agg && agg_info.agg_kind == kMODE) {
491  return InternalTargetValue(i1); // AggMode*
492  }
493  return InternalTargetValue(
494  type_info.is_fp() ? i1 : int_resize_cast(i1, type_info.get_logical_size()));
495  }
496 }
497 
499  // Compute offsets for base storage and all appended storage
500  const auto key_width = result_set_->query_mem_desc_.getEffectiveKeyWidth();
501  for (size_t storage_idx = 0; storage_idx < result_set_->appended_storage_.size() + 1;
502  ++storage_idx) {
503  offsets_for_storage_.emplace_back();
504 
505  const int8_t* buff = storage_idx == 0
506  ? result_set_->storage_->buff_
507  : result_set_->appended_storage_[storage_idx - 1]->buff_;
508  CHECK(buff);
509 
510  const auto& crt_query_mem_desc =
511  storage_idx == 0
512  ? result_set_->storage_->query_mem_desc_
513  : result_set_->appended_storage_[storage_idx - 1]->query_mem_desc_;
514  const int8_t* crt_col_ptr = get_cols_ptr(buff, crt_query_mem_desc);
515 
516  size_t agg_col_idx = 0;
517  for (size_t target_idx = 0; target_idx < result_set_->storage_->targets_.size();
518  ++target_idx) {
519  const auto& agg_info = result_set_->storage_->targets_[target_idx];
520 
521  const auto compact_sz1 =
522  crt_query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx)
523  ? crt_query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx)
524  : key_width;
525 
526  const auto next_col_ptr = advance_to_next_columnar_target_buff(
527  crt_col_ptr, crt_query_mem_desc, agg_col_idx);
528  const bool uses_two_slots = (agg_info.is_agg && agg_info.agg_kind == kAVG) ||
529  is_real_str_or_array(agg_info);
530  const auto col2_ptr = uses_two_slots ? next_col_ptr : nullptr;
531  const auto compact_sz2 =
532  (agg_info.is_agg && agg_info.agg_kind == kAVG) || is_real_str_or_array(agg_info)
533  ? crt_query_mem_desc.getPaddedSlotWidthBytes(agg_col_idx + 1)
534  : 0;
535 
536  offsets_for_storage_[storage_idx].push_back(
537  TargetOffsets{crt_col_ptr,
538  static_cast<size_t>(compact_sz1),
539  col2_ptr,
540  static_cast<size_t>(compact_sz2)});
541 
542  crt_col_ptr = next_col_ptr;
543  if (uses_two_slots) {
545  crt_col_ptr, crt_query_mem_desc, agg_col_idx + 1);
546  }
547  agg_col_idx = advance_slot(
548  agg_col_idx, agg_info, result_set_->separate_varlen_storage_valid_);
549  }
550  CHECK_EQ(offsets_for_storage_[storage_idx].size(),
551  result_set_->storage_->targets_.size());
552  }
553 }
554 
556  const int8_t* buff,
557  const size_t entry_idx,
558  const size_t target_logical_idx,
559  const StorageLookupResult& storage_lookup_result) const {
560  const size_t storage_idx = storage_lookup_result.storage_idx;
561 
562  CHECK_LT(storage_idx, offsets_for_storage_.size());
563  CHECK_LT(target_logical_idx, offsets_for_storage_[storage_idx].size());
564 
565  const auto& offsets_for_target = offsets_for_storage_[storage_idx][target_logical_idx];
566  const auto& agg_info = result_set_->storage_->targets_[target_logical_idx];
567  const auto& type_info = agg_info.sql_type;
568  auto ptr1 = offsets_for_target.ptr1;
569  if (result_set_->query_mem_desc_.targetGroupbyIndicesSize() > 0) {
570  if (result_set_->query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) >= 0) {
571  ptr1 =
572  buff + result_set_->query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) *
573  result_set_->query_mem_desc_.getEffectiveKeyWidth() *
574  result_set_->query_mem_desc_.entry_count_;
575  }
576  }
577 
578  const auto i1 = result_set_->lazyReadInt(
580  columnar_elem_ptr(entry_idx, ptr1, offsets_for_target.compact_sz1),
581  offsets_for_target.compact_sz1),
582  target_logical_idx,
583  storage_lookup_result);
584  if (agg_info.is_agg && agg_info.agg_kind == kAVG) {
585  CHECK(offsets_for_target.ptr2);
586  const auto i2 = read_int_from_buff(
588  entry_idx, offsets_for_target.ptr2, offsets_for_target.compact_sz2),
589  offsets_for_target.compact_sz2);
590  return InternalTargetValue(i1, i2);
591  } else {
592  // for TEXT ENCODING NONE:
593  if (type_info.is_string() && type_info.get_compression() == kENCODING_NONE) {
594  CHECK(!agg_info.is_agg);
595  if (!result_set_->lazy_fetch_info_.empty()) {
596  CHECK_LT(target_logical_idx, result_set_->lazy_fetch_info_.size());
597  const auto& col_lazy_fetch = result_set_->lazy_fetch_info_[target_logical_idx];
598  if (col_lazy_fetch.is_lazily_fetched) {
599  return InternalTargetValue(reinterpret_cast<const std::string*>(i1));
600  }
601  }
602  if (result_set_->separate_varlen_storage_valid_) {
603  if (i1 < 0) {
604  CHECK_EQ(-1, i1);
605  return InternalTargetValue(static_cast<const std::string*>(nullptr));
606  }
607  CHECK_LT(storage_lookup_result.storage_idx,
608  result_set_->serialized_varlen_buffer_.size());
609  const auto& varlen_buffer_for_fragment =
610  result_set_->serialized_varlen_buffer_[storage_lookup_result.storage_idx];
611  CHECK_LT(static_cast<size_t>(i1), varlen_buffer_for_fragment.size());
612  return InternalTargetValue(&varlen_buffer_for_fragment[i1]);
613  }
614  CHECK(offsets_for_target.ptr2);
615  const auto i2 = read_int_from_buff(
617  entry_idx, offsets_for_target.ptr2, offsets_for_target.compact_sz2),
618  offsets_for_target.compact_sz2);
619  CHECK_GE(i2, 0);
620  return result_set_->getVarlenOrderEntry(i1, i2);
621  }
622  return InternalTargetValue(
623  type_info.is_fp() ? i1 : int_resize_cast(i1, type_info.get_logical_size()));
624  }
625 }
626 
628  const size_t str_len) const {
629  char* host_str_ptr{nullptr};
630  std::vector<int8_t> cpu_buffer;
632  cpu_buffer.resize(str_len);
633  const auto executor = query_mem_desc_.getExecutor();
634  CHECK(executor);
635  auto data_mgr = executor->getDataMgr();
636  auto allocator = std::make_unique<CudaAllocator>(
637  data_mgr, device_id_, getQueryEngineCudaStreamForDevice(device_id_));
638  allocator->copyFromDevice(
639  &cpu_buffer[0], reinterpret_cast<int8_t*>(str_ptr), str_len);
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 shared::StringDictKey& dict_key,
797  const bool translate_strings,
798  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner) {
799  std::vector<ScalarTargetValue> values;
800  CHECK_EQ(size_t(0), buff_sz % sizeof(int32_t));
801  const size_t num_elems = buff_sz / sizeof(int32_t);
802  if (translate_strings) {
803  for (size_t i = 0; i < num_elems; ++i) {
804  const auto string_id = buff[i];
805 
806  if (string_id == NULL_INT) {
807  values.emplace_back(NullableString(nullptr));
808  } else {
809  if (dict_key.dict_id == 0) {
810  StringDictionaryProxy* sdp = row_set_mem_owner->getLiteralStringDictProxy();
811  values.emplace_back(sdp->getString(string_id));
812  } else {
813  values.emplace_back(NullableString(
814  row_set_mem_owner
815  ->getOrAddStringDictProxy(dict_key, /*with_generation=*/false)
816  ->getString(string_id)));
817  }
818  }
819  }
820  } else {
821  for (size_t i = 0; i < num_elems; i++) {
822  values.emplace_back(static_cast<int64_t>(buff[i]));
823  }
824  }
825  return ArrayTargetValue(values);
826 }
827 
829  const SQLTypeInfo& array_ti,
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  CHECK(array_ti.is_array());
835  const auto& elem_ti = array_ti.get_elem_type();
836  if (elem_ti.is_string()) {
837  return build_string_array_target_value(reinterpret_cast<const int32_t*>(buff),
838  buff_sz,
839  elem_ti.getStringDictKey(),
840  translate_strings,
841  row_set_mem_owner);
842  }
843  switch (elem_ti.get_size()) {
844  case 1:
845  return build_array_target_value<int8_t>(buff, buff_sz, row_set_mem_owner);
846  case 2:
847  return build_array_target_value<int16_t>(buff, buff_sz, row_set_mem_owner);
848  case 4:
849  if (elem_ti.is_fp()) {
850  return build_array_target_value<float>(buff, buff_sz, row_set_mem_owner);
851  } else {
852  return build_array_target_value<int32_t>(buff, buff_sz, row_set_mem_owner);
853  }
854  case 8:
855  if (elem_ti.is_fp()) {
856  return build_array_target_value<double>(buff, buff_sz, row_set_mem_owner);
857  } else {
858  return build_array_target_value<int64_t>(buff, buff_sz, row_set_mem_owner);
859  }
860  default:
861  CHECK(false);
862  }
863  CHECK(false);
864  return TargetValue(nullptr);
865 }
866 
867 template <class Tuple, size_t... indices>
868 inline std::vector<std::pair<const int8_t*, const int64_t>> make_vals_vector(
869  std::index_sequence<indices...>,
870  const Tuple& tuple) {
871  return std::vector<std::pair<const int8_t*, const int64_t>>{
872  std::make_pair(std::get<2 * indices>(tuple), std::get<2 * indices + 1>(tuple))...};
873 }
874 
875 inline std::unique_ptr<ArrayDatum> lazy_fetch_chunk(const int8_t* ptr,
876  const int64_t varlen_ptr) {
877  auto ad = std::make_unique<ArrayDatum>();
878  bool is_end;
879  ChunkIter_get_nth(reinterpret_cast<ChunkIter*>(const_cast<int8_t*>(ptr)),
880  varlen_ptr,
881  ad.get(),
882  &is_end);
883  CHECK(!is_end);
884  return ad;
885 }
886 
888  template <typename... T>
889  static inline auto fetch(const SQLTypeInfo& geo_ti,
890  const ResultSet::GeoReturnType return_type,
891  T&&... vals) {
892  constexpr int num_vals = sizeof...(vals);
893  static_assert(
894  num_vals % 2 == 0,
895  "Must have consistent pointer/size pairs for lazy fetch of geo target values.");
896  const auto vals_vector = make_vals_vector(std::make_index_sequence<num_vals / 2>{},
897  std::make_tuple(vals...));
898  std::array<VarlenDatumPtr, num_vals / 2> ad_arr;
899  size_t ctr = 0;
900  for (const auto& col_pair : vals_vector) {
901  ad_arr[ctr] = lazy_fetch_chunk(col_pair.first, col_pair.second);
902  // Regular chunk iterator used to fetch this datum sets the right nullness.
903  // That includes the fixlen bounds array.
904  // However it may incorrectly set it for the POINT coord array datum
905  // if 1st byte happened to hold NULL_ARRAY_TINYINT. One should either use
906  // the specialized iterator for POINT coords or rely on regular iterator +
907  // reset + recheck, which is what is done below.
908  auto is_point = (geo_ti.get_type() == kPOINT && ctr == 0);
909  if (is_point) {
910  // Resetting POINT coords array nullness here
911  ad_arr[ctr]->is_null = false;
912  }
913  if (!geo_ti.get_notnull()) {
914  // Recheck and set nullness
915  if (ad_arr[ctr]->length == 0 || ad_arr[ctr]->pointer == NULL ||
916  (is_point &&
917  is_null_point(geo_ti, ad_arr[ctr]->pointer, ad_arr[ctr]->length))) {
918  ad_arr[ctr]->is_null = true;
919  }
920  }
921  ctr++;
922  }
923  return ad_arr;
924  }
925 };
926 
927 inline std::unique_ptr<ArrayDatum> fetch_data_from_gpu(int64_t varlen_ptr,
928  const int64_t length,
929  Data_Namespace::DataMgr* data_mgr,
930  const int device_id) {
931  auto cpu_buf =
932  std::shared_ptr<int8_t>(new int8_t[length], std::default_delete<int8_t[]>());
933  auto allocator = std::make_unique<CudaAllocator>(
934  data_mgr, device_id, getQueryEngineCudaStreamForDevice(device_id));
935  allocator->copyFromDevice(cpu_buf.get(), reinterpret_cast<int8_t*>(varlen_ptr), length);
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, ctr++) {
980  if (vals_vector[i] == 0) {
981  // projected null
982  CHECK(!geo_ti.get_notnull());
983  ad_arr[ctr] = std::make_unique<ArrayDatum>(0, nullptr, true);
984  continue;
985  }
986  ad_arr[ctr] = datum_fetcher(vals_vector[i], vals_vector[i + 1]);
987  // All fetched datums come in with is_null set to false
988  if (!geo_ti.get_notnull()) {
989  bool is_null = false;
990  // Now need to set the nullness
991  if (ad_arr[ctr]->length == 0 || ad_arr[ctr]->pointer == NULL) {
992  is_null = true;
993  } else if (geo_ti.get_type() == kPOINT && ctr == 0 &&
994  is_null_point(geo_ti, ad_arr[ctr]->pointer, ad_arr[ctr]->length)) {
995  is_null = true; // recognizes compressed and uncompressed points
996  } else if (ad_arr[ctr]->length == 4 * sizeof(double)) {
997  // Bounds
998  auto dti = SQLTypeInfo(kARRAY, 0, 0, false, kENCODING_NONE, 0, kDOUBLE);
999  is_null = dti.is_null_fixlen_array(ad_arr[ctr]->pointer, ad_arr[ctr]->length);
1000  }
1001  ad_arr[ctr]->is_null = is_null;
1002  }
1003  }
1004  return ad_arr;
1005  };
1006 
1007  if (fetch_data_from_gpu) {
1009  return ad_arr_generator(yieldGpuPtrFetcher());
1010  } else {
1011  return ad_arr_generator(yieldGpuDatumFetcher(data_mgr, device_id));
1012  }
1013  } else {
1014  return ad_arr_generator(yieldCpuDatumFetcher());
1015  }
1016  }
1017 };
1018 
1019 template <SQLTypes GEO_SOURCE_TYPE, typename GeoTargetFetcher>
1021  template <typename... T>
1022  static inline TargetValue build(const SQLTypeInfo& geo_ti,
1023  const ResultSet::GeoReturnType return_type,
1024  T&&... vals) {
1025  auto ad_arr = GeoTargetFetcher::fetch(geo_ti, return_type, std::forward<T>(vals)...);
1026  static_assert(std::tuple_size<decltype(ad_arr)>::value > 0,
1027  "ArrayDatum array for Geo Target must contain at least one value.");
1028 
1029  // Fetcher sets the geo nullness based on geo typeinfo's notnull, type and
1030  // compression. Serializers will generate appropriate NULL geo where necessary.
1031  switch (return_type) {
1033  if (!geo_ti.get_notnull() && ad_arr[0]->is_null) {
1034  return GeoTargetValue();
1035  }
1037  GEO_SOURCE_TYPE>::GeoSerializerType::serialize(geo_ti,
1038  ad_arr);
1039  }
1041  if (!geo_ti.get_notnull() && ad_arr[0]->is_null) {
1042  // Generating NULL wkt string to represent NULL geo
1043  return NullableString(nullptr);
1044  }
1046  GEO_SOURCE_TYPE>::GeoSerializerType::serialize(geo_ti,
1047  ad_arr);
1048  }
1051  if (!geo_ti.get_notnull() && ad_arr[0]->is_null) {
1052  // NULL geo
1053  // Pass along null datum, instead of an empty/null GeoTargetValuePtr
1054  // return GeoTargetValuePtr();
1055  }
1057  GEO_SOURCE_TYPE>::GeoSerializerType::serialize(geo_ti,
1058  ad_arr);
1059  }
1060  default: {
1061  UNREACHABLE();
1062  return TargetValue(nullptr);
1063  }
1064  }
1065  }
1066 };
1067 
1068 template <typename T>
1069 inline std::pair<int64_t, int64_t> get_frag_id_and_local_idx(
1070  const std::vector<std::vector<T>>& frag_offsets,
1071  const size_t tab_or_col_idx,
1072  const int64_t global_idx) {
1073  CHECK_GE(global_idx, int64_t(0));
1074  for (int64_t frag_id = frag_offsets.size() - 1; frag_id > 0; --frag_id) {
1075  CHECK_LT(tab_or_col_idx, frag_offsets[frag_id].size());
1076  const auto frag_off = static_cast<int64_t>(frag_offsets[frag_id][tab_or_col_idx]);
1077  if (frag_off < global_idx) {
1078  return {frag_id, global_idx - frag_off};
1079  }
1080  }
1081  return {-1, -1};
1082 }
1083 
1084 } // namespace
1085 
1086 // clang-format off
1087 // formatted by clang-format 14.0.6
1089  bool const translate_strings,
1090  int64_t const val) const {
1091  if (ti.is_string()) {
1093  return makeStringTargetValue(ti, translate_strings, val);
1094  } else {
1095  return ti.is_any<kDOUBLE>() ? ScalarTargetValue(shared::bit_cast<double>(val))
1096  : ti.is_any<kFLOAT>() ? ScalarTargetValue(shared::bit_cast<float>(val))
1097  : ScalarTargetValue(val);
1098  }
1099 }
1100 
1102  bool const translate_strings) {
1105  : ti.is_string() ? translate_strings
1106  ? ScalarTargetValue(NullableString(nullptr))
1107  : ScalarTargetValue(static_cast<int64_t>(NULL_INT))
1109 }
1110 
1112  int64_t const lhs,
1113  int64_t const rhs) const {
1114  if (ti.is_string()) {
1116  return getString(ti, lhs) < getString(ti, rhs);
1117  } else {
1118  return ti.is_any<kDOUBLE>()
1119  ? shared::bit_cast<double>(lhs) < shared::bit_cast<double>(rhs)
1120  : ti.is_any<kFLOAT>()
1121  ? shared::bit_cast<float>(lhs) < shared::bit_cast<float>(rhs)
1122  : lhs < rhs;
1123  }
1124 }
1125 
1127  bool const translate_strings,
1128  int64_t const ival) {
1129  return ti.is_any<kDOUBLE>() ? shared::bit_cast<double>(ival) == NULL_DOUBLE
1130  : ti.is_any<kFLOAT>() ? shared::bit_cast<float>(ival) == NULL_FLOAT
1131  : ti.is_string() ? translate_strings ? ival == NULL_INT : ival == 0
1132  : ival == inline_int_null_val(ti);
1133 }
1134 // clang-format on
1135 
1136 const std::vector<const int8_t*>& ResultSet::getColumnFrag(const size_t storage_idx,
1137  const size_t col_logical_idx,
1138  int64_t& global_idx) const {
1139  CHECK_LT(static_cast<size_t>(storage_idx), col_buffers_.size());
1140  if (col_buffers_[storage_idx].size() > 1) {
1141  int64_t frag_id = 0;
1142  int64_t local_idx = global_idx;
1143  if (consistent_frag_sizes_[storage_idx][col_logical_idx] != -1) {
1144  frag_id = global_idx / consistent_frag_sizes_[storage_idx][col_logical_idx];
1145  local_idx = global_idx % consistent_frag_sizes_[storage_idx][col_logical_idx];
1146  } else {
1147  std::tie(frag_id, local_idx) = get_frag_id_and_local_idx(
1148  frag_offsets_[storage_idx], col_logical_idx, global_idx);
1149  CHECK_LE(local_idx, global_idx);
1150  }
1151  CHECK_GE(frag_id, int64_t(0));
1152  CHECK_LT(static_cast<size_t>(frag_id), col_buffers_[storage_idx].size());
1153  global_idx = local_idx;
1154  return col_buffers_[storage_idx][frag_id];
1155  } else {
1156  CHECK_EQ(size_t(1), col_buffers_[storage_idx].size());
1157  return col_buffers_[storage_idx][0];
1158  }
1159 }
1160 
1161 const VarlenOutputInfo* ResultSet::getVarlenOutputInfo(const size_t entry_idx) const {
1162  auto storage_lookup_result = findStorage(entry_idx);
1163  CHECK(storage_lookup_result.storage_ptr);
1164  return storage_lookup_result.storage_ptr->getVarlenOutputInfo();
1165 }
1166 
1171 void ResultSet::copyColumnIntoBuffer(const size_t column_idx,
1172  int8_t* output_buffer,
1173  const size_t output_buffer_size) const {
1175  CHECK_LT(column_idx, query_mem_desc_.getSlotCount());
1176  CHECK(output_buffer_size > 0);
1177  CHECK(output_buffer);
1178  const auto column_width_size = query_mem_desc_.getPaddedSlotWidthBytes(column_idx);
1179  size_t out_buff_offset = 0;
1180 
1181  // the main storage:
1182  const size_t crt_storage_row_count = storage_->query_mem_desc_.getEntryCount();
1183  const size_t crt_buffer_size = crt_storage_row_count * column_width_size;
1184  const size_t column_offset = storage_->query_mem_desc_.getColOffInBytes(column_idx);
1185  const int8_t* storage_buffer = storage_->getUnderlyingBuffer() + column_offset;
1186  CHECK(crt_buffer_size <= output_buffer_size);
1187  std::memcpy(output_buffer, storage_buffer, crt_buffer_size);
1188 
1189  out_buff_offset += crt_buffer_size;
1190 
1191  // the appended storages:
1192  for (size_t i = 0; i < appended_storage_.size(); i++) {
1193  const size_t crt_storage_row_count =
1194  appended_storage_[i]->query_mem_desc_.getEntryCount();
1195  if (crt_storage_row_count == 0) {
1196  // skip an empty appended storage
1197  continue;
1198  }
1199  CHECK_LT(out_buff_offset, output_buffer_size);
1200  const size_t crt_buffer_size = crt_storage_row_count * column_width_size;
1201  const size_t column_offset =
1202  appended_storage_[i]->query_mem_desc_.getColOffInBytes(column_idx);
1203  const int8_t* storage_buffer =
1204  appended_storage_[i]->getUnderlyingBuffer() + column_offset;
1205  CHECK(out_buff_offset + crt_buffer_size <= output_buffer_size);
1206  std::memcpy(output_buffer + out_buff_offset, storage_buffer, crt_buffer_size);
1207 
1208  out_buff_offset += crt_buffer_size;
1209  }
1210 }
1211 
1212 template <typename ENTRY_TYPE, QueryDescriptionType QUERY_TYPE, bool COLUMNAR_FORMAT>
1213 ENTRY_TYPE ResultSet::getEntryAt(const size_t row_idx,
1214  const size_t target_idx,
1215  const size_t slot_idx) const {
1216  if constexpr (QUERY_TYPE == QueryDescriptionType::GroupByPerfectHash) { // NOLINT
1217  if constexpr (COLUMNAR_FORMAT) { // NOLINT
1218  return getColumnarPerfectHashEntryAt<ENTRY_TYPE>(row_idx, target_idx, slot_idx);
1219  } else {
1220  return getRowWisePerfectHashEntryAt<ENTRY_TYPE>(row_idx, target_idx, slot_idx);
1221  }
1222  } else if constexpr (QUERY_TYPE == QueryDescriptionType::GroupByBaselineHash) {
1223  if constexpr (COLUMNAR_FORMAT) { // NOLINT
1224  return getColumnarBaselineEntryAt<ENTRY_TYPE>(row_idx, target_idx, slot_idx);
1225  } else {
1226  return getRowWiseBaselineEntryAt<ENTRY_TYPE>(row_idx, target_idx, slot_idx);
1227  }
1228  } else {
1229  UNREACHABLE() << "Invalid query type is used";
1230  return 0;
1231  }
1232 }
1233 
1234 #define DEF_GET_ENTRY_AT(query_type, columnar_output) \
1235  template DATA_T ResultSet::getEntryAt<DATA_T, query_type, columnar_output>( \
1236  const size_t row_idx, const size_t target_idx, const size_t slot_idx) const;
1237 
1238 #define DATA_T int64_t
1240 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1241 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1242 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1243 #undef DATA_T
1244 
1245 #define DATA_T int32_t
1247 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1248 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1249 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1250 #undef DATA_T
1251 
1252 #define DATA_T int16_t
1254 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1255 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1256 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1257 #undef DATA_T
1258 
1259 #define DATA_T int8_t
1261 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1262 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1263 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1264 #undef DATA_T
1265 
1266 #define DATA_T float
1268 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1269 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1270 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1271 #undef DATA_T
1272 
1273 #define DATA_T double
1275 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByPerfectHash, false)
1276 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, true)
1277 DEF_GET_ENTRY_AT(QueryDescriptionType::GroupByBaselineHash, false)
1278 #undef DATA_T
1279 
1280 #undef DEF_GET_ENTRY_AT
1281 
1288 template <typename ENTRY_TYPE>
1289 ENTRY_TYPE ResultSet::getColumnarPerfectHashEntryAt(const size_t row_idx,
1290  const size_t target_idx,
1291  const size_t slot_idx) const {
1292  const size_t column_offset = storage_->query_mem_desc_.getColOffInBytes(slot_idx);
1293  const int8_t* storage_buffer = storage_->getUnderlyingBuffer() + column_offset;
1294  return reinterpret_cast<const ENTRY_TYPE*>(storage_buffer)[row_idx];
1295 }
1296 
1303 template <typename ENTRY_TYPE>
1304 ENTRY_TYPE ResultSet::getRowWisePerfectHashEntryAt(const size_t row_idx,
1305  const size_t target_idx,
1306  const size_t slot_idx) const {
1307  const size_t row_offset = storage_->query_mem_desc_.getRowSize() * row_idx;
1308  const size_t column_offset = storage_->query_mem_desc_.getColOffInBytes(slot_idx);
1309  const int8_t* storage_buffer =
1310  storage_->getUnderlyingBuffer() + row_offset + column_offset;
1311  return *reinterpret_cast<const ENTRY_TYPE*>(storage_buffer);
1312 }
1313 
1320 template <typename ENTRY_TYPE>
1321 ENTRY_TYPE ResultSet::getRowWiseBaselineEntryAt(const size_t row_idx,
1322  const size_t target_idx,
1323  const size_t slot_idx) const {
1324  CHECK_NE(storage_->query_mem_desc_.targetGroupbyIndicesSize(), size_t(0));
1325  const auto key_width = storage_->query_mem_desc_.getEffectiveKeyWidth();
1326  auto keys_ptr = row_ptr_rowwise(
1327  storage_->getUnderlyingBuffer(), storage_->query_mem_desc_, row_idx);
1328  const auto column_offset =
1329  (storage_->query_mem_desc_.getTargetGroupbyIndex(target_idx) < 0)
1330  ? storage_->query_mem_desc_.getColOffInBytes(slot_idx)
1331  : storage_->query_mem_desc_.getTargetGroupbyIndex(target_idx) * key_width;
1332  const auto storage_buffer = keys_ptr + column_offset;
1333  return *reinterpret_cast<const ENTRY_TYPE*>(storage_buffer);
1334 }
1335 
1342 template <typename ENTRY_TYPE>
1343 ENTRY_TYPE ResultSet::getColumnarBaselineEntryAt(const size_t row_idx,
1344  const size_t target_idx,
1345  const size_t slot_idx) const {
1346  CHECK_NE(storage_->query_mem_desc_.targetGroupbyIndicesSize(), size_t(0));
1347  const auto key_width = storage_->query_mem_desc_.getEffectiveKeyWidth();
1348  const auto column_offset =
1349  (storage_->query_mem_desc_.getTargetGroupbyIndex(target_idx) < 0)
1350  ? storage_->query_mem_desc_.getColOffInBytes(slot_idx)
1351  : storage_->query_mem_desc_.getTargetGroupbyIndex(target_idx) * key_width *
1352  storage_->query_mem_desc_.getEntryCount();
1353  const auto column_buffer = storage_->getUnderlyingBuffer() + column_offset;
1354  return reinterpret_cast<const ENTRY_TYPE*>(column_buffer)[row_idx];
1355 }
1356 
1357 // Interprets ptr1, ptr2 as the ptr and len pair used for variable length data.
1359  const int8_t compact_sz1,
1360  const int8_t* ptr2,
1361  const int8_t compact_sz2,
1362  const TargetInfo& target_info,
1363  const size_t target_logical_idx,
1364  const bool translate_strings,
1365  const size_t entry_buff_idx) const {
1366  auto varlen_ptr = read_int_from_buff(ptr1, compact_sz1);
1367  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1368  if (varlen_ptr < 0) {
1369  CHECK_EQ(-1, varlen_ptr);
1370  if (target_info.sql_type.get_type() == kARRAY) {
1371  return ArrayTargetValue(boost::optional<std::vector<ScalarTargetValue>>{});
1372  }
1373  return TargetValue(nullptr);
1374  }
1375  const auto storage_idx = getStorageIndex(entry_buff_idx);
1376  if (target_info.sql_type.is_string()) {
1377  CHECK(target_info.sql_type.get_compression() == kENCODING_NONE);
1378  CHECK_LT(storage_idx.first, serialized_varlen_buffer_.size());
1379  const auto& varlen_buffer_for_storage =
1380  serialized_varlen_buffer_[storage_idx.first];
1381  CHECK_LT(static_cast<size_t>(varlen_ptr), varlen_buffer_for_storage.size());
1382  return varlen_buffer_for_storage[varlen_ptr];
1383  } else if (target_info.sql_type.get_type() == kARRAY) {
1384  CHECK_LT(storage_idx.first, serialized_varlen_buffer_.size());
1385  const auto& varlen_buffer = serialized_varlen_buffer_[storage_idx.first];
1386  CHECK_LT(static_cast<size_t>(varlen_ptr), varlen_buffer.size());
1387 
1388  return build_array_target_value(
1389  target_info.sql_type,
1390  reinterpret_cast<const int8_t*>(varlen_buffer[varlen_ptr].data()),
1391  varlen_buffer[varlen_ptr].size(),
1392  translate_strings,
1394  } else {
1395  CHECK(false);
1396  }
1397  }
1398  if (!lazy_fetch_info_.empty()) {
1399  CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
1400  const auto& col_lazy_fetch = lazy_fetch_info_[target_logical_idx];
1401  if (col_lazy_fetch.is_lazily_fetched) {
1402  const auto storage_idx = getStorageIndex(entry_buff_idx);
1403  CHECK_LT(storage_idx.first, col_buffers_.size());
1404  auto& frag_col_buffers =
1405  getColumnFrag(storage_idx.first, target_logical_idx, varlen_ptr);
1406  bool is_end{false};
1407  auto col_buf = const_cast<int8_t*>(frag_col_buffers[col_lazy_fetch.local_col_id]);
1408  if (target_info.sql_type.is_string()) {
1409  if (FlatBufferManager::isFlatBuffer(col_buf)) {
1410  FlatBufferManager m{col_buf};
1411  std::string fetched_str;
1412  bool is_null{};
1413  auto status = m.getItem(varlen_ptr, fetched_str, is_null);
1414  if (is_null) {
1415  return TargetValue(nullptr);
1416  }
1417  CHECK_EQ(status, FlatBufferManager::Status::Success);
1418  return fetched_str;
1419  }
1420  VarlenDatum vd;
1422  reinterpret_cast<ChunkIter*>(col_buf), varlen_ptr, false, &vd, &is_end);
1423  CHECK(!is_end);
1424  if (vd.is_null) {
1425  return TargetValue(nullptr);
1426  }
1427  CHECK(vd.pointer);
1428  CHECK_GT(vd.length, 0u);
1429  std::string fetched_str(reinterpret_cast<char*>(vd.pointer), vd.length);
1430  return fetched_str;
1431  } else {
1432  CHECK(target_info.sql_type.is_array());
1433  ArrayDatum ad;
1434  if (FlatBufferManager::isFlatBuffer(col_buf)) {
1435  VarlenArray_get_nth(col_buf, varlen_ptr, &ad, &is_end);
1436  } else {
1438  reinterpret_cast<ChunkIter*>(col_buf), varlen_ptr, &ad, &is_end);
1439  }
1440  if (ad.is_null) {
1441  return ArrayTargetValue(boost::optional<std::vector<ScalarTargetValue>>{});
1442  }
1443  CHECK_GE(ad.length, 0u);
1444  if (ad.length > 0) {
1445  CHECK(ad.pointer);
1446  }
1447  return build_array_target_value(target_info.sql_type,
1448  ad.pointer,
1449  ad.length,
1450  translate_strings,
1452  }
1453  }
1454  }
1455  if (!varlen_ptr) {
1456  if (target_info.sql_type.is_array()) {
1457  return ArrayTargetValue(boost::optional<std::vector<ScalarTargetValue>>{});
1458  }
1459  return TargetValue(nullptr);
1460  }
1461  auto length = read_int_from_buff(ptr2, compact_sz2);
1462  if (target_info.sql_type.is_array()) {
1463  const auto& elem_ti = target_info.sql_type.get_elem_type();
1464  length *= elem_ti.get_array_context_logical_size();
1465  }
1466  std::vector<int8_t> cpu_buffer;
1467  if (varlen_ptr && device_type_ == ExecutorDeviceType::GPU) {
1468  cpu_buffer.resize(length);
1469  const auto executor = query_mem_desc_.getExecutor();
1470  CHECK(executor);
1471  auto data_mgr = executor->getDataMgr();
1472  auto allocator = std::make_unique<CudaAllocator>(
1473  data_mgr, device_id_, getQueryEngineCudaStreamForDevice(device_id_));
1474 
1475  allocator->copyFromDevice(
1476  &cpu_buffer[0], reinterpret_cast<int8_t*>(varlen_ptr), length);
1477  varlen_ptr = reinterpret_cast<int64_t>(&cpu_buffer[0]);
1478  }
1479  if (target_info.sql_type.is_array()) {
1480  return build_array_target_value(target_info.sql_type,
1481  reinterpret_cast<const int8_t*>(varlen_ptr),
1482  length,
1483  translate_strings,
1485  }
1486  return std::string(reinterpret_cast<char*>(varlen_ptr), length);
1487 }
1488 
1489 bool ResultSet::isGeoColOnGpu(const size_t col_idx) const {
1490  // This should match the logic in makeGeoTargetValue which ultimately calls
1491  // fetch_data_from_gpu when the geo column is on the device.
1492  // TODO(croot): somehow find a way to refactor this and makeGeoTargetValue to use a
1493  // utility function that handles this logic in one place
1494  CHECK_LT(col_idx, targets_.size());
1495  if (!IS_GEO(targets_[col_idx].sql_type.get_type())) {
1496  throw std::runtime_error("Column target at index " + std::to_string(col_idx) +
1497  " is not a geo column. It is of type " +
1498  targets_[col_idx].sql_type.get_type_name() + ".");
1499  }
1500 
1501  const auto& target_info = targets_[col_idx];
1502  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1503  return false;
1504  }
1505 
1506  if (!lazy_fetch_info_.empty()) {
1507  CHECK_LT(col_idx, lazy_fetch_info_.size());
1508  if (lazy_fetch_info_[col_idx].is_lazily_fetched) {
1509  return false;
1510  }
1511  }
1512 
1514 }
1515 
1516 template <size_t NDIM,
1517  typename GeospatialGeoType,
1518  typename GeoTypeTargetValue,
1519  typename GeoTypeTargetValuePtr>
1521  const int64_t index,
1522  const SQLTypeInfo& ti,
1523  const ResultSet::GeoReturnType return_type) {
1524  FlatBufferManager m{const_cast<int8_t*>(buf)};
1525  const SQLTypeInfoLite* ti_lite =
1526  reinterpret_cast<const SQLTypeInfoLite*>(m.get_user_data_buffer());
1527  if (ti_lite->is_geoint()) {
1529  } else {
1531  }
1533  auto status = m.getItem(index, item);
1534  CHECK_EQ(status, FlatBufferManager::Status::Success);
1535  if (!item.is_null) {
1536  // to ensure we can access item.sizes_buffers[...] and item.sizes_lengths[...]
1537  CHECK_EQ(item.nof_sizes, NDIM - 1);
1538  }
1539  switch (return_type) {
1541  if (item.is_null) {
1542  return NullableString(nullptr);
1543  }
1544  std::vector<double> coords;
1545  if (ti_lite->is_geoint()) {
1547  ti, item.values, 2 * item.nof_values * sizeof(int32_t));
1548  } else {
1549  const double* values_buf = reinterpret_cast<const double*>(item.values);
1550  coords.insert(coords.end(), values_buf, values_buf + 2 * item.nof_values);
1551  }
1552  if constexpr (NDIM == 1) {
1553  GeospatialGeoType obj(coords);
1554  return NullableString(obj.getWktString());
1555  } else if constexpr (NDIM == 2) {
1556  std::vector<int32_t> rings;
1557  rings.insert(rings.end(),
1558  item.sizes_buffers[0],
1559  item.sizes_buffers[0] + item.sizes_lengths[0]);
1560  GeospatialGeoType obj(coords, rings);
1561  return NullableString(obj.getWktString());
1562  } else if constexpr (NDIM == 3) {
1563  std::vector<int32_t> rings;
1564  std::vector<int32_t> poly_rings;
1565  poly_rings.insert(poly_rings.end(),
1566  item.sizes_buffers[0],
1567  item.sizes_buffers[0] + item.sizes_lengths[0]);
1568  rings.insert(rings.end(),
1569  item.sizes_buffers[1],
1570  item.sizes_buffers[1] + item.sizes_lengths[1]);
1571  GeospatialGeoType obj(coords, rings, poly_rings);
1572  return NullableString(obj.getWktString());
1573  } else {
1574  UNREACHABLE();
1575  }
1576  } break;
1578  if (item.is_null) {
1579  return GeoTargetValue();
1580  }
1581  std::vector<double> coords;
1582  if (ti_lite->is_geoint()) {
1584  ti, item.values, 2 * item.nof_values * sizeof(int32_t));
1585  } else {
1586  const double* values_buf = reinterpret_cast<const double*>(item.values);
1587  coords.insert(coords.end(), values_buf, values_buf + 2 * item.nof_values);
1588  }
1589  if constexpr (NDIM == 1) {
1590  return GeoTargetValue(GeoTypeTargetValue(coords));
1591  } else if constexpr (NDIM == 2) {
1592  std::vector<int32_t> rings;
1593  rings.insert(rings.end(),
1594  item.sizes_buffers[0],
1595  item.sizes_buffers[0] + item.sizes_lengths[0]);
1596  return GeoTargetValue(GeoTypeTargetValue(coords, rings));
1597  } else if constexpr (NDIM == 3) {
1598  std::vector<int32_t> rings;
1599  std::vector<int32_t> poly_rings;
1600  poly_rings.insert(poly_rings.end(),
1601  item.sizes_buffers[0],
1602  item.sizes_buffers[0] + item.sizes_lengths[0]);
1603  rings.insert(rings.end(),
1604  item.sizes_buffers[1],
1605  item.sizes_buffers[1] + item.sizes_lengths[1]);
1606  return GeoTargetValue(GeoTypeTargetValue(coords, rings, poly_rings));
1607  } else {
1608  UNREACHABLE();
1609  }
1610  } break;
1613  if (item.is_null) {
1614  return GeoTypeTargetValuePtr();
1615  }
1616  auto coords = std::make_shared<VarlenDatum>(
1617  item.nof_values * m.getValueSize(), item.values, false);
1618 
1619  if constexpr (NDIM == 1) {
1620  return GeoTypeTargetValuePtr({std::move(coords)});
1621  } else if constexpr (NDIM == 2) {
1622  auto rings = std::make_shared<VarlenDatum>(
1623  item.sizes_lengths[0] * sizeof(int32_t),
1624  reinterpret_cast<int8_t*>(item.sizes_buffers[0]),
1625  false);
1626  return GeoTypeTargetValuePtr({std::move(coords), std::move(rings)});
1627  } else if constexpr (NDIM == 3) {
1628  auto poly_rings = std::make_shared<VarlenDatum>(
1629  item.sizes_lengths[0] * sizeof(int32_t),
1630  reinterpret_cast<int8_t*>(item.sizes_buffers[0]),
1631  false);
1632  auto rings = std::make_shared<VarlenDatum>(
1633  item.sizes_lengths[1] * sizeof(int32_t),
1634  reinterpret_cast<int8_t*>(item.sizes_buffers[1]),
1635  false);
1636  return GeoTypeTargetValuePtr(
1637  {std::move(coords), std::move(rings), std::move(poly_rings)});
1638  } else {
1639  UNREACHABLE();
1640  }
1641  } break;
1642  default:
1643  UNREACHABLE();
1644  }
1645  return TargetValue(nullptr);
1646 }
1647 
1648 // Reads a geo value from a series of ptrs to var len types
1649 // In Columnar format, geo_target_ptr is the geo column ptr (a pointer to the beginning
1650 // of that specific geo column) and should be appropriately adjusted with the
1651 // entry_buff_idx
1652 TargetValue ResultSet::makeGeoTargetValue(const int8_t* geo_target_ptr,
1653  const size_t slot_idx,
1654  const TargetInfo& target_info,
1655  const size_t target_logical_idx,
1656  const size_t entry_buff_idx) const {
1657  CHECK(target_info.sql_type.is_geometry());
1658 
1659  auto getNextTargetBufferRowWise = [&](const size_t slot_idx, const size_t range) {
1660  return geo_target_ptr + query_mem_desc_.getPaddedColWidthForRange(slot_idx, range);
1661  };
1662 
1663  auto getNextTargetBufferColWise = [&](const size_t slot_idx, const size_t range) {
1664  const auto storage_info = findStorage(entry_buff_idx);
1665  auto crt_geo_col_ptr = geo_target_ptr;
1666  for (size_t i = slot_idx; i < slot_idx + range; i++) {
1667  crt_geo_col_ptr = advance_to_next_columnar_target_buff(
1668  crt_geo_col_ptr, storage_info.storage_ptr->query_mem_desc_, i);
1669  }
1670  // adjusting the column pointer to represent a pointer to the geo target value
1671  return crt_geo_col_ptr +
1672  storage_info.fixedup_entry_idx *
1673  storage_info.storage_ptr->query_mem_desc_.getPaddedSlotWidthBytes(
1674  slot_idx + range);
1675  };
1676 
1677  auto getNextTargetBuffer = [&](const size_t slot_idx, const size_t range) {
1679  ? getNextTargetBufferColWise(slot_idx, range)
1680  : getNextTargetBufferRowWise(slot_idx, range);
1681  };
1682 
1683  auto getCoordsDataPtr = [&](const int8_t* geo_target_ptr) {
1684  return read_int_from_buff(getNextTargetBuffer(slot_idx, 0),
1686  };
1687 
1688  auto getCoordsLength = [&](const int8_t* geo_target_ptr) {
1689  return read_int_from_buff(getNextTargetBuffer(slot_idx, 1),
1691  };
1692 
1693  auto getRingSizesPtr = [&](const int8_t* geo_target_ptr) {
1694  return read_int_from_buff(getNextTargetBuffer(slot_idx, 2),
1696  };
1697 
1698  auto getRingSizesLength = [&](const int8_t* geo_target_ptr) {
1699  return read_int_from_buff(getNextTargetBuffer(slot_idx, 3),
1701  };
1702 
1703  auto getPolyRingsPtr = [&](const int8_t* geo_target_ptr) {
1704  return read_int_from_buff(getNextTargetBuffer(slot_idx, 4),
1706  };
1707 
1708  auto getPolyRingsLength = [&](const int8_t* geo_target_ptr) {
1709  return read_int_from_buff(getNextTargetBuffer(slot_idx, 5),
1711  };
1712 
1713  auto getFragColBuffers = [&]() -> decltype(auto) {
1714  const auto storage_idx = getStorageIndex(entry_buff_idx);
1715  CHECK_LT(storage_idx.first, col_buffers_.size());
1716  auto global_idx = getCoordsDataPtr(geo_target_ptr);
1717  return getColumnFrag(storage_idx.first, target_logical_idx, global_idx);
1718  };
1719 
1720  const bool is_gpu_fetch = device_type_ == ExecutorDeviceType::GPU;
1721 
1722  auto getDataMgr = [&]() {
1723  auto executor = query_mem_desc_.getExecutor();
1724  CHECK(executor);
1725  return executor->getDataMgr();
1726  };
1727 
1728  auto getSeparateVarlenStorage = [&]() -> decltype(auto) {
1729  const auto storage_idx = getStorageIndex(entry_buff_idx);
1730  CHECK_LT(storage_idx.first, serialized_varlen_buffer_.size());
1731  const auto& varlen_buffer = serialized_varlen_buffer_[storage_idx.first];
1732  return varlen_buffer;
1733  };
1734 
1735  if (separate_varlen_storage_valid_ && getCoordsDataPtr(geo_target_ptr) < 0) {
1736  CHECK_EQ(-1, getCoordsDataPtr(geo_target_ptr));
1737  return TargetValue(nullptr);
1738  }
1739 
1740  const ColumnLazyFetchInfo* col_lazy_fetch = nullptr;
1741  if (!lazy_fetch_info_.empty()) {
1742  CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
1743  col_lazy_fetch = &lazy_fetch_info_[target_logical_idx];
1744  }
1745 
1746  switch (target_info.sql_type.get_type()) {
1747  case kPOINT: {
1748  if (query_mem_desc_.slotIsVarlenOutput(slot_idx)) {
1749  auto varlen_output_info = getVarlenOutputInfo(entry_buff_idx);
1750  CHECK(varlen_output_info);
1751  auto geo_data_ptr = read_int_from_buff(
1752  geo_target_ptr, query_mem_desc_.getPaddedSlotWidthBytes(slot_idx));
1753  auto cpu_data_ptr =
1754  reinterpret_cast<int64_t>(varlen_output_info->computeCpuOffset(geo_data_ptr));
1755  return GeoTargetValueBuilder<kPOINT, GeoQueryOutputFetchHandler>::build(
1756  target_info.sql_type,
1758  /*data_mgr=*/nullptr,
1759  /*is_gpu_fetch=*/false,
1760  device_id_,
1761  cpu_data_ptr,
1762  target_info.sql_type.get_compression() == kENCODING_GEOINT ? 8 : 16);
1763  } else if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1764  const auto& varlen_buffer = getSeparateVarlenStorage();
1765  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr)),
1766  varlen_buffer.size());
1767 
1768  return GeoTargetValueBuilder<kPOINT, GeoQueryOutputFetchHandler>::build(
1769  target_info.sql_type,
1771  nullptr,
1772  false,
1773  device_id_,
1774  reinterpret_cast<int64_t>(
1775  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1776  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()));
1777  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1778  const auto& frag_col_buffers = getFragColBuffers();
1779  return GeoTargetValueBuilder<kPOINT, GeoLazyFetchHandler>::build(
1780  target_info.sql_type,
1782  frag_col_buffers[col_lazy_fetch->local_col_id],
1783  getCoordsDataPtr(geo_target_ptr));
1784  } else {
1785  return GeoTargetValueBuilder<kPOINT, GeoQueryOutputFetchHandler>::build(
1786  target_info.sql_type,
1788  is_gpu_fetch ? getDataMgr() : nullptr,
1789  is_gpu_fetch,
1790  device_id_,
1791  getCoordsDataPtr(geo_target_ptr),
1792  getCoordsLength(geo_target_ptr));
1793  }
1794  break;
1795  }
1796  case kMULTIPOINT: {
1797  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1798  const auto& varlen_buffer = getSeparateVarlenStorage();
1799  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr)),
1800  varlen_buffer.size());
1801 
1802  return GeoTargetValueBuilder<kMULTIPOINT, GeoQueryOutputFetchHandler>::build(
1803  target_info.sql_type,
1805  nullptr,
1806  false,
1807  device_id_,
1808  reinterpret_cast<int64_t>(
1809  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1810  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()));
1811  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1812  const auto& frag_col_buffers = getFragColBuffers();
1813 
1814  auto ptr = frag_col_buffers[col_lazy_fetch->local_col_id];
1816  int64_t index = getCoordsDataPtr(geo_target_ptr);
1817  return NestedArrayToGeoTargetValue<1,
1821  ptr, index, target_info.sql_type, geo_return_type_);
1822  }
1823  return GeoTargetValueBuilder<kMULTIPOINT, GeoLazyFetchHandler>::build(
1824  target_info.sql_type,
1826  frag_col_buffers[col_lazy_fetch->local_col_id],
1827  getCoordsDataPtr(geo_target_ptr));
1828  } else {
1829  return GeoTargetValueBuilder<kMULTIPOINT, GeoQueryOutputFetchHandler>::build(
1830  target_info.sql_type,
1832  is_gpu_fetch ? getDataMgr() : nullptr,
1833  is_gpu_fetch,
1834  device_id_,
1835  getCoordsDataPtr(geo_target_ptr),
1836  getCoordsLength(geo_target_ptr));
1837  }
1838  break;
1839  }
1840  case kLINESTRING: {
1841  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1842  const auto& varlen_buffer = getSeparateVarlenStorage();
1843  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr)),
1844  varlen_buffer.size());
1845 
1846  return GeoTargetValueBuilder<kLINESTRING, GeoQueryOutputFetchHandler>::build(
1847  target_info.sql_type,
1849  nullptr,
1850  false,
1851  device_id_,
1852  reinterpret_cast<int64_t>(
1853  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1854  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()));
1855  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1856  const auto& frag_col_buffers = getFragColBuffers();
1857 
1858  auto ptr = frag_col_buffers[col_lazy_fetch->local_col_id];
1860  int64_t index = getCoordsDataPtr(geo_target_ptr);
1861  return NestedArrayToGeoTargetValue<1,
1865  ptr, index, target_info.sql_type, geo_return_type_);
1866  }
1867  return GeoTargetValueBuilder<kLINESTRING, GeoLazyFetchHandler>::build(
1868  target_info.sql_type,
1870  frag_col_buffers[col_lazy_fetch->local_col_id],
1871  getCoordsDataPtr(geo_target_ptr));
1872  } else {
1873  return GeoTargetValueBuilder<kLINESTRING, GeoQueryOutputFetchHandler>::build(
1874  target_info.sql_type,
1876  is_gpu_fetch ? getDataMgr() : nullptr,
1877  is_gpu_fetch,
1878  device_id_,
1879  getCoordsDataPtr(geo_target_ptr),
1880  getCoordsLength(geo_target_ptr));
1881  }
1882  break;
1883  }
1884  case kMULTILINESTRING: {
1885  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1886  const auto& varlen_buffer = getSeparateVarlenStorage();
1887  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr) + 1),
1888  varlen_buffer.size());
1889 
1890  return GeoTargetValueBuilder<kMULTILINESTRING, GeoQueryOutputFetchHandler>::build(
1891  target_info.sql_type,
1893  nullptr,
1894  false,
1895  device_id_,
1896  reinterpret_cast<int64_t>(
1897  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1898  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()),
1899  reinterpret_cast<int64_t>(
1900  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].data()),
1901  static_cast<int64_t>(
1902  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].size()));
1903  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1904  const auto& frag_col_buffers = getFragColBuffers();
1905 
1906  auto ptr = frag_col_buffers[col_lazy_fetch->local_col_id];
1908  int64_t index = getCoordsDataPtr(geo_target_ptr);
1909  return NestedArrayToGeoTargetValue<2,
1913  ptr, index, target_info.sql_type, geo_return_type_);
1914  }
1915 
1916  return GeoTargetValueBuilder<kMULTILINESTRING, GeoLazyFetchHandler>::build(
1917  target_info.sql_type,
1919  frag_col_buffers[col_lazy_fetch->local_col_id],
1920  getCoordsDataPtr(geo_target_ptr),
1921  frag_col_buffers[col_lazy_fetch->local_col_id + 1],
1922  getCoordsDataPtr(geo_target_ptr));
1923  } else {
1924  return GeoTargetValueBuilder<kMULTILINESTRING, GeoQueryOutputFetchHandler>::build(
1925  target_info.sql_type,
1927  is_gpu_fetch ? getDataMgr() : nullptr,
1928  is_gpu_fetch,
1929  device_id_,
1930  getCoordsDataPtr(geo_target_ptr),
1931  getCoordsLength(geo_target_ptr),
1932  getRingSizesPtr(geo_target_ptr),
1933  getRingSizesLength(geo_target_ptr) * 4);
1934  }
1935  break;
1936  }
1937  case kPOLYGON: {
1938  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1939  const auto& varlen_buffer = getSeparateVarlenStorage();
1940  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr) + 1),
1941  varlen_buffer.size());
1942 
1943  return GeoTargetValueBuilder<kPOLYGON, GeoQueryOutputFetchHandler>::build(
1944  target_info.sql_type,
1946  nullptr,
1947  false,
1948  device_id_,
1949  reinterpret_cast<int64_t>(
1950  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
1951  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()),
1952  reinterpret_cast<int64_t>(
1953  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].data()),
1954  static_cast<int64_t>(
1955  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].size()));
1956  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
1957  const auto& frag_col_buffers = getFragColBuffers();
1958  auto ptr = frag_col_buffers[col_lazy_fetch->local_col_id];
1960  int64_t index = getCoordsDataPtr(geo_target_ptr);
1961  return NestedArrayToGeoTargetValue<2,
1965  ptr, index, target_info.sql_type, geo_return_type_);
1966  }
1967 
1968  return GeoTargetValueBuilder<kPOLYGON, GeoLazyFetchHandler>::build(
1969  target_info.sql_type,
1971  frag_col_buffers[col_lazy_fetch->local_col_id],
1972  getCoordsDataPtr(geo_target_ptr),
1973  frag_col_buffers[col_lazy_fetch->local_col_id + 1],
1974  getCoordsDataPtr(geo_target_ptr));
1975  } else {
1976  return GeoTargetValueBuilder<kPOLYGON, GeoQueryOutputFetchHandler>::build(
1977  target_info.sql_type,
1979  is_gpu_fetch ? getDataMgr() : nullptr,
1980  is_gpu_fetch,
1981  device_id_,
1982  getCoordsDataPtr(geo_target_ptr),
1983  getCoordsLength(geo_target_ptr),
1984  getRingSizesPtr(geo_target_ptr),
1985  getRingSizesLength(geo_target_ptr) * 4);
1986  }
1987  break;
1988  }
1989  case kMULTIPOLYGON: {
1990  if (separate_varlen_storage_valid_ && !target_info.is_agg) {
1991  const auto& varlen_buffer = getSeparateVarlenStorage();
1992  CHECK_LT(static_cast<size_t>(getCoordsDataPtr(geo_target_ptr) + 2),
1993  varlen_buffer.size());
1994 
1995  return GeoTargetValueBuilder<kMULTIPOLYGON, GeoQueryOutputFetchHandler>::build(
1996  target_info.sql_type,
1998  nullptr,
1999  false,
2000  device_id_,
2001  reinterpret_cast<int64_t>(
2002  varlen_buffer[getCoordsDataPtr(geo_target_ptr)].data()),
2003  static_cast<int64_t>(varlen_buffer[getCoordsDataPtr(geo_target_ptr)].size()),
2004  reinterpret_cast<int64_t>(
2005  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].data()),
2006  static_cast<int64_t>(
2007  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 1].size()),
2008  reinterpret_cast<int64_t>(
2009  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 2].data()),
2010  static_cast<int64_t>(
2011  varlen_buffer[getCoordsDataPtr(geo_target_ptr) + 2].size()));
2012  } else if (col_lazy_fetch && col_lazy_fetch->is_lazily_fetched) {
2013  const auto& frag_col_buffers = getFragColBuffers();
2014  auto ptr = frag_col_buffers[col_lazy_fetch->local_col_id];
2016  int64_t index = getCoordsDataPtr(geo_target_ptr);
2017  return NestedArrayToGeoTargetValue<3,
2021  ptr, index, target_info.sql_type, geo_return_type_);
2022  }
2023 
2024  return GeoTargetValueBuilder<kMULTIPOLYGON, GeoLazyFetchHandler>::build(
2025  target_info.sql_type,
2027  frag_col_buffers[col_lazy_fetch->local_col_id],
2028  getCoordsDataPtr(geo_target_ptr),
2029  frag_col_buffers[col_lazy_fetch->local_col_id + 1],
2030  getCoordsDataPtr(geo_target_ptr),
2031  frag_col_buffers[col_lazy_fetch->local_col_id + 2],
2032  getCoordsDataPtr(geo_target_ptr));
2033  } else {
2034  return GeoTargetValueBuilder<kMULTIPOLYGON, GeoQueryOutputFetchHandler>::build(
2035  target_info.sql_type,
2037  is_gpu_fetch ? getDataMgr() : nullptr,
2038  is_gpu_fetch,
2039  device_id_,
2040  getCoordsDataPtr(geo_target_ptr),
2041  getCoordsLength(geo_target_ptr),
2042  getRingSizesPtr(geo_target_ptr),
2043  getRingSizesLength(geo_target_ptr) * 4,
2044  getPolyRingsPtr(geo_target_ptr),
2045  getPolyRingsLength(geo_target_ptr) * 4);
2046  }
2047  break;
2048  }
2049  default:
2050  throw std::runtime_error("Unknown Geometry type encountered: " +
2051  target_info.sql_type.get_type_name());
2052  }
2053  UNREACHABLE();
2054  return TargetValue(nullptr);
2055 }
2056 
2057 std::string ResultSet::getString(SQLTypeInfo const& ti, int64_t const ival) const {
2058  const auto& dict_key = ti.getStringDictKey();
2059  StringDictionaryProxy* sdp;
2060  if (dict_key.dict_id) {
2061  constexpr bool with_generation = false;
2062  sdp = dict_key.db_id > 0
2063  ? row_set_mem_owner_->getOrAddStringDictProxy(dict_key, with_generation)
2064  : row_set_mem_owner_->getStringDictProxy(
2065  dict_key); // unit tests bypass the catalog
2066  } else {
2067  sdp = row_set_mem_owner_->getLiteralStringDictProxy();
2068  }
2069  return sdp->getString(ival);
2070 }
2071 
2073  bool const translate_strings,
2074  int64_t const ival) const {
2075  if (translate_strings) {
2076  if (static_cast<int32_t>(ival) == NULL_INT) { // TODO(alex): this isn't nice, fix it
2077  return NullableString(nullptr);
2078  } else {
2079  return NullableString(getString(chosen_type, ival));
2080  }
2081  } else {
2082  return static_cast<int64_t>(static_cast<int32_t>(ival));
2083  }
2084 }
2085 
2086 // Reads an integer or a float from ptr based on the type and the byte width.
2088  const int8_t compact_sz,
2089  const TargetInfo& target_info,
2090  const size_t target_logical_idx,
2091  const bool translate_strings,
2092  const bool decimal_to_double,
2093  const size_t entry_buff_idx) const {
2094  auto actual_compact_sz = compact_sz;
2095  const auto& type_info = target_info.sql_type;
2096  if (type_info.get_type() == kFLOAT && !query_mem_desc_.forceFourByteFloat()) {
2098  actual_compact_sz = sizeof(float);
2099  } else {
2100  actual_compact_sz = sizeof(double);
2101  }
2102  if (target_info.is_agg &&
2103  (target_info.agg_kind == kAVG || target_info.agg_kind == kSUM ||
2104  target_info.agg_kind == kSUM_IF || target_info.agg_kind == kMIN ||
2105  target_info.agg_kind == kMAX || target_info.agg_kind == kSINGLE_VALUE)) {
2106  // The above listed aggregates use two floats in a single 8-byte slot. Set the
2107  // padded size to 4 bytes to properly read each value.
2108  actual_compact_sz = sizeof(float);
2109  }
2110  }
2111  if (get_compact_type(target_info).is_date_in_days()) {
2112  // Dates encoded in days are converted to 8 byte values on read.
2113  actual_compact_sz = sizeof(int64_t);
2114  }
2115 
2116  // String dictionary keys are read as 32-bit values regardless of encoding
2117  if (type_info.is_string() && type_info.get_compression() == kENCODING_DICT &&
2118  type_info.getStringDictKey().dict_id) {
2119  actual_compact_sz = sizeof(int32_t);
2120  }
2121 
2122  auto ival = read_int_from_buff(ptr, actual_compact_sz);
2123  const auto& chosen_type = get_compact_type(target_info);
2124  if (!lazy_fetch_info_.empty()) {
2125  CHECK_LT(target_logical_idx, lazy_fetch_info_.size());
2126  const auto& col_lazy_fetch = lazy_fetch_info_[target_logical_idx];
2127  if (col_lazy_fetch.is_lazily_fetched) {
2128  CHECK_GE(ival, 0);
2129  const auto storage_idx = getStorageIndex(entry_buff_idx);
2130  CHECK_LT(storage_idx.first, col_buffers_.size());
2131  auto& frag_col_buffers = getColumnFrag(storage_idx.first, target_logical_idx, ival);
2132  CHECK_LT(size_t(col_lazy_fetch.local_col_id), frag_col_buffers.size());
2133  ival = result_set::lazy_decode(
2134  col_lazy_fetch, frag_col_buffers[col_lazy_fetch.local_col_id], ival);
2135  if (chosen_type.is_fp()) {
2136  const auto dval = *reinterpret_cast<const double*>(may_alias_ptr(&ival));
2137  if (chosen_type.get_type() == kFLOAT) {
2138  return ScalarTargetValue(static_cast<float>(dval));
2139  } else {
2140  return ScalarTargetValue(dval);
2141  }
2142  }
2143  }
2144  }
2145  if (target_info.agg_kind == kMODE) {
2146  if (!isNullIval(chosen_type, translate_strings, ival)) {
2147  auto const* const* const agg_mode = reinterpret_cast<AggMode const* const*>(ptr);
2148  if (std::optional<int64_t> const mode = (*agg_mode)->mode()) {
2149  return convertToScalarTargetValue(chosen_type, translate_strings, *mode);
2150  }
2151  }
2152  return nullScalarTargetValue(chosen_type, translate_strings);
2153  }
2154  if (chosen_type.is_fp()) {
2155  if (target_info.agg_kind == kAPPROX_QUANTILE) {
2156  return *reinterpret_cast<double const*>(ptr) == NULL_DOUBLE
2157  ? NULL_DOUBLE // sql_validate / just_validate
2158  : calculateQuantile(*reinterpret_cast<quantile::TDigest* const*>(ptr));
2159  }
2160  switch (actual_compact_sz) {
2161  case 8: {
2162  const auto dval = *reinterpret_cast<const double*>(ptr);
2163  return chosen_type.get_type() == kFLOAT
2164  ? ScalarTargetValue(static_cast<const float>(dval))
2165  : ScalarTargetValue(dval);
2166  }
2167  case 4: {
2168  CHECK_EQ(kFLOAT, chosen_type.get_type());
2169  return *reinterpret_cast<const float*>(ptr);
2170  }
2171  default:
2172  CHECK(false);
2173  }
2174  }
2175  if (chosen_type.is_integer() || chosen_type.is_boolean() || chosen_type.is_time() ||
2176  chosen_type.is_timeinterval()) {
2177  if (is_distinct_target(target_info)) {
2179  ival, query_mem_desc_.getCountDistinctDescriptor(target_logical_idx)));
2180  }
2181  // TODO(alex): remove int_resize_cast, make read_int_from_buff return the
2182  // right type instead
2183  if (inline_int_null_val(chosen_type) ==
2184  int_resize_cast(ival, chosen_type.get_logical_size())) {
2185  return inline_int_null_val(type_info);
2186  }
2187  return ival;
2188  }
2189  if (chosen_type.is_string() && chosen_type.get_compression() == kENCODING_DICT) {
2190  return makeStringTargetValue(chosen_type, translate_strings, ival);
2191  }
2192  if (chosen_type.is_decimal()) {
2193  if (decimal_to_double) {
2194  if (target_info.is_agg &&
2195  (target_info.agg_kind == kAVG || target_info.agg_kind == kSUM ||
2196  target_info.agg_kind == kSUM_IF || target_info.agg_kind == kMIN ||
2197  target_info.agg_kind == kMAX) &&
2198  ival == inline_int_null_val(SQLTypeInfo(kBIGINT, false))) {
2199  return NULL_DOUBLE;
2200  }
2201  if (!chosen_type.get_notnull() &&
2202  ival ==
2203  inline_int_null_val(SQLTypeInfo(decimal_to_int_type(chosen_type), false))) {
2204  return NULL_DOUBLE;
2205  }
2206  return static_cast<double>(ival) / exp_to_scale(chosen_type.get_scale());
2207  }
2208  return ival;
2209  }
2210  CHECK(false);
2211  return TargetValue(int64_t(0));
2212 }
2213 
2215  const int8_t* col_ptr,
2216  const TargetInfo& target_info,
2217  const size_t slot_idx,
2218  const size_t target_logical_idx,
2219  const size_t global_entry_idx,
2220  const size_t local_entry_idx,
2221  const bool translate_strings,
2222  const std::shared_ptr<RowSetMemoryOwner>& row_set_mem_owner_) {
2224  FlatBufferManager m{const_cast<int8_t*>(col_ptr)};
2225  FlatBufferManager::Status status{};
2226  CHECK(m.isNestedArray());
2227  switch (target_info.sql_type.get_type()) {
2228  case kARRAY: {
2229  ArrayDatum ad;
2231  status = m.getItem(local_entry_idx, item);
2232  if (status == FlatBufferManager::Status::Success) {
2233  ad.length = item.nof_values * m.getValueSize();
2234  ad.pointer = item.values;
2235  ad.is_null = item.is_null;
2236  } else {
2237  ad.length = 0;
2238  ad.pointer = NULL;
2239  ad.is_null = true;
2240  CHECK_EQ(status, FlatBufferManager::Status::ItemUnspecifiedError);
2241  }
2242  if (ad.is_null) {
2243  return ArrayTargetValue(boost::optional<std::vector<ScalarTargetValue>>{});
2244  }
2245  CHECK_GE(ad.length, 0u);
2246  if (ad.length > 0) {
2247  CHECK(ad.pointer);
2248  }
2249  return build_array_target_value(target_info.sql_type,
2250  ad.pointer,
2251  ad.length,
2252  translate_strings,
2253  row_set_mem_owner_);
2254  } break;
2255  default:
2256  UNREACHABLE() << "ti=" << target_info.sql_type;
2257  }
2258  CHECK(false);
2259  return {};
2260 }
2261 
2262 // Gets the TargetValue stored at position local_entry_idx in the col1_ptr and col2_ptr
2263 // column buffers. The second column is only used for AVG.
2264 // the global_entry_idx is passed to makeTargetValue to be used for
2265 // final lazy fetch (if there's any).
2267  const int8_t* col_ptr,
2268  const int8_t* keys_ptr,
2270  const size_t local_entry_idx,
2271  const size_t global_entry_idx,
2272  const TargetInfo& target_info,
2273  const size_t target_logical_idx,
2274  const size_t slot_idx,
2275  const bool translate_strings,
2276  const bool decimal_to_double) const {
2278  const auto col1_ptr = col_ptr;
2279  if (target_info.sql_type.usesFlatBuffer()) {
2281  << "target_info.sql_type=" << target_info.sql_type;
2282  return getTargetValueFromFlatBuffer(col_ptr,
2283  target_info,
2284  slot_idx,
2285  target_logical_idx,
2286  global_entry_idx,
2287  local_entry_idx,
2288  translate_strings,
2289  row_set_mem_owner_);
2290  }
2291  const auto compact_sz1 = query_mem_desc.getPaddedSlotWidthBytes(slot_idx);
2292  const auto next_col_ptr =
2293  advance_to_next_columnar_target_buff(col1_ptr, query_mem_desc, slot_idx);
2294  const auto col2_ptr = ((target_info.is_agg && target_info.agg_kind == kAVG) ||
2295  is_real_str_or_array(target_info))
2296  ? next_col_ptr
2297  : nullptr;
2298  const auto compact_sz2 = ((target_info.is_agg && target_info.agg_kind == kAVG) ||
2299  is_real_str_or_array(target_info))
2300  ? query_mem_desc.getPaddedSlotWidthBytes(slot_idx + 1)
2301  : 0;
2302  // TODO(Saman): add required logics for count distinct
2303  // geospatial target values:
2304  if (target_info.sql_type.is_geometry()) {
2305  return makeGeoTargetValue(
2306  col1_ptr, slot_idx, target_info, target_logical_idx, global_entry_idx);
2307  }
2308 
2309  const auto ptr1 = columnar_elem_ptr(local_entry_idx, col1_ptr, compact_sz1);
2310  if (target_info.agg_kind == kAVG || is_real_str_or_array(target_info)) {
2311  CHECK(col2_ptr);
2312  CHECK(compact_sz2);
2313  const auto ptr2 = columnar_elem_ptr(local_entry_idx, col2_ptr, compact_sz2);
2314  return target_info.agg_kind == kAVG
2315  ? make_avg_target_value(ptr1, compact_sz1, ptr2, compact_sz2, target_info)
2316  : makeVarlenTargetValue(ptr1,
2317  compact_sz1,
2318  ptr2,
2319  compact_sz2,
2320  target_info,
2321  target_logical_idx,
2322  translate_strings,
2323  global_entry_idx);
2324  }
2326  query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) < 0) {
2327  return makeTargetValue(ptr1,
2328  compact_sz1,
2329  target_info,
2330  target_logical_idx,
2331  translate_strings,
2332  decimal_to_double,
2333  global_entry_idx);
2334  }
2335  const auto key_width = query_mem_desc_.getEffectiveKeyWidth();
2336  const auto key_idx = query_mem_desc_.getTargetGroupbyIndex(target_logical_idx);
2337  CHECK_GE(key_idx, 0);
2338  auto key_col_ptr = keys_ptr + key_idx * query_mem_desc_.getEntryCount() * key_width;
2339  return makeTargetValue(columnar_elem_ptr(local_entry_idx, key_col_ptr, key_width),
2340  key_width,
2341  target_info,
2342  target_logical_idx,
2343  translate_strings,
2344  decimal_to_double,
2345  global_entry_idx);
2346 }
2347 
2348 // Gets the TargetValue stored in slot_idx (and slot_idx for AVG) of
2349 // rowwise_target_ptr.
2351  int8_t* rowwise_target_ptr,
2352  int8_t* keys_ptr,
2353  const size_t entry_buff_idx,
2354  const TargetInfo& target_info,
2355  const size_t target_logical_idx,
2356  const size_t slot_idx,
2357  const bool translate_strings,
2358  const bool decimal_to_double,
2359  const bool fixup_count_distinct_pointers) const {
2360  // FlatBuffer can exists only in a columnar storage. If the
2361  // following check fails it means that storage specific attributes
2362  // of type info have leaked.
2363  CHECK(!target_info.sql_type.usesFlatBuffer());
2364 
2365  if (UNLIKELY(fixup_count_distinct_pointers)) {
2366  if (is_distinct_target(target_info)) {
2367  auto count_distinct_ptr_ptr = reinterpret_cast<int64_t*>(rowwise_target_ptr);
2368  const auto remote_ptr = *count_distinct_ptr_ptr;
2369  if (remote_ptr) {
2370  const auto ptr = storage_->mappedPtr(remote_ptr);
2371  if (ptr) {
2372  *count_distinct_ptr_ptr = ptr;
2373  } else {
2374  // need to create a zero filled buffer for this remote_ptr
2375  const auto& count_distinct_desc =
2376  query_mem_desc_.count_distinct_descriptors_[target_logical_idx];
2377  const auto bitmap_byte_sz = count_distinct_desc.sub_bitmap_count == 1
2378  ? count_distinct_desc.bitmapSizeBytes()
2379  : count_distinct_desc.bitmapPaddedSizeBytes();
2380  constexpr size_t thread_idx{0};
2381  row_set_mem_owner_->initCountDistinctBufferAllocator(bitmap_byte_sz,
2382  thread_idx);
2383  auto count_distinct_buffer =
2384  row_set_mem_owner_->allocateCountDistinctBuffer(bitmap_byte_sz, thread_idx);
2385  *count_distinct_ptr_ptr = reinterpret_cast<int64_t>(count_distinct_buffer);
2386  }
2387  }
2388  }
2389  return int64_t(0);
2390  }
2391  if (target_info.sql_type.is_geometry()) {
2392  return makeGeoTargetValue(
2393  rowwise_target_ptr, slot_idx, target_info, target_logical_idx, entry_buff_idx);
2394  }
2395 
2396  auto ptr1 = rowwise_target_ptr;
2397  int8_t compact_sz1 = query_mem_desc_.getPaddedSlotWidthBytes(slot_idx);
2399  !query_mem_desc_.hasKeylessHash() && !target_info.is_agg) {
2400  // Single column perfect hash group by can utilize one slot for both the key and the
2401  // target value if both values fit in 8 bytes. Use the target value actual size for
2402  // this case. If they don't, the target value should be 8 bytes, so we can still use
2403  // the actual size rather than the compact size.
2404  compact_sz1 = query_mem_desc_.getLogicalSlotWidthBytes(slot_idx);
2405  }
2406 
2407  // logic for deciding width of column
2408  if (target_info.agg_kind == kAVG || is_real_str_or_array(target_info)) {
2409  const auto ptr2 =
2410  rowwise_target_ptr + query_mem_desc_.getPaddedSlotWidthBytes(slot_idx);
2411  int8_t compact_sz2 = 0;
2412  // Skip reading the second slot if we have a none encoded string and are using
2413  // the none encoded strings buffer attached to ResultSetStorage
2415  (target_info.sql_type.is_array() ||
2416  (target_info.sql_type.is_string() &&
2417  target_info.sql_type.get_compression() == kENCODING_NONE)))) {
2418  compact_sz2 = query_mem_desc_.getPaddedSlotWidthBytes(slot_idx + 1);
2419  }
2420  if (separate_varlen_storage_valid_ && target_info.is_agg) {
2421  compact_sz2 = 8; // TODO(adb): is there a better way to do this?
2422  }
2423  CHECK(ptr2);
2424  return target_info.agg_kind == kAVG
2425  ? make_avg_target_value(ptr1, compact_sz1, ptr2, compact_sz2, target_info)
2426  : makeVarlenTargetValue(ptr1,
2427  compact_sz1,
2428  ptr2,
2429  compact_sz2,
2430  target_info,
2431  target_logical_idx,
2432  translate_strings,
2433  entry_buff_idx);
2434  }
2436  query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) < 0) {
2437  return makeTargetValue(ptr1,
2438  compact_sz1,
2439  target_info,
2440  target_logical_idx,
2441  translate_strings,
2442  decimal_to_double,
2443  entry_buff_idx);
2444  }
2445  const auto key_width = query_mem_desc_.getEffectiveKeyWidth();
2446  ptr1 = keys_ptr + query_mem_desc_.getTargetGroupbyIndex(target_logical_idx) * key_width;
2447  return makeTargetValue(ptr1,
2448  key_width,
2449  target_info,
2450  target_logical_idx,
2451  translate_strings,
2452  decimal_to_double,
2453  entry_buff_idx);
2454 }
2455 
2456 // Returns true iff the entry at position entry_idx in buff contains a valid row.
2457 bool ResultSetStorage::isEmptyEntry(const size_t entry_idx, const int8_t* buff) const {
2460  return false;
2461  }
2463  return isEmptyEntryColumnar(entry_idx, buff);
2464  }
2469  CHECK_LT(static_cast<size_t>(query_mem_desc_.getTargetIdxForKey()),
2470  target_init_vals_.size());
2471  const auto rowwise_target_ptr = row_ptr_rowwise(buff, query_mem_desc_, entry_idx);
2472  const auto target_slot_off = result_set::get_byteoff_of_slot(
2474  return read_int_from_buff(rowwise_target_ptr + target_slot_off,
2477  target_init_vals_[query_mem_desc_.getTargetIdxForKey()];
2478  } else {
2479  const auto keys_ptr = row_ptr_rowwise(buff, query_mem_desc_, entry_idx);
2481  case 4:
2484  return *reinterpret_cast<const int32_t*>(keys_ptr) == EMPTY_KEY_32;
2485  case 8:
2486  return *reinterpret_cast<const int64_t*>(keys_ptr) == EMPTY_KEY_64;
2487  default:
2488  CHECK(false);
2489  return true;
2490  }
2491  }
2492 }
2493 
2494 /*
2495  * Returns true if the entry contain empty keys
2496  * This function should only be used with columnar format.
2497  */
2498 bool ResultSetStorage::isEmptyEntryColumnar(const size_t entry_idx,
2499  const int8_t* buff) const {
2503  return false;
2504  }
2506  // For table functions the entry count should always be set to the actual output size
2507  // (i.e. there are not empty entries), so just assume value is non-empty
2508  CHECK_LT(entry_idx, getEntryCount());
2509  return false;
2510  }
2515  CHECK_LT(static_cast<size_t>(query_mem_desc_.getTargetIdxForKey()),
2516  target_init_vals_.size());
2517  const auto col_buff = advance_col_buff_to_slot(
2519  const auto entry_buff =
2520  col_buff + entry_idx * query_mem_desc_.getPaddedSlotWidthBytes(
2522  return read_int_from_buff(entry_buff,
2525  target_init_vals_[query_mem_desc_.getTargetIdxForKey()];
2526  } else {
2527  // it's enough to find the first group key which is empty
2529  return reinterpret_cast<const int64_t*>(buff)[entry_idx] == EMPTY_KEY_64;
2530  } else {
2532  const auto target_buff = buff + query_mem_desc_.getPrependedGroupColOffInBytes(0);
2533  switch (query_mem_desc_.groupColWidth(0)) {
2534  case 8:
2535  return reinterpret_cast<const int64_t*>(target_buff)[entry_idx] == EMPTY_KEY_64;
2536  case 4:
2537  return reinterpret_cast<const int32_t*>(target_buff)[entry_idx] == EMPTY_KEY_32;
2538  case 2:
2539  return reinterpret_cast<const int16_t*>(target_buff)[entry_idx] == EMPTY_KEY_16;
2540  case 1:
2541  return reinterpret_cast<const int8_t*>(target_buff)[entry_idx] == EMPTY_KEY_8;
2542  default:
2543  CHECK(false);
2544  }
2545  }
2546  return false;
2547  }
2548  return false;
2549 }
2550 
2551 namespace {
2552 
2553 template <typename T>
2554 inline size_t make_bin_search(size_t l, size_t r, T&& is_empty_fn) {
2555  // Avoid search if there are no empty keys.
2556  if (!is_empty_fn(r - 1)) {
2557  return r;
2558  }
2559 
2560  --r;
2561  while (l != r) {
2562  size_t c = (l + r) / 2;
2563  if (is_empty_fn(c)) {
2564  r = c;
2565  } else {
2566  l = c + 1;
2567  }
2568  }
2569 
2570  return r;
2571 }
2572 
2573 } // namespace
2574 
2576  // Note that table function result sets should never use this path as the row count
2577  // can be known statically (as the output buffers do not contain empty entries)
2580 
2581  if (!query_mem_desc_.getEntryCount()) {
2582  return 0;
2583  }
2584 
2586  return make_bin_search(0, query_mem_desc_.getEntryCount(), [this](size_t idx) {
2587  return reinterpret_cast<const int64_t*>(buff_)[idx] == EMPTY_KEY_64;
2588  });
2589  } else {
2590  return make_bin_search(0, query_mem_desc_.getEntryCount(), [this](size_t idx) {
2591  const auto keys_ptr = row_ptr_rowwise(buff_, query_mem_desc_, idx);
2592  return *reinterpret_cast<const int64_t*>(keys_ptr) == EMPTY_KEY_64;
2593  });
2594  }
2595 }
2596 
2597 bool ResultSetStorage::isEmptyEntry(const size_t entry_idx) const {
2598  return isEmptyEntry(entry_idx, buff_);
2599 }
2600 
2602  const InternalTargetValue& val,
2603  const bool float_argument_input) {
2604  if (ti.get_notnull()) {
2605  return false;
2606  }
2607  if (val.isInt()) {
2608  return val.i1 == null_val_bit_pattern(ti, float_argument_input);
2609  }
2610  if (val.isPair()) {
2611  return !val.i2;
2612  }
2613  if (val.isStr()) {
2614  return !val.i1;
2615  }
2616  CHECK(val.isNull());
2617  return true;
2618 }
bool is_geoint() const
Definition: sqltypes_lite.h:61
GroupByPerfectHash
Definition: enums.h:58
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:301
bool slotIsVarlenOutput(const size_t slot_idx) const
std::pair< size_t, size_t > getStorageIndex(const size_t entry_idx) const
Definition: ResultSet.cpp:926
#define NULL_DOUBLE
Permutation permutation_
Definition: ResultSet.h:955
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
double decimal_to_double(const SQLTypeInfo &otype, int64_t oval)
static ScalarTargetValue nullScalarTargetValue(SQLTypeInfo const &, bool const translate_strings)
bool isPair() const
Definition: TargetValue.h:65
NonGroupedAggregate
Definition: enums.h:58
AppendedStorage appended_storage_
Definition: ResultSet.h:949
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:988
bool isEmptyEntryColumnar(const size_t entry_idx, const int8_t *buff) const
#define NULL_FLOAT
bool isStr() const
Definition: TargetValue.h:69
bool is_null
Definition: Datum.h:59
bool isLogicalSizedColumnsAllowed() const
T advance_to_next_columnar_target_buff(T target_ptr, const QueryMemoryDescriptor &query_mem_desc, const size_t target_slot_idx)
SQLTypeInfo sql_type
Definition: TargetInfo.h:52
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)
TargetValue NestedArrayToGeoTargetValue(const int8_t *buf, const int64_t index, const SQLTypeInfo &ti, const ResultSet::GeoReturnType return_type)
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:947
#define UNREACHABLE()
Definition: Logger.h:338
#define CHECK_GE(x, y)
Definition: Logger.h:306
std::unique_ptr< ResultSetStorage > storage_
Definition: ResultSet.h:948
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
Projection
Definition: enums.h:58
High-level representation of SQL values.
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 HEAVY.AI.
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:953
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:106
TargetValue getTargetValueFromFlatBuffer(const int8_t *col_ptr, const TargetInfo &target_info, const size_t slot_idx, const size_t target_logical_idx, const size_t global_entry_idx, const size_t local_entry_idx, const bool translate_strings, const std::shared_ptr< RowSetMemoryOwner > &row_set_mem_owner_)
std::vector< SerializedVarlenBufferStorage > serialized_varlen_buffer_
Definition: ResultSet.h:979
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:391
OneIntegerColumnRow getOneColRow(const size_t index) const
static bool isNullIval(SQLTypeInfo const &, bool const translate_strings, int64_t const ival)
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:305
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:182
TargetValue build_array_target_value(const int8_t *buff, const size_t buff_sz, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner)
ExecutorDeviceType
std::string to_string(char const *&&v)
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:53
int8_t * pointer
Definition: Datum.h:58
ScalarTargetValue makeStringTargetValue(SQLTypeInfo const &chosen_type, bool const translate_strings, int64_t const ival) const
#define NULL_INT
const ResultSet * result_set_
Definition: ResultSet.h:803
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:78
Serialization routines for geospatial types.
std::conditional_t< is_cuda_compiler(), DeviceArrayDatum, HostArrayDatum > ArrayDatum
Definition: sqltypes.h:229
const SQLTypeInfo get_compact_type(const TargetInfo &target)
TableFunction
Definition: enums.h:58
size_t global_entry_idx_
Definition: ResultSet.h:126
InternalTargetValue getVarlenOrderEntry(const int64_t str_ptr, const size_t str_len) const
std::shared_ptr< std::vector< double > > decompress_coords< double, SQLTypeInfo >(const SQLTypeInfo &geo_ti, const int8_t *coords, const size_t coords_sz)
const std::vector< TargetInfo > targets_
Definition: ResultSet.h:943
int8_t groupColWidth(const size_t key_idx) const
std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner_
Definition: ResultSet.h:954
size_t get_byteoff_of_slot(const size_t slot_idx, const QueryMemoryDescriptor &query_mem_desc)
size_t drop_first_
Definition: ResultSet.h:952
bool is_agg
Definition: TargetInfo.h:50
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:302
size_t targetGroupbyIndicesSize() const
size_t binSearchRowCount() const
boost::optional< std::vector< ScalarTargetValue >> ArrayTargetValue
Definition: TargetValue.h:181
TargetValue build_string_array_target_value(const int32_t *buff, const size_t buff_sz, const shared::StringDictKey &dict_key, const bool translate_strings, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner)
int64_t lazy_decode(const ColumnLazyFetchInfo &col_lazy_fetch, const int8_t *byte_stream, const int64_t pos)
ScalarTargetValue convertToScalarTargetValue(SQLTypeInfo const &, bool const translate_strings, int64_t const val) const
CountDistinctDescriptors count_distinct_descriptors_
Definition: sqldefs.h:80
size_t getPaddedColWidthForRange(const size_t offset, const size_t range) const
StorageLookupResult findStorage(const size_t entry_idx) const
Definition: ResultSet.cpp:951
boost::optional< boost::variant< GeoPointTargetValue, GeoMultiPointTargetValue, GeoLineStringTargetValue, GeoMultiLineStringTargetValue, GeoPolyTargetValue, GeoMultiPolyTargetValue >> GeoTargetValue
Definition: TargetValue.h:187
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:102
bool usesFlatBuffer() const
Definition: sqltypes.h:1083
bool isNull() const
Definition: TargetValue.h:67
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
#define EMPTY_KEY_8
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
static double calculateQuantile(quantile::TDigest *const t_digest)
Definition: ResultSet.cpp:1047
T row_ptr_rowwise(T buff, const QueryMemoryDescriptor &query_mem_desc, const size_t entry_idx)
SQLAgg agg_kind
Definition: TargetInfo.h:51
const VarlenOutputInfo * getVarlenOutputInfo(const size_t entry_idx) const
QueryDescriptionType getQueryDescriptionType() const
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:561
#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:303
bool is_real_str_or_array(const TargetInfo &target_info)
bool isSingleColumnGroupByWithPerfectHash() const
#define CHECK_LE(x, y)
Definition: Logger.h:304
void serialize(Archive &ar, RegisteredQueryHint &query_hint, const unsigned int version)
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
bool is_date_in_days() const
Definition: sqltypes.h:1018
int64_t int_resize_cast(const int64_t ival, const size_t sz)
int get_array_context_logical_size() const
Definition: sqltypes.h:691
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:967
#define DEF_GET_ENTRY_AT(query_type, columnar_output)
bool isRowAtEmpty(const size_t index) const
size_t entryCount() const
Returns the number of entries the result set is allocated to hold.
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:484
GroupByBaselineHash
Definition: enums.h:58
boost::variant< std::string, void * > NullableString
Definition: TargetValue.h:179
CUstream getQueryEngineCudaStreamForDevice(int device_num)
Definition: QueryEngine.cpp:7
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
TO bit_cast(FROM &&from)
Definition: misc.h:307
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:969
std::string getString(SQLTypeInfo const &, int64_t const ival) const
const ExecutorDeviceType device_type_
Definition: ResultSet.h:944
std::vector< TargetValue > getNextRowImpl(const bool translate_strings, const bool decimal_to_double) const
bool isInt() const
Definition: TargetValue.h:63
bool g_enable_watchdog false
Definition: Execute.cpp:80
static auto fetch(const SQLTypeInfo &geo_ti, const ResultSet::GeoReturnType return_type, T &&...vals)
#define CHECK(condition)
Definition: Logger.h:291
bool is_geometry() const
Definition: sqltypes.h:597
ScalarTargetValue make_scalar_tv(const T val)
size_t getBufferSizeBytes(const ExecutorDeviceType device_type) const
#define EMPTY_KEY_32
std::vector< ColumnLazyFetchInfo > lazy_fetch_info_
Definition: ResultSet.h:966
uint64_t exp_to_scale(const unsigned exp)
size_t crt_row_buff_idx_
Definition: ResultSet.h:950
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::vector< std::vector< std::vector< int64_t > > > frag_offsets_
Definition: ResultSet.h:968
bool is_any() const
Definition: sqltypes.h:558
Basic constructors and methods of the row set interface.
bool separate_varlen_storage_valid_
Definition: ResultSet.h:980
boost::variant< ScalarTargetValue, ArrayTargetValue, GeoTargetValue, GeoTargetValuePtr > TargetValue
Definition: TargetValue.h:195
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:561
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:398
ENTRY_TYPE getColumnarBaselineEntryAt(const size_t row_idx, const size_t target_idx, const size_t slot_idx) const
Definition: sqldefs.h:79
size_t crt_row_buff_idx_
Definition: ResultSet.h:125
HOST static DEVICE bool isFlatBuffer(const void *buffer)
Definition: FlatBuffer.h:528
InternalTargetValue getColumnInternal(const int8_t *buff, const size_t entry_idx, const size_t target_logical_idx, const StorageLookupResult &storage_lookup_result) const
bool isLessThan(SQLTypeInfo const &, int64_t const lhs, int64_t const rhs) const
std::vector< std::vector< TargetOffsets > > offsets_for_storage_
Definition: ResultSet.h:801
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:977
T get_cols_ptr(T buff, const QueryMemoryDescriptor &query_mem_desc)
const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const
Definition: sqldefs.h:77
bool global_entry_idx_valid_
Definition: ResultSet.h:127
std::unique_ptr< VarlenDatum > VarlenDatumPtr
#define IS_GEO(T)
Definition: sqltypes.h:310
bool isDirectColumnarConversionPossible() const
Definition: ResultSet.cpp:1477
size_t get_key_bytes_rowwise(const QueryMemoryDescriptor &query_mem_desc)
Definition: sqldefs.h:86
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:585
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)
DEVICE void VarlenArray_get_nth(int8_t *buf, int n, ArrayDatum *result, bool *is_end)
Definition: sqltypes.h:1716
ENTRY_TYPE getRowWiseBaselineEntryAt(const size_t row_idx, const size_t target_idx, const size_t slot_idx) const
const shared::StringDictKey & getStringDictKey() const
Definition: sqltypes.h:1057
boost::variant< int64_t, double, float, NullableString > ScalarTargetValue
Definition: TargetValue.h:180
int32_t getTargetIdxForKey() const
size_t length
Definition: Datum.h:57
size_t getPrependedGroupColOffInBytes(const size_t group_idx) const
const int device_id_
Definition: ResultSet.h:945