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>
|
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_)
|
2019-08-22 09:28:23 +00:00
|
|
|
: IAccumulatingTransform(std::move(header), params_->getHeader())
|
2019-03-19 13:36:19 +00:00
|
|
|
, params(std::move(params_))
|
|
|
|
, keys(params->params.keys)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 = (UInt64(1) << keys.size()) - 1;
|
|
|
|
|
|
|
|
current_columns = cube_chunk.getColumns();
|
|
|
|
current_zero_columns.clear();
|
|
|
|
current_zero_columns.reserve(keys.size());
|
|
|
|
|
|
|
|
for (auto key : keys)
|
|
|
|
current_zero_columns.emplace_back(current_columns[key]->cloneEmpty()->cloneResized(num_rows));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-08 13:02:19 +00:00
|
|
|
finalizeChunk(gen_chunk);
|
|
|
|
return gen_chunk;
|
2019-03-19 13:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|