From: | Merlin Moncure <mmoncure(at)gmail(dot)com> |
---|---|
To: | Tjibbe Rijpma <tjibbe(at)rijpma(dot)org> |
Cc: | PostgreSQL mailing lists <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: why VARIADIC functions executed on array input? |
Date: | 2014-02-19 14:38:00 |
Message-ID: | CAHyXU0yDR2rOofm2PFiE1r8KWzv1Fxe0tjXp6F4Xc8M-i7hj0Q@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Wed, Feb 19, 2014 at 8:17 AM, Tjibbe <tjibbe(at)rijpma(dot)org> wrote:
> CREATE FUNCTION foo (VARIADIC anyarray) RETURNS void AS $$
> DECLARE
> BEGIN
> END
> $$ LANGUAGE plpgsql;
> SELECT foo (ARRAY[1]);
>
> I would expect an error: Like:
> ERROR: function f3oo(integer[]) does not exist
>
> But I get this error:
> ERROR: could not find array type for data type integer[]
>
> Why VARIADIC functions are found on array input? In my opinion it
> makes no sense and the disadvantage is that overloading with a
> function like: CREATE OR REPLACE FUNCTION foo (anyarray) doesn't
> work.
There are three things working against you here.
#1: sql (and pl/pgsql) level functions have very simple variadic
handling. They only support a single type which is converted to array
#2: postgres isn't smart enough to make arrays of arrays. This can be
worked around in other cases but not here since you're not in control
of the array creating statement. That might be fixed some day but
won't help you in this case unless the way dimensions work is
restructured (which I think unlikely).
#3: the right way to do variadic functions is variadic "any", but via
#1 you can only do this in C.
When you add these together, it comes up with sql level user defined
variadic functions can't take array arguments. The workaround isn't
too bad: just work everything to text.
CREATE FUNCTION foo (VARIADIC anyarray) RETURNS void AS $$
DECLARE
r record;
BEGIN
FOR r IN SELECT unnest(unnest($1)::text[])
LOOP
RAISE NOTICE '%', r;
END LOOP;
END
$$ LANGUAGE plpgsql;
select foo(array[1,2,3]::text, array['foo','bar']::text);
NOTICE: (1)
NOTICE: (2)
NOTICE: (3)
NOTICE: (foo)
NOTICE: (bar)
foo
-----
(1 row)
note: the elements inside may have to be casted back from text
depending on what you want to do with them.
merlin
From | Date | Subject | |
---|---|---|---|
Next Message | Pavel Stehule | 2014-02-19 14:40:20 | Re: why VARIADIC functions executed on array input? |
Previous Message | Dev Kumkar | 2014-02-19 14:19:19 | Timezone information |