plpython returns integer[] fails for multi-dimensional array

Started by TJ O'Donnellover 15 years ago6 messagesgeneral
Jump to latest
#1TJ O'Donnell
tjo@acm.org

In postgresql-9.0.1 I have to modify my plpython functions that return arrays.
It seems one dimesional arrays are handled properly, but not
2-dimensional arrays.

create or replace function atest() returns integer[] as $eopy$
a = list()
a.append(1)
a.append(2)
a.append(3)
#return a works fine
b = list()
b.append(a)
b.append(a)
# error
return b
$eopy$ language plpythonu

select atest() gives
obtest=# select atest();
ERROR: invalid input syntax for integer: "[1, 2, 3]"
CONTEXT: while creating return value
PL/Python function "atest"

How can I return multi-dimensional arrays in plpython?

TJ O'Donnell

#2Thom Brown
thom@linux.com
In reply to: TJ O'Donnell (#1)
Re: plpython returns integer[] fails for multi-dimensional array

On 21 December 2010 22:48, TJ O'Donnell <tjo@acm.org> wrote:

In postgresql-9.0.1 I have to modify my plpython functions that return arrays.
It seems one dimesional arrays are handled properly, but not
2-dimensional arrays.

create or replace function atest() returns integer[] as $eopy$
 a = list()
 a.append(1)
 a.append(2)
 a.append(3)
 #return a works fine
 b = list()
 b.append(a)
 b.append(a)
 # error
 return b
$eopy$ language plpythonu

select atest() gives
obtest=# select atest();
ERROR:  invalid input syntax for integer: "[1, 2, 3]"
CONTEXT:  while creating return value
PL/Python function "atest"

How can I return multi-dimensional arrays in plpython?

Are you sure that "a" returns okay in that scenario. You're using a
list. Shouldn't you be using an array? Like: a = []

--
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

In reply to: Thom Brown (#2)
Re: plpython returns integer[] fails for multi-dimensional array

On 21 December 2010 23:17, Thom Brown <thom@linux.com> wrote:

Are you sure that "a" returns okay in that scenario. You're using a
list. Shouldn't you be using an array? Like: a = []

a =[] actually declares an empty list in Python. You can return a list
or a tuple from a pl/python function in 9.0 and it will be interpreted
as an array at the SQL call site. You cannot in prior versions.

--
Regards,
Peter Geoghegan

#4Adrian Klaver
adrian.klaver@aklaver.com
In reply to: TJ O'Donnell (#1)
Re: plpython returns integer[] fails for multi-dimensional array

On Tuesday 21 December 2010 2:48:16 pm TJ O'Donnell wrote:

In postgresql-9.0.1 I have to modify my plpython functions that return
arrays. It seems one dimesional arrays are handled properly, but not
2-dimensional arrays.

create or replace function atest() returns integer[] as $eopy$
a = list()
a.append(1)
a.append(2)
a.append(3)
#return a works fine
b = list()
b.append(a)
b.append(a)
# error
return b
$eopy$ language plpythonu

select atest() gives
obtest=# select atest();
ERROR: invalid input syntax for integer: "[1, 2, 3]"
CONTEXT: while creating return value
PL/Python function "atest"

How can I return multi-dimensional arrays in plpython?

TJ O'Donnell

Maybe:
create or replace function atest() returns integer[][]

--
Adrian Klaver
adrian.klaver@gmail.com

#5Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Peter Geoghegan (#3)
Re: plpython returns integer[] fails for multi-dimensional array

On Tuesday 21 December 2010 3:25:48 pm Peter Geoghegan wrote:

On 21 December 2010 23:17, Thom Brown <thom@linux.com> wrote:

Are you sure that "a" returns okay in that scenario. You're using a
list. Shouldn't you be using an array? Like: a = []

a =[] actually declares an empty list in Python. You can return a list
or a tuple from a pl/python function in 9.0 and it will be interpreted
as an array at the SQL call site. You cannot in prior versions.

--
Regards,
Peter Geoghegan

Digging into the source for plpython seems to show it only supports one
dimensional arrays. When I tried my previous example on a 9.0.1 instance it
kept changing integer[][] to integer[].

--
Adrian Klaver
adrian.klaver@gmail.com

#6TJ O'Donnell
tjo@acm.org
In reply to: Adrian Klaver (#5)
Re: plpython returns integer[] fails for multi-dimensional array

In previous versions (8.x) for plpython fn returning integer[]
I created (had to create) a string in the proper SQL format {
{1,2,3}, {4,5,6} }
and returned that. It worked fine.

I LIKE the ability to not have to do that in 9.0
but I CAN'T return and string like { {1,2,3}, {4,5,6} } for a fn that
returns integer[]
AND I can't return a two-dimensional array. Not a happy 9.0 camper.

Anyone know of any plans to 9.0 plpython to support multi-dimensional arrays?

TJ O'Donnell

Show quoted text

On Tue, Dec 21, 2010 at 4:02 PM, Adrian Klaver <adrian.klaver@gmail.com> wrote:

On Tuesday 21 December 2010 3:25:48 pm Peter Geoghegan wrote:

On 21 December 2010 23:17, Thom Brown <thom@linux.com> wrote:

Are you sure that "a" returns okay in that scenario.  You're using a
list.  Shouldn't you be using an array?  Like: a = []

a =[] actually declares an empty list in Python. You can return a list
or a tuple from a pl/python function in 9.0 and it will be interpreted
as an array at the SQL call site. You cannot in prior versions.

--
Regards,
Peter Geoghegan

Digging into the source for plpython seems to show it only supports one
dimensional arrays. When I tried my previous example on a 9.0.1 instance it
kept changing integer[][] to integer[].

--
Adrian Klaver
adrian.klaver@gmail.com