2000-01-05 18:31:08 +01:00
|
|
|
--
|
|
|
|
-- CREATE_AGGREGATE
|
|
|
|
--
|
|
|
|
-- all functions CREATEd
|
|
|
|
CREATE AGGREGATE newavg (
|
2010-11-23 21:27:50 +01:00
|
|
|
sfunc = int4_avg_accum, basetype = int4, stype = _int8,
|
2007-02-17 01:55:58 +01:00
|
|
|
finalfunc = int8_avg,
|
|
|
|
initcond1 = '{0,0}'
|
1997-04-06 08:07:13 +02:00
|
|
|
);
|
2003-11-21 23:32:49 +01:00
|
|
|
-- test comments
|
|
|
|
COMMENT ON AGGREGATE newavg_wrong (int4) IS 'an agg comment';
|
|
|
|
ERROR: aggregate newavg_wrong(integer) does not exist
|
|
|
|
COMMENT ON AGGREGATE newavg (int4) IS 'an agg comment';
|
|
|
|
COMMENT ON AGGREGATE newavg (int4) IS NULL;
|
2000-07-17 05:05:41 +02:00
|
|
|
-- without finalfunc; test obsolete spellings 'sfunc1' etc
|
2000-01-05 18:31:08 +01:00
|
|
|
CREATE AGGREGATE newsum (
|
2010-11-23 21:27:50 +01:00
|
|
|
sfunc1 = int4pl, basetype = int4, stype1 = int4,
|
1997-04-06 08:07:13 +02:00
|
|
|
initcond1 = '0'
|
|
|
|
);
|
2006-07-27 21:52:07 +02:00
|
|
|
-- zero-argument aggregate
|
|
|
|
CREATE AGGREGATE newcnt (*) (
|
|
|
|
sfunc = int8inc, stype = int8,
|
|
|
|
initcond = '0'
|
|
|
|
);
|
|
|
|
-- old-style spelling of same
|
|
|
|
CREATE AGGREGATE oldcnt (
|
|
|
|
sfunc = int8inc, basetype = 'ANY', stype = int8,
|
|
|
|
initcond = '0'
|
|
|
|
);
|
|
|
|
-- aggregate that only cares about null/nonnull input
|
|
|
|
CREATE AGGREGATE newcnt ("any") (
|
|
|
|
sfunc = int8inc_any, stype = int8,
|
|
|
|
initcond = '0'
|
|
|
|
);
|
2009-12-15 18:57:48 +01:00
|
|
|
COMMENT ON AGGREGATE nosuchagg (*) IS 'should fail';
|
|
|
|
ERROR: aggregate nosuchagg(*) does not exist
|
|
|
|
COMMENT ON AGGREGATE newcnt (*) IS 'an agg(*) comment';
|
|
|
|
COMMENT ON AGGREGATE newcnt ("any") IS 'an agg(any) comment';
|
2006-07-27 21:52:07 +02:00
|
|
|
-- multi-argument aggregate
|
|
|
|
create function sum3(int8,int8,int8) returns int8 as
|
|
|
|
'select $1 + $2 + $3' language sql strict immutable;
|
|
|
|
create aggregate sum2(int8,int8) (
|
|
|
|
sfunc = sum3, stype = int8,
|
2000-07-17 05:05:41 +02:00
|
|
|
initcond = '0'
|
1997-04-06 08:07:13 +02:00
|
|
|
);
|
2009-12-15 18:57:48 +01:00
|
|
|
-- multi-argument aggregates sensitive to distinct/order, strict/nonstrict
|
|
|
|
create type aggtype as (a integer, b integer, c text);
|
|
|
|
create function aggf_trans(aggtype[],integer,integer,text) returns aggtype[]
|
|
|
|
as 'select array_append($1,ROW($2,$3,$4)::aggtype)'
|
|
|
|
language sql strict immutable;
|
|
|
|
create function aggfns_trans(aggtype[],integer,integer,text) returns aggtype[]
|
|
|
|
as 'select array_append($1,ROW($2,$3,$4)::aggtype)'
|
|
|
|
language sql immutable;
|
|
|
|
create aggregate aggfstr(integer,integer,text) (
|
|
|
|
sfunc = aggf_trans, stype = aggtype[],
|
|
|
|
initcond = '{}'
|
|
|
|
);
|
|
|
|
create aggregate aggfns(integer,integer,text) (
|
2013-11-16 22:03:40 +01:00
|
|
|
sfunc = aggfns_trans, stype = aggtype[], sspace = 10000,
|
2009-12-15 18:57:48 +01:00
|
|
|
initcond = '{}'
|
|
|
|
);
|
Allow aggregate functions to be VARIADIC.
There's no inherent reason why an aggregate function can't be variadic
(even VARIADIC ANY) if its transition function can handle the case.
Indeed, this patch to add the feature touches none of the planner or
executor, and little of the parser; the main missing stuff was DDL and
pg_dump support.
It is true that variadic aggregates can create the same sort of ambiguity
about parameters versus ORDER BY keys that was complained of when we
(briefly) had both one- and two-argument forms of string_agg(). However,
the policy formed in response to that discussion only said that we'd not
create any built-in aggregates with varying numbers of arguments, not that
we shouldn't allow users to do it. So the logical extension of that is
we can allow users to make variadic aggregates as long as we're wary about
shipping any such in core.
In passing, this patch allows aggregate function arguments to be named, to
the extent of remembering the names in pg_proc and dumping them in pg_dump.
You can't yet call an aggregate using named-parameter notation. That seems
like a likely future extension, but it'll take some work, and it's not what
this patch is really about. Likewise, there's still some work needed to
make window functions handle VARIADIC fully, but I left that for another
day.
initdb forced because of new aggvariadic field in Aggref parse nodes.
2013-09-03 23:08:38 +02:00
|
|
|
-- variadic aggregate
|
|
|
|
create function least_accum(anyelement, variadic anyarray)
|
|
|
|
returns anyelement language sql as
|
|
|
|
'select least($1, min($2[i])) from generate_subscripts($2,1) g(i)';
|
|
|
|
create aggregate least_agg(variadic items anyarray) (
|
|
|
|
stype = anyelement, sfunc = least_accum
|
|
|
|
);
|