rdblue commented on a change in pull request #1495: URL: https://github.com/apache/iceberg/pull/1495#discussion_r494568776
########## File path: mr/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java ########## @@ -0,0 +1,161 @@ +/* + * 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.HashSet; +import java.util.Properties; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.metastore.HiveMetaHook; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.PartitionSpecParser; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SchemaParser; +import org.apache.iceberg.Table; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.hive.HiveTableOperations; +import org.apache.iceberg.mr.Catalogs; +import org.apache.iceberg.mr.InputFormatConfig; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HiveIcebergMetaHook implements HiveMetaHook { + private static final Logger LOG = LoggerFactory.getLogger(HiveIcebergMetaHook.class); + private static final Set<String> PARAMETERS_TO_REMOVE = Stream + .of(InputFormatConfig.TABLE_SCHEMA, InputFormatConfig.PARTITION_SPEC, Catalogs.LOCATION, Catalogs.NAME) + .collect(Collectors.toCollection(HashSet::new)); + private static final Set<String> PROPERTIES_TO_REMOVE = Stream + .of(InputFormatConfig.HIVE_DELETE_BACKING_TABLE, "storage_handler", "EXTERNAL") + .collect(Collectors.toCollection(HashSet::new)); + + private final Configuration conf; + private Table icebergTable = null; + private Properties catalogProperties; + private boolean deleteIcebergTable; + + public HiveIcebergMetaHook(Configuration conf) { + this.conf = conf; + } + + @Override + public void preCreateTable(org.apache.hadoop.hive.metastore.api.Table hmsTable) { + catalogProperties = getCatalogProperties(hmsTable); + try { + icebergTable = Catalogs.loadTable(conf, catalogProperties); + + Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA) == null, + "Iceberg table already created - can not use provided schema"); + Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC) == null, + "Iceberg table already created - can not use provided partition specification"); + + LOG.info("Iceberg table already exists {}", icebergTable); + } catch (NoSuchTableException nte) { + String schemaString = catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA); + Preconditions.checkNotNull(schemaString, "Please provide a table schema"); + // Just check if it is parsable, and later use for partition specification parsing + Schema schema = SchemaParser.fromJson(schemaString); + + String specString = catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC); + if (specString != null) { + // Just check if it is parsable + PartitionSpecParser.fromJson(schema, schemaString); + } + + // Allow purging table data if the table is created now and not set otherwise + if (hmsTable.getParameters().get(InputFormatConfig.HIVE_DELETE_BACKING_TABLE) == null) { + hmsTable.getParameters().put(InputFormatConfig.HIVE_DELETE_BACKING_TABLE, "TRUE"); Review comment: Would it work to drop the table if it is a HMS table and not if it uses a non-Hive catalog? ########## File path: mr/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java ########## @@ -0,0 +1,161 @@ +/* + * 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.HashSet; +import java.util.Properties; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.metastore.HiveMetaHook; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.PartitionSpecParser; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SchemaParser; +import org.apache.iceberg.Table; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.hive.HiveTableOperations; +import org.apache.iceberg.mr.Catalogs; +import org.apache.iceberg.mr.InputFormatConfig; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HiveIcebergMetaHook implements HiveMetaHook { + private static final Logger LOG = LoggerFactory.getLogger(HiveIcebergMetaHook.class); + private static final Set<String> PARAMETERS_TO_REMOVE = Stream + .of(InputFormatConfig.TABLE_SCHEMA, InputFormatConfig.PARTITION_SPEC, Catalogs.LOCATION, Catalogs.NAME) + .collect(Collectors.toCollection(HashSet::new)); + private static final Set<String> PROPERTIES_TO_REMOVE = Stream + .of(InputFormatConfig.HIVE_DELETE_BACKING_TABLE, "storage_handler", "EXTERNAL") + .collect(Collectors.toCollection(HashSet::new)); + + private final Configuration conf; + private Table icebergTable = null; + private Properties catalogProperties; + private boolean deleteIcebergTable; + + public HiveIcebergMetaHook(Configuration conf) { + this.conf = conf; + } + + @Override + public void preCreateTable(org.apache.hadoop.hive.metastore.api.Table hmsTable) { + catalogProperties = getCatalogProperties(hmsTable); + try { + icebergTable = Catalogs.loadTable(conf, catalogProperties); + + Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA) == null, + "Iceberg table already created - can not use provided schema"); + Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC) == null, + "Iceberg table already created - can not use provided partition specification"); + + LOG.info("Iceberg table already exists {}", icebergTable); + } catch (NoSuchTableException nte) { + String schemaString = catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA); + Preconditions.checkNotNull(schemaString, "Please provide a table schema"); + // Just check if it is parsable, and later use for partition specification parsing + Schema schema = SchemaParser.fromJson(schemaString); + + String specString = catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC); + if (specString != null) { + // Just check if it is parsable + PartitionSpecParser.fromJson(schema, schemaString); + } + + // Allow purging table data if the table is created now and not set otherwise + if (hmsTable.getParameters().get(InputFormatConfig.HIVE_DELETE_BACKING_TABLE) == null) { + hmsTable.getParameters().put(InputFormatConfig.HIVE_DELETE_BACKING_TABLE, "TRUE"); Review comment: Or alternatively, could we use the existing `EXTERNAL` option? I think that does basically the same thing for data in paths that Hive points to. ########## File path: hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java ########## @@ -139,6 +140,8 @@ protected void doRefresh() { @Override protected void doCommit(TableMetadata base, TableMetadata metadata) { + boolean updateTable = base != null || metadata.propertyAsBoolean(TABLE_FROM_HIVE, false); Review comment: What is the purpose of this? Is it because the table already exists in the Hive MetaStore but there is no Iceberg metadata? ########## File path: hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java ########## @@ -139,6 +140,8 @@ protected void doRefresh() { @Override protected void doCommit(TableMetadata base, TableMetadata metadata) { + boolean updateTable = base != null || metadata.propertyAsBoolean(TABLE_FROM_HIVE, false); Review comment: Okay, thanks for the context. I think we may be able to catch this case within `HiveTableOperations`. When creating a table, we will create a new `TableOperations` and that will attempt to load the current metadata. Normally, [that will fail](https://github.com/apache/iceberg/blob/master/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveTableOperations.java#L125) and because the `currentMetadataLocation` was `null`, the failure is caught and metadata is set to null because the table doesn't exist. If the table exists but doesn't have a metadata location, then we will get a table from Hive, but the location property won't exist. We can detect that the location property doesn't exist and set a flag, `updateToCreate`. That avoids the need for a table property because we detect the state of the Hive MetaStore, and it doesn't incur extra thrift calls. ########## File path: mr/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java ########## @@ -0,0 +1,161 @@ +/* + * 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.HashSet; +import java.util.Properties; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.metastore.HiveMetaHook; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.PartitionSpecParser; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SchemaParser; +import org.apache.iceberg.Table; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.hive.HiveTableOperations; +import org.apache.iceberg.mr.Catalogs; +import org.apache.iceberg.mr.InputFormatConfig; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HiveIcebergMetaHook implements HiveMetaHook { + private static final Logger LOG = LoggerFactory.getLogger(HiveIcebergMetaHook.class); + private static final Set<String> PARAMETERS_TO_REMOVE = Stream + .of(InputFormatConfig.TABLE_SCHEMA, InputFormatConfig.PARTITION_SPEC, Catalogs.LOCATION, Catalogs.NAME) + .collect(Collectors.toCollection(HashSet::new)); + private static final Set<String> PROPERTIES_TO_REMOVE = Stream + .of(InputFormatConfig.HIVE_DELETE_BACKING_TABLE, "storage_handler", "EXTERNAL") + .collect(Collectors.toCollection(HashSet::new)); + + private final Configuration conf; + private Table icebergTable = null; + private Properties catalogProperties; + private boolean deleteIcebergTable; + + public HiveIcebergMetaHook(Configuration conf) { + this.conf = conf; + } + + @Override + public void preCreateTable(org.apache.hadoop.hive.metastore.api.Table hmsTable) { + catalogProperties = getCatalogProperties(hmsTable); + try { + icebergTable = Catalogs.loadTable(conf, catalogProperties); + + Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA) == null, + "Iceberg table already created - can not use provided schema"); + Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC) == null, + "Iceberg table already created - can not use provided partition specification"); + + LOG.info("Iceberg table already exists {}", icebergTable); + } catch (NoSuchTableException nte) { + String schemaString = catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA); + Preconditions.checkNotNull(schemaString, "Please provide a table schema"); + // Just check if it is parsable, and later use for partition specification parsing + Schema schema = SchemaParser.fromJson(schemaString); + + String specString = catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC); + if (specString != null) { + // Just check if it is parsable + PartitionSpecParser.fromJson(schema, schemaString); + } + + // Allow purging table data if the table is created now and not set otherwise + if (hmsTable.getParameters().get(InputFormatConfig.HIVE_DELETE_BACKING_TABLE) == null) { + hmsTable.getParameters().put(InputFormatConfig.HIVE_DELETE_BACKING_TABLE, "TRUE"); Review comment: I don't quite understand the new definition of `MANAGED` that you're talking about, but I think we should try to make it work. We could still respect `external.table.purge`. I'll try to find some docs on the new definition. ########## File path: mr/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java ########## @@ -0,0 +1,161 @@ +/* + * 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.HashSet; +import java.util.Properties; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.metastore.HiveMetaHook; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.PartitionSpecParser; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SchemaParser; +import org.apache.iceberg.Table; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.hive.HiveTableOperations; +import org.apache.iceberg.mr.Catalogs; +import org.apache.iceberg.mr.InputFormatConfig; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HiveIcebergMetaHook implements HiveMetaHook { + private static final Logger LOG = LoggerFactory.getLogger(HiveIcebergMetaHook.class); + private static final Set<String> PARAMETERS_TO_REMOVE = Stream + .of(InputFormatConfig.TABLE_SCHEMA, InputFormatConfig.PARTITION_SPEC, Catalogs.LOCATION, Catalogs.NAME) + .collect(Collectors.toCollection(HashSet::new)); + private static final Set<String> PROPERTIES_TO_REMOVE = Stream + .of(InputFormatConfig.HIVE_DELETE_BACKING_TABLE, "storage_handler", "EXTERNAL") + .collect(Collectors.toCollection(HashSet::new)); + + private final Configuration conf; + private Table icebergTable = null; + private Properties catalogProperties; + private boolean deleteIcebergTable; + + public HiveIcebergMetaHook(Configuration conf) { + this.conf = conf; + } + + @Override + public void preCreateTable(org.apache.hadoop.hive.metastore.api.Table hmsTable) { + catalogProperties = getCatalogProperties(hmsTable); + try { + icebergTable = Catalogs.loadTable(conf, catalogProperties); + + Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA) == null, + "Iceberg table already created - can not use provided schema"); + Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC) == null, + "Iceberg table already created - can not use provided partition specification"); + + LOG.info("Iceberg table already exists {}", icebergTable); + } catch (NoSuchTableException nte) { + String schemaString = catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA); + Preconditions.checkNotNull(schemaString, "Please provide a table schema"); + // Just check if it is parsable, and later use for partition specification parsing + Schema schema = SchemaParser.fromJson(schemaString); + + String specString = catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC); + if (specString != null) { + // Just check if it is parsable + PartitionSpecParser.fromJson(schema, schemaString); + } + + // Allow purging table data if the table is created now and not set otherwise + if (hmsTable.getParameters().get(InputFormatConfig.HIVE_DELETE_BACKING_TABLE) == null) { + hmsTable.getParameters().put(InputFormatConfig.HIVE_DELETE_BACKING_TABLE, "TRUE"); Review comment: Also: if we know that Hive is creating the table, we should definitely set the correct storage handler and serde. Tables that are created by Spark might be used by Hive (and should depend on hive-site.xml) but we can assume that tables created by Hive have the Iceberg library present and can be set up automatically, I think. ---------------------------------------------------------------- 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 --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org