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