OmniSciDB  c1a53651b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryMemoryDescriptor.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_QUERYMEMORYDESCRIPTOR_H
24 #define QUERYENGINE_QUERYMEMORYDESCRIPTOR_H
25 
26 #include "../CompilationOptions.h"
27 #include "../CountDistinct.h"
28 #include "ColSlotContext.h"
29 #include "Types.h"
30 
31 #include <boost/optional.hpp>
32 #include "Logger/Logger.h"
33 
34 #include <algorithm>
35 #include <cstddef>
36 #include <cstdint>
37 #include <memory>
38 #include <numeric>
39 #include <unordered_map>
40 #include <vector>
41 
42 #include <Shared/SqlTypesLayout.h>
43 #include <Shared/TargetInfo.h>
44 
45 extern bool g_cluster;
46 
47 class Executor;
49 class RenderInfo;
50 class RowSetMemoryOwner;
51 struct InputTableInfo;
52 struct RelAlgExecutionUnit;
53 class TResultSetBufferDescriptor;
55 struct ColRangeInfo;
56 struct KeylessInfo;
57 
58 class StreamingTopNOOM : public std::runtime_error {
59  public:
60  StreamingTopNOOM(const size_t heap_size_bytes)
61  : std::runtime_error("Unable to use streaming top N due to required heap size of " +
62  std::to_string(heap_size_bytes) +
63  " bytes exceeding maximum slab size.") {}
64 };
65 
67  public:
69 
70  // constructor for init call
71  QueryMemoryDescriptor(const Executor* executor,
72  const RelAlgExecutionUnit& ra_exe_unit,
73  const std::vector<InputTableInfo>& query_infos,
74  const bool allow_multifrag,
75  const bool keyless_hash,
76  const bool interleaved_bins_on_gpu,
77  const int32_t idx_target_as_key,
78  const ColRangeInfo& col_range_info,
79  const ColSlotContext& col_slot_context,
80  const std::vector<int8_t>& group_col_widths,
81  const int8_t group_col_compact_width,
82  const std::vector<int64_t>& target_groupby_indices,
83  const size_t entry_count,
84  const CountDistinctDescriptors count_distinct_descriptors,
85  const bool sort_on_gpu_hint,
86  const bool output_columnar,
87  const bool render_output,
88  const bool must_use_baseline_sort,
89  const bool use_streaming_top_n);
90 
91  QueryMemoryDescriptor(const Executor* executor,
92  const size_t entry_count,
93  const QueryDescriptionType query_desc_type,
94  const bool is_table_function);
95 
96  QueryMemoryDescriptor(const QueryDescriptionType query_desc_type,
97  const int64_t min_val,
98  const int64_t max_val,
99  const bool has_nulls,
100  const std::vector<int8_t>& group_col_widths);
101 
102  // Serialization
103  QueryMemoryDescriptor(const TResultSetBufferDescriptor& thrift_query_memory_descriptor);
104  static TResultSetBufferDescriptor toThrift(const QueryMemoryDescriptor&);
105 
106  bool operator==(const QueryMemoryDescriptor& other) const;
107 
108  static std::unique_ptr<QueryMemoryDescriptor> init(
109  const Executor* executor,
110  const RelAlgExecutionUnit& ra_exe_unit,
111  const std::vector<InputTableInfo>& query_infos,
112  const ColRangeInfo& col_range_info,
113  const KeylessInfo& keyless_info,
114  const bool allow_multifrag,
115  const ExecutorDeviceType device_type,
116  const int8_t crt_min_byte_width,
117  const bool sort_on_gpu_hint,
118  const size_t shard_count,
119  const size_t max_groups_buffer_entry_count,
120  RenderInfo* render_info,
121  const CountDistinctDescriptors count_distinct_descriptors,
122  const bool must_use_baseline_sort,
123  const bool output_columnar_hint,
124  const bool streaming_top_n_hint);
125 
126  std::unique_ptr<QueryExecutionContext> getQueryExecutionContext(
127  const RelAlgExecutionUnit&,
128  const Executor* executor,
129  const ExecutorDeviceType device_type,
130  const ExecutorDispatchMode dispatch_mode,
131  const int device_id,
132  const shared::TableKey& outer_table_key,
133  const int64_t num_rows,
134  const std::vector<std::vector<const int8_t*>>& col_buffers,
135  const std::vector<std::vector<uint64_t>>& frag_offsets,
136  std::shared_ptr<RowSetMemoryOwner>,
137  const bool output_columnar,
138  const bool sort_on_gpu,
139  const size_t thread_idx,
140  RenderInfo*) const;
141 
142  static bool many_entries(const int64_t max_val,
143  const int64_t min_val,
144  const int64_t bucket) {
145  return max_val - min_val > 10000 * std::max(bucket, int64_t(1));
146  }
147 
149  const CountDistinctDescriptors& count_distinct_descriptors) {
150  return std::all_of(count_distinct_descriptors.begin(),
151  count_distinct_descriptors.end(),
152  [](const CountDistinctDescriptor& desc) {
153  return desc.impl_type_ == CountDistinctImplType::Invalid;
154  });
155  }
156 
159  }
160 
161  static int8_t pick_target_compact_width(const RelAlgExecutionUnit& ra_exe_unit,
162  const std::vector<InputTableInfo>& query_infos,
163  const int8_t crt_min_byte_width);
164 
165  // Getters and Setters
166  const Executor* getExecutor() const { return executor_; }
167 
172  getGroupbyColCount() == 1;
173  }
174 
175  bool hasKeylessHash() const { return keyless_hash_; }
176  void setHasKeylessHash(const bool val) { keyless_hash_ = val; }
177 
179  void setHasInterleavedBinsOnGpu(const bool val) { interleaved_bins_on_gpu_ = val; }
180 
181  int32_t getTargetIdxForKey() const { return idx_target_as_key_; }
182  void setTargetIdxForKey(const int32_t val) { idx_target_as_key_ = val; }
183 
184  int8_t groupColWidth(const size_t key_idx) const {
185  CHECK_LT(key_idx, group_col_widths_.size());
186  return group_col_widths_[key_idx];
187  }
188  size_t getPrependedGroupColOffInBytes(const size_t group_idx) const;
189  size_t getPrependedGroupBufferSizeInBytes() const;
190 
191  const auto groupColWidthsBegin() const { return group_col_widths_.begin(); }
192  const auto groupColWidthsEnd() const { return group_col_widths_.end(); }
194 
195  bool isGroupBy() const { return !group_col_widths_.empty(); }
196 
197  void setGroupColCompactWidth(const int8_t val) { group_col_compact_width_ = val; }
198 
199  size_t getColCount() const;
200  size_t getSlotCount() const;
201 
202  const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const;
203  const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const;
204 
205  void setPaddedSlotWidthBytes(const size_t slot_idx, const int8_t bytes);
206 
207  const int8_t getSlotIndexForSingleSlotCol(const size_t col_idx) const;
208 
209  size_t getPaddedColWidthForRange(const size_t offset, const size_t range) const {
210  size_t ret = 0;
211  for (size_t i = offset; i < offset + range; i++) {
212  ret += static_cast<size_t>(getPaddedSlotWidthBytes(i));
213  }
214  return ret;
215  }
216 
217  void useConsistentSlotWidthSize(const int8_t slot_width_size);
218  size_t getRowWidth() const;
219 
220  int8_t updateActualMinByteWidth(const int8_t actual_min_byte_width) const;
221 
222  void addColSlotInfo(const std::vector<std::tuple<int8_t, int8_t>>& slots_for_col);
223 
224  // FlatBuffer support:
225  void addColSlotInfoFlatBuffer(const int64_t flatbuffer_size);
226  int64_t getFlatBufferSize(const size_t slot_idx) const {
227  return col_slot_context_.getFlatBufferSize(slot_idx);
228  }
229 
230  void clearSlotInfo();
231 
232  void alignPaddedSlots();
233 
234  int64_t getTargetGroupbyIndex(const size_t target_idx) const {
235  CHECK_LT(target_idx, target_groupby_indices_.size());
236  return target_groupby_indices_[target_idx];
237  }
238 
239  void setAllTargetGroupbyIndices(std::vector<int64_t> group_by_indices) {
240  target_groupby_indices_ = group_by_indices;
241  }
242 
243  size_t targetGroupbyIndicesSize() const { return target_groupby_indices_.size(); }
245  return std::count_if(
246  target_groupby_indices_.begin(),
248  [](const int64_t& target_group_by_index) { return target_group_by_index < 0; });
249  }
251 
252  size_t getEntryCount() const { return entry_count_; }
253  void setEntryCount(const size_t val) { entry_count_ = val; }
254 
255  int64_t getMinVal() const { return min_val_; }
256  int64_t getMaxVal() const { return max_val_; }
257  int64_t getBucket() const { return bucket_; }
258 
259  bool hasNulls() const { return has_nulls_; }
260 
261  const CountDistinctDescriptor& getCountDistinctDescriptor(const size_t idx) const {
263  return count_distinct_descriptors_[idx];
264  }
266  return count_distinct_descriptors_.size();
267  }
268 
269  bool sortOnGpu() const { return sort_on_gpu_; }
270 
271  bool canOutputColumnar() const;
272  bool didOutputColumnar() const { return output_columnar_; }
273  void setOutputColumnar(const bool val);
274 
275  bool useStreamingTopN() const { return use_streaming_top_n_; }
276 
277  bool isLogicalSizedColumnsAllowed() const;
278 
280 
281  // TODO(adb): remove and store this info more naturally in another
282  // member
283  bool forceFourByteFloat() const { return force_4byte_float_; }
284  void setForceFourByteFloat(const bool val) { force_4byte_float_ = val; }
285 
286  // Getters derived from state
287  size_t getGroupbyColCount() const { return group_col_widths_.size(); }
288  size_t getKeyCount() const { return keyless_hash_ ? 0 : getGroupbyColCount(); }
289  size_t getBufferColSlotCount() const;
290 
291  size_t getBufferSizeBytes(const RelAlgExecutionUnit& ra_exe_unit,
292  const unsigned thread_count,
293  const ExecutorDeviceType device_type) const;
294  size_t getBufferSizeBytes(const ExecutorDeviceType device_type) const;
295  size_t getBufferSizeBytes(const ExecutorDeviceType device_type,
296  const size_t override_entry_count) const;
297 
299 
300  // TODO(alex): remove
301  bool usesGetGroupValueFast() const;
302 
303  bool blocksShareMemory() const;
304  bool threadsShareMemory() const;
305 
306  bool lazyInitGroups(const ExecutorDeviceType) const;
307 
308  bool interleavedBins(const ExecutorDeviceType) const;
309 
310  size_t getColOffInBytes(const size_t col_idx) const;
311  size_t getColOffInBytesInNextBin(const size_t col_idx) const;
312  size_t getNextColOffInBytes(const int8_t* col_ptr,
313  const size_t bin,
314  const size_t col_idx) const;
315 
316  // returns the ptr offset of the next column, 64-bit aligned
317  size_t getNextColOffInBytesRowOnly(const int8_t* col_ptr, const size_t col_idx) const;
318  // returns the ptr offset of the current column, 64-bit aligned
319  size_t getColOnlyOffInBytes(const size_t col_idx) const;
320  size_t getRowSize() const;
321  size_t getColsSize() const;
322  size_t getWarpCount() const;
323 
324  size_t getCompactByteWidth() const;
325 
326  inline size_t getEffectiveKeyWidth() const {
327  return group_col_compact_width_ ? group_col_compact_width_ : sizeof(int64_t);
328  }
329 
330  bool isWarpSyncRequired(const ExecutorDeviceType) const;
331 
332  std::string queryDescTypeToString() const;
333  std::string toString() const;
334 
335  std::string reductionKey() const;
336 
338 
339  // returns a value if the buffer can be a fixed size; otherwise, we will need to use the
340  // bump allocator
341  std::optional<size_t> varlenOutputBufferElemSize() const;
342 
343  // returns the number of bytes needed for all slots preceeding slot_idx. Used to compute
344  // the offset into the varlen buffer for each projected target in a given row.
345  size_t varlenOutputRowSizeToSlot(const size_t slot_idx) const;
346 
347  bool slotIsVarlenOutput(const size_t slot_idx) const {
348  return col_slot_context_.slotIsVarlen(slot_idx);
349  }
350 
351  protected:
352  void resetGroupColWidths(const std::vector<int8_t>& new_group_col_widths) {
353  group_col_widths_ = new_group_col_widths;
354  }
355 
356  private:
362  int32_t idx_target_as_key_; // If keyless_hash_ enabled, then represents what target
363  // expression should be used to identify the key (e.g., in
364  // locating empty entries). Currently only valid with
365  // keyless_hash_ and single-column GroupByPerfectHash
366  std::vector<int8_t> group_col_widths_;
367  int8_t group_col_compact_width_; // compact width for all group
368  // cols if able to be consistent
369  // otherwise 0
370  std::vector<int64_t> target_groupby_indices_;
371  size_t entry_count_; // the number of entries in the main buffer
372  int64_t min_val_; // meaningful for OneColKnownRange,
373  // MultiColPerfectHash only
374  int64_t max_val_;
375  int64_t bucket_;
384 
386 
388 
389  size_t getTotalBytesOfColumnarBuffers() const;
390  size_t getTotalBytesOfColumnarBuffers(const size_t num_entries_per_column) const;
391  size_t getTotalBytesOfColumnarProjections(const size_t projection_count) const;
392 
393  friend class ResultSet;
394  friend class QueryExecutionContext;
395 };
396 
397 inline void set_notnull(TargetInfo& target, const bool not_null) {
398  target.skip_null_val = !not_null;
399  auto new_type = get_compact_type(target);
400  new_type.set_notnull(not_null);
401  set_compact_type(target, new_type);
402 }
403 
404 std::vector<TargetInfo> target_exprs_to_infos(
405  const std::vector<Analyzer::Expr*>& targets,
407 
408 #endif // QUERYENGINE_QUERYMEMORYDESCRIPTOR_H
size_t varlenOutputRowSizeToSlot(const size_t slot_idx) const
void set_compact_type(TargetInfo &target, const SQLTypeInfo &new_type)
static bool many_entries(const int64_t max_val, const int64_t min_val, const int64_t bucket)
void addColSlotInfoFlatBuffer(const int64_t flatbuffer_size)
size_t getBufferSizeBytes(const RelAlgExecutionUnit &ra_exe_unit, const unsigned thread_count, const ExecutorDeviceType device_type) const
bool slotIsVarlenOutput(const size_t slot_idx) const
bool countDistinctDescriptorsLogicallyEmpty() const
bool slotIsVarlen(const size_t slot_idx) const
size_t getTotalBytesOfColumnarProjections(const size_t projection_count) const
void setEntryCount(const size_t val)
int64_t getTargetGroupbyIndex(const size_t target_idx) const
ExecutorDeviceType
void sort_on_gpu(int64_t *val_buff, int32_t *idx_buff, const uint64_t entry_count, const bool desc, const uint32_t chosen_bytes, ThrustAllocator &alloc, const int device_id)
std::string toString() const
bool isLogicalSizedColumnsAllowed() const
void setHasKeylessHash(const bool val)
void setGroupColCompactWidth(const int8_t val)
static std::unique_ptr< QueryMemoryDescriptor > init(const Executor *executor, const RelAlgExecutionUnit &ra_exe_unit, const std::vector< InputTableInfo > &query_infos, const ColRangeInfo &col_range_info, const KeylessInfo &keyless_info, const bool allow_multifrag, const ExecutorDeviceType device_type, const int8_t crt_min_byte_width, const bool sort_on_gpu_hint, const size_t shard_count, const size_t max_groups_buffer_entry_count, RenderInfo *render_info, const CountDistinctDescriptors count_distinct_descriptors, const bool must_use_baseline_sort, const bool output_columnar_hint, const bool streaming_top_n_hint)
void setOutputColumnar(const bool val)
size_t getNextColOffInBytes(const int8_t *col_ptr, const size_t bin, const size_t col_idx) const
size_t getEffectiveKeyWidth() const
bool use_streaming_top_n(const RelAlgExecutionUnit &ra_exe_unit, const bool output_columnar)
bool hasVarlenOutput() const
bool skip_null_val
Definition: TargetInfo.h:54
void setQueryDescriptionType(const QueryDescriptionType val)
size_t targetGroupbyNegativeIndicesSize() const
bool hasInterleavedBinsOnGpu() const
std::string to_string(char const *&&v)
const auto groupColWidthsBegin() const
void useConsistentSlotWidthSize(const int8_t slot_width_size)
size_t getColOnlyOffInBytes(const size_t col_idx) const
ExecutorDispatchMode
const SQLTypeInfo get_compact_type(const TargetInfo &target)
const auto groupColWidthsEnd() const
int8_t groupColWidth(const size_t key_idx) const
void setTargetIdxForKey(const int32_t val)
std::vector< CountDistinctDescriptor > CountDistinctDescriptors
Definition: CountDistinct.h:34
Provides column info and slot info for the output buffer and some metadata helpers.
size_t getGroupbyColCount() const
bool lazyInitGroups(const ExecutorDeviceType) const
size_t targetGroupbyIndicesSize() const
size_t getPrependedGroupBufferSizeInBytes() const
size_t getTotalBytesOfColumnarBuffers() const
std::vector< int64_t > target_groupby_indices_
static int8_t pick_target_compact_width(const RelAlgExecutionUnit &ra_exe_unit, const std::vector< InputTableInfo > &query_infos, const int8_t crt_min_byte_width)
CountDistinctDescriptors count_distinct_descriptors_
size_t getPaddedColWidthForRange(const size_t offset, const size_t range) const
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
static TResultSetBufferDescriptor toThrift(const QueryMemoryDescriptor &)
size_t getCountDistinctDescriptorsSize() const
QueryDescriptionType getQueryDescriptionType() const
const CountDistinctDescriptor & getCountDistinctDescriptor(const size_t idx) const
std::optional< size_t > varlenOutputBufferElemSize() const
#define CHECK_LT(x, y)
Definition: Logger.h:303
bool isSingleColumnGroupByWithPerfectHash() const
size_t getNextColOffInBytesRowOnly(const int8_t *col_ptr, const size_t col_idx) const
StreamingTopNOOM(const size_t heap_size_bytes)
QueryDescriptionType query_desc_type_
int8_t updateActualMinByteWidth(const int8_t actual_min_byte_width) const
bool operator==(const QueryMemoryDescriptor &other) const
void setForceFourByteFloat(const bool val)
bool isWarpSyncRequired(const ExecutorDeviceType) const
bool interleavedBins(const ExecutorDeviceType) const
const ColSlotContext & getColSlotContext() const
std::vector< int8_t > group_col_widths_
void setAllTargetGroupbyIndices(std::vector< int64_t > group_by_indices)
QueryDescriptionType
Definition: Types.h:29
bool g_cluster
void setPaddedSlotWidthBytes(const size_t slot_idx, const int8_t bytes)
void resetGroupColWidths(const std::vector< int8_t > &new_group_col_widths)
std::vector< TargetInfo > target_exprs_to_infos(const std::vector< Analyzer::Expr * > &targets, const QueryMemoryDescriptor &query_mem_desc)
std::string queryDescTypeToString() const
void addColSlotInfo(const std::vector< std::tuple< int8_t, int8_t >> &slots_for_col)
void setHasInterleavedBinsOnGpu(const bool val)
static bool countDescriptorsLogicallyEmpty(const CountDistinctDescriptors &count_distinct_descriptors)
int64_t getFlatBufferSize(const size_t slot_idx) const
const int8_t getSlotIndexForSingleSlotCol(const size_t col_idx) const
const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const
size_t getColOffInBytes(const size_t col_idx) const
int64_t getFlatBufferSize(const size_t slot_idx) const
size_t getColOffInBytesInNextBin(const size_t col_idx) const
std::unique_ptr< QueryExecutionContext > getQueryExecutionContext(const RelAlgExecutionUnit &, const Executor *executor, const ExecutorDeviceType device_type, const ExecutorDispatchMode dispatch_mode, const int device_id, const shared::TableKey &outer_table_key, const int64_t num_rows, const std::vector< std::vector< const int8_t * >> &col_buffers, const std::vector< std::vector< uint64_t >> &frag_offsets, std::shared_ptr< RowSetMemoryOwner >, const bool output_columnar, const bool sort_on_gpu, const size_t thread_idx, RenderInfo *) const
std::string reductionKey() const
const Executor * getExecutor() const
void set_notnull(TargetInfo &target, const bool not_null)
int32_t getTargetIdxForKey() const
size_t getPrependedGroupColOffInBytes(const size_t group_idx) const