drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 12 | ** This file contains routines used for analyzing expressions and |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** for generating VDBE code that evaluates expressions in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | ** |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 15 | ** $Id: expr.c,v 1.66 2002/05/31 15:51:25 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 19 | |
| 20 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 21 | ** Construct a new expression node and return a pointer to it. Memory |
| 22 | ** for this node is obtained from sqliteMalloc(). The calling function |
| 23 | ** is responsible for making sure the node eventually gets freed. |
| 24 | */ |
| 25 | Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ |
| 26 | Expr *pNew; |
| 27 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 28 | if( pNew==0 ){ |
| 29 | sqliteExprDelete(pLeft); |
| 30 | sqliteExprDelete(pRight); |
| 31 | return 0; |
| 32 | } |
| 33 | pNew->op = op; |
| 34 | pNew->pLeft = pLeft; |
| 35 | pNew->pRight = pRight; |
| 36 | if( pToken ){ |
| 37 | pNew->token = *pToken; |
| 38 | }else{ |
| 39 | pNew->token.z = 0; |
| 40 | pNew->token.n = 0; |
| 41 | } |
| 42 | if( pLeft && pRight ){ |
| 43 | sqliteExprSpan(pNew, &pLeft->span, &pRight->span); |
| 44 | }else{ |
| 45 | pNew->span = pNew->token; |
| 46 | } |
| 47 | return pNew; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | ** Set the Expr.token field of the given expression to span all |
| 52 | ** text between the two given tokens. |
| 53 | */ |
| 54 | void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
| 55 | if( pExpr ){ |
| 56 | pExpr->span.z = pLeft->z; |
| 57 | pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | ** Construct a new expression node for a function with multiple |
| 63 | ** arguments. |
| 64 | */ |
| 65 | Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ |
| 66 | Expr *pNew; |
| 67 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 68 | if( pNew==0 ){ |
| 69 | sqliteExprListDelete(pList); |
| 70 | return 0; |
| 71 | } |
| 72 | pNew->op = TK_FUNCTION; |
| 73 | pNew->pList = pList; |
| 74 | if( pToken ){ |
| 75 | pNew->token = *pToken; |
| 76 | }else{ |
| 77 | pNew->token.z = 0; |
| 78 | pNew->token.n = 0; |
| 79 | } |
| 80 | return pNew; |
| 81 | } |
| 82 | |
| 83 | /* |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 84 | ** Recursively delete an expression tree. |
| 85 | */ |
| 86 | void sqliteExprDelete(Expr *p){ |
| 87 | if( p==0 ) return; |
drh | 75148a2 | 2002-03-03 03:42:31 +0000 | [diff] [blame] | 88 | if( p->pLeft ) sqliteExprDelete(p->pLeft); |
| 89 | if( p->pRight ) sqliteExprDelete(p->pRight); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 90 | if( p->pList ) sqliteExprListDelete(p->pList); |
| 91 | if( p->pSelect ) sqliteSelectDelete(p->pSelect); |
| 92 | sqliteFree(p); |
| 93 | } |
| 94 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 95 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 96 | ** The following group of functions are used to translate the string |
| 97 | ** pointers of tokens in expression from one buffer to another. |
| 98 | ** |
| 99 | ** Normally, the Expr.token.z and Expr.span.z fields point into the |
| 100 | ** original input buffer of an SQL statement. This is usually OK |
| 101 | ** since the SQL statement is executed and the expression is deleted |
| 102 | ** before the input buffer is freed. Making the tokens point to the |
| 103 | ** original input buffer saves many calls to malloc() and thus helps |
| 104 | ** the library to run faster. |
| 105 | ** |
| 106 | ** But sometimes we need an expression to persist past the time when |
| 107 | ** the input buffer is freed. (Example: The SELECT clause of a |
| 108 | ** CREATE VIEW statement contains expressions that must persist for |
| 109 | ** the life of the view.) When that happens we have to make a |
| 110 | ** persistent copy of the input buffer and translate the Expr.token.z |
| 111 | ** and Expr.span.z fields to point to the copy rather than the |
drh | a2ed560 | 2002-02-26 23:55:31 +0000 | [diff] [blame] | 112 | ** original input buffer. The following group of routines handle that |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 113 | ** translation. |
| 114 | ** |
| 115 | ** The "offset" parameter is the distance from the original input buffer |
| 116 | ** to the persistent copy. These routines recursively walk the entire |
| 117 | ** expression tree and shift all tokens by "offset" amount. |
| 118 | ** |
| 119 | ** The work of figuring out the appropriate "offset" and making the |
| 120 | ** presistent copy of the input buffer is done by the calling routine. |
| 121 | */ |
| 122 | void sqliteExprMoveStrings(Expr *p, int offset){ |
| 123 | if( p==0 ) return; |
| 124 | if( p->token.z ) p->token.z += offset; |
| 125 | if( p->span.z ) p->span.z += offset; |
| 126 | if( p->pLeft ) sqliteExprMoveStrings(p->pLeft, offset); |
| 127 | if( p->pRight ) sqliteExprMoveStrings(p->pRight, offset); |
| 128 | if( p->pList ) sqliteExprListMoveStrings(p->pList, offset); |
| 129 | if( p->pSelect ) sqliteSelectMoveStrings(p->pSelect, offset); |
| 130 | } |
| 131 | void sqliteExprListMoveStrings(ExprList *pList, int offset){ |
| 132 | int i; |
| 133 | if( pList==0 ) return; |
| 134 | for(i=0; i<pList->nExpr; i++){ |
| 135 | sqliteExprMoveStrings(pList->a[i].pExpr, offset); |
| 136 | } |
| 137 | } |
| 138 | void sqliteSelectMoveStrings(Select *pSelect, int offset){ |
| 139 | if( pSelect==0 ) return; |
| 140 | sqliteExprListMoveStrings(pSelect->pEList, offset); |
| 141 | sqliteExprMoveStrings(pSelect->pWhere, offset); |
| 142 | sqliteExprListMoveStrings(pSelect->pGroupBy, offset); |
| 143 | sqliteExprMoveStrings(pSelect->pHaving, offset); |
| 144 | sqliteExprListMoveStrings(pSelect->pOrderBy, offset); |
| 145 | sqliteSelectMoveStrings(pSelect->pPrior, offset); |
| 146 | } |
| 147 | |
| 148 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 149 | ** The following group of routines make deep copies of expressions, |
| 150 | ** expression lists, ID lists, and select statements. The copies can |
| 151 | ** be deleted (by being passed to their respective ...Delete() routines) |
| 152 | ** without effecting the originals. |
| 153 | ** |
| 154 | ** Note, however, that the Expr.token.z and Expr.span.z fields point to |
| 155 | ** string space that is allocated separately from the expression tree |
| 156 | ** itself. These routines do NOT duplicate that string space. |
| 157 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 158 | ** The expression list, ID, and source lists return by sqliteExprListDup(), |
| 159 | ** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded |
| 160 | ** by subsequent calls to sqlite*ListAppend() routines. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 161 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 162 | ** Any tables that the SrcList might point to are not duplicated. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 163 | */ |
| 164 | Expr *sqliteExprDup(Expr *p){ |
| 165 | Expr *pNew; |
| 166 | if( p==0 ) return 0; |
| 167 | pNew = sqliteMalloc( sizeof(*p) ); |
| 168 | if( pNew==0 ) return 0; |
| 169 | pNew->op = p->op; |
| 170 | pNew->pLeft = sqliteExprDup(p->pLeft); |
| 171 | pNew->pRight = sqliteExprDup(p->pRight); |
| 172 | pNew->pList = sqliteExprListDup(p->pList); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 173 | pNew->iTable = p->iTable; |
| 174 | pNew->iColumn = p->iColumn; |
| 175 | pNew->iAgg = p->iAgg; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 176 | pNew->token = p->token; |
| 177 | pNew->span = p->span; |
| 178 | pNew->pSelect = sqliteSelectDup(p->pSelect); |
| 179 | return pNew; |
| 180 | } |
| 181 | ExprList *sqliteExprListDup(ExprList *p){ |
| 182 | ExprList *pNew; |
| 183 | int i; |
| 184 | if( p==0 ) return 0; |
| 185 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 186 | if( pNew==0 ) return 0; |
| 187 | pNew->nExpr = p->nExpr; |
| 188 | pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); |
drh | e4697f5 | 2002-05-23 02:09:03 +0000 | [diff] [blame] | 189 | if( pNew->a==0 ) return 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 190 | for(i=0; i<p->nExpr; i++){ |
| 191 | pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr); |
| 192 | pNew->a[i].zName = sqliteStrDup(p->a[i].zName); |
| 193 | pNew->a[i].sortOrder = p->a[i].sortOrder; |
| 194 | pNew->a[i].isAgg = p->a[i].isAgg; |
| 195 | pNew->a[i].done = 0; |
| 196 | } |
| 197 | return pNew; |
| 198 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 199 | SrcList *sqliteSrcListDup(SrcList *p){ |
| 200 | SrcList *pNew; |
| 201 | int i; |
| 202 | if( p==0 ) return 0; |
| 203 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 204 | if( pNew==0 ) return 0; |
| 205 | pNew->nSrc = p->nSrc; |
| 206 | pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) ); |
| 207 | if( pNew->a==0 ) return 0; |
| 208 | for(i=0; i<p->nSrc; i++){ |
| 209 | pNew->a[i].zName = sqliteStrDup(p->a[i].zName); |
| 210 | pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias); |
| 211 | pNew->a[i].jointype = p->a[i].jointype; |
| 212 | pNew->a[i].pTab = 0; |
| 213 | pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect); |
| 214 | pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn); |
| 215 | pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing); |
| 216 | } |
| 217 | return pNew; |
| 218 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 219 | IdList *sqliteIdListDup(IdList *p){ |
| 220 | IdList *pNew; |
| 221 | int i; |
| 222 | if( p==0 ) return 0; |
| 223 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 224 | if( pNew==0 ) return 0; |
| 225 | pNew->nId = p->nId; |
| 226 | pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) ); |
drh | e4697f5 | 2002-05-23 02:09:03 +0000 | [diff] [blame] | 227 | if( pNew->a==0 ) return 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 228 | for(i=0; i<p->nId; i++){ |
| 229 | pNew->a[i].zName = sqliteStrDup(p->a[i].zName); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 230 | pNew->a[i].idx = p->a[i].idx; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 231 | } |
| 232 | return pNew; |
| 233 | } |
| 234 | Select *sqliteSelectDup(Select *p){ |
| 235 | Select *pNew; |
| 236 | if( p==0 ) return 0; |
| 237 | pNew = sqliteMalloc( sizeof(*p) ); |
| 238 | if( pNew==0 ) return 0; |
| 239 | pNew->isDistinct = p->isDistinct; |
| 240 | pNew->pEList = sqliteExprListDup(p->pEList); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 241 | pNew->pSrc = sqliteSrcListDup(p->pSrc); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 242 | pNew->pWhere = sqliteExprDup(p->pWhere); |
| 243 | pNew->pGroupBy = sqliteExprListDup(p->pGroupBy); |
| 244 | pNew->pHaving = sqliteExprDup(p->pHaving); |
| 245 | pNew->pOrderBy = sqliteExprListDup(p->pOrderBy); |
| 246 | pNew->op = p->op; |
| 247 | pNew->pPrior = sqliteSelectDup(p->pPrior); |
| 248 | pNew->nLimit = p->nLimit; |
| 249 | pNew->nOffset = p->nOffset; |
| 250 | pNew->zSelect = 0; |
| 251 | return pNew; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 256 | ** Add a new element to the end of an expression list. If pList is |
| 257 | ** initially NULL, then create a new expression list. |
| 258 | */ |
| 259 | ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
| 260 | int i; |
| 261 | if( pList==0 ){ |
| 262 | pList = sqliteMalloc( sizeof(ExprList) ); |
| 263 | if( pList==0 ){ |
| 264 | sqliteExprDelete(pExpr); |
| 265 | return 0; |
| 266 | } |
| 267 | } |
| 268 | if( (pList->nExpr & 7)==0 ){ |
| 269 | int n = pList->nExpr + 8; |
| 270 | struct ExprList_item *a; |
| 271 | a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); |
| 272 | if( a==0 ){ |
| 273 | sqliteExprDelete(pExpr); |
| 274 | return pList; |
| 275 | } |
| 276 | pList->a = a; |
| 277 | } |
| 278 | if( pExpr || pName ){ |
| 279 | i = pList->nExpr++; |
| 280 | pList->a[i].pExpr = pExpr; |
| 281 | pList->a[i].zName = 0; |
| 282 | if( pName ){ |
| 283 | sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); |
| 284 | sqliteDequote(pList->a[i].zName); |
| 285 | } |
| 286 | } |
| 287 | return pList; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | ** Delete an entire expression list. |
| 292 | */ |
| 293 | void sqliteExprListDelete(ExprList *pList){ |
| 294 | int i; |
| 295 | if( pList==0 ) return; |
| 296 | for(i=0; i<pList->nExpr; i++){ |
| 297 | sqliteExprDelete(pList->a[i].pExpr); |
| 298 | sqliteFree(pList->a[i].zName); |
| 299 | } |
| 300 | sqliteFree(pList->a); |
| 301 | sqliteFree(pList); |
| 302 | } |
| 303 | |
| 304 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 305 | ** Walk an expression tree. Return 1 if the expression is constant |
| 306 | ** and 0 if it involves variables. |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 307 | ** |
| 308 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 309 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 310 | ** a constant. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 311 | */ |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 312 | int sqliteExprIsConstant(Expr *p){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 313 | switch( p->op ){ |
| 314 | case TK_ID: |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 315 | case TK_COLUMN: |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 316 | case TK_DOT: |
| 317 | return 0; |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 318 | case TK_STRING: |
| 319 | return p->token.z[0]=='\''; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 320 | case TK_INTEGER: |
| 321 | case TK_FLOAT: |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 322 | return 1; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 323 | default: { |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 324 | if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0; |
| 325 | if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 326 | if( p->pList ){ |
| 327 | int i; |
| 328 | for(i=0; i<p->pList->nExpr; i++){ |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 329 | if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 332 | return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 335 | return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 339 | ** Return TRUE if the given string is a row-id column name. |
| 340 | */ |
| 341 | static int sqliteIsRowid(const char *z){ |
| 342 | if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1; |
| 343 | if( sqliteStrICmp(z, "ROWID")==0 ) return 1; |
| 344 | if( sqliteStrICmp(z, "OID")==0 ) return 1; |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 349 | ** This routine walks an expression tree and resolves references to |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 350 | ** table columns. Nodes of the form ID.ID or ID resolve into an |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 351 | ** index to the table in the table list and a column offset. The |
| 352 | ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable |
| 353 | ** value is changed to the index of the referenced table in pTabList |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 354 | ** plus the "base" value. The base value will ultimately become the |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 355 | ** VDBE cursor number for a cursor that is pointing into the referenced |
| 356 | ** table. The Expr.iColumn value is changed to the index of the column |
| 357 | ** of the referenced table. The Expr.iColumn value for the special |
| 358 | ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an |
| 359 | ** alias for ROWID. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 360 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 361 | ** We also check for instances of the IN operator. IN comes in two |
| 362 | ** forms: |
| 363 | ** |
| 364 | ** expr IN (exprlist) |
| 365 | ** and |
| 366 | ** expr IN (SELECT ...) |
| 367 | ** |
| 368 | ** The first form is handled by creating a set holding the list |
| 369 | ** of allowed values. The second form causes the SELECT to generate |
| 370 | ** a temporary table. |
| 371 | ** |
| 372 | ** This routine also looks for scalar SELECTs that are part of an expression. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 373 | ** If it finds any, it generates code to write the value of that select |
| 374 | ** into a memory cell. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 375 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 376 | ** Unknown columns or tables provoke an error. The function returns |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 377 | ** the number of errors seen and leaves an error message on pParse->zErrMsg. |
| 378 | */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 379 | int sqliteExprResolveIds( |
| 380 | Parse *pParse, /* The parser context */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 381 | int base, /* VDBE cursor number for first entry in pTabList */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 382 | SrcList *pTabList, /* List of tables used to resolve column names */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 383 | ExprList *pEList, /* List of expressions used to resolve "AS" */ |
| 384 | Expr *pExpr /* The expression to be analyzed. */ |
| 385 | ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 386 | if( pExpr==0 || pTabList==0 ) return 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 387 | assert( base+pTabList->nSrc<=pParse->nTab ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 388 | switch( pExpr->op ){ |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 389 | /* Double-quoted strings (ex: "abc") are used as identifiers if |
| 390 | ** possible. Otherwise they remain as strings. Single-quoted |
| 391 | ** strings (ex: 'abc') are always string literals. |
| 392 | */ |
| 393 | case TK_STRING: { |
| 394 | if( pExpr->token.z[0]=='\'' ) break; |
| 395 | /* Fall thru into the TK_ID case if this is a double-quoted string */ |
| 396 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 397 | /* A lone identifier. Try and match it as follows: |
| 398 | ** |
| 399 | ** 1. To the name of a column of one of the tables in pTabList |
| 400 | ** |
| 401 | ** 2. To the right side of an AS keyword in the column list of |
| 402 | ** a SELECT statement. (For example, match against 'x' in |
| 403 | ** "SELECT a+b AS 'x' FROM t1".) |
| 404 | ** |
| 405 | ** 3. One of the special names "ROWID", "OID", or "_ROWID_". |
| 406 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 407 | case TK_ID: { |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 408 | int cnt = 0; /* Number of matches */ |
| 409 | int i; /* Loop counter */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 410 | char *z; |
| 411 | assert( pExpr->token.z ); |
| 412 | z = sqliteStrNDup(pExpr->token.z, pExpr->token.n); |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 413 | sqliteDequote(z); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 414 | if( z==0 ) return 1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 415 | for(i=0; i<pTabList->nSrc; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 416 | int j; |
| 417 | Table *pTab = pTabList->a[i].pTab; |
| 418 | if( pTab==0 ) continue; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 419 | assert( pTab->nCol>0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 420 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 421 | if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 422 | cnt++; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 423 | pExpr->iTable = i + base; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 424 | if( j==pTab->iPKey ){ |
| 425 | /* Substitute the record number for the INTEGER PRIMARY KEY */ |
| 426 | pExpr->iColumn = -1; |
| 427 | }else{ |
| 428 | pExpr->iColumn = j; |
| 429 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 430 | pExpr->op = TK_COLUMN; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 434 | if( cnt==0 && pEList!=0 ){ |
| 435 | int j; |
| 436 | for(j=0; j<pEList->nExpr; j++){ |
| 437 | char *zAs = pEList->a[j].zName; |
| 438 | if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){ |
| 439 | cnt++; |
| 440 | assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
| 441 | pExpr->op = TK_AS; |
| 442 | pExpr->iColumn = j; |
drh | 75148a2 | 2002-03-03 03:42:31 +0000 | [diff] [blame] | 443 | pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 447 | if( cnt==0 && sqliteIsRowid(z) ){ |
| 448 | pExpr->iColumn = -1; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 449 | pExpr->iTable = base; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 450 | cnt = 1 + (pTabList->nSrc>1); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 451 | pExpr->op = TK_COLUMN; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 452 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 453 | sqliteFree(z); |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 454 | if( cnt==0 && pExpr->token.z[0]!='"' ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 455 | sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 456 | pExpr->token.z, pExpr->token.n, 0); |
| 457 | pParse->nErr++; |
| 458 | return 1; |
| 459 | }else if( cnt>1 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 460 | sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 461 | pExpr->token.z, pExpr->token.n, 0); |
| 462 | pParse->nErr++; |
| 463 | return 1; |
| 464 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 468 | /* A table name and column name: ID.ID */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 469 | case TK_DOT: { |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 470 | int cnt = 0; /* Number of matches */ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 471 | int cntTab = 0; /* Number of matching tables */ |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 472 | int i; /* Loop counter */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 473 | Expr *pLeft, *pRight; /* Left and right subbranches of the expr */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 474 | char *zLeft, *zRight; /* Text of an identifier */ |
| 475 | |
| 476 | pLeft = pExpr->pLeft; |
| 477 | pRight = pExpr->pRight; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 478 | assert( pLeft && pLeft->op==TK_ID && pLeft->token.z ); |
| 479 | assert( pRight && pRight->op==TK_ID && pRight->token.z ); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 480 | zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n); |
| 481 | zRight = sqliteStrNDup(pRight->token.z, pRight->token.n); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 482 | if( zLeft==0 || zRight==0 ){ |
| 483 | sqliteFree(zLeft); |
| 484 | sqliteFree(zRight); |
| 485 | return 1; |
| 486 | } |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 487 | sqliteDequote(zLeft); |
| 488 | sqliteDequote(zRight); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 489 | pExpr->iTable = -1; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 490 | for(i=0; i<pTabList->nSrc; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 491 | int j; |
| 492 | char *zTab; |
| 493 | Table *pTab = pTabList->a[i].pTab; |
| 494 | if( pTab==0 ) continue; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 495 | assert( pTab->nCol>0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 496 | if( pTabList->a[i].zAlias ){ |
| 497 | zTab = pTabList->a[i].zAlias; |
| 498 | }else{ |
| 499 | zTab = pTab->zName; |
| 500 | } |
drh | 094b2bb | 2002-03-13 18:54:07 +0000 | [diff] [blame] | 501 | if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 502 | if( 0==(cntTab++) ) pExpr->iTable = i + base; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 503 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 504 | if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 505 | cnt++; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 506 | pExpr->iTable = i + base; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 507 | if( j==pTab->iPKey ){ |
| 508 | /* Substitute the record number for the INTEGER PRIMARY KEY */ |
| 509 | pExpr->iColumn = -1; |
| 510 | }else{ |
| 511 | pExpr->iColumn = j; |
| 512 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 516 | |
| 517 | /* If we have not already resolved this *.* expression, then maybe |
| 518 | * it is a new.* or old.* trigger argument reference */ |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 519 | if( cnt == 0 && pParse->trigStack != 0 ){ |
| 520 | TriggerStack *pTriggerStack = pParse->trigStack; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 521 | int t = 0; |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 522 | if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){ |
| 523 | pExpr->iTable = pTriggerStack->newIdx; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 524 | cntTab++; |
| 525 | t = 1; |
| 526 | } |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 527 | if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){ |
| 528 | pExpr->iTable = pTriggerStack->oldIdx; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 529 | cntTab++; |
| 530 | t = 1; |
| 531 | } |
| 532 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 533 | if( t ){ |
| 534 | int j; |
| 535 | for(j=0; j < pTriggerStack->pTab->nCol; j++) { |
| 536 | if( sqliteStrICmp(pTriggerStack->pTab->aCol[j].zName, zRight)==0 ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 537 | cnt++; |
| 538 | pExpr->iColumn = j; |
| 539 | } |
| 540 | } |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 541 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 542 | } |
| 543 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 544 | if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){ |
| 545 | cnt = 1; |
| 546 | pExpr->iColumn = -1; |
| 547 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 548 | sqliteFree(zLeft); |
| 549 | sqliteFree(zRight); |
| 550 | if( cnt==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 551 | sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 552 | pLeft->token.z, pLeft->token.n, ".", 1, |
| 553 | pRight->token.z, pRight->token.n, 0); |
| 554 | pParse->nErr++; |
| 555 | return 1; |
| 556 | }else if( cnt>1 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 557 | sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 558 | pLeft->token.z, pLeft->token.n, ".", 1, |
| 559 | pRight->token.z, pRight->token.n, 0); |
| 560 | pParse->nErr++; |
| 561 | return 1; |
| 562 | } |
| 563 | sqliteExprDelete(pLeft); |
| 564 | pExpr->pLeft = 0; |
| 565 | sqliteExprDelete(pRight); |
| 566 | pExpr->pRight = 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 567 | pExpr->op = TK_COLUMN; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 568 | break; |
| 569 | } |
| 570 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 571 | case TK_IN: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 572 | Vdbe *v = sqliteGetVdbe(pParse); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 573 | if( v==0 ) return 1; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 574 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){ |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 575 | return 1; |
| 576 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 577 | if( pExpr->pSelect ){ |
| 578 | /* Case 1: expr IN (SELECT ...) |
| 579 | ** |
| 580 | ** Generate code to write the results of the select into a temporary |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 581 | ** table. The cursor number of the temporary table has already |
| 582 | ** been put in iTable by sqliteExprResolveInSelect(). |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 583 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 584 | pExpr->iTable = pParse->nTab++; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 585 | sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 586 | sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 587 | }else if( pExpr->pList ){ |
| 588 | /* Case 2: expr IN (exprlist) |
| 589 | ** |
| 590 | ** Create a set to put the exprlist values in. The Set id is stored |
| 591 | ** in iTable. |
| 592 | */ |
| 593 | int i, iSet; |
| 594 | for(i=0; i<pExpr->pList->nExpr; i++){ |
| 595 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 596 | if( !sqliteExprIsConstant(pE2) ){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 597 | sqliteSetString(&pParse->zErrMsg, |
| 598 | "right-hand side of IN operator must be constant", 0); |
| 599 | pParse->nErr++; |
| 600 | return 1; |
| 601 | } |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 602 | if( sqliteExprCheck(pParse, pE2, 0, 0) ){ |
| 603 | return 1; |
| 604 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 605 | } |
| 606 | iSet = pExpr->iTable = pParse->nSet++; |
| 607 | for(i=0; i<pExpr->pList->nExpr; i++){ |
| 608 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
| 609 | switch( pE2->op ){ |
| 610 | case TK_FLOAT: |
| 611 | case TK_INTEGER: |
| 612 | case TK_STRING: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 613 | int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 614 | assert( pE2->token.z ); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 615 | sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n); |
| 616 | sqliteVdbeDequoteP3(v, addr); |
| 617 | break; |
| 618 | } |
| 619 | default: { |
| 620 | sqliteExprCode(pParse, pE2); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 621 | sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 622 | break; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | } |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 627 | break; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 628 | } |
| 629 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 630 | case TK_SELECT: { |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 631 | /* This has to be a scalar SELECT. Generate code to put the |
| 632 | ** value of this select in a memory cell and record the number |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 633 | ** of the memory cell in iColumn. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 634 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 635 | pExpr->iColumn = pParse->nMem++; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 636 | if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 637 | return 1; |
| 638 | } |
| 639 | break; |
| 640 | } |
| 641 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 642 | /* For all else, just recursively walk the tree */ |
| 643 | default: { |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 644 | if( pExpr->pLeft |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 645 | && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 646 | return 1; |
| 647 | } |
| 648 | if( pExpr->pRight |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 649 | && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 650 | return 1; |
| 651 | } |
| 652 | if( pExpr->pList ){ |
| 653 | int i; |
| 654 | ExprList *pList = pExpr->pList; |
| 655 | for(i=0; i<pList->nExpr; i++){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 656 | Expr *pArg = pList->a[i].pExpr; |
| 657 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 658 | return 1; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | return 0; |
| 665 | } |
| 666 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 667 | /* |
| 668 | ** Error check the functions in an expression. Make sure all |
| 669 | ** function names are recognized and all functions have the correct |
| 670 | ** number of arguments. Leave an error message in pParse->zErrMsg |
| 671 | ** if anything is amiss. Return the number of errors. |
| 672 | ** |
| 673 | ** if pIsAgg is not null and this expression is an aggregate function |
| 674 | ** (like count(*) or max(value)) then write a 1 into *pIsAgg. |
| 675 | */ |
| 676 | int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ |
| 677 | int nErr = 0; |
| 678 | if( pExpr==0 ) return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 679 | switch( pExpr->op ){ |
| 680 | case TK_FUNCTION: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 681 | int n = pExpr->pList ? pExpr->pList->nExpr : 0; |
| 682 | int no_such_func = 0; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 683 | int wrong_num_args = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 684 | int is_agg = 0; |
| 685 | int i; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 686 | FuncDef *pDef; |
| 687 | |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 688 | pDef = sqliteFindFunction(pParse->db, |
| 689 | pExpr->token.z, pExpr->token.n, n, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 690 | if( pDef==0 ){ |
| 691 | pDef = sqliteFindFunction(pParse->db, |
| 692 | pExpr->token.z, pExpr->token.n, -1, 0); |
| 693 | if( pDef==0 ){ |
| 694 | no_such_func = 1; |
| 695 | }else{ |
| 696 | wrong_num_args = 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 697 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 698 | }else{ |
| 699 | is_agg = pDef->xFunc==0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 700 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 701 | if( is_agg && !allowAgg ){ |
| 702 | sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1, |
| 703 | pExpr->token.z, pExpr->token.n, "()", 2, 0); |
| 704 | pParse->nErr++; |
| 705 | nErr++; |
| 706 | is_agg = 0; |
| 707 | }else if( no_such_func ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 708 | sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, |
| 709 | pExpr->token.z, pExpr->token.n, 0); |
| 710 | pParse->nErr++; |
| 711 | nErr++; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 712 | }else if( wrong_num_args ){ |
| 713 | sqliteSetNString(&pParse->zErrMsg, |
| 714 | "wrong number of arguments to function ",-1, |
| 715 | pExpr->token.z, pExpr->token.n, "()", 2, 0); |
| 716 | pParse->nErr++; |
| 717 | nErr++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 718 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 719 | if( is_agg ) pExpr->op = TK_AGG_FUNCTION; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 720 | if( is_agg && pIsAgg ) *pIsAgg = 1; |
| 721 | for(i=0; nErr==0 && i<n; i++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 722 | nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, |
| 723 | allowAgg && !is_agg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | default: { |
| 727 | if( pExpr->pLeft ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 728 | nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 729 | } |
| 730 | if( nErr==0 && pExpr->pRight ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 731 | nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 732 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 733 | if( nErr==0 && pExpr->pList ){ |
| 734 | int n = pExpr->pList->nExpr; |
| 735 | int i; |
| 736 | for(i=0; nErr==0 && i<n; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 737 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
| 738 | nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 741 | break; |
| 742 | } |
| 743 | } |
| 744 | return nErr; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | ** Generate code into the current Vdbe to evaluate the given |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 749 | ** expression and leave the result on the top of stack. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 750 | */ |
| 751 | void sqliteExprCode(Parse *pParse, Expr *pExpr){ |
| 752 | Vdbe *v = pParse->pVdbe; |
| 753 | int op; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 754 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 755 | switch( pExpr->op ){ |
| 756 | case TK_PLUS: op = OP_Add; break; |
| 757 | case TK_MINUS: op = OP_Subtract; break; |
| 758 | case TK_STAR: op = OP_Multiply; break; |
| 759 | case TK_SLASH: op = OP_Divide; break; |
| 760 | case TK_AND: op = OP_And; break; |
| 761 | case TK_OR: op = OP_Or; break; |
| 762 | case TK_LT: op = OP_Lt; break; |
| 763 | case TK_LE: op = OP_Le; break; |
| 764 | case TK_GT: op = OP_Gt; break; |
| 765 | case TK_GE: op = OP_Ge; break; |
| 766 | case TK_NE: op = OP_Ne; break; |
| 767 | case TK_EQ: op = OP_Eq; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 768 | case TK_ISNULL: op = OP_IsNull; break; |
| 769 | case TK_NOTNULL: op = OP_NotNull; break; |
| 770 | case TK_NOT: op = OP_Not; break; |
| 771 | case TK_UMINUS: op = OP_Negative; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 772 | case TK_BITAND: op = OP_BitAnd; break; |
| 773 | case TK_BITOR: op = OP_BitOr; break; |
| 774 | case TK_BITNOT: op = OP_BitNot; break; |
| 775 | case TK_LSHIFT: op = OP_ShiftLeft; break; |
| 776 | case TK_RSHIFT: op = OP_ShiftRight; break; |
| 777 | case TK_REM: op = OP_Remainder; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 778 | default: break; |
| 779 | } |
| 780 | switch( pExpr->op ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 781 | case TK_COLUMN: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 782 | if( pParse->useAgg ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 783 | sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 784 | }else if( pExpr->iColumn>=0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 785 | sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 786 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 787 | sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 788 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 789 | break; |
| 790 | } |
| 791 | case TK_INTEGER: { |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 792 | sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0); |
| 793 | sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); |
| 794 | break; |
| 795 | } |
| 796 | case TK_FLOAT: { |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 797 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 798 | assert( pExpr->token.z ); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 799 | sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 800 | break; |
| 801 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 802 | case TK_STRING: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 803 | int addr = sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 804 | assert( pExpr->token.z ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 805 | sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n); |
| 806 | sqliteVdbeDequoteP3(v, addr); |
| 807 | break; |
| 808 | } |
| 809 | case TK_NULL: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 810 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 811 | break; |
| 812 | } |
| 813 | case TK_AND: |
| 814 | case TK_OR: |
| 815 | case TK_PLUS: |
| 816 | case TK_STAR: |
| 817 | case TK_MINUS: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 818 | case TK_REM: |
| 819 | case TK_BITAND: |
| 820 | case TK_BITOR: |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 821 | case TK_SLASH: |
| 822 | case TK_LT: |
| 823 | case TK_LE: |
| 824 | case TK_GT: |
| 825 | case TK_GE: |
| 826 | case TK_NE: |
| 827 | case TK_EQ: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 828 | sqliteExprCode(pParse, pExpr->pLeft); |
| 829 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 830 | sqliteVdbeAddOp(v, op, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 831 | break; |
| 832 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 833 | case TK_LSHIFT: |
| 834 | case TK_RSHIFT: { |
| 835 | sqliteExprCode(pParse, pExpr->pRight); |
| 836 | sqliteExprCode(pParse, pExpr->pLeft); |
| 837 | sqliteVdbeAddOp(v, op, 0, 0); |
| 838 | break; |
| 839 | } |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 840 | case TK_CONCAT: { |
| 841 | sqliteExprCode(pParse, pExpr->pLeft); |
| 842 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 843 | sqliteVdbeAddOp(v, OP_Concat, 2, 0); |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 844 | break; |
| 845 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 846 | case TK_UMINUS: { |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 847 | assert( pExpr->pLeft ); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 848 | if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 849 | Token *p = &pExpr->pLeft->token; |
| 850 | char *z = sqliteMalloc( p->n + 2 ); |
| 851 | sprintf(z, "-%.*s", p->n, p->z); |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 852 | if( pExpr->pLeft->op==TK_INTEGER ){ |
| 853 | sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0); |
| 854 | }else{ |
| 855 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 856 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 857 | sqliteVdbeChangeP3(v, -1, z, p->n+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 858 | sqliteFree(z); |
| 859 | break; |
| 860 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 861 | /* Fall through into TK_NOT */ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 862 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 863 | case TK_BITNOT: |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 864 | case TK_NOT: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 865 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 866 | sqliteVdbeAddOp(v, op, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 867 | break; |
| 868 | } |
| 869 | case TK_ISNULL: |
| 870 | case TK_NOTNULL: { |
| 871 | int dest; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 872 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 873 | sqliteExprCode(pParse, pExpr->pLeft); |
| 874 | dest = sqliteVdbeCurrentAddr(v) + 2; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 875 | sqliteVdbeAddOp(v, op, 1, dest); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 876 | sqliteVdbeAddOp(v, OP_AddImm, -1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 877 | break; |
| 878 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 879 | case TK_AGG_FUNCTION: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 880 | sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 881 | break; |
| 882 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 883 | case TK_FUNCTION: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 884 | int i; |
| 885 | ExprList *pList = pExpr->pList; |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 886 | int nExpr = pList ? pList->nExpr : 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 887 | FuncDef *pDef; |
| 888 | pDef = sqliteFindFunction(pParse->db, |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 889 | pExpr->token.z, pExpr->token.n, nExpr, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 890 | assert( pDef!=0 ); |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 891 | for(i=0; i<nExpr; i++){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 892 | sqliteExprCode(pParse, pList->a[i].pExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 893 | } |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 894 | sqliteVdbeAddOp(v, OP_Function, nExpr, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 895 | sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 896 | break; |
| 897 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 898 | case TK_SELECT: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 899 | sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 900 | break; |
| 901 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 902 | case TK_IN: { |
| 903 | int addr; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 904 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 905 | sqliteExprCode(pParse, pExpr->pLeft); |
| 906 | addr = sqliteVdbeCurrentAddr(v); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 907 | sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4); |
| 908 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 909 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 910 | sqliteVdbeAddOp(v, OP_Goto, 0, addr+6); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 911 | if( pExpr->pSelect ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 912 | sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 913 | }else{ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 914 | sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 915 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 916 | sqliteVdbeAddOp(v, OP_AddImm, -1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
| 919 | case TK_BETWEEN: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 920 | sqliteExprCode(pParse, pExpr->pLeft); |
| 921 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 922 | sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); |
| 923 | sqliteVdbeAddOp(v, OP_Ge, 0, 0); |
| 924 | sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
| 925 | sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); |
| 926 | sqliteVdbeAddOp(v, OP_Le, 0, 0); |
| 927 | sqliteVdbeAddOp(v, OP_And, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 928 | break; |
| 929 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 930 | case TK_AS: { |
| 931 | sqliteExprCode(pParse, pExpr->pLeft); |
| 932 | break; |
| 933 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 934 | case TK_CASE: { |
| 935 | int expr_end_label; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 936 | int jumpInst; |
| 937 | int addr; |
| 938 | int nExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 939 | int i; |
| 940 | |
| 941 | assert(pExpr->pList); |
| 942 | assert((pExpr->pList->nExpr % 2) == 0); |
| 943 | assert(pExpr->pList->nExpr > 0); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 944 | nExpr = pExpr->pList->nExpr; |
| 945 | expr_end_label = sqliteVdbeMakeLabel(v); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 946 | if( pExpr->pLeft ){ |
| 947 | sqliteExprCode(pParse, pExpr->pLeft); |
| 948 | } |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 949 | for(i=0; i<nExpr; i=i+2){ |
| 950 | sqliteExprCode(pParse, pExpr->pList->a[i].pExpr); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 951 | if( pExpr->pLeft ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 952 | sqliteVdbeAddOp(v, OP_Dup, 1, 1); |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 953 | jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0); |
| 954 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 955 | }else{ |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 956 | jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 957 | } |
| 958 | sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 959 | sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 960 | addr = sqliteVdbeCurrentAddr(v); |
| 961 | sqliteVdbeChangeP2(v, jumpInst, addr); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 962 | } |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 963 | if( pExpr->pLeft ){ |
| 964 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 965 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 966 | if( pExpr->pRight ){ |
| 967 | sqliteExprCode(pParse, pExpr->pRight); |
| 968 | }else{ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 969 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 970 | } |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 971 | sqliteVdbeResolveLabel(v, expr_end_label); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 972 | } |
| 973 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 974 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* |
| 978 | ** Generate code for a boolean expression such that a jump is made |
| 979 | ** to the label "dest" if the expression is true but execution |
| 980 | ** continues straight thru if the expression is false. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 981 | ** |
| 982 | ** If the expression evaluates to NULL (neither true nor false), then |
| 983 | ** take the jump if the jumpIfNull flag is true. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 984 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 985 | void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 986 | Vdbe *v = pParse->pVdbe; |
| 987 | int op = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 988 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 989 | switch( pExpr->op ){ |
| 990 | case TK_LT: op = OP_Lt; break; |
| 991 | case TK_LE: op = OP_Le; break; |
| 992 | case TK_GT: op = OP_Gt; break; |
| 993 | case TK_GE: op = OP_Ge; break; |
| 994 | case TK_NE: op = OP_Ne; break; |
| 995 | case TK_EQ: op = OP_Eq; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 996 | case TK_ISNULL: op = OP_IsNull; break; |
| 997 | case TK_NOTNULL: op = OP_NotNull; break; |
| 998 | default: break; |
| 999 | } |
| 1000 | switch( pExpr->op ){ |
| 1001 | case TK_AND: { |
| 1002 | int d2 = sqliteVdbeMakeLabel(v); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1003 | sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); |
| 1004 | sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1005 | sqliteVdbeResolveLabel(v, d2); |
| 1006 | break; |
| 1007 | } |
| 1008 | case TK_OR: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1009 | sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 1010 | sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1011 | break; |
| 1012 | } |
| 1013 | case TK_NOT: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1014 | sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1015 | break; |
| 1016 | } |
| 1017 | case TK_LT: |
| 1018 | case TK_LE: |
| 1019 | case TK_GT: |
| 1020 | case TK_GE: |
| 1021 | case TK_NE: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 1022 | case TK_EQ: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1023 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1024 | sqliteExprCode(pParse, pExpr->pRight); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1025 | sqliteVdbeAddOp(v, op, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1026 | break; |
| 1027 | } |
| 1028 | case TK_ISNULL: |
| 1029 | case TK_NOTNULL: { |
| 1030 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1031 | sqliteVdbeAddOp(v, op, 1, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1032 | break; |
| 1033 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1034 | case TK_IN: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1035 | int addr; |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 1036 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1037 | addr = sqliteVdbeCurrentAddr(v); |
| 1038 | sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3); |
| 1039 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1040 | sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1041 | if( pExpr->pSelect ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1042 | sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1043 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1044 | sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1045 | } |
| 1046 | break; |
| 1047 | } |
| 1048 | case TK_BETWEEN: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1049 | int addr; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1050 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1051 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1052 | sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1053 | addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1054 | sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1055 | sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1056 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1057 | sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1058 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1059 | break; |
| 1060 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1061 | default: { |
| 1062 | sqliteExprCode(pParse, pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1063 | sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | /* |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 1070 | ** Generate code for a boolean expression such that a jump is made |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1071 | ** to the label "dest" if the expression is false but execution |
| 1072 | ** continues straight thru if the expression is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1073 | ** |
| 1074 | ** If the expression evaluates to NULL (neither true nor false) then |
| 1075 | ** jump if jumpIfNull is true or fall through if jumpIfNull is false. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1076 | */ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1077 | void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1078 | Vdbe *v = pParse->pVdbe; |
| 1079 | int op = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1080 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1081 | switch( pExpr->op ){ |
| 1082 | case TK_LT: op = OP_Ge; break; |
| 1083 | case TK_LE: op = OP_Gt; break; |
| 1084 | case TK_GT: op = OP_Le; break; |
| 1085 | case TK_GE: op = OP_Lt; break; |
| 1086 | case TK_NE: op = OP_Eq; break; |
| 1087 | case TK_EQ: op = OP_Ne; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1088 | case TK_ISNULL: op = OP_NotNull; break; |
| 1089 | case TK_NOTNULL: op = OP_IsNull; break; |
| 1090 | default: break; |
| 1091 | } |
| 1092 | switch( pExpr->op ){ |
| 1093 | case TK_AND: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1094 | sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 1095 | sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | } |
| 1098 | case TK_OR: { |
| 1099 | int d2 = sqliteVdbeMakeLabel(v); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1100 | sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); |
| 1101 | sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1102 | sqliteVdbeResolveLabel(v, d2); |
| 1103 | break; |
| 1104 | } |
| 1105 | case TK_NOT: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1106 | sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1107 | break; |
| 1108 | } |
| 1109 | case TK_LT: |
| 1110 | case TK_LE: |
| 1111 | case TK_GT: |
| 1112 | case TK_GE: |
| 1113 | case TK_NE: |
| 1114 | case TK_EQ: { |
| 1115 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1116 | sqliteExprCode(pParse, pExpr->pRight); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1117 | sqliteVdbeAddOp(v, op, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1118 | break; |
| 1119 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1120 | case TK_ISNULL: |
| 1121 | case TK_NOTNULL: { |
| 1122 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1123 | sqliteVdbeAddOp(v, op, 1, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1124 | break; |
| 1125 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1126 | case TK_IN: { |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1127 | int addr; |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 1128 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1129 | addr = sqliteVdbeCurrentAddr(v); |
| 1130 | sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3); |
| 1131 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1132 | sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1133 | if( pExpr->pSelect ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1134 | sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1135 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1136 | sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1137 | } |
| 1138 | break; |
| 1139 | } |
| 1140 | case TK_BETWEEN: { |
| 1141 | int addr; |
| 1142 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1143 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1144 | sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); |
| 1145 | addr = sqliteVdbeCurrentAddr(v); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1146 | sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1147 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1148 | sqliteVdbeAddOp(v, OP_Goto, 0, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1149 | sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1150 | sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1151 | break; |
| 1152 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1153 | default: { |
| 1154 | sqliteExprCode(pParse, pExpr); |
drh | 461c281 | 2002-05-30 02:35:11 +0000 | [diff] [blame] | 1155 | sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1156 | break; |
| 1157 | } |
| 1158 | } |
| 1159 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* |
| 1162 | ** Do a deep comparison of two expression trees. Return TRUE (non-zero) |
| 1163 | ** if they are identical and return FALSE if they differ in any way. |
| 1164 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1165 | int sqliteExprCompare(Expr *pA, Expr *pB){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1166 | int i; |
| 1167 | if( pA==0 ){ |
| 1168 | return pB==0; |
| 1169 | }else if( pB==0 ){ |
| 1170 | return 0; |
| 1171 | } |
| 1172 | if( pA->op!=pB->op ) return 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1173 | if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0; |
| 1174 | if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1175 | if( pA->pList ){ |
| 1176 | if( pB->pList==0 ) return 0; |
| 1177 | if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; |
| 1178 | for(i=0; i<pA->pList->nExpr; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1179 | if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1180 | return 0; |
| 1181 | } |
| 1182 | } |
| 1183 | }else if( pB->pList ){ |
| 1184 | return 0; |
| 1185 | } |
| 1186 | if( pA->pSelect || pB->pSelect ) return 0; |
| 1187 | if( pA->token.z ){ |
| 1188 | if( pB->token.z==0 ) return 0; |
| 1189 | if( pB->token.n!=pA->token.n ) return 0; |
| 1190 | if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0; |
| 1191 | } |
| 1192 | return 1; |
| 1193 | } |
| 1194 | |
| 1195 | /* |
| 1196 | ** Add a new element to the pParse->aAgg[] array and return its index. |
| 1197 | */ |
| 1198 | static int appendAggInfo(Parse *pParse){ |
| 1199 | if( (pParse->nAgg & 0x7)==0 ){ |
| 1200 | int amt = pParse->nAgg + 8; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1201 | AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); |
| 1202 | if( aAgg==0 ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1203 | return -1; |
| 1204 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1205 | pParse->aAgg = aAgg; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1206 | } |
| 1207 | memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); |
| 1208 | return pParse->nAgg++; |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | ** Analyze the given expression looking for aggregate functions and |
| 1213 | ** for variables that need to be added to the pParse->aAgg[] array. |
| 1214 | ** Make additional entries to the pParse->aAgg[] array as necessary. |
| 1215 | ** |
| 1216 | ** This routine should only be called after the expression has been |
| 1217 | ** analyzed by sqliteExprResolveIds() and sqliteExprCheck(). |
| 1218 | ** |
| 1219 | ** If errors are seen, leave an error message in zErrMsg and return |
| 1220 | ** the number of errors. |
| 1221 | */ |
| 1222 | int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ |
| 1223 | int i; |
| 1224 | AggExpr *aAgg; |
| 1225 | int nErr = 0; |
| 1226 | |
| 1227 | if( pExpr==0 ) return 0; |
| 1228 | switch( pExpr->op ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1229 | case TK_COLUMN: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1230 | aAgg = pParse->aAgg; |
| 1231 | for(i=0; i<pParse->nAgg; i++){ |
| 1232 | if( aAgg[i].isAgg ) continue; |
| 1233 | if( aAgg[i].pExpr->iTable==pExpr->iTable |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1234 | && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1235 | break; |
| 1236 | } |
| 1237 | } |
| 1238 | if( i>=pParse->nAgg ){ |
| 1239 | i = appendAggInfo(pParse); |
| 1240 | if( i<0 ) return 1; |
| 1241 | pParse->aAgg[i].isAgg = 0; |
| 1242 | pParse->aAgg[i].pExpr = pExpr; |
| 1243 | } |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 1244 | pExpr->iAgg = i; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1245 | break; |
| 1246 | } |
| 1247 | case TK_AGG_FUNCTION: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1248 | aAgg = pParse->aAgg; |
| 1249 | for(i=0; i<pParse->nAgg; i++){ |
| 1250 | if( !aAgg[i].isAgg ) continue; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1251 | if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1252 | break; |
| 1253 | } |
| 1254 | } |
| 1255 | if( i>=pParse->nAgg ){ |
| 1256 | i = appendAggInfo(pParse); |
| 1257 | if( i<0 ) return 1; |
| 1258 | pParse->aAgg[i].isAgg = 1; |
| 1259 | pParse->aAgg[i].pExpr = pExpr; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1260 | pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db, |
drh | f55f25f | 2002-02-28 01:46:11 +0000 | [diff] [blame] | 1261 | pExpr->token.z, pExpr->token.n, |
| 1262 | pExpr->pList ? pExpr->pList->nExpr : 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1263 | } |
| 1264 | pExpr->iAgg = i; |
| 1265 | break; |
| 1266 | } |
| 1267 | default: { |
| 1268 | if( pExpr->pLeft ){ |
| 1269 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft); |
| 1270 | } |
| 1271 | if( nErr==0 && pExpr->pRight ){ |
| 1272 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight); |
| 1273 | } |
| 1274 | if( nErr==0 && pExpr->pList ){ |
| 1275 | int n = pExpr->pList->nExpr; |
| 1276 | int i; |
| 1277 | for(i=0; nErr==0 && i<n; i++){ |
| 1278 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); |
| 1279 | } |
| 1280 | } |
| 1281 | break; |
| 1282 | } |
| 1283 | } |
| 1284 | return nErr; |
| 1285 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1286 | |
| 1287 | /* |
| 1288 | ** Locate a user function given a name and a number of arguments. |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1289 | ** Return a pointer to the FuncDef structure that defines that |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1290 | ** function, or return NULL if the function does not exist. |
| 1291 | ** |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1292 | ** If the createFlag argument is true, then a new (blank) FuncDef |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1293 | ** structure is created and liked into the "db" structure if a |
| 1294 | ** no matching function previously existed. When createFlag is true |
| 1295 | ** and the nArg parameter is -1, then only a function that accepts |
| 1296 | ** any number of arguments will be returned. |
| 1297 | ** |
| 1298 | ** If createFlag is false and nArg is -1, then the first valid |
| 1299 | ** function found is returned. A function is valid if either xFunc |
| 1300 | ** or xStep is non-zero. |
| 1301 | */ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1302 | FuncDef *sqliteFindFunction( |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1303 | sqlite *db, /* An open database */ |
| 1304 | const char *zName, /* Name of the function. Not null-terminated */ |
| 1305 | int nName, /* Number of characters in the name */ |
| 1306 | int nArg, /* Number of arguments. -1 means any number */ |
| 1307 | int createFlag /* Create new entry if true and does not otherwise exist */ |
| 1308 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1309 | FuncDef *pFirst, *p, *pMaybe; |
| 1310 | pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName); |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1311 | if( p && !createFlag && nArg<0 ){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1312 | while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } |
| 1313 | return p; |
| 1314 | } |
| 1315 | pMaybe = 0; |
| 1316 | while( p && p->nArg!=nArg ){ |
| 1317 | if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; |
| 1318 | p = p->pNext; |
| 1319 | } |
| 1320 | if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ |
| 1321 | return 0; |
| 1322 | } |
| 1323 | if( p==0 && pMaybe ){ |
| 1324 | assert( createFlag==0 ); |
| 1325 | return pMaybe; |
| 1326 | } |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 1327 | if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1328 | p->nArg = nArg; |
| 1329 | p->pNext = pFirst; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1330 | sqliteHashInsert(&db->aFunc, zName, nName, (void*)p); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1331 | } |
| 1332 | return p; |
| 1333 | } |