MCPcopy Create free account
hub / github.com/documentdb/documentdb / tdigest_combine

Function tdigest_combine

pg_documentdb/src/aggregation/bson_tdigest.c:1076–1144  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1074
1075
1076Datum
1077tdigest_combine(PG_FUNCTION_ARGS)
1078{
1079 tdigest_aggstate_t *src;
1080 tdigest_aggstate_t *dst;
1081 MemoryContext aggcontext;
1082 MemoryContext oldcontext;
1083
1084 if (!AggCheckCallContext(fcinfo, &aggcontext))
1085 {
1086 elog(ERROR, "tdigest_combine called in non-aggregate context");
1087 }
1088
1089 /* if no "merged" state yet, try creating it */
1090 if (PG_ARGISNULL(0))
1091 {
1092 /* nope, the second argument is NULL to, so return NULL */
1093 if (PG_ARGISNULL(1))
1094 {
1095 PG_RETURN_NULL();
1096 }
1097
1098 /* the second argument is not NULL, so copy it */
1099 src = (tdigest_aggstate_t *) PG_GETARG_POINTER(1);
1100
1101 /* copy the digest into the right long-lived memory context */
1102 oldcontext = MemoryContextSwitchTo(aggcontext);
1103 src = tdigest_copy(src);
1104 MemoryContextSwitchTo(oldcontext);
1105
1106 PG_RETURN_POINTER(src);
1107 }
1108
1109 /*
1110 * If the second argument is NULL, just return the first one (we know
1111 * it's not NULL at this point).
1112 */
1113 if (PG_ARGISNULL(1))
1114 {
1115 PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1116 }
1117
1118 /* Now we know neither argument is NULL, so merge them. */
1119 src = (tdigest_aggstate_t *) PG_GETARG_POINTER(1);
1120 dst = (tdigest_aggstate_t *) PG_GETARG_POINTER(0);
1121
1122 /*
1123 * Do a compaction on each digest, to make sure we have enough space.
1124 *
1125 * XXX Maybe do this only when necessary, i.e. when we can't fit the
1126 * data into the dst digest? Also, is it really ensured this gives us
1127 * enough free space?
1128 */
1129 tdigest_compact(dst);
1130 tdigest_compact(src);
1131
1132 /* copy the second part */
1133 memcpy(&dst->centroids[dst->ncentroids],

Callers

nothing calls this directly

Calls 2

tdigest_copyFunction · 0.85
tdigest_compactFunction · 0.85

Tested by

no test coverage detected