mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +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:
@@ -1717,6 +1717,69 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward,
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Advance over N tuples in either forward or back direction,
|
||||
* without returning any data. N==0 is a no-op.
|
||||
* Returns TRUE if successful, FALSE if ran out of tuples.
|
||||
*/
|
||||
bool
|
||||
tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward)
|
||||
{
|
||||
/*
|
||||
* We don't actually support backwards skip yet, because no callers need
|
||||
* it. The API is designed to allow for that later, though.
|
||||
*/
|
||||
Assert(forward);
|
||||
Assert(ntuples >= 0);
|
||||
|
||||
switch (state->status)
|
||||
{
|
||||
case TSS_SORTEDINMEM:
|
||||
if (state->memtupcount - state->current >= ntuples)
|
||||
{
|
||||
state->current += ntuples;
|
||||
return true;
|
||||
}
|
||||
state->current = state->memtupcount;
|
||||
state->eof_reached = true;
|
||||
|
||||
/*
|
||||
* Complain if caller tries to retrieve more tuples than
|
||||
* originally asked for in a bounded sort. This is because
|
||||
* returning EOF here might be the wrong thing.
|
||||
*/
|
||||
if (state->bounded && state->current >= state->bound)
|
||||
elog(ERROR, "retrieved too many tuples in a bounded sort");
|
||||
|
||||
return false;
|
||||
|
||||
case TSS_SORTEDONTAPE:
|
||||
case TSS_FINALMERGE:
|
||||
|
||||
/*
|
||||
* We could probably optimize these cases better, but for now it's
|
||||
* not worth the trouble.
|
||||
*/
|
||||
while (ntuples-- > 0)
|
||||
{
|
||||
SortTuple stup;
|
||||
bool should_free;
|
||||
|
||||
if (!tuplesort_gettuple_common(state, forward,
|
||||
&stup, &should_free))
|
||||
return false;
|
||||
if (should_free)
|
||||
pfree(stup.tuple);
|
||||
CHECK_FOR_INTERRUPTS();
|
||||
}
|
||||
return true;
|
||||
|
||||
default:
|
||||
elog(ERROR, "invalid tuplesort state");
|
||||
return false; /* keep compiler quiet */
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* tuplesort_merge_order - report merge order we'll use for given memory
|
||||
* (note: "merge order" just means the number of input tapes in the merge).
|
||||
|
||||
Reference in New Issue
Block a user