1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains routines used for analyzing expressions and 13 ** for generating VDBE code that evaluates expressions in SQLite. 14 ** 15 ** $Id: expr.c,v 1.98 2003/07/30 12:34:12 drh Exp $ 16 */ 17 #include "sqliteInt.h" 18 #include <ctype.h> 19 20 /* 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 assert( pToken->dyn==0 ); 38 pNew->token = *pToken; 39 pNew->span = *pToken; 40 }else{ 41 pNew->token.dyn = 0; 42 pNew->token.z = 0; 43 pNew->token.n = 0; 44 if( pLeft && pRight ){ 45 sqliteExprSpan(pNew, &pLeft->span, &pRight->span); 46 }else{ 47 pNew->span = pNew->token; 48 } 49 } 50 return pNew; 51 } 52 53 /* 54 ** Set the Expr.span field of the given expression to span all 55 ** text between the two given tokens. 56 */ 57 void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 58 if( pExpr && pRight && pRight->z && pLeft && pLeft->z ){ 59 if( pLeft->dyn==0 && pRight->dyn==0 ){ 60 pExpr->span.z = pLeft->z; 61 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); 62 }else{ 63 pExpr->span.z = 0; 64 pExpr->span.n = 0; 65 pExpr->span.dyn = 0; 66 } 67 } 68 } 69 70 /* 71 ** Construct a new expression node for a function with multiple 72 ** arguments. 73 */ 74 Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ 75 Expr *pNew; 76 pNew = sqliteMalloc( sizeof(Expr) ); 77 if( pNew==0 ){ 78 sqliteExprListDelete(pList); 79 return 0; 80 } 81 pNew->op = TK_FUNCTION; 82 pNew->pList = pList; 83 pNew->token.dyn = 0; 84 if( pToken ){ 85 assert( pToken->dyn==0 ); 86 pNew->token = *pToken; 87 }else{ 88 pNew->token.z = 0; 89 pNew->token.n = 0; 90 } 91 pNew->span = pNew->token; 92 return pNew; 93 } 94 95 /* 96 ** Recursively delete an expression tree. 97 */ 98 void sqliteExprDelete(Expr *p){ 99 if( p==0 ) return; 100 if( p->span.dyn && p->span.z ) sqliteFree((char*)p->span.z); 101 if( p->token.dyn && p->token.z ) sqliteFree((char*)p->token.z); 102 if( p->pLeft ) sqliteExprDelete(p->pLeft); 103 if( p->pRight ) sqliteExprDelete(p->pRight); 104 if( p->pList ) sqliteExprListDelete(p->pList); 105 if( p->pSelect ) sqliteSelectDelete(p->pSelect); 106 sqliteFree(p); 107 } 108 109 110 /* 111 ** The following group of routines make deep copies of expressions, 112 ** expression lists, ID lists, and select statements. The copies can 113 ** be deleted (by being passed to their respective ...Delete() routines) 114 ** without effecting the originals. 115 ** 116 ** The expression list, ID, and source lists return by sqliteExprListDup(), 117 ** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded 118 ** by subsequent calls to sqlite*ListAppend() routines. 119 ** 120 ** Any tables that the SrcList might point to are not duplicated. 121 */ 122 Expr *sqliteExprDup(Expr *p){ 123 Expr *pNew; 124 if( p==0 ) return 0; 125 pNew = sqliteMallocRaw( sizeof(*p) ); 126 if( pNew==0 ) return 0; 127 memcpy(pNew, p, sizeof(*pNew)); 128 if( p->token.z!=0 ){ 129 pNew->token.z = sqliteStrDup(p->token.z); 130 pNew->token.dyn = 1; 131 }else{ 132 pNew->token.z = 0; 133 pNew->token.n = 0; 134 pNew->token.dyn = 0; 135 } 136 pNew->span.z = 0; 137 pNew->span.n = 0; 138 pNew->span.dyn = 0; 139 pNew->pLeft = sqliteExprDup(p->pLeft); 140 pNew->pRight = sqliteExprDup(p->pRight); 141 pNew->pList = sqliteExprListDup(p->pList); 142 pNew->pSelect = sqliteSelectDup(p->pSelect); 143 return pNew; 144 } 145 void sqliteTokenCopy(Token *pTo, Token *pFrom){ 146 if( pTo->dyn ) sqliteFree((char*)pTo->z); 147 if( pFrom->z ){ 148 pTo->n = pFrom->n; 149 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 150 pTo->dyn = 1; 151 }else{ 152 pTo->n = 0; 153 pTo->z = 0; 154 pTo->dyn = 0; 155 } 156 } 157 ExprList *sqliteExprListDup(ExprList *p){ 158 ExprList *pNew; 159 int i; 160 if( p==0 ) return 0; 161 pNew = sqliteMalloc( sizeof(*pNew) ); 162 if( pNew==0 ) return 0; 163 pNew->nExpr = pNew->nAlloc = p->nExpr; 164 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 165 if( pNew->a==0 ) return 0; 166 for(i=0; i<p->nExpr; i++){ 167 Expr *pNewExpr, *pOldExpr; 168 pNew->a[i].pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr); 169 if( pOldExpr->span.z!=0 && pNewExpr ){ 170 /* Always make a copy of the span for top-level expressions in the 171 ** expression list. The logic in SELECT processing that determines 172 ** the names of columns in the result set needs this information */ 173 sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span); 174 } 175 assert( pNewExpr==0 || pNewExpr->span.z!=0 176 || pOldExpr->span.z==0 || sqlite_malloc_failed ); 177 pNew->a[i].zName = sqliteStrDup(p->a[i].zName); 178 pNew->a[i].sortOrder = p->a[i].sortOrder; 179 pNew->a[i].isAgg = p->a[i].isAgg; 180 pNew->a[i].done = 0; 181 } 182 return pNew; 183 } 184 SrcList *sqliteSrcListDup(SrcList *p){ 185 SrcList *pNew; 186 int i; 187 int nByte; 188 if( p==0 ) return 0; 189 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 190 pNew = sqliteMalloc( nByte ); 191 if( pNew==0 ) return 0; 192 pNew->nSrc = pNew->nAlloc = p->nSrc; 193 for(i=0; i<p->nSrc; i++){ 194 pNew->a[i].zDatabase = sqliteStrDup(p->a[i].zDatabase); 195 pNew->a[i].zName = sqliteStrDup(p->a[i].zName); 196 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias); 197 pNew->a[i].jointype = p->a[i].jointype; 198 pNew->a[i].iCursor = p->a[i].iCursor; 199 pNew->a[i].pTab = 0; 200 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect); 201 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn); 202 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing); 203 } 204 return pNew; 205 } 206 IdList *sqliteIdListDup(IdList *p){ 207 IdList *pNew; 208 int i; 209 if( p==0 ) return 0; 210 pNew = sqliteMalloc( sizeof(*pNew) ); 211 if( pNew==0 ) return 0; 212 pNew->nId = pNew->nAlloc = p->nId; 213 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) ); 214 if( pNew->a==0 ) return 0; 215 for(i=0; i<p->nId; i++){ 216 pNew->a[i].zName = sqliteStrDup(p->a[i].zName); 217 pNew->a[i].idx = p->a[i].idx; 218 } 219 return pNew; 220 } 221 Select *sqliteSelectDup(Select *p){ 222 Select *pNew; 223 if( p==0 ) return 0; 224 pNew = sqliteMalloc( sizeof(*p) ); 225 if( pNew==0 ) return 0; 226 pNew->isDistinct = p->isDistinct; 227 pNew->pEList = sqliteExprListDup(p->pEList); 228 pNew->pSrc = sqliteSrcListDup(p->pSrc); 229 pNew->pWhere = sqliteExprDup(p->pWhere); 230 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy); 231 pNew->pHaving = sqliteExprDup(p->pHaving); 232 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy); 233 pNew->op = p->op; 234 pNew->pPrior = sqliteSelectDup(p->pPrior); 235 pNew->nLimit = p->nLimit; 236 pNew->nOffset = p->nOffset; 237 pNew->zSelect = 0; 238 pNew->iLimit = -1; 239 pNew->iOffset = -1; 240 return pNew; 241 } 242 243 244 /* 245 ** Add a new element to the end of an expression list. If pList is 246 ** initially NULL, then create a new expression list. 247 */ 248 ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 249 int i; 250 if( pList==0 ){ 251 pList = sqliteMalloc( sizeof(ExprList) ); 252 if( pList==0 ){ 253 sqliteExprDelete(pExpr); 254 return 0; 255 } 256 pList->nAlloc = 0; 257 } 258 if( pList->nAlloc<=pList->nExpr ){ 259 struct ExprList_item *a; 260 pList->nAlloc = pList->nAlloc*2 + 4; 261 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); 262 if( a==0 ){ 263 sqliteExprDelete(pExpr); 264 return pList; 265 } 266 pList->a = a; 267 } 268 if( pExpr || pName ){ 269 i = pList->nExpr++; 270 pList->a[i].pExpr = pExpr; 271 pList->a[i].zName = 0; 272 if( pName ){ 273 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); 274 sqliteDequote(pList->a[i].zName); 275 } 276 } 277 return pList; 278 } 279 280 /* 281 ** Delete an entire expression list. 282 */ 283 void sqliteExprListDelete(ExprList *pList){ 284 int i; 285 if( pList==0 ) return; 286 for(i=0; i<pList->nExpr; i++){ 287 sqliteExprDelete(pList->a[i].pExpr); 288 sqliteFree(pList->a[i].zName); 289 } 290 sqliteFree(pList->a); 291 sqliteFree(pList); 292 } 293 294 /* 295 ** Walk an expression tree. Return 1 if the expression is constant 296 ** and 0 if it involves variables. 297 ** 298 ** For the purposes of this function, a double-quoted string (ex: "abc") 299 ** is considered a variable but a single-quoted string (ex: 'abc') is 300 ** a constant. 301 */ 302 int sqliteExprIsConstant(Expr *p){ 303 switch( p->op ){ 304 case TK_ID: 305 case TK_COLUMN: 306 case TK_DOT: 307 case TK_FUNCTION: 308 return 0; 309 case TK_NULL: 310 case TK_STRING: 311 case TK_INTEGER: 312 case TK_FLOAT: 313 return 1; 314 default: { 315 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0; 316 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0; 317 if( p->pList ){ 318 int i; 319 for(i=0; i<p->pList->nExpr; i++){ 320 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0; 321 } 322 } 323 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); 324 } 325 } 326 return 0; 327 } 328 329 /* 330 ** If the given expression codes a constant integer, return 1 and put 331 ** the value of the integer in *pValue. If the expression is not an 332 ** integer, return 0 and leave *pValue unchanged. 333 */ 334 int sqliteExprIsInteger(Expr *p, int *pValue){ 335 switch( p->op ){ 336 case TK_INTEGER: { 337 *pValue = atoi(p->token.z); 338 return 1; 339 } 340 case TK_STRING: { 341 const char *z = p->token.z; 342 int n = p->token.n; 343 if( n>0 && z[0]=='-' ){ z++; n--; } 344 while( n>0 && *z && isdigit(*z) ){ z++; n--; } 345 if( n==0 ){ 346 *pValue = atoi(p->token.z); 347 return 1; 348 } 349 break; 350 } 351 case TK_UPLUS: { 352 return sqliteExprIsInteger(p->pLeft, pValue); 353 } 354 case TK_UMINUS: { 355 int v; 356 if( sqliteExprIsInteger(p->pLeft, &v) ){ 357 *pValue = -v; 358 return 1; 359 } 360 break; 361 } 362 default: break; 363 } 364 return 0; 365 } 366 367 /* 368 ** Return TRUE if the given string is a row-id column name. 369 */ 370 int sqliteIsRowid(const char *z){ 371 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1; 372 if( sqliteStrICmp(z, "ROWID")==0 ) return 1; 373 if( sqliteStrICmp(z, "OID")==0 ) return 1; 374 return 0; 375 } 376 377 /* 378 ** This routine walks an expression tree and resolves references to 379 ** table columns. Nodes of the form ID.ID or ID resolve into an 380 ** index to the table in the table list and a column offset. The 381 ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 382 ** value is changed to the index of the referenced table in pTabList 383 ** plus the "base" value. The base value will ultimately become the 384 ** VDBE cursor number for a cursor that is pointing into the referenced 385 ** table. The Expr.iColumn value is changed to the index of the column 386 ** of the referenced table. The Expr.iColumn value for the special 387 ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 388 ** alias for ROWID. 389 ** 390 ** We also check for instances of the IN operator. IN comes in two 391 ** forms: 392 ** 393 ** expr IN (exprlist) 394 ** and 395 ** expr IN (SELECT ...) 396 ** 397 ** The first form is handled by creating a set holding the list 398 ** of allowed values. The second form causes the SELECT to generate 399 ** a temporary table. 400 ** 401 ** This routine also looks for scalar SELECTs that are part of an expression. 402 ** If it finds any, it generates code to write the value of that select 403 ** into a memory cell. 404 ** 405 ** Unknown columns or tables provoke an error. The function returns 406 ** the number of errors seen and leaves an error message on pParse->zErrMsg. 407 */ 408 int sqliteExprResolveIds( 409 Parse *pParse, /* The parser context */ 410 SrcList *pTabList, /* List of tables used to resolve column names */ 411 ExprList *pEList, /* List of expressions used to resolve "AS" */ 412 Expr *pExpr /* The expression to be analyzed. */ 413 ){ 414 int i; 415 416 if( pExpr==0 || pTabList==0 ) return 0; 417 for(i=0; i<pTabList->nSrc; i++){ 418 assert( pTabList->a[i].iCursor>=0 && pTabList->a[i].iCursor<pParse->nTab ); 419 } 420 switch( pExpr->op ){ 421 /* Double-quoted strings (ex: "abc") are used as identifiers if 422 ** possible. Otherwise they remain as strings. Single-quoted 423 ** strings (ex: 'abc') are always string literals. 424 */ 425 case TK_STRING: { 426 if( pExpr->token.z[0]=='\'' ) break; 427 /* Fall thru into the TK_ID case if this is a double-quoted string */ 428 } 429 /* A lone identifier. Try and match it as follows: 430 ** 431 ** 1. To the name of a column of one of the tables in pTabList 432 ** 433 ** 2. To the right side of an AS keyword in the column list of 434 ** a SELECT statement. (For example, match against 'x' in 435 ** "SELECT a+b AS 'x' FROM t1".) 436 ** 437 ** 3. One of the special names "ROWID", "OID", or "_ROWID_". 438 */ 439 case TK_ID: { 440 int cnt = 0; /* Number of matches */ 441 char *z; 442 int iDb = -1; 443 444 assert( pExpr->token.z ); 445 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n); 446 sqliteDequote(z); 447 if( z==0 ) return 1; 448 for(i=0; i<pTabList->nSrc; i++){ 449 int j; 450 Table *pTab = pTabList->a[i].pTab; 451 if( pTab==0 ) continue; 452 iDb = pTab->iDb; 453 assert( pTab->nCol>0 ); 454 for(j=0; j<pTab->nCol; j++){ 455 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){ 456 cnt++; 457 pExpr->iTable = pTabList->a[i].iCursor; 458 pExpr->iDb = pTab->iDb; 459 if( j==pTab->iPKey ){ 460 /* Substitute the record number for the INTEGER PRIMARY KEY */ 461 pExpr->iColumn = -1; 462 pExpr->dataType = SQLITE_SO_NUM; 463 }else{ 464 pExpr->iColumn = j; 465 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK; 466 } 467 pExpr->op = TK_COLUMN; 468 } 469 } 470 } 471 if( cnt==0 && pEList!=0 ){ 472 int j; 473 for(j=0; j<pEList->nExpr; j++){ 474 char *zAs = pEList->a[j].zName; 475 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){ 476 cnt++; 477 assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 478 pExpr->op = TK_AS; 479 pExpr->iColumn = j; 480 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr); 481 } 482 } 483 } 484 if( cnt==0 && iDb>=0 && sqliteIsRowid(z) ){ 485 pExpr->iColumn = -1; 486 pExpr->iTable = pTabList->a[0].iCursor; 487 pExpr->iDb = iDb; 488 cnt = 1 + (pTabList->nSrc>1); 489 pExpr->op = TK_COLUMN; 490 pExpr->dataType = SQLITE_SO_NUM; 491 } 492 sqliteFree(z); 493 if( cnt==0 && pExpr->token.z[0]!='"' ){ 494 sqliteErrorMsg(pParse, "no such column: %T", &pExpr->token); 495 return 1; 496 }else if( cnt>1 ){ 497 sqliteErrorMsg(pParse, "ambiguous column name: %T", &pExpr->token); 498 return 1; 499 } 500 if( pExpr->op==TK_COLUMN ){ 501 sqliteAuthRead(pParse, pExpr, pTabList); 502 } 503 break; 504 } 505 506 /* A table name and column name: ID.ID 507 ** Or a database, table and column: ID.ID.ID 508 */ 509 case TK_DOT: { 510 int cnt = 0; /* Number of matches */ 511 int cntTab = 0; /* Number of matching tables */ 512 int i; /* Loop counter */ 513 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */ 514 char *zLeft, *zRight; /* Text of an identifier */ 515 char *zDb; /* Name of database holding table */ 516 sqlite *db = pParse->db; 517 518 pRight = pExpr->pRight; 519 if( pRight->op==TK_ID ){ 520 pLeft = pExpr->pLeft; 521 zDb = 0; 522 }else{ 523 Expr *pDb = pExpr->pLeft; 524 assert( pDb && pDb->op==TK_ID && pDb->token.z ); 525 zDb = sqliteStrNDup(pDb->token.z, pDb->token.n); 526 pLeft = pRight->pLeft; 527 pRight = pRight->pRight; 528 } 529 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z ); 530 assert( pRight && pRight->op==TK_ID && pRight->token.z ); 531 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n); 532 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n); 533 if( zLeft==0 || zRight==0 ){ 534 sqliteFree(zLeft); 535 sqliteFree(zRight); 536 sqliteFree(zDb); 537 return 1; 538 } 539 sqliteDequote(zDb); 540 sqliteDequote(zLeft); 541 sqliteDequote(zRight); 542 pExpr->iTable = -1; 543 for(i=0; i<pTabList->nSrc; i++){ 544 int j; 545 char *zTab; 546 Table *pTab = pTabList->a[i].pTab; 547 if( pTab==0 ) continue; 548 assert( pTab->nCol>0 ); 549 if( pTabList->a[i].zAlias ){ 550 zTab = pTabList->a[i].zAlias; 551 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue; 552 }else{ 553 zTab = pTab->zName; 554 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue; 555 if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 556 continue; 557 } 558 } 559 if( 0==(cntTab++) ){ 560 pExpr->iTable = pTabList->a[i].iCursor; 561 pExpr->iDb = pTab->iDb; 562 } 563 for(j=0; j<pTab->nCol; j++){ 564 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){ 565 cnt++; 566 pExpr->iTable = pTabList->a[i].iCursor; 567 pExpr->iDb = pTab->iDb; 568 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 569 pExpr->iColumn = j==pTab->iPKey ? -1 : j; 570 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK; 571 } 572 } 573 } 574 575 /* If we have not already resolved this *.* expression, then maybe 576 * it is a new.* or old.* trigger argument reference */ 577 if( cnt == 0 && pParse->trigStack != 0 ){ 578 TriggerStack *pTriggerStack = pParse->trigStack; 579 int t = 0; 580 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){ 581 pExpr->iTable = pTriggerStack->newIdx; 582 assert( pTriggerStack->pTab ); 583 pExpr->iDb = pTriggerStack->pTab->iDb; 584 cntTab++; 585 t = 1; 586 } 587 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){ 588 pExpr->iTable = pTriggerStack->oldIdx; 589 assert( pTriggerStack->pTab ); 590 pExpr->iDb = pTriggerStack->pTab->iDb; 591 cntTab++; 592 t = 1; 593 } 594 595 if( t ){ 596 int j; 597 Table *pTab = pTriggerStack->pTab; 598 for(j=0; j < pTab->nCol; j++) { 599 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){ 600 cnt++; 601 pExpr->iColumn = j==pTab->iPKey ? -1 : j; 602 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK; 603 } 604 } 605 } 606 } 607 608 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){ 609 cnt = 1; 610 pExpr->iColumn = -1; 611 pExpr->dataType = SQLITE_SO_NUM; 612 } 613 sqliteFree(zDb); 614 sqliteFree(zLeft); 615 sqliteFree(zRight); 616 if( cnt==0 ){ 617 sqliteErrorMsg(pParse, "no such column: %T.%T", 618 &pLeft->token, &pRight->token); 619 return 1; 620 }else if( cnt>1 ){ 621 sqliteErrorMsg(pParse, "ambiguous column name: %T.%T", 622 &pLeft->token, &pRight->token); 623 return 1; 624 } 625 sqliteExprDelete(pExpr->pLeft); 626 pExpr->pLeft = 0; 627 sqliteExprDelete(pExpr->pRight); 628 pExpr->pRight = 0; 629 pExpr->op = TK_COLUMN; 630 sqliteAuthRead(pParse, pExpr, pTabList); 631 break; 632 } 633 634 case TK_IN: { 635 Vdbe *v = sqliteGetVdbe(pParse); 636 if( v==0 ) return 1; 637 if( sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){ 638 return 1; 639 } 640 if( pExpr->pSelect ){ 641 /* Case 1: expr IN (SELECT ...) 642 ** 643 ** Generate code to write the results of the select into a temporary 644 ** table. The cursor number of the temporary table has already 645 ** been put in iTable by sqliteExprResolveInSelect(). 646 */ 647 pExpr->iTable = pParse->nTab++; 648 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1); 649 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0); 650 }else if( pExpr->pList ){ 651 /* Case 2: expr IN (exprlist) 652 ** 653 ** Create a set to put the exprlist values in. The Set id is stored 654 ** in iTable. 655 */ 656 int i, iSet; 657 for(i=0; i<pExpr->pList->nExpr; i++){ 658 Expr *pE2 = pExpr->pList->a[i].pExpr; 659 if( !sqliteExprIsConstant(pE2) ){ 660 sqliteErrorMsg(pParse, 661 "right-hand side of IN operator must be constant"); 662 return 1; 663 } 664 if( sqliteExprCheck(pParse, pE2, 0, 0) ){ 665 return 1; 666 } 667 } 668 iSet = pExpr->iTable = pParse->nSet++; 669 for(i=0; i<pExpr->pList->nExpr; i++){ 670 Expr *pE2 = pExpr->pList->a[i].pExpr; 671 switch( pE2->op ){ 672 case TK_FLOAT: 673 case TK_INTEGER: 674 case TK_STRING: { 675 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); 676 assert( pE2->token.z ); 677 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n); 678 sqliteVdbeDequoteP3(v, addr); 679 break; 680 } 681 default: { 682 sqliteExprCode(pParse, pE2); 683 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); 684 break; 685 } 686 } 687 } 688 } 689 break; 690 } 691 692 case TK_SELECT: { 693 /* This has to be a scalar SELECT. Generate code to put the 694 ** value of this select in a memory cell and record the number 695 ** of the memory cell in iColumn. 696 */ 697 pExpr->iColumn = pParse->nMem++; 698 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){ 699 return 1; 700 } 701 break; 702 } 703 704 /* For all else, just recursively walk the tree */ 705 default: { 706 if( pExpr->pLeft 707 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){ 708 return 1; 709 } 710 if( pExpr->pRight 711 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pRight) ){ 712 return 1; 713 } 714 if( pExpr->pList ){ 715 int i; 716 ExprList *pList = pExpr->pList; 717 for(i=0; i<pList->nExpr; i++){ 718 Expr *pArg = pList->a[i].pExpr; 719 if( sqliteExprResolveIds(pParse, pTabList, pEList, pArg) ){ 720 return 1; 721 } 722 } 723 } 724 } 725 } 726 return 0; 727 } 728 729 /* 730 ** pExpr is a node that defines a function of some kind. It might 731 ** be a syntactic function like "count(x)" or it might be a function 732 ** that implements an operator, like "a LIKE b". 733 ** 734 ** This routine makes *pzName point to the name of the function and 735 ** *pnName hold the number of characters in the function name. 736 */ 737 static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 738 switch( pExpr->op ){ 739 case TK_FUNCTION: { 740 *pzName = pExpr->token.z; 741 *pnName = pExpr->token.n; 742 break; 743 } 744 case TK_LIKE: { 745 *pzName = "like"; 746 *pnName = 4; 747 break; 748 } 749 case TK_GLOB: { 750 *pzName = "glob"; 751 *pnName = 4; 752 break; 753 } 754 default: { 755 *pzName = "can't happen"; 756 *pnName = 12; 757 break; 758 } 759 } 760 } 761 762 /* 763 ** Error check the functions in an expression. Make sure all 764 ** function names are recognized and all functions have the correct 765 ** number of arguments. Leave an error message in pParse->zErrMsg 766 ** if anything is amiss. Return the number of errors. 767 ** 768 ** if pIsAgg is not null and this expression is an aggregate function 769 ** (like count(*) or max(value)) then write a 1 into *pIsAgg. 770 */ 771 int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ 772 int nErr = 0; 773 if( pExpr==0 ) return 0; 774 switch( pExpr->op ){ 775 case TK_GLOB: 776 case TK_LIKE: 777 case TK_FUNCTION: { 778 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ 779 int no_such_func = 0; /* True if no such function exists */ 780 int is_type_of = 0; /* True if is the special TypeOf() function */ 781 int wrong_num_args = 0; /* True if wrong number of arguments */ 782 int is_agg = 0; /* True if is an aggregate function */ 783 int i; 784 int nId; /* Number of characters in function name */ 785 const char *zId; /* The function name. */ 786 FuncDef *pDef; 787 788 getFunctionName(pExpr, &zId, &nId); 789 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0); 790 if( pDef==0 ){ 791 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0); 792 if( pDef==0 ){ 793 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){ 794 is_type_of = 1; 795 }else { 796 no_such_func = 1; 797 } 798 }else{ 799 wrong_num_args = 1; 800 } 801 }else{ 802 is_agg = pDef->xFunc==0; 803 } 804 if( is_agg && !allowAgg ){ 805 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1, 806 zId, nId, "()", 2, 0); 807 pParse->nErr++; 808 nErr++; 809 is_agg = 0; 810 }else if( no_such_func ){ 811 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0); 812 pParse->nErr++; 813 nErr++; 814 }else if( wrong_num_args ){ 815 sqliteSetNString(&pParse->zErrMsg, 816 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0); 817 pParse->nErr++; 818 nErr++; 819 } 820 if( is_agg ) pExpr->op = TK_AGG_FUNCTION; 821 if( is_agg && pIsAgg ) *pIsAgg = 1; 822 for(i=0; nErr==0 && i<n; i++){ 823 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 824 allowAgg && !is_agg, pIsAgg); 825 } 826 if( pDef==0 ){ 827 if( is_type_of ){ 828 pExpr->op = TK_STRING; 829 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){ 830 pExpr->token.z = "numeric"; 831 pExpr->token.n = 7; 832 }else{ 833 pExpr->token.z = "text"; 834 pExpr->token.n = 4; 835 } 836 } 837 }else if( pDef->dataType>=0 ){ 838 if( pDef->dataType<n ){ 839 pExpr->dataType = 840 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr); 841 }else{ 842 pExpr->dataType = SQLITE_SO_NUM; 843 } 844 }else if( pDef->dataType==SQLITE_ARGS ){ 845 pDef->dataType = SQLITE_SO_TEXT; 846 for(i=0; i<n; i++){ 847 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){ 848 pExpr->dataType = SQLITE_SO_NUM; 849 break; 850 } 851 } 852 }else if( pDef->dataType==SQLITE_NUMERIC ){ 853 pExpr->dataType = SQLITE_SO_NUM; 854 }else{ 855 pExpr->dataType = SQLITE_SO_TEXT; 856 } 857 } 858 default: { 859 if( pExpr->pLeft ){ 860 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); 861 } 862 if( nErr==0 && pExpr->pRight ){ 863 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); 864 } 865 if( nErr==0 && pExpr->pList ){ 866 int n = pExpr->pList->nExpr; 867 int i; 868 for(i=0; nErr==0 && i<n; i++){ 869 Expr *pE2 = pExpr->pList->a[i].pExpr; 870 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg); 871 } 872 } 873 break; 874 } 875 } 876 return nErr; 877 } 878 879 /* 880 ** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the 881 ** given expression should sort as numeric values or as text. 882 ** 883 ** The sqliteExprResolveIds() and sqliteExprCheck() routines must have 884 ** both been called on the expression before it is passed to this routine. 885 */ 886 int sqliteExprType(Expr *p){ 887 if( p==0 ) return SQLITE_SO_NUM; 888 while( p ) switch( p->op ){ 889 case TK_PLUS: 890 case TK_MINUS: 891 case TK_STAR: 892 case TK_SLASH: 893 case TK_AND: 894 case TK_OR: 895 case TK_ISNULL: 896 case TK_NOTNULL: 897 case TK_NOT: 898 case TK_UMINUS: 899 case TK_UPLUS: 900 case TK_BITAND: 901 case TK_BITOR: 902 case TK_BITNOT: 903 case TK_LSHIFT: 904 case TK_RSHIFT: 905 case TK_REM: 906 case TK_INTEGER: 907 case TK_FLOAT: 908 case TK_IN: 909 case TK_BETWEEN: 910 case TK_GLOB: 911 case TK_LIKE: 912 return SQLITE_SO_NUM; 913 914 case TK_STRING: 915 case TK_NULL: 916 case TK_CONCAT: 917 return SQLITE_SO_TEXT; 918 919 case TK_LT: 920 case TK_LE: 921 case TK_GT: 922 case TK_GE: 923 case TK_NE: 924 case TK_EQ: 925 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){ 926 return SQLITE_SO_NUM; 927 } 928 p = p->pRight; 929 break; 930 931 case TK_AS: 932 p = p->pLeft; 933 break; 934 935 case TK_COLUMN: 936 case TK_FUNCTION: 937 case TK_AGG_FUNCTION: 938 return p->dataType; 939 940 case TK_SELECT: 941 assert( p->pSelect ); 942 assert( p->pSelect->pEList ); 943 assert( p->pSelect->pEList->nExpr>0 ); 944 p = p->pSelect->pEList->a[0].pExpr; 945 break; 946 947 case TK_CASE: { 948 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){ 949 return SQLITE_SO_NUM; 950 } 951 if( p->pList ){ 952 int i; 953 ExprList *pList = p->pList; 954 for(i=1; i<pList->nExpr; i+=2){ 955 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){ 956 return SQLITE_SO_NUM; 957 } 958 } 959 } 960 return SQLITE_SO_TEXT; 961 } 962 963 default: 964 assert( p->op==TK_ABORT ); /* Can't Happen */ 965 break; 966 } 967 return SQLITE_SO_NUM; 968 } 969 970 /* 971 ** Generate code into the current Vdbe to evaluate the given 972 ** expression and leave the result on the top of stack. 973 */ 974 void sqliteExprCode(Parse *pParse, Expr *pExpr){ 975 Vdbe *v = pParse->pVdbe; 976 int op; 977 if( v==0 || pExpr==0 ) return; 978 switch( pExpr->op ){ 979 case TK_PLUS: op = OP_Add; break; 980 case TK_MINUS: op = OP_Subtract; break; 981 case TK_STAR: op = OP_Multiply; break; 982 case TK_SLASH: op = OP_Divide; break; 983 case TK_AND: op = OP_And; break; 984 case TK_OR: op = OP_Or; break; 985 case TK_LT: op = OP_Lt; break; 986 case TK_LE: op = OP_Le; break; 987 case TK_GT: op = OP_Gt; break; 988 case TK_GE: op = OP_Ge; break; 989 case TK_NE: op = OP_Ne; break; 990 case TK_EQ: op = OP_Eq; break; 991 case TK_ISNULL: op = OP_IsNull; break; 992 case TK_NOTNULL: op = OP_NotNull; break; 993 case TK_NOT: op = OP_Not; break; 994 case TK_UMINUS: op = OP_Negative; break; 995 case TK_BITAND: op = OP_BitAnd; break; 996 case TK_BITOR: op = OP_BitOr; break; 997 case TK_BITNOT: op = OP_BitNot; break; 998 case TK_LSHIFT: op = OP_ShiftLeft; break; 999 case TK_RSHIFT: op = OP_ShiftRight; break; 1000 case TK_REM: op = OP_Remainder; break; 1001 default: break; 1002 } 1003 switch( pExpr->op ){ 1004 case TK_COLUMN: { 1005 if( pParse->useAgg ){ 1006 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1007 }else if( pExpr->iColumn>=0 ){ 1008 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1009 }else{ 1010 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 1011 } 1012 break; 1013 } 1014 case TK_INTEGER: { 1015 int iVal = atoi(pExpr->token.z); 1016 char zBuf[30]; 1017 sprintf(zBuf,"%d",iVal); 1018 if( strlen(zBuf)!=pExpr->token.n 1019 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){ 1020 /* If the integer value cannot be represented exactly in 32 bits, 1021 ** then code it as a string instead. */ 1022 sqliteVdbeAddOp(v, OP_String, 0, 0); 1023 }else{ 1024 sqliteVdbeAddOp(v, OP_Integer, iVal, 0); 1025 } 1026 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 1027 break; 1028 } 1029 case TK_FLOAT: { 1030 sqliteVdbeAddOp(v, OP_String, 0, 0); 1031 assert( pExpr->token.z ); 1032 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 1033 break; 1034 } 1035 case TK_STRING: { 1036 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0); 1037 assert( pExpr->token.z ); 1038 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n); 1039 sqliteVdbeDequoteP3(v, addr); 1040 break; 1041 } 1042 case TK_NULL: { 1043 sqliteVdbeAddOp(v, OP_String, 0, 0); 1044 break; 1045 } 1046 case TK_LT: 1047 case TK_LE: 1048 case TK_GT: 1049 case TK_GE: 1050 case TK_NE: 1051 case TK_EQ: { 1052 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ 1053 op += 6; /* Convert numeric opcodes to text opcodes */ 1054 } 1055 /* Fall through into the next case */ 1056 } 1057 case TK_AND: 1058 case TK_OR: 1059 case TK_PLUS: 1060 case TK_STAR: 1061 case TK_MINUS: 1062 case TK_REM: 1063 case TK_BITAND: 1064 case TK_BITOR: 1065 case TK_SLASH: { 1066 sqliteExprCode(pParse, pExpr->pLeft); 1067 sqliteExprCode(pParse, pExpr->pRight); 1068 sqliteVdbeAddOp(v, op, 0, 0); 1069 break; 1070 } 1071 case TK_LSHIFT: 1072 case TK_RSHIFT: { 1073 sqliteExprCode(pParse, pExpr->pRight); 1074 sqliteExprCode(pParse, pExpr->pLeft); 1075 sqliteVdbeAddOp(v, op, 0, 0); 1076 break; 1077 } 1078 case TK_CONCAT: { 1079 sqliteExprCode(pParse, pExpr->pLeft); 1080 sqliteExprCode(pParse, pExpr->pRight); 1081 sqliteVdbeAddOp(v, OP_Concat, 2, 0); 1082 break; 1083 } 1084 case TK_UPLUS: { 1085 Expr *pLeft = pExpr->pLeft; 1086 if( pLeft && pLeft->op==TK_INTEGER ){ 1087 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0); 1088 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n); 1089 }else if( pLeft && pLeft->op==TK_FLOAT ){ 1090 sqliteVdbeAddOp(v, OP_String, 0, 0); 1091 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n); 1092 }else{ 1093 sqliteExprCode(pParse, pExpr->pLeft); 1094 } 1095 break; 1096 } 1097 case TK_UMINUS: { 1098 assert( pExpr->pLeft ); 1099 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){ 1100 Token *p = &pExpr->pLeft->token; 1101 char *z = sqliteMalloc( p->n + 2 ); 1102 sprintf(z, "-%.*s", p->n, p->z); 1103 if( pExpr->pLeft->op==TK_INTEGER ){ 1104 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0); 1105 }else{ 1106 sqliteVdbeAddOp(v, OP_String, 0, 0); 1107 } 1108 sqliteVdbeChangeP3(v, -1, z, p->n+1); 1109 sqliteFree(z); 1110 break; 1111 } 1112 /* Fall through into TK_NOT */ 1113 } 1114 case TK_BITNOT: 1115 case TK_NOT: { 1116 sqliteExprCode(pParse, pExpr->pLeft); 1117 sqliteVdbeAddOp(v, op, 0, 0); 1118 break; 1119 } 1120 case TK_ISNULL: 1121 case TK_NOTNULL: { 1122 int dest; 1123 sqliteVdbeAddOp(v, OP_Integer, 1, 0); 1124 sqliteExprCode(pParse, pExpr->pLeft); 1125 dest = sqliteVdbeCurrentAddr(v) + 2; 1126 sqliteVdbeAddOp(v, op, 1, dest); 1127 sqliteVdbeAddOp(v, OP_AddImm, -1, 0); 1128 break; 1129 } 1130 case TK_AGG_FUNCTION: { 1131 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1132 break; 1133 } 1134 case TK_GLOB: 1135 case TK_LIKE: 1136 case TK_FUNCTION: { 1137 int i; 1138 ExprList *pList = pExpr->pList; 1139 int nExpr = pList ? pList->nExpr : 0; 1140 FuncDef *pDef; 1141 int nId; 1142 const char *zId; 1143 getFunctionName(pExpr, &zId, &nId); 1144 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0); 1145 assert( pDef!=0 ); 1146 for(i=0; i<nExpr; i++){ 1147 sqliteExprCode(pParse, pList->a[i].pExpr); 1148 } 1149 sqliteVdbeAddOp(v, OP_Function, nExpr, 0); 1150 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER); 1151 break; 1152 } 1153 case TK_SELECT: { 1154 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1155 break; 1156 } 1157 case TK_IN: { 1158 int addr; 1159 sqliteVdbeAddOp(v, OP_Integer, 1, 0); 1160 sqliteExprCode(pParse, pExpr->pLeft); 1161 addr = sqliteVdbeCurrentAddr(v); 1162 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4); 1163 sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1164 sqliteVdbeAddOp(v, OP_String, 0, 0); 1165 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6); 1166 if( pExpr->pSelect ){ 1167 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6); 1168 }else{ 1169 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6); 1170 } 1171 sqliteVdbeAddOp(v, OP_AddImm, -1, 0); 1172 break; 1173 } 1174 case TK_BETWEEN: { 1175 sqliteExprCode(pParse, pExpr->pLeft); 1176 sqliteVdbeAddOp(v, OP_Dup, 0, 0); 1177 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); 1178 sqliteVdbeAddOp(v, OP_Ge, 0, 0); 1179 sqliteVdbeAddOp(v, OP_Pull, 1, 0); 1180 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); 1181 sqliteVdbeAddOp(v, OP_Le, 0, 0); 1182 sqliteVdbeAddOp(v, OP_And, 0, 0); 1183 break; 1184 } 1185 case TK_AS: { 1186 sqliteExprCode(pParse, pExpr->pLeft); 1187 break; 1188 } 1189 case TK_CASE: { 1190 int expr_end_label; 1191 int jumpInst; 1192 int addr; 1193 int nExpr; 1194 int i; 1195 1196 assert(pExpr->pList); 1197 assert((pExpr->pList->nExpr % 2) == 0); 1198 assert(pExpr->pList->nExpr > 0); 1199 nExpr = pExpr->pList->nExpr; 1200 expr_end_label = sqliteVdbeMakeLabel(v); 1201 if( pExpr->pLeft ){ 1202 sqliteExprCode(pParse, pExpr->pLeft); 1203 } 1204 for(i=0; i<nExpr; i=i+2){ 1205 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr); 1206 if( pExpr->pLeft ){ 1207 sqliteVdbeAddOp(v, OP_Dup, 1, 1); 1208 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0); 1209 sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1210 }else{ 1211 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0); 1212 } 1213 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr); 1214 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label); 1215 addr = sqliteVdbeCurrentAddr(v); 1216 sqliteVdbeChangeP2(v, jumpInst, addr); 1217 } 1218 if( pExpr->pLeft ){ 1219 sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1220 } 1221 if( pExpr->pRight ){ 1222 sqliteExprCode(pParse, pExpr->pRight); 1223 }else{ 1224 sqliteVdbeAddOp(v, OP_String, 0, 0); 1225 } 1226 sqliteVdbeResolveLabel(v, expr_end_label); 1227 break; 1228 } 1229 case TK_RAISE: { 1230 if( !pParse->trigStack ){ 1231 sqliteErrorMsg(pParse, 1232 "RAISE() may only be used within a trigger-program"); 1233 pParse->nErr++; 1234 return; 1235 } 1236 if( pExpr->iColumn == OE_Rollback || 1237 pExpr->iColumn == OE_Abort || 1238 pExpr->iColumn == OE_Fail ){ 1239 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n); 1240 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn); 1241 sqliteDequote(msg); 1242 sqliteVdbeChangeP3(v, -1, msg, 0); 1243 sqliteFree(msg); 1244 } else { 1245 assert( pExpr->iColumn == OE_Ignore ); 1246 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 1247 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", 0); 1248 } 1249 } 1250 break; 1251 } 1252 } 1253 1254 /* 1255 ** Generate code for a boolean expression such that a jump is made 1256 ** to the label "dest" if the expression is true but execution 1257 ** continues straight thru if the expression is false. 1258 ** 1259 ** If the expression evaluates to NULL (neither true nor false), then 1260 ** take the jump if the jumpIfNull flag is true. 1261 */ 1262 void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1263 Vdbe *v = pParse->pVdbe; 1264 int op = 0; 1265 if( v==0 || pExpr==0 ) return; 1266 switch( pExpr->op ){ 1267 case TK_LT: op = OP_Lt; break; 1268 case TK_LE: op = OP_Le; break; 1269 case TK_GT: op = OP_Gt; break; 1270 case TK_GE: op = OP_Ge; break; 1271 case TK_NE: op = OP_Ne; break; 1272 case TK_EQ: op = OP_Eq; break; 1273 case TK_ISNULL: op = OP_IsNull; break; 1274 case TK_NOTNULL: op = OP_NotNull; break; 1275 default: break; 1276 } 1277 switch( pExpr->op ){ 1278 case TK_AND: { 1279 int d2 = sqliteVdbeMakeLabel(v); 1280 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 1281 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1282 sqliteVdbeResolveLabel(v, d2); 1283 break; 1284 } 1285 case TK_OR: { 1286 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1287 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1288 break; 1289 } 1290 case TK_NOT: { 1291 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1292 break; 1293 } 1294 case TK_LT: 1295 case TK_LE: 1296 case TK_GT: 1297 case TK_GE: 1298 case TK_NE: 1299 case TK_EQ: { 1300 sqliteExprCode(pParse, pExpr->pLeft); 1301 sqliteExprCode(pParse, pExpr->pRight); 1302 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ 1303 op += 6; /* Convert numeric opcodes to text opcodes */ 1304 } 1305 sqliteVdbeAddOp(v, op, jumpIfNull, dest); 1306 break; 1307 } 1308 case TK_ISNULL: 1309 case TK_NOTNULL: { 1310 sqliteExprCode(pParse, pExpr->pLeft); 1311 sqliteVdbeAddOp(v, op, 1, dest); 1312 break; 1313 } 1314 case TK_IN: { 1315 int addr; 1316 sqliteExprCode(pParse, pExpr->pLeft); 1317 addr = sqliteVdbeCurrentAddr(v); 1318 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3); 1319 sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1320 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1321 if( pExpr->pSelect ){ 1322 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest); 1323 }else{ 1324 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest); 1325 } 1326 break; 1327 } 1328 case TK_BETWEEN: { 1329 int addr; 1330 sqliteExprCode(pParse, pExpr->pLeft); 1331 sqliteVdbeAddOp(v, OP_Dup, 0, 0); 1332 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); 1333 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0); 1334 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); 1335 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest); 1336 sqliteVdbeAddOp(v, OP_Integer, 0, 0); 1337 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); 1338 sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1339 break; 1340 } 1341 default: { 1342 sqliteExprCode(pParse, pExpr); 1343 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest); 1344 break; 1345 } 1346 } 1347 } 1348 1349 /* 1350 ** Generate code for a boolean expression such that a jump is made 1351 ** to the label "dest" if the expression is false but execution 1352 ** continues straight thru if the expression is true. 1353 ** 1354 ** If the expression evaluates to NULL (neither true nor false) then 1355 ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1356 */ 1357 void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1358 Vdbe *v = pParse->pVdbe; 1359 int op = 0; 1360 if( v==0 || pExpr==0 ) return; 1361 switch( pExpr->op ){ 1362 case TK_LT: op = OP_Ge; break; 1363 case TK_LE: op = OP_Gt; break; 1364 case TK_GT: op = OP_Le; break; 1365 case TK_GE: op = OP_Lt; break; 1366 case TK_NE: op = OP_Eq; break; 1367 case TK_EQ: op = OP_Ne; break; 1368 case TK_ISNULL: op = OP_NotNull; break; 1369 case TK_NOTNULL: op = OP_IsNull; break; 1370 default: break; 1371 } 1372 switch( pExpr->op ){ 1373 case TK_AND: { 1374 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1375 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1376 break; 1377 } 1378 case TK_OR: { 1379 int d2 = sqliteVdbeMakeLabel(v); 1380 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 1381 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1382 sqliteVdbeResolveLabel(v, d2); 1383 break; 1384 } 1385 case TK_NOT: { 1386 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1387 break; 1388 } 1389 case TK_LT: 1390 case TK_LE: 1391 case TK_GT: 1392 case TK_GE: 1393 case TK_NE: 1394 case TK_EQ: { 1395 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ 1396 /* Convert numeric comparison opcodes into text comparison opcodes. 1397 ** This step depends on the fact that the text comparision opcodes are 1398 ** always 6 greater than their corresponding numeric comparison 1399 ** opcodes. 1400 */ 1401 assert( OP_Eq+6 == OP_StrEq ); 1402 op += 6; 1403 } 1404 sqliteExprCode(pParse, pExpr->pLeft); 1405 sqliteExprCode(pParse, pExpr->pRight); 1406 sqliteVdbeAddOp(v, op, jumpIfNull, dest); 1407 break; 1408 } 1409 case TK_ISNULL: 1410 case TK_NOTNULL: { 1411 sqliteExprCode(pParse, pExpr->pLeft); 1412 sqliteVdbeAddOp(v, op, 1, dest); 1413 break; 1414 } 1415 case TK_IN: { 1416 int addr; 1417 sqliteExprCode(pParse, pExpr->pLeft); 1418 addr = sqliteVdbeCurrentAddr(v); 1419 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3); 1420 sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1421 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1422 if( pExpr->pSelect ){ 1423 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); 1424 }else{ 1425 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); 1426 } 1427 break; 1428 } 1429 case TK_BETWEEN: { 1430 int addr; 1431 sqliteExprCode(pParse, pExpr->pLeft); 1432 sqliteVdbeAddOp(v, OP_Dup, 0, 0); 1433 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); 1434 addr = sqliteVdbeCurrentAddr(v); 1435 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3); 1436 sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1437 sqliteVdbeAddOp(v, OP_Goto, 0, dest); 1438 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); 1439 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest); 1440 break; 1441 } 1442 default: { 1443 sqliteExprCode(pParse, pExpr); 1444 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1445 break; 1446 } 1447 } 1448 } 1449 1450 /* 1451 ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 1452 ** if they are identical and return FALSE if they differ in any way. 1453 */ 1454 int sqliteExprCompare(Expr *pA, Expr *pB){ 1455 int i; 1456 if( pA==0 ){ 1457 return pB==0; 1458 }else if( pB==0 ){ 1459 return 0; 1460 } 1461 if( pA->op!=pB->op ) return 0; 1462 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0; 1463 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0; 1464 if( pA->pList ){ 1465 if( pB->pList==0 ) return 0; 1466 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 1467 for(i=0; i<pA->pList->nExpr; i++){ 1468 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 1469 return 0; 1470 } 1471 } 1472 }else if( pB->pList ){ 1473 return 0; 1474 } 1475 if( pA->pSelect || pB->pSelect ) return 0; 1476 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 1477 if( pA->token.z ){ 1478 if( pB->token.z==0 ) return 0; 1479 if( pB->token.n!=pA->token.n ) return 0; 1480 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 1481 } 1482 return 1; 1483 } 1484 1485 /* 1486 ** Add a new element to the pParse->aAgg[] array and return its index. 1487 */ 1488 static int appendAggInfo(Parse *pParse){ 1489 if( (pParse->nAgg & 0x7)==0 ){ 1490 int amt = pParse->nAgg + 8; 1491 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 1492 if( aAgg==0 ){ 1493 return -1; 1494 } 1495 pParse->aAgg = aAgg; 1496 } 1497 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 1498 return pParse->nAgg++; 1499 } 1500 1501 /* 1502 ** Analyze the given expression looking for aggregate functions and 1503 ** for variables that need to be added to the pParse->aAgg[] array. 1504 ** Make additional entries to the pParse->aAgg[] array as necessary. 1505 ** 1506 ** This routine should only be called after the expression has been 1507 ** analyzed by sqliteExprResolveIds() and sqliteExprCheck(). 1508 ** 1509 ** If errors are seen, leave an error message in zErrMsg and return 1510 ** the number of errors. 1511 */ 1512 int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 1513 int i; 1514 AggExpr *aAgg; 1515 int nErr = 0; 1516 1517 if( pExpr==0 ) return 0; 1518 switch( pExpr->op ){ 1519 case TK_COLUMN: { 1520 aAgg = pParse->aAgg; 1521 for(i=0; i<pParse->nAgg; i++){ 1522 if( aAgg[i].isAgg ) continue; 1523 if( aAgg[i].pExpr->iTable==pExpr->iTable 1524 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 1525 break; 1526 } 1527 } 1528 if( i>=pParse->nAgg ){ 1529 i = appendAggInfo(pParse); 1530 if( i<0 ) return 1; 1531 pParse->aAgg[i].isAgg = 0; 1532 pParse->aAgg[i].pExpr = pExpr; 1533 } 1534 pExpr->iAgg = i; 1535 break; 1536 } 1537 case TK_AGG_FUNCTION: { 1538 aAgg = pParse->aAgg; 1539 for(i=0; i<pParse->nAgg; i++){ 1540 if( !aAgg[i].isAgg ) continue; 1541 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){ 1542 break; 1543 } 1544 } 1545 if( i>=pParse->nAgg ){ 1546 i = appendAggInfo(pParse); 1547 if( i<0 ) return 1; 1548 pParse->aAgg[i].isAgg = 1; 1549 pParse->aAgg[i].pExpr = pExpr; 1550 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db, 1551 pExpr->token.z, pExpr->token.n, 1552 pExpr->pList ? pExpr->pList->nExpr : 0, 0); 1553 } 1554 pExpr->iAgg = i; 1555 break; 1556 } 1557 default: { 1558 if( pExpr->pLeft ){ 1559 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft); 1560 } 1561 if( nErr==0 && pExpr->pRight ){ 1562 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight); 1563 } 1564 if( nErr==0 && pExpr->pList ){ 1565 int n = pExpr->pList->nExpr; 1566 int i; 1567 for(i=0; nErr==0 && i<n; i++){ 1568 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); 1569 } 1570 } 1571 break; 1572 } 1573 } 1574 return nErr; 1575 } 1576 1577 /* 1578 ** Locate a user function given a name and a number of arguments. 1579 ** Return a pointer to the FuncDef structure that defines that 1580 ** function, or return NULL if the function does not exist. 1581 ** 1582 ** If the createFlag argument is true, then a new (blank) FuncDef 1583 ** structure is created and liked into the "db" structure if a 1584 ** no matching function previously existed. When createFlag is true 1585 ** and the nArg parameter is -1, then only a function that accepts 1586 ** any number of arguments will be returned. 1587 ** 1588 ** If createFlag is false and nArg is -1, then the first valid 1589 ** function found is returned. A function is valid if either xFunc 1590 ** or xStep is non-zero. 1591 */ 1592 FuncDef *sqliteFindFunction( 1593 sqlite *db, /* An open database */ 1594 const char *zName, /* Name of the function. Not null-terminated */ 1595 int nName, /* Number of characters in the name */ 1596 int nArg, /* Number of arguments. -1 means any number */ 1597 int createFlag /* Create new entry if true and does not otherwise exist */ 1598 ){ 1599 FuncDef *pFirst, *p, *pMaybe; 1600 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName); 1601 if( p && !createFlag && nArg<0 ){ 1602 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } 1603 return p; 1604 } 1605 pMaybe = 0; 1606 while( p && p->nArg!=nArg ){ 1607 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; 1608 p = p->pNext; 1609 } 1610 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ 1611 return 0; 1612 } 1613 if( p==0 && pMaybe ){ 1614 assert( createFlag==0 ); 1615 return pMaybe; 1616 } 1617 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){ 1618 p->nArg = nArg; 1619 p->pNext = pFirst; 1620 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC; 1621 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p); 1622 } 1623 return p; 1624 } 1625