/ Check-in [1f2e1b0c]
Login

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

Overview
Comment:Merge latest trunk changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | deferred-open
Files: files | file ages | folders
SHA1: 1f2e1b0c64934ec80f09d596d512f7c320e36f81
User & Date: dan 2014-02-11 05:26:22
Context
2014-02-11
19:35
Test case updates to account for the fact that database files are not created until they are first written. check-in: 8ef97420 user: dan tags: deferred-open
05:26
Merge latest trunk changes. check-in: 1f2e1b0c user: dan tags: deferred-open
2014-02-10
21:09
Fix a bug causing the POWERSAFE_OVERWRITE device-characteristic flag to be set incorrectly if file opening is deferred. check-in: 95d0c58d user: dan tags: deferred-open
21:07
Fix a pointless conditional. Add a test case. check-in: 9367632d user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/expr.c.

  1056   1056     if( pPrior ) pPrior->pNext = pNew;
  1057   1057     pNew->pNext = 0;
  1058   1058     pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
  1059   1059     pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
  1060   1060     pNew->iLimit = 0;
  1061   1061     pNew->iOffset = 0;
  1062   1062     pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
  1063         -  pNew->pRightmost = 0;
  1064   1063     pNew->addrOpenEphm[0] = -1;
  1065   1064     pNew->addrOpenEphm[1] = -1;
  1066   1065     pNew->addrOpenEphm[2] = -1;
  1067   1066     pNew->nSelectRow = p->nSelectRow;
  1068   1067     pNew->pWith = withDup(db, p->pWith);
  1069   1068     return pNew;
  1070   1069   }

Changes to src/pager.c.

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

Changes to src/parse.y.

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

Changes to src/select.c.

   104    104   */
   105    105   void sqlite3SelectDelete(sqlite3 *db, Select *p){
   106    106     if( p ){
   107    107       clearSelect(db, p);
   108    108       sqlite3DbFree(db, p);
   109    109     }
   110    110   }
          111  +
          112  +/*
          113  +** Return a pointer to the right-most SELECT statement in a compound.
          114  +*/
          115  +static Select *findRightmost(Select *p){
          116  +  while( p->pNext ) p = p->pNext;
          117  +  return p;
          118  +}
   111    119   
   112    120   /*
   113    121   ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
   114    122   ** type of join.  Return an integer constant that expresses that type
   115    123   ** in terms of the following bit values:
   116    124   **
   117    125   **     JT_INNER
................................................................................
  1873   1881       p->selFlags |= SF_UsesEphemeral;
  1874   1882     }
  1875   1883   
  1876   1884     /* Detach the ORDER BY clause from the compound SELECT */
  1877   1885     p->pOrderBy = 0;
  1878   1886   
  1879   1887     /* Store the results of the setup-query in Queue. */
         1888  +  pSetup->pNext = 0;
  1880   1889     rc = sqlite3Select(pParse, pSetup, &destQueue);
         1890  +  pSetup->pNext = p;
  1881   1891     if( rc ) goto end_of_recursive_query;
  1882   1892   
  1883   1893     /* Find the next row in the Queue and output that row */
  1884   1894     addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak);
  1885   1895   
  1886   1896     /* Transfer the next row in Queue over to Current */
  1887   1897     sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
................................................................................
  1978   1988     /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
  1979   1989     ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
  1980   1990     */
  1981   1991     assert( p && p->pPrior );  /* Calling function guarantees this much */
  1982   1992     assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
  1983   1993     db = pParse->db;
  1984   1994     pPrior = p->pPrior;
  1985         -  assert( pPrior->pRightmost!=pPrior );
  1986         -  assert( pPrior->pRightmost==p->pRightmost );
  1987   1995     dest = *pDest;
  1988   1996     if( pPrior->pOrderBy ){
  1989   1997       sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
  1990   1998         selectOpName(p->op));
  1991   1999       rc = 1;
  1992   2000       goto multi_select_end;
  1993   2001     }
