Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/iceberg/SnapshotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public ManageSnapshots createBranch(String name) {
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;
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestSnapshotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ public void testCreateBranchOnEmptyTable() {
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();
Expand Down