OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 int64_t num_rows,
135  const std::vector<std::vector<const int8_t*>>& col_buffers,
136  const std::vector<std::vector<uint64_t>>& frag_offsets,
137  std::shared_ptr<RowSetMemoryOwner>,
138  const bool output_columnar,
139  const bool sort_on_gpu,
140  const size_t thread_idx,
141  RenderInfo*) const;
142 
143  static bool many_entries(const int64_t max_val,
144  const int64_t min_val,
145  const int64_t bucket) {
146  return max_val - min_val > 10000 * std::max(bucket, int64_t(1));
147  }
148 
150  const CountDistinctDescriptors& count_distinct_descriptors) {
151  return std::all_of(count_distinct_descriptors.begin(),
152  count_distinct_descriptors.end(),
153  [](const CountDistinctDescriptor& desc) {
154  return desc.impl_type_ == CountDistinctImplType::Invalid;
155  });
156  }
157 
160  }
161 
162  static int8_t pick_target_compact_width(const RelAlgExecutionUnit& ra_exe_unit,
163  const std::vector<InputTableInfo>& query_infos,
164  const int8_t crt_min_byte_width);
165 
166  // Getters and Setters
167  const Executor* getExecutor() const { return executor_; }
168 
173  getGroupbyColCount() == 1;
174  }
175 
176  bool hasKeylessHash() const { return keyless_hash_; }
177  void setHasKeylessHash(const bool val) { keyless_hash_ = val; }
178 
180  void setHasInterleavedBinsOnGpu(const bool val) { interleaved_bins_on_gpu_ = val; }
181 
182  int32_t getTargetIdxForKey() const { return idx_target_as_key_; }
183  void setTargetIdxForKey(const int32_t val) { idx_target_as_key_ = val; }
184 
185  int8_t groupColWidth(const size_t key_idx) const {
186  CHECK_LT(key_idx, group_col_widths_.size());
187  return group_col_widths_[key_idx];
188  }
189  size_t getPrependedGroupColOffInBytes(const size_t group_idx) const;
190  size_t getPrependedGroupBufferSizeInBytes() const;
191 
192  const auto groupColWidthsBegin() const { return group_col_widths_.begin(); }
193  const auto groupColWidthsEnd() const { return group_col_widths_.end(); }
195 
196  bool isGroupBy() const { return !group_col_widths_.empty(); }
197 
198  void setGroupColCompactWidth(const int8_t val) { group_col_compact_width_ = val; }
199 
200  size_t getColCount() const;
201  size_t getSlotCount() const;
202 
203  const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const;
204  const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const;
205 
206  const int8_t getSlotIndexForSingleSlotCol(const size_t col_idx) const;
207 
208  size_t getPaddedColWidthForRange(const size_t offset, const size_t range) const {
209  size_t ret = 0;
210  for (size_t i = offset; i < offset + range; i++) {
211  ret += static_cast<size_t>(getPaddedSlotWidthBytes(i));
212  }
213  return ret;
214  }
215 
216  void useConsistentSlotWidthSize(const int8_t slot_width_size);
217  size_t getRowWidth() const;
218 
219  int8_t updateActualMinByteWidth(const int8_t actual_min_byte_width) const;
220 
221  void addColSlotInfo(const std::vector<std::tuple<int8_t, int8_t>>& slots_for_col);
222 
223  void clearSlotInfo();
224 
225  void alignPaddedSlots();
226 
227  int64_t getTargetGroupbyIndex(const size_t target_idx) const {
228  CHECK_LT(target_idx, target_groupby_indices_.size());
229  return target_groupby_indices_[target_idx];
230  }
231 
232  void setAllTargetGroupbyIndices(std::vector<int64_t> group_by_indices) {
233  target_groupby_indices_ = group_by_indices;
234  }
235 
236  size_t targetGroupbyIndicesSize() const { return target_groupby_indices_.size(); }
238  return std::count_if(
239  target_groupby_indices_.begin(),
241  [](const int64_t& target_group_by_index) { return target_group_by_index < 0; });
242  }
244 
245  size_t getEntryCount() const { return entry_count_; }
246  void setEntryCount(const size_t val) { entry_count_ = val; }
247 
248  int64_t getMinVal() const { return min_val_; }
249  int64_t getMaxVal() const { return max_val_; }
250  int64_t getBucket() const { return bucket_; }
251 
252  bool hasNulls() const { return has_nulls_; }
253 
254  const CountDistinctDescriptor& getCountDistinctDescriptor(const size_t idx) const {
256  return count_distinct_descriptors_[idx];
257  }
259  return count_distinct_descriptors_.size();
260  }
261 
262  bool sortOnGpu() const { return sort_on_gpu_; }
263 
264  bool canOutputColumnar() const;
265  bool didOutputColumnar() const { return output_columnar_; }
266  void setOutputColumnar(const bool val);
267 
268  bool useStreamingTopN() const { return use_streaming_top_n_; }
269 
270  bool isLogicalSizedColumnsAllowed() const;
271 
273 
274  // TODO(adb): remove and store this info more naturally in another
275  // member
276  bool forceFourByteFloat() const { return force_4byte_float_; }
277  void setForceFourByteFloat(const bool val) { force_4byte_float_ = val; }
278 
279  // Getters derived from state
280  size_t getGroupbyColCount() const { return group_col_widths_.size(); }
281  size_t getKeyCount() const { return keyless_hash_ ? 0 : getGroupbyColCount(); }
282  size_t getBufferColSlotCount() const;
283 
284  size_t getBufferSizeBytes(const RelAlgExecutionUnit& ra_exe_unit,
285  const unsigned thread_count,
286  const ExecutorDeviceType device_type) const;
287  size_t getBufferSizeBytes(const ExecutorDeviceType device_type) const;
288  size_t getBufferSizeBytes(const ExecutorDeviceType device_type,
289  const size_t override_entry_count) const;
290 
292 
293  // TODO(alex): remove
294  bool usesGetGroupValueFast() const;
295 
296  bool blocksShareMemory() const;
297  bool threadsShareMemory() const;
298 
299  bool lazyInitGroups(const ExecutorDeviceType) const;
300 
301  bool interleavedBins(const ExecutorDeviceType) const;
302 
303  size_t getColOffInBytes(const size_t col_idx) const;
304  size_t getColOffInBytesInNextBin(const size_t col_idx) const;
305  size_t getNextColOffInBytes(const int8_t* col_ptr,
306  const size_t bin,
307  const size_t col_idx) const;
308 
309  // returns the ptr offset of the next column, 64-bit aligned
310  size_t getNextColOffInBytesRowOnly(const int8_t* col_ptr, const size_t col_idx) const;
311  // returns the ptr offset of the current column, 64-bit aligned
312  size_t getColOnlyOffInBytes(const size_t col_idx) const;
313  size_t getRowSize() const;
314  size_t getColsSize() const;
315  size_t getWarpCount() const;
316 
317  size_t getCompactByteWidth() const;
318 
319  inline size_t getEffectiveKeyWidth() const {
320  return group_col_compact_width_ ? group_col_compact_width_ : sizeof(int64_t);
321  }
322 
323  bool isWarpSyncRequired(const ExecutorDeviceType) const;
324 
325  std::string queryDescTypeToString() const;
326  std::string toString() const;
327 
328  std::string reductionKey() const;
329 
330  protected:
331  void resetGroupColWidths(const std::vector<int8_t>& new_group_col_widths) {
332  group_col_widths_ = new_group_col_widths;
333  }
334 
335  private:
341  int32_t idx_target_as_key_; // If keyless_hash_ enabled, then represents what target
342  // expression should be used to identify the key (e.g., in
343  // locating empty entries). Currently only valid with
344  // keyless_hash_ and single-column GroupByPerfectHash
345  std::vector<int8_t> group_col_widths_;
346  int8_t group_col_compact_width_; // compact width for all group
347  // cols if able to be consistent
348  // otherwise 0
349  std::vector<int64_t> target_groupby_indices_;
350  size_t entry_count_; // the number of entries in the main buffer
351  int64_t min_val_; // meaningful for OneColKnownRange,
352  // MultiColPerfectHash only
353  int64_t max_val_;
354  int64_t bucket_;
363 
365 
367 
368  size_t getTotalBytesOfColumnarBuffers() const;
369  size_t getTotalBytesOfColumnarBuffers(const size_t num_entries_per_column) const;
370  size_t getTotalBytesOfColumnarProjections(const size_t projection_count) const;
371 
372  friend class ResultSet;
373  friend class QueryExecutionContext;
374 };
375 
376 inline void set_notnull(TargetInfo& target, const bool not_null) {
377  target.skip_null_val = !not_null;
378  auto new_type = get_compact_type(target);
379  new_type.set_notnull(not_null);
380  set_compact_type(target, new_type);
381 }
382 
383 std::vector<TargetInfo> target_exprs_to_infos(
384  const std::vector<Analyzer::Expr*>& targets,
386 
387 #endif // QUERYENGINE_QUERYMEMORYDESCRIPTOR_H
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 countDistinctDescriptorsLogicallyEmpty() 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
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 skip_null_val
Definition: TargetInfo.h:44
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:35
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
std::unique_ptr< QueryExecutionContext > getQueryExecutionContext(const RelAlgExecutionUnit &, const Executor *executor, const ExecutorDeviceType device_type, const ExecutorDispatchMode dispatch_mode, const int device_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
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
#define CHECK_LT(x, y)
Definition: Logger.h:213
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 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 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)
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