aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/backend/executor/nodeAgg.c31
-rw-r--r--src/test/regress/expected/aggregates.out25
-rw-r--r--src/test/regress/sql/aggregates.sql7
3 files changed, 42 insertions, 21 deletions
diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index af93b4db6c0..70fb457f930 100644
--- a/src/backend/executor/nodeAgg.c
+++ b/src/backend/executor/nodeAgg.c
@@ -2907,12 +2907,6 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans,
pertrans->aggtranstype = aggtranstype;
- /* Detect how many arguments to pass to the transfn */
- if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
- pertrans->numTransInputs = numInputs;
- else
- pertrans->numTransInputs = numArguments;
-
/*
* When combining states, we have no use at all for the aggregate
* function's transfn. Instead we use the combinefn. In this case, the
@@ -2922,6 +2916,17 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans,
if (DO_AGGSPLIT_COMBINE(aggstate->aggsplit))
{
Expr *combinefnexpr;
+ size_t numTransArgs;
+
+ /*
+ * When combining there's only one input, the to-be-combined added
+ * transition value from below (this node's transition value is
+ * counted separately).
+ */
+ pertrans->numTransInputs = 1;
+
+ /* account for the current transition state */
+ numTransArgs = pertrans->numTransInputs + 1;
build_aggregate_combinefn_expr(aggtranstype,
aggref->inputcollid,
@@ -2932,7 +2937,7 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans,
InitFunctionCallInfoData(pertrans->transfn_fcinfo,
&pertrans->transfn,
- 2,
+ numTransArgs,
pertrans->aggCollation,
(void *) aggstate, NULL);
@@ -2950,6 +2955,16 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans,
else
{
Expr *transfnexpr;
+ size_t numTransArgs;
+
+ /* Detect how many arguments to pass to the transfn */
+ if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
+ pertrans->numTransInputs = numInputs;
+ else
+ pertrans->numTransInputs = numArguments;
+
+ /* account for the current transition state */
+ numTransArgs = pertrans->numTransInputs + 1;
/*
* Set up infrastructure for calling the transfn. Note that invtrans
@@ -2970,7 +2985,7 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans,
InitFunctionCallInfoData(pertrans->transfn_fcinfo,
&pertrans->transfn,
- pertrans->numTransInputs + 1,
+ numTransArgs,
pertrans->aggCollation,
(void *) aggstate, NULL);
diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out
index deed5bef469..637aee14fca 100644
--- a/src/test/regress/expected/aggregates.out
+++ b/src/test/regress/expected/aggregates.out
@@ -2074,21 +2074,26 @@ SET max_parallel_workers_per_gather = 4;
SET enable_indexonlyscan = off;
-- variance(int4) covers numeric_poly_combine
-- sum(int8) covers int8_avg_combine
-EXPLAIN (COSTS OFF)
- SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
- QUERY PLAN
-----------------------------------------------
+-- regr_count(float8, float8) covers int8inc_float8_float8 and aggregates with > 1 arg
+EXPLAIN (COSTS OFF, VERBOSE)
+ SELECT variance(unique1::int4), sum(unique1::int8), regr_count(unique1::float8, unique1::float8) FROM tenk1;
+ QUERY PLAN
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate
+ Output: variance(unique1), sum((unique1)::bigint), regr_count((unique1)::double precision, (unique1)::double precision)
-> Gather
+ Output: (PARTIAL variance(unique1)), (PARTIAL sum((unique1)::bigint)), (PARTIAL regr_count((unique1)::double precision, (unique1)::double precision))
Workers Planned: 4
-> Partial Aggregate
- -> Parallel Seq Scan on tenk1
-(5 rows)
+ Output: PARTIAL variance(unique1), PARTIAL sum((unique1)::bigint), PARTIAL regr_count((unique1)::double precision, (unique1)::double precision)
+ -> Parallel Seq Scan on public.tenk1
+ Output: unique1, unique2, two, four, ten, twenty, hundred, thousand, twothousand, fivethous, tenthous, odd, even, stringu1, stringu2, string4
+(9 rows)
-SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
- variance | sum
-----------------------+----------
- 8334166.666666666667 | 49995000
+SELECT variance(unique1::int4), sum(unique1::int8), regr_count(unique1::float8, unique1::float8) FROM tenk1;
+ variance | sum | regr_count
+----------------------+----------+------------
+ 8334166.666666666667 | 49995000 | 10000
(1 row)
ROLLBACK;
diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql
index a03f897bcc2..f22dd776f41 100644
--- a/src/test/regress/sql/aggregates.sql
+++ b/src/test/regress/sql/aggregates.sql
@@ -919,10 +919,11 @@ SET enable_indexonlyscan = off;
-- variance(int4) covers numeric_poly_combine
-- sum(int8) covers int8_avg_combine
-EXPLAIN (COSTS OFF)
- SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
+-- regr_count(float8, float8) covers int8inc_float8_float8 and aggregates with > 1 arg
+EXPLAIN (COSTS OFF, VERBOSE)
+ SELECT variance(unique1::int4), sum(unique1::int8), regr_count(unique1::float8, unique1::float8) FROM tenk1;
-SELECT variance(unique1::int4), sum(unique1::int8) FROM tenk1;
+SELECT variance(unique1::int4), sum(unique1::int8), regr_count(unique1::float8, unique1::float8) FROM tenk1;
ROLLBACK;