/ Check-in [9b43e559]
Login

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

Overview
Comment:Bring in all the latest trunk changes, including the Common Table Expressions implementation.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 9b43e559195680e558264c4c00d34dc9cf9d9146
User & Date: drh 2014-01-24 14:05:18
Context
2014-01-28
18:06
Bring in the latest updates from trunk. check-in: 7b5f3773 user: drh tags: sessions
2014-01-24
14:05
Bring in all the latest trunk changes, including the Common Table Expressions implementation. check-in: 9b43e559 user: drh tags: sessions
11:16
Add test cases showing the use of ORDER BY on a recursive query to control depth-first versus breath-first search of a tree. check-in: 83b0b291 user: drh tags: trunk
2013-12-24
12:09
Merge the latest trunk changes into the sessions branch. check-in: cfd110bf user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to addopcodes.awk.

    26     26     printf "#define TK_%-29s %4d\n", "UNCLOSED_STRING", ++max
    27     27     printf "#define TK_%-29s %4d\n", "FUNCTION",        ++max
    28     28     printf "#define TK_%-29s %4d\n", "COLUMN",          ++max
    29     29     printf "#define TK_%-29s %4d\n", "AGG_FUNCTION",    ++max
    30     30     printf "#define TK_%-29s %4d\n", "AGG_COLUMN",      ++max
    31     31     printf "#define TK_%-29s %4d\n", "UMINUS",          ++max
    32     32     printf "#define TK_%-29s %4d\n", "UPLUS",           ++max
           33  +  printf "#define TK_%-29s %4d\n", "REGISTER",        ++max
    33     34   }

Changes to ext/fts3/fts3.c.

  1467   1467     int argc,                       /* Number of elements in argv array */
  1468   1468     const char * const *argv,       /* xCreate/xConnect argument array */
  1469   1469     sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
  1470   1470     char **pzErr                    /* OUT: sqlite3_malloc'd error message */
  1471   1471   ){
  1472   1472     return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
  1473   1473   }
         1474  +
         1475  +/*
         1476  +** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
         1477  +** extension is currently being used by a version of SQLite too old to
         1478  +** support estimatedRows. In that case this function is a no-op.
         1479  +*/
         1480  +static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
         1481  +#if SQLITE_VERSION_NUMBER>=3008002
         1482  +  if( sqlite3_libversion_number()>=3008002 ){
         1483  +    pIdxInfo->estimatedRows = nRow;
         1484  +  }
         1485  +#endif
         1486  +}
  1474   1487   
  1475   1488   /* 
  1476   1489   ** Implementation of the xBestIndex method for FTS3 tables. There
  1477   1490   ** are three possible strategies, in order of preference:
  1478   1491   **
  1479   1492   **   1. Direct lookup by rowid or docid. 
  1480   1493   **   2. Full-text search using a MATCH operator on a non-docid column.
................................................................................
  1495   1508     ** strategy is possible.
  1496   1509     */
  1497   1510     pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
  1498   1511     pInfo->estimatedCost = 5000000;
  1499   1512     for(i=0; i<pInfo->nConstraint; i++){
  1500   1513       int bDocid;                 /* True if this constraint is on docid */
  1501   1514       struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
  1502         -    if( pCons->usable==0 ) continue;
         1515  +    if( pCons->usable==0 ){
         1516  +      if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
         1517  +        /* There exists an unusable MATCH constraint. This means that if
         1518  +        ** the planner does elect to use the results of this call as part
         1519  +        ** of the overall query plan the user will see an "unable to use
         1520  +        ** function MATCH in the requested context" error. To discourage
         1521  +        ** this, return a very high cost here.  */
         1522  +        pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
         1523  +        pInfo->estimatedCost = 1e50;
         1524  +        fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
         1525  +        return SQLITE_OK;
         1526  +      }
         1527  +      continue;
         1528  +    }
  1503   1529   
  1504   1530       bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
  1505   1531   
  1506   1532       /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
  1507   1533       if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
  1508   1534         pInfo->idxNum = FTS3_DOCID_SEARCH;
  1509   1535         pInfo->estimatedCost = 1.0;

Changes to ext/misc/spellfix.c.

  2047   2047   **
  2048   2048   **   (A)    word MATCH $str
  2049   2049   **   (B)    langid == $langid
  2050   2050   **   (C)    top = $top
  2051   2051   **   (D)    scope = $scope
  2052   2052   **   (E)    distance < $distance
  2053   2053   **   (F)    distance <= $distance
         2054  +**   (G)    rowid = $rowid
  2054   2055   **
  2055   2056   ** The plan number is a bit mask formed with these bits:
  2056   2057   **
  2057   2058   **   0x01   (A) is found
  2058   2059   **   0x02   (B) is found
  2059   2060   **   0x04   (C) is found
  2060   2061   **   0x08   (D) is found
  2061   2062   **   0x10   (E) is found
  2062   2063   **   0x20   (F) is found
         2064  +**   0x40   (G) is found
  2063   2065   **
  2064         -** filter.argv[*] values contains $str, $langid, $top, and $scope,
         2066  +** filter.argv[*] values contains $str, $langid, $top, $scope and $rowid
  2065   2067   ** if specified and in that order.
  2066   2068   */
  2067   2069   static int spellfix1BestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
  2068   2070     int iPlan = 0;
  2069   2071     int iLangTerm = -1;
  2070   2072     int iTopTerm = -1;
  2071   2073     int iScopeTerm = -1;
  2072   2074     int iDistTerm = -1;
         2075  +  int iRowidTerm = -1;
  2073   2076     int i;
  2074   2077     const struct sqlite3_index_constraint *pConstraint;
  2075   2078     pConstraint = pIdxInfo->aConstraint;
  2076   2079     for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
  2077   2080       if( pConstraint->usable==0 ) continue;
  2078   2081   
  2079   2082       /* Terms of the form:  word MATCH $str */
................................................................................
  2118   2121        && pConstraint->iColumn==SPELLFIX_COL_DISTANCE
  2119   2122        && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
  2120   2123             || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE)
  2121   2124       ){
  2122   2125         iPlan |= pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ? 16 : 32;
  2123   2126         iDistTerm = i;
  2124   2127       }
         2128  +
         2129  +    /* Terms of the form:  distance < $dist or distance <= $dist */
         2130  +    if( (iPlan & 64)==0
         2131  +     && pConstraint->iColumn<0
         2132  +     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
         2133  +    ){
         2134  +      iPlan |= 64;
         2135  +      iRowidTerm = i;
         2136  +    }
  2125   2137     }
  2126   2138     if( iPlan&1 ){
  2127   2139       int idx = 2;
  2128   2140       pIdxInfo->idxNum = iPlan;
  2129   2141       if( pIdxInfo->nOrderBy==1
  2130   2142        && pIdxInfo->aOrderBy[0].iColumn==SPELLFIX_COL_SCORE
  2131   2143        && pIdxInfo->aOrderBy[0].desc==0
................................................................................
  2145   2157         pIdxInfo->aConstraintUsage[iScopeTerm].omit = 1;
  2146   2158       }
  2147   2159       if( iPlan&(16|32) ){
  2148   2160         pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = idx++;
  2149   2161         pIdxInfo->aConstraintUsage[iDistTerm].omit = 1;
  2150   2162       }
  2151   2163       pIdxInfo->estimatedCost = 1e5;
         2164  +  }else if( (iPlan & 64) ){
         2165  +    pIdxInfo->idxNum = 64;
         2166  +    pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
         2167  +    pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
         2168  +    pIdxInfo->estimatedCost = 5;
  2152   2169     }else{
  2153   2170       pIdxInfo->idxNum = 0;
  2154   2171       pIdxInfo->estimatedCost = 1e50;
  2155   2172     }
  2156   2173     return SQLITE_OK;
  2157   2174   }
  2158   2175   
................................................................................
  2461   2478   */
  2462   2479   static int spellfix1FilterForFullScan(
  2463   2480     spellfix1_cursor *pCur,
  2464   2481     int idxNum,
  2465   2482     int argc,
  2466   2483     sqlite3_value **argv
  2467   2484   ){
  2468         -  int rc;
         2485  +  int rc = SQLITE_OK;
  2469   2486     char *zSql;
  2470   2487     spellfix1_vtab *pVTab = pCur->pVTab;
  2471   2488     spellfix1ResetCursor(pCur);
         2489  +  assert( idxNum==0 || idxNum==64 );
  2472   2490     zSql = sqlite3_mprintf(
  2473         -     "SELECT word, rank, NULL, langid, id FROM \"%w\".\"%w_vocab\"",
  2474         -     pVTab->zDbName, pVTab->zTableName);
         2491  +     "SELECT word, rank, NULL, langid, id FROM \"%w\".\"%w_vocab\"%s",
         2492  +     pVTab->zDbName, pVTab->zTableName,
         2493  +     ((idxNum & 64) ? " WHERE rowid=?" : "")
         2494  +  );
  2475   2495     if( zSql==0 ) return SQLITE_NOMEM;
  2476   2496     rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pFullScan, 0);
  2477   2497     sqlite3_free(zSql);
         2498  +  if( rc==SQLITE_OK && (idxNum & 64) ){
         2499  +    assert( argc==1 );
         2500  +    rc = sqlite3_bind_value(pCur->pFullScan, 1, argv[0]);
         2501  +  }
  2478   2502     pCur->nRow = pCur->iRow = 0;
  2479   2503     if( rc==SQLITE_OK ){
  2480   2504       rc = sqlite3_step(pCur->pFullScan);
  2481   2505       if( rc==SQLITE_ROW ){ pCur->iRow = -1; rc = SQLITE_OK; }
  2482   2506       if( rc==SQLITE_DONE ){ rc = SQLITE_OK; }
  2483   2507     }else{
  2484   2508       pCur->iRow = 0;

Changes to src/btree.c.

  3750   3750   */
  3751   3751   #ifndef NDEBUG
  3752   3752     static void assertCellInfo(BtCursor *pCur){
  3753   3753       CellInfo info;
  3754   3754       int iPage = pCur->iPage;
  3755   3755       memset(&info, 0, sizeof(info));
  3756   3756       btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
  3757         -    assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
         3757  +    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
  3758   3758     }
  3759   3759   #else
  3760   3760     #define assertCellInfo(x)
  3761   3761   #endif
  3762   3762   #ifdef _MSC_VER
  3763   3763     /* Use a real function in MSVC to work around bugs in that compiler. */
  3764   3764     static void getCellInfo(BtCursor *pCur){
................................................................................
  4386   4386       rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
  4387   4387                           pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
  4388   4388       if( rc!=SQLITE_OK ){
  4389   4389         pCur->eState = CURSOR_INVALID;
  4390   4390         return rc;
  4391   4391       }
  4392   4392       pCur->iPage = 0;
  4393         -
  4394         -    /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
  4395         -    ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
  4396         -    ** NULL, the caller expects a table b-tree. If this is not the case,
  4397         -    ** return an SQLITE_CORRUPT error.  */
  4398         -    assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
  4399         -    if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
  4400         -      return SQLITE_CORRUPT_BKPT;
  4401         -    }
  4402   4393     }
  4403         -
  4404         -  /* Assert that the root page is of the correct type. This must be the
  4405         -  ** case as the call to this function that loaded the root-page (either
  4406         -  ** this call or a previous invocation) would have detected corruption 
  4407         -  ** if the assumption were not true, and it is not possible for the flags 
  4408         -  ** byte to have been modified while this cursor is holding a reference
  4409         -  ** to the page.  */
  4410   4394     pRoot = pCur->apPage[0];
  4411   4395     assert( pRoot->pgno==pCur->pgnoRoot );
  4412         -  assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
         4396  +
         4397  +  /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
         4398  +  ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
         4399  +  ** NULL, the caller expects a table b-tree. If this is not the case,
         4400  +  ** return an SQLITE_CORRUPT error. 
         4401  +  **
         4402  +  ** Earlier versions of SQLite assumed that this test could not fail
         4403  +  ** if the root page was already loaded when this function was called (i.e.
         4404  +  ** if pCur->iPage>=0). But this is not so if the database is corrupted 
         4405  +  ** in such a way that page pRoot is linked into a second b-tree table 
         4406  +  ** (or the freelist).  */
         4407  +  assert( pRoot->intKey==1 || pRoot->intKey==0 );
         4408  +  if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
         4409  +    return SQLITE_CORRUPT_BKPT;
         4410  +  }
  4413   4411   
  4414   4412     pCur->aiIdx[0] = 0;
  4415   4413     pCur->info.nSize = 0;
  4416   4414     pCur->atLast = 0;
  4417   4415     pCur->validNKey = 0;
  4418   4416   
  4419   4417     if( pRoot->nCell>0 ){
................................................................................
  5247   5245   
  5248   5246   end_allocate_page:
  5249   5247     releasePage(pTrunk);
  5250   5248     releasePage(pPrevTrunk);
  5251   5249     if( rc==SQLITE_OK ){
  5252   5250       if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
  5253   5251         releasePage(*ppPage);
         5252  +      *ppPage = 0;
  5254   5253         return SQLITE_CORRUPT_BKPT;
  5255   5254       }
  5256   5255       (*ppPage)->isInit = 0;
  5257   5256     }else{
  5258   5257       *ppPage = 0;
  5259   5258     }
  5260   5259     assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
................................................................................
  7346   7345     int freePageFlag,        /* Deallocate page if true */
  7347   7346     int *pnChange            /* Add number of Cells freed to this counter */
  7348   7347   ){
  7349   7348     MemPage *pPage;
  7350   7349     int rc;
  7351   7350     unsigned char *pCell;
  7352   7351     int i;
         7352  +  int hdr;
  7353   7353   
  7354   7354     assert( sqlite3_mutex_held(pBt->mutex) );
  7355   7355     if( pgno>btreePagecount(pBt) ){
  7356   7356       return SQLITE_CORRUPT_BKPT;
  7357   7357     }
  7358   7358   
  7359   7359     rc = getAndInitPage(pBt, pgno, &pPage, 0);
  7360   7360     if( rc ) return rc;
         7361  +  hdr = pPage->hdrOffset;
  7361   7362     for(i=0; i<pPage->nCell; i++){
  7362   7363       pCell = findCell(pPage, i);
  7363   7364       if( !pPage->leaf ){
  7364   7365         rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
  7365   7366         if( rc ) goto cleardatabasepage_out;
  7366   7367       }
  7367   7368       rc = clearCell(pPage, pCell);
  7368   7369       if( rc ) goto cleardatabasepage_out;
  7369   7370     }
  7370   7371     if( !pPage->leaf ){
  7371         -    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
         7372  +    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
  7372   7373       if( rc ) goto cleardatabasepage_out;
  7373   7374     }else if( pnChange ){
  7374   7375       assert( pPage->intKey );
  7375   7376       *pnChange += pPage->nCell;
  7376   7377     }
  7377   7378     if( freePageFlag ){
  7378   7379       freePage(pPage, &rc);
  7379   7380     }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
  7380         -    zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
         7381  +    zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
  7381   7382     }
  7382   7383   
  7383   7384   cleardatabasepage_out:
  7384   7385     releasePage(pPage);
  7385   7386     return rc;
  7386   7387   }
  7387   7388   

Changes to src/build.c.

   136    136     /* Begin by generating some termination code at the end of the
   137    137     ** vdbe program
   138    138     */
   139    139     v = sqlite3GetVdbe(pParse);
   140    140     assert( !pParse->isMultiWrite 
   141    141          || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
   142    142     if( v ){
          143  +    while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
   143    144       sqlite3VdbeAddOp0(v, OP_Halt);
   144    145   
   145    146       /* The cookie mask contains one bit for each database file open.
   146    147       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
   147    148       ** set for each database that is used.  Generate code to start a
   148    149       ** transaction on each used database and to verify the schema cookie
   149    150       ** on each used database.
................................................................................
  1447   1448     unsigned char *zIdent = (unsigned char*)zSignedIdent;
  1448   1449     int i, j, needQuote;
  1449   1450     i = *pIdx;
  1450   1451   
  1451   1452     for(j=0; zIdent[j]; j++){
  1452   1453       if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
  1453   1454     }
  1454         -  needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
  1455         -  if( !needQuote ){
  1456         -    needQuote = zIdent[j];
  1457         -  }
         1455  +  needQuote = sqlite3Isdigit(zIdent[0])
         1456  +            || sqlite3KeywordCode(zIdent, j)!=TK_ID
         1457  +            || zIdent[j]!=0
         1458  +            || j==0;
  1458   1459   
  1459   1460     if( needQuote ) z[i++] = '"';
  1460   1461     for(j=0; zIdent[j]; j++){
  1461   1462       z[i++] = zIdent[j];
  1462   1463       if( zIdent[j]=='"' ) z[i++] = '"';
  1463   1464     }
  1464   1465     if( needQuote ) z[i++] = '"';
................................................................................
  2673   2674   
  2674   2675     /* Open the table. Loop through all rows of the table, inserting index
  2675   2676     ** records into the sorter. */
  2676   2677     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2677   2678     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
  2678   2679     regRecord = sqlite3GetTempReg(pParse);
  2679   2680   
  2680         -  sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 0, &iPartIdxLabel);
         2681  +  sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
  2681   2682     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
  2682   2683     sqlite3VdbeResolveLabel(v, iPartIdxLabel);
  2683   2684     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
  2684   2685     sqlite3VdbeJumpHere(v, addr1);
  2685   2686     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
  2686   2687     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2687   2688                       (char *)pKey, P4_KEYINFO);
................................................................................
  4193   4194         }else{
  4194   4195           pIdx->pKeyInfo = pKey;
  4195   4196         }
  4196   4197       }
  4197   4198     }
  4198   4199     return sqlite3KeyInfoRef(pIdx->pKeyInfo);
  4199   4200   }
         4201  +
         4202  +#ifndef SQLITE_OMIT_CTE
         4203  +/* 
         4204  +** This routine is invoked once per CTE by the parser while parsing a 
         4205  +** WITH clause. 
         4206  +*/
         4207  +With *sqlite3WithAdd(
         4208  +  Parse *pParse,          /* Parsing context */
         4209  +  With *pWith,            /* Existing WITH clause, or NULL */
         4210  +  Token *pName,           /* Name of the common-table */
         4211  +  ExprList *pArglist,     /* Optional column name list for the table */
         4212  +  Select *pQuery          /* Query used to initialize the table */
         4213  +){
         4214  +  sqlite3 *db = pParse->db;
         4215  +  With *pNew;
         4216  +  char *zName;
         4217  +
         4218  +  /* Check that the CTE name is unique within this WITH clause. If
         4219  +  ** not, store an error in the Parse structure. */
         4220  +  zName = sqlite3NameFromToken(pParse->db, pName);
         4221  +  if( zName && pWith ){
         4222  +    int i;
         4223  +    for(i=0; i<pWith->nCte; i++){
         4224  +      if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
         4225  +        sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
         4226  +      }
         4227  +    }
         4228  +  }
         4229  +
         4230  +  if( pWith ){
         4231  +    int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
         4232  +    pNew = sqlite3DbRealloc(db, pWith, nByte);
         4233  +  }else{
         4234  +    pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
         4235  +  }
         4236  +  assert( zName!=0 || pNew==0 );
         4237  +  assert( db->mallocFailed==0 || pNew==0 );
         4238  +
         4239  +  if( pNew==0 ){
         4240  +    sqlite3ExprListDelete(db, pArglist);
         4241  +    sqlite3SelectDelete(db, pQuery);
         4242  +    sqlite3DbFree(db, zName);
         4243  +    pNew = pWith;
         4244  +  }else{
         4245  +    pNew->a[pNew->nCte].pSelect = pQuery;
         4246  +    pNew->a[pNew->nCte].pCols = pArglist;
         4247  +    pNew->a[pNew->nCte].zName = zName;
         4248  +    pNew->a[pNew->nCte].zErr = 0;
         4249  +    pNew->nCte++;
         4250  +  }
         4251  +
         4252  +  return pNew;
         4253  +}
         4254  +
         4255  +/*
         4256  +** Free the contents of the With object passed as the second argument.
         4257  +*/
         4258  +void sqlite3WithDelete(sqlite3 *db, With *pWith){
         4259  +  if( pWith ){
         4260  +    int i;
         4261  +    for(i=0; i<pWith->nCte; i++){
         4262  +      struct Cte *pCte = &pWith->a[i];
         4263  +      sqlite3ExprListDelete(db, pCte->pCols);
         4264  +      sqlite3SelectDelete(db, pCte->pSelect);
         4265  +      sqlite3DbFree(db, pCte->zName);
         4266  +    }
         4267  +    sqlite3DbFree(db, pWith);
         4268  +  }
         4269  +}
         4270  +#endif /* !defined(SQLITE_OMIT_CTE) */

Changes to src/delete.c.

   724    724     Parse *pParse,     /* Parsing and code generating context */
   725    725     Table *pTab,       /* Table containing the row to be deleted */
   726    726     int iDataCur,      /* Cursor of table holding data. */
   727    727     int iIdxCur,       /* First index cursor */
   728    728     int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
   729    729   ){
   730    730     int i;             /* Index loop counter */
   731         -  int r1;            /* Register holding an index key */
          731  +  int r1 = -1;       /* Register holding an index key */
   732    732     int iPartIdxLabel; /* Jump destination for skipping partial index entries */
   733    733     Index *pIdx;       /* Current index */
          734  +  Index *pPrior = 0; /* Prior index */
   734    735     Vdbe *v;           /* The prepared statement under construction */
   735    736     Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
   736    737   
   737    738     v = pParse->pVdbe;
   738    739     pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
   739    740     for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   740    741       assert( iIdxCur+i!=iDataCur || pPk==pIdx );
   741    742       if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
   742    743       if( pIdx==pPk ) continue;
   743    744       VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
   744         -    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1, &iPartIdxLabel);
          745  +    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
          746  +                                 &iPartIdxLabel, pPrior, r1);
   745    747       sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
   746    748                         pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
   747    749       sqlite3VdbeResolveLabel(v, iPartIdxLabel);
          750  +    pPrior = pIdx;
   748    751     }
   749    752   }
   750    753   
   751    754   /*
   752    755   ** Generate code that will assemble an index key and stores it in register
   753    756   ** regOut.  The key with be for index pIdx which is an index on pTab.
   754    757   ** iCur is the index of a cursor open on the pTab table and pointing to
................................................................................
   762    765   **
   763    766   ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
   764    767   ** to that label if pIdx is a partial index that should be skipped.
   765    768   ** A partial index should be skipped if its WHERE clause evaluates
   766    769   ** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
   767    770   ** will be set to zero which is an empty label that is ignored by
   768    771   ** sqlite3VdbeResolveLabel().
          772  +**
          773  +** The pPrior and regPrior parameters are used to implement a cache to
          774  +** avoid unnecessary register loads.  If pPrior is not NULL, then it is
          775  +** a pointer to a different index for which an index key has just been
          776  +** computed into register regPrior.  If the current pIdx index is generating
          777  +** its key into the same sequence of registers and if pPrior and pIdx share
          778  +** a column in common, then the register corresponding to that column already
          779  +** holds the correct value and the loading of that register is skipped.
          780  +** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK 
          781  +** on a table with multiple indices, and especially with the ROWID or
          782  +** PRIMARY KEY columns of the index.
   769    783   */
   770    784   int sqlite3GenerateIndexKey(
   771    785     Parse *pParse,       /* Parsing context */
   772    786     Index *pIdx,         /* The index for which to generate a key */
   773    787     int iDataCur,        /* Cursor number from which to take column data */
   774    788     int regOut,          /* Put the new key into this register if not 0 */
   775    789     int prefixOnly,      /* Compute only a unique prefix of the key */
   776         -  int *piPartIdxLabel  /* OUT: Jump to this label to skip partial index */
          790  +  int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
          791  +  Index *pPrior,       /* Previously generated index key */
          792  +  int regPrior         /* Register holding previous generated key */
   777    793   ){
   778    794     Vdbe *v = pParse->pVdbe;
   779    795     int j;
   780    796     Table *pTab = pIdx->pTable;
   781    797     int regBase;
   782    798     int nCol;
   783    799   
................................................................................
   789    805                            SQLITE_JUMPIFNULL);
   790    806       }else{
   791    807         *piPartIdxLabel = 0;
   792    808       }
   793    809     }
   794    810     nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
   795    811     regBase = sqlite3GetTempRange(pParse, nCol);
          812  +  if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
   796    813     for(j=0; j<nCol; j++){
          814  +    if( pPrior && pPrior->aiColumn[j]==pIdx->aiColumn[j] ) continue;
   797    815       sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pIdx->aiColumn[j],
   798    816                                       regBase+j);
          817  +    /* If the column affinity is REAL but the number is an integer, then it
          818  +    ** might be stored in the table as an integer (using a compact
          819  +    ** representation) then converted to REAL by an OP_RealAffinity opcode.
          820  +    ** But we are getting ready to store this value back into an index, where
          821  +    ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
          822  +    ** opcode if it is present */
          823  +    sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
   799    824     }
   800    825     if( regOut ){
   801         -    const char *zAff;
   802         -    if( pTab->pSelect
   803         -     || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
   804         -    ){
   805         -      zAff = 0;
   806         -    }else{
   807         -      zAff = sqlite3IndexAffinityStr(v, pIdx);
   808         -    }
   809    826       sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
   810         -    sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
   811    827     }
   812    828     sqlite3ReleaseTempRange(pParse, regBase, nCol);
   813    829     return regBase;
   814    830   }

Changes to src/expr.c.

   519    519     if( p ) {
   520    520       sqlite3ExprCheckHeight(pParse, p->nHeight);
   521    521     }
   522    522     return p;
   523    523   }
   524    524   
   525    525   /*
   526         -** Return 1 if an expression must be FALSE in all cases and 0 if the
   527         -** expression might be true.  This is an optimization.  If is OK to
   528         -** return 0 here even if the expression really is always false (a 
   529         -** false negative).  But it is a bug to return 1 if the expression
   530         -** might be true in some rare circumstances (a false positive.)
          526  +** If the expression is always either TRUE or FALSE (respectively),
          527  +** then return 1.  If one cannot determine the truth value of the
          528  +** expression at compile-time return 0.
          529  +**
          530  +** This is an optimization.  If is OK to return 0 here even if
          531  +** the expression really is always false or false (a false negative).
          532  +** But it is a bug to return 1 if the expression might have different
          533  +** boolean values in different circumstances (a false positive.)
   531    534   **
   532    535   ** Note that if the expression is part of conditional for a
   533    536   ** LEFT JOIN, then we cannot determine at compile-time whether or not
   534    537   ** is it true or false, so always return 0.
   535    538   */
          539  +static int exprAlwaysTrue(Expr *p){
          540  +  int v = 0;
          541  +  if( ExprHasProperty(p, EP_FromJoin) ) return 0;
          542  +  if( !sqlite3ExprIsInteger(p, &v) ) return 0;
          543  +  return v!=0;
          544  +}
   536    545   static int exprAlwaysFalse(Expr *p){
   537    546     int v = 0;
   538    547     if( ExprHasProperty(p, EP_FromJoin) ) return 0;
   539    548     if( !sqlite3ExprIsInteger(p, &v) ) return 0;
   540    549     return v==0;
   541    550   }
   542    551   
................................................................................
   882    891         }
   883    892   
   884    893       }
   885    894     }
   886    895     return pNew;
   887    896   }
   888    897   
          898  +/*
          899  +** Create and return a deep copy of the object passed as the second 
          900  +** argument. If an OOM condition is encountered, NULL is returned
          901  +** and the db->mallocFailed flag set.
          902  +*/
          903  +#ifndef SQLITE_OMIT_CTE
          904  +static With *withDup(sqlite3 *db, With *p){
          905  +  With *pRet = 0;
          906  +  if( p ){
          907  +    int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
          908  +    pRet = sqlite3DbMallocZero(db, nByte);
          909  +    if( pRet ){
          910  +      int i;
          911  +      pRet->nCte = p->nCte;
          912  +      for(i=0; i<p->nCte; i++){
          913  +        pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
          914  +        pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
          915  +        pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
          916  +      }
          917  +    }
          918  +  }
          919  +  return pRet;
          920  +}
          921  +#else
          922  +# define withDup(x,y) 0
          923  +#endif
          924  +
   889    925   /*
   890    926   ** The following group of routines make deep copies of expressions,
   891    927   ** expression lists, ID lists, and select statements.  The copies can
   892    928   ** be deleted (by being passed to their respective ...Delete() routines)
   893    929   ** without effecting the originals.
   894    930   **
   895    931   ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
................................................................................
   962    998       pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
   963    999       pNewItem->jointype = pOldItem->jointype;
   964   1000       pNewItem->iCursor = pOldItem->iCursor;
   965   1001       pNewItem->addrFillSub = pOldItem->addrFillSub;
   966   1002       pNewItem->regReturn = pOldItem->regReturn;
   967   1003       pNewItem->isCorrelated = pOldItem->isCorrelated;
   968   1004       pNewItem->viaCoroutine = pOldItem->viaCoroutine;
         1005  +    pNewItem->isRecursive = pOldItem->isRecursive;
   969   1006       pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
   970   1007       pNewItem->notIndexed = pOldItem->notIndexed;
   971   1008       pNewItem->pIndex = pOldItem->pIndex;
   972   1009       pTab = pNewItem->pTab = pOldItem->pTab;
   973   1010       if( pTab ){
   974   1011         pTab->nRef++;
   975   1012       }
................................................................................
  1023   1060     pNew->iLimit = 0;
  1024   1061     pNew->iOffset = 0;
  1025   1062     pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
  1026   1063     pNew->pRightmost = 0;
  1027   1064     pNew->addrOpenEphm[0] = -1;
  1028   1065     pNew->addrOpenEphm[1] = -1;
  1029   1066     pNew->addrOpenEphm[2] = -1;
         1067  +  pNew->pWith = withDup(db, p->pWith);
  1030   1068     return pNew;
  1031   1069   }
  1032   1070   #else
  1033   1071   Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
  1034   1072     assert( p==0 );
  1035   1073     return 0;
  1036   1074   }
................................................................................
  3525   3563     if( NEVER(v==0) )     return;  /* Existence of VDBE checked by caller */
  3526   3564     if( NEVER(pExpr==0) ) return;  /* No way this can happen */
  3527   3565     op = pExpr->op;
  3528   3566     switch( op ){
  3529   3567       case TK_AND: {
  3530   3568         int d2 = sqlite3VdbeMakeLabel(v);
  3531   3569         testcase( jumpIfNull==0 );
  3532         -      sqlite3ExprCachePush(pParse);
  3533   3570         sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
         3571  +      sqlite3ExprCachePush(pParse);
  3534   3572         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  3535   3573         sqlite3VdbeResolveLabel(v, d2);
  3536   3574         sqlite3ExprCachePop(pParse, 1);
  3537   3575         break;
  3538   3576       }
  3539   3577       case TK_OR: {
  3540   3578         testcase( jumpIfNull==0 );
  3541   3579         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
         3580  +      sqlite3ExprCachePush(pParse);
  3542   3581         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
         3582  +      sqlite3ExprCachePop(pParse, 1);
  3543   3583         break;
  3544   3584       }
  3545   3585       case TK_NOT: {
  3546   3586         testcase( jumpIfNull==0 );
  3547   3587         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  3548   3588         break;
  3549   3589       }
................................................................................
  3610   3650         sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
  3611   3651         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
  3612   3652         sqlite3VdbeResolveLabel(v, destIfFalse);
  3613   3653         break;
  3614   3654       }
  3615   3655   #endif
  3616   3656       default: {
  3617         -      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
  3618         -      sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
  3619         -      testcase( regFree1==0 );
  3620         -      testcase( jumpIfNull==0 );
         3657  +      if( exprAlwaysTrue(pExpr) ){
         3658  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
         3659  +      }else if( exprAlwaysFalse(pExpr) ){
         3660  +        /* No-op */
         3661  +      }else{
         3662  +        r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
         3663  +        sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
         3664  +        testcase( regFree1==0 );
         3665  +        testcase( jumpIfNull==0 );
         3666  +      }
  3621   3667         break;
  3622   3668       }
  3623   3669     }
  3624   3670     sqlite3ReleaseTempReg(pParse, regFree1);
  3625   3671     sqlite3ReleaseTempReg(pParse, regFree2);  
  3626   3672   }
  3627   3673   
................................................................................
  3676   3722     assert( pExpr->op!=TK_GT || op==OP_Le );
  3677   3723     assert( pExpr->op!=TK_GE || op==OP_Lt );
  3678   3724   
  3679   3725     switch( pExpr->op ){
  3680   3726       case TK_AND: {
  3681   3727         testcase( jumpIfNull==0 );
  3682   3728         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
         3729  +      sqlite3ExprCachePush(pParse);
  3683   3730         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
         3731  +      sqlite3ExprCachePop(pParse, 1);
  3684   3732         break;
  3685   3733       }
  3686   3734       case TK_OR: {
  3687   3735         int d2 = sqlite3VdbeMakeLabel(v);
  3688   3736         testcase( jumpIfNull==0 );
  3689         -      sqlite3ExprCachePush(pParse);
  3690   3737         sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
         3738  +      sqlite3ExprCachePush(pParse);
  3691   3739         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  3692   3740         sqlite3VdbeResolveLabel(v, d2);
  3693   3741         sqlite3ExprCachePop(pParse, 1);
  3694   3742         break;
  3695   3743       }
  3696   3744       case TK_NOT: {
  3697   3745         testcase( jumpIfNull==0 );
................................................................................
  3755   3803           sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
  3756   3804           sqlite3VdbeResolveLabel(v, destIfNull);
  3757   3805         }
  3758   3806         break;
  3759   3807       }
  3760   3808   #endif
  3761   3809       default: {
  3762         -      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
  3763         -      sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
  3764         -      testcase( regFree1==0 );
  3765         -      testcase( jumpIfNull==0 );
         3810  +      if( exprAlwaysFalse(pExpr) ){
         3811  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
         3812  +      }else if( exprAlwaysTrue(pExpr) ){
         3813  +        /* no-op */
         3814  +      }else{
         3815  +        r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
         3816  +        sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
         3817  +        testcase( regFree1==0 );
         3818  +        testcase( jumpIfNull==0 );
         3819  +      }
  3766   3820         break;
  3767   3821       }
  3768   3822     }
  3769   3823     sqlite3ReleaseTempReg(pParse, regFree1);
  3770   3824     sqlite3ReleaseTempReg(pParse, regFree2);
  3771   3825   }
  3772   3826   

Changes to src/insert.c.

   536    536   **           transfer values form intermediate table into <table>
   537    537   **         end loop
   538    538   **      D: cleanup
   539    539   */
   540    540   void sqlite3Insert(
   541    541     Parse *pParse,        /* Parser context */
   542    542     SrcList *pTabList,    /* Name of table into which we are inserting */
   543         -  ExprList *pList,      /* List of values to be inserted */
   544    543     Select *pSelect,      /* A SELECT statement to use as the data source */
   545    544     IdList *pColumn,      /* Column names corresponding to IDLIST. */
   546    545     int onError           /* How to handle constraint errors */
   547    546   ){
   548    547     sqlite3 *db;          /* The main database structure */
   549    548     Table *pTab;          /* The table to insert into.  aka TABLE */
   550    549     char *zTab;           /* Name of the table into which we are inserting */
................................................................................
   564    563     int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
   565    564     int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
   566    565     SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
   567    566     int iDb;              /* Index of database holding TABLE */
   568    567     Db *pDb;              /* The database containing table being inserted into */
   569    568     int appendFlag = 0;   /* True if the insert is likely to be an append */
   570    569     int withoutRowid;     /* 0 for normal table.  1 for WITHOUT ROWID table */
          570  +  ExprList *pList = 0;  /* List of VALUES() to be inserted  */
   571    571   
   572    572     /* Register allocations */
   573    573     int regFromSelect = 0;/* Base register for data coming from SELECT */
   574    574     int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
   575    575     int regRowCount = 0;  /* Memory cell used for the row counter */
   576    576     int regIns;           /* Block of regs holding rowid+data being inserted */
   577    577     int regRowid;         /* registers holding insert rowid */
................................................................................
   586    586   #endif
   587    587   
   588    588     db = pParse->db;
   589    589     memset(&dest, 0, sizeof(dest));
   590    590     if( pParse->nErr || db->mallocFailed ){
   591    591       goto insert_cleanup;
   592    592     }
          593  +
          594  +  /* If the Select object is really just a simple VALUES() list with a
          595  +  ** single row values (the common case) then keep that one row of values
          596  +  ** and go ahead and discard the Select object
          597  +  */
          598  +  if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
          599  +    pList = pSelect->pEList;
          600  +    pSelect->pEList = 0;
          601  +    sqlite3SelectDelete(db, pSelect);
          602  +    pSelect = 0;
          603  +  }
   593    604   
   594    605     /* Locate the table into which we will be inserting new information.
   595    606     */
   596    607     assert( pTabList->nSrc==1 );
   597    608     zTab = pTabList->a[0].zName;
   598    609     if( NEVER(zTab==0) ) goto insert_cleanup;
   599    610     pTab = sqlite3SrcListLookup(pParse, pTabList);
