diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala index 805cad5cb953edc02fd0dedb941ffe642100ea86..37f0c8ed19d3707db3fc2bcbe70f89f619a540e5 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala @@ -932,7 +932,7 @@ object PushPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { split(joinCondition.map(splitConjunctivePredicates).getOrElse(Nil), left, right) joinType match { - case _: InnerLike | LeftSemi | ExistenceJoin(_) => + case _: InnerLike | LeftSemi => // push down the single side only join filter for both sides sub queries val newLeft = leftJoinConditions. reduceLeftOption(And).map(Filter(_, left)).getOrElse(left) @@ -949,7 +949,7 @@ object PushPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { val newJoinCond = (rightJoinConditions ++ commonJoinCondition).reduceLeftOption(And) Join(newLeft, newRight, RightOuter, newJoinCond) - case LeftOuter | LeftAnti => + case LeftOuter | LeftAnti | ExistenceJoin(_) => // push down the right side only join filter for right sub query val newLeft = left val newRight = rightJoinConditions. diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala index 3e67282d687f5790c4af281f6b2d20fd3b30553e..6feea4060f46ab94e15e4d4cdf991362fca00e7b 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala @@ -546,6 +546,23 @@ class FilterPushdownSuite extends PlanTest { comparePlans(optimized, analysis.EliminateSubqueryAliases(correctAnswer)) } + test("joins: only push down join conditions to the right of an existence join") { + val x = testRelation.subquery('x) + val y = testRelation.subquery('y) + val fillerVal = 'val.boolean + val originalQuery = + x.join(y, + ExistenceJoin(fillerVal), + Some("x.a".attr > 1 && "y.b".attr > 2)).analyze + val optimized = Optimize.execute(originalQuery) + val correctAnswer = + x.join( + y.where("y.b".attr > 2), + ExistenceJoin(fillerVal), + Some("x.a".attr > 1)) + .analyze + comparePlans(optimized, analysis.EliminateSubqueryAliases(correctAnswer)) + } val testRelationWithArrayType = LocalRelation('a.int, 'b.int, 'c_arr.array(IntegerType)) diff --git a/sql/core/src/test/resources/sql-tests/inputs/anti-join.sql b/sql/core/src/test/resources/sql-tests/inputs/pred-pushdown.sql similarity index 64% rename from sql/core/src/test/resources/sql-tests/inputs/anti-join.sql rename to sql/core/src/test/resources/sql-tests/inputs/pred-pushdown.sql index 0346f57d609adf4345ec0a6315b83d933c54a4f1..eff258a06635a803084f227c586777c7f787caa1 100644 --- a/sql/core/src/test/resources/sql-tests/inputs/anti-join.sql +++ b/sql/core/src/test/resources/sql-tests/inputs/pred-pushdown.sql @@ -1,7 +1,12 @@ --- SPARK-18597: Do not push down predicates to left hand side in an anti-join CREATE OR REPLACE TEMPORARY VIEW tbl_a AS VALUES (1, 1), (2, 1), (3, 6) AS T(c1, c2); CREATE OR REPLACE TEMPORARY VIEW tbl_b AS VALUES 1 AS T(c1); +-- SPARK-18597: Do not push down predicates to left hand side in an anti-join SELECT * FROM tbl_a LEFT ANTI JOIN tbl_b ON ((tbl_a.c1 = tbl_a.c2) IS NULL OR tbl_a.c1 = tbl_a.c2); + +-- SPARK-18614: Do not push down predicates on left table below ExistenceJoin +SELECT l.c1, l.c2 +FROM tbl_a l +WHERE EXISTS (SELECT 1 FROM tbl_b r WHERE l.c1 = l.c2) OR l.c2 < 2; diff --git a/sql/core/src/test/resources/sql-tests/results/anti-join.sql.out b/sql/core/src/test/resources/sql-tests/results/pred-pushdown.sql.out similarity index 71% rename from sql/core/src/test/resources/sql-tests/results/anti-join.sql.out rename to sql/core/src/test/resources/sql-tests/results/pred-pushdown.sql.out index 6f38c4d08bc5a9fcc3b3c9621fca216934b46e2b..1b8ddbe4c7211894e946d78d5179503886345970 100644 --- a/sql/core/src/test/resources/sql-tests/results/anti-join.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/pred-pushdown.sql.out @@ -1,5 +1,5 @@ -- Automatically generated by SQLQueryTestSuite --- Number of queries: 3 +-- Number of queries: 4 -- !query 0 @@ -27,3 +27,14 @@ struct<c1:int,c2:int> -- !query 2 output 2 1 3 6 + + +-- !query 3 +SELECT l.c1, l.c2 +FROM tbl_a l +WHERE EXISTS (SELECT 1 FROM tbl_b r WHERE l.c1 = l.c2) OR l.c2 < 2 +-- !query 3 schema +struct<c1:int,c2:int> +-- !query 3 output +1 1 +2 1