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

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


The following commit(s) were added to refs/heads/master by this push:
     new 82ccd7820b Core: Fail branch creation on empty table when branch 
exists (#8197)
82ccd7820b is described below

commit 82ccd7820b057f3759a978e1f040c18e5eb8d8f9
Author: Xianyang Liu <[email protected]>
AuthorDate: Tue Aug 1 17:03:09 2023 +0800

    Core: Fail branch creation on empty table when branch exists (#8197)
---
 .../main/java/org/apache/iceberg/SnapshotManager.java    |  2 ++
 .../java/org/apache/iceberg/TestSnapshotManager.java     | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/core/src/main/java/org/apache/iceberg/SnapshotManager.java 
b/core/src/main/java/org/apache/iceberg/SnapshotManager.java
index 75ccad0177..c9774f3b92 100644
--- a/core/src/main/java/org/apache/iceberg/SnapshotManager.java
+++ b/core/src/main/java/org/apache/iceberg/SnapshotManager.java
@@ -75,6 +75,8 @@ public class SnapshotManager implements ManageSnapshots {
       return createBranch(name, currentSnapshot.snapshotId());
     }
 
+    SnapshotRef existingRef = transaction.currentMetadata().ref(name);
+    Preconditions.checkArgument(existingRef == null, "Ref %s already exists", 
name);
     // Create an empty snapshot for the branch
     transaction.newFastAppend().toBranch(name).commit();
     return this;
diff --git a/core/src/test/java/org/apache/iceberg/TestSnapshotManager.java 
b/core/src/test/java/org/apache/iceberg/TestSnapshotManager.java
index 67a383583e..d497dbd360 100644
--- a/core/src/test/java/org/apache/iceberg/TestSnapshotManager.java
+++ b/core/src/test/java/org/apache/iceberg/TestSnapshotManager.java
@@ -262,6 +262,22 @@ public class TestSnapshotManager extends TableTestBase {
     Assertions.assertThat(snapshot.removedDeleteFiles(table.io())).isEmpty();
   }
 
+  @Test
+  public void testCreateBranchOnEmptyTableFailsWhenRefAlreadyExists() {
+    table.manageSnapshots().createBranch("branch1").commit();
+
+    // Trying to create a branch with an existing name should fail
+    Assertions.assertThatThrownBy(() -> 
table.manageSnapshots().createBranch("branch1").commit())
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Ref branch1 already exists");
+
+    // Trying to create another branch within the same chain
+    Assertions.assertThatThrownBy(
+            () -> 
table.manageSnapshots().createBranch("branch2").createBranch("branch2").commit())
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Ref branch2 already exists");
+  }
+
   @Test
   public void testCreateBranchFailsWhenRefAlreadyExists() {
     table.newAppend().appendFile(FILE_A).commit();

Reply via email to