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 58b8c61e2a [core] Fix main table's CatalogEnvironment used when 
switching branch on fallback table (#7878)
58b8c61e2a is described below

commit 58b8c61e2a2c7471ab326ab68e82de19ddd2c905
Author: XiaoHongbo <[email protected]>
AuthorDate: Mon May 18 15:27:37 2026 +0800

    [core] Fix main table's CatalogEnvironment used when switching branch on 
fallback table (#7878)
    
    When a table has `scan.fallback-branch` or `scan.primary-branch` set,
    `switchToBranch()` passed the main
    table's `CatalogEnvironment` to the branch table. This caused REST
    snapshot loading to query the main
      table instead of the branch, returning the wrong snapshot ID.
---
 .../paimon/table/FallbackReadFileStoreTable.java   | 23 ++++++++++---
 .../table/FallbackReadFileStoreTableTest.java      | 39 ++++++++++++++++++++++
 2 files changed, 57 insertions(+), 5 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/FallbackReadFileStoreTable.java
 
b/paimon-core/src/main/java/org/apache/paimon/table/FallbackReadFileStoreTable.java
index 5c32135f76..75353983bb 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/FallbackReadFileStoreTable.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/FallbackReadFileStoreTable.java
@@ -20,6 +20,7 @@ package org.apache.paimon.table;
 
 import org.apache.paimon.CoreOptions;
 import org.apache.paimon.Snapshot;
+import org.apache.paimon.catalog.Identifier;
 import org.apache.paimon.data.BinaryRow;
 import org.apache.paimon.data.InternalRow;
 import org.apache.paimon.disk.IOManager;
@@ -154,12 +155,24 @@ public class FallbackReadFileStoreTable extends 
DelegatedFileStoreTable {
         Options branchOptions = new Options(branchSchema.options());
         branchOptions.set(CoreOptions.BRANCH, branchName);
         branchSchema = branchSchema.copy(branchOptions.toMap());
+
+        // Create branch-aware CatalogEnvironment so that REST snapshot loading
+        // targets the branch table (e.g. tableName$branch_branchName) instead 
of main.
+        CatalogEnvironment wrappedEnv = wrapped.catalogEnvironment();
+        CatalogEnvironment branchEnv = wrappedEnv;
+        Identifier wrappedId = wrappedEnv.identifier();
+        if (wrappedId != null) {
+            branchEnv =
+                    wrappedEnv.copy(
+                            new Identifier(
+                                    wrappedId.getDatabaseName(),
+                                    wrappedId.getTableName(),
+                                    branchName,
+                                    wrappedId.getSystemTableName()));
+        }
+
         return FileStoreTableFactory.createWithoutFallbackBranch(
-                wrapped.fileIO(),
-                wrapped.location(),
-                branchSchema,
-                new Options(),
-                wrapped.catalogEnvironment());
+                wrapped.fileIO(), wrapped.location(), branchSchema, new 
Options(), branchEnv);
     }
 
     protected Map<String, String> rewriteOtherOptions(Map<String, String> 
options) {
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/table/FallbackReadFileStoreTableTest.java
 
b/paimon-core/src/test/java/org/apache/paimon/table/FallbackReadFileStoreTableTest.java
index 7f586875a1..483ef861eb 100644
--- 
a/paimon-core/src/test/java/org/apache/paimon/table/FallbackReadFileStoreTableTest.java
+++ 
b/paimon-core/src/test/java/org/apache/paimon/table/FallbackReadFileStoreTableTest.java
@@ -19,6 +19,7 @@
 package org.apache.paimon.table;
 
 import org.apache.paimon.CoreOptions;
+import org.apache.paimon.catalog.Identifier;
 import org.apache.paimon.data.InternalRow;
 import org.apache.paimon.fs.FileIO;
 import org.apache.paimon.fs.FileIOFinder;
@@ -332,6 +333,44 @@ public class FallbackReadFileStoreTableTest {
         };
     }
 
+    @Test
+    void testSwitchToBranch() throws Exception {
+        String branchName = "bc";
+
+        Identifier mainId = Identifier.create("mydb", "mytable");
+        CatalogEnvironment env =
+                new CatalogEnvironment(mainId, "uuid-1", null, null, null, 
null, false, false);
+
+        TableSchema tableSchema =
+                SchemaUtils.forceCommit(
+                        new SchemaManager(LocalFileIO.create(), tablePath),
+                        new Schema(
+                                ROW_TYPE.getFields(),
+                                Collections.singletonList("pt"),
+                                Collections.emptyList(),
+                                Collections.emptyMap(),
+                                ""));
+        AppendOnlyFileStoreTable mainTable =
+                new AppendOnlyFileStoreTable(fileIO, tablePath, tableSchema, 
env);
+
+        writeDataIntoTable(mainTable, 0, rowData(1, 10));
+        mainTable.createBranch(branchName);
+
+        FileStoreTable branchTable = createTableFromBranch(mainTable, 
branchName);
+        writeDataIntoTable(branchTable, 0, rowData(2, 20));
+
+        FallbackReadFileStoreTable fallbackTable =
+                new FallbackReadFileStoreTable(mainTable, branchTable, true);
+
+        FileStoreTable switched = fallbackTable.switchToBranch(branchName);
+        Identifier switchedId = switched.catalogEnvironment().identifier();
+
+        assertThat(switchedId).isNotNull();
+        assertThat(switchedId.getDatabaseName()).isEqualTo("mydb");
+        assertThat(switchedId.getBranchName()).isEqualTo(branchName);
+        assertThat(switchedId.getObjectName()).isEqualTo("mytable$branch_bc");
+    }
+
     private void writeDataIntoTable(
             FileStoreTable table, long commitIdentifier, InternalRow... 
allData) throws Exception {
         StreamTableWrite write = table.newWrite(commitUser);

Reply via email to