/ Check-in [cdff3af7]
Login

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

Overview
Comment:Remove the column-cache from the code generator. The column-cache has been a persistent source of bugs for years and with recent improvements in the performance of OP_Column, it no longer provides a benefit. After the column cache is removed, the binary is almost 2KB smaller and the speed-check.sh performance test is over 3 million cycles faster.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: cdff3af7bedc5ebea04bd9b5e2112d3db451a475c18c9a9ebd45f6f3a89a43cb
User & Date: drh 2018-08-04 20:30:55
Context
2018-08-06
01:21
Fix an overzealous assert() statement discovered by OSSFuzz. check-in: d9c9fe9f user: drh tags: trunk
2018-08-04
20:30
Remove the column-cache from the code generator. The column-cache has been a persistent source of bugs for years and with recent improvements in the performance of OP_Column, it no longer provides a benefit. After the column cache is removed, the binary is almost 2KB smaller and the speed-check.sh performance test is over 3 million cycles faster. check-in: cdff3af7 user: drh tags: trunk
20:12
Remove a testcase() that is no longer reachable without the column cache. Provide an assert() to help prove that the testcase is no longer reachable. Closed-Leaf check-in: a500893b user: drh tags: omit-column-cache
15:28
Add test cases for the constant expression fix of the previous check-in. check-in: 95db5bd9 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/build.c.

   221    221       }
   222    222     }
   223    223   
   224    224   
   225    225     /* Get the VDBE program ready for execution
   226    226     */
   227    227     if( v && pParse->nErr==0 && !db->mallocFailed ){
   228         -    assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
   229    228       /* A minimum of one cursor is required if autoincrement is used
   230    229       *  See ticket [a696379c1f08866] */
   231    230       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
   232    231       sqlite3VdbeMakeReady(v, pParse);
   233    232       pParse->rc = SQLITE_DONE;
   234    233     }else{
   235    234       pParse->rc = SQLITE_ERROR;

Changes to src/delete.c.

   459    459         for(i=0; i<nPk; i++){
   460    460           assert( pPk->aiColumn[i]>=0 );
   461    461           sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
   462    462                                           pPk->aiColumn[i], iPk+i);
   463    463         }
   464    464         iKey = iPk;
   465    465       }else{
   466         -      iKey = pParse->nMem + 1;
   467         -      iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
   468         -      if( iKey>pParse->nMem ) pParse->nMem = iKey;
          466  +      iKey = ++pParse->nMem;
          467  +      sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
   469    468       }
   470    469     
   471    470       if( eOnePass!=ONEPASS_OFF ){
   472    471         /* For ONEPASS, no need to store the rowid/primary-key. There is only
   473    472         ** one, so just keep it in its register(s) and fall through to the
   474    473         ** delete code.  */
   475    474         nKey = nPk; /* OP_Found will use an unpacked key */
................................................................................
   894    893     int regBase;
   895    894     int nCol;
   896    895   
   897    896     if( piPartIdxLabel ){
   898    897       if( pIdx->pPartIdxWhere ){
   899    898         *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
   900    899         pParse->iSelfTab = iDataCur + 1;
   901         -      sqlite3ExprCachePush(pParse);
   902    900         sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
   903    901                               SQLITE_JUMPIFNULL);
   904    902         pParse->iSelfTab = 0;
   905    903       }else{
   906    904         *piPartIdxLabel = 0;
   907    905       }
   908    906     }
................................................................................
   941    939   ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
   942    940   ** because it was a partial index, then this routine should be called to
   943    941   ** resolve that label.
   944    942   */
   945    943   void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
   946    944     if( iLabel ){
   947    945       sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
   948         -    sqlite3ExprCachePop(pParse);
   949    946     }
   950    947   }

Changes to src/expr.c.

   577    577     regRight = exprCodeSubselect(pParse, pRight);
   578    578   
   579    579     for(i=0; 1 /*Loop exits by "break"*/; i++){
   580    580       int regFree1 = 0, regFree2 = 0;
   581    581       Expr *pL, *pR; 
   582    582       int r1, r2;
   583    583       assert( i>=0 && i<nLeft );
   584         -    if( i>0 ) sqlite3ExprCachePush(pParse);
   585    584       r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
   586    585       r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
   587    586       codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
   588    587       testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
   589    588       testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
   590    589       testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
   591    590       testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
   592    591       testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
   593    592       testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
   594    593       sqlite3ReleaseTempReg(pParse, regFree1);
   595    594       sqlite3ReleaseTempReg(pParse, regFree2);
   596         -    if( i>0 ) sqlite3ExprCachePop(pParse);
   597    595       if( i==nLeft-1 ){
   598    596         break;
   599    597       }
   600    598       if( opx==TK_EQ ){
   601    599         sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
   602    600         p5 |= SQLITE_KEEPNULL;
   603    601       }else if( opx==TK_NE ){
................................................................................
  2623   2621     int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
  2624   2622     int isRowid             /* If true, LHS of IN operator is a rowid */
  2625   2623   ){
  2626   2624     int jmpIfDynamic = -1;                      /* One-time test address */
  2627   2625     int rReg = 0;                           /* Register storing resulting */
  2628   2626     Vdbe *v = sqlite3GetVdbe(pParse);
  2629   2627     if( NEVER(v==0) ) return 0;
  2630         -  sqlite3ExprCachePush(pParse);
  2631   2628   
  2632   2629     /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it
  2633   2630     ** is encountered if any of the following is true:
  2634   2631     **
  2635   2632     **    *  The right-hand side is a correlated subquery
  2636   2633     **    *  The right-hand side is an expression list containing variables
  2637   2634     **    *  We are inside a trigger
................................................................................
  2759   2756               if( isRowid ){
  2760   2757                 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
  2761   2758                                   sqlite3VdbeCurrentAddr(v)+2);
  2762   2759                 VdbeCoverage(v);
  2763   2760                 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
  2764   2761               }else{
  2765   2762                 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
  2766         -              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
  2767   2763                 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1);
  2768   2764               }
  2769   2765             }
  2770   2766           }
  2771   2767           sqlite3ReleaseTempReg(pParse, r1);
  2772   2768           sqlite3ReleaseTempReg(pParse, r2);
  2773   2769         }
................................................................................
  2840   2836     if( rHasNullFlag ){
  2841   2837       sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
  2842   2838     }
  2843   2839   
  2844   2840     if( jmpIfDynamic>=0 ){
  2845   2841       sqlite3VdbeJumpHere(v, jmpIfDynamic);
  2846   2842     }
  2847         -  sqlite3ExprCachePop(pParse);
  2848   2843   
  2849   2844     return rReg;
  2850   2845   }
  2851   2846   #endif /* SQLITE_OMIT_SUBQUERY */
  2852   2847   
  2853   2848   #ifndef SQLITE_OMIT_SUBQUERY
  2854   2849   /*
................................................................................
  2959   2954     ** at r1.
  2960   2955     **
  2961   2956     ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
  2962   2957     ** so that the fields are in the same order as an existing index.   The
  2963   2958     ** aiMap[] array contains a mapping from the original LHS field order to
  2964   2959     ** the field order that matches the RHS index.
  2965   2960     */
  2966         -  sqlite3ExprCachePush(pParse);
  2967   2961     rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
  2968   2962     for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
  2969   2963     if( i==nVector ){
  2970   2964       /* LHS fields are not reordered */
  2971   2965       rLhs = rLhsOrig;
  2972   2966     }else{
  2973   2967       /* Need to reorder the LHS fields according to aiMap */
................................................................................
  3118   3112     }
  3119   3113   
  3120   3114     /* Jumps here in order to return true. */
  3121   3115     sqlite3VdbeJumpHere(v, addrTruthOp);
  3122   3116   
  3123   3117   sqlite3ExprCodeIN_finished:
  3124   3118     if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
  3125         -  sqlite3ExprCachePop(pParse);
  3126   3119     VdbeComment((v, "end IN expr"));
  3127   3120   sqlite3ExprCodeIN_oom_error:
  3128   3121     sqlite3DbFree(pParse->db, aiMap);
  3129   3122     sqlite3DbFree(pParse->db, zAff);
  3130   3123   }
  3131   3124   #endif /* SQLITE_OMIT_SUBQUERY */
  3132   3125   
