-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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-22042] [SQL] ReorderJoinPredicates can break when child's partitioning is not decided #19257
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,14 @@ | |
|
||
package org.apache.spark.sql.execution.exchange | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
|
||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.plans.physical._ | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.execution._ | ||
import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, ShuffledHashJoinExec, | ||
SortMergeJoinExec} | ||
import org.apache.spark.sql.internal.SQLConf | ||
|
||
/** | ||
|
@@ -248,13 +252,83 @@ case class EnsureRequirements(conf: SQLConf) extends Rule[SparkPlan] { | |
operator.withNewChildren(children) | ||
} | ||
|
||
/** | ||
* 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. | ||
*/ | ||
def reorderJoinPredicates(plan: SparkPlan): SparkPlan = { | ||
def reorderJoinKeys( | ||
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. We do not prefer the embedded function. |
||
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]) = { | ||
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. indents. |
||
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) | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
def apply(plan: SparkPlan): SparkPlan = plan.transformUp { | ||
case operator @ ShuffleExchangeExec(partitioning, child, _) => | ||
child.children match { | ||
case ShuffleExchangeExec(childPartitioning, baseChild, _)::Nil => | ||
if (childPartitioning.guarantees(partitioning)) child else operator | ||
case _ => operator | ||
} | ||
case operator: SparkPlan => ensureDistributionAndOrdering(operator) | ||
case operator: SparkPlan => | ||
ensureDistributionAndOrdering(reorderJoinPredicates(operator)) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -602,6 +602,37 @@ abstract class BucketedReadSuite extends QueryTest with SQLTestUtils { | |
) | ||
} | ||
|
||
test("SPARK-22042 ReorderJoinPredicates can break when child's partitioning is not decided") { | ||
withTable("bucketed_table", "table1", "table2") { | ||
df.write.format("parquet").saveAsTable("table1") | ||
df.write.format("parquet").saveAsTable("table2") | ||
df.write.format("parquet").bucketBy(8, "j", "k").saveAsTable("bucketed_table") | ||
|
||
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "0") { | ||
checkAnswer( | ||
sql(""" | ||
|SELECT ab.i, ab.j, ab.k, c.i, c.j, c.k | ||
|FROM ( | ||
| SELECT a.i, a.j, a.k | ||
| FROM bucketed_table a | ||
| JOIN table1 b | ||
| ON a.i = b.i | ||
|) ab | ||
|JOIN table2 c | ||
|ON ab.i = c.i | ||
|""".stripMargin), | ||
sql(""" | ||
|SELECT a.i, a.j, a.k, c.i, c.j, c.k | ||
|FROM bucketed_table a | ||
|JOIN table1 b | ||
|ON a.i = b.i | ||
|JOIN table2 c | ||
|ON a.i = c.i | ||
|""".stripMargin)) | ||
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. Please follow the other test cases.
|
||
} | ||
} | ||
} | ||
|
||
test("error if there exists any malformed bucket files") { | ||
withTable("bucketed_table") { | ||
df1.write.format("parquet").bucketBy(8, "i").saveAsTable("bucketed_table") | ||
|
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.
private