6
6
*
7
7
*
8
8
* 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 $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
@@ -491,7 +491,7 @@ modifyAggrefMakeSublink(Expr *origexp, Query *parsetree)
491
491
target = (Var * ) (aggref -> target );
492
492
if (! IsA (target , Var ))
493
493
elog (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 );
495
495
496
496
resdom = makeNode (Resdom );
497
497
resdom -> resno = 1 ;
@@ -916,7 +916,7 @@ ApplyRetrieveRule(Query *parsetree,
916
916
917
917
if (relation_level )
918
918
{
919
- RangeTblEntry * rte = ( RangeTblEntry * ) nth ( rt_index - 1 , rtable );
919
+ RangeTblEntry * rte = rt_fetch ( rt_index , rtable );
920
920
921
921
parsetree = (Query * ) apply_RIR_view ((Node * ) parsetree ,
922
922
rt_index , rte ,
@@ -1024,7 +1024,7 @@ fireRIRrules(Query *parsetree)
1024
1024
{
1025
1025
++ rt_index ;
1026
1026
1027
- rte = nth (rt_index - 1 , parsetree -> rtable );
1027
+ rte = rt_fetch (rt_index , parsetree -> rtable );
1028
1028
1029
1029
/*
1030
1030
* If the table is not one named in the original FROM clause
@@ -1110,7 +1110,8 @@ fireRIRrules(Query *parsetree)
1110
1110
heap_close (rel , AccessShareLock );
1111
1111
}
1112
1112
1113
- fireRIRonSubselect ((Node * ) parsetree , NULL );
1113
+ if (parsetree -> hasSubLinks )
1114
+ fireRIRonSubselect ((Node * ) parsetree , NULL );
1114
1115
1115
1116
parsetree -> qual = modifyAggrefQual (parsetree -> qual , parsetree );
1116
1117
@@ -1246,8 +1247,7 @@ fireRules(Query *parsetree,
1246
1247
break ;
1247
1248
}
1248
1249
1249
- rte = (RangeTblEntry * ) nth (parsetree -> resultRelation - 1 ,
1250
- parsetree -> rtable );
1250
+ rte = rt_fetch (parsetree -> resultRelation , parsetree -> rtable );
1251
1251
if (!rte -> skipAcl )
1252
1252
{
1253
1253
acl_rc = pg_aclcheck (rte -> relname ,
@@ -1546,50 +1546,6 @@ QueryRewriteOne(Query *parsetree)
1546
1546
}
1547
1547
1548
1548
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
-
1593
1549
/*
1594
1550
* BasicQueryRewrite -
1595
1551
* rewrite one query via query rewrite system, possibly returning 0
@@ -1606,20 +1562,12 @@ BasicQueryRewrite(Query *parsetree)
1606
1562
/*
1607
1563
* Step 1
1608
1564
*
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
- *
1617
1565
* Apply all non-SELECT rules possibly getting 0 or many queries
1618
1566
*/
1619
1567
querylist = QueryRewriteOne (parsetree );
1620
1568
1621
1569
/*
1622
- * Step 3
1570
+ * Step 2
1623
1571
*
1624
1572
* Apply all the RIR rules on each query
1625
1573
*/
@@ -1629,17 +1577,20 @@ BasicQueryRewrite(Query *parsetree)
1629
1577
1630
1578
/*
1631
1579
* 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.
1635
1584
*/
1636
1585
if (query -> hasAggs )
1637
1586
query -> hasAggs = checkQueryHasAggs ((Node * ) (query -> targetList ))
1638
1587
|| 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 ));
1641
1591
results = lappend (results , query );
1642
1592
}
1593
+
1643
1594
return results ;
1644
1595
}
1645
1596
@@ -1809,8 +1760,7 @@ Except_Intersect_Rewrite(Query *parsetree)
1809
1760
Node * limitOffset ,
1810
1761
* limitCount ;
1811
1762
CmdType commandType = CMD_SELECT ;
1812
- List * rtable_insert = NIL ;
1813
-
1763
+ RangeTblEntry * rtable_insert = NULL ;
1814
1764
List * prev_target = NIL ;
1815
1765
1816
1766
/*
@@ -1837,15 +1787,15 @@ Except_Intersect_Rewrite(Query *parsetree)
1837
1787
*/
1838
1788
if (parsetree -> commandType == CMD_INSERT )
1839
1789
{
1840
- parsetree -> commandType = CMD_SELECT ;
1841
- commandType = CMD_INSERT ;
1842
- parsetree -> resultRelation = 0 ;
1843
-
1844
1790
/*
1845
1791
* The result relation ( = the one to insert into) has to be
1846
1792
* attached to the rtable list of the new top node
1847
1793
*/
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 ;
1849
1799
}
1850
1800
1851
1801
/*
0 commit comments