OmniSciDB  ca0c39ec8f
 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 (target_info.agg_kind == kCOUNT ||
88  target_info.agg_kind == kAPPROX_COUNT_DISTINCT) {
89  target_init_vals.push_back(0);
90  continue;
91  }
92  if (!target_info.sql_type.get_notnull()) {
93  int64_t init_val =
94  null_val_bit_pattern(target_info.sql_type, takes_float_argument(target_info));
95  target_init_vals.push_back(target_info.is_agg ? init_val : 0);
96  } else {
97  target_init_vals.push_back(target_info.is_agg ? 0xdeadbeef : 0);
98  }
99  if (target_info.agg_kind == kAVG) {
100  target_init_vals.push_back(0);
101  } else if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_geometry()) {
102  for (int i = 1; i < 2 * target_info.sql_type.get_physical_coord_cols(); i++) {
103  target_init_vals.push_back(0);
104  }
105  } else if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_varlen()) {
106  target_init_vals.push_back(0);
107  }
108  }
109  return target_init_vals;
110 }
111 
112 int64_t result_set::lazy_decode(const ColumnLazyFetchInfo& col_lazy_fetch,
113  const int8_t* byte_stream,
114  const int64_t pos) {
115  CHECK(col_lazy_fetch.is_lazily_fetched);
116  const auto& type_info = col_lazy_fetch.type;
117  if (type_info.is_fp()) {
118  if (type_info.get_type() == kFLOAT) {
119  double fval = fixed_width_float_decode_noinline(byte_stream, pos);
120  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&fval));
121  } else {
122  double fval = fixed_width_double_decode_noinline(byte_stream, pos);
123  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&fval));
124  }
125  }
126  CHECK(type_info.is_integer() || type_info.is_decimal() || type_info.is_time() ||
127  type_info.is_timeinterval() || type_info.is_boolean() || type_info.is_string() ||
128  type_info.is_array());
129  size_t type_bitwidth = get_bit_width(type_info);
130  if (type_info.get_compression() == kENCODING_FIXED) {
131  type_bitwidth = type_info.get_comp_param();
132  } else if (type_info.get_compression() == kENCODING_DICT) {
133  type_bitwidth = 8 * type_info.get_size();
134  }
135  CHECK_EQ(size_t(0), type_bitwidth % 8);
136  int64_t val;
137  if (type_info.is_date_in_days()) {
138  val = type_info.get_comp_param() == 16
140  byte_stream, 2, NULL_SMALLINT, NULL_BIGINT, pos)
142  byte_stream, 4, NULL_INT, NULL_BIGINT, pos);
143  } else {
144  val = (type_info.get_compression() == kENCODING_DICT &&
145  type_info.get_size() < type_info.get_logical_size() &&
146  type_info.get_comp_param())
147  ? fixed_width_unsigned_decode_noinline(byte_stream, type_bitwidth / 8, pos)
148  : fixed_width_int_decode_noinline(byte_stream, type_bitwidth / 8, pos);
149  }
150  if (type_info.get_compression() != kENCODING_NONE &&
151  type_info.get_compression() != kENCODING_DATE_IN_DAYS) {
152  CHECK(type_info.get_compression() == kENCODING_FIXED ||
153  type_info.get_compression() == kENCODING_DICT);
154  auto encoding = type_info.get_compression();
155  if (encoding == kENCODING_FIXED) {
156  encoding = kENCODING_NONE;
157  }
158  SQLTypeInfo col_logical_ti(type_info.get_type(),
159  type_info.get_dimension(),
160  type_info.get_scale(),
161  false,
162  encoding,
163  0,
164  type_info.get_subtype());
165  if (val == inline_fixed_encoding_null_val(type_info)) {
166  return inline_int_null_val(col_logical_ti);
167  }
168  }
169  return val;
170 }
const SQLTypeInfo type
#define CHECK_EQ(x, y)
Definition: Logger.h:230
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:235
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:111
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
Definition: sqldefs.h:77
const bool is_lazily_fetched
#define CHECK(condition)
Definition: Logger.h:222
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:73