This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 50d7b687cd [core] Preserve deletion vector changes in snapshot diffs 
(#11258)
50d7b687cd is described below

commit 50d7b687cdd1cbd68795026dd077ace84760d301
Author: YueZhang <[email protected]>
AuthorDate: Thu Nov 19 00:45:35 2026 -0500

    [core] Preserve deletion vector changes in snapshot diffs (#11258)
---
 .../table/source/snapshot/SnapshotReaderImpl.java  |  71 ++++++--
 .../table/IncrementalDeletionVectorDiffTest.java   | 178 +++++++++++++++++++++
 2 files changed, 233 insertions(+), 16 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReaderImpl.java
 
b/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReaderImpl.java
index 18adcf15dc..934e8d39e5 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReaderImpl.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReaderImpl.java
@@ -24,6 +24,7 @@ import org.apache.paimon.codegen.CodeGenUtils;
 import org.apache.paimon.codegen.RecordComparator;
 import org.apache.paimon.consumer.ConsumerManager;
 import org.apache.paimon.data.BinaryRow;
+import org.apache.paimon.deletionvectors.DeletionVectorsIndexFile;
 import org.apache.paimon.fs.Path;
 import org.apache.paimon.index.DeletionVectorMeta;
 import org.apache.paimon.index.IndexFileHandler;
@@ -74,6 +75,7 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
 import java.util.function.BiConsumer;
@@ -573,8 +575,28 @@ public class SnapshotReaderImpl implements SnapshotReader {
                     totalBuckets = beforeEntries.get(0).totalBuckets();
                 }
 
-                // deduplicate: remove entries common to both lists
-                deduplicate(beforeEntries, dataEntries);
+                Map<String, DeletionFile> beforeVectors =
+                        beforeDeletionFilesMap == null
+                                ? Collections.emptyMap()
+                                : beforeDeletionFilesMap.getOrDefault(
+                                        Pair.of(part, bucket), 
Collections.emptyMap());
+                Map<String, DeletionFile> afterVectors =
+                        afterDeletionFilesMap == null
+                                ? Collections.emptyMap()
+                                : afterDeletionFilesMap.getOrDefault(
+                                        Pair.of(part, bucket), 
Collections.emptyMap());
+                // A primary-key diff must read both versions of a shared file 
when its visible
+                // rows changed. Append diffs only read newly added files.
+                deduplicate(
+                        beforeEntries,
+                        dataEntries,
+                        file ->
+                                tableSchema.primaryKeys().isEmpty()
+                                        || sameDeletionVector(
+                                                part,
+                                                bucket,
+                                                beforeVectors.get(file),
+                                                afterVectors.get(file)));
 
                 List<DataFileMeta> before =
                         beforeEntries.stream()
@@ -585,20 +607,12 @@ public class SnapshotReaderImpl implements SnapshotReader 
{
 
                 List<DeletionFile> beforeDeletionFiles = null;
                 if (deletionVectors && beforeDeletionFilesMap != null) {
-                    beforeDeletionFiles =
-                            getDeletionFiles(
-                                    before,
-                                    beforeDeletionFilesMap.getOrDefault(
-                                            Pair.of(part, bucket), 
Collections.emptyMap()));
+                    beforeDeletionFiles = getDeletionFiles(before, 
beforeVectors);
                 }
 
                 List<DeletionFile> afterDeletionFiles = null;
                 if (deletionVectors && afterDeletionFilesMap != null) {
-                    afterDeletionFiles =
-                            getDeletionFiles(
-                                    after,
-                                    afterDeletionFilesMap.getOrDefault(
-                                            Pair.of(part, bucket), 
Collections.emptyMap()));
+                    afterDeletionFiles = getDeletionFiles(after, afterVectors);
                 }
 
                 IncrementalSplit split =
@@ -765,16 +779,41 @@ public class SnapshotReaderImpl implements SnapshotReader 
{
     }
 
     /**
-     * Remove entries common to both lists using HashSet for O(n+m) complexity 
instead of O(n*m)
-     * with List.remove().
+     * A changed deletion vector rewrites every vector of its bucket into a 
new index file, so
+     * different descriptors may still hold the same bitmap.
+     */
+    private boolean sameDeletionVector(
+            BinaryRow partition,
+            int bucket,
+            @Nullable DeletionFile before,
+            @Nullable DeletionFile after) {
+        if (Objects.equals(before, after)) {
+            return true;
+        }
+        if (before == null
+                || after == null
+                || (before.cardinality() != null
+                        && after.cardinality() != null
+                        && !before.cardinality().equals(after.cardinality()))) 
{
+            return false;
+        }
+        DeletionVectorsIndexFile dvIndex = indexFileHandler.dvIndex(partition, 
bucket);
+        return 
dvIndex.readDeletionVector(before).equals(dvIndex.readDeletionVector(after));
+    }
+
+    /**
+     * Remove unchanged entries common to both lists using HashSet for O(n+m) 
complexity instead of
+     * O(n*m) with List.remove().
      */
     private static void deduplicate(
-            List<ManifestEntry> beforeEntries, List<ManifestEntry> 
dataEntries) {
+            List<ManifestEntry> beforeEntries,
+            List<ManifestEntry> dataEntries,
+            Filter<String> unchangedFile) {
         Set<ManifestEntry> afterSet = new HashSet<>(dataEntries);
         Set<ManifestEntry> commonEntries = new HashSet<>();
         beforeEntries.removeIf(
                 entry -> {
-                    if (afterSet.contains(entry)) {
+                    if (afterSet.contains(entry) && 
unchangedFile.test(entry.file().fileName())) {
                         commonEntries.add(entry);
                         return true;
                     }
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/table/IncrementalDeletionVectorDiffTest.java
 
b/paimon-core/src/test/java/org/apache/paimon/table/IncrementalDeletionVectorDiffTest.java
new file mode 100644
index 0000000000..ff4e70c118
--- /dev/null
+++ 
b/paimon-core/src/test/java/org/apache/paimon/table/IncrementalDeletionVectorDiffTest.java
@@ -0,0 +1,178 @@
+/*
+ * 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.
+ */
+
+package org.apache.paimon.table;
+
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.data.GenericRow;
+import org.apache.paimon.data.InternalRow;
+import org.apache.paimon.data.serializer.InternalRowSerializer;
+import org.apache.paimon.reader.RecordReader;
+import org.apache.paimon.schema.Schema;
+import org.apache.paimon.table.sink.StreamTableCommit;
+import org.apache.paimon.table.sink.StreamTableWrite;
+import org.apache.paimon.table.source.IncrementalSplit;
+import org.apache.paimon.table.source.TableScan;
+import org.apache.paimon.table.system.AuditLogTable;
+import org.apache.paimon.types.DataTypes;
+import org.apache.paimon.types.RowKind;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for deletion-vector changes to shared files in primary-key snapshot 
diffs. */
+class IncrementalDeletionVectorDiffTest extends TableTestBase {
+
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    void testDeletionOnRetainedDataFile(boolean deletionVectors) throws 
Exception {
+        FileStoreTable table = createTable(deletionVectors);
+        commit(table, 0, GenericRow.of(1, 10), GenericRow.of(2, 20));
+        long before = table.snapshotManager().latestSnapshotId();
+        assertThat(read(table))
+                .containsExactlyInAnyOrder(GenericRow.of(1, 10), 
GenericRow.of(2, 20));
+        commit(table, 1, GenericRow.ofKind(RowKind.DELETE, 1, 10));
+        assertThat(read(table)).containsExactly(GenericRow.of(2, 20));
+
+        FileStoreTable diff = diffFrom(table, before);
+        assertThat(read(new AuditLogTable(diff)))
+                .containsExactly(GenericRow.of(BinaryString.fromString("-D"), 
1, 10));
+        assertThat(read(diff)).isEmpty();
+    }
+
+    @Test
+    void testRepeatedDeletionDoesNotRepeatEarlierChange() throws Exception {
+        FileStoreTable table = createTable(true);
+        commit(table, 0, GenericRow.of(1, 10), GenericRow.of(2, 20), 
GenericRow.of(3, 30));
+        commit(table, 1, GenericRow.ofKind(RowKind.DELETE, 1, 10));
+        long before = table.snapshotManager().latestSnapshotId();
+        commit(table, 2, GenericRow.ofKind(RowKind.DELETE, 2, 20));
+        assertThat(read(table)).containsExactly(GenericRow.of(3, 30));
+        assertThat(readDiff(diffFrom(table, before)))
+                .containsExactly(GenericRow.ofKind(RowKind.DELETE, 2, 20));
+    }
+
+    @ParameterizedTest
+    @ValueSource(ints = {10, 11})
+    void testUpdateRetainsOnlyChangedValues(int updatedValue) throws Exception 
{
+        FileStoreTable table = createTable(true);
+        commit(table, 0, GenericRow.of(1, 10), GenericRow.of(2, 20));
+        long before = table.snapshotManager().latestSnapshotId();
+        commit(table, 1, GenericRow.of(1, updatedValue));
+        assertThat(read(table))
+                .containsExactlyInAnyOrder(GenericRow.of(1, updatedValue), 
GenericRow.of(2, 20));
+        List<InternalRow> changes = readDiff(diffFrom(table, before));
+        if (updatedValue == 10) {
+            assertThat(changes).isEmpty();
+        } else {
+            assertThat(changes).containsExactly(GenericRow.of(1, 11));
+        }
+    }
+
+    @Test
+    void testUnchangedDeletionVectorsStillPruneCommonFiles() throws Exception {
+        FileStoreTable table = createTable(true);
+        commit(table, 0, GenericRow.of(1, 10), GenericRow.of(2, 20));
+        commit(table, 1, GenericRow.ofKind(RowKind.DELETE, 1, 10));
+        long before = table.snapshotManager().latestSnapshotId();
+        commit(table, 2, GenericRow.of(3, 30));
+        FileStoreTable diff = diffFrom(table, before);
+        TableScan.Plan plan = diff.newScan().plan();
+        assertThat(plan.splits()).hasSize(1);
+        IncrementalSplit split = (IncrementalSplit) plan.splits().get(0);
+        assertThat(split.beforeFiles()).isEmpty();
+        assertThat(split.afterFiles()).hasSize(1);
+        assertThat(readDiff(diff)).containsExactly(GenericRow.of(3, 30));
+    }
+
+    @Test
+    void testUnchangedBitmapInRewrittenIndexStillPrunesCommonFile() throws 
Exception {
+        FileStoreTable table =
+                createTable(true)
+                        
.copy(Collections.singletonMap("compaction.small-file-ratio", "0"));
+        commit(table, 0, GenericRow.of(1, 10), GenericRow.of(2, 20), 
GenericRow.of(3, 30));
+        commit(table, 1, GenericRow.of(10, 100), GenericRow.of(11, 110), 
GenericRow.of(12, 120));
+        commit(
+                table,
+                2,
+                GenericRow.ofKind(RowKind.DELETE, 1, 10),
+                GenericRow.ofKind(RowKind.DELETE, 10, 100));
+        long before = table.snapshotManager().latestSnapshotId();
+        // Rewrites the index file holding both vectors, but only the first 
bitmap changes.
+        commit(table, 3, GenericRow.ofKind(RowKind.DELETE, 2, 20));
+
+        FileStoreTable diff = diffFrom(table, before);
+        TableScan.Plan plan = diff.newScan().plan();
+        assertThat(plan.splits()).hasSize(1);
+        IncrementalSplit split = (IncrementalSplit) plan.splits().get(0);
+        assertThat(split.beforeFiles()).hasSize(1);
+        assertThat(split.afterFiles()).hasSize(1);
+        
assertThat(readDiff(diff)).containsExactly(GenericRow.ofKind(RowKind.DELETE, 2, 
20));
+    }
+
+    private FileStoreTable createTable(boolean deletionVectors) throws 
Exception {
+        Schema schema =
+                Schema.newBuilder()
+                        .column("k", DataTypes.INT())
+                        .column("v", DataTypes.INT())
+                        .primaryKey("k")
+                        .option("bucket", "1")
+                        .option("deletion-vectors.enabled", 
String.valueOf(deletionVectors))
+                        .build();
+        catalog.createTable(identifier(), schema, false);
+        return (FileStoreTable) catalog.getTable(identifier());
+    }
+
+    private void commit(FileStoreTable table, long identifier, GenericRow... 
rows)
+            throws Exception {
+        try (StreamTableWrite write = 
table.newWrite(commitUser).withIOManager(ioManager);
+                StreamTableCommit commit = table.newCommit(commitUser)) {
+            for (GenericRow row : rows) {
+                write.write(row);
+            }
+            commit.commit(identifier, write.prepareCommit(true, identifier));
+        }
+    }
+
+    private FileStoreTable diffFrom(FileStoreTable table, long before) {
+        Map<String, String> options = new HashMap<>();
+        options.put(
+                "incremental-between", before + "," + 
table.snapshotManager().latestSnapshotId());
+        options.put("incremental-between-scan-mode", "diff");
+        return table.copy(options);
+    }
+
+    private List<InternalRow> readDiff(FileStoreTable table) throws Exception {
+        List<InternalRow> rows = new ArrayList<>();
+        InternalRowSerializer serializer = new 
InternalRowSerializer(table.rowType());
+        try (RecordReader<InternalRow> reader =
+                
table.newRead().forceKeepDelete().createReader(table.newScan().plan())) {
+            reader.forEachRemaining(row -> rows.add(serializer.copy(row)));
+        }
+        return rows;
+    }
+}

Reply via email to