From db0af74af28663e060e8c5d6f8dd0927bb572f25 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sat, 29 Sep 2012 12:41:00 -0400 Subject: [PATCH] PL/Python: Convert oid to long/int oid is a numeric type, so transform it to the appropriate Python numeric type like the other ones. --- doc/src/sgml/plpython.sgml | 2 +- src/pl/plpython/expected/plpython_types.out | 28 +++++++++++++++++++ src/pl/plpython/expected/plpython_types_3.out | 28 +++++++++++++++++++ src/pl/plpython/plpy_typeio.c | 10 +++++++ src/pl/plpython/sql/plpython_types.sql | 10 +++++++ 5 files changed, 77 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/plpython.sgml b/doc/src/sgml/plpython.sgml index c15188c3e0..dd50c4475d 100644 --- a/doc/src/sgml/plpython.sgml +++ b/doc/src/sgml/plpython.sgml @@ -302,7 +302,7 @@ $$ LANGUAGE plpythonu; PostgreSQL smallint and int are converted to Python int. - PostgreSQL bigint is converted + PostgreSQL bigint and oid are converted to long in Python 2 and to int in Python 3. diff --git a/src/pl/plpython/expected/plpython_types.out b/src/pl/plpython/expected/plpython_types.out index 888161323d..46413455c8 100644 --- a/src/pl/plpython/expected/plpython_types.out +++ b/src/pl/plpython/expected/plpython_types.out @@ -321,6 +321,34 @@ CONTEXT: PL/Python function "test_type_conversion_float8" (1 row) +CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$ +plpy.info(x, type(x)) +return x +$$ LANGUAGE plpythonu; +SELECT * FROM test_type_conversion_oid(100); +INFO: (100L, ) +CONTEXT: PL/Python function "test_type_conversion_oid" + test_type_conversion_oid +-------------------------- + 100 +(1 row) + +SELECT * FROM test_type_conversion_oid(2147483649); +INFO: (2147483649L, ) +CONTEXT: PL/Python function "test_type_conversion_oid" + test_type_conversion_oid +-------------------------- + 2147483649 +(1 row) + +SELECT * FROM test_type_conversion_oid(null); +INFO: (None, ) +CONTEXT: PL/Python function "test_type_conversion_oid" + test_type_conversion_oid +-------------------------- + +(1 row) + CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$ plpy.info(x, type(x)) return x diff --git a/src/pl/plpython/expected/plpython_types_3.out b/src/pl/plpython/expected/plpython_types_3.out index d1ae86377b..511ef5a4c9 100644 --- a/src/pl/plpython/expected/plpython_types_3.out +++ b/src/pl/plpython/expected/plpython_types_3.out @@ -321,6 +321,34 @@ CONTEXT: PL/Python function "test_type_conversion_float8" (1 row) +CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$ +plpy.info(x, type(x)) +return x +$$ LANGUAGE plpython3u; +SELECT * FROM test_type_conversion_oid(100); +INFO: (100, ) +CONTEXT: PL/Python function "test_type_conversion_oid" + test_type_conversion_oid +-------------------------- + 100 +(1 row) + +SELECT * FROM test_type_conversion_oid(2147483649); +INFO: (2147483649, ) +CONTEXT: PL/Python function "test_type_conversion_oid" + test_type_conversion_oid +-------------------------- + 2147483649 +(1 row) + +SELECT * FROM test_type_conversion_oid(null); +INFO: (None, ) +CONTEXT: PL/Python function "test_type_conversion_oid" + test_type_conversion_oid +-------------------------- + +(1 row) + CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$ plpy.info(x, type(x)) return x diff --git a/src/pl/plpython/plpy_typeio.c b/src/pl/plpython/plpy_typeio.c index 2402c151a4..0ad542f21e 100644 --- a/src/pl/plpython/plpy_typeio.c +++ b/src/pl/plpython/plpy_typeio.c @@ -39,6 +39,7 @@ static PyObject *PLyFloat_FromNumeric(PLyDatumToOb *arg, Datum d); static PyObject *PLyInt_FromInt16(PLyDatumToOb *arg, Datum d); static PyObject *PLyInt_FromInt32(PLyDatumToOb *arg, Datum d); static PyObject *PLyLong_FromInt64(PLyDatumToOb *arg, Datum d); +static PyObject *PLyLong_FromOid(PLyDatumToOb *arg, Datum d); static PyObject *PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d); static PyObject *PLyString_FromDatum(PLyDatumToOb *arg, Datum d); static PyObject *PLyList_FromArray(PLyDatumToOb *arg, Datum d); @@ -460,6 +461,9 @@ PLy_input_datum_func2(PLyDatumToOb *arg, Oid typeOid, HeapTuple typeTup) case INT8OID: arg->func = PLyLong_FromInt64; break; + case OIDOID: + arg->func = PLyLong_FromOid; + break; case BYTEAOID: arg->func = PLyBytes_FromBytea; break; @@ -546,6 +550,12 @@ PLyLong_FromInt64(PLyDatumToOb *arg, Datum d) return PyLong_FromLong(DatumGetInt64(d)); } +static PyObject * +PLyLong_FromOid(PLyDatumToOb *arg, Datum d) +{ + return PyLong_FromUnsignedLong(DatumGetObjectId(d)); +} + static PyObject * PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d) { diff --git a/src/pl/plpython/sql/plpython_types.sql b/src/pl/plpython/sql/plpython_types.sql index 82cd9d4268..6a50b4236d 100644 --- a/src/pl/plpython/sql/plpython_types.sql +++ b/src/pl/plpython/sql/plpython_types.sql @@ -119,6 +119,16 @@ SELECT * FROM test_type_conversion_float8(5000000000.5); SELECT * FROM test_type_conversion_float8(null); +CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$ +plpy.info(x, type(x)) +return x +$$ LANGUAGE plpythonu; + +SELECT * FROM test_type_conversion_oid(100); +SELECT * FROM test_type_conversion_oid(2147483649); +SELECT * FROM test_type_conversion_oid(null); + + CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$ plpy.info(x, type(x)) return x