2018-05-23 20:19:33 +00:00
|
|
|
#include <Processors/ResizeProcessor.h>
|
2020-01-18 11:39:48 +00:00
|
|
|
#include <iostream>
|
2018-05-23 20:19:33 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2018-05-23 20:19:33 +00:00
|
|
|
|
|
|
|
ResizeProcessor::Status ResizeProcessor::prepare()
|
|
|
|
{
|
2019-04-26 11:34:51 +00:00
|
|
|
bool is_first_output = true;
|
|
|
|
auto output_end = current_output;
|
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
bool all_outs_full_or_unneeded = true;
|
|
|
|
bool all_outs_finished = true;
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-04-26 11:34:51 +00:00
|
|
|
bool is_first_input = true;
|
|
|
|
auto input_end = current_input;
|
|
|
|
|
|
|
|
bool all_inputs_finished = true;
|
|
|
|
|
|
|
|
auto is_end_input = [&]() { return !is_first_input && current_input == input_end; };
|
|
|
|
auto is_end_output = [&]() { return !is_first_output && current_output == output_end; };
|
|
|
|
|
|
|
|
auto inc_current_input = [&]()
|
|
|
|
{
|
|
|
|
is_first_input = false;
|
|
|
|
++current_input;
|
|
|
|
|
|
|
|
if (current_input == inputs.end())
|
|
|
|
current_input = inputs.begin();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto inc_current_output = [&]()
|
|
|
|
{
|
|
|
|
is_first_output = false;
|
|
|
|
++current_output;
|
|
|
|
|
|
|
|
if (current_output == outputs.end())
|
|
|
|
current_output = outputs.begin();
|
|
|
|
};
|
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
/// Find next output where can push.
|
|
|
|
auto get_next_out = [&, this]() -> OutputPorts::iterator
|
|
|
|
{
|
2019-04-26 11:34:51 +00:00
|
|
|
while (!is_end_output())
|
2018-05-24 02:39:22 +00:00
|
|
|
{
|
2019-04-26 11:34:51 +00:00
|
|
|
if (!current_output->isFinished())
|
2019-02-07 18:51:53 +00:00
|
|
|
{
|
|
|
|
all_outs_finished = false;
|
|
|
|
|
2019-04-26 11:34:51 +00:00
|
|
|
if (current_output->canPush())
|
2019-02-07 18:51:53 +00:00
|
|
|
{
|
|
|
|
all_outs_full_or_unneeded = false;
|
2019-04-26 11:34:51 +00:00
|
|
|
auto res_output = current_output;
|
|
|
|
inc_current_output();
|
|
|
|
return res_output;
|
2019-02-07 18:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-04-26 11:34:51 +00:00
|
|
|
inc_current_output();
|
2018-05-24 02:39:22 +00:00
|
|
|
}
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-04-26 11:40:35 +00:00
|
|
|
return outputs.end();
|
2019-02-07 18:51:53 +00:00
|
|
|
};
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
/// Find next input from where can pull.
|
|
|
|
auto get_next_input = [&, this]() -> InputPorts::iterator
|
|
|
|
{
|
2019-04-26 11:34:51 +00:00
|
|
|
while (!is_end_input())
|
2018-05-24 02:39:22 +00:00
|
|
|
{
|
2019-04-26 11:34:51 +00:00
|
|
|
if (!current_input->isFinished())
|
2019-02-07 18:51:53 +00:00
|
|
|
{
|
|
|
|
all_inputs_finished = false;
|
2018-05-24 02:39:22 +00:00
|
|
|
|
2019-04-26 11:34:51 +00:00
|
|
|
current_input->setNeeded();
|
|
|
|
if (current_input->hasData())
|
2019-02-07 18:51:53 +00:00
|
|
|
{
|
2019-04-26 11:34:51 +00:00
|
|
|
auto res_input = current_input;
|
|
|
|
inc_current_input();
|
|
|
|
return res_input;
|
2019-02-07 18:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 11:34:51 +00:00
|
|
|
inc_current_input();
|
2018-05-24 02:39:22 +00:00
|
|
|
}
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-04-26 11:40:35 +00:00
|
|
|
return inputs.end();
|
2019-02-07 18:51:53 +00:00
|
|
|
};
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
auto get_status_if_no_outputs = [&]() -> Status
|
|
|
|
{
|
|
|
|
if (all_outs_finished)
|
2018-05-23 20:19:33 +00:00
|
|
|
{
|
2019-02-07 18:51:53 +00:00
|
|
|
for (auto & in : inputs)
|
|
|
|
in.close();
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
return Status::Finished;
|
2018-05-23 20:19:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
if (all_outs_full_or_unneeded)
|
2018-05-24 02:39:22 +00:00
|
|
|
{
|
2019-02-07 18:51:53 +00:00
|
|
|
for (auto & in : inputs)
|
|
|
|
in.setNotNeeded();
|
|
|
|
|
|
|
|
return Status::PortFull;
|
2018-05-24 02:39:22 +00:00
|
|
|
}
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
/// Now, we pushed to output, and it must be full.
|
|
|
|
return Status::PortFull;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto get_status_if_no_inputs = [&]() -> Status
|
|
|
|
{
|
|
|
|
if (all_inputs_finished)
|
2018-05-23 20:19:33 +00:00
|
|
|
{
|
2019-02-07 18:51:53 +00:00
|
|
|
for (auto & out : outputs)
|
|
|
|
out.finish();
|
|
|
|
|
|
|
|
return Status::Finished;
|
2018-05-23 20:19:33 +00:00
|
|
|
}
|
2019-02-07 18:51:53 +00:00
|
|
|
|
|
|
|
return Status::NeedData;
|
|
|
|
};
|
|
|
|
|
2019-04-26 11:15:33 +00:00
|
|
|
/// Set all inputs needed in order to evenly process them.
|
|
|
|
/// Otherwise, in case num_outputs < num_inputs and chunks are consumed faster than produced,
|
|
|
|
/// some inputs can be skipped.
|
2019-04-26 11:34:51 +00:00
|
|
|
// auto set_all_unprocessed_inputs_needed = [&]()
|
|
|
|
// {
|
|
|
|
// for (; cur_input != inputs.end(); ++cur_input)
|
|
|
|
// if (!cur_input->isFinished())
|
|
|
|
// cur_input->setNeeded();
|
|
|
|
// };
|
|
|
|
|
|
|
|
while (!is_end_input() && !is_end_output())
|
2019-02-07 18:51:53 +00:00
|
|
|
{
|
|
|
|
auto output = get_next_out();
|
2019-07-09 17:47:13 +00:00
|
|
|
auto input = get_next_input();
|
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
if (output == outputs.end())
|
|
|
|
return get_status_if_no_outputs();
|
|
|
|
|
2019-07-09 17:47:13 +00:00
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
if (input == inputs.end())
|
|
|
|
return get_status_if_no_inputs();
|
|
|
|
|
|
|
|
output->push(input->pull());
|
2018-05-23 20:19:33 +00:00
|
|
|
}
|
2019-02-07 18:51:53 +00:00
|
|
|
|
2019-04-26 11:34:51 +00:00
|
|
|
if (is_end_input())
|
2019-02-07 18:51:53 +00:00
|
|
|
return get_status_if_no_outputs();
|
|
|
|
|
|
|
|
/// cur_input == inputs_end()
|
|
|
|
return get_status_if_no_inputs();
|
2018-05-23 20:19:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:24:44 +00:00
|
|
|
IProcessor::Status ResizeProcessor::prepare(const PortNumbers & updated_inputs, const PortNumbers & updated_outputs)
|
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
initialized = true;
|
|
|
|
|
2020-01-13 12:04:02 +00:00
|
|
|
for (auto & input : inputs)
|
|
|
|
{
|
|
|
|
input.setNeeded();
|
|
|
|
input_ports.push_back({.port = &input, .status = InputStatus::NotActive});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & output : outputs)
|
|
|
|
output_ports.push_back({.port = &output, .status = OutputStatus::NotActive});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & output_number : updated_outputs)
|
|
|
|
{
|
|
|
|
auto & output = output_ports[output_number];
|
|
|
|
if (output.port->isFinished())
|
|
|
|
{
|
|
|
|
if (output.status != OutputStatus::Finished)
|
|
|
|
{
|
|
|
|
++num_finished_outputs;
|
|
|
|
output.status = OutputStatus::Finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.port->canPush())
|
|
|
|
{
|
|
|
|
if (output.status != OutputStatus::NeedData)
|
|
|
|
{
|
|
|
|
output.status = OutputStatus::NeedData;
|
|
|
|
waiting_outputs.push(output_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_finished_outputs == outputs.size())
|
|
|
|
{
|
|
|
|
for (auto & input : inputs)
|
|
|
|
input.close();
|
|
|
|
|
|
|
|
return Status::Finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & input_number : updated_inputs)
|
|
|
|
{
|
|
|
|
auto & input = input_ports[input_number];
|
|
|
|
if (input.port->isFinished())
|
|
|
|
{
|
|
|
|
if (input.status != InputStatus::Finished)
|
|
|
|
{
|
|
|
|
input.status = InputStatus::Finished;
|
|
|
|
++num_finished_inputs;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input.port->hasData())
|
|
|
|
{
|
|
|
|
if (input.status != InputStatus::HasData)
|
|
|
|
{
|
|
|
|
input.status = InputStatus::HasData;
|
|
|
|
inputs_with_data.push(input_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!waiting_outputs.empty() && !inputs_with_data.empty())
|
|
|
|
{
|
|
|
|
auto & waiting_output = output_ports[waiting_outputs.front()];
|
|
|
|
waiting_outputs.pop();
|
|
|
|
|
|
|
|
auto & input_with_data = input_ports[inputs_with_data.front()];
|
|
|
|
inputs_with_data.pop();
|
|
|
|
|
|
|
|
waiting_output.port->pushData(input_with_data.port->pullData());
|
|
|
|
input_with_data.status = InputStatus::NotActive;
|
|
|
|
waiting_output.status = OutputStatus::NotActive;
|
|
|
|
|
|
|
|
if (input_with_data.port->isFinished())
|
|
|
|
{
|
|
|
|
input_with_data.status = InputStatus::Finished;
|
|
|
|
++num_finished_inputs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_finished_inputs == inputs.size())
|
|
|
|
{
|
|
|
|
for (auto & output : outputs)
|
|
|
|
output.finish();
|
|
|
|
|
|
|
|
return Status::Finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!waiting_outputs.empty())
|
|
|
|
return Status::NeedData;
|
|
|
|
|
|
|
|
return Status::PortFull;
|
|
|
|
}
|
|
|
|
|
|
|
|
IProcessor::Status StrictResizeProcessor::prepare(const PortNumbers & updated_inputs, const PortNumbers & updated_outputs)
|
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
initialized = true;
|
|
|
|
|
2019-11-27 16:24:44 +00:00
|
|
|
for (auto & input : inputs)
|
2020-01-14 09:15:34 +00:00
|
|
|
input_ports.push_back({.port = &input, .status = InputStatus::NotActive, .waiting_output = -1});
|
2020-01-13 10:32:55 +00:00
|
|
|
|
|
|
|
for (UInt64 i = 0; i < input_ports.size(); ++i)
|
|
|
|
disabled_input_ports.push(i);
|
2019-11-27 16:24:44 +00:00
|
|
|
|
|
|
|
for (auto & output : outputs)
|
|
|
|
output_ports.push_back({.port = &output, .status = OutputStatus::NotActive});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto & output_number : updated_outputs)
|
|
|
|
{
|
|
|
|
auto & output = output_ports[output_number];
|
|
|
|
if (output.port->isFinished())
|
|
|
|
{
|
|
|
|
if (output.status != OutputStatus::Finished)
|
|
|
|
{
|
|
|
|
++num_finished_outputs;
|
|
|
|
output.status = OutputStatus::Finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.port->canPush())
|
|
|
|
{
|
|
|
|
if (output.status != OutputStatus::NeedData)
|
|
|
|
{
|
|
|
|
output.status = OutputStatus::NeedData;
|
|
|
|
waiting_outputs.push(output_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_finished_outputs == outputs.size())
|
|
|
|
{
|
|
|
|
for (auto & input : inputs)
|
|
|
|
input.close();
|
|
|
|
|
|
|
|
return Status::Finished;
|
|
|
|
}
|
|
|
|
|
2020-01-13 10:32:55 +00:00
|
|
|
std::queue<UInt64> inputs_with_data;
|
|
|
|
|
2019-11-27 16:24:44 +00:00
|
|
|
for (auto & input_number : updated_inputs)
|
|
|
|
{
|
|
|
|
auto & input = input_ports[input_number];
|
|
|
|
if (input.port->isFinished())
|
|
|
|
{
|
|
|
|
if (input.status != InputStatus::Finished)
|
|
|
|
{
|
|
|
|
input.status = InputStatus::Finished;
|
|
|
|
++num_finished_inputs;
|
2020-01-17 17:04:37 +00:00
|
|
|
|
|
|
|
waiting_outputs.push(input.waiting_output);
|
2019-11-27 16:24:44 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input.port->hasData())
|
|
|
|
{
|
2020-01-13 10:32:55 +00:00
|
|
|
if (input.status != InputStatus::NotActive)
|
2019-11-27 16:24:44 +00:00
|
|
|
{
|
2020-01-13 10:32:55 +00:00
|
|
|
input.status = InputStatus::NotActive;
|
2019-11-27 16:24:44 +00:00
|
|
|
inputs_with_data.push(input_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 09:15:34 +00:00
|
|
|
while (!inputs_with_data.empty())
|
2019-11-27 16:24:44 +00:00
|
|
|
{
|
2020-01-13 10:32:55 +00:00
|
|
|
auto input_number = inputs_with_data.front();
|
|
|
|
auto & input_with_data = input_ports[input_number];
|
2019-11-27 16:24:44 +00:00
|
|
|
inputs_with_data.pop();
|
|
|
|
|
2020-01-14 09:15:34 +00:00
|
|
|
if (input_with_data.waiting_output == -1)
|
|
|
|
throw Exception("No associated output for input with data.", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
auto & waiting_output = output_ports[input_with_data.waiting_output];
|
|
|
|
|
|
|
|
if (waiting_output.status != OutputStatus::NeedData)
|
|
|
|
throw Exception("Invalid status for associated output.", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2020-01-13 10:32:55 +00:00
|
|
|
waiting_output.port->pushData(input_with_data.port->pullData(/* set_not_deeded = */ true));
|
2019-11-27 16:24:44 +00:00
|
|
|
waiting_output.status = OutputStatus::NotActive;
|
|
|
|
|
|
|
|
if (input_with_data.port->isFinished())
|
|
|
|
{
|
2019-12-03 12:08:59 +00:00
|
|
|
input_with_data.status = InputStatus::Finished;
|
|
|
|
++num_finished_inputs;
|
2019-11-27 16:24:44 +00:00
|
|
|
}
|
2020-01-13 10:32:55 +00:00
|
|
|
else
|
|
|
|
disabled_input_ports.push(input_number);
|
|
|
|
}
|
|
|
|
|
2020-01-13 10:47:07 +00:00
|
|
|
if (num_finished_inputs == inputs.size())
|
|
|
|
{
|
|
|
|
for (auto & output : outputs)
|
|
|
|
output.finish();
|
|
|
|
|
|
|
|
return Status::Finished;
|
|
|
|
}
|
|
|
|
|
2020-01-13 10:32:55 +00:00
|
|
|
/// Enable more inputs if needed.
|
2020-01-14 09:15:34 +00:00
|
|
|
while (!disabled_input_ports.empty() && !waiting_outputs.empty())
|
2020-01-13 10:32:55 +00:00
|
|
|
{
|
|
|
|
auto & input = input_ports[disabled_input_ports.front()];
|
|
|
|
disabled_input_ports.pop();
|
|
|
|
|
|
|
|
input.port->setNeeded();
|
|
|
|
input.status = InputStatus::NeedData;
|
2020-01-14 09:15:34 +00:00
|
|
|
input.waiting_output = waiting_outputs.front();
|
|
|
|
|
|
|
|
waiting_outputs.pop();
|
2019-11-27 16:24:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 11:39:48 +00:00
|
|
|
while (!waiting_outputs.empty())
|
|
|
|
{
|
|
|
|
auto & output = output_ports[waiting_outputs.front()];
|
|
|
|
waiting_outputs.pop();
|
2019-11-27 16:24:44 +00:00
|
|
|
|
2020-01-18 11:39:48 +00:00
|
|
|
output.status = OutputStatus::Finished;
|
|
|
|
output.port->finish();
|
|
|
|
++num_finished_outputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disabled_input_ports.empty())
|
|
|
|
return Status::NeedData;
|
|
|
|
|
|
|
|
return Status::PortFull;
|
2019-11-27 16:24:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:19:33 +00:00
|
|
|
}
|
|
|
|
|