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
|
|
|
|
2017-04-01 09:19:00 +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:
|
2018-04-07 01:46:50 +00:00
|
|
|
AggregatingSortedBlockInputStream(
|
2019-02-10 16:55:12 +00:00
|
|
|
const BlockInputStreams & inputs_, const SortDescription & description_, UInt64 max_block_size_);
|
2014-05-26 16:11:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return "AggregatingSorted"; }
|
2014-05-26 16:11:20 +00:00
|
|
|
|
2017-04-07 19:47:21 +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.
|
2017-04-01 07:20:54 +00:00
|
|
|
Block readImpl() override;
|
2014-05-26 16:11:20 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Logger * log = &Logger::get("AggregatingSortedBlockInputStream");
|
2015-01-18 08:25:56 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Read finished.
|
2017-04-01 07:20:54 +00:00
|
|
|
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.
|
2017-04-01 07:20:54 +00:00
|
|
|
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.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2017-12-15 00:06:56 +00:00
|
|
|
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.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2017-12-15 00:06:56 +00:00
|
|
|
void addRow(SortCursor & cursor);
|
2014-05-26 16:11:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|