/ Check-in [b0067926]
Login

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

Overview
Comment:Sync the latest trunk changes, and in particular the STAT4 IS NOT NULL fix.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1:b006792695d23980e1923b21915d5c1138ecf29d
User & Date: drh 2014-02-11 04:30:29
Context
2014-02-11
16:31
Sync with trunk. Bring in the command-line shell updates and the new 3.8.4 version number. check-in: 2cd35ff6 user: drh tags: sessions
04:30
Sync the latest trunk changes, and in particular the STAT4 IS NOT NULL fix. check-in: b0067926 user: drh tags: sessions
01:50
Make sure that virtual WHERE-clause terms do not get transformed into real terms when processing set of OR-connected terms. Fix for ticket [4c86b126f22ad]. check-in: c950d6c4 user: drh tags: trunk
2014-02-03
13:58
Sync with version 3.8.3. check-in: a704b65b user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/misc/spellfix.c.

  1929   1929   #define SPELLFIX_COL_TOP             7
  1930   1930   #define SPELLFIX_COL_SCOPE           8
  1931   1931   #define SPELLFIX_COL_SRCHCNT         9
  1932   1932   #define SPELLFIX_COL_SOUNDSLIKE     10
  1933   1933   #define SPELLFIX_COL_COMMAND        11
  1934   1934       }
  1935   1935       if( rc==SQLITE_OK && isCreate ){
  1936         -      sqlite3_uint64 r;
  1937   1936         spellfix1DbExec(&rc, db,
  1938   1937            "CREATE TABLE IF NOT EXISTS \"%w\".\"%w_vocab\"(\n"
  1939   1938            "  id INTEGER PRIMARY KEY,\n"
  1940   1939            "  rank INT,\n"
  1941   1940            "  langid INT,\n"
  1942   1941            "  word TEXT,\n"
  1943   1942            "  k1 TEXT,\n"
  1944   1943            "  k2 TEXT\n"
  1945   1944            ");\n",
  1946   1945            zDbName, zTableName
  1947   1946         );
  1948         -      sqlite3_randomness(sizeof(r), &r);
  1949   1947         spellfix1DbExec(&rc, db,
  1950         -         "CREATE INDEX IF NOT EXISTS \"%w\".\"%w_index_%llx\" "
         1948  +         "CREATE INDEX IF NOT EXISTS \"%w\".\"%w_vocab_index_langid_k2\" "
  1951   1949               "ON \"%w_vocab\"(langid,k2);",
  1952         -         zDbName, zModule, r, zTableName
         1950  +         zDbName, zModule, zTableName
  1953   1951         );
  1954   1952       }
  1955   1953       for(i=3; rc==SQLITE_OK && i<argc; i++){
  1956   1954         if( strncmp(argv[i],"edit_cost_table=",16)==0 && pNew->zCostTable==0 ){
  1957   1955           pNew->zCostTable = spellfix1Dequote(&argv[i][16]);
  1958   1956           if( pNew->zCostTable==0 ) rc = SQLITE_NOMEM;
  1959   1957           continue;

Changes to ext/session/sqlite3session.c.

  2543   2543   ** See comments in the body of this function for details.
  2544   2544   */
  2545   2545   static int sessionBindValue(
  2546   2546     sqlite3_stmt *pStmt,            /* Statement to bind value to */
  2547   2547     int i,                          /* Parameter number to bind to */
  2548   2548     sqlite3_value *pVal             /* Value to bind */
  2549   2549   ){
  2550         -  if( (pVal->type==SQLITE_TEXT || pVal->type==SQLITE_BLOB) && pVal->z==0 ){
         2550  +  int eType = sqlite3_value_type(pVal);
         2551  +  if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
  2551   2552       /* This condition occurs when an earlier OOM in a call to
  2552   2553       ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
  2553   2554       ** a conflict-hanler) has zeroed the pVal->z pointer. Return NOMEM. */
  2554   2555       return SQLITE_NOMEM;
  2555   2556     }
  2556   2557     return sqlite3_bind_value(pStmt, i, pVal);
  2557   2558   }

Changes to src/alter.c.

   465    465       pVTab = sqlite3GetVTable(db, pTab);
   466    466       if( pVTab->pVtab->pModule->xRename==0 ){
   467    467         pVTab = 0;
   468    468       }
   469    469     }
   470    470   #endif
   471    471   
   472         -  /* Begin a transaction and code the VerifyCookie for database iDb. 
          472  +  /* Begin a transaction for database iDb. 
   473    473     ** Then modify the schema cookie (since the ALTER TABLE modifies the
   474    474     ** schema). Open a statement transaction if the table is a virtual
   475    475     ** table.
   476    476     */
   477    477     v = sqlite3GetVdbe(pParse);
   478    478     if( v==0 ){
   479    479       goto exit_rename_table;

Changes to src/btree.c.

  4773   4773   }
  4774   4774   
  4775   4775   /*
  4776   4776   ** Advance the cursor to the next entry in the database.  If
  4777   4777   ** successful then set *pRes=0.  If the cursor
  4778   4778   ** was already pointing to the last entry in the database before
  4779   4779   ** this routine was called, then set *pRes=1.
         4780  +**
         4781  +** The calling function will set *pRes to 0 or 1.  The initial *pRes value
         4782  +** will be 1 if the cursor being stepped corresponds to an SQL index and
         4783  +** if this routine could have been skipped if that SQL index had been
         4784  +** a unique index.  Otherwise the caller will have set *pRes to zero.
         4785  +** Zero is the common case. The btree implementation is free to use the
         4786  +** initial *pRes value as a hint to improve performance, but the current
         4787  +** SQLite btree implementation does not. (Note that the comdb2 btree
         4788  +** implementation does use this hint, however.)
  4780   4789   */
  4781   4790   int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
  4782   4791     int rc;
  4783   4792     int idx;
  4784   4793     MemPage *pPage;
  4785   4794   
  4786   4795     assert( cursorHoldsMutex(pCur) );
  4787   4796     assert( pRes!=0 );
         4797  +  assert( *pRes==0 || *pRes==1 );
  4788   4798     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  4789   4799     if( pCur->eState!=CURSOR_VALID ){
  4790   4800       rc = restoreCursorPosition(pCur);
  4791   4801       if( rc!=SQLITE_OK ){
  4792   4802         *pRes = 0;
  4793   4803         return rc;
  4794   4804       }
................................................................................
  4859   4869   
  4860   4870   
  4861   4871   /*
  4862   4872   ** Step the cursor to the back to the previous entry in the database.  If
  4863   4873   ** successful then set *pRes=0.  If the cursor
  4864   4874   ** was already pointing to the first entry in the database before
  4865   4875   ** this routine was called, then set *pRes=1.
         4876  +**
         4877  +** The calling function will set *pRes to 0 or 1.  The initial *pRes value
         4878  +** will be 1 if the cursor being stepped corresponds to an SQL index and
         4879  +** if this routine could have been skipped if that SQL index had been
         4880  +** a unique index.  Otherwise the caller will have set *pRes to zero.
         4881  +** Zero is the common case. The btree implementation is free to use the
         4882  +** initial *pRes value as a hint to improve performance, but the current
         4883  +** SQLite btree implementation does not. (Note that the comdb2 btree
         4884  +** implementation does use this hint, however.)
  4866   4885   */
  4867   4886   int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
  4868   4887     int rc;
  4869   4888     MemPage *pPage;
  4870   4889   
  4871   4890     assert( cursorHoldsMutex(pCur) );
  4872   4891     assert( pRes!=0 );
         4892  +  assert( *pRes==0 || *pRes==1 );
  4873   4893     assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
  4874   4894     pCur->atLast = 0;
  4875   4895     if( pCur->eState!=CURSOR_VALID ){
  4876   4896       if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
  4877   4897         rc = btreeRestoreCursorPosition(pCur);
  4878   4898         if( rc!=SQLITE_OK ){
  4879   4899           *pRes = 0;
................................................................................
  7092   7112     ** the cursor to the largest entry in the tree that is smaller than
  7093   7113     ** the entry being deleted. This cell will replace the cell being deleted
  7094   7114     ** from the internal node. The 'previous' entry is used for this instead
  7095   7115     ** of the 'next' entry, as the previous entry is always a part of the
  7096   7116     ** sub-tree headed by the child page of the cell being deleted. This makes
  7097   7117     ** balancing the tree following the delete operation easier.  */
  7098   7118     if( !pPage->leaf ){
  7099         -    int notUsed;
         7119  +    int notUsed = 0;
  7100   7120       rc = sqlite3BtreePrevious(pCur, &notUsed);
  7101   7121       if( rc ) return rc;
  7102   7122     }
  7103   7123   
  7104   7124     /* Save the positions of any other cursors open on this table before
  7105   7125     ** making any modifications. Make the page containing the entry to be 
  7106   7126     ** deleted writable. Then free any overflow pages associated with the 

Changes to src/build.c.

   145    145   
   146    146       /* The cookie mask contains one bit for each database file open.
   147    147       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
   148    148       ** set for each database that is used.  Generate code to start a
   149    149       ** transaction on each used database and to verify the schema cookie
   150    150       ** on each used database.
   151    151       */
   152         -    if( pParse->cookieGoto>0 ){
          152  +    if( db->mallocFailed==0 && (pParse->cookieMask || pParse->pConstExpr) ){
   153    153         yDbMask mask;
   154         -      int iDb, i, addr;
   155         -      sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
          154  +      int iDb, i;
          155  +      assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
          156  +      sqlite3VdbeJumpHere(v, 0);
   156    157         for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
   157    158           if( (mask & pParse->cookieMask)==0 ) continue;
   158    159           sqlite3VdbeUsesBtree(v, iDb);
   159         -        sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
   160         -        if( db->init.busy==0 ){
   161         -          assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   162         -          sqlite3VdbeAddOp3(v, OP_VerifyCookie,
   163         -                            iDb, pParse->cookieValue[iDb],
   164         -                            db->aDb[iDb].pSchema->iGeneration);
   165         -        }
          160  +        sqlite3VdbeAddOp4Int(v,
          161  +          OP_Transaction,                    /* Opcode */
          162  +          iDb,                               /* P1 */
          163  +          (mask & pParse->writeMask)!=0,     /* P2 */
          164  +          pParse->cookieValue[iDb],          /* P3 */
          165  +          db->aDb[iDb].pSchema->iGeneration  /* P4 */
          166  +        );
          167  +        if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
   166    168         }
   167    169   #ifndef SQLITE_OMIT_VIRTUALTABLE
   168    170         for(i=0; i<pParse->nVtabLock; i++){
   169    171           char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
   170    172           sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
   171    173         }
   172    174         pParse->nVtabLock = 0;
................................................................................
   179    181         codeTableLocks(pParse);
   180    182   
   181    183         /* Initialize any AUTOINCREMENT data structures required.
   182    184         */
   183    185         sqlite3AutoincrementBegin(pParse);
   184    186   
   185    187         /* Code constant expressions that where factored out of inner loops */
   186         -      addr = pParse->cookieGoto;
   187    188         if( pParse->pConstExpr ){
   188    189           ExprList *pEL = pParse->pConstExpr;
   189         -        pParse->cookieGoto = 0;
          190  +        pParse->okConstFactor = 0;
   190    191           for(i=0; i<pEL->nExpr; i++){
   191    192             sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
   192    193           }
   193    194         }
   194    195   
   195    196         /* Finally, jump back to the beginning of the executable code. */
   196         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
          197  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, 1);
   197    198       }
   198    199     }
   199    200   
   200    201   
   201    202     /* Get the VDBE program ready for execution
   202    203     */
   203    204     if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
................................................................................
   212    213       pParse->rc = SQLITE_ERROR;
   213    214     }
   214    215     pParse->nTab = 0;
   215    216     pParse->nMem = 0;
   216    217     pParse->nSet = 0;
   217    218     pParse->nVar = 0;
   218    219     pParse->cookieMask = 0;
   219         -  pParse->cookieGoto = 0;
   220    220   }
   221    221   
   222    222   /*
   223    223   ** Run the parser and code generator recursively in order to generate
   224    224   ** code for the SQL statement given onto the end of the pParse context
   225    225   ** currently under construction.  When the parser is run recursively
   226    226   ** this way, the final OP_Halt is not appended and other initialization
................................................................................
  3821   3821         return 1;
  3822   3822       }
  3823   3823     }
  3824   3824     return 0;
  3825   3825   }
  3826   3826   
  3827   3827   /*
  3828         -** Generate VDBE code that will verify the schema cookie and start
  3829         -** a read-transaction for all named database files.
  3830         -**
  3831         -** It is important that all schema cookies be verified and all
  3832         -** read transactions be started before anything else happens in
  3833         -** the VDBE program.  But this routine can be called after much other
  3834         -** code has been generated.  So here is what we do:
  3835         -**
  3836         -** The first time this routine is called, we code an OP_Goto that
  3837         -** will jump to a subroutine at the end of the program.  Then we
  3838         -** record every database that needs its schema verified in the
  3839         -** pParse->cookieMask field.  Later, after all other code has been
  3840         -** generated, the subroutine that does the cookie verifications and
  3841         -** starts the transactions will be coded and the OP_Goto P2 value
  3842         -** will be made to point to that subroutine.  The generation of the
  3843         -** cookie verification subroutine code happens in sqlite3FinishCoding().
  3844         -**
  3845         -** If iDb<0 then code the OP_Goto only - don't set flag to verify the
  3846         -** schema on any databases.  This can be used to position the OP_Goto
  3847         -** early in the code, before we know if any database tables will be used.
         3828  +** Record the fact that the schema cookie will need to be verified
         3829  +** for database iDb.  The code to actually verify the schema cookie
         3830  +** will occur at the end of the top-level VDBE and will be generated
         3831  +** later, by sqlite3FinishCoding().
  3848   3832   */
  3849   3833   void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
  3850   3834     Parse *pToplevel = sqlite3ParseToplevel(pParse);
         3835  +  sqlite3 *db = pToplevel->db;
         3836  +  yDbMask mask;
  3851   3837   
  3852         -#ifndef SQLITE_OMIT_TRIGGER
  3853         -  if( pToplevel!=pParse ){
  3854         -    /* This branch is taken if a trigger is currently being coded. In this
  3855         -    ** case, set cookieGoto to a non-zero value to show that this function
  3856         -    ** has been called. This is used by the sqlite3ExprCodeConstants()
  3857         -    ** function. */
  3858         -    pParse->cookieGoto = -1;
  3859         -  }
  3860         -#endif
  3861         -  if( pToplevel->cookieGoto==0 ){
  3862         -    Vdbe *v = sqlite3GetVdbe(pToplevel);
  3863         -    if( v==0 ) return;  /* This only happens if there was a prior error */
  3864         -    pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
  3865         -  }
  3866         -  if( iDb>=0 ){
  3867         -    sqlite3 *db = pToplevel->db;
  3868         -    yDbMask mask;
  3869         -
  3870         -    assert( iDb<db->nDb );
  3871         -    assert( db->aDb[iDb].pBt!=0 || iDb==1 );
  3872         -    assert( iDb<SQLITE_MAX_ATTACHED+2 );
  3873         -    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  3874         -    mask = ((yDbMask)1)<<iDb;
  3875         -    if( (pToplevel->cookieMask & mask)==0 ){
  3876         -      pToplevel->cookieMask |= mask;
  3877         -      pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
  3878         -      if( !OMIT_TEMPDB && iDb==1 ){
  3879         -        sqlite3OpenTempDatabase(pToplevel);
  3880         -      }
         3838  +  assert( iDb>=0 && iDb<db->nDb );
         3839  +  assert( db->aDb[iDb].pBt!=0 || iDb==1 );
         3840  +  assert( iDb<SQLITE_MAX_ATTACHED+2 );
         3841  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
         3842  +  mask = ((yDbMask)1)<<iDb;
         3843  +  if( (pToplevel->cookieMask & mask)==0 ){
         3844  +    pToplevel->cookieMask |= mask;
         3845  +    pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
         3846  +    if( !OMIT_TEMPDB && iDb==1 ){
         3847  +      sqlite3OpenTempDatabase(pToplevel);
  3881   3848       }
  3882   3849     }
  3883   3850   }
  3884   3851   
  3885   3852   /*
  3886   3853   ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
  3887   3854   ** attached database. Otherwise, invoke it for the database named zDb only.

Changes to src/delete.c.

    93     93     int iCur             /* Cursor number for ephemerial table */
    94     94   ){
    95     95     SelectDest dest;
    96     96     Select *pSel;
    97     97     SrcList *pFrom;
    98     98     sqlite3 *db = pParse->db;
    99     99     int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
   100         -
   101    100     pWhere = sqlite3ExprDup(db, pWhere, 0);
   102    101     pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
   103         -
   104    102     if( pFrom ){
   105    103       assert( pFrom->nSrc==1 );
   106    104       pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
   107    105       pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
   108    106       assert( pFrom->a[0].pOn==0 );
   109    107       assert( pFrom->a[0].pUsing==0 );
   110    108     }
   111         -
   112    109     pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
   113         -  if( pSel ) pSel->selFlags |= SF_Materialize;
   114         -
   115    110     sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
   116    111     sqlite3Select(pParse, pSel, &dest);
   117    112     sqlite3SelectDelete(db, pSel);
   118    113   }
   119    114   #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
   120    115   
   121    116   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)

Changes to src/expr.c.

  1056   1056     if( pPrior ) pPrior->pNext = pNew;
  1057   1057     pNew->pNext = 0;
  1058   1058     pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
  1059   1059     pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
  1060   1060     pNew->iLimit = 0;
  1061   1061     pNew->iOffset = 0;
  1062   1062     pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
  1063         -  pNew->pRightmost = 0;
  1064   1063     pNew->addrOpenEphm[0] = -1;
  1065   1064     pNew->addrOpenEphm[1] = -1;
  1066   1065     pNew->addrOpenEphm[2] = -1;
  1067   1066     pNew->nSelectRow = p->nSelectRow;
  1068   1067     pNew->pWith = withDup(db, p->pWith);
  1069   1068     return pNew;
  1070   1069   }
................................................................................
  1580   1579       assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
  1581   1580       assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
  1582   1581       assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
  1583   1582       pTab = p->pSrc->a[0].pTab;
  1584   1583       pExpr = p->pEList->a[0].pExpr;
  1585   1584       iCol = (i16)pExpr->iColumn;
  1586   1585      
  1587         -    /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
         1586  +    /* Code an OP_Transaction and OP_TableLock for <table>. */
  1588   1587       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  1589   1588       sqlite3CodeVerifySchema(pParse, iDb);
  1590   1589       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  1591   1590   
  1592   1591       /* This function is only called from two places. In both cases the vdbe
  1593   1592       ** has already been allocated. So assume sqlite3GetVdbe() is always
  1594   1593       ** successful here.
................................................................................
  1759   1758         ** SELECT... statement are columns, then numeric affinity is used
  1760   1759         ** if either column has NUMERIC or INTEGER affinity. If neither
  1761   1760         ** 'x' nor the SELECT... statement are columns, then numeric affinity
  1762   1761         ** is used.
  1763   1762         */
  1764   1763         pExpr->iTable = pParse->nTab++;
  1765   1764         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
  1766         -      if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  1767   1765         pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
  1768   1766   
  1769   1767         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1770   1768           /* Case 1:     expr IN (SELECT ...)
  1771   1769           **
  1772   1770           ** Generate code to write the results of the select into the temporary
  1773   1771           ** table allocated and opened above.

Changes to src/insert.c.

   345    345   **
   346    346   ** Registers are allocated as follows:
   347    347   **
   348    348   **   pDest->iSDParm      The register holding the next entry-point of the
   349    349   **                       co-routine.  Run the co-routine to its next breakpoint
   350    350   **                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
   351    351   **
   352         -**   pDest->iSDParm+1    The register holding the "completed" flag for the
   353         -**                       co-routine. This register is 0 if the previous Yield
   354         -**                       generated a new result row, or 1 if the subquery
   355         -**                       has completed.  If the Yield is called again
   356         -**                       after this register becomes 1, then the VDBE will
   357         -**                       halt with an SQLITE_INTERNAL error.
   358         -**
   359    352   **   pDest->iSdst        First result register.
   360    353   **
   361    354   **   pDest->nSdst        Number of result registers.
          355  +**
          356  +** At EOF the first result register will be marked as "undefined" so that
          357  +** the caller can know when to stop reading results.
   362    358   **
   363    359   ** This routine handles all of the register allocation and fills in the
   364    360   ** pDest structure appropriately.
   365    361   **
   366    362   ** Here is a schematic of the generated code assuming that X is the 
   367    363   ** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
   368    364   ** completed flag reg[pDest->iSDParm+1], and R and S are the range of
   369    365   ** registers that hold the result set, reg[pDest->iSdst] through
   370    366   ** reg[pDest->iSdst+pDest->nSdst-1]:
   371    367   **
   372    368   **         X <- A
   373         -**         EOF <- 0
   374    369   **         goto B
   375    370   **      A: setup for the SELECT
   376    371   **         loop rows in the SELECT
   377    372   **           load results into registers R..S
   378    373   **           yield X
   379    374   **         end loop
   380    375   **         cleanup after the SELECT
   381         -**         EOF <- 1
   382         -**         yield X
   383         -**         halt-error
          376  +**         end co-routine R
   384    377   **      B:
   385    378   **
   386    379   ** To use this subroutine, the caller generates code as follows:
   387    380   **
   388    381   **         [ Co-routine generated by this subroutine, shown above ]
   389         -**      S: yield X
   390         -**         if EOF goto E
          382  +**      S: yield X, at EOF goto E
   391    383   **         if skip this row, goto C
   392    384   **         if terminate loop, goto E
   393    385   **         deal with this row
   394    386   **      C: goto S
   395    387   **      E:
   396    388   */
   397    389   int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
   398    390     int regYield;       /* Register holding co-routine entry-point */
   399         -  int regEof;         /* Register holding co-routine completion flag */
   400    391     int addrTop;        /* Top of the co-routine */
   401         -  int j1;             /* Jump instruction */
   402    392     int rc;             /* Result code */
   403    393     Vdbe *v;            /* VDBE under construction */
   404    394   
   405    395     regYield = ++pParse->nMem;
   406         -  regEof = ++pParse->nMem;
   407    396     v = sqlite3GetVdbe(pParse);
   408         -  addrTop = sqlite3VdbeCurrentAddr(v);
   409         -  sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
   410         -  VdbeComment((v, "Co-routine entry point"));
   411         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);           /* EOF <- 0 */
   412         -  VdbeComment((v, "Co-routine completion flag"));
          397  +  addrTop = sqlite3VdbeCurrentAddr(v) + 1;
          398  +  sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
   413    399     sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
   414         -  j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
   415    400     rc = sqlite3Select(pParse, pSelect, pDest);
   416    401     assert( pParse->nErr==0 || rc );
   417    402     if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
   418    403     if( rc ) return rc;
   419         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);            /* EOF <- 1 */
   420         -  sqlite3VdbeAddOp1(v, OP_Yield, regYield);   /* yield X */
   421         -  sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
   422         -  VdbeComment((v, "End of coroutine"));
   423         -  sqlite3VdbeJumpHere(v, j1);                             /* label B: */
          404  +  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
          405  +  sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
   424    406     return rc;
   425    407   }
   426    408   
   427    409   
   428    410   
   429    411   /* Forward declaration */
   430    412   static int xferOptimization(
................................................................................
   484    466   **           close cursors
   485    467   **         end foreach
   486    468   **
   487    469   ** The 3rd template is for when the second template does not apply
   488    470   ** and the SELECT clause does not read from <table> at any time.
   489    471   ** The generated code follows this template:
   490    472   **
   491         -**         EOF <- 0
   492    473   **         X <- A
   493    474   **         goto B
   494    475   **      A: setup for the SELECT
   495    476   **         loop over the rows in the SELECT
   496    477   **           load values into registers R..R+n
   497    478   **           yield X
   498    479   **         end loop
   499    480   **         cleanup after the SELECT
   500         -**         EOF <- 1
   501         -**         yield X
   502         -**         goto A
          481  +**         end-coroutine X
   503    482   **      B: open write cursor to <table> and its indices
   504         -**      C: yield X
   505         -**         if EOF goto D
          483  +**      C: yield X, at EOF goto D
   506    484   **         insert the select result into <table> from R..R+n
   507    485   **         goto C
   508    486   **      D: cleanup
   509    487   **
   510    488   ** The 4th template is used if the insert statement takes its
   511    489   ** values from a SELECT but the data is being inserted into a table
   512    490   ** that is also read as part of the SELECT.  In the third form,
   513    491   ** we have to use a intermediate table to store the results of
   514    492   ** the select.  The template is like this:
   515    493   **
   516         -**         EOF <- 0
   517    494   **         X <- A
   518    495   **         goto B
   519    496   **      A: setup for the SELECT
   520    497   **         loop over the tables in the SELECT
   521    498   **           load value into register R..R+n
   522    499   **           yield X
   523    500   **         end loop
   524    501   **         cleanup after the SELECT
   525         -**         EOF <- 1
   526         -**         yield X
   527         -**         halt-error
          502  +**         end co-routine R
   528    503   **      B: open temp table
   529         -**      L: yield X
   530         -**         if EOF goto M
          504  +**      L: yield X, at EOF goto M
   531    505   **         insert row from R..R+n into temp table
   532    506   **         goto L
   533    507   **      M: open write cursor to <table> and its indices
   534    508   **         rewind temp table
   535    509   **      C: loop over rows of intermediate table
   536    510   **           transfer values form intermediate table into <table>
   537    511   **         end loop
................................................................................
   572    546     /* Register allocations */
   573    547     int regFromSelect = 0;/* Base register for data coming from SELECT */
   574    548     int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
   575    549     int regRowCount = 0;  /* Memory cell used for the row counter */
   576    550     int regIns;           /* Block of regs holding rowid+data being inserted */
   577    551     int regRowid;         /* registers holding insert rowid */
   578    552     int regData;          /* register holding first column to insert */
   579         -  int regEof = 0;       /* Register recording end of SELECT data */
   580    553     int *aRegIdx = 0;     /* One register allocated to each index */
   581    554   
   582    555   #ifndef SQLITE_OMIT_TRIGGER
   583    556     int isView;                 /* True if attempting to insert into a view */
   584    557     Trigger *pTrigger;          /* List of triggers on pTab, if required */
   585    558     int tmask;                  /* Mask of trigger times */
   586    559   #endif
................................................................................
   685    658     ** co-routine is the common header to the 3rd and 4th templates.
   686    659     */
   687    660     if( pSelect ){
   688    661       /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
   689    662       int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
   690    663       if( rc ) goto insert_cleanup;
   691    664   
   692         -    regEof = dest.iSDParm + 1;
   693    665       regFromSelect = dest.iSdst;
   694    666       assert( pSelect->pEList );
   695    667       nColumn = pSelect->pEList->nExpr;
   696    668       assert( dest.nSdst==nColumn );
   697    669   
   698    670       /* Set useTempTable to TRUE if the result of the SELECT statement
   699    671       ** should be written into a temporary table (template 4).  Set to
................................................................................
   710    682   
   711    683       if( useTempTable ){
   712    684         /* Invoke the coroutine to extract information from the SELECT
   713    685         ** and add it to a transient table srcTab.  The code generated
   714    686         ** here is from the 4th template:
   715    687         **
   716    688         **      B: open temp table
   717         -      **      L: yield X
   718         -      **         if EOF goto M
          689  +      **      L: yield X, goto M at EOF
   719    690         **         insert row from R..R+n into temp table
   720    691         **         goto L
   721    692         **      M: ...
   722    693         */
   723    694         int regRec;          /* Register to hold packed record */
   724    695         int regTempRowid;    /* Register to hold temp table ROWID */
   725    696         int addrTop;         /* Label "L" */
   726         -      int addrIf;          /* Address of jump to M */
   727    697   
   728    698         srcTab = pParse->nTab++;
   729    699         regRec = sqlite3GetTempReg(pParse);
   730    700         regTempRowid = sqlite3GetTempReg(pParse);
   731    701         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
   732    702         addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
   733         -      addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
   734    703         sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
   735    704         sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
   736    705         sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
   737    706         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
   738         -      sqlite3VdbeJumpHere(v, addrIf);
          707  +      sqlite3VdbeJumpHere(v, addrTop);
   739    708         sqlite3ReleaseTempReg(pParse, regRec);
   740    709         sqlite3ReleaseTempReg(pParse, regTempRowid);
   741    710       }
   742    711     }else{
   743    712       /* This is the case if the data for the INSERT is coming from a VALUES
   744    713       ** clause
   745    714       */
................................................................................
   843    812     }
   844    813   
   845    814     /* This is the top of the main insertion loop */
   846    815     if( useTempTable ){
   847    816       /* This block codes the top of loop only.  The complete loop is the
   848    817       ** following pseudocode (template 4):
   849    818       **
   850         -    **         rewind temp table
          819  +    **         rewind temp table, if empty goto D
   851    820       **      C: loop over rows of intermediate table
   852    821       **           transfer values form intermediate table into <table>
   853    822       **         end loop
   854    823       **      D: ...
   855    824       */
   856    825       addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
   857    826       addrCont = sqlite3VdbeCurrentAddr(v);
   858    827     }else if( pSelect ){
   859    828       /* This block codes the top of loop only.  The complete loop is the
   860    829       ** following pseudocode (template 3):
   861    830       **
   862         -    **      C: yield X
   863         -    **         if EOF goto D
          831  +    **      C: yield X, at EOF goto D
   864    832       **         insert the select result into <table> from R..R+n
   865    833       **         goto C
   866    834       **      D: ...
   867    835       */
   868         -    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
   869         -    addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
          836  +    addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
   870    837     }
   871    838   
   872    839     /* Allocate registers for holding the rowid of the new row,
   873    840     ** the content of the new row, and the assemblied row record.
   874    841     */
   875    842     regRowid = regIns = pParse->nMem+1;
   876    843     pParse->nMem += pTab->nCol + 1;
