Commit ac4a9d92 authored by Tom Lane's avatar Tom Lane

Fix incorrect handling of polymorphic aggregates used as window functions.

The transfunction was told that its first argument and result were
of the window function output type, not the aggregate state type.
This'd only matter if the transfunction consults get_fn_expr_argtype,
which typically only polymorphic functions would do.

Although we have several regression tests around polymorphic aggs,
none of them detected this mistake --- in fact, they still didn't
fail when I injected the same mistake into nodeAgg.c.  So add some
more tests covering both plain agg and window-function-agg cases.

Per report from Sebastian Luque.  Back-patch to 9.6 where the error
was introduced (by sloppy refactoring in commit 804163bc, looks like).

Report: <87int2qkat.fsf@gmail.com>
parent e55a946a
...@@ -2218,7 +2218,7 @@ initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc, ...@@ -2218,7 +2218,7 @@ initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc,
numArguments, numArguments,
0, /* no ordered-set window functions yet */ 0, /* no ordered-set window functions yet */
false, /* no variadic window functions yet */ false, /* no variadic window functions yet */
wfunc->wintype, aggtranstype,
wfunc->inputcollid, wfunc->inputcollid,
transfn_oid, transfn_oid,
invtransfn_oid, invtransfn_oid,
......
...@@ -635,6 +635,61 @@ create aggregate build_group(int8, integer) ( ...@@ -635,6 +635,61 @@ create aggregate build_group(int8, integer) (
SFUNC = add_group, SFUNC = add_group,
STYPE = int8[] STYPE = int8[]
); );
-- check proper resolution of data types for polymorphic transfn/finalfn
create function first_el(anyarray) returns anyelement as
'select $1[1]' language sql strict immutable;
create aggregate first_el_agg_f8(float8) (
SFUNC = array_append,
STYPE = float8[],
FINALFUNC = first_el
);
create aggregate first_el_agg_any(anyelement) (
SFUNC = array_append,
STYPE = anyarray,
FINALFUNC = first_el
);
select first_el_agg_f8(x::float8) from generate_series(1,10) x;
first_el_agg_f8
-----------------
1
(1 row)
select first_el_agg_any(x) from generate_series(1,10) x;
first_el_agg_any
------------------
1
(1 row)
select first_el_agg_f8(x::float8) over(order by x) from generate_series(1,10) x;
first_el_agg_f8
-----------------
1
1
1
1
1
1
1
1
1
1
(10 rows)
select first_el_agg_any(x) over(order by x) from generate_series(1,10) x;
first_el_agg_any
------------------
1
1
1
1
1
1
1
1
1
1
(10 rows)
-- check that we can apply functions taking ANYARRAY to pg_stats -- check that we can apply functions taking ANYARRAY to pg_stats
select distinct array_ndims(histogram_bounds) from pg_stats select distinct array_ndims(histogram_bounds) from pg_stats
where histogram_bounds is not null; where histogram_bounds is not null;
......
...@@ -443,6 +443,28 @@ create aggregate build_group(int8, integer) ( ...@@ -443,6 +443,28 @@ create aggregate build_group(int8, integer) (
STYPE = int8[] STYPE = int8[]
); );
-- check proper resolution of data types for polymorphic transfn/finalfn
create function first_el(anyarray) returns anyelement as
'select $1[1]' language sql strict immutable;
create aggregate first_el_agg_f8(float8) (
SFUNC = array_append,
STYPE = float8[],
FINALFUNC = first_el
);
create aggregate first_el_agg_any(anyelement) (
SFUNC = array_append,
STYPE = anyarray,
FINALFUNC = first_el
);
select first_el_agg_f8(x::float8) from generate_series(1,10) x;
select first_el_agg_any(x) from generate_series(1,10) x;
select first_el_agg_f8(x::float8) over(order by x) from generate_series(1,10) x;
select first_el_agg_any(x) over(order by x) from generate_series(1,10) x;
-- check that we can apply functions taking ANYARRAY to pg_stats -- check that we can apply functions taking ANYARRAY to pg_stats
select distinct array_ndims(histogram_bounds) from pg_stats select distinct array_ndims(histogram_bounds) from pg_stats
where histogram_bounds is not null; where histogram_bounds is not null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment