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,12 +21,15 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
ALTER AGGREGATE <replaceable>name</replaceable> ( [ <replaceable>argmode</replaceable> ] [ <replaceable>arg_name</replaceable> ] <replaceable>arg_data_type</replaceable> [ , ... ] )
RENAME TO <replaceable>new_name</replaceable>
ALTER AGGREGATE <replaceable>name</replaceable> ( [ <replaceable>argmode</replaceable> ] [ <replaceable>arg_name</replaceable> ] <replaceable>arg_data_type</replaceable> [ , ... ] )
OWNER TO <replaceable>new_owner</replaceable>
ALTER AGGREGATE <replaceable>name</replaceable> ( [ <replaceable>argmode</replaceable> ] [ <replaceable>arg_name</replaceable> ] <replaceable>arg_data_type</replaceable> [ , ... ] )
SET SCHEMA <replaceable>new_schema</replaceable>
ALTER AGGREGATE <replaceable>name</replaceable> ( <replaceable>aggregate_signature</replaceable> ) RENAME TO <replaceable>new_name</replaceable>
ALTER AGGREGATE <replaceable>name</replaceable> ( <replaceable>aggregate_signature</replaceable> ) OWNER TO <replaceable>new_owner</replaceable>
ALTER AGGREGATE <replaceable>name</replaceable> ( <replaceable>aggregate_signature</replaceable> ) SET SCHEMA <replaceable>new_schema</replaceable>
<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>
@@ -76,7 +79,7 @@ ALTER AGGREGATE <replaceable>name</replaceable> ( [ <replaceable>argmode</replac
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">arg_name</replaceable></term>
<term><replaceable class="parameter">argname</replaceable></term>
<listitem>
<para>
@@ -89,12 +92,15 @@ ALTER AGGREGATE <replaceable>name</replaceable> ( [ <replaceable>argmode</replac
</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>
@@ -128,6 +134,21 @@ ALTER AGGREGATE <replaceable>name</replaceable> ( [ <replaceable>argmode</replac
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para>
The recommended syntax for referencing an ordered-set aggregate
is to write <literal>ORDER BY</> between the direct and aggregated
argument specifications, in the same style as in
<xref linkend="sql-createaggregate">. However, it will also work to
omit <literal>ORDER BY</> and just run the direct and aggregated
argument specifications into a single list. In this abbreviated form,
if <literal>VARIADIC "any"</> was used in both the direct and
aggregated argument lists, write <literal>VARIADIC "any"</> only once.
</para>
</refsect1>
<refsect1>
<title>Examples</title>
@@ -148,11 +169,17 @@ ALTER AGGREGATE myavg(integer) OWNER TO joe;
</para>
<para>
To move the aggregate function <literal>myavg</literal> for type
<type>integer</type> into schema <literal>myschema</literal>:
To move the ordered-set aggregate <literal>mypercentile</literal> with
direct argument of type <type>float8</type> and aggregated argument
of type <type>integer</type> into schema <literal>myschema</literal>:
<programlisting>
ALTER AGGREGATE myavg(integer) SET SCHEMA myschema;
</programlisting></para>
ALTER AGGREGATE mypercentile(float8 ORDER BY integer) SET SCHEMA myschema;
</programlisting>
This will work too:
<programlisting>
ALTER AGGREGATE mypercentile(float8, integer) SET SCHEMA myschema;
</programlisting>
</para>
</refsect1>
<refsect1>