/ Check-in [58023bfc]
Login

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

Overview
Comment:Merge coverage improvements and comment fixes from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 58023bfccce40c611791c1963d47b620ffb9447a
User & Date: drh 2016-03-08 14:01:52
Context
2016-03-08
16:35
Merge changes from trunk, especially the SQLITE_DEFAULT_SYNCHRONOUS enhancements. check-in: 29741941 user: drh tags: apple-osx
14:01
Merge coverage improvements and comment fixes from trunk. check-in: 58023bfc user: drh tags: apple-osx
13:56
Fix comments on pager flag settings to include synchronous=EXTRA. check-in: 3a65a1fc user: drh tags: trunk
2016-03-07
21:28
Fix a typo and remove an unused variable from test logic. check-in: 9f31516c user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/pager.c.

  3464   3464   
  3465   3465   /*
  3466   3466   ** Adjust settings of the pager to those specified in the pgFlags parameter.
  3467   3467   **
  3468   3468   ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
  3469   3469   ** of the database to damage due to OS crashes or power failures by
  3470   3470   ** changing the number of syncs()s when writing the journals.
  3471         -** There are three levels:
         3471  +** There are four levels:
  3472   3472   **
  3473   3473   **    OFF       sqlite3OsSync() is never called.  This is the default
  3474   3474   **              for temporary and transient files.
  3475   3475   **
  3476   3476   **    NORMAL    The journal is synced once before writes begin on the
  3477   3477   **              database.  This is normally adequate protection, but
  3478   3478   **              it is theoretically possible, though very unlikely,
................................................................................
  3483   3483   **    FULL      The journal is synced twice before writes begin on the
  3484   3484   **              database (with some additional information - the nRec field
  3485   3485   **              of the journal header - being written in between the two
  3486   3486   **              syncs).  If we assume that writing a
  3487   3487   **              single disk sector is atomic, then this mode provides
  3488   3488   **              assurance that the journal will not be corrupted to the
  3489   3489   **              point of causing damage to the database during rollback.
         3490  +**
         3491  +**    EXTRA     This is like FULL except that is also syncs the directory
         3492  +**              that contains the rollback journal after the rollback
         3493  +**              journal is unlinked.
  3490   3494   **
  3491   3495   ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
  3492   3496   ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
  3493   3497   ** prior to the start of checkpoint and that the database file is synced
  3494   3498   ** at the conclusion of the checkpoint if the entire content of the WAL
  3495   3499   ** was written back into the database.  But no sync operations occur for
  3496   3500   ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
  3497   3501   ** file is synced following each commit operation, in addition to the
  3498         -** syncs associated with NORMAL.
         3502  +** syncs associated with NORMAL.  There is no difference between FULL
         3503  +** and EXTRA for WAL mode.
  3499   3504   **
  3500   3505   ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
  3501   3506   ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
  3502   3507   ** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
  3503   3508   ** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
  3504   3509   ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
  3505   3510   ** synchronous=FULL versus synchronous=NORMAL setting determines when

Changes to src/sqliteInt.h.

  2359   2359   #define BMS  ((int)(sizeof(Bitmask)*8))
  2360   2360   
  2361   2361   /*
  2362   2362   ** A bit in a Bitmask
  2363   2363   */
  2364   2364   #define MASKBIT(n)   (((Bitmask)1)<<(n))
  2365   2365   #define MASKBIT32(n) (((unsigned int)1)<<(n))
         2366  +#define ALLBITS      ((Bitmask)-1)
  2366   2367   
  2367   2368   /*
  2368   2369   ** The following structure describes the FROM clause of a SELECT statement.
  2369   2370   ** Each table or subquery in the FROM clause is a separate element of
  2370   2371   ** the SrcList.a[] array.
  2371   2372   **
  2372   2373   ** With the addition of multiple database support, the following structure

Changes to src/update.c.

   264    264     chngKey = chngRowid + chngPk;
   265    265   
   266    266     /* The SET expressions are not actually used inside the WHERE loop.  
   267    267     ** So reset the colUsed mask. Unless this is a virtual table. In that
   268    268     ** case, set all bits of the colUsed mask (to ensure that the virtual
   269    269     ** table implementation makes all columns available).
   270    270     */
   271         -  pTabList->a[0].colUsed = IsVirtual(pTab) ? (Bitmask)-1 : 0;
          271  +  pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
   272    272   
   273    273     hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
   274    274   
   275    275     /* There is one entry in the aRegIdx[] array for each index on the table
   276    276     ** being updated.  Fill in aRegIdx[] with a register number that will hold
   277    277     ** the key for accessing each index.
   278    278     **

Changes to src/where.c.

   930    930   **
   931    931   ** Whether or not an error is returned, it is the responsibility of the
   932    932   ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
   933    933   ** that this is required.
   934    934   */
   935    935   static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
   936    936     sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
   937         -  int i;
   938    937     int rc;
   939    938   
   940    939     TRACE_IDX_INPUTS(p);
   941    940     rc = pVtab->pModule->xBestIndex(pVtab, p);
   942    941     TRACE_IDX_OUTPUTS(p);
   943    942   
   944    943     if( rc!=SQLITE_OK ){
................................................................................
   949    948       }else{
   950    949         sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
   951    950       }
   952    951     }
   953    952     sqlite3_free(pVtab->zErrMsg);
   954    953     pVtab->zErrMsg = 0;
   955    954   
          955  +#if 0
          956  +  /* This error is now caught by the caller.
          957  +  ** Search for "xBestIndex malfunction" below */
   956    958     for(i=0; i<p->nConstraint; i++){
   957    959       if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
   958    960         sqlite3ErrorMsg(pParse, 
   959    961             "table %s: xBestIndex returned an invalid plan", pTab->zName);
   960    962       }
   961    963     }
          964  +#endif
   962    965   
   963    966     return pParse->nErr;
   964    967   }
   965    968   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
   966    969   
   967    970   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   968    971   /*
................................................................................
  2550   2553   ** log(nRow) factor is omitted from a non-covering index scan in order to
  2551   2554   ** bias the scoring in favor of using an index, since the worst-case
  2552   2555   ** performance of using an index is far better than the worst-case performance
  2553   2556   ** of a full table scan.
  2554   2557   */
  2555   2558   static int whereLoopAddBtree(
  2556   2559     WhereLoopBuilder *pBuilder, /* WHERE clause information */
  2557         -  Bitmask mExtra              /* Extra prerequesites for using this table */
         2560  +  Bitmask mPrereq             /* Extra prerequesites for using this table */
  2558   2561   ){
  2559   2562     WhereInfo *pWInfo;          /* WHERE analysis context */
  2560   2563     Index *pProbe;              /* An index we are evaluating */
  2561   2564     Index sPk;                  /* A fake index object for the primary key */
  2562   2565     LogEst aiRowEstPk[2];       /* The aiRowLogEst[] value for the sPk index */
  2563   2566     i16 aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
  2564   2567     SrcList *pTabList;          /* The FROM clause */
................................................................................
  2650   2653           /* TUNING: Each index lookup yields 20 rows in the table.  This
  2651   2654           ** is more than the usual guess of 10 rows, since we have no way
  2652   2655           ** of knowing how selective the index will ultimately be.  It would
  2653   2656           ** not be unreasonable to make this value much larger. */
  2654   2657           pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
  2655   2658           pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
  2656   2659           pNew->wsFlags = WHERE_AUTO_INDEX;
  2657         -        pNew->prereq = mExtra | pTerm->prereqRight;
         2660  +        pNew->prereq = mPrereq | pTerm->prereqRight;
  2658   2661           rc = whereLoopInsert(pBuilder, pNew);
  2659   2662         }
  2660   2663       }
  2661   2664     }
  2662   2665   #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
  2663   2666   
  2664   2667     /* Loop over all indices
................................................................................
  2671   2674       }
  2672   2675       rSize = pProbe->aiRowLogEst[0];
  2673   2676       pNew->u.btree.nEq = 0;
  2674   2677       pNew->nSkip = 0;
  2675   2678       pNew->nLTerm = 0;
  2676   2679       pNew->iSortIdx = 0;
  2677   2680       pNew->rSetup = 0;
  2678         -    pNew->prereq = mExtra;
         2681  +    pNew->prereq = mPrereq;
  2679   2682       pNew->nOut = rSize;
  2680   2683       pNew->u.btree.pIndex = pProbe;
  2681   2684       b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
  2682   2685       /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
  2683   2686       assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
  2684   2687       if( pProbe->tnum<=0 ){
  2685   2688         /* Integer primary key index */
