/ Check-in [2078454a]
Login

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

Overview
Comment:Merge the latest enhancements from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 2078454ac998ccb5e837c4f13d8d8b2f312f4f50
User & Date: drh 2014-10-01 01:46:35
Context
2014-10-09
11:27
Merge the latest trunk enhancements into the apple-osx branch. check-in: 6fc72077 user: drh tags: apple-osx
2014-10-01
01:46
Merge the latest enhancements from trunk. check-in: 2078454a user: drh tags: apple-osx
2014-09-30
19:04
Improvements to the new syntax-tree output routines: Omit the "END SELECT" mark and instead terminate the graph at the last item. Increase the maximum tree depth to 100. check-in: 5ce05757 user: drh tags: trunk
2014-09-21
23:08
Merge in all recent changes from trunk. check-in: 3967ebe8 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

   489    489     i64 iRow,               /* The rowid that might be changing */
   490    490     int isClearTable        /* True if all rows are being deleted */
   491    491   ){
   492    492     BtCursor *p;
   493    493     BtShared *pBt = pBtree->pBt;
   494    494     assert( sqlite3BtreeHoldsMutex(pBtree) );
   495    495     for(p=pBt->pCursor; p; p=p->pNext){
   496         -    if( (p->curFlags & BTCF_Incrblob)!=0 && (isClearTable || p->info.nKey==iRow) ){
          496  +    if( (p->curFlags & BTCF_Incrblob)!=0
          497  +     && (isClearTable || p->info.nKey==iRow)
          498  +    ){
   497    499         p->eState = CURSOR_INVALID;
   498    500       }
   499    501     }
   500    502   }
   501    503   
   502    504   #else
   503    505     /* Stub function when INCRBLOB is omitted */
................................................................................
   662    664   
   663    665   /* This helper routine to saveAllCursors does the actual work of saving
   664    666   ** the cursors if and when a cursor is found that actually requires saving.
   665    667   ** The common case is that no cursors need to be saved, so this routine is
   666    668   ** broken out from its caller to avoid unnecessary stack pointer movement.
   667    669   */
   668    670   static int SQLITE_NOINLINE saveCursorsOnList(
   669         -  BtCursor *p,           /* The first cursor that needs saving */
   670         -  Pgno iRoot,            /* Only save cursor with this iRoot.  Save all if zero */
   671         -  BtCursor *pExcept      /* Do not save this cursor */
          671  +  BtCursor *p,         /* The first cursor that needs saving */
          672  +  Pgno iRoot,          /* Only save cursor with this iRoot. Save all if zero */
          673  +  BtCursor *pExcept    /* Do not save this cursor */
   672    674   ){
   673    675     do{
   674    676       if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
   675    677         if( p->eState==CURSOR_VALID ){
   676    678           int rc = saveCursorPosition(p);
   677    679           if( SQLITE_OK!=rc ){
   678    680             return rc;
................................................................................
   970    972   }
   971    973   
   972    974   /*
   973    975   ** Parse a cell content block and fill in the CellInfo structure.  There
   974    976   ** are two versions of this function.  btreeParseCell() takes a 
   975    977   ** cell index as the second argument and btreeParseCellPtr() 
   976    978   ** takes a pointer to the body of the cell as its second argument.
   977         -**
   978         -** Within this file, the parseCell() macro can be called instead of
   979         -** btreeParseCellPtr(). Using some compilers, this will be faster.
   980    979   */
   981    980   static void btreeParseCellPtr(
   982    981     MemPage *pPage,         /* Page containing the cell */
   983    982     u8 *pCell,              /* Pointer to the cell text. */
   984    983     CellInfo *pInfo         /* Fill in this structure */
   985    984   ){
   986         -  u16 n;                  /* Number bytes in cell content header */
          985  +  u8 *pIter;              /* For scanning through pCell */
   987    986     u32 nPayload;           /* Number of bytes of cell payload */
   988    987   
   989    988     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   990         -
   991         -  pInfo->pCell = pCell;
   992    989     assert( pPage->leaf==0 || pPage->leaf==1 );
   993         -  n = pPage->childPtrSize;
   994         -  assert( n==4-4*pPage->leaf );
   995         -  if( pPage->intKey ){
   996         -    if( pPage->hasData ){
   997         -      assert( n==0 );
   998         -      n = getVarint32(pCell, nPayload);
   999         -    }else{
  1000         -      nPayload = 0;
  1001         -    }
  1002         -    n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
  1003         -    pInfo->nData = nPayload;
          990  +  if( pPage->intKeyLeaf ){
          991  +    assert( pPage->childPtrSize==0 );
          992  +    pIter = pCell + getVarint32(pCell, nPayload);
          993  +    pIter += getVarint(pIter, (u64*)&pInfo->nKey);
          994  +  }else if( pPage->noPayload ){
          995  +    assert( pPage->childPtrSize==4 );
          996  +    pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
          997  +    pInfo->nPayload = 0;
          998  +    pInfo->nLocal = 0;
          999  +    pInfo->iOverflow = 0;
         1000  +    pInfo->pPayload = 0;
         1001  +    return;
  1004   1002     }else{
  1005         -    pInfo->nData = 0;
  1006         -    n += getVarint32(&pCell[n], nPayload);
         1003  +    pIter = pCell + pPage->childPtrSize;
         1004  +    pIter += getVarint32(pIter, nPayload);
  1007   1005       pInfo->nKey = nPayload;
  1008   1006     }
  1009   1007     pInfo->nPayload = nPayload;
  1010         -  pInfo->nHeader = n;
         1008  +  pInfo->pPayload = pIter;
  1011   1009     testcase( nPayload==pPage->maxLocal );
  1012   1010     testcase( nPayload==pPage->maxLocal+1 );
  1013         -  if( likely(nPayload<=pPage->maxLocal) ){
         1011  +  if( nPayload<=pPage->maxLocal ){
  1014   1012       /* This is the (easy) common case where the entire payload fits
  1015   1013       ** on the local page.  No overflow is required.
  1016   1014       */
  1017         -    if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
         1015  +    pInfo->nSize = nPayload + (u16)(pIter - pCell);
         1016  +    if( pInfo->nSize<4 ) pInfo->nSize = 4;
  1018   1017       pInfo->nLocal = (u16)nPayload;
  1019   1018       pInfo->iOverflow = 0;
  1020   1019     }else{
  1021   1020       /* If the payload will not fit completely on the local page, we have
  1022   1021       ** to decide how much to store locally and how much to spill onto
  1023   1022       ** overflow pages.  The strategy is to minimize the amount of unused
  1024   1023       ** space on overflow pages while keeping the amount of local storage
................................................................................
  1037   1036       testcase( surplus==maxLocal );
  1038   1037       testcase( surplus==maxLocal+1 );
  1039   1038       if( surplus <= maxLocal ){
  1040   1039         pInfo->nLocal = (u16)surplus;
  1041   1040       }else{
  1042   1041         pInfo->nLocal = (u16)minLocal;
  1043   1042       }
  1044         -    pInfo->iOverflow = (u16)(pInfo->nLocal + n);
         1043  +    pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
  1045   1044       pInfo->nSize = pInfo->iOverflow + 4;
  1046   1045     }
  1047   1046   }
  1048         -#define parseCell(pPage, iCell, pInfo) \
  1049         -  btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
  1050   1047   static void btreeParseCell(
  1051   1048     MemPage *pPage,         /* Page containing the cell */
  1052   1049     int iCell,              /* The cell index.  First cell is 0 */
  1053   1050     CellInfo *pInfo         /* Fill in this structure */
  1054   1051   ){
  1055         -  parseCell(pPage, iCell, pInfo);
         1052  +  btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo);
  1056   1053   }
  1057   1054   
  1058   1055   /*
  1059   1056   ** Compute the total number of bytes that a Cell needs in the cell
  1060   1057   ** data area of the btree-page.  The return number includes the cell
  1061   1058   ** data header and the local payload, but not any overflow page or
  1062   1059   ** the space used by the cell pointer.
  1063   1060   */
  1064   1061   static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
  1065         -  u8 *pIter = &pCell[pPage->childPtrSize];
  1066         -  u32 nSize;
         1062  +  u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
         1063  +  u8 *pEnd;                                /* End mark for a varint */
         1064  +  u32 nSize;                               /* Size value to return */
  1067   1065   
  1068   1066   #ifdef SQLITE_DEBUG
  1069   1067     /* The value returned by this function should always be the same as
  1070   1068     ** the (CellInfo.nSize) value found by doing a full parse of the
  1071   1069     ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
  1072   1070     ** this function verifies that this invariant is not violated. */
  1073   1071     CellInfo debuginfo;
  1074   1072     btreeParseCellPtr(pPage, pCell, &debuginfo);
  1075   1073   #endif
  1076   1074   
         1075  +  if( pPage->noPayload ){
         1076  +    pEnd = &pIter[9];
         1077  +    while( (*pIter++)&0x80 && pIter<pEnd );
         1078  +    assert( pPage->childPtrSize==4 );
         1079  +    return (u16)(pIter - pCell);
         1080  +  }
         1081  +  nSize = *pIter;
         1082  +  if( nSize>=0x80 ){
         1083  +    pEnd = &pIter[9];
         1084  +    nSize &= 0x7f;
         1085  +    do{
         1086  +      nSize = (nSize<<7) | (*++pIter & 0x7f);
         1087  +    }while( *(pIter)>=0x80 && pIter<pEnd );
         1088  +  }
         1089  +  pIter++;
  1077   1090     if( pPage->intKey ){
  1078         -    u8 *pEnd;
  1079         -    if( pPage->hasData ){
  1080         -      pIter += getVarint32(pIter, nSize);
  1081         -    }else{
  1082         -      nSize = 0;
  1083         -    }
  1084         -
  1085   1091       /* pIter now points at the 64-bit integer key value, a variable length 
  1086   1092       ** integer. The following block moves pIter to point at the first byte
  1087   1093       ** past the end of the key value. */
  1088   1094       pEnd = &pIter[9];
  1089   1095       while( (*pIter++)&0x80 && pIter<pEnd );
  1090         -  }else{
  1091         -    pIter += getVarint32(pIter, nSize);
  1092   1096     }
  1093         -
  1094   1097     testcase( nSize==pPage->maxLocal );
  1095   1098     testcase( nSize==pPage->maxLocal+1 );
  1096         -  if( nSize>pPage->maxLocal ){
         1099  +  if( nSize<=pPage->maxLocal ){
         1100  +    nSize += (u32)(pIter - pCell);
         1101  +    if( nSize<4 ) nSize = 4;
         1102  +  }else{
  1097   1103       int minLocal = pPage->minLocal;
  1098   1104       nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
  1099   1105       testcase( nSize==pPage->maxLocal );
  1100   1106       testcase( nSize==pPage->maxLocal+1 );
  1101   1107       if( nSize>pPage->maxLocal ){
  1102   1108         nSize = minLocal;
  1103   1109       }
  1104         -    nSize += 4;
         1110  +    nSize += 4 + (u16)(pIter - pCell);
  1105   1111     }
  1106         -  nSize += (u32)(pIter - pCell);
  1107         -
  1108         -  /* The minimum size of any cell is 4 bytes. */
  1109         -  if( nSize<4 ){
  1110         -    nSize = 4;
  1111         -  }
  1112         -
  1113         -  assert( nSize==debuginfo.nSize );
         1112  +  assert( nSize==debuginfo.nSize || CORRUPT_DB );
  1114   1113     return (u16)nSize;
  1115   1114   }
  1116   1115   
  1117   1116   #ifdef SQLITE_DEBUG
  1118   1117   /* This variation on cellSizePtr() is used inside of assert() statements
  1119   1118   ** only. */
  1120   1119   static u16 cellSize(MemPage *pPage, int iCell){
................................................................................
  1129   1128   ** for the overflow page.
  1130   1129   */
  1131   1130   static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
  1132   1131     CellInfo info;
  1133   1132     if( *pRC ) return;
  1134   1133     assert( pCell!=0 );
  1135   1134     btreeParseCellPtr(pPage, pCell, &info);
  1136         -  assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
  1137   1135     if( info.iOverflow ){
  1138   1136       Pgno ovfl = get4byte(&pCell[info.iOverflow]);
  1139   1137       ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
  1140   1138     }
  1141   1139   }
  1142   1140   #endif
  1143   1141   
................................................................................
  1342   1340   ** Note that even though the freeblock list was checked by btreeInitPage(),
  1343   1341   ** that routine will not detect overlap between cells or freeblocks.  Nor
  1344   1342   ** does it detect cells or freeblocks that encrouch into the reserved bytes
  1345   1343   ** at the end of the page.  So do additional corruption checks inside this
  1346   1344   ** routine and return SQLITE_CORRUPT if any problems are found.
  1347   1345   */
  1348   1346   static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
  1349         -  u16 iPtr;                             /* Address of pointer to next freeblock */
         1347  +  u16 iPtr;                             /* Address of ptr to next freeblock */
  1350   1348     u16 iFreeBlk;                         /* Address of the next freeblock */
  1351   1349     u8 hdr;                               /* Page header size.  0 or 100 */
  1352   1350     u8 nFrag = 0;                         /* Reduction in fragmentation */
  1353   1351     u16 iOrigSize = iSize;                /* Original value of iSize */
  1354   1352     u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
  1355   1353     u32 iEnd = iStart + iSize;            /* First byte past the iStart buffer */
  1356   1354     unsigned char *data = pPage->aData;   /* Page content */
................................................................................
  1394   1392         nFrag = iFreeBlk - iEnd;
  1395   1393         if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
  1396   1394         iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
  1397   1395         iSize = iEnd - iStart;
  1398   1396         iFreeBlk = get2byte(&data[iFreeBlk]);
  1399   1397       }
  1400   1398     
  1401         -    /* If iPtr is another freeblock (that is, if iPtr is not the freelist pointer
  1402         -    ** in the page header) then check to see if iStart should be coalesced 
  1403         -    ** onto the end of iPtr.
         1399  +    /* If iPtr is another freeblock (that is, if iPtr is not the freelist
         1400  +    ** pointer in the page header) then check to see if iStart should be
         1401  +    ** coalesced onto the end of iPtr.
  1404   1402       */
  1405   1403       if( iPtr>hdr+1 ){
  1406   1404         int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
  1407   1405         if( iPtrEnd+3>=iStart ){
  1408   1406           if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
  1409   1407           nFrag += iStart - iPtrEnd;
  1410   1408           iSize = iEnd - iPtr;
................................................................................
  1450   1448     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1451   1449     pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
  1452   1450     flagByte &= ~PTF_LEAF;
  1453   1451     pPage->childPtrSize = 4-4*pPage->leaf;
  1454   1452     pBt = pPage->pBt;
  1455   1453     if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
  1456   1454       pPage->intKey = 1;
  1457         -    pPage->hasData = pPage->leaf;
         1455  +    pPage->intKeyLeaf = pPage->leaf;
         1456  +    pPage->noPayload = !pPage->leaf;
  1458   1457       pPage->maxLocal = pBt->maxLeaf;
  1459   1458       pPage->minLocal = pBt->minLeaf;
  1460   1459     }else if( flagByte==PTF_ZERODATA ){
  1461   1460       pPage->intKey = 0;
  1462         -    pPage->hasData = 0;
         1461  +    pPage->intKeyLeaf = 0;
         1462  +    pPage->noPayload = 0;
  1463   1463       pPage->maxLocal = pBt->maxLocal;
  1464   1464       pPage->minLocal = pBt->minLocal;
  1465   1465     }else{
  1466   1466       return SQLITE_CORRUPT_BKPT;
  1467   1467     }
  1468   1468     pPage->max1bytePayload = pBt->max1bytePayload;
  1469   1469     return SQLITE_OK;
................................................................................
  2650   2650   **
  2651   2651   ** If there is a transaction in progress, this routine is a no-op.
  2652   2652   */
  2653   2653   static void unlockBtreeIfUnused(BtShared *pBt){
  2654   2654     assert( sqlite3_mutex_held(pBt->mutex) );
  2655   2655     assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
  2656   2656     if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
  2657         -    assert( pBt->pPage1->aData );
         2657  +    MemPage *pPage1 = pBt->pPage1;
         2658  +    assert( pPage1->aData );
  2658   2659       assert( sqlite3PagerRefcount(pBt->pPager)==1 );
  2659         -    assert( pBt->pPage1->aData );
  2660         -    releasePage(pBt->pPage1);
  2661   2660       pBt->pPage1 = 0;
         2661  +    releasePage(pPage1);
  2662   2662     }
  2663   2663   }
  2664   2664   
  2665   2665   /*
  2666   2666   ** If pBt points to an empty file then convert that empty file
  2667   2667   ** into a new empty database by initializing the first page of
  2668   2668   ** the database.
................................................................................
  3695   3695     assert( p->inTrans>TRANS_NONE );
  3696   3696     assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
  3697   3697     assert( pBt->pPage1 && pBt->pPage1->aData );
  3698   3698   
  3699   3699     if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
  3700   3700       return SQLITE_READONLY;
  3701   3701     }
         3702  +  if( wrFlag ){
         3703  +    allocateTempSpace(pBt);
         3704  +    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
         3705  +  }
  3702   3706     if( iTable==1 && btreePagecount(pBt)==0 ){
  3703   3707       assert( wrFlag==0 );
  3704   3708       iTable = 0;
  3705   3709     }
  3706   3710   
  3707   3711     /* Now that no other errors can occur, finish filling in the BtCursor
  3708   3712     ** variables and link the cursor into the BtShared list.  */
................................................................................
  3884   3888   ** Failure is not possible.  This function always returns SQLITE_OK.
  3885   3889   ** It might just as well be a procedure (returning void) but we continue
  3886   3890   ** to return an integer result code for historical reasons.
  3887   3891   */
  3888   3892   int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
  3889   3893     assert( cursorHoldsMutex(pCur) );
  3890   3894     assert( pCur->eState==CURSOR_VALID );
         3895  +  assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
  3891   3896     getCellInfo(pCur);
  3892         -  *pSize = pCur->info.nData;
         3897  +  *pSize = pCur->info.nPayload;
  3893   3898     return SQLITE_OK;
  3894   3899   }
  3895   3900   
  3896   3901   /*
  3897   3902   ** Given the page number of an overflow page in the database (parameter
  3898   3903   ** ovfl), this function finds the page number of the next page in the 
  3899   3904   ** linked list of overflow pages. If possible, it uses the auto-vacuum
................................................................................
  4036   4041     u32 offset,          /* Begin reading this far into payload */
  4037   4042     u32 amt,             /* Read this many bytes */
  4038   4043     unsigned char *pBuf, /* Write the bytes into this buffer */ 
  4039   4044     int eOp              /* zero to read. non-zero to write. */
  4040   4045   ){
  4041   4046     unsigned char *aPayload;
  4042   4047     int rc = SQLITE_OK;
  4043         -  u32 nKey;
  4044   4048     int iIdx = 0;
  4045   4049     MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
  4046   4050     BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
  4047   4051   #ifdef SQLITE_DIRECT_OVERFLOW_READ
  4048         -  int bEnd;                                   /* True if reading to end of data */
         4052  +  int bEnd;                                 /* True if reading to end of data */
  4049   4053   #endif
  4050   4054   
  4051   4055     assert( pPage );
  4052   4056     assert( pCur->eState==CURSOR_VALID );
  4053   4057     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  4054   4058     assert( cursorHoldsMutex(pCur) );
  4055         -  assert( eOp!=2 || offset==0 );      /* Always start from beginning for eOp==2 */
         4059  +  assert( eOp!=2 || offset==0 );    /* Always start from beginning for eOp==2 */
  4056   4060   
  4057   4061     getCellInfo(pCur);
  4058         -  aPayload = pCur->info.pCell + pCur->info.nHeader;
  4059         -  nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
         4062  +  aPayload = pCur->info.pPayload;
  4060   4063   #ifdef SQLITE_DIRECT_OVERFLOW_READ
  4061         -  bEnd = (offset+amt==nKey+pCur->info.nData);
         4064  +  bEnd = offset+amt==pCur->info.nPayload;
  4062   4065   #endif
         4066  +  assert( offset+amt <= pCur->info.nPayload );
  4063   4067   
  4064         -  if( NEVER(offset+amt > nKey+pCur->info.nData) 
  4065         -   || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
  4066         -  ){
         4068  +  if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){
  4067   4069       /* Trying to read or write past the end of the data is an error */
  4068   4070       return SQLITE_CORRUPT_BKPT;
  4069   4071     }
  4070   4072   
  4071   4073     /* Check if data must be read/written to/from the btree page itself. */
  4072   4074     if( offset<pCur->info.nLocal ){
  4073   4075       int a = amt;
................................................................................
  4115   4117         }
  4116   4118       }
  4117   4119   
  4118   4120       /* If the overflow page-list cache has been allocated and the
  4119   4121       ** entry for the first required overflow page is valid, skip
  4120   4122       ** directly to it.
  4121   4123       */
  4122         -    if( (pCur->curFlags & BTCF_ValidOvfl)!=0 && pCur->aOverflow[offset/ovflSize] ){
         4124  +    if( (pCur->curFlags & BTCF_ValidOvfl)!=0
         4125  +     && pCur->aOverflow[offset/ovflSize]
         4126  +    ){
  4123   4127         iIdx = (offset/ovflSize);
  4124   4128         nextPage = pCur->aOverflow[iIdx];
  4125   4129         offset = (offset%ovflSize);
  4126   4130       }
  4127   4131   
  4128   4132       for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
  4129   4133   
................................................................................
  4293   4297     assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
  4294   4298     assert( pCur->eState==CURSOR_VALID );
  4295   4299     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4296   4300     assert( cursorHoldsMutex(pCur) );
  4297   4301     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
  4298   4302     assert( pCur->info.nSize>0 );
  4299   4303     *pAmt = pCur->info.nLocal;
  4300         -  return (void*)(pCur->info.pCell + pCur->info.nHeader);
         4304  +  return (void*)pCur->info.pPayload;
  4301   4305   }
  4302   4306   
  4303   4307   
  4304   4308   /*
  4305   4309   ** For the entry that cursor pCur is point to, return as
  4306   4310   ** many bytes of the key or data as are available on the local
  4307   4311   ** b-tree page.  Write the number of available bytes into *pAmt.
................................................................................
  4721   4725       assert( biasRight==0 || biasRight==1 );
  4722   4726       idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
  4723   4727       pCur->aiIdx[pCur->iPage] = (u16)idx;
  4724   4728       if( xRecordCompare==0 ){
  4725   4729         for(;;){
  4726   4730           i64 nCellKey;
  4727   4731           pCell = findCell(pPage, idx) + pPage->childPtrSize;
  4728         -        if( pPage->hasData ){
         4732  +        if( pPage->intKeyLeaf ){
  4729   4733             while( 0x80 <= *(pCell++) ){
  4730   4734               if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
  4731   4735             }
  4732   4736           }
  4733   4737           getVarint(pCell, (u64*)&nCellKey);
  4734   4738           if( nCellKey<intKey ){
  4735   4739             lwr = idx+1;
................................................................................
  4980   4984   ** Step the cursor to the back to the previous entry in the database.  If
  4981   4985   ** successful then set *pRes=0.  If the cursor
  4982   4986   ** was already pointing to the first entry in the database before
  4983   4987   ** this routine was called, then set *pRes=1.
  4984   4988   **
  4985   4989   ** The main entry point is sqlite3BtreePrevious().  That routine is optimized
  4986   4990   ** for the common case of merely decrementing the cell counter BtCursor.aiIdx
  4987         -** to the previous cell on the current page.  The (slower) btreePrevious() helper
  4988         -** routine is called when it is necessary to move to a different page or
  4989         -** to restore the cursor.
         4991  +** to the previous cell on the current page.  The (slower) btreePrevious()
         4992  +** helper routine is called when it is necessary to move to a different page
         4993  +** or to restore the cursor.
  4990   4994   **
  4991   4995   ** The calling function will set *pRes to 0 or 1.  The initial *pRes value
  4992   4996   ** will be 1 if the cursor being stepped corresponds to an SQL index and
  4993   4997   ** if this routine could have been skipped if that SQL index had been
  4994   4998   ** a unique index.  Otherwise the caller will have set *pRes to zero.
  4995   4999   ** Zero is the common case. The btree implementation is free to use the
  4996   5000   ** initial *pRes value as a hint to improve performance, but the current
................................................................................
  5004   5008     assert( cursorHoldsMutex(pCur) );
  5005   5009     assert( pRes!=0 );
  5006   5010     assert( *pRes==0 );
  5007   5011     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  5008   5012     assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
  5009   5013     assert( pCur->info.nSize==0 );
  5010   5014     if( pCur->eState!=CURSOR_VALID ){
  5011         -    assert( pCur->eState>=CURSOR_REQUIRESEEK );
  5012         -    rc = btreeRestoreCursorPosition(pCur);
         5015  +    rc = restoreCursorPosition(pCur);
  5013   5016       if( rc!=SQLITE_OK ){
  5014   5017         return rc;
  5015   5018       }
  5016   5019       if( CURSOR_INVALID==pCur->eState ){
  5017   5020         *pRes = 1;
  5018   5021         return SQLITE_OK;
  5019   5022       }
................................................................................
  5310   5313                    *pPgno, closest+1, k, pTrunk->pgno, n-1));
  5311   5314             rc = sqlite3PagerWrite(pTrunk->pDbPage);
  5312   5315             if( rc ) goto end_allocate_page;
  5313   5316             if( closest<k-1 ){
  5314   5317               memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
  5315   5318             }
  5316   5319             put4byte(&aData[4], k-1);
  5317         -          noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0;
         5320  +          noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
  5318   5321             rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
  5319   5322             if( rc==SQLITE_OK ){
  5320   5323               rc = sqlite3PagerWrite((*ppPage)->pDbPage);
  5321   5324               if( rc!=SQLITE_OK ){
  5322   5325                 releasePage(*ppPage);
  5323   5326               }
  5324   5327             }
................................................................................
  5343   5346       **
  5344   5347       ** Note that the pager will not actually attempt to load or journal 
  5345   5348       ** content for any page that really does lie past the end of the database
  5346   5349       ** file on disk. So the effects of disabling the no-content optimization
  5347   5350       ** here are confined to those pages that lie between the end of the
  5348   5351       ** database image and the end of the database file.
  5349   5352       */
  5350         -    int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0;
         5353  +    int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
  5351   5354   
  5352   5355       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
  5353   5356       if( rc ) return rc;
  5354   5357       pBt->nPage++;
  5355   5358       if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
  5356   5359   
  5357   5360   #ifndef SQLITE_OMIT_AUTOVACUUM
................................................................................
  5542   5545   static void freePage(MemPage *pPage, int *pRC){
  5543   5546     if( (*pRC)==SQLITE_OK ){
  5544   5547       *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
  5545   5548     }
  5546   5549   }
  5547   5550   
  5548   5551   /*
  5549         -** Free any overflow pages associated with the given Cell.
         5552  +** Free any overflow pages associated with the given Cell.  Write the
         5553  +** local Cell size (the number of bytes on the original page, omitting
         5554  +** overflow) into *pnSize.
  5550   5555   */
  5551         -static int clearCell(MemPage *pPage, unsigned char *pCell){
         5556  +static int clearCell(
         5557  +  MemPage *pPage,          /* The page that contains the Cell */
         5558  +  unsigned char *pCell,    /* First byte of the Cell */
         5559  +  u16 *pnSize              /* Write the size of the Cell here */
         5560  +){
  5552   5561     BtShared *pBt = pPage->pBt;
  5553   5562     CellInfo info;
  5554   5563     Pgno ovflPgno;
  5555   5564     int rc;
  5556   5565     int nOvfl;
  5557   5566     u32 ovflPageSize;
  5558   5567   
  5559   5568     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  5560   5569     btreeParseCellPtr(pPage, pCell, &info);
         5570  +  *pnSize = info.nSize;
  5561   5571     if( info.iOverflow==0 ){
  5562   5572       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
  5563   5573     }
  5564   5574     if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
  5565   5575       return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
  5566   5576     }
  5567   5577     ovflPgno = get4byte(&pCell[info.iOverflow]);
................................................................................
  5637   5647     MemPage *pOvfl = 0;
  5638   5648     MemPage *pToRelease = 0;
  5639   5649     unsigned char *pPrior;
  5640   5650     unsigned char *pPayload;
  5641   5651     BtShared *pBt = pPage->pBt;
  5642   5652     Pgno pgnoOvfl = 0;
  5643   5653     int nHeader;
  5644         -  CellInfo info;
  5645   5654   
  5646   5655     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  5647   5656   
  5648   5657     /* pPage is not necessarily writeable since pCell might be auxiliary
  5649   5658     ** buffer space that is separate from the pPage buffer area */
  5650   5659     assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
  5651   5660               || sqlite3PagerIswriteable(pPage->pDbPage) );
  5652   5661   
  5653   5662     /* Fill in the header. */
  5654         -  nHeader = 0;
  5655         -  if( !pPage->leaf ){
  5656         -    nHeader += 4;
  5657         -  }
  5658         -  if( pPage->hasData ){
  5659         -    nHeader += putVarint32(&pCell[nHeader], nData+nZero);
         5663  +  nHeader = pPage->childPtrSize;
         5664  +  nPayload = nData + nZero;
         5665  +  if( pPage->intKeyLeaf ){
         5666  +    nHeader += putVarint32(&pCell[nHeader], nPayload);
  5660   5667     }else{
  5661         -    nData = nZero = 0;
         5668  +    assert( nData==0 );
         5669  +    assert( nZero==0 );
  5662   5670     }
  5663   5671     nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
  5664         -  btreeParseCellPtr(pPage, pCell, &info);
  5665         -  assert( info.nHeader==nHeader );
  5666         -  assert( info.nKey==nKey );
  5667         -  assert( info.nData==(u32)(nData+nZero) );
  5668   5672     
  5669         -  /* Fill in the payload */
  5670         -  nPayload = nData + nZero;
         5673  +  /* Fill in the payload size */
  5671   5674     if( pPage->intKey ){
  5672   5675       pSrc = pData;
  5673   5676       nSrc = nData;
  5674   5677       nData = 0;
  5675   5678     }else{ 
  5676   5679       if( NEVER(nKey>0x7fffffff || pKey==0) ){
  5677   5680         return SQLITE_CORRUPT_BKPT;
  5678   5681       }
  5679         -    nPayload += (int)nKey;
         5682  +    nPayload = (int)nKey;
  5680   5683       pSrc = pKey;
  5681   5684       nSrc = (int)nKey;
  5682   5685     }
  5683         -  *pnSize = info.nSize;
  5684         -  spaceLeft = info.nLocal;
         5686  +  if( nPayload<=pPage->maxLocal ){
         5687  +    n = nHeader + nPayload;
         5688  +    testcase( n==3 );
         5689  +    testcase( n==4 );
         5690  +    if( n<4 ) n = 4;
         5691  +    *pnSize = n;
         5692  +    spaceLeft = nPayload;
         5693  +    pPrior = pCell;
         5694  +  }else{
         5695  +    int mn = pPage->minLocal;
         5696  +    n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
         5697  +    testcase( n==pPage->maxLocal );
         5698  +    testcase( n==pPage->maxLocal+1 );
         5699  +    if( n > pPage->maxLocal ) n = mn;
         5700  +    spaceLeft = n;
         5701  +    *pnSize = n + nHeader + 4;
         5702  +    pPrior = &pCell[nHeader+n];
         5703  +  }
  5685   5704     pPayload = &pCell[nHeader];
  5686         -  pPrior = &pCell[info.iOverflow];
  5687   5705   
         5706  +  /* At this point variables should be set as follows:
         5707  +  **
         5708  +  **   nPayload           Total payload size in bytes
         5709  +  **   pPayload           Begin writing payload here
         5710  +  **   spaceLeft          Space available at pPayload.  If nPayload>spaceLeft,
         5711  +  **                      that means content must spill into overflow pages.
         5712  +  **   *pnSize            Size of the local cell (not counting overflow pages)
         5713  +  **   pPrior             Where to write the pgno of the first overflow page
         5714  +  **
         5715  +  ** Use a call to btreeParseCellPtr() to verify that the values above
         5716  +  ** were computed correctly.
         5717  +  */
         5718  +#if SQLITE_DEBUG
         5719  +  {
         5720  +    CellInfo info;
         5721  +    btreeParseCellPtr(pPage, pCell, &info);
         5722  +    assert( nHeader=(int)(info.pPayload - pCell) );
         5723  +    assert( info.nKey==nKey );
         5724  +    assert( *pnSize == info.nSize );
         5725  +    assert( spaceLeft == info.nLocal );
         5726  +    assert( pPrior == &pCell[info.iOverflow] );
         5727  +  }
         5728  +#endif
         5729  +
         5730  +  /* Write the payload into the local Cell and any extra into overflow pages */
  5688   5731     while( nPayload>0 ){
  5689   5732       if( spaceLeft==0 ){
  5690   5733   #ifndef SQLITE_OMIT_AUTOVACUUM
  5691   5734         Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
  5692   5735         if( pBt->autoVacuum ){
  5693   5736           do{
  5694   5737             pgnoOvfl++;
................................................................................
  6388   6431     ** apCell[] include child pointers.  Either way, all cells in apCell[]
  6389   6432     ** are alike.
  6390   6433     **
  6391   6434     ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
  6392   6435     **       leafData:  1 if pPage holds key+data and pParent holds only keys.
  6393   6436     */
  6394   6437     leafCorrection = apOld[0]->leaf*4;
  6395         -  leafData = apOld[0]->hasData;
         6438  +  leafData = apOld[0]->intKeyLeaf;
  6396   6439     for(i=0; i<nOld; i++){
  6397   6440       int limit;
  6398   6441       
  6399   6442       /* Before doing anything else, take a copy of the i'th original sibling
  6400   6443       ** The rest of this function will use data from the copies rather
  6401   6444       ** that the original pages since the original pages will be in the
  6402   6445       ** process of being overwritten.  */
................................................................................
  6964   7007       }else{
  6965   7008         MemPage * const pParent = pCur->apPage[iPage-1];
  6966   7009         int const iIdx = pCur->aiIdx[iPage-1];
  6967   7010   
  6968   7011         rc = sqlite3PagerWrite(pParent->pDbPage);
  6969   7012         if( rc==SQLITE_OK ){
  6970   7013   #ifndef SQLITE_OMIT_QUICKBALANCE
  6971         -        if( pPage->hasData
         7014  +        if( pPage->intKeyLeaf
  6972   7015            && pPage->nOverflow==1
  6973   7016            && pPage->aiOvfl[0]==pPage->nCell
  6974   7017            && pParent->pgno!=1
  6975   7018            && pParent->nCell==iIdx
  6976   7019           ){
  6977   7020             /* Call balance_quick() to create a new sibling of pPage on which
  6978   7021             ** to store the overflow cell. balance_quick() inserts a new cell
................................................................................
  7083   7126   
  7084   7127     if( pCur->eState==CURSOR_FAULT ){
  7085   7128       assert( pCur->skipNext!=SQLITE_OK );
  7086   7129       return pCur->skipNext;
  7087   7130     }
  7088   7131   
  7089   7132     assert( cursorHoldsMutex(pCur) );
  7090         -  assert( (pCur->curFlags & BTCF_WriteFlag)!=0 && pBt->inTransaction==TRANS_WRITE
         7133  +  assert( (pCur->curFlags & BTCF_WriteFlag)!=0
         7134  +              && pBt->inTransaction==TRANS_WRITE
  7091   7135                 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
  7092   7136     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  7093   7137   
  7094   7138     /* Assert that the caller has been consistent. If this cursor was opened
  7095   7139     ** expecting an index b-tree, then the caller should be inserting blob
  7096   7140     ** keys with no associated data. If the cursor was opened expecting an
  7097   7141     ** intkey table, the caller should be inserting integer keys with a
................................................................................
  7116   7160       /* If this is an insert into a table b-tree, invalidate any incrblob 
  7117   7161       ** cursors open on the row being replaced */
  7118   7162       invalidateIncrblobCursors(p, nKey, 0);
  7119   7163   
  7120   7164       /* If the cursor is currently on the last row and we are appending a
  7121   7165       ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
  7122   7166       ** call */
  7123         -    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 && pCur->info.nKey==nKey-1 ){
         7167  +    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
         7168  +      && pCur->info.nKey==nKey-1 ){
  7124   7169         loc = -1;
  7125   7170       }
  7126   7171     }
  7127   7172   
  7128   7173     if( !loc ){
  7129   7174       rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
  7130   7175       if( rc ) return rc;
................................................................................
  7135   7180     assert( pPage->intKey || nKey>=0 );
  7136   7181     assert( pPage->leaf || !pPage->intKey );
  7137   7182   
  7138   7183     TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
  7139   7184             pCur->pgnoRoot, nKey, nData, pPage->pgno,
  7140   7185             loc==0 ? "overwrite" : "new entry"));
  7141   7186     assert( pPage->isInit );
  7142         -  allocateTempSpace(pBt);
  7143   7187     newCell = pBt->pTmpSpace;
  7144         -  if( newCell==0 ) return SQLITE_NOMEM;
         7188  +  assert( newCell!=0 );
  7145   7189     rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
  7146   7190     if( rc ) goto end_insert;
  7147   7191     assert( szNew==cellSizePtr(pPage, newCell) );
  7148   7192     assert( szNew <= MX_CELL_SIZE(pBt) );
  7149   7193     idx = pCur->aiIdx[pCur->iPage];
  7150   7194     if( loc==0 ){
  7151   7195       u16 szOld;
................................................................................
  7154   7198       if( rc ){
  7155   7199         goto end_insert;
  7156   7200       }
  7157   7201       oldCell = findCell(pPage, idx);
  7158   7202       if( !pPage->leaf ){
  7159   7203         memcpy(newCell, oldCell, 4);
  7160   7204       }
  7161         -    szOld = cellSizePtr(pPage, oldCell);
  7162         -    rc = clearCell(pPage, oldCell);
         7205  +    rc = clearCell(pPage, oldCell, &szOld);
  7163   7206       dropCell(pPage, idx, szOld, &rc);
  7164   7207       if( rc ) goto end_insert;
  7165   7208     }else if( loc<0 && pPage->nCell>0 ){
  7166   7209       assert( pPage->leaf );
  7167   7210       idx = ++pCur->aiIdx[pCur->iPage];
  7168   7211     }else{
  7169   7212       assert( pPage->leaf );
................................................................................
  7217   7260     Btree *p = pCur->pBtree;
  7218   7261     BtShared *pBt = p->pBt;              
  7219   7262     int rc;                              /* Return code */
  7220   7263     MemPage *pPage;                      /* Page to delete cell from */
  7221   7264     unsigned char *pCell;                /* Pointer to cell to delete */
  7222   7265     int iCellIdx;                        /* Index of cell to delete */
  7223   7266     int iCellDepth;                      /* Depth of node containing pCell */ 
         7267  +  u16 szCell;                          /* Size of the cell being deleted */
  7224   7268   
  7225   7269     assert( cursorHoldsMutex(pCur) );
  7226   7270     assert( pBt->inTransaction==TRANS_WRITE );
  7227   7271     assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  7228   7272     assert( pCur->curFlags & BTCF_WriteFlag );
  7229   7273     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  7230   7274     assert( !hasReadConflicts(p, pCur->pgnoRoot) );
................................................................................
  7265   7309     ** invalidate any incrblob cursors open on the row being deleted.  */
  7266   7310     if( pCur->pKeyInfo==0 ){
  7267   7311       invalidateIncrblobCursors(p, pCur->info.nKey, 0);
  7268   7312     }
  7269   7313   
  7270   7314     rc = sqlite3PagerWrite(pPage->pDbPage);
  7271   7315     if( rc ) return rc;
  7272         -  rc = clearCell(pPage, pCell);
  7273         -  dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
         7316  +  rc = clearCell(pPage, pCell, &szCell);
         7317  +  dropCell(pPage, iCellIdx, szCell, &rc);
  7274   7318     if( rc ) return rc;
  7275   7319   
  7276   7320     /* If the cell deleted was not located on a leaf page, then the cursor
  7277   7321     ** is currently pointing to the largest entry in the sub-tree headed
  7278   7322     ** by the child-page of the cell that was just deleted from an internal
  7279   7323     ** node. The cell from the leaf node needs to be moved to the internal
  7280   7324     ** node to replace the deleted cell.  */
................................................................................
  7283   7327       int nCell;
  7284   7328       Pgno n = pCur->apPage[iCellDepth+1]->pgno;
  7285   7329       unsigned char *pTmp;
  7286   7330   
  7287   7331       pCell = findCell(pLeaf, pLeaf->nCell-1);
  7288   7332       nCell = cellSizePtr(pLeaf, pCell);
  7289   7333       assert( MX_CELL_SIZE(pBt) >= nCell );
  7290         -
  7291         -    allocateTempSpace(pBt);
  7292   7334       pTmp = pBt->pTmpSpace;
  7293         -
         7335  +    assert( pTmp!=0 );
  7294   7336       rc = sqlite3PagerWrite(pLeaf->pDbPage);
  7295   7337       insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
  7296   7338       dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
  7297   7339       if( rc ) return rc;
  7298   7340     }
  7299   7341   
  7300   7342     /* Balance the tree. If the entry deleted was located on a leaf page,
................................................................................
  7498   7540     int *pnChange            /* Add number of Cells freed to this counter */
  7499   7541   ){
  7500   7542     MemPage *pPage;
  7501   7543     int rc;
  7502   7544     unsigned char *pCell;
  7503   7545     int i;
  7504   7546     int hdr;
         7547  +  u16 szCell;
  7505   7548   
  7506   7549     assert( sqlite3_mutex_held(pBt->mutex) );
  7507   7550     if( pgno>btreePagecount(pBt) ){
  7508   7551       return SQLITE_CORRUPT_BKPT;
  7509   7552     }
  7510   7553   
  7511   7554     rc = getAndInitPage(pBt, pgno, &pPage, 0);
................................................................................
  7513   7556     hdr = pPage->hdrOffset;
  7514   7557     for(i=0; i<pPage->nCell; i++){
  7515   7558       pCell = findCell(pPage, i);
  7516   7559       if( !pPage->leaf ){
  7517   7560         rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
  7518   7561         if( rc ) goto cleardatabasepage_out;
  7519   7562       }
  7520         -    rc = clearCell(pPage, pCell);
         7563  +    rc = clearCell(pPage, pCell, &szCell);
  7521   7564       if( rc ) goto cleardatabasepage_out;
  7522   7565     }
  7523   7566     if( !pPage->leaf ){
  7524   7567       rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
  7525   7568       if( rc ) goto cleardatabasepage_out;
  7526   7569     }else if( pnChange ){
  7527   7570       assert( pPage->intKey );
................................................................................
  7859   7902   
  7860   7903   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
  7861   7904   /*
  7862   7905   ** Append a message to the error message string.
  7863   7906   */
  7864   7907   static void checkAppendMsg(
  7865   7908     IntegrityCk *pCheck,
  7866         -  char *zMsg1,
  7867   7909     const char *zFormat,
  7868   7910     ...
  7869   7911   ){
  7870   7912     va_list ap;
         7913  +  char zBuf[200];
  7871   7914     if( !pCheck->mxErr ) return;
  7872   7915     pCheck->mxErr--;
  7873   7916     pCheck->nErr++;
  7874   7917     va_start(ap, zFormat);
  7875   7918     if( pCheck->errMsg.nChar ){
  7876   7919       sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
  7877   7920     }
  7878         -  if( zMsg1 ){
  7879         -    sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
         7921  +  if( pCheck->zPfx ){
         7922  +    sqlite3_snprintf(sizeof(zBuf), zBuf, pCheck->zPfx, pCheck->v1, pCheck->v2);
         7923  +    sqlite3StrAccumAppendAll(&pCheck->errMsg, zBuf);
  7880   7924     }
  7881   7925     sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
  7882   7926     va_end(ap);
  7883   7927     if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
  7884   7928       pCheck->mallocFailed = 1;
  7885   7929     }
  7886   7930   }
................................................................................
  7910   7954   ** Add 1 to the reference count for page iPage.  If this is the second
  7911   7955   ** reference to the page, add an error message to pCheck->zErrMsg.
  7912   7956   ** Return 1 if there are 2 or more references to the page and 0 if
  7913   7957   ** if this is the first reference to the page.
  7914   7958   **
  7915   7959   ** Also check that the page number is in bounds.
  7916   7960   */
  7917         -static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
         7961  +static int checkRef(IntegrityCk *pCheck, Pgno iPage){
  7918   7962     if( iPage==0 ) return 1;
  7919   7963     if( iPage>pCheck->nPage ){
  7920         -    checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
         7964  +    checkAppendMsg(pCheck, "invalid page number %d", iPage);
  7921   7965       return 1;
  7922   7966     }
  7923   7967     if( getPageReferenced(pCheck, iPage) ){
  7924         -    checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
         7968  +    checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
  7925   7969       return 1;
  7926   7970     }
  7927   7971     setPageReferenced(pCheck, iPage);
  7928   7972     return 0;
  7929   7973   }
  7930   7974   
  7931   7975   #ifndef SQLITE_OMIT_AUTOVACUUM
................................................................................
  7934   7978   ** page iParent, pointer type ptrType. If not, append an error message
  7935   7979   ** to pCheck.
  7936   7980   */
  7937   7981   static void checkPtrmap(
  7938   7982     IntegrityCk *pCheck,   /* Integrity check context */
  7939   7983     Pgno iChild,           /* Child page number */
  7940   7984     u8 eType,              /* Expected pointer map type */
  7941         -  Pgno iParent,          /* Expected pointer map parent page number */
  7942         -  char *zContext         /* Context description (used for error msg) */
         7985  +  Pgno iParent           /* Expected pointer map parent page number */
  7943   7986   ){
  7944   7987     int rc;
  7945   7988     u8 ePtrmapType;
  7946   7989     Pgno iPtrmapParent;
  7947   7990   
  7948   7991     rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
  7949   7992     if( rc!=SQLITE_OK ){
  7950   7993       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
  7951         -    checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
         7994  +    checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
  7952   7995       return;
  7953   7996     }
  7954   7997   
  7955   7998     if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
  7956         -    checkAppendMsg(pCheck, zContext, 
         7999  +    checkAppendMsg(pCheck,
  7957   8000         "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
  7958   8001         iChild, eType, iParent, ePtrmapType, iPtrmapParent);
  7959   8002     }
  7960   8003   }
  7961   8004   #endif
  7962   8005   
  7963   8006   /*
................................................................................
  7964   8007   ** Check the integrity of the freelist or of an overflow page list.
  7965   8008   ** Verify that the number of pages on the list is N.
  7966   8009   */
  7967   8010   static void checkList(
  7968   8011     IntegrityCk *pCheck,  /* Integrity checking context */
  7969   8012     int isFreeList,       /* True for a freelist.  False for overflow page list */
  7970   8013     int iPage,            /* Page number for first page in the list */
  7971         -  int N,                /* Expected number of pages in the list */
  7972         -  char *zContext        /* Context for error messages */
         8014  +  int N                 /* Expected number of pages in the list */
  7973   8015   ){
  7974   8016     int i;
  7975   8017     int expected = N;
  7976   8018     int iFirst = iPage;
  7977   8019     while( N-- > 0 && pCheck->mxErr ){
  7978   8020       DbPage *pOvflPage;
  7979   8021       unsigned char *pOvflData;
  7980   8022       if( iPage<1 ){
  7981         -      checkAppendMsg(pCheck, zContext,
         8023  +      checkAppendMsg(pCheck,
  7982   8024            "%d of %d pages missing from overflow list starting at %d",
  7983   8025             N+1, expected, iFirst);
  7984   8026         break;
  7985   8027       }
  7986         -    if( checkRef(pCheck, iPage, zContext) ) break;
         8028  +    if( checkRef(pCheck, iPage) ) break;
  7987   8029       if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
  7988         -      checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
         8030  +      checkAppendMsg(pCheck, "failed to get page %d", iPage);
  7989   8031         break;
  7990   8032       }
  7991   8033       pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
  7992   8034       if( isFreeList ){
  7993   8035         int n = get4byte(&pOvflData[4]);
  7994   8036   #ifndef SQLITE_OMIT_AUTOVACUUM
  7995   8037         if( pCheck->pBt->autoVacuum ){
  7996         -        checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
         8038  +        checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
  7997   8039         }
  7998   8040   #endif
  7999   8041         if( n>(int)pCheck->pBt->usableSize/4-2 ){
  8000         -        checkAppendMsg(pCheck, zContext,
         8042  +        checkAppendMsg(pCheck,
  8001   8043              "freelist leaf count too big on page %d", iPage);
  8002   8044           N--;
  8003   8045         }else{
  8004   8046           for(i=0; i<n; i++){
  8005   8047             Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
  8006   8048   #ifndef SQLITE_OMIT_AUTOVACUUM
  8007   8049             if( pCheck->pBt->autoVacuum ){
  8008         -            checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
         8050  +            checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
  8009   8051             }
  8010   8052   #endif
  8011         -          checkRef(pCheck, iFreePage, zContext);
         8053  +          checkRef(pCheck, iFreePage);
  8012   8054           }
  8013   8055           N -= n;
  8014   8056         }
  8015   8057       }
  8016   8058   #ifndef SQLITE_OMIT_AUTOVACUUM
  8017   8059       else{
  8018   8060         /* If this database supports auto-vacuum and iPage is not the last
  8019   8061         ** page in this overflow list, check that the pointer-map entry for
  8020   8062         ** the following page matches iPage.
  8021   8063         */
  8022   8064         if( pCheck->pBt->autoVacuum && N>0 ){
  8023   8065           i = get4byte(pOvflData);
  8024         -        checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
         8066  +        checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
  8025   8067         }
  8026   8068       }
  8027   8069   #endif
  8028   8070       iPage = get4byte(pOvflData);
  8029   8071       sqlite3PagerUnref(pOvflPage);
  8030   8072     }
  8031   8073   }
................................................................................
  8049   8091   **      7.  Verify that the depth of all children is the same.
  8050   8092   **      8.  Make sure this page is at least 33% full or else it is
  8051   8093   **          the root of the tree.
  8052   8094   */
  8053   8095   static int checkTreePage(
  8054   8096     IntegrityCk *pCheck,  /* Context for the sanity check */
  8055   8097     int iPage,            /* Page number of the page to check */
  8056         -  char *zParentContext, /* Parent context */
  8057   8098     i64 *pnParentMinKey, 
  8058   8099     i64 *pnParentMaxKey
  8059   8100   ){
  8060   8101     MemPage *pPage;
  8061   8102     int i, rc, depth, d2, pgno, cnt;
  8062   8103     int hdr, cellStart;
  8063   8104     int nCell;
  8064   8105     u8 *data;
  8065   8106     BtShared *pBt;
  8066   8107     int usableSize;
  8067         -  char zContext[100];
  8068   8108     char *hit = 0;
  8069   8109     i64 nMinKey = 0;
  8070   8110     i64 nMaxKey = 0;
  8071         -
  8072         -  sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
         8111  +  const char *saved_zPfx = pCheck->zPfx;
         8112  +  int saved_v1 = pCheck->v1;
         8113  +  int saved_v2 = pCheck->v2;
  8073   8114   
  8074   8115     /* Check that the page exists
  8075   8116     */
  8076   8117     pBt = pCheck->pBt;
  8077   8118     usableSize = pBt->usableSize;
  8078   8119     if( iPage==0 ) return 0;
  8079         -  if( checkRef(pCheck, iPage, zParentContext) ) return 0;
         8120  +  if( checkRef(pCheck, iPage) ) return 0;
         8121  +  pCheck->zPfx = "Page %d: ";
         8122  +  pCheck->v1 = iPage;
  8080   8123     if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
  8081         -    checkAppendMsg(pCheck, zContext,
         8124  +    checkAppendMsg(pCheck,
  8082   8125          "unable to get the page. error code=%d", rc);
  8083         -    return 0;
         8126  +    depth = -1;
         8127  +    goto end_of_check;
  8084   8128     }
  8085   8129   
  8086   8130     /* Clear MemPage.isInit to make sure the corruption detection code in
  8087   8131     ** btreeInitPage() is executed.  */
  8088   8132     pPage->isInit = 0;
  8089   8133     if( (rc = btreeInitPage(pPage))!=0 ){
  8090   8134       assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
  8091         -    checkAppendMsg(pCheck, zContext, 
         8135  +    checkAppendMsg(pCheck,
  8092   8136                      "btreeInitPage() returns error code %d", rc);
  8093   8137       releasePage(pPage);
  8094         -    return 0;
         8138  +    depth = -1;
         8139  +    goto end_of_check;
  8095   8140     }
  8096   8141   
  8097   8142     /* Check out all the cells.
  8098   8143     */
  8099   8144     depth = 0;
  8100   8145     for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
  8101   8146       u8 *pCell;
  8102   8147       u32 sz;
  8103   8148       CellInfo info;
  8104   8149   
  8105   8150       /* Check payload overflow pages
  8106   8151       */
  8107         -    sqlite3_snprintf(sizeof(zContext), zContext,
  8108         -             "On tree page %d cell %d: ", iPage, i);
         8152  +    pCheck->zPfx = "On tree page %d cell %d: ";
         8153  +    pCheck->v1 = iPage;
         8154  +    pCheck->v2 = i;
  8109   8155       pCell = findCell(pPage,i);
  8110   8156       btreeParseCellPtr(pPage, pCell, &info);
  8111         -    sz = info.nData;
  8112         -    if( !pPage->intKey ) sz += (int)info.nKey;
         8157  +    sz = info.nPayload;
  8113   8158       /* For intKey pages, check that the keys are in order.
  8114   8159       */
  8115         -    else if( i==0 ) nMinKey = nMaxKey = info.nKey;
  8116         -    else{
  8117         -      if( info.nKey <= nMaxKey ){
  8118         -        checkAppendMsg(pCheck, zContext, 
  8119         -            "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
         8160  +    if( pPage->intKey ){
         8161  +      if( i==0 ){
         8162  +        nMinKey = nMaxKey = info.nKey;
         8163  +      }else if( info.nKey <= nMaxKey ){
         8164  +        checkAppendMsg(pCheck,
         8165  +           "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
  8120   8166         }
  8121   8167         nMaxKey = info.nKey;
  8122   8168       }
  8123         -    assert( sz==info.nPayload );
  8124   8169       if( (sz>info.nLocal) 
  8125   8170        && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
  8126   8171       ){
  8127   8172         int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
  8128   8173         Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
  8129   8174   #ifndef SQLITE_OMIT_AUTOVACUUM
  8130   8175         if( pBt->autoVacuum ){
  8131         -        checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
         8176  +        checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
  8132   8177         }
  8133   8178   #endif
  8134         -      checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
         8179  +      checkList(pCheck, 0, pgnoOvfl, nPage);
  8135   8180       }
  8136   8181   
  8137   8182       /* Check sanity of left child page.
  8138   8183       */
  8139   8184       if( !pPage->leaf ){
  8140   8185         pgno = get4byte(pCell);
  8141   8186   #ifndef SQLITE_OMIT_AUTOVACUUM
  8142   8187         if( pBt->autoVacuum ){
  8143         -        checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
         8188  +        checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
  8144   8189         }
  8145   8190   #endif
  8146         -      d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
         8191  +      d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey);
  8147   8192         if( i>0 && d2!=depth ){
  8148         -        checkAppendMsg(pCheck, zContext, "Child page depth differs");
         8193  +        checkAppendMsg(pCheck, "Child page depth differs");
  8149   8194         }
  8150   8195         depth = d2;
  8151   8196       }
  8152   8197     }
  8153   8198   
  8154   8199     if( !pPage->leaf ){
  8155   8200       pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
  8156         -    sqlite3_snprintf(sizeof(zContext), zContext, 
  8157         -                     "On page %d at right child: ", iPage);
         8201  +    pCheck->zPfx = "On page %d at right child: ";
         8202  +    pCheck->v1 = iPage;
  8158   8203   #ifndef SQLITE_OMIT_AUTOVACUUM
  8159   8204       if( pBt->autoVacuum ){
  8160         -      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
         8205  +      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
  8161   8206       }
  8162   8207   #endif
  8163         -    checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
         8208  +    checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey);
  8164   8209     }
  8165   8210    
  8166   8211     /* For intKey leaf pages, check that the min/max keys are in order
  8167   8212     ** with any left/parent/right pages.
  8168   8213     */
         8214  +  pCheck->zPfx = "Page %d: ";
         8215  +  pCheck->v1 = iPage;
  8169   8216     if( pPage->leaf && pPage->intKey ){
  8170   8217       /* if we are a left child page */
  8171   8218       if( pnParentMinKey ){
  8172   8219         /* if we are the left most child page */
  8173   8220         if( !pnParentMaxKey ){
  8174   8221           if( nMaxKey > *pnParentMinKey ){
  8175         -          checkAppendMsg(pCheck, zContext, 
         8222  +          checkAppendMsg(pCheck,
  8176   8223                 "Rowid %lld out of order (max larger than parent min of %lld)",
  8177   8224                 nMaxKey, *pnParentMinKey);
  8178   8225           }
  8179   8226         }else{
  8180   8227           if( nMinKey <= *pnParentMinKey ){
  8181         -          checkAppendMsg(pCheck, zContext, 
         8228  +          checkAppendMsg(pCheck,
  8182   8229                 "Rowid %lld out of order (min less than parent min of %lld)",
  8183   8230                 nMinKey, *pnParentMinKey);
  8184   8231           }
  8185   8232           if( nMaxKey > *pnParentMaxKey ){
  8186         -          checkAppendMsg(pCheck, zContext, 
         8233  +          checkAppendMsg(pCheck,
  8187   8234                 "Rowid %lld out of order (max larger than parent max of %lld)",
  8188   8235                 nMaxKey, *pnParentMaxKey);
  8189   8236           }
  8190   8237           *pnParentMinKey = nMaxKey;
  8191   8238         }
  8192   8239       /* else if we're a right child page */
  8193   8240       } else if( pnParentMaxKey ){
  8194   8241         if( nMinKey <= *pnParentMaxKey ){
  8195         -        checkAppendMsg(pCheck, zContext, 
         8242  +        checkAppendMsg(pCheck,
  8196   8243               "Rowid %lld out of order (min less than parent max of %lld)",
  8197   8244               nMinKey, *pnParentMaxKey);
  8198   8245         }
  8199   8246       }
  8200   8247     }
  8201   8248   
  8202   8249     /* Check for complete coverage of the page
  8203   8250     */
  8204   8251     data = pPage->aData;
  8205   8252     hdr = pPage->hdrOffset;
  8206   8253     hit = sqlite3PageMalloc( pBt->pageSize );
         8254  +  pCheck->zPfx = 0;
  8207   8255     if( hit==0 ){
  8208   8256       pCheck->mallocFailed = 1;
  8209   8257     }else{
  8210   8258       int contentOffset = get2byteNotZero(&data[hdr+5]);
  8211   8259       assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
  8212   8260       memset(hit+contentOffset, 0, usableSize-contentOffset);
  8213   8261       memset(hit, 1, contentOffset);
................................................................................
  8217   8265         int pc = get2byte(&data[cellStart+i*2]);
  8218   8266         u32 size = 65536;
  8219   8267         int j;
  8220   8268         if( pc<=usableSize-4 ){
  8221   8269           size = cellSizePtr(pPage, &data[pc]);
  8222   8270         }
  8223   8271         if( (int)(pc+size-1)>=usableSize ){
  8224         -        checkAppendMsg(pCheck, 0, 
         8272  +        pCheck->zPfx = 0;
         8273  +        checkAppendMsg(pCheck,
  8225   8274               "Corruption detected in cell %d on page %d",i,iPage);
  8226   8275         }else{
  8227   8276           for(j=pc+size-1; j>=pc; j--) hit[j]++;
  8228   8277         }
  8229   8278       }
  8230   8279       i = get2byte(&data[hdr+1]);
  8231   8280       while( i>0 ){
................................................................................
  8239   8288         assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
  8240   8289         i = j;
  8241   8290       }
  8242   8291       for(i=cnt=0; i<usableSize; i++){
  8243   8292         if( hit[i]==0 ){
  8244   8293           cnt++;
  8245   8294         }else if( hit[i]>1 ){
  8246         -        checkAppendMsg(pCheck, 0,
         8295  +        checkAppendMsg(pCheck,
  8247   8296             "Multiple uses for byte %d of page %d", i, iPage);
  8248   8297           break;
  8249   8298         }
  8250   8299       }
  8251   8300       if( cnt!=data[hdr+7] ){
  8252         -      checkAppendMsg(pCheck, 0, 
         8301  +      checkAppendMsg(pCheck,
  8253   8302             "Fragmentation of %d bytes reported as %d on page %d",
  8254   8303             cnt, data[hdr+7], iPage);
  8255   8304       }
  8256   8305     }
  8257   8306     sqlite3PageFree(hit);
  8258   8307     releasePage(pPage);
         8308  +
         8309  +end_of_check:
         8310  +  pCheck->zPfx = saved_zPfx;
         8311  +  pCheck->v1 = saved_v1;
         8312  +  pCheck->v2 = saved_v2;
  8259   8313     return depth+1;
  8260   8314   }
  8261   8315   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  8262   8316   
  8263   8317   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
  8264   8318   /*
  8265   8319   ** This routine does a complete check of the given BTree file.  aRoot[] is
................................................................................
  8292   8346     nRef = sqlite3PagerRefcount(pBt->pPager);
  8293   8347     sCheck.pBt = pBt;
  8294   8348     sCheck.pPager = pBt->pPager;
  8295   8349     sCheck.nPage = btreePagecount(sCheck.pBt);
  8296   8350     sCheck.mxErr = mxErr;
  8297   8351     sCheck.nErr = 0;
  8298   8352     sCheck.mallocFailed = 0;
         8353  +  sCheck.zPfx = 0;
         8354  +  sCheck.v1 = 0;
         8355  +  sCheck.v2 = 0;
  8299   8356     *pnErr = 0;
  8300   8357     if( sCheck.nPage==0 ){
  8301   8358       sqlite3BtreeLeave(p);
  8302   8359       return 0;
  8303   8360     }
  8304   8361   
  8305   8362     sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
................................................................................
  8311   8368     i = PENDING_BYTE_PAGE(pBt);
  8312   8369     if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
  8313   8370     sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
  8314   8371     sCheck.errMsg.useMalloc = 2;
  8315   8372   
  8316   8373     /* Check the integrity of the freelist
  8317   8374     */
         8375  +  sCheck.zPfx = "Main freelist: ";
  8318   8376     checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
  8319         -            get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
         8377  +            get4byte(&pBt->pPage1->aData[36]));
         8378  +  sCheck.zPfx = 0;
  8320   8379   
  8321   8380     /* Check all the tables.
  8322   8381     */
  8323   8382     for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
  8324   8383       if( aRoot[i]==0 ) continue;
  8325   8384   #ifndef SQLITE_OMIT_AUTOVACUUM
  8326   8385       if( pBt->autoVacuum && aRoot[i]>1 ){
  8327         -      checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
         8386  +      checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
  8328   8387       }
  8329   8388   #endif
  8330         -    checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
         8389  +    sCheck.zPfx = "List of tree roots: ";
         8390  +    checkTreePage(&sCheck, aRoot[i], NULL, NULL);
         8391  +    sCheck.zPfx = 0;
  8331   8392     }
  8332   8393   
  8333   8394     /* Make sure every page in the file is referenced
  8334   8395     */
  8335   8396     for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
  8336   8397   #ifdef SQLITE_OMIT_AUTOVACUUM
  8337   8398       if( getPageReferenced(&sCheck, i)==0 ){
  8338         -      checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
         8399  +      checkAppendMsg(&sCheck, "Page %d is never used", i);
  8339   8400       }
  8340   8401   #else
  8341   8402       /* If the database supports auto-vacuum, make sure no tables contain
  8342   8403       ** references to pointer-map pages.
  8343   8404       */
  8344   8405       if( getPageReferenced(&sCheck, i)==0 && 
  8345   8406          (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
  8346         -      checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
         8407  +      checkAppendMsg(&sCheck, "Page %d is never used", i);
  8347   8408       }
  8348   8409       if( getPageReferenced(&sCheck, i)!=0 && 
  8349   8410          (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
  8350         -      checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
         8411  +      checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
  8351   8412       }
  8352   8413   #endif
  8353   8414     }
  8354   8415   
  8355   8416     /* Make sure this analysis did not leave any unref() pages.
  8356   8417     ** This is an internal consistency check; an integrity check
  8357   8418     ** of the integrity check.
  8358   8419     */
  8359   8420     if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
  8360         -    checkAppendMsg(&sCheck, 0, 
         8421  +    checkAppendMsg(&sCheck,
  8361   8422         "Outstanding page count goes from %d to %d during this analysis",
  8362   8423         nRef, sqlite3PagerRefcount(pBt->pPager)
  8363   8424       );
  8364   8425     }
  8365   8426   
  8366   8427     /* Clean  up and report errors.
  8367   8428     */
................................................................................
  8549   8610       return SQLITE_ABORT;
  8550   8611     }
  8551   8612   
  8552   8613     /* Save the positions of all other cursors open on this table. This is
  8553   8614     ** required in case any of them are holding references to an xFetch
  8554   8615     ** version of the b-tree page modified by the accessPayload call below.
  8555   8616     **
  8556         -  ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition()
         8617  +  ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
  8557   8618     ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
  8558   8619     ** saveAllCursors can only return SQLITE_OK.
  8559   8620     */
  8560   8621     VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
  8561   8622     assert( rc==SQLITE_OK );
  8562   8623   
  8563   8624     /* Check some assumptions: 

Changes to src/btreeInt.h.

   269    269   **
   270    270   ** Access to all fields of this structure is controlled by the mutex
   271    271   ** stored in MemPage.pBt->mutex.
   272    272   */
   273    273   struct MemPage {
   274    274     u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
   275    275     u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
   276         -  u8 intKey;           /* True if intkey flag is set */
   277         -  u8 leaf;             /* True if leaf flag is set */
   278         -  u8 hasData;          /* True if this page stores data */
          276  +  u8 intKey;           /* True if table b-trees.  False for index b-trees */
          277  +  u8 intKeyLeaf;       /* True if the leaf of an intKey table */
          278  +  u8 noPayload;        /* True if internal intKey page (thus w/o data) */
          279  +  u8 leaf;             /* True if a leaf page */
   279    280     u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
   280    281     u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
   281    282     u8 max1bytePayload;  /* min(maxLocal,127) */
   282    283     u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
   283    284     u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
   284    285     u16 cellOffset;      /* Index in aData of first cell pointer */
   285    286     u16 nFree;           /* Number of free bytes on the page */
................................................................................
   452    453   /*
   453    454   ** An instance of the following structure is used to hold information
   454    455   ** about a cell.  The parseCellPtr() function fills in this structure
   455    456   ** based on information extract from the raw disk page.
   456    457   */
   457    458   typedef struct CellInfo CellInfo;
   458    459   struct CellInfo {
   459         -  i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
   460         -  u8 *pCell;     /* Pointer to the start of cell content */
   461         -  u32 nData;     /* Number of bytes of data */
   462         -  u32 nPayload;  /* Total amount of payload */
   463         -  u16 nHeader;   /* Size of the cell content header in bytes */
   464         -  u16 nLocal;    /* Amount of payload held locally */
          460  +  i64 nKey;      /* The key for INTKEY tables, or nPayload otherwise */
          461  +  u8 *pPayload;  /* Pointer to the start of payload */
          462  +  u32 nPayload;  /* Bytes of payload */
          463  +  u16 nLocal;    /* Amount of payload held locally, not on overflow */
   465    464     u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
   466    465     u16 nSize;     /* Size of the cell content on the main b-tree page */
   467    466   };
   468    467   
   469    468   /*
   470    469   ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
   471    470   ** this will be declared corrupt. This value is calculated based on a
................................................................................
   654    653     BtShared *pBt;    /* The tree being checked out */
   655    654     Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
   656    655     u8 *aPgRef;       /* 1 bit per page in the db (see above) */
   657    656     Pgno nPage;       /* Number of pages in the database */
   658    657     int mxErr;        /* Stop accumulating errors when this reaches zero */
   659    658     int nErr;         /* Number of messages written to zErrMsg so far */
   660    659     int mallocFailed; /* A memory allocation error has occurred */
          660  +  const char *zPfx; /* Error message prefix */
          661  +  int v1, v2;       /* Values for up to two %d fields in zPfx */
   661    662     StrAccum errMsg;  /* Accumulate the error message text here */
   662    663   };
   663    664   
   664    665   /*
   665    666   ** Routines to read or write a two- and four-byte big-endian integer values.
   666    667   */
   667    668   #define get2byte(x)   ((x)[0]<<8 | (x)[1])
   668    669   #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
   669    670   #define get4byte sqlite3Get4byte
   670    671   #define put4byte sqlite3Put4byte

Changes to src/build.c.

  1232   1232   void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
  1233   1233     Table *p;
  1234   1234     Column *pCol;
  1235   1235     sqlite3 *db = pParse->db;
  1236   1236     p = pParse->pNewTable;
  1237   1237     if( p!=0 ){
  1238   1238       pCol = &(p->aCol[p->nCol-1]);
  1239         -    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
         1239  +    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
  1240   1240         sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
  1241   1241             pCol->zName);
  1242   1242       }else{
  1243   1243         /* A copy of pExpr is used instead of the original, as pExpr contains
  1244   1244         ** tokens that point to volatile memory. The 'span' of the expression
  1245   1245         ** is required by pragma table_info.
  1246   1246         */

Changes to src/ctime.c.

   391    391     if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
   392    392     n = sqlite3Strlen30(zOptName);
   393    393   
   394    394     /* Since ArraySize(azCompileOpt) is normally in single digits, a
   395    395     ** linear search is adequate.  No need for a binary search. */
   396    396     for(i=0; i<ArraySize(azCompileOpt); i++){
   397    397       if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
   398         -     && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
          398  +     && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
   399    399       ){
   400    400         return 1;
   401    401       }
   402    402     }
   403    403     return 0;
   404    404   }
   405    405   

Changes to src/expr.c.

  1208   1208     sqlite3DbFree(db, pList->a);
  1209   1209     sqlite3DbFree(db, pList);
  1210   1210   }
  1211   1211   
  1212   1212   /*
  1213   1213   ** These routines are Walker callbacks.  Walker.u.pi is a pointer
  1214   1214   ** to an integer.  These routines are checking an expression to see
  1215         -** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
         1215  +** if it is a constant.  Set *Walker.u.i to 0 if the expression is
  1216   1216   ** not constant.
  1217   1217   **
  1218   1218   ** These callback routines are used to implement the following:
  1219   1219   **
  1220         -**     sqlite3ExprIsConstant()
  1221         -**     sqlite3ExprIsConstantNotJoin()
  1222         -**     sqlite3ExprIsConstantOrFunction()
         1220  +**     sqlite3ExprIsConstant()                  pWalker->u.i==1
         1221  +**     sqlite3ExprIsConstantNotJoin()           pWalker->u.i==2
         1222  +**     sqlite3ExprIsConstantOrFunction()        pWalker->u.i==3 or 4
  1223   1223   **
         1224  +** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
         1225  +** in a CREATE TABLE statement.  The Walker.u.i value is 4 when parsing
         1226  +** an existing schema and 3 when processing a new statement.  A bound
         1227  +** parameter raises an error for new statements, but is silently converted
         1228  +** to NULL for existing schemas.  This allows sqlite_master tables that 
         1229  +** contain a bound parameter because they were generated by older versions
         1230  +** of SQLite to be parsed by newer versions of SQLite without raising a
         1231  +** malformed schema error.
  1224   1232   */
  1225   1233   static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
  1226   1234   
  1227         -  /* If pWalker->u.i is 3 then any term of the expression that comes from
         1235  +  /* If pWalker->u.i is 2 then any term of the expression that comes from
  1228   1236     ** the ON or USING clauses of a join disqualifies the expression
  1229   1237     ** from being considered constant. */
  1230         -  if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){
         1238  +  if( pWalker->u.i==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
  1231   1239       pWalker->u.i = 0;
  1232   1240       return WRC_Abort;
  1233   1241     }
  1234   1242   
  1235   1243     switch( pExpr->op ){
  1236   1244       /* Consider functions to be constant if all their arguments are constant
  1237         -    ** and either pWalker->u.i==2 or the function as the SQLITE_FUNC_CONST
         1245  +    ** and either pWalker->u.i==3 or 4 or the function as the SQLITE_FUNC_CONST
  1238   1246       ** flag. */
  1239   1247       case TK_FUNCTION:
  1240         -      if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){
         1248  +      if( pWalker->u.i>=3 || ExprHasProperty(pExpr,EP_Constant) ){
  1241   1249           return WRC_Continue;
  1242   1250         }
  1243   1251         /* Fall through */
  1244   1252       case TK_ID:
  1245   1253       case TK_COLUMN:
  1246   1254       case TK_AGG_FUNCTION:
  1247   1255       case TK_AGG_COLUMN:
  1248   1256         testcase( pExpr->op==TK_ID );
  1249   1257         testcase( pExpr->op==TK_COLUMN );
  1250   1258         testcase( pExpr->op==TK_AGG_FUNCTION );
  1251   1259         testcase( pExpr->op==TK_AGG_COLUMN );
  1252   1260         pWalker->u.i = 0;
  1253   1261         return WRC_Abort;
         1262  +    case TK_VARIABLE:
         1263  +      if( pWalker->u.i==4 ){
         1264  +        /* Silently convert bound parameters that appear inside of CREATE
         1265  +        ** statements into a NULL when parsing the CREATE statement text out
         1266  +        ** of the sqlite_master table */
         1267  +        pExpr->op = TK_NULL;
         1268  +      }else if( pWalker->u.i==3 ){
         1269  +        /* A bound parameter in a CREATE statement that originates from
         1270  +        ** sqlite3_prepare() causes an error */
         1271  +        pWalker->u.i = 0;
         1272  +        return WRC_Abort;
         1273  +      }
         1274  +      /* Fall through */
  1254   1275       default:
  1255   1276         testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
  1256   1277         testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
  1257   1278         return WRC_Continue;
  1258   1279     }
  1259   1280   }
  1260   1281   static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
................................................................................
  1287   1308   /*
  1288   1309   ** Walk an expression tree.  Return 1 if the expression is constant
  1289   1310   ** that does no originate from the ON or USING clauses of a join.
  1290   1311   ** Return 0 if it involves variables or function calls or terms from
  1291   1312   ** an ON or USING clause.
  1292   1313   */
  1293   1314   int sqlite3ExprIsConstantNotJoin(Expr *p){
  1294         -  return exprIsConst(p, 3);
         1315  +  return exprIsConst(p, 2);
  1295   1316   }
  1296   1317   
  1297   1318   /*
  1298   1319   ** Walk an expression tree.  Return 1 if the expression is constant
  1299   1320   ** or a function call with constant arguments.  Return and 0 if there
  1300   1321   ** are any variables.
  1301   1322   **
  1302   1323   ** For the purposes of this function, a double-quoted string (ex: "abc")
  1303   1324   ** is considered a variable but a single-quoted string (ex: 'abc') is
  1304   1325   ** a constant.
  1305   1326   */
  1306         -int sqlite3ExprIsConstantOrFunction(Expr *p){
  1307         -  return exprIsConst(p, 2);
         1327  +int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
         1328  +  assert( isInit==0 || isInit==1 );
         1329  +  return exprIsConst(p, 3+isInit);
  1308   1330   }
  1309   1331   
  1310   1332   /*
  1311   1333   ** If the expression p codes a constant integer that is small enough
  1312   1334   ** to fit in a 32-bit integer, return 1 and put the value of the integer
  1313   1335   ** in *pValue.  If the expression is not an integer or if it is too big
  1314   1336   ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
................................................................................
  3210   3232     assert( pExpr->op!=TK_REGISTER );
  3211   3233     sqlite3ExprCode(pParse, pExpr, target);
  3212   3234     iMem = ++pParse->nMem;
  3213   3235     sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
  3214   3236     exprToRegister(pExpr, iMem);
  3215   3237   }
  3216   3238   
  3217         -#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
         3239  +#ifdef SQLITE_DEBUG
  3218   3240   /*
  3219   3241   ** Generate a human-readable explanation of an expression tree.
  3220   3242   */
  3221         -void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
  3222         -  int op;                   /* The opcode being coded */
         3243  +void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
  3223   3244     const char *zBinOp = 0;   /* Binary operator */
  3224   3245     const char *zUniOp = 0;   /* Unary operator */
         3246  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
  3225   3247     if( pExpr==0 ){
  3226         -    op = TK_NULL;
  3227         -  }else{
  3228         -    op = pExpr->op;
         3248  +    sqlite3TreeViewLine(pView, "nil");
         3249  +    sqlite3TreeViewPop(pView);
         3250  +    return;
  3229   3251     }
  3230         -  switch( op ){
         3252  +  switch( pExpr->op ){
  3231   3253       case TK_AGG_COLUMN: {
  3232         -      sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
         3254  +      sqlite3TreeViewLine(pView, "AGG{%d:%d}",
  3233   3255               pExpr->iTable, pExpr->iColumn);
  3234   3256         break;
  3235   3257       }
  3236   3258       case TK_COLUMN: {
  3237   3259         if( pExpr->iTable<0 ){
  3238   3260           /* This only happens when coding check constraints */
  3239         -        sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
         3261  +        sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
  3240   3262         }else{
  3241         -        sqlite3ExplainPrintf(pOut, "{%d:%d}",
         3263  +        sqlite3TreeViewLine(pView, "{%d:%d}",
  3242   3264                                pExpr->iTable, pExpr->iColumn);
  3243   3265         }
  3244   3266         break;
  3245   3267       }
  3246   3268       case TK_INTEGER: {
  3247   3269         if( pExpr->flags & EP_IntValue ){
  3248         -        sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
         3270  +        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
  3249   3271         }else{
  3250         -        sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
         3272  +        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
  3251   3273         }
  3252   3274         break;
  3253   3275       }
  3254   3276   #ifndef SQLITE_OMIT_FLOATING_POINT
  3255   3277       case TK_FLOAT: {
  3256         -      sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
         3278  +      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
  3257   3279         break;
  3258   3280       }
  3259   3281   #endif
  3260   3282       case TK_STRING: {
  3261         -      sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
         3283  +      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
  3262   3284         break;
  3263   3285       }
  3264   3286       case TK_NULL: {
  3265         -      sqlite3ExplainPrintf(pOut,"NULL");
         3287  +      sqlite3TreeViewLine(pView,"NULL");
  3266   3288         break;
  3267   3289       }
  3268   3290   #ifndef SQLITE_OMIT_BLOB_LITERAL
  3269   3291       case TK_BLOB: {
  3270         -      sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
         3292  +      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
  3271   3293         break;
  3272   3294       }
  3273   3295   #endif
  3274   3296       case TK_VARIABLE: {
  3275         -      sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
  3276         -                           pExpr->u.zToken, pExpr->iColumn);
         3297  +      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
         3298  +                          pExpr->u.zToken, pExpr->iColumn);
  3277   3299         break;
  3278   3300       }
  3279   3301       case TK_REGISTER: {
  3280         -      sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
         3302  +      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
  3281   3303         break;
  3282   3304       }
  3283   3305       case TK_AS: {
  3284         -      sqlite3ExplainExpr(pOut, pExpr->pLeft);
         3306  +      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
         3307  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
         3308  +      break;
         3309  +    }
         3310  +    case TK_ID: {
         3311  +      sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken);
  3285   3312         break;
  3286   3313       }
  3287   3314   #ifndef SQLITE_OMIT_CAST
  3288   3315       case TK_CAST: {
  3289   3316         /* Expressions of the form:   CAST(pLeft AS token) */
  3290         -      const char *zAff = "unk";
  3291         -      switch( sqlite3AffinityType(pExpr->u.zToken, 0) ){
  3292         -        case SQLITE_AFF_TEXT:    zAff = "TEXT";     break;
  3293         -        case SQLITE_AFF_NONE:    zAff = "NONE";     break;
  3294         -        case SQLITE_AFF_NUMERIC: zAff = "NUMERIC";  break;
  3295         -        case SQLITE_AFF_INTEGER: zAff = "INTEGER";  break;
  3296         -        case SQLITE_AFF_REAL:    zAff = "REAL";     break;
  3297         -      }
  3298         -      sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
  3299         -      sqlite3ExplainExpr(pOut, pExpr->pLeft);
  3300         -      sqlite3ExplainPrintf(pOut, ")");
         3317  +      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
         3318  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3301   3319         break;
  3302   3320       }
  3303   3321   #endif /* SQLITE_OMIT_CAST */
  3304   3322       case TK_LT:      zBinOp = "LT";     break;
  3305   3323       case TK_LE:      zBinOp = "LE";     break;
  3306   3324       case TK_GT:      zBinOp = "GT";     break;
  3307   3325       case TK_GE:      zBinOp = "GE";     break;
................................................................................
  3326   3344       case TK_UPLUS:   zUniOp = "UPLUS";  break;
  3327   3345       case TK_BITNOT:  zUniOp = "BITNOT"; break;
  3328   3346       case TK_NOT:     zUniOp = "NOT";    break;
  3329   3347       case TK_ISNULL:  zUniOp = "ISNULL"; break;
  3330   3348       case TK_NOTNULL: zUniOp = "NOTNULL"; break;
  3331   3349   
  3332   3350       case TK_COLLATE: {
  3333         -      sqlite3ExplainExpr(pOut, pExpr->pLeft);
  3334         -      sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
         3351  +      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
         3352  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3335   3353         break;
  3336   3354       }
  3337   3355   
  3338   3356       case TK_AGG_FUNCTION:
  3339   3357       case TK_FUNCTION: {
  3340   3358         ExprList *pFarg;       /* List of function arguments */
  3341   3359         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
  3342   3360           pFarg = 0;
  3343   3361         }else{
  3344   3362           pFarg = pExpr->x.pList;
  3345   3363         }
  3346         -      if( op==TK_AGG_FUNCTION ){
  3347         -        sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
         3364  +      if( pExpr->op==TK_AGG_FUNCTION ){
         3365  +        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
  3348   3366                                pExpr->op2, pExpr->u.zToken);
  3349   3367         }else{
  3350         -        sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
         3368  +        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
  3351   3369         }
  3352   3370         if( pFarg ){
  3353         -        sqlite3ExplainExprList(pOut, pFarg);
         3371  +        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
  3354   3372         }
  3355         -      sqlite3ExplainPrintf(pOut, ")");
  3356   3373         break;
  3357   3374       }
  3358   3375   #ifndef SQLITE_OMIT_SUBQUERY
  3359   3376       case TK_EXISTS: {
  3360         -      sqlite3ExplainPrintf(pOut, "EXISTS(");
  3361         -      sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
  3362         -      sqlite3ExplainPrintf(pOut,")");
         3377  +      sqlite3TreeViewLine(pView, "EXISTS-expr");
         3378  +      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3363   3379         break;
  3364   3380       }
  3365   3381       case TK_SELECT: {
  3366         -      sqlite3ExplainPrintf(pOut, "(");
  3367         -      sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
  3368         -      sqlite3ExplainPrintf(pOut, ")");
         3382  +      sqlite3TreeViewLine(pView, "SELECT-expr");
         3383  +      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3369   3384         break;
  3370   3385       }
  3371   3386       case TK_IN: {
  3372         -      sqlite3ExplainPrintf(pOut, "IN(");
  3373         -      sqlite3ExplainExpr(pOut, pExpr->pLeft);
  3374         -      sqlite3ExplainPrintf(pOut, ",");
         3387  +      sqlite3TreeViewLine(pView, "IN");
         3388  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
  3375   3389         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  3376         -        sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
         3390  +        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3377   3391         }else{
  3378         -        sqlite3ExplainExprList(pOut, pExpr->x.pList);
         3392  +        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
  3379   3393         }
  3380         -      sqlite3ExplainPrintf(pOut, ")");
  3381   3394         break;
  3382   3395       }
  3383   3396   #endif /* SQLITE_OMIT_SUBQUERY */
  3384   3397   
  3385   3398       /*
  3386   3399       **    x BETWEEN y AND z
  3387   3400       **
................................................................................
  3393   3406       ** Y is stored in pExpr->pList->a[0].pExpr.
  3394   3407       ** Z is stored in pExpr->pList->a[1].pExpr.
  3395   3408       */
  3396   3409       case TK_BETWEEN: {
  3397   3410         Expr *pX = pExpr->pLeft;
  3398   3411         Expr *pY = pExpr->x.pList->a[0].pExpr;
  3399   3412         Expr *pZ = pExpr->x.pList->a[1].pExpr;
  3400         -      sqlite3ExplainPrintf(pOut, "BETWEEN(");
  3401         -      sqlite3ExplainExpr(pOut, pX);
  3402         -      sqlite3ExplainPrintf(pOut, ",");
  3403         -      sqlite3ExplainExpr(pOut, pY);
  3404         -      sqlite3ExplainPrintf(pOut, ",");
  3405         -      sqlite3ExplainExpr(pOut, pZ);
  3406         -      sqlite3ExplainPrintf(pOut, ")");
         3413  +      sqlite3TreeViewLine(pView, "BETWEEN");
         3414  +      sqlite3TreeViewExpr(pView, pX, 1);
         3415  +      sqlite3TreeViewExpr(pView, pY, 1);
         3416  +      sqlite3TreeViewExpr(pView, pZ, 0);
  3407   3417         break;
  3408   3418       }
  3409   3419       case TK_TRIGGER: {
  3410   3420         /* If the opcode is TK_TRIGGER, then the expression is a reference
  3411   3421         ** to a column in the new.* or old.* pseudo-tables available to
  3412   3422         ** trigger programs. In this case Expr.iTable is set to 1 for the
  3413   3423         ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
  3414   3424         ** is set to the column of the pseudo-table to read, or to -1 to
  3415   3425         ** read the rowid field.
  3416   3426         */
  3417         -      sqlite3ExplainPrintf(pOut, "%s(%d)", 
         3427  +      sqlite3TreeViewLine(pView, "%s(%d)", 
  3418   3428             pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
  3419   3429         break;
  3420   3430       }
  3421   3431       case TK_CASE: {
  3422         -      sqlite3ExplainPrintf(pOut, "CASE(");
  3423         -      sqlite3ExplainExpr(pOut, pExpr->pLeft);
  3424         -      sqlite3ExplainPrintf(pOut, ",");
  3425         -      sqlite3ExplainExprList(pOut, pExpr->x.pList);
         3432  +      sqlite3TreeViewLine(pView, "CASE");
         3433  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
         3434  +      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
  3426   3435         break;
  3427   3436       }
  3428   3437   #ifndef SQLITE_OMIT_TRIGGER
  3429   3438       case TK_RAISE: {
  3430   3439         const char *zType = "unk";
  3431   3440         switch( pExpr->affinity ){
  3432   3441           case OE_Rollback:   zType = "rollback";  break;
  3433   3442           case OE_Abort:      zType = "abort";     break;
  3434   3443           case OE_Fail:       zType = "fail";      break;
  3435   3444           case OE_Ignore:     zType = "ignore";    break;
  3436   3445         }
  3437         -      sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
         3446  +      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
  3438   3447         break;
  3439   3448       }
  3440   3449   #endif
         3450  +    default: {
         3451  +      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
         3452  +      break;
         3453  +    }
  3441   3454     }
  3442   3455     if( zBinOp ){
  3443         -    sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
  3444         -    sqlite3ExplainExpr(pOut, pExpr->pLeft);
  3445         -    sqlite3ExplainPrintf(pOut,",");
  3446         -    sqlite3ExplainExpr(pOut, pExpr->pRight);
  3447         -    sqlite3ExplainPrintf(pOut,")");
         3456  +    sqlite3TreeViewLine(pView, "%s", zBinOp);
         3457  +    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
         3458  +    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
  3448   3459     }else if( zUniOp ){
  3449         -    sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
  3450         -    sqlite3ExplainExpr(pOut, pExpr->pLeft);
  3451         -    sqlite3ExplainPrintf(pOut,")");
         3460  +    sqlite3TreeViewLine(pView, "%s", zUniOp);
         3461  +    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3452   3462     }
         3463  +  sqlite3TreeViewPop(pView);
  3453   3464   }
  3454         -#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
         3465  +#endif /* SQLITE_DEBUG */
  3455   3466   
  3456         -#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
         3467  +#ifdef SQLITE_DEBUG
  3457   3468   /*
  3458   3469   ** Generate a human-readable explanation of an expression list.
  3459   3470   */
  3460         -void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
         3471  +void sqlite3TreeViewExprList(
         3472  +  TreeView *pView,
         3473  +  const ExprList *pList,
         3474  +  u8 moreToFollow,
         3475  +  const char *zLabel
         3476  +){
  3461   3477     int i;
  3462         -  if( pList==0 || pList->nExpr==0 ){
  3463         -    sqlite3ExplainPrintf(pOut, "(empty-list)");
  3464         -    return;
  3465         -  }else if( pList->nExpr==1 ){
  3466         -    sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
         3478  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
         3479  +  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
         3480  +  if( pList==0 ){
         3481  +    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
  3467   3482     }else{
  3468         -    sqlite3ExplainPush(pOut);
         3483  +    sqlite3TreeViewLine(pView, "%s", zLabel);
  3469   3484       for(i=0; i<pList->nExpr; i++){
  3470         -      sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
  3471         -      sqlite3ExplainPush(pOut);
  3472         -      sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
  3473         -      sqlite3ExplainPop(pOut);
  3474         -      if( pList->a[i].zName ){
         3485  +      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
         3486  +#if 0
         3487  +     if( pList->a[i].zName ){
  3475   3488           sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
  3476   3489         }
  3477   3490         if( pList->a[i].bSpanIsTab ){
  3478   3491           sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
  3479   3492         }
  3480         -      if( i<pList->nExpr-1 ){
  3481         -        sqlite3ExplainNL(pOut);
  3482         -      }
         3493  +#endif
  3483   3494       }
  3484         -    sqlite3ExplainPop(pOut);
  3485   3495     }
         3496  +  sqlite3TreeViewPop(pView);
  3486   3497   }
  3487   3498   #endif /* SQLITE_DEBUG */
  3488   3499   
  3489   3500   /*
  3490   3501   ** Generate code that pushes the value of every element of the given
  3491   3502   ** expression list into a sequence of registers beginning at target.
  3492   3503   **

Changes to src/func.c.

    18     18   #include <assert.h>
    19     19   #include "vdbeInt.h"
    20     20   
    21     21   /*
    22     22   ** Return the collating function associated with a function.
    23     23   */
    24     24   static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
    25         -  return context->pColl;
           25  +  VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1];
           26  +  assert( pOp->opcode==OP_CollSeq );
           27  +  assert( pOp->p4type==P4_COLLSEQ );
           28  +  return pOp->p4.pColl;
    26     29   }
    27     30   
    28     31   /*
    29     32   ** Indicate that the accumulator load should be skipped on this
    30     33   ** iteration of the aggregate loop.
    31     34   */
    32     35   static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
................................................................................
   566    569   /*
   567    570   ** For LIKE and GLOB matching on EBCDIC machines, assume that every
   568    571   ** character is exactly one byte in size.  Also, all characters are
   569    572   ** able to participate in upper-case-to-lower-case mappings in EBCDIC
   570    573   ** whereas only characters less than 0x80 do in ASCII.
   571    574   */
   572    575   #if defined(SQLITE_EBCDIC)
   573         -# define sqlite3Utf8Read(A)    (*((*A)++))
   574         -# define GlobUpperToLower(A)   A = sqlite3UpperToLower[A]
          576  +# define sqlite3Utf8Read(A)        (*((*A)++))
          577  +# define GlobUpperToLower(A)       A = sqlite3UpperToLower[A]
          578  +# define GlobUpperToLowerAscii(A)  A = sqlite3UpperToLower[A]
   575    579   #else
   576         -# define GlobUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
          580  +# define GlobUpperToLower(A)       if( A<=0x7f ){ A = sqlite3UpperToLower[A]; }
          581  +# define GlobUpperToLowerAscii(A)  A = sqlite3UpperToLower[A]
   577    582   #endif
   578    583   
   579    584   static const struct compareInfo globInfo = { '*', '?', '[', 0 };
   580    585   /* The correct SQL-92 behavior is for the LIKE operator to ignore
   581    586   ** case.  Thus  'a' LIKE 'A' would be true. */
   582    587   static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
   583    588   /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
   584    589   ** is case sensitive causing 'a' LIKE 'A' to be false */
   585    590   static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
   586    591   
   587    592   /*
   588    593   ** Compare two UTF-8 strings for equality where the first string can
   589         -** potentially be a "glob" expression.  Return true (1) if they
          594  +** potentially be a "glob" or "like" expression.  Return true (1) if they
   590    595   ** are the same and false (0) if they are different.
   591    596   **
   592    597   ** Globbing rules:
   593    598   **
   594    599   **      '*'       Matches any sequence of zero or more characters.
   595    600   **
   596    601   **      '?'       Matches exactly one character.
................................................................................
   602    607   **
   603    608   ** With the [...] and [^...] matching, a ']' character can be included
   604    609   ** in the list by making it the first character after '[' or '^'.  A
   605    610   ** range of characters can be specified using '-'.  Example:
   606    611   ** "[a-z]" matches any single lower-case letter.  To match a '-', make
   607    612   ** it the last character in the list.
   608    613   **
   609         -** This routine is usually quick, but can be N**2 in the worst case.
          614  +** Like matching rules:
          615  +** 
          616  +**      '%'       Matches any sequence of zero or more characters
          617  +**
          618  +***     '_'       Matches any one character
          619  +**
          620  +**      Ec        Where E is the "esc" character and c is any other
          621  +**                character, including '%', '_', and esc, match exactly c.
   610    622   **
   611         -** Hints: to match '*' or '?', put them in "[]".  Like this:
          623  +** The comments through this routine usually assume glob matching.
   612    624   **
   613         -**         abc[*]xyz        Matches "abc*xyz" only
          625  +** This routine is usually quick, but can be N**2 in the worst case.
   614    626   */
   615    627   static int patternCompare(
   616    628     const u8 *zPattern,              /* The glob pattern */
   617    629     const u8 *zString,               /* The string to compare against the glob */
   618    630     const struct compareInfo *pInfo, /* Information about how to do the compare */
   619    631     u32 esc                          /* The escape character */
   620    632   ){
   621         -  u32 c, c2;
   622         -  int invert;
   623         -  int seen;
   624         -  u8 matchOne = pInfo->matchOne;
   625         -  u8 matchAll = pInfo->matchAll;
   626         -  u8 matchSet = pInfo->matchSet;
   627         -  u8 noCase = pInfo->noCase; 
   628         -  int prevEscape = 0;     /* True if the previous character was 'escape' */
          633  +  u32 c, c2;                       /* Next pattern and input string chars */
          634  +  u32 matchOne = pInfo->matchOne;  /* "?" or "_" */
          635  +  u32 matchAll = pInfo->matchAll;  /* "*" or "%" */
          636  +  u32 matchOther;                  /* "[" or the escape character */
          637  +  u8 noCase = pInfo->noCase;       /* True if uppercase==lowercase */
          638  +  const u8 *zEscaped = 0;          /* One past the last escaped input char */
          639  +  
          640  +  /* The GLOB operator does not have an ESCAPE clause.  And LIKE does not
          641  +  ** have the matchSet operator.  So we either have to look for one or
          642  +  ** the other, never both.  Hence the single variable matchOther is used
          643  +  ** to store the one we have to look for.
          644  +  */
          645  +  matchOther = esc ? esc : pInfo->matchSet;
   629    646   
   630    647     while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
   631         -    if( c==matchAll && !prevEscape ){
          648  +    if( c==matchAll ){  /* Match "*" */
          649  +      /* Skip over multiple "*" characters in the pattern.  If there
          650  +      ** are also "?" characters, skip those as well, but consume a
          651  +      ** single character of the input string for each "?" skipped */
   632    652         while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
   633    653                  || c == matchOne ){
   634    654           if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
   635    655             return 0;
   636    656           }
   637    657         }
   638    658         if( c==0 ){
   639         -        return 1;
   640         -      }else if( c==esc ){
   641         -        c = sqlite3Utf8Read(&zPattern);
   642         -        if( c==0 ){
   643         -          return 0;
   644         -        }
   645         -      }else if( c==matchSet ){
   646         -        assert( esc==0 );         /* This is GLOB, not LIKE */
   647         -        assert( matchSet<0x80 );  /* '[' is a single-byte character */
   648         -        while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
   649         -          SQLITE_SKIP_UTF8(zString);
   650         -        }
   651         -        return *zString!=0;
   652         -      }
   653         -      while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
          659  +        return 1;   /* "*" at the end of the pattern matches */
          660  +      }else if( c==matchOther ){
          661  +        if( esc ){
          662  +          c = sqlite3Utf8Read(&zPattern);
          663  +          if( c==0 ) return 0;
          664  +        }else{
          665  +          /* "[...]" immediately follows the "*".  We have to do a slow
          666  +          ** recursive search in this case, but it is an unusual case. */
          667  +          assert( matchOther<0x80 );  /* '[' is a single-byte character */
          668  +          while( *zString
          669  +                 && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
          670  +            SQLITE_SKIP_UTF8(zString);
          671  +          }
          672  +          return *zString!=0;
          673  +        }
          674  +      }
          675  +
          676  +      /* At this point variable c contains the first character of the
          677  +      ** pattern string past the "*".  Search in the input string for the
          678  +      ** first matching character and recursively contine the match from
          679  +      ** that point.
          680  +      **
          681  +      ** For a case-insensitive search, set variable cx to be the same as
          682  +      ** c but in the other case and search the input string for either
          683  +      ** c or cx.
          684  +      */
          685  +      if( c<=0x80 ){
          686  +        u32 cx;
   654    687           if( noCase ){
   655         -          GlobUpperToLower(c2);
   656         -          GlobUpperToLower(c);
   657         -          while( c2 != 0 && c2 != c ){
   658         -            c2 = sqlite3Utf8Read(&zString);
   659         -            GlobUpperToLower(c2);
   660         -          }
          688  +          cx = sqlite3Toupper(c);
          689  +          c = sqlite3Tolower(c);
   661    690           }else{
   662         -          while( c2 != 0 && c2 != c ){
   663         -            c2 = sqlite3Utf8Read(&zString);
   664         -          }
          691  +          cx = c;
   665    692           }
   666         -        if( c2==0 ) return 0;
   667         -        if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
          693  +        while( (c2 = *(zString++))!=0 ){
          694  +          if( c2!=c && c2!=cx ) continue;
          695  +          if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
          696  +        }
          697  +      }else{
          698  +        while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
          699  +          if( c2!=c ) continue;
          700  +          if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
          701  +        }
   668    702         }
   669    703         return 0;
   670         -    }else if( c==matchOne && !prevEscape ){
   671         -      if( sqlite3Utf8Read(&zString)==0 ){
   672         -        return 0;
   673         -      }
   674         -    }else if( c==matchSet ){
   675         -      u32 prior_c = 0;
   676         -      assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
   677         -      seen = 0;
   678         -      invert = 0;
   679         -      c = sqlite3Utf8Read(&zString);
   680         -      if( c==0 ) return 0;
   681         -      c2 = sqlite3Utf8Read(&zPattern);
   682         -      if( c2=='^' ){
   683         -        invert = 1;
          704  +    }
          705  +    if( c==matchOther ){
          706  +      if( esc ){
          707  +        c = sqlite3Utf8Read(&zPattern);
          708  +        if( c==0 ) return 0;
          709  +        zEscaped = zPattern;
          710  +      }else{
          711  +        u32 prior_c = 0;
          712  +        int seen = 0;
          713  +        int invert = 0;
          714  +        c = sqlite3Utf8Read(&zString);
          715  +        if( c==0 ) return 0;
   684    716           c2 = sqlite3Utf8Read(&zPattern);
   685         -      }
   686         -      if( c2==']' ){
   687         -        if( c==']' ) seen = 1;
   688         -        c2 = sqlite3Utf8Read(&zPattern);
   689         -      }
   690         -      while( c2 && c2!=']' ){
   691         -        if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
          717  +        if( c2=='^' ){
          718  +          invert = 1;
          719  +          c2 = sqlite3Utf8Read(&zPattern);
          720  +        }
          721  +        if( c2==']' ){
          722  +          if( c==']' ) seen = 1;
   692    723             c2 = sqlite3Utf8Read(&zPattern);
   693         -          if( c>=prior_c && c<=c2 ) seen = 1;
   694         -          prior_c = 0;
   695         -        }else{
   696         -          if( c==c2 ){
   697         -            seen = 1;
          724  +        }
          725  +        while( c2 && c2!=']' ){
          726  +          if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
          727  +            c2 = sqlite3Utf8Read(&zPattern);
          728  +            if( c>=prior_c && c<=c2 ) seen = 1;
          729  +            prior_c = 0;
          730  +          }else{
          731  +            if( c==c2 ){
          732  +              seen = 1;
          733  +            }
          734  +            prior_c = c2;
   698    735             }
   699         -          prior_c = c2;
          736  +          c2 = sqlite3Utf8Read(&zPattern);
          737  +        }
          738  +        if( c2==0 || (seen ^ invert)==0 ){
          739  +          return 0;
   700    740           }
   701         -        c2 = sqlite3Utf8Read(&zPattern);
          741  +        continue;
   702    742         }
   703         -      if( c2==0 || (seen ^ invert)==0 ){
   704         -        return 0;
   705         -      }
   706         -    }else if( esc==c && !prevEscape ){
   707         -      prevEscape = 1;
   708         -    }else{
   709         -      c2 = sqlite3Utf8Read(&zString);
   710         -      if( noCase ){
   711         -        GlobUpperToLower(c);
   712         -        GlobUpperToLower(c2);
   713         -      }
   714         -      if( c!=c2 ){
   715         -        return 0;
   716         -      }
   717         -      prevEscape = 0;
   718    743       }
          744  +    c2 = sqlite3Utf8Read(&zString);
          745  +    if( c==c2 ) continue;
          746  +    if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
          747  +      continue;
          748  +    }
          749  +    if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
          750  +    return 0;
   719    751     }
   720    752     return *zString==0;
   721    753   }
   722    754   
   723    755   /*
   724    756   ** The sqlite3_strglob() interface.
   725    757   */

Changes to src/main.c.

  3454   3454       ** undo this setting.
  3455   3455       */
  3456   3456       case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
  3457   3457         sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
  3458   3458         break;
  3459   3459       }
  3460   3460   
  3461         -#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  3462         -    /*   sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
  3463         -    **                        sqlite3_stmt*,const char**);
  3464         -    **
  3465         -    ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
  3466         -    ** a string that describes the optimized parse tree.  This test-control
  3467         -    ** returns a pointer to that string.
  3468         -    */
  3469         -    case SQLITE_TESTCTRL_EXPLAIN_STMT: {
  3470         -      sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
  3471         -      const char **pzRet = va_arg(ap, const char**);
  3472         -      *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
  3473         -      break;
  3474         -    }
  3475         -#endif
  3476         -
  3477   3461       /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
  3478   3462       **
  3479   3463       ** Set or clear a flag that indicates that the database file is always well-
  3480   3464       ** formed and never corrupt.  This flag is clear by default, indicating that
  3481   3465       ** database files might have arbitrary corruption.  Setting the flag during
  3482   3466       ** testing causes certain assert() statements in the code to be activated
  3483   3467       ** that demonstrat invariants on well-formed database files.

Changes to src/os_unix.c.

  6120   6120   **
  6121   6121   **   *  A constant sqlite3_io_methods object call METHOD that has locking
  6122   6122   **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
  6123   6123   **
  6124   6124   **   *  An I/O method finder function called FINDER that returns a pointer
  6125   6125   **      to the METHOD object in the previous bullet.
  6126   6126   */
  6127         -#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
         6127  +#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK, SHMMAP) \
  6128   6128   static const sqlite3_io_methods METHOD = {                                   \
  6129   6129      VERSION,                    /* iVersion */                                \
  6130   6130      CLOSE,                      /* xClose */                                  \
  6131   6131      unixRead,                   /* xRead */                                   \
  6132   6132      unixWrite,                  /* xWrite */                                  \
  6133   6133      unixTruncate,               /* xTruncate */                               \
  6134   6134      unixSync,                   /* xSync */                                   \
................................................................................
  6135   6135      unixFileSize,               /* xFileSize */                               \
  6136   6136      LOCK,                       /* xLock */                                   \
  6137   6137      UNLOCK,                     /* xUnlock */                                 \
  6138   6138      CKLOCK,                     /* xCheckReservedLock */                      \
  6139   6139      unixFileControl,            /* xFileControl */                            \
  6140   6140      unixSectorSize,             /* xSectorSize */                             \
  6141   6141      unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
  6142         -   unixShmMap,                 /* xShmMap */                                 \
         6142  +   SHMMAP,                     /* xShmMap */                                 \
  6143   6143      unixShmLock,                /* xShmLock */                                \
  6144   6144      unixShmBarrier,             /* xShmBarrier */                             \
  6145   6145      unixShmUnmap,               /* xShmUnmap */                               \
  6146   6146      unixFetch,                  /* xFetch */                                  \
  6147   6147      unixUnfetch,                /* xUnfetch */                                \
  6148   6148   };                                                                           \
  6149   6149   static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
................................................................................
  6161   6161   IOMETHODS(
  6162   6162     posixIoFinder,            /* Finder function name */
  6163   6163     posixIoMethods,           /* sqlite3_io_methods object name */
  6164   6164     3,                        /* shared memory and mmap are enabled */
  6165   6165     unixClose,                /* xClose method */
  6166   6166     unixLock,                 /* xLock method */
  6167   6167     unixUnlock,               /* xUnlock method */
  6168         -  unixCheckReservedLock     /* xCheckReservedLock method */
         6168  +  unixCheckReservedLock,    /* xCheckReservedLock method */
         6169  +  unixShmMap                /* xShmMap method */
  6169   6170   )
  6170   6171   IOMETHODS(
  6171   6172     nolockIoFinder,           /* Finder function name */
  6172   6173     nolockIoMethods,          /* sqlite3_io_methods object name */
  6173   6174     2,                        /* shared memory is enabled */
  6174   6175     nolockClose,              /* xClose method */
  6175   6176     nolockLock,               /* xLock method */
  6176   6177     nolockUnlock,             /* xUnlock method */
  6177         -  nolockCheckReservedLock   /* xCheckReservedLock method */
         6178  +  nolockCheckReservedLock,  /* xCheckReservedLock method */
         6179  +  0                         /* xShmMap method */
  6178   6180   )
  6179   6181   IOMETHODS(
  6180   6182     dotlockIoFinder,          /* Finder function name */
  6181   6183     dotlockIoMethods,         /* sqlite3_io_methods object name */
  6182   6184     1,                        /* shared memory is disabled */
  6183   6185     dotlockClose,             /* xClose method */
  6184   6186     dotlockLock,              /* xLock method */
  6185   6187     dotlockUnlock,            /* xUnlock method */
  6186         -  dotlockCheckReservedLock  /* xCheckReservedLock method */
         6188  +  dotlockCheckReservedLock, /* xCheckReservedLock method */
         6189  +  0                         /* xShmMap method */
  6187   6190   )
  6188   6191   
  6189   6192   #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
  6190   6193   IOMETHODS(
  6191   6194     flockIoFinder,            /* Finder function name */
  6192   6195     flockIoMethods,           /* sqlite3_io_methods object name */
  6193   6196     1,                        /* shared memory is disabled */
  6194   6197     flockClose,               /* xClose method */
  6195   6198     flockLock,                /* xLock method */
  6196   6199     flockUnlock,              /* xUnlock method */
  6197         -  flockCheckReservedLock    /* xCheckReservedLock method */
         6200  +  flockCheckReservedLock,   /* xCheckReservedLock method */
         6201  +  0                         /* xShmMap method */
  6198   6202   )
  6199   6203   #endif
  6200   6204   
  6201   6205   #if OS_VXWORKS
  6202   6206   IOMETHODS(
  6203   6207     semIoFinder,              /* Finder function name */
  6204   6208     semIoMethods,             /* sqlite3_io_methods object name */
  6205   6209     1,                        /* shared memory is disabled */
  6206   6210     semClose,                 /* xClose method */
  6207   6211     semLock,                  /* xLock method */
  6208   6212     semUnlock,                /* xUnlock method */
  6209         -  semCheckReservedLock      /* xCheckReservedLock method */
         6213  +  semCheckReservedLock,     /* xCheckReservedLock method */
         6214  +  0                         /* xShmMap method */
  6210   6215   )
  6211   6216   #endif
  6212   6217   
  6213   6218   #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  6214   6219   IOMETHODS(
  6215   6220     afpIoFinder,              /* Finder function name */
  6216   6221     afpIoMethods,             /* sqlite3_io_methods object name */
  6217   6222     1,                        /* shared memory is disabled */
  6218   6223     afpClose,                 /* xClose method */
  6219   6224     afpLock,                  /* xLock method */
  6220   6225     afpUnlock,                /* xUnlock method */
  6221         -  afpCheckReservedLock      /* xCheckReservedLock method */
         6226  +  afpCheckReservedLock,     /* xCheckReservedLock method */
         6227  +  0                         /* xShmMap method */
  6222   6228   )
  6223   6229   #endif
  6224   6230   
  6225   6231   /*
  6226   6232   ** The proxy locking method is a "super-method" in the sense that it
  6227   6233   ** opens secondary file descriptors for the conch and lock files and
  6228   6234   ** it uses proxy, dot-file, AFP, and flock() locking methods on those
................................................................................
  6239   6245   IOMETHODS(
  6240   6246     proxyIoFinder,            /* Finder function name */
  6241   6247     proxyIoMethods,           /* sqlite3_io_methods object name */
  6242   6248     2,                        /* shared memory is enabled */
  6243   6249     proxyClose,               /* xClose method */
  6244   6250     proxyLock,                /* xLock method */
  6245   6251     proxyUnlock,              /* xUnlock method */
  6246         -  proxyCheckReservedLock    /* xCheckReservedLock method */
         6252  +  proxyCheckReservedLock,   /* xCheckReservedLock method */
         6253  +  0                         /* xShmMap method */
  6247   6254   )
  6248   6255   #endif
  6249   6256   
  6250   6257   /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
  6251   6258   #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  6252   6259   IOMETHODS(
  6253   6260     nfsIoFinder,               /* Finder function name */
  6254   6261     nfsIoMethods,              /* sqlite3_io_methods object name */
  6255   6262     1,                         /* shared memory is disabled */
  6256   6263     unixClose,                 /* xClose method */
  6257   6264     unixLock,                  /* xLock method */
  6258   6265     nfsUnlock,                 /* xUnlock method */
  6259         -  unixCheckReservedLock      /* xCheckReservedLock method */
         6266  +  unixCheckReservedLock,     /* xCheckReservedLock method */
         6267  +  0                          /* xShmMap method */
  6260   6268   )
  6261   6269   #endif
  6262   6270   
  6263   6271   #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  6264   6272   /* 
  6265   6273   ** This "finder" function attempts to determine the best locking strategy 
  6266   6274   ** for the database file "filePath".  It then returns the sqlite3_io_methods

Changes to src/parse.y.

   395    395   %endif  SQLITE_OMIT_VIEW
   396    396   
   397    397   //////////////////////// The SELECT statement /////////////////////////////////
   398    398   //
   399    399   cmd ::= select(X).  {
   400    400     SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
   401    401     sqlite3Select(pParse, X, &dest);
   402         -  sqlite3ExplainBegin(pParse->pVdbe);
   403         -  sqlite3ExplainSelect(pParse->pVdbe, X);
   404         -  sqlite3ExplainFinish(pParse->pVdbe);
   405    402     sqlite3SelectDelete(pParse->db, X);
   406    403   }
   407    404   
   408    405   %type select {Select*}
   409    406   %destructor select {sqlite3SelectDelete(pParse->db, $$);}
   410    407   %type selectnowith {Select*}
   411    408   %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}