................................................................................
  2087   2095         Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
  2088   2096         int addr;
  2089   2097         SelectDest uniondest;
  2090   2098   
  2091   2099         testcase( p->op==TK_EXCEPT );
  2092   2100         testcase( p->op==TK_UNION );
  2093   2101         priorOp = SRT_Union;
  2094         -      if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
         2102  +      if( dest.eDest==priorOp ){
  2095   2103           /* We can reuse a temporary table generated by a SELECT to our
  2096   2104           ** right.
  2097   2105           */
  2098         -        assert( p->pRightmost!=p );  /* Can only happen for leftward elements
  2099         -                                     ** of a 3-way or more compound */
  2100   2106           assert( p->pLimit==0 );      /* Not allowed on leftward elements */
  2101   2107           assert( p->pOffset==0 );     /* Not allowed on leftward elements */
  2102   2108           unionTab = dest.iSDParm;
  2103   2109         }else{
  2104   2110           /* We will need to create our own temporary table to hold the
  2105   2111           ** intermediate results.
  2106   2112           */
  2107   2113           unionTab = pParse->nTab++;
  2108   2114           assert( p->pOrderBy==0 );
  2109   2115           addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
  2110   2116           assert( p->addrOpenEphm[0] == -1 );
  2111   2117           p->addrOpenEphm[0] = addr;
  2112         -        p->pRightmost->selFlags |= SF_UsesEphemeral;
         2118  +        findRightmost(p)->selFlags |= SF_UsesEphemeral;
  2113   2119           assert( p->pEList );
  2114   2120         }
  2115   2121   
  2116   2122         /* Code the SELECT statements to our left
  2117   2123         */
  2118   2124         assert( !pPrior->pOrderBy );
  2119   2125         sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
................................................................................
  2194   2200         tab1 = pParse->nTab++;
  2195   2201         tab2 = pParse->nTab++;
  2196   2202         assert( p->pOrderBy==0 );
  2197   2203   
  2198   2204         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
  2199   2205         assert( p->addrOpenEphm[0] == -1 );
  2200   2206         p->addrOpenEphm[0] = addr;
  2201         -      p->pRightmost->selFlags |= SF_UsesEphemeral;
         2207  +      findRightmost(p)->selFlags |= SF_UsesEphemeral;
  2202   2208         assert( p->pEList );
  2203   2209   
  2204   2210         /* Code the SELECTs to our left into temporary table "tab1".
  2205   2211         */
  2206   2212         sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
  2207   2213         explainSetInteger(iSub1, pParse->iNextSelectId);
  2208   2214         rc = sqlite3Select(pParse, pPrior, &intersectdest);
................................................................................
  2273   2279     if( p->selFlags & SF_UsesEphemeral ){
  2274   2280       int i;                        /* Loop counter */
  2275   2281       KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
  2276   2282       Select *pLoop;                /* For looping through SELECT statements */
  2277   2283       CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
  2278   2284       int nCol;                     /* Number of columns in result set */
  2279   2285   
  2280         -    assert( p->pRightmost==p );
         2286  +    assert( p->pNext==0 );
  2281   2287       nCol = p->pEList->nExpr;
  2282   2288       pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
  2283   2289       if( !pKeyInfo ){
  2284   2290         rc = SQLITE_NOMEM;
  2285   2291         goto multi_select_end;
  2286   2292       }
  2287   2293       for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
................................................................................
  2689   2695         }
  2690   2696       }
  2691   2697     }
  2692   2698    
  2693   2699     /* Separate the left and the right query from one another
  2694   2700     */
  2695   2701     p->pPrior = 0;
         2702  +  pPrior->pNext = 0;
  2696   2703     sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
  2697   2704     if( pPrior->pPrior==0 ){
  2698   2705       sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
  2699   2706     }
  2700   2707   
  2701   2708     /* Compute the limit registers */
  2702   2709     computeLimitRegisters(pParse, p, labelEnd);
................................................................................
  2854   2861   
  2855   2862     /* Reassembly the compound query so that it will be freed correctly
  2856   2863     ** by the calling function */
  2857   2864     if( p->pPrior ){
  2858   2865       sqlite3SelectDelete(db, p->pPrior);
  2859   2866     }
  2860   2867     p->pPrior = pPrior;
         2868  +  pPrior->pNext = p;
  2861   2869   
  2862   2870     /*** TBD:  Insert subroutine calls to close cursors on incomplete
  2863   2871     **** subqueries ****/
  2864   2872     explainComposite(pParse, p->op, iSub1, iSub2, 0);
  2865   2873     return SQLITE_OK;
  2866   2874   }
  2867   2875   #endif
................................................................................
  3119   3127     /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
  3120   3128     ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
  3121   3129     ** because they could be computed at compile-time.  But when LIMIT and OFFSET
  3122   3130     ** became arbitrary expressions, we were forced to add restrictions (13)
  3123   3131     ** and (14). */
  3124   3132     if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
  3125   3133     if( pSub->pOffset ) return 0;                          /* Restriction (14) */
  3126         -  if( p->pRightmost && pSub->pLimit ){
         3134  +  if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
  3127   3135       return 0;                                            /* Restriction (15) */
  3128   3136     }
  3129   3137     if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
  3130   3138     if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
  3131   3139     if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
  3132   3140        return 0;         /* Restrictions (8)(9) */
  3133   3141     }