................................................................................
  2758   2761   ** A constraint is marked usable if:
  2759   2762   **
  2760   2763   **   * Argument mUsable indicates that its prerequisites are available, and
  2761   2764   **
  2762   2765   **   * It is not one of the operators specified in the mExclude mask passed
  2763   2766   **     as the fourth argument (which in practice is either WO_IN or 0).
  2764   2767   **
  2765         -** Argument mExtra is a mask of tables that must be scanned before the
         2768  +** Argument mPrereq is a mask of tables that must be scanned before the
  2766   2769   ** virtual table in question. These are added to the plans prerequisites
  2767   2770   ** before it is added to pBuilder.
  2768   2771   **
  2769   2772   ** Output parameter *pbIn is set to true if the plan added to pBuilder
  2770   2773   ** uses one or more WO_IN terms, or false otherwise.
  2771   2774   */
  2772   2775   static int whereLoopAddVirtualOne(
  2773   2776     WhereLoopBuilder *pBuilder,
  2774         -  Bitmask mExtra,                 /* Mask of tables that must be used. */
  2775         -  Bitmask mUsable,                /* Mask of usable prereqs */
  2776         -  u16 mExclude,                   /* Exclude terms for this operator */
         2777  +  Bitmask mPrereq,                /* Mask of tables that must be used. */
         2778  +  Bitmask mUsable,                /* Mask of usable tables */
         2779  +  u16 mExclude,                   /* Exclude terms using these operators */
  2777   2780     sqlite3_index_info *pIdxInfo,   /* Populated object for xBestIndex */
  2778   2781     int *pbIn                       /* OUT: True if plan uses an IN(...) op */
  2779   2782   ){
  2780   2783     WhereClause *pWC = pBuilder->pWC;
  2781   2784     struct sqlite3_index_constraint *pIdxCons;
  2782   2785     struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
  2783   2786     int i;
................................................................................
  2784   2787     int mxTerm;
  2785   2788     int rc = SQLITE_OK;
  2786   2789     WhereLoop *pNew = pBuilder->pNew;
  2787   2790     Parse *pParse = pBuilder->pWInfo->pParse;
  2788   2791     struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
  2789   2792     int nConstraint = pIdxInfo->nConstraint;
  2790   2793   
  2791         -  assert( (mUsable & mExtra)==mExtra );
         2794  +  assert( (mUsable & mPrereq)==mPrereq );
  2792   2795     *pbIn = 0;
  2793         -  pNew->prereq = mExtra;
         2796  +  pNew->prereq = mPrereq;
  2794   2797   
  2795   2798     /* Set the usable flag on the subset of constraints identified by 
  2796   2799     ** arguments mUsable and mExclude. */
  2797   2800     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
  2798   2801     for(i=0; i<nConstraint; i++, pIdxCons++){
  2799   2802       WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
  2800   2803       pIdxCons->usable = 0;
................................................................................
  2831   2834       if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
  2832   2835         WhereTerm *pTerm;
  2833   2836         int j = pIdxCons->iTermOffset;
  2834   2837         if( iTerm>=nConstraint
  2835   2838          || j<0
  2836   2839          || j>=pWC->nTerm
  2837   2840          || pNew->aLTerm[iTerm]!=0
         2841  +       || pIdxCons->usable==0
  2838   2842         ){
  2839   2843           rc = SQLITE_ERROR;
  2840         -        sqlite3ErrorMsg(pParse,"%s.xBestIndex() malfunction",pSrc->pTab->zName);
         2844  +        sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
  2841   2845           return rc;
  2842   2846         }
  2843   2847         testcase( iTerm==nConstraint-1 );
  2844   2848         testcase( j==0 );
  2845   2849         testcase( j==pWC->nTerm-1 );
  2846   2850         pTerm = &pWC->a[j];
  2847   2851         pNew->prereq |= pTerm->prereqRight;
................................................................................
  2855   2859           /* A virtual table that is constrained by an IN clause may not
  2856   2860           ** consume the ORDER BY clause because (1) the order of IN terms
  2857   2861           ** is not necessarily related to the order of output terms and
  2858   2862           ** (2) Multiple outputs from a single IN value will not merge
  2859   2863           ** together.  */
  2860   2864           pIdxInfo->orderByConsumed = 0;
  2861   2865           pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
  2862         -        *pbIn = 1;
         2866  +        *pbIn = 1; assert( (mExclude & WO_IN)==0 );
  2863   2867         }
  2864   2868       }
  2865   2869     }
  2866   2870   
  2867   2871     pNew->nLTerm = mxTerm+1;
  2868   2872     assert( pNew->nLTerm<=pNew->nLSlot );
  2869   2873     pNew->u.vtab.idxNum = pIdxInfo->idxNum;