................................................................................
  1229   1240     int onError;         /* Conflict resolution strategy */
  1230   1241     int j1;              /* Addresss of jump instruction */
  1231   1242     int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
  1232   1243     int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
  1233   1244     int ipkTop = 0;      /* Top of the rowid change constraint check */
  1234   1245     int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
  1235   1246     u8 isUpdate;         /* True if this is an UPDATE operation */
         1247  +  int regRowid = -1;   /* Register holding ROWID value */
  1236   1248   
  1237   1249     isUpdate = regOldData!=0;
  1238   1250     db = pParse->db;
  1239   1251     v = sqlite3GetVdbe(pParse);
  1240   1252     assert( v!=0 );
  1241   1253     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
  1242   1254     nCol = pTab->nCol;
................................................................................
  1471   1483       ** the insert or update.  Store that record in the aRegIdx[ix] register
  1472   1484       */
  1473   1485       regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
  1474   1486       for(i=0; i<pIdx->nColumn; i++){
  1475   1487         int iField = pIdx->aiColumn[i];
  1476   1488         int x;
  1477   1489         if( iField<0 || iField==pTab->iPKey ){
         1490  +        if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
  1478   1491           x = regNewData;
         1492  +        regRowid =  pIdx->pPartIdxWhere ? -1 : regIdx+i;
  1479   1493         }else{
  1480   1494           x = iField + regNewData + 1;
  1481   1495         }
  1482   1496         sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
  1483   1497         VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
  1484   1498       }
  1485   1499       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
................................................................................
  1851   1865     Vdbe *v;                         /* The VDBE we are building */
  1852   1866     int regAutoinc;                  /* Memory register used by AUTOINC */
  1853   1867     int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
  1854   1868     int regData, regRowid;           /* Registers holding data and rowid */
  1855   1869   
  1856   1870     if( pSelect==0 ){
  1857   1871       return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
         1872  +  }
         1873  +  if( pParse->pWith || pSelect->pWith ){
         1874  +    /* Do not attempt to process this query if there are an WITH clauses
         1875  +    ** attached to it. Proceeding may generate a false "no such table: xxx"
         1876  +    ** error if pSelect reads from a CTE named "xxx".  */
         1877  +    return 0;
  1858   1878     }
  1859   1879     if( sqlite3TriggerList(pParse, pDest) ){
  1860   1880       return 0;   /* tab1 must not have triggers */
  1861   1881     }
  1862   1882   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1863   1883     if( pDest->tabFlags & TF_Virtual ){
  1864   1884       return 0;   /* tab1 must not be a virtual table */

Changes to src/main.c.

   131    131     /* If SQLite is already completely initialized, then this call
   132    132     ** to sqlite3_initialize() should be a no-op.  But the initialization
   133    133     ** must be complete.  So isInit must not be set until the very end
   134    134     ** of this routine.
   135    135     */
   136    136     if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
   137    137   
   138         -#ifdef SQLITE_ENABLE_SQLLOG
   139         -  {
   140         -    extern void sqlite3_init_sqllog(void);
   141         -    sqlite3_init_sqllog();
   142         -  }
   143         -#endif
   144         -
   145    138     /* Make sure the mutex subsystem is initialized.  If unable to 
   146    139     ** initialize the mutex subsystem, return early with the error.
   147    140     ** If the system is so sick that we are unable to allocate a mutex,
   148    141     ** there is not much SQLite is going to be able to do.
   149    142     **
   150    143     ** The mutex subsystem must take care of serializing its own
   151    144     ** initialization.
................................................................................
  3118   3111   
  3119   3112       /*
  3120   3113       ** Reset the PRNG back to its uninitialized state.  The next call
  3121   3114       ** to sqlite3_randomness() will reseed the PRNG using a single call
  3122   3115       ** to the xRandomness method of the default VFS.
  3123   3116       */
  3124   3117       case SQLITE_TESTCTRL_PRNG_RESET: {
  3125         -      sqlite3PrngResetState();
         3118  +      sqlite3_randomness(0,0);
  3126   3119         break;
  3127   3120       }
  3128   3121   
  3129   3122       /*
  3130   3123       **  sqlite3_test_control(BITVEC_TEST, size, program)
  3131   3124       **
  3132   3125       ** Run a test against a Bitvec object of size.  The program argument

Changes to src/os_unix.c.

   256    256     /* In test mode, increase the size of this structure a bit so that 
   257    257     ** it is larger than the struct CrashFile defined in test6.c.
   258    258     */
   259    259     char aPadding[32];
   260    260   #endif
   261    261   };
   262    262   
          263  +/* This variable holds the process id (pid) from when the xRandomness()
          264  +** method was called.  If xOpen() is called from a different process id,
          265  +** indicating that a fork() has occurred, the PRNG will be reset.
          266  +*/
          267  +static int randomnessPid = 0;
          268  +
   263    269   /*
   264    270   ** Allowed values for the unixFile.ctrlFlags bitmask:
   265    271   */
   266    272   #define UNIXFILE_EXCL        0x01     /* Connections from one process only */
   267    273   #define UNIXFILE_RDONLY      0x02     /* Connection is read only */
   268    274   #define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
   269    275   #ifndef SQLITE_DISABLE_DIRSYNC
................................................................................
  4838   4844   ** argument that was passed to the unixFetch() invocation. 
  4839   4845   **
  4840   4846   ** Or, if the third argument is NULL, then this function is being called 
  4841   4847   ** to inform the VFS layer that, according to POSIX, any existing mapping 
  4842   4848   ** may now be invalid and should be unmapped.
  4843   4849   */
  4844   4850   static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
         4851  +#if SQLITE_MAX_MMAP_SIZE>0
  4845   4852     unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
  4846   4853     UNUSED_PARAMETER(iOff);
  4847   4854   
  4848         -#if SQLITE_MAX_MMAP_SIZE>0
  4849   4855     /* If p==0 (unmap the entire file) then there must be no outstanding 
  4850   4856     ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
  4851   4857     ** then there must be at least one outstanding.  */
  4852   4858     assert( (p==0)==(pFd->nFetchOut==0) );
  4853   4859   
  4854   4860     /* If p!=0, it must match the iOff value. */
  4855   4861     assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
................................................................................
  4857   4863     if( p ){
  4858   4864       pFd->nFetchOut--;
  4859   4865     }else{
  4860   4866       unixUnmapfile(pFd);
  4861   4867     }
  4862   4868   
  4863   4869     assert( pFd->nFetchOut>=0 );
         4870  +#else
         4871  +  UNUSED_PARAMETER(fd);
         4872  +  UNUSED_PARAMETER(p);
         4873  +  UNUSED_PARAMETER(iOff);
  4864   4874   #endif
  4865   4875     return SQLITE_OK;
  4866   4876   }
  4867   4877   
  4868   4878   /*
  4869   4879   ** Here ends the implementation of all sqlite3_file methods.
  4870   4880   **
................................................................................
  5646   5656   
  5647   5657     /* Assert that the upper layer has set one of the "file-type" flags. */
  5648   5658     assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
  5649   5659          || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
  5650   5660          || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
  5651   5661          || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  5652   5662     );
         5663  +
         5664  +  /* Detect a pid change and reset the PRNG.  There is a race condition
         5665  +  ** here such that two or more threads all trying to open databases at
         5666  +  ** the same instant might all reset the PRNG.  But multiple resets
         5667  +  ** are harmless.
         5668  +  */
         5669  +  if( randomnessPid!=getpid() ){
         5670  +    randomnessPid = getpid();
         5671  +    sqlite3_randomness(0,0);
         5672  +  }
  5653   5673   
  5654   5674     memset(p, 0, sizeof(unixFile));
  5655   5675   
  5656   5676     if( eType==SQLITE_OPEN_MAIN_DB ){
  5657   5677       UnixUnusedFd *pUnused;
  5658   5678       pUnused = findReusableFd(zName, flags);
  5659   5679       if( pUnused ){
................................................................................
  6034   6054     ** in the random seed.
  6035   6055     **
  6036   6056     ** When testing, initializing zBuf[] to zero is all we do.  That means
  6037   6057     ** that we always use the same random number sequence.  This makes the
  6038   6058     ** tests repeatable.
  6039   6059     */
  6040   6060     memset(zBuf, 0, nBuf);
         6061  +  randomnessPid = getpid();  
  6041   6062   #if !defined(SQLITE_TEST)
  6042   6063     {
  6043         -    int pid, fd, got;
         6064  +    int fd, got;
  6044   6065       fd = robust_open("/dev/urandom", O_RDONLY, 0);
  6045   6066       if( fd<0 ){
  6046   6067         time_t t;
  6047   6068         time(&t);
  6048   6069         memcpy(zBuf, &t, sizeof(t));
  6049         -      pid = getpid();
  6050         -      memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
  6051         -      assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
  6052         -      nBuf = sizeof(t) + sizeof(pid);
         6070  +      memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
         6071  +      assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
         6072  +      nBuf = sizeof(t) + sizeof(randomnessPid);
  6053   6073       }else{
  6054   6074         do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
  6055   6075         robust_close(0, fd, __LINE__);
  6056   6076       }
  6057   6077     }
  6058   6078   #endif
  6059   6079     return nBuf;

Changes to src/os_win.c.

  3220   3220   */
  3221   3221   static void winShmEnterMutex(void){
  3222   3222     sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  3223   3223   }
  3224   3224   static void winShmLeaveMutex(void){
  3225   3225     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  3226   3226   }
  3227         -#ifdef SQLITE_DEBUG
         3227  +#ifndef NDEBUG
  3228   3228   static int winShmMutexHeld(void) {
  3229   3229     return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  3230   3230   }
  3231   3231   #endif
  3232   3232   
  3233   3233   /*
  3234   3234   ** Object used to represent a single file opened and mmapped to provide

Changes to src/parse.y.

    90     90   struct TrigEvent { int a; IdList * b; };
    91     91   
    92     92   /*
    93     93   ** An instance of this structure holds the ATTACH key and the key type.
    94     94   */
    95     95   struct AttachKey { int type;  Token key; };
    96     96   
    97         -/*
    98         -** One or more VALUES claues
    99         -*/
   100         -struct ValueList {
   101         -  ExprList *pList;
   102         -  Select *pSelect;
   103         -};
   104         -
   105     97   } // end %include
   106     98   
   107     99   // Input is a single SQL command
   108    100   input ::= cmdlist.
   109    101   cmdlist ::= cmdlist ecmd.
   110    102   cmdlist ::= ecmd.
   111    103   ecmd ::= SEMI.
................................................................................
   198    190     pParse->constraintName.n = 0;
   199    191   }
   200    192   
   201    193   
   202    194   // An IDENTIFIER can be a generic identifier, or one of several
   203    195   // keywords.  Any non-standard keyword can also be an identifier.
   204    196   //
   205         -%type id {Token}
   206         -id(A) ::= ID(X).         {A = X;}
   207         -id(A) ::= INDEXED(X).    {A = X;}
          197  +%token_class id  ID|INDEXED.
   208    198   
   209    199   // The following directive causes tokens ABORT, AFTER, ASC, etc. to
   210    200   // fallback to ID if they will not parse as their original value.
   211    201   // This obviates the need for the "id" nonterminal.
   212    202   //
   213    203   %fallback ID
   214    204     ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
   215    205     CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR
   216    206     IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
   217         -  QUERY KEY OF OFFSET PRAGMA RAISE RELEASE REPLACE RESTRICT ROW ROLLBACK
   218         -  SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITHOUT
          207  +  QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW
          208  +  ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
   219    209   %ifdef SQLITE_OMIT_COMPOUND_SELECT
   220    210     EXCEPT INTERSECT UNION
   221    211   %endif SQLITE_OMIT_COMPOUND_SELECT
   222    212     REINDEX RENAME CTIME_KW IF
   223    213     .
   224    214   %wildcard ANY.
   225    215   
................................................................................
   245    235   %left STAR SLASH REM.
   246    236   %left CONCAT.
   247    237   %left COLLATE.
   248    238   %right BITNOT.
   249    239   
   250    240   // And "ids" is an identifer-or-string.
   251    241   //
   252         -%type ids {Token}
   253         -ids(A) ::= ID|STRING(X).   {A = X;}
          242  +%token_class ids  ID|STRING.
   254    243   
   255    244   // The name of a column or table can be any of the following:
   256    245   //
   257    246   %type nm {Token}
   258    247   nm(A) ::= id(X).         {A = X;}
   259    248   nm(A) ::= STRING(X).     {A = X;}
   260    249   nm(A) ::= JOIN_KW(X).    {A = X;}
................................................................................
   404    393     sqlite3DropTable(pParse, X, 1, E);
   405    394   }
   406    395   %endif  SQLITE_OMIT_VIEW
   407    396   
   408    397   //////////////////////// The SELECT statement /////////////////////////////////
   409    398   //
   410    399   cmd ::= select(X).  {
   411         -  SelectDest dest = {SRT_Output, 0, 0, 0, 0};
          400  +  SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
   412    401     sqlite3Select(pParse, X, &dest);
   413    402     sqlite3ExplainBegin(pParse->pVdbe);
   414    403     sqlite3ExplainSelect(pParse->pVdbe, X);
   415    404     sqlite3ExplainFinish(pParse->pVdbe);
   416    405     sqlite3SelectDelete(pParse->db, X);
   417    406   }
   418    407   
   419    408   %type select {Select*}
   420    409   %destructor select {sqlite3SelectDelete(pParse->db, $$);}
          410  +%type selectnowith {Select*}
          411  +%destructor selectnowith {sqlite3SelectDelete(pParse->db, $$);}
   421    412   %type oneselect {Select*}
   422    413   %destructor oneselect {sqlite3SelectDelete(pParse->db, $$);}
   423    414   
   424         -select(A) ::= oneselect(X).                      {A = X;}
          415  +select(A) ::= with(W) selectnowith(X). { 
          416  +  if( X ){
          417  +    X->pWith = W; 
          418  +  }else{
          419  +    sqlite3WithDelete(pParse->db, W);
          420  +  }
          421  +  A = X; 
          422  +}
          423  +
          424  +selectnowith(A) ::= oneselect(X).                      {A = X;}
   425    425   %ifndef SQLITE_OMIT_COMPOUND_SELECT
   426         -select(A) ::= select(X) multiselect_op(Y) oneselect(Z).  {
          426  +selectnowith(A) ::= selectnowith(X) multiselect_op(Y) oneselect(Z).  {
   427    427     if( Z ){
   428    428       Z->op = (u8)Y;
   429    429       Z->pPrior = X;
   430    430       if( Y!=TK_ALL ) pParse->hasCompound = 1;
   431    431     }else{
   432    432       sqlite3SelectDelete(pParse->db, X);
   433    433     }
................................................................................
   437    437   multiselect_op(A) ::= UNION(OP).             {A = @OP;}
   438    438   multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
   439    439   multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
   440    440   %endif SQLITE_OMIT_COMPOUND_SELECT
   441    441   oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
   442    442                    groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
   443    443     A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
          444  +}
          445  +oneselect(A) ::= values(X).    {A = X;}
          446  +
          447  +%type values {Select*}
          448  +%destructor values {sqlite3SelectDelete(pParse->db, $$);}
          449  +values(A) ::= VALUES LP nexprlist(X) RP. {
          450  +  A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0,0);
          451  +}
          452  +values(A) ::= values(X) COMMA LP exprlist(Y) RP. {
          453  +  Select *pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values,0,0);
          454  +  if( pRight ){
          455  +    pRight->op = TK_ALL;
          456  +    pRight->pPrior = X;
          457  +    A = pRight;
          458  +  }else{
          459  +    A = X;
          460  +  }
   444    461   }
   445    462   
   446    463   // The "distinct" nonterminal is true (1) if the DISTINCT keyword is
   447    464   // present and false (0) if it is not.
   448    465   //
   449    466   %type distinct {u16}
   450    467   distinct(A) ::= DISTINCT.   {A = SF_Distinct;}
................................................................................
   638    655                                         {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;}
   639    656   limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 
   640    657                                         {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;}
   641    658   
   642    659   /////////////////////////// The DELETE statement /////////////////////////////
   643    660   //
   644    661   %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   645         -cmd ::= DELETE FROM fullname(X) indexed_opt(I) where_opt(W) 
          662  +cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W) 
   646    663           orderby_opt(O) limit_opt(L). {
          664  +  sqlite3WithPush(pParse, C, 1);
   647    665     sqlite3SrcListIndexedBy(pParse, X, &I);
   648    666     W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "DELETE");
   649    667     sqlite3DeleteFrom(pParse,X,W);
   650    668   }
   651    669   %endif
   652    670   %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   653         -cmd ::= DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
          671  +cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
          672  +  sqlite3WithPush(pParse, C, 1);
   654    673     sqlite3SrcListIndexedBy(pParse, X, &I);
   655    674     sqlite3DeleteFrom(pParse,X,W);
   656    675   }
   657    676   %endif
   658    677   
   659    678   %type where_opt {Expr*}
   660    679   %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
................................................................................
   661    680   
   662    681   where_opt(A) ::= .                    {A = 0;}
   663    682   where_opt(A) ::= WHERE expr(X).       {A = X.pExpr;}
   664    683   
   665    684   ////////////////////////// The UPDATE command ////////////////////////////////
   666    685   //
   667    686   %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   668         -cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W)
   669         -        orderby_opt(O) limit_opt(L).  {
          687  +cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
          688  +        where_opt(W) orderby_opt(O) limit_opt(L).  {
          689  +  sqlite3WithPush(pParse, C, 1);
   670    690     sqlite3SrcListIndexedBy(pParse, X, &I);
   671    691     sqlite3ExprListCheckLength(pParse,Y,"set list"); 
   672    692     W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE");
   673    693     sqlite3Update(pParse,X,Y,W,R);
   674    694   }
   675    695   %endif
   676    696   %ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   677         -cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
          697  +cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
   678    698           where_opt(W).  {
          699  +  sqlite3WithPush(pParse, C, 1);
   679    700     sqlite3SrcListIndexedBy(pParse, X, &I);
   680    701     sqlite3ExprListCheckLength(pParse,Y,"set list"); 
   681    702     sqlite3Update(pParse,X,Y,W,R);
   682    703   }
   683    704   %endif
   684    705   
   685    706   %type setlist {ExprList*}
................................................................................
   692    713   setlist(A) ::= nm(X) EQ expr(Y). {
   693    714     A = sqlite3ExprListAppend(pParse, 0, Y.pExpr);
   694    715     sqlite3ExprListSetName(pParse, A, &X, 1);
   695    716   }
   696    717   
   697    718   ////////////////////////// The INSERT command /////////////////////////////////
   698    719   //
   699         -cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) valuelist(Y).
   700         -            {sqlite3Insert(pParse, X, Y.pList, Y.pSelect, F, R);}
   701         -cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
   702         -            {sqlite3Insert(pParse, X, 0, S, F, R);}
   703         -cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) DEFAULT VALUES.
   704         -            {sqlite3Insert(pParse, X, 0, 0, F, R);}
          720  +cmd ::= with(W) insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S). {
          721  +  sqlite3WithPush(pParse, W, 1);
          722  +  sqlite3Insert(pParse, X, S, F, R);
          723  +}
          724  +cmd ::= with(W) insert_cmd(R) INTO fullname(X) inscollist_opt(F) DEFAULT VALUES.
          725  +{
          726  +  sqlite3WithPush(pParse, W, 1);
          727  +  sqlite3Insert(pParse, X, 0, F, R);
          728  +}
   705    729   
   706    730   %type insert_cmd {u8}
   707    731   insert_cmd(A) ::= INSERT orconf(R).   {A = R;}
   708    732   insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
   709    733   
   710         -// A ValueList is either a single VALUES clause or a comma-separated list
   711         -// of VALUES clauses.  If it is a single VALUES clause then the
   712         -// ValueList.pList field points to the expression list of that clause.
   713         -// If it is a list of VALUES clauses, then those clauses are transformed
   714         -// into a set of SELECT statements without FROM clauses and connected by
   715         -// UNION ALL and the ValueList.pSelect points to the right-most SELECT in
   716         -// that compound.
   717         -%type valuelist {struct ValueList}
   718         -%destructor valuelist {
   719         -  sqlite3ExprListDelete(pParse->db, $$.pList);
   720         -  sqlite3SelectDelete(pParse->db, $$.pSelect);
   721         -}
   722         -valuelist(A) ::= VALUES LP nexprlist(X) RP. {
   723         -  A.pList = X;
   724         -  A.pSelect = 0;
   725         -}
   726         -
   727         -// Since a list of VALUEs is inplemented as a compound SELECT, we have
   728         -// to disable the value list option if compound SELECTs are disabled.
   729         -%ifndef SQLITE_OMIT_COMPOUND_SELECT
   730         -valuelist(A) ::= valuelist(X) COMMA LP exprlist(Y) RP. {
   731         -  Select *pRight = sqlite3SelectNew(pParse, Y, 0, 0, 0, 0, 0, 0, 0, 0);
   732         -  if( X.pList ){
   733         -    X.pSelect = sqlite3SelectNew(pParse, X.pList, 0, 0, 0, 0, 0, 0, 0, 0);
   734         -    X.pList = 0;
   735         -  }
   736         -  A.pList = 0;
   737         -  if( X.pSelect==0 || pRight==0 ){
   738         -    sqlite3SelectDelete(pParse->db, pRight);
   739         -    sqlite3SelectDelete(pParse->db, X.pSelect);
   740         -    A.pSelect = 0;
   741         -  }else{
   742         -    pRight->op = TK_ALL;
   743         -    pRight->pPrior = X.pSelect;
   744         -    pRight->selFlags |= SF_Values;
   745         -    pRight->pPrior->selFlags |= SF_Values;
   746         -    A.pSelect = pRight;
   747         -  }
   748         -}
   749         -%endif SQLITE_OMIT_COMPOUND_SELECT
   750         -
   751    734   %type inscollist_opt {IdList*}
   752    735   %destructor inscollist_opt {sqlite3IdListDelete(pParse->db, $$);}
   753    736   %type idlist {IdList*}
   754    737   %destructor idlist {sqlite3IdListDelete(pParse->db, $$);}
   755    738   
   756    739   inscollist_opt(A) ::= .                       {A = 0;}
   757    740   inscollist_opt(A) ::= LP idlist(X) RP.    {A = X;}
................................................................................
   806    789     Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z);
   807    790     Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
   808    791     A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
   809    792     spanSet(&A,&X,&Z);
   810    793   }
   811    794   term(A) ::= INTEGER|FLOAT|BLOB(X).  {spanExpr(&A, pParse, @X, &X);}
   812    795   term(A) ::= STRING(X).              {spanExpr(&A, pParse, @X, &X);}
   813         -expr(A) ::= REGISTER(X).     {
   814         -  /* When doing a nested parse, one can include terms in an expression
   815         -  ** that look like this:   #1 #2 ...  These terms refer to registers
   816         -  ** in the virtual machine.  #N is the N-th register. */
   817         -  if( pParse->nested==0 ){
   818         -    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &X);
   819         -    A.pExpr = 0;
          796  +expr(A) ::= VARIABLE(X).     {
          797  +  if( X.n>=2 && X.z[0]=='#' && sqlite3Isdigit(X.z[1]) ){
          798  +    /* When doing a nested parse, one can include terms in an expression
          799  +    ** that look like this:   #1 #2 ...  These terms refer to registers
          800  +    ** in the virtual machine.  #N is the N-th register. */
          801  +    if( pParse->nested==0 ){
          802  +      sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &X);
          803  +      A.pExpr = 0;
          804  +    }else{
          805  +      A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &X);
          806  +      if( A.pExpr ) sqlite3GetInt32(&X.z[1], &A.pExpr->iTable);
          807  +    }
   820    808     }else{
   821         -    A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &X);
   822         -    if( A.pExpr ) sqlite3GetInt32(&X.z[1], &A.pExpr->iTable);
          809  +    spanExpr(&A, pParse, TK_VARIABLE, &X);
          810  +    sqlite3ExprAssignVarNumber(pParse, A.pExpr);
   823    811     }
   824    812     spanSet(&A, &X, &X);
   825    813   }
   826         -expr(A) ::= VARIABLE(X).     {
   827         -  spanExpr(&A, pParse, TK_VARIABLE, &X);
   828         -  sqlite3ExprAssignVarNumber(pParse, A.pExpr);
   829         -  spanSet(&A, &X, &X);
   830         -}
   831    814   expr(A) ::= expr(E) COLLATE ids(C). {
   832    815     A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C);
   833    816     A.zStart = E.zStart;
   834    817     A.zEnd = &C.z[C.n];
   835    818   }
   836    819   %ifndef SQLITE_OMIT_CAST
   837    820   expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
   838    821     A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T);
   839    822     spanSet(&A,&X,&Y);
   840    823   }
   841    824   %endif  SQLITE_OMIT_CAST
   842         -expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
          825  +expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). {
   843    826     if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
   844    827       sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
   845    828     }
   846    829     A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
   847    830     spanSet(&A,&X,&E);
   848    831     if( D && A.pExpr ){
   849    832       A.pExpr->flags |= EP_Distinct;
   850    833     }
   851    834   }
   852         -expr(A) ::= ID(X) LP STAR RP(E). {
          835  +expr(A) ::= id(X) LP STAR RP(E). {
   853    836     A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
   854    837     spanSet(&A,&X,&E);
   855    838   }
   856    839   term(A) ::= CTIME_KW(OP). {
   857    840     A.pExpr = sqlite3ExprFunction(pParse, 0, &OP);
   858    841     spanSet(&A, &OP, &OP);
   859    842   }
................................................................................
   884    867                                           {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
   885    868   expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).
   886    869                                           {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
   887    870   expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y).
   888    871                                           {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
   889    872   expr(A) ::= expr(X) CONCAT(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
   890    873   %type likeop {struct LikeOp}
   891         -likeop(A) ::= LIKE_KW(X).     {A.eOperator = X; A.bNot = 0;}
   892         -likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.bNot = 1;}
   893         -likeop(A) ::= MATCH(X).       {A.eOperator = X; A.bNot = 0;}
   894         -likeop(A) ::= NOT MATCH(X).   {A.eOperator = X; A.bNot = 1;}
          874  +likeop(A) ::= LIKE_KW|MATCH(X).     {A.eOperator = X; A.bNot = 0;}
          875  +likeop(A) ::= NOT LIKE_KW|MATCH(X). {A.eOperator = X; A.bNot = 1;}
   895    876   expr(A) ::= expr(X) likeop(OP) expr(Y).  [LIKE_KW]  {
   896    877     ExprList *pList;
   897    878     pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
   898    879     pList = sqlite3ExprListAppend(pParse,pList, X.pExpr);
   899    880     A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
   900    881     if( OP.bNot ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
   901    882     A.zStart = X.zStart;
................................................................................
  1195   1176   
  1196   1177   nmnum(A) ::= plus_num(X).             {A = X;}
  1197   1178   nmnum(A) ::= nm(X).                   {A = X;}
  1198   1179   nmnum(A) ::= ON(X).                   {A = X;}
  1199   1180   nmnum(A) ::= DELETE(X).               {A = X;}
  1200   1181   nmnum(A) ::= DEFAULT(X).              {A = X;}
  1201   1182   %endif SQLITE_OMIT_PRAGMA
         1183  +%token_class number INTEGER|FLOAT.
  1202   1184   plus_num(A) ::= PLUS number(X).       {A = X;}
  1203   1185   plus_num(A) ::= number(X).            {A = X;}
  1204   1186   minus_num(A) ::= MINUS number(X).     {A = X;}
  1205         -number(A) ::= INTEGER|FLOAT(X).       {A = X;}
  1206         -
  1207   1187   //////////////////////////// The CREATE TRIGGER command /////////////////////
  1208   1188   
  1209   1189   %ifndef SQLITE_OMIT_TRIGGER
  1210   1190   
  1211   1191   cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
  1212   1192     Token all;
  1213   1193     all.z = A.z;
................................................................................
  1291   1271   %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
  1292   1272   // UPDATE 
  1293   1273   trigger_cmd(A) ::=
  1294   1274      UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z).  
  1295   1275      { A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R); }
  1296   1276   
  1297   1277   // INSERT
  1298         -trigger_cmd(A) ::=
  1299         -   insert_cmd(R) INTO trnm(X) inscollist_opt(F) valuelist(Y).
  1300         -   {A = sqlite3TriggerInsertStep(pParse->db, &X, F, Y.pList, Y.pSelect, R);}
  1301         -
  1302   1278   trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) inscollist_opt(F) select(S).
  1303         -               {A = sqlite3TriggerInsertStep(pParse->db, &X, F, 0, S, R);}
         1279  +               {A = sqlite3TriggerInsertStep(pParse->db, &X, F, S, R);}
  1304   1280   
  1305   1281   // DELETE
  1306   1282   trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y).
  1307   1283                  {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
  1308   1284   
  1309   1285   // SELECT
  1310   1286   trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(pParse->db, X); }
................................................................................
  1402   1378   vtabargtoken ::= ANY(X).            {sqlite3VtabArgExtend(pParse,&X);}
  1403   1379   vtabargtoken ::= lp anylist RP(X).  {sqlite3VtabArgExtend(pParse,&X);}
  1404   1380   lp ::= LP(X).                       {sqlite3VtabArgExtend(pParse,&X);}
  1405   1381   anylist ::= .
  1406   1382   anylist ::= anylist LP anylist RP.
  1407   1383   anylist ::= anylist ANY.
  1408   1384   %endif  SQLITE_OMIT_VIRTUALTABLE
         1385  +
         1386  +
         1387  +//////////////////////// COMMON TABLE EXPRESSIONS ////////////////////////////
         1388  +%type with {With*}
         1389  +%type wqlist {With*}
         1390  +%destructor with {sqlite3WithDelete(pParse->db, $$);}
         1391  +%destructor wqlist {sqlite3WithDelete(pParse->db, $$);}
         1392  +
         1393  +with(A) ::= . {A = 0;}
         1394  +%ifndef SQLITE_OMIT_CTE
         1395  +with(A) ::= WITH wqlist(W).              { A = W; }
         1396  +with(A) ::= WITH RECURSIVE wqlist(W).    { A = W; }
         1397  +
         1398  +wqlist(A) ::= nm(X) idxlist_opt(Y) AS LP select(Z) RP. {
         1399  +  A = sqlite3WithAdd(pParse, 0, &X, Y, Z);
         1400  +}
         1401  +wqlist(A) ::= wqlist(W) COMMA nm(X) idxlist_opt(Y) AS LP select(Z) RP. {
         1402  +  A = sqlite3WithAdd(pParse, W, &X, Y, Z);
         1403  +}
         1404  +%endif  SQLITE_OMIT_CTE

Changes to src/pragma.c.

  1877   1877         sqlite3VdbeJumpHere(v, addr);
  1878   1878   
  1879   1879         /* Make sure all the indices are constructed correctly.
  1880   1880         */
  1881   1881         for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
  1882   1882           Table *pTab = sqliteHashData(x);
  1883   1883           Index *pIdx, *pPk;
         1884  +        Index *pPrior = 0;
  1884   1885           int loopTop;
  1885   1886           int iDataCur, iIdxCur;
         1887  +        int r1 = -1;
  1886   1888   
  1887   1889           if( pTab->pIndex==0 ) continue;
  1888   1890           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
  1889   1891           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
  1890   1892           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1891   1893           sqlite3VdbeJumpHere(v, addr);
  1892   1894           sqlite3ExprCacheClear(pParse);
................................................................................
  1897   1899             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
  1898   1900           }
  1899   1901           pParse->nMem = MAX(pParse->nMem, 8+j);
  1900   1902           sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0);
  1901   1903           loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
  1902   1904           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1903   1905             int jmp2, jmp3, jmp4;
  1904         -          int r1;
  1905   1906             if( pPk==pIdx ) continue;
  1906         -          r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3);
         1907  +          r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
         1908  +                                       pPrior, r1);
         1909  +          pPrior = pIdx;
  1907   1910             sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
  1908   1911             jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
  1909   1912                                         pIdx->nColumn);
  1910   1913             sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
  1911   1914             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
  1912   1915             sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
  1913   1916             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",

Changes to src/random.c.

    47     47   # define wsdPrng sqlite3Prng
    48     48   #endif
    49     49   
    50     50   #if SQLITE_THREADSAFE
    51     51     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
    52     52     sqlite3_mutex_enter(mutex);
    53     53   #endif
           54  +
           55  +  if( N<=0 ){
           56  +    wsdPrng.isInit = 0;
           57  +    sqlite3_mutex_leave(mutex);
           58  +    return;
           59  +  }
    54     60   
    55     61     /* Initialize the state of the random number generator once,
    56     62     ** the first time this routine is called.  The seed value does
    57     63     ** not need to contain a lot of randomness since we are not
    58     64     ** trying to do secure encryption or anything like that...
    59     65     **
    60     66     ** Nothing in this file or anywhere else in SQLite does any kind of
................................................................................
    75     81         t = wsdPrng.s[wsdPrng.j];
    76     82         wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
    77     83         wsdPrng.s[i] = t;
    78     84       }
    79     85       wsdPrng.isInit = 1;
    80     86     }
    81     87   
    82         -  while( N-- ){
           88  +  assert( N>0 );
           89  +  do{
    83     90       wsdPrng.i++;
    84     91       t = wsdPrng.s[wsdPrng.i];
    85     92       wsdPrng.j += t;
    86     93       wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
    87     94       wsdPrng.s[wsdPrng.j] = t;
    88     95       t += wsdPrng.s[wsdPrng.i];
    89     96       *(zBuf++) = wsdPrng.s[t];
    90         -  }
           97  +  }while( --N );
    91     98     sqlite3_mutex_leave(mutex);
    92     99   }
    93    100   
    94    101   #ifndef SQLITE_OMIT_BUILTIN_TEST
    95    102   /*
    96    103   ** For testing purposes, we sometimes want to preserve the state of
    97    104   ** PRNG and restore the PRNG to its saved state at a later time, or
................................................................................
   112    119   void sqlite3PrngRestoreState(void){
   113    120     memcpy(
   114    121       &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
   115    122       &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
   116    123       sizeof(sqlite3Prng)
   117    124     );
   118    125   }
   119         -void sqlite3PrngResetState(void){
   120         -  GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
   121         -}
   122    126   #endif /* SQLITE_OMIT_BUILTIN_TEST */

Changes to src/select.c.

    25     25     sqlite3ExprDelete(db, p->pWhere);
    26     26     sqlite3ExprListDelete(db, p->pGroupBy);
    27     27     sqlite3ExprDelete(db, p->pHaving);
    28     28     sqlite3ExprListDelete(db, p->pOrderBy);
    29     29     sqlite3SelectDelete(db, p->pPrior);
    30     30     sqlite3ExprDelete(db, p->pLimit);
    31     31     sqlite3ExprDelete(db, p->pOffset);
           32  +  sqlite3WithDelete(db, p->pWith);
    32     33   }
    33     34   
    34     35   /*
    35     36   ** Initialize a SelectDest structure.
    36     37   */
    37     38   void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
    38     39     pDest->eDest = (u8)eDest;
