ClickHouse/src/Storages/MergeTree/MergeTreeDataPartCompact.cpp

223 lines
9.0 KiB
C++
Raw Normal View History

2019-10-16 18:27:53 +00:00
#include "MergeTreeDataPartCompact.h"
2020-11-10 17:32:00 +00:00
#include <DataTypes/NestedUtils.h>
2019-10-16 18:27:53 +00:00
#include <Storages/MergeTree/MergeTreeReaderCompact.h>
2019-10-22 10:50:17 +00:00
#include <Storages/MergeTree/MergeTreeDataPartWriterCompact.h>
2022-09-05 16:55:00 +00:00
#include <Storages/MergeTree/LoadedMergeTreeDataPartInfoForReader.h>
2019-10-16 18:27:53 +00:00
namespace DB
{
namespace ErrorCodes
{
2020-02-25 18:02:41 +00:00
extern const int CANNOT_READ_ALL_DATA;
extern const int NOT_IMPLEMENTED;
2019-10-16 18:27:53 +00:00
extern const int NO_FILE_IN_DATA_PART;
extern const int BAD_SIZE_OF_FILE_IN_DATA_PART;
}
MergeTreeDataPartCompact::MergeTreeDataPartCompact(
2019-10-16 18:27:53 +00:00
MergeTreeData & storage_,
const String & name_,
2022-04-07 17:44:49 +00:00
const DataPartStoragePtr & data_part_storage_,
const IMergeTreeDataPart * parent_part_)
: IMergeTreeDataPart(storage_, name_, data_part_storage_, Type::Compact, parent_part_)
2019-10-16 18:27:53 +00:00
{
}
MergeTreeDataPartCompact::MergeTreeDataPartCompact(
const MergeTreeData & storage_,
const String & name_,
const MergeTreePartInfo & info_,
2022-04-07 17:44:49 +00:00
const DataPartStoragePtr & data_part_storage_,
const IMergeTreeDataPart * parent_part_)
: IMergeTreeDataPart(storage_, name_, info_, data_part_storage_, Type::Compact, parent_part_)
2019-10-16 18:27:53 +00:00
{
}
IMergeTreeDataPart::MergeTreeReaderPtr MergeTreeDataPartCompact::getReader(
const NamesAndTypesList & columns_to_read,
const StorageMetadataPtr & metadata_snapshot,
2019-10-16 18:27:53 +00:00
const MarkRanges & mark_ranges,
UncompressedCache * uncompressed_cache,
MarkCache * mark_cache,
const MergeTreeReaderSettings & reader_settings,
2019-10-16 18:27:53 +00:00
const ValueSizeMap & avg_value_size_hints,
2020-01-17 12:24:27 +00:00
const ReadBufferFromFileBase::ProfileCallback & profile_callback) const
2019-10-16 18:27:53 +00:00
{
2022-09-05 16:55:00 +00:00
auto read_info = std::make_shared<LoadedMergeTreeDataPartInfoForReader>(shared_from_this());
2019-11-05 11:53:22 +00:00
return std::make_unique<MergeTreeReaderCompact>(
2022-09-05 16:55:00 +00:00
read_info, columns_to_read, metadata_snapshot, uncompressed_cache,
2020-01-17 12:24:27 +00:00
mark_cache, mark_ranges, reader_settings,
avg_value_size_hints, profile_callback);
2019-10-16 18:27:53 +00:00
}
2019-10-21 15:33:59 +00:00
IMergeTreeDataPart::MergeTreeWriterPtr MergeTreeDataPartCompact::getWriter(
2022-04-12 18:59:49 +00:00
DataPartStorageBuilderPtr data_part_storage_builder,
2019-10-21 15:33:59 +00:00
const NamesAndTypesList & columns_list,
2020-06-17 12:39:20 +00:00
const StorageMetadataPtr & metadata_snapshot,
2019-11-07 11:11:38 +00:00
const std::vector<MergeTreeIndexPtr> & indices_to_recalc,
const CompressionCodecPtr & default_codec_,
const MergeTreeWriterSettings & writer_settings,
2019-11-07 11:11:38 +00:00
const MergeTreeIndexGranularity & computed_index_granularity) const
2019-10-21 15:33:59 +00:00
{
2019-12-16 14:51:19 +00:00
NamesAndTypesList ordered_columns_list;
std::copy_if(columns_list.begin(), columns_list.end(), std::back_inserter(ordered_columns_list),
2019-12-16 14:51:19 +00:00
[this](const auto & column) { return getColumnPosition(column.name) != std::nullopt; });
/// Order of writing is important in compact format
ordered_columns_list.sort([this](const auto & lhs, const auto & rhs)
{ return *getColumnPosition(lhs.name) < *getColumnPosition(rhs.name); });
return std::make_unique<MergeTreeDataPartWriterCompact>(
2022-04-12 18:59:49 +00:00
shared_from_this(), std::move(data_part_storage_builder), ordered_columns_list, metadata_snapshot,
2022-09-05 05:26:58 +00:00
indices_to_recalc, getMarksFileExtension(),
default_codec_, writer_settings, computed_index_granularity);
2019-10-21 15:33:59 +00:00
}
2020-03-23 12:19:43 +00:00
2020-06-29 20:36:18 +00:00
void MergeTreeDataPartCompact::calculateEachColumnSizes(ColumnSizeByName & /*each_columns_size*/, ColumnSize & total_size) const
{
auto bin_checksum = checksums.files.find(DATA_FILE_NAME_WITH_EXTENSION);
if (bin_checksum != checksums.files.end())
{
total_size.data_compressed += bin_checksum->second.file_size;
total_size.data_uncompressed += bin_checksum->second.uncompressed_size;
}
2019-11-21 16:10:22 +00:00
2022-09-05 05:26:58 +00:00
auto mrk_checksum = checksums.files.find(DATA_FILE_NAME + getMarksFileExtension());
if (mrk_checksum != checksums.files.end())
total_size.marks += mrk_checksum->second.file_size;
2019-10-16 18:27:53 +00:00
}
2022-09-05 16:55:00 +00:00
void MergeTreeDataPartCompact::loadIndexGranularityImpl(
MergeTreeIndexGranularity & index_granularity_, const MergeTreeIndexGranularityInfo & index_granularity_info_,
const NamesAndTypesList & columns_, const DataPartStoragePtr & data_part_storage_)
2019-10-16 18:27:53 +00:00
{
if (!index_granularity_info_.mark_type.adaptive)
throw Exception("MergeTreeDataPartCompact cannot be created with non-adaptive granulary.", ErrorCodes::NOT_IMPLEMENTED);
2019-10-31 14:44:17 +00:00
2022-09-05 16:55:00 +00:00
auto marks_file_path = index_granularity_info_.getMarksFilePath("data");
if (!data_part_storage_->exists(marks_file_path))
2022-04-07 17:44:49 +00:00
throw Exception(
ErrorCodes::NO_FILE_IN_DATA_PART,
"Marks file '{}' doesn't exist",
2022-09-05 16:55:00 +00:00
std::string(fs::path(data_part_storage_->getFullPath()) / marks_file_path));
2019-10-16 18:27:53 +00:00
2022-09-05 16:55:00 +00:00
size_t marks_file_size = data_part_storage_->getFileSize(marks_file_path);
2019-10-16 18:27:53 +00:00
2022-09-05 16:55:00 +00:00
auto buffer = data_part_storage_->readFile(marks_file_path, ReadSettings().adjustBufferSize(marks_file_size), marks_file_size, std::nullopt);
2020-02-28 17:14:55 +00:00
while (!buffer->eof())
2019-10-16 18:27:53 +00:00
{
2019-12-27 21:17:53 +00:00
/// Skip offsets for columns
2022-09-05 16:55:00 +00:00
buffer->seek(columns_.size() * sizeof(MarkInCompressedFile), SEEK_CUR);
2019-10-16 18:27:53 +00:00
size_t granularity;
2020-02-28 17:14:55 +00:00
readIntBinary(granularity, *buffer);
2022-09-05 16:55:00 +00:00
index_granularity_.appendMark(granularity);
2019-10-16 18:27:53 +00:00
}
2019-10-31 14:44:17 +00:00
2022-09-05 16:55:00 +00:00
if (index_granularity_.getMarksCount() * index_granularity_info_.getMarkSizeInBytes(columns_.size()) != marks_file_size)
2019-10-16 18:27:53 +00:00
throw Exception("Cannot read all marks from file " + marks_file_path, ErrorCodes::CANNOT_READ_ALL_DATA);
2022-09-05 16:55:00 +00:00
index_granularity_.setInitialized();
}
void MergeTreeDataPartCompact::loadIndexGranularity()
{
if (columns.empty())
throw Exception("No columns in part " + name, ErrorCodes::NO_FILE_IN_DATA_PART);
loadIndexGranularityImpl(index_granularity, index_granularity_info, columns, data_part_storage);
2019-10-16 18:27:53 +00:00
}
bool MergeTreeDataPartCompact::hasColumnFiles(const NameAndTypePair & column) const
2019-12-12 18:55:19 +00:00
{
2021-11-02 17:44:15 +00:00
if (!getColumnPosition(column.getNameInStorage()))
2019-12-12 18:55:19 +00:00
return false;
2020-01-13 14:53:32 +00:00
auto bin_checksum = checksums.files.find(DATA_FILE_NAME_WITH_EXTENSION);
2022-09-05 05:26:58 +00:00
auto mrk_checksum = checksums.files.find(DATA_FILE_NAME + getMarksFileExtension());
2019-12-12 18:55:19 +00:00
return (bin_checksum != checksums.files.end() && mrk_checksum != checksums.files.end());
}
2020-01-16 16:15:01 +00:00
void MergeTreeDataPartCompact::checkConsistency(bool require_part_metadata) const
2019-10-16 18:27:53 +00:00
{
checkConsistencyBase();
2022-09-05 05:26:58 +00:00
String mrk_file_name = DATA_FILE_NAME + getMarksFileExtension();
if (!checksums.empty())
{
/// count.txt should be present even in non custom-partitioned parts
if (!checksums.files.contains("count.txt"))
throw Exception("No checksum for count.txt", ErrorCodes::NO_FILE_IN_DATA_PART);
2020-01-16 16:15:01 +00:00
if (require_part_metadata)
{
if (!checksums.files.contains(mrk_file_name))
2022-04-07 17:44:49 +00:00
throw Exception(
ErrorCodes::NO_FILE_IN_DATA_PART,
"No marks file checksum for column in part {}",
data_part_storage->getFullPath());
if (!checksums.files.contains(DATA_FILE_NAME_WITH_EXTENSION))
2022-04-07 17:44:49 +00:00
throw Exception(
ErrorCodes::NO_FILE_IN_DATA_PART,
"No data file checksum for in part {}",
data_part_storage->getFullPath());
}
}
else
{
{
/// count.txt should be present even in non custom-partitioned parts
2022-04-07 17:44:49 +00:00
std::string file_path = "count.txt";
if (!data_part_storage->exists(file_path) || data_part_storage->getFileSize(file_path) == 0)
throw Exception(
ErrorCodes::BAD_SIZE_OF_FILE_IN_DATA_PART,
"Part {} is broken: {} is empty",
2022-06-20 18:18:17 +00:00
data_part_storage->getRelativePath(),
2022-04-07 17:44:49 +00:00
std::string(fs::path(data_part_storage->getFullPath()) / file_path));
}
2020-01-16 16:15:01 +00:00
/// Check that marks are nonempty and have the consistent size with columns number.
2022-04-07 17:44:49 +00:00
if (data_part_storage->exists(mrk_file_name))
{
2022-04-07 17:44:49 +00:00
UInt64 file_size = data_part_storage->getFileSize(mrk_file_name);
if (!file_size)
2022-04-07 17:44:49 +00:00
throw Exception(
ErrorCodes::BAD_SIZE_OF_FILE_IN_DATA_PART,
"Part {} is broken: {} is empty.",
2022-06-20 18:18:17 +00:00
data_part_storage->getRelativePath(),
2022-04-07 17:44:49 +00:00
std::string(fs::path(data_part_storage->getFullPath()) / mrk_file_name));
2020-01-16 16:15:01 +00:00
UInt64 expected_file_size = index_granularity_info.getMarkSizeInBytes(columns.size()) * index_granularity.getMarksCount();
if (expected_file_size != file_size)
throw Exception(
2022-04-07 17:44:49 +00:00
ErrorCodes::BAD_SIZE_OF_FILE_IN_DATA_PART,
"Part {} is broken: bad size of marks file '{}': {}, must be: {}",
2022-06-20 18:18:17 +00:00
data_part_storage->getRelativePath(),
2022-04-07 17:44:49 +00:00
std::string(fs::path(data_part_storage->getFullPath()) / mrk_file_name),
std::to_string(file_size), std::to_string(expected_file_size));
}
}
2019-10-16 18:27:53 +00:00
}
bool MergeTreeDataPartCompact::isStoredOnRemoteDisk() const
{
2022-04-07 17:44:49 +00:00
return data_part_storage->isStoredOnRemoteDisk();
}
bool MergeTreeDataPartCompact::isStoredOnRemoteDiskWithZeroCopySupport() const
{
2022-05-03 15:48:05 +00:00
return data_part_storage->supportZeroCopyReplication();
}
2019-11-18 12:22:27 +00:00
MergeTreeDataPartCompact::~MergeTreeDataPartCompact()
{
removeIfNeeded();
}
2019-10-16 18:27:53 +00:00
}