-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32276][SQL] Remove redundant sorts before repartition nodes #29089
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
4 commits
Select commit
Hold shift + click to select a range
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
193 changes: 193 additions & 0 deletions
193
.../scala/org/apache/spark/sql/catalyst/optimizer/EliminateSortsBeforeRepartitionSuite.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,193 @@ | ||
| /* | ||
| * 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.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
|
|
||
| class EliminateSortsBeforeRepartitionSuite extends PlanTest { | ||
|
|
||
| val catalog = new SessionCatalog(new InMemoryCatalog, EmptyFunctionRegistry, conf) | ||
| val analyzer = new Analyzer(catalog, conf) | ||
|
|
||
| val testRelation = LocalRelation('a.int, 'b.int, 'c.int) | ||
| val anotherTestRelation = LocalRelation('d.int, 'e.int) | ||
|
|
||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("Default", FixedPoint(10), | ||
| FoldablePropagation, | ||
| LimitPushDown) :: | ||
| Batch("Eliminate Sorts", Once, | ||
| EliminateSorts) :: | ||
| Batch("Collapse Project", Once, | ||
| CollapseProject) :: Nil | ||
| } | ||
|
|
||
| def repartition(plan: LogicalPlan): LogicalPlan = plan.repartition(10) | ||
|
|
||
| test("sortBy") { | ||
| val plan = testRelation.select('a, 'b).sortBy('a.asc, 'b.desc) | ||
| val optimizedPlan = testRelation.select('a, 'b) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("sortBy with projection") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).select('a + 1 as "a", 'b + 2 as "b") | ||
| val optimizedPlan = testRelation.select('a + 1 as "a", 'b + 2 as "b") | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("sortBy with projection and filter") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).select('a, 'b).where('a === 10) | ||
| val optimizedPlan = testRelation.select('a, 'b).where('a === 10) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("sortBy with limit") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).limit(10) | ||
| val optimizedPlan = testRelation.sortBy('a.asc, 'b.asc).limit(10) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("sortBy with non-deterministic projection") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).select(rand(1), 'a, 'b) | ||
| val optimizedPlan = testRelation.sortBy('a.asc, 'b.asc).select(rand(1), 'a, 'b) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("orderBy") { | ||
| val plan = testRelation.select('a, 'b).orderBy('a.asc, 'b.asc) | ||
| val optimizedPlan = testRelation.select('a, 'b) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("orderBy with projection") { | ||
| val plan = testRelation.orderBy('a.asc, 'b.asc).select('a + 1 as "a", 'b + 2 as "b") | ||
| val optimizedPlan = testRelation.select('a + 1 as "a", 'b + 2 as "b") | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("orderBy with projection and filter") { | ||
| val plan = testRelation.orderBy('a.asc, 'b.asc).select('a, 'b).where('a === 10) | ||
| val optimizedPlan = testRelation.select('a, 'b).where('a === 10) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("orderBy with limit") { | ||
| val plan = testRelation.orderBy('a.asc, 'b.asc).limit(10) | ||
| val optimizedPlan = testRelation.orderBy('a.asc, 'b.asc).limit(10) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("orderBy with non-deterministic projection") { | ||
| val plan = testRelation.orderBy('a.asc, 'b.asc).select(rand(1), 'a, 'b) | ||
| val optimizedPlan = testRelation.orderBy('a.asc, 'b.asc).select(rand(1), 'a, 'b) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("additional coalesce and sortBy") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).coalesce(1) | ||
| val optimizedPlan = testRelation.coalesce(1) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("additional projection, repartition and sortBy") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).repartition(100).select('a + 1 as "a") | ||
| val optimizedPlan = testRelation.repartition(100).select('a + 1 as "a") | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("additional filter, distribute and sortBy") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).distribute('a)(2).where('a === 10) | ||
| val optimizedPlan = testRelation.distribute('a)(2).where('a === 10) | ||
| checkRepartitionCases(plan, optimizedPlan) | ||
| } | ||
|
|
||
| test("join") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).distribute('a)(2).where('a === 10) | ||
| val optimizedPlan = testRelation.distribute('a)(2).where('a === 10) | ||
| val anotherPlan = anotherTestRelation.select('d) | ||
| val joinPlan = plan.join(anotherPlan) | ||
| val optimizedJoinPlan = optimize(joinPlan) | ||
| val correctJoinPlan = analyze(optimizedPlan.join(anotherPlan)) | ||
| comparePlans(optimizedJoinPlan, correctJoinPlan) | ||
| } | ||
|
|
||
| test("aggregate") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).distribute('a)(2).where('a === 10) | ||
| val optimizedPlan = testRelation.distribute('a)(2).where('a === 10) | ||
| val aggPlan = plan.groupBy('a)(sum('b)) | ||
| val optimizedAggPlan = optimize(aggPlan) | ||
| val correctAggPlan = analyze(optimizedPlan.groupBy('a)(sum('b))) | ||
| comparePlans(optimizedAggPlan, correctAggPlan) | ||
| } | ||
|
|
||
| protected def checkRepartitionCases(plan: LogicalPlan, optimizedPlan: LogicalPlan): Unit = { | ||
| // cannot remove sortBy before repartition without sortBy/orderBy | ||
| val planWithRepartition = repartition(plan) | ||
| val optimizedPlanWithRepartition = optimize(planWithRepartition) | ||
| val correctPlanWithRepartition = analyze(planWithRepartition) | ||
| comparePlans(optimizedPlanWithRepartition, correctPlanWithRepartition) | ||
|
|
||
| // can remove sortBy before repartition with sortBy | ||
| val planWithRepartitionAndSortBy = planWithRepartition.sortBy('a.asc) | ||
| val optimizedPlanWithRepartitionAndSortBy = optimize(planWithRepartitionAndSortBy) | ||
| val correctPlanWithRepartitionAndSortBy = analyze(repartition(optimizedPlan).sortBy('a.asc)) | ||
| comparePlans(optimizedPlanWithRepartitionAndSortBy, correctPlanWithRepartitionAndSortBy) | ||
|
|
||
| // can remove sortBy before repartition with orderBy | ||
| val planWithRepartitionAndOrderBy = planWithRepartition.orderBy('a.asc) | ||
| val optimizedPlanWithRepartitionAndOrderBy = optimize(planWithRepartitionAndOrderBy) | ||
| val correctPlanWithRepartitionAndOrderBy = analyze(repartition(optimizedPlan).orderBy('a.asc)) | ||
| comparePlans(optimizedPlanWithRepartitionAndOrderBy, correctPlanWithRepartitionAndOrderBy) | ||
| } | ||
|
|
||
| private def analyze(plan: LogicalPlan): LogicalPlan = { | ||
| analyzer.execute(plan) | ||
| } | ||
|
|
||
| private def optimize(plan: LogicalPlan): LogicalPlan = { | ||
| Optimize.execute(analyzer.execute(plan)) | ||
| } | ||
| } | ||
|
|
||
| class EliminateSortsBeforeRepartitionByExprsSuite extends EliminateSortsBeforeRepartitionSuite { | ||
| override def repartition(plan: LogicalPlan): LogicalPlan = plan.distribute('a)(10) | ||
|
|
||
| test("sortBy before repartition with non-deterministic expressions") { | ||
| val plan = testRelation.sortBy('a.asc, 'b.asc).limit(10) | ||
| val planWithRepartition = plan.distribute(rand(1).asc, 'a.asc)(20) | ||
| checkRepartitionCases(plan = planWithRepartition, optimizedPlan = planWithRepartition) | ||
| } | ||
|
|
||
| test("orderBy before repartition with non-deterministic expressions") { | ||
| val plan = testRelation.orderBy('a.asc, 'b.asc).limit(10) | ||
| val planWithRepartition = plan.distribute(rand(1).asc, 'a.asc)(20) | ||
| checkRepartitionCases(plan = planWithRepartition, optimizedPlan = planWithRepartition) | ||
| } | ||
| } | ||
|
|
||
| class EliminateSortsBeforeCoalesceSuite extends EliminateSortsBeforeRepartitionSuite { | ||
| override def repartition(plan: LogicalPlan): LogicalPlan = plan.coalesce(1) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.