Changes to src/printf.c.

  1052   1052     va_end(ap);
  1053   1053     sqlite3StrAccumFinish(&acc);
  1054   1054     fprintf(stdout,"%s", zBuf);
  1055   1055     fflush(stdout);
  1056   1056   }
  1057   1057   #endif
  1058   1058   
         1059  +#ifdef SQLITE_DEBUG
         1060  +/*************************************************************************
         1061  +** Routines for implementing the "TreeView" display of hierarchical
         1062  +** data structures for debugging.
         1063  +**
         1064  +** The main entry points (coded elsewhere) are:
         1065  +**     sqlite3TreeViewExpr(0, pExpr, 0);
         1066  +**     sqlite3TreeViewExprList(0, pList, 0, 0);
         1067  +**     sqlite3TreeViewSelect(0, pSelect, 0);
         1068  +** Insert calls to those routines while debugging in order to display
         1069  +** a diagram of Expr, ExprList, and Select objects.
         1070  +**
         1071  +*/
         1072  +/* Add a new subitem to the tree.  The moreToFollow flag indicates that this
         1073  +** is not the last item in the tree. */
         1074  +TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
         1075  +  if( p==0 ){
         1076  +    p = sqlite3_malloc( sizeof(*p) );
         1077  +    if( p==0 ) return 0;
         1078  +    memset(p, 0, sizeof(*p));
         1079  +  }else{
         1080  +    p->iLevel++;
         1081  +  }
         1082  +  assert( moreToFollow==0 || moreToFollow==1 );
         1083  +  if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
         1084  +  return p;
         1085  +}
         1086  +/* Finished with one layer of the tree */
         1087  +void sqlite3TreeViewPop(TreeView *p){
         1088  +  if( p==0 ) return;
         1089  +  p->iLevel--;
         1090  +  if( p->iLevel<0 ) sqlite3_free(p);
         1091  +}
         1092  +/* Generate a single line of output for the tree, with a prefix that contains
         1093  +** all the appropriate tree lines */
         1094  +void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
         1095  +  va_list ap;
         1096  +  int i;
         1097  +  StrAccum acc;
         1098  +  char zBuf[500];
         1099  +  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
         1100  +  acc.useMalloc = 0;
         1101  +  if( p ){
         1102  +    for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
         1103  +      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
         1104  +    }
         1105  +    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
         1106  +  }
         1107  +  va_start(ap, zFormat);
         1108  +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
         1109  +  va_end(ap);
         1110  +  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
         1111  +  sqlite3StrAccumFinish(&acc);
         1112  +  fprintf(stdout,"%s", zBuf);
         1113  +  fflush(stdout);
         1114  +}
         1115  +/* Shorthand for starting a new tree item that consists of a single label */
         1116  +void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
         1117  +  p = sqlite3TreeViewPush(p, moreToFollow);
         1118  +  sqlite3TreeViewLine(p, "%s", zLabel);
         1119  +}
         1120  +#endif /* SQLITE_DEBUG */
         1121  +
  1059   1122   /*
  1060   1123   ** variable-argument wrapper around sqlite3VXPrintf().
  1061   1124   */
  1062   1125   void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
  1063   1126     va_list ap;
  1064   1127     va_start(ap,zFormat);
  1065   1128     sqlite3VXPrintf(p, bFlags, zFormat, ap);
  1066   1129     va_end(ap);
  1067   1130   }

