OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GroupByAndAggregate.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 
17 #ifndef QUERYENGINE_GROUPBYANDAGGREGATE_H
18 #define QUERYENGINE_GROUPBYANDAGGREGATE_H
19 
20 #include "BufferCompaction.h"
21 #include "ColumnarResults.h"
22 #include "CompilationOptions.h"
23 #include "GpuMemUtils.h"
24 #include "GpuSharedMemoryContext.h"
25 #include "InputMetadata.h"
26 #include "QueryExecutionContext.h"
27 #include "Rendering/RenderInfo.h"
28 #include "RuntimeFunctions.h"
29 
31 
32 #include "../Shared/sqltypes.h"
33 #include "Logger/Logger.h"
34 
35 #include <llvm/IR/Function.h>
36 #include <llvm/IR/Instructions.h>
37 #include <llvm/IR/Value.h>
38 #include <boost/algorithm/string/join.hpp>
39 #include <boost/make_unique.hpp>
40 
41 #include <stack>
42 #include <vector>
43 
44 extern bool g_enable_smem_group_by;
45 extern bool g_bigint_count;
46 
47 struct ColRangeInfo {
48  QueryDescriptionType hash_type_;
49  int64_t min;
50  int64_t max;
51  int64_t bucket;
52  bool has_nulls;
53  bool isEmpty() const;
54 };
55 
56 struct KeylessInfo {
57  const bool keyless;
58  const int32_t target_index;
59 };
60 
62  public:
63  GroupByAndAggregate(Executor* executor,
64  const ExecutorDeviceType device_type,
65  const RelAlgExecutionUnit& ra_exe_unit,
66  const std::vector<InputTableInfo>& query_infos,
67  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
68  const std::optional<int64_t>& group_cardinality_estimation);
69 
70  // returns true iff checking the error code after every row
71  // is required -- slow path group by queries for now
72  bool codegen(llvm::Value* filter_result,
73  llvm::BasicBlock* sc_false,
75  const CompilationOptions& co,
76  const GpuSharedMemoryContext& gpu_smem_context);
77 
78  static size_t shard_count_for_top_groups(const RelAlgExecutionUnit& ra_exe_unit);
79 
80  private:
81  bool gpuCanHandleOrderEntries(const std::list<Analyzer::OrderEntry>& order_entries);
82 
84 
85  std::unique_ptr<QueryMemoryDescriptor> initQueryMemoryDescriptor(
86  const bool allow_multifrag,
87  const size_t max_groups_buffer_entry_count,
88  const int8_t crt_min_byte_width,
89  RenderInfo* render_info,
90  const bool output_columnar_hint);
91 
92  std::unique_ptr<QueryMemoryDescriptor> initQueryMemoryDescriptorImpl(
93  const bool allow_multifrag,
94  const size_t max_groups_buffer_entry_count,
95  const int8_t crt_min_byte_width,
96  const bool sort_on_gpu_hint,
97  RenderInfo* render_info,
98  const bool must_use_baseline_sort,
99  const bool output_columnar_hint);
100 
101  int64_t getShardedTopBucket(const ColRangeInfo& col_range_info,
102  const size_t shard_count) const;
103 
104  llvm::Value* codegenOutputSlot(llvm::Value* groups_buffer,
105  const QueryMemoryDescriptor& query_mem_desc,
106  const CompilationOptions& co,
107  DiamondCodegen& diamond_codegen);
108 
109  std::tuple<llvm::Value*, llvm::Value*> codegenGroupBy(
110  const QueryMemoryDescriptor& query_mem_desc,
111  const CompilationOptions& co,
112  DiamondCodegen& codegen);
113 
114  llvm::Value* codegenVarlenOutputBuffer(const QueryMemoryDescriptor& query_mem_desc);
115 
116  std::tuple<llvm::Value*, llvm::Value*> codegenSingleColumnPerfectHash(
117  const QueryMemoryDescriptor& query_mem_desc,
118  const CompilationOptions& co,
119  llvm::Value* groups_buffer,
120  llvm::Value* group_expr_lv_translated,
121  llvm::Value* group_expr_lv_original,
122  const int32_t row_size_quad);
123 
124  std::tuple<llvm::Value*, llvm::Value*> codegenMultiColumnPerfectHash(
125  llvm::Value* groups_buffer,
126  llvm::Value* group_key,
127  llvm::Value* key_size_lv,
128  const QueryMemoryDescriptor& query_mem_desc,
129  const int32_t row_size_quad);
130  llvm::Function* codegenPerfectHashFunction();
131 
132  std::tuple<llvm::Value*, llvm::Value*> codegenMultiColumnBaselineHash(
133  const CompilationOptions& co,
134  llvm::Value* groups_buffer,
135  llvm::Value* group_key,
136  llvm::Value* key_size_lv,
137  const QueryMemoryDescriptor& query_mem_desc,
138  const size_t key_width,
139  const int32_t row_size_quad);
140 
142 
143  static int64_t getBucketedCardinality(const ColRangeInfo& col_range_info);
144 
145  llvm::Value* convertNullIfAny(const SQLTypeInfo& arg_type,
146  const TargetInfo& agg_info,
147  llvm::Value* target);
148 
149  bool codegenAggCalls(const std::tuple<llvm::Value*, llvm::Value*>& agg_out_ptr_w_idx,
150  llvm::Value* varlen_output_buffer,
151  const std::vector<llvm::Value*>& agg_out_vec,
152  QueryMemoryDescriptor& query_mem_desc,
153  const CompilationOptions& co,
154  const GpuSharedMemoryContext& gpu_smem_context,
155  DiamondCodegen& diamond_codegen);
156 
157  llvm::Value* codegenWindowRowPointer(const Analyzer::WindowFunction* window_func,
158  const QueryMemoryDescriptor& query_mem_desc,
159  const CompilationOptions& co,
160  DiamondCodegen& diamond_codegen);
161 
162  llvm::Value* codegenAggColumnPtr(
163  llvm::Value* output_buffer_byte_stream,
164  llvm::Value* out_row_idx,
165  const std::tuple<llvm::Value*, llvm::Value*>& agg_out_ptr_w_idx,
166  const QueryMemoryDescriptor& query_mem_desc,
167  const size_t chosen_bytes,
168  const size_t agg_out_off,
169  const size_t target_idx);
170 
171  void codegenEstimator(std::stack<llvm::BasicBlock*>& array_loops,
172  DiamondCodegen& diamond_codegen,
173  const QueryMemoryDescriptor& query_mem_desc,
174  const CompilationOptions&);
175 
176  void codegenCountDistinct(const size_t target_idx,
177  const Analyzer::Expr* target_expr,
178  std::vector<llvm::Value*>& agg_args,
179  const QueryMemoryDescriptor&,
180  const ExecutorDeviceType);
181 
182  void codegenApproxQuantile(const size_t target_idx,
183  const Analyzer::Expr* target_expr,
184  std::vector<llvm::Value*>& agg_args,
185  const QueryMemoryDescriptor& query_mem_desc,
186  const ExecutorDeviceType device_type);
187 
188  void codegenMode(const size_t target_idx,
189  const Analyzer::Expr* target_expr,
190  std::vector<llvm::Value*>& agg_args,
191  const QueryMemoryDescriptor& query_mem_desc,
192  const ExecutorDeviceType device_type);
193 
194  llvm::Value* getAdditionalLiteral(const int32_t off);
195 
196  std::vector<llvm::Value*> codegenAggArg(const Analyzer::Expr* target_expr,
197  const CompilationOptions& co);
198 
199  llvm::Value* emitCall(const std::string& fname, const std::vector<llvm::Value*>& args);
200 
201  void checkErrorCode(llvm::Value* retCode);
202 
203  bool needsUnnestDoublePatch(llvm::Value const* val_ptr,
204  const std::string& agg_base_name,
205  const bool threads_share_memory,
206  const CompilationOptions& co) const;
207 
208  void prependForceSync();
209 
212  const std::vector<InputTableInfo>& query_infos_;
213  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner_;
216 
217  const std::optional<int64_t> group_cardinality_estimation_;
218 
219  friend class Executor;
220  friend class QueryMemoryDescriptor;
221  friend class CodeGenerator;
222  friend class ExecutionKernel;
223  friend struct TargetExprCodegen;
225 };
226 
227 inline size_t get_count_distinct_sub_bitmap_count(const size_t bitmap_sz_bits,
229  const ExecutorDeviceType device_type) {
230  // For count distinct on a column with a very small number of distinct values
231  // contention can be very high, especially for non-grouped queries. We'll split
232  // the bitmap into multiple sub-bitmaps which are unified to get the full result.
233  // The threshold value for bitmap_sz_bits works well on Kepler.
234  return bitmap_sz_bits < 50000 && ra_exe_unit.groupby_exprs.empty() &&
235  (device_type == ExecutorDeviceType::GPU || g_cluster)
236  ? 64 // NB: must be a power of 2 to keep runtime offset computations cheap
237  : 1;
238 }
239 
240 #endif // QUERYENGINE_GROUPBYANDAGGREGATE_H
const RelAlgExecutionUnit & ra_exe_unit
ApproxQuantileDescriptors initApproxQuantileDescriptors()
bool g_enable_smem_group_by
bool gpuCanHandleOrderEntries(const std::list< Analyzer::OrderEntry > &order_entries)
static int64_t getBucketedCardinality(const ColRangeInfo &col_range_info)
llvm::Value * getAdditionalLiteral(const int32_t off)
llvm::Value * codegenAggColumnPtr(llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const QueryMemoryDescriptor &query_mem_desc, const size_t chosen_bytes, const size_t agg_out_off, const size_t target_idx)
: returns the pointer to where the aggregation should be stored.
bool codegen(llvm::Value *filter_result, llvm::BasicBlock *sc_false, QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const GpuSharedMemoryContext &gpu_smem_context)
const bool keyless
std::unique_ptr< QueryMemoryDescriptor > initQueryMemoryDescriptorImpl(const bool allow_multifrag, const size_t max_groups_buffer_entry_count, const int8_t crt_min_byte_width, const bool sort_on_gpu_hint, RenderInfo *render_info, const bool must_use_baseline_sort, const bool output_columnar_hint)
void codegenMode(const size_t target_idx, const Analyzer::Expr *target_expr, std::vector< llvm::Value * > &agg_args, const QueryMemoryDescriptor &query_mem_desc, const ExecutorDeviceType device_type)
ColRangeInfo getColRangeInfo()
QueryDescriptionType hash_type_
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner_
Macros and functions for groupby buffer compaction.
llvm::Value * codegenVarlenOutputBuffer(const QueryMemoryDescriptor &query_mem_desc)
void codegenApproxQuantile(const size_t target_idx, const Analyzer::Expr *target_expr, std::vector< llvm::Value * > &agg_args, const QueryMemoryDescriptor &query_mem_desc, const ExecutorDeviceType device_type)
void checkErrorCode(llvm::Value *retCode)
std::vector< ApproxQuantileDescriptor > ApproxQuantileDescriptors
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
bool needsUnnestDoublePatch(llvm::Value const *val_ptr, const std::string &agg_base_name, const bool threads_share_memory, const CompilationOptions &co) const
std::tuple< llvm::Value *, llvm::Value * > codegenMultiColumnBaselineHash(const CompilationOptions &co, llvm::Value *groups_buffer, llvm::Value *group_key, llvm::Value *key_size_lv, const QueryMemoryDescriptor &query_mem_desc, const size_t key_width, const int32_t row_size_quad)
ExecutorDeviceType
size_t get_count_distinct_sub_bitmap_count(const size_t bitmap_sz_bits, const RelAlgExecutionUnit &ra_exe_unit, const ExecutorDeviceType device_type)
bool isEmpty() const
GroupByAndAggregate(Executor *executor, const ExecutorDeviceType device_type, const RelAlgExecutionUnit &ra_exe_unit, const std::vector< InputTableInfo > &query_infos, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner, const std::optional< int64_t > &group_cardinality_estimation)
llvm::Value * convertNullIfAny(const SQLTypeInfo &arg_type, const TargetInfo &agg_info, llvm::Value *target)
std::tuple< llvm::Value *, llvm::Value * > codegenSingleColumnPerfectHash(const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, llvm::Value *groups_buffer, llvm::Value *group_expr_lv_translated, llvm::Value *group_expr_lv_original, const int32_t row_size_quad)
bool codegenAggCalls(const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, llvm::Value *varlen_output_buffer, const std::vector< llvm::Value * > &agg_out_vec, QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const GpuSharedMemoryContext &gpu_smem_context, DiamondCodegen &diamond_codegen)
std::tuple< llvm::Value *, llvm::Value * > codegenGroupBy(const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, DiamondCodegen &codegen)
bool g_bigint_count
void codegenCountDistinct(const size_t target_idx, const Analyzer::Expr *target_expr, std::vector< llvm::Value * > &agg_args, const QueryMemoryDescriptor &, const ExecutorDeviceType)
std::tuple< llvm::Value *, llvm::Value * > codegenMultiColumnPerfectHash(llvm::Value *groups_buffer, llvm::Value *group_key, llvm::Value *key_size_lv, const QueryMemoryDescriptor &query_mem_desc, const int32_t row_size_quad)
int64_t getShardedTopBucket(const ColRangeInfo &col_range_info, const size_t shard_count) const
const int32_t target_index
const std::vector< InputTableInfo > & query_infos_
std::unique_ptr< QueryMemoryDescriptor > initQueryMemoryDescriptor(const bool allow_multifrag, const size_t max_groups_buffer_entry_count, const int8_t crt_min_byte_width, RenderInfo *render_info, const bool output_columnar_hint)
const ExecutorDeviceType device_type_
void codegenEstimator(std::stack< llvm::BasicBlock * > &array_loops, DiamondCodegen &diamond_codegen, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &)
static size_t shard_count_for_top_groups(const RelAlgExecutionUnit &ra_exe_unit)
std::vector< llvm::Value * > codegenAggArg(const Analyzer::Expr *target_expr, const CompilationOptions &co)
llvm::Function * codegenPerfectHashFunction()
llvm::Value * codegenWindowRowPointer(const Analyzer::WindowFunction *window_func, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, DiamondCodegen &diamond_codegen)
const std::optional< int64_t > group_cardinality_estimation_
llvm::Value * codegenOutputSlot(llvm::Value *groups_buffer, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, DiamondCodegen &diamond_codegen)
bool g_cluster
const RelAlgExecutionUnit & ra_exe_unit_