Commit 389af07c authored by Tom Lane's avatar Tom Lane

Clean up rewriter routines to use expression_tree_walker and

expression_tree_mutator rather than ad-hoc tree walking code.  This shortens
the code materially and fixes a fair number of sins of omission.  Also,
change modifyAggrefQual to *not* recurse into subselects, since its mission
is satisfied if it removes aggregate functions from the top level of a
WHERE clause.  This cures problems with queries of the form SELECT ...
WHERE x IN (SELECT ... HAVING something-using-an-aggregate), which would
formerly get mucked up by modifyAggrefQual.  The routine is still
fundamentally broken, of course, but I don't think there's any way to get
rid of it before we implement subselects in FROM ...
parent ce1f5ed5
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/Attic/locks.c,v 1.22 1999/09/18 19:07:18 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/rewrite/Attic/locks.c,v 1.23 1999/10/01 04:08:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "catalog/pg_shadow.h" #include "catalog/pg_shadow.h"
#include "optimizer/clauses.h"
#include "rewrite/locks.h" #include "rewrite/locks.h"
#include "utils/acl.h" #include "utils/acl.h"
#include "utils/builtins.h" #include "utils/builtins.h"
...@@ -22,102 +23,86 @@ ...@@ -22,102 +23,86 @@
/* /*
* ThisLockWasTriggered * thisLockWasTriggered
* *
* walk the tree, if there we find a varnode, * walk the tree, if there we find a varnode,
* we check the varattno against the attnum * we check the varattno against the attnum
* if we find at least one such match, we return true * if we find at least one such match, we return true
* otherwise, we return false * otherwise, we return false
*
* XXX this should be unified with attribute_used()
*/ */
typedef struct {
int varno;
int attnum;
int sublevels_up;
} thisLockWasTriggered_context;
static bool static bool
nodeThisLockWasTriggered(Node *node, int varno, AttrNumber attnum, thisLockWasTriggered_walker (Node *node,
int sublevels_up) thisLockWasTriggered_context *context)
{ {
if (node == NULL) if (node == NULL)
return FALSE; return false;
switch (nodeTag(node)) if (IsA(node, Var))
{ {
case T_Var: Var *var = (Var *) node;
{
Var *var = (Var *) node;
if (varno == var->varno && if (var->varlevelsup == context->sublevels_up &&
(attnum == var->varattno || attnum == -1)) var->varno == context->varno &&
return TRUE; (var->varattno == context->attnum || context->attnum == -1))
} return true;
break; return false;
case T_Expr: }
{ if (IsA(node, SubLink))
Expr *expr = (Expr *) node; {
/*
return nodeThisLockWasTriggered((Node *) expr->args, varno, * Standard expression_tree_walker will not recurse into subselect,
attnum, sublevels_up); * but here we must do so.
} */
break; SubLink *sub = (SubLink *) node;
case T_TargetEntry:
{
TargetEntry *tle = (TargetEntry *) node;
return nodeThisLockWasTriggered(tle->expr, varno, attnum,
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
return nodeThisLockWasTriggered(aggref->target, varno, attnum,
sublevels_up);
}
break;
case T_List:
{
List *l;
foreach(l, (List *) node)
{
if (nodeThisLockWasTriggered(lfirst(l), varno, attnum,
sublevels_up))
return TRUE;
}
return FALSE;
}
break;
case T_SubLink:
{
SubLink *sublink = (SubLink *) node;
Query *query = (Query *) sublink->subselect;
return nodeThisLockWasTriggered(query->qual, varno, attnum, if (thisLockWasTriggered_walker((Node *) (sub->lefthand), context))
sublevels_up + 1); return true;
} context->sublevels_up++;
break; if (thisLockWasTriggered_walker((Node *) (sub->subselect), context))
default: {
break; context->sublevels_up--; /* not really necessary */
return true;
}
context->sublevels_up--;
return false;
}
if (IsA(node, Query))
{
/* Reach here after recursing down into subselect above... */
Query *qry = (Query *) node;
if (thisLockWasTriggered_walker((Node *) (qry->targetList), context))
return true;
if (thisLockWasTriggered_walker((Node *) (qry->qual), context))
return true;
if (thisLockWasTriggered_walker((Node *) (qry->havingQual), context))
return true;
return false;
} }
return FALSE; return expression_tree_walker(node, thisLockWasTriggered_walker,
(void *) context);
} }
/*
* thisLockWasTriggered -
* walk the tree, if there we find a varnode, we check the varattno
* against the attnum if we find at least one such match, we return true
* otherwise, we return false
*/
static bool static bool
thisLockWasTriggered(int varno, thisLockWasTriggered(int varno,
AttrNumber attnum, int attnum,
Query *parsetree) Query *parsetree)
{ {
thisLockWasTriggered_context context;
if (nodeThisLockWasTriggered(parsetree->qual, varno, attnum, 0)) context.varno = varno;
return true; context.attnum = attnum;
context.sublevels_up = 0;
if (nodeThisLockWasTriggered((Node *) parsetree->targetList, varno, attnum, 0))
return true;
return false;
return thisLockWasTriggered_walker((Node *) parsetree, &context);
} }
/* /*
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.57 1999/09/19 17:20:58 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.58 1999/10/01 04:08:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -32,6 +32,19 @@ ...@@ -32,6 +32,19 @@
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
extern void CheckSelectForUpdate(Query *rule_action); /* in analyze.c */
/* 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)) )
static RewriteInfo *gatherRewriteMeta(Query *parsetree, static RewriteInfo *gatherRewriteMeta(Query *parsetree,
Query *rule_action, Query *rule_action,
Node *rule_qual, Node *rule_qual,
...@@ -39,12 +52,14 @@ static RewriteInfo *gatherRewriteMeta(Query *parsetree, ...@@ -39,12 +52,14 @@ static RewriteInfo *gatherRewriteMeta(Query *parsetree,
CmdType event, CmdType event,
bool *instead_flag); bool *instead_flag);
static bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up); static bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up);
static bool attribute_used(Node *node, int rt_index, int attno, int sublevels_up); static bool attribute_used(Node *node, int rt_index, int attno,
static void modifyAggrefUplevel(Node *node); int sublevels_up);
static void modifyAggrefChangeVarnodes(Node **nodePtr, int rt_index, int new_index, int sublevels_up); static bool modifyAggrefUplevel(Node *node, void *context);
static void modifyAggrefDropQual(Node **nodePtr, Node *orignode, Expr *expr); static bool modifyAggrefChangeVarnodes(Node *node, int rt_index, int new_index,
int sublevels_up);
static Node *modifyAggrefDropQual(Node *node, Node *targetNode);
static SubLink *modifyAggrefMakeSublink(Expr *origexp, Query *parsetree); static SubLink *modifyAggrefMakeSublink(Expr *origexp, Query *parsetree);
static void modifyAggrefQual(Node **nodePtr, Query *parsetree); static Node *modifyAggrefQual(Node *node, Query *parsetree);
static bool checkQueryHasAggs(Node *node); static bool checkQueryHasAggs(Node *node);
static bool checkQueryHasAggs_walker(Node *node, void *context); static bool checkQueryHasAggs_walker(Node *node, void *context);
static bool checkQueryHasSubLink(Node *node); static bool checkQueryHasSubLink(Node *node);
...@@ -135,221 +150,68 @@ gatherRewriteMeta(Query *parsetree, ...@@ -135,221 +150,68 @@ gatherRewriteMeta(Query *parsetree,
* we need to process a RTE for RIR rules only if it is * we need to process a RTE for RIR rules only if it is
* referenced somewhere in var nodes of the query. * referenced somewhere in var nodes of the query.
*/ */
typedef struct {
int rt_index;
int sublevels_up;
} rangeTableEntry_used_context;
static bool static bool
rangeTableEntry_used(Node *node, int rt_index, int sublevels_up) rangeTableEntry_used_walker (Node *node,
rangeTableEntry_used_context *context)
{ {
if (node == NULL) if (node == NULL)
return FALSE; return false;
if (IsA(node, Var))
switch (nodeTag(node))
{ {
case T_TargetEntry: Var *var = (Var *) node;
{
TargetEntry *tle = (TargetEntry *) node;
return rangeTableEntry_used(
(Node *) (tle->expr),
rt_index,
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
return rangeTableEntry_used(
(Node *) (aggref->target),
rt_index,
sublevels_up);
}
break;
case T_GroupClause:
return FALSE;
case T_Expr:
{
Expr *exp = (Expr *) node;
return rangeTableEntry_used(
(Node *) (exp->args),
rt_index,
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
return rangeTableEntry_used(
(Node *) (iter->iterexpr),
rt_index,
sublevels_up);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
if (rangeTableEntry_used(
(Node *) (ref->refupperindexpr),
rt_index,
sublevels_up))
return TRUE;
if (rangeTableEntry_used(
(Node *) (ref->reflowerindexpr),
rt_index,
sublevels_up))
return TRUE;
if (rangeTableEntry_used(
(Node *) (ref->refexpr),
rt_index,
sublevels_up))
return TRUE;
if (rangeTableEntry_used(
(Node *) (ref->refassgnexpr),
rt_index,
sublevels_up))
return TRUE;
return FALSE;
}
break;
case T_Var:
{
Var *var = (Var *) node;
if (var->varlevelsup == sublevels_up)
return var->varno == rt_index;
else
return FALSE;
}
break;
case T_Param:
return FALSE;
case T_Const:
return FALSE;
case T_List:
{
List *l;
foreach(l, (List *) node)
{
if (rangeTableEntry_used(
(Node *) lfirst(l),
rt_index,
sublevels_up))
return TRUE;
}
return FALSE;
}
break;
case T_SubLink:
{
SubLink *sub = (SubLink *) node;
if (rangeTableEntry_used(
(Node *) (sub->lefthand),
rt_index,
sublevels_up))
return TRUE;
if (rangeTableEntry_used(
(Node *) (sub->subselect),
rt_index,
sublevels_up + 1))
return TRUE;
return FALSE;
}
break;
case T_CaseExpr:
{
CaseExpr *exp = (CaseExpr *) node;
if (rangeTableEntry_used(
(Node *) (exp->args),
rt_index,
sublevels_up))
return TRUE;
if (rangeTableEntry_used(
(Node *) (exp->defresult),
rt_index,
sublevels_up))
return TRUE;
return FALSE;
}
break;
case T_CaseWhen:
{
CaseWhen *when = (CaseWhen *) node;
if (rangeTableEntry_used(
(Node *) (when->expr),
rt_index,
sublevels_up))
return TRUE;
if (rangeTableEntry_used(
(Node *) (when->result),
rt_index,
sublevels_up))
return TRUE;
return FALSE;
}
break;
case T_Query:
{
Query *qry = (Query *) node;
if (rangeTableEntry_used(
(Node *) (qry->targetList),
rt_index,
sublevels_up))
return TRUE;
if (rangeTableEntry_used(
(Node *) (qry->qual),
rt_index,
sublevels_up))
return TRUE;
if (rangeTableEntry_used(
(Node *) (qry->havingQual),
rt_index,
sublevels_up))
return TRUE;
return FALSE;
}
break;
default:
elog(NOTICE, "unknown node tag %d in rangeTableEntry_used()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
if (var->varlevelsup == context->sublevels_up &&
var->varno == context->rt_index)
return true;
return false;
} }
if (IsA(node, SubLink))
{
/*
* Standard expression_tree_walker will not recurse into subselect,
* but here we must do so.
*/
SubLink *sub = (SubLink *) node;
if (rangeTableEntry_used_walker((Node *) (sub->lefthand), context))
return true;
if (rangeTableEntry_used((Node *) (sub->subselect),
context->rt_index,
context->sublevels_up + 1))
return true;
return false;
}
if (IsA(node, Query))
{
/* Reach here after recursing down into subselect above... */
Query *qry = (Query *) node;
if (rangeTableEntry_used_walker((Node *) (qry->targetList), context))
return true;
if (rangeTableEntry_used_walker((Node *) (qry->qual), context))
return true;
if (rangeTableEntry_used_walker((Node *) (qry->havingQual), context))
return true;
return false;
}
return expression_tree_walker(node, rangeTableEntry_used_walker,
(void *) context);
}
static bool
rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
{
rangeTableEntry_used_context context;
return FALSE; context.rt_index = rt_index;
context.sublevels_up = sublevels_up;
return rangeTableEntry_used_walker(node, &context);
} }
...@@ -358,667 +220,247 @@ rangeTableEntry_used(Node *node, int rt_index, int sublevels_up) ...@@ -358,667 +220,247 @@ rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
* Check if a specific attribute number of a RTE is used * Check if a specific attribute number of a RTE is used
* somewhere in the query * somewhere in the query
*/ */
typedef struct {
int rt_index;
int attno;
int sublevels_up;
} attribute_used_context;
static bool static bool
attribute_used(Node *node, int rt_index, int attno, int sublevels_up) attribute_used_walker (Node *node,
attribute_used_context *context)
{ {
if (node == NULL) if (node == NULL)
return FALSE; return false;
if (IsA(node, Var))
switch (nodeTag(node))
{ {
case T_TargetEntry: Var *var = (Var *) node;
{
TargetEntry *tle = (TargetEntry *) node;
return attribute_used(
(Node *) (tle->expr),
rt_index,
attno,
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
return attribute_used(
(Node *) (aggref->target),
rt_index,
attno,
sublevels_up);
}
break;
case T_GroupClause:
return FALSE;
case T_Expr:
{
Expr *exp = (Expr *) node;
return attribute_used(
(Node *) (exp->args),
rt_index,
attno,
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
return attribute_used(
(Node *) (iter->iterexpr),
rt_index,
attno,
sublevels_up);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
if (attribute_used(
(Node *) (ref->refupperindexpr),
rt_index,
attno,
sublevels_up))
return TRUE;
if (attribute_used(
(Node *) (ref->reflowerindexpr),
rt_index,
attno,
sublevels_up))
return TRUE;
if (attribute_used(
(Node *) (ref->refexpr),
rt_index,
attno,
sublevels_up))
return TRUE;
if (attribute_used(
(Node *) (ref->refassgnexpr),
rt_index,
attno,
sublevels_up))
return TRUE;
return FALSE;
}
break;
case T_Var:
{
Var *var = (Var *) node;
if (var->varlevelsup == sublevels_up)
return var->varno == rt_index;
else
return FALSE;
}
break;
case T_Param:
return FALSE;
case T_Const:
return FALSE;
case T_List:
{
List *l;
foreach(l, (List *) node)
{
if (attribute_used(
(Node *) lfirst(l),
rt_index,
attno,
sublevels_up))
return TRUE;
}
return FALSE;
}
break;
case T_SubLink:
{
SubLink *sub = (SubLink *) node;
if (attribute_used(
(Node *) (sub->lefthand),
rt_index,
attno,
sublevels_up))
return TRUE;
if (attribute_used(
(Node *) (sub->subselect),
rt_index,
attno,
sublevels_up + 1))
return TRUE;
return FALSE;
}
break;
case T_Query:
{
Query *qry = (Query *) node;
if (attribute_used(
(Node *) (qry->targetList),
rt_index,
attno,
sublevels_up))
return TRUE;
if (attribute_used(
(Node *) (qry->qual),
rt_index,
attno,
sublevels_up))
return TRUE;
if (attribute_used(
(Node *) (qry->havingQual),
rt_index,
attno,
sublevels_up))
return TRUE;
return FALSE;
}
break;
default:
elog(NOTICE, "unknown node tag %d in attribute_used()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
if (var->varlevelsup == context->sublevels_up &&
var->varno == context->rt_index &&
var->varattno == context->attno)
return true;
return false;
}
if (IsA(node, SubLink))
{
/*
* Standard expression_tree_walker will not recurse into subselect,
* but here we must do so.
*/
SubLink *sub = (SubLink *) node;
if (attribute_used_walker((Node *) (sub->lefthand), context))
return true;
if (attribute_used((Node *) (sub->subselect),
context->rt_index,
context->attno,
context->sublevels_up + 1))
return true;
return false;
}
if (IsA(node, Query))
{
/* Reach here after recursing down into subselect above... */
Query *qry = (Query *) node;
if (attribute_used_walker((Node *) (qry->targetList), context))
return true;
if (attribute_used_walker((Node *) (qry->qual), context))
return true;
if (attribute_used_walker((Node *) (qry->havingQual), context))
return true;
return false;
} }
return expression_tree_walker(node, attribute_used_walker,
(void *) context);
}
static bool
attribute_used(Node *node, int rt_index, int attno, int sublevels_up)
{
attribute_used_context context;
return FALSE; context.rt_index = rt_index;
context.attno = attno;
context.sublevels_up = sublevels_up;
return attribute_used_walker(node, &context);
} }
/* /*
* modifyAggrefUplevel - * modifyAggrefUplevel -
* In the newly created sublink for an aggregate column used in * In the newly created sublink for an aggregate column used in
* the qualification, we must adjust the varlevelsup in all the * the qualification, we must increment the varlevelsup in all the
* var nodes. * var nodes.
*
* NOTE: although this has the form of a walker, we cheat and modify the
* Var nodes in-place. The given expression tree should have been copied
* earlier to ensure that no unwanted side-effects occur!
*/ */
static void static bool
modifyAggrefUplevel(Node *node) modifyAggrefUplevel(Node *node, void *context)
{ {
if (node == NULL) if (node == NULL)
return; return false;
if (IsA(node, Var))
switch (nodeTag(node))
{ {
case T_TargetEntry: Var *var = (Var *) node;
{
TargetEntry *tle = (TargetEntry *) node;
modifyAggrefUplevel(
(Node *) (tle->expr));
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
modifyAggrefUplevel(
(Node *) (aggref->target));
}
break;
case T_Expr:
{
Expr *exp = (Expr *) node;
modifyAggrefUplevel(
(Node *) (exp->args));
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
modifyAggrefUplevel(
(Node *) (iter->iterexpr));
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
modifyAggrefUplevel(
(Node *) (ref->refupperindexpr));
modifyAggrefUplevel(
(Node *) (ref->reflowerindexpr));
modifyAggrefUplevel(
(Node *) (ref->refexpr));
modifyAggrefUplevel(
(Node *) (ref->refassgnexpr));
}
break;
case T_Var:
{
Var *var = (Var *) node;
var->varlevelsup++;
}
break;
case T_Param:
break;
case T_Const:
break;
case T_List:
{
List *l;
foreach(l, (List *) node)
modifyAggrefUplevel(
(Node *) lfirst(l));
}
break;
case T_SubLink:
{
SubLink *sub = (SubLink *) node;
modifyAggrefUplevel(
(Node *) (sub->lefthand));
modifyAggrefUplevel(
(Node *) (sub->subselect));
}
break;
case T_Query:
{
Query *qry = (Query *) node;
modifyAggrefUplevel(
(Node *) (qry->targetList));
modifyAggrefUplevel(
(Node *) (qry->qual));
modifyAggrefUplevel(
(Node *) (qry->havingQual));
}
break;
default:
elog(NOTICE, "unknown node tag %d in modifyAggrefUplevel()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
var->varlevelsup++;
return false;
}
if (IsA(node, SubLink))
{
/*
* Standard expression_tree_walker will not recurse into subselect,
* but here we must do so.
*/
SubLink *sub = (SubLink *) node;
if (modifyAggrefUplevel((Node *) (sub->lefthand), context))
return true;
if (modifyAggrefUplevel((Node *) (sub->subselect), context))
return true;
return false;
} }
if (IsA(node, Query))
{
/* Reach here after recursing down into subselect above... */
Query *qry = (Query *) node;
if (modifyAggrefUplevel((Node *) (qry->targetList), context))
return true;
if (modifyAggrefUplevel((Node *) (qry->qual), context))
return true;
if (modifyAggrefUplevel((Node *) (qry->havingQual), context))
return true;
return false;
}
return expression_tree_walker(node, modifyAggrefUplevel,
(void *) context);
} }
/* /*
* modifyAggrefChangeVarnodes - * modifyAggrefChangeVarnodes -
* Change the var nodes in a sublink created for an aggregate column * Change the var nodes in a sublink created for an aggregate column
* used in the qualification that is subject of the aggregate * used in the qualification to point to the correct local RTE.
* function to point to the correct local RTE. *
* NOTE: although this has the form of a walker, we cheat and modify the
* Var nodes in-place. The given expression tree should have been copied
* earlier to ensure that no unwanted side-effects occur!
*/ */
static void
modifyAggrefChangeVarnodes(Node **nodePtr, int rt_index, int new_index, int sublevels_up)
{
Node *node = *nodePtr;
if (node == NULL) typedef struct {
return; int rt_index;
int new_index;
int sublevels_up;
} modifyAggrefChangeVarnodes_context;
switch (nodeTag(node)) static bool
modifyAggrefChangeVarnodes_walker(Node *node,
modifyAggrefChangeVarnodes_context *context)
{
if (node == NULL)
return false;
if (IsA(node, Var))
{ {
case T_TargetEntry: Var *var = (Var *) node;
{
TargetEntry *tle = (TargetEntry *) node;
modifyAggrefChangeVarnodes(
(Node **) (&(tle->expr)),
rt_index,
new_index,
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
modifyAggrefChangeVarnodes(
(Node **) (&(aggref->target)),
rt_index,
new_index,
sublevels_up);
}
break;
case T_GroupClause:
break;
case T_Expr:
{
Expr *exp = (Expr *) node;
modifyAggrefChangeVarnodes(
(Node **) (&(exp->args)),
rt_index,
new_index,
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
modifyAggrefChangeVarnodes(
(Node **) (&(iter->iterexpr)),
rt_index,
new_index,
sublevels_up);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
modifyAggrefChangeVarnodes(
(Node **) (&(ref->refupperindexpr)),
rt_index,
new_index,
sublevels_up);
modifyAggrefChangeVarnodes(
(Node **) (&(ref->reflowerindexpr)),
rt_index,
new_index,
sublevels_up);
modifyAggrefChangeVarnodes(
(Node **) (&(ref->refexpr)),
rt_index,
new_index,
sublevels_up);
modifyAggrefChangeVarnodes(
(Node **) (&(ref->refassgnexpr)),
rt_index,
new_index,
sublevels_up);
}
break;
case T_Var:
{
Var *var = (Var *) node;
if (var->varlevelsup == sublevels_up &&
var->varno == rt_index)
{
var = copyObject(var);
var->varno = new_index;
var->varnoold = new_index;
var->varlevelsup = 0;
*nodePtr = (Node *) var;
}
}
break;
case T_Param:
break;
case T_Const:
break;
case T_List:
{
List *l;
foreach(l, (List *) node)
modifyAggrefChangeVarnodes(
(Node **) (&lfirst(l)),
rt_index,
new_index,
sublevels_up);
}
break;
case T_SubLink:
{
SubLink *sub = (SubLink *) node;
modifyAggrefChangeVarnodes(
(Node **) (&(sub->lefthand)),
rt_index,
new_index,
sublevels_up);
modifyAggrefChangeVarnodes(
(Node **) (&(sub->subselect)),
rt_index,
new_index,
sublevels_up + 1);
}
break;
case T_Query: if (var->varlevelsup == context->sublevels_up &&
{ var->varno == context->rt_index)
Query *qry = (Query *) node; {
var->varno = context->new_index;
modifyAggrefChangeVarnodes( var->varnoold = context->new_index;
(Node **) (&(qry->targetList)), var->varlevelsup = 0;
rt_index, }
new_index, return false;
sublevels_up); }
if (IsA(node, SubLink))
modifyAggrefChangeVarnodes( {
(Node **) (&(qry->qual)), /*
rt_index, * Standard expression_tree_walker will not recurse into subselect,
new_index, * but here we must do so.
sublevels_up); */
SubLink *sub = (SubLink *) node;
modifyAggrefChangeVarnodes(
(Node **) (&(qry->havingQual)), if (modifyAggrefChangeVarnodes_walker((Node *) (sub->lefthand),
rt_index, context))
new_index, return true;
sublevels_up); if (modifyAggrefChangeVarnodes((Node *) (sub->subselect),
} context->rt_index,
break; context->new_index,
context->sublevels_up + 1))
default: return true;
elog(NOTICE, "unknown node tag %d in modifyAggrefChangeVarnodes()", nodeTag(node)); return false;
elog(NOTICE, "Node is: %s", nodeToString(node)); }
break; if (IsA(node, Query))
{
/* Reach here after recursing down into subselect above... */
Query *qry = (Query *) node;
if (modifyAggrefChangeVarnodes_walker((Node *) (qry->targetList),
context))
return true;
if (modifyAggrefChangeVarnodes_walker((Node *) (qry->qual),
context))
return true;
if (modifyAggrefChangeVarnodes_walker((Node *) (qry->havingQual),
context))
return true;
return false;
}
return expression_tree_walker(node, modifyAggrefChangeVarnodes_walker,
(void *) context);
}
static bool
modifyAggrefChangeVarnodes(Node *node, int rt_index, int new_index,
int sublevels_up)
{
modifyAggrefChangeVarnodes_context context;
} context.rt_index = rt_index;
context.new_index = new_index;
context.sublevels_up = sublevels_up;
return modifyAggrefChangeVarnodes_walker(node, &context);
} }
/* /*
* modifyAggrefDropQual - * modifyAggrefDropQual -
* remove the pure aggref clase from a qualification * remove the pure aggref clause from a qualification
*
* targetNode is a boolean expression node somewhere within the given
* expression tree. When we find it, replace it with a constant TRUE.
* The return tree is a modified copy of the given tree; the given tree
* is not altered.
*
* Note: we don't recurse into subselects looking for targetNode; that's
* not necessary in the current usage, since in fact targetNode will be
* within the same select level as the given toplevel node.
*/ */
static void static Node *
modifyAggrefDropQual(Node **nodePtr, Node *orignode, Expr *expr) modifyAggrefDropQual(Node *node, Node *targetNode)
{ {
Node *node = *nodePtr;
if (node == NULL) if (node == NULL)
return; return NULL;
if (node == targetNode)
switch (nodeTag(node))
{ {
case T_Var: Expr *expr = (Expr *) node;
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
Aggref *oaggref = (Aggref *) orignode;
modifyAggrefDropQual(
(Node **) (&(aggref->target)),
(Node *) (oaggref->target),
expr);
}
break;
case T_Param:
break;
case T_Const:
break;
case T_GroupClause:
break;
case T_Expr:
{
Expr *this_expr = (Expr *) node;
Expr *orig_expr = (Expr *) orignode;
if (orig_expr == expr)
{
Const *ctrue;
if (expr->typeOid != BOOLOID)
elog(ERROR,
"aggregate expression in qualification isn't of type bool");
ctrue = makeNode(Const);
ctrue->consttype = BOOLOID;
ctrue->constlen = 1;
ctrue->constisnull = FALSE;
ctrue->constvalue = (Datum) TRUE;
ctrue->constbyval = TRUE;
*nodePtr = (Node *) ctrue;
}
else
modifyAggrefDropQual(
(Node **) (&(this_expr->args)),
(Node *) (orig_expr->args),
expr);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
Iter *oiter = (Iter *) orignode;
modifyAggrefDropQual(
(Node **) (&(iter->iterexpr)),
(Node *) (oiter->iterexpr),
expr);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
ArrayRef *oref = (ArrayRef *) orignode;
modifyAggrefDropQual(
(Node **) (&(ref->refupperindexpr)),
(Node *) (oref->refupperindexpr),
expr);
modifyAggrefDropQual(
(Node **) (&(ref->reflowerindexpr)),
(Node *) (oref->reflowerindexpr),
expr);
modifyAggrefDropQual(
(Node **) (&(ref->refexpr)),
(Node *) (oref->refexpr),
expr);
modifyAggrefDropQual(
(Node **) (&(ref->refassgnexpr)),
(Node *) (oref->refassgnexpr),
expr);
}
break;
case T_List:
{
List *l;
List *ol = (List *) orignode;
int li = 0;
foreach(l, (List *) node)
{
modifyAggrefDropQual(
(Node **) (&(lfirst(l))),
(Node *) nth(li, ol),
expr);
li++;
}
}
break;
case T_SubLink:
{
SubLink *sub = (SubLink *) node;
SubLink *osub = (SubLink *) orignode;
/* what about the lefthand? */
modifyAggrefDropQual(
(Node **) (&(sub->subselect)),
(Node *) (osub->subselect),
expr);
}
break;
case T_Query:
{
Query *qry = (Query *) node;
Query *oqry = (Query *) orignode;
modifyAggrefDropQual(
(Node **) (&(qry->qual)),
(Node *) (oqry->qual),
expr);
modifyAggrefDropQual(
(Node **) (&(qry->havingQual)),
(Node *) (oqry->havingQual),
expr);
}
break;
default:
elog(NOTICE, "unknown node tag %d in modifyAggrefDropQual()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
if (! IsA(expr, Expr) || expr->typeOid != BOOLOID)
elog(ERROR,
"aggregate expression in qualification isn't of type bool");
return (Node *) makeConst(BOOLOID, 1, (Datum) true,
false, true, false, false);
} }
return expression_tree_mutator(node, modifyAggrefDropQual,
(void *) targetNode);
} }
/* /*
* modifyAggrefMakeSublink - * modifyAggrefMakeSublink -
* Create a sublink node for a qualification expression that * Create a sublink node for a qualification expression that
...@@ -1029,7 +471,6 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree) ...@@ -1029,7 +471,6 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree)
{ {
SubLink *sublink; SubLink *sublink;
Query *subquery; Query *subquery;
Node *subqual;
RangeTblEntry *rte; RangeTblEntry *rte;
Aggref *aggref; Aggref *aggref;
Var *target; Var *target;
...@@ -1037,24 +478,22 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree) ...@@ -1037,24 +478,22 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree)
Resdom *resdom; Resdom *resdom;
Expr *exp = copyObject(origexp); Expr *exp = copyObject(origexp);
if (nodeTag(nth(0, exp->args)) == T_Aggref) if (IsA(nth(0, exp->args), Aggref))
{ {
if (nodeTag(nth(1, exp->args)) == T_Aggref) if (IsA(nth(1, exp->args), Aggref))
elog(ERROR, "rewrite: comparision of 2 aggregate columns not supported"); elog(ERROR, "rewrite: comparison of 2 aggregate columns not supported");
else else
elog(ERROR, "rewrite: aggregate column of view must be at right side in qual"); elog(ERROR, "rewrite: aggregate column of view must be at right side in qual");
/* XXX could try to commute operator, instead of failing */
} }
aggref = (Aggref *) nth(1, exp->args); aggref = (Aggref *) nth(1, exp->args);
target = (Var *) (aggref->target); target = (Var *) (aggref->target);
/* XXX bogus --- agg's target might not be a Var! */ if (! IsA(target, Var))
elog(ERROR, "rewrite: aggregates of views only allowed on simple variables for now");
rte = (RangeTblEntry *) nth(target->varno - 1, parsetree->rtable); rte = (RangeTblEntry *) nth(target->varno - 1, parsetree->rtable);
tle = makeNode(TargetEntry);
resdom = makeNode(Resdom); resdom = makeNode(Resdom);
aggref->usenulls = TRUE; /* XXX safe for all aggs?? */
resdom->resno = 1; resdom->resno = 1;
resdom->restype = aggref->aggtype; resdom->restype = aggref->aggtype;
resdom->restypmod = -1; resdom->restypmod = -1;
...@@ -1063,15 +502,14 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree) ...@@ -1063,15 +502,14 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree)
resdom->reskeyop = 0; resdom->reskeyop = 0;
resdom->resjunk = false; resdom->resjunk = false;
tle = makeNode(TargetEntry);
tle->resdom = resdom; tle->resdom = resdom;
tle->expr = (Node *) aggref; tle->expr = (Node *) aggref; /* note this is from the copied expr */
subqual = copyObject(parsetree->qual);
modifyAggrefDropQual((Node **) &subqual, (Node *) parsetree->qual, origexp);
sublink = makeNode(SubLink); sublink = makeNode(SubLink);
sublink->subLinkType = EXPR_SUBLINK; sublink->subLinkType = EXPR_SUBLINK;
sublink->useor = FALSE; sublink->useor = false;
/* note lefthand and oper are made from the copied expr */
sublink->lefthand = lcons(lfirst(exp->args), NIL); sublink->lefthand = lcons(lfirst(exp->args), NIL);
sublink->oper = lcons(exp->oper, NIL); sublink->oper = lcons(exp->oper, NIL);
...@@ -1088,190 +526,65 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree) ...@@ -1088,190 +526,65 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree)
subquery->unionall = FALSE; subquery->unionall = FALSE;
subquery->uniqueFlag = NULL; subquery->uniqueFlag = NULL;
subquery->sortClause = NULL; subquery->sortClause = NULL;
subquery->rtable = lappend(NIL, rte); subquery->rtable = lcons(rte, NIL);
subquery->targetList = lappend(NIL, tle); subquery->targetList = lcons(tle, NIL);
subquery->qual = subqual; subquery->qual = modifyAggrefDropQual((Node *) parsetree->qual,
(Node *) origexp);
subquery->groupClause = NIL; subquery->groupClause = NIL;
subquery->havingQual = NULL; subquery->havingQual = NULL;
subquery->hasAggs = TRUE; subquery->hasAggs = TRUE;
subquery->hasSubLinks = FALSE; subquery->hasSubLinks = FALSE;
subquery->unionClause = NULL; subquery->unionClause = NULL;
modifyAggrefUplevel((Node *) sublink);
modifyAggrefChangeVarnodes((Node **) &(sublink->lefthand), target->varno,
1, target->varlevelsup);
modifyAggrefChangeVarnodes((Node **) &(sublink->subselect), target->varno,
1, target->varlevelsup);
return sublink;
}
/*
* modifyAggrefQual -
* Search for qualification expressions that contain aggregate
* functions and substiture them by sublinks. These expressions
* originally come from qualifications that use aggregate columns
* of a view.
*/
static void
modifyAggrefQual(Node **nodePtr, Query *parsetree)
{
Node *node = *nodePtr;
if (node == NULL)
return;
switch (nodeTag(node))
{
case T_Var:
break;
case T_Param:
break;
case T_Const:
break;
case T_GroupClause:
break;
case T_Expr:
{
Expr *exp = (Expr *) node;
SubLink *sub;
if (length(exp->args) != 2)
{
modifyAggrefQual(
(Node **) (&(exp->args)),
parsetree);
break;
}
if (nodeTag(nth(0, exp->args)) != T_Aggref &&
nodeTag(nth(1, exp->args)) != T_Aggref)
{
modifyAggrefQual(
(Node **) (&(exp->args)),
parsetree);
break;
}
sub = modifyAggrefMakeSublink(exp,
parsetree);
*nodePtr = (Node *) sub;
parsetree->hasSubLinks = TRUE;
}
break;
case T_CaseExpr:
{
/*
* We're calling recursively, and this routine knows how
* to handle lists so let it do the work to handle the
* WHEN clauses...
*/
modifyAggrefQual(
(Node **) (&(((CaseExpr *) node)->args)),
parsetree);
modifyAggrefQual(
(Node **) (&(((CaseExpr *) node)->defresult)),
parsetree);
}
break;
case T_CaseWhen:
{
modifyAggrefQual(
(Node **) (&(((CaseWhen *) node)->expr)),
parsetree);
modifyAggrefQual(
(Node **) (&(((CaseWhen *) node)->result)),
parsetree);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
modifyAggrefQual(
(Node **) (&(iter->iterexpr)),
parsetree);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
modifyAggrefQual(
(Node **) (&(ref->refupperindexpr)),
parsetree);
modifyAggrefQual(
(Node **) (&(ref->reflowerindexpr)),
parsetree);
modifyAggrefQual(
(Node **) (&(ref->refexpr)),
parsetree);
modifyAggrefQual(
(Node **) (&(ref->refassgnexpr)),
parsetree);
}
break;
case T_List:
{
List *l;
foreach(l, (List *) node)
modifyAggrefQual(
(Node **) (&(lfirst(l))),
parsetree);
}
break;
case T_SubLink:
{
SubLink *sub = (SubLink *) node;
/* lefthand ??? */
modifyAggrefQual(
(Node **) (&(sub->subselect)),
(Query *) (sub->subselect));
}
break;
case T_Query:
{
Query *qry = (Query *) node;
modifyAggrefQual( modifyAggrefUplevel((Node *) subquery, NULL);
(Node **) (&(qry->qual)), /*
parsetree); * Note: it might appear that we should be passing target->varlevelsup+1
* here, since modifyAggrefUplevel has increased all the varlevelsup
* values in the subquery. However, target itself is a pointer to a
* Var node in the subquery, so it's been incremented too! What a kluge
* this all is ... we need to make subquery RTEs so it can go away...
*/
modifyAggrefChangeVarnodes((Node *) subquery, target->varno,
1, target->varlevelsup);
modifyAggrefQual( return sublink;
(Node **) (&(qry->havingQual)), }
parsetree);
}
break;
default:
elog(NOTICE, "unknown node tag %d in modifyAggrefQual()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
/*
* modifyAggrefQual -
* Search for qualification expressions that contain aggregate
* functions and substitute them by sublinks. These expressions
* originally come from qualifications that use aggregate columns
* of a view.
*
* The return value is a modified copy of the given expression tree.
*/
static Node *
modifyAggrefQual(Node *node, Query *parsetree)
{
if (node == NULL)
return NULL;
if (IsA(node, Expr))
{
Expr *expr = (Expr *) node;
if (length(expr->args) == 2 &&
(IsA(lfirst(expr->args), Aggref) ||
IsA(lsecond(expr->args), Aggref)))
{
SubLink *sub = modifyAggrefMakeSublink(expr,
parsetree);
parsetree->hasSubLinks = true;
return (Node *) sub;
}
/* otherwise, fall through and copy the expr normally */
} }
/* We do NOT recurse into subselects in this routine. It's sufficient
* to get rid of aggregates that are in the qual expression proper.
*/
return expression_tree_mutator(node, modifyAggrefQual,
(void *) parsetree);
} }
...@@ -1350,406 +663,141 @@ make_null(Oid type) ...@@ -1350,406 +663,141 @@ make_null(Oid type)
} }
static void /*
apply_RIR_adjust_sublevel(Node *node, int sublevels_up) * apply_RIR_adjust_sublevel -
* Set the varlevelsup field of all Var nodes in the given expression tree
* to sublevels_up. We do NOT recurse into subselects.
*
* NOTE: although this has the form of a walker, we cheat and modify the
* Var nodes in-place. The given expression tree should have been copied
* earlier to ensure that no unwanted side-effects occur!
*/
static bool
apply_RIR_adjust_sublevel_walker(Node *node, int *sublevels_up)
{ {
if (node == NULL) if (node == NULL)
return; return false;
if (IsA(node, Var))
switch (nodeTag(node))
{ {
case T_TargetEntry: Var *var = (Var *) node;
{
TargetEntry *tle = (TargetEntry *) node;
apply_RIR_adjust_sublevel(
(Node *) (tle->expr),
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
apply_RIR_adjust_sublevel(
(Node *) (aggref->target),
sublevels_up);
}
break;
case T_GroupClause:
break;
case T_Expr:
{
Expr *exp = (Expr *) node;
apply_RIR_adjust_sublevel(
(Node *) (exp->args),
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
apply_RIR_adjust_sublevel(
(Node *) (iter->iterexpr),
sublevels_up);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
apply_RIR_adjust_sublevel(
(Node *) (ref->refupperindexpr),
sublevels_up);
apply_RIR_adjust_sublevel(
(Node *) (ref->reflowerindexpr),
sublevels_up);
apply_RIR_adjust_sublevel(
(Node *) (ref->refexpr),
sublevels_up);
apply_RIR_adjust_sublevel(
(Node *) (ref->refassgnexpr),
sublevels_up);
}
break;
case T_Var:
{
Var *var = (Var *) node;
var->varlevelsup = sublevels_up;
}
break;
case T_Param:
break;
case T_Const:
break;
case T_List:
{
List *l;
foreach(l, (List *) node)
{
apply_RIR_adjust_sublevel(
(Node *) lfirst(l),
sublevels_up);
}
}
break;
case T_CaseExpr:
{
CaseExpr *exp = (CaseExpr *) node;
apply_RIR_adjust_sublevel(
(Node *) (exp->args),
sublevels_up);
apply_RIR_adjust_sublevel(
(Node *) (exp->defresult),
sublevels_up);
}
break;
case T_CaseWhen:
{
CaseWhen *exp = (CaseWhen *) node;
apply_RIR_adjust_sublevel(
(Node *) (exp->expr),
sublevels_up);
apply_RIR_adjust_sublevel(
(Node *) (exp->result),
sublevels_up);
}
break;
default:
elog(NOTICE, "unknown node tag %d in attribute_used()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
var->varlevelsup = *sublevels_up;
return false;
} }
return expression_tree_walker(node, apply_RIR_adjust_sublevel_walker,
(void *) sublevels_up);
} }
static void static void
apply_RIR_view(Node **nodePtr, int rt_index, RangeTblEntry *rte, List *tlist, int *modified, int sublevels_up) apply_RIR_adjust_sublevel(Node *node, int sublevels_up)
{ {
Node *node = *nodePtr; apply_RIR_adjust_sublevel_walker(node, &sublevels_up);
}
if (node == NULL)
return;
switch (nodeTag(node))
{
case T_TargetEntry:
{
TargetEntry *tle = (TargetEntry *) node;
apply_RIR_view(
(Node **) (&(tle->expr)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
apply_RIR_view(
(Node **) (&(aggref->target)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
}
break;
case T_GroupClause:
break;
case T_Expr:
{
Expr *exp = (Expr *) node;
apply_RIR_view(
(Node **) (&(exp->args)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
apply_RIR_view(
(Node **) (&(iter->iterexpr)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
apply_RIR_view(
(Node **) (&(ref->refupperindexpr)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
apply_RIR_view(
(Node **) (&(ref->reflowerindexpr)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
apply_RIR_view(
(Node **) (&(ref->refexpr)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
apply_RIR_view(
(Node **) (&(ref->refassgnexpr)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
}
break;
case T_Var: /*
{ * apply_RIR_view
Var *var = (Var *) node; * Replace Vars matching a given RT index with copies of TL expressions.
*/
if (var->varlevelsup == sublevels_up && typedef struct {
var->varno == rt_index) int rt_index;
{ int sublevels_up;
Node *exp; RangeTblEntry *rte;
List *tlist;
if (var->varattno < 0) int *modified;
elog(ERROR, "system column %s not available - %s is a view", get_attname(rte->relid, var->varattno), rte->relname); } apply_RIR_view_context;
exp = FindMatchingTLEntry(
tlist,
get_attname(rte->relid,
var->varattno));
if (exp == NULL)
{
*nodePtr = make_null(var->vartype);
return;
}
exp = copyObject(exp);
if (var->varlevelsup > 0)
apply_RIR_adjust_sublevel(exp, var->varlevelsup);
*nodePtr = exp;
*modified = TRUE;
}
}
break;
case T_Param: static Node *
break; apply_RIR_view_mutator(Node *node,
apply_RIR_view_context *context)
{
if (node == NULL)
return NULL;
if (IsA(node, Var))
{
Var *var = (Var *) node;
case T_Const: if (var->varlevelsup == context->sublevels_up &&
break; var->varno == context->rt_index)
{
Node *expr;
case T_List: if (var->varattno < 0)
{ elog(ERROR, "system column %s not available - %s is a view",
List *l; get_attname(context->rte->relid, var->varattno),
context->rte->relname);
foreach(l, (List *) node)
apply_RIR_view(
(Node **) (&(lfirst(l))),
rt_index,
rte,
tlist,
modified,
sublevels_up);
}
break;
case T_SubLink: expr = FindMatchingTLEntry(context->tlist,
get_attname(context->rte->relid,
var->varattno));
if (expr == NULL)
{ {
SubLink *sub = (SubLink *) node; /* XXX shouldn't this be an error condition? */
return make_null(var->vartype);
apply_RIR_view(
(Node **) (&(sub->lefthand)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
apply_RIR_view(
(Node **) (&(sub->subselect)),
rt_index,
rte,
tlist,
modified,
sublevels_up + 1);
} }
break;
case T_Query: expr = copyObject(expr);
{ if (var->varlevelsup > 0)
Query *qry = (Query *) node; apply_RIR_adjust_sublevel(expr, var->varlevelsup);
*(context->modified) = true;
apply_RIR_view( return (Node *) expr;
(Node **) (&(qry->targetList)), }
rt_index, /* otherwise fall through to copy the var normally */
rte, }
tlist, /*
modified, * Since expression_tree_mutator won't touch subselects, we have to
sublevels_up); * handle them specially.
*/
apply_RIR_view( if (IsA(node, SubLink))
(Node **) (&(qry->qual)), {
rt_index, SubLink *sublink = (SubLink *) node;
rte, SubLink *newnode;
tlist,
modified, FLATCOPY(newnode, sublink, SubLink);
sublevels_up); MUTATE(newnode->lefthand, sublink->lefthand, List *,
apply_RIR_view_mutator, context);
apply_RIR_view( context->sublevels_up++;
(Node **) (&(qry->havingQual)), MUTATE(newnode->subselect, sublink->subselect, Node *,
rt_index, apply_RIR_view_mutator, context);
rte, context->sublevels_up--;
tlist, return (Node *) newnode;
modified, }
sublevels_up); if (IsA(node, Query))
} {
break; Query *query = (Query *) node;
Query *newnode;
FLATCOPY(newnode, query, Query);
MUTATE(newnode->targetList, query->targetList, List *,
apply_RIR_view_mutator, context);
MUTATE(newnode->qual, query->qual, Node *,
apply_RIR_view_mutator, context);
MUTATE(newnode->havingQual, query->havingQual, Node *,
apply_RIR_view_mutator, context);
return (Node *) newnode;
}
return expression_tree_mutator(node, apply_RIR_view_mutator,
(void *) context);
}
case T_CaseExpr: static Node *
{ apply_RIR_view(Node *node, int rt_index, RangeTblEntry *rte, List *tlist,
CaseExpr *exp = (CaseExpr *) node; int *modified, int sublevels_up)
{
apply_RIR_view( apply_RIR_view_context context;
(Node **) (&(exp->args)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
apply_RIR_view(
(Node **) (&(exp->defresult)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
}
break;
case T_CaseWhen: context.rt_index = rt_index;
{ context.sublevels_up = sublevels_up;
CaseWhen *exp = (CaseWhen *) node; context.rte = rte;
context.tlist = tlist;
apply_RIR_view( context.modified = modified;
(Node **) (&(exp->expr)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
apply_RIR_view(
(Node **) (&(exp->result)),
rt_index,
rte,
tlist,
modified,
sublevels_up);
}
break;
default: return apply_RIR_view_mutator(node, &context);
elog(NOTICE, "unknown node tag %d in apply_RIR_view()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
}
} }
extern void CheckSelectForUpdate(Query *rule_action); /* in analyze.c */
static void static Query *
ApplyRetrieveRule(Query *parsetree, ApplyRetrieveRule(Query *parsetree,
RewriteRule *rule, RewriteRule *rule,
int rt_index, int rt_index,
...@@ -1764,7 +812,7 @@ ApplyRetrieveRule(Query *parsetree, ...@@ -1764,7 +812,7 @@ ApplyRetrieveRule(Query *parsetree,
*l; *l;
int nothing, int nothing,
rt_length; rt_length;
int badsql = FALSE; int badsql = false;
rule_qual = rule->qual; rule_qual = rule->qual;
if (rule->actions) if (rule->actions)
...@@ -1773,7 +821,7 @@ ApplyRetrieveRule(Query *parsetree, ...@@ -1773,7 +821,7 @@ ApplyRetrieveRule(Query *parsetree,
* rules with more than one * rules with more than one
* action? -ay */ * action? -ay */
return; return parsetree;
rule_action = copyObject(lfirst(rule->actions)); rule_action = copyObject(lfirst(rule->actions));
nothing = FALSE; nothing = FALSE;
} }
...@@ -1817,6 +865,7 @@ ApplyRetrieveRule(Query *parsetree, ...@@ -1817,6 +865,7 @@ ApplyRetrieveRule(Query *parsetree,
* them. * them.
*/ */
((RowMark *) lfirst(l))->info &= ~ROW_MARK_FOR_UPDATE; ((RowMark *) lfirst(l))->info &= ~ROW_MARK_FOR_UPDATE;
foreach(l2, rule_action->rtable) foreach(l2, rule_action->rtable)
{ {
...@@ -1847,12 +896,16 @@ ApplyRetrieveRule(Query *parsetree, ...@@ -1847,12 +896,16 @@ ApplyRetrieveRule(Query *parsetree,
if (relation_level) if (relation_level)
{ {
apply_RIR_view((Node **) &parsetree, rt_index, RangeTblEntry *rte = (RangeTblEntry *) nth(rt_index - 1, rtable);
(RangeTblEntry *) nth(rt_index - 1, rtable),
rule_action->targetList, modified, 0); parsetree = (Query *) apply_RIR_view((Node *) parsetree,
apply_RIR_view((Node **) &rule_action, rt_index, rt_index, rte,
(RangeTblEntry *) nth(rt_index - 1, rtable), rule_action->targetList,
rule_action->targetList, modified, 0); modified, 0);
rule_action = (Query *) apply_RIR_view((Node *) rule_action,
rt_index, rte,
rule_action->targetList,
modified, 0);
} }
else else
{ {
...@@ -1868,153 +921,59 @@ ApplyRetrieveRule(Query *parsetree, ...@@ -1868,153 +921,59 @@ ApplyRetrieveRule(Query *parsetree,
parsetree->hasAggs = (rule_action->hasAggs || parsetree->hasAggs); parsetree->hasAggs = (rule_action->hasAggs || parsetree->hasAggs);
parsetree->hasSubLinks = (rule_action->hasSubLinks || parsetree->hasSubLinks); parsetree->hasSubLinks = (rule_action->hasSubLinks || parsetree->hasSubLinks);
} }
return parsetree;
} }
static void /*
fireRIRonSubselect(Node *node) * fireRIRonSubselect -
* Apply fireRIRrules() to each subselect found in the given tree.
*
* NOTE: although this has the form of a walker, we cheat and modify the
* SubLink nodes in-place. It is caller's responsibility to ensure that
* no unwanted side-effects occur!
*/
static bool
fireRIRonSubselect(Node *node, void *context)
{ {
if (node == NULL) if (node == NULL)
return; return false;
if (IsA(node, SubLink))
switch (nodeTag(node))
{ {
case T_TargetEntry: /*
{ * Standard expression_tree_walker will not recurse into subselect,
TargetEntry *tle = (TargetEntry *) node; * but here we must do so.
*/
fireRIRonSubselect( SubLink *sub = (SubLink *) node;
(Node *) (tle->expr)); Query *qry;
}
break; /* Process lefthand args */
if (fireRIRonSubselect((Node *) (sub->lefthand), context))
case T_Aggref: return true;
{ /* Do what we came for */
Aggref *aggref = (Aggref *) node; qry = fireRIRrules((Query *) (sub->subselect));
sub->subselect = (Node *) qry;
fireRIRonSubselect( /* Must recurse to handle any sub-subselects! */
(Node *) (aggref->target)); if (fireRIRonSubselect((Node *) qry, context))
} return true;
break; return false;
}
case T_GroupClause: if (IsA(node, Query))
break; {
/* Reach here after recursing down into subselect above... */
case T_Expr: Query *qry = (Query *) node;
{
Expr *exp = (Expr *) node; if (fireRIRonSubselect((Node *) (qry->targetList), context))
return true;
fireRIRonSubselect( if (fireRIRonSubselect((Node *) (qry->qual), context))
(Node *) (exp->args)); return true;
} if (fireRIRonSubselect((Node *) (qry->havingQual), context))
break; return true;
return false;
case T_Iter:
{
Iter *iter = (Iter *) node;
fireRIRonSubselect(
(Node *) (iter->iterexpr));
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
fireRIRonSubselect(
(Node *) (ref->refupperindexpr));
fireRIRonSubselect(
(Node *) (ref->reflowerindexpr));
fireRIRonSubselect(
(Node *) (ref->refexpr));
fireRIRonSubselect(
(Node *) (ref->refassgnexpr));
}
break;
case T_Var:
break;
case T_Param:
break;
case T_Const:
break;
case T_List:
{
List *l;
foreach(l, (List *) node)
fireRIRonSubselect(
(Node *) (lfirst(l)));
}
break;
case T_SubLink:
{
SubLink *sub = (SubLink *) node;
Query *qry;
fireRIRonSubselect(
(Node *) (sub->lefthand));
qry = fireRIRrules((Query *) (sub->subselect));
fireRIRonSubselect(
(Node *) qry);
sub->subselect = (Node *) qry;
}
break;
case T_CaseExpr:
{
CaseExpr *exp = (CaseExpr *) node;
fireRIRonSubselect(
(Node *) (exp->args));
fireRIRonSubselect(
(Node *) (exp->defresult));
}
break;
case T_CaseWhen:
{
CaseWhen *exp = (CaseWhen *) node;
fireRIRonSubselect(
(Node *) (exp->expr));
fireRIRonSubselect(
(Node *) (exp->result));
}
break;
case T_Query:
{
Query *qry = (Query *) node;
fireRIRonSubselect(
(Node *) (qry->targetList));
fireRIRonSubselect(
(Node *) (qry->qual));
fireRIRonSubselect(
(Node *) (qry->havingQual));
}
break;
default:
elog(NOTICE, "unknown node tag %d in fireRIRonSubselect()", nodeTag(node));
elog(NOTICE, "Node is: %s", nodeToString(node));
break;
} }
return expression_tree_walker(node, fireRIRonSubselect,
(void *) context);
} }
...@@ -2032,7 +991,7 @@ fireRIRrules(Query *parsetree) ...@@ -2032,7 +991,7 @@ fireRIRrules(Query *parsetree)
RuleLock *rules; RuleLock *rules;
RewriteRule *rule; RewriteRule *rule;
RewriteRule RIRonly; RewriteRule RIRonly;
int modified; int modified = false;
int i; int i;
List *l; List *l;
...@@ -2104,19 +1063,20 @@ fireRIRrules(Query *parsetree) ...@@ -2104,19 +1063,20 @@ fireRIRrules(Query *parsetree)
RIRonly.qual = rule->qual; RIRonly.qual = rule->qual;
RIRonly.actions = rule->actions; RIRonly.actions = rule->actions;
ApplyRetrieveRule(parsetree, parsetree = ApplyRetrieveRule(parsetree,
&RIRonly, &RIRonly,
rt_index, rt_index,
RIRonly.attrno == -1, RIRonly.attrno == -1,
rel, rel,
&modified); &modified);
} }
heap_close(rel, AccessShareLock); heap_close(rel, AccessShareLock);
} }
fireRIRonSubselect((Node *) parsetree); fireRIRonSubselect((Node *) parsetree, NULL);
modifyAggrefQual((Node **) &(parsetree->qual), parsetree);
parsetree->qual = modifyAggrefQual(parsetree->qual, parsetree);
return parsetree; return parsetree;
} }
...@@ -2639,9 +1599,9 @@ BasicQueryRewrite(Query *parsetree) ...@@ -2639,9 +1599,9 @@ BasicQueryRewrite(Query *parsetree)
*/ */
if (query->hasAggs) if (query->hasAggs)
query->hasAggs = checkQueryHasAggs((Node *) (query->targetList)) query->hasAggs = checkQueryHasAggs((Node *) (query->targetList))
| checkQueryHasAggs((Node *) (query->havingQual)); || checkQueryHasAggs((Node *) (query->havingQual));
query->hasSubLinks = checkQueryHasSubLink((Node *) (query->qual)) query->hasSubLinks = checkQueryHasSubLink((Node *) (query->qual))
| checkQueryHasSubLink((Node *) (query->havingQual)); || checkQueryHasSubLink((Node *) (query->havingQual));
results = lappend(results, query); results = lappend(results, query);
} }
return results; return results;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.40 1999/08/25 23:21:43 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.41 1999/10/01 04:08:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -19,410 +19,182 @@ ...@@ -19,410 +19,182 @@
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
static void ResolveNew(RewriteInfo *info, List *targetlist,
Node **node, int sublevels_up);
/* macros borrowed from expression_tree_mutator */
/* #define FLATCOPY(newnode, node, nodetype) \
* OffsetVarnodes - ( (newnode) = makeNode(nodetype), \
*/ memcpy((newnode), (node), sizeof(nodetype)) )
void
OffsetVarNodes(Node *node, int offset, int sublevels_up)
{
if (node == NULL)
return;
switch (nodeTag(node))
{
case T_TargetEntry:
{
TargetEntry *tle = (TargetEntry *) node;
OffsetVarNodes(
(Node *) (tle->expr),
offset,
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
OffsetVarNodes(
(Node *) (aggref->target),
offset,
sublevels_up);
}
break;
case T_GroupClause:
break;
case T_Expr:
{
Expr *exp = (Expr *) node;
OffsetVarNodes(
(Node *) (exp->args),
offset,
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
OffsetVarNodes(
(Node *) (iter->iterexpr),
offset,
sublevels_up);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
OffsetVarNodes(
(Node *) (ref->refupperindexpr),
offset,
sublevels_up);
OffsetVarNodes(
(Node *) (ref->reflowerindexpr),
offset,
sublevels_up);
OffsetVarNodes(
(Node *) (ref->refexpr),
offset,
sublevels_up);
OffsetVarNodes(
(Node *) (ref->refassgnexpr),
offset,
sublevels_up);
}
break;
case T_Var:
{
Var *var = (Var *) node;
if (var->varlevelsup == sublevels_up)
{
var->varno += offset;
var->varnoold += offset;
}
}
break;
case T_Param:
break;
case T_Const:
break;
case T_List: #define MUTATE(newfield, oldfield, fieldtype, mutator, context) \
{ ( (newfield) = (fieldtype) mutator((Node *) (oldfield), (context)) )
List *l;
foreach(l, (List *) node)
OffsetVarNodes(
(Node *) lfirst(l),
offset,
sublevels_up);
}
break;
case T_SubLink:
{
SubLink *sub = (SubLink *) node;
OffsetVarNodes(
(Node *) (sub->lefthand),
offset,
sublevels_up);
OffsetVarNodes(
(Node *) (sub->subselect),
offset,
sublevels_up + 1);
}
break;
case T_Query:
{
Query *qry = (Query *) node;
OffsetVarNodes(
(Node *) (qry->targetList),
offset,
sublevels_up);
OffsetVarNodes(
(Node *) (qry->qual),
offset,
sublevels_up);
OffsetVarNodes(
(Node *) (qry->havingQual),
offset,
sublevels_up);
}
break;
case T_CaseExpr:
{
CaseExpr *exp = (CaseExpr *) node;
OffsetVarNodes(
(Node *) (exp->args),
offset,
sublevels_up);
OffsetVarNodes(
(Node *) (exp->defresult),
offset,
sublevels_up);
}
break;
case T_CaseWhen:
{
CaseWhen *exp = (CaseWhen *) node;
OffsetVarNodes(
(Node *) (exp->expr),
offset,
sublevels_up);
OffsetVarNodes( /*
(Node *) (exp->result), * OffsetVarNodes - adjust Vars when appending one query's RT to another
offset, *
sublevels_up); * Find all Var nodes in the given tree with varlevelsup == sublevels_up,
} * and increment their varno fields (rangetable indexes) by 'offset'.
break; * The varnoold fields are adjusted similarly.
*
* NOTE: although this has the form of a walker, we cheat and modify the
* Var nodes in-place. The given expression tree should have been copied
* earlier to ensure that no unwanted side-effects occur!
*/
default: typedef struct {
elog(NOTICE, "unknown node tag %d in OffsetVarNodes()", nodeTag(node)); int offset;
elog(NOTICE, "Node is: %s", nodeToString(node)); int sublevels_up;
break; } OffsetVarNodes_context;
static bool
OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
{
if (node == NULL)
return false;
if (IsA(node, Var))
{
Var *var = (Var *) node;
if (var->varlevelsup == context->sublevels_up)
{
var->varno += context->offset;
var->varnoold += context->offset;
}
return false;
} }
if (IsA(node, SubLink))
{
/*
* Standard expression_tree_walker will not recurse into subselect,
* but here we must do so.
*/
SubLink *sub = (SubLink *) node;
if (OffsetVarNodes_walker((Node *) (sub->lefthand),
context))
return true;
OffsetVarNodes((Node *) (sub->subselect),
context->offset,
context->sublevels_up + 1);
return false;
}
if (IsA(node, Query))
{
/* Reach here after recursing down into subselect above... */
Query *qry = (Query *) node;
if (OffsetVarNodes_walker((Node *) (qry->targetList),
context))
return true;
if (OffsetVarNodes_walker((Node *) (qry->qual),
context))
return true;
if (OffsetVarNodes_walker((Node *) (qry->havingQual),
context))
return true;
return false;
}
return expression_tree_walker(node, OffsetVarNodes_walker,
(void *) context);
} }
/*
* ChangeVarNodes -
*/
void void
ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up) OffsetVarNodes(Node *node, int offset, int sublevels_up)
{ {
if (node == NULL) OffsetVarNodes_context context;
return;
switch (nodeTag(node))
{
case T_TargetEntry:
{
TargetEntry *tle = (TargetEntry *) node;
ChangeVarNodes(
(Node *) (tle->expr),
rt_index,
new_index,
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
ChangeVarNodes(
(Node *) (aggref->target),
rt_index,
new_index,
sublevels_up);
}
break;
case T_GroupClause:
break;
case T_Expr:
{
Expr *exp = (Expr *) node;
ChangeVarNodes(
(Node *) (exp->args),
rt_index,
new_index,
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
ChangeVarNodes(
(Node *) (iter->iterexpr),
rt_index,
new_index,
sublevels_up);
}
break;
case T_ArrayRef:
{
ArrayRef *ref = (ArrayRef *) node;
ChangeVarNodes(
(Node *) (ref->refupperindexpr),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
(Node *) (ref->reflowerindexpr),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
(Node *) (ref->refexpr),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
(Node *) (ref->refassgnexpr),
rt_index,
new_index,
sublevels_up);
}
break;
case T_Var:
{
Var *var = (Var *) node;
if (var->varlevelsup == sublevels_up &&
var->varno == rt_index)
{
var->varno = new_index;
var->varnoold = new_index;
}
}
break;
case T_Param:
break;
case T_Const:
break;
case T_List:
{
List *l;
foreach(l, (List *) node)
ChangeVarNodes(
(Node *) lfirst(l),
rt_index,
new_index,
sublevels_up);
}
break;
case T_SubLink: context.offset = offset;
{ context.sublevels_up = sublevels_up;
SubLink *sub = (SubLink *) node; OffsetVarNodes_walker(node, &context);
}
ChangeVarNodes(
(Node *) (sub->lefthand),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
(Node *) (sub->subselect),
rt_index,
new_index,
sublevels_up + 1);
}
break;
case T_Query:
{
Query *qry = (Query *) node;
ChangeVarNodes(
(Node *) (qry->targetList),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
(Node *) (qry->qual),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
(Node *) (qry->havingQual),
rt_index,
new_index,
sublevels_up);
}
break;
case T_CaseExpr:
{
CaseExpr *exp = (CaseExpr *) node;
ChangeVarNodes(
(Node *) (exp->args),
rt_index,
new_index,
sublevels_up);
ChangeVarNodes(
(Node *) (exp->defresult),
rt_index,
new_index,
sublevels_up);
}
break;
case T_CaseWhen: /*
{ * ChangeVarNodes - adjust Var nodes for a specific change of RT index
CaseWhen *exp = (CaseWhen *) node; *
* Find all Var nodes in the given tree belonging to a specific relation
ChangeVarNodes( * (identified by sublevels_up and rt_index), and change their varno fields
(Node *) (exp->expr), * to 'new_index'. The varnoold fields are changed too.
rt_index, *
new_index, * NOTE: although this has the form of a walker, we cheat and modify the
sublevels_up); * Var nodes in-place. The given expression tree should have been copied
* earlier to ensure that no unwanted side-effects occur!
ChangeVarNodes( */
(Node *) (exp->result),
rt_index,
new_index,
sublevels_up);
}
break;
default: typedef struct {
elog(NOTICE, "unknown node tag %d in ChangeVarNodes()", nodeTag(node)); int rt_index;
elog(NOTICE, "Node is: %s", nodeToString(node)); int new_index;
break; int sublevels_up;
} ChangeVarNodes_context;
static bool
ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
{
if (node == NULL)
return false;
if (IsA(node, Var))
{
Var *var = (Var *) node;
if (var->varlevelsup == context->sublevels_up &&
var->varno == context->rt_index)
{
var->varno = context->new_index;
var->varnoold = context->new_index;
}
return false;
}
if (IsA(node, SubLink))
{
/*
* Standard expression_tree_walker will not recurse into subselect,
* but here we must do so.
*/
SubLink *sub = (SubLink *) node;
if (ChangeVarNodes_walker((Node *) (sub->lefthand),
context))
return true;
ChangeVarNodes((Node *) (sub->subselect),
context->rt_index,
context->new_index,
context->sublevels_up + 1);
return false;
} }
if (IsA(node, Query))
{
/* Reach here after recursing down into subselect above... */
Query *qry = (Query *) node;
if (ChangeVarNodes_walker((Node *) (qry->targetList),
context))
return true;
if (ChangeVarNodes_walker((Node *) (qry->qual),
context))
return true;
if (ChangeVarNodes_walker((Node *) (qry->havingQual),
context))
return true;
return false;
}
return expression_tree_walker(node, ChangeVarNodes_walker,
(void *) context);
} }
void
ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
{
ChangeVarNodes_context context;
context.rt_index = rt_index;
context.new_index = new_index;
context.sublevels_up = sublevels_up;
ChangeVarNodes_walker(node, &context);
}
/*
* Add the given qualifier condition to the query's WHERE clause
*/
void void
AddQual(Query *parsetree, Node *qual) AddQual(Query *parsetree, Node *qual)
{ {
...@@ -433,18 +205,19 @@ AddQual(Query *parsetree, Node *qual) ...@@ -433,18 +205,19 @@ AddQual(Query *parsetree, Node *qual)
return; return;
/* INTERSECT want's the original, but we need to copy - Jan */ /* INTERSECT want's the original, but we need to copy - Jan */
/* copy = qual; */
copy = copyObject(qual); copy = copyObject(qual);
old = parsetree->qual; old = parsetree->qual;
if (old == NULL) if (old == NULL)
parsetree->qual = copy; parsetree->qual = copy;
else else
parsetree->qual = (Node *) make_andclause(makeList(parsetree->qual, copy, -1)); parsetree->qual = (Node *) make_andclause(makeList(old, copy, -1));
} }
/* Adds the given havingQual to the one already contained in the parsetree just as /*
* AddQual does for the normal 'where' qual */ * Add the given havingQual to the one already contained in the parsetree
* just as AddQual does for the normal 'where' qual
*/
void void
AddHavingQual(Query *parsetree, Node *havingQual) AddHavingQual(Query *parsetree, Node *havingQual)
{ {
...@@ -455,46 +228,43 @@ AddHavingQual(Query *parsetree, Node *havingQual) ...@@ -455,46 +228,43 @@ AddHavingQual(Query *parsetree, Node *havingQual)
return; return;
/* INTERSECT want's the original, but we need to copy - Jan */ /* INTERSECT want's the original, but we need to copy - Jan */
/* copy = havingQual; */
copy = copyObject(havingQual); copy = copyObject(havingQual);
old = parsetree->havingQual; old = parsetree->havingQual;
if (old == NULL) if (old == NULL)
parsetree->havingQual = copy; parsetree->havingQual = copy;
else else
parsetree->havingQual = (Node *) make_andclause(makeList(parsetree->havingQual, copy, -1)); parsetree->havingQual = (Node *) make_andclause(makeList(old, copy, -1));
} }
#ifdef NOT_USED #ifdef NOT_USED
void void
AddNotHavingQual(Query *parsetree, Node *havingQual) AddNotHavingQual(Query *parsetree, Node *havingQual)
{ {
Node *copy; Node *notqual;
if (havingQual == NULL) if (havingQual == NULL)
return; return;
/* INTERSECT want's the original, but we need to copy - Jan */ /* Need not copy input qual, because AddHavingQual will... */
/* copy = (Node *) make_notclause((Expr *)havingQual); */ notqual = (Node *) make_notclause((Expr *) havingQual);
copy = (Node *) make_notclause((Expr *) copyObject(havingQual));
AddHavingQual(parsetree, copy); AddHavingQual(parsetree, notqual);
} }
#endif #endif
void void
AddNotQual(Query *parsetree, Node *qual) AddNotQual(Query *parsetree, Node *qual)
{ {
Node *copy; Node *notqual;
if (qual == NULL) if (qual == NULL)
return; return;
/* INTERSECT want's the original, but we need to copy - Jan */ /* Need not copy input qual, because AddQual will... */
/* copy = (Node *) make_notclause((Expr *)qual); */ notqual = (Node *) make_notclause((Expr *) qual);
copy = (Node *) make_notclause((Expr *) copyObject(qual));
AddQual(parsetree, copy); AddQual(parsetree, notqual);
} }
...@@ -583,6 +353,7 @@ FixResdomTypes(List *tlist) ...@@ -583,6 +353,7 @@ FixResdomTypes(List *tlist)
#endif #endif
/* Find a targetlist entry by resno */
static Node * static Node *
FindMatchingNew(List *tlist, int attno) FindMatchingNew(List *tlist, int attno)
{ {
...@@ -598,6 +369,7 @@ FindMatchingNew(List *tlist, int attno) ...@@ -598,6 +369,7 @@ FindMatchingNew(List *tlist, int attno)
return NULL; return NULL;
} }
/* Find a targetlist entry by resname */
static Node * static Node *
FindMatchingTLEntry(List *tlist, char *e_attname) FindMatchingTLEntry(List *tlist, char *e_attname)
{ {
...@@ -615,464 +387,270 @@ FindMatchingTLEntry(List *tlist, char *e_attname) ...@@ -615,464 +387,270 @@ FindMatchingTLEntry(List *tlist, char *e_attname)
return NULL; return NULL;
} }
static void
ResolveNew(RewriteInfo *info, List *targetlist, Node **nodePtr,
int sublevels_up)
{
Node *node = *nodePtr;
if (node == NULL)
return;
switch (nodeTag(node))
{
case T_TargetEntry:
ResolveNew(info, targetlist, &((TargetEntry *) node)->expr,
sublevels_up);
break;
case T_Aggref:
ResolveNew(info, targetlist, &((Aggref *) node)->target,
sublevels_up);
break;
case T_Expr:
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;
if (this_varno == info->new_varno &&
this_varlevelsup == sublevels_up)
{
n = FindMatchingNew(targetlist,
((Var *) node)->varattno);
if (n == NULL)
{
if (info->event == CMD_UPDATE)
{
*nodePtr = n = copyObject(node);
((Var *) n)->varno = info->current_varno;
((Var *) n)->varnoold = info->current_varno;
}
else
*nodePtr = make_null(((Var *) node)->vartype);
}
else
{
*nodePtr = copyObject(n);
((Var *) *nodePtr)->varlevelsup = this_varlevelsup;
}
}
break;
}
case T_List:
{
List *l;
foreach(l, (List *) node)
ResolveNew(info, targetlist, (Node **) &(lfirst(l)),
sublevels_up);
break;
}
case T_SubLink:
{
SubLink *sublink = (SubLink *) node;
Query *query = (Query *) sublink->subselect;
/* XXX what about lefthand? What about rest of subquery? */ /*
ResolveNew(info, targetlist, (Node **) &(query->qual), sublevels_up + 1); * ResolveNew - replace Vars with corresponding items from a targetlist
} *
break; * Vars matching info->new_varno and sublevels_up are replaced by the
case T_GroupClause: * entry with matching resno from targetlist, if there is one.
break; */
default:
/* ignore the others */
break;
}
}
void typedef struct {
FixNew(RewriteInfo *info, Query *parsetree) RewriteInfo *info;
{ List *targetlist;
ResolveNew(info, parsetree->targetList, int sublevels_up;
(Node **) &(info->rule_action->targetList), 0); } ResolveNew_context;
ResolveNew(info, parsetree->targetList,
(Node **) &info->rule_action->qual, 0);
ResolveNew(info, parsetree->targetList,
(Node **) &(info->rule_action->groupClause), 0);
}
static void static Node *
nodeHandleRIRAttributeRule(Node **nodePtr, ResolveNew_mutator(Node *node, ResolveNew_context *context)
List *rtable,
List *targetlist,
int rt_index,
int attr_num,
int *modified,
int *badsql,
int sublevels_up)
{ {
Node *node = *nodePtr;
if (node == NULL) if (node == NULL)
return; return NULL;
switch (nodeTag(node)) if (IsA(node, Var))
{ {
case T_TargetEntry: Var *var = (Var *) node;
{ int this_varno = (int) var->varno;
TargetEntry *tle = (TargetEntry *) node; int this_varlevelsup = (int) var->varlevelsup;
nodeHandleRIRAttributeRule(&tle->expr, rtable, targetlist,
rt_index, attr_num, modified, badsql,
sublevels_up);
}
break;
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
nodeHandleRIRAttributeRule(&aggref->target, rtable, targetlist, if (this_varno == context->info->new_varno &&
rt_index, attr_num, modified, badsql, this_varlevelsup == context->sublevels_up)
sublevels_up); {
} Node *n = FindMatchingNew(context->targetlist,
break; var->varattno);
case T_Expr:
{
Expr *expr = (Expr *) node;
nodeHandleRIRAttributeRule((Node **) (&(expr->args)), rtable,
targetlist, rt_index, attr_num,
modified, badsql,
sublevels_up);
}
break;
case T_Iter:
{
Iter *iter = (Iter *) node;
nodeHandleRIRAttributeRule((Node **) (&(iter->iterexpr)), rtable, if (n == NULL)
targetlist, rt_index, attr_num,
modified, badsql,
sublevels_up);
}
break;
case T_ArrayRef:
{
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:
{ {
int this_varno = ((Var *) node)->varno; if (context->info->event == CMD_UPDATE)
int this_varattno = ((Var *) node)->varattno;
int this_varlevelsup = ((Var *) node)->varlevelsup;
if (this_varno == rt_index &&
this_varattno == attr_num &&
this_varlevelsup == sublevels_up)
{ {
if (((Var *) node)->vartype == 32) /* For update, just change unmatched var's varno */
{ /* HACK */ n = copyObject(node);
*nodePtr = make_null(((Var *) node)->vartype); ((Var *) n)->varno = context->info->current_varno;
*modified = TRUE; ((Var *) n)->varnoold = context->info->current_varno;
*badsql = TRUE; return n;
break; }
} else
else {
{ /* Otherwise replace unmatched var with a null */
NameData name_to_look_for; return make_null(var->vartype);
name_to_look_for.data[0] = '\0';
namestrcpy(&name_to_look_for,
(char *) get_attname(getrelid(this_varno,
rtable),
attr_num));
if (name_to_look_for.data[0])
{
Node *n;
n = FindMatchingTLEntry(targetlist, (char *) &name_to_look_for);
if (n == NULL)
*nodePtr = make_null(((Var *) node)->vartype);
else
*nodePtr = n;
*modified = TRUE;
}
}
} }
} }
break; else
case T_List:
{ {
List *i; /* Make a copy of the tlist item to return */
n = copyObject(n);
foreach(i, (List *) node) if (IsA(n, Var))
{ {
nodeHandleRIRAttributeRule((Node **) (&(lfirst(i))), rtable, ((Var *) n)->varlevelsup = this_varlevelsup;
targetlist, rt_index, attr_num,
modified, badsql, sublevels_up);
} }
/* XXX what to do if tlist item is NOT a var?
* Should we be using something like apply_RIR_adjust_sublevel?
*/
return n;
} }
break; }
case T_SubLink: /* otherwise fall through to copy the var normally */
{ }
SubLink *sublink = (SubLink *) node; /*
Query *query = (Query *) sublink->subselect; * 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 *,
ResolveNew_mutator, context);
context->sublevels_up++;
MUTATE(newnode->subselect, sublink->subselect, Node *,
ResolveNew_mutator, context);
context->sublevels_up--;
return (Node *) newnode;
}
if (IsA(node, Query))
{
Query *query = (Query *) node;
Query *newnode;
/* XXX what about lefthand? What about rest of subquery? */ /*
nodeHandleRIRAttributeRule((Node **) &(query->qual), rtable, targetlist, * XXX original code for ResolveNew only recursed into qual field
rt_index, attr_num, modified, badsql, * of subquery. I'm assuming that was an oversight ... tgl 9/99
sublevels_up + 1); */
}
break; FLATCOPY(newnode, query, Query);
default: MUTATE(newnode->targetList, query->targetList, List *,
/* ignore the others */ ResolveNew_mutator, context);
break; MUTATE(newnode->qual, query->qual, Node *,
ResolveNew_mutator, context);
MUTATE(newnode->havingQual, query->havingQual, Node *,
ResolveNew_mutator, context);
return (Node *) newnode;
} }
return expression_tree_mutator(node, ResolveNew_mutator,
(void *) context);
}
static Node *
ResolveNew(Node *node, RewriteInfo *info, List *targetlist,
int sublevels_up)
{
ResolveNew_context context;
context.info = info;
context.targetlist = targetlist;
context.sublevels_up = sublevels_up;
return ResolveNew_mutator(node, &context);
}
void
FixNew(RewriteInfo *info, Query *parsetree)
{
info->rule_action->targetList = (List *)
ResolveNew((Node *) info->rule_action->targetList,
info, parsetree->targetList, 0);
info->rule_action->qual = ResolveNew(info->rule_action->qual,
info, parsetree->targetList, 0);
/* XXX original code didn't fix havingQual; presumably an oversight? */
info->rule_action->havingQual = ResolveNew(info->rule_action->havingQual,
info, parsetree->targetList, 0);
} }
/* /*
* HandleRIRAttributeRule
* Replace Vars matching a given RT index with copies of TL expressions.
*
* Handles 'on retrieve to relation.attribute * Handles 'on retrieve to relation.attribute
* do instead retrieve (attribute = expression) w/qual' * do instead retrieve (attribute = expression) w/qual'
*
* XXX Why is this not unified with apply_RIR_view()?
*/ */
void
HandleRIRAttributeRule(Query *parsetree,
List *rtable,
List *targetlist,
int rt_index,
int attr_num,
int *modified,
int *badsql)
{
nodeHandleRIRAttributeRule((Node **) (&(parsetree->targetList)), rtable, typedef struct {
targetlist, rt_index, attr_num, List *rtable;
modified, badsql, 0); List *targetlist;
nodeHandleRIRAttributeRule(&parsetree->qual, rtable, targetlist, int rt_index;
rt_index, attr_num, modified, badsql, 0); int attr_num;
} int *modified;
int *badsql;
int sublevels_up;
} HandleRIRAttributeRule_context;
#ifdef NOT_USED static Node *
static void HandleRIRAttributeRule_mutator(Node *node,
nodeHandleViewRule(Node **nodePtr, HandleRIRAttributeRule_context *context)
List *rtable,
List *targetlist,
int rt_index,
int *modified,
int sublevels_up)
{ {
Node *node = *nodePtr;
if (node == NULL) if (node == NULL)
return; return NULL;
if (IsA(node, Var))
switch (nodeTag(node))
{ {
case T_TargetEntry: Var *var = (Var *) node;
{ int this_varno = var->varno;
TargetEntry *tle = (TargetEntry *) node; int this_varattno = var->varattno;
int this_varlevelsup = var->varlevelsup;
nodeHandleViewRule(&(tle->expr), rtable, targetlist,
rt_index, modified, sublevels_up); if (this_varno == context->rt_index &&
} this_varattno == context->attr_num &&
break; this_varlevelsup == context->sublevels_up)
case T_Aggref: {
{ if (var->vartype == 32)
Aggref *aggref = (Aggref *) node; { /* HACK: disallow SET variables */
*context->modified = TRUE;
nodeHandleViewRule(&(aggref->target), rtable, targetlist, *context->badsql = TRUE;
rt_index, modified, sublevels_up); return make_null(var->vartype);
} }
break; else
{
/* NameData name_to_look_for;
* This has to be done to make queries using groupclauses work
* on views name_to_look_for.data[0] = '\0';
*/ namestrcpy(&name_to_look_for,
case T_GroupClause: (char *) get_attname(getrelid(this_varno,
{ context->rtable),
GroupClause *group = (GroupClause *) node; this_varattno));
if (name_to_look_for.data[0])
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,
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:
{
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;
int this_varlevelsup = var->varlevelsup;
Node *n;
if (this_varno == rt_index &&
this_varlevelsup == sublevels_up)
{ {
n = FindMatchingTLEntry(targetlist, Node *n;
get_attname(getrelid(this_varno,
rtable), *context->modified = TRUE;
var->varattno)); n = FindMatchingTLEntry(context->targetlist,
(char *) &name_to_look_for);
if (n == NULL) if (n == NULL)
*nodePtr = make_null(((Var *) node)->vartype); return make_null(var->vartype);
else else
{ return copyObject(n);
/*
* 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,
rt_index, modified, sublevels_up);
}
*modified = TRUE;
}
break;
}
case T_List:
{
List *l;
foreach(l, (List *) node)
{
nodeHandleViewRule((Node **) (&(lfirst(l))),
rtable, targetlist,
rt_index, modified, sublevels_up);
} }
} }
break; }
case T_SubLink: /* otherwise fall through to copy the var normally */
{ }
SubLink *sublink = (SubLink *) node; /*
Query *query = (Query *) sublink->subselect; * Since expression_tree_mutator won't touch subselects, we have to
* handle them specially.
nodeHandleViewRule((Node **) &(query->qual), rtable, targetlist, */
rt_index, modified, sublevels_up + 1); if (IsA(node, SubLink))
{
/***S*H*D***/ SubLink *sublink = (SubLink *) node;
nodeHandleViewRule((Node **) &(query->havingQual), rtable, targetlist, SubLink *newnode;
rt_index, modified, sublevels_up + 1);
nodeHandleViewRule((Node **) &(query->targetList), rtable, targetlist, FLATCOPY(newnode, sublink, SubLink);
rt_index, modified, sublevels_up + 1); MUTATE(newnode->lefthand, sublink->lefthand, List *,
HandleRIRAttributeRule_mutator, context);
context->sublevels_up++;
MUTATE(newnode->subselect, sublink->subselect, Node *,
HandleRIRAttributeRule_mutator, context);
context->sublevels_up--;
return (Node *) newnode;
}
if (IsA(node, Query))
{
Query *query = (Query *) node;
Query *newnode;
/*
* XXX original code for HandleRIRAttributeRule only recursed into
* qual field of subquery. I'm assuming that was an oversight ...
*/
/* FLATCOPY(newnode, query, Query);
* We also have to adapt the variables used in MUTATE(newnode->targetList, query->targetList, List *,
* sublink->lefthand HandleRIRAttributeRule_mutator, context);
*/ MUTATE(newnode->qual, query->qual, Node *,
nodeHandleViewRule((Node **) &(sublink->lefthand), rtable, HandleRIRAttributeRule_mutator, context);
targetlist, rt_index, modified, sublevels_up); MUTATE(newnode->havingQual, query->havingQual, Node *,
} HandleRIRAttributeRule_mutator, context);
break; return (Node *) newnode;
default:
/* ignore the others */
break;
} }
return expression_tree_mutator(node, HandleRIRAttributeRule_mutator,
(void *) context);
} }
void void
HandleViewRule(Query *parsetree, HandleRIRAttributeRule(Query *parsetree,
List *rtable, List *rtable,
List *targetlist, List *targetlist,
int rt_index, int rt_index,
int *modified) int attr_num,
int *modified,
int *badsql)
{ {
nodeHandleViewRule(&parsetree->qual, rtable, targetlist, rt_index, HandleRIRAttributeRule_context context;
modified, 0);
nodeHandleViewRule((Node **) (&(parsetree->targetList)), rtable, targetlist, context.rtable = rtable;
rt_index, modified, 0); context.targetlist = targetlist;
context.rt_index = rt_index;
/* context.attr_num = attr_num;
* The variables in the havingQual and groupClause also have to be context.modified = modified;
* adapted context.badsql = badsql;
*/ context.sublevels_up = 0;
nodeHandleViewRule(&parsetree->havingQual, rtable, targetlist, rt_index,
modified, 0); parsetree->targetList = (List *)
nodeHandleViewRule((Node **) (&(parsetree->groupClause)), rtable, targetlist, rt_index, HandleRIRAttributeRule_mutator((Node *) parsetree->targetList,
modified, 0); &context);
parsetree->qual = HandleRIRAttributeRule_mutator(parsetree->qual,
&context);
/* XXX original code did not fix havingQual ... oversight? */
parsetree->havingQual = HandleRIRAttributeRule_mutator(parsetree->havingQual,
&context);
} }
#endif
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: rewriteManip.h,v 1.17 1999/07/15 15:21:30 momjian Exp $ * $Id: rewriteManip.h,v 1.18 1999/10/01 04:08:15 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -16,19 +16,18 @@ ...@@ -16,19 +16,18 @@
#include "rewrite/rewriteHandler.h" #include "rewrite/rewriteHandler.h"
/* RewriteManip.c */ /* RewriteManip.c */
void OffsetVarNodes(Node *node, int offset, int sublevels_up); extern void OffsetVarNodes(Node *node, int offset, int sublevels_up);
void ChangeVarNodes(Node *node, int old_varno, int new_varno, extern void ChangeVarNodes(Node *node, int old_varno, int new_varno,
int sublevels_up); int sublevels_up);
void AddQual(Query *parsetree, Node *qual); extern void AddQual(Query *parsetree, Node *qual);
void AddHavingQual(Query *parsetree, Node *havingQual); extern void AddHavingQual(Query *parsetree, Node *havingQual);
extern void AddNotQual(Query *parsetree, Node *qual);
extern void AddGroupClause(Query *parsetree, List *group_by, List *tlist);
void AddNotQual(Query *parsetree, Node *qual); extern void FixNew(RewriteInfo *info, Query *parsetree);
void AddGroupClause(Query *parsetree, List *group_by, List *tlist);
void FixNew(RewriteInfo *info, Query *parsetree); extern void HandleRIRAttributeRule(Query *parsetree, List *rtable,
List *targetlist, int rt_index,
void HandleRIRAttributeRule(Query *parsetree, List *rtable, List *targetlist, int attr_num, int *modified, int *badsql);
int rt_index, int attr_num, int *modified,
int *badpostquel);
#endif /* REWRITEMANIP_H */ #endif /* REWRITEMANIP_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment