Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 37364c6

Browse files
committed
Handle domains over arrays like plain arrays in PL/python.
Domains over arrays are now converted to/from python lists when passed as arguments or return values. Like regular arrays. This has some potential to break applications that rely on the old behavior that they are passed as strings, but in practice there probably aren't many such applications out there. Rodolfo Campero
1 parent 7cc0ba9 commit 37364c6

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

src/pl/plpython/expected/plpython_types.out

+28
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,34 @@ SELECT * FROM test_type_conversion_array_error();
664664
ERROR: return value of function with array return type is not a Python sequence
665665
CONTEXT: while creating return value
666666
PL/Python function "test_type_conversion_array_error"
667+
CREATE DOMAIN ordered_pair_domain AS integer[] CHECK (array_length(VALUE,1)=2 AND VALUE[1] < VALUE[2]);
668+
CREATE FUNCTION test_type_conversion_array_domain(x ordered_pair_domain) RETURNS ordered_pair_domain AS $$
669+
plpy.info(x, type(x))
670+
return x
671+
$$ LANGUAGE plpythonu;
672+
SELECT * FROM test_type_conversion_array_domain(ARRAY[0, 100]::ordered_pair_domain);
673+
INFO: ([0, 100], <type 'list'>)
674+
CONTEXT: PL/Python function "test_type_conversion_array_domain"
675+
test_type_conversion_array_domain
676+
-----------------------------------
677+
{0,100}
678+
(1 row)
679+
680+
SELECT * FROM test_type_conversion_array_domain(NULL::ordered_pair_domain);
681+
INFO: (None, <type 'NoneType'>)
682+
CONTEXT: PL/Python function "test_type_conversion_array_domain"
683+
test_type_conversion_array_domain
684+
-----------------------------------
685+
686+
(1 row)
687+
688+
CREATE FUNCTION test_type_conversion_array_domain_check_violation() RETURNS ordered_pair_domain AS $$
689+
return [2,1]
690+
$$ LANGUAGE plpythonu;
691+
SELECT * FROM test_type_conversion_array_domain_check_violation();
692+
ERROR: value for domain ordered_pair_domain violates check constraint "ordered_pair_domain_check"
693+
CONTEXT: while creating return value
694+
PL/Python function "test_type_conversion_array_domain_check_violation"
667695
---
668696
--- Composite types
669697
---

src/pl/plpython/expected/plpython_types_3.out

+28
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,34 @@ SELECT * FROM test_type_conversion_array_error();
664664
ERROR: return value of function with array return type is not a Python sequence
665665
CONTEXT: while creating return value
666666
PL/Python function "test_type_conversion_array_error"
667+
CREATE DOMAIN ordered_pair_domain AS integer[] CHECK (array_length(VALUE,1)=2 AND VALUE[1] < VALUE[2]);
668+
CREATE FUNCTION test_type_conversion_array_domain(x ordered_pair_domain) RETURNS ordered_pair_domain AS $$
669+
plpy.info(x, type(x))
670+
return x
671+
$$ LANGUAGE plpython3u;
672+
SELECT * FROM test_type_conversion_array_domain(ARRAY[0, 100]::ordered_pair_domain);
673+
INFO: ([0, 100], <class 'list'>)
674+
CONTEXT: PL/Python function "test_type_conversion_array_domain"
675+
test_type_conversion_array_domain
676+
-----------------------------------
677+
{0,100}
678+
(1 row)
679+
680+
SELECT * FROM test_type_conversion_array_domain(NULL::ordered_pair_domain);
681+
INFO: (None, <class 'NoneType'>)
682+
CONTEXT: PL/Python function "test_type_conversion_array_domain"
683+
test_type_conversion_array_domain
684+
-----------------------------------
685+
686+
(1 row)
687+
688+
CREATE FUNCTION test_type_conversion_array_domain_check_violation() RETURNS ordered_pair_domain AS $$
689+
return [2,1]
690+
$$ LANGUAGE plpython3u;
691+
SELECT * FROM test_type_conversion_array_domain_check_violation();
692+
ERROR: value for domain ordered_pair_domain violates check constraint "ordered_pair_domain_check"
693+
CONTEXT: while creating return value
694+
PL/Python function "test_type_conversion_array_domain_check_violation"
667695
---
668696
--- Composite types
669697
---

src/pl/plpython/plpy_typeio.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ PLy_output_datum_func2(PLyObToDatum *arg, HeapTuple typeTup)
373373
arg->typioparam = getTypeIOParam(typeTup);
374374
arg->typbyval = typeStruct->typbyval;
375375

376-
element_type = get_element_type(arg->typoid);
376+
element_type = get_base_element_type(arg->typoid);
377377

378378
/*
379379
* Select a conversion function to convert Python objects to PostgreSQL
@@ -427,7 +427,8 @@ static void
427427
PLy_input_datum_func2(PLyDatumToOb *arg, Oid typeOid, HeapTuple typeTup)
428428
{
429429
Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
430-
Oid element_type = get_element_type(typeOid);
430+
/* It's safe to handle domains of array types as its base array type. */
431+
Oid element_type = get_base_element_type(typeOid);
431432

432433
/* Get the type's conversion information */
433434
perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
@@ -808,6 +809,7 @@ static Datum
808809
PLySequence_ToArray(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
809810
{
810811
ArrayType *array;
812+
Datum rv;
811813
int i;
812814
Datum *elems;
813815
bool *nulls;
@@ -844,8 +846,15 @@ PLySequence_ToArray(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
844846

845847
lbs = 1;
846848
array = construct_md_array(elems, nulls, 1, &len, &lbs,
847-
get_element_type(arg->typoid), arg->elm->typlen, arg->elm->typbyval, arg->elm->typalign);
848-
return PointerGetDatum(array);
849+
get_base_element_type(arg->typoid), arg->elm->typlen, arg->elm->typbyval, arg->elm->typalign);
850+
/*
851+
* If the result type is a domain of array, the resulting array must be
852+
* checked.
853+
*/
854+
rv = PointerGetDatum(array);
855+
if (get_typtype(arg->typoid) == TYPTYPE_DOMAIN)
856+
domain_check(rv, false, arg->typoid, &arg->typfunc.fn_extra, arg->typfunc.fn_mcxt);
857+
return rv;
849858
}
850859

851860

src/pl/plpython/sql/plpython_types.sql

+20
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,26 @@ $$ LANGUAGE plpythonu;
294294
SELECT * FROM test_type_conversion_array_error();
295295

296296

297+
--
298+
-- Domains over arrays
299+
--
300+
301+
CREATE DOMAIN ordered_pair_domain AS integer[] CHECK (array_length(VALUE,1)=2 AND VALUE[1] < VALUE[2]);
302+
303+
CREATE FUNCTION test_type_conversion_array_domain(x ordered_pair_domain) RETURNS ordered_pair_domain AS $$
304+
plpy.info(x, type(x))
305+
return x
306+
$$ LANGUAGE plpythonu;
307+
308+
SELECT * FROM test_type_conversion_array_domain(ARRAY[0, 100]::ordered_pair_domain);
309+
SELECT * FROM test_type_conversion_array_domain(NULL::ordered_pair_domain);
310+
311+
CREATE FUNCTION test_type_conversion_array_domain_check_violation() RETURNS ordered_pair_domain AS $$
312+
return [2,1]
313+
$$ LANGUAGE plpythonu;
314+
SELECT * FROM test_type_conversion_array_domain_check_violation();
315+
316+
297317
---
298318
--- Composite types
299319
---

0 commit comments

Comments
 (0)