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

Commitb6e21ce

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 parent164c945 commitb6e21ce

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
@@ -1736,6 +1736,10 @@ pull_up_constant_function(PlannerInfo *root, Node *jtnode,
17361736
if (rtf->funccolcount!=1)
17371737
returnjtnode;/* definitely composite */
17381738

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

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

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

‎src/backend/parser/parse_relation.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,12 +2627,17 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
26272627
{
26282628
RangeTblFunction*rtfunc= (RangeTblFunction*)lfirst(lc);
26292629
TypeFuncClassfunctypclass;
2630-
Oidfuncrettype;
2631-
TupleDesctupdesc;
2630+
Oidfuncrettype=InvalidOid;
2631+
TupleDesctupdesc=NULL;
2632+
2633+
/* If it has a coldeflist, it returns RECORD */
2634+
if (rtfunc->funccolnames!=NIL)
2635+
functypclass=TYPEFUNC_RECORD;
2636+
else
2637+
functypclass=get_expr_result_type(rtfunc->funcexpr,
2638+
&funcrettype,
2639+
&tupdesc);
26322640

2633-
functypclass=get_expr_result_type(rtfunc->funcexpr,
2634-
&funcrettype,
2635-
&tupdesc);
26362641
if (functypclass==TYPEFUNC_COMPOSITE||
26372642
functypclass==TYPEFUNC_COMPOSITE_DOMAIN)
26382643
{
@@ -3242,6 +3247,10 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
32423247
{
32433248
TupleDesctupdesc;
32443249

3250+
/* If it has a coldeflist, it returns RECORD */
3251+
if (rtfunc->funccolnames!=NIL)
3252+
return false;/* can't have any dropped columns */
3253+
32453254
tupdesc=get_expr_result_tupdesc(rtfunc->funcexpr,
32463255
true);
32473256
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,boolnoError)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,3 +2445,6 @@ with a(b) as (values (row(1,2,3)))
24452445
select * from a, coalesce(b) as c(d int, e int, f float); -- fail
24462446
ERROR: function return row and query-specified return row do not match
24472447
DETAIL: Returned type integer at ordinal position 3, but query expects double precision.
2448+
select * from int8_tbl, coalesce(row(1)) as (a int, b int); -- fail
2449+
ERROR: function return row and query-specified return row do not match
2450+
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
@@ -786,3 +786,4 @@ with a(b) as (values (row(1,2,3)))
786786
select*from a, coalesce(b)as c(dint, eint, fint, gint);-- fail
787787
with a(b)as (values (row(1,2,3)))
788788
select*from a, coalesce(b)as c(dint, eint, f float);-- fail
789+
select*from int8_tbl, coalesce(row(1))as (aint, bint);-- fail

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp