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 d85253bbdc [flink] Keep delete records in the COMPACT_DELTA lookup 
refresh (#10924)
d85253bbdc is described below

commit d85253bbdcbee098093d4fc2ea21c2708827d0d4
Author: YangJie <[email protected]>
AuthorDate: Mon Nov 2 09:50:39 2026 -0500

    [flink] Keep delete records in the COMPACT_DELTA lookup refresh (#10924)
---
 .../paimon/flink/lookup/LookupCompactDiffRead.java |  3 +
 .../flink/lookup/SecondaryIndexLookupTable.java    |  8 +-
 .../org/apache/paimon/flink/LookupJoinITCase.java  | 98 ++++++++++++++++++++++
 .../paimon/flink/lookup/LookupTableTest.java       | 28 +++++++
 4 files changed, 136 insertions(+), 1 deletion(-)

diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/LookupCompactDiffRead.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/LookupCompactDiffRead.java
index 0b390b8bf1..fd07b555f7 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/LookupCompactDiffRead.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/LookupCompactDiffRead.java
@@ -45,6 +45,9 @@ public class LookupCompactDiffRead extends 
AbstractDataTableRead {
     public LookupCompactDiffRead(MergeFileSplitRead mergeRead, TableSchema 
schema) {
         super(schema);
         this.incrementalDiffRead = new 
IncrementalCompactDiffSplitRead(mergeRead);
+        // deleted rows must reach the lookup cache as DELETE records, both 
when compaction keeps
+        // the delete record and when it drops the row and the row is only in 
the files before
+        this.incrementalDiffRead.forceKeepDelete();
         this.fullPhaseMergeRead =
                 SplitRead.convert(
                         mergeRead,
diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/SecondaryIndexLookupTable.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/SecondaryIndexLookupTable.java
index 668570d77d..09f57debad 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/SecondaryIndexLookupTable.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/SecondaryIndexLookupTable.java
@@ -101,8 +101,14 @@ public class SecondaryIndexLookupTable extends 
PrimaryKeyLookupTable {
                 tableState.delete(primaryKeyRow);
             }
         } else {
+            if (!previousFetched) {
+                previous = tableState.get(primaryKeyRow);
+            }
+            // a delete record may carry only the key, so retract the cached 
row's secondary key
+            if (previous != null) {
+                indexState.retract(secKeyRow.replaceRow(previous), 
primaryKeyRow);
+            }
             tableState.delete(primaryKeyRow);
-            indexState.retract(secKeyRow.replaceRow(row), primaryKeyRow);
         }
     }
 
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/LookupJoinITCase.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/LookupJoinITCase.java
index fe522d765c..ba63fa28fe 100644
--- 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/LookupJoinITCase.java
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/LookupJoinITCase.java
@@ -22,8 +22,10 @@ import org.apache.paimon.data.BlobDescriptor;
 import org.apache.paimon.flink.FlinkConnectorOptions.LookupCacheMode;
 import org.apache.paimon.utils.BlockingIterator;
 
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.planner.factories.TestValuesTableFactory;
 import org.apache.flink.types.Row;
+import org.apache.flink.types.RowKind;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
@@ -115,6 +117,102 @@ public class LookupJoinITCase extends CatalogITCaseBase {
         iterator.close();
     }
 
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    public void testLookupJoinDeletionPropagationAfterCompaction(boolean 
fullCompaction)
+            throws Exception {
+        sql(
+                "CREATE TABLE AGG_DIM (i INT PRIMARY KEY NOT ENFORCED, cnt 
BIGINT) WITH ("
+                        + "'merge-engine' = 'aggregation', "
+                        + "'fields.cnt.aggregate-function' = 'sum', "
+                        + "'aggregation.remove-record-on-delete' = 'true', "
+                        + "'force-lookup' = 'true', "
+                        + "'changelog-producer' = 'none', "
+                        + "'lookup.cache' = 'full', "
+                        + "'continuous.discovery-interval' = '1 ms')");
+        sql("INSERT INTO AGG_DIM VALUES (1, 10), (2, 20)");
+
+        String query =
+                "SELECT T.i, D.cnt FROM T LEFT JOIN AGG_DIM FOR SYSTEM_TIME AS 
OF T.proctime AS D "
+                        + "ON T.i = D.i";
+        BlockingIterator<Row, Row> iterator = 
BlockingIterator.of(sEnv.executeSql(query).collect());
+
+        sql("INSERT INTO T VALUES (1), (2)");
+        assertThat(iterator.collect(2)).containsExactlyInAnyOrder(Row.of(1, 
10L), Row.of(2, 20L));
+
+        if (fullCompaction) {
+            // keep the delete in level 0, then a full compaction drops the 
row, which then
+            // only exists in the files before the compaction
+            sql("ALTER TABLE AGG_DIM SET ('write-only' = 'true')");
+            sql("DELETE FROM AGG_DIM WHERE i = 1");
+            // wait for the compaction job
+            tEnv.getConfig().set(TableConfigOptions.TABLE_DML_SYNC, true);
+            sql("CALL sys.compact(`table` => 'default.AGG_DIM')");
+            assertThat(sql("SELECT level, record_count FROM `AGG_DIM$files`"))
+                    .containsExactly(Row.of(5, 1L));
+        } else {
+            // the delete job compacts into an intermediate level and keeps 
the delete record
+            sql("DELETE FROM AGG_DIM WHERE i = 1");
+        }
+        Thread.sleep(2000); // wait compact-delta refresh
+
+        sql("INSERT INTO T VALUES (1), (2)");
+        assertThat(iterator.collect(2)).containsExactlyInAnyOrder(Row.of(1, 
null), Row.of(2, 20L));
+        iterator.close();
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"full", "memory"})
+    public void testLookupJoinBySecondaryKeyAfterKeyOnlyDelete(String 
cacheMode) throws Exception {
+        sql(
+                "CREATE TABLE SEC_DIM (k INT PRIMARY KEY NOT ENFORCED, f1 INT, 
f2 INT) WITH ("
+                        + "'merge-engine' = 'partial-update', "
+                        + "'fields.f2.sequence-group' = 'f1', "
+                        + "'partial-update.remove-record-on-sequence-group' = 
'f2', "
+                        + "'force-lookup' = 'true', "
+                        + "'changelog-producer' = 'none', "
+                        + "'lookup.cache' = '%s', "
+                        + "'continuous.discovery-interval' = '1 ms')",
+                cacheMode);
+        sql("INSERT INTO SEC_DIM VALUES (1, 11, 1)");
+
+        String query =
+                "SELECT T.i, D.k, D.f2 FROM T LEFT JOIN SEC_DIM FOR 
SYSTEM_TIME AS OF T.proctime AS D "
+                        + "ON T.i = D.f1";
+        BlockingIterator<Row, Row> iterator = 
BlockingIterator.of(sEnv.executeSql(query).collect());
+
+        sql("INSERT INTO T VALUES (11)");
+        assertThat(iterator.collect(1)).containsExactly(Row.of(11, 1, 1));
+
+        // a sequence-group delete that carries no value of the join key f1
+        String dataId =
+                TestValuesTableFactory.registerData(
+                        Collections.singletonList(Row.ofKind(RowKind.DELETE, 
1, null, 2)));
+        sEnv.executeSql(
+                String.format(
+                        "CREATE TEMPORARY TABLE SEC_DELETE (k INT, f1 INT, f2 
INT) WITH ("
+                                + "'connector' = 'values', 'bounded' = 'true', 
"
+                                + "'data-id' = '%s', 'changelog-mode' = 
'I,D')",
+                        dataId));
+        sEnv.executeSql("INSERT INTO SEC_DIM SELECT * FROM 
SEC_DELETE").await();
+        // the delete record is kept in an intermediate level, so the refresh 
reads it
+        assertThat(sql("SELECT level, record_count FROM `SEC_DIM$files`"))
+                .containsExactlyInAnyOrder(Row.of(4, 1L), Row.of(5, 1L));
+        Thread.sleep(2000); // wait compact-delta refresh
+
+        sql("INSERT INTO T VALUES (11)");
+        assertThat(iterator.collect(1)).containsExactly(Row.of(11, null, 
null));
+
+        // reinsert the key with another value of f1
+        sql("INSERT INTO SEC_DIM VALUES (1, 22, 3)");
+        Thread.sleep(2000); // wait compact-delta refresh
+
+        sql("INSERT INTO T VALUES (11), (22)");
+        assertThat(iterator.collect(2))
+                .containsExactlyInAnyOrder(Row.of(11, null, null), Row.of(22, 
1, 3));
+        iterator.close();
+    }
+
     @Test
     public void testLookupIgnoreScanOptions() throws Exception {
         sql(
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/LookupTableTest.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/LookupTableTest.java
index 650c74a77d..549877d80b 100644
--- 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/LookupTableTest.java
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/lookup/LookupTableTest.java
@@ -435,6 +435,34 @@ public class LookupTableTest extends TableTestBase {
         assertThat(result.stream().map(row -> row.getInt(0))).contains(1);
     }
 
+    @TestTemplate
+    public void testSecKeyTableDeleteWithoutSecKey() throws Exception {
+        FileStoreTable storeTable = createTable(singletonList("f0"), new 
Options());
+        FullCacheLookupTable.Context context =
+                new FullCacheLookupTable.Context(
+                        storeTable,
+                        new int[] {0, 1, 2},
+                        null,
+                        null,
+                        tempDir.toFile(),
+                        singletonList("f1"),
+                        null);
+        table = FullCacheLookupTable.create(context, 
ThreadLocalRandom.current().nextInt(2) * 10);
+        table.open();
+
+        table.refresh(singletonList(row(1, 11, 111)).iterator());
+        assertThat(table.get(row(11))).hasSize(1);
+
+        // a delete record that carries only the key
+        table.refresh(singletonList(row(RowKind.DELETE, 1, null, 
222)).iterator());
+        assertThat(table.get(row(11))).isEmpty();
+
+        // reinsert the key with another sec key
+        table.refresh(singletonList(row(1, 22, 333)).iterator());
+        assertThat(table.get(row(11))).isEmpty();
+        assertThat(table.get(row(22)).stream().map(row -> 
row.getInt(2))).containsExactly(333);
+    }
+
     @TestTemplate
     public void testSecKeyTableWithSequenceField() throws Exception {
         Options options = new Options();

Reply via email to