Commit 0f413d2d authored by Bruce Momjian's avatar Bruce Momjian

Make subqueries rewrite properly.

parent 7e46348e
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/Attic/locks.c,v 1.7 1998/01/15 19:00:06 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/rewrite/Attic/locks.c,v 1.8 1998/01/21 04:24:34 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
* otherwise, we return false * otherwise, we return false
*/ */
static bool static bool
nodeThisLockWasTriggered(Node *node, int varno, AttrNumber attnum) nodeThisLockWasTriggered(Node *node, int varno, AttrNumber attnum,
int sublevels_up)
{ {
if (node == NULL) if (node == NULL)
return FALSE; return FALSE;
...@@ -46,24 +47,24 @@ nodeThisLockWasTriggered(Node *node, int varno, AttrNumber attnum) ...@@ -46,24 +47,24 @@ nodeThisLockWasTriggered(Node *node, int varno, AttrNumber attnum)
{ {
Expr *expr = (Expr *) node; Expr *expr = (Expr *) node;
return return nodeThisLockWasTriggered((Node *) expr->args, varno,
nodeThisLockWasTriggered((Node *) expr->args, varno, attnum); attnum, sublevels_up);
} }
break; break;
case T_TargetEntry: case T_TargetEntry:
{ {
TargetEntry *tle = (TargetEntry *) node; TargetEntry *tle = (TargetEntry *) node;
return return nodeThisLockWasTriggered(tle->expr, varno, attnum,
nodeThisLockWasTriggered(tle->expr, varno, attnum); sublevels_up);
} }
break; break;
case T_Aggreg: case T_Aggreg:
{ {
Aggreg *agg = (Aggreg *) node; Aggreg *agg = (Aggreg *) node;
return return nodeThisLockWasTriggered(agg->target, varno, attnum,
nodeThisLockWasTriggered(agg->target, varno, attnum); sublevels_up);
} }
break; break;
case T_List: case T_List:
...@@ -72,12 +73,22 @@ nodeThisLockWasTriggered(Node *node, int varno, AttrNumber attnum) ...@@ -72,12 +73,22 @@ nodeThisLockWasTriggered(Node *node, int varno, AttrNumber attnum)
foreach(l, (List *) node) foreach(l, (List *) node)
{ {
if (nodeThisLockWasTriggered(lfirst(l), varno, attnum)) if (nodeThisLockWasTriggered(lfirst(l), varno, attnum,
sublevels_up))
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
} }
break; break;
case T_SubLink:
{
SubLink *sublink = (SubLink *) node;
Query *query = (Query *)sublink->subselect;
return nodeThisLockWasTriggered(query->qual, varno, attnum,
sublevels_up + 1);
}
break;
default: default:
break; break;
} }
...@@ -96,10 +107,10 @@ thisLockWasTriggered(int varno, ...@@ -96,10 +107,10 @@ thisLockWasTriggered(int varno,
Query *parsetree) Query *parsetree)
{ {
if (nodeThisLockWasTriggered(parsetree->qual, varno, attnum)) if (nodeThisLockWasTriggered(parsetree->qual, varno, attnum, 0))
return true; return true;
if (nodeThisLockWasTriggered((Node *) parsetree->targetList, varno, attnum)) if (nodeThisLockWasTriggered((Node *) parsetree->targetList, varno, attnum, 0))
return true; return true;
return false; return false;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.10 1998/01/09 05:48:17 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.11 1998/01/21 04:24:36 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -29,12 +29,12 @@ ...@@ -29,12 +29,12 @@
#include "commands/creatinh.h" #include "commands/creatinh.h"
#include "access/heapam.h" #include "access/heapam.h"
static void static void ApplyRetrieveRule(Query *parsetree, RewriteRule *rule,
ApplyRetrieveRule(Query *parsetree, RewriteRule *rule,
int rt_index, int relation_level, int *modified); int rt_index, int relation_level, int *modified);
static List * static List *fireRules(Query *parsetree, int rt_index, CmdType event,
fireRules(Query *parsetree, int rt_index, CmdType event,
bool *instead_flag, List *locks, List **qual_products); bool *instead_flag, List *locks, List **qual_products);
static void QueryRewriteSubLink(Node *node);
static List *QueryRewriteOne(Query *parsetree);
static List *deepRewriteQuery(Query *parsetree); static List *deepRewriteQuery(Query *parsetree);
/* /*
...@@ -77,11 +77,11 @@ gatherRewriteMeta(Query *parsetree, ...@@ -77,11 +77,11 @@ gatherRewriteMeta(Query *parsetree,
OffsetVarNodes((Node *) info->rule_action->targetList, rt_length); OffsetVarNodes((Node *) info->rule_action->targetList, rt_length);
OffsetVarNodes(info->rule_qual, rt_length); OffsetVarNodes(info->rule_qual, rt_length);
ChangeVarNodes((Node *) info->rule_action->qual, ChangeVarNodes((Node *) info->rule_action->qual,
PRS2_CURRENT_VARNO + rt_length, rt_index); PRS2_CURRENT_VARNO + rt_length, rt_index, 0);
ChangeVarNodes((Node *) info->rule_action->targetList, ChangeVarNodes((Node *) info->rule_action->targetList,
PRS2_CURRENT_VARNO + rt_length, rt_index); PRS2_CURRENT_VARNO + rt_length, rt_index, 0);
ChangeVarNodes(info->rule_qual, ChangeVarNodes(info->rule_qual,
PRS2_CURRENT_VARNO + rt_length, rt_index); PRS2_CURRENT_VARNO + rt_length, rt_index, 0);
/* /*
* bug here about replace CURRENT -- sort of replace current is * bug here about replace CURRENT -- sort of replace current is
...@@ -292,10 +292,10 @@ ApplyRetrieveRule(Query *parsetree, ...@@ -292,10 +292,10 @@ ApplyRetrieveRule(Query *parsetree,
OffsetVarNodes((Node *) rule_action->targetList, rt_length); OffsetVarNodes((Node *) rule_action->targetList, rt_length);
OffsetVarNodes(rule_qual, rt_length); OffsetVarNodes(rule_qual, rt_length);
ChangeVarNodes(rule_action->qual, ChangeVarNodes(rule_action->qual,
PRS2_CURRENT_VARNO + rt_length, rt_index); PRS2_CURRENT_VARNO + rt_length, rt_index, 0);
ChangeVarNodes((Node *) rule_action->targetList, ChangeVarNodes((Node *) rule_action->targetList,
PRS2_CURRENT_VARNO + rt_length, rt_index); PRS2_CURRENT_VARNO + rt_length, rt_index, 0);
ChangeVarNodes(rule_qual, PRS2_CURRENT_VARNO + rt_length, rt_index); ChangeVarNodes(rule_qual, PRS2_CURRENT_VARNO + rt_length, rt_index, 0);
if (relation_level) if (relation_level)
{ {
HandleViewRule(parsetree, rtable, rule_action->targetList, rt_index, HandleViewRule(parsetree, rtable, rule_action->targetList, rt_index,
...@@ -402,7 +402,7 @@ CopyAndAddQual(Query *parsetree, ...@@ -402,7 +402,7 @@ CopyAndAddQual(Query *parsetree,
rtable = append(rtable, listCopy(rule_action->rtable)); rtable = append(rtable, listCopy(rule_action->rtable));
new_tree->rtable = rtable; new_tree->rtable = rtable;
OffsetVarNodes(new_qual, rt_length); OffsetVarNodes(new_qual, rt_length);
ChangeVarNodes(new_qual, PRS2_CURRENT_VARNO + rt_length, rt_index); ChangeVarNodes(new_qual, PRS2_CURRENT_VARNO + rt_length, rt_index, 0);
} }
/* XXX -- where current doesn't work for instead nothing.... yet */ /* XXX -- where current doesn't work for instead nothing.... yet */
AddNotQual(new_tree, new_qual); AddNotQual(new_tree, new_qual);
...@@ -627,6 +627,82 @@ static int numQueryRewriteInvoked = 0; ...@@ -627,6 +627,82 @@ static int numQueryRewriteInvoked = 0;
*/ */
List * List *
QueryRewrite(Query *parsetree) QueryRewrite(Query *parsetree)
{
QueryRewriteSubLink(parsetree->qual);
return QueryRewriteOne(parsetree);
}
/*
* QueryRewriteSubLink
*
* This rewrites the SubLink subqueries first, doing the lowest ones first.
* We already have code in the main rewrite loops to process correlated
* variables from upper queries that exist in subqueries.
*/
static void
QueryRewriteSubLink(Node *node)
{
if (node == NULL)
return;
switch (nodeTag(node))
{
case T_TargetEntry:
break;
case T_Aggreg:
break;
case T_Expr:
{
Expr *expr = (Expr *) node;
QueryRewriteSubLink((Node *)expr->args);
}
break;
case T_Var:
break;
case T_List:
{
List *l;
foreach(l, (List *) node)
QueryRewriteSubLink(lfirst(l));
}
break;
case T_SubLink:
{
SubLink *sublink = (SubLink *) node;
Query *query = (Query *)sublink->subselect;
List *ret;
/*
* Nest down first. We do this so if a rewrite adds a
* SubLink we don't process it as part of this loop.
*/
QueryRewriteSubLink((Node *)query->qual);
ret = QueryRewriteOne(query);
if (!ret)
sublink->subselect = NULL;
else if (lnext(ret) == NIL)
sublink->subselect = lfirst(ret);
else
elog(ERROR,"Don't know how to process subquery that rewrites to multiple queries.");
}
break;
default:
/* ignore the others */
break;
}
return;
}
/*
* QueryOneRewrite -
* rewrite one query
*/
static List *
QueryRewriteOne(Query *parsetree)
{ {
numQueryRewriteInvoked = 0; numQueryRewriteInvoked = 0;
......
This diff is collapsed.
...@@ -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.5 1997/11/26 01:14:25 momjian Exp $ * $Id: rewriteManip.h,v 1.6 1998/01/21 04:24:46 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
/* RewriteManip.c */ /* RewriteManip.c */
void OffsetVarNodes(Node *node, int offset); void OffsetVarNodes(Node *node, int offset);
void ChangeVarNodes(Node *node, int old_varno, int new_varno); void ChangeVarNodes(Node *node, int old_varno, int new_varno,
int sublevels_up);
void AddQual(Query *parsetree, Node *qual); void AddQual(Query *parsetree, Node *qual);
void AddNotQual(Query *parsetree, Node *qual); void AddNotQual(Query *parsetree, Node *qual);
void FixResdomTypes(List *user_tlist); void FixResdomTypes(List *user_tlist);
......
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