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 f5d4bf89fb3 HIVE-27711: Iceberg: Allow creating a branch from tag name
(#4735). (zhangbutao, reviewed by Ayush Saxena)
f5d4bf89fb3 is described below
commit f5d4bf89fb37458a3818d5d5c612b29defb1daa5
Author: Butao Zhang <[email protected]>
AuthorDate: Sat Sep 23 20:56:35 2023 +0800
HIVE-27711: Iceberg: Allow creating a branch from tag name (#4735).
(zhangbutao, reviewed by Ayush Saxena)
---
.../apache/iceberg/mr/hive/IcebergBranchExec.java | 6 +++++
.../mr/hive/TestHiveIcebergBranchOperation.java | 28 ++++++++++++++++++++++
.../queries/positive/alter_table_create_branch.q | 5 ++++
.../positive/alter_table_create_branch.q.out | 27 +++++++++++++++++++++
.../hadoop/hive/ql/parse/AlterClauseParser.g | 3 +++
.../org/apache/hadoop/hive/ql/parse/HiveParser.g | 1 +
.../AlterTableCreateSnapshotRefAnalyzer.java | 6 ++++-
.../hive/ql/parse/AlterTableSnapshotRefSpec.java | 8 ++++++-
8 files changed, 82 insertions(+), 2 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 57d3457b653..61b0dbadd55 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,8 +19,10 @@
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;
@@ -46,6 +48,10 @@ public class IcebergBranchExec {
snapshotId = createBranchSpec.getSnapshotId();
} 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();
} 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 07f2e7a6723..4a68af4f889 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
@@ -23,6 +23,7 @@ import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.iceberg.SnapshotRef;
import org.apache.iceberg.Table;
+import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;
@@ -197,4 +198,31 @@ public class TestHiveIcebergBranchOperation extends
HiveIcebergStorageHandlerWit
table.refresh();
Assert.assertNull(table.refs().get(branchName));
}
+
+ @Test
+ public void testCreateBranchFromTag() throws IOException,
InterruptedException {
+ Table table =
+ testTables.createTableWithVersions(shell, "customers",
HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA,
+ fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS,
2);
+ String branchName1 = "test_branch_1";
+ String tagName = "test_tag";
+ String branchName2 = "test_branch_2";
+ String nonExistTag = "test_non_exist_tag";
+ shell.executeStatement(String.format("ALTER TABLE customers CREATE TAG
%s", tagName));
+
+ // Create a branch based on an existing tag.
+ shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH
%s FOR TAG AS OF %s",
+ branchName1, tagName));
+ table.refresh();
+ SnapshotRef ref = table.refs().get(branchName1);
+ Assert.assertEquals(table.currentSnapshot().snapshotId(),
ref.snapshotId());
+ Assert.assertNull(ref.minSnapshotsToKeep());
+ Assert.assertNull(ref.maxSnapshotAgeMs());
+ Assert.assertNull(ref.maxRefAgeMs());
+
+ // Create a branch based on a tag which doesn't exist will fail.
+ 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");
+ }
}
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 d647a2eda5f..4b525d71be6 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
@@ -33,6 +33,11 @@ alter table iceTbl create branch test_branch_4 with snapshot
retention 5 snapsho
-- check the values, four values
select * from iceTbl for system_version as of 'test_branch_4';
+-- Create a branch based on an existing tag.
+alter table iceTbl create tag test_tag;
+explain alter table iceTbl create branch test_branch_10 for tag as of test_tag;
+alter table iceTbl create branch test_branch_10 for tag as of test_tag;
+
-- drop a branch
explain alter table iceTbl drop branch test_branch_3;
alter table iceTbl drop branch test_branch_3;
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 29ca70a69cf..5e86f7e7c9a 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
@@ -164,6 +164,33 @@ POSTHOOK: Output: hdfs://### HDFS PATH ###
2 bob
3 tom
4 lisa
+PREHOOK: query: alter table iceTbl create tag test_tag
+PREHOOK: type: ALTERTABLE_CREATETAG
+PREHOOK: Input: default@icetbl
+POSTHOOK: query: alter table iceTbl create tag test_tag
+POSTHOOK: type: ALTERTABLE_CREATETAG
+POSTHOOK: Input: default@icetbl
+PREHOOK: query: explain alter table iceTbl create branch test_branch_10 for
tag as of test_tag
+PREHOOK: type: ALTERTABLE_CREATEBRANCH
+PREHOOK: Input: default@icetbl
+POSTHOOK: query: explain alter table iceTbl create branch test_branch_10 for
tag as of test_tag
+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_10}}
+
+PREHOOK: query: alter table iceTbl create branch test_branch_10 for tag as of
test_tag
+PREHOOK: type: ALTERTABLE_CREATEBRANCH
+PREHOOK: Input: default@icetbl
+POSTHOOK: query: alter table iceTbl create branch test_branch_10 for tag as of
test_tag
+POSTHOOK: type: ALTERTABLE_CREATEBRANCH
+POSTHOOK: Input: default@icetbl
PREHOOK: query: explain alter table iceTbl drop branch test_branch_3
PREHOOK: type: ALTERTABLE_DROPBRANCH
PREHOOK: Input: default@icetbl
diff --git
a/parser/src/java/org/apache/hadoop/hive/ql/parse/AlterClauseParser.g
b/parser/src/java/org/apache/hadoop/hive/ql/parse/AlterClauseParser.g
index 4cb653fed49..48a2aed9617 100644
--- a/parser/src/java/org/apache/hadoop/hive/ql/parse/AlterClauseParser.g
+++ b/parser/src/java/org/apache/hadoop/hive/ql/parse/AlterClauseParser.g
@@ -507,6 +507,9 @@ snapshotIdOfRef
|
(KW_FOR KW_SYSTEM_TIME KW_AS KW_OF asOfTime=StringLiteral)
-> ^(TOK_AS_OF_TIME $asOfTime)
+ |
+ (KW_FOR KW_TAG KW_AS KW_OF asOfTag=identifier)
+ -> ^(TOK_AS_OF_TAG $asOfTag)
;
refRetain
diff --git a/parser/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g
b/parser/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g
index d8a8ce8b27d..586ea34af3e 100644
--- a/parser/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g
+++ b/parser/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g
@@ -514,6 +514,7 @@ TOK_BUCKET;
TOK_AS_OF_TIME;
TOK_AS_OF_VERSION;
TOK_FROM_VERSION;
+TOK_AS_OF_TAG;
}
diff --git
a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/snapshotref/AlterTableCreateSnapshotRefAnalyzer.java
b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/snapshotref/AlterTableCreateSnapshotRefAnalyzer.java
index 8708d3d3479..3b581ddfa0b 100644
---
a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/snapshotref/AlterTableCreateSnapshotRefAnalyzer.java
+++
b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/snapshotref/AlterTableCreateSnapshotRefAnalyzer.java
@@ -62,6 +62,7 @@ public abstract class AlterTableCreateSnapshotRefAnalyzer
extends AbstractAlterT
Long maxRefAgeMs = null;
Integer minSnapshotsToKeep = null;
Long maxSnapshotAgeMs = null;
+ String asOfTag = null;
for (int i = 1; i < command.getChildCount(); i++) {
ASTNode childNode = (ASTNode) command.getChild(i);
switch (childNode.getToken().getType()) {
@@ -74,6 +75,9 @@ public abstract class AlterTableCreateSnapshotRefAnalyzer
extends AbstractAlterT
TimestampTZ ts =
TimestampTZUtil.parse(stripQuotes(childNode.getChild(0).getText()), timeZone);
asOfTime = ts.toEpochMilli();
break;
+ case HiveParser.TOK_AS_OF_TAG:
+ asOfTag = unescapeIdentifier(childNode.getChild(0).getText());
+ break;
case HiveParser.TOK_RETAIN:
String maxRefAge = childNode.getChild(0).getText();
String timeUnitOfBranchRetain = childNode.getChild(1).getText();
@@ -96,7 +100,7 @@ public abstract class AlterTableCreateSnapshotRefAnalyzer
extends AbstractAlterT
AlterTableSnapshotRefSpec.CreateSnapshotRefSpec createSnapshotRefSpec =
new AlterTableSnapshotRefSpec.CreateSnapshotRefSpec(refName,
snapshotId, asOfTime,
- maxRefAgeMs, minSnapshotsToKeep, maxSnapshotAgeMs);
+ maxRefAgeMs, minSnapshotsToKeep, maxSnapshotAgeMs, asOfTag);
AlterTableSnapshotRefSpec<AlterTableSnapshotRefSpec.CreateSnapshotRefSpec>
alterTableSnapshotRefSpec
= new AlterTableSnapshotRefSpec(alterTableType, createSnapshotRefSpec);
AbstractAlterTableDesc alterTableDesc =
diff --git
a/ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableSnapshotRefSpec.java
b/ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableSnapshotRefSpec.java
index 1cd98fbef33..1300f5f5a1b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableSnapshotRefSpec.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableSnapshotRefSpec.java
@@ -53,6 +53,7 @@ public class AlterTableSnapshotRefSpec<T> {
private Long maxRefAgeMs;
private Integer minSnapshotsToKeep;
private Long maxSnapshotAgeMs;
+ private String asOfTag;
public String getRefName() {
return refName;
@@ -78,14 +79,19 @@ public class AlterTableSnapshotRefSpec<T> {
return maxSnapshotAgeMs;
}
+ public String getAsOfTag() {
+ return asOfTag;
+ }
+
public CreateSnapshotRefSpec(String refName, Long snapShotId, Long
asOfTime, Long maxRefAgeMs,
- Integer minSnapshotsToKeep, Long
maxSnapshotAgeMs) {
+ Integer minSnapshotsToKeep, Long
maxSnapshotAgeMs, String asOfTag) {
this.refName = refName;
this.snapshotId = snapShotId;
this.asOfTime = asOfTime;
this.maxRefAgeMs = maxRefAgeMs;
this.minSnapshotsToKeep = minSnapshotsToKeep;
this.maxSnapshotAgeMs = maxSnapshotAgeMs;
+ this.asOfTag = asOfTag;
}
public String toString() {