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

Function CompareNumbers

pg_documentdb_core/src/query/bson_compare.c:1361–1412  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1359
1360
1361static int
1362CompareNumbers(const bson_value_t *leftValue, const bson_value_t *rightValue,
1363 bool *isComparisonValid)
1364{
1365 *isComparisonValid = true;
1366 if (leftValue->value_type == BSON_TYPE_DECIMAL128 ||
1367 rightValue->value_type == BSON_TYPE_DECIMAL128)
1368 {
1369 /* promote type to decimal128 for both values */
1370 bson_value_t leftDecimal, rightDecimal;
1371 leftDecimal.value_type = BSON_TYPE_DECIMAL128;
1372 rightDecimal.value_type = BSON_TYPE_DECIMAL128;
1373
1374 leftDecimal.value.v_decimal128 = GetBsonValueAsDecimal128(leftValue);
1375 rightDecimal.value.v_decimal128 = GetBsonValueAsDecimal128(rightValue);
1376
1377 return CompareBsonDecimal128(&leftDecimal, &rightDecimal, isComparisonValid);
1378 }
1379 else if (leftValue->value_type == BSON_TYPE_DOUBLE ||
1380 rightValue->value_type == BSON_TYPE_DOUBLE)
1381 {
1382 long double leftVal = BsonNumberAsLongDouble(leftValue);
1383 long double rightVal = BsonNumberAsLongDouble(rightValue);
1384
1385 /* special case handling for NaN */
1386 if (isnan(leftVal) || isnan(rightVal))
1387 {
1388 if (isnan(leftVal) && isnan(rightVal))
1389 {
1390 return 0;
1391 }
1392 else if (isnan(leftVal))
1393 {
1394 *isComparisonValid = false;
1395 return -1;
1396 }
1397 else
1398 {
1399 *isComparisonValid = false;
1400 return 1;
1401 }
1402 }
1403
1404 return leftVal > rightVal ? 1 : (leftVal == rightVal ? 0 : -1);
1405 }
1406 else
1407 {
1408 int64_t leftVal = BsonValueAsInt64(leftValue);
1409 int64_t rightVal = BsonValueAsInt64(rightValue);
1410 return leftVal > rightVal ? 1 : (leftVal == rightVal ? 0 : -1);
1411 }
1412}
1413
1414
1415/*

Callers 1

CompareBsonValueFunction · 0.85

Calls 4

GetBsonValueAsDecimal128Function · 0.85
CompareBsonDecimal128Function · 0.85
BsonNumberAsLongDoubleFunction · 0.85
BsonValueAsInt64Function · 0.85

Tested by

no test coverage detected