Re: Bug? Unexpected argument handling in pl-python variadic argument function - Mailing list pgsql-hackers

From Jan Urbański
Subject Re: Bug? Unexpected argument handling in pl-python variadic argument function
Date
Msg-id 4D3D44F7.3090308@wulczer.org
Whole thread Raw
In response to Bug? Unexpected argument handling in pl-python variadic argument function  (Nate C <nate1001@gmail.com>)
List pgsql-hackers
On 24/01/11 02:01, Nate C wrote:
> Unexpected argument handling in pl/python variadic argument function
> 
> create or replace function variadic_sql
>     (template text, variadic args text[], out text)
>     language sql as
>     $$
>         select $1 || '  --  ' || $2::text
>     $$;
> 
> create or replace function variadic_python
>     (template text, variadic args text[], out text)
>     language plpythonu as
>     $$
>         return template + '  --  ' + str(args)
>     $$;
> 
> 
> -- expected
> select variadic_sql('<html>{foo}{bar}</html>', '1', '2');
>             variadic_sql
> ------------------------------------
>  <html>{foo}{bar}</html>  --  {1,2}
> 
> 
> -- first scalar arg also in the variadic args
> select variadic_python('<html>{foo}{bar}</html>', '1', '2');
> 
> 
>                            variadic_python
> ----------------------------------------------------------------------
>  <html>{foo}{bar}</html>  --  ['<html>{foo}{bar}</html>', ['1', '2']]

You've chosen an unfortunate name for the variadic argument: see
http://www.postgresql.org/docs/current/static/plpython-funcs.html

PL/Python functions automatically have a global "args" variable that is
a list of all the arguments. That's necessary because you can have
functions that don't assign names to variables, like

create function noname_concat(text, text) returns text language
plpythonu as $$ return args[0] + args[1]
$$;

Perhaps we should throw an error if you try to define a function with an
explicit "args" variable? It'd be a backwards-compatibility problem, but
then again these functions are probably already broken for people.


> I could not find very much documentation and only this on the lists:
> 
> from Jan Urbański on his planned improvements for pl/python:
> http://archives.postgresql.org/pgsql-hackers/2010-12/msg00551.php
> 
>> * variadic argument handling (is this possible for a non-plpgsql pl?)

Yes, I thought it would require some work but it turned out that the
generic mechanism already worked for variadic arguments, so there's no
need for specific PL/Python code to support them.

Cheers,
Jan


pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: Is there a way to build PostgreSQL client libraries with MinGW
Next
From: Dimitri Fontaine
Date:
Subject: Extensions support for pg_dump, patch v27