@@ -32,6 +32,74 @@ CONTEXT: PL/Python function "test_type_conversion_bool"
3232
3333(1 row)
3434
35+ -- test various other ways to express Booleans in Python
36+ CREATE FUNCTION test_type_conversion_bool_other(n int) RETURNS bool AS $$
37+ # numbers
38+ if n == 0:
39+ ret = 0
40+ elif n == 1:
41+ ret = 5
42+ # strings
43+ elif n == 2:
44+ ret = ''
45+ elif n == 3:
46+ ret = 'fa' # true in Python, false in PostgreSQL
47+ # containers
48+ elif n == 4:
49+ ret = []
50+ elif n == 5:
51+ ret = [0]
52+ plpy.info(ret, not not ret)
53+ return ret
54+ $$ LANGUAGE plpythonu;
55+ SELECT * FROM test_type_conversion_bool_other(0);
56+ INFO: (0, False)
57+ CONTEXT: PL/Python function "test_type_conversion_bool_other"
58+ test_type_conversion_bool_other
59+ ---------------------------------
60+ f
61+ (1 row)
62+
63+ SELECT * FROM test_type_conversion_bool_other(1);
64+ INFO: (5, True)
65+ CONTEXT: PL/Python function "test_type_conversion_bool_other"
66+ test_type_conversion_bool_other
67+ ---------------------------------
68+ t
69+ (1 row)
70+
71+ SELECT * FROM test_type_conversion_bool_other(2);
72+ INFO: ('', False)
73+ CONTEXT: PL/Python function "test_type_conversion_bool_other"
74+ test_type_conversion_bool_other
75+ ---------------------------------
76+ f
77+ (1 row)
78+
79+ SELECT * FROM test_type_conversion_bool_other(3);
80+ INFO: ('fa', True)
81+ CONTEXT: PL/Python function "test_type_conversion_bool_other"
82+ test_type_conversion_bool_other
83+ ---------------------------------
84+ t
85+ (1 row)
86+
87+ SELECT * FROM test_type_conversion_bool_other(4);
88+ INFO: ([], False)
89+ CONTEXT: PL/Python function "test_type_conversion_bool_other"
90+ test_type_conversion_bool_other
91+ ---------------------------------
92+ f
93+ (1 row)
94+
95+ SELECT * FROM test_type_conversion_bool_other(5);
96+ INFO: ([0], True)
97+ CONTEXT: PL/Python function "test_type_conversion_bool_other"
98+ test_type_conversion_bool_other
99+ ---------------------------------
100+ t
101+ (1 row)
102+
35103CREATE FUNCTION test_type_conversion_char(x char) RETURNS char AS $$
36104plpy.info(x, type(x))
37105return x
@@ -278,13 +346,21 @@ plpy.info(x, type(x))
278346return x
279347$$ LANGUAGE plpythonu;
280348SELECT * FROM test_type_conversion_bytea('hello world');
281- INFO: ('\\x68656c6c6f20776f726c64 ', <type 'str'>)
349+ INFO: ('hello world ', <type 'str'>)
282350CONTEXT: PL/Python function "test_type_conversion_bytea"
283351 test_type_conversion_bytea
284352----------------------------
285353 \x68656c6c6f20776f726c64
286354(1 row)
287355
356+ SELECT * FROM test_type_conversion_bytea(E'null\\000byte');
357+ INFO: ('null\x00byte', <type 'str'>)
358+ CONTEXT: PL/Python function "test_type_conversion_bytea"
359+ test_type_conversion_bytea
360+ ----------------------------
361+ \x6e756c6c0062797465
362+ (1 row)
363+
288364SELECT * FROM test_type_conversion_bytea(null);
289365INFO: (None, <type 'NoneType'>)
290366CONTEXT: PL/Python function "test_type_conversion_bytea"
@@ -304,17 +380,31 @@ try:
304380except ValueError, e:
305381 return 'FAILED: ' + str(e)
306382$$ LANGUAGE plpythonu;
307- /* This will currently fail because the bytea datum is presented to
308- Python as a string in bytea-encoding, which Python doesn't understand. */
309383SELECT test_type_unmarshal(x) FROM test_type_marshal() x;
310- test_type_unmarshal
311- --------------------------
312- FAILED: bad marshal data
384+ test_type_unmarshal
385+ ---------------------
386+ hello world
313387(1 row)
314388
315389--
316390-- Domains
317391--
392+ CREATE DOMAIN booltrue AS bool CHECK (VALUE IS TRUE OR VALUE IS NULL);
393+ CREATE FUNCTION test_type_conversion_booltrue(x booltrue, y bool) RETURNS booltrue AS $$
394+ return y
395+ $$ LANGUAGE plpythonu;
396+ SELECT * FROM test_type_conversion_booltrue(true, true);
397+ test_type_conversion_booltrue
398+ -------------------------------
399+ t
400+ (1 row)
401+
402+ SELECT * FROM test_type_conversion_booltrue(false, true);
403+ ERROR: value for domain booltrue violates check constraint "booltrue_check"
404+ SELECT * FROM test_type_conversion_booltrue(true, false);
405+ ERROR: value for domain booltrue violates check constraint "booltrue_check"
406+ CONTEXT: while creating return value
407+ PL/Python function "test_type_conversion_booltrue"
318408CREATE DOMAIN uint2 AS int2 CHECK (VALUE >= 0);
319409CREATE FUNCTION test_type_conversion_uint2(x uint2, y int) RETURNS uint2 AS $$
320410plpy.info(x, type(x))
@@ -342,13 +432,29 @@ CONTEXT: PL/Python function "test_type_conversion_uint2"
342432 1
343433(1 row)
344434
435+ CREATE DOMAIN nnint AS int CHECK (VALUE IS NOT NULL);
436+ CREATE FUNCTION test_type_conversion_nnint(x nnint, y int) RETURNS nnint AS $$
437+ return y
438+ $$ LANGUAGE plpythonu;
439+ SELECT * FROM test_type_conversion_nnint(10, 20);
440+ test_type_conversion_nnint
441+ ----------------------------
442+ 20
443+ (1 row)
444+
445+ SELECT * FROM test_type_conversion_nnint(null, 20);
446+ ERROR: value for domain nnint violates check constraint "nnint_check"
447+ SELECT * FROM test_type_conversion_nnint(10, null);
448+ ERROR: value for domain nnint violates check constraint "nnint_check"
449+ CONTEXT: while creating return value
450+ PL/Python function "test_type_conversion_nnint"
345451CREATE DOMAIN bytea10 AS bytea CHECK (octet_length(VALUE) = 10 AND VALUE IS NOT NULL);
346452CREATE FUNCTION test_type_conversion_bytea10(x bytea10, y bytea) RETURNS bytea10 AS $$
347453plpy.info(x, type(x))
348454return y
349455$$ LANGUAGE plpythonu;
350456SELECT * FROM test_type_conversion_bytea10('hello wold', 'hello wold');
351- INFO: ('\\x68656c6c6f20776f6c64 ', <type 'str'>)
457+ INFO: ('hello wold ', <type 'str'>)
352458CONTEXT: PL/Python function "test_type_conversion_bytea10"
353459 test_type_conversion_bytea10
354460------------------------------
@@ -358,15 +464,15 @@ CONTEXT: PL/Python function "test_type_conversion_bytea10"
358464SELECT * FROM test_type_conversion_bytea10('hello world', 'hello wold');
359465ERROR: value for domain bytea10 violates check constraint "bytea10_check"
360466SELECT * FROM test_type_conversion_bytea10('hello word', 'hello world');
361- INFO: ('\\x68656c6c6f20776f7264 ', <type 'str'>)
467+ INFO: ('hello word ', <type 'str'>)
362468CONTEXT: PL/Python function "test_type_conversion_bytea10"
363469ERROR: value for domain bytea10 violates check constraint "bytea10_check"
364470CONTEXT: while creating return value
365471PL/Python function "test_type_conversion_bytea10"
366472SELECT * FROM test_type_conversion_bytea10(null, 'hello word');
367473ERROR: value for domain bytea10 violates check constraint "bytea10_check"
368474SELECT * FROM test_type_conversion_bytea10('hello word', null);
369- INFO: ('\\x68656c6c6f20776f7264 ', <type 'str'>)
475+ INFO: ('hello word ', <type 'str'>)
370476CONTEXT: PL/Python function "test_type_conversion_bytea10"
371477ERROR: value for domain bytea10 violates check constraint "bytea10_check"
372478CONTEXT: while creating return value
0 commit comments