-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-23375][SQL] Eliminate unneeded Sort in Optimizer #20560
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
550ff99
[SPARK-23375][SQL] Eliminate unneeded Sort in Optimizer
mgaido91 81e4828
Use separate rule and add more tests
mgaido91 1c33263
rename to outputOrdering
mgaido91 60ea6fc
remove outputOrdering for RangePartitioning
mgaido91 1c7cae6
fix Range ordering + add more Range UT + address comments
mgaido91 e376c19
fix ut failure
mgaido91 930ef7b
rename to OrderPreservingUnaryNode
mgaido91 a1846ab
fix InMemoryRelation
mgaido91 0cd4899
Merge branch 'master' of github.com:apache/spark into SPARK-23375
mgaido91 4e441f8
fix build failure
mgaido91 6e95e37
fix UT
mgaido91 6c5f04c
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 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
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
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
101 changes: 101 additions & 0 deletions
101
...st/src/test/scala/org/apache/spark/sql/catalyst/optimizer/RemoveRedundantSortsSuite.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,101 @@ | ||
/* | ||
* 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.{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 { | ||
|
||
object Optimize extends RuleExecutor[LogicalPlan] { | ||
val batches = | ||
Batch("Remove Redundant Sorts", Once, | ||
RemoveRedundantSorts) :: | ||
Batch("Collapse Project", Once, | ||
CollapseProject) :: Nil | ||
} | ||
|
||
val testRelation = LocalRelation('a.int, 'b.int, 'c.int) | ||
|
||
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 optimized = Optimize.execute(unnecessaryReordered.analyze) | ||
val correctAnswer = orderedPlan.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 optimized = Optimize.execute(reorderedDifferently.analyze) | ||
val correctAnswer = reorderedDifferently.analyze | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("filters don't affect order") { | ||
val orderedPlan = testRelation.select('a, 'b).orderBy('a.asc, 'b.desc) | ||
val filteredAndReordered = orderedPlan.where('a > Literal(10)).orderBy('a.asc, 'b.desc) | ||
val optimized = Optimize.execute(filteredAndReordered.analyze) | ||
val correctAnswer = orderedPlan.where('a > Literal(10)).analyze | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("limits don't affect order") { | ||
val orderedPlan = testRelation.select('a, 'b).orderBy('a.asc, 'b.desc) | ||
val filteredAndReordered = orderedPlan.limit(Literal(10)).orderBy('a.asc, 'b.desc) | ||
val optimized = Optimize.execute(filteredAndReordered.analyze) | ||
val correctAnswer = orderedPlan.limit(Literal(10)).analyze | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
|
||
test("range is already sorted") { | ||
val inputPlan = Range(1L, 1000L, 1, 10) | ||
val orderedPlan = inputPlan.orderBy('id.asc) | ||
val optimized = Optimize.execute(orderedPlan.analyze) | ||
val correctAnswer = inputPlan.analyze | ||
comparePlans(optimized, correctAnswer) | ||
|
||
val reversedPlan = inputPlan.orderBy('id.desc) | ||
val reversedOptimized = Optimize.execute(reversedPlan.analyze) | ||
val reversedCorrectAnswer = reversedPlan.analyze | ||
comparePlans(reversedOptimized, reversedCorrectAnswer) | ||
|
||
val negativeStepInputPlan = Range(10L, 1L, -1, 10) | ||
val negativeStepOrderedPlan = negativeStepInputPlan.orderBy('id.desc) | ||
val negativeStepOptimized = Optimize.execute(negativeStepOrderedPlan.analyze) | ||
val negativeStepCorrectAnswer = negativeStepInputPlan.analyze | ||
comparePlans(negativeStepOptimized, negativeStepCorrectAnswer) | ||
} | ||
|
||
test("sort should not be removed when there is a node which doesn't guarantee any order") { | ||
val orderedPlan = testRelation.select('a, 'b).orderBy('a.asc) | ||
val groupedAndResorted = orderedPlan.groupBy('a)(sum('a)).orderBy('a.asc) | ||
val optimized = Optimize.execute(groupedAndResorted.analyze) | ||
val correctAnswer = groupedAndResorted.analyze | ||
comparePlans(optimized, correctAnswer) | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -26,7 +26,7 @@ import org.apache.spark.sql.catalyst.analysis.MultiInstanceRelation | |
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.plans.QueryPlan | ||
import org.apache.spark.sql.catalyst.plans.logical | ||
import org.apache.spark.sql.catalyst.plans.logical.{HintInfo, Statistics} | ||
import org.apache.spark.sql.catalyst.plans.logical.{HintInfo, LogicalPlan, Statistics} | ||
import org.apache.spark.sql.execution.SparkPlan | ||
import org.apache.spark.storage.StorageLevel | ||
import org.apache.spark.util.LongAccumulator | ||
|
@@ -39,9 +39,9 @@ object InMemoryRelation { | |
storageLevel: StorageLevel, | ||
child: SparkPlan, | ||
tableName: Option[String], | ||
statsOfPlanToCache: Statistics): InMemoryRelation = | ||
logicalPlan: LogicalPlan): InMemoryRelation = | ||
new InMemoryRelation(child.output, useCompression, batchSize, storageLevel, child, tableName)( | ||
statsOfPlanToCache = statsOfPlanToCache) | ||
statsOfPlanToCache = logicalPlan.stats, outputOrdering = logicalPlan.outputOrdering) | ||
} | ||
|
||
|
||
|
@@ -64,7 +64,8 @@ case class InMemoryRelation( | |
tableName: Option[String])( | ||
@transient var _cachedColumnBuffers: RDD[CachedBatch] = null, | ||
val sizeInBytesStats: LongAccumulator = child.sqlContext.sparkContext.longAccumulator, | ||
statsOfPlanToCache: Statistics) | ||
statsOfPlanToCache: Statistics, | ||
override val outputOrdering: Seq[SortOrder]) | ||
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. This should be added to |
||
extends logical.LeafNode with MultiInstanceRelation { | ||
|
||
override protected def innerChildren: Seq[SparkPlan] = Seq(child) | ||
|
@@ -76,7 +77,8 @@ case class InMemoryRelation( | |
tableName = None)( | ||
_cachedColumnBuffers, | ||
sizeInBytesStats, | ||
statsOfPlanToCache) | ||
statsOfPlanToCache, | ||
outputOrdering) | ||
|
||
override def producedAttributes: AttributeSet = outputSet | ||
|
||
|
@@ -159,7 +161,7 @@ case class InMemoryRelation( | |
def withOutput(newOutput: Seq[Attribute]): InMemoryRelation = { | ||
InMemoryRelation( | ||
newOutput, useCompression, batchSize, storageLevel, child, tableName)( | ||
_cachedColumnBuffers, sizeInBytesStats, statsOfPlanToCache) | ||
_cachedColumnBuffers, sizeInBytesStats, statsOfPlanToCache, outputOrdering) | ||
} | ||
|
||
override def newInstance(): this.type = { | ||
|
@@ -172,7 +174,8 @@ case class InMemoryRelation( | |
tableName)( | ||
_cachedColumnBuffers, | ||
sizeInBytesStats, | ||
statsOfPlanToCache).asInstanceOf[this.type] | ||
statsOfPlanToCache, | ||
outputOrdering).asInstanceOf[this.type] | ||
} | ||
|
||
def cachedColumnBuffers: RDD[CachedBatch] = _cachedColumnBuffers | ||
|
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
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
Oops, something went wrong.
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.
Like
ProjectExec.outputOrdering
, we can propagate ordering for aliased attributes.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.
sorry, I don't fully understand what you mean. In
ProjectExec.outputOrdering
we are getting thechild.outputOrdering
exactly as it is done here.