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



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -613,6 +613,15 @@ object LimitPushDown extends Rule[LogicalPlan] {
         case _ => join
       }
       LocalLimit(exp, newJoin)
+
+    case LocalLimit(limitExpr @ IntegerLiteral(limitVal),
+        window @ Window(Seq(Alias(WindowExpression(_: RankLike | _: RowNumber,
+            WindowSpecDefinition(Nil, orderSpec,
+                SpecifiedWindowFrame(RowFrame, UnboundedPreceding, 
CurrentRow))), _)), _, _, child))
+        if child.maxRows.forall( _ > limitVal) =>
+      LocalLimit(

Review comment:
       It should be optimized by `EliminateLimits` later.  Otherwise, the plan 
cannot be further optimized. Like this:
   ```
   !GlobalLimit 2                                                               
                                                                                
            
   !+- Window [row_number() windowspecdefinition(c#0 DESC NULLS LAST, 
specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rn#0], 
[c#0 DESC NULLS LAST] 
   !   +- GlobalLimit 2                                                         
                                                                                
            
   !      +- LocalLimit 2                                                       
                                                                                
            
   !         +- Sort [c#0 DESC NULLS LAST], true                                
                                                                                
            
   !            +- LocalRelation [a#0, b#0, c#0]                                
                                                                                
                                                                                
                                                                                
               
   ```
   




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