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

Started by Nate Calmost 15 years ago2 messages
#1Nate C
nate1001@gmail.com

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']]

select version();
PostgreSQL 9.0.1 on i686-pc-linux-gnu, compiled by GCC
i686-pc-linux-gnu-gcc (Gentoo 4.4.2 p1.0) 4.4.2, 32-bit
(1 row)

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?)

Does this mean this is already a recognized flaw or it was unknown at
the time if the feature was implemented? I would definitely would not
expect the argument to duplicated. I know there is big patch for
pl/python being reviewed right now. Perhaps this is being fixed? It
would be wonderful for plpython to support complex arguments
gracefully.

--
Thanks,
Nate Carson

#2Jan Urbański
wulczer@wulczer.org
In reply to: Nate C (#1)
Re: Bug? Unexpected argument handling in pl-python variadic argument function

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