Обсуждение: Patch to add support for text/int arrays for plpythonu

Поиск
Список
Период
Сортировка

Patch to add support for text/int arrays for plpythonu

От
"Valtonen, Hannu"
Дата:
Hi,

Attached is a small patch for adding support for INT[] and TEXT[] to
plpythonu.
There are also some small tests for this copypasted to the end of the mail.

I'd like to get this into CVS but as it's probably a bit late for 8.4 I
wouldn't mind it going into the first commitfest for 8.5.

- Hannu


---SELECT test_int_array[3] FROM return_int_array(ARRAY[1,2,3]) AS
test_int_array;
CREATE OR REPLACE FUNCTION return_int_array(int_array INT[])
RETURNS int[]
AS $$
plpy.info("Type: %s %r" % (type(int_array), int_array))
return int_array
$$ LANGUAGE plpythonu;

---SELECT jee[3] FROM return_text_array(ARRAY['test1','test2','test3'])
AS test_text_array;
CREATE OR REPLACE FUNCTION return_text_array(text_array TEXT[])
RETURNS TEXT[]
AS $$
plpy.info("Type: %s %r" % (type(text_array), text_array))
return text_array
$$ LANGUAGE plpythonu;

--CREATE TABLE test_table (id SERIAL PRIMARY KEY, bar INT[], bar2 TEXT[]);
--SELECT * FROM test_text_array_plan(ARRAY['test1', 'test2']);
CREATE OR REPLACE FUNCTION test_text_array_plan(test_array TEXT[])
RETURNS void
AS $$
plan = plpy.prepare("INSERT INTO test_table (bar) VALUES ($1)", ["TEXT[]"])
results = plpy.execute(plan, [test_array])
$$ LANGUAGE plpythonu;

--SELECT * FROM test_int_array_plan(ARRAY[1,2,3,4,5,6,7,8,9,10]);
CREATE OR REPLACE FUNCTION test_int_array_plan(test_array INT[])
RETURNS void
AS $$
plan = plpy.prepare("INSERT INTO test_table (bar2) VALUES ($1)", ["INT[]"])
results = plpy.execute(plan, [test_array])
$$ LANGUAGE plpythonu;


Вложения

Re: Patch to add support for text/int arrays for plpythonu

От
Tom Lane
Дата:
"Valtonen, Hannu" <hannu.valtonen@hut.fi> writes:
> Attached is a small patch for adding support for INT[] and TEXT[] to 
> plpythonu.

Why in the world would you confine the feature to just two data types?
This seems like a fundamentally incorrect approach.
        regards, tom lane


Re: Patch to add support for text/int arrays for plpythonu

От
"Valtonen, Hannu"
Дата:
On 4/26/09 6:23 PM, Tom Lane wrote:
> Why in the world would you confine the feature to just two data types?
> This seems like a fundamentally incorrect approach.
>
>    
The reason why I wrote it that way is because that's the way plpythonu's 
conversion to python and back is set up.

Without my patch it currently knows how to convert BOOLOID,  FLOAT4OID,  
FLOAT8OID,  NUMERICOID,  INT2OID,  INT4OID, INT8OID from pgsql form to 
python form and back again as function return types. All the rest of the 
pgsql types are converted into python strings with 1:1 textual 
representation from pgsql. (This includes all array types)

What my patch basically changes is that instead of making an INT4/TEXT 
array a python string, it converts them to python list()'s with 
corresponding types. The rest are still being converted into python 
strings. So I started with INT[] and TEXT[] because those were the ones 
I actually needed myself but extending conversion support to for example 
to FLOATs/NUMERICs wouldn't be that hard.

It also basically corresponds with postgresql/src/pl/plpython/TODO's:
"* Allow arrays as function arguments and return values.  (almost done)"

- Hannu