Last active
December 11, 2022 11:20
-
-
Save ghafran/8883689 to your computer and use it in GitHub Desktop.
MySQL Function to Calculate Murmur Hash Murmurhash (MurmurHashV2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS MurmurHashV2; | |
DELIMITER // | |
CREATE FUNCTION `MurmurHashV2`(`keyx` varchar(65535), `seed` int unsigned) | |
RETURNS int unsigned | |
BEGIN | |
DECLARE l,i,m,r INT unsigned; | |
DECLARE h,k BIGINT unsigned; | |
SET l = LENGTH(keyx), i=1, m = 0x5bd1e995, r=24; | |
SET h = seed ^ l; | |
WHILE l>=4 DO | |
set k = ( ascii(mid(keyx,i,1)) & 0xff ) | | |
((ascii(mid(keyx,i+1,1)) & 0xff) << 8) | | |
((ascii(mid(keyx,i+2,1)) & 0xff) << 16) | | |
((ascii(mid(keyx,i+3,1)) & 0xff) << 24); | |
set k = ( (k*m) & 0xffffffff ); | |
set k = k ^ (k >> 24); | |
set k = ( (k*m) & 0xffffffff ); | |
set h = ( (h*m) & 0xffffffff ); | |
set h = h^k; | |
set l = l-4; | |
set i = i+4; | |
END WHILE; | |
IF l>=3 THEN SET h = h^((ascii(mid(keyx,i + 2,1)) & 0xff) << 16); END IF; | |
IF l>=2 THEN SET h = h^((ascii(mid(keyx,i + 1,1)) & 0xff) << 8); END IF; | |
IF l>=1 THEN SET h = h^(ascii(mid(keyx,i,1)) & 0xff); | |
set h = ( (h*m) & 0xffffffff ); | |
END IF; | |
Set h = h^(h >> 13); | |
set h = ( (h*m) & 0xffffffff ); | |
set h = h^(h >> 15); | |
RETURN h; -- AUTO CAST | |
END;// | |
DELIMITER ; | |
Select MurmurHashV2('test', 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment