【问题标题】:Postgresql ERROR: operator does not exist: bigint << bigintPostgresql 错误:运算符不存在:bigint << bigint
【发布时间】:2018-05-05 10:45:33
【问题描述】:

我的算法运行良好,但使用新的大型数据库时,我的整数变量超过了最大限制大小。 (我使用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

【问题讨论】:

    标签: postgresql comparison comparison-operators bigint


    【解决方案1】:

    按位移位运算符的右操作数的类型是integer.,可惜the documentation.中没有提到这个

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

    -- 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
    

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

    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)    
    

    以上结果表明运算符&lt;&lt;(按位左移)的左操作数可以是smallint,integerbigint,右操作数必须是integer.

    【讨论】:

    • 谢谢! int2shl, int4shl, int8shl so 有什么区别?所以我必须为我所有的算法做这件事?每个比较运算符都必须在函数中更改?
    猜你喜欢
    • 2019-07-03
    • 2016-06-06
    • 1970-01-01
    • 2021-12-09
    • 2021-11-12
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多