Thread: variadic array arguments, can it work?

variadic array arguments, can it work?

From
Ingmar Brouns
Date:


Hi,

I was trying to write a variadic function where the arguments themselves are arrays, but calling it does not seem to work. I couldn't find documentation mentioning this restriction

postgres=# create or replace function foo(variadic args integer[][]) returns integer
as $$
begin return args[2][2]; end;
$$ language plpgsql;

Now I can call the function using variadic:

postgres=# select foo(variadic array[array[1,2],array[3,4]]);
 foo
-----
   4
(1 row)

but I cannot call it in the normal way...

postgres=# select foo( array[1,2] , array[3,4] );
ERROR:  function foo(integer[], integer[]) does not exist at character 8
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
STATEMENT:  select foo( array[1,2] , array[3,4] );
ERROR:  function foo(integer[], integer[]) does not exist
LINE 1: select foo( array[1,2] , array[3,4] );
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

I suspect this has to do something with multiple dimensional arrays not truly being arrays of arrays...


Kind regards,

Ingmar Brouns


                                                   version                                               
   
----------------------------------------------------------------------------------------------------------
----
 PostgreSQL 9.1.1 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.6.1 20110908 (Red Hat 4.6.1-9), 64-
bit
(1 row)

Re: variadic array arguments, can it work?

From
Tom Lane
Date:
Ingmar Brouns <swingi@gmail.com> writes:
> I was trying to write a variadic function where the arguments themselves
> are arrays, but calling it does not seem to work. I couldn't find
> documentation mentioning this restriction

> postgres=# create or replace function foo(variadic args integer[][])

The reason that doesn't work the way you're expecting is that
1-dimensional integer arrays are not a distinct datatype from
2-dimensional integer arrays.  The system just sees "variadic int[]"
and expects simple integers in a variadic expansion.  Sorry.

            regards, tom lane