summaryrefslogtreecommitdiffstats
path: root/src/pl/plpython/expected/plpython_composite.out
blob: bb101e07d539fa5bda35c9a8a10b177ab74c9955 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
CREATE FUNCTION multiout_simple(OUT i integer, OUT j integer) AS $$
return (1, 2)
$$ LANGUAGE plpython3u;
SELECT multiout_simple();
 multiout_simple 
-----------------
 (1,2)
(1 row)

SELECT * FROM multiout_simple();
 i | j 
---+---
 1 | 2
(1 row)

SELECT i, j + 2 FROM multiout_simple();
 i | ?column? 
---+----------
 1 |        4
(1 row)

SELECT (multiout_simple()).j + 3;
 ?column? 
----------
        5
(1 row)

CREATE FUNCTION multiout_simple_setof(n integer = 1, OUT integer, OUT integer) RETURNS SETOF record AS $$
return [(1, 2)] * n
$$ LANGUAGE plpython3u;
SELECT multiout_simple_setof();
 multiout_simple_setof 
-----------------------
 (1,2)
(1 row)

SELECT * FROM multiout_simple_setof();
 column1 | column2 
---------+---------
       1 |       2
(1 row)

SELECT * FROM multiout_simple_setof(3);
 column1 | column2 
---------+---------
       1 |       2
       1 |       2
       1 |       2
(3 rows)

CREATE FUNCTION multiout_record_as(typ text,
                                   first text, OUT first text,
                                   second integer, OUT second integer,
                                   retnull boolean) RETURNS record AS $$
if retnull:
    return None
if typ == 'dict':
    return { 'first': first, 'second': second, 'additionalfield': 'must not cause trouble' }
elif typ == 'tuple':
    return ( first, second )
elif typ == 'list':
    return [ first, second ]
elif typ == 'obj':
    class type_record: pass
    type_record.first = first
    type_record.second = second
    return type_record
elif typ == 'str':
    return "('%s',%r)" % (first, second)
$$ LANGUAGE plpython3u;
SELECT * FROM multiout_record_as('dict', 'foo', 1, 'f');
 first | second 
-------+--------
 foo   |      1
(1 row)

SELECT multiout_record_as('dict', 'foo', 1, 'f');
 multiout_record_as 
--------------------
 (foo,1)
(1 row)

SELECT * FROM multiout_record_as('dict', null, null, false);
 first | second 
-------+--------
       |       
(1 row)

SELECT * FROM multiout_record_as('dict', 'one', null, false);
 first | second 
-------+--------
 one   |       
(1 row)

SELECT * FROM multiout_record_as('dict', null, 2, false);
 first | second 
-------+--------
       |      2
(1 row)

SELECT * FROM multiout_record_as('dict', 'three', 3, false);
 first | second 
-------+--------
 three |      3
(1 row)

SELECT * FROM multiout_record_as('dict', null, null, true);
 first | second 
-------+--------
       |       
(1 row)

SELECT * FROM multiout_record_as('tuple', null, null, false);
 first | second 
-------+--------
       |       
(1 row)

SELECT * FROM multiout_record_as('tuple', 'one', null, false);
 first | second 
-------+--------
 one   |       
(1 row)

SELECT * FROM multiout_record_as('tuple', null, 2, false);
 first | second 
-------+--------
       |      2
(1 row)

SELECT * FROM multiout_record_as('tuple', 'three', 3, false);
 first | second 
-------+--------
 three |      3
(1 row)

SELECT * FROM multiout_record_as('tuple', null, null, true);
 first | second 
-------+--------
       |       
(1 row)

SELECT * FROM multiout_record_as('list', null, null, false);
 first | second 
-------+--------
       |       
(1 row)

SELECT * FROM multiout_record_as('list', 'one', null, false);
 first | second 
-------+--------
 one   |       
(1 row)

SELECT * FROM multiout_record_as('list', null, 2, false);
 first | second 
-------+--------
       |      2
(1 row)

SELECT * FROM multiout_record_as('list', 'three', 3, false);
 first | second 
-------+--------
 three |      3
(1 row)

SELECT * FROM multiout_record_as('list', null, null, true);
 first | second 
-------+--------
       |       
(1 row)

SELECT * FROM multiout_record_as('obj', null, null, false);
 first | second 
-------+--------
       |       
(1 row)

