ClickHouse/src/Storages/MergeTree/MergeTreeIndexGranularityInfo.cpp

100 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>
2019-06-19 10:07:56 +00:00
#include <Poco/Path.h>
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
}
2020-03-10 14:39:30 +00:00
std::optional<std::string> MergeTreeIndexGranularityInfo::getMarksExtensionFromFilesystem(const DiskPtr & disk, const String & path_to_part)
2019-06-19 10:07:56 +00:00
{
if (disk->exists(path_to_part))
2019-06-19 10:07:56 +00:00
{
for (DiskDirectoryIteratorPtr it = disk->iterateDirectory(path_to_part); it->isValid(); it->next())
2019-06-19 10:07:56 +00:00
{
Poco::Path path(it->path());
const auto & ext = "." + path.getExtension();
if (ext == getNonAdaptiveMrkExtension()
2019-11-21 16:10:22 +00:00
|| ext == getAdaptiveMrkExtension(MergeTreeDataPartType::WIDE)
|| ext == getAdaptiveMrkExtension(MergeTreeDataPartType::COMPACT))
2019-06-19 10:07:56 +00:00
return ext;
}
}
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;
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
{
2020-01-14 13:23:51 +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
void MergeTreeIndexGranularityInfo::changeGranularityIfRequired(const DiskPtr & disk, const String & path_to_part)
2019-06-19 10:07:56 +00:00
{
2020-03-10 14:39:30 +00:00
auto mrk_ext = getMarksExtensionFromFilesystem(disk, path_to_part);
2019-11-21 16:10:22 +00:00
if (mrk_ext && *mrk_ext == getNonAdaptiveMrkExtension())
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;
2020-01-14 13:23:51 +00:00
marks_file_extension = getAdaptiveMrkExtension(type);
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;
2019-11-21 16:10:22 +00:00
marks_file_extension = getNonAdaptiveMrkExtension();
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
{
2020-01-14 13:23:51 +00:00
if (type == MergeTreeDataPartType::WIDE)
return is_adaptive ? getAdaptiveMrkSizeWide() : getNonAdaptiveMrkSizeWide();
else if (type == MergeTreeDataPartType::COMPACT)
2020-02-03 12:08:40 +00:00
return getAdaptiveMrkSizeCompact(columns_num);
2020-04-14 19:47:19 +00:00
else if (type == MergeTreeDataPartType::IN_MEMORY)
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);
}
2020-01-14 13:23:51 +00:00
std::string getAdaptiveMrkExtension(MergeTreeDataPartType part_type)
{
2020-01-14 13:23:51 +00:00
if (part_type == MergeTreeDataPartType::WIDE)
return ".mrk2";
else if (part_type == MergeTreeDataPartType::COMPACT)
return ".mrk3";
2020-04-14 19:47:19 +00:00
else if (part_type == MergeTreeDataPartType::IN_MEMORY)
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
}