karubian opened a new issue, #19237:
URL: https://github.com/apache/iceberg/issues/19237

   ### Apache Iceberg version
   
   1.12.0 (latest release)
   
   ### Query engine
   
   Spark
   
   ### Please describe the bug 🐞
   
   `rewrite_position_delete_files` commits every delete file it writes for a 
group at the highest data sequence number among the group's delete files. A 
position delete applies to a data file only when the data file's data sequence 
number is less than or equal to the delete's, [Scan 
Planning](https://iceberg.apache.org/spec/#scan-planning). Take a delete at 3 
that names a live data file at 5, in a group that also holds a delete at 6. The 
rewritten delete is committed at 6 and starts to apply, since 3 < 5 ≤ 6, and 
the row it names disappears.
   
   Plain Spark SQL builds such a table:
   
   1. A metadata delete removes a data file, and its v2 position delete stays 
live.
   2. `add_files` registers the same file again at its old path, at a higher 
data sequence number.
   
   The spec advises against this: "It is not recommended to add a deleted file 
back to a table", 
[spec](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/format/spec.md?plain=1#L709),
 a note under Deleting Files on main. Spark's `add_files` accepts it, though. 
Once the file is live again, scan planning leaves the older delete out. A 
rewrite should not change that: `replace` snapshots change files "without 
changing table data", [Snapshots](https://iceberg.apache.org/spec/#snapshots), 
and `RewriteFiles` requires the new state to be "logically equivalent to the 
original table state", 
[RewriteFiles](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/api/src/main/java/org/apache/iceberg/RewriteFiles.java#L36-L37).
   
   On Spark 3.5.10, with `/tmp/wh` empty and the statements below saved as 
`rewrite.sql`:
   
   ```
   spark-sql --packages 
org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.12.0 \
     --conf 
spark.sql.extensions=org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions
 \
     --conf spark.sql.catalog.local=org.apache.iceberg.spark.SparkCatalog \
     --conf spark.sql.catalog.local.type=hadoop \
     --conf spark.sql.catalog.local.warehouse=file:/tmp/wh \
     --conf spark.sql.defaultCatalog=local \
     -f rewrite.sql
   ```
   
   ```sql
   CREATE TABLE db.t (id int, grp int) USING iceberg TBLPROPERTIES (
     'format-version' = '2', 'write.delete.mode' = 'merge-on-read', 
'write.data.path' = 'file:/tmp/wh/a');
   -- data file A, data sequence number 1
   INSERT INTO db.t SELECT /*+ REPARTITION(1) */ * FROM VALUES (1, 0), (2, 0), 
(3, 0);
   ALTER TABLE db.t SET TBLPROPERTIES ('write.data.path' = 'file:/tmp/wh/b');
   -- data file B, 2
   INSERT INTO db.t SELECT /*+ REPARTITION(1) */ * FROM VALUES (4, 1), (5, 1);
   -- position deletes at 3, one of them for A at position 0
   DELETE FROM db.t WHERE id IN (1, 4);
   -- metadata delete of A at 4, which leaves A's delete file live
   DELETE FROM db.t WHERE grp = 0;
   -- A again, at 5
   CALL system.add_files(table => 'db.t', source_table => 
'`parquet`.`file:/tmp/wh/a`');
   -- 1, 2, 3, 5: the delete at 3 does not apply to A at 5
   SELECT id FROM db.t ORDER BY id;
   -- B's deletes, now at 6
   DELETE FROM db.t WHERE id = 5;
   -- 1, 2, 3
   SELECT id FROM db.t ORDER BY id;
   SELECT data_file.content, sequence_number, data_file.file_path FROM 
db.t.entries WHERE status != 2 ORDER BY 1, 2;
   CALL system.rewrite_position_delete_files(table => 'db.t', options => 
map('rewrite-all', 'true'));
   SELECT data_file.content, sequence_number, data_file.file_path FROM 
db.t.entries WHERE status != 2 ORDER BY 1, 2;
   -- 2, 3
   SELECT id FROM db.t ORDER BY id;
   ```
   
   The two `entries` queries and the rewrite give:
   
   ```
   0  2  file:/tmp/wh/b/00000-6-...-0-00001.parquet          -- B
   0  5  file:/tmp/wh/a/00000-3-...-0-00001.parquet          -- A
   1  3  file:/tmp/wh/b/00000-8-...-00001-deletes.parquet    -- the delete for 
A's position 0
   1  6  file:/tmp/wh/b/00000-419-...-00001-deletes.parquet  -- B's deletes
   2  2  2713  2713                                          -- rewritten 2, 
added 2
   0  2  file:/tmp/wh/b/00000-6-...-0-00001.parquet
   0  5  file:/tmp/wh/a/00000-3-...-0-00001.parquet
   1  6  file:/tmp/wh/b/00000-427-...-00001-deletes.parquet
   1  6  file:/tmp/wh/b/00000-427-...-00002-deletes.parquet
   ```
   
   The `file:` scheme in the paths matters: `add_files` records it, so the 
delete names the re-added file only if Spark wrote the same spelling.
   
   - **Other runs:** the same SQL loses row 1 on 1.10.0, and on 1.12.0 with 
`'format-version' = '3'` set before the rewrite, where the deletion vector 
written for A also takes 6.
   - **PyIceberg:** in a run of the same steps through PySpark on 1.12.0, 
PyIceberg 0.12.0 read the same rows as Spark before and after the rewrite.
   - **Main:** it commits rewritten deletes the same way, 
[RewritePositionDeletesCommitManager](https://github.com/apache/iceberg/blob/65d832d7a8da4dc33ae8eb5e0b249c49c5b61301/core/src/main/java/org/apache/iceberg/actions/RewritePositionDeletesCommitManager.java#L73).
   
   Where it happens:
   
   - **The commit manager** calls `rewriteFiles.addFile(file, 
group.maxRewrittenDataSequenceNumber())` for every new delete file, 
[RewritePositionDeletesCommitManager](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/core/src/main/java/org/apache/iceberg/actions/RewritePositionDeletesCommitManager.java#L64-L78).
   - **The group** computes that max over its delete files, 
[RewritePositionDeletesGroup](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/core/src/main/java/org/apache/iceberg/actions/RewritePositionDeletesGroup.java#L53-L54).
   - **The runner** keeps a delete row by its `file_path` alone within the 
group's partition, 
[SparkRewritePositionDeleteRunner](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/SparkRewritePositionDeleteRunner.java#L113-L116).
   
   `RewriteFiles` says the max keeps "the set of applicable delete records" 
unchanged, 
[L100-L115](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/api/src/main/java/org/apache/iceberg/RewriteFiles.java#L100-L115).
 That holds unless a replaced delete names a live data file whose data sequence 
number lies above the delete's own and at or below the group's max. The max 
dates from #7651; before it, rewritten deletes took "the current data sequence 
number of the table", #7624. #4988 raised this case in 2022 and proposed a spec 
rule against it; it was closed unmerged.
   
   The rewrite could drop the delete rows whose live data file has a higher 
data sequence number than their own delete file, since they do not apply to it. 
On v2, it could instead keep the deletes of each source data sequence number in 
their own output file, and commit each file at that number. On v3 a data file 
has at most one deletion vector, so the rewrite has to drop such positions 
before it merges them.
   
   This report was drafted with an AI assistant, which also ran the 
reproduction on 1.12.0 and 1.10.0 and checked the linked lines.
   
   ### Willingness to contribute
   
   - [x] I can contribute a fix for this bug independently
   - [ ] I would be willing to contribute a fix for this bug with guidance from 
the Iceberg community
   - [ ] I cannot contribute a fix for this bug at this time
   


-- 
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