................................................................................
  3270   3278       p->pOffset = 0;
  3271   3279       pNew = sqlite3SelectDup(db, p, 0);
  3272   3280       p->pOffset = pOffset;
  3273   3281       p->pLimit = pLimit;
  3274   3282       p->pOrderBy = pOrderBy;
  3275   3283       p->pSrc = pSrc;
  3276   3284       p->op = TK_ALL;
  3277         -    p->pRightmost = 0;
  3278   3285       if( pNew==0 ){
  3279         -      pNew = pPrior;
         3286  +      p->pPrior = pPrior;
  3280   3287       }else{
  3281   3288         pNew->pPrior = pPrior;
  3282         -      pNew->pRightmost = 0;
         3289  +      if( pPrior ) pPrior->pNext = pNew;
         3290  +      pNew->pNext = p;
         3291  +      p->pPrior = pNew;
  3283   3292       }
  3284         -    p->pPrior = pNew;
  3285   3293       if( db->mallocFailed ) return 1;
  3286   3294     }
  3287   3295   
  3288   3296     /* Begin flattening the iFrom-th entry of the FROM clause 
  3289   3297     ** in the outer query.
  3290   3298     */
  3291   3299     pSub = pSub1 = pSubitem->pSelect;
................................................................................
  3616   3624     p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
  3617   3625     p->op = TK_SELECT;
  3618   3626     p->pWhere = 0;
  3619   3627     pNew->pGroupBy = 0;
  3620   3628     pNew->pHaving = 0;
  3621   3629     pNew->pOrderBy = 0;
  3622   3630     p->pPrior = 0;
         3631  +  p->pNext = 0;
         3632  +  p->selFlags &= ~SF_Compound;
         3633  +  assert( pNew->pPrior!=0 );
         3634  +  pNew->pPrior->pNext = pNew;
  3623   3635     pNew->pLimit = 0;
  3624   3636     pNew->pOffset = 0;
  3625   3637     return WRC_Continue;
  3626   3638   }
  3627   3639   
  3628   3640   #ifndef SQLITE_OMIT_CTE
  3629   3641   /*
................................................................................
  3803   3815   **
  3804   3816   ** This function is used as the xSelectCallback2() callback by
  3805   3817   ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
  3806   3818   ** names and other FROM clause elements. 
  3807   3819   */
  3808   3820   static void selectPopWith(Walker *pWalker, Select *p){
  3809   3821     Parse *pParse = pWalker->pParse;
  3810         -  if( p->pWith ){
  3811         -    assert( pParse->pWith==p->pWith );
  3812         -    pParse->pWith = p->pWith->pOuter;
         3822  +  With *pWith = findRightmost(p)->pWith;
         3823  +  if( pWith!=0 ){
         3824  +    assert( pParse->pWith==pWith );
         3825  +    pParse->pWith = pWith->pOuter;
  3813   3826     }
  3814   3827   }
  3815   3828   #else
  3816   3829   #define selectPopWith 0
  3817   3830   #endif
  3818   3831   
  3819   3832   /*
................................................................................
  3855   3868       return WRC_Abort;
  3856   3869     }
  3857   3870     if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
  3858   3871       return WRC_Prune;
  3859   3872     }
  3860   3873     pTabList = p->pSrc;
  3861   3874     pEList = p->pEList;
  3862         -  sqlite3WithPush(pParse, p->pWith, 0);
         3875  +  sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
  3863   3876   
  3864   3877     /* Make sure cursor numbers have been assigned to all entries in
  3865   3878     ** the FROM clause of the SELECT statement.
  3866   3879     */
  3867   3880     sqlite3SrcListAssignCursors(pParse, pTabList);
  3868   3881   
  3869   3882     /* Look up every table named in the FROM clause of the select.  If
................................................................................
  4533   4546         i = -1;
  4534   4547       }else if( pTabList->nSrc==1
  4535   4548              && OptimizationEnabled(db, SQLITE_SubqCoroutine)
  4536   4549       ){
  4537   4550         /* Implement a co-routine that will return a single row of the result
  4538   4551         ** set on each invocation.
  4539   4552         */
  4540         -      int addrTop;
         4553  +      int addrTop = sqlite3VdbeCurrentAddr(v)+1;
  4541   4554         pItem->regReturn = ++pParse->nMem;
  4542         -      sqlite3VdbeAddOp0(v, OP_Goto);
  4543         -      addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
  4544         -      sqlite3VdbeChangeP5(v, 1);
  4545         -      VdbeComment((v, "coroutine %s", pItem->pTab->zName));
         4555  +      sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
         4556  +      VdbeComment((v, "%s", pItem->pTab->zName));
  4546   4557         pItem->addrFillSub = addrTop;
  4547   4558         sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
  4548   4559         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
  4549   4560         sqlite3Select(pParse, pSub, &dest);
  4550   4561         pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
  4551   4562         pItem->viaCoroutine = 1;
  4552         -      sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
  4553         -      sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
         4563  +      pItem->regResult = dest.iSdst;
  4554   4564         sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
  4555   4565         sqlite3VdbeJumpHere(v, addrTop-1);
  4556   4566         sqlite3ClearTempRegCache(pParse);
  4557   4567       }else{
  4558   4568         /* Generate a subroutine that will fill an ephemeral table with
  4559   4569         ** the content of this subquery.  pItem->addrFillSub will point
  4560   4570         ** to the address of the generated subroutine.  pItem->regReturn
................................................................................
  4563   4573         int topAddr;
  4564   4574         int onceAddr = 0;
  4565   4575         int retAddr;
  4566   4576         assert( pItem->addrFillSub==0 );
  4567   4577         pItem->regReturn = ++pParse->nMem;
  4568   4578         topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
  4569   4579         pItem->addrFillSub = topAddr+1;
  4570         -      VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
  4571   4580         if( pItem->isCorrelated==0 ){
  4572   4581           /* If the subquery is not correlated and if we are not inside of
  4573   4582           ** a trigger, then we only need to compute the value of the subquery
  4574   4583           ** once. */
  4575   4584           onceAddr = sqlite3CodeOnce(pParse);
         4585  +        VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
         4586  +      }else{
         4587  +        VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
  4576   4588         }
  4577   4589         sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
  4578   4590         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
  4579   4591         sqlite3Select(pParse, pSub, &dest);
  4580   4592         pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
  4581   4593         if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
  4582   4594         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
