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 <[email protected]>
Reviewed-by: Tatsuo Ishii <[email protected]>
Reviewed-by: Kirill Reshke <[email protected]>
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(-)

Reply via email to