OmniSciDB  eb3a3d0a03
 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 2020 OmniSci, 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 "ResultSetStorage.h"
26 
29 #include "Execute.h"
30 #include "GpuMemUtils.h"
31 #include "InPlaceSort.h"
33 #include "RuntimeFunctions.h"
34 #include "Shared/SqlTypesLayout.h"
35 #include "Shared/checked_alloc.h"
36 #include "Shared/likely.h"
37 
38 #include <algorithm>
39 #include <bitset>
40 #include <future>
41 #include <numeric>
42 
43 int8_t* VarlenOutputInfo::computeCpuOffset(const int64_t gpu_offset_address) const {
44  const auto gpu_start_address_ptr = reinterpret_cast<int8_t*>(gpu_start_address);
45  const auto gpu_offset_address_ptr = reinterpret_cast<int8_t*>(gpu_offset_address);
46  if (gpu_offset_address_ptr == 0) {
47  return 0;
48  }
49  const auto offset_bytes =
50  static_cast<int64_t>(gpu_offset_address_ptr - gpu_start_address_ptr);
51  CHECK_GE(offset_bytes, int64_t(0));
52  return cpu_buffer_ptr + offset_bytes;
53 }
54 
55 ResultSetStorage::ResultSetStorage(const std::vector<TargetInfo>& targets,
57  int8_t* buff,
58  const bool buff_is_provided)
59  : targets_(targets)
60  , query_mem_desc_(query_mem_desc)
61  , buff_(buff)
62  , buff_is_provided_(buff_is_provided)
63  , target_init_vals_(result_set::initialize_target_values_for_storage(targets)) {}
64 
66  return buff_;
67 }
68 
70  const int64_t ptr) {
71  const auto it_ok = count_distinct_sets_mapping_.emplace(remote_ptr, ptr);
72  CHECK(it_ok.second);
73 }
74 
75 int64_t ResultSetStorage::mappedPtr(const int64_t remote_ptr) const {
76  const auto it = count_distinct_sets_mapping_.find(remote_ptr);
77  // Due to the removal of completely zero bitmaps in a distributed transfer there will be
78  // remote ptr that do not not exists. Return 0 if no pointer found
79  if (it == count_distinct_sets_mapping_.end()) {
80  return int64_t(0);
81  }
82  return it->second;
83 }
84 
86  const std::vector<TargetInfo>& targets) {
87  std::vector<int64_t> target_init_vals;
88  for (const auto& target_info : targets) {
89  if (target_info.agg_kind == kCOUNT ||
90  target_info.agg_kind == kAPPROX_COUNT_DISTINCT) {
91  target_init_vals.push_back(0);
92  continue;
93  }
94  if (!target_info.sql_type.get_notnull()) {
95  int64_t init_val =
96  null_val_bit_pattern(target_info.sql_type, takes_float_argument(target_info));
97  target_init_vals.push_back(target_info.is_agg ? init_val : 0);
98  } else {
99  target_init_vals.push_back(target_info.is_agg ? 0xdeadbeef : 0);
100  }
101  if (target_info.agg_kind == kAVG) {
102  target_init_vals.push_back(0);
103  } else if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_geometry()) {
104  for (int i = 1; i < 2 * target_info.sql_type.get_physical_coord_cols(); i++) {
105  target_init_vals.push_back(0);
106  }
107  } else if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_varlen()) {
108  target_init_vals.push_back(0);
109  }
110  }
111  return target_init_vals;
112 }
113 
114 int64_t result_set::lazy_decode(const ColumnLazyFetchInfo& col_lazy_fetch,
115  const int8_t* byte_stream,
116  const int64_t pos) {
117  CHECK(col_lazy_fetch.is_lazily_fetched);
118  const auto& type_info = col_lazy_fetch.type;
119  if (type_info.is_fp()) {
120  if (type_info.get_type() == kFLOAT) {
121  double fval = fixed_width_float_decode_noinline(byte_stream, pos);
122  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&fval));
123  } else {
124  double fval = fixed_width_double_decode_noinline(byte_stream, pos);
125  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&fval));
126  }
127  }
128  CHECK(type_info.is_integer() || type_info.is_decimal() || type_info.is_time() ||
129  type_info.is_timeinterval() || type_info.is_boolean() || type_info.is_string() ||
130  type_info.is_array());
131  size_t type_bitwidth = get_bit_width(type_info);
132  if (type_info.get_compression() == kENCODING_FIXED) {
133  type_bitwidth = type_info.get_comp_param();
134  } else if (type_info.get_compression() == kENCODING_DICT) {
135  type_bitwidth = 8 * type_info.get_size();
136  }
137  CHECK_EQ(size_t(0), type_bitwidth % 8);
138  int64_t val;
139  if (type_info.is_date_in_days()) {
140  val = type_info.get_comp_param() == 16
142  byte_stream, 2, NULL_SMALLINT, NULL_BIGINT, pos)
144  byte_stream, 4, NULL_INT, NULL_BIGINT, pos);
145  } else {
146  val = (type_info.get_compression() == kENCODING_DICT &&
147  type_info.get_size() < type_info.get_logical_size() &&
148  type_info.get_comp_param())
149  ? fixed_width_unsigned_decode_noinline(byte_stream, type_bitwidth / 8, pos)
150  : fixed_width_int_decode_noinline(byte_stream, type_bitwidth / 8, pos);
151  }
152  if (type_info.get_compression() != kENCODING_NONE &&
153  type_info.get_compression() != kENCODING_DATE_IN_DAYS) {
154  CHECK(type_info.get_compression() == kENCODING_FIXED ||
155  type_info.get_compression() == kENCODING_DICT);
156  auto encoding = type_info.get_compression();
157  if (encoding == kENCODING_FIXED) {
158  encoding = kENCODING_NONE;
159  }
160  SQLTypeInfo col_logical_ti(type_info.get_type(),
161  type_info.get_dimension(),
162  type_info.get_scale(),
163  false,
164  encoding,
165  0,
166  type_info.get_subtype());
167  if (val == inline_fixed_encoding_null_val(type_info)) {
168  return inline_int_null_val(col_logical_ti);
169  }
170  }
171  return val;
172 }
const SQLTypeInfo type
#define CHECK_EQ(x, y)
Definition: Logger.h:217
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:222
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:157
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
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:76
const bool is_lazily_fetched
#define CHECK(condition)
Definition: Logger.h:209
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:72