ClickHouse/src/Processors/ForkProcessor.cpp

79 lines
1.5 KiB
C++
Raw Normal View History

#include <Processors/ForkProcessor.h>
namespace DB
{
ForkProcessor::Status ForkProcessor::prepare()
{
auto & input = inputs.front();
2019-02-07 18:51:53 +00:00
/// Check can output.
bool all_can_push = true;
2019-06-18 08:25:27 +00:00
size_t num_active_outputs = 0;
for (const auto & output : outputs)
{
2019-02-07 18:51:53 +00:00
if (!output.isFinished())
{
2019-06-18 08:25:27 +00:00
++num_active_outputs;
2019-02-07 18:51:53 +00:00
/// The order is important.
if (!output.canPush())
all_can_push = false;
}
}
2020-09-20 19:59:26 +00:00
if (0 == num_active_outputs)
{
2019-02-07 18:51:53 +00:00
input.close();
return Status::Finished;
}
if (!all_can_push)
{
input.setNotNeeded();
2019-02-07 18:51:53 +00:00
return Status::PortFull;
}
2019-02-07 18:51:53 +00:00
/// Check can input.
2019-02-07 18:51:53 +00:00
if (input.isFinished())
{
2019-02-07 18:51:53 +00:00
for (auto & output : outputs)
output.finish();
return Status::Finished;
}
2019-02-07 18:51:53 +00:00
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
/// Move data.
auto data = input.pull();
2019-06-18 08:25:27 +00:00
size_t num_processed_outputs = 0;
for (auto & output : outputs)
2019-06-18 08:25:27 +00:00
{
2019-02-07 18:51:53 +00:00
if (!output.isFinished()) /// Skip finished outputs.
2019-06-18 08:25:27 +00:00
{
++num_processed_outputs;
if (num_processed_outputs == num_active_outputs)
2020-03-18 02:02:24 +00:00
output.push(std::move(data)); // NOLINT Can push because no full or unneeded outputs.
2019-06-18 08:25:27 +00:00
else
output.push(data.clone());
}
}
2019-02-07 18:51:53 +00:00
/// Now, we pulled from input. It must be empty.
return Status::NeedData;
}
}