................................................................................
  3186   3179       }else{
  3187   3180         if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
  3188   3181         sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
  3189   3182       }
  3190   3183     }
  3191   3184   }
  3192   3185   
  3193         -/*
  3194         -** Erase column-cache entry number i
  3195         -*/
  3196         -static void cacheEntryClear(Parse *pParse, int i){
  3197         -  if( pParse->aColCache[i].tempReg ){
  3198         -    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
  3199         -      pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
  3200         -    }
  3201         -  }
  3202         -  pParse->nColCache--;
  3203         -  if( i<pParse->nColCache ){
  3204         -    pParse->aColCache[i] = pParse->aColCache[pParse->nColCache];
  3205         -  }
  3206         -}
  3207         -
  3208         -
  3209         -/*
  3210         -** Record in the column cache that a particular column from a
  3211         -** particular table is stored in a particular register.
  3212         -*/
  3213         -void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
  3214         -  int i;
  3215         -  int minLru;
  3216         -  int idxLru;
  3217         -  struct yColCache *p;
  3218         -
  3219         -  /* Unless an error has occurred, register numbers are always positive. */
  3220         -  assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
  3221         -  assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
  3222         -
  3223         -  /* The SQLITE_ColumnCache flag disables the column cache.  This is used
  3224         -  ** for testing only - to verify that SQLite always gets the same answer
  3225         -  ** with and without the column cache.
  3226         -  */
  3227         -  if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
  3228         -
  3229         -  /* First replace any existing entry.
  3230         -  **
  3231         -  ** Actually, the way the column cache is currently used, we are guaranteed
  3232         -  ** that the object will never already be in cache.  Verify this guarantee.
  3233         -  */
  3234         -#ifndef NDEBUG
  3235         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3236         -    assert( p->iTable!=iTab || p->iColumn!=iCol );
  3237         -  }
  3238         -#endif
  3239         -
  3240         -#ifdef SQLITE_DEBUG_COLUMNCACHE
  3241         -  /* Add a SetTabCol opcode for run-time verification that the column
  3242         -  ** cache is working correctly.
  3243         -  */
  3244         -  sqlite3VdbeAddOp3(pParse->pVdbe, OP_SetTabCol, iTab, iCol, iReg);
  3245         -#endif
  3246         -
  3247         -  /* If the cache is already full, delete the least recently used entry */
  3248         -  if( pParse->nColCache>=SQLITE_N_COLCACHE ){
  3249         -    minLru = 0x7fffffff;
  3250         -    idxLru = -1;
  3251         -    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
  3252         -      if( p->lru<minLru ){
  3253         -        idxLru = i;
  3254         -        minLru = p->lru;
  3255         -      }
  3256         -    }
  3257         -    p = &pParse->aColCache[idxLru];
  3258         -  }else{
  3259         -    p = &pParse->aColCache[pParse->nColCache++];
  3260         -  }
  3261         -
  3262         -  /* Add the new entry to the end of the cache */
  3263         -  p->iLevel = pParse->iCacheLevel;
  3264         -  p->iTable = iTab;
  3265         -  p->iColumn = iCol;
  3266         -  p->iReg = iReg;
  3267         -  p->tempReg = 0;
  3268         -  p->lru = pParse->iCacheCnt++;
  3269         -}
  3270         -
  3271         -/*
  3272         -** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
  3273         -** Purge the range of registers from the column cache.
  3274         -*/
  3275         -void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
  3276         -  int i = 0;
  3277         -  while( i<pParse->nColCache ){
  3278         -    struct yColCache *p = &pParse->aColCache[i];
  3279         -    if( p->iReg >= iReg && p->iReg < iReg+nReg ){
  3280         -      cacheEntryClear(pParse, i);
  3281         -    }else{
  3282         -      i++;
  3283         -    }
  3284         -  }
  3285         -}
  3286         -
  3287         -/*
  3288         -** Remember the current column cache context.  Any new entries added
  3289         -** added to the column cache after this call are removed when the
  3290         -** corresponding pop occurs.
  3291         -*/
  3292         -void sqlite3ExprCachePush(Parse *pParse){
  3293         -  pParse->iCacheLevel++;
  3294         -#ifdef SQLITE_DEBUG
  3295         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3296         -    printf("PUSH to %d\n", pParse->iCacheLevel);
  3297         -  }
  3298         -#endif
  3299         -}
  3300         -
  3301         -/*
  3302         -** Remove from the column cache any entries that were added since the
  3303         -** the previous sqlite3ExprCachePush operation.  In other words, restore
  3304         -** the cache to the state it was in prior the most recent Push.
  3305         -*/
  3306         -void sqlite3ExprCachePop(Parse *pParse){
  3307         -  int i = 0;
  3308         -  assert( pParse->iCacheLevel>=1 );
  3309         -  pParse->iCacheLevel--;
  3310         -#ifdef SQLITE_DEBUG
  3311         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3312         -    printf("POP  to %d\n", pParse->iCacheLevel);
  3313         -  }
  3314         -#endif
  3315         -  while( i<pParse->nColCache ){
  3316         -    if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){
  3317         -      cacheEntryClear(pParse, i);
  3318         -    }else{
  3319         -      i++;
  3320         -    }
  3321         -  }
  3322         -}
  3323         -
  3324         -/*
  3325         -** When a cached column is reused, make sure that its register is
  3326         -** no longer available as a temp register.  ticket #3879:  that same
  3327         -** register might be in the cache in multiple places, so be sure to
  3328         -** get them all.
  3329         -*/
  3330         -static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
  3331         -  int i;
  3332         -  struct yColCache *p;
  3333         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3334         -    if( p->iReg==iReg ){
  3335         -      p->tempReg = 0;
  3336         -    }
  3337         -  }
  3338         -}
  3339   3186   
  3340   3187   /* Generate code that will load into register regOut a value that is
  3341   3188   ** appropriate for the iIdxCol-th column of index pIdx.
  3342   3189   */
  3343   3190   void sqlite3ExprCodeLoadIndexColumn(
  3344   3191     Parse *pParse,  /* The parsing context */
  3345   3192     Index *pIdx,    /* The index whose column is to be loaded */
................................................................................
  3387   3234     if( iCol>=0 ){
  3388   3235       sqlite3ColumnDefault(v, pTab, iCol, regOut);
  3389   3236     }
  3390   3237   }
  3391   3238   
  3392   3239   /*
  3393   3240   ** Generate code that will extract the iColumn-th column from
  3394         -** table pTab and store the column value in a register. 
  3395         -**
  3396         -** An effort is made to store the column value in register iReg.  This
  3397         -** is not garanteeed for GetColumn() - the result can be stored in
  3398         -** any register.  But the result is guaranteed to land in register iReg
  3399         -** for GetColumnToReg().
         3241  +** table pTab and store the column value in register iReg. 
  3400   3242   **
  3401   3243   ** There must be an open cursor to pTab in iTable when this routine
  3402   3244   ** is called.  If iColumn<0 then code is generated that extracts the rowid.
  3403   3245   */
  3404   3246   int sqlite3ExprCodeGetColumn(
  3405   3247     Parse *pParse,   /* Parsing and code generating context */
  3406   3248     Table *pTab,     /* Description of the table we are reading from */
  3407   3249     int iColumn,     /* Index of the table column */
  3408   3250     int iTable,      /* The cursor pointing to the table */
  3409   3251     int iReg,        /* Store results here */
  3410   3252     u8 p5            /* P5 value for OP_Column + FLAGS */
  3411   3253   ){
  3412   3254     Vdbe *v = pParse->pVdbe;
  3413         -  int i;
  3414         -  struct yColCache *p;
  3415         -
  3416         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3417         -    if( p->iTable==iTable && p->iColumn==iColumn ){
  3418         -      p->lru = pParse->iCacheCnt++;
  3419         -      sqlite3ExprCachePinRegister(pParse, p->iReg);
  3420         -#ifdef SQLITE_DEBUG_COLUMNCACHE
  3421         -      sqlite3VdbeAddOp3(v, OP_VerifyTabCol, iTable, iColumn, p->iReg);
  3422         -#endif
  3423         -      return p->iReg;
  3424         -    }
  3425         -  }  
  3426   3255     assert( v!=0 );
  3427   3256     sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
  3428   3257     if( p5 ){
  3429   3258       sqlite3VdbeChangeP5(v, p5);
  3430         -  }else{   
  3431         -    sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
  3432   3259     }
  3433   3260     return iReg;
  3434   3261   }
  3435         -void sqlite3ExprCodeGetColumnToReg(
  3436         -  Parse *pParse,   /* Parsing and code generating context */
  3437         -  Table *pTab,     /* Description of the table we are reading from */
  3438         -  int iColumn,     /* Index of the table column */
  3439         -  int iTable,      /* The cursor pointing to the table */
  3440         -  int iReg         /* Store results here */
  3441         -){
  3442         -  int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0);
  3443         -  if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
  3444         -}
  3445         -
  3446         -
  3447         -/*
  3448         -** Clear all column cache entries.
  3449         -*/
  3450         -void sqlite3ExprCacheClear(Parse *pParse){
  3451         -  int i;
  3452         -
  3453         -#ifdef SQLITE_DEBUG
  3454         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3455         -    printf("CLEAR\n");
  3456         -  }
  3457         -#endif
  3458         -  for(i=0; i<pParse->nColCache; i++){
  3459         -    if( pParse->aColCache[i].tempReg
  3460         -     && pParse->nTempReg<ArraySize(pParse->aTempReg)
  3461         -    ){
  3462         -       pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
  3463         -    }
  3464         -  }
  3465         -  pParse->nColCache = 0;
  3466         -}
  3467         -
  3468         -/*
  3469         -** Record the fact that an affinity change has occurred on iCount
  3470         -** registers starting with iStart.
  3471         -*/
  3472         -void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
  3473         -  sqlite3ExprCacheRemove(pParse, iStart, iCount);
  3474         -}
  3475   3262   
  3476   3263   /*
  3477   3264   ** Generate code to move content from registers iFrom...iFrom+nReg-1
  3478         -** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
         3265  +** over to iTo..iTo+nReg-1.
  3479   3266   */
  3480   3267   void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
  3481   3268     assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
  3482   3269     sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
  3483         -  sqlite3ExprCacheRemove(pParse, iFrom, nReg);
  3484   3270   }
  3485         -
  3486         -#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  3487         -/*
  3488         -** Return true if any register in the range iFrom..iTo (inclusive)
  3489         -** is used as part of the column cache.
  3490         -**
  3491         -** This routine is used within assert() and testcase() macros only
  3492         -** and does not appear in a normal build.
  3493         -*/
  3494         -static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
  3495         -  int i;
  3496         -  struct yColCache *p;
  3497         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3498         -    int r = p->iReg;
  3499         -    if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
  3500         -  }
  3501         -  return 0;
  3502         -}
  3503         -#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
  3504         -
  3505   3271   
  3506   3272   /*
  3507   3273   ** Convert a scalar expression node to a TK_REGISTER referencing
  3508   3274   ** register iReg.  The caller must ensure that iReg already contains
  3509   3275   ** the correct value for the expression.
  3510   3276   */
  3511   3277   static void exprToRegister(Expr *p, int iReg){
................................................................................
  3679   3445         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  3680   3446         if( inReg!=target ){
  3681   3447           sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
  3682   3448           inReg = target;
  3683   3449         }
  3684   3450         sqlite3VdbeAddOp2(v, OP_Cast, target,
  3685   3451                           sqlite3AffinityType(pExpr->u.zToken, 0));
  3686         -      testcase( usedAsColumnCache(pParse, inReg, inReg) );
  3687         -      sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
  3688   3452         return inReg;
  3689   3453       }
  3690   3454   #endif /* SQLITE_OMIT_CAST */
  3691   3455       case TK_IS:
  3692   3456       case TK_ISNOT:
  3693   3457         op = (op==TK_IS) ? TK_EQ : TK_NE;
  3694   3458         p5 = SQLITE_NULLEQ;
