Github user mallman commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14690#discussion_r83086945
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/PruneFileSourcePartitions.scala
 ---
    @@ -0,0 +1,72 @@
    +/*
    + * 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.execution.datasources
    +
    +import org.apache.spark.sql.catalyst.expressions._
    +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
    +
    +private[sql] object PruneFileSourcePartitions extends Rule[LogicalPlan] {
    +  override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
    +    case op @ PhysicalOperation(projects, filters,
    +        logicalRelation @
    +          LogicalRelation(fsRelation @
    +            HadoopFsRelation(
    +              tableFileCatalog: TableFileCatalog,
    +              partitionSchema,
    +              _,
    +              _,
    +              _,
    +              _),
    +            _,
    +            _))
    +        if filters.nonEmpty && fsRelation.partitionSchemaOption.isDefined 
=>
    +      // The attribute name of predicate could be different than the one 
in schema in case of
    +      // case insensitive, we should change them to match the one in 
schema, so we donot need to
    +      // worry about case sensitivity anymore.
    +      val normalizedFilters = filters.map { e =>
    +        e transform {
    +          case a: AttributeReference =>
    +            
a.withName(logicalRelation.output.find(_.semanticEquals(a)).get.name)
    +        }
    +      }
    +
    +      val sparkSession = fsRelation.sparkSession
    +      val partitionColumns =
    +        logicalRelation.resolve(
    +          partitionSchema, sparkSession.sessionState.analyzer.resolver)
    +      val partitionSet = AttributeSet(partitionColumns)
    +      val partitionKeyFilters =
    +        
ExpressionSet(normalizedFilters.filter(_.references.subsetOf(partitionSet)))
    +
    +      if (partitionKeyFilters.nonEmpty) {
    +          val prunedFileCatalog = 
tableFileCatalog.filterPartitions(partitionKeyFilters.toSeq)
    +          val prunedFsRelation =
    +            fsRelation.copy(location = prunedFileCatalog)(sparkSession)
    +          val prunedLogicalRelation = logicalRelation.copy(relation = 
prunedFsRelation)
    +
    +          // Keep partition-pruning predicates so that they are visible in 
physical planning
    +          val filterExpression = filters.reduceLeft(And)
    --- End diff --
    
    Also, I'll try some A/B testing to examine the performance impact of 
removing these partition pruning filters here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to