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
|
|
|
|
{
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2017-08-01 12:25:02 +00:00
|
|
|
void cancel() override
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(cancel_mutex);
|
|
|
|
IProfilingBlockInputStream::cancel();
|
|
|
|
}
|
|
|
|
|
2018-01-06 18:10:44 +00:00
|
|
|
Block getHeader() override
|
|
|
|
{
|
2018-02-15 19:48:41 +00:00
|
|
|
return header;
|
2018-01-06 18:10:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
auto * p_input = dynamic_cast<IProfilingBlockInputStream *>(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
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(cancel_mutex);
|
2017-08-01 12:25:02 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
/** TODO Data race here. See IProfilingBlockInputStream::collectAndSendTotalRowsApprox.
|
|
|
|
Assume following pipeline:
|
2018-01-09 00:19:58 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
RemoteBlockInputStream
|
|
|
|
AsynchronousBlockInputStream
|
|
|
|
LazyBlockInputStream
|
2017-08-01 12:25:02 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
RemoteBlockInputStream calls AsynchronousBlockInputStream::readPrefix
|
|
|
|
and AsynchronousBlockInputStream spawns a thread and returns.
|
2017-08-01 12:25:02 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
The separate thread will call LazyBlockInputStream::read
|
|
|
|
LazyBlockInputStream::read will add more children to itself
|
2018-01-09 00:19:58 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
In the same moment, in main thread, RemoteBlockInputStream::read is called,
|
|
|
|
then IProfilingBlockInputStream::collectAndSendTotalRowsApprox is called
|
|
|
|
and iterates over set of children.
|
|
|
|
*/
|
|
|
|
children.push_back(input);
|
2017-09-10 03:17:05 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
if (isCancelled() && p_input)
|
|
|
|
p_input->cancel();
|
|
|
|
}
|
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;
|
2017-08-01 12:25:02 +00:00
|
|
|
|
2018-02-15 19:48:41 +00:00
|
|
|
std::mutex cancel_mutex;
|
2015-01-14 02:44:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|