................................................................................
  2893   2897   }
  2894   2898   
  2895   2899   
  2896   2900   /*
  2897   2901   ** Add all WhereLoop objects for a table of the join identified by
  2898   2902   ** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
  2899   2903   **
  2900         -** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and
  2901         -** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause
         2904  +** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
         2905  +** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
  2902   2906   ** entries that occur before the virtual table in the FROM clause and are
  2903   2907   ** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
  2904   2908   ** mUnusable mask contains all FROM clause entries that occur after the
  2905   2909   ** virtual table and are separated from it by at least one LEFT or 
  2906   2910   ** CROSS JOIN. 
  2907   2911   **
  2908   2912   ** For example, if the query were:
  2909   2913   **
  2910   2914   **   ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
  2911   2915   **
  2912         -** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6).
         2916  +** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
  2913   2917   **
  2914         -** All the tables in mExtra must be scanned before the current virtual 
         2918  +** All the tables in mPrereq must be scanned before the current virtual 
  2915   2919   ** table. So any terms for which all prerequisites are satisfied by 
  2916         -** mExtra may be specified as "usable" in all calls to xBestIndex. 
         2920  +** mPrereq may be specified as "usable" in all calls to xBestIndex. 
  2917   2921   ** Conversely, all tables in mUnusable must be scanned after the current
  2918   2922   ** virtual table, so any terms for which the prerequisites overlap with
  2919   2923   ** mUnusable should always be configured as "not-usable" for xBestIndex.
  2920   2924   */
  2921   2925   static int whereLoopAddVirtual(
  2922   2926     WhereLoopBuilder *pBuilder,  /* WHERE clause information */
  2923         -  Bitmask mExtra,              /* Tables that must be scanned before this one */
         2927  +  Bitmask mPrereq,             /* Tables that must be scanned before this one */
  2924   2928     Bitmask mUnusable            /* Tables that must be scanned after this one */
  2925   2929   ){
  2926   2930     int rc = SQLITE_OK;          /* Return code */
  2927   2931     WhereInfo *pWInfo;           /* WHERE analysis context */
  2928   2932     Parse *pParse;               /* The parsing context */
  2929   2933     WhereClause *pWC;            /* The WHERE clause */
  2930   2934     struct SrcList_item *pSrc;   /* The FROM clause term to search */
  2931   2935     sqlite3_index_info *p;       /* Object to pass to xBestIndex() */
  2932   2936     int nConstraint;             /* Number of constraints in p */
  2933   2937     int bIn;                     /* True if plan uses IN(...) operator */
  2934   2938     WhereLoop *pNew;
  2935   2939     Bitmask mBest;               /* Tables used by best possible plan */
  2936   2940   
  2937         -  assert( (mExtra & mUnusable)==0 );
         2941  +  assert( (mPrereq & mUnusable)==0 );
  2938   2942     pWInfo = pBuilder->pWInfo;
  2939   2943     pParse = pWInfo->pParse;
  2940   2944     pWC = pBuilder->pWC;
  2941   2945     pNew = pBuilder->pNew;
  2942   2946     pSrc = &pWInfo->pTabList->a[pNew->iTab];
  2943   2947     assert( IsVirtual(pSrc->pTab) );
  2944   2948     p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy);
