Skip to content

Commit 8184255

Browse files
author
ZENOTME
committed
store current table instead of current metadata
1 parent 21b3dae commit 8184255

File tree

2 files changed

+63
-35
lines changed

2 files changed

+63
-35
lines changed

crates/iceberg/src/table.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ pub struct Table {
162162
}
163163

164164
impl Table {
165+
pub(crate) fn with_metadata(&mut self, metadata: TableMetadataRef) {
166+
self.metadata = metadata;
167+
}
168+
165169
/// Returns a TableBuilder to build a table
166170
pub fn builder() -> TableBuilder {
167171
TableBuilder::new()

crates/iceberg/src/transaction.rs

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::collections::{HashMap, HashSet};
2222
use std::future::Future;
2323
use std::mem::discriminant;
2424
use std::ops::RangeFrom;
25+
use std::sync::Arc;
2526

2627
use arrow_array::StringArray;
2728
use futures::TryStreamExt;
@@ -45,7 +46,7 @@ const META_ROOT_PATH: &str = "metadata";
4546
/// Table transaction.
4647
pub struct Transaction<'a> {
4748
base_table: &'a Table,
48-
current_metadata: TableMetadata,
49+
current_table: Table,
4950
updates: Vec<TableUpdate>,
5051
requirements: Vec<TableRequirement>,
5152
}
@@ -55,19 +56,20 @@ impl<'a> Transaction<'a> {
5556
pub fn new(table: &'a Table) -> Self {
5657
Self {
5758
base_table: table,
58-
current_metadata: table.metadata().clone(),
59+
current_table: table.clone(),
5960
updates: vec![],
6061
requirements: vec![],
6162
}
6263
}
6364

6465
fn update_table_metadata(&mut self, updates: &[TableUpdate]) -> Result<()> {
65-
let mut metadata_builder = self.current_metadata.clone().into_builder(None);
66+
let mut metadata_builder = self.current_table.metadata().clone().into_builder(None);
6667
for update in updates {
6768
metadata_builder = update.clone().apply(metadata_builder)?;
6869
}
6970

70-
self.current_metadata = metadata_builder.build()?.metadata;
71+
self.current_table
72+
.with_metadata(Arc::new(metadata_builder.build()?.metadata));
7173

7274
Ok(())
7375
}
@@ -78,7 +80,7 @@ impl<'a> Transaction<'a> {
7880
requirements: Vec<TableRequirement>,
7981
) -> Result<()> {
8082
for requirement in &requirements {
81-
requirement.check(Some(&self.current_metadata))?;
83+
requirement.check(Some(self.current_table.metadata()))?;
8284
}
8385

8486
self.update_table_metadata(&updates)?;
@@ -106,7 +108,7 @@ impl<'a> Transaction<'a> {
106108

107109
/// Sets table to a new version.
108110
pub fn upgrade_table_version(mut self, format_version: FormatVersion) -> Result<Self> {
109-
let current_version = self.current_metadata.format_version();
111+
let current_version = self.current_table.metadata().format_version();
110112
match current_version.cmp(&format_version) {
111113
Ordering::Greater => {
112114
return Err(Error::new(
@@ -145,7 +147,8 @@ impl<'a> Transaction<'a> {
145147
};
146148
let mut snapshot_id = generate_random_id();
147149
while self
148-
.current_metadata
150+
.current_table
151+
.metadata()
149152
.snapshots()
150153
.any(|s| s.snapshot_id() == snapshot_id)
151154
{
@@ -239,7 +242,8 @@ impl<'a> FastAppendAction<'a> {
239242
if !self
240243
.snapshot_produce_action
241244
.tx
242-
.current_metadata
245+
.current_table
246+
.metadata()
243247
.default_spec
244248
.is_unpartitioned()
245249
{
@@ -250,9 +254,9 @@ impl<'a> FastAppendAction<'a> {
250254
}
251255

252256
let data_files = ParquetWriter::parquet_files_to_data_files(
253-
self.snapshot_produce_action.tx.base_table.file_io(),
257+
self.snapshot_produce_action.tx.current_table.file_io(),
254258
file_path,
255-
&self.snapshot_produce_action.tx.current_metadata,
259+
self.snapshot_produce_action.tx.current_table.metadata(),
256260
)
257261
.await?;
258262

@@ -274,7 +278,7 @@ impl<'a> FastAppendAction<'a> {
274278
let mut manifest_stream = self
275279
.snapshot_produce_action
276280
.tx
277-
.base_table
281+
.current_table
278282
.inspect()
279283
.manifests()
280284
.scan()
@@ -335,14 +339,19 @@ impl SnapshotProduceOperation for FastAppendOperation {
335339
&self,
336340
snapshot_produce: &SnapshotProduceAction<'_>,
337341
) -> Result<Vec<ManifestFile>> {
338-
let Some(snapshot) = snapshot_produce.tx.current_metadata.current_snapshot() else {
342+
let Some(snapshot) = snapshot_produce
343+
.tx
344+
.current_table
345+
.metadata()
346+
.current_snapshot()
347+
else {
339348
return Ok(vec![]);
340349
};
341350

342351
let manifest_list = snapshot
343352
.load_manifest_list(
344-
snapshot_produce.tx.base_table.file_io(),
345-
&snapshot_produce.tx.current_metadata,
353+
snapshot_produce.tx.current_table.file_io(),
354+
snapshot_produce.tx.current_table.metadata(),
346355
)
347356
.await?;
348357

@@ -456,7 +465,7 @@ impl<'a> SnapshotProduceAction<'a> {
456465
for data_file in data_files {
457466
Self::validate_partition_value(
458467
data_file.partition(),
459-
self.tx.current_metadata.default_partition_type(),
468+
self.tx.current_table.metadata().default_partition_type(),
460469
)?;
461470
if data_file.content_type() == DataContentType::Data {
462471
self.added_data_files.push(data_file);
@@ -470,13 +479,16 @@ impl<'a> SnapshotProduceAction<'a> {
470479
fn new_manifest_output(&mut self) -> Result<OutputFile> {
471480
let new_manifest_path = format!(
472481
"{}/{}/{}-m{}.{}",
473-
self.tx.current_metadata.location(),
482+
self.tx.current_table.metadata().location(),
474483
META_ROOT_PATH,
475484
self.commit_uuid,
476485
self.manifest_counter.next().unwrap(),
477486
DataFileFormat::Avro
478487
);
479-
self.tx.base_table.file_io().new_output(new_manifest_path)
488+
self.tx
489+
.current_table
490+
.file_io()
491+
.new_output(new_manifest_path)
480492
}
481493

482494
// Write manifest file for added data files and return the ManifestFile for ManifestList.
@@ -485,7 +497,7 @@ impl<'a> SnapshotProduceAction<'a> {
485497
added_data_files: Vec<DataFile>,
486498
) -> Result<ManifestFile> {
487499
let snapshot_id = self.snapshot_id;
488-
let format_version = self.tx.current_metadata.format_version();
500+
let format_version = self.tx.current_table.metadata().format_version();
489501
let content_type = {
490502
let mut data_num = 0;
491503
let mut delete_num = 0;
@@ -524,14 +536,15 @@ impl<'a> SnapshotProduceAction<'a> {
524536
self.new_manifest_output()?,
525537
Some(self.snapshot_id),
526538
self.key_metadata.clone(),
527-
self.tx.current_metadata.current_schema().clone(),
539+
self.tx.current_table.metadata().current_schema().clone(),
528540
self.tx
529-
.current_metadata
541+
.current_table
542+
.metadata()
530543
.default_partition_spec()
531544
.as_ref()
532545
.clone(),
533546
);
534-
if self.tx.current_metadata.format_version() == FormatVersion::V1 {
547+
if self.tx.current_table.metadata().format_version() == FormatVersion::V1 {
535548
builder.build_v1()
536549
} else {
537550
match content_type {
@@ -581,7 +594,7 @@ impl<'a> SnapshotProduceAction<'a> {
581594
fn generate_manifest_list_file_path(&self, attempt: i64) -> String {
582595
format!(
583596
"{}/{}/snap-{}-{}-{}.{}",
584-
self.tx.current_metadata.location(),
597+
self.tx.current_table.metadata().location(),
585598
META_ROOT_PATH,
586599
self.snapshot_id,
587600
attempt,
@@ -599,28 +612,28 @@ impl<'a> SnapshotProduceAction<'a> {
599612
let new_manifests = self
600613
.manifest_file(&snapshot_produce_operation, &process)
601614
.await?;
602-
let next_seq_num = self.tx.current_metadata.next_sequence_number();
615+
let next_seq_num = self.tx.current_table.metadata().next_sequence_number();
603616

604617
let summary = self.summary(&snapshot_produce_operation);
605618

606619
let manifest_list_path = self.generate_manifest_list_file_path(0);
607620

608-
let mut manifest_list_writer = match self.tx.current_metadata.format_version() {
621+
let mut manifest_list_writer = match self.tx.current_table.metadata().format_version() {
609622
FormatVersion::V1 => ManifestListWriter::v1(
610623
self.tx
611-
.base_table
624+
.current_table
612625
.file_io()
613626
.new_output(manifest_list_path.clone())?,
614627
self.snapshot_id,
615-
self.tx.current_metadata.current_snapshot_id(),
628+
self.tx.current_table.metadata().current_snapshot_id(),
616629
),
617630
FormatVersion::V2 => ManifestListWriter::v2(
618631
self.tx
619-
.base_table
632+
.current_table
620633
.file_io()
621634
.new_output(manifest_list_path.clone())?,
622635
self.snapshot_id,
623-
self.tx.current_metadata.current_snapshot_id(),
636+
self.tx.current_table.metadata().current_snapshot_id(),
624637
next_seq_num,
625638
),
626639
};
@@ -631,10 +644,10 @@ impl<'a> SnapshotProduceAction<'a> {
631644
let new_snapshot = Snapshot::builder()
632645
.with_manifest_list(manifest_list_path)
633646
.with_snapshot_id(self.snapshot_id)
634-
.with_parent_snapshot_id(self.tx.current_metadata.current_snapshot_id())
647+
.with_parent_snapshot_id(self.tx.current_table.metadata().current_snapshot_id())
635648
.with_sequence_number(next_seq_num)
636649
.with_summary(summary)
637-
.with_schema_id(self.tx.current_metadata.current_schema_id())
650+
.with_schema_id(self.tx.current_table.metadata().current_schema_id())
638651
.with_timestamp_ms(commit_ts)
639652
.build();
640653

@@ -653,11 +666,11 @@ impl<'a> SnapshotProduceAction<'a> {
653666
],
654667
vec![
655668
TableRequirement::UuidMatch {
656-
uuid: self.tx.current_metadata.uuid(),
669+
uuid: self.tx.current_table.metadata().uuid(),
657670
},
658671
TableRequirement::RefSnapshotIdMatch {
659672
r#ref: MAIN_BRANCH.to_string(),
660-
snapshot_id: self.tx.current_metadata.current_snapshot_id(),
673+
snapshot_id: self.tx.current_table.metadata().current_snapshot_id(),
661674
},
662675
],
663676
)?;
@@ -697,10 +710,20 @@ impl<'a> ReplaceSortOrderAction<'a> {
697710

698711
let requirements = vec![
699712
TableRequirement::CurrentSchemaIdMatch {
700-
current_schema_id: self.tx.current_metadata.current_schema().schema_id(),
713+
current_schema_id: self
714+
.tx
715+
.current_table
716+
.metadata()
717+
.current_schema()
718+
.schema_id(),
701719
},
702720
TableRequirement::DefaultSortOrderIdMatch {
703-
default_sort_order_id: self.tx.current_metadata.default_sort_order().order_id,
721+
default_sort_order_id: self
722+
.tx
723+
.current_table
724+
.metadata()
725+
.default_sort_order()
726+
.order_id,
704727
},
705728
];
706729

@@ -716,7 +739,8 @@ impl<'a> ReplaceSortOrderAction<'a> {
716739
) -> Result<Self> {
717740
let field_id = self
718741
.tx
719-
.current_metadata
742+
.current_table
743+
.metadata()
720744
.current_schema()
721745
.field_id_by_name(name)
722746
.ok_or_else(|| {

0 commit comments

Comments
 (0)