................................................................................
  4600   4612     pHaving = p->pHaving;
  4601   4613     sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
  4602   4614   
  4603   4615   #ifndef SQLITE_OMIT_COMPOUND_SELECT
  4604   4616     /* If there is are a sequence of queries, do the earlier ones first.
  4605   4617     */
  4606   4618     if( p->pPrior ){
  4607         -    if( p->pRightmost==0 ){
  4608         -      Select *pLoop, *pRight = 0;
  4609         -      int cnt = 0;
  4610         -      int mxSelect;
  4611         -      for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
  4612         -        pLoop->pRightmost = p;
  4613         -        pLoop->pNext = pRight;
  4614         -        pRight = pLoop;
  4615         -      }
  4616         -      mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
  4617         -      if( mxSelect && cnt>mxSelect ){
  4618         -        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
  4619         -        goto select_end;
  4620         -      }
  4621         -    }
  4622   4619       rc = multiSelect(pParse, p, pDest);
  4623   4620       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
  4624   4621       return rc;
  4625   4622     }
  4626   4623   #endif
  4627   4624   
  4628   4625     /* If there is both a GROUP BY and an ORDER BY clause and they are
................................................................................
  5276   5273     }
  5277   5274   }
  5278   5275   void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
  5279   5276     if( p==0 ){
  5280   5277       sqlite3ExplainPrintf(pVdbe, "(null-select)");
  5281   5278       return;
  5282   5279     }
  5283         -  while( p->pPrior ){
  5284         -    p->pPrior->pNext = p;
  5285         -    p = p->pPrior;
  5286         -  }
  5287   5280     sqlite3ExplainPush(pVdbe);
  5288   5281     while( p ){
  5289   5282       explainOneSelect(pVdbe, p);
  5290   5283       p = p->pNext;
  5291   5284       if( p==0 ) break;
  5292   5285       sqlite3ExplainNL(pVdbe);
  5293   5286       sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));

Changes to src/sqliteInt.h.

  2015   2015       char *zDatabase;  /* Name of database holding this table */
  2016   2016       char *zName;      /* Name of the table */
  2017   2017       char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
  2018   2018       Table *pTab;      /* An SQL table corresponding to zName */
  2019   2019       Select *pSelect;  /* A SELECT statement used in place of a table name */
  2020   2020       int addrFillSub;  /* Address of subroutine to manifest a subquery */
  2021   2021       int regReturn;    /* Register holding return address of addrFillSub */
         2022  +    int regResult;    /* Registers holding results of a co-routine */
  2022   2023       u8 jointype;      /* Type of join between this able and the previous */
  2023   2024       unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
  2024   2025       unsigned isCorrelated :1;  /* True if sub-query is correlated */
  2025   2026       unsigned viaCoroutine :1;  /* Implemented as a co-routine */
  2026   2027       unsigned isRecursive :1;   /* True for recursive reference in WITH */
  2027   2028   #ifndef SQLITE_OMIT_EXPLAIN
  2028   2029       u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
