/ Check-in [f3f9e8e5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add evidence marks to the where.c source file. Comment only - no code changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f3f9e8e5bb90cc36e0d63a7289a8acf534c5fa2a
User & Date: drh 2010-07-22 22:40:04
Context
2010-07-23
08:31
Remove the unused Schema.db variable. check-in: 430be657 user: dan tags: trunk
2010-07-22
22:40
Add evidence marks to the where.c source file. Comment only - no code changes. check-in: f3f9e8e5 user: drh tags: trunk
17:55
Make the sqlite3_db_status() routine threadsafe. check-in: 241f7bd1 user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/where.c.

   323    323   ** WhereTerms.  All pointers to WhereTerms should be invalidated after
   324    324   ** calling this routine.  Such pointers may be reinitialized by referencing
   325    325   ** the pWC->a[] array.
   326    326   */
   327    327   static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
   328    328     WhereTerm *pTerm;
   329    329     int idx;
          330  +  testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
   330    331     if( pWC->nTerm>=pWC->nSlot ){
   331    332       WhereTerm *pOld = pWC->a;
   332    333       sqlite3 *db = pWC->pParse->db;
   333    334       pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
   334    335       if( pWC->a==0 ){
   335    336         if( wtFlags & TERM_DYNAMIC ){
   336    337           sqlite3ExprDelete(db, p);
................................................................................
   468    469     return mask;
   469    470   }
   470    471   
   471    472   /*
   472    473   ** Return TRUE if the given operator is one of the operators that is
   473    474   ** allowed for an indexable WHERE clause term.  The allowed operators are
   474    475   ** "=", "<", ">", "<=", ">=", and "IN".
          476  +**
          477  +** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
          478  +** of one of the following forms: column = expression column > expression
          479  +** column >= expression column < expression column <= expression
          480  +** expression = column expression > column expression >= column
          481  +** expression < column expression <= column column IN
          482  +** (expression-list) column IN (subquery) column IS NULL
   475    483   */
   476    484   static int allowedOp(int op){
   477    485     assert( TK_GT>TK_EQ && TK_GT<TK_GE );
   478    486     assert( TK_LT>TK_EQ && TK_LT<TK_GE );
   479    487     assert( TK_LE>TK_EQ && TK_LE<TK_GE );
   480    488     assert( TK_GE==TK_EQ+4 );
   481    489     return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
................................................................................
  1012   1020           }
  1013   1021         }
  1014   1022       }
  1015   1023   
  1016   1024       /* At this point, okToChngToIN is true if original pTerm satisfies
  1017   1025       ** case 1.  In that case, construct a new virtual term that is 
  1018   1026       ** pTerm converted into an IN operator.
         1027  +    **
         1028  +    ** EV: R-00211-15100
  1019   1029       */
  1020   1030       if( okToChngToIN ){
  1021   1031         Expr *pDup;            /* A transient duplicate expression */
  1022   1032         ExprList *pList = 0;   /* The RHS of the IN operator */
  1023   1033         Expr *pLeft = 0;       /* The LHS of the IN operator */
  1024   1034         Expr *pNew;            /* The complete IN operator */
  1025   1035   
................................................................................
  1243   1253         if( noCase ){
  1244   1254           /* The point is to increment the last character before the first
  1245   1255           ** wildcard.  But if we increment '@', that will push it into the
  1246   1256           ** alphabetic range where case conversions will mess up the 
  1247   1257           ** inequality.  To avoid this, make sure to also run the full
  1248   1258           ** LIKE on all candidate expressions by clearing the isComplete flag
  1249   1259           */
  1250         -        if( c=='A'-1 ) isComplete = 0;
         1260  +        if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
         1261  +
  1251   1262   
  1252   1263           c = sqlite3UpperToLower[c];
  1253   1264         }
  1254   1265         *pC = c + 1;
  1255   1266       }
  1256   1267       pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
  1257   1268       pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
................................................................................
  2874   2885   **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
  2875   2886   **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
  2876   2887   **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
  2877   2888   **
  2878   2889   ** The t2.z='ok' is disabled in the in (2) because it originates
  2879   2890   ** in the ON clause.  The term is disabled in (3) because it is not part
  2880   2891   ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
         2892  +**
         2893  +** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
         2894  +** completely satisfied by indices.
  2881   2895   **
  2882   2896   ** Disabling a term causes that term to not be tested in the inner loop
  2883   2897   ** of the join.  Disabling is an optimization.  When terms are satisfied
  2884   2898   ** by indices, we disable them to prevent redundant tests in the inner
  2885   2899   ** loop.  We would get the correct results if nothing were ever disabled,
  2886   2900   ** but joins might run a little slower.  The trick is to disable as much
  2887   2901   ** as we can without disabling too much.  If we disabled in (1), we'd get
................................................................................
  3086   3100       int r1;
  3087   3101       int k = pIdx->aiColumn[j];
  3088   3102       pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
  3089   3103       if( NEVER(pTerm==0) ) break;
  3090   3104       /* The following true for indices with redundant columns. 
  3091   3105       ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
  3092   3106       testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
         3107  +    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3093   3108       r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
  3094   3109       if( r1!=regBase+j ){
  3095   3110         if( nReg==1 ){
  3096   3111           sqlite3ReleaseTempReg(pParse, regBase);
  3097   3112           regBase = r1;
  3098   3113         }else{
  3099   3114           sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
................................................................................
  3229   3244       */
  3230   3245       iReleaseReg = sqlite3GetTempReg(pParse);
  3231   3246       pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
  3232   3247       assert( pTerm!=0 );
  3233   3248       assert( pTerm->pExpr!=0 );
  3234   3249       assert( pTerm->leftCursor==iCur );
  3235   3250       assert( omitTable==0 );
         3251  +    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3236   3252       iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
  3237   3253       addrNxt = pLevel->addrNxt;
  3238   3254       sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
  3239   3255       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
  3240   3256       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  3241   3257       VdbeComment((v, "pk"));
  3242   3258       pLevel->op = OP_Noop;
................................................................................
  3269   3285              /* TK_LT */  OP_SeekLt,
  3270   3286              /* TK_GE */  OP_SeekGe
  3271   3287         };
  3272   3288         assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
  3273   3289         assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
  3274   3290         assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
  3275   3291   
         3292  +      testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3276   3293         pX = pStart->pExpr;
  3277   3294         assert( pX!=0 );
  3278   3295         assert( pStart->leftCursor==iCur );
  3279   3296         r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
  3280   3297         sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
  3281   3298         VdbeComment((v, "pk"));
  3282   3299         sqlite3ExprCacheAffinityChange(pParse, r1, 1);
