]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeAgg.c
pgindent run for 9.4
[postgresql] / src / backend / executor / nodeAgg.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeAgg.c
4  *        Routines to handle aggregate nodes.
5  *
6  *        ExecAgg evaluates each aggregate in the following steps:
7  *
8  *               transvalue = initcond
9  *               foreach input_tuple do
10  *                      transvalue = transfunc(transvalue, input_value(s))
11  *               result = finalfunc(transvalue, direct_argument(s))
12  *
13  *        If a finalfunc is not supplied then the result is just the ending
14  *        value of transvalue.
15  *
16  *        If a normal aggregate call specifies DISTINCT or ORDER BY, we sort the
17  *        input tuples and eliminate duplicates (if required) before performing
18  *        the above-depicted process.  (However, we don't do that for ordered-set
19  *        aggregates; their "ORDER BY" inputs are ordinary aggregate arguments
20  *        so far as this module is concerned.)
21  *
22  *        If transfunc is marked "strict" in pg_proc and initcond is NULL,
23  *        then the first non-NULL input_value is assigned directly to transvalue,
24  *        and transfunc isn't applied until the second non-NULL input_value.
25  *        The agg's first input type and transtype must be the same in this case!
26  *
27  *        If transfunc is marked "strict" then NULL input_values are skipped,
28  *        keeping the previous transvalue.  If transfunc is not strict then it
29  *        is called for every input tuple and must deal with NULL initcond
30  *        or NULL input_values for itself.
31  *
32  *        If finalfunc is marked "strict" then it is not called when the
33  *        ending transvalue is NULL, instead a NULL result is created
34  *        automatically (this is just the usual handling of strict functions,
35  *        of course).  A non-strict finalfunc can make its own choice of
36  *        what to return for a NULL ending transvalue.
37  *
38  *        Ordered-set aggregates are treated specially in one other way: we
39  *        evaluate any "direct" arguments and pass them to the finalfunc along
40  *        with the transition value.
41  *
42  *        A finalfunc can have additional arguments beyond the transvalue and
43  *        any "direct" arguments, corresponding to the input arguments of the
44  *        aggregate.  These are always just passed as NULL.  Such arguments may be
45  *        needed to allow resolution of a polymorphic aggregate's result type.
46  *
47  *        We compute aggregate input expressions and run the transition functions
48  *        in a temporary econtext (aggstate->tmpcontext).  This is reset at
49  *        least once per input tuple, so when the transvalue datatype is
50  *        pass-by-reference, we have to be careful to copy it into a longer-lived
51  *        memory context, and free the prior value to avoid memory leakage.
52  *        We store transvalues in the memory context aggstate->aggcontext,
53  *        which is also used for the hashtable structures in AGG_HASHED mode.
54  *        The node's regular econtext (aggstate->ss.ps.ps_ExprContext)
55  *        is used to run finalize functions and compute the output tuple;
56  *        this context can be reset once per output tuple.
57  *
58  *        The executor's AggState node is passed as the fmgr "context" value in
59  *        all transfunc and finalfunc calls.  It is not recommended that the
60  *        transition functions look at the AggState node directly, but they can
61  *        use AggCheckCallContext() to verify that they are being called by
62  *        nodeAgg.c (and not as ordinary SQL functions).  The main reason a
63  *        transition function might want to know this is so that it can avoid
64  *        palloc'ing a fixed-size pass-by-ref transition value on every call:
65  *        it can instead just scribble on and return its left input.  Ordinarily
66  *        it is completely forbidden for functions to modify pass-by-ref inputs,
67  *        but in the aggregate case we know the left input is either the initial
68  *        transition value or a previous function result, and in either case its
69  *        value need not be preserved.  See int8inc() for an example.  Notice that
70  *        advance_transition_function() is coded to avoid a data copy step when
71  *        the previous transition value pointer is returned.  Also, some
72  *        transition functions want to store working state in addition to the
73  *        nominal transition value; they can use the memory context returned by
74  *        AggCheckCallContext() to do that.
75  *
76  *        Note: AggCheckCallContext() is available as of PostgreSQL 9.0.  The
77  *        AggState is available as context in earlier releases (back to 8.1),
78  *        but direct examination of the node is needed to use it before 9.0.
79  *
80  *        As of 9.4, aggregate transition functions can also use AggGetAggref()
81  *        to get hold of the Aggref expression node for their aggregate call.
82  *        This is mainly intended for ordered-set aggregates, which are not
83  *        supported as window functions.  (A regular aggregate function would
84  *        need some fallback logic to use this, since there's no Aggref node
85  *        for a window function.)
86  *
87  *
88  * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
89  * Portions Copyright (c) 1994, Regents of the University of California
90  *
91  * IDENTIFICATION
92  *        src/backend/executor/nodeAgg.c
93  *
94  *-------------------------------------------------------------------------
95  */
96
97 #include "postgres.h"
98
99 #include "access/htup_details.h"
100 #include "catalog/objectaccess.h"
101 #include "catalog/pg_aggregate.h"
102 #include "catalog/pg_proc.h"
103 #include "executor/executor.h"
104 #include "executor/nodeAgg.h"
105 #include "miscadmin.h"
106 #include "nodes/nodeFuncs.h"
107 #include "optimizer/clauses.h"
108 #include "optimizer/tlist.h"
109 #include "parser/parse_agg.h"
110 #include "parser/parse_coerce.h"
111 #include "utils/acl.h"
112 #include "utils/builtins.h"
113 #include "utils/lsyscache.h"
114 #include "utils/memutils.h"
115 #include "utils/syscache.h"
116 #include "utils/tuplesort.h"
117 #include "utils/datum.h"
118
119
120 /*
121  * AggStatePerAggData - per-aggregate working state for the Agg scan
122  */
123 typedef struct AggStatePerAggData
124 {
125         /*
126          * These values are set up during ExecInitAgg() and do not change
127          * thereafter:
128          */
129
130         /* Links to Aggref expr and state nodes this working state is for */
131         AggrefExprState *aggrefstate;
132         Aggref     *aggref;
133
134         /*
135          * Nominal number of arguments for aggregate function.  For plain aggs,
136          * this excludes any ORDER BY expressions.  For ordered-set aggs, this
137          * counts both the direct and aggregated (ORDER BY) arguments.
138          */
139         int                     numArguments;
140
141         /*
142          * Number of aggregated input columns.  This includes ORDER BY expressions
143          * in both the plain-agg and ordered-set cases.  Ordered-set direct args
144          * are not counted, though.
145          */
146         int                     numInputs;
147
148         /*
149          * Number of aggregated input columns to pass to the transfn.  This
150          * includes the ORDER BY columns for ordered-set aggs, but not for plain
151          * aggs.  (This doesn't count the transition state value!)
152          */
153         int                     numTransInputs;
154
155         /*
156          * Number of arguments to pass to the finalfn.  This is always at least 1
157          * (the transition state value) plus any ordered-set direct args. If the
158          * finalfn wants extra args then we pass nulls corresponding to the
159          * aggregated input columns.
160          */
161         int                     numFinalArgs;
162
163         /* Oids of transfer functions */
164         Oid                     transfn_oid;
165         Oid                     finalfn_oid;    /* may be InvalidOid */
166
167         /*
168          * fmgr lookup data for transfer functions --- only valid when
169          * corresponding oid is not InvalidOid.  Note in particular that fn_strict
170          * flags are kept here.
171          */
172         FmgrInfo        transfn;
173         FmgrInfo        finalfn;
174
175         /* Input collation derived for aggregate */
176         Oid                     aggCollation;
177
178         /* number of sorting columns */
179         int                     numSortCols;
180
181         /* number of sorting columns to consider in DISTINCT comparisons */
182         /* (this is either zero or the same as numSortCols) */
183         int                     numDistinctCols;
184
185         /* deconstructed sorting information (arrays of length numSortCols) */
186         AttrNumber *sortColIdx;
187         Oid                *sortOperators;
188         Oid                *sortCollations;
189         bool       *sortNullsFirst;
190
191         /*
192          * fmgr lookup data for input columns' equality operators --- only
193          * set/used when aggregate has DISTINCT flag.  Note that these are in
194          * order of sort column index, not parameter index.
195          */
196         FmgrInfo   *equalfns;           /* array of length numDistinctCols */
197
198         /*
199          * initial value from pg_aggregate entry
200          */
201         Datum           initValue;
202         bool            initValueIsNull;
203
204         /*
205          * We need the len and byval info for the agg's input, result, and
206          * transition data types in order to know how to copy/delete values.
207          *
208          * Note that the info for the input type is used only when handling
209          * DISTINCT aggs with just one argument, so there is only one input type.
210          */
211         int16           inputtypeLen,
212                                 resulttypeLen,
213                                 transtypeLen;
214         bool            inputtypeByVal,
215                                 resulttypeByVal,
216                                 transtypeByVal;
217
218         /*
219          * Stuff for evaluation of inputs.  We used to just use ExecEvalExpr, but
220          * with the addition of ORDER BY we now need at least a slot for passing
221          * data to the sort object, which requires a tupledesc, so we might as
222          * well go whole hog and use ExecProject too.
223          */
224         TupleDesc       evaldesc;               /* descriptor of input tuples */
225         ProjectionInfo *evalproj;       /* projection machinery */
226
227         /*
228          * Slots for holding the evaluated input arguments.  These are set up
229          * during ExecInitAgg() and then used for each input row.
230          */
231         TupleTableSlot *evalslot;       /* current input tuple */
232         TupleTableSlot *uniqslot;       /* used for multi-column DISTINCT */
233
234         /*
235          * These values are working state that is initialized at the start of an
236          * input tuple group and updated for each input tuple.
237          *
238          * For a simple (non DISTINCT/ORDER BY) aggregate, we just feed the input
239          * values straight to the transition function.  If it's DISTINCT or
240          * requires ORDER BY, we pass the input values into a Tuplesort object;
241          * then at completion of the input tuple group, we scan the sorted values,
242          * eliminate duplicates if needed, and run the transition function on the
243          * rest.
244          */
245
246         Tuplesortstate *sortstate;      /* sort object, if DISTINCT or ORDER BY */
247
248         /*
249          * This field is a pre-initialized FunctionCallInfo struct used for
250          * calling this aggregate's transfn.  We save a few cycles per row by not
251          * re-initializing the unchanging fields; which isn't much, but it seems
252          * worth the extra space consumption.
253          */
254         FunctionCallInfoData transfn_fcinfo;
255 }       AggStatePerAggData;
256
257 /*
258  * AggStatePerGroupData - per-aggregate-per-group working state
259  *
260  * These values are working state that is initialized at the start of
261  * an input tuple group and updated for each input tuple.
262  *
263  * In AGG_PLAIN and AGG_SORTED modes, we have a single array of these
264  * structs (pointed to by aggstate->pergroup); we re-use the array for
265  * each input group, if it's AGG_SORTED mode.  In AGG_HASHED mode, the
266  * hash table contains an array of these structs for each tuple group.
267  *
268  * Logically, the sortstate field belongs in this struct, but we do not
269  * keep it here for space reasons: we don't support DISTINCT aggregates
270  * in AGG_HASHED mode, so there's no reason to use up a pointer field
271  * in every entry of the hashtable.
272  */
273 typedef struct AggStatePerGroupData
274 {
275         Datum           transValue;             /* current transition value */
276         bool            transValueIsNull;
277
278         bool            noTransValue;   /* true if transValue not set yet */
279
280         /*
281          * Note: noTransValue initially has the same value as transValueIsNull,
282          * and if true both are cleared to false at the same time.  They are not
283          * the same though: if transfn later returns a NULL, we want to keep that
284          * NULL and not auto-replace it with a later input value. Only the first
285          * non-NULL input will be auto-substituted.
286          */
287 } AggStatePerGroupData;
288
289 /*
290  * To implement hashed aggregation, we need a hashtable that stores a
291  * representative tuple and an array of AggStatePerGroup structs for each
292  * distinct set of GROUP BY column values.  We compute the hash key from
293  * the GROUP BY columns.
294  */
295 typedef struct AggHashEntryData *AggHashEntry;
296
297 typedef struct AggHashEntryData
298 {
299         TupleHashEntryData shared;      /* common header for hash table entries */
300         /* per-aggregate transition status array - must be last! */
301         AggStatePerGroupData pergroup[1];       /* VARIABLE LENGTH ARRAY */
302 }       AggHashEntryData;       /* VARIABLE LENGTH STRUCT */
303
304
305 static void initialize_aggregates(AggState *aggstate,
306                                           AggStatePerAgg peragg,
307                                           AggStatePerGroup pergroup);
308 static void advance_transition_function(AggState *aggstate,
309                                                         AggStatePerAgg peraggstate,
310                                                         AggStatePerGroup pergroupstate);
311 static void advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup);
312 static void process_ordered_aggregate_single(AggState *aggstate,
313                                                                  AggStatePerAgg peraggstate,
314                                                                  AggStatePerGroup pergroupstate);
315 static void process_ordered_aggregate_multi(AggState *aggstate,
316                                                                 AggStatePerAgg peraggstate,
317                                                                 AggStatePerGroup pergroupstate);
318 static void finalize_aggregate(AggState *aggstate,
319                                    AggStatePerAgg peraggstate,
320                                    AggStatePerGroup pergroupstate,
321                                    Datum *resultVal, bool *resultIsNull);
322 static Bitmapset *find_unaggregated_cols(AggState *aggstate);
323 static bool find_unaggregated_cols_walker(Node *node, Bitmapset **colnos);
324 static void build_hash_table(AggState *aggstate);
325 static AggHashEntry lookup_hash_entry(AggState *aggstate,
326                                   TupleTableSlot *inputslot);
327 static TupleTableSlot *agg_retrieve_direct(AggState *aggstate);
328 static void agg_fill_hash_table(AggState *aggstate);
329 static TupleTableSlot *agg_retrieve_hash_table(AggState *aggstate);
330 static Datum GetAggInitVal(Datum textInitVal, Oid transtype);
331
332
333 /*
334  * Initialize all aggregates for a new group of input values.
335  *
336  * When called, CurrentMemoryContext should be the per-query context.
337  */
338 static void
339 initialize_aggregates(AggState *aggstate,
340                                           AggStatePerAgg peragg,
341                                           AggStatePerGroup pergroup)
342 {
343         int                     aggno;
344
345         for (aggno = 0; aggno < aggstate->numaggs; aggno++)
346         {
347                 AggStatePerAgg peraggstate = &peragg[aggno];
348                 AggStatePerGroup pergroupstate = &pergroup[aggno];
349
350                 /*
351                  * Start a fresh sort operation for each DISTINCT/ORDER BY aggregate.
352                  */
353                 if (peraggstate->numSortCols > 0)
354                 {
355                         /*
356                          * In case of rescan, maybe there could be an uncompleted sort
357                          * operation?  Clean it up if so.
358                          */
359                         if (peraggstate->sortstate)
360                                 tuplesort_end(peraggstate->sortstate);
361
362                         /*
363                          * We use a plain Datum sorter when there's a single input column;
364                          * otherwise sort the full tuple.  (See comments for
365                          * process_ordered_aggregate_single.)
366                          */
367                         peraggstate->sortstate =
368                                 (peraggstate->numInputs == 1) ?
369                                 tuplesort_begin_datum(peraggstate->evaldesc->attrs[0]->atttypid,
370                                                                           peraggstate->sortOperators[0],
371                                                                           peraggstate->sortCollations[0],
372                                                                           peraggstate->sortNullsFirst[0],
373                                                                           work_mem, false) :
374                                 tuplesort_begin_heap(peraggstate->evaldesc,
375                                                                          peraggstate->numSortCols,
376                                                                          peraggstate->sortColIdx,
377                                                                          peraggstate->sortOperators,
378                                                                          peraggstate->sortCollations,
379                                                                          peraggstate->sortNullsFirst,
380                                                                          work_mem, false);
381                 }
382
383                 /*
384                  * (Re)set transValue to the initial value.
385                  *
386                  * Note that when the initial value is pass-by-ref, we must copy it
387                  * (into the aggcontext) since we will pfree the transValue later.
388                  */
389                 if (peraggstate->initValueIsNull)
390                         pergroupstate->transValue = peraggstate->initValue;
391                 else
392                 {
393                         MemoryContext oldContext;
394
395                         oldContext = MemoryContextSwitchTo(aggstate->aggcontext);
396                         pergroupstate->transValue = datumCopy(peraggstate->initValue,
397                                                                                                   peraggstate->transtypeByVal,
398                                                                                                   peraggstate->transtypeLen);
399                         MemoryContextSwitchTo(oldContext);
400                 }
401                 pergroupstate->transValueIsNull = peraggstate->initValueIsNull;
402
403                 /*
404                  * If the initial value for the transition state doesn't exist in the
405                  * pg_aggregate table then we will let the first non-NULL value
406                  * returned from the outer procNode become the initial value. (This is
407                  * useful for aggregates like max() and min().) The noTransValue flag
408                  * signals that we still need to do this.
409                  */
410                 pergroupstate->noTransValue = peraggstate->initValueIsNull;
411         }
412 }
413
414 /*
415  * Given new input value(s), advance the transition function of an aggregate.
416  *
417  * The new values (and null flags) have been preloaded into argument positions
418  * 1 and up in peraggstate->transfn_fcinfo, so that we needn't copy them again
419  * to pass to the transition function.  We also expect that the static fields
420  * of the fcinfo are already initialized; that was done by ExecInitAgg().
421  *
422  * It doesn't matter which memory context this is called in.
423  */
424 static void
425 advance_transition_function(AggState *aggstate,
426                                                         AggStatePerAgg peraggstate,
427                                                         AggStatePerGroup pergroupstate)
428 {
429         FunctionCallInfo fcinfo = &peraggstate->transfn_fcinfo;
430         MemoryContext oldContext;
431         Datum           newVal;
432
433         if (peraggstate->transfn.fn_strict)
434         {
435                 /*
436                  * For a strict transfn, nothing happens when there's a NULL input; we
437                  * just keep the prior transValue.
438                  */
439                 int                     numTransInputs = peraggstate->numTransInputs;
440                 int                     i;
441
442                 for (i = 1; i <= numTransInputs; i++)
443                 {
444                         if (fcinfo->argnull[i])
445                                 return;
446                 }
447                 if (pergroupstate->noTransValue)
448                 {
449                         /*
450                          * transValue has not been initialized. This is the first non-NULL
451                          * input value. We use it as the initial value for transValue. (We
452                          * already checked that the agg's input type is binary-compatible
453                          * with its transtype, so straight copy here is OK.)
454                          *
455                          * We must copy the datum into aggcontext if it is pass-by-ref. We
456                          * do not need to pfree the old transValue, since it's NULL.
457                          */
458                         oldContext = MemoryContextSwitchTo(aggstate->aggcontext);
459                         pergroupstate->transValue = datumCopy(fcinfo->arg[1],
460                                                                                                   peraggstate->transtypeByVal,
461                                                                                                   peraggstate->transtypeLen);
462                         pergroupstate->transValueIsNull = false;
463                         pergroupstate->noTransValue = false;
464                         MemoryContextSwitchTo(oldContext);
465                         return;
466                 }
467                 if (pergroupstate->transValueIsNull)
468                 {
469                         /*
470                          * Don't call a strict function with NULL inputs.  Note it is
471                          * possible to get here despite the above tests, if the transfn is
472                          * strict *and* returned a NULL on a prior cycle. If that happens
473                          * we will propagate the NULL all the way to the end.
474                          */
475                         return;
476                 }
477         }
478
479         /* We run the transition functions in per-input-tuple memory context */
480         oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
481
482         /* set up aggstate->curperagg for AggGetAggref() */
483         aggstate->curperagg = peraggstate;
484
485         /*
486          * OK to call the transition function
487          */
488         fcinfo->arg[0] = pergroupstate->transValue;
489         fcinfo->argnull[0] = pergroupstate->transValueIsNull;
490         fcinfo->isnull = false;         /* just in case transfn doesn't set it */
491
492         newVal = FunctionCallInvoke(fcinfo);
493
494         aggstate->curperagg = NULL;
495
496         /*
497          * If pass-by-ref datatype, must copy the new value into aggcontext and
498          * pfree the prior transValue.  But if transfn returned a pointer to its
499          * first input, we don't need to do anything.
500          */
501         if (!peraggstate->transtypeByVal &&
502                 DatumGetPointer(newVal) != DatumGetPointer(pergroupstate->transValue))
503         {
504                 if (!fcinfo->isnull)
505                 {
506                         MemoryContextSwitchTo(aggstate->aggcontext);
507                         newVal = datumCopy(newVal,
508                                                            peraggstate->transtypeByVal,
509                                                            peraggstate->transtypeLen);
510                 }
511                 if (!pergroupstate->transValueIsNull)
512                         pfree(DatumGetPointer(pergroupstate->transValue));
513         }
514
515         pergroupstate->transValue = newVal;
516         pergroupstate->transValueIsNull = fcinfo->isnull;
517
518         MemoryContextSwitchTo(oldContext);
519 }
520
521 /*
522  * Advance all the aggregates for one input tuple.  The input tuple
523  * has been stored in tmpcontext->ecxt_outertuple, so that it is accessible
524  * to ExecEvalExpr.  pergroup is the array of per-group structs to use
525  * (this might be in a hashtable entry).
526  *
527  * When called, CurrentMemoryContext should be the per-query context.
528  */
529 static void
530 advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup)
531 {
532         int                     aggno;
533
534         for (aggno = 0; aggno < aggstate->numaggs; aggno++)
535         {
536                 AggStatePerAgg peraggstate = &aggstate->peragg[aggno];
537                 AggStatePerGroup pergroupstate = &pergroup[aggno];
538                 ExprState  *filter = peraggstate->aggrefstate->aggfilter;
539                 int                     numTransInputs = peraggstate->numTransInputs;
540                 int                     i;
541                 TupleTableSlot *slot;
542
543                 /* Skip anything FILTERed out */
544                 if (filter)
545                 {
546                         Datum           res;
547                         bool            isnull;
548
549                         res = ExecEvalExprSwitchContext(filter, aggstate->tmpcontext,
550                                                                                         &isnull, NULL);
551                         if (isnull || !DatumGetBool(res))
552                                 continue;
553                 }
554
555                 /* Evaluate the current input expressions for this aggregate */
556                 slot = ExecProject(peraggstate->evalproj, NULL);
557
558                 if (peraggstate->numSortCols > 0)
559                 {
560                         /* DISTINCT and/or ORDER BY case */
561                         Assert(slot->tts_nvalid == peraggstate->numInputs);
562
563                         /*
564                          * If the transfn is strict, we want to check for nullity before
565                          * storing the row in the sorter, to save space if there are a lot
566                          * of nulls.  Note that we must only check numTransInputs columns,
567                          * not numInputs, since nullity in columns used only for sorting
568                          * is not relevant here.
569                          */
570                         if (peraggstate->transfn.fn_strict)
571                         {
572                                 for (i = 0; i < numTransInputs; i++)
573                                 {
574                                         if (slot->tts_isnull[i])
575                                                 break;
576                                 }
577                                 if (i < numTransInputs)
578                                         continue;
579                         }
580
581                         /* OK, put the tuple into the tuplesort object */
582                         if (peraggstate->numInputs == 1)
583                                 tuplesort_putdatum(peraggstate->sortstate,
584                                                                    slot->tts_values[0],
585                                                                    slot->tts_isnull[0]);
586                         else
587                                 tuplesort_puttupleslot(peraggstate->sortstate, slot);
588                 }
589                 else
590                 {
591                         /* We can apply the transition function immediately */
592                         FunctionCallInfo fcinfo = &peraggstate->transfn_fcinfo;
593
594                         /* Load values into fcinfo */
595                         /* Start from 1, since the 0th arg will be the transition value */
596                         Assert(slot->tts_nvalid >= numTransInputs);
597                         for (i = 0; i < numTransInputs; i++)
598                         {
599                                 fcinfo->arg[i + 1] = slot->tts_values[i];
600                                 fcinfo->argnull[i + 1] = slot->tts_isnull[i];
601                         }
602
603                         advance_transition_function(aggstate, peraggstate, pergroupstate);
604                 }
605         }
606 }
607
608
609 /*
610  * Run the transition function for a DISTINCT or ORDER BY aggregate
611  * with only one input.  This is called after we have completed
612  * entering all the input values into the sort object.  We complete the
613  * sort, read out the values in sorted order, and run the transition
614  * function on each value (applying DISTINCT if appropriate).
615  *
616  * Note that the strictness of the transition function was checked when
617  * entering the values into the sort, so we don't check it again here;
618  * we just apply standard SQL DISTINCT logic.
619  *
620  * The one-input case is handled separately from the multi-input case
621  * for performance reasons: for single by-value inputs, such as the
622  * common case of count(distinct id), the tuplesort_getdatum code path
623  * is around 300% faster.  (The speedup for by-reference types is less
624  * but still noticeable.)
625  *
626  * When called, CurrentMemoryContext should be the per-query context.
627  */
628 static void
629 process_ordered_aggregate_single(AggState *aggstate,
630                                                                  AggStatePerAgg peraggstate,
631                                                                  AggStatePerGroup pergroupstate)
632 {
633         Datum           oldVal = (Datum) 0;
634         bool            oldIsNull = true;
635         bool            haveOldVal = false;
636         MemoryContext workcontext = aggstate->tmpcontext->ecxt_per_tuple_memory;
637         MemoryContext oldContext;
638         bool            isDistinct = (peraggstate->numDistinctCols > 0);
639         FunctionCallInfo fcinfo = &peraggstate->transfn_fcinfo;
640         Datum      *newVal;
641         bool       *isNull;
642
643         Assert(peraggstate->numDistinctCols < 2);
644
645         tuplesort_performsort(peraggstate->sortstate);
646
647         /* Load the column into argument 1 (arg 0 will be transition value) */
648         newVal = fcinfo->arg + 1;
649         isNull = fcinfo->argnull + 1;
650
651         /*
652          * Note: if input type is pass-by-ref, the datums returned by the sort are
653          * freshly palloc'd in the per-query context, so we must be careful to
654          * pfree them when they are no longer needed.
655          */
656
657         while (tuplesort_getdatum(peraggstate->sortstate, true,
658                                                           newVal, isNull))
659         {
660                 /*
661                  * Clear and select the working context for evaluation of the equality
662                  * function and transition function.
663                  */
664                 MemoryContextReset(workcontext);
665                 oldContext = MemoryContextSwitchTo(workcontext);
666
667                 /*
668                  * If DISTINCT mode, and not distinct from prior, skip it.
669                  *
670                  * Note: we assume equality functions don't care about collation.
671                  */
672                 if (isDistinct &&
673                         haveOldVal &&
674                         ((oldIsNull && *isNull) ||
675                          (!oldIsNull && !*isNull &&
676                           DatumGetBool(FunctionCall2(&peraggstate->equalfns[0],
677                                                                                  oldVal, *newVal)))))
678                 {
679                         /* equal to prior, so forget this one */
680                         if (!peraggstate->inputtypeByVal && !*isNull)
681                                 pfree(DatumGetPointer(*newVal));
682                 }
683                 else
684                 {
685                         advance_transition_function(aggstate, peraggstate, pergroupstate);
686                         /* forget the old value, if any */
687                         if (!oldIsNull && !peraggstate->inputtypeByVal)
688                                 pfree(DatumGetPointer(oldVal));
689                         /* and remember the new one for subsequent equality checks */
690                         oldVal = *newVal;
691                         oldIsNull = *isNull;
692                         haveOldVal = true;
693                 }
694
695                 MemoryContextSwitchTo(oldContext);
696         }
697
698         if (!oldIsNull && !peraggstate->inputtypeByVal)
699                 pfree(DatumGetPointer(oldVal));
700
701         tuplesort_end(peraggstate->sortstate);
702         peraggstate->sortstate = NULL;
703 }
704
705 /*
706  * Run the transition function for a DISTINCT or ORDER BY aggregate
707  * with more than one input.  This is called after we have completed
708  * entering all the input values into the sort object.  We complete the
709  * sort, read out the values in sorted order, and run the transition
710  * function on each value (applying DISTINCT if appropriate).
711  *
712  * When called, CurrentMemoryContext should be the per-query context.
713  */
714 static void
715 process_ordered_aggregate_multi(AggState *aggstate,
716                                                                 AggStatePerAgg peraggstate,
717                                                                 AggStatePerGroup pergroupstate)
718 {
719         MemoryContext workcontext = aggstate->tmpcontext->ecxt_per_tuple_memory;
720         FunctionCallInfo fcinfo = &peraggstate->transfn_fcinfo;
721         TupleTableSlot *slot1 = peraggstate->evalslot;
722         TupleTableSlot *slot2 = peraggstate->uniqslot;
723         int                     numTransInputs = peraggstate->numTransInputs;
724         int                     numDistinctCols = peraggstate->numDistinctCols;
725         bool            haveOldValue = false;
726         int                     i;
727
728         tuplesort_performsort(peraggstate->sortstate);
729
730         ExecClearTuple(slot1);
731         if (slot2)
732                 ExecClearTuple(slot2);
733
734         while (tuplesort_gettupleslot(peraggstate->sortstate, true, slot1))
735         {
736                 /*
737                  * Extract the first numTransInputs columns as datums to pass to the
738                  * transfn.  (This will help execTuplesMatch too, so we do it
739                  * immediately.)
740                  */
741                 slot_getsomeattrs(slot1, numTransInputs);
742
743                 if (numDistinctCols == 0 ||
744                         !haveOldValue ||
745                         !execTuplesMatch(slot1, slot2,
746                                                          numDistinctCols,
747                                                          peraggstate->sortColIdx,
748                                                          peraggstate->equalfns,
749                                                          workcontext))
750                 {
751                         /* Load values into fcinfo */
752                         /* Start from 1, since the 0th arg will be the transition value */
753                         for (i = 0; i < numTransInputs; i++)
754                         {
755                                 fcinfo->arg[i + 1] = slot1->tts_values[i];
756                                 fcinfo->argnull[i + 1] = slot1->tts_isnull[i];
757                         }
758
759                         advance_transition_function(aggstate, peraggstate, pergroupstate);
760
761                         if (numDistinctCols > 0)
762                         {
763                                 /* swap the slot pointers to retain the current tuple */
764                                 TupleTableSlot *tmpslot = slot2;
765
766                                 slot2 = slot1;
767                                 slot1 = tmpslot;
768                                 haveOldValue = true;
769                         }
770                 }
771
772                 /* Reset context each time, unless execTuplesMatch did it for us */
773                 if (numDistinctCols == 0)
774                         MemoryContextReset(workcontext);
775
776                 ExecClearTuple(slot1);
777         }
778
779         if (slot2)
780                 ExecClearTuple(slot2);
781
782         tuplesort_end(peraggstate->sortstate);
783         peraggstate->sortstate = NULL;
784 }
785
786 /*
787  * Compute the final value of one aggregate.
788  *
789  * The finalfunction will be run, and the result delivered, in the
790  * output-tuple context; caller's CurrentMemoryContext does not matter.
791  */
792 static void
793 finalize_aggregate(AggState *aggstate,
794                                    AggStatePerAgg peraggstate,
795                                    AggStatePerGroup pergroupstate,
796                                    Datum *resultVal, bool *resultIsNull)
797 {
798         FunctionCallInfoData fcinfo;
799         bool            anynull = false;
800         MemoryContext oldContext;
801         int                     i;
802         ListCell   *lc;
803
804         oldContext = MemoryContextSwitchTo(aggstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
805
806         /*
807          * Evaluate any direct arguments.  We do this even if there's no finalfn
808          * (which is unlikely anyway), so that side-effects happen as expected.
809          * The direct arguments go into arg positions 1 and up, leaving position 0
810          * for the transition state value.
811          */
812         i = 1;
813         foreach(lc, peraggstate->aggrefstate->aggdirectargs)
814         {
815                 ExprState  *expr = (ExprState *) lfirst(lc);
816
817                 fcinfo.arg[i] = ExecEvalExpr(expr,
818                                                                          aggstate->ss.ps.ps_ExprContext,
819                                                                          &fcinfo.argnull[i],
820                                                                          NULL);
821                 anynull |= fcinfo.argnull[i];
822                 i++;
823         }
824
825         /*
826          * Apply the agg's finalfn if one is provided, else return transValue.
827          */
828         if (OidIsValid(peraggstate->finalfn_oid))
829         {
830                 int                     numFinalArgs = peraggstate->numFinalArgs;
831
832                 /* set up aggstate->curperagg for AggGetAggref() */
833                 aggstate->curperagg = peraggstate;
834
835                 InitFunctionCallInfoData(fcinfo, &(peraggstate->finalfn),
836                                                                  numFinalArgs,
837                                                                  peraggstate->aggCollation,
838                                                                  (void *) aggstate, NULL);
839
840                 /* Fill in the transition state value */
841                 fcinfo.arg[0] = pergroupstate->transValue;
842                 fcinfo.argnull[0] = pergroupstate->transValueIsNull;
843                 anynull |= pergroupstate->transValueIsNull;
844
845                 /* Fill any remaining argument positions with nulls */
846                 for (; i < numFinalArgs; i++)
847                 {
848                         fcinfo.arg[i] = (Datum) 0;
849                         fcinfo.argnull[i] = true;
850                         anynull = true;
851                 }
852
853                 if (fcinfo.flinfo->fn_strict && anynull)
854                 {
855                         /* don't call a strict function with NULL inputs */
856                         *resultVal = (Datum) 0;
857                         *resultIsNull = true;
858                 }
859                 else
860                 {
861                         *resultVal = FunctionCallInvoke(&fcinfo);
862                         *resultIsNull = fcinfo.isnull;
863                 }
864                 aggstate->curperagg = NULL;
865         }
866         else
867         {
868                 *resultVal = pergroupstate->transValue;
869                 *resultIsNull = pergroupstate->transValueIsNull;
870         }
871
872         /*
873          * If result is pass-by-ref, make sure it is in the right context.
874          */
875         if (!peraggstate->resulttypeByVal && !*resultIsNull &&
876                 !MemoryContextContains(CurrentMemoryContext,
877                                                            DatumGetPointer(*resultVal)))
878                 *resultVal = datumCopy(*resultVal,
879                                                            peraggstate->resulttypeByVal,
880                                                            peraggstate->resulttypeLen);
881
882         MemoryContextSwitchTo(oldContext);
883 }
884
885 /*
886  * find_unaggregated_cols
887  *        Construct a bitmapset of the column numbers of un-aggregated Vars
888  *        appearing in our targetlist and qual (HAVING clause)
889  */
890 static Bitmapset *
891 find_unaggregated_cols(AggState *aggstate)
892 {
893         Agg                *node = (Agg *) aggstate->ss.ps.plan;
894         Bitmapset  *colnos;
895
896         colnos = NULL;
897         (void) find_unaggregated_cols_walker((Node *) node->plan.targetlist,
898                                                                                  &colnos);
899         (void) find_unaggregated_cols_walker((Node *) node->plan.qual,
900                                                                                  &colnos);
901         return colnos;
902 }
903
904 static bool
905 find_unaggregated_cols_walker(Node *node, Bitmapset **colnos)
906 {
907         if (node == NULL)
908                 return false;
909         if (IsA(node, Var))
910         {
911                 Var                *var = (Var *) node;
912
913                 /* setrefs.c should have set the varno to OUTER_VAR */
914                 Assert(var->varno == OUTER_VAR);
915                 Assert(var->varlevelsup == 0);
916                 *colnos = bms_add_member(*colnos, var->varattno);
917                 return false;
918         }
919         if (IsA(node, Aggref))          /* do not descend into aggregate exprs */
920                 return false;
921         return expression_tree_walker(node, find_unaggregated_cols_walker,
922                                                                   (void *) colnos);
923 }
924
925 /*
926  * Initialize the hash table to empty.
927  *
928  * The hash table always lives in the aggcontext memory context.
929  */
930 static void
931 build_hash_table(AggState *aggstate)
932 {
933         Agg                *node = (Agg *) aggstate->ss.ps.plan;
934         MemoryContext tmpmem = aggstate->tmpcontext->ecxt_per_tuple_memory;
935         Size            entrysize;
936
937         Assert(node->aggstrategy == AGG_HASHED);
938         Assert(node->numGroups > 0);
939
940         entrysize = sizeof(AggHashEntryData) +
941                 (aggstate->numaggs - 1) * sizeof(AggStatePerGroupData);
942
943         aggstate->hashtable = BuildTupleHashTable(node->numCols,
944                                                                                           node->grpColIdx,
945                                                                                           aggstate->eqfunctions,
946                                                                                           aggstate->hashfunctions,
947                                                                                           node->numGroups,
948                                                                                           entrysize,
949                                                                                           aggstate->aggcontext,
950                                                                                           tmpmem);
951 }
952
953 /*
954  * Create a list of the tuple columns that actually need to be stored in
955  * hashtable entries.  The incoming tuples from the child plan node will
956  * contain grouping columns, other columns referenced in our targetlist and
957  * qual, columns used to compute the aggregate functions, and perhaps just
958  * junk columns we don't use at all.  Only columns of the first two types
959  * need to be stored in the hashtable, and getting rid of the others can
960  * make the table entries significantly smaller.  To avoid messing up Var
961  * numbering, we keep the same tuple descriptor for hashtable entries as the
962  * incoming tuples have, but set unwanted columns to NULL in the tuples that
963  * go into the table.
964  *
965  * To eliminate duplicates, we build a bitmapset of the needed columns, then
966  * convert it to an integer list (cheaper to scan at runtime). The list is
967  * in decreasing order so that the first entry is the largest;
968  * lookup_hash_entry depends on this to use slot_getsomeattrs correctly.
969  * Note that the list is preserved over ExecReScanAgg, so we allocate it in
970  * the per-query context (unlike the hash table itself).
971  *
972  * Note: at present, searching the tlist/qual is not really necessary since
973  * the parser should disallow any unaggregated references to ungrouped
974  * columns.  However, the search will be needed when we add support for
975  * SQL99 semantics that allow use of "functionally dependent" columns that
976  * haven't been explicitly grouped by.
977  */
978 static List *
979 find_hash_columns(AggState *aggstate)
980 {
981         Agg                *node = (Agg *) aggstate->ss.ps.plan;
982         Bitmapset  *colnos;
983         List       *collist;
984         int                     i;
985
986         /* Find Vars that will be needed in tlist and qual */
987         colnos = find_unaggregated_cols(aggstate);
988         /* Add in all the grouping columns */
989         for (i = 0; i < node->numCols; i++)
990                 colnos = bms_add_member(colnos, node->grpColIdx[i]);
991         /* Convert to list, using lcons so largest element ends up first */
992         collist = NIL;
993         while ((i = bms_first_member(colnos)) >= 0)
994                 collist = lcons_int(i, collist);
995         bms_free(colnos);
996
997         return collist;
998 }
999
1000 /*
1001  * Estimate per-hash-table-entry overhead for the planner.
1002  *
1003  * Note that the estimate does not include space for pass-by-reference
1004  * transition data values, nor for the representative tuple of each group.
1005  */
1006 Size
1007 hash_agg_entry_size(int numAggs)
1008 {
1009         Size            entrysize;
1010
1011         /* This must match build_hash_table */
1012         entrysize = sizeof(AggHashEntryData) +
1013                 (numAggs - 1) * sizeof(AggStatePerGroupData);
1014         entrysize = MAXALIGN(entrysize);
1015         /* Account for hashtable overhead (assuming fill factor = 1) */
1016         entrysize += 3 * sizeof(void *);
1017         return entrysize;
1018 }
1019
1020 /*
1021  * Find or create a hashtable entry for the tuple group containing the
1022  * given tuple.
1023  *
1024  * When called, CurrentMemoryContext should be the per-query context.
1025  */
1026 static AggHashEntry
1027 lookup_hash_entry(AggState *aggstate, TupleTableSlot *inputslot)
1028 {
1029         TupleTableSlot *hashslot = aggstate->hashslot;
1030         ListCell   *l;
1031         AggHashEntry entry;
1032         bool            isnew;
1033
1034         /* if first time through, initialize hashslot by cloning input slot */
1035         if (hashslot->tts_tupleDescriptor == NULL)
1036         {
1037                 ExecSetSlotDescriptor(hashslot, inputslot->tts_tupleDescriptor);
1038                 /* Make sure all unused columns are NULLs */
1039                 ExecStoreAllNullTuple(hashslot);
1040         }
1041
1042         /* transfer just the needed columns into hashslot */
1043         slot_getsomeattrs(inputslot, linitial_int(aggstate->hash_needed));
1044         foreach(l, aggstate->hash_needed)
1045         {
1046                 int                     varNumber = lfirst_int(l) - 1;
1047
1048                 hashslot->tts_values[varNumber] = inputslot->tts_values[varNumber];
1049                 hashslot->tts_isnull[varNumber] = inputslot->tts_isnull[varNumber];
1050         }
1051
1052         /* find or create the hashtable entry using the filtered tuple */
1053         entry = (AggHashEntry) LookupTupleHashEntry(aggstate->hashtable,
1054                                                                                                 hashslot,
1055                                                                                                 &isnew);
1056
1057         if (isnew)
1058         {
1059                 /* initialize aggregates for new tuple group */
1060                 initialize_aggregates(aggstate, aggstate->peragg, entry->pergroup);
1061         }
1062
1063         return entry;
1064 }
1065
1066 /*
1067  * ExecAgg -
1068  *
1069  *        ExecAgg receives tuples from its outer subplan and aggregates over
1070  *        the appropriate attribute for each aggregate function use (Aggref
1071  *        node) appearing in the targetlist or qual of the node.  The number
1072  *        of tuples to aggregate over depends on whether grouped or plain
1073  *        aggregation is selected.  In grouped aggregation, we produce a result
1074  *        row for each group; in plain aggregation there's a single result row
1075  *        for the whole query.  In either case, the value of each aggregate is
1076  *        stored in the expression context to be used when ExecProject evaluates
1077  *        the result tuple.
1078  */
1079 TupleTableSlot *
1080 ExecAgg(AggState *node)
1081 {
1082         /*
1083          * Check to see if we're still projecting out tuples from a previous agg
1084          * tuple (because there is a function-returning-set in the projection
1085          * expressions).  If so, try to project another one.
1086          */
1087         if (node->ss.ps.ps_TupFromTlist)
1088         {
1089                 TupleTableSlot *result;
1090                 ExprDoneCond isDone;
1091
1092                 result = ExecProject(node->ss.ps.ps_ProjInfo, &isDone);
1093                 if (isDone == ExprMultipleResult)
1094                         return result;
1095                 /* Done with that source tuple... */
1096                 node->ss.ps.ps_TupFromTlist = false;
1097         }
1098
1099         /*
1100          * Exit if nothing left to do.  (We must do the ps_TupFromTlist check
1101          * first, because in some cases agg_done gets set before we emit the final
1102          * aggregate tuple, and we have to finish running SRFs for it.)
1103          */
1104         if (node->agg_done)
1105                 return NULL;
1106
1107         /* Dispatch based on strategy */
1108         if (((Agg *) node->ss.ps.plan)->aggstrategy == AGG_HASHED)
1109         {
1110                 if (!node->table_filled)
1111                         agg_fill_hash_table(node);
1112                 return agg_retrieve_hash_table(node);
1113         }
1114         else
1115                 return agg_retrieve_direct(node);
1116 }
1117
1118 /*
1119  * ExecAgg for non-hashed case
1120  */
1121 static TupleTableSlot *
1122 agg_retrieve_direct(AggState *aggstate)
1123 {
1124         Agg                *node = (Agg *) aggstate->ss.ps.plan;
1125         PlanState  *outerPlan;
1126         ExprContext *econtext;
1127         ExprContext *tmpcontext;
1128         Datum      *aggvalues;
1129         bool       *aggnulls;
1130         AggStatePerAgg peragg;
1131         AggStatePerGroup pergroup;
1132         TupleTableSlot *outerslot;
1133         TupleTableSlot *firstSlot;
1134         int                     aggno;
1135
1136         /*
1137          * get state info from node
1138          */
1139         outerPlan = outerPlanState(aggstate);
1140         /* econtext is the per-output-tuple expression context */
1141         econtext = aggstate->ss.ps.ps_ExprContext;
1142         aggvalues = econtext->ecxt_aggvalues;
1143         aggnulls = econtext->ecxt_aggnulls;
1144         /* tmpcontext is the per-input-tuple expression context */
1145         tmpcontext = aggstate->tmpcontext;
1146         peragg = aggstate->peragg;
1147         pergroup = aggstate->pergroup;
1148         firstSlot = aggstate->ss.ss_ScanTupleSlot;
1149
1150         /*
1151          * We loop retrieving groups until we find one matching
1152          * aggstate->ss.ps.qual
1153          */
1154         while (!aggstate->agg_done)
1155         {
1156                 /*
1157                  * If we don't already have the first tuple of the new group, fetch it
1158                  * from the outer plan.
1159                  */
1160                 if (aggstate->grp_firstTuple == NULL)
1161                 {
1162                         outerslot = ExecProcNode(outerPlan);
1163                         if (!TupIsNull(outerslot))
1164                         {
1165                                 /*
1166                                  * Make a copy of the first input tuple; we will use this for
1167                                  * comparisons (in group mode) and for projection.
1168                                  */
1169                                 aggstate->grp_firstTuple = ExecCopySlotTuple(outerslot);
1170                         }
1171                         else
1172                         {
1173                                 /* outer plan produced no tuples at all */
1174                                 aggstate->agg_done = true;
1175                                 /* If we are grouping, we should produce no tuples too */
1176                                 if (node->aggstrategy != AGG_PLAIN)
1177                                         return NULL;
1178                         }
1179                 }
1180
1181                 /*
1182                  * Clear the per-output-tuple context for each group, as well as
1183                  * aggcontext (which contains any pass-by-ref transvalues of the old
1184                  * group).  We also clear any child contexts of the aggcontext; some
1185                  * aggregate functions store working state in such contexts.
1186                  *
1187                  * We use ReScanExprContext not just ResetExprContext because we want
1188                  * any registered shutdown callbacks to be called.  That allows
1189                  * aggregate functions to ensure they've cleaned up any non-memory
1190                  * resources.
1191                  */
1192                 ReScanExprContext(econtext);
1193
1194                 MemoryContextResetAndDeleteChildren(aggstate->aggcontext);
1195
1196                 /*
1197                  * Initialize working state for a new input tuple group
1198                  */
1199                 initialize_aggregates(aggstate, peragg, pergroup);
1200
1201                 if (aggstate->grp_firstTuple != NULL)
1202                 {
1203                         /*
1204                          * Store the copied first input tuple in the tuple table slot
1205                          * reserved for it.  The tuple will be deleted when it is cleared
1206                          * from the slot.
1207                          */
1208                         ExecStoreTuple(aggstate->grp_firstTuple,
1209                                                    firstSlot,
1210                                                    InvalidBuffer,
1211                                                    true);
1212                         aggstate->grp_firstTuple = NULL;        /* don't keep two pointers */
1213
1214                         /* set up for first advance_aggregates call */
1215                         tmpcontext->ecxt_outertuple = firstSlot;
1216
1217                         /*
1218                          * Process each outer-plan tuple, and then fetch the next one,
1219                          * until we exhaust the outer plan or cross a group boundary.
1220                          */
1221                         for (;;)
1222                         {
1223                                 advance_aggregates(aggstate, pergroup);
1224
1225                                 /* Reset per-input-tuple context after each tuple */
1226                                 ResetExprContext(tmpcontext);
1227
1228                                 outerslot = ExecProcNode(outerPlan);
1229                                 if (TupIsNull(outerslot))
1230                                 {
1231                                         /* no more outer-plan tuples available */
1232                                         aggstate->agg_done = true;
1233                                         break;
1234                                 }
1235                                 /* set up for next advance_aggregates call */
1236                                 tmpcontext->ecxt_outertuple = outerslot;
1237
1238                                 /*
1239                                  * If we are grouping, check whether we've crossed a group
1240                                  * boundary.
1241                                  */
1242                                 if (node->aggstrategy == AGG_SORTED)
1243                                 {
1244                                         if (!execTuplesMatch(firstSlot,
1245                                                                                  outerslot,
1246                                                                                  node->numCols, node->grpColIdx,
1247                                                                                  aggstate->eqfunctions,
1248                                                                                  tmpcontext->ecxt_per_tuple_memory))
1249                                         {
1250                                                 /*
1251                                                  * Save the first input tuple of the next group.
1252                                                  */
1253                                                 aggstate->grp_firstTuple = ExecCopySlotTuple(outerslot);
1254                                                 break;
1255                                         }
1256                                 }
1257                         }
1258                 }
1259
1260                 /*
1261                  * Use the representative input tuple for any references to
1262                  * non-aggregated input columns in aggregate direct args, the node
1263                  * qual, and the tlist.  (If we are not grouping, and there are no
1264                  * input rows at all, we will come here with an empty firstSlot ...
1265                  * but if not grouping, there can't be any references to
1266                  * non-aggregated input columns, so no problem.)
1267                  */
1268                 econtext->ecxt_outertuple = firstSlot;
1269
1270                 /*
1271                  * Done scanning input tuple group. Finalize each aggregate
1272                  * calculation, and stash results in the per-output-tuple context.
1273                  */
1274                 for (aggno = 0; aggno < aggstate->numaggs; aggno++)
1275                 {
1276                         AggStatePerAgg peraggstate = &peragg[aggno];
1277                         AggStatePerGroup pergroupstate = &pergroup[aggno];
1278
1279                         if (peraggstate->numSortCols > 0)
1280                         {
1281                                 if (peraggstate->numInputs == 1)
1282                                         process_ordered_aggregate_single(aggstate,
1283                                                                                                          peraggstate,
1284                                                                                                          pergroupstate);
1285                                 else
1286                                         process_ordered_aggregate_multi(aggstate,
1287                                                                                                         peraggstate,
1288                                                                                                         pergroupstate);
1289                         }
1290
1291                         finalize_aggregate(aggstate, peraggstate, pergroupstate,
1292                                                            &aggvalues[aggno], &aggnulls[aggno]);
1293                 }
1294
1295                 /*
1296                  * Check the qual (HAVING clause); if the group does not match, ignore
1297                  * it and loop back to try to process another group.
1298                  */
1299                 if (ExecQual(aggstate->ss.ps.qual, econtext, false))
1300                 {
1301                         /*
1302                          * Form and return a projection tuple using the aggregate results
1303                          * and the representative input tuple.
1304                          */
1305                         TupleTableSlot *result;
1306                         ExprDoneCond isDone;
1307
1308                         result = ExecProject(aggstate->ss.ps.ps_ProjInfo, &isDone);
1309
1310                         if (isDone != ExprEndResult)
1311                         {
1312                                 aggstate->ss.ps.ps_TupFromTlist =
1313                                         (isDone == ExprMultipleResult);
1314                                 return result;
1315                         }
1316                 }
1317                 else
1318                         InstrCountFiltered1(aggstate, 1);
1319         }
1320
1321         /* No more groups */
1322         return NULL;
1323 }
1324
1325 /*
1326  * ExecAgg for hashed case: phase 1, read input and build hash table
1327  */
1328 static void
1329 agg_fill_hash_table(AggState *aggstate)
1330 {
1331         PlanState  *outerPlan;
1332         ExprContext *tmpcontext;
1333         AggHashEntry entry;
1334         TupleTableSlot *outerslot;
1335
1336         /*
1337          * get state info from node
1338          */
1339         outerPlan = outerPlanState(aggstate);
1340         /* tmpcontext is the per-input-tuple expression context */
1341         tmpcontext = aggstate->tmpcontext;
1342
1343         /*
1344          * Process each outer-plan tuple, and then fetch the next one, until we
1345          * exhaust the outer plan.
1346          */
1347         for (;;)
1348         {
1349                 outerslot = ExecProcNode(outerPlan);
1350                 if (TupIsNull(outerslot))
1351                         break;
1352                 /* set up for advance_aggregates call */
1353                 tmpcontext->ecxt_outertuple = outerslot;
1354
1355                 /* Find or build hashtable entry for this tuple's group */
1356                 entry = lookup_hash_entry(aggstate, outerslot);
1357
1358                 /* Advance the aggregates */
1359                 advance_aggregates(aggstate, entry->pergroup);
1360
1361                 /* Reset per-input-tuple context after each tuple */
1362                 ResetExprContext(tmpcontext);
1363         }
1364
1365         aggstate->table_filled = true;
1366         /* Initialize to walk the hash table */
1367         ResetTupleHashIterator(aggstate->hashtable, &aggstate->hashiter);
1368 }
1369
1370 /*
1371  * ExecAgg for hashed case: phase 2, retrieving groups from hash table
1372  */
1373 static TupleTableSlot *
1374 agg_retrieve_hash_table(AggState *aggstate)
1375 {
1376         ExprContext *econtext;
1377         Datum      *aggvalues;
1378         bool       *aggnulls;
1379         AggStatePerAgg peragg;
1380         AggStatePerGroup pergroup;
1381         AggHashEntry entry;
1382         TupleTableSlot *firstSlot;
1383         int                     aggno;
1384
1385         /*
1386          * get state info from node
1387          */
1388         /* econtext is the per-output-tuple expression context */
1389         econtext = aggstate->ss.ps.ps_ExprContext;
1390         aggvalues = econtext->ecxt_aggvalues;
1391         aggnulls = econtext->ecxt_aggnulls;
1392         peragg = aggstate->peragg;
1393         firstSlot = aggstate->ss.ss_ScanTupleSlot;
1394
1395         /*
1396          * We loop retrieving groups until we find one satisfying
1397          * aggstate->ss.ps.qual
1398          */
1399         while (!aggstate->agg_done)
1400         {
1401                 /*
1402                  * Find the next entry in the hash table
1403                  */
1404                 entry = (AggHashEntry) ScanTupleHashTable(&aggstate->hashiter);
1405                 if (entry == NULL)
1406                 {
1407                         /* No more entries in hashtable, so done */
1408                         aggstate->agg_done = TRUE;
1409                         return NULL;
1410                 }
1411
1412                 /*
1413                  * Clear the per-output-tuple context for each group
1414                  *
1415                  * We intentionally don't use ReScanExprContext here; if any aggs have
1416                  * registered shutdown callbacks, they mustn't be called yet, since we
1417                  * might not be done with that agg.
1418                  */
1419                 ResetExprContext(econtext);
1420
1421                 /*
1422                  * Store the copied first input tuple in the tuple table slot reserved
1423                  * for it, so that it can be used in ExecProject.
1424                  */
1425                 ExecStoreMinimalTuple(entry->shared.firstTuple,
1426                                                           firstSlot,
1427                                                           false);
1428
1429                 pergroup = entry->pergroup;
1430
1431                 /*
1432                  * Finalize each aggregate calculation, and stash results in the
1433                  * per-output-tuple context.
1434                  */
1435                 for (aggno = 0; aggno < aggstate->numaggs; aggno++)
1436                 {
1437                         AggStatePerAgg peraggstate = &peragg[aggno];
1438                         AggStatePerGroup pergroupstate = &pergroup[aggno];
1439
1440                         Assert(peraggstate->numSortCols == 0);
1441                         finalize_aggregate(aggstate, peraggstate, pergroupstate,
1442                                                            &aggvalues[aggno], &aggnulls[aggno]);
1443                 }
1444
1445                 /*
1446                  * Use the representative input tuple for any references to
1447                  * non-aggregated input columns in the qual and tlist.
1448                  */
1449                 econtext->ecxt_outertuple = firstSlot;
1450
1451                 /*
1452                  * Check the qual (HAVING clause); if the group does not match, ignore
1453                  * it and loop back to try to process another group.
1454                  */
1455                 if (ExecQual(aggstate->ss.ps.qual, econtext, false))
1456                 {
1457                         /*
1458                          * Form and return a projection tuple using the aggregate results
1459                          * and the representative input tuple.
1460                          */
1461                         TupleTableSlot *result;
1462                         ExprDoneCond isDone;
1463
1464                         result = ExecProject(aggstate->ss.ps.ps_ProjInfo, &isDone);
1465
1466                         if (isDone != ExprEndResult)
1467                         {
1468                                 aggstate->ss.ps.ps_TupFromTlist =
1469                                         (isDone == ExprMultipleResult);
1470                                 return result;
1471                         }
1472                 }
1473                 else
1474                         InstrCountFiltered1(aggstate, 1);
1475         }
1476
1477         /* No more groups */
1478         return NULL;
1479 }
1480
1481 /* -----------------
1482  * ExecInitAgg
1483  *
1484  *      Creates the run-time information for the agg node produced by the
1485  *      planner and initializes its outer subtree
1486  * -----------------
1487  */
1488 AggState *
1489 ExecInitAgg(Agg *node, EState *estate, int eflags)
1490 {
1491         AggState   *aggstate;
1492         AggStatePerAgg peragg;
1493         Plan       *outerPlan;
1494         ExprContext *econtext;
1495         int                     numaggs,
1496                                 aggno;
1497         ListCell   *l;
1498
1499         /* check for unsupported flags */
1500         Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1501
1502         /*
1503          * create state structure
1504          */
1505         aggstate = makeNode(AggState);
1506         aggstate->ss.ps.plan = (Plan *) node;
1507         aggstate->ss.ps.state = estate;
1508
1509         aggstate->aggs = NIL;
1510         aggstate->numaggs = 0;
1511         aggstate->eqfunctions = NULL;
1512         aggstate->hashfunctions = NULL;
1513         aggstate->peragg = NULL;
1514         aggstate->curperagg = NULL;
1515         aggstate->agg_done = false;
1516         aggstate->pergroup = NULL;
1517         aggstate->grp_firstTuple = NULL;
1518         aggstate->hashtable = NULL;
1519
1520         /*
1521          * Create expression contexts.  We need two, one for per-input-tuple
1522          * processing and one for per-output-tuple processing.  We cheat a little
1523          * by using ExecAssignExprContext() to build both.
1524          */
1525         ExecAssignExprContext(estate, &aggstate->ss.ps);
1526         aggstate->tmpcontext = aggstate->ss.ps.ps_ExprContext;
1527         ExecAssignExprContext(estate, &aggstate->ss.ps);
1528
1529         /*
1530          * We also need a long-lived memory context for holding hashtable data
1531          * structures and transition values.  NOTE: the details of what is stored
1532          * in aggcontext and what is stored in the regular per-query memory
1533          * context are driven by a simple decision: we want to reset the
1534          * aggcontext at group boundaries (if not hashing) and in ExecReScanAgg to
1535          * recover no-longer-wanted space.
1536          */
1537         aggstate->aggcontext =
1538                 AllocSetContextCreate(CurrentMemoryContext,
1539                                                           "AggContext",
1540                                                           ALLOCSET_DEFAULT_MINSIZE,
1541                                                           ALLOCSET_DEFAULT_INITSIZE,
1542                                                           ALLOCSET_DEFAULT_MAXSIZE);
1543
1544         /*
1545          * tuple table initialization
1546          */
1547         ExecInitScanTupleSlot(estate, &aggstate->ss);
1548         ExecInitResultTupleSlot(estate, &aggstate->ss.ps);
1549         aggstate->hashslot = ExecInitExtraTupleSlot(estate);
1550
1551         /*
1552          * initialize child expressions
1553          *
1554          * Note: ExecInitExpr finds Aggrefs for us, and also checks that no aggs
1555          * contain other agg calls in their arguments.  This would make no sense
1556          * under SQL semantics anyway (and it's forbidden by the spec). Because
1557          * that is true, we don't need to worry about evaluating the aggs in any
1558          * particular order.
1559          */
1560         aggstate->ss.ps.targetlist = (List *)
1561                 ExecInitExpr((Expr *) node->plan.targetlist,
1562                                          (PlanState *) aggstate);
1563         aggstate->ss.ps.qual = (List *)
1564                 ExecInitExpr((Expr *) node->plan.qual,
1565                                          (PlanState *) aggstate);
1566
1567         /*
1568          * initialize child nodes
1569          *
1570          * If we are doing a hashed aggregation then the child plan does not need
1571          * to handle REWIND efficiently; see ExecReScanAgg.
1572          */
1573         if (node->aggstrategy == AGG_HASHED)
1574                 eflags &= ~EXEC_FLAG_REWIND;
1575         outerPlan = outerPlan(node);
1576         outerPlanState(aggstate) = ExecInitNode(outerPlan, estate, eflags);
1577
1578         /*
1579          * initialize source tuple type.
1580          */
1581         ExecAssignScanTypeFromOuterPlan(&aggstate->ss);
1582
1583         /*
1584          * Initialize result tuple type and projection info.
1585          */
1586         ExecAssignResultTypeFromTL(&aggstate->ss.ps);
1587         ExecAssignProjectionInfo(&aggstate->ss.ps, NULL);
1588
1589         aggstate->ss.ps.ps_TupFromTlist = false;
1590
1591         /*
1592          * get the count of aggregates in targetlist and quals
1593          */
1594         numaggs = aggstate->numaggs;
1595         Assert(numaggs == list_length(aggstate->aggs));
1596         if (numaggs <= 0)
1597         {
1598                 /*
1599                  * This is not an error condition: we might be using the Agg node just
1600                  * to do hash-based grouping.  Even in the regular case,
1601                  * constant-expression simplification could optimize away all of the
1602                  * Aggrefs in the targetlist and qual.  So keep going, but force local
1603                  * copy of numaggs positive so that palloc()s below don't choke.
1604                  */
1605                 numaggs = 1;
1606         }
1607
1608         /*
1609          * If we are grouping, precompute fmgr lookup data for inner loop. We need
1610          * both equality and hashing functions to do it by hashing, but only
1611          * equality if not hashing.
1612          */
1613         if (node->numCols > 0)
1614         {
1615                 if (node->aggstrategy == AGG_HASHED)
1616                         execTuplesHashPrepare(node->numCols,
1617                                                                   node->grpOperators,
1618                                                                   &aggstate->eqfunctions,
1619                                                                   &aggstate->hashfunctions);
1620                 else
1621                         aggstate->eqfunctions =
1622                                 execTuplesMatchPrepare(node->numCols,
1623                                                                            node->grpOperators);
1624         }
1625
1626         /*
1627          * Set up aggregate-result storage in the output expr context, and also
1628          * allocate my private per-agg working storage
1629          */
1630         econtext = aggstate->ss.ps.ps_ExprContext;
1631         econtext->ecxt_aggvalues = (Datum *) palloc0(sizeof(Datum) * numaggs);
1632         econtext->ecxt_aggnulls = (bool *) palloc0(sizeof(bool) * numaggs);
1633
1634         peragg = (AggStatePerAgg) palloc0(sizeof(AggStatePerAggData) * numaggs);
1635         aggstate->peragg = peragg;
1636
1637         if (node->aggstrategy == AGG_HASHED)
1638         {
1639                 build_hash_table(aggstate);
1640                 aggstate->table_filled = false;
1641                 /* Compute the columns we actually need to hash on */
1642                 aggstate->hash_needed = find_hash_columns(aggstate);
1643         }
1644         else
1645         {
1646                 AggStatePerGroup pergroup;
1647
1648                 pergroup = (AggStatePerGroup) palloc0(sizeof(AggStatePerGroupData) * numaggs);
1649                 aggstate->pergroup = pergroup;
1650         }
1651
1652         /*
1653          * Perform lookups of aggregate function info, and initialize the
1654          * unchanging fields of the per-agg data.  We also detect duplicate
1655          * aggregates (for example, "SELECT sum(x) ... HAVING sum(x) > 0"). When
1656          * duplicates are detected, we only make an AggStatePerAgg struct for the
1657          * first one.  The clones are simply pointed at the same result entry by
1658          * giving them duplicate aggno values.
1659          */
1660         aggno = -1;
1661         foreach(l, aggstate->aggs)
1662         {
1663                 AggrefExprState *aggrefstate = (AggrefExprState *) lfirst(l);
1664                 Aggref     *aggref = (Aggref *) aggrefstate->xprstate.expr;
1665                 AggStatePerAgg peraggstate;
1666                 Oid                     inputTypes[FUNC_MAX_ARGS];
1667                 int                     numArguments;
1668                 int                     numDirectArgs;
1669                 int                     numInputs;
1670                 int                     numSortCols;
1671                 int                     numDistinctCols;
1672                 List       *sortlist;
1673                 HeapTuple       aggTuple;
1674                 Form_pg_aggregate aggform;
1675                 Oid                     aggtranstype;
1676                 AclResult       aclresult;
1677                 Oid                     transfn_oid,
1678                                         finalfn_oid;
1679                 Expr       *transfnexpr,
1680                                    *finalfnexpr;
1681                 Datum           textInitVal;
1682                 int                     i;
1683                 ListCell   *lc;
1684
1685                 /* Planner should have assigned aggregate to correct level */
1686                 Assert(aggref->agglevelsup == 0);
1687
1688                 /* Look for a previous duplicate aggregate */
1689                 for (i = 0; i <= aggno; i++)
1690                 {
1691                         if (equal(aggref, peragg[i].aggref) &&
1692                                 !contain_volatile_functions((Node *) aggref))
1693                                 break;
1694                 }
1695                 if (i <= aggno)
1696                 {
1697                         /* Found a match to an existing entry, so just mark it */
1698                         aggrefstate->aggno = i;
1699                         continue;
1700                 }
1701
1702                 /* Nope, so assign a new PerAgg record */
1703                 peraggstate = &peragg[++aggno];
1704
1705                 /* Mark Aggref state node with assigned index in the result array */
1706                 aggrefstate->aggno = aggno;
1707
1708                 /* Begin filling in the peraggstate data */
1709                 peraggstate->aggrefstate = aggrefstate;
1710                 peraggstate->aggref = aggref;
1711                 peraggstate->sortstate = NULL;
1712
1713                 /* Fetch the pg_aggregate row */
1714                 aggTuple = SearchSysCache1(AGGFNOID,
1715                                                                    ObjectIdGetDatum(aggref->aggfnoid));
1716                 if (!HeapTupleIsValid(aggTuple))
1717                         elog(ERROR, "cache lookup failed for aggregate %u",
1718                                  aggref->aggfnoid);
1719                 aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
1720
1721                 /* Check permission to call aggregate function */
1722                 aclresult = pg_proc_aclcheck(aggref->aggfnoid, GetUserId(),
1723                                                                          ACL_EXECUTE);
1724                 if (aclresult != ACLCHECK_OK)
1725                         aclcheck_error(aclresult, ACL_KIND_PROC,
1726                                                    get_func_name(aggref->aggfnoid));
1727                 InvokeFunctionExecuteHook(aggref->aggfnoid);
1728
1729                 peraggstate->transfn_oid = transfn_oid = aggform->aggtransfn;
1730                 peraggstate->finalfn_oid = finalfn_oid = aggform->aggfinalfn;
1731
1732                 /* Check that aggregate owner has permission to call component fns */
1733                 {
1734                         HeapTuple       procTuple;
1735                         Oid                     aggOwner;
1736
1737                         procTuple = SearchSysCache1(PROCOID,
1738                                                                                 ObjectIdGetDatum(aggref->aggfnoid));
1739                         if (!HeapTupleIsValid(procTuple))
1740                                 elog(ERROR, "cache lookup failed for function %u",
1741                                          aggref->aggfnoid);
1742                         aggOwner = ((Form_pg_proc) GETSTRUCT(procTuple))->proowner;
1743                         ReleaseSysCache(procTuple);
1744
1745                         aclresult = pg_proc_aclcheck(transfn_oid, aggOwner,
1746                                                                                  ACL_EXECUTE);
1747                         if (aclresult != ACLCHECK_OK)
1748                                 aclcheck_error(aclresult, ACL_KIND_PROC,
1749                                                            get_func_name(transfn_oid));
1750                         InvokeFunctionExecuteHook(transfn_oid);
1751                         if (OidIsValid(finalfn_oid))
1752                         {
1753                                 aclresult = pg_proc_aclcheck(finalfn_oid, aggOwner,
1754                                                                                          ACL_EXECUTE);
1755                                 if (aclresult != ACLCHECK_OK)
1756                                         aclcheck_error(aclresult, ACL_KIND_PROC,
1757                                                                    get_func_name(finalfn_oid));
1758                                 InvokeFunctionExecuteHook(finalfn_oid);
1759                         }
1760                 }
1761
1762                 /*
1763                  * Get actual datatypes of the (nominal) aggregate inputs.  These
1764                  * could be different from the agg's declared input types, when the
1765                  * agg accepts ANY or a polymorphic type.
1766                  */
1767                 numArguments = get_aggregate_argtypes(aggref, inputTypes);
1768                 peraggstate->numArguments = numArguments;
1769
1770                 /* Count the "direct" arguments, if any */
1771                 numDirectArgs = list_length(aggref->aggdirectargs);
1772
1773                 /* Count the number of aggregated input columns */
1774                 numInputs = list_length(aggref->args);
1775                 peraggstate->numInputs = numInputs;
1776
1777                 /* Detect how many arguments to pass to the transfn */
1778                 if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
1779                         peraggstate->numTransInputs = numInputs;
1780                 else
1781                         peraggstate->numTransInputs = numArguments;
1782
1783                 /* Detect how many arguments to pass to the finalfn */
1784                 if (aggform->aggfinalextra)
1785                         peraggstate->numFinalArgs = numArguments + 1;
1786                 else
1787                         peraggstate->numFinalArgs = numDirectArgs + 1;
1788
1789                 /* resolve actual type of transition state, if polymorphic */
1790                 aggtranstype = resolve_aggregate_transtype(aggref->aggfnoid,
1791                                                                                                    aggform->aggtranstype,
1792                                                                                                    inputTypes,
1793                                                                                                    numArguments);
1794
1795                 /* build expression trees using actual argument & result types */
1796                 build_aggregate_fnexprs(inputTypes,
1797                                                                 numArguments,
1798                                                                 numDirectArgs,
1799                                                                 peraggstate->numFinalArgs,
1800                                                                 aggref->aggvariadic,
1801                                                                 aggtranstype,
1802                                                                 aggref->aggtype,
1803                                                                 aggref->inputcollid,
1804                                                                 transfn_oid,
1805                                                                 InvalidOid,             /* invtrans is not needed here */
1806                                                                 finalfn_oid,
1807                                                                 &transfnexpr,
1808                                                                 NULL,
1809                                                                 &finalfnexpr);
1810
1811                 /* set up infrastructure for calling the transfn and finalfn */
1812                 fmgr_info(transfn_oid, &peraggstate->transfn);
1813                 fmgr_info_set_expr((Node *) transfnexpr, &peraggstate->transfn);
1814
1815                 if (OidIsValid(finalfn_oid))
1816                 {
1817                         fmgr_info(finalfn_oid, &peraggstate->finalfn);
1818                         fmgr_info_set_expr((Node *) finalfnexpr, &peraggstate->finalfn);
1819                 }
1820
1821                 peraggstate->aggCollation = aggref->inputcollid;
1822
1823                 InitFunctionCallInfoData(peraggstate->transfn_fcinfo,
1824                                                                  &peraggstate->transfn,
1825                                                                  peraggstate->numTransInputs + 1,
1826                                                                  peraggstate->aggCollation,
1827                                                                  (void *) aggstate, NULL);
1828
1829                 /* get info about relevant datatypes */
1830                 get_typlenbyval(aggref->aggtype,
1831                                                 &peraggstate->resulttypeLen,
1832                                                 &peraggstate->resulttypeByVal);
1833                 get_typlenbyval(aggtranstype,
1834                                                 &peraggstate->transtypeLen,
1835                                                 &peraggstate->transtypeByVal);
1836
1837                 /*
1838                  * initval is potentially null, so don't try to access it as a struct
1839                  * field. Must do it the hard way with SysCacheGetAttr.
1840                  */
1841                 textInitVal = SysCacheGetAttr(AGGFNOID, aggTuple,
1842                                                                           Anum_pg_aggregate_agginitval,
1843                                                                           &peraggstate->initValueIsNull);
1844
1845                 if (peraggstate->initValueIsNull)
1846                         peraggstate->initValue = (Datum) 0;
1847                 else
1848                         peraggstate->initValue = GetAggInitVal(textInitVal,
1849                                                                                                    aggtranstype);
1850
1851                 /*
1852                  * If the transfn is strict and the initval is NULL, make sure input
1853                  * type and transtype are the same (or at least binary-compatible), so
1854                  * that it's OK to use the first aggregated input value as the initial
1855                  * transValue.  This should have been checked at agg definition time,
1856                  * but we must check again in case the transfn's strictness property
1857                  * has been changed.
1858                  */
1859                 if (peraggstate->transfn.fn_strict && peraggstate->initValueIsNull)
1860                 {
1861                         if (numArguments <= numDirectArgs ||
1862                                 !IsBinaryCoercible(inputTypes[numDirectArgs], aggtranstype))
1863                                 ereport(ERROR,
1864                                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1865                                                  errmsg("aggregate %u needs to have compatible input type and transition type",
1866                                                                 aggref->aggfnoid)));
1867                 }
1868
1869                 /*
1870                  * Get a tupledesc corresponding to the aggregated inputs (including
1871                  * sort expressions) of the agg.
1872                  */
1873                 peraggstate->evaldesc = ExecTypeFromTL(aggref->args, false);
1874
1875                 /* Create slot we're going to do argument evaluation in */
1876                 peraggstate->evalslot = ExecInitExtraTupleSlot(estate);
1877                 ExecSetSlotDescriptor(peraggstate->evalslot, peraggstate->evaldesc);
1878
1879                 /* Set up projection info for evaluation */
1880                 peraggstate->evalproj = ExecBuildProjectionInfo(aggrefstate->args,
1881                                                                                                                 aggstate->tmpcontext,
1882                                                                                                                 peraggstate->evalslot,
1883                                                                                                                 NULL);
1884
1885                 /*
1886                  * If we're doing either DISTINCT or ORDER BY for a plain agg, then we
1887                  * have a list of SortGroupClause nodes; fish out the data in them and
1888                  * stick them into arrays.  We ignore ORDER BY for an ordered-set agg,
1889                  * however; the agg's transfn and finalfn are responsible for that.
1890                  *
1891                  * Note that by construction, if there is a DISTINCT clause then the
1892                  * ORDER BY clause is a prefix of it (see transformDistinctClause).
1893                  */
1894                 if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
1895                 {
1896                         sortlist = NIL;
1897                         numSortCols = numDistinctCols = 0;
1898                 }
1899                 else if (aggref->aggdistinct)
1900                 {
1901                         sortlist = aggref->aggdistinct;
1902                         numSortCols = numDistinctCols = list_length(sortlist);
1903                         Assert(numSortCols >= list_length(aggref->aggorder));
1904                 }
1905                 else
1906                 {
1907                         sortlist = aggref->aggorder;
1908                         numSortCols = list_length(sortlist);
1909                         numDistinctCols = 0;
1910                 }
1911
1912                 peraggstate->numSortCols = numSortCols;
1913                 peraggstate->numDistinctCols = numDistinctCols;
1914
1915                 if (numSortCols > 0)
1916                 {
1917                         /*
1918                          * We don't implement DISTINCT or ORDER BY aggs in the HASHED case
1919                          * (yet)
1920                          */
1921                         Assert(node->aggstrategy != AGG_HASHED);
1922
1923                         /* If we have only one input, we need its len/byval info. */
1924                         if (numInputs == 1)
1925                         {
1926                                 get_typlenbyval(inputTypes[numDirectArgs],
1927                                                                 &peraggstate->inputtypeLen,
1928                                                                 &peraggstate->inputtypeByVal);
1929                         }
1930                         else if (numDistinctCols > 0)
1931                         {
1932                                 /* we will need an extra slot to store prior values */
1933                                 peraggstate->uniqslot = ExecInitExtraTupleSlot(estate);
1934                                 ExecSetSlotDescriptor(peraggstate->uniqslot,
1935                                                                           peraggstate->evaldesc);
1936                         }
1937
1938                         /* Extract the sort information for use later */
1939                         peraggstate->sortColIdx =
1940                                 (AttrNumber *) palloc(numSortCols * sizeof(AttrNumber));
1941                         peraggstate->sortOperators =
1942                                 (Oid *) palloc(numSortCols * sizeof(Oid));
1943                         peraggstate->sortCollations =
1944                                 (Oid *) palloc(numSortCols * sizeof(Oid));
1945                         peraggstate->sortNullsFirst =
1946                                 (bool *) palloc(numSortCols * sizeof(bool));
1947
1948                         i = 0;
1949                         foreach(lc, sortlist)
1950                         {
1951                                 SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
1952                                 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
1953                                                                                                                    aggref->args);
1954
1955                                 /* the parser should have made sure of this */
1956                                 Assert(OidIsValid(sortcl->sortop));
1957
1958                                 peraggstate->sortColIdx[i] = tle->resno;
1959                                 peraggstate->sortOperators[i] = sortcl->sortop;
1960                                 peraggstate->sortCollations[i] = exprCollation((Node *) tle->expr);
1961                                 peraggstate->sortNullsFirst[i] = sortcl->nulls_first;
1962                                 i++;
1963                         }
1964                         Assert(i == numSortCols);
1965                 }
1966
1967                 if (aggref->aggdistinct)
1968                 {
1969                         Assert(numArguments > 0);
1970
1971                         /*
1972                          * We need the equal function for each DISTINCT comparison we will
1973                          * make.
1974                          */
1975                         peraggstate->equalfns =
1976                                 (FmgrInfo *) palloc(numDistinctCols * sizeof(FmgrInfo));
1977
1978                         i = 0;
1979                         foreach(lc, aggref->aggdistinct)
1980                         {
1981                                 SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
1982
1983                                 fmgr_info(get_opcode(sortcl->eqop), &peraggstate->equalfns[i]);
1984                                 i++;
1985                         }
1986                         Assert(i == numDistinctCols);
1987                 }
1988
1989                 ReleaseSysCache(aggTuple);
1990         }
1991
1992         /* Update numaggs to match number of unique aggregates found */
1993         aggstate->numaggs = aggno + 1;
1994
1995         return aggstate;
1996 }
1997
1998 static Datum
1999 GetAggInitVal(Datum textInitVal, Oid transtype)
2000 {
2001         Oid                     typinput,
2002                                 typioparam;
2003         char       *strInitVal;
2004         Datum           initVal;
2005
2006         getTypeInputInfo(transtype, &typinput, &typioparam);
2007         strInitVal = TextDatumGetCString(textInitVal);
2008         initVal = OidInputFunctionCall(typinput, strInitVal,
2009                                                                    typioparam, -1);
2010         pfree(strInitVal);
2011         return initVal;
2012 }
2013
2014 void
2015 ExecEndAgg(AggState *node)
2016 {
2017         PlanState  *outerPlan;
2018         int                     aggno;
2019
2020         /* Make sure we have closed any open tuplesorts */
2021         for (aggno = 0; aggno < node->numaggs; aggno++)
2022         {
2023                 AggStatePerAgg peraggstate = &node->peragg[aggno];
2024
2025                 if (peraggstate->sortstate)
2026                         tuplesort_end(peraggstate->sortstate);
2027         }
2028
2029         /* And ensure any agg shutdown callbacks have been called */
2030         ReScanExprContext(node->ss.ps.ps_ExprContext);
2031
2032         /*
2033          * Free both the expr contexts.
2034          */
2035         ExecFreeExprContext(&node->ss.ps);
2036         node->ss.ps.ps_ExprContext = node->tmpcontext;
2037         ExecFreeExprContext(&node->ss.ps);
2038
2039         /* clean up tuple table */
2040         ExecClearTuple(node->ss.ss_ScanTupleSlot);
2041
2042         MemoryContextDelete(node->aggcontext);
2043
2044         outerPlan = outerPlanState(node);
2045         ExecEndNode(outerPlan);
2046 }
2047
2048 void
2049 ExecReScanAgg(AggState *node)
2050 {
2051         ExprContext *econtext = node->ss.ps.ps_ExprContext;
2052         int                     aggno;
2053
2054         node->agg_done = false;
2055
2056         node->ss.ps.ps_TupFromTlist = false;
2057
2058         if (((Agg *) node->ss.ps.plan)->aggstrategy == AGG_HASHED)
2059         {
2060                 /*
2061                  * In the hashed case, if we haven't yet built the hash table then we
2062                  * can just return; nothing done yet, so nothing to undo. If subnode's
2063                  * chgParam is not NULL then it will be re-scanned by ExecProcNode,
2064                  * else no reason to re-scan it at all.
2065                  */
2066                 if (!node->table_filled)
2067                         return;
2068
2069                 /*
2070                  * If we do have the hash table and the subplan does not have any
2071                  * parameter changes, then we can just rescan the existing hash table;
2072                  * no need to build it again.
2073                  */
2074                 if (node->ss.ps.lefttree->chgParam == NULL)
2075                 {
2076                         ResetTupleHashIterator(node->hashtable, &node->hashiter);
2077                         return;
2078                 }
2079         }
2080
2081         /* Make sure we have closed any open tuplesorts */
2082         for (aggno = 0; aggno < node->numaggs; aggno++)
2083         {
2084                 AggStatePerAgg peraggstate = &node->peragg[aggno];
2085
2086                 if (peraggstate->sortstate)
2087                         tuplesort_end(peraggstate->sortstate);
2088                 peraggstate->sortstate = NULL;
2089         }
2090
2091         /* We don't need to ReScanExprContext here; ExecReScan already did it */
2092
2093         /* Release first tuple of group, if we have made a copy */
2094         if (node->grp_firstTuple != NULL)
2095         {
2096                 heap_freetuple(node->grp_firstTuple);
2097                 node->grp_firstTuple = NULL;
2098         }
2099
2100         /* Forget current agg values */
2101         MemSet(econtext->ecxt_aggvalues, 0, sizeof(Datum) * node->numaggs);
2102         MemSet(econtext->ecxt_aggnulls, 0, sizeof(bool) * node->numaggs);
2103
2104         /*
2105          * Release all temp storage. Note that with AGG_HASHED, the hash table is
2106          * allocated in a sub-context of the aggcontext. We're going to rebuild
2107          * the hash table from scratch, so we need to use
2108          * MemoryContextResetAndDeleteChildren() to avoid leaking the old hash
2109          * table's memory context header.
2110          */
2111         MemoryContextResetAndDeleteChildren(node->aggcontext);
2112
2113         if (((Agg *) node->ss.ps.plan)->aggstrategy == AGG_HASHED)
2114         {
2115                 /* Rebuild an empty hash table */
2116                 build_hash_table(node);
2117                 node->table_filled = false;
2118         }
2119         else
2120         {
2121                 /*
2122                  * Reset the per-group state (in particular, mark transvalues null)
2123                  */
2124                 MemSet(node->pergroup, 0,
2125                            sizeof(AggStatePerGroupData) * node->numaggs);
2126         }
2127
2128         /*
2129          * if chgParam of subnode is not null then plan will be re-scanned by
2130          * first ExecProcNode.
2131          */
2132         if (node->ss.ps.lefttree->chgParam == NULL)
2133                 ExecReScan(node->ss.ps.lefttree);
2134 }
2135
2136
2137 /***********************************************************************
2138  * API exposed to aggregate functions
2139  ***********************************************************************/
2140
2141
2142 /*
2143  * AggCheckCallContext - test if a SQL function is being called as an aggregate
2144  *
2145  * The transition and/or final functions of an aggregate may want to verify
2146  * that they are being called as aggregates, rather than as plain SQL
2147  * functions.  They should use this function to do so.  The return value
2148  * is nonzero if being called as an aggregate, or zero if not.  (Specific
2149  * nonzero values are AGG_CONTEXT_AGGREGATE or AGG_CONTEXT_WINDOW, but more
2150  * values could conceivably appear in future.)
2151  *
2152  * If aggcontext isn't NULL, the function also stores at *aggcontext the
2153  * identity of the memory context that aggregate transition values are
2154  * being stored in.
2155  */
2156 int
2157 AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
2158 {
2159         if (fcinfo->context && IsA(fcinfo->context, AggState))
2160         {
2161                 if (aggcontext)
2162                         *aggcontext = ((AggState *) fcinfo->context)->aggcontext;
2163                 return AGG_CONTEXT_AGGREGATE;
2164         }
2165         if (fcinfo->context && IsA(fcinfo->context, WindowAggState))
2166         {
2167                 if (aggcontext)
2168                         *aggcontext = ((WindowAggState *) fcinfo->context)->curaggcontext;
2169                 return AGG_CONTEXT_WINDOW;
2170         }
2171
2172         /* this is just to prevent "uninitialized variable" warnings */
2173         if (aggcontext)
2174                 *aggcontext = NULL;
2175         return 0;
2176 }
2177
2178 /*
2179  * AggGetAggref - allow an aggregate support function to get its Aggref
2180  *
2181  * If the function is being called as an aggregate support function,
2182  * return the Aggref node for the aggregate call.  Otherwise, return NULL.
2183  *
2184  * Note that if an aggregate is being used as a window function, this will
2185  * return NULL.  We could provide a similar function to return the relevant
2186  * WindowFunc node in such cases, but it's not needed yet.
2187  */
2188 Aggref *
2189 AggGetAggref(FunctionCallInfo fcinfo)
2190 {
2191         if (fcinfo->context && IsA(fcinfo->context, AggState))
2192         {
2193                 AggStatePerAgg curperagg = ((AggState *) fcinfo->context)->curperagg;
2194
2195                 if (curperagg)
2196                         return curperagg->aggref;
2197         }
2198         return NULL;
2199 }
2200
2201 /*
2202  * AggGetPerTupleEContext - fetch per-input-tuple ExprContext
2203  *
2204  * This is useful in agg final functions; the econtext returned is the
2205  * same per-tuple context that the transfn was called in (which can
2206  * safely get reset during the final function).
2207  *
2208  * As above, this is currently not useful for aggs called as window functions.
2209  */
2210 ExprContext *
2211 AggGetPerTupleEContext(FunctionCallInfo fcinfo)
2212 {
2213         if (fcinfo->context && IsA(fcinfo->context, AggState))
2214         {
2215                 AggState   *aggstate = (AggState *) fcinfo->context;
2216
2217                 return aggstate->tmpcontext;
2218         }
2219         return NULL;
2220 }
2221
2222 /*
2223  * AggGetPerAggEContext - fetch per-output-tuple ExprContext
2224  *
2225  * This is useful for aggs to register shutdown callbacks, which will ensure
2226  * that non-memory resources are freed.
2227  *
2228  * As above, this is currently not useful for aggs called as window functions.
2229  */
2230 ExprContext *
2231 AggGetPerAggEContext(FunctionCallInfo fcinfo)
2232 {
2233         if (fcinfo->context && IsA(fcinfo->context, AggState))
2234         {
2235                 AggState   *aggstate = (AggState *) fcinfo->context;
2236
2237                 return aggstate->ss.ps.ps_ExprContext;
2238         }
2239         return NULL;
2240 }
2241
2242
2243 /*
2244  * aggregate_dummy - dummy execution routine for aggregate functions
2245  *
2246  * This function is listed as the implementation (prosrc field) of pg_proc
2247  * entries for aggregate functions.  Its only purpose is to throw an error
2248  * if someone mistakenly executes such a function in the normal way.
2249  *
2250  * Perhaps someday we could assign real meaning to the prosrc field of
2251  * an aggregate?
2252  */
2253 Datum
2254 aggregate_dummy(PG_FUNCTION_ARGS)
2255 {
2256         elog(ERROR, "aggregate function %u called as normal function",
2257                  fcinfo->flinfo->fn_oid);
2258         return (Datum) 0;                       /* keep compiler quiet */
2259 }