forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionsMergeTreePartInfo.cpp
More file actions
269 lines (222 loc) · 9.28 KB
/
FunctionsMergeTreePartInfo.cpp
File metadata and controls
269 lines (222 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <Storages/MergeTree/MergeTreePartInfo.h>
#include <Functions/IFunction.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/FunctionFactory.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnVector.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnTuple.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeLowCardinality.h>
#include <DataTypes/DataTypeTuple.h>
#include <Common/register_objects.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
namespace
{
struct UnpackedPartSegments
{
MergeTreePartInfo part_info;
std::string prefix;
std::string suffix;
};
std::optional<MergeTreePartInfo> tryParseMergeTreePartInfo(std::string_view data)
{
return MergeTreePartInfo::tryParsePartName(data, MERGE_TREE_DATA_MIN_FORMAT_VERSION_WITH_CUSTOM_PARTITIONING);
}
[[noreturn]] void throwInvalidPartName(std::string_view data)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Invalid MergeTree part name: '{}'.", data);
}
MergeTreePartInfo constructPartInfo(std::string_view data)
{
if (auto info = tryParseMergeTreePartInfo(data))
return std::move(info.value());
throwInvalidPartName(data);
}
/// Tries to parse part name format: "<prefix>_<part_name>_<tryN>".
UnpackedPartSegments unpackPartName(std::string_view data)
{
UnpackedPartSegments unpacked;
/// prefix_name_tryN
/// ^
size_t right = data.size();
size_t first_delimiter = data.find_first_of('_');
size_t last_delimiter = data.find_last_of('_');
size_t number_of_segments = std::ranges::count(data, '_') + 1;
if (number_of_segments < 4)
throwInvalidPartName(data);
/// Process suffix first because it can be easily determined.
if (data.substr(last_delimiter + 1).starts_with("try"))
{
unpacked.suffix = data.substr(last_delimiter + 1);
right = last_delimiter;
number_of_segments -= 1;
}
switch (number_of_segments)
{
case 6: /// prefix_partition_min_max_level_mutation
{
unpacked.prefix = data.substr(0, first_delimiter);
unpacked.part_info = constructPartInfo(data.substr(first_delimiter + 1, right - (first_delimiter + 1)));
break;
}
case 5: /// prefix_partition_min_max_level or partition_min_max_level_mutation
{
/// Still incorrect if partition can be parsed as number and will be < min
if (auto info = tryParseMergeTreePartInfo(data.substr(0, right)))
{
unpacked.prefix = "";
unpacked.part_info = std::move(info.value());
break;
}
if (auto info = tryParseMergeTreePartInfo(data.substr(first_delimiter + 1, right - (first_delimiter + 1))))
{
unpacked.prefix = data.substr(0, first_delimiter);
unpacked.part_info = std::move(info.value());
break;
}
throwInvalidPartName(data);
}
case 4: /// partition_min_max_level
{
unpacked.part_info = constructPartInfo(data.substr(0, right));
break;
}
default:
throwInvalidPartName(data);
}
return unpacked;
}
bool isAnyStringType(const IDataType & data_type)
{
return isStringOrFixedString(removeLowCardinality(data_type.getPtr()));
}
class FunctionMergeTreePartCoverage : public IFunction
{
static MergeTreePartInfo constructCoveringPart(const ColumnPtr & covering_column, size_t row_number)
{
if (isColumnConst(*covering_column))
return unpackPartName(covering_column->getDataAt(0).toView()).part_info;
return unpackPartName(covering_column->getDataAt(row_number).toView()).part_info;
}
public:
static constexpr auto name = "isMergeTreePartCoveredBy";
String getName() const override { return name; }
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionMergeTreePartCoverage>(); }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
bool useDefaultImplementationForSparseColumns() const override { return false; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo &) const override { return false; }
size_t getNumberOfArguments() const override { return 2; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
validateFunctionArguments(*this, arguments, FunctionArgumentDescriptors{
{"nested_part", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isAnyStringType), nullptr, "String or FixedString or LowCardinality String"},
{"covering_part", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isAnyStringType), nullptr, "String or FixedString or LowCardinality String"}
});
return std::make_shared<DataTypeUInt8>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
auto result_column = ColumnUInt8::create();
const ColumnPtr & input_column = arguments.front().column;
for (size_t i = 0; i < input_rows_count; ++i)
{
const auto part_info = unpackPartName(input_column->getDataAt(i).toView()).part_info;
const auto covering_part = constructCoveringPart(arguments[1].column, i);
result_column->insertValue(covering_part.contains(part_info));
}
return result_column;
}
};
class FunctionMergeTreePartInfo : public IFunction
{
public:
static constexpr auto name = "mergeTreePartInfo";
String getName() const override { return name; }
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionMergeTreePartInfo>(); }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
bool useDefaultImplementationForSparseColumns() const override { return false; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo &) const override { return false; }
size_t getNumberOfArguments() const override { return 1; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
validateFunctionArguments(*this, arguments, FunctionArgumentDescriptors{
{"part_name", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isAnyStringType), nullptr, "String or FixedString or LowCardinality String"}
});
DataTypes types = {
std::make_shared<DataTypeString>(),
std::make_shared<DataTypeString>(),
std::make_shared<DataTypeString>(),
std::make_shared<DataTypeInt64>(),
std::make_shared<DataTypeInt64>(),
std::make_shared<DataTypeInt64>(),
std::make_shared<DataTypeInt64>(),
};
Names names = {
"partition_id",
"prefix",
"suffix",
"min_block",
"max_block",
"level",
"mutation",
};
return std::make_shared<DataTypeTuple>(std::move(types), std::move(names));
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const ColumnPtr & input_column = arguments[0].column;
auto partition_column = ColumnString::create();
auto prefix_column = ColumnString::create();
auto suffix_column = ColumnString::create();
auto min_block_column = ColumnInt64::create();
auto max_block_column = ColumnInt64::create();
auto level_column = ColumnInt64::create();
auto mutation_column = ColumnInt64::create();
for (size_t i = 0; i < input_rows_count; ++i)
{
const auto [part_info, prefix, suffix] = unpackPartName(input_column->getDataAt(i).toView());
partition_column->insertData(part_info.getPartitionId().data(), part_info.getPartitionId().size());
prefix_column->insertData(prefix.data(), prefix.size());
suffix_column->insertData(suffix.data(), suffix.size());
min_block_column->insertValue(part_info.min_block);
max_block_column->insertValue(part_info.max_block);
level_column->insertValue(part_info.level);
mutation_column->insertValue(part_info.mutation);
}
return ColumnTuple::create(Columns{
std::move(partition_column),
std::move(prefix_column),
std::move(suffix_column),
std::move(min_block_column),
std::move(max_block_column),
std::move(level_column),
std::move(mutation_column),
});
}
};
}
REGISTER_FUNCTION(MergeTreePartInfoTools)
{
factory.registerFunction<FunctionMergeTreePartCoverage>(
FunctionDocumentation{
.description = "Checks if one MergeTree part covers another",
.introduced_in = {25, 6},
.category = FunctionDocumentation::Category::Introspection,
},
FunctionFactory::Case::Insensitive);
factory.registerFunction<FunctionMergeTreePartInfo>(
FunctionDocumentation{
.description = "Represents String value as a MergeTreePartInfo structure",
.introduced_in = {25, 6},
.category = FunctionDocumentation::Category::Introspection,
},
FunctionFactory::Case::Insensitive);
}
}