zhangbutao commented on code in PR #4216: URL: https://github.com/apache/hive/pull/4216#discussion_r1181105965
########## iceberg/iceberg-handler/src/test/queries/negative/alter_table_create_branch_negative.q: ########## @@ -0,0 +1,3 @@ +create table ice_tbl (id int, name string) Stored by Iceberg; + +alter table ice_tbl create branch test_branch_1; Review Comment: Qtest: creating branch on table without current snapshot will fail ########## iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergBranchOperation.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 java.util.concurrent.TimeUnit; +import org.apache.iceberg.SnapshotRef; +import org.apache.iceberg.Table; +import org.junit.Assert; +import org.junit.Test; + +public class TestHiveIcebergBranchOperation extends HiveIcebergStorageHandlerWithEngineBase { + + @Test + public void testCreateBranchWithDefaultConfig() throws InterruptedException, IOException { + Table table = + testTables.createTableWithVersions(shell, "customers", HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, + fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 2); + + String branchName = "test_branch_1"; + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s", branchName)); + table.refresh(); + SnapshotRef ref = table.refs().get(branchName); + Assert.assertEquals(table.currentSnapshot().snapshotId(), ref.snapshotId()); + Assert.assertNull(ref.minSnapshotsToKeep()); + Assert.assertNull(ref.maxSnapshotAgeMs()); + Assert.assertNull(ref.maxRefAgeMs()); + + // creating a branch which is already exists will fail + try { + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s", branchName)); Review Comment: Test: creating a branch which is already exists will fail ########## iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergStorageHandler.java: ########## @@ -676,6 +678,35 @@ public void executeOperation(org.apache.hadoop.hive.ql.metadata.Table hmsTable, } } + @Override + public void createBranchOperation(org.apache.hadoop.hive.ql.metadata.Table hmsTable, + AlterTableCreateBranchSpec createBranchSpec) { + TableDesc tableDesc = Utilities.getTableDesc(hmsTable); + Table icebergTable = IcebergTableUtil.getTable(conf, tableDesc.getProperties()); + + String branchName = createBranchSpec.getBranchName(); + Optional.ofNullable(icebergTable.currentSnapshot()).orElseThrow(() -> new UnsupportedOperationException( Review Comment: Here give a check if table have current snapshot. ########## iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergBranchOperation.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 java.util.concurrent.TimeUnit; +import org.apache.iceberg.SnapshotRef; +import org.apache.iceberg.Table; +import org.junit.Assert; +import org.junit.Test; + +public class TestHiveIcebergBranchOperation extends HiveIcebergStorageHandlerWithEngineBase { + + @Test + public void testCreateBranchWithDefaultConfig() throws InterruptedException, IOException { + Table table = + testTables.createTableWithVersions(shell, "customers", HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, + fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 2); + + String branchName = "test_branch_1"; + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s", branchName)); + table.refresh(); + SnapshotRef ref = table.refs().get(branchName); + Assert.assertEquals(table.currentSnapshot().snapshotId(), ref.snapshotId()); + Assert.assertNull(ref.minSnapshotsToKeep()); + Assert.assertNull(ref.maxSnapshotAgeMs()); + Assert.assertNull(ref.maxRefAgeMs()); + + // creating a branch which is already exists will fail + try { + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s", branchName)); + } catch (Throwable e) { + while (e.getCause() != null) { + e = e.getCause(); + } + Assert.assertTrue(e.getMessage().contains("Ref test_branch_1 already exists")); + } + } + + @Test + public void testCreateBranchWithSnapshotId() throws InterruptedException, IOException { + Table table = + testTables.createTableWithVersions(shell, "customers", HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, + fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 2); + + String branchName = "test_branch_1"; + Long snapshotId = table.history().get(0).snapshotId(); + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s AS OF VERSION %d", + branchName, snapshotId)); + table.refresh(); + SnapshotRef ref = table.refs().get(branchName); + Assert.assertEquals(snapshotId.longValue(), ref.snapshotId()); + Assert.assertNull(ref.minSnapshotsToKeep()); + Assert.assertNull(ref.maxSnapshotAgeMs()); + Assert.assertNull(ref.maxRefAgeMs()); + } + + @Test + public void testCreateBranchWithMaxRefAge() throws InterruptedException, IOException { + Table table = + testTables.createTableWithVersions(shell, "customers", HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, + fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 2); + + String branchName = "test_branch_1"; + long maxRefAge = 5L; + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s RETAIN %d DAYS", + branchName, maxRefAge)); + table.refresh(); + SnapshotRef ref = table.refs().get(branchName); + Assert.assertEquals(table.currentSnapshot().snapshotId(), ref.snapshotId()); + Assert.assertNull(ref.minSnapshotsToKeep()); + Assert.assertNull(ref.maxSnapshotAgeMs()); + Assert.assertEquals(TimeUnit.DAYS.toMillis(maxRefAge), ref.maxRefAgeMs().longValue()); + } + + @Test + public void testCreateBranchWithMinSnapshotsToKeep() throws InterruptedException, IOException { + Table table = + testTables.createTableWithVersions(shell, "customers", HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, + fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 2); + + String branchName = "test_branch_1"; + Integer minSnapshotsToKeep = 2; + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s WITH SNAPSHOT RETENTION %d SNAPSHOTS", + branchName, minSnapshotsToKeep)); + table.refresh(); + SnapshotRef ref = table.refs().get(branchName); + Assert.assertEquals(table.currentSnapshot().snapshotId(), ref.snapshotId()); + Assert.assertEquals(minSnapshotsToKeep, ref.minSnapshotsToKeep()); + Assert.assertNull(ref.maxSnapshotAgeMs()); + Assert.assertNull(ref.maxRefAgeMs()); + } + + @Test + public void testCreateBranchWithMinSnapshotsToKeepAndMaxSnapshotAge() throws InterruptedException, IOException { + Table table = + testTables.createTableWithVersions(shell, "customers", HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, + fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 2); + + String branchName = "test_branch_1"; + Integer minSnapshotsToKeep = 2; + long maxSnapshotAge = 2L; + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s WITH SNAPSHOT RETENTION %d SNAPSHOTS" + + " %d DAYS", branchName, minSnapshotsToKeep, maxSnapshotAge)); + table.refresh(); + SnapshotRef ref = table.refs().get(branchName); + Assert.assertEquals(table.currentSnapshot().snapshotId(), ref.snapshotId()); + Assert.assertEquals(minSnapshotsToKeep, ref.minSnapshotsToKeep()); + Assert.assertEquals(TimeUnit.DAYS.toMillis(maxSnapshotAge), ref.maxSnapshotAgeMs().longValue()); + Assert.assertNull(ref.maxRefAgeMs()); + } + + @Test + public void testCreateBranchWithAllCustomConfig() throws InterruptedException, IOException { + Table table = + testTables.createTableWithVersions(shell, "customers", HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, + fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS, 2); + + String branchName = "test_branch_1"; + Long snapshotId = table.history().get(0).snapshotId(); + Integer minSnapshotsToKeep = 2; + long maxSnapshotAge = 2L; + long maxRefAge = 5L; + shell.executeStatement(String.format("ALTER TABLE customers CREATE BRANCH %s AS OF VERSION %d RETAIN %d DAYS WITH" + + " SNAPSHOT RETENTION %d SNAPSHOTS %d days", + branchName, snapshotId, maxRefAge, minSnapshotsToKeep, maxSnapshotAge)); + table.refresh(); + SnapshotRef ref = table.refs().get(branchName); + Assert.assertEquals(snapshotId.longValue(), ref.snapshotId()); + Assert.assertEquals(minSnapshotsToKeep, ref.minSnapshotsToKeep()); + Assert.assertEquals(TimeUnit.DAYS.toMillis(maxSnapshotAge), ref.maxSnapshotAgeMs().longValue()); + Assert.assertEquals(TimeUnit.DAYS.toMillis(maxRefAge), ref.maxRefAgeMs().longValue()); + } + + @Test + public void testCreateBranchWithNonIcebergTable() { + shell.executeStatement("create table nonice_tbl (id int, name string)"); + + String branchName = "test_branch_1"; + try { + shell.executeStatement(String.format("ALTER TABLE nonice_tbl CREATE BRANCH %s", branchName)); Review Comment: Test: creating branch on a non-icebeg table will fail -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
