7
7
CREATE FUNCTION test_type_conversion_bool(x bool) RETURNS bool AS $$
8
8
plpy.info(x, type(x))
9
9
return x
10
- $$ LANGUAGE plpythonu ;
10
+ $$ LANGUAGE plpython3u ;
11
11
SELECT * FROM test_type_conversion_bool(true);
12
12
INFO: (True, <class 'bool'>)
13
13
CONTEXT: PL/Python function "test_type_conversion_bool"
@@ -51,7 +51,7 @@ elif n == 5:
51
51
ret = [0]
52
52
plpy.info(ret, not not ret)
53
53
return ret
54
- $$ LANGUAGE plpythonu ;
54
+ $$ LANGUAGE plpython3u ;
55
55
SELECT * FROM test_type_conversion_bool_other(0);
56
56
INFO: (0, False)
57
57
CONTEXT: PL/Python function "test_type_conversion_bool_other"
@@ -103,7 +103,7 @@ CONTEXT: PL/Python function "test_type_conversion_bool_other"
103
103
CREATE FUNCTION test_type_conversion_char(x char) RETURNS char AS $$
104
104
plpy.info(x, type(x))
105
105
return x
106
- $$ LANGUAGE plpythonu ;
106
+ $$ LANGUAGE plpython3u ;
107
107
SELECT * FROM test_type_conversion_char('a');
108
108
INFO: ('a', <class 'str'>)
109
109
CONTEXT: PL/Python function "test_type_conversion_char"
@@ -123,7 +123,7 @@ CONTEXT: PL/Python function "test_type_conversion_char"
123
123
CREATE FUNCTION test_type_conversion_int2(x int2) RETURNS int2 AS $$
124
124
plpy.info(x, type(x))
125
125
return x
126
- $$ LANGUAGE plpythonu ;
126
+ $$ LANGUAGE plpython3u ;
127
127
SELECT * FROM test_type_conversion_int2(100::int2);
128
128
INFO: (100, <class 'int'>)
129
129
CONTEXT: PL/Python function "test_type_conversion_int2"
@@ -151,7 +151,7 @@ CONTEXT: PL/Python function "test_type_conversion_int2"
151
151
CREATE FUNCTION test_type_conversion_int4(x int4) RETURNS int4 AS $$
152
152
plpy.info(x, type(x))
153
153
return x
154
- $$ LANGUAGE plpythonu ;
154
+ $$ LANGUAGE plpython3u ;
155
155
SELECT * FROM test_type_conversion_int4(100);
156
156
INFO: (100, <class 'int'>)
157
157
CONTEXT: PL/Python function "test_type_conversion_int4"
@@ -179,25 +179,25 @@ CONTEXT: PL/Python function "test_type_conversion_int4"
179
179
CREATE FUNCTION test_type_conversion_int8(x int8) RETURNS int8 AS $$
180
180
plpy.info(x, type(x))
181
181
return x
182
- $$ LANGUAGE plpythonu ;
182
+ $$ LANGUAGE plpython3u ;
183
183
SELECT * FROM test_type_conversion_int8(100);
184
- INFO: (100L , <type 'long '>)
184
+ INFO: (100 , <class 'int '>)
185
185
CONTEXT: PL/Python function "test_type_conversion_int8"
186
186
test_type_conversion_int8
187
187
---------------------------
188
188
100
189
189
(1 row)
190
190
191
191
SELECT * FROM test_type_conversion_int8(-100);
192
- INFO: (-100L , <type 'long '>)
192
+ INFO: (-100 , <class 'int '>)
193
193
CONTEXT: PL/Python function "test_type_conversion_int8"
194
194
test_type_conversion_int8
195
195
---------------------------
196
196
-100
197
197
(1 row)
198
198
199
199
SELECT * FROM test_type_conversion_int8(5000000000);
200
- INFO: (5000000000L , <type 'long '>)
200
+ INFO: (5000000000 , <class 'int '>)
201
201
CONTEXT: PL/Python function "test_type_conversion_int8"
202
202
test_type_conversion_int8
203
203
---------------------------
@@ -215,7 +215,7 @@ CONTEXT: PL/Python function "test_type_conversion_int8"
215
215
CREATE FUNCTION test_type_conversion_numeric(x numeric) RETURNS numeric AS $$
216
216
plpy.info(x, type(x))
217
217
return x
218
- $$ LANGUAGE plpythonu ;
218
+ $$ LANGUAGE plpython3u ;
219
219
/* The current implementation converts numeric to float. */
220
220
SELECT * FROM test_type_conversion_numeric(100);
221
221
INFO: (100.0, <class 'float'>)
@@ -252,7 +252,7 @@ CONTEXT: PL/Python function "test_type_conversion_numeric"
252
252
CREATE FUNCTION test_type_conversion_float4(x float4) RETURNS float4 AS $$
253
253
plpy.info(x, type(x))
254
254
return x
255
- $$ LANGUAGE plpythonu ;
255
+ $$ LANGUAGE plpython3u ;
256
256
SELECT * FROM test_type_conversion_float4(100);
257
257
INFO: (100.0, <class 'float'>)
258
258
CONTEXT: PL/Python function "test_type_conversion_float4"
@@ -288,7 +288,7 @@ CONTEXT: PL/Python function "test_type_conversion_float4"
288
288
CREATE FUNCTION test_type_conversion_float8(x float8) RETURNS float8 AS $$
289
289
plpy.info(x, type(x))
290
290
return x
291
- $$ LANGUAGE plpythonu ;
291
+ $$ LANGUAGE plpython3u ;
292
292
SELECT * FROM test_type_conversion_float8(100);
293
293
INFO: (100.0, <class 'float'>)
294
294
CONTEXT: PL/Python function "test_type_conversion_float8"
@@ -324,7 +324,7 @@ CONTEXT: PL/Python function "test_type_conversion_float8"
324
324
CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
325
325
plpy.info(x, type(x))
326
326
return x
327
- $$ LANGUAGE plpythonu ;
327
+ $$ LANGUAGE plpython3u ;
328
328
SELECT * FROM test_type_conversion_text('hello world');
329
329
INFO: ('hello world', <class 'str'>)
330
330
CONTEXT: PL/Python function "test_type_conversion_text"
@@ -344,7 +344,7 @@ CONTEXT: PL/Python function "test_type_conversion_text"
344
344
CREATE FUNCTION test_type_conversion_bytea(x bytea) RETURNS bytea AS $$
345
345
plpy.info(x, type(x))
346
346
return x
347
- $$ LANGUAGE plpythonu ;
347
+ $$ LANGUAGE plpython3u ;
348
348
SELECT * FROM test_type_conversion_bytea('hello world');
349
349
INFO: (b'hello world', <class 'bytes'>)
350
350
CONTEXT: PL/Python function "test_type_conversion_bytea"
@@ -372,14 +372,14 @@ CONTEXT: PL/Python function "test_type_conversion_bytea"
372
372
CREATE FUNCTION test_type_marshal() RETURNS bytea AS $$
373
373
import marshal
374
374
return marshal.dumps('hello world')
375
- $$ LANGUAGE plpythonu ;
375
+ $$ LANGUAGE plpython3u ;
376
376
CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
377
377
import marshal
378
378
try:
379
379
return marshal.loads(x)
380
- except ValueError, e:
380
+ except ValueError as e:
381
381
return 'FAILED: ' + str(e)
382
- $$ LANGUAGE plpythonu ;
382
+ $$ LANGUAGE plpython3u ;
383
383
SELECT test_type_unmarshal(x) FROM test_type_marshal() x;
384
384
test_type_unmarshal
385
385
---------------------
@@ -392,7 +392,7 @@ SELECT test_type_unmarshal(x) FROM test_type_marshal() x;
392
392
CREATE DOMAIN booltrue AS bool CHECK (VALUE IS TRUE OR VALUE IS NULL);
393
393
CREATE FUNCTION test_type_conversion_booltrue(x booltrue, y bool) RETURNS booltrue AS $$
394
394
return y
395
- $$ LANGUAGE plpythonu ;
395
+ $$ LANGUAGE plpython3u ;
396
396
SELECT * FROM test_type_conversion_booltrue(true, true);
397
397
test_type_conversion_booltrue
398
398
-------------------------------
@@ -409,7 +409,7 @@ CREATE DOMAIN uint2 AS int2 CHECK (VALUE >= 0);
409
409
CREATE FUNCTION test_type_conversion_uint2(x uint2, y int) RETURNS uint2 AS $$
410
410
plpy.info(x, type(x))
411
411
return y
412
- $$ LANGUAGE plpythonu ;
412
+ $$ LANGUAGE plpython3u ;
413
413
SELECT * FROM test_type_conversion_uint2(100::uint2, 50);
414
414
INFO: (100, <class 'int'>)
415
415
CONTEXT: PL/Python function "test_type_conversion_uint2"
@@ -435,7 +435,7 @@ CONTEXT: PL/Python function "test_type_conversion_uint2"
435
435
CREATE DOMAIN nnint AS int CHECK (VALUE IS NOT NULL);
436
436
CREATE FUNCTION test_type_conversion_nnint(x nnint, y int) RETURNS nnint AS $$
437
437
return y
438
- $$ LANGUAGE plpythonu ;
438
+ $$ LANGUAGE plpython3u ;
439
439
SELECT * FROM test_type_conversion_nnint(10, 20);
440
440
test_type_conversion_nnint
441
441
----------------------------
@@ -452,7 +452,7 @@ CREATE DOMAIN bytea10 AS bytea CHECK (octet_length(VALUE) = 10 AND VALUE IS NOT
452
452
CREATE FUNCTION test_type_conversion_bytea10(x bytea10, y bytea) RETURNS bytea10 AS $$
453
453
plpy.info(x, type(x))
454
454
return y
455
- $$ LANGUAGE plpythonu ;
455
+ $$ LANGUAGE plpython3u ;
456
456
SELECT * FROM test_type_conversion_bytea10('hello wold', 'hello wold');
457
457
INFO: (b'hello wold', <class 'bytes'>)
458
458
CONTEXT: PL/Python function "test_type_conversion_bytea10"
@@ -483,7 +483,7 @@ PL/Python function "test_type_conversion_bytea10"
483
483
CREATE FUNCTION test_type_conversion_array_int4(x int4[]) RETURNS int4[] AS $$
484
484
plpy.info(x, type(x))
485
485
return x
486
- $$ LANGUAGE plpythonu ;
486
+ $$ LANGUAGE plpython3u ;
487
487
SELECT * FROM test_type_conversion_array_int4(ARRAY[0, 100]);
488
488
INFO: ([0, 100], <class 'list'>)
489
489
CONTEXT: PL/Python function "test_type_conversion_array_int4"
@@ -531,7 +531,7 @@ CONTEXT: PL/Python function "test_type_conversion_array_int4"
531
531
CREATE FUNCTION test_type_conversion_array_bytea(x bytea[]) RETURNS bytea[] AS $$
532
532
plpy.info(x, type(x))
533
533
return x
534
- $$ LANGUAGE plpythonu ;
534
+ $$ LANGUAGE plpython3u ;
535
535
SELECT * FROM test_type_conversion_array_bytea(ARRAY[E'\\xdeadbeef'::bytea, NULL]);
536
536
INFO: ([b'\xde\xad\xbe\xef', None], <class 'list'>)
537
537
CONTEXT: PL/Python function "test_type_conversion_array_bytea"
@@ -542,7 +542,7 @@ CONTEXT: PL/Python function "test_type_conversion_array_bytea"
542
542
543
543
CREATE FUNCTION test_type_conversion_array_mixed1() RETURNS text[] AS $$
544
544
return [123, 'abc']
545
- $$ LANGUAGE plpythonu ;
545
+ $$ LANGUAGE plpython3u ;
546
546
SELECT * FROM test_type_conversion_array_mixed1();
547
547
test_type_conversion_array_mixed1
548
548
-----------------------------------
@@ -551,20 +551,20 @@ SELECT * FROM test_type_conversion_array_mixed1();
551
551
552
552
CREATE FUNCTION test_type_conversion_array_mixed2() RETURNS int[] AS $$
553
553
return [123, 'abc']
554
- $$ LANGUAGE plpythonu ;
554
+ $$ LANGUAGE plpython3u ;
555
555
SELECT * FROM test_type_conversion_array_mixed2();
556
556
ERROR: invalid input syntax for integer: "abc"
557
557
CONTEXT: while creating return value
558
558
PL/Python function "test_type_conversion_array_mixed2"
559
559
CREATE FUNCTION test_type_conversion_array_record() RETURNS type_record[] AS $$
560
560
return [None]
561
- $$ LANGUAGE plpythonu ;
561
+ $$ LANGUAGE plpython3u ;
562
562
SELECT * FROM test_type_conversion_array_record();
563
563
ERROR: PL/Python functions cannot return type type_record[]
564
564
DETAIL: PL/Python does not support conversion to arrays of row types.
565
565
CREATE FUNCTION test_type_conversion_array_string() RETURNS text[] AS $$
566
566
return 'abc'
567
- $$ LANGUAGE plpythonu ;
567
+ $$ LANGUAGE plpython3u ;
568
568
SELECT * FROM test_type_conversion_array_string();
569
569
test_type_conversion_array_string
570
570
-----------------------------------
@@ -573,7 +573,7 @@ SELECT * FROM test_type_conversion_array_string();
573
573
574
574
CREATE FUNCTION test_type_conversion_array_tuple() RETURNS text[] AS $$
575
575
return ('abc', 'def')
576
- $$ LANGUAGE plpythonu ;
576
+ $$ LANGUAGE plpython3u ;
577
577
SELECT * FROM test_type_conversion_array_tuple();
578
578
test_type_conversion_array_tuple
579
579
----------------------------------
@@ -582,8 +582,69 @@ SELECT * FROM test_type_conversion_array_tuple();
582
582
583
583
CREATE FUNCTION test_type_conversion_array_error() RETURNS int[] AS $$
584
584
return 5
585
- $$ LANGUAGE plpythonu ;
585
+ $$ LANGUAGE plpython3u ;
586
586
SELECT * FROM test_type_conversion_array_error();
587
587
ERROR: PL/Python: return value of function with array return type is not a Python sequence
588
588
CONTEXT: while creating return value
589
589
PL/Python function "test_type_conversion_array_error"
590
+ --
591
+ -- Prepared statements
592
+ --
593
+ CREATE OR REPLACE FUNCTION test_prep_bool_input() RETURNS int
594
+ LANGUAGE plpython3u
595
+ AS $$
596
+ plan = plpy.prepare("SELECT CASE WHEN $1 THEN 1 ELSE 0 END AS val", ['boolean'])
597
+ rv = plpy.execute(plan, ['fa'], 5) # 'fa' is true in Python
598
+ return rv[0]['val']
599
+ $$;
600
+ SELECT test_prep_bool_input(); -- 1
601
+ test_prep_bool_input
602
+ ----------------------
603
+ 1
604
+ (1 row)
605
+
606
+ CREATE OR REPLACE FUNCTION test_prep_bool_output() RETURNS bool
607
+ LANGUAGE plpython3u
608
+ AS $$
609
+ plan = plpy.prepare("SELECT $1 = 1 AS val", ['int'])
610
+ rv = plpy.execute(plan, [0], 5)
611
+ plpy.info(rv[0])
612
+ return rv[0]['val']
613
+ $$;
614
+ SELECT test_prep_bool_output(); -- false
615
+ INFO: {'val': False}
616
+ CONTEXT: PL/Python function "test_prep_bool_output"
617
+ test_prep_bool_output
618
+ -----------------------
619
+ f
620
+ (1 row)
621
+
622
+ CREATE OR REPLACE FUNCTION test_prep_bytea_input(bb bytea) RETURNS int
623
+ LANGUAGE plpython3u
624
+ AS $$
625
+ plan = plpy.prepare("SELECT octet_length($1) AS val", ['bytea'])
626
+ rv = plpy.execute(plan, [bb], 5)
627
+ return rv[0]['val']
628
+ $$;
629
+ SELECT test_prep_bytea_input(E'a\\000b'); -- 3 (embedded null formerly truncated value)
630
+ test_prep_bytea_input
631
+ -----------------------
632
+ 3
633
+ (1 row)
634
+
635
+ CREATE OR REPLACE FUNCTION test_prep_bytea_output() RETURNS bytea
636
+ LANGUAGE plpython3u
637
+ AS $$
638
+ plan = plpy.prepare("SELECT decode('aa00bb', 'hex') AS val")
639
+ rv = plpy.execute(plan, [], 5)
640
+ plpy.info(rv[0])
641
+ return rv[0]['val']
642
+ $$;
643
+ SELECT test_prep_bytea_output();
644
+ INFO: {'val': b'\xaa\x00\xbb'}
645
+ CONTEXT: PL/Python function "test_prep_bytea_output"
646
+ test_prep_bytea_output
647
+ ------------------------
648
+ \xaa00bb
649
+ (1 row)
650
+
0 commit comments