................................................................................
  3866   3630         if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
  3867   3631           int endCoalesce = sqlite3VdbeMakeLabel(v);
  3868   3632           assert( nFarg>=2 );
  3869   3633           sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
  3870   3634           for(i=1; i<nFarg; i++){
  3871   3635             sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
  3872   3636             VdbeCoverage(v);
  3873         -          sqlite3ExprCacheRemove(pParse, target, 1);
  3874         -          sqlite3ExprCachePush(pParse);
  3875   3637             sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
  3876         -          sqlite3ExprCachePop(pParse);
  3877   3638           }
  3878   3639           sqlite3VdbeResolveLabel(v, endCoalesce);
  3879   3640           break;
  3880   3641         }
  3881   3642   
  3882   3643         /* The UNLIKELY() function is a no-op.  The result is the value
  3883   3644         ** of the first argument.
................................................................................
  3935   3696               assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
  3936   3697               testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
  3937   3698               pFarg->a[0].pExpr->op2 = 
  3938   3699                     pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
  3939   3700             }
  3940   3701           }
  3941   3702   
  3942         -        sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
  3943   3703           sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
  3944   3704                                   SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
  3945         -        sqlite3ExprCachePop(pParse);      /* Ticket 2ea2425d34be */
  3946   3705         }else{
  3947   3706           r1 = 0;
  3948   3707         }
  3949   3708   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3950   3709         /* Possibly overload the function if the first argument is
  3951   3710         ** a virtual table column.
  3952   3711         **
................................................................................
  4111   3870         sqlite3ErrorMsg(pParse, "row value misused");
  4112   3871         break;
  4113   3872       }
  4114   3873   
  4115   3874       case TK_IF_NULL_ROW: {
  4116   3875         int addrINR;
  4117   3876         addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
  4118         -      sqlite3ExprCachePush(pParse);
  4119   3877         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  4120         -      sqlite3ExprCachePop(pParse);
  4121   3878         sqlite3VdbeJumpHere(v, addrINR);
  4122   3879         sqlite3VdbeChangeP3(v, addrINR, inReg);
  4123   3880         break;
  4124   3881       }
  4125   3882   
  4126   3883       /*
  4127   3884       ** Form A:
................................................................................
  4150   3907         int nExpr;                        /* 2x number of WHEN terms */
  4151   3908         int i;                            /* Loop counter */
  4152   3909         ExprList *pEList;                 /* List of WHEN terms */
  4153   3910         struct ExprList_item *aListelem;  /* Array of WHEN terms */
  4154   3911         Expr opCompare;                   /* The X==Ei expression */
  4155   3912         Expr *pX;                         /* The X expression */
  4156   3913         Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
  4157         -      VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
  4158   3914   
  4159   3915         assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
  4160   3916         assert(pExpr->x.pList->nExpr > 0);
  4161   3917         pEList = pExpr->x.pList;
  4162   3918         aListelem = pEList->a;
  4163   3919         nExpr = pEList->nExpr;
  4164   3920         endLabel = sqlite3VdbeMakeLabel(v);
................................................................................
  4174   3930           /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
  4175   3931           ** The value in regFree1 might get SCopy-ed into the file result.
  4176   3932           ** So make sure that the regFree1 register is not reused for other
  4177   3933           ** purposes and possibly overwritten.  */
  4178   3934           regFree1 = 0;
  4179   3935         }
  4180   3936         for(i=0; i<nExpr-1; i=i+2){
  4181         -        sqlite3ExprCachePush(pParse);
  4182   3937           if( pX ){
  4183   3938             assert( pTest!=0 );
  4184   3939             opCompare.pRight = aListelem[i].pExpr;
  4185   3940           }else{
  4186   3941             pTest = aListelem[i].pExpr;
  4187   3942           }
  4188   3943           nextCase = sqlite3VdbeMakeLabel(v);
  4189   3944           testcase( pTest->op==TK_COLUMN );
  4190   3945           sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
  4191   3946           testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
  4192   3947           sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
  4193   3948           sqlite3VdbeGoto(v, endLabel);
  4194         -        sqlite3ExprCachePop(pParse);
  4195   3949           sqlite3VdbeResolveLabel(v, nextCase);
  4196   3950         }
  4197   3951         if( (nExpr&1)!=0 ){
  4198         -        sqlite3ExprCachePush(pParse);
  4199   3952           sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
  4200         -        sqlite3ExprCachePop(pParse);
  4201   3953         }else{
  4202   3954           sqlite3VdbeAddOp2(v, OP_Null, 0, target);
  4203   3955         }
  4204         -      assert( pParse->db->mallocFailed || pParse->nErr>0 
  4205         -           || pParse->iCacheLevel==iCacheLevel );
  4206   3956         sqlite3VdbeResolveLabel(v, endLabel);
  4207   3957         break;
  4208   3958       }
  4209   3959   #ifndef SQLITE_OMIT_TRIGGER
  4210   3960       case TK_RAISE: {
  4211   3961         assert( pExpr->affinity==OE_Rollback 
  4212   3962              || pExpr->affinity==OE_Abort
................................................................................
  4558   4308     if( NEVER(pExpr==0) ) return;  /* No way this can happen */
  4559   4309     op = pExpr->op;
  4560   4310     switch( op ){
  4561   4311       case TK_AND: {
  4562   4312         int d2 = sqlite3VdbeMakeLabel(v);
  4563   4313         testcase( jumpIfNull==0 );
  4564   4314         sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
  4565         -      sqlite3ExprCachePush(pParse);
  4566   4315         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  4567   4316         sqlite3VdbeResolveLabel(v, d2);
  4568         -      sqlite3ExprCachePop(pParse);
  4569   4317         break;
  4570   4318       }
  4571   4319       case TK_OR: {
  4572   4320         testcase( jumpIfNull==0 );
  4573   4321         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4574         -      sqlite3ExprCachePush(pParse);
  4575   4322         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  4576         -      sqlite3ExprCachePop(pParse);
  4577   4323         break;
  4578   4324       }
  4579   4325       case TK_NOT: {
  4580   4326         testcase( jumpIfNull==0 );
  4581   4327         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4582   4328         break;
  4583   4329       }
................................................................................
  4728   4474     assert( pExpr->op!=TK_GT || op==OP_Le );
  4729   4475     assert( pExpr->op!=TK_GE || op==OP_Lt );
  4730   4476   
  4731   4477     switch( pExpr->op ){
  4732   4478       case TK_AND: {
  4733   4479         testcase( jumpIfNull==0 );
  4734   4480         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4735         -      sqlite3ExprCachePush(pParse);
  4736   4481         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  4737         -      sqlite3ExprCachePop(pParse);
  4738   4482         break;
  4739   4483       }
  4740   4484       case TK_OR: {
  4741   4485         int d2 = sqlite3VdbeMakeLabel(v);
  4742   4486         testcase( jumpIfNull==0 );
  4743   4487         sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
  4744         -      sqlite3ExprCachePush(pParse);
  4745   4488         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  4746   4489         sqlite3VdbeResolveLabel(v, d2);
  4747         -      sqlite3ExprCachePop(pParse);
  4748   4490         break;
  4749   4491       }
  4750   4492       case TK_NOT: {
  4751   4493         testcase( jumpIfNull==0 );
  4752   4494         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4753   4495         break;
  4754   4496       }
................................................................................
  5508   5250     }
  5509   5251     return pParse->aTempReg[--pParse->nTempReg];
  5510   5252   }
  5511   5253   
  5512   5254   /*
  5513   5255   ** Deallocate a register, making available for reuse for some other
  5514   5256   ** purpose.
  5515         -**
  5516         -** If a register is currently being used by the column cache, then
  5517         -** the deallocation is deferred until the column cache line that uses
  5518         -** the register becomes stale.
  5519   5257   */
  5520   5258   void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
  5521   5259     if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
  5522         -    int i;
  5523         -    struct yColCache *p;
  5524         -    for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  5525         -      if( p->iReg==iReg ){
  5526         -        p->tempReg = 1;
  5527         -        return;
  5528         -      }
  5529         -    }
  5530   5260       pParse->aTempReg[pParse->nTempReg++] = iReg;
  5531   5261     }
  5532   5262   }
  5533   5263   
  5534   5264   /*
  5535   5265   ** Allocate or deallocate a block of nReg consecutive registers.
  5536   5266   */
  5537   5267   int sqlite3GetTempRange(Parse *pParse, int nReg){
  5538   5268     int i, n;
  5539   5269     if( nReg==1 ) return sqlite3GetTempReg(pParse);
  5540   5270     i = pParse->iRangeReg;
  5541   5271     n = pParse->nRangeReg;
  5542   5272     if( nReg<=n ){
  5543         -    assert( !usedAsColumnCache(pParse, i, i+n-1) );
  5544   5273       pParse->iRangeReg += nReg;
  5545   5274       pParse->nRangeReg -= nReg;
  5546   5275     }else{
  5547   5276       i = pParse->nMem+1;
  5548   5277       pParse->nMem += nReg;
  5549   5278     }
  5550   5279     return i;
  5551   5280   }
  5552   5281   void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
  5553   5282     if( nReg==1 ){
  5554   5283       sqlite3ReleaseTempReg(pParse, iReg);
  5555   5284       return;
  5556   5285     }
  5557         -  sqlite3ExprCacheRemove(pParse, iReg, nReg);
  5558   5286     if( nReg>pParse->nRangeReg ){
  5559   5287       pParse->nRangeReg = nReg;
  5560   5288       pParse->iRangeReg = iReg;
  5561   5289     }
  5562   5290   }
  5563   5291   
  5564   5292   /*

Changes to src/insert.c.

  1529   1529   
  1530   1530       /* Check to see if the new rowid already exists in the table.  Skip
  1531   1531       ** the following conflict logic if it does not. */
  1532   1532       VdbeNoopComment((v, "uniqueness check for ROWID"));
  1533   1533       sqlite3VdbeVerifyAbortable(v, onError);
  1534   1534       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
  1535   1535       VdbeCoverage(v);
  1536         -    sqlite3ExprCachePush(pParse);
  1537   1536   
  1538   1537       switch( onError ){
  1539   1538         default: {
  1540   1539           onError = OE_Abort;
  1541   1540           /* Fall thru into the next case */
  1542   1541         }
  1543   1542         case OE_Rollback:
................................................................................
  1606   1605   #endif
  1607   1606         case OE_Ignore: {
  1608   1607           testcase( onError==OE_Ignore );
  1609   1608           sqlite3VdbeGoto(v, ignoreDest);
  1610   1609           break;
  1611   1610         }
  1612   1611       }
  1613         -    sqlite3ExprCachePop(pParse);
  1614   1612       sqlite3VdbeResolveLabel(v, addrRowidOk);
  1615   1613       if( sAddr.ipkTop ){
  1616   1614         sAddr.ipkBtm = sqlite3VdbeAddOp0(v, OP_Goto);
  1617   1615         sqlite3VdbeJumpHere(v, sAddr.ipkTop-1);
  1618   1616       }
  1619   1617     }
  1620   1618   
