[WIP] Core: rewrite_position_delete_files can hide a live row after its data file is re-added

Open
#19,237 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java, spark
Domain
backend, databases

Research direction

Start with RewritePositionDeletesCommitManager.java, RewritePositionDeletesGroup.java, and SparkRewritePositionDeleteRunner.java, using the linked lines as entry points. Read RewriteFiles.java's sequence-number requirements, then trace how the runner groups delete rows and how the commit manager assigns output sequence numbers. Reproduce the supplied Spark SQL case and verify that rewriting preserves the rows returned before the rewrite, including the re-added data file.

Written by the indexing model from the issue text.

Description

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. 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, 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, and RewriteFiles requires the new state to be "logically equivalent to the original table state", RewriteFiles.

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

Where it happens:

RewriteFiles says the max keeps "the set of applicable delete records" unchanged, 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
  • 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
Dominant language
Java
Stars
9.5k
Forks
3.7k
Avg merge
2d 2h
Merged PRs (30d)
170

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/iceberg

All issues in apache/iceberg

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.