ClickHouse/src/Processors/ISource.cpp

88 lines
1.5 KiB
C++
Raw Normal View History

#include <Processors/ISource.h>
namespace DB
{
2020-12-04 10:52:57 +00:00
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
ISource::ISource(Block header)
: IProcessor({}, {std::move(header)}), output(outputs.front())
{
}
ISource::Status ISource::prepare()
{
2020-01-27 18:09:20 +00:00
if (finished || isCancelled())
{
2019-02-07 18:51:53 +00:00
output.finish();
return Status::Finished;
}
2019-02-07 18:51:53 +00:00
/// Check can output.
if (output.isFinished())
return Status::Finished;
if (!output.canPush())
return Status::PortFull;
2019-02-07 18:51:53 +00:00
if (!has_input)
return Status::Ready;
2019-04-29 18:43:50 +00:00
output.pushData(std::move(current_chunk));
2019-02-07 18:51:53 +00:00
has_input = false;
2019-04-29 18:43:50 +00:00
if (got_exception)
{
finished = true;
output.finish();
return Status::Finished;
}
2019-02-07 18:51:53 +00:00
/// Now, we pushed to output, and it must be full.
return Status::PortFull;
}
void ISource::work()
{
try
2019-04-29 18:43:50 +00:00
{
2020-12-04 10:52:57 +00:00
if (auto chunk = tryGenerate())
{
current_chunk.chunk = std::move(*chunk);
if (current_chunk.chunk)
has_input = true;
}
2019-04-29 18:43:50 +00:00
else
2020-12-04 10:52:57 +00:00
finished = true;
if (isCancelled())
finished = true;
2019-04-29 18:43:50 +00:00
}
catch (...)
{
finished = true;
throw;
}
}
2020-12-04 10:52:57 +00:00
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;
}
}