OmniSciDB  2e3a973ef4
WindowContext.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 OmniSci, 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 #pragma once
18 
19 #include "Analyzer/Analyzer.h"
20 #include "DataMgr/Chunk/Chunk.h"
22 
23 #include <functional>
24 #include <unordered_map>
25 
26 // Returns true for value window functions, false otherwise.
28  switch (kind) {
33  return true;
34  }
35  default: {
36  return false;
37  }
38  }
39 }
40 
41 // Returns true for aggregate window functions, false otherwise.
43  switch (kind) {
50  return true;
51  }
52  default: {
53  return false;
54  }
55  }
56 }
57 
58 class Executor;
59 
60 // Per-window function context which encapsulates the logic for computing the various
61 // window function kinds and keeps ownership of buffers which contain the results. For
62 // rank functions, the code generated for the projection simply reads the values and
63 // writes them to the result set. For value and aggregate functions, only the iteration
64 // order is written to the buffer, the rest is handled by generating code in a similar way
65 // we do for non-window queries.
67  public:
69  const std::shared_ptr<JoinHashTableInterface>& partitions,
70  const size_t elem_count,
71  const ExecutorDeviceType device_type,
72  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner);
73 
75 
77 
79 
80  // Adds the order column buffer to the context and keeps ownership of it.
81  void addOrderColumn(const int8_t* column,
82  const Analyzer::ColumnVar* col_var,
83  const std::vector<std::shared_ptr<Chunk_NS::Chunk>>& chunks_owner);
84 
85  // Computes the window function result to be used during the actual projection query.
86  void compute();
87 
88  // Returns a pointer to the window function associated with this context.
90 
91  // Returns a pointer to the output buffer of the window function result.
92  const int8_t* output() const;
93 
94  // Returns a pointer to the value field of the aggregation state.
95  const int64_t* aggregateState() const;
96 
97  // Returns a pointer to the count field of the aggregation state.
98  const int64_t* aggregateStateCount() const;
99 
100  // Returns a handle to the pending outputs for the aggregate window function.
101  int64_t aggregateStatePendingOutputs() const;
102 
103  // Returns a pointer to the partition start bitmap.
104  const int8_t* partitionStart() const;
105 
106  // Returns a pointer to the partition end bitmap.
107  const int8_t* partitionEnd() const;
108 
109  // Returns the element count in the columns used by the window function.
110  size_t elementCount() const;
111 
112  // Sets the row number expression for this window function.
113  void setRowNumber(llvm::Value* row_number);
114 
115  // Gets the row number expression for this window function.
116  llvm::Value* getRowNumber() const;
117 
118  using Comparator = std::function<bool(const int64_t lhs, const int64_t rhs)>;
119 
120  private:
121  // State for a window aggregate. The count field is only used for average.
122  struct AggregateState {
123  int64_t val;
124  int64_t count;
125  std::vector<void*> outputs;
126  llvm::Value* row_number = nullptr;
127  };
128 
129  static Comparator makeComparator(const Analyzer::ColumnVar* col_var,
130  const int8_t* partition_values,
131  const int32_t* partition_indices,
132  const bool nulls_first);
133 
134  void computePartition(
135  int64_t* output_for_partition_buff,
136  const size_t partition_size,
137  const size_t off,
138  const Analyzer::WindowFunction* window_func,
139  const std::function<bool(const int64_t lhs, const int64_t rhs)>& comparator);
140 
141  void fillPartitionStart();
142 
143  void fillPartitionEnd();
144 
145  const int32_t* payload() const;
146 
147  const int32_t* offsets() const;
148 
149  const int32_t* counts() const;
150 
151  size_t partitionCount() const;
152 
154  // Keeps ownership of order column.
155  std::vector<std::vector<std::shared_ptr<Chunk_NS::Chunk>>> order_columns_owner_;
156  // Order column buffers.
157  std::vector<const int8_t*> order_columns_;
158  // Hash table which contains the partitions specified by the window.
159  std::shared_ptr<JoinHashTableInterface> partitions_;
160  // The number of elements in the table.
161  size_t elem_count_;
162  // The output of the window function.
163  int8_t* output_;
164  // Markers for partition start used to reinitialize state for aggregate window
165  // functions.
167  // Markers for partition start used to reinitialize state for aggregate window
168  // functions.
169  int8_t* partition_end_;
170  // State for aggregate function over a window.
173  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner_;
174 };
175 
176 // Keeps track of the multiple window functions in a window query.
178  public:
179  void addWindowFunctionContext(
180  std::unique_ptr<WindowFunctionContext> window_function_context,
181  const size_t target_index);
182 
183  // Marks the window function at the given target index as active. This simplifies the
184  // code generation since it's now context sensitive. Each value window function can have
185  // its own iteration order, therefore fetching a column at a given position changes
186  // depending on which window function is active.
187  const WindowFunctionContext* activateWindowFunctionContext(
188  Executor* executor,
189  const size_t target_index) const;
190 
191  // Resets the active window function, which restores the regular (non-window) codegen
192  // behavior.
193  static void resetWindowFunctionContext(Executor* executor);
194 
195  // Gets the current active window function.
196  static WindowFunctionContext* getActiveWindowFunctionContext(Executor* executor);
197 
198  // Creates the context for a window function execution unit.
199  static WindowProjectNodeContext* create(Executor* executor);
200 
201  // Retrieves the context for the active window function execution unit.
202  static const WindowProjectNodeContext* get(Executor* executor);
203 
204  // Resets the active context.
205  static void reset(Executor* executor);
206 
207  private:
208  // A map from target index to the context associated with the window function at that
209  // target index.
210  std::unordered_map<size_t, std::unique_ptr<WindowFunctionContext>> window_contexts_;
211 };
212 
214 
Defines data structures for the semantic analysis phase of query processing.
size_t elementCount() const
std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner_
const int32_t * payload() const
ExecutorDeviceType
const int32_t * counts() const
std::function< bool(const int64_t lhs, const int64_t rhs)> Comparator
const Analyzer::WindowFunction * window_func_
size_t partitionCount() const
void computePartition(int64_t *output_for_partition_buff, const size_t partition_size, const size_t off, const Analyzer::WindowFunction *window_func, const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator)
AggregateState aggregate_state_
bool window_function_is_value(const SqlWindowFunctionKind kind)
Definition: WindowContext.h:27
const Analyzer::WindowFunction * getWindowFunction() const
void setRowNumber(llvm::Value *row_number)
const int64_t * aggregateState() const
WindowFunctionContext(const Analyzer::WindowFunction *window_func, const std::shared_ptr< JoinHashTableInterface > &partitions, const size_t elem_count, const ExecutorDeviceType device_type, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner)
static Comparator makeComparator(const Analyzer::ColumnVar *col_var, const int8_t *partition_values, const int32_t *partition_indices, const bool nulls_first)
WindowFunctionContext & operator=(const WindowFunctionContext &)=delete
std::shared_ptr< JoinHashTableInterface > partitions_
llvm::Value * getRowNumber() const
bool window_function_is_aggregate(const SqlWindowFunctionKind kind)
Definition: WindowContext.h:42
int64_t aggregateStatePendingOutputs() const
const int8_t * partitionStart() const
const int32_t * offsets() const
const int64_t * aggregateStateCount() const
SqlWindowFunctionKind
Definition: sqldefs.h:82
const int8_t * output() const
std::unordered_map< size_t, std::unique_ptr< WindowFunctionContext > > window_contexts_
std::vector< const int8_t * > order_columns_
Executor(const ExecutorId id, const size_t block_size_x, const size_t grid_size_x, const size_t max_gpu_slab_size, const std::string &debug_dir, const std::string &debug_file)
Definition: Execute.cpp:131
bool window_function_requires_peer_handling(const Analyzer::WindowFunction *window_func)
const int8_t * partitionEnd() const
std::vector< std::vector< std::shared_ptr< Chunk_NS::Chunk > > > order_columns_owner_
void addOrderColumn(const int8_t *column, const Analyzer::ColumnVar *col_var, const std::vector< std::shared_ptr< Chunk_NS::Chunk >> &chunks_owner)
const ExecutorDeviceType device_type_