................................................................................
   457    458   }
   458    459   
   459    460   /*
   460    461   ** Add code to implement the OFFSET
   461    462   */
   462    463   static void codeOffset(
   463    464     Vdbe *v,          /* Generate code into this VM */
   464         -  Select *p,        /* The SELECT statement being coded */
          465  +  int iOffset,      /* Register holding the offset counter */
   465    466     int iContinue     /* Jump here to skip the current record */
   466    467   ){
   467         -  if( p->iOffset && iContinue!=0 ){
          468  +  if( iOffset>0 && iContinue!=0 ){
   468    469       int addr;
   469         -    sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
   470         -    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
          470  +    sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
          471  +    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset);
   471    472       sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
   472    473       VdbeComment((v, "skip OFFSET records"));
   473    474       sqlite3VdbeJumpHere(v, addr);
   474    475     }
   475    476   }
   476    477   
   477    478   /*
................................................................................
   538    539     int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
   539    540   };
   540    541   
   541    542   /*
   542    543   ** This routine generates the code for the inside of the inner loop
   543    544   ** of a SELECT.
   544    545   **
   545         -** If srcTab and nColumn are both zero, then the pEList expressions
   546         -** are evaluated in order to get the data for this row.  If nColumn>0
   547         -** then data is pulled from srcTab and pEList is used only to get the
   548         -** datatypes for each column.
          546  +** If srcTab is negative, then the pEList expressions
          547  +** are evaluated in order to get the data for this row.  If srcTab is
          548  +** zero or more, then data is pulled from srcTab and pEList is used only 
          549  +** to get number columns and the datatype for each column.
   549    550   */
   550    551   static void selectInnerLoop(
   551    552     Parse *pParse,          /* The parser context */
   552    553     Select *p,              /* The complete select statement being coded */
   553    554     ExprList *pEList,       /* List of values being extracted */
   554    555     int srcTab,             /* Pull data from this table */
   555         -  int nColumn,            /* Number of columns in the source table */
   556    556     ExprList *pOrderBy,     /* If not NULL, sort results using this key */
   557    557     DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
   558    558     SelectDest *pDest,      /* How to dispose of the results */
   559    559     int iContinue,          /* Jump here to continue with next row */
   560    560     int iBreak              /* Jump here to break out of the inner loop */
   561    561   ){
   562    562     Vdbe *v = pParse->pVdbe;
................................................................................
   564    564     int hasDistinct;        /* True if the DISTINCT keyword is present */
   565    565     int regResult;              /* Start of memory holding result set */
   566    566     int eDest = pDest->eDest;   /* How to dispose of results */
   567    567     int iParm = pDest->iSDParm; /* First argument to disposal method */
   568    568     int nResultCol;             /* Number of result columns */
   569    569   
   570    570     assert( v );
   571         -  if( NEVER(v==0) ) return;
   572    571     assert( pEList!=0 );
   573    572     hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
   574    573     if( pOrderBy==0 && !hasDistinct ){
   575         -    codeOffset(v, p, iContinue);
          574  +    codeOffset(v, p->iOffset, iContinue);
   576    575     }
   577    576   
   578    577     /* Pull the requested columns.
   579    578     */
   580         -  if( nColumn>0 ){
   581         -    nResultCol = nColumn;
   582         -  }else{
   583         -    nResultCol = pEList->nExpr;
   584         -  }
          579  +  nResultCol = pEList->nExpr;
   585    580     if( pDest->iSdst==0 ){
   586    581       pDest->iSdst = pParse->nMem+1;
   587    582       pDest->nSdst = nResultCol;
   588    583       pParse->nMem += nResultCol;
   589    584     }else{ 
   590    585       assert( pDest->nSdst==nResultCol );
   591    586     }
   592    587     regResult = pDest->iSdst;
   593         -  if( nColumn>0 ){
   594         -    for(i=0; i<nColumn; i++){
          588  +  if( srcTab>=0 ){
          589  +    for(i=0; i<nResultCol; i++){
   595    590         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
          591  +      VdbeComment((v, "%s", pEList->a[i].zName));
   596    592       }
   597    593     }else if( eDest!=SRT_Exists ){
   598    594       /* If the destination is an EXISTS(...) expression, the actual
   599    595       ** values returned by the SELECT are not required.
   600    596       */
   601    597       sqlite3ExprCodeExprList(pParse, pEList, regResult,
   602    598                               (eDest==SRT_Output)?SQLITE_ECEL_DUP:0);
   603    599     }
   604         -  nColumn = nResultCol;
   605    600   
   606    601     /* If the DISTINCT keyword was present on the SELECT statement
   607    602     ** and this row has been seen before, then do not make this row
   608    603     ** part of the result.
   609    604     */
   610    605     if( hasDistinct ){
   611         -    assert( pEList!=0 );
   612         -    assert( pEList->nExpr==nColumn );
   613    606       switch( pDistinct->eTnctType ){
   614    607         case WHERE_DISTINCT_ORDERED: {
   615    608           VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
   616    609           int iJump;              /* Jump destination */
   617    610           int regPrev;            /* Previous row content */
   618    611   
   619    612           /* Allocate space for the previous row */
   620    613           regPrev = pParse->nMem+1;
   621         -        pParse->nMem += nColumn;
          614  +        pParse->nMem += nResultCol;
   622    615   
   623    616           /* Change the OP_OpenEphemeral coded earlier to an OP_Null
   624    617           ** sets the MEM_Cleared bit on the first register of the
   625    618           ** previous value.  This will cause the OP_Ne below to always
   626    619           ** fail on the first iteration of the loop even if the first
   627    620           ** row is all NULLs.
   628    621           */
   629    622           sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
   630    623           pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
   631    624           pOp->opcode = OP_Null;
   632    625           pOp->p1 = 1;
   633    626           pOp->p2 = regPrev;
   634    627   
   635         -        iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
   636         -        for(i=0; i<nColumn; i++){
          628  +        iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
          629  +        for(i=0; i<nResultCol; i++){
   637    630             CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
   638         -          if( i<nColumn-1 ){
          631  +          if( i<nResultCol-1 ){
   639    632               sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
   640    633             }else{
   641    634               sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
   642    635             }
   643    636             sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
   644    637             sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
   645    638           }
   646    639           assert( sqlite3VdbeCurrentAddr(v)==iJump );
   647         -        sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
          640  +        sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
   648    641           break;
   649    642         }
   650    643   
   651    644         case WHERE_DISTINCT_UNIQUE: {
   652    645           sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
   653    646           break;
   654    647         }
   655    648   
   656    649         default: {
   657    650           assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
   658         -        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
          651  +        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
   659    652           break;
   660    653         }
   661    654       }
   662    655       if( pOrderBy==0 ){
   663         -      codeOffset(v, p, iContinue);
          656  +      codeOffset(v, p->iOffset, iContinue);
   664    657       }
   665    658     }
   666    659   
   667    660     switch( eDest ){
   668    661       /* In this mode, write each query result to the key of the temporary
   669    662       ** table iParm.
   670    663       */
   671    664   #ifndef SQLITE_OMIT_COMPOUND_SELECT
   672    665       case SRT_Union: {
   673    666         int r1;
   674    667         r1 = sqlite3GetTempReg(pParse);
   675         -      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
          668  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
   676    669         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
   677    670         sqlite3ReleaseTempReg(pParse, r1);
   678    671         break;
   679    672       }
   680    673   
   681    674       /* Construct a record from the query result, but instead of
   682    675       ** saving that record, use it as a key to delete elements from
   683    676       ** the temporary table iParm.
   684    677       */
   685    678       case SRT_Except: {
   686         -      sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
          679  +      sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
   687    680         break;
   688    681       }
   689         -#endif
          682  +#endif /* SQLITE_OMIT_COMPOUND_SELECT */
   690    683   
   691    684       /* Store the result as data using a unique key.
   692    685       */
          686  +    case SRT_DistTable:
   693    687       case SRT_Table:
   694    688       case SRT_EphemTab: {
   695    689         int r1 = sqlite3GetTempReg(pParse);
   696    690         testcase( eDest==SRT_Table );
   697    691         testcase( eDest==SRT_EphemTab );
   698         -      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
          692  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
          693  +#ifndef SQLITE_OMIT_CTE
          694  +      if( eDest==SRT_DistTable ){
          695  +        /* If the destination is DistTable, then cursor (iParm+1) is open
          696  +        ** on an ephemeral index. If the current row is already present
          697  +        ** in the index, do not write it to the output. If not, add the
          698  +        ** current row to the index and proceed with writing it to the
          699  +        ** output table as well.  */
          700  +        int addr = sqlite3VdbeCurrentAddr(v) + 4;
          701  +        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
          702  +        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
          703  +        assert( pOrderBy==0 );
          704  +      }
          705  +#endif
   699    706         if( pOrderBy ){
   700    707           pushOntoSorter(pParse, pOrderBy, p, r1);
   701    708         }else{
   702    709           int r2 = sqlite3GetTempReg(pParse);
   703    710           sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
   704    711           sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
   705    712           sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
................................................................................
   711    718   
   712    719   #ifndef SQLITE_OMIT_SUBQUERY
   713    720       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
   714    721       ** then there should be a single item on the stack.  Write this
   715    722       ** item into the set table with bogus data.
   716    723       */
   717    724       case SRT_Set: {
   718         -      assert( nColumn==1 );
          725  +      assert( nResultCol==1 );
   719    726         pDest->affSdst =
   720    727                     sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
   721    728         if( pOrderBy ){
   722    729           /* At first glance you would think we could optimize out the
   723    730           ** ORDER BY in this case since the order of entries in the set
   724    731           ** does not matter.  But there might be a LIMIT clause, in which
   725    732           ** case the order does matter */
................................................................................
   743    750       }
   744    751   
   745    752       /* If this is a scalar select that is part of an expression, then
   746    753       ** store the results in the appropriate memory cell and break out
   747    754       ** of the scan loop.
   748    755       */
   749    756       case SRT_Mem: {
   750         -      assert( nColumn==1 );
          757  +      assert( nResultCol==1 );
   751    758         if( pOrderBy ){
   752    759           pushOntoSorter(pParse, pOrderBy, p, regResult);
   753    760         }else{
   754    761           sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
   755    762           /* The LIMIT clause will jump out of the loop for us */
   756    763         }
   757    764         break;
................................................................................
   764    771       */
   765    772       case SRT_Coroutine:
   766    773       case SRT_Output: {
   767    774         testcase( eDest==SRT_Coroutine );
   768    775         testcase( eDest==SRT_Output );
   769    776         if( pOrderBy ){
   770    777           int r1 = sqlite3GetTempReg(pParse);
   771         -        sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
          778  +        sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
   772    779           pushOntoSorter(pParse, pOrderBy, p, r1);
   773    780           sqlite3ReleaseTempReg(pParse, r1);
   774    781         }else if( eDest==SRT_Coroutine ){
   775    782           sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
   776    783         }else{
   777         -        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
   778         -        sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
          784  +        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
          785  +        sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
   779    786         }
   780    787         break;
   781    788       }
          789  +
          790  +#ifndef SQLITE_OMIT_CTE
          791  +    /* Write the results into a priority queue that is order according to
          792  +    ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
          793  +    ** index with pSO->nExpr+2 columns.  Build a key using pSO for the first
          794  +    ** pSO->nExpr columns, then make sure all keys are unique by adding a
          795  +    ** final OP_Sequence column.  The last column is the record as a blob.
          796  +    */
          797  +    case SRT_DistQueue:
          798  +    case SRT_Queue: {
          799  +      int nKey;
          800  +      int r1, r2, r3;
          801  +      int addrTest = 0;
          802  +      ExprList *pSO;
          803  +      pSO = pDest->pOrderBy;
          804  +      assert( pSO );
          805  +      nKey = pSO->nExpr;
          806  +      r1 = sqlite3GetTempReg(pParse);
          807  +      r2 = sqlite3GetTempRange(pParse, nKey+2);
          808  +      r3 = r2+nKey+1;
          809  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
          810  +      if( eDest==SRT_DistQueue ){
          811  +        /* If the destination is DistQueue, then cursor (iParm+1) is open
          812  +        ** on a second ephemeral index that holds all values every previously
          813  +        ** added to the queue.  Only add this new value if it has never before
          814  +        ** been added */
          815  +        addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, r3, 0);
          816  +        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
          817  +        sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
          818  +      }
          819  +      for(i=0; i<nKey; i++){
          820  +        sqlite3VdbeAddOp2(v, OP_SCopy,
          821  +                          regResult + pSO->a[i].u.x.iOrderByCol - 1,
          822  +                          r2+i);
          823  +      }
          824  +      sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
          825  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
          826  +      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
          827  +      if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
          828  +      sqlite3ReleaseTempReg(pParse, r1);
          829  +      sqlite3ReleaseTempRange(pParse, r2, nKey+2);
          830  +      break;
          831  +    }
          832  +#endif /* SQLITE_OMIT_CTE */
          833  +
          834  +
   782    835   
   783    836   #if !defined(SQLITE_OMIT_TRIGGER)
   784    837       /* Discard the results.  This is used for SELECT statements inside
   785    838       ** the body of a TRIGGER.  The purpose of such selects is to call
   786    839       ** user-defined functions that have side effects.  We do not care
   787    840       ** about the actual results of the select.
   788    841       */
................................................................................
   864    917   ** then the KeyInfo structure is appropriate for initializing a virtual
   865    918   ** index to implement a DISTINCT test.
   866    919   **
   867    920   ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
   868    921   ** function is responsible for seeing that this structure is eventually
   869    922   ** freed.
   870    923   */
   871         -static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
          924  +static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList, int nExtra){
   872    925     int nExpr;
   873    926     KeyInfo *pInfo;
   874    927     struct ExprList_item *pItem;
   875    928     sqlite3 *db = pParse->db;
   876    929     int i;
   877    930   
   878    931     nExpr = pList->nExpr;
   879         -  pInfo = sqlite3KeyInfoAlloc(db, nExpr, 1);
          932  +  pInfo = sqlite3KeyInfoAlloc(db, nExpr+nExtra, 1);
   880    933     if( pInfo ){
   881    934       assert( sqlite3KeyInfoIsWriteable(pInfo) );
   882    935       for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
   883    936         CollSeq *pColl;
   884    937         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
   885    938         if( !pColl ) pColl = db->pDfltColl;
   886    939         pInfo->aColl[i] = pColl;
................................................................................
  1013   1066       regRowid = sqlite3GetTempReg(pParse);
  1014   1067     }
  1015   1068     if( p->selFlags & SF_UseSorter ){
  1016   1069       int regSortOut = ++pParse->nMem;
  1017   1070       int ptab2 = pParse->nTab++;
  1018   1071       sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
  1019   1072       addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
  1020         -    codeOffset(v, p, addrContinue);
         1073  +    codeOffset(v, p->iOffset, addrContinue);
  1021   1074       sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
  1022   1075       sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
  1023   1076       sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
  1024   1077     }else{
  1025   1078       addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
  1026         -    codeOffset(v, p, addrContinue);
         1079  +    codeOffset(v, p->iOffset, addrContinue);
  1027   1080       sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
  1028   1081     }
  1029   1082     switch( eDest ){
  1030   1083       case SRT_Table:
  1031   1084       case SRT_EphemTab: {
  1032   1085         testcase( eDest==SRT_Table );
  1033   1086         testcase( eDest==SRT_EphemTab );
................................................................................
  1198   1251             NameContext sNC;
  1199   1252             Expr *p = pS->pEList->a[iCol].pExpr;
  1200   1253             sNC.pSrcList = pS->pSrc;
  1201   1254             sNC.pNext = pNC;
  1202   1255             sNC.pParse = pNC->pParse;
  1203   1256             zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); 
  1204   1257           }
  1205         -      }else if( ALWAYS(pTab->pSchema) ){
         1258  +      }else if( pTab->pSchema ){
  1206   1259           /* A real table */
  1207   1260           assert( !pS );
  1208   1261           if( iCol<0 ) iCol = pTab->iPKey;
  1209   1262           assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
  1210   1263   #ifdef SQLITE_ENABLE_COLUMN_METADATA
  1211   1264           if( iCol<0 ){
  1212   1265             zType = "INTEGER";
................................................................................
  1359   1412           char *zName = 0;
  1360   1413           zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
  1361   1414           sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
  1362   1415         }else{
  1363   1416           sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
  1364   1417         }
  1365   1418       }else{
  1366         -      sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
  1367         -          sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
         1419  +      const char *z = pEList->a[i].zSpan;
         1420  +      z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
         1421  +      sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
  1368   1422       }
  1369   1423     }
  1370   1424     generateColumnTypes(pParse, pTabList, pEList);
  1371   1425   }
  1372   1426   
  1373   1427   /*
  1374   1428   ** Given a an expression list (which is really the list of expressions
................................................................................
  1582   1636   ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
  1583   1637   ** are the integer memory register numbers for counters used to compute 
  1584   1638   ** the limit and offset.  If there is no limit and/or offset, then 
  1585   1639   ** iLimit and iOffset are negative.
  1586   1640   **
  1587   1641   ** This routine changes the values of iLimit and iOffset only if
  1588   1642   ** a limit or offset is defined by pLimit and pOffset.  iLimit and
  1589         -** iOffset should have been preset to appropriate default values
  1590         -** (usually but not always -1) prior to calling this routine.
         1643  +** iOffset should have been preset to appropriate default values (zero)
         1644  +** prior to calling this routine.
         1645  +**
         1646  +** The iOffset register (if it exists) is initialized to the value
         1647  +** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
         1648  +** iOffset+1 is initialized to LIMIT+OFFSET.
         1649  +**
  1591   1650   ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
  1592   1651   ** redefined.  The UNION ALL operator uses this property to force
  1593   1652   ** the reuse of the same limit and offset registers across multiple
  1594   1653   ** SELECT statements.
  1595   1654   */
  1596   1655   static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
  1597   1656     Vdbe *v = 0;
................................................................................
  1607   1666     ** no rows.
  1608   1667     */
  1609   1668     sqlite3ExprCacheClear(pParse);
  1610   1669     assert( p->pOffset==0 || p->pLimit!=0 );
  1611   1670     if( p->pLimit ){
  1612   1671       p->iLimit = iLimit = ++pParse->nMem;
  1613   1672       v = sqlite3GetVdbe(pParse);
  1614         -    if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
         1673  +    assert( v!=0 );
  1615   1674       if( sqlite3ExprIsInteger(p->pLimit, &n) ){
  1616   1675         sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
  1617   1676         VdbeComment((v, "LIMIT counter"));
  1618   1677         if( n==0 ){
  1619   1678           sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
  1620   1679         }else if( n>=0 && p->nSelectRow>(u64)n ){
  1621   1680           p->nSelectRow = n;
................................................................................
  1664   1723     if( pRet==0 && iCol<p->pEList->nExpr ){
  1665   1724       pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
  1666   1725     }
  1667   1726     return pRet;
  1668   1727   }
  1669   1728   #endif /* SQLITE_OMIT_COMPOUND_SELECT */
  1670   1729   
  1671         -/* Forward reference */
         1730  +#ifndef SQLITE_OMIT_CTE
         1731  +/*
         1732  +** This routine generates VDBE code to compute the content of a WITH RECURSIVE
         1733  +** query of the form:
         1734  +**
         1735  +**   <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
         1736  +**                         \___________/             \_______________/
         1737  +**                           p->pPrior                      p
         1738  +**
         1739  +**
         1740  +** There is exactly one reference to the recursive-table in the FROM clause
         1741  +** of recursive-query, marked with the SrcList->a[].isRecursive flag.
         1742  +**
         1743  +** The setup-query runs once to generate an initial set of rows that go
         1744  +** into a Queue table.  Rows are extracted from the Queue table one by
         1745  +** one.  Each row extracted from Queue is output to pDest.  Then the single
         1746  +** extracted row (now in the iCurrent table) becomes the content of the
         1747  +** recursive-table for a recursive-query run.  The output of the recursive-query
         1748  +** is added back into the Queue table.  Then another row is extracted from Queue
         1749  +** and the iteration continues until the Queue table is empty.
         1750  +**
         1751  +** If the compound query operator is UNION then no duplicate rows are ever
         1752  +** inserted into the Queue table.  The iDistinct table keeps a copy of all rows
         1753  +** that have ever been inserted into Queue and causes duplicates to be
         1754  +** discarded.  If the operator is UNION ALL, then duplicates are allowed.
         1755  +** 
         1756  +** If the query has an ORDER BY, then entries in the Queue table are kept in
         1757  +** ORDER BY order and the first entry is extracted for each cycle.  Without
         1758  +** an ORDER BY, the Queue table is just a FIFO.
         1759  +**
         1760  +** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
         1761  +** have been output to pDest.  A LIMIT of zero means to output no rows and a
         1762  +** negative LIMIT means to output all rows.  If there is also an OFFSET clause
         1763  +** with a positive value, then the first OFFSET outputs are discarded rather
         1764  +** than being sent to pDest.  The LIMIT count does not begin until after OFFSET
         1765  +** rows have been skipped.
         1766  +*/
         1767  +static void generateWithRecursiveQuery(
         1768  +  Parse *pParse,        /* Parsing context */
         1769  +  Select *p,            /* The recursive SELECT to be coded */
         1770  +  SelectDest *pDest     /* What to do with query results */
         1771  +){
         1772  +  SrcList *pSrc = p->pSrc;      /* The FROM clause of the recursive query */
         1773  +  int nCol = p->pEList->nExpr;  /* Number of columns in the recursive table */
         1774  +  Vdbe *v = pParse->pVdbe;      /* The prepared statement under construction */
         1775  +  Select *pSetup = p->pPrior;   /* The setup query */
         1776  +  int addrTop;                  /* Top of the loop */
         1777  +  int addrCont, addrBreak;      /* CONTINUE and BREAK addresses */
         1778  +  int iCurrent = 0;             /* The Current table */
         1779  +  int regCurrent;               /* Register holding Current table */
         1780  +  int iQueue;                   /* The Queue table */
         1781  +  int iDistinct = 0;            /* To ensure unique results if UNION */
         1782  +  int eDest = SRT_Table;        /* How to write to Queue */
         1783  +  SelectDest destQueue;         /* SelectDest targetting the Queue table */
         1784  +  int i;                        /* Loop counter */
         1785  +  int rc;                       /* Result code */
         1786  +  ExprList *pOrderBy;           /* The ORDER BY clause */
         1787  +  Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
         1788  +  int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
         1789  +
         1790  +  /* Obtain authorization to do a recursive query */
         1791  +  if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
         1792  +
         1793  +  /* Process the LIMIT and OFFSET clauses, if they exist */
         1794  +  addrBreak = sqlite3VdbeMakeLabel(v);
         1795  +  computeLimitRegisters(pParse, p, addrBreak);
         1796  +  pLimit = p->pLimit;
         1797  +  pOffset = p->pOffset;
         1798  +  regLimit = p->iLimit;
         1799  +  regOffset = p->iOffset;
         1800  +  p->pLimit = p->pOffset = 0;
         1801  +  p->iLimit = p->iOffset = 0;
         1802  +
         1803  +  /* Locate the cursor number of the Current table */
         1804  +  for(i=0; ALWAYS(i<pSrc->nSrc); i++){
         1805  +    if( pSrc->a[i].isRecursive ){
         1806  +      iCurrent = pSrc->a[i].iCursor;
         1807  +      break;
         1808  +    }
         1809  +  }
         1810  +
         1811  +  /* Detach the ORDER BY clause from the compound SELECT */
         1812  +  pOrderBy = p->pOrderBy;
         1813  +  p->pOrderBy = 0;
         1814  +
         1815  +  /* Allocate cursors numbers for Queue and Distinct.  The cursor number for
         1816  +  ** the Distinct table must be exactly one greater than Queue in order
         1817  +  ** for the SRT_DistTable and SRT_DistQueue destinations to work. */
         1818  +  iQueue = pParse->nTab++;
         1819  +  if( p->op==TK_UNION ){
         1820  +    eDest = pOrderBy ? SRT_DistQueue : SRT_DistTable;
         1821  +    iDistinct = pParse->nTab++;
         1822  +  }else{
         1823  +    eDest = pOrderBy ? SRT_Queue : SRT_Table;
         1824  +  }
         1825  +  sqlite3SelectDestInit(&destQueue, eDest, iQueue);
         1826  +
         1827  +  /* Allocate cursors for Current, Queue, and Distinct. */
         1828  +  regCurrent = ++pParse->nMem;
         1829  +  sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
         1830  +  if( pOrderBy ){
         1831  +    KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pOrderBy, 1);
         1832  +    sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
         1833  +                      (char*)pKeyInfo, P4_KEYINFO);
         1834  +    destQueue.pOrderBy = pOrderBy;
         1835  +  }else{
         1836  +    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
         1837  +  }
         1838  +  VdbeComment((v, "Queue table"));
         1839  +  if( iDistinct ){
         1840  +    p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
         1841  +    p->selFlags |= SF_UsesEphemeral;
         1842  +  }
         1843  +
         1844  +  /* Store the results of the setup-query in Queue. */
         1845  +  rc = sqlite3Select(pParse, pSetup, &destQueue);
         1846  +  if( rc ) goto end_of_recursive_query;
         1847  +
         1848  +  /* Find the next row in the Queue and output that row */
         1849  +  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak);
         1850  +
         1851  +  /* Transfer the next row in Queue over to Current */
         1852  +  sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
         1853  +  if( pOrderBy ){
         1854  +    sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
         1855  +  }else{
         1856  +    sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
         1857  +  }
         1858  +  sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
         1859  +
         1860  +  /* Output the single row in Current */
         1861  +  addrCont = sqlite3VdbeMakeLabel(v);
         1862  +  codeOffset(v, regOffset, addrCont);
         1863  +  selectInnerLoop(pParse, p, p->pEList, iCurrent,
         1864  +      0, 0, pDest, addrCont, addrBreak);
         1865  +  if( regLimit ) sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1);
         1866  +  sqlite3VdbeResolveLabel(v, addrCont);
         1867  +
         1868  +  /* Execute the recursive SELECT taking the single row in Current as
         1869  +  ** the value for the recursive-table. Store the results in the Queue.
         1870  +  */
         1871  +  p->pPrior = 0;
         1872  +  sqlite3Select(pParse, p, &destQueue);
         1873  +  assert( p->pPrior==0 );
         1874  +  p->pPrior = pSetup;
         1875  +
         1876  +  /* Keep running the loop until the Queue is empty */
         1877  +  sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
         1878  +  sqlite3VdbeResolveLabel(v, addrBreak);
         1879  +
         1880  +end_of_recursive_query:
         1881  +  p->pOrderBy = pOrderBy;
         1882  +  p->pLimit = pLimit;
         1883  +  p->pOffset = pOffset;
         1884  +  return;
         1885  +}
         1886  +#endif
         1887  +
         1888  +/* Forward references */
  1672   1889   static int multiSelectOrderBy(
  1673   1890     Parse *pParse,        /* Parsing context */
  1674   1891     Select *p,            /* The right-most of SELECTs to be coded */
  1675   1892     SelectDest *pDest     /* What to do with query results */
  1676   1893   );
  1677   1894   
  1678   1895   
................................................................................
  1716   1933     int rc = SQLITE_OK;   /* Success code from a subroutine */
  1717   1934     Select *pPrior;       /* Another SELECT immediately to our left */
  1718   1935     Vdbe *v;              /* Generate code to this VDBE */
  1719   1936     SelectDest dest;      /* Alternative data destination */
  1720   1937     Select *pDelete = 0;  /* Chain of simple selects to delete */
  1721   1938     sqlite3 *db;          /* Database connection */
  1722   1939   #ifndef SQLITE_OMIT_EXPLAIN
  1723         -  int iSub1;            /* EQP id of left-hand query */
  1724         -  int iSub2;            /* EQP id of right-hand query */
         1940  +  int iSub1 = 0;        /* EQP id of left-hand query */
         1941  +  int iSub2 = 0;        /* EQP id of right-hand query */
  1725   1942   #endif
  1726   1943   
  1727   1944     /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
  1728   1945     ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
  1729   1946     */
  1730   1947     assert( p && p->pPrior );  /* Calling function guarantees this much */
         1948  +  assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
  1731   1949     db = pParse->db;
  1732   1950     pPrior = p->pPrior;
  1733   1951     assert( pPrior->pRightmost!=pPrior );
  1734   1952     assert( pPrior->pRightmost==p->pRightmost );
  1735   1953     dest = *pDest;
  1736   1954     if( pPrior->pOrderBy ){
  1737   1955       sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
................................................................................
  1768   1986       }else{
  1769   1987         sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
  1770   1988           " do not have the same number of result columns", selectOpName(p->op));
  1771   1989       }
  1772   1990       rc = 1;
  1773   1991       goto multi_select_end;
  1774   1992     }
         1993  +
         1994  +#ifndef SQLITE_OMIT_CTE
         1995  +  if( p->selFlags & SF_Recursive ){
         1996  +    generateWithRecursiveQuery(pParse, p, &dest);
         1997  +  }else
         1998  +#endif
  1775   1999   
  1776   2000     /* Compound SELECTs that have an ORDER BY clause are handled separately.
  1777   2001     */
  1778   2002     if( p->pOrderBy ){
  1779   2003       return multiSelectOrderBy(pParse, p, pDest);
  1780         -  }
         2004  +  }else
  1781   2005   
  1782   2006     /* Generate code for the left and right SELECT statements.
  1783   2007     */
  1784   2008     switch( p->op ){
  1785   2009       case TK_ALL: {
  1786   2010         int addr = 0;
  1787   2011         int nLimit;
................................................................................
  1908   2132             generateColumnNames(pParse, 0, pFirst->pEList);
  1909   2133           }
  1910   2134           iBreak = sqlite3VdbeMakeLabel(v);
  1911   2135           iCont = sqlite3VdbeMakeLabel(v);
  1912   2136           computeLimitRegisters(pParse, p, iBreak);
  1913   2137           sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
  1914   2138           iStart = sqlite3VdbeCurrentAddr(v);
  1915         -        selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
         2139  +        selectInnerLoop(pParse, p, p->pEList, unionTab,
  1916   2140                           0, 0, &dest, iCont, iBreak);
  1917   2141           sqlite3VdbeResolveLabel(v, iCont);
  1918   2142           sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
  1919   2143           sqlite3VdbeResolveLabel(v, iBreak);
  1920   2144           sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
  1921   2145         }
  1922   2146         break;
................................................................................
  1986   2210         iCont = sqlite3VdbeMakeLabel(v);
  1987   2211         computeLimitRegisters(pParse, p, iBreak);
  1988   2212         sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
  1989   2213         r1 = sqlite3GetTempReg(pParse);
  1990   2214         iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
  1991   2215         sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
  1992   2216         sqlite3ReleaseTempReg(pParse, r1);
  1993         -      selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
         2217  +      selectInnerLoop(pParse, p, p->pEList, tab1,
  1994   2218                         0, 0, &dest, iCont, iBreak);
  1995   2219         sqlite3VdbeResolveLabel(v, iCont);
  1996   2220         sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
  1997   2221         sqlite3VdbeResolveLabel(v, iBreak);
  1998   2222         sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
  1999   2223         sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
  2000   2224         break;
................................................................................
  2108   2332       sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
  2109   2333       sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
  2110   2334     }
  2111   2335     if( pParse->db->mallocFailed ) return 0;
  2112   2336   
  2113   2337     /* Suppress the first OFFSET entries if there is an OFFSET clause
  2114   2338     */
  2115         -  codeOffset(v, p, iContinue);
         2339  +  codeOffset(v, p->iOffset, iContinue);
  2116   2340   
  2117   2341     switch( pDest->eDest ){
  2118   2342       /* Store the result as data using a unique key.
  2119   2343       */
  2120   2344       case SRT_Table:
  2121   2345       case SRT_EphemTab: {
  2122   2346         int r1 = sqlite3GetTempReg(pParse);
................................................................................
  2836   3060   **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
  2837   3061   **        somewhat by saying that the terms of the ORDER BY clause must
  2838   3062   **        appear as unmodified result columns in the outer query.  But we
  2839   3063   **        have other optimizations in mind to deal with that case.
  2840   3064   **
  2841   3065   **  (21)  The subquery does not use LIMIT or the outer query is not
  2842   3066   **        DISTINCT.  (See ticket [752e1646fc]).
         3067  +**
         3068  +**  (22)  The subquery is not a recursive CTE.
         3069  +**
         3070  +**  (23)  The parent is not a recursive CTE, or the sub-query is not a
         3071  +**        compound query. This restriction is because transforming the
         3072  +**        parent to a compound query confuses the code that handles
         3073  +**        recursive queries in multiSelect().
         3074  +**
  2843   3075   **
  2844   3076   ** In this routine, the "p" parameter is a pointer to the outer query.
  2845   3077   ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
  2846   3078   ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
  2847   3079   **
  2848   3080   ** If flattening is not attempted, this routine is a no-op and returns 0.
  2849   3081   ** If flattening is attempted this routine returns 1.
................................................................................
  2908   3140        return 0;                                           /* Restriction (11) */
  2909   3141     }
  2910   3142     if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
  2911   3143     if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
  2912   3144     if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
  2913   3145        return 0;         /* Restriction (21) */
  2914   3146     }
         3147  +  if( pSub->selFlags & SF_Recursive ) return 0;          /* Restriction (22)  */
         3148  +  if( (p->selFlags & SF_Recursive) && pSub->pPrior ) return 0;       /* (23)  */
  2915   3149   
  2916   3150     /* OBSOLETE COMMENT 1:
  2917   3151     ** Restriction 3:  If the subquery is a join, make sure the subquery is 
  2918   3152     ** not used as the right operand of an outer join.  Examples of why this
  2919   3153     ** is not allowed:
  2920   3154     **
  2921   3155     **         t1 LEFT OUTER JOIN (t2 JOIN t3)
................................................................................
  3389   3623     pNew->pOrderBy = 0;
  3390   3624     p->pPrior = 0;
  3391   3625     pNew->pLimit = 0;
  3392   3626     pNew->pOffset = 0;
  3393   3627     return WRC_Continue;
  3394   3628   }
  3395   3629   
         3630  +#ifndef SQLITE_OMIT_CTE
         3631  +/*
         3632  +** Argument pWith (which may be NULL) points to a linked list of nested 
         3633  +** WITH contexts, from inner to outermost. If the table identified by 
         3634  +** FROM clause element pItem is really a common-table-expression (CTE) 
         3635  +** then return a pointer to the CTE definition for that table. Otherwise
         3636  +** return NULL.
         3637  +**
         3638  +** If a non-NULL value is returned, set *ppContext to point to the With
         3639  +** object that the returned CTE belongs to.
         3640  +*/
         3641  +static struct Cte *searchWith(
         3642  +  With *pWith,                    /* Current outermost WITH clause */
         3643  +  struct SrcList_item *pItem,     /* FROM clause element to resolve */
         3644  +  With **ppContext                /* OUT: WITH clause return value belongs to */
         3645  +){
         3646  +  const char *zName;
         3647  +  if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
         3648  +    With *p;
         3649  +    for(p=pWith; p; p=p->pOuter){
         3650  +      int i;
         3651  +      for(i=0; i<p->nCte; i++){
         3652  +        if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
         3653  +          *ppContext = p;
         3654  +          return &p->a[i];
         3655  +        }
         3656  +      }
         3657  +    }
         3658  +  }
         3659  +  return 0;
         3660  +}
         3661  +
         3662  +/* The code generator maintains a stack of active WITH clauses
         3663  +** with the inner-most WITH clause being at the top of the stack.
         3664  +**
         3665  +** This routine pushes the WITH clause passed as the second argument
         3666  +** onto the top of the stack. If argument bFree is true, then this
         3667  +** WITH clause will never be popped from the stack. In this case it
         3668  +** should be freed along with the Parse object. In other cases, when
         3669  +** bFree==0, the With object will be freed along with the SELECT 
         3670  +** statement with which it is associated.
         3671  +*/
         3672  +void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
         3673  +  assert( bFree==0 || pParse->pWith==0 );
         3674  +  if( pWith ){
         3675  +    pWith->pOuter = pParse->pWith;
         3676  +    pParse->pWith = pWith;
         3677  +    pParse->bFreeWith = bFree;
         3678  +  }
         3679  +}
         3680  +
         3681  +/*
         3682  +** This function checks if argument pFrom refers to a CTE declared by 
         3683  +** a WITH clause on the stack currently maintained by the parser. And,
         3684  +** if currently processing a CTE expression, if it is a recursive
         3685  +** reference to the current CTE.
         3686  +**
         3687  +** If pFrom falls into either of the two categories above, pFrom->pTab
         3688  +** and other fields are populated accordingly. The caller should check
         3689  +** (pFrom->pTab!=0) to determine whether or not a successful match
         3690  +** was found.
         3691  +**
         3692  +** Whether or not a match is found, SQLITE_OK is returned if no error
         3693  +** occurs. If an error does occur, an error message is stored in the
         3694  +** parser and some error code other than SQLITE_OK returned.
         3695  +*/
         3696  +static int withExpand(
         3697  +  Walker *pWalker, 
         3698  +  struct SrcList_item *pFrom
         3699  +){
         3700  +  Parse *pParse = pWalker->pParse;
         3701  +  sqlite3 *db = pParse->db;
         3702  +  struct Cte *pCte;               /* Matched CTE (or NULL if no match) */
         3703  +  With *pWith;                    /* WITH clause that pCte belongs to */
         3704  +
         3705  +  assert( pFrom->pTab==0 );
         3706  +
         3707  +  pCte = searchWith(pParse->pWith, pFrom, &pWith);
         3708  +  if( pCte ){
         3709  +    Table *pTab;
         3710  +    ExprList *pEList;
         3711  +    Select *pSel;
         3712  +    Select *pLeft;                /* Left-most SELECT statement */
         3713  +    int bMayRecursive;            /* True if compound joined by UNION [ALL] */
         3714  +    With *pSavedWith;             /* Initial value of pParse->pWith */
         3715  +
         3716  +    /* If pCte->zErr is non-NULL at this point, then this is an illegal
         3717  +    ** recursive reference to CTE pCte. Leave an error in pParse and return
         3718  +    ** early. If pCte->zErr is NULL, then this is not a recursive reference.
         3719  +    ** In this case, proceed.  */
         3720  +    if( pCte->zErr ){
         3721  +      sqlite3ErrorMsg(pParse, pCte->zErr, pCte->zName);
         3722  +      return SQLITE_ERROR;
         3723  +    }
         3724  +
         3725  +    assert( pFrom->pTab==0 );
         3726  +    pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
         3727  +    if( pTab==0 ) return WRC_Abort;
         3728  +    pTab->nRef = 1;
         3729  +    pTab->zName = sqlite3DbStrDup(db, pCte->zName);
         3730  +    pTab->iPKey = -1;
         3731  +    pTab->nRowEst = 1048576;
         3732  +    pTab->tabFlags |= TF_Ephemeral;
         3733  +    pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
         3734  +    if( db->mallocFailed ) return SQLITE_NOMEM;
         3735  +    assert( pFrom->pSelect );
         3736  +
         3737  +    /* Check if this is a recursive CTE. */
         3738  +    pSel = pFrom->pSelect;
         3739  +    bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
         3740  +    if( bMayRecursive ){
         3741  +      int i;
         3742  +      SrcList *pSrc = pFrom->pSelect->pSrc;
         3743  +      for(i=0; i<pSrc->nSrc; i++){
         3744  +        struct SrcList_item *pItem = &pSrc->a[i];
         3745  +        if( pItem->zDatabase==0 
         3746  +         && pItem->zName!=0 
         3747  +         && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
         3748  +          ){
         3749  +          pItem->pTab = pTab;
         3750  +          pItem->isRecursive = 1;
         3751  +          pTab->nRef++;
         3752  +          pSel->selFlags |= SF_Recursive;
         3753  +        }
         3754  +      }
         3755  +    }
         3756  +
         3757  +    /* Only one recursive reference is permitted. */ 
         3758  +    if( pTab->nRef>2 ){
         3759  +      sqlite3ErrorMsg(
         3760  +          pParse, "multiple references to recursive table: %s", pCte->zName
         3761  +      );
         3762  +      return SQLITE_ERROR;
         3763  +    }
         3764  +    assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 ));
         3765  +
         3766  +    pCte->zErr = "circular reference: %s";
         3767  +    pSavedWith = pParse->pWith;
         3768  +    pParse->pWith = pWith;
         3769  +    sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
         3770  +
         3771  +    for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
         3772  +    pEList = pLeft->pEList;
         3773  +    if( pCte->pCols ){
         3774  +      if( pEList->nExpr!=pCte->pCols->nExpr ){
         3775  +        sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
         3776  +            pCte->zName, pEList->nExpr, pCte->pCols->nExpr
         3777  +        );
         3778  +        pParse->pWith = pSavedWith;
         3779  +        return SQLITE_ERROR;
         3780  +      }
         3781  +      pEList = pCte->pCols;
         3782  +    }
         3783  +
         3784  +    selectColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
         3785  +    if( bMayRecursive ){
         3786  +      if( pSel->selFlags & SF_Recursive ){
         3787  +        pCte->zErr = "multiple recursive references: %s";
         3788  +      }else{
         3789  +        pCte->zErr = "recursive reference in a subquery: %s";
         3790  +      }
         3791  +      sqlite3WalkSelect(pWalker, pSel);
         3792  +    }
         3793  +    pCte->zErr = 0;
         3794  +    pParse->pWith = pSavedWith;
         3795  +  }
         3796  +
         3797  +  return SQLITE_OK;
         3798  +}
         3799  +#endif
         3800  +
         3801  +#ifndef SQLITE_OMIT_CTE
         3802  +/*
         3803  +** If the SELECT passed as the second argument has an associated WITH 
         3804  +** clause, pop it from the stack stored as part of the Parse object.
         3805  +**
         3806  +** This function is used as the xSelectCallback2() callback by
         3807  +** sqlite3SelectExpand() when walking a SELECT tree to resolve table
         3808  +** names and other FROM clause elements. 
         3809  +*/
         3810  +static void selectPopWith(Walker *pWalker, Select *p){
         3811  +  Parse *pParse = pWalker->pParse;
         3812  +  if( p->pWith ){
         3813  +    assert( pParse->pWith==p->pWith );
         3814  +    pParse->pWith = p->pWith->pOuter;
         3815  +  }
         3816  +}
         3817  +#else
         3818  +#define selectPopWith 0
         3819  +#endif
         3820  +
  3396   3821   /*
  3397   3822   ** This routine is a Walker callback for "expanding" a SELECT statement.
  3398   3823   ** "Expanding" means to do the following:
  3399   3824   **
  3400   3825   **    (1)  Make sure VDBE cursor numbers have been assigned to every
  3401   3826   **         element of the FROM clause.
  3402   3827   **
................................................................................
  3432   3857       return WRC_Abort;
  3433   3858     }
  3434   3859     if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
  3435   3860       return WRC_Prune;
  3436   3861     }
  3437   3862     pTabList = p->pSrc;
  3438   3863     pEList = p->pEList;
         3864  +  sqlite3WithPush(pParse, p->pWith, 0);
  3439   3865   
  3440   3866     /* Make sure cursor numbers have been assigned to all entries in
  3441   3867     ** the FROM clause of the SELECT statement.
  3442   3868     */
  3443   3869     sqlite3SrcListAssignCursors(pParse, pTabList);
  3444   3870   
  3445   3871     /* Look up every table named in the FROM clause of the select.  If
  3446   3872     ** an entry of the FROM clause is a subquery instead of a table or view,
  3447   3873     ** then create a transient table structure to describe the subquery.
  3448   3874     */
  3449   3875     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  3450   3876       Table *pTab;
         3877  +    assert( pFrom->isRecursive==0 || pFrom->pTab );
         3878  +    if( pFrom->isRecursive ) continue;
  3451   3879       if( pFrom->pTab!=0 ){
  3452   3880         /* This statement has already been prepared.  There is no need
  3453   3881         ** to go further. */
  3454   3882         assert( i==0 );
         3883  +#ifndef SQLITE_OMIT_CTE
         3884  +      selectPopWith(pWalker, p);
         3885  +#endif
  3455   3886         return WRC_Prune;
  3456   3887       }
         3888  +#ifndef SQLITE_OMIT_CTE
         3889  +    if( withExpand(pWalker, pFrom) ) return WRC_Abort;
         3890  +    if( pFrom->pTab ) {} else
         3891  +#endif
  3457   3892       if( pFrom->zName==0 ){
  3458   3893   #ifndef SQLITE_OMIT_SUBQUERY
  3459   3894         Select *pSel = pFrom->pSelect;
  3460   3895         /* A sub-query in the FROM clause of a SELECT */
  3461   3896         assert( pSel!=0 );
  3462   3897         assert( pFrom->pTab==0 );
  3463   3898         sqlite3WalkSelect(pWalker, pSel);
................................................................................
  3712   4147     w.xExprCallback = exprWalkNoop;
  3713   4148     w.pParse = pParse;
  3714   4149     if( pParse->hasCompound ){
  3715   4150       w.xSelectCallback = convertCompoundSelectToSubquery;
  3716   4151       sqlite3WalkSelect(&w, pSelect);
  3717   4152     }
  3718   4153     w.xSelectCallback = selectExpander;
         4154  +  w.xSelectCallback2 = selectPopWith;
  3719   4155     sqlite3WalkSelect(&w, pSelect);
  3720   4156   }
  3721   4157   
  3722   4158   
  3723   4159   #ifndef SQLITE_OMIT_SUBQUERY
  3724   4160   /*
  3725   4161   ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
................................................................................
  3730   4166   ** of that subquery.
  3731   4167   **
  3732   4168   ** The Table structure that represents the result set was constructed
  3733   4169   ** by selectExpander() but the type and collation information was omitted
  3734   4170   ** at that point because identifiers had not yet been resolved.  This
  3735   4171   ** routine is called after identifier resolution.
  3736   4172   */
  3737         -static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
         4173  +static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
  3738   4174     Parse *pParse;
  3739   4175     int i;
  3740   4176     SrcList *pTabList;
  3741   4177     struct SrcList_item *pFrom;
  3742   4178   
  3743   4179     assert( p->selFlags & SF_Resolved );
  3744   4180     if( (p->selFlags & SF_HasTypeInfo)==0 ){
................................................................................
  3746   4182       pParse = pWalker->pParse;
  3747   4183       pTabList = p->pSrc;
  3748   4184       for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  3749   4185         Table *pTab = pFrom->pTab;
  3750   4186         if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
  3751   4187           /* A sub-query in the FROM clause of a SELECT */
  3752   4188           Select *pSel = pFrom->pSelect;
  3753         -        assert( pSel );
  3754         -        while( pSel->pPrior ) pSel = pSel->pPrior;
  3755         -        selectAddColumnTypeAndCollation(pParse, pTab, pSel);
         4189  +        if( pSel ){
         4190  +          while( pSel->pPrior ) pSel = pSel->pPrior;
         4191  +          selectAddColumnTypeAndCollation(pParse, pTab, pSel);
         4192  +        }
  3756   4193         }
  3757   4194       }
  3758   4195     }
  3759         -  return WRC_Continue;
  3760   4196   }
  3761   4197   #endif
  3762   4198   
  3763   4199   
  3764   4200   /*
  3765   4201   ** This routine adds datatype and collating sequence information to
  3766   4202   ** the Table structures of all FROM-clause subqueries in a
................................................................................
  3768   4204   **
  3769   4205   ** Use this routine after name resolution.
  3770   4206   */
  3771   4207   static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
  3772   4208   #ifndef SQLITE_OMIT_SUBQUERY
  3773   4209     Walker w;
  3774   4210     memset(&w, 0, sizeof(w));
  3775         -  w.xSelectCallback = selectAddSubqueryTypeInfo;
         4211  +  w.xSelectCallback2 = selectAddSubqueryTypeInfo;
  3776   4212     w.xExprCallback = exprWalkNoop;
  3777   4213     w.pParse = pParse;
  3778         -  w.bSelectDepthFirst = 1;
  3779   4214     sqlite3WalkSelect(&w, pSelect);
  3780   4215   #endif
  3781   4216   }
  3782   4217   
  3783   4218   
  3784   4219   /*
  3785   4220   ** This routine sets up a SELECT statement for processing.  The
................................................................................
  3843   4278         Expr *pE = pFunc->pExpr;
  3844   4279         assert( !ExprHasProperty(pE, EP_xIsSelect) );
  3845   4280         if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
  3846   4281           sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
  3847   4282              "argument");
  3848   4283           pFunc->iDistinct = -1;
  3849   4284         }else{
  3850         -        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
         4285  +        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0);
  3851   4286           sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
  3852   4287                             (char*)pKeyInfo, P4_KEYINFO);
  3853   4288         }
  3854   4289       }
  3855   4290     }
  3856   4291   }
  3857   4292   
................................................................................
  3976   4411   #else
  3977   4412   # define explainSimpleCount(a,b,c)
  3978   4413   #endif
  3979   4414   
  3980   4415   /*
  3981   4416   ** Generate code for the SELECT statement given in the p argument.  
  3982   4417   **
  3983         -** The results are distributed in various ways depending on the
  3984         -** contents of the SelectDest structure pointed to by argument pDest
  3985         -** as follows:
  3986         -**
  3987         -**     pDest->eDest    Result
  3988         -**     ------------    -------------------------------------------
  3989         -**     SRT_Output      Generate a row of output (using the OP_ResultRow
  3990         -**                     opcode) for each row in the result set.
  3991         -**
  3992         -**     SRT_Mem         Only valid if the result is a single column.
  3993         -**                     Store the first column of the first result row
  3994         -**                     in register pDest->iSDParm then abandon the rest
  3995         -**                     of the query.  This destination implies "LIMIT 1".
  3996         -**
  3997         -**     SRT_Set         The result must be a single column.  Store each
  3998         -**                     row of result as the key in table pDest->iSDParm. 
  3999         -**                     Apply the affinity pDest->affSdst before storing
  4000         -**                     results.  Used to implement "IN (SELECT ...)".
  4001         -**
  4002         -**     SRT_Union       Store results as a key in a temporary table 
  4003         -**                     identified by pDest->iSDParm.
  4004         -**
  4005         -**     SRT_Except      Remove results from the temporary table pDest->iSDParm.
  4006         -**
  4007         -**     SRT_Table       Store results in temporary table pDest->iSDParm.
  4008         -**                     This is like SRT_EphemTab except that the table
  4009         -**                     is assumed to already be open.
  4010         -**
  4011         -**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
  4012         -**                     the result there. The cursor is left open after
  4013         -**                     returning.  This is like SRT_Table except that
  4014         -**                     this destination uses OP_OpenEphemeral to create
  4015         -**                     the table first.
  4016         -**
  4017         -**     SRT_Coroutine   Generate a co-routine that returns a new row of
  4018         -**                     results each time it is invoked.  The entry point
  4019         -**                     of the co-routine is stored in register pDest->iSDParm.
  4020         -**
  4021         -**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
  4022         -**                     set is not empty.
  4023         -**
  4024         -**     SRT_Discard     Throw the results away.  This is used by SELECT
  4025         -**                     statements within triggers whose only purpose is
  4026         -**                     the side-effects of functions.
         4418  +** The results are returned according to the SelectDest structure.
         4419  +** See comments in sqliteInt.h for further information.
  4027   4420   **
  4028   4421   ** This routine returns the number of errors.  If any errors are
  4029   4422   ** encountered, then an appropriate error message is left in
  4030   4423   ** pParse->zErrMsg.
  4031   4424   **
  4032   4425   ** This routine does NOT free the Select structure passed in.  The
  4033   4426   ** calling function needs to do that.
................................................................................
  4294   4687     ** extracted in pre-sorted order.  If that is the case, then the
  4295   4688     ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
  4296   4689     ** we figure out that the sorting index is not needed.  The addrSortIndex
  4297   4690     ** variable is used to facilitate that change.
  4298   4691     */
  4299   4692     if( pOrderBy ){
  4300   4693       KeyInfo *pKeyInfo;
  4301         -    pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
         4694  +    pKeyInfo = keyInfoFromExprList(pParse, pOrderBy, 0);
  4302   4695       pOrderBy->iECursor = pParse->nTab++;
  4303   4696       p->addrOpenEphm[2] = addrSortIndex =
  4304   4697         sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
  4305   4698                              pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
  4306   4699                              (char*)pKeyInfo, P4_KEYINFO);
  4307   4700     }else{
  4308   4701       addrSortIndex = -1;
................................................................................
  4326   4719   
  4327   4720     /* Open a virtual index to use for the distinct set.
  4328   4721     */
  4329   4722     if( p->selFlags & SF_Distinct ){
  4330   4723       sDistinct.tabTnct = pParse->nTab++;
  4331   4724       sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
  4332   4725                                   sDistinct.tabTnct, 0, 0,
  4333         -                                (char*)keyInfoFromExprList(pParse, p->pEList),
         4726  +                                (char*)keyInfoFromExprList(pParse, p->pEList, 0),
  4334   4727                                   P4_KEYINFO);
  4335   4728       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  4336   4729       sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
  4337   4730     }else{
  4338   4731       sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
  4339   4732     }
  4340   4733   
