Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e097086

Browse files
committed
Fix type-checking of RECORD-returning functions in FROM, redux.
Commit 2ed8f9a intended to institute a policy that if a RangeTblFunction has a coldeflist, then the function return type is certainly RECORD, and we should use the coldeflist as the source of truth about what the columns of the record type are. When the original function has been folded to a constant, inspection of the constant might give a different answer. This situation will lead to a tuple-type-mismatch error at execution, but up until that point we need to consistently believe the coldeflist, or we'll have problems from different bits of code reaching different conclusions. expandRTE didn't get that memo though, and would try to produce a tupdesc based on the constant in this situation, leading to an assertion failure. (Desultory testing suggests that non-assert builds often manage to give the expected error, although I also saw a "cache lookup failed for type 0" error, and it seems at least possible that a crash could happen.) Some other callers of get_expr_result_type and get_expr_result_tupdesc were also being incautious about this. While none of them seem to have actual bugs, they're working harder than necessary in this case, besides which it seems safest to have an explicit policy of not using those functions on an RTE with a coldeflist. Adjust the code accordingly, and add commentary to funcapi.c about this policy. Also fix an obsolete comment that claimed "get_expr_result_type() doesn't know how to extract type info from a RECORD constant". That hasn't been true since commit d575347. Per bug #18422 from Alexander Lakhin. As with the previous commit, back-patch to all supported branches. Discussion: https://postgr.es/m/18422-89ca86c8eac5246d@postgresql.org
1 parent 7924036 commit e097086

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4360,12 +4360,11 @@ evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
43604360
* Can't simplify if it returns RECORD. The immediate problem is that it
43614361
* will be needing an expected tupdesc which we can't supply here.
43624362
*
4363-
* In the case where it has OUT parameters, it could get by without an
4364-
* expected tupdesc, but we still have issues: get_expr_result_type()
4365-
* doesn't know how to extract type info from a RECORD constant, and in
4366-
* the case of a NULL function result there doesn't seem to be any clean
4367-
* way to fix that. In view of the likelihood of there being still other
4368-
* gotchas, seems best to leave the function call unreduced.
4363+
* In the case where it has OUT parameters, we could build an expected
4364+
* tupdesc from those, but there may be other gotchas lurking. In
4365+
* particular, if the function were to return NULL, we would produce a
4366+
* null constant with no remaining indication of which concrete record
4367+
* type it is. For now, seems best to leave the function call unreduced.
43694368
*/
43704369
if (funcform->prorettype == RECORDOID)
43714370
return NULL;

src/backend/parser/parse_relation.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,12 +2347,17 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
23472347
{
23482348
RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
23492349
TypeFuncClass functypclass;
2350-
Oid funcrettype;
2351-
TupleDesc tupdesc;
2350+
Oid funcrettype = InvalidOid;
2351+
TupleDesc tupdesc = NULL;
2352+
2353+
/* If it has a coldeflist, it returns RECORD */
2354+
if (rtfunc->funccolnames != NIL)
2355+
functypclass = TYPEFUNC_RECORD;
2356+
else
2357+
functypclass = get_expr_result_type(rtfunc->funcexpr,
2358+
&funcrettype,
2359+
&tupdesc);
23522360

2353-
functypclass = get_expr_result_type(rtfunc->funcexpr,
2354-
&funcrettype,
2355-
&tupdesc);
23562361
if (functypclass == TYPEFUNC_COMPOSITE ||
23572362
functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
23582363
{
@@ -3092,6 +3097,10 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
30923097
{
30933098
TupleDesc tupdesc;
30943099

3100+
/* If it has a coldeflist, it returns RECORD */
3101+
if (rtfunc->funccolnames != NIL)
3102+
return false; /* can't have any dropped columns */
3103+
30953104
tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr,
30963105
true);
30973106
if (tupdesc)

src/backend/utils/fmgr/funcapi.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ get_call_result_type(FunctionCallInfo fcinfo,
216216
/*
217217
* get_expr_result_type
218218
* As above, but work from a calling expression node tree
219+
*
220+
* Beware of using this on the funcexpr of a RTE that has a coldeflist.
221+
* The correct conclusion in such cases is always that the function returns
222+
* RECORD with the columns defined by the coldeflist fields (funccolnames etc).
223+
* If it does not, it's the executor's responsibility to catch the discrepancy
224+
* at runtime; but code processing the query in advance of that point might
225+
* come to inconsistent conclusions if it checks the actual expression.
219226
*/
220227
TypeFuncClass
221228
get_expr_result_type(Node *expr,
@@ -466,7 +473,8 @@ internal_get_result_type(Oid funcid,
466473
* if noError is true, else throws error.
467474
*
468475
* This is a simpler version of get_expr_result_type() for use when the caller
469-
* is only interested in determinate rowtype results.
476+
* is only interested in determinate rowtype results. As with that function,
477+
* beware of using this on the funcexpr of a RTE that has a coldeflist.
470478
*/
471479
TupleDesc
472480
get_expr_result_tupdesc(Node *expr, bool noError)

src/test/regress/expected/rangefuncs.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,3 +2111,6 @@ with a(b) as (values (row(1,2,3)))
21112111
select * from a, coalesce(b) as c(d int, e int, f float); -- fail
21122112
ERROR: function return row and query-specified return row do not match
21132113
DETAIL: Returned type integer at ordinal position 3, but query expects double precision.
2114+
select * from int8_tbl, coalesce(row(1)) as (a int, b int); -- fail
2115+
ERROR: function return row and query-specified return row do not match
2116+
DETAIL: Returned row contains 1 attribute, but query expects 2.

src/test/regress/sql/rangefuncs.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,4 @@ with a(b) as (values (row(1,2,3)))
665665
select * from a, coalesce(b) as c(d int, e int, f int, g int); -- fail
666666
with a(b) as (values (row(1,2,3)))
667667
select * from a, coalesce(b) as c(d int, e int, f float); -- fail
668+
select * from int8_tbl, coalesce(row(1)) as (a int, b int); -- fail

0 commit comments

Comments
 (0)