Repository navigation
Expand file tree
/
Copy pathMergingAggregatedStep.cpp
More file actions
336 lines (287 loc) · 13.5 KB
/
Copy pathMergingAggregatedStep.cpp
File metadata and controls
336 lines (287 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <Interpreters/Context.h>
#include <Processors/Merges/FinishAggregatingInOrderTransform.h>
#include <Processors/QueryPlan/MergingAggregatedStep.h>
#include <Processors/QueryPlan/QueryPlanSerializationSettings.h>
#include <Processors/QueryPlan/QueryPlanStepRegistry.h>
#include <Processors/QueryPlan/Serialization.h>
#include <Processors/Transforms/AggregatingTransform.h>
#include <Processors/Transforms/MemoryBoundMerging.h>
#include <Processors/Transforms/MergingAggregatedMemoryEfficientTransform.h>
#include <Processors/Transforms/MergingAggregatedTransform.h>
#include <QueryPipeline/QueryPipelineBuilder.h>
#include <Common/JSONBuilder.h>
#include <Core/Settings.h>
namespace DB
{
namespace QueryPlanSerializationSetting
{
extern const QueryPlanSerializationSettingsUInt64 aggregation_in_order_max_block_bytes;
extern const QueryPlanSerializationSettingsBool collect_hash_table_stats_during_aggregation;
extern const QueryPlanSerializationSettingsUInt64 max_block_size;
extern const QueryPlanSerializationSettingsUInt64 max_entries_for_hash_table_stats;
extern const QueryPlanSerializationSettingsUInt64 max_size_to_preallocate_for_aggregation;
extern const QueryPlanSerializationSettingsFloat min_hit_rate_to_use_consecutive_keys_optimization;
extern const QueryPlanSerializationSettingsBool distributed_aggregation_memory_efficient;
}
namespace Setting
{
extern const SettingsMaxThreads max_threads;
extern const SettingsUInt64 aggregation_memory_efficient_merge_threads;
extern const SettingsBool enable_memory_bound_merging_of_aggregation_results;
}
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int INCORRECT_DATA;
}
static ITransformingStep::Traits getTraits(bool should_produce_results_in_order_of_bucket_number)
{
return ITransformingStep::Traits
{
{
.returns_single_stream = should_produce_results_in_order_of_bucket_number,
.preserves_number_of_streams = false,
.preserves_sorting = false,
},
{
.preserves_number_of_rows = false,
}
};
}
MergingAggregatedStep::MergingAggregatedStep(
const SharedHeader & input_header_,
Aggregator::Params params_,
GroupingSetsParamsList grouping_sets_params_,
bool final_,
bool memory_efficient_aggregation_,
size_t memory_efficient_merge_threads_,
bool should_produce_results_in_order_of_bucket_number_,
size_t max_block_size_,
size_t memory_bound_merging_max_block_bytes_,
bool memory_bound_merging_of_aggregation_results_enabled_)
: ITransformingStep(
input_header_,
std::make_shared<const Block>(MergingAggregatedTransform::appendGroupingIfNeeded(*input_header_, params_.getHeader(*input_header_, final_))),
getTraits(should_produce_results_in_order_of_bucket_number_))
, params(std::move(params_))
, grouping_sets_params(std::move(grouping_sets_params_))
, final(final_)
, memory_efficient_aggregation(memory_efficient_aggregation_)
, max_threads(params.max_threads)
, memory_efficient_merge_threads(memory_efficient_merge_threads_)
, max_block_size(max_block_size_)
, memory_bound_merging_max_block_bytes(memory_bound_merging_max_block_bytes_)
, should_produce_results_in_order_of_bucket_number(should_produce_results_in_order_of_bucket_number_)
, memory_bound_merging_of_aggregation_results_enabled(memory_bound_merging_of_aggregation_results_enabled_)
{
}
void MergingAggregatedStep::applyOrder(SortDescription input_sort_description)
{
/// Columns might be reordered during optimization, so we better to update sort description.
group_by_sort_description = std::move(input_sort_description);
}
void MergingAggregatedStep::transformPipeline(QueryPipelineBuilder & pipeline, const BuildQueryPipelineSettings &)
{
if (memoryBoundMergingWillBeUsed())
{
if (input_headers.front()->has("__grouping_set") || !grouping_sets_params.empty())
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Memory bound merging of aggregated results is not supported for grouping sets.");
auto transform_params = std::make_shared<AggregatingTransformParams>(pipeline.getSharedHeader(), std::move(params), final);
auto transform = std::make_shared<FinishAggregatingInOrderTransform>(
pipeline.getSharedHeader(),
pipeline.getNumStreams(),
transform_params,
group_by_sort_description,
max_block_size,
memory_bound_merging_max_block_bytes);
pipeline.addTransform(std::move(transform));
/// Do merge of aggregated data in parallel.
pipeline.resize(max_threads);
const auto & required_sort_description
= should_produce_results_in_order_of_bucket_number ? group_by_sort_description : SortDescription{};
pipeline.addSimpleTransform(
[&](const SharedHeader &) { return std::make_shared<MergingAggregatedBucketTransform>(transform_params, required_sort_description); });
if (should_produce_results_in_order_of_bucket_number)
{
pipeline.addTransform(
std::make_shared<SortingAggregatedForMemoryBoundMergingTransform>(pipeline.getHeader(), pipeline.getNumStreams()));
}
return;
}
if (!memory_efficient_aggregation)
{
/// We union several sources into one, paralleling the work.
pipeline.resize(1);
/// Now merge the aggregated blocks
auto transform = std::make_shared<MergingAggregatedTransform>(pipeline.getSharedHeader(), params, final, grouping_sets_params);
pipeline.addTransform(std::move(transform));
}
else
{
if (input_headers.front()->has("__grouping_set") || !grouping_sets_params.empty())
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Memory efficient merging of aggregated results is not supported for grouping sets.");
auto num_merge_threads = memory_efficient_merge_threads
? memory_efficient_merge_threads
: max_threads;
auto transform_params = std::make_shared<AggregatingTransformParams>(pipeline.getSharedHeader(), std::move(params), final);
pipeline.addMergingAggregatedMemoryEfficientTransform(transform_params, num_merge_threads);
}
pipeline.resize(should_produce_results_in_order_of_bucket_number ? 1 : max_threads);
}
void MergingAggregatedStep::describeActions(FormatSettings & settings) const
{
params.explain(settings.out, settings.offset);
if (!group_by_sort_description.empty())
{
String prefix(settings.offset, settings.indent_char);
settings.out << prefix << "Order: " << dumpSortDescription(group_by_sort_description) << '\n';
}
}
void MergingAggregatedStep::describeActions(JSONBuilder::JSONMap & map) const
{
params.explain(map);
if (!group_by_sort_description.empty())
map.add("Order", dumpSortDescription(group_by_sort_description));
}
void MergingAggregatedStep::updateOutputHeader()
{
const auto & in_header = input_headers.front();
output_header = std::make_shared<const Block>(MergingAggregatedTransform::appendGroupingIfNeeded(*in_header, params.getHeader(*in_header, final)));
}
bool MergingAggregatedStep::memoryBoundMergingWillBeUsed() const
{
return memory_bound_merging_of_aggregation_results_enabled && !group_by_sort_description.empty();
}
const SortDescription & MergingAggregatedStep::getSortDescription() const
{
if (memoryBoundMergingWillBeUsed() && should_produce_results_in_order_of_bucket_number)
return group_by_sort_description;
return IQueryPlanStep::getSortDescription();
}
void MergingAggregatedStep::serializeSettings(QueryPlanSerializationSettings & settings) const
{
settings[QueryPlanSerializationSetting::max_block_size] = max_block_size;
settings[QueryPlanSerializationSetting::aggregation_in_order_max_block_bytes] = memory_bound_merging_max_block_bytes;
settings[QueryPlanSerializationSetting::min_hit_rate_to_use_consecutive_keys_optimization] = params.min_hit_rate_to_use_consecutive_keys_optimization;
settings[QueryPlanSerializationSetting::collect_hash_table_stats_during_aggregation] = params.stats_collecting_params.isCollectionAndUseEnabled();
settings[QueryPlanSerializationSetting::max_entries_for_hash_table_stats] = params.stats_collecting_params.max_entries_for_hash_table_stats;
settings[QueryPlanSerializationSetting::max_size_to_preallocate_for_aggregation] = params.stats_collecting_params.max_size_to_preallocate;
settings[QueryPlanSerializationSetting::distributed_aggregation_memory_efficient] = memory_efficient_aggregation;
}
void MergingAggregatedStep::serialize(Serialization & ctx) const
{
UInt8 flags = 0;
if (final)
flags |= 1;
if (params.overflow_row)
flags |= 2;
if (!grouping_sets_params.empty())
flags |= 4;
if (params.stats_collecting_params.isCollectionAndUseEnabled())
flags |= 8;
if (should_produce_results_in_order_of_bucket_number)
flags |= 16;
if (memory_bound_merging_of_aggregation_results_enabled)
flags |= 32;
writeIntBinary(flags, ctx.out);
writeVarUInt(params.keys.size(), ctx.out);
for (const auto & key : params.keys)
writeStringBinary(key, ctx.out);
if (!grouping_sets_params.empty())
{
writeVarUInt(grouping_sets_params.size(), ctx.out);
for (const auto & grouping_set : grouping_sets_params)
{
/// Only used keys are needed.
writeVarUInt(grouping_set.used_keys.size(), ctx.out);
for (const auto & used_key : grouping_set.used_keys)
writeStringBinary(used_key, ctx.out);
}
}
serializeAggregateDescriptions(params.aggregates, ctx.out);
serializeSortDescription(group_by_sort_description, ctx.out);
if (params.stats_collecting_params.isCollectionAndUseEnabled())
writeIntBinary(params.stats_collecting_params.key, ctx.out);
}
std::unique_ptr<IQueryPlanStep> MergingAggregatedStep::deserialize(Deserialization & ctx)
{
if (ctx.input_headers.size() != 1)
throw Exception(ErrorCodes::INCORRECT_DATA, "MergingAggregatedStep must have one input stream");
UInt8 flags;
readIntBinary(flags, ctx.in);
const bool final = bool(flags & 1);
const bool overflow_row = bool(flags & 2);
const bool has_grouping_sets = bool(flags & 4);
const bool has_stats_key = bool(flags & 8);
const bool should_produce_results_in_order_of_bucket_number = bool(flags & 16);
const bool memory_bound_merging_of_aggregation_results_enabled = bool(flags & 32);
UInt64 num_keys;
readVarUInt(num_keys, ctx.in);
Names keys(num_keys);
for (auto & key : keys)
readStringBinary(key, ctx.in);
GroupingSetsParamsList grouping_sets_params;
if (has_grouping_sets)
{
UInt64 num_groups;
readVarUInt(num_groups, ctx.in);
for (size_t group_num = 0; group_num < num_groups; ++group_num)
{
auto & grouping_set = grouping_sets_params.emplace_back();
UInt64 num_used_keys;
readVarUInt(num_used_keys, ctx.in);
grouping_set.used_keys.resize(num_used_keys);
NameSet used_keys_set;
for (auto & used_key : grouping_set.used_keys)
{
readStringBinary(used_key, ctx.in);
used_keys_set.insert(used_key);
}
if (num_keys > num_used_keys)
grouping_set.missing_keys.reserve(num_keys - num_used_keys);
for (const auto & key : keys)
if (!used_keys_set.contains(key))
grouping_set.missing_keys.push_back(key);
}
}
AggregateDescriptions aggregates;
deserializeAggregateDescriptions(aggregates, ctx.in);
SortDescription group_by_sort_description;
deserializeSortDescription(group_by_sort_description, ctx.in);
UInt64 stats_key = 0;
if (has_stats_key)
readIntBinary(stats_key, ctx.in);
StatsCollectingParams stats_collecting_params(
stats_key,
ctx.settings[QueryPlanSerializationSetting::collect_hash_table_stats_during_aggregation],
ctx.settings[QueryPlanSerializationSetting::max_entries_for_hash_table_stats],
ctx.settings[QueryPlanSerializationSetting::max_size_to_preallocate_for_aggregation]);
const auto & settings = ctx.context->getSettingsRef();
Aggregator::Params params(
keys,
aggregates,
overflow_row,
settings[Setting::max_threads],
ctx.settings[QueryPlanSerializationSetting::max_block_size],
ctx.settings[QueryPlanSerializationSetting::min_hit_rate_to_use_consecutive_keys_optimization]);
auto merging_aggregated_step = std::make_unique<MergingAggregatedStep>(
ctx.input_headers.front(),
std::move(params),
std::move(grouping_sets_params),
final,
ctx.settings[QueryPlanSerializationSetting::distributed_aggregation_memory_efficient],
settings[Setting::aggregation_memory_efficient_merge_threads],
should_produce_results_in_order_of_bucket_number,
ctx.settings[QueryPlanSerializationSetting::max_block_size],
ctx.settings[QueryPlanSerializationSetting::aggregation_in_order_max_block_bytes],
memory_bound_merging_of_aggregation_results_enabled);
merging_aggregated_step->applyOrder(std::move(group_by_sort_description));
return merging_aggregated_step;
}
void registerMergingAggregatedStep(QueryPlanStepRegistry & registry)
{
registry.registerStep("MergingAggregated", MergingAggregatedStep::deserialize);
}
}