................................................................................
  4360   4753       */
  4361   4754       if( addrSortIndex>=0 && pOrderBy==0 ){
  4362   4755         sqlite3VdbeChangeToNoop(v, addrSortIndex);
  4363   4756         p->addrOpenEphm[2] = -1;
  4364   4757       }
  4365   4758   
  4366   4759       /* Use the standard inner loop. */
  4367         -    selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
         4760  +    selectInnerLoop(pParse, p, pEList, -1, pOrderBy, &sDistinct, pDest,
  4368   4761                       sqlite3WhereContinueLabel(pWInfo),
  4369   4762                       sqlite3WhereBreakLabel(pWInfo));
  4370   4763   
  4371   4764       /* End the database scan loop.
  4372   4765       */
  4373   4766       sqlite3WhereEnd(pWInfo);
  4374   4767     }else{
................................................................................
  4450   4843   
  4451   4844         /* If there is a GROUP BY clause we might need a sorting index to
  4452   4845         ** implement it.  Allocate that sorting index now.  If it turns out
  4453   4846         ** that we do not need it after all, the OP_SorterOpen instruction
  4454   4847         ** will be converted into a Noop.  
  4455   4848         */
  4456   4849         sAggInfo.sortingIdx = pParse->nTab++;
  4457         -      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
         4850  +      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0);
  4458   4851         addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
  4459   4852             sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
  4460   4853             0, (char*)pKeyInfo, P4_KEYINFO);
  4461   4854   
  4462   4855         /* Initialize memory locations used by GROUP BY aggregate processing
  4463   4856         */
  4464   4857         iUseFlag = ++pParse->nMem;
................................................................................
  4632   5025         sqlite3VdbeResolveLabel(v, addrOutputRow);
  4633   5026         addrOutputRow = sqlite3VdbeCurrentAddr(v);
  4634   5027         sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
  4635   5028         VdbeComment((v, "Groupby result generator entry point"));
  4636   5029         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  4637   5030         finalizeAggFunctions(pParse, &sAggInfo);
  4638   5031         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
  4639         -      selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
         5032  +      selectInnerLoop(pParse, p, p->pEList, -1, pOrderBy,
  4640   5033                         &sDistinct, pDest,
  4641   5034                         addrOutputRow+1, addrSetAbort);
  4642   5035         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  4643   5036         VdbeComment((v, "end groupby result generator"));
  4644   5037   
  4645   5038         /* Generate a subroutine that will reset the group-by accumulator
  4646   5039         */
................................................................................
  4775   5168           }
  4776   5169           sqlite3WhereEnd(pWInfo);
  4777   5170           finalizeAggFunctions(pParse, &sAggInfo);
  4778   5171         }
  4779   5172   
  4780   5173         pOrderBy = 0;
  4781   5174         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
  4782         -      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0, 
         5175  +      selectInnerLoop(pParse, p, p->pEList, -1, 0, 0, 
  4783   5176                         pDest, addrEnd, addrEnd);
  4784   5177         sqlite3ExprListDelete(db, pDel);
  4785   5178       }
  4786   5179       sqlite3VdbeResolveLabel(v, addrEnd);
  4787   5180       
  4788   5181     } /* endif aggregate query */
  4789   5182   

Changes to src/shell.c.

   593    593   
   594    594   /*
   595    595   ** Output the given string with characters that are special to
   596    596   ** HTML escaped.
   597    597   */
   598    598   static void output_html_string(FILE *out, const char *z){
   599    599     int i;
          600  +  if( z==0 ) z = "";
   600    601     while( *z ){
   601    602       for(i=0;   z[i] 
   602    603               && z[i]!='<' 
   603    604               && z[i]!='&' 
   604    605               && z[i]!='>' 
   605    606               && z[i]!='\"' 
   606    607               && z[i]!='\'';
................................................................................
  1172   1173   **
  1173   1174   **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
  1174   1175   **       all opcodes that occur between the p2 jump destination and the opcode
  1175   1176   **       itself by 2 spaces.
  1176   1177   **
  1177   1178   **     * For each "Goto", if the jump destination is earlier in the program
  1178   1179   **       and ends on one of:
  1179         -**          Yield  SeekGt  SeekLt  RowSetRead
         1180  +**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
  1180   1181   **       then indent all opcodes between the earlier instruction
  1181   1182   **       and "Goto" by 2 spaces.
  1182   1183   */
  1183   1184   static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
  1184   1185     const char *zSql;               /* The text of the SQL statement */
  1185   1186     const char *z;                  /* Used to check if this is an EXPLAIN */
  1186   1187     int *abYield = 0;               /* True if op is an OP_Yield */
  1187   1188     int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
  1188   1189     int iOp;                        /* Index of operation in p->aiIndent[] */
  1189   1190   
  1190   1191     const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
  1191         -  const char *azYield[] = { "Yield", "SeekLt", "SeekGt", "RowSetRead", 0 };
         1192  +  const char *azYield[] = { "Yield", "SeekLt", "SeekGt", "RowSetRead", "Rewind", 0 };
  1192   1193     const char *azGoto[] = { "Goto", 0 };
  1193   1194   
  1194   1195     /* Try to figure out if this is really an EXPLAIN statement. If this
  1195   1196     ** cannot be verified, return early.  */
  1196   1197     zSql = sqlite3_sql(pSql);
  1197   1198     if( zSql==0 ) return;
  1198   1199     for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
................................................................................
  1221   1222       p->aiIndent[iOp] = 0;
  1222   1223       p->nIndent = iOp+1;
  1223   1224   
  1224   1225       if( str_in_array(zOp, azNext) ){
  1225   1226         for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
  1226   1227       }
  1227   1228       if( str_in_array(zOp, azGoto) && p2op<p->nIndent && abYield[p2op] ){
  1228         -      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
         1229  +      for(i=p2op+1; i<iOp; i++) p->aiIndent[i] += 2;
  1229   1230       }
  1230   1231     }
  1231   1232   
  1232   1233     p->iIndent = 0;
  1233   1234     sqlite3_free(abYield);
  1234   1235     sqlite3_reset(pSql);
  1235   1236   }

Changes to src/sqlite.h.in.

  2391   2391   ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
  2392   2392   ** select random [ROWID | ROWIDs] when inserting new records into a table that
  2393   2393   ** already uses the largest possible [ROWID].  The PRNG is also used for
  2394   2394   ** the build-in random() and randomblob() SQL functions.  This interface allows
  2395   2395   ** applications to access the same PRNG for other purposes.
  2396   2396   **
  2397   2397   ** ^A call to this routine stores N bytes of randomness into buffer P.
         2398  +** ^If N is less than one, then P can be a NULL pointer.
  2398   2399   **
  2399         -** ^The first time this routine is invoked (either internally or by
  2400         -** the application) the PRNG is seeded using randomness obtained
  2401         -** from the xRandomness method of the default [sqlite3_vfs] object.
  2402         -** ^On all subsequent invocations, the pseudo-randomness is generated
         2400  +** ^If this routine has not been previously called or if the previous
         2401  +** call had N less than one, then the PRNG is seeded using randomness
         2402  +** obtained from the xRandomness method of the default [sqlite3_vfs] object.
         2403  +** ^If the previous call to this routine had an N of 1 or more then
         2404  +** the pseudo-randomness is generated
  2403   2405   ** internally and without recourse to the [sqlite3_vfs] xRandomness
  2404   2406   ** method.
  2405   2407   */
  2406   2408   void sqlite3_randomness(int N, void *P);
  2407   2409   
  2408   2410   /*
  2409   2411   ** CAPI3REF: Compile-Time Authorization Callbacks
................................................................................
  2555   2557   #define SQLITE_REINDEX              27   /* Index Name      NULL            */
  2556   2558   #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
  2557   2559   #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
  2558   2560   #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
  2559   2561   #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
  2560   2562   #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
  2561   2563   #define SQLITE_COPY                  0   /* No longer used */
         2564  +#define SQLITE_RECURSIVE            33   /* NULL            NULL            */
  2562   2565   
  2563   2566   /*
  2564   2567   ** CAPI3REF: Tracing And Profiling Functions
  2565   2568   **
  2566   2569   ** These routines register callback functions that can be used for
  2567   2570   ** tracing and profiling the execution of SQL statements.
  2568   2571   **

Changes to src/sqliteInt.h.

   757    757   typedef struct TriggerPrg TriggerPrg;
   758    758   typedef struct TriggerStep TriggerStep;
   759    759   typedef struct UnpackedRecord UnpackedRecord;
   760    760   typedef struct VTable VTable;
   761    761   typedef struct VtabCtx VtabCtx;
   762    762   typedef struct Walker Walker;
   763    763   typedef struct WhereInfo WhereInfo;
          764  +typedef struct With With;
   764    765   
   765    766   /*
   766    767   ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
   767    768   ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
   768    769   ** pointer types (i.e. FuncDef) defined above.
   769    770   */
   770    771   #include "btree.h"
................................................................................
  1060   1061   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
  1061   1062   ** selectively disable various optimizations.
  1062   1063   */
  1063   1064   #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
  1064   1065   #define SQLITE_ColumnCache    0x0002   /* Column cache */
  1065   1066   #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
  1066   1067   #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
  1067         -#define SQLITE_IdxRealAsInt   0x0010   /* Store REAL as INT in indices */
         1068  +/*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
  1068   1069   #define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
  1069   1070   #define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
  1070   1071   #define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
  1071   1072   #define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
  1072   1073   #define SQLITE_Transitive     0x0200   /* Transitive constraints */
  1073   1074   #define SQLITE_OmitNoopJoin   0x0400   /* Omit unused tables in joins */
  1074   1075   #define SQLITE_Stat3          0x0800   /* Use the SQLITE_STAT3 table */
................................................................................
  1432   1433   #endif
  1433   1434     Trigger *pTrigger;   /* List of triggers stored in pSchema */
  1434   1435     Schema *pSchema;     /* Schema that contains this table */
  1435   1436     Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
  1436   1437   };
  1437   1438   
  1438   1439   /*
  1439         -** Allowed values for Tabe.tabFlags.
         1440  +** Allowed values for Table.tabFlags.
  1440   1441   */
  1441   1442   #define TF_Readonly        0x01    /* Read-only system table */
  1442   1443   #define TF_Ephemeral       0x02    /* An ephemeral table */
  1443   1444   #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
  1444   1445   #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
  1445   1446   #define TF_Virtual         0x10    /* Is a virtual table */
  1446   1447   #define TF_WithoutRowid    0x20    /* No rowid used. PRIMARY KEY is the key */
................................................................................
  2021   2022       Select *pSelect;  /* A SELECT statement used in place of a table name */
  2022   2023       int addrFillSub;  /* Address of subroutine to manifest a subquery */
  2023   2024       int regReturn;    /* Register holding return address of addrFillSub */
  2024   2025       u8 jointype;      /* Type of join between this able and the previous */
  2025   2026       unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
  2026   2027       unsigned isCorrelated :1;  /* True if sub-query is correlated */
  2027   2028       unsigned viaCoroutine :1;  /* Implemented as a co-routine */
         2029  +    unsigned isRecursive :1;   /* True for recursive reference in WITH */
  2028   2030   #ifndef SQLITE_OMIT_EXPLAIN
  2029   2031       u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
  2030   2032   #endif
  2031   2033       int iCursor;      /* The VDBE cursor number used to access this table */
  2032   2034       Expr *pOn;        /* The ON clause of a join */
  2033   2035       IdList *pUsing;   /* The USING clause of a join */
  2034   2036       Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
................................................................................
  2147   2149     Expr *pHaving;         /* The HAVING clause */
  2148   2150     ExprList *pOrderBy;    /* The ORDER BY clause */
  2149   2151     Select *pPrior;        /* Prior select in a compound select statement */
  2150   2152     Select *pNext;         /* Next select to the left in a compound */
  2151   2153     Select *pRightmost;    /* Right-most select in a compound select statement */
  2152   2154     Expr *pLimit;          /* LIMIT expression. NULL means not used. */
  2153   2155     Expr *pOffset;         /* OFFSET expression. NULL means not used. */
         2156  +  With *pWith;           /* WITH clause attached to this select. Or NULL. */
  2154   2157   };
  2155   2158   
  2156   2159   /*
  2157   2160   ** Allowed values for Select.selFlags.  The "SF" prefix stands for
  2158   2161   ** "Select Flag".
  2159   2162   */
  2160   2163   #define SF_Distinct        0x0001  /* Output should be DISTINCT */
................................................................................
  2164   2167   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
  2165   2168   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
  2166   2169   #define SF_UseSorter       0x0040  /* Sort using a sorter */
  2167   2170   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  2168   2171   #define SF_Materialize     0x0100  /* Force materialization of views */
  2169   2172   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  2170   2173   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
         2174  +#define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
  2171   2175   
  2172   2176   
  2173   2177   /*
  2174         -** The results of a select can be distributed in several ways.  The
  2175         -** "SRT" prefix means "SELECT Result Type".
         2178  +** The results of a SELECT can be distributed in several ways, as defined
         2179  +** by one of the following macros.  The "SRT" prefix means "SELECT Result
         2180  +** Type".
         2181  +**
         2182  +**     SRT_Union       Store results as a key in a temporary index 
         2183  +**                     identified by pDest->iSDParm.
         2184  +**
         2185  +**     SRT_Except      Remove results from the temporary index pDest->iSDParm.
         2186  +**
         2187  +**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
         2188  +**                     set is not empty.
         2189  +**
         2190  +**     SRT_Discard     Throw the results away.  This is used by SELECT
         2191  +**                     statements within triggers whose only purpose is
         2192  +**                     the side-effects of functions.
         2193  +**
         2194  +** All of the above are free to ignore their ORDER BY clause. Those that
         2195  +** follow must honor the ORDER BY clause.
         2196  +**
         2197  +**     SRT_Output      Generate a row of output (using the OP_ResultRow
         2198  +**                     opcode) for each row in the result set.
         2199  +**
         2200  +**     SRT_Mem         Only valid if the result is a single column.
         2201  +**                     Store the first column of the first result row
         2202  +**                     in register pDest->iSDParm then abandon the rest
         2203  +**                     of the query.  This destination implies "LIMIT 1".
         2204  +**
         2205  +**     SRT_Set         The result must be a single column.  Store each
         2206  +**                     row of result as the key in table pDest->iSDParm. 
         2207  +**                     Apply the affinity pDest->affSdst before storing
         2208  +**                     results.  Used to implement "IN (SELECT ...)".
         2209  +**
         2210  +**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
         2211  +**                     the result there. The cursor is left open after
         2212  +**                     returning.  This is like SRT_Table except that
         2213  +**                     this destination uses OP_OpenEphemeral to create
         2214  +**                     the table first.
         2215  +**
         2216  +**     SRT_Coroutine   Generate a co-routine that returns a new row of
         2217  +**                     results each time it is invoked.  The entry point
         2218  +**                     of the co-routine is stored in register pDest->iSDParm
         2219  +**                     and the result row is stored in pDest->nDest registers
         2220  +**                     starting with pDest->iSdst.
         2221  +**
         2222  +**     SRT_Table       Store results in temporary table pDest->iSDParm.
         2223  +**                     This is like SRT_EphemTab except that the table
         2224  +**                     is assumed to already be open.
         2225  +**
         2226  +**     SRT_DistTable   Store results in a temporary table pDest->iSDParm.
         2227  +**                     But also use temporary table pDest->iSDParm+1 as
         2228  +**                     a record of all prior results and ignore any duplicate
         2229  +**                     rows.  Name means:  "Distinct Table".
         2230  +**
         2231  +**     SRT_Queue       Store results in priority queue pDest->iSDParm (really
         2232  +**                     an index).  Append a sequence number so that all entries
         2233  +**                     are distinct.
         2234  +**
         2235  +**     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
         2236  +**                     the same record has never been stored before.  The
         2237  +**                     index at pDest->iSDParm+1 hold all prior stores.
  2176   2238   */
  2177   2239   #define SRT_Union        1  /* Store result as keys in an index */
  2178   2240   #define SRT_Except       2  /* Remove result from a UNION index */
  2179   2241   #define SRT_Exists       3  /* Store 1 if the result is not empty */
  2180   2242   #define SRT_Discard      4  /* Do not save the results anywhere */
  2181   2243   
  2182   2244   /* The ORDER BY clause is ignored for all of the above */
  2183   2245   #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
  2184   2246   
  2185   2247   #define SRT_Output       5  /* Output each row of result */
  2186   2248   #define SRT_Mem          6  /* Store result in a memory cell */
  2187   2249   #define SRT_Set          7  /* Store results as keys in an index */
  2188         -#define SRT_Table        8  /* Store result as data with an automatic rowid */
  2189         -#define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
  2190         -#define SRT_Coroutine   10  /* Generate a single row of result */
         2250  +#define SRT_EphemTab     8  /* Create transient tab and store like SRT_Table */
         2251  +#define SRT_Coroutine    9  /* Generate a single row of result */
         2252  +#define SRT_Table       10  /* Store result as data with an automatic rowid */
         2253  +#define SRT_DistTable   11  /* Like SRT_Table, but unique results only */
         2254  +#define SRT_Queue       12  /* Store result in an queue */
         2255  +#define SRT_DistQueue   13  /* Like SRT_Queue, but unique results only */
  2191   2256   
  2192   2257   /*
  2193   2258   ** An instance of this object describes where to put of the results of
  2194   2259   ** a SELECT statement.
  2195   2260   */
  2196   2261   struct SelectDest {
  2197         -  u8 eDest;         /* How to dispose of the results.  On of SRT_* above. */
  2198         -  char affSdst;     /* Affinity used when eDest==SRT_Set */
  2199         -  int iSDParm;      /* A parameter used by the eDest disposal method */
  2200         -  int iSdst;        /* Base register where results are written */
  2201         -  int nSdst;        /* Number of registers allocated */
         2262  +  u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
         2263  +  char affSdst;        /* Affinity used when eDest==SRT_Set */
         2264  +  int iSDParm;         /* A parameter used by the eDest disposal method */
         2265  +  int iSdst;           /* Base register where results are written */
         2266  +  int nSdst;           /* Number of registers allocated */
         2267  +  ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
  2202   2268   };
  2203   2269   
  2204   2270   /*
  2205   2271   ** During code generation of statements that do inserts into AUTOINCREMENT 
  2206   2272   ** tables, the following information is attached to the Table.u.autoInc.p
  2207   2273   ** pointer of each autoincrement table to record some side information that
  2208   2274   ** the code generator needs.  We have to keep per-table autoincrement
................................................................................
  2297   2363     int nTab;            /* Number of previously allocated VDBE cursors */
  2298   2364     int nMem;            /* Number of memory cells used so far */
  2299   2365     int nSet;            /* Number of sets used so far */
  2300   2366     int nOnce;           /* Number of OP_Once instructions so far */
  2301   2367     int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
  2302   2368     int nLabel;          /* Number of labels used */
  2303   2369     int *aLabel;         /* Space to hold the labels */
         2370  +  int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
  2304   2371     int ckBase;          /* Base register of data during check constraints */
  2305   2372     int iPartIdxTab;     /* Table corresponding to a partial index */
  2306   2373     int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
  2307   2374     int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
  2308   2375     struct yColCache {
  2309   2376       int iTable;           /* Table cursor number */
  2310   2377       int iColumn;          /* Table column number */
................................................................................
  2367   2434     Token sLastToken;         /* The last token parsed */
  2368   2435   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2369   2436     Token sArg;               /* Complete text of a module argument */
  2370   2437     Table **apVtabLock;       /* Pointer to virtual tables needing locking */
  2371   2438   #endif
  2372   2439     Table *pZombieTab;        /* List of Table objects to delete after code gen */
  2373   2440     TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
         2441  +  With *pWith;              /* Current WITH clause, or NULL */
         2442  +  u8 bFreeWith;             /* True if pWith should be freed with parser */
  2374   2443   };
  2375   2444   
  2376   2445   /*
  2377   2446   ** Return true if currently inside an sqlite3_declare_vtab() call.
  2378   2447   */
  2379   2448   #ifdef SQLITE_OMIT_VIRTUALTABLE
  2380   2449     #define IN_DECLARE_VTAB 0
................................................................................
  2490   2559   struct TriggerStep {
  2491   2560     u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
  2492   2561     u8 orconf;           /* OE_Rollback etc. */
  2493   2562     Trigger *pTrig;      /* The trigger that this step is a part of */
  2494   2563     Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
  2495   2564     Token target;        /* Target table for DELETE, UPDATE, INSERT */
  2496   2565     Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
  2497         -  ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
         2566  +  ExprList *pExprList; /* SET clause for UPDATE. */
  2498   2567     IdList *pIdList;     /* Column names for INSERT */
  2499   2568     TriggerStep *pNext;  /* Next in the link-list */
  2500   2569     TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
  2501   2570   };
  2502   2571   
  2503   2572   /*
  2504   2573   ** The following structure contains information used by the sqliteFix...
................................................................................
  2612   2681   
  2613   2682   /*
  2614   2683   ** Context pointer passed down through the tree-walk.
  2615   2684   */
  2616   2685   struct Walker {
  2617   2686     int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
  2618   2687     int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
         2688  +  void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
  2619   2689     Parse *pParse;                            /* Parser context.  */
  2620   2690     int walkerDepth;                          /* Number of subqueries */
  2621         -  u8 bSelectDepthFirst;                     /* Do subqueries first */
  2622   2691     union {                                   /* Extra data for callback */
  2623   2692       NameContext *pNC;                          /* Naming context */
  2624   2693       int i;                                     /* Integer value */
  2625   2694       SrcList *pSrcList;                         /* FROM clause */
  2626   2695       struct SrcCount *pSrcCount;                /* Counting column references */
  2627   2696     } u;
  2628   2697   };
