2015-01-14 02:44:25 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
|
#include <DataStreams/IProfilingBlockInputStream.h>
|
2015-01-14 02:44:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Инициализировать другой источник при первом вызове read, и затем использовать его.
|
|
|
|
|
* Это нужно, например, для чтения из таблицы, которая будет заполнена
|
|
|
|
|
* после создания объекта LazyBlockInputStream, но до первого вызова read.
|
|
|
|
|
*/
|
|
|
|
|
class LazyBlockInputStream : public IProfilingBlockInputStream
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
using Generator = std::function<BlockInputStreamPtr()>;
|
2015-01-14 02:44:25 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
LazyBlockInputStream(Generator generator_)
|
|
|
|
|
: generator(generator_) {}
|
2015-01-14 02:44:25 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
String getName() const override { return "Lazy"; }
|
2015-01-14 02:44:25 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
String getID() const override
|
|
|
|
|
{
|
|
|
|
|
std::stringstream res;
|
|
|
|
|
res << "Lazy(" << this << ")";
|
|
|
|
|
return res.str();
|
|
|
|
|
}
|
2015-01-14 02:44:25 +00:00
|
|
|
|
|
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Block readImpl() override
|
|
|
|
|
{
|
|
|
|
|
if (!input)
|
|
|
|
|
{
|
|
|
|
|
input = generator();
|
|
|
|
|
|
|
|
|
|
if (!input)
|
|
|
|
|
return Block();
|
|
|
|
|
|
|
|
|
|
children.push_back(input);
|
|
|
|
|
|
|
|
|
|
if (IProfilingBlockInputStream * p_input = dynamic_cast<IProfilingBlockInputStream *>(input.get()))
|
|
|
|
|
{
|
|
|
|
|
/// Они могли быть установлены раньше, но не были протащены в input.
|
|
|
|
|
if (progress_callback)
|
|
|
|
|
p_input->setProgressCallback(progress_callback);
|
|
|
|
|
if (process_list_elem)
|
|
|
|
|
p_input->setProcessListElement(process_list_elem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return input->read();
|
|
|
|
|
}
|
2015-01-14 02:44:25 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Generator generator;
|
|
|
|
|
BlockInputStreamPtr input;
|
2015-01-14 02:44:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|