ClickHouse/src/Processors/Pipe.cpp

153 lines
4.7 KiB
C++
Raw Normal View History

#include <Processors/Pipe.h>
#include <IO/WriteHelpers.h>
2020-04-08 12:40:04 +00:00
#include <Processors/Sources/SourceFromInputStream.h>
namespace DB
{
2020-02-25 18:10:48 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
static void checkSingleInput(const IProcessor & transform)
{
if (transform.getInputs().size() != 1)
throw Exception("Processor for pipe should have single input, "
"but " + transform.getName() + " has " +
toString(transform.getInputs().size()) + " inputs.", ErrorCodes::LOGICAL_ERROR);
}
static void checkMultipleInputs(const IProcessor & transform, size_t num_inputs)
{
if (transform.getInputs().size() != num_inputs)
throw Exception("Processor for pipe should have " + toString(num_inputs) + " inputs, "
"but " + transform.getName() + " has " +
toString(transform.getInputs().size()) + " inputs.", ErrorCodes::LOGICAL_ERROR);
}
static void checkSingleOutput(const IProcessor & transform)
{
if (transform.getOutputs().size() != 1)
throw Exception("Processor for pipe should have single output, "
"but " + transform.getName() + " has " +
toString(transform.getOutputs().size()) + " outputs.", ErrorCodes::LOGICAL_ERROR);
}
static void checkSimpleTransform(const IProcessor & transform)
{
checkSingleInput(transform);
checkSingleOutput(transform);
}
static void checkSource(const IProcessor & source)
{
if (!source.getInputs().empty())
throw Exception("Source for pipe shouldn't have any input, but " + source.getName() + " has " +
toString(source.getInputs().size()) + " inputs.", ErrorCodes::LOGICAL_ERROR);
if (source.getOutputs().empty())
throw Exception("Source for pipe should have single output, but it doesn't have any",
ErrorCodes::LOGICAL_ERROR);
2020-04-08 12:40:04 +00:00
if (source.getOutputs().size() > 1)
2019-11-05 17:33:03 +00:00
throw Exception("Source for pipe should have single or two outputs, but " + source.getName() + " has " +
toString(source.getOutputs().size()) + " outputs.", ErrorCodes::LOGICAL_ERROR);
}
Pipe::Pipe(ProcessorPtr source)
{
2020-04-08 12:40:04 +00:00
if (auto * source_from_input_stream = typeid_cast<SourceFromInputStream *>(source.get()))
{
totals = source_from_input_stream->getTotalsPort();
extremes = source_from_input_stream->getExtremesPort();
}
else if (source->getOutputs().size() != 1)
checkSource(*source);
2019-11-05 17:33:03 +00:00
2020-04-08 12:40:04 +00:00
output_port = &source->getOutputs().front();
2019-11-05 17:33:03 +00:00
processors.emplace_back(std::move(source));
max_parallel_streams = 1;
}
2020-04-08 12:40:04 +00:00
Pipe::Pipe(Processors processors_, OutputPort * output_port_, OutputPort * totals_, OutputPort * extremes_)
: processors(std::move(processors_)), output_port(output_port_), totals(totals_), extremes(extremes_)
{
}
Pipe::Pipe(Pipes && pipes, ProcessorPtr transform)
{
checkSingleOutput(*transform);
checkMultipleInputs(*transform, pipes.size());
auto it = transform->getInputs().begin();
for (auto & pipe : pipes)
{
connect(*pipe.output_port, *it);
++it;
max_parallel_streams += pipe.max_parallel_streams;
processors.insert(processors.end(), pipe.processors.begin(), pipe.processors.end());
}
output_port = &transform->getOutputs().front();
processors.emplace_back(std::move(transform));
}
2020-04-22 13:52:07 +00:00
Pipe::Pipe(OutputPort * port) : output_port(port)
{
}
void Pipe::addProcessors(const Processors & processors_)
{
processors.insert(processors.end(), processors_.begin(), processors_.end());
}
void Pipe::addSimpleTransform(ProcessorPtr transform)
{
checkSimpleTransform(*transform);
connect(*output_port, transform->getInputs().front());
output_port = &transform->getOutputs().front();
processors.emplace_back(std::move(transform));
}
2019-11-05 17:33:03 +00:00
void Pipe::setLimits(const ISourceWithProgress::LocalLimits & limits)
{
for (auto & processor : processors)
{
2019-11-06 11:36:33 +00:00
if (auto * source_with_progress = dynamic_cast<ISourceWithProgress *>(processor.get()))
2019-11-05 17:33:03 +00:00
source_with_progress->setLimits(limits);
}
}
void Pipe::setQuota(const std::shared_ptr<const EnabledQuota> & quota)
2019-11-05 17:33:03 +00:00
{
for (auto & processor : processors)
{
2019-11-06 11:36:33 +00:00
if (auto * source_with_progress = dynamic_cast<ISourceWithProgress *>(processor.get()))
2019-11-05 17:33:03 +00:00
source_with_progress->setQuota(quota);
}
}
void Pipe::pinSources(size_t executor_number)
{
for (auto & processor : processors)
{
2019-11-06 11:36:33 +00:00
if (auto * source = dynamic_cast<ISource *>(processor.get()))
2019-11-05 17:33:03 +00:00
source->setStream(executor_number);
}
}
2019-12-26 16:15:31 +00:00
void Pipe::enableQuota()
{
for (auto & processor : processors)
{
if (auto * source = dynamic_cast<ISource *>(processor.get()))
source->enableQuota();
}
}
}