2 -- Test returning tuples
4 CREATE TABLE table_record (
8 CREATE TYPE type_record AS (
12 CREATE FUNCTION test_table_record_as(typ text, first text, second integer, retnull boolean) RETURNS table_record AS $$
16 return { 'first': first, 'second': second, 'additionalfield': 'must not cause trouble' }
18 return ( first, second )
20 return [ first, second ]
22 class type_record: pass
23 type_record.first = first
24 type_record.second = second
26 $$ LANGUAGE plpythonu;
27 CREATE FUNCTION test_type_record_as(typ text, first text, second integer, retnull boolean) RETURNS type_record AS $$
31 return { 'first': first, 'second': second, 'additionalfield': 'must not cause trouble' }
33 return ( first, second )
35 return [ first, second ]
37 class type_record: pass
38 type_record.first = first
39 type_record.second = second
42 return "('%s',%r)" % (first, second)
43 $$ LANGUAGE plpythonu;
44 CREATE FUNCTION test_in_out_params(first in text, second out text) AS $$
45 return first + '_in_to_out';
46 $$ LANGUAGE plpythonu;
47 CREATE FUNCTION test_in_out_params_multi(first in text,
48 second out text, third out text) AS $$
49 return (first + '_record_in_to_out_1', first + '_record_in_to_out_2');
50 $$ LANGUAGE plpythonu;
51 CREATE FUNCTION test_inout_params(first inout text) AS $$
52 return first + '_inout';
53 $$ LANGUAGE plpythonu;
54 -- Test tuple returning functions
55 SELECT * FROM test_table_record_as('dict', null, null, false);
61 SELECT * FROM test_table_record_as('dict', 'one', null, false);
67 SELECT * FROM test_table_record_as('dict', null, 2, false);
73 SELECT * FROM test_table_record_as('dict', 'three', 3, false);
79 SELECT * FROM test_table_record_as('dict', null, null, true);
85 SELECT * FROM test_table_record_as('tuple', null, null, false);
91 SELECT * FROM test_table_record_as('tuple', 'one', null, false);
97 SELECT * FROM test_table_record_as('tuple', null, 2, false);
103 SELECT * FROM test_table_record_as('tuple', 'three', 3, false);
109 SELECT * FROM test_table_record_as('tuple', null, null, true);
115 SELECT * FROM test_table_record_as('list', null, null, false);
121 SELECT * FROM test_table_record_as('list', 'one', null, false);
127 SELECT * FROM test_table_record_as('list', null, 2, false);
133 SELECT * FROM test_table_record_as('list', 'three', 3, false);
139 SELECT * FROM test_table_record_as('list', null, null, true);
145 SELECT * FROM test_table_record_as('obj', null, null, false);
151 SELECT * FROM test_table_record_as('obj', 'one', null, false);
157 SELECT * FROM test_table_record_as('obj', null, 2, false);
163 SELECT * FROM test_table_record_as('obj', 'three', 3, false);
169 SELECT * FROM test_table_record_as('obj', null, null, true);
175 SELECT * FROM test_type_record_as('dict', null, null, false);
181 SELECT * FROM test_type_record_as('dict', 'one', null, false);
187 SELECT * FROM test_type_record_as('dict', null, 2, false);
193 SELECT * FROM test_type_record_as('dict', 'three', 3, false);
199 SELECT * FROM test_type_record_as('dict', null, null, true);
205 SELECT * FROM test_type_record_as('tuple', null, null, false);
211 SELECT * FROM test_type_record_as('tuple', 'one', null, false);
217 SELECT * FROM test_type_record_as('tuple', null, 2, false);
223 SELECT * FROM test_type_record_as('tuple', 'three', 3, false);
229 SELECT * FROM test_type_record_as('tuple', null, null, true);
235 SELECT * FROM test_type_record_as('list', null, null, false);
241 SELECT * FROM test_type_record_as('list', 'one', null, false);
247 SELECT * FROM test_type_record_as('list', null, 2, false);
253 SELECT * FROM test_type_record_as('list', 'three', 3, false);
259 SELECT * FROM test_type_record_as('list', null, null, true);
265 SELECT * FROM test_type_record_as('obj', null, null, false);
271 SELECT * FROM test_type_record_as('obj', 'one', null, false);
277 SELECT * FROM test_type_record_as('obj', null, 2, false);
283 SELECT * FROM test_type_record_as('obj', 'three', 3, false);
289 SELECT * FROM test_type_record_as('obj', null, null, true);
295 SELECT * FROM test_type_record_as('str', 'one', 1, false);
301 SELECT * FROM test_in_out_params('test_in');
307 SELECT * FROM test_in_out_params_multi('test_in');
309 ----------------------------+----------------------------
310 test_in_record_in_to_out_1 | test_in_record_in_to_out_2
313 SELECT * FROM test_inout_params('test_in');
319 -- try changing the return types and call functions again
320 ALTER TABLE table_record DROP COLUMN first;
321 ALTER TABLE table_record DROP COLUMN second;
322 ALTER TABLE table_record ADD COLUMN first text;
323 ALTER TABLE table_record ADD COLUMN second int4;
324 SELECT * FROM test_table_record_as('obj', 'one', 1, false);
330 ALTER TYPE type_record DROP ATTRIBUTE first;
331 ALTER TYPE type_record DROP ATTRIBUTE second;
332 ALTER TYPE type_record ADD ATTRIBUTE first text;
333 ALTER TYPE type_record ADD ATTRIBUTE second int4;
334 SELECT * FROM test_type_record_as('obj', 'one', 1, false);
341 CREATE FUNCTION test_type_record_error1() RETURNS type_record AS $$
342 return { 'first': 'first' }
343 $$ LANGUAGE plpythonu;
344 SELECT * FROM test_type_record_error1();
345 ERROR: key "second" not found in mapping
346 HINT: To return null in a column, add the value None to the mapping with the key named after the column.
347 CONTEXT: while creating return value
348 PL/Python function "test_type_record_error1"
349 CREATE FUNCTION test_type_record_error2() RETURNS type_record AS $$
351 $$ LANGUAGE plpythonu;
352 SELECT * FROM test_type_record_error2();
353 ERROR: length of returned sequence did not match number of columns in row
354 CONTEXT: while creating return value
355 PL/Python function "test_type_record_error2"
356 CREATE FUNCTION test_type_record_error3() RETURNS type_record AS $$
357 class type_record: pass
358 type_record.first = 'first'
360 $$ LANGUAGE plpythonu;
361 SELECT * FROM test_type_record_error3();
362 ERROR: attribute "second" does not exist in Python object
363 HINT: To return null in a column, let the returned object have an attribute named after column with value None.
364 CONTEXT: while creating return value
365 PL/Python function "test_type_record_error3"
366 CREATE FUNCTION test_type_record_error4() RETURNS type_record AS $$
368 $$ LANGUAGE plpythonu;
369 SELECT * FROM test_type_record_error4();
370 ERROR: malformed record literal: "foo"
371 DETAIL: Missing left parenthesis.
372 CONTEXT: while creating return value
373 PL/Python function "test_type_record_error4"