in src/aod_sketch_pg_functions.c [270:318]
Datum pg_aod_sketch_union_combine(PG_FUNCTION_ARGS) {
struct aod_agg_state* stateptr1;
struct aod_agg_state* stateptr2;
struct aod_agg_state* stateptr;
MemoryContext oldcontext;
MemoryContext aggcontext;
if (PG_ARGISNULL(0) && PG_ARGISNULL(1)) PG_RETURN_NULL();
if (!AggCheckCallContext(fcinfo, &aggcontext)) {
elog(ERROR, "aod_sketch_union_combine called in non-aggregate context");
}
oldcontext = MemoryContextSwitchTo(aggcontext);
stateptr1 = (struct aod_agg_state*) PG_GETARG_POINTER(0);
stateptr2 = (struct aod_agg_state*) PG_GETARG_POINTER(1);
stateptr = palloc(sizeof(struct aod_agg_state));
stateptr->type = IMMUTABLE_SKETCH;
stateptr->lg_k = stateptr1 ? stateptr1->lg_k : stateptr2->lg_k;
stateptr->num_values = stateptr1 ? stateptr1->num_values : stateptr2->num_values;
stateptr->ptr = stateptr->lg_k ? aod_union_new_lgk(stateptr->num_values, stateptr->lg_k) : aod_union_new(stateptr->num_values);
if (stateptr1) {
if (stateptr1->type == UNION) {
stateptr1->ptr = aod_union_get_result(stateptr1->ptr);
} else if (stateptr1->type == MUTABLE_SKETCH) {
stateptr1->ptr = aod_sketch_compact(stateptr1->ptr);
}
aod_union_update(stateptr->ptr, stateptr1->ptr);
compact_aod_sketch_delete(stateptr1->ptr);
pfree(stateptr1);
}
if (stateptr2) {
if (stateptr2->type == UNION) {
stateptr2->ptr = aod_union_get_result(stateptr2->ptr);
} else if (stateptr2->type == MUTABLE_SKETCH) {
stateptr2->ptr = aod_sketch_compact(stateptr2->ptr);
}
aod_union_update(stateptr->ptr, stateptr2->ptr);
compact_aod_sketch_delete(stateptr2->ptr);
pfree(stateptr2);
}
stateptr->ptr = aod_union_get_result(stateptr->ptr);
MemoryContextSwitchTo(oldcontext);
PG_RETURN_POINTER(stateptr);
}