1998-02-13 04:26:53 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* nodeSubplan.c
|
1998-02-13 04:26:53 +01:00
|
|
|
* routines to support subselects
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* INTERFACE ROUTINES
|
1998-02-26 05:46:47 +01:00
|
|
|
* ExecSubPlan - process a subselect
|
1998-02-13 04:26:53 +01:00
|
|
|
* ExecInitSubPlan - initialize a subselect
|
1998-02-26 05:46:47 +01:00
|
|
|
* ExecEndSubPlan - shut down a subselect
|
1998-02-13 04:26:53 +01:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "executor/nodeSubplan.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "tcop/pquery.h"
|
1998-02-13 04:26:53 +01:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecSubPlan(node)
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
Plan *plan = node->plan;
|
|
|
|
SubLink *sublink = node->sublink;
|
1999-04-19 06:17:11 +02:00
|
|
|
SubLinkType subLinkType = sublink->subLinkType;
|
1998-02-13 04:26:53 +01:00
|
|
|
TupleTableSlot *slot;
|
1998-02-26 05:46:47 +01:00
|
|
|
List *lst;
|
1999-04-19 06:17:11 +02:00
|
|
|
Datum result = (Datum) false;
|
|
|
|
bool found = false; /* TRUE if got at least one subplan tuple */
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
if (node->setParam != NULL)
|
|
|
|
elog(ERROR, "ExecSubPlan: can't set parent params from subquery");
|
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
/*
|
|
|
|
* Set Params of this plan from parent plan correlation Vars
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
if (node->parParam != NULL)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
foreach(lst, node->parParam)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ParamExecData *prm = &(econtext->ecxt_param_exec_vals[lfirsti(lst)]);
|
|
|
|
|
|
|
|
prm->value = ExecEvalExpr((Node *) lfirst(pvar),
|
|
|
|
econtext,
|
|
|
|
&(prm->isnull), NULL);
|
|
|
|
pvar = lnext(pvar);
|
1998-02-13 04:26:53 +01:00
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
plan->chgParam = nconc(plan->chgParam, listCopy(node->parParam));
|
1998-02-13 04:26:53 +01:00
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
ExecReScan(plan, (ExprContext *) NULL, plan);
|
|
|
|
|
1999-04-19 06:17:11 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* For all sublink types except EXPR_SUBLINK, the result type is
|
|
|
|
* boolean, and we have a fairly clear idea of how to combine multiple
|
|
|
|
* subitems and deal with NULL values or an empty subplan result.
|
1999-04-19 06:17:11 +02:00
|
|
|
*
|
|
|
|
* For EXPR_SUBLINK, the result type is whatever the combining operator
|
|
|
|
* returns. We have no way to deal with more than one column in the
|
1999-05-25 18:15:34 +02:00
|
|
|
* subplan result --- hopefully the parser forbids that. More
|
|
|
|
* seriously, it's unclear what to do with NULL values or an empty
|
|
|
|
* subplan result. For now, we error out, but should something else
|
|
|
|
* happen?
|
1999-04-19 06:17:11 +02:00
|
|
|
*/
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
for (slot = ExecProcNode(plan, plan);
|
|
|
|
!TupIsNull(slot);
|
|
|
|
slot = ExecProcNode(plan, plan))
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
|
|
|
HeapTuple tup = slot->val;
|
|
|
|
TupleDesc tdesc = slot->ttc_tupleDescriptor;
|
|
|
|
int i = 1;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1999-04-19 06:17:11 +02:00
|
|
|
if (subLinkType == EXPR_SUBLINK && found)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
elog(ERROR, "ExecSubPlan: more than one tuple returned by expression subselect");
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Datum) false;
|
1998-02-13 04:26:53 +01:00
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1999-04-19 06:17:11 +02:00
|
|
|
if (subLinkType == EXISTS_SUBLINK)
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Datum) true;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
found = true;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
foreach(lst, sublink->oper)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
Expr *expr = (Expr *) lfirst(lst);
|
|
|
|
Const *con = lsecond(expr->args);
|
|
|
|
bool isnull;
|
|
|
|
|
1999-08-26 01:21:43 +02:00
|
|
|
/*
|
|
|
|
* The righthand side of the expression should be either a Const
|
|
|
|
* or a function call taking a Const as arg (the function would
|
|
|
|
* be a run-time type coercion inserted by the parser to get to
|
|
|
|
* the input type needed by the operator). Find the Const node
|
|
|
|
* and insert the actual righthand side value into it.
|
|
|
|
*/
|
|
|
|
if (! IsA(con, Const))
|
|
|
|
{
|
|
|
|
Assert(IsA(con, Expr));
|
|
|
|
con = lfirst(((Expr *) con)->args);
|
|
|
|
Assert(IsA(con, Const));
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
con->constvalue = heap_getattr(tup, i, tdesc, &(con->constisnull));
|
1999-08-26 01:21:43 +02:00
|
|
|
/*
|
|
|
|
* Now we can eval the expression.
|
|
|
|
*/
|
|
|
|
result = ExecEvalExpr((Node *) expr, econtext, &isnull,
|
|
|
|
(bool *) NULL);
|
1998-02-26 05:46:47 +01:00
|
|
|
if (isnull)
|
1999-04-19 06:17:11 +02:00
|
|
|
{
|
|
|
|
if (subLinkType == EXPR_SUBLINK)
|
|
|
|
elog(ERROR, "ExecSubPlan: null value returned by expression subselect");
|
|
|
|
else
|
|
|
|
result = (Datum) false;
|
|
|
|
}
|
|
|
|
if (subLinkType != EXPR_SUBLINK)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
if ((!(bool) result && !(sublink->useor)) ||
|
1999-04-19 06:17:11 +02:00
|
|
|
((bool) result && sublink->useor))
|
|
|
|
break;
|
|
|
|
}
|
1998-02-13 04:26:53 +01:00
|
|
|
i++;
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
if (subLinkType == ALL_SUBLINK && !(bool) result)
|
1999-04-19 06:17:11 +02:00
|
|
|
break;
|
|
|
|
if (subLinkType == ANY_SUBLINK && (bool) result)
|
1998-02-13 04:26:53 +01:00
|
|
|
break;
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1999-04-19 06:17:11 +02:00
|
|
|
if (!found)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
/* deal with empty subplan result. Note default result is 'false' */
|
1999-04-19 06:17:11 +02:00
|
|
|
if (subLinkType == ALL_SUBLINK)
|
|
|
|
result = (Datum) true;
|
|
|
|
else if (subLinkType == EXPR_SUBLINK)
|
|
|
|
elog(ERROR, "ExecSubPlan: no tuples returned by expression subselect");
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1999-04-19 06:17:11 +02:00
|
|
|
return result;
|
1998-02-13 04:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecInitSubPlan
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1998-02-27 09:43:53 +01:00
|
|
|
extern void ExecCheckPerms(CmdType op, int resRel, List *rtable, Query *q);
|
1998-02-13 04:26:53 +01:00
|
|
|
bool
|
|
|
|
ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
EState *sp_estate = CreateExecutorState();
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
ExecCheckPerms(CMD_SELECT, 0, node->rtable, (Query *) NULL);
|
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
sp_estate->es_range_table = node->rtable;
|
|
|
|
sp_estate->es_param_list_info = estate->es_param_list_info;
|
|
|
|
sp_estate->es_param_exec_vals = estate->es_param_exec_vals;
|
1998-02-26 05:46:47 +01:00
|
|
|
sp_estate->es_tupleTable =
|
|
|
|
ExecCreateTupleTable(ExecCountSlotsNode(node->plan) + 10);
|
1999-02-01 14:33:27 +01:00
|
|
|
sp_estate->es_snapshot = estate->es_snapshot;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
if (!ExecInitNode(node->plan, sp_estate, NULL))
|
1998-09-01 05:29:17 +02:00
|
|
|
return false;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
node->shutdown = true;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
/*
|
1998-02-26 05:46:47 +01:00
|
|
|
* If this plan is un-correlated or undirect correlated one and want
|
|
|
|
* to set params for parent plan then prepare parameters.
|
1998-02-13 04:26:53 +01:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
if (node->setParam != NULL)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
List *lst;
|
|
|
|
|
|
|
|
foreach(lst, node->setParam)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ParamExecData *prm = &(estate->es_param_exec_vals[lfirsti(lst)]);
|
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
prm->execPlan = node;
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
/*
|
|
|
|
* Note that in the case of un-correlated subqueries we don't care
|
1998-02-26 05:46:47 +01:00
|
|
|
* about setting parent->chgParam here: indices take care about
|
|
|
|
* it, for others - it doesn't matter...
|
1998-02-13 04:26:53 +01:00
|
|
|
*/
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return true;
|
1998-02-13 04:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecSetParamPlan
|
|
|
|
*
|
|
|
|
* Executes plan of node and sets parameters.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
1998-02-26 05:46:47 +01:00
|
|
|
ExecSetParamPlan(SubPlan *node)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
Plan *plan = node->plan;
|
|
|
|
SubLink *sublink = node->sublink;
|
1998-02-13 04:26:53 +01:00
|
|
|
TupleTableSlot *slot;
|
1998-02-26 05:46:47 +01:00
|
|
|
List *lst;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
if (sublink->subLinkType == ANY_SUBLINK ||
|
|
|
|
sublink->subLinkType == ALL_SUBLINK)
|
|
|
|
elog(ERROR, "ExecSetParamPlan: ANY/ALL subselect unsupported");
|
|
|
|
|
|
|
|
if (plan->chgParam != NULL)
|
|
|
|
ExecReScan(plan, (ExprContext *) NULL, plan);
|
|
|
|
|
|
|
|
for (slot = ExecProcNode(plan, plan);
|
|
|
|
!TupIsNull(slot);
|
|
|
|
slot = ExecProcNode(plan, plan))
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
|
|
|
HeapTuple tup = slot->val;
|
|
|
|
TupleDesc tdesc = slot->ttc_tupleDescriptor;
|
|
|
|
int i = 1;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
if (sublink->subLinkType == EXPR_SUBLINK && found)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
elog(ERROR, "ExecSetParamPlan: more than one tuple returned by expression subselect");
|
1998-02-13 04:26:53 +01:00
|
|
|
return;
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
found = true;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
if (sublink->subLinkType == EXISTS_SUBLINK)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(node->setParam)]);
|
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
prm->execPlan = NULL;
|
|
|
|
prm->value = (Datum) true;
|
|
|
|
prm->isnull = false;
|
|
|
|
break;
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
/*
|
1998-02-19 05:47:07 +01:00
|
|
|
* If this is uncorrelated subquery then its plan will be closed
|
|
|
|
* (see below) and this tuple will be free-ed - bad for not byval
|
|
|
|
* types... But is free-ing possible in the next ExecProcNode in
|
1998-02-26 05:46:47 +01:00
|
|
|
* this loop ? Who knows... Someday we'll keep track of saved
|
1998-02-19 05:47:07 +01:00
|
|
|
* tuples...
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
tup = heap_copytuple(tup);
|
|
|
|
|
|
|
|
foreach(lst, node->setParam)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(lst)]);
|
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
prm->execPlan = NULL;
|
1998-02-26 05:46:47 +01:00
|
|
|
prm->value = heap_getattr(tup, i, tdesc, &(prm->isnull));
|
1998-02-13 04:26:53 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
if (!found)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
if (sublink->subLinkType == EXISTS_SUBLINK)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(node->setParam)]);
|
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
prm->execPlan = NULL;
|
|
|
|
prm->value = (Datum) false;
|
|
|
|
prm->isnull = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
foreach(lst, node->setParam)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(lst)]);
|
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
prm->execPlan = NULL;
|
|
|
|
prm->value = (Datum) NULL;
|
|
|
|
prm->isnull = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
if (plan->extParam == NULL) /* un-correlated ... */
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ExecEndNode(plan, plan);
|
1998-02-13 04:26:53 +01:00
|
|
|
node->shutdown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecEndSubPlan
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ExecEndSubPlan(SubPlan *node)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
if (node->shutdown)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ExecEndNode(node->plan, node->plan);
|
1998-02-13 04:26:53 +01:00
|
|
|
node->shutdown = false;
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
}
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
void
|
|
|
|
ExecReScanSetParamPlan(SubPlan *node, Plan *parent)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
Plan *plan = node->plan;
|
|
|
|
List *lst;
|
|
|
|
|
|
|
|
if (node->parParam != NULL)
|
|
|
|
elog(ERROR, "ExecReScanSetParamPlan: direct correlated subquery unsupported, yet");
|
|
|
|
if (node->setParam == NULL)
|
|
|
|
elog(ERROR, "ExecReScanSetParamPlan: setParam list is NULL");
|
|
|
|
if (plan->extParam == NULL)
|
|
|
|
elog(ERROR, "ExecReScanSetParamPlan: extParam list of plan is NULL");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't actual re-scan: ExecSetParamPlan does re-scan if
|
|
|
|
* node->plan->chgParam is not NULL... ExecReScan (plan, NULL, plan);
|
1998-02-13 04:26:53 +01:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
foreach(lst, node->setParam)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(lst)]);
|
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
prm->execPlan = node;
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
parent->chgParam = nconc(parent->chgParam, listCopy(node->setParam));
|
1998-02-13 04:26:53 +01:00
|
|
|
|
|
|
|
}
|