ulysses-you commented on a change in pull request #30368:
URL: https://github.com/apache/spark/pull/30368#discussion_r524804089



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -1452,11 +1452,27 @@ object PushPredicateThroughJoin extends 
Rule[LogicalPlan] with PredicateHelper {
 }
 
 /**
- * Combines two adjacent [[Limit]] operators into one, merging the
- * expressions into one single expression.
+ * 1. Eliminate [[Limit]] operators if it's child max row <= limit.
+ * 2. Combines two adjacent [[Limit]] operators into one, merging the
+ *    expressions into one single expression.
  */
-object CombineLimits extends Rule[LogicalPlan] {
-  def apply(plan: LogicalPlan): LogicalPlan = plan transform {
+object EliminateLimits extends Rule[LogicalPlan] {
+  private def canEliminate(limitExpr: Expression, child: LogicalPlan): Boolean 
= {
+    // We skip such case that Sort is after Limit since
+    // SparkStrategies will convert them to TakeOrderedAndProjectExec
+    val skipEliminate = child match {
+      case Sort(_, true, _) => true

Review comment:
       BTW, also add the shuffle benchmark
   ```
       runBenchmark("Sort and Limit") {
         val N = 100000
         val benchmark = new Benchmark("benchmark sort and limit", N)
   
         benchmark.addCase("TakeOrderedAndProject", 3) { _ =>
           spark.range(N).toDF("c").repartition(200).sort("c").take(200000)
         }
   
         benchmark.addCase("Sort And Limit", 3) { _ =>
           withSQLConf("spark.sql.execution.topKSortFallbackThreshold" -> "-1") 
{
             spark.range(N).toDF("c").repartition(200).sort("c").take(200000)
           }
         }
   
         benchmark.addCase("Sort", 3) { _ =>
           spark.range(N).toDF("c").repartition(200).sort("c").collect()
         }
         benchmark.run()
       }
   ```
   
   and the result is 
   ```
   Java HotSpot(TM) 64-Bit Server VM 1.8.0_191-b12 on Mac OS X 10.15.6
   Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
   benchmark sort and limit:                 Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
------------------------------------------------------------------------------------------------------------------------
   TakeOrderedAndProject                              1833           2259       
  382          0.1       18327.1       1.0X
   Sort And Limit                                     1417           1658       
  285          0.1       14167.5       1.3X
   Sort                                               1324           1484       
  225          0.1       13238.3       1.4X
   ```




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



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

Reply via email to