2017-08-14 18:16:11 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-06-26 08:25:54 +00:00
|
|
|
#include <limits>
|
2021-08-24 12:57:49 +00:00
|
|
|
#include <optional>
|
2018-09-17 03:44:29 +00:00
|
|
|
#include <tuple>
|
2021-08-04 14:42:48 +00:00
|
|
|
#include <vector>
|
2021-08-24 12:57:49 +00:00
|
|
|
#include <array>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
|
|
|
#include <base/DayNum.h>
|
2017-08-25 20:41:45 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataFormatVersion.h>
|
2017-08-14 18:16:11 +00:00
|
|
|
|
2017-08-16 19:24:50 +00:00
|
|
|
|
2017-08-14 18:16:11 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Information about partition and the range of blocks contained in the part.
|
|
|
|
/// Allows determining if parts are disjoint or one part fully contains the other.
|
|
|
|
struct MergeTreePartInfo
|
|
|
|
{
|
|
|
|
String partition_id;
|
2017-08-16 19:24:50 +00:00
|
|
|
Int64 min_block = 0;
|
|
|
|
Int64 max_block = 0;
|
|
|
|
UInt32 level = 0;
|
2018-05-13 00:24:23 +00:00
|
|
|
Int64 mutation = 0; /// If the part has been mutated or contains mutated parts, is equal to mutation version number.
|
2017-08-16 19:24:50 +00:00
|
|
|
|
2021-05-14 12:55:30 +00:00
|
|
|
bool use_leagcy_max_level = false; /// For compatibility. TODO remove it
|
|
|
|
|
2017-08-16 19:24:50 +00:00
|
|
|
MergeTreePartInfo() = default;
|
2018-05-13 00:24:23 +00:00
|
|
|
|
2017-08-16 19:24:50 +00:00
|
|
|
MergeTreePartInfo(String partition_id_, Int64 min_block_, Int64 max_block_, UInt32 level_)
|
|
|
|
: partition_id(std::move(partition_id_)), min_block(min_block_), max_block(max_block_), level(level_)
|
|
|
|
{
|
|
|
|
}
|
2017-08-14 18:16:11 +00:00
|
|
|
|
2018-05-13 00:24:23 +00:00
|
|
|
MergeTreePartInfo(String partition_id_, Int64 min_block_, Int64 max_block_, UInt32 level_, Int64 mutation_)
|
|
|
|
: partition_id(std::move(partition_id_)), min_block(min_block_), max_block(max_block_), level(level_), mutation(mutation_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-14 18:16:11 +00:00
|
|
|
bool operator<(const MergeTreePartInfo & rhs) const
|
|
|
|
{
|
2018-05-13 00:24:23 +00:00
|
|
|
return std::forward_as_tuple(partition_id, min_block, max_block, level, mutation)
|
|
|
|
< std::forward_as_tuple(rhs.partition_id, rhs.min_block, rhs.max_block, rhs.level, rhs.mutation);
|
2017-08-14 18:16:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 15:31:43 +00:00
|
|
|
bool operator==(const MergeTreePartInfo & rhs) const
|
|
|
|
{
|
2018-05-10 15:01:10 +00:00
|
|
|
return !(*this != rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const MergeTreePartInfo & rhs) const
|
|
|
|
{
|
|
|
|
return *this < rhs || rhs < *this;
|
2018-02-19 15:31:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 00:24:23 +00:00
|
|
|
/// Get block number that can be used to determine which mutations we still need to apply to this part
|
|
|
|
/// (all mutations with version greater than this block number).
|
|
|
|
Int64 getDataVersion() const { return mutation ? mutation : min_block; }
|
|
|
|
|
|
|
|
/// True if contains rhs (this part is obtained by merging rhs with some other parts or mutating rhs)
|
2017-08-14 18:16:11 +00:00
|
|
|
bool contains(const MergeTreePartInfo & rhs) const
|
|
|
|
{
|
2021-10-11 18:42:46 +00:00
|
|
|
/// Containing part may have equal level iff block numbers are equal (unless level is MAX_LEVEL)
|
|
|
|
/// (e.g. all_0_5_2 does not contain all_0_4_2, but all_0_5_3 or all_0_4_2_9 do)
|
|
|
|
bool strictly_contains_block_range = (min_block == rhs.min_block && max_block == rhs.max_block) || level > rhs.level
|
|
|
|
|| level == MAX_LEVEL || level == LEGACY_MAX_LEVEL;
|
2017-08-14 18:16:11 +00:00
|
|
|
return partition_id == rhs.partition_id /// Parts for different partitions are not merged
|
|
|
|
&& min_block <= rhs.min_block
|
|
|
|
&& max_block >= rhs.max_block
|
2018-05-13 00:24:23 +00:00
|
|
|
&& level >= rhs.level
|
2021-10-11 18:42:46 +00:00
|
|
|
&& mutation >= rhs.mutation
|
|
|
|
&& strictly_contains_block_range;
|
2017-08-14 18:16:11 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 11:26:20 +00:00
|
|
|
/// Part was created with mutation of parent_candidate part
|
|
|
|
bool isMutationChildOf(const MergeTreePartInfo & parent_candidate) const
|
|
|
|
{
|
|
|
|
return partition_id == parent_candidate.partition_id
|
|
|
|
&& min_block == parent_candidate.min_block
|
|
|
|
&& max_block == parent_candidate.max_block
|
|
|
|
&& level == parent_candidate.level
|
|
|
|
&& mutation >= parent_candidate.mutation;
|
|
|
|
}
|
|
|
|
|
2021-08-18 08:56:18 +00:00
|
|
|
/// Return part mutation version, if part wasn't mutated return zero
|
|
|
|
Int64 getMutationVersion() const
|
|
|
|
{
|
2022-04-12 12:14:26 +00:00
|
|
|
return mutation;
|
2021-08-18 08:56:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 16:12:16 +00:00
|
|
|
/// True if parts do not intersect in any way.
|
|
|
|
bool isDisjoint(const MergeTreePartInfo & rhs) const
|
|
|
|
{
|
|
|
|
return partition_id != rhs.partition_id
|
|
|
|
|| min_block > rhs.max_block
|
|
|
|
|| max_block < rhs.min_block;
|
|
|
|
}
|
|
|
|
|
2021-05-30 21:29:37 +00:00
|
|
|
bool isFakeDropRangePart() const
|
|
|
|
{
|
|
|
|
/// Another max level was previously used for REPLACE/MOVE PARTITION
|
|
|
|
auto another_max_level = std::numeric_limits<decltype(level)>::max();
|
|
|
|
return level == MergeTreePartInfo::MAX_LEVEL || level == another_max_level;
|
|
|
|
}
|
|
|
|
|
2017-08-25 20:41:45 +00:00
|
|
|
String getPartName() const;
|
2018-05-25 13:29:15 +00:00
|
|
|
String getPartNameV0(DayNum left_date, DayNum right_date) const;
|
2018-02-21 17:06:29 +00:00
|
|
|
UInt64 getBlocksCount() const
|
|
|
|
{
|
|
|
|
return static_cast<UInt64>(max_block - min_block + 1);
|
|
|
|
}
|
2017-08-14 18:16:11 +00:00
|
|
|
|
2021-07-29 13:11:05 +00:00
|
|
|
/// Simple sanity check for partition ID. Checking that it's not too long or too short, doesn't contain a lot of '_'.
|
2021-08-06 18:03:38 +00:00
|
|
|
static void validatePartitionID(const String & partition_id, MergeTreeDataFormatVersion format_version);
|
2021-07-29 13:11:05 +00:00
|
|
|
|
2021-06-25 15:21:56 +00:00
|
|
|
static MergeTreePartInfo fromPartName(const String & part_name, MergeTreeDataFormatVersion format_version); // -V1071
|
2017-08-14 18:16:11 +00:00
|
|
|
|
2021-08-24 12:57:49 +00:00
|
|
|
static std::optional<MergeTreePartInfo> tryParsePartName(
|
|
|
|
std::string_view part_name, MergeTreeDataFormatVersion format_version);
|
2017-08-14 18:16:11 +00:00
|
|
|
|
2018-05-25 13:29:15 +00:00
|
|
|
static void parseMinMaxDatesFromPartName(const String & part_name, DayNum & min_date, DayNum & max_date);
|
2017-08-14 18:16:11 +00:00
|
|
|
|
2017-08-25 20:41:45 +00:00
|
|
|
static bool contains(const String & outer_part_name, const String & inner_part_name, MergeTreeDataFormatVersion format_version);
|
2018-06-04 11:23:47 +00:00
|
|
|
|
|
|
|
static constexpr UInt32 MAX_LEVEL = 999999999;
|
|
|
|
static constexpr UInt32 MAX_BLOCK_NUMBER = 999999999;
|
2021-05-14 12:55:30 +00:00
|
|
|
|
|
|
|
static constexpr UInt32 LEGACY_MAX_LEVEL = std::numeric_limits<decltype(level)>::max();
|
2017-08-14 18:16:11 +00:00
|
|
|
};
|
|
|
|
|
2021-11-24 19:45:10 +00:00
|
|
|
class IDisk;
|
|
|
|
using DiskPtr = std::shared_ptr<IDisk>;
|
|
|
|
|
2019-05-20 16:24:36 +00:00
|
|
|
/// Information about detached part, which includes its prefix in
|
|
|
|
/// addition to the above fields.
|
|
|
|
struct DetachedPartInfo : public MergeTreePartInfo
|
|
|
|
{
|
2019-07-26 20:04:45 +00:00
|
|
|
String dir_name;
|
2019-05-20 16:24:36 +00:00
|
|
|
String prefix;
|
2019-07-23 19:43:33 +00:00
|
|
|
|
2021-11-24 19:45:10 +00:00
|
|
|
DiskPtr disk;
|
2019-11-19 06:44:10 +00:00
|
|
|
|
2019-07-26 20:04:45 +00:00
|
|
|
/// If false, MergeTreePartInfo is in invalid state (directory name was not successfully parsed).
|
2019-07-23 19:43:33 +00:00
|
|
|
bool valid_name;
|
|
|
|
|
2021-08-25 19:18:12 +00:00
|
|
|
static constexpr auto DETACH_REASONS = std::to_array<std::string_view>({
|
2021-09-03 11:33:40 +00:00
|
|
|
"broken",
|
|
|
|
"unexpected",
|
|
|
|
"noquorum",
|
|
|
|
"ignored",
|
|
|
|
"broken-on-start",
|
|
|
|
"clone",
|
|
|
|
"attaching",
|
|
|
|
"deleting",
|
2021-10-18 20:16:02 +00:00
|
|
|
"tmp-fetch",
|
|
|
|
"covered-by-broken",
|
2021-08-24 12:57:49 +00:00
|
|
|
});
|
2021-08-04 14:42:48 +00:00
|
|
|
|
|
|
|
/// NOTE: It may parse part info incorrectly.
|
2021-08-24 12:57:49 +00:00
|
|
|
/// For example, if prefix contains '_' or if DETACH_REASONS doesn't contain prefix.
|
2021-09-03 11:33:40 +00:00
|
|
|
// This method has different semantics with MergeTreePartInfo::tryParsePartName.
|
|
|
|
// Detached parts are always parsed regardless of their validity.
|
|
|
|
// DetachedPartInfo::valid_name field specifies whether parsing was successful or not.
|
2021-11-24 19:45:10 +00:00
|
|
|
static DetachedPartInfo parseDetachedPartName(const DiskPtr & disk, std::string_view dir_name, MergeTreeDataFormatVersion format_version);
|
2021-08-24 12:57:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void addParsedPartInfo(const MergeTreePartInfo& part);
|
2019-05-20 16:24:36 +00:00
|
|
|
};
|
|
|
|
|
2019-07-31 14:44:55 +00:00
|
|
|
using DetachedPartsInfo = std::vector<DetachedPartInfo>;
|
|
|
|
|
2017-08-14 18:16:11 +00:00
|
|
|
}
|