66 *
77 *
88 * IDENTIFICATION
9- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.61 1999/10/17 23:50:43 tgl Exp $
9+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.62 1999/11/01 05:18:31 tgl Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -491,7 +491,7 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree)
491491target = (Var * ) (aggref -> target );
492492if (!IsA (target ,Var ))
493493elog (ERROR ,"rewrite: aggregates of views only allowed on simple variables for now" );
494- rte = ( RangeTblEntry * ) nth ( target -> varno - 1 ,parsetree -> rtable );
494+ rte = rt_fetch ( target -> varno ,parsetree -> rtable );
495495
496496resdom = makeNode (Resdom );
497497resdom -> resno = 1 ;
@@ -916,7 +916,7 @@ ApplyRetrieveRule(Query *parsetree,
916916
917917if (relation_level )
918918{
919- RangeTblEntry * rte = ( RangeTblEntry * ) nth ( rt_index - 1 ,rtable );
919+ RangeTblEntry * rte = rt_fetch ( rt_index ,rtable );
920920
921921parsetree = (Query * )apply_RIR_view ((Node * )parsetree ,
922922rt_index ,rte ,
@@ -1024,7 +1024,7 @@ fireRIRrules(Query *parsetree)
10241024{
10251025++ rt_index ;
10261026
1027- rte = nth (rt_index - 1 ,parsetree -> rtable );
1027+ rte = rt_fetch (rt_index ,parsetree -> rtable );
10281028
10291029/*
10301030 * If the table is not one named in the original FROM clause
@@ -1110,7 +1110,8 @@ fireRIRrules(Query *parsetree)
11101110heap_close (rel ,AccessShareLock );
11111111}
11121112
1113- fireRIRonSubselect ((Node * )parsetree ,NULL );
1113+ if (parsetree -> hasSubLinks )
1114+ fireRIRonSubselect ((Node * )parsetree ,NULL );
11141115
11151116parsetree -> qual = modifyAggrefQual (parsetree -> qual ,parsetree );
11161117
@@ -1246,8 +1247,7 @@ fireRules(Query *parsetree,
12461247break ;
12471248}
12481249
1249- rte = (RangeTblEntry * )nth (parsetree -> resultRelation - 1 ,
1250- parsetree -> rtable );
1250+ rte = rt_fetch (parsetree -> resultRelation ,parsetree -> rtable );
12511251if (!rte -> skipAcl )
12521252{
12531253acl_rc = pg_aclcheck (rte -> relname ,
@@ -1546,50 +1546,6 @@ QueryRewriteOne(Query *parsetree)
15461546}
15471547
15481548
1549- /* ----------
1550- * RewritePreprocessQuery -
1551- *adjust details in the parsetree, the rule system
1552- *depends on
1553- * ----------
1554- */
1555- static void
1556- RewritePreprocessQuery (Query * parsetree )
1557- {
1558- /* ----------
1559- * if the query has a resultRelation, reassign the
1560- * result domain numbers to the attribute numbers in the
1561- * target relation. FixNew() depends on it when replacing
1562- * *new* references in a rule action by the expressions
1563- * from the rewritten query.
1564- * resjunk targets are somewhat arbitrarily given a resno of 0;
1565- * this is to prevent FixNew() from matching them to var nodes.
1566- * ----------
1567- */
1568- if (parsetree -> resultRelation > 0 )
1569- {
1570- RangeTblEntry * rte ;
1571- Relation rd ;
1572- List * tl ;
1573-
1574- rte = (RangeTblEntry * )nth (parsetree -> resultRelation - 1 ,
1575- parsetree -> rtable );
1576- rd = heap_openr (rte -> relname ,AccessShareLock );
1577-
1578- foreach (tl ,parsetree -> targetList )
1579- {
1580- TargetEntry * tle = (TargetEntry * )lfirst (tl );
1581-
1582- if (!tle -> resdom -> resjunk )
1583- tle -> resdom -> resno = attnameAttNum (rd ,tle -> resdom -> resname );
1584- else
1585- tle -> resdom -> resno = 0 ;
1586- }
1587-
1588- heap_close (rd ,AccessShareLock );
1589- }
1590- }
1591-
1592-
15931549/*
15941550 * BasicQueryRewrite -
15951551 * rewrite one query via query rewrite system, possibly returning 0
@@ -1606,20 +1562,12 @@ BasicQueryRewrite(Query *parsetree)
16061562/*
16071563 * Step 1
16081564 *
1609- * There still seems something broken with the resdom numbers so we
1610- * reassign them first.
1611- */
1612- RewritePreprocessQuery (parsetree );
1613-
1614- /*
1615- * Step 2
1616- *
16171565 * Apply all non-SELECT rules possibly getting 0 or many queries
16181566 */
16191567querylist = QueryRewriteOne (parsetree );
16201568
16211569/*
1622- * Step3
1570+ * Step2
16231571 *
16241572 * Apply all the RIR rules on each query
16251573 */
@@ -1629,17 +1577,20 @@ BasicQueryRewrite(Query *parsetree)
16291577
16301578/*
16311579 * If the query was marked having aggregates, check if this is
1632- * still true after rewriting. This check must get expanded when
1633- * someday aggregates can appear somewhere else than in the
1634- * targetlist or the having qual.
1580+ * still true after rewriting. Ditto for sublinks.
1581+ *
1582+ * This check must get expanded when someday aggregates can appear
1583+ * somewhere else than in the targetlist or the having qual.
16351584 */
16361585if (query -> hasAggs )
16371586query -> hasAggs = checkQueryHasAggs ((Node * ) (query -> targetList ))
16381587|| checkQueryHasAggs ((Node * ) (query -> havingQual ));
1639- query -> hasSubLinks = checkQueryHasSubLink ((Node * ) (query -> qual ))
1640- || checkQueryHasSubLink ((Node * ) (query -> havingQual ));
1588+ if (query -> hasSubLinks )
1589+ query -> hasSubLinks = checkQueryHasSubLink ((Node * ) (query -> qual ))
1590+ || checkQueryHasSubLink ((Node * ) (query -> havingQual ));
16411591results = lappend (results ,query );
16421592}
1593+
16431594return results ;
16441595}
16451596
@@ -1809,8 +1760,7 @@ Except_Intersect_Rewrite(Query *parsetree)
18091760Node * limitOffset ,
18101761* limitCount ;
18111762CmdType commandType = CMD_SELECT ;
1812- List * rtable_insert = NIL ;
1813-
1763+ RangeTblEntry * rtable_insert = NULL ;
18141764List * prev_target = NIL ;
18151765
18161766/*
@@ -1837,15 +1787,15 @@ Except_Intersect_Rewrite(Query *parsetree)
18371787 */
18381788if (parsetree -> commandType == CMD_INSERT )
18391789{
1840- parsetree -> commandType = CMD_SELECT ;
1841- commandType = CMD_INSERT ;
1842- parsetree -> resultRelation = 0 ;
1843-
18441790/*
18451791 * The result relation ( = the one to insert into) has to be
18461792 * attached to the rtable list of the new top node
18471793 */
1848- rtable_insert = nth (length (parsetree -> rtable )- 1 ,parsetree -> rtable );
1794+ rtable_insert = rt_fetch (parsetree -> resultRelation ,parsetree -> rtable );
1795+
1796+ parsetree -> commandType = CMD_SELECT ;
1797+ commandType = CMD_INSERT ;
1798+ parsetree -> resultRelation = 0 ;
18491799}
18501800
18511801/*