-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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-32282][SQL] Improve EnsureRquirement.reorderJoinKeys to handle more scenarios such as PartitioningCollection #29074
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a0366f2
initial commit
imback82 99493e4
update comments
imback82 ab237bc
Merge branch 'master' into reorder_keys
imback82 8308649
Fix merge error
imback82 268326b
Merge branch 'master' into reorder_keys
imback82 d91bcdd
Merge branch 'master' into reorder_keys
imback82 e5b078f
address PR comments
imback82 89ad6ef
address comments
imback82 fa3aafa
Merge branch 'master' into reorder_keys
imback82 1729c8b
Address PR comment
imback82 e2f7e44
Merge branch 'master' into reorder_keys
imback82 10b4d5a
Address PR comments
imback82 3cd6df9
Address PR comments
imback82 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
122 changes: 122 additions & 0 deletions
122
...core/src/test/scala/org/apache/spark/sql/execution/exchange/EnsureRequirementsSuite.scala
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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.execution.exchange | ||
|
||
import org.apache.spark.sql.catalyst.expressions.Literal | ||
import org.apache.spark.sql.catalyst.plans.Inner | ||
import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, PartitioningCollection} | ||
import org.apache.spark.sql.execution.{DummySparkPlan, SortExec} | ||
import org.apache.spark.sql.execution.joins.SortMergeJoinExec | ||
import org.apache.spark.sql.test.SharedSparkSession | ||
|
||
class EnsureRequirementsSuite extends SharedSparkSession { | ||
private val exprA = Literal(1) | ||
private val exprB = Literal(2) | ||
private val exprC = Literal(3) | ||
|
||
test("reorder should handle PartitioningCollection") { | ||
val plan1 = DummySparkPlan( | ||
outputPartitioning = PartitioningCollection(Seq( | ||
HashPartitioning(exprA :: exprB :: Nil, 5), | ||
HashPartitioning(exprA :: Nil, 5)))) | ||
val plan2 = DummySparkPlan() | ||
|
||
// Test PartitioningCollection on the left side of join. | ||
val smjExec1 = SortMergeJoinExec( | ||
exprB :: exprA :: Nil, exprA :: exprB :: Nil, Inner, None, plan1, plan2) | ||
EnsureRequirements(spark.sessionState.conf).apply(smjExec1) match { | ||
case SortMergeJoinExec(leftKeys, rightKeys, _, _, | ||
SortExec(_, _, DummySparkPlan(_, _, _: PartitioningCollection, _, _), _), | ||
SortExec(_, _, ShuffleExchangeExec(_: HashPartitioning, _, _), _), _) => | ||
assert(leftKeys === Seq(exprA, exprB)) | ||
assert(rightKeys === Seq(exprB, exprA)) | ||
case other => fail(other.toString) | ||
} | ||
|
||
// Test PartitioningCollection on the right side of join. | ||
val smjExec2 = SortMergeJoinExec( | ||
exprA :: exprB :: Nil, exprB :: exprA :: Nil, Inner, None, plan2, plan1) | ||
EnsureRequirements(spark.sessionState.conf).apply(smjExec2) match { | ||
case SortMergeJoinExec(leftKeys, rightKeys, _, _, | ||
SortExec(_, _, ShuffleExchangeExec(_: HashPartitioning, _, _), _), | ||
SortExec(_, _, DummySparkPlan(_, _, _: PartitioningCollection, _, _), _), _) => | ||
assert(leftKeys === Seq(exprB, exprA)) | ||
assert(rightKeys === Seq(exprA, exprB)) | ||
case other => fail(other.toString) | ||
} | ||
|
||
// Both sides are PartitioningCollection, but left side cannot be reorderd to match | ||
// and it should fall back to the right side. | ||
val smjExec3 = SortMergeJoinExec( | ||
exprA :: exprC :: Nil, exprB :: exprA :: Nil, Inner, None, plan1, plan1) | ||
EnsureRequirements(spark.sessionState.conf).apply(smjExec3) match { | ||
case SortMergeJoinExec(leftKeys, rightKeys, _, _, | ||
SortExec(_, _, ShuffleExchangeExec(_: HashPartitioning, _, _), _), | ||
SortExec(_, _, DummySparkPlan(_, _, _: PartitioningCollection, _, _), _), _) => | ||
assert(leftKeys === Seq(exprC, exprA)) | ||
assert(rightKeys === Seq(exprA, exprB)) | ||
case other => fail(other.toString) | ||
} | ||
} | ||
|
||
test("reorder should fallback to the other side partitioning") { | ||
val plan1 = DummySparkPlan( | ||
outputPartitioning = HashPartitioning(exprA :: exprB :: exprC :: Nil, 5)) | ||
val plan2 = DummySparkPlan( | ||
outputPartitioning = HashPartitioning(exprB :: exprC :: Nil, 5)) | ||
|
||
// Test fallback to the right side, which has HashPartitioning. | ||
val smjExec1 = SortMergeJoinExec( | ||
exprA :: exprB :: Nil, exprC :: exprB :: Nil, Inner, None, plan1, plan2) | ||
EnsureRequirements(spark.sessionState.conf).apply(smjExec1) match { | ||
case SortMergeJoinExec(leftKeys, rightKeys, _, _, | ||
SortExec(_, _, ShuffleExchangeExec(_: HashPartitioning, _, _), _), | ||
SortExec(_, _, DummySparkPlan(_, _, _: HashPartitioning, _, _), _), _) => | ||
assert(leftKeys === Seq(exprB, exprA)) | ||
assert(rightKeys === Seq(exprB, exprC)) | ||
case other => fail(other.toString) | ||
} | ||
|
||
// Test fallback to the right side, which has PartitioningCollection. | ||
val plan3 = DummySparkPlan( | ||
outputPartitioning = PartitioningCollection(Seq(HashPartitioning(exprB :: exprC :: Nil, 5)))) | ||
val smjExec2 = SortMergeJoinExec( | ||
exprA :: exprB :: Nil, exprC :: exprB :: Nil, Inner, None, plan1, plan3) | ||
EnsureRequirements(spark.sessionState.conf).apply(smjExec2) match { | ||
case SortMergeJoinExec(leftKeys, rightKeys, _, _, | ||
SortExec(_, _, ShuffleExchangeExec(_: HashPartitioning, _, _), _), | ||
SortExec(_, _, DummySparkPlan(_, _, _: PartitioningCollection, _, _), _), _) => | ||
assert(leftKeys === Seq(exprB, exprA)) | ||
assert(rightKeys === Seq(exprB, exprC)) | ||
case other => fail(other.toString) | ||
} | ||
|
||
// The right side has HashPartitioning, so it is matched first, but no reordering match is | ||
// found, and it should fall back to the left side, which has a PartitioningCollection. | ||
val smjExec3 = SortMergeJoinExec( | ||
exprC :: exprB :: Nil, exprA :: exprB :: Nil, Inner, None, plan3, plan1) | ||
EnsureRequirements(spark.sessionState.conf).apply(smjExec3) match { | ||
case SortMergeJoinExec(leftKeys, rightKeys, _, _, | ||
SortExec(_, _, DummySparkPlan(_, _, _: PartitioningCollection, _, _), _), | ||
SortExec(_, _, ShuffleExchangeExec(_: HashPartitioning, _, _), _), _) => | ||
assert(leftKeys === Seq(exprB, exprC)) | ||
assert(rightKeys === Seq(exprB, exprA)) | ||
case other => fail(other.toString) | ||
} | ||
} | ||
} |
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.
This can be also implemented by looking at left partitioning first then move to the right partitionoing:
However, I chose this way so that the behavior remains the same. If you have
leftPartitioning = PartitioningCollection
andrightPartitioning = HashPartitioning
, it will match therightPartitioning
first, which is the existing behavior.