bowenli86 commented on a change in pull request #8703: [FLINK-12807][hive]Support Hive table columnstats related operations in HiveCatalog URL: https://github.com/apache/flink/pull/8703#discussion_r293551568
########## File path: flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/util/HiveCatalogUtil.java ########## @@ -0,0 +1,235 @@ +/* + * 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.flink.table.catalog.hive.util; + +import org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataBase; +import org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataBinary; +import org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataBoolean; +import org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataDate; +import org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataDouble; +import org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataLong; +import org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataString; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.logical.BigIntType; +import org.apache.flink.table.types.logical.BooleanType; +import org.apache.flink.table.types.logical.CharType; +import org.apache.flink.table.types.logical.DateType; +import org.apache.flink.table.types.logical.DoubleType; +import org.apache.flink.table.types.logical.FloatType; +import org.apache.flink.table.types.logical.IntType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.SmallIntType; +import org.apache.flink.table.types.logical.TimestampType; +import org.apache.flink.table.types.logical.TinyIntType; +import org.apache.flink.table.types.logical.VarCharType; + +import org.apache.hadoop.hive.metastore.api.BinaryColumnStatsData; +import org.apache.hadoop.hive.metastore.api.BooleanColumnStatsData; +import org.apache.hadoop.hive.metastore.api.ColumnStatistics; +import org.apache.hadoop.hive.metastore.api.ColumnStatisticsData; +import org.apache.hadoop.hive.metastore.api.ColumnStatisticsDesc; +import org.apache.hadoop.hive.metastore.api.ColumnStatisticsObj; +import org.apache.hadoop.hive.metastore.api.DateColumnStatsData; +import org.apache.hadoop.hive.metastore.api.DoubleColumnStatsData; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hadoop.hive.metastore.api.LongColumnStatsData; +import org.apache.hadoop.hive.metastore.api.Partition; +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; +import org.apache.hadoop.hive.metastore.api.StringColumnStatsData; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Utils to for HiveCatalog. + */ +public class HiveCatalogUtil { + private static final Logger LOG = LoggerFactory.getLogger(HiveCatalogUtil.class); + + private HiveCatalogUtil() {} + + /** + * Create a map of Flink column stats from the given Hive column stats. + */ + public static Map<String, CatalogColumnStatisticsDataBase> createCatalogColumnStats(List<ColumnStatisticsObj> hiveColStats) { + Map<String, CatalogColumnStatisticsDataBase> colStats = new HashMap<>(); + + if (hiveColStats != null) { + for (ColumnStatisticsObj colStatsObj : hiveColStats) { + CatalogColumnStatisticsDataBase columnStats = + createTableColumnStats( + HiveTypeUtil.toFlinkType(TypeInfoUtils.getTypeInfoFromTypeString(colStatsObj.getColType())), + colStatsObj.getStatsData()); + + if (colStats != null) { + colStats.put(colStatsObj.getColName(), columnStats); + } + } + } + + return colStats; + } + + /** + * Create columnStatistics from the given Hive column stats of a hive table. + */ + public static ColumnStatistics createTableColumnStats( + Table hiveTable, + Map<String, CatalogColumnStatisticsDataBase> colStats) { + ColumnStatisticsDesc desc = new ColumnStatisticsDesc(true, hiveTable.getDbName(), hiveTable.getTableName()); + return createColumnStatistics(colStats, hiveTable.getSd(), desc); + } + + /** + * Create columnStatistics from the given Hive column stats of a hive partition. + */ + public static ColumnStatistics createPartitionColumnStats( + Partition hivePartition, + String partName, + Map<String, CatalogColumnStatisticsDataBase> colStats) { + ColumnStatisticsDesc desc = new ColumnStatisticsDesc(false, hivePartition.getDbName(), hivePartition.getTableName()); + desc.setPartName(partName); + return createColumnStatistics(colStats, hivePartition.getSd(), desc); + } + + private static ColumnStatistics createColumnStatistics( + Map<String, CatalogColumnStatisticsDataBase> colStats, + StorageDescriptor sd, + ColumnStatisticsDesc desc) { + List<ColumnStatisticsObj> colStatsList = new ArrayList<>(); + + for (FieldSchema field : sd.getCols()) { + String hiveColName = field.getName(); + String hiveColType = field.getType(); + + if (colStats.containsKey(hiveColName)) { Review comment: Flink's coding style recommends not using the access pattern: ``` if (map.contains(k)) { map.get(k) } ``` instead, we should use access pattern: ``` v = map.get(k) if (v != null) { ... } ``` ---------------------------------------------------------------- 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 With regards, Apache Git Services