blob: d047ccc039405aaace3c59d4aa19b49bdee87dba [file] [log] [blame]
drh7d10d5a2008-08-20 16:35:10 +00001/*
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.
drh7d10d5a2008-08-20 16:35:10 +000014*/
15#include "sqliteInt.h"
16#include <stdlib.h>
17#include <string.h>
18
19
dand9995032019-01-23 16:59:24 +000020#if !defined(SQLITE_OMIT_WINDOWFUNC)
21/*
22** Walk all expressions linked into the list of Window objects passed
23** as the second argument.
24*/
dana3d33eb2020-12-21 18:39:58 +000025static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
dand9995032019-01-23 16:59:24 +000026 Window *pWin;
27 for(pWin=pList; pWin; pWin=pWin->pNextWin){
drhc5cc8702019-07-19 15:00:32 +000028 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;
dana3d33eb2020-12-21 18:39:58 +000044 if( bOneOnly ) break;
dand9995032019-01-23 16:59:24 +000045 }
46 return WRC_Continue;
47}
48#endif
49
drh7d10d5a2008-08-20 16:35:10 +000050/*
51** Walk an expression tree. Invoke the callback once for each node
peter.d.reid60ec9142014-09-06 16:39:46 +000052** of the expression, while descending. (In other words, the callback
drh7d10d5a2008-08-20 16:35:10 +000053** 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**
drh567bd442017-03-07 12:18:23 +000060** WRC_Prune Do not descend into child nodes, but allow
drh7d10d5a2008-08-20 16:35:10 +000061** the walk to continue with sibling nodes.
62**
63** WRC_Abort Do no more callbacks. Unwind the stack and
drh567bd442017-03-07 12:18:23 +000064** return from the top-level walk call.
drh7d10d5a2008-08-20 16:35:10 +000065**
66** The return value from this routine is WRC_Abort to abandon the tree walk
67** and WRC_Continue to continue.
68*/
drh50922cf2016-01-11 14:19:14 +000069static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +000070 int rc;
drh0a9aa222009-04-08 12:21:30 +000071 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
drh0a9aa222009-04-08 12:21:30 +000072 testcase( ExprHasProperty(pExpr, EP_Reduced) );
drhdceed862017-08-17 19:23:16 +000073 while(1){
74 rc = pWalker->xExprCallback(pWalker, pExpr);
75 if( rc ) return rc & WRC_Abort;
76 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
drhec123e12019-11-22 11:47:04 +000077 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
drhdceed862017-08-17 19:23:16 +000078 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
drhdceed862017-08-17 19:23:16 +000079 if( pExpr->pRight ){
dan4f9adee2019-07-13 16:22:50 +000080 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
drhdceed862017-08-17 19:23:16 +000081 pExpr = pExpr->pRight;
82 continue;
83 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
dan4f9adee2019-07-13 16:22:50 +000084 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
drhdceed862017-08-17 19:23:16 +000085 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
dan8117f112019-07-10 20:16:53 +000086 }else{
87 if( pExpr->x.pList ){
88 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
89 }
dan867be212018-06-25 11:42:08 +000090#ifndef SQLITE_OMIT_WINDOWFUNC
dan4f9adee2019-07-13 16:22:50 +000091 if( ExprHasProperty(pExpr, EP_WinFunc) ){
dana3d33eb2020-12-21 18:39:58 +000092 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
dan8117f112019-07-10 20:16:53 +000093 }
dan867be212018-06-25 11:42:08 +000094#endif
dan8117f112019-07-10 20:16:53 +000095 }
drhc2d14a92017-07-07 12:58:30 +000096 }
drhdceed862017-08-17 19:23:16 +000097 break;
drh7d10d5a2008-08-20 16:35:10 +000098 }
drh922802c2016-08-10 18:56:32 +000099 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +0000100}
drh50922cf2016-01-11 14:19:14 +0000101int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
102 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
103}
drh7d10d5a2008-08-20 16:35:10 +0000104
105/*
106** Call sqlite3WalkExpr() for every expression in list p or until
107** an abort request is seen.
108*/
109int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
drhf7828b52009-06-15 23:15:59 +0000110 int i;
drh7d10d5a2008-08-20 16:35:10 +0000111 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 }
drhf7828b52009-06-15 23:15:59 +0000117 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +0000118}
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*/
126int 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;
dand9995032019-01-23 16:59:24 +0000133#if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
134 {
135 Parse *pParse = pWalker->pParse;
136 if( pParse && IN_RENAME_OBJECT ){
dana1ac0352019-07-19 10:31:29 +0000137 /* The following may return WRC_Abort if there are unresolvable
138 ** symbols (e.g. a table that does not exist) in a window definition. */
dana3d33eb2020-12-21 18:39:58 +0000139 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
drh6a02f232019-01-24 04:44:54 +0000140 return rc;
dand9995032019-01-23 16:59:24 +0000141 }
142 }
143#endif
drh7d10d5a2008-08-20 16:35:10 +0000144 return WRC_Continue;
145}
146
147/*
148** Walk the parse trees associated with all subqueries in the
149** FROM clause of SELECT statement p. Do not invoke the select
150** callback on p, but do invoke it on each FROM clause subquery
151** and on any subqueries further down in the tree. Return
152** WRC_Abort or WRC_Continue;
153*/
154int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
155 SrcList *pSrc;
156 int i;
157 struct SrcList_item *pItem;
158
159 pSrc = p->pSrc;
drh2aee5142020-03-21 00:05:53 +0000160 if( pSrc ){
161 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
162 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
163 return WRC_Abort;
164 }
165 if( pItem->fg.isTabFunc
166 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
167 ){
168 return WRC_Abort;
169 }
drh7d10d5a2008-08-20 16:35:10 +0000170 }
171 }
172 return WRC_Continue;
173}
174
175/*
176** Call sqlite3WalkExpr() for every expression in Select statement p.
177** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
danb290f112014-01-17 14:59:27 +0000178** on the compound select chain, p->pPrior.
179**
180** If it is not NULL, the xSelectCallback() callback is invoked before
181** the walk of the expressions and FROM clause. The xSelectCallback2()
drh979dd1b2017-05-29 14:26:07 +0000182** method is invoked following the walk of the expressions and FROM clause,
183** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
184** and if the expressions and FROM clause both return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +0000185**
186** Return WRC_Continue under normal conditions. Return WRC_Abort if
187** there is an abort request.
188**
189** If the Walker does not have an xSelectCallback() then this routine
190** is a no-op returning WRC_Continue.
191*/
192int sqlite3WalkSelect(Walker *pWalker, Select *p){
193 int rc;
drh3da70a62017-07-07 11:49:48 +0000194 if( p==0 ) return WRC_Continue;
195 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
drh979dd1b2017-05-29 14:26:07 +0000196 do{
197 rc = pWalker->xSelectCallback(pWalker, p);
198 if( rc ) return rc & WRC_Abort;
drhed551b92012-08-23 19:46:11 +0000199 if( sqlite3WalkSelectExpr(pWalker, p)
200 || sqlite3WalkSelectFrom(pWalker, p)
201 ){
drhed551b92012-08-23 19:46:11 +0000202 return WRC_Abort;
203 }
danb290f112014-01-17 14:59:27 +0000204 if( pWalker->xSelectCallback2 ){
205 pWalker->xSelectCallback2(pWalker, p);
drhaa87f9a2013-04-25 00:57:10 +0000206 }
drh7d10d5a2008-08-20 16:35:10 +0000207 p = p->pPrior;
drh979dd1b2017-05-29 14:26:07 +0000208 }while( p!=0 );
209 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +0000210}
drhe40cc162020-05-24 03:01:36 +0000211
212/* Increase the walkerDepth when entering a subquery, and
213** descrease when leaving the subquery.
214*/
215int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
216 UNUSED_PARAMETER(pSelect);
217 pWalker->walkerDepth++;
218 return WRC_Continue;
219}
220void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
221 UNUSED_PARAMETER(pSelect);
222 pWalker->walkerDepth--;
223}
224
225
226/*
227** No-op routine for the parse-tree walker.
228**
229** When this routine is the Walker.xExprCallback then expression trees
230** are walked without any actions being taken at each node. Presumably,
231** when this routine is used for Walker.xExprCallback then
232** Walker.xSelectCallback is set to do something useful for every
233** subquery in the parser tree.
234*/
235int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
236 UNUSED_PARAMETER2(NotUsed, NotUsed2);
237 return WRC_Continue;
238}
239
240/*
241** No-op routine for the parse-tree walker for SELECT statements.
242** subquery in the parser tree.
243*/
244int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
245 UNUSED_PARAMETER2(NotUsed, NotUsed2);
246 return WRC_Continue;
247}