drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 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. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 14 | */ |
| 15 | #include "sqliteInt.h" |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | |
dan | d999503 | 2019-01-23 16:59:24 +0000 | [diff] [blame] | 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){ |
| 26 | Window *pWin; |
| 27 | for(pWin=pList; pWin; pWin=pWin->pNextWin){ |
drh | c5cc870 | 2019-07-19 15:00:32 +0000 | [diff] [blame] | 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; |
dan | d999503 | 2019-01-23 16:59:24 +0000 | [diff] [blame] | 44 | } |
| 45 | return WRC_Continue; |
| 46 | } |
| 47 | #endif |
| 48 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 49 | /* |
| 50 | ** Walk an expression tree. Invoke the callback once for each node |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 51 | ** of the expression, while descending. (In other words, the callback |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 52 | ** is invoked before visiting children.) |
| 53 | ** |
| 54 | ** The return value from the callback should be one of the WRC_* |
| 55 | ** constants to specify how to proceed with the walk. |
| 56 | ** |
| 57 | ** WRC_Continue Continue descending down the tree. |
| 58 | ** |
drh | 567bd44 | 2017-03-07 12:18:23 +0000 | [diff] [blame] | 59 | ** WRC_Prune Do not descend into child nodes, but allow |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 60 | ** the walk to continue with sibling nodes. |
| 61 | ** |
| 62 | ** WRC_Abort Do no more callbacks. Unwind the stack and |
drh | 567bd44 | 2017-03-07 12:18:23 +0000 | [diff] [blame] | 63 | ** return from the top-level walk call. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 64 | ** |
| 65 | ** The return value from this routine is WRC_Abort to abandon the tree walk |
| 66 | ** and WRC_Continue to continue. |
| 67 | */ |
drh | 50922cf | 2016-01-11 14:19:14 +0000 | [diff] [blame] | 68 | static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 69 | int rc; |
drh | 0a9aa22 | 2009-04-08 12:21:30 +0000 | [diff] [blame] | 70 | testcase( ExprHasProperty(pExpr, EP_TokenOnly) ); |
drh | 0a9aa22 | 2009-04-08 12:21:30 +0000 | [diff] [blame] | 71 | testcase( ExprHasProperty(pExpr, EP_Reduced) ); |
drh | dceed86 | 2017-08-17 19:23:16 +0000 | [diff] [blame] | 72 | while(1){ |
| 73 | rc = pWalker->xExprCallback(pWalker, pExpr); |
| 74 | if( rc ) return rc & WRC_Abort; |
| 75 | if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ |
| 76 | if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; |
| 77 | assert( pExpr->x.pList==0 || pExpr->pRight==0 ); |
| 78 | if( pExpr->pRight ){ |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 79 | assert( !ExprHasProperty(pExpr, EP_WinFunc) ); |
drh | dceed86 | 2017-08-17 19:23:16 +0000 | [diff] [blame] | 80 | pExpr = pExpr->pRight; |
| 81 | continue; |
| 82 | }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 83 | assert( !ExprHasProperty(pExpr, EP_WinFunc) ); |
drh | dceed86 | 2017-08-17 19:23:16 +0000 | [diff] [blame] | 84 | if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; |
dan | 8117f11 | 2019-07-10 20:16:53 +0000 | [diff] [blame] | 85 | }else{ |
| 86 | if( pExpr->x.pList ){ |
| 87 | if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; |
| 88 | } |
dan | 867be21 | 2018-06-25 11:42:08 +0000 | [diff] [blame] | 89 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 90 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 91 | if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort; |
dan | 8117f11 | 2019-07-10 20:16:53 +0000 | [diff] [blame] | 92 | } |
dan | 867be21 | 2018-06-25 11:42:08 +0000 | [diff] [blame] | 93 | #endif |
dan | 8117f11 | 2019-07-10 20:16:53 +0000 | [diff] [blame] | 94 | } |
drh | c2d14a9 | 2017-07-07 12:58:30 +0000 | [diff] [blame] | 95 | } |
drh | dceed86 | 2017-08-17 19:23:16 +0000 | [diff] [blame] | 96 | break; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 97 | } |
drh | 922802c | 2016-08-10 18:56:32 +0000 | [diff] [blame] | 98 | return WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 99 | } |
drh | 50922cf | 2016-01-11 14:19:14 +0000 | [diff] [blame] | 100 | int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){ |
| 101 | return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue; |
| 102 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | ** Call sqlite3WalkExpr() for every expression in list p or until |
| 106 | ** an abort request is seen. |
| 107 | */ |
| 108 | int sqlite3WalkExprList(Walker *pWalker, ExprList *p){ |
drh | f7828b5 | 2009-06-15 23:15:59 +0000 | [diff] [blame] | 109 | int i; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 110 | struct ExprList_item *pItem; |
| 111 | if( p ){ |
| 112 | for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ |
| 113 | if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort; |
| 114 | } |
| 115 | } |
drh | f7828b5 | 2009-06-15 23:15:59 +0000 | [diff] [blame] | 116 | return WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | ** Walk all expressions associated with SELECT statement p. Do |
| 121 | ** not invoke the SELECT callback on p, but do (of course) invoke |
| 122 | ** any expr callbacks and SELECT callbacks that come from subqueries. |
| 123 | ** Return WRC_Abort or WRC_Continue. |
| 124 | */ |
| 125 | int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ |
| 126 | if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort; |
| 127 | if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort; |
| 128 | if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort; |
| 129 | if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; |
| 130 | if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; |
| 131 | if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; |
dan | d999503 | 2019-01-23 16:59:24 +0000 | [diff] [blame] | 132 | #if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE) |
| 133 | { |
| 134 | Parse *pParse = pWalker->pParse; |
| 135 | if( pParse && IN_RENAME_OBJECT ){ |
dan | a1ac035 | 2019-07-19 10:31:29 +0000 | [diff] [blame] | 136 | /* The following may return WRC_Abort if there are unresolvable |
| 137 | ** symbols (e.g. a table that does not exist) in a window definition. */ |
drh | 6a02f23 | 2019-01-24 04:44:54 +0000 | [diff] [blame] | 138 | int rc = walkWindowList(pWalker, p->pWinDefn); |
drh | 6a02f23 | 2019-01-24 04:44:54 +0000 | [diff] [blame] | 139 | return rc; |
dan | d999503 | 2019-01-23 16:59:24 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | #endif |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 143 | return WRC_Continue; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | ** Walk the parse trees associated with all subqueries in the |
| 148 | ** FROM clause of SELECT statement p. Do not invoke the select |
| 149 | ** callback on p, but do invoke it on each FROM clause subquery |
| 150 | ** and on any subqueries further down in the tree. Return |
| 151 | ** WRC_Abort or WRC_Continue; |
| 152 | */ |
| 153 | int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ |
| 154 | SrcList *pSrc; |
| 155 | int i; |
| 156 | struct SrcList_item *pItem; |
| 157 | |
| 158 | pSrc = p->pSrc; |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 159 | assert( pSrc!=0 ); |
| 160 | for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ |
| 161 | if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){ |
| 162 | return WRC_Abort; |
| 163 | } |
| 164 | if( pItem->fg.isTabFunc |
| 165 | && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg) |
| 166 | ){ |
| 167 | return WRC_Abort; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | return WRC_Continue; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | ** Call sqlite3WalkExpr() for every expression in Select statement p. |
| 175 | ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 176 | ** on the compound select chain, p->pPrior. |
| 177 | ** |
| 178 | ** If it is not NULL, the xSelectCallback() callback is invoked before |
| 179 | ** the walk of the expressions and FROM clause. The xSelectCallback2() |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 180 | ** method is invoked following the walk of the expressions and FROM clause, |
| 181 | ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL |
| 182 | ** and if the expressions and FROM clause both return WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 183 | ** |
| 184 | ** Return WRC_Continue under normal conditions. Return WRC_Abort if |
| 185 | ** there is an abort request. |
| 186 | ** |
| 187 | ** If the Walker does not have an xSelectCallback() then this routine |
| 188 | ** is a no-op returning WRC_Continue. |
| 189 | */ |
| 190 | int sqlite3WalkSelect(Walker *pWalker, Select *p){ |
| 191 | int rc; |
drh | 3da70a6 | 2017-07-07 11:49:48 +0000 | [diff] [blame] | 192 | if( p==0 ) return WRC_Continue; |
| 193 | if( pWalker->xSelectCallback==0 ) return WRC_Continue; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 194 | do{ |
| 195 | rc = pWalker->xSelectCallback(pWalker, p); |
| 196 | if( rc ) return rc & WRC_Abort; |
drh | ed551b9 | 2012-08-23 19:46:11 +0000 | [diff] [blame] | 197 | if( sqlite3WalkSelectExpr(pWalker, p) |
| 198 | || sqlite3WalkSelectFrom(pWalker, p) |
| 199 | ){ |
drh | ed551b9 | 2012-08-23 19:46:11 +0000 | [diff] [blame] | 200 | return WRC_Abort; |
| 201 | } |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 202 | if( pWalker->xSelectCallback2 ){ |
| 203 | pWalker->xSelectCallback2(pWalker, p); |
drh | aa87f9a | 2013-04-25 00:57:10 +0000 | [diff] [blame] | 204 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 205 | p = p->pPrior; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 206 | }while( p!=0 ); |
| 207 | return WRC_Continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 208 | } |