................................................................................
  2079   2046       sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
  2080   2047       sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
  2081   2048       sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
  2082   2049       sqlite3VdbeJumpHere(v, addr1);
  2083   2050       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  2084   2051       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  2085   2052     }
  2086         -  sqlite3VdbeJumpHere(v, emptySrcTest);
         2053  +  if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
  2087   2054     sqlite3ReleaseTempReg(pParse, regRowid);
  2088   2055     sqlite3ReleaseTempReg(pParse, regData);
  2089   2056     if( emptyDestTest ){
  2090   2057       sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
  2091   2058       sqlite3VdbeJumpHere(v, emptyDestTest);
  2092   2059       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  2093   2060       return 0;
  2094   2061     }else{
  2095   2062       return 1;
  2096   2063     }
  2097   2064   }
  2098   2065   #endif /* SQLITE_OMIT_XFER_OPT */

Changes to src/os_unix.c.

    79     79   #  if defined(__RTP__) || defined(_WRS_KERNEL)
    80     80   #    define OS_VXWORKS 1
    81     81   #  else
    82     82   #    define OS_VXWORKS 0
    83     83   #  endif
    84     84   #endif
    85     85   
    86         -/*
    87         -** These #defines should enable >2GB file support on Posix if the
    88         -** underlying operating system supports it.  If the OS lacks
    89         -** large file support, these should be no-ops.
    90         -**
    91         -** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    92         -** on the compiler command line.  This is necessary if you are compiling
    93         -** on a recent machine (ex: RedHat 7.2) but you want your code to work
    94         -** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
    95         -** without this option, LFS is enable.  But LFS does not exist in the kernel
    96         -** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
    97         -** portability you should omit LFS.
    98         -**
    99         -** The previous paragraph was written in 2005.  (This paragraph is written
   100         -** on 2008-11-28.) These days, all Linux kernels support large files, so
   101         -** you should probably leave LFS enabled.  But some embedded platforms might
   102         -** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
   103         -*/
   104         -#ifndef SQLITE_DISABLE_LFS
   105         -# define _LARGE_FILE       1
   106         -# ifndef _FILE_OFFSET_BITS
   107         -#   define _FILE_OFFSET_BITS 64
   108         -# endif
   109         -# define _LARGEFILE_SOURCE 1
   110         -#endif
   111         -
   112     86   /*
   113     87   ** standard include files.
   114     88   */
   115     89   #include <sys/types.h>
   116     90   #include <sys/stat.h>
   117     91   #include <fcntl.h>
   118     92   #include <unistd.h>

Changes to src/os_win.c.

  3197   3197   
  3198   3198   /* 
  3199   3199   ** Windows will only let you create file view mappings
  3200   3200   ** on allocation size granularity boundaries.
  3201   3201   ** During sqlite3_os_init() we do a GetSystemInfo()
  3202   3202   ** to get the granularity size.
  3203   3203   */
  3204         -SYSTEM_INFO winSysInfo;
         3204  +static SYSTEM_INFO winSysInfo;
  3205   3205   
  3206   3206   #ifndef SQLITE_OMIT_WAL
  3207   3207   
  3208   3208   /*
  3209   3209   ** Helper functions to obtain and relinquish the global mutex. The
  3210   3210   ** global mutex is used to protect the winLockInfo objects used by 
  3211   3211   ** this file, all of which may be shared by multiple threads.

Changes to src/pager.c.

  1679   1679   
  1680   1680   /*
  1681   1681   ** Find a page in the hash table given its page number. Return
  1682   1682   ** a pointer to the page or NULL if the requested page is not 
  1683   1683   ** already in memory.
  1684   1684   */
  1685   1685   static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
  1686         -  PgHdr *p;                         /* Return value */
         1686  +  PgHdr *p = 0;                     /* Return value */
  1687   1687   
  1688   1688     /* It is not possible for a call to PcacheFetch() with createFlag==0 to
  1689   1689     ** fail, since no attempt to allocate dynamic memory will be made.
  1690   1690     */
  1691   1691     (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
  1692   1692     return p;
  1693   1693   }

Changes to src/parse.y.

   408    408   %type select {Select*}
   409    409   %destructor select {sqlite3SelectDelete(pParse->db, $$);}
   410    410   %type selectnowith {Select*}
   411    411   %destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
   412    412   %type oneselect {Select*}
   413    413   %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
   414    414   
   415         -select(A) ::= with(W) selectnowith(X). { 
   416         -  if( X ){
   417         -    X->pWith = W; 
          415  +select(A) ::= with(W) selectnowith(X). {
          416  +  Select *p = X, *pNext, *pLoop;
          417  +  if( p ){
          418  +    int cnt = 0, mxSelect;
          419  +    p->pWith = W;
          420  +    if( p->pPrior ){
          421  +      pNext = 0;
          422  +      for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
          423  +        pLoop->pNext = pNext;
          424  +        pLoop->selFlags |= SF_Compound;
          425  +      }
          426  +      mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
          427  +      if( mxSelect && cnt>mxSelect ){
          428  +        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
          429  +      }
          430  +    }
   418    431     }else{
   419    432       sqlite3WithDelete(pParse->db, W);
   420    433     }
   421         -  A = X; 
          434  +  A = p;
   422    435   }
   423    436   
   424    437   selectnowith(A) ::= oneselect(X).                      {A = X;}
   425    438   %ifndef SQLITE_OMIT_COMPOUND_SELECT
   426    439   selectnowith(A) ::= selectnowith(X) multiselect_op(Y) oneselect(Z).  {
   427    440     if( Z ){
   428    441       Z->op = (u8)Y;

Changes to src/pcache.c.

    19     19   struct PCache {
    20     20     PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
    21     21     PgHdr *pSynced;                     /* Last synced page in dirty page list */
    22     22     int nRef;                           /* Number of referenced pages */
    23     23     int szCache;                        /* Configured cache size */
    24     24     int szPage;                         /* Size of every page in this cache */
    25     25     int szExtra;                        /* Size of extra space for each page */
    26         -  int bPurgeable;                     /* True if pages are on backing store */
           26  +  u8 bPurgeable;                      /* True if pages are on backing store */
           27  +  u8 eCreate;                         /* eCreate value for for xFetch() */
    27     28     int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
    28     29     void *pStress;                      /* Argument to xStress */
    29     30     sqlite3_pcache *pCache;             /* Pluggable cache module */
    30     31     PgHdr *pPage1;                      /* Reference to page 1 */
    31     32   };
    32     33   
    33     34   /*
................................................................................
    86     87       p->pDirtyTail = pPage->pDirtyPrev;
    87     88     }
    88     89     if( pPage->pDirtyPrev ){
    89     90       pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
    90     91     }else{
    91     92       assert( pPage==p->pDirty );
    92     93       p->pDirty = pPage->pDirtyNext;
           94  +    if( p->pDirty==0 && p->bPurgeable ){
           95  +      assert( p->eCreate==1 );
           96  +      p->eCreate = 2;
           97  +    }
    93     98     }
    94     99     pPage->pDirtyNext = 0;
    95    100     pPage->pDirtyPrev = 0;
    96    101   
    97    102     expensive_assert( pcacheCheckSynced(p) );
    98    103   }
    99    104   
................................................................................
   106    111   
   107    112     assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
   108    113   
   109    114     pPage->pDirtyNext = p->pDirty;
   110    115     if( pPage->pDirtyNext ){
   111    116       assert( pPage->pDirtyNext->pDirtyPrev==0 );
   112    117       pPage->pDirtyNext->pDirtyPrev = pPage;
          118  +  }else if( p->bPurgeable ){
          119  +    assert( p->eCreate==2 );
          120  +    p->eCreate = 1;
   113    121     }
   114    122     p->pDirty = pPage;
   115    123     if( !p->pDirtyTail ){
   116    124       p->pDirtyTail = pPage;
   117    125     }
   118    126     if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
   119    127       p->pSynced = pPage;
................................................................................
   175    183     void *pStress,               /* Argument to xStress */
   176    184     PCache *p                    /* Preallocated space for the PCache */
   177    185   ){
   178    186     memset(p, 0, sizeof(PCache));
   179    187     p->szPage = szPage;
   180    188     p->szExtra = szExtra;
   181    189     p->bPurgeable = bPurgeable;
          190  +  p->eCreate = 2;
   182    191     p->xStress = xStress;
   183    192     p->pStress = pStress;
   184    193     p->szCache = 100;
   185    194   }
   186    195   
   187    196   /*
   188    197   ** Change the page size for PCache object. The caller must ensure that there
................................................................................
   214    223   */
   215    224   int sqlite3PcacheFetch(
   216    225     PCache *pCache,       /* Obtain the page from this cache */
   217    226     Pgno pgno,            /* Page number to obtain */
   218    227     int createFlag,       /* If true, create page if it does not exist already */
   219    228     PgHdr **ppPage        /* Write the page here */
   220    229   ){
   221         -  sqlite3_pcache_page *pPage = 0;
          230  +  sqlite3_pcache_page *pPage;
   222    231     PgHdr *pPgHdr = 0;
   223    232     int eCreate;
   224    233   
   225    234     assert( pCache!=0 );
   226    235     assert( createFlag==1 || createFlag==0 );
   227    236     assert( pgno>0 );
   228    237   
   229    238     /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
   230    239     ** allocate it now.
   231    240     */
   232         -  if( !pCache->pCache && createFlag ){
          241  +  if( !pCache->pCache ){
   233    242       sqlite3_pcache *p;
          243  +    if( !createFlag ){
          244  +      *ppPage = 0;
          245  +      return SQLITE_OK;
          246  +    }
   234    247       p = sqlite3GlobalConfig.pcache2.xCreate(
   235    248           pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
   236    249       );
   237    250       if( !p ){
   238    251         return SQLITE_NOMEM;
   239    252       }
   240    253       sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
   241    254       pCache->pCache = p;
   242    255     }
   243    256   
   244         -  eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
   245         -  if( pCache->pCache ){
   246         -    pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
   247         -  }
   248         -
          257  +  /* eCreate defines what to do if the page does not exist.
          258  +  **    0     Do not allocate a new page.  (createFlag==0)
          259  +  **    1     Allocate a new page if doing so is inexpensive.
          260  +  **          (createFlag==1 AND bPurgeable AND pDirty)
          261  +  **    2     Allocate a new page even it doing so is difficult.
          262  +  **          (createFlag==1 AND !(bPurgeable AND pDirty)
          263  +  */
          264  +  eCreate = createFlag==0 ? 0 : pCache->eCreate;
          265  +  assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
          266  +  pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
   249    267     if( !pPage && eCreate==1 ){
   250    268       PgHdr *pPg;
   251    269   
   252    270       /* Find a dirty page to write-out and recycle. First try to find a 
   253    271       ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
   254    272       ** cleared), but if that is not possible settle for any other 
   255    273       ** unreferenced dirty page.

Changes to src/select.c.

   104    104   */
   105    105   void sqlite3SelectDelete(sqlite3 *db, Select *p){
   106    106     if( p ){
   107    107       clearSelect(db, p);
   108    108       sqlite3DbFree(db, p);
   109    109     }
   110    110   }
          111  +
          112  +/*
          113  +** Return a pointer to the right-most SELECT statement in a compound.
          114  +*/
          115  +static Select *findRightmost(Select *p){
          116  +  while( p->pNext ) p = p->pNext;
          117  +  return p;
          118  +}
   111    119   
   112    120   /*
   113    121   ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
   114    122   ** type of join.  Return an integer constant that expresses that type
   115    123   ** in terms of the following bit values:
   116    124   **
   117    125   **     JT_INNER
................................................................................
   761    769           sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
   762    770           /* The LIMIT clause will jump out of the loop for us */
   763    771         }
   764    772         break;
   765    773       }
   766    774   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
   767    775   
   768         -    /* Send the data to the callback function or to a subroutine.  In the
   769         -    ** case of a subroutine, the subroutine itself is responsible for
   770         -    ** popping the data from the stack.
   771         -    */
   772         -    case SRT_Coroutine:
   773         -    case SRT_Output: {
          776  +    case SRT_Coroutine:       /* Send data to a co-routine */
          777  +    case SRT_Output: {        /* Return the results */
   774    778         testcase( eDest==SRT_Coroutine );
   775    779         testcase( eDest==SRT_Output );
   776    780         if( pOrderBy ){
   777    781           int r1 = sqlite3GetTempReg(pParse);
   778    782           sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
   779    783           pushOntoSorter(pParse, pOrderBy, p, r1);
   780    784           sqlite3ReleaseTempReg(pParse, r1);
................................................................................
  1615   1619   ** Get a VDBE for the given parser context.  Create a new one if necessary.
  1616   1620   ** If an error occurs, return NULL and leave a message in pParse.
  1617   1621   */
  1618   1622   Vdbe *sqlite3GetVdbe(Parse *pParse){
  1619   1623     Vdbe *v = pParse->pVdbe;
  1620   1624     if( v==0 ){
  1621   1625       v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
  1622         -#ifndef SQLITE_OMIT_TRACE
  1623         -    if( v ){
  1624         -      sqlite3VdbeAddOp0(v, OP_Trace);
         1626  +    if( v ) sqlite3VdbeAddOp0(v, OP_Init);
         1627  +    if( pParse->pToplevel==0
         1628  +     && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
         1629  +    ){
         1630  +      pParse->okConstFactor = 1;
  1625   1631       }
  1626         -#endif
         1632  +
  1627   1633     }
  1628   1634     return v;
  1629   1635   }
  1630   1636   
  1631   1637   
  1632   1638   /*
  1633   1639   ** Compute the iLimit and iOffset fields of the SELECT based on the
................................................................................
  1875   1881       p->selFlags |= SF_UsesEphemeral;
  1876   1882     }
  1877   1883   
  1878   1884     /* Detach the ORDER BY clause from the compound SELECT */
  1879   1885     p->pOrderBy = 0;
  1880   1886   
  1881   1887     /* Store the results of the setup-query in Queue. */
         1888  +  pSetup->pNext = 0;
  1882   1889     rc = sqlite3Select(pParse, pSetup, &destQueue);
         1890  +  pSetup->pNext = p;
  1883   1891     if( rc ) goto end_of_recursive_query;
  1884   1892   
  1885   1893     /* Find the next row in the Queue and output that row */
  1886   1894     addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak);
  1887   1895   
  1888   1896     /* Transfer the next row in Queue over to Current */
  1889   1897     sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
................................................................................
  1980   1988     /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
  1981   1989     ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
  1982   1990     */
  1983   1991     assert( p && p->pPrior );  /* Calling function guarantees this much */
  1984   1992     assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
  1985   1993     db = pParse->db;
  1986   1994     pPrior = p->pPrior;
  1987         -  assert( pPrior->pRightmost!=pPrior );
  1988         -  assert( pPrior->pRightmost==p->pRightmost );
  1989   1995     dest = *pDest;
  1990   1996     if( pPrior->pOrderBy ){
  1991   1997       sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
  1992   1998         selectOpName(p->op));
  1993   1999       rc = 1;
  1994   2000       goto multi_select_end;
  1995   2001     }
................................................................................
  2089   2095         Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
  2090   2096         int addr;
  2091   2097         SelectDest uniondest;
  2092   2098   
  2093   2099         testcase( p->op==TK_EXCEPT );
  2094   2100         testcase( p->op==TK_UNION );
  2095   2101         priorOp = SRT_Union;
  2096         -      if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
         2102  +      if( dest.eDest==priorOp ){
  2097   2103           /* We can reuse a temporary table generated by a SELECT to our
  2098   2104           ** right.
  2099   2105           */
  2100         -        assert( p->pRightmost!=p );  /* Can only happen for leftward elements
  2101         -                                     ** of a 3-way or more compound */
  2102   2106           assert( p->pLimit==0 );      /* Not allowed on leftward elements */
  2103   2107           assert( p->pOffset==0 );     /* Not allowed on leftward elements */
  2104   2108           unionTab = dest.iSDParm;
  2105   2109         }else{
  2106   2110           /* We will need to create our own temporary table to hold the
  2107   2111           ** intermediate results.
  2108   2112           */
  2109   2113           unionTab = pParse->nTab++;
  2110   2114           assert( p->pOrderBy==0 );
  2111   2115           addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
  2112   2116           assert( p->addrOpenEphm[0] == -1 );
  2113   2117           p->addrOpenEphm[0] = addr;
  2114         -        p->pRightmost->selFlags |= SF_UsesEphemeral;
         2118  +        findRightmost(p)->selFlags |= SF_UsesEphemeral;
  2115   2119           assert( p->pEList );
  2116   2120         }
  2117   2121   
  2118   2122         /* Code the SELECT statements to our left
  2119   2123         */
  2120   2124         assert( !pPrior->pOrderBy );
  2121   2125         sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
................................................................................
  2196   2200         tab1 = pParse->nTab++;
  2197   2201         tab2 = pParse->nTab++;
  2198   2202         assert( p->pOrderBy==0 );
  2199   2203   
  2200   2204         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
  2201   2205         assert( p->addrOpenEphm[0] == -1 );
  2202   2206         p->addrOpenEphm[0] = addr;
  2203         -      p->pRightmost->selFlags |= SF_UsesEphemeral;
         2207  +      findRightmost(p)->selFlags |= SF_UsesEphemeral;
  2204   2208         assert( p->pEList );
  2205   2209   
  2206   2210         /* Code the SELECTs to our left into temporary table "tab1".
  2207   2211         */
  2208   2212         sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
  2209   2213         explainSetInteger(iSub1, pParse->iNextSelectId);
  2210   2214         rc = sqlite3Select(pParse, pPrior, &intersectdest);
................................................................................
  2275   2279     if( p->selFlags & SF_UsesEphemeral ){
  2276   2280       int i;                        /* Loop counter */
  2277   2281       KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
  2278   2282       Select *pLoop;                /* For looping through SELECT statements */
  2279   2283       CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
  2280   2284       int nCol;                     /* Number of columns in result set */
  2281   2285   
  2282         -    assert( p->pRightmost==p );
         2286  +    assert( p->pNext==0 );
  2283   2287       nCol = p->pEList->nExpr;
  2284   2288       pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
  2285   2289       if( !pKeyInfo ){
  2286   2290         rc = SQLITE_NOMEM;
  2287   2291         goto multi_select_end;
  2288   2292       }
  2289   2293       for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
................................................................................
  2568   2572   ){
  2569   2573     int i, j;             /* Loop counters */
  2570   2574     Select *pPrior;       /* Another SELECT immediately to our left */
  2571   2575     Vdbe *v;              /* Generate code to this VDBE */
  2572   2576     SelectDest destA;     /* Destination for coroutine A */
  2573   2577     SelectDest destB;     /* Destination for coroutine B */
  2574   2578     int regAddrA;         /* Address register for select-A coroutine */
  2575         -  int regEofA;          /* Flag to indicate when select-A is complete */
  2576   2579     int regAddrB;         /* Address register for select-B coroutine */
  2577         -  int regEofB;          /* Flag to indicate when select-B is complete */
  2578   2580     int addrSelectA;      /* Address of the select-A coroutine */
  2579   2581     int addrSelectB;      /* Address of the select-B coroutine */
  2580   2582     int regOutA;          /* Address register for the output-A subroutine */
  2581   2583     int regOutB;          /* Address register for the output-B subroutine */
  2582   2584     int addrOutA;         /* Address of the output-A subroutine */
  2583   2585     int addrOutB = 0;     /* Address of the output-B subroutine */
  2584   2586     int addrEofA;         /* Address of the select-A-exhausted subroutine */
         2587  +  int addrEofA_noB;     /* Alternate addrEofA if B is uninitialized */
  2585   2588     int addrEofB;         /* Address of the select-B-exhausted subroutine */
  2586   2589     int addrAltB;         /* Address of the A<B subroutine */
  2587   2590     int addrAeqB;         /* Address of the A==B subroutine */
  2588   2591     int addrAgtB;         /* Address of the A>B subroutine */
  2589   2592     int regLimitA;        /* Limit register for select-A */
  2590   2593     int regLimitB;        /* Limit register for select-A */
  2591   2594     int regPrev;          /* A range of registers to hold previous output */
................................................................................
  2692   2695         }
  2693   2696       }
  2694   2697     }
  2695   2698    
  2696   2699     /* Separate the left and the right query from one another
  2697   2700     */
  2698   2701     p->pPrior = 0;
         2702  +  pPrior->pNext = 0;
  2699   2703     sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
  2700   2704     if( pPrior->pPrior==0 ){
  2701   2705       sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
  2702   2706     }
  2703   2707   
  2704   2708     /* Compute the limit registers */
  2705   2709     computeLimitRegisters(pParse, p, labelEnd);
................................................................................
  2714   2718     }
  2715   2719     sqlite3ExprDelete(db, p->pLimit);
  2716   2720     p->pLimit = 0;
  2717   2721     sqlite3ExprDelete(db, p->pOffset);
  2718   2722     p->pOffset = 0;
  2719   2723   
  2720   2724     regAddrA = ++pParse->nMem;
  2721         -  regEofA = ++pParse->nMem;
  2722   2725     regAddrB = ++pParse->nMem;
  2723         -  regEofB = ++pParse->nMem;
  2724   2726     regOutA = ++pParse->nMem;
  2725   2727     regOutB = ++pParse->nMem;
  2726   2728     sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
  2727   2729     sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
  2728         -
  2729         -  /* Jump past the various subroutines and coroutines to the main
  2730         -  ** merge loop
  2731         -  */
  2732         -  j1 = sqlite3VdbeAddOp0(v, OP_Goto);
  2733         -  addrSelectA = sqlite3VdbeCurrentAddr(v);
  2734         -
  2735   2730   
  2736   2731     /* Generate a coroutine to evaluate the SELECT statement to the
  2737   2732     ** left of the compound operator - the "A" select.
  2738   2733     */
  2739         -  VdbeNoopComment((v, "Begin coroutine for left SELECT"));
         2734  +  addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
         2735  +  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
         2736  +  VdbeComment((v, "left SELECT"));
  2740   2737     pPrior->iLimit = regLimitA;
  2741   2738     explainSetInteger(iSub1, pParse->iNextSelectId);
  2742   2739     sqlite3Select(pParse, pPrior, &destA);
  2743         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
  2744         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
  2745         -  VdbeNoopComment((v, "End coroutine for left SELECT"));
         2740  +  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
         2741  +  sqlite3VdbeJumpHere(v, j1);
  2746   2742   
  2747   2743     /* Generate a coroutine to evaluate the SELECT statement on 
  2748   2744     ** the right - the "B" select
  2749   2745     */
  2750         -  addrSelectB = sqlite3VdbeCurrentAddr(v);
  2751         -  VdbeNoopComment((v, "Begin coroutine for right SELECT"));
         2746  +  addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
         2747  +  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
         2748  +  VdbeComment((v, "right SELECT"));
  2752   2749     savedLimit = p->iLimit;
  2753   2750     savedOffset = p->iOffset;
  2754   2751     p->iLimit = regLimitB;
  2755   2752     p->iOffset = 0;  
  2756   2753     explainSetInteger(iSub2, pParse->iNextSelectId);
  2757   2754     sqlite3Select(pParse, p, &destB);
  2758   2755     p->iLimit = savedLimit;
  2759   2756     p->iOffset = savedOffset;
  2760         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
  2761         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
  2762         -  VdbeNoopComment((v, "End coroutine for right SELECT"));
         2757  +  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
  2763   2758   
  2764   2759     /* Generate a subroutine that outputs the current row of the A
  2765   2760     ** select as the next output row of the compound select.
  2766   2761     */
  2767   2762     VdbeNoopComment((v, "Output routine for A"));
  2768   2763     addrOutA = generateOutputSubroutine(pParse,
  2769   2764                    p, &destA, pDest, regOutA,
................................................................................
  2779   2774                    regPrev, pKeyDup, labelEnd);
  2780   2775     }
  2781   2776     sqlite3KeyInfoUnref(pKeyDup);
  2782   2777   
  2783   2778     /* Generate a subroutine to run when the results from select A
  2784   2779     ** are exhausted and only data in select B remains.
  2785   2780     */
  2786         -  VdbeNoopComment((v, "eof-A subroutine"));
  2787   2781     if( op==TK_EXCEPT || op==TK_INTERSECT ){
  2788         -    addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
         2782  +    addrEofA_noB = addrEofA = labelEnd;
  2789   2783     }else{  
  2790         -    addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
  2791         -    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
  2792         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
         2784  +    VdbeNoopComment((v, "eof-A subroutine"));
         2785  +    addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
         2786  +    addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
  2793   2787       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
  2794   2788       p->nSelectRow += pPrior->nSelectRow;
  2795   2789     }
  2796   2790   
  2797   2791     /* Generate a subroutine to run when the results from select B
  2798   2792     ** are exhausted and only data in select A remains.
  2799   2793     */
  2800   2794     if( op==TK_INTERSECT ){
  2801   2795       addrEofB = addrEofA;
  2802   2796       if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
  2803   2797     }else{  
  2804   2798       VdbeNoopComment((v, "eof-B subroutine"));
  2805         -    addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
  2806         -    sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
  2807         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
         2799  +    addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
         2800  +    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd);
  2808   2801       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
  2809   2802     }
  2810   2803   
  2811   2804     /* Generate code to handle the case of A<B
  2812   2805     */
  2813   2806     VdbeNoopComment((v, "A-lt-B subroutine"));
  2814   2807     addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
  2815         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
  2816         -  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
         2808  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA);
  2817   2809     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2818   2810   
  2819   2811     /* Generate code to handle the case of A==B
  2820   2812     */
  2821   2813     if( op==TK_ALL ){
  2822   2814       addrAeqB = addrAltB;
  2823   2815     }else if( op==TK_INTERSECT ){
  2824   2816       addrAeqB = addrAltB;
  2825   2817       addrAltB++;
  2826   2818     }else{
  2827   2819       VdbeNoopComment((v, "A-eq-B subroutine"));
  2828   2820       addrAeqB =
  2829         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
  2830         -    sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
         2821  +    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA);
  2831   2822       sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2832   2823     }
  2833   2824   
  2834   2825     /* Generate code to handle the case of A>B
  2835   2826     */
  2836   2827     VdbeNoopComment((v, "A-gt-B subroutine"));
  2837   2828     addrAgtB = sqlite3VdbeCurrentAddr(v);
  2838   2829     if( op==TK_ALL || op==TK_UNION ){
  2839   2830       sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
  2840   2831     }
  2841         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
  2842         -  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
         2832  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB);
  2843   2833     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2844   2834   
  2845   2835     /* This code runs once to initialize everything.
  2846   2836     */
  2847   2837     sqlite3VdbeJumpHere(v, j1);
  2848         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
  2849         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
  2850         -  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
  2851         -  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
  2852         -  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
  2853         -  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
         2838  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB);
         2839  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB);
  2854   2840   
  2855   2841     /* Implement the main merge loop
  2856   2842     */
  2857   2843     sqlite3VdbeResolveLabel(v, labelCmpr);
  2858   2844     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
  2859   2845     sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
  2860   2846                            (char*)pKeyMerge, P4_KEYINFO);
