summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/backend/executor/nodeAgg.c34
-rw-r--r--src/test/regress/expected/aggregates.out25
-rw-r--r--src/test/regress/sql/aggregates.sql7
3 files changed, 43 insertions, 23 deletions
diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index d01fc4f52e1..fd3c71e7641 100644
--- a/src/backend/executor/nodeAgg.c
+++ b/src/backend/executor/nodeAgg.c
@@ -2911,12 +2911,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
@@ -2926,6 +2920,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,
@@ -2938,7 +2943,7 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans,
(FunctionCallInfo) palloc(SizeForFunctionCallInfo(2));
InitFunctionCallInfoData(*pertrans->transfn_fcinfo,
&pertrans->transfn,
- 2,
+ numTransArgs,
pertrans->aggCollation,
(void *) aggstate, NULL);
@@ -2956,7 +2961,16 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans,
else
{
Expr *transfnexpr;
- size_t numInputs = pertrans->numTransInputs + 1;
+ 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
@@ -2976,10 +2990,10 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans,
fmgr_info_set_expr((Node *) transfnexpr, &pertrans->transfn);
pertrans->transfn_fcinfo =
- (FunctionCallInfo) palloc(SizeForFunctionCallInfo(numInputs));
+ (FunctionCallInfo) palloc(SizeForFunctionCallInfo(numTransArgs));
InitFunctionCallInfoData(*pertrans->transfn_fcinfo,
&pertrans->transfn,
- numInputs,
+ numTransArgs,
pertrans->aggCollation,
(void *) aggstate, NULL);
diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out
index 129c1e5075f..fed69dc9e19 100644
--- a/src/test/regress/expected/aggregates.out
+++ b/src/test/regress/expected/aggregates.out
@@ -2204,21 +2204,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 d4fd6571886..230f44666aa 100644
--- a/src/test/regress/sql/aggregates.sql
+++ b/src/test/regress/sql/aggregates.sql
@@ -963,10 +963,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;