MCPcopy Create free account
hub / github.com/apache/cloudberry / process_ordered_aggregate_single

Function process_ordered_aggregate_single

src/backend/executor/nodeAgg.c:887–965  ·  view source on GitHub ↗

* Run the transition function for a DISTINCT or ORDER BY aggregate * with only one input. This is called after we have completed * entering all the input values into the sort object. We complete the * sort, read out the values in sorted order, and run the transition * function on each value (applying DISTINCT if appropriate). * * Note that the strictness of the transition function was chec

Source from the content-addressed store, hash-verified

885 * When called, CurrentMemoryContext should be the per-query context.
886 */
887static void
888process_ordered_aggregate_single(AggState *aggstate,
889 AggStatePerTrans pertrans,
890 AggStatePerGroup pergroupstate)
891{
892 Datum oldVal = (Datum) 0;
893 bool oldIsNull = true;
894 bool haveOldVal = false;
895 MemoryContext workcontext = aggstate->tmpcontext->ecxt_per_tuple_memory;
896 MemoryContext oldContext;
897 bool isDistinct = (pertrans->numDistinctCols > 0);
898 Datum newAbbrevVal = (Datum) 0;
899 Datum oldAbbrevVal = (Datum) 0;
900 FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
901 Datum *newVal;
902 bool *isNull;
903
904 Assert(pertrans->numDistinctCols < 2);
905
906 tuplesort_performsort(pertrans->sortstates[aggstate->current_set]);
907
908 /* Load the column into argument 1 (arg 0 will be transition value) */
909 newVal = &fcinfo->args[1].value;
910 isNull = &fcinfo->args[1].isnull;
911
912 /*
913 * Note: if input type is pass-by-ref, the datums returned by the sort are
914 * freshly palloc'd in the per-query context, so we must be careful to
915 * pfree them when they are no longer needed.
916 */
917
918 while (tuplesort_getdatum(pertrans->sortstates[aggstate->current_set],
919 true, newVal, isNull, &newAbbrevVal))
920 {
921 /*
922 * Clear and select the working context for evaluation of the equality
923 * function and transition function.
924 */
925 MemoryContextReset(workcontext);
926 oldContext = MemoryContextSwitchTo(workcontext);
927
928 /*
929 * If DISTINCT mode, and not distinct from prior, skip it.
930 */
931 if (isDistinct &&
932 haveOldVal &&
933 ((oldIsNull && *isNull) ||
934 (!oldIsNull && !*isNull &&
935 oldAbbrevVal == newAbbrevVal &&
936 DatumGetBool(FunctionCall2Coll(&pertrans->equalfnOne,
937 pertrans->aggCollation,
938 oldVal, *newVal)))))
939 {
940 /* equal to prior, so forget this one */
941 if (!pertrans->inputtypeByVal && !*isNull)
942 pfree(DatumGetPointer(*newVal));
943 }
944 else

Callers 1

finalize_aggregatesFunction · 0.85

Calls 9

tuplesort_performsortFunction · 0.85
tuplesort_getdatumFunction · 0.85
MemoryContextResetFunction · 0.85
MemoryContextSwitchToFunction · 0.85
DatumGetBoolFunction · 0.85
FunctionCall2CollFunction · 0.85
tuplesort_endFunction · 0.85
pfreeFunction · 0.50

Tested by

no test coverage detected