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