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.
* nodeSubplan.c
* 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 $
+ *
*-------------------------------------------------------------------------
*/
/*
* within a tuple (if there are multiple columns) using OR semantics
* if "useor" is true, AND semantics if not. We then combine results
* across tuples (if the subplan produces more than one) using OR
- * semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK. NULL
- * results from the combining operators are handled according to the
- * usual SQL semantics for OR and AND. The result for no input
- * tuples is FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK.
+ * semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK.
+ * (MULTIEXPR_SUBLINK doesn't allow multiple tuples from the subplan.)
+ * NULL results from the combining operators are handled according to
+ * 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
* tuple, else an error is raised. If zero tuples are produced, we
- * return NULL. (XXX it would probably be more correct to evaluate
- * the combining operator with a NULL input?) Assuming we get a tuple:
- * 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.
+ * return NULL. Assuming we get a tuple, we just return its first
+ * column (there can be only one non-junk column in this case).
*/
result = (Datum) (subLinkType == ALL_SUBLINK ? true : false);
*isNull = false;
if (subLinkType == EXISTS_SUBLINK)
return (Datum) true;
- /* cannot allow multiple input tuples for EXPR sublink */
- if (subLinkType == EXPR_SUBLINK && found)
+ if (subLinkType == EXPR_SUBLINK)
+ {
+ /* 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");
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)
{
Expr *expr = (Expr *) lfirst(lst);
}
else
{
- /* must be EXPR_SUBLINK */
+ /* must be MULTIEXPR_SUBLINK */
result = rowresult;
*isNull = rownull;
}
if (!found)
{
/* 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;
*isNull = true;
* If this plan is un-correlated or undirect correlated one and want
* to set params for parent plan then prepare parameters.
*/
- if (node->setParam != NULL)
+ if (node->setParam != NIL)
{
List *lst;
TupleDesc tdesc = slot->ttc_tupleDescriptor;
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)
{
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(node->setParam)]);
prm->execPlan = NULL;
prm->value = (Datum) true;
prm->isnull = false;
+ found = true;
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
* (see below) and this tuple will be free-ed - bad for not byval
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
newnode->locParam = listCopy(from->locParam);
newnode->chgParam = listCopy(from->chgParam);
Node_Copy(from, newnode, initPlan);
- if (from->subPlan != NULL)
- newnode->subPlan = SS_pull_subplan((Node *) newnode->qual);
+ if (from->subPlan != NIL)
+ newnode->subPlan = nconc(SS_pull_subplan((Node *) newnode->targetlist),
+ SS_pull_subplan((Node *) newnode->qual));
else
- newnode->subPlan = NULL;
+ newnode->subPlan = NIL;
newnode->nParamExec = from->nParamExec;
}
* We must add subplans in resconstantqual to the new plan's subPlan
* list
*/
- newnode->plan.subPlan = nconc(newnode->plan.subPlan,
- SS_pull_subplan(newnode->resconstantqual));
+ if (from->plan.subPlan != NIL)
+ newnode->plan.subPlan = nconc(newnode->plan.subPlan,
+ SS_pull_subplan(newnode->resconstantqual));
return newnode;
}
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
tlist = (List *) SS_replace_correlation_vars((Node *) tlist);
qual = (List *) SS_replace_correlation_vars((Node *) qual);
}
+
/* Expand SubLinks to SubPlans */
if (root->hasSubLinks)
+ {
+ tlist = (List *) SS_process_sublinks((Node *) tlist);
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
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
{
/* Expand SubLinks to SubPlans */
parse->havingQual = SS_process_sublinks(parse->havingQual);
-
- /*
- * 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.)
- */
+ /* Check for ungrouped variables passed to subplans */
if (check_subplans_for_ungrouped_vars(parse->havingQual,
parse->groupClause,
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");
}
}
* Copyright (c) 1994, Regents of the University of California
*
* 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 $
*
*-------------------------------------------------------------------------
*/
}
/*
- * Un-correlated or undirect correlated plans of EXISTS or EXPR types
- * can be used as initPlans...
+ * Un-correlated or undirect correlated plans of EXISTS, EXPR, or
+ * 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;
int i = 0;
prm->paramkind = PARAM_EXEC;
prm->paramid = (AttrNumber) new_param(var, PlannerQueryLevel);
prm->paramtype = var->vartype;
+ pfree(var); /* var is only needed for new_param */
Assert(IsA(oper, Oper));
tup = get_operator_tuple(oper->opno);
(Var *) left,
(Var *) right));
node->setParam = lappendi(node->setParam, prm->paramid);
- pfree(var);
i++;
}
slink->oper = newoper;
else
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
{
/* make expression of SUBPLAN type */
/*
* 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 {
List *paramids; /* List of PARAM_EXEC paramids found */
} 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
-finalize_primnode_walker(Node *node,
- finalize_primnode_results *results)
+finalize_primnode(Node *node, finalize_primnode_results *results)
{
if (node == NULL)
return false;
}
/* fall through to recurse into subplan args */
}
- return expression_tree_walker(node, finalize_primnode_walker,
+ return expression_tree_walker(node, finalize_primnode,
(void *) results);
}
{
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,
* but that should be OK (make_subplan does it too!)
*/
List *lst;
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);
- Assert(results.subplans == NIL);
+ finalize_primnode((Node *) plan->qual, &results);
- /* From here on, we invoke finalize_primnode_walker not finalize_primnode,
- * 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.
- */
+ /* Check additional node-type-specific fields */
switch (nodeTag(plan))
{
case T_Result:
- finalize_primnode_walker(((Result *) plan)->resconstantqual,
- &results);
- /* results.subplans is NOT necessarily empty here ... */
+ finalize_primnode(((Result *) plan)->resconstantqual,
+ &results);
break;
case T_Append:
break;
case T_IndexScan:
- finalize_primnode_walker((Node *) ((IndexScan *) plan)->indxqual,
- &results);
- Assert(results.subplans == NIL);
+ finalize_primnode((Node *) ((IndexScan *) plan)->indxqual,
+ &results);
break;
case T_MergeJoin:
- finalize_primnode_walker((Node *) ((MergeJoin *) plan)->mergeclauses,
- &results);
- Assert(results.subplans == NIL);
+ finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
+ &results);
break;
case T_HashJoin:
- finalize_primnode_walker((Node *) ((HashJoin *) plan)->hashclauses,
- &results);
- Assert(results.subplans == NIL);
+ finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
+ &results);
break;
case T_Hash:
- finalize_primnode_walker((Node *) ((Hash *) plan)->hashkey,
- &results);
- Assert(results.subplans == NIL);
+ finalize_primnode((Node *) ((Hash *) plan)->hashkey,
+ &results);
break;
case T_Agg:
- /* XXX Code used to reject subplans in Aggref args; needed?? */
- break;
-
case T_SeqScan:
case T_NestLoop:
case T_Material:
default:
elog(ERROR, "SS_finalize_plan: node %d unsupported",
nodeTag(plan));
- return NULL;
}
- finalize_primnode_walker((Node *) plan->qual, &results);
- /* subplans are OK in the qual... */
-
+ /* Process left and right subplans, if any */
results.paramids = set_unioni(results.paramids,
SS_finalize_plan(plan->lefttree));
results.paramids = set_unioni(results.paramids,
*
* 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 $
*
*-------------------------------------------------------------------------
*/
/* fix where clause */
qry->qual = transformWhereClause(pstate, stmt->whereClause, NULL);
- qry->hasSubLinks = pstate->p_hasSubLinks;
qry->rtable = pstate->p_rtable;
qry->resultRelation = refnameRangeTablePosn(pstate, stmt->relname, NULL);
+ qry->hasSubLinks = pstate->p_hasSubLinks;
qry->hasAggs = pstate->p_hasAggs;
if (pstate->p_hasAggs)
parseCheckAggregates(pstate, qry);
if (stmt->forUpdate != NULL)
transformForUpdate(qry, stmt->forUpdate);
+ /* in case of subselects in default clauses... */
+ qry->hasSubLinks = pstate->p_hasSubLinks;
+
return (Query *) qry;
}
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.113 1999/10/29 23:52:20 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.114 1999/11/15 02:00:10 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
#include "mb/pg_wchar.h"
#endif
+extern List *parsetree; /* final parse result is delivered here */
+
static char saved_relname[NAMEDATALEN]; /* need this for complex attributes */
static bool QueryIsRule = FALSE;
static Oid *param_type_info;
static int pfunc_num_args;
-extern List *parsetree;
/*
static void param_type_init(Oid *typev, int nargs);
static Node *doNegate(Node *n);
-Oid param_type(int t); /* used in parse_expr.c */
-
/* old versions of flex define this as a macro */
#if defined(yywrap)
#undef yywrap
%type <boolean> TriggerForOpt, TriggerForType, OptTemp, OptTempType, OptTempScope
%type <list> for_update_clause, update_list
-%type <boolean> opt_union
+%type <boolean> opt_all
%type <boolean> opt_table
%type <boolean> opt_trans
| /*EMPTY*/ { $$ = NIL; }
;
-CreateAsStmt: CREATE OptTemp TABLE relation_name OptCreateAs AS SubSelect
+/*
+ * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
+ * SELECT ... INTO.
+ */
+
+CreateAsStmt: CREATE OptTemp TABLE relation_name OptCreateAs AS SelectStmt
{
SelectStmt *n = (SelectStmt *)$7;
if ($5 != NIL)
mapTargetColumns($5, n->targetList);
+ if (n->into != NULL)
+ elog(ERROR,"CREATE TABLE/AS SELECT may not specify INTO");
n->istemp = $2;
n->into = $4;
$$ = (Node *)n;
$$ = (Node *)makeA_Expr(AND,NULL,$1,
makeA_Expr(NOT,NULL,NULL,$3));
}
- | select_clause UNION opt_union select_clause
+ | select_clause UNION opt_all select_clause
{
if (IsA($4, SelectStmt))
{
| /*EMPTY*/ { $$ = FALSE; }
;
-opt_union: ALL { $$ = TRUE; }
+opt_all: ALL { $$ = TRUE; }
| /*EMPTY*/ { $$ = FALSE; }
;
* Define row_descriptor to allow yacc to break the reduce/reduce conflict
* with singleton expressions.
* Eliminated lots of code by defining row_op and sub_type clauses.
- * However, can not consolidate EXPR_LINK case with others subselects
- * due to shift/reduce conflict with the non-subselect clause (the parser
- * would have to look ahead more than one token to resolve the conflict).
* - thomas 1998-05-09
*/
row_expr: '(' row_descriptor ')' IN '(' SubSelect ')'
{
SubLink *n = makeNode(SubLink);
n->lefthand = $2;
- n->oper = lcons("=",NIL);
+ n->oper = lcons("=", NIL);
n->useor = false;
n->subLinkType = ANY_SUBLINK;
n->subselect = $6;
{
SubLink *n = makeNode(SubLink);
n->lefthand = $2;
- n->oper = lcons("<>",NIL);
+ n->oper = lcons("<>", NIL);
n->useor = true;
n->subLinkType = ALL_SUBLINK;
n->subselect = $7;
n->useor = true;
else
n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
+ n->subLinkType = MULTIEXPR_SUBLINK;
n->subselect = $6;
$$ = (Node *)n;
}
n->args = NIL;
$$ = (Node *)n;
}
- | EXISTS '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = NIL;
- n->useor = false;
- n->oper = NIL;
- n->subLinkType = EXISTS_SUBLINK;
- n->subselect = $3;
- $$ = (Node *)n;
- }
| EXTRACT '(' extract_list ')'
{
FuncCall *n = makeNode(FuncCall);
{
SubLink *n = (SubLink *)$4;
n->lefthand = lcons($1, NIL);
- n->oper = lcons("=",NIL);
+ n->oper = lcons("=", NIL);
n->useor = false;
n->subLinkType = ANY_SUBLINK;
$$ = (Node *)n;
{
SubLink *n = (SubLink *)$5;
n->lefthand = lcons($1, NIL);
- n->oper = lcons("<>",NIL);
+ n->oper = lcons("<>", NIL);
n->useor = false;
n->subLinkType = ALL_SUBLINK;
$$ = (Node *)n;
$$ = n;
}
}
- | a_expr Op '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons($2,NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '+' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("+",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '-' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("-",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '*' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("*",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '/' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("/",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '%' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("%",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '^' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("^",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '|' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("|",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '<' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("<",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '>' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons(">",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr '=' '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("=",NIL);
- n->useor = false;
- n->subLinkType = EXPR_SUBLINK;
- n->subselect = $4;
- $$ = (Node *)n;
- }
- | a_expr Op ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons($2,NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '+' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("+",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '-' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("-",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '*' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("*",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '/' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("/",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '%' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("%",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '^' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("^",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '|' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("|",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '<' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("<",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '>' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons(">",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '=' ANY '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1,NIL);
- n->oper = lcons("=",NIL);
- n->useor = false;
- n->subLinkType = ANY_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr Op ALL '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons($2,NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '+' ALL '(' SubSelect ')'
+ | a_expr row_op sub_type '(' SubSelect ')'
{
SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("+",NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
+ n->lefthand = lcons($1, NIL);
+ n->oper = lcons($2, NIL);
+ n->useor = false; /* doesn't matter since only one col */
+ n->subLinkType = $3;
n->subselect = $5;
$$ = (Node *)n;
}
- | a_expr '-' ALL '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("-",NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '*' ALL '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("*",NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '/' ALL '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("/",NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '%' ALL '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("%",NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '^' ALL '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("^",NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '|' ALL '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("|",NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '<' ALL '(' SubSelect ')'
- {
- SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("<",NIL);
- n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
- $$ = (Node *)n;
- }
- | a_expr '>' ALL '(' SubSelect ')'
+ | EXISTS '(' SubSelect ')'
{
SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons(">",NIL);
+ n->lefthand = NIL;
+ n->oper = NIL;
n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
+ n->subLinkType = EXISTS_SUBLINK;
+ n->subselect = $3;
$$ = (Node *)n;
}
- | a_expr '=' ALL '(' SubSelect ')'
+ | '(' SubSelect ')'
{
SubLink *n = makeNode(SubLink);
- n->lefthand = lcons($1, NULL);
- n->oper = lcons("=",NIL);
+ n->lefthand = NIL;
+ n->oper = NIL;
n->useor = false;
- n->subLinkType = ALL_SUBLINK;
- n->subselect = $5;
+ n->subLinkType = EXPR_SUBLINK;
+ n->subselect = $2;
$$ = (Node *)n;
}
| a_expr AND a_expr
n->args = $3;
$$ = (Node *)n;
}
+ | '(' SubSelect ')'
+ {
+ SubLink *n = makeNode(SubLink);
+ n->lefthand = NIL;
+ n->oper = NIL;
+ n->useor = false;
+ n->subLinkType = EXPR_SUBLINK;
+ n->subselect = $2;
+ $$ = (Node *)n;
+ }
;
opt_indirection: '[' a_expr ']' opt_indirection
src = lnext(src);
dst = lnext(dst);
}
-
- return;
} /* mapTargetColumns() */
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
sublink->lefthand = 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
{
+ /* ALL, ANY, or MULTIEXPR: generate operator list */
char *op = lfirst(sublink->oper);
List *left_list = sublink->lefthand;
List *right_list = qtree->targetList;
lfirst(elist) = transformExpr(pstate, lfirst(elist),
precedence);
- if (length(left_list) > 1 &&
+ /* Combining operators other than =/<> is dubious... */
+ if (length(left_list) != 1 &&
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);
sublink->oper = NIL;
FALSE);
opform = (Form_pg_operator) GETSTRUCT(optup);
- if (opform->oprresult != BOOLOID &&
- sublink->subLinkType != EXPR_SUBLINK)
+ if (opform->oprresult != BOOLOID)
elog(ERROR, "parser: '%s' must return 'bool' to be used with quantified predicate subquery", op);
newop = makeOper(oprid(optup),/* opno */
if (sublink->subLinkType == EXPR_SUBLINK)
{
- /* return the result type of the combining operator;
- * should only be one...
- */
- Oper *op = (Oper *) lfirst(sublink->oper);
-
- Assert(IsA(op, Oper));
- type = op->opresulttype;
+ /* get the type of the subselect's first target column */
+ Query *qtree = (Query *) sublink->subselect;
+ TargetEntry *tent;
+
+ if (! qtree || ! IsA(qtree, Query))
+ elog(ERROR, "exprType: can't get type for untransformed sublink");
+ tent = (TargetEntry *) lfirst(qtree->targetList);
+ type = tent->resdom->restype;
}
else
{
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.62 1999/11/01 05:18:31 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.63 1999/11/15 02:00:03 tgl Exp $
*
*-------------------------------------------------------------------------
*/
int sublevels_up);
static bool modifyAggrefUplevel(Node *node, void *context);
static bool modifyAggrefChangeVarnodes(Node *node, int rt_index, int new_index,
- int sublevels_up);
+ int sublevels_up, int new_sublevels_up);
static Node *modifyAggrefDropQual(Node *node, Node *targetNode);
-static SubLink *modifyAggrefMakeSublink(Expr *origexp, Query *parsetree);
+static SubLink *modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree);
static Node *modifyAggrefQual(Node *node, Query *parsetree);
static bool checkQueryHasAggs(Node *node);
static bool checkQueryHasAggs_walker(Node *node, void *context);
int rt_index;
int new_index;
int sublevels_up;
+ int new_sublevels_up;
} modifyAggrefChangeVarnodes_context;
static bool
{
var->varno = context->new_index;
var->varnoold = context->new_index;
- var->varlevelsup = 0;
+ var->varlevelsup = context->new_sublevels_up;
}
return false;
}
if (modifyAggrefChangeVarnodes((Node *) (sub->subselect),
context->rt_index,
context->new_index,
- context->sublevels_up + 1))
+ context->sublevels_up + 1,
+ context->new_sublevels_up + 1))
return true;
return false;
}
static bool
modifyAggrefChangeVarnodes(Node *node, int rt_index, int new_index,
- int sublevels_up)
+ int sublevels_up, int new_sublevels_up)
{
modifyAggrefChangeVarnodes_context context;
context.rt_index = rt_index;
context.new_index = new_index;
context.sublevels_up = sublevels_up;
+ context.new_sublevels_up = new_sublevels_up;
return modifyAggrefChangeVarnodes_walker(node, &context);
}
* modifyAggrefDropQual -
* remove the pure aggref clause from a qualification
*
- * targetNode is a boolean expression node somewhere within the given
- * expression tree. When we find it, replace it with a constant TRUE.
+ * targetNode is an Aggref node somewhere within the given expression tree.
+ * Find the boolean operator that's presumably somewhere above it, and replace
+ * that whole operator expression with a constant TRUE. (This is NOT really
+ * quite the right thing, but it handles simple cases. This whole set of
+ * Aggref-in-qual routines needs to be thrown away when we can do subselects
+ * in FROM.)
+ *
* The return tree is a modified copy of the given tree; the given tree
* is not altered.
*
if (node == NULL)
return NULL;
if (node == targetNode)
+ {
+ /* Oops, it's not inside an Expr we can rearrange... */
+ elog(ERROR, "Cannot handle aggregate function inserted at this place in WHERE clause");
+ }
+ if (IsA(node, Expr))
{
Expr *expr = (Expr *) node;
+ List *i;
- if (! IsA(expr, Expr) || expr->typeOid != BOOLOID)
- elog(ERROR,
- "aggregate expression in qualification isn't of type bool");
- return (Node *) makeConst(BOOLOID, 1, (Datum) true,
- false, true, false, false);
+ foreach(i, expr->args)
+ {
+ if (((Node *) lfirst(i)) == targetNode)
+ {
+ /* Found the parent expression containing the Aggref */
+ if (expr->typeOid != BOOLOID)
+ elog(ERROR,
+ "aggregate function in qual must be argument of boolean operator");
+ return (Node *) makeConst(BOOLOID, 1, (Datum) true,
+ false, true, false, false);
+ }
+ }
+ /* else this isn't the expr we want, keep going */
}
return expression_tree_mutator(node, modifyAggrefDropQual,
(void *) targetNode);
* uses an aggregate column of a view
*/
static SubLink *
-modifyAggrefMakeSublink(Expr *origexp, Query *parsetree)
+modifyAggrefMakeSublink(Aggref *aggref, Query *parsetree)
{
- SubLink *sublink;
- Query *subquery;
- RangeTblEntry *rte;
- Aggref *aggref;
+ /* target and rte point to old structures: */
Var *target;
+ RangeTblEntry *rte;
+ /* these point to newly-created structures: */
+ Query *subquery;
+ SubLink *sublink;
TargetEntry *tle;
Resdom *resdom;
- Expr *exp = copyObject(origexp);
- if (IsA(nth(0, exp->args), Aggref))
- {
- if (IsA(nth(1, exp->args), Aggref))
- elog(ERROR, "rewrite: comparison of 2 aggregate columns not supported");
- else
- elog(ERROR, "rewrite: aggregate column of view must be at right side in qual");
- /* XXX could try to commute operator, instead of failing */
- }
-
- aggref = (Aggref *) nth(1, exp->args);
target = (Var *) (aggref->target);
if (! IsA(target, Var))
elog(ERROR, "rewrite: aggregates of views only allowed on simple variables for now");
tle = makeNode(TargetEntry);
tle->resdom = resdom;
- tle->expr = (Node *) aggref; /* note this is from the copied expr */
+ tle->expr = copyObject(aggref); /* make a modifiable copy! */
+
+ subquery = makeNode(Query);
sublink = makeNode(SubLink);
sublink->subLinkType = EXPR_SUBLINK;
sublink->useor = false;
- /* note lefthand and oper are made from the copied expr */
- sublink->lefthand = lcons(lfirst(exp->args), NIL);
- sublink->oper = lcons(exp->oper, NIL);
-
- subquery = makeNode(Query);
+ sublink->lefthand = NIL;
+ sublink->oper = NIL;
sublink->subselect = (Node *) subquery;
subquery->commandType = CMD_SELECT;
subquery->unionall = FALSE;
subquery->uniqueFlag = NULL;
subquery->sortClause = NULL;
- subquery->rtable = lcons(rte, NIL);
+ subquery->rtable = lcons(copyObject(rte), NIL);
subquery->targetList = lcons(tle, NIL);
subquery->qual = modifyAggrefDropQual((Node *) parsetree->qual,
- (Node *) origexp);
+ (Node *) aggref);
/*
* If there are still aggs in the subselect's qual, give up.
* Recursing would be a bad idea --- we'd likely produce an
* infinite recursion. This whole technique is a crock, really...
*/
if (checkQueryHasAggs(subquery->qual))
- elog(ERROR, "Cannot handle aggregate function inserted at this place in WHERE clause");
+ elog(ERROR, "Cannot handle multiple aggregate functions in WHERE clause");
subquery->groupClause = NIL;
subquery->havingQual = NULL;
subquery->hasAggs = TRUE;
- subquery->hasSubLinks = FALSE;
+ subquery->hasSubLinks = checkQueryHasSubLink(subquery->qual);
subquery->unionClause = NULL;
+ /* Increment all varlevelsup fields in the new subquery */
modifyAggrefUplevel((Node *) subquery, NULL);
- /*
- * Note: it might appear that we should be passing target->varlevelsup+1
- * here, since modifyAggrefUplevel has increased all the varlevelsup
- * values in the subquery. However, target itself is a pointer to a
- * Var node in the subquery, so it's been incremented too! What a kluge
- * this all is ... we need to make subquery RTEs so it can go away...
+
+ /* Replace references to the target table with correct varno.
+ * Note +1 here to account for effects of previous line!
*/
modifyAggrefChangeVarnodes((Node *) subquery, target->varno,
- 1, target->varlevelsup);
+ 1, target->varlevelsup+1, 0);
return sublink;
}
{
if (node == NULL)
return NULL;
- if (IsA(node, Expr))
- {
- Expr *expr = (Expr *) node;
-
- if (length(expr->args) == 2 &&
- (IsA(lfirst(expr->args), Aggref) ||
- IsA(lsecond(expr->args), Aggref)))
- {
- SubLink *sub = modifyAggrefMakeSublink(expr,
- parsetree);
- parsetree->hasSubLinks = true;
- /* check for aggs in resulting lefthand... */
- sub->lefthand = (List *) modifyAggrefQual((Node *) sub->lefthand,
- parsetree);
- return (Node *) sub;
- }
- /* otherwise, fall through and copy the expr normally */
- }
if (IsA(node, Aggref))
{
- /* Oops, found one that's not inside an Expr we can rearrange... */
- elog(ERROR, "Cannot handle aggregate function inserted at this place in WHERE clause");
+ SubLink *sub = modifyAggrefMakeSublink((Aggref *) node, parsetree);
+
+ parsetree->hasSubLinks = true;
+ return (Node *) sub;
}
- /* We do NOT recurse into subselects in this routine. It's sufficient
+ /*
+ * Otherwise, fall through and copy the expr normally.
+ *
+ * We do NOT recurse into subselects in this routine. It's sufficient
* to get rid of aggregates that are in the qual expression proper.
*/
return expression_tree_mutator(node, modifyAggrefQual,
if (parsetree->hasSubLinks)
fireRIRonSubselect((Node *) parsetree, NULL);
- parsetree->qual = modifyAggrefQual(parsetree->qual, parsetree);
+ if (parsetree->hasAggs)
+ parsetree->qual = modifyAggrefQual(parsetree->qual, parsetree);
return parsetree;
}
/*
* If the query was marked having aggregates, check if this is
- * still true after rewriting. Ditto for sublinks.
- *
- * This check must get expanded when someday aggregates can appear
- * somewhere else than in the targetlist or the having qual.
+ * still true after rewriting. Ditto for sublinks. Note there
+ * should be no aggs in the qual at this point.
*/
if (query->hasAggs)
- query->hasAggs = checkQueryHasAggs((Node *) (query->targetList))
- || checkQueryHasAggs((Node *) (query->havingQual));
+ {
+ query->hasAggs =
+ checkQueryHasAggs((Node *) (query->targetList)) ||
+ checkQueryHasAggs((Node *) (query->havingQual));
+ if (checkQueryHasAggs((Node *) (query->qual)))
+ elog(ERROR, "BasicQueryRewrite: failed to remove aggs from qual");
+ }
if (query->hasSubLinks)
- query->hasSubLinks = checkQueryHasSubLink((Node *) (query->qual))
- || checkQueryHasSubLink((Node *) (query->havingQual));
+ query->hasSubLinks =
+ checkQueryHasSubLink((Node *) (query->targetList)) ||
+ checkQueryHasSubLink((Node *) (query->qual)) ||
+ checkQueryHasSubLink((Node *) (query->havingQual));
results = lappend(results, query);
}
* out of it's tuple
*
* 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.
*
StringInfo buf = context->buf;
SubLink *sublink = (SubLink *) node;
Query *query = (Query *) (sublink->subselect);
- Oper *oper;
List *l;
char *sep;
+ Oper *oper;
+ bool need_paren;
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, "(");
sep = "";
get_rule_expr((Node *) lfirst(l), context);
}
- if (length(sublink->lefthand) > 1)
+ if (need_paren)
appendStringInfo(buf, ") ");
else
appendStringInfo(buf, " ");
}
+ need_paren = true;
+
switch (sublink->subLinkType)
{
case EXISTS_SUBLINK:
appendStringInfo(buf, "%s ALL ", get_opname(oper->opno));
break;
- case EXPR_SUBLINK:
+ case MULTIEXPR_SUBLINK:
oper = (Oper *) lfirst(sublink->oper);
appendStringInfo(buf, "%s ", get_opname(oper->opno));
break;
+ case EXPR_SUBLINK:
+ need_paren = false;
+ break;
+
default:
elog(ERROR, "get_sublink_expr: unsupported sublink type %d",
sublink->subLinkType);
break;
}
- appendStringInfo(buf, "(");
+ if (need_paren)
+ appendStringInfo(buf, "(");
+
get_query_def(query, buf, context->rangetables);
- appendStringInfo(buf, "))");
+
+ if (need_paren)
+ appendStringInfo(buf, "))");
+ else
+ appendStringInfo(buf, ")");
}
/* ----------
*
* 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 $
*
*-------------------------------------------------------------------------
*/
List *setParam; /* non-correlated EXPR & EXISTS subqueries
* have to set some Params for paren Plan */
List *parParam; /* indices of corr. Vars from parent plan */
- SubLink *sublink; /* SubLink node for subselects in WHERE
- * and HAVING */
+ SubLink *sublink; /* SubLink node from parser; holds info about
+ * what to do with subselect's results */
bool shutdown; /* shutdown plan if TRUE */
} SubPlan;
*
* 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 $
*
*-------------------------------------------------------------------------
*/
* Expr
* typeOid - oid of the type of this expression
* opType - type of this expression
- * oper - the Oper node if it is an OPER_EXPR or the
- * Func node if it is a FUNC_EXPR
+ * oper - operator node if needed (Oper, Func, or SubPlan)
* args - arguments to this expression
* ----------------
*/
/* ----------------
* SubLink
- * subLinkType - EXISTS, ALL, ANY, EXPR
- * useor - TRUE for <> (combine op results with "or" not "and")
+ * subLinkType - EXISTS, ALL, ANY, MULTIEXPR, EXPR
+ * useor - TRUE to combine column results with "OR" not "AND"
* 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
*
+ * 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
* in the parse/plan pipeline:
* 1. gram.y delivers a list of the (untransformed) lefthand expressions in
* representation 2 appears in a "bare" SubLink, while representation 3 is
* 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
{
- EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, EXPR_SUBLINK
+ EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, MULTIEXPR_SUBLINK, EXPR_SUBLINK
} SubLinkType;