................................................................................
  1740   1738            (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
  1741   1739       ){
  1742   1740         sqlite3VdbeResolveLabel(v, addrUniqueOk);
  1743   1741         continue;
  1744   1742       }
  1745   1743   
  1746   1744       /* Check to see if the new index entry will be unique */
  1747         -    sqlite3ExprCachePush(pParse);
  1748   1745       sqlite3VdbeVerifyAbortable(v, onError);
  1749   1746       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
  1750   1747                            regIdx, pIdx->nKeyCol); VdbeCoverage(v);
  1751   1748   
  1752   1749       /* Generate code to handle collisions */
  1753   1750       regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
  1754   1751       if( isUpdate || onError==OE_Replace ){
................................................................................
  1846   1843         }
  1847   1844       }
  1848   1845       if( pUpIdx==pIdx ){
  1849   1846         sqlite3VdbeJumpHere(v, upsertBypass);
  1850   1847       }else{
  1851   1848         sqlite3VdbeResolveLabel(v, addrUniqueOk);
  1852   1849       }
  1853         -    sqlite3ExprCachePop(pParse);
  1854   1850       if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
  1855   1851   
  1856   1852     }
  1857   1853     testcase( sAddr.ipkTop!=0 );
  1858   1854     testcase( sAddr.upsertTop
  1859   1855            && sqlite3VdbeLabelHasBeenResolved(v,sAddr.upsertTop) );
  1860   1856     reorderConstraintChecks(v, &sAddr);
................................................................................
  1950   1946     if( !HasRowid(pTab) ) return;
  1951   1947     regData = regNewData + 1;
  1952   1948     regRec = sqlite3GetTempReg(pParse);
  1953   1949     sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
  1954   1950     sqlite3SetMakeRecordP5(v, pTab);
  1955   1951     if( !bAffinityDone ){
  1956   1952       sqlite3TableAffinity(v, pTab, 0);
  1957         -    sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
  1958   1953     }
  1959   1954     if( pParse->nested ){
  1960   1955       pik_flags = 0;
  1961   1956     }else{
  1962   1957       pik_flags = OPFLAG_NCHANGE;
  1963   1958       pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
  1964   1959     }

Changes to src/pragma.c.

  1546   1546           Index *pPrior = 0;
  1547   1547           int loopTop;
  1548   1548           int iDataCur, iIdxCur;
  1549   1549           int r1 = -1;
  1550   1550   
  1551   1551           if( pTab->tnum<1 ) continue;  /* Skip VIEWs or VIRTUAL TABLEs */
  1552   1552           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
  1553         -        sqlite3ExprCacheClear(pParse);
  1554   1553           sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
  1555   1554                                      1, 0, &iDataCur, &iIdxCur);
  1556   1555           /* reg[7] counts the number of entries in the table.
  1557   1556           ** reg[8+i] counts the number of entries in the i-th index 
  1558   1557           */
  1559   1558           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
  1560   1559           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
................................................................................
  1589   1588             ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
  1590   1589             if( db->mallocFailed==0 ){
  1591   1590               int addrCkFault = sqlite3VdbeMakeLabel(v);
  1592   1591               int addrCkOk = sqlite3VdbeMakeLabel(v);
  1593   1592               char *zErr;
  1594   1593               int k;
  1595   1594               pParse->iSelfTab = iDataCur + 1;
  1596         -            sqlite3ExprCachePush(pParse);
  1597   1595               for(k=pCheck->nExpr-1; k>0; k--){
  1598   1596                 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
  1599   1597               }
  1600   1598               sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 
  1601   1599                   SQLITE_JUMPIFNULL);
  1602   1600               sqlite3VdbeResolveLabel(v, addrCkFault);
  1603   1601               pParse->iSelfTab = 0;
  1604   1602               zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
  1605   1603                   pTab->zName);
  1606   1604               sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
  1607   1605               integrityCheckResultRow(v);
  1608   1606               sqlite3VdbeResolveLabel(v, addrCkOk);
  1609         -            sqlite3ExprCachePop(pParse);
  1610   1607             }
  1611   1608             sqlite3ExprListDelete(db, pCheck);
  1612   1609           }
  1613   1610           if( !isQuick ){ /* Omit the remaining tests for quick_check */
  1614   1611             /* Validate index entries for the current row */
  1615   1612             for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1616   1613               int jmp2, jmp3, jmp4, jmp5;

Changes to src/select.c.

  1145   1145           pushOntoSorter(
  1146   1146               pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
  1147   1147         }else{
  1148   1148           int r1 = sqlite3GetTempReg(pParse);
  1149   1149           assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
  1150   1150           sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol, 
  1151   1151               r1, pDest->zAffSdst, nResultCol);
  1152         -        sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
  1153   1152           sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
  1154   1153           sqlite3ReleaseTempReg(pParse, r1);
  1155   1154         }
  1156   1155         break;
  1157   1156       }
  1158   1157   
  1159   1158       /* If any row exist in the result set, record that fact and abort.
................................................................................
  1189   1188         if( pSort ){
  1190   1189           pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
  1191   1190                          nPrefixReg);
  1192   1191         }else if( eDest==SRT_Coroutine ){
  1193   1192           sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
  1194   1193         }else{
  1195   1194           sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
  1196         -        sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
  1197   1195         }
  1198   1196         break;
  1199   1197       }
  1200   1198   
  1201   1199   #ifndef SQLITE_OMIT_CTE
  1202   1200       /* Write the results into a priority queue that is order according to
  1203   1201       ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
................................................................................
  1546   1544         break;
  1547   1545       }
  1548   1546   #ifndef SQLITE_OMIT_SUBQUERY
  1549   1547       case SRT_Set: {
  1550   1548         assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
  1551   1549         sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
  1552   1550                           pDest->zAffSdst, nColumn);
  1553         -      sqlite3ExprCacheAffinityChange(pParse, regRow, nColumn);
  1554   1551         sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
  1555   1552         break;
  1556   1553       }
  1557   1554       case SRT_Mem: {
  1558   1555         /* The LIMIT clause will terminate the loop for us */
  1559   1556         break;
  1560   1557       }
