Skip to content
This repository was archived by the owner on Jan 9, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
999ec13
[SPARK-22570][SQL] Avoid to create a lot of global variables by using…
kiszk Nov 30, 2017
6ac57fd
[SPARK-21417][SQL] Infer join conditions using propagated constraints
Nov 30, 2017
bcceab6
[SPARK-22489][SQL] Shouldn't change broadcast join buildSide if user …
wangyum Nov 30, 2017
f5f8e84
[SPARK-22614] Dataset API: repartitionByRange(...)
adrian-ionescu Nov 30, 2017
7e5f669
[SPARK-22428][DOC] Add spark application garbage collector configurat…
gaborgsomogyi Dec 1, 2017
7da1f57
[SPARK-22373] Bump Janino dependency version to fix thread safety issue…
Victsm Dec 1, 2017
dc36542
[SPARK-22653] executorAddress registered in CoarseGrainedSchedulerBac…
tgravescs Dec 1, 2017
16adaf6
[SPARK-22601][SQL] Data load is getting displayed successful on provi…
sujith71955 Dec 1, 2017
9d06a9e
[SPARK-22393][SPARK-SHELL] spark-shell can't find imported types in c…
mpetruska Dec 1, 2017
ee10ca7
[SPARK-22638][SS] Use a separate queue for StreamingQueryListenerBus
zsxwing Dec 1, 2017
aa4cf2b
[SPARK-22651][PYTHON][ML] Prevent initiating multiple Hive clients fo…
HyukjinKwon Dec 2, 2017
d2cf95a
[SPARK-22634][BUILD] Update Bouncy Castle to 1.58
srowen Dec 2, 2017
f23dddf
[SPARK-20682][SPARK-15474][SPARK-21791] Add new ORCFileFormat based o…
dongjoon-hyun Dec 3, 2017
2c16267
[SPARK-22669][SQL] Avoid unnecessary function calls in code generation
mgaido91 Dec 3, 2017
dff440f
[SPARK-22626][SQL] deals with wrong Hive's statistics (zero rowCount)
wangyum Dec 3, 2017
4131ad0
[SPARK-22489][DOC][FOLLOWUP] Update broadcast behavior changes in mig…
wangyum Dec 4, 2017
3927bb9
[SPARK-22473][FOLLOWUP][TEST] Remove deprecated Date functions
mgaido91 Dec 4, 2017
f81401e
[SPARK-22162] Executors and the driver should use consistent JobIDs i…
Dec 4, 2017
e1dd03e
[SPARK-22372][CORE, YARN] Make cluster submission use SparkApplication.
Dec 4, 2017
dcaac45
Spark on Kubernetes - basic submission client
liyinan926 Nov 10, 2017
27c67ff
Addressed first round of review comments
liyinan926 Nov 27, 2017
6d597d0
Made Client implement the SparkApplication trait
liyinan926 Nov 28, 2017
5b9fa39
Addressed the second round of comments
liyinan926 Nov 28, 2017
5ccadb5
Added missing step for supporting local:// dependencies and addressed…
liyinan926 Nov 30, 2017
12f2797
Fixed Scala style check errors
liyinan926 Nov 30, 2017
c35fe48
Addressed another round of comments
liyinan926 Dec 4, 2017
faa2849
Rebased on master and added a constant val for the Client class
liyinan926 Dec 4, 2017
347ed69
Addressed another major round of comments
liyinan926 Dec 5, 2017
0e8ca01
Addressed one more round of comments
liyinan926 Dec 5, 2017
3a0b8e3
Removed mentioning of kubernetes-namespace
liyinan926 Dec 6, 2017
83d0b9c
Fixed a couple of bugs found during manual tests
liyinan926 Dec 7, 2017
44c40b1
Guard against client mode in SparkContext
liyinan926 Dec 8, 2017
67bc847
Added libc6-compat into the base docker image
liyinan926 Dec 8, 2017
7d2b303
Addressed latest comments
liyinan926 Dec 8, 2017
caf2206
Addressed docs comments
liyinan926 Dec 9, 2017
2e7810b
Fixed a comment
liyinan926 Dec 11, 2017
cbcd30e
Addressed latest comments
liyinan926 Dec 11, 2017
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
Prev Previous commit
Next Next commit
[SPARK-21417][SQL] Infer join conditions using propagated constraints
## What changes were proposed in this pull request?

