* 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
| 885 | * When called, CurrentMemoryContext should be the per-query context. |
| 886 | */ |
| 887 | static void |
| 888 | process_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 |
no test coverage detected