Skip to content

[SPARK-32383][SQL] Preserve hash join (BHJ and SHJ) stream side ordering #29181

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,41 @@ trait HashJoin extends BaseJoinExec {
}
}

override def outputPartitioning: Partitioning = streamedPlan.outputPartitioning
override def outputPartitioning: Partitioning = buildSide match {
case BuildLeft =>
joinType match {
case _: InnerLike | RightOuter => right.outputPartitioning
case x =>
throw new IllegalArgumentException(
s"HashJoin should not take $x as the JoinType with building left side")
}
case BuildRight =>
joinType match {
case _: InnerLike | LeftOuter | LeftSemi | LeftAnti | _: ExistenceJoin =>
left.outputPartitioning
case x =>
throw new IllegalArgumentException(
s"HashJoin should not take $x as the JoinType with building right side")
}
}

override def outputOrdering: Seq[SortOrder] = buildSide match {
case BuildLeft =>
joinType match {
case _: InnerLike | RightOuter => right.outputOrdering
case x =>
throw new IllegalArgumentException(
s"HashJoin should not take $x as the JoinType with building left side")
}
case BuildRight =>
joinType match {
case _: InnerLike | LeftOuter | LeftSemi | LeftAnti | _: ExistenceJoin =>
left.outputOrdering
case x =>
throw new IllegalArgumentException(
s"HashJoin should not take $x as the JoinType with building right side")
}
}

protected lazy val (buildPlan, streamedPlan) = buildSide match {
case BuildLeft => (left, right)
Expand Down
43 changes: 43 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1104,4 +1104,47 @@ class JoinSuite extends QueryTest with SharedSparkSession with AdaptiveSparkPlan
})
}
}

test("SPARK-32383: Preserve hash join (BHJ and SHJ) stream side ordering") {
val df1 = spark.range(100).select($"id".as("k1"))
val df2 = spark.range(100).select($"id".as("k2"))
val df3 = spark.range(3).select($"id".as("k3"))
val df4 = spark.range(100).select($"id".as("k4"))

// Test broadcast hash join
withSQLConf(
SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "50") {
Seq("inner", "left_outer").foreach(joinType => {
val plan = df1.join(df2, $"k1" === $"k2", joinType)
.join(df3, $"k1" === $"k3", joinType)
.join(df4, $"k1" === $"k4", joinType)
.queryExecution
.executedPlan
assert(plan.collect { case _: SortMergeJoinExec => true }.size === 2)
assert(plan.collect { case _: BroadcastHashJoinExec => true }.size === 1)
// No extra sort before last sort merge join
assert(plan.collect { case _: SortExec => true }.size === 3)
})
}

// Test shuffled hash join
withSQLConf(
SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "50",
SQLConf.SHUFFLE_PARTITIONS.key -> "2",
SQLConf.PREFER_SORTMERGEJOIN.key -> "false") {
val df3 = spark.range(10).select($"id".as("k3"))

Seq("inner", "left_outer").foreach(joinType => {
val plan = df1.join(df2, $"k1" === $"k2", joinType)
.join(df3, $"k1" === $"k3", joinType)
.join(df4, $"k1" === $"k4", joinType)
.queryExecution
.executedPlan
assert(plan.collect { case _: SortMergeJoinExec => true }.size === 2)
assert(plan.collect { case _: ShuffledHashJoinExec => true }.size === 1)
// No extra sort before last sort merge join
assert(plan.collect { case _: SortExec => true }.size === 3)
})
}
}
}