Changes to src/select.c.

  3637   3637       }
  3638   3638     }
  3639   3639   
  3640   3640     /* Finially, delete what is left of the subquery and return
  3641   3641     ** success.
  3642   3642     */
  3643   3643     sqlite3SelectDelete(db, pSub1);
         3644  +
         3645  +#if SELECTTRACE_ENABLED
         3646  +  if( sqlite3SelectTrace & 0x100 ){
         3647  +    sqlite3DebugPrintf("After flattening:\n");
         3648  +    sqlite3TreeViewSelect(0, p, 0);
         3649  +  }
         3650  +#endif
  3644   3651   
  3645   3652     return 1;
  3646   3653   }
  3647   3654   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  3648   3655   
  3649   3656   /*
  3650   3657   ** Based on the contents of the AggInfo structure indicated by the first
................................................................................
  4645   4652     if( p==0 || db->mallocFailed || pParse->nErr ){
  4646   4653       return 1;
  4647   4654     }
  4648   4655     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  4649   4656     memset(&sAggInfo, 0, sizeof(sAggInfo));
  4650   4657   #if SELECTTRACE_ENABLED
  4651   4658     pParse->nSelectIndent++;
  4652         -  SELECTTRACE(1,pParse,p, ("begin processing\n"));
         4659  +  SELECTTRACE(1,pParse,p, ("begin processing:\n"));
         4660  +  if( sqlite3SelectTrace & 0x100 ){
         4661  +    sqlite3TreeViewSelect(0, p, 0);
         4662  +  }
  4653   4663   #endif
  4654   4664   
  4655   4665     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
  4656   4666     assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
  4657   4667     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
  4658   4668     assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
  4659   4669     if( IgnorableOrderby(pDest) ){
................................................................................
  5413   5423   #if SELECTTRACE_ENABLED
  5414   5424     SELECTTRACE(1,pParse,p,("end processing\n"));
  5415   5425     pParse->nSelectIndent--;
  5416   5426   #endif
  5417   5427     return rc;
  5418   5428   }
  5419   5429   
  5420         -#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
         5430  +#ifdef SQLITE_DEBUG
  5421   5431   void sqlite3PrintExpr(Expr *p);
  5422   5432   void sqlite3PrintExprList(ExprList *pList);
  5423   5433   void sqlite3PrintSelect(Select *p, int indent);
  5424   5434   /*
  5425   5435   ** Generate a human-readable description of a the Select object.
  5426   5436   */
  5427         -static void explainOneSelect(Vdbe *pVdbe, Select *p){
  5428         -  sqlite3ExplainPrintf(pVdbe, "SELECT ");
  5429         -  if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
  5430         -    if( p->selFlags & SF_Distinct ){
  5431         -      sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
  5432         -    }
  5433         -    if( p->selFlags & SF_Aggregate ){
  5434         -      sqlite3ExplainPrintf(pVdbe, "agg_flag ");
  5435         -    }
  5436         -    sqlite3ExplainNL(pVdbe);
  5437         -    sqlite3ExplainPrintf(pVdbe, "   ");
  5438         -  }
  5439         -  sqlite3ExplainExprList(pVdbe, p->pEList);
  5440         -  sqlite3ExplainNL(pVdbe);
         5437  +void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
         5438  +  int n = 0;
         5439  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
         5440  +  sqlite3TreeViewLine(pView, "SELECT%s%s",
         5441  +    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
         5442  +    ((p->selFlags & SF_Aggregate) ? " agg_flag" : "")
         5443  +  );
         5444  +  if( p->pSrc && p->pSrc->nSrc ) n++;
         5445  +  if( p->pWhere ) n++;
         5446  +  if( p->pGroupBy ) n++;
         5447  +  if( p->pHaving ) n++;
         5448  +  if( p->pOrderBy ) n++;
         5449  +  if( p->pLimit ) n++;
         5450  +  if( p->pOffset ) n++;
         5451  +  if( p->pPrior ) n++;
         5452  +  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
  5441   5453     if( p->pSrc && p->pSrc->nSrc ){
  5442   5454       int i;
  5443         -    sqlite3ExplainPrintf(pVdbe, "FROM ");
  5444         -    sqlite3ExplainPush(pVdbe);
         5455  +    pView = sqlite3TreeViewPush(pView, (n--)>0);
         5456  +    sqlite3TreeViewLine(pView, "FROM");
  5445   5457       for(i=0; i<p->pSrc->nSrc; i++){
  5446   5458         struct SrcList_item *pItem = &p->pSrc->a[i];
  5447         -      sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
  5448         -      if( pItem->pSelect ){
  5449         -        sqlite3ExplainSelect(pVdbe, pItem->pSelect);
  5450         -        if( pItem->pTab ){
  5451         -          sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
  5452         -        }
         5459  +      StrAccum x;
         5460  +      char zLine[100];
         5461  +      sqlite3StrAccumInit(&x, zLine, sizeof(zLine), 0);
         5462  +      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
         5463  +      if( pItem->zDatabase ){
         5464  +        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
  5453   5465         }else if( pItem->zName ){
  5454         -        sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
         5466  +        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
         5467  +      }
         5468  +      if( pItem->pTab ){
         5469  +        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
  5455   5470         }
  5456   5471         if( pItem->zAlias ){
  5457         -        sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
         5472  +        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
  5458   5473         }
  5459   5474         if( pItem->jointype & JT_LEFT ){
  5460         -        sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
         5475  +        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
         5476  +      }
         5477  +      sqlite3StrAccumFinish(&x);
         5478  +      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
         5479  +      if( pItem->pSelect ){
         5480  +        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
  5461   5481         }
  5462         -      sqlite3ExplainNL(pVdbe);
         5482  +      sqlite3TreeViewPop(pView);
  5463   5483       }
  5464         -    sqlite3ExplainPop(pVdbe);
         5484  +    sqlite3TreeViewPop(pView);
  5465   5485     }
  5466   5486     if( p->pWhere ){
  5467         -    sqlite3ExplainPrintf(pVdbe, "WHERE ");
  5468         -    sqlite3ExplainExpr(pVdbe, p->pWhere);
  5469         -    sqlite3ExplainNL(pVdbe);
         5487  +    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
         5488  +    sqlite3TreeViewExpr(pView, p->pWhere, 0);
         5489  +    sqlite3TreeViewPop(pView);
  5470   5490     }
  5471   5491     if( p->pGroupBy ){
  5472         -    sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
  5473         -    sqlite3ExplainExprList(pVdbe, p->pGroupBy);
  5474         -    sqlite3ExplainNL(pVdbe);
         5492  +    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
  5475   5493     }
  5476   5494     if( p->pHaving ){
  5477         -    sqlite3ExplainPrintf(pVdbe, "HAVING ");
  5478         -    sqlite3ExplainExpr(pVdbe, p->pHaving);
  5479         -    sqlite3ExplainNL(pVdbe);
         5495  +    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
         5496  +    sqlite3TreeViewExpr(pView, p->pHaving, 0);
         5497  +    sqlite3TreeViewPop(pView);
  5480   5498     }
  5481   5499     if( p->pOrderBy ){
  5482         -    sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
  5483         -    sqlite3ExplainExprList(pVdbe, p->pOrderBy);
  5484         -    sqlite3ExplainNL(pVdbe);
         5500  +    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
  5485   5501     }
  5486   5502     if( p->pLimit ){
  5487         -    sqlite3ExplainPrintf(pVdbe, "LIMIT ");
  5488         -    sqlite3ExplainExpr(pVdbe, p->pLimit);
  5489         -    sqlite3ExplainNL(pVdbe);
         5503  +    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
         5504  +    sqlite3TreeViewExpr(pView, p->pLimit, 0);
         5505  +    sqlite3TreeViewPop(pView);
  5490   5506     }
  5491   5507     if( p->pOffset ){
  5492         -    sqlite3ExplainPrintf(pVdbe, "OFFSET ");
  5493         -    sqlite3ExplainExpr(pVdbe, p->pOffset);
  5494         -    sqlite3ExplainNL(pVdbe);
         5508  +    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
         5509  +    sqlite3TreeViewExpr(pView, p->pOffset, 0);
         5510  +    sqlite3TreeViewPop(pView);
  5495   5511     }
  5496         -}
  5497         -void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
  5498         -  if( p==0 ){
  5499         -    sqlite3ExplainPrintf(pVdbe, "(null-select)");
  5500         -    return;
         5512  +  if( p->pPrior ){
         5513  +    const char *zOp = "UNION";
         5514  +    switch( p->op ){
         5515  +      case TK_ALL:         zOp = "UNION ALL";  break;
         5516  +      case TK_INTERSECT:   zOp = "INTERSECT";  break;
         5517  +      case TK_EXCEPT:      zOp = "EXCEPT";     break;
         5518  +    }
         5519  +    sqlite3TreeViewItem(pView, zOp, (n--)>0);
         5520  +    sqlite3TreeViewSelect(pView, p->pPrior, 0);
         5521  +    sqlite3TreeViewPop(pView);
  5501   5522     }
  5502         -  sqlite3ExplainPush(pVdbe);
  5503         -  while( p ){
  5504         -    explainOneSelect(pVdbe, p);
  5505         -    p = p->pNext;
  5506         -    if( p==0 ) break;
  5507         -    sqlite3ExplainNL(pVdbe);
  5508         -    sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
  5509         -  }
  5510         -  sqlite3ExplainPrintf(pVdbe, "END");
  5511         -  sqlite3ExplainPop(pVdbe);
         5523  +  sqlite3TreeViewPop(pView);
  5512   5524   }
  5513         -
  5514         -/* End of the structure debug printing code
  5515         -*****************************************************************************/
  5516         -#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
         5525  +#endif /* SQLITE_DEBUG */

