[SPARK-35906][SQL][FOLLOWUP] Recursive remove sort if the maximum number of rows less than or equal to 1

### What changes were proposed in this pull request?

Make it recursive remove sort  if the maximum number of rows less than or equal to 1. For example:
```sql
select a from (select a from values(0, 1) t(a, b) order by a) order by a
```

### Why are the changes needed?

Fix Once strategy's idempotence is broken for batch Eliminate Sorts.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Unit test.

Closes #33240 from wangyum/SPARK-35906-2.

Authored-by: Yuming Wang <yumwang@ebay.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
This commit is contained in:
Yuming Wang 2021-07-07 14:27:00 +09:00 committed by Hyukjin Kwon
parent 16c195ccfb
commit ddc5cb9051
2 changed files with 5 additions and 1 deletions

View file

@ -1214,7 +1214,7 @@ object EliminateSorts extends Rule[LogicalPlan] {
_.containsPattern(SORT))(applyLocally)
private val applyLocally: PartialFunction[LogicalPlan, LogicalPlan] = {
case Sort(_, _, child) if child.maxRows.exists(_ <= 1L) => child
case Sort(_, _, child) if child.maxRows.exists(_ <= 1L) => recursiveRemoveSort(child)
case s @ Sort(orders, _, child) if orders.isEmpty || orders.exists(_.child.foldable) =>
val newOrders = orders.filterNot(_.child.foldable)
if (newOrders.isEmpty) {

View file

@ -427,5 +427,9 @@ class EliminateSortsSuite extends AnalysisTest {
comparePlans(
Optimize.execute(testRelation.groupBy()(count(1).as("cnt")).orderBy('cnt.asc)).analyze,
testRelation.groupBy()(count(1).as("cnt")).analyze)
comparePlans(
Optimize.execute(testRelation.limit(Literal(1)).orderBy('a.asc).orderBy('a.asc)).analyze,
testRelation.limit(Literal(1)).analyze)
}
}