ClickHouse/dbms/src/DataStreams/AggregatingSortedBlockInputStream.h

60 lines
2.1 KiB
C++
Raw Normal View History

2014-05-26 16:11:20 +00:00
#pragma once
2015-09-29 19:19:54 +00:00
#include <common/logger_useful.h>
2014-05-26 16:11:20 +00:00
#include <Core/Row.h>
#include <Core/ColumnNumbers.h>
#include <DataStreams/MergingSortedBlockInputStream.h>
#include <AggregateFunctions/IAggregateFunction.h>
#include <Columns/ColumnAggregateFunction.h>
2014-05-26 16:11:20 +00:00
namespace DB
{
2017-05-13 22:19:04 +00:00
/** Merges several sorted streams to one.
* During this for each group of consecutive identical values of the primary key (the columns by which the data is sorted),
* merges them into one row. When merging, the data is pre-aggregated - merge of states of aggregate functions,
* corresponding to a one value of the primary key. For columns that are not part of the primary key and which do not have the AggregateFunction type,
2017-08-12 21:02:45 +00:00
* when merged, the first value is selected.
2014-05-26 16:11:20 +00:00
*/
class AggregatingSortedBlockInputStream : public MergingSortedBlockInputStream
{
public:
AggregatingSortedBlockInputStream(
const BlockInputStreams & inputs_, const SortDescription & description_, size_t max_block_size_);
2014-05-26 16:11:20 +00:00
String getName() const override { return "AggregatingSorted"; }
2014-05-26 16:11:20 +00:00
bool isSortedOutput() const override { return true; }
2014-05-26 16:11:20 +00:00
protected:
2017-05-13 22:19:04 +00:00
/// Can return 1 more records than max_block_size.
Block readImpl() override;
2014-05-26 16:11:20 +00:00
private:
Logger * log = &Logger::get("AggregatingSortedBlockInputStream");
2015-01-18 08:25:56 +00:00
2017-05-13 22:19:04 +00:00
/// Read finished.
bool finished = false;
2014-05-26 16:11:20 +00:00
2017-05-13 22:19:04 +00:00
/// Columns with which numbers should be aggregated.
ColumnNumbers column_numbers_to_aggregate;
ColumnNumbers column_numbers_not_to_aggregate;
std::vector<ColumnAggregateFunction *> columns_to_aggregate;
2014-05-26 16:11:20 +00:00
2017-05-13 22:19:04 +00:00
RowRef current_key; /// The current primary key.
RowRef next_key; /// The primary key of the next row.
2014-05-26 16:11:20 +00:00
2017-05-13 22:19:04 +00:00
/** We support two different cursors - with Collation and without.
* Templates are used instead of polymorphic SortCursor and calls to virtual functions.
*/
void merge(MutableColumns & merged_columns, std::priority_queue<SortCursor> & queue);
2014-05-26 16:11:20 +00:00
2017-05-13 22:19:04 +00:00
/** Extract all states of aggregate functions and merge them with the current group.
*/
void addRow(SortCursor & cursor);
2014-05-26 16:11:20 +00:00
};
}