On Thu, Nov 26, 2026 at 2:54 AM Henson Choi <[email protected]> wrote:
>
> Hi Tatsuo,
>
> > Ok, so the case is the division by zero in the constant folding at the
> > planning time. If that is the only case, I would consider working
> > hard for #2 not worth the trouble. I prefer #1 and we should write it
> > as a limitation of the patch so that we don't forget about it.
>
> Agreed, I will go with #1. Division by zero is only the example;
> any other error that constant folding can raise behaves the same.
>
> Core already documents the same behavior in "Expression Evaluation
> Rules" [1]:
>
> SELECT CASE WHEN x > 0 THEN x ELSE 1/0 END FROM tab;
>
> can fail with division by zero at plan time even if every row has
> x > 0, and the same can happen in queries within functions, whose
> arguments and local variables can be inserted as constants.
>
> So I think it would be good to describe this case in that section as
> well. My next increment adds paragraphs there on how a column can
> become a constant while the query is planned, with examples outside
> row pattern recognition, and one on navigation arguments. It points
> to them from the navigation functions and notes the limitation in
> README.rpr, with tests that show the plan-time error.
>
Hi.
WITH t(id, v) AS (VALUES (1, 10))
SELECT count(*) OVER w AS cnt
FROM t
WINDOW w AS (
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
PATTERN (A) DEFINE A AS PREV(v / 0) > 0);
return `` division by zero``
is the direction to move forward, i think.
Constant folding and function inlining happen at plan time; the
expression (v / 0) can be
evaluated before the executor ever decides whether PREV() has a row to read.
Exempting one subtree of an expression from constant folding (or function
inlining) is hacky, and it tends to cause subtle bugs. I made the same
mistake with CAST(... DEFAULT ... ON CONVERSION ERROR).
Overall, i think in eval_const_expressions_mutator, we do below should
be enough.
+ case T_RPRNavExpr:
+ {
+ RPRNavExpr *nav = (RPRNavExpr *) node;
+ RPRNavExpr *newnav;
+
+ /*
+ * The argument and the offsets are
simplified like any other
+ * expression, SQL function inlining included.
+ */
+ newnav = makeNode(RPRNavExpr);
+ memcpy(newnav, nav, sizeof(RPRNavExpr));
+ newnav->arg = (Expr *)
+
eval_const_expressions_mutator((Node *) nav->arg, context);
+ newnav->offset_arg = (Expr *)
+
eval_const_expressions_mutator((Node *) nav->offset_arg,
+
context);
+ newnav->compound_offset_arg = (Expr *)
+
eval_const_expressions_mutator((Node *) nav->compound_offset_arg,
+
context);
+ return (Node *) newnav;
+ }
It is OK not to document anything, see
https://www.postgresql.org/message-id/258f84d5-57c6-454a-a22b-6242871216ec%40illuminatedcomputing.com
--
jian
https://www.enterprisedb.com/