Repository navigation
Expand file tree
/
Copy pathJoinStep.cpp
More file actions
338 lines (291 loc) · 10.9 KB
/
Copy pathJoinStep.cpp
File metadata and controls
338 lines (291 loc) · 10.9 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
337
338
#include <IO/Operators.h>
#include <Interpreters/IJoin.h>
#include <Interpreters/TableJoin.h>
#include <Interpreters/ExpressionActions.h>
#include <Processors/QueryPlan/JoinStep.h>
#include <Processors/Transforms/JoiningTransform.h>
#include <Processors/Transforms/SquashingTransform.h>
#include <QueryPipeline/QueryPipelineBuilder.h>
#include <Common/JSONBuilder.h>
#include <Common/typeid_cast.h>
#include <Core/BlockNameMap.h>
#include <Processors/Transforms/ColumnPermuteTransform.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
namespace
{
std::vector<std::pair<String, String>> describeJoinActions(const JoinPtr & join)
{
std::vector<std::pair<String, String>> description;
const auto & table_join = join->getTableJoin();
description.emplace_back("Type", toString(table_join.kind()));
description.emplace_back("Strictness", toString(table_join.strictness()));
description.emplace_back("Algorithm", join->getName());
if (table_join.strictness() == JoinStrictness::Asof)
description.emplace_back("ASOF inequality", toString(table_join.getAsofInequality()));
if (!table_join.getClauses().empty())
description.emplace_back("Clauses", TableJoin::formatClauses(table_join.getClauses(), true /*short_format*/));
if (const auto & mixed_expression = table_join.getMixedJoinExpression())
description.emplace_back("Residual filter", mixed_expression->getSampleBlock().dumpNames());
return description;
}
std::vector<size_t> getPermutationForBlock(
const Block & block,
const Block & lhs_block,
const Block & rhs_block,
const NameSet & name_filter)
{
std::vector<size_t> permutation;
permutation.reserve(block.columns());
BlockNameMap name_map = getNamesToIndexesMap(block);
bool is_trivial = true;
for (const auto & other_block : {lhs_block, rhs_block})
{
for (const auto & col : other_block)
{
if (!name_filter.contains(col.name))
continue;
if (auto it = name_map.find(col.name); it != name_map.end())
{
is_trivial = is_trivial && it->second == permutation.size();
permutation.push_back(it->second);
}
}
}
if (is_trivial && permutation.size() == block.columns())
return {};
return permutation;
}
}
JoinStep::JoinStep(
const SharedHeader & left_header_,
const SharedHeader & right_header_,
JoinPtr join_,
size_t max_block_size_,
size_t min_block_size_rows_,
size_t min_block_size_bytes_,
size_t max_streams_,
NameSet required_output_,
bool keep_left_read_in_order_,
bool use_new_analyzer_)
: join(std::move(join_))
, max_block_size(max_block_size_)
, min_block_size_rows(min_block_size_rows_)
, min_block_size_bytes(min_block_size_bytes_)
, max_streams(max_streams_)
, required_output(std::move(required_output_))
, keep_left_read_in_order(keep_left_read_in_order_)
, use_new_analyzer(use_new_analyzer_)
{
updateInputHeaders({left_header_, right_header_});
}
QueryPipelineBuilderPtr JoinStep::updatePipeline(QueryPipelineBuilders pipelines, const BuildQueryPipelineSettings &)
{
if (pipelines.size() != 2)
throw Exception(ErrorCodes::LOGICAL_ERROR, "JoinStep expect two input steps");
Block lhs_header = pipelines[0]->getHeader();
Block rhs_header = pipelines[1]->getHeader();
if (swap_streams)
std::swap(pipelines[0], pipelines[1]);
std::unique_ptr<QueryPipelineBuilder> joined_pipeline;
if (primary_key_sharding.empty())
{
if (join->pipelineType() == JoinPipelineType::YShaped)
{
joined_pipeline = QueryPipelineBuilder::joinPipelinesYShaped(
std::move(pipelines[0]), std::move(pipelines[1]), join, join_algorithm_header, max_block_size, &processors);
joined_pipeline->resize(max_streams);
}
else
{
joined_pipeline = QueryPipelineBuilder::joinPipelinesRightLeft(
std::move(pipelines[0]),
std::move(pipelines[1]),
join,
join_algorithm_header,
max_block_size,
min_block_size_rows,
min_block_size_bytes,
max_streams,
keep_left_read_in_order,
&processors);
}
}
else
{
if (join->pipelineType() == JoinPipelineType::YShaped)
{
joined_pipeline = QueryPipelineBuilder::joinPipelinesYShapedByShards(
std::move(pipelines[0]), std::move(pipelines[1]), join, join_algorithm_header, max_block_size, &processors);
}
else
{
joined_pipeline = QueryPipelineBuilder::joinPipelinesByShards(
std::move(pipelines[0]),
std::move(pipelines[1]),
join,
join_algorithm_header,
max_block_size,
&processors);
}
}
if (!use_new_analyzer)
return joined_pipeline;
auto column_permutation = getPermutationForBlock(joined_pipeline->getHeader(), lhs_header, rhs_header, required_output);
if (!column_permutation.empty())
{
joined_pipeline->addSimpleTransform([&column_permutation](const SharedHeader & header)
{
return std::make_shared<ColumnPermuteTransform>(header, column_permutation);
});
}
if (join->supportParallelJoin())
{
joined_pipeline->addSimpleTransform(
[&](const SharedHeader & header)
{ return std::make_shared<SimpleSquashingChunksTransform>(header, min_block_size_rows, min_block_size_bytes); });
}
const auto & pipeline_output_header = joined_pipeline->getHeader();
const auto & expected_output_header = getOutputHeader();
if (!isCompatibleHeader(pipeline_output_header, *expected_output_header))
{
assertBlocksHaveEqualStructure(pipeline_output_header, *expected_output_header,
fmt::format("JoinStep: [{}] and [{}]", pipeline_output_header.dumpNames(), expected_output_header->dumpNames()));
}
return joined_pipeline;
}
bool JoinStep::allowPushDownToRight() const
{
return join->pipelineType() == JoinPipelineType::YShaped || join->pipelineType() == JoinPipelineType::FillRightFirst;
}
void JoinStep::describePipeline(FormatSettings & settings) const
{
IQueryPlanStep::describePipeline(processors, settings);
}
void JoinStep::describeActions(FormatSettings & settings) const
{
String prefix(settings.offset, ' ');
for (const auto & [name, value] : describeJoinActions(join))
settings.out << prefix << name << ": " << value << '\n';
if (swap_streams)
settings.out << prefix << "Swapped: true\n";
if (!primary_key_sharding.empty())
{
settings.out << prefix << "Sharding: [";
bool first = true;
for (const auto & [lhs, rhs] : primary_key_sharding)
{
if (!first)
settings.out << ", ";
first = false;
settings.out << "(" << lhs << " = " << rhs << ")";
}
settings.out << "]\n";
}
}
void JoinStep::describeActions(JSONBuilder::JSONMap & map) const
{
for (const auto & [name, value] : describeJoinActions(join))
map.add(name, value);
if (swap_streams)
map.add("Swapped", true);
if (!primary_key_sharding.empty())
{
auto array = std::make_unique<JSONBuilder::JSONArray>();
for (const auto & [lhs, rhs] : primary_key_sharding)
{
auto item = std::make_unique<JSONBuilder::JSONArray>();
item->add(lhs);
item->add(rhs);
array->add(std::move(item));
}
map.add("Sharding", std::move(array));
}
}
void JoinStep::setJoin(JoinPtr join_, bool swap_streams_)
{
join_algorithm_header.reset();
swap_streams = swap_streams_;
join = std::move(join_);
updateOutputHeader();
}
void JoinStep::updateOutputHeader()
{
if (join_algorithm_header && !join_algorithm_header->empty())
return;
const auto & header = swap_streams ? input_headers[1] : input_headers[0];
join_algorithm_header = std::make_shared<const Block>(JoiningTransform::transformHeader(*header, join));
if (!use_new_analyzer)
{
if (swap_streams)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot swap streams without new analyzer");
output_header = join_algorithm_header;
return;
}
auto column_permutation = getPermutationForBlock(*join_algorithm_header, *input_headers[0], *input_headers[1], required_output);
if (!column_permutation.empty())
output_header = std::make_shared<const Block>(ColumnPermuteTransform::permute(*join_algorithm_header, column_permutation));
else
output_header = join_algorithm_header;
}
static ITransformingStep::Traits getStorageJoinTraits()
{
return ITransformingStep::Traits
{
{
.returns_single_stream = false,
.preserves_number_of_streams = true,
.preserves_sorting = false,
},
{
.preserves_number_of_rows = false,
}
};
}
FilledJoinStep::FilledJoinStep(const SharedHeader & input_header_, JoinPtr join_, size_t max_block_size_)
: ITransformingStep(
input_header_,
std::make_shared<const Block>(JoiningTransform::transformHeader(*input_header_, join_)),
getStorageJoinTraits())
, join(std::move(join_))
, max_block_size(max_block_size_)
{
if (!join->isFilled())
throw Exception(ErrorCodes::LOGICAL_ERROR, "FilledJoinStep expects Join to be filled");
}
void FilledJoinStep::transformPipeline(QueryPipelineBuilder & pipeline, const BuildQueryPipelineSettings &)
{
bool default_totals = false;
if (!pipeline.hasTotals() && !join->getTotals().empty())
{
pipeline.addDefaultTotals();
default_totals = true;
}
auto finish_counter = std::make_shared<FinishCounter>(pipeline.getNumStreams());
pipeline.addSimpleTransform([&](const SharedHeader & header, QueryPipelineBuilder::StreamType stream_type)
{
bool on_totals = stream_type == QueryPipelineBuilder::StreamType::Totals;
auto counter = on_totals ? nullptr : finish_counter;
return std::make_shared<JoiningTransform>(header, output_header, join, max_block_size, on_totals, default_totals, counter);
});
}
void FilledJoinStep::updateOutputHeader()
{
output_header = std::make_shared<const Block>(JoiningTransform::transformHeader(*input_headers.front(), join));
}
void FilledJoinStep::describeActions(FormatSettings & settings) const
{
String prefix(settings.offset, ' ');
for (const auto & [name, value] : describeJoinActions(join))
settings.out << prefix << name << ": " << value << '\n';
}
void FilledJoinStep::describeActions(JSONBuilder::JSONMap & map) const
{
for (const auto & [name, value] : describeJoinActions(join))
map.add(name, value);
}
}