................................................................................
  2637   2706   /*
  2638   2707   ** Return code from the parse-tree walking primitives and their
  2639   2708   ** callbacks.
  2640   2709   */
  2641   2710   #define WRC_Continue    0   /* Continue down into children */
  2642   2711   #define WRC_Prune       1   /* Omit children but continue walking siblings */
  2643   2712   #define WRC_Abort       2   /* Abandon the tree walk */
         2713  +
         2714  +/*
         2715  +** An instance of this structure represents a set of one or more CTEs
         2716  +** (common table expressions) created by a single WITH clause.
         2717  +*/
         2718  +struct With {
         2719  +  int nCte;                       /* Number of CTEs in the WITH clause */
         2720  +  With *pOuter;                   /* Containing WITH clause, or NULL */
         2721  +  struct Cte {                    /* For each CTE in the WITH clause.... */
         2722  +    char *zName;                    /* Name of this CTE */
         2723  +    ExprList *pCols;                /* List of explicit column names, or NULL */
         2724  +    Select *pSelect;                /* The definition of this CTE */
         2725  +    const char *zErr;               /* Error message for circular references */
         2726  +  } a[1];
         2727  +};
  2644   2728   
  2645   2729   /*
  2646   2730   ** Assuming zIn points to the first byte of a UTF-8 character,
  2647   2731   ** advance zIn to point to the first byte of the next UTF-8 character.
  2648   2732   */
  2649   2733   #define SQLITE_SKIP_UTF8(zIn) {                        \
  2650   2734     if( (*(zIn++))>=0xc0 ){                              \
................................................................................
  2905   2989     void sqlite3AutoincrementBegin(Parse *pParse);
  2906   2990     void sqlite3AutoincrementEnd(Parse *pParse);
  2907   2991   #else
  2908   2992   # define sqlite3AutoincrementBegin(X)
  2909   2993   # define sqlite3AutoincrementEnd(X)
  2910   2994   #endif
  2911   2995   int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
  2912         -void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
         2996  +void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
  2913   2997   void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
  2914   2998   IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
  2915   2999   int sqlite3IdListIndex(IdList*,const char*);
  2916   3000   SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
  2917   3001   SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
  2918   3002   SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
  2919   3003                                         Token*, Select*, Expr*, IdList*);
................................................................................
  2980   3064   int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
  2981   3065   void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
  2982   3066   void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
  2983   3067   int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
  2984   3068   Vdbe *sqlite3GetVdbe(Parse*);
  2985   3069   void sqlite3PrngSaveState(void);
  2986   3070   void sqlite3PrngRestoreState(void);
  2987         -void sqlite3PrngResetState(void);
  2988   3071   void sqlite3RollbackAll(sqlite3*,int);
  2989   3072   void sqlite3CodeVerifySchema(Parse*, int);
  2990   3073   void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
  2991   3074   void sqlite3BeginTransaction(Parse*, int);
  2992   3075   void sqlite3CommitTransaction(Parse*);
  2993   3076   void sqlite3RollbackTransaction(Parse*);
  2994   3077   void sqlite3Savepoint(Parse*, int, Token*);
................................................................................
  3000   3083   int sqlite3ExprIsInteger(Expr*, int*);
  3001   3084   int sqlite3ExprCanBeNull(const Expr*);
  3002   3085   void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
  3003   3086   int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  3004   3087   int sqlite3IsRowid(const char*);
  3005   3088   void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
  3006   3089   void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
  3007         -int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
         3090  +int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
  3008   3091   void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
  3009   3092                                        u8,u8,int,int*);
  3010   3093   void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
  3011   3094   int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
  3012   3095   void sqlite3BeginWriteOperation(Parse*, int, int);
  3013   3096   void sqlite3MultiWrite(Parse*);
  3014   3097   void sqlite3MayAbort(Parse*);
................................................................................
  3044   3127     void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
  3045   3128                               int, int, int);
  3046   3129     void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
  3047   3130     void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
  3048   3131     void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
  3049   3132     TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
  3050   3133     TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
  3051         -                                        ExprList*,Select*,u8);
         3134  +                                        Select*,u8);
  3052   3135     TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
  3053   3136     TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
  3054   3137     void sqlite3DeleteTrigger(sqlite3*, Trigger*);
  3055   3138     void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
  3056   3139     u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
  3057   3140   # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
  3058   3141   #else
................................................................................
  3337   3420   CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
  3338   3421   int sqlite3TempInMemory(const sqlite3*);
  3339   3422   const char *sqlite3JournalModename(int);
  3340   3423   #ifndef SQLITE_OMIT_WAL
  3341   3424     int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
  3342   3425     int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
  3343   3426   #endif
         3427  +#ifndef SQLITE_OMIT_CTE
         3428  +  With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
         3429  +  void sqlite3WithDelete(sqlite3*,With*);
         3430  +  void sqlite3WithPush(Parse*, With*, u8);
         3431  +#else
         3432  +#define sqlite3WithPush(x,y,z)
         3433  +#define sqlite3WithDelete(x,y)
         3434  +#endif
  3344   3435   
  3345   3436   /* Declarations for functions in fkey.c. All of these are replaced by
  3346   3437   ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
  3347   3438   ** key functionality is available. If OMIT_TRIGGER is defined but
  3348   3439   ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
  3349   3440   ** this case foreign keys are parsed, but no other functionality is 
  3350   3441   ** provided (enforcement of FK constraints requires the triggers sub-system).

Changes to src/tclsqlite.c.

   954    954       case SQLITE_ALTER_TABLE       : zCode="SQLITE_ALTER_TABLE"; break;
   955    955       case SQLITE_REINDEX           : zCode="SQLITE_REINDEX"; break;
   956    956       case SQLITE_ANALYZE           : zCode="SQLITE_ANALYZE"; break;
   957    957       case SQLITE_CREATE_VTABLE     : zCode="SQLITE_CREATE_VTABLE"; break;
   958    958       case SQLITE_DROP_VTABLE       : zCode="SQLITE_DROP_VTABLE"; break;
   959    959       case SQLITE_FUNCTION          : zCode="SQLITE_FUNCTION"; break;
   960    960       case SQLITE_SAVEPOINT         : zCode="SQLITE_SAVEPOINT"; break;
          961  +    case SQLITE_RECURSIVE         : zCode="SQLITE_RECURSIVE"; break;
   961    962       default                       : zCode="????"; break;
   962    963     }
   963    964     Tcl_DStringInit(&str);
   964    965     Tcl_DStringAppend(&str, pDb->zAuth, -1);
   965    966     Tcl_DStringAppendElement(&str, zCode);
   966    967     Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
   967    968     Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");

Changes to src/test1.c.

   238    238       }
   239    239       sqlite3IoTrace = io_trace_callback;
   240    240     }
   241    241   #endif
   242    242     return TCL_OK;
   243    243   }
   244    244   
   245         -
          245  +/*
          246  +** Usage:  clang_sanitize_address 
          247  +**
          248  +** Returns true if the program was compiled using clang with the 
          249  +** -fsanitize=address switch on the command line. False otherwise.
          250  +*/
          251  +static int clang_sanitize_address(
          252  +  void *NotUsed,
          253  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
          254  +  int argc,              /* Number of arguments */
          255  +  char **argv            /* Text of each argument */
          256  +){
          257  +  int res = 0;
          258  +#if defined(__has_feature)
          259  +# if __has_feature(address_sanitizer)
          260  +  res = 1;
          261  +# endif
          262  +#endif
          263  +  Tcl_SetObjResult(interp, Tcl_NewIntObj(res));
          264  +  return TCL_OK;
          265  +}
          266  +  
   246    267   /*
   247    268   ** Usage:  sqlite3_exec_printf  DB  FORMAT  STRING
   248    269   **
   249    270   ** Invoke the sqlite3_exec_printf() interface using the open database
   250    271   ** DB.  The SQL is the string FORMAT.  The format string should contain
   251    272   ** one %s or %q.  STRING is the value inserted into %s or %q.
   252    273   */
................................................................................
  6156   6177     } aOpt[] = {
  6157   6178       { "all",                 SQLITE_AllOpts        },
  6158   6179       { "none",                0                     },
  6159   6180       { "query-flattener",     SQLITE_QueryFlattener },
  6160   6181       { "column-cache",        SQLITE_ColumnCache    },
  6161   6182       { "groupby-order",       SQLITE_GroupByOrder   },
  6162   6183       { "factor-constants",    SQLITE_FactorOutConst },
  6163         -    { "real-as-int",         SQLITE_IdxRealAsInt   },
  6164   6184       { "distinct-opt",        SQLITE_DistinctOpt    },
  6165   6185       { "cover-idx-scan",      SQLITE_CoverIdxScan   },
  6166   6186       { "order-by-idx-join",   SQLITE_OrderByIdxJoin },
  6167   6187       { "transitive",          SQLITE_Transitive     },
  6168   6188       { "subquery-coroutine",  SQLITE_SubqCoroutine  },
  6169   6189       { "omit-noop-join",      SQLITE_OmitNoopJoin   },
  6170   6190       { "stat3",               SQLITE_Stat3          },
................................................................................
  6320   6340        { "sqlite_delete_function",        (Tcl_CmdProc*)delete_function       },
  6321   6341        { "sqlite_delete_collation",       (Tcl_CmdProc*)delete_collation      },
  6322   6342        { "sqlite3_get_autocommit",        (Tcl_CmdProc*)get_autocommit        },
  6323   6343        { "sqlite3_stack_used",            (Tcl_CmdProc*)test_stack_used       },
  6324   6344        { "sqlite3_busy_timeout",          (Tcl_CmdProc*)test_busy_timeout     },
  6325   6345        { "printf",                        (Tcl_CmdProc*)test_printf           },
  6326   6346        { "sqlite3IoTrace",              (Tcl_CmdProc*)test_io_trace         },
         6347  +     { "clang_sanitize_address",        (Tcl_CmdProc*)clang_sanitize_address },
  6327   6348     };
  6328   6349     static struct {
  6329   6350        char *zName;
  6330   6351        Tcl_ObjCmdProc *xProc;
  6331   6352        void *clientData;
  6332   6353     } aObjCmd[] = {
  6333   6354        { "sqlite3_connection_pointer",    get_sqlite_pointer, 0 },

Changes to src/test_config.c.

   226    226   #endif
   227    227   
   228    228   #ifdef SQLITE_OMIT_CHECK
   229    229     Tcl_SetVar2(interp, "sqlite_options", "check", "0", TCL_GLOBAL_ONLY);
   230    230   #else
   231    231     Tcl_SetVar2(interp, "sqlite_options", "check", "1", TCL_GLOBAL_ONLY);
   232    232   #endif
          233  +
          234  +#ifdef SQLITE_OMIT_CTE
          235  +  Tcl_SetVar2(interp, "sqlite_options", "cte", "0", TCL_GLOBAL_ONLY);
          236  +#else
          237  +  Tcl_SetVar2(interp, "sqlite_options", "cte", "1", TCL_GLOBAL_ONLY);
          238  +#endif
   233    239   
   234    240   #ifdef SQLITE_ENABLE_COLUMN_METADATA
   235    241     Tcl_SetVar2(interp, "sqlite_options", "columnmetadata", "1", TCL_GLOBAL_ONLY);
   236    242   #else
   237    243     Tcl_SetVar2(interp, "sqlite_options", "columnmetadata", "0", TCL_GLOBAL_ONLY);
   238    244   #endif
   239    245   

Changes to src/tokenize.c.

   299    299         return i;
   300    300       }
   301    301       case '?': {
   302    302         *tokenType = TK_VARIABLE;
   303    303         for(i=1; sqlite3Isdigit(z[i]); i++){}
   304    304         return i;
   305    305       }
   306         -    case '#': {
   307         -      for(i=1; sqlite3Isdigit(z[i]); i++){}
   308         -      if( i>1 ){
   309         -        /* Parameters of the form #NNN (where NNN is a number) are used
   310         -        ** internally by sqlite3NestedParse.  */
   311         -        *tokenType = TK_REGISTER;
   312         -        return i;
   313         -      }
   314         -      /* Fall through into the next case if the '#' is not followed by
   315         -      ** a digit. Try to match #AAAA where AAAA is a parameter name. */
   316         -    }
   317    306   #ifndef SQLITE_OMIT_TCL_VARIABLE
   318    307       case '$':
   319    308   #endif
   320    309       case '@':  /* For compatibility with MS SQL Server */
          310  +    case '#':
   321    311       case ':': {
   322    312         int n = 0;
   323         -      testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
          313  +      testcase( z[0]=='$' );  testcase( z[0]=='@' );
          314  +      testcase( z[0]==':' );  testcase( z[0]=='#' );
   324    315         *tokenType = TK_VARIABLE;
   325    316         for(i=1; (c=z[i])!=0; i++){
   326    317           if( IdChar(c) ){
   327    318             n++;
   328    319   #ifndef SQLITE_OMIT_TCL_VARIABLE
   329    320           }else if( c=='(' && n>0 ){
   330    321             do{
................................................................................
   499    490       /* If the pParse->declareVtab flag is set, do not delete any table 
   500    491       ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
   501    492       ** will take responsibility for freeing the Table structure.
   502    493       */
   503    494       sqlite3DeleteTable(db, pParse->pNewTable);
   504    495     }
   505    496   
          497  +  if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
   506    498     sqlite3DeleteTrigger(db, pParse->pNewTrigger);
   507    499     for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
   508    500     sqlite3DbFree(db, pParse->azVar);
   509    501     while( pParse->pAinc ){
   510    502       AutoincInfo *p = pParse->pAinc;
   511    503       pParse->pAinc = p->pNext;
   512    504       sqlite3DbFree(db, p);

Changes to src/trigger.c.

   393    393   ** The parser calls this routine when it sees an INSERT inside the
   394    394   ** body of a trigger.
   395    395   */
   396    396   TriggerStep *sqlite3TriggerInsertStep(
   397    397     sqlite3 *db,        /* The database connection */
   398    398     Token *pTableName,  /* Name of the table into which we insert */
   399    399     IdList *pColumn,    /* List of columns in pTableName to insert into */
   400         -  ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
   401    400     Select *pSelect,    /* A SELECT statement that supplies values */
   402    401     u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
   403    402   ){
   404    403     TriggerStep *pTriggerStep;
   405    404   
   406         -  assert(pEList == 0 || pSelect == 0);
   407         -  assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
          405  +  assert(pSelect != 0 || db->mallocFailed);
   408    406   
   409    407     pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
   410    408     if( pTriggerStep ){
   411    409       pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
   412    410       pTriggerStep->pIdList = pColumn;
   413         -    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
   414    411       pTriggerStep->orconf = orconf;
   415    412     }else{
   416    413       sqlite3IdListDelete(db, pColumn);
   417    414     }
   418         -  sqlite3ExprListDelete(db, pEList);
   419    415     sqlite3SelectDelete(db, pSelect);
   420    416   
   421    417     return pTriggerStep;
   422    418   }
   423    419   
   424    420   /*
   425    421   ** Construct a trigger step that implements an UPDATE statement and return
................................................................................
   749    745             pParse->eOrconf
   750    746           );
   751    747           break;
   752    748         }
   753    749         case TK_INSERT: {
   754    750           sqlite3Insert(pParse, 
   755    751             targetSrcList(pParse, pStep),
   756         -          sqlite3ExprListDup(db, pStep->pExprList, 0), 
   757    752             sqlite3SelectDup(db, pStep->pSelect, 0), 
   758    753             sqlite3IdListDup(db, pStep->pIdList), 
   759    754             pParse->eOrconf
   760    755           );
   761    756           break;
   762    757         }
   763    758         case TK_DELETE: {

Changes to src/vdbe.c.

  4423   4423   
  4424   4424     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4425   4425     pC = p->apCsr[pOp->p1];
  4426   4426     assert( pC!=0 );
  4427   4427     pC->nullRow = 1;
  4428   4428     pC->rowidIsValid = 0;
  4429   4429     pC->cacheStatus = CACHE_STALE;
  4430         -  assert( pC->pCursor || pC->pVtabCursor );
  4431   4430     if( pC->pCursor ){
  4432   4431       sqlite3BtreeClearCursor(pC->pCursor);
  4433   4432     }
  4434   4433     break;
  4435   4434   }
  4436   4435   
  4437   4436   /* Opcode: Last P1 P2 * * *

Changes to src/vdbe.h.

   173    173   void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
   174    174   void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
   175    175   void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
   176    176   void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
   177    177   void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
   178    178   void sqlite3VdbeJumpHere(Vdbe*, int addr);
   179    179   void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
          180  +int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
   180    181   void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
   181    182   void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
   182    183   void sqlite3VdbeUsesBtree(Vdbe*, int);
   183    184   VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
   184    185   int sqlite3VdbeMakeLabel(Vdbe*);
   185    186   void sqlite3VdbeRunOnlyOnce(Vdbe*);
   186    187   void sqlite3VdbeDelete(Vdbe*);

Changes to src/vdbeaux.c.

   273    273     Parse *p = v->pParse;
   274    274     int j = -1-x;
   275    275     assert( v->magic==VDBE_MAGIC_INIT );
   276    276     assert( j<p->nLabel );
   277    277     if( j>=0 && p->aLabel ){
   278    278       p->aLabel[j] = v->nOp;
   279    279     }
          280  +  p->iFixedOp = v->nOp - 1;
   280    281   }
   281    282   
   282    283   /*
   283    284   ** Mark the VDBE as one that can only be run one time.
   284    285   */
   285    286   void sqlite3VdbeRunOnlyOnce(Vdbe *p){
   286    287     p->runOnlyOnce = 1;
................................................................................
   621    622   }
   622    623   
   623    624   /*
   624    625   ** Change the P2 operand of instruction addr so that it points to
   625    626   ** the address of the next instruction to be coded.
   626    627   */
   627    628   void sqlite3VdbeJumpHere(Vdbe *p, int addr){
   628         -  if( ALWAYS(addr>=0) ) sqlite3VdbeChangeP2(p, addr, p->nOp);
          629  +  sqlite3VdbeChangeP2(p, addr, p->nOp);
          630  +  p->pParse->iFixedOp = p->nOp - 1;
   629    631   }
   630    632   
   631    633   
   632    634   /*
   633    635   ** If the input FuncDef structure is ephemeral, then free it.  If
   634    636   ** the FuncDef is not ephermal, then do nothing.
   635    637   */
................................................................................
   722    724       sqlite3 *db = p->db;
   723    725       freeP4(db, pOp->p4type, pOp->p4.p);
   724    726       memset(pOp, 0, sizeof(pOp[0]));
   725    727       pOp->opcode = OP_Noop;
   726    728       if( addr==p->nOp-1 ) p->nOp--;
   727    729     }
   728    730   }
          731  +
          732  +/*
          733  +** Remove the last opcode inserted
          734  +*/
          735  +int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
          736  +  if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
          737  +    sqlite3VdbeChangeToNoop(p, p->nOp-1);
          738  +    return 1;
          739  +  }else{
          740  +    return 0;
          741  +  }
          742  +}
   729    743   
   730    744   /*
   731    745   ** Change the value of the P4 operand for a specific instruction.
   732    746   ** This routine is useful when a large program is loaded from a
   733    747   ** static array using sqlite3VdbeAddOpList but we want to make a
   734    748   ** few minor changes to the program.
   735    749   **

Changes to src/walker.c.

   109    109     }
   110    110     return WRC_Continue;
   111    111   } 
   112    112   
   113    113   /*
   114    114   ** Call sqlite3WalkExpr() for every expression in Select statement p.
   115    115   ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
   116         -** on the compound select chain, p->pPrior.  Invoke the xSelectCallback()
   117         -** either before or after the walk of expressions and FROM clause, depending
   118         -** on whether pWalker->bSelectDepthFirst is false or true, respectively.
          116  +** on the compound select chain, p->pPrior. 
          117  +**
          118  +** If it is not NULL, the xSelectCallback() callback is invoked before
          119  +** the walk of the expressions and FROM clause. The xSelectCallback2()
          120  +** method, if it is not NULL, is invoked following the walk of the 
          121  +** expressions and FROM clause.
   119    122   **
   120    123   ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
   121    124   ** there is an abort request.
   122    125   **
   123    126   ** If the Walker does not have an xSelectCallback() then this routine
   124    127   ** is a no-op returning WRC_Continue.
   125    128   */
   126    129   int sqlite3WalkSelect(Walker *pWalker, Select *p){
   127    130     int rc;
   128         -  if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
          131  +  if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
          132  +    return WRC_Continue;
          133  +  }
   129    134     rc = WRC_Continue;
   130    135     pWalker->walkerDepth++;
   131    136     while( p ){
   132         -    if( !pWalker->bSelectDepthFirst ){
          137  +    if( pWalker->xSelectCallback ){
   133    138          rc = pWalker->xSelectCallback(pWalker, p);
   134    139          if( rc ) break;
   135    140       }
   136    141       if( sqlite3WalkSelectExpr(pWalker, p)
   137    142        || sqlite3WalkSelectFrom(pWalker, p)
   138    143       ){
   139    144         pWalker->walkerDepth--;
   140    145         return WRC_Abort;
   141    146       }
   142         -    if( pWalker->bSelectDepthFirst ){
   143         -      rc = pWalker->xSelectCallback(pWalker, p);
   144         -      /* Depth-first search is currently only used for
   145         -      ** selectAddSubqueryTypeInfo() and that routine always returns
   146         -      ** WRC_Continue (0).  So the following branch is never taken. */
   147         -      if( NEVER(rc) ) break;
          147  +    if( pWalker->xSelectCallback2 ){
          148  +      pWalker->xSelectCallback2(pWalker, p);
   148    149       }
   149    150       p = p->pPrior;
   150    151     }
   151    152     pWalker->walkerDepth--;
   152    153     return rc & WRC_Abort;
   153    154   }

Changes to src/where.c.

   663    663     ){
   664    664       /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
   665    665       ** be the name of an indexed column with TEXT affinity. */
   666    666       return 0;
   667    667     }
   668    668     assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
   669    669   
   670         -  pRight = pList->a[0].pExpr;
          670  +  pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
   671    671     op = pRight->op;
   672    672     if( op==TK_VARIABLE ){
   673    673       Vdbe *pReprepare = pParse->pReprepare;
   674    674       int iCol = pRight->iColumn;
   675    675       pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
   676    676       if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
   677    677         z = (char *)sqlite3_value_text(pVal);
................................................................................
  1706   1706     sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
  1707   1707     sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1708   1708     VdbeComment((v, "for %s", pTable->zName));
  1709   1709   
  1710   1710     /* Fill the automatic index with content */
  1711   1711     addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
  1712   1712     regRecord = sqlite3GetTempReg(pParse);
  1713         -  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0);
         1713  +  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
  1714   1714     sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
  1715   1715     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  1716   1716     sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
  1717   1717     sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
  1718   1718     sqlite3VdbeJumpHere(v, addrTop);
  1719   1719     sqlite3ReleaseTempReg(pParse, regRecord);
  1720   1720     
