Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pyiceberg/table/update/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,13 @@ def _existing_manifests(self) -> list[ManifestFile]:
manifest_evaluators: dict[int, Callable[[ManifestFile], bool]] = KeyDefaultDict(self._build_manifest_evaluator)
if snapshot := self._transaction.table_metadata.snapshot_by_name(name=self._target_branch):
for manifest_file in snapshot.manifests(io=self._io):
if not (
manifest_file.has_added_files()
or manifest_file.has_existing_files()
or manifest_file.added_snapshot_id == self._snapshot_id
):
continue

# Manifest does not contain rows that match the files to delete partitions
if not manifest_evaluators[manifest_file.partition_spec_id](manifest_file):
existing_files.append(manifest_file)
Expand Down Expand Up @@ -862,6 +869,8 @@ def _deleted_entries(self) -> list[ManifestEntry]:
manifest_evaluators: dict[int, Callable[[ManifestFile], bool]] = KeyDefaultDict(self._build_manifest_evaluator)

def _get_entries(manifest: ManifestFile) -> list[ManifestEntry]:
if not (manifest.has_added_files() or manifest.has_existing_files()):
return []
if not manifest_evaluators[manifest.partition_spec_id](manifest):
return []

Expand Down
106 changes: 104 additions & 2 deletions tests/table/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import re
import uuid
from pathlib import Path
from typing import cast
from typing import Any, cast
from urllib.parse import urlparse

import pyarrow as pa
Expand All @@ -28,7 +28,7 @@
from pyiceberg.catalog import Catalog
from pyiceberg.exceptions import ValidationException
from pyiceberg.io.pyarrow import _dataframe_to_data_files
from pyiceberg.manifest import DataFile, DataFileContent, ManifestContent, ManifestFile
from pyiceberg.manifest import DataFile, DataFileContent, ManifestContent, ManifestEntry, ManifestFile
from pyiceberg.partitioning import PartitionField, PartitionSpec
from pyiceberg.schema import Schema
from pyiceberg.table import Table
Expand Down Expand Up @@ -844,3 +844,105 @@ def test_overwrite_rejects_explicit_delete_without_parent_snapshot(
with empty.transaction() as tx:
with tx.update_snapshot().overwrite() as overwrite:
overwrite.delete_data_file(stale_file)


def test_overwrite_drops_previous_deleted_only_manifests(overwrite_table: Table, arrow_table_simple: pa.Table) -> None:
for i in range(5):
current_files = [task.file for task in overwrite_table.scan().plan_files()]
replacement = _write_data_file(overwrite_table, arrow_table_simple.slice(i % len(arrow_table_simple), 1))

with overwrite_table.transaction() as tx:
with tx.update_snapshot().overwrite() as overwrite:
for data_file in current_files:
overwrite.delete_data_file(data_file)
overwrite.append_data_file(replacement)

snapshot = overwrite_table.current_snapshot()
assert snapshot is not None
manifests = snapshot.manifests(overwrite_table.io)
live_manifests = [m for m in manifests if m.has_added_files() or m.has_existing_files()]
deleted_only_manifests = [m for m in manifests if not (m.has_added_files() or m.has_existing_files())]

assert len(manifests) == 2
assert len(live_manifests) == 1
assert len(deleted_only_manifests) == 1
assert deleted_only_manifests[0].added_snapshot_id == snapshot.snapshot_id
assert _total_data_file_count(overwrite_table) == 1


def test_overwrite_drops_deleted_only_manifests_across_partitions(catalog: Catalog) -> None:
catalog.create_namespace("default")
schema = Schema(
NestedField(1, "part", StringType(), required=False),
NestedField(2, "val", IntegerType(), required=False),
)
spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="part"))
table = catalog.create_table("default.partitioned_overwrite", schema=schema, partition_spec=spec)

table.append(pa.table({"part": ["a", "b"], "val": pa.array([1, 2], type=pa.int32())}))

# Overwrite partition "a" -> leaves a deleted-only manifest for partition "a"
file_a = next(task.file for task in table.scan(row_filter="part == 'a'").plan_files())
new_a = _write_data_file(table, pa.table({"part": ["a"], "val": pa.array([10], type=pa.int32())}))
with table.transaction() as tx:
with tx.update_snapshot().overwrite() as overwrite:
overwrite.delete_data_file(file_a)
overwrite.append_data_file(new_a)

# Overwrite partition "b" -> must drop partition "a"'s prior deleted-only manifest while keeping "a"'s live manifest
file_b = next(task.file for task in table.scan(row_filter="part == 'b'").plan_files())
new_b = _write_data_file(table, pa.table({"part": ["b"], "val": pa.array([20], type=pa.int32())}))
with table.transaction() as tx:
with tx.update_snapshot().overwrite() as overwrite:
overwrite.delete_data_file(file_b)
overwrite.append_data_file(new_b)

snapshot = table.current_snapshot()
assert snapshot is not None
manifests = snapshot.manifests(table.io)
deleted_only_manifests = [m for m in manifests if not (m.has_added_files() or m.has_existing_files())]

assert len(deleted_only_manifests) == 1
assert deleted_only_manifests[0].added_snapshot_id == snapshot.snapshot_id
assert sorted(table.scan().to_arrow().to_pylist(), key=lambda r: r["part"]) == [
{"part": "a", "val": 10},
{"part": "b", "val": 20},
]


def test_overwrite_skips_reading_deleted_only_manifests(
overwrite_table: Table, arrow_table_simple: pa.Table, monkeypatch: pytest.MonkeyPatch
) -> None:
first_file = next(iter(overwrite_table.scan().plan_files())).file
second_file = _write_data_file(overwrite_table, arrow_table_simple.slice(0, 1))
with overwrite_table.transaction() as tx:
with tx.update_snapshot().overwrite() as overwrite:
overwrite.delete_data_file(first_file)
overwrite.append_data_file(second_file)

first_snapshot = overwrite_table.current_snapshot()
assert first_snapshot is not None
deleted_only_paths = {
m.manifest_path
for m in first_snapshot.manifests(overwrite_table.io)
if not (m.has_added_files() or m.has_existing_files())
}
assert len(deleted_only_paths) == 1

fetched_paths: list[str] = []
original_fetch = ManifestFile.fetch_manifest_entry

def tracking_fetch(self: ManifestFile, *args: Any, **kwargs: Any) -> list[ManifestEntry]:
fetched_paths.append(self.manifest_path)
return original_fetch(self, *args, **kwargs)

monkeypatch.setattr(ManifestFile, "fetch_manifest_entry", tracking_fetch)

third_file = _write_data_file(overwrite_table, arrow_table_simple.slice(1, 1))
with overwrite_table.transaction() as tx:
with tx.update_snapshot().overwrite() as overwrite:
overwrite.delete_data_file(second_file)
overwrite.append_data_file(third_file)

assert deleted_only_paths.isdisjoint(fetched_paths)
assert overwrite_table.scan().to_arrow()["foo"].to_pylist() == ["b"]
Loading