From 779ac2c74bd15744aa92b588f70606a1a95256d2 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 18 May 2024 14:26:05 -0400 Subject: [PATCH] Add test case showing that commit d0d44049d fixed a live bug. When I committed d0d44049d (Account for optimized MinMax aggregates during SS_finalize_plan), I didn't have a test case showing that it was fixing any reachable bug. Here is one, based on bug #18465 from Hal Takahara. Without the fix, all rows of the result show the same "min" value, because the aggregate doesn't get recalculated. Committed despite beta1 release freeze, with the concurrence of pgsql-release. Discussion: https://postgr.es/m/18465-2fae927718976b22@postgresql.org Discussion: https://postgr.es/m/2391880.1689025003@sss.pgh.pa.us --- src/test/regress/expected/aggregates.out | 31 ++++++++++++++++++++++++ src/test/regress/sql/aggregates.sql | 10 ++++++++ 2 files changed, 41 insertions(+) diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out index 56c361ccef7..2442342e9d1 100644 --- a/src/test/regress/expected/aggregates.out +++ b/src/test/regress/expected/aggregates.out @@ -1273,6 +1273,37 @@ NOTICE: drop cascades to 3 other objects DETAIL: drop cascades to table minmaxtest1 drop cascades to table minmaxtest2 drop cascades to table minmaxtest3 +-- DISTINCT can also trigger wrong answers with hash aggregation (bug #18465) +begin; +set local enable_sort = off; +explain (costs off) + select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1) + from int4_tbl t0; + QUERY PLAN +--------------------------------------------------------------------- + Seq Scan on int4_tbl t0 + SubPlan 2 + -> HashAggregate + Group Key: (InitPlan 1).col1 + InitPlan 1 + -> Limit + -> Seq Scan on int4_tbl t1 + Filter: ((f1 IS NOT NULL) AND (f1 = t0.f1)) + -> Result +(9 rows) + +select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1) +from int4_tbl t0; + f1 | min +-------------+------------- + 0 | 0 + 123456 | 123456 + -123456 | -123456 + 2147483647 | 2147483647 + -2147483647 | -2147483647 +(5 rows) + +rollback; -- check for correct detection of nested-aggregate errors select max(min(unique1)) from tenk1; ERROR: aggregate function calls cannot be nested diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql index d28338ba3da..61a3424c845 100644 --- a/src/test/regress/sql/aggregates.sql +++ b/src/test/regress/sql/aggregates.sql @@ -436,6 +436,16 @@ select distinct min(f1), max(f1) from minmaxtest; drop table minmaxtest cascade; +-- DISTINCT can also trigger wrong answers with hash aggregation (bug #18465) +begin; +set local enable_sort = off; +explain (costs off) + select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1) + from int4_tbl t0; +select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1) +from int4_tbl t0; +rollback; + -- check for correct detection of nested-aggregate errors select max(min(unique1)) from tenk1; select (select max(min(unique1)) from int8_tbl) from tenk1;