................................................................................
  1747   1747     /* Count the number of possible WHERE clause constraints referring
  1748   1748     ** to this virtual table */
  1749   1749     for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  1750   1750       if( pTerm->leftCursor != pSrc->iCursor ) continue;
  1751   1751       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
  1752   1752       testcase( pTerm->eOperator & WO_IN );
  1753   1753       testcase( pTerm->eOperator & WO_ISNULL );
  1754         -    if( pTerm->eOperator & (WO_ISNULL) ) continue;
         1754  +    testcase( pTerm->eOperator & WO_ALL );
         1755  +    if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
  1755   1756       if( pTerm->wtFlags & TERM_VNULL ) continue;
  1756   1757       nTerm++;
  1757   1758     }
  1758   1759   
  1759   1760     /* If the ORDER BY clause contains only columns in the current 
  1760   1761     ** virtual table then allocate space for the aOrderBy part of
  1761   1762     ** the sqlite3_index_info structure.
................................................................................
  1799   1800   
  1800   1801     for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  1801   1802       u8 op;
  1802   1803       if( pTerm->leftCursor != pSrc->iCursor ) continue;
  1803   1804       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
  1804   1805       testcase( pTerm->eOperator & WO_IN );
  1805   1806       testcase( pTerm->eOperator & WO_ISNULL );
  1806         -    if( pTerm->eOperator & (WO_ISNULL) ) continue;
         1807  +    testcase( pTerm->eOperator & WO_ALL );
         1808  +    if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
  1807   1809       if( pTerm->wtFlags & TERM_VNULL ) continue;
  1808   1810       pIdxCons[j].iColumn = pTerm->u.leftColumn;
  1809   1811       pIdxCons[j].iTermOffset = i;
  1810   1812       op = (u8)pTerm->eOperator & WO_ALL;
  1811   1813       if( op==WO_IN ) op = WO_EQ;
  1812   1814       pIdxCons[j].op = op;
  1813   1815       /* The direct assignment in the previous line is possible only because
................................................................................
  3404   3406     {
  3405   3407       /* Case 6:  There is no usable index.  We must do a complete
  3406   3408       **          scan of the entire table.
  3407   3409       */
  3408   3410       static const u8 aStep[] = { OP_Next, OP_Prev };
  3409   3411       static const u8 aStart[] = { OP_Rewind, OP_Last };
  3410   3412       assert( bRev==0 || bRev==1 );
  3411         -    pLevel->op = aStep[bRev];
  3412         -    pLevel->p1 = iCur;
  3413         -    pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
  3414         -    pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
         3413  +    if( pTabItem->isRecursive ){
         3414  +      /* Tables marked isRecursive have only a single row that is stored in
         3415  +      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
         3416  +      pLevel->op = OP_Noop;
         3417  +    }else{
         3418  +      pLevel->op = aStep[bRev];
         3419  +      pLevel->p1 = iCur;
         3420  +      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
         3421  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
         3422  +    }
  3415   3423     }
  3416   3424   
  3417   3425     /* Insert code to test every subexpression that can be completely
  3418   3426     ** computed using the current set of tables.
  3419   3427     */
  3420   3428     for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
  3421   3429       Expr *pE;
................................................................................
  4192   4200     if( !pBuilder->pOrSet
  4193   4201      && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
  4194   4202      && pSrc->pIndex==0
  4195   4203      && !pSrc->viaCoroutine
  4196   4204      && !pSrc->notIndexed
  4197   4205      && HasRowid(pTab)
  4198   4206      && !pSrc->isCorrelated
         4207  +   && !pSrc->isRecursive
  4199   4208     ){
  4200   4209       /* Generate auto-index WhereLoops */
  4201   4210       WhereTerm *pTerm;
  4202   4211       WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
  4203   4212       for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
  4204   4213         if( pTerm->prereqRight & pNew->maskSelf ) continue;
  4205   4214         if( termCanDriveIndex(pTerm, pSrc, 0) ){
................................................................................
  5426   5435     whereClauseInit(&pWInfo->sWC, pWInfo);
  5427   5436     whereSplit(&pWInfo->sWC, pWhere, TK_AND);
  5428   5437     sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
  5429   5438       
  5430   5439     /* Special case: a WHERE clause that is constant.  Evaluate the
  5431   5440     ** expression and either jump over all of the code or fall thru.
  5432   5441     */
  5433         -  if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
  5434         -    sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
  5435         -    pWhere = 0;
         5442  +  for(ii=0; ii<sWLB.pWC->nTerm; ii++){
         5443  +    if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
         5444  +      sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
         5445  +                         SQLITE_JUMPIFNULL);
         5446  +      sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
         5447  +    }
  5436   5448     }
  5437   5449   
  5438   5450     /* Special case: No FROM clause
  5439   5451     */
  5440   5452     if( nTabList==0 ){
  5441   5453       if( pOrderBy ) pWInfo->bOBSat = 1;
  5442   5454       if( wctrlFlags & WHERE_WANT_DISTINCT ){

Changes to test/auth.test.

  2076   2076   
  2077   2077     do_test auth-1.308 {
  2078   2078       set authargs
  2079   2079     } {main t5 {} {}}
  2080   2080     execsql {DROP TABLE t5}
  2081   2081   } ;# ifcapable altertable
  2082   2082   
         2083  +ifcapable {cte} {
         2084  +  do_test auth-1.310 {
         2085  +    proc auth {code arg1 arg2 arg3 arg4} {
         2086  +      if {$code=="SQLITE_RECURSIVE"} {
         2087  +        return SQLITE_DENY
         2088  +      }
         2089  +      return SQLITE_OK
         2090  +    }
         2091  +    db eval {
         2092  +       DROP TABLE IF EXISTS t1;
         2093  +       CREATE TABLE t1(a,b);
         2094  +       INSERT INTO t1 VALUES(1,2),(3,4),(5,6);
         2095  +    }
         2096  +  } {}
         2097  +  do_catchsql_test auth-1.311 {
         2098  +    WITH
         2099  +       auth1311(x,y) AS (SELECT a+b, b-a FROM t1)
         2100  +    SELECT * FROM auth1311 ORDER BY x;
         2101  +  } {0 {3 1 7 1 11 1}}
         2102  +  do_catchsql_test auth-1.312 {
         2103  +    WITH RECURSIVE
         2104  +       auth1312(x,y) AS (SELECT a+b, b-a FROM t1)
         2105  +    SELECT x, y FROM auth1312 ORDER BY x;
         2106  +  } {0 {3 1 7 1 11 1}}
         2107  +  do_catchsql_test auth-1.313 {
         2108  +    WITH RECURSIVE
         2109  +       auth1313(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM auth1313 WHERE x<5)
         2110  +    SELECT * FROM t1;
         2111  +  } {0 {1 2 3 4 5 6}}
         2112  +  do_catchsql_test auth-1.314 {
         2113  +    WITH RECURSIVE
         2114  +       auth1314(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM auth1314 WHERE x<5)
         2115  +    SELECT * FROM t1 LEFT JOIN auth1314;
         2116  +  } {1 {not authorized}}
         2117  +} ;# ifcapable cte
         2118  +
  2083   2119   do_test auth-2.1 {
  2084   2120     proc auth {code arg1 arg2 arg3 arg4} {
  2085   2121       if {$code=="SQLITE_READ" && $arg1=="t3" && $arg2=="x"} {
  2086   2122         return SQLITE_DENY
  2087   2123       }
  2088   2124       return SQLITE_OK
  2089   2125     }

Changes to test/capi3.test.

   175    175   } {SQLITE_CANTOPEN}
   176    176   do_test capi3-3.4 {
   177    177     sqlite3_errmsg $db2
   178    178   } {unable to open database file}
   179    179   do_test capi3-3.5 {
   180    180     sqlite3_close $db2
   181    181   } {SQLITE_OK}
   182         -do_test capi3-3.6.1-misuse {
   183         -  sqlite3_close $db2
   184         -} {SQLITE_MISUSE}
   185         -do_test capi3-3.6.2-misuse {
   186         -  sqlite3_errmsg $db2
   187         -} {library routine called out of sequence}
   188         -ifcapable {utf16} {
   189         -  do_test capi3-3.6.3-misuse {
   190         -    utf8 [sqlite3_errmsg16 $db2]
          182  +if {[clang_sanitize_address]==0} {
          183  +  do_test capi3-3.6.1-misuse {
          184  +    sqlite3_close $db2
          185  +  } {SQLITE_MISUSE}
          186  +  do_test capi3-3.6.2-misuse {
          187  +    sqlite3_errmsg $db2
   191    188     } {library routine called out of sequence}
          189  +  ifcapable {utf16} {
          190  +    do_test capi3-3.6.3-misuse {
          191  +      utf8 [sqlite3_errmsg16 $db2]
          192  +    } {library routine called out of sequence}
          193  +  }
   192    194   }
   193    195   
   194    196   do_test capi3-3.7 {
   195    197     set db2 [sqlite3_open]
   196    198     sqlite3_errcode $db2
   197    199   } {SQLITE_OK}
   198    200   do_test capi3-3.8 {
................................................................................
   657    659     sqlite3_step $STMT
   658    660   } {SQLITE_ROW}
   659    661   #check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1}
   660    662   do_test capi3-6.3 {
   661    663     sqlite3_finalize $STMT
   662    664   } {SQLITE_OK}
   663    665   
   664         -do_test capi3-6.4-misuse {
   665         -  db cache flush
   666         -  sqlite3_close $DB
   667         -} {SQLITE_OK}
          666  +if {[clang_sanitize_address]==0} {
          667  +  do_test capi3-6.4-misuse {
          668  +    db cache flush
          669  +    sqlite3_close $DB
          670  +  } {SQLITE_OK}
          671  +}
   668    672   db close
   669    673   
   670    674   # This procedure sets the value of the file-format in file 'test.db'
   671    675   # to $newval. Also, the schema cookie is incremented.
   672    676   # 
   673    677   proc set_file_format {newval} {
   674    678     hexio_write test.db 44 [hexio_render_int32 $newval]
................................................................................
  1056   1060     do_test capi3-13-5 {
  1057   1061       set ms [sqlite3_sleep 80]
  1058   1062       expr {$ms==80 || $ms==1000}
  1059   1063     } {1}
  1060   1064   }
  1061   1065   
  1062   1066   # Ticket #1219:  Make sure binding APIs can handle a NULL pointer.
  1063         -#
  1064         -do_test capi3-14.1-misuse {
  1065         -  set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
  1066         -  lappend rc $msg
  1067         -} {1 SQLITE_MISUSE}
         1067  +# 
         1068  +if {[clang_sanitize_address]==0} {
         1069  +  do_test capi3-14.1-misuse {
         1070  +    set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
         1071  +      lappend rc $msg
         1072  +  } {1 SQLITE_MISUSE}
         1073  +}
  1068   1074   
  1069   1075   # Ticket #1650:  Honor the nBytes parameter to sqlite3_prepare.
  1070   1076   #
  1071   1077   do_test capi3-15.1 {
  1072   1078     set sql {SELECT * FROM t2}
  1073   1079     set nbytes [string length $sql]
  1074   1080     append sql { WHERE a==1}

Changes to test/capi3c.test.

   165    165   } {SQLITE_CANTOPEN}
   166    166   do_test capi3c-3.4 {
   167    167     sqlite3_errmsg $db2
   168    168   } {unable to open database file}
   169    169   do_test capi3c-3.5 {
   170    170     sqlite3_close $db2
   171    171   } {SQLITE_OK}
   172         -do_test capi3c-3.6.1-misuse {
   173         -  sqlite3_close $db2
   174         -} {SQLITE_MISUSE}
   175         -do_test capi3c-3.6.2-misuse {
   176         -  sqlite3_errmsg $db2
   177         -} {library routine called out of sequence}
   178         -ifcapable {utf16} {
   179         -  do_test capi3c-3.6.3-misuse {
   180         -    utf8 [sqlite3_errmsg16 $db2]
          172  +if {[clang_sanitize_address]==0} {
          173  +  do_test capi3c-3.6.1-misuse {
          174  +    sqlite3_close $db2
          175  +  } {SQLITE_MISUSE}
          176  +  do_test capi3c-3.6.2-misuse {
          177  +    sqlite3_errmsg $db2
   181    178     } {library routine called out of sequence}
          179  +  ifcapable {utf16} {
          180  +    do_test capi3c-3.6.3-misuse {
          181  +      utf8 [sqlite3_errmsg16 $db2]
          182  +    } {library routine called out of sequence}
          183  +  }
   182    184   }
   183    185   
   184    186   # rename sqlite3_open ""
   185    187   # rename sqlite3_open_old sqlite3_open
   186    188   
   187    189   ifcapable {utf16} {
   188    190   do_test capi3c-4.1 {
................................................................................
   623    625   do_test capi3c-6.2 {
   624    626     sqlite3_step $STMT
   625    627   } {SQLITE_ROW}
   626    628   check_data $STMT capi3c-6.3 {INTEGER} {1} {1.0} {1}
   627    629   do_test capi3c-6.3 {
   628    630     sqlite3_finalize $STMT
   629    631   } {SQLITE_OK}
   630         -do_test capi3c-6.4 {
   631         -  db cache flush
   632         -  sqlite3_close $DB
   633         -} {SQLITE_OK}
   634         -do_test capi3c-6.99-misuse {
          632  +if {[clang_sanitize_address]==0} {
          633  +  do_test capi3c-6.4 {
          634  +    db cache flush
          635  +      sqlite3_close $DB
          636  +  } {SQLITE_OK}
          637  +  do_test capi3c-6.99-misuse {
          638  +    db close
          639  +  } {}
          640  +} else {
   635    641     db close
   636         -} {}
          642  +}
   637    643   
   638    644   # This procedure sets the value of the file-format in file 'test.db'
   639    645   # to $newval. Also, the schema cookie is incremented.
   640    646   # 
   641    647   proc set_file_format {newval} {
   642    648     hexio_write test.db 44 [hexio_render_int32 $newval]
   643    649     set schemacookie [hexio_get_int [hexio_read test.db 40 4]]

Added test/corruptH.test.

            1  +# 2014-01-20
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +set testprefix corruptH
           16  +
           17  +# Do not use a codec for tests in this file, as the database file is
           18  +# manipulated directly using tcl scripts (using the [hexio_write] command).
           19  +#
           20  +do_not_use_codec
           21  +database_may_be_corrupt
           22  +
           23  +# Initialize the database.
           24  +#
           25  +do_execsql_test 1.1 {
           26  +  PRAGMA page_size=1024;
           27  +
           28  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
           29  +  INSERT INTO t1 VALUES(1, 'one');
           30  +  INSERT INTO t1 VALUES(2, 'two');
           31  +
           32  +  CREATE TABLE t2(x);
           33  +  INSERT INTO t2 VALUES(randomblob(200));
           34  +  INSERT INTO t2 SELECT randomblob(200) FROM t2;
           35  +  INSERT INTO t2 SELECT randomblob(200) FROM t2;
           36  +  INSERT INTO t2 SELECT randomblob(200) FROM t2;
           37  +  INSERT INTO t2 SELECT randomblob(200) FROM t2;
           38  +  INSERT INTO t2 SELECT randomblob(200) FROM t2;
           39  +  INSERT INTO t2 SELECT randomblob(200) FROM t2;
           40  +} {}
           41  +
           42  +# Corrupt the file so that the root page of t1 is also linked into t2 as
           43  +# a leaf page.
           44  +#
           45  +do_test 1.2 {
           46  +  db eval { SELECT name, rootpage FROM sqlite_master } { 
           47  +    set r($name) $rootpage 
           48  +  }
           49  +  db close
           50  +  hexio_write test.db [expr {($r(t2)-1)*1024 + 11}] [format %.2X $r(t1)]
           51  +  sqlite3 db test.db
           52  +} {}
           53  +
           54  +do_test 1.3 {
           55  +  db eval { PRAGMA secure_delete=1 }
           56  +  list [catch {
           57  +    db eval { SELECT * FROM t1 WHERE a IN (1, 2) } {
           58  +      db eval { DELETE FROM t2 }
           59  +    }
           60  +  } msg] $msg
           61  +} {1 {database disk image is malformed}}
           62  +
           63  +#-------------------------------------------------------------------------
           64  +reset_db
           65  +
           66  +# Initialize the database.
           67  +#
           68  +do_execsql_test 2.1 {
           69  +  PRAGMA page_size=1024;
           70  +
           71  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
           72  +  INSERT INTO t1 VALUES(1, 'one');
           73  +  INSERT INTO t1 VALUES(2, 'two');
           74  +
           75  +  CREATE TABLE t3(x);
           76  +
           77  +  CREATE TABLE t2(x PRIMARY KEY) WITHOUT ROWID;
           78  +  INSERT INTO t2 VALUES(randomblob(100));
           79  +
           80  +  DROP TABLE t3;
           81  +} {}
           82  +
           83  +do_test 2.2 {
           84  +  db eval { SELECT name, rootpage FROM sqlite_master } { 
           85  +    set r($name) $rootpage 
           86  +  }
           87  +  db close
           88  +  set fl [hexio_get_int [hexio_read test.db 32 4]]
           89  +
           90  +  hexio_write test.db [expr {($fl-1) * 1024 + 0}] 00000000 
           91  +  hexio_write test.db [expr {($fl-1) * 1024 + 4}] 00000001 
           92  +  hexio_write test.db [expr {($fl-1) * 1024 + 8}] [format %.8X $r(t1)]
           93  +  hexio_write test.db 36 00000002
           94  +
           95  +  sqlite3 db test.db
           96  +} {}
           97  +
           98  +do_test 2.3 {
           99  +  list [catch {
          100  +  db eval { SELECT * FROM t1 WHERE a IN (1, 2) } {
          101  +    db eval { 
          102  +      INSERT INTO t2 SELECT randomblob(100) FROM t2;
          103  +      INSERT INTO t2 SELECT randomblob(100) FROM t2;
          104  +      INSERT INTO t2 SELECT randomblob(100) FROM t2;
          105  +      INSERT INTO t2 SELECT randomblob(100) FROM t2;
          106  +      INSERT INTO t2 SELECT randomblob(100) FROM t2;
          107  +    }
          108  +  }
          109  +  } msg] $msg
          110  +} {1 {database disk image is malformed}}
          111  +
          112  +#-------------------------------------------------------------------------
          113  +reset_db
          114  +
          115  +# Initialize the database.
          116  +#
          117  +do_execsql_test 3.1 {
          118  +  PRAGMA page_size=1024;
          119  +
          120  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
          121  +  INSERT INTO t1 VALUES(1, 'one');
          122  +  INSERT INTO t1 VALUES(2, 'two');
          123  +
          124  +  CREATE TABLE t2(c INTEGER PRAGMA KEY, d);
          125  +  INSERT INTO t2 VALUES(1, randomblob(1100));
          126  +} {}
          127  +
          128  +do_test 3.2 {
          129  +  db eval { SELECT name, rootpage FROM sqlite_master } { 
          130  +    set r($name) $rootpage 
          131  +  }
          132  +  db close
          133  +
          134  +  hexio_write test.db [expr {($r(t2)-1) * 1024 + 1020}] 00000002
          135  +
          136  +  sqlite3 db test.db
          137  +} {}
          138  +
          139  +do_test 3.3 {
          140  +  list [catch {
          141  +  db eval { SELECT * FROM t1 WHERE a IN (1, 2) } {
          142  +    db eval { 
          143  +      DELETE FROM t2 WHERE c=1;
          144  +    }
          145  +  }
          146  +  } msg] $msg
          147  +} {1 {database disk image is malformed}}
          148  +
          149  +finish_test
          150  +

Changes to test/e_fkey.test.

  2942   2942     execsql COMMIT
  2943   2943     catchsql "
  2944   2944       UPDATE t0 SET a = 'yyy';
  2945   2945       SELECT NOT (a='yyy') FROM t$limit;
  2946   2946     "
  2947   2947   }
  2948   2948   
  2949         -do_test e_fkey-63.1.1 {
  2950         -  test_on_delete_recursion $SQLITE_MAX_TRIGGER_DEPTH
  2951         -} {0 0}
  2952         -do_test e_fkey-63.1.2 {
  2953         -  test_on_delete_recursion [expr $SQLITE_MAX_TRIGGER_DEPTH+1]
  2954         -} {1 {too many levels of trigger recursion}}
  2955         -do_test e_fkey-63.1.3 {
  2956         -  sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 5
  2957         -  test_on_delete_recursion 5
  2958         -} {0 0}
  2959         -do_test e_fkey-63.1.4 {
  2960         -  test_on_delete_recursion 6
  2961         -} {1 {too many levels of trigger recursion}}
  2962         -do_test e_fkey-63.1.5 {
  2963         -  sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000000
  2964         -} {5}
  2965         -do_test e_fkey-63.2.1 {
  2966         -  test_on_update_recursion $SQLITE_MAX_TRIGGER_DEPTH
  2967         -} {0 0}
  2968         -do_test e_fkey-63.2.2 {
  2969         -  test_on_update_recursion [expr $SQLITE_MAX_TRIGGER_DEPTH+1]
  2970         -} {1 {too many levels of trigger recursion}}
  2971         -do_test e_fkey-63.2.3 {
  2972         -  sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 5
  2973         -  test_on_update_recursion 5
  2974         -} {0 0}
  2975         -do_test e_fkey-63.2.4 {
  2976         -  test_on_update_recursion 6
  2977         -} {1 {too many levels of trigger recursion}}
  2978         -do_test e_fkey-63.2.5 {
  2979         -  sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000000
  2980         -} {5}
         2949  +# If the current build was created using clang with the -fsanitize=address
         2950  +# switch, then the library uses considerably more stack space than usual.
         2951  +# So much more, that some of the following tests cause stack overflows
         2952  +# if they are run under this configuration.
         2953  +#
         2954  +if {[clang_sanitize_address]==0} {
         2955  +  do_test e_fkey-63.1.1 {
         2956  +    test_on_delete_recursion $SQLITE_MAX_TRIGGER_DEPTH
         2957  +  } {0 0}
         2958  +  do_test e_fkey-63.1.2 {
         2959  +    test_on_delete_recursion [expr $SQLITE_MAX_TRIGGER_DEPTH+1]
         2960  +  } {1 {too many levels of trigger recursion}}
         2961  +  do_test e_fkey-63.1.3 {
         2962  +    sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 5
         2963  +      test_on_delete_recursion 5
         2964  +  } {0 0}
         2965  +  do_test e_fkey-63.1.4 {
         2966  +    test_on_delete_recursion 6
         2967  +  } {1 {too many levels of trigger recursion}}
         2968  +  do_test e_fkey-63.1.5 {
         2969  +    sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000000
         2970  +  } {5}
         2971  +  do_test e_fkey-63.2.1 {
         2972  +    test_on_update_recursion $SQLITE_MAX_TRIGGER_DEPTH
         2973  +  } {0 0}
         2974  +  do_test e_fkey-63.2.2 {
         2975  +    test_on_update_recursion [expr $SQLITE_MAX_TRIGGER_DEPTH+1]
         2976  +  } {1 {too many levels of trigger recursion}}
         2977  +  do_test e_fkey-63.2.3 {
         2978  +    sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 5
         2979  +      test_on_update_recursion 5
         2980  +  } {0 0}
         2981  +  do_test e_fkey-63.2.4 {
         2982  +    test_on_update_recursion 6
         2983  +  } {1 {too many levels of trigger recursion}}
         2984  +  do_test e_fkey-63.2.5 {
         2985  +    sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000000
         2986  +  } {5}
         2987  +}
  2981   2988   
  2982   2989   #-------------------------------------------------------------------------
  2983   2990   # The setting of the recursive_triggers pragma does not affect foreign
  2984   2991   # key actions.
  2985   2992   #
  2986   2993   # EVIDENCE-OF: R-51769-32730 The PRAGMA recursive_triggers setting does
  2987   2994   # not not affect the operation of foreign key actions.

Added test/fts3join.test.

            1  +# 2014 January 4
            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.  The
           12  +# focus of this script is testing the FTS3 module.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set ::testprefix fts3join
           18  +
           19  +# If SQLITE_ENABLE_FTS3 is defined, omit this file.
           20  +ifcapable !fts3 {
           21  +  finish_test
           22  +  return
           23  +}
           24  +
           25  +do_execsql_test 1.0 {
           26  +  CREATE VIRTUAL TABLE ft1 USING fts4(x);
           27  +  INSERT INTO ft1 VALUES('aaa aaa');
           28  +  INSERT INTO ft1 VALUES('aaa bbb');
           29  +  INSERT INTO ft1 VALUES('bbb aaa');
           30  +  INSERT INTO ft1 VALUES('bbb bbb');
           31  +
           32  +  CREATE TABLE t1(id, y);
           33  +  INSERT INTO t1 VALUES(1, 'aaa');
           34  +  INSERT INTO t1 VALUES(2, 'bbb');
           35  +}
           36  +
           37  +do_execsql_test 1.1 {
           38  +  SELECT docid FROM ft1, t1 WHERE ft1 MATCH y AND id=1;
           39  +} {1 2 3}
           40  +
           41  +do_execsql_test 1.2 {
           42  +  SELECT docid FROM ft1, t1 WHERE ft1 MATCH y AND id=1 ORDER BY docid;
           43  +} {1 2 3}
           44  +
           45  +do_execsql_test 2.0 {
           46  +  CREATE VIRTUAL TABLE ft2 USING fts4(x);
           47  +  CREATE VIRTUAL TABLE ft3 USING fts4(y);
           48  +
           49  +  INSERT INTO ft2 VALUES('abc');
           50  +  INSERT INTO ft2 VALUES('def');
           51  +  INSERT INTO ft3 VALUES('ghi');
           52  +  INSERT INTO ft3 VALUES('abc');
           53  +}
           54  +
           55  +do_execsql_test 2.1 { SELECT * FROM ft2, ft3 WHERE x MATCH y; } {abc abc}
           56  +do_execsql_test 2.2 { SELECT * FROM ft2, ft3 WHERE y MATCH x; } {abc abc}
           57  +do_execsql_test 2.3 { SELECT * FROM ft3, ft2 WHERE x MATCH y; } {abc abc}
           58  +do_execsql_test 2.4 { SELECT * FROM ft3, ft2 WHERE y MATCH x; } {abc abc}
           59  +
           60  +do_catchsql_test 2.5 { 
           61  +  SELECT * FROM ft3, ft2 WHERE y MATCH x AND x MATCH y; 
           62  +} {1 {unable to use function MATCH in the requested context}}
           63  +
           64  +finish_test
           65  +
           66  +

Changes to test/keyword1.test.

    61     61     match
    62     62     of
    63     63     offset
    64     64     plan
    65     65     pragma
    66     66     query
    67     67     raise
           68  +  recursive
    68     69     regexp
    69     70     reindex
    70     71     release
    71     72     rename
    72     73     replace
    73     74     restrict
    74     75     rollback
................................................................................
    76     77     savepoint
    77     78     temp
    78     79     temporary
    79     80     trigger
    80     81     vacuum
    81     82     view
    82     83     virtual
           84  +  with
           85  +  without
    83     86   };
    84     87   set exprkw {
    85     88     cast
    86     89     current_date
    87     90     current_time
    88     91     current_timestamp
    89     92     raise

Changes to test/like.test.

   888    888     }
   889    889   } {abc abcd ABC ABCD sort {} t11cnc}
   890    890   do_test like-11.10 {
   891    891     queryplan {
   892    892       SELECT c FROM t11 WHERE c GLOB 'abc*' ORDER BY +a;
   893    893     }
   894    894   } {abc abcd sort {} t11cb}
          895  +
          896  +# A COLLATE clause on the pattern does not change the result of a
          897  +# LIKE operator.
          898  +#
          899  +do_execsql_test like-12.1 {
          900  +  CREATE TABLE t12nc(id INTEGER, x TEXT UNIQUE COLLATE nocase);
          901  +  INSERT INTO t12nc VALUES(1,'abcde'),(2,'uvwxy'),(3,'ABCDEF');
          902  +  CREATE TABLE t12b(id INTEGER, x TEXT UNIQUE COLLATE binary);
          903  +  INSERT INTO t12b VALUES(1,'abcde'),(2,'uvwxy'),(3,'ABCDEF');
          904  +  SELECT id FROM t12nc WHERE x LIKE 'abc%' ORDER BY +id;
          905  +} {1 3}
          906  +do_execsql_test like-12.2 {
          907  +  SELECT id FROM t12b WHERE x LIKE 'abc%' ORDER BY +id;
          908  +} {1 3}
          909  +do_execsql_test like-12.3 {
          910  +  SELECT id FROM t12nc WHERE x LIKE 'abc%' COLLATE binary ORDER BY +id;
          911  +} {1 3}
          912  +do_execsql_test like-12.4 {
          913  +  SELECT id FROM t12b WHERE x LIKE 'abc%' COLLATE binary ORDER BY +id;
          914  +} {1 3}
          915  +do_execsql_test like-12.5 {
          916  +  SELECT id FROM t12nc WHERE x LIKE 'abc%' COLLATE nocase ORDER BY +id;
          917  +} {1 3}
          918  +do_execsql_test like-12.6 {
          919  +  SELECT id FROM t12b WHERE x LIKE 'abc%' COLLATE nocase ORDER BY +id;
          920  +} {1 3}
          921  +
          922  +# Adding a COLLATE clause to the pattern of a LIKE operator does nothing
          923  +# to change the suitability of using an index to satisfy that LIKE
          924  +# operator.
          925  +#
          926  +do_execsql_test like-12.11 {
          927  +  EXPLAIN QUERY PLAN
          928  +  SELECT id FROM t12nc WHERE x LIKE 'abc%' ORDER BY +id;
          929  +} {/SEARCH/}
          930  +do_execsql_test like-12.12 {
          931  +  EXPLAIN QUERY PLAN
          932  +  SELECT id FROM t12b WHERE x LIKE 'abc%' ORDER BY +id;
          933  +} {/SCAN/}
          934  +do_execsql_test like-12.13 {
          935  +  EXPLAIN QUERY PLAN
          936  +  SELECT id FROM t12nc WHERE x LIKE 'abc%' COLLATE nocase ORDER BY +id;
          937  +} {/SEARCH/}
          938  +do_execsql_test like-12.14 {
          939  +  EXPLAIN QUERY PLAN
          940  +  SELECT id FROM t12b WHERE x LIKE 'abc%' COLLATE nocase ORDER BY +id;
          941  +} {/SCAN/}
          942  +do_execsql_test like-12.15 {
          943  +  EXPLAIN QUERY PLAN
          944  +  SELECT id FROM t12nc WHERE x LIKE 'abc%' COLLATE binary ORDER BY +id;
          945  +} {/SEARCH/}
          946  +do_execsql_test like-12.16 {
          947  +  EXPLAIN QUERY PLAN
          948  +  SELECT id FROM t12b WHERE x LIKE 'abc%' COLLATE binary ORDER BY +id;
          949  +} {/SCAN/}
   895    950   
   896    951   
   897    952   finish_test

Changes to test/misc1.test.

   587    587   } {2 3}
   588    588   }
   589    589   
   590    590   do_test misc1-18.1 {
   591    591     set n [sqlite3_sleep 100]
   592    592     expr {$n>=100}
   593    593   } {1}
          594  +
          595  +# 2014-01-10:  In a CREATE TABLE AS, if one or more of the column names
          596  +# are an empty string, that is still OK.
          597  +#
          598  +do_execsql_test misc1-19.1 {
          599  +  CREATE TABLE t19 AS SELECT 1, 2 AS '', 3;
          600  +  SELECT * FROM t19;
          601  +} {1 2 3}
          602  +do_execsql_test misc1-19.2 {
          603  +  CREATE TABLE t19b AS SELECT 4 AS '', 5 AS '',  6 AS '';
          604  +  SELECT * FROM t19b;
          605  +} {4 5 6}
          606  +
   594    607   
   595    608   finish_test

Changes to test/misc7.test.

    11     11   # This file implements regression tests for SQLite library.
    12     12   #
    13     13   # $Id: misc7.test,v 1.29 2009/07/16 18:21:18 drh Exp $
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17     17   
    18         -do_test misc7-1-misuse {
    19         -  c_misuse_test
    20         -} {}
           18  +if {[clang_sanitize_address]==0} {
           19  +  do_test misc7-1-misuse {
           20  +    c_misuse_test
           21  +  } {}
           22  +}
    21     23   
    22     24   do_test misc7-2 {
    23     25     c_realloc_test
    24     26   } {}
    25     27   
    26     28   do_test misc7-3 {
    27     29     c_collation_test

Changes to test/misuse.test.

   167    167     set v [catch {
   168    168       db eval {SELECT * FROM t1} {} {
   169    169         set r [sqlite3_close $::DB]
   170    170       }
   171    171     } msg]
   172    172     lappend v $msg $r
   173    173   } {0 {} SQLITE_BUSY}
   174         -do_test misuse-4.4 {
          174  +
          175  +if {[clang_sanitize_address]==0} {
          176  +  do_test misuse-4.4 {
   175    177     # Flush the TCL statement cache here, otherwise the sqlite3_close() will
   176    178     # fail because there are still un-finalized() VDBEs.
   177         -  db cache flush
   178         -  sqlite3_close $::DB
   179         -  catchsql2 {SELECT * FROM t1}
   180         -} {1 {library routine called out of sequence}}
   181         -do_test misuse-4.5 {
   182         -  catchsql {
   183         -    SELECT * FROM t1
   184         -  }
   185         -} {1 {library routine called out of sequence}}
          179  +    db cache flush
          180  +      sqlite3_close $::DB
          181  +      catchsql2 {SELECT * FROM t1}
          182  +  } {1 {library routine called out of sequence}}
          183  +  do_test misuse-4.5 {
          184  +    catchsql {
          185  +      SELECT * FROM t1
          186  +    }
          187  +  } {1 {library routine called out of sequence}}
   186    188   
   187         -# Attempt to use a database after it has been closed.
   188         -#
   189         -do_test misuse-5.1 {
   190         -  db close
   191         -  sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
   192         -  execsql {
   193         -    SELECT * FROM t1
   194         -  }
   195         -} {1 2}
   196         -do_test misuse-5.2 {
   197         -  catchsql2 {SELECT * FROM t1}
   198         -} {0 {a b 1 2}}
   199         -do_test misuse-5.3 {
   200         -  db close
   201         -  set r [catch {
   202         -    sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL
   203         -  } msg]
   204         -  lappend r $msg
   205         -} {1 {(21) library routine called out of sequence}}
          189  +  # Attempt to use a database after it has been closed.
          190  +  #
          191  +  do_test misuse-5.1 {
          192  +    db close
          193  +      sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
          194  +      execsql {
          195  +        SELECT * FROM t1
          196  +      }
          197  +  } {1 2}
          198  +  do_test misuse-5.2 {
          199  +    catchsql2 {SELECT * FROM t1}
          200  +  } {0 {a b 1 2}}
          201  +  do_test misuse-5.3 {
          202  +    db close
          203  +      set r [catch {
          204  +        sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL
          205  +      } msg]
          206  +    lappend r $msg
          207  +  } {1 {(21) library routine called out of sequence}}
          208  +}
   206    209   
   207    210   finish_test

Changes to test/select7.test.

   134    134     } [list 1 \
   135    135        {only a single result allowed for a SELECT that is part of an expression}]
   136    136   }
   137    137   
   138    138   # Verify that an error occurs if you have too many terms on a
   139    139   # compound select statement.
   140    140   #
   141         -ifcapable compound {
   142         -  if {$SQLITE_MAX_COMPOUND_SELECT>0} {
   143         -    set sql {SELECT 0}
   144         -    set result 0
   145         -    for {set i 1} {$i<$SQLITE_MAX_COMPOUND_SELECT} {incr i} {
   146         -      append sql " UNION ALL SELECT $i"
   147         -      lappend result $i
          141  +if {[clang_sanitize_address]==0} {
          142  +  ifcapable compound {
          143  +    if {$SQLITE_MAX_COMPOUND_SELECT>0} {
          144  +      set sql {SELECT 0}
          145  +      set result 0
          146  +        for {set i 1} {$i<$SQLITE_MAX_COMPOUND_SELECT} {incr i} {
          147  +          append sql " UNION ALL SELECT $i"
          148  +            lappend result $i
          149  +        }
          150  +      do_test select7-6.1 {
          151  +        catchsql $sql
          152  +      } [list 0 $result]
          153  +      append sql { UNION ALL SELECT 99999999}
          154  +      do_test select7-6.2 {
          155  +        catchsql $sql
          156  +      } {1 {too many terms in compound SELECT}}
   148    157       }
   149         -    do_test select7-6.1 {
   150         -      catchsql $sql
   151         -    } [list 0 $result]
   152         -    append sql { UNION ALL SELECT 99999999}
   153         -    do_test select7-6.2 {
   154         -      catchsql $sql
   155         -    } {1 {too many terms in compound SELECT}}
   156    158     }
   157    159   }
   158    160   
   159    161   # This block of tests verifies that bug aa92c76cd4 is fixed.
   160    162   #
   161    163   do_test select7-7.1 {
   162    164     execsql {

Changes to test/spellfix.test.

   216    216     5   kusher     {kosher 16}
   217    217   } {
   218    218     do_execsql_test 5.1.$tn {
   219    219       SELECT word, distance FROM t3 WHERE word MATCH $word
   220    220        ORDER BY score, word LIMIT 1
   221    221     } $res
   222    222   }
          223  +
          224  +#-------------------------------------------------------------------------
          225  +# Try some queries by rowid.
          226  +#
          227  +do_execsql_test 6.1.1 {
          228  +  SELECT word FROM t3 WHERE rowid = 10;
          229  +} {keener}
          230  +do_execsql_test 6.1.2 {
          231  +  SELECT word, distance FROM t3 WHERE rowid = 10;
          232  +} {keener {}}
          233  +do_execsql_test 6.1.3 {
          234  +  SELECT word, distance FROM t3 WHERE rowid = 10 AND word MATCH 'kiiner';
          235  +} {keener 300}
          236  +
          237  +proc trace_callback {sql} {
          238  +  if {[string range $sql 0 2] == "-- "} {
          239  +    lappend ::trace [string range $sql 3 end]
          240  +  }
          241  +}
          242  +
          243  +proc do_tracesql_test {tn sql {res {}}} {
          244  +  set ::trace [list]
          245  +  uplevel [list do_test $tn [subst -nocommands {
          246  +    set vals [execsql {$sql}]
          247  +    concat [set vals] [set ::trace]
          248  +  }] [list {*}$res]]
          249  +}
          250  +
          251  +db trace trace_callback
          252  +do_tracesql_test 6.2.1 {
          253  +  SELECT word FROM t3 WHERE rowid = 10;
          254  +} {keener
          255  +  {SELECT word, rank, NULL, langid, id FROM "main"."t3_vocab" WHERE rowid=?}
          256  +}
          257  +do_tracesql_test 6.2.2 {
          258  +  SELECT word, distance FROM t3 WHERE rowid = 10;
          259  +} {keener {}
          260  +  {SELECT word, rank, NULL, langid, id FROM "main"."t3_vocab" WHERE rowid=?}
          261  +}
          262  +do_tracesql_test 6.2.3 {
          263  +  SELECT word, distance FROM t3 WHERE rowid = 10 AND word MATCH 'kiiner';
          264  +} {keener 300
          265  +  {SELECT id, word, rank, k1  FROM "main"."t3_vocab" WHERE langid=0 AND k2>=?1 AND k2<?2}
          266  +}
          267  +
   223    268   
   224    269   
   225    270   
   226    271   finish_test

Changes to test/vtab1.test.

  1371   1371     SELECT a, b FROM (
  1372   1372         SELECT a, b FROM t7v WHERE a=11 OR b=12
  1373   1373         UNION ALL
  1374   1374         SELECT c, d FROM t8v WHERE c=5 OR d=6
  1375   1375     )
  1376   1376     ORDER BY 1, 2;
  1377   1377   } {5 5 6 6 11 11 12 12}
         1378  +
         1379  +#-------------------------------------------------------------------------
         1380  +#
         1381  +do_execsql_test 21.1 {
         1382  +  CREATE TABLE t9(a,b,c);
         1383  +  CREATE VIRTUAL TABLE t9v USING echo(t9);
         1384  +
         1385  +  INSERT INTO t9 VALUES(1,2,3);
         1386  +  INSERT INTO t9 VALUES(3,2,1);
         1387  +  INSERT INTO t9 VALUES(2,2,2);
         1388  +}
         1389  +
         1390  +do_execsql_test 21.2 {
         1391  +  SELECT * FROM t9v WHERE a<b;
         1392  +} {1 2 3}
         1393  +
         1394  +do_execsql_test 21.3 {
         1395  +  SELECT * FROM t9v WHERE a=b;
         1396  +} {2 2 2}
  1378   1397   
  1379   1398   finish_test

Added test/with1.test.

            1  +# 2014 January 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.  The
           12  +# focus of this file is testing the WITH clause.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set ::testprefix with1
           18  +
           19  +ifcapable {!cte} {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +do_execsql_test 1.0 {
           25  +  CREATE TABLE t1(x INTEGER, y INTEGER);
           26  +  WITH x(a) AS ( SELECT * FROM t1) SELECT 10
           27  +} {10}
           28  +
           29  +do_execsql_test 1.1 {
           30  +  SELECT * FROM ( WITH x AS ( SELECT * FROM t1) SELECT 10 );
           31  +} {10}
           32  +
           33  +do_execsql_test 1.2 {
           34  +  WITH x(a) AS ( SELECT * FROM t1) INSERT INTO t1 VALUES(1,2);
           35  +} {}
           36  +
           37  +do_execsql_test 1.3 {
           38  +  WITH x(a) AS ( SELECT * FROM t1) DELETE FROM t1;
           39  +} {}
           40  +
           41  +do_execsql_test 1.4 {
           42  +  WITH x(a) AS ( SELECT * FROM t1) UPDATE t1 SET x = y;
           43  +} {}
           44  + 
           45  +#--------------------------------------------------------------------------
           46  +
           47  +do_execsql_test 2.1 {
           48  +  DROP TABLE IF EXISTS t1;
           49  +  CREATE TABLE t1(x);
           50  +  INSERT INTO t1 VALUES(1);
           51  +  INSERT INTO t1 VALUES(2);
           52  +  WITH tmp AS ( SELECT * FROM t1 ) SELECT x FROM tmp;
           53  +} {1 2}
           54  +
           55  +do_execsql_test 2.2 {
           56  +  WITH tmp(a) AS ( SELECT * FROM t1 ) SELECT a FROM tmp;
           57  +} {1 2}
           58  +
           59  +do_execsql_test 2.3 {
           60  +  SELECT * FROM (
           61  +    WITH tmp(a) AS ( SELECT * FROM t1 ) SELECT a FROM tmp
           62  +  );
           63  +} {1 2}
           64  +
           65  +do_execsql_test 2.4 {
           66  +  WITH tmp1(a) AS ( SELECT * FROM t1 ),
           67  +       tmp2(x) AS ( SELECT * FROM tmp1)
           68  +  SELECT * FROM tmp2;
           69  +} {1 2}
           70  +
           71  +do_execsql_test 2.5 {
           72  +  WITH tmp2(x) AS ( SELECT * FROM tmp1),
           73  +       tmp1(a) AS ( SELECT * FROM t1 )
           74  +  SELECT * FROM tmp2;
           75  +} {1 2}
           76  +
           77  +#-------------------------------------------------------------------------
           78  +do_catchsql_test 3.1 {
           79  +  WITH tmp2(x) AS ( SELECT * FROM tmp1 ),
           80  +       tmp1(a) AS ( SELECT * FROM tmp2 )
           81  +  SELECT * FROM tmp1;
           82  +} {1 {circular reference: tmp1}}
           83  +
           84  +do_catchsql_test 3.2 {
           85  +  CREATE TABLE t2(x INTEGER);
           86  +  WITH tmp(a) AS (SELECT * FROM t1),
           87  +       tmp(a) AS (SELECT * FROM t1)
           88  +  SELECT * FROM tmp;
           89  +} {1 {duplicate WITH table name: tmp}}
           90  +
           91  +do_execsql_test 3.3 {
           92  +  CREATE TABLE t3(x);
           93  +  CREATE TABLE t4(x);
           94  +
           95  +  INSERT INTO t3 VALUES('T3');
           96  +  INSERT INTO t4 VALUES('T4');
           97  +
           98  +  WITH t3(a) AS (SELECT * FROM t4)
           99  +  SELECT * FROM t3;
          100  +} {T4}
          101  +
          102  +do_execsql_test 3.4 {
          103  +  WITH tmp  AS ( SELECT * FROM t3 ),
          104  +       tmp2 AS ( WITH tmp AS ( SELECT * FROM t4 ) SELECT * FROM tmp )
          105  +  SELECT * FROM tmp2;
          106  +} {T4}
          107  +
          108  +do_execsql_test 3.5 {
          109  +  WITH tmp  AS ( SELECT * FROM t3 ),
          110  +       tmp2 AS ( WITH xxxx AS ( SELECT * FROM t4 ) SELECT * FROM tmp )
          111  +  SELECT * FROM tmp2;
          112  +} {T3}
          113  +
          114  +do_catchsql_test 3.6 {
          115  +  WITH tmp AS ( SELECT * FROM t3 ),
          116  +  SELECT * FROM tmp;
          117  +} {1 {near "SELECT": syntax error}}
          118  +
          119  +#-------------------------------------------------------------------------
          120  +do_execsql_test 4.1 {
          121  +  DROP TABLE IF EXISTS t1;
          122  +  CREATE TABLE t1(x);
          123  +  INSERT INTO t1 VALUES(1);
          124  +  INSERT INTO t1 VALUES(2);
          125  +  INSERT INTO t1 VALUES(3);
          126  +  INSERT INTO t1 VALUES(4);
          127  +
          128  +  WITH dset AS ( SELECT 2 UNION ALL SELECT 4 )
          129  +  DELETE FROM t1 WHERE x IN dset;
          130  +  SELECT * FROM t1;
          131  +} {1 3}
          132  +
          133  +do_execsql_test 4.2 {
          134  +  WITH iset AS ( SELECT 2 UNION ALL SELECT 4 )
          135  +  INSERT INTO t1 SELECT * FROM iset;
          136  +  SELECT * FROM t1;
          137  +} {1 3 2 4}
          138  +
          139  +do_execsql_test 4.3 {
          140  +  WITH uset(a, b) AS ( SELECT 2, 8 UNION ALL SELECT 4, 9 )
          141  +  UPDATE t1 SET x = COALESCE( (SELECT b FROM uset WHERE a=x), x );
          142  +  SELECT * FROM t1;
          143  +} {1 3 8 9}
          144  +
          145  +#-------------------------------------------------------------------------
          146  +#
          147  +do_execsql_test 5.1 {
          148  +  WITH i(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM i)
          149  +  SELECT x FROM i LIMIT 10;
          150  +} {1 2 3 4 5 6 7 8 9 10}
          151  +
          152  +do_catchsql_test 5.2 {
          153  +  WITH i(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM i ORDER BY 1)
          154  +  SELECT x FROM i LIMIT 10;
          155  +} {0 {1 2 3 4 5 6 7 8 9 10}}
          156  +
          157  +do_execsql_test 5.2.1 {
          158  +  CREATE TABLE edge(xfrom, xto, seq, PRIMARY KEY(xfrom, xto)) WITHOUT ROWID;
          159  +  INSERT INTO edge VALUES(0, 1, 10);
          160  +  INSERT INTO edge VALUES(1, 2, 20);
          161  +  INSERT INTO edge VALUES(0, 3, 30);
          162  +  INSERT INTO edge VALUES(2, 4, 40);
          163  +  INSERT INTO edge VALUES(3, 4, 40);
          164  +  INSERT INTO edge VALUES(2, 5, 50);
          165  +  INSERT INTO edge VALUES(3, 6, 60);
          166  +  INSERT INTO edge VALUES(5, 7, 70);
          167  +  INSERT INTO edge VALUES(3, 7, 70);
          168  +  INSERT INTO edge VALUES(4, 8, 80);
          169  +  INSERT INTO edge VALUES(7, 8, 80);
          170  +  INSERT INTO edge VALUES(8, 9, 90);
          171  +  
          172  +  WITH RECURSIVE
          173  +    ancest(id, mtime) AS
          174  +      (VALUES(0, 0)
          175  +       UNION
          176  +       SELECT edge.xto, edge.seq FROM edge, ancest
          177  +        WHERE edge.xfrom=ancest.id
          178  +        ORDER BY 2
          179  +      )
          180  +  SELECT * FROM ancest;
          181  +} {0 0 1 10 2 20 3 30 4 40 5 50 6 60 7 70 8 80 9 90}
          182  +do_execsql_test 5.2.2 {
          183  +  WITH RECURSIVE
          184  +    ancest(id, mtime) AS
          185  +      (VALUES(0, 0)
          186  +       UNION ALL
          187  +       SELECT edge.xto, edge.seq FROM edge, ancest
          188  +        WHERE edge.xfrom=ancest.id
          189  +        ORDER BY 2
          190  +      )
          191  +  SELECT * FROM ancest;
          192  +} {0 0 1 10 2 20 3 30 4 40 4 40 5 50 6 60 7 70 7 70 8 80 8 80 8 80 8 80 9 90 9 90 9 90 9 90}
          193  +do_execsql_test 5.2.3 {
          194  +  WITH RECURSIVE
          195  +    ancest(id, mtime) AS
          196  +      (VALUES(0, 0)
          197  +       UNION ALL
          198  +       SELECT edge.xto, edge.seq FROM edge, ancest
          199  +        WHERE edge.xfrom=ancest.id
          200  +        ORDER BY 2 LIMIT 4 OFFSET 2
          201  +      )
          202  +  SELECT * FROM ancest;
          203  +} {2 20 3 30 4 40 4 40}
          204  +
          205  +do_catchsql_test 5.3 {
          206  +  WITH i(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM i LIMIT 5)
          207  +  SELECT x FROM i;
          208  +} {0 {1 2 3 4 5}}
          209  +
          210  +do_execsql_test 5.4 {
          211  +  WITH i(x) AS ( VALUES(1) UNION ALL SELECT (x+1)%10 FROM i)
          212  +  SELECT x FROM i LIMIT 20;
          213  +} {1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0}
          214  +
          215  +do_execsql_test 5.5 {
          216  +  WITH i(x) AS ( VALUES(1) UNION SELECT (x+1)%10 FROM i)
          217  +  SELECT x FROM i LIMIT 20;
          218  +} {1 2 3 4 5 6 7 8 9 0}
          219  +
          220  +do_catchsql_test 5.6.1 {
          221  +  WITH i(x, y) AS ( VALUES(1) )
          222  +  SELECT * FROM i;
          223  +} {1 {table i has 1 values for 2 columns}}
          224  +
          225  +do_catchsql_test 5.6.2 {
          226  +  WITH i(x) AS ( VALUES(1,2) )
          227  +  SELECT * FROM i;
          228  +} {1 {table i has 2 values for 1 columns}}
          229  +
          230  +do_catchsql_test 5.6.3 {
          231  +  CREATE TABLE t5(a, b);
          232  +  WITH i(x) AS ( SELECT * FROM t5 )
          233  +  SELECT * FROM i;
          234  +} {1 {table i has 2 values for 1 columns}}
          235  +
          236  +do_catchsql_test 5.6.4 {
          237  +  WITH i(x) AS ( SELECT 1, 2 UNION ALL SELECT 1 )
          238  +  SELECT * FROM i;
          239  +} {1 {table i has 2 values for 1 columns}}
          240  +
          241  +do_catchsql_test 5.6.5 {
          242  +  WITH i(x) AS ( SELECT 1 UNION ALL SELECT 1, 2 )
          243  +  SELECT * FROM i;
          244  +} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
          245  +
          246  +do_catchsql_test 5.6.6 {
          247  +  WITH i(x) AS ( SELECT 1 UNION ALL SELECT x+1, x*2 FROM i )
          248  +  SELECT * FROM i;
          249  +} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
          250  +
          251  +do_catchsql_test 5.6.7 {
          252  +  WITH i(x) AS ( SELECT 1, 2 UNION SELECT x+1 FROM i )
          253  +  SELECT * FROM i;
          254  +} {1 {table i has 2 values for 1 columns}}
          255  +
          256  +#-------------------------------------------------------------------------
          257  +#
          258  +do_execsql_test 6.1 {
          259  +  CREATE TABLE f(
          260  +      id INTEGER PRIMARY KEY, parentid REFERENCES f, name TEXT
          261  +  );
          262  +
          263  +  INSERT INTO f VALUES(0, NULL, '');
          264  +  INSERT INTO f VALUES(1, 0, 'bin');
          265  +    INSERT INTO f VALUES(2, 1, 'true');
          266  +    INSERT INTO f VALUES(3, 1, 'false');
          267  +    INSERT INTO f VALUES(4, 1, 'ls');
          268  +    INSERT INTO f VALUES(5, 1, 'grep');
          269  +  INSERT INTO f VALUES(6, 0, 'etc');
          270  +    INSERT INTO f VALUES(7, 6, 'rc.d');
          271  +      INSERT INTO f VALUES(8, 7, 'rc.apache');
          272  +      INSERT INTO f VALUES(9, 7, 'rc.samba');
          273  +  INSERT INTO f VALUES(10, 0, 'home');
          274  +    INSERT INTO f VALUES(11, 10, 'dan');
          275  +      INSERT INTO f VALUES(12, 11, 'public_html');
          276  +        INSERT INTO f VALUES(13, 12, 'index.html');
          277  +          INSERT INTO f VALUES(14, 13, 'logo.gif');
          278  +}
          279  +
          280  +do_execsql_test 6.2 {
          281  +  WITH flat(fid, fpath) AS (
          282  +    SELECT id, '' FROM f WHERE parentid IS NULL
          283  +    UNION ALL
          284  +    SELECT id, fpath || '/' || name FROM f, flat WHERE parentid=fid
          285  +  )
          286  +  SELECT fpath FROM flat WHERE fpath!='' ORDER BY 1;
          287  +} {
          288  +  /bin 
          289  +  /bin/false /bin/grep /bin/ls /bin/true 
          290  +  /etc 
          291  +  /etc/rc.d 
          292  +  /etc/rc.d/rc.apache /etc/rc.d/rc.samba 
          293  +  /home 
          294  +  /home/dan 
          295  +  /home/dan/public_html 
          296  +  /home/dan/public_html/index.html 
          297  +  /home/dan/public_html/index.html/logo.gif
          298  +}
          299  +
          300  +do_execsql_test 6.3 {
          301  +  WITH flat(fid, fpath) AS (
          302  +    SELECT id, '' FROM f WHERE parentid IS NULL
          303  +    UNION ALL
          304  +    SELECT id, fpath || '/' || name FROM f, flat WHERE parentid=fid
          305  +  )
          306  +  SELECT count(*) FROM flat;
          307  +} {15}
          308  +
          309  +do_execsql_test 6.4 {
          310  +  WITH x(i) AS (
          311  +    SELECT 1
          312  +    UNION ALL
          313  +    SELECT i+1 FROM x WHERE i<10
          314  +  )
          315  +  SELECT count(*) FROM x
          316  +} {10}
          317  +
          318  +
          319  +#-------------------------------------------------------------------------
          320  +
          321  +do_execsql_test 7.1 {
          322  +  CREATE TABLE tree(i, p);
          323  +  INSERT INTO tree VALUES(1, NULL);
          324  +  INSERT INTO tree VALUES(2, 1);
          325  +  INSERT INTO tree VALUES(3, 1);
          326  +  INSERT INTO tree VALUES(4, 2);
          327  +  INSERT INTO tree VALUES(5, 4);
          328  +}
          329  +
          330  +do_execsql_test 7.2 {
          331  +  WITH t(id, path) AS (
          332  +    SELECT i, '' FROM tree WHERE p IS NULL
          333  +    UNION ALL
          334  +    SELECT i, path || '/' || i FROM tree, t WHERE p = id
          335  +  ) 
          336  +  SELECT path FROM t;
          337  +} {{} /2 /3 /2/4 /2/4/5}
          338  +
          339  +do_execsql_test 7.3 {
          340  +  WITH t(id) AS (
          341  +    VALUES(2)
          342  +    UNION ALL
          343  +    SELECT i FROM tree, t WHERE p = id
          344  +  ) 
          345  +  SELECT id FROM t;
          346  +} {2 4 5}
          347  +
          348  +do_catchsql_test 7.4 {
          349  +  WITH t(id) AS (
          350  +    VALUES(2)
          351  +    UNION ALL
          352  +    SELECT i FROM tree WHERE p IN (SELECT id FROM t)
          353  +  ) 
          354  +  SELECT id FROM t;
          355  +} {1 {recursive reference in a subquery: t}}
          356  +
          357  +do_catchsql_test 7.5 {
          358  +  WITH t(id) AS (
          359  +    VALUES(2)
          360  +    UNION ALL
          361  +    SELECT i FROM tree, t WHERE p = id AND p IN (SELECT id FROM t)
          362  +  ) 
          363  +  SELECT id FROM t;
          364  +} {1 {multiple recursive references: t}}
          365  +
          366  +do_catchsql_test 7.6 {
          367  +  WITH t(id) AS (
          368  +    SELECT i FROM tree WHERE 2 IN (SELECT id FROM t)
          369  +    UNION ALL
          370  +    SELECT i FROM tree, t WHERE p = id
          371  +  ) 
          372  +  SELECT id FROM t;
          373  +} {1 {circular reference: t}}
          374  +
          375  +# Compute the mandelbrot set using a recursive query
          376  +#
          377  +do_execsql_test 8.1-mandelbrot {
          378  +  WITH RECURSIVE
          379  +    xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2),
          380  +    yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+0.1 FROM yaxis WHERE y<1.0),
          381  +    m(iter, cx, cy, x, y) AS (
          382  +      SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis
          383  +      UNION ALL
          384  +      SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m 
          385  +       WHERE (x*x + y*y) < 4.0 AND iter<28
          386  +    ),
          387  +    m2(iter, cx, cy) AS (
          388  +      SELECT max(iter), cx, cy FROM m GROUP BY cx, cy
          389  +    ),
          390  +    a(t) AS (
          391  +      SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') 
          392  +      FROM m2 GROUP BY cy
          393  +    )
          394  +  SELECT group_concat(rtrim(t),x'0a') FROM a;
          395  +} {{                                    ....#
          396  +                                   ..#*..
          397  +                                 ..+####+.
          398  +                            .......+####....   +
          399  +                           ..##+*##########+.++++
          400  +                          .+.##################+.
          401  +              .............+###################+.+
          402  +              ..++..#.....*#####################+.
          403  +             ...+#######++#######################.
          404  +          ....+*################################.
          405  + #############################################...
          406  +          ....+*################################.
          407  +             ...+#######++#######################.
          408  +              ..++..#.....*#####################+.
          409  +              .............+###################+.+
          410  +                          .+.##################+.
          411  +                           ..##+*##########+.++++
          412  +                            .......+####....   +
          413  +                                 ..+####+.
          414  +                                   ..#*..
          415  +                                    ....#
          416  +                                    +.}}
          417  +
          418  +# Solve a sudoku puzzle using a recursive query
          419  +#
          420  +do_execsql_test 8.2-soduko {
          421  +  WITH RECURSIVE
          422  +    input(sud) AS (
          423  +      VALUES('53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79')
          424  +    ),
          425  +  
          426  +    /* A table filled with digits 1..9, inclusive. */
          427  +    digits(z, lp) AS (
          428  +      VALUES('1', 1)
          429  +      UNION ALL SELECT
          430  +      CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9
          431  +    ),
          432  +  
          433  +    /* The tricky bit. */
          434  +    x(s, ind) AS (
          435  +      SELECT sud, instr(sud, '.') FROM input
          436  +      UNION ALL
          437  +      SELECT
          438  +        substr(s, 1, ind-1) || z || substr(s, ind+1),
          439  +        instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )
          440  +       FROM x, digits AS z
          441  +      WHERE ind>0
          442  +        AND NOT EXISTS (
          443  +              SELECT 1
          444  +                FROM digits AS lp
          445  +               WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)
          446  +                  OR z.z = substr(s, ((ind-1)%9) + (lp-1)*9 + 1, 1)
          447  +                  OR z.z = substr(s, (((ind-1)/3) % 3) * 3
          448  +                          + ((ind-1)/27) * 27 + lp
          449  +                          + ((lp-1) / 3) * 6, 1)
          450  +           )
          451  +    )
          452  +  SELECT s FROM x WHERE ind=0;
          453  +} {534678912672195348198342567859761423426853791713924856961537284287419635345286179}
          454  +
          455  +
          456  +# Test cases to illustrate on the ORDER BY clause on a recursive query can be
          457  +# used to control depth-first versus breath-first search in a tree.
          458  +#
          459  +do_execsql_test 9.1 {
          460  +  CREATE TABLE org(
          461  +    name TEXT PRIMARY KEY,
          462  +    boss TEXT REFERENCES org
          463  +  ) WITHOUT ROWID;
          464  +  INSERT INTO org VALUES('Alice',NULL);
          465  +  INSERT INTO org VALUES('Bob','Alice');
          466  +  INSERT INTO org VALUES('Cindy','Alice');
          467  +  INSERT INTO org VALUES('Dave','Bob');
          468  +  INSERT INTO org VALUES('Emma','Bob');
          469  +  INSERT INTO org VALUES('Fred','Cindy');
          470  +  INSERT INTO org VALUES('Gail','Cindy');
          471  +  INSERT INTO org VALUES('Harry','Dave');
          472  +  INSERT INTO org VALUES('Ingrid','Dave');
          473  +  INSERT INTO org VALUES('Jim','Emma');
          474  +  INSERT INTO org VALUES('Kate','Emma');
          475  +  INSERT INTO org VALUES('Lanny','Fred');
          476  +  INSERT INTO org VALUES('Mary','Fred');
          477  +  INSERT INTO org VALUES('Noland','Gail');
          478  +  INSERT INTO org VALUES('Olivia','Gail');
          479  +  -- The above are all under Alice.  Add a few more records for people
          480  +  -- not in Alice's group, just to prove that they won't be selected.
          481  +  INSERT INTO org VALUES('Xaviar',NULL);
          482  +  INSERT INTO org VALUES('Xia','Xaviar');
          483  +  INSERT INTO org VALUES('Xerxes','Xaviar');
          484  +  INSERT INTO org VALUES('Xena','Xia');
          485  +  -- Find all members of Alice's group, breath-first order  
          486  +  WITH RECURSIVE
          487  +    under_alice(name,level) AS (
          488  +       VALUES('Alice','0')
          489  +       UNION ALL
          490  +       SELECT org.name, under_alice.level+1
          491  +         FROM org, under_alice
          492  +        WHERE org.boss=under_alice.name
          493  +        ORDER BY 2
          494  +    )
          495  +  SELECT group_concat(substr('...............',1,level*3) || name,x'0a')
          496  +    FROM under_alice;
          497  +} {{Alice
          498  +...Bob
          499  +...Cindy
          500  +......Dave
          501  +......Emma
          502  +......Fred
          503  +......Gail
          504  +.........Harry
          505  +.........Ingrid
          506  +.........Jim
          507  +.........Kate
          508  +.........Lanny
          509  +.........Mary
          510  +.........Noland
          511  +.........Olivia}}
          512  +
          513  +# The previous query used "ORDER BY level" to yield a breath-first search.
          514  +# Change that to "ORDER BY level DESC" for a depth-first search.
          515  +#
          516  +do_execsql_test 9.2 {
          517  +  WITH RECURSIVE
          518  +    under_alice(name,level) AS (
          519  +       VALUES('Alice','0')
          520  +       UNION ALL
          521  +       SELECT org.name, under_alice.level+1
          522  +         FROM org, under_alice
          523  +        WHERE org.boss=under_alice.name
          524  +        ORDER BY 2 DESC
          525  +    )
          526  +  SELECT group_concat(substr('...............',1,level*3) || name,x'0a')
          527  +    FROM under_alice;
          528  +} {{Alice
          529  +...Bob
          530  +......Dave
          531  +.........Harry
          532  +.........Ingrid
          533  +......Emma
          534  +.........Jim
          535  +.........Kate
          536  +...Cindy
          537  +......Fred
          538  +.........Lanny
          539  +.........Mary
          540  +......Gail
          541  +.........Noland
          542  +.........Olivia}}
          543  +
          544  +# Without an ORDER BY clause, the recursive query should use a FIFO,
          545  +# resulting in a breath-first search.
          546  +#
          547  +do_execsql_test 9.3 {
          548  +  WITH RECURSIVE
          549  +    under_alice(name,level) AS (
          550  +       VALUES('Alice','0')
          551  +       UNION ALL
          552  +       SELECT org.name, under_alice.level+1
          553  +         FROM org, under_alice
          554  +        WHERE org.boss=under_alice.name
          555  +    )
          556  +  SELECT group_concat(substr('...............',1,level*3) || name,x'0a')
          557  +    FROM under_alice;
          558  +} {{Alice
          559  +...Bob
          560  +...Cindy
          561  +......Dave
          562  +......Emma
          563  +......Fred
          564  +......Gail
          565  +.........Harry
          566  +.........Ingrid
          567  +.........Jim
          568  +.........Kate
          569  +.........Lanny
          570  +.........Mary
          571  +.........Noland
          572  +.........Olivia}}
          573  +
          574  +finish_test

Added test/with2.test.

            1  +# 2014 January 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.  The
           12  +# focus of this file is testing the WITH clause.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set ::testprefix with2
           18  +
           19  +ifcapable {!cte} {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +do_execsql_test 1.0 {
           25  +  CREATE TABLE t1(a);
           26  +  INSERT INTO t1 VALUES(1);
           27  +  INSERT INTO t1 VALUES(2);
           28  +}
           29  +
           30  +do_execsql_test 1.1 {
           31  +  WITH x1 AS (SELECT * FROM t1)
           32  +  SELECT sum(a) FROM x1;
           33  +} {3}
           34  +
           35  +do_execsql_test 1.2 {
           36  +  WITH x1 AS (SELECT * FROM t1)
           37  +  SELECT (SELECT sum(a) FROM x1);
           38  +} {3}
           39  +
           40  +do_execsql_test 1.3 {
           41  +  WITH x1 AS (SELECT * FROM t1)
           42  +  SELECT (SELECT sum(a) FROM x1);
           43  +} {3}
           44  +
           45  +do_execsql_test 1.4 {
           46  +  CREATE TABLE t2(i);
           47  +  INSERT INTO t2 VALUES(2);
           48  +  INSERT INTO t2 VALUES(3);
           49  +  INSERT INTO t2 VALUES(5);
           50  +
           51  +  WITH x1   AS (SELECT i FROM t2),
           52  +       i(a) AS (
           53  +         SELECT min(i)-1 FROM x1 UNION SELECT a+1 FROM i WHERE a<10
           54  +       )
           55  +  SELECT a FROM i WHERE a NOT IN x1
           56  +} {1 4 6 7 8 9 10}
           57  +
           58  +do_execsql_test 1.5 {
           59  +  WITH x1 AS (SELECT a FROM t1),
           60  +       x2 AS (SELECT i FROM t2),
           61  +       x3 AS (SELECT * FROM x1, x2 WHERE x1.a IN x2 AND x2.i IN x1)
           62  +  SELECT * FROM x3 
           63  +} {2 2}
           64  +
           65  +do_execsql_test 1.6 {
           66  +  CREATE TABLE t3 AS SELECT 3 AS x;
           67  +  CREATE TABLE t4 AS SELECT 4 AS x;
           68  +
           69  +  WITH x1 AS (SELECT * FROM t3),
           70  +       x2 AS (
           71  +         WITH t3 AS (SELECT * FROM t4)
           72  +         SELECT * FROM x1
           73  +       )
           74  +  SELECT * FROM x2;
           75  +} {3}
           76  +
           77  +do_execsql_test 1.7 {
           78  +  WITH x2 AS (
           79  +         WITH t3 AS (SELECT * FROM t4)
           80  +         SELECT * FROM t3
           81  +       )
           82  +  SELECT * FROM x2;
           83  +} {4}
           84  +
           85  +do_execsql_test 1.8 {
           86  +  WITH x2 AS (
           87  +         WITH t3 AS (SELECT * FROM t4)
           88  +         SELECT * FROM main.t3
           89  +       )
           90  +  SELECT * FROM x2;
           91  +} {3}
           92  +
           93  +do_execsql_test 1.9 {
           94  +  WITH x1 AS (SELECT * FROM t1)
           95  +  SELECT (SELECT sum(a) FROM x1), (SELECT max(a) FROM x1);
           96  +} {3 2}
           97  +
           98  +do_execsql_test 1.10 {
           99  +  WITH x1 AS (SELECT * FROM t1)
          100  +  SELECT (SELECT sum(a) FROM x1), (SELECT max(a) FROM x1), a FROM x1;
          101  +} {3 2 1 3 2 2}
          102  +
          103  +do_execsql_test 1.11 {
          104  +  WITH 
          105  +  i(x) AS ( 
          106  +    WITH 
          107  +    j(x) AS ( SELECT * FROM i ), 
          108  +    i(x) AS ( SELECT * FROM t1 )
          109  +    SELECT * FROM j
          110  +  )
          111  +  SELECT * FROM i;
          112  +} {1 2}
          113  +
          114  +do_execsql_test 1.12 {
          115  +  WITH r(i) AS (
          116  +    VALUES('.')
          117  +    UNION ALL
          118  +    SELECT i || '.' FROM r, (
          119  +      SELECT x FROM x INTERSECT SELECT y FROM y
          120  +    ) WHERE length(i) < 10
          121  +  ),
          122  +  x(x) AS ( VALUES(1) UNION ALL VALUES(2) UNION ALL VALUES(3) ),
          123  +  y(y) AS ( VALUES(2) UNION ALL VALUES(4) UNION ALL VALUES(6) )
          124  +
          125  +  SELECT * FROM r;
          126  +} {. .. ... .... ..... ...... ....... ........ ......... ..........}
          127  +
          128  +do_execsql_test 1.13 {
          129  +  WITH r(i) AS (
          130  +    VALUES('.')
          131  +    UNION ALL
          132  +    SELECT i || '.' FROM r, ( SELECT x FROM x WHERE x=2 ) WHERE length(i) < 10
          133  +  ),
          134  +  x(x) AS ( VALUES(1) UNION ALL VALUES(2) UNION ALL VALUES(3) )
          135  +
          136  +  SELECT * FROM r ORDER BY length(i) DESC;
          137  +} {.......... ......... ........ ....... ...... ..... .... ... .. .}
          138  +
          139  +do_execsql_test 1.14 {
          140  +  WITH 
          141  +  t4(x) AS ( 
          142  +    VALUES(4)
          143  +    UNION ALL 
          144  +    SELECT x+1 FROM t4 WHERE x<10
          145  +  )
          146  +  SELECT * FROM t4;
          147  +} {4 5 6 7 8 9 10}
          148  +
          149  +do_execsql_test 1.15 {
          150  +  WITH 
          151  +  t4(x) AS ( 
          152  +    VALUES(4)
          153  +    UNION ALL 
          154  +    SELECT x+1 FROM main.t4 WHERE x<10
          155  +  )
          156  +  SELECT * FROM t4;
          157  +} {4 5}
          158  +
          159  +do_catchsql_test 1.16 {
          160  +  WITH 
          161  +  t4(x) AS ( 
          162  +    VALUES(4)
          163  +    UNION ALL 
          164  +    SELECT x+1 FROM t4, main.t4, t4 WHERE x<10
          165  +  )
          166  +  SELECT * FROM t4;
          167  +} {1 {multiple references to recursive table: t4}}
          168  +
          169  +
          170  +#---------------------------------------------------------------------------
          171  +# Check that variables can be used in CTEs.
          172  +#
          173  +set ::min [expr 3]
          174  +set ::max [expr 9]
          175  +do_execsql_test 2.1 {
          176  +  WITH i(x) AS (
          177  +    VALUES($min) UNION ALL SELECT x+1 FROM i WHERE x < $max
          178  +  )
          179  +  SELECT * FROM i;
          180  +} {3 4 5 6 7 8 9}
          181  +
          182  +do_execsql_test 2.2 {
          183  +  WITH i(x) AS (
          184  +    VALUES($min) UNION ALL SELECT x+1 FROM i WHERE x < $max
          185  +  )
          186  +  SELECT x FROM i JOIN i AS j USING (x);
          187  +} {3 4 5 6 7 8 9}
          188  +
          189  +#---------------------------------------------------------------------------
          190  +# Check that circular references are rejected.
          191  +#
          192  +do_catchsql_test 3.1 {
          193  +  WITH i(x, y) AS ( VALUES(1, (SELECT x FROM i)) )
          194  +  SELECT * FROM i;
          195  +} {1 {circular reference: i}}
          196  +
          197  +do_catchsql_test 3.2 {
          198  +  WITH 
          199  +  i(x) AS ( SELECT * FROM j ),
          200  +  j(x) AS ( SELECT * FROM k ),
          201  +  k(x) AS ( SELECT * FROM i )
          202  +  SELECT * FROM i;
          203  +} {1 {circular reference: i}}
          204  +
          205  +do_catchsql_test 3.3 {
          206  +  WITH 
          207  +  i(x) AS ( SELECT * FROM (SELECT * FROM j) ),
          208  +  j(x) AS ( SELECT * FROM (SELECT * FROM i) )
          209  +  SELECT * FROM i;
          210  +} {1 {circular reference: i}}
          211  +
          212  +do_catchsql_test 3.4 {
          213  +  WITH 
          214  +  i(x) AS ( SELECT * FROM (SELECT * FROM j) ),
          215  +  j(x) AS ( SELECT * FROM (SELECT * FROM i) )
          216  +  SELECT * FROM j;
          217  +} {1 {circular reference: j}}
          218  +
          219  +do_catchsql_test 3.5 {
          220  +  WITH 
          221  +  i(x) AS ( 
          222  +    WITH j(x) AS ( SELECT * FROM i )
          223  +    SELECT * FROM j
          224  +  )
          225  +  SELECT * FROM i;
          226  +} {1 {circular reference: i}}
          227  +
          228  +#---------------------------------------------------------------------------
          229  +# Try empty and very long column lists.
          230  +#
          231  +do_catchsql_test 4.1 {
          232  +  WITH x() AS ( SELECT 1,2,3 )
          233  +  SELECT * FROM x;
          234  +} {1 {near ")": syntax error}}
          235  +
          236  +proc genstmt {n} {
          237  +  for {set i 1} {$i<=$n} {incr i} {
          238  +    lappend cols "c$i"
          239  +    lappend vals $i
          240  +  }
          241  +  return "
          242  +    WITH x([join $cols ,]) AS (SELECT [join $vals ,])
          243  +    SELECT (c$n == $n) FROM x
          244  +  "
          245  +}
          246  +
          247  +do_execsql_test  4.2 [genstmt 10] 1
          248  +do_execsql_test  4.3 [genstmt 100] 1
          249  +do_execsql_test  4.4 [genstmt 255] 1
          250  +set nLimit [sqlite3_limit db SQLITE_LIMIT_COLUMN -1]
          251  +do_execsql_test  4.5 [genstmt [expr $nLimit-1]] 1
          252  +do_execsql_test  4.6 [genstmt $nLimit] 1
          253  +do_catchsql_test 4.7 [genstmt [expr $nLimit+1]] {1 {too many columns in index}}
          254  +
          255  +#---------------------------------------------------------------------------
          256  +# Check that adding a WITH clause to an INSERT disables the xfer 
          257  +# optimization.
          258  +#
          259  +proc do_xfer_test {tn bXfer sql {res {}}} {
          260  +  set ::sqlite3_xferopt_count 0
          261  +  uplevel [list do_test $tn [subst -nocommands {
          262  +    set dres [db eval {$sql}]
          263  +    list [set ::sqlite3_xferopt_count] [set dres]
          264  +  }] [list $bXfer $res]]
          265  +}
          266  +
          267  +do_execsql_test 5.1 {
          268  +  DROP TABLE IF EXISTS t1;
          269  +  DROP TABLE IF EXISTS t2;
          270  +  CREATE TABLE t1(a, b);
          271  +  CREATE TABLE t2(a, b);
          272  +}
          273  +
          274  +do_xfer_test 5.2 1 { INSERT INTO t1 SELECT * FROM t2 }
          275  +do_xfer_test 5.3 0 { INSERT INTO t1 SELECT a, b FROM t2 }
          276  +do_xfer_test 5.4 0 { INSERT INTO t1 SELECT b, a FROM t2 }
          277  +do_xfer_test 5.5 0 { 
          278  +  WITH x AS (SELECT a, b FROM t2) INSERT INTO t1 SELECT * FROM x 
          279  +}
          280  +do_xfer_test 5.6 0 { 
          281  +  WITH x AS (SELECT a, b FROM t2) INSERT INTO t1 SELECT * FROM t2 
          282  +}
          283  +do_xfer_test 5.7 0 { 
          284  + INSERT INTO t1 WITH x AS ( SELECT * FROM t2 ) SELECT * FROM x
          285  +}
          286  +do_xfer_test 5.8 0 { 
          287  + INSERT INTO t1 WITH x(a,b) AS ( SELECT * FROM t2 ) SELECT * FROM x
          288  +}
          289  +
          290  +#---------------------------------------------------------------------------
          291  +# Check that syntax (and other) errors in statements with WITH clauses
          292  +# attached to them do not cause problems (e.g. memory leaks).
          293  +#
          294  +do_execsql_test 6.1 {
          295  +  DROP TABLE IF EXISTS t1;
          296  +  DROP TABLE IF EXISTS t2;
          297  +  CREATE TABLE t1(a, b);
          298  +  CREATE TABLE t2(a, b);
          299  +}
          300  +
          301  +do_catchsql_test 6.2 {
          302  +  WITH x AS (SELECT * FROM t1)
          303  +  INSERT INTO t2 VALUES(1, 2,);
          304  +} {1 {near ")": syntax error}}
          305  +
          306  +do_catchsql_test 6.3 {
          307  +  WITH x AS (SELECT * FROM t1)
          308  +  INSERT INTO t2 SELECT a, b, FROM t1;
          309  +} {1 {near "FROM": syntax error}}
          310  +
          311  +do_catchsql_test 6.3 {
          312  +  WITH x AS (SELECT * FROM t1)
          313  +  INSERT INTO t2 SELECT a, b FROM abc;
          314  +} {1 {no such table: abc}}
          315  +
          316  +do_catchsql_test 6.4 {
          317  +  WITH x AS (SELECT * FROM t1)
          318  +  INSERT INTO t2 SELECT a, b, FROM t1 a a a;
          319  +} {1 {near "FROM": syntax error}}
          320  +
          321  +do_catchsql_test 6.5 {
          322  +  WITH x AS (SELECT * FROM t1)
          323  +  DELETE FROM t2 WHERE;
          324  +} {1 {near ";": syntax error}}
          325  +
          326  +do_catchsql_test 6.6 { 
          327  +  WITH x AS (SELECT * FROM t1) DELETE FROM t2 WHERE
          328  +} {/1 {near .* syntax error}/}
          329  +
          330  +do_catchsql_test 6.7 { 
          331  +  WITH x AS (SELECT * FROM t1) DELETE FROM t2 WHRE 1;
          332  +} {/1 {near .* syntax error}/}
          333  +
          334  +do_catchsql_test 6.8 { 
          335  +  WITH x AS (SELECT * FROM t1) UPDATE t2 SET a = 10, b = ;
          336  +} {/1 {near .* syntax error}/}
          337  +
          338  +do_catchsql_test 6.9 { 
          339  +  WITH x AS (SELECT * FROM t1) UPDATE t2 SET a = 10, b = 1 WHERE a===b;
          340  +} {/1 {near .* syntax error}/}
          341  +
          342  +do_catchsql_test 6.10 {
          343  +  WITH x(a,b) AS (
          344  +    SELECT 1, 1
          345  +    UNION ALL
          346  +    SELECT a*b,a+b FROM x WHERE c=2
          347  +  )
          348  +  SELECT * FROM x
          349  +} {1 {no such column: c}}
          350  +
          351  +#-------------------------------------------------------------------------
          352  +# Recursive queries in IN(...) expressions.
          353  +#
          354  +do_execsql_test 7.1 {
          355  +  CREATE TABLE t5(x INTEGER);
          356  +  CREATE TABLE t6(y INTEGER);
          357  +
          358  +  WITH s(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM s WHERE x<49 )
          359  +  INSERT INTO t5 
          360  +  SELECT * FROM s;
          361  +
          362  +  INSERT INTO t6 
          363  +  WITH s(x) AS ( VALUES(2) UNION ALL SELECT x+2 FROM s WHERE x<49 )
          364  +  SELECT * FROM s;
          365  +}
          366  +
          367  +do_execsql_test 7.2 {
          368  +  SELECT * FROM t6 WHERE y IN (SELECT x FROM t5)
          369  +} {14 28 42}
          370  +
          371  +do_execsql_test 7.3 {
          372  +  WITH ss AS (SELECT x FROM t5)
          373  +  SELECT * FROM t6 WHERE y IN (SELECT x FROM ss)
          374  +} {14 28 42}
          375  +
          376  +do_execsql_test 7.4 {
          377  +  WITH ss(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM ss WHERE x<49 )
          378  +  SELECT * FROM t6 WHERE y IN (SELECT x FROM ss)
          379  +} {14 28 42}
          380  +
          381  +do_execsql_test 7.5 {
          382  +  SELECT * FROM t6 WHERE y IN (
          383  +    WITH ss(x) AS ( VALUES(7) UNION ALL SELECT x+7 FROM ss WHERE x<49 )
          384  +    SELECT x FROM ss
          385  +  )
          386  +} {14 28 42}
          387  +
          388  +
          389  +
          390  +finish_test
          391  +

Added test/withM.test.

            1  +# 2014 January 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.  The
           12  +# focus of this file is testing the WITH clause.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +source $testdir/malloc_common.tcl
           18  +set ::testprefix withM
           19  +
           20  +ifcapable {!cte} {
           21  +  finish_test
           22  +  return
           23  +}
           24  +
           25  +do_execsql_test 1.0 {
           26  +  CREATE TABLE t1(x INTEGER, y INTEGER);
           27  +  INSERT INTO t1 VALUES(123, 456);
           28  +}
           29  +
           30  +do_faultsim_test withM-1.1 -prep {
           31  +  sqlite3 db test.db
           32  +} -body {
           33  +  execsql { 
           34  +    WITH tmp AS ( SELECT * FROM t1 )
           35  +    SELECT * FROM tmp;
           36  +  }
           37  +} -test {
           38  +  faultsim_test_result {0 {123 456}}
           39  +  db close
           40  +}
           41  +
           42  +do_faultsim_test withM-1.2 -prep {
           43  +  sqlite3 db test.db
           44  +} -body {
           45  +  execsql { 
           46  +    WITH w1 AS ( SELECT * FROM t1 ),
           47  +         w2 AS ( 
           48  +           WITH w3 AS ( SELECT * FROM w1 )
           49  +           SELECT * FROM w3
           50  +         )
           51  +    SELECT * FROM w2;
           52  +  }
           53  +} -test {
           54  +  faultsim_test_result {0 {123 456}}
           55  +  db close
           56  +}
           57  +
           58  +do_faultsim_test withM-1.3 -prep {
           59  +  sqlite3 db test.db
           60  +} -body {
           61  +  execsql { 
           62  +    WITH w1(a,b) AS ( 
           63  +      SELECT 1, 1
           64  +      UNION ALL
           65  +      SELECT a+1, b + 2*a + 1 FROM w1
           66  +    )
           67  +    SELECT * FROM w1 LIMIT 5;
           68  +  }
           69  +} -test {
           70  +  faultsim_test_result {0 {1 1 2 4 3 9 4 16 5 25}}
           71  +  db close
           72  +}
           73  +
           74  +finish_test
           75  +
           76  +
           77  +

Changes to tool/lemon.c.

    45     45   
    46     46   /*
    47     47   ** Compilers are getting increasingly pedantic about type conversions
    48     48   ** as C evolves ever closer to Ada....  To work around the latest problems
    49     49   ** we have to define the following variant of strlen().
    50     50   */
    51     51   #define lemonStrlen(X)   ((int)strlen(X))
           52  +
           53  +/*
           54  +** Compilers are starting to complain about the use of sprintf() and strcpy(),
           55  +** saying they are unsafe.  So we define our own versions of those routines too.
           56  +**
           57  +** There are three routines here:  lemon_sprintf(), lemon_vsprintf(), and
           58  +** lemon_addtext().  The first two are replacements for sprintf() and vsprintf().
           59  +** The third is a helper routine for vsnprintf() that adds texts to the end of a
           60  +** buffer, making sure the buffer is always zero-terminated.
           61  +**
           62  +** The string formatter is a minimal subset of stdlib sprintf() supporting only
           63  +** a few simply conversions:
           64  +**
           65  +**   %d
           66  +**   %s
           67  +**   %.*s
           68  +**
           69  +*/
           70  +static void lemon_addtext(
           71  +  char *zBuf,           /* The buffer to which text is added */
           72  +  int *pnUsed,          /* Slots of the buffer used so far */
           73  +  const char *zIn,      /* Text to add */
           74  +  int nIn,              /* Bytes of text to add.  -1 to use strlen() */
           75  +  int iWidth            /* Field width.  Negative to left justify */
           76  +){
           77  +  if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){}
           78  +  while( iWidth>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth--; }
           79  +  if( nIn==0 ) return;
           80  +  memcpy(&zBuf[*pnUsed], zIn, nIn);
           81  +  *pnUsed += nIn;
           82  +  while( (-iWidth)>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth++; }
           83  +  zBuf[*pnUsed] = 0;
           84  +}
           85  +static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){
           86  +  int i, j, k, c;
           87  +  int nUsed = 0;
           88  +  const char *z;
           89  +  char zTemp[50];
           90  +  str[0] = 0;
           91  +  for(i=j=0; (c = zFormat[i])!=0; i++){
           92  +    if( c=='%' ){
           93  +      int iWidth = 0;
           94  +      lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0);
           95  +      c = zFormat[++i];
           96  +      if( isdigit(c) || (c=='-' && isdigit(zFormat[i+1])) ){
           97  +        if( c=='-' ) i++;
           98  +        while( isdigit(zFormat[i]) ) iWidth = iWidth*10 + zFormat[i++] - '0';
           99  +        if( c=='-' ) iWidth = -iWidth;
          100  +        c = zFormat[i];
          101  +      }
          102  +      if( c=='d' ){
          103  +        int v = va_arg(ap, int);
          104  +        if( v<0 ){
          105  +          lemon_addtext(str, &nUsed, "-", 1, iWidth);
          106  +          v = -v;
          107  +        }else if( v==0 ){
          108  +          lemon_addtext(str, &nUsed, "0", 1, iWidth);
          109  +        }
          110  +        k = 0;
          111  +        while( v>0 ){
          112  +          k++;
          113  +          zTemp[sizeof(zTemp)-k] = (v%10) + '0';
          114  +          v /= 10;
          115  +        }
          116  +        lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k, iWidth);
          117  +      }else if( c=='s' ){
          118  +        z = va_arg(ap, const char*);
          119  +        lemon_addtext(str, &nUsed, z, -1, iWidth);
          120  +      }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){
          121  +        i += 2;
          122  +        k = va_arg(ap, int);
          123  +        z = va_arg(ap, const char*);
          124  +        lemon_addtext(str, &nUsed, z, k, iWidth);
          125  +      }else if( c=='%' ){
          126  +        lemon_addtext(str, &nUsed, "%", 1, 0);
          127  +      }else{
          128  +        fprintf(stderr, "illegal format\n");
          129  +        exit(1);
          130  +      }
          131  +      j = i+1;
          132  +    }
          133  +  }
          134  +  lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0);
          135  +  return nUsed;
          136  +}
          137  +static int lemon_sprintf(char *str, const char *format, ...){
          138  +  va_list ap;
          139  +  int rc;
          140  +  va_start(ap, format);
          141  +  rc = lemon_vsprintf(str, format, ap);
          142  +  va_end(ap);
          143  +  return rc;
          144  +}
          145  +static void lemon_strcpy(char *dest, const char *src){
          146  +  while( (*(dest++) = *(src++))!=0 ){}
          147  +}
          148  +static void lemon_strcat(char *dest, const char *src){
          149  +  while( *dest ) dest++;
          150  +  lemon_strcpy(dest, src);
          151  +}
          152  +
    52    153   
    53    154   /* a few forward declarations... */
    54    155   struct rule;
    55    156   struct lemon;
    56    157   struct action;
    57    158   
    58    159   static struct action *Action_new(void);
