neilconway commented on code in PR #22534: URL: https://github.com/apache/datafusion/pull/22534#discussion_r3305153454
########## datafusion/sqllogictest/test_files/eliminate_outer_join.slt: ########## @@ -538,6 +538,110 @@ select * from t1 left join t2 on t1.a = t2.x where (t2.y > 150) is true and t2.z ---- 2 20 b 2 200 q +### +### Projection between Filter and Join +### + +# A filter on a volatile, projected expression can still be used for outer join +# elimination. +query TT +explain +select s.a +from ( + select t1.a, random() + cast(t2.y as double) as ry + from t1 left join t2 on t1.a = t2.x +) s +where s.ry > 150.0; +---- +logical_plan +01)SubqueryAlias: s +02)--Projection: t1.a +03)----Filter: ry > Float64(150) +04)------Projection: t1.a, random() + CAST(t2.y AS Float64) AS ry +05)--------Inner Join: t1.a = t2.x +06)----------TableScan: t1 projection=[a] +07)----------TableScan: t2 projection=[x, y] + +query I rowsort +select s.a +from ( + select t1.a, random() + cast(t2.y as double) as ry + from t1 left join t2 on t1.a = t2.x +) s +where s.ry > 150.0; +---- +2 + +# This query has the shape of TPC-DS Q49: `OptimizeProjections` results in +# placing a `Projection` node between the `Filter` and `Join`, but we can look +# through that node to convert the outer join. +statement ok +create table d(k int, flag int); + +statement ok +insert into d values (1, 1), (2, 1), (3, 0); + +query TT +explain +select t1.a, sum(coalesce(t2.y, 0)) as ret_sum +from t1 left join t2 on t1.a = t2.x, d +where t2.y > 150 + and t1.a = d.k + and d.flag = 1 +group by t1.a; +---- +logical_plan +01)Projection: t1.a, sum(coalesce(t2.y,Int64(0))) AS ret_sum +02)--Aggregate: groupBy=[[t1.a]], aggr=[[sum(CASE WHEN __common_expr_1 IS NOT NULL THEN __common_expr_1 ELSE Int64(0) END) AS sum(coalesce(t2.y,Int64(0)))]] +03)----Projection: CAST(t2.y AS Int64) AS __common_expr_1, t1.a +04)------Inner Join: t1.a = d.k +05)--------Projection: t1.a, t2.y +06)----------Inner Join: t1.a = t2.x +07)------------TableScan: t1 projection=[a] +08)------------Filter: t2.y > Int32(150) +09)--------------TableScan: t2 projection=[x, y] +10)--------Projection: d.k +11)----------Filter: d.flag = Int32(1) +12)------------TableScan: d projection=[k, flag] + +query II rowsort +select t1.a, sum(coalesce(t2.y, 0)) as ret_sum +from t1 left join t2 on t1.a = t2.x, d +where t2.y > 150 + and t1.a = d.k + and d.flag = 1 +group by t1.a; +---- +2 200 + +# A CTE can introduce a query boundary between the outer filter and the +# LEFT JOIN. +query TT +explain +with s as ( + select t1.a, t2.y + from t1 left join t2 on t1.a = t2.x +) +select s.a from s where s.y > 150; +---- +logical_plan +01)SubqueryAlias: s +02)--Projection: t1.a +03)----Inner Join: t1.a = t2.x +04)------TableScan: t1 projection=[a] +05)------Projection: t2.x +06)--------Filter: t2.y > Int32(150) +07)----------TableScan: t2 projection=[x, y] + +query I rowsort +with s as ( + select t1.a, t2.y + from t1 left join t2 on t1.a = t2.x +) +select s.a from s where s.y > 150; +---- +2 Review Comment: The "Filter -> Projection -> Join" pattern does not actually appear in the final plan, because of subsequent optimizer passes; I verified that the plan contains an outer join if we revert the changes in this PR. -- 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]
