From 967747485b8561fd7c7239bdba8f86c7984ade8d Mon Sep 17 00:00:00 2001
From: Alain Lesage <alain.lesage@dalibo.com>
Date: Sun, 29 Nov 2026 14:57:25 +0100
Subject: [PATCH v2] Add tests for hash aggregate spilling with unneeded input
 columns

When only some of the input columns are needed, spilled tuples contain
just those columns and are mapped back to the input layout when read.
The existing spill tests barely reach that path. Add two tests that
compare hash and sort-based results with work_mem = 64kB: one with
NULLs, by-ref values and respilling, run with JIT, and one where the
aggregate is rescanned under a lateral join.
---
 src/test/regress/expected/aggregates.out | 72 ++++++++++++++++++++++
 src/test/regress/sql/aggregates.sql      | 76 ++++++++++++++++++++++++
 2 files changed, 148 insertions(+)

diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out
index ce555a3..f4dfaf5 100644
--- a/src/test/regress/expected/aggregates.out
+++ b/src/test/regress/expected/aggregates.out
@@ -3986,3 +3986,75 @@ drop table agg_hash_1;
 drop table agg_hash_2;
 drop table agg_hash_3;
 drop table agg_hash_4;
+--
+-- Hash Aggregation Spill tests where only some of the input columns are
+-- needed. Check NULLs, by-ref values and respilling, with JIT, and a
+-- rescan, against sort-based results.
+--
+create table agg_data_wide as
+select g, g % 2500 as k, nullif(g % 7, 0) as n, (g % 300)::text as t,
+       repeat('x', 20) as pad1, g * 2 as pad2
+  from generate_series(0, 9999) g;
+analyze agg_data_wide;
+set work_mem = '64kB';
+-- Produce results with sorting.
+set enable_hashagg = false;
+create table agg_group_wide_1 as
+select k, count(n) as c1, sum(n::numeric) as c2, max(t) as c3
+  from agg_data_wide group by k;
+create table agg_group_wide_2 as
+select * from
+  (values (2500), (10000)) as r(a),
+  lateral (
+    select k, count(n) as c1, max(t) as c2
+    from agg_data_wide
+    where g < r.a
+    group by k) as s;
+-- Produce results with hash aggregation.
+set enable_hashagg = true;
+set enable_groupagg = false;
+set jit_above_cost = 0;
+explain (costs off)
+select k, count(n) as c1, sum(n::numeric) as c2, max(t) as c3
+  from agg_data_wide group by k;
+           QUERY PLAN            
+---------------------------------
+ HashAggregate
+   Group Key: k
+   ->  Seq Scan on agg_data_wide
+(3 rows)
+
+create table agg_hash_wide_1 as
+select k, count(n) as c1, sum(n::numeric) as c2, max(t) as c3
+  from agg_data_wide group by k;
+set jit_above_cost to default;
+create table agg_hash_wide_2 as
+select * from
+  (values (2500), (10000)) as r(a),
+  lateral (
+    select k, count(n) as c1, max(t) as c2
+    from agg_data_wide
+    where g < r.a
+    group by k) as s;
+set enable_groupagg = true;
+set work_mem to default;
+-- Compare group aggregation results to hash aggregation results
+(select * from agg_hash_wide_1 except select * from agg_group_wide_1)
+  union all
+(select * from agg_group_wide_1 except select * from agg_hash_wide_1);
+ k | c1 | c2 | c3 
+---+----+----+----
+(0 rows)
+
+(select * from agg_hash_wide_2 except select * from agg_group_wide_2)
+  union all
+(select * from agg_group_wide_2 except select * from agg_hash_wide_2);
+ a | k | c1 | c2 
+---+---+----+----
+(0 rows)
+
+drop table agg_data_wide;
+drop table agg_group_wide_1;
+drop table agg_group_wide_2;
+drop table agg_hash_wide_1;
+drop table agg_hash_wide_2;
diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql
index 0936088..c93c170 100644
--- a/src/test/regress/sql/aggregates.sql
+++ b/src/test/regress/sql/aggregates.sql
@@ -1803,3 +1803,79 @@ drop table agg_hash_1;
 drop table agg_hash_2;
 drop table agg_hash_3;
 drop table agg_hash_4;
+
+--
+-- Hash Aggregation Spill tests where only some of the input columns are
+-- needed. Check NULLs, by-ref values and respilling, with JIT, and a
+-- rescan, against sort-based results.
+--
+
+create table agg_data_wide as
+select g, g % 2500 as k, nullif(g % 7, 0) as n, (g % 300)::text as t,
+       repeat('x', 20) as pad1, g * 2 as pad2
+  from generate_series(0, 9999) g;
+analyze agg_data_wide;
+
+set work_mem = '64kB';
+
+-- Produce results with sorting.
+
+set enable_hashagg = false;
+
+create table agg_group_wide_1 as
+select k, count(n) as c1, sum(n::numeric) as c2, max(t) as c3
+  from agg_data_wide group by k;
+
+create table agg_group_wide_2 as
+select * from
+  (values (2500), (10000)) as r(a),
+  lateral (
+    select k, count(n) as c1, max(t) as c2
+    from agg_data_wide
+    where g < r.a
+    group by k) as s;
+
+-- Produce results with hash aggregation.
+
+set enable_hashagg = true;
+set enable_groupagg = false;
+
+set jit_above_cost = 0;
+
+explain (costs off)
+select k, count(n) as c1, sum(n::numeric) as c2, max(t) as c3
+  from agg_data_wide group by k;
+
+create table agg_hash_wide_1 as
+select k, count(n) as c1, sum(n::numeric) as c2, max(t) as c3
+  from agg_data_wide group by k;
+
+set jit_above_cost to default;
+
+create table agg_hash_wide_2 as
+select * from
+  (values (2500), (10000)) as r(a),
+  lateral (
+    select k, count(n) as c1, max(t) as c2
+    from agg_data_wide
+    where g < r.a
+    group by k) as s;
+
+set enable_groupagg = true;
+set work_mem to default;
+
+-- Compare group aggregation results to hash aggregation results
+
+(select * from agg_hash_wide_1 except select * from agg_group_wide_1)
+  union all
+(select * from agg_group_wide_1 except select * from agg_hash_wide_1);
+
+(select * from agg_hash_wide_2 except select * from agg_group_wide_2)
+  union all
+(select * from agg_group_wide_2 except select * from agg_hash_wide_2);
+
+drop table agg_data_wide;
+drop table agg_group_wide_1;
+drop table agg_group_wide_2;
+drop table agg_hash_wide_1;
+drop table agg_hash_wide_2;
-- 
2.47.3

