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

Function BsonValueHashUint32

pg_documentdb_core/src/query/bson_hash.c:289–453  ·  view source on GitHub ↗

* BsonValueHashUint32 generates a uint32 hash value for a given BSON value. */

Source from the content-addressed store, hash-verified

287 * BsonValueHashUint32 generates a uint32 hash value for a given BSON value.
288 */
289uint32
290BsonValueHashUint32(const bson_value_t *bsonValue)
291{
292 switch (bsonValue->value_type)
293 {
294 case BSON_TYPE_BOOL:
295 {
296 return hash_bytes((const unsigned char *)
297 &(bsonValue->value.v_bool),
298 sizeof(bool));
299 }
300
301 case BSON_TYPE_INT32:
302 case BSON_TYPE_INT64:
303 {
304 int64 value = BsonValueAsInt64(bsonValue);
305 return hash_bytes((const unsigned char *) &value,
306 sizeof(int64));
307 }
308
309 case BSON_TYPE_DOUBLE:
310 {
311 /* NaN and -NaN should create same hash value */
312 if (isnan(bsonValue->value.v_double))
313 {
314 return 1;
315 }
316
317 /* If the value can be converted to int64, then convert it to int64 and generate a hash,
318 * which ensures that the hash value of both 1.00 and 1 remains the same. */
319 bool checkFixedInteger = true;
320 if (IsBsonValue64BitInteger(bsonValue, checkFixedInteger))
321 {
322 int64 value = BsonValueAsInt64(bsonValue);
323 return hash_bytes((const unsigned char *) &value,
324 sizeof(int64));
325 }
326
327 /* In set operators aggregation, non-fixed double and Decimal128 values with the same numerical value are not considered equal.
328 * For example, if we have a double value of "1.1" and a Decimal128 value of "1.1", they will not be considered equal.
329 * To ensure that these values are not treated as equal, different hashes are generated for these values.*/
330 return hash_bytes((const unsigned char *)
331 &bsonValue->value.v_double,
332 sizeof(double));
333 }
334
335 case BSON_TYPE_DECIMAL128:
336 {
337 /* NaN and -NaN should create same hash value */
338 if (IsDecimal128NaN(bsonValue))
339 {
340 return 1;
341 }
342
343 /* If the value can be converted to int64, then convert it to int64 and generate a hash,
344 * which ensures that the hash value of both 1.00 and 1 remains the same. */
345 bool checkFixedInteger = true;
346 if (IsBsonValue64BitInteger(bsonValue, checkFixedInteger))

Callers 1

BsonValueHashFuncCoreFunction · 0.85

Calls 3

BsonValueAsInt64Function · 0.85
IsBsonValue64BitIntegerFunction · 0.85
IsDecimal128NaNFunction · 0.85

Tested by

no test coverage detected