Re: PL/Python array support

Lists: pgsql-hackers
From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: PL/Python array support
Date: 2009-11-04 14:02:08
Message-ID: 1257343328.18382.18.camel@fsopti579.F-Secure.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Here is a patch to support arrays in PL/Python as parameters and return
values. It converts an array parameter to a Python "list", and converts
a Python "sequence" return value back to an array.

I have settled on two implementation restrictions for the moment:

- Only supports one-dimensional arrays. (Python has no multidimensional
lists, so the semantics of this would be dubious.)

- Does not support returning arrays of composite types. (Basically too
complicated to implement right now and seemingly of limited practical
value.)

Attachment Content-Type Size
plpython-arrays.diff text/x-patch 12.2 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: PL/Python array support
Date: 2009-11-04 14:44:10
Message-ID: 603c8f070911040644q36b4e2bcpb0918d0bdfdd0107@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Nov 4, 2009 at 9:02 AM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> Here is a patch to support arrays in PL/Python as parameters and return
> values.  It converts an array parameter to a Python "list", and converts
> a Python "sequence" return value back to an array.

This is probably a stupid question, but why would you use different
types for incoming and outgoing data flow?

...Robert


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: PL/Python array support
Date: 2009-11-04 16:06:38
Message-ID: 1257350798.12075.0.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On ons, 2009-11-04 at 09:44 -0500, Robert Haas wrote:
> On Wed, Nov 4, 2009 at 9:02 AM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> > Here is a patch to support arrays in PL/Python as parameters and return
> > values. It converts an array parameter to a Python "list", and converts
> > a Python "sequence" return value back to an array.
>
> This is probably a stupid question, but why would you use different
> types for incoming and outgoing data flow?

A list is one particular kind of sequence.

See also
http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: PL/Python array support
Date: 2009-11-12 15:28:57
Message-ID: 1258039737.24453.0.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On ons, 2009-11-04 at 16:02 +0200, Peter Eisentraut wrote:
> Here is a patch to support arrays in PL/Python as parameters and
> return values.

Slightly updated version with fixed reference counting.

Attachment Content-Type Size
plpython-arrays.patch text/x-patch 12.3 KB

From: Teodor Sigaev <teodor(at)sigaev(dot)ru>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: PL/Python array support
Date: 2009-11-13 15:46:42
Message-ID: 4AFD7F62.3010901@sigaev.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


CREATE OR REPLACE FUNCTION incr(stuff int[]) RETURNS int[] AS $$
for x in stuff:
yield x+1
$$
LANGUAGE 'plpythonu';

# select incr(ARRAY[1,2,3]);
ERROR: invalid memory alloc request size 18446744073709551608
CONTEXT: while creating return value
PL/Python function "incr"

