/ Check-in [2a1af0f2]
Login

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

Overview
Comment:Merge latest trunk changes with this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fts5
Files: files | file ages | folders
SHA1: 2a1af0f29e4c387721ec1fb3a2b55e8605b8401d
User & Date: dan 2015-06-26 19:33:04
Context
2015-06-26
20:08
Fix a segfault that could follow an OOM error in fts5. check-in: 713239b8 user: dan tags: fts5
19:33
Merge latest trunk changes with this branch. check-in: 2a1af0f2 user: dan tags: fts5
18:50
Fix some cases in the fts5 code where a corrupt database could cause a buffer overread. check-in: 360c57bb user: dan tags: fts5
18:47
Further optimization of SQL function dispatch. Improvements to opcode documentation. check-in: eaddbf29 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to mkopcodeh.awk.

   118    118     order[n_op++] = "OP_Explain";
   119    119   
   120    120     # Assign small values to opcodes that are processed by resolveP2Values()
   121    121     # to make code generation for the switch() statement smaller and faster.
   122    122     for(i=0; i<n_op; i++){
   123    123       name = order[i];
   124    124       if( op[name]>=0 ) continue;
   125         -    if( name=="OP_Function"      \
   126         -     || name=="OP_AggStep"       \
   127         -     || name=="OP_Transaction"   \
          125  +    if( name=="OP_Transaction"   \
   128    126        || name=="OP_AutoCommit"    \
   129    127        || name=="OP_Savepoint"     \
   130    128        || name=="OP_Checkpoint"    \
   131    129        || name=="OP_Vacuum"        \
   132    130        || name=="OP_JournalMode"   \
   133    131        || name=="OP_VUpdate"       \
   134    132        || name=="OP_VFilter"       \

Changes to src/analyze.c.

   939    939   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   940    940     sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
   941    941   #elif SQLITE_DEBUG
   942    942     assert( iParam==STAT_GET_STAT1 );
   943    943   #else
   944    944     UNUSED_PARAMETER( iParam );
   945    945   #endif
   946         -  sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
          946  +  sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4, regOut);
   947    947     sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
   948    948     sqlite3VdbeChangeP5(v, 1 + IsStat34);
   949    949   }
   950    950   
   951    951   /*
   952    952   ** Generate code to do an analysis of all indices associated with
   953    953   ** a single table.
................................................................................
  1094   1094       ** The third argument is only used for STAT3 and STAT4
  1095   1095       */
  1096   1096   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1097   1097       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
  1098   1098   #endif
  1099   1099       sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
  1100   1100       sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
  1101         -    sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
         1101  +    sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4+1, regStat4);
  1102   1102       sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
  1103   1103       sqlite3VdbeChangeP5(v, 2+IsStat34);
  1104   1104   
  1105   1105       /* Implementation of the following:
  1106   1106       **
  1107   1107       **   Rewind csr
  1108   1108       **   if eof(csr) goto end_of_scan;
................................................................................
  1190   1190           VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
  1191   1191         }
  1192   1192         sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
  1193   1193         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
  1194   1194       }
  1195   1195   #endif
  1196   1196       assert( regChng==(regStat4+1) );
  1197         -    sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
         1197  +    sqlite3VdbeAddOp3(v, OP_Function0, 1, regStat4, regTemp);
  1198   1198       sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
  1199   1199       sqlite3VdbeChangeP5(v, 2+IsStat34);
  1200   1200       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
  1201   1201   
  1202   1202       /* Add the entry to the stat1 table. */
  1203   1203       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
  1204   1204       assert( "BBB"[0]==SQLITE_AFF_TEXT );

Changes to src/attach.c.

   355    355     regArgs = sqlite3GetTempRange(pParse, 4);
   356    356     sqlite3ExprCode(pParse, pFilename, regArgs);
   357    357     sqlite3ExprCode(pParse, pDbname, regArgs+1);
   358    358     sqlite3ExprCode(pParse, pKey, regArgs+2);
   359    359   
   360    360     assert( v || db->mallocFailed );
   361    361     if( v ){
   362         -    sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
          362  +    sqlite3VdbeAddOp3(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3);
   363    363       assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
   364    364       sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
   365    365       sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
   366    366   
   367    367       /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
   368    368       ** statement only). For DETACH, set it to false (expire all existing
   369    369       ** statements).

