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



##########
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:
       Some thoughts here:
   - the condition sounds right to me;
   - however, maybe it's just me, but it seems fragile for me to maintain two 
versions of the same logic forward - in `if` guard and in branch body;
   - `unrequiredChildIndex` is a `Seq` rather than a set. Can it result in 
quadratic behaviors (probably in extreme cases where both `g.child.output` and 
`g.generator.references` are large)? 
   
   Does it make sense to use an extractor pattern?




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