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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -1322,6 +1322,25 @@ object CombineUnions extends Rule[LogicalPlan] {
         case Union(children, byName, allowMissingCol)
             if byName == topByName && allowMissingCol == topAllowMissingCol =>
           stack.pushAll(children.reverse)
+        // Push down projection and then push pushed plan to Stack if there is 
a Project.
+        case Project(projectList, Distinct(u @ Union(children, byName, 
allowMissingCol)))
+            if projectList.forall(_.deterministic) && children.nonEmpty &&
+              flattenDistinct && byName == topByName && allowMissingCol == 
topAllowMissingCol =>
+          val newChildren = 
PushProjectionThroughUnion.pushProjectionThroughUnion(projectList, u)
+            .map(CollapseProject(_))
+          stack.pushAll(newChildren.reverse)
+        case Project(projectList, Deduplicate(keys: Seq[Attribute], u: Union))
+            if projectList.forall(_.deterministic) && flattenDistinct && 
u.byName == topByName &&
+              u.allowMissingCol == topAllowMissingCol && AttributeSet(keys) == 
u.outputSet =>
+          val newChildren = 
PushProjectionThroughUnion.pushProjectionThroughUnion(projectList, u)
+            .map(CollapseProject(_))
+          stack.pushAll(newChildren.reverse)
+        case Project(projectList, u @ Union(children, byName, allowMissingCol))
+            if projectList.forall(_.deterministic) && children.nonEmpty &&
+              byName == topByName && allowMissingCol == topAllowMissingCol =>
+          val newChildren = 
PushProjectionThroughUnion.pushProjectionThroughUnion(projectList, u)
+            .map(CollapseProject(_))

Review comment:
       Make sense.




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