Suppose, it could be fixed by additional check in PLy_function_handler near line
947 :
if (proc->is_setof) {
...
}
else if (PyIter_Check(plrv))
{
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("returned object should be iterated"),
errdetail("PL/Python returns iterable object in non-setof returning
context")));
}

Peter Eisentraut wrote:
> On ons, 2009-11-04 at 16:02 +0200, Peter Eisentraut wrote:
>> Here is a patch to support arrays in PL/Python as parameters and
>> return values.
>
> Slightly updated version with fixed reference counting.
>
>
> ------------------------------------------------------------------------
>
>

--
Teodor Sigaev E-mail: teodor(at)sigaev(dot)ru
WWW: http://www.sigaev.ru/


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Teodor Sigaev <teodor(at)sigaev(dot)ru>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: PL/Python array support
Date: 2009-11-19 22:00:24
Message-ID: 1258668024.26726.16.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On fre, 2009-11-13 at 18:46 +0300, Teodor Sigaev wrote:
> CREATE OR REPLACE FUNCTION incr(stuff int[]) RETURNS int[] AS $$
> for x in stuff:
> yield x+1
> $$
> LANGUAGE 'plpythonu';
>
> # select incr(ARRAY[1,2,3]);
> ERROR: invalid memory alloc request size 18446744073709551608
> CONTEXT: while creating return value
> PL/Python function "incr"

Fixed with additional error check and regression test. (The problem
could be more simply demonstrated by returning any non-sequence from the
function.) Thanks for catching it.

Attachment Content-Type Size
plpython-arrays.patch text/x-patch 12.5 KB

From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: PL/Python array support
Date: 2009-12-02 03:53:49
Message-ID: 20091202035349.GA20931@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 20, 2009 at 12:00:24AM +0200, Peter Eisentraut wrote:
> On fre, 2009-11-13 at 18:46 +0300, Teodor Sigaev wrote:
> > CREATE OR REPLACE FUNCTION incr(stuff int[]) RETURNS int[] AS $$
> > for x in stuff:
> > yield x+1
> > $$
> > LANGUAGE 'plpythonu';
> >
> > # select incr(ARRAY[1,2,3]);
> > ERROR: invalid memory alloc request size 18446744073709551608
> > CONTEXT: while creating return value
> > PL/Python function "incr"
>
> Fixed with additional error check and regression test. (The problem
> could be more simply demonstrated by returning any non-sequence from the
> function.) Thanks for catching it.

I've finally gotten around to getting a review done, and basically it looks
good to me. There appears to be a problem with the tests in that the expected
output doesn't include the test_type_conversion_array_error() function
mentioned in sql/plpython_types.sql. Diff generated by the regression test is
attached. Other than that problem, though, the code looks fine to me (should I
presume to judge Peter's code? :). Aside from the problem mentioned above, the
tests work fine, and seem fairly comprehensive. Other testing I've done also
passes.

This patch doesn't include any documentation; my reading of the PL/Python docs
suggests that's probably acceptable, as the existing docs don't talk about its
array handling. That said, it might be useful to include an example, to show
for instance that identical PL/Python code could create either an array of a
type or a set of rows of that type:

5432 josh(at)josh*# create function return_set() returns setof int as $$ return
(1, 2, 3, 4, 5) $$ language plpythonu;
CREATE FUNCTION
5432 josh(at)josh*# create function return_arr() returns int[] as $$ return (1,
2, 3, 4, 5) $$ language plpythonu;
CREATE FUNCTION
5432 josh(at)josh*# select return_arr();
return_arr
-------------
{1,2,3,4,5}
(1 row)

5432 josh(at)josh*# select * from return_set();
return_set
------------
1
2
3
4
5
(5 rows)

Perhaps that's overkill, though.

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com


From: Joshua Tolley <eggyknap(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Teodor Sigaev <teodor(at)sigaev(dot)ru>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: PL/Python array support
Date: 2009-12-02 12:11:56
Message-ID: 20091202121156.GC20931@eddie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Nov 20, 2009 at 12:00:24AM +0200, Peter Eisentraut wrote:
> On fre, 2009-11-13 at 18:46 +0300, Teodor Sigaev wrote:
> > CREATE OR REPLACE FUNCTION incr(stuff int[]) RETURNS int[] AS $$
> > for x in stuff:
> > yield x+1
> > $$
> > LANGUAGE 'plpythonu';
> >
> > # select incr(ARRAY[1,2,3]);
> > ERROR: invalid memory alloc request size 18446744073709551608
> > CONTEXT: while creating return value
> > PL/Python function "incr"
>
> Fixed with additional error check and regression test. (The problem
> could be more simply demonstrated by returning any non-sequence from the
> function.) Thanks for catching it.

My last email claimed that the regression test needed some additional changes
to its expected output, and further claimed that it had the regression test's
diff attached. As was helpfully pointed out off-list, it actually wasn't
attached. Trying again..

-- Josh

Attachment Content-Type Size
regression.diffs text/plain 723 bytes

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Joshua Tolley <eggyknap(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: PL/Python array support
Date: 2009-12-10 20:44:50
Message-ID: 1260477890.716.8.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On tis, 2009-12-01 at 20:53 -0700, Joshua Tolley wrote:
> This patch doesn't include any documentation; my reading of the PL/Python docs
> suggests that's probably acceptable, as the existing docs don't talk about its
> array handling. That said, it might be useful to include an example, to show
> for instance that identical PL/Python code could create either an array of a
> type or a set of rows of that type:

I added a bit of documentation like that. Thanks.