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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
#include <Processors/Transforms/JoiningTransform.h>
#include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/JoinUtils.h>
#include <Common/logger_useful.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
Block JoiningTransform::transformHeader(Block header, const JoinPtr & join)
{
LOG_DEBUG(&Poco::Logger::get("JoiningTransform"), "Before join block: '{}'", header.dumpStructure());
join->checkTypesOfKeys(header);
join->initialize(header);
ExtraBlockPtr tmp;
join->joinBlock(header, tmp);
LOG_DEBUG(&Poco::Logger::get("JoiningTransform"), "After join block: '{}'", header.dumpStructure());
return header;
}
JoiningTransform::JoiningTransform(
const Block & input_header,
const Block & output_header,
JoinPtr join_,
size_t max_block_size_,
bool on_totals_,
bool default_totals_,
FinishCounterPtr finish_counter_)
: IProcessor({input_header}, {output_header})
, join(std::move(join_))
, on_totals(on_totals_)
, default_totals(default_totals_)
, finish_counter(std::move(finish_counter_))
, max_block_size(max_block_size_)
{
if (!join->isFilled())
inputs.emplace_back(Block(), this); // Wait for FillingRightJoinSideTransform
}
JoiningTransform::~JoiningTransform() = default;
OutputPort & JoiningTransform::getFinishedSignal()
{
assert(outputs.size() == 2);
return outputs.back();
}
IProcessor::Status JoiningTransform::prepare()
{
auto & output = outputs.front();
auto & on_finish_output = outputs.back();
/// Check can output.
if (output.isFinished() || stop_reading)
{
output.finish();
on_finish_output.finish();
for (auto & input : inputs)
input.close();
return Status::Finished;
}
if (!output.canPush())
{
for (auto & input : inputs)
input.setNotNeeded();
return Status::PortFull;
}
/// Output if has data.
if (has_output)
{
output.push(std::move(output_chunk));
has_output = false;
return Status::PortFull;
}
if (inputs.size() > 1)
{
auto & last_in = inputs.back();
if (!last_in.isFinished())
{
last_in.setNeeded();
if (last_in.hasData())
throw Exception(ErrorCodes::LOGICAL_ERROR, "No data is expected from second JoiningTransform port");
return Status::NeedData;
}
}
if (has_input)
return Status::Ready;
auto & input = inputs.front();
if (input.isFinished())
{
if (process_non_joined)
return Status::Ready;
output.finish();
on_finish_output.finish();
return Status::Finished;
}
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
input_chunk = input.pull(true);
has_input = true;
return Status::Ready;
}
void JoiningTransform::work()
{
if (has_input)
{
transform(input_chunk);
output_chunk.swap(input_chunk);
has_input = not_processed != nullptr;
has_output = !output_chunk.empty();
}
else
{
if (!non_joined_blocks)
{
if (!finish_counter || !finish_counter->isLast())
{
process_non_joined = false;
return;
}
non_joined_blocks = join->getNonJoinedBlocks(
inputs.front().getHeader(), outputs.front().getHeader(), max_block_size);
if (!non_joined_blocks)
{
process_non_joined = false;
return;
}
}
Block block = non_joined_blocks->next();
if (!block)
{
process_non_joined = false;
return;
}
auto rows = block.rows();
output_chunk.setColumns(block.getColumns(), rows);
has_output = true;
}
}
void JoiningTransform::transform(Chunk & chunk)
{
if (!initialized)
{
initialized = true;
if (join->alwaysReturnsEmptySet() && !on_totals)
{
stop_reading = true;
chunk.clear();
return;
}
}
Block block;
if (on_totals)
{
const auto & left_totals = inputs.front().getHeader().cloneWithColumns(chunk.detachColumns());
const auto & right_totals = join->getTotals();
/// Drop totals if both out stream and joined stream doesn't have ones.
/// See comment in ExpressionTransform.h
if (default_totals && !right_totals)
return;
block = outputs.front().getHeader().cloneEmpty();
JoinCommon::joinTotals(left_totals, right_totals, join->getTableJoin(), block);
}
else
block = readExecute(chunk);
auto num_rows = block.rows();
chunk.setColumns(block.getColumns(), num_rows);
}
Block JoiningTransform::readExecute(Chunk & chunk)
{
Block res;
if (!not_processed)
{
if (chunk.hasColumns())
res = inputs.front().getHeader().cloneWithColumns(chunk.detachColumns());
if (res)
join->joinBlock(res, not_processed);
}
else if (not_processed->empty()) /// There's not processed data inside expression.
{
if (chunk.hasColumns())
res = inputs.front().getHeader().cloneWithColumns(chunk.detachColumns());
not_processed.reset();
join->joinBlock(res, not_processed);
}
else
{
res = std::move(not_processed->block);
join->joinBlock(res, not_processed);
}
return res;
}
FillingRightJoinSideTransform::FillingRightJoinSideTransform(Block input_header, JoinPtr join_)
: IProcessor({input_header}, {Block()})
, join(std::move(join_))
{}
InputPort * FillingRightJoinSideTransform::addTotalsPort()
{
if (inputs.size() > 1)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Totals port was already added to FillingRightJoinSideTransform");
return &inputs.emplace_back(inputs.front().getHeader(), this);
}
IProcessor::Status FillingRightJoinSideTransform::prepare()
{
auto & output = outputs.front();
/// Check can output.
if (output.isFinished())
{
for (auto & input : inputs)
input.close();
return Status::Finished;
}
if (!output.canPush())
{
for (auto & input : inputs)
input.setNotNeeded();
return Status::PortFull;
}
auto & input = inputs.front();
if (stop_reading)
{
input.close();
}
else if (!input.isFinished())
{
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
chunk = input.pull(true);
return Status::Ready;
}
if (inputs.size() > 1)
{
auto & totals_input = inputs.back();
if (!totals_input.isFinished())
{
totals_input.setNeeded();
if (!totals_input.hasData())
return Status::NeedData;
chunk = totals_input.pull(true);
for_totals = true;
return Status::Ready;
}
}
else if (!set_totals)
{
chunk.setColumns(inputs.front().getHeader().cloneEmpty().getColumns(), 0);
for_totals = true;
return Status::Ready;
}
output.finish();
return Status::Finished;
}
void FillingRightJoinSideTransform::work()
{
auto block = inputs.front().getHeader().cloneWithColumns(chunk.detachColumns());
if (for_totals)
join->setTotals(block);
else
stop_reading = !join->addBlockToJoin(block);
set_totals = for_totals;
}
DelayedJoinedBlocksWorkerTransform::DelayedJoinedBlocksWorkerTransform(
Block output_header_,
NonJoinedStreamBuilder non_joined_stream_builder_)
: IProcessor(InputPorts{Block()}, OutputPorts{output_header_})
, non_joined_stream_builder(std::move(non_joined_stream_builder_))
{
}
IProcessor::Status DelayedJoinedBlocksWorkerTransform::prepare()
{
auto & output = outputs.front();
auto & input = inputs.front();
if (output.isFinished())
{
input.close();
return Status::Finished;
}
if (!output.canPush())
{
input.setNotNeeded();
return Status::PortFull;
}
if (inputs.size() != 1 && outputs.size() != 1)
throw Exception(ErrorCodes::LOGICAL_ERROR, "DelayedJoinedBlocksWorkerTransform must have exactly one input port");
if (output_chunk)
{
input.setNotNeeded();
if (!output.canPush())
return Status::PortFull;
output.push(std::move(output_chunk));
output_chunk.clear();
return Status::PortFull;
}
if (!task)
{
if (!input.hasData())
{
input.setNeeded();
return Status::NeedData;
}
auto data = input.pullData(true);
if (data.exception)
{
output.pushException(data.exception);
return Status::Finished;
}
if (!data.chunk.hasChunkInfo())
throw Exception(ErrorCodes::LOGICAL_ERROR, "DelayedJoinedBlocksWorkerTransform must have chunk info");
task = std::dynamic_pointer_cast<const DelayedBlocksTask>(data.chunk.getChunkInfo());
}
else
{
input.setNotNeeded();
}
// When delayed_blocks is nullptr, it means that all buckets have been joined.
if (!task->delayed_blocks)
{
input.close();
output.finish();
return Status::Finished;
}
return Status::Ready;
}
void DelayedJoinedBlocksWorkerTransform::work()
{
if (!task)
return;
Block block;
/// All joined and non-joined rows from left stream are emitted, only right non-joined rows are left
if (!task->delayed_blocks->isFinished())
{
block = task->delayed_blocks->next();
if (!block)
block = nextNonJoinedBlock();
}
else
{
block = nextNonJoinedBlock();
}
if (!block)
{
resetTask();
return;
}
// Add block to the output
auto rows = block.rows();
output_chunk.setColumns(block.getColumns(), rows);
}
void DelayedJoinedBlocksWorkerTransform::resetTask()
{
task.reset();
non_joined_delayed_stream = nullptr;
}
Block DelayedJoinedBlocksWorkerTransform::nextNonJoinedBlock()
{
// Before read from non-joined stream, all blocks in left file reader must have been joined.
// For example, in HashJoin, it may return invalid mismatch rows from non-joined stream before
// the all blocks in left file reader have been finished, since the used flags are incomplete.
// To make only one processor could read from non-joined stream seems be a easy way.
if (!non_joined_delayed_stream && task && task->left_delayed_stream_finish_counter->isLast())
{
non_joined_delayed_stream = non_joined_stream_builder();
}
if (non_joined_delayed_stream)
{
return non_joined_delayed_stream->next();
}
return {};
}
DelayedJoinedBlocksTransform::DelayedJoinedBlocksTransform(size_t num_streams, JoinPtr join_)
: IProcessor(InputPorts{}, OutputPorts(num_streams, Block()))
, join(std::move(join_))
{
}
void DelayedJoinedBlocksTransform::work()
{
if (finished)
return;
delayed_blocks = join->getDelayedBlocks();
finished = finished || delayed_blocks == nullptr;
}
IProcessor::Status DelayedJoinedBlocksTransform::prepare()
{
for (auto & output : outputs)
{
if (output.isFinished())
{
/// If at least one output is finished, then we have read all data from buckets.
/// Some workers can still be busy with joining the last chunk of data in memory,
/// but after that they also will finish when they will try to get next chunk.
finished = true;
continue;
}
if (!output.canPush())
return Status::PortFull;
}
if (finished)
{
// Since have memory limit, cannot handle all buckets parallelly by different
// DelayedJoinedBlocksWorkerTransform. So send the same task to all outputs.
// Wait for all DelayedJoinedBlocksWorkerTransform be idle before getting next bucket.
for (auto & output : outputs)
{
if (output.isFinished())
continue;
Chunk chunk;
chunk.setChunkInfo(std::make_shared<DelayedBlocksTask>());
output.push(std::move(chunk));
output.finish();
}
return Status::Finished;
}
if (delayed_blocks)
{
// This counter is used to ensure that only the last DelayedJoinedBlocksWorkerTransform
// could read right non-joined blocks from the join.
auto left_delayed_stream_finished_counter = std::make_shared<JoiningTransform::FinishCounter>(outputs.size());
for (auto & output : outputs)
{
Chunk chunk;
auto task = std::make_shared<DelayedBlocksTask>(delayed_blocks, left_delayed_stream_finished_counter);
chunk.setChunkInfo(task);
output.push(std::move(chunk));
}
delayed_blocks = nullptr;
return Status::PortFull;
}
return Status::Ready;
}
}
|