maropu commented on a change in pull request #27077: [SPARK-30408][SQL] Should 
not remove orderBy in sortBy clause in Optimizer
URL: https://github.com/apache/spark/pull/27077#discussion_r364100529
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ##########
 @@ -980,15 +980,25 @@ object EliminateSorts extends Rule[LogicalPlan] {
       if (newOrders.isEmpty) child else s.copy(order = newOrders)
     case Sort(orders, true, child) if 
SortOrder.orderingSatisfies(child.outputOrdering, orders) =>
       child
-    case s @ Sort(_, _, child) => s.copy(child = recursiveRemoveSort(child))
+    case s @ Sort(_, _, child) => s.copy(child = recursiveRemoveSort(s, child))
     case j @ Join(originLeft, originRight, _, cond, _) if 
cond.forall(_.deterministic) =>
       j.copy(left = recursiveRemoveSort(originLeft), right = 
recursiveRemoveSort(originRight))
     case g @ Aggregate(_, aggs, originChild) if isOrderIrrelevantAggs(aggs) =>
       g.copy(child = recursiveRemoveSort(originChild))
   }
 
+  private def recursiveRemoveSort(parent: Sort, plan: LogicalPlan): 
LogicalPlan = plan match {
+    case s @ Sort(_, _, child) =>
+      if (parent.global == s.global) {
+        recursiveRemoveSort(child)
+      } else {
+        s.copy(child = recursiveRemoveSort(child))
+      }
+    case _ => recursiveRemoveSort(plan)
+  }
+
   private def recursiveRemoveSort(plan: LogicalPlan): LogicalPlan = plan match 
{
-    case Sort(_, _, child) => recursiveRemoveSort(child)
+    case s @ Sort(_, _, child) => recursiveRemoveSort(s, child)
 
 Review comment:
   How about the case , `global sort => project/filter => local sort`?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to