................................................................................
  2875   2861   
  2876   2862     /* Reassembly the compound query so that it will be freed correctly
  2877   2863     ** by the calling function */
  2878   2864     if( p->pPrior ){
  2879   2865       sqlite3SelectDelete(db, p->pPrior);
  2880   2866     }
  2881   2867     p->pPrior = pPrior;
         2868  +  pPrior->pNext = p;
  2882   2869   
  2883   2870     /*** TBD:  Insert subroutine calls to close cursors on incomplete
  2884   2871     **** subqueries ****/
  2885   2872     explainComposite(pParse, p->op, iSub1, iSub2, 0);
  2886   2873     return SQLITE_OK;
  2887   2874   }
  2888   2875   #endif
................................................................................
  3140   3127     /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
  3141   3128     ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
  3142   3129     ** because they could be computed at compile-time.  But when LIMIT and OFFSET
  3143   3130     ** became arbitrary expressions, we were forced to add restrictions (13)
  3144   3131     ** and (14). */
  3145   3132     if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
  3146   3133     if( pSub->pOffset ) return 0;                          /* Restriction (14) */
  3147         -  if( p->pRightmost && pSub->pLimit ){
         3134  +  if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
  3148   3135       return 0;                                            /* Restriction (15) */
  3149   3136     }
  3150   3137     if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
  3151   3138     if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
  3152   3139     if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
  3153   3140        return 0;         /* Restrictions (8)(9) */
  3154   3141     }
................................................................................
  3291   3278       p->pOffset = 0;
  3292   3279       pNew = sqlite3SelectDup(db, p, 0);
  3293   3280       p->pOffset = pOffset;
  3294   3281       p->pLimit = pLimit;
  3295   3282       p->pOrderBy = pOrderBy;
  3296   3283       p->pSrc = pSrc;
  3297   3284       p->op = TK_ALL;
  3298         -    p->pRightmost = 0;
  3299   3285       if( pNew==0 ){
  3300         -      pNew = pPrior;
         3286  +      p->pPrior = pPrior;
  3301   3287       }else{
  3302   3288         pNew->pPrior = pPrior;
  3303         -      pNew->pRightmost = 0;
         3289  +      if( pPrior ) pPrior->pNext = pNew;
         3290  +      pNew->pNext = p;
         3291  +      p->pPrior = pNew;
  3304   3292       }
  3305         -    p->pPrior = pNew;
  3306   3293       if( db->mallocFailed ) return 1;
  3307   3294     }
  3308   3295   
  3309   3296     /* Begin flattening the iFrom-th entry of the FROM clause 
  3310   3297     ** in the outer query.
  3311   3298     */
  3312   3299     pSub = pSub1 = pSubitem->pSelect;
................................................................................
  3637   3624     p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
  3638   3625     p->op = TK_SELECT;
  3639   3626     p->pWhere = 0;
  3640   3627     pNew->pGroupBy = 0;
  3641   3628     pNew->pHaving = 0;
  3642   3629     pNew->pOrderBy = 0;
  3643   3630     p->pPrior = 0;
         3631  +  p->pNext = 0;
         3632  +  p->selFlags &= ~SF_Compound;
         3633  +  assert( pNew->pPrior!=0 );
         3634  +  pNew->pPrior->pNext = pNew;
  3644   3635     pNew->pLimit = 0;
  3645   3636     pNew->pOffset = 0;
  3646   3637     return WRC_Continue;
  3647   3638   }
  3648   3639   
  3649   3640   #ifndef SQLITE_OMIT_CTE
  3650   3641   /*
................................................................................
  3824   3815   **
  3825   3816   ** This function is used as the xSelectCallback2() callback by
  3826   3817   ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
  3827   3818   ** names and other FROM clause elements. 
  3828   3819   */
  3829   3820   static void selectPopWith(Walker *pWalker, Select *p){
  3830   3821     Parse *pParse = pWalker->pParse;
  3831         -  if( p->pWith ){
  3832         -    assert( pParse->pWith==p->pWith );
  3833         -    pParse->pWith = p->pWith->pOuter;
         3822  +  With *pWith = findRightmost(p)->pWith;
         3823  +  if( pWith!=0 ){
         3824  +    assert( pParse->pWith==pWith );
         3825  +    pParse->pWith = pWith->pOuter;
  3834   3826     }
  3835   3827   }
  3836   3828   #else
  3837   3829   #define selectPopWith 0
  3838   3830   #endif
  3839   3831   
  3840   3832   /*
................................................................................
  3876   3868       return WRC_Abort;
  3877   3869     }
  3878   3870     if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
  3879   3871       return WRC_Prune;
  3880   3872     }
  3881   3873     pTabList = p->pSrc;
  3882   3874     pEList = p->pEList;
  3883         -  sqlite3WithPush(pParse, p->pWith, 0);
         3875  +  sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
  3884   3876   
  3885   3877     /* Make sure cursor numbers have been assigned to all entries in
  3886   3878     ** the FROM clause of the SELECT statement.
  3887   3879     */
  3888   3880     sqlite3SrcListAssignCursors(pParse, pTabList);
  3889   3881   
  3890   3882     /* Look up every table named in the FROM clause of the select.  If
................................................................................
  4548   4540       if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
  4549   4541         /* This subquery can be absorbed into its parent. */
  4550   4542         if( isAggSub ){
  4551   4543           isAgg = 1;
  4552   4544           p->selFlags |= SF_Aggregate;
  4553   4545         }
  4554   4546         i = -1;
  4555         -    }else if( pTabList->nSrc==1 && (p->selFlags & SF_Materialize)==0
  4556         -      && OptimizationEnabled(db, SQLITE_SubqCoroutine)
         4547  +    }else if( pTabList->nSrc==1
         4548  +           && OptimizationEnabled(db, SQLITE_SubqCoroutine)
  4557   4549       ){
  4558   4550         /* Implement a co-routine that will return a single row of the result
  4559   4551         ** set on each invocation.
  4560   4552         */
  4561         -      int addrTop;
  4562         -      int addrEof;
         4553  +      int addrTop = sqlite3VdbeCurrentAddr(v)+1;
  4563   4554         pItem->regReturn = ++pParse->nMem;
  4564         -      addrEof = ++pParse->nMem;
  4565         -      /* Before coding the OP_Goto to jump to the start of the main routine,
  4566         -      ** ensure that the jump to the verify-schema routine has already
  4567         -      ** been coded. Otherwise, the verify-schema would likely be coded as 
  4568         -      ** part of the co-routine. If the main routine then accessed the 
  4569         -      ** database before invoking the co-routine for the first time (for 
  4570         -      ** example to initialize a LIMIT register from a sub-select), it would 
  4571         -      ** be doing so without having verified the schema version and obtained 
  4572         -      ** the required db locks. See ticket d6b36be38.  */
  4573         -      sqlite3CodeVerifySchema(pParse, -1);
  4574         -      sqlite3VdbeAddOp0(v, OP_Goto);
  4575         -      addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
  4576         -      sqlite3VdbeChangeP5(v, 1);
  4577         -      VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
         4555  +      sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
         4556  +      VdbeComment((v, "%s", pItem->pTab->zName));
  4578   4557         pItem->addrFillSub = addrTop;
  4579         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
  4580         -      sqlite3VdbeChangeP5(v, 1);
  4581   4558         sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
  4582   4559         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
  4583   4560         sqlite3Select(pParse, pSub, &dest);
  4584   4561         pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
  4585   4562         pItem->viaCoroutine = 1;
  4586         -      sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
  4587         -      sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
  4588         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, addrEof);
  4589         -      sqlite3VdbeAddOp1(v, OP_Yield, pItem->regReturn);
  4590         -      VdbeComment((v, "end %s", pItem->pTab->zName));
         4563  +      pItem->regResult = dest.iSdst;
         4564  +      sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
  4591   4565         sqlite3VdbeJumpHere(v, addrTop-1);
  4592   4566         sqlite3ClearTempRegCache(pParse);
  4593   4567       }else{
  4594   4568         /* Generate a subroutine that will fill an ephemeral table with
  4595   4569         ** the content of this subquery.  pItem->addrFillSub will point
  4596   4570         ** to the address of the generated subroutine.  pItem->regReturn
  4597   4571         ** is a register allocated to hold the subroutine return address
................................................................................
  4599   4573         int topAddr;
  4600   4574         int onceAddr = 0;
  4601   4575         int retAddr;
  4602   4576         assert( pItem->addrFillSub==0 );
  4603   4577         pItem->regReturn = ++pParse->nMem;
  4604   4578         topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
  4605   4579         pItem->addrFillSub = topAddr+1;
  4606         -      VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
  4607   4580         if( pItem->isCorrelated==0 ){
  4608   4581           /* If the subquery is not correlated and if we are not inside of
  4609   4582           ** a trigger, then we only need to compute the value of the subquery
  4610   4583           ** once. */
  4611   4584           onceAddr = sqlite3CodeOnce(pParse);
         4585  +        VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
         4586  +      }else{
         4587  +        VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
  4612   4588         }
  4613   4589         sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
  4614   4590         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
  4615   4591         sqlite3Select(pParse, pSub, &dest);
  4616   4592         pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
  4617   4593         if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
  4618   4594         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
................................................................................
  4636   4612     pHaving = p->pHaving;
  4637   4613     sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
  4638   4614   
  4639   4615   #ifndef SQLITE_OMIT_COMPOUND_SELECT
  4640   4616     /* If there is are a sequence of queries, do the earlier ones first.
  4641   4617     */
  4642   4618     if( p->pPrior ){
  4643         -    if( p->pRightmost==0 ){
  4644         -      Select *pLoop, *pRight = 0;
  4645         -      int cnt = 0;
  4646         -      int mxSelect;
  4647         -      for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
  4648         -        pLoop->pRightmost = p;
  4649         -        pLoop->pNext = pRight;
  4650         -        pRight = pLoop;
  4651         -      }
  4652         -      mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
  4653         -      if( mxSelect && cnt>mxSelect ){
  4654         -        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
  4655         -        goto select_end;
  4656         -      }
  4657         -    }
  4658   4619       rc = multiSelect(pParse, p, pDest);
  4659   4620       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
  4660   4621       return rc;
  4661   4622     }
  4662   4623   #endif
  4663   4624   
  4664   4625     /* If there is both a GROUP BY and an ORDER BY clause and they are
................................................................................
  5312   5273     }
  5313   5274   }
  5314   5275   void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
  5315   5276     if( p==0 ){
  5316   5277       sqlite3ExplainPrintf(pVdbe, "(null-select)");
  5317   5278       return;
  5318   5279     }
  5319         -  while( p->pPrior ){
  5320         -    p->pPrior->pNext = p;
  5321         -    p = p->pPrior;
  5322         -  }
  5323   5280     sqlite3ExplainPush(pVdbe);
  5324   5281     while( p ){
  5325   5282       explainOneSelect(pVdbe, p);
  5326   5283       p = p->pNext;
  5327   5284       if( p==0 ) break;
  5328   5285       sqlite3ExplainNL(pVdbe);
  5329   5286       sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));

Changes to src/shell.c.

  1539   1539   
  1540   1540   /*
  1541   1541   ** Text of a help message
  1542   1542   */
  1543   1543   static char zHelp[] =
  1544   1544     ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
  1545   1545     ".bail ON|OFF           Stop after hitting an error.  Default OFF\n"
         1546  +  ".clone NEWDB           Clone data into NEWDB from the existing database\n"
  1546   1547     ".databases             List names and files of attached databases\n"
  1547   1548     ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
  1548   1549     "                         If TABLE specified, only dump tables matching\n"
  1549   1550     "                         LIKE pattern TABLE.\n"
  1550   1551     ".echo ON|OFF           Turn command echo on or off\n"
  1551   1552     ".exit                  Exit this program\n"
  1552   1553     ".explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.\n"
................................................................................
  1891   1892         if( p->n>1 && p->z[p->n-1]=='\r' ) p->n--;
  1892   1893       }
  1893   1894       p->cTerm = c;
  1894   1895     }
  1895   1896     if( p->z ) p->z[p->n] = 0;
  1896   1897     return p->z;
  1897   1898   }
         1899  +
         1900  +/*
         1901  +** Try to transfer data for table zTable.  If an error is seen while
         1902  +** moving forward, try to go backwards.  The backwards movement won't
         1903  +** work for WITHOUT ROWID tables.
         1904  +*/
         1905  +static void tryToCloneData(
         1906  +  struct callback_data *p,
         1907  +  sqlite3 *newDb,
         1908  +  const char *zTable
         1909  +){
         1910  +  sqlite3_stmt *pQuery = 0; 
         1911  +  sqlite3_stmt *pInsert = 0;
         1912  +  char *zQuery = 0;
         1913  +  char *zInsert = 0;
         1914  +  int rc;
         1915  +  int i, j, n;
         1916  +  int nTable = (int)strlen(zTable);
         1917  +  int k = 0;
         1918  +  int cnt = 0;
         1919  +  const int spinRate = 10000;
         1920  +
         1921  +  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
         1922  +  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
         1923  +  if( rc ){
         1924  +    fprintf(stderr, "Error %d: %s on [%s]\n",
         1925  +            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
         1926  +            zQuery);
         1927  +    goto end_data_xfer;
         1928  +  }
         1929  +  n = sqlite3_column_count(pQuery);
         1930  +  zInsert = sqlite3_malloc(200 + nTable + n*3);
         1931  +  if( zInsert==0 ){
         1932  +    fprintf(stderr, "out of memory\n");
         1933  +    goto end_data_xfer;
         1934  +  }
         1935  +  sqlite3_snprintf(200+nTable,zInsert,
         1936  +                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
         1937  +  i = (int)strlen(zInsert);
         1938  +  for(j=1; j<n; j++){
         1939  +    memcpy(zInsert+i, ",?", 2);
         1940  +    i += 2;
         1941  +  }
         1942  +  memcpy(zInsert+i, ");", 3);
         1943  +  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
         1944  +  if( rc ){
         1945  +    fprintf(stderr, "Error %d: %s on [%s]\n",
         1946  +            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
         1947  +            zQuery);
         1948  +    goto end_data_xfer;
         1949  +  }
         1950  +  for(k=0; k<2; k++){
         1951  +    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
         1952  +      for(i=0; i<n; i++){
         1953  +        switch( sqlite3_column_type(pQuery, i) ){
         1954  +          case SQLITE_NULL: {
         1955  +            sqlite3_bind_null(pInsert, i+1);
         1956  +            break;
         1957  +          }
         1958  +          case SQLITE_INTEGER: {
         1959  +            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
         1960  +            break;
         1961  +          }
         1962  +          case SQLITE_FLOAT: {
         1963  +            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
         1964  +            break;
         1965  +          }
         1966  +          case SQLITE_TEXT: {
         1967  +            sqlite3_bind_text(pInsert, i+1,
         1968  +                             (const char*)sqlite3_column_text(pQuery,i),
         1969  +                             -1, SQLITE_STATIC);
         1970  +            break;
         1971  +          }
         1972  +          case SQLITE_BLOB: {
         1973  +            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
         1974  +                                            sqlite3_column_bytes(pQuery,i),
         1975  +                                            SQLITE_STATIC);
         1976  +            break;
         1977  +          }
         1978  +        }
         1979  +      } /* End for */
         1980  +      rc = sqlite3_step(pInsert);
         1981  +      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
         1982  +        fprintf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
         1983  +                        sqlite3_errmsg(newDb));
         1984  +      }
         1985  +      sqlite3_reset(pInsert);
         1986  +      cnt++;
         1987  +      if( (cnt%spinRate)==0 ){
         1988  +        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
         1989  +        fflush(stdout);
         1990  +      }
         1991  +    } /* End while */
         1992  +    if( rc==SQLITE_DONE ) break;
         1993  +    sqlite3_finalize(pQuery);
         1994  +    sqlite3_free(zQuery);
         1995  +    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
         1996  +                             zTable);
         1997  +    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
         1998  +    if( rc ){
         1999  +      fprintf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
         2000  +      break;
         2001  +    }
         2002  +  } /* End for(k=0...) */
         2003  +
         2004  +end_data_xfer:
         2005  +  sqlite3_finalize(pQuery);
         2006  +  sqlite3_finalize(pInsert);
         2007  +  sqlite3_free(zQuery);
         2008  +  sqlite3_free(zInsert);
         2009  +}
         2010  +
         2011  +
         2012  +/*
         2013  +** Try to transfer all rows of the schema that match zWhere.  For
         2014  +** each row, invoke xForEach() on the object defined by that row.
         2015  +** If an error is encountered while moving forward through the
         2016  +** sqlite_master table, try again moving backwards.
         2017  +*/
         2018  +static void tryToCloneSchema(
         2019  +  struct callback_data *p,
         2020  +  sqlite3 *newDb,
         2021  +  const char *zWhere,
         2022  +  void (*xForEach)(struct callback_data*,sqlite3*,const char*)
         2023  +){
         2024  +  sqlite3_stmt *pQuery = 0;
         2025  +  char *zQuery = 0;
         2026  +  int rc;
         2027  +  const unsigned char *zName;
         2028  +  const unsigned char *zSql;
         2029  +  char *zErrMsg = 0;
         2030  +
         2031  +  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
         2032  +                           " WHERE %s", zWhere);
         2033  +  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
         2034  +  if( rc ){
         2035  +    fprintf(stderr, "Error: (%d) %s on [%s]\n",
         2036  +                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
         2037  +                    zQuery);
         2038  +    goto end_schema_xfer;
         2039  +  }
         2040  +  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
         2041  +    zName = sqlite3_column_text(pQuery, 0);
         2042  +    zSql = sqlite3_column_text(pQuery, 1);
         2043  +    printf("%s... ", zName); fflush(stdout);
         2044  +    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
         2045  +    if( zErrMsg ){
         2046  +      fprintf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
         2047  +      sqlite3_free(zErrMsg);
         2048  +      zErrMsg = 0;
         2049  +    }
         2050  +    if( xForEach ){
         2051  +      xForEach(p, newDb, (const char*)zName);
         2052  +    }
         2053  +    printf("done\n");
         2054  +  }
         2055  +  if( rc!=SQLITE_DONE ){
         2056  +    sqlite3_finalize(pQuery);
         2057  +    sqlite3_free(zQuery);
         2058  +    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
         2059  +                             " WHERE %s ORDER BY rowid DESC", zWhere);
         2060  +    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
         2061  +    if( rc ){
         2062  +      fprintf(stderr, "Error: (%d) %s on [%s]\n",
         2063  +                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
         2064  +                      zQuery);
         2065  +      goto end_schema_xfer;
         2066  +    }
         2067  +    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
         2068  +      zName = sqlite3_column_text(pQuery, 0);
         2069  +      zSql = sqlite3_column_text(pQuery, 1);
         2070  +      printf("%s... ", zName); fflush(stdout);
         2071  +      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
         2072  +      if( zErrMsg ){
         2073  +        fprintf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
         2074  +        sqlite3_free(zErrMsg);
         2075  +        zErrMsg = 0;
         2076  +      }
         2077  +      if( xForEach ){
         2078  +        xForEach(p, newDb, (const char*)zName);
         2079  +      }
         2080  +      printf("done\n");
         2081  +    }
         2082  +  }
         2083  +end_schema_xfer:
         2084  +  sqlite3_finalize(pQuery);
         2085  +  sqlite3_free(zQuery);
         2086  +}
         2087  +
         2088  +/*
         2089  +** Open a new database file named "zNewDb".  Try to recover as much information
         2090  +** as possible out of the main database (which might be corrupt) and write it
         2091  +** into zNewDb.
         2092  +*/
         2093  +static void tryToClone(struct callback_data *p, const char *zNewDb){
         2094  +  int rc;
         2095  +  sqlite3 *newDb = 0;
         2096  +  if( access(zNewDb,0)==0 ){
         2097  +    fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
         2098  +    return;
         2099  +  }
         2100  +  rc = sqlite3_open(zNewDb, &newDb);
         2101  +  if( rc ){
         2102  +    fprintf(stderr, "Cannot create output database: %s\n",
         2103  +            sqlite3_errmsg(newDb));
         2104  +  }else{
         2105  +    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
         2106  +    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
         2107  +    tryToCloneSchema(p, newDb, "type!='table'", 0);
         2108  +    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
         2109  +  }
         2110  +  sqlite3_close(newDb);
         2111  +}
  1898   2112   
  1899   2113   /*
  1900   2114   ** If an input line begins with "." then invoke this routine to
  1901   2115   ** process that line.
  1902   2116   **
  1903   2117   ** Return 1 on error, 2 to exit, and 0 otherwise.
  1904   2118   */
................................................................................
  1998   2212   
  1999   2213     /* The undocumented ".breakpoint" command causes a call to the no-op
  2000   2214     ** routine named test_breakpoint().
  2001   2215     */
  2002   2216     if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
  2003   2217       test_breakpoint();
  2004   2218     }else
         2219  +
         2220  +  if( c=='c' && strncmp(azArg[0], "clone", n)==0 && nArg>1 && nArg<3 ){
         2221  +    tryToClone(p, azArg[1]);
         2222  +  }else
  2005   2223   
  2006   2224     if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
  2007   2225       struct callback_data data;
  2008   2226       char *zErrMsg = 0;
  2009   2227       open_db(p, 0);
  2010   2228       memcpy(&data, p, sizeof(data));
  2011   2229       data.showHeader = 1;

Changes to src/sqliteInt.h.

    29     29   ** on the compiler command line.  This is necessary if you are compiling
    30     30   ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
    31     31   ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
    32     32   ** without this option, LFS is enable.  But LFS does not exist in the kernel
    33     33   ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
    34     34   ** portability you should omit LFS.
    35     35   **
           36  +** The previous paragraph was written in 2005.  (This paragraph is written
           37  +** on 2008-11-28.) These days, all Linux kernels support large files, so
           38  +** you should probably leave LFS enabled.  But some embedded platforms might
           39  +** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
           40  +**
    36     41   ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
    37     42   */
    38     43   #ifndef SQLITE_DISABLE_LFS
    39     44   # define _LARGE_FILE       1
    40     45   # ifndef _FILE_OFFSET_BITS
    41     46   #   define _FILE_OFFSET_BITS 64
    42     47   # endif
................................................................................
  1087   1092   #define OptimizationEnabled(db, mask)   1
  1088   1093   #endif
  1089   1094   
  1090   1095   /*
  1091   1096   ** Return true if it OK to factor constant expressions into the initialization
  1092   1097   ** code. The argument is a Parse object for the code generator.
  1093   1098   */
  1094         -#define ConstFactorOk(P) \
  1095         -  ((P)->cookieGoto>0 && OptimizationEnabled((P)->db,SQLITE_FactorOutConst))
         1099  +#define ConstFactorOk(P) ((P)->okConstFactor)
  1096   1100   
  1097   1101   /*
  1098   1102   ** Possible values for the sqlite.magic field.
  1099   1103   ** The numbers are obtained at random and have no special meaning, other
  1100   1104   ** than being distinct from one another.
  1101   1105   */
  1102   1106   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
