1997-04-06 08:07:13 +02:00
|
|
|
--
|
2000-01-05 18:32:29 +01:00
|
|
|
-- CREATE_AGGREGATE
|
1997-04-06 08:07:13 +02:00
|
|
|
--
|
|
|
|
|
|
|
|
-- 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';
|
|
|
|
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
|
1997-04-06 08:07:13 +02: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';
|
|
|
|
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,
|
Allow polymorphic aggregates to have non-polymorphic state data types.
Before 9.4, such an aggregate couldn't be declared, because its final
function would have to have polymorphic result type but no polymorphic
argument, which CREATE FUNCTION would quite properly reject. The
ordered-set-aggregate patch found a workaround: allow the final function
to be declared as accepting additional dummy arguments that have types
matching the aggregate's regular input arguments. However, we failed
to notice that this problem applies just as much to regular aggregates,
despite the fact that we had a built-in regular aggregate array_agg()
that was known to be undeclarable in SQL because its final function
had an illegal signature. So what we should have done, and what this
patch does, is to decouple the extra-dummy-arguments behavior from
ordered-set aggregates and make it generally available for all aggregate
declarations. We have to put this into 9.4 rather than waiting till
later because it slightly alters the rules for declaring ordered-set
aggregates.
The patch turned out a bit bigger than I'd hoped because it proved
necessary to record the extra-arguments option in a new pg_aggregate
column. I'd thought we could just look at the final function's pronargs
at runtime, but that didn't work well for variadic final functions.
It's probably just as well though, because it simplifies life for pg_dump
to record the option explicitly.
While at it, fix array_agg() to have a valid final-function signature,
and add an opr_sanity test to notice future deviations from polymorphic
consistency. I also marked the percentile_cont() aggregates as not
needing extra arguments, since they don't.
2014-04-24 01:17:31 +02:00
|
|
|
finalfunc = percentile_disc_final,
|
|
|
|
finalfunc_extra = true
|
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
|
|
|
);
|
|
|
|
|
|
|
|
create aggregate my_rank(VARIADIC "any" ORDER BY VARIADIC "any") (
|
|
|
|
stype = internal,
|
|
|
|
sfunc = ordered_set_transition_multi,
|
|
|
|
finalfunc = rank_final,
|
Allow polymorphic aggregates to have non-polymorphic state data types.
Before 9.4, such an aggregate couldn't be declared, because its final
function would have to have polymorphic result type but no polymorphic
argument, which CREATE FUNCTION would quite properly reject. The
ordered-set-aggregate patch found a workaround: allow the final function
to be declared as accepting additional dummy arguments that have types
matching the aggregate's regular input arguments. However, we failed
to notice that this problem applies just as much to regular aggregates,
despite the fact that we had a built-in regular aggregate array_agg()
that was known to be undeclarable in SQL because its final function
had an illegal signature. So what we should have done, and what this
patch does, is to decouple the extra-dummy-arguments behavior from
ordered-set aggregates and make it generally available for all aggregate
declarations. We have to put this into 9.4 rather than waiting till
later because it slightly alters the rules for declaring ordered-set
aggregates.
The patch turned out a bit bigger than I'd hoped because it proved
necessary to record the extra-arguments option in a new pg_aggregate
column. I'd thought we could just look at the final function's pronargs
at runtime, but that didn't work well for variadic final functions.
It's probably just as well though, because it simplifies life for pg_dump
to record the option explicitly.
While at it, fix array_agg() to have a valid final-function signature,
and add an opr_sanity test to notice future deviations from polymorphic
consistency. I also marked the percentile_cont() aggregates as not
needing extra arguments, since they don't.
2014-04-24 01:17:31 +02:00
|
|
|
finalfunc_extra = true,
|
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
|
|
|
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_*
|
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
|
|
|
|
);
|
|
|
|
|
2016-01-20 19:46:50 +01:00
|
|
|
-- Test aggregate combine function
|
|
|
|
|
|
|
|
-- ensure create aggregate works.
|
|
|
|
CREATE AGGREGATE mysum (int)
|
|
|
|
(
|
|
|
|
stype = int,
|
|
|
|
sfunc = int4pl,
|
|
|
|
combinefunc = int4pl
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Ensure all these functions made it into the catalog
|
|
|
|
SELECT aggfnoid,aggtransfn,aggcombinefn,aggtranstype
|
|
|
|
FROM pg_aggregate
|
|
|
|
WHERE aggfnoid = 'mysum'::REGPROC;
|
|
|
|
|
|
|
|
DROP AGGREGATE mysum (int);
|
|
|
|
|
2014-04-12 17:58:53 +02:00
|
|
|
-- 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
|
|
|
|
);
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
);
|