From bbfc303393de5fdac4c4ff2d1a7fc4fb52e4bfed Mon Sep 17 00:00:00 2001 From: Sreesh Maheshwar Date: Thu, 15 Oct 2026 20:32:23 +0100 Subject: [PATCH] Write manifests to the transaction's metadata location Snapshot producers wrote manifests and manifest lists using the location provider of the table the transaction started from, while data files use the transaction's metadata. A transaction that changes the location or write.metadata.path before writing would split its files between the old and new paths. Use the transaction's metadata for both, as Java does. Co-Authored-By: Claude Opus 5.5 --- pyiceberg/table/update/snapshot.py | 12 ++++++++---- tests/catalog/test_catalog_behaviors.py | 26 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/pyiceberg/table/update/snapshot.py b/pyiceberg/table/update/snapshot.py index 7a37b76287..918c96f686 100644 --- a/pyiceberg/table/update/snapshot.py +++ b/pyiceberg/table/update/snapshot.py @@ -57,6 +57,7 @@ ) from pyiceberg.partitioning import PartitionSpec from pyiceberg.schema import Schema +from pyiceberg.table.locations import LocationProvider, load_location_provider from pyiceberg.table.refs import MAIN_BRANCH, SnapshotRef, SnapshotRefType from pyiceberg.table.snapshots import ( Operation, @@ -344,8 +345,7 @@ def _commit(self) -> UpdatesAndRequirements: attempt=0, commit_uuid=self.commit_uuid, ) - location_provider = self._transaction._table.location_provider() - manifest_list_file_path = location_provider.new_metadata_location(file_name) + manifest_list_file_path = self._location_provider().new_metadata_location(file_name) self._written_manifest_lists.append(manifest_list_file_path) with write_manifest_list( @@ -422,12 +422,16 @@ def new_manifest_writer(self, spec: PartitionSpec) -> ManifestWriter: ) def new_manifest_output(self) -> OutputFile: - location_provider = self._transaction._table.location_provider() file_name = _new_manifest_file_name(num=next(self._manifest_num_counter), commit_uuid=self.commit_uuid) - file_path = location_provider.new_metadata_location(file_name) + file_path = self._location_provider().new_metadata_location(file_name) self._written_manifests.append(file_path) return self._io.new_output(file_path) + def _location_provider(self) -> LocationProvider: + # Use the metadata of the transaction, as for data files, in case it changed the location or properties + table_metadata = self._transaction.table_metadata + return load_location_provider(table_location=table_metadata.location, table_properties=table_metadata.properties) + def fetch_manifest_entry(self, manifest: ManifestFile, discard_deleted: bool = True) -> list[ManifestEntry]: return manifest.fetch_manifest_entry(io=self._io, discard_deleted=discard_deleted) diff --git a/tests/catalog/test_catalog_behaviors.py b/tests/catalog/test_catalog_behaviors.py index 82990c1033..7f4fa172c4 100644 --- a/tests/catalog/test_catalog_behaviors.py +++ b/tests/catalog/test_catalog_behaviors.py @@ -1013,6 +1013,32 @@ def test_union_by_name_case_insensitive_duplicates(catalog: Catalog) -> None: assert catalog.load_table("default.nested_variants").schema() == nested_schema +def test_transaction_writes_manifests_to_staged_metadata_path( + catalog: Catalog, + test_table_identifier: Identifier, + table_schema_simple: Schema, + warehouse: Path, +) -> None: + namespace = Catalog.namespace_from(test_table_identifier) + catalog.create_namespace(namespace) + table = catalog.create_table(identifier=test_table_identifier, schema=table_schema_simple) + metadata_path = f"file://{warehouse}/custom/path" + df = pa.Table.from_pydict( + {"foo": ["a"], "bar": [1], "baz": [True]}, + schema=schema_to_pyarrow(table_schema_simple), + ) + + with table.transaction() as txn: + txn.set_properties({TableProperties.WRITE_METADATA_PATH: metadata_path}) + txn.append(df) + + snapshot = table.current_snapshot() + assert snapshot is not None + assert snapshot.manifest_list.startswith(metadata_path) + assert all(manifest.manifest_path.startswith(metadata_path) for manifest in snapshot.manifests(table.io)) + assert table.metadata_location.startswith(metadata_path) + + @pytest.mark.parametrize("format_version", [1, 2]) def test_write_and_evolve(catalog: Catalog, format_version: int) -> None: identifier = f"default.arrow_write_data_and_evolve_schema_v{format_version}"