2018-05-23 20:19:33 +00:00
|
|
|
#include <Processors/ISource.h>
|
2022-05-31 14:43:38 +00:00
|
|
|
#include <QueryPipeline/StreamLocalLimits.h>
|
2018-05-23 20:19:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-12-04 10:52:57 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2022-05-31 14:43:38 +00:00
|
|
|
ISource::~ISource() = default;
|
|
|
|
|
2022-05-20 19:49:31 +00:00
|
|
|
ISource::ISource(Block header, bool enable_auto_progress)
|
|
|
|
: IProcessor({}, {std::move(header)})
|
|
|
|
, auto_progress(enable_auto_progress)
|
|
|
|
, output(outputs.front())
|
2018-05-23 20:19:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ISource::Status ISource::prepare()
|
|
|
|
{
|
2022-05-31 19:40:50 +00:00
|
|
|
if (finished)
|
2018-05-24 02:52:21 +00:00
|
|
|
{
|
2019-02-07 18:51:53 +00:00
|
|
|
output.finish();
|
2018-05-23 20:19:33 +00:00
|
|
|
return Status::Finished;
|
2018-05-24 02:52:21 +00:00
|
|
|
}
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
/// Check can output.
|
|
|
|
if (output.isFinished())
|
|
|
|
return Status::Finished;
|
|
|
|
|
|
|
|
if (!output.canPush())
|
2018-05-23 20:19:33 +00:00
|
|
|
return Status::PortFull;
|
|
|
|
|
2019-02-07 18:51:53 +00:00
|
|
|
if (!has_input)
|
|
|
|
return Status::Ready;
|
2018-05-23 20:19:33 +00:00
|
|
|
|
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;
|
2018-05-23 20:19:33 +00:00
|
|
|
|
2022-05-31 19:40:50 +00:00
|
|
|
if (isCancelled())
|
|
|
|
{
|
|
|
|
output.finish();
|
|
|
|
return Status::Finished;
|
|
|
|
}
|
|
|
|
|
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;
|
2018-05-23 20:19:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 14:43:38 +00:00
|
|
|
void ISource::setStorageLimits(const std::shared_ptr<const StorageLimitsList> & storage_limits_)
|
|
|
|
{
|
|
|
|
storage_limits = storage_limits_;
|
|
|
|
}
|
|
|
|
|
2022-05-09 10:28:05 +00:00
|
|
|
void ISource::progress(size_t read_rows, size_t read_bytes)
|
|
|
|
{
|
2022-05-30 13:10:30 +00:00
|
|
|
//std::cerr << "========= Progress " << read_rows << " from " << getName() << std::endl << StackTrace().toString() << std::endl;
|
2022-05-09 10:28:05 +00:00
|
|
|
read_progress_was_set = true;
|
|
|
|
read_progress.read_rows += read_rows;
|
|
|
|
read_progress.read_bytes += read_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ISource::ReadProgress> ISource::getReadProgress()
|
|
|
|
{
|
2022-06-01 17:39:12 +00:00
|
|
|
if (finished && read_progress.read_bytes == 0 && read_progress.read_bytes == 0 && read_progress.total_rows_approx == 0)
|
2022-06-01 16:56:26 +00:00
|
|
|
return {};
|
|
|
|
|
2022-05-31 14:43:38 +00:00
|
|
|
ReadProgressCounters res_progress;
|
2022-05-09 10:28:05 +00:00
|
|
|
std::swap(read_progress, res_progress);
|
2022-05-31 14:43:38 +00:00
|
|
|
|
|
|
|
if (storage_limits)
|
|
|
|
return ReadProgress{res_progress, *storage_limits};
|
|
|
|
|
|
|
|
static StorageLimitsList empty_limits;
|
|
|
|
return ReadProgress{res_progress, empty_limits};
|
2022-05-09 10:28:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:19:33 +00:00
|
|
|
void ISource::work()
|
|
|
|
{
|
2019-05-14 09:50:04 +00:00
|
|
|
try
|
2019-04-29 18:43:50 +00:00
|
|
|
{
|
2022-05-09 10:28:05 +00:00
|
|
|
read_progress_was_set = false;
|
|
|
|
|
2020-12-04 10:52:57 +00:00
|
|
|
if (auto chunk = tryGenerate())
|
|
|
|
{
|
|
|
|
current_chunk.chunk = std::move(*chunk);
|
|
|
|
if (current_chunk.chunk)
|
2022-05-09 10:28:05 +00:00
|
|
|
{
|
2020-12-04 10:52:57 +00:00
|
|
|
has_input = true;
|
2022-05-20 19:49:31 +00:00
|
|
|
if (auto_progress && !read_progress_was_set)
|
2022-05-09 10:28:05 +00:00
|
|
|
progress(current_chunk.chunk.getNumRows(), current_chunk.chunk.bytes());
|
|
|
|
}
|
2020-12-04 10:52:57 +00:00
|
|
|
}
|
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
|
|
|
}
|
2019-05-14 09:50:04 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
finished = true;
|
|
|
|
throw;
|
|
|
|
}
|
2018-05-23 20:19:33 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-23 20:19:33 +00:00
|
|
|
}
|
|
|
|
|