SELECT * FROM multiout_record_as('obj', 'one', null, false);
 first | second 
-------+--------
 one   |       
(1 row)

SELECT * FROM multiout_record_as('obj', null, 2, false);
 first | second 
-------+--------
       |      2
(1 row)

SELECT * FROM multiout_record_as('obj', 'three', 3, false);
 first | second 
-------+--------
 three |      3
(1 row)

SELECT * FROM multiout_record_as('obj', null, null, true);
 first | second 
-------+--------
       |       
(1 row)

SELECT * FROM multiout_record_as('str', 'one', 1, false);
 first | second 
-------+--------
 'one' |      1
(1 row)

SELECT * FROM multiout_record_as('str', 'one', 2, false);
 first | second 
-------+--------
 'one' |      2
(1 row)

SELECT *, s IS NULL AS snull FROM multiout_record_as('tuple', 'xxx', NULL, 'f') AS f(f, s);
  f  | s | snull 
-----+---+-------
 xxx |   | t
(1 row)

SELECT *, f IS NULL AS fnull, s IS NULL AS snull FROM multiout_record_as('tuple', 'xxx', 1, 't') AS f(f, s);
 f | s | fnull | snull 
---+---+-------+-------
   |   | t     | t
(1 row)

SELECT * FROM multiout_record_as('obj', NULL, 10, 'f');
 first | second 
-------+--------
       |     10
(1 row)

CREATE FUNCTION multiout_setof(n integer,
                               OUT power_of_2 integer,
                               OUT length integer) RETURNS SETOF record AS $$
for i in range(n):
    power = 2 ** i
    length = plpy.execute("select length('%d')" % power)[0]['length']
    yield power, length
$$ LANGUAGE plpython3u;
SELECT * FROM multiout_setof(3);
 power_of_2 | length 
------------+--------
          1 |      1
          2 |      1
          4 |      1
(3 rows)

SELECT multiout_setof(5);
 multiout_setof 
----------------
 (1,1)
 (2,1)
 (4,1)
 (8,1)
 (16,2)
(5 rows)

CREATE FUNCTION multiout_return_table() RETURNS TABLE (x integer, y text) AS $$
return [{'x': 4, 'y' :'four'},
        {'x': 7, 'y' :'seven'},
        {'x': 0, 'y' :'zero'}]
$$ LANGUAGE plpython3u;
SELECT * FROM multiout_return_table();
 x |   y   
---+-------
 4 | four
 7 | seven
 0 | zero
(3 rows)

CREATE FUNCTION multiout_array(OUT integer[], OUT text) RETURNS SETOF record AS $$
yield [[1], 'a']
yield [[1,2], 'b']
yield [[1,2,3], None]
$$ LANGUAGE plpython3u;
SELECT * FROM multiout_array();
 column1 | column2 
---------+---------
 {1}     | a
 {1,2}   | b
 {1,2,3} | 
(3 rows)

CREATE FUNCTION singleout_composite(OUT type_record) AS $$
return {'first': 1, 'second': 2}
$$ LANGUAGE plpython3u;
CREATE FUNCTION multiout_composite(OUT type_record) RETURNS SETOF type_record AS $$
return [{'first': 1, 'second': 2},
       {'first': 3, 'second': 4	}]
$$ LANGUAGE plpython3u;
SELECT * FROM singleout_composite();
 first | second 
-------+--------
 1     |      2
(1 row)

SELECT * FROM multiout_composite();
 first | second 
-------+--------
 1     |      2
 3     |      4
(2 rows)

-- composite OUT parameters in functions returning RECORD not supported yet
CREATE FUNCTION multiout_composite(INOUT n integer, OUT type_record) AS $$
return (n, (n * 2, n * 3))
$$ LANGUAGE plpython3u;
CREATE FUNCTION multiout_table_type_setof(typ text, returnnull boolean, INOUT n integer, OUT table_record) RETURNS SETOF record AS $$
if returnnull:
    d = None
elif typ == 'dict':
    d = {'first': n * 2, 'second': n * 3, 'extra': 'not important'}
elif typ == 'tuple':
    d = (n * 2, n * 3)
elif typ == 'list':
    d = [ n * 2, n * 3 ]
elif typ == 'obj':
    class d: pass
    d.first = n * 2
    d.second = n * 3
