2017-04-10 16:15:06 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <DataStreams/IBlockInputStream.h>
|
2017-04-10 16:15:06 +00:00
|
|
|
#include <Interpreters/SetVariants.h>
|
|
|
|
|
2018-03-11 00:15:26 +00:00
|
|
|
|
2017-04-10 16:15:06 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/** This class is intended for implementation of SELECT DISTINCT clause and
|
|
|
|
* leaves only unique rows in the stream.
|
|
|
|
*
|
2017-04-14 19:59:37 +00:00
|
|
|
* Implementation for case, when input stream has rows for same DISTINCT key or at least its prefix,
|
|
|
|
* grouped together (going consecutively).
|
|
|
|
*
|
2017-04-10 16:15:06 +00:00
|
|
|
* To optimize the SELECT DISTINCT ... LIMIT clause we can
|
|
|
|
* set limit_hint to non zero value. So we stop emitting new rows after
|
|
|
|
* count of already emitted rows will reach the limit_hint.
|
|
|
|
*/
|
2019-01-23 14:48:50 +00:00
|
|
|
class DistinctSortedBlockInputStream : public IBlockInputStream
|
2017-04-10 16:15:06 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Empty columns_ means all collumns.
|
2018-03-11 00:15:26 +00:00
|
|
|
DistinctSortedBlockInputStream(const BlockInputStreamPtr & input, const SizeLimits & set_size_limits, size_t limit_hint_, const Names & columns);
|
2017-04-10 16:15:06 +00:00
|
|
|
|
|
|
|
String getName() const override { return "DistinctSorted"; }
|
|
|
|
|
2018-02-18 03:23:48 +00:00
|
|
|
Block getHeader() const override { return children.at(0)->getHeader(); }
|
2018-01-06 18:10:44 +00:00
|
|
|
|
2017-04-10 16:15:06 +00:00
|
|
|
protected:
|
|
|
|
Block readImpl() override;
|
|
|
|
|
|
|
|
private:
|
2017-12-13 01:27:53 +00:00
|
|
|
ColumnRawPtrs getKeyColumns(const Block & block) const;
|
2017-04-10 16:15:06 +00:00
|
|
|
/// When clearing_columns changed, we can clean HashSet to memory optimization
|
|
|
|
/// clearing_columns is a left-prefix of SortDescription exists in key_columns
|
2017-12-13 01:27:53 +00:00
|
|
|
ColumnRawPtrs getClearingColumns(const Block & block, const ColumnRawPtrs & key_columns) const;
|
|
|
|
static bool rowsEqual(const ColumnRawPtrs & lhs, size_t n, const ColumnRawPtrs & rhs, size_t m);
|
2017-04-10 16:15:06 +00:00
|
|
|
|
|
|
|
/// return true if has new data
|
|
|
|
template <typename Method>
|
|
|
|
bool buildFilter(
|
|
|
|
Method & method,
|
2017-12-13 01:27:53 +00:00
|
|
|
const ColumnRawPtrs & key_columns,
|
|
|
|
const ColumnRawPtrs & clearing_hint_columns,
|
2017-04-10 16:15:06 +00:00
|
|
|
IColumn::Filter & filter,
|
|
|
|
size_t rows,
|
|
|
|
ClearableSetVariants & variants) const;
|
|
|
|
|
2017-04-14 19:59:37 +00:00
|
|
|
const SortDescription & description;
|
2017-09-08 02:29:47 +00:00
|
|
|
|
2017-04-10 16:15:06 +00:00
|
|
|
struct PreviousBlock
|
|
|
|
{
|
|
|
|
Block block;
|
2017-12-13 01:27:53 +00:00
|
|
|
ColumnRawPtrs clearing_hint_columns;
|
2017-04-14 19:59:37 +00:00
|
|
|
};
|
|
|
|
PreviousBlock prev_block;
|
2017-04-10 16:15:06 +00:00
|
|
|
|
|
|
|
Names columns_names;
|
|
|
|
ClearableSetVariants data;
|
|
|
|
Sizes key_sizes;
|
|
|
|
size_t limit_hint;
|
|
|
|
|
|
|
|
/// Restrictions on the maximum size of the output data.
|
2018-03-11 00:15:26 +00:00
|
|
|
SizeLimits set_size_limits;
|
2017-04-10 16:15:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|