database - 错误 :could not identify an equality operator for type point

标签 database postgresql

我已经使用这段代码定义了 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;

最佳答案

为了检查 UNIONEXCEPT 子句中的相等性,PostgreSQL 使用类型默认运算符类<的相等运算符/em> 用于 btreehash 访问方法(有关这些术语的解释,请参阅 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/

相关文章:

java - 在 java 中使用多个 WHERE 子句更新 sql 数据库

php - 使用 mysql_real_escape_string 返回空

postgresql - Postgres 的大表建议

arrays - 当数组不超过 50 个元素时,在 PostgreSql 中使用表而不是数组字段类型更好吗?

linux - 须藤: psql: command not found: can't fix it

database - Laravel SelectRaw 与数据库 :Raw

Android 解析指针返回空数据

database - 使用 delphi 的 Dll 中的数据模块?

sql - COPY 动态文件名

postgresql - 配置文件的Postgres加密