harshilv17 opened a new issue, #4507:
URL: https://github.com/apache/iceberg-python/issues/4507

   ### Apache Iceberg version
   
   0.12.0 (latest release) — also reproduced on main (7864d4c3)
   
   ### Please describe the bug 🐞
   
   When a `delete()` or `overwrite(..., overwrite_filter=...)` is retried after 
a concurrent commit, `_validate_concurrent_changes` checks for new delete files 
with `_validate_no_new_delete_files(table, catalog_head, 
conflict_detection_filter, None, starting_snapshot, ...)` 
(`pyiceberg/table/update/snapshot.py`, 
`_SnapshotProducer._validate_concurrent_changes`).
   
   With `partition_set=None`, `_added_delete_files` 
(`pyiceberg/table/update/validate.py`) can only rule out a delete file through 
`_InclusiveMetricsEvaluator` on the file's column stats. Position delete files 
and deletion vectors have no stats on data columns: a position delete only has 
bounds for `file_path`/`pos`. So every position delete committed in the 
validation window counts as a conflict, whatever partition it is in.
   
   Java scopes this by partition. 
`MergingSnapshotProducer.validateNoNewDeleteFiles(base, startingSnapshotId, 
dataFilter, parent)` builds a `DeleteFileIndex` with `.filterData(dataFilter)`. 
`DeleteFileIndex.Builder#deleteManifestReaders` projects the filter onto each 
spec (`Projections.inclusive(spec, caseSensitive).project(dataFilter)`), prunes 
delete manifests with a `ManifestEvaluator`, and filters entries with 
`ManifestReader.filterPartitions(...)`. A position delete in partition `b` is 
therefore not a conflict for a delete of `category = 'a'`.
   
   This was already raised in a review comment on #3320 
(https://github.com/apache/iceberg-python/pull/3320#discussion_r3610876958). It 
was deferred with a code comment ("Scope this via partition projection (as Java 
does) when delete-file write support lands"). PyIceberg doesn't need to write 
delete files for this to happen, though. Any table shared with a Spark or Flink 
merge-on-read writer gets position deletes, so on such a table a PyIceberg 
`delete()`/`overwrite()` of one partition fails whenever another engine commits 
a MoR delete in any other partition during the retry window.
   
   **Reproduction** (uses the existing `commit_partition_position_delete` 
helper in `tests/table/test_commit_retry.py`):
   
   ```python
   schema = Schema(NestedField(1, "category", StringType()), NestedField(2, 
"value", LongType()))
   spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, 
transform=IdentityTransform(), name="category"))
   table = catalog.create_table("default.t", schema=schema, 
partition_spec=spec, properties={"format-version": "2"})
   table.append(pa.table({"category": ["a", "a", "b", "b"], "value": [1, 2, 3, 
4]}))
   file_b = next(t.file for t in table.scan().plan_files() if 
t.file.partition[0] == "b")
   
   writer = catalog.load_table("default.t")
   # another engine commits a position delete for a row in partition b
   commit_partition_position_delete(table, Record("b"), file_b.file_path, pos=0)
   
   writer.delete("category == 'a'")  # retries, then fails
   ```
   
   ```
   pyiceberg.exceptions.ValidationException: Found new conflicting delete files 
that can apply to records
   matching EqualTo(term=Reference(name='category'), literal=literal('a')): 
['.../pos-delete-....parquet']
   ```
   
   Expected: the delete succeeds, as it would in Java, because the delete file 
is in partition `b` and cannot apply to rows matching `category = 'a'`. The 
same happens with `overwrite(df, overwrite_filter="category == 'a'")`. With the 
position delete in partition `a`, the conflict is still raised, which is 
correct.
   
   **Proposed fix**: in `_added_delete_files`, when a `data_filter` is given, 
project it onto each entry's spec with `inclusive_projection` and drop entries 
whose partition can't match, using a per-spec `expression_evaluator`. This 
mirrors `DataScan._build_partition_evaluator`. I have a small patch with tests 
(same partition still conflicts, other partition succeeds, for both `delete` 
and `overwrite`). The existing `tests/table` suite passes once one mock delete 
file in `test_validate.py` gets a real partition value: it currently has 
`partition=None` on a table partitioned by `x`.
   
   ### Willingness to contribute
   
   - [x] I can contribute a fix for this bug independently
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to