elif typ == 'str':
    d = "(%r,%r)" % (n * 2, n * 3)
for i in range(n):
    yield (i, d)
$$ LANGUAGE plpython3u;
SELECT * FROM multiout_composite(2);
 n | column2 
---+---------
 2 | (4,6)
(1 row)

SELECT * FROM multiout_table_type_setof('dict', 'f', 3);
 n | column2 
---+---------
 0 | (6,9)
 1 | (6,9)
 2 | (6,9)
(3 rows)

SELECT * FROM multiout_table_type_setof('dict', 'f', 7);
 n | column2 
---+---------
 0 | (14,21)
 1 | (14,21)
 2 | (14,21)
 3 | (14,21)
 4 | (14,21)
 5 | (14,21)
 6 | (14,21)
(7 rows)

SELECT * FROM multiout_table_type_setof('tuple', 'f', 2);
 n | column2 
---+---------
 0 | (4,6)
 1 | (4,6)
(2 rows)

SELECT * FROM multiout_table_type_setof('tuple', 'f', 3);
 n | column2 
---+---------
 0 | (6,9)
 1 | (6,9)
 2 | (6,9)
(3 rows)

SELECT * FROM multiout_table_type_setof('list', 'f', 2);
 n | column2 
---+---------
 0 | (4,6)
 1 | (4,6)
(2 rows)

SELECT * FROM multiout_table_type_setof('list', 'f', 3);
 n | column2 
---+---------
 0 | (6,9)
 1 | (6,9)
 2 | (6,9)
(3 rows)

SELECT * FROM multiout_table_type_setof('obj', 'f', 4);
 n | column2 
---+---------
 0 | (8,12)
 1 | (8,12)
 2 | (8,12)
 3 | (8,12)
(4 rows)

SELECT * FROM multiout_table_type_setof('obj', 'f', 5);
 n | column2 
---+---------
 0 | (10,15)
 1 | (10,15)
 2 | (10,15)
 3 | (10,15)
 4 | (10,15)
(5 rows)

SELECT * FROM multiout_table_type_setof('str', 'f', 6);
 n | column2 
---+---------
 0 | (12,18)
 1 | (12,18)
 2 | (12,18)
 3 | (12,18)
 4 | (12,18)
 5 | (12,18)
(6 rows)

SELECT * FROM multiout_table_type_setof('str', 'f', 7);
 n | column2 
---+---------
 0 | (14,21)
 1 | (14,21)
 2 | (14,21)
 3 | (14,21)
 4 | (14,21)
 5 | (14,21)
 6 | (14,21)
(7 rows)

SELECT * FROM multiout_table_type_setof('dict', 't', 3);
 n | column2 
---+---------
 0 | 
 1 | 
 2 | 
(3 rows)

-- check what happens if a type changes under us
CREATE TABLE changing (
    i integer,
    j integer
);
CREATE FUNCTION changing_test(OUT n integer, OUT changing) RETURNS SETOF record AS $$
return [(1, {'i': 1, 'j': 2}),
        (1, (3, 4))]
$$ LANGUAGE plpython3u;
SELECT * FROM changing_test();
 n | column2 
---+---------
 1 | (1,2)
 1 | (3,4)
(2 rows)

ALTER TABLE changing DROP COLUMN j;
SELECT * FROM changing_test();
ERROR:  length of returned sequence did not match number of columns in row
CONTEXT:  while creating return value
PL/Python function "changing_test"
SELECT * FROM changing_test();
ERROR:  length of returned sequence did not match number of columns in row
CONTEXT:  while creating return value
PL/Python function "changing_test"
ALTER TABLE changing ADD COLUMN j integer;
SELECT * FROM changing_test();
 n | column2 
---+---------
 1 | (1,2)
 1 | (3,4)
(2 rows)

-- tables of composite types
CREATE FUNCTION composite_types_table(OUT tab table_record[], OUT typ type_record[] ) RETURNS SETOF record AS $$
yield {'tab': [('first', 1), ('second', 2)],
      'typ': [{'first': 'third', 'second': 3},
              {'first': 'fourth', 'second': 4}]}
yield {'tab': [('first', 1), ('second', 2)],
      'typ': [{'first': 'third', 'second': 3},
              {'first': 'fourth', 'second': 4}]}
yield {'tab': [('first', 1), ('second', 2)],
      'typ': [{'first': 'third', 'second': 3},
              {'first': 'fourth', 'second': 4}]}
