From 0b4a3f3663d9ccf8c253e74ede0850724e883346 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Wed, 18 Nov 2026 15:13:06 +0900 Subject: [PATCH 9/9] Stop counting a row pattern alternation as a nesting level An alternation keeps no count, yet its branches were compiled one depth below it, leaving a counts[] slot unused. Nested parenthesized alternations reached the depth limit at 127 levels instead of 254. Compile the branches at the alternation's own depth and count only groups in the parse-time depth test. A state follows one branch only, so the branches can share the slots, as sequence siblings already do. Since depth no longer bounds a branch, isUnboundedStart() and isFixedLengthChildren() take a group's body as the elements between its BEGIN and the END that BEGIN links to, and test a group only at its BEGIN. --- doc/src/sgml/ref/select.sgml | 4 +- src/backend/executor/README.rpr | 52 ++++++------ src/backend/optimizer/plan/rpr.c | 110 ++++++++++--------------- src/backend/parser/parse_rpr.c | 6 +- src/test/regress/expected/rpr_base.out | 16 +++- src/test/regress/expected/rpr_nfa.out | 87 +++++++++++++++++++ src/test/regress/sql/rpr_base.sql | 24 +++++- src/test/regress/sql/rpr_nfa.sql | 59 +++++++++++++ 8 files changed, 254 insertions(+), 104 deletions(-) diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index f5e897117ed..a5e0826a2ff 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -1178,9 +1178,7 @@ DEFINE definition_variable_name AS Note that the maximum number of unique pattern variables used in the PATTERN clause is 240. Additionally, the maximum nesting depth of pattern groups - (parentheses) is 254 levels. An alternation costs a level of its own, - so a pattern that nests alternations reaches the limit at about half - as many parentheses. + (parentheses) is 254 levels. The depth is that of the pattern as written, so parentheses count even where they are redundant. The pattern is also limited to 32767 elements, counted after pattern diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index 03dbb32a933..d9bdc4d528b 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -302,8 +302,8 @@ follows: steps below see the final expression form (3) Wraps in a TargetEntry with the variable name set in resname -The depth limit is the planner's (V-2), counted the same way (a GROUP and -an ALT one level each, a SEQ none) but on the tree as written. Phase 1 +The depth limit is the planner's (V-2), counted the same way (a GROUP one +level, an ALT and a SEQ none) but on the tree as written. Phase 1 never deepens a pattern, so nothing stored can fail the depth test later; parentheses that Phase 1 would remove count too. The element limit stays in the planner, because it is defined on the optimized pattern. @@ -843,16 +843,12 @@ to be added to scanRPRPattern() and fillRPRPattern() together, and so does any change to the depth an element is given, which the parse-time depth test in validateRPRPattern() (III-3) must follow as well. -The rule is that a node's own markers sit at the depth the node was -given and its contents one level deeper: BEGIN and END at the group's -depth with the body at depth + 1, ALT and every SEP at the alternation's -depth with each branch at depth + 1. A SEQ adds no level, and a VAR -sits at the depth it was given. counts[] (VI-1) is indexed by depth, so -a group's count lives at its markers' depth and a VAR's at its own; an -alternation keeps no count, but its branches still add a level to -maxDepth. The first element after a group body that is shallower than -the body is the group's END, which is how isUnboundedStart() finds where -the group closes and where isFixedLengthChildren() stops. +The rule is that only a group adds a level: BEGIN and END sit at the +group's depth with the body at depth + 1, and every other element sits +at the depth it was given. counts[] (VI-1) is indexed by depth, so a +group's count lives at its markers' depth and a VAR's at its own. An +alternation keeps no count, and a state follows one branch only, so +its branches share the slots as sequence siblings do. RPRPatternElement struct (16 bytes): @@ -960,10 +956,10 @@ Example: PATTERN (A+ B | C) -- ALT idx varId depth min max next jump Description ------------------------------------------------------------ 0 ALT 0 1 1 1 3 Alternation start - 1 A(0) 1 1 INF 2 -1 Branch 1: A+ - 2 B(1) 1 1 1 6 -1 Branch 1: B -> FIN + 1 A(0) 0 1 INF 2 -1 Branch 1: A+ + 2 B(1) 0 1 1 6 -1 Branch 1: B -> FIN 3 SEP 0 1 1 4 5 Branch 1 terminator - 4 C(2) 1 1 1 6 -1 Branch 2: C -> FIN + 4 C(2) 0 1 1 6 -1 Branch 2: C -> FIN 5 SEP 0 1 1 6 -1 Branch 2 terminator (last) 6 FIN 0 1 1 -1 -1 Pattern completion @@ -988,12 +984,12 @@ Example: PATTERN ((B C)* | A) -- GROUP + ALT combined idx varId depth min max next jump Description -------------------------------------------------------------- 0 ALT 0 1 1 1 5 Alternation start - 1 BEGIN 1 0 INF 2 4 Branch 1: (B C)* -- jump is - 2 B(1) 2 1 1 3 -1 its own END; the skip - 3 C(2) 2 1 1 4 -1 leaves via END.next - 4 END 1 0 INF 8 2 Branch 1 tail -> post-ALT + 1 BEGIN 0 0 INF 2 4 Branch 1: (B C)* -- jump is + 2 B(1) 1 1 1 3 -1 its own END; the skip + 3 C(2) 1 1 1 4 -1 leaves via END.next + 4 END 0 0 INF 8 2 Branch 1 tail -> post-ALT 5 SEP 0 1 1 6 7 Branch 1 terminator - 6 A(0) 1 1 1 8 -1 Branch 2: A -> post-ALT + 6 A(0) 0 1 1 8 -1 Branch 2: A -> post-ALT 7 SEP 0 1 1 8 -1 Branch 2 terminator (last) 8 FIN 0 1 1 -1 -1 Pattern completion @@ -2901,11 +2897,11 @@ B-3. PATTERN (A | B | C) idx varId depth min max next jump ---------------------------------------- 0 ALT 0 1 1 1 2 next -> branch 1, jump -> SEP1 - 1 A 1 1 1 7 -1 branch 1 -> post-ALT + 1 A 0 1 1 7 -1 branch 1 -> post-ALT 2 SEP 0 1 1 3 4 branch 1 term.; next B, jump SEP2 - 3 B 1 1 1 7 -1 branch 2 -> post-ALT + 3 B 0 1 1 7 -1 branch 2 -> post-ALT 4 SEP 0 1 1 5 6 branch 2 term.; next C, jump SEP3 - 5 C 1 1 1 7 -1 branch 3 -> post-ALT + 5 C 0 1 1 7 -1 branch 3 -> post-ALT 6 SEP 0 1 1 7 -1 branch 3 terminator (last) 7 FIN 0 1 1 -1 -1 @@ -2933,9 +2929,9 @@ B-5. PATTERN ((A | B)+? C) ------------------------------------------------------------------- 0 BEGIN 0 1 INF 1 6 RELUCTANT, group start 1 ALT 1 1 1 2 3 next -> branch 1, jump -> SEP1 - 2 A 2 1 1 6 -1 branch 1 -> END + 2 A 1 1 1 6 -1 branch 1 -> END 3 SEP 1 1 1 4 5 branch 1 term.; jump -> SEP2 - 4 B 2 1 1 6 -1 branch 2 -> END + 4 B 1 1 1 6 -1 branch 2 -> END 5 SEP 1 1 1 6 -1 branch 2 terminator (last) 6 END 0 1 INF 7 1 RELUCTANT, group end 7 C 0 1 1 8 -1 @@ -2966,10 +2962,10 @@ B-7. PATTERN ((A+ B | C*)+ D) -- Per-branch absorption in ALT --------------------------------------------------------------------------- 0 BEGIN 0 1 INF 1 7 ABSORBABLE_BRANCH 1 ALT 1 1 1 2 4 ABSORBABLE_BRANCH; jump -> SEP1 - 2 A 2 1 INF 3 -1 ABSORBABLE | ABSORBABLE_BRANCH - 3 B 2 1 1 7 -1 branch 1 -> END + 2 A 1 1 INF 3 -1 ABSORBABLE | ABSORBABLE_BRANCH + 3 B 1 1 1 7 -1 branch 1 -> END 4 SEP 1 1 1 5 6 branch 1 term.; jump -> SEP2 - 5 C 2 0 INF 7 -1 ABSORBABLE | ABSORBABLE_BRANCH + 5 C 1 0 INF 7 -1 ABSORBABLE | ABSORBABLE_BRANCH 6 SEP 1 1 1 7 -1 branch 2 terminator (last) 7 END 0 1 INF 8 1 EMPTY_LOOP 8 D 0 1 1 9 -1 diff --git a/src/backend/optimizer/plan/rpr.c b/src/backend/optimizer/plan/rpr.c index bd3f9b38752..68fc2dbb646 100644 --- a/src/backend/optimizer/plan/rpr.c +++ b/src/backend/optimizer/plan/rpr.c @@ -1213,13 +1213,13 @@ scanRPRPatternRecursive(RPRPatternNode *node, char **varNames, int *numVars, /* Count ALT start element */ (*numElements)++; - /* Recurse into children at increased depth */ + /* Recurse into branches at the same depth */ foreach_node(RPRPatternNode, child, node->children) { /* Each branch is terminated by a SEP branch-separator marker */ (*numElements)++; scanRPRPatternRecursive(child, varNames, - numVars, numElements, depth + 1, maxDepth); + numVars, numElements, depth, maxDepth); } break; } @@ -1448,7 +1448,7 @@ fillRPRPatternGroup(RPRPatternNode *node, RPRPattern *pat, int *idx, RPRDepth de * fillRPRPatternAlt * Fill an ALT pattern and its alternatives. * - * Creates the ALT marker and fills each alternative at increased depth, + * Creates the ALT marker and fills each alternative at the ALT's own depth, * terminating every alternative (including the last) with a SEP * branch-separator marker. The branch link runs from ALT through the SEP * chain, never through the branch content: a branch's first element may @@ -1505,7 +1505,7 @@ fillRPRPatternAlt(RPRPatternNode *node, RPRPattern *pat, int *idx, RPRDepth dept RPRElemFlags branchFlags; altBranchStarts = lappend_int(altBranchStarts, branchStart); - branchFlags = fillRPRPattern(alt, pat, idx, depth + 1); + branchFlags = fillRPRPattern(alt, pat, idx, depth); /* nullable if ANY branch is; empty-preferred per the FIRST branch */ altFlags |= (branchFlags & RPR_ELEM_EMPTY_LOOP); @@ -1520,7 +1520,7 @@ fillRPRPatternAlt(RPRPatternNode *node, RPRPattern *pat, int *idx, RPRDepth dept sep = &pat->elements[*idx]; memset(sep, 0, sizeof(RPRPatternElement)); sep->varId = RPR_VARID_SEP; - sep->depth = depth; /* ALT-level boundary, not branch-level */ + sep->depth = depth; sep->min = 1; sep->max = 1; sep->next = RPR_ELEMIDX_INVALID; @@ -1763,37 +1763,31 @@ finalizeRPRPattern(RPRPattern *result) /* * isFixedLengthChildren - * Check if every element in elem's scope has a fixed-length - * quantifier (min == max), nested subgroups included. + * Check if every element in the body of the group whose BEGIN is given + * has a fixed-length quantifier (min == max), nested subgroups included. * * A fixed-length group is semantically equivalent to unrolling each child * to {1,1} copies, which is the existing Case 2 already proven correct * for absorption. This check recognizes fixed-length groups at compile * time without actually unrolling them. * - * Walks the next chain from elem to the element that closes the enclosing - * group, testing min == max on everything it passes. A group's quantifier - * sits on its BEGIN as well as on its END, so one test per element covers - * a nested subgroup as well, and the walk needs no notion of how deeply - * that subgroup nests: a GROUP{1,1} that tryUnwrapGroup() has not removed - * emits no markers at all, and its children still have to be fixed-length - * for the region to be. ALT elements are rejected (alternation inside an - * absorbable group is not supported). + * The body is the run of elements between BEGIN and the END it links to, + * and is tested in array order. A group's quantifier sits on its BEGIN as + * well as on its END, so one test per element covers a nested subgroup as + * well. ALT elements are rejected (alternation inside an absorbable group + * is not supported). * - * Returns true if every element in the scope is fixed-length. + * Returns true if every element in the body is fixed-length. */ static bool -isFixedLengthChildren(RPRPattern *pattern, RPRPatternElement *elem) +isFixedLengthChildren(RPRPattern *pattern, RPRPatternElement *begin) { - RPRDepth scopeDepth = elem->depth; + RPRPatternElement *end = &pattern->elements[begin->jump]; - /* FIN bounds the walk where depth cannot, as in isUnboundedStart() */ - for (; elem->depth >= scopeDepth && !RPRElemIsFin(elem); - elem = &pattern->elements[elem->next]) - { - /* FIN is the only element without a successor, and it stopped us */ - Assert(elem->next != RPR_ELEMIDX_INVALID); + Assert(RPRElemIsBegin(begin) && RPRElemIsEnd(end)); + for (RPRPatternElement *elem = begin + 1; elem < end; elem++) + { /* Alternation inside an absorbable group is not supported */ if (RPRElemIsAlt(elem)) return false; @@ -1807,7 +1801,8 @@ isFixedLengthChildren(RPRPattern *pattern, RPRPatternElement *elem) /* * isUnboundedStart - * Check if elem starts an unbounded greedy sequence. + * Check if elem, a VAR or a group's BEGIN, starts an unbounded greedy + * sequence. * * For context absorption to work, the sequence starting at elem must be: * - Unbounded (max = infinity) @@ -1845,12 +1840,15 @@ isFixedLengthChildren(RPRPattern *pattern, RPRPatternElement *elem) static bool isUnboundedStart(RPRPattern *pattern, RPRPatternElement *elem) { - RPRDepth startDepth = elem->depth; + RPRPatternElement *endElem; RPRPatternElement *e; - /* Case 1: Simple unbounded VAR at start (greedy only) */ - if (RPRElemIsVar(elem) && RPRElemIsUnbounded(elem) && - !RPRElemIsReluctant(elem)) + /* Either case needs an unbounded greedy quantifier on elem itself */ + if (!RPRElemIsUnbounded(elem) || RPRElemIsReluctant(elem)) + return false; + + /* Case 1: Simple unbounded VAR at start */ + if (RPRElemIsVar(elem)) { /* Set both flags on first element */ elem->flags |= RPR_ELEM_ABSORBABLE_BRANCH | RPR_ELEM_ABSORBABLE; @@ -1858,42 +1856,25 @@ isUnboundedStart(RPRPattern *pattern, RPRPatternElement *elem) } /* - * Case 2: Unbounded GROUP with fixed-length children. Each child must - * have min == max (recursively for nested subgroups), ensuring a fixed - * step size per iteration so that count-dominance holds. + * Case 2: Unbounded GROUP with fixed-length children; elem is its BEGIN, + * which carries the group's quantifier and links to its END. Each child + * must have min == max (recursively for nested subgroups), ensuring a + * fixed step size per iteration so that count-dominance holds. */ + Assert(RPRElemIsBegin(elem)); if (!isFixedLengthChildren(pattern, elem)) return false; - /* - * Find the END that closes the group, at startDepth - 1. Group markers - * sit at their parent's depth, so the first element shallower than - * startDepth is that END, not a nested subgroup's, which sits at - * startDepth. FIN bounds the walk where depth cannot: at startDepth == 0 - * nothing is shallower and FIN's next would walk off the array. - */ - e = elem; - while (e->depth >= startDepth && !RPRElemIsFin(e)) - e = &pattern->elements[e->next]; - - /* END must be unbounded greedy */ - if (e->depth == startDepth - 1 && - RPRElemIsEnd(e) && RPRElemIsUnbounded(e) && - !RPRElemIsReluctant(e)) - { - RPRPatternElement *endElem = e; - - /* END points back to first child */ - Assert(&pattern->elements[e->jump] == elem); - - /* Set ABSORBABLE_BRANCH on all children, ABSORBABLE on END only */ - for (e = elem; e != endElem; e = &pattern->elements[e->next]) - e->flags |= RPR_ELEM_ABSORBABLE_BRANCH; - endElem->flags |= RPR_ELEM_ABSORBABLE_BRANCH | RPR_ELEM_ABSORBABLE; - return true; - } + endElem = &pattern->elements[elem->jump]; + Assert(RPRElemIsEnd(endElem) && RPRElemIsUnbounded(endElem) && + !RPRElemIsReluctant(endElem)); + Assert(endElem->jump == elem->next); - return false; + /* Set ABSORBABLE_BRANCH on all children, ABSORBABLE on END only */ + for (e = elem + 1; e < endElem; e++) + e->flags |= RPR_ELEM_ABSORBABLE_BRANCH; + endElem->flags |= RPR_ELEM_ABSORBABLE_BRANCH | RPR_ELEM_ABSORBABLE; + return true; } /* @@ -1959,12 +1940,11 @@ computeAbsorbabilityRecursive(RPRPattern *pattern, return; /* - * BEGIN: first try to treat this BEGIN's children as an unbounded - * group directly (handles nested fixed-length groups like ((A{2} - * B{3}){2})+). If that fails, skip to first child and recurse as - * before. + * BEGIN: first try to treat this group as an unbounded group with + * fixed-length children (handles nested fixed-length groups like + * ((A{2} B{3}){2})+). If that fails, recurse into the first child. */ - if (isUnboundedStart(pattern, &pattern->elements[elem->next])) + if (isUnboundedStart(pattern, elem)) { *hasAbsorbable = true; elem->flags |= RPR_ELEM_ABSORBABLE_BRANCH; diff --git a/src/backend/parser/parse_rpr.c b/src/backend/parser/parse_rpr.c index 3632ebdbc6f..adf96d3a5b7 100644 --- a/src/backend/parser/parse_rpr.c +++ b/src/backend/parser/parse_rpr.c @@ -227,14 +227,14 @@ validateRPRPattern(ParseState *pstate, RPRPatternNode *node, int depth, break; case RPR_PATTERN_SEQ: - /* A sequence adds no level */ + case RPR_PATTERN_ALT: + /* Neither adds a level */ foreach_node(RPRPatternNode, child, node->children) validateRPRPattern(pstate, child, depth, varNames); break; - case RPR_PATTERN_ALT: case RPR_PATTERN_GROUP: - /* An alternation and a group each add one */ + /* A group adds one */ foreach_node(RPRPatternNode, child, node->children) validateRPRPattern(pstate, child, depth + 1, varNames); break; diff --git a/src/test/regress/expected/rpr_base.out b/src/test/regress/expected/rpr_base.out index cd2894f4686..da0c9b10ebb 100644 --- a/src/test/regress/expected/rpr_base.out +++ b/src/test/regress/expected/rpr_base.out @@ -12518,7 +12518,7 @@ ERROR: pattern nesting too deep LINE 6: ...((((((((((((((((((((((((((((((((((((((((((((((((((A)))))))))... ^ DETAIL: Pattern nesting depth 255 exceeds maximum 254. --- A parenthesized alternation costs two levels: 127 nested pass, 128 fail. +-- An alternation costs no level: 254 nested pass, 255 fail. \set ECHO none QUERY PLAN ----------------------------------------------------------------------------------------------- @@ -12531,7 +12531,19 @@ DETAIL: Pattern nesting depth 255 exceeds maximum 254. (6 rows) ERROR: pattern nesting too deep -LINE 6: ...A | (A | (A | (A | (A | (A | (A | (A | (A | (A | (A | (A))))... +LINE 6: ...A | (A | (A | (A | (A | (A | (A | (A | (A | (A | (A)))))))))... + ^ +DETAIL: Pattern nesting depth 255 exceeds maximum 254. +-- Merging (A | B) (A | B) into (A | B){2} keeps the depth: 253 pass, 254 fail. +\set ECHO none + id | count +----+------- + 1 | 0 + 2 | 0 +(2 rows) + +ERROR: pattern nesting too deep +LINE 6: ...((((((((((((((((((((((((((((((((((((((((((((((((((A | B) (A ... ^ DETAIL: Pattern nesting depth 255 exceeds maximum 254. DROP TABLE rpr_errors; diff --git a/src/test/regress/expected/rpr_nfa.out b/src/test/regress/expected/rpr_nfa.out index f36ee926b6d..57ee5b6c41e 100644 --- a/src/test/regress/expected/rpr_nfa.out +++ b/src/test/regress/expected/rpr_nfa.out @@ -508,6 +508,93 @@ WINDOW w AS ( 8 | {D} | | (8 rows) +-- Alternation branches share one depth slot: A+ and B{2,3} count in it +WITH test_alt_slot_var AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['A']), + (3, ARRAY['C']), + (4, ARRAY['B']), + (5, ARRAY['B']), + (6, ARRAY['C']), + (7, ARRAY['B']), + (8, ARRAY['B']), + (9, ARRAY['B']), + (10, ARRAY['C']) + ) AS t(id, flags) +) +SELECT id, flags, first_value(id) OVER w AS match_start, last_value(id) OVER w AS match_end +FROM test_alt_slot_var +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A+ | B{2,3}) C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {A} | 1 | 3 + 2 | {A} | | + 3 | {C} | | + 4 | {B} | 4 | 6 + 5 | {B} | | + 6 | {C} | | + 7 | {B} | 7 | 10 + 8 | {B} | | + 9 | {B} | | + 10 | {C} | | +(10 rows) + +-- A group in a branch counts in the branch's slot, inside an outer group +WITH test_alt_slot_group AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['D']), + (3, ARRAY['B']), + (4, ARRAY['C']), + (5, ARRAY['B']), + (6, ARRAY['C']), + (7, ARRAY['D']), + (8, ARRAY['A']), + (9, ARRAY['D']), + (10, ARRAY['B']), + (11, ARRAY['C']), + (12, ARRAY['D']) + ) AS t(id, flags) +) +SELECT id, flags, first_value(id) OVER w AS match_start, last_value(id) OVER w AS match_end +FROM test_alt_slot_group +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN (((A | (B C){2,3}) D)+) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags), + D AS 'D' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {A} | 1 | 9 + 2 | {D} | | + 3 | {B} | | + 4 | {C} | | + 5 | {B} | | + 6 | {C} | | + 7 | {D} | | + 8 | {A} | | + 9 | {D} | | + 10 | {B} | | + 11 | {C} | | + 12 | {D} | | +(12 rows) + -- Fixed-length group absorption: (A B{2})+ C -- B{2} has min == max, equivalent to unrolling to (A B B)+ C WITH test_absorb_fixedlen AS ( diff --git a/src/test/regress/sql/rpr_base.sql b/src/test/regress/sql/rpr_base.sql index da1847edd5e..40a04f39cf5 100644 --- a/src/test/regress/sql/rpr_base.sql +++ b/src/test/regress/sql/rpr_base.sql @@ -7373,7 +7373,7 @@ SELECT format($$CREATE VIEW rpr_deep_v AS SELECT count(*) OVER w repeat('(', 255), repeat(')', 255)) \gexec \set ECHO all --- A parenthesized alternation costs two levels: 127 nested pass, 128 fail. +-- An alternation costs no level: 254 nested pass, 255 fail. \set ECHO none SELECT format($$EXPLAIN (COSTS OFF) SELECT count(*) OVER w FROM (VALUES @@ -7381,14 +7381,32 @@ SELECT format($$EXPLAIN (COSTS OFF) SELECT count(*) OVER w (2)) AS t(id) WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (%sA%s) DEFINE A AS id > 0)$$, - repeat('A | (', 127), repeat(')', 127)) \gexec + repeat('A | (', 254), repeat(')', 254)) \gexec SELECT format($$EXPLAIN (COSTS OFF) SELECT count(*) OVER w FROM (VALUES (1), (2)) AS t(id) WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN (%sA%s) DEFINE A AS id > 0)$$, - repeat('A | (', 128), repeat(')', 128)) \gexec + repeat('A | (', 255), repeat(')', 255)) \gexec +\set ECHO all + +-- Merging (A | B) (A | B) into (A | B){2} keeps the depth: 253 pass, 254 fail. +\set ECHO none +SELECT format($$SELECT id, count(*) OVER w + FROM (VALUES + (1, 1), + (2, 2)) AS t(id, v) + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (%s(A | B) (A | B)%s) DEFINE A AS v = 1, B AS v = 2)$$, + repeat('(', 253), repeat('){2,3}', 253)) \gexec +SELECT format($$SELECT id, count(*) OVER w + FROM (VALUES + (1, 1), + (2, 2)) AS t(id, v) + WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (%s(A | B) (A | B)%s) DEFINE A AS v = 1, B AS v = 2)$$, + repeat('(', 254), repeat('){2,3}', 254)) \gexec \set ECHO all DROP TABLE rpr_errors; diff --git a/src/test/regress/sql/rpr_nfa.sql b/src/test/regress/sql/rpr_nfa.sql index e7e3f5429e1..e7cad2a8e52 100644 --- a/src/test/regress/sql/rpr_nfa.sql +++ b/src/test/regress/sql/rpr_nfa.sql @@ -397,6 +397,65 @@ WINDOW w AS ( D AS 'D' = ANY(flags) ); +-- Alternation branches share one depth slot: A+ and B{2,3} count in it +WITH test_alt_slot_var AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['A']), + (3, ARRAY['C']), + (4, ARRAY['B']), + (5, ARRAY['B']), + (6, ARRAY['C']), + (7, ARRAY['B']), + (8, ARRAY['B']), + (9, ARRAY['B']), + (10, ARRAY['C']) + ) AS t(id, flags) +) +SELECT id, flags, first_value(id) OVER w AS match_start, last_value(id) OVER w AS match_end +FROM test_alt_slot_var +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A+ | B{2,3}) C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +-- A group in a branch counts in the branch's slot, inside an outer group +WITH test_alt_slot_group AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['D']), + (3, ARRAY['B']), + (4, ARRAY['C']), + (5, ARRAY['B']), + (6, ARRAY['C']), + (7, ARRAY['D']), + (8, ARRAY['A']), + (9, ARRAY['D']), + (10, ARRAY['B']), + (11, ARRAY['C']), + (12, ARRAY['D']) + ) AS t(id, flags) +) +SELECT id, flags, first_value(id) OVER w AS match_start, last_value(id) OVER w AS match_end +FROM test_alt_slot_group +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN (((A | (B C){2,3}) D)+) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags), + D AS 'D' = ANY(flags) +); + -- Fixed-length group absorption: (A B{2})+ C -- B{2} has min == max, equivalent to unrolling to (A B B)+ C WITH test_absorb_fixedlen AS ( -- 2.54.0 (Apple Git-157)