Intersection of multiple text arrays: ERROR: array value must start with "{"

Intersection of multiple text arrays: ERROR: array value must start with "{"

我正在尝试让这个问题中的函数起作用:Intersection of multiple arrays in PostgreSQL

与那个问题不同,我想与文本数组而不是整数数组相交。我相应地修改了这两个函数。基本数组相交函数:

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: array value must start with "{" 或维度信息 SQL 状态:22P02" 当我尝试 运行 以下代码时:

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

需要更改哪些内容才能使这些功能发挥作用?

对于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[]
);