Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -51,7 +51,7 @@ object CostBasedJoinReorder extends Rule[LogicalPlan] with PredicateHelper {
}
// After reordering is finished, convert OrderedJoin back to Join.
result transform {
case OrderedJoin(left, right, jt, cond) => Join(left, right, jt, cond, JoinHint.NONE)
case OrderedJoin(left, right, jt, cond, hint) => Join(left, right, jt, cond, hint)
}
}
}
Expand All @@ -77,24 +77,24 @@ object CostBasedJoinReorder extends Rule[LogicalPlan] with PredicateHelper {
*/
private def extractInnerJoins(plan: LogicalPlan): (Seq[LogicalPlan], Set[Expression]) = {
plan match {
case Join(left, right, _: InnerLike, Some(cond), _) =>
case Join(left, right, _: InnerLike, Some(cond), hint) if hint == JoinHint.NONE =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use pattern match as @dongjoon-hyun's PR, it will be simpler.

val (leftPlans, leftConditions) = extractInnerJoins(left)
val (rightPlans, rightConditions) = extractInnerJoins(right)
(leftPlans ++ rightPlans, splitConjunctivePredicates(cond).toSet ++
leftConditions ++ rightConditions)
case Project(projectList, j @ Join(_, _, _: InnerLike, Some(cond), _))
if projectList.forall(_.isInstanceOf[Attribute]) =>
case Project(projectList, j @ Join(_, _, _: InnerLike, Some(cond), hint))
if projectList.forall(_.isInstanceOf[Attribute]) && hint == JoinHint.NONE =>
extractInnerJoins(j)
case _ =>
(Seq(plan), Set())
}
}

private def replaceWithOrderedJoin(plan: LogicalPlan): LogicalPlan = plan match {
case j @ Join(left, right, jt: InnerLike, Some(cond), _) =>
case j @ Join(left, right, jt: InnerLike, Some(cond), hint) =>
val replacedLeft = replaceWithOrderedJoin(left)
val replacedRight = replaceWithOrderedJoin(right)
OrderedJoin(replacedLeft, replacedRight, jt, Some(cond))
OrderedJoin(replacedLeft, replacedRight, jt, Some(cond), hint)
case p @ Project(projectList, j @ Join(_, _, _: InnerLike, Some(cond), _)) =>
p.copy(child = replaceWithOrderedJoin(j))
case _ =>
Expand All @@ -107,7 +107,8 @@ case class OrderedJoin(
left: LogicalPlan,
right: LogicalPlan,
joinType: JoinType,
condition: Option[Expression]) extends BinaryNode {
condition: Option[Expression],
hint: JoinHint) extends BinaryNode {
override def output: Seq[Attribute] = left.output ++ right.output
}

Expand Down
92 changes: 47 additions & 45 deletions sql/core/src/test/scala/org/apache/spark/sql/JoinHintSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,58 +102,60 @@ class JoinHintSuite extends PlanTest with SharedSQLContext {
}

test("hints prevent join reorder") {
withTempView("a", "b", "c") {
df1.createOrReplaceTempView("a")
df2.createOrReplaceTempView("b")
df3.createOrReplaceTempView("c")
verifyJoinHint(
sql("select /*+ broadcast(a, c)*/ * from a, b, c " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint(
None,
Some(HintInfo(broadcast = true))) ::
JoinHint(
Some(HintInfo(broadcast = true)),
None):: Nil
)
verifyJoinHint(
sql("select /*+ broadcast(a, c)*/ * from a, c, b " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint.NONE ::
withSQLConf(SQLConf.CBO_ENABLED.key -> "true", SQLConf.JOIN_REORDER_ENABLED.key -> "true") {
withTempView("a", "b", "c") {
df1.createOrReplaceTempView("a")
df2.createOrReplaceTempView("b")
df3.createOrReplaceTempView("c")
verifyJoinHint(
sql("select /*+ broadcast(a, c)*/ * from a, b, c " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint(
Some(HintInfo(broadcast = true)),
Some(HintInfo(broadcast = true))):: Nil
)
verifyJoinHint(
sql("select /*+ broadcast(b, c)*/ * from a, c, b " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint(
None,
Some(HintInfo(broadcast = true))) ::
None,
Some(HintInfo(broadcast = true))) ::
JoinHint(
Some(HintInfo(broadcast = true)),
None) :: Nil
)
verifyJoinHint(
sql("select /*+ broadcast(a, c)*/ * from a, c, b " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint.NONE ::
JoinHint(
Some(HintInfo(broadcast = true)),
Some(HintInfo(broadcast = true))) :: Nil
)
verifyJoinHint(
sql("select /*+ broadcast(b, c)*/ * from a, c, b " +
"where a.a1 = b.b1 and b.b1 = c.c1"),
JoinHint(
None,
Some(HintInfo(broadcast = true))):: Nil
)

verifyJoinHint(
df1.join(df2, 'a1 === 'b1 && 'a1 > 5).hint("broadcast")
.join(df3, 'b1 === 'c1 && 'a1 < 10),
JoinHint(
Some(HintInfo(broadcast = true)),
None) ::
JoinHint.NONE:: Nil
)
Some(HintInfo(broadcast = true))) ::
JoinHint(
None,
Some(HintInfo(broadcast = true))) :: Nil
)

verifyJoinHint(
df1.join(df2, 'a1 === 'b1 && 'a1 > 5).hint("broadcast")
.join(df3, 'b1 === 'c1 && 'a1 < 10)
.join(df, 'b1 === 'id),
JoinHint.NONE ::
verifyJoinHint(
df1.join(df2, 'a1 === 'b1 && 'a1 > 5).hint("broadcast")
.join(df3, 'b1 === 'c1 && 'a1 < 10),
JoinHint(
Some(HintInfo(broadcast = true)),
None) ::
JoinHint.NONE:: Nil
)
JoinHint.NONE :: Nil
)

verifyJoinHint(
df1.join(df2, 'a1 === 'b1 && 'a1 > 5).hint("broadcast")
.join(df3, 'b1 === 'c1 && 'a1 < 10)
.join(df, 'b1 === 'id),
JoinHint.NONE ::
JoinHint(
Some(HintInfo(broadcast = true)),
None) ::
JoinHint.NONE :: Nil
)
}
}
}

Expand Down