Skip to content

Commit db0af74

Browse files
committed
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.
1 parent 811ca13 commit db0af74

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

doc/src/sgml/plpython.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ $$ LANGUAGE plpythonu;
302302
<para>
303303
PostgreSQL <type>smallint</type> and <type>int</type> are
304304
converted to Python <type>int</type>.
305-
PostgreSQL <type>bigint</type> is converted
305+
PostgreSQL <type>bigint</type> and <type>oid</type> are converted
306306
to <type>long</type> in Python 2 and to <type>int</type> in
307307
Python 3.
308308
</para>

src/pl/plpython/expected/plpython_types.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,34 @@ CONTEXT: PL/Python function "test_type_conversion_float8"
321321

322322
(1 row)
323323

324+
CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$
325+
plpy.info(x, type(x))
326+
return x
327+
$$ LANGUAGE plpythonu;
328+
SELECT * FROM test_type_conversion_oid(100);
329+
INFO: (100L, <type 'long'>)
330+
CONTEXT: PL/Python function "test_type_conversion_oid"
331+
test_type_conversion_oid
332+
--------------------------
333+
100
334+
(1 row)
335+
336+
SELECT * FROM test_type_conversion_oid(2147483649);
337+
INFO: (2147483649L, <type 'long'>)
338+
CONTEXT: PL/Python function "test_type_conversion_oid"
339+
test_type_conversion_oid
340+
--------------------------
341+
2147483649
342+
(1 row)
343+
344+
SELECT * FROM test_type_conversion_oid(null);
345+
INFO: (None, <type 'NoneType'>)
346+
CONTEXT: PL/Python function "test_type_conversion_oid"
347+
test_type_conversion_oid
348+
--------------------------
349+
350+
(1 row)
351+
324352
CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
325353
plpy.info(x, type(x))
326354
return x

src/pl/plpython/expected/plpython_types_3.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,34 @@ CONTEXT: PL/Python function "test_type_conversion_float8"
321321

322322
(1 row)
323323

324+
CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$
325+
plpy.info(x, type(x))
326+
return x
327+
$$ LANGUAGE plpython3u;
328+
SELECT * FROM test_type_conversion_oid(100);
329+
INFO: (100, <class 'int'>)
330+
CONTEXT: PL/Python function "test_type_conversion_oid"
331+
test_type_conversion_oid
332+
--------------------------
333+
100
334+
(1 row)
335+
336+
SELECT * FROM test_type_conversion_oid(2147483649);
337+
INFO: (2147483649, <class 'int'>)
338+
CONTEXT: PL/Python function "test_type_conversion_oid"
339+
test_type_conversion_oid
340+
--------------------------
341+
2147483649
342+
(1 row)
343+
344+
SELECT * FROM test_type_conversion_oid(null);
345+
INFO: (None, <class 'NoneType'>)
346+
CONTEXT: PL/Python function "test_type_conversion_oid"
347+
test_type_conversion_oid
348+
--------------------------
349+
350+
(1 row)
351+
324352
CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
325353
plpy.info(x, type(x))
326354
return x

src/pl/plpython/plpy_typeio.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static PyObject *PLyFloat_FromNumeric(PLyDatumToOb *arg, Datum d);
3939
static PyObject *PLyInt_FromInt16(PLyDatumToOb *arg, Datum d);
4040
static PyObject *PLyInt_FromInt32(PLyDatumToOb *arg, Datum d);
4141
static PyObject *PLyLong_FromInt64(PLyDatumToOb *arg, Datum d);
42+
static PyObject *PLyLong_FromOid(PLyDatumToOb *arg, Datum d);
4243
static PyObject *PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d);
4344
static PyObject *PLyString_FromDatum(PLyDatumToOb *arg, Datum d);
4445
static PyObject *PLyList_FromArray(PLyDatumToOb *arg, Datum d);
@@ -460,6 +461,9 @@ PLy_input_datum_func2(PLyDatumToOb *arg, Oid typeOid, HeapTuple typeTup)
460461
case INT8OID:
461462
arg->func = PLyLong_FromInt64;
462463
break;
464+
case OIDOID:
465+
arg->func = PLyLong_FromOid;
466+
break;
463467
case BYTEAOID:
464468
arg->func = PLyBytes_FromBytea;
465469
break;
@@ -546,6 +550,12 @@ PLyLong_FromInt64(PLyDatumToOb *arg, Datum d)
546550
return PyLong_FromLong(DatumGetInt64(d));
547551
}
548552

553+
static PyObject *
554+
PLyLong_FromOid(PLyDatumToOb *arg, Datum d)
555+
{
556+
return PyLong_FromUnsignedLong(DatumGetObjectId(d));
557+
}
558+
549559
static PyObject *
550560
PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d)
551561
{

src/pl/plpython/sql/plpython_types.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ SELECT * FROM test_type_conversion_float8(5000000000.5);
119119
SELECT * FROM test_type_conversion_float8(null);
120120

121121

122+
CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$
123+
plpy.info(x, type(x))
124+
return x
125+
$$ LANGUAGE plpythonu;
126+
127+
SELECT * FROM test_type_conversion_oid(100);
128+
SELECT * FROM test_type_conversion_oid(2147483649);
129+
SELECT * FROM test_type_conversion_oid(null);
130+
131+
122132
CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
123133
plpy.info(x, type(x))
124134
return x

0 commit comments

Comments
 (0)