-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-23973][SQL] Remove consecutive Sorts #21072
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ba4186
[SPARK-23973][SQL] Remove consecutive Sorts
mgaido91 ff7d412
remove Sort operator separated by 0...n Project/Filter operators
mgaido91 ac03bed
fix test
mgaido91 1d6ca1e
add test cases + fix typo
mgaido91 e7391f3
address comments
mgaido91 e2f4d4d
address comment
mgaido91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,16 +17,12 @@ | |
|
||
package org.apache.spark.sql.catalyst.optimizer | ||
|
||
import org.apache.spark.sql.catalyst.analysis.{Analyzer, EmptyFunctionRegistry} | ||
import org.apache.spark.sql.catalyst.catalog.{InMemoryCatalog, SessionCatalog} | ||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.dsl.plans._ | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.plans._ | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules._ | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.internal.SQLConf.{CASE_SENSITIVE, ORDER_BY_ORDINAL} | ||
|
||
class RemoveRedundantSortsSuite extends PlanTest { | ||
|
||
|
@@ -42,15 +38,15 @@ class RemoveRedundantSortsSuite extends PlanTest { | |
|
||
test("remove redundant order by") { | ||
val orderedPlan = testRelation.select('a, 'b).orderBy('a.asc, 'b.desc_nullsFirst) | ||
val unnecessaryReordered = orderedPlan.select('a).orderBy('a.asc, 'b.desc_nullsFirst) | ||
val unnecessaryReordered = orderedPlan.limit(2).select('a).orderBy('a.asc, 'b.desc_nullsFirst) | ||
val optimized = Optimize.execute(unnecessaryReordered.analyze) | ||
val correctAnswer = orderedPlan.select('a).analyze | ||
val correctAnswer = orderedPlan.limit(2).select('a).analyze | ||
comparePlans(Optimize.execute(optimized), correctAnswer) | ||
} | ||
|
||
test("do not remove sort if the order is different") { | ||
val orderedPlan = testRelation.select('a, 'b).orderBy('a.asc, 'b.desc_nullsFirst) | ||
val reorderedDifferently = orderedPlan.select('a).orderBy('a.asc, 'b.desc) | ||
val reorderedDifferently = orderedPlan.limit(2).select('a).orderBy('a.asc, 'b.desc) | ||
val optimized = Optimize.execute(reorderedDifferently.analyze) | ||
val correctAnswer = reorderedDifferently.analyze | ||
comparePlans(optimized, correctAnswer) | ||
|
@@ -72,6 +68,14 @@ class RemoveRedundantSortsSuite extends PlanTest { | |
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("different sorts are not simplified if limit is in between") { | ||
val orderedPlan = testRelation.select('a, 'b).orderBy('b.desc).limit(Literal(10)) | ||
.orderBy('a.asc) | ||
val optimized = Optimize.execute(orderedPlan.analyze) | ||
val correctAnswer = orderedPlan.analyze | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("range is already sorted") { | ||
val inputPlan = Range(1L, 1000L, 1, 10) | ||
val orderedPlan = inputPlan.orderBy('id.asc) | ||
|
@@ -98,4 +102,37 @@ class RemoveRedundantSortsSuite extends PlanTest { | |
val correctAnswer = groupedAndResorted.analyze | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("remove two consecutive sorts") { | ||
val orderedTwice = testRelation.orderBy('a.asc).orderBy('b.desc) | ||
val optimized = Optimize.execute(orderedTwice.analyze) | ||
val correctAnswer = testRelation.orderBy('b.desc).analyze | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test for three consecutive sorts? Two is the base case, three will help us show the inductive case :) |
||
|
||
test("remove sorts separated by Filter/Project operators") { | ||
val orderedTwiceWithProject = testRelation.orderBy('a.asc).select('b).orderBy('b.desc) | ||
val optimizedWithProject = Optimize.execute(orderedTwiceWithProject.analyze) | ||
val correctAnswerWithProject = testRelation.select('b).orderBy('b.desc).analyze | ||
comparePlans(optimizedWithProject, correctAnswerWithProject) | ||
|
||
val orderedTwiceWithFilter = | ||
testRelation.orderBy('a.asc).where('b > Literal(0)).orderBy('b.desc) | ||
val optimizedWithFilter = Optimize.execute(orderedTwiceWithFilter.analyze) | ||
val correctAnswerWithFilter = testRelation.where('b > Literal(0)).orderBy('b.desc).analyze | ||
comparePlans(optimizedWithFilter, correctAnswerWithFilter) | ||
|
||
val orderedTwiceWithBoth = | ||
testRelation.orderBy('a.asc).select('b).where('b > Literal(0)).orderBy('b.desc) | ||
val optimizedWithBoth = Optimize.execute(orderedTwiceWithBoth.analyze) | ||
val correctAnswerWithBoth = | ||
testRelation.select('b).where('b > Literal(0)).orderBy('b.desc).analyze | ||
comparePlans(optimizedWithBoth, correctAnswerWithBoth) | ||
|
||
val orderedThrice = orderedTwiceWithBoth.select(('b + 1).as('c)).orderBy('c.asc) | ||
val optimizedThrice = Optimize.execute(orderedThrice.analyze) | ||
val correctAnswerThrice = testRelation.select('b).where('b > Literal(0)) | ||
.select(('b + 1).as('c)).orderBy('c.asc).analyze | ||
comparePlans(optimizedThrice, correctAnswerThrice) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test which explicitly confirms that sort.limit.sort is not simplified? I know the above two tests cover that case, but it's good to have one dedicated to testing this important property.