in src/hll_sketch_pg_functions.c [391:432]
Datum pg_hll_sketch_union(PG_FUNCTION_ARGS) {
const bytea* bytes_in1;
const bytea* bytes_in2;
void* sketchptr1;
void* sketchptr2;
void* unionptr;
void* sketchptr;
struct ptr_with_size bytes_out;
unsigned lg_k;
unsigned tgt_type;
lg_k = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : HLL_DEFAULT_LG_K;
tgt_type = PG_NARGS() > 3 ? PG_GETARG_INT32(3) : 0;
if (tgt_type) {
if ((tgt_type != 4) && (tgt_type != 6) && (tgt_type != 8)) {
elog(ERROR, "hll_sketch_union: unsupported target type, must be 4, 6 or 8");
}
}
unionptr = hll_union_new(lg_k);
if (!PG_ARGISNULL(0)) {
bytes_in1 = PG_GETARG_BYTEA_P(0);
sketchptr1 = hll_sketch_deserialize(VARDATA(bytes_in1), VARSIZE(bytes_in1) - VARHDRSZ);
hll_union_update(unionptr, sketchptr1);
hll_sketch_delete(sketchptr1);
}
if (!PG_ARGISNULL(1)) {
bytes_in2 = PG_GETARG_BYTEA_P(1);
sketchptr2 = hll_sketch_deserialize(VARDATA(bytes_in2), VARSIZE(bytes_in2) - VARHDRSZ);
hll_union_update(unionptr, sketchptr2);
hll_sketch_delete(sketchptr2);
}
if (tgt_type) {
sketchptr = hll_union_get_result_tgt_type(unionptr, tgt_type);
} else {
sketchptr = hll_union_get_result(unionptr);
}
bytes_out = hll_sketch_serialize(sketchptr, VARHDRSZ);
hll_sketch_delete(sketchptr);
SET_VARSIZE(bytes_out.ptr, bytes_out.size);
PG_RETURN_BYTEA_P(bytes_out.ptr);
}