Datum pg_theta_sketch_intersection_combine()

in src/theta_sketch_pg_functions.c [292:334]


Datum pg_theta_sketch_intersection_combine(PG_FUNCTION_ARGS) {
  struct agg_state* stateptr1;
  struct agg_state* stateptr2;
  struct agg_state* stateptr;

  MemoryContext oldcontext;
  MemoryContext aggcontext;

  if (PG_ARGISNULL(0) && PG_ARGISNULL(1)) PG_RETURN_NULL();

  if (!AggCheckCallContext(fcinfo, &aggcontext)) {
    elog(ERROR, "theta_sketch_intersection_combine called in non-aggregate context");
  }
  oldcontext = MemoryContextSwitchTo(aggcontext);

  stateptr1 = (struct agg_state*) PG_GETARG_POINTER(0);
  stateptr2 = (struct agg_state*) PG_GETARG_POINTER(1);

  stateptr = palloc(sizeof(struct agg_state));
  stateptr->type = IMMUTABLE_SKETCH;
  stateptr->ptr = theta_intersection_new_default();
  if (stateptr1) {
    if (stateptr1->type == INTERSECTION) {
      stateptr1->ptr = theta_intersection_get_result(stateptr1->ptr);
    }
    theta_intersection_update_with_sketch(stateptr->ptr, stateptr1->ptr);
    theta_sketch_delete(stateptr1->ptr);
    pfree(stateptr1);
  }
  if (stateptr2) {
    if (stateptr2->type == INTERSECTION) {
      stateptr2->ptr = theta_intersection_get_result(stateptr2->ptr);
    }
    theta_intersection_update_with_sketch(stateptr->ptr, stateptr2->ptr);
    theta_sketch_delete(stateptr2->ptr);
    pfree(stateptr2);
  }
  stateptr->ptr = theta_intersection_get_result(stateptr->ptr);

  MemoryContextSwitchTo(oldcontext);

  PG_RETURN_POINTER(stateptr);
}