Changes to src/shell.c.

  1349   1349               fprintf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
  1350   1350             }
  1351   1351           }
  1352   1352           sqlite3_finalize(pExplain);
  1353   1353           sqlite3_free(zEQP);
  1354   1354         }
  1355   1355   
  1356         -      /* Output TESTCTRL_EXPLAIN text of requested */
  1357         -      if( pArg && pArg->mode==MODE_Explain ){
  1358         -        const char *zExplain = 0;
  1359         -        sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
  1360         -        if( zExplain && zExplain[0] ){
  1361         -          fprintf(pArg->out, "%s", zExplain);
  1362         -        }
  1363         -      }
  1364         -
  1365   1356         /* If the shell is currently in ".explain" mode, gather the extra
  1366   1357         ** data required to add indents to the output.*/
  1367   1358         if( pArg && pArg->mode==MODE_Explain ){
  1368   1359           explain_data_prepare(pArg, pStmt);
  1369   1360         }
  1370   1361   
  1371   1362         /* perform the first step.  this will tell us if we

Changes to src/sqlite.h.in.

  6211   6211   #define SQLITE_TESTCTRL_ASSERT                  12
  6212   6212   #define SQLITE_TESTCTRL_ALWAYS                  13
  6213   6213   #define SQLITE_TESTCTRL_RESERVE                 14
  6214   6214   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  6215   6215   #define SQLITE_TESTCTRL_ISKEYWORD               16
  6216   6216   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6217   6217   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6218         -#define SQLITE_TESTCTRL_EXPLAIN_STMT            19
         6218  +#define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
  6219   6219   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  6220   6220   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  6221   6221   #define SQLITE_TESTCTRL_BYTEORDER               22
  6222   6222   #define SQLITE_TESTCTRL_ISINIT                  23
  6223   6223   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  6224   6224   #define SQLITE_TESTCTRL_LAST                    24
  6225   6225   

Changes to src/sqliteInt.h.

   465    465   
   466    466   /*
   467    467   ** Macros to compute minimum and maximum of two numbers.
   468    468   */
   469    469   #define MIN(A,B) ((A)<(B)?(A):(B))
   470    470   #define MAX(A,B) ((A)>(B)?(A):(B))
   471    471   
          472  +/*
          473  +** Swap two objects of type TYPE.
          474  +*/
          475  +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
          476  +
   472    477   /*
   473    478   ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
   474    479   ** not, there are still machines out there that use EBCDIC.)
   475    480   */
   476    481   #if 'A' == '\301'
   477    482   # define SQLITE_EBCDIC 1
   478    483   #else