................................................................................
  2950   2954     nConstraint = p->nConstraint;
  2951   2955     if( whereLoopResize(pParse->db, pNew, nConstraint) ){
  2952   2956       sqlite3DbFree(pParse->db, p);
  2953   2957       return SQLITE_NOMEM_BKPT;
  2954   2958     }
  2955   2959   
  2956   2960     /* First call xBestIndex() with all constraints usable. */
  2957         -  rc = whereLoopAddVirtualOne(pBuilder, mExtra, (Bitmask)(-1), 0, p, &bIn);
  2958         -  mBest = pNew->prereq & ~mExtra;
         2961  +  rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, &bIn);
         2962  +  mBest = pNew->prereq & ~mPrereq;
  2959   2963   
  2960   2964     /* If the call to xBestIndex() with all terms enabled produced a plan
  2961   2965     ** that does not require any source tables, there is no point in making
  2962   2966     ** any further calls - if the xBestIndex() method is sane they will all
  2963   2967     ** return the same plan anyway.
  2964   2968     */
  2965   2969     if( mBest ){
................................................................................
  2967   2971       int seenZeroNoIN = 0;         /* Plan with no prereqs and no IN(...) seen */
  2968   2972       Bitmask mPrev = 0;
  2969   2973       Bitmask mBestNoIn = 0;
  2970   2974   
  2971   2975       /* If the plan produced by the earlier call uses an IN(...) term, call
  2972   2976       ** xBestIndex again, this time with IN(...) terms disabled. */
  2973   2977       if( rc==SQLITE_OK && bIn ){
  2974         -      rc = whereLoopAddVirtualOne(pBuilder, mExtra, (Bitmask)-1, WO_IN, p,&bIn);
  2975         -      mBestNoIn = pNew->prereq & ~mExtra;
         2978  +      rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, WO_IN, p, &bIn);
         2979  +      assert( bIn==0 );
         2980  +      mBestNoIn = pNew->prereq & ~mPrereq;
  2976   2981         if( mBestNoIn==0 ){
  2977   2982           seenZero = 1;
  2978         -        if( bIn==0 ) seenZeroNoIN = 1;
         2983  +        seenZeroNoIN = 1;
  2979   2984         }
  2980   2985       }
  2981   2986   
  2982         -    /* Call xBestIndex once for each distinct value of (prereqRight & ~mExtra) 
         2987  +    /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) 
  2983   2988       ** in the set of terms that apply to the current virtual table.  */
  2984   2989       while( rc==SQLITE_OK ){
  2985   2990         int i;
  2986         -      Bitmask mNext = (Bitmask)(-1);
         2991  +      Bitmask mNext = ALLBITS;
  2987   2992         assert( mNext>0 );
  2988   2993         for(i=0; i<nConstraint; i++){
  2989   2994           Bitmask mThis = (
  2990         -            pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mExtra
         2995  +            pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
  2991   2996           );
  2992   2997           if( mThis>mPrev && mThis<mNext ) mNext = mThis;
  2993   2998         }
  2994   2999         mPrev = mNext;
  2995         -      if( mNext==(Bitmask)(-1) ) break;
         3000  +      if( mNext==ALLBITS ) break;
  2996   3001         if( mNext==mBest || mNext==mBestNoIn ) continue;
  2997         -      rc = whereLoopAddVirtualOne(pBuilder, mExtra, mNext|mExtra, 0, p, &bIn);
  2998         -      if( pNew->prereq==mExtra ){
         3002  +      rc = whereLoopAddVirtualOne(pBuilder, mPrereq, mNext|mPrereq, 0, p, &bIn);
         3003  +      if( pNew->prereq==mPrereq ){
  2999   3004           seenZero = 1;
  3000   3005           if( bIn==0 ) seenZeroNoIN = 1;
  3001   3006         }
  3002   3007       }
  3003   3008   
  3004   3009       /* If the calls to xBestIndex() in the above loop did not find a plan
  3005   3010       ** that requires no source tables at all (i.e. one guaranteed to be
  3006   3011       ** usable), make a call here with all source tables disabled */
  3007   3012       if( rc==SQLITE_OK && seenZero==0 ){
  3008         -      rc = whereLoopAddVirtualOne(pBuilder, mExtra, mExtra, 0, p, &bIn);
         3013  +      rc = whereLoopAddVirtualOne(pBuilder, mPrereq, mPrereq, 0, p, &bIn);
  3009   3014         if( bIn==0 ) seenZeroNoIN = 1;
  3010   3015       }
  3011   3016   
  3012   3017       /* If the calls to xBestIndex() have so far failed to find a plan
  3013   3018       ** that requires no source tables at all and does not use an IN(...)
  3014   3019       ** operator, make a final call to obtain one here.  */
  3015   3020       if( rc==SQLITE_OK && seenZeroNoIN==0 ){
  3016         -      rc = whereLoopAddVirtualOne(pBuilder, mExtra, mExtra, WO_IN, p, &bIn);
         3021  +      rc = whereLoopAddVirtualOne(pBuilder, mPrereq, mPrereq, WO_IN, p, &bIn);
  3017   3022       }
  3018   3023     }
  3019   3024   
  3020   3025     if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
  3021   3026     sqlite3DbFree(pParse->db, p);
  3022   3027     return rc;
  3023   3028   }
