From b73c711674585cd6106cc8149b97e8415c2cda5f Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Wed, 18 Nov 2026 16:11:08 +0900 Subject: [PATCH 6/9] Carry DEFINE inputs through grouping and eager aggregation A row pattern DEFINE clause is evaluated by the WindowAgg above the grouping step, but two places that decide what the Agg input carries looked only at the target list and HAVING. make_group_input_target() left out a column read only by DEFINE that is functionally dependent on a GROUP BY key. create_agg_clause_infos() took a column that DEFINE reads, and that appears elsewhere only inside an aggregate, as used within aggregates only, so eager aggregation dropped it above the partial aggregation. Either way setrefs.c failed with "variable not found in subplan target list" or "... target lists". Call add_define_inputs_walker() in make_group_input_target(), as the window input target does. It stops at an expression the input target already computes, so a grouped expression such as GROUP BY a + b keeps its plan. Collect the Vars of DEFINE clauses in create_agg_clause_infos() as well. --- src/backend/executor/README.rpr | 10 +- src/backend/optimizer/plan/initsplan.c | 26 +- src/backend/optimizer/plan/planner.c | 32 ++- src/backend/optimizer/util/relnode.c | 6 +- src/include/nodes/pathnodes.h | 2 +- src/test/regress/expected/rpr_integration.out | 249 ++++++++++++++++++ src/test/regress/sql/rpr_integration.sql | 143 ++++++++++ 7 files changed, 455 insertions(+), 13 deletions(-) diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index b4fba5efe08..be9a867bd9f 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -654,9 +654,13 @@ insists the two agree; a mismatch is not a wrong answer at runtime but a This is also why make_window_input_target() stops at a subexpression the input target already computes whole. The grouping step produces only that expression, so asking for the Vars underneath it would ask the grouping step -for columns it cannot produce. This is the one rule the havingQual handling -it is modelled on does not need: make_group_input_target() builds the input -of the grouping step, while the window input target sits above it. +for columns it cannot produce. make_group_input_target() calls the same +walker for the DEFINE clause, so a column only DEFINE reads, such as one +functionally dependent on a GROUP BY key, reaches the Agg input; there the +stop only keeps the columns under a grouped expression from being added on +their own. create_agg_clause_infos() (initsplan.c) collects the Vars of +DEFINE clauses with those of the targetlist and havingQual, so eager +aggregation counts them as used outside aggregates. IV-5. Optimizations an RPR Window Is Excluded From diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c index db4938e8e11..cb429e8ba4e 100644 --- a/src/backend/optimizer/plan/initsplan.c +++ b/src/backend/optimizer/plan/initsplan.c @@ -726,7 +726,7 @@ setup_eager_aggregation(PlannerInfo *root) /* * Collect aggregate expressions and plain Vars that appear in the - * targetlist and havingQual. + * targetlist, havingQual and DEFINE clauses. */ create_agg_clause_infos(root); @@ -773,7 +773,8 @@ is_partial_agg_memory_risky(PlannerInfo *root) /* * create_agg_clause_infos * Search the targetlist and havingQual for Aggrefs and plain Vars, and - * create an AggClauseInfo for each Aggref node. + * DEFINE clauses for plain Vars, and create an AggClauseInfo for each + * Aggref node. */ static void create_agg_clause_infos(PlannerInfo *root) @@ -812,6 +813,27 @@ create_agg_clause_infos(PlannerInfo *root) } } + /* + * The WindowAgg above reads DEFINE's columns, so they must not count as + * used only within aggregates. The parser rejects aggregates in DEFINE, + * so plain Vars are all there is to collect. + */ + foreach_node(WindowClause, wc, root->parse->windowClause) + { + List *define_exprs; + + if (wc->defineClause == NIL) + continue; + + define_exprs = pull_var_clause((Node *) wc->defineClause, + PVC_RECURSE_PLACEHOLDERS); + if (define_exprs != NIL) + { + tlist_exprs = list_concat(tlist_exprs, define_exprs); + list_free(define_exprs); + } + } + foreach(lc, tlist_exprs) { Expr *expr = (Expr *) lfirst(lc); diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 8fd89744504..7946065db09 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -5998,6 +5998,29 @@ make_group_input_target(PlannerInfo *root, PathTarget *final_target) } add_new_columns_to_pathtarget(input_target, non_group_vars); + /* + * The WindowAgg above evaluates DEFINE, so bring up the columns it reads, + * stopping at a grouping expression computed whole. grouping_planner() + * has emptied the DEFINE clauses of windows that are not planned. + */ + foreach_node(WindowClause, wc, parse->windowClause) + { + Node *define = (Node *) wc->defineClause; + + if (define == NULL) + continue; + + /* the target is logically below the grouping step, as above */ + if (parse->hasGroupRTE && parse->groupingSets != NIL) + { + Assert(root->group_rtindex > 0); + define = remove_nulling_relids(define, + bms_make_singleton(root->group_rtindex), + NULL); + } + (void) add_define_inputs_walker(define, input_target); + } + /* clean up cruft */ list_free(non_group_vars); list_free(non_group_cols); @@ -6531,15 +6554,16 @@ common_prefix_cmp(const void *a, const void *b) * the join tree, and the node's own input target has to ask for them again * because the upper planner projects through explicit targets rather than * propagating attr_needed. make_group_input_target() does the same for - * havingQual. + * havingQual, and calls this walker directly for the DEFINE clause. * * The walk stops at any expression the target already computes whole, since * setrefs.c resolves the DEFINE copy of it against that column. Stopping * matters rather than merely saving work: under GROUP BY the Vars underneath * a grouping expression are not available on their own, so descending into - * one would ask the grouping step for a column it cannot produce. This is - * the one rule HAVING does not need, the Agg's input target sitting below - * the grouping step rather than above it. + * one would ask the grouping step for a column it cannot produce. Below + * the grouping step, where make_group_input_target() calls it, the same + * stop only keeps the columns under a grouped expression from being added + * on their own next to it. */ static bool add_define_inputs_walker(Node *node, PathTarget *input_target) diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c index 90a687c32da..d67b82a8cbd 100644 --- a/src/backend/optimizer/util/relnode.c +++ b/src/backend/optimizer/util/relnode.c @@ -3257,8 +3257,8 @@ init_grouping_targets(PlannerInfo *root, RelOptInfo *rel, /* * The expression is referenced by an aggregate function pushed * down to this relation and does not appear elsewhere in the - * targetlist or havingQual. Add it to 'agg_input' but not to - * 'target'. + * targetlist, havingQual or a DEFINE clause. Add it to + * 'agg_input' but not to 'target'. */ add_new_column_to_pathtarget(agg_input, expr); } @@ -3321,7 +3321,7 @@ init_grouping_targets(PlannerInfo *root, RelOptInfo *rel, /* * is_var_in_aggref_only * Check whether the given Var appears in aggregate expressions and not - * elsewhere in the targetlist or havingQual. + * elsewhere in the targetlist, havingQual or a DEFINE clause. */ static bool is_var_in_aggref_only(PlannerInfo *root, Var *var) diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index 2ba1dbfb67f..d845aa095e4 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -504,7 +504,7 @@ struct PlannerInfo /* list of GroupingExprInfos */ List *group_expr_list; - /* list of plain Vars contained in targetlist and havingQual */ + /* list of plain Vars contained in targetlist, havingQual and DEFINE */ List *tlist_vars; /* array of PlaceHolderInfos indexed by phid */ diff --git a/src/test/regress/expected/rpr_integration.out b/src/test/regress/expected/rpr_integration.out index e71696e453a..6fc63620ca1 100644 --- a/src/test/regress/expected/rpr_integration.out +++ b/src/test/regress/expected/rpr_integration.out @@ -38,6 +38,7 @@ -- B12. RPR + Correlated navigation offsets -- B13. RPR + DEFINE-only parameter caching -- B14. RPR + Multiple window definitions +-- B15. RPR + DEFINE-only column dependent on the GROUP BY key -- CREATE TABLE rpr_integ (id INT, val INT); INSERT INTO rpr_integ VALUES @@ -2640,6 +2641,254 @@ ORDER BY id; 2 | 0 | 2 (2 rows) +-- ============================================================ +-- B15. RPR + DEFINE-only column dependent on the GROUP BY key +-- ============================================================ +-- A column read only by DEFINE (v) is in neither the target list nor HAVING. +-- When it is functionally dependent on the GROUP BY key (the primary key), +-- the grouping step has to put it into the Agg input so that the WindowAgg +-- above can evaluate DEFINE. +CREATE TABLE rpr_integ_fd (id int PRIMARY KEY, v int); +INSERT INTO rpr_integ_fd SELECT g, (g * 7) % 10 FROM generate_series(1, 6) g; +CREATE TABLE rpr_integ_fd2 (id int, w int); +INSERT INTO rpr_integ_fd2 SELECT g, g FROM generate_series(1, 6) g; +ANALYZE rpr_integ_fd; +ANALYZE rpr_integ_fd2; +-- Grouping one table +SELECT id, count(*) OVER w AS c +FROM rpr_integ_fd +GROUP BY id +WINDOW w AS (ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY id; + id | c +----+--- + 1 | 0 + 2 | 0 + 3 | 2 + 4 | 0 + 5 | 0 + 6 | 0 +(6 rows) + +-- Aggregate over a join +SELECT t.id, sum(u.w) AS sw, count(*) OVER w AS c +FROM rpr_integ_fd t JOIN rpr_integ_fd2 u ON u.id = t.id +GROUP BY t.id +WINDOW w AS (ORDER BY t.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY t.id; + id | sw | c +----+----+--- + 1 | 1 | 0 + 2 | 2 | 0 + 3 | 3 | 2 + 4 | 4 | 0 + 5 | 5 | 0 + 6 | 6 | 0 +(6 rows) + +-- u has no primary key, so u.w is not dependent on t.id +SELECT t.id, count(*) OVER w AS c +FROM rpr_integ_fd t JOIN rpr_integ_fd2 u ON u.id = t.id +GROUP BY t.id +WINDOW w AS (ORDER BY t.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS w > PREV(w)); +ERROR: column "u.w" must appear in the GROUP BY clause or be used in an aggregate function +LINE 7: DEFINE Y AS w > PREV(w)); + ^ +-- Eager aggregation: v appears in the target list only inside an aggregate +CREATE TABLE rpr_integ_ea_c (pid int, x int); +INSERT INTO rpr_integ_ea_c SELECT (g % 6) + 1, g FROM generate_series(1, 6000) g; +CREATE TABLE rpr_integ_ea_d (pid int); +INSERT INTO rpr_integ_ea_d SELECT (g % 6) + 1 FROM generate_series(1, 60) g; +ANALYZE rpr_integ_ea_c; +ANALYZE rpr_integ_ea_d; +EXPLAIN (COSTS OFF) +SELECT p.id, sum(p.v + c.x) AS s, count(*) OVER w AS cnt +FROM rpr_integ_fd p + JOIN rpr_integ_ea_c c ON c.pid = p.id + JOIN rpr_integ_ea_d d ON d.pid = p.id +GROUP BY p.id +WINDOW w AS (ORDER BY p.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY p.id; + QUERY PLAN +--------------------------------------------------------------------------------- + WindowAgg + Window: w AS (ORDER BY p.id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + Pattern: x y+ + Nav Mark Lookback: 1 + -> Sort + Sort Key: p.id + -> Finalize HashAggregate + Group Key: p.id + -> Hash Join + Hash Cond: (d.pid = c.pid) + -> Seq Scan on rpr_integ_ea_d d + -> Hash + -> Partial HashAggregate + Group Key: p.id + -> Hash Join + Hash Cond: (c.pid = p.id) + -> Seq Scan on rpr_integ_ea_c c + -> Hash + -> Seq Scan on rpr_integ_fd p +(19 rows) + +SELECT p.id, sum(p.v + c.x) AS s, count(*) OVER w AS cnt +FROM rpr_integ_fd p + JOIN rpr_integ_ea_c c ON c.pid = p.id + JOIN rpr_integ_ea_d d ON d.pid = p.id +GROUP BY p.id +WINDOW w AS (ORDER BY p.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY p.id; + id | s | cnt +----+----------+----- + 1 | 30100000 | 0 + 2 | 30020000 | 0 + 3 | 30000000 | 2 + 4 | 30080000 | 0 + 5 | 30060000 | 0 + 6 | 30040000 | 0 +(6 rows) + +-- v only in an aggregate FILTER +SELECT p.id, sum(c.x) FILTER (WHERE p.v > 3) AS s, count(*) OVER w AS cnt +FROM rpr_integ_fd p + JOIN rpr_integ_ea_c c ON c.pid = p.id + JOIN rpr_integ_ea_d d ON d.pid = p.id +GROUP BY p.id +WINDOW w AS (ORDER BY p.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY p.id; + id | s | cnt +----+----------+----- + 1 | 30030000 | 0 + 2 | 29980000 | 0 + 3 | | 2 + 4 | 30000000 | 0 + 5 | 30010000 | 0 + 6 | | 0 +(6 rows) + +-- v only in an aggregate in HAVING +SELECT p.id, sum(c.x) AS s, count(*) OVER w AS cnt +FROM rpr_integ_fd p + JOIN rpr_integ_ea_c c ON c.pid = p.id + JOIN rpr_integ_ea_d d ON d.pid = p.id +GROUP BY p.id +HAVING sum(p.v) >= 0 +WINDOW w AS (ORDER BY p.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY p.id; + id | s | cnt +----+----------+----- + 1 | 30030000 | 0 + 2 | 29980000 | 0 + 3 | 29990000 | 2 + 4 | 30000000 | 0 + 5 | 30010000 | 0 + 6 | 30020000 | 0 +(6 rows) + +DROP TABLE rpr_integ_ea_c; +DROP TABLE rpr_integ_ea_d; +DROP TABLE rpr_integ_fd2; +DROP TABLE rpr_integ_fd; +-- DEFINE reads a FULL JOIN USING merged column; only r.id comes up +CREATE TABLE rpr_integ_gl (id int, a int); +CREATE TABLE rpr_integ_gr (pk int PRIMARY KEY, id int, b int); +INSERT INTO rpr_integ_gl VALUES (1, 1), (2, 2), (3, 3), (5, 5); +INSERT INTO rpr_integ_gr VALUES (10, 2, 20), (11, 3, 30), (12, 4, 40); +CREATE VIEW rpr_integ_gv AS +SELECT l.id AS lid, r.pk, count(*) OVER w AS c +FROM rpr_integ_gl l FULL JOIN rpr_integ_gr r USING (id) +GROUP BY l.id, r.pk +WINDOW w AS (ORDER BY l.id NULLS LAST, r.pk + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) + DEFINE A AS id IS NOT NULL); +SELECT * FROM rpr_integ_gv ORDER BY lid NULLS LAST, pk; + lid | pk | c +-----+----+--- + 1 | | 5 + 2 | 10 | 0 + 3 | 11 | 0 + 5 | | 0 + | 12 | 0 +(5 rows) + +SELECT 'CREATE VIEW rpr_integ_gv_rt AS ' + || pg_get_viewdef('rpr_integ_gv'::regclass, true) \gexec +CREATE VIEW rpr_integ_gv_rt AS SELECT l.id AS lid, + r.pk, + count(*) OVER w AS c + FROM rpr_integ_gl l + FULL JOIN rpr_integ_gr r USING (id) + GROUP BY l.id, r.pk + WINDOW w AS (ORDER BY l.id, r.pk ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + INITIAL + PATTERN (a+) + DEFINE + a AS id IS NOT NULL); +SELECT pg_get_viewdef('rpr_integ_gv'::regclass, true) + = pg_get_viewdef('rpr_integ_gv_rt'::regclass, true) AS round_trips; + round_trips +------------- + t +(1 row) + +SELECT * FROM rpr_integ_gv_rt ORDER BY lid NULLS LAST, pk; + lid | pk | c +-----+----+--- + 1 | | 5 + 2 | 10 | 0 + 3 | 11 | 0 + 5 | | 0 + | 12 | 0 +(5 rows) + +DROP VIEW rpr_integ_gv_rt, rpr_integ_gv; +DROP TABLE rpr_integ_gl, rpr_integ_gr; +-- DEFINE reads a column from the nullable side of an outer join +CREATE TABLE rpr_integ_oo (id int PRIMARY KEY); +INSERT INTO rpr_integ_oo SELECT generate_series(1, 4); +CREATE TABLE rpr_integ_oi (id int PRIMARY KEY, v int); +INSERT INTO rpr_integ_oi VALUES (1, 10), (3, 10); +SELECT o.id, count(*) OVER w AS c +FROM rpr_integ_oo o LEFT JOIN rpr_integ_oi i ON i.id = o.id +GROUP BY o.id, i.id +WINDOW w AS (ORDER BY o.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) + DEFINE B AS PREV(v) IS NULL) +ORDER BY o.id; + id | c +----+--- + 1 | 0 + 2 | 2 + 3 | 0 + 4 | 0 +(4 rows) + +DROP TABLE rpr_integ_oo, rpr_integ_oi; -- Cleanup DROP TABLE rpr_integ; DROP TABLE rpr_integ2; diff --git a/src/test/regress/sql/rpr_integration.sql b/src/test/regress/sql/rpr_integration.sql index 6e51eb2d26d..213a72eb59c 100644 --- a/src/test/regress/sql/rpr_integration.sql +++ b/src/test/regress/sql/rpr_integration.sql @@ -38,6 +38,7 @@ -- B12. RPR + Correlated navigation offsets -- B13. RPR + DEFINE-only parameter caching -- B14. RPR + Multiple window definitions +-- B15. RPR + DEFINE-only column dependent on the GROUP BY key -- CREATE TABLE rpr_integ (id INT, val INT); @@ -1597,6 +1598,148 @@ WINDOW w2 AS (PARTITION BY grp ORDER BY id), DEFINE U AS val > PREV(val)) ORDER BY id; +-- ============================================================ +-- B15. RPR + DEFINE-only column dependent on the GROUP BY key +-- ============================================================ +-- A column read only by DEFINE (v) is in neither the target list nor HAVING. +-- When it is functionally dependent on the GROUP BY key (the primary key), +-- the grouping step has to put it into the Agg input so that the WindowAgg +-- above can evaluate DEFINE. + +CREATE TABLE rpr_integ_fd (id int PRIMARY KEY, v int); +INSERT INTO rpr_integ_fd SELECT g, (g * 7) % 10 FROM generate_series(1, 6) g; +CREATE TABLE rpr_integ_fd2 (id int, w int); +INSERT INTO rpr_integ_fd2 SELECT g, g FROM generate_series(1, 6) g; +ANALYZE rpr_integ_fd; +ANALYZE rpr_integ_fd2; + +-- Grouping one table +SELECT id, count(*) OVER w AS c +FROM rpr_integ_fd +GROUP BY id +WINDOW w AS (ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY id; + +-- Aggregate over a join +SELECT t.id, sum(u.w) AS sw, count(*) OVER w AS c +FROM rpr_integ_fd t JOIN rpr_integ_fd2 u ON u.id = t.id +GROUP BY t.id +WINDOW w AS (ORDER BY t.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY t.id; + +-- u has no primary key, so u.w is not dependent on t.id +SELECT t.id, count(*) OVER w AS c +FROM rpr_integ_fd t JOIN rpr_integ_fd2 u ON u.id = t.id +GROUP BY t.id +WINDOW w AS (ORDER BY t.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS w > PREV(w)); + +-- Eager aggregation: v appears in the target list only inside an aggregate +CREATE TABLE rpr_integ_ea_c (pid int, x int); +INSERT INTO rpr_integ_ea_c SELECT (g % 6) + 1, g FROM generate_series(1, 6000) g; +CREATE TABLE rpr_integ_ea_d (pid int); +INSERT INTO rpr_integ_ea_d SELECT (g % 6) + 1 FROM generate_series(1, 60) g; +ANALYZE rpr_integ_ea_c; +ANALYZE rpr_integ_ea_d; + +EXPLAIN (COSTS OFF) +SELECT p.id, sum(p.v + c.x) AS s, count(*) OVER w AS cnt +FROM rpr_integ_fd p + JOIN rpr_integ_ea_c c ON c.pid = p.id + JOIN rpr_integ_ea_d d ON d.pid = p.id +GROUP BY p.id +WINDOW w AS (ORDER BY p.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY p.id; + +SELECT p.id, sum(p.v + c.x) AS s, count(*) OVER w AS cnt +FROM rpr_integ_fd p + JOIN rpr_integ_ea_c c ON c.pid = p.id + JOIN rpr_integ_ea_d d ON d.pid = p.id +GROUP BY p.id +WINDOW w AS (ORDER BY p.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY p.id; + +-- v only in an aggregate FILTER +SELECT p.id, sum(c.x) FILTER (WHERE p.v > 3) AS s, count(*) OVER w AS cnt +FROM rpr_integ_fd p + JOIN rpr_integ_ea_c c ON c.pid = p.id + JOIN rpr_integ_ea_d d ON d.pid = p.id +GROUP BY p.id +WINDOW w AS (ORDER BY p.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY p.id; + +-- v only in an aggregate in HAVING +SELECT p.id, sum(c.x) AS s, count(*) OVER w AS cnt +FROM rpr_integ_fd p + JOIN rpr_integ_ea_c c ON c.pid = p.id + JOIN rpr_integ_ea_d d ON d.pid = p.id +GROUP BY p.id +HAVING sum(p.v) >= 0 +WINDOW w AS (ORDER BY p.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (X Y+) + DEFINE Y AS v > PREV(v)) +ORDER BY p.id; + +DROP TABLE rpr_integ_ea_c; +DROP TABLE rpr_integ_ea_d; +DROP TABLE rpr_integ_fd2; +DROP TABLE rpr_integ_fd; + +-- DEFINE reads a FULL JOIN USING merged column; only r.id comes up +CREATE TABLE rpr_integ_gl (id int, a int); +CREATE TABLE rpr_integ_gr (pk int PRIMARY KEY, id int, b int); +INSERT INTO rpr_integ_gl VALUES (1, 1), (2, 2), (3, 3), (5, 5); +INSERT INTO rpr_integ_gr VALUES (10, 2, 20), (11, 3, 30), (12, 4, 40); +CREATE VIEW rpr_integ_gv AS +SELECT l.id AS lid, r.pk, count(*) OVER w AS c +FROM rpr_integ_gl l FULL JOIN rpr_integ_gr r USING (id) +GROUP BY l.id, r.pk +WINDOW w AS (ORDER BY l.id NULLS LAST, r.pk + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A+) + DEFINE A AS id IS NOT NULL); +SELECT * FROM rpr_integ_gv ORDER BY lid NULLS LAST, pk; +SELECT 'CREATE VIEW rpr_integ_gv_rt AS ' + || pg_get_viewdef('rpr_integ_gv'::regclass, true) \gexec +SELECT pg_get_viewdef('rpr_integ_gv'::regclass, true) + = pg_get_viewdef('rpr_integ_gv_rt'::regclass, true) AS round_trips; +SELECT * FROM rpr_integ_gv_rt ORDER BY lid NULLS LAST, pk; +DROP VIEW rpr_integ_gv_rt, rpr_integ_gv; +DROP TABLE rpr_integ_gl, rpr_integ_gr; + +-- DEFINE reads a column from the nullable side of an outer join +CREATE TABLE rpr_integ_oo (id int PRIMARY KEY); +INSERT INTO rpr_integ_oo SELECT generate_series(1, 4); +CREATE TABLE rpr_integ_oi (id int PRIMARY KEY, v int); +INSERT INTO rpr_integ_oi VALUES (1, 10), (3, 10); +SELECT o.id, count(*) OVER w AS c +FROM rpr_integ_oo o LEFT JOIN rpr_integ_oi i ON i.id = o.id +GROUP BY o.id, i.id +WINDOW w AS (ORDER BY o.id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A B+) + DEFINE B AS PREV(v) IS NULL) +ORDER BY o.id; +DROP TABLE rpr_integ_oo, rpr_integ_oi; + -- Cleanup DROP TABLE rpr_integ; DROP TABLE rpr_integ2; -- 2.54.0 (Apple Git-157)