Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-43095][SQL] Avoid Once strategy's idempotence is broken for batch: Infer Filters #40742

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -66,13 +66,13 @@ trait ConstraintHelper {
val predicates = constraints.filterNot(_.isInstanceOf[IsNotNull])
predicates.foreach {
case eq @ EqualTo(l: Attribute, r: Attribute) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

not related to this PR, but shall we match Equality here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. It seems ok.

val candidateConstraints = predicates - eq
val candidateConstraints = predicates - eq - EqualNullSafe(l, r)
Copy link
Contributor

Choose a reason for hiding this comment

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

if l.semanticEquals(r), we don't need to infer predicates at all, right?

Copy link
Member Author

@wangyum wangyum Apr 13, 2023

Choose a reason for hiding this comment

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

Yes. But the current situation is like this: l <=> l and r <=> r is inferred from l === r and l <=> r. At this time, l.semanticEquals(r) is false.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah got it. Let's add a code comment to explain it with this example.

Copy link
Contributor

@cloud-fan cloud-fan Apr 13, 2023

Choose a reason for hiding this comment

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

can you explain it a bit more about why it makes the optimizer not idempotent?

Copy link
Member Author

Choose a reason for hiding this comment

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

For example:

Join Inner, (a#0 = a#7)
:- Project [a#0, a#0 AS xa#3]
:  +- Join Inner, (a#0 = a#4)
:     :- LocalRelation <empty>, [a#0, b#1, c#2]
:     +- LocalRelation <empty>, [a#4, b#5, c#6]
+- LocalRelation <empty>, [a#7, b#8, c#9]

After InferFiltersFromConstraints:

Join Inner, (a#0 = a#7)
:- Filter ((a#0 <=> a#0) AND (xa#3 <=> xa#3))
:  +- Project [a#0, a#0 AS xa#3]
:     +- Join Inner, (a#0 = a#4)
:        :- Filter isnotnull(a#0)
:        :  +- LocalRelation <empty>, [a#0, b#1, c#2]
:        +- Filter isnotnull(a#4)
:           +- LocalRelation <empty>, [a#4, b#5, c#6]
+- Filter isnotnull(a#7)
   +- LocalRelation <empty>, [a#7, b#8, c#9]

And run InferFiltersFromConstraints again. The left side constraints:

(a#0 <=> xa#3)
(xa#3 = a#0)
(a#0 <=> a#0)
(xa#3 <=> xa#3)

The right side constraints:

(isnotnull(a#7))

Join condition is:

(a#0 = a#7)

Based on these constraints, the inferred result is:

(a#7 <=> xa#3)
(xa#3 = a#7)
(a#7 <=> a#7)

(a#7 <=> a#7) is a valid constraints for right side. The result:

Join Inner, (a#0 = a#7)
:- Filter ((a#0 <=> a#0) AND (xa#3 <=> xa#3))
:  +- Project [a#0, a#0 AS xa#3]
:     +- Join Inner, (a#0 = a#4)
:        :- Filter isnotnull(a#0)
:        :  +- LocalRelation <empty>, [a#0, b#1, c#2]
:        +- Filter isnotnull(a#4)
:           +- LocalRelation <empty>, [a#4, b#5, c#6]
+- Filter (a#7 <=> a#7)
   +- Filter isnotnull(a#7)
      +- LocalRelation <empty>, [a#7, b#8, c#9]

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,32 @@ class InferFiltersFromConstraintsSuite extends PlanTest {
val optimized = Optimize.execute(originalQuery)
comparePlans(optimized, correctAnswer)
}

test("SPARK-43095: Avoid Once strategy's idempotence is broken for batch: Infer Filters") {
val x = testRelation.as("x")
val y = testRelation.as("y")
val z = testRelation.as("z")

// 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)

// 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")).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")).analyze

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