xref: /sqlite-3.40.0/src/walker.c (revision 7b0d34fe)
1 /*
2 ** 2008 August 16
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains routines used for walking the parser tree for
13 ** an SQL statement.
14 */
15 #include "sqliteInt.h"
16 #include <stdlib.h>
17 #include <string.h>
18 
19 
20 #if !defined(SQLITE_OMIT_WINDOWFUNC)
21 /*
22 ** Walk all expressions linked into the list of Window objects passed
23 ** as the second argument.
24 */
25 static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
26   Window *pWin;
27   for(pWin=pList; pWin; pWin=pWin->pNextWin){
28     int rc;
29     rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
30     if( rc ) return WRC_Abort;
31     rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
32     if( rc ) return WRC_Abort;
33     rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
34     if( rc ) return WRC_Abort;
35 
36     /* The next two are purely for calls to sqlite3RenameExprUnmap()
37     ** within sqlite3WindowOffsetExpr().  Because of constraints imposed
38     ** by sqlite3WindowOffsetExpr(), they can never fail.  The results do
39     ** not matter anyhow. */
40     rc = sqlite3WalkExpr(pWalker, pWin->pStart);
41     if( NEVER(rc) ) return WRC_Abort;
42     rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
43     if( NEVER(rc) ) return WRC_Abort;
44     if( bOneOnly ) break;
45   }
46   return WRC_Continue;
47 }
48 #endif
49 
50 /*
51 ** Walk an expression tree.  Invoke the callback once for each node
52 ** of the expression, while descending.  (In other words, the callback
53 ** is invoked before visiting children.)
54 **
55 ** The return value from the callback should be one of the WRC_*
56 ** constants to specify how to proceed with the walk.
57 **
58 **    WRC_Continue      Continue descending down the tree.
59 **
60 **    WRC_Prune         Do not descend into child nodes, but allow
61 **                      the walk to continue with sibling nodes.
62 **
63 **    WRC_Abort         Do no more callbacks.  Unwind the stack and
64 **                      return from the top-level walk call.
65 **
66 ** The return value from this routine is WRC_Abort to abandon the tree walk
67 ** and WRC_Continue to continue.
68 */
69 static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
70   int rc;
71   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
72   testcase( ExprHasProperty(pExpr, EP_Reduced) );
73   while(1){
74     rc = pWalker->xExprCallback(pWalker, pExpr);
75     if( rc ) return rc & WRC_Abort;
76     if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
77       assert( pExpr->x.pList==0 || pExpr->pRight==0 );
78       if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
79       if( pExpr->pRight ){
80         assert( !ExprHasProperty(pExpr, EP_WinFunc) );
81         pExpr = pExpr->pRight;
82         continue;
83       }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
84         assert( !ExprHasProperty(pExpr, EP_WinFunc) );
85         if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
86       }else{
87         if( pExpr->x.pList ){
88           if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
89         }
90 #ifndef SQLITE_OMIT_WINDOWFUNC
91         if( ExprHasProperty(pExpr, EP_WinFunc) ){
92           if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
93         }
94 #endif
95       }
96     }
97     break;
98   }
99   return WRC_Continue;
100 }
101 int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
102   return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
103 }
104 
105 /*
106 ** Call sqlite3WalkExpr() for every expression in list p or until
107 ** an abort request is seen.
108 */
109 int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
110   int i;
111   struct ExprList_item *pItem;
112   if( p ){
113     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
114       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
115     }
116   }
117   return WRC_Continue;
118 }
119 
120 /*
121 ** Walk all expressions associated with SELECT statement p.  Do
122 ** not invoke the SELECT callback on p, but do (of course) invoke
123 ** any expr callbacks and SELECT callbacks that come from subqueries.
124 ** Return WRC_Abort or WRC_Continue.
125 */
126 int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
127   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
128   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
129   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
130   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
131   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
132   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
133 #if !defined(SQLITE_OMIT_WINDOWFUNC)
134   if( p->pWinDefn ){
135     Parse *pParse;
136     if( pWalker->bWalkWinDefn
137      || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
138     ){
139       /* The following may return WRC_Abort if there are unresolvable
140       ** symbols (e.g. a table that does not exist) in a window definition. */
141       int rc = walkWindowList(pWalker, p->pWinDefn, 0);
142       return rc;
143     }
144   }
145 #endif
146   return WRC_Continue;
147 }
148 
149 /*
150 ** Walk the parse trees associated with all subqueries in the
151 ** FROM clause of SELECT statement p.  Do not invoke the select
152 ** callback on p, but do invoke it on each FROM clause subquery
153 ** and on any subqueries further down in the tree.  Return
154 ** WRC_Abort or WRC_Continue;
155 */
156 int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
157   SrcList *pSrc;
158   int i;
159   SrcItem *pItem;
160 
161   pSrc = p->pSrc;
162   if( pSrc ){
163     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
164       if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
165         return WRC_Abort;
166       }
167       if( pItem->fg.isTabFunc
168        && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
169       ){
170         return WRC_Abort;
171       }
172     }
173   }
174   return WRC_Continue;
175 }
176 
177 /*
178 ** Call sqlite3WalkExpr() for every expression in Select statement p.
179 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
180 ** on the compound select chain, p->pPrior.
181 **
182 ** If it is not NULL, the xSelectCallback() callback is invoked before
183 ** the walk of the expressions and FROM clause. The xSelectCallback2()
184 ** method is invoked following the walk of the expressions and FROM clause,
185 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
186 ** and if the expressions and FROM clause both return WRC_Continue;
187 **
188 ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
189 ** there is an abort request.
190 **
191 ** If the Walker does not have an xSelectCallback() then this routine
192 ** is a no-op returning WRC_Continue.
193 */
194 int sqlite3WalkSelect(Walker *pWalker, Select *p){
195   int rc;
196   if( p==0 ) return WRC_Continue;
197   if( pWalker->xSelectCallback==0 ) return WRC_Continue;
198   do{
199     rc = pWalker->xSelectCallback(pWalker, p);
200     if( rc ) return rc & WRC_Abort;
201     if( sqlite3WalkSelectExpr(pWalker, p)
202      || sqlite3WalkSelectFrom(pWalker, p)
203     ){
204       return WRC_Abort;
205     }
206     if( pWalker->xSelectCallback2 ){
207       pWalker->xSelectCallback2(pWalker, p);
208     }
209     p = p->pPrior;
210   }while( p!=0 );
211   return WRC_Continue;
212 }
213 
214 /* Increase the walkerDepth when entering a subquery, and
215 ** descrease when leaving the subquery.
216 */
217 int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
218   UNUSED_PARAMETER(pSelect);
219   pWalker->walkerDepth++;
220   return WRC_Continue;
221 }
222 void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
223   UNUSED_PARAMETER(pSelect);
224   pWalker->walkerDepth--;
225 }
226 
227 
228 /*
229 ** No-op routine for the parse-tree walker.
230 **
231 ** When this routine is the Walker.xExprCallback then expression trees
232 ** are walked without any actions being taken at each node.  Presumably,
233 ** when this routine is used for Walker.xExprCallback then
234 ** Walker.xSelectCallback is set to do something useful for every
235 ** subquery in the parser tree.
236 */
237 int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
238   UNUSED_PARAMETER2(NotUsed, NotUsed2);
239   return WRC_Continue;
240 }
241 
242 /*
243 ** No-op routine for the parse-tree walker for SELECT statements.
244 ** subquery in the parser tree.
245 */
246 int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
247   UNUSED_PARAMETER2(NotUsed, NotUsed2);
248   return WRC_Continue;
249 }
250