Re: Bug? Unexpected argument handling in pl-python variadic argument function

From: Jan Urbański <wulczer(at)wulczer(dot)org>
To: Nate C <nate1001(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Bug? Unexpected argument handling in pl-python variadic argument function
Date: 2011-01-24 09:23:03
Message-ID: 4D3D44F7.3090308@wulczer.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Itagaki Takahiro 2011-01-24 10:15:26 Re: Add support for logging the current role
Previous Message Dimitri Fontaine 2011-01-24 09:22:23 Extensions support for pg_dump, patch v27