ClickHouse/src/Processors/Transforms/CubeTransform.cpp

88 lines
2.7 KiB
C++
Raw Normal View History

2019-03-19 13:36:19 +00:00
#include <Processors/Transforms/CubeTransform.h>
2019-04-08 13:02:19 +00:00
#include <Processors/Transforms/TotalsHavingTransform.h>
#include <Processors/QueryPlan/AggregatingStep.h>
2019-03-19 13:36:19 +00:00
namespace DB
{
2020-02-25 18:10:48 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
2019-03-19 13:36:19 +00:00
CubeTransform::CubeTransform(Block header, AggregatingTransformParamsPtr params_)
: IAccumulatingTransform(std::move(header), appendGroupingSetColumn(params_->getHeader()))
2019-03-19 13:36:19 +00:00
, params(std::move(params_))
, keys(params->params.keys)
, aggregates_mask(getAggregatesMask(params->getHeader(), params->params.aggregates))
2019-03-19 13:36:19 +00:00
{
if (keys.size() >= 8 * sizeof(mask))
throw Exception("Too many keys are used for CubeTransform.", ErrorCodes::LOGICAL_ERROR);
}
2019-08-22 09:28:23 +00:00
Chunk CubeTransform::merge(Chunks && chunks, bool final)
2019-03-19 13:36:19 +00:00
{
2019-08-22 09:28:23 +00:00
BlocksList rollup_blocks;
for (auto & chunk : chunks)
rollup_blocks.emplace_back(getInputPort().getHeader().cloneWithColumns(chunk.detachColumns()));
2019-03-19 13:36:19 +00:00
2019-08-22 09:28:23 +00:00
auto rollup_block = params->aggregator.mergeBlocks(rollup_blocks, final);
auto num_rows = rollup_block.rows();
return Chunk(rollup_block.getColumns(), num_rows);
2019-03-19 13:36:19 +00:00
}
2019-08-22 09:28:23 +00:00
void CubeTransform::consume(Chunk chunk)
2019-03-19 13:36:19 +00:00
{
2019-08-22 09:28:23 +00:00
consumed_chunks.emplace_back(std::move(chunk));
2019-03-19 13:36:19 +00:00
}
MutableColumnPtr getColumnWithDefaults(Block const & header, size_t key, size_t n);
2019-03-19 13:36:19 +00:00
Chunk CubeTransform::generate()
{
2019-08-22 09:28:23 +00:00
if (!consumed_chunks.empty())
{
if (consumed_chunks.size() > 1)
cube_chunk = merge(std::move(consumed_chunks), false);
else
cube_chunk = std::move(consumed_chunks.front());
consumed_chunks.clear();
auto num_rows = cube_chunk.getNumRows();
mask = (static_cast<UInt64>(1) << keys.size()) - 1;
2019-08-22 09:28:23 +00:00
current_columns = cube_chunk.getColumns();
current_zero_columns.clear();
current_zero_columns.reserve(keys.size());
auto const & input_header = getInputPort().getHeader();
2019-08-22 09:28:23 +00:00
for (auto key : keys)
current_zero_columns.emplace_back(getColumnWithDefaults(input_header, key, num_rows));
2019-08-22 09:28:23 +00:00
}
auto gen_chunk = std::move(cube_chunk);
2019-03-19 13:36:19 +00:00
if (mask)
{
--mask;
auto columns = current_columns;
2019-04-18 17:28:55 +00:00
auto size = keys.size();
for (size_t i = 0; i < size; ++i)
/// Reverse bit order to support previous behaviour.
2019-04-18 17:32:01 +00:00
if ((mask & (UInt64(1) << (size - i - 1))) == 0)
2019-04-18 12:09:03 +00:00
columns[keys[i]] = current_zero_columns[i];
2019-03-19 13:36:19 +00:00
2019-08-22 09:28:23 +00:00
Chunks chunks;
chunks.emplace_back(std::move(columns), current_columns.front()->size());
cube_chunk = merge(std::move(chunks), false);
2019-03-19 13:36:19 +00:00
}
finalizeChunk(gen_chunk, aggregates_mask);
if (!gen_chunk.empty())
gen_chunk.addColumn(0, ColumnUInt64::create(gen_chunk.getNumRows(), grouping_set++));
2019-04-08 13:02:19 +00:00
return gen_chunk;
2019-03-19 13:36:19 +00:00
}
}