................................................................................
  1561   1558   #endif
  1562   1559       default: {
  1563   1560         assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
  1564   1561         testcase( eDest==SRT_Output );
  1565   1562         testcase( eDest==SRT_Coroutine );
  1566   1563         if( eDest==SRT_Output ){
  1567   1564           sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
  1568         -        sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
  1569   1565         }else{
  1570   1566           sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
  1571   1567         }
  1572   1568         break;
  1573   1569       }
  1574   1570     }
  1575   1571     if( regRowid ){
................................................................................
  2162   2158   
  2163   2159     /* 
  2164   2160     ** "LIMIT -1" always shows all rows.  There is some
  2165   2161     ** controversy about what the correct behavior should be.
  2166   2162     ** The current implementation interprets "LIMIT 0" to mean
  2167   2163     ** no rows.
  2168   2164     */
  2169         -  sqlite3ExprCacheClear(pParse);
  2170   2165     if( pLimit ){
  2171   2166       assert( pLimit->op==TK_LIMIT );
  2172   2167       assert( pLimit->pLeft!=0 );
  2173   2168       p->iLimit = iLimit = ++pParse->nMem;
  2174   2169       v = sqlite3GetVdbe(pParse);
  2175   2170       assert( v!=0 );
  2176   2171       if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
................................................................................
  2948   2943       */
  2949   2944       case SRT_Set: {
  2950   2945         int r1;
  2951   2946         testcase( pIn->nSdst>1 );
  2952   2947         r1 = sqlite3GetTempReg(pParse);
  2953   2948         sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, 
  2954   2949             r1, pDest->zAffSdst, pIn->nSdst);
  2955         -      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
  2956   2950         sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
  2957   2951                              pIn->iSdst, pIn->nSdst);
  2958   2952         sqlite3ReleaseTempReg(pParse, r1);
  2959   2953         break;
  2960   2954       }
  2961   2955   
  2962   2956       /* If this is a scalar select that is part of an expression, then
................................................................................
  2991   2985       ** For SRT_Output, results are stored in a sequence of registers.  
  2992   2986       ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
  2993   2987       ** return the next row of result.
  2994   2988       */
  2995   2989       default: {
  2996   2990         assert( pDest->eDest==SRT_Output );
  2997   2991         sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
  2998         -      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
  2999   2992         break;
  3000   2993       }
  3001   2994     }
  3002   2995   
  3003   2996     /* Jump to the end of the loop if the LIMIT is reached.
  3004   2997     */
  3005   2998     if( p->iLimit ){
................................................................................
  5332   5325         }
  5333   5326         if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
  5334   5327         sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
  5335   5328       }
  5336   5329       sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
  5337   5330       sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
  5338   5331       sqlite3VdbeChangeP5(v, (u8)nArg);
  5339         -    sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
  5340   5332       sqlite3ReleaseTempRange(pParse, regAgg, nArg);
  5341   5333       if( addrNext ){
  5342   5334         sqlite3VdbeResolveLabel(v, addrNext);
  5343         -      sqlite3ExprCacheClear(pParse);
  5344   5335       }
  5345   5336     }
  5346         -
  5347         -  /* Before populating the accumulator registers, clear the column cache.
  5348         -  ** Otherwise, if any of the required column values are already present 
  5349         -  ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
  5350         -  ** to pC->iMem. But by the time the value is used, the original register
  5351         -  ** may have been used, invalidating the underlying buffer holding the
  5352         -  ** text or blob value. See ticket [883034dcb5].
  5353         -  **
  5354         -  ** Another solution would be to change the OP_SCopy used to copy cached
  5355         -  ** values to an OP_Copy.
  5356         -  */
  5357   5337     if( regHit==0 && pAggInfo->nAccumulator ){
  5358   5338       regHit = regAcc;
  5359   5339     }
  5360   5340     if( regHit ){
  5361   5341       addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
  5362   5342     }
  5363         -  sqlite3ExprCacheClear(pParse);
  5364   5343     for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
  5365   5344       sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
  5366   5345     }
  5367   5346     pAggInfo->directMode = 0;
  5368         -  sqlite3ExprCacheClear(pParse);
  5369   5347     if( addrHitTest ){
  5370   5348       sqlite3VdbeJumpHere(v, addrHitTest);
  5371   5349     }
  5372   5350   }
  5373   5351   
  5374   5352   /*
  5375   5353   ** Add a single OP_Explain instruction to the VDBE to explain a simple
................................................................................
  6314   6292           for(i=0; i<sAggInfo.nColumn; i++){
  6315   6293             if( sAggInfo.aCol[i].iSorterColumn>=j ){
  6316   6294               nCol++;
  6317   6295               j++;
  6318   6296             }
  6319   6297           }
  6320   6298           regBase = sqlite3GetTempRange(pParse, nCol);
  6321         -        sqlite3ExprCacheClear(pParse);
  6322   6299           sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
  6323   6300           j = nGroupBy;
  6324   6301           for(i=0; i<sAggInfo.nColumn; i++){
  6325   6302             struct AggInfo_col *pCol = &sAggInfo.aCol[i];
  6326   6303             if( pCol->iSorterColumn>=j ){
  6327   6304               int r1 = j + regBase;
  6328         -            sqlite3ExprCodeGetColumnToReg(pParse, 
  6329         -                               pCol->pTab, pCol->iColumn, pCol->iTable, r1);
         6305  +            sqlite3ExprCodeGetColumnOfTable(v,
         6306  +                               pCol->pTab, pCol->iTable, pCol->iColumn, r1);
  6330   6307               j++;
  6331   6308             }
  6332   6309           }
  6333   6310           regRecord = sqlite3GetTempReg(pParse);
  6334   6311           sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
  6335   6312           sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
  6336   6313           sqlite3ReleaseTempReg(pParse, regRecord);
................................................................................
  6338   6315           sqlite3WhereEnd(pWInfo);
  6339   6316           sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
  6340   6317           sortOut = sqlite3GetTempReg(pParse);
  6341   6318           sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
  6342   6319           sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
  6343   6320           VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
  6344   6321           sAggInfo.useSortingIdx = 1;
  6345         -        sqlite3ExprCacheClear(pParse);
  6346         -
  6347   6322         }
  6348   6323   
  6349   6324         /* If the index or temporary table used by the GROUP BY sort
  6350   6325         ** will naturally deliver rows in the order required by the ORDER BY
  6351   6326         ** clause, cancel the ephemeral table open coded earlier.
  6352   6327         **
  6353   6328         ** This is an optimization - the correct answer should result regardless.
................................................................................
  6362   6337   
  6363   6338         /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
  6364   6339         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
  6365   6340         ** Then compare the current GROUP BY terms against the GROUP BY terms
  6366   6341         ** from the previous row currently stored in a0, a1, a2...
  6367   6342         */
  6368   6343         addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
  6369         -      sqlite3ExprCacheClear(pParse);
  6370   6344         if( groupBySort ){
  6371   6345           sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
  6372   6346                             sortOut, sortPTab);
  6373   6347         }
  6374   6348         for(j=0; j<pGroupBy->nExpr; j++){
  6375   6349           if( groupBySort ){
  6376   6350             sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);

Changes to src/sqliteInt.h.

   363    363   #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
   364    364   # define NDEBUG 1
   365    365   #endif
   366    366   #if defined(NDEBUG) && defined(SQLITE_DEBUG)
   367    367   # undef NDEBUG
   368    368   #endif
   369    369   
   370         -/* SQLITE_DEBUG_COLUMNCACHE is synomous with SQLITE_DEBUG.  The 
   371         -** SQLITE_DEBUG_COLUMNCACHE symbol only exists to provide a convenient
   372         -** way to search for all code that deals with verifying correct behavior
   373         -** of the column cache.
   374         -*/
   375         -#ifdef SQLITE_DEBUG
   376         -# define SQLITE_DEBUG_COLUMNCACHE 1
   377         -#else
   378         -# undef SQLIT_DEBUG_COLUMNCACHE
   379         -#endif
   380         -
   381    370   /*
   382    371   ** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
   383    372   */
   384    373   #if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
   385    374   # define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
   386    375   #endif
   387    376   
................................................................................
  1565   1554   
  1566   1555   /*
  1567   1556   ** Bits of the sqlite3.dbOptFlags field that are used by the
  1568   1557   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
  1569   1558   ** selectively disable various optimizations.
  1570   1559   */
  1571   1560   #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
  1572         -#define SQLITE_ColumnCache    0x0002   /* Column cache */
         1561  +                          /*  0x0002   available for reuse */
  1573   1562   #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
  1574   1563   #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
  1575   1564   #define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
  1576   1565   #define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
  1577   1566   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
  1578   1567   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
  1579   1568   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
................................................................................
  2964   2953   struct AutoincInfo {
  2965   2954     AutoincInfo *pNext;   /* Next info block in a list of them all */
  2966   2955     Table *pTab;          /* Table this info block refers to */
  2967   2956     int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
  2968   2957     int regCtr;           /* Memory register holding the rowid counter */
  2969   2958   };
  2970   2959   
  2971         -/*
  2972         -** Size of the column cache
  2973         -*/
  2974         -#ifndef SQLITE_N_COLCACHE
  2975         -# define SQLITE_N_COLCACHE 10
  2976         -#endif
  2977         -
  2978   2960   /*
  2979   2961   ** At least one instance of the following structure is created for each
  2980   2962   ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
  2981   2963   ** statement. All such objects are stored in the linked list headed at
  2982   2964   ** Parse.pTriggerPrg and deleted once statement compilation has been
  2983   2965   ** completed.
  2984   2966   **
................................................................................
  3046   3028     u8 nested;           /* Number of nested calls to the parser/code generator */
  3047   3029     u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
  3048   3030     u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
  3049   3031     u8 mayAbort;         /* True if statement may throw an ABORT exception */
  3050   3032     u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
  3051   3033     u8 okConstFactor;    /* OK to factor out constants */
  3052   3034     u8 disableLookaside; /* Number of times lookaside has been disabled */
  3053         -  u8 nColCache;        /* Number of entries in aColCache[] */
  3054   3035     int nRangeReg;       /* Size of the temporary register block */
  3055   3036     int iRangeReg;       /* First register in temporary register block */
  3056   3037     int nErr;            /* Number of errors seen */
  3057   3038     int nTab;            /* Number of previously allocated VDBE cursors */
  3058   3039     int nMem;            /* Number of memory cells used so far */
  3059   3040     int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
  3060   3041     int szOpAlloc;       /* Bytes of memory space allocated for Vdbe.aOp[] */
  3061   3042     int iSelfTab;        /* Table associated with an index on expr, or negative
  3062   3043                          ** of the base register during check-constraint eval */
  3063         -  int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
  3064         -  int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
  3065   3044     int nLabel;          /* Number of labels used */
  3066   3045     int *aLabel;         /* Space to hold the labels */
  3067   3046     ExprList *pConstExpr;/* Constant expressions */
  3068   3047     Token constraintName;/* Name of the constraint currently being parsed */
  3069   3048     yDbMask writeMask;   /* Start a write transaction on these databases */
  3070   3049     yDbMask cookieMask;  /* Bitmask of schema verified databases */
  3071   3050     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
................................................................................
  3087   3066     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
  3088   3067     u8 disableTriggers;  /* True to disable triggers */
  3089   3068   
  3090   3069     /**************************************************************************
  3091   3070     ** Fields above must be initialized to zero.  The fields that follow,
  3092   3071     ** down to the beginning of the recursive section, do not need to be
  3093   3072     ** initialized as they will be set before being used.  The boundary is
  3094         -  ** determined by offsetof(Parse,aColCache).
         3073  +  ** determined by offsetof(Parse,aTempReg).
  3095   3074     **************************************************************************/
  3096   3075   
  3097         -  struct yColCache {
  3098         -    int iTable;           /* Table cursor number */
  3099         -    i16 iColumn;          /* Table column number */
  3100         -    u8 tempReg;           /* iReg is a temp register that needs to be freed */
  3101         -    int iLevel;           /* Nesting level */
  3102         -    int iReg;             /* Reg with value of this column. 0 means none. */
  3103         -    int lru;              /* Least recently used entry has the smallest value */
  3104         -  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
  3105   3076     int aTempReg[8];        /* Holding area for temporary registers */
  3106   3077     Token sNameToken;       /* Token with unqualified schema object name */
  3107   3078   
  3108   3079     /************************************************************************
  3109   3080     ** Above is constant between recursions.  Below is reset before and after
  3110   3081     ** each recursion.  The boundary between these two regions is determined
  3111   3082     ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
................................................................................
  3139   3110     With *pWith;              /* Current WITH clause, or NULL */
  3140   3111     With *pWithToFree;        /* Free this WITH object at the end of the parse */
  3141   3112   };
  3142   3113   
  3143   3114   /*
  3144   3115   ** Sizes and pointers of various parts of the Parse object.
  3145   3116   */
  3146         -#define PARSE_HDR_SZ offsetof(Parse,aColCache) /* Recursive part w/o aColCache*/
         3117  +#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
  3147   3118   #define PARSE_RECURSE_SZ offsetof(Parse,sLastToken)    /* Recursive part */
  3148   3119   #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
  3149   3120   #define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ)  /* Pointer to tail */
  3150   3121   
  3151   3122   /*
  3152   3123   ** Return true if currently inside an sqlite3_declare_vtab() call.
  3153   3124   */
................................................................................
  3923   3894   int sqlite3WhereBreakLabel(WhereInfo*);
  3924   3895   int sqlite3WhereOkOnePass(WhereInfo*, int*);
  3925   3896   #define ONEPASS_OFF      0        /* Use of ONEPASS not allowed */
  3926   3897   #define ONEPASS_SINGLE   1        /* ONEPASS valid for a single row update */
  3927   3898   #define ONEPASS_MULTI    2        /* ONEPASS is valid for multiple rows */
  3928   3899   void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
  3929   3900   int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
  3930         -void sqlite3ExprCodeGetColumnToReg(Parse*, Table*, int, int, int);
  3931   3901   void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
  3932   3902   void sqlite3ExprCodeMove(Parse*, int, int, int);
  3933         -void sqlite3ExprCacheStore(Parse*, int, int, int);
  3934         -void sqlite3ExprCachePush(Parse*);
  3935         -void sqlite3ExprCachePop(Parse*);
  3936         -void sqlite3ExprCacheRemove(Parse*, int, int);
  3937         -void sqlite3ExprCacheClear(Parse*);
  3938         -void sqlite3ExprCacheAffinityChange(Parse*, int, int);
  3939   3903   void sqlite3ExprCode(Parse*, Expr*, int);
  3940   3904   void sqlite3ExprCodeCopy(Parse*, Expr*, int);
  3941   3905   void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
  3942   3906   int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
  3943   3907   int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
  3944   3908   int sqlite3ExprCodeTarget(Parse*, Expr*, int);
  3945   3909   void sqlite3ExprCodeAndCache(Parse*, Expr*, int);

Changes to src/test1.c.

  6978   6978     static const struct {
  6979   6979       const char *zOptName;
  6980   6980       int mask;
  6981   6981     } aOpt[] = {
  6982   6982       { "all",                 SQLITE_AllOpts        },
  6983   6983       { "none",                0                     },
  6984   6984       { "query-flattener",     SQLITE_QueryFlattener },
  6985         -    { "column-cache",        SQLITE_ColumnCache    },
  6986   6985       { "groupby-order",       SQLITE_GroupByOrder   },
  6987   6986       { "factor-constants",    SQLITE_FactorOutConst },
  6988   6987       { "distinct-opt",        SQLITE_DistinctOpt    },
  6989   6988       { "cover-idx-scan",      SQLITE_CoverIdxScan   },
  6990   6989       { "order-by-idx-join",   SQLITE_OrderByIdxJoin },
  6991   6990       { "transitive",          SQLITE_Transitive     },
  6992   6991       { "omit-noop-join",      SQLITE_OmitNoopJoin   },

Changes to src/update.c.

   606    606           /* This branch loads the value of a column that will not be changed 
   607    607           ** into a register. This is done if there are no BEFORE triggers, or
   608    608           ** if there are one or more BEFORE triggers that use this value via
   609    609           ** a new.* reference in a trigger program.
   610    610           */
   611    611           testcase( i==31 );
   612    612           testcase( i==32 );
   613         -        sqlite3ExprCodeGetColumnToReg(pParse, pTab, i, iDataCur, regNew+i);
   614         -        if( tmask & TRIGGER_BEFORE ){
   615         -          /* This value will be recomputed in After-BEFORE-trigger-reload-loop
   616         -          ** below, so make sure that it is not cached and reused.
   617         -          ** Ticket d85fffd6ffe856092ed8daefa811b1e399706b28. */
   618         -          sqlite3ExprCacheRemove(pParse, regNew+i, 1);
   619         -        }
          613  +        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
   620    614         }else{
   621    615           sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
   622    616         }
   623    617       }
   624    618     }
   625    619   
   626    620     /* Fire any BEFORE UPDATE triggers. This happens before constraints are

Changes to src/vdbe.c.

    32     32   */
    33     33   #ifdef SQLITE_DEBUG
    34     34   # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
    35     35   #else
    36     36   # define memAboutToChange(P,M)
    37     37   #endif
    38     38   
    39         -/*
    40         -** Given a cursor number and a column for a table or index, compute a
    41         -** hash value for use in the Mem.iTabColHash value.  The iTabColHash
    42         -** column is only used for verification - it is omitted from production
    43         -** builds.  Collisions are harmless in the sense that the correct answer
    44         -** still results.  The only harm of collisions is that they can potential
    45         -** reduce column-cache error detection during SQLITE_DEBUG builds.
    46         -**
    47         -** No valid hash should be 0.
    48         -*/
    49         -#define TableColumnHash(T,C)  (((u32)(T)<<16)^(u32)(C+2))
    50         -
    51     39   /*
    52     40   ** The following global variable is incremented every time a cursor
    53     41   ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
    54     42   ** procedures use this information to make sure that indices are
    55     43   ** working correctly.  This variable has no function other than to
    56     44   ** help verify the correct operation of the library.
    57     45   */