................................................................................
  3286   3303         sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
  3287   3304       }
  3288   3305       if( pEnd ){
  3289   3306         Expr *pX;
  3290   3307         pX = pEnd->pExpr;
  3291   3308         assert( pX!=0 );
  3292   3309         assert( pEnd->leftCursor==iCur );
         3310  +      testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3293   3311         memEndValue = ++pParse->nMem;
  3294   3312         sqlite3ExprCode(pParse, pX->pRight, memEndValue);
  3295   3313         if( pX->op==TK_LT || pX->op==TK_GT ){
  3296   3314           testOp = bRev ? OP_Le : OP_Ge;
  3297   3315         }else{
  3298   3316           testOp = bRev ? OP_Lt : OP_Gt;
  3299   3317         }
................................................................................
  3453   3471             zStartAff[nEq] = SQLITE_AFF_NONE;
  3454   3472           }
  3455   3473           if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
  3456   3474             zStartAff[nEq] = SQLITE_AFF_NONE;
  3457   3475           }
  3458   3476         }  
  3459   3477         nConstraint++;
         3478  +      testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3460   3479       }else if( isMinQuery ){
  3461   3480         sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
  3462   3481         nConstraint++;
  3463   3482         startEq = 0;
  3464   3483         start_constraints = 1;
  3465   3484       }
  3466   3485       codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
................................................................................
  3492   3511           }
  3493   3512           if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
  3494   3513             zEndAff[nEq] = SQLITE_AFF_NONE;
  3495   3514           }
  3496   3515         }  
  3497   3516         codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
  3498   3517         nConstraint++;
         3518  +      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3499   3519       }
  3500   3520       sqlite3DbFree(pParse->db, zStartAff);
  3501   3521       sqlite3DbFree(pParse->db, zEndAff);
  3502   3522   
  3503   3523       /* Top of the loop body */
  3504   3524       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  3505   3525   
................................................................................
  3698   3718       pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
  3699   3719       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  3700   3720     }
  3701   3721     notReady &= ~getMask(pWC->pMaskSet, iCur);
  3702   3722   
  3703   3723     /* Insert code to test every subexpression that can be completely
  3704   3724     ** computed using the current set of tables.
         3725  +  **
         3726  +  ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
         3727  +  ** the use of indices become tests that are evaluated against each row of
         3728  +  ** the relevant input tables.
  3705   3729     */
  3706   3730     k = 0;
  3707   3731     for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
  3708   3732       Expr *pE;
  3709         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
         3733  +    testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
  3710   3734       testcase( pTerm->wtFlags & TERM_CODED );
  3711   3735       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  3712   3736       if( (pTerm->prereqAll & notReady)!=0 ){
  3713   3737         testcase( pWInfo->untestedTerms==0
  3714   3738                  && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
  3715   3739         pWInfo->untestedTerms = 1;
  3716   3740         continue;
................................................................................
  3730   3754     */
  3731   3755     if( pLevel->iLeftJoin ){
  3732   3756       pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
  3733   3757       sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
  3734   3758       VdbeComment((v, "record LEFT JOIN hit"));
  3735   3759       sqlite3ExprCacheClear(pParse);
  3736   3760       for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
  3737         -      testcase( pTerm->wtFlags & TERM_VIRTUAL );
         3761  +      testcase( pTerm->wtFlags & TERM_VIRTUAL );  /* IMP: R-30575-11662 */
  3738   3762         testcase( pTerm->wtFlags & TERM_CODED );
  3739   3763         if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  3740   3764         if( (pTerm->prereqAll & notReady)!=0 ){
  3741   3765           assert( pWInfo->untestedTerms );
  3742   3766           continue;
  3743   3767         }
  3744   3768         assert( pTerm->pExpr );
................................................................................
  3948   3972   
  3949   3973     /* Split the WHERE clause into separate subexpressions where each
  3950   3974     ** subexpression is separated by an AND operator.
  3951   3975     */
  3952   3976     initMaskSet(pMaskSet);
  3953   3977     whereClauseInit(pWC, pParse, pMaskSet);
  3954   3978     sqlite3ExprCodeConstants(pParse, pWhere);
  3955         -  whereSplit(pWC, pWhere, TK_AND);
         3979  +  whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
  3956   3980       
  3957   3981     /* Special case: a WHERE clause that is constant.  Evaluate the
  3958   3982     ** expression and either jump over all of the code or fall thru.
  3959   3983     */
  3960   3984     if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
  3961   3985       sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
  3962   3986       pWhere = 0;