................................................................................
  2019   2023       char *zDatabase;  /* Name of database holding this table */
  2020   2024       char *zName;      /* Name of the table */
  2021   2025       char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
  2022   2026       Table *pTab;      /* An SQL table corresponding to zName */
  2023   2027       Select *pSelect;  /* A SELECT statement used in place of a table name */
  2024   2028       int addrFillSub;  /* Address of subroutine to manifest a subquery */
  2025   2029       int regReturn;    /* Register holding return address of addrFillSub */
         2030  +    int regResult;    /* Registers holding results of a co-routine */
  2026   2031       u8 jointype;      /* Type of join between this able and the previous */
  2027   2032       unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
  2028   2033       unsigned isCorrelated :1;  /* True if sub-query is correlated */
  2029   2034       unsigned viaCoroutine :1;  /* Implemented as a co-routine */
  2030   2035       unsigned isRecursive :1;   /* True for recursive reference in WITH */
  2031   2036   #ifndef SQLITE_OMIT_EXPLAIN
  2032   2037       u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
................................................................................
  2147   2152     SrcList *pSrc;         /* The FROM clause */
  2148   2153     Expr *pWhere;          /* The WHERE clause */
  2149   2154     ExprList *pGroupBy;    /* The GROUP BY clause */
  2150   2155     Expr *pHaving;         /* The HAVING clause */
  2151   2156     ExprList *pOrderBy;    /* The ORDER BY clause */
  2152   2157     Select *pPrior;        /* Prior select in a compound select statement */
  2153   2158     Select *pNext;         /* Next select to the left in a compound */
  2154         -  Select *pRightmost;    /* Right-most select in a compound select statement */
  2155   2159     Expr *pLimit;          /* LIMIT expression. NULL means not used. */
  2156   2160     Expr *pOffset;         /* OFFSET expression. NULL means not used. */
  2157   2161     With *pWith;           /* WITH clause attached to this select. Or NULL. */
  2158   2162   };
  2159   2163   
  2160   2164   /*
  2161   2165   ** Allowed values for Select.selFlags.  The "SF" prefix stands for
................................................................................
  2165   2169   #define SF_Resolved        0x0002  /* Identifiers have been resolved */
  2166   2170   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
  2167   2171   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
  2168   2172   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
  2169   2173   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
  2170   2174   #define SF_UseSorter       0x0040  /* Sort using a sorter */
  2171   2175   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  2172         -#define SF_Materialize     0x0100  /* Force materialization of views */
         2176  +#define SF_Materialize     0x0100  /* NOT USED */
  2173   2177   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  2174   2178   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
  2175   2179   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
         2180  +#define SF_Compound        0x1000  /* Part of a compound query */
  2176   2181   
  2177   2182   
  2178   2183   /*
  2179   2184   ** The results of a SELECT can be distributed in several ways, as defined
  2180   2185   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2181   2186   ** Type".
  2182   2187   **
................................................................................
  2353   2358     u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
  2354   2359     u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
  2355   2360     u8 nColCache;        /* Number of entries in aColCache[] */
  2356   2361     u8 iColCache;        /* Next entry in aColCache[] to replace */
  2357   2362     u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
  2358   2363     u8 mayAbort;         /* True if statement may throw an ABORT exception */
  2359   2364     u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
         2365  +  u8 okConstFactor;    /* OK to factor out constants */
  2360   2366     int aTempReg[8];     /* Holding area for temporary registers */
  2361   2367     int nRangeReg;       /* Size of the temporary register block */
  2362   2368     int iRangeReg;       /* First register in temporary register block */
  2363   2369     int nErr;            /* Number of errors seen */
  2364   2370     int nTab;            /* Number of previously allocated VDBE cursors */
  2365   2371     int nMem;            /* Number of memory cells used so far */
  2366   2372     int nSet;            /* Number of sets used so far */
  2367   2373     int nOnce;           /* Number of OP_Once instructions so far */
  2368   2374     int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
  2369         -  int nLabel;          /* Number of labels used */
  2370         -  int *aLabel;         /* Space to hold the labels */
  2371   2375     int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
  2372   2376     int ckBase;          /* Base register of data during check constraints */
  2373   2377     int iPartIdxTab;     /* Table corresponding to a partial index */
  2374   2378     int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
  2375   2379     int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
         2380  +  int nLabel;          /* Number of labels used */
         2381  +  int *aLabel;         /* Space to hold the labels */
  2376   2382     struct yColCache {
  2377   2383       int iTable;           /* Table cursor number */
  2378         -    int iColumn;          /* Table column number */
         2384  +    i16 iColumn;          /* Table column number */
  2379   2385       u8 tempReg;           /* iReg is a temp register that needs to be freed */
  2380   2386       int iLevel;           /* Nesting level */
  2381   2387       int iReg;             /* Reg with value of this column. 0 means none. */
  2382   2388       int lru;              /* Least recently used entry has the smallest value */
  2383   2389     } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
  2384   2390     ExprList *pConstExpr;/* Constant expressions */
         2391  +  Token constraintName;/* Name of the constraint currently being parsed */
  2385   2392     yDbMask writeMask;   /* Start a write transaction on these databases */
  2386   2393     yDbMask cookieMask;  /* Bitmask of schema verified databases */
  2387         -  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
  2388   2394     int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
  2389   2395     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
  2390   2396     int regRoot;         /* Register holding root page number for new objects */
  2391   2397     int nMaxArg;         /* Max args passed to user function by sub-program */
  2392         -  Token constraintName;/* Name of the constraint currently being parsed */
  2393   2398   #ifndef SQLITE_OMIT_SHARED_CACHE
  2394   2399     int nTableLock;        /* Number of locks in aTableLock */
  2395   2400     TableLock *aTableLock; /* Required table locks for shared-cache mode */
  2396   2401   #endif
  2397   2402     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
  2398   2403   
  2399   2404     /* Information used while coding trigger programs. */
................................................................................
  2404   2409     u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
  2405   2410     u32 oldmask;         /* Mask of old.* columns referenced */
  2406   2411     u32 newmask;         /* Mask of new.* columns referenced */
  2407   2412     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
  2408   2413     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
  2409   2414     u8 disableTriggers;  /* True to disable triggers */
  2410   2415   
  2411         -  /* Above is constant between recursions.  Below is reset before and after
  2412         -  ** each recursion */
         2416  +  /************************************************************************
         2417  +  ** Above is constant between recursions.  Below is reset before and after
         2418  +  ** each recursion.  The boundary between these two regions is determined
         2419  +  ** using offsetof(Parse,nVar) so the nVar field must be the first field
         2420  +  ** in the recursive region.
         2421  +  ************************************************************************/
  2413   2422   
  2414   2423     int nVar;                 /* Number of '?' variables seen in the SQL so far */
  2415   2424     int nzVar;                /* Number of available slots in azVar[] */
  2416   2425     u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
         2426  +  u8 bFreeWith;             /* True if pWith should be freed with parser */
  2417   2427     u8 explain;               /* True if the EXPLAIN flag is found on the query */
  2418   2428   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2419   2429     u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
  2420   2430     int nVtabLock;            /* Number of virtual tables to lock */
  2421   2431   #endif
  2422   2432     int nAlias;               /* Number of aliased result set columns */
  2423   2433     int nHeight;              /* Expression tree height of current sub-select */
................................................................................
  2436   2446   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2437   2447     Token sArg;               /* Complete text of a module argument */
  2438   2448     Table **apVtabLock;       /* Pointer to virtual tables needing locking */
  2439   2449   #endif
  2440   2450     Table *pZombieTab;        /* List of Table objects to delete after code gen */
  2441   2451     TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
  2442   2452     With *pWith;              /* Current WITH clause, or NULL */
  2443         -  u8 bFreeWith;             /* True if pWith should be freed with parser */
  2444   2453   };
  2445   2454   
  2446   2455   /*
  2447   2456   ** Return true if currently inside an sqlite3_declare_vtab() call.
  2448   2457   */
  2449   2458   #ifdef SQLITE_OMIT_VIRTUALTABLE
  2450   2459     #define IN_DECLARE_VTAB 0

Changes to src/tclsqlite.c.

  1065   1065         ** and try to rollback the transaction.
  1066   1066         **
  1067   1067         ** But it could also be that the user executed one or more BEGIN, 
  1068   1068         ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing
  1069   1069         ** this method's logic. Not clear how this would be best handled.
  1070   1070         */
  1071   1071       if( rc!=TCL_ERROR ){
  1072         -      Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0);
         1072  +      Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
  1073   1073         rc = TCL_ERROR;
  1074   1074       }
  1075   1075       sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0);
  1076   1076     }
  1077   1077     pDb->disableAuth--;
  1078   1078   
  1079   1079     return rc;
................................................................................
  1756   1756     ** SQLITE_IGNORE, or SQLITE_DENY.  Any other return value is an error.
  1757   1757     **
  1758   1758     ** If this method is invoked with no arguments, the current authorization
  1759   1759     ** callback string is returned.
  1760   1760     */
  1761   1761     case DB_AUTHORIZER: {
  1762   1762   #ifdef SQLITE_OMIT_AUTHORIZATION
  1763         -    Tcl_AppendResult(interp, "authorization not available in this build", 0);
         1763  +    Tcl_AppendResult(interp, "authorization not available in this build",
         1764  +                     (char*)0);
  1764   1765       return TCL_ERROR;
  1765   1766   #else
  1766   1767       if( objc>3 ){
  1767   1768         Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
  1768   1769         return TCL_ERROR;
  1769   1770       }else if( objc==2 ){
  1770   1771         if( pDb->zAuth ){
  1771         -        Tcl_AppendResult(interp, pDb->zAuth, 0);
         1772  +        Tcl_AppendResult(interp, pDb->zAuth, (char*)0);
  1772   1773         }
  1773   1774       }else{
  1774   1775         char *zAuth;
  1775   1776         int len;
  1776   1777         if( pDb->zAuth ){
  1777   1778           Tcl_Free(pDb->zAuth);
  1778   1779         }
................................................................................
  1850   1851     */
  1851   1852     case DB_BUSY: {
  1852   1853       if( objc>3 ){
  1853   1854         Tcl_WrongNumArgs(interp, 2, objv, "CALLBACK");
  1854   1855         return TCL_ERROR;
  1855   1856       }else if( objc==2 ){
  1856   1857         if( pDb->zBusy ){
  1857         -        Tcl_AppendResult(interp, pDb->zBusy, 0);
         1858  +        Tcl_AppendResult(interp, pDb->zBusy, (char*)0);
  1858   1859         }
  1859   1860       }else{
  1860   1861         char *zBusy;
  1861   1862         int len;
  1862   1863         if( pDb->zBusy ){
  1863   1864           Tcl_Free(pDb->zBusy);
  1864   1865         }
................................................................................
  1904   1905       }else if( *subCmd=='s' && strcmp(subCmd,"size")==0 ){
  1905   1906         if( objc!=4 ){
  1906   1907           Tcl_WrongNumArgs(interp, 2, objv, "size n");
  1907   1908           return TCL_ERROR;
  1908   1909         }else{
  1909   1910           if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){
  1910   1911             Tcl_AppendResult( interp, "cannot convert \"", 
  1911         -               Tcl_GetStringFromObj(objv[3],0), "\" to integer", 0);
         1912  +               Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0);
  1912   1913             return TCL_ERROR;
  1913   1914           }else{
  1914   1915             if( n<0 ){
  1915   1916               flushStmtCache( pDb );
  1916   1917               n = 0;
  1917   1918             }else if( n>MAX_PREPARED_STMTS ){
  1918   1919               n = MAX_PREPARED_STMTS;
  1919   1920             }
  1920   1921             pDb->maxStmt = n;
  1921   1922           }
  1922   1923         }
  1923   1924       }else{
  1924   1925         Tcl_AppendResult( interp, "bad option \"", 
  1925         -          Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", 0);
         1926  +          Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size",
         1927  +          (char*)0);
  1926   1928         return TCL_ERROR;
  1927   1929       }
  1928   1930       break;
  1929   1931     }
  1930   1932   
  1931   1933     /*     $db changes
  1932   1934     **
................................................................................
  2015   2017     */
  2016   2018     case DB_COMMIT_HOOK: {
  2017   2019       if( objc>3 ){
  2018   2020         Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
  2019   2021         return TCL_ERROR;
  2020   2022       }else if( objc==2 ){
  2021   2023         if( pDb->zCommit ){
  2022         -        Tcl_AppendResult(interp, pDb->zCommit, 0);
         2024  +        Tcl_AppendResult(interp, pDb->zCommit, (char*)0);
  2023   2025         }
  2024   2026       }else{
  2025   2027         const char *zCommit;
  2026   2028         int len;
  2027   2029         if( pDb->zCommit ){
  2028   2030           Tcl_Free(pDb->zCommit);
  2029   2031         }
................................................................................
  2120   2122       }
  2121   2123       zConflict = Tcl_GetStringFromObj(objv[2], 0);
  2122   2124       zTable = Tcl_GetStringFromObj(objv[3], 0);
  2123   2125       zFile = Tcl_GetStringFromObj(objv[4], 0);
  2124   2126       nSep = strlen30(zSep);
  2125   2127       nNull = strlen30(zNull);
  2126   2128       if( nSep==0 ){
  2127         -      Tcl_AppendResult(interp,"Error: non-null separator required for copy",0);
         2129  +      Tcl_AppendResult(interp,"Error: non-null separator required for copy",
         2130  +                       (char*)0);
  2128   2131         return TCL_ERROR;
  2129   2132       }
  2130   2133       if(strcmp(zConflict, "rollback") != 0 &&
  2131   2134          strcmp(zConflict, "abort"   ) != 0 &&
  2132   2135          strcmp(zConflict, "fail"    ) != 0 &&
  2133   2136          strcmp(zConflict, "ignore"  ) != 0 &&
  2134   2137          strcmp(zConflict, "replace" ) != 0 ) {
  2135   2138         Tcl_AppendResult(interp, "Error: \"", zConflict, 
  2136   2139               "\", conflict-algorithm must be one of: rollback, "
  2137         -            "abort, fail, ignore, or replace", 0);
         2140  +            "abort, fail, ignore, or replace", (char*)0);
  2138   2141         return TCL_ERROR;
  2139   2142       }
  2140   2143       zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
  2141   2144       if( zSql==0 ){
  2142         -      Tcl_AppendResult(interp, "Error: no such table: ", zTable, 0);
         2145  +      Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0);
  2143   2146         return TCL_ERROR;
  2144   2147       }
  2145   2148       nByte = strlen30(zSql);
  2146   2149       rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
  2147   2150       sqlite3_free(zSql);
  2148   2151       if( rc ){
  2149         -      Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
         2152  +      Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0);
  2150   2153         nCol = 0;
  2151   2154       }else{
  2152   2155         nCol = sqlite3_column_count(pStmt);
  2153   2156       }
  2154   2157       sqlite3_finalize(pStmt);
  2155   2158       if( nCol==0 ) {
  2156   2159         return TCL_ERROR;
  2157   2160       }
  2158   2161       zSql = malloc( nByte + 50 + nCol*2 );
  2159   2162       if( zSql==0 ) {
  2160         -      Tcl_AppendResult(interp, "Error: can't malloc()", 0);
         2163  +      Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0);
  2161   2164         return TCL_ERROR;
  2162   2165       }
  2163   2166       sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
  2164   2167            zConflict, zTable);
  2165   2168       j = strlen30(zSql);
  2166   2169       for(i=1; i<nCol; i++){
  2167   2170         zSql[j++] = ',';
................................................................................
  2168   2171         zSql[j++] = '?';
  2169   2172       }
  2170   2173       zSql[j++] = ')';
  2171   2174       zSql[j] = 0;
  2172   2175       rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
  2173   2176       free(zSql);
  2174   2177       if( rc ){
  2175         -      Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), 0);
         2178  +      Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0);
  2176   2179         sqlite3_finalize(pStmt);
  2177   2180         return TCL_ERROR;
  2178   2181       }
  2179   2182       in = fopen(zFile, "rb");
  2180   2183       if( in==0 ){
  2181   2184         Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL);
  2182   2185         sqlite3_finalize(pStmt);
  2183   2186         return TCL_ERROR;
  2184   2187       }
  2185   2188       azCol = malloc( sizeof(azCol[0])*(nCol+1) );
  2186   2189       if( azCol==0 ) {
  2187         -      Tcl_AppendResult(interp, "Error: can't malloc()", 0);
         2190  +      Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0);
  2188   2191         fclose(in);
  2189   2192         return TCL_ERROR;
  2190   2193       }
  2191   2194       (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0);
  2192   2195       zCommit = "COMMIT";
  2193   2196       while( (zLine = local_getline(0, in))!=0 ){
  2194   2197         char *z;
................................................................................
  2208   2211           char *zErr;
  2209   2212           int nErr = strlen30(zFile) + 200;
  2210   2213           zErr = malloc(nErr);
  2211   2214           if( zErr ){
  2212   2215             sqlite3_snprintf(nErr, zErr,
  2213   2216                "Error: %s line %d: expected %d columns of data but found %d",
  2214   2217                zFile, lineno, nCol, i+1);
  2215         -          Tcl_AppendResult(interp, zErr, 0);
         2218  +          Tcl_AppendResult(interp, zErr, (char*)0);
  2216   2219             free(zErr);
  2217   2220           }
  2218   2221           zCommit = "ROLLBACK";
  2219   2222           break;
  2220   2223         }
  2221   2224         for(i=0; i<nCol; i++){
  2222   2225           /* check for null data, if so, bind as null */
................................................................................
  2228   2231             sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
  2229   2232           }
  2230   2233         }
  2231   2234         sqlite3_step(pStmt);
  2232   2235         rc = sqlite3_reset(pStmt);
  2233   2236         free(zLine);
  2234   2237         if( rc!=SQLITE_OK ){
  2235         -        Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), 0);
         2238  +        Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0);
  2236   2239           zCommit = "ROLLBACK";
  2237   2240           break;
  2238   2241         }
  2239   2242       }
  2240   2243       free(azCol);
  2241   2244       fclose(in);
  2242   2245       sqlite3_finalize(pStmt);
................................................................................
  2246   2249         /* success, set result as number of lines processed */
  2247   2250         pResult = Tcl_GetObjResult(interp);
  2248   2251         Tcl_SetIntObj(pResult, lineno);
  2249   2252         rc = TCL_OK;
  2250   2253       }else{
  2251   2254         /* failure, append lineno where failed */
  2252   2255         sqlite3_snprintf(sizeof(zLineNum), zLineNum,"%d",lineno);
  2253         -      Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,0);
         2256  +      Tcl_AppendResult(interp,", failed while processing line: ",zLineNum,
         2257  +                       (char*)0);
  2254   2258         rc = TCL_ERROR;
  2255   2259       }
  2256   2260       break;
  2257   2261     }
  2258   2262   
  2259   2263     /*
  2260   2264     **    $db enable_load_extension BOOLEAN
................................................................................
  2272   2276       if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
  2273   2277         return TCL_ERROR;
  2274   2278       }
  2275   2279       sqlite3_enable_load_extension(pDb->db, onoff);
  2276   2280       break;
  2277   2281   #else
  2278   2282       Tcl_AppendResult(interp, "extension loading is turned off at compile-time",
  2279         -                     0);
         2283  +                     (char*)0);
  2280   2284       return TCL_ERROR;
  2281   2285   #endif
  2282   2286     }
  2283   2287   
  2284   2288     /*
  2285   2289     **    $db errorcode
  2286   2290     **
................................................................................
  2430   2434     }
  2431   2435   
  2432   2436     /*
  2433   2437     **     $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID
  2434   2438     */
  2435   2439     case DB_INCRBLOB: {
  2436   2440   #ifdef SQLITE_OMIT_INCRBLOB
  2437         -    Tcl_AppendResult(interp, "incrblob not available in this build", 0);
         2441  +    Tcl_AppendResult(interp, "incrblob not available in this build", (char*)0);
  2438   2442       return TCL_ERROR;
  2439   2443   #else
  2440   2444       int isReadonly = 0;
  2441   2445       const char *zDb = "main";
  2442   2446       const char *zTable;
  2443   2447       const char *zColumn;
  2444   2448       Tcl_WideInt iRow;
................................................................................
  2537   2541     ** 
  2538   2542     ** Invoke the given callback every N virtual machine opcodes while executing
  2539   2543     ** queries.
  2540   2544     */
  2541   2545     case DB_PROGRESS: {
  2542   2546       if( objc==2 ){
  2543   2547         if( pDb->zProgress ){
  2544         -        Tcl_AppendResult(interp, pDb->zProgress, 0);
         2548  +        Tcl_AppendResult(interp, pDb->zProgress, (char*)0);
  2545   2549         }
  2546   2550       }else if( objc==4 ){
  2547   2551         char *zProgress;
  2548   2552         int len;
  2549   2553         int N;
  2550   2554         if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){
  2551   2555           return TCL_ERROR;
................................................................................
  2583   2587     */
  2584   2588     case DB_PROFILE: {
  2585   2589       if( objc>3 ){
  2586   2590         Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
  2587   2591         return TCL_ERROR;
  2588   2592       }else if( objc==2 ){
  2589   2593         if( pDb->zProfile ){
  2590         -        Tcl_AppendResult(interp, pDb->zProfile, 0);
         2594  +        Tcl_AppendResult(interp, pDb->zProfile, (char*)0);
  2591   2595         }
  2592   2596       }else{
  2593   2597         char *zProfile;
  2594   2598         int len;
  2595   2599         if( pDb->zProfile ){
  2596   2600           Tcl_Free(pDb->zProfile);
  2597   2601         }
................................................................................
  2628   2632         Tcl_WrongNumArgs(interp, 2, objv, "KEY");
  2629   2633         return TCL_ERROR;
  2630   2634       }
  2631   2635   #ifdef SQLITE_HAS_CODEC
  2632   2636       pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
  2633   2637       rc = sqlite3_rekey(pDb->db, pKey, nKey);
  2634   2638       if( rc ){
  2635         -      Tcl_AppendResult(interp, sqlite3_errstr(rc), 0);
         2639  +      Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0);
  2636   2640         rc = TCL_ERROR;
  2637   2641       }
  2638   2642   #endif
  2639   2643       break;
  2640   2644     }
  2641   2645   
  2642   2646     /*    $db restore ?DATABASE? FILENAME
................................................................................
  2769   2773     */
  2770   2774     case DB_TRACE: {
  2771   2775       if( objc>3 ){
  2772   2776         Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
  2773   2777         return TCL_ERROR;
  2774   2778       }else if( objc==2 ){
  2775   2779         if( pDb->zTrace ){
  2776         -        Tcl_AppendResult(interp, pDb->zTrace, 0);
         2780  +        Tcl_AppendResult(interp, pDb->zTrace, (char*)0);
  2777   2781         }
  2778   2782       }else{
  2779   2783         char *zTrace;
  2780   2784         int len;
  2781   2785         if( pDb->zTrace ){
  2782   2786           Tcl_Free(pDb->zTrace);
  2783   2787         }
................................................................................
  2840   2844       pScript = objv[objc-1];
  2841   2845   
  2842   2846       /* Run the SQLite BEGIN command to open a transaction or savepoint. */
  2843   2847       pDb->disableAuth++;
  2844   2848       rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0);
  2845   2849       pDb->disableAuth--;
  2846   2850       if( rc!=SQLITE_OK ){
  2847         -      Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0);
         2851  +      Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
  2848   2852         return TCL_ERROR;
  2849   2853       }
  2850   2854       pDb->nTransaction++;
  2851   2855   
  2852   2856       /* If using NRE, schedule a callback to invoke the script pScript, then
  2853   2857       ** a second callback to commit (or rollback) the transaction or savepoint
  2854   2858       ** opened above. If not using NRE, evaluate the script directly, then
................................................................................
  2864   2868     }
  2865   2869   
  2866   2870     /*
  2867   2871     **    $db unlock_notify ?script?
  2868   2872     */
  2869   2873     case DB_UNLOCK_NOTIFY: {
  2870   2874   #ifndef SQLITE_ENABLE_UNLOCK_NOTIFY
  2871         -    Tcl_AppendResult(interp, "unlock_notify not available in this build", 0);
         2875  +    Tcl_AppendResult(interp, "unlock_notify not available in this build",
         2876  +                     (char*)0);
  2872   2877       rc = TCL_ERROR;
  2873   2878   #else
  2874   2879       if( objc!=2 && objc!=3 ){
  2875   2880         Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?");
  2876   2881         rc = TCL_ERROR;
  2877   2882       }else{
  2878   2883         void (*xNotify)(void **, int) = 0;
................................................................................
  2887   2892           xNotify = DbUnlockNotify;
  2888   2893           pNotifyArg = (void *)pDb;
  2889   2894           pDb->pUnlockNotify = objv[2];
  2890   2895           Tcl_IncrRefCount(pDb->pUnlockNotify);
  2891   2896         }
  2892   2897     
  2893   2898         if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){
  2894         -        Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), 0);
         2899  +        Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
  2895   2900           rc = TCL_ERROR;
  2896   2901         }
  2897   2902       }
  2898   2903   #endif
  2899   2904       break;
  2900   2905     }
  2901   2906   
................................................................................
  3077   3082   #else
  3078   3083     flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX;
  3079   3084   #endif
  3080   3085   
  3081   3086     if( objc==2 ){
  3082   3087       zArg = Tcl_GetStringFromObj(objv[1], 0);
  3083   3088       if( strcmp(zArg,"-version")==0 ){
  3084         -      Tcl_AppendResult(interp,sqlite3_libversion(),0);
         3089  +      Tcl_AppendResult(interp,sqlite3_libversion(), (char*)0);
  3085   3090         return TCL_OK;
  3086   3091       }
  3087   3092       if( strcmp(zArg,"-has-codec")==0 ){
  3088   3093   #ifdef SQLITE_HAS_CODEC
  3089         -      Tcl_AppendResult(interp,"1",0);
         3094  +      Tcl_AppendResult(interp,"1",(char*)0);
  3090   3095   #else
  3091         -      Tcl_AppendResult(interp,"0",0);
         3096  +      Tcl_AppendResult(interp,"0",(char*)0);
  3092   3097   #endif
  3093   3098         return TCL_OK;
  3094   3099       }
  3095   3100     }
  3096   3101     for(i=3; i+1<objc; i+=2){
  3097   3102       zArg = Tcl_GetString(objv[i]);
  3098   3103       if( strcmp(zArg,"-key")==0 ){
................................................................................
  3566   3571     MD5Context ctx;
  3567   3572     unsigned char digest[16];
  3568   3573     char zBuf[50];
  3569   3574     void (*converter)(unsigned char*, char*);
  3570   3575   
  3571   3576     if( argc!=2 ){
  3572   3577       Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 
  3573         -        " TEXT\"", 0);
         3578  +        " TEXT\"", (char*)0);
  3574   3579       return TCL_ERROR;
  3575   3580     }
  3576   3581     MD5Init(&ctx);
  3577   3582     MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1]));
  3578   3583     MD5Final(digest, &ctx);
  3579   3584     converter = (void(*)(unsigned char*,char*))cd;
  3580   3585     converter(digest, zBuf);