................................................................................
  1304   1292     assert( pOut!=pIn1 );
  1305   1293     while( 1 ){
  1306   1294       memAboutToChange(p, pOut);
  1307   1295       sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1308   1296       Deephemeralize(pOut);
  1309   1297   #ifdef SQLITE_DEBUG
  1310   1298       pOut->pScopyFrom = 0;
  1311         -    pOut->iTabColHash = 0;
  1312   1299   #endif
  1313   1300       REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
  1314   1301       if( (n--)==0 ) break;
  1315   1302       pOut++;
  1316   1303       pIn1++;
  1317   1304     }
  1318   1305     break;
................................................................................
  1921   1908   case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
  1922   1909   case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
  1923   1910     int res, res2;      /* Result of the comparison of pIn1 against pIn3 */
  1924   1911     char affinity;      /* Affinity to use for comparison */
  1925   1912     u16 flags1;         /* Copy of initial value of pIn1->flags */
  1926   1913     u16 flags3;         /* Copy of initial value of pIn3->flags */
  1927   1914   
         1915  +  /* The only way for P1 and P3 to be the same is when comparing constants.
         1916  +  ** But in that case, the affinities will always be SQLITE_AFF_BLOB or none */
         1917  +  assert( pOp->p1!=pOp->p3 || (pOp->p5 & SQLITE_AFF_MASK)<=SQLITE_AFF_BLOB );
         1918  +  testcase( pOp->p1==pOp->p3 );
         1919  +
  1928   1920     pIn1 = &aMem[pOp->p1];
  1929   1921     pIn3 = &aMem[pOp->p3];
  1930   1922     flags1 = pIn1->flags;
  1931   1923     flags3 = pIn3->flags;
  1932   1924     if( (flags1 | flags3)&MEM_Null ){
  1933   1925       /* One or both operands are NULL */
  1934   1926       if( pOp->p5 & SQLITE_NULLEQ ){
................................................................................
  1968   1960     }else{
  1969   1961       /* Neither operand is NULL.  Do a comparison. */
  1970   1962       affinity = pOp->p5 & SQLITE_AFF_MASK;
  1971   1963       if( affinity>=SQLITE_AFF_NUMERIC ){
  1972   1964         if( (flags1 | flags3)&MEM_Str ){
  1973   1965           if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
  1974   1966             applyNumericAffinity(pIn1,0);
  1975         -          testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
         1967  +          /* testcase( flags3!=pIn3->flags );
         1968  +          ** this used to be possible with pIn1==pIn3, but not since
         1969  +          ** the column cache was removed.  The following assignment
         1970  +          ** is essentially a no-op.  But, it prevents defense-in-depth
         1971  +          ** in case our analysis is incorrect, so it is left in. */
  1976   1972             flags3 = pIn3->flags;
  1977   1973           }
  1978   1974           if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
  1979   1975             applyNumericAffinity(pIn3,0);
  1980   1976           }
  1981   1977         }
  1982   1978         /* Handle the common case of integer comparison here, as an
................................................................................
  7440   7436   ** An Abort is safe if either there have been no writes, or if there is
  7441   7437   ** an active statement journal.
  7442   7438   */
  7443   7439   case OP_Abortable: {
  7444   7440     sqlite3VdbeAssertAbortable(p);
  7445   7441     break;
  7446   7442   }
  7447         -#endif
  7448         -
  7449         -#ifdef SQLITE_DEBUG_COLUMNCACHE
  7450         -/* Opcode:  SetTabCol   P1 P2 P3 * *
  7451         -**
  7452         -** Set a flag in register REG[P3] indicating that it holds the value
  7453         -** of column P2 from the table on cursor P1.  This flag is checked
  7454         -** by a subsequent VerifyTabCol opcode.
  7455         -**
  7456         -** This opcode only appears SQLITE_DEBUG builds.  It is used to verify
  7457         -** that the expression table column cache is working correctly.
  7458         -*/
  7459         -case OP_SetTabCol: {
  7460         -  aMem[pOp->p3].iTabColHash = TableColumnHash(pOp->p1,pOp->p2);
  7461         -  break;
  7462         -}
  7463         -/* Opcode:  VerifyTabCol   P1 P2 P3 * *
  7464         -**
  7465         -** Verify that register REG[P3] contains the value of column P2 from
  7466         -** cursor P1.  Assert() if this is not the case.
  7467         -**
  7468         -** This opcode only appears SQLITE_DEBUG builds.  It is used to verify
  7469         -** that the expression table column cache is working correctly.
  7470         -*/
  7471         -case OP_VerifyTabCol: {
  7472         -  assert( aMem[pOp->p3].iTabColHash == TableColumnHash(pOp->p1,pOp->p2) );
  7473         -  break;
  7474         -}
  7475   7443   #endif
  7476   7444   
  7477   7445   /* Opcode: Noop * * * * *
  7478   7446   **
  7479   7447   ** Do nothing.  This instruction is often useful as a jump
  7480   7448   ** destination.
  7481   7449   */

Changes to src/vdbeInt.h.

   207    207     u32 uTemp;          /* Transient storage for serial_type in OP_MakeRecord */
   208    208     sqlite3 *db;        /* The associated database connection */
   209    209     void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
   210    210   #ifdef SQLITE_DEBUG
   211    211     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   212    212     u16 mScopyFlags;    /* flags value immediately after the shallow copy */
   213    213   #endif
   214         -#ifdef SQLITE_DEBUG_COLUMNCACHE
   215         -  u32 iTabColHash;    /* Hash of table.column that is origin of this value */
   216         -  u32 iPadding;       /* sqlite3_value objects must be 8-byte aligned */
   217         -#endif
   218    214   };
   219    215   
   220    216   /*
   221    217   ** Size of struct Mem not including the Mem.zMalloc member or anything that
   222    218   ** follows.
   223    219   */
   224    220   #define MEMCELLSIZE offsetof(Mem,zMalloc)

