From abdf12a8fc85136662b287d9b5ce4401cb75ade7 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Sun, 15 Nov 2026 14:44:14 +0900 Subject: [PATCH 3/9] Fix wrong row pattern matches when a group follows a copy of itself When an element X is directly followed by a group whose body is also X, pattern optimization folds X into the group, turning X X{0,M} into X{1,M+1}. When the group can be skipped, has a finite upper bound and X can match empty, this changes the matches. X becomes the first iteration of the merged loop, and an empty first iteration there ends the loop under ISO/IEC 19075-5 7.2.8, while the written pattern can still go on to the group's own first iteration. Over rows {B},{A,C},{C}, PATTERN ((A | B??) ((A | B??))? C) matched rows 1-3 instead of rows 1-2 and 3-3; Trino and Perl agree with the standard. Do not fold into such a group. A group with a lower bound of at least 1, or with no upper bound, still folds: an empty first iteration stays below the bound in the first case, and in the second X X* and X+ both continue with X* after a consuming iteration. Content that cannot match empty folds as before. One fold raises the lower bound to 1, so only the first fold needs the check. New regression tests cover the case above, the same pattern with the fold blocked by renaming, a {1,2} and a * group, a body that cannot match empty, and one more shape, B* A?? (B* A??)? B, with its renamed and {1,2} forms. --- src/backend/executor/README.rpr | 10 +- src/backend/optimizer/plan/rpr.c | 83 +++++- src/test/regress/expected/rpr_base.out | 349 +++++++++++++++++++++++++ src/test/regress/sql/rpr_base.sql | 265 +++++++++++++++++++ 4 files changed, 694 insertions(+), 13 deletions(-) diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index 646bdfa0b56..c2a953c962e 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -796,10 +796,14 @@ error. (A | B) (A | B) (A | B) -> (A | B){3} (e) Prefix/suffix merging: Merge an identical sequence before or after - a group. A prefix copy is mandatory and comes before the group, so - it always merges; a suffix copy merges only when the body consumes a - fixed number of rows. + a group. A prefix copy merges unless the group has min 0, a finite + max and content that can match empty; a suffix copy merges only when + the body consumes a fixed number of rows. A B (A B)+ -> (A B){2,INF} + (A | B??) ((A | B??))* -> (A | B??)+ + (A | B??) ((A | B??))? stays as-is (an empty first iteration would + end the merged loop and lose the path where the group's own first + iteration consumes rows) (f) ALT flattening and deduplication (A | (B | C)) -> (A | B | C) diff --git a/src/backend/optimizer/plan/rpr.c b/src/backend/optimizer/plan/rpr.c index c5d18b615af..bd3f9b38752 100644 --- a/src/backend/optimizer/plan/rpr.c +++ b/src/backend/optimizer/plan/rpr.c @@ -50,6 +50,8 @@ static int64 rprBodyRowCount(List *children); static bool rprBodyHasUniformLength(List *children); static bool rprChildrenMatchAt(List *children, int start, List *content); static List *rprGroupContent(RPRPatternNode *group); +static bool rprNodeIsNullable(RPRPatternNode *node); +static bool rprBodyIsNullable(List *children); static bool rprTryAddIteration(RPRPatternNode *group); pg_nodiscard static List *flattenSeqChildren(List *children); pg_nodiscard static List *mergeConsecutiveVars(List *children); @@ -234,6 +236,59 @@ rprGroupContent(RPRPatternNode *group) return content; } +/* + * rprNodeIsNullable + * Can node match zero rows? + * + * A VAR can when its quantifier allows zero repetitions, a GROUP when it can + * be skipped or its body can match empty, a SEQ when every child can, and an + * ALT when any branch can. This is the parse-tree twin of the + * RPR_ELEM_EMPTY_LOOP flag the fill pass derives later. + */ +static bool +rprNodeIsNullable(RPRPatternNode *node) +{ + check_stack_depth(); + + switch (node->nodeType) + { + case RPR_PATTERN_VAR: + return node->min == 0; + + case RPR_PATTERN_GROUP: + return node->min == 0 || rprBodyIsNullable(node->children); + + case RPR_PATTERN_SEQ: + return rprBodyIsNullable(node->children); + + case RPR_PATTERN_ALT: + foreach_node(RPRPatternNode, branch, node->children) + { + if (rprNodeIsNullable(branch)) + return true; + } + return false; + } + + pg_unreachable(); + return false; +} + +/* + * rprBodyIsNullable + * Can children, taken in order, match zero rows? + */ +static bool +rprBodyIsNullable(List *children) +{ + foreach_node(RPRPatternNode, child, children) + { + if (!rprNodeIsNullable(child)) + return false; + } + return true; +} + /* * rprTryAddIteration * Raise a GROUP's quantifier by one iteration, if that is representable. @@ -550,13 +605,17 @@ mergeConsecutiveAlts(List *children) * (A B)+ A B -> (A B){2,} * A B (A B)+ A B -> (A B){3,} * - * The two phases are not equally safe. A prefix copy is mandatory and comes - * before the group, exactly like the leading mandatory iterations it becomes, - * so the decision trees stay isomorphic for any content. A suffix copy comes - * after the group has already decided to stop, which the merged form defers - * until after the last iteration's own choices. Merge a suffix only when the - * content consumes a fixed number of rows, which leaves it nothing to decide; - * see mergeConsecutiveGroups for why that condition is the right one. + * The two phases are not equally safe. A prefix copy becomes the merged + * loop's first iteration, so do not fold it when the group has min 0, a + * finite max and content that can match empty: an empty first iteration + * would end the merged loop (ISO/IEC 19075-5 7.2.8) where the written + * pattern still tries the group. See README.rpr V-3 (e). + * + * A suffix copy comes after the group has already decided to stop, which the + * merged form defers until after the last iteration's own choices. Merge a + * suffix only when the content consumes a fixed number of rows, which leaves + * it nothing to decide; see mergeConsecutiveGroups for why that condition is + * the right one. */ static List * mergeGroupPrefixSuffix(List *children) @@ -572,8 +631,8 @@ mergeGroupPrefixSuffix(List *children) * and a prefix of the one after, and this order hands it to the second, * which is the safer of the two rules: a mandatory copy before a group * already sits where the leading iterations it becomes would sit, so - * folding it as a prefix holds for any content, while folding one as a - * suffix needs a fixed-length body. + * folding it as a prefix needs only the min-0 check in the header + * comment, while folding one as a suffix needs a fixed-length body. * * A prefix copy is one of the survivors already stored, so folding it is * a step back of the write cursor. @@ -590,7 +649,11 @@ mergeGroupPrefixSuffix(List *children) List *content = rprGroupContent(child); int content_len = list_length(content); - while (rprChildrenMatchAt(children, writepos - content_len, content) && + /* the min-0 check from the header comment */ + while ((child->min > 0 || + child->max == RPR_QUANTITY_INF || + !rprBodyIsNullable(content)) && + rprChildrenMatchAt(children, writepos - content_len, content) && rprTryAddIteration(child)) writepos -= content_len; } diff --git a/src/test/regress/expected/rpr_base.out b/src/test/regress/expected/rpr_base.out index c0cb4db155d..1f16f16e57a 100644 --- a/src/test/regress/expected/rpr_base.out +++ b/src/test/regress/expected/rpr_base.out @@ -8394,6 +8394,355 @@ WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING -> Seq Scan on rpr_plan (6 rows) +-- PREFIX fold rejected for a skippable bounded group with a nullable body +EXPLAIN (COSTS OFF) +WITH test_prefix_fold_nullable AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, ARRAY['C']) + ) AS t(id, flags) +) +SELECT COUNT(*) OVER w FROM test_prefix_fold_nullable +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + QUERY PLAN +----------------------------------------------------------------------------------------------- + WindowAgg + Window: w AS (ORDER BY "*VALUES*".column1 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + Pattern: (a | b??) (a | b??)? c + -> Sort + Sort Key: "*VALUES*".column1 + -> Values Scan on "*VALUES*" +(6 rows) + +WITH test_prefix_fold_nullable AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_nullable +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {B} | 1 | 2 + 2 | {A,C} | | + 3 | {C} | 3 | 3 +(3 rows) + +-- same shape with renamed variables (no fold possible): same result +WITH test_prefix_fold_nullable_renamed AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_nullable_renamed +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A1 | B1??))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags), + A1 AS 'A' = ANY(flags), + B1 AS 'B' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {B} | 1 | 2 + 2 | {A,C} | | + 3 | {C} | 3 | 3 +(3 rows) + +-- nullable body with {1,2}: min >= 1, so the fold applies +EXPLAIN (COSTS OFF) +WITH test_prefix_fold_nullable_range AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, ARRAY['C']) + ) AS t(id, flags) +) +SELECT COUNT(*) OVER w FROM test_prefix_fold_nullable_range +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??)){1,2} C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + QUERY PLAN +----------------------------------------------------------------------------------------------- + WindowAgg + Window: w AS (ORDER BY "*VALUES*".column1 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + Pattern: (a | b??){2,3} c + -> Sort + Sort Key: "*VALUES*".column1 + -> Values Scan on "*VALUES*" +(6 rows) + +WITH test_prefix_fold_nullable_range AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_nullable_range +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??)){1,2} C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {B} | 1 | 3 + 2 | {A,C} | | + 3 | {C} | | +(3 rows) + +-- unbounded min-0 group: the fold applies to a nullable body too +EXPLAIN (COSTS OFF) +WITH test_prefix_fold_nullable_star AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, ARRAY['C']) + ) AS t(id, flags) +) +SELECT COUNT(*) OVER w FROM test_prefix_fold_nullable_star +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??))* C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + QUERY PLAN +----------------------------------------------------------------------------------------------- + WindowAgg + Window: w AS (ORDER BY "*VALUES*".column1 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + Pattern: (a | b??)+ c + -> Sort + Sort Key: "*VALUES*".column1 + -> Values Scan on "*VALUES*" +(6 rows) + +WITH test_prefix_fold_nullable_star AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_nullable_star +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??))* C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {B} | 1 | 3 + 2 | {A,C} | | + 3 | {C} | | +(3 rows) + +-- non-nullable body: the fold still applies +EXPLAIN (COSTS OFF) +WITH test_prefix_fold_non_nullable AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, ARRAY['C']) + ) AS t(id, flags) +) +SELECT COUNT(*) OVER w FROM test_prefix_fold_non_nullable +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B) ((A | B))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + QUERY PLAN +----------------------------------------------------------------------------------------------- + WindowAgg + Window: w AS (ORDER BY "*VALUES*".column1 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + Pattern: (a | b){1,2} c + -> Sort + Sort Key: "*VALUES*".column1 + -> Values Scan on "*VALUES*" +(6 rows) + +WITH test_prefix_fold_non_nullable AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_non_nullable +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B) ((A | B))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {B} | 1 | 3 + 2 | {A,C} | | + 3 | {C} | | +(3 rows) + +-- nullable SEQ body: B* A?? (B* A??)? B must not fold either +WITH test_prefix_fold_nullable_seq AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['B']), + (3, ARRAY['B']) + ) 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_prefix_fold_nullable_seq +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN (B* A?? (B* A??)? B) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {A} | 1 | 2 + 2 | {B} | | + 3 | {B} | 3 | 3 +(3 rows) + +-- same shape with renamed variables (no fold possible): same result +WITH test_prefix_fold_nullable_seq_renamed AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['B']), + (3, ARRAY['B']) + ) 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_prefix_fold_nullable_seq_renamed +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN (B* A?? (B1* A1??)? B) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + A1 AS 'A' = ANY(flags), + B1 AS 'B' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {A} | 1 | 2 + 2 | {B} | | + 3 | {B} | 3 | 3 +(3 rows) + +-- the folded form written out: a different match +WITH test_prefix_fold_nullable_seq_range AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['B']), + (3, ARRAY['B']) + ) 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_prefix_fold_nullable_seq_range +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((B* A??){1,2} B) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags) +); + id | flags | match_start | match_end +----+-------+-------------+----------- + 1 | {A} | 1 | 3 + 2 | {B} | | + 3 | {B} | | +(3 rows) + -- Unwrap GROUP{1,1}: ((A | B | C)) -> (a | b | c) EXPLAIN (COSTS OFF) SELECT COUNT(*) OVER w FROM rpr_plan diff --git a/src/test/regress/sql/rpr_base.sql b/src/test/regress/sql/rpr_base.sql index ebf9c183cdc..adbedd1ea80 100644 --- a/src/test/regress/sql/rpr_base.sql +++ b/src/test/regress/sql/rpr_base.sql @@ -4804,6 +4804,271 @@ WINDOW w AS (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING PATTERN ((A B*)+ A B*) DEFINE A AS val <= 50, B AS val > 50); +-- PREFIX fold rejected for a skippable bounded group with a nullable body +EXPLAIN (COSTS OFF) +WITH test_prefix_fold_nullable AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, ARRAY['C']) + ) AS t(id, flags) +) +SELECT COUNT(*) OVER w FROM test_prefix_fold_nullable +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +WITH test_prefix_fold_nullable AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_nullable +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +-- same shape with renamed variables (no fold possible): same result +WITH test_prefix_fold_nullable_renamed AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_nullable_renamed +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A1 | B1??))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags), + A1 AS 'A' = ANY(flags), + B1 AS 'B' = ANY(flags) +); + +-- nullable body with {1,2}: min >= 1, so the fold applies +EXPLAIN (COSTS OFF) +WITH test_prefix_fold_nullable_range AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, ARRAY['C']) + ) AS t(id, flags) +) +SELECT COUNT(*) OVER w FROM test_prefix_fold_nullable_range +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??)){1,2} C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +WITH test_prefix_fold_nullable_range AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_nullable_range +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??)){1,2} C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +-- unbounded min-0 group: the fold applies to a nullable body too +EXPLAIN (COSTS OFF) +WITH test_prefix_fold_nullable_star AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, ARRAY['C']) + ) AS t(id, flags) +) +SELECT COUNT(*) OVER w FROM test_prefix_fold_nullable_star +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??))* C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +WITH test_prefix_fold_nullable_star AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_nullable_star +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B??) ((A | B??))* C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +-- non-nullable body: the fold still applies +EXPLAIN (COSTS OFF) +WITH test_prefix_fold_non_nullable AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, ARRAY['C']) + ) AS t(id, flags) +) +SELECT COUNT(*) OVER w FROM test_prefix_fold_non_nullable +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B) ((A | B))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +WITH test_prefix_fold_non_nullable AS ( + SELECT * FROM (VALUES + (1, ARRAY['B']), + (2, ARRAY['A','C']), + (3, 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_prefix_fold_non_nullable +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((A | B) ((A | B))? C) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + C AS 'C' = ANY(flags) +); + +-- nullable SEQ body: B* A?? (B* A??)? B must not fold either +WITH test_prefix_fold_nullable_seq AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['B']), + (3, ARRAY['B']) + ) 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_prefix_fold_nullable_seq +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN (B* A?? (B* A??)? B) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags) +); + +-- same shape with renamed variables (no fold possible): same result +WITH test_prefix_fold_nullable_seq_renamed AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['B']), + (3, ARRAY['B']) + ) 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_prefix_fold_nullable_seq_renamed +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN (B* A?? (B1* A1??)? B) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags), + A1 AS 'A' = ANY(flags), + B1 AS 'B' = ANY(flags) +); + +-- the folded form written out: a different match +WITH test_prefix_fold_nullable_seq_range AS ( + SELECT * FROM (VALUES + (1, ARRAY['A']), + (2, ARRAY['B']), + (3, ARRAY['B']) + ) 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_prefix_fold_nullable_seq_range +WINDOW w AS ( + ORDER BY id + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + AFTER MATCH SKIP PAST LAST ROW + PATTERN ((B* A??){1,2} B) + DEFINE + A AS 'A' = ANY(flags), + B AS 'B' = ANY(flags) +); + -- Unwrap GROUP{1,1}: ((A | B | C)) -> (a | b | c) EXPLAIN (COSTS OFF) SELECT COUNT(*) OVER w FROM rpr_plan -- 2.54.0 (Apple Git-157)