Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-25352][SQL] Perform ordered global limit when limit number is …
…bigger than topKSortFallbackThreshold ## What changes were proposed in this pull request? We have optimization on global limit to evenly distribute limit rows across all partitions. This optimization doesn't work for ordered results. For a query ending with sort + limit, in most cases it is performed by `TakeOrderedAndProjectExec`. But if limit number is bigger than `SQLConf.TOP_K_SORT_FALLBACK_THRESHOLD`, global limit will be used. At this moment, we need to do ordered global limit. ## How was this patch tested? Unit tests. Closes #22344 from viirya/SPARK-25352. Authored-by: Liang-Chi Hsieh <viirya@gmail.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
- Loading branch information
Showing
5 changed files
with
192 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
sql/core/src/test/scala/org/apache/spark/sql/execution/LimitSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.execution | ||
|
||
import scala.util.Random | ||
|
||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.test.SharedSQLContext | ||
|
||
|
||
class LimitSuite extends SparkPlanTest with SharedSQLContext { | ||
|
||
private var rand: Random = _ | ||
private var seed: Long = 0 | ||
|
||
protected override def beforeAll(): Unit = { | ||
super.beforeAll() | ||
seed = System.currentTimeMillis() | ||
rand = new Random(seed) | ||
} | ||
|
||
test("Produce ordered global limit if more than topKSortFallbackThreshold") { | ||
withSQLConf(SQLConf.TOP_K_SORT_FALLBACK_THRESHOLD.key -> "100") { | ||
val df = LimitTest.generateRandomInputData(spark, rand).sort("a") | ||
|
||
val globalLimit = df.limit(99).queryExecution.executedPlan.collect { | ||
case g: GlobalLimitExec => g | ||
} | ||
assert(globalLimit.size == 0) | ||
|
||
val topKSort = df.limit(99).queryExecution.executedPlan.collect { | ||
case t: TakeOrderedAndProjectExec => t | ||
} | ||
assert(topKSort.size == 1) | ||
|
||
val orderedGlobalLimit = df.limit(100).queryExecution.executedPlan.collect { | ||
case g: GlobalLimitExec => g | ||
} | ||
assert(orderedGlobalLimit.size == 1 && orderedGlobalLimit(0).orderedLimit == true) | ||
} | ||
} | ||
|
||
test("Ordered global limit") { | ||
val baseDf = LimitTest.generateRandomInputData(spark, rand) | ||
.select("a").repartition(3).sort("a") | ||
|
||
withSQLConf(SQLConf.LIMIT_FLAT_GLOBAL_LIMIT.key -> "true") { | ||
val orderedGlobalLimit = GlobalLimitExec(3, baseDf.queryExecution.sparkPlan, | ||
orderedLimit = true) | ||
val orderedGlobalLimitResult = SparkPlanTest.executePlan(orderedGlobalLimit, spark.sqlContext) | ||
.map(_.getInt(0)) | ||
|
||
val globalLimit = GlobalLimitExec(3, baseDf.queryExecution.sparkPlan, orderedLimit = false) | ||
val globalLimitResult = SparkPlanTest.executePlan(globalLimit, spark.sqlContext) | ||
.map(_.getInt(0)) | ||
|
||
// Global limit without order takes values at each partition sequentially. | ||
// After global sort, the values in second partition must be larger than the values | ||
// in first partition. | ||
assert(orderedGlobalLimitResult(0) == globalLimitResult(0)) | ||
assert(orderedGlobalLimitResult(1) < globalLimitResult(1)) | ||
assert(orderedGlobalLimitResult(2) < globalLimitResult(2)) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters