Commit 05e3d0ee authored by Tom Lane's avatar Tom Lane

Reimplementation of UNION/INTERSECT/EXCEPT. INTERSECT/EXCEPT now meet the

SQL92 semantics, including support for ALL option.  All three can be used
in subqueries and views.  DISTINCT and ORDER BY work now in views, too.
This rewrite fixes many problems with cross-datatype UNIONs and INSERT/SELECT
where the SELECT yields different datatypes than the INSERT needs.  I did
that by making UNION subqueries and SELECT in INSERT be treated like
subselects-in-FROM, thereby allowing an extra level of targetlist where the
datatype conversions can be inserted safely.
INITDB NEEDED!
parent 5292637f
......@@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994-5, Regents of the University of California
*
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.59 2000/09/29 18:21:26 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.60 2000/10/05 19:11:26 tgl Exp $
*
*/
......@@ -197,6 +197,26 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
case T_Unique:
pname = "Unique";
break;
case T_SetOp:
switch (((SetOp *) plan)->cmd)
{
case SETOPCMD_INTERSECT:
pname = "SetOp Intersect";
break;
case SETOPCMD_INTERSECT_ALL:
pname = "SetOp Intersect All";
break;
case SETOPCMD_EXCEPT:
pname = "SetOp Except";
break;
case SETOPCMD_EXCEPT_ALL:
pname = "SetOp Except All";
break;
default:
pname = "SetOp ???";
break;
}
break;
case T_Hash:
pname = "Hash";
break;
......@@ -320,8 +340,6 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
Assert(rtentry != NULL);
rt_store(appendplan->inheritrelid, es->rtable, rtentry);
}
else
es->rtable = nth(whichplan, appendplan->unionrtables);
for (i = 0; i < indent; i++)
appendStringInfo(str, " ");
......
......@@ -4,7 +4,7 @@
# Makefile for executor
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/backend/executor/Makefile,v 1.14 2000/09/29 18:21:28 tgl Exp $
# $Header: /cvsroot/pgsql/src/backend/executor/Makefile,v 1.15 2000/10/05 19:11:26 tgl Exp $
#
#-------------------------------------------------------------------------
......@@ -16,7 +16,7 @@ OBJS = execAmi.o execFlatten.o execJunk.o execMain.o \
execProcnode.o execQual.o execScan.o execTuples.o \
execUtils.o functions.o nodeAppend.o nodeAgg.o nodeHash.o \
nodeHashjoin.o nodeIndexscan.o nodeMaterial.o nodeMergejoin.o \
nodeNestloop.o nodeResult.o nodeSeqscan.o nodeSort.o \
nodeNestloop.o nodeResult.o nodeSeqscan.o nodeSetOp.o nodeSort.o \
nodeUnique.o nodeGroup.o spi.o nodeSubplan.o \
nodeSubqueryscan.o nodeTidscan.o
......
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: execAmi.c,v 1.52 2000/09/29 18:21:28 tgl Exp $
* $Id: execAmi.c,v 1.53 2000/10/05 19:11:26 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -42,6 +42,7 @@
#include "executor/nodeNestloop.h"
#include "executor/nodeResult.h"
#include "executor/nodeSeqscan.h"
#include "executor/nodeSetOp.h"
#include "executor/nodeSort.h"
#include "executor/nodeSubplan.h"
#include "executor/nodeSubqueryscan.h"
......@@ -345,6 +346,10 @@ ExecReScan(Plan *node, ExprContext *exprCtxt, Plan *parent)
ExecReScanUnique((Unique *) node, exprCtxt, parent);
break;
case T_SetOp:
ExecReScanSetOp((SetOp *) node, exprCtxt, parent);
break;
case T_Sort:
ExecReScanSort((Sort *) node, exprCtxt, parent);
break;
......
......@@ -27,7 +27,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.128 2000/09/29 18:21:28 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.129 2000/10/05 19:11:26 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -463,7 +463,6 @@ ExecCheckPlanPerms(Plan *plan, List *rangeTable, CmdType operation)
/* Append implements expansion of inheritance */
ExecCheckRTPerms(app->inheritrtable, operation);
/* Check appended plans w/outer rangetable */
foreach(appendplans, app->appendplans)
{
ExecCheckPlanPerms((Plan *) lfirst(appendplans),
......@@ -474,15 +473,11 @@ ExecCheckPlanPerms(Plan *plan, List *rangeTable, CmdType operation)
else
{
/* Append implements UNION, which must be a SELECT */
List *rtables = app->unionrtables;
/* Check appended plans with their rangetables */
foreach(appendplans, app->appendplans)
{
ExecCheckPlanPerms((Plan *) lfirst(appendplans),
(List *) lfirst(rtables),
rangeTable,
CMD_SELECT);
rtables = lnext(rtables);
}
}
break;
......
......@@ -12,7 +12,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/execProcnode.c,v 1.20 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/execProcnode.c,v 1.21 2000/10/05 19:11:26 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -88,6 +88,7 @@
#include "executor/nodeNestloop.h"
#include "executor/nodeResult.h"
#include "executor/nodeSeqscan.h"
#include "executor/nodeSetOp.h"
#include "executor/nodeSort.h"
#include "executor/nodeSubplan.h"
#include "executor/nodeSubqueryscan.h"
......@@ -199,6 +200,10 @@ ExecInitNode(Plan *node, EState *estate, Plan *parent)
result = ExecInitUnique((Unique *) node, estate, parent);
break;
case T_SetOp:
result = ExecInitSetOp((SetOp *) node, estate, parent);
break;
case T_Group:
result = ExecInitGroup((Group *) node, estate, parent);
break;
......@@ -322,6 +327,10 @@ ExecProcNode(Plan *node, Plan *parent)
result = ExecUnique((Unique *) node);
break;
case T_SetOp:
result = ExecSetOp((SetOp *) node);
break;
case T_Group:
result = ExecGroup((Group *) node);
break;
......@@ -401,6 +410,9 @@ ExecCountSlotsNode(Plan *node)
case T_Unique:
return ExecCountSlotsUnique((Unique *) node);
case T_SetOp:
return ExecCountSlotsSetOp((SetOp *) node);
case T_Group:
return ExecCountSlotsGroup((Group *) node);
......@@ -519,6 +531,10 @@ ExecEndNode(Plan *node, Plan *parent)
ExecEndUnique((Unique *) node);
break;
case T_SetOp:
ExecEndSetOp((SetOp *) node);
break;
case T_Group:
ExecEndGroup((Group *) node);
break;
......
......@@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/execTuples.c,v 1.40 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/execTuples.c,v 1.41 2000/10/05 19:11:26 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -762,6 +762,14 @@ NodeGetResultTupleSlot(Plan *node)
}
break;
case T_SetOp:
{
SetOpState *setopstate = ((SetOp *) node)->setopstate;
slot = setopstate->cstate.cs_ResultTupleSlot;
}
break;
case T_MergeJoin:
{
MergeJoinState *mergestate = ((MergeJoin *) node)->mergestate;
......@@ -783,8 +791,8 @@ NodeGetResultTupleSlot(Plan *node)
* should never get here
* ----------------
*/
elog(ERROR, "NodeGetResultTupleSlot: node not yet supported: %d ",
nodeTag(node));
elog(ERROR, "NodeGetResultTupleSlot: node not yet supported: %d",
(int) nodeTag(node));
return NULL;
}
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.35 2000/07/12 02:37:03 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.36 2000/10/05 19:11:26 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -80,11 +80,9 @@ exec_append_initialize_next(Append *node)
AppendState *appendstate;
TupleTableSlot *result_slot;
List *rangeTable;
int whichplan;
int nplans;
List *rtables;
List *rtable;
List *inheritrtable;
RangeTblEntry *rtentry;
/* ----------------
......@@ -98,8 +96,7 @@ exec_append_initialize_next(Append *node)
whichplan = appendstate->as_whichplan;
nplans = appendstate->as_nplans;
rtables = node->unionrtables;
rtable = node->inheritrtable;
inheritrtable = node->inheritrtable;
if (whichplan < 0)
{
......@@ -131,19 +128,17 @@ exec_append_initialize_next(Append *node)
/* ----------------
* initialize the scan
* (and update the range table appropriately)
*
* (doesn't this leave the range table hosed for anybody upstream
* of the Append node??? - jolly )
* ----------------
*/
if (node->inheritrelid > 0)
{
rtentry = nth(whichplan, rtable);
rtentry = nth(whichplan, inheritrtable);
Assert(rtentry != NULL);
rt_store(node->inheritrelid, rangeTable, rtentry);
}
else
estate->es_range_table = nth(whichplan, rtables);
if (appendstate->as_junkFilter_list)
{
......@@ -181,7 +176,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
{
AppendState *appendstate;
int nplans;
List *rtable;
List *inheritrtable;
List *appendplans;
bool *initialized;
int i;
......@@ -201,7 +196,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
appendplans = node->appendplans;
nplans = length(appendplans);
rtable = node->inheritrtable;
inheritrtable = node->inheritrtable;
initialized = (bool *) palloc(nplans * sizeof(bool));
MemSet(initialized, 0, nplans * sizeof(bool));
......@@ -214,7 +209,6 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
appendstate->as_whichplan = 0;
appendstate->as_nplans = nplans;
appendstate->as_initialized = initialized;
appendstate->as_rtentries = rtable;
node->appendstate = appendstate;
......@@ -250,7 +244,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
inherited_result_rel = true;
foreach(rtentryP, rtable)
foreach(rtentryP, inheritrtable)
{
RangeTblEntry *rtentry = lfirst(rtentryP);
Oid reloid = rtentry->relid;
......@@ -522,8 +516,7 @@ ExecEndAppend(Append *node)
estate->es_result_relation_info = NULL;
/*
* XXX should free appendstate->as_rtentries and
* appendstate->as_junkfilter_list here
* XXX should free appendstate->as_junkfilter_list here
*/
}
void
......
/*-------------------------------------------------------------------------
*
* nodeSetOp.c
* Routines to handle INTERSECT and EXCEPT selection
*
* The input of a SetOp node consists of tuples from two relations,
* which have been combined into one dataset and sorted on all the nonjunk
* attributes. In addition there is a junk attribute that shows which
* relation each tuple came from. The SetOp node scans each group of
* identical tuples to determine how many came from each input relation.
* Then it is a simple matter to emit the output demanded by the SQL spec
* for INTERSECT, INTERSECT ALL, EXCEPT, or EXCEPT ALL.
*
* This node type is not used for UNION or UNION ALL, since those can be
* implemented more cheaply (there's no need for the junk attribute to
* identify the source relation).
*
*
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/nodeSetOp.c,v 1.1 2000/10/05 19:11:26 tgl Exp $
*
*-------------------------------------------------------------------------
*/
/*
* INTERFACE ROUTINES
* ExecSetOp - filter input to generate INTERSECT/EXCEPT results
* ExecInitSetOp - initialize node and subnodes..
* ExecEndSetOp - shutdown node and subnodes
*/
#include "postgres.h"
#include "access/heapam.h"
#include "executor/executor.h"
#include "executor/nodeGroup.h"
#include "executor/nodeSetOp.h"
/* ----------------------------------------------------------------
* ExecSetOp
* ----------------------------------------------------------------
*/
TupleTableSlot * /* return: a tuple or NULL */
ExecSetOp(SetOp *node)
{
SetOpState *setopstate;
TupleTableSlot *resultTupleSlot;
Plan *outerPlan;
TupleDesc tupDesc;
/* ----------------
* get information from the node
* ----------------
*/
setopstate = node->setopstate;
outerPlan = outerPlan((Plan *) node);
resultTupleSlot = setopstate->cstate.cs_ResultTupleSlot;
tupDesc = ExecGetResultType(&setopstate->cstate);
/* ----------------
* If the previously-returned tuple needs to be returned more than
* once, keep returning it.
* ----------------
*/
if (setopstate->numOutput > 0)
{
setopstate->numOutput--;
return resultTupleSlot;
}
/* Flag that we have no current tuple */
ExecClearTuple(resultTupleSlot);
/* ----------------
* Absorb groups of duplicate tuples, counting them, and
* saving the first of each group as a possible return value.
* At the end of each group, decide whether to return anything.
*
* We assume that the tuples arrive in sorted order
* so we can detect duplicates easily.
* ----------------
*/
for (;;)
{
TupleTableSlot *inputTupleSlot;
bool endOfGroup;
/* ----------------
* fetch a tuple from the outer subplan, unless we already did.
* ----------------
*/
if (setopstate->cstate.cs_OuterTupleSlot == NULL &&
! setopstate->subplan_done)
{
setopstate->cstate.cs_OuterTupleSlot =
ExecProcNode(outerPlan, (Plan *) node);
if (TupIsNull(setopstate->cstate.cs_OuterTupleSlot))
setopstate->subplan_done = true;
}
inputTupleSlot = setopstate->cstate.cs_OuterTupleSlot;
if (TupIsNull(resultTupleSlot))
{
/*
* First of group: save a copy in result slot, and reset
* duplicate-counters for new group.
*/
if (setopstate->subplan_done)
return NULL; /* no more tuples */
ExecStoreTuple(heap_copytuple(inputTupleSlot->val),
resultTupleSlot,
InvalidBuffer,
true); /* free copied tuple at ExecClearTuple */
setopstate->numLeft = 0;
setopstate->numRight = 0;
endOfGroup = false;
}
else if (setopstate->subplan_done)
{
/*
* Reached end of input, so finish processing final group
*/
endOfGroup = true;
}
else
{
/*
* Else test if the new tuple and the previously saved tuple match.
*/
if (execTuplesMatch(inputTupleSlot->val,
resultTupleSlot->val,
tupDesc,
node->numCols, node->dupColIdx,
setopstate->eqfunctions,
setopstate->tempContext))
endOfGroup = false;
else
endOfGroup = true;
}
if (endOfGroup)
{
/*
* We've reached the end of the group containing resultTuple.
* Decide how many copies (if any) to emit. This logic is
* straight from the SQL92 specification.
*/
switch (node->cmd)
{
case SETOPCMD_INTERSECT:
if (setopstate->numLeft > 0 && setopstate->numRight > 0)
setopstate->numOutput = 1;
else
setopstate->numOutput = 0;
break;
case SETOPCMD_INTERSECT_ALL:
setopstate->numOutput =
(setopstate->numLeft < setopstate->numRight) ?
setopstate->numLeft : setopstate->numRight;
break;
case SETOPCMD_EXCEPT:
if (setopstate->numLeft > 0 && setopstate->numRight == 0)
setopstate->numOutput = 1;
else
setopstate->numOutput = 0;
break;
case SETOPCMD_EXCEPT_ALL:
setopstate->numOutput =
(setopstate->numLeft < setopstate->numRight) ?
0 : (setopstate->numLeft - setopstate->numRight);
break;
default:
elog(ERROR, "ExecSetOp: bogus command code %d",
(int) node->cmd);
break;
}
/* Fall out of for-loop if we have tuples to emit */
if (setopstate->numOutput > 0)
break;
/* Else flag that we have no current tuple, and loop around */
ExecClearTuple(resultTupleSlot);
}
else
{
/*
* Current tuple is member of same group as resultTuple.
* Count it in the appropriate counter.
*/
int flag;
bool isNull;
flag = DatumGetInt32(heap_getattr(inputTupleSlot->val,
node->flagColIdx,
tupDesc,
&isNull));
Assert(!isNull);
if (flag)
setopstate->numRight++;
else
setopstate->numLeft++;
/* Set flag to fetch a new input tuple, and loop around */
setopstate->cstate.cs_OuterTupleSlot = NULL;
}
}
/*
* If we fall out of loop, then we need to emit at least one copy
* of resultTuple.
*/
Assert(setopstate->numOutput > 0);
setopstate->numOutput--;
return resultTupleSlot;
}
/* ----------------------------------------------------------------
* ExecInitSetOp
*
* This initializes the setop node state structures and
* the node's subplan.
* ----------------------------------------------------------------
*/
bool /* return: initialization status */
ExecInitSetOp(SetOp *node, EState *estate, Plan *parent)
{
SetOpState *setopstate;
Plan *outerPlan;
/* ----------------
* assign execution state to node
* ----------------
*/
node->plan.state = estate;
/* ----------------
* create new SetOpState for node
* ----------------
*/
setopstate = makeNode(SetOpState);
node->setopstate = setopstate;
setopstate->cstate.cs_OuterTupleSlot = NULL;
setopstate->subplan_done = false;
setopstate->numOutput = 0;
/* ----------------
* Miscellaneous initialization
*
* SetOp nodes have no ExprContext initialization because
* they never call ExecQual or ExecProject. But they do need a
* per-tuple memory context anyway for calling execTuplesMatch.
* ----------------
*/
setopstate->tempContext =
AllocSetContextCreate(CurrentMemoryContext,
"SetOp",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
#define SETOP_NSLOTS 1
/* ------------
* Tuple table initialization
* ------------
*/
ExecInitResultTupleSlot(estate, &setopstate->cstate);
/* ----------------
* then initialize outer plan
* ----------------
*/
outerPlan = outerPlan((Plan *) node);
ExecInitNode(outerPlan, estate, (Plan *) node);
/* ----------------
* setop nodes do no projections, so initialize
* projection info for this node appropriately
* ----------------
*/
ExecAssignResultTypeFromOuterPlan((Plan *) node, &setopstate->cstate);
setopstate->cstate.cs_ProjInfo = NULL;
/*
* Precompute fmgr lookup data for inner loop
*/
setopstate->eqfunctions =
execTuplesMatchPrepare(ExecGetResultType(&setopstate->cstate),
node->numCols,
node->dupColIdx);
return TRUE;
}
int
ExecCountSlotsSetOp(SetOp *node)
{
return ExecCountSlotsNode(outerPlan(node)) +
ExecCountSlotsNode(innerPlan(node)) +
SETOP_NSLOTS;
}
/* ----------------------------------------------------------------
* ExecEndSetOp
*
* This shuts down the subplan and frees resources allocated
* to this node.
* ----------------------------------------------------------------
*/
void
ExecEndSetOp(SetOp *node)
{
SetOpState *setopstate = node->setopstate;
ExecEndNode(outerPlan((Plan *) node), (Plan *) node);
MemoryContextDelete(setopstate->tempContext);
/* clean up tuple table */
ExecClearTuple(setopstate->cstate.cs_ResultTupleSlot);
setopstate->cstate.cs_OuterTupleSlot = NULL;
}
void
ExecReScanSetOp(SetOp *node, ExprContext *exprCtxt, Plan *parent)
{
SetOpState *setopstate = node->setopstate;
ExecClearTuple(setopstate->cstate.cs_ResultTupleSlot);
setopstate->cstate.cs_OuterTupleSlot = NULL;
setopstate->subplan_done = false;
setopstate->numOutput = 0;
/*
* if chgParam of subnode is not null then plan will be re-scanned by
* first ExecProcNode.
*/
if (((Plan *) node)->lefttree->chgParam == NULL)
ExecReScan(((Plan *) node)->lefttree, exprCtxt, (Plan *) node);
}
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/nodeSubplan.c,v 1.27 2000/08/24 03:29:03 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/nodeSubplan.c,v 1.28 2000/10/05 19:11:26 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -328,7 +328,14 @@ ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent)
/* ----------------------------------------------------------------
* ExecSetParamPlan
*
* Executes plan of node and sets parameters.
* Executes an InitPlan subplan and sets its output parameters.
*
* This is called from ExecEvalParam() when the value of a PARAM_EXEC
* parameter is requested and the param's execPlan field is set (indicating
* that the param has not yet been evaluated). This allows lazy evaluation
* of initplans: we don't run the subplan until/unless we need its output.
* Note that this routine MUST clear the execPlan fields of the plan's
* output parameters after evaluating them!
* ----------------------------------------------------------------
*/
void
......@@ -424,13 +431,13 @@ ExecSetParamPlan(SubPlan *node, ExprContext *econtext)
}
}
MemoryContextSwitchTo(oldcontext);
if (plan->extParam == NULL) /* un-correlated ... */
{
ExecEndNode(plan, plan);
node->needShutdown = false;
}
MemoryContextSwitchTo(oldcontext);
}
/* ----------------------------------------------------------------
......@@ -470,6 +477,9 @@ ExecReScanSetParamPlan(SubPlan *node, Plan *parent)
* node->plan->chgParam is not NULL... ExecReScan (plan, NULL, plan);
*/
/*
* Mark this subplan's output parameters as needing recalculation
*/
foreach(lst, node->setParam)
{
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(lst)]);
......
......@@ -3,12 +3,16 @@
* nodeSubqueryscan.c
* Support routines for scanning subqueries (subselects in rangetable).
*
* This is just enough different from sublinks (nodeSubplan.c) to mean that
* we need two sets of code. Ought to look at trying to unify the cases.
*
*
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/nodeSubqueryscan.c,v 1.1 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/nodeSubqueryscan.c,v 1.2 2000/10/05 19:11:26 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -49,9 +53,7 @@ SubqueryNext(SubqueryScan *node)
SubqueryScanState *subquerystate;
EState *estate;
ScanDirection direction;
int execdir;
TupleTableSlot *slot;
Const countOne;
/* ----------------
* get information from the estate and scan state
......@@ -60,7 +62,6 @@ SubqueryNext(SubqueryScan *node)
estate = node->scan.plan.state;
subquerystate = (SubqueryScanState *) node->scan.scanstate;
direction = estate->es_direction;
execdir = ScanDirectionIsBackward(direction) ? EXEC_BACK : EXEC_FOR;
slot = subquerystate->csstate.css_ScanTupleSlot;
/*
......@@ -85,25 +86,13 @@ SubqueryNext(SubqueryScan *node)
return (slot);
}
memset(&countOne, 0, sizeof(countOne));
countOne.type = T_Const;
countOne.consttype = INT4OID;
countOne.constlen = sizeof(int4);
countOne.constvalue = Int32GetDatum(1);
countOne.constisnull = false;
countOne.constbyval = true;
countOne.constisset = false;
countOne.constiscast = false;
/* ----------------
* get the next tuple from the sub-query
* ----------------
*/
slot = ExecutorRun(subquerystate->sss_SubQueryDesc,
subquerystate->sss_SubEState,
execdir,
NULL, /* offset */
(Node *) &countOne);
subquerystate->sss_SubEState->es_direction = direction;
slot = ExecProcNode(node->subplan, node->subplan);
subquerystate->csstate.css_ScanTupleSlot = slot;
......@@ -139,6 +128,7 @@ ExecInitSubqueryScan(SubqueryScan *node, EState *estate, Plan *parent)
{
SubqueryScanState *subquerystate;
RangeTblEntry *rte;
EState *sp_estate;
/* ----------------
* SubqueryScan should not have any "normal" children.
......@@ -177,18 +167,25 @@ ExecInitSubqueryScan(SubqueryScan *node, EState *estate, Plan *parent)
/* ----------------
* initialize subquery
*
* This should agree with ExecInitSubPlan
* ----------------
*/
rte = rt_fetch(node->scan.scanrelid, estate->es_range_table);
Assert(rte->subquery != NULL);
subquerystate->sss_SubQueryDesc = CreateQueryDesc(rte->subquery,
node->subplan,
None);
subquerystate->sss_SubEState = CreateExecutorState();
sp_estate = CreateExecutorState();
subquerystate->sss_SubEState = sp_estate;
sp_estate->es_range_table = rte->subquery->rtable;
sp_estate->es_param_list_info = estate->es_param_list_info;
sp_estate->es_param_exec_vals = estate->es_param_exec_vals;
sp_estate->es_tupleTable =
ExecCreateTupleTable(ExecCountSlotsNode(node->subplan) + 10);
sp_estate->es_snapshot = estate->es_snapshot;
ExecutorStart(subquerystate->sss_SubQueryDesc,
subquerystate->sss_SubEState);
if (!ExecInitNode(node->subplan, sp_estate, NULL))
return false;
subquerystate->csstate.css_ScanTupleSlot = NULL;
subquerystate->csstate.cstate.cs_TupFromTlist = false;
......@@ -247,10 +244,9 @@ ExecEndSubqueryScan(SubqueryScan *node)
* close down subquery
* ----------------
*/
ExecutorEnd(subquerystate->sss_SubQueryDesc,
subquerystate->sss_SubEState);
ExecEndNode(node->subplan, node->subplan);
/* XXX we seem to be leaking the querydesc and sub-EState... */
/* XXX we seem to be leaking the sub-EState and tuple table... */
subquerystate->csstate.css_ScanTupleSlot = NULL;
......@@ -284,6 +280,7 @@ ExecSubqueryReScan(SubqueryScan *node, ExprContext *exprCtxt, Plan *parent)
return;
}
ExecReScan(node->subplan, NULL, NULL);
ExecReScan(node->subplan, NULL, node->subplan);
subquerystate->csstate.css_ScanTupleSlot = NULL;
}
......@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.123 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.124 2000/10/05 19:11:27 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -176,7 +176,6 @@ _copyAppend(Append *from)
* ----------------
*/
Node_Copy(from, newnode, appendplans);
Node_Copy(from, newnode, unionrtables);
newnode->inheritrelid = from->inheritrelid;
Node_Copy(from, newnode, inheritrtable);
......@@ -565,6 +564,33 @@ _copyUnique(Unique *from)
return newnode;
}
/* ----------------
* _copySetOp
* ----------------
*/
static SetOp *
_copySetOp(SetOp *from)
{
SetOp *newnode = makeNode(SetOp);
/* ----------------
* copy node superclass fields
* ----------------
*/
CopyPlanFields((Plan *) from, (Plan *) newnode);
/* ----------------
* copy remainder of node
* ----------------
*/
newnode->cmd = from->cmd;
newnode->numCols = from->numCols;
newnode->dupColIdx = palloc(from->numCols * sizeof(AttrNumber));
memcpy(newnode->dupColIdx, from->dupColIdx, from->numCols * sizeof(AttrNumber));
newnode->flagColIdx = from->flagColIdx;
return newnode;
}
/* ----------------
* _copyHash
......@@ -1696,28 +1722,26 @@ _copyQuery(Query *from)
newnode->isPortal = from->isPortal;
newnode->isBinary = from->isBinary;
newnode->isTemp = from->isTemp;
newnode->unionall = from->unionall;
newnode->hasAggs = from->hasAggs;
newnode->hasSubLinks = from->hasSubLinks;
Node_Copy(from, newnode, rtable);
Node_Copy(from, newnode, jointree);
Node_Copy(from, newnode, targetList);
newnode->rowMarks = listCopy(from->rowMarks);
Node_Copy(from, newnode, distinctClause);
Node_Copy(from, newnode, sortClause);
Node_Copy(from, newnode, targetList);
Node_Copy(from, newnode, groupClause);
Node_Copy(from, newnode, havingQual);
/* why is intersectClause missing? */
Node_Copy(from, newnode, unionClause);
Node_Copy(from, newnode, distinctClause);
Node_Copy(from, newnode, sortClause);
Node_Copy(from, newnode, limitOffset);
Node_Copy(from, newnode, limitCount);
Node_Copy(from, newnode, setOperations);
/*
* We do not copy the planner internal fields: base_rel_list,
* join_rel_list, equi_key_list, query_pathkeys. Not entirely clear if
......@@ -1734,17 +1758,9 @@ _copyInsertStmt(InsertStmt *from)
if (from->relname)
newnode->relname = pstrdup(from->relname);
Node_Copy(from, newnode, distinctClause);
Node_Copy(from, newnode, cols);
Node_Copy(from, newnode, targetList);
Node_Copy(from, newnode, fromClause);
Node_Copy(from, newnode, whereClause);
Node_Copy(from, newnode, groupClause);
Node_Copy(from, newnode, havingClause);
Node_Copy(from, newnode, unionClause);
newnode->unionall = from->unionall;
Node_Copy(from, newnode, intersectClause);
Node_Copy(from, newnode, forUpdate);
Node_Copy(from, newnode, selectStmt);
return newnode;
}
......@@ -1790,15 +1806,11 @@ _copySelectStmt(SelectStmt *from)
Node_Copy(from, newnode, whereClause);
Node_Copy(from, newnode, groupClause);
Node_Copy(from, newnode, havingClause);
Node_Copy(from, newnode, intersectClause);
Node_Copy(from, newnode, exceptClause);
Node_Copy(from, newnode, unionClause);
Node_Copy(from, newnode, sortClause);
if (from->portalname)
newnode->portalname = pstrdup(from->portalname);
newnode->binary = from->binary;
newnode->istemp = from->istemp;
newnode->unionall = from->unionall;
Node_Copy(from, newnode, limitOffset);
Node_Copy(from, newnode, limitCount);
Node_Copy(from, newnode, forUpdate);
......@@ -1806,6 +1818,20 @@ _copySelectStmt(SelectStmt *from)
return newnode;
}
static SetOperationStmt *
_copySetOperationStmt(SetOperationStmt *from)
{
SetOperationStmt *newnode = makeNode(SetOperationStmt);
newnode->op = from->op;
newnode->all = from->all;
Node_Copy(from, newnode, larg);
Node_Copy(from, newnode, rarg);
newnode->colTypes = listCopy(from->colTypes);
return newnode;
}
static AlterTableStmt *
_copyAlterTableStmt(AlterTableStmt *from)
{
......@@ -2553,6 +2579,9 @@ copyObject(void *from)
case T_Unique:
retval = _copyUnique(from);
break;
case T_SetOp:
retval = _copySetOp(from);
break;
case T_Hash:
retval = _copyHash(from);
break;
......@@ -2700,6 +2729,9 @@ copyObject(void *from)
case T_SelectStmt:
retval = _copySelectStmt(from);
break;
case T_SetOperationStmt:
retval = _copySetOperationStmt(from);
break;
case T_AlterTableStmt:
retval = _copyAlterTableStmt(from);
break;
......
......@@ -20,7 +20,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.74 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.75 2000/10/05 19:11:27 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -33,8 +33,6 @@
#include "utils/datum.h"
static bool equali(List *a, List *b);
/* Macro for comparing string fields that might be NULL */
#define equalstr(a, b) \
(((a) != NULL && (b) != NULL) ? (strcmp(a, b) == 0) : (a) == (b))
......@@ -600,8 +598,6 @@ _equalQuery(Query *a, Query *b)
return false;
if (a->isTemp != b->isTemp)
return false;
if (a->unionall != b->unionall)
return false;
if (a->hasAggs != b->hasAggs)
return false;
if (a->hasSubLinks != b->hasSubLinks)
......@@ -610,26 +606,24 @@ _equalQuery(Query *a, Query *b)
return false;
if (!equal(a->jointree, b->jointree))
return false;
if (!equal(a->targetList, b->targetList))
return false;
if (!equali(a->rowMarks, b->rowMarks))
return false;
if (!equal(a->distinctClause, b->distinctClause))
return false;
if (!equal(a->sortClause, b->sortClause))
if (!equal(a->targetList, b->targetList))
return false;
if (!equal(a->groupClause, b->groupClause))
return false;
if (!equal(a->havingQual, b->havingQual))
return false;
if (!equal(a->intersectClause, b->intersectClause))
if (!equal(a->distinctClause, b->distinctClause))
return false;
if (!equal(a->unionClause, b->unionClause))
if (!equal(a->sortClause, b->sortClause))
return false;
if (!equal(a->limitOffset, b->limitOffset))
return false;
if (!equal(a->limitCount, b->limitCount))
return false;
if (!equal(a->setOperations, b->setOperations))
return false;
/*
* We do not check the internal-to-the-planner fields: base_rel_list,
......@@ -645,27 +639,11 @@ _equalInsertStmt(InsertStmt *a, InsertStmt *b)
{
if (!equalstr(a->relname, b->relname))
return false;
if (!equal(a->distinctClause, b->distinctClause))
return false;
if (!equal(a->cols, b->cols))
return false;
if (!equal(a->targetList, b->targetList))
return false;
if (!equal(a->fromClause, b->fromClause))
return false;
if (!equal(a->whereClause, b->whereClause))
return false;
if (!equal(a->groupClause, b->groupClause))
return false;
if (!equal(a->havingClause, b->havingClause))
return false;
if (!equal(a->unionClause, b->unionClause))
return false;
if (a->unionall != b->unionall)
return false;
if (!equal(a->intersectClause, b->intersectClause))
return false;
if (!equal(a->forUpdate, b->forUpdate))
if (!equal(a->selectStmt, b->selectStmt))
return false;
return true;
......@@ -718,12 +696,6 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return false;
if (!equal(a->havingClause, b->havingClause))
return false;
if (!equal(a->intersectClause, b->intersectClause))
return false;
if (!equal(a->exceptClause, b->exceptClause))
return false;
if (!equal(a->unionClause, b->unionClause))
return false;
if (!equal(a->sortClause, b->sortClause))
return false;
if (!equalstr(a->portalname, b->portalname))
......@@ -732,8 +704,6 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return false;
if (a->istemp != b->istemp)
return false;
if (a->unionall != b->unionall)
return false;
if (!equal(a->limitOffset, b->limitOffset))
return false;
if (!equal(a->limitCount, b->limitCount))
......@@ -744,6 +714,23 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return true;
}
static bool
_equalSetOperationStmt(SetOperationStmt *a, SetOperationStmt *b)
{
if (a->op != b->op)
return false;
if (a->all != b->all)
return false;
if (!equal(a->larg, b->larg))
return false;
if (!equal(a->rarg, b->rarg))
return false;
if (!equali(a->colTypes, b->colTypes))
return false;
return true;
}
static bool
_equalAlterTableStmt(AlterTableStmt *a, AlterTableStmt *b)
{
......@@ -1929,6 +1916,9 @@ equal(void *a, void *b)
case T_SelectStmt:
retval = _equalSelectStmt(a, b);
break;
case T_SetOperationStmt:
retval = _equalSetOperationStmt(a, b);
break;
case T_AlterTableStmt:
retval = _equalAlterTableStmt(a, b);
break;
......@@ -2159,25 +2149,3 @@ equal(void *a, void *b)
return retval;
}
/*
* equali
* compares two lists of integers
*/
static bool
equali(List *a, List *b)
{
List *l;
foreach(l, a)
{
if (b == NIL)
return false;
if (lfirsti(l) != lfirsti(b))
return false;
b = lnext(b);
}
if (b != NIL)
return false;
return true;
}
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/list.c,v 1.34 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/list.c,v 1.35 2000/10/05 19:11:27 tgl Exp $
*
* NOTES
* XXX a few of the following functions are duplicated to handle
......@@ -236,11 +236,33 @@ freeList(List *list)
}
}
/*
* equali
* compares two lists of integers
*/
bool
equali(List *list1, List *list2)
{
List *l;
foreach(l, list1)
{
if (list2 == NIL)
return false;
if (lfirsti(l) != lfirsti(list2))
return false;
list2 = lnext(list2);
}
if (list2 != NIL)
return false;
return true;
}
/*
* sameseti
*
* Returns t if two integer lists contain the same elements
* (but unlike equal(), they need not be in the same order)
* (but unlike equali(), they need not be in the same order)
*
* Caution: this routine could be fooled if list1 contains
* duplicate elements. It is intended to be used on lists
......
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.127 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.128 2000/10/05 19:11:27 tgl Exp $
*
* NOTES
* Every (plan) node in POSTGRES has an associated "out" routine which
......@@ -147,6 +147,7 @@ _outIndexStmt(StringInfo str, IndexStmt *node)
static void
_outSelectStmt(StringInfo str, SelectStmt *node)
{
/* XXX this is pretty durn incomplete */
appendStringInfo(str, "SELECT :where ");
_outNode(str, node->whereClause);
}
......@@ -258,11 +259,10 @@ _outQuery(StringInfo str, Query *node)
_outToken(str, node->into);
appendStringInfo(str, " :isPortal %s :isBinary %s :isTemp %s"
" :unionall %s :hasAggs %s :hasSubLinks %s :rtable ",
" :hasAggs %s :hasSubLinks %s :rtable ",
node->isPortal ? "true" : "false",
node->isBinary ? "true" : "false",
node->isTemp ? "true" : "false",
node->unionall ? "true" : "false",
node->hasAggs ? "true" : "false",
node->hasSubLinks ? "true" : "false");
_outNode(str, node->rtable);
......@@ -270,17 +270,11 @@ _outQuery(StringInfo str, Query *node)
appendStringInfo(str, " :jointree ");
_outNode(str, node->jointree);
appendStringInfo(str, " :targetList ");
_outNode(str, node->targetList);
appendStringInfo(str, " :rowMarks ");
_outIntList(str, node->rowMarks);
appendStringInfo(str, " :distinctClause ");
_outNode(str, node->distinctClause);
appendStringInfo(str, " :sortClause ");
_outNode(str, node->sortClause);
appendStringInfo(str, " :targetList ");
_outNode(str, node->targetList);
appendStringInfo(str, " :groupClause ");
_outNode(str, node->groupClause);
......@@ -288,17 +282,20 @@ _outQuery(StringInfo str, Query *node)
appendStringInfo(str, " :havingQual ");
_outNode(str, node->havingQual);
appendStringInfo(str, " :intersectClause ");
_outNode(str, node->intersectClause);
appendStringInfo(str, " :distinctClause ");
_outNode(str, node->distinctClause);
appendStringInfo(str, " :unionClause ");
_outNode(str, node->unionClause);
appendStringInfo(str, " :sortClause ");
_outNode(str, node->sortClause);
appendStringInfo(str, " :limitOffset ");
_outNode(str, node->limitOffset);
appendStringInfo(str, " :limitCount ");
_outNode(str, node->limitCount);
appendStringInfo(str, " :setOperations ");
_outNode(str, node->setOperations);
}
static void
......@@ -315,6 +312,19 @@ _outGroupClause(StringInfo str, GroupClause *node)
node->tleSortGroupRef, node->sortop);
}
static void
_outSetOperationStmt(StringInfo str, SetOperationStmt *node)
{
appendStringInfo(str, " SETOPERATIONSTMT :op %d :all %s :larg ",
(int) node->op,
node->all ? "true" : "false");
_outNode(str, node->larg);
appendStringInfo(str, " :rarg ");
_outNode(str, node->rarg);
appendStringInfo(str, " :colTypes ");
_outIntList(str, node->colTypes);
}
/*
* print the basic stuff of all nodes that inherit from Plan
*/
......@@ -384,11 +394,7 @@ _outAppend(StringInfo str, Append *node)
appendStringInfo(str, " :appendplans ");
_outNode(str, node->appendplans);
appendStringInfo(str, " :unionrtables ");
_outNode(str, node->unionrtables);
appendStringInfo(str,
" :inheritrelid %u :inheritrtable ",
appendStringInfo(str, " :inheritrelid %u :inheritrtable ",
node->inheritrelid);
_outNode(str, node->inheritrtable);
}
......@@ -601,6 +607,22 @@ _outUnique(StringInfo str, Unique *node)
appendStringInfo(str, "%d ", (int) node->uniqColIdx[i]);
}
static void
_outSetOp(StringInfo str, SetOp *node)
{
int i;
appendStringInfo(str, " SETOP ");
_outPlanInfo(str, (Plan *) node);
appendStringInfo(str, " :cmd %d :numCols %d :dupColIdx ",
(int) node->cmd, node->numCols);
for (i = 0; i < node->numCols; i++)
appendStringInfo(str, "%d ", (int) node->dupColIdx[i]);
appendStringInfo(str, " :flagColIdx %d ",
(int) node->flagColIdx);
}
/*
* Hash is a subclass of Plan
*/
......@@ -1480,6 +1502,9 @@ _outNode(StringInfo str, void *obj)
case T_GroupClause:
_outGroupClause(str, obj);
break;
case T_SetOperationStmt:
_outSetOperationStmt(str, obj);
break;
case T_Plan:
_outPlan(str, obj);
break;
......@@ -1531,6 +1556,9 @@ _outNode(StringInfo str, void *obj)
case T_Unique:
_outUnique(str, obj);
break;
case T_SetOp:
_outSetOp(str, obj);
break;
case T_Hash:
_outHash(str, obj);
break;
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/print.c,v 1.42 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/print.c,v 1.43 2000/10/05 19:11:27 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
......@@ -338,6 +338,9 @@ plannode_type(Plan *p)
case T_Unique:
return "UNIQUE";
break;
case T_SetOp:
return "SETOP";
break;
case T_Hash:
return "HASH";
break;
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.97 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.98 2000/10/05 19:11:27 tgl Exp $
*
* NOTES
* Most of the read functions for plan nodes are tested. (In fact, they
......@@ -109,10 +109,6 @@ _readQuery()
token = lsptok(NULL, &length); /* get isTemp */
local_node->isTemp = (token[0] == 't') ? true : false;
token = lsptok(NULL, &length); /* skip :unionall */
token = lsptok(NULL, &length); /* get unionall */
local_node->unionall = (token[0] == 't') ? true : false;
token = lsptok(NULL, &length); /* skip the :hasAggs */
token = lsptok(NULL, &length); /* get hasAggs */
local_node->hasAggs = (token[0] == 't') ? true : false;
......@@ -127,17 +123,11 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :jointree */
local_node->jointree = nodeRead(true);
token = lsptok(NULL, &length); /* skip :targetlist */
local_node->targetList = nodeRead(true);
token = lsptok(NULL, &length); /* skip :rowMarks */
local_node->rowMarks = toIntList(nodeRead(true));
token = lsptok(NULL, &length); /* skip :distinctClause */
local_node->distinctClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :sortClause */
local_node->sortClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :targetlist */
local_node->targetList = nodeRead(true);
token = lsptok(NULL, &length); /* skip :groupClause */
local_node->groupClause = nodeRead(true);
......@@ -145,11 +135,11 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :havingQual */
local_node->havingQual = nodeRead(true);
token = lsptok(NULL, &length); /* skip :intersectClause */
local_node->intersectClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :distinctClause */
local_node->distinctClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :unionClause */
local_node->unionClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :sortClause */
local_node->sortClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :limitOffset */
local_node->limitOffset = nodeRead(true);
......@@ -157,6 +147,9 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :limitCount */
local_node->limitCount = nodeRead(true);
token = lsptok(NULL, &length); /* skip :setOperations */
local_node->setOperations = nodeRead(true);
return local_node;
}
......@@ -208,6 +201,39 @@ _readGroupClause()
return local_node;
}
/* ----------------
* _readSetOperationStmt
* ----------------
*/
static SetOperationStmt *
_readSetOperationStmt()
{
SetOperationStmt *local_node;
char *token;
int length;
local_node = makeNode(SetOperationStmt);
token = lsptok(NULL, &length); /* eat :op */
token = lsptok(NULL, &length); /* get op */
local_node->op = (SetOperation) atoi(token);
token = lsptok(NULL, &length); /* eat :all */
token = lsptok(NULL, &length); /* get all */
local_node->all = (token[0] == 't') ? true : false;
token = lsptok(NULL, &length); /* eat :larg */
local_node->larg = nodeRead(true); /* get larg */
token = lsptok(NULL, &length); /* eat :rarg */
local_node->rarg = nodeRead(true); /* get rarg */
token = lsptok(NULL, &length); /* eat :colTypes */
local_node->colTypes = toIntList(nodeRead(true));
return local_node;
}
/* ----------------
* _getPlan
* ----------------
......@@ -322,9 +348,6 @@ _readAppend()
token = lsptok(NULL, &length); /* eat :appendplans */
local_node->appendplans = nodeRead(true); /* now read it */
token = lsptok(NULL, &length); /* eat :unionrtables */
local_node->unionrtables = nodeRead(true); /* now read it */
token = lsptok(NULL, &length); /* eat :inheritrelid */
token = lsptok(NULL, &length); /* get inheritrelid */
local_node->inheritrelid = strtoul(token, NULL, 10);
......@@ -1995,6 +2018,8 @@ parsePlanString(void)
return_value = _readSortClause();
else if (length == 11 && strncmp(token, "GROUPCLAUSE", length) == 0)
return_value = _readGroupClause();
else if (length == 16 && strncmp(token, "SETOPERATIONSTMT", length) == 0)
return_value = _readSetOperationStmt();
else if (length == 4 && strncmp(token, "CASE", length) == 0)
return_value = _readCaseExpr();
else if (length == 4 && strncmp(token, "WHEN", length) == 0)
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.65 2000/09/29 18:21:31 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.66 2000/10/05 19:11:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -98,7 +98,8 @@ set_base_rel_pathlist(Query *root)
*/
/* Generate the plan for the subquery */
rel->subplan = planner(rte->subquery);
rel->subplan = subquery_planner(rte->subquery,
-1.0 /* default case */ );
/* Copy number of output rows from subplan */
rel->tuples = rel->subplan->plan_rows;
......
......@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.97 2000/09/29 18:21:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.98 2000/10/05 19:11:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -68,8 +68,6 @@ static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
ScanDirection indexscandir);
static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
List *tideval);
static SubqueryScan *make_subqueryscan(List *qptlist, List *qpqual,
Index scanrelid, Plan *subplan);
static NestLoop *make_nestloop(List *tlist,
List *joinclauses, List *otherclauses,
Plan *lefttree, Plan *righttree,
......@@ -86,7 +84,6 @@ static MergeJoin *make_mergejoin(List *tlist,
Plan *lefttree, Plan *righttree,
JoinType jointype);
static void copy_path_costsize(Plan *dest, Path *src);
static void copy_plan_costsize(Plan *dest, Plan *src);
/*
* create_plan
......@@ -1109,7 +1106,7 @@ copy_path_costsize(Plan *dest, Path *src)
* but it helps produce more reasonable-looking EXPLAIN output.
* (Some callers alter the info after copying it.)
*/
static void
void
copy_plan_costsize(Plan *dest, Plan *src)
{
if (src)
......@@ -1206,7 +1203,7 @@ make_tidscan(List *qptlist,
return node;
}
static SubqueryScan *
SubqueryScan *
make_subqueryscan(List *qptlist,
List *qpqual,
Index scanrelid,
......@@ -1593,6 +1590,67 @@ make_unique(List *tlist, Plan *lefttree, List *distinctList)
return node;
}
/*
* distinctList is a list of SortClauses, identifying the targetlist items
* that should be considered by the SetOp filter.
*/
SetOp *
make_setop(SetOpCmd cmd, List *tlist, Plan *lefttree,
List *distinctList, AttrNumber flagColIdx)
{
SetOp *node = makeNode(SetOp);
Plan *plan = &node->plan;
int numCols = length(distinctList);
int keyno = 0;
AttrNumber *dupColIdx;
List *slitem;
copy_plan_costsize(plan, lefttree);
/*
* Charge one cpu_operator_cost per comparison per input tuple. We
* assume all columns get compared at most of the tuples.
*/
plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
/*
* As for Group, we make the unsupported assumption that there will be
* 10% as many tuples out as in.
*/
plan->plan_rows *= 0.1;
if (plan->plan_rows < 1)
plan->plan_rows = 1;
plan->state = (EState *) NULL;
plan->targetlist = tlist;
plan->qual = NIL;
plan->lefttree = lefttree;
plan->righttree = NULL;
/*
* convert SortClause list into array of attr indexes, as wanted by
* exec
*/
Assert(numCols > 0);
dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
foreach(slitem, distinctList)
{
SortClause *sortcl = (SortClause *) lfirst(slitem);
TargetEntry *tle = get_sortgroupclause_tle(sortcl, tlist);
dupColIdx[keyno++] = tle->resdom->resno;
}
node->cmd = cmd;
node->numCols = numCols;
node->dupColIdx = dupColIdx;
node->flagColIdx = flagColIdx;
return node;
}
Result *
make_result(List *tlist,
Node *resconstantqual,
......
......@@ -14,7 +14,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.60 2000/09/29 18:21:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.61 2000/10/05 19:11:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -174,8 +174,6 @@ subplanner(Query *root,
List *brel;
RelOptInfo *final_rel;
Plan *resultplan;
MemoryContext mycontext;
MemoryContext oldcxt;
Path *cheapestpath;
Path *presortedpath;
......@@ -227,24 +225,6 @@ subplanner(Query *root,
*/
root->query_pathkeys = canonicalize_pathkeys(root, root->query_pathkeys);
/*
* We might allocate quite a lot of storage during planning (due to
* constructing lots of Paths), but all of it can be reclaimed after
* we generate the finished Plan tree. Work in a temporary context
* to let that happen. We make the context a child of
* TransactionCommandContext so it will be freed if error abort.
*
* Note: beware of trying to move this up to the start of this routine.
* Some of the data structures built above --- notably the pathkey
* equivalence sets --- will still be needed after this routine exits.
*/
mycontext = AllocSetContextCreate(TransactionCommandContext,
"Planner",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
oldcxt = MemoryContextSwitchTo(mycontext);
/*
* Ready to do the primary planning.
*/
......@@ -355,25 +335,5 @@ subplanner(Query *root,
plan_built:
/*
* Must copy the completed plan tree and its pathkeys out of temporary
* context. We also have to copy the rtable in case it contains any
* subqueries. (If it does, they'll have been modified during the
* recursive invocation of planner.c, and hence will contain substructure
* allocated in my temporary context...)
*/
MemoryContextSwitchTo(oldcxt);
resultplan = copyObject(resultplan);
root->query_pathkeys = copyObject(root->query_pathkeys);
root->rtable = copyObject(root->rtable);
/*
* Now we can release the Path storage.
*/
MemoryContextDelete(mycontext);
return resultplan;
}
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.91 2000/09/29 18:21:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.92 2000/10/05 19:11:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -48,7 +48,6 @@ static List *make_subplanTargetList(Query *parse, List *tlist,
static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
List *groupClause, AttrNumber *grpColIdx,
bool is_presorted, Plan *subplan);
static Plan *make_sortplan(List *tlist, Plan *plannode, List *sortcls);
/*****************************************************************************
*
......@@ -60,43 +59,32 @@ planner(Query *parse)
{
Plan *result_plan;
Index save_PlannerQueryLevel;
List *save_PlannerInitPlan;
List *save_PlannerParamVar;
int save_PlannerPlanId;
/*
* The outer planner can be called recursively, for example to process
* a subquery in the rangetable. (A less obvious example occurs when
* eval_const_expressions tries to simplify an SQL function.)
* So, global state variables must be saved and restored.
* The planner can be called recursively (an example is when
* eval_const_expressions tries to simplify an SQL function).
* So, these global state variables must be saved and restored.
*
* (Perhaps these should be moved into the Query structure instead?)
* These vars cannot be moved into the Query structure since their
* whole purpose is communication across multiple sub-Queries.
*
* Note we do NOT save and restore PlannerPlanId: it exists to assign
* unique IDs to SubPlan nodes, and we want those IDs to be unique
* for the life of a backend. Also, PlannerInitPlan is saved/restored
* in subquery_planner, not here.
*/
save_PlannerQueryLevel = PlannerQueryLevel;
save_PlannerInitPlan = PlannerInitPlan;
save_PlannerParamVar = PlannerParamVar;
save_PlannerPlanId = PlannerPlanId;
/* Initialize state for subselects */
PlannerQueryLevel = 1;
PlannerInitPlan = NULL;
PlannerParamVar = NULL;
PlannerPlanId = 0;
/* Initialize state for handling outer-level references and params */
PlannerQueryLevel = 0; /* will be 1 in top-level subquery_planner */
PlannerParamVar = NIL;
/* this should go away sometime soon */
transformKeySetQuery(parse);
/* primary planning entry point (may recurse for sublinks) */
/* primary planning entry point (may recurse for subqueries) */
result_plan = subquery_planner(parse, -1.0 /* default case */ );
Assert(PlannerQueryLevel == 1);
/* if top-level query had subqueries, do housekeeping for them */
if (PlannerPlanId > 0)
{
(void) SS_finalize_plan(result_plan);
result_plan->initPlan = PlannerInitPlan;
}
Assert(PlannerQueryLevel == 0);
/* executor wants to know total number of Params used overall */
result_plan->nParamExec = length(PlannerParamVar);
......@@ -106,9 +94,7 @@ planner(Query *parse)
/* restore state for outer planner, if any */
PlannerQueryLevel = save_PlannerQueryLevel;
PlannerInitPlan = save_PlannerInitPlan;
PlannerParamVar = save_PlannerParamVar;
PlannerPlanId = save_PlannerPlanId;
return result_plan;
}
......@@ -125,14 +111,9 @@ planner(Query *parse)
*
* Basically, this routine does the stuff that should only be done once
* per Query object. It then calls union_planner, which may be called
* recursively on the same Query node in order to handle UNIONs and/or
* inheritance. subquery_planner is called recursively from subselect.c
* to handle sub-Query nodes found within the query's expressions.
*
* prepunion.c uses an unholy combination of calling union_planner when
* recursing on the primary Query node, or subquery_planner when recursing
* on a UNION'd Query node that hasn't previously been seen by
* subquery_planner. That whole chunk of code needs rewritten from scratch.
* recursively on the same Query node in order to handle inheritance.
* subquery_planner will be called recursively to handle sub-Query nodes
* found within the query's expressions and rangetable.
*
* Returns a query plan.
*--------------------
......@@ -140,6 +121,20 @@ planner(Query *parse)
Plan *
subquery_planner(Query *parse, double tuple_fraction)
{
List *saved_initplan = PlannerInitPlan;
int saved_planid = PlannerPlanId;
Plan *plan;
List *lst;
/* Set up for a new level of subquery */
PlannerQueryLevel++;
PlannerInitPlan = NIL;
#ifdef ENABLE_KEY_SET_QUERY
/* this should go away sometime soon */
transformKeySetQuery(parse);
#endif
/*
* Check to see if any subqueries in the rangetable can be merged into
* this query.
......@@ -179,9 +174,9 @@ subquery_planner(Query *parse, double tuple_fraction)
EXPRKIND_HAVING);
/*
* Do the main planning (potentially recursive)
* Do the main planning (potentially recursive for inheritance)
*/
return union_planner(parse, tuple_fraction);
plan = union_planner(parse, tuple_fraction);
/*
* XXX should any more of union_planner's activity be moved here?
......@@ -190,6 +185,35 @@ subquery_planner(Query *parse, double tuple_fraction)
* but I suspect it would pay off in simplicity and avoidance of
* wasted cycles.
*/
/*
* If any subplans were generated, or if we're inside a subplan,
* build subPlan, extParam and locParam lists for plan nodes.
*/
if (PlannerPlanId != saved_planid || PlannerQueryLevel > 1)
{
(void) SS_finalize_plan(plan);
/*
* At the moment, SS_finalize_plan doesn't handle initPlans
* and so we assign them to the topmost plan node.
*/
plan->initPlan = PlannerInitPlan;
/* Must add the initPlans' extParams to the topmost node's, too */
foreach(lst, plan->initPlan)
{
SubPlan *subplan = (SubPlan *) lfirst(lst);
plan->extParam = set_unioni(plan->extParam,
subplan->plan->extParam);
}
}
/* Return to outer subquery context */
PlannerQueryLevel--;
PlannerInitPlan = saved_initplan;
/* we do NOT restore PlannerPlanId; that's not an oversight! */
return plan;
}
/*
......@@ -320,9 +344,10 @@ is_simple_subquery(Query *subquery)
if (subquery->limitOffset || subquery->limitCount)
elog(ERROR, "LIMIT is not supported in subselects");
/*
* Can't currently pull up a union query. Maybe after querytree redesign.
* Can't currently pull up a query with setops.
* Maybe after querytree redesign...
*/
if (subquery->unionClause)
if (subquery->setOperations)
return false;
/*
* Can't pull up a subquery involving grouping, aggregation, or sorting.
......@@ -573,7 +598,7 @@ preprocess_qual_conditions(Query *parse, Node *jtnode)
/*--------------------
* union_planner
* Invokes the planner on union-type queries (both regular UNIONs and
* Invokes the planner on union-type queries (both set operations and
* appends produced by inheritance), recursing if necessary to get them
* all, then processes normal plans.
*
......@@ -606,24 +631,31 @@ union_planner(Query *parse,
Index rt_index;
List *inheritors;
if (parse->unionClause)
if (parse->setOperations)
{
result_plan = plan_union_queries(parse);
/* XXX do we need to do this? bjm 12/19/97 */
tlist = preprocess_targetlist(tlist,
parse->commandType,
parse->resultRelation,
parse->rtable);
/*
* Construct the plan for set operations. The result will
* not need any work except perhaps a top-level sort.
*/
result_plan = plan_set_operations(parse);
/*
* We should not need to call preprocess_targetlist, since we must
* be in a SELECT query node.
*/
Assert(parse->commandType == CMD_SELECT);
/*
* We leave current_pathkeys NIL indicating we do not know sort
* order. This is correct for the appended-together subplan
* results, even if the subplans themselves produced sorted results.
* order. This is correct when the top set operation is UNION ALL,
* since the appended-together results are unsorted even if the
* subplans were sorted. For other set operations we could be
* smarter --- future improvement!
*/
/*
* Calculate pathkeys that represent grouping/ordering
* requirements
* requirements (grouping should always be null, but...)
*/
group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
tlist);
......@@ -886,7 +918,7 @@ union_planner(Query *parse,
tuple_fraction = 0.25;
}
/* Generate the (sub) plan */
/* Generate the basic plan for this Query */
result_plan = query_planner(parse,
sub_tlist,
tuple_fraction);
......@@ -1176,7 +1208,7 @@ make_groupplan(List *group_tlist,
* make_sortplan
* Add a Sort node to implement an explicit ORDER BY clause.
*/
static Plan *
Plan *
make_sortplan(List *tlist, Plan *plannode, List *sortcls)
{
List *sort_tlist;
......
......@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.66 2000/09/29 18:21:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.67 2000/10/05 19:11:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -103,9 +103,15 @@ set_plan_references(Plan *plan)
fix_expr_references(plan, (Node *) plan->qual);
break;
case T_SubqueryScan:
/*
* We do not do set_uppernode_references() here, because
* a SubqueryScan will always have been created with correct
* references to its subplan's outputs to begin with.
*/
fix_expr_references(plan, (Node *) plan->targetlist);
fix_expr_references(plan, (Node *) plan->qual);
/* No need to recurse into the subplan, it's fixed already */
/* Recurse into subplan too */
set_plan_references(((SubqueryScan *) plan)->subplan);
break;
case T_NestLoop:
set_join_references((Join *) plan);
......@@ -132,6 +138,7 @@ set_plan_references(Plan *plan)
case T_Material:
case T_Sort:
case T_Unique:
case T_SetOp:
case T_Hash:
/*
......@@ -170,6 +177,7 @@ set_plan_references(Plan *plan)
* Append, like Sort et al, doesn't actually evaluate its
* targetlist or quals, and we haven't bothered to give it
* its own tlist copy. So, don't fix targetlist/qual.
* But do recurse into subplans.
*/
foreach(pl, ((Append *) plan)->appendplans)
set_plan_references((Plan *) lfirst(pl));
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.42 2000/09/29 18:21:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.43 2000/10/05 19:11:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -29,7 +29,8 @@
Index PlannerQueryLevel; /* level of current query */
List *PlannerInitPlan; /* init subplans for current query */
List *PlannerParamVar; /* to get Var from Param->paramid */
int PlannerPlanId; /* to assign unique ID to subquery plans */
int PlannerPlanId = 0; /* to assign unique ID to subquery plans */
/*--------------------
* PlannerParamVar is a list of Var nodes, wherein the n'th entry
......@@ -81,7 +82,7 @@ replace_var(Var *var)
/*
* If there's already a PlannerParamVar entry for this same Var, just
* use it. NOTE: in situations involving UNION or inheritance, it is
* use it. NOTE: in sufficiently complex querytrees, it is
* possible for the same varno/varlevel to refer to different RTEs in
* different parts of the parsetree, so that different fields might
* end up sharing the same Param number. As long as we check the
......@@ -128,11 +129,6 @@ make_subplan(SubLink *slink)
Plan *plan;
List *lst;
Node *result;
List *saved_ip = PlannerInitPlan;
PlannerInitPlan = NULL;
PlannerQueryLevel++; /* we become child */
/*
* Check to see if this node was already processed; if so we have
......@@ -181,45 +177,30 @@ make_subplan(SubLink *slink)
else
tuple_fraction = -1.0; /* default behavior */
node->plan = plan = subquery_planner(subquery, tuple_fraction);
/*
* Assign subPlan, extParam and locParam to plan nodes. At the moment,
* SS_finalize_plan doesn't handle initPlan-s and so we assign them to
* the topmost plan node and take care about its extParam too.
* Generate the plan for the subquery.
*/
(void) SS_finalize_plan(plan);
plan->initPlan = PlannerInitPlan;
/* Create extParam list as union of InitPlan-s' lists */
foreach(lst, PlannerInitPlan)
{
List *lp;
foreach(lp, ((SubPlan *) lfirst(lst))->plan->extParam)
{
if (!intMember(lfirsti(lp), plan->extParam))
plan->extParam = lappendi(plan->extParam, lfirsti(lp));
}
}
node->plan = plan = subquery_planner(subquery, tuple_fraction);
/* and now we are parent again */
PlannerInitPlan = saved_ip;
PlannerQueryLevel--;
node->plan_id = PlannerPlanId++; /* Assign unique ID to this SubPlan */
node->plan_id = PlannerPlanId++;
node->rtable = subquery->rtable;
node->sublink = slink;
slink->subselect = NULL; /* cool ?! see error check above! */
/* make parParam list of params coming from current query level */
/*
* Make parParam list of params that current query level will pass
* to this child plan.
*/
foreach(lst, plan->extParam)
{
Var *var = nth(lfirsti(lst), PlannerParamVar);
int paramid = lfirsti(lst);
Var *var = nth(paramid, PlannerParamVar);
/* note varlevelsup is absolute level number */
if (var->varlevelsup == PlannerQueryLevel)
node->parParam = lappendi(node->parParam, lfirsti(lst));
node->parParam = lappendi(node->parParam, paramid);
}
/*
......@@ -625,6 +606,11 @@ SS_finalize_plan(Plan *plan)
SS_finalize_plan((Plan *) lfirst(lst)));
break;
case T_SubqueryScan:
results.paramids = set_unioni(results.paramids,
SS_finalize_plan(((SubqueryScan *) plan)->subplan));
break;
case T_IndexScan:
finalize_primnode((Node *) ((IndexScan *) plan)->indxqual,
&results);
......@@ -667,10 +653,10 @@ SS_finalize_plan(Plan *plan)
case T_Agg:
case T_SeqScan:
case T_SubqueryScan:
case T_Material:
case T_Sort:
case T_Unique:
case T_SetOp:
case T_Group:
break;
......@@ -689,17 +675,18 @@ SS_finalize_plan(Plan *plan)
foreach(lst, results.paramids)
{
Var *var = nth(lfirsti(lst), PlannerParamVar);
int paramid = lfirsti(lst);
Var *var = nth(paramid, PlannerParamVar);
/* note varlevelsup is absolute level number */
if (var->varlevelsup < PlannerQueryLevel)
extParam = lappendi(extParam, lfirsti(lst));
extParam = lappendi(extParam, paramid);
else if (var->varlevelsup > PlannerQueryLevel)
elog(ERROR, "SS_finalize_plan: plan shouldn't reference subplan's variable");
else
{
Assert(var->varno == 0 && var->varattno == 0);
locParam = lappendi(locParam, lfirsti(lst));
locParam = lappendi(locParam, paramid);
}
}
......
......@@ -20,6 +20,8 @@
bool _use_keyset_query_optimizer = FALSE;
#ifdef ENABLE_KEY_SET_QUERY
static int inspectOpNode(Expr *expr);
static int inspectAndNode(Expr *expr);
static int inspectOrNode(Expr *expr);
......@@ -213,3 +215,5 @@ inspectOpNode(Expr *expr)
secondExpr = lsecond(expr->args);
return (firstExpr && secondExpr && nodeTag(firstExpr) == T_Var && nodeTag(secondExpr) == T_Const);
}
#endif /* ENABLE_KEY_SET_QUERY */
......@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.38 2000/08/08 15:41:48 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.39 2000/10/05 19:11:30 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -49,6 +49,17 @@ preprocess_targetlist(List *tlist,
Index result_relation,
List *range_table)
{
/*
* Sanity check: if there is a result relation, it'd better be a
* real relation not a subquery. Else parser or rewriter messed up.
*/
if (result_relation)
{
RangeTblEntry *rte = rt_fetch(result_relation, range_table);
if (rte->subquery != NULL || rte->relid == InvalidOid)
elog(ERROR, "preprocess_targetlist: subquery cannot be result relation");
}
/*
* for heap_formtuple to work, the targetlist must match the exact
......
This diff is collapsed.
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.76 2000/09/29 18:21:23 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.77 2000/10/05 19:11:32 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
......@@ -1636,8 +1636,8 @@ simplify_op_or_func(Expr *expr, List *args)
* so that a scan of a target list can be handled without additional code.
* (But only the "expr" part of a TargetEntry is examined, unless the walker
* chooses to process TargetEntry nodes specially.) Also, RangeTblRef,
* FromExpr, and JoinExpr nodes are handled, so that qual expressions in a
* jointree can be processed without additional code.
* FromExpr, JoinExpr, and SetOperationStmt nodes are handled, so that query
* jointrees and setOperation trees can be processed without additional code.
*
* expression_tree_walker will handle SubLink and SubPlan nodes by recursing
* normally into the "lefthand" arguments (which belong to the outer plan).
......@@ -1654,7 +1654,8 @@ simplify_op_or_func(Expr *expr, List *args)
* if (IsA(node, Query))
* {
* adjust context for subquery;
* result = query_tree_walker((Query *) node, my_walker, context);
* result = query_tree_walker((Query *) node, my_walker, context,
* true); // to visit subquery RTEs too
* restore context if needed;
* return result;
* }
......@@ -1827,6 +1828,16 @@ expression_tree_walker(Node *node,
*/
}
break;
case T_SetOperationStmt:
{
SetOperationStmt *setop = (SetOperationStmt *) node;
if (walker(setop->larg, context))
return true;
if (walker(setop->rarg, context))
return true;
}
break;
default:
elog(ERROR, "expression_tree_walker: Unexpected node type %d",
nodeTag(node));
......@@ -1843,11 +1854,17 @@ expression_tree_walker(Node *node,
* for starting a walk at top level of a Query regardless of whether the
* walker intends to descend into subqueries. It is also useful for
* descending into subqueries within a walker.
*
* If visitQueryRTEs is true, the walker will also be called on sub-Query
* nodes present in subquery rangetable entries of the given Query. This
* is optional since some callers handle those sub-queries separately,
* or don't really want to see subqueries anyway.
*/
bool
query_tree_walker(Query *query,
bool (*walker) (),
void *context)
void *context,
bool visitQueryRTEs)
{
Assert(query != NULL && IsA(query, Query));
......@@ -1855,11 +1872,23 @@ query_tree_walker(Query *query,
return true;
if (walker((Node *) query->jointree, context))
return true;
if (walker(query->setOperations, context))
return true;
if (walker(query->havingQual, context))
return true;
/*
* XXX for subselect-in-FROM, may need to examine rtable as well?
*/
if (visitQueryRTEs)
{
List *rt;
foreach(rt, query->rtable)
{
RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
if (rte->subquery)
if (walker(rte->subquery, context))
return true;
}
}
return false;
}
......@@ -2158,6 +2187,17 @@ expression_tree_mutator(Node *node,
return (Node *) newnode;
}
break;
case T_SetOperationStmt:
{
SetOperationStmt *setop = (SetOperationStmt *) node;
SetOperationStmt *newnode;
FLATCOPY(newnode, setop, SetOperationStmt);
MUTATE(newnode->larg, setop->larg, Node *);
MUTATE(newnode->rarg, setop->rarg, Node *);
return (Node *) newnode;
}
break;
default:
elog(ERROR, "expression_tree_mutator: Unexpected node type %d",
nodeTag(node));
......@@ -2166,3 +2206,58 @@ expression_tree_mutator(Node *node,
/* can't get here, but keep compiler happy */
return NULL;
}
/*
* query_tree_mutator --- initiate modification of a Query's expressions
*
* This routine exists just to reduce the number of places that need to know
* where all the expression subtrees of a Query are. Note it can be used
* for starting a walk at top level of a Query regardless of whether the
* mutator intends to descend into subqueries. It is also useful for
* descending into subqueries within a mutator.
*
* The specified Query node is modified-in-place; do a FLATCOPY() beforehand
* if you don't want to change the original. All substructure is safely
* copied, however.
*
* If visitQueryRTEs is true, the mutator will also be called on sub-Query
* nodes present in subquery rangetable entries of the given Query. This
* is optional since some callers handle those sub-queries separately,
* or don't really want to see subqueries anyway.
*/
void
query_tree_mutator(Query *query,
Node *(*mutator) (),
void *context,
bool visitQueryRTEs)
{
Assert(query != NULL && IsA(query, Query));
MUTATE(query->targetList, query->targetList, List *);
MUTATE(query->jointree, query->jointree, FromExpr *);
MUTATE(query->setOperations, query->setOperations, Node *);
MUTATE(query->havingQual, query->havingQual, Node *);
if (visitQueryRTEs)
{
List *newrt = NIL;
List *rt;
foreach(rt, query->rtable)
{
RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
if (rte->subquery)
{
RangeTblEntry *newrte;
FLATCOPY(newrte, rte, RangeTblEntry);
CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
MUTATE(newrte->subquery, newrte->subquery, Query *);
rte = newrte;
}
newrt = lappend(newrt, rte);
}
query->rtable = newrt;
}
}
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.27 2000/09/12 21:06:59 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.28 2000/10/05 19:11:32 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -67,7 +67,7 @@ pull_varnos(Node *node)
*/
if (node && IsA(node, Query))
query_tree_walker((Query *) node, pull_varnos_walker,
(void *) &context);
(void *) &context, true);
else
pull_varnos_walker(node, &context);
......@@ -108,12 +108,12 @@ pull_varnos_walker(Node *node, pull_varnos_context *context)
}
if (IsA(node, Query))
{
/* Recurse into not-yet-planned subquery */
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
bool result;
context->sublevels_up++;
result = query_tree_walker((Query *) node, pull_varnos_walker,
(void *) context);
(void *) context, true);
context->sublevels_up--;
return result;
}
......
This diff is collapsed.
This diff is collapsed.
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.68 2000/09/29 18:21:36 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.69 2000/10/05 19:11:33 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -347,7 +347,8 @@ transformTableEntry(ParseState *pstate, RangeVar *r)
static RangeTblRef *
transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
{
SelectStmt *subquery = (SelectStmt *) r->subquery;
List *save_rtable;
List *save_joinlist;
List *parsetrees;
Query *query;
RangeTblEntry *rte;
......@@ -362,19 +363,21 @@ transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
elog(ERROR, "sub-select in FROM must have an alias");
/*
* subquery node might not be SelectStmt if user wrote something like
* FROM (SELECT ... UNION SELECT ...). Our current implementation of
* UNION/INTERSECT/EXCEPT is too messy to deal with here, so punt until
* we redesign querytrees to make it more reasonable.
* Analyze and transform the subquery. This is a bit tricky because
* we don't want the subquery to be able to see any FROM items already
* created in the current query (per SQL92, the scope of a FROM item
* does not include other FROM items). But it does need to be able to
* see any further-up parent states, so we can't just pass a null parent
* pstate link. So, temporarily make the current query level have an
* empty rtable and joinlist.
*/
if (subquery == NULL || !IsA(subquery, SelectStmt))
elog(ERROR, "Set operations not yet supported in subselects in FROM");
/*
* Analyze and transform the subquery as if it were an independent
* statement (we do NOT want it to see the outer query as a parent).
*/
parsetrees = parse_analyze(makeList1(subquery), NULL);
save_rtable = pstate->p_rtable;
save_joinlist = pstate->p_joinlist;
pstate->p_rtable = NIL;
pstate->p_joinlist = NIL;
parsetrees = parse_analyze(makeList1(r->subquery), pstate);
pstate->p_rtable = save_rtable;
pstate->p_joinlist = save_joinlist;
/*
* Check that we got something reasonable. Some of these conditions
......@@ -1181,108 +1184,3 @@ exprIsInSortList(Node *expr, List *sortList, List *targetList)
}
return false;
}
/* transformUnionClause()
* Transform a UNION clause.
* Note that the union clause is actually a fully-formed select structure.
* So, it is evaluated as a select, then the resulting target fields
* are matched up to ensure correct types in the results.
* The select clause parsing is done recursively, so the unions are evaluated
* right-to-left. One might want to look at all columns from all clauses before
* trying to coerce, but unless we keep track of the call depth we won't know
* when to do this because of the recursion.
* Let's just try matching in pairs for now (right to left) and see if it works.
* - thomas 1998-05-22
*/
#ifdef NOT_USED
static List *
transformUnionClause(List *unionClause, List *targetlist)
{
List *union_list = NIL;
List *qlist,
*qlist_item;
if (unionClause)
{
/* recursion */
qlist = parse_analyze(unionClause, NULL);
foreach(qlist_item, qlist)
{
Query *query = (Query *) lfirst(qlist_item);
List *prev_target = targetlist;
List *next_target;
int prev_len = 0,
next_len = 0;
foreach(prev_target, targetlist)
if (!((TargetEntry *) lfirst(prev_target))->resdom->resjunk)
prev_len++;
foreach(next_target, query->targetList)
if (!((TargetEntry *) lfirst(next_target))->resdom->resjunk)
next_len++;
if (prev_len != next_len)
elog(ERROR, "Each UNION clause must have the same number of columns");
foreach(next_target, query->targetList)
{
Oid itype;
Oid otype;
otype = ((TargetEntry *) lfirst(prev_target))->resdom->restype;
itype = ((TargetEntry *) lfirst(next_target))->resdom->restype;
/* one or both is a NULL column? then don't convert... */
if (otype == InvalidOid)
{
/* propagate a known type forward, if available */
if (itype != InvalidOid)
((TargetEntry *) lfirst(prev_target))->resdom->restype = itype;
#if FALSE
else
{
((TargetEntry *) lfirst(prev_target))->resdom->restype = UNKNOWNOID;
((TargetEntry *) lfirst(next_target))->resdom->restype = UNKNOWNOID;
}
#endif
}
else if (itype == InvalidOid)
{
}
/* they don't match in type? then convert... */
else if (itype != otype)
{
Node *expr;
expr = ((TargetEntry *) lfirst(next_target))->expr;
expr = CoerceTargetExpr(NULL, expr, itype, otype, -1);
if (expr == NULL)
{
elog(ERROR, "Unable to transform %s to %s"
"\n\tEach UNION clause must have compatible target types",
typeidTypeName(itype),
typeidTypeName(otype));
}
((TargetEntry *) lfirst(next_target))->expr = expr;
((TargetEntry *) lfirst(next_target))->resdom->restype = otype;
}
/* both are UNKNOWN? then evaluate as text... */
else if (itype == UNKNOWNOID)
{
((TargetEntry *) lfirst(next_target))->resdom->restype = TEXTOID;
((TargetEntry *) lfirst(prev_target))->resdom->restype = TEXTOID;
}
prev_target = lnext(prev_target);
}
union_list = lappend(union_list, query);
}
return union_list;
}
else
return NIL;
}
#endif
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.46 2000/07/30 22:13:50 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.47 2000/10/05 19:11:33 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -314,6 +314,100 @@ coerce_type_typmod(ParseState *pstate, Node *node,
}
/* select_common_type()
* Determine the common supertype of a list of input expression types.
* This is used for determining the output type of CASE and UNION
* constructs.
*
* typeids is a nonempty integer list of type OIDs. Note that earlier items
* in the list will be preferred if there is doubt.
* 'context' is a phrase to use in the error message if we fail to select
* a usable type.
*
* XXX this code is WRONG, since (for example) given the input (int4,int8)
* it will select int4, whereas according to SQL92 clause 9.3 the correct
* answer is clearly int8. To fix this we need a notion of a promotion
* hierarchy within type categories --- something more complete than
* just a single preferred type.
*/
Oid
select_common_type(List *typeids, const char *context)
{
Oid ptype;
CATEGORY pcategory;
List *l;
Assert(typeids != NIL);
ptype = (Oid) lfirsti(typeids);
pcategory = TypeCategory(ptype);
foreach(l, lnext(typeids))
{
Oid ntype = (Oid) lfirsti(l);
/* move on to next one if no new information... */
if (ntype && (ntype != UNKNOWNOID) && (ntype != ptype))
{
if (!ptype || ptype == UNKNOWNOID)
{
/* so far, only nulls so take anything... */
ptype = ntype;
pcategory = TypeCategory(ptype);
}
else if (TypeCategory(ntype) != pcategory)
{
/*
* both types in different categories? then
* not much hope...
*/
elog(ERROR, "%s types \"%s\" and \"%s\" not matched",
context, typeidTypeName(ptype), typeidTypeName(ntype));
}
else if (IsPreferredType(pcategory, ntype)
&& can_coerce_type(1, &ptype, &ntype))
{
/*
* new one is preferred and can convert? then
* take it...
*/
ptype = ntype;
pcategory = TypeCategory(ptype);
}
}
}
return ptype;
}
/* coerce_to_common_type()
* Coerce an expression to the given type.
*
* This is used following select_common_type() to coerce the individual
* expressions to the desired type. 'context' is a phrase to use in the
* error message if we fail to coerce.
*
* NOTE: pstate may be NULL.
*/
Node *
coerce_to_common_type(ParseState *pstate, Node *node,
Oid targetTypeId,
const char *context)
{
Oid inputTypeId = exprType(node);
if (inputTypeId == targetTypeId)
return node; /* no work */
if (can_coerce_type(1, &inputTypeId, &targetTypeId))
{
node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1);
}
else
{
elog(ERROR, "%s unable to convert to type \"%s\"",
context, typeidTypeName(targetTypeId));
}
return node;
}
/* TypeCategory()
* Assign a category to the specified OID.
*/
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.84 2000/09/29 18:21:36 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.85 2000/10/05 19:11:33 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -412,9 +412,9 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
{
CaseExpr *c = (CaseExpr *) expr;
CaseWhen *w;
List *typeids = NIL;
List *args;
Oid ptype;
CATEGORY pcategory;
/* transform the list of arguments */
foreach(args, c->args)
......@@ -432,6 +432,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
w->expr = (Node *) a;
}
lfirst(args) = transformExpr(pstate, (Node *) w, precedence);
typeids = lappendi(typeids, exprType(w->result));
}
/*
......@@ -452,104 +453,26 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
c->defresult = (Node *) n;
}
c->defresult = transformExpr(pstate, c->defresult, precedence);
/* now check types across result clauses... */
c->casetype = exprType(c->defresult);
ptype = c->casetype;
pcategory = TypeCategory(ptype);
foreach(args, c->args)
{
Oid wtype;
w = lfirst(args);
wtype = exprType(w->result);
/* move on to next one if no new information... */
if (wtype && (wtype != UNKNOWNOID)
&& (wtype != ptype))
{
if (!ptype || ptype == UNKNOWNOID)
{
/* so far, only nulls so take anything... */
ptype = wtype;
pcategory = TypeCategory(ptype);
}
else if ((TypeCategory(wtype) != pcategory)
|| ((TypeCategory(wtype) == USER_TYPE)
&& (TypeCategory(c->casetype) == USER_TYPE)))
{
/*
* both types in different categories? then
* not much hope...
* Note: default result is considered the most significant
* type in determining preferred type. This is how the code
* worked before, but it seems a little bogus to me --- tgl
*/
elog(ERROR, "CASE/WHEN types '%s' and '%s' not matched",
typeidTypeName(c->casetype), typeidTypeName(wtype));
}
else if (IsPreferredType(pcategory, wtype)
&& can_coerce_type(1, &ptype, &wtype))
{
typeids = lconsi(exprType(c->defresult), typeids);
/*
* new one is preferred and can convert? then
* take it...
*/
ptype = wtype;
pcategory = TypeCategory(ptype);
}
}
}
ptype = select_common_type(typeids, "CASE");
c->casetype = ptype;
/* Convert default result clause, if necessary */
if (c->casetype != ptype)
{
if (!c->casetype || c->casetype == UNKNOWNOID)
{
c->defresult = coerce_to_common_type(pstate, c->defresult,
ptype, "CASE/ELSE");
/*
* default clause is NULL, so assign preferred
* type from WHEN clauses...
*/
c->casetype = ptype;
}
else if (can_coerce_type(1, &c->casetype, &ptype))
{
c->defresult = coerce_type(pstate, c->defresult,
c->casetype, ptype, -1);
c->casetype = ptype;
}
else
{
elog(ERROR, "CASE/ELSE unable to convert to type '%s'",
typeidTypeName(ptype));
}
}
/* Convert when clauses, if not null and if necessary */
/* Convert when-clause results, if necessary */
foreach(args, c->args)
{
Oid wtype;
w = lfirst(args);
wtype = exprType(w->result);
/*
* only bother with conversion if not NULL and
* different type...
*/
if (wtype && (wtype != UNKNOWNOID)
&& (wtype != ptype))
{
if (can_coerce_type(1, &wtype, &ptype))
{
w->result = coerce_type(pstate, w->result, wtype,
ptype, -1);
}
else
{
elog(ERROR, "CASE/WHEN unable to convert to type '%s'",
typeidTypeName(ptype));
}
}
w->result = coerce_to_common_type(pstate, w->result,
ptype, "CASE/WHEN");
}
result = expr;
......@@ -560,7 +483,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
{
CaseWhen *w = (CaseWhen *) expr;
w->expr = transformExpr(pstate, (Node *) w->expr, precedence);
w->expr = transformExpr(pstate, w->expr, precedence);
if (exprType(w->expr) != BOOLOID)
elog(ERROR, "WHEN clause must have a boolean result");
......@@ -575,7 +498,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
n->val.type = T_Null;
w->result = (Node *) n;
}
w->result = transformExpr(pstate, (Node *) w->result, precedence);
w->result = transformExpr(pstate, w->result, precedence);
result = expr;
break;
}
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.53 2000/09/29 18:21:24 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.54 2000/10/05 19:11:34 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -449,7 +449,8 @@ setRuleCheckAsUser(Query *qry, Oid userid)
/* If there are sublinks, search for them and process their RTEs */
if (qry->hasSubLinks)
query_tree_walker(qry, setRuleCheckAsUser_walker, (void *) &userid);
query_tree_walker(qry, setRuleCheckAsUser_walker, (void *) &userid,
false /* already did the ones in rtable */);
}
/*
......
This diff is collapsed.
......@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.49 2000/09/29 18:21:24 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.50 2000/10/05 19:11:34 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -48,7 +48,7 @@ checkExprHasAggs(Node *node)
*/
if (node && IsA(node, Query))
return query_tree_walker((Query *) node, checkExprHasAggs_walker,
NULL);
NULL, false);
else
return checkExprHasAggs_walker(node, NULL);
}
......@@ -78,7 +78,7 @@ checkExprHasSubLink(Node *node)
*/
if (node && IsA(node, Query))
return query_tree_walker((Query *) node, checkExprHasSubLink_walker,
NULL);
NULL, false);
else
return checkExprHasSubLink_walker(node, NULL);
}
......@@ -101,7 +101,7 @@ checkExprHasSubLink_walker(Node *node, void *context)
* Find all Var nodes in the given tree with varlevelsup == sublevels_up,
* and increment their varno fields (rangetable indexes) by 'offset'.
* The varnoold fields are adjusted similarly. Also, RangeTblRef nodes
* in join trees are adjusted.
* in join trees and setOp trees are adjusted.
*
* NOTE: although this has the form of a walker, we cheat and modify the
* nodes in-place. The given expression tree should have been copied
......@@ -136,6 +136,7 @@ OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
if (context->sublevels_up == 0)
rtr->rtindex += context->offset;
/* the subquery itself is visited separately */
return false;
}
if (IsA(node, Query))
......@@ -145,7 +146,7 @@ OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
context->sublevels_up++;
result = query_tree_walker((Query *) node, OffsetVarNodes_walker,
(void *) context);
(void *) context, true);
context->sublevels_up--;
return result;
}
......@@ -168,7 +169,7 @@ OffsetVarNodes(Node *node, int offset, int sublevels_up)
*/
if (node && IsA(node, Query))
query_tree_walker((Query *) node, OffsetVarNodes_walker,
(void *) &context);
(void *) &context, true);
else
OffsetVarNodes_walker(node, &context);
}
......@@ -179,7 +180,7 @@ OffsetVarNodes(Node *node, int offset, int sublevels_up)
* Find all Var nodes in the given tree belonging to a specific relation
* (identified by sublevels_up and rt_index), and change their varno fields
* to 'new_index'. The varnoold fields are changed too. Also, RangeTblRef
* nodes in join trees are adjusted.
* nodes in join trees and setOp trees are adjusted.
*
* NOTE: although this has the form of a walker, we cheat and modify the
* nodes in-place. The given expression tree should have been copied
......@@ -217,6 +218,7 @@ ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
if (context->sublevels_up == 0 &&
rtr->rtindex == context->rt_index)
rtr->rtindex = context->new_index;
/* the subquery itself is visited separately */
return false;
}
if (IsA(node, Query))
......@@ -226,7 +228,7 @@ ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
context->sublevels_up++;
result = query_tree_walker((Query *) node, ChangeVarNodes_walker,
(void *) context);
(void *) context, true);
context->sublevels_up--;
return result;
}
......@@ -250,7 +252,7 @@ ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
*/
if (node && IsA(node, Query))
query_tree_walker((Query *) node, ChangeVarNodes_walker,
(void *) &context);
(void *) &context, true);
else
ChangeVarNodes_walker(node, &context);
}
......@@ -300,7 +302,7 @@ IncrementVarSublevelsUp_walker(Node *node,
context->min_sublevels_up++;
result = query_tree_walker((Query *) node,
IncrementVarSublevelsUp_walker,
(void *) context);
(void *) context, true);
context->min_sublevels_up--;
return result;
}
......@@ -324,7 +326,7 @@ IncrementVarSublevelsUp(Node *node, int delta_sublevels_up,
*/
if (node && IsA(node, Query))
query_tree_walker((Query *) node, IncrementVarSublevelsUp_walker,
(void *) &context);
(void *) &context, true);
else
IncrementVarSublevelsUp_walker(node, &context);
}
......@@ -332,7 +334,7 @@ IncrementVarSublevelsUp(Node *node, int delta_sublevels_up,
/*
* rangeTableEntry_used - detect whether an RTE is referenced somewhere
* in var nodes or jointree nodes of a query or expression.
* in var nodes or join or setOp trees of a query or expression.
*/
typedef struct
......@@ -363,6 +365,7 @@ rangeTableEntry_used_walker(Node *node,
if (rtr->rtindex == context->rt_index &&
context->sublevels_up == 0)
return true;
/* the subquery itself is visited separately */
return false;
}
if (IsA(node, Query))
......@@ -372,7 +375,7 @@ rangeTableEntry_used_walker(Node *node,
context->sublevels_up++;
result = query_tree_walker((Query *) node, rangeTableEntry_used_walker,
(void *) context);
(void *) context, true);
context->sublevels_up--;
return result;
}
......@@ -395,7 +398,7 @@ rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
*/
if (node && IsA(node, Query))
return query_tree_walker((Query *) node, rangeTableEntry_used_walker,
(void *) &context);
(void *) &context, true);
else
return rangeTableEntry_used_walker(node, &context);
}
......@@ -437,7 +440,7 @@ attribute_used_walker(Node *node,
context->sublevels_up++;
result = query_tree_walker((Query *) node, attribute_used_walker,
(void *) context);
(void *) context, true);
context->sublevels_up--;
return result;
}
......@@ -461,7 +464,7 @@ attribute_used(Node *node, int rt_index, int attno, int sublevels_up)
*/
if (node && IsA(node, Query))
return query_tree_walker((Query *) node, attribute_used_walker,
(void *) &context);
(void *) &context, true);
else
return attribute_used_walker(node, &context);
}
......@@ -681,10 +684,8 @@ ResolveNew_mutator(Node *node, ResolveNew_context *context)
FLATCOPY(newnode, sublink, SubLink);
MUTATE(newnode->lefthand, sublink->lefthand, List *,
ResolveNew_mutator, context);
context->sublevels_up++;
MUTATE(newnode->subselect, sublink->subselect, Node *,
ResolveNew_mutator, context);
context->sublevels_up--;
return (Node *) newnode;
}
if (IsA(node, Query))
......@@ -693,12 +694,9 @@ ResolveNew_mutator(Node *node, ResolveNew_context *context)
Query *newnode;
FLATCOPY(newnode, query, Query);
MUTATE(newnode->targetList, query->targetList, List *,
ResolveNew_mutator, context);
MUTATE(newnode->jointree, query->jointree, FromExpr *,
ResolveNew_mutator, context);
MUTATE(newnode->havingQual, query->havingQual, Node *,
ResolveNew_mutator, context);
context->sublevels_up++;
query_tree_mutator(newnode, ResolveNew_mutator, context, true);
context->sublevels_up--;
return (Node *) newnode;
}
return expression_tree_mutator(node, ResolveNew_mutator,
......@@ -718,10 +716,21 @@ ResolveNew(Node *node, int target_varno, int sublevels_up,
context.update_varno = update_varno;
/*
* Note: if an entire Query is passed, the right things will happen,
* because ResolveNew_mutator increments sublevels_up when it sees
* a SubLink, not a Query.
* Must be prepared to start with a Query or a bare expression tree;
* if it's a Query, go straight to query_tree_mutator to make sure that
* sublevels_up doesn't get incremented prematurely.
*/
if (node && IsA(node, Query))
{
Query *query = (Query *) node;
Query *newnode;
FLATCOPY(newnode, query, Query);
query_tree_mutator(newnode, ResolveNew_mutator,
(void *) &context, true);
return (Node *) newnode;
}
else
return ResolveNew_mutator(node, &context);
}
......@@ -740,12 +749,8 @@ FixNew(RewriteInfo *info, Query *parsetree)
context.event = info->event;
context.update_varno = info->current_varno;
info->rule_action->targetList = (List *)
ResolveNew_mutator((Node *) info->rule_action->targetList, &context);
info->rule_action->jointree = (FromExpr *)
ResolveNew_mutator((Node *) info->rule_action->jointree, &context);
info->rule_action->havingQual =
ResolveNew_mutator(info->rule_action->havingQual, &context);
query_tree_mutator(info->rule_action, ResolveNew_mutator,
(void *) &context, true);
}
......@@ -837,10 +842,8 @@ HandleRIRAttributeRule_mutator(Node *node,
FLATCOPY(newnode, sublink, SubLink);
MUTATE(newnode->lefthand, sublink->lefthand, List *,
HandleRIRAttributeRule_mutator, context);
context->sublevels_up++;
MUTATE(newnode->subselect, sublink->subselect, Node *,
HandleRIRAttributeRule_mutator, context);
context->sublevels_up--;
return (Node *) newnode;
}
if (IsA(node, Query))
......@@ -849,14 +852,10 @@ HandleRIRAttributeRule_mutator(Node *node,
Query *newnode;
FLATCOPY(newnode, query, Query);
MUTATE(newnode->targetList, query->targetList, List *,
HandleRIRAttributeRule_mutator, context);
MUTATE(newnode->jointree, query->jointree, FromExpr *,
HandleRIRAttributeRule_mutator, context);
MUTATE(newnode->havingQual, query->havingQual, Node *,
HandleRIRAttributeRule_mutator, context);
context->sublevels_up++;
query_tree_mutator(newnode, HandleRIRAttributeRule_mutator,
context, true);
context->sublevels_up--;
return (Node *) newnode;
}
return expression_tree_mutator(node, HandleRIRAttributeRule_mutator,
......@@ -882,15 +881,8 @@ HandleRIRAttributeRule(Query *parsetree,
context.badsql = badsql;
context.sublevels_up = 0;
parsetree->targetList = (List *)
HandleRIRAttributeRule_mutator((Node *) parsetree->targetList,
&context);
parsetree->jointree = (FromExpr *)
HandleRIRAttributeRule_mutator((Node *) parsetree->jointree,
&context);
parsetree->havingQual =
HandleRIRAttributeRule_mutator(parsetree->havingQual,
&context);
query_tree_mutator(parsetree, HandleRIRAttributeRule_mutator,
(void *) &context, true);
}
#endif /* NOT_USED */
This diff is collapsed.
......@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: catversion.h,v 1.48 2000/09/29 18:21:37 tgl Exp $
* $Id: catversion.h,v 1.49 2000/10/05 19:11:35 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 200009281
#define CATALOG_VERSION_NO 200010041
#endif
/*-------------------------------------------------------------------------
*
* nodeSetOp.h
*
*
*
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: nodeSetOp.h,v 1.1 2000/10/05 19:11:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef NODESETOP_H
#define NODESETOP_H
#include "nodes/plannodes.h"
extern TupleTableSlot *ExecSetOp(SetOp *node);
extern bool ExecInitSetOp(SetOp *node, EState *estate, Plan *parent);
extern int ExecCountSlotsSetOp(SetOp *node);
extern void ExecEndSetOp(SetOp *node);
extern void ExecReScanSetOp(SetOp *node, ExprContext *exprCtxt, Plan *parent);
#endif /* NODESETOP_H */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: planner.h,v 1.16 2000/08/21 20:55:28 tgl Exp $
* $Id: planner.h,v 1.17 2000/10/05 19:11:37 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -22,4 +22,6 @@ extern Plan *planner(Query *parse);
extern Plan *subquery_planner(Query *parse, double tuple_fraction);
extern Plan *union_planner(Query *parse, double tuple_fraction);
extern Plan *make_sortplan(List *tlist, Plan *plannode, List *sortcls);
#endif /* PLANNER_H */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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