ClickHouse/dbms/DataStreams/RollupBlockInputStream.cpp

73 lines
1.9 KiB
C++
Raw Normal View History

2018-08-21 16:08:45 +00:00
#include <DataStreams/RollupBlockInputStream.h>
2018-09-07 14:18:15 +00:00
#include <DataStreams/finalizeBlock.h>
2018-08-21 16:08:45 +00:00
#include <DataTypes/DataTypeAggregateFunction.h>
#include <Columns/ColumnAggregateFunction.h>
#include <Columns/FilterDescription.h>
#include <Common/typeid_cast.h>
namespace DB
{
2018-09-08 03:07:12 +00:00
2018-08-24 15:00:00 +00:00
RollupBlockInputStream::RollupBlockInputStream(
2018-09-06 01:06:30 +00:00
const BlockInputStreamPtr & input_, const Aggregator::Params & params_) : aggregator(params_),
2018-09-05 14:39:51 +00:00
keys(params_.keys)
2018-08-24 15:00:00 +00:00
{
children.push_back(input_);
2018-09-05 16:23:28 +00:00
Aggregator::CancellationHook hook = [this]() { return this->isCancelled(); };
2018-09-05 14:39:51 +00:00
aggregator.setCancellationHook(hook);
2018-08-24 15:00:00 +00:00
}
2018-08-21 16:08:45 +00:00
Block RollupBlockInputStream::getHeader() const
{
Block res = children.at(0)->getHeader();
2018-09-07 14:18:15 +00:00
finalizeBlock(res);
2018-08-21 16:08:45 +00:00
return res;
}
Block RollupBlockInputStream::readImpl()
{
2018-09-06 00:58:24 +00:00
/** After reading a block from input stream,
* we will subsequently roll it up on next iterations of 'readImpl'
* by zeroing out every column one-by-one and re-merging a block.
*/
2018-09-06 01:06:30 +00:00
if (!is_data_read)
2018-08-21 16:08:45 +00:00
{
BlocksList source_blocks;
while (auto block = children[0]->read())
source_blocks.push_back(block);
2018-08-21 16:08:45 +00:00
if (source_blocks.empty())
return {};
2018-08-21 16:08:45 +00:00
is_data_read = true;
if (source_blocks.size() > 1)
rollup_block = aggregator.mergeBlocks(source_blocks, false);
else
rollup_block = std::move(source_blocks.front());
current_key = keys.size() - 1;
auto finalized = rollup_block;
2018-09-07 14:18:15 +00:00
finalizeBlock(finalized);
2018-09-05 14:39:51 +00:00
return finalized;
}
2018-08-21 16:08:45 +00:00
if (current_key < 0)
return {};
auto & current = rollup_block.getByPosition(keys[current_key]);
current.column = current.column->cloneEmpty()->cloneResized(rollup_block.rows());
--current_key;
2018-08-21 16:08:45 +00:00
BlocksList rollup_blocks = { rollup_block };
rollup_block = aggregator.mergeBlocks(rollup_blocks, false);
2018-08-21 16:08:45 +00:00
auto finalized = rollup_block;
finalizeBlock(finalized);
return finalized;
2018-08-21 16:08:45 +00:00
}
}