forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFractionalLimitTransform.cpp
More file actions
509 lines (413 loc) · 17.9 KB
/
Copy pathFractionalLimitTransform.cpp
File metadata and controls
509 lines (413 loc) · 17.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
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 <Core/SortDescription.h>
#include <Processors/Chunk.h>
#include <Processors/FractionalLimitTransform.h>
#include <Columns/IColumn.h>
#include <Processors/Port.h>
#include <Storages/MergeTree/ReplicatedMergeTreeLogEntry.h>
#include <base/types.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
void FractionalLimitTransform::finalizeLimits()
{
/// Fractions depend on the final total number of rows, so we can only compute the integral
/// limit/offset once all input is read.
if (limits_are_final)
return;
limit_rows = static_cast<UInt64>(std::ceil(static_cast<double>(total_input_rows) * limit_fraction));
offset_rows += static_cast<UInt64>(std::ceil(static_cast<double>(total_input_rows) * offset_fraction));
if (with_ties && rows_processed < limit_rows + offset_rows)
ties_last_row = {};
limits_are_final = true;
}
FractionalLimitTransform::FractionalLimitTransform(
SharedHeader header_,
Float64 limit_fraction_,
Float64 offset_fraction_,
UInt64 offset_,
size_t num_streams,
bool with_ties_,
SortDescription limit_with_ties_sort_description_)
: IProcessor(InputPorts(num_streams, header_), OutputPorts(num_streams, header_))
, limit_fraction(limit_fraction_)
, offset_fraction(offset_fraction_)
, offset_rows(offset_)
, with_ties(with_ties_)
, limit_with_ties_sort_description(std::move(limit_with_ties_sort_description_))
{
if (limit_fraction <= 0.0 || limit_fraction >= 1.0)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Fractional LIMIT values must be in the range (0, 1)");
if (offset_fraction < 0.0 || offset_fraction >= 1.0)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Fractional OFFSET values must be in the range (0, 1)");
if (num_streams != 1 && with_ties)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot use FractionalLimitTransform with multiple ports and ties");
ports_data.resize(num_streams);
size_t stream_idx = 0;
for (auto & input : inputs)
{
ports_data[stream_idx].input_port = &input;
++stream_idx;
}
stream_idx = 0;
for (auto & output : outputs)
{
ports_data[stream_idx].output_port = &output;
++stream_idx;
}
for (const auto & desc : limit_with_ties_sort_description)
sort_key_positions.push_back(header_->getPositionByName(desc.column_name));
}
Chunk FractionalLimitTransform::makeChunkWithPreviousRow(const Chunk & chunk, UInt64 row) const
{
chassert(row < chunk.getNumRows());
ColumnRawPtrs current_columns = extractSortColumns(chunk.getColumns());
MutableColumns last_row_sort_columns;
for (size_t i = 0; i < current_columns.size(); ++i)
{
last_row_sort_columns.emplace_back(current_columns[i]->cloneEmpty());
last_row_sort_columns[i]->insertFrom(*current_columns[i], row);
}
return Chunk(std::move(last_row_sort_columns), 1);
}
FractionalLimitTransform::Status FractionalLimitTransform::prepare()
{
if (allOutputsFinished())
{
/// Nobody needs data: stop sources.
for (auto & port : ports_data)
port.input_port->close();
return Status::Finished;
}
/// Check can we still pull data from input?
if (num_finished_input_ports != ports_data.size())
{
auto process_port = [&](size_t port_idx)
{
auto status = pullData(ports_data[port_idx]);
switch (status)
{
case IProcessor::Status::Finished:
{
if (!ports_data[port_idx].is_input_finished)
{
ports_data[port_idx].is_input_finished = true;
++num_finished_input_ports;
}
return;
}
case IProcessor::Status::NeedData:
return;
default:
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Unexpected status for FractionalLimitTransform::pullData : {}",
IProcessor::statusToName(status));
}
};
for (size_t port_idx = 0; port_idx < ports_data.size(); ++port_idx)
process_port(port_idx);
/// Without fractional offset we can push chunks early before reading all inputs.
if (offset_fraction == 0.0)
{
bool pushed_any = false;
while (!cached_chunks.empty())
{
auto * output = getAvailableOutputPort();
if (!output)
{
if (num_finished_input_ports == ports_data.size())
finalizeLimits();
return Status::PortFull;
}
auto & front_chunk = cached_chunks.front();
UInt64 chunk_rows = front_chunk.getNumRows();
/// When integral OFFSET ends inside the first cached chunk, we must skip that prefix.
UInt64 offset_rows_remaining = 0;
if (rows_processed < offset_rows)
offset_rows_remaining = offset_rows - rows_processed;
/// If we push this whole chunk now, would it exceed the current fractional LIMIT
/// computed from the number of rows already read?
const UInt64 limit_rows_for_current_input = static_cast<UInt64>(
std::ceil(static_cast<double>(total_input_rows) * limit_fraction));
if (limit_rows_for_current_input + offset_rows_remaining < chunk_rows + early_pushed_rows)
break;
/// If we still have an integral offset that didn't cause the chunk
/// to be dropped entirely above then its offset is in part of the chunk => split it
/// Notice that it only happens once
if (offset_rows_remaining)
{
/// offset_rows_remaining is guaranteed to be < chunk_rows here: cached_chunks.front() is the
/// first chunk that crosses the integral OFFSET boundary. Otherwise the whole chunk would still
/// be within OFFSET and would have been dropped in pullData().
if (offset_rows_remaining >= chunk_rows)
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Remaining offset ({}) must be less than chunk rows ({})",
offset_rows_remaining,
chunk_rows);
const UInt64 num_columns = front_chunk.getNumColumns();
auto columns = front_chunk.detachColumns();
const UInt64 rows_after_offset = chunk_rows - offset_rows_remaining;
for (UInt64 i = 0; i < num_columns; ++i)
columns[i] = columns[i]->cut(offset_rows_remaining, rows_after_offset);
front_chunk.setColumns(std::move(columns), rows_after_offset);
chunk_rows = rows_after_offset;
rows_processed += offset_rows_remaining;
}
rows_processed += chunk_rows;
early_pushed_rows += chunk_rows;
/// Skip an empty chunk: there is no row to remember and the stored ties_last_row
/// is still the correct tie key for the boundary (avoids chunk_rows - 1 underflow).
if (with_ties && rows_processed == limit_rows_for_current_input + offset_rows && chunk_rows > 0)
ties_last_row = makeChunkWithPreviousRow(front_chunk, chunk_rows - 1);
output->push(std::move(front_chunk));
cached_chunks.pop_front();
pushed_any = true;
}
if (pushed_any)
{
if (num_finished_input_ports == ports_data.size())
finalizeLimits();
return Status::PortFull;
}
}
if (num_finished_input_ports != ports_data.size())
/// Some input ports still available => we can read more data
return Status::NeedData;
finalizeLimits();
}
else if (!limits_are_final)
{
finalizeLimits();
}
/// If we reached here all input ports are finished.
/// we start pushing cached chunks to output ports.
auto status = pushData();
if (status != Status::Finished)
return status;
for (auto & port : ports_data)
{
port.input_port->close();
port.output_port->finish();
}
return Status::Finished;
}
bool FractionalLimitTransform::allOutputsFinished() const
{
for (const auto & data : ports_data)
if (!data.output_port->isFinished())
return false;
return true;
}
OutputPort * FractionalLimitTransform::getAvailableOutputPort()
{
const size_t num_outputs = ports_data.size();
if (num_outputs == 0)
return nullptr;
for (size_t i = 0; i < num_outputs; ++i)
{
const size_t idx = (next_output_port + i) % num_outputs;
auto & output = *ports_data[idx].output_port;
if (output.isFinished())
continue;
if (!output.canPush())
continue;
next_output_port = (idx + 1) % num_outputs;
return &output;
}
return nullptr;
}
FractionalLimitTransform::Status FractionalLimitTransform::pullData(PortsData & data)
{
auto & input = *data.input_port;
/// Check can input?
if (input.isFinished())
return Status::Finished;
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
data.current_chunk = input.pull();
const UInt64 chunk_rows = data.current_chunk.getNumRows();
if (rows_before_limit_at_least && !data.input_port_has_counter)
rows_before_limit_at_least->add(chunk_rows);
/// Process block.
total_input_rows += chunk_rows;
/// Ignore chunk if it should be offsetted
if (total_input_rows <= offset_rows)
{
/// As if it was put in cache then evicted due to offset.
rows_processed += chunk_rows;
data.current_chunk.clear();
if (input.isFinished())
return Status::Finished;
/// Now, we pulled from input, and it must be empty.
input.setNeeded();
return Status::NeedData;
}
cached_chunks.push_back(std::move(data.current_chunk));
/// This optimizes if offset_fraction is set but the above block optimizes if integral offset is set and both can't be non-zero.
///
/// Detect blocks that will 100% get removed by the fractional offset and remove them as early as possible.
/// Example: if we have 10 blocks with same num of rows and offset 0.1 we can freely drop the first block even before reading all data.
const UInt64 fractional_offset_rows = static_cast<UInt64>(std::ceil(static_cast<double>(total_input_rows) * offset_fraction));
while (!cached_chunks.empty() && fractional_offset_rows >= cached_chunks.front().getNumRows() + rows_processed)
{
rows_processed += cached_chunks.front().getNumRows();
cached_chunks.pop_front();
}
if (input.isFinished())
return Status::Finished;
input.setNeeded();
return Status::NeedData;
}
FractionalLimitTransform::Status FractionalLimitTransform::pushData()
{
/// Drain the cache while we have an output that can accept data. Return PortFull only if
/// all outputs are currently blocked; output finishing is handled by prepare().
while (!cached_chunks.empty())
{
/// Check if we reached limit.
const bool is_limit_reached = rows_processed >= offset_rows + limit_rows && !ties_last_row;
if (is_limit_reached)
return Status::Finished;
auto * output = getAvailableOutputPort();
if (!output)
return Status::PortFull;
/// The early removal of blocks by offset and fractional_offset at pullData() should have detected
/// all chunks that will be dropped entirely, but we may still need to offset inside the first block
/// and drop a portion of it.
auto & chunk = cached_chunks.front();
const UInt64 chunk_rows = chunk.getNumRows();
rows_processed += chunk_rows;
if (chunk_rows <= std::numeric_limits<UInt64>::max() - offset_rows && rows_processed >= offset_rows + chunk_rows
&& rows_processed <= offset_rows + limit_rows)
{
/// Return the whole chunk.
/// Save the last row of current chunk to check if next block begins with the same row (for WITH TIES).
/// Skip an empty chunk: there is no row to remember and the stored ties_last_row
/// is still the correct tie key for the boundary (avoids chunk_rows - 1 underflow).
if (with_ties && rows_processed == offset_rows + limit_rows && chunk_rows > 0)
ties_last_row = makeChunkWithPreviousRow(chunk, chunk_rows - 1);
}
else
/// This function may be heavy to execute. But it happens no more than twice.
splitChunk(chunk);
output->push(std::move(chunk));
cached_chunks.pop_front();
}
return Status::Finished;
}
void FractionalLimitTransform::splitChunk(Chunk & current_chunk)
{
auto current_chunk_sort_columns = extractSortColumns(current_chunk.getColumns());
const UInt64 chunk_rows = current_chunk.getNumRows();
const UInt64 num_columns = current_chunk.getNumColumns();
if (ties_last_row && rows_processed >= offset_rows + limit_rows)
{
/// Scan until the first row, which is not equal to ties_last_row (for WITH TIES)
UInt64 current_row_num = 0;
for (; current_row_num < chunk_rows; ++current_row_num)
{
if (!sortColumnsEqualAt(current_chunk_sort_columns, current_row_num))
break;
}
auto columns = current_chunk.detachColumns();
if (current_row_num < chunk_rows)
{
ties_last_row = {};
for (UInt64 i = 0; i < num_columns; ++i)
columns[i] = columns[i]->cut(0, current_row_num);
}
current_chunk.setColumns(std::move(columns), current_row_num);
return;
}
/// return a piece of the block
UInt64 cut_start = 0;
/// ------------[....(...).]
/// <----------------------> rows_processed
/// <----------> chunk_rows
/// <---------------> offset_rows
/// <---> cut_start
chassert(offset_rows < rows_processed);
if (offset_rows + chunk_rows > rows_processed)
cut_start = offset_rows + chunk_rows - rows_processed;
/// ------------[....(...).]
/// <----------------------> rows_processed
/// <----------> chunk_rows
/// <---------------> offset_rows
/// <---> limit
/// <---> cut_length
/// <---> cut_start
/// Or:
/// -----------------(------[....)....]
/// <---------------------------------> rows_processed
/// <---------> chunk_rows
/// <---------------> offset_rows
/// <-----------> limit
/// <----> cut_length
/// 0 = cut_start
UInt64 cut_length = chunk_rows - cut_start;
if (offset_rows + limit_rows < rows_processed)
{
if (offset_rows + limit_rows < rows_processed - chunk_rows)
cut_length = 0;
else
cut_length = offset_rows + limit_rows - (rows_processed - chunk_rows) - cut_start;
}
/// Check if other rows in current block equals to last one in limit
/// when rows_processed >= offset_rows + limit_rows.
if (with_ties && offset_rows + limit_rows <= rows_processed && cut_length)
{
UInt64 current_row_num = cut_start + cut_length;
ties_last_row = makeChunkWithPreviousRow(current_chunk, current_row_num - 1);
for (; current_row_num < chunk_rows; ++current_row_num)
{
if (!sortColumnsEqualAt(current_chunk_sort_columns, current_row_num))
{
ties_last_row = {};
break;
}
}
cut_length = current_row_num - cut_start;
}
if (cut_length == chunk_rows)
return;
auto columns = current_chunk.detachColumns();
for (UInt64 i = 0; i < num_columns; ++i)
columns[i] = columns[i]->cut(cut_start, cut_length);
current_chunk.setColumns(std::move(columns), cut_length);
}
ColumnRawPtrs FractionalLimitTransform::extractSortColumns(const Columns & columns) const
{
ColumnRawPtrs res;
res.reserve(limit_with_ties_sort_description.size());
for (size_t pos : sort_key_positions)
res.push_back(columns[pos].get());
return res;
}
bool FractionalLimitTransform::sortColumnsEqualAt(const ColumnRawPtrs & current_chunk_sort_columns, UInt64 current_chunk_row_num) const
{
chassert(current_chunk_sort_columns.size() == ties_last_row.getNumColumns());
const size_t num_sort_columns = current_chunk_sort_columns.size();
const auto & ties_last_row_sort_columns = ties_last_row.getColumns();
for (size_t i = 0; i < num_sort_columns; ++i)
{
const auto & column = *current_chunk_sort_columns[i];
const auto & ties_last_row_column = *ties_last_row_sort_columns[i];
/// Compare ties using the same collation as ORDER BY, otherwise rows that are equal
/// according to the collation (for example '1' and '01' under numeric collation) would
/// be treated as distinct and wrongly dropped from the result.
int res = 0;
if (limit_with_ties_sort_description[i].collator && column.isCollationSupported())
res = column.compareAtWithCollation(current_chunk_row_num, 0, ties_last_row_column, 1, *limit_with_ties_sort_description[i].collator);
else
res = column.compareAt(current_chunk_row_num, 0, ties_last_row_column, 1);
if (res != 0)
return false;
}
return true;
}
}