ClickHouse/dbms/src/DataStreams/ReplacingSortedBlockInputStream.cpp

116 lines
2.5 KiB
C++
Raw Normal View History

2016-04-15 19:09:42 +00:00
#include <DB/DataStreams/ReplacingSortedBlockInputStream.h>
#include <DB/Columns/ColumnsNumber.h>
namespace DB
{
void ReplacingSortedBlockInputStream::insertRow(ColumnPlainPtrs & merged_columns, size_t & merged_rows)
{
++merged_rows;
for (size_t i = 0; i < num_columns; ++i)
merged_columns[i]->insertFrom(*selected_row.columns[i], selected_row.row_num);
}
Block ReplacingSortedBlockInputStream::readImpl()
{
if (finished)
return Block();
if (children.size() == 1)
return children[0]->read();
Block merged_block;
ColumnPlainPtrs merged_columns;
init(merged_block, merged_columns);
if (merged_columns.empty())
return Block();
/// Additional initialization.
2016-04-15 19:09:42 +00:00
if (selected_row.empty())
{
selected_row.columns.resize(num_columns);
if (!version_column.empty())
version_column_number = merged_block.getPositionByName(version_column);
}
if (has_collation)
merge(merged_columns, queue_with_collation);
else
merge(merged_columns, queue);
return merged_block;
}
template<class TSortCursor>
void ReplacingSortedBlockInputStream::merge(ColumnPlainPtrs & merged_columns, std::priority_queue<TSortCursor> & queue)
{
size_t merged_rows = 0;
2017-03-25 20:12:56 +00:00
/// Take the rows in needed order and put them into `merged_block` until rows no more than `max_block_size`
2016-04-15 19:09:42 +00:00
while (!queue.empty())
{
TSortCursor current = queue.top();
if (current_key.empty())
{
current_key.columns.resize(description.size());
next_key.columns.resize(description.size());
setPrimaryKeyRef(current_key, current);
}
UInt64 version = version_column_number != -1
? current->all_columns[version_column_number]->get64(current->pos)
: 0;
setPrimaryKeyRef(next_key, current);
bool key_differs = next_key != current_key;
2017-03-25 20:12:56 +00:00
/// if there are enough rows and the last one is calculated completely
2016-04-15 19:09:42 +00:00
if (key_differs && merged_rows >= max_block_size)
return;
queue.pop();
if (key_differs)
{
max_version = 0;
2017-03-25 20:12:56 +00:00
/// Write the data for the previous primary key.
2016-04-15 19:09:42 +00:00
insertRow(merged_columns, merged_rows);
current_key.swap(next_key);
}
2017-03-25 20:12:56 +00:00
/// A non-strict comparison, since we select the last row for the same version values.
2016-04-15 19:09:42 +00:00
if (version >= max_version)
{
max_version = version;
setRowRef(selected_row, current);
}
if (!current->isLast())
{
current->next();
queue.push(current);
}
else
{
2017-03-25 20:12:56 +00:00
/// We get the next block from the corresponding source, if there is one.
2016-04-15 19:09:42 +00:00
fetchNextBlock(current, queue);
}
}
2017-03-25 20:12:56 +00:00
/// We will write the data for the last primary key.
2016-04-15 19:09:42 +00:00
insertRow(merged_columns, merged_rows);
finished = true;
}
}