Changes to src/btree.c.

  1438   1438     assert( gap<=65536 );
  1439   1439     /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
  1440   1440     ** and the reserved space is zero (the usual value for reserved space)
  1441   1441     ** then the cell content offset of an empty page wants to be 65536.
  1442   1442     ** However, that integer is too large to be stored in a 2-byte unsigned
  1443   1443     ** integer, so a value of 0 is used in its place. */
  1444   1444     top = get2byte(&data[hdr+5]);
  1445         -  assert( top<=pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
         1445  +  assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
  1446   1446     if( gap>top ){
  1447   1447       if( top==0 && pPage->pBt->usableSize==65536 ){
  1448   1448         top = 65536;
  1449   1449       }else{
  1450   1450         return SQLITE_CORRUPT_BKPT;
  1451   1451       }
  1452   1452     }

Changes to src/expr.c.

  2921   2921           pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
  2922   2922         }
  2923   2923   #endif
  2924   2924         if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  2925   2925           if( !pColl ) pColl = db->pDfltColl; 
  2926   2926           sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
  2927   2927         }
  2928         -      sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
         2928  +      sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
  2929   2929                           (char*)pDef, P4_FUNCDEF);
  2930   2930         sqlite3VdbeChangeP5(v, (u8)nFarg);
  2931   2931         if( nFarg && constMask==0 ){
  2932   2932           sqlite3ReleaseTempRange(pParse, r1, nFarg);
  2933   2933         }
  2934   2934         break;
  2935   2935       }

Changes to src/pcache.c.

    24     24     int szPage;                         /* Size of every page in this cache */
    25     25     int szExtra;                        /* Size of extra space for each page */
    26     26     u8 bPurgeable;                      /* True if pages are on backing store */
    27     27     u8 eCreate;                         /* eCreate value for for xFetch() */
    28     28     int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
    29     29     void *pStress;                      /* Argument to xStress */
    30     30     sqlite3_pcache *pCache;             /* Pluggable cache module */
    31         -  PgHdr *pPage1;                      /* Reference to page 1 */
    32     31   };
    33     32   
    34     33   /********************************** Linked List Management ********************/
    35     34   
    36     35   /* Allowed values for second argument to pcacheManageDirtyList() */
    37     36   #define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */
    38     37   #define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */
................................................................................
   102    101   
   103    102   /*
   104    103   ** Wrapper around the pluggable caches xUnpin method. If the cache is
   105    104   ** being used for an in-memory database, this function is a no-op.
   106    105   */
   107    106   static void pcacheUnpin(PgHdr *p){
   108    107     if( p->pCache->bPurgeable ){
   109         -    if( p->pgno==1 ){
   110         -      p->pCache->pPage1 = 0;
   111         -    }
   112    108       sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
   113    109     }
   114    110   }
   115    111   
   116    112   /*
   117    113   ** Compute the number of pages of cache requested.  p->szCache is the
   118    114   ** cache size requested by the "PRAGMA cache_size" statement.
................................................................................
   197    193       );
   198    194       if( pNew==0 ) return SQLITE_NOMEM;
   199    195       sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
   200    196       if( pCache->pCache ){
   201    197         sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   202    198       }
   203    199       pCache->pCache = pNew;
   204         -    pCache->pPage1 = 0;
   205    200       pCache->szPage = szPage;
   206    201     }
   207    202     return SQLITE_OK;
   208    203   }
   209    204   
   210    205   /*
   211    206   ** Try to obtain a page from the cache.
................................................................................
   355    350     if( !pPgHdr->pPage ){
   356    351       return pcacheFetchFinishWithInit(pCache, pgno, pPage);
   357    352     }
   358    353     if( 0==pPgHdr->nRef ){
   359    354       pCache->nRef++;
   360    355     }
   361    356     pPgHdr->nRef++;
   362         -  if( pgno==1 ){
   363         -    pCache->pPage1 = pPgHdr;
   364         -  }
   365    357     return pPgHdr;
   366    358   }
   367    359   
   368    360   /*
   369    361   ** Decrement the reference count on a page. If the page is clean and the
   370    362   ** reference count drops to 0, then it is made eligible for recycling.
   371    363   */
