forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathManifestFile.cpp
More file actions
751 lines (667 loc) · 30.4 KB
/
ManifestFile.cpp
File metadata and controls
751 lines (667 loc) · 30.4 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#include <format>
#include "config.h"
#if USE_AVRO
#include <compare>
#include <optional>
#include <Interpreters/Context.h>
#include <Interpreters/IcebergMetadataLog.h>
#include <Storages/ObjectStorage/DataLakes/Iceberg/Constant.h>
#include <Storages/ObjectStorage/DataLakes/Iceberg/ManifestFile.h>
#include <Storages/ObjectStorage/DataLakes/Iceberg/ManifestFilesPruning.h>
#include <Storages/ObjectStorage/DataLakes/Iceberg/PositionDeleteTransform.h>
#include <Storages/ObjectStorage/DataLakes/Iceberg/Utils.h>
#include <Core/Settings.h>
#include <Core/TypeId.h>
#include <DataTypes/DataTypesDecimal.h>
#include <Poco/JSON/Parser.h>
#include <Storages/ColumnsDescription.h>
#include <Parsers/ASTFunction.h>
#include <Common/quoteString.h>
#include <DataTypes/DataTypeNullable.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <Common/logger_useful.h>
namespace DB::ErrorCodes
{
extern const int ICEBERG_SPECIFICATION_VIOLATION;
extern const int LOGICAL_ERROR;
extern const int BAD_ARGUMENTS;
}
namespace DB::Setting
{
extern const SettingsTimezone iceberg_partition_timezone;
}
namespace DB::Iceberg
{
String FileContentTypeToString(FileContentType type)
{
switch (type)
{
case FileContentType::DATA:
return "data";
case FileContentType::POSITION_DELETE:
return "position_deletes";
case FileContentType::EQUALITY_DELETE:
return "equality_deletes";
}
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Unsupported content type: {}", static_cast<int>(type));
}
namespace
{
/// Iceberg stores lower_bounds and upper_bounds serialized with some custom deserialization as bytes array
/// https://iceberg.apache.org/spec/#appendix-d-single-value-serialization
std::optional<DB::Field> deserializeFieldFromBinaryRepr(std::string str, DB::DataTypePtr expected_type, bool lower_bound)
{
auto non_nullable_type = DB::removeNullable(expected_type);
auto column = non_nullable_type->createColumn();
if (DB::WhichDataType(non_nullable_type).isDecimal())
{
/// Iceberg store decimal values as unscaled value with two’s-complement big-endian binary
/// using the minimum number of bytes for the value
/// Our decimal binary representation is little endian
/// so we cannot reuse our default code for parsing it.
int64_t unscaled_value = 0;
// Convert from big-endian to signed int
for (const auto byte : str)
unscaled_value = (unscaled_value << 8) | static_cast<uint8_t>(byte);
/// Add sign
if (str[0] & 0x80)
{
int64_t sign_extension = -1;
sign_extension <<= (str.size() * 8);
unscaled_value |= sign_extension;
}
/// NOTE: It's very weird, but Decimal values for lower bound and upper bound
/// are stored rounded, without fractional part. What is more strange
/// the integer part is rounded mathematically correctly according to fractional part.
/// Example: 17.22 -> 17, 8888.999 -> 8889, 1423.77 -> 1424.
/// I've checked two implementations: Spark and Amazon Athena and both of them
/// do this.
///
/// The problem is -- we cannot use rounded values for lower bounds and upper bounds.
/// Example: upper_bound(x) = 17.22, but it's rounded 17.00, now condition WHERE x >= 17.21 will
/// check rounded value and say: "Oh largest value is 17, so values bigger than 17.21 cannot be in this file,
/// let's skip it". But it will produce incorrect result since actual value (17.22 >= 17.21) is stored in this file.
///
/// To handle this issue we subtract 1 from the integral part for lower_bound and add 1 to integral
/// part of upper_bound. This produces: 17.22 -> [16.0, 18.0]. So this is more rough boundary,
/// but at least it doesn't lead to incorrect results.
if (int32_t scale = DB::getDecimalScale(*non_nullable_type))
{
int64_t scaler = lower_bound ? -10 : 10;
while (--scale)
scaler *= 10;
unscaled_value += scaler;
}
if (const auto * decimal_type = DB::checkDecimal<DB::Decimal32>(*non_nullable_type))
{
DB::DecimalField<DB::Decimal32> result(static_cast<Int32>(unscaled_value), decimal_type->getScale());
return result;
}
if (const auto * decimal_type = DB::checkDecimal<DB::Decimal64>(*non_nullable_type))
{
DB::DecimalField<DB::Decimal64> result(unscaled_value, decimal_type->getScale());
return result;
}
else
{
return std::nullopt;
}
}
else
{
/// For all other types except decimal binary representation
/// matches our internal representation
column->insertData(str.data(), str.length());
DB::Field result;
column->get(0, result);
return result;
}
}
}
const std::vector<ManifestFileEntryPtr> & ManifestFileContent::getFilesWithoutDeleted(FileContentType content_type) const
{
if (content_type == FileContentType::DATA)
return data_files_without_deleted;
else if (content_type == FileContentType::POSITION_DELETE)
return position_deletes_files_without_deleted;
else
return equality_deletes_files;
}
using namespace DB;
ManifestFileContent::ManifestFileContent(
const AvroForIcebergDeserializer & manifest_file_deserializer,
const String & manifest_file_name,
Int32 format_version_,
const String & common_path,
IcebergSchemaProcessor & schema_processor,
Int64 inherited_sequence_number,
Int64 inherited_snapshot_id,
const String & table_location,
DB::ContextPtr context,
const String & path_to_manifest_file_)
: path_to_manifest_file(path_to_manifest_file_)
{
insertRowToLogTable(
context,
manifest_file_deserializer.getMetadataContent(),
DB::IcebergMetadataLogLevel::ManifestFileMetadata,
common_path,
path_to_manifest_file,
std::nullopt,
std::nullopt);
for (const auto & column_name : {f_status, f_data_file})
{
if (!manifest_file_deserializer.hasPath(column_name))
throw Exception(
DB::ErrorCodes::ICEBERG_SPECIFICATION_VIOLATION, "Required columns are not found in manifest file: {}", column_name);
}
if (format_version_ > 1 && !manifest_file_deserializer.hasPath(f_sequence_number))
throw Exception(
ErrorCodes::ICEBERG_SPECIFICATION_VIOLATION, "Required columns are not found in manifest file: {}", f_sequence_number);
Poco::JSON::Parser parser;
auto partition_spec_json_string = manifest_file_deserializer.tryGetAvroMetadataValue("partition-spec");
if (!partition_spec_json_string.has_value())
throw Exception(ErrorCodes::ICEBERG_SPECIFICATION_VIOLATION, "No partition-spec in iceberg manifest file");
Poco::Dynamic::Var partition_spec_json = parser.parse(*partition_spec_json_string);
const Poco::JSON::Array::Ptr & partition_specification = partition_spec_json.extract<Poco::JSON::Array::Ptr>();
DB::NamesAndTypesList partition_columns_description;
auto partition_key_ast = make_intrusive<ASTFunction>();
partition_key_ast->name = "tuple";
partition_key_ast->arguments = make_intrusive<DB::ASTExpressionList>();
partition_key_ast->children.push_back(partition_key_ast->arguments);
auto schema_json_string = manifest_file_deserializer.tryGetAvroMetadataValue(f_schema);
if (!schema_json_string.has_value())
throw Exception(
ErrorCodes::BAD_ARGUMENTS,
"Cannot read Iceberg table: manifest file '{}' doesn't have field '{}' in its metadata",
manifest_file_name,
f_schema);
Poco::Dynamic::Var json = parser.parse(*schema_json_string);
const Poco::JSON::Object::Ptr & schema_object = json.extract<Poco::JSON::Object::Ptr>();
Int32 manifest_schema_id = schema_object->getValue<int>(f_schema_id);
schema_processor.addIcebergTableSchema(schema_object);
for (size_t i = 0; i != partition_specification->size(); ++i)
{
auto partition_specification_field = partition_specification->getObject(static_cast<UInt32>(i));
auto source_id = partition_specification_field->getValue<Int32>(f_source_id);
/// NOTE: tricky part to support RENAME column in partition key. Instead of some name
/// we use column internal number as it's name.
auto numeric_column_name = DB::backQuote(DB::toString(source_id));
std::optional<DB::NameAndTypePair> manifest_file_column_characteristics = schema_processor.tryGetFieldCharacteristics(manifest_schema_id, source_id);
if (!manifest_file_column_characteristics.has_value())
continue;
auto transform_name = partition_specification_field->getValue<String>(f_partition_transform);
auto partition_name = partition_specification_field->getValue<String>(f_partition_name);
common_partition_specification.emplace_back(source_id, transform_name, partition_name);
auto partition_ast = getASTFromTransform(transform_name, numeric_column_name, context->getSettingsRef()[Setting::iceberg_partition_timezone]);
/// Unsupported partition key expression
if (partition_ast == nullptr)
continue;
partition_key_ast->as<ASTFunction>()->arguments->children.emplace_back(std::move(partition_ast));
partition_columns_description.emplace_back(numeric_column_name, removeNullable(manifest_file_column_characteristics->type));
}
if (!partition_columns_description.empty())
this->partition_key_description.emplace(DB::KeyDescription::getKeyFromAST(std::move(partition_key_ast), ColumnsDescription(partition_columns_description), context));
for (size_t i = 0; i < manifest_file_deserializer.rows(); ++i)
{
insertRowToLogTable(
context,
manifest_file_deserializer.getContent(i),
DB::IcebergMetadataLogLevel::ManifestFileEntry,
common_path,
path_to_manifest_file,
i,
std::nullopt);
FileContentType content_type = FileContentType::DATA;
if (format_version_ > 1)
content_type = FileContentType(manifest_file_deserializer.getValueFromRowByName(i, c_data_file_content, TypeIndex::Int32).safeGet<UInt64>());
const auto status = ManifestEntryStatus(manifest_file_deserializer.getValueFromRowByName(i, f_status, TypeIndex::Int32).safeGet<UInt64>());
if (status == ManifestEntryStatus::DELETED)
continue;
const auto snapshot_id_value = manifest_file_deserializer.getValueFromRowByName(i, f_snapshot_id);
Int64 snapshot_id;
if (snapshot_id_value.isNull())
{
if (status == ManifestEntryStatus::EXISTING)
{
throw Exception(
ErrorCodes::ICEBERG_SPECIFICATION_VIOLATION,
"Cannot read Iceberg table: manifest file '{}' has entry with status 'EXISTING' without snapshot id",
manifest_file_name);
}
snapshot_id = inherited_snapshot_id;
}
else
{
snapshot_id = snapshot_id_value.safeGet<Int64>();
}
const auto schema_id_opt = schema_processor.tryGetSchemaIdForSnapshot(snapshot_id);
if (!schema_id_opt.has_value())
{
/// Error logged but not thrown to avoid breaking whole query because of backward compatibility reasons.
/// That's actually an error because it can lead to incorrect query results, so we are creating an exception to put it to system.error_log.
try
{
throw Exception(
ErrorCodes::ICEBERG_SPECIFICATION_VIOLATION,
"Cannot read Iceberg table: manifest file '{}' has entry with snapshot_id '{}' for which write file schema is unknown",
manifest_file_name,
snapshot_id);
}
catch (const Exception &)
{
tryLogCurrentException("ICEBERG_SPECIFICATION_VIOLATION", "", LogsLevel::error);
}
}
const auto schema_id = schema_id_opt.has_value() ? schema_id_opt.value() : manifest_schema_id;
const auto file_path_key
= manifest_file_deserializer.getValueFromRowByName(i, c_data_file_file_path, TypeIndex::String).safeGet<String>();
const auto file_path = getProperFilePathFromMetadataInfo(manifest_file_deserializer.getValueFromRowByName(i, c_data_file_file_path, TypeIndex::String).safeGet<String>(), common_path, table_location);
/// NOTE: This is weird, because in manifest file partition looks like this:
/// {
/// ...
/// "data_file": {
/// "partition": {
/// "total_amount_trunc": {
/// "decimal_10_2": "\u0000\u0000\u0000\u0013<U+0086>"
/// }
/// },
/// ....
/// However, somehow parser ignores all these nested keys like "total_amount_trunc" or "decimal_10_2" and
/// directly returns tuple of partition values. However it's exactly what we need.
Field partition_value = manifest_file_deserializer.getValueFromRowByName(i, c_data_file_partition);
auto tuple = partition_value.safeGet<Tuple>();
DB::Row partition_key_value;
for (const auto & value : tuple)
partition_key_value.emplace_back(value);
std::unordered_map<Int32, ColumnInfo> columns_infos;
for (const auto & path : {c_data_file_value_counts, c_data_file_column_sizes, c_data_file_null_value_counts})
{
if (manifest_file_deserializer.hasPath(path))
{
Field values_count = manifest_file_deserializer.getValueFromRowByName(i, path);
for (const auto & column_stats : values_count.safeGet<Array>())
{
const auto & column_number_and_count = column_stats.safeGet<Tuple>();
Int32 number = static_cast<Int32>(column_number_and_count[0].safeGet<Int32>());
Int64 count = column_number_and_count[1].safeGet<Int64>();
if (path == c_data_file_value_counts)
columns_infos[number].rows_count = count;
else if (path == c_data_file_column_sizes)
columns_infos[number].bytes_size = count;
else
columns_infos[number].nulls_count = count;
}
}
}
std::unordered_map<Int32, std::pair<Field, Field>> value_for_bounds;
for (const auto & path : {c_data_file_lower_bounds, c_data_file_upper_bounds})
{
if (manifest_file_deserializer.hasPath(path))
{
Field bounds = manifest_file_deserializer.getValueFromRowByName(i, path);
for (const auto & column_stats : bounds.safeGet<Array>())
{
const auto & column_number_and_bound = column_stats.safeGet<Tuple>();
Int32 number = static_cast<Int32>(column_number_and_bound[0].safeGet<Int32>());
const Field & bound_value = column_number_and_bound[1];
if (!value_for_bounds.contains(number))
{
value_for_bounds[number] = std::make_pair(Field{}, Field{});
}
if (path == c_data_file_lower_bounds)
value_for_bounds[number].first = bound_value;
else
value_for_bounds[number].second = bound_value;
column_ids_which_have_bounds.insert(number);
}
}
}
if (content_type == FileContentType::DATA)
{
for (const auto & [column_id, bounds] : value_for_bounds)
{
auto field_characteristics = schema_processor.tryGetFieldCharacteristics(schema_id, column_id);
/// If we don't have column characteristics, bounds don't have any sense.
/// This happens if the subfield is inside map ot array, because we don't support
/// name generation for such subfields (we support names of nested subfields in structs only).
if (!field_characteristics)
{
continue;
}
const auto & name_and_type = *field_characteristics;
String left_str;
String right_str;
/// lower_bound and upper_bound may be NULL.
if (!bounds.first.tryGet(left_str) || !bounds.second.tryGet(right_str))
continue;
if (const auto type_id = name_and_type.type->getTypeId();
type_id == DB::TypeIndex::Tuple || type_id == DB::TypeIndex::Map || type_id == DB::TypeIndex::Array)
continue;
auto left = deserializeFieldFromBinaryRepr(left_str, name_and_type.type, true);
auto right = deserializeFieldFromBinaryRepr(right_str, name_and_type.type, false);
if (!left || !right)
continue;
columns_infos[column_id].hyperrectangle.emplace(*left, true, *right, true);
}
}
Int64 added_sequence_number = 0;
String file_format
= manifest_file_deserializer.getValueFromRowByName(i, c_data_file_file_format, TypeIndex::String).safeGet<String>();
if (format_version_ > 1)
{
switch (status)
{
case ManifestEntryStatus::ADDED:
added_sequence_number = inherited_sequence_number;
break;
case ManifestEntryStatus::EXISTING:
{
auto value = manifest_file_deserializer.getValueFromRowByName(i, f_sequence_number);
if (value.isNull())
throw Exception(
DB::ErrorCodes::ICEBERG_SPECIFICATION_VIOLATION,
"Data sequence number is null for the file added in another snapshot");
else
added_sequence_number = value.safeGet<UInt64>();
break;
}
case ManifestEntryStatus::DELETED:
added_sequence_number = inherited_sequence_number;
break;
}
}
std::optional<Int32> sort_order_id;
if (manifest_file_deserializer.hasPath(c_data_file_sort_order_id))
{
auto sort_order_id_value = manifest_file_deserializer.getValueFromRowByName(i, c_data_file_sort_order_id);
if (sort_order_id_value.isNull())
sort_order_id = std::nullopt;
else
sort_order_id = sort_order_id_value.safeGet<Int32>();
}
switch (content_type)
{
case FileContentType::DATA:
this->data_files_without_deleted.emplace_back(
std::make_shared<ManifestFileEntry>(
file_path_key,
file_path,
i,
status,
added_sequence_number,
snapshot_id,
schema_id,
partition_key_value,
common_partition_specification,
columns_infos,
file_format,
/*lower_reference_data_file_path_ = */ std::nullopt,
/*upper_reference_data_file_path_ = */ std::nullopt,
/*equality_ids*/ std::nullopt,
sort_order_id));
break;
case FileContentType::POSITION_DELETE:
{
/// reference_file_path can be absent in schema for some reason, though it is present in specification: https://iceberg.apache.org/spec/#manifests
std::optional<String> lower_reference_data_file_path = std::nullopt;
std::optional<String> upper_reference_data_file_path = std::nullopt;
bool bounds_set_by_referenced_data_file = false;
if (manifest_file_deserializer.hasPath(c_data_file_referenced_data_file))
{
Field reference_file_path_field = manifest_file_deserializer.getValueFromRowByName(i, c_data_file_referenced_data_file);
if (!reference_file_path_field.isNull())
{
lower_reference_data_file_path = reference_file_path_field.safeGet<String>();
upper_reference_data_file_path = reference_file_path_field.safeGet<String>();
bounds_set_by_referenced_data_file = true;
}
}
if (!bounds_set_by_referenced_data_file)
{
if (auto it = value_for_bounds.find(IcebergPositionDeleteTransform::data_file_path_column_field_id);
it != value_for_bounds.end())
{
auto & [lower, upper] = it->second;
if (!lower.isNull())
lower_reference_data_file_path = lower.safeGet<String>();
if (!upper.isNull())
upper_reference_data_file_path = upper.safeGet<String>();
}
}
this->position_deletes_files_without_deleted.emplace_back(
std::make_shared<ManifestFileEntry>(
file_path_key,
file_path,
i,
status,
added_sequence_number,
snapshot_id,
schema_id,
partition_key_value,
common_partition_specification,
columns_infos,
file_format,
lower_reference_data_file_path,
upper_reference_data_file_path,
/*equality_ids*/ std::nullopt,
/*sort_order_id = */ std::nullopt));
break;
}
case FileContentType::EQUALITY_DELETE:
{
std::vector<Int32> equality_ids;
if (manifest_file_deserializer.hasPath(c_data_file_equality_ids))
{
Field equality_ids_field = manifest_file_deserializer.getValueFromRowByName(i, c_data_file_equality_ids);
for (const Field & id : equality_ids_field.safeGet<Array>())
equality_ids.push_back(static_cast<Int32>(id.safeGet<Int32>()));
}
else
throw Exception(
DB::ErrorCodes::ICEBERG_SPECIFICATION_VIOLATION,
"Couldn't find field {} in equality delete file entry", c_data_file_equality_ids);
this->equality_deletes_files.emplace_back(
std::make_shared<ManifestFileEntry>(
file_path_key,
file_path,
i,
status,
added_sequence_number,
snapshot_id,
schema_id,
partition_key_value,
common_partition_specification,
columns_infos,
file_format,
/*lower_reference_data_file_path_ = */ std::nullopt,
/*upper_reference_data_file_path_ = */ std::nullopt,
equality_ids,
/*sort_order_id = */ std::nullopt));
break;
}
}
}
sortManifestEntriesBySchemaId(data_files_without_deleted);
}
// We prefer files to be sorted by schema id, because it allows us to reuse ManifestFilePruner during partition and minmax pruning
void ManifestFileContent::sortManifestEntriesBySchemaId(std::vector<ManifestFileEntryPtr> & files)
{
std::vector<size_t> indices(files.size());
std::iota(indices.begin(), indices.end(), 0);
std::sort(
indices.begin(),
indices.end(),
[&](size_t i, size_t j)
{
if (files[i]->schema_id != files[j]->schema_id)
{
return files[i]->schema_id < files[j]->schema_id;
}
return i < j;
});
std::vector<ManifestFileEntryPtr> sorted_files;
sorted_files.reserve(files.size());
for (const auto & index : indices)
{
sorted_files.emplace_back(std::move(files[index]));
}
files = std::move(sorted_files);
}
bool ManifestFileContent::hasPartitionKey() const
{
return partition_key_description.has_value();
}
const DB::KeyDescription & ManifestFileContent::getPartitionKeyDescription() const
{
if (!hasPartitionKey())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table has no partition key, but it was requested");
return *(partition_key_description);
}
bool ManifestFileContent::hasBoundsInfoInManifests() const
{
return !column_ids_which_have_bounds.empty();
}
const std::set<Int32> & ManifestFileContent::getColumnsIDsWithBounds() const
{
return column_ids_which_have_bounds;
}
bool ManifestFileContent::areAllDataFilesSortedBySortOrderID(Int32 sort_order_id) const
{
for (const auto & file : data_files_without_deleted)
{
// Treat missing sort_order_id as "not sorted by the expected order".
// This can happen if:
// 1. The field is not present in older Iceberg format versions.
// 2. The data file was written without sort order information.
if (!file->sort_order_id.has_value() || (*file->sort_order_id != sort_order_id))
return false;
}
/// Empty manifest (no data files) is considered sorted by definition
return true;
}
size_t ManifestFileContent::getSizeInMemory() const
{
size_t total_size = sizeof(ManifestFileContent);
if (partition_key_description)
total_size += sizeof(DB::KeyDescription);
total_size += column_ids_which_have_bounds.size() * sizeof(Int32);
total_size += data_files_without_deleted.capacity() * sizeof(ManifestFileEntry);
total_size += position_deletes_files_without_deleted.capacity() * sizeof(ManifestFileEntry);
return total_size;
}
std::optional<Int64> ManifestFileContent::getRowsCountInAllFilesExcludingDeleted(FileContentType content) const
{
Int64 result = 0;
for (const auto & file : getFilesWithoutDeleted(content))
{
/// Have at least one column with rows count
bool found = false;
for (const auto & [column, column_info] : file->columns_infos)
{
if (column_info.rows_count.has_value())
{
result += *column_info.rows_count;
found = true;
break;
}
}
if (!found)
return std::nullopt;
}
return result;
}
std::optional<Int64> ManifestFileContent::getBytesCountInAllDataFilesExcludingDeleted() const
{
Int64 result = 0;
for (const auto & file : data_files_without_deleted)
{
/// Have at least one column with bytes count
bool found = false;
for (const auto & [column, column_info] : file->columns_infos)
{
if (column_info.bytes_size.has_value())
{
result += *column_info.bytes_size;
found = true;
break;
}
}
if (!found)
return std::nullopt;
}
return result;
}
std::strong_ordering operator<=>(const PartitionSpecsEntry & lhs, const PartitionSpecsEntry & rhs)
{
return std::tie(lhs.source_id, lhs.transform_name, lhs.partition_name)
<=> std::tie(rhs.source_id, rhs.transform_name, rhs.partition_name);
}
template <typename A>
bool less(const std::vector<A> & lhs, const std::vector<A> & rhs)
{
if (lhs.size() != rhs.size())
return lhs.size() < rhs.size();
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](const A & a, const A & b) { return a < b; });
}
bool operator<(const PartitionSpecification & lhs, const PartitionSpecification & rhs)
{
return less(lhs, rhs);
}
bool operator<(const DB::Row & lhs, const DB::Row & rhs)
{
return less(lhs, rhs);
}
std::weak_ordering operator<=>(const ManifestFileEntryPtr & lhs, const ManifestFileEntryPtr & rhs)
{
return std::tie(lhs->common_partition_specification, lhs->partition_key_value, lhs->added_sequence_number)
<=> std::tie(rhs->common_partition_specification, rhs->partition_key_value, rhs->added_sequence_number);
}
String dumpPartitionSpecification(const PartitionSpecification & partition_specification)
{
if (partition_specification.empty())
return "[empty]";
else
{
String answer{"["};
for (size_t i = 0; i < partition_specification.size(); ++i)
{
const auto & entry = partition_specification[i];
answer += fmt::format(
"(source id: {}, transform name: {}, partition name: {})", entry.source_id, entry.transform_name, entry.partition_name);
if (i != partition_specification.size() - 1)
answer += ", ";
}
answer += ']';
return answer;
}
}
String dumpPartitionKeyValue(const DB::Row & partition_key_value)
{
if (partition_key_value.empty())
return "[empty]";
else
{
String answer{"["};
for (size_t i = 0; i < partition_key_value.size(); ++i)
{
const auto & entry = partition_key_value[i];
answer += entry.dump();
if (i != partition_key_value.size() - 1)
answer += ", ";
}
answer += ']';
return answer;
}
}
String ManifestFileEntry::dumpDeletesMatchingInfo() const
{
return fmt::format(
"Partition specification: {}, partition key value: {}, added sequence number: {}",
dumpPartitionSpecification(common_partition_specification),
dumpPartitionKeyValue(partition_key_value),
added_sequence_number);
}
}
#endif