1#pragma once
2
3#include <DataStreams/IBlockInputStream.h>
4
5#include <Common/HashTable/HashMap.h>
6#include <Common/UInt128.h>
7
8
9namespace 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 */
18class LimitByBlockInputStream : public IBlockInputStream
19{
20public:
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
27protected:
28 Block readImpl() override;
29
30private:
31 ColumnRawPtrs getKeyColumns(Block & block) const;
32
33private:
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