................................................................................
  2143   2144     SrcList *pSrc;         /* The FROM clause */
  2144   2145     Expr *pWhere;          /* The WHERE clause */
  2145   2146     ExprList *pGroupBy;    /* The GROUP BY clause */
  2146   2147     Expr *pHaving;         /* The HAVING clause */
  2147   2148     ExprList *pOrderBy;    /* The ORDER BY clause */
  2148   2149     Select *pPrior;        /* Prior select in a compound select statement */
  2149   2150     Select *pNext;         /* Next select to the left in a compound */
  2150         -  Select *pRightmost;    /* Right-most select in a compound select statement */
  2151   2151     Expr *pLimit;          /* LIMIT expression. NULL means not used. */
  2152   2152     Expr *pOffset;         /* OFFSET expression. NULL means not used. */
  2153   2153     With *pWith;           /* WITH clause attached to this select. Or NULL. */
  2154   2154   };
  2155   2155   
  2156   2156   /*
  2157   2157   ** Allowed values for Select.selFlags.  The "SF" prefix stands for
................................................................................
  2165   2165   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
  2166   2166   #define SF_UseSorter       0x0040  /* Sort using a sorter */
  2167   2167   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  2168   2168   #define SF_Materialize     0x0100  /* NOT USED */
  2169   2169   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  2170   2170   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
  2171   2171   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
         2172  +#define SF_Compound        0x1000  /* Part of a compound query */
  2172   2173   
  2173   2174   
  2174   2175   /*
  2175   2176   ** The results of a SELECT can be distributed in several ways, as defined
  2176   2177   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2177   2178   ** Type".
  2178   2179   **

Changes to src/test5.c.

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

Changes to src/util.c.

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

Changes to src/vdbe.c.

   129    129   #define Deephemeralize(P) \
   130    130      if( ((P)->flags&MEM_Ephem)!=0 \
   131    131          && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
   132    132   
   133    133   /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
   134    134   #define isSorter(x) ((x)->pSorter!=0)
   135    135   
   136         -/*
   137         -** Argument pMem points at a register that will be passed to a
   138         -** user-defined function or returned to the user as the result of a query.
   139         -** This routine sets the pMem->type variable used by the sqlite3_value_*() 
   140         -** routines.
   141         -*/
   142         -void sqlite3VdbeMemStoreType(Mem *pMem){
   143         -  int flags = pMem->flags;
   144         -  if( flags & MEM_Null ){
   145         -    pMem->type = SQLITE_NULL;
   146         -  }
   147         -  else if( flags & MEM_Int ){
   148         -    pMem->type = SQLITE_INTEGER;
   149         -  }
   150         -  else if( flags & MEM_Real ){
   151         -    pMem->type = SQLITE_FLOAT;
   152         -  }
   153         -  else if( flags & MEM_Str ){
   154         -    pMem->type = SQLITE_TEXT;
   155         -  }else{
   156         -    pMem->type = SQLITE_BLOB;
   157         -  }
   158         -}
   159         -
   160    136   /*
   161    137   ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
   162    138   ** if we run out of memory.
   163    139   */
   164    140   static VdbeCursor *allocateCursor(
   165    141     Vdbe *p,              /* The virtual machine */
   166    142     int iCur,             /* Index of the new VdbeCursor */
................................................................................
   281    257   /*
   282    258   ** Try to convert the type of a function argument or a result column
   283    259   ** into a numeric representation.  Use either INTEGER or REAL whichever
   284    260   ** is appropriate.  But only do the conversion if it is possible without
   285    261   ** loss of information and return the revised type of the argument.
   286    262   */
   287    263   int sqlite3_value_numeric_type(sqlite3_value *pVal){
   288         -  Mem *pMem = (Mem*)pVal;
   289         -  if( pMem->type==SQLITE_TEXT ){
          264  +  int eType = sqlite3_value_type(pVal);
          265  +  if( eType==SQLITE_TEXT ){
          266  +    Mem *pMem = (Mem*)pVal;
   290    267       applyNumericAffinity(pMem);
   291    268       sqlite3VdbeMemStoreType(pMem);
          269  +    eType = sqlite3_value_type(pVal);
   292    270     }
   293         -  return pMem->type;
          271  +  return eType;
   294    272   }
   295    273   
   296    274   /*
   297    275   ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
   298    276   ** not the internal Mem* type.
   299    277   */
   300    278   void sqlite3ValueApplyAffinity(
................................................................................
  2277   2255     rc = sqlite3VdbeCursorMoveto(pC);
  2278   2256     if( rc ) goto abort_due_to_error;
  2279   2257     if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
  2280   2258       if( pC->nullRow ){
  2281   2259         if( pCrsr==0 ){
  2282   2260           assert( pC->pseudoTableReg>0 );
  2283   2261           pReg = &aMem[pC->pseudoTableReg];
  2284         -        if( pC->multiPseudo ){
  2285         -          sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
  2286         -          Deephemeralize(pDest);
  2287         -          goto op_column_out;
  2288         -        }
  2289   2262           assert( pReg->flags & MEM_Blob );
  2290   2263           assert( memIsValid(pReg) );
  2291   2264           pC->payloadSize = pC->szRow = avail = pReg->n;
  2292   2265           pC->aRow = (u8*)pReg->z;
  2293   2266         }else{
  2294   2267           MemSetTypeFlag(pDest, MEM_Null);
  2295   2268           goto op_column_out;
................................................................................
  3350   3323     pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3351   3324     assert( pCx->pKeyInfo->db==db );
  3352   3325     assert( pCx->pKeyInfo->enc==ENC(db) );
  3353   3326     rc = sqlite3VdbeSorterInit(db, pCx);
  3354   3327     break;
  3355   3328   }
  3356   3329   
  3357         -/* Opcode: OpenPseudo P1 P2 P3 * P5
  3358         -** Synopsis: content in r[P2@P3]
         3330  +/* Opcode: OpenPseudo P1 P2 P3 * *
         3331  +** Synopsis: P3 columns in r[P2]
  3359   3332   **
  3360   3333   ** Open a new cursor that points to a fake table that contains a single
  3361         -** row of data.  The content of that one row in the content of memory
  3362         -** register P2 when P5==0.  In other words, cursor P1 becomes an alias for the 
  3363         -** MEM_Blob content contained in register P2.  When P5==1, then the
  3364         -** row is represented by P3 consecutive registers beginning with P2.
         3334  +** row of data.  The content of that one row is the content of memory
         3335  +** register P2.  In other words, cursor P1 becomes an alias for the 
         3336  +** MEM_Blob content contained in register P2.
  3365   3337   **
  3366   3338   ** A pseudo-table created by this opcode is used to hold a single
  3367   3339   ** row output from the sorter so that the row can be decomposed into
  3368   3340   ** individual columns using the OP_Column opcode.  The OP_Column opcode
  3369   3341   ** is the only cursor opcode that works with a pseudo-table.
  3370   3342   **
  3371   3343   ** P3 is the number of fields in the records that will be stored by
................................................................................
  3377   3349     assert( pOp->p1>=0 );
  3378   3350     assert( pOp->p3>=0 );
  3379   3351     pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
  3380   3352     if( pCx==0 ) goto no_mem;
  3381   3353     pCx->nullRow = 1;
  3382   3354     pCx->pseudoTableReg = pOp->p2;
  3383   3355     pCx->isTable = 1;
  3384         -  pCx->multiPseudo = pOp->p5;
         3356  +  assert( pOp->p5==0 );
  3385   3357     break;
  3386   3358   }
  3387   3359   
  3388   3360   /* Opcode: Close P1 * * * *
  3389   3361   **
  3390   3362   ** Close a cursor previously opened as P1.  If P1 is not
  3391   3363   ** currently open, this instruction is a no-op.
................................................................................
  3690   3662     assert( pC->pCursor!=0 );
  3691   3663     assert( pC->isTable==0 );
  3692   3664     pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
  3693   3665     if( pOp->p4.i>0 ){
  3694   3666       r.pKeyInfo = pC->pKeyInfo;
  3695   3667       r.nField = (u16)pOp->p4.i;
  3696   3668       r.aMem = pIn3;
         3669  +    for(ii=0; ii<r.nField; ii++){
         3670  +      assert( memIsValid(&r.aMem[ii]) );
         3671  +      ExpandBlob(&r.aMem[ii]);
  3697   3672   #ifdef SQLITE_DEBUG
  3698         -    {
  3699         -      int i;
  3700         -      for(i=0; i<r.nField; i++){
  3701         -        assert( memIsValid(&r.aMem[i]) );
  3702         -        if( i ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
  3703         -      }
         3673  +      if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
         3674  +#endif
  3704   3675       }
  3705         -#endif
  3706   3676       r.flags = UNPACKED_PREFIX_MATCH;
  3707   3677       pIdxKey = &r;
  3708   3678     }else{
  3709   3679       pIdxKey = sqlite3VdbeAllocUnpackedRecord(
  3710   3680           pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
  3711   3681       ); 
  3712   3682       if( pIdxKey==0 ) goto no_mem;

Changes to src/vdbeInt.h.

    71     71     i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
    72     72     u8 nullRow;           /* True if pointing to a row with no data */
    73     73     u8 rowidIsValid;      /* True if lastRowid is valid */
    74     74     u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
    75     75     Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
    76     76     Bool isTable:1;       /* True if a table requiring integer keys */
    77     77     Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
    78         -  Bool multiPseudo:1;   /* Multi-register pseudo-cursor */
    79     78     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    80     79     i64 seqCount;         /* Sequence counter */
    81     80     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    82     81     i64 lastRowid;        /* Rowid being deleted by OP_Delete */
    83     82     VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
    84     83   
    85     84     /* Cached information about the header for the data record that the
................................................................................
   165    164       int nZero;          /* Used when bit MEM_Zero is set in flags */
   166    165       FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   167    166       RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   168    167       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   169    168     } u;
   170    169     int n;              /* Number of characters in string value, excluding '\0' */
   171    170     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   172         -  u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
          171  +  u8  memType;        /* Lower 5 bits of flags */
   173    172     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   174    173   #ifdef SQLITE_DEBUG
   175    174     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   176    175     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   177    176   #endif
   178    177     void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
   179    178     char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
................................................................................
   431    430     if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
   432    431   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   433    432   const char *sqlite3OpcodeName(int);
   434    433   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   435    434   int sqlite3VdbeCloseStatement(Vdbe *, int);
   436    435   void sqlite3VdbeFrameDelete(VdbeFrame*);
   437    436   int sqlite3VdbeFrameRestore(VdbeFrame *);
   438         -void sqlite3VdbeMemStoreType(Mem *pMem);
          437  +#define sqlite3VdbeMemStoreType(X)  (X)->memType = (u8)((X)->flags&0x1f)
          438  +/* void sqlite3VdbeMemStoreType(Mem *pMem); */
   439    439   int sqlite3VdbeTransferError(Vdbe *p);
   440    440   
   441    441   int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
   442    442   void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   443    443   int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
   444    444   int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
   445    445   int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);

Changes to src/vdbeapi.c.

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

Changes to src/vdbeaux.c.

  1341   1341         for(j=0; i>=apSub[j]->nOp; j++){
  1342   1342           i -= apSub[j]->nOp;
  1343   1343         }
  1344   1344         pOp = &apSub[j]->aOp[i];
  1345   1345       }
  1346   1346       if( p->explain==1 ){
  1347   1347         pMem->flags = MEM_Int;
  1348         -      pMem->type = SQLITE_INTEGER;
         1348  +      pMem->memType = MEM_Int;
  1349   1349         pMem->u.i = i;                                /* Program counter */
  1350   1350         pMem++;
  1351   1351     
  1352   1352         pMem->flags = MEM_Static|MEM_Str|MEM_Term;
  1353   1353         pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
  1354   1354         assert( pMem->z!=0 );
  1355   1355         pMem->n = sqlite3Strlen30(pMem->z);
  1356         -      pMem->type = SQLITE_TEXT;
         1356  +      pMem->memType = MEM_Str;
  1357   1357         pMem->enc = SQLITE_UTF8;
  1358   1358         pMem++;
  1359   1359   
  1360   1360         /* When an OP_Program opcode is encounter (the only opcode that has
  1361   1361         ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
  1362   1362         ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
  1363   1363         ** has not already been seen.
................................................................................
  1375   1375             pSub->n = nSub*sizeof(SubProgram*);
  1376   1376           }
  1377   1377         }
  1378   1378       }
  1379   1379   
  1380   1380       pMem->flags = MEM_Int;
  1381   1381       pMem->u.i = pOp->p1;                          /* P1 */
  1382         -    pMem->type = SQLITE_INTEGER;
         1382  +    pMem->memType = MEM_Int;
  1383   1383       pMem++;
  1384   1384   
  1385   1385       pMem->flags = MEM_Int;
  1386   1386       pMem->u.i = pOp->p2;                          /* P2 */
  1387         -    pMem->type = SQLITE_INTEGER;
         1387  +    pMem->memType = MEM_Int;
  1388   1388       pMem++;
  1389   1389   
  1390   1390       pMem->flags = MEM_Int;
  1391   1391       pMem->u.i = pOp->p3;                          /* P3 */
  1392         -    pMem->type = SQLITE_INTEGER;
         1392  +    pMem->memType = MEM_Int;
  1393   1393       pMem++;
  1394   1394   
  1395   1395       if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
  1396   1396         assert( p->db->mallocFailed );
  1397   1397         return SQLITE_ERROR;
  1398   1398       }
  1399   1399       pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
................................................................................
  1401   1401       if( zP4!=pMem->z ){
  1402   1402         sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
  1403   1403       }else{
  1404   1404         assert( pMem->z!=0 );
  1405   1405         pMem->n = sqlite3Strlen30(pMem->z);
  1406   1406         pMem->enc = SQLITE_UTF8;
  1407   1407       }
  1408         -    pMem->type = SQLITE_TEXT;
         1408  +    pMem->memType = MEM_Str;
  1409   1409       pMem++;
  1410   1410   
  1411   1411       if( p->explain==1 ){
  1412   1412         if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
  1413   1413           assert( p->db->mallocFailed );
  1414   1414           return SQLITE_ERROR;
  1415   1415         }
  1416   1416         pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
  1417   1417         pMem->n = 2;
  1418   1418         sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
  1419         -      pMem->type = SQLITE_TEXT;
         1419  +      pMem->memType = MEM_Str;
  1420   1420         pMem->enc = SQLITE_UTF8;
  1421   1421         pMem++;
  1422   1422     
  1423   1423   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
  1424   1424         if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
  1425   1425           assert( p->db->mallocFailed );
  1426   1426           return SQLITE_ERROR;
  1427   1427         }
  1428   1428         pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
  1429   1429         pMem->n = displayComment(pOp, zP4, pMem->z, 500);
  1430         -      pMem->type = SQLITE_TEXT;
         1430  +      pMem->memType = MEM_Str;
  1431   1431         pMem->enc = SQLITE_UTF8;
  1432   1432   #else
  1433   1433         pMem->flags = MEM_Null;                       /* Comment */
  1434         -      pMem->type = SQLITE_NULL;
         1434  +      pMem->memType = MEM_Null;
  1435   1435   #endif
  1436   1436       }
  1437   1437   
  1438   1438       p->nResColumn = 8 - 4*(p->explain-1);
  1439   1439       p->pResultSet = &p->aMem[1];
  1440   1440       p->rc = SQLITE_OK;
  1441   1441       rc = SQLITE_ROW;

