ClickHouse/dbms/src/Processors/QueryPipeline.h

136 lines
4.5 KiB
C++
Raw Normal View History

2019-03-26 18:28:37 +00:00
#pragma once
#include <Processors/IProcessor.h>
2019-04-05 10:52:07 +00:00
#include <Processors/Executors/PipelineExecutor.h>
2019-11-05 17:33:03 +00:00
#include <Processors/Pipe.h>
2019-04-05 10:52:07 +00:00
#include <DataStreams/IBlockInputStream.h>
#include <DataStreams/IBlockOutputStream.h>
#include <Storages/IStorage_fwd.h>
2019-03-26 18:28:37 +00:00
namespace DB
{
class TableStructureReadLock;
using TableStructureReadLockPtr = std::shared_ptr<TableStructureReadLock>;
2019-11-05 17:33:03 +00:00
using TableStructureReadLocks = std::vector<TableStructureReadLockHolder>;
2019-03-26 18:28:37 +00:00
class Context;
2019-04-08 14:55:20 +00:00
class IOutputFormat;
2019-03-26 18:28:37 +00:00
class QueryPipeline
{
public:
QueryPipeline() = default;
2019-11-05 17:33:03 +00:00
/// All pipes must have same header.
void init(Pipes pipes);
void init(Pipe pipe); /// Simple init for single pipe
2019-03-26 18:28:37 +00:00
bool initialized() { return !processors.empty(); }
2019-04-09 10:17:25 +00:00
enum class StreamType
{
Main = 0,
Totals,
Extremes,
};
2019-03-26 18:28:37 +00:00
using ProcessorGetter = std::function<ProcessorPtr(const Block & header)>;
2019-04-09 10:17:25 +00:00
using ProcessorGetterWithStreamKind = std::function<ProcessorPtr(const Block & header, StreamType stream_type)>;
2019-03-26 18:28:37 +00:00
2019-04-05 10:52:07 +00:00
void addSimpleTransform(const ProcessorGetter & getter);
2019-04-09 10:17:25 +00:00
void addSimpleTransform(const ProcessorGetterWithStreamKind & getter);
2019-03-26 18:28:37 +00:00
void addPipe(Processors pipe);
void addTotalsHavingTransform(ProcessorPtr transform);
void addExtremesTransform(ProcessorPtr transform);
void addCreatingSetsTransform(ProcessorPtr transform);
void setOutput(ProcessorPtr output);
2019-04-09 14:51:38 +00:00
/// Add totals which returns one chunk with single row with defaults.
void addDefaultTotals();
2019-04-10 16:28:37 +00:00
/// Add already calculated totals.
void addTotals(ProcessorPtr source);
2019-04-17 15:35:22 +00:00
void dropTotalsIfHas();
2019-03-26 18:28:37 +00:00
/// Will read from this stream after all data was read from other streams.
void addDelayedStream(ProcessorPtr source);
2019-04-08 09:31:49 +00:00
bool hasDelayedStream() const { return delayed_stream_port; }
2019-05-14 11:04:11 +00:00
/// Check if resize transform was used. (In that case another distinct transform will be added).
2019-05-14 13:13:12 +00:00
bool hasMixedStreams() const { return has_resize || hasMoreThanOneStream(); }
2019-03-26 18:28:37 +00:00
void resize(size_t num_streams, bool force = false);
2019-03-26 18:28:37 +00:00
2019-04-09 13:07:07 +00:00
void unitePipelines(std::vector<QueryPipeline> && pipelines, const Block & common_header, const Context & context);
2019-03-26 18:28:37 +00:00
PipelineExecutorPtr execute();
2019-03-26 18:28:37 +00:00
2019-04-08 09:31:49 +00:00
size_t getNumStreams() const { return streams.size() + (hasDelayedStream() ? 1 : 0); }
size_t getNumMainStreams() const { return streams.size(); }
2019-04-09 14:51:38 +00:00
2019-04-03 11:21:38 +00:00
bool hasMoreThanOneStream() const { return getNumStreams() > 1; }
2019-04-09 14:51:38 +00:00
bool hasTotals() const { return totals_having_port != nullptr; }
2019-03-26 18:28:37 +00:00
const Block & getHeader() const { return current_header; }
2019-11-05 17:33:03 +00:00
void addTableLock(const TableStructureReadLockHolder & lock) { table_locks.push_back(lock); }
void addInterpreterContext(std::shared_ptr<Context> context) { interpreter_context.emplace_back(std::move(context)); }
void addStorageHolder(StoragePtr storage) { storage_holder.emplace_back(std::move(storage)); }
2019-03-26 18:28:37 +00:00
/// For compatibility with IBlockInputStream.
void setProgressCallback(const ProgressCallback & callback);
void setProcessListElement(QueryStatus * elem);
2019-04-08 14:55:20 +00:00
/// Call after execution.
void finalize();
2019-08-27 18:37:28 +00:00
void setMaxThreads(size_t max_threads_) { max_threads = max_threads_; }
size_t getMaxThreads() const { return max_threads; }
2019-03-26 18:28:37 +00:00
private:
/// All added processors.
Processors processors;
/// Port for each independent "stream".
std::vector<OutputPort *> streams;
/// Special ports for extremes and totals having.
OutputPort * totals_having_port = nullptr;
OutputPort * extremes_port = nullptr;
2019-04-08 09:31:49 +00:00
/// Special port for delayed stream.
OutputPort * delayed_stream_port = nullptr;
2019-05-14 11:04:11 +00:00
/// If resize processor was added to pipeline.
2019-05-14 13:13:12 +00:00
bool has_resize = false;
2019-05-14 11:04:11 +00:00
2019-03-26 18:28:37 +00:00
/// Common header for each stream.
Block current_header;
TableStructureReadLocks table_locks;
/// Some Streams (or Processors) may implicitly use Context or temporary Storage created by Interpreter.
/// But lifetime of Streams is not nested in lifetime of Interpreters, so we have to store it here,
/// because QueryPipeline is alive until query is finished.
std::vector<std::shared_ptr<Context>> interpreter_context;
std::vector<StoragePtr> storage_holder;
2019-04-08 14:55:20 +00:00
IOutputFormat * output_format = nullptr;
2019-03-26 18:28:37 +00:00
2019-08-27 18:37:28 +00:00
size_t max_threads = 0;
2019-03-26 18:28:37 +00:00
void checkInitialized();
2019-04-17 14:38:16 +00:00
void checkSource(const ProcessorPtr & source, bool can_have_totals);
2019-03-26 18:28:37 +00:00
void concatDelayedStream();
2019-04-08 14:55:20 +00:00
2019-04-09 10:17:25 +00:00
template <typename TProcessorGetter>
void addSimpleTransformImpl(const TProcessorGetter & getter);
2019-04-08 14:55:20 +00:00
void calcRowsBeforeLimit();
2019-03-26 18:28:37 +00:00
};
}