Skip to content

Commit

Permalink
Merge branch 'jiang.filter-set'
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjaguar committed Dec 7, 2015
2 parents e63df88 + 9043afd commit da46b1c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ trait PredicateHelper {
}
}

/**
* Returns true if two expressions are equivalent predicates. Equality check is tolerant of
* ordering different.
*/
def equivalentPredicates(left: Expression, right: Expression): Boolean = {
val leftAndPredicates = splitConjunctivePredicates(left).toSet
val rightAndPredicates = splitConjunctivePredicates(right).toSet
val leftOrPredicates = splitDisjunctivePredicates(left).toSet
val rightOrPredicates = splitDisjunctivePredicates(right).toSet
// We split the two conditions into conjunctive predicates and disjunctive predicates
// If either of them match, we consider them equivalent conditions
(leftAndPredicates == rightAndPredicates) || (leftOrPredicates == rightOrPredicates)
}

// Substitute any known alias from a map.
protected def replaceAlias(
condition: Expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,31 +129,14 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with PredicateHelper w
s"[${cleanRight.cleanArgs.mkString(", ")}] == [${cleanLeft.cleanArgs.mkString(", ")}]")
(cleanRight.cleanArgs == cleanLeft.cleanArgs) || ((cleanLeft, cleanRight) match {
case (l: Filter, r: Filter) =>
equivalentConditions(cleanExpression(l.condition, l.children.flatMap(_.output)),
equivalentPredicates(cleanExpression(l.condition, l.children.flatMap(_.output)),
cleanExpression(r.condition, r.children.flatMap(_.output)))
case _ => false
})
} &&
(cleanLeft.children, cleanRight.children).zipped.forall(_ sameResult _)
}

/**
* Returns true if two conditions are equivalent. Equality check is tolerant of ordering
* different.
*/
def equivalentConditions(left: Expression, right: Expression): Boolean = {
logDebug(s"equivalentConditions: [${left.toString}] with [${right.toString}]")

val leftAndPredicates = splitConjunctivePredicates(left).toSet
val rightAndPredicates = splitConjunctivePredicates(right).toSet
val leftOrPredicates = splitDisjunctivePredicates(left).toSet
val rightOrPredicates = splitDisjunctivePredicates(right).toSet
logDebug(s"equivalentConditions Result: [${leftAndPredicates == rightAndPredicates}]")
// We split the two conditions into conjunctive predicates and disjunctive predicates
// If either of them match, we consider them equivalent conditions
(leftAndPredicates == rightAndPredicates) || (leftOrPredicates == rightOrPredicates)
}

/** Clean an expression so that differences in expression id should not affect equality */
def cleanExpression(e: Expression, input: Seq[Attribute]) = e match {
case a: Alias =>
Expand Down

0 comments on commit da46b1c

Please sign in to comment.