mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Restructure representation of join alias variables. An explicit JOIN
now has an RTE of its own, and references to its outputs now are Vars referencing the JOIN RTE, rather than CASE-expressions. This allows reverse-listing in ruleutils.c to use the correct alias easily, rather than painfully reverse-engineering the alias namespace as it used to do. Also, nested FULL JOINs work correctly, because the result of the inner joins are simple Vars that the planner can cope with. This fixes a bug reported a couple times now, notably by Tatsuo on 18-Nov-01. The alias Vars are expanded into COALESCE expressions where needed at the very end of planning, rather than during parsing. Also, beginnings of support for showing plan qualifier expressions in EXPLAIN. There are probably still cases that need work. initdb forced due to change of stored-rule representation.
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.98 2001/10/25 05:49:41 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.99 2002/03/12 00:51:58 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -289,6 +289,7 @@ ApplyRetrieveRule(Query *parsetree,
|
||||
*/
|
||||
rte = rt_fetch(rt_index, parsetree->rtable);
|
||||
|
||||
rte->rtekind = RTE_SUBQUERY;
|
||||
rte->relname = NULL;
|
||||
rte->relid = InvalidOid;
|
||||
rte->subquery = rule_action;
|
||||
@ -354,17 +355,17 @@ markQueryForUpdate(Query *qry, bool skipOldNew)
|
||||
(rti == PRS2_OLD_VARNO || rti == PRS2_NEW_VARNO))
|
||||
continue;
|
||||
|
||||
if (rte->subquery)
|
||||
{
|
||||
/* FOR UPDATE of subquery is propagated to subquery's rels */
|
||||
markQueryForUpdate(rte->subquery, false);
|
||||
}
|
||||
else
|
||||
if (rte->rtekind == RTE_RELATION)
|
||||
{
|
||||
if (!intMember(rti, qry->rowMarks))
|
||||
qry->rowMarks = lappendi(qry->rowMarks, rti);
|
||||
rte->checkForWrite = true;
|
||||
}
|
||||
else if (rte->rtekind == RTE_SUBQUERY)
|
||||
{
|
||||
/* FOR UPDATE of subquery is propagated to subquery's rels */
|
||||
markQueryForUpdate(rte->subquery, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -440,12 +441,18 @@ fireRIRrules(Query *parsetree)
|
||||
* to do to this level of the query, but we must recurse into the
|
||||
* subquery to expand any rule references in it.
|
||||
*/
|
||||
if (rte->subquery)
|
||||
if (rte->rtekind == RTE_SUBQUERY)
|
||||
{
|
||||
rte->subquery = fireRIRrules(rte->subquery);
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Joins and other non-relation RTEs can be ignored completely.
|
||||
*/
|
||||
if (rte->rtekind != RTE_RELATION)
|
||||
continue;
|
||||
|
||||
/*
|
||||
* If the table is not referenced in the query, then we ignore it.
|
||||
* This prevents infinite expansion loop due to new rtable entries
|
||||
@ -756,6 +763,7 @@ RewriteQuery(Query *parsetree, bool *instead_flag, List **qual_products)
|
||||
result_relation = parsetree->resultRelation;
|
||||
Assert(result_relation != 0);
|
||||
rt_entry = rt_fetch(result_relation, parsetree->rtable);
|
||||
Assert(rt_entry->rtekind == RTE_RELATION);
|
||||
|
||||
/*
|
||||
* This may well be the first access to the result relation during the
|
||||
@ -945,7 +953,7 @@ QueryRewrite(Query *parsetree)
|
||||
RangeTblEntry *rte = rt_fetch(query->resultRelation,
|
||||
query->rtable);
|
||||
|
||||
if (rte->subquery)
|
||||
if (rte->rtekind == RTE_SUBQUERY)
|
||||
{
|
||||
switch (query->commandType)
|
||||
{
|
||||
|
@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.61 2001/11/05 17:46:27 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.62 2002/03/12 00:51:58 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -101,8 +101,8 @@ checkExprHasSubLink_walker(Node *node, void *context)
|
||||
*
|
||||
* Find all Var nodes in the given tree with varlevelsup == sublevels_up,
|
||||
* and increment their varno fields (rangetable indexes) by 'offset'.
|
||||
* The varnoold fields are adjusted similarly. Also, RangeTblRef nodes
|
||||
* in join trees and setOp trees are adjusted.
|
||||
* The varnoold fields are adjusted similarly. Also, RangeTblRef and
|
||||
* JoinExpr nodes in join trees and setOp trees are adjusted.
|
||||
*
|
||||
* NOTE: although this has the form of a walker, we cheat and modify the
|
||||
* nodes in-place. The given expression tree should have been copied
|
||||
@ -140,6 +140,14 @@ OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
|
||||
/* the subquery itself is visited separately */
|
||||
return false;
|
||||
}
|
||||
if (IsA(node, JoinExpr))
|
||||
{
|
||||
JoinExpr *j = (JoinExpr *) node;
|
||||
|
||||
if (context->sublevels_up == 0)
|
||||
j->rtindex += context->offset;
|
||||
/* fall through to examine children */
|
||||
}
|
||||
if (IsA(node, Query))
|
||||
{
|
||||
/* Recurse into subselects */
|
||||
@ -200,7 +208,7 @@ OffsetVarNodes(Node *node, int offset, int sublevels_up)
|
||||
* Find all Var nodes in the given tree belonging to a specific relation
|
||||
* (identified by sublevels_up and rt_index), and change their varno fields
|
||||
* to 'new_index'. The varnoold fields are changed too. Also, RangeTblRef
|
||||
* nodes in join trees and setOp trees are adjusted.
|
||||
* and JoinExpr nodes in join trees and setOp trees are adjusted.
|
||||
*
|
||||
* NOTE: although this has the form of a walker, we cheat and modify the
|
||||
* nodes in-place. The given expression tree should have been copied
|
||||
@ -241,6 +249,15 @@ ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
|
||||
/* the subquery itself is visited separately */
|
||||
return false;
|
||||
}
|
||||
if (IsA(node, JoinExpr))
|
||||
{
|
||||
JoinExpr *j = (JoinExpr *) node;
|
||||
|
||||
if (context->sublevels_up == 0 &&
|
||||
j->rtindex == context->rt_index)
|
||||
j->rtindex = context->new_index;
|
||||
/* fall through to examine children */
|
||||
}
|
||||
if (IsA(node, Query))
|
||||
{
|
||||
/* Recurse into subselects */
|
||||
@ -410,6 +427,15 @@ rangeTableEntry_used_walker(Node *node,
|
||||
/* the subquery itself is visited separately */
|
||||
return false;
|
||||
}
|
||||
if (IsA(node, JoinExpr))
|
||||
{
|
||||
JoinExpr *j = (JoinExpr *) node;
|
||||
|
||||
if (j->rtindex == context->rt_index &&
|
||||
context->sublevels_up == 0)
|
||||
return true;
|
||||
/* fall through to examine children */
|
||||
}
|
||||
if (IsA(node, Query))
|
||||
{
|
||||
/* Recurse into subselects */
|
||||
|
Reference in New Issue
Block a user