-
Notifications
You must be signed in to change notification settings - Fork 187
/
manifest_list.rs
1541 lines (1437 loc) · 59.2 KB
/
manifest_list.rs
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! ManifestList for Iceberg.
use std::collections::HashMap;
use std::str::FromStr;
use apache_avro::types::Value;
use apache_avro::{from_value, Reader, Writer};
use bytes::Bytes;
use self::_const_schema::{MANIFEST_LIST_AVRO_SCHEMA_V1, MANIFEST_LIST_AVRO_SCHEMA_V2};
use self::_serde::{ManifestFileV1, ManifestFileV2};
use super::{Datum, FormatVersion, Manifest, StructType};
use crate::error::Result;
use crate::io::{FileIO, OutputFile};
use crate::{Error, ErrorKind};
/// Placeholder for sequence number. The field with this value must be replaced with the actual sequence number before it write.
pub const UNASSIGNED_SEQUENCE_NUMBER: i64 = -1;
/// Snapshots are embedded in table metadata, but the list of manifests for a
/// snapshot are stored in a separate manifest list file.
///
/// A new manifest list is written for each attempt to commit a snapshot
/// because the list of manifests always changes to produce a new snapshot.
/// When a manifest list is written, the (optimistic) sequence number of the
/// snapshot is written for all new manifest files tracked by the list.
///
/// A manifest list includes summary metadata that can be used to avoid
/// scanning all of the manifests in a snapshot when planning a table scan.
/// This includes the number of added, existing, and deleted files, and a
/// summary of values for each field of the partition spec used to write the
/// manifest.
#[derive(Debug, Clone, PartialEq)]
pub struct ManifestList {
/// Entries in a manifest list.
entries: Vec<ManifestFile>,
}
impl ManifestList {
/// Parse manifest list from bytes.
pub fn parse_with_version(
bs: &[u8],
version: FormatVersion,
partition_type_provider: impl Fn(i32) -> Result<Option<StructType>>,
) -> Result<ManifestList> {
match version {
FormatVersion::V1 => {
let reader = Reader::with_schema(&MANIFEST_LIST_AVRO_SCHEMA_V1, bs)?;
let values = Value::Array(reader.collect::<std::result::Result<Vec<Value>, _>>()?);
from_value::<_serde::ManifestListV1>(&values)?.try_into(partition_type_provider)
}
FormatVersion::V2 => {
let reader = Reader::new(bs)?;
let values = Value::Array(reader.collect::<std::result::Result<Vec<Value>, _>>()?);
from_value::<_serde::ManifestListV2>(&values)?.try_into(partition_type_provider)
}
}
}
/// Get the entries in the manifest list.
pub fn entries(&self) -> &[ManifestFile] {
&self.entries
}
/// Take ownership of the entries in the manifest list, consuming it
pub fn consume_entries(self) -> impl IntoIterator<Item = ManifestFile> {
Box::new(self.entries.into_iter())
}
}
/// A manifest list writer.
pub struct ManifestListWriter {
format_version: FormatVersion,
output_file: OutputFile,
avro_writer: Writer<'static, Vec<u8>>,
sequence_number: i64,
snapshot_id: i64,
}
impl std::fmt::Debug for ManifestListWriter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ManifestListWriter")
.field("format_version", &self.format_version)
.field("output_file", &self.output_file)
.field("avro_writer", &self.avro_writer.schema())
.finish_non_exhaustive()
}
}
impl ManifestListWriter {
/// Construct a v1 [`ManifestListWriter`] that writes to a provided [`OutputFile`].
pub fn v1(output_file: OutputFile, snapshot_id: i64, parent_snapshot_id: Option<i64>) -> Self {
let mut metadata = HashMap::from_iter([
("snapshot-id".to_string(), snapshot_id.to_string()),
("format-version".to_string(), "1".to_string()),
]);
if let Some(parent_snapshot_id) = parent_snapshot_id {
metadata.insert(
"parent-snapshot-id".to_string(),
parent_snapshot_id.to_string(),
);
}
Self::new(FormatVersion::V1, output_file, metadata, 0, snapshot_id)
}
/// Construct a v2 [`ManifestListWriter`] that writes to a provided [`OutputFile`].
pub fn v2(
output_file: OutputFile,
snapshot_id: i64,
parent_snapshot_id: Option<i64>,
sequence_number: i64,
) -> Self {
let mut metadata = HashMap::from_iter([
("snapshot-id".to_string(), snapshot_id.to_string()),
("sequence-number".to_string(), sequence_number.to_string()),
("format-version".to_string(), "2".to_string()),
]);
metadata.insert(
"parent-snapshot-id".to_string(),
parent_snapshot_id
.map(|v| v.to_string())
.unwrap_or("null".to_string()),
);
Self::new(
FormatVersion::V2,
output_file,
metadata,
sequence_number,
snapshot_id,
)
}
fn new(
format_version: FormatVersion,
output_file: OutputFile,
metadata: HashMap<String, String>,
sequence_number: i64,
snapshot_id: i64,
) -> Self {
let avro_schema = match format_version {
FormatVersion::V1 => &MANIFEST_LIST_AVRO_SCHEMA_V1,
FormatVersion::V2 => &MANIFEST_LIST_AVRO_SCHEMA_V2,
};
let mut avro_writer = Writer::new(avro_schema, Vec::new());
for (key, value) in metadata {
avro_writer
.add_user_metadata(key, value)
.expect("Avro metadata should be added to the writer before the first record.");
}
Self {
format_version,
output_file,
avro_writer,
sequence_number,
snapshot_id,
}
}
/// Append manifests to be written.
pub fn add_manifests(&mut self, manifests: impl Iterator<Item = ManifestFile>) -> Result<()> {
match self.format_version {
FormatVersion::V1 => {
for manifest in manifests {
let manifes: ManifestFileV1 = manifest.try_into()?;
self.avro_writer.append_ser(manifes)?;
}
}
FormatVersion::V2 => {
for mut manifest in manifests {
if manifest.sequence_number == UNASSIGNED_SEQUENCE_NUMBER {
if manifest.added_snapshot_id != self.snapshot_id {
return Err(Error::new(
ErrorKind::DataInvalid,
format!(
"Found unassigned sequence number for a manifest from snapshot {}.",
manifest.added_snapshot_id
),
));
}
manifest.sequence_number = self.sequence_number;
}
if manifest.min_sequence_number == UNASSIGNED_SEQUENCE_NUMBER {
if manifest.added_snapshot_id != self.snapshot_id {
return Err(Error::new(
ErrorKind::DataInvalid,
format!(
"Found unassigned sequence number for a manifest from snapshot {}.",
manifest.added_snapshot_id
),
));
}
manifest.min_sequence_number = self.sequence_number;
}
let manifest_entry: ManifestFileV2 = manifest.try_into()?;
self.avro_writer.append_ser(manifest_entry)?;
}
}
}
Ok(())
}
/// Write the manifest list to the output file.
pub async fn close(self) -> Result<()> {
let data = self.avro_writer.into_inner()?;
let mut writer = self.output_file.writer().await?;
writer.write(Bytes::from(data)).await?;
writer.close().await?;
Ok(())
}
}
/// This is a helper module that defines the schema field of the manifest list entry.
mod _const_schema {
use std::sync::Arc;
use apache_avro::Schema as AvroSchema;
use once_cell::sync::Lazy;
use crate::avro::schema_to_avro_schema;
use crate::spec::{
ListType, NestedField, NestedFieldRef, PrimitiveType, Schema, StructType, Type,
};
static MANIFEST_PATH: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
500,
"manifest_path",
Type::Primitive(PrimitiveType::String),
))
})
};
static MANIFEST_LENGTH: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
501,
"manifest_length",
Type::Primitive(PrimitiveType::Long),
))
})
};
static PARTITION_SPEC_ID: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
502,
"partition_spec_id",
Type::Primitive(PrimitiveType::Int),
))
})
};
static CONTENT: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
517,
"content",
Type::Primitive(PrimitiveType::Int),
))
})
};
static SEQUENCE_NUMBER: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
515,
"sequence_number",
Type::Primitive(PrimitiveType::Long),
))
})
};
static MIN_SEQUENCE_NUMBER: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
516,
"min_sequence_number",
Type::Primitive(PrimitiveType::Long),
))
})
};
static ADDED_SNAPSHOT_ID: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
503,
"added_snapshot_id",
Type::Primitive(PrimitiveType::Long),
))
})
};
static ADDED_FILES_COUNT_V2: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
504,
"added_files_count",
Type::Primitive(PrimitiveType::Int),
))
})
};
static ADDED_FILES_COUNT_V1: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::optional(
504,
"added_data_files_count",
Type::Primitive(PrimitiveType::Int),
))
})
};
static EXISTING_FILES_COUNT_V2: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
505,
"existing_files_count",
Type::Primitive(PrimitiveType::Int),
))
})
};
static EXISTING_FILES_COUNT_V1: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::optional(
505,
"existing_data_files_count",
Type::Primitive(PrimitiveType::Int),
))
})
};
static DELETED_FILES_COUNT_V2: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
506,
"deleted_files_count",
Type::Primitive(PrimitiveType::Int),
))
})
};
static DELETED_FILES_COUNT_V1: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::optional(
506,
"deleted_data_files_count",
Type::Primitive(PrimitiveType::Int),
))
})
};
static ADDED_ROWS_COUNT_V2: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
512,
"added_rows_count",
Type::Primitive(PrimitiveType::Long),
))
})
};
static ADDED_ROWS_COUNT_V1: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::optional(
512,
"added_rows_count",
Type::Primitive(PrimitiveType::Long),
))
})
};
static EXISTING_ROWS_COUNT_V2: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
513,
"existing_rows_count",
Type::Primitive(PrimitiveType::Long),
))
})
};
static EXISTING_ROWS_COUNT_V1: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::optional(
513,
"existing_rows_count",
Type::Primitive(PrimitiveType::Long),
))
})
};
static DELETED_ROWS_COUNT_V2: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::required(
514,
"deleted_rows_count",
Type::Primitive(PrimitiveType::Long),
))
})
};
static DELETED_ROWS_COUNT_V1: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::optional(
514,
"deleted_rows_count",
Type::Primitive(PrimitiveType::Long),
))
})
};
static PARTITIONS: Lazy<NestedFieldRef> = {
Lazy::new(|| {
// element type
let fields = vec![
Arc::new(NestedField::required(
509,
"contains_null",
Type::Primitive(PrimitiveType::Boolean),
)),
Arc::new(NestedField::optional(
518,
"contains_nan",
Type::Primitive(PrimitiveType::Boolean),
)),
Arc::new(NestedField::optional(
510,
"lower_bound",
Type::Primitive(PrimitiveType::Binary),
)),
Arc::new(NestedField::optional(
511,
"upper_bound",
Type::Primitive(PrimitiveType::Binary),
)),
];
let element_field = Arc::new(NestedField::required(
508,
"r_508",
Type::Struct(StructType::new(fields)),
));
Arc::new(NestedField::optional(
507,
"partitions",
Type::List(ListType { element_field }),
))
})
};
static KEY_METADATA: Lazy<NestedFieldRef> = {
Lazy::new(|| {
Arc::new(NestedField::optional(
519,
"key_metadata",
Type::Primitive(PrimitiveType::Binary),
))
})
};
static V1_SCHEMA: Lazy<Schema> = {
Lazy::new(|| {
let fields = vec![
MANIFEST_PATH.clone(),
MANIFEST_LENGTH.clone(),
PARTITION_SPEC_ID.clone(),
ADDED_SNAPSHOT_ID.clone(),
ADDED_FILES_COUNT_V1.clone().to_owned(),
EXISTING_FILES_COUNT_V1.clone(),
DELETED_FILES_COUNT_V1.clone(),
ADDED_ROWS_COUNT_V1.clone(),
EXISTING_ROWS_COUNT_V1.clone(),
DELETED_ROWS_COUNT_V1.clone(),
PARTITIONS.clone(),
KEY_METADATA.clone(),
];
Schema::builder().with_fields(fields).build().unwrap()
})
};
static V2_SCHEMA: Lazy<Schema> = {
Lazy::new(|| {
let fields = vec![
MANIFEST_PATH.clone(),
MANIFEST_LENGTH.clone(),
PARTITION_SPEC_ID.clone(),
CONTENT.clone(),
SEQUENCE_NUMBER.clone(),
MIN_SEQUENCE_NUMBER.clone(),
ADDED_SNAPSHOT_ID.clone(),
ADDED_FILES_COUNT_V2.clone(),
EXISTING_FILES_COUNT_V2.clone(),
DELETED_FILES_COUNT_V2.clone(),
ADDED_ROWS_COUNT_V2.clone(),
EXISTING_ROWS_COUNT_V2.clone(),
DELETED_ROWS_COUNT_V2.clone(),
PARTITIONS.clone(),
KEY_METADATA.clone(),
];
Schema::builder().with_fields(fields).build().unwrap()
})
};
pub(super) static MANIFEST_LIST_AVRO_SCHEMA_V1: Lazy<AvroSchema> =
Lazy::new(|| schema_to_avro_schema("manifest_file", &V1_SCHEMA).unwrap());
pub(super) static MANIFEST_LIST_AVRO_SCHEMA_V2: Lazy<AvroSchema> =
Lazy::new(|| schema_to_avro_schema("manifest_file", &V2_SCHEMA).unwrap());
}
/// Entry in a manifest list.
#[derive(Debug, PartialEq, Clone)]
pub struct ManifestFile {
/// field: 500
///
/// Location of the manifest file
pub manifest_path: String,
/// field: 501
///
/// Length of the manifest file in bytes
pub manifest_length: i64,
/// field: 502
///
/// ID of a partition spec used to write the manifest; must be listed
/// in table metadata partition-specs
pub partition_spec_id: i32,
/// field: 517
///
/// The type of files tracked by the manifest, either data or delete
/// files; 0 for all v1 manifests
pub content: ManifestContentType,
/// field: 515
///
/// The sequence number when the manifest was added to the table; use 0
/// when reading v1 manifest lists
pub sequence_number: i64,
/// field: 516
///
/// The minimum data sequence number of all live data or delete files in
/// the manifest; use 0 when reading v1 manifest lists
pub min_sequence_number: i64,
/// field: 503
///
/// ID of the snapshot where the manifest file was added
pub added_snapshot_id: i64,
/// field: 504
///
/// Number of entries in the manifest that have status ADDED, when null
/// this is assumed to be non-zero
pub added_files_count: Option<u32>,
/// field: 505
///
/// Number of entries in the manifest that have status EXISTING (0),
/// when null this is assumed to be non-zero
pub existing_files_count: Option<u32>,
/// field: 506
///
/// Number of entries in the manifest that have status DELETED (2),
/// when null this is assumed to be non-zero
pub deleted_files_count: Option<u32>,
/// field: 512
///
/// Number of rows in all of files in the manifest that have status
/// ADDED, when null this is assumed to be non-zero
pub added_rows_count: Option<u64>,
/// field: 513
///
/// Number of rows in all of files in the manifest that have status
/// EXISTING, when null this is assumed to be non-zero
pub existing_rows_count: Option<u64>,
/// field: 514
///
/// Number of rows in all of files in the manifest that have status
/// DELETED, when null this is assumed to be non-zero
pub deleted_rows_count: Option<u64>,
/// field: 507
/// element_field: 508
///
/// A list of field summaries for each partition field in the spec. Each
/// field in the list corresponds to a field in the manifest file’s
/// partition spec.
pub partitions: Vec<FieldSummary>,
/// field: 519
///
/// Implementation-specific key metadata for encryption
pub key_metadata: Vec<u8>,
}
impl ManifestFile {
/// Checks if the manifest file has any added files.
pub fn has_added_files(&self) -> bool {
self.added_files_count.is_none() || self.added_files_count.unwrap() > 0
}
/// Checks if the manifest file has any existed files.
pub fn has_existing_files(&self) -> bool {
self.existing_files_count.is_none() || self.existing_files_count.unwrap() > 0
}
}
/// The type of files tracked by the manifest, either data or delete files; Data(0) for all v1 manifests
#[derive(Debug, PartialEq, Clone, Copy, Eq)]
pub enum ManifestContentType {
/// The manifest content is data.
Data = 0,
/// The manifest content is deletes.
Deletes = 1,
}
impl FromStr for ManifestContentType {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"data" => Ok(ManifestContentType::Data),
"deletes" => Ok(ManifestContentType::Deletes),
_ => Err(Error::new(
ErrorKind::DataInvalid,
format!("Invalid manifest content type: {s}"),
)),
}
}
}
impl std::fmt::Display for ManifestContentType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ManifestContentType::Data => write!(f, "data"),
ManifestContentType::Deletes => write!(f, "deletes"),
}
}
}
impl TryFrom<i32> for ManifestContentType {
type Error = Error;
fn try_from(value: i32) -> std::result::Result<Self, Self::Error> {
match value {
0 => Ok(ManifestContentType::Data),
1 => Ok(ManifestContentType::Deletes),
_ => Err(Error::new(
crate::ErrorKind::DataInvalid,
format!(
"Invalid manifest content type. Expected 0 or 1, got {}",
value
),
)),
}
}
}
impl ManifestFile {
/// Load [`Manifest`].
///
/// This method will also initialize inherited values of [`ManifestEntry`], such as `sequence_number`.
pub async fn load_manifest(&self, file_io: &FileIO) -> Result<Manifest> {
let avro = file_io.new_input(&self.manifest_path)?.read().await?;
let (metadata, mut entries) = Manifest::try_from_avro_bytes(&avro)?;
// Let entries inherit values from the manifest list entry.
for entry in &mut entries {
entry.inherit_data(self);
}
Ok(Manifest::new(metadata, entries))
}
}
/// Field summary for partition field in the spec.
///
/// Each field in the list corresponds to a field in the manifest file’s partition spec.
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct FieldSummary {
/// field: 509
///
/// Whether the manifest contains at least one partition with a null
/// value for the field
pub contains_null: bool,
/// field: 518
/// Whether the manifest contains at least one partition with a NaN
/// value for the field
pub contains_nan: Option<bool>,
/// field: 510
/// The minimum value for the field in the manifests
/// partitions.
pub lower_bound: Option<Datum>,
/// field: 511
/// The maximum value for the field in the manifests
/// partitions.
pub upper_bound: Option<Datum>,
}
/// This is a helper module that defines types to help with serialization/deserialization.
/// For deserialization the input first gets read into either the [ManifestFileV1] or [ManifestFileV2] struct
/// and then converted into the [ManifestFile] struct. Serialization works the other way around.
/// [ManifestFileV1] and [ManifestFileV2] are internal struct that are only used for serialization and deserialization.
pub(super) mod _serde {
pub use serde_bytes::ByteBuf;
use serde_derive::{Deserialize, Serialize};
use super::ManifestFile;
use crate::error::Result;
use crate::spec::{Datum, PrimitiveType, StructType};
use crate::Error;
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(transparent)]
pub(crate) struct ManifestListV2 {
entries: Vec<ManifestFileV2>,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(transparent)]
pub(crate) struct ManifestListV1 {
entries: Vec<ManifestFileV1>,
}
impl ManifestListV2 {
/// Converts the [ManifestListV2] into a [ManifestList].
/// The convert of [entries] need the partition_type info so use this function instead of [std::TryFrom] trait.
pub fn try_into(
self,
partition_type_provider: impl Fn(i32) -> Result<Option<StructType>>,
) -> Result<super::ManifestList> {
Ok(super::ManifestList {
entries: self
.entries
.into_iter()
.map(|v| {
let partition_spec_id = v.partition_spec_id;
let manifest_path = v.manifest_path.clone();
v.try_into(partition_type_provider(partition_spec_id)?.as_ref())
.map_err(|err| {
err.with_context("manifest file path", manifest_path)
.with_context(
"partition spec id",
partition_spec_id.to_string(),
)
})
})
.collect::<Result<Vec<_>>>()?,
})
}
}
impl TryFrom<super::ManifestList> for ManifestListV2 {
type Error = Error;
fn try_from(value: super::ManifestList) -> std::result::Result<Self, Self::Error> {
Ok(Self {
entries: value
.entries
.into_iter()
.map(TryInto::try_into)
.collect::<std::result::Result<Vec<_>, _>>()?,
})
}
}
impl ManifestListV1 {
/// Converts the [ManifestListV1] into a [ManifestList].
/// The convert of [entries] need the partition_type info so use this function instead of [std::TryFrom] trait.
pub fn try_into(
self,
partition_type_provider: impl Fn(i32) -> Result<Option<StructType>>,
) -> Result<super::ManifestList> {
Ok(super::ManifestList {
entries: self
.entries
.into_iter()
.map(|v| {
let partition_spec_id = v.partition_spec_id;
let manifest_path = v.manifest_path.clone();
v.try_into(partition_type_provider(partition_spec_id)?.as_ref())
.map_err(|err| {
err.with_context("manifest file path", manifest_path)
.with_context(
"partition spec id",
partition_spec_id.to_string(),
)
})
})
.collect::<Result<Vec<_>>>()?,
})
}
}
impl TryFrom<super::ManifestList> for ManifestListV1 {
type Error = Error;
fn try_from(value: super::ManifestList) -> std::result::Result<Self, Self::Error> {
Ok(Self {
entries: value
.entries
.into_iter()
.map(TryInto::try_into)
.collect::<std::result::Result<Vec<_>, _>>()?,
})
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub(super) struct ManifestFileV1 {
pub manifest_path: String,
pub manifest_length: i64,
pub partition_spec_id: i32,
pub added_snapshot_id: i64,
pub added_data_files_count: Option<i32>,
pub existing_data_files_count: Option<i32>,
pub deleted_data_files_count: Option<i32>,
pub added_rows_count: Option<i64>,
pub existing_rows_count: Option<i64>,
pub deleted_rows_count: Option<i64>,
pub partitions: Option<Vec<FieldSummary>>,
pub key_metadata: Option<ByteBuf>,
}
// Aliases were added to fields that were renamed in Iceberg 1.5.0 (https://github.com/apache/iceberg/pull/5338), in order to support both conventions/versions.
// In the current implementation deserialization is done using field names, and therefore these fields may appear as either.
// see issue that raised this here: https://github.com/apache/iceberg-rust/issues/338
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub(super) struct ManifestFileV2 {
pub manifest_path: String,
pub manifest_length: i64,
pub partition_spec_id: i32,
pub content: i32,
pub sequence_number: i64,
pub min_sequence_number: i64,
pub added_snapshot_id: i64,
#[serde(alias = "added_data_files_count", alias = "added_files_count")]
pub added_files_count: i32,
#[serde(alias = "existing_data_files_count", alias = "existing_files_count")]
pub existing_files_count: i32,
#[serde(alias = "deleted_data_files_count", alias = "deleted_files_count")]
pub deleted_files_count: i32,
pub added_rows_count: i64,
pub existing_rows_count: i64,
pub deleted_rows_count: i64,
pub partitions: Option<Vec<FieldSummary>>,
pub key_metadata: Option<ByteBuf>,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub(super) struct FieldSummary {
pub contains_null: bool,
pub contains_nan: Option<bool>,
pub lower_bound: Option<ByteBuf>,
pub upper_bound: Option<ByteBuf>,
}
impl FieldSummary {
/// Converts the [FieldSummary] into a [super::FieldSummary].
/// [lower_bound] and [upper_bound] are converted into [Literal]s need the type info so use
/// this function instead of [std::TryFrom] trait.
pub(crate) fn try_into(self, r#type: &PrimitiveType) -> Result<super::FieldSummary> {
Ok(super::FieldSummary {
contains_null: self.contains_null,
contains_nan: self.contains_nan,
lower_bound: self
.lower_bound
.as_ref()
.map(|v| Datum::try_from_bytes(v, r#type.clone()))
.transpose()
.map_err(|err| err.with_context("type", format!("{:?}", r#type)))?,
upper_bound: self
.upper_bound
.as_ref()
.map(|v| Datum::try_from_bytes(v, r#type.clone()))
.transpose()
.map_err(|err| err.with_context("type", format!("{:?}", r#type)))?,
})
}
}
fn try_convert_to_field_summary(
partitions: Option<Vec<FieldSummary>>,
partition_type: Option<&StructType>,
) -> Result<Vec<super::FieldSummary>> {
if let Some(partitions) = partitions {
if let Some(partition_type) = partition_type {
let partition_types = partition_type.fields();
if partitions.len() != partition_types.len() {
return Err(Error::new(
crate::ErrorKind::DataInvalid,
format!(
"Invalid partition spec. Expected {} fields, got {}",
partition_types.len(),
partitions.len()
),
));
}
partitions
.into_iter()
.zip(partition_types)
.map(|(v, field)| {
v.try_into(field.field_type.as_primitive_type().ok_or_else(|| {
Error::new(
crate::ErrorKind::DataInvalid,
"Invalid partition spec. Field type is not primitive",
)
})?)
})
.collect::<Result<Vec<_>>>()
} else {
Err(Error::new(
crate::ErrorKind::DataInvalid,
"Invalid partition spec. Partition type is required",
))
}
} else {
Ok(Vec::new())
}
}
impl ManifestFileV2 {
/// Converts the [ManifestFileV2] into a [ManifestFile].
/// The convert of [partitions] need the partition_type info so use this function instead of [std::TryFrom] trait.
pub fn try_into(self, partition_type: Option<&StructType>) -> Result<ManifestFile> {
let partitions = try_convert_to_field_summary(self.partitions, partition_type)?;
Ok(ManifestFile {
manifest_path: self.manifest_path,
manifest_length: self.manifest_length,
partition_spec_id: self.partition_spec_id,
content: self.content.try_into()?,
sequence_number: self.sequence_number,
min_sequence_number: self.min_sequence_number,
added_snapshot_id: self.added_snapshot_id,
added_files_count: Some(self.added_files_count.try_into()?),
existing_files_count: Some(self.existing_files_count.try_into()?),
deleted_files_count: Some(self.deleted_files_count.try_into()?),
added_rows_count: Some(self.added_rows_count.try_into()?),
existing_rows_count: Some(self.existing_rows_count.try_into()?),
deleted_rows_count: Some(self.deleted_rows_count.try_into()?),
partitions,
key_metadata: self.key_metadata.map(|b| b.into_vec()).unwrap_or_default(),
})
}
}
impl ManifestFileV1 {
/// Converts the [MManifestFileV1] into a [ManifestFile].
/// The convert of [partitions] need the partition_type info so use this function instead of [std::TryFrom] trait.
pub fn try_into(self, partition_type: Option<&StructType>) -> Result<ManifestFile> {
let partitions = try_convert_to_field_summary(self.partitions, partition_type)?;
Ok(ManifestFile {
manifest_path: self.manifest_path,
manifest_length: self.manifest_length,
partition_spec_id: self.partition_spec_id,
added_snapshot_id: self.added_snapshot_id,
added_files_count: self
.added_data_files_count
.map(TryInto::try_into)
.transpose()?,
existing_files_count: self
.existing_data_files_count
.map(TryInto::try_into)
.transpose()?,
deleted_files_count: self
.deleted_data_files_count
.map(TryInto::try_into)
.transpose()?,
added_rows_count: self.added_rows_count.map(TryInto::try_into).transpose()?,
existing_rows_count: self
.existing_rows_count
.map(TryInto::try_into)
.transpose()?,
deleted_rows_count: self.deleted_rows_count.map(TryInto::try_into).transpose()?,
partitions,
key_metadata: self.key_metadata.map(|b| b.into_vec()).unwrap_or_default(),
// as ref: https://iceberg.apache.org/spec/#partitioning
// use 0 when reading v1 manifest lists
content: super::ManifestContentType::Data,
sequence_number: 0,
min_sequence_number: 0,
})
}
}
fn convert_to_serde_field_summary(
partitions: Vec<super::FieldSummary>,
) -> Result<Option<Vec<FieldSummary>>> {
if partitions.is_empty() {
Ok(None)
} else {
let mut vs = Vec::with_capacity(partitions.len());
for v in partitions {
let fs = FieldSummary {
contains_null: v.contains_null,
contains_nan: v.contains_nan,
lower_bound: v.lower_bound.map(|v| v.to_bytes()).transpose()?,
upper_bound: v.upper_bound.map(|v| v.to_bytes()).transpose()?,
};
vs.push(fs);
}
Ok(Some(vs))
}
}
fn convert_to_serde_key_metadata(key_metadata: Vec<u8>) -> Option<ByteBuf> {
if key_metadata.is_empty() {