................................................................................
   398    390   */
   399    391   void sqlite3PcacheDrop(PgHdr *p){
   400    392     assert( p->nRef==1 );
   401    393     if( p->flags&PGHDR_DIRTY ){
   402    394       pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   403    395     }
   404    396     p->pCache->nRef--;
   405         -  if( p->pgno==1 ){
   406         -    p->pCache->pPage1 = 0;
   407         -  }
   408    397     sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
   409    398   }
   410    399   
   411    400   /*
   412    401   ** Make sure the page is marked as dirty. If it isn't dirty already,
   413    402   ** make it so.
   414    403   */
................................................................................
   491    480         */
   492    481         assert( p->pgno>0 );
   493    482         if( ALWAYS(p->pgno>pgno) ){
   494    483           assert( p->flags&PGHDR_DIRTY );
   495    484           sqlite3PcacheMakeClean(p);
   496    485         }
   497    486       }
   498         -    if( pgno==0 && pCache->pPage1 ){
   499         -      memset(pCache->pPage1->pData, 0, pCache->szPage);
   500         -      pgno = 1;
          487  +    if( pgno==0 && pCache->nRef ){
          488  +      sqlite3_pcache_page *pPage1;
          489  +      pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
          490  +      if( ALWAYS(pPage1) ){  /* Page 1 is always available in cache, because
          491  +                             ** pCache->nRef>0 */
          492  +        memset(pPage1->pBuf, 0, pCache->szPage);
          493  +        pgno = 1;
          494  +      }
   501    495       }
   502    496       sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
   503    497     }
   504    498   }
   505    499   
   506    500   /*
   507    501   ** Close a cache.

Changes to src/select.c.

  4682   4682         }
  4683   4683         if( !pColl ){
  4684   4684           pColl = pParse->db->pDfltColl;
  4685   4685         }
  4686   4686         if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
  4687   4687         sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
  4688   4688       }
  4689         -    sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
         4689  +    sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem,
  4690   4690                         (void*)pF->pFunc, P4_FUNCDEF);
  4691   4691       sqlite3VdbeChangeP5(v, (u8)nArg);
  4692   4692       sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
  4693   4693       sqlite3ReleaseTempRange(pParse, regAgg, nArg);
  4694   4694       if( addrNext ){
  4695   4695         sqlite3VdbeResolveLabel(v, addrNext);
  4696   4696         sqlite3ExprCacheClear(pParse);

Changes to src/vdbe.c.

  1542   1542     assert( pOp->p4type==P4_COLLSEQ );
  1543   1543     if( pOp->p1 ){
  1544   1544       sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
  1545   1545     }
  1546   1546     break;
  1547   1547   }
  1548   1548   
  1549         -/* Opcode: Function P1 P2 P3 P4 P5
         1549  +/* Opcode: Function0 P1 P2 P3 P4 P5
  1550   1550   ** Synopsis: r[P3]=func(r[P2@P5])
  1551   1551   **
  1552         -** Invoke a user function (P4 is a pointer to a Function structure that
         1552  +** Invoke a user function (P4 is a pointer to a FuncDef object that
  1553   1553   ** defines the function) with P5 arguments taken from register P2 and
  1554   1554   ** successors.  The result of the function is stored in register P3.
  1555   1555   ** Register P3 must not be one of the function inputs.
  1556   1556   **
  1557   1557   ** P1 is a 32-bit bitmask indicating whether or not each argument to the 
  1558   1558   ** function was determined to be constant at compile time. If the first
  1559   1559   ** argument was constant then bit 0 of P1 is set. This is used to determine
  1560   1560   ** whether meta data associated with a user function argument using the
  1561   1561   ** sqlite3_set_auxdata() API may be safely retained until the next
  1562   1562   ** invocation of this opcode.
  1563   1563   **
  1564         -** See also: AggStep and AggFinal
         1564  +** See also: Function, AggStep, AggFinal
  1565   1565   */
  1566         -case OP_Function: {
  1567         -  int i;
  1568         -  Mem *pArg;
  1569         -  sqlite3_context ctx;
  1570         -  sqlite3_value **apVal;
         1566  +/* Opcode: Function P1 P2 P3 P4 P5
         1567  +** Synopsis: r[P3]=func(r[P2@P5])
         1568  +**
         1569  +** Invoke a user function (P4 is a pointer to an sqlite3_context object that
         1570  +** contains a pointer to the function to be run) with P5 arguments taken
         1571  +** from register P2 and successors.  The result of the function is stored
         1572  +** in register P3.  Register P3 must not be one of the function inputs.
         1573  +**
         1574  +** P1 is a 32-bit bitmask indicating whether or not each argument to the 
         1575  +** function was determined to be constant at compile time. If the first
         1576  +** argument was constant then bit 0 of P1 is set. This is used to determine
         1577  +** whether meta data associated with a user function argument using the
         1578  +** sqlite3_set_auxdata() API may be safely retained until the next
         1579  +** invocation of this opcode.
         1580  +**
         1581  +** SQL functions are initially coded as OP_Function0 with P4 pointing
         1582  +** to a FuncDef object.  But on first evaluation, the P4 operand is
         1583  +** automatically converted into an sqlite3_context object and the operation
         1584  +** changed to this OP_Function opcode.  In this way, the initialization of
         1585  +** the sqlite3_context object occurs only once, rather than once for each
         1586  +** evaluation of the function.
         1587  +**
         1588  +** See also: Function0, AggStep, AggFinal
         1589  +*/
         1590  +case OP_Function0: {
  1571   1591     int n;
  1572         -
  1573         -  n = pOp->p5;
  1574         -  apVal = p->apArg;
  1575         -  assert( apVal || n==0 );
  1576         -  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  1577         -  ctx.pOut = &aMem[pOp->p3];
  1578         -  memAboutToChange(p, ctx.pOut);
  1579         -
  1580         -  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
  1581         -  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
  1582         -  pArg = &aMem[pOp->p2];
  1583         -  for(i=0; i<n; i++, pArg++){
  1584         -    assert( memIsValid(pArg) );
  1585         -    apVal[i] = pArg;
  1586         -    Deephemeralize(pArg);
  1587         -    REGISTER_TRACE(pOp->p2+i, pArg);
  1588         -  }
         1592  +  sqlite3_context *pCtx;
  1589   1593   
  1590   1594     assert( pOp->p4type==P4_FUNCDEF );
  1591         -  ctx.pFunc = pOp->p4.pFunc;
  1592         -  ctx.iOp = (int)(pOp - aOp);
  1593         -  ctx.pVdbe = p;
  1594         -  MemSetTypeFlag(ctx.pOut, MEM_Null);
  1595         -  ctx.fErrorOrAux = 0;
         1595  +  n = pOp->p5;
         1596  +  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
         1597  +  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
         1598  +  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
         1599  +  pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
         1600  +  if( pCtx==0 ) goto no_mem;
         1601  +  pCtx->pOut = 0;
         1602  +  pCtx->pFunc = pOp->p4.pFunc;
         1603  +  pCtx->iOp = (int)(pOp - aOp);
         1604  +  pCtx->pVdbe = p;
         1605  +  pCtx->argc = n;
         1606  +  pOp->p4type = P4_FUNCCTX;
         1607  +  pOp->p4.pCtx = pCtx;
         1608  +  pOp->opcode = OP_Function;
         1609  +  /* Fall through into OP_Function */
         1610  +}
         1611  +case OP_Function: {
         1612  +  int i;
         1613  +  sqlite3_context *pCtx;
         1614  +
         1615  +  assert( pOp->p4type==P4_FUNCCTX );
         1616  +  pCtx = pOp->p4.pCtx;
         1617  +
         1618  +  /* If this function is inside of a trigger, the register array in aMem[]
         1619  +  ** might change from one evaluation to the next.  The next block of code
         1620  +  ** checks to see if the register array has changed, and if so it
         1621  +  ** reinitializes the relavant parts of the sqlite3_context object */
         1622  +  pOut = &aMem[pOp->p3];
         1623  +  if( pCtx->pOut != pOut ){
         1624  +    pCtx->pOut = pOut;
         1625  +    for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
         1626  +  }
         1627  +
         1628  +  memAboutToChange(p, pCtx->pOut);
         1629  +#ifdef SQLITE_DEBUG
         1630  +  for(i=0; i<pCtx->argc; i++){
         1631  +    assert( memIsValid(pCtx->argv[i]) );
         1632  +    REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
         1633  +  }
         1634  +#endif
         1635  +  MemSetTypeFlag(pCtx->pOut, MEM_Null);
         1636  +  pCtx->fErrorOrAux = 0;
  1596   1637     db->lastRowid = lastRowid;
  1597         -  (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
         1638  +  (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */
  1598   1639     lastRowid = db->lastRowid;  /* Remember rowid changes made by xFunc */
  1599   1640   
  1600   1641     /* If the function returned an error, throw an exception */
  1601         -  if( ctx.fErrorOrAux ){
  1602         -    if( ctx.isError ){
  1603         -      sqlite3VdbeError(p, "%s", sqlite3_value_text(ctx.pOut));
  1604         -      rc = ctx.isError;
         1642  +  if( pCtx->fErrorOrAux ){
         1643  +    if( pCtx->isError ){
         1644  +      sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
         1645  +      rc = pCtx->isError;
  1605   1646       }
  1606         -    sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
         1647  +    sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1);
  1607   1648     }
  1608   1649   
  1609   1650     /* Copy the result of the function into register P3 */
  1610         -  sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
  1611         -  if( sqlite3VdbeMemTooBig(ctx.pOut) ){
  1612         -    goto too_big;
         1651  +  if( pOut->flags & (MEM_Str|MEM_Blob) ){
         1652  +    sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
         1653  +    if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
  1613   1654     }
  1614   1655   
  1615         -  REGISTER_TRACE(pOp->p3, ctx.pOut);
  1616         -  UPDATE_MAX_BLOBSIZE(ctx.pOut);
         1656  +  REGISTER_TRACE(pOp->p3, pCtx->pOut);
         1657  +  UPDATE_MAX_BLOBSIZE(pCtx->pOut);
  1617   1658     break;
  1618   1659   }
  1619   1660   
  1620   1661   /* Opcode: BitAnd P1 P2 P3 * *
  1621   1662   ** Synopsis:  r[P3]=r[P1]&r[P2]
  1622   1663   **
  1623   1664   ** Take the bit-wise AND of the values in register P1 and P2 and
................................................................................
  5693   5734     pIn1 = &aMem[pOp->p1];
  5694   5735     assert( pIn1->flags&MEM_Int );
  5695   5736     VdbeBranchTaken(pIn1->u.i==0, 2);
  5696   5737     if( (pIn1->u.i++)==0 ) goto jump_to_p2;
  5697   5738     break;
  5698   5739   }
  5699   5740   
  5700         -/* Opcode: AggStep * P2 P3 P4 P5
         5741  +/* Opcode: AggStep0 * P2 P3 P4 P5
  5701   5742   ** Synopsis: accum=r[P3] step(r[P2@P5])
  5702   5743   **
  5703   5744   ** Execute the step function for an aggregate.  The
  5704   5745   ** function has P5 arguments.   P4 is a pointer to the FuncDef
  5705         -** structure that specifies the function.  Use register
  5706         -** P3 as the accumulator.
         5746  +** structure that specifies the function.  Register P3 is the
         5747  +** accumulator.
         5748  +**
         5749  +** The P5 arguments are taken from register P2 and its
         5750  +** successors.
         5751  +*/
         5752  +/* Opcode: AggStep * P2 P3 P4 P5
         5753  +** Synopsis: accum=r[P3] step(r[P2@P5])
         5754  +**
         5755  +** Execute the step function for an aggregate.  The
         5756  +** function has P5 arguments.   P4 is a pointer to an sqlite3_context
         5757  +** object that is used to run the function.  Register P3 is
         5758  +** as the accumulator.
  5707   5759   **
  5708   5760   ** The P5 arguments are taken from register P2 and its
  5709   5761   ** successors.
         5762  +**
         5763  +** This opcode is initially coded as OP_AggStep0.  On first evaluation,
         5764  +** the FuncDef stored in P4 is converted into an sqlite3_context and
         5765  +** the opcode is changed.  In this way, the initialization of the
         5766  +** sqlite3_context only happens once, instead of on each call to the
         5767  +** step function.
  5710   5768   */
  5711         -case OP_AggStep: {
         5769  +case OP_AggStep0: {
  5712   5770     int n;
  5713         -  int i;
  5714         -  Mem *pMem;
  5715         -  Mem *pRec;
  5716         -  Mem t;
  5717         -  sqlite3_context ctx;
  5718         -  sqlite3_value **apVal;
         5771  +  sqlite3_context *pCtx;
  5719   5772   
         5773  +  assert( pOp->p4type==P4_FUNCDEF );
  5720   5774     n = pOp->p5;
  5721         -  assert( n>=0 );
  5722         -  pRec = &aMem[pOp->p2];
  5723         -  apVal = p->apArg;
  5724         -  assert( apVal || n==0 );
  5725         -  for(i=0; i<n; i++, pRec++){
  5726         -    assert( memIsValid(pRec) );
  5727         -    apVal[i] = pRec;
  5728         -    memAboutToChange(p, pRec);
  5729         -  }
  5730         -  ctx.pFunc = pOp->p4.pFunc;
  5731   5775     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5732         -  ctx.pMem = pMem = &aMem[pOp->p3];
         5776  +  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
         5777  +  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
         5778  +  pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
         5779  +  if( pCtx==0 ) goto no_mem;
         5780  +  pCtx->pMem = 0;
         5781  +  pCtx->pFunc = pOp->p4.pFunc;
         5782  +  pCtx->iOp = (int)(pOp - aOp);
         5783  +  pCtx->pVdbe = p;
         5784  +  pCtx->argc = n;
         5785  +  pOp->p4type = P4_FUNCCTX;
         5786  +  pOp->p4.pCtx = pCtx;
         5787  +  pOp->opcode = OP_AggStep;
         5788  +  /* Fall through into OP_AggStep */
         5789  +}
         5790  +case OP_AggStep: {
         5791  +  int i;
         5792  +  sqlite3_context *pCtx;
         5793  +  Mem *pMem;
         5794  +  Mem t;
         5795  +
         5796  +  assert( pOp->p4type==P4_FUNCCTX );
         5797  +  pCtx = pOp->p4.pCtx;
         5798  +  pMem = &aMem[pOp->p3];
         5799  +
         5800  +  /* If this function is inside of a trigger, the register array in aMem[]
         5801  +  ** might change from one evaluation to the next.  The next block of code
         5802  +  ** checks to see if the register array has changed, and if so it
         5803  +  ** reinitializes the relavant parts of the sqlite3_context object */
         5804  +  if( pCtx->pMem != pMem ){
         5805  +    pCtx->pMem = pMem;
         5806  +    for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
         5807  +  }
         5808  +
         5809  +#ifdef SQLITE_DEBUG
         5810  +  for(i=0; i<pCtx->argc; i++){
         5811  +    assert( memIsValid(pCtx->argv[i]) );
         5812  +    REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
         5813  +  }
         5814  +#endif
         5815  +
  5733   5816     pMem->n++;
  5734   5817     sqlite3VdbeMemInit(&t, db, MEM_Null);
  5735         -  ctx.pOut = &t;
  5736         -  ctx.isError = 0;
  5737         -  ctx.pVdbe = p;
  5738         -  ctx.iOp = (int)(pOp - aOp);
  5739         -  ctx.skipFlag = 0;
  5740         -  (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  5741         -  if( ctx.isError ){
  5742         -    sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
  5743         -    rc = ctx.isError;
         5818  +  pCtx->pOut = &t;
         5819  +  pCtx->fErrorOrAux = 0;
         5820  +  pCtx->skipFlag = 0;
         5821  +  (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
         5822  +  if( pCtx->fErrorOrAux ){
         5823  +    if( pCtx->isError ){
         5824  +      sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
         5825  +      rc = pCtx->isError;
         5826  +    }
         5827  +    sqlite3VdbeMemRelease(&t);
         5828  +  }else{
         5829  +    assert( t.flags==MEM_Null );
  5744   5830     }
  5745         -  if( ctx.skipFlag ){
         5831  +  if( pCtx->skipFlag ){
  5746   5832       assert( pOp[-1].opcode==OP_CollSeq );
  5747   5833       i = pOp[-1].p1;
  5748   5834       if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
  5749   5835     }
  5750         -  sqlite3VdbeMemRelease(&t);
  5751   5836     break;
  5752   5837   }
  5753   5838   
  5754   5839   /* Opcode: AggFinal P1 P2 * P4 *
  5755   5840   ** Synopsis: accum=r[P1] N=P2
  5756   5841   **
  5757   5842   ** Execute the finalizer function for an aggregate.  P1 is

Changes to src/vdbe.h.

    42     42     u8 opcode;          /* What operation to perform */
    43     43     signed char p4type; /* One of the P4_xxx constants for p4 */
    44     44     u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
    45     45     u8 p5;              /* Fifth parameter is an unsigned character */
    46     46     int p1;             /* First operand */
    47     47     int p2;             /* Second parameter (often the jump destination) */
    48     48     int p3;             /* The third parameter */
    49         -  union {             /* fourth parameter */
           49  +  union p4union {     /* fourth parameter */
    50     50       int i;                 /* Integer value if p4type==P4_INT32 */
    51     51       void *p;               /* Generic pointer */
    52     52       char *z;               /* Pointer to data for string (char array) types */
    53     53       i64 *pI64;             /* Used when p4type is P4_INT64 */
    54     54       double *pReal;         /* Used when p4type is P4_REAL */
    55     55       FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
           56  +    sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
    56     57       CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
    57     58       Mem *pMem;             /* Used when p4type is P4_MEM */
    58     59       VTable *pVtab;         /* Used when p4type is P4_VTAB */
    59     60       KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
    60     61       int *ai;               /* Used when p4type is P4_INTARRAY */
    61     62       SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
    62     63       int (*xAdvance)(BtCursor *, int *);
................................................................................
   115    116   #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
   116    117   #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
   117    118   #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
   118    119   #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
   119    120   #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
   120    121   #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
   121    122   #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
          123  +#define P4_FUNCCTX  (-20) /* P4 is a pointer to an sqlite3_context object */
   122    124   
   123    125   /* Error message codes for OP_Halt */
   124    126   #define P5_ConstraintNotNull 1
   125    127   #define P5_ConstraintUnique  2
   126    128   #define P5_ConstraintCheck   3
   127    129   #define P5_ConstraintFK      4
   128    130   

Changes to src/vdbeInt.h.

   275    275   ** But this file is the only place where the internal details of this
   276    276   ** structure are known.
   277    277   **
   278    278   ** This structure is defined inside of vdbeInt.h because it uses substructures
   279    279   ** (Mem) which are only defined there.
   280    280   */
   281    281   struct sqlite3_context {
   282         -  Mem *pOut;            /* The return value is stored here */
   283         -  FuncDef *pFunc;       /* Pointer to function information */
   284         -  Mem *pMem;            /* Memory cell used to store aggregate context */
   285         -  Vdbe *pVdbe;          /* The VM that owns this context */
   286         -  int iOp;              /* Instruction number of OP_Function */
   287         -  int isError;          /* Error code returned by the function. */
   288         -  u8 skipFlag;          /* Skip accumulator loading if true */
   289         -  u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */
          282  +  Mem *pOut;              /* The return value is stored here */
          283  +  FuncDef *pFunc;         /* Pointer to function information */
          284  +  Mem *pMem;              /* Memory cell used to store aggregate context */
          285  +  Vdbe *pVdbe;            /* The VM that owns this context */
          286  +  int iOp;                /* Instruction number of OP_Function */
          287  +  int isError;            /* Error code returned by the function. */
          288  +  u8 skipFlag;            /* Skip accumulator loading if true */
          289  +  u8 fErrorOrAux;         /* isError!=0 or pVdbe->pAuxData modified */
          290  +  u8 argc;                /* Number of arguments */
          291  +  sqlite3_value *argv[1]; /* Argument set */
   290    292   };
   291    293   
   292    294   /*
   293    295   ** An Explain object accumulates indented output which is helpful
   294    296   ** in describing recursive data structures.
   295    297   */
   296    298   struct Explain {

Changes to src/vdbeaux.c.

   485    485     p->bIsReader = 0;
   486    486     for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
   487    487       u8 opcode = pOp->opcode;
   488    488   
   489    489       /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
   490    490       ** cases from this switch! */
   491    491       switch( opcode ){
   492         -      case OP_Function:
   493         -      case OP_AggStep: {
   494         -        if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
   495         -        break;
   496         -      }
   497    492         case OP_Transaction: {
   498    493           if( pOp->p2!=0 ) p->readOnly = 0;
   499    494           /* fall thru */
   500    495         }
   501    496         case OP_AutoCommit:
   502    497         case OP_Savepoint: {
   503    498           p->bIsReader = 1;
................................................................................
   733    728   /*
   734    729   ** Delete a P4 value if necessary.
   735    730   */
   736    731   static void freeP4(sqlite3 *db, int p4type, void *p4){
   737    732     if( p4 ){
   738    733       assert( db );
   739    734       switch( p4type ){
          735  +      case P4_FUNCCTX: {
          736  +        freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
          737  +        /* Fall through into the next case */
          738  +      }
   740    739         case P4_REAL:
   741    740         case P4_INT64:
   742    741         case P4_DYNAMIC:
   743    742         case P4_INTARRAY: {
   744    743           sqlite3DbFree(db, p4);
   745    744           break;
   746    745         }
................................................................................
  1117   1116         break;
  1118   1117       }
  1119   1118       case P4_FUNCDEF: {
  1120   1119         FuncDef *pDef = pOp->p4.pFunc;
  1121   1120         sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
  1122   1121         break;
  1123   1122       }
         1123  +#ifdef SQLITE_DEBUG
         1124  +    case P4_FUNCCTX: {
         1125  +      FuncDef *pDef = pOp->p4.pCtx->pFunc;
         1126  +      sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
         1127  +      break;
         1128  +    }
         1129  +#endif
  1124   1130       case P4_INT64: {
  1125   1131         sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
  1126   1132         break;
  1127   1133       }
  1128   1134       case P4_INT32: {
  1129   1135         sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
  1130   1136         break;

Changes to src/vdbemem.c.

  1690   1690   void sqlite3ValueFree(sqlite3_value *v){
  1691   1691     if( !v ) return;
  1692   1692     sqlite3VdbeMemRelease((Mem *)v);
  1693   1693     sqlite3DbFree(((Mem*)v)->db, v);
  1694   1694   }
  1695   1695   
  1696   1696   /*
  1697         -** Return the number of bytes in the sqlite3_value object assuming
  1698         -** that it uses the encoding "enc"
         1697  +** The sqlite3ValueBytes() routine returns the number of bytes in the
         1698  +** sqlite3_value object assuming that it uses the encoding "enc".
         1699  +** The valueBytes() routine is a helper function.
  1699   1700   */
         1701  +static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
         1702  +  return valueToText(pVal, enc)!=0 ? pVal->n : 0;
         1703  +}
  1700   1704   int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
  1701   1705     Mem *p = (Mem*)pVal;
  1702         -  if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
         1706  +  assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
         1707  +  if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
         1708  +    return p->n;
         1709  +  }
         1710  +  if( (p->flags & MEM_Blob)!=0 ){
  1703   1711       if( p->flags & MEM_Zero ){
  1704   1712         return p->n + p->u.nZero;
  1705   1713       }else{
  1706   1714         return p->n;
  1707   1715       }
  1708   1716     }
  1709         -  return 0;
         1717  +  if( p->flags & MEM_Null ) return 0;
         1718  +  return valueBytes(pVal, enc);
  1710   1719   }