diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala index 424f4b96271d3..6c0d7189862d3 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala @@ -1064,7 +1064,7 @@ object DecorrelateInnerQuery extends PredicateHelper { // Project, they could get added at the beginning or the end of the output columns // depending on the child plan. // The inner expressions for the domain are the values of newOuterReferenceMap. - val domainProjections = collectedChildOuterReferences.map(newOuterReferenceMap(_)) + val domainProjections = newOuterReferences.map(newOuterReferenceMap(_)) val newChild = Project(child.output ++ domainProjections, decorrelatedChild) (newChild, newJoinCond, newOuterReferenceMap) } diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/join-lateral.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/join-lateral.sql.out index e81ee769f57d6..5bf893605423c 100644 --- a/sql/core/src/test/resources/sql-tests/analyzer-results/join-lateral.sql.out +++ b/sql/core/src/test/resources/sql-tests/analyzer-results/join-lateral.sql.out @@ -3017,6 +3017,53 @@ Project [c1#x, c2#x, t#x] +- LocalRelation [col1#x, col2#x] +-- !query +select 1 +from t1 as t_outer +left join + lateral( + select b1,b2 + from + ( + select + t2.c1 as b1, + 1 as b2 + from t2 + union + select t_outer.c1 as b1, + null as b2 + ) as t_inner + where (t_inner.b1 < t_outer.c2 or t_inner.b1 is null) + and t_inner.b1 = t_outer.c1 + order by t_inner.b1,t_inner.b2 desc limit 1 + ) as lateral_table +-- !query analysis +Project [1 AS 1#x] ++- LateralJoin lateral-subquery#x [c2#x && c1#x && c1#x], LeftOuter + : +- SubqueryAlias lateral_table + : +- GlobalLimit 1 + : +- LocalLimit 1 + : +- Sort [b1#x ASC NULLS FIRST, b2#x DESC NULLS LAST], true + : +- Project [b1#x, b2#x] + : +- Filter (((b1#x < outer(c2#x)) OR isnull(b1#x)) AND (b1#x = outer(c1#x))) + : +- SubqueryAlias t_inner + : +- Distinct + : +- Union false, false + : :- Project [c1#x AS b1#x, 1 AS b2#x] + : : +- SubqueryAlias spark_catalog.default.t2 + : : +- View (`spark_catalog`.`default`.`t2`, [c1#x, c2#x]) + : : +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x] + : : +- LocalRelation [col1#x, col2#x] + : +- Project [b1#x, cast(b2#x as int) AS b2#x] + : +- Project [outer(c1#x) AS b1#x, null AS b2#x] + : +- OneRowRelation + +- SubqueryAlias t_outer + +- SubqueryAlias spark_catalog.default.t1 + +- View (`spark_catalog`.`default`.`t1`, [c1#x, c2#x]) + +- Project [cast(col1#x as int) AS c1#x, cast(col2#x as int) AS c2#x] + +- LocalRelation [col1#x, col2#x] + + -- !query DROP VIEW t1 -- !query analysis diff --git a/sql/core/src/test/resources/sql-tests/inputs/join-lateral.sql b/sql/core/src/test/resources/sql-tests/inputs/join-lateral.sql index 8bff1f109aa65..e3cef9207d20f 100644 --- a/sql/core/src/test/resources/sql-tests/inputs/join-lateral.sql +++ b/sql/core/src/test/resources/sql-tests/inputs/join-lateral.sql @@ -531,6 +531,27 @@ select * from t1 join lateral (select t4.c1 as t from t4 where t1.c1 = t4.c1)) as foo order by foo.t limit 5); + +select 1 +from t1 as t_outer +left join + lateral( + select b1,b2 + from + ( + select + t2.c1 as b1, + 1 as b2 + from t2 + union + select t_outer.c1 as b1, + null as b2 + ) as t_inner + where (t_inner.b1 < t_outer.c2 or t_inner.b1 is null) + and t_inner.b1 = t_outer.c1 + order by t_inner.b1,t_inner.b2 desc limit 1 + ) as lateral_table; + -- clean up DROP VIEW t1; DROP VIEW t2; diff --git a/sql/core/src/test/resources/sql-tests/results/join-lateral.sql.out b/sql/core/src/test/resources/sql-tests/results/join-lateral.sql.out index ced8d6398a66f..11bafb2cf63c9 100644 --- a/sql/core/src/test/resources/sql-tests/results/join-lateral.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/join-lateral.sql.out @@ -1878,6 +1878,33 @@ struct 1 2 3 +-- !query +select 1 +from t1 as t_outer +left join + lateral( + select b1,b2 + from + ( + select + t2.c1 as b1, + 1 as b2 + from t2 + union + select t_outer.c1 as b1, + null as b2 + ) as t_inner + where (t_inner.b1 < t_outer.c2 or t_inner.b1 is null) + and t_inner.b1 = t_outer.c1 + order by t_inner.b1,t_inner.b2 desc limit 1 + ) as lateral_table +-- !query schema +struct<1:int> +-- !query output +1 +1 + + -- !query DROP VIEW t1 -- !query schema