1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-23 03:21:12 +03:00

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!
This commit is contained in:
Tom Lane
2000-10-05 19:11:39 +00:00
parent 5292637f52
commit 05e3d0ee86
51 changed files with 2621 additions and 1935 deletions

View File

@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.123 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.124 2000/10/05 19:11:27 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -176,7 +176,6 @@ _copyAppend(Append *from)
* ----------------
*/
Node_Copy(from, newnode, appendplans);
Node_Copy(from, newnode, unionrtables);
newnode->inheritrelid = from->inheritrelid;
Node_Copy(from, newnode, inheritrtable);
@ -565,6 +564,33 @@ _copyUnique(Unique *from)
return newnode;
}
/* ----------------
* _copySetOp
* ----------------
*/
static SetOp *
_copySetOp(SetOp *from)
{
SetOp *newnode = makeNode(SetOp);
/* ----------------
* copy node superclass fields
* ----------------
*/
CopyPlanFields((Plan *) from, (Plan *) newnode);
/* ----------------
* copy remainder of node
* ----------------
*/
newnode->cmd = from->cmd;
newnode->numCols = from->numCols;
newnode->dupColIdx = palloc(from->numCols * sizeof(AttrNumber));
memcpy(newnode->dupColIdx, from->dupColIdx, from->numCols * sizeof(AttrNumber));
newnode->flagColIdx = from->flagColIdx;
return newnode;
}
/* ----------------
* _copyHash
@ -1696,28 +1722,26 @@ _copyQuery(Query *from)
newnode->isPortal = from->isPortal;
newnode->isBinary = from->isBinary;
newnode->isTemp = from->isTemp;
newnode->unionall = from->unionall;
newnode->hasAggs = from->hasAggs;
newnode->hasSubLinks = from->hasSubLinks;
Node_Copy(from, newnode, rtable);
Node_Copy(from, newnode, jointree);
Node_Copy(from, newnode, targetList);
newnode->rowMarks = listCopy(from->rowMarks);
Node_Copy(from, newnode, distinctClause);
Node_Copy(from, newnode, sortClause);
Node_Copy(from, newnode, targetList);
Node_Copy(from, newnode, groupClause);
Node_Copy(from, newnode, havingQual);
/* why is intersectClause missing? */
Node_Copy(from, newnode, unionClause);
Node_Copy(from, newnode, distinctClause);
Node_Copy(from, newnode, sortClause);
Node_Copy(from, newnode, limitOffset);
Node_Copy(from, newnode, limitCount);
Node_Copy(from, newnode, setOperations);
/*
* We do not copy the planner internal fields: base_rel_list,
* join_rel_list, equi_key_list, query_pathkeys. Not entirely clear if
@ -1734,17 +1758,9 @@ _copyInsertStmt(InsertStmt *from)
if (from->relname)
newnode->relname = pstrdup(from->relname);
Node_Copy(from, newnode, distinctClause);
Node_Copy(from, newnode, cols);
Node_Copy(from, newnode, targetList);
Node_Copy(from, newnode, fromClause);
Node_Copy(from, newnode, whereClause);
Node_Copy(from, newnode, groupClause);
Node_Copy(from, newnode, havingClause);
Node_Copy(from, newnode, unionClause);
newnode->unionall = from->unionall;
Node_Copy(from, newnode, intersectClause);
Node_Copy(from, newnode, forUpdate);
Node_Copy(from, newnode, selectStmt);
return newnode;
}
@ -1790,15 +1806,11 @@ _copySelectStmt(SelectStmt *from)
Node_Copy(from, newnode, whereClause);
Node_Copy(from, newnode, groupClause);
Node_Copy(from, newnode, havingClause);
Node_Copy(from, newnode, intersectClause);
Node_Copy(from, newnode, exceptClause);
Node_Copy(from, newnode, unionClause);
Node_Copy(from, newnode, sortClause);
if (from->portalname)
newnode->portalname = pstrdup(from->portalname);
newnode->binary = from->binary;
newnode->istemp = from->istemp;
newnode->unionall = from->unionall;
Node_Copy(from, newnode, limitOffset);
Node_Copy(from, newnode, limitCount);
Node_Copy(from, newnode, forUpdate);
@ -1806,6 +1818,20 @@ _copySelectStmt(SelectStmt *from)
return newnode;
}
static SetOperationStmt *
_copySetOperationStmt(SetOperationStmt *from)
{
SetOperationStmt *newnode = makeNode(SetOperationStmt);
newnode->op = from->op;
newnode->all = from->all;
Node_Copy(from, newnode, larg);
Node_Copy(from, newnode, rarg);
newnode->colTypes = listCopy(from->colTypes);
return newnode;
}
static AlterTableStmt *
_copyAlterTableStmt(AlterTableStmt *from)
{
@ -2553,6 +2579,9 @@ copyObject(void *from)
case T_Unique:
retval = _copyUnique(from);
break;
case T_SetOp:
retval = _copySetOp(from);
break;
case T_Hash:
retval = _copyHash(from);
break;
@ -2700,6 +2729,9 @@ copyObject(void *from)
case T_SelectStmt:
retval = _copySelectStmt(from);
break;
case T_SetOperationStmt:
retval = _copySetOperationStmt(from);
break;
case T_AlterTableStmt:
retval = _copyAlterTableStmt(from);
break;

View File

@ -20,7 +20,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.74 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.75 2000/10/05 19:11:27 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -33,8 +33,6 @@
#include "utils/datum.h"
static bool equali(List *a, List *b);
/* Macro for comparing string fields that might be NULL */
#define equalstr(a, b) \
(((a) != NULL && (b) != NULL) ? (strcmp(a, b) == 0) : (a) == (b))
@ -600,8 +598,6 @@ _equalQuery(Query *a, Query *b)
return false;
if (a->isTemp != b->isTemp)
return false;
if (a->unionall != b->unionall)
return false;
if (a->hasAggs != b->hasAggs)
return false;
if (a->hasSubLinks != b->hasSubLinks)
@ -610,26 +606,24 @@ _equalQuery(Query *a, Query *b)
return false;
if (!equal(a->jointree, b->jointree))
return false;
if (!equal(a->targetList, b->targetList))
return false;
if (!equali(a->rowMarks, b->rowMarks))
return false;
if (!equal(a->distinctClause, b->distinctClause))
return false;
if (!equal(a->sortClause, b->sortClause))
if (!equal(a->targetList, b->targetList))
return false;
if (!equal(a->groupClause, b->groupClause))
return false;
if (!equal(a->havingQual, b->havingQual))
return false;
if (!equal(a->intersectClause, b->intersectClause))
if (!equal(a->distinctClause, b->distinctClause))
return false;
if (!equal(a->unionClause, b->unionClause))
if (!equal(a->sortClause, b->sortClause))
return false;
if (!equal(a->limitOffset, b->limitOffset))
return false;
if (!equal(a->limitCount, b->limitCount))
return false;
if (!equal(a->setOperations, b->setOperations))
return false;
/*
* We do not check the internal-to-the-planner fields: base_rel_list,
@ -645,27 +639,11 @@ _equalInsertStmt(InsertStmt *a, InsertStmt *b)
{
if (!equalstr(a->relname, b->relname))
return false;
if (!equal(a->distinctClause, b->distinctClause))
return false;
if (!equal(a->cols, b->cols))
return false;
if (!equal(a->targetList, b->targetList))
return false;
if (!equal(a->fromClause, b->fromClause))
return false;
if (!equal(a->whereClause, b->whereClause))
return false;
if (!equal(a->groupClause, b->groupClause))
return false;
if (!equal(a->havingClause, b->havingClause))
return false;
if (!equal(a->unionClause, b->unionClause))
return false;
if (a->unionall != b->unionall)
return false;
if (!equal(a->intersectClause, b->intersectClause))
return false;
if (!equal(a->forUpdate, b->forUpdate))
if (!equal(a->selectStmt, b->selectStmt))
return false;
return true;
@ -718,12 +696,6 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return false;
if (!equal(a->havingClause, b->havingClause))
return false;
if (!equal(a->intersectClause, b->intersectClause))
return false;
if (!equal(a->exceptClause, b->exceptClause))
return false;
if (!equal(a->unionClause, b->unionClause))
return false;
if (!equal(a->sortClause, b->sortClause))
return false;
if (!equalstr(a->portalname, b->portalname))
@ -732,8 +704,6 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return false;
if (a->istemp != b->istemp)
return false;
if (a->unionall != b->unionall)
return false;
if (!equal(a->limitOffset, b->limitOffset))
return false;
if (!equal(a->limitCount, b->limitCount))
@ -744,6 +714,23 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
return true;
}
static bool
_equalSetOperationStmt(SetOperationStmt *a, SetOperationStmt *b)
{
if (a->op != b->op)
return false;
if (a->all != b->all)
return false;
if (!equal(a->larg, b->larg))
return false;
if (!equal(a->rarg, b->rarg))
return false;
if (!equali(a->colTypes, b->colTypes))
return false;
return true;
}
static bool
_equalAlterTableStmt(AlterTableStmt *a, AlterTableStmt *b)
{
@ -1929,6 +1916,9 @@ equal(void *a, void *b)
case T_SelectStmt:
retval = _equalSelectStmt(a, b);
break;
case T_SetOperationStmt:
retval = _equalSetOperationStmt(a, b);
break;
case T_AlterTableStmt:
retval = _equalAlterTableStmt(a, b);
break;
@ -2159,25 +2149,3 @@ equal(void *a, void *b)
return retval;
}
/*
* equali
* compares two lists of integers
*/
static bool
equali(List *a, List *b)
{
List *l;
foreach(l, a)
{
if (b == NIL)
return false;
if (lfirsti(l) != lfirsti(b))
return false;
b = lnext(b);
}
if (b != NIL)
return false;
return true;
}

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/list.c,v 1.34 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/list.c,v 1.35 2000/10/05 19:11:27 tgl Exp $
*
* NOTES
* XXX a few of the following functions are duplicated to handle
@ -236,11 +236,33 @@ freeList(List *list)
}
}
/*
* equali
* compares two lists of integers
*/
bool
equali(List *list1, List *list2)
{
List *l;
foreach(l, list1)
{
if (list2 == NIL)
return false;
if (lfirsti(l) != lfirsti(list2))
return false;
list2 = lnext(list2);
}
if (list2 != NIL)
return false;
return true;
}
/*
* sameseti
*
* Returns t if two integer lists contain the same elements
* (but unlike equal(), they need not be in the same order)
* (but unlike equali(), they need not be in the same order)
*
* Caution: this routine could be fooled if list1 contains
* duplicate elements. It is intended to be used on lists

View File

@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.127 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.128 2000/10/05 19:11:27 tgl Exp $
*
* NOTES
* Every (plan) node in POSTGRES has an associated "out" routine which
@ -147,6 +147,7 @@ _outIndexStmt(StringInfo str, IndexStmt *node)
static void
_outSelectStmt(StringInfo str, SelectStmt *node)
{
/* XXX this is pretty durn incomplete */
appendStringInfo(str, "SELECT :where ");
_outNode(str, node->whereClause);
}
@ -258,11 +259,10 @@ _outQuery(StringInfo str, Query *node)
_outToken(str, node->into);
appendStringInfo(str, " :isPortal %s :isBinary %s :isTemp %s"
" :unionall %s :hasAggs %s :hasSubLinks %s :rtable ",
" :hasAggs %s :hasSubLinks %s :rtable ",
node->isPortal ? "true" : "false",
node->isBinary ? "true" : "false",
node->isTemp ? "true" : "false",
node->unionall ? "true" : "false",
node->hasAggs ? "true" : "false",
node->hasSubLinks ? "true" : "false");
_outNode(str, node->rtable);
@ -270,17 +270,11 @@ _outQuery(StringInfo str, Query *node)
appendStringInfo(str, " :jointree ");
_outNode(str, node->jointree);
appendStringInfo(str, " :targetList ");
_outNode(str, node->targetList);
appendStringInfo(str, " :rowMarks ");
_outIntList(str, node->rowMarks);
appendStringInfo(str, " :distinctClause ");
_outNode(str, node->distinctClause);
appendStringInfo(str, " :sortClause ");
_outNode(str, node->sortClause);
appendStringInfo(str, " :targetList ");
_outNode(str, node->targetList);
appendStringInfo(str, " :groupClause ");
_outNode(str, node->groupClause);
@ -288,17 +282,20 @@ _outQuery(StringInfo str, Query *node)
appendStringInfo(str, " :havingQual ");
_outNode(str, node->havingQual);
appendStringInfo(str, " :intersectClause ");
_outNode(str, node->intersectClause);
appendStringInfo(str, " :distinctClause ");
_outNode(str, node->distinctClause);
appendStringInfo(str, " :unionClause ");
_outNode(str, node->unionClause);
appendStringInfo(str, " :sortClause ");
_outNode(str, node->sortClause);
appendStringInfo(str, " :limitOffset ");
_outNode(str, node->limitOffset);
appendStringInfo(str, " :limitCount ");
_outNode(str, node->limitCount);
appendStringInfo(str, " :setOperations ");
_outNode(str, node->setOperations);
}
static void
@ -315,6 +312,19 @@ _outGroupClause(StringInfo str, GroupClause *node)
node->tleSortGroupRef, node->sortop);
}
static void
_outSetOperationStmt(StringInfo str, SetOperationStmt *node)
{
appendStringInfo(str, " SETOPERATIONSTMT :op %d :all %s :larg ",
(int) node->op,
node->all ? "true" : "false");
_outNode(str, node->larg);
appendStringInfo(str, " :rarg ");
_outNode(str, node->rarg);
appendStringInfo(str, " :colTypes ");
_outIntList(str, node->colTypes);
}
/*
* print the basic stuff of all nodes that inherit from Plan
*/
@ -384,11 +394,7 @@ _outAppend(StringInfo str, Append *node)
appendStringInfo(str, " :appendplans ");
_outNode(str, node->appendplans);
appendStringInfo(str, " :unionrtables ");
_outNode(str, node->unionrtables);
appendStringInfo(str,
" :inheritrelid %u :inheritrtable ",
appendStringInfo(str, " :inheritrelid %u :inheritrtable ",
node->inheritrelid);
_outNode(str, node->inheritrtable);
}
@ -601,6 +607,22 @@ _outUnique(StringInfo str, Unique *node)
appendStringInfo(str, "%d ", (int) node->uniqColIdx[i]);
}
static void
_outSetOp(StringInfo str, SetOp *node)
{
int i;
appendStringInfo(str, " SETOP ");
_outPlanInfo(str, (Plan *) node);
appendStringInfo(str, " :cmd %d :numCols %d :dupColIdx ",
(int) node->cmd, node->numCols);
for (i = 0; i < node->numCols; i++)
appendStringInfo(str, "%d ", (int) node->dupColIdx[i]);
appendStringInfo(str, " :flagColIdx %d ",
(int) node->flagColIdx);
}
/*
* Hash is a subclass of Plan
*/
@ -1480,6 +1502,9 @@ _outNode(StringInfo str, void *obj)
case T_GroupClause:
_outGroupClause(str, obj);
break;
case T_SetOperationStmt:
_outSetOperationStmt(str, obj);
break;
case T_Plan:
_outPlan(str, obj);
break;
@ -1531,6 +1556,9 @@ _outNode(StringInfo str, void *obj)
case T_Unique:
_outUnique(str, obj);
break;
case T_SetOp:
_outSetOp(str, obj);
break;
case T_Hash:
_outHash(str, obj);
break;

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/print.c,v 1.42 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/print.c,v 1.43 2000/10/05 19:11:27 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
@ -338,6 +338,9 @@ plannode_type(Plan *p)
case T_Unique:
return "UNIQUE";
break;
case T_SetOp:
return "SETOP";
break;
case T_Hash:
return "HASH";
break;

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.97 2000/09/29 18:21:29 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.98 2000/10/05 19:11:27 tgl Exp $
*
* NOTES
* Most of the read functions for plan nodes are tested. (In fact, they
@ -109,10 +109,6 @@ _readQuery()
token = lsptok(NULL, &length); /* get isTemp */
local_node->isTemp = (token[0] == 't') ? true : false;
token = lsptok(NULL, &length); /* skip :unionall */
token = lsptok(NULL, &length); /* get unionall */
local_node->unionall = (token[0] == 't') ? true : false;
token = lsptok(NULL, &length); /* skip the :hasAggs */
token = lsptok(NULL, &length); /* get hasAggs */
local_node->hasAggs = (token[0] == 't') ? true : false;
@ -127,17 +123,11 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :jointree */
local_node->jointree = nodeRead(true);
token = lsptok(NULL, &length); /* skip :targetlist */
local_node->targetList = nodeRead(true);
token = lsptok(NULL, &length); /* skip :rowMarks */
local_node->rowMarks = toIntList(nodeRead(true));
token = lsptok(NULL, &length); /* skip :distinctClause */
local_node->distinctClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :sortClause */
local_node->sortClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :targetlist */
local_node->targetList = nodeRead(true);
token = lsptok(NULL, &length); /* skip :groupClause */
local_node->groupClause = nodeRead(true);
@ -145,11 +135,11 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :havingQual */
local_node->havingQual = nodeRead(true);
token = lsptok(NULL, &length); /* skip :intersectClause */
local_node->intersectClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :distinctClause */
local_node->distinctClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :unionClause */
local_node->unionClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :sortClause */
local_node->sortClause = nodeRead(true);
token = lsptok(NULL, &length); /* skip :limitOffset */
local_node->limitOffset = nodeRead(true);
@ -157,6 +147,9 @@ _readQuery()
token = lsptok(NULL, &length); /* skip :limitCount */
local_node->limitCount = nodeRead(true);
token = lsptok(NULL, &length); /* skip :setOperations */
local_node->setOperations = nodeRead(true);
return local_node;
}
@ -208,6 +201,39 @@ _readGroupClause()
return local_node;
}
/* ----------------
* _readSetOperationStmt
* ----------------
*/
static SetOperationStmt *
_readSetOperationStmt()
{
SetOperationStmt *local_node;
char *token;
int length;
local_node = makeNode(SetOperationStmt);
token = lsptok(NULL, &length); /* eat :op */
token = lsptok(NULL, &length); /* get op */
local_node->op = (SetOperation) atoi(token);
token = lsptok(NULL, &length); /* eat :all */
token = lsptok(NULL, &length); /* get all */
local_node->all = (token[0] == 't') ? true : false;
token = lsptok(NULL, &length); /* eat :larg */
local_node->larg = nodeRead(true); /* get larg */
token = lsptok(NULL, &length); /* eat :rarg */
local_node->rarg = nodeRead(true); /* get rarg */
token = lsptok(NULL, &length); /* eat :colTypes */
local_node->colTypes = toIntList(nodeRead(true));
return local_node;
}
/* ----------------
* _getPlan
* ----------------
@ -322,9 +348,6 @@ _readAppend()
token = lsptok(NULL, &length); /* eat :appendplans */
local_node->appendplans = nodeRead(true); /* now read it */
token = lsptok(NULL, &length); /* eat :unionrtables */
local_node->unionrtables = nodeRead(true); /* now read it */
token = lsptok(NULL, &length); /* eat :inheritrelid */
token = lsptok(NULL, &length); /* get inheritrelid */
local_node->inheritrelid = strtoul(token, NULL, 10);
@ -1995,6 +2018,8 @@ parsePlanString(void)
return_value = _readSortClause();
else if (length == 11 && strncmp(token, "GROUPCLAUSE", length) == 0)
return_value = _readGroupClause();
else if (length == 16 && strncmp(token, "SETOPERATIONSTMT", length) == 0)
return_value = _readSetOperationStmt();
else if (length == 4 && strncmp(token, "CASE", length) == 0)
return_value = _readCaseExpr();
else if (length == 4 && strncmp(token, "WHEN", length) == 0)