in src/aod_sketch_pg_functions.c [238:268]
Datum pg_aod_sketch_from_internal(PG_FUNCTION_ARGS) {
struct aod_agg_state* stateptr;
struct ptr_with_size bytes_out;
MemoryContext oldcontext;
MemoryContext aggcontext;
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
if (!AggCheckCallContext(fcinfo, &aggcontext)) {
elog(ERROR, "aod_sketch_from_internal called in non-aggregate context");
}
oldcontext = MemoryContextSwitchTo(aggcontext);
stateptr = (struct aod_agg_state*) PG_GETARG_POINTER(0);
if (stateptr->type == MUTABLE_SKETCH) {
stateptr->ptr = aod_sketch_compact(stateptr->ptr);
} else if (stateptr->type == UNION) {
stateptr->ptr = aod_union_get_result(stateptr->ptr);
} else if (stateptr->type == INTERSECTION) {
stateptr->ptr = aod_intersection_get_result(stateptr->ptr);
}
bytes_out = aod_sketch_serialize(stateptr->ptr, VARHDRSZ);
compact_aod_sketch_delete(stateptr->ptr);
pfree(stateptr);
SET_VARSIZE(bytes_out.ptr, bytes_out.size);
MemoryContextSwitchTo(oldcontext);
PG_RETURN_BYTEA_P(bytes_out.ptr);
}