Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Add test case showing that commit d0d44049d fixed a live bug.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 18 May 2024 18:26:05 +0000 (14:26 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 18 May 2024 18:26:13 +0000 (14:26 -0400)
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
src/test/regress/sql/aggregates.sql

index 56c361ccef7cc5ca96e44d44fed253e07a0265ae..2442342e9d1937aea481c6dad7f9b48461e6dcf9 100644 (file)
@@ -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
index d28338ba3dab038b4303b957904b8f0977488412..61a3424c845b90e4ea6353657523893d26bc90fd 100644 (file)
@@ -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;