jmalkin commented on code in PR #63:
URL:
https://github.com/apache/datasketches-postgresql/pull/63#discussion_r1177464929
##########
src/hll_sketch_pg_functions.c:
##########
@@ -242,43 +209,105 @@ Datum
pg_hll_sketch_get_estimate_from_internal(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
if (!AggCheckCallContext(fcinfo, &aggcontext)) {
- elog(ERROR, "hll_sketch_from_internal called in non-aggregate context");
+ elog(ERROR, "hll_sketch_get_estimate_from_internal called in non-aggregate
context");
}
oldcontext = MemoryContextSwitchTo(aggcontext);
- sketchptr = PG_GETARG_POINTER(0);
- estimate = hll_sketch_get_estimate(sketchptr);
- hll_sketch_delete(sketchptr);
+ stateptr = (struct hll_agg_state*) PG_GETARG_POINTER(0);
+ estimate = hll_sketch_get_estimate(stateptr->ptr);
+ hll_sketch_delete(stateptr->ptr);
+ pfree(stateptr);
MemoryContextSwitchTo(oldcontext);
PG_RETURN_FLOAT8(estimate);
}
-Datum pg_hll_union_get_result(PG_FUNCTION_ARGS) {
- struct hll_union_state* stateptr;
- void* sketchptr;
- struct ptr_with_size bytes_out;
+Datum pg_hll_sketch_combine(PG_FUNCTION_ARGS) {
+ struct hll_agg_state* stateptr1;
+ struct hll_agg_state* stateptr2;
+ struct hll_agg_state* stateptr;
MemoryContext oldcontext;
MemoryContext aggcontext;
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
+ if (PG_ARGISNULL(0) && PG_ARGISNULL(1)) PG_RETURN_NULL();
if (!AggCheckCallContext(fcinfo, &aggcontext)) {
- elog(ERROR, "hll_union_get_result called in non-aggregate context");
+ elog(ERROR, "hll_sketch_combine called in non-aggregate context");
}
oldcontext = MemoryContextSwitchTo(aggcontext);
- stateptr = (struct hll_union_state*) PG_GETARG_POINTER(0);
+ stateptr1 = (struct hll_agg_state*) PG_GETARG_POINTER(0);
+ stateptr2 = (struct hll_agg_state*) PG_GETARG_POINTER(1);
+
+ stateptr = palloc(sizeof(struct hll_agg_state));
+ stateptr->type = SKETCH;
+ stateptr->lg_k = stateptr1 ? stateptr1->lg_k : stateptr2->lg_k;
+ stateptr->tgt_type = stateptr1 ? stateptr1->tgt_type : stateptr2->tgt_type;
+ stateptr->ptr = hll_union_new(stateptr->lg_k);
Review Comment:
A comment on why we're creating a union after setting `stateptr->type =
SKETCH;` might be useful, even though reading on shows that we overwrite it
with the union result.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]