................................................................................
  3025   3030   
  3026   3031   /*
  3027   3032   ** Add WhereLoop entries to handle OR terms.  This works for either
  3028   3033   ** btrees or virtual tables.
  3029   3034   */
  3030   3035   static int whereLoopAddOr(
  3031   3036     WhereLoopBuilder *pBuilder, 
  3032         -  Bitmask mExtra, 
         3037  +  Bitmask mPrereq, 
  3033   3038     Bitmask mUnusable
  3034   3039   ){
  3035   3040     WhereInfo *pWInfo = pBuilder->pWInfo;
  3036   3041     WhereClause *pWC;
  3037   3042     WhereLoop *pNew;
  3038   3043     WhereTerm *pTerm, *pWCEnd;
  3039   3044     int rc = SQLITE_OK;
................................................................................
  3086   3091             for(i=0; i<sSubBuild.pWC->nTerm; i++){
  3087   3092               whereTermPrint(&sSubBuild.pWC->a[i], i);
  3088   3093             }
  3089   3094           }
  3090   3095   #endif
  3091   3096   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3092   3097           if( IsVirtual(pItem->pTab) ){
  3093         -          rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable);
         3098  +          rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
  3094   3099           }else
  3095   3100   #endif
  3096   3101           {
  3097         -          rc = whereLoopAddBtree(&sSubBuild, mExtra);
         3102  +          rc = whereLoopAddBtree(&sSubBuild, mPrereq);
  3098   3103           }
  3099   3104           if( rc==SQLITE_OK ){
  3100         -          rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable);
         3105  +          rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
  3101   3106           }
  3102   3107           assert( rc==SQLITE_OK || sCur.n==0 );
  3103   3108           if( sCur.n==0 ){
  3104   3109             sSum.n = 0;
  3105   3110             break;
  3106   3111           }else if( once ){
  3107   3112             whereOrMove(&sSum, &sCur);
................................................................................
  3150   3155   }
  3151   3156   
  3152   3157   /*
  3153   3158   ** Add all WhereLoop objects for all tables 
  3154   3159   */
  3155   3160   static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
  3156   3161     WhereInfo *pWInfo = pBuilder->pWInfo;
  3157         -  Bitmask mExtra = 0;
         3162  +  Bitmask mPrereq = 0;
  3158   3163     Bitmask mPrior = 0;
  3159   3164     int iTab;
  3160   3165     SrcList *pTabList = pWInfo->pTabList;
  3161   3166     struct SrcList_item *pItem;
  3162   3167     struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
  3163   3168     sqlite3 *db = pWInfo->pParse->db;
  3164   3169     int rc = SQLITE_OK;
