From 4c640f4f38d5d68cbe33ddfabbdc56eea8f1e173 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Sat, 3 Nov 2018 14:35:23 -0700 Subject: [PATCH] Fix STRICT check for strict aggregates with NULL ORDER BY columns. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit I (Andres) broke this unintentionally in 69c3936a14, by checking strictness for all input expressions computed for an aggregate, rather than just the input for the aggregate transition function. Reported-By: Ondřej Bouda Bisected-By: Tom Lane Diagnosed-By: Andrew Gierth Discussion: https://postgr.es/m/2a505161-2727-2473-7c46-591ed108ac52@email.cz Backpatch: 11-, like 69c3936a14 --- src/backend/executor/execExpr.c | 2 +- src/test/regress/expected/aggregates.out | 15 +++++++++++++++ src/test/regress/sql/aggregates.sql | 7 +++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c index c5e8634aed..5397de4e05 100644 --- a/src/backend/executor/execExpr.c +++ b/src/backend/executor/execExpr.c @@ -3028,7 +3028,7 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase, scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK; scratch.d.agg_strict_input_check.nulls = strictnulls; scratch.d.agg_strict_input_check.jumpnull = -1; /* adjust later */ - scratch.d.agg_strict_input_check.nargs = numInputs; + scratch.d.agg_strict_input_check.nargs = pertrans->numTransInputs; ExprEvalPushStep(state, &scratch); adjust_bailout = lappend_int(adjust_bailout, state->steps_len - 1); diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out index 717e965f30..20dacfe4b0 100644 --- a/src/test/regress/expected/aggregates.out +++ b/src/test/regress/expected/aggregates.out @@ -2229,3 +2229,18 @@ SELECT dense_rank(x) WITHIN GROUP (ORDER BY x) FROM (VALUES (1),(1),(2),(2),(3), 1 (3 rows) +-- Ensure that the STRICT checks for aggregates does not take NULLness +-- of ORDER BY columns into account. See bug report around +-- 2a505161-2727-2473-7c46-591ed108ac52@email.cz +SELECT min(x ORDER BY y) FROM (VALUES(1, NULL)) AS d(x,y); + min +----- + 1 +(1 row) + +SELECT min(x ORDER BY y) FROM (VALUES(1, 2)) AS d(x,y); + min +----- + 1 +(1 row) + diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql index 83a5492fdd..8192d457e9 100644 --- a/src/test/regress/sql/aggregates.sql +++ b/src/test/regress/sql/aggregates.sql @@ -969,3 +969,10 @@ ROLLBACK; -- test coverage for dense_rank SELECT dense_rank(x) WITHIN GROUP (ORDER BY x) FROM (VALUES (1),(1),(2),(2),(3),(3)) v(x) GROUP BY (x) ORDER BY 1; + + +-- Ensure that the STRICT checks for aggregates does not take NULLness +-- of ORDER BY columns into account. See bug report around +-- 2a505161-2727-2473-7c46-591ed108ac52@email.cz +SELECT min(x ORDER BY y) FROM (VALUES(1, NULL)) AS d(x,y); +SELECT min(x ORDER BY y) FROM (VALUES(1, 2)) AS d(x,y); -- 2.40.0