Skip to content

Commit c97d6fc

Browse files
authored
Skip copying the table metadata when a transaction has nothing staged (#4302)
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 c4cd513 commit c97d6fc

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
@@ -277,6 +277,8 @@ def __init__(self, table: Table, autocommit: bool = False):
277277

278278
@property
279279
def table_metadata(self) -> TableMetadata:
280+
if not self._updates:
281+
return self._table.metadata
280282
return update_table_metadata(self._table.metadata, self._updates)
281283

282284
def __enter__(self) -> Transaction:

‎tests/table/test_init.py‎

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

11451145

1146+
def test_transaction_table_metadata_without_staged_updates(table_v2: Table) -> None:
1147+
transaction = table_v2.transaction()
1148+
assert transaction.table_metadata is table_v2.metadata
1149+
1150+
1151+
def test_transaction_table_metadata_with_staged_updates(table_v2: Table) -> None:
1152+
transaction = table_v2.transaction()
1153+
transaction.set_properties(test_a="test_a")
1154+
1155+
assert transaction.table_metadata.properties["test_a"] == "test_a"
1156+
assert transaction.table_metadata is not table_v2.metadata
1157+
assert "test_a" not in table_v2.metadata.properties
1158+
1159+
11461160
def test_update_metadata_add_snapshot(table_v2: Table) -> None:
11471161
new_snapshot = Snapshot(
11481162
snapshot_id=25,

‎tests/table/test_snapshots.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ def make_file() -> DataFile:
676676
)
677677

678678
txn = table_v2.transaction()
679+
# Stage an update, so every read calls update_table_metadata
680+
txn.set_properties(test_a="test_a")
679681

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

0 commit comments

Comments
 (0)