1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-19 17:02:53 +03:00

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
This commit is contained in:
Tom Lane
2013-12-23 16:11:35 -05:00
parent 37484ad2aa
commit 8d65da1f01
64 changed files with 4686 additions and 755 deletions

View File

@@ -21,9 +21,13 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
DROP AGGREGATE [ IF EXISTS ]
<replaceable class="parameter">name</replaceable> ( [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">arg_name</replaceable> ] <replaceable class="parameter">arg_data_type</replaceable> [ , ... ] )
[ CASCADE | RESTRICT ]
DROP AGGREGATE [ IF EXISTS ] <replaceable>name</replaceable> ( <replaceable>aggregate_signature</replaceable> ) [ CASCADE | RESTRICT ]
<phrase>where <replaceable>aggregate_signature</replaceable> is:</phrase>
* |
[ <replaceable>argmode</replaceable> ] [ <replaceable>argname</replaceable> ] <replaceable>argtype</replaceable> [ , ... ] |
[ [ <replaceable>argmode</replaceable> ] [ <replaceable>argname</replaceable> ] <replaceable>argtype</replaceable> [ , ... ] ] ORDER BY [ <replaceable>argmode</replaceable> ] [ <replaceable>argname</replaceable> ] <replaceable>argtype</replaceable> [ , ... ]
</synopsis>
</refsynopsisdiv>
@@ -73,7 +77,7 @@ DROP AGGREGATE [ IF EXISTS ]
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">arg_name</replaceable></term>
<term><replaceable class="parameter">argname</replaceable></term>
<listitem>
<para>
@@ -86,12 +90,15 @@ DROP AGGREGATE [ IF EXISTS ]
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">arg_data_type</replaceable></term>
<term><replaceable class="parameter">argtype</replaceable></term>
<listitem>
<para>
An input data type on which the aggregate function operates.
To reference a zero-argument aggregate function, write <literal>*</>
in place of the list of argument specifications.
To reference an ordered-set aggregate function, write
<literal>ORDER BY</> between the direct and aggregated argument
specifications.
</para>
</listitem>
</varlistentry>
@@ -117,6 +124,15 @@ DROP AGGREGATE [ IF EXISTS ]
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para>
Alternative syntaxes for referencing ordered-set aggregates
are described under <xref linkend="sql-alteraggregate">.
</para>
</refsect1>
<refsect1>
<title>Examples</title>
@@ -125,7 +141,17 @@ DROP AGGREGATE [ IF EXISTS ]
<type>integer</type>:
<programlisting>
DROP AGGREGATE myavg(integer);
</programlisting></para>
</programlisting>
</para>
<para>
To remove the hypothetical-set aggregate function <literal>myrank</>,
which takes an arbitrary list of ordering columns and a matching list
of direct arguments:
<programlisting>
DROP AGGREGATE myrank(VARIADIC "any" ORDER BY VARIADIC "any");
</programlisting>
</para>
</refsect1>
<refsect1>