................................................................................
  3171   3176     for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
  3172   3177       Bitmask mUnusable = 0;
  3173   3178       pNew->iTab = iTab;
  3174   3179       pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
  3175   3180       if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
  3176   3181         /* This condition is true when pItem is the FROM clause term on the
  3177   3182         ** right-hand-side of a LEFT or CROSS JOIN.  */
  3178         -      mExtra = mPrior;
         3183  +      mPrereq = mPrior;
  3179   3184       }
  3180   3185       priorJointype = pItem->fg.jointype;
  3181   3186       if( IsVirtual(pItem->pTab) ){
  3182   3187         struct SrcList_item *p;
  3183   3188         for(p=&pItem[1]; p<pEnd; p++){
  3184   3189           if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
  3185   3190             mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
  3186   3191           }
  3187   3192         }
  3188         -      rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable);
         3193  +      rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
  3189   3194       }else{
  3190         -      rc = whereLoopAddBtree(pBuilder, mExtra);
         3195  +      rc = whereLoopAddBtree(pBuilder, mPrereq);
  3191   3196       }
  3192   3197       if( rc==SQLITE_OK ){
  3193         -      rc = whereLoopAddOr(pBuilder, mExtra, mUnusable);
         3198  +      rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
  3194   3199       }
  3195   3200       mPrior |= pNew->maskSelf;
  3196   3201       if( rc || db->mallocFailed ) break;
  3197   3202     }
  3198   3203   
  3199   3204     whereLoopClear(db, pNew);
  3200   3205     return rc;
