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
|
|
|
|
);
|
Support ordered-set (WITHIN GROUP) aggregates.
This patch introduces generic support for ordered-set and hypothetical-set
aggregate functions, as well as implementations of the instances defined in
SQL:2008 (percentile_cont(), percentile_disc(), rank(), dense_rank(),
percent_rank(), cume_dist()). We also added mode() though it is not in the
spec, as well as versions of percentile_cont() and percentile_disc() that
can compute multiple percentile values in one pass over the data.
Unlike the original submission, this patch puts full control of the sorting
process in the hands of the aggregate's support functions. To allow the
support functions to find out how they're supposed to sort, a new API
function AggGetAggref() is added to nodeAgg.c. This allows retrieval of
the aggregate call's Aggref node, which may have other uses beyond the
immediate need. There is also support for ordered-set aggregates to
install cleanup callback functions, so that they can be sure that
infrastructure such as tuplesort objects gets cleaned up.
In passing, make some fixes in the recently-added support for variadic
aggregates, and make some editorial adjustments in the recent FILTER
additions for aggregates. Also, simplify use of IsBinaryCoercible() by
allowing it to succeed whenever the target type is ANY or ANYELEMENT.
It was inconsistent that it dealt with other polymorphic target types
but not these.
Atri Sharma and Andrew Gierth; reviewed by Pavel Stehule and Vik Fearing,
and rather heavily editorialized upon by Tom Lane
2013-12-23 22:11:35 +01:00
|
|
|
-- test ordered-set aggs using built-in support functions
|
|
|
|
create aggregate my_percentile_disc(float8 ORDER BY anyelement) (
|
|
|
|
stype = internal,
|
|
|
|
sfunc = ordered_set_transition,
|
|
|
|
finalfunc = percentile_disc_final
|
|
|
|
);
|
|
|
|
create aggregate my_rank(VARIADIC "any" ORDER BY VARIADIC "any") (
|
|
|
|
stype = internal,
|
|
|
|
sfunc = ordered_set_transition_multi,
|
|
|
|
finalfunc = rank_final,
|
|
|
|
hypothetical
|
|
|
|
);
|
|
|
|
alter aggregate my_percentile_disc(float8 ORDER BY anyelement)
|
|
|
|
rename to test_percentile_disc;
|
|
|
|
alter aggregate my_rank(VARIADIC "any" ORDER BY VARIADIC "any")
|
|
|
|
rename to test_rank;
|
|
|
|
\da test_*
|
|
|
|
List of aggregate functions
|
|
|
|
Schema | Name | Result data type | Argument data types | Description
|
|
|
|
--------+----------------------+------------------+----------------------------------------+-------------
|
|
|
|
public | test_percentile_disc | anyelement | double precision ORDER BY anyelement |
|
|
|
|
public | test_rank | bigint | VARIADIC "any" ORDER BY VARIADIC "any" |
|
|
|
|
(2 rows)
|
|
|
|
|
2014-04-12 17:58:53 +02:00
|
|
|
-- moving-aggregate options
|
|
|
|
CREATE AGGREGATE sumdouble (float8)
|
|
|
|
(
|
|
|
|
stype = float8,
|
|
|
|
sfunc = float8pl,
|
|
|
|
mstype = float8,
|
|
|
|
msfunc = float8pl,
|
|
|
|
minvfunc = float8mi
|
|
|
|
);
|
|
|
|
-- invalid: nonstrict inverse with strict forward function
|
|
|
|
CREATE FUNCTION float8mi_n(float8, float8) RETURNS float8 AS
|
|
|
|
$$ SELECT $1 - $2; $$
|
|
|
|
LANGUAGE SQL;
|
|
|
|
CREATE AGGREGATE invalidsumdouble (float8)
|
|
|
|
(
|
|
|
|
stype = float8,
|
|
|
|
sfunc = float8pl,
|
|
|
|
mstype = float8,
|
|
|
|
msfunc = float8pl,
|
|
|
|
minvfunc = float8mi_n
|
|
|
|
);
|
|
|
|
ERROR: strictness of aggregate's forward and inverse transition functions must match
|
|
|
|
-- invalid: non-matching result types
|
|
|
|
CREATE FUNCTION float8mi_int(float8, float8) RETURNS int AS
|
|
|
|
$$ SELECT CAST($1 - $2 AS INT); $$
|
|
|
|
LANGUAGE SQL;
|
|
|
|
CREATE AGGREGATE wrongreturntype (float8)
|
|
|
|
(
|
|
|
|
stype = float8,
|
|
|
|
sfunc = float8pl,
|
|
|
|
mstype = float8,
|
|
|
|
msfunc = float8pl,
|
|
|
|
minvfunc = float8mi_int
|
|
|
|
);
|
|
|
|
ERROR: return type of inverse transition function float8mi_int is not double precision
|