Is there any hash function in PostgreSQL?

邮差的信 提交于 2019-12-08 00:27:53

问题


I am using Sphinx to index my database. The problem 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?


回答1:


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);
$$;



回答2:


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;


来源:https://stackoverflow.com/questions/6800487/is-there-any-hash-function-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!