CREATE LANGUAGE plpythonu;
postgresql函数
CREATE OR REPLACE FUNCTION myfun1(text)
RETURNS text AS
$BODY$
s = args[0]
h = 0;
n = len(s);
for i, c in enumerate(s):
h = h + ord(c)*31**(n-1-i);
bits = 4*8;
return (h + 2**(bits-1)) % 2**bits - 2**(bits-1)
$BODY$
LANGUAGE ‘plpythonu‘;
调用:返回字符串的hashcode值
select myfun1(‘测试‘),myfun1(‘a‘),myfun1(‘A‘)
上述代码在MAC 笔记本上运行成功
时间: 2024-10-21 01:20:17