................................................................................
  4276   4281       if( db->mallocFailed ) goto whereBeginError;
  4277   4282       if( pWInfo->pOrderBy ){
  4278   4283          wherePathSolver(pWInfo, pWInfo->nRowOut+1);
  4279   4284          if( db->mallocFailed ) goto whereBeginError;
  4280   4285       }
  4281   4286     }
  4282   4287     if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
  4283         -     pWInfo->revMask = (Bitmask)(-1);
         4288  +     pWInfo->revMask = ALLBITS;
  4284   4289     }
  4285   4290     if( pParse->nErr || NEVER(db->mallocFailed) ){
  4286   4291       goto whereBeginError;
  4287   4292     }
  4288   4293   #ifdef WHERETRACE_ENABLED
  4289   4294     if( sqlite3WhereTrace ){
  4290   4295       sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);

Changes to src/wherecode.c.

   878    878   
   879    879       sqlite3ExprCachePush(pParse);
   880    880       iReg = sqlite3GetTempRange(pParse, nConstraint+2);
   881    881       addrNotFound = pLevel->addrBrk;
   882    882       for(j=0; j<nConstraint; j++){
   883    883         int iTarget = iReg+j+2;
   884    884         pTerm = pLoop->aLTerm[j];
   885         -      if( pTerm==0 ) continue;
          885  +      if( NEVER(pTerm==0) ) continue;
   886    886         if( pTerm->eOperator & WO_IN ){
   887    887           codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
   888    888           addrNotFound = pLevel->addrNxt;
   889    889         }else{
   890    890           sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
   891    891         }
   892    892       }
................................................................................
   911    911           VdbeOp *pOp;     /* Opcode to access the value of the IN constraint */
   912    912   
   913    913           /* Reload the constraint value into reg[iReg+j+2].  The same value
   914    914           ** was loaded into the same register prior to the OP_VFilter, but
   915    915           ** the xFilter implementation might have changed the datatype or
   916    916           ** encoding of the value in the register, so it *must* be reloaded. */
   917    917           assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
   918         -        if( pLevel->u.in.aInLoop!=0 ){
          918  +        if( !db->mallocFailed ){
   919    919             assert( iIn>0 );
   920    920             pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop);
   921    921             assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
   922    922             assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
   923    923             assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
   924    924             testcase( pOp->opcode==OP_Rowid );
   925    925             sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);

Changes to test/vtab6.test.

   562    562   set ::echo_module_ignore_usable 1
   563    563   db cache flush
   564    564   
   565    565   do_test vtab6-11.4.1 {
   566    566     catchsql {
   567    567       SELECT a, b, c FROM ab NATURAL JOIN bc;
   568    568     }
   569         -} {1 {table ab: xBestIndex returned an invalid plan}}
          569  +} {1 {ab.xBestIndex malfunction}}
   570    570   do_test vtab6-11.4.2 {
   571    571     catchsql {
   572    572       SELECT a, b, c FROM bc NATURAL JOIN ab;
   573    573     }
   574         -} {1 {table bc: xBestIndex returned an invalid plan}}
          574  +} {1 {bc.xBestIndex malfunction}}
   575    575   
   576    576   unset ::echo_module_ignore_usable
   577    577   
   578    578   finish_test