-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19122][SQL] Unnecessary shuffle+sort added if join predicates ordering differ from bucketing and sorting order #16985
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
tejasapatil
wants to merge
9
commits into
apache:master
from
tejasapatil:SPARK-19122_join_order_shuffle
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
217a843
[SPARK-19122][SQL] Unnecessary shuffle+sort added if join predicates …
tejasapatil d11fe32
resolve ordering of join keys everytime `requiredChildDistribution` i…
tejasapatil 060730c
minor refac
tejasapatil 213c273
use lazy val
tejasapatil 3f27ff2
always return result in test case
tejasapatil c36cd1a
create a physical optimizer rule which reorders join predicates based…
tejasapatil acd4dcb
checkstyle
tejasapatil b295093
new test case
tejasapatil 7171b58
removed checking for qury result in the test case
tejasapatil 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
94 changes: 94 additions & 0 deletions
94
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ReorderJoinPredicates.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,94 @@ | ||
| /* | ||
| * 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.joins | ||
|
|
||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.Expression | ||
| import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
|
|
||
| /** | ||
| * When the physical operators are created for JOIN, the ordering of join keys is based on order | ||
| * in which the join keys appear in the user query. That might not match with the output | ||
| * partitioning of the join node's children (thus leading to extra sort / shuffle being | ||
| * introduced). This rule will change the ordering of the join keys to match with the | ||
| * partitioning of the join nodes' children. | ||
| */ | ||
| class ReorderJoinPredicates extends Rule[SparkPlan] { | ||
| private def reorderJoinKeys( | ||
| leftKeys: Seq[Expression], | ||
| rightKeys: Seq[Expression], | ||
| leftPartitioning: Partitioning, | ||
| rightPartitioning: Partitioning): (Seq[Expression], Seq[Expression]) = { | ||
|
|
||
| def reorder( | ||
| expectedOrderOfKeys: Seq[Expression], | ||
| currentOrderOfKeys: Seq[Expression]): (Seq[Expression], Seq[Expression]) = { | ||
| val leftKeysBuffer = ArrayBuffer[Expression]() | ||
| val rightKeysBuffer = ArrayBuffer[Expression]() | ||
|
|
||
| expectedOrderOfKeys.foreach(expression => { | ||
| val index = currentOrderOfKeys.indexWhere(e => e.semanticEquals(expression)) | ||
| leftKeysBuffer.append(leftKeys(index)) | ||
| rightKeysBuffer.append(rightKeys(index)) | ||
| }) | ||
| (leftKeysBuffer, rightKeysBuffer) | ||
| } | ||
|
|
||
| if (leftKeys.forall(_.deterministic) && rightKeys.forall(_.deterministic)) { | ||
| leftPartitioning match { | ||
| case HashPartitioning(leftExpressions, _) | ||
| if leftExpressions.length == leftKeys.length && | ||
| leftKeys.forall(x => leftExpressions.exists(_.semanticEquals(x))) => | ||
| reorder(leftExpressions, leftKeys) | ||
|
|
||
| case _ => rightPartitioning match { | ||
| case HashPartitioning(rightExpressions, _) | ||
| if rightExpressions.length == rightKeys.length && | ||
| rightKeys.forall(x => rightExpressions.exists(_.semanticEquals(x))) => | ||
| reorder(rightExpressions, rightKeys) | ||
|
|
||
| case _ => (leftKeys, rightKeys) | ||
| } | ||
| } | ||
| } else { | ||
| (leftKeys, rightKeys) | ||
| } | ||
| } | ||
|
|
||
| def apply(plan: SparkPlan): SparkPlan = plan.transformUp { | ||
| case BroadcastHashJoinExec(leftKeys, rightKeys, joinType, buildSide, condition, left, right) => | ||
| val (reorderedLeftKeys, reorderedRightKeys) = | ||
| reorderJoinKeys(leftKeys, rightKeys, left.outputPartitioning, right.outputPartitioning) | ||
| BroadcastHashJoinExec(reorderedLeftKeys, reorderedRightKeys, joinType, buildSide, condition, | ||
| left, right) | ||
|
|
||
| case ShuffledHashJoinExec(leftKeys, rightKeys, joinType, buildSide, condition, left, right) => | ||
| val (reorderedLeftKeys, reorderedRightKeys) = | ||
| reorderJoinKeys(leftKeys, rightKeys, left.outputPartitioning, right.outputPartitioning) | ||
| ShuffledHashJoinExec(reorderedLeftKeys, reorderedRightKeys, joinType, buildSide, condition, | ||
| left, right) | ||
|
|
||
| case SortMergeJoinExec(leftKeys, rightKeys, joinType, condition, left, right) => | ||
| val (reorderedLeftKeys, reorderedRightKeys) = | ||
| reorderJoinKeys(leftKeys, rightKeys, left.outputPartitioning, right.outputPartitioning) | ||
| SortMergeJoinExec(reorderedLeftKeys, reorderedRightKeys, joinType, condition, left, right) | ||
| } | ||
| } | ||
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
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.
why do we need the same length? Let's say the child partitioning is
a, b, c, dand the join key isb, a, we can reorder the join key to avoid shuffle, right?Uh oh!
There was an error while loading. Please reload this page.
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 don't think that would be right thing to do. If child is partitioned on
a, b, c, d, its basically means rows are distributed over hash ofa, b, c, d. Lets say we have two rows with values ofa, b, c, das:If the join key
b,ais reordered asa,band we want to avoid shuffle, that would mean that we expect the child to have same values ofa,bin the same partition. But if you look at row1 and row2 above, even if values ofaandbare the same, there is no guarantee that they would belong to the same partition... as the partition is based on hash of alla,b,c,d.If the join keys are a subset of the partitioning, then there needs to be a shuffle to be done. There is only one exception to this (more of a corner case) : https://issues.apache.org/jira/browse/SPARK-18067
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.
oh sorry I made a mistake.
if the child partitioning is
a, band the join key isb, a, c, d, does it make sense to reorder it asa, b ,c ,d?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.
EnsureRequirementswould still add a shuffle in either case even if we reorder.JOIN would expect data to be distributed over
b, a, c, d(ora,b,c,dif you reorder) which maps to HashPartitioning(a,b,c,d) :spark/sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/EnsureRequirements.scala
Line 185 in e9c91ba
But the child nodes won't have matching partitioning ie. they will have HashPartitioning(
a,b).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.
The contract for reordering is that the set of join keys must be equal to the set of child's partitioning columns (implemented at L58-L59 in this file). Thus there won't be reordering for the case you pointed out. I have added a test case of the same.