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)
2022-02-08 21:21:32 +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
2022-02-08 21:21:32 +00:00
output.push(data.clone()); /// NOLINT
2019-06-18 08:25:27 +00:00
}
}
2019-02-07 18:51:53 +00:00
/// Now, we pulled from input. It must be empty.
return Status::NeedData;
}
}