diff --git a/src/backend/distributed/planner/multi_physical_planner.c b/src/backend/distributed/planner/multi_physical_planner.c index ff7796429..7758a1185 100644 --- a/src/backend/distributed/planner/multi_physical_planner.c +++ b/src/backend/distributed/planner/multi_physical_planner.c @@ -2302,6 +2302,18 @@ QueryPushdownSqlTaskList(Query *query, uint64 jobId, } } + /* + * We might fail to find outer joins from the relationRestrictionContext + * when the original query has CTEs. In order to ensure that we always mark + * the outer joins correctly and compute additional quals when necessary, + * check the task query as well. + */ + if (!updateQualsForOuterJoin && FindNodeMatchingCheckFunction((Node *) query, + IsOuterJoinExpr)) + { + updateQualsForOuterJoin = true; + } + /* * We keep track of minShardOffset to skip over a potentially big amount of pruned * shards. However, we need to start at minShardOffset - 1 to make sure we don't diff --git a/src/test/regress/expected/recurring_join_pushdown.out b/src/test/regress/expected/recurring_join_pushdown.out index 696861a17..4bc19fcea 100644 --- a/src/test/regress/expected/recurring_join_pushdown.out +++ b/src/test/regress/expected/recurring_join_pushdown.out @@ -845,6 +845,14 @@ DEBUG: assigned task to node localhost:xxxxx (1 row) SET client_min_messages TO ERROR; +-- Ensure that even when CTEs are replaced, we insert push +-- down conditions for outer joins when necessary. +WITH cte_0 AS ( SELECT table_0.a FROM d1 AS table_0 WHERE table_0.a IN ( SELECT table_1.a FROM d1 AS table_1 ORDER BY a LIMIT 2 ) ORDER BY a ) SELECT count(*), avg(avgsub.a) FROM ( SELECT table_2.a FROM cte_0 AS table_2 RIGHT JOIN r1 AS table_3 USING (a)) AS avgsub; + count | avg +--------------------------------------------------------------------- + 15 | 1.00000000000000000000 +(1 row) + -- The following queries trigger recursive computing, recurring outer-join push down -- methods introduced in#7973 can be enhanced to cover these cases in the future. CREATE TABLE r1_local AS SELECT * FROM r1; diff --git a/src/test/regress/sql/recurring_join_pushdown.sql b/src/test/regress/sql/recurring_join_pushdown.sql index 8dec836f7..792704abb 100644 --- a/src/test/regress/sql/recurring_join_pushdown.sql +++ b/src/test/regress/sql/recurring_join_pushdown.sql @@ -117,6 +117,11 @@ SELECT count(*) FROM (SELECT * FROM d1) AS t1 RIGHT JOIN r1 USING (a); SET client_min_messages TO ERROR; +-- Ensure that even when CTEs are replaced, we insert push +-- down conditions for outer joins when necessary. +WITH cte_0 AS ( SELECT table_0.a FROM d1 AS table_0 WHERE table_0.a IN ( SELECT table_1.a FROM d1 AS table_1 ORDER BY a LIMIT 2 ) ORDER BY a ) SELECT count(*), avg(avgsub.a) FROM ( SELECT table_2.a FROM cte_0 AS table_2 RIGHT JOIN r1 AS table_3 USING (a)) AS avgsub; + + -- The following queries trigger recursive computing, recurring outer-join push down -- methods introduced in#7973 can be enhanced to cover these cases in the future. CREATE TABLE r1_local AS SELECT * FROM r1;