Guosmilesmile commented on code in PR #19146: URL: https://github.com/apache/iceberg/pull/19146#discussion_r4706567966
########## site/docs/blog/posts/2026-11-11-converting-equality-deletes-to-deletion-vectors.md: ########## @@ -0,0 +1,235 @@ +--- +date: 2026-11-11 +title: "Cheap Writes, Fast Reads: Converting Equality Deletes to Deletion Vectors" +slug: flink-convert-equality-deletes-to-deletion-vectors +authors: + - mxm +categories: + - blog +--- + +<!-- + - Licensed to the Apache Software Foundation (ASF) under one or more + - contributor license agreements. See the NOTICE file distributed with + - this work for additional information regarding copyright ownership. + - The ASF licenses this file to You under the Apache License, Version 2.0 + - (the "License"); you may not use this file except in compliance with + - the License. You may obtain a copy of the License at + - + - http://www.apache.org/licenses/LICENSE-2.0 + - + - Unless required by applicable law or agreed to in writing, software + - distributed under the License is distributed on an "AS IS" BASIS, + - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + - See the License for the specific language governing permissions and + - limitations under the License. + --> + +Equality deletes allow low-latency row-level deletes in streaming applications, but they can greatly impact read performance. In a benchmark, we found that a full scan of a table after 1,000 upsert commits of 500 rows each takes about 23 seconds with equality deletes. The same table with the deletes converted to deletion vectors only takes about 1 second. + +When equality deletes were first introduced, they looked like an elegant solution for streaming writes with deletes because they defer the work of resolving deletes to the readers. Yet, their long term costs for readers were underestimated, which caused major pain to many Iceberg users. + +To solve this, we built `ConvertEqualityDeletes`, a Flink table maintenance task that converts equality deletes to deletion vectors in the background. The task can be used seamlessly with existing writers, without changing their existing write patterns. In table format v3, this enables converting equality deletes into deletion vectors which allows for efficient reads. For table format v4 and beyond, we believe that this work forms the basis for stopping to write equality deletes altogether. + +<!-- more --> + +## A short history of deletes + +In the v1 table spec, the only way to delete rows was to rewrite the data files that contain them (copy-on-write). The v2 spec added position deletes, which mark rows in existing data files as deleted without rewriting the files. The catch is that readers now have to skip the deleted rows (merge-on-read). The v3 spec replaced position delete files with deletion vectors, which store the deleted row positions of a data file as a compact bitmap. + +The v2 spec also added equality deletes for low-latency row-level deletes, typically found in streaming applications like change data capture (CDC). While a position delete marks a row as deleted, an equality delete only records the values of the row's equality columns, for example its primary key. + +## Two ways to delete a row + +Take a `users` table with the primary key `id`. A Flink job upserts the row with `id = 42`, which deletes the old version of the row and inserts the new one. The old version is row 17 of the data file `file-A`, and the new version goes to a new data file, `file-B`. Iceberg can store the delete in two ways: + +- An **equality delete** says "delete every row where `id = 42`". It identifies rows by value, so the writer only needs the key, which it already has. +- A **deletion vector** says "delete row 17 of `file-A`". It identifies rows by position, which the writer needs to keep track of. + +<figure markdown="span">{ width="750" }<figcaption>The same delete as an equality delete and as a deletion vector.</figcaption></figure> + +Semantically, both describe the same delete. However, equality deletes are cheap to write while producing deletion vectors require the writer to keep track of row positions across data files. On the other hand, equality deletes push the work to resolve the deletes to the readers while deletion vectors are an efficient format for the readers to perform merge-on-read. + +## Why equality deletes slow down reads + +An equality delete applies to every older data file in its partition. To read `file-A`, a reader loads all newer equality delete files, collects their keys, and checks every row of `file-A` against them. A streaming job commits on every checkpoint, and each commit adds more equality delete files. Older data files collect more and more of them, so reads get slower with every commit until compaction rewrites the data. + +To measure this, we modeled a Flink upsert job on a table with 10 million rows. Each commit replaces 500 random keys and adds an equality delete file. We compared full Spark scans of this table with scans of the same table with the deletes stored as deletion vectors, which is what `ConvertEqualityDeletes` produces: + +<!-- + TODO: update the numbers, the commit, and the benchmark link with a 3-fork rerun on the upstream + benchmark. The chart is from the 3-fork sweep. The bucketed and planning numbers are from + single-fork runs. +--> + +<figure markdown="span">{ width="750" }<figcaption>Full table scan time of the same table with equality deletes and with deletion vectors.</figcaption></figure> + +Up to 100 commits, the difference is within run-to-run noise. After that, scans with equality deletes slow down quickly. Every commit adds a data file and a delete file that applies to all older data files. The number of data files and the number of delete files per data file both grow with the commits, so the work grows with the square of the number of commits. After 1,000 commits, which are 17 hours of upserts with one-minute checkpoints, only 4.8% of the rows are replaced, yet a scan is 20x slower than with deletion vectors. With deletion vectors, every data file has at most one, and scan time grows slowly, from 0.3 s to 1.1 s. + +Partitioning doesn't fix this. With `bucket(5, id)`, a full scan after 1,000 commits still takes 18 s with equality deletes vs. 2.9 s with deletion vectors. Scan planning gets slower as well, by 3x for the unpartitioned table and 12x for the bucketed one. + +These numbers are a lower bound. Things could be a lot worse on remote file storage. The benchmark reads from local disk, and Spark caches the parsed delete files between scans. On object storage, every delete file will potentially add additional latency. + +*Benchmark: `IcebergSourceUpsertDeleteBenchmark`, Spark 4.2.0 in local mode, Java 17 with a 12 GB heap, on a 10-core laptop. Each number is the mean of 3 runs in separate Java virtual machines with 5 measured scans each. At 1,000 commits, 8 s of the 23 s are garbage collection pauses. Without them, the slowdown is about 13x.* + +We've learned that a deletion vector avoids comparing rows with delete keys. The reader skips the deleted rows with a bitmap lookup. So why doesn't the writer produce deletion vectors in the first place? To do that, it has to find the file and row of `id = 42` for every upsert, which means reading data files while ingesting. That's too slow for a streaming job. Review Comment: ```suggestion A deletion vector avoids comparing rows with delete keys. The reader skips the deleted rows with a bitmap lookup. So why doesn't the writer produce deletion vectors in the first place? To do that, it has to find the file and row of `id = 42` for every upsert, which means reading data files while ingesting. That's too slow for a streaming job. ``` -- 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]
