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 @@ ...@@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994-5, Regents of the University of California * 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) ...@@ -197,6 +197,26 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
case T_Unique: case T_Unique:
pname = "Unique"; pname = "Unique";
break; 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: case T_Hash:
pname = "Hash"; pname = "Hash";
break; break;
...@@ -320,8 +340,6 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es) ...@@ -320,8 +340,6 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
Assert(rtentry != NULL); Assert(rtentry != NULL);
rt_store(appendplan->inheritrelid, es->rtable, rtentry); rt_store(appendplan->inheritrelid, es->rtable, rtentry);
} }
else
es->rtable = nth(whichplan, appendplan->unionrtables);
for (i = 0; i < indent; i++) for (i = 0; i < indent; i++)
appendStringInfo(str, " "); appendStringInfo(str, " ");
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# Makefile for executor # Makefile for executor
# #
# IDENTIFICATION # 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 \ ...@@ -16,7 +16,7 @@ OBJS = execAmi.o execFlatten.o execJunk.o execMain.o \
execProcnode.o execQual.o execScan.o execTuples.o \ execProcnode.o execQual.o execScan.o execTuples.o \
execUtils.o functions.o nodeAppend.o nodeAgg.o nodeHash.o \ execUtils.o functions.o nodeAppend.o nodeAgg.o nodeHash.o \
nodeHashjoin.o nodeIndexscan.o nodeMaterial.o nodeMergejoin.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 \ nodeUnique.o nodeGroup.o spi.o nodeSubplan.o \
nodeSubqueryscan.o nodeTidscan.o nodeSubqueryscan.o nodeTidscan.o
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * 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 @@ ...@@ -42,6 +42,7 @@
#include "executor/nodeNestloop.h" #include "executor/nodeNestloop.h"
#include "executor/nodeResult.h" #include "executor/nodeResult.h"
#include "executor/nodeSeqscan.h" #include "executor/nodeSeqscan.h"
#include "executor/nodeSetOp.h"
#include "executor/nodeSort.h" #include "executor/nodeSort.h"
#include "executor/nodeSubplan.h" #include "executor/nodeSubplan.h"
#include "executor/nodeSubqueryscan.h" #include "executor/nodeSubqueryscan.h"
...@@ -345,6 +346,10 @@ ExecReScan(Plan *node, ExprContext *exprCtxt, Plan *parent) ...@@ -345,6 +346,10 @@ ExecReScan(Plan *node, ExprContext *exprCtxt, Plan *parent)
ExecReScanUnique((Unique *) node, exprCtxt, parent); ExecReScanUnique((Unique *) node, exprCtxt, parent);
break; break;
case T_SetOp:
ExecReScanSetOp((SetOp *) node, exprCtxt, parent);
break;
case T_Sort: case T_Sort:
ExecReScanSort((Sort *) node, exprCtxt, parent); ExecReScanSort((Sort *) node, exprCtxt, parent);
break; break;
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -463,7 +463,6 @@ ExecCheckPlanPerms(Plan *plan, List *rangeTable, CmdType operation)
/* Append implements expansion of inheritance */ /* Append implements expansion of inheritance */
ExecCheckRTPerms(app->inheritrtable, operation); ExecCheckRTPerms(app->inheritrtable, operation);
/* Check appended plans w/outer rangetable */
foreach(appendplans, app->appendplans) foreach(appendplans, app->appendplans)
{ {
ExecCheckPlanPerms((Plan *) lfirst(appendplans), ExecCheckPlanPerms((Plan *) lfirst(appendplans),
...@@ -474,15 +473,11 @@ ExecCheckPlanPerms(Plan *plan, List *rangeTable, CmdType operation) ...@@ -474,15 +473,11 @@ ExecCheckPlanPerms(Plan *plan, List *rangeTable, CmdType operation)
else else
{ {
/* Append implements UNION, which must be a SELECT */ /* Append implements UNION, which must be a SELECT */
List *rtables = app->unionrtables;
/* Check appended plans with their rangetables */
foreach(appendplans, app->appendplans) foreach(appendplans, app->appendplans)
{ {
ExecCheckPlanPerms((Plan *) lfirst(appendplans), ExecCheckPlanPerms((Plan *) lfirst(appendplans),
(List *) lfirst(rtables), rangeTable,
CMD_SELECT); CMD_SELECT);
rtables = lnext(rtables);
} }
} }
break; break;
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* *
* *
* IDENTIFICATION * 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 @@ ...@@ -88,6 +88,7 @@
#include "executor/nodeNestloop.h" #include "executor/nodeNestloop.h"
#include "executor/nodeResult.h" #include "executor/nodeResult.h"
#include "executor/nodeSeqscan.h" #include "executor/nodeSeqscan.h"
#include "executor/nodeSetOp.h"
#include "executor/nodeSort.h" #include "executor/nodeSort.h"
#include "executor/nodeSubplan.h" #include "executor/nodeSubplan.h"
#include "executor/nodeSubqueryscan.h" #include "executor/nodeSubqueryscan.h"
...@@ -199,6 +200,10 @@ ExecInitNode(Plan *node, EState *estate, Plan *parent) ...@@ -199,6 +200,10 @@ ExecInitNode(Plan *node, EState *estate, Plan *parent)
result = ExecInitUnique((Unique *) node, estate, parent); result = ExecInitUnique((Unique *) node, estate, parent);
break; break;
case T_SetOp:
result = ExecInitSetOp((SetOp *) node, estate, parent);
break;
case T_Group: case T_Group:
result = ExecInitGroup((Group *) node, estate, parent); result = ExecInitGroup((Group *) node, estate, parent);
break; break;
...@@ -322,6 +327,10 @@ ExecProcNode(Plan *node, Plan *parent) ...@@ -322,6 +327,10 @@ ExecProcNode(Plan *node, Plan *parent)
result = ExecUnique((Unique *) node); result = ExecUnique((Unique *) node);
break; break;
case T_SetOp:
result = ExecSetOp((SetOp *) node);
break;
case T_Group: case T_Group:
result = ExecGroup((Group *) node); result = ExecGroup((Group *) node);
break; break;
...@@ -401,6 +410,9 @@ ExecCountSlotsNode(Plan *node) ...@@ -401,6 +410,9 @@ ExecCountSlotsNode(Plan *node)
case T_Unique: case T_Unique:
return ExecCountSlotsUnique((Unique *) node); return ExecCountSlotsUnique((Unique *) node);
case T_SetOp:
return ExecCountSlotsSetOp((SetOp *) node);
case T_Group: case T_Group:
return ExecCountSlotsGroup((Group *) node); return ExecCountSlotsGroup((Group *) node);
...@@ -519,6 +531,10 @@ ExecEndNode(Plan *node, Plan *parent) ...@@ -519,6 +531,10 @@ ExecEndNode(Plan *node, Plan *parent)
ExecEndUnique((Unique *) node); ExecEndUnique((Unique *) node);
break; break;
case T_SetOp:
ExecEndSetOp((SetOp *) node);
break;
case T_Group: case T_Group:
ExecEndGroup((Group *) node); ExecEndGroup((Group *) node);
break; break;
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -762,6 +762,14 @@ NodeGetResultTupleSlot(Plan *node)
} }
break; break;
case T_SetOp:
{
SetOpState *setopstate = ((SetOp *) node)->setopstate;
slot = setopstate->cstate.cs_ResultTupleSlot;
}
break;
case T_MergeJoin: case T_MergeJoin:
{ {
MergeJoinState *mergestate = ((MergeJoin *) node)->mergestate; MergeJoinState *mergestate = ((MergeJoin *) node)->mergestate;
...@@ -783,8 +791,8 @@ NodeGetResultTupleSlot(Plan *node) ...@@ -783,8 +791,8 @@ NodeGetResultTupleSlot(Plan *node)
* should never get here * should never get here
* ---------------- * ----------------
*/ */
elog(ERROR, "NodeGetResultTupleSlot: node not yet supported: %d ", elog(ERROR, "NodeGetResultTupleSlot: node not yet supported: %d",
nodeTag(node)); (int) nodeTag(node));
return NULL; return NULL;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -80,11 +80,9 @@ exec_append_initialize_next(Append *node)
AppendState *appendstate; AppendState *appendstate;
TupleTableSlot *result_slot; TupleTableSlot *result_slot;
List *rangeTable; List *rangeTable;
int whichplan; int whichplan;
int nplans; int nplans;
List *rtables; List *inheritrtable;
List *rtable;
RangeTblEntry *rtentry; RangeTblEntry *rtentry;
/* ---------------- /* ----------------
...@@ -98,8 +96,7 @@ exec_append_initialize_next(Append *node) ...@@ -98,8 +96,7 @@ exec_append_initialize_next(Append *node)
whichplan = appendstate->as_whichplan; whichplan = appendstate->as_whichplan;
nplans = appendstate->as_nplans; nplans = appendstate->as_nplans;
rtables = node->unionrtables; inheritrtable = node->inheritrtable;
rtable = node->inheritrtable;
if (whichplan < 0) if (whichplan < 0)
{ {
...@@ -131,19 +128,17 @@ exec_append_initialize_next(Append *node) ...@@ -131,19 +128,17 @@ exec_append_initialize_next(Append *node)
/* ---------------- /* ----------------
* initialize the scan * initialize the scan
* (and update the range table appropriately) * (and update the range table appropriately)
* (doesn't this leave the range table hosed for anybody upstream *
* of the Append node??? - jolly ) * (doesn't this leave the range table hosed for anybody upstream
* of the Append node??? - jolly )
* ---------------- * ----------------
*/ */
if (node->inheritrelid > 0) if (node->inheritrelid > 0)
{ {
rtentry = nth(whichplan, rtable); rtentry = nth(whichplan, inheritrtable);
Assert(rtentry != NULL); Assert(rtentry != NULL);
rt_store(node->inheritrelid, rangeTable, rtentry); rt_store(node->inheritrelid, rangeTable, rtentry);
} }
else
estate->es_range_table = nth(whichplan, rtables);
if (appendstate->as_junkFilter_list) if (appendstate->as_junkFilter_list)
{ {
...@@ -181,7 +176,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent) ...@@ -181,7 +176,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
{ {
AppendState *appendstate; AppendState *appendstate;
int nplans; int nplans;
List *rtable; List *inheritrtable;
List *appendplans; List *appendplans;
bool *initialized; bool *initialized;
int i; int i;
...@@ -201,7 +196,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent) ...@@ -201,7 +196,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
appendplans = node->appendplans; appendplans = node->appendplans;
nplans = length(appendplans); nplans = length(appendplans);
rtable = node->inheritrtable; inheritrtable = node->inheritrtable;
initialized = (bool *) palloc(nplans * sizeof(bool)); initialized = (bool *) palloc(nplans * sizeof(bool));
MemSet(initialized, 0, nplans * sizeof(bool)); MemSet(initialized, 0, nplans * sizeof(bool));
...@@ -214,7 +209,6 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent) ...@@ -214,7 +209,6 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
appendstate->as_whichplan = 0; appendstate->as_whichplan = 0;
appendstate->as_nplans = nplans; appendstate->as_nplans = nplans;
appendstate->as_initialized = initialized; appendstate->as_initialized = initialized;
appendstate->as_rtentries = rtable;
node->appendstate = appendstate; node->appendstate = appendstate;
...@@ -250,7 +244,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent) ...@@ -250,7 +244,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
inherited_result_rel = true; inherited_result_rel = true;
foreach(rtentryP, rtable) foreach(rtentryP, inheritrtable)
{ {
RangeTblEntry *rtentry = lfirst(rtentryP); RangeTblEntry *rtentry = lfirst(rtentryP);
Oid reloid = rtentry->relid; Oid reloid = rtentry->relid;
...@@ -522,8 +516,7 @@ ExecEndAppend(Append *node) ...@@ -522,8 +516,7 @@ ExecEndAppend(Append *node)
estate->es_result_relation_info = NULL; estate->es_result_relation_info = NULL;
/* /*
* XXX should free appendstate->as_rtentries and * XXX should free appendstate->as_junkfilter_list here
* appendstate->as_junkfilter_list here
*/ */
} }
void 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 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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) ...@@ -328,7 +328,14 @@ ExecInitSubPlan(SubPlan *node, EState *estate, Plan *parent)
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
* ExecSetParamPlan * 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 void
...@@ -424,13 +431,13 @@ ExecSetParamPlan(SubPlan *node, ExprContext *econtext) ...@@ -424,13 +431,13 @@ ExecSetParamPlan(SubPlan *node, ExprContext *econtext)
} }
} }
MemoryContextSwitchTo(oldcontext);
if (plan->extParam == NULL) /* un-correlated ... */ if (plan->extParam == NULL) /* un-correlated ... */
{ {
ExecEndNode(plan, plan); ExecEndNode(plan, plan);
node->needShutdown = false; node->needShutdown = false;
} }
MemoryContextSwitchTo(oldcontext);
} }
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
...@@ -470,6 +477,9 @@ ExecReScanSetParamPlan(SubPlan *node, Plan *parent) ...@@ -470,6 +477,9 @@ ExecReScanSetParamPlan(SubPlan *node, Plan *parent)
* node->plan->chgParam is not NULL... ExecReScan (plan, NULL, plan); * node->plan->chgParam is not NULL... ExecReScan (plan, NULL, plan);
*/ */
/*
* Mark this subplan's output parameters as needing recalculation
*/
foreach(lst, node->setParam) foreach(lst, node->setParam)
{ {
ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(lst)]); ParamExecData *prm = &(plan->state->es_param_exec_vals[lfirsti(lst)]);
......
...@@ -3,12 +3,16 @@ ...@@ -3,12 +3,16 @@
* nodeSubqueryscan.c * nodeSubqueryscan.c
* Support routines for scanning subqueries (subselects in rangetable). * 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) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* *
* IDENTIFICATION * 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) ...@@ -49,9 +53,7 @@ SubqueryNext(SubqueryScan *node)
SubqueryScanState *subquerystate; SubqueryScanState *subquerystate;
EState *estate; EState *estate;
ScanDirection direction; ScanDirection direction;
int execdir;
TupleTableSlot *slot; TupleTableSlot *slot;
Const countOne;
/* ---------------- /* ----------------
* get information from the estate and scan state * get information from the estate and scan state
...@@ -60,7 +62,6 @@ SubqueryNext(SubqueryScan *node) ...@@ -60,7 +62,6 @@ SubqueryNext(SubqueryScan *node)
estate = node->scan.plan.state; estate = node->scan.plan.state;
subquerystate = (SubqueryScanState *) node->scan.scanstate; subquerystate = (SubqueryScanState *) node->scan.scanstate;
direction = estate->es_direction; direction = estate->es_direction;
execdir = ScanDirectionIsBackward(direction) ? EXEC_BACK : EXEC_FOR;
slot = subquerystate->csstate.css_ScanTupleSlot; slot = subquerystate->csstate.css_ScanTupleSlot;
/* /*
...@@ -85,25 +86,13 @@ SubqueryNext(SubqueryScan *node) ...@@ -85,25 +86,13 @@ SubqueryNext(SubqueryScan *node)
return (slot); 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 * get the next tuple from the sub-query
* ---------------- * ----------------
*/ */
slot = ExecutorRun(subquerystate->sss_SubQueryDesc, subquerystate->sss_SubEState->es_direction = direction;
subquerystate->sss_SubEState,
execdir, slot = ExecProcNode(node->subplan, node->subplan);
NULL, /* offset */
(Node *) &countOne);
subquerystate->csstate.css_ScanTupleSlot = slot; subquerystate->csstate.css_ScanTupleSlot = slot;
...@@ -139,6 +128,7 @@ ExecInitSubqueryScan(SubqueryScan *node, EState *estate, Plan *parent) ...@@ -139,6 +128,7 @@ ExecInitSubqueryScan(SubqueryScan *node, EState *estate, Plan *parent)
{ {
SubqueryScanState *subquerystate; SubqueryScanState *subquerystate;
RangeTblEntry *rte; RangeTblEntry *rte;
EState *sp_estate;
/* ---------------- /* ----------------
* SubqueryScan should not have any "normal" children. * SubqueryScan should not have any "normal" children.
...@@ -177,18 +167,25 @@ ExecInitSubqueryScan(SubqueryScan *node, EState *estate, Plan *parent) ...@@ -177,18 +167,25 @@ ExecInitSubqueryScan(SubqueryScan *node, EState *estate, Plan *parent)
/* ---------------- /* ----------------
* initialize subquery * initialize subquery
*
* This should agree with ExecInitSubPlan
* ---------------- * ----------------
*/ */
rte = rt_fetch(node->scan.scanrelid, estate->es_range_table); rte = rt_fetch(node->scan.scanrelid, estate->es_range_table);
Assert(rte->subquery != NULL); Assert(rte->subquery != NULL);
subquerystate->sss_SubQueryDesc = CreateQueryDesc(rte->subquery, sp_estate = CreateExecutorState();
node->subplan, subquerystate->sss_SubEState = sp_estate;
None);
subquerystate->sss_SubEState = CreateExecutorState(); 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, if (!ExecInitNode(node->subplan, sp_estate, NULL))
subquerystate->sss_SubEState); return false;
subquerystate->csstate.css_ScanTupleSlot = NULL; subquerystate->csstate.css_ScanTupleSlot = NULL;
subquerystate->csstate.cstate.cs_TupFromTlist = false; subquerystate->csstate.cstate.cs_TupFromTlist = false;
...@@ -247,10 +244,9 @@ ExecEndSubqueryScan(SubqueryScan *node) ...@@ -247,10 +244,9 @@ ExecEndSubqueryScan(SubqueryScan *node)
* close down subquery * close down subquery
* ---------------- * ----------------
*/ */
ExecutorEnd(subquerystate->sss_SubQueryDesc, ExecEndNode(node->subplan, node->subplan);
subquerystate->sss_SubEState);
/* 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; subquerystate->csstate.css_ScanTupleSlot = NULL;
...@@ -284,6 +280,7 @@ ExecSubqueryReScan(SubqueryScan *node, ExprContext *exprCtxt, Plan *parent) ...@@ -284,6 +280,7 @@ ExecSubqueryReScan(SubqueryScan *node, ExprContext *exprCtxt, Plan *parent)
return; return;
} }
ExecReScan(node->subplan, NULL, NULL); ExecReScan(node->subplan, NULL, node->subplan);
subquerystate->csstate.css_ScanTupleSlot = NULL; subquerystate->csstate.css_ScanTupleSlot = NULL;
} }
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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) ...@@ -176,7 +176,6 @@ _copyAppend(Append *from)
* ---------------- * ----------------
*/ */
Node_Copy(from, newnode, appendplans); Node_Copy(from, newnode, appendplans);
Node_Copy(from, newnode, unionrtables);
newnode->inheritrelid = from->inheritrelid; newnode->inheritrelid = from->inheritrelid;
Node_Copy(from, newnode, inheritrtable); Node_Copy(from, newnode, inheritrtable);
...@@ -565,6 +564,33 @@ _copyUnique(Unique *from) ...@@ -565,6 +564,33 @@ _copyUnique(Unique *from)
return newnode; 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 * _copyHash
...@@ -1696,28 +1722,26 @@ _copyQuery(Query *from) ...@@ -1696,28 +1722,26 @@ _copyQuery(Query *from)
newnode->isPortal = from->isPortal; newnode->isPortal = from->isPortal;
newnode->isBinary = from->isBinary; newnode->isBinary = from->isBinary;
newnode->isTemp = from->isTemp; newnode->isTemp = from->isTemp;
newnode->unionall = from->unionall;
newnode->hasAggs = from->hasAggs; newnode->hasAggs = from->hasAggs;
newnode->hasSubLinks = from->hasSubLinks; newnode->hasSubLinks = from->hasSubLinks;
Node_Copy(from, newnode, rtable); Node_Copy(from, newnode, rtable);
Node_Copy(from, newnode, jointree); Node_Copy(from, newnode, jointree);
Node_Copy(from, newnode, targetList);
newnode->rowMarks = listCopy(from->rowMarks); newnode->rowMarks = listCopy(from->rowMarks);
Node_Copy(from, newnode, distinctClause); Node_Copy(from, newnode, targetList);
Node_Copy(from, newnode, sortClause);
Node_Copy(from, newnode, groupClause); Node_Copy(from, newnode, groupClause);
Node_Copy(from, newnode, havingQual); Node_Copy(from, newnode, havingQual);
Node_Copy(from, newnode, distinctClause);
/* why is intersectClause missing? */ Node_Copy(from, newnode, sortClause);
Node_Copy(from, newnode, unionClause);
Node_Copy(from, newnode, limitOffset); Node_Copy(from, newnode, limitOffset);
Node_Copy(from, newnode, limitCount); Node_Copy(from, newnode, limitCount);
Node_Copy(from, newnode, setOperations);
/* /*
* We do not copy the planner internal fields: base_rel_list, * We do not copy the planner internal fields: base_rel_list,
* join_rel_list, equi_key_list, query_pathkeys. Not entirely clear if * join_rel_list, equi_key_list, query_pathkeys. Not entirely clear if
...@@ -1734,17 +1758,9 @@ _copyInsertStmt(InsertStmt *from) ...@@ -1734,17 +1758,9 @@ _copyInsertStmt(InsertStmt *from)
if (from->relname) if (from->relname)
newnode->relname = pstrdup(from->relname); newnode->relname = pstrdup(from->relname);
Node_Copy(from, newnode, distinctClause);
Node_Copy(from, newnode, cols); Node_Copy(from, newnode, cols);
Node_Copy(from, newnode, targetList); Node_Copy(from, newnode, targetList);
Node_Copy(from, newnode, fromClause); Node_Copy(from, newnode, selectStmt);
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);
return newnode; return newnode;
} }
...@@ -1790,15 +1806,11 @@ _copySelectStmt(SelectStmt *from) ...@@ -1790,15 +1806,11 @@ _copySelectStmt(SelectStmt *from)
Node_Copy(from, newnode, whereClause); Node_Copy(from, newnode, whereClause);
Node_Copy(from, newnode, groupClause); Node_Copy(from, newnode, groupClause);
Node_Copy(from, newnode, havingClause); 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); Node_Copy(from, newnode, sortClause);
if (from->portalname) if (from->portalname)
newnode->portalname = pstrdup(from->portalname); newnode->portalname = pstrdup(from->portalname);
newnode->binary = from->binary; newnode->binary = from->binary;
newnode->istemp = from->istemp; newnode->istemp = from->istemp;
newnode->unionall = from->unionall;
Node_Copy(from, newnode, limitOffset); Node_Copy(from, newnode, limitOffset);
Node_Copy(from, newnode, limitCount); Node_Copy(from, newnode, limitCount);
Node_Copy(from, newnode, forUpdate); Node_Copy(from, newnode, forUpdate);
...@@ -1806,6 +1818,20 @@ _copySelectStmt(SelectStmt *from) ...@@ -1806,6 +1818,20 @@ _copySelectStmt(SelectStmt *from)
return newnode; 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 * static AlterTableStmt *
_copyAlterTableStmt(AlterTableStmt *from) _copyAlterTableStmt(AlterTableStmt *from)
{ {
...@@ -2553,6 +2579,9 @@ copyObject(void *from) ...@@ -2553,6 +2579,9 @@ copyObject(void *from)
case T_Unique: case T_Unique:
retval = _copyUnique(from); retval = _copyUnique(from);
break; break;
case T_SetOp:
retval = _copySetOp(from);
break;
case T_Hash: case T_Hash:
retval = _copyHash(from); retval = _copyHash(from);
break; break;
...@@ -2700,6 +2729,9 @@ copyObject(void *from) ...@@ -2700,6 +2729,9 @@ copyObject(void *from)
case T_SelectStmt: case T_SelectStmt:
retval = _copySelectStmt(from); retval = _copySelectStmt(from);
break; break;
case T_SetOperationStmt:
retval = _copySetOperationStmt(from);
break;
case T_AlterTableStmt: case T_AlterTableStmt:
retval = _copyAlterTableStmt(from); retval = _copyAlterTableStmt(from);
break; break;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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 @@ ...@@ -33,8 +33,6 @@
#include "utils/datum.h" #include "utils/datum.h"
static bool equali(List *a, List *b);
/* Macro for comparing string fields that might be NULL */ /* Macro for comparing string fields that might be NULL */
#define equalstr(a, b) \ #define equalstr(a, b) \
(((a) != NULL && (b) != NULL) ? (strcmp(a, b) == 0) : (a) == (b)) (((a) != NULL && (b) != NULL) ? (strcmp(a, b) == 0) : (a) == (b))
...@@ -600,8 +598,6 @@ _equalQuery(Query *a, Query *b) ...@@ -600,8 +598,6 @@ _equalQuery(Query *a, Query *b)
return false; return false;
if (a->isTemp != b->isTemp) if (a->isTemp != b->isTemp)
return false; return false;
if (a->unionall != b->unionall)
return false;
if (a->hasAggs != b->hasAggs) if (a->hasAggs != b->hasAggs)
return false; return false;
if (a->hasSubLinks != b->hasSubLinks) if (a->hasSubLinks != b->hasSubLinks)
...@@ -610,26 +606,24 @@ _equalQuery(Query *a, Query *b) ...@@ -610,26 +606,24 @@ _equalQuery(Query *a, Query *b)
return false; return false;
if (!equal(a->jointree, b->jointree)) if (!equal(a->jointree, b->jointree))
return false; return false;
if (!equal(a->targetList, b->targetList))
return false;
if (!equali(a->rowMarks, b->rowMarks)) if (!equali(a->rowMarks, b->rowMarks))
return false; return false;
if (!equal(a->distinctClause, b->distinctClause)) if (!equal(a->targetList, b->targetList))
return false;
if (!equal(a->sortClause, b->sortClause))
return false; return false;
if (!equal(a->groupClause, b->groupClause)) if (!equal(a->groupClause, b->groupClause))
return false; return false;
if (!equal(a->havingQual, b->havingQual)) if (!equal(a->havingQual, b->havingQual))
return false; return false;
if (!equal(a->intersectClause, b->intersectClause)) if (!equal(a->distinctClause, b->distinctClause))
return false; return false;
if (!equal(a->unionClause, b->unionClause)) if (!equal(a->sortClause, b->sortClause))
return false; return false;
if (!equal(a->limitOffset, b->limitOffset)) if (!equal(a->limitOffset, b->limitOffset))
return false; return false;
if (!equal(a->limitCount, b->limitCount)) if (!equal(a->limitCount, b->limitCount))
return false; return false;
if (!equal(a->setOperations, b->setOperations))
return false;
/* /*
* We do not check the internal-to-the-planner fields: base_rel_list, * We do not check the internal-to-the-planner fields: base_rel_list,
...@@ -645,27 +639,11 @@ _equalInsertStmt(InsertStmt *a, InsertStmt *b) ...@@ -645,27 +639,11 @@ _equalInsertStmt(InsertStmt *a, InsertStmt *b)
{ {
if (!equalstr(a->relname, b->relname)) if (!equalstr(a->relname, b->relname))
return false; return false;
if (!equal(a->distinctClause, b->distinctClause))
return false;
if (!equal(a->cols, b->cols)) if (!equal(a->cols, b->cols))
return false; return false;
if (!equal(a->targetList, b->targetList)) if (!equal(a->targetList, b->targetList))
return false; return false;
if (!equal(a->fromClause, b->fromClause)) if (!equal(a->selectStmt, b->selectStmt))
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))
return false; return false;
return true; return true;
...@@ -718,12 +696,6 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b) ...@@ -718,12 +696,6 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return false; return false;
if (!equal(a->havingClause, b->havingClause)) if (!equal(a->havingClause, b->havingClause))
return false; 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)) if (!equal(a->sortClause, b->sortClause))
return false; return false;
if (!equalstr(a->portalname, b->portalname)) if (!equalstr(a->portalname, b->portalname))
...@@ -732,8 +704,6 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b) ...@@ -732,8 +704,6 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return false; return false;
if (a->istemp != b->istemp) if (a->istemp != b->istemp)
return false; return false;
if (a->unionall != b->unionall)
return false;
if (!equal(a->limitOffset, b->limitOffset)) if (!equal(a->limitOffset, b->limitOffset))
return false; return false;
if (!equal(a->limitCount, b->limitCount)) if (!equal(a->limitCount, b->limitCount))
...@@ -744,6 +714,23 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b) ...@@ -744,6 +714,23 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return true; 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 static bool
_equalAlterTableStmt(AlterTableStmt *a, AlterTableStmt *b) _equalAlterTableStmt(AlterTableStmt *a, AlterTableStmt *b)
{ {
...@@ -1929,6 +1916,9 @@ equal(void *a, void *b) ...@@ -1929,6 +1916,9 @@ equal(void *a, void *b)
case T_SelectStmt: case T_SelectStmt:
retval = _equalSelectStmt(a, b); retval = _equalSelectStmt(a, b);
break; break;
case T_SetOperationStmt:
retval = _equalSetOperationStmt(a, b);
break;
case T_AlterTableStmt: case T_AlterTableStmt:
retval = _equalAlterTableStmt(a, b); retval = _equalAlterTableStmt(a, b);
break; break;
...@@ -2159,25 +2149,3 @@ equal(void *a, void *b) ...@@ -2159,25 +2149,3 @@ equal(void *a, void *b)
return retval; 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 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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 * NOTES
* XXX a few of the following functions are duplicated to handle * XXX a few of the following functions are duplicated to handle
...@@ -236,11 +236,33 @@ freeList(List *list) ...@@ -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 * sameseti
* *
* Returns t if two integer lists contain the same elements * 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 * Caution: this routine could be fooled if list1 contains
* duplicate elements. It is intended to be used on lists * duplicate elements. It is intended to be used on lists
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * 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 * NOTES
* Every (plan) node in POSTGRES has an associated "out" routine which * Every (plan) node in POSTGRES has an associated "out" routine which
...@@ -147,6 +147,7 @@ _outIndexStmt(StringInfo str, IndexStmt *node) ...@@ -147,6 +147,7 @@ _outIndexStmt(StringInfo str, IndexStmt *node)
static void static void
_outSelectStmt(StringInfo str, SelectStmt *node) _outSelectStmt(StringInfo str, SelectStmt *node)
{ {
/* XXX this is pretty durn incomplete */
appendStringInfo(str, "SELECT :where "); appendStringInfo(str, "SELECT :where ");
_outNode(str, node->whereClause); _outNode(str, node->whereClause);
} }
...@@ -258,11 +259,10 @@ _outQuery(StringInfo str, Query *node) ...@@ -258,11 +259,10 @@ _outQuery(StringInfo str, Query *node)
_outToken(str, node->into); _outToken(str, node->into);
appendStringInfo(str, " :isPortal %s :isBinary %s :isTemp %s" 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->isPortal ? "true" : "false",
node->isBinary ? "true" : "false", node->isBinary ? "true" : "false",
node->isTemp ? "true" : "false", node->isTemp ? "true" : "false",
node->unionall ? "true" : "false",
node->hasAggs ? "true" : "false", node->hasAggs ? "true" : "false",
node->hasSubLinks ? "true" : "false"); node->hasSubLinks ? "true" : "false");
_outNode(str, node->rtable); _outNode(str, node->rtable);
...@@ -270,17 +270,11 @@ _outQuery(StringInfo str, Query *node) ...@@ -270,17 +270,11 @@ _outQuery(StringInfo str, Query *node)
appendStringInfo(str, " :jointree "); appendStringInfo(str, " :jointree ");
_outNode(str, node->jointree); _outNode(str, node->jointree);
appendStringInfo(str, " :targetList ");
_outNode(str, node->targetList);
appendStringInfo(str, " :rowMarks "); appendStringInfo(str, " :rowMarks ");
_outIntList(str, node->rowMarks); _outIntList(str, node->rowMarks);
appendStringInfo(str, " :distinctClause "); appendStringInfo(str, " :targetList ");
_outNode(str, node->distinctClause); _outNode(str, node->targetList);
appendStringInfo(str, " :sortClause ");
_outNode(str, node->sortClause);
appendStringInfo(str, " :groupClause "); appendStringInfo(str, " :groupClause ");
_outNode(str, node->groupClause); _outNode(str, node->groupClause);
...@@ -288,17 +282,20 @@ _outQuery(StringInfo str, Query *node) ...@@ -288,17 +282,20 @@ _outQuery(StringInfo str, Query *node)
appendStringInfo(str, " :havingQual "); appendStringInfo(str, " :havingQual ");
_outNode(str, node->havingQual); _outNode(str, node->havingQual);
appendStringInfo(str, " :intersectClause "); appendStringInfo(str, " :distinctClause ");
_outNode(str, node->intersectClause); _outNode(str, node->distinctClause);
appendStringInfo(str, " :unionClause "); appendStringInfo(str, " :sortClause ");
_outNode(str, node->unionClause); _outNode(str, node->sortClause);
appendStringInfo(str, " :limitOffset "); appendStringInfo(str, " :limitOffset ");
_outNode(str, node->limitOffset); _outNode(str, node->limitOffset);
appendStringInfo(str, " :limitCount "); appendStringInfo(str, " :limitCount ");
_outNode(str, node->limitCount); _outNode(str, node->limitCount);
appendStringInfo(str, " :setOperations ");
_outNode(str, node->setOperations);
} }
static void static void
...@@ -315,6 +312,19 @@ _outGroupClause(StringInfo str, GroupClause *node) ...@@ -315,6 +312,19 @@ _outGroupClause(StringInfo str, GroupClause *node)
node->tleSortGroupRef, node->sortop); 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 * print the basic stuff of all nodes that inherit from Plan
*/ */
...@@ -384,11 +394,7 @@ _outAppend(StringInfo str, Append *node) ...@@ -384,11 +394,7 @@ _outAppend(StringInfo str, Append *node)
appendStringInfo(str, " :appendplans "); appendStringInfo(str, " :appendplans ");
_outNode(str, node->appendplans); _outNode(str, node->appendplans);
appendStringInfo(str, " :unionrtables "); appendStringInfo(str, " :inheritrelid %u :inheritrtable ",
_outNode(str, node->unionrtables);
appendStringInfo(str,
" :inheritrelid %u :inheritrtable ",
node->inheritrelid); node->inheritrelid);
_outNode(str, node->inheritrtable); _outNode(str, node->inheritrtable);
} }
...@@ -601,6 +607,22 @@ _outUnique(StringInfo str, Unique *node) ...@@ -601,6 +607,22 @@ _outUnique(StringInfo str, Unique *node)
appendStringInfo(str, "%d ", (int) node->uniqColIdx[i]); 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 * Hash is a subclass of Plan
*/ */
...@@ -1480,6 +1502,9 @@ _outNode(StringInfo str, void *obj) ...@@ -1480,6 +1502,9 @@ _outNode(StringInfo str, void *obj)
case T_GroupClause: case T_GroupClause:
_outGroupClause(str, obj); _outGroupClause(str, obj);
break; break;
case T_SetOperationStmt:
_outSetOperationStmt(str, obj);
break;
case T_Plan: case T_Plan:
_outPlan(str, obj); _outPlan(str, obj);
break; break;
...@@ -1531,6 +1556,9 @@ _outNode(StringInfo str, void *obj) ...@@ -1531,6 +1556,9 @@ _outNode(StringInfo str, void *obj)
case T_Unique: case T_Unique:
_outUnique(str, obj); _outUnique(str, obj);
break; break;
case T_SetOp:
_outSetOp(str, obj);
break;
case T_Hash: case T_Hash:
_outHash(str, obj); _outHash(str, obj);
break; break;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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 * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
...@@ -338,6 +338,9 @@ plannode_type(Plan *p) ...@@ -338,6 +338,9 @@ plannode_type(Plan *p)
case T_Unique: case T_Unique:
return "UNIQUE"; return "UNIQUE";
break; break;
case T_SetOp:
return "SETOP";
break;
case T_Hash: case T_Hash:
return "HASH"; return "HASH";
break; break;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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 * NOTES
* Most of the read functions for plan nodes are tested. (In fact, they * Most of the read functions for plan nodes are tested. (In fact, they
...@@ -109,10 +109,6 @@ _readQuery() ...@@ -109,10 +109,6 @@ _readQuery()
token = lsptok(NULL, &length); /* get isTemp */ token = lsptok(NULL, &length); /* get isTemp */
local_node->isTemp = (token[0] == 't') ? true : false; 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); /* skip the :hasAggs */
token = lsptok(NULL, &length); /* get hasAggs */ token = lsptok(NULL, &length); /* get hasAggs */
local_node->hasAggs = (token[0] == 't') ? true : false; local_node->hasAggs = (token[0] == 't') ? true : false;
...@@ -127,17 +123,11 @@ _readQuery() ...@@ -127,17 +123,11 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :jointree */ token = lsptok(NULL, &length); /* skip :jointree */
local_node->jointree = nodeRead(true); local_node->jointree = nodeRead(true);
token = lsptok(NULL, &length); /* skip :targetlist */
local_node->targetList = nodeRead(true);
token = lsptok(NULL, &length); /* skip :rowMarks */ token = lsptok(NULL, &length); /* skip :rowMarks */
local_node->rowMarks = toIntList(nodeRead(true)); local_node->rowMarks = toIntList(nodeRead(true));
token = lsptok(NULL, &length); /* skip :distinctClause */ token = lsptok(NULL, &length); /* skip :targetlist */
local_node->distinctClause = nodeRead(true); local_node->targetList = nodeRead(true);
token = lsptok(NULL, &length); /* skip :sortClause */
local_node->sortClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :groupClause */ token = lsptok(NULL, &length); /* skip :groupClause */
local_node->groupClause = nodeRead(true); local_node->groupClause = nodeRead(true);
...@@ -145,11 +135,11 @@ _readQuery() ...@@ -145,11 +135,11 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :havingQual */ token = lsptok(NULL, &length); /* skip :havingQual */
local_node->havingQual = nodeRead(true); local_node->havingQual = nodeRead(true);
token = lsptok(NULL, &length); /* skip :intersectClause */ token = lsptok(NULL, &length); /* skip :distinctClause */
local_node->intersectClause = nodeRead(true); local_node->distinctClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :unionClause */ token = lsptok(NULL, &length); /* skip :sortClause */
local_node->unionClause = nodeRead(true); local_node->sortClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :limitOffset */ token = lsptok(NULL, &length); /* skip :limitOffset */
local_node->limitOffset = nodeRead(true); local_node->limitOffset = nodeRead(true);
...@@ -157,6 +147,9 @@ _readQuery() ...@@ -157,6 +147,9 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :limitCount */ token = lsptok(NULL, &length); /* skip :limitCount */
local_node->limitCount = nodeRead(true); local_node->limitCount = nodeRead(true);
token = lsptok(NULL, &length); /* skip :setOperations */
local_node->setOperations = nodeRead(true);
return local_node; return local_node;
} }
...@@ -208,6 +201,39 @@ _readGroupClause() ...@@ -208,6 +201,39 @@ _readGroupClause()
return local_node; 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 * _getPlan
* ---------------- * ----------------
...@@ -322,9 +348,6 @@ _readAppend() ...@@ -322,9 +348,6 @@ _readAppend()
token = lsptok(NULL, &length); /* eat :appendplans */ token = lsptok(NULL, &length); /* eat :appendplans */
local_node->appendplans = nodeRead(true); /* now read it */ 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); /* eat :inheritrelid */
token = lsptok(NULL, &length); /* get inheritrelid */ token = lsptok(NULL, &length); /* get inheritrelid */
local_node->inheritrelid = strtoul(token, NULL, 10); local_node->inheritrelid = strtoul(token, NULL, 10);
...@@ -1995,6 +2018,8 @@ parsePlanString(void) ...@@ -1995,6 +2018,8 @@ parsePlanString(void)
return_value = _readSortClause(); return_value = _readSortClause();
else if (length == 11 && strncmp(token, "GROUPCLAUSE", length) == 0) else if (length == 11 && strncmp(token, "GROUPCLAUSE", length) == 0)
return_value = _readGroupClause(); return_value = _readGroupClause();
else if (length == 16 && strncmp(token, "SETOPERATIONSTMT", length) == 0)
return_value = _readSetOperationStmt();
else if (length == 4 && strncmp(token, "CASE", length) == 0) else if (length == 4 && strncmp(token, "CASE", length) == 0)
return_value = _readCaseExpr(); return_value = _readCaseExpr();
else if (length == 4 && strncmp(token, "WHEN", length) == 0) else if (length == 4 && strncmp(token, "WHEN", length) == 0)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -98,7 +98,8 @@ set_base_rel_pathlist(Query *root)
*/ */
/* Generate the plan for the subquery */ /* 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 */ /* Copy number of output rows from subplan */
rel->tuples = rel->subplan->plan_rows; rel->tuples = rel->subplan->plan_rows;
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * 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, ...@@ -68,8 +68,6 @@ static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
ScanDirection indexscandir); ScanDirection indexscandir);
static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid, static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
List *tideval); List *tideval);
static SubqueryScan *make_subqueryscan(List *qptlist, List *qpqual,
Index scanrelid, Plan *subplan);
static NestLoop *make_nestloop(List *tlist, static NestLoop *make_nestloop(List *tlist,
List *joinclauses, List *otherclauses, List *joinclauses, List *otherclauses,
Plan *lefttree, Plan *righttree, Plan *lefttree, Plan *righttree,
...@@ -86,7 +84,6 @@ static MergeJoin *make_mergejoin(List *tlist, ...@@ -86,7 +84,6 @@ static MergeJoin *make_mergejoin(List *tlist,
Plan *lefttree, Plan *righttree, Plan *lefttree, Plan *righttree,
JoinType jointype); JoinType jointype);
static void copy_path_costsize(Plan *dest, Path *src); static void copy_path_costsize(Plan *dest, Path *src);
static void copy_plan_costsize(Plan *dest, Plan *src);
/* /*
* create_plan * create_plan
...@@ -1109,7 +1106,7 @@ copy_path_costsize(Plan *dest, Path *src) ...@@ -1109,7 +1106,7 @@ copy_path_costsize(Plan *dest, Path *src)
* but it helps produce more reasonable-looking EXPLAIN output. * but it helps produce more reasonable-looking EXPLAIN output.
* (Some callers alter the info after copying it.) * (Some callers alter the info after copying it.)
*/ */
static void void
copy_plan_costsize(Plan *dest, Plan *src) copy_plan_costsize(Plan *dest, Plan *src)
{ {
if (src) if (src)
...@@ -1206,7 +1203,7 @@ make_tidscan(List *qptlist, ...@@ -1206,7 +1203,7 @@ make_tidscan(List *qptlist,
return node; return node;
} }
static SubqueryScan * SubqueryScan *
make_subqueryscan(List *qptlist, make_subqueryscan(List *qptlist,
List *qpqual, List *qpqual,
Index scanrelid, Index scanrelid,
...@@ -1593,6 +1590,67 @@ make_unique(List *tlist, Plan *lefttree, List *distinctList) ...@@ -1593,6 +1590,67 @@ make_unique(List *tlist, Plan *lefttree, List *distinctList)
return node; 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 * Result *
make_result(List *tlist, make_result(List *tlist,
Node *resconstantqual, Node *resconstantqual,
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* *
* *
* IDENTIFICATION * 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, ...@@ -174,8 +174,6 @@ subplanner(Query *root,
List *brel; List *brel;
RelOptInfo *final_rel; RelOptInfo *final_rel;
Plan *resultplan; Plan *resultplan;
MemoryContext mycontext;
MemoryContext oldcxt;
Path *cheapestpath; Path *cheapestpath;
Path *presortedpath; Path *presortedpath;
...@@ -227,24 +225,6 @@ subplanner(Query *root, ...@@ -227,24 +225,6 @@ subplanner(Query *root,
*/ */
root->query_pathkeys = canonicalize_pathkeys(root, root->query_pathkeys); 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. * Ready to do the primary planning.
*/ */
...@@ -355,25 +335,5 @@ subplanner(Query *root, ...@@ -355,25 +335,5 @@ subplanner(Query *root,
plan_built: 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; return resultplan;
} }
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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, ...@@ -48,7 +48,6 @@ static List *make_subplanTargetList(Query *parse, List *tlist,
static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup, static Plan *make_groupplan(List *group_tlist, bool tuplePerGroup,
List *groupClause, AttrNumber *grpColIdx, List *groupClause, AttrNumber *grpColIdx,
bool is_presorted, Plan *subplan); bool is_presorted, Plan *subplan);
static Plan *make_sortplan(List *tlist, Plan *plannode, List *sortcls);
/***************************************************************************** /*****************************************************************************
* *
...@@ -60,43 +59,32 @@ planner(Query *parse) ...@@ -60,43 +59,32 @@ planner(Query *parse)
{ {
Plan *result_plan; Plan *result_plan;
Index save_PlannerQueryLevel; Index save_PlannerQueryLevel;
List *save_PlannerInitPlan;
List *save_PlannerParamVar; List *save_PlannerParamVar;
int save_PlannerPlanId;
/* /*
* The outer planner can be called recursively, for example to process * The planner can be called recursively (an example is when
* a subquery in the rangetable. (A less obvious example occurs when * eval_const_expressions tries to simplify an SQL function).
* eval_const_expressions tries to simplify an SQL function.) * So, these global state variables must be saved and restored.
* So, 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_PlannerQueryLevel = PlannerQueryLevel;
save_PlannerInitPlan = PlannerInitPlan;
save_PlannerParamVar = PlannerParamVar; save_PlannerParamVar = PlannerParamVar;
save_PlannerPlanId = PlannerPlanId;
/* Initialize state for subselects */ /* Initialize state for handling outer-level references and params */
PlannerQueryLevel = 1; PlannerQueryLevel = 0; /* will be 1 in top-level subquery_planner */
PlannerInitPlan = NULL; PlannerParamVar = NIL;
PlannerParamVar = NULL;
PlannerPlanId = 0;
/* this should go away sometime soon */ /* primary planning entry point (may recurse for subqueries) */
transformKeySetQuery(parse);
/* primary planning entry point (may recurse for sublinks) */
result_plan = subquery_planner(parse, -1.0 /* default case */ ); result_plan = subquery_planner(parse, -1.0 /* default case */ );
Assert(PlannerQueryLevel == 1); Assert(PlannerQueryLevel == 0);
/* if top-level query had subqueries, do housekeeping for them */
if (PlannerPlanId > 0)
{
(void) SS_finalize_plan(result_plan);
result_plan->initPlan = PlannerInitPlan;
}
/* executor wants to know total number of Params used overall */ /* executor wants to know total number of Params used overall */
result_plan->nParamExec = length(PlannerParamVar); result_plan->nParamExec = length(PlannerParamVar);
...@@ -106,9 +94,7 @@ planner(Query *parse) ...@@ -106,9 +94,7 @@ planner(Query *parse)
/* restore state for outer planner, if any */ /* restore state for outer planner, if any */
PlannerQueryLevel = save_PlannerQueryLevel; PlannerQueryLevel = save_PlannerQueryLevel;
PlannerInitPlan = save_PlannerInitPlan;
PlannerParamVar = save_PlannerParamVar; PlannerParamVar = save_PlannerParamVar;
PlannerPlanId = save_PlannerPlanId;
return result_plan; return result_plan;
} }
...@@ -125,14 +111,9 @@ planner(Query *parse) ...@@ -125,14 +111,9 @@ planner(Query *parse)
* *
* Basically, this routine does the stuff that should only be done once * Basically, this routine does the stuff that should only be done once
* per Query object. It then calls union_planner, which may be called * 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 * recursively on the same Query node in order to handle inheritance.
* inheritance. subquery_planner is called recursively from subselect.c * subquery_planner will be called recursively to handle sub-Query nodes
* to handle sub-Query nodes found within the query's expressions. * found within the query's expressions and rangetable.
*
* 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.
* *
* Returns a query plan. * Returns a query plan.
*-------------------- *--------------------
...@@ -140,6 +121,20 @@ planner(Query *parse) ...@@ -140,6 +121,20 @@ planner(Query *parse)
Plan * Plan *
subquery_planner(Query *parse, double tuple_fraction) 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 * Check to see if any subqueries in the rangetable can be merged into
* this query. * this query.
...@@ -179,9 +174,9 @@ subquery_planner(Query *parse, double tuple_fraction) ...@@ -179,9 +174,9 @@ subquery_planner(Query *parse, double tuple_fraction)
EXPRKIND_HAVING); 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? * XXX should any more of union_planner's activity be moved here?
...@@ -190,6 +185,35 @@ subquery_planner(Query *parse, double tuple_fraction) ...@@ -190,6 +185,35 @@ subquery_planner(Query *parse, double tuple_fraction)
* but I suspect it would pay off in simplicity and avoidance of * but I suspect it would pay off in simplicity and avoidance of
* wasted cycles. * 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) ...@@ -320,9 +344,10 @@ is_simple_subquery(Query *subquery)
if (subquery->limitOffset || subquery->limitCount) if (subquery->limitOffset || subquery->limitCount)
elog(ERROR, "LIMIT is not supported in subselects"); 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; return false;
/* /*
* Can't pull up a subquery involving grouping, aggregation, or sorting. * Can't pull up a subquery involving grouping, aggregation, or sorting.
...@@ -573,7 +598,7 @@ preprocess_qual_conditions(Query *parse, Node *jtnode) ...@@ -573,7 +598,7 @@ preprocess_qual_conditions(Query *parse, Node *jtnode)
/*-------------------- /*--------------------
* union_planner * 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 * appends produced by inheritance), recursing if necessary to get them
* all, then processes normal plans. * all, then processes normal plans.
* *
...@@ -606,24 +631,31 @@ union_planner(Query *parse, ...@@ -606,24 +631,31 @@ union_planner(Query *parse,
Index rt_index; Index rt_index;
List *inheritors; 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 */ * Construct the plan for set operations. The result will
tlist = preprocess_targetlist(tlist, * not need any work except perhaps a top-level sort.
parse->commandType, */
parse->resultRelation, result_plan = plan_set_operations(parse);
parse->rtable);
/*
* 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 * We leave current_pathkeys NIL indicating we do not know sort
* order. This is correct for the appended-together subplan * order. This is correct when the top set operation is UNION ALL,
* results, even if the subplans themselves produced sorted results. * 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 * Calculate pathkeys that represent grouping/ordering
* requirements * requirements (grouping should always be null, but...)
*/ */
group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause, group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
tlist); tlist);
...@@ -886,7 +918,7 @@ union_planner(Query *parse, ...@@ -886,7 +918,7 @@ union_planner(Query *parse,
tuple_fraction = 0.25; tuple_fraction = 0.25;
} }
/* Generate the (sub) plan */ /* Generate the basic plan for this Query */
result_plan = query_planner(parse, result_plan = query_planner(parse,
sub_tlist, sub_tlist,
tuple_fraction); tuple_fraction);
...@@ -1176,7 +1208,7 @@ make_groupplan(List *group_tlist, ...@@ -1176,7 +1208,7 @@ make_groupplan(List *group_tlist,
* make_sortplan * make_sortplan
* Add a Sort node to implement an explicit ORDER BY clause. * Add a Sort node to implement an explicit ORDER BY clause.
*/ */
static Plan * Plan *
make_sortplan(List *tlist, Plan *plannode, List *sortcls) make_sortplan(List *tlist, Plan *plannode, List *sortcls)
{ {
List *sort_tlist; List *sort_tlist;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -103,9 +103,15 @@ set_plan_references(Plan *plan)
fix_expr_references(plan, (Node *) plan->qual); fix_expr_references(plan, (Node *) plan->qual);
break; break;
case T_SubqueryScan: 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->targetlist);
fix_expr_references(plan, (Node *) plan->qual); 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; break;
case T_NestLoop: case T_NestLoop:
set_join_references((Join *) plan); set_join_references((Join *) plan);
...@@ -132,6 +138,7 @@ set_plan_references(Plan *plan) ...@@ -132,6 +138,7 @@ set_plan_references(Plan *plan)
case T_Material: case T_Material:
case T_Sort: case T_Sort:
case T_Unique: case T_Unique:
case T_SetOp:
case T_Hash: case T_Hash:
/* /*
...@@ -170,6 +177,7 @@ set_plan_references(Plan *plan) ...@@ -170,6 +177,7 @@ set_plan_references(Plan *plan)
* Append, like Sort et al, doesn't actually evaluate its * Append, like Sort et al, doesn't actually evaluate its
* targetlist or quals, and we haven't bothered to give it * targetlist or quals, and we haven't bothered to give it
* its own tlist copy. So, don't fix targetlist/qual. * its own tlist copy. So, don't fix targetlist/qual.
* But do recurse into subplans.
*/ */
foreach(pl, ((Append *) plan)->appendplans) foreach(pl, ((Append *) plan)->appendplans)
set_plan_references((Plan *) lfirst(pl)); set_plan_references((Plan *) lfirst(pl));
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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 @@ ...@@ -29,7 +29,8 @@
Index PlannerQueryLevel; /* level of current query */ Index PlannerQueryLevel; /* level of current query */
List *PlannerInitPlan; /* init subplans for current query */ List *PlannerInitPlan; /* init subplans for current query */
List *PlannerParamVar; /* to get Var from Param->paramid */ 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 * PlannerParamVar is a list of Var nodes, wherein the n'th entry
...@@ -81,7 +82,7 @@ replace_var(Var *var) ...@@ -81,7 +82,7 @@ replace_var(Var *var)
/* /*
* If there's already a PlannerParamVar entry for this same Var, just * 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 * possible for the same varno/varlevel to refer to different RTEs in
* different parts of the parsetree, so that different fields might * different parts of the parsetree, so that different fields might
* end up sharing the same Param number. As long as we check the * end up sharing the same Param number. As long as we check the
...@@ -128,11 +129,6 @@ make_subplan(SubLink *slink) ...@@ -128,11 +129,6 @@ make_subplan(SubLink *slink)
Plan *plan; Plan *plan;
List *lst; List *lst;
Node *result; 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 * Check to see if this node was already processed; if so we have
...@@ -181,45 +177,30 @@ make_subplan(SubLink *slink) ...@@ -181,45 +177,30 @@ make_subplan(SubLink *slink)
else else
tuple_fraction = -1.0; /* default behavior */ 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, * Generate the plan for the subquery.
* 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.
*/ */
(void) SS_finalize_plan(plan); node->plan = plan = subquery_planner(subquery, tuple_fraction);
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));
}
}
/* and now we are parent again */ node->plan_id = PlannerPlanId++; /* Assign unique ID to this SubPlan */
PlannerInitPlan = saved_ip;
PlannerQueryLevel--;
node->plan_id = PlannerPlanId++;
node->rtable = subquery->rtable; node->rtable = subquery->rtable;
node->sublink = slink; node->sublink = slink;
slink->subselect = NULL; /* cool ?! see error check above! */ 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) 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 */ /* note varlevelsup is absolute level number */
if (var->varlevelsup == PlannerQueryLevel) 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) ...@@ -625,6 +606,11 @@ SS_finalize_plan(Plan *plan)
SS_finalize_plan((Plan *) lfirst(lst))); SS_finalize_plan((Plan *) lfirst(lst)));
break; break;
case T_SubqueryScan:
results.paramids = set_unioni(results.paramids,
SS_finalize_plan(((SubqueryScan *) plan)->subplan));
break;
case T_IndexScan: case T_IndexScan:
finalize_primnode((Node *) ((IndexScan *) plan)->indxqual, finalize_primnode((Node *) ((IndexScan *) plan)->indxqual,
&results); &results);
...@@ -667,10 +653,10 @@ SS_finalize_plan(Plan *plan) ...@@ -667,10 +653,10 @@ SS_finalize_plan(Plan *plan)
case T_Agg: case T_Agg:
case T_SeqScan: case T_SeqScan:
case T_SubqueryScan:
case T_Material: case T_Material:
case T_Sort: case T_Sort:
case T_Unique: case T_Unique:
case T_SetOp:
case T_Group: case T_Group:
break; break;
...@@ -689,17 +675,18 @@ SS_finalize_plan(Plan *plan) ...@@ -689,17 +675,18 @@ SS_finalize_plan(Plan *plan)
foreach(lst, results.paramids) 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 */ /* note varlevelsup is absolute level number */
if (var->varlevelsup < PlannerQueryLevel) if (var->varlevelsup < PlannerQueryLevel)
extParam = lappendi(extParam, lfirsti(lst)); extParam = lappendi(extParam, paramid);
else if (var->varlevelsup > PlannerQueryLevel) else if (var->varlevelsup > PlannerQueryLevel)
elog(ERROR, "SS_finalize_plan: plan shouldn't reference subplan's variable"); elog(ERROR, "SS_finalize_plan: plan shouldn't reference subplan's variable");
else else
{ {
Assert(var->varno == 0 && var->varattno == 0); Assert(var->varno == 0 && var->varattno == 0);
locParam = lappendi(locParam, lfirsti(lst)); locParam = lappendi(locParam, paramid);
} }
} }
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
bool _use_keyset_query_optimizer = FALSE; bool _use_keyset_query_optimizer = FALSE;
#ifdef ENABLE_KEY_SET_QUERY
static int inspectOpNode(Expr *expr); static int inspectOpNode(Expr *expr);
static int inspectAndNode(Expr *expr); static int inspectAndNode(Expr *expr);
static int inspectOrNode(Expr *expr); static int inspectOrNode(Expr *expr);
...@@ -213,3 +215,5 @@ inspectOpNode(Expr *expr) ...@@ -213,3 +215,5 @@ inspectOpNode(Expr *expr)
secondExpr = lsecond(expr->args); secondExpr = lsecond(expr->args);
return (firstExpr && secondExpr && nodeTag(firstExpr) == T_Var && nodeTag(secondExpr) == T_Const); return (firstExpr && secondExpr && nodeTag(firstExpr) == T_Var && nodeTag(secondExpr) == T_Const);
} }
#endif /* ENABLE_KEY_SET_QUERY */
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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, ...@@ -49,6 +49,17 @@ preprocess_targetlist(List *tlist,
Index result_relation, Index result_relation,
List *range_table) 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 * for heap_formtuple to work, the targetlist must match the exact
......
This diff is collapsed.
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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 * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
...@@ -1636,8 +1636,8 @@ simplify_op_or_func(Expr *expr, List *args) ...@@ -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. * 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 * (But only the "expr" part of a TargetEntry is examined, unless the walker
* chooses to process TargetEntry nodes specially.) Also, RangeTblRef, * chooses to process TargetEntry nodes specially.) Also, RangeTblRef,
* FromExpr, and JoinExpr nodes are handled, so that qual expressions in a * FromExpr, JoinExpr, and SetOperationStmt nodes are handled, so that query
* jointree can be processed without additional code. * jointrees and setOperation trees can be processed without additional code.
* *
* expression_tree_walker will handle SubLink and SubPlan nodes by recursing * expression_tree_walker will handle SubLink and SubPlan nodes by recursing
* normally into the "lefthand" arguments (which belong to the outer plan). * normally into the "lefthand" arguments (which belong to the outer plan).
...@@ -1654,7 +1654,8 @@ simplify_op_or_func(Expr *expr, List *args) ...@@ -1654,7 +1654,8 @@ simplify_op_or_func(Expr *expr, List *args)
* if (IsA(node, Query)) * if (IsA(node, Query))
* { * {
* adjust context for subquery; * 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; * restore context if needed;
* return result; * return result;
* } * }
...@@ -1827,6 +1828,16 @@ expression_tree_walker(Node *node, ...@@ -1827,6 +1828,16 @@ expression_tree_walker(Node *node,
*/ */
} }
break; break;
case T_SetOperationStmt:
{
SetOperationStmt *setop = (SetOperationStmt *) node;
if (walker(setop->larg, context))
return true;
if (walker(setop->rarg, context))
return true;
}
break;
default: default:
elog(ERROR, "expression_tree_walker: Unexpected node type %d", elog(ERROR, "expression_tree_walker: Unexpected node type %d",
nodeTag(node)); nodeTag(node));
...@@ -1843,11 +1854,17 @@ expression_tree_walker(Node *node, ...@@ -1843,11 +1854,17 @@ expression_tree_walker(Node *node,
* for starting a walk at top level of a Query regardless of whether the * 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 * walker intends to descend into subqueries. It is also useful for
* descending into subqueries within a walker. * 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 bool
query_tree_walker(Query *query, query_tree_walker(Query *query,
bool (*walker) (), bool (*walker) (),
void *context) void *context,
bool visitQueryRTEs)
{ {
Assert(query != NULL && IsA(query, Query)); Assert(query != NULL && IsA(query, Query));
...@@ -1855,11 +1872,23 @@ query_tree_walker(Query *query, ...@@ -1855,11 +1872,23 @@ query_tree_walker(Query *query,
return true; return true;
if (walker((Node *) query->jointree, context)) if (walker((Node *) query->jointree, context))
return true; return true;
if (walker(query->setOperations, context))
return true;
if (walker(query->havingQual, context)) if (walker(query->havingQual, context))
return true; return true;
/* if (visitQueryRTEs)
* XXX for subselect-in-FROM, may need to examine rtable as well? {
*/ List *rt;
foreach(rt, query->rtable)
{
RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
if (rte->subquery)
if (walker(rte->subquery, context))
return true;
}
}
return false; return false;
} }
...@@ -2158,6 +2187,17 @@ expression_tree_mutator(Node *node, ...@@ -2158,6 +2187,17 @@ expression_tree_mutator(Node *node,
return (Node *) newnode; return (Node *) newnode;
} }
break; 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: default:
elog(ERROR, "expression_tree_mutator: Unexpected node type %d", elog(ERROR, "expression_tree_mutator: Unexpected node type %d",
nodeTag(node)); nodeTag(node));
...@@ -2166,3 +2206,58 @@ expression_tree_mutator(Node *node, ...@@ -2166,3 +2206,58 @@ expression_tree_mutator(Node *node,
/* can't get here, but keep compiler happy */ /* can't get here, but keep compiler happy */
return NULL; 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 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -67,7 +67,7 @@ pull_varnos(Node *node)
*/ */
if (node && IsA(node, Query)) if (node && IsA(node, Query))
query_tree_walker((Query *) node, pull_varnos_walker, query_tree_walker((Query *) node, pull_varnos_walker,
(void *) &context); (void *) &context, true);
else else
pull_varnos_walker(node, &context); pull_varnos_walker(node, &context);
...@@ -108,12 +108,12 @@ pull_varnos_walker(Node *node, pull_varnos_context *context) ...@@ -108,12 +108,12 @@ pull_varnos_walker(Node *node, pull_varnos_context *context)
} }
if (IsA(node, Query)) if (IsA(node, Query))
{ {
/* Recurse into not-yet-planned subquery */ /* Recurse into RTE subquery or not-yet-planned sublink subquery */
bool result; bool result;
context->sublevels_up++; context->sublevels_up++;
result = query_tree_walker((Query *) node, pull_varnos_walker, result = query_tree_walker((Query *) node, pull_varnos_walker,
(void *) context); (void *) context, true);
context->sublevels_up--; context->sublevels_up--;
return result; return result;
} }
......
This diff is collapsed.
This diff is collapsed.
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -347,7 +347,8 @@ transformTableEntry(ParseState *pstate, RangeVar *r)
static RangeTblRef * static RangeTblRef *
transformRangeSubselect(ParseState *pstate, RangeSubselect *r) transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
{ {
SelectStmt *subquery = (SelectStmt *) r->subquery; List *save_rtable;
List *save_joinlist;
List *parsetrees; List *parsetrees;
Query *query; Query *query;
RangeTblEntry *rte; RangeTblEntry *rte;
...@@ -362,19 +363,21 @@ transformRangeSubselect(ParseState *pstate, RangeSubselect *r) ...@@ -362,19 +363,21 @@ transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
elog(ERROR, "sub-select in FROM must have an alias"); elog(ERROR, "sub-select in FROM must have an alias");
/* /*
* subquery node might not be SelectStmt if user wrote something like * Analyze and transform the subquery. This is a bit tricky because
* FROM (SELECT ... UNION SELECT ...). Our current implementation of * we don't want the subquery to be able to see any FROM items already
* UNION/INTERSECT/EXCEPT is too messy to deal with here, so punt until * created in the current query (per SQL92, the scope of a FROM item
* we redesign querytrees to make it more reasonable. * 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)) save_rtable = pstate->p_rtable;
elog(ERROR, "Set operations not yet supported in subselects in FROM"); save_joinlist = pstate->p_joinlist;
pstate->p_rtable = NIL;
/* pstate->p_joinlist = NIL;
* Analyze and transform the subquery as if it were an independent parsetrees = parse_analyze(makeList1(r->subquery), pstate);
* statement (we do NOT want it to see the outer query as a parent). pstate->p_rtable = save_rtable;
*/ pstate->p_joinlist = save_joinlist;
parsetrees = parse_analyze(makeList1(subquery), NULL);
/* /*
* Check that we got something reasonable. Some of these conditions * Check that we got something reasonable. Some of these conditions
...@@ -1181,108 +1184,3 @@ exprIsInSortList(Node *expr, List *sortList, List *targetList) ...@@ -1181,108 +1184,3 @@ exprIsInSortList(Node *expr, List *sortList, List *targetList)
} }
return false; 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 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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, ...@@ -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() /* TypeCategory()
* Assign a category to the specified OID. * Assign a category to the specified OID.
*/ */
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -412,9 +412,9 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
{ {
CaseExpr *c = (CaseExpr *) expr; CaseExpr *c = (CaseExpr *) expr;
CaseWhen *w; CaseWhen *w;
List *typeids = NIL;
List *args; List *args;
Oid ptype; Oid ptype;
CATEGORY pcategory;
/* transform the list of arguments */ /* transform the list of arguments */
foreach(args, c->args) foreach(args, c->args)
...@@ -432,6 +432,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence) ...@@ -432,6 +432,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
w->expr = (Node *) a; w->expr = (Node *) a;
} }
lfirst(args) = transformExpr(pstate, (Node *) w, precedence); lfirst(args) = transformExpr(pstate, (Node *) w, precedence);
typeids = lappendi(typeids, exprType(w->result));
} }
/* /*
...@@ -452,104 +453,26 @@ transformExpr(ParseState *pstate, Node *expr, int precedence) ...@@ -452,104 +453,26 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
c->defresult = (Node *) n; c->defresult = (Node *) n;
} }
c->defresult = transformExpr(pstate, c->defresult, precedence); c->defresult = transformExpr(pstate, c->defresult, precedence);
/*
* 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
*/
typeids = lconsi(exprType(c->defresult), typeids);
/* now check types across result clauses... */ ptype = select_common_type(typeids, "CASE");
c->casetype = exprType(c->defresult); c->casetype = ptype;
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...
*/
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))
{
/*
* new one is preferred and can convert? then
* take it...
*/
ptype = wtype;
pcategory = TypeCategory(ptype);
}
}
}
/* Convert default result clause, if necessary */ /* Convert default result clause, if necessary */
if (c->casetype != ptype) c->defresult = coerce_to_common_type(pstate, c->defresult,
{ ptype, "CASE/ELSE");
if (!c->casetype || c->casetype == UNKNOWNOID)
{
/* /* Convert when-clause results, if necessary */
* 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 */
foreach(args, c->args) foreach(args, c->args)
{ {
Oid wtype;
w = lfirst(args); w = lfirst(args);
wtype = exprType(w->result); w->result = coerce_to_common_type(pstate, w->result,
ptype, "CASE/WHEN");
/*
* 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));
}
}
} }
result = expr; result = expr;
...@@ -560,7 +483,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence) ...@@ -560,7 +483,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
{ {
CaseWhen *w = (CaseWhen *) expr; CaseWhen *w = (CaseWhen *) expr;
w->expr = transformExpr(pstate, (Node *) w->expr, precedence); w->expr = transformExpr(pstate, w->expr, precedence);
if (exprType(w->expr) != BOOLOID) if (exprType(w->expr) != BOOLOID)
elog(ERROR, "WHEN clause must have a boolean result"); elog(ERROR, "WHEN clause must have a boolean result");
...@@ -575,7 +498,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence) ...@@ -575,7 +498,7 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
n->val.type = T_Null; n->val.type = T_Null;
w->result = (Node *) n; w->result = (Node *) n;
} }
w->result = transformExpr(pstate, (Node *) w->result, precedence); w->result = transformExpr(pstate, w->result, precedence);
result = expr; result = expr;
break; break;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -449,7 +449,8 @@ setRuleCheckAsUser(Query *qry, Oid userid)
/* If there are sublinks, search for them and process their RTEs */ /* If there are sublinks, search for them and process their RTEs */
if (qry->hasSubLinks) 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.
This diff is collapsed.
This diff is collapsed.
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * 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 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 200009281 #define CATALOG_VERSION_NO 200010041
#endif #endif
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.
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.
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