mirror of
https://github.com/postgres/postgres.git
synced 2025-08-19 23:22:23 +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:
@@ -225,8 +225,9 @@ typedef struct Param
|
||||
/*
|
||||
* Aggref
|
||||
*
|
||||
* The aggregate's args list is a targetlist, ie, a list of TargetEntry nodes
|
||||
* (before Postgres 9.0 it was just bare expressions). The non-resjunk TLEs
|
||||
* The aggregate's args list is a targetlist, ie, a list of TargetEntry nodes.
|
||||
*
|
||||
* For a normal (non-ordered-set) aggregate, the non-resjunk TargetEntries
|
||||
* represent the aggregate's regular arguments (if any) and resjunk TLEs can
|
||||
* be added at the end to represent ORDER BY expressions that are not also
|
||||
* arguments. As in a top-level Query, the TLEs can be marked with
|
||||
@@ -236,6 +237,12 @@ typedef struct Param
|
||||
* they are passed to the transition function. The grammar only allows a
|
||||
* simple "DISTINCT" specifier for the arguments, but we use the full
|
||||
* query-level representation to allow more code sharing.
|
||||
*
|
||||
* For an ordered-set aggregate, the args list represents the WITHIN GROUP
|
||||
* (aggregated) arguments, all of which will be listed in the aggorder list.
|
||||
* DISTINCT is not supported in this case, so aggdistinct will be NIL.
|
||||
* The direct arguments appear in aggdirectargs (as a list of plain
|
||||
* expressions, not TargetEntry nodes).
|
||||
*/
|
||||
typedef struct Aggref
|
||||
{
|
||||
@@ -244,12 +251,14 @@ typedef struct Aggref
|
||||
Oid aggtype; /* type Oid of result of the aggregate */
|
||||
Oid aggcollid; /* OID of collation of result */
|
||||
Oid inputcollid; /* OID of collation that function should use */
|
||||
List *args; /* arguments and sort expressions */
|
||||
List *aggdirectargs; /* direct arguments, if an ordered-set agg */
|
||||
List *args; /* aggregated arguments and sort expressions */
|
||||
List *aggorder; /* ORDER BY (list of SortGroupClause) */
|
||||
List *aggdistinct; /* DISTINCT (list of SortGroupClause) */
|
||||
Expr *aggfilter; /* FILTER expression */
|
||||
Expr *aggfilter; /* FILTER expression, if any */
|
||||
bool aggstar; /* TRUE if argument list was really '*' */
|
||||
bool aggvariadic; /* TRUE if VARIADIC was used in call */
|
||||
char aggkind; /* aggregate kind (see pg_aggregate.h) */
|
||||
Index agglevelsup; /* > 0 if agg belongs to outer query */
|
||||
int location; /* token location, or -1 if unknown */
|
||||
} Aggref;
|
||||
@@ -265,7 +274,7 @@ typedef struct WindowFunc
|
||||
Oid wincollid; /* OID of collation of result */
|
||||
Oid inputcollid; /* OID of collation that function should use */
|
||||
List *args; /* arguments to the window function */
|
||||
Expr *aggfilter; /* FILTER expression */
|
||||
Expr *aggfilter; /* FILTER expression, if any */
|
||||
Index winref; /* index of associated WindowClause */
|
||||
bool winstar; /* TRUE if argument list was really '*' */
|
||||
bool winagg; /* is function a simple aggregate? */
|
||||
|
Reference in New Issue
Block a user