#pragma once #include #include #include #include namespace DB { /// Merges several sorted inputs into one sorted output. class MergingSortedAlgorithm final : public IMergingAlgorithm { public: MergingSortedAlgorithm( Block header_, size_t num_inputs, const SortDescription & description_, size_t max_block_size_, size_t max_block_size_bytes_, SortingQueueStrategy sorting_queue_strategy_, UInt64 limit_ = 0, WriteBuffer * out_row_sources_buf_ = nullptr, bool use_average_block_sizes = false); void addInput(); const char * getName() const override { return "MergingSortedAlgorithm"; } void initialize(Inputs inputs) override; void consume(Input & input, size_t source_num) override; Status merge() override; MergedStats getMergedStats() const override { return merged_data.getMergedStats(); } private: Block header; MergedData merged_data; /// Settings SortDescription description; const UInt64 limit; bool has_collation = false; /// Used in Vertical merge algorithm to gather non-PK/non-index columns (on next step) /// If it is not nullptr then it should be populated during execution WriteBuffer * out_row_sources_buf = nullptr; /// Chunks currently being merged. Inputs current_inputs; SortingQueueStrategy sorting_queue_strategy; SortCursorImpls cursors; SortQueueVariants queue_variants; template Status mergeImpl(TSortingQueue & queue); template Status mergeBatchImpl(TSortingQueue & queue); }; }