From d1bfa45e6d65084f3c727fad9c215e2f12eee779 Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Sat, 5 Dec 2026 17:13:37 +0900 Subject: [PATCH v1] Wrap variable-free virtual column expansions in FULL JOIN quals If a virtual generated column's generation expression is, or simplifies to, a constant, then a reference to it in a FULL JOIN's ON clause expands to a join clause that refers to only one side of the join. Such a clause is neither merge-joinable nor hash-joinable, so planning fails with "FULL JOIN is only supported with merge-joinable or hash-joinable join conditions". Subquery pullup avoids this by wrapping variable-free expressions in PHVs within FULL JOIN quals, via replace_vars_in_jointree(), but expand_virtual_generated_columns() applies pullup_replace_vars() to the whole query at once and so never does that. To fix, detach the jointree before expanding the rest of the query, and then expand its quals with replace_vars_in_jointree(). Since the contents of LATERAL RTEs have already been processed by then, add a field to pullup_replace_vars_context to tell replace_vars_in_jointree() whether to process them. Back-patch to v18, where virtual generated columns were introduced. --- src/backend/optimizer/prep/prepjointree.c | 30 +++++++++++++++---- .../regress/expected/generated_virtual.out | 12 ++++++++ src/test/regress/sql/generated_virtual.sql | 4 +++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c index 58f04927041..13c45ae16de 100644 --- a/src/backend/optimizer/prep/prepjointree.c +++ b/src/backend/optimizer/prep/prepjointree.c @@ -81,6 +81,7 @@ typedef struct pullup_replace_vars_context bool *outer_hasSubLinks; /* -> outer query's hasSubLinks */ int varno; /* varno of subquery */ ReplaceWrapOption wrap_option; /* do we need certain outputs to be PHVs? */ + bool process_lateral; /* do we need to process LATERAL RTEs? */ Node **rv_cache; /* cache for results with PHVs */ } pullup_replace_vars_context; @@ -542,6 +543,7 @@ expand_virtual_generated_columns(PlannerInfo *root, Query *parse, List *tlist = NIL; pullup_replace_vars_context rvcontext; List *save_exclRelTlist = NIL; + FromExpr *save_jointree; for (int i = 0; i < tupdesc->natts; i++) { @@ -595,6 +597,8 @@ expand_virtual_generated_columns(PlannerInfo *root, Query *parse, rvcontext.varno = rt_index; /* this flag will be set below, if needed */ rvcontext.wrap_option = REPLACE_WRAP_NONE; + /* LATERAL RTEs are handled by the whole-query pass below */ + rvcontext.process_lateral = false; /* initialize cache array with indexes 0 .. length(tlist) */ rvcontext.rv_cache = palloc0_array(Node *, list_length(tlist) + 1); @@ -624,10 +628,20 @@ expand_virtual_generated_columns(PlannerInfo *root, Query *parse, parse->onConflict->exclRelTlist = NIL; } + /* + * Detach the jointree and expand its quals separately, since FULL + * JOIN quals must use PHVs for variable-free expressions. + */ + save_jointree = parse->jointree; + parse->jointree = NULL; + parse = (Query *) pullup_replace_vars((Node *) parse, &rvcontext); if (parse->onConflict) parse->onConflict->exclRelTlist = save_exclRelTlist; + + parse->jointree = save_jointree; + replace_vars_in_jointree((Node *) parse->jointree, &rvcontext); } return parse; @@ -1644,6 +1658,7 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, rvcontext.varno = varno; /* this flag will be set below, if needed */ rvcontext.wrap_option = REPLACE_WRAP_NONE; + rvcontext.process_lateral = true; /* initialize cache array with indexes 0 .. length(tlist) */ rvcontext.rv_cache = palloc0_array(Node *, list_length(subquery->targetList) + 1); @@ -2191,6 +2206,7 @@ pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte) rvcontext.outer_hasSubLinks = &parse->hasSubLinks; rvcontext.varno = varno; rvcontext.wrap_option = REPLACE_WRAP_NONE; + rvcontext.process_lateral = true; /* initialize cache array with indexes 0 .. length(tlist) */ rvcontext.rv_cache = palloc0_array(Node *, list_length(tlist) + 1); @@ -2358,6 +2374,7 @@ pull_up_constant_function(PlannerInfo *root, Node *jtnode, rvcontext.varno = ((RangeTblRef *) jtnode)->rtindex; /* this flag will be set below, if needed */ rvcontext.wrap_option = REPLACE_WRAP_NONE; + rvcontext.process_lateral = true; /* initialize cache array with indexes 0 .. length(tlist) */ rvcontext.rv_cache = palloc0_array(Node *, list_length(rvcontext.targetlist) + 1); @@ -2738,9 +2755,10 @@ perform_pullup_replace_vars(PlannerInfo *root, } /* - * Helper routine for perform_pullup_replace_vars: do pullup_replace_vars on - * every expression in the jointree, without changing the jointree structure - * itself. Ugly, but there's no other way... + * Helper routine for perform_pullup_replace_vars and + * expand_virtual_generated_columns: do pullup_replace_vars on every expression + * in the jointree, without changing the jointree structure itself. Ugly, but + * there's no other way... */ static void replace_vars_in_jointree(Node *jtnode, @@ -2755,11 +2773,13 @@ replace_vars_in_jointree(Node *jtnode, * same subquery we're pulling up), it might contain references to the * target subquery, which we must replace. We drive this from the * jointree scan, rather than a scan of the rtable, so that we can - * avoid processing no-longer-referenced RTEs. + * avoid processing no-longer-referenced RTEs. The caller can tell us + * not to, if it has already processed them. */ int varno = ((RangeTblRef *) jtnode)->rtindex; - if (varno != context->varno) /* ignore target subquery itself */ + if (context->process_lateral && + varno != context->varno) /* ignore target subquery itself */ { RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable); diff --git a/src/test/regress/expected/generated_virtual.out b/src/test/regress/expected/generated_virtual.out index b14e10fb5a0..d342d4d7326 100644 --- a/src/test/regress/expected/generated_virtual.out +++ b/src/test/regress/expected/generated_virtual.out @@ -1766,6 +1766,18 @@ select * from gtest32 t group by grouping sets (a, b, c, d, e, f) having c = 20; | | 20 | | | (1 row) +-- Ensure that variable-free expressions in FULL JOIN quals are wrapped into PHVs +explain (costs off) +select t1.a, t2.a from gtest32 t1 full join gtest32 t2 on t1.c = t2.a; + QUERY PLAN +------------------------------------ + Hash Full Join + Hash Cond: ((20) = t2.a) + -> Seq Scan on gtest32 t1 + -> Hash + -> Seq Scan on gtest32 t2 +(5 rows) + -- Ensure that the virtual generated columns in ALTER COLUMN TYPE USING expression are expanded alter table gtest32 alter column e type bigint using b; -- Ensure that virtual generated column references within SubLinks that should diff --git a/src/test/regress/sql/generated_virtual.sql b/src/test/regress/sql/generated_virtual.sql index e62880f941a..d1fa4fc2fdc 100644 --- a/src/test/regress/sql/generated_virtual.sql +++ b/src/test/regress/sql/generated_virtual.sql @@ -943,6 +943,10 @@ explain (verbose, costs off) select * from gtest32 t group by grouping sets (a, b, c, d, e, f) having c = 20; select * from gtest32 t group by grouping sets (a, b, c, d, e, f) having c = 20; +-- Ensure that variable-free expressions in FULL JOIN quals are wrapped into PHVs +explain (costs off) +select t1.a, t2.a from gtest32 t1 full join gtest32 t2 on t1.c = t2.a; + -- Ensure that the virtual generated columns in ALTER COLUMN TYPE USING expression are expanded alter table gtest32 alter column e type bigint using b; -- 2.54.0 (Apple Git-157)