................................................................................
   851    856   typedef struct SQLiteThread SQLiteThread;
   852    857   typedef struct SelectDest SelectDest;
   853    858   typedef struct SrcList SrcList;
   854    859   typedef struct StrAccum StrAccum;
   855    860   typedef struct Table Table;
   856    861   typedef struct TableLock TableLock;
   857    862   typedef struct Token Token;
          863  +typedef struct TreeView TreeView;
   858    864   typedef struct Trigger Trigger;
   859    865   typedef struct TriggerPrg TriggerPrg;
   860    866   typedef struct TriggerStep TriggerStep;
   861    867   typedef struct UnpackedRecord UnpackedRecord;
   862    868   typedef struct VTable VTable;
   863    869   typedef struct VtabCtx VtabCtx;
   864    870   typedef struct Walker Walker;
................................................................................
  2231   2237   #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
  2232   2238   #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
  2233   2239   #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
  2234   2240   #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
  2235   2241   #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
  2236   2242   #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
  2237   2243   #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
  2238         -#define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
         2244  +                          /*   0x0080 // not currently used */
  2239   2245   #define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
  2240   2246   #define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
  2241   2247   #define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
  2242   2248   #define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
  2243   2249   #define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
  2244   2250   
  2245   2251   /* Allowed return values from sqlite3WhereIsDistinct()
................................................................................
  2660   2666     Parse *pParse;              /* The Parse structure */
  2661   2667   };
  2662   2668   
  2663   2669   /*
  2664   2670   ** Bitfield flags for P5 value in various opcodes.
  2665   2671   */
  2666   2672   #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
         2673  +#define OPFLAG_EPHEM         0x01    /* OP_Column: Ephemeral output is ok */
  2667   2674   #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
  2668   2675   #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
  2669   2676   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
  2670   2677   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
  2671   2678   #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
  2672   2679   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
  2673   2680   #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