................................................................................
  3591   3596     MD5Context ctx;
  3592   3597     void (*converter)(unsigned char*, char*);
  3593   3598     unsigned char digest[16];
  3594   3599     char zBuf[10240];
  3595   3600   
  3596   3601     if( argc!=2 ){
  3597   3602       Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 
  3598         -        " FILENAME\"", 0);
         3603  +        " FILENAME\"", (char*)0);
  3599   3604       return TCL_ERROR;
  3600   3605     }
  3601   3606     in = fopen(argv[1],"rb");
  3602   3607     if( in==0 ){
  3603   3608       Tcl_AppendResult(interp,"unable to open file \"", argv[1], 
  3604         -         "\" for reading", 0);
         3609  +         "\" for reading", (char*)0);
  3605   3610       return TCL_ERROR;
  3606   3611     }
  3607   3612     MD5Init(&ctx);
  3608   3613     for(;;){
  3609   3614       int n;
  3610   3615       n = (int)fread(zBuf, 1, sizeof(zBuf), in);
  3611   3616       if( n<=0 ) break;

Changes to src/test5.c.

    72     72     }
    73     73   
    74     74     if( Tcl_GetIntFromObj(interp, objv[1], &repeat_count) ) return TCL_ERROR;
    75     75     if( Tcl_GetIntFromObj(interp, objv[2], &do_calls) ) return TCL_ERROR;
    76     76   
    77     77     val.flags = MEM_Str|MEM_Term|MEM_Static;
    78     78     val.z = "hello world";
    79         -  val.type = SQLITE_TEXT;
           79  +  val.memType = MEM_Str;
    80     80     val.enc = SQLITE_UTF8;
    81     81   
    82     82     for(i=0; i<repeat_count; i++){
    83     83       if( do_calls ){
    84     84         sqlite3_value_text(&val);
    85     85       }
    86     86     }

