| 1 | #pragma once |
| 2 | |
| 3 | #include <DataStreams/IBlockInputStream.h> |
| 4 | #include <Interpreters/SetVariants.h> |
| 5 | #include <Core/SortDescription.h> |
| 6 | |
| 7 | |
| 8 | namespace DB |
| 9 | { |
| 10 | |
| 11 | /** This class is intended for implementation of SELECT DISTINCT clause and |
| 12 | * leaves only unique rows in the stream. |
| 13 | * |
| 14 | * Implementation for case, when input stream has rows for same DISTINCT key or at least its prefix, |
| 15 | * grouped together (going consecutively). |
| 16 | * |
| 17 | * To optimize the SELECT DISTINCT ... LIMIT clause we can |
| 18 | * set limit_hint to non zero value. So we stop emitting new rows after |
| 19 | * count of already emitted rows will reach the limit_hint. |
| 20 | */ |
| 21 | class DistinctSortedBlockInputStream : public IBlockInputStream |
| 22 | { |
| 23 | public: |
| 24 | /// Empty columns_ means all collumns. |
| 25 | DistinctSortedBlockInputStream(const BlockInputStreamPtr & input, const SizeLimits & set_size_limits_, UInt64 limit_hint_, const Names & columns); |
| 26 | |
| 27 | String getName() const override { return "DistinctSorted" ; } |
| 28 | |
| 29 | Block () const override { return children.at(0)->getHeader(); } |
| 30 | |
| 31 | protected: |
| 32 | Block readImpl() override; |
| 33 | |
| 34 | private: |
| 35 | ColumnRawPtrs getKeyColumns(const Block & block) const; |
| 36 | /// When clearing_columns changed, we can clean HashSet to memory optimization |
| 37 | /// clearing_columns is a left-prefix of SortDescription exists in key_columns |
| 38 | ColumnRawPtrs getClearingColumns(const Block & block, const ColumnRawPtrs & key_columns) const; |
| 39 | static bool rowsEqual(const ColumnRawPtrs & lhs, size_t n, const ColumnRawPtrs & rhs, size_t m); |
| 40 | |
| 41 | /// return true if has new data |
| 42 | template <typename Method> |
| 43 | bool buildFilter( |
| 44 | Method & method, |
| 45 | const ColumnRawPtrs & key_columns, |
| 46 | const ColumnRawPtrs & clearing_hint_columns, |
| 47 | IColumn::Filter & filter, |
| 48 | size_t rows, |
| 49 | ClearableSetVariants & variants) const; |
| 50 | |
| 51 | const SortDescription & description; |
| 52 | |
| 53 | struct PreviousBlock |
| 54 | { |
| 55 | Block block; |
| 56 | ColumnRawPtrs clearing_hint_columns; |
| 57 | }; |
| 58 | PreviousBlock prev_block; |
| 59 | |
| 60 | Names columns_names; |
| 61 | ClearableSetVariants data; |
| 62 | Sizes key_sizes; |
| 63 | UInt64 limit_hint; |
| 64 | |
| 65 | /// Restrictions on the maximum size of the output data. |
| 66 | SizeLimits set_size_limits; |
| 67 | }; |
| 68 | |
| 69 | } |
| 70 | |