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_r364587799
 
 

 ##########
 File path: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/PruneHiveTablePartitions.scala
 ##########
 @@ -0,0 +1,112 @@
+/*
+ * 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 org.apache.hadoop.hive.common.StatsSetupConst
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.catalog.{CatalogStatistics, 
HiveTableRelation}
+import org.apache.spark.sql.catalyst.expressions.{And, AttributeReference, 
AttributeSet, Expression, PredicateHelper, 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
+
+/**
+ * TODO: merge this with PruneFileSourcePartitions after we completely make 
hive as a data source.
+ */
+private[sql] class PruneHiveTablePartitions(session: SparkSession)
+  extends Rule[LogicalPlan] with PredicateHelper {
+  /**
+   * 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,
+   * and also update the statistics of the table.
+   */
+  private def prunedHiveTableWithStats(
+              relation: HiveTableRelation,
+              partitionFilters: Seq[Expression]): HiveTableRelation = {
+    val conf = session.sessionState.conf
+    val prunedPartitions = session.sessionState.catalog.listPartitionsByFilter(
+      relation.tableMeta.identifier,
+      partitionFilters)
+    val sizeInBytes = try {
+      val sizeOfPartitions = 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(
+            session.sessionState, relation.tableMeta.identifier, 
partition.storage.locationUri)
+        }
+      }.sum
+      // If size of partitions is zero fall back to the default size.
+      if (sizeOfPartitions == 0L) conf.defaultSizeInBytes else sizeOfPartitions
+    } catch {
+      case e: IOException =>
+        logWarning("Failed to get table size from HDFS.", e)
+        conf.defaultSizeInBytes
+    }
+    val newTableMeta =
+      if (relation.tableMeta.stats.isDefined) {
+        relation.tableMeta.copy(
+          stats = Some(relation.tableMeta.stats.get.copy(sizeInBytes = 
BigInt(sizeInBytes))))
 
 Review comment:
   could be inconsistent, eg. rowCount and sizeInBytes may be inconsistent 
after this rule.
   Restored to creating new CatalogStatistics instance. But by doing so, some 
statistics may be lost which should not impact accuracy.

----------------------------------------------------------------
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

Reply via email to