ClickHouse/src/Storages/MergeTree/MergeTreeIndexGranularityInfo.cpp

96 lines
3.2 KiB
C++
Raw Normal View History

2019-06-19 10:07:56 +00:00
#include <Storages/MergeTree/MergeTreeIndexGranularityInfo.h>
2019-06-19 14:46:06 +00:00
#include <Storages/MergeTree/MergeTreeData.h>
2021-04-27 00:05:43 +00:00
namespace fs = std::filesystem;
2019-06-19 10:07:56 +00:00
namespace DB
{
2019-12-05 13:23:36 +00:00
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
2020-01-17 12:24:27 +00:00
extern const int UNKNOWN_PART_TYPE;
2019-12-05 13:23:36 +00:00
}
2022-04-05 19:12:48 +00:00
std::optional<std::string> MergeTreeIndexGranularityInfo::getMarksExtensionFromFilesystem(const DataPartStoragePtr & data_part_storage)
2019-06-19 10:07:56 +00:00
{
2022-04-05 19:12:48 +00:00
if (data_part_storage->exists())
2022-04-22 16:58:09 +00:00
for (auto it = data_part_storage->iterate(); it->isValid(); it->next())
2022-09-05 04:31:19 +00:00
if (it->isFile())
if (std::string ext = fs::path(it->name()).extension(); MarkType::isMarkFileExtension(ext))
return ext;
2019-06-19 10:07:56 +00:00
return {};
}
2020-01-14 13:23:51 +00:00
MergeTreeIndexGranularityInfo::MergeTreeIndexGranularityInfo(const MergeTreeData & storage, MergeTreeDataPartType type_)
: type(type_)
2019-11-21 16:10:22 +00:00
{
2019-10-16 18:27:53 +00:00
const auto storage_settings = storage.getSettings();
2019-08-13 10:29:31 +00:00
fixed_index_granularity = storage_settings->index_granularity;
compress_marks = storage_settings->compress_marks;
2019-11-21 16:10:22 +00:00
2019-06-19 10:07:56 +00:00
/// Granularity is fixed
2019-10-16 18:27:53 +00:00
if (!storage.canUseAdaptiveGranularity())
2019-11-21 16:10:22 +00:00
{
2022-03-14 14:42:09 +00:00
if (type != MergeTreeDataPartType::Wide)
2019-12-05 13:23:36 +00:00
throw Exception("Only Wide parts can be used with non-adaptive granularity.", ErrorCodes::NOT_IMPLEMENTED);
2019-06-19 10:07:56 +00:00
setNonAdaptive();
2019-11-21 16:10:22 +00:00
}
2019-06-19 10:07:56 +00:00
else
2020-01-14 13:23:51 +00:00
setAdaptive(storage_settings->index_granularity_bytes);
}
2019-06-19 10:07:56 +00:00
2022-04-05 19:12:48 +00:00
void MergeTreeIndexGranularityInfo::changeGranularityIfRequired(const DataPartStoragePtr & data_part_storage)
2019-06-19 10:07:56 +00:00
{
2022-04-05 19:12:48 +00:00
auto mrk_ext = getMarksExtensionFromFilesystem(data_part_storage);
if (mrk_ext && *mrk_ext == getNonAdaptiveMrkExtension(compress_marks))
2019-06-19 10:07:56 +00:00
setNonAdaptive();
}
2020-01-14 13:23:51 +00:00
void MergeTreeIndexGranularityInfo::setAdaptive(size_t index_granularity_bytes_)
2019-06-19 10:07:56 +00:00
{
2019-06-19 11:59:08 +00:00
is_adaptive = true;
marks_file_extension = getAdaptiveMrkExtension(type, compress_marks);
2019-06-19 10:07:56 +00:00
index_granularity_bytes = index_granularity_bytes_;
}
void MergeTreeIndexGranularityInfo::setNonAdaptive()
{
2019-06-19 11:59:08 +00:00
is_adaptive = false;
marks_file_extension = getNonAdaptiveMrkExtension(compress_marks);
2019-06-19 10:07:56 +00:00
index_granularity_bytes = 0;
}
2020-01-14 13:23:51 +00:00
size_t MergeTreeIndexGranularityInfo::getMarkSizeInBytes(size_t columns_num) const
{
2022-03-14 14:42:09 +00:00
if (type == MergeTreeDataPartType::Wide)
2020-01-14 13:23:51 +00:00
return is_adaptive ? getAdaptiveMrkSizeWide() : getNonAdaptiveMrkSizeWide();
2022-03-14 14:42:09 +00:00
else if (type == MergeTreeDataPartType::Compact)
2020-02-03 12:08:40 +00:00
return getAdaptiveMrkSizeCompact(columns_num);
2022-03-14 14:42:09 +00:00
else if (type == MergeTreeDataPartType::InMemory)
2020-04-14 19:47:19 +00:00
return 0;
2020-01-14 13:23:51 +00:00
else
throw Exception("Unknown part type", ErrorCodes::UNKNOWN_PART_TYPE);
}
2020-02-03 12:08:40 +00:00
size_t getAdaptiveMrkSizeCompact(size_t columns_num)
{
/// Each mark contains number of rows in granule and two offsets for every column.
return sizeof(UInt64) * (columns_num * 2 + 1);
}
std::string getAdaptiveMrkExtension(MergeTreeDataPartType part_type, bool compress_marks)
{
2022-03-14 14:42:09 +00:00
if (part_type == MergeTreeDataPartType::Wide)
return compress_marks ? ".cmrk2" : ".mrk2";
2022-03-14 14:42:09 +00:00
else if (part_type == MergeTreeDataPartType::Compact)
return compress_marks ? ".cmrk3" : ".mrk3";
2022-03-14 14:42:09 +00:00
else if (part_type == MergeTreeDataPartType::InMemory)
2020-04-14 19:47:19 +00:00
return "";
2020-01-14 13:23:51 +00:00
else
throw Exception("Unknown part type", ErrorCodes::UNKNOWN_PART_TYPE);
}
2019-06-19 10:07:56 +00:00
}