From 498e3247de220bd101b670efbd146c0980b826af Mon Sep 17 00:00:00 2001 From: Faisal Sharif Date: Sun, 6 Dec 2026 12:13:40 +0530 Subject: [PATCH] Skip already-deleted manifests and manifest lists in purge_table --- pyiceberg/catalog/__init__.py | 13 ++++- tests/catalog/test_base.py | 105 +++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/pyiceberg/catalog/__init__.py b/pyiceberg/catalog/__init__.py index 5af504f8ac..9d168de988 100644 --- a/pyiceberg/catalog/__init__.py +++ b/pyiceberg/catalog/__init__.py @@ -362,7 +362,12 @@ def delete_data_files(io: FileIO, manifests_to_delete: list[ManifestFile]) -> No """ deleted_files: dict[str, bool] = {} for manifest_file in manifests_to_delete: - for entry in manifest_file.fetch_manifest_entry(io, discard_deleted=False): + try: + entries = manifest_file.fetch_manifest_entry(io, discard_deleted=False) + except FileNotFoundError: + logger.debug(f"Manifest file already deleted: {manifest_file.manifest_path}") + continue + for entry in entries: path = entry.data_file.file_path if not deleted_files.get(path, False): try: @@ -1033,7 +1038,11 @@ def purge_table(self, identifier: str | Identifier) -> None: manifest_lists_to_delete = set() manifests_to_delete: list[ManifestFile] = [] for snapshot in metadata.snapshots: - manifests_to_delete += snapshot.manifests(io, encryption_manager) + try: + manifests_to_delete += snapshot.manifests(io, encryption_manager) + except FileNotFoundError: + logger.debug(f"Manifest list file already deleted: {snapshot.manifest_list}") + continue manifest_lists_to_delete.add(snapshot.manifest_list) manifest_paths_to_delete = {manifest.manifest_path for manifest in manifests_to_delete} diff --git a/tests/catalog/test_base.py b/tests/catalog/test_base.py index 8e1891a908..ab4259eaab 100644 --- a/tests/catalog/test_base.py +++ b/tests/catalog/test_base.py @@ -17,13 +17,15 @@ # pylint:disable=redefined-outer-name +import logging from collections.abc import Generator from pathlib import PosixPath +from unittest.mock import MagicMock import pytest from pytest_mock import MockerFixture -from pyiceberg.catalog import Catalog, load_catalog +from pyiceberg.catalog import Catalog, delete_data_files, load_catalog from pyiceberg.catalog.memory import InMemoryCatalog from pyiceberg.io import WAREHOUSE from pyiceberg.schema import Schema @@ -101,3 +103,104 @@ def test_in_memory_catalog_context_manager(self, catalog: InMemoryCatalog) -> No # InMemoryCatalog inherits close from SqlCatalog, so engine should be disposed assert hasattr(cat, "engine") + + +class TestPurgeTableMissingManifests: + """Regression tests for skipping already-deleted manifests/manifest lists during purge.""" + + def test_delete_data_files_skips_missing_manifest(self, caplog: pytest.LogCaptureFixture) -> None: + io = MagicMock() + + missing_manifest = MagicMock() + missing_manifest.manifest_path = "s3://bucket/missing-manifest.avro" + missing_manifest.fetch_manifest_entry.side_effect = FileNotFoundError("already removed") + + live_entry = MagicMock() + live_entry.data_file.file_path = "s3://bucket/data-1.parquet" + live_manifest = MagicMock() + live_manifest.manifest_path = "s3://bucket/live-manifest.avro" + live_manifest.fetch_manifest_entry.return_value = [live_entry] + + with caplog.at_level(logging.DEBUG, logger="pyiceberg.catalog"): + delete_data_files(io, [missing_manifest, live_manifest]) + + io.delete.assert_called_once_with("s3://bucket/data-1.parquet") + assert "s3://bucket/missing-manifest.avro" in caplog.text + + def test_delete_data_files_propagates_non_file_not_found_error(self) -> None: + io = MagicMock() + denied_manifest = MagicMock() + denied_manifest.manifest_path = "s3://bucket/denied-manifest.avro" + denied_manifest.fetch_manifest_entry.side_effect = PermissionError("access denied") + + with pytest.raises(PermissionError, match="access denied"): + delete_data_files(io, [denied_manifest]) + + def test_purge_table_skips_missing_manifest_list_and_deletes_remaining_files( + self, catalog: InMemoryCatalog, mocker: MockerFixture, caplog: pytest.LogCaptureFixture + ) -> None: + io = MagicMock() + mocker.patch("pyiceberg.catalog.load_file_io", return_value=io) + + missing_snapshot = MagicMock() + missing_snapshot.manifest_list = "s3://bucket/snap-missing.avro" + missing_snapshot.manifests.side_effect = FileNotFoundError("manifest list gone") + + live_entry = MagicMock() + live_entry.data_file.file_path = "s3://bucket/data-live.parquet" + live_manifest = MagicMock() + live_manifest.manifest_path = "s3://bucket/manifest-live.avro" + live_manifest.fetch_manifest_entry.return_value = [live_entry] + + live_snapshot = MagicMock() + live_snapshot.manifest_list = "s3://bucket/snap-live.avro" + live_snapshot.manifests.return_value = [live_manifest] + + prev_log = MagicMock() + prev_log.metadata_file = "s3://bucket/v1.metadata.json" + + table = MagicMock() + table.metadata_location = "s3://bucket/v2.metadata.json" + table.encryption_manager = None + table.metadata.snapshots = [missing_snapshot, live_snapshot] + table.metadata.metadata_log = [prev_log] + + mocker.patch.object(catalog, "load_table", return_value=table) + drop_mock = mocker.patch.object(catalog, "drop_table") + + with caplog.at_level(logging.DEBUG, logger="pyiceberg.catalog"): + catalog.purge_table(("db", "tbl")) + + drop_mock.assert_called_once_with(("db", "tbl")) + deleted_paths = {call.args[0] for call in io.delete.call_args_list} + assert deleted_paths == { + "s3://bucket/data-live.parquet", + "s3://bucket/manifest-live.avro", + "s3://bucket/snap-live.avro", + "s3://bucket/v1.metadata.json", + "s3://bucket/v2.metadata.json", + } + assert "s3://bucket/snap-missing.avro" not in deleted_paths + assert "s3://bucket/snap-missing.avro" in caplog.text + + def test_purge_table_propagates_non_file_not_found_manifest_list_error( + self, catalog: InMemoryCatalog, mocker: MockerFixture + ) -> None: + io = MagicMock() + mocker.patch("pyiceberg.catalog.load_file_io", return_value=io) + + denied_snapshot = MagicMock() + denied_snapshot.manifest_list = "s3://bucket/snap-denied.avro" + denied_snapshot.manifests.side_effect = PermissionError("forbidden") + + table = MagicMock() + table.metadata_location = "s3://bucket/v1.metadata.json" + table.encryption_manager = None + table.metadata.snapshots = [denied_snapshot] + table.metadata.metadata_log = [] + + mocker.patch.object(catalog, "load_table", return_value=table) + mocker.patch.object(catalog, "drop_table") + + with pytest.raises(PermissionError, match="forbidden"): + catalog.purge_table(("db", "tbl"))