OmniSciDB  5ade3759e0
ResultSetBufferAccessors.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 #ifndef QUERYENGINE_RESULTSETBUFFERACCESSORS_H
26 #define QUERYENGINE_RESULTSETBUFFERACCESSORS_H
27 
28 #include "../Shared/SqlTypesLayout.h"
29 #include "BufferCompaction.h"
30 #include "TypePunning.h"
31 
32 #ifndef __CUDACC__
33 
35 
36 #include <algorithm>
37 
38 inline bool is_real_str_or_array(const TargetInfo& target_info) {
39  return (!target_info.is_agg || target_info.agg_kind == kSAMPLE) &&
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 (separate_varlen_storage && !target_info.is_agg) {
51  return 1;
52  } else {
53  return 2 * target_info.sql_type.get_physical_coord_cols();
54  }
55 }
56 
57 inline size_t get_slots_for_target(const TargetInfo& target_info,
58  const bool separate_varlen_storage) {
59  if (target_info.is_agg) {
60  if (target_info.agg_kind == kAVG || is_real_str_or_array(target_info)) {
61  return 2;
62  } else {
63  return 1;
64  }
65  } else {
66  if (is_real_str_or_array(target_info) && !separate_varlen_storage) {
67  return 2;
68  } else {
69  return 1;
70  }
71  }
72 }
73 
74 inline size_t advance_slot(const size_t j,
75  const TargetInfo& target_info,
76  const bool separate_varlen_storage) {
77  if (target_info.sql_type.is_geometry()) {
78  return j + get_slots_for_geo_target(target_info, separate_varlen_storage);
79  }
80  return j + get_slots_for_target(target_info, separate_varlen_storage);
81 }
82 
83 inline size_t slot_offset_rowwise(const size_t entry_idx,
84  const size_t slot_idx,
85  const size_t key_count,
86  const size_t slot_count) {
87  return (key_count + slot_count) * entry_idx + (key_count + slot_idx);
88 }
89 
90 inline size_t slot_offset_colwise(const size_t entry_idx,
91  const size_t slot_idx,
92  const size_t key_count,
93  const size_t entry_count) {
94  return (key_count + slot_idx) * entry_count + entry_idx;
95 }
96 
97 inline size_t key_offset_rowwise(const size_t entry_idx,
98  const size_t key_count,
99  const size_t slot_count) {
100  return (key_count + slot_count) * entry_idx;
101 }
102 
103 inline size_t key_offset_colwise(const size_t entry_idx,
104  const size_t key_idx,
105  const size_t entry_count) {
106  return key_idx * entry_count + entry_idx;
107 }
108 
109 template <class T>
111  const QueryMemoryDescriptor& query_mem_desc,
112  const size_t target_slot_idx) {
113  auto new_target_ptr = target_ptr;
114  const auto column_size = query_mem_desc.getEntryCount() *
115  query_mem_desc.getPaddedSlotWidthBytes(target_slot_idx);
116  new_target_ptr += align_to_int64(column_size);
117 
118  return new_target_ptr;
119 }
120 
121 template <class T>
122 inline T get_cols_ptr(T buff, const QueryMemoryDescriptor& query_mem_desc) {
123  CHECK(query_mem_desc.didOutputColumnar());
124  return buff + query_mem_desc.getColOffInBytes(0);
125 }
126 
127 inline size_t get_key_bytes_rowwise(const QueryMemoryDescriptor& query_mem_desc) {
128  if (query_mem_desc.hasKeylessHash()) {
129  return 0;
130  }
131  auto consist_key_width = query_mem_desc.getEffectiveKeyWidth();
132  CHECK(consist_key_width);
133  return consist_key_width * query_mem_desc.groupColWidthsSize();
134 }
135 
136 inline size_t get_row_bytes(const QueryMemoryDescriptor& query_mem_desc) {
137  size_t result = align_to_int64(get_key_bytes_rowwise(query_mem_desc)); // plus padding
138  return result + query_mem_desc.getRowWidth();
139 }
140 
141 template <class T>
142 inline T row_ptr_rowwise(T buff,
143  const QueryMemoryDescriptor& query_mem_desc,
144  const size_t entry_idx) {
145  const auto row_bytes = get_row_bytes(query_mem_desc);
146  return buff + entry_idx * row_bytes;
147 }
148 
149 template <class T>
150 inline T advance_target_ptr_row_wise(T target_ptr,
151  const TargetInfo& target_info,
152  const size_t slot_idx,
153  const QueryMemoryDescriptor& query_mem_desc,
154  const bool separate_varlen_storage) {
155  auto result = target_ptr + query_mem_desc.getPaddedSlotWidthBytes(slot_idx);
156  if ((target_info.is_agg && target_info.agg_kind == kAVG) ||
157  ((!separate_varlen_storage || target_info.is_agg) &&
158  is_real_str_or_array(target_info))) {
159  return result + query_mem_desc.getPaddedSlotWidthBytes(slot_idx + 1);
160  }
161  if (target_info.sql_type.is_geometry() &&
162  (!separate_varlen_storage || target_info.is_agg)) {
163  for (auto i = 1; i < 2 * target_info.sql_type.get_physical_coord_cols(); ++i) {
164  result += query_mem_desc.getPaddedSlotWidthBytes(slot_idx + i);
165  }
166  }
167  return result;
168 }
169 
170 template <class T>
171 inline T advance_target_ptr_col_wise(T target_ptr,
172  const TargetInfo& target_info,
173  const size_t slot_idx,
174  const QueryMemoryDescriptor& query_mem_desc,
175  const bool separate_varlen_storage) {
176  auto result =
177  advance_to_next_columnar_target_buff(target_ptr, query_mem_desc, slot_idx);
178  if ((target_info.is_agg && target_info.agg_kind == kAVG) ||
179  (is_real_str_or_array(target_info) && !separate_varlen_storage)) {
180  return advance_to_next_columnar_target_buff(result, query_mem_desc, slot_idx + 1);
181  } else if (target_info.sql_type.is_geometry() && !separate_varlen_storage) {
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 
191 inline size_t get_slot_off_quad(const QueryMemoryDescriptor& query_mem_desc) {
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  double dividend{0.0};
201  int64_t null_val{0};
202  switch (ti.get_type()) {
203  case kFLOAT: {
204  if (float_argument_input) {
205  dividend = static_cast<double>(
206  *reinterpret_cast<const float*>(may_alias_ptr(&fp_pair.first)));
207  } else {
208  dividend = *reinterpret_cast<const double*>(may_alias_ptr(&fp_pair.first));
209  }
210  double null_float = inline_fp_null_val(ti);
211  null_val = *reinterpret_cast<const int64_t*>(may_alias_ptr(&null_float));
212  break;
213  }
214  case kDOUBLE: {
215  dividend = *reinterpret_cast<const double*>(may_alias_ptr(&fp_pair.first));
216  double null_double = inline_fp_null_val(ti);
217  null_val = *reinterpret_cast<const int64_t*>(may_alias_ptr(&null_double));
218  break;
219  }
220  default: {
221 #ifndef __CUDACC__
222  LOG_IF(FATAL, !(ti.is_integer() || ti.is_decimal()))
223  << "Unsupported type for pair to double conversion: " << ti.get_type_name();
224 #else
225  CHECK(ti.is_integer() || ti.is_decimal());
226 #endif
227  dividend = static_cast<double>(fp_pair.first);
228  null_val = inline_int_null_val(ti);
229  break;
230  }
231  }
232  if (!ti.get_notnull() && null_val == fp_pair.first) {
233  return inline_fp_null_val(SQLTypeInfo(kDOUBLE, false));
234  }
235 
236  return ti.is_integer() || ti.is_decimal()
237  ? (dividend / exp_to_scale(ti.is_decimal() ? ti.get_scale() : 0)) /
238  static_cast<double>(fp_pair.second)
239  : dividend / static_cast<double>(fp_pair.second);
240 }
241 
242 inline int64_t null_val_bit_pattern(const SQLTypeInfo& ti,
243  const bool float_argument_input) {
244  if (ti.is_fp()) {
245  if (float_argument_input && ti.get_type() == kFLOAT) {
246  int64_t float_null_val = 0;
247  *reinterpret_cast<float*>(may_alias_ptr(&float_null_val)) =
248  static_cast<float>(inline_fp_null_val(ti));
249  return float_null_val;
250  }
251  const auto double_null_val = inline_fp_null_val(ti);
252  return *reinterpret_cast<const int64_t*>(may_alias_ptr(&double_null_val));
253  }
254  if ((ti.is_string() && ti.get_compression() == kENCODING_NONE) || ti.is_array() ||
255  ti.is_geometry()) {
256  return 0;
257  }
258  return inline_int_null_val(ti);
259 }
260 
261 // Interprets ptr as an integer of compact_sz byte width and reads it.
262 inline int64_t read_int_from_buff(const int8_t* ptr, const int8_t compact_sz) {
263  switch (compact_sz) {
264  case 8: {
265  return *reinterpret_cast<const int64_t*>(ptr);
266  }
267  case 4: {
268  return *reinterpret_cast<const int32_t*>(ptr);
269  }
270  case 2: {
271  return *reinterpret_cast<const int16_t*>(ptr);
272  }
273  case 1: {
274  return *reinterpret_cast<const int8_t*>(ptr);
275  }
276  default:
277  UNREACHABLE();
278  }
279  UNREACHABLE();
280  return 0;
281 }
282 
283 #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)
int get_physical_coord_cols() const
Definition: sqltypes.h:359
bool is_fp() const
Definition: sqltypes.h:454
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)
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:42
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:330
HOST DEVICE int get_scale() const
Definition: sqltypes.h:328
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:323
#define UNREACHABLE()
Definition: Logger.h:231
size_t get_slot_off_quad(const QueryMemoryDescriptor &query_mem_desc)
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)
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)
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:331
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)
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
#define LOG_IF(severity, condition)
Definition: Logger.h:273
std::string get_type_name() const
Definition: sqltypes.h:426
bool is_array() const
Definition: sqltypes.h:458
bool is_integer() const
Definition: sqltypes.h:452
bool is_agg
Definition: TargetInfo.h:40
size_t advance_slot(const size_t j, const TargetInfo &target_info, const bool separate_varlen_storage)
bool is_decimal() const
Definition: sqltypes.h:453
SQLTypeInfoCore< ArrayContextTypeSizer, ExecutorTypePackaging, DateTimeFacilities > SQLTypeInfo
Definition: sqltypes.h:823
T row_ptr_rowwise(T buff, const QueryMemoryDescriptor &query_mem_desc, const size_t entry_idx)
SQLAgg agg_kind
Definition: TargetInfo.h:41
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)
size_t get_row_bytes(const QueryMemoryDescriptor &query_mem_desc)
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:187
uint64_t exp_to_scale(const unsigned exp)
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
bool is_geometry() const
Definition: sqltypes.h:462
size_t getColOffInBytes(const size_t col_idx) const
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)
size_t get_slots_for_target(const TargetInfo &target_info, const bool separate_varlen_storage)
T get_cols_ptr(T buff, const QueryMemoryDescriptor &query_mem_desc)
Definition: sqldefs.h:71
bool is_string() const
Definition: sqltypes.h:450
size_t get_key_bytes_rowwise(const QueryMemoryDescriptor &query_mem_desc)
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)
size_t getEffectiveKeyWidth() const