Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit78e81e1

Browse files
committed
Fix type-checking of RECORD-returning functions in FROM, redux.
Commit2ed8f9a intended to institute a policy that if aRangeTblFunction has a coldeflist, then the function return type iscertainly RECORD, and we should use the coldeflist as the source oftruth about what the columns of the record type are. When theoriginal function has been folded to a constant, inspection of theconstant might give a different answer. This situation will lead toa tuple-type-mismatch error at execution, but up until that point weneed to consistently believe the coldeflist, or we'll have problemsfrom different bits of code reaching different conclusions.expandRTE didn't get that memo though, and would try to produce atupdesc based on the constant in this situation, leading to anassertion failure. (Desultory testing suggests that non-assertbuilds often manage to give the expected error, although I alsosaw a "cache lookup failed for type 0" error, and it seems atleast possible that a crash could happen.)Some other callers of get_expr_result_type and get_expr_result_tupdescwere also being incautious about this. While none of them seem tohave actual bugs, they're working harder than necessary in this case,besides which it seems safest to have an explicit policy of not usingthose functions on an RTE with a coldeflist. Adjust the codeaccordingly, 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 commitd575347.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 parentad23af8 commit78e81e1

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

‎src/backend/optimizer/prep/prepjointree.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,10 @@ pull_up_constant_function(PlannerInfo *root, Node *jtnode,
17351735
if (rtf->funccolcount!=1)
17361736
returnjtnode;/* definitely composite */
17371737

1738+
/* If it has a coldeflist, it certainly returns RECORD */
1739+
if (rtf->funccolnames!=NIL)
1740+
returnjtnode;/* must be a one-column RECORD type */
1741+
17381742
functypclass=get_expr_result_type(rtf->funcexpr,
17391743
&funcrettype,
17401744
&tupdesc);

‎src/backend/optimizer/util/clauses.c

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

‎src/backend/parser/parse_relation.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,12 +2708,17 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
27082708
{
27092709
RangeTblFunction*rtfunc= (RangeTblFunction*)lfirst(lc);
27102710
TypeFuncClassfunctypclass;
2711-
Oidfuncrettype;
2712-
TupleDesctupdesc;
2711+
Oidfuncrettype=InvalidOid;
2712+
TupleDesctupdesc=NULL;
2713+
2714+
/* If it has a coldeflist, it returns RECORD */
2715+
if (rtfunc->funccolnames!=NIL)
2716+
functypclass=TYPEFUNC_RECORD;
2717+
else
2718+
functypclass=get_expr_result_type(rtfunc->funcexpr,
2719+
&funcrettype,
2720+
&tupdesc);
27132721

2714-
functypclass=get_expr_result_type(rtfunc->funcexpr,
2715-
&funcrettype,
2716-
&tupdesc);
27172722
if (functypclass==TYPEFUNC_COMPOSITE||
27182723
functypclass==TYPEFUNC_COMPOSITE_DOMAIN)
27192724
{
@@ -3327,6 +3332,10 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
33273332
{
33283333
TupleDesctupdesc;
33293334

3335+
/* If it has a coldeflist, it returns RECORD */
3336+
if (rtfunc->funccolnames!=NIL)
3337+
return false;/* can't have any dropped columns */
3338+
33303339
tupdesc=get_expr_result_tupdesc(rtfunc->funcexpr,
33313340
true);
33323341
if (tupdesc)

‎src/backend/utils/fmgr/funcapi.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ get_call_result_type(FunctionCallInfo fcinfo,
218218
/*
219219
* get_expr_result_type
220220
*As above, but work from a calling expression node tree
221+
*
222+
* Beware of using this on the funcexpr of a RTE that has a coldeflist.
223+
* The correct conclusion in such cases is always that the function returns
224+
* RECORD with the columns defined by the coldeflist fields (funccolnames etc).
225+
* If it does not, it's the executor's responsibility to catch the discrepancy
226+
* at runtime; but code processing the query in advance of that point might
227+
* come to inconsistent conclusions if it checks the actual expression.
221228
*/
222229
TypeFuncClass
223230
get_expr_result_type(Node*expr,
@@ -468,7 +475,8 @@ internal_get_result_type(Oid funcid,
468475
* if noError is true, else throws error.
469476
*
470477
* This is a simpler version of get_expr_result_type() for use when the caller
471-
* is only interested in determinate rowtype results.
478+
* is only interested in determinate rowtype results. As with that function,
479+
* beware of using this on the funcexpr of a RTE that has a coldeflist.
472480
*/
473481
TupleDesc
474482
get_expr_result_tupdesc(Node*expr,boolnoError)

‎src/test/regress/expected/rangefuncs.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,3 +2458,6 @@ with a(b) as (values (row(1,2,3)))
24582458
select * from a, coalesce(b) as c(d int, e int, f float); -- fail
24592459
ERROR: function return row and query-specified return row do not match
24602460
DETAIL: Returned type integer at ordinal position 3, but query expects double precision.
2461+
select * from int8_tbl, coalesce(row(1)) as (a int, b int); -- fail
2462+
ERROR: function return row and query-specified return row do not match
2463+
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
@@ -791,3 +791,4 @@ with a(b) as (values (row(1,2,3)))
791791
select*from a, coalesce(b)as c(dint, eint, fint, gint);-- fail
792792
with a(b)as (values (row(1,2,3)))
793793
select*from a, coalesce(b)as c(dint, eint, f float);-- fail
794+
select*from int8_tbl, coalesce(row(1))as (aint, bint);-- fail

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp