ClickHouse/src/Processors/Transforms/ExtremesTransform.cpp

124 lines
2.9 KiB
C++
Raw Normal View History

2019-04-05 11:34:11 +00:00
#include <Processors/Transforms/ExtremesTransform.h>
2019-03-26 18:28:37 +00:00
2019-10-04 17:46:36 +00:00
#include <Core/Field.h>
2019-03-26 18:28:37 +00:00
namespace DB
{
ExtremesTransform::ExtremesTransform(const Block & header)
: ISimpleTransform(header, header, true)
{
/// Port for Extremes.
outputs.emplace_back(outputs.front().getHeader(), this);
}
IProcessor::Status ExtremesTransform::prepare()
{
if (!finished_transform)
{
auto status = ISimpleTransform::prepare();
if (status != Status::Finished)
return status;
finished_transform = true;
}
auto & totals_output = getExtremesPort();
/// Check can output.
if (totals_output.isFinished())
return Status::Finished;
if (!totals_output.canPush())
return Status::PortFull;
2019-04-08 13:38:02 +00:00
if (!extremes && !extremes_columns.empty())
2019-03-26 18:28:37 +00:00
return Status::Ready;
2019-04-08 13:38:02 +00:00
if (extremes)
totals_output.push(std::move(extremes));
2019-03-26 18:28:37 +00:00
totals_output.finish();
return Status::Finished;
}
void ExtremesTransform::work()
{
if (finished_transform)
{
2019-04-08 13:29:24 +00:00
if (!extremes && !extremes_columns.empty())
2019-03-26 18:28:37 +00:00
extremes.setColumns(std::move(extremes_columns), 2);
}
else
ISimpleTransform::work();
}
void ExtremesTransform::transform(DB::Chunk & chunk)
{
2019-07-08 18:08:44 +00:00
if (chunk.getNumRows() == 0)
return;
2019-03-26 18:28:37 +00:00
size_t num_columns = chunk.getNumColumns();
2020-04-22 07:03:43 +00:00
const auto & columns = chunk.getColumns();
2019-03-26 18:28:37 +00:00
if (extremes_columns.empty())
{
extremes_columns.resize(num_columns);
for (size_t i = 0; i < num_columns; ++i)
{
const ColumnPtr & src = columns[i];
2019-07-08 13:15:45 +00:00
if (isColumnConst(*src))
2019-03-26 18:28:37 +00:00
{
/// Equal min and max.
extremes_columns[i] = src->cloneResized(2);
}
else
{
Field min_value;
Field max_value;
src->getExtremes(min_value, max_value);
extremes_columns[i] = src->cloneEmpty();
extremes_columns[i]->insert(min_value);
extremes_columns[i]->insert(max_value);
}
}
}
else
{
for (size_t i = 0; i < num_columns; ++i)
{
2019-07-08 13:15:45 +00:00
if (isColumnConst(*extremes_columns[i]))
2019-03-26 18:28:37 +00:00
continue;
Field min_value = (*extremes_columns[i])[0];
Field max_value = (*extremes_columns[i])[1];
Field cur_min_value;
Field cur_max_value;
columns[i]->getExtremes(cur_min_value, cur_max_value);
if (cur_min_value < min_value)
min_value = cur_min_value;
if (cur_max_value > max_value)
max_value = cur_max_value;
MutableColumnPtr new_extremes = extremes_columns[i]->cloneEmpty();
new_extremes->insert(min_value);
new_extremes->insert(max_value);
extremes_columns[i] = std::move(new_extremes);
}
}
}
}