From e7438fb4a23d8d7e690a3332cf11e83b52ded84e Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Fri, 27 Nov 2026 01:35:21 -0500 Subject: [PATCH v1] Fix WinGetFuncArgInPartition() with WINDOW_SEEK_TAIL and IGNORE NULLS The TAIL case added relpos to the last row and then stepped relpos rows again. Reported-by: chunling qin <303677365@qq.com> Discussion: https://postgr.es/m/19870-a58b65793ce2a466@postgresql.org --- src/backend/executor/nodeWindowAgg.c | 5 ++++- src/test/regress/expected/window.out | 21 +++++++++++++++++++++ src/test/regress/regress.c | 23 +++++++++++++++++++++++ src/test/regress/sql/window.sql | 18 ++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/backend/executor/nodeWindowAgg.c b/src/backend/executor/nodeWindowAgg.c index 8d80f2cc023..bf6fce8a34e 100644 --- a/src/backend/executor/nodeWindowAgg.c +++ b/src/backend/executor/nodeWindowAgg.c @@ -3901,7 +3901,10 @@ WinGetFuncArgInPartition(WindowObject winobj, int argno, break; case WINDOW_SEEK_TAIL: spool_tuples(winstate, -1); - abs_pos = winstate->spooled_rows - 1 + relpos; + if (null_treatment) + abs_pos = winstate->spooled_rows - 1; + else + abs_pos = winstate->spooled_rows - 1 + relpos; break; default: elog(ERROR, "unrecognized window seek type: %d", seektype); diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out index 112551c350b..da21aeb3618 100644 --- a/src/test/regress/expected/window.out +++ b/src/test/regress/expected/window.out @@ -6316,6 +6316,27 @@ SELECT last_value FROM null_treatment_seq; 8 (1 row) +-- WINDOW_SEEK_TAIL with IGNORE NULLS, via a test function +\getenv libdir PG_LIBDIR +\getenv dlsuffix PG_DLSUFFIX +\set regresslib :libdir '/regress' :dlsuffix +CREATE FUNCTION test_window_seek_tail(anyelement, int4) + RETURNS anyelement + AS :'regresslib', 'test_window_seek_tail' + LANGUAGE C WINDOW; +SELECT x, + test_window_seek_tail(x, -1) IGNORE NULLS OVER w AS prev, + test_window_seek_tail(x, 2147483647) IGNORE NULLS OVER w AS out_of_range +FROM generate_series(1,3) g(x) +WINDOW w AS (ORDER BY x); + x | prev | out_of_range +---+------+-------------- + 1 | 2 | + 2 | 2 | + 3 | 2 | +(3 rows) + +DROP FUNCTION test_window_seek_tail(anyelement, int4); --cleanup DROP TABLE planets CASCADE; NOTICE: drop cascades to view planets_view diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c index 6e61c0d201b..31a1a47dd95 100644 --- a/src/test/regress/regress.c +++ b/src/test/regress/regress.c @@ -53,6 +53,7 @@ #include "utils/pg_locale.h" #include "utils/rel.h" #include "utils/typcache.h" +#include "windowapi.h" /* define our text domain for translations */ #undef TEXTDOMAIN @@ -1744,3 +1745,25 @@ test_pg_locale_apis(PG_FUNCTION_ARGS) PG_RETURN_VOID(); } + +/* Return WinGetFuncArgInPartition() for relpos from WINDOW_SEEK_TAIL. */ +PG_FUNCTION_INFO_V1(test_window_seek_tail); +Datum +test_window_seek_tail(PG_FUNCTION_ARGS) +{ + WindowObject winobj = PG_WINDOW_OBJECT(); + bool isnull; + bool isout; + int32 relpos; + Datum result; + + WinCheckAndInitializeNullTreatment(winobj, true, fcinfo); + + relpos = DatumGetInt32(WinGetFuncArgCurrent(winobj, 1, &isnull)); + + result = WinGetFuncArgInPartition(winobj, 0, relpos, WINDOW_SEEK_TAIL, + false, &isnull, &isout); + if (isnull) + PG_RETURN_NULL(); + PG_RETURN_DATUM(result); +} diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql index e848557174b..f9ce11d5b54 100644 --- a/src/test/regress/sql/window.sql +++ b/src/test/regress/sql/window.sql @@ -2331,5 +2331,23 @@ SELECT x, FROM generate_series(1,5) g(x); SELECT last_value FROM null_treatment_seq; +-- WINDOW_SEEK_TAIL with IGNORE NULLS, via a test function +\getenv libdir PG_LIBDIR +\getenv dlsuffix PG_DLSUFFIX +\set regresslib :libdir '/regress' :dlsuffix + +CREATE FUNCTION test_window_seek_tail(anyelement, int4) + RETURNS anyelement + AS :'regresslib', 'test_window_seek_tail' + LANGUAGE C WINDOW; + +SELECT x, + test_window_seek_tail(x, -1) IGNORE NULLS OVER w AS prev, + test_window_seek_tail(x, 2147483647) IGNORE NULLS OVER w AS out_of_range +FROM generate_series(1,3) g(x) +WINDOW w AS (ORDER BY x); + +DROP FUNCTION test_window_seek_tail(anyelement, int4); + --cleanup DROP TABLE planets CASCADE; -- 2.37.1 (Apple Git-137.1)