Changes to src/trigger.c.

   722    722       **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
   723    723       **   END;
   724    724       **
   725    725       **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
   726    726       **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
   727    727       */
   728    728       pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
   729         -
   730         -    /* Clear the cookieGoto flag. When coding triggers, the cookieGoto 
   731         -    ** variable is used as a flag to indicate to sqlite3ExprCodeConstants()
   732         -    ** that it is not safe to refactor constants (this happens after the
   733         -    ** start of the first loop in the SQL statement is coded - at that 
   734         -    ** point code may be conditionally executed, so it is no longer safe to 
   735         -    ** initialize constant register values).  */
   736         -    assert( pParse->cookieGoto==0 || pParse->cookieGoto==-1 );
   737         -    pParse->cookieGoto = 0;
          729  +    assert( pParse->okConstFactor==0 );
   738    730   
   739    731       switch( pStep->op ){
   740    732         case TK_UPDATE: {
   741    733           sqlite3Update(pParse, 
   742    734             targetSrcList(pParse, pStep),
   743    735             sqlite3ExprListDup(db, pStep->pExprList, 0), 
   744    736             sqlite3ExprDup(db, pStep->pWhere, 0), 

Changes to src/util.c.

  1119   1119     i64 iA = *pA;
  1120   1120     testcase( iA==0 ); testcase( iA==1 );
  1121   1121     testcase( iB==-1 ); testcase( iB==0 );
  1122   1122     if( iB>=0 ){
  1123   1123       testcase( iA>0 && LARGEST_INT64 - iA == iB );
  1124   1124       testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
  1125   1125       if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
  1126         -    *pA += iB;
  1127   1126     }else{
  1128   1127       testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
  1129   1128       testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
  1130   1129       if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
  1131         -    *pA += iB;
  1132   1130     }
         1131  +  *pA += iB;
  1133   1132     return 0; 
  1134   1133   }
  1135   1134   int sqlite3SubInt64(i64 *pA, i64 iB){
  1136   1135     testcase( iB==SMALLEST_INT64+1 );
  1137   1136     if( iB==SMALLEST_INT64 ){
  1138   1137       testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
  1139   1138       if( (*pA)>=0 ) return 1;
................................................................................
  1149   1148     i64 iA = *pA;
  1150   1149     i64 iA1, iA0, iB1, iB0, r;
  1151   1150   
  1152   1151     iA1 = iA/TWOPOWER32;
  1153   1152     iA0 = iA % TWOPOWER32;
  1154   1153     iB1 = iB/TWOPOWER32;
  1155   1154     iB0 = iB % TWOPOWER32;
  1156         -  if( iA1*iB1 != 0 ) return 1;
  1157         -  assert( iA1*iB0==0 || iA0*iB1==0 );
  1158         -  r = iA1*iB0 + iA0*iB1;
         1155  +  if( iA1==0 ){
         1156  +    if( iB1==0 ){
         1157  +      *pA *= iB;
         1158  +      return 0;
         1159  +    }
         1160  +    r = iA0*iB1;
         1161  +  }else if( iB1==0 ){
         1162  +    r = iA1*iB0;
         1163  +  }else{
         1164  +    /* If both iA1 and iB1 are non-zero, overflow will result */
         1165  +    return 1;
         1166  +  }
  1159   1167     testcase( r==(-TWOPOWER31)-1 );
  1160   1168     testcase( r==(-TWOPOWER31) );
  1161   1169     testcase( r==TWOPOWER31 );
  1162   1170     testcase( r==TWOPOWER31-1 );
  1163   1171     if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
  1164   1172     r *= TWOPOWER32;
  1165   1173     if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;

Changes to src/vdbe.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** The code in this file implements execution method of the 
    13         -** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
    14         -** handles housekeeping details such as creating and deleting
    15         -** VDBE instances.  This file is solely interested in executing
    16         -** the VDBE program.
    17         -**
    18         -** In the external interface, an "sqlite3_stmt*" is an opaque pointer
    19         -** to a VDBE.
    20         -**
    21         -** The SQL parser generates a program which is then executed by
    22         -** the VDBE to do the work of the SQL statement.  VDBE programs are 
    23         -** similar in form to assembly language.  The program consists of
    24         -** a linear sequence of operations.  Each operation has an opcode 
    25         -** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4 
    26         -** is a null-terminated string.  Operand P5 is an unsigned character.
    27         -** Few opcodes use all 5 operands.
    28         -**
    29         -** Computation results are stored on a set of registers numbered beginning
    30         -** with 1 and going up to Vdbe.nMem.  Each register can store
    31         -** either an integer, a null-terminated string, a floating point
    32         -** number, or the SQL "NULL" value.  An implicit conversion from one
    33         -** type to the other occurs as necessary.
    34         -** 
    35         -** Most of the code in this file is taken up by the sqlite3VdbeExec()
    36         -** function which does the work of interpreting a VDBE program.
    37         -** But other routines are also provided to help in building up
    38         -** a program instruction by instruction.
           12  +** The code in this file implements the function that runs the
           13  +** bytecode of a prepared statement.
    39     14   **
    40     15   ** Various scripts scan this source file in order to generate HTML
    41     16   ** documentation, headers files, or other derived files.  The formatting
    42     17   ** of the code in this file is, therefore, important.  See other comments
    43     18   ** in this file for details.  If in doubt, do not deviate from existing
    44     19   ** commenting and indentation practices when changing or adding code.
    45     20   */
    46     21   #include "sqliteInt.h"
    47     22   #include "vdbeInt.h"
    48     23   
    49     24   /*
    50     25   ** Invoke this macro on memory cells just prior to changing the
    51     26   ** value of the cell.  This macro verifies that shallow copies are
    52         -** not misused.
           27  +** not misused.  A shallow copy of a string or blob just copies a
           28  +** pointer to the string or blob, not the content.  If the original
           29  +** is changed while the copy is still in use, the string or blob might
           30  +** be changed out from under the copy.  This macro verifies that nothing
           31  +** like that every happens.
    53     32   */
    54     33   #ifdef SQLITE_DEBUG
    55     34   # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
    56     35   #else
    57     36   # define memAboutToChange(P,M)
    58     37   #endif
    59     38   
................................................................................
   114     93   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
   115     94   # define HAS_UPDATE_HOOK(DB)   ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
   116     95   #else
   117     96   # define HAS_UPDATE_HOOK(DB)  ((DB)->xUpdateCallback)
   118     97   #endif
   119     98   
   120     99   /*
   121         -** The next global variable is incremented each type the OP_Found opcode
          100  +** The next global variable is incremented each time the OP_Found opcode
   122    101   ** is executed. This is used to test whether or not the foreign key
   123    102   ** operation implemented using OP_FkIsZero is working. This variable
   124    103   ** has no function other than to help verify the correct operation of the
   125    104   ** library.
   126    105   */
   127    106   #ifdef SQLITE_TEST
   128    107   int sqlite3_found_count = 0;
................................................................................
   158    137   ** converts an MEM_Ephem string into an MEM_Dyn string.
   159    138   */
   160    139   #define Deephemeralize(P) \
   161    140      if( ((P)->flags&MEM_Ephem)!=0 \
   162    141          && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
   163    142   
   164    143   /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
   165         -# define isSorter(x) ((x)->pSorter!=0)
   166         -
   167         -/*
   168         -** Argument pMem points at a register that will be passed to a
   169         -** user-defined function or returned to the user as the result of a query.
   170         -** This routine sets the pMem->type variable used by the sqlite3_value_*() 
   171         -** routines.
   172         -*/
   173         -void sqlite3VdbeMemStoreType(Mem *pMem){
   174         -  int flags = pMem->flags;
   175         -  if( flags & MEM_Null ){
   176         -    pMem->type = SQLITE_NULL;
   177         -  }
   178         -  else if( flags & MEM_Int ){
   179         -    pMem->type = SQLITE_INTEGER;
   180         -  }
   181         -  else if( flags & MEM_Real ){
   182         -    pMem->type = SQLITE_FLOAT;
   183         -  }
   184         -  else if( flags & MEM_Str ){
   185         -    pMem->type = SQLITE_TEXT;
   186         -  }else{
   187         -    pMem->type = SQLITE_BLOB;
   188         -  }
   189         -}
          144  +#define isSorter(x) ((x)->pSorter!=0)
   190    145   
   191    146   /*
   192    147   ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
   193    148   ** if we run out of memory.
   194    149   */
   195    150   static VdbeCursor *allocateCursor(
   196    151     Vdbe *p,              /* The virtual machine */
................................................................................
   312    267   /*
   313    268   ** Try to convert the type of a function argument or a result column
   314    269   ** into a numeric representation.  Use either INTEGER or REAL whichever
   315    270   ** is appropriate.  But only do the conversion if it is possible without
   316    271   ** loss of information and return the revised type of the argument.
   317    272   */
   318    273   int sqlite3_value_numeric_type(sqlite3_value *pVal){
   319         -  Mem *pMem = (Mem*)pVal;
   320         -  if( pMem->type==SQLITE_TEXT ){
          274  +  int eType = sqlite3_value_type(pVal);
          275  +  if( eType==SQLITE_TEXT ){
          276  +    Mem *pMem = (Mem*)pVal;
   321    277       applyNumericAffinity(pMem);
   322    278       sqlite3VdbeMemStoreType(pMem);
          279  +    eType = sqlite3_value_type(pVal);
   323    280     }
   324         -  return pMem->type;
          281  +  return eType;
   325    282   }
   326    283   
   327    284   /*
   328    285   ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
   329    286   ** not the internal Mem* type.
   330    287   */
   331    288   void sqlite3ValueApplyAffinity(
................................................................................
   420    377   #endif
   421    378   
   422    379   #ifdef SQLITE_DEBUG
   423    380   /*
   424    381   ** Print the value of a register for tracing purposes:
   425    382   */
   426    383   static void memTracePrint(Mem *p){
   427         -  if( p->flags & MEM_Invalid ){
          384  +  if( p->flags & MEM_Undefined ){
   428    385       printf(" undefined");
   429    386     }else if( p->flags & MEM_Null ){
   430    387       printf(" NULL");
   431    388     }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
   432    389       printf(" si:%lld", p->u.i);
   433    390     }else if( p->flags & MEM_Int ){
   434    391       printf(" i:%lld", p->u.i);
................................................................................
   463    420   /* 
   464    421   ** hwtime.h contains inline assembler code for implementing 
   465    422   ** high-performance timing routines.
   466    423   */
   467    424   #include "hwtime.h"
   468    425   
   469    426   #endif
   470         -
   471         -/*
   472         -** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
   473         -** sqlite3_interrupt() routine has been called.  If it has been, then
   474         -** processing of the VDBE program is interrupted.
   475         -**
   476         -** This macro added to every instruction that does a jump in order to
   477         -** implement a loop.  This test used to be on every single instruction,
   478         -** but that meant we more testing than we needed.  By only testing the
   479         -** flag on jump instructions, we get a (small) speed improvement.
   480         -*/
   481         -#define CHECK_FOR_INTERRUPT \
   482         -   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
   483         -
   484    427   
   485    428   #ifndef NDEBUG
   486    429   /*
   487    430   ** This function is only called from within an assert() expression. It
   488    431   ** checks that the sqlite3.nTransaction variable is correctly set to
   489    432   ** the number of non-transaction savepoints currently in the 
   490    433   ** linked list starting at sqlite3.pSavepoint.
................................................................................
   500    443     assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
   501    444     return 1;
   502    445   }
   503    446   #endif
   504    447   
   505    448   
   506    449   /*
   507         -** Execute as much of a VDBE program as we can then return.
   508         -**
   509         -** sqlite3VdbeMakeReady() must be called before this routine in order to
   510         -** close the program with a final OP_Halt and to set up the callbacks
   511         -** and the error message pointer.
   512         -**
   513         -** Whenever a row or result data is available, this routine will either
   514         -** invoke the result callback (if there is one) or return with
   515         -** SQLITE_ROW.
   516         -**
   517         -** If an attempt is made to open a locked database, then this routine
   518         -** will either invoke the busy callback (if there is one) or it will
   519         -** return SQLITE_BUSY.
   520         -**
   521         -** If an error occurs, an error message is written to memory obtained
   522         -** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
   523         -** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
   524         -**
   525         -** If the callback ever returns non-zero, then the program exits
   526         -** immediately.  There will be no error message but the p->rc field is
   527         -** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
   528         -**
   529         -** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
   530         -** routine to return SQLITE_ERROR.
   531         -**
   532         -** Other fatal errors return SQLITE_ERROR.
   533         -**
   534         -** After this routine has finished, sqlite3VdbeFinalize() should be
   535         -** used to clean up the mess that was left behind.
          450  +** Execute as much of a VDBE program as we can.
          451  +** This is the core of sqlite3_step().  
   536    452   */
   537    453   int sqlite3VdbeExec(
   538    454     Vdbe *p                    /* The VDBE */
   539    455   ){
   540    456     int pc=0;                  /* The program counter */
   541    457     Op *aOp = p->aOp;          /* Copy of p->aOp */
   542    458     Op *pOp;                   /* Current operation */
................................................................................
   572    488     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
   573    489     assert( p->bIsReader || p->readOnly!=0 );
   574    490     p->rc = SQLITE_OK;
   575    491     p->iCurrentTime = 0;
   576    492     assert( p->explain==0 );
   577    493     p->pResultSet = 0;
   578    494     db->busyHandler.nBusy = 0;
   579         -  CHECK_FOR_INTERRUPT;
          495  +  if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
   580    496     sqlite3VdbeIOTraceSql(p);
   581    497   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   582    498     if( db->xProgress ){
   583    499       assert( 0 < db->nProgressOps );
   584    500       nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
   585    501       if( nProgressLimit==0 ){
   586    502         nProgressLimit = db->nProgressOps;
................................................................................
   748    664     **
   749    665     ** This code uses unstructured "goto" statements and does not look clean.
   750    666     ** But that is not due to sloppy coding habits. The code is written this
   751    667     ** way for performance, to avoid having to run the interrupt and progress
   752    668     ** checks on every opcode.  This helps sqlite3_step() to run about 1.5%
   753    669     ** faster according to "valgrind --tool=cachegrind" */
   754    670   check_for_interrupt:
   755         -  CHECK_FOR_INTERRUPT;
          671  +  if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
   756    672   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   757    673     /* Call the progress callback if it is configured and the required number
   758    674     ** of VDBE ops have been executed (either since this invocation of
   759    675     ** sqlite3VdbeExec() or since last time the progress callback was called).
   760    676     ** If the progress callback returns non-zero, exit the virtual machine with
   761    677     ** a return code SQLITE_ABORT.
   762    678     */
................................................................................
   788    704     REGISTER_TRACE(pOp->p1, pIn1);
   789    705     pc = pOp->p2 - 1;
   790    706     break;
   791    707   }
   792    708   
   793    709   /* Opcode:  Return P1 * * * *
   794    710   **
   795         -** Jump to the next instruction after the address in register P1.
          711  +** Jump to the next instruction after the address in register P1.  After
          712  +** the jump, register P1 becomes undefined.
   796    713   */
   797    714   case OP_Return: {           /* in1 */
   798    715     pIn1 = &aMem[pOp->p1];
   799         -  assert( pIn1->flags & MEM_Int );
          716  +  assert( pIn1->flags==MEM_Int );
   800    717     pc = (int)pIn1->u.i;
   801         -  break;
   802         -}
   803         -
   804         -/* Opcode:  Yield P1 * * * *
   805         -**
   806         -** Swap the program counter with the value in register P1.
   807         -*/
   808         -case OP_Yield: {            /* in1 */
          718  +  pIn1->flags = MEM_Undefined;
          719  +  break;
          720  +}
          721  +
          722  +/* Opcode: InitCoroutine P1 P2 P3 * *
          723  +**
          724  +** Set up register P1 so that it will OP_Yield to the co-routine
          725  +** located at address P3.
          726  +**
          727  +** If P2!=0 then the co-routine implementation immediately follows
          728  +** this opcode.  So jump over the co-routine implementation to
          729  +** address P2.
          730  +*/
          731  +case OP_InitCoroutine: {     /* jump */
          732  +  assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
          733  +  assert( pOp->p2>=0 && pOp->p2<p->nOp );
          734  +  assert( pOp->p3>=0 && pOp->p3<p->nOp );
          735  +  pOut = &aMem[pOp->p1];
          736  +  assert( !VdbeMemDynamic(pOut) );
          737  +  pOut->u.i = pOp->p3 - 1;
          738  +  pOut->flags = MEM_Int;
          739  +  if( pOp->p2 ) pc = pOp->p2 - 1;
          740  +  break;
          741  +}
          742  +
          743  +/* Opcode:  EndCoroutine P1 * * * *
          744  +**
          745  +** The instruction at the address in register P1 is an OP_Yield.
          746  +** Jump to the P2 parameter of that OP_Yield.
          747  +** After the jump, register P1 becomes undefined.
          748  +*/
          749  +case OP_EndCoroutine: {           /* in1 */
          750  +  VdbeOp *pCaller;
          751  +  pIn1 = &aMem[pOp->p1];
          752  +  assert( pIn1->flags==MEM_Int );
          753  +  assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
          754  +  pCaller = &aOp[pIn1->u.i];
          755  +  assert( pCaller->opcode==OP_Yield );
          756  +  assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
          757  +  pc = pCaller->p2 - 1;
          758  +  pIn1->flags = MEM_Undefined;
          759  +  break;
          760  +}
          761  +
          762  +/* Opcode:  Yield P1 P2 * * *
          763  +**
          764  +** Swap the program counter with the value in register P1.
          765  +**
          766  +** If the co-routine ends with OP_Yield or OP_Return then continue
          767  +** to the next instruction.  But if the co-routine ends with
          768  +** OP_EndCoroutine, jump immediately to P2.
          769  +*/
          770  +case OP_Yield: {            /* in1, jump */
   809    771     int pcDest;
   810    772     pIn1 = &aMem[pOp->p1];
   811    773     assert( (pIn1->flags & MEM_Dyn)==0 );
   812    774     pIn1->flags = MEM_Int;
   813    775     pcDest = (int)pIn1->u.i;
   814    776     pIn1->u.i = pc;
   815    777     REGISTER_TRACE(pOp->p1, pIn1);
   816    778     pc = pcDest;
   817    779     break;
   818    780   }
   819    781   
   820    782   /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
   821         -** Synopsis:  if r[P3] null then halt
          783  +** Synopsis:  if r[P3]=null halt
   822    784   **
   823    785   ** Check the value in register P3.  If it is NULL then Halt using
   824    786   ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
   825    787   ** value in register P3 is not NULL, then this routine is a no-op.
   826    788   ** The P5 parameter should be 1.
   827    789   */
   828    790   case OP_HaltIfNull: {      /* in3 */
................................................................................
   962    924   }
   963    925   #endif
   964    926   
   965    927   /* Opcode: String8 * P2 * P4 *
   966    928   ** Synopsis: r[P2]='P4'
   967    929   **
   968    930   ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
   969         -** into an OP_String before it is executed for the first time.
          931  +** into an OP_String before it is executed for the first time.  During
          932  +** this transformation, the length of string P4 is computed and stored
          933  +** as the P1 parameter.
   970    934   */
   971    935   case OP_String8: {         /* same as TK_STRING, out2-prerelease */
   972    936     assert( pOp->p4.z!=0 );
   973    937     pOp->opcode = OP_String;
   974    938     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
   975    939   
   976    940   #ifndef SQLITE_OMIT_UTF16
................................................................................
  1037   1001       pOut->flags = nullFlag;
  1038   1002       cnt--;
  1039   1003     }
  1040   1004     break;
  1041   1005   }
  1042   1006   
  1043   1007   
  1044         -/* Opcode: Blob P1 P2 * P4
         1008  +/* Opcode: Blob P1 P2 * P4 *
  1045   1009   ** Synopsis: r[P2]=P4 (len=P1)
  1046   1010   **
  1047   1011   ** P4 points to a blob of data P1 bytes long.  Store this
  1048   1012   ** blob in register P2.
  1049   1013   */
  1050   1014   case OP_Blob: {                /* out2-prerelease */
  1051   1015     assert( pOp->p1 <= SQLITE_MAX_LENGTH );
................................................................................
  1056   1020   }
  1057   1021   
  1058   1022   /* Opcode: Variable P1 P2 * P4 *
  1059   1023   ** Synopsis: r[P2]=parameter(P1,P4)
  1060   1024   **
  1061   1025   ** Transfer the values of bound parameter P1 into register P2
  1062   1026   **
  1063         -** If the parameter is named, then its name appears in P4 and P3==1.
         1027  +** If the parameter is named, then its name appears in P4.
  1064   1028   ** The P4 value is used by sqlite3_bind_parameter_name().
  1065   1029   */
  1066   1030   case OP_Variable: {            /* out2-prerelease */
  1067   1031     Mem *pVar;       /* Value being transferred */
  1068   1032   
  1069   1033     assert( pOp->p1>0 && pOp->p1<=p->nVar );
  1070   1034     assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
................................................................................
  1175   1139   
  1176   1140   /* Opcode: ResultRow P1 P2 * * *
  1177   1141   ** Synopsis:  output=r[P1@P2]
  1178   1142   **
  1179   1143   ** The registers P1 through P1+P2-1 contain a single row of
  1180   1144   ** results. This opcode causes the sqlite3_step() call to terminate
  1181   1145   ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
  1182         -** structure to provide access to the top P1 values as the result
  1183         -** row.
         1146  +** structure to provide access to the r[P1]..r[P1+P2-1] values as
         1147  +** the result row.
  1184   1148   */
  1185   1149   case OP_ResultRow: {
  1186   1150     Mem *pMem;
  1187   1151     int i;
  1188   1152     assert( p->nResColumn==pOp->p2 );
  1189   1153     assert( pOp->p1>0 );
  1190   1154     assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
................................................................................
  1704   1668   #endif
  1705   1669   
  1706   1670   #ifndef SQLITE_OMIT_CAST
  1707   1671   /* Opcode: ToText P1 * * * *
  1708   1672   **
  1709   1673   ** Force the value in register P1 to be text.
  1710   1674   ** If the value is numeric, convert it to a string using the
  1711         -** equivalent of printf().  Blob values are unchanged and
         1675  +** equivalent of sprintf().  Blob values are unchanged and
  1712   1676   ** are afterwards simply interpreted as text.
  1713   1677   **
  1714   1678   ** A NULL value is not changed by this routine.  It remains NULL.
  1715   1679   */
  1716   1680   case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
  1717   1681     pIn1 = &aMem[pOp->p1];
  1718   1682     memAboutToChange(p, pIn1);
................................................................................
  2159   2123     }
  2160   2124     break;
  2161   2125   }
  2162   2126   
  2163   2127   /* Opcode: Once P1 P2 * * *
  2164   2128   **
  2165   2129   ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
  2166         -** set the flag and fall through to the next instruction.
         2130  +** set the flag and fall through to the next instruction.  In other words,
         2131  +** this opcode causes all following up codes up through P2 (but not including
         2132  +** P2) to run just once and skipped on subsequent times through the loop.
  2167   2133   */
  2168   2134   case OP_Once: {             /* jump */
  2169   2135     assert( pOp->p1<p->nOnceFlag );
  2170   2136     if( p->aOnceFlag[pOp->p1] ){
  2171   2137       pc = pOp->p2-1;
  2172   2138     }else{
  2173   2139       p->aOnceFlag[pOp->p1] = 1;
................................................................................
  2299   2265     rc = sqlite3VdbeCursorMoveto(pC);
  2300   2266     if( rc ) goto abort_due_to_error;
  2301   2267     if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
  2302   2268       if( pC->nullRow ){
  2303   2269         if( pCrsr==0 ){
  2304   2270           assert( pC->pseudoTableReg>0 );
  2305   2271           pReg = &aMem[pC->pseudoTableReg];
  2306         -        if( pC->multiPseudo ){
  2307         -          sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
  2308         -          Deephemeralize(pDest);
  2309         -          goto op_column_out;
  2310         -        }
  2311   2272           assert( pReg->flags & MEM_Blob );
  2312   2273           assert( memIsValid(pReg) );
  2313   2274           pC->payloadSize = pC->szRow = avail = pReg->n;
  2314   2275           pC->aRow = (u8*)pReg->z;
  2315   2276         }else{
  2316   2277           MemSetTypeFlag(pDest, MEM_Null);
  2317   2278           goto op_column_out;
................................................................................
  2952   2913                      "cannot commit - no transaction is active"));
  2953   2914            
  2954   2915       rc = SQLITE_ERROR;
  2955   2916     }
  2956   2917     break;
  2957   2918   }
  2958   2919   
  2959         -/* Opcode: Transaction P1 P2 * * *
         2920  +/* Opcode: Transaction P1 P2 P3 P4 P5
  2960   2921   **
  2961   2922   ** Begin a transaction.  The transaction ends when a Commit or Rollback
  2962   2923   ** opcode is encountered.  Depending on the ON CONFLICT setting, the
  2963   2924   ** transaction might also be rolled back if an error is encountered.
  2964   2925   **
  2965   2926   ** P1 is the index of the database file on which the transaction is
  2966   2927   ** started.  Index 0 is the main database file and index 1 is the
................................................................................
  2982   2943   ** connection is currently not in autocommit mode, or if there are other
  2983   2944   ** active statements. A statement transaction allows the changes made by this
  2984   2945   ** VDBE to be rolled back after an error without having to roll back the
  2985   2946   ** entire transaction. If no error is encountered, the statement transaction
  2986   2947   ** will automatically commit when the VDBE halts.
  2987   2948   **
  2988   2949   ** If P2 is zero, then a read-lock is obtained on the database file.
         2950  +**
         2951  +** If P5!=0 then this opcode also checks the schema cookie against P3
         2952  +** and the schema generation counter against P4.
         2953  +** The cookie changes its value whenever the database schema changes.
         2954  +** This operation is used to detect when that the cookie has changed
         2955  +** and that the current process needs to reread the schema.
  2989   2956   */
  2990   2957   case OP_Transaction: {
  2991   2958     Btree *pBt;
         2959  +  int iMeta;
         2960  +  int iGen;
  2992   2961   
  2993   2962     assert( p->bIsReader );
  2994   2963     assert( p->readOnly==0 || pOp->p2==0 );
  2995   2964     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  2996   2965     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  2997   2966     if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
  2998   2967       rc = SQLITE_READONLY;
................................................................................
  3028   2997   
  3029   2998         /* Store the current value of the database handles deferred constraint
  3030   2999         ** counter. If the statement transaction needs to be rolled back,
  3031   3000         ** the value of this counter needs to be restored too.  */
  3032   3001         p->nStmtDefCons = db->nDeferredCons;
  3033   3002         p->nStmtDefImmCons = db->nDeferredImmCons;
  3034   3003       }
         3004  +
         3005  +    /* Gather the schema version number for checking */
         3006  +    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
         3007  +    iGen = db->aDb[pOp->p1].pSchema->iGeneration;
         3008  +  }else{
         3009  +    iGen = iMeta = 0;
         3010  +  }
         3011  +  assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
         3012  +  if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
         3013  +    sqlite3DbFree(db, p->zErrMsg);
         3014  +    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
         3015  +    /* If the schema-cookie from the database file matches the cookie 
         3016  +    ** stored with the in-memory representation of the schema, do
         3017  +    ** not reload the schema from the database file.
         3018  +    **
         3019  +    ** If virtual-tables are in use, this is not just an optimization.
         3020  +    ** Often, v-tables store their data in other SQLite tables, which
         3021  +    ** are queried from within xNext() and other v-table methods using
         3022  +    ** prepared queries. If such a query is out-of-date, we do not want to
         3023  +    ** discard the database schema, as the user code implementing the
         3024  +    ** v-table would have to be ready for the sqlite3_vtab structure itself
         3025  +    ** to be invalidated whenever sqlite3_step() is called from within 
         3026  +    ** a v-table method.
         3027  +    */
         3028  +    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
         3029  +      sqlite3ResetOneSchema(db, pOp->p1);
         3030  +    }
         3031  +    p->expired = 1;
         3032  +    rc = SQLITE_SCHEMA;
  3035   3033     }
  3036   3034     break;
  3037   3035   }
  3038   3036   
  3039   3037   /* Opcode: ReadCookie P1 P2 P3 * *
  3040   3038   **
  3041   3039   ** Read cookie number P3 from database P1 and write it into register P2.
................................................................................
  3099   3097     }
  3100   3098     if( pOp->p1==1 ){
  3101   3099       /* Invalidate all prepared statements whenever the TEMP database
  3102   3100       ** schema is changed.  Ticket #1644 */
  3103   3101       sqlite3ExpirePreparedStatements(db);
  3104   3102       p->expired = 0;
  3105   3103     }
  3106         -  break;
  3107         -}
  3108         -
  3109         -/* Opcode: VerifyCookie P1 P2 P3 * *
  3110         -**
  3111         -** Check the value of global database parameter number 0 (the
  3112         -** schema version) and make sure it is equal to P2 and that the
  3113         -** generation counter on the local schema parse equals P3.
  3114         -**
  3115         -** P1 is the database number which is 0 for the main database file
  3116         -** and 1 for the file holding temporary tables and some higher number
  3117         -** for auxiliary databases.
  3118         -**
  3119         -** The cookie changes its value whenever the database schema changes.
  3120         -** This operation is used to detect when that the cookie has changed
  3121         -** and that the current process needs to reread the schema.
  3122         -**
  3123         -** Either a transaction needs to have been started or an OP_Open needs
  3124         -** to be executed (to establish a read lock) before this opcode is
  3125         -** invoked.
  3126         -*/
  3127         -case OP_VerifyCookie: {
  3128         -  int iMeta;
  3129         -  int iGen;
  3130         -  Btree *pBt;
  3131         -
  3132         -  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  3133         -  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  3134         -  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
  3135         -  assert( p->bIsReader );
  3136         -  pBt = db->aDb[pOp->p1].pBt;
  3137         -  if( pBt ){
  3138         -    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
  3139         -    iGen = db->aDb[pOp->p1].pSchema->iGeneration;
  3140         -  }else{
  3141         -    iGen = iMeta = 0;
  3142         -  }
  3143         -  if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
  3144         -    sqlite3DbFree(db, p->zErrMsg);
  3145         -    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
  3146         -    /* If the schema-cookie from the database file matches the cookie 
  3147         -    ** stored with the in-memory representation of the schema, do
  3148         -    ** not reload the schema from the database file.
  3149         -    **
  3150         -    ** If virtual-tables are in use, this is not just an optimization.
  3151         -    ** Often, v-tables store their data in other SQLite tables, which
  3152         -    ** are queried from within xNext() and other v-table methods using
  3153         -    ** prepared queries. If such a query is out-of-date, we do not want to
  3154         -    ** discard the database schema, as the user code implementing the
  3155         -    ** v-table would have to be ready for the sqlite3_vtab structure itself
  3156         -    ** to be invalidated whenever sqlite3_step() is called from within 
  3157         -    ** a v-table method.
  3158         -    */
  3159         -    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
  3160         -      sqlite3ResetOneSchema(db, pOp->p1);
  3161         -    }
  3162         -
  3163         -    p->expired = 1;
  3164         -    rc = SQLITE_SCHEMA;
  3165         -  }
  3166   3104     break;
  3167   3105   }
  3168   3106   
  3169   3107   /* Opcode: OpenRead P1 P2 P3 P4 P5
  3170   3108   ** Synopsis: root=P2 iDb=P3
  3171   3109   **
  3172   3110   ** Open a read-only cursor for the database table whose root page is
................................................................................
  3375   3313         pCx->isTable = 1;
  3376   3314       }
  3377   3315     }
  3378   3316     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3379   3317     break;
  3380   3318   }
  3381   3319   
  3382         -/* Opcode: SorterOpen P1 * * P4 *
         3320  +/* Opcode: SorterOpen P1 P2 * P4 *
  3383   3321   **
  3384   3322   ** This opcode works like OP_OpenEphemeral except that it opens
  3385   3323   ** a transient index that is specifically designed to sort large
  3386   3324   ** tables using an external merge-sort algorithm.
  3387   3325   */
  3388   3326   case OP_SorterOpen: {
  3389   3327     VdbeCursor *pCx;
................................................................................
  3395   3333     pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3396   3334     assert( pCx->pKeyInfo->db==db );
  3397   3335     assert( pCx->pKeyInfo->enc==ENC(db) );
  3398   3336     rc = sqlite3VdbeSorterInit(db, pCx);
  3399   3337     break;
  3400   3338   }
  3401   3339   
  3402         -/* Opcode: OpenPseudo P1 P2 P3 * P5
  3403         -** Synopsis: content in r[P2@P3]
         3340  +/* Opcode: OpenPseudo P1 P2 P3 * *
         3341  +** Synopsis: P3 columns in r[P2]
  3404   3342   **
  3405   3343   ** Open a new cursor that points to a fake table that contains a single
  3406         -** row of data.  The content of that one row in the content of memory
  3407         -** register P2 when P5==0.  In other words, cursor P1 becomes an alias for the 
  3408         -** MEM_Blob content contained in register P2.  When P5==1, then the
  3409         -** row is represented by P3 consecutive registers beginning with P2.
         3344  +** row of data.  The content of that one row is the content of memory
         3345  +** register P2.  In other words, cursor P1 becomes an alias for the 
         3346  +** MEM_Blob content contained in register P2.
  3410   3347   **
  3411   3348   ** A pseudo-table created by this opcode is used to hold a single
  3412   3349   ** row output from the sorter so that the row can be decomposed into
  3413   3350   ** individual columns using the OP_Column opcode.  The OP_Column opcode
  3414   3351   ** is the only cursor opcode that works with a pseudo-table.
  3415   3352   **
  3416   3353   ** P3 is the number of fields in the records that will be stored by
................................................................................
  3422   3359     assert( pOp->p1>=0 );
  3423   3360     assert( pOp->p3>=0 );
  3424   3361     pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
  3425   3362     if( pCx==0 ) goto no_mem;
  3426   3363     pCx->nullRow = 1;
  3427   3364     pCx->pseudoTableReg = pOp->p2;
  3428   3365     pCx->isTable = 1;
  3429         -  pCx->multiPseudo = pOp->p5;
         3366  +  assert( pOp->p5==0 );
  3430   3367     break;
  3431   3368   }
  3432   3369   
  3433   3370   /* Opcode: Close P1 * * * *
  3434   3371   **
  3435   3372   ** Close a cursor previously opened as P1.  If P1 is not
  3436   3373   ** currently open, this instruction is a no-op.
................................................................................
  3605   3542     pC->deferredMoveto = 0;
  3606   3543     pC->cacheStatus = CACHE_STALE;
  3607   3544   #ifdef SQLITE_TEST
  3608   3545     sqlite3_search_count++;
  3609   3546   #endif
  3610   3547     if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
  3611   3548       if( res<0 || (res==0 && oc==OP_SeekGt) ){
         3549  +      res = 0;
  3612   3550         rc = sqlite3BtreeNext(pC->pCursor, &res);
  3613   3551         if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3614   3552         pC->rowidIsValid = 0;
  3615   3553       }else{
  3616   3554         res = 0;
  3617   3555       }
  3618   3556     }else{
  3619   3557       assert( oc==OP_SeekLt || oc==OP_SeekLe );
  3620   3558       if( res>0 || (res==0 && oc==OP_SeekLt) ){
         3559  +      res = 0;
  3621   3560         rc = sqlite3BtreePrevious(pC->pCursor, &res);
  3622   3561         if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3623   3562         pC->rowidIsValid = 0;
  3624   3563       }else{
  3625   3564         /* res might be negative because the table is empty.  Check to
  3626   3565         ** see if this is the case.
  3627   3566         */
................................................................................
  3733   3672     assert( pC->pCursor!=0 );
  3734   3673     assert( pC->isTable==0 );
  3735   3674     pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
  3736   3675     if( pOp->p4.i>0 ){
  3737   3676       r.pKeyInfo = pC->pKeyInfo;
  3738   3677       r.nField = (u16)pOp->p4.i;
  3739   3678       r.aMem = pIn3;
         3679  +    for(ii=0; ii<r.nField; ii++){
         3680  +      assert( memIsValid(&r.aMem[ii]) );
         3681  +      ExpandBlob(&r.aMem[ii]);
  3740   3682   #ifdef SQLITE_DEBUG
  3741         -    {
  3742         -      int i;
  3743         -      for(i=0; i<r.nField; i++){
  3744         -        assert( memIsValid(&r.aMem[i]) );
  3745         -        if( i ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
  3746         -      }
         3683  +      if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
         3684  +#endif
  3747   3685       }
  3748         -#endif
  3749   3686       r.flags = UNPACKED_PREFIX_MATCH;
  3750   3687       pIdxKey = &r;
  3751   3688     }else{
  3752   3689       pIdxKey = sqlite3VdbeAllocUnpackedRecord(
  3753   3690           pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
  3754   3691       ); 
  3755   3692       if( pIdxKey==0 ) goto no_mem;
................................................................................
  4291   4228   ** of a real table, not a pseudo-table.
  4292   4229   */
  4293   4230   /* Opcode: RowKey P1 P2 * * *
  4294   4231   ** Synopsis: r[P2]=key
  4295   4232   **
  4296   4233   ** Write into register P2 the complete row key for cursor P1.
  4297   4234   ** There is no interpretation of the data.  
  4298         -** The key is copied onto the P3 register exactly as 
         4235  +** The key is copied onto the P2 register exactly as 
  4299   4236   ** it is found in the database file.
  4300   4237   **
  4301   4238   ** If the P1 cursor must be pointing to a valid row (not a NULL row)
  4302   4239   ** of a real table, not a pseudo-table.
  4303   4240   */
  4304   4241   case OP_RowKey:
  4305   4242   case OP_RowData: {
................................................................................
  4517   4454     assert( pOp->p2>0 && pOp->p2<p->nOp );
  4518   4455     if( res ){
  4519   4456       pc = pOp->p2 - 1;
  4520   4457     }
  4521   4458     break;
  4522   4459   }
  4523   4460   
  4524         -/* Opcode: Next P1 P2 * * P5
         4461  +/* Opcode: Next P1 P2 P3 P4 P5
  4525   4462   **
  4526   4463   ** Advance cursor P1 so that it points to the next key/data pair in its
  4527   4464   ** table or index.  If there are no more key/value pairs then fall through
  4528   4465   ** to the following instruction.  But if the cursor advance was successful,
  4529   4466   ** jump immediately to P2.
  4530   4467   **
  4531   4468   ** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
  4532   4469   ** been opened prior to this opcode or the program will segfault.
         4470  +**
         4471  +** The P3 value is a hint to the btree implementation. If P3==1, that
         4472  +** means P1 is an SQL index and that this instruction could have been
         4473  +** omitted if that index had been unique.  P3 is usually 0.  P3 is
         4474  +** always either 0 or 1.
  4533   4475   **
  4534   4476   ** P4 is always of type P4_ADVANCE. The function pointer points to
  4535   4477   ** sqlite3BtreeNext().
  4536   4478   **
  4537   4479   ** If P5 is positive and the jump is taken, then event counter
  4538   4480   ** number P5-1 in the prepared statement is incremented.
  4539   4481   **
  4540   4482   ** See also: Prev, NextIfOpen
  4541   4483   */
  4542         -/* Opcode: NextIfOpen P1 P2 * * P5
         4484  +/* Opcode: NextIfOpen P1 P2 P3 P4 P5
  4543   4485   **
  4544   4486   ** This opcode works just like OP_Next except that if cursor P1 is not
  4545   4487   ** open it behaves a no-op.
  4546   4488   */
  4547         -/* Opcode: Prev P1 P2 * * P5
         4489  +/* Opcode: Prev P1 P2 P3 P4 P5
  4548   4490   **
  4549   4491   ** Back up cursor P1 so that it points to the previous key/data pair in its
  4550   4492   ** table or index.  If there is no previous key/value pairs then fall through
  4551   4493   ** to the following instruction.  But if the cursor backup was successful,
  4552   4494   ** jump immediately to P2.
  4553   4495   **
  4554   4496   ** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
  4555   4497   ** not open then the behavior is undefined.
         4498  +**
         4499  +** The P3 value is a hint to the btree implementation. If P3==1, that
         4500  +** means P1 is an SQL index and that this instruction could have been
         4501  +** omitted if that index had been unique.  P3 is usually 0.  P3 is
         4502  +** always either 0 or 1.
  4556   4503   **
  4557   4504   ** P4 is always of type P4_ADVANCE. The function pointer points to
  4558   4505   ** sqlite3BtreePrevious().
  4559   4506   **
  4560   4507   ** If P5 is positive and the jump is taken, then event counter
  4561   4508   ** number P5-1 in the prepared statement is incremented.
  4562   4509   */
  4563         -/* Opcode: PrevIfOpen P1 P2 * * P5
         4510  +/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
  4564   4511   **
  4565   4512   ** This opcode works just like OP_Prev except that if cursor P1 is not
  4566   4513   ** open it behaves a no-op.
  4567   4514   */
  4568   4515   case OP_SorterNext: {  /* jump */
  4569   4516     VdbeCursor *pC;
  4570   4517     int res;
................................................................................
  4578   4525     if( p->apCsr[pOp->p1]==0 ) break;
  4579   4526     /* Fall through */
  4580   4527   case OP_Prev:          /* jump */
  4581   4528   case OP_Next:          /* jump */
  4582   4529     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4583   4530     assert( pOp->p5<ArraySize(p->aCounter) );
  4584   4531     pC = p->apCsr[pOp->p1];
         4532  +  res = pOp->p3;
  4585   4533     assert( pC!=0 );
  4586   4534     assert( pC->deferredMoveto==0 );
  4587   4535     assert( pC->pCursor );
         4536  +  assert( res==0 || (res==1 && pC->isTable==0) );
         4537  +  testcase( res==1 );
  4588   4538     assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
  4589   4539     assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
  4590   4540     assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
  4591   4541     assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
  4592   4542     rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4593   4543   next_tail:
  4594   4544     pC->cacheStatus = CACHE_STALE;
................................................................................
  4611   4561   **
  4612   4562   ** Register P2 holds an SQL index key made using the
  4613   4563   ** MakeRecord instructions.  This opcode writes that key
  4614   4564   ** into the index P1.  Data for the entry is nil.
  4615   4565   **
  4616   4566   ** P3 is a flag that provides a hint to the b-tree layer that this
  4617   4567   ** insert is likely to be an append.
         4568  +**
         4569  +** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
         4570  +** incremented by this instruction.  If the OPFLAG_NCHANGE bit is clear,
         4571  +** then the change counter is unchanged.
         4572  +**
         4573  +** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
         4574  +** just done a seek to the spot where the new entry is to be inserted.
         4575  +** This flag avoids doing an extra seek.
  4618   4576   **
  4619   4577   ** This instruction only works for indices.  The equivalent instruction
  4620   4578   ** for tables is OP_Insert.
  4621   4579   */
  4622   4580   case OP_SorterInsert:       /* in2 */
  4623   4581   case OP_IdxInsert: {        /* in2 */
  4624   4582     VdbeCursor *pC;
................................................................................
  5212   5170     }
  5213   5171     break;
  5214   5172   }
  5215   5173   
  5216   5174   
  5217   5175   #ifndef SQLITE_OMIT_TRIGGER
  5218   5176   
  5219         -/* Opcode: Program P1 P2 P3 P4 *
         5177  +/* Opcode: Program P1 P2 P3 P4 P5
  5220   5178   **
  5221   5179   ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 
  5222   5180   **
  5223   5181   ** P1 contains the address of the memory cell that contains the first memory 
  5224   5182   ** cell in an array of values used as arguments to the sub-program. P2 
  5225   5183   ** contains the address to jump to if the sub-program throws an IGNORE 
  5226   5184   ** exception using the RAISE() function. Register P3 contains the address 
  5227   5185   ** of a memory cell in this (the parent) VM that is used to allocate the 
  5228   5186   ** memory required by the sub-vdbe at runtime.
  5229   5187   **
  5230   5188   ** P4 is a pointer to the VM containing the trigger program.
         5189  +**
         5190  +** If P5 is non-zero, then recursive program invocation is enabled.
  5231   5191   */
  5232   5192   case OP_Program: {        /* jump */
  5233   5193     int nMem;               /* Number of memory registers for sub-program */
  5234   5194     int nByte;              /* Bytes of runtime space required for sub-program */
  5235   5195     Mem *pRt;               /* Register to allocate runtime space */
  5236   5196     Mem *pMem;              /* Used to iterate through memory cells */
  5237   5197     Mem *pEnd;              /* Last memory cell in new array */
................................................................................
  5301   5261       pFrame->nOp = p->nOp;
  5302   5262       pFrame->token = pProgram->token;
  5303   5263       pFrame->aOnceFlag = p->aOnceFlag;
  5304   5264       pFrame->nOnceFlag = p->nOnceFlag;
  5305   5265   
  5306   5266       pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
  5307   5267       for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
  5308         -      pMem->flags = MEM_Invalid;
         5268  +      pMem->flags = MEM_Undefined;
  5309   5269         pMem->db = db;
  5310   5270       }
  5311   5271     }else{
  5312   5272       pFrame = pRt->u.pFrame;
  5313   5273       assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
  5314   5274       assert( pProgram->nCsr==pFrame->nChildCsr );
  5315   5275       assert( pc==pFrame->pc );
................................................................................
  5611   5571       sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
  5612   5572     }    
  5613   5573     break;
  5614   5574   };  
  5615   5575   #endif
  5616   5576   
  5617   5577   #ifndef SQLITE_OMIT_PRAGMA
  5618         -/* Opcode: JournalMode P1 P2 P3 * P5
         5578  +/* Opcode: JournalMode P1 P2 P3 * *
  5619   5579   **
  5620   5580   ** Change the journal mode of database P1 to P3. P3 must be one of the
  5621   5581   ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
  5622   5582   ** modes (delete, truncate, persist, off and memory), this is a simple
  5623   5583   ** operation. No IO is required.
  5624   5584   **
  5625   5585   ** If changing into or out of WAL mode the procedure is more complicated.
................................................................................
  6097   6057       p->expired = 0;
  6098   6058     }
  6099   6059     break;
  6100   6060   }
  6101   6061   #endif
  6102   6062   
  6103   6063   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6104         -/* Opcode: VUpdate P1 P2 P3 P4 *
         6064  +/* Opcode: VUpdate P1 P2 P3 P4 P5
  6105   6065   ** Synopsis: data=r[P3@P2]
  6106   6066   **
  6107   6067   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
  6108   6068   ** This opcode invokes the corresponding xUpdate method. P2 values
  6109   6069   ** are contiguous memory cells starting at P3 to pass to the xUpdate 
  6110   6070   ** invocation. The value in register (P3+P2-1) corresponds to the 
  6111   6071   ** p2th element of the argv array passed to xUpdate.
................................................................................
  6120   6080   **
  6121   6081   ** If P2==1 then no insert is performed.  argv[0] is the rowid of
  6122   6082   ** a row to delete.
  6123   6083   **
  6124   6084   ** P1 is a boolean flag. If it is set to true and the xUpdate call
  6125   6085   ** is successful, then the value returned by sqlite3_last_insert_rowid() 
  6126   6086   ** is set to the value of the rowid for the row just inserted.
         6087  +**
         6088  +** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
         6089  +** apply in the case of a constraint failure on an insert or update.
  6127   6090   */
  6128   6091   case OP_VUpdate: {
  6129   6092     sqlite3_vtab *pVtab;
  6130   6093     sqlite3_module *pModule;
  6131   6094     int nArg;
  6132   6095     int i;
  6133   6096     sqlite_int64 rowid;
................................................................................
  6208   6171     }
  6209   6172     pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
  6210   6173     break;
  6211   6174   }
  6212   6175   #endif
  6213   6176   
  6214   6177   
  6215         -#ifndef SQLITE_OMIT_TRACE
  6216         -/* Opcode: Trace * * * P4 *
         6178  +/* Opcode: Init * P2 * P4 *
         6179  +** Synopsis:  Start at P2
         6180  +**
         6181  +** Programs contain a single instance of this opcode as the very first
         6182  +** opcode.
  6217   6183   **
  6218   6184   ** If tracing is enabled (by the sqlite3_trace()) interface, then
  6219   6185   ** the UTF-8 string contained in P4 is emitted on the trace callback.
         6186  +** Or if P4 is blank, use the string returned by sqlite3_sql().
         6187  +**
         6188  +** If P2 is not zero, jump to instruction P2.
  6220   6189   */
  6221         -case OP_Trace: {
         6190  +case OP_Init: {          /* jump */
  6222   6191     char *zTrace;
  6223   6192     char *z;
  6224   6193   
         6194  +  if( pOp->p2 ){
         6195  +    pc = pOp->p2 - 1;
         6196  +  }
         6197  +#ifndef SQLITE_OMIT_TRACE
  6225   6198     if( db->xTrace
  6226   6199      && !p->doingRerun
  6227   6200      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
  6228   6201     ){
  6229   6202       z = sqlite3VdbeExpandSql(p, zTrace);
  6230   6203       db->xTrace(db->pTraceArg, z);
  6231   6204       sqlite3DbFree(db, z);
................................................................................
  6243   6216   #ifdef SQLITE_DEBUG
  6244   6217     if( (db->flags & SQLITE_SqlTrace)!=0
  6245   6218      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
  6246   6219     ){
  6247   6220       sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
  6248   6221     }
  6249   6222   #endif /* SQLITE_DEBUG */
         6223  +#endif /* SQLITE_OMIT_TRACE */
  6250   6224     break;
  6251   6225   }
  6252         -#endif
  6253   6226   
  6254   6227   
  6255   6228   /* Opcode: Noop * * * * *
  6256   6229   **
  6257   6230   ** Do nothing.  This instruction is often useful as a jump
  6258   6231   ** destination.
  6259   6232   */

Changes to src/vdbeInt.h.

    71     71     i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
    72     72     u8 nullRow;           /* True if pointing to a row with no data */
    73     73     u8 rowidIsValid;      /* True if lastRowid is valid */
    74     74     u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
    75     75     Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
    76     76     Bool isTable:1;       /* True if a table requiring integer keys */
    77     77     Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
    78         -  Bool multiPseudo:1;   /* Multi-register pseudo-cursor */
    79     78     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    80     79     i64 seqCount;         /* Sequence counter */
    81     80     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    82     81     i64 lastRowid;        /* Rowid being deleted by OP_Delete */
    83     82     VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
    84     83   
    85     84     /* Cached information about the header for the data record that the
................................................................................
   165    164       int nZero;          /* Used when bit MEM_Zero is set in flags */
   166    165       FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   167    166       RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   168    167       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   169    168     } u;
   170    169     int n;              /* Number of characters in string value, excluding '\0' */
   171    170     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   172         -  u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
          171  +  u8  memType;        /* Lower 5 bits of flags */
   173    172     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   174    173   #ifdef SQLITE_DEBUG
   175    174     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   176    175     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   177    176   #endif
   178    177     void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
   179    178     char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
................................................................................
   194    193   #define MEM_Null      0x0001   /* Value is NULL */
   195    194   #define MEM_Str       0x0002   /* Value is a string */
   196    195   #define MEM_Int       0x0004   /* Value is an integer */
   197    196   #define MEM_Real      0x0008   /* Value is a real number */
   198    197   #define MEM_Blob      0x0010   /* Value is a BLOB */
   199    198   #define MEM_RowSet    0x0020   /* Value is a RowSet object */
   200    199   #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
   201         -#define MEM_Invalid   0x0080   /* Value is undefined */
          200  +#define MEM_Undefined 0x0080   /* Value is undefined */
   202    201   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
   203    202   #define MEM_TypeMask  0x01ff   /* Mask of type bits */
   204    203   
   205    204   
   206    205   /* Whenever Mem contains a valid string or blob representation, one of
   207    206   ** the following flags must be set to determine the memory management
   208    207   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
................................................................................
   226    225      ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
   227    226   
   228    227   /*
   229    228   ** Return true if a memory cell is not marked as invalid.  This macro
   230    229   ** is for use inside assert() statements only.
   231    230   */
   232    231   #ifdef SQLITE_DEBUG
   233         -#define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
          232  +#define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
   234    233   #endif
   235    234   
   236    235   /*
   237    236   ** Each auxilliary data pointer stored by a user defined function 
   238    237   ** implementation calling sqlite3_set_auxdata() is stored in an instance
   239    238   ** of this structure. All such structures associated with a single VM
   240    239   ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
................................................................................
   440    439   double sqlite3VdbeRealValue(Mem*);
   441    440   void sqlite3VdbeIntegerAffinity(Mem*);
   442    441   int sqlite3VdbeMemRealify(Mem*);
   443    442   int sqlite3VdbeMemNumerify(Mem*);
   444    443   int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
   445    444   void sqlite3VdbeMemRelease(Mem *p);
   446    445   void sqlite3VdbeMemReleaseExternal(Mem *p);
          446  +#define VdbeMemDynamic(X)  \
          447  +  (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
   447    448   #define VdbeMemRelease(X)  \
   448         -  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
   449         -    sqlite3VdbeMemReleaseExternal(X);
          449  +  if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
   450    450   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   451    451   const char *sqlite3OpcodeName(int);
   452    452   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   453    453   int sqlite3VdbeCloseStatement(Vdbe *, int);
   454    454   void sqlite3VdbeFrameDelete(VdbeFrame*);
   455    455   int sqlite3VdbeFrameRestore(VdbeFrame *);
   456         -void sqlite3VdbeMemStoreType(Mem *pMem);
          456  +#define sqlite3VdbeMemStoreType(X)  (X)->memType = (u8)((X)->flags&0x1f)
          457  +/* void sqlite3VdbeMemStoreType(Mem *pMem); */
   457    458   void sqlite3VdbePreUpdateHook(
   458    459       Vdbe *, VdbeCursor *, int, const char*, Table *, i64, int);
   459    460   int sqlite3VdbeTransferError(Vdbe *p);
   460    461   
   461    462   int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
   462    463   void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   463    464   int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);

Changes to src/vdbeapi.c.

   168    168     return sqlite3ValueText(pVal, SQLITE_UTF16BE);
   169    169   }
   170    170   const void *sqlite3_value_text16le(sqlite3_value *pVal){
   171    171     return sqlite3ValueText(pVal, SQLITE_UTF16LE);
   172    172   }
   173    173   #endif /* SQLITE_OMIT_UTF16 */
   174    174   int sqlite3_value_type(sqlite3_value* pVal){
   175         -  return pVal->type;
          175  +  static const u8 aType[] = {
          176  +     SQLITE_BLOB,     /* 0x00 */
          177  +     SQLITE_NULL,     /* 0x01 */
          178  +     SQLITE_TEXT,     /* 0x02 */
          179  +     SQLITE_NULL,     /* 0x03 */
          180  +     SQLITE_INTEGER,  /* 0x04 */
          181  +     SQLITE_NULL,     /* 0x05 */
          182  +     SQLITE_INTEGER,  /* 0x06 */
          183  +     SQLITE_NULL,     /* 0x07 */
          184  +     SQLITE_FLOAT,    /* 0x08 */
          185  +     SQLITE_NULL,     /* 0x09 */
          186  +     SQLITE_FLOAT,    /* 0x0a */
          187  +     SQLITE_NULL,     /* 0x0b */
          188  +     SQLITE_INTEGER,  /* 0x0c */
          189  +     SQLITE_NULL,     /* 0x0d */
          190  +     SQLITE_INTEGER,  /* 0x0e */
          191  +     SQLITE_NULL,     /* 0x0f */
          192  +     SQLITE_BLOB,     /* 0x10 */
          193  +     SQLITE_NULL,     /* 0x11 */
          194  +     SQLITE_TEXT,     /* 0x12 */
          195  +     SQLITE_NULL,     /* 0x13 */
          196  +     SQLITE_INTEGER,  /* 0x14 */
          197  +     SQLITE_NULL,     /* 0x15 */
          198  +     SQLITE_INTEGER,  /* 0x16 */
          199  +     SQLITE_NULL,     /* 0x17 */
          200  +     SQLITE_FLOAT,    /* 0x18 */
          201  +     SQLITE_NULL,     /* 0x19 */
          202  +     SQLITE_FLOAT,    /* 0x1a */
          203  +     SQLITE_NULL,     /* 0x1b */
          204  +     SQLITE_INTEGER,  /* 0x1c */
          205  +     SQLITE_NULL,     /* 0x1d */
          206  +     SQLITE_INTEGER,  /* 0x1e */
          207  +     SQLITE_NULL,     /* 0x1f */
          208  +  };
          209  +  return aType[pVal->memType&0x1f];
   176    210   }
   177    211   
   178    212   /**************************** sqlite3_result_  *******************************
   179    213   ** The following routines are used by user-defined functions to specify
   180    214   ** the function result.
   181    215   **
   182    216   ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
................................................................................
  1126   1160     void (*xDel)(void*)
  1127   1161   ){
  1128   1162     return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
  1129   1163   }
  1130   1164   #endif /* SQLITE_OMIT_UTF16 */
  1131   1165   int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
  1132   1166     int rc;
  1133         -  switch( pValue->type ){
         1167  +  switch( sqlite3_value_type((sqlite3_value*)pValue) ){
  1134   1168       case SQLITE_INTEGER: {
  1135   1169         rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
  1136   1170         break;
  1137   1171       }
  1138   1172       case SQLITE_FLOAT: {
  1139   1173         rc = sqlite3_bind_double(pStmt, i, pValue->r);
  1140   1174         break;

Changes to src/vdbeaux.c.

   860    860   ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
   861    861   ** is readable but not writable, though it is cast to a writable value.
   862    862   ** The return of a dummy opcode allows the call to continue functioning
   863    863   ** after a OOM fault without having to check to see if the return from 
   864    864   ** this routine is a valid pointer.  But because the dummy.opcode is 0,
   865    865   ** dummy will never be written to.  This is verified by code inspection and
   866    866   ** by running with Valgrind.
   867         -**
   868         -** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
   869         -** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
   870         -** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
   871         -** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
   872         -** having to double-check to make sure that the result is non-negative. But
   873         -** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
   874         -** check the value of p->nOp-1 before continuing.
   875    867   */
   876    868   VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
   877    869     /* C89 specifies that the constant "dummy" will be initialized to all
   878    870     ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
   879    871     static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
   880    872     assert( p->magic==VDBE_MAGIC_INIT );
   881    873     if( addr<0 ){
   882         -#ifdef SQLITE_OMIT_TRACE
   883         -    if( p->nOp==0 ) return (VdbeOp*)&dummy;
   884         -#endif
   885    874       addr = p->nOp - 1;
   886    875     }
   887    876     assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
   888    877     if( p->db->mallocFailed ){
   889    878       return (VdbeOp*)&dummy;
   890    879     }else{
   891    880       return &p->aOp[addr];
................................................................................
  1231   1220         if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
  1232   1221           sqlite3VdbeMemRelease(p);
  1233   1222         }else if( p->zMalloc ){
  1234   1223           sqlite3DbFree(db, p->zMalloc);
  1235   1224           p->zMalloc = 0;
  1236   1225         }
  1237   1226   
  1238         -      p->flags = MEM_Invalid;
         1227  +      p->flags = MEM_Undefined;
  1239   1228       }
  1240   1229       db->mallocFailed = malloc_failed;
  1241   1230     }
  1242   1231   }
  1243   1232   
  1244   1233   /*
  1245   1234   ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
................................................................................
  1353   1342         for(j=0; i>=apSub[j]->nOp; j++){
  1354   1343           i -= apSub[j]->nOp;
  1355   1344         }
  1356   1345         pOp = &apSub[j]->aOp[i];
  1357   1346       }
  1358   1347       if( p->explain==1 ){
  1359   1348         pMem->flags = MEM_Int;
  1360         -      pMem->type = SQLITE_INTEGER;
         1349  +      pMem->memType = MEM_Int;
  1361   1350         pMem->u.i = i;                                /* Program counter */
  1362   1351         pMem++;
  1363   1352     
  1364   1353         pMem->flags = MEM_Static|MEM_Str|MEM_Term;
  1365   1354         pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
  1366   1355         assert( pMem->z!=0 );
  1367   1356         pMem->n = sqlite3Strlen30(pMem->z);
  1368         -      pMem->type = SQLITE_TEXT;
         1357  +      pMem->memType = MEM_Str;
  1369   1358         pMem->enc = SQLITE_UTF8;
  1370   1359         pMem++;
  1371   1360   
  1372   1361         /* When an OP_Program opcode is encounter (the only opcode that has
  1373   1362         ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
  1374   1363         ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
  1375   1364         ** has not already been seen.
................................................................................
  1387   1376             pSub->n = nSub*sizeof(SubProgram*);
  1388   1377           }
  1389   1378         }
  1390   1379       }
  1391   1380   
  1392   1381       pMem->flags = MEM_Int;
  1393   1382       pMem->u.i = pOp->p1;                          /* P1 */
  1394         -    pMem->type = SQLITE_INTEGER;
         1383  +    pMem->memType = MEM_Int;
  1395   1384       pMem++;
  1396   1385   
  1397   1386       pMem->flags = MEM_Int;
  1398   1387       pMem->u.i = pOp->p2;                          /* P2 */
  1399         -    pMem->type = SQLITE_INTEGER;
         1388  +    pMem->memType = MEM_Int;
  1400   1389       pMem++;
  1401   1390   
  1402   1391       pMem->flags = MEM_Int;
  1403   1392       pMem->u.i = pOp->p3;                          /* P3 */
  1404         -    pMem->type = SQLITE_INTEGER;
         1393  +    pMem->memType = MEM_Int;
  1405   1394       pMem++;
  1406   1395   
  1407   1396       if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
  1408   1397         assert( p->db->mallocFailed );
  1409   1398         return SQLITE_ERROR;
  1410   1399       }
  1411   1400       pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
................................................................................
  1413   1402       if( zP4!=pMem->z ){
  1414   1403         sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
  1415   1404       }else{
  1416   1405         assert( pMem->z!=0 );
  1417   1406         pMem->n = sqlite3Strlen30(pMem->z);
  1418   1407         pMem->enc = SQLITE_UTF8;
  1419   1408       }
  1420         -    pMem->type = SQLITE_TEXT;
         1409  +    pMem->memType = MEM_Str;
  1421   1410       pMem++;
  1422   1411   
  1423   1412       if( p->explain==1 ){
  1424   1413         if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
  1425   1414           assert( p->db->mallocFailed );
  1426   1415           return SQLITE_ERROR;
  1427   1416         }
  1428   1417         pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
  1429   1418         pMem->n = 2;
  1430   1419         sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
  1431         -      pMem->type = SQLITE_TEXT;
         1420  +      pMem->memType = MEM_Str;
  1432   1421         pMem->enc = SQLITE_UTF8;
  1433   1422         pMem++;
  1434   1423     
  1435   1424   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
  1436   1425         if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
  1437   1426           assert( p->db->mallocFailed );
  1438   1427           return SQLITE_ERROR;
  1439   1428         }
  1440   1429         pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
  1441   1430         pMem->n = displayComment(pOp, zP4, pMem->z, 500);
  1442         -      pMem->type = SQLITE_TEXT;
         1431  +      pMem->memType = MEM_Str;
  1443   1432         pMem->enc = SQLITE_UTF8;
  1444   1433   #else
  1445   1434         pMem->flags = MEM_Null;                       /* Comment */
  1446         -      pMem->type = SQLITE_NULL;
         1435  +      pMem->memType = MEM_Null;
  1447   1436   #endif
  1448   1437       }
  1449   1438   
  1450   1439       p->nResColumn = 8 - 4*(p->explain-1);
  1451   1440       p->pResultSet = &p->aMem[1];
  1452   1441       p->rc = SQLITE_OK;
  1453   1442       rc = SQLITE_ROW;
................................................................................
  1462   1451   */
  1463   1452   void sqlite3VdbePrintSql(Vdbe *p){
  1464   1453     const char *z = 0;
  1465   1454     if( p->zSql ){
  1466   1455       z = p->zSql;
  1467   1456     }else if( p->nOp>=1 ){
  1468   1457       const VdbeOp *pOp = &p->aOp[0];
  1469         -    if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
         1458  +    if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
  1470   1459         z = pOp->p4.z;
  1471   1460         while( sqlite3Isspace(*z) ) z++;
  1472   1461       }
  1473   1462     }
  1474   1463     if( z ) printf("SQL: [%s]\n", z);
  1475   1464   }
  1476   1465   #endif
................................................................................
  1481   1470   */
  1482   1471   void sqlite3VdbeIOTraceSql(Vdbe *p){
  1483   1472     int nOp = p->nOp;
  1484   1473     VdbeOp *pOp;
  1485   1474     if( sqlite3IoTrace==0 ) return;
  1486   1475     if( nOp<1 ) return;
  1487   1476     pOp = &p->aOp[0];
  1488         -  if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
         1477  +  if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
  1489   1478       int i, j;
  1490   1479       char z[1000];
  1491   1480       sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
  1492   1481       for(i=0; sqlite3Isspace(z[i]); i++){}
  1493   1482       for(j=0; z[i]; i++){
  1494   1483         if( sqlite3Isspace(z[i]) ){
  1495   1484           if( z[i-1]!=' ' ){
................................................................................
  1699   1688       memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
  1700   1689       memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
  1701   1690     }
  1702   1691     if( p->aMem ){
  1703   1692       p->aMem--;                      /* aMem[] goes from 1..nMem */
  1704   1693       p->nMem = nMem;                 /*       not from 0..nMem-1 */
  1705   1694       for(n=1; n<=nMem; n++){
  1706         -      p->aMem[n].flags = MEM_Invalid;
         1695  +      p->aMem[n].flags = MEM_Undefined;
  1707   1696         p->aMem[n].db = db;
  1708   1697       }
  1709   1698     }
  1710   1699     p->explain = pParse->explain;
  1711   1700     sqlite3VdbeRewind(p);
  1712   1701   }
  1713   1702   
................................................................................
  1811   1800   
  1812   1801   #ifdef SQLITE_DEBUG
  1813   1802     /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
  1814   1803     ** Vdbe.aMem[] arrays have already been cleaned up.  */
  1815   1804     int i;
  1816   1805     if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
  1817   1806     if( p->aMem ){
  1818         -    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
         1807  +    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
  1819   1808     }
  1820   1809   #endif
  1821   1810   
  1822   1811     sqlite3DbFree(db, p->zErrMsg);
  1823   1812     p->zErrMsg = 0;
  1824   1813     p->pResultSet = 0;
  1825   1814   }

Changes to src/vdbeblob.c.

   132    132     ** blob_bytes() functions.
   133    133     **
   134    134     ** The sqlite3_blob_close() function finalizes the vdbe program,
   135    135     ** which closes the b-tree cursor and (possibly) commits the 
   136    136     ** transaction.
   137    137     */
   138    138     static const VdbeOpList openBlob[] = {
   139         -    {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
   140         -    {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
   141         -    {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
          139  +    /* {OP_Transaction, 0, 0, 0},  // 0: Inserted separately */
          140  +    {OP_TableLock, 0, 0, 0},       /* 1: Acquire a read or write lock */
   142    141   
   143    142       /* One of the following two instructions is replaced by an OP_Noop. */
   144         -    {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
   145         -    {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
          143  +    {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
          144  +    {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
   146    145   
   147         -    {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
   148         -    {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
   149         -    {OP_Column, 0, 0, 1},          /* 7  */
   150         -    {OP_ResultRow, 1, 0, 0},       /* 8  */
   151         -    {OP_Goto, 0, 5, 0},            /* 9  */
   152         -    {OP_Close, 0, 0, 0},           /* 10 */
   153         -    {OP_Halt, 0, 0, 0},            /* 11 */
          146  +    {OP_Variable, 1, 1, 1},        /* 4: Push the rowid to the stack */
          147  +    {OP_NotExists, 0, 10, 1},      /* 5: Seek the cursor */
          148  +    {OP_Column, 0, 0, 1},          /* 6  */
          149  +    {OP_ResultRow, 1, 0, 0},       /* 7  */
          150  +    {OP_Goto, 0, 4, 0},            /* 8  */
          151  +    {OP_Close, 0, 0, 0},           /* 9  */
          152  +    {OP_Halt, 0, 0, 0},            /* 10 */
   154    153     };
   155    154   
   156    155     int rc = SQLITE_OK;
   157    156     char *zErr = 0;
   158    157     Table *pTab;
   159    158     Parse *pParse = 0;
   160    159     Incrblob *pBlob = 0;
................................................................................
   261    260   
   262    261       pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
   263    262       assert( pBlob->pStmt || db->mallocFailed );
   264    263       if( pBlob->pStmt ){
   265    264         Vdbe *v = (Vdbe *)pBlob->pStmt;
   266    265         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   267    266   
          267  +
          268  +      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, 
          269  +                           pTab->pSchema->schema_cookie,
          270  +                           pTab->pSchema->iGeneration);
          271  +      sqlite3VdbeChangeP5(v, 1);     
   268    272         sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
   269    273   
   270         -
   271         -      /* Configure the OP_Transaction */
   272         -      sqlite3VdbeChangeP1(v, 0, iDb);
   273         -      sqlite3VdbeChangeP2(v, 0, flags);
   274         -
   275         -      /* Configure the OP_VerifyCookie */
   276         -      sqlite3VdbeChangeP1(v, 1, iDb);
   277         -      sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
   278         -      sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
   279         -
   280    274         /* Make sure a mutex is held on the table to be accessed */
   281    275         sqlite3VdbeUsesBtree(v, iDb); 
   282    276   
   283    277         /* Configure the OP_TableLock instruction */
   284    278   #ifdef SQLITE_OMIT_SHARED_CACHE
   285         -      sqlite3VdbeChangeToNoop(v, 2);
          279  +      sqlite3VdbeChangeToNoop(v, 1);
   286    280   #else
   287         -      sqlite3VdbeChangeP1(v, 2, iDb);
   288         -      sqlite3VdbeChangeP2(v, 2, pTab->tnum);
   289         -      sqlite3VdbeChangeP3(v, 2, flags);
   290         -      sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
          281  +      sqlite3VdbeChangeP1(v, 1, iDb);
          282  +      sqlite3VdbeChangeP2(v, 1, pTab->tnum);
          283  +      sqlite3VdbeChangeP3(v, 1, flags);
          284  +      sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
   291    285   #endif
   292    286   
   293    287         /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
   294    288         ** parameter of the other to pTab->tnum.  */
   295         -      sqlite3VdbeChangeToNoop(v, 4 - flags);
   296         -      sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
   297         -      sqlite3VdbeChangeP3(v, 3 + flags, iDb);
          289  +      sqlite3VdbeChangeToNoop(v, 3 - flags);
          290  +      sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
          291  +      sqlite3VdbeChangeP3(v, 2 + flags, iDb);
   298    292   
   299    293         /* Configure the number of columns. Configure the cursor to
   300    294         ** think that the table has one more column than it really
   301    295         ** does. An OP_Column to retrieve this imaginary column will
   302    296         ** always return an SQL NULL. This is useful because it means
   303    297         ** we can invoke OP_Column to fill in the vdbe cursors type 
   304    298         ** and offset cache without causing any IO.
   305    299         */
   306         -      sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
   307         -      sqlite3VdbeChangeP2(v, 7, pTab->nCol);
          300  +      sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
          301  +      sqlite3VdbeChangeP2(v, 6, pTab->nCol);
   308    302         if( !db->mallocFailed ){
   309    303           pParse->nVar = 1;
   310    304           pParse->nMem = 1;
   311    305           pParse->nTab = 1;
   312    306           sqlite3VdbeMakeReady(v, pParse);
   313    307         }
   314    308       }

Changes to src/vdbemem.c.

   285    285       sqlite3VdbeMemSetNull(p);
   286    286     }
   287    287   }
   288    288   
   289    289   /*
   290    290   ** Release any memory held by the Mem. This may leave the Mem in an
   291    291   ** inconsistent state, for example with (Mem.z==0) and
   292         -** (Mem.type==SQLITE_TEXT).
          292  +** (Mem.memType==MEM_Str).
   293    293   */
   294    294   void sqlite3VdbeMemRelease(Mem *p){
   295    295     VdbeMemRelease(p);
   296    296     if( p->zMalloc ){
   297    297       sqlite3DbFree(p->db, p->zMalloc);
   298    298       p->zMalloc = 0;
   299    299     }
................................................................................
   476    476       pFrame->pParent = pFrame->v->pDelFrame;
   477    477       pFrame->v->pDelFrame = pFrame;
   478    478     }
   479    479     if( pMem->flags & MEM_RowSet ){
   480    480       sqlite3RowSetClear(pMem->u.pRowSet);
   481    481     }
   482    482     MemSetTypeFlag(pMem, MEM_Null);
   483         -  pMem->type = SQLITE_NULL;
          483  +  pMem->memType = MEM_Null;
   484    484   }
   485    485   void sqlite3ValueSetNull(sqlite3_value *p){
   486    486     sqlite3VdbeMemSetNull((Mem*)p); 
   487    487   }
   488    488   
   489    489   /*
   490    490   ** Delete any previous value and set the value to be a BLOB of length
   491    491   ** n containing all zeros.
   492    492   */
   493    493   void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
   494    494     sqlite3VdbeMemRelease(pMem);
   495    495     pMem->flags = MEM_Blob|MEM_Zero;
   496         -  pMem->type = SQLITE_BLOB;
          496  +  pMem->memType = MEM_Blob;
   497    497     pMem->n = 0;
   498    498     if( n<0 ) n = 0;
   499    499     pMem->u.nZero = n;
   500    500     pMem->enc = SQLITE_UTF8;
   501    501   
   502    502   #ifdef SQLITE_OMIT_INCRBLOB
   503    503     sqlite3VdbeMemGrow(pMem, n, 0);
................................................................................
   512    512   ** Delete any previous value and set the value stored in *pMem to val,
   513    513   ** manifest type INTEGER.
   514    514   */
   515    515   void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
   516    516     sqlite3VdbeMemRelease(pMem);
   517    517     pMem->u.i = val;
   518    518     pMem->flags = MEM_Int;
   519         -  pMem->type = SQLITE_INTEGER;
          519  +  pMem->memType = MEM_Int;
   520    520   }
   521    521   
   522    522   #ifndef SQLITE_OMIT_FLOATING_POINT
   523    523   /*
   524    524   ** Delete any previous value and set the value stored in *pMem to val,
   525    525   ** manifest type REAL.
   526    526   */
................................................................................
   527    527   void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
   528    528     if( sqlite3IsNaN(val) ){
   529    529       sqlite3VdbeMemSetNull(pMem);
   530    530     }else{
   531    531       sqlite3VdbeMemRelease(pMem);
   532    532       pMem->r = val;
   533    533       pMem->flags = MEM_Real;
   534         -    pMem->type = SQLITE_FLOAT;
          534  +    pMem->memType = MEM_Real;
   535    535     }
   536    536   }
   537    537   #endif
   538    538   
   539    539   /*
   540    540   ** Delete any previous value and set the value of pMem to be an
   541    541   ** empty boolean index.
................................................................................
   583    583   ** copies are not misused.
   584    584   */
   585    585   void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
   586    586     int i;
   587    587     Mem *pX;
   588    588     for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
   589    589       if( pX->pScopyFrom==pMem ){
   590         -      pX->flags |= MEM_Invalid;
          590  +      pX->flags |= MEM_Undefined;
   591    591         pX->pScopyFrom = 0;
   592    592       }
   593    593     }
   594    594     pMem->pScopyFrom = 0;
   595    595   }
   596    596   #endif /* SQLITE_DEBUG */
   597    597   
................................................................................
   735    735       pMem->xDel = xDel;
   736    736       flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
   737    737     }
   738    738   
   739    739     pMem->n = nByte;
   740    740     pMem->flags = flags;
   741    741     pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
   742         -  pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
          742  +  pMem->memType = flags&0x1f;
   743    743   
   744    744   #ifndef SQLITE_OMIT_UTF16
   745    745     if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
   746    746       return SQLITE_NOMEM;
   747    747     }
   748    748   #endif
   749    749   
................................................................................
   906    906     if( offset+amt<=available ){
   907    907       sqlite3VdbeMemRelease(pMem);
   908    908       pMem->z = &zData[offset];
   909    909       pMem->flags = MEM_Blob|MEM_Ephem;
   910    910     }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
   911    911       pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
   912    912       pMem->enc = 0;
   913         -    pMem->type = SQLITE_BLOB;
          913  +    pMem->memType = MEM_Blob;
   914    914       if( key ){
   915    915         rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
   916    916       }else{
   917    917         rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
   918    918       }
   919    919       pMem->z[amt] = 0;
   920    920       pMem->z[amt+1] = 0;
................................................................................
   976    976   /*
   977    977   ** Create a new sqlite3_value object.
   978    978   */
   979    979   sqlite3_value *sqlite3ValueNew(sqlite3 *db){
   980    980     Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
   981    981     if( p ){
   982    982       p->flags = MEM_Null;
   983         -    p->type = SQLITE_NULL;
          983  +    p->memType = MEM_Null;
   984    984       p->db = db;
   985    985     }
   986    986     return p;
   987    987   }
   988    988   
   989    989   /*
   990    990   ** Context object passed by sqlite3Stat4ProbeSetValue() through to 
................................................................................
  1026   1026           if( pRec->pKeyInfo ){
  1027   1027             assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
  1028   1028             assert( pRec->pKeyInfo->enc==ENC(db) );
  1029   1029             pRec->flags = UNPACKED_PREFIX_MATCH;
  1030   1030             pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
  1031   1031             for(i=0; i<nCol; i++){
  1032   1032               pRec->aMem[i].flags = MEM_Null;
  1033         -            pRec->aMem[i].type = SQLITE_NULL;
         1033  +            pRec->aMem[i].memType = MEM_Null;
  1034   1034               pRec->aMem[i].db = db;
  1035   1035             }
  1036   1036           }else{
  1037   1037             sqlite3DbFree(db, pRec);
  1038   1038             pRec = 0;
  1039   1039           }
  1040   1040         }
................................................................................
  1099   1099       if( pVal==0 ) goto no_mem;
  1100   1100       if( ExprHasProperty(pExpr, EP_IntValue) ){
  1101   1101         sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
  1102   1102       }else{
  1103   1103         zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
  1104   1104         if( zVal==0 ) goto no_mem;
  1105   1105         sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
  1106         -      if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
         1106  +      if( op==TK_FLOAT ) pVal->memType = MEM_Real;
  1107   1107       }
  1108   1108       if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
  1109   1109         sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
  1110   1110       }else{
  1111   1111         sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
  1112   1112       }
  1113   1113       if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;

Changes to src/where.c.

  2781   2781       sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
  2782   2782       VdbeComment((v, "init LEFT JOIN no-match flag"));
  2783   2783     }
  2784   2784   
  2785   2785     /* Special case of a FROM clause subquery implemented as a co-routine */
  2786   2786     if( pTabItem->viaCoroutine ){
  2787   2787       int regYield = pTabItem->regReturn;
  2788         -    sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield);
  2789         -    pLevel->p2 =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
  2790         -    VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
  2791         -    sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
         2788  +    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
         2789  +    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
         2790  +    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
  2792   2791       pLevel->op = OP_Goto;
  2793   2792     }else
  2794   2793   
  2795   2794   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2796   2795     if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  2797   2796       /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
  2798   2797       **          to access the data.
