mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +03:00
Hi!
INTERSECT and EXCEPT is available for postgresql-v6.4! The patch against v6.4 is included at the end of the current text (in uuencoded form!) I also included the text of my Master's Thesis. (a postscript version). I hope that you find something of it useful and would be happy if parts of it find their way into the PostgreSQL documentation project (If so, tell me, then I send the sources of the document!) The contents of the document are: -) The first chapter might be of less interest as it gives only an overview on SQL. -) The second chapter gives a description on much of PostgreSQL's features (like user defined types etc. and how to use these features) -) The third chapter starts with an overview of PostgreSQL's internal structure with focus on the stages a query has to pass (i.e. parser, planner/optimizer, executor). Then a detailed description of the implementation of the Having clause and the Intersect/Except logic is given. Originally I worked on v6.3.2 but never found time enough to prepare and post a patch. Now I applied the changes to v6.4 to get Intersect and Except working with the new version. Chapter 3 of my documentation deals with the changes against v6.3.2, so keep that in mind when comparing the parts of the code printed there with the patched sources of v6.4. Here are some remarks on the patch. There are some things that have still to be done but at the moment I don't have time to do them myself. (I'm doing my military service at the moment) Sorry for that :-( -) I used a rewrite technique for the implementation of the Except/Intersect logic which rewrites the query to a semantically equivalent query before it is handed to the rewrite system (for views, rules etc.), planner, executor etc. -) In v6.3.2 the types of the attributes of two select statements connected by the UNION keyword had to match 100%. In v6.4 the types only need to be familiar (i.e. int and float can be mixed). Since this feature did not exist when I worked on Intersect/Except it does not work correctly for Except/Intersect queries WHEN USED IN COMBINATION WITH UNIONS! (i.e. sometimes the wrong type is used for the resulting table. This is because until now the types of the attributes of the first select statement have been used for the resulting table. When Intersects and/or Excepts are used in combination with Unions it might happen, that the first select statement of the original query appears at another position in the query which will be executed. The reason for this is the technique used for the implementation of Except/Intersect which does a query rewrite!) NOTE: It is NOT broken for pure UNION queries and pure INTERSECT/EXCEPT queries!!! -) I had to add the field intersect_clause to some data structures but did not find time to implement printfuncs for the new field. This does NOT break the debug modes but when an Except/Intersect is used the query debug output will be the already rewritten query. -) Massive changes to the grammar rules for SELECT and INSERT statements have been necessary (see comments in gram.y and documentation for deatails) in order to be able to use mixed queries like (SELECT ... UNION (SELECT ... EXCEPT SELECT)) INTERSECT SELECT...; -) When using UNION/EXCEPT/INTERSECT you will get: NOTICE: equal: "Don't know if nodes of type xxx are equal". I did not have time to add comparsion support for all the needed nodes, but the default behaviour of the function equal met my requirements. I did not dare to supress this message! That's the reason why the regression test for union will fail: These messages are also included in the union.out file! -) Somebody of you changed the union_planner() function for v6.4 (I copied the targetlist to new_tlist and that was removed and replaced by a cleanup of the original targetlist). These chnages violated some having queries executed against views so I changed it back again. I did not have time to examine the differences between the two versions but now it works :-) If you want to find out, try the file queries/view_having.sql on both versions and compare the results . Two queries won't produce a correct result with your version. regards Stefan
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.35 1998/09/09 03:48:01 vadim Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.36 1999/01/18 00:09:47 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -95,20 +95,26 @@ Plan *
|
||||
union_planner(Query *parse)
|
||||
{
|
||||
List *tlist = parse->targetList;
|
||||
int tlist_len = length(tlist);
|
||||
List *rangetable = parse->rtable;
|
||||
Plan *result_plan = (Plan *) NULL;
|
||||
Index rt_index;
|
||||
|
||||
/***S*H***/
|
||||
/* copy the original tlist, we will need the original one
|
||||
* for the AGG node later on */
|
||||
List *new_tlist = new_unsorted_tlist(tlist);
|
||||
|
||||
List *rangetable = parse->rtable;
|
||||
|
||||
Plan *result_plan = (Plan *) NULL;
|
||||
|
||||
Index rt_index;
|
||||
|
||||
if (parse->unionClause)
|
||||
{
|
||||
result_plan = (Plan *) plan_union_queries(parse);
|
||||
/* XXX do we need to do this? bjm 12/19/97 */
|
||||
tlist = preprocess_targetlist(tlist,
|
||||
parse->commandType,
|
||||
parse->resultRelation,
|
||||
parse->rtable);
|
||||
result_plan = (Plan *) plan_union_queries(parse);
|
||||
/* XXX do we need to do this? bjm 12/19/97 */
|
||||
tlist = preprocess_targetlist(tlist,
|
||||
parse->commandType,
|
||||
parse->resultRelation,
|
||||
parse->rtable);
|
||||
}
|
||||
else if ((rt_index =
|
||||
first_inherit_rt_entry(rangetable)) != -1)
|
||||
@ -116,47 +122,65 @@ union_planner(Query *parse)
|
||||
result_plan = (Plan *) plan_inherit_queries(parse, rt_index);
|
||||
/* XXX do we need to do this? bjm 12/19/97 */
|
||||
tlist = preprocess_targetlist(tlist,
|
||||
parse->commandType,
|
||||
parse->resultRelation,
|
||||
parse->rtable);
|
||||
parse->commandType,
|
||||
parse->resultRelation,
|
||||
parse->rtable);
|
||||
}
|
||||
else
|
||||
{
|
||||
List **vpm = NULL;
|
||||
|
||||
/*
|
||||
* check_having_qual_for_vars takes the havingQual and the tlist
|
||||
* as arguments and recursively scans the havingQual for VAR nodes
|
||||
* that are not contained in tlist yet. If so, it creates a new entry
|
||||
* and attaches it to the tlist. Latter, we use tlist_len to
|
||||
* truncate tlist - ie restore actual tlist...
|
||||
*/
|
||||
if (parse->hasAggs)
|
||||
List **vpm = NULL;
|
||||
|
||||
/***S*H***/
|
||||
/* This is only necessary if aggregates are in use in queries like:
|
||||
* SELECT sid
|
||||
* FROM part
|
||||
* GROUP BY sid
|
||||
* HAVING MIN(pid) > 1; (pid is used but never selected for!!!)
|
||||
* because the function 'query_planner' creates the plan for the lefttree
|
||||
* of the 'GROUP' node and returns only those attributes contained in 'tlist'.
|
||||
* The original 'tlist' contains only 'sid' here and that's why we have to
|
||||
* to extend it to attributes which are not selected but are used in the
|
||||
* havingQual. */
|
||||
|
||||
/* 'check_having_qual_for_vars' takes the havingQual and the actual 'tlist'
|
||||
* as arguments and recursively scans the havingQual for attributes
|
||||
* (VAR nodes) that are not contained in 'tlist' yet. If so, it creates
|
||||
* a new entry and attaches it to the list 'new_tlist' (consisting of the
|
||||
* VAR node and the RESDOM node as usual with tlists :-) ) */
|
||||
if (parse->hasAggs)
|
||||
{
|
||||
if (parse->havingQual != NULL)
|
||||
{
|
||||
if (parse->havingQual != NULL)
|
||||
tlist = check_having_qual_for_vars(parse->havingQual, tlist);
|
||||
new_tlist = check_having_qual_for_vars(parse->havingQual,new_tlist);
|
||||
}
|
||||
|
||||
tlist = preprocess_targetlist(tlist,
|
||||
parse->commandType,
|
||||
parse->resultRelation,
|
||||
parse->rtable);
|
||||
|
||||
if (parse->rtable != NULL)
|
||||
{
|
||||
vpm = (List **) palloc(length(parse->rtable) * sizeof(List *));
|
||||
memset(vpm, 0, length(parse->rtable) * sizeof(List *));
|
||||
}
|
||||
PlannerVarParam = lcons(vpm, PlannerVarParam);
|
||||
result_plan = query_planner(parse,
|
||||
parse->commandType,
|
||||
tlist,
|
||||
(List *) parse->qual);
|
||||
PlannerVarParam = lnext(PlannerVarParam);
|
||||
if (vpm != NULL)
|
||||
pfree(vpm);
|
||||
}
|
||||
|
||||
new_tlist = preprocess_targetlist(new_tlist,
|
||||
parse->commandType,
|
||||
parse->resultRelation,
|
||||
parse->rtable);
|
||||
|
||||
/* Here starts the original (pre having) code */
|
||||
tlist = preprocess_targetlist(tlist,
|
||||
parse->commandType,
|
||||
parse->resultRelation,
|
||||
parse->rtable);
|
||||
|
||||
if (parse->rtable != NULL)
|
||||
{
|
||||
vpm = (List **) palloc(length(parse->rtable) * sizeof(List *));
|
||||
memset(vpm, 0, length(parse->rtable) * sizeof(List *));
|
||||
}
|
||||
PlannerVarParam = lcons(vpm, PlannerVarParam);
|
||||
result_plan = query_planner(parse,
|
||||
parse->commandType,
|
||||
new_tlist,
|
||||
(List *) parse->qual);
|
||||
PlannerVarParam = lnext(PlannerVarParam);
|
||||
if (vpm != NULL)
|
||||
pfree(vpm);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* If we have a GROUP BY clause, insert a group node (with the
|
||||
* appropriate sort node.)
|
||||
@ -173,8 +197,10 @@ union_planner(Query *parse)
|
||||
*/
|
||||
tuplePerGroup = parse->hasAggs;
|
||||
|
||||
/***S*H***/
|
||||
/* Use 'new_tlist' instead of 'tlist' */
|
||||
result_plan =
|
||||
make_groupPlan(&tlist,
|
||||
make_groupPlan(&new_tlist,
|
||||
tuplePerGroup,
|
||||
parse->groupClause,
|
||||
result_plan);
|
||||
@ -185,6 +211,11 @@ union_planner(Query *parse)
|
||||
*/
|
||||
if (parse->hasAggs)
|
||||
{
|
||||
int old_length=0, new_length=0;
|
||||
|
||||
/* Create the AGG node but use 'tlist' not 'new_tlist' as target list because we
|
||||
* don't want the additional attributes (only used for the havingQual, see above)
|
||||
* to show up in the result */
|
||||
result_plan = (Plan *) make_agg(tlist, result_plan);
|
||||
|
||||
/*
|
||||
@ -192,78 +223,74 @@ union_planner(Query *parse)
|
||||
* the result tuple of the subplans.
|
||||
*/
|
||||
((Agg *) result_plan)->aggs =
|
||||
set_agg_tlist_references((Agg *) result_plan);
|
||||
set_agg_tlist_references((Agg *) result_plan);
|
||||
|
||||
|
||||
if (parse->havingQual != NULL)
|
||||
{
|
||||
List *clause;
|
||||
List **vpm = NULL;
|
||||
/***S*H***/
|
||||
if(parse->havingQual!=NULL)
|
||||
{
|
||||
List *clause;
|
||||
List **vpm = NULL;
|
||||
|
||||
|
||||
/* stuff copied from above to handle the use of attributes from outside
|
||||
* in subselects */
|
||||
|
||||
/*
|
||||
* Restore target list: get rid of Vars added for havingQual.
|
||||
* Assumption: tlist_len > 0...
|
||||
*/
|
||||
{
|
||||
List *l;
|
||||
int tlen = 0;
|
||||
if (parse->rtable != NULL)
|
||||
{
|
||||
vpm = (List **) palloc(length(parse->rtable) * sizeof(List *));
|
||||
memset(vpm, 0, length(parse->rtable) * sizeof(List *));
|
||||
}
|
||||
PlannerVarParam = lcons(vpm, PlannerVarParam);
|
||||
|
||||
|
||||
/* convert the havingQual to conjunctive normal form (cnf) */
|
||||
(List *) parse->havingQual=cnfify((Expr *)(Node *) parse->havingQual,true);
|
||||
|
||||
/* There is a subselect in the havingQual, so we have to process it
|
||||
* using the same function as for a subselect in 'where' */
|
||||
if (parse->hasSubLinks)
|
||||
{
|
||||
(List *) parse->havingQual =
|
||||
(List *) SS_process_sublinks((Node *) parse->havingQual);
|
||||
}
|
||||
|
||||
|
||||
/* Calculate the opfids from the opnos (=select the correct functions for
|
||||
* the used VAR datatypes) */
|
||||
(List *) parse->havingQual=fix_opids((List *) parse->havingQual);
|
||||
|
||||
((Agg *) result_plan)->plan.qual=(List *) parse->havingQual;
|
||||
|
||||
/* Check every clause of the havingQual for aggregates used and append
|
||||
* them to result_plan->aggs */
|
||||
foreach(clause, ((Agg *) result_plan)->plan.qual)
|
||||
{
|
||||
/* Make sure there are aggregates in the havingQual
|
||||
* if so, the list must be longer after check_having_qual_for_aggs */
|
||||
old_length=length(((Agg *) result_plan)->aggs);
|
||||
|
||||
foreach (l, ((Agg *) result_plan)->plan.targetlist)
|
||||
{
|
||||
if (++tlen == tlist_len)
|
||||
break;
|
||||
}
|
||||
lnext(l) = NIL;
|
||||
}
|
||||
|
||||
/*
|
||||
* stuff copied from above to handle the use of attributes
|
||||
* from outside in subselects
|
||||
*/
|
||||
|
||||
if (parse->rtable != NULL)
|
||||
{
|
||||
vpm = (List **) palloc(length(parse->rtable) * sizeof(List *));
|
||||
memset(vpm, 0, length(parse->rtable) * sizeof(List *));
|
||||
}
|
||||
PlannerVarParam = lcons(vpm, PlannerVarParam);
|
||||
|
||||
/*
|
||||
* There is a subselect in the havingQual, so we have to
|
||||
* process it using the same function as for a subselect in
|
||||
* 'where'
|
||||
*/
|
||||
if (parse->hasSubLinks)
|
||||
parse->havingQual = SS_process_sublinks((Node *) parse->havingQual);
|
||||
|
||||
/* convert the havingQual to conjunctive normal form (cnf) */
|
||||
parse->havingQual = (Node *) cnfify((Expr *) (Node *) parse->havingQual, true);
|
||||
|
||||
/*
|
||||
* Calculate the opfids from the opnos (=select the correct
|
||||
* functions for the used VAR datatypes)
|
||||
*/
|
||||
parse->havingQual = (Node *) fix_opids((List *) parse->havingQual);
|
||||
|
||||
((Agg *) result_plan)->plan.qual = (List *) parse->havingQual;
|
||||
|
||||
/*
|
||||
* Check every clause of the havingQual for aggregates used
|
||||
* and append them to result_plan->aggs
|
||||
*/
|
||||
foreach(clause, ((Agg *) result_plan)->plan.qual)
|
||||
{
|
||||
((Agg *) result_plan)->aggs = nconc(((Agg *) result_plan)->aggs,
|
||||
check_having_qual_for_aggs((Node *) lfirst(clause),
|
||||
((Agg *) result_plan)->plan.lefttree->targetlist,
|
||||
((List *) parse->groupClause)));
|
||||
}
|
||||
PlannerVarParam = lnext(PlannerVarParam);
|
||||
if (vpm != NULL)
|
||||
pfree(vpm);
|
||||
}
|
||||
}
|
||||
((Agg *) result_plan)->aggs = nconc(((Agg *) result_plan)->aggs,
|
||||
check_having_qual_for_aggs((Node *) lfirst(clause),
|
||||
((Agg *) result_plan)->plan.lefttree->targetlist,
|
||||
((List *) parse->groupClause)));
|
||||
|
||||
/* Have a look at the length of the returned list. If there is no
|
||||
* difference, no aggregates have been found and that means, that
|
||||
* the Qual belongs to the where clause */
|
||||
if (((new_length=length(((Agg *) result_plan)->aggs)) == old_length) ||
|
||||
(new_length == 0))
|
||||
{
|
||||
elog(ERROR,"This could have been done in a where clause!!");
|
||||
return (Plan *)NIL;
|
||||
}
|
||||
}
|
||||
PlannerVarParam = lnext(PlannerVarParam);
|
||||
if (vpm != NULL)
|
||||
pfree(vpm);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* For now, before we hand back the plan, check to see if there is a
|
||||
* user-specified sort that needs to be done. Eventually, this will
|
||||
@ -277,14 +304,14 @@ union_planner(Query *parse)
|
||||
{
|
||||
Plan *sortplan = make_sortplan(tlist, parse->sortClause, result_plan);
|
||||
|
||||
return (Plan *) make_unique(tlist, sortplan, parse->uniqueFlag);
|
||||
return ((Plan *) make_unique(tlist, sortplan, parse->uniqueFlag));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (parse->sortClause)
|
||||
return make_sortplan(tlist, parse->sortClause, result_plan);
|
||||
return (make_sortplan(tlist, parse->sortClause, result_plan));
|
||||
else
|
||||
return (Plan *) result_plan;
|
||||
return ((Plan *) result_plan);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user