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 80899f86c47 HIVE-27711: Iceberg: Allow creating a branch from tag name 
- ADDENDUM (#4742). (zhangbutao,  reviewed by Ayush Saxena)
80899f86c47 is described below

commit 80899f86c47a2f263c93dbf0e6481fa1feb45ebb
Author: Butao Zhang <[email protected]>
AuthorDate: Tue Sep 26 15:01:28 2023 +0800

    HIVE-27711: Iceberg: Allow creating a branch from tag name - ADDENDUM 
(#4742). (zhangbutao,  reviewed by Ayush Saxena)
---
 .../java/org/apache/iceberg/mr/hive/IcebergBranchExec.java   | 12 +++++++-----
 .../iceberg/mr/hive/TestHiveIcebergBranchOperation.java      |  5 +++++
 2 files changed, 12 insertions(+), 5 deletions(-)

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 61b0dbadd55..bd92b577114 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,10 +19,8 @@
 
 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.Snapshot;
 import org.apache.iceberg.SnapshotRef;
 import org.apache.iceberg.Table;
 import org.apache.iceberg.util.SnapshotUtil;
@@ -49,9 +47,13 @@ public class IcebergBranchExec {
     } else if (createBranchSpec.getAsOfTime() != null) {
       snapshotId = SnapshotUtil.snapshotIdAsOfTime(table, 
createBranchSpec.getAsOfTime());
     } else if (createBranchSpec.getAsOfTag() != null) {
-      Snapshot snapshot = 
Optional.ofNullable(table.snapshot(createBranchSpec.getAsOfTag())).orElseThrow(()
 ->
-          new IllegalArgumentException(String.format("Tag %s does not exist", 
createBranchSpec.getAsOfTag())));
-      snapshotId = snapshot.snapshotId();
+      String tagName = createBranchSpec.getAsOfTag();
+      SnapshotRef snapshotRef = table.refs().get(tagName);
+      if (snapshotRef != null && snapshotRef.isTag()) {
+        snapshotId = snapshotRef.snapshotId();
+      } else {
+        throw new IllegalArgumentException(String.format("Tag %s does not 
exist", tagName));
+      }
     } else {
       snapshotId = table.currentSnapshot().snapshotId();
     }
diff --git 
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergBranchOperation.java
 
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergBranchOperation.java
index 4a68af4f889..24816c9abaa 100644
--- 
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergBranchOperation.java
+++ 
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergBranchOperation.java
@@ -224,5 +224,10 @@ public class TestHiveIcebergBranchOperation extends 
HiveIcebergStorageHandlerWit
     Assertions.assertThatThrownBy(() -> shell.executeStatement(String.format(
         "ALTER TABLE customers CREATE BRANCH %s FOR TAG AS OF %s", 
branchName2, nonExistTag)))
         
.isInstanceOf(IllegalArgumentException.class).hasMessageEndingWith("does not 
exist");
+
+    // Create a branch based on a branch will fail.
+    Assertions.assertThatThrownBy(() -> shell.executeStatement(String.format(
+            "ALTER TABLE customers CREATE BRANCH %s FOR TAG AS OF %s", 
branchName2, branchName1)))
+        
.isInstanceOf(IllegalArgumentException.class).hasMessageEndingWith("does not 
exist");
   }
 }

Reply via email to