ClickHouse/dbms/include/DB/DataStreams/AggregatingSortedBlockInputStream.h

79 lines
3.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 <DB/Core/Row.h>
#include <DB/Core/ColumnNumbers.h>
#include <DB/DataStreams/MergingSortedBlockInputStream.h>
#include <DB/AggregateFunctions/IAggregateFunction.h>
#include <DB/Columns/ColumnAggregateFunction.h>
namespace DB
{
/** Соединяет несколько сортированных потоков в один.
* При этом, для каждой группы идущих подряд одинаковых значений первичного ключа (столбцов, по которым сортируются данные),
* сливает их в одну строку. При слиянии, производится доагрегация данных - слияние состояний агрегатных функций,
* соответствующих одному значению первичного ключа. Для столбцов, не входящих в первичный ключ, и не имеющих тип AggregateFunction,
* при слиянии, выбирается первое попавшееся значение.
*/
class AggregatingSortedBlockInputStream : public MergingSortedBlockInputStream
{
public:
AggregatingSortedBlockInputStream(BlockInputStreams inputs_, const SortDescription & description_, size_t max_block_size_)
2015-01-18 08:25:56 +00:00
: 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
2014-05-26 16:11:20 +00:00
{
std::stringstream res;
res << "AggregatingSorted(inputs";
for (size_t i = 0; i < children.size(); ++i)
res << ", " << children[i]->getID();
res << ", description";
for (size_t i = 0; i < description.size(); ++i)
res << ", " << description[i].getID();
res << ")";
return res.str();
}
protected:
/// Может возвращаться на 1 больше записей, чем 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
/// Прочитали до конца.
bool finished = false;
2014-05-26 16:11:20 +00:00
/// Столбцы с какими номерами надо аггрегировать.
ColumnNumbers column_numbers_to_aggregate;
2016-04-14 05:03:33 +00:00
ColumnNumbers column_numbers_not_to_aggregate;
2014-06-06 19:35:41 +00:00
std::vector<ColumnAggregateFunction *> columns_to_aggregate;
2014-05-26 16:11:20 +00:00
RowRef current_key; /// Текущий первичный ключ.
2016-04-14 05:03:33 +00:00
RowRef next_key; /// Первичный ключ следующей строки.
2014-05-26 16:11:20 +00:00
/** Делаем поддержку двух разных курсоров - с Collation и без.
* Шаблоны используем вместо полиморфных SortCursor'ов и вызовов виртуальных функций.
*/
template <class TSortCursor>
void merge(ColumnPlainPtrs & merged_columns, std::priority_queue<TSortCursor> & queue);
2014-05-26 16:11:20 +00:00
2014-06-06 19:35:41 +00:00
/** Извлечь все состояния аггрегатных функций и объединить с текущей группой.
2014-05-26 16:11:20 +00:00
*/
template <class TSortCursor>
2016-04-14 05:03:33 +00:00
void addRow(TSortCursor & cursor);
2014-05-26 16:11:20 +00:00
};
}