Github user vkhristenko commented on a diff in the pull request: https://github.com/apache/spark/pull/16578#discussion_r139312613 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetSchemaPruning.scala --- @@ -0,0 +1,130 @@ +/* + * 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, _) => StructType(Array(field)) } + .reduceLeft(_ merge _) --- End diff -- @viirya @mallman If I may add here, given a schema: ``` root | - a: StructType | | - f1: Int | | - f2: Int ``` and a selection `df.select("a.f1", "a.f2")` vs `df.select("a.f2", "a.f1")` will produce different requiredSchema fed into buildReader upon some action. In the first case it will be `StructType( StructField( "a", StructType(StructField("f1") :: StructField("f2") :: Nil)) :: Nil)` and in the second `StructType( StructField( "a", StructType(StructField("f2") :: StructField("f1") :: Nil)) :: Nil)` which means that the original schema is different from the one that is required for the second case. But as long as fields f1 and f2 are splitted (can be read without reading the other), it's remappable on the data source level. VK
--- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org