From 2ac704db41135f33c471e96798d55b6e3583b09e Mon Sep 17 00:00:00 2001 From: Harshil Valecha Date: Sat, 28 Nov 2026 12:09:45 +0530 Subject: [PATCH] Scope concurrent delete-file conflicts by partition Commit validation checked concurrent delete files only against the data filter's column metrics, which position deletes and deletion vectors don't have for data columns, so any delete file committed in another partition failed a partition-filtered delete or overwrite. Project the data filter onto each delete file's spec, as Java's DeleteFileIndex does, and skip delete files whose partition can't match. --- pyiceberg/table/update/snapshot.py | 3 -- pyiceberg/table/update/validate.py | 27 ++++++++++++++++-- tests/table/test_commit_retry.py | 45 ++++++++++++++++++++++++++++++ tests/table/test_validate.py | 1 + 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/pyiceberg/table/update/snapshot.py b/pyiceberg/table/update/snapshot.py index 7a37b76287..6f249be09d 100644 --- a/pyiceberg/table/update/snapshot.py +++ b/pyiceberg/table/update/snapshot.py @@ -515,9 +515,6 @@ def _validate_concurrent_changes(self) -> None: _validate_added_data_files(table, catalog_head, conflict_detection_filter, starting_snapshot, self._case_sensitive) if self._predicate != AlwaysFalse(): - # partition_set is None, so any concurrently committed delete file counts as - # a conflict even in unrelated partitions. Scope this via partition projection - # (as Java does) when delete-file write support lands. _validate_no_new_delete_files( table, catalog_head, conflict_detection_filter, None, starting_snapshot, self._case_sensitive ) diff --git a/pyiceberg/table/update/validate.py b/pyiceberg/table/update/validate.py index 66f5ddb451..44bff0dc97 100644 --- a/pyiceberg/table/update/validate.py +++ b/pyiceberg/table/update/validate.py @@ -15,11 +15,16 @@ # specific language governing permissions and limitations # under the License. from collections import defaultdict -from collections.abc import Iterator +from collections.abc import Callable, Iterator from pyiceberg.exceptions import ValidationException from pyiceberg.expressions import BooleanExpression -from pyiceberg.expressions.visitors import ROWS_CANNOT_MATCH, _InclusiveMetricsEvaluator +from pyiceberg.expressions.visitors import ( + ROWS_CANNOT_MATCH, + _InclusiveMetricsEvaluator, + expression_evaluator, + inclusive_projection, +) from pyiceberg.manifest import ( INITIAL_SEQUENCE_NUMBER, DataFile, @@ -32,7 +37,7 @@ from pyiceberg.table import Table from pyiceberg.table.delete_file_index import DeleteFileIndex from pyiceberg.table.snapshots import Operation, Snapshot, ancestors_between -from pyiceberg.typedef import Record +from pyiceberg.typedef import KeyDefaultDict, Record VALIDATE_DATA_FILES_EXIST_OPERATIONS: set[Operation] = {Operation.OVERWRITE, Operation.REPLACE, Operation.DELETE} VALIDATE_ADDED_DATA_FILES_OPERATIONS: set[Operation] = {Operation.APPEND, Operation.OVERWRITE} @@ -294,8 +299,24 @@ def _added_delete_files( dfi = DeleteFileIndex() + # Delete files have no stats on the data columns, so only the partition can rule them out: + # project the data filter onto each spec, as Java's DeleteFileIndex does. + partition_evaluators: KeyDefaultDict[int, Callable[[Record], bool]] | None = None + if data_filter is not None: + row_filter = data_filter + + def _partition_evaluator(spec_id: int) -> Callable[[Record], bool]: + spec = table.specs()[spec_id] + partition_schema = Schema(*spec.partition_type(table.schema()).fields) + projected = inclusive_projection(table.schema(), spec, case_sensitive)(row_filter) + return expression_evaluator(partition_schema, projected, case_sensitive) + + partition_evaluators = KeyDefaultDict(_partition_evaluator) + for manifest in manifests: for entry in manifest.fetch_manifest_entry(table.io, discard_deleted=True): + if partition_evaluators is not None and not partition_evaluators[entry.data_file.spec_id](entry.data_file.partition): + continue if _filter_manifest_entries( entry, snapshot_ids, data_filter, partition_set, ManifestEntryStatus.ADDED, table.schema(), case_sensitive ): diff --git a/tests/table/test_commit_retry.py b/tests/table/test_commit_retry.py index 22f743c1c8..1035f680e7 100644 --- a/tests/table/test_commit_retry.py +++ b/tests/table/test_commit_retry.py @@ -682,6 +682,51 @@ def test_partial_delete_validates_concurrent_delete_outside_the_filter(catalog: assert catalog.load_table(identifier).scan().to_arrow()["k"].to_pylist() == [1, 3] +def test_partition_filtered_write_validates_concurrent_position_delete_by_partition(catalog: Catalog) -> None: + """A retried write filtered to one partition must only conflict with a concurrent delete file in that partition.""" + import pyarrow as pa + + from pyiceberg.partitioning import PartitionField, PartitionSpec + from pyiceberg.transforms import IdentityTransform + + catalog.create_namespace("default") + schema = Schema( + NestedField(1, "category", StringType(), required=False), + NestedField(2, "value", LongType(), required=False), + ) + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="category")) + + for operation in ("delete", "overwrite"): + for delete_partition, expect_conflict in (("a", True), ("b", False)): + identifier = f"default.concurrent_position_delete_{operation}_{delete_partition}" + table = catalog.create_table(identifier, schema=schema, partition_spec=spec, properties={"format-version": "2"}) + table.append(pa.table({"category": ["a", "a", "b", "b"], "value": [1, 2, 3, 4]})) + target_file = next(task.file for task in table.scan().plan_files() if task.file.partition[0] == delete_partition) + + writing_table = catalog.load_table(identifier) + + # Another engine commits a merge-on-read delete of the first row of the file in `delete_partition`. + commit_partition_position_delete(table, Record(delete_partition), target_file.file_path, pos=0) + + if expect_conflict: + with pytest.raises(ValidationException, match="Found new conflicting delete files"): + _write_partition_a(writing_table, operation) + continue + + _write_partition_a(writing_table, operation) + expected = [4] if operation == "delete" else [4, 10] + assert sorted(catalog.load_table(identifier).scan().to_arrow()["value"].to_pylist()) == expected, operation + + +def _write_partition_a(table: Table, operation: str) -> None: + import pyarrow as pa + + if operation == "delete": + table.delete("category == 'a'") + else: + table.overwrite(pa.table({"category": ["a"], "value": [10]}), overwrite_filter="category == 'a'") + + def test_concurrent_overwrite_append_retries_successfully(catalog: Catalog) -> None: """Append after a concurrent overwrite should succeed via retry.""" catalog.create_namespace("default") diff --git a/tests/table/test_validate.py b/tests/table/test_validate.py index cc4f3ba3f9..fd83dc4da0 100644 --- a/tests/table/test_validate.py +++ b/tests/table/test_validate.py @@ -527,6 +527,7 @@ def test_added_delete_files_case_insensitive_filter( content=DataFileContent.POSITION_DELETES, file_path="s3://dummy/path", record_count=1, + partition=Record(1), ) mock_delete_file.spec_id = 0