Postgresql错误:运算符不存在:bigint<< BIGINT

时间:2017-11-21 20:27:28

标签: postgresql comparison comparison-operators bigint

我的算法工作正常,但是使用新的大数据库,我的整数变量超过了最大限制大小。 (我使用的是powerset算法:https://www.postgresql.org/message-id/20060924054759.GA71934%40winnie.fuhr.org

所以我决定将所有整数更改为bigint,但现在我在比较运算符上遇到了问题...我不知道如何管理它:

CREATE OR REPLACE FUNCTION powerset(a anyarray)
  RETURNS SETOF anyarray AS
$BODY$
DECLARE
    retval  a%TYPE;
    alower  bigint := array_lower(a, 1);
    aupper  bigint := array_upper(a, 1);
    j       bigint;
    k       bigint;
BEGIN
    FOR i IN 1 .. COALESCE((CAST(1 AS BIGINT) << (aupper - alower + 1)) - 1, 0) LOOP
        retval := '{}';
        j := alower;
        k := i;

        WHILE k > CAST(0 AS BIGINT) LOOP
            IF k & CAST(1 AS BIGINT) = CAST(1 AS BIGINT) THEN
                retval := array_append(retval, a[j]);
            END IF;

            j := j + CAST(1 AS BIGINT);
            k := k >> CAST(1 AS BIGINT);
        END LOOP;

        RETURN NEXT retval;
    END LOOP;

    RETURN;
END;
$BODY$
  LANGUAGE plpgsql IMMUTABLE STRICT
  COST 100
  ROWS 1000;
ALTER FUNCTION powerset(anyarray)
  OWNER TO postgres;

我在网上得到了错误:

FOR i IN 1 .. COALESCE((CAST(1 AS BIGINT) << (aupper - alower + 1)) - 1, 0) LOOP

错误42883 Postgresql错误:运算符不存在:bigint&lt;&lt; BIGINT

1 个答案:

答案 0 :(得分:1)

按位移位运算符的右操作数的类型是<ArtistInfo />不幸的是,the documentation.

中没有提到

您应该将移位运算符的右操作数强制转换为integer.

integer:

您可以通过查询系统目录pg_operator,来检查可能的操作数类型,例如:

-- instead of 
-- COALESCE((CAST(1 AS BIGINT) << (aupper - alower + 1)) - 1, 0)
-- use
select COALESCE(1 << (aupper - alower + 1)::int- 1, 0)::bigint

-- instead of
-- k := k >> CAST(1 AS BIGINT);
--- use
k := k >> 1;
-- etc

上面的结果显示运算符select oprname, oprleft::regtype, oprright::regtype from pg_operator where oprname = '<<' and oprcode::text like '%shl%' -- shift left functions oprname | oprleft | oprright ---------+----------+---------- << | smallint | integer << | integer | integer << | bigint | integer (3 rows) 的左操作数(左移位)可以是<< smallint,integer,右操作数必须是{{1} }