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

Commit 7bacf2b

Browse files
committed
Add expected tuple descriptor to ReturnSetInfo information for table
functions, per suggestion from John Gray and Joe Conway. Also, fix plpgsql RETURN NEXT to verify that returned values match the expected tupdesc.
1 parent 9c27935 commit 7bacf2b

File tree

6 files changed

+113
-74
lines changed

6 files changed

+113
-74
lines changed

src/backend/executor/execQual.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/execQual.c,v 1.102 2002/08/30 00:28:41 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/execQual.c,v 1.103 2002/08/30 23:59:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -707,6 +707,7 @@ ExecMakeFunctionResult(FunctionCachePtr fcache,
707707
fcinfo.resultinfo = (Node *) &rsinfo;
708708
rsinfo.type = T_ReturnSetInfo;
709709
rsinfo.econtext = econtext;
710+
rsinfo.expectedDesc = NULL;
710711
rsinfo.allowedModes = (int) SFRM_ValuePerCall;
711712
rsinfo.returnMode = SFRM_ValuePerCall;
712713
/* isDone is filled below */
@@ -851,6 +852,7 @@ ExecMakeFunctionResult(FunctionCachePtr fcache,
851852
Tuplestorestate *
852853
ExecMakeTableFunctionResult(Expr *funcexpr,
853854
ExprContext *econtext,
855+
TupleDesc expectedDesc,
854856
TupleDesc *returnDesc)
855857
{
856858
Tuplestorestate *tupstore = NULL;
@@ -859,7 +861,7 @@ ExecMakeTableFunctionResult(Expr *funcexpr,
859861
List *argList;
860862
FunctionCachePtr fcache;
861863
FunctionCallInfoData fcinfo;
862-
ReturnSetInfo rsinfo; /* for functions returning sets */
864+
ReturnSetInfo rsinfo;
863865
ExprDoneCond argDone;
864866
MemoryContext callerContext;
865867
MemoryContext oldcontext;
@@ -918,17 +920,14 @@ ExecMakeTableFunctionResult(Expr *funcexpr,
918920
}
919921

920922
/*
921-
* If function returns set, prepare a resultinfo node for
922-
* communication
923+
* Prepare a resultinfo node for communication. We always do this even
924+
* if not expecting a set result, so that we can pass expectedDesc.
923925
*/
924-
if (fcache->func.fn_retset)
925-
{
926-
fcinfo.resultinfo = (Node *) &rsinfo;
927-
rsinfo.type = T_ReturnSetInfo;
928-
rsinfo.econtext = econtext;
929-
rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize);
930-
}
931-
/* we set these fields always since we examine them below */
926+
fcinfo.resultinfo = (Node *) &rsinfo;
927+
rsinfo.type = T_ReturnSetInfo;
928+
rsinfo.econtext = econtext;
929+
rsinfo.expectedDesc = expectedDesc;
930+
rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize);
932931
rsinfo.returnMode = SFRM_ValuePerCall;
933932
/* isDone is filled below */
934933
rsinfo.setResult = NULL;

src/backend/executor/nodeFunctionscan.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/nodeFunctionscan.c,v 1.8 2002/08/30 00:28:41 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/nodeFunctionscan.c,v 1.9 2002/08/30 23:59:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -79,6 +79,7 @@ FunctionNext(FunctionScan *node)
7979
scanstate->tuplestorestate = tuplestorestate =
8080
ExecMakeTableFunctionResult((Expr *) scanstate->funcexpr,
8181
econtext,
82+
scanstate->tupdesc,
8283
&funcTupdesc);
8384

8485
/*

src/backend/utils/fmgr/README

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ function is called in). The function stores pointers to the Tuplestore and
423423
TupleDesc into ReturnSetInfo, sets returnMode to indicate materialize mode,
424424
and returns null. isDone is not used and should be left at ExprSingleResult.
425425

426+
If the function is being called as a table function (ie, it appears in a
427+
FROM item), then the expected tuple descriptor is passed in ReturnSetInfo;
428+
in other contexts the expectedDesc field will be NULL. The function need
429+
not pay attention to expectedDesc, but it may be useful in special cases.
430+
426431
There is no support for functions accepting sets; instead, the function will
427432
be called multiple times, once for each element of the input set.
428433

src/include/executor/executor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: executor.h,v 1.75 2002/08/30 00:28:41 tgl Exp $
10+
* $Id: executor.h,v 1.76 2002/08/30 23:59:46 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -82,6 +82,7 @@ extern Datum ExecMakeFunctionResult(FunctionCachePtr fcache,
8282
ExprDoneCond *isDone);
8383
extern Tuplestorestate *ExecMakeTableFunctionResult(Expr *funcexpr,
8484
ExprContext *econtext,
85+
TupleDesc expectedDesc,
8586
TupleDesc *returnDesc);
8687
extern Datum ExecEvalExpr(Node *expression, ExprContext *econtext,
8788
bool *isNull, ExprDoneCond *isDone);

src/include/nodes/execnodes.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: execnodes.h,v 1.73 2002/08/30 00:28:41 tgl Exp $
10+
* $Id: execnodes.h,v 1.74 2002/08/30 23:59:46 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -147,12 +147,16 @@ typedef enum
147147
typedef struct ReturnSetInfo
148148
{
149149
NodeTag type;
150+
/* values set by caller: */
150151
ExprContext *econtext; /* context function is being called in */
152+
TupleDesc expectedDesc; /* tuple descriptor expected by caller */
151153
int allowedModes; /* bitmask: return modes caller can handle */
152-
SetFunctionReturnMode returnMode; /* actual return mode */
154+
/* result status from function (but pre-initialized by caller): */
155+
SetFunctionReturnMode returnMode; /* actual return mode */
153156
ExprDoneCond isDone; /* status for ValuePerCall mode */
154-
Tuplestorestate *setResult; /* return object for Materialize mode */
155-
TupleDesc setDesc; /* descriptor for Materialize mode */
157+
/* fields filled by function in Materialize return mode: */
158+
Tuplestorestate *setResult; /* holds the complete returned tuple set */
159+
TupleDesc setDesc; /* actual descriptor for returned tuples */
156160
} ReturnSetInfo;
157161

158162
/* ----------------

0 commit comments

Comments
 (0)