给定一个integer[]的数组,求其所有非空子集
-- 必须用distinct去重,暂时没找到合适的算法在函数中去重
-- select distinct ov from get_all_sub_array('{3,5,7,2,1}'::integer[]);
CREATE OR REPLACE FUNCTION public.get_all_sub_array(
IN iv integer[],
OUT ov integer[]
)
RETURNS SETOF integer[] AS
$BODY$
DECLARE
nn integer;
ii integer;
xv integer[];
rec record;
BEGIN
if array_length(iv,1)=1 then
ov := iv;
return next;
else
ov := iv;
return next;
for ii in 1..array_length(iv,1) loop
for rec in select * from get_all_sub_array(array_remove(iv,iv[ii])) loop
ov := rec.ov;
return next;
end loop;
end loop;
end if;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.get_all_sub_array(integer[])
OWNER TO root;