* Applies the "combine function" (COMBINEFUNC) for max. * takes two bsons * makes a new bson equal to the maximum */
| 997 | * makes a new bson equal to the maximum |
| 998 | */ |
| 999 | Datum |
| 1000 | bson_max_combine(PG_FUNCTION_ARGS) |
| 1001 | { |
| 1002 | MemoryContext aggregateContext; |
| 1003 | if (!AggCheckCallContext(fcinfo, &aggregateContext)) |
| 1004 | { |
| 1005 | ereport(ERROR, errmsg( |
| 1006 | "Aggregate function invoked in non-aggregate context")); |
| 1007 | } |
| 1008 | |
| 1009 | /* Create the aggregate state in the aggregate context. */ |
| 1010 | MemoryContext oldContext = MemoryContextSwitchTo(aggregateContext); |
| 1011 | |
| 1012 | pgbson *left = PG_GETARG_MAYBE_NULL_PGBSON(0); |
| 1013 | pgbson *right = PG_GETARG_MAYBE_NULL_PGBSON(1); |
| 1014 | pgbson *result; |
| 1015 | if (left == NULL) |
| 1016 | { |
| 1017 | if (right == NULL) |
| 1018 | { |
| 1019 | result = NULL; |
| 1020 | } |
| 1021 | else |
| 1022 | { |
| 1023 | result = PgbsonCloneFromPgbson(right); |
| 1024 | } |
| 1025 | } |
| 1026 | else if (right == NULL) |
| 1027 | { |
| 1028 | result = PgbsonCloneFromPgbson(left); |
| 1029 | } |
| 1030 | else |
| 1031 | { |
| 1032 | int32_t compResult = ComparePgbson(left, right); |
| 1033 | if (compResult > 0) |
| 1034 | { |
| 1035 | result = PgbsonCloneFromPgbson(left); |
| 1036 | } |
| 1037 | else |
| 1038 | { |
| 1039 | result = PgbsonCloneFromPgbson(right); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | MemoryContextSwitchTo(oldContext); |
| 1044 | |
| 1045 | if (result == NULL) |
| 1046 | { |
| 1047 | PG_RETURN_NULL(); |
| 1048 | } |
| 1049 | |
| 1050 | PG_RETURN_POINTER(result); |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | /* |
nothing calls this directly
no test coverage detected