From 2ac704db41135f33c471e96798d55b6e3583b09e Mon Sep 17 00:00:00 2001 From: Harshil Valecha Date: Sat, 28 Nov 2026 12:09:45 +0530 Subject: [PATCH 1/2] 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 From ef0a0552152c646d05d908552e6243fd6e00e8a2 Mon Sep 17 00:00:00 2001 From: harshilv17 Date: Fri, 4 Dec 2026 13:02:28 +0530 Subject: [PATCH 2/2] Test delete-file conflicts under an older partition spec Cover a concurrent delete file written under a spec that is no longer current, so validation must project the filter onto that file's spec. Clarify the comment on why delete files are pruned by partition. --- pyiceberg/table/update/validate.py | 2 +- tests/table/test_commit_retry.py | 50 ++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/pyiceberg/table/update/validate.py b/pyiceberg/table/update/validate.py index 44bff0dc97..204bce4b95 100644 --- a/pyiceberg/table/update/validate.py +++ b/pyiceberg/table/update/validate.py @@ -299,7 +299,7 @@ def _added_delete_files( dfi = DeleteFileIndex() - # Delete files have no stats on the data columns, so only the partition can rule them out: + # Position deletes and deletion vectors usually have no stats on the data columns, so also prune by partition: # 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: diff --git a/tests/table/test_commit_retry.py b/tests/table/test_commit_retry.py index 1035f680e7..80beb8af89 100644 --- a/tests/table/test_commit_retry.py +++ b/tests/table/test_commit_retry.py @@ -568,11 +568,17 @@ def test_file_overwrite_validates_concurrent_file_delete( def commit_partition_position_delete( - table: Table, partition: Record, file_path: str, pos: int, row: dict[str, Any] | None = None + table: Table, + partition: Record, + file_path: str, + pos: int, + row: dict[str, Any] | None = None, + spec_id: int | None = None, ) -> None: """PyIceberg cannot write delete files, so commit one without path bounds, as other engines write them. If ``row`` is given, it is stored as the deleted row, which gives the delete file bounds on the data columns. + ``spec_id`` defaults to the current spec. """ import pyarrow as pa import pyarrow.parquet as pq @@ -613,7 +619,7 @@ def new_delete_manifest_writer(producer: _OverwriteFiles, spec: PartitionSpec) - file_size_in_bytes=len(table.io.new_input(delete_path)), lower_bounds=bounds, upper_bounds=bounds, - spec_id=table.spec().spec_id, + spec_id=table.spec().spec_id if spec_id is None else spec_id, ) with patch.object(_OverwriteFiles, "new_manifest_writer", new_delete_manifest_writer): @@ -718,6 +724,46 @@ def test_partition_filtered_write_validates_concurrent_position_delete_by_partit assert sorted(catalog.load_table(identifier).scan().to_arrow()["value"].to_pylist()) == expected, operation +def test_partition_filtered_write_validates_concurrent_position_delete_under_older_spec(catalog: Catalog) -> None: + """A delete file written under an older spec is scoped by that spec's partition, not the current one.""" + import pyarrow as pa + + from pyiceberg.partitioning import PartitionField, PartitionSpec + from pyiceberg.transforms import BucketTransform, 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 delete_partition, expect_conflict in (("a", True), ("b", False)): + identifier = f"default.concurrent_position_delete_older_spec_{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]})) + old_spec_id = table.spec().spec_id + target_file = next(task.file for task in table.scan().plan_files() if task.file.partition[0] == delete_partition) + + with table.update_spec() as update: + update.remove_field("category") + update.add_field("value", BucketTransform(4), "value_bucket") + assert table.spec().spec_id != old_spec_id + + writing_table = catalog.load_table(identifier) + + # Another engine commits a delete of a file written under the older identity spec. + commit_partition_position_delete(table, Record(delete_partition), target_file.file_path, pos=0, spec_id=old_spec_id) + + if expect_conflict: + with pytest.raises(ValidationException, match="Found new conflicting delete files"): + writing_table.delete("category == 'a'") + continue + + writing_table.delete("category == 'a'") + assert sorted(catalog.load_table(identifier).scan().to_arrow()["value"].to_pylist()) == [4] + + def _write_partition_a(table: Table, operation: str) -> None: import pyarrow as pa