Skip to content

[IcebergIO] Stop CDC reads reporting a deleted row as deleted again - #41979

Open
claudevdm wants to merge 1 commit into
apache:masterfrom
claudevdm:iceberg-fix-cdc-earlier-deletes
Open

claudevdm wants to merge 1 commit into
apache:masterfrom
claudevdm:iceberg-fix-cdc-earlier-deletes

Conversation

@claudevdm

Copy link
Copy Markdown
Collaborator

A CDC read (IcebergIO.readRows(...).withCdc() or Managed ICEBERG_CDC) reports a DELETE a second time when a snapshot adds a position delete or a deletion vector that covers a row an earlier snapshot already deleted. Nothing fails: the change stream just says the row was deleted again.

The usual way to hit it is a second merge-on-read delete in the same data file:

  • Format v3 keeps one deletion vector per data file. A new vector also holds the earlier positions and replaces the old one, so every later delete in that file reports all the earlier ones again.
  • On v2, Spark writes file-scoped position deletes by default and carries a file's earlier positions over into the new delete file in the same way.

Example (v3): a data file holds ids 1 and 2. An UPDATE of id 1 writes the deletion vector {0} and the new row; an UPDATE of id 2 writes {0, 1} and its new row. The second snapshot should give UPDATE_BEFORE (2, a) and UPDATE_AFTER (2, b). It also gives DELETE (1, a), so a consumer applying the stream by key would delete id 1, which is still in the table.

Cause. BeamBaseIncrementalChangelogScan gives a DeletedRowsScanTask the deletes committed before its snapshot (existingDeletes) only when the snapshot adds equality deletes. The reader drops the rows existingDeletes remove before it reports the rows the added deletes remove, so for position deletes and deletion vectors nothing was dropped.

Fix. Load the earlier deletes whenever a snapshot adds row-level deletes of any kind. The reader needs no change.

@claudevdm
claudevdm requested a review from ahmedabu98 December 4, 2026 21:59
@claudevdm

Copy link
Copy Markdown
Collaborator Author

R: @ahmedabu98

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

A CDC read reported a DELETE a second time when a snapshot added a
position delete or a deletion vector for a row that an earlier snapshot
had already deleted. On format v3 tables this happened on every later
delete in the same data file, because a new deletion vector repeats the
positions of the one it replaces. On format v2 tables it happened when a
writer merged a data file's position deletes into a new file, as Spark
does with file-granularity deletes. Next to a merge-on-read update, the
repeated row came out as an extra DELETE.

The changelog scan read a snapshot's added deletes against the deletes
committed before it only when the snapshot added equality deletes. Do it
for added deletes of any kind.

Those earlier deletes come from the snapshot before the scanned range,
and table maintenance may have expired it. The scan then failed with
"Cannot find starting snapshot". That already happened for snapshots
that add equality deletes or remove a data file, and would now have
happened for every snapshot that adds deletes. Log a warning instead and
read the snapshot without the earlier deletes, so it may report a row
as deleted again.

A scan over several snapshots, which the source does not use but the
public scan class allows, kept the delete files that a snapshot in the
range removed when it read later snapshots. A replaced deletion vector
then sat next to its replacement and planning failed with "Can't index
multiple DVs". Drop them, as the planning of removed data files already
did, and match removed files as Iceberg does, by location, content
offset and size, because deletion vectors can share a Puffin file.

The tests cover a position delete after an equality delete or after
another position delete of the same row, successive deletion vectors
alone and in merge-on-read updates through both resolve paths, a v2
position delete file that replaces an earlier one, an identity-
partitioned v3 table, a v2 table upgraded to v3, a deletion vector and a
file deletion whose parent snapshot has expired, and a scan over several
snapshots.
@claudevdm
claudevdm force-pushed the iceberg-fix-cdc-earlier-deletes branch from aa19915 to 3ae4bf4 Compare December 5, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant