Postgresql aggregate function sample

create the sfunc

CREATE or replace FUNCTION uuid_set_intersect(uuid[], uuid[])
  RETURNS uuid[]
  language plpgsql
  IMMUTABLE
as $FUNCTION$
declare  _r uuid[];
begin
    if $1 = '{00000000-0000-0000-0000-000000000000}'::uuid[] then 
        _r := $2;
    else 
        SELECT ARRAY(
            SELECT UNNEST($1)
            INTERSECT
            SELECT UNNEST($2)
        ) into _r;
    end if;
    return _r;
end;
$FUNCTION$;

drop aggregate if exists uuid_set_intersect_agg(uuid[]);
create aggregate uuid_set_intersect_agg(uuid[])(
    sfunc = uuid_set_intersect, 
    stype = uuid[],
    INITCOND  = '{00000000-0000-0000-0000-000000000000}'
);
  • https://www.postgresql.org/docs/11/sql-createaggregate.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章