This PR adds an optimization rule that infers join conditions using propagated constraints.

For instance, if there is a join, where the left relation has 'a = 1' and the right relation has 'b = 1', then the rule infers 'a = b' as a join predicate. Only semantically new predicates are appended to the existing join condition.

Refer to the corresponding ticket and tests for more details.

## How was this patch tested?

This patch comes with a new test suite to cover the implemented logic.

Author: aokolnychyi <[email protected]>

Closes apache#18692 from aokolnychyi/spark-21417.
  • Loading branch information
aokolnychyi authored and gatorsmile committed Nov 30, 2017
commit 6ac57fd0d1c82b834eb4bf0dd57596b92a99d6de
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.expressions

import scala.collection.mutable

import org.apache.spark.sql.catalyst.expressions.EquivalentExpressionMap.SemanticallyEqualExpr

/**
* A class that allows you to map an expression into a set of equivalent expressions. The keys are
* handled based on their semantic meaning and ignoring cosmetic differences. The values are
* represented as [[ExpressionSet]]s.
*
* The underlying representation of keys depends on the [[Expression.semanticHash]] and
* [[Expression.semanticEquals]] methods.
*
* {{{
* val map = new EquivalentExpressionMap()
*
* map.put(1 + 2, a)
* map.put(rand(), b)
*
* map.get(2 + 1) => Set(a) // 1 + 2 and 2 + 1 are semantically equivalent
* map.get(1 + 2) => Set(a) // 1 + 2 and 2 + 1 are semantically equivalent
* map.get(rand()) => Set() // non-deterministic expressions are not equivalent
* }}}
*/
class EquivalentExpressionMap {

private val equivalenceMap = mutable.HashMap.empty[SemanticallyEqualExpr, ExpressionSet]

def put(expression: Expression, equivalentExpression: Expression): Unit = {
val equivalentExpressions = equivalenceMap.getOrElseUpdate(expression, ExpressionSet.empty)
equivalenceMap(expression) = equivalentExpressions + equivalentExpression
}

def get(expression: Expression): Set[Expression] =
equivalenceMap.getOrElse(expression, ExpressionSet.empty)
}

