ClickHouse/src/Processors/IAccumulatingTransform.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
2.4 KiB
C++
Raw Normal View History

#include <Processors/IAccumulatingTransform.h>
namespace DB
{
2020-02-25 18:10:48 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
IAccumulatingTransform::IAccumulatingTransform(Block input_header, Block output_header)
: IProcessor({std::move(input_header)}, {std::move(output_header)}),
input(inputs.front()), output(outputs.front())
{
}
IAccumulatingTransform::Status IAccumulatingTransform::prepare()
{
2019-02-07 18:51:53 +00:00
/// Check can output.
if (output.isFinished())
{
2020-09-04 20:44:15 +00:00
for (auto & in : inputs)
in.close();
2019-02-07 18:51:53 +00:00
return Status::Finished;
}
2019-02-07 18:51:53 +00:00
if (!output.canPush())
{
2019-02-07 18:51:53 +00:00
input.setNotNeeded();
return Status::PortFull;
}
2019-02-07 18:51:53 +00:00
/// Output if has data.
if (current_output_chunk)
output.push(std::move(current_output_chunk));
2019-02-07 18:51:53 +00:00
if (finished_generate)
{
2019-02-07 18:51:53 +00:00
output.finish();
return Status::Finished;
}
if (input.isFinished())
finished_input = true;
2020-09-04 17:14:36 +00:00
if (finished_input)
{
/// Close input if flag was set manually.
2020-09-04 17:14:36 +00:00
input.close();
/// Read from totals port if has it.
2020-09-04 08:36:47 +00:00
if (inputs.size() > 1)
{
auto & totals_input = inputs.back();
if (!totals_input.isFinished())
{
totals_input.setNeeded();
if (!totals_input.hasData())
return Status::NeedData;
totals = totals_input.pull();
totals_input.close();
}
}
2019-03-04 14:56:09 +00:00
/// Generate output block.
2019-03-04 14:56:09 +00:00
return Status::Ready;
}
2019-02-07 18:51:53 +00:00
/// Check can input.
if (!has_input)
{
2019-02-07 18:51:53 +00:00
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
current_input_chunk = input.pull();
2019-02-07 18:51:53 +00:00
has_input = true;
}
2019-02-07 18:51:53 +00:00
return Status::Ready;
}
void IAccumulatingTransform::work()
{
2019-02-07 18:51:53 +00:00
if (!finished_input)
{
consume(std::move(current_input_chunk));
2019-02-07 18:51:53 +00:00
has_input = false;
}
else
{
current_output_chunk = generate();
if (!current_output_chunk)
2019-02-07 18:51:53 +00:00
finished_generate = true;
}
}
2019-02-26 18:40:08 +00:00
void IAccumulatingTransform::setReadyChunk(Chunk chunk)
{
if (current_output_chunk)
throw Exception(ErrorCodes::LOGICAL_ERROR,
"IAccumulatingTransform already has input. "
"Cannot set another chunk. Probably, setReadyChunk method was called twice per consume().");
2019-02-26 18:40:08 +00:00
current_output_chunk = std::move(chunk);
}
}