我已经使用这段代码定义了 operator =:
create operator = (leftarg = point, rightarg = point, procedure = point_eq, commutator = =);
但是下面的代码仍然没有运行,并且像标题一样出现了错误,有什么问题吗?
create or replace function skyband_get(dataset text, k integer)
returns point[]
as $$
declare
rest point[];
collect point[];
i integer :=0;
begin
execute '(select array_agg('||dataset||' order by y DESC,x DESC) from '||dataset||')' into rest;
while i<k loop
collect := array_cat(collect,array(select * from skyband_sortedlist(rest)));
rest := array(select * from(select * from unnest(rest) except select * from unnest(collect)) p);
i := i + 1;
end loop;
return collect;
end;
$$ language plpgsql;
最佳答案
为了检查 UNION
或 EXCEPT
子句中的相等性,PostgreSQL 使用类型默认运算符类<的相等运算符/em> 用于 btree
或 hash
访问方法(有关这些术语的解释,请参阅 the documentation)。
问题是 point
类型没有这样的运算符类。
您可以自己创建一个。您必须使用 hash
访问方法,因为无法以合理的方式对点进行排序。
hash
运算符类除了相等运算符外,还需要数据类型的哈希函数,但是点
很容易写一个:
CREATE OR REPLACE FUNCTION public.hashpoint(point) RETURNS integer
LANGUAGE sql IMMUTABLE
AS 'SELECT hashfloat8($1[0]) # hashfloat8($1[1])';
现在您可以定义一个运算符类(您必须是 super 用户,因为定义不当的运算符类会混淆或导致服务器崩溃):
CREATE OPERATOR CLASS public.point_hash_ops DEFAULT FOR TYPE point USING hash AS
OPERATOR 1 ~=(point,point),
FUNCTION 1 public.hashpoint(point);
现在它应该可以工作了:
VALUES (POINT '(1,1)'), (POINT '(2, 2)')
EXCEPT
VALUES (POINT '(1,1)');
┌─────────┐
│ column1 │
├─────────┤
│ (2,2) │
└─────────┘
(1 row)
关于database - 错误 :could not identify an equality operator for type point,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43998658/