ClickHouse/src/Storages/MergeTree/MergeTreeDataPartWriterInMemory.cpp

84 lines
2.8 KiB
C++
Raw Normal View History

2020-04-14 19:47:19 +00:00
#include <Storages/MergeTree/MergeTreeDataPartWriterInMemory.h>
#include <Storages/MergeTree/MergeTreeDataPartInMemory.h>
#include <Storages/MergeTree/MergeTreeWriteAheadLog.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
MergeTreeDataPartWriterInMemory::MergeTreeDataPartWriterInMemory(
const DataPartInMemoryPtr & part_,
const NamesAndTypesList & columns_list_,
2020-06-26 11:30:23 +00:00
const StorageMetadataPtr & metadata_snapshot_,
2020-04-14 19:47:19 +00:00
const MergeTreeWriterSettings & settings_)
2020-06-26 11:30:23 +00:00
: IMergeTreeDataPartWriter(part_, columns_list_, metadata_snapshot_, settings_)
, part_in_memory(part_) {}
2020-04-14 19:47:19 +00:00
void MergeTreeDataPartWriterInMemory::write(
const Block & block, const IColumn::Permutation * permutation,
const Block & primary_key_block, const Block & /* skip_indexes_block */)
{
if (part_in_memory->block)
2020-04-14 19:47:19 +00:00
throw Exception("DataPartWriterInMemory supports only one write", ErrorCodes::LOGICAL_ERROR);
Block result_block;
if (permutation)
{
2020-05-14 20:08:15 +00:00
for (const auto & col : columns_list)
2020-04-14 19:47:19 +00:00
{
2020-05-14 20:08:15 +00:00
if (primary_key_block.has(col.name))
result_block.insert(primary_key_block.getByName(col.name));
2020-04-14 19:47:19 +00:00
else
{
2020-05-14 20:08:15 +00:00
auto permuted = block.getByName(col.name);
permuted.column = permuted.column->permute(*permutation, 0);
result_block.insert(permuted);
2020-04-14 19:47:19 +00:00
}
}
}
else
{
2020-05-14 20:08:15 +00:00
for (const auto & col : columns_list)
result_block.insert(block.getByName(col.name));
2020-04-14 19:47:19 +00:00
}
2020-05-20 12:02:02 +00:00
index_granularity.appendMark(result_block.rows());
if (with_final_mark)
index_granularity.appendMark(0);
part_in_memory->block = std::move(result_block);
2020-04-14 19:47:19 +00:00
}
void MergeTreeDataPartWriterInMemory::calculateAndSerializePrimaryIndex(const Block & primary_index_block)
{
size_t rows = primary_index_block.rows();
if (!rows)
return;
size_t primary_columns_num = primary_index_block.columns();
index_columns.resize(primary_columns_num);
for (size_t i = 0; i < primary_columns_num; ++i)
{
const auto & primary_column = *primary_index_block.getByPosition(i).column;
index_columns[i] = primary_column.cloneEmpty();
index_columns[i]->insertFrom(primary_column, 0);
2020-05-14 20:08:15 +00:00
if (with_final_mark)
index_columns[i]->insertFrom(primary_column, rows - 1);
2020-04-14 19:47:19 +00:00
}
}
2020-08-24 13:09:23 +00:00
void MergeTreeDataPartWriterInMemory::finishDataSerialization(IMergeTreeDataPart::Checksums & checksums, bool)
2020-04-14 19:47:19 +00:00
{
2020-06-05 20:47:46 +00:00
/// If part is empty we still need to initialize block by empty columns.
if (!part_in_memory->block)
for (const auto & column : columns_list)
part_in_memory->block.insert(ColumnWithTypeAndName{column.type, column.name});
checksums.files["data.bin"] = part_in_memory->calculateBlockChecksum();
2020-04-14 19:47:19 +00:00
}
}