................................................................................
  1363   1464     }
  1364   1465     paz = &azDefine[nDefine-1];
  1365   1466     *paz = (char *) malloc( lemonStrlen(z)+1 );
  1366   1467     if( *paz==0 ){
  1367   1468       fprintf(stderr,"out of memory\n");
  1368   1469       exit(1);
  1369   1470     }
  1370         -  strcpy(*paz, z);
         1471  +  lemon_strcpy(*paz, z);
  1371   1472     for(z=*paz; *z && *z!='='; z++){}
  1372   1473     *z = 0;
  1373   1474   }
  1374   1475   
  1375   1476   static char *user_templatename = NULL;
  1376   1477   static void handle_T_option(char *z){
  1377   1478     user_templatename = (char *) malloc( lemonStrlen(z)+1 );
  1378   1479     if( user_templatename==0 ){
  1379   1480       memory_error();
  1380   1481     }
  1381         -  strcpy(user_templatename, z);
         1482  +  lemon_strcpy(user_templatename, z);
  1382   1483   }
  1383   1484   
  1384   1485   /* The main program.  Parse the command line and do it... */
  1385   1486   int main(int argc, char **argv)
  1386   1487   {
  1387   1488     static int version = 0;
  1388   1489     static int rpflag = 0;
................................................................................
  1443   1544     if( lem.errorcnt ) exit(lem.errorcnt);
  1444   1545     if( lem.nrule==0 ){
  1445   1546       fprintf(stderr,"Empty grammar.\n");
  1446   1547       exit(1);
  1447   1548     }
  1448   1549   
  1449   1550     /* Count and index the symbols of the grammar */
  1450         -  lem.nsymbol = Symbol_count();
  1451   1551     Symbol_new("{default}");
         1552  +  lem.nsymbol = Symbol_count();
  1452   1553     lem.symbols = Symbol_arrayof();
  1453         -  for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
  1454         -  qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*), Symbolcmpp);
  1455         -  for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
         1554  +  for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
         1555  +  qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp);
         1556  +  for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
         1557  +  while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; }
         1558  +  assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 );
         1559  +  lem.nsymbol = i - 1;
  1456   1560     for(i=1; isupper(lem.symbols[i]->name[0]); i++);
  1457   1561     lem.nterminal = i;
  1458   1562   
  1459   1563     /* Generate a reprint of the grammar, if requested on the command line */
  1460   1564     if( rpflag ){
  1461   1565       Reprint(&lem);
  1462   1566     }else{
................................................................................
  1936   2040     PRECEDENCE_MARK_1,
  1937   2041     PRECEDENCE_MARK_2,
  1938   2042     RESYNC_AFTER_RULE_ERROR,
  1939   2043     RESYNC_AFTER_DECL_ERROR,
  1940   2044     WAITING_FOR_DESTRUCTOR_SYMBOL,
  1941   2045     WAITING_FOR_DATATYPE_SYMBOL,
  1942   2046     WAITING_FOR_FALLBACK_ID,
  1943         -  WAITING_FOR_WILDCARD_ID
         2047  +  WAITING_FOR_WILDCARD_ID,
         2048  +  WAITING_FOR_CLASS_ID,
         2049  +  WAITING_FOR_CLASS_TOKEN
  1944   2050   };
  1945   2051   struct pstate {
  1946   2052     char *filename;       /* Name of the input file */
  1947   2053     int tokenlineno;      /* Linenumber at which current token starts */
  1948   2054     int errorcnt;         /* Number of errors so far */
  1949   2055     char *tokenstart;     /* Text of current token */
  1950   2056     struct lemon *gp;     /* Global state vector */
  1951   2057     enum e_state state;        /* The state of the parser */
  1952   2058     struct symbol *fallback;   /* The fallback token */
         2059  +  struct symbol *tkclass;    /* Token class symbol */
  1953   2060     struct symbol *lhs;        /* Left-hand side of current rule */
  1954   2061     const char *lhsalias;      /* Alias for the LHS */
  1955   2062     int nrhs;                  /* Number of right-hand side symbols seen */
  1956   2063     struct symbol *rhs[MAXRHS];  /* RHS symbols */
  1957   2064     const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
  1958   2065     struct rule *prevrule;     /* Previous rule parsed */
  1959   2066     const char *declkeyword;   /* Keyword of a declaration */
................................................................................
  2250   2357           }else if( strcmp(x,"type")==0 ){
  2251   2358             psp->state = WAITING_FOR_DATATYPE_SYMBOL;
  2252   2359           }else if( strcmp(x,"fallback")==0 ){
  2253   2360             psp->fallback = 0;
  2254   2361             psp->state = WAITING_FOR_FALLBACK_ID;
  2255   2362           }else if( strcmp(x,"wildcard")==0 ){
  2256   2363             psp->state = WAITING_FOR_WILDCARD_ID;
         2364  +        }else if( strcmp(x,"token_class")==0 ){
         2365  +          psp->state = WAITING_FOR_CLASS_ID;
  2257   2366           }else{
  2258   2367             ErrorMsg(psp->filename,psp->tokenlineno,
  2259   2368               "Unknown declaration keyword: \"%%%s\".",x);
  2260   2369             psp->errorcnt++;
  2261   2370             psp->state = RESYNC_AFTER_DECL_ERROR;
  2262   2371           }
  2263   2372         }else{
................................................................................
  2343   2452           n = nOld + nNew + 20;
  2344   2453           addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
  2345   2454                           (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
  2346   2455           if( addLineMacro ){
  2347   2456             for(z=psp->filename, nBack=0; *z; z++){
  2348   2457               if( *z=='\\' ) nBack++;
  2349   2458             }
  2350         -          sprintf(zLine, "#line %d ", psp->tokenlineno);
         2459  +          lemon_sprintf(zLine, "#line %d ", psp->tokenlineno);
  2351   2460             nLine = lemonStrlen(zLine);
  2352   2461             n += nLine + lemonStrlen(psp->filename) + nBack;
  2353   2462           }
  2354   2463           *psp->declargslot = (char *) realloc(*psp->declargslot, n);
  2355   2464           zBuf = *psp->declargslot + nOld;
  2356   2465           if( addLineMacro ){
  2357   2466             if( nOld && zBuf[-1]!='\n' ){
................................................................................
  2417   2526             psp->gp->wildcard = sp;
  2418   2527           }else{
  2419   2528             ErrorMsg(psp->filename, psp->tokenlineno,
  2420   2529               "Extra wildcard to token: %s", x);
  2421   2530             psp->errorcnt++;
  2422   2531           }
  2423   2532         }
         2533  +      break;
         2534  +    case WAITING_FOR_CLASS_ID:
         2535  +      if( !islower(x[0]) ){
         2536  +        ErrorMsg(psp->filename, psp->tokenlineno,
         2537  +          "%%token_class must be followed by an identifier: ", x);
         2538  +        psp->errorcnt++;
         2539  +        psp->state = RESYNC_AFTER_DECL_ERROR;
         2540  +     }else if( Symbol_find(x) ){
         2541  +        ErrorMsg(psp->filename, psp->tokenlineno,
         2542  +          "Symbol \"%s\" already used", x);
         2543  +        psp->errorcnt++;
         2544  +        psp->state = RESYNC_AFTER_DECL_ERROR;
         2545  +      }else{
         2546  +        psp->tkclass = Symbol_new(x);
         2547  +        psp->tkclass->type = MULTITERMINAL;
         2548  +        psp->state = WAITING_FOR_CLASS_TOKEN;
         2549  +      }
         2550  +      break;
         2551  +    case WAITING_FOR_CLASS_TOKEN:
         2552  +      if( x[0]=='.' ){
         2553  +        psp->state = WAITING_FOR_DECL_OR_RULE;
         2554  +      }else if( isupper(x[0]) || ((x[0]=='|' || x[0]=='/') && isupper(x[1])) ){
         2555  +        struct symbol *msp = psp->tkclass;
         2556  +        msp->nsubsym++;
         2557  +        msp->subsym = (struct symbol **) realloc(msp->subsym,
         2558  +          sizeof(struct symbol*)*msp->nsubsym);
         2559  +        if( !isupper(x[0]) ) x++;
         2560  +        msp->subsym[msp->nsubsym-1] = Symbol_new(x);
         2561  +      }else{
         2562  +        ErrorMsg(psp->filename, psp->tokenlineno,
         2563  +          "%%token_class argument \"%s\" should be a token", x);
         2564  +        psp->errorcnt++;
         2565  +        psp->state = RESYNC_AFTER_DECL_ERROR;
         2566  +      }
  2424   2567         break;
  2425   2568       case RESYNC_AFTER_RULE_ERROR:
  2426   2569   /*      if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
  2427   2570   **      break; */
  2428   2571       case RESYNC_AFTER_DECL_ERROR:
  2429   2572         if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
  2430   2573         if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
................................................................................
  2512   2655       gp->errorcnt++;
  2513   2656       return;
  2514   2657     }
  2515   2658     fseek(fp,0,2);
  2516   2659     filesize = ftell(fp);
  2517   2660     rewind(fp);
  2518   2661     filebuf = (char *)malloc( filesize+1 );
  2519         -  if( filebuf==0 ){
  2520         -    ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.",
  2521         -      filesize+1);
         2662  +  if( filesize>100000000 || filebuf==0 ){
         2663  +    ErrorMsg(ps.filename,0,"Input file too large.");
  2522   2664       gp->errorcnt++;
  2523   2665       fclose(fp);
  2524   2666       return;
  2525   2667     }
  2526   2668     if( fread(filebuf,1,filesize,fp)!=filesize ){
  2527   2669       ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
  2528   2670         filesize);
................................................................................
  2712   2854     char *cp;
  2713   2855   
  2714   2856     name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
  2715   2857     if( name==0 ){
  2716   2858       fprintf(stderr,"Can't allocate space for a filename.\n");
  2717   2859       exit(1);
  2718   2860     }
  2719         -  strcpy(name,lemp->filename);
         2861  +  lemon_strcpy(name,lemp->filename);
  2720   2862     cp = strrchr(name,'.');
  2721   2863     if( cp ) *cp = 0;
  2722         -  strcat(name,suffix);
         2864  +  lemon_strcat(name,suffix);
  2723   2865     return name;
  2724   2866   }
  2725   2867   
  2726   2868   /* Open a file with a name based on the name of the input file,
  2727   2869   ** but with a different (specified) suffix, and return a pointer
  2728   2870   ** to the stream */
  2729   2871   PRIVATE FILE *file_open(
................................................................................
  2772   2914     }
  2773   2915     for(rp=lemp->rule; rp; rp=rp->next){
  2774   2916       printf("%s",rp->lhs->name);
  2775   2917       /*    if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
  2776   2918       printf(" ::=");
  2777   2919       for(i=0; i<rp->nrhs; i++){
  2778   2920         sp = rp->rhs[i];
  2779         -      printf(" %s", sp->name);
  2780   2921         if( sp->type==MULTITERMINAL ){
         2922  +        printf(" %s", sp->subsym[0]->name);
  2781   2923           for(j=1; j<sp->nsubsym; j++){
  2782   2924             printf("|%s", sp->subsym[j]->name);
  2783   2925           }
         2926  +      }else{
         2927  +        printf(" %s", sp->name);
  2784   2928         }
  2785   2929         /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
  2786   2930       }
  2787   2931       printf(".");
  2788   2932       if( rp->precsym ) printf(" [%s]",rp->precsym->name);
  2789   2933       /* if( rp->code ) printf("\n    %s",rp->code); */
  2790   2934       printf("\n");
................................................................................
  2798   2942     int i, j;
  2799   2943     rp = cfp->rp;
  2800   2944     fprintf(fp,"%s ::=",rp->lhs->name);
  2801   2945     for(i=0; i<=rp->nrhs; i++){
  2802   2946       if( i==cfp->dot ) fprintf(fp," *");
  2803   2947       if( i==rp->nrhs ) break;
  2804   2948       sp = rp->rhs[i];
  2805         -    fprintf(fp," %s", sp->name);
  2806   2949       if( sp->type==MULTITERMINAL ){
         2950  +      fprintf(fp," %s", sp->subsym[0]->name);
  2807   2951         for(j=1; j<sp->nsubsym; j++){
  2808   2952           fprintf(fp,"|%s",sp->subsym[j]->name);
  2809   2953         }
         2954  +    }else{
         2955  +      fprintf(fp," %s", sp->name);
  2810   2956       }
  2811   2957     }
  2812   2958   }
  2813   2959   
  2814   2960   /* #define TEST */
  2815   2961   #if 0
  2816   2962   /* Print a set */