................................................................................
  3180   3179         pLevel->op = OP_Noop;
  3181   3180       }else if( bRev ){
  3182   3181         pLevel->op = OP_Prev;
  3183   3182       }else{
  3184   3183         pLevel->op = OP_Next;
  3185   3184       }
  3186   3185       pLevel->p1 = iIdxCur;
         3186  +    assert( (WHERE_UNQ_WANTED>>16)==1 );
         3187  +    pLevel->p3 = (pLoop->wsFlags>>16)&1;
  3187   3188       if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
  3188   3189         pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  3189   3190       }else{
  3190   3191         assert( pLevel->p5==0 );
  3191   3192       }
  3192   3193     }else
  3193   3194   
................................................................................
  3310   3311       */
  3311   3312       if( pWC->nTerm>1 ){
  3312   3313         int iTerm;
  3313   3314         for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
  3314   3315           Expr *pExpr = pWC->a[iTerm].pExpr;
  3315   3316           if( &pWC->a[iTerm] == pTerm ) continue;
  3316   3317           if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
  3317         -        if( pWC->a[iTerm].wtFlags & (TERM_ORINFO) ) continue;
         3318  +        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
         3319  +        testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
         3320  +        if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue;
  3318   3321           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
  3319   3322           pExpr = sqlite3ExprDup(db, pExpr, 0);
  3320   3323           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
  3321   3324         }
  3322   3325         if( pAndExpr ){
  3323   3326           pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
  3324   3327         }
