Add IGNORE NULLS/RESPECT NULLS option to Window functions. master github/master
authorTatsuo Ishii <[email protected]>
Fri, 3 Oct 2025 00:47:36 +0000 (09:47 +0900)
committerTatsuo Ishii <[email protected]>
Fri, 3 Oct 2025 00:47:36 +0000 (09:47 +0900)
commit25a30bbd4235a49c854036c84fe90f2bc5a87652
tree263bb113d48edb9858b6ad140590e9cec17a81d2
parent381f5cffae0040a402e082adc5d5e7636035d2a7
Add IGNORE NULLS/RESPECT NULLS option to Window functions.

Add IGNORE NULLS/RESPECT NULLS option (null treatment clause) to lead,
lag, first_value, last_value and nth_value window functions.  If
unspecified, the default is RESPECT NULLS which includes NULL values
in any result calculation. IGNORE NULLS ignores NULL values.

Built-in window functions are modified to call new API
WinCheckAndInitializeNullTreatment() to indicate whether they accept
IGNORE NULLS/RESPECT NULLS option or not (the API can be called by
user defined window functions as well).  If WinGetFuncArgInPartition's
allowNullTreatment argument is true and IGNORE NULLS option is given,
WinGetFuncArgInPartition() or WinGetFuncArgInFrame() will return
evaluated function's argument expression on specified non NULL row (if
it exists) in the partition or the frame.

When IGNORE NULLS option is given, window functions need to visit and
evaluate same rows over and over again to look for non null rows. To
mitigate the issue, 2-bit not null information array is created while
executing window functions to remember whether the row has been
already evaluated to NULL or NOT NULL. If already evaluated, we could
skip the evaluation work, thus we could get better performance.

Author: Oliver Ford <[email protected]>
Co-authored-by: Tatsuo Ishii <[email protected]>
Reviewed-by: Krasiyan Andreev <[email protected]>
Reviewed-by: Andrew Gierth <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Reviewed-by: David Fetter <[email protected]>
Reviewed-by: Vik Fearing <[email protected]>
Reviewed-by: "David G. Johnston" <[email protected]>
Reviewed-by: Chao Li <[email protected]>
Discussion: https://postgr.es/m/flat/CAGMVOdsbtRwE_4+v8zjH1d9xfovDeQAGLkP_B6k69_VoFEgX-A@mail.gmail.com
15 files changed:
doc/src/sgml/func/func-window.sgml
doc/src/sgml/syntax.sgml
src/backend/catalog/sql_features.txt
src/backend/executor/nodeWindowAgg.c
src/backend/optimizer/util/clauses.c
src/backend/parser/gram.y
src/backend/parser/parse_func.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/adt/windowfuncs.c
src/include/nodes/parsenodes.h
src/include/nodes/primnodes.h
src/include/parser/kwlist.h
src/include/windowapi.h
src/test/regress/expected/window.out
src/test/regress/sql/window.sql