fuwhu commented on a change in pull request #26805: [SPARK-15616][SQL] Add optimizer rule PruneHiveTablePartitions URL: https://github.com/apache/spark/pull/26805#discussion_r366133849
########## File path: sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/PruneHiveTablePartitions.scala ########## @@ -0,0 +1,140 @@ +/* + * 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.spark.sql.hive.execution + +import java.io.IOException + +import scala.collection.JavaConverters._ + +import org.apache.hadoop.hive.common.StatsSetupConst + +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.analysis.CastSupport +import org.apache.spark.sql.catalyst.catalog.{CatalogStatistics, CatalogTable, CatalogTablePartition, HiveTableRelation} +import org.apache.spark.sql.catalyst.expressions.{And, AttributeReference, AttributeSet, BindReferences, Expression, Literal, SubqueryExpression} +import org.apache.spark.sql.catalyst.planning.PhysicalOperation +import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.execution.command.CommandUtils +import org.apache.spark.sql.hive.client.HiveClientImpl +import org.apache.spark.sql.types.BooleanType + +/** + * TODO: merge this with PruneFileSourcePartitions after we completely make hive as a data source. + */ +private[sql] class PruneHiveTablePartitions(session: SparkSession) + extends Rule[LogicalPlan] with CastSupport { + + override val conf = session.sessionState.conf + + /** + * Extract the partition filters from the filters on the table. + */ + private def extractPartitionPruningFilters( + filters: Seq[Expression], + relation: HiveTableRelation): Seq[Expression] = { + val normalizedFilters = filters.map { e => + e transform { + case a: AttributeReference => + a.withName(relation.output.find(_.semanticEquals(a)).get.name) + } + } + val partitionSet = AttributeSet(relation.partitionCols) + normalizedFilters.filter { predicate => + !predicate.references.isEmpty && predicate.references.subsetOf(partitionSet) + } + } + + /** + * Prune the hive table using filters on the partitions of the table. + */ + private def prunePartitions(relation: HiveTableRelation, partitionFilters: Seq[Expression]) + : Seq[CatalogTablePartition] = { + val partitions = + if (conf.metastorePartitionPruning) { + session.sessionState.catalog.listPartitionsByFilter( + relation.tableMeta.identifier, partitionFilters) + } else { + session.sessionState.catalog.listPartitions(relation.tableMeta.identifier) + } + val shouldKeep = partitionFilters.reduceLeftOption(And).map { filter => + require(filter.dataType == BooleanType, + s"Data type of predicate $filter must be ${BooleanType.catalogString} rather than " + + s"${filter.dataType.catalogString}.") + BindReferences.bindReference(filter, relation.partitionCols) + } + if (shouldKeep.nonEmpty) { + partitions.filter{ partition => + val hivePartition = + HiveClientImpl.toHivePartition(partition, HiveClientImpl.toHiveTable(relation.tableMeta)) + val dataTypes = relation.partitionCols.map(_.dataType) + val castedValues = hivePartition.getValues.asScala.zip(dataTypes) + .map { case (value, dataType) => cast(Literal(value), dataType).eval(null) } + val row = InternalRow.fromSeq(castedValues) + shouldKeep.get.eval(row).asInstanceOf[Boolean] + } + } else { + partitions + } + } + + /** + * Update the statistics of the table. + */ + private def updateTableMeta( + tableMeta: CatalogTable, + prunedPartitions: Seq[CatalogTablePartition]): CatalogTable = { + val sizeInBytes = try { + prunedPartitions.map { partition => + val rawDataSize = partition.parameters.get(StatsSetupConst.RAW_DATA_SIZE).map(_.toLong) + val totalSize = partition.parameters.get(StatsSetupConst.TOTAL_SIZE).map(_.toLong) + if (rawDataSize.isDefined && rawDataSize.get > 0) { + rawDataSize.get + } else if (totalSize.isDefined && totalSize.get > 0L) { + totalSize.get + } else { + CommandUtils.calculateLocationSize( Review comment: In PruneFileSourcePartitions, it calls "prunedFileIndex.sizeInBytes" which get size from file system for each leaf file (include files of all partitions). I think it's ok to get size from file system for each partition, but we need to limit the number of partitions, just as proposed in #27129 . Here, I changed it to drop the size stats if it can't be got from metadata, will update it after #27129 finished. Does it make sense to you? ---------------------------------------------------------------- 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 --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org