-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23957][SQL] Remove redundant sort operators from subqueries #21049
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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
78 changes: 78 additions & 0 deletions
78
...catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/RemoveSubquerySorts.scala
This file contains hidden or 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,78 @@ | ||
| /* | ||
| * 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.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.{EliminateSubqueryAliases, SimpleAnalyzer} | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules._ | ||
|
|
||
| class RemoveSubquerySortsSuite extends PlanTest { | ||
|
|
||
| private object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("Subqueries", Once, | ||
| RemoveSubquerySorts, | ||
| EliminateSubqueryAliases) :: Nil | ||
| } | ||
|
|
||
| private val testRelation = LocalRelation('a.int, 'b.int, 'c.int) | ||
|
|
||
| private def analyzeAndCompare(plan: LogicalPlan, correct: LogicalPlan) { | ||
| // We can't use the implicit analyze method, that tests usually use, for 'plan' | ||
| // because it explicitly calls EliminateSubqueryAliases. | ||
| comparePlans(Optimize.execute(SimpleAnalyzer.execute(plan)), correct.analyze) | ||
| } | ||
|
|
||
| test("Remove top-level sort") { | ||
| val query = testRelation.orderBy('a.asc).subquery('x) | ||
| analyzeAndCompare(query, testRelation) | ||
| } | ||
|
|
||
| test("Remove sort behind filter and project") { | ||
| val query = testRelation.orderBy('a.asc).where('a.attr > 10).select('b).subquery('x) | ||
| analyzeAndCompare(query, testRelation.where('a.attr > 10).select('b)) | ||
| } | ||
|
|
||
| test("Remove sort below subquery that is not at root") { | ||
| val query = testRelation.orderBy('a.asc).subquery('x).groupBy('a)(sum('b)) | ||
| analyzeAndCompare(query, testRelation.groupBy('a)(sum('b))) | ||
| } | ||
|
|
||
| test("Sorts with limits must not be removed from subqueries") { | ||
| val query = testRelation.orderBy('a.asc).limit(10).subquery('x) | ||
| analyzeAndCompare(query, testRelation.orderBy('a.asc).limit(10)) | ||
| } | ||
|
|
||
| test("Remove more than one sort") { | ||
| val query = testRelation.orderBy('a.asc).orderBy('b.desc).subquery('x) | ||
| analyzeAndCompare(query, testRelation) | ||
| } | ||
|
|
||
| test("Nested subqueries") { | ||
| val query = testRelation.orderBy('a.asc).subquery('x).orderBy('b.desc).subquery('y) | ||
| analyzeAndCompare(query, testRelation) | ||
| } | ||
|
|
||
| test("Sorts below non-project / filter operators don't get removed") { | ||
| val query = testRelation.orderBy('a.asc).groupBy('a)(sum('b)).subquery('x) | ||
| analyzeAndCompare(query, testRelation.orderBy('a.asc).groupBy('a)(sum('b))) | ||
| } | ||
| } |
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.
SubqueryAliasis not the subquery you want. This is just an alias of a query/table/view. For example,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.
Thanks! I've been trying to understand the role of
SubqueryandSubqueryAlias. My confusion is that subqueries do seem to get planned asSubqueryAliasoperators, e.g.:In the example you give I (personally) think it's still reasonable to drop the ordering, but understand that might surprise some users. It wouldn't be hard to skip the root if it's a subquery - but what do you propose for detecting subqueries if my method isn't right?
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.
Seq((1, 2, "1"), (3, 4, "3")).toDF("int", "int2", "str_sort").orderBy('int.asc).as('df1)Before entering optimizer, we get rid of
SubqueryAliasby the ruleEliminateSubqueryAliases. Basically, it is no-op after query analysis. The name is a little bit confusing, I have to admit.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.
Yep, that's why I added the new rule just before
EliminateSubqueryAliases(which runs in the optimizer, as part of the 'finish analysis' batch). AfterEliminateSubqueryAliasesthere doesn't seem to be any way to detect subqueries.Another approach I suppose would be to handle this like
SparkPlan'srequiredChildOrdering- if a parent doesn't require any ordering of the child, (and the child is aSortnode), the childSortshould be dropped. That seems like a more fundamental change though.