OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResultSetStorage.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
23 #include "ResultSetStorage.h"
24 
27 #include "Execute.h"
28 #include "GpuMemUtils.h"
29 #include "InPlaceSort.h"
31 #include "RuntimeFunctions.h"
32 #include "Shared/SqlTypesLayout.h"
33 #include "Shared/checked_alloc.h"
34 #include "Shared/likely.h"
35 
36 #include <algorithm>
37 #include <bitset>
38 #include <future>
39 #include <numeric>
40 
41 int8_t* VarlenOutputInfo::computeCpuOffset(const int64_t gpu_offset_address) const {
42  const auto gpu_start_address_ptr = reinterpret_cast<int8_t*>(gpu_start_address);
43  const auto gpu_offset_address_ptr = reinterpret_cast<int8_t*>(gpu_offset_address);
44  if (gpu_offset_address_ptr == 0) {
45  return 0;
46  }
47  const auto offset_bytes =
48  static_cast<int64_t>(gpu_offset_address_ptr - gpu_start_address_ptr);
49  CHECK_GE(offset_bytes, int64_t(0));
50  return cpu_buffer_ptr + offset_bytes;
51 }
52 
53 ResultSetStorage::ResultSetStorage(const std::vector<TargetInfo>& targets,
55  int8_t* buff,
56  const bool buff_is_provided)
57  : targets_(targets)
58  , query_mem_desc_(query_mem_desc)
59  , buff_(buff)
60  , buff_is_provided_(buff_is_provided)
61  , target_init_vals_(result_set::initialize_target_values_for_storage(targets)) {}
62 
64  return buff_;
65 }
66 
68  const int64_t ptr) {
69  const auto it_ok = count_distinct_sets_mapping_.emplace(remote_ptr, ptr);
70  CHECK(it_ok.second);
71 }
72 
73 int64_t ResultSetStorage::mappedPtr(const int64_t remote_ptr) const {
74  const auto it = count_distinct_sets_mapping_.find(remote_ptr);
75  // Due to the removal of completely zero bitmaps in a distributed transfer there will be
76  // remote ptr that do not not exists. Return 0 if no pointer found
77  if (it == count_distinct_sets_mapping_.end()) {
78  return int64_t(0);
79  }
80  return it->second;
81 }
82 
84  const std::vector<TargetInfo>& targets) {
85  std::vector<int64_t> target_init_vals;
86  for (const auto& target_info : targets) {
87  if (shared::is_any<kCOUNT, kCOUNT_IF, kAPPROX_COUNT_DISTINCT>(target_info.agg_kind)) {
88  target_init_vals.push_back(0);
89  continue;
90  }
91  if (!target_info.sql_type.get_notnull()) {
92  int64_t init_val =
93  null_val_bit_pattern(target_info.sql_type, takes_float_argument(target_info));
94  target_init_vals.push_back(target_info.is_agg ? init_val : 0);
95  } else {
96  target_init_vals.push_back(target_info.is_agg ? 0xdeadbeef : 0);
97  }
98  if (target_info.agg_kind == kAVG) {
99  target_init_vals.push_back(0);
100  } else if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_geometry()) {
101  for (int i = 1; i < 2 * target_info.sql_type.get_physical_coord_cols(); i++) {
102  target_init_vals.push_back(0);
103  }
104  } else if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_varlen()) {
105  target_init_vals.push_back(0);
106  }
107  }
108  return target_init_vals;
109 }
110 
111 int64_t result_set::lazy_decode(const ColumnLazyFetchInfo& col_lazy_fetch,
112  const int8_t* byte_stream,
113  const int64_t pos) {
114  CHECK(col_lazy_fetch.is_lazily_fetched);
115  const auto& type_info = col_lazy_fetch.type;
116  if (type_info.is_fp()) {
117  if (type_info.get_type() == kFLOAT) {
118  double fval = fixed_width_float_decode_noinline(byte_stream, pos);
119  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&fval));
120  } else {
121  double fval = fixed_width_double_decode_noinline(byte_stream, pos);
122  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&fval));
123  }
124  }
125  CHECK(type_info.is_integer() || type_info.is_decimal() || type_info.is_time() ||
126  type_info.is_timeinterval() || type_info.is_boolean() || type_info.is_string() ||
127  type_info.is_array());
128  size_t type_bitwidth = get_bit_width(type_info);
129  if (type_info.get_compression() == kENCODING_FIXED) {
130  type_bitwidth = type_info.get_comp_param();
131  } else if (type_info.get_compression() == kENCODING_DICT) {
132  type_bitwidth = 8 * type_info.get_size();
133  }
134  CHECK_EQ(size_t(0), type_bitwidth % 8);
135  int64_t val;
136  if (type_info.is_date_in_days()) {
137  val = type_info.get_comp_param() == 16
139  byte_stream, 2, NULL_SMALLINT, NULL_BIGINT, pos)
141  byte_stream, 4, NULL_INT, NULL_BIGINT, pos);
142  } else {
143  val = (type_info.get_compression() == kENCODING_DICT &&
144  type_info.get_size() < type_info.get_logical_size() &&
145  type_info.get_comp_param())
146  ? fixed_width_unsigned_decode_noinline(byte_stream, type_bitwidth / 8, pos)
147  : fixed_width_int_decode_noinline(byte_stream, type_bitwidth / 8, pos);
148  }
149  if (type_info.get_compression() != kENCODING_NONE &&
150  type_info.get_compression() != kENCODING_DATE_IN_DAYS) {
151  CHECK(type_info.get_compression() == kENCODING_FIXED ||
152  type_info.get_compression() == kENCODING_DICT);
153  auto encoding = type_info.get_compression();
154  if (encoding == kENCODING_FIXED) {
155  encoding = kENCODING_NONE;
156  }
157  SQLTypeInfo col_logical_ti(type_info.get_type(),
158  type_info.get_dimension(),
159  type_info.get_scale(),
160  false,
161  encoding,
162  0,
163  type_info.get_subtype());
164  if (val == inline_fixed_encoding_null_val(type_info)) {
165  return inline_int_null_val(col_logical_ti);
166  }
167  }
168  return val;
169 }
const SQLTypeInfo type
#define CHECK_EQ(x, y)
Definition: Logger.h:301
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:91
#define NULL_BIGINT
void addCountDistinctSetPointerMapping(const int64_t remote_ptr, const int64_t ptr)
#define CHECK_GE(x, y)
Definition: Logger.h:306
ResultSetStorage(const std::vector< TargetInfo > &targets, const QueryMemoryDescriptor &query_mem_desc, int8_t *buff, const bool buff_is_provided)
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:106
int64_t null_val_bit_pattern(const SQLTypeInfo &ti, const bool float_argument_input)
std::unordered_map< int64_t, int64_t > count_distinct_sets_mapping_
#define NULL_INT
int8_t * getUnderlyingBuffer() const
size_t get_bit_width(const SQLTypeInfo &ti)
int64_t lazy_decode(const ColumnLazyFetchInfo &col_lazy_fetch, const int8_t *byte_stream, const int64_t pos)
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:98
This file includes the class specification for the buffer manager (BufferMgr), and related data struc...
Basic constructors and methods of the row set interface.
std::vector< int64_t > initialize_target_values_for_storage(const std::vector< TargetInfo > &targets)
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:149
int64_t mappedPtr(const int64_t) const
int8_t * computeCpuOffset(const int64_t gpu_offset_address) const
const bool is_lazily_fetched
#define CHECK(condition)
Definition: Logger.h:291
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
#define NULL_SMALLINT
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)
DEVICE NEVER_INLINE float SUFFIX() fixed_width_float_decode_noinline(const int8_t *byte_stream, const int64_t pos)
Definition: DecodersImpl.h:121
DEVICE NEVER_INLINE double SUFFIX() fixed_width_double_decode_noinline(const int8_t *byte_stream, const int64_t pos)
Definition: DecodersImpl.h:134
Allocate GPU memory using GpuBuffers via DataMgr.
Definition: sqldefs.h:74