diff --git a/src/backend/distributed/planner/recursive_planning.c b/src/backend/distributed/planner/recursive_planning.c index 5fcb91eab..0087aec8f 100644 --- a/src/backend/distributed/planner/recursive_planning.c +++ b/src/backend/distributed/planner/recursive_planning.c @@ -2801,23 +2801,24 @@ JoinTreeContainsLateral(Node *node, List *rtable) } if (IsA(node, RangeTblRef)) - { - RangeTblEntry *rte = rt_fetch(((RangeTblRef *) node)->rtindex, rtable); + { + RangeTblEntry *rte = rt_fetch(((RangeTblRef *) node)->rtindex, rtable); if (rte == NULL) { return false; } if (rte->lateral) - { + { return true; } - - if(rte->rtekind == RTE_SUBQUERY) + + if (rte->rtekind == RTE_SUBQUERY) { if (rte->subquery) { - return JoinTreeContainsLateral((Node *) rte->subquery->jointree, rte->subquery->rtable); + return JoinTreeContainsLateral((Node *) rte->subquery->jointree, + rte->subquery->rtable); } } return false; @@ -2826,7 +2827,7 @@ JoinTreeContainsLateral(Node *node, List *rtable) { JoinExpr *join = (JoinExpr *) node; return JoinTreeContainsLateral(join->larg, rtable) || - JoinTreeContainsLateral(join->rarg, rtable); + JoinTreeContainsLateral(join->rarg, rtable); } else if (IsA(node, FromExpr)) { @@ -2844,7 +2845,6 @@ JoinTreeContainsLateral(Node *node, List *rtable) } - /* * CheckPushDownFeasibilityAndComputeIndexes checks if the given join expression * is a left outer join and if it is feasible to push down the join. If feasible, @@ -2903,11 +2903,12 @@ CheckPushDownFeasibilityAndComputeIndexes(JoinExpr *joinExpr, Query *query, return false; } - /* For now if we see any lateral join in the join tree, we return false. - * This check can be improved to support the cases where the lateral reference + /* For now if we see any lateral join in the join tree, we return false. + * This check can be improved to support the cases where the lateral reference * does not cause an error in the final planner checks. - */ - if (JoinTreeContainsLateral(joinExpr->rarg, query->rtable) || JoinTreeContainsLateral(joinExpr->larg, query->rtable)) + */ + if (JoinTreeContainsLateral(joinExpr->rarg, query->rtable) || JoinTreeContainsLateral( + joinExpr->larg, query->rtable)) { ereport(DEBUG5, (errmsg( "Lateral join is not supported for pushdown in this path."))); diff --git a/src/test/regress/sql/pg17.sql b/src/test/regress/sql/pg17.sql index 17dfd6b8f..009d6d12a 100644 --- a/src/test/regress/sql/pg17.sql +++ b/src/test/regress/sql/pg17.sql @@ -170,16 +170,16 @@ SET client_min_messages TO DEBUG3; CREATE TABLE users_ref(user_id int, dept int); SELECT create_reference_table('users_ref'); INSERT INTO users_ref VALUES (1, 3), (2, 4), (3, 3), (4, 4); --- In PG17, the planner can pull up a correlated ANY subquery to a join, resulting +-- In PG17, the planner can pull up a correlated ANY subquery to a join, resulting -- in a different query plan compared to PG16. Specifically, for the following query -- the rewritten query has a lateral recurring outer join, which requires recursive -- computation of the inner part. However, this join is not analyzed during the recursive -- planning step, as it is performed on the original query structure. As a result, -- the lateral join is not recursively planned, and a lateral join error is raised --- at a later stage. -SELECT user_id FROM +-- at a later stage. +SELECT user_id FROM users RIGHT JOIN users_ref USING (user_id) -WHERE users_ref.dept IN +WHERE users_ref.dept IN ( SELECT events.event_type FROM events WHERE events.user_id = users.user_id ) ORDER BY 1 LIMIT 1;