ClickHouse/src/Processors/ISource.cpp
2020-12-04 13:52:57 +03:00

88 lines
1.5 KiB
C++

#include <Processors/ISource.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
ISource::ISource(Block header)
: IProcessor({}, {std::move(header)}), output(outputs.front())
{
}
ISource::Status ISource::prepare()
{
if (finished || isCancelled())
{
output.finish();
return Status::Finished;
}
/// Check can output.
if (output.isFinished())
return Status::Finished;
if (!output.canPush())
return Status::PortFull;
if (!has_input)
return Status::Ready;
output.pushData(std::move(current_chunk));
has_input = false;
if (got_exception)
{
finished = true;
output.finish();
return Status::Finished;
}
/// Now, we pushed to output, and it must be full.
return Status::PortFull;
}
void ISource::work()
{
try
{
if (auto chunk = tryGenerate())
{
current_chunk.chunk = std::move(*chunk);
if (current_chunk.chunk)
has_input = true;
}
else
finished = true;
if (isCancelled())
finished = true;
}
catch (...)
{
finished = true;
throw;
}
}
Chunk ISource::generate()
{
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "generate is not implemented for {}", getName());
}
std::optional<Chunk> ISource::tryGenerate()
{
auto chunk = generate();
if (!chunk)
return {};
return chunk;
}
}