ClickHouse/dbms/src/DataStreams/ReplacingSortedBlockInputStream.h

60 lines
1.8 KiB
C++
Raw Normal View History

2016-04-15 19:09:42 +00:00
#pragma once
#include <common/logger_useful.h>
#include <DataStreams/MergingSortedBlockInputStream.h>
2016-04-15 19:09:42 +00:00
namespace DB
{
2017-05-13 22:19:04 +00:00
/** Merges several sorted streams into one.
* For each group of consecutive identical values of the primary key (the columns by which the data is sorted),
* keeps row with max `version` value.
2016-04-15 19:09:42 +00:00
*/
class ReplacingSortedBlockInputStream : public MergingSortedBlockInputStream
{
public:
ReplacingSortedBlockInputStream(
const BlockInputStreams & inputs_, const SortDescription & description_,
const String & version_column, size_t max_block_size_, WriteBuffer * out_row_sources_buf_ = nullptr)
: MergingSortedBlockInputStream(inputs_, description_, max_block_size_, 0, out_row_sources_buf_)
{
if (!version_column.empty())
version_column_number = header.getPositionByName(version_column);
}
2016-04-15 19:09:42 +00:00
String getName() const override { return "ReplacingSorted"; }
2016-04-15 19:09:42 +00:00
protected:
2017-05-13 22:19:04 +00:00
/// Can return 1 more records than max_block_size.
Block readImpl() override;
2016-04-15 19:09:42 +00:00
private:
ssize_t version_column_number = -1;
2016-04-15 19:09:42 +00:00
Logger * log = &Logger::get("ReplacingSortedBlockInputStream");
2016-04-15 19:09:42 +00:00
/// All data has been read.
bool finished = false;
2016-04-15 19:09:42 +00:00
/// Primary key of current row.
RowRef current_key;
/// Primary key of next row.
RowRef next_key;
/// Last row with maximum version for current primary key.
RowRef selected_row;
/// The position (into current_row_sources) of the row with the highest version.
size_t max_pos = 0;
2016-04-15 19:09:42 +00:00
/// Sources of rows with the current primary key.
PODArray<RowSourcePart> current_row_sources;
void merge(MutableColumns & merged_columns, std::priority_queue<SortCursor> & queue);
2016-04-15 19:09:42 +00:00
2017-05-13 22:19:04 +00:00
/// Output into result the rows for current primary key.
void insertRow(MutableColumns & merged_columns, size_t & merged_rows);
2016-04-15 19:09:42 +00:00
};
}