$$ LANGUAGE plpython3u;
SELECT * FROM composite_types_table();
            tab             |            typ             
----------------------------+----------------------------
 {"(first,1)","(second,2)"} | {"(third,3)","(fourth,4)"}
 {"(first,1)","(second,2)"} | {"(third,3)","(fourth,4)"}
 {"(first,1)","(second,2)"} | {"(third,3)","(fourth,4)"}
(3 rows)

-- check what happens if the output record descriptor changes
CREATE FUNCTION return_record(t text) RETURNS record AS $$
return {'t': t, 'val': 10}
$$ LANGUAGE plpython3u;
SELECT * FROM return_record('abc') AS r(t text, val integer);
  t  | val 
-----+-----
 abc |  10
(1 row)

SELECT * FROM return_record('abc') AS r(t text, val bigint);
  t  | val 
-----+-----
 abc |  10
(1 row)

SELECT * FROM return_record('abc') AS r(t text, val integer);
  t  | val 
-----+-----
 abc |  10
(1 row)

SELECT * FROM return_record('abc') AS r(t varchar(30), val integer);
  t  | val 
-----+-----
 abc |  10
(1 row)

SELECT * FROM return_record('abc') AS r(t varchar(100), val integer);
  t  | val 
-----+-----
 abc |  10
(1 row)

SELECT * FROM return_record('999') AS r(val text, t integer);
 val |  t  
-----+-----
 10  | 999
(1 row)

CREATE FUNCTION return_record_2(t text) RETURNS record AS $$
return {'v1':1,'v2':2,t:3}
$$ LANGUAGE plpython3u;
SELECT * FROM return_record_2('v3') AS (v3 int, v2 int, v1 int);
 v3 | v2 | v1 
----+----+----
  3 |  2 |  1
(1 row)

SELECT * FROM return_record_2('v3') AS (v2 int, v3 int, v1 int);
 v2 | v3 | v1 
----+----+----
  2 |  3 |  1
(1 row)

SELECT * FROM return_record_2('v4') AS (v1 int, v4 int, v2 int);
 v1 | v4 | v2 
----+----+----
  1 |  3 |  2
(1 row)

SELECT * FROM return_record_2('v4') AS (v1 int, v4 int, v2 int);
 v1 | v4 | v2 
----+----+----
  1 |  3 |  2
(1 row)

-- error
SELECT * FROM return_record_2('v4') AS (v1 int, v3 int, v2 int);
ERROR:  key "v3" not found in mapping
HINT:  To return null in a column, add the value None to the mapping with the key named after the column.
CONTEXT:  while creating return value
PL/Python function "return_record_2"
-- works
SELECT * FROM return_record_2('v3') AS (v1 int, v3 int, v2 int);
 v1 | v3 | v2 
----+----+----
  1 |  3 |  2
(1 row)

SELECT * FROM return_record_2('v3') AS (v1 int, v2 int, v3 int);
 v1 | v2 | v3 
----+----+----
  1 |  2 |  3
(1 row)

-- multi-dimensional array of composite types.
CREATE FUNCTION composite_type_as_list()  RETURNS type_record[] AS $$
  return [[('first', 1), ('second', 1)], [('first', 2), ('second', 2)], [('first', 3), ('second', 3)]];
$$ LANGUAGE plpython3u;
SELECT * FROM composite_type_as_list();
                               composite_type_as_list                               
------------------------------------------------------------------------------------
 {{"(first,1)","(second,1)"},{"(first,2)","(second,2)"},{"(first,3)","(second,3)"}}
(1 row)

-- Starting with PostgreSQL 10, a composite type in an array cannot be
-- represented as a Python list, because it's ambiguous with multi-dimensional
-- arrays. So this throws an error now. The error should contain a useful hint
-- on the issue.
CREATE FUNCTION composite_type_as_list_broken()  RETURNS type_record[] AS $$
  return [['first', 1]];
$$ LANGUAGE plpython3u;
SELECT * FROM composite_type_as_list_broken();
ERROR:  malformed record literal: "first"
DETAIL:  Missing left parenthesis.
HINT:  To return a composite type in an array, return the composite type as a Python tuple, e.g., "[('foo',)]".
CONTEXT:  while creating return value
PL/Python function "composite_type_as_list_broken"