CodeCommitsIssuesPull requestsActionsInsightsSecurity
addee61bcb6179bf5807f3437980589b604d298a

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Columns/ColumnAggregateFunction.h

217lines · modecode

1#pragma once
2
3#include <AggregateFunctions/IAggregateFunction.h>
4
5#include <Columns/IColumn.h>
6#include <Common/PODArray.h>
7
8#include <Core/Field.h>
9
10#include <IO/ReadBufferFromString.h>
11#include <IO/WriteBuffer.h>
12#include <IO/WriteHelpers.h>
13
14#include <Functions/FunctionHelpers.h>
15
16namespace DB
17{
18
19namespace ErrorCodes
20{
21 extern const int NOT_IMPLEMENTED;
22}
23
24class Arena;
25using ArenaPtr = std::shared_ptr<Arena>;
26using ConstArenaPtr = std::shared_ptr<const Arena>;
27using ConstArenas = std::vector<ConstArenaPtr>;
28
29
30/** Column of states of aggregate functions.
31 * Presented as an array of pointers to the states of aggregate functions (data).
32 * The states themselves are stored in one of the pools (arenas).
33 *
34 * It can be in two variants:
35 *
36 * 1. Own its values - that is, be responsible for destroying them.
37 * The column consists of the values "assigned to it" after the aggregation is performed (see Aggregator, convertToBlocks function),
38 * or from values created by itself (see `insert` method).
39 * In this case, `src` will be `nullptr`, and the column itself will be destroyed (call `IAggregateFunction::destroy`)
40 * states of aggregate functions in the destructor.
41 *
42 * 2. Do not own its values, but use values taken from another ColumnAggregateFunction column.
43 * For example, this is a column obtained by permutation/filtering or other transformations from another column.
44 * In this case, `src` will be `shared ptr` to the source column. Destruction of values will be handled by this source column.
45 *
46 * This solution is somewhat limited:
47 * - the variant in which the column contains a part of "it's own" and a part of "another's" values is not supported;
48 * - the option of having multiple source columns is not supported, which may be necessary for a more optimal merge of the two columns.
49 *
50 * These restrictions can be removed if you add an array of flags or even refcount,
51 * specifying which individual values should be destroyed and which ones should not.
52 * Clearly, this method would have a substantially non-zero price.
53 */
54class ColumnAggregateFunction final : public COWHelper<IColumn, ColumnAggregateFunction>
55{
56public:
57 using Container = PaddedPODArray<AggregateDataPtr>;
58
59private:
60 friend class COWHelper<IColumn, ColumnAggregateFunction>;
61
62 /// Arenas used by function states that are created elsewhere. We own these
63 /// arenas in the sense of extending their lifetime, but do not modify them.
64 /// Even reading these arenas is unsafe, because they may be shared with
65 /// other data blocks and modified by other threads concurrently.
66 ConstArenas foreign_arenas;
67
68 /// Arena for allocating the internals of function states created by current
69 /// column (e.g., when inserting new states).
70 ArenaPtr my_arena;
71
72 /// Used for destroying states and for finalization of values.
73 AggregateFunctionPtr func;
74
75 /// Source column. Used (holds source from destruction),
76 /// if this column has been constructed from another and uses all or part of its values.
77 ColumnPtr src;
78
79 /// Array of pointers to aggregation states, that are placed in arenas.
80 Container data;
81
82 /// Name of the type to distinguish different aggregation states.
83 String type_string;
84
85 ColumnAggregateFunction() {}
86
87 /// Create a new column that has another column as a source.
88 MutablePtr createView() const;
89
90 /// If we have another column as a source (owner of data), copy all data to ourself and reset source.
91 /// This is needed before inserting new elements, because we must own these elements (to destroy them in destructor),
92 /// but ownership of different elements cannot be mixed by different columns.
93 void ensureOwnership();
94
95 ColumnAggregateFunction(const AggregateFunctionPtr & func_);
96
97 ColumnAggregateFunction(const AggregateFunctionPtr & func_,
98 const ConstArenas & arenas_);
99
100 ColumnAggregateFunction(const ColumnAggregateFunction & src_);
101
102public:
103 ~ColumnAggregateFunction() override;
104
105 void set(const AggregateFunctionPtr & func_);
106
107 AggregateFunctionPtr getAggregateFunction() { return func; }
108 AggregateFunctionPtr getAggregateFunction() const { return func; }
109
110 /// Take shared ownership of Arena, that holds memory for states of aggregate functions.
111 void addArena(ConstArenaPtr arena_);
112
113 /// Transform column with states of aggregate functions to column with final result values.
114 /// It expects ColumnAggregateFunction as an argument, this column will be destroyed.
115 /// This method is made static and receive MutableColumnPtr object to explicitly destroy it.
116 static MutableColumnPtr convertToValues(MutableColumnPtr column);
117
118 std::string getName() const override { return "AggregateFunction(" + func->getName() + ")"; }
119 const char * getFamilyName() const override { return "AggregateFunction"; }
120 TypeIndex getDataType() const override { return TypeIndex::AggregateFunction; }
121
122 MutableColumnPtr predictValues(Block & block, const ColumnNumbers & arguments, const Context & context) const;
123
124 size_t size() const override
125 {
126 return getData().size();
127 }
128
129 MutableColumnPtr cloneEmpty() const override;
130
131 Field operator[](size_t n) const override;
132
133 void get(size_t n, Field & res) const override;
134
135 StringRef getDataAt(size_t n) const override;
136
137 void insertData(const char * pos, size_t length) override;
138
139 void insertFrom(const IColumn & from, size_t n) override;
140
141 void insertFrom(ConstAggregateDataPtr place);
142
143 /// Merge state at last row with specified state in another column.
144 void insertMergeFrom(ConstAggregateDataPtr place);
145
146 void insertMergeFrom(const IColumn & from, size_t n);
147
148 Arena & createOrGetArena();
149
150 void insert(const Field & x) override;
151
152 void insertDefault() override;
153
154 StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override;
155
156 const char * deserializeAndInsertFromArena(const char * src_arena) override;
157
158 void updateHashWithValue(size_t n, SipHash & hash) const override;
159
160 void updateWeakHash32(WeakHash32 & hash) const override;
161
162 size_t byteSize() const override;
163
164 size_t allocatedBytes() const override;
165
166 void protect() override;
167
168 void insertRangeFrom(const IColumn & from, size_t start, size_t length) override;
169
170 void popBack(size_t n) override;
171
172 ColumnPtr filter(const Filter & filter, ssize_t result_size_hint) const override;
173
174 ColumnPtr permute(const Permutation & perm, size_t limit) const override;
175
176 ColumnPtr index(const IColumn & indexes, size_t limit) const override;
177
178 template <typename Type>
179 ColumnPtr indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const;
180
181 ColumnPtr replicate(const Offsets & offsets) const override;
182
183 MutableColumns scatter(ColumnIndex num_columns, const Selector & selector) const override;
184
185 void gather(ColumnGathererStream & gatherer_stream) override;
186
187 int compareAt(size_t, size_t, const IColumn &, int) const override
188 {
189 return 0;
190 }
191
192 void compareColumn(const IColumn &, size_t, PaddedPODArray<UInt64> *, PaddedPODArray<Int8> &, int, int) const override
193 {
194 throw Exception("Method compareColumn is not supported for ColumnAggregateFunction", ErrorCodes::NOT_IMPLEMENTED);
195 }
196
197 void getPermutation(bool reverse, size_t limit, int nan_direction_hint, Permutation & res) const override;
198 void updatePermutation(bool reverse, size_t limit, int, Permutation & res, EqualRanges & equal_range) const override;
199
200 /** More efficient manipulation methods */
201 Container & getData()
202 {
203 return data;
204 }
205
206 const Container & getData() const
207 {
208 return data;
209 }
210
211 void getExtremes(Field & min, Field & max) const override;
212
213 bool structureEquals(const IColumn &) const override;
214};
215
216
217}