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
This commit is contained in:
Heikki Linnakangas 2013-11-26 14:22:38 +02:00
parent 7cc0ba9f17
commit 37364c6311
4 changed files with 89 additions and 4 deletions

View File

@ -664,6 +664,34 @@ SELECT * FROM test_type_conversion_array_error();
ERROR: return value of function with array return type is not a Python sequence ERROR: return value of function with array return type is not a Python sequence
CONTEXT: while creating return value CONTEXT: while creating return value
PL/Python function "test_type_conversion_array_error" PL/Python function "test_type_conversion_array_error"
CREATE DOMAIN ordered_pair_domain AS integer[] CHECK (array_length(VALUE,1)=2 AND VALUE[1] < VALUE[2]);
CREATE FUNCTION test_type_conversion_array_domain(x ordered_pair_domain) RETURNS ordered_pair_domain AS $$
plpy.info(x, type(x))
return x
$$ LANGUAGE plpythonu;
SELECT * FROM test_type_conversion_array_domain(ARRAY[0, 100]::ordered_pair_domain);
INFO: ([0, 100], <type 'list'>)
CONTEXT: PL/Python function "test_type_conversion_array_domain"
test_type_conversion_array_domain
-----------------------------------
{0,100}
(1 row)
SELECT * FROM test_type_conversion_array_domain(NULL::ordered_pair_domain);
INFO: (None, <type 'NoneType'>)
CONTEXT: PL/Python function "test_type_conversion_array_domain"
test_type_conversion_array_domain
-----------------------------------
(1 row)
CREATE FUNCTION test_type_conversion_array_domain_check_violation() RETURNS ordered_pair_domain AS $$
return [2,1]
$$ LANGUAGE plpythonu;
SELECT * FROM test_type_conversion_array_domain_check_violation();
ERROR: value for domain ordered_pair_domain violates check constraint "ordered_pair_domain_check"
CONTEXT: while creating return value
PL/Python function "test_type_conversion_array_domain_check_violation"
--- ---
--- Composite types --- Composite types
--- ---

View File

@ -664,6 +664,34 @@ SELECT * FROM test_type_conversion_array_error();
ERROR: return value of function with array return type is not a Python sequence ERROR: return value of function with array return type is not a Python sequence
CONTEXT: while creating return value CONTEXT: while creating return value
PL/Python function "test_type_conversion_array_error" PL/Python function "test_type_conversion_array_error"
CREATE DOMAIN ordered_pair_domain AS integer[] CHECK (array_length(VALUE,1)=2 AND VALUE[1] < VALUE[2]);
CREATE FUNCTION test_type_conversion_array_domain(x ordered_pair_domain) RETURNS ordered_pair_domain AS $$
plpy.info(x, type(x))
return x
$$ LANGUAGE plpython3u;
SELECT * FROM test_type_conversion_array_domain(ARRAY[0, 100]::ordered_pair_domain);
INFO: ([0, 100], <class 'list'>)
CONTEXT: PL/Python function "test_type_conversion_array_domain"
test_type_conversion_array_domain
-----------------------------------
{0,100}
(1 row)
SELECT * FROM test_type_conversion_array_domain(NULL::ordered_pair_domain);
INFO: (None, <class 'NoneType'>)
CONTEXT: PL/Python function "test_type_conversion_array_domain"
test_type_conversion_array_domain
-----------------------------------
(1 row)
CREATE FUNCTION test_type_conversion_array_domain_check_violation() RETURNS ordered_pair_domain AS $$
return [2,1]
$$ LANGUAGE plpython3u;
SELECT * FROM test_type_conversion_array_domain_check_violation();
ERROR: value for domain ordered_pair_domain violates check constraint "ordered_pair_domain_check"
CONTEXT: while creating return value
PL/Python function "test_type_conversion_array_domain_check_violation"
--- ---
--- Composite types --- Composite types
--- ---

View File

@ -373,7 +373,7 @@ PLy_output_datum_func2(PLyObToDatum *arg, HeapTuple typeTup)
arg->typioparam = getTypeIOParam(typeTup); arg->typioparam = getTypeIOParam(typeTup);
arg->typbyval = typeStruct->typbyval; arg->typbyval = typeStruct->typbyval;
element_type = get_element_type(arg->typoid); element_type = get_base_element_type(arg->typoid);
/* /*
* Select a conversion function to convert Python objects to PostgreSQL * Select a conversion function to convert Python objects to PostgreSQL
@ -427,7 +427,8 @@ static void
PLy_input_datum_func2(PLyDatumToOb *arg, Oid typeOid, HeapTuple typeTup) PLy_input_datum_func2(PLyDatumToOb *arg, Oid typeOid, HeapTuple typeTup)
{ {
Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup); Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
Oid element_type = get_element_type(typeOid); /* It's safe to handle domains of array types as its base array type. */
Oid element_type = get_base_element_type(typeOid);
/* Get the type's conversion information */ /* Get the type's conversion information */
perm_fmgr_info(typeStruct->typoutput, &arg->typfunc); perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
@ -808,6 +809,7 @@ static Datum
PLySequence_ToArray(PLyObToDatum *arg, int32 typmod, PyObject *plrv) PLySequence_ToArray(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
{ {
ArrayType *array; ArrayType *array;
Datum rv;
int i; int i;
Datum *elems; Datum *elems;
bool *nulls; bool *nulls;
@ -844,8 +846,15 @@ PLySequence_ToArray(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
lbs = 1; lbs = 1;
array = construct_md_array(elems, nulls, 1, &len, &lbs, array = construct_md_array(elems, nulls, 1, &len, &lbs,
get_element_type(arg->typoid), arg->elm->typlen, arg->elm->typbyval, arg->elm->typalign); get_base_element_type(arg->typoid), arg->elm->typlen, arg->elm->typbyval, arg->elm->typalign);
return PointerGetDatum(array); /*
* If the result type is a domain of array, the resulting array must be
* checked.
*/
rv = PointerGetDatum(array);
if (get_typtype(arg->typoid) == TYPTYPE_DOMAIN)
domain_check(rv, false, arg->typoid, &arg->typfunc.fn_extra, arg->typfunc.fn_mcxt);
return rv;
} }

View File

@ -294,6 +294,26 @@ $$ LANGUAGE plpythonu;
SELECT * FROM test_type_conversion_array_error(); SELECT * FROM test_type_conversion_array_error();
--
-- Domains over arrays
--
CREATE DOMAIN ordered_pair_domain AS integer[] CHECK (array_length(VALUE,1)=2 AND VALUE[1] < VALUE[2]);
CREATE FUNCTION test_type_conversion_array_domain(x ordered_pair_domain) RETURNS ordered_pair_domain AS $$
plpy.info(x, type(x))
return x
$$ LANGUAGE plpythonu;
SELECT * FROM test_type_conversion_array_domain(ARRAY[0, 100]::ordered_pair_domain);
SELECT * FROM test_type_conversion_array_domain(NULL::ordered_pair_domain);
CREATE FUNCTION test_type_conversion_array_domain_check_violation() RETURNS ordered_pair_domain AS $$
return [2,1]
$$ LANGUAGE plpythonu;
SELECT * FROM test_type_conversion_array_domain_check_violation();
--- ---
--- Composite types --- Composite types
--- ---