Hello
+ /*
+ * Make sure that the row pattern definition search condition is a boolean
+ * expression.
+ */
+ foreach_ptr(TargetEntry, te, defineClause)
+ (void) coerce_to_boolean(pstate, (Node *) te->expr, "DEFINE");
Isn't this incorrect? I think it should update te->expr, as currently
it is possible to construct queries where this produces unexpected
results.
CREATE TYPE truthyint AS (v int);
CREATE FUNCTION truthyint_to_bool(truthyint) RETURNS boolean AS $$
SELECT ($1).v <> 0;
$$ LANGUAGE SQL IMMUTABLE STRICT;
CREATE CAST (truthyint AS boolean)
WITH FUNCTION truthyint_to_bool(truthyint)
AS ASSIGNMENT;
CREATE TABLE test_coerce (id serial, val truthyint);
INSERT INTO test_coerce VALUES
(1, ROW(1)),
(2, ROW(0)),
(3, ROW(5)),
(4, ROW(0));
SELECT id, val, COUNT(*) OVER w AS cnt
FROM test_coerce
WINDOW w AS (
ORDER BY id
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
PATTERN (A+)
DEFINE A AS val
)
ORDER BY id;
Same query provides the correct result with a table that has an actual
boolean column.