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