1#pragma once
2
3#include <Core/SortDescription.h>
4#include <Interpreters/sortBlock.h>
5#include <DataStreams/IBlockInputStream.h>
6
7
8namespace DB
9{
10
11/** Takes stream already sorted by `x` and finishes sorting it by (`x`, `y`).
12 * During sorting only blocks with rows that equal by `x` saved in RAM.
13 * */
14class FinishSortingBlockInputStream : public IBlockInputStream
15{
16public:
17 /// limit - if not 0, allowed to return just first 'limit' rows in sorted order.
18 FinishSortingBlockInputStream(const BlockInputStreamPtr & input, const SortDescription & description_sorted_,
19 const SortDescription & description_to_sort_,
20 size_t max_merged_block_size_, UInt64 limit_);
21
22 String getName() const override { return "FinishSorting"; }
23
24 bool isSortedOutput() const override { return true; }
25 const SortDescription & getSortDescription() const override { return description_to_sort; }
26
27 Block getHeader() const override { return header; }
28
29protected:
30 Block readImpl() override;
31
32private:
33 SortDescription description_sorted;
34 SortDescription description_to_sort;
35 size_t max_merged_block_size;
36 UInt64 limit;
37
38 Block tail_block;
39 Blocks blocks;
40
41 std::unique_ptr<IBlockInputStream> impl;
42
43 /// Before operation, will remove constant columns from blocks. And after, place constant columns back.
44 /// to avoid excessive virtual function calls
45 /// Save original block structure here.
46 Block header;
47
48 bool end_of_stream = false;
49 size_t total_rows_processed = 0;
50};
51}
52