Skip to content

Commit 312035c

Browse files
committed
Skip copying the table metadata when a transaction has nothing staged
Transaction.table_metadata deep-copied the whole metadata on every read, even with nothing staged, so appends slowed down as the snapshot history grew.
1 parent 718c790 commit 312035c

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎pyiceberg/table/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ def __init__(self, table: Table, autocommit: bool = False):
275275

276276
@property
277277
def table_metadata(self) -> TableMetadata:
278+
if not self._updates:
279+
return self._table.metadata
278280
return update_table_metadata(self._table.metadata, self._updates)
279281

280282
def __enter__(self) -> Transaction:

‎tests/table/test_init.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,20 @@ def test_update_metadata_table_schema(table_v2: Table) -> None:
11031103
assert new_metadata.current_schema_id == 2
11041104

11051105

1106+
def test_transaction_table_metadata_without_staged_updates(table_v2: Table) -> None:
1107+
transaction = table_v2.transaction()
1108+
assert transaction.table_metadata is table_v2.metadata
1109+
1110+
1111+
def test_transaction_table_metadata_with_staged_updates(table_v2: Table) -> None:
1112+
transaction = table_v2.transaction()
1113+
transaction.set_properties(test_a="test_a")
1114+
1115+
assert transaction.table_metadata.properties["test_a"] == "test_a"
1116+
assert transaction.table_metadata is not table_v2.metadata
1117+
assert "test_a" not in table_v2.metadata.properties
1118+
1119+
11061120
def test_update_metadata_add_snapshot(table_v2: Table) -> None:
11071121
new_snapshot = Snapshot(
11081122
snapshot_id=25,

‎tests/table/test_snapshots.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ def make_file() -> DataFile:
674674
return DataFile.from_args(content=DataFileContent.DATA, record_count=1, file_size_in_bytes=1, partition=Record())
675675

676676
txn = table_v2.transaction()
677+
# Stage an update, so every read calls update_table_metadata
678+
txn.set_properties(test_a="test_a")
677679

678680
with mock.patch("pyiceberg.table.update_table_metadata", wraps=update_table_metadata) as spy:
679681
# _summary() cost must not scale with the number of data files

0 commit comments

Comments
 (0)