Changes to src/vdbeapi.c.

   968    968           /* .uTemp      = */ (u32)0,
   969    969           /* .db         = */ (sqlite3*)0,
   970    970           /* .xDel       = */ (void(*)(void*))0,
   971    971   #ifdef SQLITE_DEBUG
   972    972           /* .pScopyFrom = */ (Mem*)0,
   973    973           /* .mScopyFlags= */ 0,
   974    974   #endif
   975         -#ifdef SQLITE_DEBUG_COLUMNCACHE
   976         -        /* .iTabColHash= */ 0,
   977         -#endif
   978    975         };
   979    976     return &nullMem;
   980    977   }
   981    978   
   982    979   /*
   983    980   ** Check to see if column iCol of the given statement is valid.  If
   984    981   ** it is, return a pointer to the Mem for the value of that column.

Changes to src/vdbeaux.c.

   189    189     pOp->p4.p = 0;
   190    190     pOp->p4type = P4_NOTUSED;
   191    191   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   192    192     pOp->zComment = 0;
   193    193   #endif
   194    194   #ifdef SQLITE_DEBUG
   195    195     if( p->db->flags & SQLITE_VdbeAddopTrace ){
   196         -    int jj, kk;
   197         -    Parse *pParse = p->pParse;
   198         -    for(jj=kk=0; jj<pParse->nColCache; jj++){
   199         -      struct yColCache *x = pParse->aColCache + jj;
   200         -      printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
   201         -      kk++;
   202         -    }
   203         -    if( kk ) printf("\n");
   204    196       sqlite3VdbePrintOp(0, i, &p->aOp[i]);
   205    197       test_addop_breakpoint();
   206    198     }
   207    199   #endif
   208    200   #ifdef VDBE_PROFILE
   209    201     pOp->cycles = 0;
   210    202     pOp->cnt = 0;
................................................................................
  1643   1635     while( (N--)>0 ){
  1644   1636       p->db = db;
  1645   1637       p->flags = flags;
  1646   1638       p->szMalloc = 0;
  1647   1639   #ifdef SQLITE_DEBUG
  1648   1640       p->pScopyFrom = 0;
  1649   1641   #endif
  1650         -#ifdef SQLITE_DEBUG_COLUMNCACHE
  1651         -    p->iTabColHash = 0;
  1652         -#endif
  1653   1642       p++;
  1654   1643     }
  1655   1644   }
  1656   1645   
  1657   1646   /*
  1658   1647   ** Release an array of N Mem elements
  1659   1648   */

Changes to src/vdbemem.c.

   930    930         /* pMem is the register that is changing.  But also mark pX as
   931    931         ** undefined so that we can quickly detect the shallow-copy error */
   932    932         pX->flags = MEM_Undefined;
   933    933         pX->pScopyFrom = 0;
   934    934       }
   935    935     }
   936    936     pMem->pScopyFrom = 0;
   937         -#ifdef SQLITE_DEBUG_COLUMN_CACHE
   938         -  pMem->iTabColHash = 0;
   939         -#endif
   940    937   }
   941    938   #endif /* SQLITE_DEBUG */
   942    939   
   943    940   
   944    941   /*
   945    942   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   946    943   ** pTo are freed.  The pFrom->z field is not duplicated.  If
................................................................................
   953    950     sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
   954    951   }
   955    952   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   956    953     assert( (pFrom->flags & MEM_RowSet)==0 );
   957    954     assert( pTo->db==pFrom->db );
   958    955     if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
   959    956     memcpy(pTo, pFrom, MEMCELLSIZE);
   960         -#ifdef SQLITE_DEBUG_COLUMNCACHE
   961         -  pTo->iTabColHash = pFrom->iTabColHash;
   962         -#endif
   963    957     if( (pFrom->flags&MEM_Static)==0 ){
   964    958       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   965    959       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   966    960       pTo->flags |= srcType;
   967    961     }
   968    962   }
   969    963   
................................................................................
   973    967   */
   974    968   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   975    969     int rc = SQLITE_OK;
   976    970   
   977    971     assert( (pFrom->flags & MEM_RowSet)==0 );
   978    972     if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
   979    973     memcpy(pTo, pFrom, MEMCELLSIZE);
   980         -#ifdef SQLITE_DEBUG_COLUMNCACHE
   981         -  pTo->iTabColHash = pFrom->iTabColHash;
   982         -#endif
   983    974     pTo->flags &= ~MEM_Dyn;
   984    975     if( pTo->flags&(MEM_Str|MEM_Blob) ){
   985    976       if( 0==(pFrom->flags&MEM_Static) ){
   986    977         pTo->flags |= MEM_Ephem;
   987    978         rc = sqlite3VdbeMemMakeWriteable(pTo);
   988    979       }
   989    980     }

Changes to src/where.c.

   798    798     assert( pLevel->iIdxCur>=0 );
   799    799     pLevel->iIdxCur = pParse->nTab++;
   800    800     sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
   801    801     sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
   802    802     VdbeComment((v, "for %s", pTable->zName));
   803    803   
   804    804     /* Fill the automatic index with content */
   805         -  sqlite3ExprCachePush(pParse);
   806    805     pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
   807    806     if( pTabItem->fg.viaCoroutine ){
   808    807       int regYield = pTabItem->regReturn;
   809    808       addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
   810    809       sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
   811    810       addrTop =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
   812    811       VdbeCoverage(v);
................................................................................
   835    834       pTabItem->fg.viaCoroutine = 0;
   836    835     }else{
   837    836       sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
   838    837     }
   839    838     sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
   840    839     sqlite3VdbeJumpHere(v, addrTop);
   841    840     sqlite3ReleaseTempReg(pParse, regRecord);
   842         -  sqlite3ExprCachePop(pParse);
   843    841     
   844    842     /* Jump here when skipping the initialization */
   845    843     sqlite3VdbeJumpHere(v, addrInit);
   846    844   
   847    845   end_auto_index_create:
   848    846     sqlite3ExprDelete(pParse->db, pPartial);
   849    847   }
