postgresql/src/backend/rewrite/rewriteManip.c

1079 lines
23 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* rewriteManip.c
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.40 1999/08/25 23:21:43 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "optimizer/clauses.h"
1999-07-16 07:00:38 +02:00
#include "parser/parsetree.h"
#include "parser/parse_clause.h"
1999-07-16 07:00:38 +02:00
#include "rewrite/rewriteManip.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
static void ResolveNew(RewriteInfo *info, List *targetlist,
Node **node, int sublevels_up);
/*
* OffsetVarnodes -
*/
void
OffsetVarNodes(Node *node, int offset, int sublevels_up)
{
if (node == NULL)
return;
1999-05-25 18:15:34 +02:00
switch (nodeTag(node))
{
case T_TargetEntry:
{
1999-05-25 18:15:34 +02:00
TargetEntry *tle = (TargetEntry *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (tle->expr),
offset,
sublevels_up);
}
break;
1999-01-24 01:28:37 +01:00
case T_Aggref:
{
1999-05-25 18:15:34 +02:00
Aggref *aggref = (Aggref *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (aggref->target),
offset,
sublevels_up);
}
break;
case T_GroupClause:
break;
case T_Expr:
{
1999-05-25 18:15:34 +02:00
Expr *exp = (Expr *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->args),
offset,
sublevels_up);
}
break;
case T_Iter:
{
1999-05-25 18:15:34 +02:00
Iter *iter = (Iter *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (iter->iterexpr),
offset,
sublevels_up);
}
break;
case T_ArrayRef:
{
1999-05-25 18:15:34 +02:00
ArrayRef *ref = (ArrayRef *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (ref->refupperindexpr),
offset,
sublevels_up);
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (ref->reflowerindexpr),
offset,
sublevels_up);
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (ref->refexpr),
offset,
sublevels_up);
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (ref->refassgnexpr),
offset,
sublevels_up);
}
break;
case T_Var:
{
1999-05-25 18:15:34 +02:00
Var *var = (Var *) node;
1999-05-25 18:15:34 +02:00
if (var->varlevelsup == sublevels_up)
{
var->varno += offset;
var->varnoold += offset;
}
}
break;
case T_Param:
break;
case T_Const:
break;
case T_List:
{
1999-05-25 18:15:34 +02:00
List *l;
1999-05-25 18:15:34 +02:00
foreach(l, (List *) node)
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) lfirst(l),
offset,
sublevels_up);
}
break;
case T_SubLink:
{
1999-05-25 18:15:34 +02:00
SubLink *sub = (SubLink *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (sub->lefthand),
offset,
sublevels_up);
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (sub->subselect),
offset,
sublevels_up + 1);
}
break;
case T_Query:
{
1999-05-25 18:15:34 +02:00
Query *qry = (Query *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (qry->targetList),
offset,
sublevels_up);
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (qry->qual),
offset,
sublevels_up);
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (qry->havingQual),
offset,
sublevels_up);
}
break;
case T_CaseExpr:
{
1999-05-25 18:15:34 +02:00
CaseExpr *exp = (CaseExpr *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->args),
offset,
sublevels_up);
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->defresult),
offset,
sublevels_up);
}
break;
case T_CaseWhen:
{
1999-05-25 18:15:34 +02:00
CaseWhen *exp = (CaseWhen *) node;
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->expr),
offset,
sublevels_up);
OffsetVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->result),
offset,
sublevels_up);
}
break;
default:
elog(NOTICE, "unknown node tag %d in OffsetVarNodes()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
}
}
/*
* ChangeVarNodes -
*/
void
ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
{
if (node == NULL)
return;
1999-05-25 18:15:34 +02:00
switch (nodeTag(node))
{
case T_TargetEntry:
{
1999-05-25 18:15:34 +02:00
TargetEntry *tle = (TargetEntry *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (tle->expr),
rt_index,
new_index,
sublevels_up);
}
break;
1999-01-24 01:28:37 +01:00
case T_Aggref:
{
1999-05-25 18:15:34 +02:00
Aggref *aggref = (Aggref *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (aggref->target),
rt_index,
new_index,
sublevels_up);
}
break;
case T_GroupClause:
break;
case T_Expr:
{
1999-05-25 18:15:34 +02:00
Expr *exp = (Expr *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->args),
rt_index,
new_index,
sublevels_up);
}
break;
case T_Iter:
{
1999-05-25 18:15:34 +02:00
Iter *iter = (Iter *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (iter->iterexpr),
rt_index,
new_index,
sublevels_up);
}
break;
case T_ArrayRef:
{
1999-05-25 18:15:34 +02:00
ArrayRef *ref = (ArrayRef *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (ref->refupperindexpr),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (ref->reflowerindexpr),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (ref->refexpr),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (ref->refassgnexpr),
rt_index,
new_index,
sublevels_up);
}
break;
case T_Var:
{
1999-05-25 18:15:34 +02:00
Var *var = (Var *) node;
if (var->varlevelsup == sublevels_up &&
1999-05-25 18:15:34 +02:00
var->varno == rt_index)
{
var->varno = new_index;
var->varnoold = new_index;
}
}
break;
case T_Param:
break;
case T_Const:
break;
case T_List:
{
1999-05-25 18:15:34 +02:00
List *l;
1999-05-25 18:15:34 +02:00
foreach(l, (List *) node)
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) lfirst(l),
rt_index,
new_index,
sublevels_up);
1998-01-21 05:24:46 +01:00
}
break;
1998-01-21 05:24:46 +01:00
case T_SubLink:
{
1999-05-25 18:15:34 +02:00
SubLink *sub = (SubLink *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (sub->lefthand),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (sub->subselect),
rt_index,
new_index,
sublevels_up + 1);
}
break;
case T_Query:
{
1999-05-25 18:15:34 +02:00
Query *qry = (Query *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (qry->targetList),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (qry->qual),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (qry->havingQual),
rt_index,
new_index,
sublevels_up);
}
break;
case T_CaseExpr:
{
1999-05-25 18:15:34 +02:00
CaseExpr *exp = (CaseExpr *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->args),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->defresult),
rt_index,
new_index,
sublevels_up);
}
break;
case T_CaseWhen:
{
1999-05-25 18:15:34 +02:00
CaseWhen *exp = (CaseWhen *) node;
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->expr),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
1999-05-25 18:15:34 +02:00
(Node *) (exp->result),
rt_index,
new_index,
sublevels_up);
}
break;
default:
elog(NOTICE, "unknown node tag %d in ChangeVarNodes()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
}
}
void
AddQual(Query *parsetree, Node *qual)
{
Node *copy,
*old;
if (qual == NULL)
return;
/* INTERSECT want's the original, but we need to copy - Jan */
/* copy = qual; */
copy = copyObject(qual);
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
1999-01-18 01:10:17 +01:00
old = parsetree->qual;
if (old == NULL)
parsetree->qual = copy;
else
parsetree->qual = (Node *) make_andclause(makeList(parsetree->qual, copy, -1));
}
/* Adds the given havingQual to the one already contained in the parsetree just as
* AddQual does for the normal 'where' qual */
void
AddHavingQual(Query *parsetree, Node *havingQual)
{
Node *copy,
*old;
if (havingQual == NULL)
return;
/* INTERSECT want's the original, but we need to copy - Jan */
/* copy = havingQual; */
copy = copyObject(havingQual);
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
1999-01-18 01:10:17 +01:00
old = parsetree->havingQual;
if (old == NULL)
parsetree->havingQual = copy;
else
parsetree->havingQual = (Node *) make_andclause(makeList(parsetree->havingQual, copy, -1));
}
#ifdef NOT_USED
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
1999-01-18 01:10:17 +01:00
void
AddNotHavingQual(Query *parsetree, Node *havingQual)
{
Node *copy;
if (havingQual == NULL)
return;
/* INTERSECT want's the original, but we need to copy - Jan */
/* copy = (Node *) make_notclause((Expr *)havingQual); */
1999-05-25 18:15:34 +02:00
copy = (Node *) make_notclause((Expr *) copyObject(havingQual));
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
1999-01-18 01:10:17 +01:00
AddHavingQual(parsetree, copy);
}
#endif
void
AddNotQual(Query *parsetree, Node *qual)
{
Node *copy;
if (qual == NULL)
return;
/* INTERSECT want's the original, but we need to copy - Jan */
/* copy = (Node *) make_notclause((Expr *)qual); */
1999-05-25 18:15:34 +02:00
copy = (Node *) make_notclause((Expr *) copyObject(qual));
AddQual(parsetree, copy);
}
/*
* Add all expressions used by the given GroupClause list to the
* parsetree's targetlist and groupclause list.
*
* tlist is the old targetlist associated with the input groupclauses.
*
* XXX shouldn't we be checking to see if there are already matching
* entries in parsetree->targetlist?
*/
void
AddGroupClause(Query *parsetree, List *group_by, List *tlist)
{
1999-05-25 18:15:34 +02:00
List *l;
1999-05-25 18:15:34 +02:00
foreach(l, group_by)
{
GroupClause *groupclause = (GroupClause *) copyObject(lfirst(l));
Index refnumber = groupclause->tleSortGroupRef;
TargetEntry *tle = NULL;
List *tl;
/* Find and copy the groupclause's TLE in the old tlist */
foreach(tl, tlist)
{
if (((TargetEntry *) lfirst(tl))->resdom->ressortgroupref ==
refnumber)
{
1999-05-25 18:15:34 +02:00
tle = (TargetEntry *) copyObject(lfirst(tl));
break;
}
}
if (tle == NULL)
elog(ERROR, "AddGroupClause(): GROUP BY entry not found in rules targetlist");
/* The ressortgroupref number in the old tlist might be already
* taken in the new tlist, so force assignment of a new number.
*/
tle->resdom->ressortgroupref = 0;
groupclause->tleSortGroupRef =
assignSortGroupRef(tle, parsetree->targetList);
/* Also need to set the resno and mark it resjunk. */
tle->resdom->resno = length(parsetree->targetList) + 1;
tle->resdom->resjunk = true;
1999-05-25 18:15:34 +02:00
parsetree->targetList = lappend(parsetree->targetList, tle);
parsetree->groupClause = lappend(parsetree->groupClause, groupclause);
}
}
static Node *
make_null(Oid type)
{
Const *c = makeNode(Const);
c->consttype = type;
c->constlen = get_typlen(type);
c->constvalue = PointerGetDatum(NULL);
c->constisnull = true;
c->constbyval = get_typbyval(type);
return (Node *) c;
}
#ifdef NOT_USED
void
FixResdomTypes(List *tlist)
{
List *i;
foreach(i, tlist)
{
TargetEntry *tle = lfirst(i);
if (nodeTag(tle->expr) == T_Var)
{
Var *var = (Var *) tle->expr;
tle->resdom->restype = var->vartype;
tle->resdom->restypmod = var->vartypmod;
}
}
}
1999-05-25 18:15:34 +02:00
#endif
static Node *
FindMatchingNew(List *tlist, int attno)
{
List *i;
foreach(i, tlist)
{
TargetEntry *tle = lfirst(i);
if (tle->resdom->resno == attno)
1998-09-01 05:29:17 +02:00
return tle->expr;
}
return NULL;
}
static Node *
FindMatchingTLEntry(List *tlist, char *e_attname)
{
List *i;
foreach(i, tlist)
{
TargetEntry *tle = lfirst(i);
char *resname;
resname = tle->resdom->resname;
if (!strcmp(e_attname, resname))
1998-09-01 05:29:17 +02:00
return tle->expr;
}
return NULL;
}
static void
1998-01-21 05:24:46 +01:00
ResolveNew(RewriteInfo *info, List *targetlist, Node **nodePtr,
int sublevels_up)
{
Node *node = *nodePtr;
if (node == NULL)
return;
switch (nodeTag(node))
{
case T_TargetEntry:
1998-01-21 05:24:46 +01:00
ResolveNew(info, targetlist, &((TargetEntry *) node)->expr,
sublevels_up);
break;
1999-01-24 01:28:37 +01:00
case T_Aggref:
ResolveNew(info, targetlist, &((Aggref *) node)->target,
sublevels_up);
break;
case T_Expr:
1998-01-21 05:24:46 +01:00
ResolveNew(info, targetlist, (Node **) (&(((Expr *) node)->args)),
sublevels_up);
break;
case T_Iter:
ResolveNew(info, targetlist, (Node **) (&(((Iter *) node)->iterexpr)),
sublevels_up);
break;
case T_ArrayRef:
ResolveNew(info, targetlist, (Node **) (&(((ArrayRef *) node)->refupperindexpr)),
sublevels_up);
ResolveNew(info, targetlist, (Node **) (&(((ArrayRef *) node)->reflowerindexpr)),
sublevels_up);
ResolveNew(info, targetlist, (Node **) (&(((ArrayRef *) node)->refexpr)),
sublevels_up);
ResolveNew(info, targetlist, (Node **) (&(((ArrayRef *) node)->refassgnexpr)),
sublevels_up);
break;
case T_Var:
{
int this_varno = (int) ((Var *) node)->varno;
int this_varlevelsup = (int) ((Var *) node)->varlevelsup;
Node *n;
1998-01-21 05:24:46 +01:00
if (this_varno == info->new_varno &&
this_varlevelsup == sublevels_up)
{
n = FindMatchingNew(targetlist,
((Var *) node)->varattno);
if (n == NULL)
{
if (info->event == CMD_UPDATE)
{
1998-10-20 19:21:44 +02:00
*nodePtr = n = copyObject(node);
((Var *) n)->varno = info->current_varno;
((Var *) n)->varnoold = info->current_varno;
}
else
*nodePtr = make_null(((Var *) node)->vartype);
}
else
{
1998-10-20 19:21:44 +02:00
*nodePtr = copyObject(n);
((Var *) *nodePtr)->varlevelsup = this_varlevelsup;
}
}
break;
}
case T_List:
{
List *l;
foreach(l, (List *) node)
1998-01-21 05:24:46 +01:00
ResolveNew(info, targetlist, (Node **) &(lfirst(l)),
sublevels_up);
break;
}
1998-01-21 05:24:46 +01:00
case T_SubLink:
{
SubLink *sublink = (SubLink *) node;
Query *query = (Query *) sublink->subselect;
1998-01-21 05:24:46 +01:00
/* XXX what about lefthand? What about rest of subquery? */
ResolveNew(info, targetlist, (Node **) &(query->qual), sublevels_up + 1);
1998-01-21 05:24:46 +01:00
}
break;
case T_GroupClause:
break;
default:
/* ignore the others */
break;
}
}
void
FixNew(RewriteInfo *info, Query *parsetree)
{
ResolveNew(info, parsetree->targetList,
1998-01-21 05:24:46 +01:00
(Node **) &(info->rule_action->targetList), 0);
1999-05-25 18:15:34 +02:00
ResolveNew(info, parsetree->targetList,
(Node **) &info->rule_action->qual, 0);
ResolveNew(info, parsetree->targetList,
(Node **) &(info->rule_action->groupClause), 0);
}
static void
nodeHandleRIRAttributeRule(Node **nodePtr,
List *rtable,
List *targetlist,
int rt_index,
int attr_num,
int *modified,
1998-01-21 05:24:46 +01:00
int *badsql,
int sublevels_up)
{
Node *node = *nodePtr;
if (node == NULL)
return;
switch (nodeTag(node))
{
case T_TargetEntry:
{
TargetEntry *tle = (TargetEntry *) node;
nodeHandleRIRAttributeRule(&tle->expr, rtable, targetlist,
rt_index, attr_num, modified, badsql,
sublevels_up);
}
break;
1999-01-24 01:28:37 +01:00
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
nodeHandleRIRAttributeRule(&aggref->target, rtable, targetlist,
rt_index, attr_num, modified, badsql,
sublevels_up);
}
break;
case T_Expr:
{
Expr *expr = (Expr *) node;
nodeHandleRIRAttributeRule((Node **) (&(expr->args)), rtable,
targetlist, rt_index, attr_num,
1998-01-21 05:24:46 +01:00
modified, badsql,
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
nodeHandleRIRAttributeRule((Node **) (&(iter->iterexpr)), rtable,
targetlist, rt_index, attr_num,
modified, badsql,
sublevels_up);
}
break;
case T_ArrayRef:
{
1999-05-25 18:15:34 +02:00
ArrayRef *ref = (ArrayRef *) node;
nodeHandleRIRAttributeRule((Node **) (&(ref->refupperindexpr)), rtable,
targetlist, rt_index, attr_num,
modified, badsql,
sublevels_up);
nodeHandleRIRAttributeRule((Node **) (&(ref->reflowerindexpr)), rtable,
targetlist, rt_index, attr_num,
modified, badsql,
sublevels_up);
nodeHandleRIRAttributeRule((Node **) (&(ref->refexpr)), rtable,
targetlist, rt_index, attr_num,
modified, badsql,
sublevels_up);
nodeHandleRIRAttributeRule((Node **) (&(ref->refassgnexpr)), rtable,
targetlist, rt_index, attr_num,
modified, badsql,
sublevels_up);
}
break;
case T_Var:
{
1998-01-21 05:24:46 +01:00
int this_varno = ((Var *) node)->varno;
int this_varattno = ((Var *) node)->varattno;
int this_varlevelsup = ((Var *) node)->varlevelsup;
if (this_varno == rt_index &&
1998-01-21 05:24:46 +01:00
this_varattno == attr_num &&
this_varlevelsup == sublevels_up)
{
if (((Var *) node)->vartype == 32)
{ /* HACK */
*nodePtr = make_null(((Var *) node)->vartype);
*modified = TRUE;
*badsql = TRUE;
break;
}
else
{
1998-01-21 05:24:46 +01:00
NameData name_to_look_for;
name_to_look_for.data[0] = '\0';
namestrcpy(&name_to_look_for,
(char *) get_attname(getrelid(this_varno,
rtable),
attr_num));
1998-01-21 05:24:46 +01:00
if (name_to_look_for.data[0])
{
Node *n;
1998-01-21 05:24:46 +01:00
n = FindMatchingTLEntry(targetlist, (char *) &name_to_look_for);
if (n == NULL)
*nodePtr = make_null(((Var *) node)->vartype);
else
*nodePtr = n;
*modified = TRUE;
}
}
}
1998-01-21 05:24:46 +01:00
}
break;
case T_List:
{
List *i;
1998-01-21 05:24:46 +01:00
foreach(i, (List *) node)
{
nodeHandleRIRAttributeRule((Node **) (&(lfirst(i))), rtable,
targetlist, rt_index, attr_num,
modified, badsql, sublevels_up);
}
}
break;
1998-01-21 05:24:46 +01:00
case T_SubLink:
{
SubLink *sublink = (SubLink *) node;
Query *query = (Query *) sublink->subselect;
1998-01-21 05:24:46 +01:00
/* XXX what about lefthand? What about rest of subquery? */
nodeHandleRIRAttributeRule((Node **) &(query->qual), rtable, targetlist,
rt_index, attr_num, modified, badsql,
sublevels_up + 1);
1998-01-21 05:24:46 +01:00
}
break;
default:
/* ignore the others */
break;
}
}
/*
* Handles 'on retrieve to relation.attribute
* do instead retrieve (attribute = expression) w/qual'
*/
void
HandleRIRAttributeRule(Query *parsetree,
List *rtable,
List *targetlist,
int rt_index,
int attr_num,
int *modified,
int *badsql)
{
nodeHandleRIRAttributeRule((Node **) (&(parsetree->targetList)), rtable,
targetlist, rt_index, attr_num,
1998-01-21 05:24:46 +01:00
modified, badsql, 0);
nodeHandleRIRAttributeRule(&parsetree->qual, rtable, targetlist,
1998-01-21 05:24:46 +01:00
rt_index, attr_num, modified, badsql, 0);
}
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
1999-01-18 01:10:17 +01:00
#ifdef NOT_USED
static void
nodeHandleViewRule(Node **nodePtr,
List *rtable,
List *targetlist,
int rt_index,
1998-01-21 05:24:46 +01:00
int *modified,
int sublevels_up)
{
Node *node = *nodePtr;
if (node == NULL)
return;
switch (nodeTag(node))
{
case T_TargetEntry:
{
TargetEntry *tle = (TargetEntry *) node;
nodeHandleViewRule(&(tle->expr), rtable, targetlist,
1998-01-21 05:24:46 +01:00
rt_index, modified, sublevels_up);
}
break;
1999-01-24 01:28:37 +01:00
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
nodeHandleViewRule(&(aggref->target), rtable, targetlist,
rt_index, modified, sublevels_up);
}
break;
/*
* This has to be done to make queries using groupclauses work
* on views
*/
case T_GroupClause:
{
GroupClause *group = (GroupClause *) node;
nodeHandleViewRule((Node **) (&(group->entry)), rtable, targetlist,
rt_index, modified, sublevels_up);
}
break;
case T_Expr:
{
Expr *expr = (Expr *) node;
nodeHandleViewRule((Node **) (&(expr->args)),
rtable, targetlist,
1998-01-21 05:24:46 +01:00
rt_index, modified, sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
nodeHandleViewRule((Node **) (&(iter->iterexpr)),
rtable, targetlist,
rt_index, modified, sublevels_up);
}
break;
case T_ArrayRef:
{
1999-05-25 18:15:34 +02:00
ArrayRef *ref = (ArrayRef *) node;
nodeHandleViewRule((Node **) (&(ref->refupperindexpr)),
rtable, targetlist,
rt_index, modified, sublevels_up);
nodeHandleViewRule((Node **) (&(ref->reflowerindexpr)),
rtable, targetlist,
rt_index, modified, sublevels_up);
nodeHandleViewRule((Node **) (&(ref->refexpr)),
rtable, targetlist,
rt_index, modified, sublevels_up);
nodeHandleViewRule((Node **) (&(ref->refassgnexpr)),
rtable, targetlist,
rt_index, modified, sublevels_up);
}
break;
case T_Var:
{
Var *var = (Var *) node;
int this_varno = var->varno;
1998-01-21 05:24:46 +01:00
int this_varlevelsup = var->varlevelsup;
Node *n;
1998-01-21 05:24:46 +01:00
if (this_varno == rt_index &&
this_varlevelsup == sublevels_up)
{
n = FindMatchingTLEntry(targetlist,
get_attname(getrelid(this_varno,
rtable),
var->varattno));
if (n == NULL)
*nodePtr = make_null(((Var *) node)->vartype);
else
{
1999-05-25 18:15:34 +02:00
/*
* This is a hack: The varlevelsup of the orignal
* variable and the new one should be the same.
* Normally we adapt the node by changing a
* pointer to point to a var contained in
* 'targetlist'. In the targetlist all
* varlevelsups are 0 so if we want to change it
* to the original value we have to copy the node
* before! (Maybe this will cause troubles with
* some sophisticated queries on views?)
*/
if (this_varlevelsup > 0)
*nodePtr = copyObject(n);
else
*nodePtr = n;
if (nodeTag(nodePtr) == T_Var)
((Var *) *nodePtr)->varlevelsup = this_varlevelsup;
else
nodeHandleViewRule(&n, rtable, targetlist,
1999-05-25 18:15:34 +02:00
rt_index, modified, sublevels_up);
}
*modified = TRUE;
}
break;
}
1998-01-21 05:24:46 +01:00
case T_List:
{
List *l;
foreach(l, (List *) node)
{
nodeHandleViewRule((Node **) (&(lfirst(l))),
rtable, targetlist,
rt_index, modified, sublevels_up);
}
}
break;
case T_SubLink:
{
SubLink *sublink = (SubLink *) node;
Query *query = (Query *) sublink->subselect;
1998-01-21 05:24:46 +01:00
nodeHandleViewRule((Node **) &(query->qual), rtable, targetlist,
rt_index, modified, sublevels_up + 1);
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
1999-01-18 01:10:17 +01:00
/***S*H*D***/
nodeHandleViewRule((Node **) &(query->havingQual), rtable, targetlist,
1999-05-25 18:15:34 +02:00
rt_index, modified, sublevels_up + 1);
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
1999-01-18 01:10:17 +01:00
nodeHandleViewRule((Node **) &(query->targetList), rtable, targetlist,
1999-05-25 18:15:34 +02:00
rt_index, modified, sublevels_up + 1);
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
1999-01-18 01:10:17 +01:00
/*
* We also have to adapt the variables used in
* sublink->lefthand
*/
nodeHandleViewRule((Node **) &(sublink->lefthand), rtable,
targetlist, rt_index, modified, sublevels_up);
1999-05-25 18:15:34 +02:00
}
1998-01-21 05:24:46 +01:00
break;
default:
/* ignore the others */
break;
}
}
void
HandleViewRule(Query *parsetree,
List *rtable,
List *targetlist,
int rt_index,
int *modified)
{
nodeHandleViewRule(&parsetree->qual, rtable, targetlist, rt_index,
1998-01-21 05:24:46 +01:00
modified, 0);
nodeHandleViewRule((Node **) (&(parsetree->targetList)), rtable, targetlist,
1998-01-21 05:24:46 +01:00
rt_index, modified, 0);
/*
* The variables in the havingQual and groupClause also have to be
* adapted
*/
nodeHandleViewRule(&parsetree->havingQual, rtable, targetlist, rt_index,
modified, 0);
nodeHandleViewRule((Node **) (&(parsetree->groupClause)), rtable, targetlist, rt_index,
modified, 0);
}
1999-05-25 18:15:34 +02:00
#endif