................................................................................
  2928   2935       char *zName;                    /* Name of this CTE */
  2929   2936       ExprList *pCols;                /* List of explicit column names, or NULL */
  2930   2937       Select *pSelect;                /* The definition of this CTE */
  2931   2938       const char *zErr;               /* Error message for circular references */
  2932   2939     } a[1];
  2933   2940   };
  2934   2941   
         2942  +#ifdef SQLITE_DEBUG
         2943  +/*
         2944  +** An instance of the TreeView object is used for printing the content of
         2945  +** data structures on sqlite3DebugPrintf() using a tree-like view.
         2946  +*/
         2947  +struct TreeView {
         2948  +  int iLevel;             /* Which level of the tree we are on */
         2949  +  u8  bLine[100];         /* Draw vertical in column i if bLine[i] is true */
         2950  +};
         2951  +#endif /* SQLITE_DEBUG */
         2952  +
  2935   2953   /*
  2936   2954   ** Assuming zIn points to the first byte of a UTF-8 character,
  2937   2955   ** advance zIn to point to the first byte of the next UTF-8 character.
  2938   2956   */
  2939   2957   #define SQLITE_SKIP_UTF8(zIn) {                        \
  2940   2958     if( (*(zIn++))>=0xc0 ){                              \
  2941   2959       while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
................................................................................
  2993   3011   # define sqlite3Isspace(x)   isspace((unsigned char)(x))
  2994   3012   # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
  2995   3013   # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
  2996   3014   # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
  2997   3015   # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
  2998   3016   # define sqlite3Tolower(x)   tolower((unsigned char)(x))
  2999   3017   #endif
         3018  +int sqlite3IsIdChar(u8);
  3000   3019   
  3001   3020   /*
  3002   3021   ** Internal function prototypes
  3003   3022   */
  3004   3023   #define sqlite3StrICmp sqlite3_stricmp
  3005   3024   int sqlite3Strlen30(const char*);
  3006   3025   #define sqlite3StrNICmp sqlite3_strnicmp
................................................................................
  3091   3110   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
  3092   3111     void sqlite3DebugPrintf(const char*, ...);
  3093   3112   #endif
  3094   3113   #if defined(SQLITE_TEST)
  3095   3114     void *sqlite3TestTextToPtr(const char*);
  3096   3115   #endif
  3097   3116   
  3098         -/* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
  3099         -#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  3100         -  void sqlite3ExplainBegin(Vdbe*);
  3101         -  void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
  3102         -  void sqlite3ExplainNL(Vdbe*);
  3103         -  void sqlite3ExplainPush(Vdbe*);
  3104         -  void sqlite3ExplainPop(Vdbe*);
  3105         -  void sqlite3ExplainFinish(Vdbe*);
  3106         -  void sqlite3ExplainSelect(Vdbe*, Select*);
  3107         -  void sqlite3ExplainExpr(Vdbe*, Expr*);
  3108         -  void sqlite3ExplainExprList(Vdbe*, ExprList*);
  3109         -  const char *sqlite3VdbeExplanation(Vdbe*);
  3110         -#else
  3111         -# define sqlite3ExplainBegin(X)
  3112         -# define sqlite3ExplainSelect(A,B)
  3113         -# define sqlite3ExplainExpr(A,B)
  3114         -# define sqlite3ExplainExprList(A,B)
  3115         -# define sqlite3ExplainFinish(X)
  3116         -# define sqlite3VdbeExplanation(X) 0
         3117  +#if defined(SQLITE_DEBUG)
         3118  +  TreeView *sqlite3TreeViewPush(TreeView*,u8);
         3119  +  void sqlite3TreeViewPop(TreeView*);
         3120  +  void sqlite3TreeViewLine(TreeView*, const char*, ...);
         3121  +  void sqlite3TreeViewItem(TreeView*, const char*, u8);
         3122  +  void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
         3123  +  void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
         3124  +  void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
  3117   3125   #endif
  3118   3126   
  3119   3127   
  3120   3128   void sqlite3SetString(char **, sqlite3*, const char*, ...);
  3121   3129   void sqlite3ErrorMsg(Parse*, const char*, ...);
  3122   3130   int sqlite3Dequote(char*);
  3123   3131   int sqlite3KeywordCode(const unsigned char*, int);
................................................................................
  3291   3299   void sqlite3CommitTransaction(Parse*);
  3292   3300   void sqlite3RollbackTransaction(Parse*);
  3293   3301   void sqlite3Savepoint(Parse*, int, Token*);
  3294   3302   void sqlite3CloseSavepoints(sqlite3 *);
  3295   3303   void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
  3296   3304   int sqlite3ExprIsConstant(Expr*);
  3297   3305   int sqlite3ExprIsConstantNotJoin(Expr*);
  3298         -int sqlite3ExprIsConstantOrFunction(Expr*);
         3306  +int sqlite3ExprIsConstantOrFunction(Expr*, u8);
  3299   3307   int sqlite3ExprIsInteger(Expr*, int*);
  3300   3308   int sqlite3ExprCanBeNull(const Expr*);
  3301   3309   int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  3302   3310   int sqlite3IsRowid(const char*);
  3303   3311   void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
  3304   3312   void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
  3305   3313   int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);

Changes to src/test_multiplex.c.

   998    998         rc = SQLITE_OK;
   999    999         break;
  1000   1000       case SQLITE_FCNTL_SIZE_HINT:
  1001   1001       case SQLITE_FCNTL_CHUNK_SIZE:
  1002   1002         /* no-op these */
  1003   1003         rc = SQLITE_OK;
  1004   1004         break;
         1005  +    case SQLITE_FCNTL_PRAGMA: {
         1006  +      char **aFcntl = (char**)pArg;
         1007  +      if( aFcntl[1] && sqlite3_stricmp(aFcntl[1],"multiplex_truncate")==0 ){
         1008  +        if( aFcntl[2] && aFcntl[2][0] ){
         1009  +          if( sqlite3_stricmp(aFcntl[2], "on")==0
         1010  +           || sqlite3_stricmp(aFcntl[2], "1")==0 ){
         1011  +            pGroup->bTruncate = 1;
         1012  +          }else
         1013  +          if( sqlite3_stricmp(aFcntl[2], "off")==0
         1014  +           || sqlite3_stricmp(aFcntl[2], "0")==0 ){
         1015  +            pGroup->bTruncate = 0;
         1016  +          }
         1017  +        }
         1018  +        aFcntl[0] = sqlite3_mprintf(pGroup->bTruncate ? "on" : "off");
         1019  +        rc = SQLITE_OK;
         1020  +        break;
         1021  +      }
         1022  +      /* If the multiplexor does not handle the pragma, pass it through
         1023  +      ** into the default case. */
         1024  +    }
  1005   1025       default:
  1006   1026         pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
  1007   1027         if( pSubOpen ){
  1008   1028           rc = pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
  1009   1029           if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){
  1010   1030            *(char**)pArg = sqlite3_mprintf("multiplex/%z", *(char**)pArg);
  1011   1031           }

Changes to src/tokenize.c.

    98     98       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
    99     99       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
   100    100       0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
   101    101       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
   102    102   };
   103    103   #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
   104    104   #endif
          105  +int sqlite3IsIdChar(u8 c){ return IdChar(c); }
   105    106   
   106    107   
   107    108   /*
   108    109   ** Return the length of the token that begins at z[0]. 
   109    110   ** Store the token type in *tokenType before returning.
   110    111   */
   111    112   int sqlite3GetToken(const unsigned char *z, int *tokenType){

Changes to src/vdbe.c.

  1553   1553     }
  1554   1554   
  1555   1555     assert( pOp->p4type==P4_FUNCDEF );
  1556   1556     ctx.pFunc = pOp->p4.pFunc;
  1557   1557     ctx.iOp = pc;
  1558   1558     ctx.pVdbe = p;
  1559   1559     MemSetTypeFlag(ctx.pOut, MEM_Null);
  1560         -
  1561   1560     ctx.fErrorOrAux = 0;
  1562         -  if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  1563         -    assert( pOp>aOp );
  1564         -    assert( pOp[-1].p4type==P4_COLLSEQ );
  1565         -    assert( pOp[-1].opcode==OP_CollSeq );
  1566         -    ctx.pColl = pOp[-1].p4.pColl;
  1567         -  }
  1568         -  db->lastRowid = lastRowid;
         1561  +  assert( db->lastRowid==lastRowid );
  1569   1562     (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  1570         -  lastRowid = db->lastRowid;
         1563  +  lastRowid = db->lastRowid;  /* Remember rowid changes made by xFunc */
  1571   1564   
  1572   1565     /* If the function returned an error, throw an exception */
  1573   1566     if( ctx.fErrorOrAux ){
  1574   1567       if( ctx.isError ){
  1575   1568         sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
  1576   1569         rc = ctx.isError;
  1577   1570       }
................................................................................
  3283   3276     pCur->isOrdered = 1;
  3284   3277     pCur->pgnoRoot = p2;
  3285   3278     rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
  3286   3279     pCur->pKeyInfo = pKeyInfo;
  3287   3280     assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
  3288   3281     sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
  3289   3282   
  3290         -  /* Since it performs no memory allocation or IO, the only value that
  3291         -  ** sqlite3BtreeCursor() may return is SQLITE_OK. */
  3292         -  assert( rc==SQLITE_OK );
  3293         -
  3294   3283     /* Set the VdbeCursor.isTable variable. Previous versions of
  3295   3284     ** SQLite used to check if the root-page flags were sane at this point
  3296   3285     ** and report database corruption if they were not, but this check has
  3297   3286     ** since moved into the btree layer.  */  
  3298   3287     pCur->isTable = pOp->p4type!=P4_KEYINFO;
  3299   3288     break;
  3300   3289   }
................................................................................
  4020   4009       if( pC->useRandomRowid ){
  4021   4010         /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
  4022   4011         ** largest possible integer (9223372036854775807) then the database
  4023   4012         ** engine starts picking positive candidate ROWIDs at random until
  4024   4013         ** it finds one that is not previously used. */
  4025   4014         assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
  4026   4015                                ** an AUTOINCREMENT table. */
  4027         -      /* on the first attempt, simply do one more than previous */
  4028         -      v = lastRowid;
  4029         -      v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
  4030         -      v++; /* ensure non-zero */
  4031   4016         cnt = 0;
  4032         -      while(   ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
         4017  +      do{
         4018  +        sqlite3_randomness(sizeof(v), &v);
         4019  +        v &= (MAX_ROWID>>1); v++;  /* Ensure that v is greater than zero */
         4020  +      }while(  ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
  4033   4021                                                    0, &res))==SQLITE_OK)
  4034   4022               && (res==0)
  4035         -            && (++cnt<100)){
  4036         -        /* collision - try another random rowid */
  4037         -        sqlite3_randomness(sizeof(v), &v);
  4038         -        if( cnt<5 ){
  4039         -          /* try "small" random rowids for the initial attempts */
  4040         -          v &= 0xffffff;
  4041         -        }else{
  4042         -          v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
  4043         -        }
  4044         -        v++; /* ensure non-zero */
  4045         -      }
         4023  +            && (++cnt<100));
  4046   4024         if( rc==SQLITE_OK && res==0 ){
  4047   4025           rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
  4048   4026           goto abort_due_to_error;
  4049   4027         }
  4050   4028         assert( v>0 );  /* EV: R-40812-03570 */
  4051   4029       }
  4052   4030       pC->rowidIsValid = 0;
