Skip to content

Commit 8187b88

Browse files
committed
[SPARK-8300] DataFrame hint for broadcast join.
1 parent 44fa7df commit 8187b88

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/QueryPlanner.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.spark.sql.catalyst.planning
1919

2020
import org.apache.spark.Logging
21-
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
21+
import org.apache.spark.sql.catalyst.plans.logical.{BroadcastHint, LogicalPlan}
2222
import org.apache.spark.sql.catalyst.trees.TreeNode
2323

2424
/**
@@ -51,7 +51,10 @@ abstract class QueryPlanner[PhysicalPlan <: TreeNode[PhysicalPlan]] {
5151
* filled in automatically by the QueryPlanner using the other execution strategies that are
5252
* available.
5353
*/
54-
protected def planLater(plan: LogicalPlan) = this.plan(plan).next()
54+
protected def planLater(plan: LogicalPlan) = plan match {
55+
case BroadcastHint(child) => this.plan(child).next()
56+
case _ => this.plan(plan).next()
57+
}
5558

5659
def plan(plan: LogicalPlan): Iterator[PhysicalPlan] = {
5760
// Obviously a lot to do here still...

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicOperators.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ case class Join(
130130
}
131131
}
132132

133+
/**
134+
* A hint for the optimizer that we should broadcast the `child` if used in a join operator.
135+
*/
136+
case class BroadcastHint(child: LogicalPlan) extends UnaryNode {
137+
override def output: Seq[Attribute] = child.output
138+
}
139+
140+
133141
case class Except(left: LogicalPlan, right: LogicalPlan) extends BinaryNode {
134142
override def output: Seq[Attribute] = left.output
135143
}

sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package org.apache.spark.sql.execution
2020
import org.apache.spark.sql.catalyst.expressions._
2121
import org.apache.spark.sql.catalyst.planning._
2222
import org.apache.spark.sql.catalyst.plans._
23-
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
23+
import org.apache.spark.sql.catalyst.plans.logical.{BroadcastHint, LogicalPlan}
2424
import org.apache.spark.sql.catalyst.plans.physical._
2525
import org.apache.spark.sql.columnar.{InMemoryColumnarTableScan, InMemoryRelation}
2626
import org.apache.spark.sql.execution.{DescribeCommand => RunnableDescribeCommand}
@@ -80,6 +80,12 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
8080
}
8181

8282
def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
83+
case ExtractEquiJoinKeys(Inner, leftKeys, rightKeys, condition, left, BroadcastHint(right)) =>
84+
makeBroadcastHashJoin(leftKeys, rightKeys, left, right, condition, joins.BuildRight)
85+
86+
case ExtractEquiJoinKeys(Inner, leftKeys, rightKeys, condition, BroadcastHint(left), right) =>
87+
makeBroadcastHashJoin(leftKeys, rightKeys, left, right, condition, joins.BuildLeft)
88+
8389
case ExtractEquiJoinKeys(Inner, leftKeys, rightKeys, condition, left, right)
8490
if sqlContext.conf.autoBroadcastJoinThreshold > 0 &&
8591
right.statistics.sizeInBytes <= sqlContext.conf.autoBroadcastJoinThreshold =>
@@ -329,6 +335,7 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
329335
case e @ EvaluatePython(udf, child, _) =>
330336
BatchPythonEvaluation(udf, e.output, planLater(child)) :: Nil
331337
case LogicalRDD(output, rdd) => PhysicalRDD(output, rdd) :: Nil
338+
case BroadcastHint(child) => apply(child)
332339
case _ => Nil
333340
}
334341
}

sql/core/src/main/scala/org/apache/spark/sql/functions.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.spark.sql
1919

20+
import org.apache.spark.sql.catalyst.plans.logical.BroadcastHint
21+
2022
import scala.language.implicitConversions
2123
import scala.reflect.runtime.universe.{TypeTag, typeTag}
2224

@@ -565,6 +567,22 @@ object functions {
565567
array((colName +: colNames).map(col) : _*)
566568
}
567569

570+
/**
571+
* Marks a DataFrame as small enough for use in broadcast joins.
572+
*
573+
* The following example marks the right DataFrame for broadcast hash join using `joinKey`.
574+
* {{{
575+
* // left and right are DataFrames
576+
* left.join(broadcast(right), "joinKey")
577+
* }}}
578+
*
579+
* @group normal_funcs
580+
* @since 1.5.0
581+
*/
582+
def broadcast(df: DataFrame): DataFrame = {
583+
DataFrame(df.sqlContext, BroadcastHint(df.logicalPlan))
584+
}
585+
568586
/**
569587
* Returns the first column that is not null.
570588
* {{{

sql/core/src/test/scala/org/apache/spark/sql/DataFrameJoinSuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.spark.sql
1919

2020
import org.apache.spark.sql.TestData._
21+
import org.apache.spark.sql.execution.joins.BroadcastHashJoin
2122
import org.apache.spark.sql.functions._
2223

2324
class DataFrameJoinSuite extends QueryTest {
@@ -93,4 +94,20 @@ class DataFrameJoinSuite extends QueryTest {
9394
left.join(right, left("key") === right("key")),
9495
Row(1, 1, 1, 1) :: Row(2, 1, 2, 2) :: Nil)
9596
}
97+
98+
test("broadcast join hint") {
99+
val df1 = Seq((1, "1"), (2, "2")).toDF("key", "value")
100+
val df2 = Seq((1, "1"), (2, "2")).toDF("key", "value")
101+
102+
// equijoin - should be converted into broadcast join
103+
val plan1 = df1.join(broadcast(df2), "key").queryExecution.executedPlan
104+
assert(plan1.collect { case p: BroadcastHashJoin => p }.size === 1)
105+
106+
// no join key -- should not be a broadcast join
107+
val plan2 = df1.join(broadcast(df2)).queryExecution.executedPlan
108+
assert(plan2.collect { case p: BroadcastHashJoin => p }.size === 0)
109+
110+
// planner should not crash without a join
111+
broadcast(df1).queryExecution.executedPlan
112+
}
96113
}

0 commit comments

Comments
 (0)