Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronan Dunklau committed Apr 3, 2012
1 parent 86563dc commit b532ca2
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/weighted_mean.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,67 @@ PG_FUNCTION_INFO_V1(_weighted_mean_final);

typedef struct WeightedMeanInternalState
{
Numeric running_sum;
Numeric running_amount;
MemoryContext mcontext;
Numeric running_sum;
Numeric running_amount;
MemoryContext mcontext;
} WeightedMeanInternalState;


Datum
_weighted_mean_final(PG_FUNCTION_ARGS)
{
WeightedMeanInternalState *state;
MemoryContext oldcontext;
Datum total;
MemoryContext oldcontext;
Datum total;

state = (WeightedMeanInternalState *) PG_GETARG_POINTER(0);
oldcontext = MemoryContextSwitchTo(state->mcontext);
oldcontext = MemoryContextSwitchTo(state->mcontext);
total = DirectFunctionCall2(numeric_div, NumericGetDatum(state->running_sum), NumericGetDatum(state->running_amount));
pfree(state);
MemoryContextSwitchTo(oldcontext);
pfree(state);
MemoryContextSwitchTo(oldcontext);
PG_RETURN_NUMERIC(total);
}

Datum
_weighted_mean_intermediate(PG_FUNCTION_ARGS)
{
WeightedMeanInternalState *state;
Datum value,
amount,
temp_total;
MemoryContext aggcontext,
oldcontext,
internalcontext;
Datum value,
amount,
temp_total;
MemoryContext aggcontext,
oldcontext,
internalcontext;


if (!AggCheckCallContext(fcinfo, &aggcontext))
if (!AggCheckCallContext(fcinfo, &aggcontext))
{
/* cannot be called directly because of internal-type argument */
elog(ERROR, "array_agg_transfn called in non-aggregate context");
elog(ERROR, "_weighted_mean_intermediate called in non-aggregate context");
}
if (PG_ARGISNULL(0))
{
internalcontext = AllocSetContextCreate(aggcontext, "WeightedMeanState",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
oldcontext = MemoryContextSwitchTo(internalcontext);
internalcontext = AllocSetContextCreate(aggcontext, "WeightedMeanState",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
oldcontext = MemoryContextSwitchTo(internalcontext);
state = (WeightedMeanInternalState *) palloc(sizeof(WeightedMeanInternalState));
state->mcontext = internalcontext;
state->mcontext = internalcontext;
state->running_sum = DatumGetNumeric(DirectFunctionCall1(numeric_in, CStringGetDatum("0")));
state->running_amount = DatumGetNumeric(DirectFunctionCall1(numeric_in, CStringGetDatum("0")));
}
else
{
state = (WeightedMeanInternalState *) PG_GETARG_POINTER(0);
oldcontext = MemoryContextSwitchTo(state->mcontext);
oldcontext = MemoryContextSwitchTo(state->mcontext);
}
value = PG_GETARG_DATUM(1);
amount = PG_GETARG_DATUM(2);
temp_total = DirectFunctionCall2(numeric_mul, value, amount);
temp_total = DirectFunctionCall2(numeric_mul, value, amount);
state->running_sum = DatumGetNumeric(DirectFunctionCall2(numeric_add, NumericGetDatum(state->running_sum), temp_total));
state->running_amount = DatumGetNumeric(DirectFunctionCall2(numeric_add,
NumericGetDatum(state->running_amount), amount));
MemoryContextSwitchTo(oldcontext);
MemoryContextSwitchTo(oldcontext);
PG_RETURN_POINTER(state);
}

0 comments on commit b532ca2

Please sign in to comment.