forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLimitTransform.cpp
More file actions
403 lines (325 loc) · 12.3 KB
/
Copy pathLimitTransform.cpp
File metadata and controls
403 lines (325 loc) · 12.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
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
#include <Processors/LimitTransform.h>
#include <Columns/IColumn.h>
#include <Processors/Port.h>
#include <Processors/QueryPlan/Optimizations/RuntimeDataflowStatistics.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
LimitTransform::LimitTransform(
SharedHeader header_,
UInt64 limit_,
UInt64 offset_,
size_t num_streams,
bool always_read_till_end_,
bool with_ties_,
SortDescription description_,
RuntimeDataflowStatisticsCacheUpdaterPtr updater_)
: IProcessor(InputPorts(num_streams, header_), OutputPorts(num_streams, header_))
, limit(limit_)
, offset(offset_)
, always_read_till_end(always_read_till_end_)
, with_ties(with_ties_)
, description(std::move(description_))
, updater(std::move(updater_))
{
if (num_streams != 1 && with_ties)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot use LimitTransform with multiple ports and ties");
ports_data.resize(num_streams);
size_t cur_stream = 0;
for (auto & input : inputs)
{
ports_data[cur_stream].input_port = &input;
input_port_to_data[&input] = &ports_data[cur_stream];
++cur_stream;
}
cur_stream = 0;
for (auto & output : outputs)
{
ports_data[cur_stream].output_port = &output;
output_port_to_data[&output] = &ports_data[cur_stream];
++cur_stream;
}
for (const auto & desc : description)
sort_column_positions.push_back(header_->getPositionByName(desc.column_name));
}
Chunk LimitTransform::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);
}
IProcessor::Status LimitTransform::prepare(
const UpdatedInputPorts & updated_input_ports,
const UpdatedOutputPorts & updated_output_ports)
{
bool has_full_port = false;
auto process_pair = [&](PortsData & data)
{
auto status = preparePair(data);
switch (status)
{
case IProcessor::Status::Finished:
{
if (!data.is_finished)
{
data.is_finished = true;
++num_finished_port_pairs;
}
return;
}
case IProcessor::Status::PortFull:
{
has_full_port = true;
return;
}
case IProcessor::Status::NeedData:
return;
default:
throw Exception(
ErrorCodes::LOGICAL_ERROR, "Unexpected status for LimitTransform::preparePair : {}", IProcessor::statusToName(status));
}
};
for (const auto * port : updated_input_ports)
process_pair(*input_port_to_data.at(port));
for (const auto * port : updated_output_ports)
process_pair(*output_port_to_data.at(port));
/// All ports are finished. It may happen even before we reached the limit (has less data then limit).
if (num_finished_port_pairs == ports_data.size())
return Status::Finished;
bool limit_is_unreachable = (limit > std::numeric_limits<UInt64>::max() - offset);
/// If we reached limit for some port, then close others. Otherwise some sources may infinitely read data.
/// Example: SELECT * FROM system.numbers_mt WHERE number = 1000000 LIMIT 1
if ((!limit_is_unreachable && rows_read >= offset + limit)
&& !previous_row_chunk && !always_read_till_end)
{
for (auto & input : inputs)
input.close();
for (auto & output : outputs)
output.finish();
return Status::Finished;
}
if (has_full_port)
return Status::PortFull;
return Status::NeedData;
}
LimitTransform::Status LimitTransform::prepare()
{
if (ports_data.size() != 1)
throw Exception(ErrorCodes::LOGICAL_ERROR, "prepare without arguments is not supported for multi-port LimitTransform");
return prepare({ports_data.front().input_port}, {ports_data.front().output_port});
}
LimitTransform::Status LimitTransform::preparePair(PortsData & data)
{
auto & output = *data.output_port;
auto & input = *data.input_port;
/// Check can output.
bool output_finished = false;
if (output.isFinished())
{
output_finished = true;
if (!always_read_till_end || rows_read == 0)
{
/// The rows_read == 0 is a corner case. If no rows were read before the output is closed,
/// do not read data even with always_read_till_end to avoid Not-ready Set (sets might not be built).
input.close();
return Status::Finished;
}
}
if (!output_finished && !output.canPush())
{
input.setNotNeeded();
return Status::PortFull;
}
bool limit_is_unreachable = (limit > std::numeric_limits<UInt64>::max() - offset);
/// Check if we are done with pushing.
bool is_limit_reached = !limit_is_unreachable && rows_read >= offset + limit && !previous_row_chunk;
if (is_limit_reached)
{
if (!always_read_till_end)
{
output.finish();
input.close();
return Status::Finished;
}
}
/// Check can input.
if (input.isFinished())
{
output.finish();
return Status::Finished;
}
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
data.current_chunk = input.pull(true);
auto rows = data.current_chunk.getNumRows();
if (rows_before_limit_at_least && !data.input_port_has_counter)
rows_before_limit_at_least->add(rows);
/// Skip block (for 'always_read_till_end' case).
if (is_limit_reached || output_finished)
{
data.current_chunk.clear();
if (input.isFinished())
{
output.finish();
return Status::Finished;
}
/// Now, we pulled from input, and it must be empty.
input.setNeeded();
return Status::NeedData;
}
/// Process block.
rows_read += rows;
if (rows_read <= offset)
{
data.current_chunk.clear();
if (input.isFinished())
{
output.finish();
return Status::Finished;
}
/// Now, we pulled from input, and it must be empty.
input.setNeeded();
return Status::NeedData;
}
if (rows <= std::numeric_limits<UInt64>::max() - offset && rows_read >= offset + rows
&& !limit_is_unreachable && rows_read <= offset + limit)
{
/// Return the whole chunk.
/// Save the last row of current chunk to check if next block begins with the same row (for WITH TIES).
if (with_ties && rows_read == offset + limit)
previous_row_chunk = makeChunkWithPreviousRow(data.current_chunk, data.current_chunk.getNumRows() - 1);
}
else
/// This function may be heavy to execute in prepare. But it happens no more than twice, and make code simpler.
splitChunk(data);
bool may_need_more_data_for_ties = previous_row_chunk || rows_read - rows <= offset + limit;
/// No more data is needed.
if (!always_read_till_end && !limit_is_unreachable && rows_read >= offset + limit && !may_need_more_data_for_ties)
input.close();
if (updater)
updater->recordOutputChunk(data.current_chunk, output.getHeader());
output.push(std::move(data.current_chunk));
return Status::PortFull;
}
void LimitTransform::splitChunk(PortsData & data)
{
auto current_chunk_sort_columns = extractSortColumns(data.current_chunk.getColumns());
UInt64 num_rows = data.current_chunk.getNumRows();
UInt64 num_columns = data.current_chunk.getNumColumns();
bool limit_is_unreachable = (limit > std::numeric_limits<UInt64>::max() - offset);
if (previous_row_chunk && !limit_is_unreachable && rows_read >= offset + limit)
{
/// Scan until the first row, which is not equal to previous_row_chunk (for WITH TIES)
UInt64 current_row_num = 0;
for (; current_row_num < num_rows; ++current_row_num)
{
if (!sortColumnsEqualAt(current_chunk_sort_columns, current_row_num))
break;
}
auto columns = data.current_chunk.detachColumns();
if (current_row_num < num_rows)
{
previous_row_chunk = {};
for (UInt64 i = 0; i < num_columns; ++i)
columns[i] = columns[i]->cut(0, current_row_num);
}
data.current_chunk.setColumns(std::move(columns), current_row_num);
return;
}
/// return a piece of the block
UInt64 start = 0;
/// ------------[....(...).]
/// <----------------------> rows_read
/// <----------> num_rows
/// <---------------> offset
/// <---> start
chassert(offset < rows_read);
if (offset + num_rows > rows_read)
start = offset + num_rows - rows_read;
/// ------------[....(...).]
/// <----------------------> rows_read
/// <----------> num_rows
/// <---------------> offset
/// <---> limit
/// <---> length
/// <---> start
/// Or:
/// -----------------(------[....)....]
/// <---------------------------------> rows_read
/// <---------> num_rows
/// <---------------> offset
/// <-----------> limit
/// <----> length
/// 0 = start
UInt64 length = num_rows - start;
if (!limit_is_unreachable && offset + limit < rows_read)
{
if (offset + limit < rows_read - num_rows)
length = 0;
else
length = offset + limit - (rows_read - num_rows) - start;
}
/// Check if other rows in current block equals to last one in limit
/// when rows read >= offset + limit.
if (with_ties && offset + limit <= rows_read && length)
{
UInt64 current_row_num = start + length;
previous_row_chunk = makeChunkWithPreviousRow(data.current_chunk, current_row_num - 1);
for (; current_row_num < num_rows; ++current_row_num)
{
if (!sortColumnsEqualAt(current_chunk_sort_columns, current_row_num))
{
previous_row_chunk = {};
break;
}
}
length = current_row_num - start;
}
if (length == num_rows)
return;
auto columns = data.current_chunk.detachColumns();
for (UInt64 i = 0; i < num_columns; ++i)
columns[i] = columns[i]->cut(start, length);
data.current_chunk.setColumns(std::move(columns), length);
}
ColumnRawPtrs LimitTransform::extractSortColumns(const Columns & columns) const
{
ColumnRawPtrs res;
res.reserve(description.size());
for (size_t pos : sort_column_positions)
res.push_back(columns[pos].get());
return res;
}
bool LimitTransform::sortColumnsEqualAt(const ColumnRawPtrs & current_chunk_sort_columns, UInt64 current_chunk_row_num) const
{
chassert(current_chunk_sort_columns.size() == previous_row_chunk.getNumColumns());
size_t size = current_chunk_sort_columns.size();
const auto & previous_row_sort_columns = previous_row_chunk.getColumns();
for (size_t i = 0; i < size; ++i)
{
const auto & column = *current_chunk_sort_columns[i];
const auto & previous_column = *previous_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 (description[i].collator && column.isCollationSupported())
res = column.compareAtWithCollation(current_chunk_row_num, 0, previous_column, 1, *description[i].collator);
else
res = column.compareAt(current_chunk_row_num, 0, previous_column, 1);
if (res != 0)
return false;
}
return true;
}
}