-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-12503][SPARK-12505] Limit pushdown in UNION ALL and OUTER JOIN #11121
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
16 commits
Select commit
Hold shift + click to select a range
9df9ffd
Split logical limit operator.
JoshRosen 449e96e
Also split physical planning.
JoshRosen 060b9b8
Import gatorsmile's pushdown through union and clean up to work with …
JoshRosen 00e7f39
Fix join pushdown.
JoshRosen 7b86111
Define maxRows in more operators.
JoshRosen 985e00f
Style and indentation fixes.
JoshRosen 8e1491b
Keep LocalLimit on top after pushdown.
JoshRosen 30b9613
Add failing tests demonstrating why FullOuter limit pushdown rule nee…
JoshRosen 96701ad
Update FullOuter pushdown per discussion to address repeated applicat…
JoshRosen 9abb38f
Undo weird IntelliJ auto-formatting issue.
JoshRosen d9a3ca4
Merge remote-tracking branch 'origin/master' into limit-pushdown-2
JoshRosen d59d2c4
More pushdown tests.
JoshRosen ad5e40f
Disable optimizer rule for now.
JoshRosen c28343f
Change maxRows from an expression into a Long.
JoshRosen ac3e978
Merge remote-tracking branch 'origin/master' into limit-pushdown-2
JoshRosen 020fafe
Fix compilation in SQLBuilder.
JoshRosen 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
145 changes: 145 additions & 0 deletions
145
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/LimitPushdownSuite.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,145 @@ | ||
| /* | ||
| * 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.EliminateSubQueries | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions.Add | ||
| import org.apache.spark.sql.catalyst.plans.{FullOuter, LeftOuter, PlanTest, RightOuter} | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules._ | ||
|
|
||
| class LimitPushdownSuite extends PlanTest { | ||
|
|
||
| private object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("Subqueries", Once, | ||
| EliminateSubQueries) :: | ||
| Batch("Limit pushdown", FixedPoint(100), | ||
| LimitPushDown, | ||
| CombineLimits, | ||
| ConstantFolding, | ||
| BooleanSimplification) :: Nil | ||
| } | ||
|
|
||
| private val testRelation = LocalRelation('a.int, 'b.int, 'c.int) | ||
| private val testRelation2 = LocalRelation('d.int, 'e.int, 'f.int) | ||
| private val x = testRelation.subquery('x) | ||
| private val y = testRelation.subquery('y) | ||
|
|
||
| // Union --------------------------------------------------------------------------------------- | ||
|
|
||
| test("Union: limit to each side") { | ||
| val unionQuery = Union(testRelation, testRelation2).limit(1) | ||
| val unionOptimized = Optimize.execute(unionQuery.analyze) | ||
| val unionCorrectAnswer = | ||
| Limit(1, Union(LocalLimit(1, testRelation), LocalLimit(1, testRelation2))).analyze | ||
| comparePlans(unionOptimized, unionCorrectAnswer) | ||
| } | ||
|
|
||
| test("Union: limit to each side with constant-foldable limit expressions") { | ||
| val unionQuery = Union(testRelation, testRelation2).limit(Add(1, 1)) | ||
| val unionOptimized = Optimize.execute(unionQuery.analyze) | ||
| val unionCorrectAnswer = | ||
| Limit(2, Union(LocalLimit(2, testRelation), LocalLimit(2, testRelation2))).analyze | ||
| comparePlans(unionOptimized, unionCorrectAnswer) | ||
| } | ||
|
|
||
| test("Union: limit to each side with the new limit number") { | ||
| val unionQuery = Union(testRelation, testRelation2.limit(3)).limit(1) | ||
| val unionOptimized = Optimize.execute(unionQuery.analyze) | ||
| val unionCorrectAnswer = | ||
| Limit(1, Union(LocalLimit(1, testRelation), LocalLimit(1, testRelation2))).analyze | ||
| comparePlans(unionOptimized, unionCorrectAnswer) | ||
| } | ||
|
|
||
| test("Union: no limit to both sides if children having smaller limit values") { | ||
| val unionQuery = Union(testRelation.limit(1), testRelation2.select('d).limit(1)).limit(2) | ||
| val unionOptimized = Optimize.execute(unionQuery.analyze) | ||
| val unionCorrectAnswer = | ||
| Limit(2, Union(testRelation.limit(1), testRelation2.select('d).limit(1))).analyze | ||
| comparePlans(unionOptimized, unionCorrectAnswer) | ||
| } | ||
|
|
||
| test("Union: limit to each sides if children having larger limit values") { | ||
| val testLimitUnion = Union(testRelation.limit(3), testRelation2.select('d).limit(4)) | ||
| val unionQuery = testLimitUnion.limit(2) | ||
| val unionOptimized = Optimize.execute(unionQuery.analyze) | ||
| val unionCorrectAnswer = | ||
| Limit(2, Union(LocalLimit(2, testRelation), LocalLimit(2, testRelation2.select('d)))).analyze | ||
| comparePlans(unionOptimized, unionCorrectAnswer) | ||
| } | ||
|
|
||
| // Outer join ---------------------------------------------------------------------------------- | ||
|
|
||
| test("left outer join") { | ||
| val originalQuery = x.join(y, LeftOuter).limit(1) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(1, LocalLimit(1, y).join(y, LeftOuter)).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("right outer join") { | ||
| val originalQuery = x.join(y, RightOuter).limit(1) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(1, x.join(LocalLimit(1, y), RightOuter)).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("larger limits are not pushed on top of smaller ones in right outer join") { | ||
| val originalQuery = x.join(y.limit(5), RightOuter).limit(10) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(10, x.join(Limit(5, y), RightOuter)).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("full outer join where neither side is limited and both sides have same statistics") { | ||
| assert(x.statistics.sizeInBytes === y.statistics.sizeInBytes) | ||
| val originalQuery = x.join(y, FullOuter).limit(1) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(1, LocalLimit(1, x).join(y, FullOuter)).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("full outer join where neither side is limited and left side has larger statistics") { | ||
| val xBig = testRelation.copy(data = Seq.fill(2)(null)).subquery('x) | ||
| assert(xBig.statistics.sizeInBytes > y.statistics.sizeInBytes) | ||
| val originalQuery = xBig.join(y, FullOuter).limit(1) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(1, LocalLimit(1, xBig).join(y, FullOuter)).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("full outer join where neither side is limited and right side has larger statistics") { | ||
| val yBig = testRelation.copy(data = Seq.fill(2)(null)).subquery('y) | ||
| assert(x.statistics.sizeInBytes < yBig.statistics.sizeInBytes) | ||
| val originalQuery = x.join(yBig, FullOuter).limit(1) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(1, x.join(LocalLimit(1, yBig), FullOuter)).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("full outer join where both sides are limited") { | ||
| val originalQuery = x.limit(2).join(y.limit(2), FullOuter).limit(1) | ||
| val optimized = Optimize.execute(originalQuery.analyze) | ||
| val correctAnswer = Limit(1, Limit(2, x).join(Limit(2, y), FullOuter)).analyze | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
| } | ||
|
|
||
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.
I'll also add tests for the cases where both inputs are limited.
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.
I've added more tests now.