Commit f68e11f3 authored by Tom Lane's avatar Tom Lane

Implement subselects in target lists. Also, relax requirement that

subselects can only appear on the righthand side of a binary operator.
That's still true for quantified predicates like x = ANY (SELECT ...),
but a subselect that delivers a single result can now appear anywhere
in an expression.  This is implemented by changing EXPR_SUBLINK sublinks
to represent just the (SELECT ...) expression, without any 'left hand
side' or combining operator --- so they're now more like EXISTS_SUBLINK.
To handle the case of '(x, y, z) = (SELECT ...)', I added a new sublink
type MULTIEXPR_SUBLINK, which acts just like EXPR_SUBLINK used to.
But the grammar will only generate one for a multiple-left-hand-side
row expression.
parent 1ecb129d
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
* nodeSubplan.c * nodeSubplan.c
* routines to support subselects * routines to support subselects
* *
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/nodeSubplan.c,v 1.16 1999/11/15 02:00:01 tgl Exp $
*
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
/* /*
...@@ -68,19 +73,17 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull) ...@@ -68,19 +73,17 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull)
* within a tuple (if there are multiple columns) using OR semantics * within a tuple (if there are multiple columns) using OR semantics
* if "useor" is true, AND semantics if not. We then combine results * if "useor" is true, AND semantics if not. We then combine results
* across tuples (if the subplan produces more than one) using OR * across tuples (if the subplan produces more than one) using OR
* semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK. NULL * semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK.
* results from the combining operators are handled according to the * (MULTIEXPR_SUBLINK doesn't allow multiple tuples from the subplan.)
* usual SQL semantics for OR and AND. The result for no input * NULL results from the combining operators are handled according to
* tuples is FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK. * the usual SQL semantics for OR and AND. The result for no input
* tuples is FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK, NULL for
* MULTIEXPR_SUBLINK.
* *
* For EXPR_SUBLINK we require the subplan to produce no more than one * For EXPR_SUBLINK we require the subplan to produce no more than one
* tuple, else an error is raised. If zero tuples are produced, we * tuple, else an error is raised. If zero tuples are produced, we
* return NULL. (XXX it would probably be more correct to evaluate * return NULL. Assuming we get a tuple, we just return its first
* the combining operator with a NULL input?) Assuming we get a tuple: * column (there can be only one non-junk column in this case).
* if there is only one column then we just return its result as-is, NULL
* or otherwise. If there is more than one column we combine the results
* per "useor" --- this only makes sense if the combining operators yield
* boolean, and we assume the parser has checked that.
*/ */
result = (Datum) (subLinkType == ALL_SUBLINK ? true : false); result = (Datum) (subLinkType == ALL_SUBLINK ? true : false);
*isNull = false; *isNull = false;
...@@ -98,13 +101,29 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull) ...@@ -98,13 +101,29 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull)
if (subLinkType == EXISTS_SUBLINK) if (subLinkType == EXISTS_SUBLINK)
return (Datum) true; return (Datum) true;
/* cannot allow multiple input tuples for EXPR sublink */ if (subLinkType == EXPR_SUBLINK)
if (subLinkType == EXPR_SUBLINK && found) {
/* cannot allow multiple input tuples for EXPR sublink */
if (found)
elog(ERROR, "ExecSubPlan: more than one tuple returned by expression subselect");
found = true;
/* XXX need to copy tuple in case pass by ref */
/* XXX need to ref-count the tuple to avoid mem leak! */
tup = heap_copytuple(tup);
result = heap_getattr(tup, col, tdesc, isNull);
/* keep scanning subplan to make sure there's only one tuple */
continue;
}
/* cannot allow multiple input tuples for MULTIEXPR sublink either */
if (subLinkType == MULTIEXPR_SUBLINK && found)
elog(ERROR, "ExecSubPlan: more than one tuple returned by expression subselect"); elog(ERROR, "ExecSubPlan: more than one tuple returned by expression subselect");
found = true; found = true;
/* iterate over combining operators for columns of tuple */ /* For ALL, ANY, and MULTIEXPR sublinks, iterate over combining
* operators for columns of tuple.
*/
foreach(lst, sublink->oper) foreach(lst, sublink->oper)
{ {
Expr *expr = (Expr *) lfirst(lst); Expr *expr = (Expr *) lfirst(lst);
...@@ -193,7 +212,7 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull) ...@@ -193,7 +212,7 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull)
} }
else else
{ {
/* must be EXPR_SUBLINK */ /* must be MULTIEXPR_SUBLINK */
result = rowresult; result = rowresult;
*isNull = rownull; *isNull = rownull;
} }
...@@ -202,9 +221,10 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull) ...@@ -202,9 +221,10 @@ ExecSubPlan(SubPlan *node, List *pvar, ExprContext *econtext, bool *isNull)
if (!found) if (!found)
{ {
/* deal with empty subplan result. result/isNull were previously /* deal with empty subplan result. result/isNull were previously
* initialized correctly for all sublink types except EXPR. * initialized correctly for all sublink types except EXPR and
* MULTIEXPR; for those, return NULL.
*/ */
if (subLinkType == EXPR_SUBLINK) if (subLinkType == EXPR_SUBLINK || subLinkType == MULTIEXPR_SUBLINK)
{ {
result = (Datum) false; result = (Datum) false;
*isNull = true; *isNull = true;
...@@ -242,7 +262,7 @@ ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent) ...@@ -242,7 +262,7 @@ ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent)
* If this plan is un-correlated or undirect correlated one and want * If this plan is un-correlated or undirect correlated one and want
* to set params for parent plan then prepare parameters. * to set params for parent plan then prepare parameters.
*/ */
if (node->setParam != NULL) if (node->setParam != NIL)
{ {
List *lst; List *lst;
...@@ -293,14 +313,6 @@ ExecSetParamPlan(SubPlan *node) ...@@ -293,14 +313,6 @@ ExecSetParamPlan(SubPlan *node)
TupleDesc tdesc = slot->ttc_tupleDescriptor; TupleDesc tdesc = slot->ttc_tupleDescriptor;
int i = 1; int i = 1;
if (sublink->subLinkType == EXPR_SUBLINK && found)
{
elog(ERROR, "ExecSetParamPlan: more than one tuple returned by expression subselect");
return;
}
found = true;
if (sublink->subLinkType == EXISTS_SUBLINK) if (sublink->subLinkType == EXISTS_SUBLINK)
{ {
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(node->setParam)]); ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(node->setParam)]);
...@@ -308,9 +320,17 @@ ExecSetParamPlan(SubPlan *node) ...@@ -308,9 +320,17 @@ ExecSetParamPlan(SubPlan *node)
prm->execPlan = NULL; prm->execPlan = NULL;
prm->value = (Datum) true; prm->value = (Datum) true;
prm->isnull = false; prm->isnull = false;
found = true;
break; break;
} }
if (found &&
(sublink->subLinkType == EXPR_SUBLINK ||
sublink->subLinkType == MULTIEXPR_SUBLINK))
elog(ERROR, "ExecSetParamPlan: more than one tuple returned by expression subselect");
found = true;
/* /*
* If this is uncorrelated subquery then its plan will be closed * If this is uncorrelated subquery then its plan will be closed
* (see below) and this tuple will be free-ed - bad for not byval * (see below) and this tuple will be free-ed - bad for not byval
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.94 1999/11/01 05:15:13 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.95 1999/11/15 02:00:01 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -86,10 +86,11 @@ CopyPlanFields(Plan *from, Plan *newnode) ...@@ -86,10 +86,11 @@ CopyPlanFields(Plan *from, Plan *newnode)
newnode->locParam = listCopy(from->locParam); newnode->locParam = listCopy(from->locParam);
newnode->chgParam = listCopy(from->chgParam); newnode->chgParam = listCopy(from->chgParam);
Node_Copy(from, newnode, initPlan); Node_Copy(from, newnode, initPlan);
if (from->subPlan != NULL) if (from->subPlan != NIL)
newnode->subPlan = SS_pull_subplan((Node *) newnode->qual); newnode->subPlan = nconc(SS_pull_subplan((Node *) newnode->targetlist),
SS_pull_subplan((Node *) newnode->qual));
else else
newnode->subPlan = NULL; newnode->subPlan = NIL;
newnode->nParamExec = from->nParamExec; newnode->nParamExec = from->nParamExec;
} }
...@@ -137,8 +138,9 @@ _copyResult(Result *from) ...@@ -137,8 +138,9 @@ _copyResult(Result *from)
* We must add subplans in resconstantqual to the new plan's subPlan * We must add subplans in resconstantqual to the new plan's subPlan
* list * list
*/ */
newnode->plan.subPlan = nconc(newnode->plan.subPlan, if (from->plan.subPlan != NIL)
SS_pull_subplan(newnode->resconstantqual)); newnode->plan.subPlan = nconc(newnode->plan.subPlan,
SS_pull_subplan(newnode->resconstantqual));
return newnode; return newnode;
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.46 1999/10/07 04:23:06 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.47 1999/11/15 02:00:07 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -87,9 +87,25 @@ query_planner(Query *root, ...@@ -87,9 +87,25 @@ query_planner(Query *root,
tlist = (List *) SS_replace_correlation_vars((Node *) tlist); tlist = (List *) SS_replace_correlation_vars((Node *) tlist);
qual = (List *) SS_replace_correlation_vars((Node *) qual); qual = (List *) SS_replace_correlation_vars((Node *) qual);
} }
/* Expand SubLinks to SubPlans */ /* Expand SubLinks to SubPlans */
if (root->hasSubLinks) if (root->hasSubLinks)
{
tlist = (List *) SS_process_sublinks((Node *) tlist);
qual = (List *) SS_process_sublinks((Node *) qual); qual = (List *) SS_process_sublinks((Node *) qual);
if (root->groupClause != NIL)
{
/*
* Check for ungrouped variables passed to subplans.
* Note we do NOT do this for subplans in WHERE; it's legal
* there because WHERE is evaluated pre-GROUP.
*/
if (check_subplans_for_ungrouped_vars((Node *) tlist,
root->groupClause,
tlist))
elog(ERROR, "Sub-SELECT must use only GROUPed attributes from outer SELECT");
}
}
/* /*
* If the query contains no relation references at all, it must be * If the query contains no relation references at all, it must be
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.70 1999/10/07 04:23:06 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.71 1999/11/15 02:00:08 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -343,17 +343,11 @@ union_planner(Query *parse) ...@@ -343,17 +343,11 @@ union_planner(Query *parse)
{ {
/* Expand SubLinks to SubPlans */ /* Expand SubLinks to SubPlans */
parse->havingQual = SS_process_sublinks(parse->havingQual); parse->havingQual = SS_process_sublinks(parse->havingQual);
/* Check for ungrouped variables passed to subplans */
/*
* Check for ungrouped variables passed to subplans. (Probably
* this should be done for the targetlist as well??? But we
* should NOT do it for the WHERE qual, since WHERE is
* evaluated pre-GROUP.)
*/
if (check_subplans_for_ungrouped_vars(parse->havingQual, if (check_subplans_for_ungrouped_vars(parse->havingQual,
parse->groupClause, parse->groupClause,
parse->targetList)) parse->targetList))
elog(ERROR, "Sub-SELECT in HAVING clause must use only GROUPed attributes from outer SELECT"); elog(ERROR, "Sub-SELECT must use only GROUPed attributes from outer SELECT");
} }
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.24 1999/08/25 23:21:39 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.25 1999/11/15 02:00:08 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -173,10 +173,43 @@ make_subplan(SubLink *slink) ...@@ -173,10 +173,43 @@ make_subplan(SubLink *slink)
} }
/* /*
* Un-correlated or undirect correlated plans of EXISTS or EXPR types * Un-correlated or undirect correlated plans of EXISTS, EXPR, or
* can be used as initPlans... * MULTIEXPR types can be used as initPlans. For EXISTS or EXPR,
* we just produce a Param referring to the result of evaluating the
* initPlan. For MULTIEXPR, we must build an AND or OR-clause of the
* individual comparison operators, using the appropriate lefthand
* side expressions and Params for the initPlan's target items.
*/ */
if (node->parParam == NULL && slink->subLinkType == EXPR_SUBLINK) if (node->parParam == NIL && slink->subLinkType == EXISTS_SUBLINK)
{
Var *var = makeVar(0, 0, BOOLOID, -1, 0);
Param *prm = makeNode(Param);
prm->paramkind = PARAM_EXEC;
prm->paramid = (AttrNumber) new_param(var, PlannerQueryLevel);
prm->paramtype = var->vartype;
pfree(var); /* var is only needed for new_param */
node->setParam = lappendi(node->setParam, prm->paramid);
PlannerInitPlan = lappend(PlannerInitPlan, node);
result = (Node *) prm;
}
else if (node->parParam == NIL && slink->subLinkType == EXPR_SUBLINK)
{
TargetEntry *te = lfirst(plan->targetlist);
/* need a var node just to pass to new_param()... */
Var *var = makeVar(0, 0, te->resdom->restype,
te->resdom->restypmod, 0);
Param *prm = makeNode(Param);
prm->paramkind = PARAM_EXEC;
prm->paramid = (AttrNumber) new_param(var, PlannerQueryLevel);
prm->paramtype = var->vartype;
pfree(var); /* var is only needed for new_param */
node->setParam = lappendi(node->setParam, prm->paramid);
PlannerInitPlan = lappend(PlannerInitPlan, node);
result = (Node *) prm;
}
else if (node->parParam == NIL && slink->subLinkType == MULTIEXPR_SUBLINK)
{ {
List *newoper = NIL; List *newoper = NIL;
int i = 0; int i = 0;
...@@ -202,6 +235,7 @@ make_subplan(SubLink *slink) ...@@ -202,6 +235,7 @@ make_subplan(SubLink *slink)
prm->paramkind = PARAM_EXEC; prm->paramkind = PARAM_EXEC;
prm->paramid = (AttrNumber) new_param(var, PlannerQueryLevel); prm->paramid = (AttrNumber) new_param(var, PlannerQueryLevel);
prm->paramtype = var->vartype; prm->paramtype = var->vartype;
pfree(var); /* var is only needed for new_param */
Assert(IsA(oper, Oper)); Assert(IsA(oper, Oper));
tup = get_operator_tuple(oper->opno); tup = get_operator_tuple(oper->opno);
...@@ -219,7 +253,6 @@ make_subplan(SubLink *slink) ...@@ -219,7 +253,6 @@ make_subplan(SubLink *slink)
(Var *) left, (Var *) left,
(Var *) right)); (Var *) right));
node->setParam = lappendi(node->setParam, prm->paramid); node->setParam = lappendi(node->setParam, prm->paramid);
pfree(var);
i++; i++;
} }
slink->oper = newoper; slink->oper = newoper;
...@@ -231,19 +264,6 @@ make_subplan(SubLink *slink) ...@@ -231,19 +264,6 @@ make_subplan(SubLink *slink)
else else
result = (Node *) lfirst(newoper); result = (Node *) lfirst(newoper);
} }
else if (node->parParam == NULL && slink->subLinkType == EXISTS_SUBLINK)
{
Var *var = makeVar(0, 0, BOOLOID, -1, 0);
Param *prm = makeNode(Param);
prm->paramkind = PARAM_EXEC;
prm->paramid = (AttrNumber) new_param(var, PlannerQueryLevel);
prm->paramtype = var->vartype;
node->setParam = lappendi(node->setParam, prm->paramid);
pfree(var);
PlannerInitPlan = lappend(PlannerInitPlan, node);
result = (Node *) prm;
}
else else
{ {
/* make expression of SUBPLAN type */ /* make expression of SUBPLAN type */
...@@ -333,7 +353,8 @@ set_unioni(List *l1, List *l2) ...@@ -333,7 +353,8 @@ set_unioni(List *l1, List *l2)
/* /*
* finalize_primnode: build lists of subplans and params appearing * finalize_primnode: build lists of subplans and params appearing
* in the given expression tree. * in the given expression tree. NOTE: items are added to lists passed in,
* so caller must initialize lists to NIL before first call!
*/ */
typedef struct finalize_primnode_results { typedef struct finalize_primnode_results {
...@@ -341,20 +362,8 @@ typedef struct finalize_primnode_results { ...@@ -341,20 +362,8 @@ typedef struct finalize_primnode_results {
List *paramids; /* List of PARAM_EXEC paramids found */ List *paramids; /* List of PARAM_EXEC paramids found */
} finalize_primnode_results; } finalize_primnode_results;
static bool finalize_primnode_walker(Node *node,
finalize_primnode_results *results);
static void
finalize_primnode(Node *expr, finalize_primnode_results *results)
{
results->subplans = NIL; /* initialize */
results->paramids = NIL;
(void) finalize_primnode_walker(expr, results);
}
static bool static bool
finalize_primnode_walker(Node *node, finalize_primnode(Node *node, finalize_primnode_results *results)
finalize_primnode_results *results)
{ {
if (node == NULL) if (node == NULL)
return false; return false;
...@@ -389,7 +398,7 @@ finalize_primnode_walker(Node *node, ...@@ -389,7 +398,7 @@ finalize_primnode_walker(Node *node,
} }
/* fall through to recurse into subplan args */ /* fall through to recurse into subplan args */
} }
return expression_tree_walker(node, finalize_primnode_walker, return expression_tree_walker(node, finalize_primnode,
(void *) results); (void *) results);
} }
...@@ -443,7 +452,7 @@ process_sublinks_mutator(Node *node, void *context) ...@@ -443,7 +452,7 @@ process_sublinks_mutator(Node *node, void *context)
{ {
SubLink *sublink = (SubLink *) node; SubLink *sublink = (SubLink *) node;
/* First, scan the lefthand-side expressions. /* First, scan the lefthand-side expressions, if any.
* This is a tad klugy since we modify the input SubLink node, * This is a tad klugy since we modify the input SubLink node,
* but that should be OK (make_subplan does it too!) * but that should be OK (make_subplan does it too!)
*/ */
...@@ -475,23 +484,28 @@ SS_finalize_plan(Plan *plan) ...@@ -475,23 +484,28 @@ SS_finalize_plan(Plan *plan)
List *lst; List *lst;
if (plan == NULL) if (plan == NULL)
return NULL; return NIL;
/* Find params in targetlist, make sure there are no subplans there */ results.subplans = NIL; /* initialize lists to NIL */
results.paramids = NIL;
/*
* When we call finalize_primnode, results.paramids lists are
* automatically merged together. But when recursing to self,
* we have to do it the hard way. We want the paramids list
* to include params in subplans as well as at this level.
* (We don't care about finding subplans of subplans, though.)
*/
/* Find params and subplans in targetlist and qual */
finalize_primnode((Node *) plan->targetlist, &results); finalize_primnode((Node *) plan->targetlist, &results);
Assert(results.subplans == NIL); finalize_primnode((Node *) plan->qual, &results);
/* From here on, we invoke finalize_primnode_walker not finalize_primnode, /* Check additional node-type-specific fields */
* so that results.paramids lists are automatically merged together and
* we don't have to do it the hard way. But when recursing to self,
* we do have to merge the lists. Oh well.
*/
switch (nodeTag(plan)) switch (nodeTag(plan))
{ {
case T_Result: case T_Result:
finalize_primnode_walker(((Result *) plan)->resconstantqual, finalize_primnode(((Result *) plan)->resconstantqual,
&results); &results);
/* results.subplans is NOT necessarily empty here ... */
break; break;
case T_Append: case T_Append:
...@@ -501,33 +515,26 @@ SS_finalize_plan(Plan *plan) ...@@ -501,33 +515,26 @@ SS_finalize_plan(Plan *plan)
break; break;
case T_IndexScan: case T_IndexScan:
finalize_primnode_walker((Node *) ((IndexScan *) plan)->indxqual, finalize_primnode((Node *) ((IndexScan *) plan)->indxqual,
&results); &results);
Assert(results.subplans == NIL);
break; break;
case T_MergeJoin: case T_MergeJoin:
finalize_primnode_walker((Node *) ((MergeJoin *) plan)->mergeclauses, finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
&results); &results);
Assert(results.subplans == NIL);
break; break;
case T_HashJoin: case T_HashJoin:
finalize_primnode_walker((Node *) ((HashJoin *) plan)->hashclauses, finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
&results); &results);
Assert(results.subplans == NIL);
break; break;
case T_Hash: case T_Hash:
finalize_primnode_walker((Node *) ((Hash *) plan)->hashkey, finalize_primnode((Node *) ((Hash *) plan)->hashkey,
&results); &results);
Assert(results.subplans == NIL);
break; break;
case T_Agg: case T_Agg:
/* XXX Code used to reject subplans in Aggref args; needed?? */
break;
case T_SeqScan: case T_SeqScan:
case T_NestLoop: case T_NestLoop:
case T_Material: case T_Material:
...@@ -539,12 +546,9 @@ SS_finalize_plan(Plan *plan) ...@@ -539,12 +546,9 @@ SS_finalize_plan(Plan *plan)
default: default:
elog(ERROR, "SS_finalize_plan: node %d unsupported", elog(ERROR, "SS_finalize_plan: node %d unsupported",
nodeTag(plan)); nodeTag(plan));
return NULL;
} }
finalize_primnode_walker((Node *) plan->qual, &results); /* Process left and right subplans, if any */
/* subplans are OK in the qual... */
results.paramids = set_unioni(results.paramids, results.paramids = set_unioni(results.paramids,
SS_finalize_plan(plan->lefttree)); SS_finalize_plan(plan->lefttree));
results.paramids = set_unioni(results.paramids, results.paramids = set_unioni(results.paramids,
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: analyze.c,v 1.123 1999/11/07 23:08:10 momjian Exp $ * $Id: analyze.c,v 1.124 1999/11/15 02:00:09 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -231,11 +231,11 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt) ...@@ -231,11 +231,11 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
/* fix where clause */ /* fix where clause */
qry->qual = transformWhereClause(pstate, stmt->whereClause, NULL); qry->qual = transformWhereClause(pstate, stmt->whereClause, NULL);
qry->hasSubLinks = pstate->p_hasSubLinks;
qry->rtable = pstate->p_rtable; qry->rtable = pstate->p_rtable;
qry->resultRelation = refnameRangeTablePosn(pstate, stmt->relname, NULL); qry->resultRelation = refnameRangeTablePosn(pstate, stmt->relname, NULL);
qry->hasSubLinks = pstate->p_hasSubLinks;
qry->hasAggs = pstate->p_hasAggs; qry->hasAggs = pstate->p_hasAggs;
if (pstate->p_hasAggs) if (pstate->p_hasAggs)
parseCheckAggregates(pstate, qry); parseCheckAggregates(pstate, qry);
...@@ -423,6 +423,9 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt) ...@@ -423,6 +423,9 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
if (stmt->forUpdate != NULL) if (stmt->forUpdate != NULL)
transformForUpdate(qry, stmt->forUpdate); transformForUpdate(qry, stmt->forUpdate);
/* in case of subselects in default clauses... */
qry->hasSubLinks = pstate->p_hasSubLinks;
return (Query *) qry; return (Query *) qry;
} }
......
This diff is collapsed.
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.58 1999/09/13 04:14:56 thomas Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.59 1999/11/15 02:00:10 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -220,8 +220,30 @@ transformExpr(ParseState *pstate, Node *expr, int precedence) ...@@ -220,8 +220,30 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
sublink->lefthand = NIL; sublink->lefthand = NIL;
sublink->oper = NIL; sublink->oper = NIL;
} }
else if (sublink->subLinkType == EXPR_SUBLINK)
{
List *tlist = qtree->targetList;
/* Make sure the subselect delivers a single column
* (ignoring resjunk targets).
*/
if (tlist == NIL ||
((TargetEntry *) lfirst(tlist))->resdom->resjunk)
elog(ERROR, "parser: subselect must have a field");
while ((tlist = lnext(tlist)) != NIL)
{
if (! ((TargetEntry *) lfirst(tlist))->resdom->resjunk)
elog(ERROR, "parser: subselect must have only one field");
}
/* EXPR needs no lefthand or combining operator.
* These fields should be NIL already, but make sure.
*/
sublink->lefthand = NIL;
sublink->oper = NIL;
}
else else
{ {
/* ALL, ANY, or MULTIEXPR: generate operator list */
char *op = lfirst(sublink->oper); char *op = lfirst(sublink->oper);
List *left_list = sublink->lefthand; List *left_list = sublink->lefthand;
List *right_list = qtree->targetList; List *right_list = qtree->targetList;
...@@ -231,9 +253,10 @@ transformExpr(ParseState *pstate, Node *expr, int precedence) ...@@ -231,9 +253,10 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
lfirst(elist) = transformExpr(pstate, lfirst(elist), lfirst(elist) = transformExpr(pstate, lfirst(elist),
precedence); precedence);
if (length(left_list) > 1 && /* Combining operators other than =/<> is dubious... */
if (length(left_list) != 1 &&
strcmp(op, "=") != 0 && strcmp(op, "<>") != 0) strcmp(op, "=") != 0 && strcmp(op, "<>") != 0)
elog(ERROR, "parser: '%s' is not relational operator", elog(ERROR, "parser: '%s' is not usable for row comparison",
op); op);
sublink->oper = NIL; sublink->oper = NIL;
...@@ -266,8 +289,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence) ...@@ -266,8 +289,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
FALSE); FALSE);
opform = (Form_pg_operator) GETSTRUCT(optup); opform = (Form_pg_operator) GETSTRUCT(optup);
if (opform->oprresult != BOOLOID && if (opform->oprresult != BOOLOID)
sublink->subLinkType != EXPR_SUBLINK)
elog(ERROR, "parser: '%s' must return 'bool' to be used with quantified predicate subquery", op); elog(ERROR, "parser: '%s' must return 'bool' to be used with quantified predicate subquery", op);
newop = makeOper(oprid(optup),/* opno */ newop = makeOper(oprid(optup),/* opno */
...@@ -589,13 +611,14 @@ exprType(Node *expr) ...@@ -589,13 +611,14 @@ exprType(Node *expr)
if (sublink->subLinkType == EXPR_SUBLINK) if (sublink->subLinkType == EXPR_SUBLINK)
{ {
/* return the result type of the combining operator; /* get the type of the subselect's first target column */
* should only be one... Query *qtree = (Query *) sublink->subselect;
*/ TargetEntry *tent;
Oper *op = (Oper *) lfirst(sublink->oper);
if (! qtree || ! IsA(qtree, Query))
Assert(IsA(op, Oper)); elog(ERROR, "exprType: can't get type for untransformed sublink");
type = op->opresulttype; tent = (TargetEntry *) lfirst(qtree->targetList);
type = tent->resdom->restype;
} }
else else
{ {
......
This diff is collapsed.
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* out of it's tuple * out of it's tuple
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.30 1999/11/07 23:08:24 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.31 1999/11/15 02:00:05 tgl Exp $
* *
* This software is copyrighted by Jan Wieck - Hamburg. * This software is copyrighted by Jan Wieck - Hamburg.
* *
...@@ -1681,15 +1681,17 @@ get_sublink_expr(Node *node, deparse_context *context) ...@@ -1681,15 +1681,17 @@ get_sublink_expr(Node *node, deparse_context *context)
StringInfo buf = context->buf; StringInfo buf = context->buf;
SubLink *sublink = (SubLink *) node; SubLink *sublink = (SubLink *) node;
Query *query = (Query *) (sublink->subselect); Query *query = (Query *) (sublink->subselect);
Oper *oper;
List *l; List *l;
char *sep; char *sep;
Oper *oper;
bool need_paren;
appendStringInfo(buf, "("); appendStringInfo(buf, "(");
if (sublink->lefthand != NULL) if (sublink->lefthand != NIL)
{ {
if (length(sublink->lefthand) > 1) need_paren = (length(sublink->lefthand) > 1);
if (need_paren)
appendStringInfo(buf, "("); appendStringInfo(buf, "(");
sep = ""; sep = "";
...@@ -1700,12 +1702,14 @@ get_sublink_expr(Node *node, deparse_context *context) ...@@ -1700,12 +1702,14 @@ get_sublink_expr(Node *node, deparse_context *context)
get_rule_expr((Node *) lfirst(l), context); get_rule_expr((Node *) lfirst(l), context);
} }
if (length(sublink->lefthand) > 1) if (need_paren)
appendStringInfo(buf, ") "); appendStringInfo(buf, ") ");
else else
appendStringInfo(buf, " "); appendStringInfo(buf, " ");
} }
need_paren = true;
switch (sublink->subLinkType) switch (sublink->subLinkType)
{ {
case EXISTS_SUBLINK: case EXISTS_SUBLINK:
...@@ -1722,20 +1726,30 @@ get_sublink_expr(Node *node, deparse_context *context) ...@@ -1722,20 +1726,30 @@ get_sublink_expr(Node *node, deparse_context *context)
appendStringInfo(buf, "%s ALL ", get_opname(oper->opno)); appendStringInfo(buf, "%s ALL ", get_opname(oper->opno));
break; break;
case EXPR_SUBLINK: case MULTIEXPR_SUBLINK:
oper = (Oper *) lfirst(sublink->oper); oper = (Oper *) lfirst(sublink->oper);
appendStringInfo(buf, "%s ", get_opname(oper->opno)); appendStringInfo(buf, "%s ", get_opname(oper->opno));
break; break;
case EXPR_SUBLINK:
need_paren = false;
break;
default: default:
elog(ERROR, "get_sublink_expr: unsupported sublink type %d", elog(ERROR, "get_sublink_expr: unsupported sublink type %d",
sublink->subLinkType); sublink->subLinkType);
break; break;
} }
appendStringInfo(buf, "("); if (need_paren)
appendStringInfo(buf, "(");
get_query_def(query, buf, context->rangetables); get_query_def(query, buf, context->rangetables);
appendStringInfo(buf, "))");
if (need_paren)
appendStringInfo(buf, "))");
else
appendStringInfo(buf, ")");
} }
/* ---------- /* ----------
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: plannodes.h,v 1.31 1999/10/17 22:15:07 tgl Exp $ * $Id: plannodes.h,v 1.32 1999/11/15 02:00:13 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -356,8 +356,8 @@ typedef struct SubPlan ...@@ -356,8 +356,8 @@ typedef struct SubPlan
List *setParam; /* non-correlated EXPR & EXISTS subqueries List *setParam; /* non-correlated EXPR & EXISTS subqueries
* have to set some Params for paren Plan */ * have to set some Params for paren Plan */
List *parParam; /* indices of corr. Vars from parent plan */ List *parParam; /* indices of corr. Vars from parent plan */
SubLink *sublink; /* SubLink node for subselects in WHERE SubLink *sublink; /* SubLink node from parser; holds info about
* and HAVING */ * what to do with subselect's results */
bool shutdown; /* shutdown plan if TRUE */ bool shutdown; /* shutdown plan if TRUE */
} SubPlan; } SubPlan;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: primnodes.h,v 1.36 1999/08/25 23:21:36 tgl Exp $ * $Id: primnodes.h,v 1.37 1999/11/15 02:00:15 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -98,8 +98,7 @@ typedef struct Fjoin ...@@ -98,8 +98,7 @@ typedef struct Fjoin
* Expr * Expr
* typeOid - oid of the type of this expression * typeOid - oid of the type of this expression
* opType - type of this expression * opType - type of this expression
* oper - the Oper node if it is an OPER_EXPR or the * oper - operator node if needed (Oper, Func, or SubPlan)
* Func node if it is a FUNC_EXPR
* args - arguments to this expression * args - arguments to this expression
* ---------------- * ----------------
*/ */
...@@ -318,12 +317,31 @@ typedef struct Aggref ...@@ -318,12 +317,31 @@ typedef struct Aggref
/* ---------------- /* ----------------
* SubLink * SubLink
* subLinkType - EXISTS, ALL, ANY, EXPR * subLinkType - EXISTS, ALL, ANY, MULTIEXPR, EXPR
* useor - TRUE for <> (combine op results with "or" not "and") * useor - TRUE to combine column results with "OR" not "AND"
* lefthand - list of outer-query expressions on the left * lefthand - list of outer-query expressions on the left
* oper - list of Oper nodes * oper - list of Oper nodes for combining operators
* subselect - subselect as Query* or parsetree * subselect - subselect as Query* or parsetree
* *
* A SubLink represents a subselect appearing in an expression, and in some
* cases also the combining operator(s) just above it. The subLinkType
* indicates the form of the expression represented:
* EXISTS_SUBLINK EXISTS(SELECT ...)
* ALL_SUBLINK (lefthand) op ALL (SELECT ...)
* ANY_SUBLINK (lefthand) op ANY (SELECT ...)
* MULTIEXPR_SUBLINK (lefthand) op (SELECT ...)
* EXPR_SUBLINK (SELECT with single targetlist item ...)
* For ALL, ANY, and MULTIEXPR, the lefthand is a list of expressions of the
* same length as the subselect's targetlist. MULTIEXPR will *always* have
* a list with more than one entry; if the subselect has just one target
* then the parser will create an EXPR_SUBLINK instead (and any operator
* above the subselect will be represented separately). Note that both
* MULTIEXPR and EXPR require the subselect to deliver only one row.
* ALL, ANY, and MULTIEXPR require the combining operators to deliver boolean
* results. These are reduced to one result per row using OR or AND semantics
* depending on the "useor" flag. ALL and ANY combine the per-row results
* using AND and OR semantics respectively.
*
* NOTE: lefthand and oper have varying meanings depending on where you look * NOTE: lefthand and oper have varying meanings depending on where you look
* in the parse/plan pipeline: * in the parse/plan pipeline:
* 1. gram.y delivers a list of the (untransformed) lefthand expressions in * 1. gram.y delivers a list of the (untransformed) lefthand expressions in
...@@ -348,12 +366,13 @@ typedef struct Aggref ...@@ -348,12 +366,13 @@ typedef struct Aggref
* representation 2 appears in a "bare" SubLink, while representation 3 is * representation 2 appears in a "bare" SubLink, while representation 3 is
* found in SubLinks that are children of SubPlan nodes. * found in SubLinks that are children of SubPlan nodes.
* *
* In an EXISTS SubLink, both lefthand and oper are unused and are always NIL. * In EXISTS and EXPR SubLinks, both lefthand and oper are unused and are
* always NIL. useor is not significant either for these sublink types.
* ---------------- * ----------------
*/ */
typedef enum SubLinkType typedef enum SubLinkType
{ {
EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, EXPR_SUBLINK EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, MULTIEXPR_SUBLINK, EXPR_SUBLINK
} SubLinkType; } SubLinkType;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment