nodeWindowAgg.c IGNORE NULLS abs overflow

Started by jian heabout 1 month ago8 messageshackers
Jump to latest
#1jian he
jian.universality@gmail.com

Hi.

SELECT v,
lead(v, -2147483648) OVER w AS respect_nulls,
lead(v, -2147483648) IGNORE NULLS OVER w AS ignore_nulls
FROM (VALUES (1), (2)) AS t(v)
WINDOW w AS (ORDER BY v);

should return all NULL values for column ignore_nulls.

The fix is to replace the abs function in nodeWindowAgg.c with pg_abs_s32.

--
jian
https://www.enterprisedb.com/

Attachments:

t254298_1
fix_abs_overflow_nodeWindowAgg.c.difftext/x-patch; charset=US-ASCII; name=fix_abs_overflow_nodeWindowAgg.c.diffDownload+23-6
#2Tatsuo Ishii
ishii@postgresql.org
In reply to: jian he (#1)
Re: nodeWindowAgg.c IGNORE NULLS abs overflow

Hi Jian,

Hi.

SELECT v,
lead(v, -2147483648) OVER w AS respect_nulls,
lead(v, -2147483648) IGNORE NULLS OVER w AS ignore_nulls
FROM (VALUES (1), (2)) AS t(v)
WINDOW w AS (ORDER BY v);

should return all NULL values for column ignore_nulls.

The fix is to replace the abs function in nodeWindowAgg.c with pg_abs_s32.

Thank you for the report and the fix. Unfortunately I have no time to
take care this for now (till November 3rd). I will work on this after
November 4th unless someone beats me.

Regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp

#3Tatsuo Ishii
ishii@postgresql.org
In reply to: Tatsuo Ishii (#2)
Re: nodeWindowAgg.c IGNORE NULLS abs overflow

Hi Jian,

SELECT v,
lead(v, -2147483648) OVER w AS respect_nulls,
lead(v, -2147483648) IGNORE NULLS OVER w AS ignore_nulls
FROM (VALUES (1), (2)) AS t(v)
WINDOW w AS (ORDER BY v);

should return all NULL values for column ignore_nulls.

The fix is to replace the abs function in nodeWindowAgg.c with pg_abs_s32.

I confirmed the problem. It seems it's a corner case bug: only happens
when offset = -2147483648. Anyway, bug is a bug.

Attached is a patch which should be ready for commit IMO. I haven't
changed the C code part from yours. I have changed the regression test
part to use low case SQL statements which fits in better with
surroundings.

Regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp

Attachments:

t254298_3
v1-0001-Fix-some-window-functions-return-wrong-results-wi.patchtext/x-patch; charset=us-asciiDownload+23-7
#4Tatsuo Ishii
ishii@postgresql.org
In reply to: Tatsuo Ishii (#3)
Re: nodeWindowAgg.c IGNORE NULLS abs overflow

Hi Jian,

I confirmed the problem. It seems it's a corner case bug: only happens
when offset = -2147483648. Anyway, bug is a bug.

Attached is a patch which should be ready for commit IMO. I haven't
changed the C code part from yours. I have changed the regression test
part to use low case SQL statements which fits in better with
surroundings.

Attached is the v2 patch. I just added a test case for lag() to
clarify that it is affected by the bug too. If there's no objection,
I will push it to master and 19 branches.

Regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp

Attachments:

t254298_4
v2-0001-Fix-wrong-results-of-some-window-functions-with-I.patchtext/x-patch; charset=us-asciiDownload+40-7
#5Kirill Reshke
reshkekirill@gmail.com
In reply to: Tatsuo Ishii (#4)
Re: nodeWindowAgg.c IGNORE NULLS abs overflow

On Thu, 5 Nov 2026 at 09:18, Tatsuo Ishii <ishii@postgresql.org> wrote:

Hi Jian,

I confirmed the problem. It seems it's a corner case bug: only happens
when offset = -2147483648. Anyway, bug is a bug.

Attached is a patch which should be ready for commit IMO. I haven't
changed the C code part from yours. I have changed the regression test
part to use low case SQL statements which fits in better with
surroundings.

Attached is the v2 patch. I just added a test case for lag() to
clarify that it is affected by the bug too. If there's no objection,
I will push it to master and 19 branches.

Regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp

Hi!
Discussion link in your v2 is wrong - for `IGNORE/RESPECT NULLS can be
specified for (prokind == 'f'). ` thread

Also, is there one more issue with leadlag_common:

result = WinGetFuncArgInPartition(winobj, 0,
(forward ? offset : -offset),
WINDOW_SEEK_CURRENT,
const_offset,
&isnull, &isout);

forward ? offset : -offset - same overflow? looks like lag(v,
-2147483648) would be wrong

--
Best regards,
Kirill Reshke

#6Tatsuo Ishii
ishii@postgresql.org
In reply to: Kirill Reshke (#5)
Re: nodeWindowAgg.c IGNORE NULLS abs overflow

Attached is the v2 patch. I just added a test case for lag() to
clarify that it is affected by the bug too. If there's no objection,
I will push it to master and 19 branches.

Hi!
Discussion link in your v2 is wrong - for `IGNORE/RESPECT NULLS can be
specified for (prokind == 'f'). ` thread

Opps. You are right.

Also, is there one more issue with leadlag_common:

result = WinGetFuncArgInPartition(winobj, 0,
(forward ? offset : -offset),
WINDOW_SEEK_CURRENT,
const_offset,
&isnull, &isout);

forward ? offset : -offset - same overflow? looks like lag(v,
-2147483648) would be wrong

Good point. If offset == -2147483648, -offset will be -2147483648 due to an
overflow. However, this is an old problem with leadlag_common, not
specific to IGNORE NULLS. So I think this issue does not belong to v19
open items. What do you think?

Regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp

#7Kirill Reshke
reshkekirill@gmail.com
In reply to: Tatsuo Ishii (#6)
Re: nodeWindowAgg.c IGNORE NULLS abs overflow

On Thu, 5 Nov 2026 at 12:11, Tatsuo Ishii <ishii@postgresql.org> wrote:

However, this is an old problem with leadlag_common, not
specific to IGNORE NULLS. So I think this issue does not belong to v19
open items. What do you think?

Yes, we can fix this separately

--
Best regards,
Kirill Reshke

#8Tatsuo Ishii
ishii@postgresql.org
In reply to: Kirill Reshke (#7)
Re: nodeWindowAgg.c IGNORE NULLS abs overflow

Hi Jian and Kirill,

On Thu, 5 Nov 2026 at 12:11, Tatsuo Ishii <ishii@postgresql.org> wrote:

However, this is an old problem with leadlag_common, not
specific to IGNORE NULLS. So I think this issue does not belong to v19
open items. What do you think?

Yes, we can fix this separately

I have pushed v2 patch (with slight modifications to the commit
message). Thanks!

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=b8d82d178758b6e956ee02af512bbd949c335cc2

Regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp