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

    https://github.com/apache/spark/pull/16578#discussion_r98845999
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetSchemaPruning.scala
 ---
    @@ -0,0 +1,139 @@
    +/*
    + * 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.parquet
    +
    +import org.apache.spark.sql.catalyst.expressions.{And, Attribute, 
Expression, NamedExpression}
    +import org.apache.spark.sql.catalyst.planning.{PhysicalOperation, 
ProjectionOverSchema, SelectedField}
    +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.datasources.{HadoopFsRelation, 
LogicalRelation}
    +import org.apache.spark.sql.types.{StructField, StructType}
    +
    +/**
    + * Prunes unnecessary Parquet columns given a [[PhysicalOperation]] over a
    + * [[ParquetRelation]]. By "Parquet column", we mean a column as defined 
in the
    + * Parquet format. In Spark SQL, a root-level Parquet column corresponds 
to a
    + * SQL column, and a nested Parquet column corresponds to a 
[[StructField]].
    + */
    +private[sql] object ParquetSchemaPruning extends Rule[LogicalPlan] {
    +  override def apply(plan: LogicalPlan): LogicalPlan =
    +    plan transformDown {
    +      case op @ PhysicalOperation(projects, filters,
    +          l @ LogicalRelation(hadoopFsRelation @ HadoopFsRelation(_, 
partitionSchema,
    +            dataSchema, _, parquetFormat: ParquetFileFormat, _), _, _)) =>
    +        val projectionFields = projects.flatMap(getFields)
    +        val filterFields = filters.flatMap(getFields)
    +        val requestedFields = (projectionFields ++ filterFields).distinct
    +
    +        // If [[requestedFields]] includes a proper field, continue. 
Otherwise,
    +        // return [[op]]
    +        if (requestedFields.exists { case (_, optAtt) => optAtt.isEmpty }) 
{
    +          val prunedSchema = requestedFields
    +            .map { case (field, _) => field }
    +            .map(field => StructType(Array(field)))
    +            .reduceLeft(_ merge _)
    +          val parquetDataColumnNames = dataSchema.fieldNames
    +          val prunedDataSchema =
    +            StructType(prunedSchema.filter(f => 
parquetDataColumnNames.contains(f.name)))
    +          val parquetDataFields = dataSchema.fields.toSet
    +          val prunedDataFields = prunedDataSchema.fields.toSet
    +
    +          // If the original Parquet relation data fields are different 
from the
    +          // pruned data fields, continue. Otherwise, return [[op]]
    +          if (parquetDataFields != prunedDataFields) {
    +            val dataSchemaFieldNames = 
hadoopFsRelation.dataSchema.fieldNames
    +            val newDataSchema =
    +              StructType(prunedSchema.filter(f => 
dataSchemaFieldNames.contains(f.name)))
    +            val prunedParquetRelation =
    +              hadoopFsRelation.copy(dataSchema = 
newDataSchema)(hadoopFsRelation.sparkSession)
    +            val outputMap = l.output.map(att => (att.name, att)).toMap
    +
    +            // We need to map the output of the original logical relation
    +            // to the attributes of the pruned parquet schema where
    +            // possible so that references to those attributes elsewhere in
    +            // the query plan are not broken
    +            val expectedOutputAttributes =
    +              prunedParquetRelation
    +                .schema
    +                .toAttributes.map(att => outputMap.getOrElse(att.name, 
att))
    +            val prunedRelation =
    +              LogicalRelation(prunedParquetRelation, 
Some(expectedOutputAttributes))
    +
    +            val projectionOverSchema = 
ProjectionOverSchema(prunedDataSchema)
    +
    +            // Construct a new target for our projection by rewriting and
    +            // including the original filters where available
    +            val projectionChild =
    +              if (filters.nonEmpty) {
    +                val projectedFilters = filters.map(_.transformDown {
    +                  case projectionOverSchema(expr) => expr
    +                })
    +                val newFilterCondition = projectedFilters.reduce(And)
    +                Filter(newFilterCondition, prunedRelation)
    +              } else {
    +                prunedRelation
    +              }
    +
    +            val nonDataPartitionColumnNames =
    +              
partitionSchema.filterNot(parquetDataFields.contains).map(_.name)
    +
    +            // Construct the new projections of our [[Project]] by
    +            // rewriting the original projections
    +            val newProjects = projects.map {
    +              case project if 
(nonDataPartitionColumnNames.contains(project.name)) => project
    +              case project =>
    +                (project transformDown {
    +                  case projectionOverSchema(expr) => expr
    +                }).asInstanceOf[NamedExpression]
    +            }
    +
    +            logDebug("New projects:\n" + 
newProjects.map(_.treeString).mkString("\n"))
    +
    +            require(prunedDataSchema == prunedParquetRelation.dataSchema,
    --- End diff --
    
    Could we break this require? I think it is not.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to