Changes to src/vdbemem.c.

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

Changes to src/where.c.

  2783   2783     }
  2784   2784   
  2785   2785     /* Special case of a FROM clause subquery implemented as a co-routine */
  2786   2786     if( pTabItem->viaCoroutine ){
  2787   2787       int regYield = pTabItem->regReturn;
  2788   2788       sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
  2789   2789       pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
  2790         -    VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
         2790  +    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
  2791   2791       pLevel->op = OP_Goto;
  2792   2792     }else
  2793   2793   
  2794   2794   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2795   2795     if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  2796   2796       /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
  2797   2797       **          to access the data.
................................................................................
  5831   5831     /* The "break" point is here, just past the end of the outer loop.
  5832   5832     ** Set it.
  5833   5833     */
  5834   5834     sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
  5835   5835   
  5836   5836     assert( pWInfo->nLevel<=pTabList->nSrc );
  5837   5837     for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
         5838  +    int k, last;
         5839  +    VdbeOp *pOp;
  5838   5840       Index *pIdx = 0;
  5839   5841       struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
  5840   5842       Table *pTab = pTabItem->pTab;
  5841   5843       assert( pTab!=0 );
  5842   5844       pLoop = pLevel->pWLoop;
         5845  +
         5846  +    /* For a co-routine, change all OP_Column references to the table of
         5847  +    ** the co-routine into OP_SCopy of result contained in a register.
         5848  +    ** OP_Rowid becomes OP_Null.
         5849  +    */
         5850  +    if( pTabItem->viaCoroutine ){
         5851  +      last = sqlite3VdbeCurrentAddr(v);
         5852  +      k = pLevel->addrBody;
         5853  +      pOp = sqlite3VdbeGetOp(v, k);
         5854  +      for(; k<last; k++, pOp++){
         5855  +        if( pOp->p1!=pLevel->iTabCur ) continue;
         5856  +        if( pOp->opcode==OP_Column ){
         5857  +          pOp->opcode = OP_SCopy;
         5858  +          pOp->p1 = pOp->p2 + pTabItem->regResult;
         5859  +          pOp->p2 = pOp->p3;
         5860  +          pOp->p3 = 0;
         5861  +        }else if( pOp->opcode==OP_Rowid ){
         5862  +          pOp->opcode = OP_Null;
         5863  +          pOp->p1 = 0;
         5864  +          pOp->p3 = 0;
         5865  +        }
         5866  +      }
         5867  +      continue;
         5868  +    }
  5843   5869   
  5844   5870       /* Close all of the cursors that were opened by sqlite3WhereBegin.
  5845   5871       ** Except, do not close cursors that will be reused by the OR optimization
  5846   5872       ** (WHERE_OMIT_OPEN_CLOSE).  And do not close the OP_OpenWrite cursors
  5847   5873       ** created for the ONEPASS optimization.
  5848   5874       */
  5849   5875       if( (pTab->tabFlags & TF_Ephemeral)==0
................................................................................
  5875   5901       */
  5876   5902       if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
  5877   5903         pIdx = pLoop->u.btree.pIndex;
  5878   5904       }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
  5879   5905         pIdx = pLevel->u.pCovidx;
  5880   5906       }
  5881   5907       if( pIdx && !db->mallocFailed ){
  5882         -      int k, last;
  5883         -      VdbeOp *pOp;
  5884         -
  5885   5908         last = sqlite3VdbeCurrentAddr(v);
  5886   5909         k = pLevel->addrBody;
  5887   5910         pOp = sqlite3VdbeGetOp(v, k);
  5888   5911         for(; k<last; k++, pOp++){
  5889   5912           if( pOp->p1!=pLevel->iTabCur ) continue;
  5890   5913           if( pOp->opcode==OP_Column ){
  5891   5914             int x = pOp->p2;

Changes to test/distinct.test.

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

Changes to test/selectA.test.

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

Changes to test/speedtest1.c.

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

Changes to test/with1.test.

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