[ https://issues.apache.org/jira/browse/HIVE-25034?focusedWorklogId=599064&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-599064 ]
ASF GitHub Bot logged work on HIVE-25034: ----------------------------------------- Author: ASF GitHub Bot Created on: 19/May/21 08:20 Start Date: 19/May/21 08:20 Worklog Time Spent: 10m Work Description: lcspinter commented on a change in pull request #2243: URL: https://github.com/apache/hive/pull/2243#discussion_r635016099 ########## File path: iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStorageHandlerWithEngine.java ########## @@ -514,6 +519,75 @@ public void testInsertOverwritePartitionedTable() throws IOException { HiveIcebergTestUtils.validateData(table, expected, 0); } + @Test + public void testCTASFromHiveTable() { + Assume.assumeTrue("CTAS target table is supported only for HiveCatalog tables", + testTableType == TestTables.TestTableType.HIVE_CATALOG); + + shell.executeStatement("CREATE TABLE source (id bigint, name string) PARTITIONED BY (dept string) STORED AS ORC"); + shell.executeStatement("INSERT INTO source VALUES (1, 'Mike', 'HR'), (2, 'Linda', 'Finance')"); + + shell.executeStatement(String.format( + "CREATE TABLE target STORED BY '%s' %s TBLPROPERTIES ('%s'='%s') AS SELECT * FROM source", + HiveIcebergStorageHandler.class.getName(), + testTables.locationForCreateTableSQL(TableIdentifier.of("default", "target")), + TableProperties.DEFAULT_FILE_FORMAT, fileFormat)); + + List<Object[]> objects = shell.executeStatement("SELECT * FROM target ORDER BY id"); + Assert.assertEquals(2, objects.size()); + Assert.assertArrayEquals(new Object[]{1L, "Mike", "HR"}, objects.get(0)); + Assert.assertArrayEquals(new Object[]{2L, "Linda", "Finance"}, objects.get(1)); + } + + @Test + public void testCTASFromDifferentIcebergCatalog() { + Assume.assumeTrue("CTAS target table is supported only for HiveCatalog tables", + testTableType == TestTables.TestTableType.HIVE_CATALOG); + + // get source data from a different catalog + shell.executeStatement(String.format( + "CREATE TABLE source STORED BY '%s' LOCATION '%s' TBLPROPERTIES ('%s'='%s', '%s'='%s')", + HiveIcebergStorageHandler.class.getName(), + temp.getRoot().getPath() + "/default/source/", + InputFormatConfig.CATALOG_NAME, Catalogs.ICEBERG_HADOOP_TABLE_NAME, + InputFormatConfig.TABLE_SCHEMA, SchemaParser.toJson(HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA))); + shell.executeStatement("INSERT INTO source VALUES (1, 'Mike', 'Roger'), (2, 'Linda', 'Albright')"); + + // CTAS into a new HiveCatalog table + shell.executeStatement(String.format( + "CREATE TABLE target STORED BY '%s' TBLPROPERTIES ('%s'='%s') AS SELECT * FROM source", + HiveIcebergStorageHandler.class.getName(), + TableProperties.DEFAULT_FILE_FORMAT, fileFormat)); + + List<Object[]> objects = shell.executeStatement("SELECT * FROM target ORDER BY customer_id"); + Assert.assertEquals(2, objects.size()); + Assert.assertArrayEquals(new Object[]{1L, "Mike", "Roger"}, objects.get(0)); + Assert.assertArrayEquals(new Object[]{2L, "Linda", "Albright"}, objects.get(1)); + } + + @Test + public void testCTASFailureRollback() throws IOException { Review comment: Could you please add a test method that checks the rollback in case the source and dest tables are in different catalogs? ########## File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergCTASHook.java ########## @@ -0,0 +1,74 @@ +/* + * 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.util.Properties; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.ql.hooks.QueryLifeTimeHook; +import org.apache.hadoop.hive.ql.hooks.QueryLifeTimeHookContext; +import org.apache.iceberg.mr.Catalogs; +import org.apache.iceberg.mr.InputFormatConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HiveIcebergCTASHook implements QueryLifeTimeHook { + + private static final Logger LOG = LoggerFactory.getLogger(HiveIcebergCTASHook.class); + + @Override + public void beforeCompile(QueryLifeTimeHookContext ctx) { + + } + + @Override + public void afterCompile(QueryLifeTimeHookContext ctx, boolean hasError) { + if (hasError) { + checkAndRollbackIcebergCTAS(ctx); + } + } + + @Override + public void beforeExecution(QueryLifeTimeHookContext ctx) { + + } + + @Override + public void afterExecution(QueryLifeTimeHookContext ctx, boolean hasError) { + if (hasError) { + checkAndRollbackIcebergCTAS(ctx); + } + } + + private void checkAndRollbackIcebergCTAS(QueryLifeTimeHookContext ctx) { + HiveConf conf = ctx.getHiveConf(); + String queryId = conf.getVar(HiveConf.ConfVars.HIVEQUERYID); + if (conf.getBoolean(String.format(InputFormatConfig.IS_CTAS_QUERY_TEMPLATE, queryId), false)) { + try { + String tableName = conf.get(String.format(InputFormatConfig.CTAS_TABLE_NAME_TEMPLATE, queryId)); + LOG.info("Dropping the following CTAS target table as part of rollback: {}", tableName); + Properties props = new Properties(); + props.put(Catalogs.NAME, tableName); + Catalogs.dropTable(conf, props); Review comment: Shouldn't we set the catalog info here as well? Without the catalog name, the dropTable will try to remove the table from the default catalog. -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 599064) Time Spent: 3h 40m (was: 3.5h) > Implement CTAS for Iceberg > -------------------------- > > Key: HIVE-25034 > URL: https://issues.apache.org/jira/browse/HIVE-25034 > Project: Hive > Issue Type: New Feature > Reporter: Marton Bod > Assignee: Marton Bod > Priority: Major > Labels: pull-request-available > Time Spent: 3h 40m > Remaining Estimate: 0h > -- This message was sent by Atlassian Jira (v8.3.4#803005)