Repository navigation
Expand file tree
/
Copy pathCreatingSetsStep.cpp
More file actions
260 lines (211 loc) · 8.3 KB
/
Copy pathCreatingSetsStep.cpp
File metadata and controls
260 lines (211 loc) · 8.3 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
#include <Processors/QueryPlan/CreatingSetsStep.h>
#include <Processors/QueryPlan/QueryPlan.h>
#include <Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.h>
#include <QueryPipeline/QueryPipelineBuilder.h>
#include <Processors/Transforms/CreatingSetsTransform.h>
#include <IO/Operators.h>
#include <Common/JSONBuilder.h>
#include <Core/Settings.h>
#include <Interpreters/PreparedSets.h>
#include <Interpreters/Context.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
namespace Setting
{
extern const SettingsUInt64 max_bytes_to_transfer;
extern const SettingsUInt64 max_rows_to_transfer;
extern const SettingsOverflowMode transfer_overflow_mode;
}
static ITransformingStep::Traits getTraits()
{
return ITransformingStep::Traits
{
{
.returns_single_stream = false,
.preserves_number_of_streams = true,
.preserves_sorting = true,
},
{
.preserves_number_of_rows = true,
}
};
}
CreatingSetStep::CreatingSetStep(
const SharedHeader & input_header_,
SetAndKeyPtr set_and_key_,
StoragePtr external_table_,
SizeLimits network_transfer_limits_,
PreparedSetsCachePtr prepared_sets_cache_)
: ITransformingStep(input_header_, std::make_shared<const Block>(Block{}), getTraits())
, set_and_key(std::move(set_and_key_))
, external_table(std::move(external_table_))
, network_transfer_limits(std::move(network_transfer_limits_))
, prepared_sets_cache(std::move(prepared_sets_cache_))
{
}
void CreatingSetStep::transformPipeline(QueryPipelineBuilder & pipeline, const BuildQueryPipelineSettings &)
{
pipeline.addCreatingSetsTransform(
getOutputHeader(),
std::move(set_and_key),
std::move(external_table),
network_transfer_limits,
std::move(prepared_sets_cache));
}
void CreatingSetStep::updateOutputHeader()
{
output_header = std::make_shared<const Block>(Block{});
}
void CreatingSetStep::describeActions(FormatSettings & settings) const
{
String prefix(settings.offset, ' ');
settings.out << prefix;
if (set_and_key->set)
settings.out << "Set: ";
settings.out << set_and_key->key << '\n';
}
void CreatingSetStep::describeActions(JSONBuilder::JSONMap & map) const
{
if (set_and_key->set)
map.add("Set", set_and_key->key);
}
CreatingSetsStep::CreatingSetsStep(SharedHeaders input_headers_)
{
if (input_headers_.empty())
throw Exception(ErrorCodes::LOGICAL_ERROR, "CreatingSetsStep cannot be created with no inputs");
input_headers = std::move(input_headers_);
output_header = input_headers.front();
for (size_t i = 1; i < input_headers.size(); ++i)
if (!input_headers[i]->empty())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Creating set input must have empty header. Got: {}",
input_headers[i]->dumpStructure());
}
QueryPipelineBuilderPtr CreatingSetsStep::updatePipeline(QueryPipelineBuilders pipelines, const BuildQueryPipelineSettings &)
{
if (pipelines.empty())
throw Exception(ErrorCodes::LOGICAL_ERROR, "CreatingSetsStep cannot be created with no inputs");
auto main_pipeline = std::move(pipelines.front());
if (pipelines.size() == 1)
return main_pipeline;
pipelines.erase(pipelines.begin());
QueryPipelineBuilder delayed_pipeline;
if (pipelines.size() > 1)
{
QueryPipelineProcessorsCollector collector(delayed_pipeline, this);
delayed_pipeline = QueryPipelineBuilder::unitePipelines(std::move(pipelines));
processors = collector.detachProcessors();
}
else
delayed_pipeline = std::move(*pipelines.front());
QueryPipelineProcessorsCollector collector(*main_pipeline, this);
main_pipeline->addPipelineBefore(std::move(delayed_pipeline));
auto added_processors = collector.detachProcessors();
processors.insert(processors.end(), added_processors.begin(), added_processors.end());
return main_pipeline;
}
void CreatingSetsStep::describePipeline(FormatSettings & settings) const
{
IQueryPlanStep::describePipeline(processors, settings);
}
void addCreatingSetsStep(QueryPlan & query_plan, PreparedSets::Subqueries subqueries, ContextPtr context)
{
SharedHeaders input_headers;
input_headers.emplace_back(query_plan.getCurrentHeader());
std::vector<std::unique_ptr<QueryPlan>> plans;
plans.emplace_back(std::make_unique<QueryPlan>(std::move(query_plan)));
query_plan = QueryPlan();
const auto & settings = context->getSettingsRef();
SizeLimits network_transfer_limits(settings[Setting::max_rows_to_transfer], settings[Setting::max_bytes_to_transfer], settings[Setting::transfer_overflow_mode]);
auto prepared_sets_cache = context->getPreparedSetsCache();
for (auto & future_set : subqueries)
{
if (future_set->get())
continue;
auto plan = future_set->build(network_transfer_limits, prepared_sets_cache);
if (!plan)
continue;
input_headers.emplace_back(plan->getCurrentHeader());
plans.emplace_back(std::move(plan));
}
if (plans.size() == 1)
{
query_plan = std::move(*plans.front());
return;
}
auto creating_sets = std::make_unique<CreatingSetsStep>(std::move(input_headers));
creating_sets->setStepDescription("Create sets before main query execution");
query_plan.unitePlans(std::move(creating_sets), std::move(plans));
}
QueryPipelineBuilderPtr addCreatingSetsTransform(QueryPipelineBuilderPtr pipeline, PreparedSets::Subqueries subqueries, ContextPtr context)
{
SharedHeaders input_headers;
input_headers.emplace_back(pipeline->getSharedHeader());
QueryPipelineBuilders pipelines;
pipelines.reserve(1 + subqueries.size());
pipelines.push_back(std::move(pipeline));
QueryPlanOptimizationSettings plan_settings(context);
BuildQueryPipelineSettings pipeline_settings(context);
const auto & settings = context->getSettingsRef();
SizeLimits network_transfer_limits(settings[Setting::max_rows_to_transfer], settings[Setting::max_bytes_to_transfer], settings[Setting::transfer_overflow_mode]);
auto prepared_sets_cache = context->getPreparedSetsCache();
for (auto & future_set : subqueries)
{
if (future_set->get())
continue;
auto plan = future_set->build(network_transfer_limits, prepared_sets_cache);
if (!plan)
continue;
input_headers.emplace_back(plan->getCurrentHeader());
pipelines.emplace_back(plan->buildQueryPipeline(plan_settings, pipeline_settings));
}
return CreatingSetsStep(input_headers).updatePipeline(std::move(pipelines), pipeline_settings);
}
std::vector<std::unique_ptr<QueryPlan>> DelayedCreatingSetsStep::makePlansForSets(
DelayedCreatingSetsStep && step,
const QueryPlanOptimizationSettings & optimization_settings)
{
std::vector<std::unique_ptr<QueryPlan>> plans;
for (auto & future_set : step.subqueries)
{
if (future_set->get())
continue;
auto plan = future_set->build(optimization_settings.network_transfer_limits, optimization_settings.prepared_sets_cache);
if (!plan)
continue;
plan->optimize(optimization_settings);
plans.emplace_back(std::move(plan));
}
return plans;
}
void addCreatingSetsStep(QueryPlan & query_plan, PreparedSetsPtr prepared_sets, ContextPtr context)
{
if (!prepared_sets)
return;
auto subqueries = prepared_sets->getSubqueries();
if (subqueries.empty())
return;
addCreatingSetsStep(query_plan, std::move(subqueries), context);
}
DelayedCreatingSetsStep::DelayedCreatingSetsStep(
SharedHeader input_header,
PreparedSets::Subqueries subqueries_,
SizeLimits network_transfer_limits_,
PreparedSetsCachePtr prepared_sets_cache_)
: subqueries(std::move(subqueries_))
, network_transfer_limits(std::move(network_transfer_limits_))
, prepared_sets_cache(std::move(prepared_sets_cache_))
{
input_headers = {input_header};
output_header = std::move(input_header);
}
QueryPipelineBuilderPtr DelayedCreatingSetsStep::updatePipeline(QueryPipelineBuilders, const BuildQueryPipelineSettings &)
{
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Cannot build pipeline in DelayedCreatingSets. This step should be optimized out.");
}
}