ClickHouse/dbms/src/DataStreams/AggregatingSortedBlockInputStream.h

81 lines
2.7 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(BlockInputStreams inputs_, const SortDescription & description_, size_t max_block_size_)
: MergingSortedBlockInputStream(inputs_, description_, 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
String getID() const override
{
std::stringstream res;
res << "AggregatingSorted(inputs";
2014-05-26 16:11:20 +00:00
for (size_t i = 0; i < children.size(); ++i)
res << ", " << children[i]->getID();
2014-05-26 16:11:20 +00:00
res << ", description";
2014-05-26 16:11:20 +00:00
for (size_t i = 0; i < description.size(); ++i)
res << ", " << description[i].getID();
2014-05-26 16:11:20 +00:00
res << ")";
return res.str();
}
2014-05-26 16:11:20 +00:00
2017-04-10 14:50:55 +00:00
bool isGroupedOutput() const override { return true; }
bool isSortedOutput() const override { return true; }
const SortDescription & getSortDescription() const override { return description; }
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(MutableColumnRawPtrs & merged_columns, std::priority_queue<TSortCursor> & 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(TSortCursor & cursor);
2014-05-26 16:11:20 +00:00
};
}