ClickHouse/dbms/include/DB/DataStreams/LazyBlockInputStream.h

61 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <DB/DataStreams/IProfilingBlockInputStream.h>
namespace DB
{
/** Инициализировать другой источник при первом вызове read, и затем использовать его.
* Это нужно, например, для чтения из таблицы, которая будет заполнена
* после создания объекта LazyBlockInputStream, но до первого вызова read.
*/
class LazyBlockInputStream : public IProfilingBlockInputStream
{
public:
using Generator = std::function<BlockInputStreamPtr()>;
LazyBlockInputStream(Generator generator_)
: generator(generator_) {}
String getName() const override { return "Lazy"; }
String getID() const override
{
std::stringstream res;
res << "Lazy(" << this << ")";
return res.str();
}
protected:
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();
}
private:
Generator generator;
BlockInputStreamPtr input;
};
}