................................................................................
  2912   3058       stp = lemp->sorted[i];
  2913   3059       fprintf(fp,"State %d:\n",stp->statenum);
  2914   3060       if( lemp->basisflag ) cfp=stp->bp;
  2915   3061       else                  cfp=stp->cfp;
  2916   3062       while( cfp ){
  2917   3063         char buf[20];
  2918   3064         if( cfp->dot==cfp->rp->nrhs ){
  2919         -        sprintf(buf,"(%d)",cfp->rp->index);
         3065  +        lemon_sprintf(buf,"(%d)",cfp->rp->index);
  2920   3066           fprintf(fp,"    %5s ",buf);
  2921   3067         }else{
  2922   3068           fprintf(fp,"          ");
  2923   3069         }
  2924   3070         ConfigPrint(fp,cfp);
  2925   3071         fprintf(fp,"\n");
  2926   3072   #if 0
................................................................................
  2977   3123   #else
  2978   3124     cp = strrchr(argv0,'/');
  2979   3125   #endif
  2980   3126     if( cp ){
  2981   3127       c = *cp;
  2982   3128       *cp = 0;
  2983   3129       path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
  2984         -    if( path ) sprintf(path,"%s/%s",argv0,name);
         3130  +    if( path ) lemon_sprintf(path,"%s/%s",argv0,name);
  2985   3131       *cp = c;
  2986   3132     }else{
  2987   3133       pathlist = getenv("PATH");
  2988   3134       if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
  2989   3135       pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
  2990   3136       path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
  2991   3137       if( (pathbuf != 0) && (path!=0) ){
  2992   3138         pathbufptr = pathbuf;
  2993         -      strcpy(pathbuf, pathlist);
         3139  +      lemon_strcpy(pathbuf, pathlist);
  2994   3140         while( *pathbuf ){
  2995   3141           cp = strchr(pathbuf,':');
  2996   3142           if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
  2997   3143           c = *cp;
  2998   3144           *cp = 0;
  2999         -        sprintf(path,"%s/%s",pathbuf,name);
         3145  +        lemon_sprintf(path,"%s/%s",pathbuf,name);
  3000   3146           *cp = c;
  3001   3147           if( c==0 ) pathbuf[0] = 0;
  3002   3148           else pathbuf = &cp[1];
  3003   3149           if( access(path,modemask)==0 ) break;
  3004   3150         }
  3005   3151         free(pathbufptr);
  3006   3152       }
................................................................................
  3083   3229         return 0;
  3084   3230       }
  3085   3231       return in;
  3086   3232     }
  3087   3233   
  3088   3234     cp = strrchr(lemp->filename,'.');
  3089   3235     if( cp ){
  3090         -    sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
         3236  +    lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
  3091   3237     }else{
  3092         -    sprintf(buf,"%s.lt",lemp->filename);
         3238  +    lemon_sprintf(buf,"%s.lt",lemp->filename);
  3093   3239     }
  3094   3240     if( access(buf,004)==0 ){
  3095   3241       tpltname = buf;
  3096   3242     }else if( access(templatename,004)==0 ){
  3097   3243       tpltname = templatename;
  3098   3244     }else{
  3099   3245       tpltname = pathsearch(lemp->argv0,templatename,0);
................................................................................
  3236   3382       alloced = n + sizeof(zInt)*2 + used + 200;
  3237   3383       z = (char *) realloc(z,  alloced);
  3238   3384     }
  3239   3385     if( z==0 ) return empty;
  3240   3386     while( n-- > 0 ){
  3241   3387       c = *(zText++);
  3242   3388       if( c=='%' && n>0 && zText[0]=='d' ){
  3243         -      sprintf(zInt, "%d", p1);
         3389  +      lemon_sprintf(zInt, "%d", p1);
  3244   3390         p1 = p2;
  3245         -      strcpy(&z[used], zInt);
         3391  +      lemon_strcpy(&z[used], zInt);
  3246   3392         used += lemonStrlen(&z[used]);
  3247   3393         zText++;
  3248   3394         n--;
  3249   3395       }else{
  3250   3396         z[used++] = c;
  3251   3397       }
  3252   3398     }
................................................................................
  3463   3609       if( types[hash]==0 ){
  3464   3610         sp->dtnum = hash + 1;
  3465   3611         types[hash] = (char*)malloc( lemonStrlen(stddt)+1 );
  3466   3612         if( types[hash]==0 ){
  3467   3613           fprintf(stderr,"Out of memory.\n");
  3468   3614           exit(1);
  3469   3615         }
  3470         -      strcpy(types[hash],stddt);
         3616  +      lemon_strcpy(types[hash],stddt);
  3471   3617       }
  3472   3618     }
  3473   3619   
  3474   3620     /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
  3475   3621     name = lemp->name ? lemp->name : "Parse";
  3476   3622     lineno = *plineno;
  3477   3623     if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
................................................................................
  3549   3695   ** Write text on "out" that describes the rule "rp".
  3550   3696   */
  3551   3697   static void writeRuleText(FILE *out, struct rule *rp){
  3552   3698     int j;
  3553   3699     fprintf(out,"%s ::=", rp->lhs->name);
  3554   3700     for(j=0; j<rp->nrhs; j++){
  3555   3701       struct symbol *sp = rp->rhs[j];
  3556         -    fprintf(out," %s", sp->name);
  3557         -    if( sp->type==MULTITERMINAL ){
         3702  +    if( sp->type!=MULTITERMINAL ){
         3703  +      fprintf(out," %s", sp->name);
         3704  +    }else{
  3558   3705         int k;
         3706  +      fprintf(out," %s", sp->subsym[0]->name);
  3559   3707         for(k=1; k<sp->nsubsym; k++){
  3560   3708           fprintf(out,"|%s",sp->subsym[k]->name);
  3561   3709         }
  3562   3710       }
  3563   3711     }
  3564   3712   }
  3565   3713   
................................................................................
  3852   4000       }
  3853   4001     }
  3854   4002     tplt_xfer(lemp->name, in, out, &lineno);
  3855   4003   
  3856   4004     /* Generate a table containing the symbolic name of every symbol
  3857   4005     */
  3858   4006     for(i=0; i<lemp->nsymbol; i++){
  3859         -    sprintf(line,"\"%s\",",lemp->symbols[i]->name);
         4007  +    lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name);
  3860   4008       fprintf(out,"  %-15s",line);
  3861   4009       if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
  3862   4010     }
  3863   4011     if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
  3864   4012     tplt_xfer(lemp->name,in,out,&lineno);
  3865   4013   
  3866   4014     /* Generate a table containing a text string that describes every
................................................................................
  4019   4167   
  4020   4168     if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
  4021   4169     else                    prefix = "";
  4022   4170     in = file_open(lemp,".h","rb");
  4023   4171     if( in ){
  4024   4172       int nextChar;
  4025   4173       for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
  4026         -      sprintf(pattern,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
         4174  +      lemon_sprintf(pattern,"#define %s%-30s %3d\n",
         4175  +                    prefix,lemp->symbols[i]->name,i);
  4027   4176         if( strcmp(line,pattern) ) break;
  4028   4177       }
  4029   4178       nextChar = fgetc(in);
  4030   4179       fclose(in);
  4031   4180       if( i==lemp->nterminal && nextChar==EOF ){
  4032   4181         /* No change in the file.  Don't rewrite it. */
  4033   4182         return;
  4034   4183       }
  4035   4184     }
  4036   4185     out = file_open(lemp,".h","wb");
  4037   4186     if( out ){
  4038   4187       for(i=1; i<lemp->nterminal; i++){
  4039         -      fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
         4188  +      fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i);
  4040   4189       }
  4041   4190       fclose(out);  
  4042   4191     }
  4043   4192     return;
  4044   4193   }
  4045   4194   
  4046   4195   /* Reduce the size of the action tables, if possible, by making use
................................................................................
  4249   4398   {
  4250   4399     const char *z;
  4251   4400     char *cpy;
  4252   4401   
  4253   4402     if( y==0 ) return 0;
  4254   4403     z = Strsafe_find(y);
  4255   4404     if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
  4256         -    strcpy(cpy,y);
         4405  +    lemon_strcpy(cpy,y);
  4257   4406       z = cpy;
  4258   4407       Strsafe_insert(z);
  4259   4408     }
  4260   4409     MemoryCheck(z);
  4261   4410     return z;
  4262   4411   }
  4263   4412   
................................................................................
  4288   4437   /* Allocate a new associative array */
  4289   4438   void Strsafe_init(){
  4290   4439     if( x1a ) return;
  4291   4440     x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
  4292   4441     if( x1a ){
  4293   4442       x1a->size = 1024;
  4294   4443       x1a->count = 0;
  4295         -    x1a->tbl = (x1node*)malloc( 
  4296         -      (sizeof(x1node) + sizeof(x1node*))*1024 );
         4444  +    x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*));
  4297   4445       if( x1a->tbl==0 ){
  4298   4446         free(x1a);
  4299   4447         x1a = 0;
  4300   4448       }else{
  4301   4449         int i;
  4302   4450         x1a->ht = (x1node**)&(x1a->tbl[1024]);
  4303   4451         for(i=0; i<1024; i++) x1a->ht[i] = 0;
................................................................................
  4326   4474     }
  4327   4475     if( x1a->count>=x1a->size ){
  4328   4476       /* Need to make the hash table bigger */
  4329   4477       int i,size;
  4330   4478       struct s_x1 array;
  4331   4479       array.size = size = x1a->size*2;
  4332   4480       array.count = x1a->count;
  4333         -    array.tbl = (x1node*)malloc(
  4334         -      (sizeof(x1node) + sizeof(x1node*))*size );
         4481  +    array.tbl = (x1node*)calloc(size, sizeof(x1node) + sizeof(x1node*));
  4335   4482       if( array.tbl==0 ) return 0;  /* Fail due to malloc failure */
  4336   4483       array.ht = (x1node**)&(array.tbl[size]);
  4337   4484       for(i=0; i<size; i++) array.ht[i] = 0;
  4338   4485       for(i=0; i<x1a->count; i++){
  4339   4486         x1node *oldnp, *newnp;
  4340   4487         oldnp = &(x1a->tbl[i]);
  4341   4488         h = strhash(oldnp->data) & (size-1);
................................................................................
  4402   4549       sp->useCnt = 0;
  4403   4550       Symbol_insert(sp,sp->name);
  4404   4551     }
  4405   4552     sp->useCnt++;
  4406   4553     return sp;
  4407   4554   }
  4408   4555   
  4409         -/* Compare two symbols for working purposes
         4556  +/* Compare two symbols for sorting purposes.  Return negative,
         4557  +** zero, or positive if a is less then, equal to, or greater
         4558  +** than b.
  4410   4559   **
  4411   4560   ** Symbols that begin with upper case letters (terminals or tokens)
  4412   4561   ** must sort before symbols that begin with lower case letters
  4413         -** (non-terminals).  Other than that, the order does not matter.
         4562  +** (non-terminals).  And MULTITERMINAL symbols (created using the
         4563  +** %token_class directive) must sort at the very end. Other than
         4564  +** that, the order does not matter.
  4414   4565   **
  4415   4566   ** We find experimentally that leaving the symbols in their original
  4416   4567   ** order (the order they appeared in the grammar file) gives the
  4417   4568   ** smallest parser tables in SQLite.
  4418   4569   */
  4419   4570   int Symbolcmpp(const void *_a, const void *_b)
  4420   4571   {
  4421         -  const struct symbol **a = (const struct symbol **) _a;
  4422         -  const struct symbol **b = (const struct symbol **) _b;
  4423         -  int i1 = (**a).index + 10000000*((**a).name[0]>'Z');
  4424         -  int i2 = (**b).index + 10000000*((**b).name[0]>'Z');
  4425         -  assert( i1!=i2 || strcmp((**a).name,(**b).name)==0 );
  4426         -  return i1-i2;
         4572  +  const struct symbol *a = *(const struct symbol **) _a;
         4573  +  const struct symbol *b = *(const struct symbol **) _b;
         4574  +  int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1;
         4575  +  int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1;
         4576  +  return i1==i2 ? a->index - b->index : i1 - i2;
  4427   4577   }
  4428   4578   
  4429   4579   /* There is one instance of the following structure for each
  4430   4580   ** associative array of type "x2".
  4431   4581   */
  4432   4582   struct s_x2 {
  4433   4583     int size;               /* The number of available slots. */
................................................................................
  4454   4604   /* Allocate a new associative array */
  4455   4605   void Symbol_init(){
  4456   4606     if( x2a ) return;
  4457   4607     x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
  4458   4608     if( x2a ){
  4459   4609       x2a->size = 128;
  4460   4610       x2a->count = 0;
  4461         -    x2a->tbl = (x2node*)malloc( 
  4462         -      (sizeof(x2node) + sizeof(x2node*))*128 );
         4611  +    x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*));
  4463   4612       if( x2a->tbl==0 ){
  4464   4613         free(x2a);
  4465   4614         x2a = 0;
  4466   4615       }else{
  4467   4616         int i;
  4468   4617         x2a->ht = (x2node**)&(x2a->tbl[128]);
  4469   4618         for(i=0; i<128; i++) x2a->ht[i] = 0;
................................................................................
  4492   4641     }
  4493   4642     if( x2a->count>=x2a->size ){
  4494   4643       /* Need to make the hash table bigger */
  4495   4644       int i,size;
  4496   4645       struct s_x2 array;
  4497   4646       array.size = size = x2a->size*2;
  4498   4647       array.count = x2a->count;
  4499         -    array.tbl = (x2node*)malloc(
  4500         -      (sizeof(x2node) + sizeof(x2node*))*size );
         4648  +    array.tbl = (x2node*)calloc(size, sizeof(x2node) + sizeof(x2node*));
  4501   4649       if( array.tbl==0 ) return 0;  /* Fail due to malloc failure */
  4502   4650       array.ht = (x2node**)&(array.tbl[size]);
  4503   4651       for(i=0; i<size; i++) array.ht[i] = 0;
  4504   4652       for(i=0; i<x2a->count; i++){
  4505   4653         x2node *oldnp, *newnp;
  4506   4654         oldnp = &(x2a->tbl[i]);
  4507   4655         h = strhash(oldnp->key) & (size-1);
................................................................................
  4653   4801   /* Allocate a new associative array */
  4654   4802   void State_init(){
  4655   4803     if( x3a ) return;
  4656   4804     x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
  4657   4805     if( x3a ){
  4658   4806       x3a->size = 128;
  4659   4807       x3a->count = 0;
  4660         -    x3a->tbl = (x3node*)malloc( 
  4661         -      (sizeof(x3node) + sizeof(x3node*))*128 );
         4808  +    x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*));
  4662   4809       if( x3a->tbl==0 ){
  4663   4810         free(x3a);
  4664   4811         x3a = 0;
  4665   4812       }else{
  4666   4813         int i;
  4667   4814         x3a->ht = (x3node**)&(x3a->tbl[128]);
  4668   4815         for(i=0; i<128; i++) x3a->ht[i] = 0;
................................................................................
  4691   4838     }
  4692   4839     if( x3a->count>=x3a->size ){
  4693   4840       /* Need to make the hash table bigger */
  4694   4841       int i,size;
  4695   4842       struct s_x3 array;
  4696   4843       array.size = size = x3a->size*2;
  4697   4844       array.count = x3a->count;
  4698         -    array.tbl = (x3node*)malloc(
  4699         -      (sizeof(x3node) + sizeof(x3node*))*size );
         4845  +    array.tbl = (x3node*)calloc(size, sizeof(x3node) + sizeof(x3node*));
  4700   4846       if( array.tbl==0 ) return 0;  /* Fail due to malloc failure */
  4701   4847       array.ht = (x3node**)&(array.tbl[size]);
  4702   4848       for(i=0; i<size; i++) array.ht[i] = 0;
  4703   4849       for(i=0; i<x3a->count; i++){
  4704   4850         x3node *oldnp, *newnp;
  4705   4851         oldnp = &(x3a->tbl[i]);
  4706   4852         h = statehash(oldnp->key) & (size-1);
................................................................................
  4749   4895   ** problems, or if the array is empty. */
  4750   4896   struct state **State_arrayof()
  4751   4897   {
  4752   4898     struct state **array;
  4753   4899     int i,size;
  4754   4900     if( x3a==0 ) return 0;
  4755   4901     size = x3a->count;
  4756         -  array = (struct state **)malloc( sizeof(struct state *)*size );
         4902  +  array = (struct state **)calloc(size, sizeof(struct state *));
  4757   4903     if( array ){
  4758   4904       for(i=0; i<size; i++) array[i] = x3a->tbl[i].data;
  4759   4905     }
  4760   4906     return array;
  4761   4907   }
  4762   4908   
  4763   4909   /* Hash a configuration */
................................................................................
  4795   4941   /* Allocate a new associative array */
  4796   4942   void Configtable_init(){
  4797   4943     if( x4a ) return;
  4798   4944     x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
  4799   4945     if( x4a ){
  4800   4946       x4a->size = 64;
  4801   4947       x4a->count = 0;
  4802         -    x4a->tbl = (x4node*)malloc( 
  4803         -      (sizeof(x4node) + sizeof(x4node*))*64 );
         4948  +    x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*));
  4804   4949       if( x4a->tbl==0 ){
  4805   4950         free(x4a);
  4806   4951         x4a = 0;
  4807   4952       }else{
  4808   4953         int i;
  4809   4954         x4a->ht = (x4node**)&(x4a->tbl[64]);
  4810   4955         for(i=0; i<64; i++) x4a->ht[i] = 0;
................................................................................
  4833   4978     }
  4834   4979     if( x4a->count>=x4a->size ){
  4835   4980       /* Need to make the hash table bigger */
  4836   4981       int i,size;
  4837   4982       struct s_x4 array;
  4838   4983       array.size = size = x4a->size*2;
  4839   4984       array.count = x4a->count;
  4840         -    array.tbl = (x4node*)malloc(
  4841         -      (sizeof(x4node) + sizeof(x4node*))*size );
         4985  +    array.tbl = (x4node*)calloc(size, sizeof(x4node) + sizeof(x4node*));
  4842   4986       if( array.tbl==0 ) return 0;  /* Fail due to malloc failure */
  4843   4987       array.ht = (x4node**)&(array.tbl[size]);
  4844   4988       for(i=0; i<size; i++) array.ht[i] = 0;
  4845   4989       for(i=0; i<x4a->count; i++){
  4846   4990         x4node *oldnp, *newnp;
  4847   4991         oldnp = &(x4a->tbl[i]);
  4848   4992         h = confighash(oldnp->data) & (size-1);

Changes to tool/mkkeywordhash.c.

   134    134   #  define VTAB       0x00010000
   135    135   #endif
   136    136   #ifdef SQLITE_OMIT_AUTOVACUUM
   137    137   #  define AUTOVACUUM 0
   138    138   #else
   139    139   #  define AUTOVACUUM 0x00020000
   140    140   #endif
          141  +#ifdef SQLITE_OMIT_CTE
          142  +#  define CTE        0
          143  +#else
          144  +#  define CTE        0x00040000
          145  +#endif
   141    146   
   142    147   /*
   143    148   ** These are the keywords
   144    149   */
   145    150   static Keyword aKeywordTable[] = {
   146    151     { "ABORT",            "TK_ABORT",        CONFLICT|TRIGGER       },
   147    152     { "ACTION",           "TK_ACTION",       FKEY                   },
................................................................................
   230    235     { "ORDER",            "TK_ORDER",        ALWAYS                 },
   231    236     { "OUTER",            "TK_JOIN_KW",      ALWAYS                 },
   232    237     { "PLAN",             "TK_PLAN",         EXPLAIN                },
   233    238     { "PRAGMA",           "TK_PRAGMA",       PRAGMA                 },
   234    239     { "PRIMARY",          "TK_PRIMARY",      ALWAYS                 },
   235    240     { "QUERY",            "TK_QUERY",        EXPLAIN                },
   236    241     { "RAISE",            "TK_RAISE",        TRIGGER                },
          242  +  { "RECURSIVE",        "TK_RECURSIVE",    CTE                    },
   237    243     { "REFERENCES",       "TK_REFERENCES",   FKEY                   },
   238    244     { "REGEXP",           "TK_LIKE_KW",      ALWAYS                 },
   239    245     { "REINDEX",          "TK_REINDEX",      REINDEX                },
   240    246     { "RELEASE",          "TK_RELEASE",      ALWAYS                 },
   241    247     { "RENAME",           "TK_RENAME",       ALTER                  },
   242    248     { "REPLACE",          "TK_REPLACE",      CONFLICT               },
   243    249     { "RESTRICT",         "TK_RESTRICT",     FKEY                   },
................................................................................
   258    264     { "UNIQUE",           "TK_UNIQUE",       ALWAYS                 },
   259    265     { "UPDATE",           "TK_UPDATE",       ALWAYS                 },
   260    266     { "USING",            "TK_USING",        ALWAYS                 },
   261    267     { "VACUUM",           "TK_VACUUM",       VACUUM                 },
   262    268     { "VALUES",           "TK_VALUES",       ALWAYS                 },
   263    269     { "VIEW",             "TK_VIEW",         VIEW                   },
   264    270     { "VIRTUAL",          "TK_VIRTUAL",      VTAB                   },
          271  +  { "WITH",             "TK_WITH",         CTE                    },
   265    272     { "WITHOUT",          "TK_WITHOUT",      ALWAYS                 },
   266    273     { "WHEN",             "TK_WHEN",         ALWAYS                 },
   267    274     { "WHERE",            "TK_WHERE",        ALWAYS                 },
   268    275   };
   269    276   
   270    277   /* Number of keywords */
   271    278   static int nKeyword = (sizeof(aKeywordTable)/sizeof(aKeywordTable[0]));