You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/01/10 03:25:12 UTC

[GitHub] [iceberg] Initial-neko commented on a change in pull request #3432: Doc: add a page to explain row-level deletes

Initial-neko commented on a change in pull request #3432:
URL: https://github.com/apache/iceberg/pull/3432#discussion_r780876564



##########
File path: site/docs/row-level-deletes.md
##########
@@ -0,0 +1,196 @@
+<!--
+ - 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.
+ -->
+
+# Row-Level Deletes
+
+Iceberg supports metadata-based deletion through the `DeleteFiles` interface.
+It allows you to quickly delete a specific file or any file that might match a given expression without the need to read or write any data in the table.
+
+Row-level delete targets more complicated use cases such as general data protection regulation (GDPR).
+Copy-on-write and merge-on-read are two different approaches to handle row-level delete operations. Here are their definitions in Iceberg:
+
+- **copy-on-write**: a delete directly rewrites all the affected data files.
+- **merge-on-read**: delete information is encoded in the form of _delete files_. The table reader can apply all delete information at read time.
+
+Overall, copy-on-write is more efficient in reading data, whereas merge-on-read is more efficient in writing deletes, but requires more maintenance and tuning to be performant in reading data with deletes.
+Users can choose to use **both** copy-on-write and merge-on-read for the same Iceberg table based on different situations. 
+For example, a time-partitioned table can have newer partitions with more frequent updates maintained in the merge-on-read approach through a streaming pipeline,
+and older partitions maintained in the copy-on-write approach with less frequent GDPR deletes from batch ETL jobs.
+
+There are use cases that could only be supported by one approach such as change data capture (CDC).
+There are also limitations for different compute engines that lead them to prefer one approach over another.
+Please check out the documentation of the specific compute engines to see the details of their capabilities for these two approaches.
+In this article, we will focus on explaining Iceberg's core design of copy-on-write and merge-on-read.
+
+!!!Note
+    In Iceberg, update is modeled as a delete with an insert within the same transaction. 
+    Therefore, we will focus our discussion on delete in this article. 
+
+## Copy-on-write
+
+In the copy-on-write approach, given a user's delete requirement, the write process would search for all the affected data files and perform a rewrite operation.
+
+For example, consider an unpartitioned table with schema `(id bigint, category string, data string)` that has the following files:
+
+```
+file A: (1, 'c1', 'data1'), (2, 'c1', 'data2')
+file B: (3, 'c2', 'data1'), (4, 'c2', 'data2')
+file C: (5, 'c3', 'data3'), (6, 'c3', 'data2')
+```
+
+A copy-on-write deletion of `data='data1'` rewrites files A and B into a different set of files. An example output table file layout might look like:
+
+```
+file D: (2, 'c1', 'data2'), (4, 'c2', 'data2')
+file C: (5, 'c3', 'data3'), (6, 'c3', 'data2')
+```
+
+There is no effect on read side in the copy-on-write approach.
+
+## Merge-on-read
+
+### Row-level delete file spec
+
+Iceberg supports 2 different types of row-level delete files: **position deletes** and **equality deletes**.
+If you are unfamiliar with these concepts, please read the [spec](../spec/#row-level-deletes) page for more information before proceeding.
+
+Also note that because row-level delete files are valid Iceberg data files, all rows in each delete file must belong to the same partition.
+If a delete file belongs to `Unpartitioned` (the partition spec has no partition field), then the delete file is called a **global delete**. 
+Otherwise, it is called a **partition delete**.
+
+### Writing delete files
+
+From the end user's perspective, it is very rare to directly request deletion of a specific row of a specific file. 
+A delete requirement usually comes as a predicate such as `id = 5` or `date < TIMESTAMP '2000-01-01'`. 
+Given a predicate, delete files can be produced in one or some combinations of the following ways:
+
+1. **partition position deletes**: perform a scan \[1\] to know the data files and row positions affected by the predicate and then write partition position deletes \[2\]
+2. **partition equality deletes**: convert input predicate to equality predicates \[3\] for each affected partition and write partition equality deletes
+3. **partition global deletes**: convert input predicate to equality predicates and write global equality deletes 
+
+\[1\] scan here can mean a table scan, or a scan of unflushed files (stored in memory, local RocksDB, etc.) for use cases like streaming.
+
+\[2\] it is in theory possible to write global position deletes, but it is always preferred to write partition position deletes because the writer already knows the exact partition to use after the scan.
+
+\[3\] if the input inequality predicate cannot be easily converted to a finite number of equality predicates (e.g. `price > 2.33`), then it is preferred to use position deletes instead.
+
+For example, in the change data capture use case, both partition position deletes and partition equality deletes are used. 
+For a new delete such as `id = 1001`, the writer checks the unflushed data index to see if there is an existing row matching `id = 1001` and performs a position delete.
+It also writes an equality delete that is applied to all the existing data files that are already in the table.
+
+### Reading data with delete files
+
+During Iceberg's scan file planning phase, a delete file index is constructed to filter the delete files associated with any data file, with the following rules:
+
+1. equality delete files are applied to data files of strictly lower sequence numbers

Review comment:
       Is id the primary key? (1, 'C10', 'data10') records should only generate equality-deletes. The condition for 
   pos-delete generation is that there are the same key in this commit, that is, insertedrowmap needs corresponding records to generate POS delete。
   
   `/**
        * Write the pos-delete if there's an existing row matching the given key.
        *
        * @param key has the same columns with the equality fields.
        */
       private void internalPosDelete(StructLike key) {
         PathOffset previous = insertedRowMap.remove(key);
   
         if (previous != null) {
           // TODO attach the previous row if has a positional-delete row schema in appender factory.
           posDeleteWriter.delete(previous.path, previous.rowOffset, null);
         }
       }`




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org