................................................................................
  5634   5612     ctx.pFunc = pOp->p4.pFunc;
  5635   5613     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5636   5614     ctx.pMem = pMem = &aMem[pOp->p3];
  5637   5615     pMem->n++;
  5638   5616     sqlite3VdbeMemInit(&t, db, MEM_Null);
  5639   5617     ctx.pOut = &t;
  5640   5618     ctx.isError = 0;
  5641         -  ctx.pColl = 0;
         5619  +  ctx.pVdbe = p;
         5620  +  ctx.iOp = pc;
  5642   5621     ctx.skipFlag = 0;
  5643         -  if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5644         -    assert( pOp>p->aOp );
  5645         -    assert( pOp[-1].p4type==P4_COLLSEQ );
  5646         -    assert( pOp[-1].opcode==OP_CollSeq );
  5647         -    ctx.pColl = pOp[-1].p4.pColl;
  5648         -  }
  5649   5622     (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  5650   5623     if( ctx.isError ){
  5651   5624       sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
  5652   5625       rc = ctx.isError;
  5653   5626     }
  5654   5627     if( ctx.skipFlag ){
  5655   5628       assert( pOp[-1].opcode==OP_CollSeq );

Changes to src/vdbeInt.h.

   268    268   ** This structure is defined inside of vdbeInt.h because it uses substructures
   269    269   ** (Mem) which are only defined there.
   270    270   */
   271    271   struct sqlite3_context {
   272    272     Mem *pOut;            /* The return value is stored here */
   273    273     FuncDef *pFunc;       /* Pointer to function information */
   274    274     Mem *pMem;            /* Memory cell used to store aggregate context */
   275         -  CollSeq *pColl;       /* Collating sequence */
   276    275     Vdbe *pVdbe;          /* The VM that owns this context */
   277    276     int iOp;              /* Instruction number of OP_Function */
   278    277     int isError;          /* Error code returned by the function. */
   279    278     u8 skipFlag;          /* Skip accumulator loading if true */
   280    279     u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */
   281    280   };
   282    281   
................................................................................
   357    356   #endif
   358    357     i64 iCurrentTime;       /* Value of julianday('now') for this statement */
   359    358     i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
   360    359     i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
   361    360     i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
   362    361     char *zSql;             /* Text of the SQL statement that generated this */
   363    362     void *pFree;            /* Free this when deleting the vdbe */
   364         -#ifdef SQLITE_ENABLE_TREE_EXPLAIN
   365         -  Explain *pExplain;      /* The explainer */
   366         -  char *zExplain;         /* Explanation of data structures */
   367         -#endif
   368    363     VdbeFrame *pFrame;      /* Parent frame */
   369    364     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
   370    365     int nFrame;             /* Number of frames in pFrame list */
   371    366     u32 expmask;            /* Binding to these vars invalidates VM */
   372    367     SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
   373    368     int nOnceFlag;          /* Size of array aOnceFlag[] */
   374    369     u8 *aOnceFlag;          /* Flags for OP_Once */

Changes to src/vdbeaux.c.

   748    748       memset(pOp, 0, sizeof(pOp[0]));
   749    749       pOp->opcode = OP_Noop;
   750    750       if( addr==p->nOp-1 ) p->nOp--;
   751    751     }
   752    752   }
   753    753   
   754    754   /*
   755         -** Remove the last opcode inserted
          755  +** If the last opcode is "op" and it is not a jump destination,
          756  +** then remove it.  Return true if and only if an opcode was removed.
   756    757   */
   757    758   int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
   758    759     if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
   759    760       sqlite3VdbeChangeToNoop(p, p->nOp-1);
   760    761       return 1;
   761    762     }else{
   762    763       return 0;
................................................................................
  2675   2676       sqlite3DbFree(db, pSub);
  2676   2677     }
  2677   2678     for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
  2678   2679     vdbeFreeOpArray(db, p->aOp, p->nOp);
  2679   2680     sqlite3DbFree(db, p->aColName);
  2680   2681     sqlite3DbFree(db, p->zSql);
  2681   2682     sqlite3DbFree(db, p->pFree);
  2682         -#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  2683         -  sqlite3DbFree(db, p->zExplain);
  2684         -  sqlite3DbFree(db, p->pExplain);
  2685         -#endif
  2686   2683   }
  2687   2684   
  2688   2685   /*
  2689   2686   ** Delete an entire VDBE.
  2690   2687   */
  2691   2688   void sqlite3VdbeDelete(Vdbe *p){
  2692   2689     sqlite3 *db;

Changes to src/vdbetrace.c.

   179    179         }
   180    180       }
   181    181     }
   182    182     return sqlite3StrAccumFinish(&out);
   183    183   }
   184    184   
   185    185   #endif /* #ifndef SQLITE_OMIT_TRACE */
   186         -
   187         -/*****************************************************************************
   188         -** The following code implements the data-structure explaining logic
   189         -** for the Vdbe.
   190         -*/
   191         -
   192         -#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
   193         -
   194         -/*
   195         -** Allocate a new Explain object
   196         -*/
   197         -void sqlite3ExplainBegin(Vdbe *pVdbe){
   198         -  if( pVdbe ){
   199         -    Explain *p;
   200         -    sqlite3BeginBenignMalloc();
   201         -    p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
   202         -    if( p ){
   203         -      p->pVdbe = pVdbe;
   204         -      sqlite3_free(pVdbe->pExplain);
   205         -      pVdbe->pExplain = p;
   206         -      sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
   207         -                          SQLITE_MAX_LENGTH);
   208         -      p->str.useMalloc = 2;
   209         -    }else{
   210         -      sqlite3EndBenignMalloc();
   211         -    }
   212         -  }
   213         -}
   214         -
   215         -/*
   216         -** Return true if the Explain ends with a new-line.
   217         -*/
   218         -static int endsWithNL(Explain *p){
   219         -  return p && p->str.zText && p->str.nChar
   220         -           && p->str.zText[p->str.nChar-1]=='\n';
   221         -}
   222         -    
   223         -/*
   224         -** Append text to the indentation
   225         -*/
   226         -void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
   227         -  Explain *p;
   228         -  if( pVdbe && (p = pVdbe->pExplain)!=0 ){
   229         -    va_list ap;
   230         -    if( p->nIndent && endsWithNL(p) ){
   231         -      int n = p->nIndent;
   232         -      if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
   233         -      sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
   234         -    }   
   235         -    va_start(ap, zFormat);
   236         -    sqlite3VXPrintf(&p->str, SQLITE_PRINTF_INTERNAL, zFormat, ap);
   237         -    va_end(ap);
   238         -  }
   239         -}
   240         -
   241         -/*
   242         -** Append a '\n' if there is not already one.
   243         -*/
   244         -void sqlite3ExplainNL(Vdbe *pVdbe){
   245         -  Explain *p;
   246         -  if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
   247         -    sqlite3StrAccumAppend(&p->str, "\n", 1);
   248         -  }
   249         -}
   250         -
   251         -/*
   252         -** Push a new indentation level.  Subsequent lines will be indented
   253         -** so that they begin at the current cursor position.
   254         -*/
   255         -void sqlite3ExplainPush(Vdbe *pVdbe){
   256         -  Explain *p;
   257         -  if( pVdbe && (p = pVdbe->pExplain)!=0 ){
   258         -    if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
   259         -      const char *z = p->str.zText;
   260         -      int i = p->str.nChar-1;
   261         -      int x;
   262         -      while( i>=0 && z[i]!='\n' ){ i--; }
   263         -      x = (p->str.nChar - 1) - i;
   264         -      if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
   265         -        x = p->aIndent[p->nIndent-1];
   266         -      }
   267         -      p->aIndent[p->nIndent] = x;
   268         -    }
   269         -    p->nIndent++;
   270         -  }
   271         -}
   272         -
   273         -/*
   274         -** Pop the indentation stack by one level.
   275         -*/
   276         -void sqlite3ExplainPop(Vdbe *p){
   277         -  if( p && p->pExplain ) p->pExplain->nIndent--;
   278         -}
   279         -
   280         -/*
   281         -** Free the indentation structure
   282         -*/
   283         -void sqlite3ExplainFinish(Vdbe *pVdbe){
   284         -  if( pVdbe && pVdbe->pExplain ){
   285         -    sqlite3_free(pVdbe->zExplain);
   286         -    sqlite3ExplainNL(pVdbe);
   287         -    pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
   288         -    sqlite3_free(pVdbe->pExplain);
   289         -    pVdbe->pExplain = 0;
   290         -    sqlite3EndBenignMalloc();
   291         -  }
   292         -}
   293         -
   294         -/*
   295         -** Return the explanation of a virtual machine.
   296         -*/
   297         -const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
   298         -  return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
   299         -}
   300         -#endif /* defined(SQLITE_DEBUG) */

Changes to src/where.c.

   360    360     assert( TK_GT>TK_EQ && TK_GT<TK_GE );
   361    361     assert( TK_LT>TK_EQ && TK_LT<TK_GE );
   362    362     assert( TK_LE>TK_EQ && TK_LE<TK_GE );
   363    363     assert( TK_GE==TK_EQ+4 );
   364    364     return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
   365    365   }
   366    366   
   367         -/*
   368         -** Swap two objects of type TYPE.
   369         -*/
   370         -#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
   371         -
   372    367   /*
   373    368   ** Commute a comparison operator.  Expressions of the form "X op Y"
   374    369   ** are converted into "Y op X".
   375    370   **
   376    371   ** If left/right precedence rules come into play when determining the
   377    372   ** collating sequence, then COLLATE operators are adjusted to ensure
   378    373   ** that the collating sequence does not change.  For example:
................................................................................
  3525   3520         }
  3526   3521       }
  3527   3522   
  3528   3523       /* Run a separate WHERE clause for each term of the OR clause.  After
  3529   3524       ** eliminating duplicates from other WHERE clauses, the action for each
  3530   3525       ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
  3531   3526       */
  3532         -    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
  3533         -                  WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY;
         3527  +    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
         3528  +                | WHERE_FORCE_TABLE
         3529  +                | WHERE_ONETABLE_ONLY;
  3534   3530       for(ii=0; ii<pOrWc->nTerm; ii++){
  3535   3531         WhereTerm *pOrTerm = &pOrWc->a[ii];
  3536   3532         if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
  3537   3533           WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
  3538   3534           Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
  3539   3535           int j1 = 0;                     /* Address of jump operation */
  3540   3536           if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
  3541   3537             pAndExpr->pLeft = pOrExpr;
  3542   3538             pOrExpr = pAndExpr;
  3543   3539           }
  3544   3540           /* Loop through table entries that match term pOrTerm. */
         3541  +        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
  3545   3542           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
  3546   3543                                         wctrlFlags, iCovCur);
  3547   3544           assert( pSubWInfo || pParse->nErr || db->mallocFailed );
  3548   3545           if( pSubWInfo ){
  3549   3546             WhereLoop *pSubLoop;
  3550   3547             explainOneScan(
  3551   3548                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
................................................................................
  3757   3754         pTerm->wtFlags |= TERM_CODED;
  3758   3755       }
  3759   3756     }
  3760   3757   
  3761   3758     return pLevel->notReady;
  3762   3759   }
  3763   3760   
  3764         -#if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
         3761  +#ifdef WHERETRACE_ENABLED
  3765   3762   /*
  3766         -** Generate "Explanation" text for a WhereTerm.
         3763  +** Print the content of a WhereTerm object
  3767   3764   */
  3768         -static void whereExplainTerm(Vdbe *v, WhereTerm *pTerm){
  3769         -  char zType[4];
  3770         -  memcpy(zType, "...", 4);
  3771         -  if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
  3772         -  if( pTerm->eOperator & WO_EQUIV  ) zType[1] = 'E';
  3773         -  if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
  3774         -  sqlite3ExplainPrintf(v, "%s ", zType);
  3775         -  sqlite3ExplainExpr(v, pTerm->pExpr);
         3765  +static void whereTermPrint(WhereTerm *pTerm, int iTerm){
         3766  +  if( pTerm==0 ){
         3767  +    sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
         3768  +  }else{
         3769  +    char zType[4];
         3770  +    memcpy(zType, "...", 4);
         3771  +    if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
         3772  +    if( pTerm->eOperator & WO_EQUIV  ) zType[1] = 'E';
         3773  +    if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
         3774  +    sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n",
         3775  +                       iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
         3776  +                       pTerm->eOperator);
         3777  +    sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
         3778  +  }
  3776   3779   }
  3777         -#endif /* WHERETRACE_ENABLED && SQLITE_ENABLE_TREE_EXPLAIN */
  3778         -
         3780  +#endif
  3779   3781   
  3780   3782   #ifdef WHERETRACE_ENABLED
  3781   3783   /*
  3782   3784   ** Print a WhereLoop object for debugging purposes
  3783   3785   */
  3784   3786   static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
  3785   3787     WhereInfo *pWInfo = pWC->pWInfo;
................................................................................
  3815   3817     }
  3816   3818     if( p->wsFlags & WHERE_SKIPSCAN ){
  3817   3819       sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip);
  3818   3820     }else{
  3819   3821       sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
  3820   3822     }
  3821   3823     sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
  3822         -#ifdef SQLITE_ENABLE_TREE_EXPLAIN
  3823         -  /* If the 0x100 bit of wheretracing is set, then show all of the constraint
  3824         -  ** expressions in the WhereLoop.aLTerm[] array.
  3825         -  */
  3826         -  if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){  /* WHERETRACE 0x100 */
         3824  +  if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
  3827   3825       int i;
  3828         -    Vdbe *v = pWInfo->pParse->pVdbe;
  3829         -    sqlite3ExplainBegin(v);
  3830   3826       for(i=0; i<p->nLTerm; i++){
  3831         -      WhereTerm *pTerm = p->aLTerm[i];
  3832         -      if( pTerm==0 ) continue;
  3833         -      sqlite3ExplainPrintf(v, "  (%d) #%-2d ", i+1, (int)(pTerm-pWC->a));
  3834         -      sqlite3ExplainPush(v);
  3835         -      whereExplainTerm(v, pTerm);
  3836         -      sqlite3ExplainPop(v);
  3837         -      sqlite3ExplainNL(v);
         3827  +      whereTermPrint(p->aLTerm[i], i);
  3838   3828       }
  3839         -    sqlite3ExplainFinish(v);
  3840         -    sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v));
  3841   3829     }
  3842         -#endif
  3843   3830   }
  3844   3831   #endif
  3845   3832   
  3846   3833   /*
  3847   3834   ** Convert bulk memory into a valid WhereLoop that can be passed
  3848   3835   ** to whereLoopClear harmlessly.
  3849   3836   */
................................................................................
  4355   4342       pNew->u.btree.nEq++;
  4356   4343       pNew->u.btree.nSkip++;
  4357   4344       pNew->aLTerm[pNew->nLTerm++] = 0;
  4358   4345       pNew->wsFlags |= WHERE_SKIPSCAN;
  4359   4346       nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
  4360   4347       if( pTerm ){
  4361   4348         /* TUNING:  When estimating skip-scan for a term that is also indexable,
  4362         -      ** increase the cost of the skip-scan by 2x, to make it a little less
         4349  +      ** multiply the cost of the skip-scan by 2.0, to make it a little less
  4363   4350         ** desirable than the regular index lookup. */
  4364   4351         nIter += 10;  assert( 10==sqlite3LogEst(2) );
  4365   4352       }
  4366   4353       pNew->nOut -= nIter;
         4354  +    /* TUNING:  Because uncertainties in the estimates for skip-scan queries,
         4355  +    ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
         4356  +    nIter += 5;
  4367   4357       whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
  4368   4358       pNew->nOut = saved_nOut;
  4369   4359       pNew->u.btree.nEq = saved_nEq;
  4370   4360       pNew->u.btree.nSkip = saved_nSkip;
  4371   4361     }
  4372   4362     for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
  4373   4363       u16 eOp = pTerm->eOperator;   /* Shorthand for pTerm->eOperator */
................................................................................
  4714   4704         if( termCanDriveIndex(pTerm, pSrc, 0) ){
  4715   4705           pNew->u.btree.nEq = 1;
  4716   4706           pNew->u.btree.nSkip = 0;
  4717   4707           pNew->u.btree.pIndex = 0;
  4718   4708           pNew->nLTerm = 1;
  4719   4709           pNew->aLTerm[0] = pTerm;
  4720   4710           /* TUNING: One-time cost for computing the automatic index is
  4721         -        ** approximately 7*N*log2(N) where N is the number of rows in
  4722         -        ** the table being indexed. */
  4723         -        pNew->rSetup = rLogSize + rSize + 28;  assert( 28==sqlite3LogEst(7) );
         4711  +        ** estimated to be X*N*log2(N) where N is the number of rows in
         4712  +        ** the table being indexed and where X is 7 (LogEst=28) for normal
         4713  +        ** tables or 1.375 (LogEst=4) for views and subqueries.  The value
         4714  +        ** of X is smaller for views and subqueries so that the query planner
         4715  +        ** will be more aggressive about generating automatic indexes for
         4716  +        ** those objects, since there is no opportunity to add schema
         4717  +        ** indexes on subqueries and views. */
         4718  +        pNew->rSetup = rLogSize + rSize + 4;
         4719  +        if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
         4720  +          pNew->rSetup += 24;
         4721  +        }
  4724   4722           ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
  4725   4723           /* TUNING: Each index lookup yields 20 rows in the table.  This
  4726   4724           ** is more than the usual guess of 10 rows, since we have no way
  4727   4725           ** of knowing how selective the index will ultimately be.  It would
  4728   4726           ** not be unreasonable to make this value much larger. */
  4729   4727           pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
  4730   4728           pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
................................................................................
  5004   5002     int iCur;
  5005   5003     WhereClause tempWC;
  5006   5004     WhereLoopBuilder sSubBuild;
  5007   5005     WhereOrSet sSum, sCur;
  5008   5006     struct SrcList_item *pItem;
  5009   5007     
  5010   5008     pWC = pBuilder->pWC;
  5011         -  if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
  5012   5009     pWCEnd = pWC->a + pWC->nTerm;
  5013   5010     pNew = pBuilder->pNew;
  5014   5011     memset(&sSum, 0, sizeof(sSum));
  5015   5012     pItem = pWInfo->pTabList->a + pNew->iTab;
  5016   5013     iCur = pItem->iCursor;
  5017   5014   
  5018   5015     for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
