wangyum commented on code in PR #39908:
URL: https://github.com/apache/spark/pull/39908#discussion_r1109853967
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/OuterJoinEliminationSuite.scala:
##########
@@ -315,4 +315,28 @@ class OuterJoinEliminationSuite extends PlanTest {
.select($"a", $"d").analyze
comparePlans(Optimize.execute(p3), p3)
}
+
+ test("SPARK-42360: Transform LeftOuter join with IsNull filter on right side
to Anti join") {
+ object InferFilterOptimize extends RuleExecutor[LogicalPlan] {
+ val batches =
+ Batch("Infer Filters", Once,
+ InferFiltersFromConstraints) ::
+ Batch("Subqueries", Once,
+ EliminateSubqueryAliases) ::
+ Batch("Outer Join Elimination", Once,
+ EliminateOuterJoin,
+ PushPredicateThroughJoin) :: Nil
+ }
+
+ val x = testRelation.subquery("x")
+ val y = testRelation1.subquery("y")
+ comparePlans(InferFilterOptimize.execute(
+ x.join(y, LeftOuter, Some($"a" === $"d"))
+ .where($"d".isNull && rand(1) < 0.5)
+ .select($"a", $"b", $"c").analyze),
+ x.join(y.where($"d".isNotNull), LeftAnti, Some($"a" === $"d"))
+ .where(rand(1) < 0.5)
+ .select($"a", $"b", $"c").analyze
+ )
Review Comment:
We can remove `InferFiltersFromConstraints`:
```scala
val x = testRelation.subquery("x")
val y = testRelation1.subquery("y").where($"d".isNotNull)
val originQuery = x.join(y, LeftOuter, Some($"a" === $"d"))
.where($"d".isNull && rand(1) < 0.5)
.select($"a", $"b", $"c")
val correctAnswer = x.join(y, LeftAnti, Some($"a" === $"d"))
.where(rand(1) < 0.5)
.select($"a", $"b", $"c")
comparePlans(Optimize.execute(originQuery.analyze),
correctAnswer.analyze)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]