ClickHouse/src/Storages/MergeTree/MergeTreeIndexGranularityInfo.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
4.5 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
{
2022-09-05 05:26:58 +00:00
extern const int LOGICAL_ERROR;
2020-01-17 12:24:27 +00:00
extern const int UNKNOWN_PART_TYPE;
2022-09-05 05:26:58 +00:00
extern const int INCORRECT_FILE_NAME;
2019-12-05 13:23:36 +00:00
}
2022-09-05 05:26:58 +00:00
MarkType::MarkType(std::string_view extension)
{
if (extension.starts_with('.'))
extension = extension.substr(1);
if (extension.starts_with('c'))
{
compressed = true;
extension = extension.substr(1);
}
if (!extension.starts_with("mrk"))
2022-09-05 05:31:29 +00:00
throw Exception(ErrorCodes::INCORRECT_FILE_NAME, "Mark file extension does not start with .mrk or .cmrk: {}", extension);
2022-09-05 05:26:58 +00:00
extension = extension.substr(strlen("mrk"));
if (extension.empty())
{
adaptive = false;
part_type = MergeTreeDataPartType::Wide;
}
else if (extension == "2")
{
adaptive = true;
part_type = MergeTreeDataPartType::Wide;
}
else if (extension == "3")
{
adaptive = true;
part_type = MergeTreeDataPartType::Compact;
}
else
throw Exception(ErrorCodes::INCORRECT_FILE_NAME, "Unknown mark file extension: '{}'", extension);
}
MarkType::MarkType(bool adaptive_, bool compressed_, MergeTreeDataPartType::Value part_type_)
: adaptive(adaptive_), compressed(compressed_), part_type(part_type_)
{
if (!adaptive && part_type != MergeTreeDataPartType::Wide)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Logical error: non-Wide data part type with non-adaptive granularity");
if (part_type == MergeTreeDataPartType::Unknown)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Logical error: unknown data part type");
}
bool MarkType::isMarkFileExtension(std::string_view extension)
{
return extension.find("mrk") != std::string_view::npos;
}
std::string MarkType::getFileExtension() const
{
std::string res = compressed ? ".cmrk" : ".mrk";
if (!adaptive)
{
if (part_type != MergeTreeDataPartType::Wide)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Logical error: non-Wide data part type with non-adaptive granularity");
return res;
}
switch (part_type)
{
case MergeTreeDataPartType::Wide:
return res + "2";
case MergeTreeDataPartType::Compact:
return res + "3";
case MergeTreeDataPartType::InMemory:
return "";
case MergeTreeDataPartType::Unknown:
throw Exception(ErrorCodes::LOGICAL_ERROR, "Logical error: unknown data part type");
}
}
std::optional<std::string> MergeTreeIndexGranularityInfo::getMarksExtensionFromFilesystem(const IDataPartStorage & data_part_storage)
2019-06-19 10:07:56 +00:00
{
if (data_part_storage.exists())
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_)
2022-09-06 15:41:39 +00:00
: MergeTreeIndexGranularityInfo(storage, {storage.canUseAdaptiveGranularity(), storage.getSettings()->compress_marks, type_.getValue()})
{
}
MergeTreeIndexGranularityInfo::MergeTreeIndexGranularityInfo(const MergeTreeData & storage, MarkType mark_type_)
: mark_type(mark_type_)
2019-11-21 16:10:22 +00:00
{
2022-09-05 05:26:58 +00:00
fixed_index_granularity = storage.getSettings()->index_granularity;
}
2019-06-19 10:07:56 +00:00
void MergeTreeIndexGranularityInfo::changeGranularityIfRequired(const IDataPartStorage & 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);
2022-09-05 05:26:58 +00:00
if (mrk_ext && !MarkType(*mrk_ext).adaptive)
{
mark_type.adaptive = false;
index_granularity_bytes = 0;
}
2019-06-19 10:07:56 +00:00
}
2020-01-14 13:23:51 +00:00
size_t MergeTreeIndexGranularityInfo::getMarkSizeInBytes(size_t columns_num) const
{
2022-09-05 05:26:58 +00:00
if (mark_type.part_type == MergeTreeDataPartType::Wide)
return mark_type.adaptive ? getAdaptiveMrkSizeWide() : getNonAdaptiveMrkSizeWide();
else if (mark_type.part_type == MergeTreeDataPartType::Compact)
2020-02-03 12:08:40 +00:00
return getAdaptiveMrkSizeCompact(columns_num);
2022-09-05 05:26:58 +00:00
else if (mark_type.part_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);
}
2019-06-19 10:07:56 +00:00
}