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 48da48ae2e [core] Pin eager Data Evolution index queries to the scan 
snapshot (#11318)
48da48ae2e is described below

commit 48da48ae2e7ba1f29fb380218481d3555c62f1c0
Author: Jingsong Lee <[email protected]>
AuthorDate: Sat Nov 21 16:04:37 2026 +0800

    [core] Pin eager Data Evolution index queries to the scan snapshot (#11318)
---
 .../paimon/globalindex/DataEvolutionBatchScan.java |  6 ++-
 .../DataEvolutionGlobalIndexScanner.java           | 24 +++++++++++-
 .../table/source/AbstractBatchTableScan.java       | 17 ++++++++
 .../source/snapshot/ReadPlanStartingScanner.java   | 15 +++++++-
 .../table/source/snapshot/SnapshotReader.java      |  6 +++
 .../table/source/snapshot/SnapshotReaderImpl.java  | 11 +++++-
 .../apache/paimon/table/system/AuditLogTable.java  |  6 +++
 .../apache/paimon/table/IndexQuerySplitTest.java   | 45 ++++++++++++++++++++++
 8 files changed, 123 insertions(+), 7 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionBatchScan.java
 
b/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionBatchScan.java
index d63fbb46cc..da3c379866 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionBatchScan.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionBatchScan.java
@@ -521,7 +521,8 @@ public class DataEvolutionBatchScan implements 
DataTableScan {
                 batchScan.snapshotReader().manifestsReader().partitionFilter();
         long totalStart = System.nanoTime();
         Optional<DataEvolutionGlobalIndexScanner> optionalScanner =
-                DataEvolutionGlobalIndexScanner.create(table, partitionFilter, 
globalIndexFilter);
+                DataEvolutionGlobalIndexScanner.createForPredicate(
+                        table, batchScan.prepareSnapshot(), partitionFilter, 
globalIndexFilter);
         long metadataDuration = System.nanoTime() - totalStart;
         if (!optionalScanner.isPresent()) {
             return Optional.empty();
@@ -560,7 +561,8 @@ public class DataEvolutionBatchScan implements 
DataTableScan {
                 batchScan.snapshotReader().manifestsReader().partitionFilter();
         long totalStart = System.nanoTime();
         Optional<DataEvolutionGlobalIndexScanner> optionalScanner =
-                DataEvolutionGlobalIndexScanner.createForTopN(table, 
partitionFilter, topN);
+                DataEvolutionGlobalIndexScanner.createForTopN(
+                        table, batchScan.prepareSnapshot(), partitionFilter, 
topN);
         long metadataDuration = System.nanoTime() - totalStart;
         if (!optionalScanner.isPresent()) {
             return Optional.empty();
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionGlobalIndexScanner.java
 
b/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionGlobalIndexScanner.java
index 97b22143d9..4316e4f0cb 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionGlobalIndexScanner.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionGlobalIndexScanner.java
@@ -253,7 +253,17 @@ public class DataEvolutionGlobalIndexScanner implements 
Closeable {
             FileStoreTable table,
             @Nullable PartitionPredicate partitionFilter,
             @Nullable Predicate filter) {
-        @Nullable Snapshot snapshot = tryTravelOrLatest(table);
+        return createForPredicate(table, tryTravelOrLatest(table), 
partitionFilter, filter);
+    }
+
+    static Optional<DataEvolutionGlobalIndexScanner> createForPredicate(
+            FileStoreTable table,
+            @Nullable Snapshot snapshot,
+            @Nullable PartitionPredicate partitionFilter,
+            @Nullable Predicate filter) {
+        if (snapshot == null) {
+            return Optional.empty();
+        }
         List<IndexFileMeta> indexFiles =
                 table.store().newIndexFileHandler()
                         .scan(snapshot, indexFileFilter(table, 
partitionFilter, filter)).stream()
@@ -285,10 +295,20 @@ public class DataEvolutionGlobalIndexScanner implements 
Closeable {
         if (!isSupportedTopN(topN)) {
             return Optional.empty();
         }
+        return createForTopN(table, tryTravelOrLatest(table), partitionFilter, 
topN);
+    }
+
+    static Optional<DataEvolutionGlobalIndexScanner> createForTopN(
+            FileStoreTable table,
+            @Nullable Snapshot snapshot,
+            @Nullable PartitionPredicate partitionFilter,
+            TopN topN) {
+        if (snapshot == null || !isSupportedTopN(topN)) {
+            return Optional.empty();
+        }
 
         DataField indexField = 
table.rowType().getField(topN.orders().get(0).field().name());
         int fieldId = indexField.id();
-        @Nullable Snapshot snapshot = tryTravelOrLatest(table);
         List<IndexFileMeta> indexFiles =
                 table.store().newIndexFileHandler()
                         .scan(snapshot, topNIndexFileFilter(partitionFilter, 
fieldId)).stream()
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/source/AbstractBatchTableScan.java
 
b/paimon-core/src/main/java/org/apache/paimon/table/source/AbstractBatchTableScan.java
index 0a19f5326c..d82bb6e343 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/source/AbstractBatchTableScan.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/source/AbstractBatchTableScan.java
@@ -19,6 +19,7 @@
 package org.apache.paimon.table.source;
 
 import org.apache.paimon.CoreOptions;
+import org.apache.paimon.Snapshot;
 import org.apache.paimon.data.BinaryRow;
 import org.apache.paimon.manifest.PartitionEntry;
 import org.apache.paimon.predicate.Predicate;
@@ -27,6 +28,7 @@ import org.apache.paimon.predicate.TopN;
 import org.apache.paimon.schema.SchemaManager;
 import org.apache.paimon.schema.TableSchema;
 import org.apache.paimon.table.BucketMode;
+import org.apache.paimon.table.source.snapshot.ReadPlanStartingScanner;
 import org.apache.paimon.table.source.snapshot.SnapshotReader;
 import org.apache.paimon.table.source.snapshot.StartingScanner;
 import org.apache.paimon.table.source.snapshot.StartingScanner.ScannedResult;
@@ -109,6 +111,21 @@ public abstract class AbstractBatchTableScan extends 
AbstractDataTableScan {
         return this;
     }
 
+    /** Selects the snapshot for a single-snapshot scan before planning its 
data splits. */
+    @Nullable
+    public Snapshot prepareSnapshot() {
+        if (startingScanner == null) {
+            startingScanner = createStartingScanner(false);
+        }
+        if (!(startingScanner instanceof ReadPlanStartingScanner)) {
+            return null;
+        }
+
+        SnapshotReader configured =
+                ((ReadPlanStartingScanner) 
startingScanner).prepareRead(snapshotReader);
+        return configured == null ? null : configured.specifiedSnapshot();
+    }
+
     @Override
     protected final TableScan.Plan planWithoutAuth() {
         if (!hasNext) {
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/ReadPlanStartingScanner.java
 
b/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/ReadPlanStartingScanner.java
index 56b7680eb7..86cf0c02f7 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/ReadPlanStartingScanner.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/ReadPlanStartingScanner.java
@@ -29,6 +29,9 @@ import java.util.List;
 /** An {@link AbstractStartingScanner} to return plan. */
 public abstract class ReadPlanStartingScanner extends AbstractStartingScanner {
 
+    private boolean prepared;
+    @Nullable private SnapshotReader preparedReader;
+
     ReadPlanStartingScanner(SnapshotManager snapshotManager) {
         super(snapshotManager);
     }
@@ -36,9 +39,19 @@ public abstract class ReadPlanStartingScanner extends 
AbstractStartingScanner {
     @Nullable
     protected abstract SnapshotReader configure(SnapshotReader snapshotReader);
 
+    /** Configures the reader once for callers that need the snapshot before 
reading. */
+    @Nullable
+    public SnapshotReader prepareRead(SnapshotReader snapshotReader) {
+        if (!prepared) {
+            preparedReader = configure(snapshotReader);
+            prepared = true;
+        }
+        return preparedReader;
+    }
+
     @Override
     public Result scan(SnapshotReader snapshotReader) {
-        SnapshotReader configured = configure(snapshotReader);
+        SnapshotReader configured = prepared ? preparedReader : 
configure(snapshotReader);
         if (configured == null) {
             return new NoSnapshot();
         }
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReader.java
 
b/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReader.java
index 68611f0091..43421fe3df 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReader.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReader.java
@@ -73,6 +73,12 @@ public interface SnapshotReader {
     @Nullable
     IndexFileHandler indexFileHandler();
 
+    /** Snapshot explicitly selected for this reader, if available before 
planning. */
+    @Nullable
+    default Snapshot specifiedSnapshot() {
+        return null;
+    }
+
     SnapshotReader withSnapshot(long snapshotId);
 
     SnapshotReader withSnapshot(Snapshot snapshot);
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 934e8d39e5..3a59d94f48 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
@@ -105,6 +105,7 @@ public class SnapshotReaderImpl implements SnapshotReader {
     @Nullable private final DVMetaCache dvMetaCache;
 
     private ScanMode scanMode = ScanMode.ALL;
+    @Nullable private Snapshot specifiedSnapshot;
     private boolean hasNonPartitionFilter;
     private RecordComparator lazyPartitionComparator;
     private CacheMetrics dvMetaCacheMetrics;
@@ -186,14 +187,20 @@ public class SnapshotReaderImpl implements SnapshotReader 
{
         return indexFileHandler;
     }
 
+    @Override
+    @Nullable
+    public Snapshot specifiedSnapshot() {
+        return specifiedSnapshot;
+    }
+
     @Override
     public SnapshotReader withSnapshot(long snapshotId) {
-        scan.withSnapshot(snapshotId);
-        return this;
+        return withSnapshot(snapshotManager.snapshot(snapshotId));
     }
 
     @Override
     public SnapshotReader withSnapshot(Snapshot snapshot) {
+        specifiedSnapshot = snapshot;
         scan.withSnapshot(snapshot);
         return this;
     }
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/system/AuditLogTable.java 
b/paimon-core/src/main/java/org/apache/paimon/table/system/AuditLogTable.java
index 32fb8b3b26..a96f43cd4f 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/system/AuditLogTable.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/system/AuditLogTable.java
@@ -350,6 +350,12 @@ public class AuditLogTable implements DataTable, 
ReadonlyTable {
             return null;
         }
 
+        @Override
+        @Nullable
+        public Snapshot specifiedSnapshot() {
+            return wrapped.specifiedSnapshot();
+        }
+
         public SnapshotReader withSnapshot(long snapshotId) {
             wrapped.withSnapshot(snapshotId);
             return this;
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/table/IndexQuerySplitTest.java 
b/paimon-core/src/test/java/org/apache/paimon/table/IndexQuerySplitTest.java
index 3e1612003f..5e2c6ffd1d 100644
--- a/paimon-core/src/test/java/org/apache/paimon/table/IndexQuerySplitTest.java
+++ b/paimon-core/src/test/java/org/apache/paimon/table/IndexQuerySplitTest.java
@@ -73,6 +73,7 @@ import org.apache.paimon.utils.DataEvolutionUtils;
 import org.apache.paimon.utils.InstantiationUtil;
 import org.apache.paimon.utils.JsonSerdeUtil;
 import org.apache.paimon.utils.Range;
+import org.apache.paimon.utils.SnapshotManager;
 
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -328,6 +329,50 @@ public class IndexQuerySplitTest extends 
DataEvolutionTestBase {
         assertThat(read(read, splits)).hasSize(200);
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = {"full", "detail"})
+    public void 
testEagerIndexQueryUsesDataSnapshotAfterConcurrentAppend(String mode)
+            throws Exception {
+        write(100);
+        createIndex("btree", "f1");
+        FileStoreTable table =
+                getTableDefault()
+                        .copy(
+                                Collections.singletonMap(
+                                        
CoreOptions.SCALAR_INDEX_SEARCH_MODE.key(), mode));
+        SnapshotReader snapshotReader = spy(table.newSnapshotReader());
+        SnapshotManager snapshotManager = 
spy(snapshotReader.snapshotManager());
+        doReturn(snapshotManager).when(snapshotReader).snapshotManager();
+        AtomicBoolean appended = new AtomicBoolean();
+        doAnswer(
+                        invocation -> {
+                            // Commit when the data scan selects its snapshot. 
The old eager path
+                            // has already evaluated the index by this point.
+                            if (appended.compareAndSet(false, true)) {
+                                appendRows(100, 200);
+                            }
+                            return invocation.callRealMethod();
+                        })
+                .when(snapshotManager)
+                .latestSnapshot();
+
+        PredicateBuilder predicates = new PredicateBuilder(table.rowType());
+        Predicate predicate = predicates.in(1, Arrays.asList(str("a50"), 
str("a150")));
+        List<Split> splits =
+                table.newScan(ignored -> 
snapshotReader).withFilter(predicate).plan().splits();
+        long snapshotId = table.latestSnapshot().get().id();
+        assertThat(splits)
+                .isNotEmpty()
+                .allSatisfy(
+                        split -> {
+                            assertThat(split).isInstanceOf(IndexedSplit.class);
+                            assertThat(((IndexedSplit) 
split).dataSplit().snapshotId())
+                                    .isEqualTo(snapshotId);
+                        });
+        ReadBuilder read = table.newReadBuilder().withFilter(predicate);
+        assertThat(read(read, splits)).containsExactlyInAnyOrder(50, 150);
+    }
+
     @ParameterizedTest
     @ValueSource(strings = {"btree", "bitmap"})
     public void 
testPlanningDoesNotOpenIndexesAndRestoredSplitUsesPinnedFiles(String indexType)

Reply via email to