This is an automated email from the ASF dual-hosted git repository.

reshke pushed a commit to branch jdejii
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 65b0b74b863fab25cc7978ef223c0e32a54c4dc2
Author: reshke <[email protected]>
AuthorDate: Mon Aug 17 08:39:37 2026 +0000

    Fix JOIN motion type selection for join quals containing outer refs
---
 src/backend/optimizer/plan/initsplan.c    | 5 ++---
 src/backend/optimizer/util/restrictinfo.c | 9 +++++++++
 src/test/regress/sql/join_gp.sql          | 8 ++++++++
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/backend/optimizer/plan/initsplan.c 
b/src/backend/optimizer/plan/initsplan.c
index b65897043ca..c5bc77b67c3 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -2798,7 +2798,9 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
                if (maybe_equivalence)
                {
                        if (process_equivalence(root, &restrictinfo, 
jtitem->jdomain))
+                       {
                                return;
+                       }
                        /* EC rejected it, so set left_ec/right_ec the hard way 
... */
                        if (restrictinfo->mergeopfamilies)      /* EC might 
have changed this */
                                initialize_mergeclause_eclasses(root, 
restrictinfo);
@@ -2970,9 +2972,6 @@ distribute_restrictinfo_to_rels(PlannerInfo *root,
        Relids          relids = restrictinfo->required_relids;
        RelOptInfo *rel;
 
-       if (contains_outer_params((Node *) restrictinfo->clause, root))
-               restrictinfo->contain_outer_query_references = true;
-
        switch (bms_membership(relids))
        {
                case BMS_SINGLETON:
diff --git a/src/backend/optimizer/util/restrictinfo.c 
b/src/backend/optimizer/util/restrictinfo.c
index 45552d53aaf..c1a9404e91d 100644
--- a/src/backend/optimizer/util/restrictinfo.c
+++ b/src/backend/optimizer/util/restrictinfo.c
@@ -14,6 +14,7 @@
  */
 #include "postgres.h"
 
+#include "cdb/cdbmutate.h"
 #include "nodes/makefuncs.h"
 #include "nodes/nodeFuncs.h"
 #include "optimizer/clauses.h"
@@ -263,6 +264,14 @@ make_restrictinfo_internal(PlannerInfo *root,
        restrictinfo->left_hasheqoperator = InvalidOid;
        restrictinfo->right_hasheqoperator = InvalidOid;
 
+       /*
+        * Determine whether this clause references any var in outer query 
levels.
+        * Such clauses must be evaluated in the same slice as the parent query,
+        * so we set this planner hint to later use in join motion planning.
+        */
+       restrictinfo->contain_outer_query_references =
+               contains_outer_params((Node *) clause, root);
+
        return restrictinfo;
 }
 
diff --git a/src/test/regress/sql/join_gp.sql b/src/test/regress/sql/join_gp.sql
index 6a96d9b98e1..528dcc39300 100644
--- a/src/test/regress/sql/join_gp.sql
+++ b/src/test/regress/sql/join_gp.sql
@@ -1316,3 +1316,11 @@ drop table if exists repli_t1_pk;
 drop table if exists repli_t2_pk;
 drop table if exists repli_t3_pk;
 drop table if exists repli_t4_pk;
+
+--
+-- Test that a join qual containing an outer-level reference
+-- is correctly identified as referring to the outer
+-- query, so that the join motion is planned in the parent slice.
+--
+explain (costs off) select * from generate_series(1,2) t1, lateral (select 
t3.unique1 from int4_tbl t2 join tenk1 t3 on t2.f1 = t3.unique1 + t1 order by 
1) z;
+select * from generate_series(1,2) t1, lateral (select t3.unique1 from 
int4_tbl t2 join tenk1 t3 on t2.f1 = t3.unique1 + t1 order by 1) z;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to