1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-11 00:12:06 +03:00

Adjust API of expression_tree_mutator and query_tree_mutator to

simplify callers.  It turns out the common case is that the caller
does want to recurse into sub-queries, so push support for that into
these subroutines.
This commit is contained in:
Tom Lane
2003-01-17 02:01:21 +00:00
parent 227a404cf4
commit a4d82dd4b4
8 changed files with 167 additions and 220 deletions

View File

@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.66 2003/01/13 18:10:53 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/subselect.c,v 1.67 2003/01/17 02:01:11 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -628,12 +628,12 @@ process_sublinks_mutator(Node *node, bool *isTopQual)
}
/*
* Note that we will never see a SubPlan expression in the input
* (since this is the very routine that creates 'em to begin with). So
* the code in expression_tree_mutator() that might do inappropriate
* things with SubPlans or SubLinks will not be exercised.
* We should never see a SubPlan expression in the input (since this is
* the very routine that creates 'em to begin with). We shouldn't find
* ourselves invoked directly on a Query, either.
*/
Assert(!is_subplan(node));
Assert(!IsA(node, Query));
/*
* If we recurse down through anything other than a List node, we are

View File

@@ -14,7 +14,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.86 2003/01/15 19:35:44 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.87 2003/01/17 02:01:16 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -34,11 +34,6 @@
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
/* macros borrowed from expression_tree_mutator */
#define FLATCOPY(newnode, node, nodetype) \
( (newnode) = makeNode(nodetype), \
memcpy((newnode), (node), sizeof(nodetype)) )
typedef struct
{
@@ -765,12 +760,12 @@ adjust_inherited_attrs(Node *node,
*/
if (node && IsA(node, Query))
{
Query *query = (Query *) node;
Query *newnode;
FLATCOPY(newnode, query, Query);
query_tree_mutator(newnode, adjust_inherited_attrs_mutator,
(void *) &context, QTW_IGNORE_SUBQUERIES);
newnode = query_tree_mutator((Query *) node,
adjust_inherited_attrs_mutator,
(void *) &context,
QTW_IGNORE_RT_SUBQUERIES);
if (newnode->resultRelation == old_rt_index)
{
newnode->resultRelation = new_rt_index;
@@ -899,6 +894,7 @@ adjust_inherited_attrs_mutator(Node *node,
* already have been converted to subplans before we see them.
*/
Assert(!IsA(node, SubLink));
Assert(!IsA(node, Query));
/*
* BUT: although we don't need to recurse into subplans, we do need to

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.122 2003/01/15 19:35:44 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.123 2003/01/17 02:01:16 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
@@ -2153,7 +2153,7 @@ substitute_actual_parameters_mutator(Node *node,
* {
* adjust context for subquery;
* result = query_tree_walker((Query *) node, my_walker, context,
* 0); // to visit rtable items too
* 0); // adjust flags as needed
* restore context if needed;
* return result;
* }
@@ -2414,7 +2414,7 @@ query_tree_walker(Query *query,
/* nothing to do */
break;
case RTE_SUBQUERY:
if (! (flags & QTW_IGNORE_SUBQUERIES))
if (! (flags & QTW_IGNORE_RT_SUBQUERIES))
if (walker(rte->subquery, context))
return true;
break;
@@ -2477,17 +2477,22 @@ query_tree_walker(Query *query,
* expression_tree_mutator include all those normally found in target lists
* and qualifier clauses during the planning stage.
*
* expression_tree_mutator will handle SubLink nodes by recursing normally
* into the "lefthand" arguments (which are expressions belonging to the outer
* plan). It will also call the mutator on the sub-Query node; however, when
* expression_tree_mutator itself is called on a Query node, it does nothing
* and returns the unmodified Query node. The net effect is that unless the
* mutator does something special at a Query node, sub-selects will not be
* visited or modified; the original sub-select will be linked to by the new
* SubLink node. Mutators that want to descend into sub-selects will usually
* do so by recognizing Query nodes and calling query_tree_mutator (below).
*
* expression_tree_mutator will handle a SubPlan node by recursing into
* the "exprs" and "args" lists (which belong to the outer plan), but it
* will simply copy the link to the inner plan, since that's typically what
* expression tree mutators want. A mutator that wants to modify the subplan
* can force appropriate behavior by recognizing SubPlan expression nodes
* and doing the right thing.
*
* SubLink nodes are handled by recursing into the "lefthand" argument list
* only. (A SubLink will be seen only if the tree has not yet been
* processed by subselect.c.) Again, this can be overridden by the mutator,
* but it seems to be the most useful default behavior.
*--------------------
*/
@@ -2593,14 +2598,16 @@ expression_tree_mutator(Node *node,
break;
case T_SubLink:
{
/*
* We transform the lefthand side, but not the subquery.
*/
SubLink *sublink = (SubLink *) node;
SubLink *newnode;
FLATCOPY(newnode, sublink, SubLink);
MUTATE(newnode->lefthand, sublink->lefthand, List *);
/*
* Also invoke the mutator on the sublink's Query node, so
* it can recurse into the sub-query if it wants to.
*/
MUTATE(newnode->subselect, sublink->subselect, Node *);
return (Node *) newnode;
}
break;
@@ -2707,6 +2714,9 @@ expression_tree_mutator(Node *node,
return (Node *) newnode;
}
break;
case T_Query:
/* Do nothing with a sub-Query, per discussion above */
return node;
case T_List:
{
/*
@@ -2781,17 +2791,17 @@ expression_tree_mutator(Node *node,
* mutator intends to descend into subqueries. It is also useful for
* descending into subqueries within a mutator.
*
* The specified Query node is modified-in-place; do a FLATCOPY() beforehand
* if you don't want to change the original. All substructure is safely
* copied, however.
*
* Some callers want to suppress mutating of certain items in the sub-Query,
* Some callers want to suppress mutating of certain items in the Query,
* typically because they need to process them specially, or don't actually
* want to recurse into subqueries. This is supported by the flags argument,
* which is the bitwise OR of flag values to suppress mutating of
* indicated items. (More flag bits may be added as needed.)
*
* Normally the Query node itself is copied, but some callers want it to be
* modified in-place; they must pass QTW_DONT_COPY_QUERY in flags. All
* modified substructure is safely copied in any case.
*/
void
Query *
query_tree_mutator(Query *query,
Node *(*mutator) (),
void *context,
@@ -2802,6 +2812,14 @@ query_tree_mutator(Query *query,
Assert(query != NULL && IsA(query, Query));
if (! (flags & QTW_DONT_COPY_QUERY))
{
Query *newquery;
FLATCOPY(newquery, query, Query);
query = newquery;
}
MUTATE(query->targetList, query->targetList, List *);
MUTATE(query->jointree, query->jointree, FromExpr *);
MUTATE(query->setOperations, query->setOperations, Node *);
@@ -2818,7 +2836,7 @@ query_tree_mutator(Query *query,
/* nothing to do, don't bother to make a copy */
break;
case RTE_SUBQUERY:
if (! (flags & QTW_IGNORE_SUBQUERIES))
if (! (flags & QTW_IGNORE_RT_SUBQUERIES))
{
FLATCOPY(newrte, rte, RangeTblEntry);
CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
@@ -2843,4 +2861,51 @@ query_tree_mutator(Query *query,
newrt = lappend(newrt, rte);
}
query->rtable = newrt;
return query;
}
/*
* query_or_expression_tree_walker --- hybrid form
*
* This routine will invoke query_tree_walker if called on a Query node,
* else will invoke the walker directly. This is a useful way of starting
* the recursion when the walker's normal change of state is not appropriate
* for the outermost Query node.
*/
bool
query_or_expression_tree_walker(Node *node,
bool (*walker) (),
void *context,
int flags)
{
if (node && IsA(node, Query))
return query_tree_walker((Query *) node,
walker,
context,
flags);
else
return walker(node, context);
}
/*
* query_or_expression_tree_mutator --- hybrid form
*
* This routine will invoke query_tree_mutator if called on a Query node,
* else will invoke the mutator directly. This is a useful way of starting
* the recursion when the mutator's normal change of state is not appropriate
* for the outermost Query node.
*/
Node *
query_or_expression_tree_mutator(Node *node,
Node *(*mutator) (),
void *context,
int flags)
{
if (node && IsA(node, Query))
return (Node *) query_tree_mutator((Query *) node,
mutator,
context,
flags);
else
return mutator(node, context);
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.45 2003/01/16 18:26:02 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/var.c,v 1.46 2003/01/17 02:01:16 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -20,16 +20,6 @@
#include "parser/parsetree.h"
/* macros borrowed from expression_tree_mutator */
#define FLATCOPY(newnode, node, nodetype) \
( (newnode) = makeNode(nodetype), \
memcpy((newnode), (node), sizeof(nodetype)) )
#define MUTATE(newfield, oldfield, fieldtype, mutator, context) \
( (newfield) = (fieldtype) mutator((Node *) (oldfield), (context)) )
typedef struct
{
List *varlist;
@@ -87,14 +77,12 @@ pull_varnos(Node *node)
/*
* Must be prepared to start with a Query or a bare expression tree;
* if it's a Query, go straight to query_tree_walker to make sure that
* sublevels_up doesn't get incremented prematurely.
* if it's a Query, we don't want to increment sublevels_up.
*/
if (node && IsA(node, Query))
query_tree_walker((Query *) node, pull_varnos_walker,
(void *) &context, 0);
else
pull_varnos_walker(node, &context);
query_or_expression_tree_walker(node,
pull_varnos_walker,
(void *) &context,
0);
return context.varlist;
}
@@ -113,24 +101,6 @@ pull_varnos_walker(Node *node, pull_varnos_context *context)
context->varlist = lconsi(var->varno, context->varlist);
return false;
}
if (is_subplan(node))
{
/*
* Already-planned subquery. Examine the args list (parameters to
* be passed to subquery), as well as the exprs list which is
* executed by the outer query. But short-circuit recursion into
* the subquery itself, which would be a waste of effort.
*/
SubPlan *subplan = (SubPlan *) node;
if (pull_varnos_walker((Node *) subplan->exprs,
context))
return true;
if (pull_varnos_walker((Node *) subplan->args,
context))
return true;
return false;
}
if (IsA(node, Query))
{
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
@@ -169,15 +139,12 @@ contain_var_reference(Node *node, int varno, int varattno, int levelsup)
/*
* Must be prepared to start with a Query or a bare expression tree;
* if it's a Query, go straight to query_tree_walker to make sure that
* sublevels_up doesn't get incremented prematurely.
* if it's a Query, we don't want to increment sublevels_up.
*/
if (node && IsA(node, Query))
return query_tree_walker((Query *) node,
contain_var_reference_walker,
(void *) &context, 0);
else
return contain_var_reference_walker(node, &context);
return query_or_expression_tree_walker(node,
contain_var_reference_walker,
(void *) &context,
0);
}
static bool
@@ -196,24 +163,6 @@ contain_var_reference_walker(Node *node,
return true;
return false;
}
if (is_subplan(node))
{
/*
* Already-planned subquery. Examine the args list (parameters to
* be passed to subquery), as well as the exprs list which is
* executed by the outer query. But short-circuit recursion into
* the subquery itself, which would be a waste of effort.
*/
SubPlan *subplan = (SubPlan *) node;
if (contain_var_reference_walker((Node *) subplan->exprs,
context))
return true;
if (contain_var_reference_walker((Node *) subplan->args,
context))
return true;
return false;
}
if (IsA(node, Query))
{
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
@@ -361,32 +310,16 @@ flatten_join_alias_vars_mutator(Node *node,
return flatten_join_alias_vars_mutator(newvar, context);
}
/*
* Since expression_tree_mutator won't touch subselects, we have to
* handle them specially.
*/
if (IsA(node, SubLink))
{
SubLink *sublink = (SubLink *) node;
SubLink *newnode;
FLATCOPY(newnode, sublink, SubLink);
MUTATE(newnode->lefthand, sublink->lefthand, List *,
flatten_join_alias_vars_mutator, context);
MUTATE(newnode->subselect, sublink->subselect, Node *,
flatten_join_alias_vars_mutator, context);
return (Node *) newnode;
}
if (IsA(node, Query))
{
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
Query *query = (Query *) node;
Query *newnode;
FLATCOPY(newnode, query, Query);
context->sublevels_up++;
query_tree_mutator(newnode, flatten_join_alias_vars_mutator,
(void *) context, QTW_IGNORE_JOINALIASES);
newnode = query_tree_mutator((Query *) node,
flatten_join_alias_vars_mutator,
(void *) context,
QTW_IGNORE_JOINALIASES);
context->sublevels_up--;
return (Node *) newnode;
}