minyyy commented on a change in pull request #35864:
URL: https://github.com/apache/spark/pull/35864#discussion_r828412078



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -831,8 +831,16 @@ object ColumnPruning extends Rule[LogicalPlan] {
       e.copy(child = prunedChild(child, e.references))
 
     // prune unrequired references
-    case p @ Project(_, g: Generate) if p.references != g.outputSet =>
-      val requiredAttrs = p.references -- g.producedAttributes ++ 
g.generator.references
+    // There are 2 types of pruning here:
+    // 1. For attributes in g.child.outputSet that is not used by the 
generator nor the project,
+    //    we directly remove it from the output list of g.child.
+    // 2. For attributes that is not used by the project but it is used by the 
generator, we put
+    //    it in g.unrequiredChildIndex to save memory usage.
+    case p @ Project(_, g: Generate) if g.child.output.zipWithIndex.exists(
+      pair =>
+      !p.references.contains(pair._1) &&
+        (!g.generator.references.contains(pair._1) || 
!g.unrequiredChildIndex.contains(pair._2))) =>

Review comment:
       Done. But the 2 tests are the same, if you want I can remove one test.

##########
File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasingSuite.scala
##########
@@ -313,6 +313,25 @@ class NestedColumnAliasingSuite extends SchemaPruningTest {
     comparePlans(optimized, expected)
   }
 
+  test("Nested field pruning for Project and PosExplode") {

Review comment:
       Done.




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

To unsubscribe, e-mail: [email protected]

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