This is an automated email from the ASF dual-hosted git repository.
ayushsaxena pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git
The following commit(s) were added to refs/heads/master by this push:
new aa0237d6209 HIVE-27880: Iceberg: Support creating a branch on an empty
table (#4875). (zhangbutao, reviewed by Ayush Saxena)
aa0237d6209 is described below
commit aa0237d62099d23bcfadb1ff4c4171a15de25447
Author: Butao Zhang <[email protected]>
AuthorDate: Sat Nov 18 20:48:52 2023 +0800
HIVE-27880: Iceberg: Support creating a branch on an empty table (#4875).
(zhangbutao, reviewed by Ayush Saxena)
---
.../iceberg/mr/hive/HiveIcebergStorageHandler.java | 7 ++++---
.../apache/iceberg/mr/hive/IcebergBranchExec.java | 12 +++++++++---
.../negative/alter_table_create_branch_negative.q | 3 ---
.../queries/positive/alter_table_create_branch.q | 5 ++++-
.../alter_table_create_branch_negative.q.out | 12 ------------
.../positive/alter_table_create_branch.q.out | 21 +++++++++++++++++++++
6 files changed, 38 insertions(+), 22 deletions(-)
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 537ffd28d41..1088d6d4302 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 class HiveIcebergStorageHandler implements
HiveStoragePredicateHandler, H
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 class HiveIcebergStorageHandler implements
HiveStoragePredicateHandler, H
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 bd92b577114..7425ff95c66 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 class IcebergBranchExec {
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 45078a252b3..00000000000
---
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 4b525d71be6..6e83bef5a4c 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 75c7f0936c6..00000000000
---
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 5e86f7e7c9a..f86670d182a 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