0

我attemping得到这个问题的职能工作:Intersection of multiple arrays in PostgreSQL交叉口多个文本数组:ERROR:数组值必须以“{”

不像这个问题,我想相交文字数组来代替整数数组。我相应地修改了这两个函数。基本阵列相交功能:

CREATE FUNCTION array_intersect(a1 text[], a2 text[]) RETURNS text[] AS $$ 
DECLARE 
    ret text[]; 
BEGIN 
    IF a1 is null THEN 
     return a2; 
    ELSEIF a2 is null THEN 
     RETURN a1; 
    END IF; 
    SELECT array_agg(e) INTO ret 
    FROM (
     SELECT unnest(a1) 
     INTERSECT 
     SELECT unnest(a2) 
    ) AS dt(e); 
    RETURN ret; 
END; 
$$ language plpgsql; 

聚合函数的定义:

CREATE AGGREGATE utility.array_intersect_agg(
    sfunc = array_intersect, 
    basetype = text[], 
    stype = text[], 
    initcond = NULL 
); 

我得到的错误 “ERROR:数组值必须以 ”{“ 或维度信息 SQL状态:22P02” 当我尝试运行以下代码:

SELECT array_intersect_agg(test) 
    FROM(
    SELECT ARRAY['A','B','C'] test 
    UNION ALL 
    SELECT ARRAY['A','C'] test 
    ) a 

需要更改哪些功能才能使这些功能起作用?

回答

1

对于the documentation

initial_condition

The initial setting for the state value. This must be a string constant in the form accepted for the data type state_data_type. If not specified, the state value starts out null.

所以总的声明应该是这样的:

CREATE AGGREGATE array_intersect_agg(
    sfunc = array_intersect, 
    basetype = text[], 
    stype = text[] 
);