blob: ee7d0ca9c0cc23dec27ae115ea5dd821ea7b1faf [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*/
25static int walkWindowList(Walker *pWalker, Window *pList){
26 Window *pWin;
27 for(pWin=pList; pWin; pWin=pWin->pNextWin){
28 if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
29 if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
30 if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;
31 }
32 return WRC_Continue;
33}
34#endif
35
drh7d10d5a2008-08-20 16:35:10 +000036/*
37** Walk an expression tree. Invoke the callback once for each node
peter.d.reid60ec9142014-09-06 16:39:46 +000038** of the expression, while descending. (In other words, the callback
drh7d10d5a2008-08-20 16:35:10 +000039** is invoked before visiting children.)
40**
41** The return value from the callback should be one of the WRC_*
42** constants to specify how to proceed with the walk.
43**
44** WRC_Continue Continue descending down the tree.
45**
drh567bd442017-03-07 12:18:23 +000046** WRC_Prune Do not descend into child nodes, but allow
drh7d10d5a2008-08-20 16:35:10 +000047** the walk to continue with sibling nodes.
48**
49** WRC_Abort Do no more callbacks. Unwind the stack and
drh567bd442017-03-07 12:18:23 +000050** return from the top-level walk call.
drh7d10d5a2008-08-20 16:35:10 +000051**
52** The return value from this routine is WRC_Abort to abandon the tree walk
53** and WRC_Continue to continue.
54*/
drh50922cf2016-01-11 14:19:14 +000055static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +000056 int rc;
drh0a9aa222009-04-08 12:21:30 +000057 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
drh0a9aa222009-04-08 12:21:30 +000058 testcase( ExprHasProperty(pExpr, EP_Reduced) );
drhdceed862017-08-17 19:23:16 +000059 while(1){
60 rc = pWalker->xExprCallback(pWalker, pExpr);
61 if( rc ) return rc & WRC_Abort;
62 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
63 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
64 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
65 if( pExpr->pRight ){
dan4f9adee2019-07-13 16:22:50 +000066 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
drhdceed862017-08-17 19:23:16 +000067 pExpr = pExpr->pRight;
68 continue;
69 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
dan4f9adee2019-07-13 16:22:50 +000070 assert( !ExprHasProperty(pExpr, EP_WinFunc) );
drhdceed862017-08-17 19:23:16 +000071 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
dan8117f112019-07-10 20:16:53 +000072 }else{
73 if( pExpr->x.pList ){
74 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
75 }
dan867be212018-06-25 11:42:08 +000076#ifndef SQLITE_OMIT_WINDOWFUNC
dan4f9adee2019-07-13 16:22:50 +000077 if( ExprHasProperty(pExpr, EP_WinFunc) ){
78 if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort;
dan8117f112019-07-10 20:16:53 +000079 }
dan867be212018-06-25 11:42:08 +000080#endif
dan8117f112019-07-10 20:16:53 +000081 }
drhc2d14a92017-07-07 12:58:30 +000082 }
drhdceed862017-08-17 19:23:16 +000083 break;
drh7d10d5a2008-08-20 16:35:10 +000084 }
drh922802c2016-08-10 18:56:32 +000085 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +000086}
drh50922cf2016-01-11 14:19:14 +000087int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
88 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
89}
drh7d10d5a2008-08-20 16:35:10 +000090
91/*
92** Call sqlite3WalkExpr() for every expression in list p or until
93** an abort request is seen.
94*/
95int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
drhf7828b52009-06-15 23:15:59 +000096 int i;
drh7d10d5a2008-08-20 16:35:10 +000097 struct ExprList_item *pItem;
98 if( p ){
99 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
100 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
101 }
102 }
drhf7828b52009-06-15 23:15:59 +0000103 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +0000104}
105
106/*
107** Walk all expressions associated with SELECT statement p. Do
108** not invoke the SELECT callback on p, but do (of course) invoke
109** any expr callbacks and SELECT callbacks that come from subqueries.
110** Return WRC_Abort or WRC_Continue.
111*/
112int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
113 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
114 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
115 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
116 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
117 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
118 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
dand9995032019-01-23 16:59:24 +0000119#if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
120 {
121 Parse *pParse = pWalker->pParse;
122 if( pParse && IN_RENAME_OBJECT ){
dana1ac0352019-07-19 10:31:29 +0000123 /* The following may return WRC_Abort if there are unresolvable
124 ** symbols (e.g. a table that does not exist) in a window definition. */
drh6a02f232019-01-24 04:44:54 +0000125 int rc = walkWindowList(pWalker, p->pWinDefn);
drh6a02f232019-01-24 04:44:54 +0000126 return rc;
dand9995032019-01-23 16:59:24 +0000127 }
128 }
129#endif
drh7d10d5a2008-08-20 16:35:10 +0000130 return WRC_Continue;
131}
132
133/*
134** Walk the parse trees associated with all subqueries in the
135** FROM clause of SELECT statement p. Do not invoke the select
136** callback on p, but do invoke it on each FROM clause subquery
137** and on any subqueries further down in the tree. Return
138** WRC_Abort or WRC_Continue;
139*/
140int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
141 SrcList *pSrc;
142 int i;
143 struct SrcList_item *pItem;
144
145 pSrc = p->pSrc;
drh9d9c41e2017-10-31 03:40:15 +0000146 assert( pSrc!=0 );
147 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
148 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
149 return WRC_Abort;
150 }
151 if( pItem->fg.isTabFunc
152 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
153 ){
154 return WRC_Abort;
drh7d10d5a2008-08-20 16:35:10 +0000155 }
156 }
157 return WRC_Continue;
158}
159
160/*
161** Call sqlite3WalkExpr() for every expression in Select statement p.
162** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
danb290f112014-01-17 14:59:27 +0000163** on the compound select chain, p->pPrior.
164**
165** If it is not NULL, the xSelectCallback() callback is invoked before
166** the walk of the expressions and FROM clause. The xSelectCallback2()
drh979dd1b2017-05-29 14:26:07 +0000167** method is invoked following the walk of the expressions and FROM clause,
168** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
169** and if the expressions and FROM clause both return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +0000170**
171** Return WRC_Continue under normal conditions. Return WRC_Abort if
172** there is an abort request.
173**
174** If the Walker does not have an xSelectCallback() then this routine
175** is a no-op returning WRC_Continue.
176*/
177int sqlite3WalkSelect(Walker *pWalker, Select *p){
178 int rc;
drh3da70a62017-07-07 11:49:48 +0000179 if( p==0 ) return WRC_Continue;
180 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
drh979dd1b2017-05-29 14:26:07 +0000181 do{
182 rc = pWalker->xSelectCallback(pWalker, p);
183 if( rc ) return rc & WRC_Abort;
drhed551b92012-08-23 19:46:11 +0000184 if( sqlite3WalkSelectExpr(pWalker, p)
185 || sqlite3WalkSelectFrom(pWalker, p)
186 ){
drhed551b92012-08-23 19:46:11 +0000187 return WRC_Abort;
188 }
danb290f112014-01-17 14:59:27 +0000189 if( pWalker->xSelectCallback2 ){
190 pWalker->xSelectCallback2(pWalker, p);
drhaa87f9a2013-04-25 00:57:10 +0000191 }
drh7d10d5a2008-08-20 16:35:10 +0000192 p = p->pPrior;
drh979dd1b2017-05-29 14:26:07 +0000193 }while( p!=0 );
194 return WRC_Continue;
drh7d10d5a2008-08-20 16:35:10 +0000195}