This is an automated email from the ASF dual-hosted git repository.
pvary 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 e0f1baaa59 HIVE-26354: Support expiring snapshots on iceberg table
(Peter Vary reviewed by Laszlo Pinter) (#3401)
e0f1baaa59 is described below
commit e0f1baaa59153cc260f0a50f04218a450cbdc1b8
Author: pvary <[email protected]>
AuthorDate: Tue Jun 28 11:55:06 2022 +0200
HIVE-26354: Support expiring snapshots on iceberg table (Peter Vary
reviewed by Laszlo Pinter) (#3401)
---
.../iceberg/mr/hive/HiveIcebergStorageHandler.java | 11 +++++-
.../mr/hive/TestHiveIcebergExpireSnapshots.java | 45 ++++++++++++++++++++++
.../hadoop/hive/ql/parse/AlterClauseParser.g | 2 +
.../apache/hadoop/hive/ql/parse/HiveLexerParent.g | 1 +
.../hadoop/hive/ql/parse/IdentifiersParser.g | 1 +
.../table/execute/AlterTableExecuteAnalyzer.java | 17 +++++++-
.../hive/ql/parse/AlterTableExecuteSpec.java | 29 +++++++++++++-
7 files changed, 100 insertions(+), 6 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 74d75f5741..dcec3468ad 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
@@ -454,10 +454,10 @@ public class HiveIcebergStorageHandler implements
HiveStoragePredicateHandler, H
@Override
public void executeOperation(org.apache.hadoop.hive.ql.metadata.Table
hmsTable, AlterTableExecuteSpec executeSpec) {
+ TableDesc tableDesc = Utilities.getTableDesc(hmsTable);
+ Table icebergTable = IcebergTableUtil.getTable(conf,
tableDesc.getProperties());
switch (executeSpec.getOperationType()) {
case ROLLBACK:
- TableDesc tableDesc = Utilities.getTableDesc(hmsTable);
- Table icebergTable = IcebergTableUtil.getTable(conf,
tableDesc.getProperties());
LOG.info("Executing rollback operation on iceberg table. If you would
like to revert rollback you could " +
"try altering the metadata location to the current metadata
location by executing the following query:" +
"ALTER TABLE {}.{} SET TBLPROPERTIES('metadata_location'='{}').
This operation is supported for Hive " +
@@ -467,6 +467,13 @@ public class HiveIcebergStorageHandler implements
HiveStoragePredicateHandler, H
(AlterTableExecuteSpec.RollbackSpec)
executeSpec.getOperationParams();
IcebergTableUtil.rollback(icebergTable,
rollbackSpec.getRollbackType(), rollbackSpec.getParam());
break;
+ case EXPIRE_SNAPSHOT:
+ LOG.info("Executing expire snapshots operation on iceberg table
{}.{}", hmsTable.getDbName(),
+ hmsTable.getTableName());
+ AlterTableExecuteSpec.ExpireSnapshotsSpec expireSnapshotsSpec =
+ (AlterTableExecuteSpec.ExpireSnapshotsSpec)
executeSpec.getOperationParams();
+
icebergTable.expireSnapshots().expireOlderThan(expireSnapshotsSpec.getTimestampMillis()).commit();
+ break;
default:
throw new UnsupportedOperationException(
String.format("Operation type %s is not supported",
executeSpec.getOperationType().name()));
diff --git
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergExpireSnapshots.java
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergExpireSnapshots.java
new file mode 100644
index 0000000000..3b17e2402b
--- /dev/null
+++
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergExpireSnapshots.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg.mr.hive;
+
+import java.io.IOException;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests covering the rollback feature
+ */
+public class TestHiveIcebergExpireSnapshots extends
HiveIcebergStorageHandlerWithEngineBase {
+
+ @Test
+ public void testExpireSnapshotsWithTimestamp() throws IOException,
InterruptedException {
+ TableIdentifier identifier = TableIdentifier.of("default", "source");
+ Table table = testTables.createTableWithVersions(shell, identifier.name(),
+ HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, fileFormat,
+ HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 5);
+ Assert.assertEquals(5, table.history().size());
+ shell.executeStatement("ALTER TABLE " + identifier.name() + " EXECUTE
EXPIRE_SNAPSHOTS('" +
+ HiveIcebergTestUtils.timestampAfterSnapshot(table, 2) + "')");
+ table.refresh();
+ Assert.assertEquals(2, table.history().size());
+ }
+}
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 a89055d760..e87f96ea94 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
@@ -458,6 +458,8 @@ alterStatementSuffixExecute
@after { gParent.popMsg(state); }
: KW_EXECUTE KW_ROLLBACK LPAREN (rollbackParam=(StringLiteral | Number))
RPAREN
-> ^(TOK_ALTERTABLE_EXECUTE KW_ROLLBACK $rollbackParam)
+ | KW_EXECUTE KW_EXPIRE_SNAPSHOTS LPAREN (expireParam=StringLiteral) RPAREN
+ -> ^(TOK_ALTERTABLE_EXECUTE KW_EXPIRE_SNAPSHOTS $expireParam)
;
fileFormat
diff --git a/parser/src/java/org/apache/hadoop/hive/ql/parse/HiveLexerParent.g
b/parser/src/java/org/apache/hadoop/hive/ql/parse/HiveLexerParent.g
index 4bc7bb2078..449ac6938d 100644
--- a/parser/src/java/org/apache/hadoop/hive/ql/parse/HiveLexerParent.g
+++ b/parser/src/java/org/apache/hadoop/hive/ql/parse/HiveLexerParent.g
@@ -389,6 +389,7 @@ KW_REMOTE: 'REMOTE';
KW_SPEC: 'SPEC';
KW_SYSTEM_TIME: 'SYSTEM_TIME';
KW_SYSTEM_VERSION: 'SYSTEM_VERSION';
+KW_EXPIRE_SNAPSHOTS: 'EXPIRE_SNAPSHOTS';
// Operators
diff --git
a/parser/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
b/parser/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
index e40d266824..921061a635 100644
--- a/parser/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
+++ b/parser/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
@@ -975,6 +975,7 @@ nonReserved
| KW_TRIM
| KW_SPEC
| KW_SYSTEM_TIME | KW_SYSTEM_VERSION
+ | KW_EXPIRE_SNAPSHOTS
;
//The following SQL2011 reserved keywords are used as function name only, but
not as identifiers.
diff --git
a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/execute/AlterTableExecuteAnalyzer.java
b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/execute/AlterTableExecuteAnalyzer.java
index 6c4471dc60..7f5d5dd43f 100644
---
a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/execute/AlterTableExecuteAnalyzer.java
+++
b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/execute/AlterTableExecuteAnalyzer.java
@@ -33,6 +33,8 @@ import org.apache.hadoop.hive.ql.metadata.Table;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.AlterTableExecuteSpec;
+import
org.apache.hadoop.hive.ql.parse.AlterTableExecuteSpec.ExpireSnapshotsSpec;
+import org.apache.hadoop.hive.ql.parse.AlterTableExecuteSpec.RollbackSpec;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.hadoop.hive.ql.plan.PlanUtils;
import org.apache.hadoop.hive.ql.session.SessionState;
@@ -40,6 +42,7 @@ import org.apache.hadoop.hive.ql.session.SessionState;
import java.time.ZoneId;
import java.util.Map;
+import static
org.apache.hadoop.hive.ql.parse.AlterTableExecuteSpec.ExecuteOperationType.EXPIRE_SNAPSHOT;
import static
org.apache.hadoop.hive.ql.parse.AlterTableExecuteSpec.ExecuteOperationType.ROLLBACK;
import static
org.apache.hadoop.hive.ql.parse.AlterTableExecuteSpec.RollbackSpec.RollbackType.TIME;
import static
org.apache.hadoop.hive.ql.parse.AlterTableExecuteSpec.RollbackSpec.RollbackType.VERSION;
@@ -72,12 +75,22 @@ public class AlterTableExecuteAnalyzer extends
AbstractAlterTableAnalyzer {
ZoneId timeZone = SessionState.get() == null ? new
HiveConf().getLocalTimeZone() : SessionState.get().getConf()
.getLocalTimeZone();
TimestampTZ time =
TimestampTZUtil.parse(PlanUtils.stripQuotes(child.getText()), timeZone);
- spec = new AlterTableExecuteSpec(ROLLBACK, new
AlterTableExecuteSpec.RollbackSpec(TIME, time.toEpochMilli()));
+ spec = new AlterTableExecuteSpec(ROLLBACK, new RollbackSpec(TIME,
time.toEpochMilli()));
} else {
- spec = new AlterTableExecuteSpec(ROLLBACK, new
AlterTableExecuteSpec.RollbackSpec(VERSION,
+ spec = new AlterTableExecuteSpec(ROLLBACK, new RollbackSpec(VERSION,
Long.valueOf(child.getText())));
}
desc = new AlterTableExecuteDesc(tableName, partitionSpec, spec);
+ } else if (HiveParser.KW_EXPIRE_SNAPSHOTS == executeCommandType.getType())
{
+ AlterTableExecuteSpec<AlterTableExecuteSpec.ExpireSnapshotsSpec> spec;
+ // the second child must be the rollback parameter
+ ASTNode child = (ASTNode) command.getChild(1);
+
+ ZoneId timeZone = SessionState.get() == null ? new
HiveConf().getLocalTimeZone() : SessionState.get().getConf()
+ .getLocalTimeZone();
+ TimestampTZ time =
TimestampTZUtil.parse(PlanUtils.stripQuotes(child.getText()), timeZone);
+ spec = new AlterTableExecuteSpec(EXPIRE_SNAPSHOT, new
ExpireSnapshotsSpec(time.toEpochMilli()));
+ desc = new AlterTableExecuteDesc(tableName, partitionSpec, spec);
}
rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(),
desc)));
diff --git
a/ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableExecuteSpec.java
b/ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableExecuteSpec.java
index 5480b090c3..d1bb59f565 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableExecuteSpec.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/AlterTableExecuteSpec.java
@@ -22,16 +22,18 @@ import com.google.common.base.MoreObjects;
/**
* Execute operation specification. It stores the type of the operation and
its parameters.
- * The following operation are supported
+ * The following operations are supported
* <ul>
* <li>Rollback</li>
+ * <li>EXPIRE_SNAPSHOT</li>
* </ul>
* @param <T> Value object class to store the operation specific parameters
*/
public class AlterTableExecuteSpec<T> {
public enum ExecuteOperationType {
- ROLLBACK
+ ROLLBACK,
+ EXPIRE_SNAPSHOT
}
private final ExecuteOperationType operationType;
@@ -91,4 +93,27 @@ public class AlterTableExecuteSpec<T> {
.add("param", param).toString();
}
}
+
+ /**
+ * Value object class, that stores the expire snapshot operation specific
parameters
+ * <ul>
+ * <li>Expire snapshot value: it should be a timestamp in milliseconds</li>
+ * </ul>
+ */
+ public static class ExpireSnapshotsSpec {
+ private final long timestampMillis;
+
+ public ExpireSnapshotsSpec(long timestampMillis) {
+ this.timestampMillis = timestampMillis;
+ }
+
+ public Long getTimestampMillis() {
+ return timestampMillis;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this).add("timestampMillis",
timestampMillis).toString();
+ }
+ }
}