1 | #pragma once |
---|---|
2 | |
3 | #include <DataStreams/IBlockInputStream.h> |
4 | |
5 | #include <Common/HashTable/HashMap.h> |
6 | #include <Common/UInt128.h> |
7 | |
8 | |
9 | namespace DB |
10 | { |
11 | |
12 | /** Implements LIMIT BY clause witch can be used to obtain a "top N by subgroup". |
13 | * |
14 | * For example, if you have table T like this (Num: 1 1 3 3 3 4 4 5 7 7 7 7), |
15 | * the query SELECT Num FROM T LIMIT 2 BY Num |
16 | * will give you the following result: (Num: 1 1 3 3 4 4 5 7 7). |
17 | */ |
18 | class LimitByBlockInputStream : public IBlockInputStream |
19 | { |
20 | public: |
21 | LimitByBlockInputStream(const BlockInputStreamPtr & input, size_t group_length_, size_t group_offset_, const Names & columns); |
22 | |
23 | String getName() const override { return "LimitBy"; } |
24 | |
25 | Block getHeader() const override { return children.at(0)->getHeader(); } |
26 | |
27 | protected: |
28 | Block readImpl() override; |
29 | |
30 | private: |
31 | ColumnRawPtrs getKeyColumns(Block & block) const; |
32 | |
33 | private: |
34 | using MapHashed = HashMap<UInt128, UInt64, UInt128TrivialHash>; |
35 | |
36 | const Names columns_names; |
37 | const size_t group_length; |
38 | const size_t group_offset; |
39 | MapHashed keys_counts; |
40 | }; |
41 | |
42 | } |
43 |