| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2001 September 15 | 2 ** 2001 September 15 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains routines used for analyzing expressions and | 12 ** This file contains routines used for analyzing expressions and |
| 13 ** for generating VDBE code that evaluates expressions in SQLite. | 13 ** for generating VDBE code that evaluates expressions in SQLite. |
| 14 */ | 14 */ |
| 15 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
| 16 | 16 |
| 17 /* | 17 /* |
| 18 ** Return the 'affinity' of the expression pExpr if any. | 18 ** Return the 'affinity' of the expression pExpr if any. |
| 19 ** | 19 ** |
| 20 ** If pExpr is a column, a reference to a column via an 'AS' alias, | 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 | 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, | 22 ** affinity of that column is returned. Otherwise, 0x00 is returned, |
| 23 ** indicating no affinity for the expression. | 23 ** indicating no affinity for the expression. |
| 24 ** | 24 ** |
| 25 ** i.e. the WHERE clause expresssions in the following statements all | 25 ** i.e. the WHERE clause expressions in the following statements all |
| 26 ** have an affinity: | 26 ** have an affinity: |
| 27 ** | 27 ** |
| 28 ** CREATE TABLE t1(a); | 28 ** CREATE TABLE t1(a); |
| 29 ** SELECT * FROM t1 WHERE a; | 29 ** SELECT * FROM t1 WHERE a; |
| 30 ** SELECT a AS b FROM t1 WHERE b; | 30 ** SELECT a AS b FROM t1 WHERE b; |
| 31 ** SELECT * FROM t1 WHERE (select a from t1); | 31 ** SELECT * FROM t1 WHERE (select a from t1); |
| 32 */ | 32 */ |
| 33 char sqlite3ExprAffinity(Expr *pExpr){ | 33 char sqlite3ExprAffinity(Expr *pExpr){ |
| 34 int op = pExpr->op; | 34 int op; |
| 35 pExpr = sqlite3ExprSkipCollate(pExpr); |
| 36 if( pExpr->flags & EP_Generic ) return 0; |
| 37 op = pExpr->op; |
| 35 if( op==TK_SELECT ){ | 38 if( op==TK_SELECT ){ |
| 36 assert( pExpr->flags&EP_xIsSelect ); | 39 assert( pExpr->flags&EP_xIsSelect ); |
| 37 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); | 40 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); |
| 38 } | 41 } |
| 39 #ifndef SQLITE_OMIT_CAST | 42 #ifndef SQLITE_OMIT_CAST |
| 40 if( op==TK_CAST ){ | 43 if( op==TK_CAST ){ |
| 41 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | 44 assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 42 return sqlite3AffinityType(pExpr->u.zToken); | 45 return sqlite3AffinityType(pExpr->u.zToken, 0); |
| 43 } | 46 } |
| 44 #endif | 47 #endif |
| 45 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) | 48 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) |
| 46 && pExpr->pTab!=0 | 49 && pExpr->pTab!=0 |
| 47 ){ | 50 ){ |
| 48 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally | 51 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally |
| 49 ** a TK_COLUMN but was previously evaluated and cached in a register */ | 52 ** a TK_COLUMN but was previously evaluated and cached in a register */ |
| 50 int j = pExpr->iColumn; | 53 int j = pExpr->iColumn; |
| 51 if( j<0 ) return SQLITE_AFF_INTEGER; | 54 if( j<0 ) return SQLITE_AFF_INTEGER; |
| 52 assert( pExpr->pTab && j<pExpr->pTab->nCol ); | 55 assert( pExpr->pTab && j<pExpr->pTab->nCol ); |
| 53 return pExpr->pTab->aCol[j].affinity; | 56 return pExpr->pTab->aCol[j].affinity; |
| 54 } | 57 } |
| 55 return pExpr->affinity; | 58 return pExpr->affinity; |
| 56 } | 59 } |
| 57 | 60 |
| 58 /* | 61 /* |
| 59 ** Set the explicit collating sequence for an expression to the | 62 ** Set the collating sequence for expression pExpr to be the collating |
| 60 ** collating sequence supplied in the second argument. | 63 ** sequence named by pToken. Return a pointer to a new Expr node that |
| 64 ** implements the COLLATE operator. |
| 65 ** |
| 66 ** If a memory allocation error occurs, that fact is recorded in pParse->db |
| 67 ** and the pExpr parameter is returned unchanged. |
| 61 */ | 68 */ |
| 62 Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){ | 69 Expr *sqlite3ExprAddCollateToken( |
| 63 if( pExpr && pColl ){ | 70 Parse *pParse, /* Parsing context */ |
| 64 pExpr->pColl = pColl; | 71 Expr *pExpr, /* Add the "COLLATE" clause to this expression */ |
| 65 pExpr->flags |= EP_ExpCollate; | 72 const Token *pCollName /* Name of collating sequence */ |
| 73 ){ |
| 74 if( pCollName->n>0 ){ |
| 75 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1); |
| 76 if( pNew ){ |
| 77 pNew->pLeft = pExpr; |
| 78 pNew->flags |= EP_Collate|EP_Skip; |
| 79 pExpr = pNew; |
| 80 } |
| 66 } | 81 } |
| 67 return pExpr; | 82 return pExpr; |
| 68 } | 83 } |
| 84 Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ |
| 85 Token s; |
| 86 assert( zC!=0 ); |
| 87 s.z = zC; |
| 88 s.n = sqlite3Strlen30(s.z); |
| 89 return sqlite3ExprAddCollateToken(pParse, pExpr, &s); |
| 90 } |
| 91 |
| 92 /* |
| 93 ** Skip over any TK_COLLATE or TK_AS operators and any unlikely() |
| 94 ** or likelihood() function at the root of an expression. |
| 95 */ |
| 96 Expr *sqlite3ExprSkipCollate(Expr *pExpr){ |
| 97 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ |
| 98 if( ExprHasProperty(pExpr, EP_Unlikely) ){ |
| 99 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 100 assert( pExpr->x.pList->nExpr>0 ); |
| 101 assert( pExpr->op==TK_FUNCTION ); |
| 102 pExpr = pExpr->x.pList->a[0].pExpr; |
| 103 }else{ |
| 104 assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS ); |
| 105 pExpr = pExpr->pLeft; |
| 106 } |
| 107 } |
| 108 return pExpr; |
| 109 } |
| 69 | 110 |
| 70 /* | 111 /* |
| 71 ** Set the collating sequence for expression pExpr to be the collating | 112 ** Return the collation sequence for the expression pExpr. If |
| 72 ** sequence named by pToken. Return a pointer to the revised expression. | 113 ** there is no defined collating sequence, return NULL. |
| 73 ** The collating sequence is marked as "explicit" using the EP_ExpCollate | 114 ** |
| 74 ** flag. An explicit collating sequence will override implicit | 115 ** The collating sequence might be determined by a COLLATE operator |
| 75 ** collating sequences. | 116 ** or by the presence of a column with a defined collating sequence. |
| 76 */ | 117 ** COLLATE operators take first precedence. Left operands take |
| 77 Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){ | 118 ** precedence over right operands. |
| 78 char *zColl = 0; /* Dequoted name of collation sequence */ | |
| 79 CollSeq *pColl; | |
| 80 sqlite3 *db = pParse->db; | |
| 81 zColl = sqlite3NameFromToken(db, pCollName); | |
| 82 pColl = sqlite3LocateCollSeq(pParse, zColl); | |
| 83 sqlite3ExprSetColl(pExpr, pColl); | |
| 84 sqlite3DbFree(db, zColl); | |
| 85 return pExpr; | |
| 86 } | |
| 87 | |
| 88 /* | |
| 89 ** Return the default collation sequence for the expression pExpr. If | |
| 90 ** there is no default collation type, return 0. | |
| 91 */ | 119 */ |
| 92 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ | 120 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ |
| 121 sqlite3 *db = pParse->db; |
| 93 CollSeq *pColl = 0; | 122 CollSeq *pColl = 0; |
| 94 Expr *p = pExpr; | 123 Expr *p = pExpr; |
| 95 while( p ){ | 124 while( p ){ |
| 96 int op; | 125 int op = p->op; |
| 97 pColl = p->pColl; | 126 if( p->flags & EP_Generic ) break; |
| 98 if( pColl ) break; | 127 if( op==TK_CAST || op==TK_UPLUS ){ |
| 99 op = p->op; | 128 p = p->pLeft; |
| 100 if( p->pTab!=0 && ( | 129 continue; |
| 101 op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER | 130 } |
| 102 )){ | 131 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){ |
| 132 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); |
| 133 break; |
| 134 } |
| 135 if( p->pTab!=0 |
| 136 && (op==TK_AGG_COLUMN || op==TK_COLUMN |
| 137 || op==TK_REGISTER || op==TK_TRIGGER) |
| 138 ){ |
| 103 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally | 139 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally |
| 104 ** a TK_COLUMN but was previously evaluated and cached in a register */ | 140 ** a TK_COLUMN but was previously evaluated and cached in a register */ |
| 105 const char *zColl; | |
| 106 int j = p->iColumn; | 141 int j = p->iColumn; |
| 107 if( j>=0 ){ | 142 if( j>=0 ){ |
| 108 sqlite3 *db = pParse->db; | 143 const char *zColl = p->pTab->aCol[j].zColl; |
| 109 zColl = p->pTab->aCol[j].zColl; | |
| 110 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); | 144 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
| 111 pExpr->pColl = pColl; | |
| 112 } | 145 } |
| 113 break; | 146 break; |
| 114 } | 147 } |
| 115 if( op!=TK_CAST && op!=TK_UPLUS ){ | 148 if( p->flags & EP_Collate ){ |
| 149 if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){ |
| 150 p = p->pLeft; |
| 151 }else{ |
| 152 p = p->pRight; |
| 153 } |
| 154 }else{ |
| 116 break; | 155 break; |
| 117 } | 156 } |
| 118 p = p->pLeft; | |
| 119 } | 157 } |
| 120 if( sqlite3CheckCollSeq(pParse, pColl) ){ | 158 if( sqlite3CheckCollSeq(pParse, pColl) ){ |
| 121 pColl = 0; | 159 pColl = 0; |
| 122 } | 160 } |
| 123 return pColl; | 161 return pColl; |
| 124 } | 162 } |
| 125 | 163 |
| 126 /* | 164 /* |
| 127 ** pExpr is an operand of a comparison operator. aff2 is the | 165 ** pExpr is an operand of a comparison operator. aff2 is the |
| 128 ** type affinity of the other operand. This routine returns the | 166 ** type affinity of the other operand. This routine returns the |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 ** Argument pRight (but not pLeft) may be a null pointer. In this case, | 250 ** Argument pRight (but not pLeft) may be a null pointer. In this case, |
| 213 ** it is not considered. | 251 ** it is not considered. |
| 214 */ | 252 */ |
| 215 CollSeq *sqlite3BinaryCompareCollSeq( | 253 CollSeq *sqlite3BinaryCompareCollSeq( |
| 216 Parse *pParse, | 254 Parse *pParse, |
| 217 Expr *pLeft, | 255 Expr *pLeft, |
| 218 Expr *pRight | 256 Expr *pRight |
| 219 ){ | 257 ){ |
| 220 CollSeq *pColl; | 258 CollSeq *pColl; |
| 221 assert( pLeft ); | 259 assert( pLeft ); |
| 222 if( pLeft->flags & EP_ExpCollate ){ | 260 if( pLeft->flags & EP_Collate ){ |
| 223 assert( pLeft->pColl ); | 261 pColl = sqlite3ExprCollSeq(pParse, pLeft); |
| 224 pColl = pLeft->pColl; | 262 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){ |
| 225 }else if( pRight && pRight->flags & EP_ExpCollate ){ | 263 pColl = sqlite3ExprCollSeq(pParse, pRight); |
| 226 assert( pRight->pColl ); | |
| 227 pColl = pRight->pColl; | |
| 228 }else{ | 264 }else{ |
| 229 pColl = sqlite3ExprCollSeq(pParse, pLeft); | 265 pColl = sqlite3ExprCollSeq(pParse, pLeft); |
| 230 if( !pColl ){ | 266 if( !pColl ){ |
| 231 pColl = sqlite3ExprCollSeq(pParse, pRight); | 267 pColl = sqlite3ExprCollSeq(pParse, pRight); |
| 232 } | 268 } |
| 233 } | 269 } |
| 234 return pColl; | 270 return pColl; |
| 235 } | 271 } |
| 236 | 272 |
| 237 /* | 273 /* |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 if( pNew ){ | 432 if( pNew ){ |
| 397 pNew->op = (u8)op; | 433 pNew->op = (u8)op; |
| 398 pNew->iAgg = -1; | 434 pNew->iAgg = -1; |
| 399 if( pToken ){ | 435 if( pToken ){ |
| 400 if( nExtra==0 ){ | 436 if( nExtra==0 ){ |
| 401 pNew->flags |= EP_IntValue; | 437 pNew->flags |= EP_IntValue; |
| 402 pNew->u.iValue = iValue; | 438 pNew->u.iValue = iValue; |
| 403 }else{ | 439 }else{ |
| 404 int c; | 440 int c; |
| 405 pNew->u.zToken = (char*)&pNew[1]; | 441 pNew->u.zToken = (char*)&pNew[1]; |
| 406 memcpy(pNew->u.zToken, pToken->z, pToken->n); | 442 assert( pToken->z!=0 || pToken->n==0 ); |
| 443 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); |
| 407 pNew->u.zToken[pToken->n] = 0; | 444 pNew->u.zToken[pToken->n] = 0; |
| 408 if( dequote && nExtra>=3 | 445 if( dequote && nExtra>=3 |
| 409 && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){ | 446 && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){ |
| 410 sqlite3Dequote(pNew->u.zToken); | 447 sqlite3Dequote(pNew->u.zToken); |
| 411 if( c=='"' ) pNew->flags |= EP_DblQuoted; | 448 if( c=='"' ) pNew->flags |= EP_DblQuoted; |
| 412 } | 449 } |
| 413 } | 450 } |
| 414 } | 451 } |
| 415 #if SQLITE_MAX_EXPR_DEPTH>0 | 452 #if SQLITE_MAX_EXPR_DEPTH>0 |
| 416 pNew->nHeight = 1; | 453 pNew->nHeight = 1; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 446 Expr *pLeft, | 483 Expr *pLeft, |
| 447 Expr *pRight | 484 Expr *pRight |
| 448 ){ | 485 ){ |
| 449 if( pRoot==0 ){ | 486 if( pRoot==0 ){ |
| 450 assert( db->mallocFailed ); | 487 assert( db->mallocFailed ); |
| 451 sqlite3ExprDelete(db, pLeft); | 488 sqlite3ExprDelete(db, pLeft); |
| 452 sqlite3ExprDelete(db, pRight); | 489 sqlite3ExprDelete(db, pRight); |
| 453 }else{ | 490 }else{ |
| 454 if( pRight ){ | 491 if( pRight ){ |
| 455 pRoot->pRight = pRight; | 492 pRoot->pRight = pRight; |
| 456 if( pRight->flags & EP_ExpCollate ){ | 493 pRoot->flags |= EP_Collate & pRight->flags; |
| 457 pRoot->flags |= EP_ExpCollate; | |
| 458 pRoot->pColl = pRight->pColl; | |
| 459 } | |
| 460 } | 494 } |
| 461 if( pLeft ){ | 495 if( pLeft ){ |
| 462 pRoot->pLeft = pLeft; | 496 pRoot->pLeft = pLeft; |
| 463 if( pLeft->flags & EP_ExpCollate ){ | 497 pRoot->flags |= EP_Collate & pLeft->flags; |
| 464 pRoot->flags |= EP_ExpCollate; | |
| 465 pRoot->pColl = pLeft->pColl; | |
| 466 } | |
| 467 } | 498 } |
| 468 exprSetHeight(pRoot); | 499 exprSetHeight(pRoot); |
| 469 } | 500 } |
| 470 } | 501 } |
| 471 | 502 |
| 472 /* | 503 /* |
| 473 ** Allocate a Expr node which joins as many as two subtrees. | 504 ** Allocate an Expr node which joins as many as two subtrees. |
| 474 ** | 505 ** |
| 475 ** One or both of the subtrees can be NULL. Return a pointer to the new | 506 ** One or both of the subtrees can be NULL. Return a pointer to the new |
| 476 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, | 507 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, |
| 477 ** free the subtrees and return NULL. | 508 ** free the subtrees and return NULL. |
| 478 */ | 509 */ |
| 479 Expr *sqlite3PExpr( | 510 Expr *sqlite3PExpr( |
| 480 Parse *pParse, /* Parsing context */ | 511 Parse *pParse, /* Parsing context */ |
| 481 int op, /* Expression opcode */ | 512 int op, /* Expression opcode */ |
| 482 Expr *pLeft, /* Left operand */ | 513 Expr *pLeft, /* Left operand */ |
| 483 Expr *pRight, /* Right operand */ | 514 Expr *pRight, /* Right operand */ |
| 484 const Token *pToken /* Argument token */ | 515 const Token *pToken /* Argument token */ |
| 485 ){ | 516 ){ |
| 486 Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1); | 517 Expr *p; |
| 487 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); | 518 if( op==TK_AND && pLeft && pRight ){ |
| 519 /* Take advantage of short-circuit false optimization for AND */ |
| 520 p = sqlite3ExprAnd(pParse->db, pLeft, pRight); |
| 521 }else{ |
| 522 p = sqlite3ExprAlloc(pParse->db, op, pToken, 1); |
| 523 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); |
| 524 } |
| 488 if( p ) { | 525 if( p ) { |
| 489 sqlite3ExprCheckHeight(pParse, p->nHeight); | 526 sqlite3ExprCheckHeight(pParse, p->nHeight); |
| 490 } | 527 } |
| 491 return p; | 528 return p; |
| 492 } | 529 } |
| 493 | 530 |
| 494 /* | 531 /* |
| 532 ** If the expression is always either TRUE or FALSE (respectively), |
| 533 ** then return 1. If one cannot determine the truth value of the |
| 534 ** expression at compile-time return 0. |
| 535 ** |
| 536 ** This is an optimization. If is OK to return 0 here even if |
| 537 ** the expression really is always false or false (a false negative). |
| 538 ** But it is a bug to return 1 if the expression might have different |
| 539 ** boolean values in different circumstances (a false positive.) |
| 540 ** |
| 541 ** Note that if the expression is part of conditional for a |
| 542 ** LEFT JOIN, then we cannot determine at compile-time whether or not |
| 543 ** is it true or false, so always return 0. |
| 544 */ |
| 545 static int exprAlwaysTrue(Expr *p){ |
| 546 int v = 0; |
| 547 if( ExprHasProperty(p, EP_FromJoin) ) return 0; |
| 548 if( !sqlite3ExprIsInteger(p, &v) ) return 0; |
| 549 return v!=0; |
| 550 } |
| 551 static int exprAlwaysFalse(Expr *p){ |
| 552 int v = 0; |
| 553 if( ExprHasProperty(p, EP_FromJoin) ) return 0; |
| 554 if( !sqlite3ExprIsInteger(p, &v) ) return 0; |
| 555 return v==0; |
| 556 } |
| 557 |
| 558 /* |
| 495 ** Join two expressions using an AND operator. If either expression is | 559 ** Join two expressions using an AND operator. If either expression is |
| 496 ** NULL, then just return the other expression. | 560 ** NULL, then just return the other expression. |
| 561 ** |
| 562 ** If one side or the other of the AND is known to be false, then instead |
| 563 ** of returning an AND expression, just return a constant expression with |
| 564 ** a value of false. |
| 497 */ | 565 */ |
| 498 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ | 566 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ |
| 499 if( pLeft==0 ){ | 567 if( pLeft==0 ){ |
| 500 return pRight; | 568 return pRight; |
| 501 }else if( pRight==0 ){ | 569 }else if( pRight==0 ){ |
| 502 return pLeft; | 570 return pLeft; |
| 571 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ |
| 572 sqlite3ExprDelete(db, pLeft); |
| 573 sqlite3ExprDelete(db, pRight); |
| 574 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); |
| 503 }else{ | 575 }else{ |
| 504 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); | 576 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); |
| 505 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); | 577 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); |
| 506 return pNew; | 578 return pNew; |
| 507 } | 579 } |
| 508 } | 580 } |
| 509 | 581 |
| 510 /* | 582 /* |
| 511 ** Construct a new expression node for a function with multiple | 583 ** Construct a new expression node for a function with multiple |
| 512 ** arguments. | 584 ** arguments. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 532 ** | 604 ** |
| 533 ** Wildcards consisting of a single "?" are assigned the next sequential | 605 ** Wildcards consisting of a single "?" are assigned the next sequential |
| 534 ** variable number. | 606 ** variable number. |
| 535 ** | 607 ** |
| 536 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make | 608 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make |
| 537 ** sure "nnn" is not too be to avoid a denial of service attack when | 609 ** sure "nnn" is not too be to avoid a denial of service attack when |
| 538 ** the SQL statement comes from an external source. | 610 ** the SQL statement comes from an external source. |
| 539 ** | 611 ** |
| 540 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number | 612 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number |
| 541 ** as the previous instance of the same wildcard. Or if this is the first | 613 ** as the previous instance of the same wildcard. Or if this is the first |
| 542 ** instance of the wildcard, the next sequenial variable number is | 614 ** instance of the wildcard, the next sequential variable number is |
| 543 ** assigned. | 615 ** assigned. |
| 544 */ | 616 */ |
| 545 void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ | 617 void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ |
| 546 sqlite3 *db = pParse->db; | 618 sqlite3 *db = pParse->db; |
| 547 const char *z; | 619 const char *z; |
| 548 | 620 |
| 549 if( pExpr==0 ) return; | 621 if( pExpr==0 ) return; |
| 550 assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); | 622 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); |
| 551 z = pExpr->u.zToken; | 623 z = pExpr->u.zToken; |
| 552 assert( z!=0 ); | 624 assert( z!=0 ); |
| 553 assert( z[0]!=0 ); | 625 assert( z[0]!=0 ); |
| 554 if( z[1]==0 ){ | 626 if( z[1]==0 ){ |
| 555 /* Wildcard of the form "?". Assign the next variable number */ | 627 /* Wildcard of the form "?". Assign the next variable number */ |
| 556 assert( z[0]=='?' ); | 628 assert( z[0]=='?' ); |
| 557 pExpr->iColumn = (ynVar)(++pParse->nVar); | 629 pExpr->iColumn = (ynVar)(++pParse->nVar); |
| 558 }else if( z[0]=='?' ){ | 630 }else{ |
| 559 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and | 631 ynVar x = 0; |
| 560 ** use it as the variable number */ | 632 u32 n = sqlite3Strlen30(z); |
| 561 i64 i; | 633 if( z[0]=='?' ){ |
| 562 int bOk = 0==sqlite3Atoi64(&z[1], &i, sqlite3Strlen30(&z[1]), SQLITE_UTF8); | 634 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
| 563 pExpr->iColumn = (ynVar)i; | 635 ** use it as the variable number */ |
| 564 testcase( i==0 ); | 636 i64 i; |
| 565 testcase( i==1 ); | 637 int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); |
| 566 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); | 638 pExpr->iColumn = x = (ynVar)i; |
| 567 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); | 639 testcase( i==0 ); |
| 568 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ | 640 testcase( i==1 ); |
| 569 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", | 641 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
| 570 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); | 642 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
| 643 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
| 644 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
| 645 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
| 646 x = 0; |
| 647 } |
| 648 if( i>pParse->nVar ){ |
| 649 pParse->nVar = (int)i; |
| 650 } |
| 651 }else{ |
| 652 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable |
| 653 ** number as the prior appearance of the same name, or if the name |
| 654 ** has never appeared before, reuse the same variable number |
| 655 */ |
| 656 ynVar i; |
| 657 for(i=0; i<pParse->nzVar; i++){ |
| 658 if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ |
| 659 pExpr->iColumn = x = (ynVar)i+1; |
| 660 break; |
| 661 } |
| 662 } |
| 663 if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); |
| 571 } | 664 } |
| 572 if( i>pParse->nVar ){ | 665 if( x>0 ){ |
| 573 pParse->nVar = (int)i; | 666 if( x>pParse->nzVar ){ |
| 574 } | 667 char **a; |
| 575 }else{ | 668 a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0])); |
| 576 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable | 669 if( a==0 ) return; /* Error reported through db->mallocFailed */ |
| 577 ** number as the prior appearance of the same name, or if the name | 670 pParse->azVar = a; |
| 578 ** has never appeared before, reuse the same variable number | 671 memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0])); |
| 579 */ | 672 pParse->nzVar = x; |
| 580 int i; | |
| 581 u32 n; | |
| 582 n = sqlite3Strlen30(z); | |
| 583 for(i=0; i<pParse->nVarExpr; i++){ | |
| 584 Expr *pE = pParse->apVarExpr[i]; | |
| 585 assert( pE!=0 ); | |
| 586 if( memcmp(pE->u.zToken, z, n)==0 && pE->u.zToken[n]==0 ){ | |
| 587 pExpr->iColumn = pE->iColumn; | |
| 588 break; | |
| 589 } | 673 } |
| 590 } | 674 if( z[0]!='?' || pParse->azVar[x-1]==0 ){ |
| 591 if( i>=pParse->nVarExpr ){ | 675 sqlite3DbFree(db, pParse->azVar[x-1]); |
| 592 pExpr->iColumn = (ynVar)(++pParse->nVar); | 676 pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n); |
| 593 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ | |
| 594 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; | |
| 595 pParse->apVarExpr = | |
| 596 sqlite3DbReallocOrFree( | |
| 597 db, | |
| 598 pParse->apVarExpr, | |
| 599 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) | |
| 600 ); | |
| 601 } | |
| 602 if( !db->mallocFailed ){ | |
| 603 assert( pParse->apVarExpr!=0 ); | |
| 604 pParse->apVarExpr[pParse->nVarExpr++] = pExpr; | |
| 605 } | 677 } |
| 606 } | 678 } |
| 607 } | 679 } |
| 608 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ | 680 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
| 609 sqlite3ErrorMsg(pParse, "too many SQL variables"); | 681 sqlite3ErrorMsg(pParse, "too many SQL variables"); |
| 610 } | 682 } |
| 611 } | 683 } |
| 612 | 684 |
| 613 /* | 685 /* |
| 614 ** Recursively delete an expression tree. | 686 ** Recursively delete an expression tree. |
| 615 */ | 687 */ |
| 616 void sqlite3ExprDelete(sqlite3 *db, Expr *p){ | 688 void sqlite3ExprDelete(sqlite3 *db, Expr *p){ |
| 617 if( p==0 ) return; | 689 if( p==0 ) return; |
| 618 /* Sanity check: Assert that the IntValue is non-negative if it exists */ | 690 /* Sanity check: Assert that the IntValue is non-negative if it exists */ |
| 619 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); | 691 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); |
| 620 if( !ExprHasAnyProperty(p, EP_TokenOnly) ){ | 692 if( !ExprHasProperty(p, EP_TokenOnly) ){ |
| 693 /* The Expr.x union is never used at the same time as Expr.pRight */ |
| 694 assert( p->x.pList==0 || p->pRight==0 ); |
| 621 sqlite3ExprDelete(db, p->pLeft); | 695 sqlite3ExprDelete(db, p->pLeft); |
| 622 sqlite3ExprDelete(db, p->pRight); | 696 sqlite3ExprDelete(db, p->pRight); |
| 623 if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){ | 697 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); |
| 624 sqlite3DbFree(db, p->u.zToken); | |
| 625 } | |
| 626 if( ExprHasProperty(p, EP_xIsSelect) ){ | 698 if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 627 sqlite3SelectDelete(db, p->x.pSelect); | 699 sqlite3SelectDelete(db, p->x.pSelect); |
| 628 }else{ | 700 }else{ |
| 629 sqlite3ExprListDelete(db, p->x.pList); | 701 sqlite3ExprListDelete(db, p->x.pList); |
| 630 } | 702 } |
| 631 } | 703 } |
| 632 if( !ExprHasProperty(p, EP_Static) ){ | 704 if( !ExprHasProperty(p, EP_Static) ){ |
| 633 sqlite3DbFree(db, p); | 705 sqlite3DbFree(db, p); |
| 634 } | 706 } |
| 635 } | 707 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 ** | 739 ** |
| 668 ** The size of the structure can be found by masking the return value | 740 ** The size of the structure can be found by masking the return value |
| 669 ** of this routine with 0xfff. The flags can be found by masking the | 741 ** of this routine with 0xfff. The flags can be found by masking the |
| 670 ** return value with EP_Reduced|EP_TokenOnly. | 742 ** return value with EP_Reduced|EP_TokenOnly. |
| 671 ** | 743 ** |
| 672 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size | 744 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size |
| 673 ** (unreduced) Expr objects as they or originally constructed by the parser. | 745 ** (unreduced) Expr objects as they or originally constructed by the parser. |
| 674 ** During expression analysis, extra information is computed and moved into | 746 ** During expression analysis, extra information is computed and moved into |
| 675 ** later parts of teh Expr object and that extra information might get chopped | 747 ** later parts of teh Expr object and that extra information might get chopped |
| 676 ** off if the expression is reduced. Note also that it does not work to | 748 ** off if the expression is reduced. Note also that it does not work to |
| 677 ** make a EXPRDUP_REDUCE copy of a reduced expression. It is only legal | 749 ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal |
| 678 ** to reduce a pristine expression tree from the parser. The implementation | 750 ** to reduce a pristine expression tree from the parser. The implementation |
| 679 ** of dupedExprStructSize() contain multiple assert() statements that attempt | 751 ** of dupedExprStructSize() contain multiple assert() statements that attempt |
| 680 ** to enforce this constraint. | 752 ** to enforce this constraint. |
| 681 */ | 753 */ |
| 682 static int dupedExprStructSize(Expr *p, int flags){ | 754 static int dupedExprStructSize(Expr *p, int flags){ |
| 683 int nSize; | 755 int nSize; |
| 684 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ | 756 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ |
| 757 assert( EXPR_FULLSIZE<=0xfff ); |
| 758 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); |
| 685 if( 0==(flags&EXPRDUP_REDUCE) ){ | 759 if( 0==(flags&EXPRDUP_REDUCE) ){ |
| 686 nSize = EXPR_FULLSIZE; | 760 nSize = EXPR_FULLSIZE; |
| 687 }else{ | 761 }else{ |
| 688 assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) ); | 762 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
| 689 assert( !ExprHasProperty(p, EP_FromJoin) ); | 763 assert( !ExprHasProperty(p, EP_FromJoin) ); |
| 690 assert( (p->flags2 & EP2_MallocedToken)==0 ); | 764 assert( !ExprHasProperty(p, EP_MemToken) ); |
| 691 assert( (p->flags2 & EP2_Irreducible)==0 ); | 765 assert( !ExprHasProperty(p, EP_NoReduce) ); |
| 692 if( p->pLeft || p->pRight || p->pColl || p->x.pList ){ | 766 if( p->pLeft || p->x.pList ){ |
| 693 nSize = EXPR_REDUCEDSIZE | EP_Reduced; | 767 nSize = EXPR_REDUCEDSIZE | EP_Reduced; |
| 694 }else{ | 768 }else{ |
| 769 assert( p->pRight==0 ); |
| 695 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; | 770 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; |
| 696 } | 771 } |
| 697 } | 772 } |
| 698 return nSize; | 773 return nSize; |
| 699 } | 774 } |
| 700 | 775 |
| 701 /* | 776 /* |
| 702 ** This function returns the space in bytes required to store the copy | 777 ** This function returns the space in bytes required to store the copy |
| 703 ** of the Expr structure and a copy of the Expr.u.zToken string (if that | 778 ** of the Expr structure and a copy of the Expr.u.zToken string (if that |
| 704 ** string is defined.) | 779 ** string is defined.) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 733 } | 808 } |
| 734 } | 809 } |
| 735 return nByte; | 810 return nByte; |
| 736 } | 811 } |
| 737 | 812 |
| 738 /* | 813 /* |
| 739 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer | 814 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer |
| 740 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough | 815 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough |
| 741 ** to store the copy of expression p, the copies of p->u.zToken | 816 ** to store the copy of expression p, the copies of p->u.zToken |
| 742 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, | 817 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, |
| 743 ** if any. Before returning, *pzBuffer is set to the first byte passed the | 818 ** if any. Before returning, *pzBuffer is set to the first byte past the |
| 744 ** portion of the buffer copied into by this function. | 819 ** portion of the buffer copied into by this function. |
| 745 */ | 820 */ |
| 746 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ | 821 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ |
| 747 Expr *pNew = 0; /* Value to return */ | 822 Expr *pNew = 0; /* Value to return */ |
| 748 if( p ){ | 823 if( p ){ |
| 749 const int isReduced = (flags&EXPRDUP_REDUCE); | 824 const int isReduced = (flags&EXPRDUP_REDUCE); |
| 750 u8 *zAlloc; | 825 u8 *zAlloc; |
| 751 u32 staticFlag = 0; | 826 u32 staticFlag = 0; |
| 752 | 827 |
| 753 assert( pzBuffer==0 || isReduced ); | 828 assert( pzBuffer==0 || isReduced ); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 778 if( isReduced ){ | 853 if( isReduced ){ |
| 779 assert( ExprHasProperty(p, EP_Reduced)==0 ); | 854 assert( ExprHasProperty(p, EP_Reduced)==0 ); |
| 780 memcpy(zAlloc, p, nNewSize); | 855 memcpy(zAlloc, p, nNewSize); |
| 781 }else{ | 856 }else{ |
| 782 int nSize = exprStructSize(p); | 857 int nSize = exprStructSize(p); |
| 783 memcpy(zAlloc, p, nSize); | 858 memcpy(zAlloc, p, nSize); |
| 784 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); | 859 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); |
| 785 } | 860 } |
| 786 | 861 |
| 787 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ | 862 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ |
| 788 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static); | 863 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); |
| 789 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); | 864 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); |
| 790 pNew->flags |= staticFlag; | 865 pNew->flags |= staticFlag; |
| 791 | 866 |
| 792 /* Copy the p->u.zToken string, if any. */ | 867 /* Copy the p->u.zToken string, if any. */ |
| 793 if( nToken ){ | 868 if( nToken ){ |
| 794 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; | 869 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; |
| 795 memcpy(zToken, p->u.zToken, nToken); | 870 memcpy(zToken, p->u.zToken, nToken); |
| 796 } | 871 } |
| 797 | 872 |
| 798 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ | 873 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ |
| 799 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ | 874 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ |
| 800 if( ExprHasProperty(p, EP_xIsSelect) ){ | 875 if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 801 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced); | 876 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced); |
| 802 }else{ | 877 }else{ |
| 803 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced); | 878 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced); |
| 804 } | 879 } |
| 805 } | 880 } |
| 806 | 881 |
| 807 /* Fill in pNew->pLeft and pNew->pRight. */ | 882 /* Fill in pNew->pLeft and pNew->pRight. */ |
| 808 if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){ | 883 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ |
| 809 zAlloc += dupedExprNodeSize(p, flags); | 884 zAlloc += dupedExprNodeSize(p, flags); |
| 810 if( ExprHasProperty(pNew, EP_Reduced) ){ | 885 if( ExprHasProperty(pNew, EP_Reduced) ){ |
| 811 pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc); | 886 pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc); |
| 812 pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc); | 887 pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc); |
| 813 } | 888 } |
| 814 if( pzBuffer ){ | 889 if( pzBuffer ){ |
| 815 *pzBuffer = zAlloc; | 890 *pzBuffer = zAlloc; |
| 816 } | 891 } |
| 817 }else{ | 892 }else{ |
| 818 pNew->flags2 = 0; | 893 if( !ExprHasProperty(p, EP_TokenOnly) ){ |
| 819 if( !ExprHasAnyProperty(p, EP_TokenOnly) ){ | |
| 820 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); | 894 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); |
| 821 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); | 895 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); |
| 822 } | 896 } |
| 823 } | 897 } |
| 824 | 898 |
| 825 } | 899 } |
| 826 } | 900 } |
| 827 return pNew; | 901 return pNew; |
| 828 } | 902 } |
| 829 | 903 |
| 830 /* | 904 /* |
| 905 ** Create and return a deep copy of the object passed as the second |
| 906 ** argument. If an OOM condition is encountered, NULL is returned |
| 907 ** and the db->mallocFailed flag set. |
| 908 */ |
| 909 #ifndef SQLITE_OMIT_CTE |
| 910 static With *withDup(sqlite3 *db, With *p){ |
| 911 With *pRet = 0; |
| 912 if( p ){ |
| 913 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); |
| 914 pRet = sqlite3DbMallocZero(db, nByte); |
| 915 if( pRet ){ |
| 916 int i; |
| 917 pRet->nCte = p->nCte; |
| 918 for(i=0; i<p->nCte; i++){ |
| 919 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); |
| 920 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); |
| 921 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); |
| 922 } |
| 923 } |
| 924 } |
| 925 return pRet; |
| 926 } |
| 927 #else |
| 928 # define withDup(x,y) 0 |
| 929 #endif |
| 930 |
| 931 /* |
| 831 ** The following group of routines make deep copies of expressions, | 932 ** The following group of routines make deep copies of expressions, |
| 832 ** expression lists, ID lists, and select statements. The copies can | 933 ** expression lists, ID lists, and select statements. The copies can |
| 833 ** be deleted (by being passed to their respective ...Delete() routines) | 934 ** be deleted (by being passed to their respective ...Delete() routines) |
| 834 ** without effecting the originals. | 935 ** without effecting the originals. |
| 835 ** | 936 ** |
| 836 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), | 937 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), |
| 837 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded | 938 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded |
| 838 ** by subsequent calls to sqlite*ListAppend() routines. | 939 ** by subsequent calls to sqlite*ListAppend() routines. |
| 839 ** | 940 ** |
| 840 ** Any tables that the SrcList might point to are not duplicated. | 941 ** Any tables that the SrcList might point to are not duplicated. |
| 841 ** | 942 ** |
| 842 ** The flags parameter contains a combination of the EXPRDUP_XXX flags. | 943 ** The flags parameter contains a combination of the EXPRDUP_XXX flags. |
| 843 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a | 944 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a |
| 844 ** truncated version of the usual Expr structure that will be stored as | 945 ** truncated version of the usual Expr structure that will be stored as |
| 845 ** part of the in-memory representation of the database schema. | 946 ** part of the in-memory representation of the database schema. |
| 846 */ | 947 */ |
| 847 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ | 948 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ |
| 848 return exprDup(db, p, flags, 0); | 949 return exprDup(db, p, flags, 0); |
| 849 } | 950 } |
| 850 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ | 951 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ |
| 851 ExprList *pNew; | 952 ExprList *pNew; |
| 852 struct ExprList_item *pItem, *pOldItem; | 953 struct ExprList_item *pItem, *pOldItem; |
| 853 int i; | 954 int i; |
| 854 if( p==0 ) return 0; | 955 if( p==0 ) return 0; |
| 855 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); | 956 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
| 856 if( pNew==0 ) return 0; | 957 if( pNew==0 ) return 0; |
| 857 pNew->iECursor = 0; | 958 pNew->nExpr = i = p->nExpr; |
| 858 pNew->nExpr = pNew->nAlloc = p->nExpr; | 959 if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){} |
| 859 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); | 960 pNew->a = pItem = sqlite3DbMallocRaw(db, i*sizeof(p->a[0]) ); |
| 860 if( pItem==0 ){ | 961 if( pItem==0 ){ |
| 861 sqlite3DbFree(db, pNew); | 962 sqlite3DbFree(db, pNew); |
| 862 return 0; | 963 return 0; |
| 863 } | 964 } |
| 864 pOldItem = p->a; | 965 pOldItem = p->a; |
| 865 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ | 966 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ |
| 866 Expr *pOldExpr = pOldItem->pExpr; | 967 Expr *pOldExpr = pOldItem->pExpr; |
| 867 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); | 968 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); |
| 868 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); | 969 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 869 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); | 970 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); |
| 870 pItem->sortOrder = pOldItem->sortOrder; | 971 pItem->sortOrder = pOldItem->sortOrder; |
| 871 pItem->done = 0; | 972 pItem->done = 0; |
| 872 pItem->iCol = pOldItem->iCol; | 973 pItem->bSpanIsTab = pOldItem->bSpanIsTab; |
| 873 pItem->iAlias = pOldItem->iAlias; | 974 pItem->u = pOldItem->u; |
| 874 } | 975 } |
| 875 return pNew; | 976 return pNew; |
| 876 } | 977 } |
| 877 | 978 |
| 878 /* | 979 /* |
| 879 ** If cursors, triggers, views and subqueries are all omitted from | 980 ** If cursors, triggers, views and subqueries are all omitted from |
| 880 ** the build, then none of the following routines, except for | 981 ** the build, then none of the following routines, except for |
| 881 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes | 982 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes |
| 882 ** called with a NULL argument. | 983 ** called with a NULL argument. |
| 883 */ | 984 */ |
| 884 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ | 985 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ |
| 885 || !defined(SQLITE_OMIT_SUBQUERY) | 986 || !defined(SQLITE_OMIT_SUBQUERY) |
| 886 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ | 987 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ |
| 887 SrcList *pNew; | 988 SrcList *pNew; |
| 888 int i; | 989 int i; |
| 889 int nByte; | 990 int nByte; |
| 890 if( p==0 ) return 0; | 991 if( p==0 ) return 0; |
| 891 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); | 992 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); |
| 892 pNew = sqlite3DbMallocRaw(db, nByte ); | 993 pNew = sqlite3DbMallocRaw(db, nByte ); |
| 893 if( pNew==0 ) return 0; | 994 if( pNew==0 ) return 0; |
| 894 pNew->nSrc = pNew->nAlloc = p->nSrc; | 995 pNew->nSrc = pNew->nAlloc = p->nSrc; |
| 895 for(i=0; i<p->nSrc; i++){ | 996 for(i=0; i<p->nSrc; i++){ |
| 896 struct SrcList_item *pNewItem = &pNew->a[i]; | 997 struct SrcList_item *pNewItem = &pNew->a[i]; |
| 897 struct SrcList_item *pOldItem = &p->a[i]; | 998 struct SrcList_item *pOldItem = &p->a[i]; |
| 898 Table *pTab; | 999 Table *pTab; |
| 1000 pNewItem->pSchema = pOldItem->pSchema; |
| 899 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); | 1001 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); |
| 900 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); | 1002 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 901 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); | 1003 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); |
| 902 pNewItem->jointype = pOldItem->jointype; | 1004 pNewItem->jointype = pOldItem->jointype; |
| 903 pNewItem->iCursor = pOldItem->iCursor; | 1005 pNewItem->iCursor = pOldItem->iCursor; |
| 904 pNewItem->isPopulated = pOldItem->isPopulated; | 1006 pNewItem->addrFillSub = pOldItem->addrFillSub; |
| 1007 pNewItem->regReturn = pOldItem->regReturn; |
| 1008 pNewItem->isCorrelated = pOldItem->isCorrelated; |
| 1009 pNewItem->viaCoroutine = pOldItem->viaCoroutine; |
| 1010 pNewItem->isRecursive = pOldItem->isRecursive; |
| 905 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex); | 1011 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex); |
| 906 pNewItem->notIndexed = pOldItem->notIndexed; | 1012 pNewItem->notIndexed = pOldItem->notIndexed; |
| 907 pNewItem->pIndex = pOldItem->pIndex; | 1013 pNewItem->pIndex = pOldItem->pIndex; |
| 908 pTab = pNewItem->pTab = pOldItem->pTab; | 1014 pTab = pNewItem->pTab = pOldItem->pTab; |
| 909 if( pTab ){ | 1015 if( pTab ){ |
| 910 pTab->nRef++; | 1016 pTab->nRef++; |
| 911 } | 1017 } |
| 912 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); | 1018 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); |
| 913 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); | 1019 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); |
| 914 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); | 1020 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); |
| 915 pNewItem->colUsed = pOldItem->colUsed; | 1021 pNewItem->colUsed = pOldItem->colUsed; |
| 916 } | 1022 } |
| 917 return pNew; | 1023 return pNew; |
| 918 } | 1024 } |
| 919 IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ | 1025 IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ |
| 920 IdList *pNew; | 1026 IdList *pNew; |
| 921 int i; | 1027 int i; |
| 922 if( p==0 ) return 0; | 1028 if( p==0 ) return 0; |
| 923 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); | 1029 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
| 924 if( pNew==0 ) return 0; | 1030 if( pNew==0 ) return 0; |
| 925 pNew->nId = pNew->nAlloc = p->nId; | 1031 pNew->nId = p->nId; |
| 926 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); | 1032 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); |
| 927 if( pNew->a==0 ){ | 1033 if( pNew->a==0 ){ |
| 928 sqlite3DbFree(db, pNew); | 1034 sqlite3DbFree(db, pNew); |
| 929 return 0; | 1035 return 0; |
| 930 } | 1036 } |
| 1037 /* Note that because the size of the allocation for p->a[] is not |
| 1038 ** necessarily a power of two, sqlite3IdListAppend() may not be called |
| 1039 ** on the duplicate created by this function. */ |
| 931 for(i=0; i<p->nId; i++){ | 1040 for(i=0; i<p->nId; i++){ |
| 932 struct IdList_item *pNewItem = &pNew->a[i]; | 1041 struct IdList_item *pNewItem = &pNew->a[i]; |
| 933 struct IdList_item *pOldItem = &p->a[i]; | 1042 struct IdList_item *pOldItem = &p->a[i]; |
| 934 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); | 1043 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 935 pNewItem->idx = pOldItem->idx; | 1044 pNewItem->idx = pOldItem->idx; |
| 936 } | 1045 } |
| 937 return pNew; | 1046 return pNew; |
| 938 } | 1047 } |
| 939 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ | 1048 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
| 940 Select *pNew; | 1049 Select *pNew, *pPrior; |
| 941 if( p==0 ) return 0; | 1050 if( p==0 ) return 0; |
| 942 pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); | 1051 pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); |
| 943 if( pNew==0 ) return 0; | 1052 if( pNew==0 ) return 0; |
| 944 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); | 1053 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); |
| 945 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); | 1054 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); |
| 946 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); | 1055 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); |
| 947 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); | 1056 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); |
| 948 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); | 1057 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); |
| 949 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); | 1058 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); |
| 950 pNew->op = p->op; | 1059 pNew->op = p->op; |
| 951 pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags); | 1060 pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); |
| 1061 if( pPrior ) pPrior->pNext = pNew; |
| 1062 pNew->pNext = 0; |
| 952 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); | 1063 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); |
| 953 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); | 1064 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); |
| 954 pNew->iLimit = 0; | 1065 pNew->iLimit = 0; |
| 955 pNew->iOffset = 0; | 1066 pNew->iOffset = 0; |
| 956 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; | 1067 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
| 957 pNew->pRightmost = 0; | |
| 958 pNew->addrOpenEphm[0] = -1; | 1068 pNew->addrOpenEphm[0] = -1; |
| 959 pNew->addrOpenEphm[1] = -1; | 1069 pNew->addrOpenEphm[1] = -1; |
| 960 pNew->addrOpenEphm[2] = -1; | 1070 pNew->nSelectRow = p->nSelectRow; |
| 1071 pNew->pWith = withDup(db, p->pWith); |
| 1072 sqlite3SelectSetName(pNew, p->zSelName); |
| 961 return pNew; | 1073 return pNew; |
| 962 } | 1074 } |
| 963 #else | 1075 #else |
| 964 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ | 1076 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
| 965 assert( p==0 ); | 1077 assert( p==0 ); |
| 966 return 0; | 1078 return 0; |
| 967 } | 1079 } |
| 968 #endif | 1080 #endif |
| 969 | 1081 |
| 970 | 1082 |
| 971 /* | 1083 /* |
| 972 ** Add a new element to the end of an expression list. If pList is | 1084 ** Add a new element to the end of an expression list. If pList is |
| 973 ** initially NULL, then create a new expression list. | 1085 ** initially NULL, then create a new expression list. |
| 974 ** | 1086 ** |
| 975 ** If a memory allocation error occurs, the entire list is freed and | 1087 ** If a memory allocation error occurs, the entire list is freed and |
| 976 ** NULL is returned. If non-NULL is returned, then it is guaranteed | 1088 ** NULL is returned. If non-NULL is returned, then it is guaranteed |
| 977 ** that the new entry was successfully appended. | 1089 ** that the new entry was successfully appended. |
| 978 */ | 1090 */ |
| 979 ExprList *sqlite3ExprListAppend( | 1091 ExprList *sqlite3ExprListAppend( |
| 980 Parse *pParse, /* Parsing context */ | 1092 Parse *pParse, /* Parsing context */ |
| 981 ExprList *pList, /* List to which to append. Might be NULL */ | 1093 ExprList *pList, /* List to which to append. Might be NULL */ |
| 982 Expr *pExpr /* Expression to be appended. Might be NULL */ | 1094 Expr *pExpr /* Expression to be appended. Might be NULL */ |
| 983 ){ | 1095 ){ |
| 984 sqlite3 *db = pParse->db; | 1096 sqlite3 *db = pParse->db; |
| 985 if( pList==0 ){ | 1097 if( pList==0 ){ |
| 986 pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); | 1098 pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); |
| 987 if( pList==0 ){ | 1099 if( pList==0 ){ |
| 988 goto no_mem; | 1100 goto no_mem; |
| 989 } | 1101 } |
| 990 assert( pList->nAlloc==0 ); | 1102 pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0])); |
| 991 } | 1103 if( pList->a==0 ) goto no_mem; |
| 992 if( pList->nAlloc<=pList->nExpr ){ | 1104 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ |
| 993 struct ExprList_item *a; | 1105 struct ExprList_item *a; |
| 994 int n = pList->nAlloc*2 + 4; | 1106 assert( pList->nExpr>0 ); |
| 995 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); | 1107 a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); |
| 996 if( a==0 ){ | 1108 if( a==0 ){ |
| 997 goto no_mem; | 1109 goto no_mem; |
| 998 } | 1110 } |
| 999 pList->a = a; | 1111 pList->a = a; |
| 1000 pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]); | |
| 1001 } | 1112 } |
| 1002 assert( pList->a!=0 ); | 1113 assert( pList->a!=0 ); |
| 1003 if( 1 ){ | 1114 if( 1 ){ |
| 1004 struct ExprList_item *pItem = &pList->a[pList->nExpr++]; | 1115 struct ExprList_item *pItem = &pList->a[pList->nExpr++]; |
| 1005 memset(pItem, 0, sizeof(*pItem)); | 1116 memset(pItem, 0, sizeof(*pItem)); |
| 1006 pItem->pExpr = pExpr; | 1117 pItem->pExpr = pExpr; |
| 1007 } | 1118 } |
| 1008 return pList; | 1119 return pList; |
| 1009 | 1120 |
| 1010 no_mem: | 1121 no_mem: |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 } | 1192 } |
| 1082 } | 1193 } |
| 1083 | 1194 |
| 1084 /* | 1195 /* |
| 1085 ** Delete an entire expression list. | 1196 ** Delete an entire expression list. |
| 1086 */ | 1197 */ |
| 1087 void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ | 1198 void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ |
| 1088 int i; | 1199 int i; |
| 1089 struct ExprList_item *pItem; | 1200 struct ExprList_item *pItem; |
| 1090 if( pList==0 ) return; | 1201 if( pList==0 ) return; |
| 1091 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); | 1202 assert( pList->a!=0 || pList->nExpr==0 ); |
| 1092 assert( pList->nExpr<=pList->nAlloc ); | |
| 1093 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ | 1203 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
| 1094 sqlite3ExprDelete(db, pItem->pExpr); | 1204 sqlite3ExprDelete(db, pItem->pExpr); |
| 1095 sqlite3DbFree(db, pItem->zName); | 1205 sqlite3DbFree(db, pItem->zName); |
| 1096 sqlite3DbFree(db, pItem->zSpan); | 1206 sqlite3DbFree(db, pItem->zSpan); |
| 1097 } | 1207 } |
| 1098 sqlite3DbFree(db, pList->a); | 1208 sqlite3DbFree(db, pList->a); |
| 1099 sqlite3DbFree(db, pList); | 1209 sqlite3DbFree(db, pList); |
| 1100 } | 1210 } |
| 1101 | 1211 |
| 1102 /* | 1212 /* |
| 1103 ** These routines are Walker callbacks. Walker.u.pi is a pointer | 1213 ** These routines are Walker callbacks. Walker.u.pi is a pointer |
| 1104 ** to an integer. These routines are checking an expression to see | 1214 ** to an integer. These routines are checking an expression to see |
| 1105 ** if it is a constant. Set *Walker.u.pi to 0 if the expression is | 1215 ** if it is a constant. Set *Walker.u.i to 0 if the expression is |
| 1106 ** not constant. | 1216 ** not constant. |
| 1107 ** | 1217 ** |
| 1108 ** These callback routines are used to implement the following: | 1218 ** These callback routines are used to implement the following: |
| 1109 ** | 1219 ** |
| 1110 ** sqlite3ExprIsConstant() | 1220 ** sqlite3ExprIsConstant() pWalker->u.i==1 |
| 1111 ** sqlite3ExprIsConstantNotJoin() | 1221 ** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2 |
| 1112 ** sqlite3ExprIsConstantOrFunction() | 1222 ** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4 |
| 1113 ** | 1223 ** |
| 1224 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
| 1225 ** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing |
| 1226 ** an existing schema and 3 when processing a new statement. A bound |
| 1227 ** parameter raises an error for new statements, but is silently converted |
| 1228 ** to NULL for existing schemas. This allows sqlite_master tables that |
| 1229 ** contain a bound parameter because they were generated by older versions |
| 1230 ** of SQLite to be parsed by newer versions of SQLite without raising a |
| 1231 ** malformed schema error. |
| 1114 */ | 1232 */ |
| 1115 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ | 1233 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
| 1116 | 1234 |
| 1117 /* If pWalker->u.i is 3 then any term of the expression that comes from | 1235 /* If pWalker->u.i is 2 then any term of the expression that comes from |
| 1118 ** the ON or USING clauses of a join disqualifies the expression | 1236 ** the ON or USING clauses of a join disqualifies the expression |
| 1119 ** from being considered constant. */ | 1237 ** from being considered constant. */ |
| 1120 if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ | 1238 if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 1121 pWalker->u.i = 0; | 1239 pWalker->u.i = 0; |
| 1122 return WRC_Abort; | 1240 return WRC_Abort; |
| 1123 } | 1241 } |
| 1124 | 1242 |
| 1125 switch( pExpr->op ){ | 1243 switch( pExpr->op ){ |
| 1126 /* Consider functions to be constant if all their arguments are constant | 1244 /* Consider functions to be constant if all their arguments are constant |
| 1127 ** and pWalker->u.i==2 */ | 1245 ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST |
| 1246 ** flag. */ |
| 1128 case TK_FUNCTION: | 1247 case TK_FUNCTION: |
| 1129 if( pWalker->u.i==2 ) return 0; | 1248 if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){ |
| 1249 return WRC_Continue; |
| 1250 } |
| 1130 /* Fall through */ | 1251 /* Fall through */ |
| 1131 case TK_ID: | 1252 case TK_ID: |
| 1132 case TK_COLUMN: | 1253 case TK_COLUMN: |
| 1133 case TK_AGG_FUNCTION: | 1254 case TK_AGG_FUNCTION: |
| 1134 case TK_AGG_COLUMN: | 1255 case TK_AGG_COLUMN: |
| 1135 testcase( pExpr->op==TK_ID ); | 1256 testcase( pExpr->op==TK_ID ); |
| 1136 testcase( pExpr->op==TK_COLUMN ); | 1257 testcase( pExpr->op==TK_COLUMN ); |
| 1137 testcase( pExpr->op==TK_AGG_FUNCTION ); | 1258 testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 1138 testcase( pExpr->op==TK_AGG_COLUMN ); | 1259 testcase( pExpr->op==TK_AGG_COLUMN ); |
| 1139 pWalker->u.i = 0; | 1260 pWalker->u.i = 0; |
| 1140 return WRC_Abort; | 1261 return WRC_Abort; |
| 1262 case TK_VARIABLE: |
| 1263 if( pWalker->u.i==4 ){ |
| 1264 /* Silently convert bound parameters that appear inside of CREATE |
| 1265 ** statements into a NULL when parsing the CREATE statement text out |
| 1266 ** of the sqlite_master table */ |
| 1267 pExpr->op = TK_NULL; |
| 1268 }else if( pWalker->u.i==3 ){ |
| 1269 /* A bound parameter in a CREATE statement that originates from |
| 1270 ** sqlite3_prepare() causes an error */ |
| 1271 pWalker->u.i = 0; |
| 1272 return WRC_Abort; |
| 1273 } |
| 1274 /* Fall through */ |
| 1141 default: | 1275 default: |
| 1142 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ | 1276 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
| 1143 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ | 1277 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ |
| 1144 return WRC_Continue; | 1278 return WRC_Continue; |
| 1145 } | 1279 } |
| 1146 } | 1280 } |
| 1147 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ | 1281 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ |
| 1148 UNUSED_PARAMETER(NotUsed); | 1282 UNUSED_PARAMETER(NotUsed); |
| 1149 pWalker->u.i = 0; | 1283 pWalker->u.i = 0; |
| 1150 return WRC_Abort; | 1284 return WRC_Abort; |
| 1151 } | 1285 } |
| 1152 static int exprIsConst(Expr *p, int initFlag){ | 1286 static int exprIsConst(Expr *p, int initFlag){ |
| 1153 Walker w; | 1287 Walker w; |
| 1288 memset(&w, 0, sizeof(w)); |
| 1154 w.u.i = initFlag; | 1289 w.u.i = initFlag; |
| 1155 w.xExprCallback = exprNodeIsConstant; | 1290 w.xExprCallback = exprNodeIsConstant; |
| 1156 w.xSelectCallback = selectNodeIsConstant; | 1291 w.xSelectCallback = selectNodeIsConstant; |
| 1157 sqlite3WalkExpr(&w, p); | 1292 sqlite3WalkExpr(&w, p); |
| 1158 return w.u.i; | 1293 return w.u.i; |
| 1159 } | 1294 } |
| 1160 | 1295 |
| 1161 /* | 1296 /* |
| 1162 ** Walk an expression tree. Return 1 if the expression is constant | 1297 ** Walk an expression tree. Return 1 if the expression is constant |
| 1163 ** and 0 if it involves variables or function calls. | 1298 ** and 0 if it involves variables or function calls. |
| 1164 ** | 1299 ** |
| 1165 ** For the purposes of this function, a double-quoted string (ex: "abc") | 1300 ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 1166 ** is considered a variable but a single-quoted string (ex: 'abc') is | 1301 ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 1167 ** a constant. | 1302 ** a constant. |
| 1168 */ | 1303 */ |
| 1169 int sqlite3ExprIsConstant(Expr *p){ | 1304 int sqlite3ExprIsConstant(Expr *p){ |
| 1170 return exprIsConst(p, 1); | 1305 return exprIsConst(p, 1); |
| 1171 } | 1306 } |
| 1172 | 1307 |
| 1173 /* | 1308 /* |
| 1174 ** Walk an expression tree. Return 1 if the expression is constant | 1309 ** Walk an expression tree. Return 1 if the expression is constant |
| 1175 ** that does no originate from the ON or USING clauses of a join. | 1310 ** that does no originate from the ON or USING clauses of a join. |
| 1176 ** Return 0 if it involves variables or function calls or terms from | 1311 ** Return 0 if it involves variables or function calls or terms from |
| 1177 ** an ON or USING clause. | 1312 ** an ON or USING clause. |
| 1178 */ | 1313 */ |
| 1179 int sqlite3ExprIsConstantNotJoin(Expr *p){ | 1314 int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 1180 return exprIsConst(p, 3); | 1315 return exprIsConst(p, 2); |
| 1181 } | 1316 } |
| 1182 | 1317 |
| 1183 /* | 1318 /* |
| 1184 ** Walk an expression tree. Return 1 if the expression is constant | 1319 ** Walk an expression tree. Return 1 if the expression is constant |
| 1185 ** or a function call with constant arguments. Return and 0 if there | 1320 ** or a function call with constant arguments. Return and 0 if there |
| 1186 ** are any variables. | 1321 ** are any variables. |
| 1187 ** | 1322 ** |
| 1188 ** For the purposes of this function, a double-quoted string (ex: "abc") | 1323 ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 1189 ** is considered a variable but a single-quoted string (ex: 'abc') is | 1324 ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 1190 ** a constant. | 1325 ** a constant. |
| 1191 */ | 1326 */ |
| 1192 int sqlite3ExprIsConstantOrFunction(Expr *p){ | 1327 int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 1193 return exprIsConst(p, 2); | 1328 assert( isInit==0 || isInit==1 ); |
| 1329 return exprIsConst(p, 3+isInit); |
| 1194 } | 1330 } |
| 1195 | 1331 |
| 1196 /* | 1332 /* |
| 1197 ** If the expression p codes a constant integer that is small enough | 1333 ** If the expression p codes a constant integer that is small enough |
| 1198 ** to fit in a 32-bit integer, return 1 and put the value of the integer | 1334 ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| 1199 ** in *pValue. If the expression is not an integer or if it is too big | 1335 ** in *pValue. If the expression is not an integer or if it is too big |
| 1200 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. | 1336 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. |
| 1201 */ | 1337 */ |
| 1202 int sqlite3ExprIsInteger(Expr *p, int *pValue){ | 1338 int sqlite3ExprIsInteger(Expr *p, int *pValue){ |
| 1203 int rc = 0; | 1339 int rc = 0; |
| 1204 | 1340 |
| 1205 /* If an expression is an integer literal that fits in a signed 32-bit | 1341 /* If an expression is an integer literal that fits in a signed 32-bit |
| 1206 ** integer, then the EP_IntValue flag will have already been set */ | 1342 ** integer, then the EP_IntValue flag will have already been set */ |
| 1207 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 | 1343 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 |
| 1208 || sqlite3GetInt32(p->u.zToken, &rc)==0 ); | 1344 || sqlite3GetInt32(p->u.zToken, &rc)==0 ); |
| 1209 | 1345 |
| 1210 if( p->flags & EP_IntValue ){ | 1346 if( p->flags & EP_IntValue ){ |
| 1211 *pValue = p->u.iValue; | 1347 *pValue = p->u.iValue; |
| 1212 return 1; | 1348 return 1; |
| 1213 } | 1349 } |
| 1214 switch( p->op ){ | 1350 switch( p->op ){ |
| 1215 case TK_UPLUS: { | 1351 case TK_UPLUS: { |
| 1216 rc = sqlite3ExprIsInteger(p->pLeft, pValue); | 1352 rc = sqlite3ExprIsInteger(p->pLeft, pValue); |
| 1217 break; | 1353 break; |
| 1218 } | 1354 } |
| 1219 case TK_UMINUS: { | 1355 case TK_UMINUS: { |
| 1220 int v; | 1356 int v; |
| 1221 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ | 1357 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ |
| 1358 assert( v!=(-2147483647-1) ); |
| 1222 *pValue = -v; | 1359 *pValue = -v; |
| 1223 rc = 1; | 1360 rc = 1; |
| 1224 } | 1361 } |
| 1225 break; | 1362 break; |
| 1226 } | 1363 } |
| 1227 default: break; | 1364 default: break; |
| 1228 } | 1365 } |
| 1229 return rc; | 1366 return rc; |
| 1230 } | 1367 } |
| 1231 | 1368 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1247 u8 op; | 1384 u8 op; |
| 1248 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } | 1385 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } |
| 1249 op = p->op; | 1386 op = p->op; |
| 1250 if( op==TK_REGISTER ) op = p->op2; | 1387 if( op==TK_REGISTER ) op = p->op2; |
| 1251 switch( op ){ | 1388 switch( op ){ |
| 1252 case TK_INTEGER: | 1389 case TK_INTEGER: |
| 1253 case TK_STRING: | 1390 case TK_STRING: |
| 1254 case TK_FLOAT: | 1391 case TK_FLOAT: |
| 1255 case TK_BLOB: | 1392 case TK_BLOB: |
| 1256 return 0; | 1393 return 0; |
| 1394 case TK_COLUMN: |
| 1395 assert( p->pTab!=0 ); |
| 1396 return ExprHasProperty(p, EP_CanBeNull) || |
| 1397 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); |
| 1257 default: | 1398 default: |
| 1258 return 1; | 1399 return 1; |
| 1259 } | 1400 } |
| 1260 } | 1401 } |
| 1261 | 1402 |
| 1262 /* | 1403 /* |
| 1263 ** Generate an OP_IsNull instruction that tests register iReg and jumps | |
| 1264 ** to location iDest if the value in iReg is NULL. The value in iReg | |
| 1265 ** was computed by pExpr. If we can look at pExpr at compile-time and | |
| 1266 ** determine that it can never generate a NULL, then the OP_IsNull operation | |
| 1267 ** can be omitted. | |
| 1268 */ | |
| 1269 void sqlite3ExprCodeIsNullJump( | |
| 1270 Vdbe *v, /* The VDBE under construction */ | |
| 1271 const Expr *pExpr, /* Only generate OP_IsNull if this expr can be NULL */ | |
| 1272 int iReg, /* Test the value in this register for NULL */ | |
| 1273 int iDest /* Jump here if the value is null */ | |
| 1274 ){ | |
| 1275 if( sqlite3ExprCanBeNull(pExpr) ){ | |
| 1276 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest); | |
| 1277 } | |
| 1278 } | |
| 1279 | |
| 1280 /* | |
| 1281 ** Return TRUE if the given expression is a constant which would be | 1404 ** Return TRUE if the given expression is a constant which would be |
| 1282 ** unchanged by OP_Affinity with the affinity given in the second | 1405 ** unchanged by OP_Affinity with the affinity given in the second |
| 1283 ** argument. | 1406 ** argument. |
| 1284 ** | 1407 ** |
| 1285 ** This routine is used to determine if the OP_Affinity operation | 1408 ** This routine is used to determine if the OP_Affinity operation |
| 1286 ** can be omitted. When in doubt return FALSE. A false negative | 1409 ** can be omitted. When in doubt return FALSE. A false negative |
| 1287 ** is harmless. A false positive, however, can result in the wrong | 1410 ** is harmless. A false positive, however, can result in the wrong |
| 1288 ** answer. | 1411 ** answer. |
| 1289 */ | 1412 */ |
| 1290 int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ | 1413 int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1364 assert( pTab->pSelect==0 ); /* FROM clause is not a view */ | 1487 assert( pTab->pSelect==0 ); /* FROM clause is not a view */ |
| 1365 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ | 1488 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ |
| 1366 pEList = p->pEList; | 1489 pEList = p->pEList; |
| 1367 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ | 1490 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ |
| 1368 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */ | 1491 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */ |
| 1369 return 1; | 1492 return 1; |
| 1370 } | 1493 } |
| 1371 #endif /* SQLITE_OMIT_SUBQUERY */ | 1494 #endif /* SQLITE_OMIT_SUBQUERY */ |
| 1372 | 1495 |
| 1373 /* | 1496 /* |
| 1497 ** Code an OP_Once instruction and allocate space for its flag. Return the |
| 1498 ** address of the new instruction. |
| 1499 */ |
| 1500 int sqlite3CodeOnce(Parse *pParse){ |
| 1501 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ |
| 1502 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); |
| 1503 } |
| 1504 |
| 1505 /* |
| 1506 ** Generate code that checks the left-most column of index table iCur to see if |
| 1507 ** it contains any NULL entries. Cause the register at regHasNull to be set |
| 1508 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull |
| 1509 ** to be set to NULL if iCur contains one or more NULL values. |
| 1510 */ |
| 1511 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ |
| 1512 int j1; |
| 1513 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); |
| 1514 j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); |
| 1515 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); |
| 1516 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 1517 VdbeComment((v, "first_entry_in(%d)", iCur)); |
| 1518 sqlite3VdbeJumpHere(v, j1); |
| 1519 } |
| 1520 |
| 1521 |
| 1522 #ifndef SQLITE_OMIT_SUBQUERY |
| 1523 /* |
| 1524 ** The argument is an IN operator with a list (not a subquery) on the |
| 1525 ** right-hand side. Return TRUE if that list is constant. |
| 1526 */ |
| 1527 static int sqlite3InRhsIsConstant(Expr *pIn){ |
| 1528 Expr *pLHS; |
| 1529 int res; |
| 1530 assert( !ExprHasProperty(pIn, EP_xIsSelect) ); |
| 1531 pLHS = pIn->pLeft; |
| 1532 pIn->pLeft = 0; |
| 1533 res = sqlite3ExprIsConstant(pIn); |
| 1534 pIn->pLeft = pLHS; |
| 1535 return res; |
| 1536 } |
| 1537 #endif |
| 1538 |
| 1539 /* |
| 1374 ** This function is used by the implementation of the IN (...) operator. | 1540 ** This function is used by the implementation of the IN (...) operator. |
| 1375 ** It's job is to find or create a b-tree structure that may be used | 1541 ** The pX parameter is the expression on the RHS of the IN operator, which |
| 1376 ** either to test for membership of the (...) set or to iterate through | 1542 ** might be either a list of expressions or a subquery. |
| 1377 ** its members, skipping duplicates. | |
| 1378 ** | 1543 ** |
| 1379 ** The index of the cursor opened on the b-tree (database table, database index | 1544 ** The job of this routine is to find or create a b-tree object that can |
| 1380 ** or ephermal table) is stored in pX->iTable before this function returns. | 1545 ** be used either to test for membership in the RHS set or to iterate through |
| 1546 ** all members of the RHS set, skipping duplicates. |
| 1547 ** |
| 1548 ** A cursor is opened on the b-tree object that is the RHS of the IN operator |
| 1549 ** and pX->iTable is set to the index of that cursor. |
| 1550 ** |
| 1381 ** The returned value of this function indicates the b-tree type, as follows: | 1551 ** The returned value of this function indicates the b-tree type, as follows: |
| 1382 ** | 1552 ** |
| 1383 ** IN_INDEX_ROWID - The cursor was opened on a database table. | 1553 ** IN_INDEX_ROWID - The cursor was opened on a database table. |
| 1384 ** IN_INDEX_INDEX - The cursor was opened on a database index. | 1554 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. |
| 1385 ** IN_INDEX_EPH - The cursor was opened on a specially created and | 1555 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. |
| 1386 ** populated epheremal table. | 1556 ** IN_INDEX_EPH - The cursor was opened on a specially created and |
| 1557 ** populated epheremal table. |
| 1558 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be |
| 1559 ** implemented as a sequence of comparisons. |
| 1387 ** | 1560 ** |
| 1388 ** An existing b-tree may only be used if the SELECT is of the simple | 1561 ** An existing b-tree might be used if the RHS expression pX is a simple |
| 1389 ** form: | 1562 ** subquery such as: |
| 1390 ** | 1563 ** |
| 1391 ** SELECT <column> FROM <table> | 1564 ** SELECT <column> FROM <table> |
| 1392 ** | 1565 ** |
| 1393 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate | 1566 ** If the RHS of the IN operator is a list or a more complex subquery, then |
| 1394 ** through the set members, skipping any duplicates. In this case an | 1567 ** an ephemeral table might need to be generated from the RHS and then |
| 1395 ** epheremal table must be used unless the selected <column> is guaranteed | 1568 ** pX->iTable made to point to the ephemeral table instead of an |
| 1569 ** existing table. |
| 1570 ** |
| 1571 ** The inFlags parameter must contain exactly one of the bits |
| 1572 ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains |
| 1573 ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a |
| 1574 ** fast membership test. When the IN_INDEX_LOOP bit is set, the |
| 1575 ** IN index will be used to loop over all values of the RHS of the |
| 1576 ** IN operator. |
| 1577 ** |
| 1578 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate |
| 1579 ** through the set members) then the b-tree must not contain duplicates. |
| 1580 ** An epheremal table must be used unless the selected <column> is guaranteed |
| 1396 ** to be unique - either because it is an INTEGER PRIMARY KEY or it | 1581 ** to be unique - either because it is an INTEGER PRIMARY KEY or it |
| 1397 ** has a UNIQUE constraint or UNIQUE index. | 1582 ** has a UNIQUE constraint or UNIQUE index. |
| 1398 ** | 1583 ** |
| 1399 ** If the prNotFound parameter is not 0, then the b-tree will be used | 1584 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used |
| 1400 ** for fast set membership tests. In this case an epheremal table must | 1585 ** for fast set membership tests) then an epheremal table must |
| 1401 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can | 1586 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can |
| 1402 ** be found with <column> as its left-most column. | 1587 ** be found with <column> as its left-most column. |
| 1403 ** | 1588 ** |
| 1589 ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and |
| 1590 ** if the RHS of the IN operator is a list (not a subquery) then this |
| 1591 ** routine might decide that creating an ephemeral b-tree for membership |
| 1592 ** testing is too expensive and return IN_INDEX_NOOP. In that case, the |
| 1593 ** calling routine should implement the IN operator using a sequence |
| 1594 ** of Eq or Ne comparison operations. |
| 1595 ** |
| 1404 ** When the b-tree is being used for membership tests, the calling function | 1596 ** When the b-tree is being used for membership tests, the calling function |
| 1405 ** needs to know whether or not the structure contains an SQL NULL | 1597 ** might need to know whether or not the RHS side of the IN operator |
| 1406 ** value in order to correctly evaluate expressions like "X IN (Y, Z)". | 1598 ** contains a NULL. If prRhsHasNull is not a NULL pointer and |
| 1407 ** If there is any chance that the (...) might contain a NULL value at | 1599 ** if there is any chance that the (...) might contain a NULL value at |
| 1408 ** runtime, then a register is allocated and the register number written | 1600 ** runtime, then a register is allocated and the register number written |
| 1409 ** to *prNotFound. If there is no chance that the (...) contains a | 1601 ** to *prRhsHasNull. If there is no chance that the (...) contains a |
| 1410 ** NULL value, then *prNotFound is left unchanged. | 1602 ** NULL value, then *prRhsHasNull is left unchanged. |
| 1411 ** | 1603 ** |
| 1412 ** If a register is allocated and its location stored in *prNotFound, then | 1604 ** If a register is allocated and its location stored in *prRhsHasNull, then |
| 1413 ** its initial value is NULL. If the (...) does not remain constant | 1605 ** the value in that register will be NULL if the b-tree contains one or more |
| 1414 ** for the duration of the query (i.e. the SELECT within the (...) | 1606 ** NULL values, and it will be some non-NULL value if the b-tree contains no |
| 1415 ** is a correlated subquery) then the value of the allocated register is | 1607 ** NULL values. |
| 1416 ** reset to NULL each time the subquery is rerun. This allows the | |
| 1417 ** caller to use vdbe code equivalent to the following: | |
| 1418 ** | |
| 1419 ** if( register==NULL ){ | |
| 1420 ** has_null = <test if data structure contains null> | |
| 1421 ** register = 1 | |
| 1422 ** } | |
| 1423 ** | |
| 1424 ** in order to avoid running the <test if data structure contains null> | |
| 1425 ** test more often than is necessary. | |
| 1426 */ | 1608 */ |
| 1427 #ifndef SQLITE_OMIT_SUBQUERY | 1609 #ifndef SQLITE_OMIT_SUBQUERY |
| 1428 int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){ | 1610 int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){ |
| 1429 Select *p; /* SELECT to the right of IN operator */ | 1611 Select *p; /* SELECT to the right of IN operator */ |
| 1430 int eType = 0; /* Type of RHS table. IN_INDEX_* */ | 1612 int eType = 0; /* Type of RHS table. IN_INDEX_* */ |
| 1431 int iTab = pParse->nTab++; /* Cursor of the RHS table */ | 1613 int iTab = pParse->nTab++; /* Cursor of the RHS table */ |
| 1432 int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */ | 1614 int mustBeUnique; /* True if RHS must be unique */ |
| 1615 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ |
| 1433 | 1616 |
| 1434 assert( pX->op==TK_IN ); | 1617 assert( pX->op==TK_IN ); |
| 1618 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; |
| 1435 | 1619 |
| 1436 /* Check to see if an existing table or index can be used to | 1620 /* Check to see if an existing table or index can be used to |
| 1437 ** satisfy the query. This is preferable to generating a new | 1621 ** satisfy the query. This is preferable to generating a new |
| 1438 ** ephemeral table. | 1622 ** ephemeral table. |
| 1439 */ | 1623 */ |
| 1440 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); | 1624 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); |
| 1441 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ | 1625 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ |
| 1442 sqlite3 *db = pParse->db; /* Database connection */ | 1626 sqlite3 *db = pParse->db; /* Database connection */ |
| 1443 Expr *pExpr = p->pEList->a[0].pExpr; /* Expression <column> */ | 1627 Table *pTab; /* Table <table>. */ |
| 1444 int iCol = pExpr->iColumn; /* Index of column <column> */ | 1628 Expr *pExpr; /* Expression <column> */ |
| 1445 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ | 1629 i16 iCol; /* Index of column <column> */ |
| 1446 Table *pTab = p->pSrc->a[0].pTab; /* Table <table>. */ | 1630 i16 iDb; /* Database idx for pTab */ |
| 1447 int iDb; /* Database idx for pTab */ | 1631 |
| 1632 assert( p ); /* Because of isCandidateForInOpt(p) */ |
| 1633 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 1634 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 1635 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 1636 pTab = p->pSrc->a[0].pTab; |
| 1637 pExpr = p->pEList->a[0].pExpr; |
| 1638 iCol = (i16)pExpr->iColumn; |
| 1448 | 1639 |
| 1449 /* Code an OP_VerifyCookie and OP_TableLock for <table>. */ | 1640 /* Code an OP_Transaction and OP_TableLock for <table>. */ |
| 1450 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | 1641 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 1451 sqlite3CodeVerifySchema(pParse, iDb); | 1642 sqlite3CodeVerifySchema(pParse, iDb); |
| 1452 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); | 1643 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 1453 | 1644 |
| 1454 /* This function is only called from two places. In both cases the vdbe | 1645 /* This function is only called from two places. In both cases the vdbe |
| 1455 ** has already been allocated. So assume sqlite3GetVdbe() is always | 1646 ** has already been allocated. So assume sqlite3GetVdbe() is always |
| 1456 ** successful here. | 1647 ** successful here. |
| 1457 */ | 1648 */ |
| 1458 assert(v); | 1649 assert(v); |
| 1459 if( iCol<0 ){ | 1650 if( iCol<0 ){ |
| 1460 int iMem = ++pParse->nMem; | 1651 int iAddr = sqlite3CodeOnce(pParse); |
| 1461 int iAddr; | 1652 VdbeCoverage(v); |
| 1462 | |
| 1463 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); | |
| 1464 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); | |
| 1465 | 1653 |
| 1466 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); | 1654 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 1467 eType = IN_INDEX_ROWID; | 1655 eType = IN_INDEX_ROWID; |
| 1468 | 1656 |
| 1469 sqlite3VdbeJumpHere(v, iAddr); | 1657 sqlite3VdbeJumpHere(v, iAddr); |
| 1470 }else{ | 1658 }else{ |
| 1471 Index *pIdx; /* Iterator variable */ | 1659 Index *pIdx; /* Iterator variable */ |
| 1472 | 1660 |
| 1473 /* The collation sequence used by the comparison. If an index is to | 1661 /* The collation sequence used by the comparison. If an index is to |
| 1474 ** be used in place of a temp-table, it must be ordered according | 1662 ** be used in place of a temp-table, it must be ordered according |
| 1475 ** to this collation sequence. */ | 1663 ** to this collation sequence. */ |
| 1476 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); | 1664 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); |
| 1477 | 1665 |
| 1478 /* Check that the affinity that will be used to perform the | 1666 /* Check that the affinity that will be used to perform the |
| 1479 ** comparison is the same as the affinity of the column. If | 1667 ** comparison is the same as the affinity of the column. If |
| 1480 ** it is not, it is not possible to use any index. | 1668 ** it is not, it is not possible to use any index. |
| 1481 */ | 1669 */ |
| 1482 char aff = comparisonAffinity(pX); | 1670 int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity); |
| 1483 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); | |
| 1484 | 1671 |
| 1485 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ | 1672 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ |
| 1486 if( (pIdx->aiColumn[0]==iCol) | 1673 if( (pIdx->aiColumn[0]==iCol) |
| 1487 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq | 1674 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq |
| 1488 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) | 1675 && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx))) |
| 1489 ){ | 1676 ){ |
| 1490 int iMem = ++pParse->nMem; | 1677 int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); |
| 1491 int iAddr; | 1678 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); |
| 1492 char *pKey; | 1679 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 1493 | |
| 1494 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); | |
| 1495 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); | |
| 1496 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); | |
| 1497 | |
| 1498 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, | |
| 1499 pKey,P4_KEYINFO_HANDOFF); | |
| 1500 VdbeComment((v, "%s", pIdx->zName)); | 1680 VdbeComment((v, "%s", pIdx->zName)); |
| 1501 eType = IN_INDEX_INDEX; | 1681 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); |
| 1682 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; |
| 1502 | 1683 |
| 1684 if( prRhsHasNull && !pTab->aCol[iCol].notNull ){ |
| 1685 *prRhsHasNull = ++pParse->nMem; |
| 1686 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); |
| 1687 } |
| 1503 sqlite3VdbeJumpHere(v, iAddr); | 1688 sqlite3VdbeJumpHere(v, iAddr); |
| 1504 if( prNotFound && !pTab->aCol[iCol].notNull ){ | |
| 1505 *prNotFound = ++pParse->nMem; | |
| 1506 } | |
| 1507 } | 1689 } |
| 1508 } | 1690 } |
| 1509 } | 1691 } |
| 1510 } | 1692 } |
| 1511 | 1693 |
| 1694 /* If no preexisting index is available for the IN clause |
| 1695 ** and IN_INDEX_NOOP is an allowed reply |
| 1696 ** and the RHS of the IN operator is a list, not a subquery |
| 1697 ** and the RHS is not contant or has two or fewer terms, |
| 1698 ** then it is not worth creating an ephemeral table to evaluate |
| 1699 ** the IN operator so return IN_INDEX_NOOP. |
| 1700 */ |
| 1701 if( eType==0 |
| 1702 && (inFlags & IN_INDEX_NOOP_OK) |
| 1703 && !ExprHasProperty(pX, EP_xIsSelect) |
| 1704 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) |
| 1705 ){ |
| 1706 eType = IN_INDEX_NOOP; |
| 1707 } |
| 1708 |
| 1709 |
| 1512 if( eType==0 ){ | 1710 if( eType==0 ){ |
| 1513 /* Could not found an existing table or index to use as the RHS b-tree. | 1711 /* Could not find an existing table or index to use as the RHS b-tree. |
| 1514 ** We will have to generate an ephemeral table to do the job. | 1712 ** We will have to generate an ephemeral table to do the job. |
| 1515 */ | 1713 */ |
| 1516 double savedNQueryLoop = pParse->nQueryLoop; | 1714 u32 savedNQueryLoop = pParse->nQueryLoop; |
| 1517 int rMayHaveNull = 0; | 1715 int rMayHaveNull = 0; |
| 1518 eType = IN_INDEX_EPH; | 1716 eType = IN_INDEX_EPH; |
| 1519 if( prNotFound ){ | 1717 if( inFlags & IN_INDEX_LOOP ){ |
| 1520 *prNotFound = rMayHaveNull = ++pParse->nMem; | 1718 pParse->nQueryLoop = 0; |
| 1521 }else{ | 1719 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ |
| 1522 testcase( pParse->nQueryLoop>(double)1 ); | |
| 1523 pParse->nQueryLoop = (double)1; | |
| 1524 if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){ | |
| 1525 eType = IN_INDEX_ROWID; | 1720 eType = IN_INDEX_ROWID; |
| 1526 } | 1721 } |
| 1722 }else if( prRhsHasNull ){ |
| 1723 *prRhsHasNull = rMayHaveNull = ++pParse->nMem; |
| 1527 } | 1724 } |
| 1528 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); | 1725 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); |
| 1529 pParse->nQueryLoop = savedNQueryLoop; | 1726 pParse->nQueryLoop = savedNQueryLoop; |
| 1530 }else{ | 1727 }else{ |
| 1531 pX->iTable = iTab; | 1728 pX->iTable = iTab; |
| 1532 } | 1729 } |
| 1533 return eType; | 1730 return eType; |
| 1534 } | 1731 } |
| 1535 #endif | 1732 #endif |
| 1536 | 1733 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1547 ** operator or subquery. | 1744 ** operator or subquery. |
| 1548 ** | 1745 ** |
| 1549 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed | 1746 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed |
| 1550 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference | 1747 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference |
| 1551 ** to some integer key column of a table B-Tree. In this case, use an | 1748 ** to some integer key column of a table B-Tree. In this case, use an |
| 1552 ** intkey B-Tree to store the set of IN(...) values instead of the usual | 1749 ** intkey B-Tree to store the set of IN(...) values instead of the usual |
| 1553 ** (slower) variable length keys B-Tree. | 1750 ** (slower) variable length keys B-Tree. |
| 1554 ** | 1751 ** |
| 1555 ** If rMayHaveNull is non-zero, that means that the operation is an IN | 1752 ** If rMayHaveNull is non-zero, that means that the operation is an IN |
| 1556 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. | 1753 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. |
| 1557 ** Furthermore, the IN is in a WHERE clause and that we really want | 1754 ** All this routine does is initialize the register given by rMayHaveNull |
| 1558 ** to iterate over the RHS of the IN operator in order to quickly locate | 1755 ** to NULL. Calling routines will take care of changing this register |
| 1559 ** all corresponding LHS elements. All this routine does is initialize | 1756 ** value to non-NULL if the RHS is NULL-free. |
| 1560 ** the register given by rMayHaveNull to NULL. Calling routines will take | |
| 1561 ** care of changing this register value to non-NULL if the RHS is NULL-free. | |
| 1562 ** | |
| 1563 ** If rMayHaveNull is zero, that means that the subquery is being used | |
| 1564 ** for membership testing only. There is no need to initialize any | |
| 1565 ** registers to indicate the presense or absence of NULLs on the RHS. | |
| 1566 ** | 1757 ** |
| 1567 ** For a SELECT or EXISTS operator, return the register that holds the | 1758 ** For a SELECT or EXISTS operator, return the register that holds the |
| 1568 ** result. For IN operators or if an error occurs, the return value is 0. | 1759 ** result. For IN operators or if an error occurs, the return value is 0. |
| 1569 */ | 1760 */ |
| 1570 #ifndef SQLITE_OMIT_SUBQUERY | 1761 #ifndef SQLITE_OMIT_SUBQUERY |
| 1571 int sqlite3CodeSubselect( | 1762 int sqlite3CodeSubselect( |
| 1572 Parse *pParse, /* Parsing context */ | 1763 Parse *pParse, /* Parsing context */ |
| 1573 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ | 1764 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ |
| 1574 int rMayHaveNull, /* Register that records whether NULLs exist in RHS */ | 1765 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ |
| 1575 int isRowid /* If true, LHS of IN operator is a rowid */ | 1766 int isRowid /* If true, LHS of IN operator is a rowid */ |
| 1576 ){ | 1767 ){ |
| 1577 int testAddr = 0; /* One-time test address */ | 1768 int jmpIfDynamic = -1; /* One-time test address */ |
| 1578 int rReg = 0; /* Register storing resulting */ | 1769 int rReg = 0; /* Register storing resulting */ |
| 1579 Vdbe *v = sqlite3GetVdbe(pParse); | 1770 Vdbe *v = sqlite3GetVdbe(pParse); |
| 1580 if( NEVER(v==0) ) return 0; | 1771 if( NEVER(v==0) ) return 0; |
| 1581 sqlite3ExprCachePush(pParse); | 1772 sqlite3ExprCachePush(pParse); |
| 1582 | 1773 |
| 1583 /* This code must be run in its entirety every time it is encountered | 1774 /* This code must be run in its entirety every time it is encountered |
| 1584 ** if any of the following is true: | 1775 ** if any of the following is true: |
| 1585 ** | 1776 ** |
| 1586 ** * The right-hand side is a correlated subquery | 1777 ** * The right-hand side is a correlated subquery |
| 1587 ** * The right-hand side is an expression list containing variables | 1778 ** * The right-hand side is an expression list containing variables |
| 1588 ** * We are inside a trigger | 1779 ** * We are inside a trigger |
| 1589 ** | 1780 ** |
| 1590 ** If all of the above are false, then we can run this code just once | 1781 ** If all of the above are false, then we can run this code just once |
| 1591 ** save the results, and reuse the same result on subsequent invocations. | 1782 ** save the results, and reuse the same result on subsequent invocations. |
| 1592 */ | 1783 */ |
| 1593 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){ | 1784 if( !ExprHasProperty(pExpr, EP_VarSelect) ){ |
| 1594 int mem = ++pParse->nMem; | 1785 jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); |
| 1595 sqlite3VdbeAddOp1(v, OP_If, mem); | |
| 1596 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); | |
| 1597 assert( testAddr>0 || pParse->db->mallocFailed ); | |
| 1598 } | 1786 } |
| 1599 | 1787 |
| 1600 #ifndef SQLITE_OMIT_EXPLAIN | 1788 #ifndef SQLITE_OMIT_EXPLAIN |
| 1601 if( pParse->explain==2 ){ | 1789 if( pParse->explain==2 ){ |
| 1602 char *zMsg = sqlite3MPrintf( | 1790 char *zMsg = sqlite3MPrintf( |
| 1603 pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr?"":"CORRELATED ", | 1791 pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED "
, |
| 1604 pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId | 1792 pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId |
| 1605 ); | 1793 ); |
| 1606 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); | 1794 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); |
| 1607 } | 1795 } |
| 1608 #endif | 1796 #endif |
| 1609 | 1797 |
| 1610 switch( pExpr->op ){ | 1798 switch( pExpr->op ){ |
| 1611 case TK_IN: { | 1799 case TK_IN: { |
| 1612 char affinity; /* Affinity of the LHS of the IN */ | 1800 char affinity; /* Affinity of the LHS of the IN */ |
| 1613 KeyInfo keyInfo; /* Keyinfo for the generated table */ | |
| 1614 int addr; /* Address of OP_OpenEphemeral instruction */ | 1801 int addr; /* Address of OP_OpenEphemeral instruction */ |
| 1615 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ | 1802 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ |
| 1616 | 1803 KeyInfo *pKeyInfo = 0; /* Key information */ |
| 1617 if( rMayHaveNull ){ | |
| 1618 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); | |
| 1619 } | |
| 1620 | 1804 |
| 1621 affinity = sqlite3ExprAffinity(pLeft); | 1805 affinity = sqlite3ExprAffinity(pLeft); |
| 1622 | 1806 |
| 1623 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' | 1807 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' |
| 1624 ** expression it is handled the same way. An ephemeral table is | 1808 ** expression it is handled the same way. An ephemeral table is |
| 1625 ** filled with single-field index keys representing the results | 1809 ** filled with single-field index keys representing the results |
| 1626 ** from the SELECT or the <exprlist>. | 1810 ** from the SELECT or the <exprlist>. |
| 1627 ** | 1811 ** |
| 1628 ** If the 'x' expression is a column value, or the SELECT... | 1812 ** If the 'x' expression is a column value, or the SELECT... |
| 1629 ** statement returns a column value, then the affinity of that | 1813 ** statement returns a column value, then the affinity of that |
| 1630 ** column is used to build the index keys. If both 'x' and the | 1814 ** column is used to build the index keys. If both 'x' and the |
| 1631 ** SELECT... statement are columns, then numeric affinity is used | 1815 ** SELECT... statement are columns, then numeric affinity is used |
| 1632 ** if either column has NUMERIC or INTEGER affinity. If neither | 1816 ** if either column has NUMERIC or INTEGER affinity. If neither |
| 1633 ** 'x' nor the SELECT... statement are columns, then numeric affinity | 1817 ** 'x' nor the SELECT... statement are columns, then numeric affinity |
| 1634 ** is used. | 1818 ** is used. |
| 1635 */ | 1819 */ |
| 1636 pExpr->iTable = pParse->nTab++; | 1820 pExpr->iTable = pParse->nTab++; |
| 1637 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); | 1821 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); |
| 1638 if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | 1822 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1); |
| 1639 memset(&keyInfo, 0, sizeof(keyInfo)); | |
| 1640 keyInfo.nField = 1; | |
| 1641 | 1823 |
| 1642 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ | 1824 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1643 /* Case 1: expr IN (SELECT ...) | 1825 /* Case 1: expr IN (SELECT ...) |
| 1644 ** | 1826 ** |
| 1645 ** Generate code to write the results of the select into the temporary | 1827 ** Generate code to write the results of the select into the temporary |
| 1646 ** table allocated and opened above. | 1828 ** table allocated and opened above. |
| 1647 */ | 1829 */ |
| 1830 Select *pSelect = pExpr->x.pSelect; |
| 1648 SelectDest dest; | 1831 SelectDest dest; |
| 1649 ExprList *pEList; | 1832 ExprList *pEList; |
| 1650 | 1833 |
| 1651 assert( !isRowid ); | 1834 assert( !isRowid ); |
| 1652 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); | 1835 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); |
| 1653 dest.affinity = (u8)affinity; | 1836 dest.affSdst = (u8)affinity; |
| 1654 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); | 1837 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); |
| 1655 pExpr->x.pSelect->iLimit = 0; | 1838 pSelect->iLimit = 0; |
| 1656 if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){ | 1839 testcase( pSelect->selFlags & SF_Distinct ); |
| 1840 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ |
| 1841 if( sqlite3Select(pParse, pSelect, &dest) ){ |
| 1842 sqlite3KeyInfoUnref(pKeyInfo); |
| 1657 return 0; | 1843 return 0; |
| 1658 } | 1844 } |
| 1659 pEList = pExpr->x.pSelect->pEList; | 1845 pEList = pSelect->pEList; |
| 1660 if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ | 1846 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ |
| 1661 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, | 1847 assert( pEList!=0 ); |
| 1662 pEList->a[0].pExpr); | 1848 assert( pEList->nExpr>0 ); |
| 1663 } | 1849 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 1850 pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, |
| 1851 pEList->a[0].pExpr); |
| 1664 }else if( ALWAYS(pExpr->x.pList!=0) ){ | 1852 }else if( ALWAYS(pExpr->x.pList!=0) ){ |
| 1665 /* Case 2: expr IN (exprlist) | 1853 /* Case 2: expr IN (exprlist) |
| 1666 ** | 1854 ** |
| 1667 ** For each expression, build an index key from the evaluation and | 1855 ** For each expression, build an index key from the evaluation and |
| 1668 ** store it in the temporary table. If <expr> is a column, then use | 1856 ** store it in the temporary table. If <expr> is a column, then use |
| 1669 ** that columns affinity when building index keys. If <expr> is not | 1857 ** that columns affinity when building index keys. If <expr> is not |
| 1670 ** a column, use numeric affinity. | 1858 ** a column, use numeric affinity. |
| 1671 */ | 1859 */ |
| 1672 int i; | 1860 int i; |
| 1673 ExprList *pList = pExpr->x.pList; | 1861 ExprList *pList = pExpr->x.pList; |
| 1674 struct ExprList_item *pItem; | 1862 struct ExprList_item *pItem; |
| 1675 int r1, r2, r3; | 1863 int r1, r2, r3; |
| 1676 | 1864 |
| 1677 if( !affinity ){ | 1865 if( !affinity ){ |
| 1678 affinity = SQLITE_AFF_NONE; | 1866 affinity = SQLITE_AFF_NONE; |
| 1679 } | 1867 } |
| 1680 keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); | 1868 if( pKeyInfo ){ |
| 1869 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 1870 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
| 1871 } |
| 1681 | 1872 |
| 1682 /* Loop through each expression in <exprlist>. */ | 1873 /* Loop through each expression in <exprlist>. */ |
| 1683 r1 = sqlite3GetTempReg(pParse); | 1874 r1 = sqlite3GetTempReg(pParse); |
| 1684 r2 = sqlite3GetTempReg(pParse); | 1875 r2 = sqlite3GetTempReg(pParse); |
| 1685 sqlite3VdbeAddOp2(v, OP_Null, 0, r2); | 1876 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); |
| 1686 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ | 1877 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ |
| 1687 Expr *pE2 = pItem->pExpr; | 1878 Expr *pE2 = pItem->pExpr; |
| 1688 int iValToIns; | 1879 int iValToIns; |
| 1689 | 1880 |
| 1690 /* If the expression is not constant then we will need to | 1881 /* If the expression is not constant then we will need to |
| 1691 ** disable the test that was generated above that makes sure | 1882 ** disable the test that was generated above that makes sure |
| 1692 ** this code only executes once. Because for a non-constant | 1883 ** this code only executes once. Because for a non-constant |
| 1693 ** expression we need to rerun this code each time. | 1884 ** expression we need to rerun this code each time. |
| 1694 */ | 1885 */ |
| 1695 if( testAddr && !sqlite3ExprIsConstant(pE2) ){ | 1886 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ |
| 1696 sqlite3VdbeChangeToNoop(v, testAddr-1, 2); | 1887 sqlite3VdbeChangeToNoop(v, jmpIfDynamic); |
| 1697 testAddr = 0; | 1888 jmpIfDynamic = -1; |
| 1698 } | 1889 } |
| 1699 | 1890 |
| 1700 /* Evaluate the expression and insert it into the temp table */ | 1891 /* Evaluate the expression and insert it into the temp table */ |
| 1701 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ | 1892 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ |
| 1702 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); | 1893 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); |
| 1703 }else{ | 1894 }else{ |
| 1704 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); | 1895 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); |
| 1705 if( isRowid ){ | 1896 if( isRowid ){ |
| 1706 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, | 1897 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, |
| 1707 sqlite3VdbeCurrentAddr(v)+2); | 1898 sqlite3VdbeCurrentAddr(v)+2); |
| 1899 VdbeCoverage(v); |
| 1708 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); | 1900 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); |
| 1709 }else{ | 1901 }else{ |
| 1710 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); | 1902 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); |
| 1711 sqlite3ExprCacheAffinityChange(pParse, r3, 1); | 1903 sqlite3ExprCacheAffinityChange(pParse, r3, 1); |
| 1712 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); | 1904 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); |
| 1713 } | 1905 } |
| 1714 } | 1906 } |
| 1715 } | 1907 } |
| 1716 sqlite3ReleaseTempReg(pParse, r1); | 1908 sqlite3ReleaseTempReg(pParse, r1); |
| 1717 sqlite3ReleaseTempReg(pParse, r2); | 1909 sqlite3ReleaseTempReg(pParse, r2); |
| 1718 } | 1910 } |
| 1719 if( !isRowid ){ | 1911 if( pKeyInfo ){ |
| 1720 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); | 1912 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); |
| 1721 } | 1913 } |
| 1722 break; | 1914 break; |
| 1723 } | 1915 } |
| 1724 | 1916 |
| 1725 case TK_EXISTS: | 1917 case TK_EXISTS: |
| 1726 case TK_SELECT: | 1918 case TK_SELECT: |
| 1727 default: { | 1919 default: { |
| 1728 /* If this has to be a scalar SELECT. Generate code to put the | 1920 /* If this has to be a scalar SELECT. Generate code to put the |
| 1729 ** value of this select in a memory cell and record the number | 1921 ** value of this select in a memory cell and record the number |
| 1730 ** of the memory cell in iColumn. If this is an EXISTS, write | 1922 ** of the memory cell in iColumn. If this is an EXISTS, write |
| 1731 ** an integer 0 (not exists) or 1 (exists) into a memory cell | 1923 ** an integer 0 (not exists) or 1 (exists) into a memory cell |
| 1732 ** and record that memory cell in iColumn. | 1924 ** and record that memory cell in iColumn. |
| 1733 */ | 1925 */ |
| 1734 Select *pSel; /* SELECT statement to encode */ | 1926 Select *pSel; /* SELECT statement to encode */ |
| 1735 SelectDest dest; /* How to deal with SELECt result */ | 1927 SelectDest dest; /* How to deal with SELECt result */ |
| 1736 | 1928 |
| 1737 testcase( pExpr->op==TK_EXISTS ); | 1929 testcase( pExpr->op==TK_EXISTS ); |
| 1738 testcase( pExpr->op==TK_SELECT ); | 1930 testcase( pExpr->op==TK_SELECT ); |
| 1739 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); | 1931 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); |
| 1740 | 1932 |
| 1741 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); | 1933 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 1742 pSel = pExpr->x.pSelect; | 1934 pSel = pExpr->x.pSelect; |
| 1743 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); | 1935 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); |
| 1744 if( pExpr->op==TK_SELECT ){ | 1936 if( pExpr->op==TK_SELECT ){ |
| 1745 dest.eDest = SRT_Mem; | 1937 dest.eDest = SRT_Mem; |
| 1746 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); | 1938 dest.iSdst = dest.iSDParm; |
| 1939 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm); |
| 1747 VdbeComment((v, "Init subquery result")); | 1940 VdbeComment((v, "Init subquery result")); |
| 1748 }else{ | 1941 }else{ |
| 1749 dest.eDest = SRT_Exists; | 1942 dest.eDest = SRT_Exists; |
| 1750 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); | 1943 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); |
| 1751 VdbeComment((v, "Init EXISTS result")); | 1944 VdbeComment((v, "Init EXISTS result")); |
| 1752 } | 1945 } |
| 1753 sqlite3ExprDelete(pParse->db, pSel->pLimit); | 1946 sqlite3ExprDelete(pParse->db, pSel->pLimit); |
| 1754 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, | 1947 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, |
| 1755 &sqlite3IntTokens[1]); | 1948 &sqlite3IntTokens[1]); |
| 1756 pSel->iLimit = 0; | 1949 pSel->iLimit = 0; |
| 1757 if( sqlite3Select(pParse, pSel, &dest) ){ | 1950 if( sqlite3Select(pParse, pSel, &dest) ){ |
| 1758 return 0; | 1951 return 0; |
| 1759 } | 1952 } |
| 1760 rReg = dest.iParm; | 1953 rReg = dest.iSDParm; |
| 1761 ExprSetIrreducible(pExpr); | 1954 ExprSetVVAProperty(pExpr, EP_NoReduce); |
| 1762 break; | 1955 break; |
| 1763 } | 1956 } |
| 1764 } | 1957 } |
| 1765 | 1958 |
| 1766 if( testAddr ){ | 1959 if( rHasNullFlag ){ |
| 1767 sqlite3VdbeJumpHere(v, testAddr-1); | 1960 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); |
| 1768 } | 1961 } |
| 1769 sqlite3ExprCachePop(pParse, 1); | 1962 |
| 1963 if( jmpIfDynamic>=0 ){ |
| 1964 sqlite3VdbeJumpHere(v, jmpIfDynamic); |
| 1965 } |
| 1966 sqlite3ExprCachePop(pParse); |
| 1770 | 1967 |
| 1771 return rReg; | 1968 return rReg; |
| 1772 } | 1969 } |
| 1773 #endif /* SQLITE_OMIT_SUBQUERY */ | 1970 #endif /* SQLITE_OMIT_SUBQUERY */ |
| 1774 | 1971 |
| 1775 #ifndef SQLITE_OMIT_SUBQUERY | 1972 #ifndef SQLITE_OMIT_SUBQUERY |
| 1776 /* | 1973 /* |
| 1777 ** Generate code for an IN expression. | 1974 ** Generate code for an IN expression. |
| 1778 ** | 1975 ** |
| 1779 ** x IN (SELECT ...) | 1976 ** x IN (SELECT ...) |
| 1780 ** x IN (value, value, ...) | 1977 ** x IN (value, value, ...) |
| 1781 ** | 1978 ** |
| 1782 ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) | 1979 ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) |
| 1783 ** is an array of zero or more values. The expression is true if the LHS is | 1980 ** is an array of zero or more values. The expression is true if the LHS is |
| 1784 ** contained within the RHS. The value of the expression is unknown (NULL) | 1981 ** contained within the RHS. The value of the expression is unknown (NULL) |
| 1785 ** if the LHS is NULL or if the LHS is not contained within the RHS and the | 1982 ** if the LHS is NULL or if the LHS is not contained within the RHS and the |
| 1786 ** RHS contains one or more NULL values. | 1983 ** RHS contains one or more NULL values. |
| 1787 ** | 1984 ** |
| 1788 ** This routine generates code will jump to destIfFalse if the LHS is not | 1985 ** This routine generates code that jumps to destIfFalse if the LHS is not |
| 1789 ** contained within the RHS. If due to NULLs we cannot determine if the LHS | 1986 ** contained within the RHS. If due to NULLs we cannot determine if the LHS |
| 1790 ** is contained in the RHS then jump to destIfNull. If the LHS is contained | 1987 ** is contained in the RHS then jump to destIfNull. If the LHS is contained |
| 1791 ** within the RHS then fall through. | 1988 ** within the RHS then fall through. |
| 1792 */ | 1989 */ |
| 1793 static void sqlite3ExprCodeIN( | 1990 static void sqlite3ExprCodeIN( |
| 1794 Parse *pParse, /* Parsing and code generating context */ | 1991 Parse *pParse, /* Parsing and code generating context */ |
| 1795 Expr *pExpr, /* The IN expression */ | 1992 Expr *pExpr, /* The IN expression */ |
| 1796 int destIfFalse, /* Jump here if LHS is not contained in the RHS */ | 1993 int destIfFalse, /* Jump here if LHS is not contained in the RHS */ |
| 1797 int destIfNull /* Jump here if the results are unknown due to NULLs */ | 1994 int destIfNull /* Jump here if the results are unknown due to NULLs */ |
| 1798 ){ | 1995 ){ |
| 1799 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ | 1996 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ |
| 1800 char affinity; /* Comparison affinity to use */ | 1997 char affinity; /* Comparison affinity to use */ |
| 1801 int eType; /* Type of the RHS */ | 1998 int eType; /* Type of the RHS */ |
| 1802 int r1; /* Temporary use register */ | 1999 int r1; /* Temporary use register */ |
| 1803 Vdbe *v; /* Statement under construction */ | 2000 Vdbe *v; /* Statement under construction */ |
| 1804 | 2001 |
| 1805 /* Compute the RHS. After this step, the table with cursor | 2002 /* Compute the RHS. After this step, the table with cursor |
| 1806 ** pExpr->iTable will contains the values that make up the RHS. | 2003 ** pExpr->iTable will contains the values that make up the RHS. |
| 1807 */ | 2004 */ |
| 1808 v = pParse->pVdbe; | 2005 v = pParse->pVdbe; |
| 1809 assert( v!=0 ); /* OOM detected prior to this routine */ | 2006 assert( v!=0 ); /* OOM detected prior to this routine */ |
| 1810 VdbeNoopComment((v, "begin IN expr")); | 2007 VdbeNoopComment((v, "begin IN expr")); |
| 1811 eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull); | 2008 eType = sqlite3FindInIndex(pParse, pExpr, |
| 2009 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, |
| 2010 destIfFalse==destIfNull ? 0 : &rRhsHasNull); |
| 1812 | 2011 |
| 1813 /* Figure out the affinity to use to create a key from the results | 2012 /* Figure out the affinity to use to create a key from the results |
| 1814 ** of the expression. affinityStr stores a static string suitable for | 2013 ** of the expression. affinityStr stores a static string suitable for |
| 1815 ** P4 of OP_MakeRecord. | 2014 ** P4 of OP_MakeRecord. |
| 1816 */ | 2015 */ |
| 1817 affinity = comparisonAffinity(pExpr); | 2016 affinity = comparisonAffinity(pExpr); |
| 1818 | 2017 |
| 1819 /* Code the LHS, the <expr> from "<expr> IN (...)". | 2018 /* Code the LHS, the <expr> from "<expr> IN (...)". |
| 1820 */ | 2019 */ |
| 1821 sqlite3ExprCachePush(pParse); | 2020 sqlite3ExprCachePush(pParse); |
| 1822 r1 = sqlite3GetTempReg(pParse); | 2021 r1 = sqlite3GetTempReg(pParse); |
| 1823 sqlite3ExprCode(pParse, pExpr->pLeft, r1); | 2022 sqlite3ExprCode(pParse, pExpr->pLeft, r1); |
| 1824 | 2023 |
| 1825 /* If the LHS is NULL, then the result is either false or NULL depending | 2024 /* If sqlite3FindInIndex() did not find or create an index that is |
| 1826 ** on whether the RHS is empty or not, respectively. | 2025 ** suitable for evaluating the IN operator, then evaluate using a |
| 2026 ** sequence of comparisons. |
| 1827 */ | 2027 */ |
| 1828 if( destIfNull==destIfFalse ){ | 2028 if( eType==IN_INDEX_NOOP ){ |
| 1829 /* Shortcut for the common case where the false and NULL outcomes are | 2029 ExprList *pList = pExpr->x.pList; |
| 1830 ** the same. */ | 2030 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
| 1831 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); | 2031 int labelOk = sqlite3VdbeMakeLabel(v); |
| 2032 int r2, regToFree; |
| 2033 int regCkNull = 0; |
| 2034 int ii; |
| 2035 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 2036 if( destIfNull!=destIfFalse ){ |
| 2037 regCkNull = sqlite3GetTempReg(pParse); |
| 2038 sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull); |
| 2039 } |
| 2040 for(ii=0; ii<pList->nExpr; ii++){ |
| 2041 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); |
| 2042 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ |
| 2043 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); |
| 2044 } |
| 2045 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ |
| 2046 sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2, |
| 2047 (void*)pColl, P4_COLLSEQ); |
| 2048 VdbeCoverageIf(v, ii<pList->nExpr-1); |
| 2049 VdbeCoverageIf(v, ii==pList->nExpr-1); |
| 2050 sqlite3VdbeChangeP5(v, affinity); |
| 2051 }else{ |
| 2052 assert( destIfNull==destIfFalse ); |
| 2053 sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, |
| 2054 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); |
| 2055 sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL); |
| 2056 } |
| 2057 sqlite3ReleaseTempReg(pParse, regToFree); |
| 2058 } |
| 2059 if( regCkNull ){ |
| 2060 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); |
| 2061 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); |
| 2062 } |
| 2063 sqlite3VdbeResolveLabel(v, labelOk); |
| 2064 sqlite3ReleaseTempReg(pParse, regCkNull); |
| 1832 }else{ | 2065 }else{ |
| 1833 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); | 2066 |
| 1834 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); | 2067 /* If the LHS is NULL, then the result is either false or NULL depending |
| 1835 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); | 2068 ** on whether the RHS is empty or not, respectively. |
| 1836 sqlite3VdbeJumpHere(v, addr1); | |
| 1837 } | |
| 1838 | |
| 1839 if( eType==IN_INDEX_ROWID ){ | |
| 1840 /* In this case, the RHS is the ROWID of table b-tree | |
| 1841 */ | 2069 */ |
| 1842 sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); | 2070 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ |
| 1843 sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1); | 2071 if( destIfNull==destIfFalse ){ |
| 1844 }else{ | 2072 /* Shortcut for the common case where the false and NULL outcomes are |
| 1845 /* In this case, the RHS is an index b-tree. | 2073 ** the same. */ |
| 1846 */ | 2074 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v); |
| 1847 sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1); | 2075 }else{ |
| 1848 | 2076 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); |
| 1849 /* If the set membership test fails, then the result of the | 2077 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); |
| 1850 ** "x IN (...)" expression must be either 0 or NULL. If the set | 2078 VdbeCoverage(v); |
| 1851 ** contains no NULL values, then the result is 0. If the set | 2079 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); |
| 1852 ** contains one or more NULL values, then the result of the | 2080 sqlite3VdbeJumpHere(v, addr1); |
| 1853 ** expression is also NULL. | 2081 } |
| 1854 */ | 2082 } |
| 1855 if( rRhsHasNull==0 || destIfFalse==destIfNull ){ | 2083 |
| 1856 /* This branch runs if it is known at compile time that the RHS | 2084 if( eType==IN_INDEX_ROWID ){ |
| 1857 ** cannot contain NULL values. This happens as the result | 2085 /* In this case, the RHS is the ROWID of table b-tree |
| 1858 ** of a "NOT NULL" constraint in the database schema. | |
| 1859 ** | |
| 1860 ** Also run this branch if NULL is equivalent to FALSE | |
| 1861 ** for this particular IN operator. | |
| 1862 */ | 2086 */ |
| 1863 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); | 2087 sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v); |
| 1864 | 2088 sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1); |
| 2089 VdbeCoverage(v); |
| 1865 }else{ | 2090 }else{ |
| 1866 /* In this branch, the RHS of the IN might contain a NULL and | 2091 /* In this case, the RHS is an index b-tree. |
| 1867 ** the presence of a NULL on the RHS makes a difference in the | |
| 1868 ** outcome. | |
| 1869 */ | 2092 */ |
| 1870 int j1, j2, j3; | 2093 sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1); |
| 1871 | 2094 |
| 1872 /* First check to see if the LHS is contained in the RHS. If so, | 2095 /* If the set membership test fails, then the result of the |
| 1873 ** then the presence of NULLs in the RHS does not matter, so jump | 2096 ** "x IN (...)" expression must be either 0 or NULL. If the set |
| 1874 ** over all of the code that follows. | 2097 ** contains no NULL values, then the result is 0. If the set |
| 2098 ** contains one or more NULL values, then the result of the |
| 2099 ** expression is also NULL. |
| 1875 */ | 2100 */ |
| 1876 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); | 2101 assert( destIfFalse!=destIfNull || rRhsHasNull==0 ); |
| 1877 | 2102 if( rRhsHasNull==0 ){ |
| 1878 /* Here we begin generating code that runs if the LHS is not | 2103 /* This branch runs if it is known at compile time that the RHS |
| 1879 ** contained within the RHS. Generate additional code that | 2104 ** cannot contain NULL values. This happens as the result |
| 1880 ** tests the RHS for NULLs. If the RHS contains a NULL then | 2105 ** of a "NOT NULL" constraint in the database schema. |
| 1881 ** jump to destIfNull. If there are no NULLs in the RHS then | 2106 ** |
| 1882 ** jump to destIfFalse. | 2107 ** Also run this branch if NULL is equivalent to FALSE |
| 1883 */ | 2108 ** for this particular IN operator. |
| 1884 j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull); | 2109 */ |
| 1885 j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1); | 2110 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); |
| 1886 sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull); | 2111 VdbeCoverage(v); |
| 1887 sqlite3VdbeJumpHere(v, j3); | 2112 }else{ |
| 1888 sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1); | 2113 /* In this branch, the RHS of the IN might contain a NULL and |
| 1889 sqlite3VdbeJumpHere(v, j2); | 2114 ** the presence of a NULL on the RHS makes a difference in the |
| 1890 | 2115 ** outcome. |
| 1891 /* Jump to the appropriate target depending on whether or not | 2116 */ |
| 1892 ** the RHS contains a NULL | 2117 int j1; |
| 1893 */ | 2118 |
| 1894 sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); | 2119 /* First check to see if the LHS is contained in the RHS. If so, |
| 1895 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); | 2120 ** then the answer is TRUE the presence of NULLs in the RHS does |
| 1896 | 2121 ** not matter. If the LHS is not contained in the RHS, then the |
| 1897 /* The OP_Found at the top of this branch jumps here when true, | 2122 ** answer is NULL if the RHS contains NULLs and the answer is |
| 1898 ** causing the overall IN expression evaluation to fall through. | 2123 ** FALSE if the RHS is NULL-free. |
| 1899 */ | 2124 */ |
| 1900 sqlite3VdbeJumpHere(v, j1); | 2125 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); |
| 2126 VdbeCoverage(v); |
| 2127 sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); |
| 2128 VdbeCoverage(v); |
| 2129 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); |
| 2130 sqlite3VdbeJumpHere(v, j1); |
| 2131 } |
| 1901 } | 2132 } |
| 1902 } | 2133 } |
| 1903 sqlite3ReleaseTempReg(pParse, r1); | 2134 sqlite3ReleaseTempReg(pParse, r1); |
| 1904 sqlite3ExprCachePop(pParse, 1); | 2135 sqlite3ExprCachePop(pParse); |
| 1905 VdbeComment((v, "end IN expr")); | 2136 VdbeComment((v, "end IN expr")); |
| 1906 } | 2137 } |
| 1907 #endif /* SQLITE_OMIT_SUBQUERY */ | 2138 #endif /* SQLITE_OMIT_SUBQUERY */ |
| 1908 | 2139 |
| 1909 /* | 2140 /* |
| 1910 ** Duplicate an 8-byte value | 2141 ** Duplicate an 8-byte value |
| 1911 */ | 2142 */ |
| 1912 static char *dup8bytes(Vdbe *v, const char *in){ | 2143 static char *dup8bytes(Vdbe *v, const char *in){ |
| 1913 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); | 2144 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); |
| 1914 if( out ){ | 2145 if( out ){ |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1951 if( pExpr->flags & EP_IntValue ){ | 2182 if( pExpr->flags & EP_IntValue ){ |
| 1952 int i = pExpr->u.iValue; | 2183 int i = pExpr->u.iValue; |
| 1953 assert( i>=0 ); | 2184 assert( i>=0 ); |
| 1954 if( negFlag ) i = -i; | 2185 if( negFlag ) i = -i; |
| 1955 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); | 2186 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
| 1956 }else{ | 2187 }else{ |
| 1957 int c; | 2188 int c; |
| 1958 i64 value; | 2189 i64 value; |
| 1959 const char *z = pExpr->u.zToken; | 2190 const char *z = pExpr->u.zToken; |
| 1960 assert( z!=0 ); | 2191 assert( z!=0 ); |
| 1961 c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); | 2192 c = sqlite3DecOrHexToI64(z, &value); |
| 1962 if( c==0 || (c==2 && negFlag) ){ | 2193 if( c==0 || (c==2 && negFlag) ){ |
| 1963 char *zV; | 2194 char *zV; |
| 1964 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } | 2195 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } |
| 1965 zV = dup8bytes(v, (char*)&value); | 2196 zV = dup8bytes(v, (char*)&value); |
| 1966 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); | 2197 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); |
| 1967 }else{ | 2198 }else{ |
| 1968 #ifdef SQLITE_OMIT_FLOATING_POINT | 2199 #ifdef SQLITE_OMIT_FLOATING_POINT |
| 1969 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); | 2200 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); |
| 1970 #else | 2201 #else |
| 1971 codeReal(v, z, negFlag, iMem); | 2202 #ifndef SQLITE_OMIT_HEX_INTEGER |
| 2203 if( sqlite3_strnicmp(z,"0x",2)==0 ){ |
| 2204 sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); |
| 2205 }else |
| 2206 #endif |
| 2207 { |
| 2208 codeReal(v, z, negFlag, iMem); |
| 2209 } |
| 1972 #endif | 2210 #endif |
| 1973 } | 2211 } |
| 1974 } | 2212 } |
| 1975 } | 2213 } |
| 1976 | 2214 |
| 1977 /* | 2215 /* |
| 1978 ** Clear a cache entry. | 2216 ** Clear a cache entry. |
| 1979 */ | 2217 */ |
| 1980 static void cacheEntryClear(Parse *pParse, struct yColCache *p){ | 2218 static void cacheEntryClear(Parse *pParse, struct yColCache *p){ |
| 1981 if( p->tempReg ){ | 2219 if( p->tempReg ){ |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1997 int idxLru; | 2235 int idxLru; |
| 1998 struct yColCache *p; | 2236 struct yColCache *p; |
| 1999 | 2237 |
| 2000 assert( iReg>0 ); /* Register numbers are always positive */ | 2238 assert( iReg>0 ); /* Register numbers are always positive */ |
| 2001 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ | 2239 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ |
| 2002 | 2240 |
| 2003 /* The SQLITE_ColumnCache flag disables the column cache. This is used | 2241 /* The SQLITE_ColumnCache flag disables the column cache. This is used |
| 2004 ** for testing only - to verify that SQLite always gets the same answer | 2242 ** for testing only - to verify that SQLite always gets the same answer |
| 2005 ** with and without the column cache. | 2243 ** with and without the column cache. |
| 2006 */ | 2244 */ |
| 2007 if( pParse->db->flags & SQLITE_ColumnCache ) return; | 2245 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; |
| 2008 | 2246 |
| 2009 /* First replace any existing entry. | 2247 /* First replace any existing entry. |
| 2010 ** | 2248 ** |
| 2011 ** Actually, the way the column cache is currently used, we are guaranteed | 2249 ** Actually, the way the column cache is currently used, we are guaranteed |
| 2012 ** that the object will never already be in cache. Verify this guarantee. | 2250 ** that the object will never already be in cache. Verify this guarantee. |
| 2013 */ | 2251 */ |
| 2014 #ifndef NDEBUG | 2252 #ifndef NDEBUG |
| 2015 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | 2253 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ |
| 2016 #if 0 /* This code wold remove the entry from the cache if it existed */ | |
| 2017 if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){ | |
| 2018 cacheEntryClear(pParse, p); | |
| 2019 p->iLevel = pParse->iCacheLevel; | |
| 2020 p->iReg = iReg; | |
| 2021 p->lru = pParse->iCacheCnt++; | |
| 2022 return; | |
| 2023 } | |
| 2024 #endif | |
| 2025 assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); | 2254 assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); |
| 2026 } | 2255 } |
| 2027 #endif | 2256 #endif |
| 2028 | 2257 |
| 2029 /* Find an empty slot and replace it */ | 2258 /* Find an empty slot and replace it */ |
| 2030 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | 2259 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ |
| 2031 if( p->iReg==0 ){ | 2260 if( p->iReg==0 ){ |
| 2032 p->iLevel = pParse->iCacheLevel; | 2261 p->iLevel = pParse->iCacheLevel; |
| 2033 p->iTable = iTab; | 2262 p->iTable = iTab; |
| 2034 p->iColumn = iCol; | 2263 p->iColumn = iCol; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2077 } | 2306 } |
| 2078 } | 2307 } |
| 2079 | 2308 |
| 2080 /* | 2309 /* |
| 2081 ** Remember the current column cache context. Any new entries added | 2310 ** Remember the current column cache context. Any new entries added |
| 2082 ** added to the column cache after this call are removed when the | 2311 ** added to the column cache after this call are removed when the |
| 2083 ** corresponding pop occurs. | 2312 ** corresponding pop occurs. |
| 2084 */ | 2313 */ |
| 2085 void sqlite3ExprCachePush(Parse *pParse){ | 2314 void sqlite3ExprCachePush(Parse *pParse){ |
| 2086 pParse->iCacheLevel++; | 2315 pParse->iCacheLevel++; |
| 2316 #ifdef SQLITE_DEBUG |
| 2317 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ |
| 2318 printf("PUSH to %d\n", pParse->iCacheLevel); |
| 2319 } |
| 2320 #endif |
| 2087 } | 2321 } |
| 2088 | 2322 |
| 2089 /* | 2323 /* |
| 2090 ** Remove from the column cache any entries that were added since the | 2324 ** Remove from the column cache any entries that were added since the |
| 2091 ** the previous N Push operations. In other words, restore the cache | 2325 ** the previous sqlite3ExprCachePush operation. In other words, restore |
| 2092 ** to the state it was in N Pushes ago. | 2326 ** the cache to the state it was in prior the most recent Push. |
| 2093 */ | 2327 */ |
| 2094 void sqlite3ExprCachePop(Parse *pParse, int N){ | 2328 void sqlite3ExprCachePop(Parse *pParse){ |
| 2095 int i; | 2329 int i; |
| 2096 struct yColCache *p; | 2330 struct yColCache *p; |
| 2097 assert( N>0 ); | 2331 assert( pParse->iCacheLevel>=1 ); |
| 2098 assert( pParse->iCacheLevel>=N ); | 2332 pParse->iCacheLevel--; |
| 2099 pParse->iCacheLevel -= N; | 2333 #ifdef SQLITE_DEBUG |
| 2334 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ |
| 2335 printf("POP to %d\n", pParse->iCacheLevel); |
| 2336 } |
| 2337 #endif |
| 2100 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | 2338 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ |
| 2101 if( p->iReg && p->iLevel>pParse->iCacheLevel ){ | 2339 if( p->iReg && p->iLevel>pParse->iCacheLevel ){ |
| 2102 cacheEntryClear(pParse, p); | 2340 cacheEntryClear(pParse, p); |
| 2103 p->iReg = 0; | 2341 p->iReg = 0; |
| 2104 } | 2342 } |
| 2105 } | 2343 } |
| 2106 } | 2344 } |
| 2107 | 2345 |
| 2108 /* | 2346 /* |
| 2109 ** When a cached column is reused, make sure that its register is | 2347 ** When a cached column is reused, make sure that its register is |
| (...skipping 10 matching lines...) Expand all Loading... |
| 2120 } | 2358 } |
| 2121 } | 2359 } |
| 2122 } | 2360 } |
| 2123 | 2361 |
| 2124 /* | 2362 /* |
| 2125 ** Generate code to extract the value of the iCol-th column of a table. | 2363 ** Generate code to extract the value of the iCol-th column of a table. |
| 2126 */ | 2364 */ |
| 2127 void sqlite3ExprCodeGetColumnOfTable( | 2365 void sqlite3ExprCodeGetColumnOfTable( |
| 2128 Vdbe *v, /* The VDBE under construction */ | 2366 Vdbe *v, /* The VDBE under construction */ |
| 2129 Table *pTab, /* The table containing the value */ | 2367 Table *pTab, /* The table containing the value */ |
| 2130 int iTabCur, /* The cursor for this table */ | 2368 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ |
| 2131 int iCol, /* Index of the column to extract */ | 2369 int iCol, /* Index of the column to extract */ |
| 2132 int regOut /* Extract the valud into this register */ | 2370 int regOut /* Extract the value into this register */ |
| 2133 ){ | 2371 ){ |
| 2134 if( iCol<0 || iCol==pTab->iPKey ){ | 2372 if( iCol<0 || iCol==pTab->iPKey ){ |
| 2135 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); | 2373 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); |
| 2136 }else{ | 2374 }else{ |
| 2137 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; | 2375 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; |
| 2138 sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut); | 2376 int x = iCol; |
| 2377 if( !HasRowid(pTab) ){ |
| 2378 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); |
| 2379 } |
| 2380 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); |
| 2139 } | 2381 } |
| 2140 if( iCol>=0 ){ | 2382 if( iCol>=0 ){ |
| 2141 sqlite3ColumnDefault(v, pTab, iCol, regOut); | 2383 sqlite3ColumnDefault(v, pTab, iCol, regOut); |
| 2142 } | 2384 } |
| 2143 } | 2385 } |
| 2144 | 2386 |
| 2145 /* | 2387 /* |
| 2146 ** Generate code that will extract the iColumn-th column from | 2388 ** Generate code that will extract the iColumn-th column from |
| 2147 ** table pTab and store the column value in a register. An effort | 2389 ** table pTab and store the column value in a register. An effort |
| 2148 ** is made to store the column value in register iReg, but this is | 2390 ** is made to store the column value in register iReg, but this is |
| 2149 ** not guaranteed. The location of the column value is returned. | 2391 ** not guaranteed. The location of the column value is returned. |
| 2150 ** | 2392 ** |
| 2151 ** There must be an open cursor to pTab in iTable when this routine | 2393 ** There must be an open cursor to pTab in iTable when this routine |
| 2152 ** is called. If iColumn<0 then code is generated that extracts the rowid. | 2394 ** is called. If iColumn<0 then code is generated that extracts the rowid. |
| 2153 */ | 2395 */ |
| 2154 int sqlite3ExprCodeGetColumn( | 2396 int sqlite3ExprCodeGetColumn( |
| 2155 Parse *pParse, /* Parsing and code generating context */ | 2397 Parse *pParse, /* Parsing and code generating context */ |
| 2156 Table *pTab, /* Description of the table we are reading from */ | 2398 Table *pTab, /* Description of the table we are reading from */ |
| 2157 int iColumn, /* Index of the table column */ | 2399 int iColumn, /* Index of the table column */ |
| 2158 int iTable, /* The cursor pointing to the table */ | 2400 int iTable, /* The cursor pointing to the table */ |
| 2159 int iReg /* Store results here */ | 2401 int iReg, /* Store results here */ |
| 2402 u8 p5 /* P5 value for OP_Column */ |
| 2160 ){ | 2403 ){ |
| 2161 Vdbe *v = pParse->pVdbe; | 2404 Vdbe *v = pParse->pVdbe; |
| 2162 int i; | 2405 int i; |
| 2163 struct yColCache *p; | 2406 struct yColCache *p; |
| 2164 | 2407 |
| 2165 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | 2408 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ |
| 2166 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ | 2409 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ |
| 2167 p->lru = pParse->iCacheCnt++; | 2410 p->lru = pParse->iCacheCnt++; |
| 2168 sqlite3ExprCachePinRegister(pParse, p->iReg); | 2411 sqlite3ExprCachePinRegister(pParse, p->iReg); |
| 2169 return p->iReg; | 2412 return p->iReg; |
| 2170 } | 2413 } |
| 2171 } | 2414 } |
| 2172 assert( v!=0 ); | 2415 assert( v!=0 ); |
| 2173 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); | 2416 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); |
| 2174 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); | 2417 if( p5 ){ |
| 2418 sqlite3VdbeChangeP5(v, p5); |
| 2419 }else{ |
| 2420 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); |
| 2421 } |
| 2175 return iReg; | 2422 return iReg; |
| 2176 } | 2423 } |
| 2177 | 2424 |
| 2178 /* | 2425 /* |
| 2179 ** Clear all column cache entries. | 2426 ** Clear all column cache entries. |
| 2180 */ | 2427 */ |
| 2181 void sqlite3ExprCacheClear(Parse *pParse){ | 2428 void sqlite3ExprCacheClear(Parse *pParse){ |
| 2182 int i; | 2429 int i; |
| 2183 struct yColCache *p; | 2430 struct yColCache *p; |
| 2184 | 2431 |
| 2432 #if SQLITE_DEBUG |
| 2433 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ |
| 2434 printf("CLEAR\n"); |
| 2435 } |
| 2436 #endif |
| 2185 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | 2437 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ |
| 2186 if( p->iReg ){ | 2438 if( p->iReg ){ |
| 2187 cacheEntryClear(pParse, p); | 2439 cacheEntryClear(pParse, p); |
| 2188 p->iReg = 0; | 2440 p->iReg = 0; |
| 2189 } | 2441 } |
| 2190 } | 2442 } |
| 2191 } | 2443 } |
| 2192 | 2444 |
| 2193 /* | 2445 /* |
| 2194 ** Record the fact that an affinity change has occurred on iCount | 2446 ** Record the fact that an affinity change has occurred on iCount |
| 2195 ** registers starting with iStart. | 2447 ** registers starting with iStart. |
| 2196 */ | 2448 */ |
| 2197 void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ | 2449 void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ |
| 2198 sqlite3ExprCacheRemove(pParse, iStart, iCount); | 2450 sqlite3ExprCacheRemove(pParse, iStart, iCount); |
| 2199 } | 2451 } |
| 2200 | 2452 |
| 2201 /* | 2453 /* |
| 2202 ** Generate code to move content from registers iFrom...iFrom+nReg-1 | 2454 ** Generate code to move content from registers iFrom...iFrom+nReg-1 |
| 2203 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. | 2455 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. |
| 2204 */ | 2456 */ |
| 2205 void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ | 2457 void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ |
| 2206 int i; | 2458 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); |
| 2207 struct yColCache *p; | |
| 2208 if( NEVER(iFrom==iTo) ) return; | |
| 2209 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); | 2459 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); |
| 2210 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | 2460 sqlite3ExprCacheRemove(pParse, iFrom, nReg); |
| 2211 int x = p->iReg; | |
| 2212 if( x>=iFrom && x<iFrom+nReg ){ | |
| 2213 p->iReg += iTo-iFrom; | |
| 2214 } | |
| 2215 } | |
| 2216 } | |
| 2217 | |
| 2218 /* | |
| 2219 ** Generate code to copy content from registers iFrom...iFrom+nReg-1 | |
| 2220 ** over to iTo..iTo+nReg-1. | |
| 2221 */ | |
| 2222 void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){ | |
| 2223 int i; | |
| 2224 if( NEVER(iFrom==iTo) ) return; | |
| 2225 for(i=0; i<nReg; i++){ | |
| 2226 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i); | |
| 2227 } | |
| 2228 } | 2461 } |
| 2229 | 2462 |
| 2230 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) | 2463 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| 2231 /* | 2464 /* |
| 2232 ** Return true if any register in the range iFrom..iTo (inclusive) | 2465 ** Return true if any register in the range iFrom..iTo (inclusive) |
| 2233 ** is used as part of the column cache. | 2466 ** is used as part of the column cache. |
| 2234 ** | 2467 ** |
| 2235 ** This routine is used within assert() and testcase() macros only | 2468 ** This routine is used within assert() and testcase() macros only |
| 2236 ** and does not appear in a normal build. | 2469 ** and does not appear in a normal build. |
| 2237 */ | 2470 */ |
| 2238 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ | 2471 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ |
| 2239 int i; | 2472 int i; |
| 2240 struct yColCache *p; | 2473 struct yColCache *p; |
| 2241 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | 2474 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ |
| 2242 int r = p->iReg; | 2475 int r = p->iReg; |
| 2243 if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ | 2476 if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ |
| 2244 } | 2477 } |
| 2245 return 0; | 2478 return 0; |
| 2246 } | 2479 } |
| 2247 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ | 2480 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ |
| 2248 | 2481 |
| 2249 /* | 2482 /* |
| 2483 ** Convert an expression node to a TK_REGISTER |
| 2484 */ |
| 2485 static void exprToRegister(Expr *p, int iReg){ |
| 2486 p->op2 = p->op; |
| 2487 p->op = TK_REGISTER; |
| 2488 p->iTable = iReg; |
| 2489 ExprClearProperty(p, EP_Skip); |
| 2490 } |
| 2491 |
| 2492 /* |
| 2250 ** Generate code into the current Vdbe to evaluate the given | 2493 ** Generate code into the current Vdbe to evaluate the given |
| 2251 ** expression. Attempt to store the results in register "target". | 2494 ** expression. Attempt to store the results in register "target". |
| 2252 ** Return the register where results are stored. | 2495 ** Return the register where results are stored. |
| 2253 ** | 2496 ** |
| 2254 ** With this routine, there is no guarantee that results will | 2497 ** With this routine, there is no guarantee that results will |
| 2255 ** be stored in target. The result might be stored in some other | 2498 ** be stored in target. The result might be stored in some other |
| 2256 ** register if it is convenient to do so. The calling function | 2499 ** register if it is convenient to do so. The calling function |
| 2257 ** must check the return code and move the results to the desired | 2500 ** must check the return code and move the results to the desired |
| 2258 ** register. | 2501 ** register. |
| 2259 */ | 2502 */ |
| 2260 int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ | 2503 int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ |
| 2261 Vdbe *v = pParse->pVdbe; /* The VM under construction */ | 2504 Vdbe *v = pParse->pVdbe; /* The VM under construction */ |
| 2262 int op; /* The opcode being coded */ | 2505 int op; /* The opcode being coded */ |
| 2263 int inReg = target; /* Results stored in register inReg */ | 2506 int inReg = target; /* Results stored in register inReg */ |
| 2264 int regFree1 = 0; /* If non-zero free this temporary register */ | 2507 int regFree1 = 0; /* If non-zero free this temporary register */ |
| 2265 int regFree2 = 0; /* If non-zero free this temporary register */ | 2508 int regFree2 = 0; /* If non-zero free this temporary register */ |
| 2266 int r1, r2, r3, r4; /* Various register numbers */ | 2509 int r1, r2, r3, r4; /* Various register numbers */ |
| 2267 sqlite3 *db = pParse->db; /* The database connection */ | 2510 sqlite3 *db = pParse->db; /* The database connection */ |
| 2511 Expr tempX; /* Temporary expression node */ |
| 2268 | 2512 |
| 2269 assert( target>0 && target<=pParse->nMem ); | 2513 assert( target>0 && target<=pParse->nMem ); |
| 2270 if( v==0 ){ | 2514 if( v==0 ){ |
| 2271 assert( pParse->db->mallocFailed ); | 2515 assert( pParse->db->mallocFailed ); |
| 2272 return 0; | 2516 return 0; |
| 2273 } | 2517 } |
| 2274 | 2518 |
| 2275 if( pExpr==0 ){ | 2519 if( pExpr==0 ){ |
| 2276 op = TK_NULL; | 2520 op = TK_NULL; |
| 2277 }else{ | 2521 }else{ |
| 2278 op = pExpr->op; | 2522 op = pExpr->op; |
| 2279 } | 2523 } |
| 2280 switch( op ){ | 2524 switch( op ){ |
| 2281 case TK_AGG_COLUMN: { | 2525 case TK_AGG_COLUMN: { |
| 2282 AggInfo *pAggInfo = pExpr->pAggInfo; | 2526 AggInfo *pAggInfo = pExpr->pAggInfo; |
| 2283 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; | 2527 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 2284 if( !pAggInfo->directMode ){ | 2528 if( !pAggInfo->directMode ){ |
| 2285 assert( pCol->iMem>0 ); | 2529 assert( pCol->iMem>0 ); |
| 2286 inReg = pCol->iMem; | 2530 inReg = pCol->iMem; |
| 2287 break; | 2531 break; |
| 2288 }else if( pAggInfo->useSortingIdx ){ | 2532 }else if( pAggInfo->useSortingIdx ){ |
| 2289 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, | 2533 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, |
| 2290 pCol->iSorterColumn, target); | 2534 pCol->iSorterColumn, target); |
| 2291 break; | 2535 break; |
| 2292 } | 2536 } |
| 2293 /* Otherwise, fall thru into the TK_COLUMN case */ | 2537 /* Otherwise, fall thru into the TK_COLUMN case */ |
| 2294 } | 2538 } |
| 2295 case TK_COLUMN: { | 2539 case TK_COLUMN: { |
| 2296 if( pExpr->iTable<0 ){ | 2540 int iTab = pExpr->iTable; |
| 2297 /* This only happens when coding check constraints */ | 2541 if( iTab<0 ){ |
| 2298 assert( pParse->ckBase>0 ); | 2542 if( pParse->ckBase>0 ){ |
| 2299 inReg = pExpr->iColumn + pParse->ckBase; | 2543 /* Generating CHECK constraints or inserting into partial index */ |
| 2300 }else{ | 2544 inReg = pExpr->iColumn + pParse->ckBase; |
| 2301 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, | 2545 break; |
| 2302 pExpr->iColumn, pExpr->iTable, target); | 2546 }else{ |
| 2547 /* Deleting from a partial index */ |
| 2548 iTab = pParse->iPartIdxTab; |
| 2549 } |
| 2303 } | 2550 } |
| 2551 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, |
| 2552 pExpr->iColumn, iTab, target, |
| 2553 pExpr->op2); |
| 2304 break; | 2554 break; |
| 2305 } | 2555 } |
| 2306 case TK_INTEGER: { | 2556 case TK_INTEGER: { |
| 2307 codeInteger(pParse, pExpr, 0, target); | 2557 codeInteger(pParse, pExpr, 0, target); |
| 2308 break; | 2558 break; |
| 2309 } | 2559 } |
| 2310 #ifndef SQLITE_OMIT_FLOATING_POINT | 2560 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 2311 case TK_FLOAT: { | 2561 case TK_FLOAT: { |
| 2312 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | 2562 assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 2313 codeReal(v, pExpr->u.zToken, 0, target); | 2563 codeReal(v, pExpr->u.zToken, 0, target); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 2338 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); | 2588 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); |
| 2339 break; | 2589 break; |
| 2340 } | 2590 } |
| 2341 #endif | 2591 #endif |
| 2342 case TK_VARIABLE: { | 2592 case TK_VARIABLE: { |
| 2343 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | 2593 assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 2344 assert( pExpr->u.zToken!=0 ); | 2594 assert( pExpr->u.zToken!=0 ); |
| 2345 assert( pExpr->u.zToken[0]!=0 ); | 2595 assert( pExpr->u.zToken[0]!=0 ); |
| 2346 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); | 2596 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); |
| 2347 if( pExpr->u.zToken[1]!=0 ){ | 2597 if( pExpr->u.zToken[1]!=0 ){ |
| 2348 sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, P4_TRANSIENT); | 2598 assert( pExpr->u.zToken[0]=='?' |
| 2599 || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); |
| 2600 sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); |
| 2349 } | 2601 } |
| 2350 break; | 2602 break; |
| 2351 } | 2603 } |
| 2352 case TK_REGISTER: { | 2604 case TK_REGISTER: { |
| 2353 inReg = pExpr->iTable; | 2605 inReg = pExpr->iTable; |
| 2354 break; | 2606 break; |
| 2355 } | 2607 } |
| 2356 case TK_AS: { | 2608 case TK_AS: { |
| 2357 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); | 2609 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
| 2358 break; | 2610 break; |
| 2359 } | 2611 } |
| 2360 #ifndef SQLITE_OMIT_CAST | 2612 #ifndef SQLITE_OMIT_CAST |
| 2361 case TK_CAST: { | 2613 case TK_CAST: { |
| 2362 /* Expressions of the form: CAST(pLeft AS token) */ | 2614 /* Expressions of the form: CAST(pLeft AS token) */ |
| 2363 int aff, to_op; | |
| 2364 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); | 2615 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
| 2365 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | |
| 2366 aff = sqlite3AffinityType(pExpr->u.zToken); | |
| 2367 to_op = aff - SQLITE_AFF_TEXT + OP_ToText; | |
| 2368 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); | |
| 2369 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); | |
| 2370 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); | |
| 2371 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); | |
| 2372 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); | |
| 2373 testcase( to_op==OP_ToText ); | |
| 2374 testcase( to_op==OP_ToBlob ); | |
| 2375 testcase( to_op==OP_ToNumeric ); | |
| 2376 testcase( to_op==OP_ToInt ); | |
| 2377 testcase( to_op==OP_ToReal ); | |
| 2378 if( inReg!=target ){ | 2616 if( inReg!=target ){ |
| 2379 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); | 2617 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); |
| 2380 inReg = target; | 2618 inReg = target; |
| 2381 } | 2619 } |
| 2382 sqlite3VdbeAddOp1(v, to_op, inReg); | 2620 sqlite3VdbeAddOp2(v, OP_Cast, target, |
| 2621 sqlite3AffinityType(pExpr->u.zToken, 0)); |
| 2383 testcase( usedAsColumnCache(pParse, inReg, inReg) ); | 2622 testcase( usedAsColumnCache(pParse, inReg, inReg) ); |
| 2384 sqlite3ExprCacheAffinityChange(pParse, inReg, 1); | 2623 sqlite3ExprCacheAffinityChange(pParse, inReg, 1); |
| 2385 break; | 2624 break; |
| 2386 } | 2625 } |
| 2387 #endif /* SQLITE_OMIT_CAST */ | 2626 #endif /* SQLITE_OMIT_CAST */ |
| 2388 case TK_LT: | 2627 case TK_LT: |
| 2389 case TK_LE: | 2628 case TK_LE: |
| 2390 case TK_GT: | 2629 case TK_GT: |
| 2391 case TK_GE: | 2630 case TK_GE: |
| 2392 case TK_NE: | 2631 case TK_NE: |
| 2393 case TK_EQ: { | 2632 case TK_EQ: { |
| 2394 assert( TK_LT==OP_Lt ); | |
| 2395 assert( TK_LE==OP_Le ); | |
| 2396 assert( TK_GT==OP_Gt ); | |
| 2397 assert( TK_GE==OP_Ge ); | |
| 2398 assert( TK_EQ==OP_Eq ); | |
| 2399 assert( TK_NE==OP_Ne ); | |
| 2400 testcase( op==TK_LT ); | |
| 2401 testcase( op==TK_LE ); | |
| 2402 testcase( op==TK_GT ); | |
| 2403 testcase( op==TK_GE ); | |
| 2404 testcase( op==TK_EQ ); | |
| 2405 testcase( op==TK_NE ); | |
| 2406 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 2633 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 2407 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); | 2634 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
| 2408 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | 2635 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
| 2409 r1, r2, inReg, SQLITE_STOREP2); | 2636 r1, r2, inReg, SQLITE_STOREP2); |
| 2637 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
| 2638 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
| 2639 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
| 2640 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
| 2641 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); |
| 2642 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); |
| 2410 testcase( regFree1==0 ); | 2643 testcase( regFree1==0 ); |
| 2411 testcase( regFree2==0 ); | 2644 testcase( regFree2==0 ); |
| 2412 break; | 2645 break; |
| 2413 } | 2646 } |
| 2414 case TK_IS: | 2647 case TK_IS: |
| 2415 case TK_ISNOT: { | 2648 case TK_ISNOT: { |
| 2416 testcase( op==TK_IS ); | 2649 testcase( op==TK_IS ); |
| 2417 testcase( op==TK_ISNOT ); | 2650 testcase( op==TK_ISNOT ); |
| 2418 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 2651 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 2419 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); | 2652 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
| 2420 op = (op==TK_IS) ? TK_EQ : TK_NE; | 2653 op = (op==TK_IS) ? TK_EQ : TK_NE; |
| 2421 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | 2654 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
| 2422 r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ); | 2655 r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ); |
| 2656 VdbeCoverageIf(v, op==TK_EQ); |
| 2657 VdbeCoverageIf(v, op==TK_NE); |
| 2423 testcase( regFree1==0 ); | 2658 testcase( regFree1==0 ); |
| 2424 testcase( regFree2==0 ); | 2659 testcase( regFree2==0 ); |
| 2425 break; | 2660 break; |
| 2426 } | 2661 } |
| 2427 case TK_AND: | 2662 case TK_AND: |
| 2428 case TK_OR: | 2663 case TK_OR: |
| 2429 case TK_PLUS: | 2664 case TK_PLUS: |
| 2430 case TK_STAR: | 2665 case TK_STAR: |
| 2431 case TK_MINUS: | 2666 case TK_MINUS: |
| 2432 case TK_REM: | 2667 case TK_REM: |
| 2433 case TK_BITAND: | 2668 case TK_BITAND: |
| 2434 case TK_BITOR: | 2669 case TK_BITOR: |
| 2435 case TK_SLASH: | 2670 case TK_SLASH: |
| 2436 case TK_LSHIFT: | 2671 case TK_LSHIFT: |
| 2437 case TK_RSHIFT: | 2672 case TK_RSHIFT: |
| 2438 case TK_CONCAT: { | 2673 case TK_CONCAT: { |
| 2439 assert( TK_AND==OP_And ); | 2674 assert( TK_AND==OP_And ); testcase( op==TK_AND ); |
| 2440 assert( TK_OR==OP_Or ); | 2675 assert( TK_OR==OP_Or ); testcase( op==TK_OR ); |
| 2441 assert( TK_PLUS==OP_Add ); | 2676 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); |
| 2442 assert( TK_MINUS==OP_Subtract ); | 2677 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); |
| 2443 assert( TK_REM==OP_Remainder ); | 2678 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); |
| 2444 assert( TK_BITAND==OP_BitAnd ); | 2679 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); |
| 2445 assert( TK_BITOR==OP_BitOr ); | 2680 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); |
| 2446 assert( TK_SLASH==OP_Divide ); | 2681 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); |
| 2447 assert( TK_LSHIFT==OP_ShiftLeft ); | 2682 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); |
| 2448 assert( TK_RSHIFT==OP_ShiftRight ); | 2683 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); |
| 2449 assert( TK_CONCAT==OP_Concat ); | 2684 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); |
| 2450 testcase( op==TK_AND ); | |
| 2451 testcase( op==TK_OR ); | |
| 2452 testcase( op==TK_PLUS ); | |
| 2453 testcase( op==TK_MINUS ); | |
| 2454 testcase( op==TK_REM ); | |
| 2455 testcase( op==TK_BITAND ); | |
| 2456 testcase( op==TK_BITOR ); | |
| 2457 testcase( op==TK_SLASH ); | |
| 2458 testcase( op==TK_LSHIFT ); | |
| 2459 testcase( op==TK_RSHIFT ); | |
| 2460 testcase( op==TK_CONCAT ); | |
| 2461 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 2685 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 2462 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); | 2686 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
| 2463 sqlite3VdbeAddOp3(v, op, r2, r1, target); | 2687 sqlite3VdbeAddOp3(v, op, r2, r1, target); |
| 2464 testcase( regFree1==0 ); | 2688 testcase( regFree1==0 ); |
| 2465 testcase( regFree2==0 ); | 2689 testcase( regFree2==0 ); |
| 2466 break; | 2690 break; |
| 2467 } | 2691 } |
| 2468 case TK_UMINUS: { | 2692 case TK_UMINUS: { |
| 2469 Expr *pLeft = pExpr->pLeft; | 2693 Expr *pLeft = pExpr->pLeft; |
| 2470 assert( pLeft ); | 2694 assert( pLeft ); |
| 2471 if( pLeft->op==TK_INTEGER ){ | 2695 if( pLeft->op==TK_INTEGER ){ |
| 2472 codeInteger(pParse, pLeft, 1, target); | 2696 codeInteger(pParse, pLeft, 1, target); |
| 2473 #ifndef SQLITE_OMIT_FLOATING_POINT | 2697 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 2474 }else if( pLeft->op==TK_FLOAT ){ | 2698 }else if( pLeft->op==TK_FLOAT ){ |
| 2475 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | 2699 assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 2476 codeReal(v, pLeft->u.zToken, 1, target); | 2700 codeReal(v, pLeft->u.zToken, 1, target); |
| 2477 #endif | 2701 #endif |
| 2478 }else{ | 2702 }else{ |
| 2479 regFree1 = r1 = sqlite3GetTempReg(pParse); | 2703 tempX.op = TK_INTEGER; |
| 2480 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); | 2704 tempX.flags = EP_IntValue|EP_TokenOnly; |
| 2705 tempX.u.iValue = 0; |
| 2706 r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); |
| 2481 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); | 2707 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); |
| 2482 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); | 2708 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); |
| 2483 testcase( regFree2==0 ); | 2709 testcase( regFree2==0 ); |
| 2484 } | 2710 } |
| 2485 inReg = target; | 2711 inReg = target; |
| 2486 break; | 2712 break; |
| 2487 } | 2713 } |
| 2488 case TK_BITNOT: | 2714 case TK_BITNOT: |
| 2489 case TK_NOT: { | 2715 case TK_NOT: { |
| 2490 assert( TK_BITNOT==OP_BitNot ); | 2716 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); |
| 2491 assert( TK_NOT==OP_Not ); | 2717 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); |
| 2492 testcase( op==TK_BITNOT ); | |
| 2493 testcase( op==TK_NOT ); | |
| 2494 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 2718 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 2495 testcase( regFree1==0 ); | 2719 testcase( regFree1==0 ); |
| 2496 inReg = target; | 2720 inReg = target; |
| 2497 sqlite3VdbeAddOp2(v, op, r1, inReg); | 2721 sqlite3VdbeAddOp2(v, op, r1, inReg); |
| 2498 break; | 2722 break; |
| 2499 } | 2723 } |
| 2500 case TK_ISNULL: | 2724 case TK_ISNULL: |
| 2501 case TK_NOTNULL: { | 2725 case TK_NOTNULL: { |
| 2502 int addr; | 2726 int addr; |
| 2503 assert( TK_ISNULL==OP_IsNull ); | 2727 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); |
| 2504 assert( TK_NOTNULL==OP_NotNull ); | 2728 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); |
| 2505 testcase( op==TK_ISNULL ); | |
| 2506 testcase( op==TK_NOTNULL ); | |
| 2507 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); | 2729 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
| 2508 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 2730 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 2509 testcase( regFree1==0 ); | 2731 testcase( regFree1==0 ); |
| 2510 addr = sqlite3VdbeAddOp1(v, op, r1); | 2732 addr = sqlite3VdbeAddOp1(v, op, r1); |
| 2511 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); | 2733 VdbeCoverageIf(v, op==TK_ISNULL); |
| 2734 VdbeCoverageIf(v, op==TK_NOTNULL); |
| 2735 sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
| 2512 sqlite3VdbeJumpHere(v, addr); | 2736 sqlite3VdbeJumpHere(v, addr); |
| 2513 break; | 2737 break; |
| 2514 } | 2738 } |
| 2515 case TK_AGG_FUNCTION: { | 2739 case TK_AGG_FUNCTION: { |
| 2516 AggInfo *pInfo = pExpr->pAggInfo; | 2740 AggInfo *pInfo = pExpr->pAggInfo; |
| 2517 if( pInfo==0 ){ | 2741 if( pInfo==0 ){ |
| 2518 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | 2742 assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 2519 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); | 2743 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); |
| 2520 }else{ | 2744 }else{ |
| 2521 inReg = pInfo->aFunc[pExpr->iAgg].iMem; | 2745 inReg = pInfo->aFunc[pExpr->iAgg].iMem; |
| 2522 } | 2746 } |
| 2523 break; | 2747 break; |
| 2524 } | 2748 } |
| 2525 case TK_CONST_FUNC: | |
| 2526 case TK_FUNCTION: { | 2749 case TK_FUNCTION: { |
| 2527 ExprList *pFarg; /* List of function arguments */ | 2750 ExprList *pFarg; /* List of function arguments */ |
| 2528 int nFarg; /* Number of function arguments */ | 2751 int nFarg; /* Number of function arguments */ |
| 2529 FuncDef *pDef; /* The function definition object */ | 2752 FuncDef *pDef; /* The function definition object */ |
| 2530 int nId; /* Length of the function name in bytes */ | 2753 int nId; /* Length of the function name in bytes */ |
| 2531 const char *zId; /* The function name */ | 2754 const char *zId; /* The function name */ |
| 2532 int constMask = 0; /* Mask of function arguments that are constant */ | 2755 u32 constMask = 0; /* Mask of function arguments that are constant */ |
| 2533 int i; /* Loop counter */ | 2756 int i; /* Loop counter */ |
| 2534 u8 enc = ENC(db); /* The text encoding used by this database */ | 2757 u8 enc = ENC(db); /* The text encoding used by this database */ |
| 2535 CollSeq *pColl = 0; /* A collating sequence */ | 2758 CollSeq *pColl = 0; /* A collating sequence */ |
| 2536 | 2759 |
| 2537 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | 2760 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 2538 testcase( op==TK_CONST_FUNC ); | 2761 if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 2539 testcase( op==TK_FUNCTION ); | |
| 2540 if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){ | |
| 2541 pFarg = 0; | 2762 pFarg = 0; |
| 2542 }else{ | 2763 }else{ |
| 2543 pFarg = pExpr->x.pList; | 2764 pFarg = pExpr->x.pList; |
| 2544 } | 2765 } |
| 2545 nFarg = pFarg ? pFarg->nExpr : 0; | 2766 nFarg = pFarg ? pFarg->nExpr : 0; |
| 2546 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | 2767 assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 2547 zId = pExpr->u.zToken; | 2768 zId = pExpr->u.zToken; |
| 2548 nId = sqlite3Strlen30(zId); | 2769 nId = sqlite3Strlen30(zId); |
| 2549 pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0); | 2770 pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0); |
| 2550 if( pDef==0 ){ | 2771 if( pDef==0 || pDef->xFunc==0 ){ |
| 2551 sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId); | 2772 sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId); |
| 2552 break; | 2773 break; |
| 2553 } | 2774 } |
| 2554 | 2775 |
| 2555 /* Attempt a direct implementation of the built-in COALESCE() and | 2776 /* Attempt a direct implementation of the built-in COALESCE() and |
| 2556 ** IFNULL() functions. This avoids unnecessary evalation of | 2777 ** IFNULL() functions. This avoids unnecessary evaluation of |
| 2557 ** arguments past the first non-NULL argument. | 2778 ** arguments past the first non-NULL argument. |
| 2558 */ | 2779 */ |
| 2559 if( pDef->flags & SQLITE_FUNC_COALESCE ){ | 2780 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ |
| 2560 int endCoalesce = sqlite3VdbeMakeLabel(v); | 2781 int endCoalesce = sqlite3VdbeMakeLabel(v); |
| 2561 assert( nFarg>=2 ); | 2782 assert( nFarg>=2 ); |
| 2562 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); | 2783 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); |
| 2563 for(i=1; i<nFarg; i++){ | 2784 for(i=1; i<nFarg; i++){ |
| 2564 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); | 2785 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); |
| 2786 VdbeCoverage(v); |
| 2565 sqlite3ExprCacheRemove(pParse, target, 1); | 2787 sqlite3ExprCacheRemove(pParse, target, 1); |
| 2566 sqlite3ExprCachePush(pParse); | 2788 sqlite3ExprCachePush(pParse); |
| 2567 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); | 2789 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); |
| 2568 sqlite3ExprCachePop(pParse, 1); | 2790 sqlite3ExprCachePop(pParse); |
| 2569 } | 2791 } |
| 2570 sqlite3VdbeResolveLabel(v, endCoalesce); | 2792 sqlite3VdbeResolveLabel(v, endCoalesce); |
| 2571 break; | 2793 break; |
| 2572 } | 2794 } |
| 2573 | 2795 |
| 2796 /* The UNLIKELY() function is a no-op. The result is the value |
| 2797 ** of the first argument. |
| 2798 */ |
| 2799 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ |
| 2800 assert( nFarg>=1 ); |
| 2801 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); |
| 2802 break; |
| 2803 } |
| 2574 | 2804 |
| 2805 for(i=0; i<nFarg; i++){ |
| 2806 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ |
| 2807 testcase( i==31 ); |
| 2808 constMask |= MASKBIT32(i); |
| 2809 } |
| 2810 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ |
| 2811 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); |
| 2812 } |
| 2813 } |
| 2575 if( pFarg ){ | 2814 if( pFarg ){ |
| 2576 r1 = sqlite3GetTempRange(pParse, nFarg); | 2815 if( constMask ){ |
| 2816 r1 = pParse->nMem+1; |
| 2817 pParse->nMem += nFarg; |
| 2818 }else{ |
| 2819 r1 = sqlite3GetTempRange(pParse, nFarg); |
| 2820 } |
| 2821 |
| 2822 /* For length() and typeof() functions with a column argument, |
| 2823 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG |
| 2824 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data |
| 2825 ** loading. |
| 2826 */ |
| 2827 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ |
| 2828 u8 exprOp; |
| 2829 assert( nFarg==1 ); |
| 2830 assert( pFarg->a[0].pExpr!=0 ); |
| 2831 exprOp = pFarg->a[0].pExpr->op; |
| 2832 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ |
| 2833 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); |
| 2834 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); |
| 2835 testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); |
| 2836 pFarg->a[0].pExpr->op2 = |
| 2837 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); |
| 2838 } |
| 2839 } |
| 2840 |
| 2577 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ | 2841 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ |
| 2578 sqlite3ExprCodeExprList(pParse, pFarg, r1, 1); | 2842 sqlite3ExprCodeExprList(pParse, pFarg, r1, |
| 2579 sqlite3ExprCachePop(pParse, 1); /* Ticket 2ea2425d34be */ | 2843 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); |
| 2844 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ |
| 2580 }else{ | 2845 }else{ |
| 2581 r1 = 0; | 2846 r1 = 0; |
| 2582 } | 2847 } |
| 2583 #ifndef SQLITE_OMIT_VIRTUALTABLE | 2848 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2584 /* Possibly overload the function if the first argument is | 2849 /* Possibly overload the function if the first argument is |
| 2585 ** a virtual table column. | 2850 ** a virtual table column. |
| 2586 ** | 2851 ** |
| 2587 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the | 2852 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the |
| 2588 ** second argument, not the first, as the argument to test to | 2853 ** second argument, not the first, as the argument to test to |
| 2589 ** see if it is a column in a virtual table. This is done because | 2854 ** see if it is a column in a virtual table. This is done because |
| 2590 ** the left operand of infix functions (the operand we want to | 2855 ** the left operand of infix functions (the operand we want to |
| 2591 ** control overloading) ends up as the second argument to the | 2856 ** control overloading) ends up as the second argument to the |
| 2592 ** function. The expression "A glob B" is equivalent to | 2857 ** function. The expression "A glob B" is equivalent to |
| 2593 ** "glob(B,A). We want to use the A in "A glob B" to test | 2858 ** "glob(B,A). We want to use the A in "A glob B" to test |
| 2594 ** for function overloading. But we use the B term in "glob(B,A)". | 2859 ** for function overloading. But we use the B term in "glob(B,A)". |
| 2595 */ | 2860 */ |
| 2596 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ | 2861 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ |
| 2597 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); | 2862 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); |
| 2598 }else if( nFarg>0 ){ | 2863 }else if( nFarg>0 ){ |
| 2599 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); | 2864 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); |
| 2600 } | 2865 } |
| 2601 #endif | 2866 #endif |
| 2602 for(i=0; i<nFarg; i++){ | 2867 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 2603 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ | |
| 2604 constMask |= (1<<i); | |
| 2605 } | |
| 2606 if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ | |
| 2607 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); | |
| 2608 } | |
| 2609 } | |
| 2610 if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){ | |
| 2611 if( !pColl ) pColl = db->pDfltColl; | 2868 if( !pColl ) pColl = db->pDfltColl; |
| 2612 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); | 2869 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); |
| 2613 } | 2870 } |
| 2614 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, | 2871 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, |
| 2615 (char*)pDef, P4_FUNCDEF); | 2872 (char*)pDef, P4_FUNCDEF); |
| 2616 sqlite3VdbeChangeP5(v, (u8)nFarg); | 2873 sqlite3VdbeChangeP5(v, (u8)nFarg); |
| 2617 if( nFarg ){ | 2874 if( nFarg && constMask==0 ){ |
| 2618 sqlite3ReleaseTempRange(pParse, r1, nFarg); | 2875 sqlite3ReleaseTempRange(pParse, r1, nFarg); |
| 2619 } | 2876 } |
| 2620 break; | 2877 break; |
| 2621 } | 2878 } |
| 2622 #ifndef SQLITE_OMIT_SUBQUERY | 2879 #ifndef SQLITE_OMIT_SUBQUERY |
| 2623 case TK_EXISTS: | 2880 case TK_EXISTS: |
| 2624 case TK_SELECT: { | 2881 case TK_SELECT: { |
| 2625 testcase( op==TK_EXISTS ); | 2882 testcase( op==TK_EXISTS ); |
| 2626 testcase( op==TK_SELECT ); | 2883 testcase( op==TK_SELECT ); |
| 2627 inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); | 2884 inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 2657 struct ExprList_item *pLItem = pExpr->x.pList->a; | 2914 struct ExprList_item *pLItem = pExpr->x.pList->a; |
| 2658 Expr *pRight = pLItem->pExpr; | 2915 Expr *pRight = pLItem->pExpr; |
| 2659 | 2916 |
| 2660 r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); | 2917 r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); |
| 2661 r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); | 2918 r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); |
| 2662 testcase( regFree1==0 ); | 2919 testcase( regFree1==0 ); |
| 2663 testcase( regFree2==0 ); | 2920 testcase( regFree2==0 ); |
| 2664 r3 = sqlite3GetTempReg(pParse); | 2921 r3 = sqlite3GetTempReg(pParse); |
| 2665 r4 = sqlite3GetTempReg(pParse); | 2922 r4 = sqlite3GetTempReg(pParse); |
| 2666 codeCompare(pParse, pLeft, pRight, OP_Ge, | 2923 codeCompare(pParse, pLeft, pRight, OP_Ge, |
| 2667 r1, r2, r3, SQLITE_STOREP2); | 2924 r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v); |
| 2668 pLItem++; | 2925 pLItem++; |
| 2669 pRight = pLItem->pExpr; | 2926 pRight = pLItem->pExpr; |
| 2670 sqlite3ReleaseTempReg(pParse, regFree2); | 2927 sqlite3ReleaseTempReg(pParse, regFree2); |
| 2671 r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); | 2928 r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); |
| 2672 testcase( regFree2==0 ); | 2929 testcase( regFree2==0 ); |
| 2673 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); | 2930 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); |
| 2931 VdbeCoverage(v); |
| 2674 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); | 2932 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); |
| 2675 sqlite3ReleaseTempReg(pParse, r3); | 2933 sqlite3ReleaseTempReg(pParse, r3); |
| 2676 sqlite3ReleaseTempReg(pParse, r4); | 2934 sqlite3ReleaseTempReg(pParse, r4); |
| 2677 break; | 2935 break; |
| 2678 } | 2936 } |
| 2937 case TK_COLLATE: |
| 2679 case TK_UPLUS: { | 2938 case TK_UPLUS: { |
| 2680 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); | 2939 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
| 2681 break; | 2940 break; |
| 2682 } | 2941 } |
| 2683 | 2942 |
| 2684 case TK_TRIGGER: { | 2943 case TK_TRIGGER: { |
| 2685 /* If the opcode is TK_TRIGGER, then the expression is a reference | 2944 /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 2686 ** to a column in the new.* or old.* pseudo-tables available to | 2945 ** to a column in the new.* or old.* pseudo-tables available to |
| 2687 ** trigger programs. In this case Expr.iTable is set to 1 for the | 2946 ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 2688 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn | 2947 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2740 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END | 2999 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
| 2741 ** | 3000 ** |
| 2742 ** Form B: | 3001 ** Form B: |
| 2743 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END | 3002 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
| 2744 ** | 3003 ** |
| 2745 ** Form A is can be transformed into the equivalent form B as follows: | 3004 ** Form A is can be transformed into the equivalent form B as follows: |
| 2746 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... | 3005 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... |
| 2747 ** WHEN x=eN THEN rN ELSE y END | 3006 ** WHEN x=eN THEN rN ELSE y END |
| 2748 ** | 3007 ** |
| 2749 ** X (if it exists) is in pExpr->pLeft. | 3008 ** X (if it exists) is in pExpr->pLeft. |
| 2750 ** Y is in pExpr->pRight. The Y is also optional. If there is no | 3009 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is |
| 2751 ** ELSE clause and no other term matches, then the result of the | 3010 ** odd. The Y is also optional. If the number of elements in x.pList |
| 2752 ** exprssion is NULL. | 3011 ** is even, then Y is omitted and the "otherwise" result is NULL. |
| 2753 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. | 3012 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. |
| 2754 ** | 3013 ** |
| 2755 ** The result of the expression is the Ri for the first matching Ei, | 3014 ** The result of the expression is the Ri for the first matching Ei, |
| 2756 ** or if there is no matching Ei, the ELSE term Y, or if there is | 3015 ** or if there is no matching Ei, the ELSE term Y, or if there is |
| 2757 ** no ELSE term, NULL. | 3016 ** no ELSE term, NULL. |
| 2758 */ | 3017 */ |
| 2759 default: assert( op==TK_CASE ); { | 3018 default: assert( op==TK_CASE ); { |
| 2760 int endLabel; /* GOTO label for end of CASE stmt */ | 3019 int endLabel; /* GOTO label for end of CASE stmt */ |
| 2761 int nextCase; /* GOTO label for next WHEN clause */ | 3020 int nextCase; /* GOTO label for next WHEN clause */ |
| 2762 int nExpr; /* 2x number of WHEN terms */ | 3021 int nExpr; /* 2x number of WHEN terms */ |
| 2763 int i; /* Loop counter */ | 3022 int i; /* Loop counter */ |
| 2764 ExprList *pEList; /* List of WHEN terms */ | 3023 ExprList *pEList; /* List of WHEN terms */ |
| 2765 struct ExprList_item *aListelem; /* Array of WHEN terms */ | 3024 struct ExprList_item *aListelem; /* Array of WHEN terms */ |
| 2766 Expr opCompare; /* The X==Ei expression */ | 3025 Expr opCompare; /* The X==Ei expression */ |
| 2767 Expr cacheX; /* Cached expression X */ | |
| 2768 Expr *pX; /* The X expression */ | 3026 Expr *pX; /* The X expression */ |
| 2769 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ | 3027 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ |
| 2770 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) | 3028 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) |
| 2771 | 3029 |
| 2772 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); | 3030 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); |
| 2773 assert((pExpr->x.pList->nExpr % 2) == 0); | |
| 2774 assert(pExpr->x.pList->nExpr > 0); | 3031 assert(pExpr->x.pList->nExpr > 0); |
| 2775 pEList = pExpr->x.pList; | 3032 pEList = pExpr->x.pList; |
| 2776 aListelem = pEList->a; | 3033 aListelem = pEList->a; |
| 2777 nExpr = pEList->nExpr; | 3034 nExpr = pEList->nExpr; |
| 2778 endLabel = sqlite3VdbeMakeLabel(v); | 3035 endLabel = sqlite3VdbeMakeLabel(v); |
| 2779 if( (pX = pExpr->pLeft)!=0 ){ | 3036 if( (pX = pExpr->pLeft)!=0 ){ |
| 2780 cacheX = *pX; | 3037 tempX = *pX; |
| 2781 testcase( pX->op==TK_COLUMN ); | 3038 testcase( pX->op==TK_COLUMN ); |
| 2782 testcase( pX->op==TK_REGISTER ); | 3039 exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); |
| 2783 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); | |
| 2784 testcase( regFree1==0 ); | 3040 testcase( regFree1==0 ); |
| 2785 cacheX.op = TK_REGISTER; | |
| 2786 opCompare.op = TK_EQ; | 3041 opCompare.op = TK_EQ; |
| 2787 opCompare.pLeft = &cacheX; | 3042 opCompare.pLeft = &tempX; |
| 2788 pTest = &opCompare; | 3043 pTest = &opCompare; |
| 2789 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: | 3044 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: |
| 2790 ** The value in regFree1 might get SCopy-ed into the file result. | 3045 ** The value in regFree1 might get SCopy-ed into the file result. |
| 2791 ** So make sure that the regFree1 register is not reused for other | 3046 ** So make sure that the regFree1 register is not reused for other |
| 2792 ** purposes and possibly overwritten. */ | 3047 ** purposes and possibly overwritten. */ |
| 2793 regFree1 = 0; | 3048 regFree1 = 0; |
| 2794 } | 3049 } |
| 2795 for(i=0; i<nExpr; i=i+2){ | 3050 for(i=0; i<nExpr-1; i=i+2){ |
| 2796 sqlite3ExprCachePush(pParse); | 3051 sqlite3ExprCachePush(pParse); |
| 2797 if( pX ){ | 3052 if( pX ){ |
| 2798 assert( pTest!=0 ); | 3053 assert( pTest!=0 ); |
| 2799 opCompare.pRight = aListelem[i].pExpr; | 3054 opCompare.pRight = aListelem[i].pExpr; |
| 2800 }else{ | 3055 }else{ |
| 2801 pTest = aListelem[i].pExpr; | 3056 pTest = aListelem[i].pExpr; |
| 2802 } | 3057 } |
| 2803 nextCase = sqlite3VdbeMakeLabel(v); | 3058 nextCase = sqlite3VdbeMakeLabel(v); |
| 2804 testcase( pTest->op==TK_COLUMN ); | 3059 testcase( pTest->op==TK_COLUMN ); |
| 2805 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); | 3060 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); |
| 2806 testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); | 3061 testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); |
| 2807 testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); | |
| 2808 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); | 3062 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); |
| 2809 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); | 3063 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); |
| 2810 sqlite3ExprCachePop(pParse, 1); | 3064 sqlite3ExprCachePop(pParse); |
| 2811 sqlite3VdbeResolveLabel(v, nextCase); | 3065 sqlite3VdbeResolveLabel(v, nextCase); |
| 2812 } | 3066 } |
| 2813 if( pExpr->pRight ){ | 3067 if( (nExpr&1)!=0 ){ |
| 2814 sqlite3ExprCachePush(pParse); | 3068 sqlite3ExprCachePush(pParse); |
| 2815 sqlite3ExprCode(pParse, pExpr->pRight, target); | 3069 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); |
| 2816 sqlite3ExprCachePop(pParse, 1); | 3070 sqlite3ExprCachePop(pParse); |
| 2817 }else{ | 3071 }else{ |
| 2818 sqlite3VdbeAddOp2(v, OP_Null, 0, target); | 3072 sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
| 2819 } | 3073 } |
| 2820 assert( db->mallocFailed || pParse->nErr>0 | 3074 assert( db->mallocFailed || pParse->nErr>0 |
| 2821 || pParse->iCacheLevel==iCacheLevel ); | 3075 || pParse->iCacheLevel==iCacheLevel ); |
| 2822 sqlite3VdbeResolveLabel(v, endLabel); | 3076 sqlite3VdbeResolveLabel(v, endLabel); |
| 2823 break; | 3077 break; |
| 2824 } | 3078 } |
| 2825 #ifndef SQLITE_OMIT_TRIGGER | 3079 #ifndef SQLITE_OMIT_TRIGGER |
| 2826 case TK_RAISE: { | 3080 case TK_RAISE: { |
| 2827 assert( pExpr->affinity==OE_Rollback | 3081 assert( pExpr->affinity==OE_Rollback |
| 2828 || pExpr->affinity==OE_Abort | 3082 || pExpr->affinity==OE_Abort |
| 2829 || pExpr->affinity==OE_Fail | 3083 || pExpr->affinity==OE_Fail |
| 2830 || pExpr->affinity==OE_Ignore | 3084 || pExpr->affinity==OE_Ignore |
| 2831 ); | 3085 ); |
| 2832 if( !pParse->pTriggerTab ){ | 3086 if( !pParse->pTriggerTab ){ |
| 2833 sqlite3ErrorMsg(pParse, | 3087 sqlite3ErrorMsg(pParse, |
| 2834 "RAISE() may only be used within a trigger-program"); | 3088 "RAISE() may only be used within a trigger-program"); |
| 2835 return 0; | 3089 return 0; |
| 2836 } | 3090 } |
| 2837 if( pExpr->affinity==OE_Abort ){ | 3091 if( pExpr->affinity==OE_Abort ){ |
| 2838 sqlite3MayAbort(pParse); | 3092 sqlite3MayAbort(pParse); |
| 2839 } | 3093 } |
| 2840 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | 3094 assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 2841 if( pExpr->affinity==OE_Ignore ){ | 3095 if( pExpr->affinity==OE_Ignore ){ |
| 2842 sqlite3VdbeAddOp4( | 3096 sqlite3VdbeAddOp4( |
| 2843 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); | 3097 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); |
| 3098 VdbeCoverage(v); |
| 2844 }else{ | 3099 }else{ |
| 2845 sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0); | 3100 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, |
| 3101 pExpr->affinity, pExpr->u.zToken, 0, 0); |
| 2846 } | 3102 } |
| 2847 | 3103 |
| 2848 break; | 3104 break; |
| 2849 } | 3105 } |
| 2850 #endif | 3106 #endif |
| 2851 } | 3107 } |
| 2852 sqlite3ReleaseTempReg(pParse, regFree1); | 3108 sqlite3ReleaseTempReg(pParse, regFree1); |
| 2853 sqlite3ReleaseTempReg(pParse, regFree2); | 3109 sqlite3ReleaseTempReg(pParse, regFree2); |
| 2854 return inReg; | 3110 return inReg; |
| 2855 } | 3111 } |
| 2856 | 3112 |
| 2857 /* | 3113 /* |
| 3114 ** Factor out the code of the given expression to initialization time. |
| 3115 */ |
| 3116 void sqlite3ExprCodeAtInit( |
| 3117 Parse *pParse, /* Parsing context */ |
| 3118 Expr *pExpr, /* The expression to code when the VDBE initializes */ |
| 3119 int regDest, /* Store the value in this register */ |
| 3120 u8 reusable /* True if this expression is reusable */ |
| 3121 ){ |
| 3122 ExprList *p; |
| 3123 assert( ConstFactorOk(pParse) ); |
| 3124 p = pParse->pConstExpr; |
| 3125 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 3126 p = sqlite3ExprListAppend(pParse, p, pExpr); |
| 3127 if( p ){ |
| 3128 struct ExprList_item *pItem = &p->a[p->nExpr-1]; |
| 3129 pItem->u.iConstExprReg = regDest; |
| 3130 pItem->reusable = reusable; |
| 3131 } |
| 3132 pParse->pConstExpr = p; |
| 3133 } |
| 3134 |
| 3135 /* |
| 2858 ** Generate code to evaluate an expression and store the results | 3136 ** Generate code to evaluate an expression and store the results |
| 2859 ** into a register. Return the register number where the results | 3137 ** into a register. Return the register number where the results |
| 2860 ** are stored. | 3138 ** are stored. |
| 2861 ** | 3139 ** |
| 2862 ** If the register is a temporary register that can be deallocated, | 3140 ** If the register is a temporary register that can be deallocated, |
| 2863 ** then write its number into *pReg. If the result register is not | 3141 ** then write its number into *pReg. If the result register is not |
| 2864 ** a temporary, then set *pReg to zero. | 3142 ** a temporary, then set *pReg to zero. |
| 3143 ** |
| 3144 ** If pExpr is a constant, then this routine might generate this |
| 3145 ** code to fill the register in the initialization section of the |
| 3146 ** VDBE program, in order to factor it out of the evaluation loop. |
| 2865 */ | 3147 */ |
| 2866 int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ | 3148 int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ |
| 2867 int r1 = sqlite3GetTempReg(pParse); | 3149 int r2; |
| 2868 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); | 3150 pExpr = sqlite3ExprSkipCollate(pExpr); |
| 2869 if( r2==r1 ){ | 3151 if( ConstFactorOk(pParse) |
| 2870 *pReg = r1; | 3152 && pExpr->op!=TK_REGISTER |
| 3153 && sqlite3ExprIsConstantNotJoin(pExpr) |
| 3154 ){ |
| 3155 ExprList *p = pParse->pConstExpr; |
| 3156 int i; |
| 3157 *pReg = 0; |
| 3158 if( p ){ |
| 3159 struct ExprList_item *pItem; |
| 3160 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ |
| 3161 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ |
| 3162 return pItem->u.iConstExprReg; |
| 3163 } |
| 3164 } |
| 3165 } |
| 3166 r2 = ++pParse->nMem; |
| 3167 sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); |
| 2871 }else{ | 3168 }else{ |
| 2872 sqlite3ReleaseTempReg(pParse, r1); | 3169 int r1 = sqlite3GetTempReg(pParse); |
| 2873 *pReg = 0; | 3170 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 3171 if( r2==r1 ){ |
| 3172 *pReg = r1; |
| 3173 }else{ |
| 3174 sqlite3ReleaseTempReg(pParse, r1); |
| 3175 *pReg = 0; |
| 3176 } |
| 2874 } | 3177 } |
| 2875 return r2; | 3178 return r2; |
| 2876 } | 3179 } |
| 2877 | 3180 |
| 2878 /* | 3181 /* |
| 2879 ** Generate code that will evaluate expression pExpr and store the | 3182 ** Generate code that will evaluate expression pExpr and store the |
| 2880 ** results in register target. The results are guaranteed to appear | 3183 ** results in register target. The results are guaranteed to appear |
| 2881 ** in register target. | 3184 ** in register target. |
| 2882 */ | 3185 */ |
| 2883 int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ | 3186 void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
| 2884 int inReg; | 3187 int inReg; |
| 2885 | 3188 |
| 2886 assert( target>0 && target<=pParse->nMem ); | 3189 assert( target>0 && target<=pParse->nMem ); |
| 2887 if( pExpr && pExpr->op==TK_REGISTER ){ | 3190 if( pExpr && pExpr->op==TK_REGISTER ){ |
| 2888 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); | 3191 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); |
| 2889 }else{ | 3192 }else{ |
| 2890 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); | 3193 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
| 2891 assert( pParse->pVdbe || pParse->db->mallocFailed ); | 3194 assert( pParse->pVdbe || pParse->db->mallocFailed ); |
| 2892 if( inReg!=target && pParse->pVdbe ){ | 3195 if( inReg!=target && pParse->pVdbe ){ |
| 2893 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); | 3196 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); |
| 2894 } | 3197 } |
| 2895 } | 3198 } |
| 2896 return target; | |
| 2897 } | 3199 } |
| 2898 | 3200 |
| 2899 /* | 3201 /* |
| 2900 ** Generate code that evalutes the given expression and puts the result | 3202 ** Generate code that will evaluate expression pExpr and store the |
| 3203 ** results in register target. The results are guaranteed to appear |
| 3204 ** in register target. If the expression is constant, then this routine |
| 3205 ** might choose to code the expression at initialization time. |
| 3206 */ |
| 3207 void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ |
| 3208 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ |
| 3209 sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); |
| 3210 }else{ |
| 3211 sqlite3ExprCode(pParse, pExpr, target); |
| 3212 } |
| 3213 } |
| 3214 |
| 3215 /* |
| 3216 ** Generate code that evaluates the given expression and puts the result |
| 2901 ** in register target. | 3217 ** in register target. |
| 2902 ** | 3218 ** |
| 2903 ** Also make a copy of the expression results into another "cache" register | 3219 ** Also make a copy of the expression results into another "cache" register |
| 2904 ** and modify the expression so that the next time it is evaluated, | 3220 ** and modify the expression so that the next time it is evaluated, |
| 2905 ** the result is a copy of the cache register. | 3221 ** the result is a copy of the cache register. |
| 2906 ** | 3222 ** |
| 2907 ** This routine is used for expressions that are used multiple | 3223 ** This routine is used for expressions that are used multiple |
| 2908 ** times. They are evaluated once and the results of the expression | 3224 ** times. They are evaluated once and the results of the expression |
| 2909 ** are reused. | 3225 ** are reused. |
| 2910 */ | 3226 */ |
| 2911 int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ | 3227 void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ |
| 2912 Vdbe *v = pParse->pVdbe; | 3228 Vdbe *v = pParse->pVdbe; |
| 2913 int inReg; | 3229 int iMem; |
| 2914 inReg = sqlite3ExprCode(pParse, pExpr, target); | 3230 |
| 2915 assert( target>0 ); | 3231 assert( target>0 ); |
| 2916 /* This routine is called for terms to INSERT or UPDATE. And the only | 3232 assert( pExpr->op!=TK_REGISTER ); |
| 2917 ** other place where expressions can be converted into TK_REGISTER is | 3233 sqlite3ExprCode(pParse, pExpr, target); |
| 2918 ** in WHERE clause processing. So as currently implemented, there is | 3234 iMem = ++pParse->nMem; |
| 2919 ** no way for a TK_REGISTER to exist here. But it seems prudent to | 3235 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); |
| 2920 ** keep the ALWAYS() in case the conditions above change with future | 3236 exprToRegister(pExpr, iMem); |
| 2921 ** modifications or enhancements. */ | |
| 2922 if( ALWAYS(pExpr->op!=TK_REGISTER) ){ | |
| 2923 int iMem; | |
| 2924 iMem = ++pParse->nMem; | |
| 2925 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); | |
| 2926 pExpr->iTable = iMem; | |
| 2927 pExpr->op2 = pExpr->op; | |
| 2928 pExpr->op = TK_REGISTER; | |
| 2929 } | |
| 2930 return inReg; | |
| 2931 } | 3237 } |
| 2932 | 3238 |
| 3239 #ifdef SQLITE_DEBUG |
| 2933 /* | 3240 /* |
| 2934 ** Return TRUE if pExpr is an constant expression that is appropriate | 3241 ** Generate a human-readable explanation of an expression tree. |
| 2935 ** for factoring out of a loop. Appropriate expressions are: | |
| 2936 ** | |
| 2937 ** * Any expression that evaluates to two or more opcodes. | |
| 2938 ** | |
| 2939 ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, | |
| 2940 ** or OP_Variable that does not need to be placed in a | |
| 2941 ** specific register. | |
| 2942 ** | |
| 2943 ** There is no point in factoring out single-instruction constant | |
| 2944 ** expressions that need to be placed in a particular register. | |
| 2945 ** We could factor them out, but then we would end up adding an | |
| 2946 ** OP_SCopy instruction to move the value into the correct register | |
| 2947 ** later. We might as well just use the original instruction and | |
| 2948 ** avoid the OP_SCopy. | |
| 2949 */ | 3242 */ |
| 2950 static int isAppropriateForFactoring(Expr *p){ | 3243 void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ |
| 2951 if( !sqlite3ExprIsConstantNotJoin(p) ){ | 3244 const char *zBinOp = 0; /* Binary operator */ |
| 2952 return 0; /* Only constant expressions are appropriate for factoring */ | 3245 const char *zUniOp = 0; /* Unary operator */ |
| 2953 } | 3246 pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 2954 if( (p->flags & EP_FixedDest)==0 ){ | 3247 if( pExpr==0 ){ |
| 2955 return 1; /* Any constant without a fixed destination is appropriate */ | 3248 sqlite3TreeViewLine(pView, "nil"); |
| 2956 } | 3249 sqlite3TreeViewPop(pView); |
| 2957 while( p->op==TK_UPLUS ) p = p->pLeft; | 3250 return; |
| 2958 switch( p->op ){ | 3251 } |
| 3252 switch( pExpr->op ){ |
| 3253 case TK_AGG_COLUMN: { |
| 3254 sqlite3TreeViewLine(pView, "AGG{%d:%d}", |
| 3255 pExpr->iTable, pExpr->iColumn); |
| 3256 break; |
| 3257 } |
| 3258 case TK_COLUMN: { |
| 3259 if( pExpr->iTable<0 ){ |
| 3260 /* This only happens when coding check constraints */ |
| 3261 sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn); |
| 3262 }else{ |
| 3263 sqlite3TreeViewLine(pView, "{%d:%d}", |
| 3264 pExpr->iTable, pExpr->iColumn); |
| 3265 } |
| 3266 break; |
| 3267 } |
| 3268 case TK_INTEGER: { |
| 3269 if( pExpr->flags & EP_IntValue ){ |
| 3270 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); |
| 3271 }else{ |
| 3272 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); |
| 3273 } |
| 3274 break; |
| 3275 } |
| 3276 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 3277 case TK_FLOAT: { |
| 3278 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 3279 break; |
| 3280 } |
| 3281 #endif |
| 3282 case TK_STRING: { |
| 3283 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); |
| 3284 break; |
| 3285 } |
| 3286 case TK_NULL: { |
| 3287 sqlite3TreeViewLine(pView,"NULL"); |
| 3288 break; |
| 3289 } |
| 2959 #ifndef SQLITE_OMIT_BLOB_LITERAL | 3290 #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 2960 case TK_BLOB: | 3291 case TK_BLOB: { |
| 3292 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 3293 break; |
| 3294 } |
| 2961 #endif | 3295 #endif |
| 2962 case TK_VARIABLE: | 3296 case TK_VARIABLE: { |
| 2963 case TK_INTEGER: | 3297 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", |
| 2964 case TK_FLOAT: | 3298 pExpr->u.zToken, pExpr->iColumn); |
| 2965 case TK_NULL: | 3299 break; |
| 2966 case TK_STRING: { | 3300 } |
| 2967 testcase( p->op==TK_BLOB ); | 3301 case TK_REGISTER: { |
| 2968 testcase( p->op==TK_VARIABLE ); | 3302 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); |
| 2969 testcase( p->op==TK_INTEGER ); | 3303 break; |
| 2970 testcase( p->op==TK_FLOAT ); | 3304 } |
| 2971 testcase( p->op==TK_NULL ); | 3305 case TK_AS: { |
| 2972 testcase( p->op==TK_STRING ); | 3306 sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken); |
| 2973 /* Single-instruction constants with a fixed destination are | 3307 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 2974 ** better done in-line. If we factor them, they will just end | 3308 break; |
| 2975 ** up generating an OP_SCopy to move the value to the destination | 3309 } |
| 2976 ** register. */ | 3310 case TK_ID: { |
| 2977 return 0; | 3311 sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken); |
| 2978 } | 3312 break; |
| 2979 case TK_UMINUS: { | 3313 } |
| 2980 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ | 3314 #ifndef SQLITE_OMIT_CAST |
| 2981 return 0; | 3315 case TK_CAST: { |
| 2982 } | 3316 /* Expressions of the form: CAST(pLeft AS token) */ |
| 2983 break; | 3317 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); |
| 2984 } | 3318 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 3319 break; |
| 3320 } |
| 3321 #endif /* SQLITE_OMIT_CAST */ |
| 3322 case TK_LT: zBinOp = "LT"; break; |
| 3323 case TK_LE: zBinOp = "LE"; break; |
| 3324 case TK_GT: zBinOp = "GT"; break; |
| 3325 case TK_GE: zBinOp = "GE"; break; |
| 3326 case TK_NE: zBinOp = "NE"; break; |
| 3327 case TK_EQ: zBinOp = "EQ"; break; |
| 3328 case TK_IS: zBinOp = "IS"; break; |
| 3329 case TK_ISNOT: zBinOp = "ISNOT"; break; |
| 3330 case TK_AND: zBinOp = "AND"; break; |
| 3331 case TK_OR: zBinOp = "OR"; break; |
| 3332 case TK_PLUS: zBinOp = "ADD"; break; |
| 3333 case TK_STAR: zBinOp = "MUL"; break; |
| 3334 case TK_MINUS: zBinOp = "SUB"; break; |
| 3335 case TK_REM: zBinOp = "REM"; break; |
| 3336 case TK_BITAND: zBinOp = "BITAND"; break; |
| 3337 case TK_BITOR: zBinOp = "BITOR"; break; |
| 3338 case TK_SLASH: zBinOp = "DIV"; break; |
| 3339 case TK_LSHIFT: zBinOp = "LSHIFT"; break; |
| 3340 case TK_RSHIFT: zBinOp = "RSHIFT"; break; |
| 3341 case TK_CONCAT: zBinOp = "CONCAT"; break; |
| 3342 case TK_DOT: zBinOp = "DOT"; break; |
| 3343 |
| 3344 case TK_UMINUS: zUniOp = "UMINUS"; break; |
| 3345 case TK_UPLUS: zUniOp = "UPLUS"; break; |
| 3346 case TK_BITNOT: zUniOp = "BITNOT"; break; |
| 3347 case TK_NOT: zUniOp = "NOT"; break; |
| 3348 case TK_ISNULL: zUniOp = "ISNULL"; break; |
| 3349 case TK_NOTNULL: zUniOp = "NOTNULL"; break; |
| 3350 |
| 3351 case TK_COLLATE: { |
| 3352 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); |
| 3353 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 3354 break; |
| 3355 } |
| 3356 |
| 3357 case TK_AGG_FUNCTION: |
| 3358 case TK_FUNCTION: { |
| 3359 ExprList *pFarg; /* List of function arguments */ |
| 3360 if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 3361 pFarg = 0; |
| 3362 }else{ |
| 3363 pFarg = pExpr->x.pList; |
| 3364 } |
| 3365 if( pExpr->op==TK_AGG_FUNCTION ){ |
| 3366 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", |
| 3367 pExpr->op2, pExpr->u.zToken); |
| 3368 }else{ |
| 3369 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); |
| 3370 } |
| 3371 if( pFarg ){ |
| 3372 sqlite3TreeViewExprList(pView, pFarg, 0, 0); |
| 3373 } |
| 3374 break; |
| 3375 } |
| 3376 #ifndef SQLITE_OMIT_SUBQUERY |
| 3377 case TK_EXISTS: { |
| 3378 sqlite3TreeViewLine(pView, "EXISTS-expr"); |
| 3379 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 3380 break; |
| 3381 } |
| 3382 case TK_SELECT: { |
| 3383 sqlite3TreeViewLine(pView, "SELECT-expr"); |
| 3384 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 3385 break; |
| 3386 } |
| 3387 case TK_IN: { |
| 3388 sqlite3TreeViewLine(pView, "IN"); |
| 3389 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 3390 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 3391 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 3392 }else{ |
| 3393 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 3394 } |
| 3395 break; |
| 3396 } |
| 3397 #endif /* SQLITE_OMIT_SUBQUERY */ |
| 3398 |
| 3399 /* |
| 3400 ** x BETWEEN y AND z |
| 3401 ** |
| 3402 ** This is equivalent to |
| 3403 ** |
| 3404 ** x>=y AND x<=z |
| 3405 ** |
| 3406 ** X is stored in pExpr->pLeft. |
| 3407 ** Y is stored in pExpr->pList->a[0].pExpr. |
| 3408 ** Z is stored in pExpr->pList->a[1].pExpr. |
| 3409 */ |
| 3410 case TK_BETWEEN: { |
| 3411 Expr *pX = pExpr->pLeft; |
| 3412 Expr *pY = pExpr->x.pList->a[0].pExpr; |
| 3413 Expr *pZ = pExpr->x.pList->a[1].pExpr; |
| 3414 sqlite3TreeViewLine(pView, "BETWEEN"); |
| 3415 sqlite3TreeViewExpr(pView, pX, 1); |
| 3416 sqlite3TreeViewExpr(pView, pY, 1); |
| 3417 sqlite3TreeViewExpr(pView, pZ, 0); |
| 3418 break; |
| 3419 } |
| 3420 case TK_TRIGGER: { |
| 3421 /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 3422 ** to a column in the new.* or old.* pseudo-tables available to |
| 3423 ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 3424 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 3425 ** is set to the column of the pseudo-table to read, or to -1 to |
| 3426 ** read the rowid field. |
| 3427 */ |
| 3428 sqlite3TreeViewLine(pView, "%s(%d)", |
| 3429 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); |
| 3430 break; |
| 3431 } |
| 3432 case TK_CASE: { |
| 3433 sqlite3TreeViewLine(pView, "CASE"); |
| 3434 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 3435 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 3436 break; |
| 3437 } |
| 3438 #ifndef SQLITE_OMIT_TRIGGER |
| 3439 case TK_RAISE: { |
| 3440 const char *zType = "unk"; |
| 3441 switch( pExpr->affinity ){ |
| 3442 case OE_Rollback: zType = "rollback"; break; |
| 3443 case OE_Abort: zType = "abort"; break; |
| 3444 case OE_Fail: zType = "fail"; break; |
| 3445 case OE_Ignore: zType = "ignore"; break; |
| 3446 } |
| 3447 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); |
| 3448 break; |
| 3449 } |
| 3450 #endif |
| 2985 default: { | 3451 default: { |
| 2986 break; | 3452 sqlite3TreeViewLine(pView, "op=%d", pExpr->op); |
| 2987 } | 3453 break; |
| 2988 } | 3454 } |
| 2989 return 1; | 3455 } |
| 3456 if( zBinOp ){ |
| 3457 sqlite3TreeViewLine(pView, "%s", zBinOp); |
| 3458 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 3459 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 3460 }else if( zUniOp ){ |
| 3461 sqlite3TreeViewLine(pView, "%s", zUniOp); |
| 3462 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 3463 } |
| 3464 sqlite3TreeViewPop(pView); |
| 2990 } | 3465 } |
| 2991 | 3466 #endif /* SQLITE_DEBUG */ |
| 3467 |
| 3468 #ifdef SQLITE_DEBUG |
| 2992 /* | 3469 /* |
| 2993 ** If pExpr is a constant expression that is appropriate for | 3470 ** Generate a human-readable explanation of an expression list. |
| 2994 ** factoring out of a loop, then evaluate the expression | |
| 2995 ** into a register and convert the expression into a TK_REGISTER | |
| 2996 ** expression. | |
| 2997 */ | 3471 */ |
| 2998 static int evalConstExpr(Walker *pWalker, Expr *pExpr){ | 3472 void sqlite3TreeViewExprList( |
| 2999 Parse *pParse = pWalker->pParse; | 3473 TreeView *pView, |
| 3000 switch( pExpr->op ){ | 3474 const ExprList *pList, |
| 3001 case TK_IN: | 3475 u8 moreToFollow, |
| 3002 case TK_REGISTER: { | 3476 const char *zLabel |
| 3003 return WRC_Prune; | 3477 ){ |
| 3004 } | 3478 int i; |
| 3005 case TK_FUNCTION: | 3479 pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 3006 case TK_AGG_FUNCTION: | 3480 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; |
| 3007 case TK_CONST_FUNC: { | 3481 if( pList==0 ){ |
| 3008 /* The arguments to a function have a fixed destination. | 3482 sqlite3TreeViewLine(pView, "%s (empty)", zLabel); |
| 3009 ** Mark them this way to avoid generated unneeded OP_SCopy | 3483 }else{ |
| 3010 ** instructions. | 3484 sqlite3TreeViewLine(pView, "%s", zLabel); |
| 3011 */ | 3485 for(i=0; i<pList->nExpr; i++){ |
| 3012 ExprList *pList = pExpr->x.pList; | 3486 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); |
| 3013 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | 3487 #if 0 |
| 3014 if( pList ){ | 3488 if( pList->a[i].zName ){ |
| 3015 int i = pList->nExpr; | 3489 sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName); |
| 3016 struct ExprList_item *pItem = pList->a; | 3490 } |
| 3017 for(; i>0; i--, pItem++){ | 3491 if( pList->a[i].bSpanIsTab ){ |
| 3018 if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest; | 3492 sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan); |
| 3019 } | 3493 } |
| 3020 } | 3494 #endif |
| 3021 break; | 3495 } |
| 3022 } | 3496 } |
| 3023 } | 3497 sqlite3TreeViewPop(pView); |
| 3024 if( isAppropriateForFactoring(pExpr) ){ | |
| 3025 int r1 = ++pParse->nMem; | |
| 3026 int r2; | |
| 3027 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); | |
| 3028 if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1); | |
| 3029 pExpr->op2 = pExpr->op; | |
| 3030 pExpr->op = TK_REGISTER; | |
| 3031 pExpr->iTable = r2; | |
| 3032 return WRC_Prune; | |
| 3033 } | |
| 3034 return WRC_Continue; | |
| 3035 } | 3498 } |
| 3036 | 3499 #endif /* SQLITE_DEBUG */ |
| 3037 /* | |
| 3038 ** Preevaluate constant subexpressions within pExpr and store the | |
| 3039 ** results in registers. Modify pExpr so that the constant subexpresions | |
| 3040 ** are TK_REGISTER opcodes that refer to the precomputed values. | |
| 3041 ** | |
| 3042 ** This routine is a no-op if the jump to the cookie-check code has | |
| 3043 ** already occur. Since the cookie-check jump is generated prior to | |
| 3044 ** any other serious processing, this check ensures that there is no | |
| 3045 ** way to accidently bypass the constant initializations. | |
| 3046 ** | |
| 3047 ** This routine is also a no-op if the SQLITE_FactorOutConst optimization | |
| 3048 ** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS) | |
| 3049 ** interface. This allows test logic to verify that the same answer is | |
| 3050 ** obtained for queries regardless of whether or not constants are | |
| 3051 ** precomputed into registers or if they are inserted in-line. | |
| 3052 */ | |
| 3053 void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ | |
| 3054 Walker w; | |
| 3055 if( pParse->cookieGoto ) return; | |
| 3056 if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return; | |
| 3057 w.xExprCallback = evalConstExpr; | |
| 3058 w.xSelectCallback = 0; | |
| 3059 w.pParse = pParse; | |
| 3060 sqlite3WalkExpr(&w, pExpr); | |
| 3061 } | |
| 3062 | |
| 3063 | 3500 |
| 3064 /* | 3501 /* |
| 3065 ** Generate code that pushes the value of every element of the given | 3502 ** Generate code that pushes the value of every element of the given |
| 3066 ** expression list into a sequence of registers beginning at target. | 3503 ** expression list into a sequence of registers beginning at target. |
| 3067 ** | 3504 ** |
| 3068 ** Return the number of elements evaluated. | 3505 ** Return the number of elements evaluated. |
| 3506 ** |
| 3507 ** The SQLITE_ECEL_DUP flag prevents the arguments from being |
| 3508 ** filled using OP_SCopy. OP_Copy must be used instead. |
| 3509 ** |
| 3510 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be |
| 3511 ** factored out into initialization code. |
| 3069 */ | 3512 */ |
| 3070 int sqlite3ExprCodeExprList( | 3513 int sqlite3ExprCodeExprList( |
| 3071 Parse *pParse, /* Parsing context */ | 3514 Parse *pParse, /* Parsing context */ |
| 3072 ExprList *pList, /* The expression list to be coded */ | 3515 ExprList *pList, /* The expression list to be coded */ |
| 3073 int target, /* Where to write results */ | 3516 int target, /* Where to write results */ |
| 3074 int doHardCopy /* Make a hard copy of every element */ | 3517 u8 flags /* SQLITE_ECEL_* flags */ |
| 3075 ){ | 3518 ){ |
| 3076 struct ExprList_item *pItem; | 3519 struct ExprList_item *pItem; |
| 3077 int i, n; | 3520 int i, n; |
| 3521 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; |
| 3078 assert( pList!=0 ); | 3522 assert( pList!=0 ); |
| 3079 assert( target>0 ); | 3523 assert( target>0 ); |
| 3080 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ | 3524 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ |
| 3081 n = pList->nExpr; | 3525 n = pList->nExpr; |
| 3526 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; |
| 3082 for(pItem=pList->a, i=0; i<n; i++, pItem++){ | 3527 for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
| 3083 Expr *pExpr = pItem->pExpr; | 3528 Expr *pExpr = pItem->pExpr; |
| 3084 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); | 3529 if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ |
| 3085 if( inReg!=target+i ){ | 3530 sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); |
| 3086 sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy, | 3531 }else{ |
| 3087 inReg, target+i); | 3532 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); |
| 3533 if( inReg!=target+i ){ |
| 3534 VdbeOp *pOp; |
| 3535 Vdbe *v = pParse->pVdbe; |
| 3536 if( copyOp==OP_Copy |
| 3537 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy |
| 3538 && pOp->p1+pOp->p3+1==inReg |
| 3539 && pOp->p2+pOp->p3+1==target+i |
| 3540 ){ |
| 3541 pOp->p3++; |
| 3542 }else{ |
| 3543 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); |
| 3544 } |
| 3545 } |
| 3088 } | 3546 } |
| 3089 } | 3547 } |
| 3090 return n; | 3548 return n; |
| 3091 } | 3549 } |
| 3092 | 3550 |
| 3093 /* | 3551 /* |
| 3094 ** Generate code for a BETWEEN operator. | 3552 ** Generate code for a BETWEEN operator. |
| 3095 ** | 3553 ** |
| 3096 ** x BETWEEN y AND z | 3554 ** x BETWEEN y AND z |
| 3097 ** | 3555 ** |
| 3098 ** The above is equivalent to | 3556 ** The above is equivalent to |
| 3099 ** | 3557 ** |
| 3100 ** x>=y AND x<=z | 3558 ** x>=y AND x<=z |
| 3101 ** | 3559 ** |
| 3102 ** Code it as such, taking care to do the common subexpression | 3560 ** Code it as such, taking care to do the common subexpression |
| 3103 ** elementation of x. | 3561 ** elimination of x. |
| 3104 */ | 3562 */ |
| 3105 static void exprCodeBetween( | 3563 static void exprCodeBetween( |
| 3106 Parse *pParse, /* Parsing and code generating context */ | 3564 Parse *pParse, /* Parsing and code generating context */ |
| 3107 Expr *pExpr, /* The BETWEEN expression */ | 3565 Expr *pExpr, /* The BETWEEN expression */ |
| 3108 int dest, /* Jump here if the jump is taken */ | 3566 int dest, /* Jump here if the jump is taken */ |
| 3109 int jumpIfTrue, /* Take the jump if the BETWEEN is true */ | 3567 int jumpIfTrue, /* Take the jump if the BETWEEN is true */ |
| 3110 int jumpIfNull /* Take the jump if the BETWEEN is NULL */ | 3568 int jumpIfNull /* Take the jump if the BETWEEN is NULL */ |
| 3111 ){ | 3569 ){ |
| 3112 Expr exprAnd; /* The AND operator in x>=y AND x<=z */ | 3570 Expr exprAnd; /* The AND operator in x>=y AND x<=z */ |
| 3113 Expr compLeft; /* The x>=y term */ | 3571 Expr compLeft; /* The x>=y term */ |
| 3114 Expr compRight; /* The x<=z term */ | 3572 Expr compRight; /* The x<=z term */ |
| 3115 Expr exprX; /* The x subexpression */ | 3573 Expr exprX; /* The x subexpression */ |
| 3116 int regFree1 = 0; /* Temporary use register */ | 3574 int regFree1 = 0; /* Temporary use register */ |
| 3117 | 3575 |
| 3118 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | 3576 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 3119 exprX = *pExpr->pLeft; | 3577 exprX = *pExpr->pLeft; |
| 3120 exprAnd.op = TK_AND; | 3578 exprAnd.op = TK_AND; |
| 3121 exprAnd.pLeft = &compLeft; | 3579 exprAnd.pLeft = &compLeft; |
| 3122 exprAnd.pRight = &compRight; | 3580 exprAnd.pRight = &compRight; |
| 3123 compLeft.op = TK_GE; | 3581 compLeft.op = TK_GE; |
| 3124 compLeft.pLeft = &exprX; | 3582 compLeft.pLeft = &exprX; |
| 3125 compLeft.pRight = pExpr->x.pList->a[0].pExpr; | 3583 compLeft.pRight = pExpr->x.pList->a[0].pExpr; |
| 3126 compRight.op = TK_LE; | 3584 compRight.op = TK_LE; |
| 3127 compRight.pLeft = &exprX; | 3585 compRight.pLeft = &exprX; |
| 3128 compRight.pRight = pExpr->x.pList->a[1].pExpr; | 3586 compRight.pRight = pExpr->x.pList->a[1].pExpr; |
| 3129 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); | 3587 exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, ®Free1)); |
| 3130 exprX.op = TK_REGISTER; | |
| 3131 if( jumpIfTrue ){ | 3588 if( jumpIfTrue ){ |
| 3132 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); | 3589 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); |
| 3133 }else{ | 3590 }else{ |
| 3134 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); | 3591 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); |
| 3135 } | 3592 } |
| 3136 sqlite3ReleaseTempReg(pParse, regFree1); | 3593 sqlite3ReleaseTempReg(pParse, regFree1); |
| 3137 | 3594 |
| 3138 /* Ensure adequate test coverage */ | 3595 /* Ensure adequate test coverage */ |
| 3139 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); | 3596 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); |
| 3140 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); | 3597 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 3161 ** below verify that the numbers are aligned correctly. | 3618 ** below verify that the numbers are aligned correctly. |
| 3162 */ | 3619 */ |
| 3163 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ | 3620 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
| 3164 Vdbe *v = pParse->pVdbe; | 3621 Vdbe *v = pParse->pVdbe; |
| 3165 int op = 0; | 3622 int op = 0; |
| 3166 int regFree1 = 0; | 3623 int regFree1 = 0; |
| 3167 int regFree2 = 0; | 3624 int regFree2 = 0; |
| 3168 int r1, r2; | 3625 int r1, r2; |
| 3169 | 3626 |
| 3170 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); | 3627 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
| 3171 if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */ | 3628 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ |
| 3172 if( NEVER(pExpr==0) ) return; /* No way this can happen */ | 3629 if( NEVER(pExpr==0) ) return; /* No way this can happen */ |
| 3173 op = pExpr->op; | 3630 op = pExpr->op; |
| 3174 switch( op ){ | 3631 switch( op ){ |
| 3175 case TK_AND: { | 3632 case TK_AND: { |
| 3176 int d2 = sqlite3VdbeMakeLabel(v); | 3633 int d2 = sqlite3VdbeMakeLabel(v); |
| 3177 testcase( jumpIfNull==0 ); | 3634 testcase( jumpIfNull==0 ); |
| 3635 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); |
| 3178 sqlite3ExprCachePush(pParse); | 3636 sqlite3ExprCachePush(pParse); |
| 3179 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); | |
| 3180 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); | 3637 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 3181 sqlite3VdbeResolveLabel(v, d2); | 3638 sqlite3VdbeResolveLabel(v, d2); |
| 3182 sqlite3ExprCachePop(pParse, 1); | 3639 sqlite3ExprCachePop(pParse); |
| 3183 break; | 3640 break; |
| 3184 } | 3641 } |
| 3185 case TK_OR: { | 3642 case TK_OR: { |
| 3186 testcase( jumpIfNull==0 ); | 3643 testcase( jumpIfNull==0 ); |
| 3187 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); | 3644 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 3645 sqlite3ExprCachePush(pParse); |
| 3188 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); | 3646 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 3647 sqlite3ExprCachePop(pParse); |
| 3189 break; | 3648 break; |
| 3190 } | 3649 } |
| 3191 case TK_NOT: { | 3650 case TK_NOT: { |
| 3192 testcase( jumpIfNull==0 ); | 3651 testcase( jumpIfNull==0 ); |
| 3193 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); | 3652 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 3194 break; | 3653 break; |
| 3195 } | 3654 } |
| 3196 case TK_LT: | 3655 case TK_LT: |
| 3197 case TK_LE: | 3656 case TK_LE: |
| 3198 case TK_GT: | 3657 case TK_GT: |
| 3199 case TK_GE: | 3658 case TK_GE: |
| 3200 case TK_NE: | 3659 case TK_NE: |
| 3201 case TK_EQ: { | 3660 case TK_EQ: { |
| 3202 assert( TK_LT==OP_Lt ); | |
| 3203 assert( TK_LE==OP_Le ); | |
| 3204 assert( TK_GT==OP_Gt ); | |
| 3205 assert( TK_GE==OP_Ge ); | |
| 3206 assert( TK_EQ==OP_Eq ); | |
| 3207 assert( TK_NE==OP_Ne ); | |
| 3208 testcase( op==TK_LT ); | |
| 3209 testcase( op==TK_LE ); | |
| 3210 testcase( op==TK_GT ); | |
| 3211 testcase( op==TK_GE ); | |
| 3212 testcase( op==TK_EQ ); | |
| 3213 testcase( op==TK_NE ); | |
| 3214 testcase( jumpIfNull==0 ); | 3661 testcase( jumpIfNull==0 ); |
| 3215 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 3662 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3216 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); | 3663 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
| 3217 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | 3664 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
| 3218 r1, r2, dest, jumpIfNull); | 3665 r1, r2, dest, jumpIfNull); |
| 3666 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
| 3667 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
| 3668 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
| 3669 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
| 3670 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); |
| 3671 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); |
| 3219 testcase( regFree1==0 ); | 3672 testcase( regFree1==0 ); |
| 3220 testcase( regFree2==0 ); | 3673 testcase( regFree2==0 ); |
| 3221 break; | 3674 break; |
| 3222 } | 3675 } |
| 3223 case TK_IS: | 3676 case TK_IS: |
| 3224 case TK_ISNOT: { | 3677 case TK_ISNOT: { |
| 3225 testcase( op==TK_IS ); | 3678 testcase( op==TK_IS ); |
| 3226 testcase( op==TK_ISNOT ); | 3679 testcase( op==TK_ISNOT ); |
| 3227 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 3680 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3228 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); | 3681 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
| 3229 op = (op==TK_IS) ? TK_EQ : TK_NE; | 3682 op = (op==TK_IS) ? TK_EQ : TK_NE; |
| 3230 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | 3683 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
| 3231 r1, r2, dest, SQLITE_NULLEQ); | 3684 r1, r2, dest, SQLITE_NULLEQ); |
| 3685 VdbeCoverageIf(v, op==TK_EQ); |
| 3686 VdbeCoverageIf(v, op==TK_NE); |
| 3232 testcase( regFree1==0 ); | 3687 testcase( regFree1==0 ); |
| 3233 testcase( regFree2==0 ); | 3688 testcase( regFree2==0 ); |
| 3234 break; | 3689 break; |
| 3235 } | 3690 } |
| 3236 case TK_ISNULL: | 3691 case TK_ISNULL: |
| 3237 case TK_NOTNULL: { | 3692 case TK_NOTNULL: { |
| 3238 assert( TK_ISNULL==OP_IsNull ); | 3693 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); |
| 3239 assert( TK_NOTNULL==OP_NotNull ); | 3694 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); |
| 3240 testcase( op==TK_ISNULL ); | |
| 3241 testcase( op==TK_NOTNULL ); | |
| 3242 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 3695 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3243 sqlite3VdbeAddOp2(v, op, r1, dest); | 3696 sqlite3VdbeAddOp2(v, op, r1, dest); |
| 3697 VdbeCoverageIf(v, op==TK_ISNULL); |
| 3698 VdbeCoverageIf(v, op==TK_NOTNULL); |
| 3244 testcase( regFree1==0 ); | 3699 testcase( regFree1==0 ); |
| 3245 break; | 3700 break; |
| 3246 } | 3701 } |
| 3247 case TK_BETWEEN: { | 3702 case TK_BETWEEN: { |
| 3248 testcase( jumpIfNull==0 ); | 3703 testcase( jumpIfNull==0 ); |
| 3249 exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull); | 3704 exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull); |
| 3250 break; | 3705 break; |
| 3251 } | 3706 } |
| 3252 #ifndef SQLITE_OMIT_SUBQUERY | 3707 #ifndef SQLITE_OMIT_SUBQUERY |
| 3253 case TK_IN: { | 3708 case TK_IN: { |
| 3254 int destIfFalse = sqlite3VdbeMakeLabel(v); | 3709 int destIfFalse = sqlite3VdbeMakeLabel(v); |
| 3255 int destIfNull = jumpIfNull ? dest : destIfFalse; | 3710 int destIfNull = jumpIfNull ? dest : destIfFalse; |
| 3256 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); | 3711 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); |
| 3257 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); | 3712 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); |
| 3258 sqlite3VdbeResolveLabel(v, destIfFalse); | 3713 sqlite3VdbeResolveLabel(v, destIfFalse); |
| 3259 break; | 3714 break; |
| 3260 } | 3715 } |
| 3261 #endif | 3716 #endif |
| 3262 default: { | 3717 default: { |
| 3263 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); | 3718 if( exprAlwaysTrue(pExpr) ){ |
| 3264 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); | 3719 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); |
| 3265 testcase( regFree1==0 ); | 3720 }else if( exprAlwaysFalse(pExpr) ){ |
| 3266 testcase( jumpIfNull==0 ); | 3721 /* No-op */ |
| 3722 }else{ |
| 3723 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 3724 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); |
| 3725 VdbeCoverage(v); |
| 3726 testcase( regFree1==0 ); |
| 3727 testcase( jumpIfNull==0 ); |
| 3728 } |
| 3267 break; | 3729 break; |
| 3268 } | 3730 } |
| 3269 } | 3731 } |
| 3270 sqlite3ReleaseTempReg(pParse, regFree1); | 3732 sqlite3ReleaseTempReg(pParse, regFree1); |
| 3271 sqlite3ReleaseTempReg(pParse, regFree2); | 3733 sqlite3ReleaseTempReg(pParse, regFree2); |
| 3272 } | 3734 } |
| 3273 | 3735 |
| 3274 /* | 3736 /* |
| 3275 ** Generate code for a boolean expression such that a jump is made | 3737 ** Generate code for a boolean expression such that a jump is made |
| 3276 ** to the label "dest" if the expression is false but execution | 3738 ** to the label "dest" if the expression is false but execution |
| 3277 ** continues straight thru if the expression is true. | 3739 ** continues straight thru if the expression is true. |
| 3278 ** | 3740 ** |
| 3279 ** If the expression evaluates to NULL (neither true nor false) then | 3741 ** If the expression evaluates to NULL (neither true nor false) then |
| 3280 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull | 3742 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull |
| 3281 ** is 0. | 3743 ** is 0. |
| 3282 */ | 3744 */ |
| 3283 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ | 3745 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
| 3284 Vdbe *v = pParse->pVdbe; | 3746 Vdbe *v = pParse->pVdbe; |
| 3285 int op = 0; | 3747 int op = 0; |
| 3286 int regFree1 = 0; | 3748 int regFree1 = 0; |
| 3287 int regFree2 = 0; | 3749 int regFree2 = 0; |
| 3288 int r1, r2; | 3750 int r1, r2; |
| 3289 | 3751 |
| 3290 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); | 3752 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
| 3291 if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */ | 3753 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ |
| 3292 if( pExpr==0 ) return; | 3754 if( pExpr==0 ) return; |
| 3293 | 3755 |
| 3294 /* The value of pExpr->op and op are related as follows: | 3756 /* The value of pExpr->op and op are related as follows: |
| 3295 ** | 3757 ** |
| 3296 ** pExpr->op op | 3758 ** pExpr->op op |
| 3297 ** --------- ---------- | 3759 ** --------- ---------- |
| 3298 ** TK_ISNULL OP_NotNull | 3760 ** TK_ISNULL OP_NotNull |
| 3299 ** TK_NOTNULL OP_IsNull | 3761 ** TK_NOTNULL OP_IsNull |
| 3300 ** TK_NE OP_Eq | 3762 ** TK_NE OP_Eq |
| 3301 ** TK_EQ OP_Ne | 3763 ** TK_EQ OP_Ne |
| (...skipping 17 matching lines...) Expand all Loading... |
| 3319 assert( pExpr->op!=TK_EQ || op==OP_Ne ); | 3781 assert( pExpr->op!=TK_EQ || op==OP_Ne ); |
| 3320 assert( pExpr->op!=TK_LT || op==OP_Ge ); | 3782 assert( pExpr->op!=TK_LT || op==OP_Ge ); |
| 3321 assert( pExpr->op!=TK_LE || op==OP_Gt ); | 3783 assert( pExpr->op!=TK_LE || op==OP_Gt ); |
| 3322 assert( pExpr->op!=TK_GT || op==OP_Le ); | 3784 assert( pExpr->op!=TK_GT || op==OP_Le ); |
| 3323 assert( pExpr->op!=TK_GE || op==OP_Lt ); | 3785 assert( pExpr->op!=TK_GE || op==OP_Lt ); |
| 3324 | 3786 |
| 3325 switch( pExpr->op ){ | 3787 switch( pExpr->op ){ |
| 3326 case TK_AND: { | 3788 case TK_AND: { |
| 3327 testcase( jumpIfNull==0 ); | 3789 testcase( jumpIfNull==0 ); |
| 3328 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); | 3790 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 3791 sqlite3ExprCachePush(pParse); |
| 3329 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); | 3792 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 3793 sqlite3ExprCachePop(pParse); |
| 3330 break; | 3794 break; |
| 3331 } | 3795 } |
| 3332 case TK_OR: { | 3796 case TK_OR: { |
| 3333 int d2 = sqlite3VdbeMakeLabel(v); | 3797 int d2 = sqlite3VdbeMakeLabel(v); |
| 3334 testcase( jumpIfNull==0 ); | 3798 testcase( jumpIfNull==0 ); |
| 3799 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); |
| 3335 sqlite3ExprCachePush(pParse); | 3800 sqlite3ExprCachePush(pParse); |
| 3336 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); | |
| 3337 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); | 3801 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 3338 sqlite3VdbeResolveLabel(v, d2); | 3802 sqlite3VdbeResolveLabel(v, d2); |
| 3339 sqlite3ExprCachePop(pParse, 1); | 3803 sqlite3ExprCachePop(pParse); |
| 3340 break; | 3804 break; |
| 3341 } | 3805 } |
| 3342 case TK_NOT: { | 3806 case TK_NOT: { |
| 3343 testcase( jumpIfNull==0 ); | 3807 testcase( jumpIfNull==0 ); |
| 3344 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); | 3808 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 3345 break; | 3809 break; |
| 3346 } | 3810 } |
| 3347 case TK_LT: | 3811 case TK_LT: |
| 3348 case TK_LE: | 3812 case TK_LE: |
| 3349 case TK_GT: | 3813 case TK_GT: |
| 3350 case TK_GE: | 3814 case TK_GE: |
| 3351 case TK_NE: | 3815 case TK_NE: |
| 3352 case TK_EQ: { | 3816 case TK_EQ: { |
| 3353 testcase( op==TK_LT ); | |
| 3354 testcase( op==TK_LE ); | |
| 3355 testcase( op==TK_GT ); | |
| 3356 testcase( op==TK_GE ); | |
| 3357 testcase( op==TK_EQ ); | |
| 3358 testcase( op==TK_NE ); | |
| 3359 testcase( jumpIfNull==0 ); | 3817 testcase( jumpIfNull==0 ); |
| 3360 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 3818 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3361 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); | 3819 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
| 3362 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | 3820 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
| 3363 r1, r2, dest, jumpIfNull); | 3821 r1, r2, dest, jumpIfNull); |
| 3822 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
| 3823 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
| 3824 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
| 3825 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
| 3826 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); |
| 3827 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); |
| 3364 testcase( regFree1==0 ); | 3828 testcase( regFree1==0 ); |
| 3365 testcase( regFree2==0 ); | 3829 testcase( regFree2==0 ); |
| 3366 break; | 3830 break; |
| 3367 } | 3831 } |
| 3368 case TK_IS: | 3832 case TK_IS: |
| 3369 case TK_ISNOT: { | 3833 case TK_ISNOT: { |
| 3370 testcase( pExpr->op==TK_IS ); | 3834 testcase( pExpr->op==TK_IS ); |
| 3371 testcase( pExpr->op==TK_ISNOT ); | 3835 testcase( pExpr->op==TK_ISNOT ); |
| 3372 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 3836 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3373 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); | 3837 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
| 3374 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; | 3838 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; |
| 3375 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, | 3839 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
| 3376 r1, r2, dest, SQLITE_NULLEQ); | 3840 r1, r2, dest, SQLITE_NULLEQ); |
| 3841 VdbeCoverageIf(v, op==TK_EQ); |
| 3842 VdbeCoverageIf(v, op==TK_NE); |
| 3377 testcase( regFree1==0 ); | 3843 testcase( regFree1==0 ); |
| 3378 testcase( regFree2==0 ); | 3844 testcase( regFree2==0 ); |
| 3379 break; | 3845 break; |
| 3380 } | 3846 } |
| 3381 case TK_ISNULL: | 3847 case TK_ISNULL: |
| 3382 case TK_NOTNULL: { | 3848 case TK_NOTNULL: { |
| 3383 testcase( op==TK_ISNULL ); | |
| 3384 testcase( op==TK_NOTNULL ); | |
| 3385 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); | 3849 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3386 sqlite3VdbeAddOp2(v, op, r1, dest); | 3850 sqlite3VdbeAddOp2(v, op, r1, dest); |
| 3851 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); |
| 3852 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); |
| 3387 testcase( regFree1==0 ); | 3853 testcase( regFree1==0 ); |
| 3388 break; | 3854 break; |
| 3389 } | 3855 } |
| 3390 case TK_BETWEEN: { | 3856 case TK_BETWEEN: { |
| 3391 testcase( jumpIfNull==0 ); | 3857 testcase( jumpIfNull==0 ); |
| 3392 exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull); | 3858 exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull); |
| 3393 break; | 3859 break; |
| 3394 } | 3860 } |
| 3395 #ifndef SQLITE_OMIT_SUBQUERY | 3861 #ifndef SQLITE_OMIT_SUBQUERY |
| 3396 case TK_IN: { | 3862 case TK_IN: { |
| 3397 if( jumpIfNull ){ | 3863 if( jumpIfNull ){ |
| 3398 sqlite3ExprCodeIN(pParse, pExpr, dest, dest); | 3864 sqlite3ExprCodeIN(pParse, pExpr, dest, dest); |
| 3399 }else{ | 3865 }else{ |
| 3400 int destIfNull = sqlite3VdbeMakeLabel(v); | 3866 int destIfNull = sqlite3VdbeMakeLabel(v); |
| 3401 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); | 3867 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); |
| 3402 sqlite3VdbeResolveLabel(v, destIfNull); | 3868 sqlite3VdbeResolveLabel(v, destIfNull); |
| 3403 } | 3869 } |
| 3404 break; | 3870 break; |
| 3405 } | 3871 } |
| 3406 #endif | 3872 #endif |
| 3407 default: { | 3873 default: { |
| 3408 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); | 3874 if( exprAlwaysFalse(pExpr) ){ |
| 3409 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); | 3875 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); |
| 3410 testcase( regFree1==0 ); | 3876 }else if( exprAlwaysTrue(pExpr) ){ |
| 3411 testcase( jumpIfNull==0 ); | 3877 /* no-op */ |
| 3878 }else{ |
| 3879 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 3880 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); |
| 3881 VdbeCoverage(v); |
| 3882 testcase( regFree1==0 ); |
| 3883 testcase( jumpIfNull==0 ); |
| 3884 } |
| 3412 break; | 3885 break; |
| 3413 } | 3886 } |
| 3414 } | 3887 } |
| 3415 sqlite3ReleaseTempReg(pParse, regFree1); | 3888 sqlite3ReleaseTempReg(pParse, regFree1); |
| 3416 sqlite3ReleaseTempReg(pParse, regFree2); | 3889 sqlite3ReleaseTempReg(pParse, regFree2); |
| 3417 } | 3890 } |
| 3418 | 3891 |
| 3419 /* | 3892 /* |
| 3420 ** Do a deep comparison of two expression trees. Return 0 if the two | 3893 ** Do a deep comparison of two expression trees. Return 0 if the two |
| 3421 ** expressions are completely identical. Return 1 if they differ only | 3894 ** expressions are completely identical. Return 1 if they differ only |
| 3422 ** by a COLLATE operator at the top level. Return 2 if there are differences | 3895 ** by a COLLATE operator at the top level. Return 2 if there are differences |
| 3423 ** other than the top-level COLLATE operator. | 3896 ** other than the top-level COLLATE operator. |
| 3424 ** | 3897 ** |
| 3898 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed |
| 3899 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. |
| 3900 ** |
| 3901 ** The pA side might be using TK_REGISTER. If that is the case and pB is |
| 3902 ** not using TK_REGISTER but is otherwise equivalent, then still return 0. |
| 3903 ** |
| 3425 ** Sometimes this routine will return 2 even if the two expressions | 3904 ** Sometimes this routine will return 2 even if the two expressions |
| 3426 ** really are equivalent. If we cannot prove that the expressions are | 3905 ** really are equivalent. If we cannot prove that the expressions are |
| 3427 ** identical, we return 2 just to be safe. So if this routine | 3906 ** identical, we return 2 just to be safe. So if this routine |
| 3428 ** returns 2, then you do not really know for certain if the two | 3907 ** returns 2, then you do not really know for certain if the two |
| 3429 ** expressions are the same. But if you get a 0 or 1 return, then you | 3908 ** expressions are the same. But if you get a 0 or 1 return, then you |
| 3430 ** can be sure the expressions are the same. In the places where | 3909 ** can be sure the expressions are the same. In the places where |
| 3431 ** this routine is used, it does not hurt to get an extra 2 - that | 3910 ** this routine is used, it does not hurt to get an extra 2 - that |
| 3432 ** just might result in some slightly slower code. But returning | 3911 ** just might result in some slightly slower code. But returning |
| 3433 ** an incorrect 0 or 1 could lead to a malfunction. | 3912 ** an incorrect 0 or 1 could lead to a malfunction. |
| 3434 */ | 3913 */ |
| 3435 int sqlite3ExprCompare(Expr *pA, Expr *pB){ | 3914 int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ |
| 3436 if( pA==0||pB==0 ){ | 3915 u32 combinedFlags; |
| 3916 if( pA==0 || pB==0 ){ |
| 3437 return pB==pA ? 0 : 2; | 3917 return pB==pA ? 0 : 2; |
| 3438 } | 3918 } |
| 3439 assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) ); | 3919 combinedFlags = pA->flags | pB->flags; |
| 3440 assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) ); | 3920 if( combinedFlags & EP_IntValue ){ |
| 3441 if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){ | 3921 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ |
| 3922 return 0; |
| 3923 } |
| 3442 return 2; | 3924 return 2; |
| 3443 } | 3925 } |
| 3444 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; | 3926 if( pA->op!=pB->op ){ |
| 3445 if( pA->op!=pB->op ) return 2; | 3927 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ |
| 3446 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2; | 3928 return 1; |
| 3447 if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2; | |
| 3448 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2; | |
| 3449 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2; | |
| 3450 if( ExprHasProperty(pA, EP_IntValue) ){ | |
| 3451 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){ | |
| 3452 return 2; | |
| 3453 } | 3929 } |
| 3454 }else if( pA->op!=TK_COLUMN && pA->u.zToken ){ | 3930 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ |
| 3455 if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2; | 3931 return 1; |
| 3456 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){ | 3932 } |
| 3457 return 2; | 3933 return 2; |
| 3934 } |
| 3935 if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){ |
| 3936 if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
| 3937 return pA->op==TK_COLLATE ? 1 : 2; |
| 3458 } | 3938 } |
| 3459 } | 3939 } |
| 3460 if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1; | 3940 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; |
| 3461 if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2; | 3941 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ |
| 3942 if( combinedFlags & EP_xIsSelect ) return 2; |
| 3943 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; |
| 3944 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; |
| 3945 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; |
| 3946 if( ALWAYS((combinedFlags & EP_Reduced)==0) ){ |
| 3947 if( pA->iColumn!=pB->iColumn ) return 2; |
| 3948 if( pA->iTable!=pB->iTable |
| 3949 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; |
| 3950 } |
| 3951 } |
| 3462 return 0; | 3952 return 0; |
| 3463 } | 3953 } |
| 3464 | 3954 |
| 3465 /* | 3955 /* |
| 3466 ** Compare two ExprList objects. Return 0 if they are identical and | 3956 ** Compare two ExprList objects. Return 0 if they are identical and |
| 3467 ** non-zero if they differ in any way. | 3957 ** non-zero if they differ in any way. |
| 3468 ** | 3958 ** |
| 3959 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed |
| 3960 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. |
| 3961 ** |
| 3469 ** This routine might return non-zero for equivalent ExprLists. The | 3962 ** This routine might return non-zero for equivalent ExprLists. The |
| 3470 ** only consequence will be disabled optimizations. But this routine | 3963 ** only consequence will be disabled optimizations. But this routine |
| 3471 ** must never return 0 if the two ExprList objects are different, or | 3964 ** must never return 0 if the two ExprList objects are different, or |
| 3472 ** a malfunction will result. | 3965 ** a malfunction will result. |
| 3473 ** | 3966 ** |
| 3474 ** Two NULL pointers are considered to be the same. But a NULL pointer | 3967 ** Two NULL pointers are considered to be the same. But a NULL pointer |
| 3475 ** always differs from a non-NULL pointer. | 3968 ** always differs from a non-NULL pointer. |
| 3476 */ | 3969 */ |
| 3477 int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){ | 3970 int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ |
| 3478 int i; | 3971 int i; |
| 3479 if( pA==0 && pB==0 ) return 0; | 3972 if( pA==0 && pB==0 ) return 0; |
| 3480 if( pA==0 || pB==0 ) return 1; | 3973 if( pA==0 || pB==0 ) return 1; |
| 3481 if( pA->nExpr!=pB->nExpr ) return 1; | 3974 if( pA->nExpr!=pB->nExpr ) return 1; |
| 3482 for(i=0; i<pA->nExpr; i++){ | 3975 for(i=0; i<pA->nExpr; i++){ |
| 3483 Expr *pExprA = pA->a[i].pExpr; | 3976 Expr *pExprA = pA->a[i].pExpr; |
| 3484 Expr *pExprB = pB->a[i].pExpr; | 3977 Expr *pExprB = pB->a[i].pExpr; |
| 3485 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; | 3978 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; |
| 3486 if( sqlite3ExprCompare(pExprA, pExprB) ) return 1; | 3979 if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; |
| 3487 } | 3980 } |
| 3488 return 0; | 3981 return 0; |
| 3489 } | 3982 } |
| 3490 | 3983 |
| 3491 /* | 3984 /* |
| 3985 ** Return true if we can prove the pE2 will always be true if pE1 is |
| 3986 ** true. Return false if we cannot complete the proof or if pE2 might |
| 3987 ** be false. Examples: |
| 3988 ** |
| 3989 ** pE1: x==5 pE2: x==5 Result: true |
| 3990 ** pE1: x>0 pE2: x==5 Result: false |
| 3991 ** pE1: x=21 pE2: x=21 OR y=43 Result: true |
| 3992 ** pE1: x!=123 pE2: x IS NOT NULL Result: true |
| 3993 ** pE1: x!=?1 pE2: x IS NOT NULL Result: true |
| 3994 ** pE1: x IS NULL pE2: x IS NOT NULL Result: false |
| 3995 ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false |
| 3996 ** |
| 3997 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has |
| 3998 ** Expr.iTable<0 then assume a table number given by iTab. |
| 3999 ** |
| 4000 ** When in doubt, return false. Returning true might give a performance |
| 4001 ** improvement. Returning false might cause a performance reduction, but |
| 4002 ** it will always give the correct answer and is hence always safe. |
| 4003 */ |
| 4004 int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ |
| 4005 if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ |
| 4006 return 1; |
| 4007 } |
| 4008 if( pE2->op==TK_OR |
| 4009 && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) |
| 4010 || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) |
| 4011 ){ |
| 4012 return 1; |
| 4013 } |
| 4014 if( pE2->op==TK_NOTNULL |
| 4015 && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0 |
| 4016 && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS) |
| 4017 ){ |
| 4018 return 1; |
| 4019 } |
| 4020 return 0; |
| 4021 } |
| 4022 |
| 4023 /* |
| 4024 ** An instance of the following structure is used by the tree walker |
| 4025 ** to count references to table columns in the arguments of an |
| 4026 ** aggregate function, in order to implement the |
| 4027 ** sqlite3FunctionThisSrc() routine. |
| 4028 */ |
| 4029 struct SrcCount { |
| 4030 SrcList *pSrc; /* One particular FROM clause in a nested query */ |
| 4031 int nThis; /* Number of references to columns in pSrcList */ |
| 4032 int nOther; /* Number of references to columns in other FROM clauses */ |
| 4033 }; |
| 4034 |
| 4035 /* |
| 4036 ** Count the number of references to columns. |
| 4037 */ |
| 4038 static int exprSrcCount(Walker *pWalker, Expr *pExpr){ |
| 4039 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() |
| 4040 ** is always called before sqlite3ExprAnalyzeAggregates() and so the |
| 4041 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If |
| 4042 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the |
| 4043 ** NEVER() will need to be removed. */ |
| 4044 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ |
| 4045 int i; |
| 4046 struct SrcCount *p = pWalker->u.pSrcCount; |
| 4047 SrcList *pSrc = p->pSrc; |
| 4048 for(i=0; i<pSrc->nSrc; i++){ |
| 4049 if( pExpr->iTable==pSrc->a[i].iCursor ) break; |
| 4050 } |
| 4051 if( i<pSrc->nSrc ){ |
| 4052 p->nThis++; |
| 4053 }else{ |
| 4054 p->nOther++; |
| 4055 } |
| 4056 } |
| 4057 return WRC_Continue; |
| 4058 } |
| 4059 |
| 4060 /* |
| 4061 ** Determine if any of the arguments to the pExpr Function reference |
| 4062 ** pSrcList. Return true if they do. Also return true if the function |
| 4063 ** has no arguments or has only constant arguments. Return false if pExpr |
| 4064 ** references columns but not columns of tables found in pSrcList. |
| 4065 */ |
| 4066 int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ |
| 4067 Walker w; |
| 4068 struct SrcCount cnt; |
| 4069 assert( pExpr->op==TK_AGG_FUNCTION ); |
| 4070 memset(&w, 0, sizeof(w)); |
| 4071 w.xExprCallback = exprSrcCount; |
| 4072 w.u.pSrcCount = &cnt; |
| 4073 cnt.pSrc = pSrcList; |
| 4074 cnt.nThis = 0; |
| 4075 cnt.nOther = 0; |
| 4076 sqlite3WalkExprList(&w, pExpr->x.pList); |
| 4077 return cnt.nThis>0 || cnt.nOther==0; |
| 4078 } |
| 4079 |
| 4080 /* |
| 3492 ** Add a new element to the pAggInfo->aCol[] array. Return the index of | 4081 ** Add a new element to the pAggInfo->aCol[] array. Return the index of |
| 3493 ** the new element. Return a negative number if malloc fails. | 4082 ** the new element. Return a negative number if malloc fails. |
| 3494 */ | 4083 */ |
| 3495 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ | 4084 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ |
| 3496 int i; | 4085 int i; |
| 3497 pInfo->aCol = sqlite3ArrayAllocate( | 4086 pInfo->aCol = sqlite3ArrayAllocate( |
| 3498 db, | 4087 db, |
| 3499 pInfo->aCol, | 4088 pInfo->aCol, |
| 3500 sizeof(pInfo->aCol[0]), | 4089 sizeof(pInfo->aCol[0]), |
| 3501 3, | |
| 3502 &pInfo->nColumn, | 4090 &pInfo->nColumn, |
| 3503 &pInfo->nColumnAlloc, | |
| 3504 &i | 4091 &i |
| 3505 ); | 4092 ); |
| 3506 return i; | 4093 return i; |
| 3507 } | 4094 } |
| 3508 | 4095 |
| 3509 /* | 4096 /* |
| 3510 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of | 4097 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of |
| 3511 ** the new element. Return a negative number if malloc fails. | 4098 ** the new element. Return a negative number if malloc fails. |
| 3512 */ | 4099 */ |
| 3513 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ | 4100 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ |
| 3514 int i; | 4101 int i; |
| 3515 pInfo->aFunc = sqlite3ArrayAllocate( | 4102 pInfo->aFunc = sqlite3ArrayAllocate( |
| 3516 db, | 4103 db, |
| 3517 pInfo->aFunc, | 4104 pInfo->aFunc, |
| 3518 sizeof(pInfo->aFunc[0]), | 4105 sizeof(pInfo->aFunc[0]), |
| 3519 3, | |
| 3520 &pInfo->nFunc, | 4106 &pInfo->nFunc, |
| 3521 &pInfo->nFuncAlloc, | |
| 3522 &i | 4107 &i |
| 3523 ); | 4108 ); |
| 3524 return i; | 4109 return i; |
| 3525 } | 4110 } |
| 3526 | 4111 |
| 3527 /* | 4112 /* |
| 3528 ** This is the xExprCallback for a tree walker. It is used to | 4113 ** This is the xExprCallback for a tree walker. It is used to |
| 3529 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates | 4114 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates |
| 3530 ** for additional information. | 4115 ** for additional information. |
| 3531 */ | 4116 */ |
| 3532 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ | 4117 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ |
| 3533 int i; | 4118 int i; |
| 3534 NameContext *pNC = pWalker->u.pNC; | 4119 NameContext *pNC = pWalker->u.pNC; |
| 3535 Parse *pParse = pNC->pParse; | 4120 Parse *pParse = pNC->pParse; |
| 3536 SrcList *pSrcList = pNC->pSrcList; | 4121 SrcList *pSrcList = pNC->pSrcList; |
| 3537 AggInfo *pAggInfo = pNC->pAggInfo; | 4122 AggInfo *pAggInfo = pNC->pAggInfo; |
| 3538 | 4123 |
| 3539 switch( pExpr->op ){ | 4124 switch( pExpr->op ){ |
| 3540 case TK_AGG_COLUMN: | 4125 case TK_AGG_COLUMN: |
| 3541 case TK_COLUMN: { | 4126 case TK_COLUMN: { |
| 3542 testcase( pExpr->op==TK_AGG_COLUMN ); | 4127 testcase( pExpr->op==TK_AGG_COLUMN ); |
| 3543 testcase( pExpr->op==TK_COLUMN ); | 4128 testcase( pExpr->op==TK_COLUMN ); |
| 3544 /* Check to see if the column is in one of the tables in the FROM | 4129 /* Check to see if the column is in one of the tables in the FROM |
| 3545 ** clause of the aggregate query */ | 4130 ** clause of the aggregate query */ |
| 3546 if( ALWAYS(pSrcList!=0) ){ | 4131 if( ALWAYS(pSrcList!=0) ){ |
| 3547 struct SrcList_item *pItem = pSrcList->a; | 4132 struct SrcList_item *pItem = pSrcList->a; |
| 3548 for(i=0; i<pSrcList->nSrc; i++, pItem++){ | 4133 for(i=0; i<pSrcList->nSrc; i++, pItem++){ |
| 3549 struct AggInfo_col *pCol; | 4134 struct AggInfo_col *pCol; |
| 3550 assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) ); | 4135 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
| 3551 if( pExpr->iTable==pItem->iCursor ){ | 4136 if( pExpr->iTable==pItem->iCursor ){ |
| 3552 /* If we reach this point, it means that pExpr refers to a table | 4137 /* If we reach this point, it means that pExpr refers to a table |
| 3553 ** that is in the FROM clause of the aggregate query. | 4138 ** that is in the FROM clause of the aggregate query. |
| 3554 ** | 4139 ** |
| 3555 ** Make an entry for the column in pAggInfo->aCol[] if there | 4140 ** Make an entry for the column in pAggInfo->aCol[] if there |
| 3556 ** is not an entry there already. | 4141 ** is not an entry there already. |
| 3557 */ | 4142 */ |
| 3558 int k; | 4143 int k; |
| 3559 pCol = pAggInfo->aCol; | 4144 pCol = pAggInfo->aCol; |
| 3560 for(k=0; k<pAggInfo->nColumn; k++, pCol++){ | 4145 for(k=0; k<pAggInfo->nColumn; k++, pCol++){ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 3589 } | 4174 } |
| 3590 if( pCol->iSorterColumn<0 ){ | 4175 if( pCol->iSorterColumn<0 ){ |
| 3591 pCol->iSorterColumn = pAggInfo->nSortingColumn++; | 4176 pCol->iSorterColumn = pAggInfo->nSortingColumn++; |
| 3592 } | 4177 } |
| 3593 } | 4178 } |
| 3594 /* There is now an entry for pExpr in pAggInfo->aCol[] (either | 4179 /* There is now an entry for pExpr in pAggInfo->aCol[] (either |
| 3595 ** because it was there before or because we just created it). | 4180 ** because it was there before or because we just created it). |
| 3596 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that | 4181 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that |
| 3597 ** pAggInfo->aCol[] entry. | 4182 ** pAggInfo->aCol[] entry. |
| 3598 */ | 4183 */ |
| 3599 ExprSetIrreducible(pExpr); | 4184 ExprSetVVAProperty(pExpr, EP_NoReduce); |
| 3600 pExpr->pAggInfo = pAggInfo; | 4185 pExpr->pAggInfo = pAggInfo; |
| 3601 pExpr->op = TK_AGG_COLUMN; | 4186 pExpr->op = TK_AGG_COLUMN; |
| 3602 pExpr->iAgg = (i16)k; | 4187 pExpr->iAgg = (i16)k; |
| 3603 break; | 4188 break; |
| 3604 } /* endif pExpr->iTable==pItem->iCursor */ | 4189 } /* endif pExpr->iTable==pItem->iCursor */ |
| 3605 } /* end loop over pSrcList */ | 4190 } /* end loop over pSrcList */ |
| 3606 } | 4191 } |
| 3607 return WRC_Prune; | 4192 return WRC_Prune; |
| 3608 } | 4193 } |
| 3609 case TK_AGG_FUNCTION: { | 4194 case TK_AGG_FUNCTION: { |
| 3610 /* The pNC->nDepth==0 test causes aggregate functions in subqueries | 4195 if( (pNC->ncFlags & NC_InAggFunc)==0 |
| 3611 ** to be ignored */ | 4196 && pWalker->walkerDepth==pExpr->op2 |
| 3612 if( pNC->nDepth==0 ){ | 4197 ){ |
| 3613 /* Check to see if pExpr is a duplicate of another aggregate | 4198 /* Check to see if pExpr is a duplicate of another aggregate |
| 3614 ** function that is already in the pAggInfo structure | 4199 ** function that is already in the pAggInfo structure |
| 3615 */ | 4200 */ |
| 3616 struct AggInfo_func *pItem = pAggInfo->aFunc; | 4201 struct AggInfo_func *pItem = pAggInfo->aFunc; |
| 3617 for(i=0; i<pAggInfo->nFunc; i++, pItem++){ | 4202 for(i=0; i<pAggInfo->nFunc; i++, pItem++){ |
| 3618 if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){ | 4203 if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ |
| 3619 break; | 4204 break; |
| 3620 } | 4205 } |
| 3621 } | 4206 } |
| 3622 if( i>=pAggInfo->nFunc ){ | 4207 if( i>=pAggInfo->nFunc ){ |
| 3623 /* pExpr is original. Make a new entry in pAggInfo->aFunc[] | 4208 /* pExpr is original. Make a new entry in pAggInfo->aFunc[] |
| 3624 */ | 4209 */ |
| 3625 u8 enc = ENC(pParse->db); | 4210 u8 enc = ENC(pParse->db); |
| 3626 i = addAggInfoFunc(pParse->db, pAggInfo); | 4211 i = addAggInfoFunc(pParse->db, pAggInfo); |
| 3627 if( i>=0 ){ | 4212 if( i>=0 ){ |
| 3628 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); | 4213 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 3629 pItem = &pAggInfo->aFunc[i]; | 4214 pItem = &pAggInfo->aFunc[i]; |
| 3630 pItem->pExpr = pExpr; | 4215 pItem->pExpr = pExpr; |
| 3631 pItem->iMem = ++pParse->nMem; | 4216 pItem->iMem = ++pParse->nMem; |
| 3632 assert( !ExprHasProperty(pExpr, EP_IntValue) ); | 4217 assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 3633 pItem->pFunc = sqlite3FindFunction(pParse->db, | 4218 pItem->pFunc = sqlite3FindFunction(pParse->db, |
| 3634 pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken), | 4219 pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken), |
| 3635 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); | 4220 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); |
| 3636 if( pExpr->flags & EP_Distinct ){ | 4221 if( pExpr->flags & EP_Distinct ){ |
| 3637 pItem->iDistinct = pParse->nTab++; | 4222 pItem->iDistinct = pParse->nTab++; |
| 3638 }else{ | 4223 }else{ |
| 3639 pItem->iDistinct = -1; | 4224 pItem->iDistinct = -1; |
| 3640 } | 4225 } |
| 3641 } | 4226 } |
| 3642 } | 4227 } |
| 3643 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry | 4228 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry |
| 3644 */ | 4229 */ |
| 3645 assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) ); | 4230 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
| 3646 ExprSetIrreducible(pExpr); | 4231 ExprSetVVAProperty(pExpr, EP_NoReduce); |
| 3647 pExpr->iAgg = (i16)i; | 4232 pExpr->iAgg = (i16)i; |
| 3648 pExpr->pAggInfo = pAggInfo; | 4233 pExpr->pAggInfo = pAggInfo; |
| 3649 return WRC_Prune; | 4234 return WRC_Prune; |
| 4235 }else{ |
| 4236 return WRC_Continue; |
| 3650 } | 4237 } |
| 3651 } | 4238 } |
| 3652 } | 4239 } |
| 3653 return WRC_Continue; | 4240 return WRC_Continue; |
| 3654 } | 4241 } |
| 3655 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ | 4242 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ |
| 3656 NameContext *pNC = pWalker->u.pNC; | 4243 UNUSED_PARAMETER(pWalker); |
| 3657 if( pNC->nDepth==0 ){ | 4244 UNUSED_PARAMETER(pSelect); |
| 3658 pNC->nDepth++; | 4245 return WRC_Continue; |
| 3659 sqlite3WalkSelect(pWalker, pSelect); | |
| 3660 pNC->nDepth--; | |
| 3661 return WRC_Prune; | |
| 3662 }else{ | |
| 3663 return WRC_Continue; | |
| 3664 } | |
| 3665 } | 4246 } |
| 3666 | 4247 |
| 3667 /* | 4248 /* |
| 3668 ** Analyze the given expression looking for aggregate functions and | 4249 ** Analyze the pExpr expression looking for aggregate functions and |
| 3669 ** for variables that need to be added to the pParse->aAgg[] array. | 4250 ** for variables that need to be added to AggInfo object that pNC->pAggInfo |
| 3670 ** Make additional entries to the pParse->aAgg[] array as necessary. | 4251 ** points to. Additional entries are made on the AggInfo object as |
| 4252 ** necessary. |
| 3671 ** | 4253 ** |
| 3672 ** This routine should only be called after the expression has been | 4254 ** This routine should only be called after the expression has been |
| 3673 ** analyzed by sqlite3ResolveExprNames(). | 4255 ** analyzed by sqlite3ResolveExprNames(). |
| 3674 */ | 4256 */ |
| 3675 void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ | 4257 void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
| 3676 Walker w; | 4258 Walker w; |
| 4259 memset(&w, 0, sizeof(w)); |
| 3677 w.xExprCallback = analyzeAggregate; | 4260 w.xExprCallback = analyzeAggregate; |
| 3678 w.xSelectCallback = analyzeAggregatesInSelect; | 4261 w.xSelectCallback = analyzeAggregatesInSelect; |
| 3679 w.u.pNC = pNC; | 4262 w.u.pNC = pNC; |
| 3680 assert( pNC->pSrcList!=0 ); | 4263 assert( pNC->pSrcList!=0 ); |
| 3681 sqlite3WalkExpr(&w, pExpr); | 4264 sqlite3WalkExpr(&w, pExpr); |
| 3682 } | 4265 } |
| 3683 | 4266 |
| 3684 /* | 4267 /* |
| 3685 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an | 4268 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an |
| 3686 ** expression list. Return the number of errors. | 4269 ** expression list. Return the number of errors. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 3705 return ++pParse->nMem; | 4288 return ++pParse->nMem; |
| 3706 } | 4289 } |
| 3707 return pParse->aTempReg[--pParse->nTempReg]; | 4290 return pParse->aTempReg[--pParse->nTempReg]; |
| 3708 } | 4291 } |
| 3709 | 4292 |
| 3710 /* | 4293 /* |
| 3711 ** Deallocate a register, making available for reuse for some other | 4294 ** Deallocate a register, making available for reuse for some other |
| 3712 ** purpose. | 4295 ** purpose. |
| 3713 ** | 4296 ** |
| 3714 ** If a register is currently being used by the column cache, then | 4297 ** If a register is currently being used by the column cache, then |
| 3715 ** the dallocation is deferred until the column cache line that uses | 4298 ** the deallocation is deferred until the column cache line that uses |
| 3716 ** the register becomes stale. | 4299 ** the register becomes stale. |
| 3717 */ | 4300 */ |
| 3718 void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ | 4301 void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ |
| 3719 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ | 4302 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ |
| 3720 int i; | 4303 int i; |
| 3721 struct yColCache *p; | 4304 struct yColCache *p; |
| 3722 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ | 4305 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ |
| 3723 if( p->iReg==iReg ){ | 4306 if( p->iReg==iReg ){ |
| 3724 p->tempReg = 1; | 4307 p->tempReg = 1; |
| 3725 return; | 4308 return; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 3746 } | 4329 } |
| 3747 return i; | 4330 return i; |
| 3748 } | 4331 } |
| 3749 void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ | 4332 void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ |
| 3750 sqlite3ExprCacheRemove(pParse, iReg, nReg); | 4333 sqlite3ExprCacheRemove(pParse, iReg, nReg); |
| 3751 if( nReg>pParse->nRangeReg ){ | 4334 if( nReg>pParse->nRangeReg ){ |
| 3752 pParse->nRangeReg = nReg; | 4335 pParse->nRangeReg = nReg; |
| 3753 pParse->iRangeReg = iReg; | 4336 pParse->iRangeReg = iReg; |
| 3754 } | 4337 } |
| 3755 } | 4338 } |
| 4339 |
| 4340 /* |
| 4341 ** Mark all temporary registers as being unavailable for reuse. |
| 4342 */ |
| 4343 void sqlite3ClearTempRegCache(Parse *pParse){ |
| 4344 pParse->nTempReg = 0; |
| 4345 pParse->nRangeReg = 0; |
| 4346 } |
| OLD | NEW |