pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix wrong results of some window functions with IGNORE NULLS.
2+ messages / 1 participants
[nested] [flat]

* pgsql: Fix wrong results of some window functions with IGNORE NULLS.
@ 2026-11-06 05:25 Tatsuo Ishii <ishii@postgresql.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Tatsuo Ishii @ 2026-11-06 05:25 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix wrong results of some window functions with IGNORE NULLS.

Window functions such as lead() and lag() could return wrong results
if IGNORE NULLS is specified and the offset argument is equal to
INT_MIN.

WinGetFuncArgInPartition() used abs() to get the absolute value of the
offset argument. If it is INT_MIN (-2147483648), the return value of
abs() is undefined. Typically it returns INT_MIN. However,
WinGetFuncArgInPartition() was not prepared for the case and returns
wrong result. To fix this, use pg_abs_s32() instead of abs() to return
2147483648, instead of -2147483648.

ignorenulls_getfuncarginframe() has the same problem. So this commit
fixes them as well. Note that currently no built in window function
hits the bug because only nth_value() has the offset argument and it
does not accept negative offset. However, WinGetFuncArgInFrame(), the
caller of ignorenulls_getfuncarginframe(), is a public API, we should
prepare the similar case when user defined window functions use it.

Moreover, this commit changes some internal variable data types from
int to int64 in order to prevent an overflow while calculating a row
position in the partition.

Also add regression test cases to window.sql.

Author: jian he <jian.universality@gmail.com>
Reviewed-by: Tatsuo Ishii <ishii@postgresql.org>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Discussion: https://postgr.es/m/CACJufxH2_4ucJ58_YQPin8E8hf2RRD6S7rA-UK9pSWxdg7YBQw%40mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b8d82d178758b6e956ee02af512bbd949c335cc2

Modified Files
--------------
src/backend/executor/nodeWindowAgg.c | 12 ++++++------
src/test/regress/expected/window.out | 22 ++++++++++++++++++++++
src/test/regress/sql/window.sql      | 12 ++++++++++++
3 files changed, 40 insertions(+), 6 deletions(-)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* pgsql: Fix wrong results of some window functions with IGNORE NULLS.
@ 2026-11-06 05:25 Tatsuo Ishii <ishii@postgresql.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Tatsuo Ishii @ 2026-11-06 05:25 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix wrong results of some window functions with IGNORE NULLS.

Window functions such as lead() and lag() could return wrong results
if IGNORE NULLS is specified and the offset argument is equal to
INT_MIN.

WinGetFuncArgInPartition() used abs() to get the absolute value of the
offset argument. If it is INT_MIN (-2147483648), the return value of
abs() is undefined. Typically it returns INT_MIN. However,
WinGetFuncArgInPartition() was not prepared for the case and returns
wrong result. To fix this, use pg_abs_s32() instead of abs() to return
2147483648, instead of -2147483648.

ignorenulls_getfuncarginframe() has the same problem. So this commit
fixes them as well. Note that currently no built in window function
hits the bug because only nth_value() has the offset argument and it
does not accept negative offset. However, WinGetFuncArgInFrame(), the
caller of ignorenulls_getfuncarginframe(), is a public API, we should
prepare the similar case when user defined window functions use it.

Moreover, this commit changes some internal variable data types from
int to int64 in order to prevent an overflow while calculating a row
position in the partition.

Also add regression test cases to window.sql.

Author: jian he <jian.universality@gmail.com>
Reviewed-by: Tatsuo Ishii <ishii@postgresql.org>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Discussion: https://postgr.es/m/CACJufxH2_4ucJ58_YQPin8E8hf2RRD6S7rA-UK9pSWxdg7YBQw%40mail.gmail.com
Backpatch-through: 19

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/24f39139668a1e73d9175f9dee16b0f8c61b95c7

Modified Files
--------------
src/backend/executor/nodeWindowAgg.c | 12 ++++++------
src/test/regress/expected/window.out | 22 ++++++++++++++++++++++
src/test/regress/sql/window.sql      | 12 ++++++++++++
3 files changed, 40 insertions(+), 6 deletions(-)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-11-06 05:25 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-06 05:25 pgsql: Fix wrong results of some window functions with IGNORE NULLS. Tatsuo Ishii <ishii@postgresql.org>
2026-11-06 05:25 pgsql: Fix wrong results of some window functions with IGNORE NULLS. Tatsuo Ishii <ishii@postgresql.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox