Datum pg_aod_sketch_intersection_combine()

in src/aod_sketch_pg_functions.c [320:363]


Datum pg_aod_sketch_intersection_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_intersection_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->num_values = stateptr1 ? stateptr1->num_values : stateptr2->num_values;
  stateptr->ptr = aod_intersection_new(stateptr->num_values);
  if (stateptr1) {
    if (stateptr1->type == INTERSECTION) {
      stateptr1->ptr = aod_intersection_get_result(stateptr1->ptr);
    }
    aod_intersection_update(stateptr->ptr, stateptr1->ptr);
    compact_aod_sketch_delete(stateptr1->ptr);
    pfree(stateptr1);
  }
  if (stateptr2) {
    if (stateptr2->type == INTERSECTION) {
      stateptr2->ptr = aod_intersection_get_result(stateptr2->ptr);
    }
    aod_intersection_update(stateptr->ptr, stateptr2->ptr);
    compact_aod_sketch_delete(stateptr2->ptr);
    pfree(stateptr2);
  }
  stateptr->ptr = aod_intersection_get_result(stateptr->ptr);

  MemoryContextSwitchTo(oldcontext);

  PG_RETURN_POINTER(stateptr);
}