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

Function bson_to_double_array

pg_documentdb/src/aggregation/bson_tdigest.c:1192–1231  ·  view source on GitHub ↗

* Transform an input bson to a plain double C array. */

Source from the content-addressed store, hash-verified

1190 * Transform an input bson to a plain double C array.
1191 */
1192static double *
1193bson_to_double_array(FunctionCallInfo fcinfo, bson_value_t *barray, int *len)
1194{
1195 if (barray->value_type != BSON_TYPE_ARRAY || BsonDocumentValueCountKeys(barray) == 0)
1196 {
1197 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_LOCATION7750301), errmsg(
1198 "Expected an array containing numbers from 0.0 to 1.0, but instead received: %s",
1199 BsonValueToJsonForLogging(barray))));
1200 }
1201
1202 (*len) = BsonDocumentValueCountKeys(barray);
1203 double *result = (double *) palloc((*len) * sizeof(double));
1204 bson_iter_t iter;
1205 BsonValueInitIterator(barray, &iter);
1206 int i = 0;
1207 while (bson_iter_next(&iter))
1208 {
1209 const bson_value_t *value = bson_iter_value(&iter);
1210 if (!BsonValueIsNumber(value))
1211 {
1212 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_COMMANDNOTSUPPORTED),
1213 errmsg(
1214 "Accumulator $percentile is not implemented yet")));
1215 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_LOCATION7750302), errmsg(
1216 "Expected an array containing numbers from 0.0 to 1.0, but instead received: %s",
1217 BsonValueToJsonForLogging(value))));
1218 }
1219 double percentile = BsonValueAsDoubleQuiet(bson_iter_value(&iter));
1220 if (percentile < 0 || percentile > 1)
1221 {
1222 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_LOCATION7750303), errmsg(
1223 "Expected an array containing numbers from 0.0 to 1.0, but instead received: %lf",
1224 percentile)));
1225 }
1226 result[i] = percentile;
1227 i++;
1228 }
1229
1230 return result;
1231}
1232
1233
1234/*

Callers 1

tdigest_add_double_arrayFunction · 0.85

Calls 5

BsonValueInitIteratorFunction · 0.85
BsonValueIsNumberFunction · 0.85
BsonValueAsDoubleQuietFunction · 0.85

Tested by

no test coverage detected