From 2226ce1b35b8dccc33edfd8b7d8222ad94ec3bc6 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Thu, 19 Nov 2026 21:28:29 +0900 Subject: [PATCH 5/9] Accept row pattern variables that the lexer turns into lookahead tokens base_yylex() turns NULLS into NULLS_LA when FIRST or LAST follows, FORMAT into FORMAT_LA before JSON, and WITHOUT into WITHOUT_LA before TIME. None of these is a ColId, and in PATTERN two variable names can stand side by side, so PATTERN (nulls first) was a syntax error. Written as PATTERN ("nulls" first) it was accepted, but pg_get_viewdef() printed it back as nulls first, so a view holding it could not be restored from a dump. format json and without time could not be written unquoted either, though their deparsed form re-parsed, since json and time are column-name keywords that deparse always quotes. Take the three lookahead tokens back as variable names in the PATTERN grammar. Deparsing needs no change: nulls first now re-parses as written. --- src/backend/executor/README.rpr | 6 +- src/backend/parser/gram.y | 16 ++++- src/backend/parser/parser.c | 2 + src/test/regress/expected/rpr_base.out | 90 +++++++++++++++++++++++++- src/test/regress/sql/rpr_base.sql | 41 +++++++++++- 5 files changed, 151 insertions(+), 4 deletions(-) diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index 170280fd95d..b4fba5efe08 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -2526,7 +2526,11 @@ A pattern variable is printed by quote_pattern_variable() (ruleutils.c), which is quote_identifier() plus one extra case: the name permute is quoted even though PERMUTE is an unreserved keyword. A bare permute followed by '(' inside a PATTERN would be re-read as the PERMUTE syntax the parser -rejects, so the deparsed text would no longer re-parse. The DEFINE clause +rejects, so the deparsed text would no longer re-parse. The lookahead +tokens NULLS_LA, FORMAT_LA and WITHOUT_LA need no quoting: the grammar takes +them back as the variables nulls, format and without, so nulls first +re-parses as written, as do format "json" and without "time", where the +column-name keywords json and time get their usual quotes. The DEFINE clause has no such hazard, because a name there is always followed by AS; get_rule_define() therefore uses plain quote_identifier(), and one variable can legitimately print bare in DEFINE and quoted in PATTERN. diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 43a279c7fd3..94f27afb155 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -703,6 +703,7 @@ static bool rpr_is_quantifier_token(const char *tok); row_pattern row_pattern_alt row_pattern_seq row_pattern_term row_pattern_primary row_pattern_quantifier_opt +%type row_pattern_var_name %type row_pattern_definition_list row_pattern_permute_list %type opt_row_pattern_skip_to @@ -17187,7 +17188,7 @@ row_pattern_term: ; row_pattern_primary: - ColId + row_pattern_var_name { RPRPatternNode *n = makeNode(RPRPatternNode); n->nodeType = RPR_PATTERN_VAR; @@ -17222,6 +17223,19 @@ row_pattern_primary: } ; +/* + * A pattern variable is any ColId. When FIRST/LAST, JSON or TIME follows, + * base_yylex() turns NULLS, FORMAT or WITHOUT into a lookahead token that is + * not a ColId; take those back as variable names, since two variables can + * sit side by side here. + */ +row_pattern_var_name: + ColId { $$ = $1; } + | NULLS_LA { $$ = pstrdup("nulls"); } + | FORMAT_LA { $$ = pstrdup("format"); } + | WITHOUT_LA { $$ = pstrdup("without"); } + ; + row_pattern_permute_list: row_pattern { $$ = list_make1($1); } | row_pattern_permute_list ',' row_pattern diff --git a/src/backend/parser/parser.c b/src/backend/parser/parser.c index b5c28523971..b5ebec98a24 100644 --- a/src/backend/parser/parser.c +++ b/src/backend/parser/parser.c @@ -92,6 +92,8 @@ raw_parser(const char *str, RawParseMode mode) * This filter is needed because in some cases the standard SQL grammar * requires more than one token lookahead. We reduce these cases to one-token * lookahead by replacing tokens here, in order to keep the grammar LALR(1). + * A replaced token is no longer a ColId, so one made from a non-reserved + * keyword must also be listed in row_pattern_var_name in gram.y. * * Using a filter is simpler than trying to recognize multiword tokens * directly in scan.l, because we'd have to allow for comments between the diff --git a/src/test/regress/expected/rpr_base.out b/src/test/regress/expected/rpr_base.out index 6b30235ed33..a3bc42a3063 100644 --- a/src/test/regress/expected/rpr_base.out +++ b/src/test/regress/expected/rpr_base.out @@ -3337,7 +3337,95 @@ WINDOW w AS ( -> Seq Scan on rpr_permute (6 rows) -DROP VIEW rpr_permute_v, rpr_permute_v2; +-- nulls, format and without are variables even before first, json and time +SELECT id, val, COUNT(*) OVER w AS cnt +FROM rpr_permute +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (nulls first | format json | without time) + DEFINE nulls AS val > 5, first AS val > 0, format AS val > 0, + json AS val > 9, without AS val > 9, time AS val > 0 +); + id | val | cnt +----+-----+----- + 1 | 10 | 0 +(1 row) + +-- Deparse leaves nulls, format and without bare, and the output re-parses +CREATE VIEW rpr_la_v AS + SELECT COUNT(*) OVER w AS cnt FROM rpr_permute + WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (nulls first | nulls last | format json | without time) + DEFINE nulls AS val > 5, first AS val > 0, last AS val > 9, + format AS val > 0, json AS val > 9, without AS val > 9, + time AS val > 0 + ); +SELECT pg_get_viewdef('rpr_la_v'::regclass); + pg_get_viewdef +----------------------------------------------------------------------------- + SELECT count(*) OVER w AS cnt + + FROM rpr_permute + + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING+ + AFTER MATCH SKIP PAST LAST ROW + + INITIAL + + PATTERN (nulls first | nulls last | format "json" | without "time") + + DEFINE + + nulls AS (val > 5), + + first AS (val > 0), + + last AS (val > 9), + + format AS (val > 0), + + "json" AS (val > 9), + + without AS (val > 9), + + "time" AS (val > 0)); +(1 row) + +SELECT 'CREATE VIEW rpr_la_rt AS ' + || pg_get_viewdef('rpr_la_v'::regclass, true) \gexec +CREATE VIEW rpr_la_rt AS SELECT count(*) OVER w AS cnt + FROM rpr_permute + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + INITIAL + PATTERN (nulls first | nulls last | format "json" | without "time") + DEFINE + nulls AS val > 5, + first AS val > 0, + last AS val > 9, + format AS val > 0, + "json" AS val > 9, + without AS val > 9, + "time" AS val > 0); +SELECT pg_get_viewdef('rpr_la_v'::regclass, true) + = pg_get_viewdef('rpr_la_rt'::regclass, true) AS round_trips; + round_trips +------------- + t +(1 row) + +-- EXPLAIN prints them the same way +EXPLAIN (COSTS OFF) +SELECT COUNT(*) OVER w +FROM rpr_permute +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (nulls first | nulls last) + DEFINE nulls AS val > 5, first AS val > 0, last AS val > 9 +); + QUERY PLAN +------------------------------------------------------------------------------- + WindowAgg + Window: w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + Pattern: (nulls first | nulls last) + -> Sort + Sort Key: id + -> Seq Scan on rpr_permute +(6 rows) + +DROP VIEW rpr_permute_v, rpr_permute_v2, rpr_la_v, rpr_la_rt; DROP TABLE rpr_permute; -- ============================================================ -- Serialization/Deserialization Tests diff --git a/src/test/regress/sql/rpr_base.sql b/src/test/regress/sql/rpr_base.sql index ebe0a50a7e3..d6a87ab33c0 100644 --- a/src/test/regress/sql/rpr_base.sql +++ b/src/test/regress/sql/rpr_base.sql @@ -2265,7 +2265,46 @@ WINDOW w AS ( DEFINE PERMUTE AS val > 5, A AS val > 0, B AS val > 9 ); -DROP VIEW rpr_permute_v, rpr_permute_v2; +-- nulls, format and without are variables even before first, json and time +SELECT id, val, COUNT(*) OVER w AS cnt +FROM rpr_permute +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (nulls first | format json | without time) + DEFINE nulls AS val > 5, first AS val > 0, format AS val > 0, + json AS val > 9, without AS val > 9, time AS val > 0 +); + +-- Deparse leaves nulls, format and without bare, and the output re-parses +CREATE VIEW rpr_la_v AS + SELECT COUNT(*) OVER w AS cnt FROM rpr_permute + WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (nulls first | nulls last | format json | without time) + DEFINE nulls AS val > 5, first AS val > 0, last AS val > 9, + format AS val > 0, json AS val > 9, without AS val > 9, + time AS val > 0 + ); +SELECT pg_get_viewdef('rpr_la_v'::regclass); +SELECT 'CREATE VIEW rpr_la_rt AS ' + || pg_get_viewdef('rpr_la_v'::regclass, true) \gexec +SELECT pg_get_viewdef('rpr_la_v'::regclass, true) + = pg_get_viewdef('rpr_la_rt'::regclass, true) AS round_trips; + +-- EXPLAIN prints them the same way +EXPLAIN (COSTS OFF) +SELECT COUNT(*) OVER w +FROM rpr_permute +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (nulls first | nulls last) + DEFINE nulls AS val > 5, first AS val > 0, last AS val > 9 +); + +DROP VIEW rpr_permute_v, rpr_permute_v2, rpr_la_v, rpr_la_rt; DROP TABLE rpr_permute; -- ============================================================ -- 2.54.0 (Apple Git-157)