From: Tatsuo Ishii <ishii@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix wrong results of some window functions with IGNORE NULLS.
Date: Fri, 06 Nov 2026 05:25:49 +0000
Message-ID: <E1xPCST-00000000QAr-1BKS@gemulon.postgresql.org> (raw)
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(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: ishii@postgresql.org
Subject: Re: pgsql: Fix wrong results of some window functions with IGNORE NULLS.
In-Reply-To: <E1xPCST-00000000QAr-1BKS@gemulon.postgresql.org>
References: <E1xPCST-00000000QAr-1BKS@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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