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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.spark.sql.execution.datasources.v2

import org.apache.iceberg.relocated.com.google.common.base.Preconditions
import org.apache.iceberg.spark.source.SparkTable
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.Attribute
Expand All @@ -41,10 +42,17 @@ 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())
val snapshotId: java.lang.Long = branchOptions.snapshotId
.orElse(Option(iceberg.table.currentSnapshot()).map(_.snapshotId()))
.map(java.lang.Long.valueOf)
.orNull

Preconditions.checkArgument(snapshotId != null,
"Cannot complete create or replace branch operation on %s, main has no snapshot", ident)

val manageSnapshots = iceberg.table().manageSnapshots()
if (!replace) {
val ref = iceberg.table().refs().get(branch);
val ref = iceberg.table().refs().get(branch)
if (ref != null && ifNotExists) {
return Nil
}
Expand Down Expand Up @@ -76,6 +84,6 @@ case class CreateOrReplaceBranchExec(
}

override def simpleString(maxFields: Int): String = {
s"CreateOrReplace branch: ${branch} for table: ${ident.quoted}"
s"CreateOrReplace branch: $branch for table: ${ident.quoted}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.spark.sql.execution.datasources.v2

import org.apache.iceberg.relocated.com.google.common.base.Preconditions
import org.apache.iceberg.spark.source.SparkTable
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.Attribute
Expand All @@ -40,10 +41,17 @@ case class CreateOrReplaceTagExec(
override protected def run(): Seq[InternalRow] = {
catalog.loadTable(ident) match {
case iceberg: SparkTable =>
val snapshotId = tagOptions.snapshotId.getOrElse(iceberg.table.currentSnapshot().snapshotId())
val snapshotId: java.lang.Long = tagOptions.snapshotId
.orElse(Option(iceberg.table.currentSnapshot()).map(_.snapshotId()))
.map(java.lang.Long.valueOf)
.orNull

Preconditions.checkArgument(snapshotId != null,
"Cannot complete create or replace tag operation on %s, main has no snapshot", ident)

val manageSnapshot = iceberg.table.manageSnapshots()
if (!replace) {
val ref = iceberg.table().refs().get(tag);
val ref = iceberg.table().refs().get(tag)
if (ref != null && ifNotExists) {
return Nil
}
Expand All @@ -67,6 +75,6 @@ case class CreateOrReplaceTagExec(
}

override def simpleString(maxFields: Int): String = {
s"Create tag: ${tag} for table: ${ident.quoted}"
s"Create tag: $tag for table: ${ident.quoted}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.spark.sql.Row;
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
import org.apache.spark.sql.catalyst.parser.extensions.IcebergParseException;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -89,6 +90,15 @@ public void testCreateBranch() throws NoSuchTableException {
() -> sql("ALTER TABLE %s CREATE BRANCH %s", tableName, branchName));
}

@Test
public void testCreateBranchOnEmptyTable() {
Assertions.assertThatThrownBy(() -> sql("ALTER TABLE %s CREATE BRANCH %s", tableName, "b1"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"Cannot complete create or replace branch operation on %s, main has no snapshot",
tableName);
}

@Test
public void testCreateBranchUseDefaultConfig() throws NoSuchTableException {
Table table = insertRows();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.spark.sql.Row;
import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
import org.apache.spark.sql.catalyst.parser.extensions.IcebergParseException;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -119,6 +120,15 @@ public void testCreateTagWithRetain() throws NoSuchTableException {
tableName, tagName, firstSnapshotId, maxRefAge));
}

@Test
public void testCreateTagOnEmptyTable() {
Assertions.assertThatThrownBy(() -> sql("ALTER TABLE %s CREATE TAG %s", tableName, "abc"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"Cannot complete create or replace tag operation on %s, main has no snapshot",
tableName);
}

@Test
public void testCreateTagUseDefaultConfig() throws NoSuchTableException {
Table table = insertRows();
Expand Down