wangyum commented on a change in pull request #31993:
URL: https://github.com/apache/spark/pull/31993#discussion_r614484444



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/SchemaPruning.scala
##########
@@ -32,11 +33,10 @@ object SchemaPruning {
     // in the resulting schema may differ from their ordering in the logical 
relation's
     // original schema
     val mergedSchema = requestedRootFields
-      .map { case root: RootField => StructType(Array(root.field)) }
+      .map { root: RootField => StructType(Array(root.field)) }
       .reduceLeft(_ merge _)
-    val dataSchemaFieldNames = dataSchema.fieldNames.toSet
     val mergedDataSchema =
-      StructType(mergedSchema.filter(f => 
dataSchemaFieldNames.contains(f.name)))
+      StructType(dataSchema.map(s => 
mergedSchema.find(_.name.equals(s.name)).getOrElse(s)))

Review comment:
       1. Prune nested schema:
   
https://github.com/apache/spark/blob/0f2c0b53e8fb18c86c67b5dd679c006db93f94a5/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/SchemaPruning.scala#L28-L43
   
   2. Use this pruned nested schema to build the `dataSchema` in `Relation`
   
https://github.com/apache/spark/blob/25e7d1ceee8c9f4ecb4ab796f51e9bcbc0500fae/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/SchemaPruning.scala#L81-L86
   
   2. The `readDataColumns` is the complete column pruning:
   
https://github.com/apache/spark/blob/7a5647a93aaea9d1d78d9262e24fc8c010db04d0/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileSourceStrategy.scala#L208-L226
   
   3. `dataSchema` from `relation.dataSchema`. It is the pruned nested schema:
   
https://github.com/apache/spark/blob/935aa8c8db6824648483f26c2889c33030985259/sql/core/src/main/scala/org/apache/spark/sql/execution/DataSourceScanExec.scala#L398-L407
   
   4. `OrcUtils.requestedColumnIds` use this pruned nested schema:
   
https://github.com/apache/spark/blob/1fc66f68703e0b14e03fe0ed5ca93e9af20f41a9/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/OrcFileFormat.scala#L193-L197




-- 
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:
[email protected]



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

Reply via email to