Skip to content

Instantly share code, notes, and snippets.

@hikoma
Created May 14, 2012 02:42
Show Gist options
  • Save hikoma/2691477 to your computer and use it in GitHub Desktop.
Save hikoma/2691477 to your computer and use it in GitHub Desktop.
A hash function controls the address calculation of linear hashing.
-- http://dev.mysql.com/doc/refman/5.5/en/partitioning-linear-hash.html
DELIMITER $$
DROP FUNCTION IF EXISTS linearHashing$$
CREATE FUNCTION linearHashing(value INT, num INT) RETURNS INT DETERMINISTIC
BEGIN
SET @v := POWER(2, CEILING(LOG(2, num)));
SET @n := value & (@v - 1);
IF @n >= num THEN
SET @v := CEILING(@v / 2);
SET @n := @n & (@v - 1);
END IF;
RETURN @n;
END;
$$
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment