gpt4 book ai didi

postgresql - PL/pgSQL 的 CRC32 函数

转载 作者:行者123 更新时间:2023-11-29 12:29:56 25 4
gpt4 key购买 nike

如何计算 32 位循环冗余校验 (CRC-32) 作为 PostgreSQL 中的函数,与 MySQL 相同?

最佳答案

您可以自己创建函数,这是 PostgreSQL 9.6 的工作示例

CREATE OR REPLACE FUNCTION crc32(text_string text) RETURNS bigint AS $$
DECLARE
tmp bigint;
i int;
j int;
byte_length int;
binary_string bytea;
BEGIN
IF text_string = '' THEN
RETURN 0;
END IF;

i = 0;
tmp = 4294967295;
byte_length = bit_length(text_string) / 8;
binary_string = decode(replace(text_string, E'\\\\', E'\\\\\\\\'), 'escape');
LOOP
tmp = (tmp # get_byte(binary_string, 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;

灵感来自 old post with an non crc32 accepted answer .我无法从 thinking sphinx 中找到原始代码.

关于postgresql - PL/pgSQL 的 CRC32 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28179335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com