wypoon commented on code in PR #12461: URL: https://github.com/apache/iceberg/pull/12461#discussion_r2014950206
########## hive-metastore/src/main/java/org/apache/iceberg/hive/HMSTablePropertyHelper.java: ########## @@ -0,0 +1,264 @@ +/* + * 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.hive; + +import static org.apache.iceberg.TableProperties.GC_ENABLED; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.hadoop.hive.common.StatsSetupConst; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.PartitionSpecParser; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SchemaParser; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotSummary; +import org.apache.iceberg.SortOrderParser; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +import org.apache.iceberg.relocated.com.google.common.collect.BiMap; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableBiMap; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.util.JsonUtil; +import org.apache.iceberg.view.ViewMetadata; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HMSTablePropertyHelper { + private static final Logger LOG = LoggerFactory.getLogger(HMSTablePropertyHelper.class); + private static final String HIVE_ICEBERG_STORAGE_HANDLER = + "org.apache.iceberg.mr.hive.HiveIcebergStorageHandler"; + + private static final BiMap<String, String> ICEBERG_TO_HMS_TRANSLATION = + ImmutableBiMap.of( + // gc.enabled in Iceberg and external.table.purge in Hive are meant to do the same things + // but with different names + GC_ENABLED, "external.table.purge"); Review Comment: I think that it would be valuable to preserve a small part of the javadoc for `HiveTableOperations.translateToIcebergProp` which is removed in this PR, and add it as a comment before this static field: "Provides key translation where necessary between Iceberg and HMS props. This translation is needed because some properties control the same behaviour but are named differently in Iceberg and Hive." Then the inner comment about `gc.enabled` and `externavl.table.purge` is not needed, as it would be clear. ########## hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveCatalog.java: ########## @@ -1048,7 +1048,7 @@ public void testSetSnapshotSummary() throws Exception { @Test public void testNotExposeTableProperties() { Configuration conf = new Configuration(); - conf.set("iceberg.hive.table-property-max-size", "0"); + final long maxHiveTablePropertySize = 0; HiveTableOperations ops = new HiveTableOperations(conf, null, null, catalog.name(), DB_NAME, "tbl"); Review Comment: Same here. `conf` and `ops` are no longer needed. ########## hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveCatalog.java: ########## @@ -1060,19 +1060,19 @@ public void testNotExposeTableProperties() { parameters.put(DEFAULT_PARTITION_SPEC, "partitionSpec"); parameters.put(DEFAULT_SORT_ORDER, "sortOrder"); - ops.setSnapshotStats(metadata, parameters); + HMSTablePropertyHelper.setSnapshotStats(metadata, parameters, maxHiveTablePropertySize); assertThat(parameters) .doesNotContainKey(CURRENT_SNAPSHOT_SUMMARY) .doesNotContainKey(CURRENT_SNAPSHOT_ID) .doesNotContainKey(CURRENT_SNAPSHOT_TIMESTAMP); - ops.setSchema(metadata.schema(), parameters); + HMSTablePropertyHelper.setSchema(metadata.schema(), parameters, maxHiveTablePropertySize); assertThat(parameters).doesNotContainKey(CURRENT_SCHEMA); - ops.setPartitionSpec(metadata, parameters); + HMSTablePropertyHelper.setSortOrder(metadata, parameters, maxHiveTablePropertySize); Review Comment: This should be `HMSTablePropertyHelper.setPartitionSpec`. This is why the test fails. Please make sure the CI passes! ########## hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveCatalog.java: ########## @@ -1015,7 +1015,7 @@ public void testSnapshotStatsTableProperties() throws Exception { @Test public void testSetSnapshotSummary() throws Exception { Configuration conf = new Configuration(); - conf.set("iceberg.hive.table-property-max-size", "4000"); + final long maxHiveTablePropertySize = 4000; HiveTableOperations ops = new HiveTableOperations(conf, null, null, catalog.name(), DB_NAME, "tbl"); Review Comment: Both `conf` and `ops` are no longer needed. Instead of calling methods on `ops`, you now call static methods in `HMSTablePropertyHelper`. ########## hive-metastore/src/main/java/org/apache/iceberg/hive/HMSTablePropertyHelper.java: ########## @@ -0,0 +1,264 @@ +/* + * 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.hive; + +import static org.apache.iceberg.TableProperties.GC_ENABLED; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.hadoop.hive.common.StatsSetupConst; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.PartitionSpecParser; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SchemaParser; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotSummary; +import org.apache.iceberg.SortOrderParser; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +import org.apache.iceberg.relocated.com.google.common.collect.BiMap; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableBiMap; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.util.JsonUtil; +import org.apache.iceberg.view.ViewMetadata; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HMSTablePropertyHelper { + private static final Logger LOG = LoggerFactory.getLogger(HMSTablePropertyHelper.class); + private static final String HIVE_ICEBERG_STORAGE_HANDLER = + "org.apache.iceberg.mr.hive.HiveIcebergStorageHandler"; + + private static final BiMap<String, String> ICEBERG_TO_HMS_TRANSLATION = + ImmutableBiMap.of( + // gc.enabled in Iceberg and external.table.purge in Hive are meant to do the same things + // but with different names + GC_ENABLED, "external.table.purge"); + + private HMSTablePropertyHelper() {} + + public static void setHmsTableParameters( + String newMetadataLocation, + Table tbl, + TableMetadata metadata, + Set<String> obsoleteProps, + boolean hiveEngineEnabled, + long maxHiveTablePropertySize, + String currentLocation) { + Map<String, String> parameters = + Optional.ofNullable(tbl.getParameters()).orElseGet(Maps::newHashMap); + Map<String, String> summary = + Optional.ofNullable(metadata.currentSnapshot()) + .map(Snapshot::summary) + .orElseGet(ImmutableMap::of); + // push all Iceberg table properties into HMS + metadata.properties().entrySet().stream() + .filter(entry -> !entry.getKey().equalsIgnoreCase(HiveCatalog.HMS_TABLE_OWNER)) + .forEach( + entry -> { + String key = entry.getKey(); + // translate key names between Iceberg and HMS where needed + String hmsKey = ICEBERG_TO_HMS_TRANSLATION.getOrDefault(key, key); + parameters.put(hmsKey, entry.getValue()); + }); + setCommonParameters( + newMetadataLocation, + metadata.uuid(), + obsoleteProps, + currentLocation, + parameters, + BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE.toUpperCase(Locale.ENGLISH), + metadata.schema(), + maxHiveTablePropertySize); + setStorageHandler(parameters, hiveEngineEnabled); + + // Set the basic statistics + if (summary.get(SnapshotSummary.TOTAL_DATA_FILES_PROP) != null) { + parameters.put(StatsSetupConst.NUM_FILES, summary.get(SnapshotSummary.TOTAL_DATA_FILES_PROP)); + } + if (summary.get(SnapshotSummary.TOTAL_RECORDS_PROP) != null) { + parameters.put(StatsSetupConst.ROW_COUNT, summary.get(SnapshotSummary.TOTAL_RECORDS_PROP)); + } + if (summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP) != null) { + parameters.put(StatsSetupConst.TOTAL_SIZE, summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP)); + } + + setSnapshotStats(metadata, parameters, maxHiveTablePropertySize); + setSchema(metadata.schema(), parameters, maxHiveTablePropertySize); + setPartitionSpec(metadata, parameters, maxHiveTablePropertySize); + setSortOrder(metadata, parameters, maxHiveTablePropertySize); + + tbl.setParameters(parameters); + } + + public static void setHmsTableParameters( + String newMetadataLocation, + Table tbl, + ViewMetadata metadata, + Set<String> obsoleteProps, + long maxHiveTablePropertySize, + String currentLocation) { + Map<String, String> parameters = + Optional.ofNullable(tbl.getParameters()).orElseGet(Maps::newHashMap); + + // push all Iceberg view properties into HMS + metadata.properties().entrySet().stream() + .filter(entry -> !entry.getKey().equalsIgnoreCase(HiveCatalog.HMS_TABLE_OWNER)) + .forEach(entry -> parameters.put(entry.getKey(), entry.getValue())); + setCommonParameters( + newMetadataLocation, + metadata.uuid(), + obsoleteProps, + currentLocation, + parameters, + HiveOperationsBase.ICEBERG_VIEW_TYPE_VALUE.toUpperCase(Locale.ENGLISH), + metadata.schema(), + maxHiveTablePropertySize); + tbl.setParameters(parameters); + } + + private static void setCommonParameters( + String newMetadataLocation, + String uuid, + Set<String> obsoleteProps, + String currentLocation, + Map<String, String> parameters, + String tableType, + Schema schema, + long maxHiveTablePropertySize) { + if (uuid != null) { + parameters.put(TableProperties.UUID, uuid); + } + + // remove any props from HMS that are no longer present in Iceberg view props Review Comment: "view" -> "table/view" ########## hive-metastore/src/main/java/org/apache/iceberg/hive/HMSTablePropertyHelper.java: ########## @@ -0,0 +1,264 @@ +/* + * 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.hive; + +import static org.apache.iceberg.TableProperties.GC_ENABLED; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.hadoop.hive.common.StatsSetupConst; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; +import org.apache.iceberg.BaseMetastoreTableOperations; +import org.apache.iceberg.PartitionSpecParser; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SchemaParser; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotSummary; +import org.apache.iceberg.SortOrderParser; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +import org.apache.iceberg.relocated.com.google.common.collect.BiMap; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableBiMap; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.util.JsonUtil; +import org.apache.iceberg.view.ViewMetadata; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HMSTablePropertyHelper { + private static final Logger LOG = LoggerFactory.getLogger(HMSTablePropertyHelper.class); + private static final String HIVE_ICEBERG_STORAGE_HANDLER = + "org.apache.iceberg.mr.hive.HiveIcebergStorageHandler"; + + private static final BiMap<String, String> ICEBERG_TO_HMS_TRANSLATION = + ImmutableBiMap.of( + // gc.enabled in Iceberg and external.table.purge in Hive are meant to do the same things + // but with different names + GC_ENABLED, "external.table.purge"); + + private HMSTablePropertyHelper() {} + + public static void setHmsTableParameters( + String newMetadataLocation, + Table tbl, + TableMetadata metadata, + Set<String> obsoleteProps, + boolean hiveEngineEnabled, + long maxHiveTablePropertySize, + String currentLocation) { + Map<String, String> parameters = + Optional.ofNullable(tbl.getParameters()).orElseGet(Maps::newHashMap); + Map<String, String> summary = + Optional.ofNullable(metadata.currentSnapshot()) + .map(Snapshot::summary) + .orElseGet(ImmutableMap::of); + // push all Iceberg table properties into HMS + metadata.properties().entrySet().stream() + .filter(entry -> !entry.getKey().equalsIgnoreCase(HiveCatalog.HMS_TABLE_OWNER)) + .forEach( + entry -> { + String key = entry.getKey(); + // translate key names between Iceberg and HMS where needed + String hmsKey = ICEBERG_TO_HMS_TRANSLATION.getOrDefault(key, key); + parameters.put(hmsKey, entry.getValue()); + }); + setCommonParameters( + newMetadataLocation, + metadata.uuid(), + obsoleteProps, + currentLocation, + parameters, + BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE.toUpperCase(Locale.ENGLISH), + metadata.schema(), + maxHiveTablePropertySize); + setStorageHandler(parameters, hiveEngineEnabled); + + // Set the basic statistics + if (summary.get(SnapshotSummary.TOTAL_DATA_FILES_PROP) != null) { + parameters.put(StatsSetupConst.NUM_FILES, summary.get(SnapshotSummary.TOTAL_DATA_FILES_PROP)); + } + if (summary.get(SnapshotSummary.TOTAL_RECORDS_PROP) != null) { + parameters.put(StatsSetupConst.ROW_COUNT, summary.get(SnapshotSummary.TOTAL_RECORDS_PROP)); + } + if (summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP) != null) { + parameters.put(StatsSetupConst.TOTAL_SIZE, summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP)); + } + + setSnapshotStats(metadata, parameters, maxHiveTablePropertySize); + setPartitionSpec(metadata, parameters, maxHiveTablePropertySize); + setSortOrder(metadata, parameters, maxHiveTablePropertySize); + + tbl.setParameters(parameters); + } + + public static void setHmsTableParameters( Review Comment: I agree with @gaborkaszab that it would be helpful to differentiate the two methods. I'd suggest `setHmsTableParametersForTable` and `setHmsTableParametersForView`. It would also be helpful to add javadoc for both methods for documentation. -- 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: issues-unsubscr...@iceberg.apache.org 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