From bbd298e0befadceaf6a72a96a12218b8a6d23568 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Sat, 21 Nov 2026 16:24:45 +0900 Subject: [PATCH 7/9] Remove PlaceHolderVar wrapping of row pattern navigation arguments A navigation's argument is evaluated only at the row the navigation lands on, and not at all when there is no such row. Subquery pull-up and join alias expansion wrapped a replacement that put a constant in such an argument in a PlaceHolderVar, so that constant folding could not reach it. The wrapping did not cover SQL function inlining or not-null proofs, and it crashed or failed with internal errors when a join alias was expanded below its join level, as when a merged JOIN USING column is passed to an inlined SQL function. Remove it. A substitution into a navigation argument is now treated as it is anywhere else, so a part of the argument that does not depend on the row may be constant-folded at plan time and raise an error, such as division by zero, even when the navigation has no row to land on. This is the limitation that already applies to a CASE arm that is never reached. Drop the XXX note in add_define_inputs_walker() on a duplicate PlaceHolderVar the wrapping made. Keep one pull-up and one join alias test of the wrapping, now showing the plan-time error, and drop the others and a test whose outer PlaceHolderVar only the wrapping produced. Add tests that read a merged JOIN USING column in a navigation argument, most through an inlined SQL function, in the shapes that crashed or failed; an inner join, whose merged column is a plain Var and was never wrapped, is among them for comparison. --- src/backend/executor/README.rpr | 35 +-- src/backend/optimizer/plan/planner.c | 14 - src/backend/optimizer/prep/prepjointree.c | 8 +- src/backend/optimizer/util/var.c | 50 ---- src/backend/rewrite/rewriteManip.c | 27 -- src/include/rewrite/rewriteManip.h | 1 - src/test/regress/expected/rpr_base.out | 89 +------ src/test/regress/expected/rpr_integration.out | 241 ++++++++++++++++-- src/test/regress/sql/rpr_base.sql | 59 +---- src/test/regress/sql/rpr_integration.sql | 136 ++++++++-- 10 files changed, 353 insertions(+), 307 deletions(-) diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index be9a867bd9f..4dc9b7524cc 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -41,9 +41,7 @@ - src/backend/optimizer/plan/subselect.c (DEFINE param sets) - src/backend/optimizer/path/allpaths.c (keeping DEFINE columns) - src/backend/optimizer/path/costsize.c (DEFINE evaluation cost) - - src/backend/optimizer/prep/prepjointree.c (PlaceHolderVar wrapping) - - src/backend/optimizer/util/var.c (join alias expansion) - - src/backend/rewrite/rewriteManip.c (marking navigation args) + - src/backend/optimizer/prep/prepjointree.c (DEFINE pull-up substitution) Executor - src/backend/executor/nodeWindowAgg.c (reduced frame and nav trim) @@ -527,28 +525,7 @@ list must keep its length and its order. rpPattern stays as well. It holds no Vars, an undefined pattern variable simply matches TRUE, and it is what marks the clause as a row pattern window (IV-5). -IV-2. Navigation Arguments, Subquery Pull-up and Join Aliases - -A navigation's argument is evaluated at the row the navigation lands on, not -at the row being tested (VII-3). When subquery pull-up substitutes a -subquery output expression into a DEFINE clause, a replacement that does not -depend on the row -- a constant, or anything built only from values outside -the subquery -- must not simply be folded into that argument, or PREV(x) -would collapse to a value that no longer varies with the row it is read at. - -replace_rte_variables_mutator() (rewriteManip.c) therefore handles RPRNavExpr -itself instead of leaving it to the generic mutator: it sets in_rpr_nav_arg -while it walks arg and restores the previous value before walking offset_arg -and compound_offset_arg, which are ordinary expressions read at the current -row. pullup_replace_vars_callback() (prepjointree.c) adds that flag to the -conditions that make a replacement a wrapping candidate, beside -varnullingrels and the caller's own wrap option. A replacement that is -itself a simple Var or PlaceHolderVar still escapes the wrapper, as does an -expression that contains the subquery's Vars and no non-strict constructs; -this costs nothing since such a replacement already varies with the row. -Anything else -- including the row-independent expression that would -otherwise be folded into the argument -- comes back wrapped in a -PlaceHolderVar. +IV-2. Subquery Pull-up and Join Aliases The pull-up does not reach the DEFINE clause through query_tree_mutator(). perform_pullup_replace_vars() (prepjointree.c) names each part of the upper @@ -560,14 +537,6 @@ field has to list defineClause itself. Join alias expansion substitutes into a DEFINE clause as well: a Var naming a join output column is replaced by the expression behind it. -flatten_join_alias_vars_mutator() (var.c) carries its own copy of the same -protection. It handles RPRNavExpr itself and sets in_rpr_nav_arg over arg -alone, as replace_rte_variables_mutator() does, and while that flag is set -it wraps a replacement in a PlaceHolderVar unless it is a Var or a -PlaceHolderVar of the same level. Unlike pullup_replace_vars_callback(), -it wraps a strict expression over row Vars as well. -It wraps only when it has a PlannerInfo; flatten_join_alias_for_parser() -passes none and gets the replacement unwrapped. IV-3. Preprocessing a DEFINE Clause as a Qual diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 7946065db09..1fb39cda47e 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -6571,20 +6571,6 @@ add_define_inputs_walker(Node *node, PathTarget *input_target) if (node == NULL) return false; - /* - * XXX this list_member() match can miss a DEFINE expression that is - * semantically the same as one already in input_target, producing a - * redundant column below the WindowAgg. Seen with a join-alias - * expression exposed through a pulled-up subquery: flattening it once for - * the target list and once for this DEFINE clause each wraps a fresh, - * independently-numbered PlaceHolderVar around an otherwise identical - * copy (make_placeholder_expr() never checks for an existing equivalent - * one), and PlaceHolderVar's equal() compares that number, not the - * wrapped expression, so the two never match. Wasteful, not known to be - * incorrect. Not specific to DEFINE -- make_group_input_target() can - * build the same kind of duplicate for GROUP BY/HAVING with no RPR - * involved. - */ if (list_member(input_target->exprs, node)) return false; diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c index 9ae100fd8f1..8220d36844d 100644 --- a/src/backend/optimizer/prep/prepjointree.c +++ b/src/backend/optimizer/prep/prepjointree.c @@ -2871,15 +2871,9 @@ pullup_replace_vars_callback(const Var *var, * a Var or PlaceHolderVar that we can just add the nullingrels to). We * also need one if the caller has instructed us that certain expression * replacements need to be wrapped for identification purposes. - * - * A Var below the argument of a row pattern navigation operation needs - * one too, so that a replacement that does not depend on the row is not - * folded in: that argument reads the row the navigation lands on, not - * this one. */ need_phv = (var->varnullingrels != NULL) || - (rcon->wrap_option != REPLACE_WRAP_NONE) || - context->in_rpr_nav_arg; + (rcon->wrap_option != REPLACE_WRAP_NONE); /* * If PlaceHolderVars are needed, we cache the modified expressions in diff --git a/src/backend/optimizer/util/var.c b/src/backend/optimizer/util/var.c index 0e82dcacd4b..5e9eb1e0b83 100644 --- a/src/backend/optimizer/util/var.c +++ b/src/backend/optimizer/util/var.c @@ -68,7 +68,6 @@ typedef struct int sublevels_up; bool possible_sublink; /* could aliases include a SubLink? */ bool inserted_sublink; /* have we inserted a SubLink? */ - bool in_rpr_nav_arg; /* below a row pattern navigation argument? */ } flatten_join_alias_vars_context; static bool pull_varnos_walker(Node *node, @@ -866,7 +865,6 @@ flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node) context.possible_sublink = query->hasSubLinks; /* if hasSubLinks is already true, no need to work hard */ context.inserted_sublink = query->hasSubLinks; - context.in_rpr_nav_arg = false; return flatten_join_alias_vars_mutator(node, &context); } @@ -906,7 +904,6 @@ flatten_join_alias_for_parser(Query *query, Node *node, int sublevels_up) context.possible_sublink = query->hasSubLinks; /* if hasSubLinks is already true, no need to work hard */ context.inserted_sublink = query->hasSubLinks; - context.in_rpr_nav_arg = false; return flatten_join_alias_vars_mutator(node, &context); } @@ -1011,56 +1008,9 @@ flatten_join_alias_vars_mutator(Node *node, if (context->possible_sublink && !context->inserted_sublink) context->inserted_sublink = checkExprHasSubLink(newvar); - /* - * Below a navigation argument, wrap a non-Var/PHV replacement in a - * PlaceHolderVar so it can't be constant-folded away before the - * navigation runs. Unlike pullup_replace_vars_callback(), this also - * wraps a strict expression over Vars. - */ - if (context->in_rpr_nav_arg && context->root != NULL && - !(IsA(newvar, Var) && ((Var *) newvar)->varlevelsup == var->varlevelsup) && - !(IsA(newvar, PlaceHolderVar) && ((PlaceHolderVar *) newvar)->phlevelsup == var->varlevelsup)) - { - Relids phrels = pull_varnos(context->root, newvar); - - if (bms_is_empty(phrels)) - { - phrels = get_relids_for_join(context->query, var->varno); - phrels = bms_del_member(phrels, var->varno); - } - newvar = (Node *) make_placeholder_expr(context->root, - (Expr *) newvar, - phrels); - } - /* Lastly, add any varnullingrels to the replacement expression */ return add_nullingrels_if_needed(context->root, newvar, var); } - if (IsA(node, RPRNavExpr)) - { - /* - * Flag the argument, but not the offsets, as a navigation argument - * (mirrors replace_rte_variables_mutator()'s handling). - */ - RPRNavExpr *nav = (RPRNavExpr *) node; - RPRNavExpr *newnode = makeNode(RPRNavExpr); - bool save_in_rpr_nav_arg = context->in_rpr_nav_arg; - - memcpy(newnode, nav, sizeof(RPRNavExpr)); - - context->in_rpr_nav_arg = true; - newnode->arg = (Expr *) - flatten_join_alias_vars_mutator((Node *) nav->arg, context); - context->in_rpr_nav_arg = save_in_rpr_nav_arg; - - newnode->offset_arg = (Expr *) - flatten_join_alias_vars_mutator((Node *) nav->offset_arg, context); - newnode->compound_offset_arg = (Expr *) - flatten_join_alias_vars_mutator((Node *) nav->compound_offset_arg, - context); - - return (Node *) newnode; - } if (IsA(node, PlaceHolderVar)) { PlaceHolderVar *phv = (PlaceHolderVar *) node; diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c index 0caac225982..7011275f8c6 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -1449,7 +1449,6 @@ replace_rte_variables(Node *node, int target_varno, int sublevels_up, context.callback_arg = callback_arg; context.target_varno = target_varno; context.sublevels_up = sublevels_up; - context.in_rpr_nav_arg = false; /* * We try to initialize inserted_sublink to true if there is no need to @@ -1508,32 +1507,6 @@ replace_rte_variables_mutator(Node *node, } /* otherwise fall through to copy the var normally */ } - else if (IsA(node, RPRNavExpr)) - { - /* - * The argument of a row pattern navigation operation is evaluated at - * the row the navigation lands on, so flag it for the callback. The - * offsets beside it are ordinary expressions. - */ - RPRNavExpr *nav = (RPRNavExpr *) node; - RPRNavExpr *newnode = makeNode(RPRNavExpr); - bool save_in_rpr_nav_arg = context->in_rpr_nav_arg; - - memcpy(newnode, nav, sizeof(RPRNavExpr)); - - context->in_rpr_nav_arg = true; - newnode->arg = (Expr *) - replace_rte_variables_mutator((Node *) nav->arg, context); - context->in_rpr_nav_arg = save_in_rpr_nav_arg; - - newnode->offset_arg = (Expr *) - replace_rte_variables_mutator((Node *) nav->offset_arg, context); - newnode->compound_offset_arg = (Expr *) - replace_rte_variables_mutator((Node *) nav->compound_offset_arg, - context); - - return (Node *) newnode; - } else if (IsA(node, Query)) { /* Recurse into RTE subquery or not-yet-planned sublink subquery */ diff --git a/src/include/rewrite/rewriteManip.h b/src/include/rewrite/rewriteManip.h index a8af1820477..2d15d8f8f95 100644 --- a/src/include/rewrite/rewriteManip.h +++ b/src/include/rewrite/rewriteManip.h @@ -32,7 +32,6 @@ struct replace_rte_variables_context int target_varno; /* RTE index to search for */ int sublevels_up; /* (current) nesting depth */ bool inserted_sublink; /* have we inserted a SubLink? */ - bool in_rpr_nav_arg; /* below a row pattern navigation argument? */ }; typedef enum ReplaceVarsNoMatchOption diff --git a/src/test/regress/expected/rpr_base.out b/src/test/regress/expected/rpr_base.out index a3bc42a3063..cd2894f4686 100644 --- a/src/test/regress/expected/rpr_base.out +++ b/src/test/regress/expected/rpr_base.out @@ -2131,60 +2131,15 @@ WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTER 2 | 0 (2 rows) --- Pulling up the VALUES substitutes 10 for v, which is not what the column --- stood for under a navigation: the argument reads the row the navigation --- lands on, not this one. The replacement is wrapped in a PlaceHolderVar --- rather than folded through. +-- Pulling up the VALUES puts 10 in place of v, and the division is folded at +-- plan time although PREV has no row to land on. WITH t(id, v) AS (VALUES (1, 10)) SELECT id, count(*) OVER w AS cnt FROM t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v IS NULL)); - id | cnt -----+----- - 1 | 0 -(1 row) - --- That wrapping is what keeps this one from raising: the divisor is constant --- but the dividend is not folded through, so the division stands until --- execution, where PREV has no row to navigate to and never reaches it. -WITH t(id, v) AS (VALUES (1, 10)) -SELECT id, count(*) OVER w AS cnt -FROM t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v / 0) > 0); - id | cnt -----+----- - 1 | 0 -(1 row) - --- A pulled-up subquery and a function RTE that folded to a constant reach a --- navigation argument the same way, so both are wrapped as well. -SELECT id, count(*) OVER w AS cnt -FROM (SELECT 1 AS id, 10 AS v) t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v / 0) > 0); - id | cnt -----+----- - 1 | 0 -(1 row) - -SELECT count(*) OVER w AS cnt -FROM abs(-10) AS v WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v / 0) > 0); - cnt ------ - 0 -(1 row) - --- Only the argument is protected. One level outside the navigation the same --- column is replaced and folded as it is anywhere else, and the division --- raises at plan time -- as it does for the same WHERE clause over the same --- one-row VALUES, with no pattern in sight. -WITH t(id, v) AS (VALUES (1, 10)) -SELECT id, count(*) OVER w AS cnt -FROM t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS v / 0 > 0); ERROR: division by zero --- A replacement that still depends on the row is left unwrapped, because it --- is what the column meant at whichever row the navigation lands on. +-- A pulled-up expression that depends on the row is read at the row the +-- navigation lands on. SELECT id, count(*) OVER w AS cnt FROM (SELECT id, val + 1 AS v FROM rpr_nav) t WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING @@ -2198,18 +2153,6 @@ WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING 5 | 0 (5 rows) --- Nesting: the inner navigation's argument is below the outer one's, so the --- column there is wrapped too. -WITH t(id, v) AS (VALUES (1, 10)) -SELECT id, count(*) OVER w AS cnt -FROM t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING - PATTERN (A) DEFINE A AS PREV(LAST(v / 0, 1), 2) > 0); - id | cnt -----+----- - 1 | 0 -(1 row) - -- eval_const_expressions() must perform a few rewrites on every expression it -- is handed -- a CollateExpr becomes a RelabelType, named arguments become -- positional, omitted defaults are inserted -- and the executor depends on @@ -10970,8 +10913,8 @@ WINDOW w AS ( (4 rows) DROP TABLE rpr_join1, rpr_join2; --- A mismatched-type USING merge column used to let the other side's --- pulled-up constant fold into a navigation argument at plan time. +-- A merged USING column built over a pulled-up constant is folded into the +-- navigation argument at plan time as well. CREATE TABLE rpr_join4 (k bigint); INSERT INTO rpr_join4 VALUES (10); SELECT count(*) OVER w AS cnt @@ -10981,25 +10924,7 @@ WINDOW w AS ( PATTERN (A) DEFINE A AS PREV(k / 0) > 0 ); - cnt ------ - 0 -(1 row) - --- Same, but RIGHT JOIN with the constant subquery on its preserved --- (right) side. -SELECT count(*) OVER w AS cnt -FROM rpr_join4 RIGHT JOIN (SELECT 10 AS k) a USING (k) -WINDOW w AS ( - ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING - PATTERN (A) - DEFINE A AS PREV(k / 0) > 0 -); - cnt ------ - 0 -(1 row) - +ERROR: division by zero DROP TABLE rpr_join4; -- Same shape with real per-row data, confirming navigation still sees -- each row's own value. diff --git a/src/test/regress/expected/rpr_integration.out b/src/test/regress/expected/rpr_integration.out index 6fc63620ca1..f7cbe8443b2 100644 --- a/src/test/regress/expected/rpr_integration.out +++ b/src/test/regress/expected/rpr_integration.out @@ -2342,11 +2342,10 @@ SELECT cnt FROM ( (15 rows) DROP TABLE rpr_over1, rpr_over2; --- A DEFINE clause can come to hold a Var, or a PlaceHolderVar, of an outer --- query level, although none may be written in one: inlining a SQL function --- substitutes the call's actual arguments into the body and raises the level --- of what it plants there, and subquery pull-up may wrap that in a --- PlaceHolderVar. Each query below leaves the function's output x unread, +-- A DEFINE clause can come to hold a Var of an outer query level, although +-- none may be written in one: inlining a SQL function substitutes the call's +-- actual arguments into the body and raises the level of what it plants +-- there. Each query below leaves the function's output x unread, -- so its entry is replaced with a NULL, and is followed by the same query -- reading x, which prunes nothing; the counts must agree. CREATE TABLE rpr_up (p int, x int); @@ -2414,30 +2413,226 @@ SELECT max(cnt) FROM rpr_up_h(4); 2 (1 row) --- an outer PlaceHolderVar, which pulling up the subquery that supplies the --- argument puts there in place of the Var: -CREATE FUNCTION rpr_up_n(int) RETURNS TABLE (cnt bigint, x int) +DROP FUNCTION rpr_up_f(int), rpr_up_h(int); +DROP TABLE rpr_up, rpr_drv; +-- A merged JOIN USING column passed to an inlined function and read inside +-- a navigation argument. The join alias is expanded one level below its +-- join, inside the function body. +CREATE TABLE rpr_ja_t (id int, val int); +INSERT INTO rpr_ja_t SELECT g, g * 2 FROM generate_series(1, 6) g; +CREATE TABLE rpr_ja_u (id int); +INSERT INTO rpr_ja_u SELECT generate_series(1, 5); +CREATE TABLE rpr_ja_v (id int); +INSERT INTO rpr_ja_v SELECT generate_series(4, 8); +CREATE TABLE rpr_ja_w (id int); +INSERT INTO rpr_ja_w SELECT generate_series(6, 9); +CREATE TABLE rpr_ja_s (id int); +INSERT INTO rpr_ja_s SELECT generate_series(2, 5); +CREATE TABLE rpr_ja_b (id bigint); +INSERT INTO rpr_ja_b VALUES (1), (2), (-10); +CREATE TABLE rpr_ja_k (k int, y int); +INSERT INTO rpr_ja_k SELECT g, g * 10 FROM generate_series(1, 8) g; +CREATE TABLE rpr_ja_k2 (k int); +INSERT INTO rpr_ja_k2 SELECT generate_series(1, 8); +CREATE TABLE rpr_ja_one (o int); +INSERT INTO rpr_ja_one VALUES (1); +CREATE FUNCTION rpr_ja_f(int) RETURNS TABLE (cnt bigint) LANGUAGE sql STABLE AS $$ - SELECT count(*) OVER w, x FROM rpr_up - WINDOW w AS (ORDER BY p ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING - INITIAL PATTERN (A+) DEFINE A AS PREV(p + $1) > 0) + SELECT count(*) OVER w FROM rpr_ja_t + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + INITIAL PATTERN (A+) DEFINE A AS PREV(val + $1) > 5) $$; -SELECT s.k, max(g.cnt) FROM (SELECT 3 AS k FROM rpr_drv) s, - LATERAL rpr_up_n(s.k) g GROUP BY s.k; - k | max ----+----- - 3 | 5 +CREATE FUNCTION rpr_ja_g(p bigint) RETURNS TABLE (id int, cnt bigint) +LANGUAGE sql STABLE AS $$ + SELECT id, count(*) OVER w FROM rpr_ja_t + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) DEFINE B AS PREV(val + p) > 0) +$$; +CREATE FUNCTION rpr_ja_j(p bigint) RETURNS TABLE (cnt bigint) +LANGUAGE sql STABLE AS $$ + SELECT count(*) OVER w FROM rpr_ja_t JOIN rpr_ja_s USING (id) + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) DEFINE B AS PREV(id + p) > 0) +$$; +CREATE FUNCTION rpr_ja_a(p int) RETURNS TABLE (cnt bigint) +LANGUAGE sql STABLE AS $$ + SELECT count(*) OVER w FROM rpr_ja_t + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) + DEFINE B AS PREV(val) + p > 0 AND NEXT(val + p) > 0) +$$; +-- FULL JOIN, with and without GROUP BY +SELECT j.id, max(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, LATERAL rpr_ja_f(j.id) g +GROUP BY j.id ORDER BY 1; + id | max +----+----- + 1 | 3 + 2 | 4 + 3 | 4 + 4 | 5 + 5 | 5 + 6 | 5 + 7 | 5 + 8 | 5 +(8 rows) + +SELECT j.id, g.cnt +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, LATERAL rpr_ja_f(j.id) g +ORDER BY 1, 2 LIMIT 3; + id | cnt +----+----- + 1 | 0 + 1 | 0 + 1 | 0 +(3 rows) + +-- INNER JOIN, whose merged column is a plain Var +SELECT j.id, max(g.cnt) +FROM (rpr_ja_u JOIN rpr_ja_v USING (id)) j, LATERAL rpr_ja_f(j.id) g +GROUP BY j.id ORDER BY 1; + id | max +----+----- + 4 | 5 + 5 | 5 +(2 rows) + +-- USING columns of different types, FULL and LEFT +SELECT k.id, sum(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_b USING (id)) k, LATERAL rpr_ja_g(k.id) g +GROUP BY k.id ORDER BY 1; + id | sum +-----+----- + -10 | 0 + 1 | 6 + 2 | 6 + 3 | 6 + 4 | 6 + 5 | 6 +(6 rows) + +SELECT k.id, sum(g.cnt) +FROM (rpr_ja_u LEFT JOIN rpr_ja_b USING (id)) k, LATERAL rpr_ja_g(k.id) g +GROUP BY k.id ORDER BY 1; + id | sum +----+----- + 1 | 6 + 2 | 6 + 3 | 6 + 4 | 6 + 5 | 6 +(5 rows) + +-- a Var-free merged column into a function body with its own join +SELECT id, max(g.cnt) +FROM (SELECT 10 AS id) a LEFT JOIN rpr_ja_b USING (id), + LATERAL rpr_ja_j(id) g +GROUP BY id; + id | max +----+----- + 10 | 4 (1 row) -SELECT s.k, max(g.cnt), max(g.x) FROM (SELECT 3 AS k FROM rpr_drv) s, - LATERAL rpr_up_n(s.k) g GROUP BY s.k; - k | max | max ----+-----+----- - 3 | 5 | 106 +-- nested join aliases +SELECT count(g.cnt) +FROM ((rpr_ja_u LEFT JOIN rpr_ja_v USING (id)) + FULL JOIN rpr_ja_w USING (id)) j, + LATERAL rpr_ja_j(j.id) g; + count +------- + 36 (1 row) -DROP FUNCTION rpr_up_f(int), rpr_up_h(int), rpr_up_n(int); -DROP TABLE rpr_up, rpr_drv; +SELECT count(g.cnt) +FROM ((rpr_ja_u FULL JOIN rpr_ja_v USING (id)) + FULL JOIN rpr_ja_w USING (id)) j, + LATERAL rpr_ja_a(j.id) g; + count +------- + 54 +(1 row) + +-- the alias combined with another relation's column +SELECT count(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, rpr_ja_w w, + LATERAL rpr_ja_f(j.id + w.id) g; + count +------- + 192 +(1 row) + +SELECT count(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, LATERAL (SELECT 1 AS c) g0, + LATERAL rpr_ja_f(j.id + g0.c) g; + count +------- + 48 +(1 row) + +-- a SubLink reading the alias, without a function +SELECT id, count(*) OVER w AS c +FROM rpr_ja_u FULL JOIN rpr_ja_v USING (id), + LATERAL (SELECT (SELECT y FROM rpr_ja_k WHERE k = id) AS m + FROM rpr_ja_one) s +WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) DEFINE B AS PREV(m) > 0) +ORDER BY id; + id | c +----+--- + 1 | 8 + 2 | 0 + 3 | 0 + 4 | 0 + 5 | 0 + 6 | 0 + 7 | 0 + 8 | 0 +(8 rows) + +SELECT id, count(*) OVER w AS c +FROM rpr_ja_one p1, rpr_ja_one p2, rpr_ja_one p3, + rpr_ja_u FULL JOIN rpr_ja_v USING (id), + LATERAL (SELECT (SELECT y FROM rpr_ja_k JOIN rpr_ja_k2 USING (k) + WHERE k = id) AS m + FROM rpr_ja_one) s +WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) DEFINE B AS PREV(m) > 0) +ORDER BY id; + id | c +----+--- + 1 | 8 + 2 | 0 + 3 | 0 + 4 | 0 + 5 | 0 + 6 | 0 + 7 | 0 + 8 | 0 +(8 rows) + +-- Two inlined levels between the alias and the navigation: the same rows as +-- the single level above. +CREATE FUNCTION rpr_ja_f2(int) RETURNS TABLE (cnt bigint) +LANGUAGE sql STABLE AS $$ SELECT cnt FROM rpr_ja_f($1) $$; +SELECT j.id, max(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, LATERAL rpr_ja_f2(j.id) g +GROUP BY j.id ORDER BY 1; + id | max +----+----- + 1 | 3 + 2 | 4 + 3 | 4 + 4 | 5 + 5 | 5 + 6 | 5 + 7 | 5 + 8 | 5 +(8 rows) + +DROP FUNCTION rpr_ja_f2(int); +DROP FUNCTION rpr_ja_f(int), rpr_ja_g(bigint), rpr_ja_j(bigint), rpr_ja_a(int); +DROP TABLE rpr_ja_t, rpr_ja_u, rpr_ja_v, rpr_ja_w, rpr_ja_s, rpr_ja_b, + rpr_ja_k, rpr_ja_k2, rpr_ja_one; -- ============================================================ -- B12. RPR + Correlated navigation offsets -- ============================================================ diff --git a/src/test/regress/sql/rpr_base.sql b/src/test/regress/sql/rpr_base.sql index d6a87ab33c0..da1847edd5e 100644 --- a/src/test/regress/sql/rpr_base.sql +++ b/src/test/regress/sql/rpr_base.sql @@ -1522,57 +1522,20 @@ SELECT id, count(*) OVER w AS cnt FROM t WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v IS NULL)); --- Pulling up the VALUES substitutes 10 for v, which is not what the column --- stood for under a navigation: the argument reads the row the navigation --- lands on, not this one. The replacement is wrapped in a PlaceHolderVar --- rather than folded through. +-- Pulling up the VALUES puts 10 in place of v, and the division is folded at +-- plan time although PREV has no row to land on. WITH t(id, v) AS (VALUES (1, 10)) SELECT id, count(*) OVER w AS cnt FROM t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v IS NULL)); - --- That wrapping is what keeps this one from raising: the divisor is constant --- but the dividend is not folded through, so the division stands until --- execution, where PREV has no row to navigate to and never reaches it. -WITH t(id, v) AS (VALUES (1, 10)) -SELECT id, count(*) OVER w AS cnt -FROM t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v / 0) > 0); - --- A pulled-up subquery and a function RTE that folded to a constant reach a --- navigation argument the same way, so both are wrapped as well. -SELECT id, count(*) OVER w AS cnt -FROM (SELECT 1 AS id, 10 AS v) t WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v / 0) > 0); -SELECT count(*) OVER w AS cnt -FROM abs(-10) AS v -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS PREV(v / 0) > 0); - --- Only the argument is protected. One level outside the navigation the same --- column is replaced and folded as it is anywhere else, and the division --- raises at plan time -- as it does for the same WHERE clause over the same --- one-row VALUES, with no pattern in sight. -WITH t(id, v) AS (VALUES (1, 10)) -SELECT id, count(*) OVER w AS cnt -FROM t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A) DEFINE A AS v / 0 > 0); - --- A replacement that still depends on the row is left unwrapped, because it --- is what the column meant at whichever row the navigation lands on. +-- A pulled-up expression that depends on the row is read at the row the +-- navigation lands on. SELECT id, count(*) OVER w AS cnt FROM (SELECT id, val + 1 AS v FROM rpr_nav) t WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (A+) DEFINE A AS PREV(v) > 0); --- Nesting: the inner navigation's argument is below the outer one's, so the --- column there is wrapped too. -WITH t(id, v) AS (VALUES (1, 10)) -SELECT id, count(*) OVER w AS cnt -FROM t -WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING - PATTERN (A) DEFINE A AS PREV(LAST(v / 0, 1), 2) > 0); - -- eval_const_expressions() must perform a few rewrites on every expression it -- is handed -- a CollateExpr becomes a RelabelType, named arguments become -- positional, omitted defaults are inserted -- and the executor depends on @@ -6260,8 +6223,8 @@ WINDOW w AS ( DROP TABLE rpr_join1, rpr_join2; --- A mismatched-type USING merge column used to let the other side's --- pulled-up constant fold into a navigation argument at plan time. +-- A merged USING column built over a pulled-up constant is folded into the +-- navigation argument at plan time as well. CREATE TABLE rpr_join4 (k bigint); INSERT INTO rpr_join4 VALUES (10); @@ -6273,16 +6236,6 @@ WINDOW w AS ( DEFINE A AS PREV(k / 0) > 0 ); --- Same, but RIGHT JOIN with the constant subquery on its preserved --- (right) side. -SELECT count(*) OVER w AS cnt -FROM rpr_join4 RIGHT JOIN (SELECT 10 AS k) a USING (k) -WINDOW w AS ( - ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING - PATTERN (A) - DEFINE A AS PREV(k / 0) > 0 -); - DROP TABLE rpr_join4; -- Same shape with real per-row data, confirming navigation still sees diff --git a/src/test/regress/sql/rpr_integration.sql b/src/test/regress/sql/rpr_integration.sql index 213a72eb59c..ab8101149bd 100644 --- a/src/test/regress/sql/rpr_integration.sql +++ b/src/test/regress/sql/rpr_integration.sql @@ -1411,11 +1411,10 @@ SELECT cnt FROM ( ) s; DROP TABLE rpr_over1, rpr_over2; --- A DEFINE clause can come to hold a Var, or a PlaceHolderVar, of an outer --- query level, although none may be written in one: inlining a SQL function --- substitutes the call's actual arguments into the body and raises the level --- of what it plants there, and subquery pull-up may wrap that in a --- PlaceHolderVar. Each query below leaves the function's output x unread, +-- A DEFINE clause can come to hold a Var of an outer query level, although +-- none may be written in one: inlining a SQL function substitutes the call's +-- actual arguments into the body and raises the level of what it plants +-- there. Each query below leaves the function's output x unread, -- so its entry is replaced with a NULL, and is followed by the same query -- reading x, which prunes nothing; the counts must agree. CREATE TABLE rpr_up (p int, x int); @@ -1452,21 +1451,124 @@ GROUP BY d.k ORDER BY 1; SELECT max(cnt) FROM rpr_up_h(2); SELECT max(cnt) FROM rpr_up_h(4); --- an outer PlaceHolderVar, which pulling up the subquery that supplies the --- argument puts there in place of the Var: -CREATE FUNCTION rpr_up_n(int) RETURNS TABLE (cnt bigint, x int) +DROP FUNCTION rpr_up_f(int), rpr_up_h(int); +DROP TABLE rpr_up, rpr_drv; + +-- A merged JOIN USING column passed to an inlined function and read inside +-- a navigation argument. The join alias is expanded one level below its +-- join, inside the function body. +CREATE TABLE rpr_ja_t (id int, val int); +INSERT INTO rpr_ja_t SELECT g, g * 2 FROM generate_series(1, 6) g; +CREATE TABLE rpr_ja_u (id int); +INSERT INTO rpr_ja_u SELECT generate_series(1, 5); +CREATE TABLE rpr_ja_v (id int); +INSERT INTO rpr_ja_v SELECT generate_series(4, 8); +CREATE TABLE rpr_ja_w (id int); +INSERT INTO rpr_ja_w SELECT generate_series(6, 9); +CREATE TABLE rpr_ja_s (id int); +INSERT INTO rpr_ja_s SELECT generate_series(2, 5); +CREATE TABLE rpr_ja_b (id bigint); +INSERT INTO rpr_ja_b VALUES (1), (2), (-10); +CREATE TABLE rpr_ja_k (k int, y int); +INSERT INTO rpr_ja_k SELECT g, g * 10 FROM generate_series(1, 8) g; +CREATE TABLE rpr_ja_k2 (k int); +INSERT INTO rpr_ja_k2 SELECT generate_series(1, 8); +CREATE TABLE rpr_ja_one (o int); +INSERT INTO rpr_ja_one VALUES (1); +CREATE FUNCTION rpr_ja_f(int) RETURNS TABLE (cnt bigint) LANGUAGE sql STABLE AS $$ - SELECT count(*) OVER w, x FROM rpr_up - WINDOW w AS (ORDER BY p ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING - INITIAL PATTERN (A+) DEFINE A AS PREV(p + $1) > 0) + SELECT count(*) OVER w FROM rpr_ja_t + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + INITIAL PATTERN (A+) DEFINE A AS PREV(val + $1) > 5) +$$; +CREATE FUNCTION rpr_ja_g(p bigint) RETURNS TABLE (id int, cnt bigint) +LANGUAGE sql STABLE AS $$ + SELECT id, count(*) OVER w FROM rpr_ja_t + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) DEFINE B AS PREV(val + p) > 0) +$$; +CREATE FUNCTION rpr_ja_j(p bigint) RETURNS TABLE (cnt bigint) +LANGUAGE sql STABLE AS $$ + SELECT count(*) OVER w FROM rpr_ja_t JOIN rpr_ja_s USING (id) + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) DEFINE B AS PREV(id + p) > 0) +$$; +CREATE FUNCTION rpr_ja_a(p int) RETURNS TABLE (cnt bigint) +LANGUAGE sql STABLE AS $$ + SELECT count(*) OVER w FROM rpr_ja_t + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) + DEFINE B AS PREV(val) + p > 0 AND NEXT(val + p) > 0) $$; -SELECT s.k, max(g.cnt) FROM (SELECT 3 AS k FROM rpr_drv) s, - LATERAL rpr_up_n(s.k) g GROUP BY s.k; -SELECT s.k, max(g.cnt), max(g.x) FROM (SELECT 3 AS k FROM rpr_drv) s, - LATERAL rpr_up_n(s.k) g GROUP BY s.k; -DROP FUNCTION rpr_up_f(int), rpr_up_h(int), rpr_up_n(int); -DROP TABLE rpr_up, rpr_drv; +-- FULL JOIN, with and without GROUP BY +SELECT j.id, max(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, LATERAL rpr_ja_f(j.id) g +GROUP BY j.id ORDER BY 1; +SELECT j.id, g.cnt +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, LATERAL rpr_ja_f(j.id) g +ORDER BY 1, 2 LIMIT 3; +-- INNER JOIN, whose merged column is a plain Var +SELECT j.id, max(g.cnt) +FROM (rpr_ja_u JOIN rpr_ja_v USING (id)) j, LATERAL rpr_ja_f(j.id) g +GROUP BY j.id ORDER BY 1; +-- USING columns of different types, FULL and LEFT +SELECT k.id, sum(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_b USING (id)) k, LATERAL rpr_ja_g(k.id) g +GROUP BY k.id ORDER BY 1; +SELECT k.id, sum(g.cnt) +FROM (rpr_ja_u LEFT JOIN rpr_ja_b USING (id)) k, LATERAL rpr_ja_g(k.id) g +GROUP BY k.id ORDER BY 1; +-- a Var-free merged column into a function body with its own join +SELECT id, max(g.cnt) +FROM (SELECT 10 AS id) a LEFT JOIN rpr_ja_b USING (id), + LATERAL rpr_ja_j(id) g +GROUP BY id; +-- nested join aliases +SELECT count(g.cnt) +FROM ((rpr_ja_u LEFT JOIN rpr_ja_v USING (id)) + FULL JOIN rpr_ja_w USING (id)) j, + LATERAL rpr_ja_j(j.id) g; +SELECT count(g.cnt) +FROM ((rpr_ja_u FULL JOIN rpr_ja_v USING (id)) + FULL JOIN rpr_ja_w USING (id)) j, + LATERAL rpr_ja_a(j.id) g; +-- the alias combined with another relation's column +SELECT count(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, rpr_ja_w w, + LATERAL rpr_ja_f(j.id + w.id) g; +SELECT count(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, LATERAL (SELECT 1 AS c) g0, + LATERAL rpr_ja_f(j.id + g0.c) g; +-- a SubLink reading the alias, without a function +SELECT id, count(*) OVER w AS c +FROM rpr_ja_u FULL JOIN rpr_ja_v USING (id), + LATERAL (SELECT (SELECT y FROM rpr_ja_k WHERE k = id) AS m + FROM rpr_ja_one) s +WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) DEFINE B AS PREV(m) > 0) +ORDER BY id; +SELECT id, count(*) OVER w AS c +FROM rpr_ja_one p1, rpr_ja_one p2, rpr_ja_one p3, + rpr_ja_u FULL JOIN rpr_ja_v USING (id), + LATERAL (SELECT (SELECT y FROM rpr_ja_k JOIN rpr_ja_k2 USING (k) + WHERE k = id) AS m + FROM rpr_ja_one) s +WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) DEFINE B AS PREV(m) > 0) +ORDER BY id; + +-- Two inlined levels between the alias and the navigation: the same rows as +-- the single level above. +CREATE FUNCTION rpr_ja_f2(int) RETURNS TABLE (cnt bigint) +LANGUAGE sql STABLE AS $$ SELECT cnt FROM rpr_ja_f($1) $$; +SELECT j.id, max(g.cnt) +FROM (rpr_ja_u FULL JOIN rpr_ja_v USING (id)) j, LATERAL rpr_ja_f2(j.id) g +GROUP BY j.id ORDER BY 1; +DROP FUNCTION rpr_ja_f2(int); +DROP FUNCTION rpr_ja_f(int), rpr_ja_g(bigint), rpr_ja_j(bigint), rpr_ja_a(int); +DROP TABLE rpr_ja_t, rpr_ja_u, rpr_ja_v, rpr_ja_w, rpr_ja_s, rpr_ja_b, + rpr_ja_k, rpr_ja_k2, rpr_ja_one; -- ============================================================ -- B12. RPR + Correlated navigation offsets -- 2.54.0 (Apple Git-157)