2015-01-14 02:44:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <DataStreams/IBlockInputStream.h>
|
2015-01-14 02:44:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** Initialize another source on the first `read` call, and then use it.
|
|
|
|
* This is needed, for example, to read from a table that will be populated
|
|
|
|
* after creation of LazyBlockInputStream object, but before the first `read` call.
|
2015-01-14 02:44:25 +00:00
|
|
|
*/
|
2019-01-23 14:48:50 +00:00
|
|
|
class LazyBlockInputStream : public IBlockInputStream
|
2015-01-14 02:44:25 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Generator = std::function<BlockInputStreamPtr()>;
|
2015-01-14 02:44:25 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
LazyBlockInputStream(const Block & header_, Generator generator_)
|
|
|
|
: header(header_), generator(std::move(generator_))
|
2017-08-01 12:25:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
LazyBlockInputStream(const char * name_, const Block & header_, Generator generator_)
|
|
|
|
: name(name_), header(header_), generator(std::move(generator_))
|
2017-08-01 12:25:02 +00:00
|
|
|
{
|
|
|
|
}
|
2015-01-14 02:44:25 +00:00
|
|
|
|
2017-08-01 12:25:02 +00:00
|
|
|
String getName() const override { return name; }
|
2015-01-14 02:44:25 +00:00
|
|
|
|
2018-02-18 03:23:48 +00:00
|
|
|
Block getHeader() const override
|
2018-01-06 18:10:44 +00:00
|
|
|
{
|
2018-02-15 19:48:41 +00:00
|
|
|
return header;
|
2018-01-06 18:10:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 17:17:16 +00:00
|
|
|
/// We call readPrefix lazily. Suppress default behaviour.
|
|
|
|
void readPrefix() override {}
|
|
|
|
|
2015-01-14 02:44:25 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
Block readImpl() override
|
|
|
|
{
|
|
|
|
if (!input)
|
2018-02-15 19:48:41 +00:00
|
|
|
{
|
|
|
|
input = generator();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
if (!input)
|
|
|
|
return Block();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
auto * p_input = dynamic_cast<IBlockInputStream *>(input.get());
|
2018-01-06 18:10:44 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
if (p_input)
|
|
|
|
{
|
|
|
|
/// They could have been set before, but were not passed into the `input`.
|
|
|
|
if (progress_callback)
|
|
|
|
p_input->setProgressCallback(progress_callback);
|
|
|
|
if (process_list_elem)
|
|
|
|
p_input->setProcessListElement(process_list_elem);
|
|
|
|
}
|
2018-01-06 18:10:44 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
input->readPrefix();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
{
|
2018-02-23 10:43:24 +00:00
|
|
|
addChild(input);
|
2017-09-10 03:17:05 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
if (isCancelled() && p_input)
|
2018-03-05 21:09:39 +00:00
|
|
|
p_input->cancel(is_killed);
|
2018-02-15 19:48:41 +00:00
|
|
|
}
|
2018-01-06 18:10:44 +00:00
|
|
|
}
|
2017-09-10 03:17:05 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
return input->read();
|
|
|
|
}
|
2015-01-14 02:44:25 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
private:
|
|
|
|
const char * name = "Lazy";
|
|
|
|
Block header;
|
|
|
|
Generator generator;
|
2017-08-01 12:25:02 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
BlockInputStreamPtr input;
|
2015-01-14 02:44:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|