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
|
|
|
|
{
|
|
|
|
|
|
|
|
CubeTransform::CubeTransform(Block header, AggregatingTransformParamsPtr params_)
|
|
|
|
: IInflatingTransform(std::move(header), params_->getHeader())
|
|
|
|
, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CubeTransform::consume(Chunk chunk)
|
|
|
|
{
|
|
|
|
consumed_chunk = std::move(chunk);
|
2019-04-18 17:08:28 +00:00
|
|
|
auto num_rows = consumed_chunk.getNumRows();
|
2019-03-19 13:36:19 +00:00
|
|
|
mask = (UInt64(1) << keys.size()) - 1;
|
|
|
|
|
|
|
|
current_columns = consumed_chunk.getColumns();
|
|
|
|
current_zero_columns.clear();
|
2019-04-18 12:09:03 +00:00
|
|
|
current_zero_columns.reserve(keys.size());
|
2019-03-19 13:36:19 +00:00
|
|
|
|
2019-04-18 12:09:03 +00:00
|
|
|
for (auto key : keys)
|
|
|
|
current_zero_columns.emplace_back(current_columns[key]->cloneEmpty()->cloneResized(num_rows));
|
2019-03-19 13:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CubeTransform::canGenerate()
|
|
|
|
{
|
|
|
|
return consumed_chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
Chunk CubeTransform::generate()
|
|
|
|
{
|
|
|
|
auto gen_chunk = std::move(consumed_chunk);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
BlocksList cube_blocks = { getInputPort().getHeader().cloneWithColumns(columns) };
|
|
|
|
auto cube_block = params->aggregator.mergeBlocks(cube_blocks, false);
|
|
|
|
|
|
|
|
auto num_rows = cube_block.rows();
|
2019-04-18 17:17:18 +00:00
|
|
|
consumed_chunk = Chunk(cube_block.getColumns(), num_rows);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|