OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #include "Shared/thread_count.h"
38 #include "Shared/threadpool.h"
39 
40 #include <algorithm>
41 #include <bitset>
42 #include <future>
43 #include <numeric>
44 
45 ResultSetStorage::ResultSetStorage(const std::vector<TargetInfo>& targets,
47  int8_t* buff,
48  const bool buff_is_provided)
49  : targets_(targets)
50  , query_mem_desc_(query_mem_desc)
51  , buff_(buff)
52  , buff_is_provided_(buff_is_provided)
53  , target_init_vals_(result_set::initialize_target_values_for_storage(targets)) {}
54 
56  return buff_;
57 }
58 
60  const int64_t ptr) {
61  const auto it_ok = count_distinct_sets_mapping_.emplace(remote_ptr, ptr);
62  CHECK(it_ok.second);
63 }
64 
65 int64_t ResultSetStorage::mappedPtr(const int64_t remote_ptr) const {
66  const auto it = count_distinct_sets_mapping_.find(remote_ptr);
67  // Due to the removal of completely zero bitmaps in a distributed transfer there will be
68  // remote ptr that do not not exists. Return 0 if no pointer found
69  if (it == count_distinct_sets_mapping_.end()) {
70  return int64_t(0);
71  }
72  return it->second;
73 }
74 
76  const std::vector<TargetInfo>& targets) {
77  std::vector<int64_t> target_init_vals;
78  for (const auto& target_info : targets) {
79  if (target_info.agg_kind == kCOUNT ||
80  target_info.agg_kind == kAPPROX_COUNT_DISTINCT) {
81  target_init_vals.push_back(0);
82  continue;
83  }
84  if (!target_info.sql_type.get_notnull()) {
85  int64_t init_val =
86  null_val_bit_pattern(target_info.sql_type, takes_float_argument(target_info));
87  target_init_vals.push_back(target_info.is_agg ? init_val : 0);
88  } else {
89  target_init_vals.push_back(target_info.is_agg ? 0xdeadbeef : 0);
90  }
91  if (target_info.agg_kind == kAVG) {
92  target_init_vals.push_back(0);
93  } else if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_geometry()) {
94  for (int i = 1; i < 2 * target_info.sql_type.get_physical_coord_cols(); i++) {
95  target_init_vals.push_back(0);
96  }
97  } else if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_varlen()) {
98  target_init_vals.push_back(0);
99  }
100  }
101  return target_init_vals;
102 }
103 
104 int64_t result_set::lazy_decode(const ColumnLazyFetchInfo& col_lazy_fetch,
105  const int8_t* byte_stream,
106  const int64_t pos) {
107  CHECK(col_lazy_fetch.is_lazily_fetched);
108  const auto& type_info = col_lazy_fetch.type;
109  if (type_info.is_fp()) {
110  if (type_info.get_type() == kFLOAT) {
111  double fval = fixed_width_float_decode_noinline(byte_stream, pos);
112  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&fval));
113  } else {
114  double fval = fixed_width_double_decode_noinline(byte_stream, pos);
115  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&fval));
116  }
117  }
118  CHECK(type_info.is_integer() || type_info.is_decimal() || type_info.is_time() ||
119  type_info.is_timeinterval() || type_info.is_boolean() || type_info.is_string() ||
120  type_info.is_array());
121  size_t type_bitwidth = get_bit_width(type_info);
122  if (type_info.get_compression() == kENCODING_FIXED) {
123  type_bitwidth = type_info.get_comp_param();
124  } else if (type_info.get_compression() == kENCODING_DICT) {
125  type_bitwidth = 8 * type_info.get_size();
126  }
127  CHECK_EQ(size_t(0), type_bitwidth % 8);
128  int64_t val;
129  if (type_info.is_date_in_days()) {
130  val = type_info.get_comp_param() == 16
132  byte_stream, 2, NULL_SMALLINT, NULL_BIGINT, pos)
134  byte_stream, 4, NULL_INT, NULL_BIGINT, pos);
135  } else {
136  val = (type_info.get_compression() == kENCODING_DICT &&
137  type_info.get_size() < type_info.get_logical_size() &&
138  type_info.get_comp_param())
139  ? fixed_width_unsigned_decode_noinline(byte_stream, type_bitwidth / 8, pos)
140  : fixed_width_int_decode_noinline(byte_stream, type_bitwidth / 8, pos);
141  }
142  if (type_info.get_compression() != kENCODING_NONE &&
143  type_info.get_compression() != kENCODING_DATE_IN_DAYS) {
144  CHECK(type_info.get_compression() == kENCODING_FIXED ||
145  type_info.get_compression() == kENCODING_DICT);
146  auto encoding = type_info.get_compression();
147  if (encoding == kENCODING_FIXED) {
148  encoding = kENCODING_NONE;
149  }
150  SQLTypeInfo col_logical_ti(type_info.get_type(),
151  type_info.get_dimension(),
152  type_info.get_scale(),
153  false,
154  encoding,
155  0,
156  type_info.get_subtype());
157  if (val == inline_fixed_encoding_null_val(type_info)) {
158  return inline_int_null_val(col_logical_ti);
159  }
160  }
161  return val;
162 }
const SQLTypeInfo type
#define CHECK_EQ(x, y)
Definition: Logger.h:205
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
#define NULL_BIGINT
Definition: sqltypes.h:255
void addCountDistinctSetPointerMapping(const int64_t remote_ptr, const int64_t ptr)
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:134
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_
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:90
#define NULL_INT
Definition: sqltypes.h:254
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:141
int64_t mappedPtr(const int64_t) const
Definition: sqldefs.h:76
const bool is_lazily_fetched
#define NULL_SMALLINT
Definition: sqltypes.h:253
#define CHECK(condition)
Definition: Logger.h:197
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
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:113
DEVICE NEVER_INLINE double SUFFIX() fixed_width_double_decode_noinline(const int8_t *byte_stream, const int64_t pos)
Definition: DecodersImpl.h:126
Allocate GPU memory using GpuBuffers via DataMgr.
Definition: sqldefs.h:72