................................................................................
  5073   5071     WhereLoop *pLoop;
  5074   5072     SrcList *pTabList = pWInfo->pTabList;
  5075   5073     sqlite3 *db = pParse->db;
  5076   5074   
  5077   5075     /* Generate loop termination code.
  5078   5076     */
  5079   5077     VdbeModuleComment((v, "End WHERE-core"));
  5080         -  sqlite3ExprCacheClear(pParse);
  5081   5078     for(i=pWInfo->nLevel-1; i>=0; i--){
  5082   5079       int addr;
  5083   5080       pLevel = &pWInfo->a[i];
  5084   5081       pLoop = pLevel->pWLoop;
  5085   5082       if( pLevel->op!=OP_Noop ){
  5086   5083   #ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
  5087   5084         int addrSeek = 0;

Changes to src/wherecode.c.

   343    343     while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
   344    344       n--;
   345    345     }
   346    346   
   347    347     /* Code the OP_Affinity opcode if there is anything left to do. */
   348    348     if( n>0 ){
   349    349       sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
   350         -    sqlite3ExprCacheAffinityChange(pParse, base, n);
   351    350     }
   352    351   }
   353    352   
   354    353   /*
   355    354   ** Expression pRight, which is the RHS of a comparison operation, is 
   356    355   ** either a vector of n elements or, if n==1, a scalar expression.
   357    356   ** Before the comparison operation, affinity zAff is to be applied
................................................................................
  1252   1251       **          to access the data.
  1253   1252       */
  1254   1253       int iReg;   /* P3 Value for OP_VFilter */
  1255   1254       int addrNotFound;
  1256   1255       int nConstraint = pLoop->nLTerm;
  1257   1256       int iIn;    /* Counter for IN constraints */
  1258   1257   
  1259         -    sqlite3ExprCachePush(pParse);
  1260   1258       iReg = sqlite3GetTempRange(pParse, nConstraint+2);
  1261   1259       addrNotFound = pLevel->addrBrk;
  1262   1260       for(j=0; j<nConstraint; j++){
  1263   1261         int iTarget = iReg+j+2;
  1264   1262         pTerm = pLoop->aLTerm[j];
  1265   1263         if( NEVER(pTerm==0) ) continue;
  1266   1264         if( pTerm->eOperator & WO_IN ){
................................................................................
  1325   1323       /* These registers need to be preserved in case there is an IN operator
  1326   1324       ** loop.  So we could deallocate the registers here (and potentially
  1327   1325       ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0.  But it seems
  1328   1326       ** simpler and safer to simply not reuse the registers.
  1329   1327       **
  1330   1328       **    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
  1331   1329       */
  1332         -    sqlite3ExprCachePop(pParse);
  1333   1330     }else
  1334   1331   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  1335   1332   
  1336   1333     if( (pLoop->wsFlags & WHERE_IPK)!=0
  1337   1334      && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
  1338   1335     ){
  1339   1336       /* Case 2:  We can directly reference a single row using an
................................................................................
  1418   1415         }
  1419   1416         sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
  1420   1417         VdbeComment((v, "pk"));
  1421   1418         VdbeCoverageIf(v, pX->op==TK_GT);
  1422   1419         VdbeCoverageIf(v, pX->op==TK_LE);
  1423   1420         VdbeCoverageIf(v, pX->op==TK_LT);
  1424   1421         VdbeCoverageIf(v, pX->op==TK_GE);
  1425         -      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
  1426   1422         sqlite3ReleaseTempReg(pParse, rTemp);
  1427   1423       }else{
  1428   1424         sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrHalt);
  1429   1425         VdbeCoverageIf(v, bRev==0);
  1430   1426         VdbeCoverageIf(v, bRev!=0);
  1431   1427       }
  1432   1428       if( pEnd ){
................................................................................
  1453   1449       pLevel->op = bRev ? OP_Prev : OP_Next;
  1454   1450       pLevel->p1 = iCur;
  1455   1451       pLevel->p2 = start;
  1456   1452       assert( pLevel->p5==0 );
  1457   1453       if( testOp!=OP_Noop ){
  1458   1454         iRowidReg = ++pParse->nMem;
  1459   1455         sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
  1460         -      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  1461   1456         sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
  1462   1457         VdbeCoverageIf(v, testOp==OP_Le);
  1463   1458         VdbeCoverageIf(v, testOp==OP_Lt);
  1464   1459         VdbeCoverageIf(v, testOp==OP_Ge);
  1465   1460         VdbeCoverageIf(v, testOp==OP_Gt);
  1466   1461         sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
  1467   1462       }
................................................................................
  1679   1674   
  1680   1675       /* Load the value for the inequality constraint at the end of the
  1681   1676       ** range (if any).
  1682   1677       */
  1683   1678       nConstraint = nEq;
  1684   1679       if( pRangeEnd ){
  1685   1680         Expr *pRight = pRangeEnd->pExpr->pRight;
  1686         -      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
  1687   1681         codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
  1688   1682         whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
  1689   1683         if( (pRangeEnd->wtFlags & TERM_VNULL)==0
  1690   1684          && sqlite3ExprCanBeNull(pRight)
  1691   1685         ){
  1692   1686           sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  1693   1687           VdbeCoverage(v);
................................................................................
  1704   1698         if( sqlite3ExprIsVector(pRight)==0 ){
  1705   1699           disableTerm(pLevel, pRangeEnd);
  1706   1700         }else{
  1707   1701           endEq = 1;
  1708   1702         }
  1709   1703       }else if( bStopAtNull ){
  1710   1704         sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
  1711         -      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
  1712   1705         endEq = 0;
  1713   1706         nConstraint++;
  1714   1707       }
  1715   1708       sqlite3DbFree(db, zStartAff);
  1716   1709       sqlite3DbFree(db, zEndAff);
  1717   1710   
  1718   1711       /* Top of the loop body */
................................................................................
  1738   1731       }else if( HasRowid(pIdx->pTable) ){
  1739   1732         if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
  1740   1733             (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE) 
  1741   1734          && (pWInfo->eOnePass==ONEPASS_SINGLE)
  1742   1735         )){
  1743   1736           iRowidReg = ++pParse->nMem;
  1744   1737           sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
  1745         -        sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  1746   1738           sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
  1747   1739           VdbeCoverage(v);
  1748   1740         }else{
  1749   1741           codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
  1750   1742         }
  1751   1743       }else if( iCur!=iIdxCur ){
  1752   1744         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
................................................................................
  1973   1965   
  1974   1966             /* This is the sub-WHERE clause body.  First skip over
  1975   1967             ** duplicate rows from prior sub-WHERE clauses, and record the
  1976   1968             ** rowid (or PRIMARY KEY) for the current row so that the same
  1977   1969             ** row will be skipped in subsequent sub-WHERE clauses.
  1978   1970             */
  1979   1971             if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  1980         -            int r;
  1981   1972               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
  1982   1973               if( HasRowid(pTab) ){
  1983         -              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
         1974  +              sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
  1984   1975                 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0,
  1985         -                                           r,iSet);
         1976  +                                          regRowid, iSet);
  1986   1977                 VdbeCoverage(v);
  1987   1978               }else{
  1988   1979                 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
  1989   1980                 int nPk = pPk->nKeyCol;
  1990   1981                 int iPk;
         1982  +              int r;
  1991   1983   
  1992   1984                 /* Read the PK into an array of temp registers. */
  1993   1985                 r = sqlite3GetTempRange(pParse, nPk);
  1994   1986                 for(iPk=0; iPk<nPk; iPk++){
  1995   1987                   int iCol = pPk->aiColumn[iPk];
  1996         -                sqlite3ExprCodeGetColumnToReg(pParse, pTab, iCol, iCur, r+iPk);
         1988  +                sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, r+iPk);
  1997   1989                 }
  1998   1990   
  1999   1991                 /* Check if the temp table already contains this key. If so,
  2000   1992                 ** the row has already been included in the result set and
  2001   1993                 ** can be ignored (by jumping past the Gosub below). Otherwise,
  2002   1994                 ** insert the key into the temp table and proceed with processing
  2003   1995                 ** the row.
................................................................................
  2222   2214     /* For a LEFT OUTER JOIN, generate code that will record the fact that
  2223   2215     ** at least one row of the right table has matched the left table.  
  2224   2216     */
  2225   2217     if( pLevel->iLeftJoin ){
  2226   2218       pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
  2227   2219       sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
  2228   2220       VdbeComment((v, "record LEFT JOIN hit"));
  2229         -    sqlite3ExprCacheClear(pParse);
  2230   2221       for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
  2231   2222         testcase( pTerm->wtFlags & TERM_VIRTUAL );
  2232   2223         testcase( pTerm->wtFlags & TERM_CODED );
  2233   2224         if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  2234   2225         if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
  2235   2226           assert( pWInfo->untestedTerms );
  2236   2227           continue;

Changes to test/btree02.test.

    29     29       INSERT INTO t3(cnt) SELECT i FROM c;
    30     30     SELECT count(*) FROM t1;
    31     31   } {10}
    32     32   do_test btree02-110 {
    33     33     db eval BEGIN
    34     34     set i 0
    35     35     db eval {SELECT a, ax, b, cnt FROM t1 CROSS JOIN t3 WHERE b IS NOT NULL} {
           36  +    if {$a==""} {set a 0}
           37  +    if {$b==""} {set b 0}    
    36     38       db eval {INSERT INTO t2(x,y) VALUES($b,$cnt)}
    37     39       # puts "a,b,cnt = ($a,$b,$cnt)"
    38     40       incr i
    39     41       if {$i%2==1} {
    40     42         set bx [expr {$b+1000}]
    41     43         # puts "INSERT ($a),$bx"
    42     44         db eval {INSERT INTO t1(a,ax,b) VALUES(printf('(%s)',$a),random(),$bx)}
................................................................................
    43     45       } else {
    44     46         # puts "DELETE a=$a"
    45     47         db eval {DELETE FROM t1 WHERE a=$a}
    46     48       }
    47     49       db eval {COMMIT; BEGIN}
    48     50     }  
    49     51     db one {COMMIT; SELECT count(*) FROM t1;}
    50         -} {20}
           52  +} {27}
    51     53   
    52     54   finish_test