Part 5 · Apache Iceberg spec · Chapter 20
Row-level deletes: v2 delete files and v3 deletion vectors
Merge-on-read in Iceberg: position deletes, equality deletes, the sequence-number scope rules that decide which delete applies to which file, how readers apply them, the v3 move to deletion vectors in Puffin files, compaction, and row lineage.
After this chapter you should be able to:
- Apply position and equality deletes to a scan correctly using sequence numbers and partitions.
- Explain why equality deletes are cheap to write and expensive to read.
- Describe the v3 deletion vector, its Puffin blob, and how it replaces position delete files.
- Explain why compaction keeps the starting sequence number, and how row ids are assigned.
Chapter 17 showed copy-on-write: a DELETE rewrites every data file that holds a matching row. Merge-on-read instead writes a small file that says which rows are gone and leaves the data files alone. Readers merge the two. Iceberg has three ways to say which rows are gone. v2 added position delete files and equality delete files. v3 added deletion vectors and forbids new position delete files. The draft v4 at this commit forbids new equality deletes and moves deletion vectors into the data file’s own manifest entry.[spec] This chapter covers v2 and v3, with v4 noted where it differs.
Links point to apache/iceberg at 9d2bf51.
Sequence numbers
Everything in this chapter depends on one number. Every successful commit gets the next sequence number, and every file a commit adds inherits it. Manifest entries are written with null and take the manifest’s sequence number when read; once a file is carried into a new manifest as EXISTING, the inherited number is written out so it never changes. A retry after a failed commit only rewrites the manifest list with a new number.[spec]
A file has two such numbers. The file sequence number is the commit that added it to the table. The data sequence number is the relative age of its content and is the one used to decide which deletes apply. They are equal unless a writer adds a file “with data that logically belongs to an older sequence number” and sets the data sequence number explicitly, which compaction does (below).[spec]
A delete file’s data sequence number says “these deletes were decided at this point in the table’s history”. A delete should hit rows that existed then and not rows added later. The rules below turn that into comparisons.
Position deletes
A position delete file is a Parquet (or Avro, ORC) file with rows of file_path and pos: the full URI of a data file, as it appears in the manifest, and the 0-based position of a row in it. The columns use reserved field ids 2147483546 and 2147483545. An optional row column can hold the deleted row’s values. Rows must be sorted by file_path, then pos: the first lets a reader push a filter on its own path into the delete file, the second lets it apply deletes while scanning instead of holding them in memory.[spec]
The writer has to know positions, so it has to read the table first. A Spark DELETE in merge-on-read mode scans the matching rows with the _file and _pos metadata columns and writes one delete row per hit.
A delete file whose entries all name one data file is file-scoped, and its manifest entry sets referenced_data_file. Otherwise it is partition-scoped and can hold deletes for many files in one partition. Spark’s default, write.delete.granularity=partition, writes one delete file per partition.[src]
Q1. Position delete files must be sorted by file_path, then pos. What does each sort key buy?
Equality deletes
An equality delete file holds column values instead of positions. Its manifest entry lists equality_ids, the field ids of the delete columns. A data row is deleted if it equals some delete row on every delete column, and null equals null (col IS NULL). From the spec’s example: to delete id = 4 AND category IS NULL, write a file with equality_ids=[1, 2] and one row 4, NULL.[spec]
The writer needs only the key. It never reads the table, which is why streaming engines that do upserts use them: an upsert of key k is an equality delete for k and an insert of the new row, in one commit. The cost moves to readers. Every reader must load the key set and test every row of every data file the delete is in scope for, until compaction applies the deletes for good.
Equality deletes outlive schema changes: “If a delete column in an equality delete file is later dropped from the table, it must still be used when applying the equality deletes.”[spec]
Q1. A streaming writer upserts id = 7 by writing an equality delete for id = 7 and a data file with the new row, in one commit. Why does the new row survive?
Q2. Why are equality deletes cheap to write and expensive to read?
Q3. A column used as an equality delete column is later dropped from the table. What happens to the existing equality deletes?
Scope rules
Which delete files apply to which data file is the core of the spec’s Scan Planning section. Restated, for a data file F with data sequence number s:[spec]
| Delete | Applies to F when |
|---|---|
| Deletion vector | referenced_data_file = F’s path, and s ≤ the DV’s sequence number, and the same spec and partition |
| Position delete file | referenced_data_file is null or F’s path, and s ≤ its sequence number, and the same spec and partition, and F has no DV |
| Equality delete file | s < its sequence number, and the same spec and partition, or the delete file’s spec is unpartitioned |
Two details carry most of the weight. Position deletes use ≤, so a delete in the same commit as the data file applies; a position can only name a row that is already in the file. Equality deletes use <, so an upsert’s delete does not remove the row written with it. Equality deletes written with an unpartitioned spec are global; nothing else crosses partitions.
- Sequence check: 8 ≤ 8 Passes. The delete can name a row position in this file.
- Sequence check: 8 < 8 Fails. New rows in the same commit survive the equality delete.
The Java implementation is DeleteFileIndex. The planner reads the live entries of the delete manifests once and groups them: DVs by referenced path, file-scoped position deletes by path, other position deletes by partition, equality deletes by partition or into a global group.[src] Each group is sorted by sequence number. Then for every data file:[src]
DeleteFile[] forDataFile(long sequenceNumber, DataFile file) {
DeleteFile[] global = findGlobalDeletes(sequenceNumber, file);
DeleteFile[] eqPartition = findEqPartitionDeletes(sequenceNumber, file);
DeleteFile dv = findDV(sequenceNumber, file);
if (dv != null && global == null && eqPartition == null) {
return new DeleteFile[] {dv};
} else if (dv != null) {
return concat(global, eqPartition, new DeleteFile[] {dv});
} else {
DeleteFile[] posPartition = findPosPartitionDeletes(sequenceNumber, file);
DeleteFile[] posPath = findPathDeletes(sequenceNumber, file);
return concat(global, eqPartition, posPartition, posPath);
}
}
A DV short-circuits the position-file lookups. Each group finds its start with a binary search for the first delete whose number is ≥ s.[src] That gives ≤ for position deletes. For equality deletes the index sorts on applySequenceNumber = dataSequenceNumber() - 1, so the same search gives the strict <.[src] Equality deletes then pass one more filter, canContainEqDeletesForFile, which compares the delete file’s bounds and null counts on each delete column with the data file’s and drops deletes that cannot match.[src] findDV also checks the DV is not older than the data file and fails with “DV data sequence number … must be greater than or equal to data file sequence number” if it is.[src]
Delete files go through scan filtering too. Their stats describe the deleted rows, so a delete file whose id range is 1 to 4 can be skipped for WHERE id = 9.[spec]
Q1. An equality delete file and a data file have the same data sequence number and partition. Does the delete apply to the data file?
Q2. A position delete file and a data file were added in the same commit. Does the delete apply?
Q3. An equality delete file is stored with an unpartitioned spec in a partitioned table. Which data files can it apply to?
Reading with deletes
- read live delete entries matching the filter. Delete files are filtered like data files: partition predicates by inclusive projection, then column stats, which for a delete file describe the deleted rows. Scan Planning ↗
- build DeleteFileIndex. DVs by referenced data file; file-scoped position deletes by path; other position deletes by partition; equality deletes by partition, or global when written with an unpartitioned spec. Each group is sorted by sequence number. DeleteFileIndex.build ↗
- for each data file: forDataFile(seq, file). Equality deletes with seq > the file's; a DV for the file if there is one, else position deletes with seq ≥ the file's. A binary search on the sorted sequence numbers finds the start. forDataFile ↗
- FileScanTask(data file, deletes[]). The task carries the delete files that apply. The planner never opens them.
- load positions (DV blob or position files) and equality keys. A DV is one read at content_offset for content_size_in_bytes. Position files are read and filtered to this file's path. Equality deletes become a set of keys per equality_ids group. DeleteFilter ↗
- applyEqDeletes(applyPosDeletes(rows)). Position deletes need the _pos column, equality deletes need their key columns, so the reader adds both to the projection if the query did not ask for them. DeleteFilter.filter ↗
Each scan task carries a data file and the delete files that apply to it. On the executor, DeleteFilter applies them in a fixed order:[src]
public CloseableIterable<T> filter(CloseableIterable<T> records) {
return applyEqDeletes(applyPosDeletes(records));
}
Position deletes, from position files or a DV, are loaded into one PositionDeleteIndex for this data file and tested against each row’s _pos.[src] Equality deletes are grouped by their set of equality_ids. Each group becomes a StructLikeSet of keys and a predicate that projects the row onto those columns and looks it up; a row is dropped if any group’s predicate matches.[src] Before reading, fileProjection adds _pos and any delete columns the query did not select to the read schema.[src] If the query asks for the _deleted metadata column, rows are marked instead of dropped.
Deletion vectors
A deletion vector is a bitmap of deleted positions for one data file, stored as a blob in a Puffin file. The format is the same 64-bit layout Delta uses: the high 32 bits of a position pick a 32-bit Roaring bitmap, which holds the low 32 bits.[spec] The blob type deletion-vector-v1 is:[puffin]
| Bytes | Content |
|---|---|
| 4, big-endian | length of the magic and the vector |
| 4 | magic D1 D3 39 64 |
| n, little-endian | the portable Roaring serialization: an 8-byte count, then per bitmap a 4-byte key and a 32-bit Roaring bitmap |
| 4, big-endian | CRC-32 of the magic and vector |
The mix of byte orders is deliberate: “Big endian values were chosen for compatibility with existing deletion vectors in Delta tables.” Read as a little-endian integer, the magic is 1681511377, Delta’s portable-format magic, and the whole blob is byte-for-byte one DV entry of a Delta DV file (chapter 13). The simulator’s test checks exactly that. Blob properties must include referenced-data-file and cardinality, and the blob is never compressed.[puffin] Java writes it in BitmapPositionDeleteIndex.serialize, whose comment says the length and checksum cover the magic “for compatibility with Delta”.[src]
In v2 and v3, a DV is tracked in a delete manifest as a position delete entry whose file_path is the Puffin file, with content_offset and content_size_in_bytes locating the blob and referenced_data_file naming the data file. Many DVs, for unrelated data files, can share one Puffin file.[spec] A reader does one ranged read per DV, with no scan of a delete file and no path filtering.
The rules that make this work:[spec]
- At most one DV per data file in a snapshot. A writer that deletes more rows “must merge new deletes with existing vectors or position delete files” and remove the old DV from the delete manifests. The old Puffin file need not be rewritten.
- A new DV must replace all earlier position delete files for its data file, so when a DV is present readers ignore position delete files for that file. That is why
forDataFilereturns the DV and skips the position lookups. - Removing a data file means removing its DV from the delete manifests too.
Q1. A v3 writer deletes more rows from a data file that already has a DV. What must it do?
Q2. A data file has a DV and also falls in the scope of an older position delete file. What does the reader apply?
Q3. Which kinds of delete may a writer add to a v3 table? (select all that apply)
Q4. How does an Iceberg deletion-vector-v1 blob relate to Delta's deletion vector format?
Writing deletes from Spark
Spark’s DELETE, UPDATE and MERGE use copy-on-write unless the table says otherwise: write.delete.mode, write.update.mode and write.merge.mode all default to copy-on-write.[src]
ALTER TABLE db.events SET TBLPROPERTIES (
'write.delete.mode' = 'merge-on-read',
'write.update.mode' = 'merge-on-read',
'write.merge.mode' = 'merge-on-read');
In merge-on-read mode Spark plans a SparkPositionDeltaWrite. The delete file format is Puffin for any v3 table, whatever write.delete.format.default says,[src] and the writer is chosen from that:[src]
if (context.useDVs()) { // deleteFileFormat == PUFFIN
return new PartitioningDVWriter<>(files, previousDeleteLoader);
} else if (inputOrdered && rewritableDeletes == null) {
return new ClusteredPositionDeleteWriter<>(writers, files, io, targetFileSize, deleteGranularity);
} else {
return new FanoutPositionOnlyDeleteWriter<>(writers, files, io, targetFileSize, deleteGranularity, previousDeleteLoader);
}
previousDeleteLoader reads the deletes that already apply to each touched data file, so the new DV contains the old positions. The commit is a RowDelta that adds the new DVs and removes the ones they replace, with the conflict checks of chapter 17. None of the three writers writes equality deletes.
Q1. A Spark DELETE runs in merge-on-read mode. When does it write DVs instead of position delete files?
Q2. With no table properties set, how do Spark DELETE, UPDATE and MERGE change an Iceberg table?
Compaction and deletes
Merge-on-read reads get slower as delete files pile up, so tables are compacted: rewrite_data_files reads data files with their deletes applied and writes new files. One detail keeps it correct. Compaction reads a snapshot, works for minutes, and commits. If an equality delete commits in between, it was decided after the rows compaction copied, and it must still remove them. So by default the rewrite commits the new files with the data sequence number of the snapshot it started from:[src][src]
RewriteFiles rewrite = table.newRewrite().validateFromSnapshot(startingSnapshotId);
if (useStartingSequenceNumber) { // use-starting-sequence-number, default true
long sequenceNumber = table.snapshot(startingSnapshotId).sequenceNumber();
rewrite.dataSequenceNumber(sequenceNumber);
}
The new files are “older” than the concurrent equality delete, which therefore still applies. Equality deletes that were already applied are not newer than the new files, so they don’t apply twice. Position deletes and DVs for the removed files are dangling: DVs for rewritten files are removed in the same commit, while position delete files stay until remove-dangling-deletes (default false) or a later rewrite removes them.[src]
Q1. Compaction rewrites partition a. Meanwhile an equality delete for id = 2 commits. Why is the row still deleted after compaction commits?
Row lineage
v3 also requires row lineage: every row has a _row_id, unique in the table, and a _last_updated_sequence_number. Both are assigned by inheritance, because neither the sequence number nor the starting row id is known until the commit succeeds. Writers write null and readers fill it in: a null _last_updated_sequence_number is the data file’s sequence number, and a null _row_id is the file’s first_row_id plus _pos.[spec]
first_row_id is inherited down the tree. A new snapshot’s first-row-id is the table’s next-row-id. When the manifest list is written, each data manifest without a first_row_id gets the next id, and the counter advances by its added plus existing rows. When a manifest is read, each live file without one gets the next id, advancing by its record count.[src][src] In the spec’s example, with next-row-id 1000, new manifests with 125, 100 and 150 rows get 1000, 1125 and 1225, and the table’s next-row-id becomes 1375; within the first, two added files of 50 rows get 1000 and 1050.[spec] The simulator’s tests reproduce these numbers.
A writer that moves a row to a new file copies its _row_id; if it changed the row it writes null for _last_updated_sequence_number so the new commit’s number is inherited. Equality deletes break the chain: the writer never read the old row, so it cannot copy its id, and the spec treats such an update as a delete plus a new row.[spec] Tables upgraded to v3 start with next-row-id 0, and rows from snapshots before the upgrade read _row_id as null until a new snapshot assigns ids to their files.[spec]
Q1. A v3 data file has first_row_id 1050 and a row at _pos 3 whose _row_id is null in the file. What is its _row_id?
Q2. A row is updated through an equality delete plus an insert. What happens to its row id?
Try it
The lab starts with a v2 table: file 00000.parquet in partition a (ids 1 to 3, seq 1) and 00001.parquet in b (ids 4 and 5, seq 2). Things to try:
DELETE WHERE id2. A position delete file appears with seq 3. Click00000.parquetto see why it applies.- Upsert id 3 into
a. The equality delete and the new data file share a sequence number, so the new row survives. Switch to “ignoring them”: the new row turns red, because a reader without sequence numbers deletes it. - Equality delete id 4 with partition
a, then again as unpartitioned. Only the global one reachesb. - Upgrade to v3 and delete another row from
00000.parquet. A DV appears that contains the old position too, and the file-scoped position delete file is gone. - Compact partition
a. Rows are rewritten without deletes, and the new file keeps the previous sequence number.
Write · format v2 · last seq 2
- Two appends: 00000.parquet (a, seq 1) and 00001.parquet (b, seq 2).
Delete files
- none
Data files · click one to see its deletes
00000.parquet partition a · data seq 1| _pos | id | v | deleted by |
|---|---|---|---|
| 0 | 1 | pen | |
| 1 | 2 | ink | |
| 2 | 3 | cup |
00001.parquet partition b · data seq 2| _pos | id | v | deleted by |
|---|---|---|---|
| 0 | 4 | map | |
| 1 | 5 | box |
Scope rules for 00000.parquet
| delete file | applies | why |
|---|---|---|
| no delete files | ||
Struck-through rows are deleted. Red rows are read differently when sequence numbers are ignored.
Scan Planning ↗ · DeleteFileIndex.forDataFile ↗ · DeleteFilter ↗
Comparison with Delta
Delta has one merge-on-read mechanism: deletion vectors, attached to the add action of the data file they modify (chapter 13).[protocol] That matches where Iceberg’s v4 draft is going, with DVs colocated in the data file’s manifest entry. The bitmap and its on-disk framing are the same in both formats. What Delta lacks is equality deletes; every Delta delete names positions, so the writer must find the rows first. The scope problem is simpler as a result. A Delta DV belongs to one add and is replaced with it, so no sequence numbers are needed to decide whether it applies, while Iceberg needs them because delete files live apart from data files. Delta’s row tracking (chapter 13) and Iceberg’s row lineage solve the same problem the same way, with ids and versions assigned at commit time by inheritance from a high-water mark.
Q1. Which Iceberg delete kind has no Delta equivalent?
Concepts in this chapter
Explained here
Primary sources
Review this chapter
This chapter has 17 quiz questions.