Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: third_party/sqlite/sqlite-src-3100200/src/expr.c

Issue 1610543003: [sql] Import reference version of SQLite 3.10.2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 **
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 } 84 }
85 Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ 85 Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
86 Token s; 86 Token s;
87 assert( zC!=0 ); 87 assert( zC!=0 );
88 s.z = zC; 88 s.z = zC;
89 s.n = sqlite3Strlen30(s.z); 89 s.n = sqlite3Strlen30(s.z);
90 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); 90 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
91 } 91 }
92 92
93 /* 93 /*
94 ** Skip over any TK_COLLATE or TK_AS operators and any unlikely() 94 ** Skip over any TK_COLLATE operators and any unlikely()
95 ** or likelihood() function at the root of an expression. 95 ** or likelihood() function at the root of an expression.
96 */ 96 */
97 Expr *sqlite3ExprSkipCollate(Expr *pExpr){ 97 Expr *sqlite3ExprSkipCollate(Expr *pExpr){
98 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ 98 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
99 if( ExprHasProperty(pExpr, EP_Unlikely) ){ 99 if( ExprHasProperty(pExpr, EP_Unlikely) ){
100 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 100 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
101 assert( pExpr->x.pList->nExpr>0 ); 101 assert( pExpr->x.pList->nExpr>0 );
102 assert( pExpr->op==TK_FUNCTION ); 102 assert( pExpr->op==TK_FUNCTION );
103 pExpr = pExpr->x.pList->a[0].pExpr; 103 pExpr = pExpr->x.pList->a[0].pExpr;
104 }else{ 104 }else{
105 assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS ); 105 assert( pExpr->op==TK_COLLATE );
106 pExpr = pExpr->pLeft; 106 pExpr = pExpr->pLeft;
107 } 107 }
108 } 108 }
109 return pExpr; 109 return pExpr;
110 } 110 }
111 111
112 /* 112 /*
113 ** Return the collation sequence for the expression pExpr. If 113 ** Return the collation sequence for the expression pExpr. If
114 ** there is no defined collating sequence, return NULL. 114 ** there is no defined collating sequence, return NULL.
115 ** 115 **
(...skipping 10 matching lines...) Expand all
126 int op = p->op; 126 int op = p->op;
127 if( p->flags & EP_Generic ) break; 127 if( p->flags & EP_Generic ) break;
128 if( op==TK_CAST || op==TK_UPLUS ){ 128 if( op==TK_CAST || op==TK_UPLUS ){
129 p = p->pLeft; 129 p = p->pLeft;
130 continue; 130 continue;
131 } 131 }
132 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){ 132 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
133 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); 133 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
134 break; 134 break;
135 } 135 }
136 if( p->pTab!=0 136 if( (op==TK_AGG_COLUMN || op==TK_COLUMN
137 && (op==TK_AGG_COLUMN || op==TK_COLUMN
138 || op==TK_REGISTER || op==TK_TRIGGER) 137 || op==TK_REGISTER || op==TK_TRIGGER)
138 && p->pTab!=0
139 ){ 139 ){
140 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally 140 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
141 ** a TK_COLUMN but was previously evaluated and cached in a register */ 141 ** a TK_COLUMN but was previously evaluated and cached in a register */
142 int j = p->iColumn; 142 int j = p->iColumn;
143 if( j>=0 ){ 143 if( j>=0 ){
144 const char *zColl = p->pTab->aCol[j].zColl; 144 const char *zColl = p->pTab->aCol[j].zColl;
145 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); 145 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
146 } 146 }
147 break; 147 break;
148 } 148 }
149 if( p->flags & EP_Collate ){ 149 if( p->flags & EP_Collate ){
150 if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){ 150 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
151 p = p->pLeft; 151 p = p->pLeft;
152 }else{ 152 }else{
153 p = p->pRight; 153 Expr *pNext = p->pRight;
154 /* The Expr.x union is never used at the same time as Expr.pRight */
155 assert( p->x.pList==0 || p->pRight==0 );
156 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
157 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
158 ** least one EP_Collate. Thus the following two ALWAYS. */
159 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
160 int i;
161 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
162 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
163 pNext = p->x.pList->a[i].pExpr;
164 break;
165 }
166 }
167 }
168 p = pNext;
154 } 169 }
155 }else{ 170 }else{
156 break; 171 break;
157 } 172 }
158 } 173 }
159 if( sqlite3CheckCollSeq(pParse, pColl) ){ 174 if( sqlite3CheckCollSeq(pParse, pColl) ){
160 pColl = 0; 175 pColl = 0;
161 } 176 }
162 return pColl; 177 return pColl;
163 } 178 }
164 179
165 /* 180 /*
166 ** pExpr is an operand of a comparison operator. aff2 is the 181 ** pExpr is an operand of a comparison operator. aff2 is the
167 ** type affinity of the other operand. This routine returns the 182 ** type affinity of the other operand. This routine returns the
168 ** type affinity that should be used for the comparison operator. 183 ** type affinity that should be used for the comparison operator.
169 */ 184 */
170 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 185 char sqlite3CompareAffinity(Expr *pExpr, char aff2){
171 char aff1 = sqlite3ExprAffinity(pExpr); 186 char aff1 = sqlite3ExprAffinity(pExpr);
172 if( aff1 && aff2 ){ 187 if( aff1 && aff2 ){
173 /* Both sides of the comparison are columns. If one has numeric 188 /* Both sides of the comparison are columns. If one has numeric
174 ** affinity, use that. Otherwise use no affinity. 189 ** affinity, use that. Otherwise use no affinity.
175 */ 190 */
176 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ 191 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
177 return SQLITE_AFF_NUMERIC; 192 return SQLITE_AFF_NUMERIC;
178 }else{ 193 }else{
179 return SQLITE_AFF_NONE; 194 return SQLITE_AFF_BLOB;
180 } 195 }
181 }else if( !aff1 && !aff2 ){ 196 }else if( !aff1 && !aff2 ){
182 /* Neither side of the comparison is a column. Compare the 197 /* Neither side of the comparison is a column. Compare the
183 ** results directly. 198 ** results directly.
184 */ 199 */
185 return SQLITE_AFF_NONE; 200 return SQLITE_AFF_BLOB;
186 }else{ 201 }else{
187 /* One side is a column, the other is not. Use the columns affinity. */ 202 /* One side is a column, the other is not. Use the columns affinity. */
188 assert( aff1==0 || aff2==0 ); 203 assert( aff1==0 || aff2==0 );
189 return (aff1 + aff2); 204 return (aff1 + aff2);
190 } 205 }
191 } 206 }
192 207
193 /* 208 /*
194 ** pExpr is a comparison operator. Return the type affinity that should 209 ** pExpr is a comparison operator. Return the type affinity that should
195 ** be applied to both operands prior to doing the comparison. 210 ** be applied to both operands prior to doing the comparison.
196 */ 211 */
197 static char comparisonAffinity(Expr *pExpr){ 212 static char comparisonAffinity(Expr *pExpr){
198 char aff; 213 char aff;
199 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 214 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
200 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 215 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
201 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT ); 216 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
202 assert( pExpr->pLeft ); 217 assert( pExpr->pLeft );
203 aff = sqlite3ExprAffinity(pExpr->pLeft); 218 aff = sqlite3ExprAffinity(pExpr->pLeft);
204 if( pExpr->pRight ){ 219 if( pExpr->pRight ){
205 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 220 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
206 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 221 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
207 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); 222 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
208 }else if( !aff ){ 223 }else if( !aff ){
209 aff = SQLITE_AFF_NONE; 224 aff = SQLITE_AFF_BLOB;
210 } 225 }
211 return aff; 226 return aff;
212 } 227 }
213 228
214 /* 229 /*
215 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 230 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
216 ** idx_affinity is the affinity of an indexed column. Return true 231 ** idx_affinity is the affinity of an indexed column. Return true
217 ** if the index with affinity idx_affinity may be used to implement 232 ** if the index with affinity idx_affinity may be used to implement
218 ** the comparison in pExpr. 233 ** the comparison in pExpr.
219 */ 234 */
220 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 235 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
221 char aff = comparisonAffinity(pExpr); 236 char aff = comparisonAffinity(pExpr);
222 switch( aff ){ 237 switch( aff ){
223 case SQLITE_AFF_NONE: 238 case SQLITE_AFF_BLOB:
224 return 1; 239 return 1;
225 case SQLITE_AFF_TEXT: 240 case SQLITE_AFF_TEXT:
226 return idx_affinity==SQLITE_AFF_TEXT; 241 return idx_affinity==SQLITE_AFF_TEXT;
227 default: 242 default:
228 return sqlite3IsNumericAffinity(idx_affinity); 243 return sqlite3IsNumericAffinity(idx_affinity);
229 } 244 }
230 } 245 }
231 246
232 /* 247 /*
233 ** Return the P5 value that should be used for a binary comparison 248 ** Return the P5 value that should be used for a binary comparison
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 heightOfSelect(p->pPrior, pnHeight); 364 heightOfSelect(p->pPrior, pnHeight);
350 } 365 }
351 } 366 }
352 367
353 /* 368 /*
354 ** Set the Expr.nHeight variable in the structure passed as an 369 ** Set the Expr.nHeight variable in the structure passed as an
355 ** argument. An expression with no children, Expr.pList or 370 ** argument. An expression with no children, Expr.pList or
356 ** Expr.pSelect member has a height of 1. Any other expression 371 ** Expr.pSelect member has a height of 1. Any other expression
357 ** has a height equal to the maximum height of any other 372 ** has a height equal to the maximum height of any other
358 ** referenced Expr plus one. 373 ** referenced Expr plus one.
374 **
375 ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
376 ** if appropriate.
359 */ 377 */
360 static void exprSetHeight(Expr *p){ 378 static void exprSetHeight(Expr *p){
361 int nHeight = 0; 379 int nHeight = 0;
362 heightOfExpr(p->pLeft, &nHeight); 380 heightOfExpr(p->pLeft, &nHeight);
363 heightOfExpr(p->pRight, &nHeight); 381 heightOfExpr(p->pRight, &nHeight);
364 if( ExprHasProperty(p, EP_xIsSelect) ){ 382 if( ExprHasProperty(p, EP_xIsSelect) ){
365 heightOfSelect(p->x.pSelect, &nHeight); 383 heightOfSelect(p->x.pSelect, &nHeight);
366 }else{ 384 }else if( p->x.pList ){
367 heightOfExprList(p->x.pList, &nHeight); 385 heightOfExprList(p->x.pList, &nHeight);
386 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
368 } 387 }
369 p->nHeight = nHeight + 1; 388 p->nHeight = nHeight + 1;
370 } 389 }
371 390
372 /* 391 /*
373 ** Set the Expr.nHeight variable using the exprSetHeight() function. If 392 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
374 ** the height is greater than the maximum allowed expression depth, 393 ** the height is greater than the maximum allowed expression depth,
375 ** leave an error in pParse. 394 ** leave an error in pParse.
395 **
396 ** Also propagate all EP_Propagate flags from the Expr.x.pList into
397 ** Expr.flags.
376 */ 398 */
377 void sqlite3ExprSetHeight(Parse *pParse, Expr *p){ 399 void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
400 if( pParse->nErr ) return;
378 exprSetHeight(p); 401 exprSetHeight(p);
379 sqlite3ExprCheckHeight(pParse, p->nHeight); 402 sqlite3ExprCheckHeight(pParse, p->nHeight);
380 } 403 }
381 404
382 /* 405 /*
383 ** Return the maximum height of any expression tree referenced 406 ** Return the maximum height of any expression tree referenced
384 ** by the select statement passed as an argument. 407 ** by the select statement passed as an argument.
385 */ 408 */
386 int sqlite3SelectExprHeight(Select *p){ 409 int sqlite3SelectExprHeight(Select *p){
387 int nHeight = 0; 410 int nHeight = 0;
388 heightOfSelect(p, &nHeight); 411 heightOfSelect(p, &nHeight);
389 return nHeight; 412 return nHeight;
390 } 413 }
391 #else 414 #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
392 #define exprSetHeight(y) 415 /*
416 ** Propagate all EP_Propagate flags from the Expr.x.pList into
417 ** Expr.flags.
418 */
419 void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
420 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
421 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
422 }
423 }
424 #define exprSetHeight(y)
393 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ 425 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
394 426
395 /* 427 /*
396 ** This routine is the core allocator for Expr nodes. 428 ** This routine is the core allocator for Expr nodes.
397 ** 429 **
398 ** Construct a new expression node and return a pointer to it. Memory 430 ** Construct a new expression node and return a pointer to it. Memory
399 ** for this node and for the pToken argument is a single allocation 431 ** for this node and for the pToken argument is a single allocation
400 ** obtained from sqlite3DbMalloc(). The calling function 432 ** obtained from sqlite3DbMalloc(). The calling function
401 ** is responsible for making sure the node eventually gets freed. 433 ** is responsible for making sure the node eventually gets freed.
402 ** 434 **
403 ** If dequote is true, then the token (if it exists) is dequoted. 435 ** If dequote is true, then the token (if it exists) is dequoted.
404 ** If dequote is false, no dequoting is performance. The deQuote 436 ** If dequote is false, no dequoting is performed. The deQuote
405 ** parameter is ignored if pToken is NULL or if the token does not 437 ** parameter is ignored if pToken is NULL or if the token does not
406 ** appear to be quoted. If the quotes were of the form "..." (double-quotes) 438 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
407 ** then the EP_DblQuoted flag is set on the expression node. 439 ** then the EP_DblQuoted flag is set on the expression node.
408 ** 440 **
409 ** Special case: If op==TK_INTEGER and pToken points to a string that 441 ** Special case: If op==TK_INTEGER and pToken points to a string that
410 ** can be translated into a 32-bit integer, then the token is not 442 ** can be translated into a 32-bit integer, then the token is not
411 ** stored in u.zToken. Instead, the integer values is written 443 ** stored in u.zToken. Instead, the integer values is written
412 ** into u.iValue and the EP_IntValue flag is set. No extra storage 444 ** into u.iValue and the EP_IntValue flag is set. No extra storage
413 ** is allocated to hold the integer text and the dequote flag is ignored. 445 ** is allocated to hold the integer text and the dequote flag is ignored.
414 */ 446 */
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 Expr *pLeft, 516 Expr *pLeft,
485 Expr *pRight 517 Expr *pRight
486 ){ 518 ){
487 if( pRoot==0 ){ 519 if( pRoot==0 ){
488 assert( db->mallocFailed ); 520 assert( db->mallocFailed );
489 sqlite3ExprDelete(db, pLeft); 521 sqlite3ExprDelete(db, pLeft);
490 sqlite3ExprDelete(db, pRight); 522 sqlite3ExprDelete(db, pRight);
491 }else{ 523 }else{
492 if( pRight ){ 524 if( pRight ){
493 pRoot->pRight = pRight; 525 pRoot->pRight = pRight;
494 pRoot->flags |= EP_Collate & pRight->flags; 526 pRoot->flags |= EP_Propagate & pRight->flags;
495 } 527 }
496 if( pLeft ){ 528 if( pLeft ){
497 pRoot->pLeft = pLeft; 529 pRoot->pLeft = pLeft;
498 pRoot->flags |= EP_Collate & pLeft->flags; 530 pRoot->flags |= EP_Propagate & pLeft->flags;
499 } 531 }
500 exprSetHeight(pRoot); 532 exprSetHeight(pRoot);
501 } 533 }
502 } 534 }
503 535
504 /* 536 /*
505 ** Allocate an Expr node which joins as many as two subtrees. 537 ** Allocate an Expr node which joins as many as two subtrees.
506 ** 538 **
507 ** One or both of the subtrees can be NULL. Return a pointer to the new 539 ** One or both of the subtrees can be NULL. Return a pointer to the new
508 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, 540 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
509 ** free the subtrees and return NULL. 541 ** free the subtrees and return NULL.
510 */ 542 */
511 Expr *sqlite3PExpr( 543 Expr *sqlite3PExpr(
512 Parse *pParse, /* Parsing context */ 544 Parse *pParse, /* Parsing context */
513 int op, /* Expression opcode */ 545 int op, /* Expression opcode */
514 Expr *pLeft, /* Left operand */ 546 Expr *pLeft, /* Left operand */
515 Expr *pRight, /* Right operand */ 547 Expr *pRight, /* Right operand */
516 const Token *pToken /* Argument token */ 548 const Token *pToken /* Argument token */
517 ){ 549 ){
518 Expr *p; 550 Expr *p;
519 if( op==TK_AND && pLeft && pRight ){ 551 if( op==TK_AND && pParse->nErr==0 ){
520 /* Take advantage of short-circuit false optimization for AND */ 552 /* Take advantage of short-circuit false optimization for AND */
521 p = sqlite3ExprAnd(pParse->db, pLeft, pRight); 553 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
522 }else{ 554 }else{
523 p = sqlite3ExprAlloc(pParse->db, op, pToken, 1); 555 p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1);
524 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); 556 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
525 } 557 }
526 if( p ) { 558 if( p ) {
527 sqlite3ExprCheckHeight(pParse, p->nHeight); 559 sqlite3ExprCheckHeight(pParse, p->nHeight);
528 } 560 }
529 return p; 561 return p;
530 } 562 }
531 563
532 /* 564 /*
533 ** If the expression is always either TRUE or FALSE (respectively), 565 ** If the expression is always either TRUE or FALSE (respectively),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 Expr *pNew; 620 Expr *pNew;
589 sqlite3 *db = pParse->db; 621 sqlite3 *db = pParse->db;
590 assert( pToken ); 622 assert( pToken );
591 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); 623 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
592 if( pNew==0 ){ 624 if( pNew==0 ){
593 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ 625 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
594 return 0; 626 return 0;
595 } 627 }
596 pNew->x.pList = pList; 628 pNew->x.pList = pList;
597 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); 629 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
598 sqlite3ExprSetHeight(pParse, pNew); 630 sqlite3ExprSetHeightAndFlags(pParse, pNew);
599 return pNew; 631 return pNew;
600 } 632 }
601 633
602 /* 634 /*
603 ** Assign a variable number to an expression that encodes a wildcard 635 ** Assign a variable number to an expression that encodes a wildcard
604 ** in the original SQL statement. 636 ** in the original SQL statement.
605 ** 637 **
606 ** Wildcards consisting of a single "?" are assigned the next sequential 638 ** Wildcards consisting of a single "?" are assigned the next sequential
607 ** variable number. 639 ** variable number.
608 ** 640 **
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 /* 846 /*
815 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 847 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer
816 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 848 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough
817 ** to store the copy of expression p, the copies of p->u.zToken 849 ** to store the copy of expression p, the copies of p->u.zToken
818 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, 850 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
819 ** if any. Before returning, *pzBuffer is set to the first byte past the 851 ** if any. Before returning, *pzBuffer is set to the first byte past the
820 ** portion of the buffer copied into by this function. 852 ** portion of the buffer copied into by this function.
821 */ 853 */
822 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ 854 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
823 Expr *pNew = 0; /* Value to return */ 855 Expr *pNew = 0; /* Value to return */
856 assert( flags==0 || flags==EXPRDUP_REDUCE );
824 if( p ){ 857 if( p ){
825 const int isReduced = (flags&EXPRDUP_REDUCE); 858 const int isReduced = (flags&EXPRDUP_REDUCE);
826 u8 *zAlloc; 859 u8 *zAlloc;
827 u32 staticFlag = 0; 860 u32 staticFlag = 0;
828 861
829 assert( pzBuffer==0 || isReduced ); 862 assert( pzBuffer==0 || isReduced );
830 863
831 /* Figure out where to write the new Expr structure. */ 864 /* Figure out where to write the new Expr structure. */
832 if( pzBuffer ){ 865 if( pzBuffer ){
833 zAlloc = *pzBuffer; 866 zAlloc = *pzBuffer;
(...skipping 14 matching lines...) Expand all
848 int nToken; 881 int nToken;
849 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 882 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
850 nToken = sqlite3Strlen30(p->u.zToken) + 1; 883 nToken = sqlite3Strlen30(p->u.zToken) + 1;
851 }else{ 884 }else{
852 nToken = 0; 885 nToken = 0;
853 } 886 }
854 if( isReduced ){ 887 if( isReduced ){
855 assert( ExprHasProperty(p, EP_Reduced)==0 ); 888 assert( ExprHasProperty(p, EP_Reduced)==0 );
856 memcpy(zAlloc, p, nNewSize); 889 memcpy(zAlloc, p, nNewSize);
857 }else{ 890 }else{
858 int nSize = exprStructSize(p); 891 u32 nSize = (u32)exprStructSize(p);
859 memcpy(zAlloc, p, nSize); 892 memcpy(zAlloc, p, nSize);
860 if( EXPR_FULLSIZE>nSize ){ 893 if( nSize<EXPR_FULLSIZE ){
861 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); 894 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
862 } 895 }
863 } 896 }
864 897
865 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ 898 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
866 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); 899 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
867 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); 900 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
868 pNew->flags |= staticFlag; 901 pNew->flags |= staticFlag;
869 902
870 /* Copy the p->u.zToken string, if any. */ 903 /* Copy the p->u.zToken string, if any. */
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
942 ** by subsequent calls to sqlite*ListAppend() routines. 975 ** by subsequent calls to sqlite*ListAppend() routines.
943 ** 976 **
944 ** Any tables that the SrcList might point to are not duplicated. 977 ** Any tables that the SrcList might point to are not duplicated.
945 ** 978 **
946 ** The flags parameter contains a combination of the EXPRDUP_XXX flags. 979 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
947 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a 980 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
948 ** truncated version of the usual Expr structure that will be stored as 981 ** truncated version of the usual Expr structure that will be stored as
949 ** part of the in-memory representation of the database schema. 982 ** part of the in-memory representation of the database schema.
950 */ 983 */
951 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ 984 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
985 assert( flags==0 || flags==EXPRDUP_REDUCE );
952 return exprDup(db, p, flags, 0); 986 return exprDup(db, p, flags, 0);
953 } 987 }
954 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ 988 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
955 ExprList *pNew; 989 ExprList *pNew;
956 struct ExprList_item *pItem, *pOldItem; 990 struct ExprList_item *pItem, *pOldItem;
957 int i; 991 int i;
958 if( p==0 ) return 0; 992 if( p==0 ) return 0;
959 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 993 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
960 if( pNew==0 ) return 0; 994 if( pNew==0 ) return 0;
961 pNew->nExpr = i = p->nExpr; 995 pNew->nExpr = i = p->nExpr;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
997 if( pNew==0 ) return 0; 1031 if( pNew==0 ) return 0;
998 pNew->nSrc = pNew->nAlloc = p->nSrc; 1032 pNew->nSrc = pNew->nAlloc = p->nSrc;
999 for(i=0; i<p->nSrc; i++){ 1033 for(i=0; i<p->nSrc; i++){
1000 struct SrcList_item *pNewItem = &pNew->a[i]; 1034 struct SrcList_item *pNewItem = &pNew->a[i];
1001 struct SrcList_item *pOldItem = &p->a[i]; 1035 struct SrcList_item *pOldItem = &p->a[i];
1002 Table *pTab; 1036 Table *pTab;
1003 pNewItem->pSchema = pOldItem->pSchema; 1037 pNewItem->pSchema = pOldItem->pSchema;
1004 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 1038 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
1005 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1039 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
1006 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 1040 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
1007 pNewItem->jointype = pOldItem->jointype; 1041 pNewItem->fg = pOldItem->fg;
1008 pNewItem->iCursor = pOldItem->iCursor; 1042 pNewItem->iCursor = pOldItem->iCursor;
1009 pNewItem->addrFillSub = pOldItem->addrFillSub; 1043 pNewItem->addrFillSub = pOldItem->addrFillSub;
1010 pNewItem->regReturn = pOldItem->regReturn; 1044 pNewItem->regReturn = pOldItem->regReturn;
1011 pNewItem->isCorrelated = pOldItem->isCorrelated; 1045 if( pNewItem->fg.isIndexedBy ){
1012 pNewItem->viaCoroutine = pOldItem->viaCoroutine; 1046 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
1013 pNewItem->isRecursive = pOldItem->isRecursive; 1047 }
1014 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex); 1048 pNewItem->pIBIndex = pOldItem->pIBIndex;
1015 pNewItem->notIndexed = pOldItem->notIndexed; 1049 if( pNewItem->fg.isTabFunc ){
1016 pNewItem->pIndex = pOldItem->pIndex; 1050 pNewItem->u1.pFuncArg =
1051 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
1052 }
1017 pTab = pNewItem->pTab = pOldItem->pTab; 1053 pTab = pNewItem->pTab = pOldItem->pTab;
1018 if( pTab ){ 1054 if( pTab ){
1019 pTab->nRef++; 1055 pTab->nRef++;
1020 } 1056 }
1021 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); 1057 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
1022 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); 1058 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
1023 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 1059 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
1024 pNewItem->colUsed = pOldItem->colUsed; 1060 pNewItem->colUsed = pOldItem->colUsed;
1025 } 1061 }
1026 return pNew; 1062 return pNew;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
1122 return pList; 1158 return pList;
1123 1159
1124 no_mem: 1160 no_mem:
1125 /* Avoid leaking memory if malloc has failed. */ 1161 /* Avoid leaking memory if malloc has failed. */
1126 sqlite3ExprDelete(db, pExpr); 1162 sqlite3ExprDelete(db, pExpr);
1127 sqlite3ExprListDelete(db, pList); 1163 sqlite3ExprListDelete(db, pList);
1128 return 0; 1164 return 0;
1129 } 1165 }
1130 1166
1131 /* 1167 /*
1168 ** Set the sort order for the last element on the given ExprList.
1169 */
1170 void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
1171 if( p==0 ) return;
1172 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
1173 assert( p->nExpr>0 );
1174 if( iSortOrder<0 ){
1175 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
1176 return;
1177 }
1178 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
1179 }
1180
1181 /*
1132 ** Set the ExprList.a[].zName element of the most recently added item 1182 ** Set the ExprList.a[].zName element of the most recently added item
1133 ** on the expression list. 1183 ** on the expression list.
1134 ** 1184 **
1135 ** pList might be NULL following an OOM error. But pName should never be 1185 ** pList might be NULL following an OOM error. But pName should never be
1136 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1186 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1137 ** is set. 1187 ** is set.
1138 */ 1188 */
1139 void sqlite3ExprListSetName( 1189 void sqlite3ExprListSetName(
1140 Parse *pParse, /* Parsing context */ 1190 Parse *pParse, /* Parsing context */
1141 ExprList *pList, /* List to which to add the span. */ 1191 ExprList *pList, /* List to which to add the span. */
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 1256 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
1207 sqlite3ExprDelete(db, pItem->pExpr); 1257 sqlite3ExprDelete(db, pItem->pExpr);
1208 sqlite3DbFree(db, pItem->zName); 1258 sqlite3DbFree(db, pItem->zName);
1209 sqlite3DbFree(db, pItem->zSpan); 1259 sqlite3DbFree(db, pItem->zSpan);
1210 } 1260 }
1211 sqlite3DbFree(db, pList->a); 1261 sqlite3DbFree(db, pList->a);
1212 sqlite3DbFree(db, pList); 1262 sqlite3DbFree(db, pList);
1213 } 1263 }
1214 1264
1215 /* 1265 /*
1216 ** These routines are Walker callbacks. Walker.u.pi is a pointer 1266 ** Return the bitwise-OR of all Expr.flags fields in the given
1217 ** to an integer. These routines are checking an expression to see 1267 ** ExprList.
1218 ** if it is a constant. Set *Walker.u.i to 0 if the expression is 1268 */
1219 ** not constant. 1269 u32 sqlite3ExprListFlags(const ExprList *pList){
1270 int i;
1271 u32 m = 0;
1272 if( pList ){
1273 for(i=0; i<pList->nExpr; i++){
1274 Expr *pExpr = pList->a[i].pExpr;
1275 if( ALWAYS(pExpr) ) m |= pExpr->flags;
1276 }
1277 }
1278 return m;
1279 }
1280
1281 /*
1282 ** These routines are Walker callbacks used to check expressions to
1283 ** see if they are "constant" for some definition of constant. The
1284 ** Walker.eCode value determines the type of "constant" we are looking
1285 ** for.
1220 ** 1286 **
1221 ** These callback routines are used to implement the following: 1287 ** These callback routines are used to implement the following:
1222 ** 1288 **
1223 ** sqlite3ExprIsConstant() pWalker->u.i==1 1289 ** sqlite3ExprIsConstant() pWalker->eCode==1
1224 ** sqlite3ExprIsConstantNotJoin() pWalker->u.i==2 1290 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
1225 ** sqlite3ExprIsConstantOrFunction() pWalker->u.i==3 or 4 1291 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
1292 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
1293 **
1294 ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
1295 ** is found to not be a constant.
1226 ** 1296 **
1227 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions 1297 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
1228 ** in a CREATE TABLE statement. The Walker.u.i value is 4 when parsing 1298 ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
1229 ** an existing schema and 3 when processing a new statement. A bound 1299 ** an existing schema and 4 when processing a new statement. A bound
1230 ** parameter raises an error for new statements, but is silently converted 1300 ** parameter raises an error for new statements, but is silently converted
1231 ** to NULL for existing schemas. This allows sqlite_master tables that 1301 ** to NULL for existing schemas. This allows sqlite_master tables that
1232 ** contain a bound parameter because they were generated by older versions 1302 ** contain a bound parameter because they were generated by older versions
1233 ** of SQLite to be parsed by newer versions of SQLite without raising a 1303 ** of SQLite to be parsed by newer versions of SQLite without raising a
1234 ** malformed schema error. 1304 ** malformed schema error.
1235 */ 1305 */
1236 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ 1306 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
1237 1307
1238 /* If pWalker->u.i is 2 then any term of the expression that comes from 1308 /* If pWalker->eCode is 2 then any term of the expression that comes from
1239 ** the ON or USING clauses of a join disqualifies the expression 1309 ** the ON or USING clauses of a left join disqualifies the expression
1240 ** from being considered constant. */ 1310 ** from being considered constant. */
1241 if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ 1311 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
1242 pWalker->u.i = 0; 1312 pWalker->eCode = 0;
1243 return WRC_Abort; 1313 return WRC_Abort;
1244 } 1314 }
1245 1315
1246 switch( pExpr->op ){ 1316 switch( pExpr->op ){
1247 /* Consider functions to be constant if all their arguments are constant 1317 /* Consider functions to be constant if all their arguments are constant
1248 ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST 1318 ** and either pWalker->eCode==4 or 5 or the function has the
1249 ** flag. */ 1319 ** SQLITE_FUNC_CONST flag. */
1250 case TK_FUNCTION: 1320 case TK_FUNCTION:
1251 if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){ 1321 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
1252 return WRC_Continue; 1322 return WRC_Continue;
1323 }else{
1324 pWalker->eCode = 0;
1325 return WRC_Abort;
1253 } 1326 }
1254 /* Fall through */
1255 case TK_ID: 1327 case TK_ID:
1256 case TK_COLUMN: 1328 case TK_COLUMN:
1257 case TK_AGG_FUNCTION: 1329 case TK_AGG_FUNCTION:
1258 case TK_AGG_COLUMN: 1330 case TK_AGG_COLUMN:
1259 testcase( pExpr->op==TK_ID ); 1331 testcase( pExpr->op==TK_ID );
1260 testcase( pExpr->op==TK_COLUMN ); 1332 testcase( pExpr->op==TK_COLUMN );
1261 testcase( pExpr->op==TK_AGG_FUNCTION ); 1333 testcase( pExpr->op==TK_AGG_FUNCTION );
1262 testcase( pExpr->op==TK_AGG_COLUMN ); 1334 testcase( pExpr->op==TK_AGG_COLUMN );
1263 pWalker->u.i = 0; 1335 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
1264 return WRC_Abort; 1336 return WRC_Continue;
1337 }else{
1338 pWalker->eCode = 0;
1339 return WRC_Abort;
1340 }
1265 case TK_VARIABLE: 1341 case TK_VARIABLE:
1266 if( pWalker->u.i==4 ){ 1342 if( pWalker->eCode==5 ){
1267 /* Silently convert bound parameters that appear inside of CREATE 1343 /* Silently convert bound parameters that appear inside of CREATE
1268 ** statements into a NULL when parsing the CREATE statement text out 1344 ** statements into a NULL when parsing the CREATE statement text out
1269 ** of the sqlite_master table */ 1345 ** of the sqlite_master table */
1270 pExpr->op = TK_NULL; 1346 pExpr->op = TK_NULL;
1271 }else if( pWalker->u.i==3 ){ 1347 }else if( pWalker->eCode==4 ){
1272 /* A bound parameter in a CREATE statement that originates from 1348 /* A bound parameter in a CREATE statement that originates from
1273 ** sqlite3_prepare() causes an error */ 1349 ** sqlite3_prepare() causes an error */
1274 pWalker->u.i = 0; 1350 pWalker->eCode = 0;
1275 return WRC_Abort; 1351 return WRC_Abort;
1276 } 1352 }
1277 /* Fall through */ 1353 /* Fall through */
1278 default: 1354 default:
1279 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ 1355 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
1280 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ 1356 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
1281 return WRC_Continue; 1357 return WRC_Continue;
1282 } 1358 }
1283 } 1359 }
1284 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ 1360 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
1285 UNUSED_PARAMETER(NotUsed); 1361 UNUSED_PARAMETER(NotUsed);
1286 pWalker->u.i = 0; 1362 pWalker->eCode = 0;
1287 return WRC_Abort; 1363 return WRC_Abort;
1288 } 1364 }
1289 static int exprIsConst(Expr *p, int initFlag){ 1365 static int exprIsConst(Expr *p, int initFlag, int iCur){
1290 Walker w; 1366 Walker w;
1291 memset(&w, 0, sizeof(w)); 1367 memset(&w, 0, sizeof(w));
1292 w.u.i = initFlag; 1368 w.eCode = initFlag;
1293 w.xExprCallback = exprNodeIsConstant; 1369 w.xExprCallback = exprNodeIsConstant;
1294 w.xSelectCallback = selectNodeIsConstant; 1370 w.xSelectCallback = selectNodeIsConstant;
1371 w.u.iCur = iCur;
1295 sqlite3WalkExpr(&w, p); 1372 sqlite3WalkExpr(&w, p);
1296 return w.u.i; 1373 return w.eCode;
1297 } 1374 }
1298 1375
1299 /* 1376 /*
1300 ** Walk an expression tree. Return 1 if the expression is constant 1377 ** Walk an expression tree. Return non-zero if the expression is constant
1301 ** and 0 if it involves variables or function calls. 1378 ** and 0 if it involves variables or function calls.
1302 ** 1379 **
1303 ** For the purposes of this function, a double-quoted string (ex: "abc") 1380 ** For the purposes of this function, a double-quoted string (ex: "abc")
1304 ** is considered a variable but a single-quoted string (ex: 'abc') is 1381 ** is considered a variable but a single-quoted string (ex: 'abc') is
1305 ** a constant. 1382 ** a constant.
1306 */ 1383 */
1307 int sqlite3ExprIsConstant(Expr *p){ 1384 int sqlite3ExprIsConstant(Expr *p){
1308 return exprIsConst(p, 1); 1385 return exprIsConst(p, 1, 0);
1309 } 1386 }
1310 1387
1311 /* 1388 /*
1312 ** Walk an expression tree. Return 1 if the expression is constant 1389 ** Walk an expression tree. Return non-zero if the expression is constant
1313 ** that does no originate from the ON or USING clauses of a join. 1390 ** that does no originate from the ON or USING clauses of a join.
1314 ** Return 0 if it involves variables or function calls or terms from 1391 ** Return 0 if it involves variables or function calls or terms from
1315 ** an ON or USING clause. 1392 ** an ON or USING clause.
1316 */ 1393 */
1317 int sqlite3ExprIsConstantNotJoin(Expr *p){ 1394 int sqlite3ExprIsConstantNotJoin(Expr *p){
1318 return exprIsConst(p, 2); 1395 return exprIsConst(p, 2, 0);
1319 } 1396 }
1320 1397
1321 /* 1398 /*
1322 ** Walk an expression tree. Return 1 if the expression is constant 1399 ** Walk an expression tree. Return non-zero if the expression is constant
1400 ** for any single row of the table with cursor iCur. In other words, the
1401 ** expression must not refer to any non-deterministic function nor any
1402 ** table other than iCur.
1403 */
1404 int sqlite3ExprIsTableConstant(Expr *p, int iCur){
1405 return exprIsConst(p, 3, iCur);
1406 }
1407
1408 /*
1409 ** Walk an expression tree. Return non-zero if the expression is constant
1323 ** or a function call with constant arguments. Return and 0 if there 1410 ** or a function call with constant arguments. Return and 0 if there
1324 ** are any variables. 1411 ** are any variables.
1325 ** 1412 **
1326 ** For the purposes of this function, a double-quoted string (ex: "abc") 1413 ** For the purposes of this function, a double-quoted string (ex: "abc")
1327 ** is considered a variable but a single-quoted string (ex: 'abc') is 1414 ** is considered a variable but a single-quoted string (ex: 'abc') is
1328 ** a constant. 1415 ** a constant.
1329 */ 1416 */
1330 int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ 1417 int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
1331 assert( isInit==0 || isInit==1 ); 1418 assert( isInit==0 || isInit==1 );
1332 return exprIsConst(p, 3+isInit); 1419 return exprIsConst(p, 4+isInit, 0);
1333 } 1420 }
1334 1421
1422 #ifdef SQLITE_ENABLE_CURSOR_HINTS
1423 /*
1424 ** Walk an expression tree. Return 1 if the expression contains a
1425 ** subquery of some kind. Return 0 if there are no subqueries.
1426 */
1427 int sqlite3ExprContainsSubquery(Expr *p){
1428 Walker w;
1429 memset(&w, 0, sizeof(w));
1430 w.eCode = 1;
1431 w.xExprCallback = sqlite3ExprWalkNoop;
1432 w.xSelectCallback = selectNodeIsConstant;
1433 sqlite3WalkExpr(&w, p);
1434 return w.eCode==0;
1435 }
1436 #endif
1437
1335 /* 1438 /*
1336 ** If the expression p codes a constant integer that is small enough 1439 ** If the expression p codes a constant integer that is small enough
1337 ** to fit in a 32-bit integer, return 1 and put the value of the integer 1440 ** to fit in a 32-bit integer, return 1 and put the value of the integer
1338 ** in *pValue. If the expression is not an integer or if it is too big 1441 ** in *pValue. If the expression is not an integer or if it is too big
1339 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 1442 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
1340 */ 1443 */
1341 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 1444 int sqlite3ExprIsInteger(Expr *p, int *pValue){
1342 int rc = 0; 1445 int rc = 0;
1343 1446
1344 /* If an expression is an integer literal that fits in a signed 32-bit 1447 /* If an expression is an integer literal that fits in a signed 32-bit
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1408 ** unchanged by OP_Affinity with the affinity given in the second 1511 ** unchanged by OP_Affinity with the affinity given in the second
1409 ** argument. 1512 ** argument.
1410 ** 1513 **
1411 ** This routine is used to determine if the OP_Affinity operation 1514 ** This routine is used to determine if the OP_Affinity operation
1412 ** can be omitted. When in doubt return FALSE. A false negative 1515 ** can be omitted. When in doubt return FALSE. A false negative
1413 ** is harmless. A false positive, however, can result in the wrong 1516 ** is harmless. A false positive, however, can result in the wrong
1414 ** answer. 1517 ** answer.
1415 */ 1518 */
1416 int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ 1519 int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
1417 u8 op; 1520 u8 op;
1418 if( aff==SQLITE_AFF_NONE ) return 1; 1521 if( aff==SQLITE_AFF_BLOB ) return 1;
1419 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1522 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
1420 op = p->op; 1523 op = p->op;
1421 if( op==TK_REGISTER ) op = p->op2; 1524 if( op==TK_REGISTER ) op = p->op2;
1422 switch( op ){ 1525 switch( op ){
1423 case TK_INTEGER: { 1526 case TK_INTEGER: {
1424 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; 1527 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
1425 } 1528 }
1426 case TK_FLOAT: { 1529 case TK_FLOAT: {
1427 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; 1530 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
1428 } 1531 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1505 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); 1608 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
1506 } 1609 }
1507 1610
1508 /* 1611 /*
1509 ** Generate code that checks the left-most column of index table iCur to see if 1612 ** Generate code that checks the left-most column of index table iCur to see if
1510 ** it contains any NULL entries. Cause the register at regHasNull to be set 1613 ** it contains any NULL entries. Cause the register at regHasNull to be set
1511 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull 1614 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
1512 ** to be set to NULL if iCur contains one or more NULL values. 1615 ** to be set to NULL if iCur contains one or more NULL values.
1513 */ 1616 */
1514 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ 1617 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
1515 int j1; 1618 int addr1;
1516 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); 1619 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
1517 j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); 1620 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
1518 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); 1621 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
1519 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 1622 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1520 VdbeComment((v, "first_entry_in(%d)", iCur)); 1623 VdbeComment((v, "first_entry_in(%d)", iCur));
1521 sqlite3VdbeJumpHere(v, j1); 1624 sqlite3VdbeJumpHere(v, addr1);
1522 } 1625 }
1523 1626
1524 1627
1525 #ifndef SQLITE_OMIT_SUBQUERY 1628 #ifndef SQLITE_OMIT_SUBQUERY
1526 /* 1629 /*
1527 ** The argument is an IN operator with a list (not a subquery) on the 1630 ** The argument is an IN operator with a list (not a subquery) on the
1528 ** right-hand side. Return TRUE if that list is constant. 1631 ** right-hand side. Return TRUE if that list is constant.
1529 */ 1632 */
1530 static int sqlite3InRhsIsConstant(Expr *pIn){ 1633 static int sqlite3InRhsIsConstant(Expr *pIn){
1531 Expr *pLHS; 1634 Expr *pLHS;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 1721 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
1619 1722
1620 assert( pX->op==TK_IN ); 1723 assert( pX->op==TK_IN );
1621 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; 1724 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
1622 1725
1623 /* Check to see if an existing table or index can be used to 1726 /* Check to see if an existing table or index can be used to
1624 ** satisfy the query. This is preferable to generating a new 1727 ** satisfy the query. This is preferable to generating a new
1625 ** ephemeral table. 1728 ** ephemeral table.
1626 */ 1729 */
1627 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); 1730 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
1628 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ 1731 if( pParse->nErr==0 && isCandidateForInOpt(p) ){
1629 sqlite3 *db = pParse->db; /* Database connection */ 1732 sqlite3 *db = pParse->db; /* Database connection */
1630 Table *pTab; /* Table <table>. */ 1733 Table *pTab; /* Table <table>. */
1631 Expr *pExpr; /* Expression <column> */ 1734 Expr *pExpr; /* Expression <column> */
1632 i16 iCol; /* Index of column <column> */ 1735 i16 iCol; /* Index of column <column> */
1633 i16 iDb; /* Database idx for pTab */ 1736 i16 iDb; /* Database idx for pTab */
1634 1737
1635 assert( p ); /* Because of isCandidateForInOpt(p) */ 1738 assert( p ); /* Because of isCandidateForInOpt(p) */
1636 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ 1739 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
1637 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ 1740 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
1638 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ 1741 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1783 ** 1886 **
1784 ** If all of the above are false, then we can run this code just once 1887 ** If all of the above are false, then we can run this code just once
1785 ** save the results, and reuse the same result on subsequent invocations. 1888 ** save the results, and reuse the same result on subsequent invocations.
1786 */ 1889 */
1787 if( !ExprHasProperty(pExpr, EP_VarSelect) ){ 1890 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
1788 jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); 1891 jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
1789 } 1892 }
1790 1893
1791 #ifndef SQLITE_OMIT_EXPLAIN 1894 #ifndef SQLITE_OMIT_EXPLAIN
1792 if( pParse->explain==2 ){ 1895 if( pParse->explain==2 ){
1793 char *zMsg = sqlite3MPrintf( 1896 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
1794 pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED " , 1897 jmpIfDynamic>=0?"":"CORRELATED ",
1795 pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId 1898 pExpr->op==TK_IN?"LIST":"SCALAR",
1899 pParse->iNextSelectId
1796 ); 1900 );
1797 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); 1901 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
1798 } 1902 }
1799 #endif 1903 #endif
1800 1904
1801 switch( pExpr->op ){ 1905 switch( pExpr->op ){
1802 case TK_IN: { 1906 case TK_IN: {
1803 char affinity; /* Affinity of the LHS of the IN */ 1907 char affinity; /* Affinity of the LHS of the IN */
1804 int addr; /* Address of OP_OpenEphemeral instruction */ 1908 int addr; /* Address of OP_OpenEphemeral instruction */
1805 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ 1909 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1859 ** store it in the temporary table. If <expr> is a column, then use 1963 ** store it in the temporary table. If <expr> is a column, then use
1860 ** that columns affinity when building index keys. If <expr> is not 1964 ** that columns affinity when building index keys. If <expr> is not
1861 ** a column, use numeric affinity. 1965 ** a column, use numeric affinity.
1862 */ 1966 */
1863 int i; 1967 int i;
1864 ExprList *pList = pExpr->x.pList; 1968 ExprList *pList = pExpr->x.pList;
1865 struct ExprList_item *pItem; 1969 struct ExprList_item *pItem;
1866 int r1, r2, r3; 1970 int r1, r2, r3;
1867 1971
1868 if( !affinity ){ 1972 if( !affinity ){
1869 affinity = SQLITE_AFF_NONE; 1973 affinity = SQLITE_AFF_BLOB;
1870 } 1974 }
1871 if( pKeyInfo ){ 1975 if( pKeyInfo ){
1872 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 1976 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
1873 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 1977 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
1874 } 1978 }
1875 1979
1876 /* Loop through each expression in <exprlist>. */ 1980 /* Loop through each expression in <exprlist>. */
1877 r1 = sqlite3GetTempReg(pParse); 1981 r1 = sqlite3GetTempReg(pParse);
1878 r2 = sqlite3GetTempReg(pParse); 1982 r2 = sqlite3GetTempReg(pParse);
1879 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); 1983 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1943 VdbeComment((v, "Init subquery result")); 2047 VdbeComment((v, "Init subquery result"));
1944 }else{ 2048 }else{
1945 dest.eDest = SRT_Exists; 2049 dest.eDest = SRT_Exists;
1946 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); 2050 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
1947 VdbeComment((v, "Init EXISTS result")); 2051 VdbeComment((v, "Init EXISTS result"));
1948 } 2052 }
1949 sqlite3ExprDelete(pParse->db, pSel->pLimit); 2053 sqlite3ExprDelete(pParse->db, pSel->pLimit);
1950 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, 2054 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
1951 &sqlite3IntTokens[1]); 2055 &sqlite3IntTokens[1]);
1952 pSel->iLimit = 0; 2056 pSel->iLimit = 0;
2057 pSel->selFlags &= ~SF_MultiValue;
1953 if( sqlite3Select(pParse, pSel, &dest) ){ 2058 if( sqlite3Select(pParse, pSel, &dest) ){
1954 return 0; 2059 return 0;
1955 } 2060 }
1956 rReg = dest.iSDParm; 2061 rReg = dest.iSDParm;
1957 ExprSetVVAProperty(pExpr, EP_NoReduce); 2062 ExprSetVVAProperty(pExpr, EP_NoReduce);
1958 break; 2063 break;
1959 } 2064 }
1960 } 2065 }
1961 2066
1962 if( rHasNullFlag ){ 2067 if( rHasNullFlag ){
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
2054 }else{ 2159 }else{
2055 assert( destIfNull==destIfFalse ); 2160 assert( destIfNull==destIfFalse );
2056 sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, 2161 sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
2057 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); 2162 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
2058 sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL); 2163 sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
2059 } 2164 }
2060 sqlite3ReleaseTempReg(pParse, regToFree); 2165 sqlite3ReleaseTempReg(pParse, regToFree);
2061 } 2166 }
2062 if( regCkNull ){ 2167 if( regCkNull ){
2063 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); 2168 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
2064 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 2169 sqlite3VdbeGoto(v, destIfFalse);
2065 } 2170 }
2066 sqlite3VdbeResolveLabel(v, labelOk); 2171 sqlite3VdbeResolveLabel(v, labelOk);
2067 sqlite3ReleaseTempReg(pParse, regCkNull); 2172 sqlite3ReleaseTempReg(pParse, regCkNull);
2068 }else{ 2173 }else{
2069 2174
2070 /* If the LHS is NULL, then the result is either false or NULL depending 2175 /* If the LHS is NULL, then the result is either false or NULL depending
2071 ** on whether the RHS is empty or not, respectively. 2176 ** on whether the RHS is empty or not, respectively.
2072 */ 2177 */
2073 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ 2178 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
2074 if( destIfNull==destIfFalse ){ 2179 if( destIfNull==destIfFalse ){
2075 /* Shortcut for the common case where the false and NULL outcomes are 2180 /* Shortcut for the common case where the false and NULL outcomes are
2076 ** the same. */ 2181 ** the same. */
2077 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v); 2182 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
2078 }else{ 2183 }else{
2079 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); 2184 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
2080 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 2185 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
2081 VdbeCoverage(v); 2186 VdbeCoverage(v);
2082 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); 2187 sqlite3VdbeGoto(v, destIfNull);
2083 sqlite3VdbeJumpHere(v, addr1); 2188 sqlite3VdbeJumpHere(v, addr1);
2084 } 2189 }
2085 } 2190 }
2086 2191
2087 if( eType==IN_INDEX_ROWID ){ 2192 if( eType==IN_INDEX_ROWID ){
2088 /* In this case, the RHS is the ROWID of table b-tree 2193 /* In this case, the RHS is the ROWID of table b-tree
2089 */ 2194 */
2090 sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v); 2195 sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
2091 sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1); 2196 sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
2092 VdbeCoverage(v); 2197 VdbeCoverage(v);
(...skipping 17 matching lines...) Expand all
2110 ** Also run this branch if NULL is equivalent to FALSE 2215 ** Also run this branch if NULL is equivalent to FALSE
2111 ** for this particular IN operator. 2216 ** for this particular IN operator.
2112 */ 2217 */
2113 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); 2218 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
2114 VdbeCoverage(v); 2219 VdbeCoverage(v);
2115 }else{ 2220 }else{
2116 /* In this branch, the RHS of the IN might contain a NULL and 2221 /* In this branch, the RHS of the IN might contain a NULL and
2117 ** the presence of a NULL on the RHS makes a difference in the 2222 ** the presence of a NULL on the RHS makes a difference in the
2118 ** outcome. 2223 ** outcome.
2119 */ 2224 */
2120 int j1; 2225 int addr1;
2121 2226
2122 /* First check to see if the LHS is contained in the RHS. If so, 2227 /* First check to see if the LHS is contained in the RHS. If so,
2123 ** then the answer is TRUE the presence of NULLs in the RHS does 2228 ** then the answer is TRUE the presence of NULLs in the RHS does
2124 ** not matter. If the LHS is not contained in the RHS, then the 2229 ** not matter. If the LHS is not contained in the RHS, then the
2125 ** answer is NULL if the RHS contains NULLs and the answer is 2230 ** answer is NULL if the RHS contains NULLs and the answer is
2126 ** FALSE if the RHS is NULL-free. 2231 ** FALSE if the RHS is NULL-free.
2127 */ 2232 */
2128 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); 2233 addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
2129 VdbeCoverage(v); 2234 VdbeCoverage(v);
2130 sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); 2235 sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
2131 VdbeCoverage(v); 2236 VdbeCoverage(v);
2132 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 2237 sqlite3VdbeGoto(v, destIfFalse);
2133 sqlite3VdbeJumpHere(v, j1); 2238 sqlite3VdbeJumpHere(v, addr1);
2134 } 2239 }
2135 } 2240 }
2136 } 2241 }
2137 sqlite3ReleaseTempReg(pParse, r1); 2242 sqlite3ReleaseTempReg(pParse, r1);
2138 sqlite3ExprCachePop(pParse); 2243 sqlite3ExprCachePop(pParse);
2139 VdbeComment((v, "end IN expr")); 2244 VdbeComment((v, "end IN expr"));
2140 } 2245 }
2141 #endif /* SQLITE_OMIT_SUBQUERY */ 2246 #endif /* SQLITE_OMIT_SUBQUERY */
2142 2247
2143 /*
2144 ** Duplicate an 8-byte value
2145 */
2146 static char *dup8bytes(Vdbe *v, const char *in){
2147 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
2148 if( out ){
2149 memcpy(out, in, 8);
2150 }
2151 return out;
2152 }
2153
2154 #ifndef SQLITE_OMIT_FLOATING_POINT 2248 #ifndef SQLITE_OMIT_FLOATING_POINT
2155 /* 2249 /*
2156 ** Generate an instruction that will put the floating point 2250 ** Generate an instruction that will put the floating point
2157 ** value described by z[0..n-1] into register iMem. 2251 ** value described by z[0..n-1] into register iMem.
2158 ** 2252 **
2159 ** The z[] string will probably not be zero-terminated. But the 2253 ** The z[] string will probably not be zero-terminated. But the
2160 ** z[n] character is guaranteed to be something that does not look 2254 ** z[n] character is guaranteed to be something that does not look
2161 ** like the continuation of the number. 2255 ** like the continuation of the number.
2162 */ 2256 */
2163 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ 2257 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
2164 if( ALWAYS(z!=0) ){ 2258 if( ALWAYS(z!=0) ){
2165 double value; 2259 double value;
2166 char *zV;
2167 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); 2260 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
2168 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ 2261 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
2169 if( negateFlag ) value = -value; 2262 if( negateFlag ) value = -value;
2170 zV = dup8bytes(v, (char*)&value); 2263 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
2171 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
2172 } 2264 }
2173 } 2265 }
2174 #endif 2266 #endif
2175 2267
2176 2268
2177 /* 2269 /*
2178 ** Generate an instruction that will put the integer describe by 2270 ** Generate an instruction that will put the integer describe by
2179 ** text z[0..n-1] into register iMem. 2271 ** text z[0..n-1] into register iMem.
2180 ** 2272 **
2181 ** Expr.u.zToken is always UTF8 and zero-terminated. 2273 ** Expr.u.zToken is always UTF8 and zero-terminated.
2182 */ 2274 */
2183 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ 2275 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
2184 Vdbe *v = pParse->pVdbe; 2276 Vdbe *v = pParse->pVdbe;
2185 if( pExpr->flags & EP_IntValue ){ 2277 if( pExpr->flags & EP_IntValue ){
2186 int i = pExpr->u.iValue; 2278 int i = pExpr->u.iValue;
2187 assert( i>=0 ); 2279 assert( i>=0 );
2188 if( negFlag ) i = -i; 2280 if( negFlag ) i = -i;
2189 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 2281 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
2190 }else{ 2282 }else{
2191 int c; 2283 int c;
2192 i64 value; 2284 i64 value;
2193 const char *z = pExpr->u.zToken; 2285 const char *z = pExpr->u.zToken;
2194 assert( z!=0 ); 2286 assert( z!=0 );
2195 c = sqlite3DecOrHexToI64(z, &value); 2287 c = sqlite3DecOrHexToI64(z, &value);
2196 if( c==0 || (c==2 && negFlag) ){ 2288 if( c==0 || (c==2 && negFlag) ){
2197 char *zV;
2198 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } 2289 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
2199 zV = dup8bytes(v, (char*)&value); 2290 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
2200 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
2201 }else{ 2291 }else{
2202 #ifdef SQLITE_OMIT_FLOATING_POINT 2292 #ifdef SQLITE_OMIT_FLOATING_POINT
2203 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); 2293 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
2204 #else 2294 #else
2205 #ifndef SQLITE_OMIT_HEX_INTEGER 2295 #ifndef SQLITE_OMIT_HEX_INTEGER
2206 if( sqlite3_strnicmp(z,"0x",2)==0 ){ 2296 if( sqlite3_strnicmp(z,"0x",2)==0 ){
2207 sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); 2297 sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
2208 }else 2298 }else
2209 #endif 2299 #endif
2210 { 2300 {
(...skipping 20 matching lines...) Expand all
2231 /* 2321 /*
2232 ** Record in the column cache that a particular column from a 2322 ** Record in the column cache that a particular column from a
2233 ** particular table is stored in a particular register. 2323 ** particular table is stored in a particular register.
2234 */ 2324 */
2235 void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ 2325 void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
2236 int i; 2326 int i;
2237 int minLru; 2327 int minLru;
2238 int idxLru; 2328 int idxLru;
2239 struct yColCache *p; 2329 struct yColCache *p;
2240 2330
2241 assert( iReg>0 ); /* Register numbers are always positive */ 2331 /* Unless an error has occurred, register numbers are always positive. */
2332 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
2242 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ 2333 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
2243 2334
2244 /* The SQLITE_ColumnCache flag disables the column cache. This is used 2335 /* The SQLITE_ColumnCache flag disables the column cache. This is used
2245 ** for testing only - to verify that SQLite always gets the same answer 2336 ** for testing only - to verify that SQLite always gets the same answer
2246 ** with and without the column cache. 2337 ** with and without the column cache.
2247 */ 2338 */
2248 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; 2339 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
2249 2340
2250 /* First replace any existing entry. 2341 /* First replace any existing entry.
2251 ** 2342 **
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
2355 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ 2446 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
2356 int i; 2447 int i;
2357 struct yColCache *p; 2448 struct yColCache *p;
2358 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2449 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2359 if( p->iReg==iReg ){ 2450 if( p->iReg==iReg ){
2360 p->tempReg = 0; 2451 p->tempReg = 0;
2361 } 2452 }
2362 } 2453 }
2363 } 2454 }
2364 2455
2456 /* Generate code that will load into register regOut a value that is
2457 ** appropriate for the iIdxCol-th column of index pIdx.
2458 */
2459 void sqlite3ExprCodeLoadIndexColumn(
2460 Parse *pParse, /* The parsing context */
2461 Index *pIdx, /* The index whose column is to be loaded */
2462 int iTabCur, /* Cursor pointing to a table row */
2463 int iIdxCol, /* The column of the index to be loaded */
2464 int regOut /* Store the index column value in this register */
2465 ){
2466 i16 iTabCol = pIdx->aiColumn[iIdxCol];
2467 if( iTabCol==XN_EXPR ){
2468 assert( pIdx->aColExpr );
2469 assert( pIdx->aColExpr->nExpr>iIdxCol );
2470 pParse->iSelfTab = iTabCur;
2471 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
2472 }else{
2473 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
2474 iTabCol, regOut);
2475 }
2476 }
2477
2365 /* 2478 /*
2366 ** Generate code to extract the value of the iCol-th column of a table. 2479 ** Generate code to extract the value of the iCol-th column of a table.
2367 */ 2480 */
2368 void sqlite3ExprCodeGetColumnOfTable( 2481 void sqlite3ExprCodeGetColumnOfTable(
2369 Vdbe *v, /* The VDBE under construction */ 2482 Vdbe *v, /* The VDBE under construction */
2370 Table *pTab, /* The table containing the value */ 2483 Table *pTab, /* The table containing the value */
2371 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ 2484 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
2372 int iCol, /* Index of the column to extract */ 2485 int iCol, /* Index of the column to extract */
2373 int regOut /* Extract the value into this register */ 2486 int regOut /* Extract the value into this register */
2374 ){ 2487 ){
2375 if( iCol<0 || iCol==pTab->iPKey ){ 2488 if( iCol<0 || iCol==pTab->iPKey ){
2376 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); 2489 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
2377 }else{ 2490 }else{
2378 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 2491 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
2379 int x = iCol; 2492 int x = iCol;
2380 if( !HasRowid(pTab) ){ 2493 if( !HasRowid(pTab) ){
2381 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); 2494 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
2382 } 2495 }
2383 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); 2496 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
2384 } 2497 }
2385 if( iCol>=0 ){ 2498 if( iCol>=0 ){
2386 sqlite3ColumnDefault(v, pTab, iCol, regOut); 2499 sqlite3ColumnDefault(v, pTab, iCol, regOut);
2387 } 2500 }
2388 } 2501 }
2389 2502
2390 /* 2503 /*
2391 ** Generate code that will extract the iColumn-th column from 2504 ** Generate code that will extract the iColumn-th column from
2392 ** table pTab and store the column value in a register. An effort 2505 ** table pTab and store the column value in a register.
2393 ** is made to store the column value in register iReg, but this is 2506 **
2394 ** not guaranteed. The location of the column value is returned. 2507 ** An effort is made to store the column value in register iReg. This
2508 ** is not garanteeed for GetColumn() - the result can be stored in
2509 ** any register. But the result is guaranteed to land in register iReg
2510 ** for GetColumnToReg().
2395 ** 2511 **
2396 ** There must be an open cursor to pTab in iTable when this routine 2512 ** There must be an open cursor to pTab in iTable when this routine
2397 ** is called. If iColumn<0 then code is generated that extracts the rowid. 2513 ** is called. If iColumn<0 then code is generated that extracts the rowid.
2398 */ 2514 */
2399 int sqlite3ExprCodeGetColumn( 2515 int sqlite3ExprCodeGetColumn(
2400 Parse *pParse, /* Parsing and code generating context */ 2516 Parse *pParse, /* Parsing and code generating context */
2401 Table *pTab, /* Description of the table we are reading from */ 2517 Table *pTab, /* Description of the table we are reading from */
2402 int iColumn, /* Index of the table column */ 2518 int iColumn, /* Index of the table column */
2403 int iTable, /* The cursor pointing to the table */ 2519 int iTable, /* The cursor pointing to the table */
2404 int iReg, /* Store results here */ 2520 int iReg, /* Store results here */
2405 u8 p5 /* P5 value for OP_Column */ 2521 u8 p5 /* P5 value for OP_Column + FLAGS */
2406 ){ 2522 ){
2407 Vdbe *v = pParse->pVdbe; 2523 Vdbe *v = pParse->pVdbe;
2408 int i; 2524 int i;
2409 struct yColCache *p; 2525 struct yColCache *p;
2410 2526
2411 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2527 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2412 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ 2528 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
2413 p->lru = pParse->iCacheCnt++; 2529 p->lru = pParse->iCacheCnt++;
2414 sqlite3ExprCachePinRegister(pParse, p->iReg); 2530 sqlite3ExprCachePinRegister(pParse, p->iReg);
2415 return p->iReg; 2531 return p->iReg;
2416 } 2532 }
2417 } 2533 }
2418 assert( v!=0 ); 2534 assert( v!=0 );
2419 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); 2535 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
2420 if( p5 ){ 2536 if( p5 ){
2421 sqlite3VdbeChangeP5(v, p5); 2537 sqlite3VdbeChangeP5(v, p5);
2422 }else{ 2538 }else{
2423 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); 2539 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
2424 } 2540 }
2425 return iReg; 2541 return iReg;
2426 } 2542 }
2543 void sqlite3ExprCodeGetColumnToReg(
2544 Parse *pParse, /* Parsing and code generating context */
2545 Table *pTab, /* Description of the table we are reading from */
2546 int iColumn, /* Index of the table column */
2547 int iTable, /* The cursor pointing to the table */
2548 int iReg /* Store results here */
2549 ){
2550 int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0);
2551 if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
2552 }
2553
2427 2554
2428 /* 2555 /*
2429 ** Clear all column cache entries. 2556 ** Clear all column cache entries.
2430 */ 2557 */
2431 void sqlite3ExprCacheClear(Parse *pParse){ 2558 void sqlite3ExprCacheClear(Parse *pParse){
2432 int i; 2559 int i;
2433 struct yColCache *p; 2560 struct yColCache *p;
2434 2561
2435 #if SQLITE_DEBUG 2562 #if SQLITE_DEBUG
2436 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 2563 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
2540 /* Otherwise, fall thru into the TK_COLUMN case */ 2667 /* Otherwise, fall thru into the TK_COLUMN case */
2541 } 2668 }
2542 case TK_COLUMN: { 2669 case TK_COLUMN: {
2543 int iTab = pExpr->iTable; 2670 int iTab = pExpr->iTable;
2544 if( iTab<0 ){ 2671 if( iTab<0 ){
2545 if( pParse->ckBase>0 ){ 2672 if( pParse->ckBase>0 ){
2546 /* Generating CHECK constraints or inserting into partial index */ 2673 /* Generating CHECK constraints or inserting into partial index */
2547 inReg = pExpr->iColumn + pParse->ckBase; 2674 inReg = pExpr->iColumn + pParse->ckBase;
2548 break; 2675 break;
2549 }else{ 2676 }else{
2550 /* Deleting from a partial index */ 2677 /* Coding an expression that is part of an index where column names
2551 iTab = pParse->iPartIdxTab; 2678 ** in the index refer to the table to which the index belongs */
2679 iTab = pParse->iSelfTab;
2552 } 2680 }
2553 } 2681 }
2554 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 2682 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
2555 pExpr->iColumn, iTab, target, 2683 pExpr->iColumn, iTab, target,
2556 pExpr->op2); 2684 pExpr->op2);
2557 break; 2685 break;
2558 } 2686 }
2559 case TK_INTEGER: { 2687 case TK_INTEGER: {
2560 codeInteger(pParse, pExpr, 0, target); 2688 codeInteger(pParse, pExpr, 0, target);
2561 break; 2689 break;
2562 } 2690 }
2563 #ifndef SQLITE_OMIT_FLOATING_POINT 2691 #ifndef SQLITE_OMIT_FLOATING_POINT
2564 case TK_FLOAT: { 2692 case TK_FLOAT: {
2565 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 2693 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2566 codeReal(v, pExpr->u.zToken, 0, target); 2694 codeReal(v, pExpr->u.zToken, 0, target);
2567 break; 2695 break;
2568 } 2696 }
2569 #endif 2697 #endif
2570 case TK_STRING: { 2698 case TK_STRING: {
2571 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 2699 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2572 sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0); 2700 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
2573 break; 2701 break;
2574 } 2702 }
2575 case TK_NULL: { 2703 case TK_NULL: {
2576 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 2704 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
2577 break; 2705 break;
2578 } 2706 }
2579 #ifndef SQLITE_OMIT_BLOB_LITERAL 2707 #ifndef SQLITE_OMIT_BLOB_LITERAL
2580 case TK_BLOB: { 2708 case TK_BLOB: {
2581 int n; 2709 int n;
2582 const char *z; 2710 const char *z;
(...skipping 18 matching lines...) Expand all
2601 assert( pExpr->u.zToken[0]=='?' 2729 assert( pExpr->u.zToken[0]=='?'
2602 || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); 2730 || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
2603 sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); 2731 sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
2604 } 2732 }
2605 break; 2733 break;
2606 } 2734 }
2607 case TK_REGISTER: { 2735 case TK_REGISTER: {
2608 inReg = pExpr->iTable; 2736 inReg = pExpr->iTable;
2609 break; 2737 break;
2610 } 2738 }
2611 case TK_AS: {
2612 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2613 break;
2614 }
2615 #ifndef SQLITE_OMIT_CAST 2739 #ifndef SQLITE_OMIT_CAST
2616 case TK_CAST: { 2740 case TK_CAST: {
2617 /* Expressions of the form: CAST(pLeft AS token) */ 2741 /* Expressions of the form: CAST(pLeft AS token) */
2618 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 2742 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2619 if( inReg!=target ){ 2743 if( inReg!=target ){
2620 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); 2744 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
2621 inReg = target; 2745 inReg = target;
2622 } 2746 }
2623 sqlite3VdbeAddOp2(v, OP_Cast, target, 2747 sqlite3VdbeAddOp2(v, OP_Cast, target,
2624 sqlite3AffinityType(pExpr->u.zToken, 0)); 2748 sqlite3AffinityType(pExpr->u.zToken, 0));
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
2794 } 2918 }
2795 sqlite3VdbeResolveLabel(v, endCoalesce); 2919 sqlite3VdbeResolveLabel(v, endCoalesce);
2796 break; 2920 break;
2797 } 2921 }
2798 2922
2799 /* The UNLIKELY() function is a no-op. The result is the value 2923 /* The UNLIKELY() function is a no-op. The result is the value
2800 ** of the first argument. 2924 ** of the first argument.
2801 */ 2925 */
2802 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 2926 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
2803 assert( nFarg>=1 ); 2927 assert( nFarg>=1 );
2804 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 2928 inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
2805 break; 2929 break;
2806 } 2930 }
2807 2931
2808 for(i=0; i<nFarg; i++){ 2932 for(i=0; i<nFarg; i++){
2809 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ 2933 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
2810 testcase( i==31 ); 2934 testcase( i==31 );
2811 constMask |= MASKBIT32(i); 2935 constMask |= MASKBIT32(i);
2812 } 2936 }
2813 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ 2937 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
2814 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); 2938 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
(...skipping 20 matching lines...) Expand all
2835 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ 2959 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
2836 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); 2960 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
2837 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); 2961 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
2838 testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); 2962 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
2839 pFarg->a[0].pExpr->op2 = 2963 pFarg->a[0].pExpr->op2 =
2840 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); 2964 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
2841 } 2965 }
2842 } 2966 }
2843 2967
2844 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ 2968 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */
2845 sqlite3ExprCodeExprList(pParse, pFarg, r1, 2969 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
2846 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); 2970 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
2847 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ 2971 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */
2848 }else{ 2972 }else{
2849 r1 = 0; 2973 r1 = 0;
2850 } 2974 }
2851 #ifndef SQLITE_OMIT_VIRTUALTABLE 2975 #ifndef SQLITE_OMIT_VIRTUALTABLE
2852 /* Possibly overload the function if the first argument is 2976 /* Possibly overload the function if the first argument is
2853 ** a virtual table column. 2977 ** a virtual table column.
2854 ** 2978 **
2855 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 2979 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2856 ** second argument, not the first, as the argument to test to 2980 ** second argument, not the first, as the argument to test to
2857 ** see if it is a column in a virtual table. This is done because 2981 ** see if it is a column in a virtual table. This is done because
2858 ** the left operand of infix functions (the operand we want to 2982 ** the left operand of infix functions (the operand we want to
2859 ** control overloading) ends up as the second argument to the 2983 ** control overloading) ends up as the second argument to the
2860 ** function. The expression "A glob B" is equivalent to 2984 ** function. The expression "A glob B" is equivalent to
2861 ** "glob(B,A). We want to use the A in "A glob B" to test 2985 ** "glob(B,A). We want to use the A in "A glob B" to test
2862 ** for function overloading. But we use the B term in "glob(B,A)". 2986 ** for function overloading. But we use the B term in "glob(B,A)".
2863 */ 2987 */
2864 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ 2988 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
2865 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); 2989 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
2866 }else if( nFarg>0 ){ 2990 }else if( nFarg>0 ){
2867 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); 2991 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
2868 } 2992 }
2869 #endif 2993 #endif
2870 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ 2994 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
2871 if( !pColl ) pColl = db->pDfltColl; 2995 if( !pColl ) pColl = db->pDfltColl;
2872 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 2996 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
2873 } 2997 }
2874 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, 2998 sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
2875 (char*)pDef, P4_FUNCDEF); 2999 (char*)pDef, P4_FUNCDEF);
2876 sqlite3VdbeChangeP5(v, (u8)nFarg); 3000 sqlite3VdbeChangeP5(v, (u8)nFarg);
2877 if( nFarg && constMask==0 ){ 3001 if( nFarg && constMask==0 ){
2878 sqlite3ReleaseTempRange(pParse, r1, nFarg); 3002 sqlite3ReleaseTempRange(pParse, r1, nFarg);
2879 } 3003 }
2880 break; 3004 break;
2881 } 3005 }
2882 #ifndef SQLITE_OMIT_SUBQUERY 3006 #ifndef SQLITE_OMIT_SUBQUERY
2883 case TK_EXISTS: 3007 case TK_EXISTS:
2884 case TK_SELECT: { 3008 case TK_SELECT: {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
2979 3103
2980 sqlite3VdbeAddOp2(v, OP_Param, p1, target); 3104 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
2981 VdbeComment((v, "%s.%s -> $%d", 3105 VdbeComment((v, "%s.%s -> $%d",
2982 (pExpr->iTable ? "new" : "old"), 3106 (pExpr->iTable ? "new" : "old"),
2983 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), 3107 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
2984 target 3108 target
2985 )); 3109 ));
2986 3110
2987 #ifndef SQLITE_OMIT_FLOATING_POINT 3111 #ifndef SQLITE_OMIT_FLOATING_POINT
2988 /* If the column has REAL affinity, it may currently be stored as an 3112 /* If the column has REAL affinity, it may currently be stored as an
2989 ** integer. Use OP_RealAffinity to make sure it is really real. */ 3113 ** integer. Use OP_RealAffinity to make sure it is really real.
3114 **
3115 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
3116 ** floating point when extracting it from the record. */
2990 if( pExpr->iColumn>=0 3117 if( pExpr->iColumn>=0
2991 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL 3118 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
2992 ){ 3119 ){
2993 sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 3120 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
2994 } 3121 }
2995 #endif 3122 #endif
2996 break; 3123 break;
2997 } 3124 }
2998 3125
2999 3126
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
3056 assert( pTest!=0 ); 3183 assert( pTest!=0 );
3057 opCompare.pRight = aListelem[i].pExpr; 3184 opCompare.pRight = aListelem[i].pExpr;
3058 }else{ 3185 }else{
3059 pTest = aListelem[i].pExpr; 3186 pTest = aListelem[i].pExpr;
3060 } 3187 }
3061 nextCase = sqlite3VdbeMakeLabel(v); 3188 nextCase = sqlite3VdbeMakeLabel(v);
3062 testcase( pTest->op==TK_COLUMN ); 3189 testcase( pTest->op==TK_COLUMN );
3063 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 3190 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
3064 testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 3191 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
3065 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 3192 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
3066 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); 3193 sqlite3VdbeGoto(v, endLabel);
3067 sqlite3ExprCachePop(pParse); 3194 sqlite3ExprCachePop(pParse);
3068 sqlite3VdbeResolveLabel(v, nextCase); 3195 sqlite3VdbeResolveLabel(v, nextCase);
3069 } 3196 }
3070 if( (nExpr&1)!=0 ){ 3197 if( (nExpr&1)!=0 ){
3071 sqlite3ExprCachePush(pParse); 3198 sqlite3ExprCachePush(pParse);
3072 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); 3199 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
3073 sqlite3ExprCachePop(pParse); 3200 sqlite3ExprCachePop(pParse);
3074 }else{ 3201 }else{
3075 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3202 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
3076 } 3203 }
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
3187 ** in register target. 3314 ** in register target.
3188 */ 3315 */
3189 void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 3316 void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
3190 int inReg; 3317 int inReg;
3191 3318
3192 assert( target>0 && target<=pParse->nMem ); 3319 assert( target>0 && target<=pParse->nMem );
3193 if( pExpr && pExpr->op==TK_REGISTER ){ 3320 if( pExpr && pExpr->op==TK_REGISTER ){
3194 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); 3321 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
3195 }else{ 3322 }else{
3196 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 3323 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
3197 assert( pParse->pVdbe || pParse->db->mallocFailed ); 3324 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
3198 if( inReg!=target && pParse->pVdbe ){ 3325 if( inReg!=target && pParse->pVdbe ){
3199 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 3326 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
3200 } 3327 }
3201 } 3328 }
3202 } 3329 }
3203 3330
3204 /* 3331 /*
3332 ** Make a transient copy of expression pExpr and then code it using
3333 ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
3334 ** except that the input expression is guaranteed to be unchanged.
3335 */
3336 void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
3337 sqlite3 *db = pParse->db;
3338 pExpr = sqlite3ExprDup(db, pExpr, 0);
3339 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
3340 sqlite3ExprDelete(db, pExpr);
3341 }
3342
3343 /*
3205 ** Generate code that will evaluate expression pExpr and store the 3344 ** Generate code that will evaluate expression pExpr and store the
3206 ** results in register target. The results are guaranteed to appear 3345 ** results in register target. The results are guaranteed to appear
3207 ** in register target. If the expression is constant, then this routine 3346 ** in register target. If the expression is constant, then this routine
3208 ** might choose to code the expression at initialization time. 3347 ** might choose to code the expression at initialization time.
3209 */ 3348 */
3210 void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ 3349 void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
3211 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ 3350 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
3212 sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); 3351 sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
3213 }else{ 3352 }else{
3214 sqlite3ExprCode(pParse, pExpr, target); 3353 sqlite3ExprCode(pParse, pExpr, target);
(...skipping 17 matching lines...) Expand all
3232 int iMem; 3371 int iMem;
3233 3372
3234 assert( target>0 ); 3373 assert( target>0 );
3235 assert( pExpr->op!=TK_REGISTER ); 3374 assert( pExpr->op!=TK_REGISTER );
3236 sqlite3ExprCode(pParse, pExpr, target); 3375 sqlite3ExprCode(pParse, pExpr, target);
3237 iMem = ++pParse->nMem; 3376 iMem = ++pParse->nMem;
3238 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); 3377 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
3239 exprToRegister(pExpr, iMem); 3378 exprToRegister(pExpr, iMem);
3240 } 3379 }
3241 3380
3242 #ifdef SQLITE_DEBUG
3243 /*
3244 ** Generate a human-readable explanation of an expression tree.
3245 */
3246 void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
3247 const char *zBinOp = 0; /* Binary operator */
3248 const char *zUniOp = 0; /* Unary operator */
3249 pView = sqlite3TreeViewPush(pView, moreToFollow);
3250 if( pExpr==0 ){
3251 sqlite3TreeViewLine(pView, "nil");
3252 sqlite3TreeViewPop(pView);
3253 return;
3254 }
3255 switch( pExpr->op ){
3256 case TK_AGG_COLUMN: {
3257 sqlite3TreeViewLine(pView, "AGG{%d:%d}",
3258 pExpr->iTable, pExpr->iColumn);
3259 break;
3260 }
3261 case TK_COLUMN: {
3262 if( pExpr->iTable<0 ){
3263 /* This only happens when coding check constraints */
3264 sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
3265 }else{
3266 sqlite3TreeViewLine(pView, "{%d:%d}",
3267 pExpr->iTable, pExpr->iColumn);
3268 }
3269 break;
3270 }
3271 case TK_INTEGER: {
3272 if( pExpr->flags & EP_IntValue ){
3273 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
3274 }else{
3275 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
3276 }
3277 break;
3278 }
3279 #ifndef SQLITE_OMIT_FLOATING_POINT
3280 case TK_FLOAT: {
3281 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
3282 break;
3283 }
3284 #endif
3285 case TK_STRING: {
3286 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
3287 break;
3288 }
3289 case TK_NULL: {
3290 sqlite3TreeViewLine(pView,"NULL");
3291 break;
3292 }
3293 #ifndef SQLITE_OMIT_BLOB_LITERAL
3294 case TK_BLOB: {
3295 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
3296 break;
3297 }
3298 #endif
3299 case TK_VARIABLE: {
3300 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
3301 pExpr->u.zToken, pExpr->iColumn);
3302 break;
3303 }
3304 case TK_REGISTER: {
3305 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
3306 break;
3307 }
3308 case TK_AS: {
3309 sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
3310 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
3311 break;
3312 }
3313 case TK_ID: {
3314 sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken);
3315 break;
3316 }
3317 #ifndef SQLITE_OMIT_CAST
3318 case TK_CAST: {
3319 /* Expressions of the form: CAST(pLeft AS token) */
3320 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
3321 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
3322 break;
3323 }
3324 #endif /* SQLITE_OMIT_CAST */
3325 case TK_LT: zBinOp = "LT"; break;
3326 case TK_LE: zBinOp = "LE"; break;
3327 case TK_GT: zBinOp = "GT"; break;
3328 case TK_GE: zBinOp = "GE"; break;
3329 case TK_NE: zBinOp = "NE"; break;
3330 case TK_EQ: zBinOp = "EQ"; break;
3331 case TK_IS: zBinOp = "IS"; break;
3332 case TK_ISNOT: zBinOp = "ISNOT"; break;
3333 case TK_AND: zBinOp = "AND"; break;
3334 case TK_OR: zBinOp = "OR"; break;
3335 case TK_PLUS: zBinOp = "ADD"; break;
3336 case TK_STAR: zBinOp = "MUL"; break;
3337 case TK_MINUS: zBinOp = "SUB"; break;
3338 case TK_REM: zBinOp = "REM"; break;
3339 case TK_BITAND: zBinOp = "BITAND"; break;
3340 case TK_BITOR: zBinOp = "BITOR"; break;
3341 case TK_SLASH: zBinOp = "DIV"; break;
3342 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
3343 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
3344 case TK_CONCAT: zBinOp = "CONCAT"; break;
3345 case TK_DOT: zBinOp = "DOT"; break;
3346
3347 case TK_UMINUS: zUniOp = "UMINUS"; break;
3348 case TK_UPLUS: zUniOp = "UPLUS"; break;
3349 case TK_BITNOT: zUniOp = "BITNOT"; break;
3350 case TK_NOT: zUniOp = "NOT"; break;
3351 case TK_ISNULL: zUniOp = "ISNULL"; break;
3352 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
3353
3354 case TK_COLLATE: {
3355 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
3356 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
3357 break;
3358 }
3359
3360 case TK_AGG_FUNCTION:
3361 case TK_FUNCTION: {
3362 ExprList *pFarg; /* List of function arguments */
3363 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
3364 pFarg = 0;
3365 }else{
3366 pFarg = pExpr->x.pList;
3367 }
3368 if( pExpr->op==TK_AGG_FUNCTION ){
3369 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
3370 pExpr->op2, pExpr->u.zToken);
3371 }else{
3372 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
3373 }
3374 if( pFarg ){
3375 sqlite3TreeViewExprList(pView, pFarg, 0, 0);
3376 }
3377 break;
3378 }
3379 #ifndef SQLITE_OMIT_SUBQUERY
3380 case TK_EXISTS: {
3381 sqlite3TreeViewLine(pView, "EXISTS-expr");
3382 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
3383 break;
3384 }
3385 case TK_SELECT: {
3386 sqlite3TreeViewLine(pView, "SELECT-expr");
3387 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
3388 break;
3389 }
3390 case TK_IN: {
3391 sqlite3TreeViewLine(pView, "IN");
3392 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
3393 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
3394 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
3395 }else{
3396 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
3397 }
3398 break;
3399 }
3400 #endif /* SQLITE_OMIT_SUBQUERY */
3401
3402 /*
3403 ** x BETWEEN y AND z
3404 **
3405 ** This is equivalent to
3406 **
3407 ** x>=y AND x<=z
3408 **
3409 ** X is stored in pExpr->pLeft.
3410 ** Y is stored in pExpr->pList->a[0].pExpr.
3411 ** Z is stored in pExpr->pList->a[1].pExpr.
3412 */
3413 case TK_BETWEEN: {
3414 Expr *pX = pExpr->pLeft;
3415 Expr *pY = pExpr->x.pList->a[0].pExpr;
3416 Expr *pZ = pExpr->x.pList->a[1].pExpr;
3417 sqlite3TreeViewLine(pView, "BETWEEN");
3418 sqlite3TreeViewExpr(pView, pX, 1);
3419 sqlite3TreeViewExpr(pView, pY, 1);
3420 sqlite3TreeViewExpr(pView, pZ, 0);
3421 break;
3422 }
3423 case TK_TRIGGER: {
3424 /* If the opcode is TK_TRIGGER, then the expression is a reference
3425 ** to a column in the new.* or old.* pseudo-tables available to
3426 ** trigger programs. In this case Expr.iTable is set to 1 for the
3427 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
3428 ** is set to the column of the pseudo-table to read, or to -1 to
3429 ** read the rowid field.
3430 */
3431 sqlite3TreeViewLine(pView, "%s(%d)",
3432 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
3433 break;
3434 }
3435 case TK_CASE: {
3436 sqlite3TreeViewLine(pView, "CASE");
3437 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
3438 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
3439 break;
3440 }
3441 #ifndef SQLITE_OMIT_TRIGGER
3442 case TK_RAISE: {
3443 const char *zType = "unk";
3444 switch( pExpr->affinity ){
3445 case OE_Rollback: zType = "rollback"; break;
3446 case OE_Abort: zType = "abort"; break;
3447 case OE_Fail: zType = "fail"; break;
3448 case OE_Ignore: zType = "ignore"; break;
3449 }
3450 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
3451 break;
3452 }
3453 #endif
3454 default: {
3455 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
3456 break;
3457 }
3458 }
3459 if( zBinOp ){
3460 sqlite3TreeViewLine(pView, "%s", zBinOp);
3461 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
3462 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
3463 }else if( zUniOp ){
3464 sqlite3TreeViewLine(pView, "%s", zUniOp);
3465 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
3466 }
3467 sqlite3TreeViewPop(pView);
3468 }
3469 #endif /* SQLITE_DEBUG */
3470
3471 #ifdef SQLITE_DEBUG
3472 /*
3473 ** Generate a human-readable explanation of an expression list.
3474 */
3475 void sqlite3TreeViewExprList(
3476 TreeView *pView,
3477 const ExprList *pList,
3478 u8 moreToFollow,
3479 const char *zLabel
3480 ){
3481 int i;
3482 pView = sqlite3TreeViewPush(pView, moreToFollow);
3483 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
3484 if( pList==0 ){
3485 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
3486 }else{
3487 sqlite3TreeViewLine(pView, "%s", zLabel);
3488 for(i=0; i<pList->nExpr; i++){
3489 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
3490 #if 0
3491 if( pList->a[i].zName ){
3492 sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
3493 }
3494 if( pList->a[i].bSpanIsTab ){
3495 sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
3496 }
3497 #endif
3498 }
3499 }
3500 sqlite3TreeViewPop(pView);
3501 }
3502 #endif /* SQLITE_DEBUG */
3503
3504 /* 3381 /*
3505 ** Generate code that pushes the value of every element of the given 3382 ** Generate code that pushes the value of every element of the given
3506 ** expression list into a sequence of registers beginning at target. 3383 ** expression list into a sequence of registers beginning at target.
3507 ** 3384 **
3508 ** Return the number of elements evaluated. 3385 ** Return the number of elements evaluated.
3509 ** 3386 **
3510 ** The SQLITE_ECEL_DUP flag prevents the arguments from being 3387 ** The SQLITE_ECEL_DUP flag prevents the arguments from being
3511 ** filled using OP_SCopy. OP_Copy must be used instead. 3388 ** filled using OP_SCopy. OP_Copy must be used instead.
3512 ** 3389 **
3513 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be 3390 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
3514 ** factored out into initialization code. 3391 ** factored out into initialization code.
3392 **
3393 ** The SQLITE_ECEL_REF flag means that expressions in the list with
3394 ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
3395 ** in registers at srcReg, and so the value can be copied from there.
3515 */ 3396 */
3516 int sqlite3ExprCodeExprList( 3397 int sqlite3ExprCodeExprList(
3517 Parse *pParse, /* Parsing context */ 3398 Parse *pParse, /* Parsing context */
3518 ExprList *pList, /* The expression list to be coded */ 3399 ExprList *pList, /* The expression list to be coded */
3519 int target, /* Where to write results */ 3400 int target, /* Where to write results */
3401 int srcReg, /* Source registers if SQLITE_ECEL_REF */
3520 u8 flags /* SQLITE_ECEL_* flags */ 3402 u8 flags /* SQLITE_ECEL_* flags */
3521 ){ 3403 ){
3522 struct ExprList_item *pItem; 3404 struct ExprList_item *pItem;
3523 int i, n; 3405 int i, j, n;
3524 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; 3406 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
3407 Vdbe *v = pParse->pVdbe;
3525 assert( pList!=0 ); 3408 assert( pList!=0 );
3526 assert( target>0 ); 3409 assert( target>0 );
3527 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ 3410 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
3528 n = pList->nExpr; 3411 n = pList->nExpr;
3529 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; 3412 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
3530 for(pItem=pList->a, i=0; i<n; i++, pItem++){ 3413 for(pItem=pList->a, i=0; i<n; i++, pItem++){
3531 Expr *pExpr = pItem->pExpr; 3414 Expr *pExpr = pItem->pExpr;
3532 if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ 3415 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){
3416 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
3417 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
3533 sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); 3418 sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
3534 }else{ 3419 }else{
3535 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); 3420 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
3536 if( inReg!=target+i ){ 3421 if( inReg!=target+i ){
3537 VdbeOp *pOp; 3422 VdbeOp *pOp;
3538 Vdbe *v = pParse->pVdbe;
3539 if( copyOp==OP_Copy 3423 if( copyOp==OP_Copy
3540 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy 3424 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
3541 && pOp->p1+pOp->p3+1==inReg 3425 && pOp->p1+pOp->p3+1==inReg
3542 && pOp->p2+pOp->p3+1==target+i 3426 && pOp->p2+pOp->p3+1==target+i
3543 ){ 3427 ){
3544 pOp->p3++; 3428 pOp->p3++;
3545 }else{ 3429 }else{
3546 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); 3430 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
3547 } 3431 }
3548 } 3432 }
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
3705 case TK_BETWEEN: { 3589 case TK_BETWEEN: {
3706 testcase( jumpIfNull==0 ); 3590 testcase( jumpIfNull==0 );
3707 exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull); 3591 exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
3708 break; 3592 break;
3709 } 3593 }
3710 #ifndef SQLITE_OMIT_SUBQUERY 3594 #ifndef SQLITE_OMIT_SUBQUERY
3711 case TK_IN: { 3595 case TK_IN: {
3712 int destIfFalse = sqlite3VdbeMakeLabel(v); 3596 int destIfFalse = sqlite3VdbeMakeLabel(v);
3713 int destIfNull = jumpIfNull ? dest : destIfFalse; 3597 int destIfNull = jumpIfNull ? dest : destIfFalse;
3714 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 3598 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
3715 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); 3599 sqlite3VdbeGoto(v, dest);
3716 sqlite3VdbeResolveLabel(v, destIfFalse); 3600 sqlite3VdbeResolveLabel(v, destIfFalse);
3717 break; 3601 break;
3718 } 3602 }
3719 #endif 3603 #endif
3720 default: { 3604 default: {
3721 if( exprAlwaysTrue(pExpr) ){ 3605 if( exprAlwaysTrue(pExpr) ){
3722 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); 3606 sqlite3VdbeGoto(v, dest);
3723 }else if( exprAlwaysFalse(pExpr) ){ 3607 }else if( exprAlwaysFalse(pExpr) ){
3724 /* No-op */ 3608 /* No-op */
3725 }else{ 3609 }else{
3726 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1); 3610 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3727 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 3611 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
3728 VdbeCoverage(v); 3612 VdbeCoverage(v);
3729 testcase( regFree1==0 ); 3613 testcase( regFree1==0 );
3730 testcase( jumpIfNull==0 ); 3614 testcase( jumpIfNull==0 );
3731 } 3615 }
3732 break; 3616 break;
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
3868 }else{ 3752 }else{
3869 int destIfNull = sqlite3VdbeMakeLabel(v); 3753 int destIfNull = sqlite3VdbeMakeLabel(v);
3870 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); 3754 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
3871 sqlite3VdbeResolveLabel(v, destIfNull); 3755 sqlite3VdbeResolveLabel(v, destIfNull);
3872 } 3756 }
3873 break; 3757 break;
3874 } 3758 }
3875 #endif 3759 #endif
3876 default: { 3760 default: {
3877 if( exprAlwaysFalse(pExpr) ){ 3761 if( exprAlwaysFalse(pExpr) ){
3878 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); 3762 sqlite3VdbeGoto(v, dest);
3879 }else if( exprAlwaysTrue(pExpr) ){ 3763 }else if( exprAlwaysTrue(pExpr) ){
3880 /* no-op */ 3764 /* no-op */
3881 }else{ 3765 }else{
3882 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1); 3766 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3883 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 3767 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
3884 VdbeCoverage(v); 3768 VdbeCoverage(v);
3885 testcase( regFree1==0 ); 3769 testcase( regFree1==0 );
3886 testcase( jumpIfNull==0 ); 3770 testcase( jumpIfNull==0 );
3887 } 3771 }
3888 break; 3772 break;
3889 } 3773 }
3890 } 3774 }
3891 sqlite3ReleaseTempReg(pParse, regFree1); 3775 sqlite3ReleaseTempReg(pParse, regFree1);
3892 sqlite3ReleaseTempReg(pParse, regFree2); 3776 sqlite3ReleaseTempReg(pParse, regFree2);
3893 } 3777 }
3894 3778
3895 /* 3779 /*
3780 ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
3781 ** code generation, and that copy is deleted after code generation. This
3782 ** ensures that the original pExpr is unchanged.
3783 */
3784 void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
3785 sqlite3 *db = pParse->db;
3786 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
3787 if( db->mallocFailed==0 ){
3788 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
3789 }
3790 sqlite3ExprDelete(db, pCopy);
3791 }
3792
3793
3794 /*
3896 ** Do a deep comparison of two expression trees. Return 0 if the two 3795 ** Do a deep comparison of two expression trees. Return 0 if the two
3897 ** expressions are completely identical. Return 1 if they differ only 3796 ** expressions are completely identical. Return 1 if they differ only
3898 ** by a COLLATE operator at the top level. Return 2 if there are differences 3797 ** by a COLLATE operator at the top level. Return 2 if there are differences
3899 ** other than the top-level COLLATE operator. 3798 ** other than the top-level COLLATE operator.
3900 ** 3799 **
3901 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 3800 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
3902 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 3801 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
3903 ** 3802 **
3904 ** The pA side might be using TK_REGISTER. If that is the case and pB is 3803 ** The pA side might be using TK_REGISTER. If that is the case and pB is
3905 ** not using TK_REGISTER but is otherwise equivalent, then still return 0. 3804 ** not using TK_REGISTER but is otherwise equivalent, then still return 0.
(...skipping 22 matching lines...) Expand all
3928 } 3827 }
3929 if( pA->op!=pB->op ){ 3828 if( pA->op!=pB->op ){
3930 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ 3829 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
3931 return 1; 3830 return 1;
3932 } 3831 }
3933 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ 3832 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
3934 return 1; 3833 return 1;
3935 } 3834 }
3936 return 2; 3835 return 2;
3937 } 3836 }
3938 if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){ 3837 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
3939 if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ 3838 if( pA->op==TK_FUNCTION ){
3839 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
3840 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
3940 return pA->op==TK_COLLATE ? 1 : 2; 3841 return pA->op==TK_COLLATE ? 1 : 2;
3941 } 3842 }
3942 } 3843 }
3943 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; 3844 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
3944 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ 3845 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
3945 if( combinedFlags & EP_xIsSelect ) return 2; 3846 if( combinedFlags & EP_xIsSelect ) return 2;
3946 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; 3847 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
3947 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; 3848 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
3948 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; 3849 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
3949 if( ALWAYS((combinedFlags & EP_Reduced)==0) ){ 3850 if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
3950 if( pA->iColumn!=pB->iColumn ) return 2; 3851 if( pA->iColumn!=pB->iColumn ) return 2;
3951 if( pA->iTable!=pB->iTable 3852 if( pA->iTable!=pB->iTable
3952 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; 3853 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
3953 } 3854 }
3954 } 3855 }
3955 return 0; 3856 return 0;
3956 } 3857 }
3957 3858
3958 /* 3859 /*
3959 ** Compare two ExprList objects. Return 0 if they are identical and 3860 ** Compare two ExprList objects. Return 0 if they are identical and
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
4041 static int exprSrcCount(Walker *pWalker, Expr *pExpr){ 3942 static int exprSrcCount(Walker *pWalker, Expr *pExpr){
4042 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() 3943 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
4043 ** is always called before sqlite3ExprAnalyzeAggregates() and so the 3944 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
4044 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If 3945 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
4045 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the 3946 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
4046 ** NEVER() will need to be removed. */ 3947 ** NEVER() will need to be removed. */
4047 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ 3948 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
4048 int i; 3949 int i;
4049 struct SrcCount *p = pWalker->u.pSrcCount; 3950 struct SrcCount *p = pWalker->u.pSrcCount;
4050 SrcList *pSrc = p->pSrc; 3951 SrcList *pSrc = p->pSrc;
4051 for(i=0; i<pSrc->nSrc; i++){ 3952 int nSrc = pSrc ? pSrc->nSrc : 0;
3953 for(i=0; i<nSrc; i++){
4052 if( pExpr->iTable==pSrc->a[i].iCursor ) break; 3954 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
4053 } 3955 }
4054 if( i<pSrc->nSrc ){ 3956 if( i<nSrc ){
4055 p->nThis++; 3957 p->nThis++;
4056 }else{ 3958 }else{
4057 p->nOther++; 3959 p->nOther++;
4058 } 3960 }
4059 } 3961 }
4060 return WRC_Continue; 3962 return WRC_Continue;
4061 } 3963 }
4062 3964
4063 /* 3965 /*
4064 ** Determine if any of the arguments to the pExpr Function reference 3966 ** Determine if any of the arguments to the pExpr Function reference
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
4340 } 4242 }
4341 } 4243 }
4342 4244
4343 /* 4245 /*
4344 ** Mark all temporary registers as being unavailable for reuse. 4246 ** Mark all temporary registers as being unavailable for reuse.
4345 */ 4247 */
4346 void sqlite3ClearTempRegCache(Parse *pParse){ 4248 void sqlite3ClearTempRegCache(Parse *pParse){
4347 pParse->nTempReg = 0; 4249 pParse->nTempReg = 0;
4348 pParse->nRangeReg = 0; 4250 pParse->nRangeReg = 0;
4349 } 4251 }
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3100200/src/delete.c ('k') | third_party/sqlite/sqlite-src-3100200/src/fault.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698