Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ case class CreateOrReplaceBranchExec(
override protected def run(): Seq[InternalRow] = {
catalog.loadTable(ident) match {
case iceberg: SparkTable =>
val snapshotId = branchOptions.snapshotId.getOrElse(iceberg.table.currentSnapshot().snapshotId())
var snapshotId = branchOptions.snapshotId.getOrElse(-1L)
if (snapshotId == -1) {
val currentSnapshot = Option(iceberg.table().currentSnapshot()).getOrElse(throw new IllegalArgumentException(
s"Please specify an explicit snapshot as table: $iceberg" + " has no latest main snapshot"))
snapshotId = currentSnapshot.snapshotId()
}
Comment on lines +44 to +49
Copy link
Contributor

@dramaticlly dramaticlly May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var snapshotId = branchOptions.snapshotId.getOrElse(-1L)
if (snapshotId == -1) {
val currentSnapshot = Option(iceberg.table().currentSnapshot()).getOrElse(throw new IllegalArgumentException(
s"Please specify an explicit snapshot as table: $iceberg" + " has no latest main snapshot"))
snapshotId = currentSnapshot.snapshotId()
}
val snapshotId: java.lang.Long = branchOptions.snapshotId
.orElse(Option(iceberg.table.currentSnapshot()).map(_.snapshotId()))
.map(java.lang.Long.valueOf)
.orNull

val manageSnapshots = iceberg.table().manageSnapshots()
if (!replace) {
val ref = iceberg.table().refs().get(branch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ public void testCreateBranchUseCustomMaxRefAge() throws NoSuchTableException {
tableName, branchName, maxRefAge));
}

@Test
public void testCreateBranchOnEmptyTable() throws NoSuchTableException {
createEmptyTable();
String branchName = "b1";
AssertHelpers.assertThrows(
"Illegal argument",
IllegalArgumentException.class,
"has no latest main snapshot",
() -> sql("ALTER TABLE %s CREATE BRANCH %s", tableName, branchName));
}

@Test
public void testDropBranch() throws NoSuchTableException {
insertRows();
Expand Down Expand Up @@ -289,4 +300,8 @@ private Table insertRows() throws NoSuchTableException {
df.writeTo(tableName).append();
return validationCatalog.loadTable(tableIdent);
}

private Table createEmptyTable() {
return validationCatalog.loadTable(tableIdent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not create table but rather load a table from catalog.

I think empty table creation is already handled in before method at line 43 so this method is not needed at all

}
}