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

Function bson_min_combine

pg_documentdb/src/aggregation/bson_aggregates.c:939–991  ·  view source on GitHub ↗

* Applies the "combine function" (COMBINEFUNC) for min. * takes two bsons * makes a new bson equal to the minimum */

Source from the content-addressed store, hash-verified

937 * makes a new bson equal to the minimum
938 */
939Datum
940bson_min_combine(PG_FUNCTION_ARGS)
941{
942 MemoryContext aggregateContext;
943 if (!AggCheckCallContext(fcinfo, &aggregateContext))
944 {
945 ereport(ERROR, errmsg(
946 "Aggregate function invoked in non-aggregate context"));
947 }
948
949 /* Create the aggregate state in the aggregate context. */
950 MemoryContext oldContext = MemoryContextSwitchTo(aggregateContext);
951
952 pgbson *left = PG_GETARG_MAYBE_NULL_PGBSON(0);
953 pgbson *right = PG_GETARG_MAYBE_NULL_PGBSON(1);
954 pgbson *result;
955 if (left == NULL)
956 {
957 if (right == NULL)
958 {
959 result = NULL;
960 }
961 else
962 {
963 result = PgbsonCloneFromPgbson(right);
964 }
965 }
966 else if (right == NULL)
967 {
968 result = PgbsonCloneFromPgbson(left);
969 }
970 else
971 {
972 int32_t compResult = ComparePgbson(left, right);
973 if (compResult < 0)
974 {
975 result = PgbsonCloneFromPgbson(left);
976 }
977 else
978 {
979 result = PgbsonCloneFromPgbson(right);
980 }
981 }
982
983 MemoryContextSwitchTo(oldContext);
984
985 if (result == NULL)
986 {
987 PG_RETURN_NULL();
988 }
989
990 PG_RETURN_POINTER(result);
991}
992
993
994/*

Callers

nothing calls this directly

Calls 2

PgbsonCloneFromPgbsonFunction · 0.85
ComparePgbsonFunction · 0.85

Tested by

no test coverage detected