-
-
Save davidfetter/7089078 to your computer and use it in GitHub Desktop.
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
CREATE OR REPLACE FUNCTION djb2_hash(string text) | |
RETURNS bigint | |
IMMUTABLE | |
STRICT | |
LANGUAGE sql | |
AS $$ | |
WITH RECURSIVE t(seed, string) AS ( | |
VALUES ( | |
87049::bigint, | |
$1 | |
) | |
UNION ALL | |
SELECT | |
( | |
( | |
(t.seed << 5) # | |
t.seed # | |
ascii ( | |
substr ( | |
t.string, | |
1, | |
1 | |
) | |
) | |
) | |
), | |
substr ( | |
t.string, | |
2 | |
) | |
FROM t | |
) | |
SELECT seed & ~(1 << 31)::bigint | |
FROM t | |
WHERE string = '' | |
$$; | |
WITH v(s, e) AS ( | |
VALUES | |
('foo', 796845231), | |
('bar', 796849528), | |
('disqusdev', 835117207), | |
('npr', 796837701), | |
('chicagocubs', 668901606) | |
), test (s,e) AS ( | |
SELECT s, djb2_hash(s) | |
FROM v | |
) | |
SELECT * FROM test | |
UNION ALL | |
SELECT 'Total mismatched', count(*) | |
FROM v JOIN test ON (v.s = test.s) | |
WHERE test.e IS DISTINCT FROM v.e; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment