]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix to not allow null treatment to non window functions.
authorTatsuo Ishii <ishii@postgresql.org>
Wed, 17 Jun 2026 01:12:07 +0000 (10:12 +0900)
committerTatsuo Ishii <ishii@postgresql.org>
Wed, 17 Jun 2026 01:12:07 +0000 (10:12 +0900)
The null treatment clause (RESPECT NULLS/IGNORE NULLS) are only
allowed to window functions per spec. Previously the check was only
applied to aggregates in window clause. Other types of functions were
allowed to use the clause, which was plain wrong.

To fix this, ParseFuncOrColumn() now checks whether other than window
functions are used with the null treatment clause. If so, error out.

Also remove the unnecessary test for "aggregate functions do not
accept RESPECT/IGNORE NULLS" because it is now checked in the
early-stage new check. The window regression test expected file is
changed accordingly.

Reported-by: jian he <jian.universality@gmail.com>
Reviewed-by: jian he <jian.universality@gmail.com>
Author: Tatsuo Ishii <ishii@postgresql.org>
Discussion: https://postgr.es/m/CACJufxFnm%2BAj2Jyhyd58PtW8e1vTZDKimkZE%2BMashCPSDKw56Q%40mail.gmail.com

src/backend/parser/parse_func.c
src/test/regress/expected/window.out

index 2e4cc1de50de2f7cba0d9e42fb3d146032b6fa50..6717ddc6881d37799bbc95a4458a18286b6fa1e9 100644 (file)
@@ -353,6 +353,15 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
                                         parser_errposition(pstate, location)));
        }
 
+       /*
+        * NULL TREATEMENT is only allowed for window functions per spec.
+        */
+       if (fdresult != FUNCDETAIL_WINDOWFUNC && ignore_nulls != NO_NULLTREATMENT)
+               ereport(ERROR,
+                               errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                               errmsg("Only window functions accept RESPECT/IGNORE NULLS"),
+                               parser_errposition(pstate, location));
+
        /*
         * So far so good, so do some fdresult-type-specific processing.
         */
@@ -519,13 +528,6 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
                                                 errmsg("%s is not an ordered-set aggregate, so it cannot have WITHIN GROUP",
                                                                NameListToString(funcname)),
                                                 parser_errposition(pstate, location)));
-
-                       /* It also can't treat nulls as a window function */
-                       if (ignore_nulls != NO_NULLTREATMENT)
-                               ereport(ERROR,
-                                               (errcode(ERRCODE_WRONG_OBJECT_TYPE),
-                                                errmsg("aggregate functions do not accept RESPECT/IGNORE NULLS"),
-                                                parser_errposition(pstate, location)));
                }
        }
        else if (fdresult == FUNCDETAIL_WINDOWFUNC)
index de0e14a686e326969231f9b30c6b980875aded30..891b015300443e7ee351f43f0bc02ecfef2fce2c 100644 (file)
@@ -5764,11 +5764,11 @@ SELECT sum(orbit) OVER () FROM planets; -- succeeds
 (10 rows)
 
 SELECT sum(orbit) RESPECT NULLS OVER () FROM planets; -- fails
-ERROR:  aggregate functions do not accept RESPECT/IGNORE NULLS
+ERROR:  Only window functions accept RESPECT/IGNORE NULLS
 LINE 1: SELECT sum(orbit) RESPECT NULLS OVER () FROM planets;
                ^
 SELECT sum(orbit) IGNORE NULLS OVER () FROM planets; -- fails
-ERROR:  aggregate functions do not accept RESPECT/IGNORE NULLS
+ERROR:  Only window functions accept RESPECT/IGNORE NULLS
 LINE 1: SELECT sum(orbit) IGNORE NULLS OVER () FROM planets;
                ^
 SELECT row_number() OVER () FROM planets; -- succeeds