................................................................................
  3982   3985         pNew->nOut = nRowEst + nInMul + nIn;
  3983   3986       }else if( pTerm->eOperator & (WO_EQ) ){
  3984   3987         assert(
  3985   3988           (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0
  3986   3989           || nInMul==0
  3987   3990         );
  3988   3991         pNew->wsFlags |= WHERE_COLUMN_EQ;
  3989         -      if( iCol<0  
  3990         -       || (pProbe->onError!=OE_None && nInMul==0
  3991         -           && pNew->u.btree.nEq==pProbe->nKeyCol-1)
  3992         -      ){
         3992  +      if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1)){
  3993   3993           assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
  3994         -        pNew->wsFlags |= WHERE_ONEROW;
         3994  +        if( iCol>=0 && pProbe->onError==OE_None ){
         3995  +          pNew->wsFlags |= WHERE_UNQ_WANTED;
         3996  +        }else{
         3997  +          pNew->wsFlags |= WHERE_ONEROW;
         3998  +        }
  3995   3999         }
  3996   4000         pNew->u.btree.nEq++;
  3997   4001         pNew->nOut = nRowEst + nInMul;
  3998   4002       }else if( pTerm->eOperator & (WO_ISNULL) ){
  3999   4003         pNew->wsFlags |= WHERE_COLUMN_NULL;
  4000   4004         pNew->u.btree.nEq++;
  4001   4005         /* TUNING: IS NULL selects 2 rows */
................................................................................
  5430   5434   
  5431   5435     /* Split the WHERE clause into separate subexpressions where each
  5432   5436     ** subexpression is separated by an AND operator.
  5433   5437     */
  5434   5438     initMaskSet(pMaskSet);
  5435   5439     whereClauseInit(&pWInfo->sWC, pWInfo);
  5436   5440     whereSplit(&pWInfo->sWC, pWhere, TK_AND);
  5437         -  sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
  5438   5441       
  5439   5442     /* Special case: a WHERE clause that is constant.  Evaluate the
  5440   5443     ** expression and either jump over all of the code or fall thru.
  5441   5444     */
  5442   5445     for(ii=0; ii<sWLB.pWC->nTerm; ii++){
  5443   5446       if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
  5444   5447         sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
................................................................................
  5719   5722         pLevel->iIdxCur = iIndexCur;
  5720   5723         assert( pIx->pSchema==pTab->pSchema );
  5721   5724         assert( iIndexCur>=0 );
  5722   5725         sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
  5723   5726         sqlite3VdbeSetP4KeyInfo(pParse, pIx);
  5724   5727         VdbeComment((v, "%s", pIx->zName));
  5725   5728       }
  5726         -    sqlite3CodeVerifySchema(pParse, iDb);
         5729  +    if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
  5727   5730       notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
  5728   5731     }
  5729   5732     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
  5730   5733     if( db->mallocFailed ) goto whereBeginError;
  5731   5734   
  5732   5735     /* Generate the code to do the search.  Each iteration of the for
  5733   5736     ** loop below generates code for a single nested loop of the VM
................................................................................
  5781   5784     sqlite3ExprCacheClear(pParse);
  5782   5785     for(i=pWInfo->nLevel-1; i>=0; i--){
  5783   5786       int addr;
  5784   5787       pLevel = &pWInfo->a[i];
  5785   5788       pLoop = pLevel->pWLoop;
  5786   5789       sqlite3VdbeResolveLabel(v, pLevel->addrCont);
  5787   5790       if( pLevel->op!=OP_Noop ){
  5788         -      sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
         5791  +      sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
  5789   5792         sqlite3VdbeChangeP5(v, pLevel->p5);
  5790   5793       }
  5791   5794       if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
  5792   5795         struct InLoop *pIn;
  5793   5796         int j;
  5794   5797         sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
  5795   5798         for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
................................................................................
  5830   5833     /* The "break" point is here, just past the end of the outer loop.
  5831   5834     ** Set it.
  5832   5835     */
  5833   5836     sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
  5834   5837   
  5835   5838     assert( pWInfo->nLevel<=pTabList->nSrc );
  5836   5839     for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
         5840  +    int k, last;
         5841  +    VdbeOp *pOp;
  5837   5842       Index *pIdx = 0;
  5838   5843       struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
  5839   5844       Table *pTab = pTabItem->pTab;
  5840   5845       assert( pTab!=0 );
  5841   5846       pLoop = pLevel->pWLoop;
         5847  +
         5848  +    /* For a co-routine, change all OP_Column references to the table of
         5849  +    ** the co-routine into OP_SCopy of result contained in a register.
         5850  +    ** OP_Rowid becomes OP_Null.
         5851  +    */
         5852  +    if( pTabItem->viaCoroutine ){
         5853  +      last = sqlite3VdbeCurrentAddr(v);
         5854  +      k = pLevel->addrBody;
         5855  +      pOp = sqlite3VdbeGetOp(v, k);
         5856  +      for(; k<last; k++, pOp++){
         5857  +        if( pOp->p1!=pLevel->iTabCur ) continue;
         5858  +        if( pOp->opcode==OP_Column ){
         5859  +          pOp->opcode = OP_SCopy;
         5860  +          pOp->p1 = pOp->p2 + pTabItem->regResult;
         5861  +          pOp->p2 = pOp->p3;
         5862  +          pOp->p3 = 0;
         5863  +        }else if( pOp->opcode==OP_Rowid ){
         5864  +          pOp->opcode = OP_Null;
         5865  +          pOp->p1 = 0;
         5866  +          pOp->p3 = 0;
         5867  +        }
         5868  +      }
         5869  +      continue;
         5870  +    }
  5842   5871   
  5843   5872       /* Close all of the cursors that were opened by sqlite3WhereBegin.
  5844   5873       ** Except, do not close cursors that will be reused by the OR optimization
  5845   5874       ** (WHERE_OMIT_OPEN_CLOSE).  And do not close the OP_OpenWrite cursors
  5846   5875       ** created for the ONEPASS optimization.
  5847   5876       */
  5848   5877       if( (pTab->tabFlags & TF_Ephemeral)==0
................................................................................
  5874   5903       */
  5875   5904       if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
  5876   5905         pIdx = pLoop->u.btree.pIndex;
  5877   5906       }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
  5878   5907         pIdx = pLevel->u.pCovidx;
  5879   5908       }
  5880   5909       if( pIdx && !db->mallocFailed ){
  5881         -      int k, last;
  5882         -      VdbeOp *pOp;
  5883         -
  5884   5910         last = sqlite3VdbeCurrentAddr(v);
  5885   5911         k = pLevel->addrBody;
  5886   5912         pOp = sqlite3VdbeGetOp(v, k);
  5887   5913         for(; k<last; k++, pOp++){
  5888   5914           if( pOp->p1!=pLevel->iTabCur ) continue;
  5889   5915           if( pOp->opcode==OP_Column ){
  5890   5916             int x = pOp->p2;

Changes to src/whereInt.h.

    66     66     int addrBrk;          /* Jump here to break out of the loop */
    67     67     int addrNxt;          /* Jump here to start the next IN combination */
    68     68     int addrSkip;         /* Jump here for next iteration of skip-scan */
    69     69     int addrCont;         /* Jump here to continue with the next loop cycle */
    70     70     int addrFirst;        /* First instruction of interior of the loop */
    71     71     int addrBody;         /* Beginning of the body of this loop */
    72     72     u8 iFrom;             /* Which entry in the FROM clause */
    73         -  u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
           73  +  u8 op, p3, p5;        /* Opcode, P3 & P5 of the opcode that ends the loop */
    74     74     int p1, p2;           /* Operands of the opcode used to ends the loop */
    75     75     union {               /* Information that depends on pWLoop->wsFlags */
    76     76       struct {
    77     77         int nIn;              /* Number of entries in aInLoop[] */
    78     78         struct InLoop {
    79     79           int iCur;              /* The VDBE cursor used by this IN operator */
    80     80           int addrInTop;         /* Top of the IN loop */
................................................................................
   453    453   #define WHERE_INDEXED      0x00000200  /* WhereLoop.u.btree.pIndex is valid */
   454    454   #define WHERE_VIRTUALTABLE 0x00000400  /* WhereLoop.u.vtab is valid */
   455    455   #define WHERE_IN_ABLE      0x00000800  /* Able to support an IN operator */
   456    456   #define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
   457    457   #define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
   458    458   #define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
   459    459   #define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
          460  +#define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/

Changes to test/distinct.test.

   192    192     }
   193    193   } {{} {} {} 3 6 {}}
   194    194   do_test 3.1 {
   195    195     regexp {OpenEphemeral} [db eval {
   196    196       EXPLAIN SELECT DISTINCT a, b FROM t3 ORDER BY +a, +b;
   197    197     }]
   198    198   } {0}
          199  +
          200  +#-------------------------------------------------------------------------
          201  +# Ticket  [fccbde530a6583bf2748400919f1603d5425995c] (2014-01-08)
          202  +# The logic that computes DISTINCT sometimes thinks that a zeroblob()
          203  +# and a blob of all zeros are different when they should be the same. 
          204  +#
          205  +do_execsql_test 4.1 {
          206  +  DROP TABLE IF EXISTS t1;
          207  +  DROP TABLE IF EXISTS t2;
          208  +  CREATE TABLE t1(a INTEGER);
          209  +  INSERT INTO t1 VALUES(3);
          210  +  INSERT INTO t1 VALUES(2);
          211  +  INSERT INTO t1 VALUES(1);
          212  +  INSERT INTO t1 VALUES(2);
          213  +  INSERT INTO t1 VALUES(3);
          214  +  INSERT INTO t1 VALUES(1);
          215  +  CREATE TABLE t2(x);
          216  +  INSERT INTO t2
          217  +    SELECT DISTINCT
          218  +      CASE a WHEN 1 THEN x'0000000000'
          219  +             WHEN 2 THEN zeroblob(5)
          220  +             ELSE 'xyzzy' END
          221  +      FROM t1;
          222  +  SELECT quote(x) FROM t2 ORDER BY 1;
          223  +} {'xyzzy' X'0000000000'}
   199    224   
   200    225   finish_test

Changes to test/selectA.test.

  1288   1288         UNION SELECT a,b,c FROM t3
  1289   1289         INTERSECT SELECT a,b,c FROM t3
  1290   1290         EXCEPT SELECT c,b,a FROM t1
  1291   1291         UNION SELECT a,b,c FROM t3
  1292   1292         ORDER BY y COLLATE NOCASE DESC,x,z)))
  1293   1293     }
  1294   1294   } {MAD}
         1295  +do_execsql_test selectA-3.98 {
         1296  +  WITH RECURSIVE
         1297  +    xyz(n) AS (
         1298  +      SELECT upper((SELECT x FROM (
         1299  +        SELECT x,y,z FROM t2
         1300  +        INTERSECT SELECT a,b,c FROM t3
         1301  +        EXCEPT SELECT c,b,a FROM t1
         1302  +        UNION SELECT a,b,c FROM t3
         1303  +        INTERSECT SELECT a,b,c FROM t3
         1304  +        EXCEPT SELECT c,b,a FROM t1
         1305  +        UNION SELECT a,b,c FROM t3
         1306  +        ORDER BY y COLLATE NOCASE DESC,x,z)))
         1307  +      UNION ALL
         1308  +      SELECT n || '+' FROM xyz WHERE length(n)<5
         1309  +    )
         1310  +  SELECT n FROM xyz ORDER BY +n;
         1311  +} {MAD MAD+ MAD++}
  1295   1312   
  1296   1313   finish_test

Changes to test/speedtest1.c.

   732    732     speedtest1_end_test();
   733    733   
   734    734   
   735    735     speedtest1_begin_test(990, "ANALYZE");
   736    736     speedtest1_exec("ANALYZE");
   737    737     speedtest1_end_test();
   738    738   }
          739  +
          740  +/*
          741  +** A testset for common table expressions.  This exercises code
          742  +** for views, subqueries, co-routines, etc.
          743  +*/
          744  +void testset_cte(void){
          745  +  static const char *azPuzzle[] = {
          746  +    /* Easy */
          747  +    "534...9.."
          748  +    "67.195..."
          749  +    ".98....6."
          750  +    "8...6...3"
          751  +    "4..8.3..1"
          752  +    "....2...6"
          753  +    ".6....28."
          754  +    "...419..5"
          755  +    "...28..79",
          756  +
          757  +    /* Medium */
          758  +    "53....9.."
          759  +    "6..195..."
          760  +    ".98....6."
          761  +    "8...6...3"
          762  +    "4..8.3..1"
          763  +    "....2...6"
          764  +    ".6....28."
          765  +    "...419..5"
          766  +    "....8..79",
          767  +
          768  +    /* Hard */
          769  +    "53......."
          770  +    "6..195..."
          771  +    ".98....6."
          772  +    "8...6...3"
          773  +    "4..8.3..1"
          774  +    "....2...6"
          775  +    ".6....28."
          776  +    "...419..5"
          777  +    "....8..79",
          778  +  };
          779  +  const char *zPuz;
          780  +  double rSpacing;
          781  +  int nElem;
          782  +
          783  +  if( g.szTest<25 ){
          784  +    zPuz = azPuzzle[0];
          785  +  }else if( g.szTest<70 ){
          786  +    zPuz = azPuzzle[1];
          787  +  }else{
          788  +    zPuz = azPuzzle[2];
          789  +  }
          790  +  speedtest1_begin_test(100, "Sudoku with recursive 'digits'");
          791  +  speedtest1_prepare(
          792  +    "WITH RECURSIVE\n"
          793  +    "  input(sud) AS (VALUES(?1)),\n"
          794  +    "  digits(z,lp) AS (\n"
          795  +    "    VALUES('1', 1)\n"
          796  +    "    UNION ALL\n"
          797  +    "    SELECT CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9\n"
          798  +    "  ),\n"
          799  +    "  x(s, ind) AS (\n"
          800  +    "    SELECT sud, instr(sud, '.') FROM input\n"
          801  +    "    UNION ALL\n"
          802  +    "    SELECT\n"
          803  +    "      substr(s, 1, ind-1) || z || substr(s, ind+1),\n"
          804  +    "      instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n"
          805  +    "     FROM x, digits AS z\n"
          806  +    "    WHERE ind>0\n"
          807  +    "      AND NOT EXISTS (\n"
          808  +    "            SELECT 1\n"
          809  +    "              FROM digits AS lp\n"
          810  +    "             WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n"
          811  +    "                OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n"
          812  +    "                OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n"
          813  +    "                        + ((ind-1)/27) * 27 + lp\n"
          814  +    "                        + ((lp-1) / 3) * 6, 1)\n"
          815  +    "         )\n"
          816  +    "  )\n"
          817  +    "SELECT s FROM x WHERE ind=0;"
          818  +  );
          819  +  sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC);
          820  +  speedtest1_run();
          821  +  speedtest1_end_test();
          822  +
          823  +  speedtest1_begin_test(200, "Sudoku with VALUES 'digits'");
          824  +  speedtest1_prepare(
          825  +    "WITH RECURSIVE\n"
          826  +    "  input(sud) AS (VALUES(?1)),\n"
          827  +    "  digits(z,lp) AS (VALUES('1',1),('2',2),('3',3),('4',4),('5',5),\n"
          828  +    "                         ('6',6),('7',7),('8',8),('9',9)),\n"
          829  +    "  x(s, ind) AS (\n"
          830  +    "    SELECT sud, instr(sud, '.') FROM input\n"
          831  +    "    UNION ALL\n"
          832  +    "    SELECT\n"
          833  +    "      substr(s, 1, ind-1) || z || substr(s, ind+1),\n"
          834  +    "      instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n"
          835  +    "     FROM x, digits AS z\n"
          836  +    "    WHERE ind>0\n"
          837  +    "      AND NOT EXISTS (\n"
          838  +    "            SELECT 1\n"
          839  +    "              FROM digits AS lp\n"
          840  +    "             WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n"
          841  +    "                OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n"
          842  +    "                OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n"
          843  +    "                        + ((ind-1)/27) * 27 + lp\n"
          844  +    "                        + ((lp-1) / 3) * 6, 1)\n"
          845  +    "         )\n"
          846  +    "  )\n"
          847  +    "SELECT s FROM x WHERE ind=0;"
          848  +  );
          849  +  sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC);
          850  +  speedtest1_run();
          851  +  speedtest1_end_test();
          852  +
          853  +  rSpacing = 5.0/g.szTest;
          854  +  speedtest1_begin_test(300, "Mandelbrot Set with spacing=%f", rSpacing);
          855  +  speedtest1_prepare(
          856  +   "WITH RECURSIVE \n"
          857  +   "  xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+?1 FROM xaxis WHERE x<1.2),\n"
          858  +   "  yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+?2 FROM yaxis WHERE y<1.0),\n"
          859  +   "  m(iter, cx, cy, x, y) AS (\n"
          860  +   "    SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis\n"
          861  +   "    UNION ALL\n"
          862  +   "    SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m \n"
          863  +   "     WHERE (x*x + y*y) < 4.0 AND iter<28\n"
          864  +   "  ),\n"
          865  +   "  m2(iter, cx, cy) AS (\n"
          866  +   "    SELECT max(iter), cx, cy FROM m GROUP BY cx, cy\n"
          867  +   "  ),\n"
          868  +   "  a(t) AS (\n"
          869  +   "    SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') \n"
          870  +   "    FROM m2 GROUP BY cy\n"
          871  +   "  )\n"
          872  +   "SELECT group_concat(rtrim(t),x'0a') FROM a;"
          873  +  );
          874  +  sqlite3_bind_double(g.pStmt, 1, rSpacing*.05);
          875  +  sqlite3_bind_double(g.pStmt, 2, rSpacing);
          876  +  speedtest1_run();
          877  +  speedtest1_end_test();
          878  +
          879  +  nElem = 10000*g.szTest;
          880  +  speedtest1_begin_test(400, "EXCEPT operator on %d-element tables", nElem);
          881  +  speedtest1_prepare(
          882  +    "WITH RECURSIVE \n"
          883  +    "  t1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM t1 WHERE x<%d),\n"
          884  +    "  t2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM t2 WHERE y<%d)\n"
          885  +    "SELECT count(x), avg(x) FROM (\n"
          886  +    "  SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1\n"
          887  +    ");",
          888  +    nElem, nElem
          889  +  );
          890  +  speedtest1_run();
          891  +  speedtest1_end_test();
          892  +
          893  +}
   739    894   
   740    895   /*
   741    896   ** A testset used for debugging speedtest1 itself.
   742    897   */
   743    898   void testset_debug1(void){
   744    899     unsigned i, n;
   745    900     unsigned x1, x2;
................................................................................
   941   1096     }
   942   1097   
   943   1098     if( g.bExplain ) printf(".explain\n.echo on\n");
   944   1099     if( strcmp(zTSet,"main")==0 ){
   945   1100       testset_main();
   946   1101     }else if( strcmp(zTSet,"debug1")==0 ){
   947   1102       testset_debug1();
         1103  +  }else if( strcmp(zTSet,"cte")==0 ){
         1104  +    testset_cte();
   948   1105     }else{
   949   1106       fatal_error("unknown testset: \"%s\"\n", zTSet);
   950   1107     }
   951   1108     speedtest1_final();
   952   1109   
   953   1110     /* Database connection statistics printed after both prepared statements
   954   1111     ** have been finalized */

Added test/tkt-4c86b126f2.test.

            1  +# 2014-02-11
            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  +# This file implements regression tests for SQLite library. Specifically,
           12  +# it tests that ticket [4c86b126f22ad548fee0125337bdc9366912d9ac].
           13  +#
           14  +# When SQLite is compiled using SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4,
           15  +# it gets the wrong answer...
           16  +#
           17  +# The problem was introduced in SQLite 3.8.1.
           18  +
           19  +set testdir [file dirname $argv0]
           20  +source $testdir/tester.tcl
           21  +
           22  +do_execsql_test tkt-4c86b126f2-1.1 {
           23  +  CREATE TABLE nodes(
           24  +     local_relpath  TEXT PRIMARY KEY,
           25  +     moved_to  TEXT
           26  +  );
           27  +  INSERT INTO nodes VALUES('A',NULL);
           28  +  INSERT INTO nodes VALUES('A/B',NULL);
           29  +  INSERT INTO nodes VALUES('',NULL);
           30  +  INSERT INTO nodes VALUES('A/B/C-move',NULL);
           31  +  INSERT INTO nodes VALUES('A/B/C','A/B/C-move');
           32  +  INSERT INTO nodes VALUES('A/B-move',NULL);
           33  +  INSERT INTO nodes VALUES('A/B-move/C-move',NULL);
           34  +  INSERT INTO nodes VALUES('A/B-move/C','x');
           35  +  SELECT local_relpath, moved_to
           36  +   FROM nodes
           37  +  WHERE (local_relpath = 'A/B' OR
           38  +           ((local_relpath > 'A/B/') AND (local_relpath < 'A/B0')))
           39  +    AND moved_to IS NOT NULL;
           40  +} {A/B/C A/B/C-move}
           41  +
           42  +do_execsql_test tkt-4c86b126f2-2.1 {
           43  +  CREATE TABLE t1(x TEXT UNIQUE, y TEXT UNIQUE, z);
           44  +  INSERT INTO t1 VALUES('ghi','jkl','y');
           45  +  SELECT * FROM t1 WHERE (x='ghi' OR y='jkl') AND z IS NOT NULL;
           46  +} {ghi jkl y}
           47  +
           48  +
           49  +finish_test

Changes to test/where8.test.

   206    206     execsql_status {
   207    207       SELECT a, d FROM t1, t2 WHERE (a = 2 OR a = 3) AND d = a
   208    208     }
   209    209   } {2 2 3 3 0 0}
   210    210   
   211    211   do_test where8-3.5 {
   212    212     execsql_status {
   213         -    SELECT a, d FROM t1, t2 WHERE (a = 2 OR a = 3) AND (d = a OR e = 'sixteen')
          213  +    SELECT a, d FROM t1, t2 WHERE (a = 2 OR a = 3) AND (d = +a OR e = 'sixteen')
   214    214        ORDER BY +a, +d;
   215    215     }
   216    216   } {2 2 2 4 3 3 3 4 0 1}
   217    217   
   218    218   do_test where8-3.6 {
   219    219     # The first part of the WHERE clause in this query, (a=2 OR a=3) is
   220    220     # transformed into "a IN (2, 3)". This is why the sort is required.
   221    221     #
   222    222     execsql_status {
   223    223       SELECT a, d 
   224    224       FROM t1, t2 
   225         -    WHERE (a = 2 OR a = 3) AND (d = a OR e = 'sixteen')
          225  +    WHERE (a = 2 OR a = 3) AND (d = +a OR e = 'sixteen')
   226    226       ORDER BY t1.rowid
   227    227     }
   228    228   } {2 2 2 4 3 3 3 4 0 1}
   229    229   do_test where8-3.7 {
   230    230     execsql_status {
   231    231       SELECT a, d 
   232    232       FROM t1, t2 

Changes to test/with1.test.

   812    812   .........Jim
   813    813   .........Kate
   814    814   .........Lanny
   815    815   .........Mary
   816    816   .........Noland
   817    817   .........Olivia}}
   818    818   
   819         -finish_test
          819  +#--------------------------------------------------------------------------
          820  +# Ticket [31a19d11b97088296ac104aaff113a9790394927] (2014-02-09)
          821  +# Name resolution issue with compound SELECTs and Common Table Expressions 
          822  +#
          823  +do_execsql_test 12.1 {
          824  +WITH RECURSIVE
          825  +  t1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM t1 WHERE x<20),
          826  +  t2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM t2 WHERE y<20)
          827  +SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1;
          828  +} {2 4 8 10 14 16 20}
          829  +
   820    830   
          831  +finish_test