Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d727c54

Browse files
committed
Fix recursive RECORD-returning plpython functions.
If we recursed to a new call of the same function, with a different coldeflist (AS clause), it would fail because the inner call would overwrite the outer call's idea of what to return. This is vaguely like 1d2fe56 and c5bec54, but it's not due to any API decisions: it's just that we computed the actual output rowtype at the start of the call, and saved it in the per-procedure data structure. We can fix it at basically zero cost by doing the computation at the end of each call instead of the start. It's not clear that there's any real-world use-case for such a function, but given that it doesn't cost anything to fix, it'd be silly not to. Per report from Andreas Karlsson. Back-patch to all supported branches. Discussion: https://postgr.es/m/[email protected]
1 parent b538e69 commit d727c54

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

src/pl/plpython/expected/plpython_composite.out

+14
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,20 @@ SELECT * FROM return_record_2('v3') AS (v1 int, v2 int, v3 int);
569569
1 | 2 | 3
570570
(1 row)
571571

572+
-- recursion with a different inner result type didn't use to work
573+
CREATE FUNCTION return_record_3(t text) RETURNS record AS $$
574+
if t == "text":
575+
plpy.execute("SELECT * FROM return_record_3('int') AS (a int)");
576+
return { "a": "x" }
577+
elif t == "int":
578+
return { "a": 1 }
579+
$$ LANGUAGE plpython3u;
580+
SELECT * FROM return_record_3('text') AS (a text);
581+
a
582+
---
583+
x
584+
(1 row)
585+
572586
-- multi-dimensional array of composite types.
573587
CREATE FUNCTION composite_type_as_list() RETURNS type_record[] AS $$
574588
return [[('first', 1), ('second', 1)], [('first', 2), ('second', 2)], [('first', 3), ('second', 3)]];

src/pl/plpython/plpy_exec.c

+17-16
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,23 @@ PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedure *proc)
231231
}
232232
else
233233
{
234-
/* Normal conversion of result */
234+
/*
235+
* Normal conversion of result. However, if the result is of type
236+
* RECORD, we have to set up for that each time through, since it
237+
* might be different from last time.
238+
*/
239+
if (proc->result.typoid == RECORDOID)
240+
{
241+
TupleDesc desc;
242+
243+
if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
244+
ereport(ERROR,
245+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
246+
errmsg("function returning record called in context "
247+
"that cannot accept type record")));
248+
PLy_output_setup_record(&proc->result, desc, proc);
249+
}
250+
235251
rv = PLy_output_convert(&proc->result, plrv,
236252
&fcinfo->isnull);
237253
}
@@ -456,21 +472,6 @@ PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
456472
PLy_elog(ERROR, "PyDict_SetItemString() failed, while setting up arguments");
457473
arg = NULL;
458474
}
459-
460-
/* Set up output conversion for functions returning RECORD */
461-
if (proc->result.typoid == RECORDOID)
462-
{
463-
TupleDesc desc;
464-
465-
if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
466-
ereport(ERROR,
467-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
468-
errmsg("function returning record called in context "
469-
"that cannot accept type record")));
470-
471-
/* cache the output conversion functions */
472-
PLy_output_setup_record(&proc->result, desc, proc);
473-
}
474475
}
475476
PG_CATCH();
476477
{

src/pl/plpython/sql/plpython_composite.sql

+11
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ SELECT * FROM return_record_2('v4') AS (v1 int, v3 int, v2 int);
208208
SELECT * FROM return_record_2('v3') AS (v1 int, v3 int, v2 int);
209209
SELECT * FROM return_record_2('v3') AS (v1 int, v2 int, v3 int);
210210

211+
-- recursion with a different inner result type didn't use to work
212+
CREATE FUNCTION return_record_3(t text) RETURNS record AS $$
213+
if t == "text":
214+
plpy.execute("SELECT * FROM return_record_3('int') AS (a int)");
215+
return { "a": "x" }
216+
elif t == "int":
217+
return { "a": 1 }
218+
$$ LANGUAGE plpython3u;
219+
220+
SELECT * FROM return_record_3('text') AS (a text);
221+
211222
-- multi-dimensional array of composite types.
212223
CREATE FUNCTION composite_type_as_list() RETURNS type_record[] AS $$
213224
return [[('first', 1), ('second', 1)], [('first', 2), ('second', 2)], [('first', 3), ('second', 3)]];

0 commit comments

Comments
 (0)