| OLD | NEW |
| (Empty) |
| 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 #include "sqliteInt.h" | |
| 16 | |
| 17 /* | |
| 18 ** Return the 'affinity' of the expression pExpr if any. | |
| 19 ** | |
| 20 ** If pExpr is a column, a reference to a column via an 'AS' alias, | |
| 21 ** or a sub-select with a column as the return value, then the | |
| 22 ** affinity of that column is returned. Otherwise, 0x00 is returned, | |
| 23 ** indicating no affinity for the expression. | |
| 24 ** | |
| 25 ** i.e. the WHERE clause expresssions in the following statements all | |
| 26 ** have an affinity: | |
| 27 ** | |
| 28 ** CREATE TABLE t1(a); | |
| 29 ** SELECT * FROM t1 WHERE a; | |
| 30 ** SELECT a AS b FROM t1 WHERE b; | |
| 31 ** SELECT * FROM t1 WHERE (select a from t1); | |
| 32 */ | |
| 33 char sqlite3ExprAffinity(Expr *pExpr){ | |
| 34 int op = pExpr->op; | |
| 35 if( op==TK_SELECT ){ | |
| 36 assert( pExpr->flags&EP_xIsSelect ); | |
| 37 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); | |
| 38 } | |
| 39 #ifndef SQLITE_OMIT_CAST | |
| 40 if( op==TK_CAST ){ | |
| 41 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 42 return sqlite3AffinityType(pExpr->u.zToken); | |
| 43 } | |
| 44 #endif | |
| 45 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) | |
| 46 && pExpr->pTab!=0 | |
| 47 ){ | |
| 48 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally | |
| 49 ** a TK_COLUMN but was previously evaluated and cached in a register */ | |
| 50 int j = pExpr->iColumn; | |
| 51 if( j<0 ) return SQLITE_AFF_INTEGER; | |
| 52 assert( pExpr->pTab && j<pExpr->pTab->nCol ); | |
| 53 return pExpr->pTab->aCol[j].affinity; | |
| 54 } | |
| 55 return pExpr->affinity; | |
| 56 } | |
| 57 | |
| 58 /* | |
| 59 ** Set the collating sequence for expression pExpr to be the collating | |
| 60 ** sequence named by pToken. Return a pointer to the revised expression. | |
| 61 ** The collating sequence is marked as "explicit" using the EP_ExpCollate | |
| 62 ** flag. An explicit collating sequence will override implicit | |
| 63 ** collating sequences. | |
| 64 */ | |
| 65 Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pCollName){ | |
| 66 char *zColl = 0; /* Dequoted name of collation sequence */ | |
| 67 CollSeq *pColl; | |
| 68 sqlite3 *db = pParse->db; | |
| 69 zColl = sqlite3NameFromToken(db, pCollName); | |
| 70 if( pExpr && zColl ){ | |
| 71 pColl = sqlite3LocateCollSeq(pParse, zColl); | |
| 72 if( pColl ){ | |
| 73 pExpr->pColl = pColl; | |
| 74 pExpr->flags |= EP_ExpCollate; | |
| 75 } | |
| 76 } | |
| 77 sqlite3DbFree(db, zColl); | |
| 78 return pExpr; | |
| 79 } | |
| 80 | |
| 81 /* | |
| 82 ** Return the default collation sequence for the expression pExpr. If | |
| 83 ** there is no default collation type, return 0. | |
| 84 */ | |
| 85 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ | |
| 86 CollSeq *pColl = 0; | |
| 87 Expr *p = pExpr; | |
| 88 while( ALWAYS(p) ){ | |
| 89 int op; | |
| 90 pColl = p->pColl; | |
| 91 if( pColl ) break; | |
| 92 op = p->op; | |
| 93 if( p->pTab!=0 && ( | |
| 94 op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER | |
| 95 )){ | |
| 96 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally | |
| 97 ** a TK_COLUMN but was previously evaluated and cached in a register */ | |
| 98 const char *zColl; | |
| 99 int j = p->iColumn; | |
| 100 if( j>=0 ){ | |
| 101 sqlite3 *db = pParse->db; | |
| 102 zColl = p->pTab->aCol[j].zColl; | |
| 103 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); | |
| 104 pExpr->pColl = pColl; | |
| 105 } | |
| 106 break; | |
| 107 } | |
| 108 if( op!=TK_CAST && op!=TK_UPLUS ){ | |
| 109 break; | |
| 110 } | |
| 111 p = p->pLeft; | |
| 112 } | |
| 113 if( sqlite3CheckCollSeq(pParse, pColl) ){ | |
| 114 pColl = 0; | |
| 115 } | |
| 116 return pColl; | |
| 117 } | |
| 118 | |
| 119 /* | |
| 120 ** pExpr is an operand of a comparison operator. aff2 is the | |
| 121 ** type affinity of the other operand. This routine returns the | |
| 122 ** type affinity that should be used for the comparison operator. | |
| 123 */ | |
| 124 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ | |
| 125 char aff1 = sqlite3ExprAffinity(pExpr); | |
| 126 if( aff1 && aff2 ){ | |
| 127 /* Both sides of the comparison are columns. If one has numeric | |
| 128 ** affinity, use that. Otherwise use no affinity. | |
| 129 */ | |
| 130 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ | |
| 131 return SQLITE_AFF_NUMERIC; | |
| 132 }else{ | |
| 133 return SQLITE_AFF_NONE; | |
| 134 } | |
| 135 }else if( !aff1 && !aff2 ){ | |
| 136 /* Neither side of the comparison is a column. Compare the | |
| 137 ** results directly. | |
| 138 */ | |
| 139 return SQLITE_AFF_NONE; | |
| 140 }else{ | |
| 141 /* One side is a column, the other is not. Use the columns affinity. */ | |
| 142 assert( aff1==0 || aff2==0 ); | |
| 143 return (aff1 + aff2); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 /* | |
| 148 ** pExpr is a comparison operator. Return the type affinity that should | |
| 149 ** be applied to both operands prior to doing the comparison. | |
| 150 */ | |
| 151 static char comparisonAffinity(Expr *pExpr){ | |
| 152 char aff; | |
| 153 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || | |
| 154 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || | |
| 155 pExpr->op==TK_NE ); | |
| 156 assert( pExpr->pLeft ); | |
| 157 aff = sqlite3ExprAffinity(pExpr->pLeft); | |
| 158 if( pExpr->pRight ){ | |
| 159 aff = sqlite3CompareAffinity(pExpr->pRight, aff); | |
| 160 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ | |
| 161 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); | |
| 162 }else if( !aff ){ | |
| 163 aff = SQLITE_AFF_NONE; | |
| 164 } | |
| 165 return aff; | |
| 166 } | |
| 167 | |
| 168 /* | |
| 169 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. | |
| 170 ** idx_affinity is the affinity of an indexed column. Return true | |
| 171 ** if the index with affinity idx_affinity may be used to implement | |
| 172 ** the comparison in pExpr. | |
| 173 */ | |
| 174 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ | |
| 175 char aff = comparisonAffinity(pExpr); | |
| 176 switch( aff ){ | |
| 177 case SQLITE_AFF_NONE: | |
| 178 return 1; | |
| 179 case SQLITE_AFF_TEXT: | |
| 180 return idx_affinity==SQLITE_AFF_TEXT; | |
| 181 default: | |
| 182 return sqlite3IsNumericAffinity(idx_affinity); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 /* | |
| 187 ** Return the P5 value that should be used for a binary comparison | |
| 188 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. | |
| 189 */ | |
| 190 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ | |
| 191 u8 aff = (char)sqlite3ExprAffinity(pExpr2); | |
| 192 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull; | |
| 193 return aff; | |
| 194 } | |
| 195 | |
| 196 /* | |
| 197 ** Return a pointer to the collation sequence that should be used by | |
| 198 ** a binary comparison operator comparing pLeft and pRight. | |
| 199 ** | |
| 200 ** If the left hand expression has a collating sequence type, then it is | |
| 201 ** used. Otherwise the collation sequence for the right hand expression | |
| 202 ** is used, or the default (BINARY) if neither expression has a collating | |
| 203 ** type. | |
| 204 ** | |
| 205 ** Argument pRight (but not pLeft) may be a null pointer. In this case, | |
| 206 ** it is not considered. | |
| 207 */ | |
| 208 CollSeq *sqlite3BinaryCompareCollSeq( | |
| 209 Parse *pParse, | |
| 210 Expr *pLeft, | |
| 211 Expr *pRight | |
| 212 ){ | |
| 213 CollSeq *pColl; | |
| 214 assert( pLeft ); | |
| 215 if( pLeft->flags & EP_ExpCollate ){ | |
| 216 assert( pLeft->pColl ); | |
| 217 pColl = pLeft->pColl; | |
| 218 }else if( pRight && pRight->flags & EP_ExpCollate ){ | |
| 219 assert( pRight->pColl ); | |
| 220 pColl = pRight->pColl; | |
| 221 }else{ | |
| 222 pColl = sqlite3ExprCollSeq(pParse, pLeft); | |
| 223 if( !pColl ){ | |
| 224 pColl = sqlite3ExprCollSeq(pParse, pRight); | |
| 225 } | |
| 226 } | |
| 227 return pColl; | |
| 228 } | |
| 229 | |
| 230 /* | |
| 231 ** Generate the operands for a comparison operation. Before | |
| 232 ** generating the code for each operand, set the EP_AnyAff | |
| 233 ** flag on the expression so that it will be able to used a | |
| 234 ** cached column value that has previously undergone an | |
| 235 ** affinity change. | |
| 236 */ | |
| 237 static void codeCompareOperands( | |
| 238 Parse *pParse, /* Parsing and code generating context */ | |
| 239 Expr *pLeft, /* The left operand */ | |
| 240 int *pRegLeft, /* Register where left operand is stored */ | |
| 241 int *pFreeLeft, /* Free this register when done */ | |
| 242 Expr *pRight, /* The right operand */ | |
| 243 int *pRegRight, /* Register where right operand is stored */ | |
| 244 int *pFreeRight /* Write temp register for right operand there */ | |
| 245 ){ | |
| 246 while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft; | |
| 247 pLeft->flags |= EP_AnyAff; | |
| 248 *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft); | |
| 249 while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft; | |
| 250 pRight->flags |= EP_AnyAff; | |
| 251 *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight); | |
| 252 } | |
| 253 | |
| 254 /* | |
| 255 ** Generate code for a comparison operator. | |
| 256 */ | |
| 257 static int codeCompare( | |
| 258 Parse *pParse, /* The parsing (and code generating) context */ | |
| 259 Expr *pLeft, /* The left operand */ | |
| 260 Expr *pRight, /* The right operand */ | |
| 261 int opcode, /* The comparison opcode */ | |
| 262 int in1, int in2, /* Register holding operands */ | |
| 263 int dest, /* Jump here if true. */ | |
| 264 int jumpIfNull /* If true, jump if either operand is NULL */ | |
| 265 ){ | |
| 266 int p5; | |
| 267 int addr; | |
| 268 CollSeq *p4; | |
| 269 | |
| 270 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); | |
| 271 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); | |
| 272 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, | |
| 273 (void*)p4, P4_COLLSEQ); | |
| 274 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); | |
| 275 if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){ | |
| 276 sqlite3ExprCacheAffinityChange(pParse, in1, 1); | |
| 277 sqlite3ExprCacheAffinityChange(pParse, in2, 1); | |
| 278 } | |
| 279 return addr; | |
| 280 } | |
| 281 | |
| 282 #if SQLITE_MAX_EXPR_DEPTH>0 | |
| 283 /* | |
| 284 ** Check that argument nHeight is less than or equal to the maximum | |
| 285 ** expression depth allowed. If it is not, leave an error message in | |
| 286 ** pParse. | |
| 287 */ | |
| 288 int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ | |
| 289 int rc = SQLITE_OK; | |
| 290 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; | |
| 291 if( nHeight>mxHeight ){ | |
| 292 sqlite3ErrorMsg(pParse, | |
| 293 "Expression tree is too large (maximum depth %d)", mxHeight | |
| 294 ); | |
| 295 rc = SQLITE_ERROR; | |
| 296 } | |
| 297 return rc; | |
| 298 } | |
| 299 | |
| 300 /* The following three functions, heightOfExpr(), heightOfExprList() | |
| 301 ** and heightOfSelect(), are used to determine the maximum height | |
| 302 ** of any expression tree referenced by the structure passed as the | |
| 303 ** first argument. | |
| 304 ** | |
| 305 ** If this maximum height is greater than the current value pointed | |
| 306 ** to by pnHeight, the second parameter, then set *pnHeight to that | |
| 307 ** value. | |
| 308 */ | |
| 309 static void heightOfExpr(Expr *p, int *pnHeight){ | |
| 310 if( p ){ | |
| 311 if( p->nHeight>*pnHeight ){ | |
| 312 *pnHeight = p->nHeight; | |
| 313 } | |
| 314 } | |
| 315 } | |
| 316 static void heightOfExprList(ExprList *p, int *pnHeight){ | |
| 317 if( p ){ | |
| 318 int i; | |
| 319 for(i=0; i<p->nExpr; i++){ | |
| 320 heightOfExpr(p->a[i].pExpr, pnHeight); | |
| 321 } | |
| 322 } | |
| 323 } | |
| 324 static void heightOfSelect(Select *p, int *pnHeight){ | |
| 325 if( p ){ | |
| 326 heightOfExpr(p->pWhere, pnHeight); | |
| 327 heightOfExpr(p->pHaving, pnHeight); | |
| 328 heightOfExpr(p->pLimit, pnHeight); | |
| 329 heightOfExpr(p->pOffset, pnHeight); | |
| 330 heightOfExprList(p->pEList, pnHeight); | |
| 331 heightOfExprList(p->pGroupBy, pnHeight); | |
| 332 heightOfExprList(p->pOrderBy, pnHeight); | |
| 333 heightOfSelect(p->pPrior, pnHeight); | |
| 334 } | |
| 335 } | |
| 336 | |
| 337 /* | |
| 338 ** Set the Expr.nHeight variable in the structure passed as an | |
| 339 ** argument. An expression with no children, Expr.pList or | |
| 340 ** Expr.pSelect member has a height of 1. Any other expression | |
| 341 ** has a height equal to the maximum height of any other | |
| 342 ** referenced Expr plus one. | |
| 343 */ | |
| 344 static void exprSetHeight(Expr *p){ | |
| 345 int nHeight = 0; | |
| 346 heightOfExpr(p->pLeft, &nHeight); | |
| 347 heightOfExpr(p->pRight, &nHeight); | |
| 348 if( ExprHasProperty(p, EP_xIsSelect) ){ | |
| 349 heightOfSelect(p->x.pSelect, &nHeight); | |
| 350 }else{ | |
| 351 heightOfExprList(p->x.pList, &nHeight); | |
| 352 } | |
| 353 p->nHeight = nHeight + 1; | |
| 354 } | |
| 355 | |
| 356 /* | |
| 357 ** Set the Expr.nHeight variable using the exprSetHeight() function. If | |
| 358 ** the height is greater than the maximum allowed expression depth, | |
| 359 ** leave an error in pParse. | |
| 360 */ | |
| 361 void sqlite3ExprSetHeight(Parse *pParse, Expr *p){ | |
| 362 exprSetHeight(p); | |
| 363 sqlite3ExprCheckHeight(pParse, p->nHeight); | |
| 364 } | |
| 365 | |
| 366 /* | |
| 367 ** Return the maximum height of any expression tree referenced | |
| 368 ** by the select statement passed as an argument. | |
| 369 */ | |
| 370 int sqlite3SelectExprHeight(Select *p){ | |
| 371 int nHeight = 0; | |
| 372 heightOfSelect(p, &nHeight); | |
| 373 return nHeight; | |
| 374 } | |
| 375 #else | |
| 376 #define exprSetHeight(y) | |
| 377 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ | |
| 378 | |
| 379 /* | |
| 380 ** This routine is the core allocator for Expr nodes. | |
| 381 ** | |
| 382 ** Construct a new expression node and return a pointer to it. Memory | |
| 383 ** for this node and for the pToken argument is a single allocation | |
| 384 ** obtained from sqlite3DbMalloc(). The calling function | |
| 385 ** is responsible for making sure the node eventually gets freed. | |
| 386 ** | |
| 387 ** If dequote is true, then the token (if it exists) is dequoted. | |
| 388 ** If dequote is false, no dequoting is performance. The deQuote | |
| 389 ** parameter is ignored if pToken is NULL or if the token does not | |
| 390 ** appear to be quoted. If the quotes were of the form "..." (double-quotes) | |
| 391 ** then the EP_DblQuoted flag is set on the expression node. | |
| 392 ** | |
| 393 ** Special case: If op==TK_INTEGER and pToken points to a string that | |
| 394 ** can be translated into a 32-bit integer, then the token is not | |
| 395 ** stored in u.zToken. Instead, the integer values is written | |
| 396 ** into u.iValue and the EP_IntValue flag is set. No extra storage | |
| 397 ** is allocated to hold the integer text and the dequote flag is ignored. | |
| 398 */ | |
| 399 Expr *sqlite3ExprAlloc( | |
| 400 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ | |
| 401 int op, /* Expression opcode */ | |
| 402 const Token *pToken, /* Token argument. Might be NULL */ | |
| 403 int dequote /* True to dequote */ | |
| 404 ){ | |
| 405 Expr *pNew; | |
| 406 int nExtra = 0; | |
| 407 int iValue = 0; | |
| 408 | |
| 409 if( pToken ){ | |
| 410 if( op!=TK_INTEGER || pToken->z==0 | |
| 411 || sqlite3GetInt32(pToken->z, &iValue)==0 ){ | |
| 412 nExtra = pToken->n+1; | |
| 413 } | |
| 414 } | |
| 415 pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra); | |
| 416 if( pNew ){ | |
| 417 pNew->op = (u8)op; | |
| 418 pNew->iAgg = -1; | |
| 419 if( pToken ){ | |
| 420 if( nExtra==0 ){ | |
| 421 pNew->flags |= EP_IntValue; | |
| 422 pNew->u.iValue = iValue; | |
| 423 }else{ | |
| 424 int c; | |
| 425 pNew->u.zToken = (char*)&pNew[1]; | |
| 426 memcpy(pNew->u.zToken, pToken->z, pToken->n); | |
| 427 pNew->u.zToken[pToken->n] = 0; | |
| 428 if( dequote && nExtra>=3 | |
| 429 && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){ | |
| 430 sqlite3Dequote(pNew->u.zToken); | |
| 431 if( c=='"' ) pNew->flags |= EP_DblQuoted; | |
| 432 } | |
| 433 } | |
| 434 } | |
| 435 #if SQLITE_MAX_EXPR_DEPTH>0 | |
| 436 pNew->nHeight = 1; | |
| 437 #endif | |
| 438 } | |
| 439 return pNew; | |
| 440 } | |
| 441 | |
| 442 /* | |
| 443 ** Allocate a new expression node from a zero-terminated token that has | |
| 444 ** already been dequoted. | |
| 445 */ | |
| 446 Expr *sqlite3Expr( | |
| 447 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ | |
| 448 int op, /* Expression opcode */ | |
| 449 const char *zToken /* Token argument. Might be NULL */ | |
| 450 ){ | |
| 451 Token x; | |
| 452 x.z = zToken; | |
| 453 x.n = zToken ? sqlite3Strlen30(zToken) : 0; | |
| 454 return sqlite3ExprAlloc(db, op, &x, 0); | |
| 455 } | |
| 456 | |
| 457 /* | |
| 458 ** Attach subtrees pLeft and pRight to the Expr node pRoot. | |
| 459 ** | |
| 460 ** If pRoot==NULL that means that a memory allocation error has occurred. | |
| 461 ** In that case, delete the subtrees pLeft and pRight. | |
| 462 */ | |
| 463 void sqlite3ExprAttachSubtrees( | |
| 464 sqlite3 *db, | |
| 465 Expr *pRoot, | |
| 466 Expr *pLeft, | |
| 467 Expr *pRight | |
| 468 ){ | |
| 469 if( pRoot==0 ){ | |
| 470 assert( db->mallocFailed ); | |
| 471 sqlite3ExprDelete(db, pLeft); | |
| 472 sqlite3ExprDelete(db, pRight); | |
| 473 }else{ | |
| 474 if( pRight ){ | |
| 475 pRoot->pRight = pRight; | |
| 476 if( pRight->flags & EP_ExpCollate ){ | |
| 477 pRoot->flags |= EP_ExpCollate; | |
| 478 pRoot->pColl = pRight->pColl; | |
| 479 } | |
| 480 } | |
| 481 if( pLeft ){ | |
| 482 pRoot->pLeft = pLeft; | |
| 483 if( pLeft->flags & EP_ExpCollate ){ | |
| 484 pRoot->flags |= EP_ExpCollate; | |
| 485 pRoot->pColl = pLeft->pColl; | |
| 486 } | |
| 487 } | |
| 488 exprSetHeight(pRoot); | |
| 489 } | |
| 490 } | |
| 491 | |
| 492 /* | |
| 493 ** Allocate a Expr node which joins as many as two subtrees. | |
| 494 ** | |
| 495 ** One or both of the subtrees can be NULL. Return a pointer to the new | |
| 496 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, | |
| 497 ** free the subtrees and return NULL. | |
| 498 */ | |
| 499 Expr *sqlite3PExpr( | |
| 500 Parse *pParse, /* Parsing context */ | |
| 501 int op, /* Expression opcode */ | |
| 502 Expr *pLeft, /* Left operand */ | |
| 503 Expr *pRight, /* Right operand */ | |
| 504 const Token *pToken /* Argument token */ | |
| 505 ){ | |
| 506 Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1); | |
| 507 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); | |
| 508 return p; | |
| 509 } | |
| 510 | |
| 511 /* | |
| 512 ** Join two expressions using an AND operator. If either expression is | |
| 513 ** NULL, then just return the other expression. | |
| 514 */ | |
| 515 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ | |
| 516 if( pLeft==0 ){ | |
| 517 return pRight; | |
| 518 }else if( pRight==0 ){ | |
| 519 return pLeft; | |
| 520 }else{ | |
| 521 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); | |
| 522 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); | |
| 523 return pNew; | |
| 524 } | |
| 525 } | |
| 526 | |
| 527 /* | |
| 528 ** Construct a new expression node for a function with multiple | |
| 529 ** arguments. | |
| 530 */ | |
| 531 Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ | |
| 532 Expr *pNew; | |
| 533 sqlite3 *db = pParse->db; | |
| 534 assert( pToken ); | |
| 535 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); | |
| 536 if( pNew==0 ){ | |
| 537 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ | |
| 538 return 0; | |
| 539 } | |
| 540 pNew->x.pList = pList; | |
| 541 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); | |
| 542 sqlite3ExprSetHeight(pParse, pNew); | |
| 543 return pNew; | |
| 544 } | |
| 545 | |
| 546 /* | |
| 547 ** Assign a variable number to an expression that encodes a wildcard | |
| 548 ** in the original SQL statement. | |
| 549 ** | |
| 550 ** Wildcards consisting of a single "?" are assigned the next sequential | |
| 551 ** variable number. | |
| 552 ** | |
| 553 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make | |
| 554 ** sure "nnn" is not too be to avoid a denial of service attack when | |
| 555 ** the SQL statement comes from an external source. | |
| 556 ** | |
| 557 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number | |
| 558 ** as the previous instance of the same wildcard. Or if this is the first | |
| 559 ** instance of the wildcard, the next sequenial variable number is | |
| 560 ** assigned. | |
| 561 */ | |
| 562 void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ | |
| 563 sqlite3 *db = pParse->db; | |
| 564 const char *z; | |
| 565 | |
| 566 if( pExpr==0 ) return; | |
| 567 assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); | |
| 568 z = pExpr->u.zToken; | |
| 569 assert( z!=0 ); | |
| 570 assert( z[0]!=0 ); | |
| 571 if( z[1]==0 ){ | |
| 572 /* Wildcard of the form "?". Assign the next variable number */ | |
| 573 assert( z[0]=='?' ); | |
| 574 pExpr->iTable = ++pParse->nVar; | |
| 575 }else if( z[0]=='?' ){ | |
| 576 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and | |
| 577 ** use it as the variable number */ | |
| 578 int i; | |
| 579 pExpr->iTable = i = atoi((char*)&z[1]); | |
| 580 testcase( i==0 ); | |
| 581 testcase( i==1 ); | |
| 582 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); | |
| 583 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); | |
| 584 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ | |
| 585 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", | |
| 586 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); | |
| 587 } | |
| 588 if( i>pParse->nVar ){ | |
| 589 pParse->nVar = i; | |
| 590 } | |
| 591 }else{ | |
| 592 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable | |
| 593 ** number as the prior appearance of the same name, or if the name | |
| 594 ** has never appeared before, reuse the same variable number | |
| 595 */ | |
| 596 int i; | |
| 597 u32 n; | |
| 598 n = sqlite3Strlen30(z); | |
| 599 for(i=0; i<pParse->nVarExpr; i++){ | |
| 600 Expr *pE = pParse->apVarExpr[i]; | |
| 601 assert( pE!=0 ); | |
| 602 if( memcmp(pE->u.zToken, z, n)==0 && pE->u.zToken[n]==0 ){ | |
| 603 pExpr->iTable = pE->iTable; | |
| 604 break; | |
| 605 } | |
| 606 } | |
| 607 if( i>=pParse->nVarExpr ){ | |
| 608 pExpr->iTable = ++pParse->nVar; | |
| 609 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ | |
| 610 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; | |
| 611 pParse->apVarExpr = | |
| 612 sqlite3DbReallocOrFree( | |
| 613 db, | |
| 614 pParse->apVarExpr, | |
| 615 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) | |
| 616 ); | |
| 617 } | |
| 618 if( !db->mallocFailed ){ | |
| 619 assert( pParse->apVarExpr!=0 ); | |
| 620 pParse->apVarExpr[pParse->nVarExpr++] = pExpr; | |
| 621 } | |
| 622 } | |
| 623 } | |
| 624 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ | |
| 625 sqlite3ErrorMsg(pParse, "too many SQL variables"); | |
| 626 } | |
| 627 } | |
| 628 | |
| 629 /* | |
| 630 ** Clear an expression structure without deleting the structure itself. | |
| 631 ** Substructure is deleted. | |
| 632 */ | |
| 633 void sqlite3ExprClear(sqlite3 *db, Expr *p){ | |
| 634 assert( p!=0 ); | |
| 635 if( !ExprHasAnyProperty(p, EP_TokenOnly) ){ | |
| 636 sqlite3ExprDelete(db, p->pLeft); | |
| 637 sqlite3ExprDelete(db, p->pRight); | |
| 638 if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){ | |
| 639 sqlite3DbFree(db, p->u.zToken); | |
| 640 } | |
| 641 if( ExprHasProperty(p, EP_xIsSelect) ){ | |
| 642 sqlite3SelectDelete(db, p->x.pSelect); | |
| 643 }else{ | |
| 644 sqlite3ExprListDelete(db, p->x.pList); | |
| 645 } | |
| 646 } | |
| 647 } | |
| 648 | |
| 649 /* | |
| 650 ** Recursively delete an expression tree. | |
| 651 */ | |
| 652 void sqlite3ExprDelete(sqlite3 *db, Expr *p){ | |
| 653 if( p==0 ) return; | |
| 654 sqlite3ExprClear(db, p); | |
| 655 if( !ExprHasProperty(p, EP_Static) ){ | |
| 656 sqlite3DbFree(db, p); | |
| 657 } | |
| 658 } | |
| 659 | |
| 660 /* | |
| 661 ** Return the number of bytes allocated for the expression structure | |
| 662 ** passed as the first argument. This is always one of EXPR_FULLSIZE, | |
| 663 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. | |
| 664 */ | |
| 665 static int exprStructSize(Expr *p){ | |
| 666 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; | |
| 667 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; | |
| 668 return EXPR_FULLSIZE; | |
| 669 } | |
| 670 | |
| 671 /* | |
| 672 ** The dupedExpr*Size() routines each return the number of bytes required | |
| 673 ** to store a copy of an expression or expression tree. They differ in | |
| 674 ** how much of the tree is measured. | |
| 675 ** | |
| 676 ** dupedExprStructSize() Size of only the Expr structure | |
| 677 ** dupedExprNodeSize() Size of Expr + space for token | |
| 678 ** dupedExprSize() Expr + token + subtree components | |
| 679 ** | |
| 680 *************************************************************************** | |
| 681 ** | |
| 682 ** The dupedExprStructSize() function returns two values OR-ed together: | |
| 683 ** (1) the space required for a copy of the Expr structure only and | |
| 684 ** (2) the EP_xxx flags that indicate what the structure size should be. | |
| 685 ** The return values is always one of: | |
| 686 ** | |
| 687 ** EXPR_FULLSIZE | |
| 688 ** EXPR_REDUCEDSIZE | EP_Reduced | |
| 689 ** EXPR_TOKENONLYSIZE | EP_TokenOnly | |
| 690 ** | |
| 691 ** The size of the structure can be found by masking the return value | |
| 692 ** of this routine with 0xfff. The flags can be found by masking the | |
| 693 ** return value with EP_Reduced|EP_TokenOnly. | |
| 694 ** | |
| 695 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size | |
| 696 ** (unreduced) Expr objects as they or originally constructed by the parser. | |
| 697 ** During expression analysis, extra information is computed and moved into | |
| 698 ** later parts of teh Expr object and that extra information might get chopped | |
| 699 ** off if the expression is reduced. Note also that it does not work to | |
| 700 ** make a EXPRDUP_REDUCE copy of a reduced expression. It is only legal | |
| 701 ** to reduce a pristine expression tree from the parser. The implementation | |
| 702 ** of dupedExprStructSize() contain multiple assert() statements that attempt | |
| 703 ** to enforce this constraint. | |
| 704 */ | |
| 705 static int dupedExprStructSize(Expr *p, int flags){ | |
| 706 int nSize; | |
| 707 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ | |
| 708 if( 0==(flags&EXPRDUP_REDUCE) ){ | |
| 709 nSize = EXPR_FULLSIZE; | |
| 710 }else{ | |
| 711 assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) ); | |
| 712 assert( !ExprHasProperty(p, EP_FromJoin) ); | |
| 713 assert( (p->flags2 & EP2_MallocedToken)==0 ); | |
| 714 assert( (p->flags2 & EP2_Irreducible)==0 ); | |
| 715 if( p->pLeft || p->pRight || p->pColl || p->x.pList ){ | |
| 716 nSize = EXPR_REDUCEDSIZE | EP_Reduced; | |
| 717 }else{ | |
| 718 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; | |
| 719 } | |
| 720 } | |
| 721 return nSize; | |
| 722 } | |
| 723 | |
| 724 /* | |
| 725 ** This function returns the space in bytes required to store the copy | |
| 726 ** of the Expr structure and a copy of the Expr.u.zToken string (if that | |
| 727 ** string is defined.) | |
| 728 */ | |
| 729 static int dupedExprNodeSize(Expr *p, int flags){ | |
| 730 int nByte = dupedExprStructSize(p, flags) & 0xfff; | |
| 731 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ | |
| 732 nByte += sqlite3Strlen30(p->u.zToken)+1; | |
| 733 } | |
| 734 return ROUND8(nByte); | |
| 735 } | |
| 736 | |
| 737 /* | |
| 738 ** Return the number of bytes required to create a duplicate of the | |
| 739 ** expression passed as the first argument. The second argument is a | |
| 740 ** mask containing EXPRDUP_XXX flags. | |
| 741 ** | |
| 742 ** The value returned includes space to create a copy of the Expr struct | |
| 743 ** itself and the buffer referred to by Expr.u.zToken, if any. | |
| 744 ** | |
| 745 ** If the EXPRDUP_REDUCE flag is set, then the return value includes | |
| 746 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft | |
| 747 ** and Expr.pRight variables (but not for any structures pointed to or | |
| 748 ** descended from the Expr.x.pList or Expr.x.pSelect variables). | |
| 749 */ | |
| 750 static int dupedExprSize(Expr *p, int flags){ | |
| 751 int nByte = 0; | |
| 752 if( p ){ | |
| 753 nByte = dupedExprNodeSize(p, flags); | |
| 754 if( flags&EXPRDUP_REDUCE ){ | |
| 755 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); | |
| 756 } | |
| 757 } | |
| 758 return nByte; | |
| 759 } | |
| 760 | |
| 761 /* | |
| 762 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer | |
| 763 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough | |
| 764 ** to store the copy of expression p, the copies of p->u.zToken | |
| 765 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, | |
| 766 ** if any. Before returning, *pzBuffer is set to the first byte passed the | |
| 767 ** portion of the buffer copied into by this function. | |
| 768 */ | |
| 769 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ | |
| 770 Expr *pNew = 0; /* Value to return */ | |
| 771 if( p ){ | |
| 772 const int isReduced = (flags&EXPRDUP_REDUCE); | |
| 773 u8 *zAlloc; | |
| 774 u32 staticFlag = 0; | |
| 775 | |
| 776 assert( pzBuffer==0 || isReduced ); | |
| 777 | |
| 778 /* Figure out where to write the new Expr structure. */ | |
| 779 if( pzBuffer ){ | |
| 780 zAlloc = *pzBuffer; | |
| 781 staticFlag = EP_Static; | |
| 782 }else{ | |
| 783 zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags)); | |
| 784 } | |
| 785 pNew = (Expr *)zAlloc; | |
| 786 | |
| 787 if( pNew ){ | |
| 788 /* Set nNewSize to the size allocated for the structure pointed to | |
| 789 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or | |
| 790 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed | |
| 791 ** by the copy of the p->u.zToken string (if any). | |
| 792 */ | |
| 793 const unsigned nStructSize = dupedExprStructSize(p, flags); | |
| 794 const int nNewSize = nStructSize & 0xfff; | |
| 795 int nToken; | |
| 796 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ | |
| 797 nToken = sqlite3Strlen30(p->u.zToken) + 1; | |
| 798 }else{ | |
| 799 nToken = 0; | |
| 800 } | |
| 801 if( isReduced ){ | |
| 802 assert( ExprHasProperty(p, EP_Reduced)==0 ); | |
| 803 memcpy(zAlloc, p, nNewSize); | |
| 804 }else{ | |
| 805 int nSize = exprStructSize(p); | |
| 806 memcpy(zAlloc, p, nSize); | |
| 807 if( EXPR_FULLSIZE>nSize ){ | |
| 808 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); | |
| 809 } | |
| 810 } | |
| 811 | |
| 812 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ | |
| 813 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static); | |
| 814 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); | |
| 815 pNew->flags |= staticFlag; | |
| 816 | |
| 817 /* Copy the p->u.zToken string, if any. */ | |
| 818 if( nToken ){ | |
| 819 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; | |
| 820 memcpy(zToken, p->u.zToken, nToken); | |
| 821 } | |
| 822 | |
| 823 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ | |
| 824 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ | |
| 825 if( ExprHasProperty(p, EP_xIsSelect) ){ | |
| 826 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced); | |
| 827 }else{ | |
| 828 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced); | |
| 829 } | |
| 830 } | |
| 831 | |
| 832 /* Fill in pNew->pLeft and pNew->pRight. */ | |
| 833 if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){ | |
| 834 zAlloc += dupedExprNodeSize(p, flags); | |
| 835 if( ExprHasProperty(pNew, EP_Reduced) ){ | |
| 836 pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc); | |
| 837 pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc); | |
| 838 } | |
| 839 if( pzBuffer ){ | |
| 840 *pzBuffer = zAlloc; | |
| 841 } | |
| 842 }else{ | |
| 843 pNew->flags2 = 0; | |
| 844 if( !ExprHasAnyProperty(p, EP_TokenOnly) ){ | |
| 845 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); | |
| 846 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); | |
| 847 } | |
| 848 } | |
| 849 | |
| 850 } | |
| 851 } | |
| 852 return pNew; | |
| 853 } | |
| 854 | |
| 855 /* | |
| 856 ** The following group of routines make deep copies of expressions, | |
| 857 ** expression lists, ID lists, and select statements. The copies can | |
| 858 ** be deleted (by being passed to their respective ...Delete() routines) | |
| 859 ** without effecting the originals. | |
| 860 ** | |
| 861 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), | |
| 862 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded | |
| 863 ** by subsequent calls to sqlite*ListAppend() routines. | |
| 864 ** | |
| 865 ** Any tables that the SrcList might point to are not duplicated. | |
| 866 ** | |
| 867 ** The flags parameter contains a combination of the EXPRDUP_XXX flags. | |
| 868 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a | |
| 869 ** truncated version of the usual Expr structure that will be stored as | |
| 870 ** part of the in-memory representation of the database schema. | |
| 871 */ | |
| 872 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ | |
| 873 return exprDup(db, p, flags, 0); | |
| 874 } | |
| 875 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ | |
| 876 ExprList *pNew; | |
| 877 struct ExprList_item *pItem, *pOldItem; | |
| 878 int i; | |
| 879 if( p==0 ) return 0; | |
| 880 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); | |
| 881 if( pNew==0 ) return 0; | |
| 882 pNew->iECursor = 0; | |
| 883 pNew->nExpr = pNew->nAlloc = p->nExpr; | |
| 884 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); | |
| 885 if( pItem==0 ){ | |
| 886 sqlite3DbFree(db, pNew); | |
| 887 return 0; | |
| 888 } | |
| 889 pOldItem = p->a; | |
| 890 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ | |
| 891 Expr *pOldExpr = pOldItem->pExpr; | |
| 892 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); | |
| 893 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); | |
| 894 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); | |
| 895 pItem->sortOrder = pOldItem->sortOrder; | |
| 896 pItem->done = 0; | |
| 897 pItem->iCol = pOldItem->iCol; | |
| 898 pItem->iAlias = pOldItem->iAlias; | |
| 899 } | |
| 900 return pNew; | |
| 901 } | |
| 902 | |
| 903 /* | |
| 904 ** If cursors, triggers, views and subqueries are all omitted from | |
| 905 ** the build, then none of the following routines, except for | |
| 906 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes | |
| 907 ** called with a NULL argument. | |
| 908 */ | |
| 909 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ | |
| 910 || !defined(SQLITE_OMIT_SUBQUERY) | |
| 911 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ | |
| 912 SrcList *pNew; | |
| 913 int i; | |
| 914 int nByte; | |
| 915 if( p==0 ) return 0; | |
| 916 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); | |
| 917 pNew = sqlite3DbMallocRaw(db, nByte ); | |
| 918 if( pNew==0 ) return 0; | |
| 919 pNew->nSrc = pNew->nAlloc = p->nSrc; | |
| 920 for(i=0; i<p->nSrc; i++){ | |
| 921 struct SrcList_item *pNewItem = &pNew->a[i]; | |
| 922 struct SrcList_item *pOldItem = &p->a[i]; | |
| 923 Table *pTab; | |
| 924 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); | |
| 925 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); | |
| 926 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); | |
| 927 pNewItem->jointype = pOldItem->jointype; | |
| 928 pNewItem->iCursor = pOldItem->iCursor; | |
| 929 pNewItem->isPopulated = pOldItem->isPopulated; | |
| 930 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex); | |
| 931 pNewItem->notIndexed = pOldItem->notIndexed; | |
| 932 pNewItem->pIndex = pOldItem->pIndex; | |
| 933 pTab = pNewItem->pTab = pOldItem->pTab; | |
| 934 if( pTab ){ | |
| 935 pTab->nRef++; | |
| 936 } | |
| 937 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); | |
| 938 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); | |
| 939 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); | |
| 940 pNewItem->colUsed = pOldItem->colUsed; | |
| 941 } | |
| 942 return pNew; | |
| 943 } | |
| 944 IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ | |
| 945 IdList *pNew; | |
| 946 int i; | |
| 947 if( p==0 ) return 0; | |
| 948 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); | |
| 949 if( pNew==0 ) return 0; | |
| 950 pNew->nId = pNew->nAlloc = p->nId; | |
| 951 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); | |
| 952 if( pNew->a==0 ){ | |
| 953 sqlite3DbFree(db, pNew); | |
| 954 return 0; | |
| 955 } | |
| 956 for(i=0; i<p->nId; i++){ | |
| 957 struct IdList_item *pNewItem = &pNew->a[i]; | |
| 958 struct IdList_item *pOldItem = &p->a[i]; | |
| 959 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); | |
| 960 pNewItem->idx = pOldItem->idx; | |
| 961 } | |
| 962 return pNew; | |
| 963 } | |
| 964 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ | |
| 965 Select *pNew; | |
| 966 if( p==0 ) return 0; | |
| 967 pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); | |
| 968 if( pNew==0 ) return 0; | |
| 969 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); | |
| 970 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); | |
| 971 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); | |
| 972 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); | |
| 973 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); | |
| 974 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); | |
| 975 pNew->op = p->op; | |
| 976 pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags); | |
| 977 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); | |
| 978 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); | |
| 979 pNew->iLimit = 0; | |
| 980 pNew->iOffset = 0; | |
| 981 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; | |
| 982 pNew->pRightmost = 0; | |
| 983 pNew->addrOpenEphm[0] = -1; | |
| 984 pNew->addrOpenEphm[1] = -1; | |
| 985 pNew->addrOpenEphm[2] = -1; | |
| 986 return pNew; | |
| 987 } | |
| 988 #else | |
| 989 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ | |
| 990 assert( p==0 ); | |
| 991 return 0; | |
| 992 } | |
| 993 #endif | |
| 994 | |
| 995 | |
| 996 /* | |
| 997 ** Add a new element to the end of an expression list. If pList is | |
| 998 ** initially NULL, then create a new expression list. | |
| 999 ** | |
| 1000 ** If a memory allocation error occurs, the entire list is freed and | |
| 1001 ** NULL is returned. If non-NULL is returned, then it is guaranteed | |
| 1002 ** that the new entry was successfully appended. | |
| 1003 */ | |
| 1004 ExprList *sqlite3ExprListAppend( | |
| 1005 Parse *pParse, /* Parsing context */ | |
| 1006 ExprList *pList, /* List to which to append. Might be NULL */ | |
| 1007 Expr *pExpr /* Expression to be appended. Might be NULL */ | |
| 1008 ){ | |
| 1009 sqlite3 *db = pParse->db; | |
| 1010 if( pList==0 ){ | |
| 1011 pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); | |
| 1012 if( pList==0 ){ | |
| 1013 goto no_mem; | |
| 1014 } | |
| 1015 assert( pList->nAlloc==0 ); | |
| 1016 } | |
| 1017 if( pList->nAlloc<=pList->nExpr ){ | |
| 1018 struct ExprList_item *a; | |
| 1019 int n = pList->nAlloc*2 + 4; | |
| 1020 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); | |
| 1021 if( a==0 ){ | |
| 1022 goto no_mem; | |
| 1023 } | |
| 1024 pList->a = a; | |
| 1025 pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]); | |
| 1026 } | |
| 1027 assert( pList->a!=0 ); | |
| 1028 if( 1 ){ | |
| 1029 struct ExprList_item *pItem = &pList->a[pList->nExpr++]; | |
| 1030 memset(pItem, 0, sizeof(*pItem)); | |
| 1031 pItem->pExpr = pExpr; | |
| 1032 } | |
| 1033 return pList; | |
| 1034 | |
| 1035 no_mem: | |
| 1036 /* Avoid leaking memory if malloc has failed. */ | |
| 1037 sqlite3ExprDelete(db, pExpr); | |
| 1038 sqlite3ExprListDelete(db, pList); | |
| 1039 return 0; | |
| 1040 } | |
| 1041 | |
| 1042 /* | |
| 1043 ** Set the ExprList.a[].zName element of the most recently added item | |
| 1044 ** on the expression list. | |
| 1045 ** | |
| 1046 ** pList might be NULL following an OOM error. But pName should never be | |
| 1047 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag | |
| 1048 ** is set. | |
| 1049 */ | |
| 1050 void sqlite3ExprListSetName( | |
| 1051 Parse *pParse, /* Parsing context */ | |
| 1052 ExprList *pList, /* List to which to add the span. */ | |
| 1053 Token *pName, /* Name to be added */ | |
| 1054 int dequote /* True to cause the name to be dequoted */ | |
| 1055 ){ | |
| 1056 assert( pList!=0 || pParse->db->mallocFailed!=0 ); | |
| 1057 if( pList ){ | |
| 1058 struct ExprList_item *pItem; | |
| 1059 assert( pList->nExpr>0 ); | |
| 1060 pItem = &pList->a[pList->nExpr-1]; | |
| 1061 assert( pItem->zName==0 ); | |
| 1062 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); | |
| 1063 if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName); | |
| 1064 } | |
| 1065 } | |
| 1066 | |
| 1067 /* | |
| 1068 ** Set the ExprList.a[].zSpan element of the most recently added item | |
| 1069 ** on the expression list. | |
| 1070 ** | |
| 1071 ** pList might be NULL following an OOM error. But pSpan should never be | |
| 1072 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag | |
| 1073 ** is set. | |
| 1074 */ | |
| 1075 void sqlite3ExprListSetSpan( | |
| 1076 Parse *pParse, /* Parsing context */ | |
| 1077 ExprList *pList, /* List to which to add the span. */ | |
| 1078 ExprSpan *pSpan /* The span to be added */ | |
| 1079 ){ | |
| 1080 sqlite3 *db = pParse->db; | |
| 1081 assert( pList!=0 || db->mallocFailed!=0 ); | |
| 1082 if( pList ){ | |
| 1083 struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; | |
| 1084 assert( pList->nExpr>0 ); | |
| 1085 assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); | |
| 1086 sqlite3DbFree(db, pItem->zSpan); | |
| 1087 pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, | |
| 1088 (int)(pSpan->zEnd - pSpan->zStart)); | |
| 1089 } | |
| 1090 } | |
| 1091 | |
| 1092 /* | |
| 1093 ** If the expression list pEList contains more than iLimit elements, | |
| 1094 ** leave an error message in pParse. | |
| 1095 */ | |
| 1096 void sqlite3ExprListCheckLength( | |
| 1097 Parse *pParse, | |
| 1098 ExprList *pEList, | |
| 1099 const char *zObject | |
| 1100 ){ | |
| 1101 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; | |
| 1102 testcase( pEList && pEList->nExpr==mx ); | |
| 1103 testcase( pEList && pEList->nExpr==mx+1 ); | |
| 1104 if( pEList && pEList->nExpr>mx ){ | |
| 1105 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); | |
| 1106 } | |
| 1107 } | |
| 1108 | |
| 1109 /* | |
| 1110 ** Delete an entire expression list. | |
| 1111 */ | |
| 1112 void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ | |
| 1113 int i; | |
| 1114 struct ExprList_item *pItem; | |
| 1115 if( pList==0 ) return; | |
| 1116 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); | |
| 1117 assert( pList->nExpr<=pList->nAlloc ); | |
| 1118 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ | |
| 1119 sqlite3ExprDelete(db, pItem->pExpr); | |
| 1120 sqlite3DbFree(db, pItem->zName); | |
| 1121 sqlite3DbFree(db, pItem->zSpan); | |
| 1122 } | |
| 1123 sqlite3DbFree(db, pList->a); | |
| 1124 sqlite3DbFree(db, pList); | |
| 1125 } | |
| 1126 | |
| 1127 /* | |
| 1128 ** These routines are Walker callbacks. Walker.u.pi is a pointer | |
| 1129 ** to an integer. These routines are checking an expression to see | |
| 1130 ** if it is a constant. Set *Walker.u.pi to 0 if the expression is | |
| 1131 ** not constant. | |
| 1132 ** | |
| 1133 ** These callback routines are used to implement the following: | |
| 1134 ** | |
| 1135 ** sqlite3ExprIsConstant() | |
| 1136 ** sqlite3ExprIsConstantNotJoin() | |
| 1137 ** sqlite3ExprIsConstantOrFunction() | |
| 1138 ** | |
| 1139 */ | |
| 1140 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ | |
| 1141 | |
| 1142 /* If pWalker->u.i is 3 then any term of the expression that comes from | |
| 1143 ** the ON or USING clauses of a join disqualifies the expression | |
| 1144 ** from being considered constant. */ | |
| 1145 if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ | |
| 1146 pWalker->u.i = 0; | |
| 1147 return WRC_Abort; | |
| 1148 } | |
| 1149 | |
| 1150 switch( pExpr->op ){ | |
| 1151 /* Consider functions to be constant if all their arguments are constant | |
| 1152 ** and pWalker->u.i==2 */ | |
| 1153 case TK_FUNCTION: | |
| 1154 if( pWalker->u.i==2 ) return 0; | |
| 1155 /* Fall through */ | |
| 1156 case TK_ID: | |
| 1157 case TK_COLUMN: | |
| 1158 case TK_AGG_FUNCTION: | |
| 1159 case TK_AGG_COLUMN: | |
| 1160 testcase( pExpr->op==TK_ID ); | |
| 1161 testcase( pExpr->op==TK_COLUMN ); | |
| 1162 testcase( pExpr->op==TK_AGG_FUNCTION ); | |
| 1163 testcase( pExpr->op==TK_AGG_COLUMN ); | |
| 1164 pWalker->u.i = 0; | |
| 1165 return WRC_Abort; | |
| 1166 default: | |
| 1167 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ | |
| 1168 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ | |
| 1169 return WRC_Continue; | |
| 1170 } | |
| 1171 } | |
| 1172 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ | |
| 1173 UNUSED_PARAMETER(NotUsed); | |
| 1174 pWalker->u.i = 0; | |
| 1175 return WRC_Abort; | |
| 1176 } | |
| 1177 static int exprIsConst(Expr *p, int initFlag){ | |
| 1178 Walker w; | |
| 1179 w.u.i = initFlag; | |
| 1180 w.xExprCallback = exprNodeIsConstant; | |
| 1181 w.xSelectCallback = selectNodeIsConstant; | |
| 1182 sqlite3WalkExpr(&w, p); | |
| 1183 return w.u.i; | |
| 1184 } | |
| 1185 | |
| 1186 /* | |
| 1187 ** Walk an expression tree. Return 1 if the expression is constant | |
| 1188 ** and 0 if it involves variables or function calls. | |
| 1189 ** | |
| 1190 ** For the purposes of this function, a double-quoted string (ex: "abc") | |
| 1191 ** is considered a variable but a single-quoted string (ex: 'abc') is | |
| 1192 ** a constant. | |
| 1193 */ | |
| 1194 int sqlite3ExprIsConstant(Expr *p){ | |
| 1195 return exprIsConst(p, 1); | |
| 1196 } | |
| 1197 | |
| 1198 /* | |
| 1199 ** Walk an expression tree. Return 1 if the expression is constant | |
| 1200 ** that does no originate from the ON or USING clauses of a join. | |
| 1201 ** Return 0 if it involves variables or function calls or terms from | |
| 1202 ** an ON or USING clause. | |
| 1203 */ | |
| 1204 int sqlite3ExprIsConstantNotJoin(Expr *p){ | |
| 1205 return exprIsConst(p, 3); | |
| 1206 } | |
| 1207 | |
| 1208 /* | |
| 1209 ** Walk an expression tree. Return 1 if the expression is constant | |
| 1210 ** or a function call with constant arguments. Return and 0 if there | |
| 1211 ** are any variables. | |
| 1212 ** | |
| 1213 ** For the purposes of this function, a double-quoted string (ex: "abc") | |
| 1214 ** is considered a variable but a single-quoted string (ex: 'abc') is | |
| 1215 ** a constant. | |
| 1216 */ | |
| 1217 int sqlite3ExprIsConstantOrFunction(Expr *p){ | |
| 1218 return exprIsConst(p, 2); | |
| 1219 } | |
| 1220 | |
| 1221 /* | |
| 1222 ** If the expression p codes a constant integer that is small enough | |
| 1223 ** to fit in a 32-bit integer, return 1 and put the value of the integer | |
| 1224 ** in *pValue. If the expression is not an integer or if it is too big | |
| 1225 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. | |
| 1226 */ | |
| 1227 int sqlite3ExprIsInteger(Expr *p, int *pValue){ | |
| 1228 int rc = 0; | |
| 1229 if( p->flags & EP_IntValue ){ | |
| 1230 *pValue = p->u.iValue; | |
| 1231 return 1; | |
| 1232 } | |
| 1233 switch( p->op ){ | |
| 1234 case TK_INTEGER: { | |
| 1235 rc = sqlite3GetInt32(p->u.zToken, pValue); | |
| 1236 assert( rc==0 ); | |
| 1237 break; | |
| 1238 } | |
| 1239 case TK_UPLUS: { | |
| 1240 rc = sqlite3ExprIsInteger(p->pLeft, pValue); | |
| 1241 break; | |
| 1242 } | |
| 1243 case TK_UMINUS: { | |
| 1244 int v; | |
| 1245 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ | |
| 1246 *pValue = -v; | |
| 1247 rc = 1; | |
| 1248 } | |
| 1249 break; | |
| 1250 } | |
| 1251 default: break; | |
| 1252 } | |
| 1253 if( rc ){ | |
| 1254 assert( ExprHasAnyProperty(p, EP_Reduced|EP_TokenOnly) | |
| 1255 || (p->flags2 & EP2_MallocedToken)==0 ); | |
| 1256 p->op = TK_INTEGER; | |
| 1257 p->flags |= EP_IntValue; | |
| 1258 p->u.iValue = *pValue; | |
| 1259 } | |
| 1260 return rc; | |
| 1261 } | |
| 1262 | |
| 1263 /* | |
| 1264 ** Return TRUE if the given string is a row-id column name. | |
| 1265 */ | |
| 1266 int sqlite3IsRowid(const char *z){ | |
| 1267 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; | |
| 1268 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; | |
| 1269 if( sqlite3StrICmp(z, "OID")==0 ) return 1; | |
| 1270 return 0; | |
| 1271 } | |
| 1272 | |
| 1273 /* | |
| 1274 ** Return true if we are able to the IN operator optimization on a | |
| 1275 ** query of the form | |
| 1276 ** | |
| 1277 ** x IN (SELECT ...) | |
| 1278 ** | |
| 1279 ** Where the SELECT... clause is as specified by the parameter to this | |
| 1280 ** routine. | |
| 1281 ** | |
| 1282 ** The Select object passed in has already been preprocessed and no | |
| 1283 ** errors have been found. | |
| 1284 */ | |
| 1285 #ifndef SQLITE_OMIT_SUBQUERY | |
| 1286 static int isCandidateForInOpt(Select *p){ | |
| 1287 SrcList *pSrc; | |
| 1288 ExprList *pEList; | |
| 1289 Table *pTab; | |
| 1290 if( p==0 ) return 0; /* right-hand side of IN is SELECT */ | |
| 1291 if( p->pPrior ) return 0; /* Not a compound SELECT */ | |
| 1292 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ | |
| 1293 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); | |
| 1294 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); | |
| 1295 return 0; /* No DISTINCT keyword and no aggregate functions */ | |
| 1296 } | |
| 1297 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ | |
| 1298 if( p->pLimit ) return 0; /* Has no LIMIT clause */ | |
| 1299 assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ | |
| 1300 if( p->pWhere ) return 0; /* Has no WHERE clause */ | |
| 1301 pSrc = p->pSrc; | |
| 1302 assert( pSrc!=0 ); | |
| 1303 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ | |
| 1304 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ | |
| 1305 pTab = pSrc->a[0].pTab; | |
| 1306 if( NEVER(pTab==0) ) return 0; | |
| 1307 assert( pTab->pSelect==0 ); /* FROM clause is not a view */ | |
| 1308 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ | |
| 1309 pEList = p->pEList; | |
| 1310 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ | |
| 1311 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */ | |
| 1312 return 1; | |
| 1313 } | |
| 1314 #endif /* SQLITE_OMIT_SUBQUERY */ | |
| 1315 | |
| 1316 /* | |
| 1317 ** This function is used by the implementation of the IN (...) operator. | |
| 1318 ** It's job is to find or create a b-tree structure that may be used | |
| 1319 ** either to test for membership of the (...) set or to iterate through | |
| 1320 ** its members, skipping duplicates. | |
| 1321 ** | |
| 1322 ** The index of the cursor opened on the b-tree (database table, database index | |
| 1323 ** or ephermal table) is stored in pX->iTable before this function returns. | |
| 1324 ** The returned value of this function indicates the b-tree type, as follows: | |
| 1325 ** | |
| 1326 ** IN_INDEX_ROWID - The cursor was opened on a database table. | |
| 1327 ** IN_INDEX_INDEX - The cursor was opened on a database index. | |
| 1328 ** IN_INDEX_EPH - The cursor was opened on a specially created and | |
| 1329 ** populated epheremal table. | |
| 1330 ** | |
| 1331 ** An existing b-tree may only be used if the SELECT is of the simple | |
| 1332 ** form: | |
| 1333 ** | |
| 1334 ** SELECT <column> FROM <table> | |
| 1335 ** | |
| 1336 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate | |
| 1337 ** through the set members, skipping any duplicates. In this case an | |
| 1338 ** epheremal table must be used unless the selected <column> is guaranteed | |
| 1339 ** to be unique - either because it is an INTEGER PRIMARY KEY or it | |
| 1340 ** has a UNIQUE constraint or UNIQUE index. | |
| 1341 ** | |
| 1342 ** If the prNotFound parameter is not 0, then the b-tree will be used | |
| 1343 ** for fast set membership tests. In this case an epheremal table must | |
| 1344 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can | |
| 1345 ** be found with <column> as its left-most column. | |
| 1346 ** | |
| 1347 ** When the b-tree is being used for membership tests, the calling function | |
| 1348 ** needs to know whether or not the structure contains an SQL NULL | |
| 1349 ** value in order to correctly evaluate expressions like "X IN (Y, Z)". | |
| 1350 ** If there is a chance that the b-tree might contain a NULL value at | |
| 1351 ** runtime, then a register is allocated and the register number written | |
| 1352 ** to *prNotFound. If there is no chance that the b-tree contains a | |
| 1353 ** NULL value, then *prNotFound is left unchanged. | |
| 1354 ** | |
| 1355 ** If a register is allocated and its location stored in *prNotFound, then | |
| 1356 ** its initial value is NULL. If the b-tree does not remain constant | |
| 1357 ** for the duration of the query (i.e. the SELECT that generates the b-tree | |
| 1358 ** is a correlated subquery) then the value of the allocated register is | |
| 1359 ** reset to NULL each time the b-tree is repopulated. This allows the | |
| 1360 ** caller to use vdbe code equivalent to the following: | |
| 1361 ** | |
| 1362 ** if( register==NULL ){ | |
| 1363 ** has_null = <test if data structure contains null> | |
| 1364 ** register = 1 | |
| 1365 ** } | |
| 1366 ** | |
| 1367 ** in order to avoid running the <test if data structure contains null> | |
| 1368 ** test more often than is necessary. | |
| 1369 */ | |
| 1370 #ifndef SQLITE_OMIT_SUBQUERY | |
| 1371 int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){ | |
| 1372 Select *p; /* SELECT to the right of IN operator */ | |
| 1373 int eType = 0; /* Type of RHS table. IN_INDEX_* */ | |
| 1374 int iTab = pParse->nTab++; /* Cursor of the RHS table */ | |
| 1375 int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */ | |
| 1376 | |
| 1377 /* Check to see if an existing table or index can be used to | |
| 1378 ** satisfy the query. This is preferable to generating a new | |
| 1379 ** ephemeral table. | |
| 1380 */ | |
| 1381 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); | |
| 1382 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ | |
| 1383 sqlite3 *db = pParse->db; /* Database connection */ | |
| 1384 Expr *pExpr = p->pEList->a[0].pExpr; /* Expression <column> */ | |
| 1385 int iCol = pExpr->iColumn; /* Index of column <column> */ | |
| 1386 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ | |
| 1387 Table *pTab = p->pSrc->a[0].pTab; /* Table <table>. */ | |
| 1388 int iDb; /* Database idx for pTab */ | |
| 1389 | |
| 1390 /* Code an OP_VerifyCookie and OP_TableLock for <table>. */ | |
| 1391 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | |
| 1392 sqlite3CodeVerifySchema(pParse, iDb); | |
| 1393 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); | |
| 1394 | |
| 1395 /* This function is only called from two places. In both cases the vdbe | |
| 1396 ** has already been allocated. So assume sqlite3GetVdbe() is always | |
| 1397 ** successful here. | |
| 1398 */ | |
| 1399 assert(v); | |
| 1400 if( iCol<0 ){ | |
| 1401 int iMem = ++pParse->nMem; | |
| 1402 int iAddr; | |
| 1403 | |
| 1404 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); | |
| 1405 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); | |
| 1406 | |
| 1407 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); | |
| 1408 eType = IN_INDEX_ROWID; | |
| 1409 | |
| 1410 sqlite3VdbeJumpHere(v, iAddr); | |
| 1411 }else{ | |
| 1412 Index *pIdx; /* Iterator variable */ | |
| 1413 | |
| 1414 /* The collation sequence used by the comparison. If an index is to | |
| 1415 ** be used in place of a temp-table, it must be ordered according | |
| 1416 ** to this collation sequence. */ | |
| 1417 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); | |
| 1418 | |
| 1419 /* Check that the affinity that will be used to perform the | |
| 1420 ** comparison is the same as the affinity of the column. If | |
| 1421 ** it is not, it is not possible to use any index. | |
| 1422 */ | |
| 1423 char aff = comparisonAffinity(pX); | |
| 1424 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); | |
| 1425 | |
| 1426 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ | |
| 1427 if( (pIdx->aiColumn[0]==iCol) | |
| 1428 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq | |
| 1429 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) | |
| 1430 ){ | |
| 1431 int iMem = ++pParse->nMem; | |
| 1432 int iAddr; | |
| 1433 char *pKey; | |
| 1434 | |
| 1435 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); | |
| 1436 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); | |
| 1437 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); | |
| 1438 | |
| 1439 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, | |
| 1440 pKey,P4_KEYINFO_HANDOFF); | |
| 1441 VdbeComment((v, "%s", pIdx->zName)); | |
| 1442 eType = IN_INDEX_INDEX; | |
| 1443 | |
| 1444 sqlite3VdbeJumpHere(v, iAddr); | |
| 1445 if( prNotFound && !pTab->aCol[iCol].notNull ){ | |
| 1446 *prNotFound = ++pParse->nMem; | |
| 1447 } | |
| 1448 } | |
| 1449 } | |
| 1450 } | |
| 1451 } | |
| 1452 | |
| 1453 if( eType==0 ){ | |
| 1454 /* Could not found an existing able or index to use as the RHS b-tree. | |
| 1455 ** We will have to generate an ephemeral table to do the job. | |
| 1456 */ | |
| 1457 int rMayHaveNull = 0; | |
| 1458 eType = IN_INDEX_EPH; | |
| 1459 if( prNotFound ){ | |
| 1460 *prNotFound = rMayHaveNull = ++pParse->nMem; | |
| 1461 }else if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){ | |
| 1462 eType = IN_INDEX_ROWID; | |
| 1463 } | |
| 1464 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); | |
| 1465 }else{ | |
| 1466 pX->iTable = iTab; | |
| 1467 } | |
| 1468 return eType; | |
| 1469 } | |
| 1470 #endif | |
| 1471 | |
| 1472 /* | |
| 1473 ** Generate code for scalar subqueries used as an expression | |
| 1474 ** and IN operators. Examples: | |
| 1475 ** | |
| 1476 ** (SELECT a FROM b) -- subquery | |
| 1477 ** EXISTS (SELECT a FROM b) -- EXISTS subquery | |
| 1478 ** x IN (4,5,11) -- IN operator with list on right-hand side | |
| 1479 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right | |
| 1480 ** | |
| 1481 ** The pExpr parameter describes the expression that contains the IN | |
| 1482 ** operator or subquery. | |
| 1483 ** | |
| 1484 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed | |
| 1485 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference | |
| 1486 ** to some integer key column of a table B-Tree. In this case, use an | |
| 1487 ** intkey B-Tree to store the set of IN(...) values instead of the usual | |
| 1488 ** (slower) variable length keys B-Tree. | |
| 1489 ** | |
| 1490 ** If rMayHaveNull is non-zero, that means that the operation is an IN | |
| 1491 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. | |
| 1492 ** Furthermore, the IN is in a WHERE clause and that we really want | |
| 1493 ** to iterate over the RHS of the IN operator in order to quickly locate | |
| 1494 ** all corresponding LHS elements. All this routine does is initialize | |
| 1495 ** the register given by rMayHaveNull to NULL. Calling routines will take | |
| 1496 ** care of changing this register value to non-NULL if the RHS is NULL-free. | |
| 1497 ** | |
| 1498 ** If rMayHaveNull is zero, that means that the subquery is being used | |
| 1499 ** for membership testing only. There is no need to initialize any | |
| 1500 ** registers to indicate the presense or absence of NULLs on the RHS. | |
| 1501 */ | |
| 1502 #ifndef SQLITE_OMIT_SUBQUERY | |
| 1503 void sqlite3CodeSubselect( | |
| 1504 Parse *pParse, /* Parsing context */ | |
| 1505 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ | |
| 1506 int rMayHaveNull, /* Register that records whether NULLs exist in RHS */ | |
| 1507 int isRowid /* If true, LHS of IN operator is a rowid */ | |
| 1508 ){ | |
| 1509 int testAddr = 0; /* One-time test address */ | |
| 1510 Vdbe *v = sqlite3GetVdbe(pParse); | |
| 1511 if( NEVER(v==0) ) return; | |
| 1512 sqlite3ExprCachePush(pParse); | |
| 1513 | |
| 1514 /* This code must be run in its entirety every time it is encountered | |
| 1515 ** if any of the following is true: | |
| 1516 ** | |
| 1517 ** * The right-hand side is a correlated subquery | |
| 1518 ** * The right-hand side is an expression list containing variables | |
| 1519 ** * We are inside a trigger | |
| 1520 ** | |
| 1521 ** If all of the above are false, then we can run this code just once | |
| 1522 ** save the results, and reuse the same result on subsequent invocations. | |
| 1523 */ | |
| 1524 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){ | |
| 1525 int mem = ++pParse->nMem; | |
| 1526 sqlite3VdbeAddOp1(v, OP_If, mem); | |
| 1527 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); | |
| 1528 assert( testAddr>0 || pParse->db->mallocFailed ); | |
| 1529 } | |
| 1530 | |
| 1531 switch( pExpr->op ){ | |
| 1532 case TK_IN: { | |
| 1533 char affinity; | |
| 1534 KeyInfo keyInfo; | |
| 1535 int addr; /* Address of OP_OpenEphemeral instruction */ | |
| 1536 Expr *pLeft = pExpr->pLeft; | |
| 1537 | |
| 1538 if( rMayHaveNull ){ | |
| 1539 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); | |
| 1540 } | |
| 1541 | |
| 1542 affinity = sqlite3ExprAffinity(pLeft); | |
| 1543 | |
| 1544 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' | |
| 1545 ** expression it is handled the same way. A virtual table is | |
| 1546 ** filled with single-field index keys representing the results | |
| 1547 ** from the SELECT or the <exprlist>. | |
| 1548 ** | |
| 1549 ** If the 'x' expression is a column value, or the SELECT... | |
| 1550 ** statement returns a column value, then the affinity of that | |
| 1551 ** column is used to build the index keys. If both 'x' and the | |
| 1552 ** SELECT... statement are columns, then numeric affinity is used | |
| 1553 ** if either column has NUMERIC or INTEGER affinity. If neither | |
| 1554 ** 'x' nor the SELECT... statement are columns, then numeric affinity | |
| 1555 ** is used. | |
| 1556 */ | |
| 1557 pExpr->iTable = pParse->nTab++; | |
| 1558 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); | |
| 1559 memset(&keyInfo, 0, sizeof(keyInfo)); | |
| 1560 keyInfo.nField = 1; | |
| 1561 | |
| 1562 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ | |
| 1563 /* Case 1: expr IN (SELECT ...) | |
| 1564 ** | |
| 1565 ** Generate code to write the results of the select into the temporary | |
| 1566 ** table allocated and opened above. | |
| 1567 */ | |
| 1568 SelectDest dest; | |
| 1569 ExprList *pEList; | |
| 1570 | |
| 1571 assert( !isRowid ); | |
| 1572 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); | |
| 1573 dest.affinity = (u8)affinity; | |
| 1574 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); | |
| 1575 if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){ | |
| 1576 return; | |
| 1577 } | |
| 1578 pEList = pExpr->x.pSelect->pEList; | |
| 1579 if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ | |
| 1580 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, | |
| 1581 pEList->a[0].pExpr); | |
| 1582 } | |
| 1583 }else if( pExpr->x.pList!=0 ){ | |
| 1584 /* Case 2: expr IN (exprlist) | |
| 1585 ** | |
| 1586 ** For each expression, build an index key from the evaluation and | |
| 1587 ** store it in the temporary table. If <expr> is a column, then use | |
| 1588 ** that columns affinity when building index keys. If <expr> is not | |
| 1589 ** a column, use numeric affinity. | |
| 1590 */ | |
| 1591 int i; | |
| 1592 ExprList *pList = pExpr->x.pList; | |
| 1593 struct ExprList_item *pItem; | |
| 1594 int r1, r2, r3; | |
| 1595 | |
| 1596 if( !affinity ){ | |
| 1597 affinity = SQLITE_AFF_NONE; | |
| 1598 } | |
| 1599 keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); | |
| 1600 | |
| 1601 /* Loop through each expression in <exprlist>. */ | |
| 1602 r1 = sqlite3GetTempReg(pParse); | |
| 1603 r2 = sqlite3GetTempReg(pParse); | |
| 1604 sqlite3VdbeAddOp2(v, OP_Null, 0, r2); | |
| 1605 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ | |
| 1606 Expr *pE2 = pItem->pExpr; | |
| 1607 | |
| 1608 /* If the expression is not constant then we will need to | |
| 1609 ** disable the test that was generated above that makes sure | |
| 1610 ** this code only executes once. Because for a non-constant | |
| 1611 ** expression we need to rerun this code each time. | |
| 1612 */ | |
| 1613 if( testAddr && !sqlite3ExprIsConstant(pE2) ){ | |
| 1614 sqlite3VdbeChangeToNoop(v, testAddr-1, 2); | |
| 1615 testAddr = 0; | |
| 1616 } | |
| 1617 | |
| 1618 /* Evaluate the expression and insert it into the temp table */ | |
| 1619 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); | |
| 1620 if( isRowid ){ | |
| 1621 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, sqlite3VdbeCurrentAddr(v)+2); | |
| 1622 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); | |
| 1623 }else{ | |
| 1624 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); | |
| 1625 sqlite3ExprCacheAffinityChange(pParse, r3, 1); | |
| 1626 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); | |
| 1627 } | |
| 1628 } | |
| 1629 sqlite3ReleaseTempReg(pParse, r1); | |
| 1630 sqlite3ReleaseTempReg(pParse, r2); | |
| 1631 } | |
| 1632 if( !isRowid ){ | |
| 1633 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); | |
| 1634 } | |
| 1635 break; | |
| 1636 } | |
| 1637 | |
| 1638 case TK_EXISTS: | |
| 1639 case TK_SELECT: | |
| 1640 default: { | |
| 1641 /* If this has to be a scalar SELECT. Generate code to put the | |
| 1642 ** value of this select in a memory cell and record the number | |
| 1643 ** of the memory cell in iColumn. If this is an EXISTS, write | |
| 1644 ** an integer 0 (not exists) or 1 (exists) into a memory cell | |
| 1645 ** and record that memory cell in iColumn. | |
| 1646 */ | |
| 1647 static const Token one = { "1", 1 }; /* Token for literal value 1 */ | |
| 1648 Select *pSel; /* SELECT statement to encode */ | |
| 1649 SelectDest dest; /* How to deal with SELECt result */ | |
| 1650 | |
| 1651 testcase( pExpr->op==TK_EXISTS ); | |
| 1652 testcase( pExpr->op==TK_SELECT ); | |
| 1653 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); | |
| 1654 | |
| 1655 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); | |
| 1656 pSel = pExpr->x.pSelect; | |
| 1657 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); | |
| 1658 if( pExpr->op==TK_SELECT ){ | |
| 1659 dest.eDest = SRT_Mem; | |
| 1660 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); | |
| 1661 VdbeComment((v, "Init subquery result")); | |
| 1662 }else{ | |
| 1663 dest.eDest = SRT_Exists; | |
| 1664 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); | |
| 1665 VdbeComment((v, "Init EXISTS result")); | |
| 1666 } | |
| 1667 sqlite3ExprDelete(pParse->db, pSel->pLimit); | |
| 1668 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); | |
| 1669 if( sqlite3Select(pParse, pSel, &dest) ){ | |
| 1670 return; | |
| 1671 } | |
| 1672 pExpr->iColumn = (i16)dest.iParm; | |
| 1673 ExprSetIrreducible(pExpr); | |
| 1674 break; | |
| 1675 } | |
| 1676 } | |
| 1677 | |
| 1678 if( testAddr ){ | |
| 1679 sqlite3VdbeJumpHere(v, testAddr-1); | |
| 1680 } | |
| 1681 sqlite3ExprCachePop(pParse, 1); | |
| 1682 | |
| 1683 return; | |
| 1684 } | |
| 1685 #endif /* SQLITE_OMIT_SUBQUERY */ | |
| 1686 | |
| 1687 /* | |
| 1688 ** Duplicate an 8-byte value | |
| 1689 */ | |
| 1690 static char *dup8bytes(Vdbe *v, const char *in){ | |
| 1691 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); | |
| 1692 if( out ){ | |
| 1693 memcpy(out, in, 8); | |
| 1694 } | |
| 1695 return out; | |
| 1696 } | |
| 1697 | |
| 1698 /* | |
| 1699 ** Generate an instruction that will put the floating point | |
| 1700 ** value described by z[0..n-1] into register iMem. | |
| 1701 ** | |
| 1702 ** The z[] string will probably not be zero-terminated. But the | |
| 1703 ** z[n] character is guaranteed to be something that does not look | |
| 1704 ** like the continuation of the number. | |
| 1705 */ | |
| 1706 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ | |
| 1707 if( ALWAYS(z!=0) ){ | |
| 1708 double value; | |
| 1709 char *zV; | |
| 1710 sqlite3AtoF(z, &value); | |
| 1711 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ | |
| 1712 if( negateFlag ) value = -value; | |
| 1713 zV = dup8bytes(v, (char*)&value); | |
| 1714 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); | |
| 1715 } | |
| 1716 } | |
| 1717 | |
| 1718 | |
| 1719 /* | |
| 1720 ** Generate an instruction that will put the integer describe by | |
| 1721 ** text z[0..n-1] into register iMem. | |
| 1722 ** | |
| 1723 ** The z[] string will probably not be zero-terminated. But the | |
| 1724 ** z[n] character is guaranteed to be something that does not look | |
| 1725 ** like the continuation of the number. | |
| 1726 */ | |
| 1727 static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){ | |
| 1728 if( pExpr->flags & EP_IntValue ){ | |
| 1729 int i = pExpr->u.iValue; | |
| 1730 if( negFlag ) i = -i; | |
| 1731 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); | |
| 1732 }else{ | |
| 1733 const char *z = pExpr->u.zToken; | |
| 1734 assert( z!=0 ); | |
| 1735 if( sqlite3FitsIn64Bits(z, negFlag) ){ | |
| 1736 i64 value; | |
| 1737 char *zV; | |
| 1738 sqlite3Atoi64(z, &value); | |
| 1739 if( negFlag ) value = -value; | |
| 1740 zV = dup8bytes(v, (char*)&value); | |
| 1741 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); | |
| 1742 }else{ | |
| 1743 codeReal(v, z, negFlag, iMem); | |
| 1744 } | |
| 1745 } | |
| 1746 } | |
| 1747 | |
| 1748 /* | |
| 1749 ** Clear a cache entry. | |
| 1750 */ | |
| 1751 static void cacheEntryClear(Parse *pParse, struct yColCache *p){ | |
| 1752 if( p->tempReg ){ | |
| 1753 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ | |
| 1754 pParse->aTempReg[pParse->nTempReg++] = p->iReg; | |
| 1755 } | |
| 1756 p->tempReg = 0; | |
| 1757 } | |
| 1758 } | |
| 1759 | |
| 1760 | |
| 1761 /* | |
| 1762 ** Record in the column cache that a particular column from a | |
| 1763 ** particular table is stored in a particular register. | |
| 1764 */ | |
| 1765 void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ | |
| 1766 int i; | |
| 1767 int minLru; | |
| 1768 int idxLru; | |
| 1769 struct yColCache *p; | |
| 1770 | |
| 1771 assert( iReg>0 ); /* Register numbers are always positive */ | |
| 1772 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ | |
| 1773 | |
| 1774 /* First replace any existing entry */ | |
| 1775 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1776 if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){ | |
| 1777 cacheEntryClear(pParse, p); | |
| 1778 p->iLevel = pParse->iCacheLevel; | |
| 1779 p->iReg = iReg; | |
| 1780 p->affChange = 0; | |
| 1781 p->lru = pParse->iCacheCnt++; | |
| 1782 return; | |
| 1783 } | |
| 1784 } | |
| 1785 | |
| 1786 /* Find an empty slot and replace it */ | |
| 1787 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1788 if( p->iReg==0 ){ | |
| 1789 p->iLevel = pParse->iCacheLevel; | |
| 1790 p->iTable = iTab; | |
| 1791 p->iColumn = iCol; | |
| 1792 p->iReg = iReg; | |
| 1793 p->affChange = 0; | |
| 1794 p->tempReg = 0; | |
| 1795 p->lru = pParse->iCacheCnt++; | |
| 1796 return; | |
| 1797 } | |
| 1798 } | |
| 1799 | |
| 1800 /* Replace the last recently used */ | |
| 1801 minLru = 0x7fffffff; | |
| 1802 idxLru = -1; | |
| 1803 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1804 if( p->lru<minLru ){ | |
| 1805 idxLru = i; | |
| 1806 minLru = p->lru; | |
| 1807 } | |
| 1808 } | |
| 1809 if( ALWAYS(idxLru>=0) ){ | |
| 1810 p = &pParse->aColCache[idxLru]; | |
| 1811 p->iLevel = pParse->iCacheLevel; | |
| 1812 p->iTable = iTab; | |
| 1813 p->iColumn = iCol; | |
| 1814 p->iReg = iReg; | |
| 1815 p->affChange = 0; | |
| 1816 p->tempReg = 0; | |
| 1817 p->lru = pParse->iCacheCnt++; | |
| 1818 return; | |
| 1819 } | |
| 1820 } | |
| 1821 | |
| 1822 /* | |
| 1823 ** Indicate that a register is being overwritten. Purge the register | |
| 1824 ** from the column cache. | |
| 1825 */ | |
| 1826 void sqlite3ExprCacheRemove(Parse *pParse, int iReg){ | |
| 1827 int i; | |
| 1828 struct yColCache *p; | |
| 1829 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1830 if( p->iReg==iReg ){ | |
| 1831 cacheEntryClear(pParse, p); | |
| 1832 p->iReg = 0; | |
| 1833 } | |
| 1834 } | |
| 1835 } | |
| 1836 | |
| 1837 /* | |
| 1838 ** Remember the current column cache context. Any new entries added | |
| 1839 ** added to the column cache after this call are removed when the | |
| 1840 ** corresponding pop occurs. | |
| 1841 */ | |
| 1842 void sqlite3ExprCachePush(Parse *pParse){ | |
| 1843 pParse->iCacheLevel++; | |
| 1844 } | |
| 1845 | |
| 1846 /* | |
| 1847 ** Remove from the column cache any entries that were added since the | |
| 1848 ** the previous N Push operations. In other words, restore the cache | |
| 1849 ** to the state it was in N Pushes ago. | |
| 1850 */ | |
| 1851 void sqlite3ExprCachePop(Parse *pParse, int N){ | |
| 1852 int i; | |
| 1853 struct yColCache *p; | |
| 1854 assert( N>0 ); | |
| 1855 assert( pParse->iCacheLevel>=N ); | |
| 1856 pParse->iCacheLevel -= N; | |
| 1857 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1858 if( p->iReg && p->iLevel>pParse->iCacheLevel ){ | |
| 1859 cacheEntryClear(pParse, p); | |
| 1860 p->iReg = 0; | |
| 1861 } | |
| 1862 } | |
| 1863 } | |
| 1864 | |
| 1865 /* | |
| 1866 ** When a cached column is reused, make sure that its register is | |
| 1867 ** no longer available as a temp register. ticket #3879: that same | |
| 1868 ** register might be in the cache in multiple places, so be sure to | |
| 1869 ** get them all. | |
| 1870 */ | |
| 1871 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ | |
| 1872 int i; | |
| 1873 struct yColCache *p; | |
| 1874 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1875 if( p->iReg==iReg ){ | |
| 1876 p->tempReg = 0; | |
| 1877 } | |
| 1878 } | |
| 1879 } | |
| 1880 | |
| 1881 /* | |
| 1882 ** Generate code that will extract the iColumn-th column from | |
| 1883 ** table pTab and store the column value in a register. An effort | |
| 1884 ** is made to store the column value in register iReg, but this is | |
| 1885 ** not guaranteed. The location of the column value is returned. | |
| 1886 ** | |
| 1887 ** There must be an open cursor to pTab in iTable when this routine | |
| 1888 ** is called. If iColumn<0 then code is generated that extracts the rowid. | |
| 1889 ** | |
| 1890 ** This routine might attempt to reuse the value of the column that | |
| 1891 ** has already been loaded into a register. The value will always | |
| 1892 ** be used if it has not undergone any affinity changes. But if | |
| 1893 ** an affinity change has occurred, then the cached value will only be | |
| 1894 ** used if allowAffChng is true. | |
| 1895 */ | |
| 1896 int sqlite3ExprCodeGetColumn( | |
| 1897 Parse *pParse, /* Parsing and code generating context */ | |
| 1898 Table *pTab, /* Description of the table we are reading from */ | |
| 1899 int iColumn, /* Index of the table column */ | |
| 1900 int iTable, /* The cursor pointing to the table */ | |
| 1901 int iReg, /* Store results here */ | |
| 1902 int allowAffChng /* True if prior affinity changes are OK */ | |
| 1903 ){ | |
| 1904 Vdbe *v = pParse->pVdbe; | |
| 1905 int i; | |
| 1906 struct yColCache *p; | |
| 1907 | |
| 1908 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1909 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn | |
| 1910 && (!p->affChange || allowAffChng) ){ | |
| 1911 p->lru = pParse->iCacheCnt++; | |
| 1912 sqlite3ExprCachePinRegister(pParse, p->iReg); | |
| 1913 return p->iReg; | |
| 1914 } | |
| 1915 } | |
| 1916 assert( v!=0 ); | |
| 1917 if( iColumn<0 ){ | |
| 1918 sqlite3VdbeAddOp2(v, OP_Rowid, iTable, iReg); | |
| 1919 }else if( ALWAYS(pTab!=0) ){ | |
| 1920 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; | |
| 1921 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg); | |
| 1922 sqlite3ColumnDefault(v, pTab, iColumn, iReg); | |
| 1923 } | |
| 1924 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); | |
| 1925 return iReg; | |
| 1926 } | |
| 1927 | |
| 1928 /* | |
| 1929 ** Clear all column cache entries. | |
| 1930 */ | |
| 1931 void sqlite3ExprCacheClear(Parse *pParse){ | |
| 1932 int i; | |
| 1933 struct yColCache *p; | |
| 1934 | |
| 1935 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1936 if( p->iReg ){ | |
| 1937 cacheEntryClear(pParse, p); | |
| 1938 p->iReg = 0; | |
| 1939 } | |
| 1940 } | |
| 1941 } | |
| 1942 | |
| 1943 /* | |
| 1944 ** Record the fact that an affinity change has occurred on iCount | |
| 1945 ** registers starting with iStart. | |
| 1946 */ | |
| 1947 void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ | |
| 1948 int iEnd = iStart + iCount - 1; | |
| 1949 int i; | |
| 1950 struct yColCache *p; | |
| 1951 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1952 int r = p->iReg; | |
| 1953 if( r>=iStart && r<=iEnd ){ | |
| 1954 p->affChange = 1; | |
| 1955 } | |
| 1956 } | |
| 1957 } | |
| 1958 | |
| 1959 /* | |
| 1960 ** Generate code to move content from registers iFrom...iFrom+nReg-1 | |
| 1961 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. | |
| 1962 */ | |
| 1963 void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ | |
| 1964 int i; | |
| 1965 struct yColCache *p; | |
| 1966 if( NEVER(iFrom==iTo) ) return; | |
| 1967 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); | |
| 1968 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1969 int x = p->iReg; | |
| 1970 if( x>=iFrom && x<iFrom+nReg ){ | |
| 1971 p->iReg += iTo-iFrom; | |
| 1972 } | |
| 1973 } | |
| 1974 } | |
| 1975 | |
| 1976 /* | |
| 1977 ** Generate code to copy content from registers iFrom...iFrom+nReg-1 | |
| 1978 ** over to iTo..iTo+nReg-1. | |
| 1979 */ | |
| 1980 void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){ | |
| 1981 int i; | |
| 1982 if( NEVER(iFrom==iTo) ) return; | |
| 1983 for(i=0; i<nReg; i++){ | |
| 1984 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i); | |
| 1985 } | |
| 1986 } | |
| 1987 | |
| 1988 /* | |
| 1989 ** Return true if any register in the range iFrom..iTo (inclusive) | |
| 1990 ** is used as part of the column cache. | |
| 1991 */ | |
| 1992 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ | |
| 1993 int i; | |
| 1994 struct yColCache *p; | |
| 1995 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 1996 int r = p->iReg; | |
| 1997 if( r>=iFrom && r<=iTo ) return 1; | |
| 1998 } | |
| 1999 return 0; | |
| 2000 } | |
| 2001 | |
| 2002 /* | |
| 2003 ** If the last instruction coded is an ephemeral copy of any of | |
| 2004 ** the registers in the nReg registers beginning with iReg, then | |
| 2005 ** convert the last instruction from OP_SCopy to OP_Copy. | |
| 2006 */ | |
| 2007 void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){ | |
| 2008 VdbeOp *pOp; | |
| 2009 Vdbe *v; | |
| 2010 | |
| 2011 assert( pParse->db->mallocFailed==0 ); | |
| 2012 v = pParse->pVdbe; | |
| 2013 assert( v!=0 ); | |
| 2014 pOp = sqlite3VdbeGetOp(v, -1); | |
| 2015 assert( pOp!=0 ); | |
| 2016 if( pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){ | |
| 2017 pOp->opcode = OP_Copy; | |
| 2018 } | |
| 2019 } | |
| 2020 | |
| 2021 /* | |
| 2022 ** Generate code to store the value of the iAlias-th alias in register | |
| 2023 ** target. The first time this is called, pExpr is evaluated to compute | |
| 2024 ** the value of the alias. The value is stored in an auxiliary register | |
| 2025 ** and the number of that register is returned. On subsequent calls, | |
| 2026 ** the register number is returned without generating any code. | |
| 2027 ** | |
| 2028 ** Note that in order for this to work, code must be generated in the | |
| 2029 ** same order that it is executed. | |
| 2030 ** | |
| 2031 ** Aliases are numbered starting with 1. So iAlias is in the range | |
| 2032 ** of 1 to pParse->nAlias inclusive. | |
| 2033 ** | |
| 2034 ** pParse->aAlias[iAlias-1] records the register number where the value | |
| 2035 ** of the iAlias-th alias is stored. If zero, that means that the | |
| 2036 ** alias has not yet been computed. | |
| 2037 */ | |
| 2038 static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr, int target){ | |
| 2039 #if 0 | |
| 2040 sqlite3 *db = pParse->db; | |
| 2041 int iReg; | |
| 2042 if( pParse->nAliasAlloc<pParse->nAlias ){ | |
| 2043 pParse->aAlias = sqlite3DbReallocOrFree(db, pParse->aAlias, | |
| 2044 sizeof(pParse->aAlias[0])*pParse->nAlias ); | |
| 2045 testcase( db->mallocFailed && pParse->nAliasAlloc>0 ); | |
| 2046 if( db->mallocFailed ) return 0; | |
| 2047 memset(&pParse->aAlias[pParse->nAliasAlloc], 0, | |
| 2048 (pParse->nAlias-pParse->nAliasAlloc)*sizeof(pParse->aAlias[0])); | |
| 2049 pParse->nAliasAlloc = pParse->nAlias; | |
| 2050 } | |
| 2051 assert( iAlias>0 && iAlias<=pParse->nAlias ); | |
| 2052 iReg = pParse->aAlias[iAlias-1]; | |
| 2053 if( iReg==0 ){ | |
| 2054 if( pParse->iCacheLevel>0 ){ | |
| 2055 iReg = sqlite3ExprCodeTarget(pParse, pExpr, target); | |
| 2056 }else{ | |
| 2057 iReg = ++pParse->nMem; | |
| 2058 sqlite3ExprCode(pParse, pExpr, iReg); | |
| 2059 pParse->aAlias[iAlias-1] = iReg; | |
| 2060 } | |
| 2061 } | |
| 2062 return iReg; | |
| 2063 #else | |
| 2064 UNUSED_PARAMETER(iAlias); | |
| 2065 return sqlite3ExprCodeTarget(pParse, pExpr, target); | |
| 2066 #endif | |
| 2067 } | |
| 2068 | |
| 2069 /* | |
| 2070 ** Generate code into the current Vdbe to evaluate the given | |
| 2071 ** expression. Attempt to store the results in register "target". | |
| 2072 ** Return the register where results are stored. | |
| 2073 ** | |
| 2074 ** With this routine, there is no guarantee that results will | |
| 2075 ** be stored in target. The result might be stored in some other | |
| 2076 ** register if it is convenient to do so. The calling function | |
| 2077 ** must check the return code and move the results to the desired | |
| 2078 ** register. | |
| 2079 */ | |
| 2080 int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ | |
| 2081 Vdbe *v = pParse->pVdbe; /* The VM under construction */ | |
| 2082 int op; /* The opcode being coded */ | |
| 2083 int inReg = target; /* Results stored in register inReg */ | |
| 2084 int regFree1 = 0; /* If non-zero free this temporary register */ | |
| 2085 int regFree2 = 0; /* If non-zero free this temporary register */ | |
| 2086 int r1, r2, r3, r4; /* Various register numbers */ | |
| 2087 sqlite3 *db = pParse->db; /* The database connection */ | |
| 2088 | |
| 2089 assert( target>0 && target<=pParse->nMem ); | |
| 2090 if( v==0 ){ | |
| 2091 assert( pParse->db->mallocFailed ); | |
| 2092 return 0; | |
| 2093 } | |
| 2094 | |
| 2095 if( pExpr==0 ){ | |
| 2096 op = TK_NULL; | |
| 2097 }else{ | |
| 2098 op = pExpr->op; | |
| 2099 } | |
| 2100 switch( op ){ | |
| 2101 case TK_AGG_COLUMN: { | |
| 2102 AggInfo *pAggInfo = pExpr->pAggInfo; | |
| 2103 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; | |
| 2104 if( !pAggInfo->directMode ){ | |
| 2105 assert( pCol->iMem>0 ); | |
| 2106 inReg = pCol->iMem; | |
| 2107 break; | |
| 2108 }else if( pAggInfo->useSortingIdx ){ | |
| 2109 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, | |
| 2110 pCol->iSorterColumn, target); | |
| 2111 break; | |
| 2112 } | |
| 2113 /* Otherwise, fall thru into the TK_COLUMN case */ | |
| 2114 } | |
| 2115 case TK_COLUMN: { | |
| 2116 if( pExpr->iTable<0 ){ | |
| 2117 /* This only happens when coding check constraints */ | |
| 2118 assert( pParse->ckBase>0 ); | |
| 2119 inReg = pExpr->iColumn + pParse->ckBase; | |
| 2120 }else{ | |
| 2121 testcase( (pExpr->flags & EP_AnyAff)!=0 ); | |
| 2122 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, | |
| 2123 pExpr->iColumn, pExpr->iTable, target, | |
| 2124 pExpr->flags & EP_AnyAff); | |
| 2125 } | |
| 2126 break; | |
| 2127 } | |
| 2128 case TK_INTEGER: { | |
| 2129 codeInteger(v, pExpr, 0, target); | |
| 2130 break; | |
| 2131 } | |
| 2132 case TK_FLOAT: { | |
| 2133 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2134 codeReal(v, pExpr->u.zToken, 0, target); | |
| 2135 break; | |
| 2136 } | |
| 2137 case TK_STRING: { | |
| 2138 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2139 sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0); | |
| 2140 break; | |
| 2141 } | |
| 2142 case TK_NULL: { | |
| 2143 sqlite3VdbeAddOp2(v, OP_Null, 0, target); | |
| 2144 break; | |
| 2145 } | |
| 2146 #ifndef SQLITE_OMIT_BLOB_LITERAL | |
| 2147 case TK_BLOB: { | |
| 2148 int n; | |
| 2149 const char *z; | |
| 2150 char *zBlob; | |
| 2151 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2152 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); | |
| 2153 assert( pExpr->u.zToken[1]=='\'' ); | |
| 2154 z = &pExpr->u.zToken[2]; | |
| 2155 n = sqlite3Strlen30(z) - 1; | |
| 2156 assert( z[n]=='\'' ); | |
| 2157 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); | |
| 2158 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); | |
| 2159 break; | |
| 2160 } | |
| 2161 #endif | |
| 2162 case TK_VARIABLE: { | |
| 2163 VdbeOp *pOp; | |
| 2164 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2165 assert( pExpr->u.zToken!=0 ); | |
| 2166 assert( pExpr->u.zToken[0]!=0 ); | |
| 2167 if( pExpr->u.zToken[1]==0 | |
| 2168 && (pOp = sqlite3VdbeGetOp(v, -1))->opcode==OP_Variable | |
| 2169 && pOp->p1+pOp->p3==pExpr->iTable | |
| 2170 && pOp->p2+pOp->p3==target | |
| 2171 && pOp->p4.z==0 | |
| 2172 ){ | |
| 2173 /* If the previous instruction was a copy of the previous unnamed | |
| 2174 ** parameter into the previous register, then simply increment the | |
| 2175 ** repeat count on the prior instruction rather than making a new | |
| 2176 ** instruction. | |
| 2177 */ | |
| 2178 pOp->p3++; | |
| 2179 }else{ | |
| 2180 sqlite3VdbeAddOp3(v, OP_Variable, pExpr->iTable, target, 1); | |
| 2181 if( pExpr->u.zToken[1]!=0 ){ | |
| 2182 sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0); | |
| 2183 } | |
| 2184 } | |
| 2185 break; | |
| 2186 } | |
| 2187 case TK_REGISTER: { | |
| 2188 inReg = pExpr->iTable; | |
| 2189 break; | |
| 2190 } | |
| 2191 case TK_AS: { | |
| 2192 inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft, target); | |
| 2193 break; | |
| 2194 } | |
| 2195 #ifndef SQLITE_OMIT_CAST | |
| 2196 case TK_CAST: { | |
| 2197 /* Expressions of the form: CAST(pLeft AS token) */ | |
| 2198 int aff, to_op; | |
| 2199 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); | |
| 2200 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2201 aff = sqlite3AffinityType(pExpr->u.zToken); | |
| 2202 to_op = aff - SQLITE_AFF_TEXT + OP_ToText; | |
| 2203 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); | |
| 2204 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); | |
| 2205 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); | |
| 2206 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); | |
| 2207 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); | |
| 2208 testcase( to_op==OP_ToText ); | |
| 2209 testcase( to_op==OP_ToBlob ); | |
| 2210 testcase( to_op==OP_ToNumeric ); | |
| 2211 testcase( to_op==OP_ToInt ); | |
| 2212 testcase( to_op==OP_ToReal ); | |
| 2213 if( inReg!=target ){ | |
| 2214 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); | |
| 2215 inReg = target; | |
| 2216 } | |
| 2217 sqlite3VdbeAddOp1(v, to_op, inReg); | |
| 2218 testcase( usedAsColumnCache(pParse, inReg, inReg) ); | |
| 2219 sqlite3ExprCacheAffinityChange(pParse, inReg, 1); | |
| 2220 break; | |
| 2221 } | |
| 2222 #endif /* SQLITE_OMIT_CAST */ | |
| 2223 case TK_LT: | |
| 2224 case TK_LE: | |
| 2225 case TK_GT: | |
| 2226 case TK_GE: | |
| 2227 case TK_NE: | |
| 2228 case TK_EQ: { | |
| 2229 assert( TK_LT==OP_Lt ); | |
| 2230 assert( TK_LE==OP_Le ); | |
| 2231 assert( TK_GT==OP_Gt ); | |
| 2232 assert( TK_GE==OP_Ge ); | |
| 2233 assert( TK_EQ==OP_Eq ); | |
| 2234 assert( TK_NE==OP_Ne ); | |
| 2235 testcase( op==TK_LT ); | |
| 2236 testcase( op==TK_LE ); | |
| 2237 testcase( op==TK_GT ); | |
| 2238 testcase( op==TK_GE ); | |
| 2239 testcase( op==TK_EQ ); | |
| 2240 testcase( op==TK_NE ); | |
| 2241 codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, | |
| 2242 pExpr->pRight, &r2, ®Free2); | |
| 2243 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | |
| 2244 r1, r2, inReg, SQLITE_STOREP2); | |
| 2245 testcase( regFree1==0 ); | |
| 2246 testcase( regFree2==0 ); | |
| 2247 break; | |
| 2248 } | |
| 2249 case TK_AND: | |
| 2250 case TK_OR: | |
| 2251 case TK_PLUS: | |
| 2252 case TK_STAR: | |
| 2253 case TK_MINUS: | |
| 2254 case TK_REM: | |
| 2255 case TK_BITAND: | |
| 2256 case TK_BITOR: | |
| 2257 case TK_SLASH: | |
| 2258 case TK_LSHIFT: | |
| 2259 case TK_RSHIFT: | |
| 2260 case TK_CONCAT: { | |
| 2261 assert( TK_AND==OP_And ); | |
| 2262 assert( TK_OR==OP_Or ); | |
| 2263 assert( TK_PLUS==OP_Add ); | |
| 2264 assert( TK_MINUS==OP_Subtract ); | |
| 2265 assert( TK_REM==OP_Remainder ); | |
| 2266 assert( TK_BITAND==OP_BitAnd ); | |
| 2267 assert( TK_BITOR==OP_BitOr ); | |
| 2268 assert( TK_SLASH==OP_Divide ); | |
| 2269 assert( TK_LSHIFT==OP_ShiftLeft ); | |
| 2270 assert( TK_RSHIFT==OP_ShiftRight ); | |
| 2271 assert( TK_CONCAT==OP_Concat ); | |
| 2272 testcase( op==TK_AND ); | |
| 2273 testcase( op==TK_OR ); | |
| 2274 testcase( op==TK_PLUS ); | |
| 2275 testcase( op==TK_MINUS ); | |
| 2276 testcase( op==TK_REM ); | |
| 2277 testcase( op==TK_BITAND ); | |
| 2278 testcase( op==TK_BITOR ); | |
| 2279 testcase( op==TK_SLASH ); | |
| 2280 testcase( op==TK_LSHIFT ); | |
| 2281 testcase( op==TK_RSHIFT ); | |
| 2282 testcase( op==TK_CONCAT ); | |
| 2283 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | |
| 2284 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); | |
| 2285 sqlite3VdbeAddOp3(v, op, r2, r1, target); | |
| 2286 testcase( regFree1==0 ); | |
| 2287 testcase( regFree2==0 ); | |
| 2288 break; | |
| 2289 } | |
| 2290 case TK_UMINUS: { | |
| 2291 Expr *pLeft = pExpr->pLeft; | |
| 2292 assert( pLeft ); | |
| 2293 if( pLeft->op==TK_FLOAT ){ | |
| 2294 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2295 codeReal(v, pLeft->u.zToken, 1, target); | |
| 2296 }else if( pLeft->op==TK_INTEGER ){ | |
| 2297 codeInteger(v, pLeft, 1, target); | |
| 2298 }else{ | |
| 2299 regFree1 = r1 = sqlite3GetTempReg(pParse); | |
| 2300 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); | |
| 2301 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); | |
| 2302 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); | |
| 2303 testcase( regFree2==0 ); | |
| 2304 } | |
| 2305 inReg = target; | |
| 2306 break; | |
| 2307 } | |
| 2308 case TK_BITNOT: | |
| 2309 case TK_NOT: { | |
| 2310 assert( TK_BITNOT==OP_BitNot ); | |
| 2311 assert( TK_NOT==OP_Not ); | |
| 2312 testcase( op==TK_BITNOT ); | |
| 2313 testcase( op==TK_NOT ); | |
| 2314 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | |
| 2315 testcase( regFree1==0 ); | |
| 2316 inReg = target; | |
| 2317 sqlite3VdbeAddOp2(v, op, r1, inReg); | |
| 2318 break; | |
| 2319 } | |
| 2320 case TK_ISNULL: | |
| 2321 case TK_NOTNULL: { | |
| 2322 int addr; | |
| 2323 assert( TK_ISNULL==OP_IsNull ); | |
| 2324 assert( TK_NOTNULL==OP_NotNull ); | |
| 2325 testcase( op==TK_ISNULL ); | |
| 2326 testcase( op==TK_NOTNULL ); | |
| 2327 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); | |
| 2328 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | |
| 2329 testcase( regFree1==0 ); | |
| 2330 addr = sqlite3VdbeAddOp1(v, op, r1); | |
| 2331 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); | |
| 2332 sqlite3VdbeJumpHere(v, addr); | |
| 2333 break; | |
| 2334 } | |
| 2335 case TK_AGG_FUNCTION: { | |
| 2336 AggInfo *pInfo = pExpr->pAggInfo; | |
| 2337 if( pInfo==0 ){ | |
| 2338 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2339 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); | |
| 2340 }else{ | |
| 2341 inReg = pInfo->aFunc[pExpr->iAgg].iMem; | |
| 2342 } | |
| 2343 break; | |
| 2344 } | |
| 2345 case TK_CONST_FUNC: | |
| 2346 case TK_FUNCTION: { | |
| 2347 ExprList *pFarg; /* List of function arguments */ | |
| 2348 int nFarg; /* Number of function arguments */ | |
| 2349 FuncDef *pDef; /* The function definition object */ | |
| 2350 int nId; /* Length of the function name in bytes */ | |
| 2351 const char *zId; /* The function name */ | |
| 2352 int constMask = 0; /* Mask of function arguments that are constant */ | |
| 2353 int i; /* Loop counter */ | |
| 2354 u8 enc = ENC(db); /* The text encoding used by this database */ | |
| 2355 CollSeq *pColl = 0; /* A collating sequence */ | |
| 2356 | |
| 2357 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | |
| 2358 testcase( op==TK_CONST_FUNC ); | |
| 2359 testcase( op==TK_FUNCTION ); | |
| 2360 if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){ | |
| 2361 pFarg = 0; | |
| 2362 }else{ | |
| 2363 pFarg = pExpr->x.pList; | |
| 2364 } | |
| 2365 nFarg = pFarg ? pFarg->nExpr : 0; | |
| 2366 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2367 zId = pExpr->u.zToken; | |
| 2368 nId = sqlite3Strlen30(zId); | |
| 2369 pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0); | |
| 2370 if( pDef==0 ){ | |
| 2371 sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId); | |
| 2372 break; | |
| 2373 } | |
| 2374 if( pFarg ){ | |
| 2375 r1 = sqlite3GetTempRange(pParse, nFarg); | |
| 2376 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ | |
| 2377 sqlite3ExprCodeExprList(pParse, pFarg, r1, 1); | |
| 2378 sqlite3ExprCachePop(pParse, 1); /* Ticket 2ea2425d34be */ | |
| 2379 }else{ | |
| 2380 r1 = 0; | |
| 2381 } | |
| 2382 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 2383 /* Possibly overload the function if the first argument is | |
| 2384 ** a virtual table column. | |
| 2385 ** | |
| 2386 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the | |
| 2387 ** second argument, not the first, as the argument to test to | |
| 2388 ** see if it is a column in a virtual table. This is done because | |
| 2389 ** the left operand of infix functions (the operand we want to | |
| 2390 ** control overloading) ends up as the second argument to the | |
| 2391 ** function. The expression "A glob B" is equivalent to | |
| 2392 ** "glob(B,A). We want to use the A in "A glob B" to test | |
| 2393 ** for function overloading. But we use the B term in "glob(B,A)". | |
| 2394 */ | |
| 2395 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ | |
| 2396 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); | |
| 2397 }else if( nFarg>0 ){ | |
| 2398 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); | |
| 2399 } | |
| 2400 #endif | |
| 2401 for(i=0; i<nFarg; i++){ | |
| 2402 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ | |
| 2403 constMask |= (1<<i); | |
| 2404 } | |
| 2405 if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ | |
| 2406 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); | |
| 2407 } | |
| 2408 } | |
| 2409 if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){ | |
| 2410 if( !pColl ) pColl = db->pDfltColl; | |
| 2411 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); | |
| 2412 } | |
| 2413 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, | |
| 2414 (char*)pDef, P4_FUNCDEF); | |
| 2415 sqlite3VdbeChangeP5(v, (u8)nFarg); | |
| 2416 if( nFarg ){ | |
| 2417 sqlite3ReleaseTempRange(pParse, r1, nFarg); | |
| 2418 } | |
| 2419 sqlite3ExprCacheAffinityChange(pParse, r1, nFarg); | |
| 2420 break; | |
| 2421 } | |
| 2422 #ifndef SQLITE_OMIT_SUBQUERY | |
| 2423 case TK_EXISTS: | |
| 2424 case TK_SELECT: { | |
| 2425 testcase( op==TK_EXISTS ); | |
| 2426 testcase( op==TK_SELECT ); | |
| 2427 sqlite3CodeSubselect(pParse, pExpr, 0, 0); | |
| 2428 inReg = pExpr->iColumn; | |
| 2429 break; | |
| 2430 } | |
| 2431 case TK_IN: { | |
| 2432 int rNotFound = 0; | |
| 2433 int rMayHaveNull = 0; | |
| 2434 int j2, j3, j4, j5; | |
| 2435 char affinity; | |
| 2436 int eType; | |
| 2437 | |
| 2438 VdbeNoopComment((v, "begin IN expr r%d", target)); | |
| 2439 eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull); | |
| 2440 if( rMayHaveNull ){ | |
| 2441 rNotFound = ++pParse->nMem; | |
| 2442 } | |
| 2443 | |
| 2444 /* Figure out the affinity to use to create a key from the results | |
| 2445 ** of the expression. affinityStr stores a static string suitable for | |
| 2446 ** P4 of OP_MakeRecord. | |
| 2447 */ | |
| 2448 affinity = comparisonAffinity(pExpr); | |
| 2449 | |
| 2450 | |
| 2451 /* Code the <expr> from "<expr> IN (...)". The temporary table | |
| 2452 ** pExpr->iTable contains the values that make up the (...) set. | |
| 2453 */ | |
| 2454 sqlite3ExprCachePush(pParse); | |
| 2455 sqlite3ExprCode(pParse, pExpr->pLeft, target); | |
| 2456 j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target); | |
| 2457 if( eType==IN_INDEX_ROWID ){ | |
| 2458 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target); | |
| 2459 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target); | |
| 2460 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); | |
| 2461 j5 = sqlite3VdbeAddOp0(v, OP_Goto); | |
| 2462 sqlite3VdbeJumpHere(v, j3); | |
| 2463 sqlite3VdbeJumpHere(v, j4); | |
| 2464 sqlite3VdbeAddOp2(v, OP_Integer, 0, target); | |
| 2465 }else{ | |
| 2466 r2 = regFree2 = sqlite3GetTempReg(pParse); | |
| 2467 | |
| 2468 /* Create a record and test for set membership. If the set contains | |
| 2469 ** the value, then jump to the end of the test code. The target | |
| 2470 ** register still contains the true (1) value written to it earlier. | |
| 2471 */ | |
| 2472 sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1); | |
| 2473 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); | |
| 2474 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2); | |
| 2475 | |
| 2476 /* If the set membership test fails, then the result of the | |
| 2477 ** "x IN (...)" expression must be either 0 or NULL. If the set | |
| 2478 ** contains no NULL values, then the result is 0. If the set | |
| 2479 ** contains one or more NULL values, then the result of the | |
| 2480 ** expression is also NULL. | |
| 2481 */ | |
| 2482 if( rNotFound==0 ){ | |
| 2483 /* This branch runs if it is known at compile time (now) that | |
| 2484 ** the set contains no NULL values. This happens as the result | |
| 2485 ** of a "NOT NULL" constraint in the database schema. No need | |
| 2486 ** to test the data structure at runtime in this case. | |
| 2487 */ | |
| 2488 sqlite3VdbeAddOp2(v, OP_Integer, 0, target); | |
| 2489 }else{ | |
| 2490 /* This block populates the rNotFound register with either NULL | |
| 2491 ** or 0 (an integer value). If the data structure contains one | |
| 2492 ** or more NULLs, then set rNotFound to NULL. Otherwise, set it | |
| 2493 ** to 0. If register rMayHaveNull is already set to some value | |
| 2494 ** other than NULL, then the test has already been run and | |
| 2495 ** rNotFound is already populated. | |
| 2496 */ | |
| 2497 static const char nullRecord[] = { 0x02, 0x00 }; | |
| 2498 j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull); | |
| 2499 sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound); | |
| 2500 sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0, | |
| 2501 nullRecord, P4_STATIC); | |
| 2502 j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull); | |
| 2503 sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound); | |
| 2504 sqlite3VdbeJumpHere(v, j4); | |
| 2505 sqlite3VdbeJumpHere(v, j3); | |
| 2506 | |
| 2507 /* Copy the value of register rNotFound (which is either NULL or 0) | |
| 2508 ** into the target register. This will be the result of the | |
| 2509 ** expression. | |
| 2510 */ | |
| 2511 sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target); | |
| 2512 } | |
| 2513 } | |
| 2514 sqlite3VdbeJumpHere(v, j2); | |
| 2515 sqlite3VdbeJumpHere(v, j5); | |
| 2516 sqlite3ExprCachePop(pParse, 1); | |
| 2517 VdbeComment((v, "end IN expr r%d", target)); | |
| 2518 break; | |
| 2519 } | |
| 2520 #endif | |
| 2521 /* | |
| 2522 ** x BETWEEN y AND z | |
| 2523 ** | |
| 2524 ** This is equivalent to | |
| 2525 ** | |
| 2526 ** x>=y AND x<=z | |
| 2527 ** | |
| 2528 ** X is stored in pExpr->pLeft. | |
| 2529 ** Y is stored in pExpr->pList->a[0].pExpr. | |
| 2530 ** Z is stored in pExpr->pList->a[1].pExpr. | |
| 2531 */ | |
| 2532 case TK_BETWEEN: { | |
| 2533 Expr *pLeft = pExpr->pLeft; | |
| 2534 struct ExprList_item *pLItem = pExpr->x.pList->a; | |
| 2535 Expr *pRight = pLItem->pExpr; | |
| 2536 | |
| 2537 codeCompareOperands(pParse, pLeft, &r1, ®Free1, | |
| 2538 pRight, &r2, ®Free2); | |
| 2539 testcase( regFree1==0 ); | |
| 2540 testcase( regFree2==0 ); | |
| 2541 r3 = sqlite3GetTempReg(pParse); | |
| 2542 r4 = sqlite3GetTempReg(pParse); | |
| 2543 codeCompare(pParse, pLeft, pRight, OP_Ge, | |
| 2544 r1, r2, r3, SQLITE_STOREP2); | |
| 2545 pLItem++; | |
| 2546 pRight = pLItem->pExpr; | |
| 2547 sqlite3ReleaseTempReg(pParse, regFree2); | |
| 2548 r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); | |
| 2549 testcase( regFree2==0 ); | |
| 2550 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); | |
| 2551 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); | |
| 2552 sqlite3ReleaseTempReg(pParse, r3); | |
| 2553 sqlite3ReleaseTempReg(pParse, r4); | |
| 2554 break; | |
| 2555 } | |
| 2556 case TK_UPLUS: { | |
| 2557 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); | |
| 2558 break; | |
| 2559 } | |
| 2560 | |
| 2561 case TK_TRIGGER: { | |
| 2562 /* If the opcode is TK_TRIGGER, then the expression is a reference | |
| 2563 ** to a column in the new.* or old.* pseudo-tables available to | |
| 2564 ** trigger programs. In this case Expr.iTable is set to 1 for the | |
| 2565 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn | |
| 2566 ** is set to the column of the pseudo-table to read, or to -1 to | |
| 2567 ** read the rowid field. | |
| 2568 ** | |
| 2569 ** The expression is implemented using an OP_Param opcode. The p1 | |
| 2570 ** parameter is set to 0 for an old.rowid reference, or to (i+1) | |
| 2571 ** to reference another column of the old.* pseudo-table, where | |
| 2572 ** i is the index of the column. For a new.rowid reference, p1 is | |
| 2573 ** set to (n+1), where n is the number of columns in each pseudo-table. | |
| 2574 ** For a reference to any other column in the new.* pseudo-table, p1 | |
| 2575 ** is set to (n+2+i), where n and i are as defined previously. For | |
| 2576 ** example, if the table on which triggers are being fired is | |
| 2577 ** declared as: | |
| 2578 ** | |
| 2579 ** CREATE TABLE t1(a, b); | |
| 2580 ** | |
| 2581 ** Then p1 is interpreted as follows: | |
| 2582 ** | |
| 2583 ** p1==0 -> old.rowid p1==3 -> new.rowid | |
| 2584 ** p1==1 -> old.a p1==4 -> new.a | |
| 2585 ** p1==2 -> old.b p1==5 -> new.b | |
| 2586 */ | |
| 2587 Table *pTab = pExpr->pTab; | |
| 2588 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; | |
| 2589 | |
| 2590 assert( pExpr->iTable==0 || pExpr->iTable==1 ); | |
| 2591 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); | |
| 2592 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); | |
| 2593 assert( p1>=0 && p1<(pTab->nCol*2+2) ); | |
| 2594 | |
| 2595 sqlite3VdbeAddOp2(v, OP_Param, p1, target); | |
| 2596 VdbeComment((v, "%s.%s -> $%d", | |
| 2597 (pExpr->iTable ? "new" : "old"), | |
| 2598 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), | |
| 2599 target | |
| 2600 )); | |
| 2601 | |
| 2602 /* If the column has REAL affinity, it may currently be stored as an | |
| 2603 ** integer. Use OP_RealAffinity to make sure it is really real. */ | |
| 2604 if( pExpr->iColumn>=0 | |
| 2605 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL | |
| 2606 ){ | |
| 2607 sqlite3VdbeAddOp1(v, OP_RealAffinity, target); | |
| 2608 } | |
| 2609 break; | |
| 2610 } | |
| 2611 | |
| 2612 | |
| 2613 /* | |
| 2614 ** Form A: | |
| 2615 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END | |
| 2616 ** | |
| 2617 ** Form B: | |
| 2618 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END | |
| 2619 ** | |
| 2620 ** Form A is can be transformed into the equivalent form B as follows: | |
| 2621 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... | |
| 2622 ** WHEN x=eN THEN rN ELSE y END | |
| 2623 ** | |
| 2624 ** X (if it exists) is in pExpr->pLeft. | |
| 2625 ** Y is in pExpr->pRight. The Y is also optional. If there is no | |
| 2626 ** ELSE clause and no other term matches, then the result of the | |
| 2627 ** exprssion is NULL. | |
| 2628 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. | |
| 2629 ** | |
| 2630 ** The result of the expression is the Ri for the first matching Ei, | |
| 2631 ** or if there is no matching Ei, the ELSE term Y, or if there is | |
| 2632 ** no ELSE term, NULL. | |
| 2633 */ | |
| 2634 default: assert( op==TK_CASE ); { | |
| 2635 int endLabel; /* GOTO label for end of CASE stmt */ | |
| 2636 int nextCase; /* GOTO label for next WHEN clause */ | |
| 2637 int nExpr; /* 2x number of WHEN terms */ | |
| 2638 int i; /* Loop counter */ | |
| 2639 ExprList *pEList; /* List of WHEN terms */ | |
| 2640 struct ExprList_item *aListelem; /* Array of WHEN terms */ | |
| 2641 Expr opCompare; /* The X==Ei expression */ | |
| 2642 Expr cacheX; /* Cached expression X */ | |
| 2643 Expr *pX; /* The X expression */ | |
| 2644 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ | |
| 2645 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) | |
| 2646 | |
| 2647 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); | |
| 2648 assert((pExpr->x.pList->nExpr % 2) == 0); | |
| 2649 assert(pExpr->x.pList->nExpr > 0); | |
| 2650 pEList = pExpr->x.pList; | |
| 2651 aListelem = pEList->a; | |
| 2652 nExpr = pEList->nExpr; | |
| 2653 endLabel = sqlite3VdbeMakeLabel(v); | |
| 2654 if( (pX = pExpr->pLeft)!=0 ){ | |
| 2655 cacheX = *pX; | |
| 2656 testcase( pX->op==TK_COLUMN ); | |
| 2657 testcase( pX->op==TK_REGISTER ); | |
| 2658 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); | |
| 2659 testcase( regFree1==0 ); | |
| 2660 cacheX.op = TK_REGISTER; | |
| 2661 opCompare.op = TK_EQ; | |
| 2662 opCompare.pLeft = &cacheX; | |
| 2663 pTest = &opCompare; | |
| 2664 } | |
| 2665 for(i=0; i<nExpr; i=i+2){ | |
| 2666 sqlite3ExprCachePush(pParse); | |
| 2667 if( pX ){ | |
| 2668 assert( pTest!=0 ); | |
| 2669 opCompare.pRight = aListelem[i].pExpr; | |
| 2670 }else{ | |
| 2671 pTest = aListelem[i].pExpr; | |
| 2672 } | |
| 2673 nextCase = sqlite3VdbeMakeLabel(v); | |
| 2674 testcase( pTest->op==TK_COLUMN ); | |
| 2675 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); | |
| 2676 testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); | |
| 2677 testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); | |
| 2678 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); | |
| 2679 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); | |
| 2680 sqlite3ExprCachePop(pParse, 1); | |
| 2681 sqlite3VdbeResolveLabel(v, nextCase); | |
| 2682 } | |
| 2683 if( pExpr->pRight ){ | |
| 2684 sqlite3ExprCachePush(pParse); | |
| 2685 sqlite3ExprCode(pParse, pExpr->pRight, target); | |
| 2686 sqlite3ExprCachePop(pParse, 1); | |
| 2687 }else{ | |
| 2688 sqlite3VdbeAddOp2(v, OP_Null, 0, target); | |
| 2689 } | |
| 2690 assert( db->mallocFailed || pParse->nErr>0 | |
| 2691 || pParse->iCacheLevel==iCacheLevel ); | |
| 2692 sqlite3VdbeResolveLabel(v, endLabel); | |
| 2693 break; | |
| 2694 } | |
| 2695 #ifndef SQLITE_OMIT_TRIGGER | |
| 2696 case TK_RAISE: { | |
| 2697 assert( pExpr->affinity==OE_Rollback | |
| 2698 || pExpr->affinity==OE_Abort | |
| 2699 || pExpr->affinity==OE_Fail | |
| 2700 || pExpr->affinity==OE_Ignore | |
| 2701 ); | |
| 2702 if( !pParse->pTriggerTab ){ | |
| 2703 sqlite3ErrorMsg(pParse, | |
| 2704 "RAISE() may only be used within a trigger-program"); | |
| 2705 return 0; | |
| 2706 } | |
| 2707 if( pExpr->affinity==OE_Abort ){ | |
| 2708 sqlite3MayAbort(pParse); | |
| 2709 } | |
| 2710 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2711 if( pExpr->affinity==OE_Ignore ){ | |
| 2712 sqlite3VdbeAddOp4( | |
| 2713 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); | |
| 2714 }else{ | |
| 2715 sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0); | |
| 2716 } | |
| 2717 | |
| 2718 break; | |
| 2719 } | |
| 2720 #endif | |
| 2721 } | |
| 2722 sqlite3ReleaseTempReg(pParse, regFree1); | |
| 2723 sqlite3ReleaseTempReg(pParse, regFree2); | |
| 2724 return inReg; | |
| 2725 } | |
| 2726 | |
| 2727 /* | |
| 2728 ** Generate code to evaluate an expression and store the results | |
| 2729 ** into a register. Return the register number where the results | |
| 2730 ** are stored. | |
| 2731 ** | |
| 2732 ** If the register is a temporary register that can be deallocated, | |
| 2733 ** then write its number into *pReg. If the result register is not | |
| 2734 ** a temporary, then set *pReg to zero. | |
| 2735 */ | |
| 2736 int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ | |
| 2737 int r1 = sqlite3GetTempReg(pParse); | |
| 2738 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); | |
| 2739 if( r2==r1 ){ | |
| 2740 *pReg = r1; | |
| 2741 }else{ | |
| 2742 sqlite3ReleaseTempReg(pParse, r1); | |
| 2743 *pReg = 0; | |
| 2744 } | |
| 2745 return r2; | |
| 2746 } | |
| 2747 | |
| 2748 /* | |
| 2749 ** Generate code that will evaluate expression pExpr and store the | |
| 2750 ** results in register target. The results are guaranteed to appear | |
| 2751 ** in register target. | |
| 2752 */ | |
| 2753 int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ | |
| 2754 int inReg; | |
| 2755 | |
| 2756 assert( target>0 && target<=pParse->nMem ); | |
| 2757 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); | |
| 2758 assert( pParse->pVdbe || pParse->db->mallocFailed ); | |
| 2759 if( inReg!=target && pParse->pVdbe ){ | |
| 2760 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); | |
| 2761 } | |
| 2762 return target; | |
| 2763 } | |
| 2764 | |
| 2765 /* | |
| 2766 ** Generate code that evalutes the given expression and puts the result | |
| 2767 ** in register target. | |
| 2768 ** | |
| 2769 ** Also make a copy of the expression results into another "cache" register | |
| 2770 ** and modify the expression so that the next time it is evaluated, | |
| 2771 ** the result is a copy of the cache register. | |
| 2772 ** | |
| 2773 ** This routine is used for expressions that are used multiple | |
| 2774 ** times. They are evaluated once and the results of the expression | |
| 2775 ** are reused. | |
| 2776 */ | |
| 2777 int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ | |
| 2778 Vdbe *v = pParse->pVdbe; | |
| 2779 int inReg; | |
| 2780 inReg = sqlite3ExprCode(pParse, pExpr, target); | |
| 2781 assert( target>0 ); | |
| 2782 /* This routine is called for terms to INSERT or UPDATE. And the only | |
| 2783 ** other place where expressions can be converted into TK_REGISTER is | |
| 2784 ** in WHERE clause processing. So as currently implemented, there is | |
| 2785 ** no way for a TK_REGISTER to exist here. But it seems prudent to | |
| 2786 ** keep the ALWAYS() in case the conditions above change with future | |
| 2787 ** modifications or enhancements. */ | |
| 2788 if( ALWAYS(pExpr->op!=TK_REGISTER) ){ | |
| 2789 int iMem; | |
| 2790 iMem = ++pParse->nMem; | |
| 2791 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); | |
| 2792 pExpr->iTable = iMem; | |
| 2793 pExpr->op = TK_REGISTER; | |
| 2794 } | |
| 2795 return inReg; | |
| 2796 } | |
| 2797 | |
| 2798 /* | |
| 2799 ** Return TRUE if pExpr is an constant expression that is appropriate | |
| 2800 ** for factoring out of a loop. Appropriate expressions are: | |
| 2801 ** | |
| 2802 ** * Any expression that evaluates to two or more opcodes. | |
| 2803 ** | |
| 2804 ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, | |
| 2805 ** or OP_Variable that does not need to be placed in a | |
| 2806 ** specific register. | |
| 2807 ** | |
| 2808 ** There is no point in factoring out single-instruction constant | |
| 2809 ** expressions that need to be placed in a particular register. | |
| 2810 ** We could factor them out, but then we would end up adding an | |
| 2811 ** OP_SCopy instruction to move the value into the correct register | |
| 2812 ** later. We might as well just use the original instruction and | |
| 2813 ** avoid the OP_SCopy. | |
| 2814 */ | |
| 2815 static int isAppropriateForFactoring(Expr *p){ | |
| 2816 if( !sqlite3ExprIsConstantNotJoin(p) ){ | |
| 2817 return 0; /* Only constant expressions are appropriate for factoring */ | |
| 2818 } | |
| 2819 if( (p->flags & EP_FixedDest)==0 ){ | |
| 2820 return 1; /* Any constant without a fixed destination is appropriate */ | |
| 2821 } | |
| 2822 while( p->op==TK_UPLUS ) p = p->pLeft; | |
| 2823 switch( p->op ){ | |
| 2824 #ifndef SQLITE_OMIT_BLOB_LITERAL | |
| 2825 case TK_BLOB: | |
| 2826 #endif | |
| 2827 case TK_VARIABLE: | |
| 2828 case TK_INTEGER: | |
| 2829 case TK_FLOAT: | |
| 2830 case TK_NULL: | |
| 2831 case TK_STRING: { | |
| 2832 testcase( p->op==TK_BLOB ); | |
| 2833 testcase( p->op==TK_VARIABLE ); | |
| 2834 testcase( p->op==TK_INTEGER ); | |
| 2835 testcase( p->op==TK_FLOAT ); | |
| 2836 testcase( p->op==TK_NULL ); | |
| 2837 testcase( p->op==TK_STRING ); | |
| 2838 /* Single-instruction constants with a fixed destination are | |
| 2839 ** better done in-line. If we factor them, they will just end | |
| 2840 ** up generating an OP_SCopy to move the value to the destination | |
| 2841 ** register. */ | |
| 2842 return 0; | |
| 2843 } | |
| 2844 case TK_UMINUS: { | |
| 2845 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ | |
| 2846 return 0; | |
| 2847 } | |
| 2848 break; | |
| 2849 } | |
| 2850 default: { | |
| 2851 break; | |
| 2852 } | |
| 2853 } | |
| 2854 return 1; | |
| 2855 } | |
| 2856 | |
| 2857 /* | |
| 2858 ** If pExpr is a constant expression that is appropriate for | |
| 2859 ** factoring out of a loop, then evaluate the expression | |
| 2860 ** into a register and convert the expression into a TK_REGISTER | |
| 2861 ** expression. | |
| 2862 */ | |
| 2863 static int evalConstExpr(Walker *pWalker, Expr *pExpr){ | |
| 2864 Parse *pParse = pWalker->pParse; | |
| 2865 switch( pExpr->op ){ | |
| 2866 case TK_REGISTER: { | |
| 2867 return WRC_Prune; | |
| 2868 } | |
| 2869 case TK_FUNCTION: | |
| 2870 case TK_AGG_FUNCTION: | |
| 2871 case TK_CONST_FUNC: { | |
| 2872 /* The arguments to a function have a fixed destination. | |
| 2873 ** Mark them this way to avoid generated unneeded OP_SCopy | |
| 2874 ** instructions. | |
| 2875 */ | |
| 2876 ExprList *pList = pExpr->x.pList; | |
| 2877 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | |
| 2878 if( pList ){ | |
| 2879 int i = pList->nExpr; | |
| 2880 struct ExprList_item *pItem = pList->a; | |
| 2881 for(; i>0; i--, pItem++){ | |
| 2882 if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest; | |
| 2883 } | |
| 2884 } | |
| 2885 break; | |
| 2886 } | |
| 2887 } | |
| 2888 if( isAppropriateForFactoring(pExpr) ){ | |
| 2889 int r1 = ++pParse->nMem; | |
| 2890 int r2; | |
| 2891 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); | |
| 2892 if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1); | |
| 2893 pExpr->op2 = pExpr->op; | |
| 2894 pExpr->op = TK_REGISTER; | |
| 2895 pExpr->iTable = r2; | |
| 2896 return WRC_Prune; | |
| 2897 } | |
| 2898 return WRC_Continue; | |
| 2899 } | |
| 2900 | |
| 2901 /* | |
| 2902 ** Preevaluate constant subexpressions within pExpr and store the | |
| 2903 ** results in registers. Modify pExpr so that the constant subexpresions | |
| 2904 ** are TK_REGISTER opcodes that refer to the precomputed values. | |
| 2905 */ | |
| 2906 void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ | |
| 2907 Walker w; | |
| 2908 w.xExprCallback = evalConstExpr; | |
| 2909 w.xSelectCallback = 0; | |
| 2910 w.pParse = pParse; | |
| 2911 sqlite3WalkExpr(&w, pExpr); | |
| 2912 } | |
| 2913 | |
| 2914 | |
| 2915 /* | |
| 2916 ** Generate code that pushes the value of every element of the given | |
| 2917 ** expression list into a sequence of registers beginning at target. | |
| 2918 ** | |
| 2919 ** Return the number of elements evaluated. | |
| 2920 */ | |
| 2921 int sqlite3ExprCodeExprList( | |
| 2922 Parse *pParse, /* Parsing context */ | |
| 2923 ExprList *pList, /* The expression list to be coded */ | |
| 2924 int target, /* Where to write results */ | |
| 2925 int doHardCopy /* Make a hard copy of every element */ | |
| 2926 ){ | |
| 2927 struct ExprList_item *pItem; | |
| 2928 int i, n; | |
| 2929 assert( pList!=0 ); | |
| 2930 assert( target>0 ); | |
| 2931 n = pList->nExpr; | |
| 2932 for(pItem=pList->a, i=0; i<n; i++, pItem++){ | |
| 2933 if( pItem->iAlias ){ | |
| 2934 int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr, target+i); | |
| 2935 Vdbe *v = sqlite3GetVdbe(pParse); | |
| 2936 if( iReg!=target+i ){ | |
| 2937 sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i); | |
| 2938 } | |
| 2939 }else{ | |
| 2940 sqlite3ExprCode(pParse, pItem->pExpr, target+i); | |
| 2941 } | |
| 2942 if( doHardCopy && !pParse->db->mallocFailed ){ | |
| 2943 sqlite3ExprHardCopy(pParse, target, n); | |
| 2944 } | |
| 2945 } | |
| 2946 return n; | |
| 2947 } | |
| 2948 | |
| 2949 /* | |
| 2950 ** Generate code for a boolean expression such that a jump is made | |
| 2951 ** to the label "dest" if the expression is true but execution | |
| 2952 ** continues straight thru if the expression is false. | |
| 2953 ** | |
| 2954 ** If the expression evaluates to NULL (neither true nor false), then | |
| 2955 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. | |
| 2956 ** | |
| 2957 ** This code depends on the fact that certain token values (ex: TK_EQ) | |
| 2958 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding | |
| 2959 ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in | |
| 2960 ** the make process cause these values to align. Assert()s in the code | |
| 2961 ** below verify that the numbers are aligned correctly. | |
| 2962 */ | |
| 2963 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ | |
| 2964 Vdbe *v = pParse->pVdbe; | |
| 2965 int op = 0; | |
| 2966 int regFree1 = 0; | |
| 2967 int regFree2 = 0; | |
| 2968 int r1, r2; | |
| 2969 | |
| 2970 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); | |
| 2971 if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */ | |
| 2972 if( NEVER(pExpr==0) ) return; /* No way this can happen */ | |
| 2973 op = pExpr->op; | |
| 2974 switch( op ){ | |
| 2975 case TK_AND: { | |
| 2976 int d2 = sqlite3VdbeMakeLabel(v); | |
| 2977 testcase( jumpIfNull==0 ); | |
| 2978 sqlite3ExprCachePush(pParse); | |
| 2979 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); | |
| 2980 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); | |
| 2981 sqlite3VdbeResolveLabel(v, d2); | |
| 2982 sqlite3ExprCachePop(pParse, 1); | |
| 2983 break; | |
| 2984 } | |
| 2985 case TK_OR: { | |
| 2986 testcase( jumpIfNull==0 ); | |
| 2987 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); | |
| 2988 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); | |
| 2989 break; | |
| 2990 } | |
| 2991 case TK_NOT: { | |
| 2992 testcase( jumpIfNull==0 ); | |
| 2993 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); | |
| 2994 break; | |
| 2995 } | |
| 2996 case TK_LT: | |
| 2997 case TK_LE: | |
| 2998 case TK_GT: | |
| 2999 case TK_GE: | |
| 3000 case TK_NE: | |
| 3001 case TK_EQ: { | |
| 3002 assert( TK_LT==OP_Lt ); | |
| 3003 assert( TK_LE==OP_Le ); | |
| 3004 assert( TK_GT==OP_Gt ); | |
| 3005 assert( TK_GE==OP_Ge ); | |
| 3006 assert( TK_EQ==OP_Eq ); | |
| 3007 assert( TK_NE==OP_Ne ); | |
| 3008 testcase( op==TK_LT ); | |
| 3009 testcase( op==TK_LE ); | |
| 3010 testcase( op==TK_GT ); | |
| 3011 testcase( op==TK_GE ); | |
| 3012 testcase( op==TK_EQ ); | |
| 3013 testcase( op==TK_NE ); | |
| 3014 testcase( jumpIfNull==0 ); | |
| 3015 codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, | |
| 3016 pExpr->pRight, &r2, ®Free2); | |
| 3017 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | |
| 3018 r1, r2, dest, jumpIfNull); | |
| 3019 testcase( regFree1==0 ); | |
| 3020 testcase( regFree2==0 ); | |
| 3021 break; | |
| 3022 } | |
| 3023 case TK_ISNULL: | |
| 3024 case TK_NOTNULL: { | |
| 3025 assert( TK_ISNULL==OP_IsNull ); | |
| 3026 assert( TK_NOTNULL==OP_NotNull ); | |
| 3027 testcase( op==TK_ISNULL ); | |
| 3028 testcase( op==TK_NOTNULL ); | |
| 3029 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | |
| 3030 sqlite3VdbeAddOp2(v, op, r1, dest); | |
| 3031 testcase( regFree1==0 ); | |
| 3032 break; | |
| 3033 } | |
| 3034 case TK_BETWEEN: { | |
| 3035 /* x BETWEEN y AND z | |
| 3036 ** | |
| 3037 ** Is equivalent to | |
| 3038 ** | |
| 3039 ** x>=y AND x<=z | |
| 3040 ** | |
| 3041 ** Code it as such, taking care to do the common subexpression | |
| 3042 ** elementation of x. | |
| 3043 */ | |
| 3044 Expr exprAnd; | |
| 3045 Expr compLeft; | |
| 3046 Expr compRight; | |
| 3047 Expr exprX; | |
| 3048 | |
| 3049 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | |
| 3050 exprX = *pExpr->pLeft; | |
| 3051 exprAnd.op = TK_AND; | |
| 3052 exprAnd.pLeft = &compLeft; | |
| 3053 exprAnd.pRight = &compRight; | |
| 3054 compLeft.op = TK_GE; | |
| 3055 compLeft.pLeft = &exprX; | |
| 3056 compLeft.pRight = pExpr->x.pList->a[0].pExpr; | |
| 3057 compRight.op = TK_LE; | |
| 3058 compRight.pLeft = &exprX; | |
| 3059 compRight.pRight = pExpr->x.pList->a[1].pExpr; | |
| 3060 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); | |
| 3061 testcase( regFree1==0 ); | |
| 3062 exprX.op = TK_REGISTER; | |
| 3063 testcase( jumpIfNull==0 ); | |
| 3064 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); | |
| 3065 break; | |
| 3066 } | |
| 3067 default: { | |
| 3068 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); | |
| 3069 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); | |
| 3070 testcase( regFree1==0 ); | |
| 3071 testcase( jumpIfNull==0 ); | |
| 3072 break; | |
| 3073 } | |
| 3074 } | |
| 3075 sqlite3ReleaseTempReg(pParse, regFree1); | |
| 3076 sqlite3ReleaseTempReg(pParse, regFree2); | |
| 3077 } | |
| 3078 | |
| 3079 /* | |
| 3080 ** Generate code for a boolean expression such that a jump is made | |
| 3081 ** to the label "dest" if the expression is false but execution | |
| 3082 ** continues straight thru if the expression is true. | |
| 3083 ** | |
| 3084 ** If the expression evaluates to NULL (neither true nor false) then | |
| 3085 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull | |
| 3086 ** is 0. | |
| 3087 */ | |
| 3088 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ | |
| 3089 Vdbe *v = pParse->pVdbe; | |
| 3090 int op = 0; | |
| 3091 int regFree1 = 0; | |
| 3092 int regFree2 = 0; | |
| 3093 int r1, r2; | |
| 3094 | |
| 3095 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); | |
| 3096 if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */ | |
| 3097 if( pExpr==0 ) return; | |
| 3098 | |
| 3099 /* The value of pExpr->op and op are related as follows: | |
| 3100 ** | |
| 3101 ** pExpr->op op | |
| 3102 ** --------- ---------- | |
| 3103 ** TK_ISNULL OP_NotNull | |
| 3104 ** TK_NOTNULL OP_IsNull | |
| 3105 ** TK_NE OP_Eq | |
| 3106 ** TK_EQ OP_Ne | |
| 3107 ** TK_GT OP_Le | |
| 3108 ** TK_LE OP_Gt | |
| 3109 ** TK_GE OP_Lt | |
| 3110 ** TK_LT OP_Ge | |
| 3111 ** | |
| 3112 ** For other values of pExpr->op, op is undefined and unused. | |
| 3113 ** The value of TK_ and OP_ constants are arranged such that we | |
| 3114 ** can compute the mapping above using the following expression. | |
| 3115 ** Assert()s verify that the computation is correct. | |
| 3116 */ | |
| 3117 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); | |
| 3118 | |
| 3119 /* Verify correct alignment of TK_ and OP_ constants | |
| 3120 */ | |
| 3121 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); | |
| 3122 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); | |
| 3123 assert( pExpr->op!=TK_NE || op==OP_Eq ); | |
| 3124 assert( pExpr->op!=TK_EQ || op==OP_Ne ); | |
| 3125 assert( pExpr->op!=TK_LT || op==OP_Ge ); | |
| 3126 assert( pExpr->op!=TK_LE || op==OP_Gt ); | |
| 3127 assert( pExpr->op!=TK_GT || op==OP_Le ); | |
| 3128 assert( pExpr->op!=TK_GE || op==OP_Lt ); | |
| 3129 | |
| 3130 switch( pExpr->op ){ | |
| 3131 case TK_AND: { | |
| 3132 testcase( jumpIfNull==0 ); | |
| 3133 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); | |
| 3134 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); | |
| 3135 break; | |
| 3136 } | |
| 3137 case TK_OR: { | |
| 3138 int d2 = sqlite3VdbeMakeLabel(v); | |
| 3139 testcase( jumpIfNull==0 ); | |
| 3140 sqlite3ExprCachePush(pParse); | |
| 3141 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); | |
| 3142 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); | |
| 3143 sqlite3VdbeResolveLabel(v, d2); | |
| 3144 sqlite3ExprCachePop(pParse, 1); | |
| 3145 break; | |
| 3146 } | |
| 3147 case TK_NOT: { | |
| 3148 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); | |
| 3149 break; | |
| 3150 } | |
| 3151 case TK_LT: | |
| 3152 case TK_LE: | |
| 3153 case TK_GT: | |
| 3154 case TK_GE: | |
| 3155 case TK_NE: | |
| 3156 case TK_EQ: { | |
| 3157 testcase( op==TK_LT ); | |
| 3158 testcase( op==TK_LE ); | |
| 3159 testcase( op==TK_GT ); | |
| 3160 testcase( op==TK_GE ); | |
| 3161 testcase( op==TK_EQ ); | |
| 3162 testcase( op==TK_NE ); | |
| 3163 testcase( jumpIfNull==0 ); | |
| 3164 codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, | |
| 3165 pExpr->pRight, &r2, ®Free2); | |
| 3166 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | |
| 3167 r1, r2, dest, jumpIfNull); | |
| 3168 testcase( regFree1==0 ); | |
| 3169 testcase( regFree2==0 ); | |
| 3170 break; | |
| 3171 } | |
| 3172 case TK_ISNULL: | |
| 3173 case TK_NOTNULL: { | |
| 3174 testcase( op==TK_ISNULL ); | |
| 3175 testcase( op==TK_NOTNULL ); | |
| 3176 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | |
| 3177 sqlite3VdbeAddOp2(v, op, r1, dest); | |
| 3178 testcase( regFree1==0 ); | |
| 3179 break; | |
| 3180 } | |
| 3181 case TK_BETWEEN: { | |
| 3182 /* x BETWEEN y AND z | |
| 3183 ** | |
| 3184 ** Is equivalent to | |
| 3185 ** | |
| 3186 ** x>=y AND x<=z | |
| 3187 ** | |
| 3188 ** Code it as such, taking care to do the common subexpression | |
| 3189 ** elementation of x. | |
| 3190 */ | |
| 3191 Expr exprAnd; | |
| 3192 Expr compLeft; | |
| 3193 Expr compRight; | |
| 3194 Expr exprX; | |
| 3195 | |
| 3196 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | |
| 3197 exprX = *pExpr->pLeft; | |
| 3198 exprAnd.op = TK_AND; | |
| 3199 exprAnd.pLeft = &compLeft; | |
| 3200 exprAnd.pRight = &compRight; | |
| 3201 compLeft.op = TK_GE; | |
| 3202 compLeft.pLeft = &exprX; | |
| 3203 compLeft.pRight = pExpr->x.pList->a[0].pExpr; | |
| 3204 compRight.op = TK_LE; | |
| 3205 compRight.pLeft = &exprX; | |
| 3206 compRight.pRight = pExpr->x.pList->a[1].pExpr; | |
| 3207 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); | |
| 3208 testcase( regFree1==0 ); | |
| 3209 exprX.op = TK_REGISTER; | |
| 3210 testcase( jumpIfNull==0 ); | |
| 3211 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); | |
| 3212 break; | |
| 3213 } | |
| 3214 default: { | |
| 3215 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); | |
| 3216 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); | |
| 3217 testcase( regFree1==0 ); | |
| 3218 testcase( jumpIfNull==0 ); | |
| 3219 break; | |
| 3220 } | |
| 3221 } | |
| 3222 sqlite3ReleaseTempReg(pParse, regFree1); | |
| 3223 sqlite3ReleaseTempReg(pParse, regFree2); | |
| 3224 } | |
| 3225 | |
| 3226 /* | |
| 3227 ** Do a deep comparison of two expression trees. Return TRUE (non-zero) | |
| 3228 ** if they are identical and return FALSE if they differ in any way. | |
| 3229 ** | |
| 3230 ** Sometimes this routine will return FALSE even if the two expressions | |
| 3231 ** really are equivalent. If we cannot prove that the expressions are | |
| 3232 ** identical, we return FALSE just to be safe. So if this routine | |
| 3233 ** returns false, then you do not really know for certain if the two | |
| 3234 ** expressions are the same. But if you get a TRUE return, then you | |
| 3235 ** can be sure the expressions are the same. In the places where | |
| 3236 ** this routine is used, it does not hurt to get an extra FALSE - that | |
| 3237 ** just might result in some slightly slower code. But returning | |
| 3238 ** an incorrect TRUE could lead to a malfunction. | |
| 3239 */ | |
| 3240 int sqlite3ExprCompare(Expr *pA, Expr *pB){ | |
| 3241 int i; | |
| 3242 if( pA==0||pB==0 ){ | |
| 3243 return pB==pA; | |
| 3244 } | |
| 3245 assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) ); | |
| 3246 assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) ); | |
| 3247 if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){ | |
| 3248 return 0; | |
| 3249 } | |
| 3250 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; | |
| 3251 if( pA->op!=pB->op ) return 0; | |
| 3252 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; | |
| 3253 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; | |
| 3254 | |
| 3255 if( pA->x.pList && pB->x.pList ){ | |
| 3256 if( pA->x.pList->nExpr!=pB->x.pList->nExpr ) return 0; | |
| 3257 for(i=0; i<pA->x.pList->nExpr; i++){ | |
| 3258 Expr *pExprA = pA->x.pList->a[i].pExpr; | |
| 3259 Expr *pExprB = pB->x.pList->a[i].pExpr; | |
| 3260 if( !sqlite3ExprCompare(pExprA, pExprB) ) return 0; | |
| 3261 } | |
| 3262 }else if( pA->x.pList || pB->x.pList ){ | |
| 3263 return 0; | |
| 3264 } | |
| 3265 | |
| 3266 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; | |
| 3267 if( ExprHasProperty(pA, EP_IntValue) ){ | |
| 3268 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){ | |
| 3269 return 0; | |
| 3270 } | |
| 3271 }else if( pA->op!=TK_COLUMN && pA->u.zToken ){ | |
| 3272 if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 0; | |
| 3273 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){ | |
| 3274 return 0; | |
| 3275 } | |
| 3276 } | |
| 3277 return 1; | |
| 3278 } | |
| 3279 | |
| 3280 | |
| 3281 /* | |
| 3282 ** Add a new element to the pAggInfo->aCol[] array. Return the index of | |
| 3283 ** the new element. Return a negative number if malloc fails. | |
| 3284 */ | |
| 3285 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ | |
| 3286 int i; | |
| 3287 pInfo->aCol = sqlite3ArrayAllocate( | |
| 3288 db, | |
| 3289 pInfo->aCol, | |
| 3290 sizeof(pInfo->aCol[0]), | |
| 3291 3, | |
| 3292 &pInfo->nColumn, | |
| 3293 &pInfo->nColumnAlloc, | |
| 3294 &i | |
| 3295 ); | |
| 3296 return i; | |
| 3297 } | |
| 3298 | |
| 3299 /* | |
| 3300 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of | |
| 3301 ** the new element. Return a negative number if malloc fails. | |
| 3302 */ | |
| 3303 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ | |
| 3304 int i; | |
| 3305 pInfo->aFunc = sqlite3ArrayAllocate( | |
| 3306 db, | |
| 3307 pInfo->aFunc, | |
| 3308 sizeof(pInfo->aFunc[0]), | |
| 3309 3, | |
| 3310 &pInfo->nFunc, | |
| 3311 &pInfo->nFuncAlloc, | |
| 3312 &i | |
| 3313 ); | |
| 3314 return i; | |
| 3315 } | |
| 3316 | |
| 3317 /* | |
| 3318 ** This is the xExprCallback for a tree walker. It is used to | |
| 3319 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates | |
| 3320 ** for additional information. | |
| 3321 */ | |
| 3322 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ | |
| 3323 int i; | |
| 3324 NameContext *pNC = pWalker->u.pNC; | |
| 3325 Parse *pParse = pNC->pParse; | |
| 3326 SrcList *pSrcList = pNC->pSrcList; | |
| 3327 AggInfo *pAggInfo = pNC->pAggInfo; | |
| 3328 | |
| 3329 switch( pExpr->op ){ | |
| 3330 case TK_AGG_COLUMN: | |
| 3331 case TK_COLUMN: { | |
| 3332 testcase( pExpr->op==TK_AGG_COLUMN ); | |
| 3333 testcase( pExpr->op==TK_COLUMN ); | |
| 3334 /* Check to see if the column is in one of the tables in the FROM | |
| 3335 ** clause of the aggregate query */ | |
| 3336 if( ALWAYS(pSrcList!=0) ){ | |
| 3337 struct SrcList_item *pItem = pSrcList->a; | |
| 3338 for(i=0; i<pSrcList->nSrc; i++, pItem++){ | |
| 3339 struct AggInfo_col *pCol; | |
| 3340 assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) ); | |
| 3341 if( pExpr->iTable==pItem->iCursor ){ | |
| 3342 /* If we reach this point, it means that pExpr refers to a table | |
| 3343 ** that is in the FROM clause of the aggregate query. | |
| 3344 ** | |
| 3345 ** Make an entry for the column in pAggInfo->aCol[] if there | |
| 3346 ** is not an entry there already. | |
| 3347 */ | |
| 3348 int k; | |
| 3349 pCol = pAggInfo->aCol; | |
| 3350 for(k=0; k<pAggInfo->nColumn; k++, pCol++){ | |
| 3351 if( pCol->iTable==pExpr->iTable && | |
| 3352 pCol->iColumn==pExpr->iColumn ){ | |
| 3353 break; | |
| 3354 } | |
| 3355 } | |
| 3356 if( (k>=pAggInfo->nColumn) | |
| 3357 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 | |
| 3358 ){ | |
| 3359 pCol = &pAggInfo->aCol[k]; | |
| 3360 pCol->pTab = pExpr->pTab; | |
| 3361 pCol->iTable = pExpr->iTable; | |
| 3362 pCol->iColumn = pExpr->iColumn; | |
| 3363 pCol->iMem = ++pParse->nMem; | |
| 3364 pCol->iSorterColumn = -1; | |
| 3365 pCol->pExpr = pExpr; | |
| 3366 if( pAggInfo->pGroupBy ){ | |
| 3367 int j, n; | |
| 3368 ExprList *pGB = pAggInfo->pGroupBy; | |
| 3369 struct ExprList_item *pTerm = pGB->a; | |
| 3370 n = pGB->nExpr; | |
| 3371 for(j=0; j<n; j++, pTerm++){ | |
| 3372 Expr *pE = pTerm->pExpr; | |
| 3373 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && | |
| 3374 pE->iColumn==pExpr->iColumn ){ | |
| 3375 pCol->iSorterColumn = j; | |
| 3376 break; | |
| 3377 } | |
| 3378 } | |
| 3379 } | |
| 3380 if( pCol->iSorterColumn<0 ){ | |
| 3381 pCol->iSorterColumn = pAggInfo->nSortingColumn++; | |
| 3382 } | |
| 3383 } | |
| 3384 /* There is now an entry for pExpr in pAggInfo->aCol[] (either | |
| 3385 ** because it was there before or because we just created it). | |
| 3386 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that | |
| 3387 ** pAggInfo->aCol[] entry. | |
| 3388 */ | |
| 3389 ExprSetIrreducible(pExpr); | |
| 3390 pExpr->pAggInfo = pAggInfo; | |
| 3391 pExpr->op = TK_AGG_COLUMN; | |
| 3392 pExpr->iAgg = (i16)k; | |
| 3393 break; | |
| 3394 } /* endif pExpr->iTable==pItem->iCursor */ | |
| 3395 } /* end loop over pSrcList */ | |
| 3396 } | |
| 3397 return WRC_Prune; | |
| 3398 } | |
| 3399 case TK_AGG_FUNCTION: { | |
| 3400 /* The pNC->nDepth==0 test causes aggregate functions in subqueries | |
| 3401 ** to be ignored */ | |
| 3402 if( pNC->nDepth==0 ){ | |
| 3403 /* Check to see if pExpr is a duplicate of another aggregate | |
| 3404 ** function that is already in the pAggInfo structure | |
| 3405 */ | |
| 3406 struct AggInfo_func *pItem = pAggInfo->aFunc; | |
| 3407 for(i=0; i<pAggInfo->nFunc; i++, pItem++){ | |
| 3408 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ | |
| 3409 break; | |
| 3410 } | |
| 3411 } | |
| 3412 if( i>=pAggInfo->nFunc ){ | |
| 3413 /* pExpr is original. Make a new entry in pAggInfo->aFunc[] | |
| 3414 */ | |
| 3415 u8 enc = ENC(pParse->db); | |
| 3416 i = addAggInfoFunc(pParse->db, pAggInfo); | |
| 3417 if( i>=0 ){ | |
| 3418 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | |
| 3419 pItem = &pAggInfo->aFunc[i]; | |
| 3420 pItem->pExpr = pExpr; | |
| 3421 pItem->iMem = ++pParse->nMem; | |
| 3422 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 3423 pItem->pFunc = sqlite3FindFunction(pParse->db, | |
| 3424 pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken), | |
| 3425 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); | |
| 3426 if( pExpr->flags & EP_Distinct ){ | |
| 3427 pItem->iDistinct = pParse->nTab++; | |
| 3428 }else{ | |
| 3429 pItem->iDistinct = -1; | |
| 3430 } | |
| 3431 } | |
| 3432 } | |
| 3433 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry | |
| 3434 */ | |
| 3435 assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) ); | |
| 3436 ExprSetIrreducible(pExpr); | |
| 3437 pExpr->iAgg = (i16)i; | |
| 3438 pExpr->pAggInfo = pAggInfo; | |
| 3439 return WRC_Prune; | |
| 3440 } | |
| 3441 } | |
| 3442 } | |
| 3443 return WRC_Continue; | |
| 3444 } | |
| 3445 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ | |
| 3446 NameContext *pNC = pWalker->u.pNC; | |
| 3447 if( pNC->nDepth==0 ){ | |
| 3448 pNC->nDepth++; | |
| 3449 sqlite3WalkSelect(pWalker, pSelect); | |
| 3450 pNC->nDepth--; | |
| 3451 return WRC_Prune; | |
| 3452 }else{ | |
| 3453 return WRC_Continue; | |
| 3454 } | |
| 3455 } | |
| 3456 | |
| 3457 /* | |
| 3458 ** Analyze the given expression looking for aggregate functions and | |
| 3459 ** for variables that need to be added to the pParse->aAgg[] array. | |
| 3460 ** Make additional entries to the pParse->aAgg[] array as necessary. | |
| 3461 ** | |
| 3462 ** This routine should only be called after the expression has been | |
| 3463 ** analyzed by sqlite3ResolveExprNames(). | |
| 3464 */ | |
| 3465 void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ | |
| 3466 Walker w; | |
| 3467 w.xExprCallback = analyzeAggregate; | |
| 3468 w.xSelectCallback = analyzeAggregatesInSelect; | |
| 3469 w.u.pNC = pNC; | |
| 3470 assert( pNC->pSrcList!=0 ); | |
| 3471 sqlite3WalkExpr(&w, pExpr); | |
| 3472 } | |
| 3473 | |
| 3474 /* | |
| 3475 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an | |
| 3476 ** expression list. Return the number of errors. | |
| 3477 ** | |
| 3478 ** If an error is found, the analysis is cut short. | |
| 3479 */ | |
| 3480 void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ | |
| 3481 struct ExprList_item *pItem; | |
| 3482 int i; | |
| 3483 if( pList ){ | |
| 3484 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ | |
| 3485 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); | |
| 3486 } | |
| 3487 } | |
| 3488 } | |
| 3489 | |
| 3490 /* | |
| 3491 ** Allocate a single new register for use to hold some intermediate result. | |
| 3492 */ | |
| 3493 int sqlite3GetTempReg(Parse *pParse){ | |
| 3494 if( pParse->nTempReg==0 ){ | |
| 3495 return ++pParse->nMem; | |
| 3496 } | |
| 3497 return pParse->aTempReg[--pParse->nTempReg]; | |
| 3498 } | |
| 3499 | |
| 3500 /* | |
| 3501 ** Deallocate a register, making available for reuse for some other | |
| 3502 ** purpose. | |
| 3503 ** | |
| 3504 ** If a register is currently being used by the column cache, then | |
| 3505 ** the dallocation is deferred until the column cache line that uses | |
| 3506 ** the register becomes stale. | |
| 3507 */ | |
| 3508 void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ | |
| 3509 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ | |
| 3510 int i; | |
| 3511 struct yColCache *p; | |
| 3512 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | |
| 3513 if( p->iReg==iReg ){ | |
| 3514 p->tempReg = 1; | |
| 3515 return; | |
| 3516 } | |
| 3517 } | |
| 3518 pParse->aTempReg[pParse->nTempReg++] = iReg; | |
| 3519 } | |
| 3520 } | |
| 3521 | |
| 3522 /* | |
| 3523 ** Allocate or deallocate a block of nReg consecutive registers | |
| 3524 */ | |
| 3525 int sqlite3GetTempRange(Parse *pParse, int nReg){ | |
| 3526 int i, n; | |
| 3527 i = pParse->iRangeReg; | |
| 3528 n = pParse->nRangeReg; | |
| 3529 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){ | |
| 3530 pParse->iRangeReg += nReg; | |
| 3531 pParse->nRangeReg -= nReg; | |
| 3532 }else{ | |
| 3533 i = pParse->nMem+1; | |
| 3534 pParse->nMem += nReg; | |
| 3535 } | |
| 3536 return i; | |
| 3537 } | |
| 3538 void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ | |
| 3539 if( nReg>pParse->nRangeReg ){ | |
| 3540 pParse->nRangeReg = nReg; | |
| 3541 pParse->iRangeReg = iReg; | |
| 3542 } | |
| 3543 } | |
| OLD | NEW |