开发者

Is there any hash function in PostgreSQL?

I am using Sphinx to index my database. The pr开发者_运维问答oblem is I have to filter the result by a character varying field. So I have to find a way to convert character varying to sql_attr_uint. I know that CRC32 in mysql can do the trick. Is there a CRC32 or any replacement in PostgreSQL?


This is the CRC32 function that defines thinking sphinx (gem):

CREATE OR REPLACE FUNCTION crc32(word text)
RETURNS bigint AS $$
DECLARE tmp bigint;
DECLARE i int;
DECLARE j int;
DECLARE byte_length int;
DECLARE word_array bytea;
BEGIN
IF COALESCE(word, '') = '' THEN
return 0;
END IF;

i = 0;
tmp = 4294967295;
byte_length = bit_length(word) / 8;
word_array = decode(replace(word, E'\\\\', E'\\\\\\\\'), 'escape');
LOOP
tmp = (tmp # get_byte(word_array, i))::bigint;
i = i + 1;
j = 0;
LOOP
tmp = ((tmp >> 1) # (3988292384 * (tmp & 1)))::bigint;
j = j + 1;
IF j >= 8 THEN
EXIT;
END IF;
END LOOP;
IF i >= byte_length THEN
EXIT;
END IF;
END LOOP;
return (tmp # 4294967295);
END
$$ IMMUTABLE LANGUAGE plpgsql;


Maybe you can use decode(substring(md5('foo') for 8), 'hex'). This would get you bytea of first 4 bytes of md5 hash of this string.

You can convert it to integer using something like:

create function bytea_to_integer(bytea)
returns integer strict
language sql as $$
  select
     (get_byte($1,0)*1::integer<<0*8)
    +(get_byte($1,1)*1::integer<<1*8)
    +(get_byte($1,2)*1::integer<<2*8)
    +(get_byte($1,3)*1::integer<<3*8);
$$;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