object EquivalentExpressionMap {

private implicit class SemanticallyEqualExpr(val expr: Expression) {
override def equals(obj: Any): Boolean = obj match {
case other: SemanticallyEqualExpr => expr.semanticEquals(other.expr)
case _ => false
}

override def hashCode: Int = expr.semanticHash()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ object ExpressionSet {
expressions.foreach(set.add)
set
}

val empty: ExpressionSet = ExpressionSet(Nil)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ abstract class Optimizer(sessionCatalog: SessionCatalog)
PushProjectionThroughUnion,
ReorderJoin,
EliminateOuterJoin,
EliminateCrossJoin,
InferFiltersFromConstraints,
BooleanSimplification,
PushPredicateThroughJoin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.catalyst.optimizer

import scala.annotation.tailrec
import scala.collection.mutable

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.planning.ExtractFiltersAndInnerJoins
Expand Down Expand Up @@ -152,3 +153,62 @@ object EliminateOuterJoin extends Rule[LogicalPlan] with PredicateHelper {
if (j.joinType == newJoinType) f else Filter(condition, j.copy(joinType = newJoinType))
}
}

/**
* A rule that eliminates CROSS joins by inferring join conditions from propagated constraints.
*
* The optimization is applicable only to CROSS joins. For other join types, adding inferred join
* conditions would potentially shuffle children as child node's partitioning won't satisfy the JOIN
* node's requirements which otherwise could have.
*
* For instance, given a CROSS join with the constraint 'a = 1' from the left child and the
* constraint 'b = 1' from the right child, this rule infers a new join predicate 'a = b' and
* converts it to an Inner join.
*/
object EliminateCrossJoin extends Rule[LogicalPlan] with PredicateHelper {

def apply(plan: LogicalPlan): LogicalPlan = {
if (SQLConf.get.constraintPropagationEnabled) {
eliminateCrossJoin(plan)
} else {
plan
}
}

private def eliminateCrossJoin(plan: LogicalPlan): LogicalPlan = plan transform {
case join @ Join(leftPlan, rightPlan, Cross, None) =>
val leftConstraints = join.constraints.filter(_.references.subsetOf(leftPlan.outputSet))
val rightConstraints = join.constraints.filter(_.references.subsetOf(rightPlan.outputSet))
val inferredJoinPredicates = inferJoinPredicates(leftConstraints, rightConstraints)
val joinConditionOpt = inferredJoinPredicates.reduceOption(And)
if (joinConditionOpt.isDefined) Join(leftPlan, rightPlan, Inner, joinConditionOpt) else join
}

private def inferJoinPredicates(
leftConstraints: Set[Expression],
rightConstraints: Set[Expression]): mutable.Set[EqualTo] = {

val equivalentExpressionMap = new EquivalentExpressionMap()

leftConstraints.foreach {
case EqualTo(attr: Attribute, expr: Expression) =>
equivalentExpressionMap.put(expr, attr)
case EqualTo(expr: Expression, attr: Attribute) =>
equivalentExpressionMap.put(expr, attr)
case _ =>
}

val joinConditions = mutable.Set.empty[EqualTo]

rightConstraints.foreach {
case EqualTo(attr: Attribute, expr: Expression) =>
joinConditions ++= equivalentExpressionMap.get(expr).map(EqualTo(attr, _))
case EqualTo(expr: Expression, attr: Attribute) =>
joinConditions ++= equivalentExpressionMap.get(expr).map(EqualTo(attr, _))
case _ =>
}

joinConditions
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.expressions

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.dsl.expressions._

class EquivalentExpressionMapSuite extends SparkFunSuite {

private val onePlusTwo = Literal(1) + Literal(2)
private val twoPlusOne = Literal(2) + Literal(1)
private val rand = Rand(10)

test("behaviour of the equivalent expression map") {
val equivalentExpressionMap = new EquivalentExpressionMap()
equivalentExpressionMap.put(onePlusTwo, 'a)
equivalentExpressionMap.put(Literal(1) + Literal(3), 'b)
equivalentExpressionMap.put(rand, 'c)

// 1 + 2 should be equivalent to 2 + 1
assertResult(ExpressionSet(Seq('a)))(equivalentExpressionMap.get(twoPlusOne))
// non-deterministic expressions should not be equivalent
assertResult(ExpressionSet.empty)(equivalentExpressionMap.get(rand))

// if the same (key, value) is added several times, the map still returns only one entry
equivalentExpressionMap.put(onePlusTwo, 'a)
equivalentExpressionMap.put(twoPlusOne, 'a)
assertResult(ExpressionSet(Seq('a)))(equivalentExpressionMap.get(twoPlusOne))

// get several equivalent attributes
equivalentExpressionMap.put(onePlusTwo, 'e)
assertResult(ExpressionSet(Seq('a, 'e)))(equivalentExpressionMap.get(onePlusTwo))
assertResult(2)(equivalentExpressionMap.get(onePlusTwo).size)

// several non-deterministic expressions should not be equivalent
equivalentExpressionMap.put(rand, 'd)
assertResult(ExpressionSet.empty)(equivalentExpressionMap.get(rand))
assertResult(0)(equivalentExpressionMap.get(rand).size)
}

}
Loading