Hrm. Conceptually, I think you actually want something like:
CREATE OR REPLACE FUNCTION public.reduce_dim(anyarray) RETURNS SETOF anyarray LANGUAGE plpgsql AS $function$ DECLARE s $1%type;
d int; BEGIN d := array_ndims($1) - 1; FOREACH s SLICE d IN ARRAY $1 LOOP
RETURN NEXT s; END LOOP; RETURN; END; $function$;
Otherwise, select * from reduce_dim(ARRAY[[1], [2], [3]]) and select * from reduce_dim(ARRAY[[[1], [2], [3]]);
produce the same results. Unfortunately, it looks like the SLICE keyword only accepts a constant.
yes, it accept only constant - it is unpleasant, but it is necessary due plpgsql internals :(
Regards
Pavel Stěhule
Zev
On 11/28/2013 02:28 AM, Pavel Stehule wrote:
Hello
postgres=# CREATE OR REPLACE FUNCTION public.reduce_dim(anyarray) RETURNS SETOF anyarray LANGUAGE plpgsql AS $function$ DECLARE s $1%type; BEGIN FOREACH s SLICE 1 IN ARRAY $1 LOOP RETURN NEXT s; END LOOP; RETURN; END; $function$; CREATE FUNCTION