i have a database that contains some signed 64bits integers, these were hash that we converted to signed integer for easy db operation, i want to perform the hamming distance calculation and return the top 5 hashes that are closest to the reference hash. but somehow i cant pull it off, using other databases is not an option now, here is the query i am currently struggling with.
WITH RECURSIVE HammingDistance AS (
SELECT signed_hash,
ABS(signed_hash - reference_signed_hash) AS xor_result,
0 AS hamming_distance
FROM hashdb
UNION ALL
SELECT signed_hash,
xor_result >> 1,
hamming_distance + (xor_result & 1)
FROM HammingDistance
WHERE xor_result > 0
)
SELECT signed_hash, hamming_distance
FROM HammingDistance
ORDER BY hamming_distance
LIMIT 5;
this is just me trying to see what sticks with the query, any help is appreciated.