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