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

Aperta
#19,237 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java, spark
Ambito
backend, databases

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Java
Stelle
9.5k
Fork
3.7k
Merge medio
2g 2h
PR unite (30g)
170

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/iceberg

Tutte le issue di apache/iceberg

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.