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

Function tdigest_add_double

pg_documentdb/src/aggregation/bson_tdigest.c:675–751  ·  view source on GitHub ↗

* Add a value to the tdigest (create one if needed). Transition function * for tdigest aggregate with a single percentile. */

Source from the content-addressed store, hash-verified

673 * for tdigest aggregate with a single percentile.
674 */
675Datum
676tdigest_add_double(PG_FUNCTION_ARGS)
677{
678 tdigest_aggstate_t *state;
679
680 MemoryContext aggcontext;
681
682 /* cannot be called directly because of internal-type argument */
683 if (!AggCheckCallContext(fcinfo, &aggcontext))
684 {
685 elog(ERROR, "tdigest_add_double called in non-aggregate context");
686 }
687
688 /*
689 * We want to skip NULL values altogether - we return either the existing
690 * t-digest (if it already exists) or NULL.
691 */
692 if (PG_ARGISNULL(1))
693 {
694 if (PG_ARGISNULL(0))
695 {
696 PG_RETURN_NULL();
697 }
698
699 /* if there already is a state accumulated, don't forget it */
700 PG_RETURN_DATUM(PG_GETARG_DATUM(0));
701 }
702
703 /* if there's no digest allocated, create it now */
704 if (PG_ARGISNULL(0))
705 {
706 int compression = PG_GETARG_INT32(2);
707 int npercentiles = 1;
708 MemoryContext oldcontext;
709
710 check_compression(compression);
711
712 oldcontext = MemoryContextSwitchTo(aggcontext);
713
714 pgbson *percentilesPgbson = PG_GETARG_MAYBE_NULL_PGBSON(3);
715 if (percentilesPgbson == NULL || IsPgbsonEmptyDocument(percentilesPgbson))
716 {
717 PG_RETURN_NULL();
718 }
719 pgbsonelement percentilesPgbsonElement;
720 PgbsonToSinglePgbsonElement(percentilesPgbson, &percentilesPgbsonElement);
721 double percentile = percentilesPgbsonElement.bsonValue.value.v_double;
722
723 state = tdigest_aggstate_allocate(npercentiles, 0, compression);
724
725 state->percentiles[0] = percentile;
726
727 MemoryContextSwitchTo(oldcontext);
728 }
729 else
730 {
731 state = (tdigest_aggstate_t *) PG_GETARG_POINTER(0);
732 }

Callers

nothing calls this directly

Calls 8

check_compressionFunction · 0.85
IsPgbsonEmptyDocumentFunction · 0.85
BsonValueIsNumberFunction · 0.85
IsBsonValueNaNFunction · 0.85
BsonValueAsDoubleQuietFunction · 0.85
tdigest_addFunction · 0.85

Tested by

no test coverage detected