Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyum committed Apr 13, 2023
1 parent 0181764 commit 842faec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,16 @@ trait ConstraintHelper {
val predicates = constraints.filterNot(_.isInstanceOf[IsNotNull])
predicates.foreach {
case eq @ EqualTo(l: Attribute, r: Attribute) =>
val candidateConstraints = predicates - eq
val candidateConstraints = predicates - eq - EqualNullSafe(l, r)
inferredConstraints ++= replaceConstraints(candidateConstraints, l, r)
inferredConstraints ++= replaceConstraints(candidateConstraints, r, l)
case eq @ EqualTo(l @ Cast(_: Attribute, _, _, _), r: Attribute) =>
inferredConstraints ++= replaceConstraints(predicates - eq, r, l)
inferredConstraints ++= replaceConstraints(predicates - eq - EqualNullSafe(l, r), r, l)
case eq @ EqualTo(l: Attribute, r @ Cast(_: Attribute, _, _, _)) =>
inferredConstraints ++= replaceConstraints(predicates - eq, l, r)
inferredConstraints ++= replaceConstraints(predicates - eq - EqualNullSafe(l, r), l, r)
case _ => // No inference
}
(inferredConstraints -- constraints).filterNot {
case a EqualNullSafe b => a.semanticEquals(b)
case _ => false
}
inferredConstraints -- constraints
}

private def replaceConstraints(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,26 +377,26 @@ class InferFiltersFromConstraintsSuite extends PlanTest {
val y = testRelation.as("y")
val z = testRelation.as("z")

// Test filter out true literal
// Removes EqualNullSafe when constructing candidate constraints
comparePlans(
InferFiltersFromConstraints(x.select($"x.a", $"x.a".as("xa"))
.where($"xa" <=> $"x.a" && $"xa" === $"x.a").analyze),
x.select($"x.a", $"x.a".as("xa"))
.where($"xa".isNotNull && $"x.a".isNotNull && $"xa" <=> $"x.a" && $"xa" === $"x.a").analyze)

// Test filter out true literal and once strategy's idempotence is not broken
// Once strategy's idempotence is not broken
val originalQuery =
x.join(y, condition = Some($"x.a" === $"y.a"))
.select($"x.a", $"x.a".as("xa")).as("xy")
.join(z, condition = Some($"xy.a" === $"z.a"))
.join(z, condition = Some($"xy.a" === $"z.a")).analyze

val correctAnswer =
x.where($"a".isNotNull).join(y.where($"a".isNotNull), condition = Some($"x.a" === $"y.a"))
.select($"x.a", $"x.a".as("xa")).as("xy")
.join(z.where($"a".isNotNull), condition = Some($"xy.a" === $"z.a"))
.join(z.where($"a".isNotNull), condition = Some($"xy.a" === $"z.a")).analyze

comparePlans(
InferFiltersFromConstraints(InferFiltersFromConstraints(originalQuery.analyze)),
correctAnswer.analyze)
val optimizedQuery = InferFiltersFromConstraints(originalQuery)
comparePlans(optimizedQuery, correctAnswer)
comparePlans(InferFiltersFromConstraints(optimizedQuery), correctAnswer)
}
}

0 comments on commit 842faec

Please sign in to comment.