I'm trying to run a plpgsql function on a multi-dimensional array column (int[][]) in a select query.
The function is as such:
CREATE OR REPLACE FUNCTION public.reduce_to_percentages(int[][])
RETURNS float[] AS
$function$
DECLARE
  s int[];
  a float[];
BEGIN
    FOREACH s SLICE 1 IN ARRAY $1 LOOP
        a := a || s[2]::float / s[1]::float;
    END LOOP;
    RETURN a;
END;
$function$
LANGUAGE plpgsql VOLATILE;
The following query works:
SELECT reduce_to_percentages(ARRAY[ARRAY[100, 20], ARRAY[300, 50]]);
So does the following query:
SELECT reduce_to_percentages((SELECT counts FROM objects LIMIT 1));
But the following query provides the function with a null value, and causes an exception when trying to FOREACH over $1:
SELECT reduce_to_percentages(counts) FROM objects;
 
     
    