OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResultSetBufferAccessors.h
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 #ifndef QUERYENGINE_RESULTSETBUFFERACCESSORS_H
24 #define QUERYENGINE_RESULTSETBUFFERACCESSORS_H
25 
26 #include "BufferCompaction.h"
27 #include "Shared/SqlTypesLayout.h"
28 #include "Shared/misc.h"
29 #include "TypePunning.h"
30 
31 #ifndef __CUDACC__
32 
34 
35 #include <algorithm>
36 
37 inline bool is_real_str_or_array(const TargetInfo& target_info) {
38  return (!target_info.is_agg || target_info.agg_kind == kSAMPLE) &&
39  !target_info.sql_type.usesFlatBuffer() &&
40  (target_info.sql_type.is_array() ||
41  (target_info.sql_type.is_string() &&
42  target_info.sql_type.get_compression() == kENCODING_NONE));
43 }
44 
45 inline size_t get_slots_for_geo_target(const TargetInfo& target_info,
46  const bool separate_varlen_storage) {
47  // Aggregates on geospatial types are serialized directly by rewriting the underlying
48  // buffer. Even if separate varlen storage is valid, treat aggregates the same on
49  // distributed and single node
50  if (target_info.is_varlen_projection ||
51  (separate_varlen_storage && !target_info.is_agg)) {
52  return 1;
53  } else {
54  return 2 * target_info.sql_type.get_physical_coord_cols();
55  }
56 }
57 
58 inline size_t get_slots_for_target(const TargetInfo& target_info,
59  const bool separate_varlen_storage) {
60  if (target_info.is_agg) {
61  if (target_info.agg_kind == kAVG || is_real_str_or_array(target_info)) {
62  return 2;
63  } else {
64  return 1;
65  }
66  } else {
67  if (is_real_str_or_array(target_info) && !separate_varlen_storage) {
68  return 2;
69  } else {
70  return 1;
71  }
72  }
73 }
74 
75 inline size_t advance_slot(const size_t j,
76  const TargetInfo& target_info,
77  const bool separate_varlen_storage) {
78  if (target_info.sql_type.is_geometry()) {
79  return j + get_slots_for_geo_target(target_info, separate_varlen_storage);
80  }
81  return j + get_slots_for_target(target_info, separate_varlen_storage);
82 }
83 
84 inline size_t slot_offset_rowwise(const size_t entry_idx,
85  const size_t slot_idx,
86  const size_t key_count,
87  const size_t slot_count) {
88  return (key_count + slot_count) * entry_idx + (key_count + slot_idx);
89 }
90 
91 inline size_t slot_offset_colwise(const size_t entry_idx,
92  const size_t slot_idx,
93  const size_t key_count,
94  const size_t entry_count) {
95  return (key_count + slot_idx) * entry_count + entry_idx;
96 }
97 
98 inline size_t key_offset_rowwise(const size_t entry_idx,
99  const size_t key_count,
100  const size_t slot_count) {
101  return (key_count + slot_count) * entry_idx;
102 }
103 
104 inline size_t key_offset_colwise(const size_t entry_idx,
105  const size_t key_idx,
106  const size_t entry_count) {
107  return key_idx * entry_count + entry_idx;
108 }
109 
110 template <class T>
113  const size_t target_slot_idx) {
114  auto new_target_ptr = target_ptr;
115  new_target_ptr += query_mem_desc.getPaddedSlotBufferSize(target_slot_idx);
116  return new_target_ptr;
117 }
118 
119 template <class T>
121  CHECK(query_mem_desc.didOutputColumnar());
122  return buff + query_mem_desc.getColOffInBytes(0);
123 }
124 
126  if (query_mem_desc.hasKeylessHash()) {
127  return 0;
128  }
129  auto consist_key_width = query_mem_desc.getEffectiveKeyWidth();
130  CHECK(consist_key_width);
131  return consist_key_width * query_mem_desc.getGroupbyColCount();
132 }
133 
135  size_t result = align_to_int64(get_key_bytes_rowwise(query_mem_desc)); // plus padding
136  return result + query_mem_desc.getRowWidth();
137 }
138 
139 template <class T>
140 inline T row_ptr_rowwise(T buff,
142  const size_t entry_idx) {
143  const auto row_bytes = get_row_bytes(query_mem_desc);
144  return buff + entry_idx * row_bytes;
145 }
146 
147 template <class T>
148 inline T advance_target_ptr_row_wise(T target_ptr,
149  const TargetInfo& target_info,
150  const size_t slot_idx,
152  const bool separate_varlen_storage) {
153  auto result = target_ptr + query_mem_desc.getPaddedSlotWidthBytes(slot_idx);
154  if ((target_info.is_agg && target_info.agg_kind == kAVG) ||
155  ((!separate_varlen_storage || target_info.is_agg) &&
156  is_real_str_or_array(target_info))) {
157  return result + query_mem_desc.getPaddedSlotWidthBytes(slot_idx + 1);
158  }
159  const bool is_varlen_output_slot = query_mem_desc.slotIsVarlenOutput(slot_idx);
160  if (target_info.sql_type.is_geometry() &&
161  (!separate_varlen_storage || target_info.is_agg) && !is_varlen_output_slot) {
162  for (auto i = 1; i < 2 * target_info.sql_type.get_physical_coord_cols(); ++i) {
163  result += query_mem_desc.getPaddedSlotWidthBytes(slot_idx + i);
164  }
165  }
166  return result;
167 }
168 
169 template <class T>
170 inline T advance_target_ptr_col_wise(T target_ptr,
171  const TargetInfo& target_info,
172  const size_t slot_idx,
174  const bool separate_varlen_storage) {
175  auto result =
176  advance_to_next_columnar_target_buff(target_ptr, query_mem_desc, slot_idx);
177  if ((target_info.is_agg && target_info.agg_kind == kAVG) ||
178  (is_real_str_or_array(target_info) && !separate_varlen_storage)) {
179  return advance_to_next_columnar_target_buff(result, query_mem_desc, slot_idx + 1);
180  } else if (target_info.sql_type.is_geometry() && !separate_varlen_storage) {
181  // TODO: handle varlen projection
182  for (auto i = 1; i < 2 * target_info.sql_type.get_physical_coord_cols(); ++i) {
183  result = advance_to_next_columnar_target_buff(result, query_mem_desc, slot_idx + i);
184  }
185  return result;
186  } else {
187  return result;
188  }
189 }
190 
192  return align_to_int64(get_key_bytes_rowwise(query_mem_desc)) / sizeof(int64_t);
193 }
194 
195 #endif // __CUDACC__
196 
197 inline double pair_to_double(const std::pair<int64_t, int64_t>& fp_pair,
198  const SQLTypeInfo& ti,
199  const bool float_argument_input) {
200  if (fp_pair.second == 0) {
201  return NULL_DOUBLE;
202  }
203  double dividend{0.0};
204  switch (ti.get_type()) {
205  case kFLOAT:
206  if (float_argument_input) {
207  dividend = shared::reinterpret_bits<float>(fp_pair.first);
208  break;
209  }
210  case kDOUBLE:
211  dividend = shared::reinterpret_bits<double>(fp_pair.first);
212  break;
213  default:
214 #ifndef __CUDACC__
215  LOG_IF(FATAL, !(ti.is_integer() || ti.is_decimal()))
216  << "Unsupported type for pair to double conversion: " << ti.get_type_name();
217 #else
218  CHECK(ti.is_integer() || ti.is_decimal());
219 #endif
220  dividend = static_cast<double>(fp_pair.first);
221  break;
222  }
223  return ti.is_decimal() && ti.get_scale()
224  ? dividend /
225  (static_cast<double>(fp_pair.second) * exp_to_scale(ti.get_scale()))
226  : dividend / static_cast<double>(fp_pair.second);
227 }
228 
229 inline int64_t null_val_bit_pattern(const SQLTypeInfo& ti,
230  const bool float_argument_input) {
231  if (ti.is_fp()) {
232  if (float_argument_input && ti.get_type() == kFLOAT) {
233  return shared::reinterpret_bits<int64_t>(NULL_FLOAT); // 1<<23
234  }
235  const auto double_null_val = inline_fp_null_val(ti);
236  return shared::reinterpret_bits<int64_t>(double_null_val); // 0x381<<52 or 1<<52
237  }
238  if ((ti.is_string() && ti.get_compression() == kENCODING_NONE) || ti.is_array() ||
239  ti.is_geometry()) {
240  return 0;
241  }
242  return inline_int_null_val(ti);
243 }
244 
245 // Interprets ptr as an integer of compact_sz byte width and reads it.
246 inline int64_t read_int_from_buff(const int8_t* ptr, const int8_t compact_sz) {
247  switch (compact_sz) {
248  case 8: {
249  return *reinterpret_cast<const int64_t*>(ptr);
250  }
251  case 4: {
252  return *reinterpret_cast<const int32_t*>(ptr);
253  }
254  case 2: {
255  return *reinterpret_cast<const int16_t*>(ptr);
256  }
257  case 1: {
258  return *reinterpret_cast<const int8_t*>(ptr);
259  }
260  default:
261  UNREACHABLE();
262  }
263  UNREACHABLE();
264  return 0;
265 }
266 
267 #endif // QUERYENGINE_RESULTSETBUFFERACCESSORS_H
size_t slot_offset_rowwise(const size_t entry_idx, const size_t slot_idx, const size_t key_count, const size_t slot_count)
bool slotIsVarlenOutput(const size_t slot_idx) const
#define NULL_DOUBLE
size_t slot_offset_colwise(const size_t entry_idx, const size_t slot_idx, const size_t key_count, const size_t entry_count)
#define NULL_FLOAT
T advance_to_next_columnar_target_buff(T target_ptr, const QueryMemoryDescriptor &query_mem_desc, const size_t target_slot_idx)
SQLTypeInfo sql_type
Definition: TargetInfo.h:52
bool is_fp() const
Definition: sqltypes.h:573
HOST DEVICE int get_scale() const
Definition: sqltypes.h:396
#define UNREACHABLE()
Definition: Logger.h:338
Macros and functions for groupby buffer compaction.
size_t get_slot_off_quad(const QueryMemoryDescriptor &query_mem_desc)
size_t getEffectiveKeyWidth() const
int64_t read_int_from_buff(const int8_t *ptr, const int8_t compact_sz)
double pair_to_double(const std::pair< int64_t, int64_t > &fp_pair, const SQLTypeInfo &ti, const bool float_argument_input)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
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)
int64_t null_val_bit_pattern(const SQLTypeInfo &ti, const bool float_argument_input)
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
size_t get_slots_for_geo_target(const TargetInfo &target_info, const bool separate_varlen_storage)
#define LOG_IF(severity, condition)
Definition: Logger.h:384
bool is_varlen_projection
Definition: TargetInfo.h:56
bool is_agg
Definition: TargetInfo.h:50
size_t advance_slot(const size_t j, const TargetInfo &target_info, const bool separate_varlen_storage)
size_t getGroupbyColCount() const
bool is_integer() const
Definition: sqltypes.h:567
bool usesFlatBuffer() const
Definition: sqltypes.h:1083
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
int64_t getPaddedSlotBufferSize(const size_t slot_idx) const
T row_ptr_rowwise(T buff, const QueryMemoryDescriptor &query_mem_desc, const size_t entry_idx)
SQLAgg agg_kind
Definition: TargetInfo.h:51
size_t key_offset_colwise(const size_t entry_idx, const size_t key_idx, const size_t entry_count)
bool is_real_str_or_array(const TargetInfo &target_info)
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
size_t get_row_bytes(const QueryMemoryDescriptor &query_mem_desc)
std::string get_type_name() const
Definition: sqltypes.h:484
size_t key_offset_rowwise(const size_t entry_idx, const size_t key_count, const size_t slot_count)
Descriptor for the result set buffer layout.
#define CHECK(condition)
Definition: Logger.h:291
bool is_geometry() const
Definition: sqltypes.h:597
uint64_t exp_to_scale(const unsigned exp)
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
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)
bool is_string() const
Definition: sqltypes.h:561
size_t get_slots_for_target(const TargetInfo &target_info, const bool separate_varlen_storage)
bool is_decimal() const
Definition: sqltypes.h:570
T get_cols_ptr(T buff, const QueryMemoryDescriptor &query_mem_desc)
int get_physical_coord_cols() const
Definition: sqltypes.h:451
Definition: sqldefs.h:77
size_t getColOffInBytes(const size_t col_idx) const
size_t get_key_bytes_rowwise(const QueryMemoryDescriptor &query_mem_desc)
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)
bool is_array() const
Definition: sqltypes.h:585