Skip to content

Commit 14560eb

Browse files
committed
Generalize pattern for planning hash joins.
1 parent f4809c1 commit 14560eb

File tree

2 files changed

+57
-35
lines changed

2 files changed

+57
-35
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ package org.apache.spark.sql.catalyst.planning
1919

2020
import scala.annotation.tailrec
2121

22+
import org.apache.spark.sql.Logging
23+
2224
import org.apache.spark.sql.catalyst.expressions._
25+
import org.apache.spark.sql.catalyst.plans._
2326
import org.apache.spark.sql.catalyst.plans.logical._
2427

2528
/**
@@ -101,6 +104,55 @@ object PhysicalOperation extends PredicateHelper {
101104
}
102105
}
103106

107+
/**
108+
* A pattern that finds joins with equality conditions that can be evaluated using hashing
109+
* techniques. For inner joins, any filters on top of the join operator are also matched.
110+
*/
111+
object HashFilteredJoin extends Logging with PredicateHelper {
112+
/** (joinType, rightKeys, leftKeys, condition, left, right) */
113+
type ReturnType =
114+
(JoinType, Seq[Expression], Seq[Expression], Option[Expression], LogicalPlan, LogicalPlan)
115+
116+
def unapply(plan: LogicalPlan): Option[ReturnType] = plan match {
117+
// All predicates can be evaluated for inner join (i.e., those that are in the ON
118+
// clause and WHERE clause.)
119+
case FilteredOperation(predicates, join @ Join(left, right, Inner, condition)) =>
120+
logger.debug(s"Considering hash inner join on: ${predicates ++ condition}")
121+
splitPredicates(predicates ++ condition, join)
122+
case join @ Join(left, right, joinType, condition) =>
123+
logger.debug(s"Considering hash join on: $condition")
124+
splitPredicates(condition.toSeq, join)
125+
case _ => None
126+
}
127+
128+
// Find equi-join predicates that can be evaluated before the join, and thus can be used
129+
// as join keys.
130+
def splitPredicates(allPredicates: Seq[Expression], join: Join): Option[ReturnType] = {
131+
val Join(left, right, joinType, _) = join
132+
val (joinPredicates, otherPredicates) = allPredicates.partition {
133+
case Equals(l, r) if (canEvaluate(l, left) && canEvaluate(r, right)) ||
134+
(canEvaluate(l, right) && canEvaluate(r, left)) => true
135+
case _ => false
136+
}
137+
138+
val joinKeys = joinPredicates.map {
139+
case Equals(l,r) if canEvaluate(l, left) && canEvaluate(r, right) => (l, r)
140+
case Equals(l,r) if canEvaluate(l, right) && canEvaluate(r, left) => (r, l)
141+
}
142+
143+
// Do not consider this strategy if there are no join keys.
144+
if (joinKeys.nonEmpty) {
145+
val leftKeys = joinKeys.map(_._1)
146+
val rightKeys = joinKeys.map(_._2)
147+
148+
Some((joinType, leftKeys, rightKeys, otherPredicates.reduceOption(And), left, right))
149+
} else {
150+
logger.debug(s"Avoiding hash join with no join keys.")
151+
None
152+
}
153+
}
154+
}
155+
104156
/**
105157
* A pattern that collects all adjacent unions and returns their children as a Seq.
106158
*/

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

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,12 @@ import org.apache.spark.sql.parquet._
2828
abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
2929
self: SQLContext#SparkPlanner =>
3030

31-
object HashJoin extends Strategy {
31+
object HashJoin extends Strategy with PredicateHelper {
3232
def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
33-
case FilteredOperation(predicates, logical.Join(left, right, Inner, condition)) =>
34-
logger.debug(s"Considering join: ${predicates ++ condition}")
35-
// Find equi-join predicates that can be evaluated before the join, and thus can be used
36-
// as join keys. Note we can only mix in the conditions with other predicates because the
37-
// match above ensures that this is and Inner join.
38-
val (joinPredicates, otherPredicates) = (predicates ++ condition).partition {
39-
case Equals(l, r) if (canEvaluate(l, left) && canEvaluate(r, right)) ||
40-
(canEvaluate(l, right) && canEvaluate(r, left)) => true
41-
case _ => false
42-
}
43-
44-
val joinKeys = joinPredicates.map {
45-
case Equals(l,r) if canEvaluate(l, left) && canEvaluate(r, right) => (l, r)
46-
case Equals(l,r) if canEvaluate(l, right) && canEvaluate(r, left) => (r, l)
47-
}
48-
49-
// Do not consider this strategy if there are no join keys.
50-
if (joinKeys.nonEmpty) {
51-
val leftKeys = joinKeys.map(_._1)
52-
val rightKeys = joinKeys.map(_._2)
53-
54-
val joinOp = execution.HashJoin(
55-
leftKeys, rightKeys, BuildRight, planLater(left), planLater(right))
56-
57-
// Make sure other conditions are met if present.
58-
if (otherPredicates.nonEmpty) {
59-
execution.Filter(combineConjunctivePredicates(otherPredicates), joinOp) :: Nil
60-
} else {
61-
joinOp :: Nil
62-
}
63-
} else {
64-
logger.debug(s"Avoiding spark join with no join keys.")
65-
Nil
66-
}
33+
case HashFilteredJoin(Inner, leftKeys, rightKeys, condition, left, right) =>
34+
val hashJoin =
35+
execution.HashJoin(leftKeys, rightKeys, BuildRight, planLater(left), planLater(right))
36+
condition.map(Filter(_, hashJoin)).getOrElse(hashJoin) :: Nil
6737
case _ => Nil
6838
}
6939
}

0 commit comments

Comments
 (0)