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

lzljs3620320 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 ef3bec1e80 [core] not allowed to delete fallback branch directly 
(#5286)
ef3bec1e80 is described below

commit ef3bec1e809584e981fe52b62b6c5aa2314b3c42
Author: LsomeYeah <[email protected]>
AuthorDate: Mon Mar 17 10:28:54 2025 +0800

    [core] not allowed to delete fallback branch directly (#5286)
---
 .../paimon/table/AbstractFileStoreTable.java       | 13 ++++++++
 .../paimon/table/FileStoreTableTestBase.java       | 16 ++++++++++
 .../flink/PrimaryKeyFileStoreTableITCase.java      | 36 ++++++++++++++++++++++
 3 files changed, 65 insertions(+)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java 
b/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
index 46554e24d6..bd6c76d484 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
@@ -62,6 +62,7 @@ import org.apache.paimon.utils.SegmentsCache;
 import org.apache.paimon.utils.SimpleFileReader;
 import org.apache.paimon.utils.SnapshotManager;
 import org.apache.paimon.utils.SnapshotNotExistException;
+import org.apache.paimon.utils.StringUtils;
 import org.apache.paimon.utils.TagManager;
 
 import 
org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Cache;
@@ -647,6 +648,18 @@ abstract class AbstractFileStoreTable implements 
FileStoreTable {
 
     @Override
     public void deleteBranch(String branchName) {
+        String fallbackBranch =
+                
coreOptions().toConfiguration().get(CoreOptions.SCAN_FALLBACK_BRANCH);
+        if (!StringUtils.isNullOrWhitespaceOnly(fallbackBranch)
+                && branchName.equals(fallbackBranch)) {
+            throw new IllegalArgumentException(
+                    String.format(
+                            "can not delete the fallback branch. "
+                                    + "branchName to be deleted is %s. you 
have set 'scan.fallback-branch' = '%s'. "
+                                    + "you should reset 'scan.fallback-branch' 
before deleting this branch.",
+                            branchName, fallbackBranch));
+        }
+
         branchManager().dropBranch(branchName);
     }
 
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/table/FileStoreTableTestBase.java 
b/paimon-core/src/test/java/org/apache/paimon/table/FileStoreTableTestBase.java
index 78ecbb4d11..8a8fd5e7f7 100644
--- 
a/paimon-core/src/test/java/org/apache/paimon/table/FileStoreTableTestBase.java
+++ 
b/paimon-core/src/test/java/org/apache/paimon/table/FileStoreTableTestBase.java
@@ -1269,6 +1269,22 @@ public abstract class FileStoreTableTestBase {
                         anyCauseMatches(
                                 IllegalArgumentException.class,
                                 "Branch name 'branch1' doesn't exist."));
+
+        // test delete fallback branch
+        table.createBranch("fallback");
+
+        Map<String, String> dynamicOptions = new HashMap<>();
+        dynamicOptions.put("scan.fallback-branch", "fallback");
+        FileStoreTable table1 = table.copy(dynamicOptions);
+        assertThatThrownBy(() -> table1.deleteBranch("fallback"))
+                .satisfies(
+                        anyCauseMatches(
+                                IllegalArgumentException.class,
+                                "can not delete the fallback branch. "
+                                        + "branchName to be deleted is 
fallback. you have set 'scan.fallback-branch' = 'fallback'. "
+                                        + "you should reset 
'scan.fallback-branch' before deleting this branch."));
+
+        table.deleteBranch("fallback");
     }
 
     @Test
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/PrimaryKeyFileStoreTableITCase.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/PrimaryKeyFileStoreTableITCase.java
index e63a6cba2b..c701cefb30 100644
--- 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/PrimaryKeyFileStoreTableITCase.java
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/PrimaryKeyFileStoreTableITCase.java
@@ -25,6 +25,7 @@ import org.apache.paimon.fs.Path;
 import org.apache.paimon.fs.local.LocalFileIO;
 import org.apache.paimon.fs.local.LocalFileIOLoader;
 import org.apache.paimon.utils.FailingFileIO;
+import org.apache.paimon.utils.StringUtils;
 import org.apache.paimon.utils.TraceableFileIO;
 
 import org.apache.flink.api.common.JobStatus;
@@ -90,6 +91,11 @@ public class PrimaryKeyFileStoreTableITCase extends 
AbstractTestBase {
     }
 
     private String createCatalogSql(String catalogName, String warehouse) {
+
+        return createCatalogSql(catalogName, warehouse, "");
+    }
+
+    private String createCatalogSql(String catalogName, String warehouse, 
String catalogOptions) {
         String defaultPropertyString = "";
         if (!tableDefaultProperties.isEmpty()) {
             defaultPropertyString = ", ";
@@ -102,6 +108,11 @@ public class PrimaryKeyFileStoreTableITCase extends 
AbstractTestBase {
                                                     e.getKey(), e.getValue()))
                             .collect(Collectors.joining(", "));
         }
+        if (!StringUtils.isNullOrWhitespaceOnly(catalogOptions)) {
+            return String.format(
+                    "CREATE CATALOG `%s` WITH ( 'type' = 'paimon', 'warehouse' 
= '%s' %s, %s )",
+                    catalogName, warehouse, defaultPropertyString, 
catalogOptions);
+        }
 
         return String.format(
                 "CREATE CATALOG `%s` WITH ( 'type' = 'paimon', 'warehouse' = 
'%s' %s )",
@@ -629,6 +640,31 @@ public class PrimaryKeyFileStoreTableITCase extends 
AbstractTestBase {
                         "The next expected snapshot is too big! Most possible 
cause might be the table had been recreated.");
     }
 
+    @Test
+    public void testDeleteFallbackBranch() {
+        TableEnvironment bEnv = tableEnvironmentBuilder().batchMode().build();
+        bEnv.executeSql(
+                createCatalogSql("testCatalog", path + "/warehouse", 
"'cache-enabled' = 'false'"));
+        bEnv.executeSql("USE CATALOG testCatalog");
+        bEnv.executeSql(
+                "CREATE TABLE t ( pt INT, k INT, v INT, PRIMARY KEY (pt, k) 
NOT ENFORCED ) "
+                        + "PARTITIONED BY (pt) "
+                        + "WITH ("
+                        + "    'bucket' = '2'\n"
+                        + "    ,'continuous.discovery-interval' = '1s'\n"
+                        + ")");
+        bEnv.executeSql("CALL sys.create_branch('default.t', 'branch1')");
+        bEnv.executeSql("ALTER TABLE t SET ('scan.fallback-branch' = 
'branch1')");
+        // branch1 is fallback branch, can not be deleted
+        assertThatCode(() -> bEnv.executeSql("CALL 
sys.delete_branch('default.t', 'branch1')"))
+                .rootCause()
+                .hasMessageContaining("can not delete the fallback branch.");
+
+        // reset scan.fallback-branch
+        bEnv.executeSql("ALTER TABLE t RESET ('scan.fallback-branch')");
+        bEnv.executeSql("CALL sys.delete_branch('default.t', 'branch1')");
+    }
+
     @Test
     @Timeout(TIMEOUT)
     public void testChangelogCompactInBatchWrite() throws Exception {

Reply via email to