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

Commit 0c73808

Browse files
committed
PL/Python regression tests for data type handling
Add some checks on various data types are converted into and out of Python. This is extracted from Caleb Welton's patch for improved bytea support, but much expanded.
1 parent cf37d68 commit 0c73808

File tree

3 files changed

+537
-1
lines changed

3 files changed

+537
-1
lines changed

src/pl/plpython/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $PostgreSQL: pgsql/src/pl/plpython/Makefile,v 1.33 2009/08/12 16:37:25 petere Exp $
1+
# $PostgreSQL: pgsql/src/pl/plpython/Makefile,v 1.34 2009/08/14 13:42:16 petere Exp $
22

33
subdir = src/pl/plpython
44
top_builddir = ../../..
@@ -70,6 +70,7 @@ REGRESS = \
7070
plpython_setof \
7171
plpython_record \
7272
plpython_trigger \
73+
plpython_types \
7374
plpython_error \
7475
plpython_unicode \
7576
plpython_drop
+378
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
1+
--
2+
-- Test data type behavior
3+
--
4+
--
5+
-- Base/common types
6+
--
7+
CREATE FUNCTION test_type_conversion_bool(x bool) RETURNS bool AS $$
8+
plpy.info(x, type(x))
9+
return x
10+
$$ LANGUAGE plpythonu;
11+
SELECT * FROM test_type_conversion_bool(true);
12+
INFO: (True, <type 'bool'>)
13+
CONTEXT: PL/Python function "test_type_conversion_bool"
14+
test_type_conversion_bool
15+
---------------------------
16+
t
17+
(1 row)
18+
19+
SELECT * FROM test_type_conversion_bool(false);
20+
INFO: (False, <type 'bool'>)
21+
CONTEXT: PL/Python function "test_type_conversion_bool"
22+
test_type_conversion_bool
23+
---------------------------
24+
f
25+
(1 row)
26+
27+
SELECT * FROM test_type_conversion_bool(null);
28+
INFO: (None, <type 'NoneType'>)
29+
CONTEXT: PL/Python function "test_type_conversion_bool"
30+
test_type_conversion_bool
31+
---------------------------
32+
33+
(1 row)
34+
35+
CREATE FUNCTION test_type_conversion_char(x char) RETURNS char AS $$
36+
plpy.info(x, type(x))
37+
return x
38+
$$ LANGUAGE plpythonu;
39+
SELECT * FROM test_type_conversion_char('a');
40+
INFO: ('a', <type 'str'>)
41+
CONTEXT: PL/Python function "test_type_conversion_char"
42+
test_type_conversion_char
43+
---------------------------
44+
a
45+
(1 row)
46+
47+
SELECT * FROM test_type_conversion_char(null);
48+
INFO: (None, <type 'NoneType'>)
49+
CONTEXT: PL/Python function "test_type_conversion_char"
50+
test_type_conversion_char
51+
---------------------------
52+
53+
(1 row)
54+
55+
CREATE FUNCTION test_type_conversion_int2(x int2) RETURNS int2 AS $$
56+
plpy.info(x, type(x))
57+
return x
58+
$$ LANGUAGE plpythonu;
59+
SELECT * FROM test_type_conversion_int2(100::int2);
60+
INFO: (100, <type 'int'>)
61+
CONTEXT: PL/Python function "test_type_conversion_int2"
62+
test_type_conversion_int2
63+
---------------------------
64+
100
65+
(1 row)
66+
67+
SELECT * FROM test_type_conversion_int2(-100::int2);
68+
INFO: (-100, <type 'int'>)
69+
CONTEXT: PL/Python function "test_type_conversion_int2"
70+
test_type_conversion_int2
71+
---------------------------
72+
-100
73+
(1 row)
74+
75+
SELECT * FROM test_type_conversion_int2(null);
76+
INFO: (None, <type 'NoneType'>)
77+
CONTEXT: PL/Python function "test_type_conversion_int2"
78+
test_type_conversion_int2
79+
---------------------------
80+
81+
(1 row)
82+
83+
CREATE FUNCTION test_type_conversion_int4(x int4) RETURNS int4 AS $$
84+
plpy.info(x, type(x))
85+
return x
86+
$$ LANGUAGE plpythonu;
87+
SELECT * FROM test_type_conversion_int4(100);
88+
INFO: (100, <type 'int'>)
89+
CONTEXT: PL/Python function "test_type_conversion_int4"
90+
test_type_conversion_int4
91+
---------------------------
92+
100
93+
(1 row)
94+
95+
SELECT * FROM test_type_conversion_int4(-100);
96+
INFO: (-100, <type 'int'>)
97+
CONTEXT: PL/Python function "test_type_conversion_int4"
98+
test_type_conversion_int4
99+
---------------------------
100+
-100
101+
(1 row)
102+
103+
SELECT * FROM test_type_conversion_int4(null);
104+
INFO: (None, <type 'NoneType'>)
105+
CONTEXT: PL/Python function "test_type_conversion_int4"
106+
test_type_conversion_int4
107+
---------------------------
108+
109+
(1 row)
110+
111+
CREATE FUNCTION test_type_conversion_int8(x int8) RETURNS int8 AS $$
112+
plpy.info(x, type(x))
113+
return x
114+
$$ LANGUAGE plpythonu;
115+
SELECT * FROM test_type_conversion_int8(100);
116+
INFO: (100L, <type 'long'>)
117+
CONTEXT: PL/Python function "test_type_conversion_int8"
118+
test_type_conversion_int8
119+
---------------------------
120+
100
121+
(1 row)
122+
123+
SELECT * FROM test_type_conversion_int8(-100);
124+
INFO: (-100L, <type 'long'>)
125+
CONTEXT: PL/Python function "test_type_conversion_int8"
126+
test_type_conversion_int8
127+
---------------------------
128+
-100
129+
(1 row)
130+
131+
SELECT * FROM test_type_conversion_int8(5000000000);
132+
INFO: (5000000000L, <type 'long'>)
133+
CONTEXT: PL/Python function "test_type_conversion_int8"
134+
test_type_conversion_int8
135+
---------------------------
136+
5000000000
137+
(1 row)
138+
139+
SELECT * FROM test_type_conversion_int8(null);
140+
INFO: (None, <type 'NoneType'>)
141+
CONTEXT: PL/Python function "test_type_conversion_int8"
142+
test_type_conversion_int8
143+
---------------------------
144+
145+
(1 row)
146+
147+
CREATE FUNCTION test_type_conversion_numeric(x numeric) RETURNS numeric AS $$
148+
plpy.info(x, type(x))
149+
return x
150+
$$ LANGUAGE plpythonu;
151+
/* The current implementation converts numeric to float. */
152+
SELECT * FROM test_type_conversion_numeric(100);
153+
INFO: (100.0, <type 'float'>)
154+
CONTEXT: PL/Python function "test_type_conversion_numeric"
155+
test_type_conversion_numeric
156+
------------------------------
157+
100.0
158+
(1 row)
159+
160+
SELECT * FROM test_type_conversion_numeric(-100);
161+
INFO: (-100.0, <type 'float'>)
162+
CONTEXT: PL/Python function "test_type_conversion_numeric"
163+
test_type_conversion_numeric
164+
------------------------------
165+
-100.0
166+
(1 row)
167+
168+
SELECT * FROM test_type_conversion_numeric(5000000000.5);
169+
INFO: (5000000000.5, <type 'float'>)
170+
CONTEXT: PL/Python function "test_type_conversion_numeric"
171+
test_type_conversion_numeric
172+
------------------------------
173+
5000000000.5
174+
(1 row)
175+
176+
SELECT * FROM test_type_conversion_numeric(79228162514264337593543950336);
177+
INFO: (7.9228162514264338e+28, <type 'float'>)
178+
CONTEXT: PL/Python function "test_type_conversion_numeric"
179+
test_type_conversion_numeric
180+
-------------------------------
181+
79228162514300000000000000000
182+
(1 row)
183+
184+
SELECT * FROM test_type_conversion_numeric(null);
185+
INFO: (None, <type 'NoneType'>)
186+
CONTEXT: PL/Python function "test_type_conversion_numeric"
187+
test_type_conversion_numeric
188+
------------------------------
189+
190+
(1 row)
191+
192+
CREATE FUNCTION test_type_conversion_float4(x float4) RETURNS float4 AS $$
193+
plpy.info(x, type(x))
194+
return x
195+
$$ LANGUAGE plpythonu;
196+
SELECT * FROM test_type_conversion_float4(100);
197+
INFO: (100.0, <type 'float'>)
198+
CONTEXT: PL/Python function "test_type_conversion_float4"
199+
test_type_conversion_float4
200+
-----------------------------
201+
100
202+
(1 row)
203+
204+
SELECT * FROM test_type_conversion_float4(-100);
205+
INFO: (-100.0, <type 'float'>)
206+
CONTEXT: PL/Python function "test_type_conversion_float4"
207+
test_type_conversion_float4
208+
-----------------------------
209+
-100
210+
(1 row)
211+
212+
SELECT * FROM test_type_conversion_float4(5000.5);
213+
INFO: (5000.5, <type 'float'>)
214+
CONTEXT: PL/Python function "test_type_conversion_float4"
215+
test_type_conversion_float4
216+
-----------------------------
217+
5000.5
218+
(1 row)
219+
220+
SELECT * FROM test_type_conversion_float4(null);
221+
INFO: (None, <type 'NoneType'>)
222+
CONTEXT: PL/Python function "test_type_conversion_float4"
223+
test_type_conversion_float4
224+
-----------------------------
225+
226+
(1 row)
227+
228+
CREATE FUNCTION test_type_conversion_float8(x float8) RETURNS float8 AS $$
229+
plpy.info(x, type(x))
230+
return x
231+
$$ LANGUAGE plpythonu;
232+
SELECT * FROM test_type_conversion_float8(100);
233+
INFO: (100.0, <type 'float'>)
234+
CONTEXT: PL/Python function "test_type_conversion_float8"
235+
test_type_conversion_float8
236+
-----------------------------
237+
100
238+
(1 row)
239+
240+
SELECT * FROM test_type_conversion_float8(-100);
241+
INFO: (-100.0, <type 'float'>)
242+
CONTEXT: PL/Python function "test_type_conversion_float8"
243+
test_type_conversion_float8
244+
-----------------------------
245+
-100
246+
(1 row)
247+
248+
SELECT * FROM test_type_conversion_float8(5000000000.5);
249+
INFO: (5000000000.5, <type 'float'>)
250+
CONTEXT: PL/Python function "test_type_conversion_float8"
251+
test_type_conversion_float8
252+
-----------------------------
253+
5000000000.5
254+
(1 row)
255+
256+
SELECT * FROM test_type_conversion_float8(null);
257+
INFO: (None, <type 'NoneType'>)
258+
CONTEXT: PL/Python function "test_type_conversion_float8"
259+
test_type_conversion_float8
260+
-----------------------------
261+
262+
(1 row)
263+
264+
CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
265+
plpy.info(x, type(x))
266+
return x
267+
$$ LANGUAGE plpythonu;
268+
SELECT * FROM test_type_conversion_text('hello world');
269+
INFO: ('hello world', <type 'str'>)
270+
CONTEXT: PL/Python function "test_type_conversion_text"
271+
test_type_conversion_text
272+
---------------------------
273+
hello world
274+
(1 row)
275+
276+
SELECT * FROM test_type_conversion_text(null);
277+
INFO: (None, <type 'NoneType'>)
278+
CONTEXT: PL/Python function "test_type_conversion_text"
279+
test_type_conversion_text
280+
---------------------------
281+
282+
(1 row)
283+
284+
CREATE FUNCTION test_type_conversion_bytea(x bytea) RETURNS bytea AS $$
285+
plpy.info(x, type(x))
286+
return x
287+
$$ LANGUAGE plpythonu;
288+
SELECT * FROM test_type_conversion_bytea('hello world');
289+
INFO: ('\\x68656c6c6f20776f726c64', <type 'str'>)
290+
CONTEXT: PL/Python function "test_type_conversion_bytea"
291+
test_type_conversion_bytea
292+
----------------------------
293+
\x68656c6c6f20776f726c64
294+
(1 row)
295+
296+
SELECT * FROM test_type_conversion_bytea(null);
297+
INFO: (None, <type 'NoneType'>)
298+
CONTEXT: PL/Python function "test_type_conversion_bytea"
299+
test_type_conversion_bytea
300+
----------------------------
301+
302+
(1 row)
303+
304+
CREATE FUNCTION test_type_marshal() RETURNS bytea AS $$
305+
import marshal
306+
return marshal.dumps('hello world')
307+
$$ LANGUAGE plpythonu;
308+
CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
309+
import marshal
310+
try:
311+
return marshal.loads(x)
312+
except ValueError, e:
313+
return 'FAILED: ' + str(e)
314+
$$ LANGUAGE plpythonu;
315+
/* This will currently fail because the bytea datum is presented to
316+
Python as a string in bytea-encoding, which Python doesn't understand. */
317+
SELECT test_type_unmarshal(x) FROM test_type_marshal() x;
318+
test_type_unmarshal
319+
--------------------------
320+
FAILED: bad marshal data
321+
(1 row)
322+
323+
--
324+
-- Domains
325+
--
326+
CREATE DOMAIN uint2 AS int2 CHECK (VALUE >= 0);
327+
CREATE FUNCTION test_type_conversion_uint2(x uint2, y int) RETURNS uint2 AS $$
328+
plpy.info(x, type(x))
329+
return y
330+
$$ LANGUAGE plpythonu;
331+
SELECT * FROM test_type_conversion_uint2(100::uint2, 50);
332+
INFO: (100, <type 'int'>)
333+
CONTEXT: PL/Python function "test_type_conversion_uint2"
334+
test_type_conversion_uint2
335+
----------------------------
336+
50
337+
(1 row)
338+
339+
SELECT * FROM test_type_conversion_uint2(100::uint2, -50);
340+
INFO: (100, <type 'int'>)
341+
CONTEXT: PL/Python function "test_type_conversion_uint2"
342+
ERROR: value for domain uint2 violates check constraint "uint2_check"
343+
CONTEXT: PL/Python function "test_type_conversion_uint2"
344+
SELECT * FROM test_type_conversion_uint2(null, 1);
345+
INFO: (None, <type 'NoneType'>)
346+
CONTEXT: PL/Python function "test_type_conversion_uint2"
347+
test_type_conversion_uint2
348+
----------------------------
349+
1
350+
(1 row)
351+
352+
CREATE DOMAIN bytea10 AS bytea CHECK (octet_length(VALUE) = 10 AND VALUE IS NOT NULL);
353+
CREATE FUNCTION test_type_conversion_bytea10(x bytea10, y bytea) RETURNS bytea10 AS $$
354+
plpy.info(x, type(x))
355+
return y
356+
$$ LANGUAGE plpythonu;
357+
SELECT * FROM test_type_conversion_bytea10('hello wold', 'hello wold');
358+
INFO: ('\\x68656c6c6f20776f6c64', <type 'str'>)
359+
CONTEXT: PL/Python function "test_type_conversion_bytea10"
360+
test_type_conversion_bytea10
361+
------------------------------
362+
\x68656c6c6f20776f6c64
363+
(1 row)
364+
365+
SELECT * FROM test_type_conversion_bytea10('hello world', 'hello wold');
366+
ERROR: value for domain bytea10 violates check constraint "bytea10_check"
367+
SELECT * FROM test_type_conversion_bytea10('hello word', 'hello world');
368+
INFO: ('\\x68656c6c6f20776f7264', <type 'str'>)
369+
CONTEXT: PL/Python function "test_type_conversion_bytea10"
370+
ERROR: value for domain bytea10 violates check constraint "bytea10_check"
371+
CONTEXT: PL/Python function "test_type_conversion_bytea10"
372+
SELECT * FROM test_type_conversion_bytea10(null, 'hello word');
373+
ERROR: value for domain bytea10 violates check constraint "bytea10_check"
374+
SELECT * FROM test_type_conversion_bytea10('hello word', null);
375+
INFO: ('\\x68656c6c6f20776f7264', <type 'str'>)
376+
CONTEXT: PL/Python function "test_type_conversion_bytea10"
377+
ERROR: value for domain bytea10 violates check constraint "bytea10_check"
378+
CONTEXT: PL/Python function "test_type_conversion_bytea10"

0 commit comments

Comments
 (0)