Skip to content

Commit

Permalink
Nong's comment
Browse files Browse the repository at this point in the history
  • Loading branch information
sameeragarwal committed Mar 8, 2016
1 parent 7441386 commit d3c4739
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ abstract class GenericStrategy[PhysicalPlan <: TreeNode[PhysicalPlan]]

// Attempts to re-order the individual conjunctive predicates in an expression to short circuit
// the evaluation of relatively cheaper checks (e.g., checking for nullability) before others.
protected def getReorderedExpression(expr: Expression): Expression = {
protected def reorderPredicates(expr: Expression): Expression = {
splitConjunctivePredicates(expr)
.sortWith((x, _) => x.isInstanceOf[IsNotNull])
.reduce(And)
}

// Wrapper around getReorderedExpression(expr: Expression) to reorder optional conditions in joins
protected def getReorderedExpression(exprOpt: Option[Expression]): Option[Expression] = {
// Wrapper around reorderPredicates(expr: Expression) to reorder optional conditions in joins
protected def reorderPredicates(exprOpt: Option[Expression]): Option[Expression] = {
exprOpt match {
case Some(expr) =>
Option(getReorderedExpression(expr))
Option(reorderPredicates(expr))
case None =>
exprOpt
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
LeftSemi, leftKeys, rightKeys, condition, left, CanBroadcast(right)) =>
joins.BroadcastLeftSemiJoinHash(
leftKeys, rightKeys, planLater(left), planLater(right),
getReorderedExpression(condition)) :: Nil
reorderPredicates(condition)) :: Nil
// Find left semi joins where at least some predicates can be evaluated by matching join keys
case ExtractEquiJoinKeys(LeftSemi, leftKeys, rightKeys, condition, left, right) =>
joins.LeftSemiJoinHash(
leftKeys, rightKeys, planLater(left), planLater(right),
getReorderedExpression(condition)) :: Nil
reorderPredicates(condition)) :: Nil
case _ => Nil
}
}
Expand Down Expand Up @@ -113,38 +113,38 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {

case ExtractEquiJoinKeys(Inner, leftKeys, rightKeys, condition, left, CanBroadcast(right)) =>
Seq(joins.BroadcastHashJoin(
leftKeys, rightKeys, Inner, BuildRight, getReorderedExpression(condition),
leftKeys, rightKeys, Inner, BuildRight, reorderPredicates(condition),
planLater(left), planLater(right)))

case ExtractEquiJoinKeys(Inner, leftKeys, rightKeys, condition, CanBroadcast(left), right) =>
Seq(joins.BroadcastHashJoin(
leftKeys, rightKeys, Inner, BuildLeft, getReorderedExpression(condition), planLater(left),
leftKeys, rightKeys, Inner, BuildLeft, reorderPredicates(condition), planLater(left),
planLater(right)))

case ExtractEquiJoinKeys(Inner, leftKeys, rightKeys, condition, left, right)
if RowOrdering.isOrderable(leftKeys) =>
joins.SortMergeJoin(
leftKeys, rightKeys, getReorderedExpression(condition), planLater(left),
leftKeys, rightKeys, reorderPredicates(condition), planLater(left),
planLater(right)) :: Nil

// --- Outer joins --------------------------------------------------------------------------

case ExtractEquiJoinKeys(
LeftOuter, leftKeys, rightKeys, condition, left, CanBroadcast(right)) =>
Seq(joins.BroadcastHashJoin(
leftKeys, rightKeys, LeftOuter, BuildRight, getReorderedExpression(condition),
leftKeys, rightKeys, LeftOuter, BuildRight, reorderPredicates(condition),
planLater(left), planLater(right)))

case ExtractEquiJoinKeys(
RightOuter, leftKeys, rightKeys, condition, CanBroadcast(left), right) =>
Seq(joins.BroadcastHashJoin(
leftKeys, rightKeys, RightOuter, BuildLeft, getReorderedExpression(condition),
leftKeys, rightKeys, RightOuter, BuildLeft, reorderPredicates(condition),
planLater(left), planLater(right)))

case ExtractEquiJoinKeys(joinType, leftKeys, rightKeys, condition, left, right)
if RowOrdering.isOrderable(leftKeys) =>
joins.SortMergeOuterJoin(
leftKeys, rightKeys, joinType, getReorderedExpression(condition), planLater(left),
leftKeys, rightKeys, joinType, reorderPredicates(condition), planLater(left),
planLater(right)) :: Nil

// --- Cases where this strategy does not apply ---------------------------------------------
Expand Down Expand Up @@ -261,11 +261,11 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
case j @ logical.Join(CanBroadcast(left), right, Inner | RightOuter, condition) =>
execution.joins.BroadcastNestedLoopJoin(
planLater(left), planLater(right), joins.BuildLeft, j.joinType,
getReorderedExpression(condition)) :: Nil
reorderPredicates(condition)) :: Nil
case j @ logical.Join(left, CanBroadcast(right), Inner | LeftOuter | LeftSemi, condition) =>
execution.joins.BroadcastNestedLoopJoin(
planLater(left), planLater(right), joins.BuildRight, j.joinType,
getReorderedExpression(condition)) :: Nil
reorderPredicates(condition)) :: Nil
case _ => Nil
}
}
Expand All @@ -275,7 +275,7 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
case logical.Join(left, right, Inner, None) =>
execution.joins.CartesianProduct(planLater(left), planLater(right)) :: Nil
case logical.Join(left, right, Inner, Some(condition)) =>
execution.Filter(getReorderedExpression(condition),
execution.Filter(reorderPredicates(condition),
execution.joins.CartesianProduct(planLater(left), planLater(right))) :: Nil
case _ => Nil
}
Expand All @@ -293,7 +293,7 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
// This join could be very slow or even hang forever
joins.BroadcastNestedLoopJoin(
planLater(left), planLater(right), buildSide, joinType,
getReorderedExpression(condition)) :: Nil
reorderPredicates(condition)) :: Nil
case _ => Nil
}
}
Expand Down Expand Up @@ -352,7 +352,7 @@ private[sql] abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
case logical.Project(projectList, child) =>
execution.Project(projectList, planLater(child)) :: Nil
case logical.Filter(condition, child) =>
execution.Filter(getReorderedExpression(condition), planLater(child)) :: Nil
execution.Filter(reorderPredicates(condition), planLater(child)) :: Nil
case e @ logical.Expand(_, _, child) =>
execution.Expand(e.projections, e.output, planLater(child)) :: Nil
case logical.Window(projectList, windowExprs, partitionSpec, orderSpec, child) =>
Expand Down

0 comments on commit d3c4739

Please sign in to comment.