2012-07-25 19:53:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Row.h>
|
|
|
|
#include <Core/SortDescription.h>
|
|
|
|
#include <Core/SortCursor.h>
|
2012-07-25 19:53:43 +00:00
|
|
|
|
2018-06-05 19:46:49 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <DataStreams/IBlockInputStream.h>
|
2012-07-25 19:53:43 +00:00
|
|
|
|
|
|
|
|
2020-03-20 03:32:47 +00:00
|
|
|
namespace Poco { class Logger; }
|
|
|
|
|
|
|
|
|
2012-07-25 19:53:43 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-12-15 20:03:21 +00:00
|
|
|
/** Merges several sorted streams into one sorted stream.
|
2012-07-25 19:53:43 +00:00
|
|
|
*/
|
2019-01-23 14:48:50 +00:00
|
|
|
class MergingSortedBlockInputStream : public IBlockInputStream
|
2012-07-25 19:53:43 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
/** limit - if isn't 0, then we can produce only first limit rows in sorted order.
|
|
|
|
* out_row_sources - if isn't nullptr, then at the end of execution it should contain part numbers of each readed row (and needed flag)
|
|
|
|
* quiet - don't log profiling info
|
|
|
|
*/
|
2017-07-04 12:38:53 +00:00
|
|
|
MergingSortedBlockInputStream(
|
2020-04-14 11:32:38 +00:00
|
|
|
const BlockInputStreams & inputs_, SortDescription description_, size_t max_block_size_,
|
2020-04-14 11:25:01 +00:00
|
|
|
UInt64 limit_ = 0, WriteBuffer * out_row_sources_buf_ = nullptr, bool quiet_ = false);
|
2012-07-25 19:53:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return "MergingSorted"; }
|
2012-07-25 19:53:43 +00:00
|
|
|
|
2017-04-07 19:47:21 +00:00
|
|
|
bool isSortedOutput() const override { return true; }
|
|
|
|
const SortDescription & getSortDescription() const override { return description; }
|
|
|
|
|
2018-04-07 01:46:50 +00:00
|
|
|
Block getHeader() const override { return header; }
|
2018-01-06 18:10:44 +00:00
|
|
|
|
2012-08-14 20:33:37 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
Block readImpl() override;
|
|
|
|
|
|
|
|
void readSuffixImpl() override;
|
|
|
|
|
2018-04-07 01:46:50 +00:00
|
|
|
/// Initializes the queue and the columns of next result block.
|
|
|
|
void init(MutableColumns & merged_columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Gets the next block from the source corresponding to the `current`.
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename TSortCursor>
|
2019-12-22 00:19:07 +00:00
|
|
|
void fetchNextBlock(const TSortCursor & current, SortingHeap<TSortCursor> & queue);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-04-07 01:46:50 +00:00
|
|
|
Block header;
|
|
|
|
|
2017-04-07 19:47:21 +00:00
|
|
|
const SortDescription description;
|
2019-02-19 01:47:27 +00:00
|
|
|
const size_t max_block_size;
|
2019-02-10 15:17:45 +00:00
|
|
|
UInt64 limit;
|
2019-02-19 01:47:27 +00:00
|
|
|
UInt64 total_merged_rows = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
bool has_collation = false;
|
|
|
|
bool quiet = false;
|
|
|
|
|
|
|
|
/// May be smaller or equal to max_block_size. To do 'reserve' for columns.
|
|
|
|
size_t expected_block_size = 0;
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Blocks currently being merged.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t num_columns = 0;
|
2020-04-14 11:32:38 +00:00
|
|
|
Blocks source_blocks;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-12-21 22:16:39 +00:00
|
|
|
SortCursorImpls cursors;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-12-22 00:19:07 +00:00
|
|
|
SortingHeap<SortCursor> queue_without_collation;
|
|
|
|
SortingHeap<SortCursorWithCollation> queue_with_collation;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
/// Used in Vertical merge algorithm to gather non-PK/non-index columns (on next step)
|
2017-04-01 07:20:54 +00:00
|
|
|
/// If it is not nullptr then it should be populated during execution
|
2017-07-04 12:38:53 +00:00
|
|
|
WriteBuffer * out_row_sources_buf;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2012-08-14 20:33:37 +00:00
|
|
|
private:
|
2014-11-08 23:52:18 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** We support two different cursors - with Collation and without.
|
2019-12-22 00:19:07 +00:00
|
|
|
* Templates are used instead of polymorphic SortCursor and calls to virtual functions.
|
|
|
|
*/
|
|
|
|
template <typename TSortingHeap>
|
|
|
|
void merge(MutableColumns & merged_columns, TSortingHeap & queue);
|
2014-11-08 23:52:18 +00:00
|
|
|
|
2020-03-20 03:32:47 +00:00
|
|
|
Poco::Logger * log;
|
2015-01-18 08:25:56 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Read is finished.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool finished = false;
|
2012-07-25 19:53:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|