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.138 2004/06/10 10:50:17 danielk1977 Exp $ 16 */ 17 #include "sqliteInt.h" 18 #include <ctype.h> 19 20 char const *sqlite3AffinityString(char affinity){ 21 switch( affinity ){ 22 case SQLITE_AFF_INTEGER: return "i"; 23 case SQLITE_AFF_NUMERIC: return "n"; 24 case SQLITE_AFF_TEXT: return "t"; 25 case SQLITE_AFF_NONE: return "o"; 26 default: 27 assert(0); 28 } 29 } 30 31 32 /* 33 ** Return the 'affinity' of the expression pExpr if any. 34 ** 35 ** If pExpr is a column, a reference to a column via an 'AS' alias, 36 ** or a sub-select with a column as the return value, then the 37 ** affinity of that column is returned. Otherwise, 0x00 is returned, 38 ** indicating no affinity for the expression. 39 ** 40 ** i.e. the WHERE clause expresssions in the following statements all 41 ** have an affinity: 42 ** 43 ** CREATE TABLE t1(a); 44 ** SELECT * FROM t1 WHERE a; 45 ** SELECT a AS b FROM t1 WHERE b; 46 ** SELECT * FROM t1 WHERE (select a from t1); 47 */ 48 char sqlite3ExprAffinity(Expr *pExpr){ 49 if( pExpr->op==TK_AS ){ 50 return sqlite3ExprAffinity(pExpr->pLeft); 51 } 52 if( pExpr->op==TK_SELECT ){ 53 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 54 } 55 return pExpr->affinity; 56 } 57 58 /* 59 ** Return the default collation sequence for the expression pExpr. If 60 ** there is no default collation type, return 0. 61 */ 62 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 63 CollSeq *pColl = 0; 64 if( pExpr ){ 65 pColl = pExpr->pColl; 66 if( pExpr->op==TK_AS && !pColl ){ 67 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 68 } 69 } 70 if( sqlite3CheckCollSeq(pParse, pColl) ){ 71 pColl = 0; 72 } 73 return pColl; 74 } 75 76 /* 77 ** pExpr is the left operand of a comparison operator. aff2 is the 78 ** type affinity of the right operand. This routine returns the 79 ** type affinity that should be used for the comparison operator. 80 */ 81 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 82 char aff1 = sqlite3ExprAffinity(pExpr); 83 if( aff1 && aff2 ){ 84 /* Both sides of the comparison are columns. If one has numeric or 85 ** integer affinity, use that. Otherwise use no affinity. 86 */ 87 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){ 88 return SQLITE_AFF_INTEGER; 89 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ 90 return SQLITE_AFF_NUMERIC; 91 }else{ 92 return SQLITE_AFF_NONE; 93 } 94 }else if( !aff1 && !aff2 ){ 95 /* Neither side of the comparison is a column. Use numeric affinity 96 ** for the comparison. 97 */ 98 return SQLITE_AFF_NUMERIC; 99 }else{ 100 /* One side is a column, the other is not. Use the columns affinity. */ 101 return (aff1 + aff2); 102 } 103 } 104 105 /* 106 ** pExpr is a comparison operator. Return the type affinity that should 107 ** be applied to both operands prior to doing the comparison. 108 */ 109 static char comparisonAffinity(Expr *pExpr){ 110 char aff; 111 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 112 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 113 pExpr->op==TK_NE ); 114 assert( pExpr->pLeft ); 115 aff = sqlite3ExprAffinity(pExpr->pLeft); 116 if( pExpr->pRight ){ 117 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 118 } 119 else if( pExpr->pSelect ){ 120 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 121 } 122 else if( !aff ){ 123 aff = SQLITE_AFF_NUMERIC; 124 } 125 return aff; 126 } 127 128 /* 129 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 130 ** idx_affinity is the affinity of an indexed column. Return true 131 ** if the index with affinity idx_affinity may be used to implement 132 ** the comparison in pExpr. 133 */ 134 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 135 char aff = comparisonAffinity(pExpr); 136 return 137 (aff==SQLITE_AFF_NONE) || 138 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) || 139 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) || 140 (aff==idx_affinity); 141 } 142 143 /* 144 ** Return the P1 value that should be used for a binary comparison 145 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 146 ** If jumpIfNull is true, then set the low byte of the returned 147 ** P1 value to tell the opcode to jump if either expression 148 ** evaluates to NULL. 149 */ 150 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 151 char aff = sqlite3ExprAffinity(pExpr2); 152 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0); 153 } 154 155 /* 156 ** Return a pointer to the collation sequence that should be used by 157 ** a binary comparison operator comparing pLeft and pRight. 158 ** 159 ** If the left hand expression has a collating sequence type, then it is 160 ** used. Otherwise the collation sequence for the right hand expression 161 ** is used, or the default (BINARY) if neither expression has a collating 162 ** type. 163 */ 164 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ 165 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft); 166 if( !pColl ){ 167 pColl = sqlite3ExprCollSeq(pParse, pRight); 168 } 169 return pColl; 170 } 171 172 /* 173 ** Construct a new expression node and return a pointer to it. Memory 174 ** for this node is obtained from sqliteMalloc(). The calling function 175 ** is responsible for making sure the node eventually gets freed. 176 */ 177 Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ 178 Expr *pNew; 179 pNew = sqliteMalloc( sizeof(Expr) ); 180 if( pNew==0 ){ 181 /* When malloc fails, we leak memory from pLeft and pRight */ 182 return 0; 183 } 184 pNew->op = op; 185 pNew->pLeft = pLeft; 186 pNew->pRight = pRight; 187 if( pToken ){ 188 assert( pToken->dyn==0 ); 189 pNew->token = *pToken; 190 pNew->span = *pToken; 191 }else{ 192 assert( pNew->token.dyn==0 ); 193 assert( pNew->token.z==0 ); 194 assert( pNew->token.n==0 ); 195 if( pLeft && pRight ){ 196 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 197 }else{ 198 pNew->span = pNew->token; 199 } 200 } 201 return pNew; 202 } 203 204 /* 205 ** Set the Expr.span field of the given expression to span all 206 ** text between the two given tokens. 207 */ 208 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 209 assert( pRight!=0 ); 210 assert( pLeft!=0 ); 211 /* Note: pExpr might be NULL due to a prior malloc failure */ 212 if( pExpr && pRight->z && pLeft->z ){ 213 if( pLeft->dyn==0 && pRight->dyn==0 ){ 214 pExpr->span.z = pLeft->z; 215 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); 216 }else{ 217 pExpr->span.z = 0; 218 } 219 } 220 } 221 222 /* 223 ** Construct a new expression node for a function with multiple 224 ** arguments. 225 */ 226 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 227 Expr *pNew; 228 pNew = sqliteMalloc( sizeof(Expr) ); 229 if( pNew==0 ){ 230 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */ 231 return 0; 232 } 233 pNew->op = TK_FUNCTION; 234 pNew->pList = pList; 235 if( pToken ){ 236 assert( pToken->dyn==0 ); 237 pNew->token = *pToken; 238 }else{ 239 pNew->token.z = 0; 240 } 241 pNew->span = pNew->token; 242 return pNew; 243 } 244 245 /* 246 ** Recursively delete an expression tree. 247 */ 248 void sqlite3ExprDelete(Expr *p){ 249 if( p==0 ) return; 250 if( p->span.dyn ) sqliteFree((char*)p->span.z); 251 if( p->token.dyn ) sqliteFree((char*)p->token.z); 252 sqlite3ExprDelete(p->pLeft); 253 sqlite3ExprDelete(p->pRight); 254 sqlite3ExprListDelete(p->pList); 255 sqlite3SelectDelete(p->pSelect); 256 sqliteFree(p); 257 } 258 259 260 /* 261 ** The following group of routines make deep copies of expressions, 262 ** expression lists, ID lists, and select statements. The copies can 263 ** be deleted (by being passed to their respective ...Delete() routines) 264 ** without effecting the originals. 265 ** 266 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 267 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 268 ** by subsequent calls to sqlite*ListAppend() routines. 269 ** 270 ** Any tables that the SrcList might point to are not duplicated. 271 */ 272 Expr *sqlite3ExprDup(Expr *p){ 273 Expr *pNew; 274 if( p==0 ) return 0; 275 pNew = sqliteMallocRaw( sizeof(*p) ); 276 if( pNew==0 ) return 0; 277 memcpy(pNew, p, sizeof(*pNew)); 278 if( p->token.z!=0 ){ 279 pNew->token.z = sqliteStrDup(p->token.z); 280 pNew->token.dyn = 1; 281 }else{ 282 assert( pNew->token.z==0 ); 283 } 284 pNew->span.z = 0; 285 pNew->pLeft = sqlite3ExprDup(p->pLeft); 286 pNew->pRight = sqlite3ExprDup(p->pRight); 287 pNew->pList = sqlite3ExprListDup(p->pList); 288 pNew->pSelect = sqlite3SelectDup(p->pSelect); 289 return pNew; 290 } 291 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 292 if( pTo->dyn ) sqliteFree((char*)pTo->z); 293 if( pFrom->z ){ 294 pTo->n = pFrom->n; 295 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 296 pTo->dyn = 1; 297 }else{ 298 pTo->z = 0; 299 } 300 } 301 ExprList *sqlite3ExprListDup(ExprList *p){ 302 ExprList *pNew; 303 struct ExprList_item *pItem; 304 int i; 305 if( p==0 ) return 0; 306 pNew = sqliteMalloc( sizeof(*pNew) ); 307 if( pNew==0 ) return 0; 308 pNew->nExpr = pNew->nAlloc = p->nExpr; 309 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 310 if( pItem==0 ) return 0; /* Leaks memory after a malloc failure */ 311 for(i=0; i<p->nExpr; i++, pItem++){ 312 Expr *pNewExpr, *pOldExpr; 313 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr); 314 if( pOldExpr->span.z!=0 && pNewExpr ){ 315 /* Always make a copy of the span for top-level expressions in the 316 ** expression list. The logic in SELECT processing that determines 317 ** the names of columns in the result set needs this information */ 318 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 319 } 320 assert( pNewExpr==0 || pNewExpr->span.z!=0 321 || pOldExpr->span.z==0 || sqlite3_malloc_failed ); 322 pItem->zName = sqliteStrDup(p->a[i].zName); 323 pItem->sortOrder = p->a[i].sortOrder; 324 pItem->isAgg = p->a[i].isAgg; 325 pItem->done = 0; 326 } 327 return pNew; 328 } 329 SrcList *sqlite3SrcListDup(SrcList *p){ 330 SrcList *pNew; 331 int i; 332 int nByte; 333 if( p==0 ) return 0; 334 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 335 pNew = sqliteMallocRaw( nByte ); 336 if( pNew==0 ) return 0; 337 pNew->nSrc = pNew->nAlloc = p->nSrc; 338 for(i=0; i<p->nSrc; i++){ 339 struct SrcList_item *pNewItem = &pNew->a[i]; 340 struct SrcList_item *pOldItem = &p->a[i]; 341 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 342 pNewItem->zName = sqliteStrDup(pOldItem->zName); 343 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 344 pNewItem->jointype = pOldItem->jointype; 345 pNewItem->iCursor = pOldItem->iCursor; 346 pNewItem->pTab = 0; 347 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 348 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 349 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 350 } 351 return pNew; 352 } 353 IdList *sqlite3IdListDup(IdList *p){ 354 IdList *pNew; 355 int i; 356 if( p==0 ) return 0; 357 pNew = sqliteMallocRaw( sizeof(*pNew) ); 358 if( pNew==0 ) return 0; 359 pNew->nId = pNew->nAlloc = p->nId; 360 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 361 if( pNew->a==0 ) return 0; 362 for(i=0; i<p->nId; i++){ 363 struct IdList_item *pNewItem = &pNew->a[i]; 364 struct IdList_item *pOldItem = &p->a[i]; 365 pNewItem->zName = sqliteStrDup(pOldItem->zName); 366 pNewItem->idx = pOldItem->idx; 367 } 368 return pNew; 369 } 370 Select *sqlite3SelectDup(Select *p){ 371 Select *pNew; 372 if( p==0 ) return 0; 373 pNew = sqliteMallocRaw( sizeof(*p) ); 374 if( pNew==0 ) return 0; 375 pNew->isDistinct = p->isDistinct; 376 pNew->pEList = sqlite3ExprListDup(p->pEList); 377 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 378 pNew->pWhere = sqlite3ExprDup(p->pWhere); 379 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 380 pNew->pHaving = sqlite3ExprDup(p->pHaving); 381 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 382 pNew->op = p->op; 383 pNew->pPrior = sqlite3SelectDup(p->pPrior); 384 pNew->nLimit = p->nLimit; 385 pNew->nOffset = p->nOffset; 386 pNew->zSelect = 0; 387 pNew->iLimit = -1; 388 pNew->iOffset = -1; 389 return pNew; 390 } 391 392 393 /* 394 ** Add a new element to the end of an expression list. If pList is 395 ** initially NULL, then create a new expression list. 396 */ 397 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 398 if( pList==0 ){ 399 pList = sqliteMalloc( sizeof(ExprList) ); 400 if( pList==0 ){ 401 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 402 return 0; 403 } 404 assert( pList->nAlloc==0 ); 405 } 406 if( pList->nAlloc<=pList->nExpr ){ 407 pList->nAlloc = pList->nAlloc*2 + 4; 408 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); 409 if( pList->a==0 ){ 410 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 411 pList->nExpr = pList->nAlloc = 0; 412 return pList; 413 } 414 } 415 assert( pList->a!=0 ); 416 if( pExpr || pName ){ 417 struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 418 memset(pItem, 0, sizeof(*pItem)); 419 pItem->pExpr = pExpr; 420 if( pName ){ 421 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0); 422 sqlite3Dequote(pItem->zName); 423 } 424 } 425 return pList; 426 } 427 428 /* 429 ** Delete an entire expression list. 430 */ 431 void sqlite3ExprListDelete(ExprList *pList){ 432 int i; 433 if( pList==0 ) return; 434 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 435 assert( pList->nExpr<=pList->nAlloc ); 436 for(i=0; i<pList->nExpr; i++){ 437 sqlite3ExprDelete(pList->a[i].pExpr); 438 sqliteFree(pList->a[i].zName); 439 } 440 sqliteFree(pList->a); 441 sqliteFree(pList); 442 } 443 444 /* 445 ** Walk an expression tree. Return 1 if the expression is constant 446 ** and 0 if it involves variables. 447 ** 448 ** For the purposes of this function, a double-quoted string (ex: "abc") 449 ** is considered a variable but a single-quoted string (ex: 'abc') is 450 ** a constant. 451 */ 452 int sqlite3ExprIsConstant(Expr *p){ 453 switch( p->op ){ 454 case TK_ID: 455 case TK_COLUMN: 456 case TK_DOT: 457 case TK_FUNCTION: 458 return 0; 459 case TK_NULL: 460 case TK_STRING: 461 case TK_BLOB: 462 case TK_INTEGER: 463 case TK_FLOAT: 464 case TK_VARIABLE: 465 return 1; 466 default: { 467 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0; 468 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0; 469 if( p->pList ){ 470 int i; 471 for(i=0; i<p->pList->nExpr; i++){ 472 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0; 473 } 474 } 475 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); 476 } 477 } 478 return 0; 479 } 480 481 /* 482 ** If the given expression codes a constant integer that is small enough 483 ** to fit in a 32-bit integer, return 1 and put the value of the integer 484 ** in *pValue. If the expression is not an integer or if it is too big 485 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 486 */ 487 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 488 switch( p->op ){ 489 case TK_INTEGER: { 490 if( sqlite3GetInt32(p->token.z, pValue) ){ 491 return 1; 492 } 493 break; 494 } 495 case TK_STRING: { 496 const char *z = p->token.z; 497 int n = p->token.n; 498 if( n>0 && z[0]=='-' ){ z++; n--; } 499 while( n>0 && *z && isdigit(*z) ){ z++; n--; } 500 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){ 501 return 1; 502 } 503 break; 504 } 505 case TK_UPLUS: { 506 return sqlite3ExprIsInteger(p->pLeft, pValue); 507 } 508 case TK_UMINUS: { 509 int v; 510 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 511 *pValue = -v; 512 return 1; 513 } 514 break; 515 } 516 default: break; 517 } 518 return 0; 519 } 520 521 /* 522 ** Return TRUE if the given string is a row-id column name. 523 */ 524 int sqlite3IsRowid(const char *z){ 525 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 526 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 527 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 528 return 0; 529 } 530 531 /* 532 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 533 ** that name in the set of source tables in pSrcList and make the pExpr 534 ** expression node refer back to that source column. The following changes 535 ** are made to pExpr: 536 ** 537 ** pExpr->iDb Set the index in db->aDb[] of the database holding 538 ** the table. 539 ** pExpr->iTable Set to the cursor number for the table obtained 540 ** from pSrcList. 541 ** pExpr->iColumn Set to the column number within the table. 542 ** pExpr->op Set to TK_COLUMN. 543 ** pExpr->pLeft Any expression this points to is deleted 544 ** pExpr->pRight Any expression this points to is deleted. 545 ** 546 ** The pDbToken is the name of the database (the "X"). This value may be 547 ** NULL meaning that name is of the form Y.Z or Z. Any available database 548 ** can be used. The pTableToken is the name of the table (the "Y"). This 549 ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 550 ** means that the form of the name is Z and that columns from any table 551 ** can be used. 552 ** 553 ** If the name cannot be resolved unambiguously, leave an error message 554 ** in pParse and return non-zero. Return zero on success. 555 */ 556 static int lookupName( 557 Parse *pParse, /* The parsing context */ 558 Token *pDbToken, /* Name of the database containing table, or NULL */ 559 Token *pTableToken, /* Name of table containing column, or NULL */ 560 Token *pColumnToken, /* Name of the column. */ 561 SrcList *pSrcList, /* List of tables used to resolve column names */ 562 ExprList *pEList, /* List of expressions used to resolve "AS" */ 563 Expr *pExpr /* Make this EXPR node point to the selected column */ 564 ){ 565 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 566 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 567 char *zCol = 0; /* Name of the column. The "Z" */ 568 int i, j; /* Loop counters */ 569 int cnt = 0; /* Number of matching column names */ 570 int cntTab = 0; /* Number of matching table names */ 571 sqlite *db = pParse->db; /* The database */ 572 573 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 574 if( pDbToken && pDbToken->z ){ 575 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n); 576 sqlite3Dequote(zDb); 577 }else{ 578 zDb = 0; 579 } 580 if( pTableToken && pTableToken->z ){ 581 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n); 582 sqlite3Dequote(zTab); 583 }else{ 584 assert( zDb==0 ); 585 zTab = 0; 586 } 587 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n); 588 sqlite3Dequote(zCol); 589 if( sqlite3_malloc_failed ){ 590 return 1; /* Leak memory (zDb and zTab) if malloc fails */ 591 } 592 assert( zTab==0 || pEList==0 ); 593 594 pExpr->iTable = -1; 595 for(i=0; i<pSrcList->nSrc; i++){ 596 struct SrcList_item *pItem = &pSrcList->a[i]; 597 Table *pTab = pItem->pTab; 598 Column *pCol; 599 600 if( pTab==0 ) continue; 601 assert( pTab->nCol>0 ); 602 if( zTab ){ 603 if( pItem->zAlias ){ 604 char *zTabName = pItem->zAlias; 605 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 606 }else{ 607 char *zTabName = pTab->zName; 608 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 609 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 610 continue; 611 } 612 } 613 } 614 if( 0==(cntTab++) ){ 615 pExpr->iTable = pItem->iCursor; 616 pExpr->iDb = pTab->iDb; 617 } 618 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 619 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 620 cnt++; 621 pExpr->iTable = pItem->iCursor; 622 pExpr->iDb = pTab->iDb; 623 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 624 pExpr->iColumn = j==pTab->iPKey ? -1 : j; 625 pExpr->affinity = pTab->aCol[j].affinity; 626 pExpr->pColl = pTab->aCol[j].pColl; 627 break; 628 } 629 } 630 } 631 632 /* If we have not already resolved the name, then maybe 633 ** it is a new.* or old.* trigger argument reference 634 */ 635 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 636 TriggerStack *pTriggerStack = pParse->trigStack; 637 Table *pTab = 0; 638 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 639 pExpr->iTable = pTriggerStack->newIdx; 640 assert( pTriggerStack->pTab ); 641 pTab = pTriggerStack->pTab; 642 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){ 643 pExpr->iTable = pTriggerStack->oldIdx; 644 assert( pTriggerStack->pTab ); 645 pTab = pTriggerStack->pTab; 646 } 647 648 if( pTab ){ 649 int j; 650 Column *pCol = pTab->aCol; 651 652 pExpr->iDb = pTab->iDb; 653 cntTab++; 654 for(j=0; j < pTab->nCol; j++, pCol++) { 655 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 656 cnt++; 657 pExpr->iColumn = j==pTab->iPKey ? -1 : j; 658 pExpr->affinity = pTab->aCol[j].affinity; 659 pExpr->pColl = pTab->aCol[j].pColl; 660 break; 661 } 662 } 663 } 664 } 665 666 /* 667 ** Perhaps the name is a reference to the ROWID 668 */ 669 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 670 cnt = 1; 671 pExpr->iColumn = -1; 672 pExpr->affinity = SQLITE_AFF_INTEGER; 673 } 674 675 /* 676 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 677 ** might refer to an result-set alias. This happens, for example, when 678 ** we are resolving names in the WHERE clause of the following command: 679 ** 680 ** SELECT a+b AS x FROM table WHERE x<10; 681 ** 682 ** In cases like this, replace pExpr with a copy of the expression that 683 ** forms the result set entry ("a+b" in the example) and return immediately. 684 ** Note that the expression in the result set should have already been 685 ** resolved by the time the WHERE clause is resolved. 686 */ 687 if( cnt==0 && pEList!=0 ){ 688 for(j=0; j<pEList->nExpr; j++){ 689 char *zAs = pEList->a[j].zName; 690 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 691 assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 692 pExpr->op = TK_AS; 693 pExpr->iColumn = j; 694 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 695 sqliteFree(zCol); 696 assert( zTab==0 && zDb==0 ); 697 return 0; 698 } 699 } 700 } 701 702 /* 703 ** If X and Y are NULL (in other words if only the column name Z is 704 ** supplied) and the value of Z is enclosed in double-quotes, then 705 ** Z is a string literal if it doesn't match any column names. In that 706 ** case, we need to return right away and not make any changes to 707 ** pExpr. 708 */ 709 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 710 sqliteFree(zCol); 711 return 0; 712 } 713 714 /* 715 ** cnt==0 means there was not match. cnt>1 means there were two or 716 ** more matches. Either way, we have an error. 717 */ 718 if( cnt!=1 ){ 719 char *z = 0; 720 char *zErr; 721 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 722 if( zDb ){ 723 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 724 }else if( zTab ){ 725 sqlite3SetString(&z, zTab, ".", zCol, 0); 726 }else{ 727 z = sqliteStrDup(zCol); 728 } 729 sqlite3ErrorMsg(pParse, zErr, z); 730 sqliteFree(z); 731 } 732 733 /* Clean up and return 734 */ 735 sqliteFree(zDb); 736 sqliteFree(zTab); 737 sqliteFree(zCol); 738 sqlite3ExprDelete(pExpr->pLeft); 739 pExpr->pLeft = 0; 740 sqlite3ExprDelete(pExpr->pRight); 741 pExpr->pRight = 0; 742 pExpr->op = TK_COLUMN; 743 sqlite3AuthRead(pParse, pExpr, pSrcList); 744 return cnt!=1; 745 } 746 747 /* 748 ** This routine walks an expression tree and resolves references to 749 ** table columns. Nodes of the form ID.ID or ID resolve into an 750 ** index to the table in the table list and a column offset. The 751 ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 752 ** value is changed to the index of the referenced table in pTabList 753 ** plus the "base" value. The base value will ultimately become the 754 ** VDBE cursor number for a cursor that is pointing into the referenced 755 ** table. The Expr.iColumn value is changed to the index of the column 756 ** of the referenced table. The Expr.iColumn value for the special 757 ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 758 ** alias for ROWID. 759 ** 760 ** We also check for instances of the IN operator. IN comes in two 761 ** forms: 762 ** 763 ** expr IN (exprlist) 764 ** and 765 ** expr IN (SELECT ...) 766 ** 767 ** The first form is handled by creating a set holding the list 768 ** of allowed values. The second form causes the SELECT to generate 769 ** a temporary table. 770 ** 771 ** This routine also looks for scalar SELECTs that are part of an expression. 772 ** If it finds any, it generates code to write the value of that select 773 ** into a memory cell. 774 ** 775 ** Unknown columns or tables provoke an error. The function returns 776 ** the number of errors seen and leaves an error message on pParse->zErrMsg. 777 */ 778 int sqlite3ExprResolveIds( 779 Parse *pParse, /* The parser context */ 780 SrcList *pSrcList, /* List of tables used to resolve column names */ 781 ExprList *pEList, /* List of expressions used to resolve "AS" */ 782 Expr *pExpr /* The expression to be analyzed. */ 783 ){ 784 int i; 785 786 if( pExpr==0 || pSrcList==0 ) return 0; 787 for(i=0; i<pSrcList->nSrc; i++){ 788 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab ); 789 } 790 switch( pExpr->op ){ 791 /* Double-quoted strings (ex: "abc") are used as identifiers if 792 ** possible. Otherwise they remain as strings. Single-quoted 793 ** strings (ex: 'abc') are always string literals. 794 */ 795 case TK_STRING: { 796 if( pExpr->token.z[0]=='\'' ) break; 797 /* Fall thru into the TK_ID case if this is a double-quoted string */ 798 } 799 /* A lone identifier is the name of a columnd. 800 */ 801 case TK_ID: { 802 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){ 803 return 1; 804 } 805 break; 806 } 807 808 /* A table name and column name: ID.ID 809 ** Or a database, table and column: ID.ID.ID 810 */ 811 case TK_DOT: { 812 Token *pColumn; 813 Token *pTable; 814 Token *pDb; 815 Expr *pRight; 816 817 pRight = pExpr->pRight; 818 if( pRight->op==TK_ID ){ 819 pDb = 0; 820 pTable = &pExpr->pLeft->token; 821 pColumn = &pRight->token; 822 }else{ 823 assert( pRight->op==TK_DOT ); 824 pDb = &pExpr->pLeft->token; 825 pTable = &pRight->pLeft->token; 826 pColumn = &pRight->pRight->token; 827 } 828 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){ 829 return 1; 830 } 831 break; 832 } 833 834 case TK_IN: { 835 char affinity; 836 Vdbe *v = sqlite3GetVdbe(pParse); 837 KeyInfo keyInfo; 838 int addr; /* Address of OP_OpenTemp instruction */ 839 840 if( v==0 ) return 1; 841 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 842 return 1; 843 } 844 affinity = sqlite3ExprAffinity(pExpr->pLeft); 845 846 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 847 ** expression it is handled the same way. A temporary table is 848 ** filled with single-field index keys representing the results 849 ** from the SELECT or the <exprlist>. 850 ** 851 ** If the 'x' expression is a column value, or the SELECT... 852 ** statement returns a column value, then the affinity of that 853 ** column is used to build the index keys. If both 'x' and the 854 ** SELECT... statement are columns, then numeric affinity is used 855 ** if either column has NUMERIC or INTEGER affinity. If neither 856 ** 'x' nor the SELECT... statement are columns, then numeric affinity 857 ** is used. 858 */ 859 pExpr->iTable = pParse->nTab++; 860 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0); 861 memset(&keyInfo, 0, sizeof(keyInfo)); 862 keyInfo.nField = 1; 863 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 864 865 if( pExpr->pSelect ){ 866 /* Case 1: expr IN (SELECT ...) 867 ** 868 ** Generate code to write the results of the select into the temporary 869 ** table allocated and opened above. 870 */ 871 int iParm = pExpr->iTable + (((int)affinity)<<16); 872 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 873 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); 874 if( pExpr->pSelect->pEList && pExpr->pSelect->pEList->nExpr>0 ){ 875 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, 876 pExpr->pSelect->pEList->a[0].pExpr); 877 } 878 }else if( pExpr->pList ){ 879 /* Case 2: expr IN (exprlist) 880 ** 881 ** For each expression, build an index key from the evaluation and 882 ** store it in the temporary table. If <expr> is a column, then use 883 ** that columns affinity when building index keys. If <expr> is not 884 ** a column, use numeric affinity. 885 */ 886 int i; 887 char const *affStr; 888 if( !affinity ){ 889 affinity = SQLITE_AFF_NUMERIC; 890 } 891 affStr = sqlite3AffinityString(affinity); 892 keyInfo.aColl[0] = pExpr->pLeft->pColl; 893 894 /* Loop through each expression in <exprlist>. */ 895 for(i=0; i<pExpr->pList->nExpr; i++){ 896 Expr *pE2 = pExpr->pList->a[i].pExpr; 897 898 /* Check that the expression is constant and valid. */ 899 if( !sqlite3ExprIsConstant(pE2) ){ 900 sqlite3ErrorMsg(pParse, 901 "right-hand side of IN operator must be constant"); 902 return 1; 903 } 904 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){ 905 return 1; 906 } 907 908 /* Evaluate the expression and insert it into the temp table */ 909 sqlite3ExprCode(pParse, pE2); 910 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); 911 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 912 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0); 913 } 914 } 915 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); 916 917 break; 918 } 919 920 case TK_SELECT: { 921 /* This has to be a scalar SELECT. Generate code to put the 922 ** value of this select in a memory cell and record the number 923 ** of the memory cell in iColumn. 924 */ 925 pExpr->iColumn = pParse->nMem++; 926 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){ 927 return 1; 928 } 929 break; 930 } 931 932 /* For all else, just recursively walk the tree */ 933 default: { 934 if( pExpr->pLeft 935 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 936 return 1; 937 } 938 if( pExpr->pRight 939 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){ 940 return 1; 941 } 942 if( pExpr->pList ){ 943 int i; 944 ExprList *pList = pExpr->pList; 945 for(i=0; i<pList->nExpr; i++){ 946 Expr *pArg = pList->a[i].pExpr; 947 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){ 948 return 1; 949 } 950 } 951 } 952 } 953 } 954 return 0; 955 } 956 957 /* 958 ** pExpr is a node that defines a function of some kind. It might 959 ** be a syntactic function like "count(x)" or it might be a function 960 ** that implements an operator, like "a LIKE b". 961 ** 962 ** This routine makes *pzName point to the name of the function and 963 ** *pnName hold the number of characters in the function name. 964 */ 965 static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 966 switch( pExpr->op ){ 967 case TK_FUNCTION: { 968 *pzName = pExpr->token.z; 969 *pnName = pExpr->token.n; 970 break; 971 } 972 case TK_LIKE: { 973 *pzName = "like"; 974 *pnName = 4; 975 break; 976 } 977 case TK_GLOB: { 978 *pzName = "glob"; 979 *pnName = 4; 980 break; 981 } 982 default: { 983 *pzName = "can't happen"; 984 *pnName = 12; 985 break; 986 } 987 } 988 } 989 990 /* 991 ** Error check the functions in an expression. Make sure all 992 ** function names are recognized and all functions have the correct 993 ** number of arguments. Leave an error message in pParse->zErrMsg 994 ** if anything is amiss. Return the number of errors. 995 ** 996 ** if pIsAgg is not null and this expression is an aggregate function 997 ** (like count(*) or max(value)) then write a 1 into *pIsAgg. 998 */ 999 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ 1000 int nErr = 0; 1001 if( pExpr==0 ) return 0; 1002 switch( pExpr->op ){ 1003 case TK_GLOB: 1004 case TK_LIKE: 1005 case TK_FUNCTION: { 1006 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ 1007 int no_such_func = 0; /* True if no such function exists */ 1008 int wrong_num_args = 0; /* True if wrong number of arguments */ 1009 int is_agg = 0; /* True if is an aggregate function */ 1010 int i; 1011 int nId; /* Number of characters in function name */ 1012 const char *zId; /* The function name. */ 1013 FuncDef *pDef; 1014 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1; 1015 1016 getFunctionName(pExpr, &zId, &nId); 1017 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, iPrefEnc, 0); 1018 if( pDef==0 ){ 1019 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, iPrefEnc, 0); 1020 if( pDef==0 ){ 1021 no_such_func = 1; 1022 }else{ 1023 wrong_num_args = 1; 1024 } 1025 }else{ 1026 is_agg = pDef->xFunc==0; 1027 } 1028 if( is_agg && !allowAgg ){ 1029 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId); 1030 nErr++; 1031 is_agg = 0; 1032 }else if( no_such_func ){ 1033 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1034 nErr++; 1035 }else if( wrong_num_args ){ 1036 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1037 nId, zId); 1038 nErr++; 1039 } 1040 if( is_agg ){ 1041 pExpr->op = TK_AGG_FUNCTION; 1042 if( pIsAgg ) *pIsAgg = 1; 1043 } 1044 for(i=0; nErr==0 && i<n; i++){ 1045 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr, 1046 allowAgg && !is_agg, pIsAgg); 1047 } 1048 /* FIX ME: Compute pExpr->affinity based on the expected return 1049 ** type of the function 1050 */ 1051 } 1052 default: { 1053 if( pExpr->pLeft ){ 1054 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); 1055 } 1056 if( nErr==0 && pExpr->pRight ){ 1057 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); 1058 } 1059 if( nErr==0 && pExpr->pList ){ 1060 int n = pExpr->pList->nExpr; 1061 int i; 1062 for(i=0; nErr==0 && i<n; i++){ 1063 Expr *pE2 = pExpr->pList->a[i].pExpr; 1064 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg); 1065 } 1066 } 1067 break; 1068 } 1069 } 1070 return nErr; 1071 } 1072 1073 /* 1074 ** Return one of the SQLITE_AFF_* affinity types that indicates the likely 1075 ** data type of the result of the given expression. 1076 ** 1077 ** Not every expression has a fixed type. If the type cannot be determined 1078 ** at compile-time, then try to return the type affinity if the expression 1079 ** is a column. Otherwise just return SQLITE_AFF_NONE. 1080 ** 1081 ** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have 1082 ** both been called on the expression before it is passed to this routine. 1083 */ 1084 int sqlite3ExprType(Expr *p){ 1085 if( p==0 ) return SQLITE_AFF_NONE; 1086 while( p ) switch( p->op ){ 1087 case TK_CONCAT: 1088 case TK_STRING: 1089 case TK_BLOB: 1090 return SQLITE_AFF_TEXT; 1091 1092 case TK_AS: 1093 p = p->pLeft; 1094 break; 1095 1096 case TK_VARIABLE: 1097 case TK_NULL: 1098 return SQLITE_AFF_NONE; 1099 1100 case TK_SELECT: /*** FIX ME ****/ 1101 case TK_COLUMN: /*** FIX ME ****/ 1102 case TK_CASE: /*** FIX ME ****/ 1103 1104 default: 1105 return SQLITE_AFF_NUMERIC; 1106 } 1107 return SQLITE_AFF_NONE; 1108 } 1109 1110 /* 1111 ** Generate an instruction that will put the integer describe by 1112 ** text z[0..n-1] on the stack. 1113 */ 1114 static void codeInteger(Vdbe *v, const char *z, int n){ 1115 int i; 1116 if( sqlite3GetInt32(z, &i) ){ 1117 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 1118 }else if( sqlite3FitsIn64Bits(z) ){ 1119 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n); 1120 }else{ 1121 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1122 } 1123 } 1124 1125 /* 1126 ** Generate code into the current Vdbe to evaluate the given 1127 ** expression and leave the result on the top of stack. 1128 */ 1129 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1130 Vdbe *v = pParse->pVdbe; 1131 int op; 1132 if( v==0 || pExpr==0 ) return; 1133 switch( pExpr->op ){ 1134 case TK_PLUS: op = OP_Add; break; 1135 case TK_MINUS: op = OP_Subtract; break; 1136 case TK_STAR: op = OP_Multiply; break; 1137 case TK_SLASH: op = OP_Divide; break; 1138 case TK_AND: op = OP_And; break; 1139 case TK_OR: op = OP_Or; break; 1140 case TK_LT: op = OP_Lt; break; 1141 case TK_LE: op = OP_Le; break; 1142 case TK_GT: op = OP_Gt; break; 1143 case TK_GE: op = OP_Ge; break; 1144 case TK_NE: op = OP_Ne; break; 1145 case TK_EQ: op = OP_Eq; break; 1146 case TK_ISNULL: op = OP_IsNull; break; 1147 case TK_NOTNULL: op = OP_NotNull; break; 1148 case TK_NOT: op = OP_Not; break; 1149 case TK_UMINUS: op = OP_Negative; break; 1150 case TK_BITAND: op = OP_BitAnd; break; 1151 case TK_BITOR: op = OP_BitOr; break; 1152 case TK_BITNOT: op = OP_BitNot; break; 1153 case TK_LSHIFT: op = OP_ShiftLeft; break; 1154 case TK_RSHIFT: op = OP_ShiftRight; break; 1155 case TK_REM: op = OP_Remainder; break; 1156 case TK_FLOAT: op = OP_Real; break; 1157 case TK_STRING: op = OP_String8; break; 1158 case TK_BLOB: op = OP_HexBlob; break; 1159 default: break; 1160 } 1161 switch( pExpr->op ){ 1162 case TK_COLUMN: { 1163 if( pParse->useAgg ){ 1164 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1165 }else if( pExpr->iColumn>=0 ){ 1166 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1167 }else{ 1168 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 1169 } 1170 break; 1171 } 1172 case TK_INTEGER: { 1173 codeInteger(v, pExpr->token.z, pExpr->token.n); 1174 break; 1175 } 1176 case TK_FLOAT: 1177 case TK_STRING: { 1178 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 1179 sqlite3VdbeDequoteP3(v, -1); 1180 break; 1181 } 1182 case TK_BLOB: { 1183 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1); 1184 sqlite3VdbeDequoteP3(v, -1); 1185 break; 1186 } 1187 case TK_NULL: { 1188 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 1189 break; 1190 } 1191 case TK_VARIABLE: { 1192 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 1193 break; 1194 } 1195 case TK_LT: 1196 case TK_LE: 1197 case TK_GT: 1198 case TK_GE: 1199 case TK_NE: 1200 case TK_EQ: { 1201 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0); 1202 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); 1203 sqlite3ExprCode(pParse, pExpr->pLeft); 1204 sqlite3ExprCode(pParse, pExpr->pRight); 1205 sqlite3VdbeOp3(v, op, p1, 0, (void *)p3, P3_COLLSEQ); 1206 break; 1207 } 1208 case TK_AND: 1209 case TK_OR: 1210 case TK_PLUS: 1211 case TK_STAR: 1212 case TK_MINUS: 1213 case TK_REM: 1214 case TK_BITAND: 1215 case TK_BITOR: 1216 case TK_SLASH: { 1217 sqlite3ExprCode(pParse, pExpr->pLeft); 1218 sqlite3ExprCode(pParse, pExpr->pRight); 1219 sqlite3VdbeAddOp(v, op, 0, 0); 1220 break; 1221 } 1222 case TK_LSHIFT: 1223 case TK_RSHIFT: { 1224 sqlite3ExprCode(pParse, pExpr->pRight); 1225 sqlite3ExprCode(pParse, pExpr->pLeft); 1226 sqlite3VdbeAddOp(v, op, 0, 0); 1227 break; 1228 } 1229 case TK_CONCAT: { 1230 sqlite3ExprCode(pParse, pExpr->pLeft); 1231 sqlite3ExprCode(pParse, pExpr->pRight); 1232 sqlite3VdbeAddOp(v, OP_Concat, 2, 0); 1233 break; 1234 } 1235 case TK_UMINUS: { 1236 Expr *pLeft = pExpr->pLeft; 1237 assert( pLeft ); 1238 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1239 Token *p = &pLeft->token; 1240 char *z = sqliteMalloc( p->n + 2 ); 1241 sprintf(z, "-%.*s", p->n, p->z); 1242 if( pLeft->op==TK_FLOAT ){ 1243 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1244 }else{ 1245 codeInteger(v, z, p->n+1); 1246 } 1247 sqliteFree(z); 1248 break; 1249 } 1250 /* Fall through into TK_NOT */ 1251 } 1252 case TK_BITNOT: 1253 case TK_NOT: { 1254 sqlite3ExprCode(pParse, pExpr->pLeft); 1255 sqlite3VdbeAddOp(v, op, 0, 0); 1256 break; 1257 } 1258 case TK_ISNULL: 1259 case TK_NOTNULL: { 1260 int dest; 1261 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1262 sqlite3ExprCode(pParse, pExpr->pLeft); 1263 dest = sqlite3VdbeCurrentAddr(v) + 2; 1264 sqlite3VdbeAddOp(v, op, 1, dest); 1265 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1266 } 1267 break; 1268 case TK_AGG_FUNCTION: { 1269 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1270 break; 1271 } 1272 case TK_GLOB: 1273 case TK_LIKE: 1274 case TK_FUNCTION: { 1275 ExprList *pList = pExpr->pList; 1276 int nExpr = pList ? pList->nExpr : 0; 1277 FuncDef *pDef; 1278 int nId; 1279 const char *zId; 1280 int p2 = 0; 1281 int i; 1282 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1; 1283 getFunctionName(pExpr, &zId, &nId); 1284 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, iPrefEnc, 0); 1285 assert( pDef!=0 ); 1286 nExpr = sqlite3ExprCodeExprList(pParse, pList); 1287 for(i=0; i<nExpr && i<32; i++){ 1288 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 1289 p2 |= (1<<i); 1290 } 1291 } 1292 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF); 1293 break; 1294 } 1295 case TK_SELECT: { 1296 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1297 break; 1298 } 1299 case TK_IN: { 1300 int addr; 1301 char const *affStr; 1302 1303 /* Figure out the affinity to use to create a key from the results 1304 ** of the expression. affinityStr stores a static string suitable for 1305 ** P3 of OP_MakeKey. 1306 */ 1307 affStr = sqlite3AffinityString(comparisonAffinity(pExpr)); 1308 1309 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1310 1311 /* Code the <expr> from "<expr> IN (...)". The temporary table 1312 ** pExpr->iTable contains the values that make up the (...) set. 1313 */ 1314 sqlite3ExprCode(pParse, pExpr->pLeft); 1315 addr = sqlite3VdbeCurrentAddr(v); 1316 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 1317 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 1318 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 1319 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 1320 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */ 1321 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1322 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1323 1324 break; 1325 } 1326 case TK_BETWEEN: { 1327 int p1; 1328 CollSeq *p3; 1329 sqlite3ExprCode(pParse, pExpr->pLeft); 1330 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1331 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 1332 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, 0); 1333 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr); 1334 sqlite3VdbeOp3(v, OP_Ge, p1, 0, (void *)p3, P3_COLLSEQ); 1335 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 1336 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 1337 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, 0); 1338 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr); 1339 sqlite3VdbeOp3(v, OP_Le, p1, 0, (void *)p3, P3_COLLSEQ); 1340 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1341 break; 1342 } 1343 case TK_UPLUS: 1344 case TK_AS: { 1345 sqlite3ExprCode(pParse, pExpr->pLeft); 1346 break; 1347 } 1348 case TK_CASE: { 1349 int expr_end_label; 1350 int jumpInst; 1351 int addr; 1352 int nExpr; 1353 int i; 1354 1355 assert(pExpr->pList); 1356 assert((pExpr->pList->nExpr % 2) == 0); 1357 assert(pExpr->pList->nExpr > 0); 1358 nExpr = pExpr->pList->nExpr; 1359 expr_end_label = sqlite3VdbeMakeLabel(v); 1360 if( pExpr->pLeft ){ 1361 sqlite3ExprCode(pParse, pExpr->pLeft); 1362 } 1363 for(i=0; i<nExpr; i=i+2){ 1364 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr); 1365 if( pExpr->pLeft ){ 1366 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[i].pExpr, 1); 1367 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, 1368 pExpr->pList->a[i].pExpr); 1369 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 1370 jumpInst = sqlite3VdbeOp3(v, OP_Ne, p1, 0, (void *)p3, P3_COLLSEQ); 1371 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1372 }else{ 1373 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 1374 } 1375 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr); 1376 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 1377 addr = sqlite3VdbeCurrentAddr(v); 1378 sqlite3VdbeChangeP2(v, jumpInst, addr); 1379 } 1380 if( pExpr->pLeft ){ 1381 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1382 } 1383 if( pExpr->pRight ){ 1384 sqlite3ExprCode(pParse, pExpr->pRight); 1385 }else{ 1386 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 1387 } 1388 sqlite3VdbeResolveLabel(v, expr_end_label); 1389 break; 1390 } 1391 case TK_RAISE: { 1392 if( !pParse->trigStack ){ 1393 sqlite3ErrorMsg(pParse, 1394 "RAISE() may only be used within a trigger-program"); 1395 pParse->nErr++; 1396 return; 1397 } 1398 if( pExpr->iColumn == OE_Rollback || 1399 pExpr->iColumn == OE_Abort || 1400 pExpr->iColumn == OE_Fail ){ 1401 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1402 pExpr->token.z, pExpr->token.n); 1403 sqlite3VdbeDequoteP3(v, -1); 1404 } else { 1405 assert( pExpr->iColumn == OE_Ignore ); 1406 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump, 1407 "(IGNORE jump)", 0); 1408 } 1409 } 1410 break; 1411 } 1412 } 1413 1414 /* 1415 ** Generate code that pushes the value of every element of the given 1416 ** expression list onto the stack. 1417 ** 1418 ** Return the number of elements pushed onto the stack. 1419 */ 1420 int sqlite3ExprCodeExprList( 1421 Parse *pParse, /* Parsing context */ 1422 ExprList *pList /* The expression list to be coded */ 1423 ){ 1424 struct ExprList_item *pItem; 1425 int i, n; 1426 Vdbe *v; 1427 if( pList==0 ) return 0; 1428 v = sqlite3GetVdbe(pParse); 1429 n = pList->nExpr; 1430 for(pItem=pList->a, i=0; i<n; i++, pItem++){ 1431 sqlite3ExprCode(pParse, pItem->pExpr); 1432 } 1433 return n; 1434 } 1435 1436 /* 1437 ** Generate code for a boolean expression such that a jump is made 1438 ** to the label "dest" if the expression is true but execution 1439 ** continues straight thru if the expression is false. 1440 ** 1441 ** If the expression evaluates to NULL (neither true nor false), then 1442 ** take the jump if the jumpIfNull flag is true. 1443 */ 1444 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1445 Vdbe *v = pParse->pVdbe; 1446 int op = 0; 1447 if( v==0 || pExpr==0 ) return; 1448 switch( pExpr->op ){ 1449 case TK_LT: op = OP_Lt; break; 1450 case TK_LE: op = OP_Le; break; 1451 case TK_GT: op = OP_Gt; break; 1452 case TK_GE: op = OP_Ge; break; 1453 case TK_NE: op = OP_Ne; break; 1454 case TK_EQ: op = OP_Eq; break; 1455 case TK_ISNULL: op = OP_IsNull; break; 1456 case TK_NOTNULL: op = OP_NotNull; break; 1457 default: break; 1458 } 1459 switch( pExpr->op ){ 1460 case TK_AND: { 1461 int d2 = sqlite3VdbeMakeLabel(v); 1462 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 1463 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1464 sqlite3VdbeResolveLabel(v, d2); 1465 break; 1466 } 1467 case TK_OR: { 1468 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1469 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1470 break; 1471 } 1472 case TK_NOT: { 1473 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1474 break; 1475 } 1476 case TK_LT: 1477 case TK_LE: 1478 case TK_GT: 1479 case TK_GE: 1480 case TK_NE: 1481 case TK_EQ: { 1482 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 1483 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); 1484 sqlite3ExprCode(pParse, pExpr->pLeft); 1485 sqlite3ExprCode(pParse, pExpr->pRight); 1486 sqlite3VdbeOp3(v, op, p1, dest, (void *)p3, P3_COLLSEQ); 1487 break; 1488 } 1489 case TK_ISNULL: 1490 case TK_NOTNULL: { 1491 sqlite3ExprCode(pParse, pExpr->pLeft); 1492 sqlite3VdbeAddOp(v, op, 1, dest); 1493 break; 1494 } 1495 case TK_BETWEEN: { 1496 /* The expression "x BETWEEN y AND z" is implemented as: 1497 ** 1498 ** 1 IF (x < y) GOTO 3 1499 ** 2 IF (x <= z) GOTO <dest> 1500 ** 3 ... 1501 */ 1502 int addr; 1503 int p1; 1504 CollSeq *p3; 1505 sqlite3ExprCode(pParse, pExpr->pLeft); 1506 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1507 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 1508 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, !jumpIfNull); 1509 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr); 1510 addr = sqlite3VdbeOp3(v, OP_Lt, p1, 0, (void *)p3, P3_COLLSEQ); 1511 1512 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 1513 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, jumpIfNull); 1514 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr); 1515 sqlite3VdbeOp3(v, OP_Le, p1, dest, (void *)p3, P3_COLLSEQ); 1516 1517 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 1518 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 1519 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1520 break; 1521 } 1522 default: { 1523 sqlite3ExprCode(pParse, pExpr); 1524 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1525 break; 1526 } 1527 } 1528 } 1529 1530 /* 1531 ** Generate code for a boolean expression such that a jump is made 1532 ** to the label "dest" if the expression is false but execution 1533 ** continues straight thru if the expression is true. 1534 ** 1535 ** If the expression evaluates to NULL (neither true nor false) then 1536 ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1537 */ 1538 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1539 Vdbe *v = pParse->pVdbe; 1540 int op = 0; 1541 if( v==0 || pExpr==0 ) return; 1542 switch( pExpr->op ){ 1543 case TK_LT: op = OP_Ge; break; 1544 case TK_LE: op = OP_Gt; break; 1545 case TK_GT: op = OP_Le; break; 1546 case TK_GE: op = OP_Lt; break; 1547 case TK_NE: op = OP_Eq; break; 1548 case TK_EQ: op = OP_Ne; break; 1549 case TK_ISNULL: op = OP_NotNull; break; 1550 case TK_NOTNULL: op = OP_IsNull; break; 1551 default: break; 1552 } 1553 switch( pExpr->op ){ 1554 case TK_AND: { 1555 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1556 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1557 break; 1558 } 1559 case TK_OR: { 1560 int d2 = sqlite3VdbeMakeLabel(v); 1561 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 1562 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1563 sqlite3VdbeResolveLabel(v, d2); 1564 break; 1565 } 1566 case TK_NOT: { 1567 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1568 break; 1569 } 1570 case TK_LT: 1571 case TK_LE: 1572 case TK_GT: 1573 case TK_GE: 1574 case TK_NE: 1575 case TK_EQ: { 1576 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 1577 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); 1578 sqlite3ExprCode(pParse, pExpr->pLeft); 1579 sqlite3ExprCode(pParse, pExpr->pRight); 1580 sqlite3VdbeOp3(v, op, p1, dest, (void *)p3, P3_COLLSEQ); 1581 break; 1582 } 1583 case TK_ISNULL: 1584 case TK_NOTNULL: { 1585 sqlite3ExprCode(pParse, pExpr->pLeft); 1586 sqlite3VdbeAddOp(v, op, 1, dest); 1587 break; 1588 } 1589 case TK_BETWEEN: { 1590 /* The expression is "x BETWEEN y AND z". It is implemented as: 1591 ** 1592 ** 1 IF (x >= y) GOTO 3 1593 ** 2 GOTO <dest> 1594 ** 3 IF (x > z) GOTO <dest> 1595 */ 1596 int addr; 1597 int p1; 1598 CollSeq *p3; 1599 sqlite3ExprCode(pParse, pExpr->pLeft); 1600 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1601 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 1602 addr = sqlite3VdbeCurrentAddr(v); 1603 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, !jumpIfNull); 1604 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr); 1605 sqlite3VdbeOp3(v, OP_Ge, p1, addr+3, (void *)p3, P3_COLLSEQ); 1606 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1607 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 1608 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 1609 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, jumpIfNull); 1610 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr); 1611 sqlite3VdbeOp3(v, OP_Gt, p1, dest, (void *)p3, P3_COLLSEQ); 1612 break; 1613 } 1614 default: { 1615 sqlite3ExprCode(pParse, pExpr); 1616 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1617 break; 1618 } 1619 } 1620 } 1621 1622 /* 1623 ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 1624 ** if they are identical and return FALSE if they differ in any way. 1625 */ 1626 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 1627 int i; 1628 if( pA==0 ){ 1629 return pB==0; 1630 }else if( pB==0 ){ 1631 return 0; 1632 } 1633 if( pA->op!=pB->op ) return 0; 1634 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 1635 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 1636 if( pA->pList ){ 1637 if( pB->pList==0 ) return 0; 1638 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 1639 for(i=0; i<pA->pList->nExpr; i++){ 1640 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 1641 return 0; 1642 } 1643 } 1644 }else if( pB->pList ){ 1645 return 0; 1646 } 1647 if( pA->pSelect || pB->pSelect ) return 0; 1648 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 1649 if( pA->token.z ){ 1650 if( pB->token.z==0 ) return 0; 1651 if( pB->token.n!=pA->token.n ) return 0; 1652 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 1653 } 1654 return 1; 1655 } 1656 1657 /* 1658 ** Add a new element to the pParse->aAgg[] array and return its index. 1659 */ 1660 static int appendAggInfo(Parse *pParse){ 1661 if( (pParse->nAgg & 0x7)==0 ){ 1662 int amt = pParse->nAgg + 8; 1663 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 1664 if( aAgg==0 ){ 1665 return -1; 1666 } 1667 pParse->aAgg = aAgg; 1668 } 1669 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 1670 return pParse->nAgg++; 1671 } 1672 1673 /* 1674 ** Analyze the given expression looking for aggregate functions and 1675 ** for variables that need to be added to the pParse->aAgg[] array. 1676 ** Make additional entries to the pParse->aAgg[] array as necessary. 1677 ** 1678 ** This routine should only be called after the expression has been 1679 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck(). 1680 ** 1681 ** If errors are seen, leave an error message in zErrMsg and return 1682 ** the number of errors. 1683 */ 1684 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 1685 int i; 1686 AggExpr *aAgg; 1687 int nErr = 0; 1688 1689 if( pExpr==0 ) return 0; 1690 switch( pExpr->op ){ 1691 case TK_COLUMN: { 1692 aAgg = pParse->aAgg; 1693 for(i=0; i<pParse->nAgg; i++){ 1694 if( aAgg[i].isAgg ) continue; 1695 if( aAgg[i].pExpr->iTable==pExpr->iTable 1696 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 1697 break; 1698 } 1699 } 1700 if( i>=pParse->nAgg ){ 1701 i = appendAggInfo(pParse); 1702 if( i<0 ) return 1; 1703 pParse->aAgg[i].isAgg = 0; 1704 pParse->aAgg[i].pExpr = pExpr; 1705 } 1706 pExpr->iAgg = i; 1707 break; 1708 } 1709 case TK_AGG_FUNCTION: { 1710 aAgg = pParse->aAgg; 1711 for(i=0; i<pParse->nAgg; i++){ 1712 if( !aAgg[i].isAgg ) continue; 1713 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 1714 break; 1715 } 1716 } 1717 if( i>=pParse->nAgg ){ 1718 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1; 1719 i = appendAggInfo(pParse); 1720 if( i<0 ) return 1; 1721 pParse->aAgg[i].isAgg = 1; 1722 pParse->aAgg[i].pExpr = pExpr; 1723 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 1724 pExpr->token.z, pExpr->token.n, 1725 pExpr->pList ? pExpr->pList->nExpr : 0, iPrefEnc, 0); 1726 } 1727 pExpr->iAgg = i; 1728 break; 1729 } 1730 default: { 1731 if( pExpr->pLeft ){ 1732 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft); 1733 } 1734 if( nErr==0 && pExpr->pRight ){ 1735 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight); 1736 } 1737 if( nErr==0 && pExpr->pList ){ 1738 int n = pExpr->pList->nExpr; 1739 int i; 1740 for(i=0; nErr==0 && i<n; i++){ 1741 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); 1742 } 1743 } 1744 break; 1745 } 1746 } 1747 return nErr; 1748 } 1749 1750 /* 1751 ** Locate a user function given a name, a number of arguments and a flag 1752 ** indicating whether the function prefers UTF-16 over UTF-8. Return a 1753 ** pointer to the FuncDef structure that defines that function, or return 1754 ** NULL if the function does not exist. 1755 ** 1756 ** If the createFlag argument is true, then a new (blank) FuncDef 1757 ** structure is created and liked into the "db" structure if a 1758 ** no matching function previously existed. When createFlag is true 1759 ** and the nArg parameter is -1, then only a function that accepts 1760 ** any number of arguments will be returned. 1761 ** 1762 ** If createFlag is false and nArg is -1, then the first valid 1763 ** function found is returned. A function is valid if either xFunc 1764 ** or xStep is non-zero. 1765 ** 1766 ** If createFlag is false, then a function with the required name and 1767 ** number of arguments may be returned even if the eTextRep flag does not 1768 ** match that requested. 1769 */ 1770 FuncDef *sqlite3FindFunction( 1771 sqlite *db, /* An open database */ 1772 const char *zName, /* Name of the function. Not null-terminated */ 1773 int nName, /* Number of characters in the name */ 1774 int nArg, /* Number of arguments. -1 means any number */ 1775 int eTextRep, /* True to retrieve UTF-16 versions. */ 1776 int createFlag /* Create new entry if true and does not otherwise exist */ 1777 ){ 1778 FuncDef *p; /* Iterator variable */ 1779 FuncDef *pFirst; /* First function with this name */ 1780 FuncDef *pBest = 0; /* Best match found so far */ 1781 int matchqual = 0; 1782 1783 /* Normalize argument values to simplify comparisons below. */ 1784 if( eTextRep ) eTextRep = 1; 1785 if( nArg<-1 ) nArg = -1; 1786 1787 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); 1788 for(p=pFirst; p; p=p->pNext){ 1789 if( 1 || p->xFunc || p->xStep ){ 1790 if( p->nArg==nArg && p->iPrefEnc==eTextRep ){ 1791 /* A perfect match. */ 1792 pBest = p; 1793 matchqual = 4; 1794 break; 1795 } 1796 if( p->nArg==nArg ){ 1797 /* Number of arguments matches, but not the text encoding */ 1798 pBest = p; 1799 matchqual = 3; 1800 } 1801 else if( (p->nArg<0) || (nArg<0) ){ 1802 if( matchqual<2 && p->iPrefEnc==eTextRep ){ 1803 /* Matched a varargs function with correct text encoding */ 1804 pBest = p; 1805 matchqual = 2; 1806 } 1807 if( matchqual<1 ){ 1808 /* Matched a varargs function with incorrect text encoding */ 1809 pBest = p; 1810 matchqual = 1; 1811 } 1812 } 1813 } 1814 } 1815 1816 if( createFlag && matchqual<4 && 1817 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){ 1818 pBest->nArg = nArg; 1819 pBest->pNext = pFirst; 1820 pBest->zName = (char*)&pBest[1]; 1821 pBest->iPrefEnc = eTextRep; 1822 memcpy(pBest->zName, zName, nName); 1823 pBest->zName[nName] = 0; 1824 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest); 1825 } 1826 1827 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ 1828 return pBest; 1829 } 1830 return 0; 1831 } 1832 1833