From 33edb822a6362362c16b201c27cee0ddb9927299 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Wed, 25 Nov 2026 22:07:31 +0900 Subject: [PATCH 8/9] Document plan-time folding of row pattern navigation arguments A navigation evaluates its argument only at the row it lands on, but planning folds a constant part of the argument like any other expression, including a constant that subquery pull-up, join alias expansion, virtual generated column expansion, a simplification such as COALESCE(0, v) or a not-null proof, or SQL function inlining leaves there. An error such folding raises, such as division by zero, is raised even when the navigation has no row to land on. "Expression Evaluation Rules" shows plan-time evaluation for a constant CASE arm, but not for a column that becomes a constant only while the query is planned. Add paragraphs there, with examples outside row pattern recognition, for the output of a pulled-up subquery, WITH query or VALUES list, a virtual generated column, COALESCE and IS NULL on a NOT NULL column, and an inlined SQL function that does not use its argument. Then note that a navigation argument is affected the same way, point to it from the navigation functions, and describe it in README.rpr. --- doc/src/sgml/func/func-window.sgml | 3 ++ doc/src/sgml/syntax.sgml | 67 ++++++++++++++++++++++++++++++ src/backend/executor/README.rpr | 18 +++++++- 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/func/func-window.sgml b/doc/src/sgml/func/func-window.sgml index 823923ac1da..0c79cbde045 100644 --- a/doc/src/sgml/func/func-window.sgml +++ b/doc/src/sgml/func/func-window.sgml @@ -420,6 +420,9 @@ IGNORE NULLS include at least one column reference; an argument made only of constants or parameters, such as PREV(1), is rejected. + As with any expression, a part of the value + argument may be evaluated when the query is planned, even if there is no + target row; see . diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml index a697218c8b3..6dc2e8c5d03 100644 --- a/doc/src/sgml/syntax.sgml +++ b/doc/src/sgml/syntax.sgml @@ -2562,6 +2562,53 @@ SELECT CASE WHEN x > 0 THEN x ELSE 1/0 END FROM tab; CASE expression. + + A column can also become a constant while the query is planned, when + the planner puts the expression behind it in its place. Each of the + following queries can fail with division by zero although it returns + no rows. + + + + The output column of a subquery, WITH query or + VALUES list that is pulled up into the outer query is + replaced by its expression, here the constant 0: + +SELECT 1/z FROM (SELECT 0 AS z) WHERE false; +WITH t(z) AS (VALUES (0)) SELECT 1/z FROM t WHERE false; + + + + + A virtual generated column is replaced by its generation expression: + +CREATE TABLE tab (x int, g int GENERATED ALWAYS AS (0) VIRTUAL); +SELECT 1/g FROM tab WHERE false; + + + + + Simplification can also drop a column and leave a constant. + COALESCE drops the arguments after one that is a + constant other than null, and IS NULL on a column + declared NOT NULL is reduced to + false: + +CREATE TABLE tab (x int NOT NULL); +SELECT 1/COALESCE(0, x) FROM tab WHERE false; +SELECT 1/(x IS NULL)::int FROM tab WHERE false; + + + + + A call to an SQL function can be replaced by the function's body, so a + function that does not use its argument leaves only the body: + +CREATE FUNCTION f(x int) RETURNS int LANGUAGE sql IMMUTABLE AS 'SELECT 1 / 0'; +SELECT f(x) FROM tab WHERE false; + + + Another limitation of the same kind is that a CASE cannot prevent evaluation of an aggregate expression contained within it, @@ -2583,6 +2630,26 @@ SELECT CASE WHEN min(employees) > 0 or FILTER clause to prevent problematic input rows from reaching an aggregate function in the first place. + + + Early evaluation of constant subexpressions also affects the argument of + a row pattern navigation function such as PREV (see + ). The argument is + evaluated only at the row the function navigates to, and not at all when + there is no such row, but as with the ELSE arm above, + a part of it that is constant, including one that becomes constant only + while the query is planned as described above, can be evaluated at that + point and raise an error even if no such row exists. The following + query has a single row, which leaves PREV no row + to navigate to, but it fails with division by zero because + z is replaced by the constant + 0: + +SELECT count(*) OVER w FROM (SELECT 0 AS z) + WINDOW w AS (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + PATTERN (A) DEFINE A AS PREV(1/z) IS NULL); + + diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index 4dc9b7524cc..03dbb32a933 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -328,6 +328,8 @@ because no column can appear there. A Param is still allowed, and is the case RPR_NAV_OFFSET_NEEDS_EVAL exists for. An omitted offset stays NULL on the node and the kind's default is supplied at execution: 1 for PREV/NEXT, 0 for FIRST/LAST, and 1 for a compound outer offset. +The column-reference rule holds for the argument as written; planning may +still reduce it to a constant (IV-3). Step (4) also changes the expression it walks. A PREV or NEXT whose argument is exactly one FIRST or LAST call is merged in place into one @@ -560,6 +562,18 @@ implicit-AND list. A TargetEntry holds one expression, so the loop turns the list straight back into one with make_ands_explicit(); a condition that folds to TRUE is left as a constant TRUE. +A navigation argument is preprocessed like the rest of the condition. +Subquery pull-up, join alias expansion (IV-2) and virtual generated column +expansion can put a constant there, as can a bound parameter under a custom +plan, and eval_const_expressions() can reduce the argument to one by +simplifying it, as with COALESCE(0, v) or a not-null proof, or by inlining a +SQL function; it then folds it. That can raise an error, such as a division +by zero, that execution would never reach because the navigation has no row +to land on and skips its argument (VII-3). This is the plan-time evaluation +the documentation describes under "Expression Evaluation Rules" +(syntax.sgml), with the ways a column becomes a constant during planning, +and it is accepted as a limitation. + The NFA evaluates a DEFINE at most once per row and caches the result (VII-3), re-evaluating only the match_start dependent variables of VII-5. The number of evaluations is not something a query can rely on, so a @@ -1426,7 +1440,9 @@ evaluating it against the wrong row. That makes the SET step's resnull a contract rather than a by-product: it has to be written on every path -- true when the target row is out of range, a definitive false when the row exists -- because resnull may still carry a stale value from an -earlier evaluation of the same expression. +earlier evaluation of the same expression. The skip happens at execution +only; planning may already have folded a constant part of the argument and +raised there (IV-3). Compound navigation (PREV(FIRST()), NEXT(FIRST()), PREV(LAST()), NEXT(LAST())) is flattened by the parser into a single RPRNavExpr with -- 2.54.0 (Apple Git-157)