Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ac4a9d9

Browse files
committed
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 804163b, looks like). Report: <87int2qkat.fsf@gmail.com>
1 parent e55a946 commit ac4a9d9

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/backend/executor/nodeWindowAgg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,7 @@ initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc,
22182218
numArguments,
22192219
0, /* no ordered-set window functions yet */
22202220
false, /* no variadic window functions yet */
2221-
wfunc->wintype,
2221+
aggtranstype,
22222222
wfunc->inputcollid,
22232223
transfn_oid,
22242224
invtransfn_oid,

src/test/regress/expected/polymorphism.out

+55
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,61 @@ create aggregate build_group(int8, integer) (
635635
SFUNC = add_group,
636636
STYPE = int8[]
637637
);
638+
-- check proper resolution of data types for polymorphic transfn/finalfn
639+
create function first_el(anyarray) returns anyelement as
640+
'select $1[1]' language sql strict immutable;
641+
create aggregate first_el_agg_f8(float8) (
642+
SFUNC = array_append,
643+
STYPE = float8[],
644+
FINALFUNC = first_el
645+
);
646+
create aggregate first_el_agg_any(anyelement) (
647+
SFUNC = array_append,
648+
STYPE = anyarray,
649+
FINALFUNC = first_el
650+
);
651+
select first_el_agg_f8(x::float8) from generate_series(1,10) x;
652+
first_el_agg_f8
653+
-----------------
654+
1
655+
(1 row)
656+
657+
select first_el_agg_any(x) from generate_series(1,10) x;
658+
first_el_agg_any
659+
------------------
660+
1
661+
(1 row)
662+
663+
select first_el_agg_f8(x::float8) over(order by x) from generate_series(1,10) x;
664+
first_el_agg_f8
665+
-----------------
666+
1
667+
1
668+
1
669+
1
670+
1
671+
1
672+
1
673+
1
674+
1
675+
1
676+
(10 rows)
677+
678+
select first_el_agg_any(x) over(order by x) from generate_series(1,10) x;
679+
first_el_agg_any
680+
------------------
681+
1
682+
1
683+
1
684+
1
685+
1
686+
1
687+
1
688+
1
689+
1
690+
1
691+
(10 rows)
692+
638693
-- check that we can apply functions taking ANYARRAY to pg_stats
639694
select distinct array_ndims(histogram_bounds) from pg_stats
640695
where histogram_bounds is not null;

src/test/regress/sql/polymorphism.sql

+22
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,28 @@ create aggregate build_group(int8, integer) (
443443
STYPE = int8[]
444444
);
445445

446+
-- check proper resolution of data types for polymorphic transfn/finalfn
447+
448+
create function first_el(anyarray) returns anyelement as
449+
'select $1[1]' language sql strict immutable;
450+
451+
create aggregate first_el_agg_f8(float8) (
452+
SFUNC = array_append,
453+
STYPE = float8[],
454+
FINALFUNC = first_el
455+
);
456+
457+
create aggregate first_el_agg_any(anyelement) (
458+
SFUNC = array_append,
459+
STYPE = anyarray,
460+
FINALFUNC = first_el
461+
);
462+
463+
select first_el_agg_f8(x::float8) from generate_series(1,10) x;
464+
select first_el_agg_any(x) from generate_series(1,10) x;
465+
select first_el_agg_f8(x::float8) over(order by x) from generate_series(1,10) x;
466+
select first_el_agg_any(x) over(order by x) from generate_series(1,10) x;
467+
446468
-- check that we can apply functions taking ANYARRAY to pg_stats
447469
select distinct array_ndims(histogram_bounds) from pg_stats
448470
where histogram_bounds is not null;

0 commit comments

Comments
 (0)