Python vs BigQuery FarmHash Sometimes Do Not Equal

Viewed 1447

In BigQuery when i run

select farm_fingerprint('6823339101') as f

resuls in

-889610237538610470

In Python

#pip install pyfarmhash
import farmhash
print(farmhash.hash64('6823339101'))

results in

17557133836170941146

BigQuery & Python do agree on most inputs, but there are specific ones like the one above where there is a mismatch for the same input

'6823339101'

How can I get bigquery & python to agree 100% of the time?

Links to bigquery & python hash documentation

https://pypi.org/project/pyfarmhash/

https://cloud.google.com/bigquery/docs/reference/standard-sql/hash_functions

2 Answers

As mentioned in the comments, the function is returning an unsigned int.
So we need to convert that as follows;

import numpy as np
np.uint64(farmhash.fingerprint64(x)).astype('int64')

Relevant issues: https://github.com/lovell/farmhash/issues/26#issuecomment-524581600

Results:

>>> import farmhash
>>> import numpy as np
>>> np.uint64(farmhash.fingerprint64('6823339101')).astype('int64')
-889610237538610470

Quickly scanning over the documentation that you have linked and pyfarmhash source:

The docs for farm_fingerprint read:

Computes the fingerprint of the STRING or BYTES input using the Fingerprint64 function

But in your python code, you are using the hash64 function, which according to the pyfarmhash source code uses a different function from the farmhash library than fingerprint does

Solution:

Use the same function farm_fingerprint is using

import farmhash
print(farmhash.fingerprint64('6823339101'))
Related