Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
@@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that writing ExpressionSet.empty would be more readable than ExpressionSet(Nil). Usually, mutable collections have def empty() and immutable ones have separate objects that represent empty collections (e.g., Nil, Stream.Empty). I defined val empty since ExpressionSet is immutable.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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
Copy link
Contributor

Choose a reason for hiding this comment

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

can we apply this optimization to all joins after #19054?

Copy link
Member

Choose a reason for hiding this comment

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

It sounds promising.

* 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