Use OP_SorterColumns in aggregate queries.  Remove OPFLAG_CLEARCACHE.

FossilOrigin-Name: 134e65c07f88218a9f9f0b526695a7121df31e68
diff --git a/manifest b/manifest
index c1d4cf9..0167692 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sthe\sOP_SorterColumns\sopcode\s-\san\sexperiment\sin\susing\sa\sspecial\scase\nopcode\sto\sdecode\sthe\sSorter\soutput\srather\sthan\sthe\sgeneric\sOP_Column.\s\sThis\nmight\sbe\sfaster.\s\sAnd\swith\sfurther\swork,\sit\scould\seventually\seliminate\sthe\nneed\sfor\sOP_OpenPseudo.
-D 2014-10-13T01:23:51.730
+C Use\sOP_SorterColumns\sin\saggregate\squeries.\s\sRemove\sOPFLAG_CLEARCACHE.
+D 2014-10-13T12:30:17.749
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in cf57f673d77606ab0f2d9627ca52a9ba1464146a
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -227,12 +227,12 @@
 F src/random.c d10c1f85b6709ca97278428fd5db5bbb9c74eece
 F src/resolve.c a3466128b52a86c466e47ac1a19e2174f7b5cf89
 F src/rowset.c eccf6af6d620aaa4579bd3b72c1b6395d9e9fa1e
-F src/select.c d32a3d223c4c0981998d69570fe095d0e563b7f6
+F src/select.c 506c2ff4118892858ccdf02d4c5f282371e6c374
 F src/shell.c 18ee8bbe9502d8848072dc2eddd1ea09254ba494
 F src/sqlite.h.in 4a5e5158c189d2bcd45c7c4607c2c0eb6d25c153
 F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
 F src/sqlite3ext.h 17d487c3c91b0b8c584a32fbeb393f6f795eea7d
-F src/sqliteInt.h c417a25e2369f705b651897a2f1cc8da0e6aa1c4
+F src/sqliteInt.h f7812f74f2d0c6041ef6b91a99c5a45f775dd408
 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
 F src/status.c 961d5926e5a8fda611d385ec22c226b8635cd1cb
 F src/table.c 2e99ef7ef16187e17033d9398dc962ce22dab5cb
@@ -289,7 +289,7 @@
 F src/utf.c fc6b889ba0779b7722634cdeaa25f1930d93820c
 F src/util.c 4006c01772bd8d8ac4306d523bbcee41d3e392d8
 F src/vacuum.c 59f03f92bcff57faa6a8ca256eb29ccddfb0614a
-F src/vdbe.c 5ffd7b182590606bb7db22628caf76cf3fad918e
+F src/vdbe.c 124df37e5583c5057f144b35378afcebd2600ad5
 F src/vdbe.h 09f5b4e3719fa454f252322b1cdab5cf1f361327
 F src/vdbeInt.h e1173bd72b282633c2ec8f3a2f78b5117229f268
 F src/vdbeapi.c 37a6c6ae284a97bcace365f2f0a225680c0499d9
@@ -1204,10 +1204,7 @@
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 91384a7d727ef0f285cd430e829ba9f3852db50e
-R 34c56059a01c319cb8bc18ad3acf3cfb
-T *branch * OP_SorterColumns
-T *sym-OP_SorterColumns *
-T -sym-trunk *
+P b9c695e8859bb9972a9890bf2ccf341c1282ab77
+R eebfd0e526b75853928a9d4bc3401243
 U drh
-Z 13a10a4b7865e9465444cc45982ddc50
+Z 514d9bf63dadaa081007370f2b345ba2
diff --git a/manifest.uuid b/manifest.uuid
index d2c4891..90a86da 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-b9c695e8859bb9972a9890bf2ccf341c1282ab77
\ No newline at end of file
+134e65c07f88218a9f9f0b526695a7121df31e68
\ No newline at end of file
diff --git a/src/select.c b/src/select.c
index 8d03c85..2f4bf59 100644
--- a/src/select.c
+++ b/src/select.c
@@ -5142,12 +5142,11 @@
       sqlite3ExprCacheClear(pParse);
       if( groupBySort ){
         sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
-      }
-      for(j=0; j<pGroupBy->nExpr; j++){
-        if( groupBySort ){
-          sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
-          if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
-        }else{
+        j = pGroupBy->nExpr;
+        sqlite3VdbeAddOp4Int(v, OP_SorterColumns, 0, j, iBMem, sortOut);
+        sqlite3VdbeAddOp1(v, OP_NullRow, sortPTab);
+      }else{
+        for(j=0; j<pGroupBy->nExpr; j++){
           sAggInfo.directMode = 1;
           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
         }
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index e648430..cba89b0 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -2666,7 +2666,6 @@
 #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
-#define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
 #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
 #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
diff --git a/src/vdbe.c b/src/vdbe.c
index 4afde4c..7e68906 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -2239,11 +2239,6 @@
 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
 ** the result.
 **
-** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
-** then the cache of the cursor is reset prior to extracting the column.
-** The first OP_Column against a pseudo-table after the value of the content
-** register has changed should have this bit set.
-**
 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
 ** the result is guaranteed to only be used as the argument of a length()
 ** or typeof() function, respectively.  The loading of large blobs can be
@@ -2288,8 +2283,7 @@
   /* If the cursor cache is stale, bring it up-to-date */
   rc = sqlite3VdbeCursorMoveto(pC);
   if( rc ) goto abort_due_to_error;
-  assert( (pOp->p5&OPFLAG_CLEARCACHE)==0 || aOp[pc-1].opcode==OP_SorterData );
-  if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
+  if( pC->cacheStatus!=p->cacheCtr ){
     if( pC->nullRow ){
       if( pCrsr==0 ){
         assert( pC->pseudoTableReg>0 );
@@ -4304,6 +4298,7 @@
     if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pDest);
     pDest->enc = encoding;
+    Deephemeralize(pDest);
     REGISTER_TRACE((int)(pDest-aMem), pDest);
     pDest++;
   }while( pDest<=pLast );