................................................................................
  5025   5022         int once = 1;
  5026   5023         int i, j;
  5027   5024       
  5028   5025         sSubBuild = *pBuilder;
  5029   5026         sSubBuild.pOrderBy = 0;
  5030   5027         sSubBuild.pOrSet = &sCur;
  5031   5028   
         5029  +      WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
  5032   5030         for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
  5033   5031           if( (pOrTerm->eOperator & WO_AND)!=0 ){
  5034   5032             sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
  5035   5033           }else if( pOrTerm->leftCursor==iCur ){
  5036   5034             tempWC.pWInfo = pWC->pWInfo;
  5037   5035             tempWC.pOuter = pWC;
  5038   5036             tempWC.op = TK_AND;
................................................................................
  5039   5037             tempWC.nTerm = 1;
  5040   5038             tempWC.a = pOrTerm;
  5041   5039             sSubBuild.pWC = &tempWC;
  5042   5040           }else{
  5043   5041             continue;
  5044   5042           }
  5045   5043           sCur.n = 0;
         5044  +#ifdef WHERETRACE_ENABLED
         5045  +        WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", 
         5046  +                   (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
         5047  +        if( sqlite3WhereTrace & 0x400 ){
         5048  +          for(i=0; i<sSubBuild.pWC->nTerm; i++){
         5049  +            whereTermPrint(&sSubBuild.pWC->a[i], i);
         5050  +          }
         5051  +        }
         5052  +#endif
  5046   5053   #ifndef SQLITE_OMIT_VIRTUALTABLE
  5047   5054           if( IsVirtual(pItem->pTab) ){
  5048   5055             rc = whereLoopAddVirtual(&sSubBuild, mExtra);
  5049   5056           }else
  5050   5057   #endif
  5051   5058           {
  5052   5059             rc = whereLoopAddBtree(&sSubBuild, mExtra);
  5053   5060           }
         5061  +        if( rc==SQLITE_OK ){
         5062  +          rc = whereLoopAddOr(&sSubBuild, mExtra);
         5063  +        }
  5054   5064           assert( rc==SQLITE_OK || sCur.n==0 );
  5055   5065           if( sCur.n==0 ){
  5056   5066             sSum.n = 0;
  5057   5067             break;
  5058   5068           }else if( once ){
  5059   5069             whereOrMove(&sSum, &sCur);
  5060   5070             once = 0;
................................................................................
  5091   5101           ** the planner may elect to "OR" together a full-table scan and an
  5092   5102           ** index lookup. And other similarly odd results.  */
  5093   5103           pNew->rRun = sSum.a[i].rRun + 1;
  5094   5104           pNew->nOut = sSum.a[i].nOut;
  5095   5105           pNew->prereq = sSum.a[i].prereq;
  5096   5106           rc = whereLoopInsert(pBuilder, pNew);
  5097   5107         }
         5108  +      WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
  5098   5109       }
  5099   5110     }
  5100   5111     return rc;
  5101   5112   }
  5102   5113   
  5103   5114   /*
  5104   5115   ** Add all WhereLoop objects for all tables 
................................................................................
  6157   6168         pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
  6158   6169         pWInfo->pOrderBy = pResultSet;
  6159   6170       }
  6160   6171     }
  6161   6172   
  6162   6173     /* Construct the WhereLoop objects */
  6163   6174     WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
         6175  +#if defined(WHERETRACE_ENABLED)
  6164   6176     /* Display all terms of the WHERE clause */
  6165         -#if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
  6166   6177     if( sqlite3WhereTrace & 0x100 ){
  6167   6178       int i;
  6168         -    Vdbe *v = pParse->pVdbe;
  6169         -    sqlite3ExplainBegin(v);
  6170   6179       for(i=0; i<sWLB.pWC->nTerm; i++){
  6171         -      sqlite3ExplainPrintf(v, "#%-2d ", i);
  6172         -      sqlite3ExplainPush(v);
  6173         -      whereExplainTerm(v, &sWLB.pWC->a[i]);
  6174         -      sqlite3ExplainPop(v);
  6175         -      sqlite3ExplainNL(v);
         6180  +      whereTermPrint(&sWLB.pWC->a[i], i);
  6176   6181       }
  6177         -    sqlite3ExplainFinish(v);
  6178         -    sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v));
  6179   6182     }
  6180   6183   #endif
         6184  +
  6181   6185     if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
  6182   6186       rc = whereLoopAddAll(&sWLB);
  6183   6187       if( rc ) goto whereBeginError;
  6184   6188     
  6185   6189       /* Display all of the WhereLoop objects if wheretrace is enabled */
  6186   6190   #ifdef WHERETRACE_ENABLED /* !=0 */
  6187   6191       if( sqlite3WhereTrace ){

Changes to test/autoindex1.test.

   408    408     EXPLAIN QUERY PLAN
   409    409     SELECT * FROM 
   410    410           data JOIN mimetypes ON (data.mimetype_id=mimetypes._id) 
   411    411                JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id) 
   412    412                JOIN accounts ON (raw_contacts.account_id=accounts._id)
   413    413      WHERE mimetypes._id=10 AND data14 IS NOT NULL;
   414    414   } {/SEARCH TABLE data .*SEARCH TABLE raw_contacts/}
          415  +
          416  +# Another test case from an important user of SQLite.  The key feature of
          417  +# this test is that the "aggindex" subquery should make use of an
          418  +# automatic index.  If it does, the query is fast.  If it does not, the
          419  +# query is deathly slow.  It worked OK in 3.7.17 but started going slow
          420  +# with version 3.8.0.  The problem was fixed for 3.8.7 by reducing the
          421  +# cost estimate for automatic indexes on views and subqueries.
          422  +#
          423  +db close
          424  +forcedelete test.db
          425  +sqlite3 db test.db
          426  +do_execsql_test autoindex1-900 {
          427  +  CREATE TABLE messages (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, message_id, document_id BLOB, in_reply_to, remote_id INTEGER, sender INTEGER, subject_prefix, subject INTEGER, date_sent INTEGER, date_received INTEGER, date_created INTEGER, date_last_viewed INTEGER, mailbox INTEGER, remote_mailbox INTEGER, original_mailbox INTEGER, flags INTEGER, read, flagged, size INTEGER, color, encoding, type INTEGER, pad, conversation_id INTEGER DEFAULT -1, snippet TEXT DEFAULT NULL, fuzzy_ancestor INTEGER DEFAULT NULL, automated_conversation INTEGER DEFAULT 0, root_status INTEGER DEFAULT -1, conversation_position INTEGER DEFAULT -1);
          428  +  CREATE INDEX date_index ON messages(date_received);
          429  +  CREATE INDEX date_last_viewed_index ON messages(date_last_viewed);
          430  +  CREATE INDEX date_created_index ON messages(date_created);
          431  +  CREATE INDEX message_message_id_mailbox_index ON messages(message_id, mailbox);
          432  +  CREATE INDEX message_document_id_index ON messages(document_id);
          433  +  CREATE INDEX message_read_index ON messages(read);
          434  +  CREATE INDEX message_flagged_index ON messages(flagged);
          435  +  CREATE INDEX message_mailbox_index ON messages(mailbox, date_received);
          436  +  CREATE INDEX message_remote_mailbox_index ON messages(remote_mailbox, remote_id);
          437  +  CREATE INDEX message_type_index ON messages(type);
          438  +  CREATE INDEX message_conversation_id_conversation_position_index ON messages(conversation_id, conversation_position);
          439  +  CREATE INDEX message_fuzzy_ancestor_index ON messages(fuzzy_ancestor);
          440  +  CREATE INDEX message_subject_fuzzy_ancestor_index ON messages(subject, fuzzy_ancestor);
          441  +  CREATE INDEX message_sender_subject_automated_conversation_index ON messages(sender, subject, automated_conversation);
          442  +  CREATE INDEX message_sender_index ON messages(sender);
          443  +  CREATE INDEX message_root_status ON messages(root_status);
          444  +  CREATE TABLE subjects (ROWID INTEGER PRIMARY KEY, subject COLLATE RTRIM, normalized_subject COLLATE RTRIM);
          445  +  CREATE INDEX subject_subject_index ON subjects(subject);
          446  +  CREATE INDEX subject_normalized_subject_index ON subjects(normalized_subject);
          447  +  CREATE TABLE addresses (ROWID INTEGER PRIMARY KEY, address COLLATE NOCASE, comment, UNIQUE(address, comment));
          448  +  CREATE INDEX addresses_address_index ON addresses(address);
          449  +  CREATE TABLE mailboxes (ROWID INTEGER PRIMARY KEY, url UNIQUE, total_count INTEGER DEFAULT 0, unread_count INTEGER DEFAULT 0, unseen_count INTEGER DEFAULT 0, deleted_count INTEGER DEFAULT 0, unread_count_adjusted_for_duplicates INTEGER DEFAULT 0, change_identifier, source INTEGER, alleged_change_identifier);
          450  +  CREATE INDEX mailboxes_source_index ON mailboxes(source);
          451  +  CREATE TABLE labels (ROWID INTEGER PRIMARY KEY, message_id INTEGER NOT NULL, mailbox_id INTEGER NOT NULL, UNIQUE(message_id, mailbox_id));
          452  +  CREATE INDEX labels_message_id_mailbox_id_index ON labels(message_id, mailbox_id);
          453  +  CREATE INDEX labels_mailbox_id_index ON labels(mailbox_id);
          454  +  
          455  +  explain query plan
          456  +  SELECT messages.ROWID,
          457  +         messages.message_id,
          458  +         messages.remote_id,
          459  +         messages.date_received,
          460  +         messages.date_sent,
          461  +         messages.flags,
          462  +         messages.size,
          463  +         messages.color,
          464  +         messages.date_last_viewed,
          465  +         messages.subject_prefix,
          466  +         subjects.subject,
          467  +         sender.comment,
          468  +         sender.address,
          469  +         NULL,
          470  +         messages.mailbox,
          471  +         messages.original_mailbox,
          472  +         NULL,
          473  +         NULL,
          474  +         messages.type,
          475  +         messages.document_id,
          476  +         sender,
          477  +         NULL,
          478  +         messages.conversation_id,
          479  +         messages.conversation_position,
          480  +         agglabels.labels
          481  +   FROM mailboxes AS mailbox
          482  +        JOIN messages ON mailbox.ROWID = messages.mailbox
          483  +        LEFT OUTER JOIN subjects ON messages.subject = subjects.ROWID
          484  +        LEFT OUTER JOIN addresses AS sender ON messages.sender = sender.ROWID
          485  +        LEFT OUTER JOIN (
          486  +               SELECT message_id, group_concat(mailbox_id) as labels
          487  +               FROM labels GROUP BY message_id
          488  +             ) AS agglabels ON messages.ROWID = agglabels.message_id
          489  +  WHERE (mailbox.url = 'imap://email.app@imap.gmail.com/%5BGmail%5D/All%20Mail')
          490  +    AND (messages.ROWID IN (
          491  +            SELECT labels.message_id
          492  +              FROM labels JOIN mailboxes ON labels.mailbox_id = mailboxes.ROWID
          493  +             WHERE mailboxes.url = 'imap://email.app@imap.gmail.com/INBOX'))
          494  +    AND messages.mailbox in (6,12,18,24,30,36,42,1,7,13,19,25,31,37,43,2,8,
          495  +                             14,20,26,32,38,3,9,15,21,27,33,39,4,10,16,22,28,
          496  +                             34,40,5,11,17,23,35,41)
          497  +   ORDER BY date_received DESC;
          498  +} {/agglabels USING AUTOMATIC COVERING INDEX/}
          499  +
          500  +# A test case for VIEWs
          501  +#
          502  +do_execsql_test autoindex1-901 {
          503  +  CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z);
          504  +  CREATE TABLE t2(a, b);
          505  +  CREATE VIEW agg2 AS SELECT a, sum(b) AS m FROM t2 GROUP BY a;
          506  +  EXPLAIN QUERY PLAN
          507  +  SELECT t1.z, agg2.m
          508  +    FROM t1 JOIN agg2 ON t1.y=agg2.m
          509  +   WHERE t1.x IN (1,2,3);
          510  +} {/USING AUTOMATIC COVERING INDEX/}
          511  +
   415    512   
   416    513   finish_test

Changes to test/default.test.

    94     94       f INT DEFAULT -9223372036854775808,
    95     95       g INT DEFAULT (-(-9223372036854775808)),
    96     96       h INT DEFAULT (-(-9223372036854775807))
    97     97     );
    98     98     INSERT INTO t300 DEFAULT VALUES;
    99     99     SELECT * FROM t300;
   100    100   } {2147483647 2147483648 9223372036854775807 -2147483647 -2147483648 -9223372036854775808 9.22337203685478e+18 9223372036854775807}
          101  +
          102  +# Do now allow bound parameters in new DEFAULT values. 
          103  +# Silently convert bound parameters to NULL in DEFAULT causes
          104  +# in the sqlite_master table, for backwards compatibility.
          105  +#
          106  +db close
          107  +forcedelete test.db
          108  +sqlite3 db test.db
          109  +do_execsql_test default-4.0 {
          110  +  CREATE TABLE t1(a TEXT, b TEXT DEFAULT(99));
          111  +  PRAGMA writable_schema=ON;
          112  +  UPDATE sqlite_master SET sql='CREATE TABLE t1(a TEXT, b TEXT DEFAULT(:xyz))';
          113  +} {}
          114  +db close 
          115  +sqlite3 db test.db
          116  +do_execsql_test default-4.1 {
          117  +  INSERT INTO t1(a) VALUES('xyzzy');
          118  +  SELECT a, quote(b) FROM t1;
          119  +} {xyzzy NULL}
          120  +do_catchsql_test default-4.2 {
          121  +  CREATE TABLE t2(a TEXT, b TEXT DEFAULT(:xyz));
          122  +} {1 {default value of column [b] is not constant}}
          123  +do_catchsql_test default-4.3 {
          124  +  CREATE TABLE t2(a TEXT, b TEXT DEFAULT(abs(:xyz)));
          125  +} {1 {default value of column [b] is not constant}}
          126  +do_catchsql_test default-4.4 {
          127  +  CREATE TABLE t2(a TEXT, b TEXT DEFAULT(98+coalesce(5,:xyz)));
          128  +} {1 {default value of column [b] is not constant}}
   101    129   
   102    130   finish_test

Changes to test/eval.test.

    50     50     execsql {
    51     51       CREATE TABLE t2(x,y);
    52     52       INSERT INTO t2 SELECT x, x+1 FROM t1 WHERE x<5;
    53     53       SELECT x, test_eval('DELETE FROM t2 WHERE x='||x), y FROM t2;
    54     54     }
    55     55   } {1 {} {} 2 {} {} 3 {} {} 4 {} {}}
    56     56   do_test eval-2.2 {
           57  +  execsql {
           58  +    SELECT * FROM t2
           59  +  }
           60  +} {}
           61  +do_test eval-2.3 {
           62  +  execsql {
           63  +    INSERT INTO t2 SELECT x, x+1 FROM t1 WHERE x<5;
           64  +    SELECT x, test_eval('DELETE FROM t2 WHERE x='||x), y FROM t2
           65  +     ORDER BY rowid DESC;
           66  +  }
           67  +} {4 {} {} 3 {} {} 2 {} {} 1 {} {}}
           68  +do_test eval-2.4 {
    57     69     execsql {
    58     70       SELECT * FROM t2
    59     71     }
    60     72   } {}
    61     73   
    62     74   # Modify a row while it is being read.
    63     75   #

Added test/multiplex4.test.

            1  +# 2014-09-25
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file contains tests for the "truncate" option in the multiplexor.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set ::testprefix multiplex4
           18  +
           19  +db close
           20  +sqlite3_shutdown
           21  +sqlite3_multiplex_initialize {} 0
           22  +
           23  +# delete all filesl with the base name of $basename
           24  +#
           25  +proc multiplex_delete_db {basename} {
           26  +  foreach file [glob -nocomplain $basename.*] {
           27  +    forcedelete $file
           28  +  }
           29  +}
           30  +
           31  +# Return a sorted list of all files with the base name of $basename.
           32  +# Except, delete all text from the end of $basename through the NNN
           33  +# suffix on the end of the filename.
           34  +#
           35  +proc multiplex_file_list {basename} {
           36  +  set x {}
           37  +  foreach file [glob -nocomplain $basename.*] {
           38  +    regsub "^$basename\\..*(\\d\\d\\d)\$" $file $basename.\\1 file
           39  +    lappend x $file
           40  +  }
           41  +  return [lsort $x]
           42  +}
           43  +
           44  +do_test multiplex4-1.0 {
           45  +  multiplex_delete_db mx4test
           46  +  sqlite3 db {file:mx4test.db?chunksize=10&truncate=1} -uri 1 -vfs multiplex
           47  +  db eval {
           48  +    CREATE TABLE t1(x);
           49  +    INSERT INTO t1(x) VALUES(randomblob(250000));
           50  +  }
           51  +  multiplex_file_list mx4test
           52  +} {mx4test.001 mx4test.db}
           53  +
           54  +do_test multiplex4-1.1 {
           55  +  db eval {
           56  +    DELETE FROM t1;
           57  +    VACUUM;
           58  +  }
           59  +  multiplex_file_list mx4test
           60  +} {mx4test.db}
           61  +
           62  +do_test multiplex4-1.2 {
           63  +  db eval {PRAGMA multiplex_truncate}
           64  +} {on}
           65  +do_test multiplex4-1.3 {
           66  +  db eval {PRAGMA multiplex_truncate=off}
           67  +} {off}
           68  +do_test multiplex4-1.4 {
           69  +  db eval {PRAGMA multiplex_truncate}
           70  +} {off}
           71  +do_test multiplex4-1.5 {
           72  +  db eval {PRAGMA multiplex_truncate=on}
           73  +} {on}
           74  +do_test multiplex4-1.6 {
           75  +  db eval {PRAGMA multiplex_truncate}
           76  +} {on}
           77  +do_test multiplex4-1.7 {
           78  +  db eval {PRAGMA multiplex_truncate=0}
           79  +} {off}
           80  +do_test multiplex4-1.8 {
           81  +  db eval {PRAGMA multiplex_truncate=1}
           82  +} {on}
           83  +do_test multiplex4-1.9 {
           84  +  db eval {PRAGMA multiplex_truncate=0}
           85  +} {off}
           86  +
           87  +do_test multiplex4-1.10 {
           88  +  db eval {
           89  +    INSERT INTO t1(x) VALUES(randomblob(250000));
           90  +  }
           91  +  multiplex_file_list mx4test
           92  +} {mx4test.001 mx4test.db}
           93  +
           94  +do_test multiplex4-1.11 {
           95  +  db eval {
           96  +    DELETE FROM t1;
           97  +    VACUUM;
           98  +  }
           99  +  multiplex_file_list mx4test
          100  +} {mx4test.001 mx4test.db}
          101  +
          102  +do_test multiplex4-1.12 {
          103  +  db eval {
          104  +    PRAGMA multiplex_truncate=ON;
          105  +    DROP TABLE t1;
          106  +    VACUUM;
          107  +  }
          108  +  multiplex_file_list mx4test
          109  +} {mx4test.db}
          110  +
          111  +catch { db close }
          112  +forcedelete mx4test.db
          113  +sqlite3_multiplex_shutdown
          114  +finish_test

Changes to test/rowid.test.

   675    675   } {a}
   676    676   do_test rowid-12.2 {
   677    677     db close
   678    678     sqlite3 db test.db
   679    679     save_prng_state
   680    680     execsql {
   681    681       INSERT INTO t7 VALUES(NULL,'b');
   682         -    SELECT x, y FROM t7;
          682  +    SELECT x, y FROM t7 ORDER BY x;
   683    683     }
   684         -} {1 b 9223372036854775807 a}
          684  +} {/\d+ b 9223372036854775807 a/}
   685    685   execsql {INSERT INTO t7 VALUES(2,'y');}
   686    686   for {set i 1} {$i<100} {incr i} {
   687    687     do_test rowid-12.3.$i {
   688    688       db eval {DELETE FROM t7temp; INSERT INTO t7temp VALUES(1);}
   689    689       restore_prng_state
   690    690       execsql {
   691    691         INSERT INTO t7 VALUES(NULL,'x');
................................................................................
   697    697     db eval {DELETE FROM t7temp; INSERT INTO t7temp VALUES(1);}
   698    698     restore_prng_state
   699    699     catchsql {
   700    700       INSERT INTO t7 VALUES(NULL,'x');
   701    701     }
   702    702   } {1 {database or disk is full}}
   703    703   
          704  +# INSERTs that happen inside of nested function calls are recorded
          705  +# by last_insert_rowid.
          706  +#
          707  +proc rowid_addrow_func {n} {
          708  +  db eval {INSERT INTO t13(rowid,x) VALUES($n,$n*$n)}
          709  +  return [db last_insert_rowid]
          710  +}
          711  +db function addrow rowid_addrow_func
          712  +do_execsql_test rowid-13.1 {
          713  +  CREATE TABLE t13(x);
          714  +  INSERT INTO t13(rowid,x) VALUES(1234,5);
          715  +  SELECT rowid, x, addrow(rowid+1000), '|' FROM t13 LIMIT 3;
          716  +  SELECT last_insert_rowid();
          717  +} {1234 5 2234 | 2234 4990756 3234 | 3234 10458756 4234 | 4234}
   704    718   
   705    719   finish_test

Changes to test/skipscan1.test.

   241    241   } {}
   242    242   db cache flush
   243    243   do_execsql_test skipscan1-5.3 {
   244    244     EXPLAIN QUERY PLAN
   245    245       SELECT xh, loc FROM t5 WHERE loc >= 'M' AND loc < 'N';
   246    246   } {/.*COVERING INDEX t5i1 .*/}
   247    247   
   248         -
          248  +# The column used by the skip-scan needs to be sufficiently selective.
          249  +# See the private email from Adi Zaimi to drh@sqlite.org on 2014-09-22.
          250  +#
          251  +db close
          252  +forcedelete test.db
          253  +sqlite3 db test.db
          254  +do_execsql_test skipscan1-6.1 {
          255  +  CREATE TABLE t1(a,b,c,d,e,f,g,h varchar(300));
          256  +  CREATE INDEX t1ab ON t1(a,b);
          257  +  ANALYZE sqlite_master;
          258  +  -- Only two distinct values for the skip-scan column.  Skip-scan is not used.
          259  +  INSERT INTO sqlite_stat1 VALUES('t1','t1ab','500000 250000 125000');
          260  +  ANALYZE sqlite_master;
          261  +  EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=1;
          262  +} {~/ANY/}
          263  +do_execsql_test skipscan1-6.2 {
          264  +  -- Four distinct values for the skip-scan column.  Skip-scan is used.
          265  +  UPDATE sqlite_stat1 SET stat='500000 250000 62500';
          266  +  ANALYZE sqlite_master;
          267  +  EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=1;
          268  +} {/ANY.a. AND b=/}
          269  +do_execsql_test skipscan1-6.3 {
          270  +  -- Two distinct values for the skip-scan column again.  Skip-scan is not used.
          271  +  UPDATE sqlite_stat1 SET stat='500000 125000 62500';
          272  +  ANALYZE sqlite_master;
          273  +  EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=1;
          274  +} {~/ANY/}
   249    275   
   250    276   finish_test

Changes to test/skipscan5.test.

   104    104   
   105    105     foreach {tn2 q res} {
   106    106       1 { c BETWEEN 'd' AND 'e' }       {/*ANY(a) AND ANY(b) AND c>? AND c<?*/}
   107    107       2 { c BETWEEN 'b' AND 'r' }       {/*SCAN TABLE t2*/}
   108    108       3 { c > 'q' }                     {/*ANY(a) AND ANY(b) AND c>?*/}
   109    109       4 { c > 'e' }                     {/*SCAN TABLE t2*/}
   110    110       5 { c < 'q' }                     {/*SCAN TABLE t2*/}
   111         -    4 { c < 'e' }                     {/*ANY(a) AND ANY(b) AND c<?*/}
          111  +    6 { c < 'c' }                     {/*ANY(a) AND ANY(b) AND c<?*/}
   112    112     } {
   113    113       set sql "EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE $q" 
   114    114       do_execsql_test 2.$tn.$tn2 $sql $res
   115    115     }
   116    116   
   117    117   }
   118    118   
................................................................................
   176    176     6 "b < 'zzz'"                        {/*SCAN TABLE t3*/}
   177    177   } {
   178    178     set sql "EXPLAIN QUERY PLAN SELECT * FROM t3 WHERE $q" 
   179    179     do_execsql_test 3.3.$tn $sql $res
   180    180   }
   181    181   
   182    182   finish_test
   183         -
   184         -
   185         -
   186         -