diff --git a/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergStorageHandler.java b/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergStorageHandler.java index 537ffd28d411..1088d6d43025 100644 --- a/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergStorageHandler.java +++ b/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergStorageHandler.java @@ -943,9 +943,6 @@ public void alterTableSnapshotRefOperation(org.apache.hadoop.hive.ql.metadata.Ta AlterTableSnapshotRefSpec alterTableSnapshotRefSpec) { TableDesc tableDesc = Utilities.getTableDesc(hmsTable); Table icebergTable = IcebergTableUtil.getTable(conf, tableDesc.getProperties()); - Optional.ofNullable(icebergTable.currentSnapshot()).orElseThrow(() -> - new UnsupportedOperationException(String.format("Cannot alter %s on iceberg table %s.%s which has no snapshot", - alterTableSnapshotRefSpec.getOperationType().getName(), hmsTable.getDbName(), hmsTable.getTableName()))); switch (alterTableSnapshotRefSpec.getOperationType()) { case CREATE_BRANCH: @@ -954,6 +951,10 @@ public void alterTableSnapshotRefOperation(org.apache.hadoop.hive.ql.metadata.Ta IcebergBranchExec.createBranch(icebergTable, createBranchSpec); break; case CREATE_TAG: + Optional.ofNullable(icebergTable.currentSnapshot()).orElseThrow(() -> new UnsupportedOperationException( + String.format("Cannot alter %s on iceberg table %s.%s which has no snapshot", + alterTableSnapshotRefSpec.getOperationType().getName(), hmsTable.getDbName(), + hmsTable.getTableName()))); AlterTableSnapshotRefSpec.CreateSnapshotRefSpec createTagSpec = (AlterTableSnapshotRefSpec.CreateSnapshotRefSpec) alterTableSnapshotRefSpec.getOperationParams(); IcebergTagExec.createTag(icebergTable, createTagSpec); diff --git a/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/IcebergBranchExec.java b/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/IcebergBranchExec.java index bd92b577114c..7425ff95c661 100644 --- a/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/IcebergBranchExec.java +++ b/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/IcebergBranchExec.java @@ -19,6 +19,7 @@ package org.apache.iceberg.mr.hive; +import java.util.Optional; import org.apache.hadoop.hive.ql.parse.AlterTableSnapshotRefSpec; import org.apache.iceberg.ManageSnapshots; import org.apache.iceberg.SnapshotRef; @@ -55,11 +56,16 @@ public static void createBranch(Table table, AlterTableSnapshotRefSpec.CreateSna throw new IllegalArgumentException(String.format("Tag %s does not exist", tagName)); } } else { - snapshotId = table.currentSnapshot().snapshotId(); + snapshotId = Optional.ofNullable(table.currentSnapshot()).map(snapshot -> snapshot.snapshotId()).orElse(null); } - LOG.info("Creating branch {} on iceberg table {} with snapshotId {}", branchName, table.name(), snapshotId); ManageSnapshots manageSnapshots = table.manageSnapshots(); - manageSnapshots.createBranch(branchName, snapshotId); + if (snapshotId != null) { + LOG.info("Creating a branch {} on an iceberg table {} with snapshotId {}", branchName, table.name(), snapshotId); + manageSnapshots.createBranch(branchName, snapshotId); + } else { + LOG.info("Creating a branch {} on an empty iceberg table {}", branchName, table.name()); + manageSnapshots.createBranch(branchName); + } if (createBranchSpec.getMaxRefAgeMs() != null) { manageSnapshots.setMaxRefAgeMs(branchName, createBranchSpec.getMaxRefAgeMs()); } diff --git a/iceberg/iceberg-handler/src/test/queries/negative/alter_table_create_branch_negative.q b/iceberg/iceberg-handler/src/test/queries/negative/alter_table_create_branch_negative.q deleted file mode 100644 index 45078a252b38..000000000000 --- a/iceberg/iceberg-handler/src/test/queries/negative/alter_table_create_branch_negative.q +++ /dev/null @@ -1,3 +0,0 @@ -create table ice_tbl (id int, name string) Stored by Iceberg; - -alter table ice_tbl create branch test_branch_1; diff --git a/iceberg/iceberg-handler/src/test/queries/positive/alter_table_create_branch.q b/iceberg/iceberg-handler/src/test/queries/positive/alter_table_create_branch.q index 4b525d71be6c..6e83bef5a4ce 100644 --- a/iceberg/iceberg-handler/src/test/queries/positive/alter_table_create_branch.q +++ b/iceberg/iceberg-handler/src/test/queries/positive/alter_table_create_branch.q @@ -3,7 +3,10 @@ set hive.explain.user=false; create table iceTbl (id int, name string) Stored by Iceberg; --- creating branch requires table to have current snapshot. here insert some values to generate current snapshot +-- create a branch on an empty table +explain alter table iceTbl create branch test_branch_0; +alter table iceTbl create branch test_branch_0; + insert into iceTbl values(1, 'jack'); -- create s branch test_branch_1 with default values based on the current snapshotId diff --git a/iceberg/iceberg-handler/src/test/results/negative/alter_table_create_branch_negative.q.out b/iceberg/iceberg-handler/src/test/results/negative/alter_table_create_branch_negative.q.out deleted file mode 100644 index 75c7f0936c6e..000000000000 --- a/iceberg/iceberg-handler/src/test/results/negative/alter_table_create_branch_negative.q.out +++ /dev/null @@ -1,12 +0,0 @@ -PREHOOK: query: create table ice_tbl (id int, name string) Stored by Iceberg -PREHOOK: type: CREATETABLE -PREHOOK: Output: database:default -PREHOOK: Output: default@ice_tbl -POSTHOOK: query: create table ice_tbl (id int, name string) Stored by Iceberg -POSTHOOK: type: CREATETABLE -POSTHOOK: Output: database:default -POSTHOOK: Output: default@ice_tbl -PREHOOK: query: alter table ice_tbl create branch test_branch_1 -PREHOOK: type: ALTERTABLE_CREATEBRANCH -PREHOOK: Input: default@ice_tbl -FAILED: Execution Error, return code 40000 from org.apache.hadoop.hive.ql.ddl.DDLTask. java.lang.UnsupportedOperationException: Cannot alter create branch on iceberg table default.ice_tbl which has no snapshot diff --git a/iceberg/iceberg-handler/src/test/results/positive/alter_table_create_branch.q.out b/iceberg/iceberg-handler/src/test/results/positive/alter_table_create_branch.q.out index 5e86f7e7c9a9..f86670d182a7 100644 --- a/iceberg/iceberg-handler/src/test/results/positive/alter_table_create_branch.q.out +++ b/iceberg/iceberg-handler/src/test/results/positive/alter_table_create_branch.q.out @@ -6,6 +6,27 @@ POSTHOOK: query: create table iceTbl (id int, name string) Stored by Iceberg POSTHOOK: type: CREATETABLE POSTHOOK: Output: database:default POSTHOOK: Output: default@iceTbl +PREHOOK: query: explain alter table iceTbl create branch test_branch_0 +PREHOOK: type: ALTERTABLE_CREATEBRANCH +PREHOOK: Input: default@icetbl +POSTHOOK: query: explain alter table iceTbl create branch test_branch_0 +POSTHOOK: type: ALTERTABLE_CREATEBRANCH +POSTHOOK: Input: default@icetbl +STAGE DEPENDENCIES: + Stage-0 is a root stage + +STAGE PLANS: + Stage: Stage-0 + SnapshotRef Operation + table name: default.iceTbl + spec: AlterTableSnapshotRefSpec{operationType=CREATE_BRANCH, operationParams=CreateSnapshotRefSpec{refName=test_branch_0}} + +PREHOOK: query: alter table iceTbl create branch test_branch_0 +PREHOOK: type: ALTERTABLE_CREATEBRANCH +PREHOOK: Input: default@icetbl +POSTHOOK: query: alter table iceTbl create branch test_branch_0 +POSTHOOK: type: ALTERTABLE_CREATEBRANCH +POSTHOOK: Input: default@icetbl PREHOOK: query: insert into iceTbl values(1, 'jack') PREHOOK: type: QUERY PREHOOK: Input: _dummy_database@_dummy_table