Fix a long-standing issue with the distinct-as-aggregate optimization that
only expressed when the new collating-sequence logic is turned on.

FossilOrigin-Name: 0aaf52a339808386984c30cca0c0c35ac2e70e7e
diff --git a/manifest b/manifest
index dae7f8e..f33fbc7 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sthe\sprocessing\sof\sORDER\sBY\sclauses\swith\sCOLLATE\sterms\son\scompound\nqueries.\s\s52\sveryquick\stest\sfailures\sremain.
-D 2012-12-07T21:02:47.824
+C Fix\sa\slong-standing\sissue\swith\sthe\sdistinct-as-aggregate\soptimization\sthat\nonly\sexpressed\swhen\sthe\snew\scollating-sequence\slogic\sis\sturned\son.
+D 2012-12-07T22:18:54.026
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 690d441a758cbffd13e814dc2724a721a6ebd400
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -172,14 +172,14 @@
 F src/prepare.c 931ad0d852a0df48f79adcba6ce79ca5f475625c
 F src/printf.c 4a9f882f1c1787a8b494a2987765acf9d97ac21f
 F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
-F src/resolve.c 34168dc9c5e7b388d0ce209dac16069263ae7ed7
+F src/resolve.c 5ed606916be6c63f0e262cc2cb817712384f18b5
 F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
-F src/select.c eb3ded8d6ffcbab20dc3e65ba6a6dc13a01e7fbf
+F src/select.c 28176222b138f30a9bf25bdcec94f58980273f84
 F src/shell.c e392dd1ccbb77cc1d75a8367a89b473c24bea019
 F src/sqlite.h.in 4e71a210f383b6d060bd3fdf81d850f0f8c4eca3
 F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0
 F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477
-F src/sqliteInt.h 02e8c8d6f3001243fccbffc1cc484fb24b344ecc
+F src/sqliteInt.h d1f0866c69d94fe018a32f78c31a043e3fc0d0de
 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
 F src/status.c bedc37ec1a6bb9399944024d63f4c769971955a9
 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@ -237,7 +237,7 @@
 F src/utf.c 8d819e2e5104a430fc2005f018db14347c95a38f
 F src/util.c 0af2e515dc0dabacec931bca39525f6c3f1c5455
 F src/vacuum.c 2727bdd08847fcb6b2d2da6d14f018910e8645d3
-F src/vdbe.c 5b49cb1f852e4604a4fb6c47e087ddab28af193f
+F src/vdbe.c 328cb259ca182026ff63d78a005821339a17ba09
 F src/vdbe.h b52887278cb173e66188da84dfab216bea61119d
 F src/vdbeInt.h 79abf9b31be406d35ca77d6999cb2d42aaf91e78
 F src/vdbeapi.c 4c2418161cf45392ba76a7ca92f9a5f06b96f89c
@@ -1025,7 +1025,7 @@
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P f9fa7581c0371fe0feb4e97487741ade18595810
-R ffd541783cdf83fbb00a5f595a07c0f7
+P 49654453ad157693414c1f86fa3afa0918acffd4
+R 1c9591aed33110a685184b6893dbfa0d
 U drh
-Z c3f0cf345664d8221c8b35557e376692
+Z 2db21ba84371cf56d0d7dbac86085c64
diff --git a/manifest.uuid b/manifest.uuid
index 8884905..16ce20b 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-49654453ad157693414c1f86fa3afa0918acffd4
\ No newline at end of file
+0aaf52a339808386984c30cca0c0c35ac2e70e7e
\ No newline at end of file
diff --git a/src/resolve.c b/src/resolve.c
index 4b9d8fa..d5d9d1c 100644
--- a/src/resolve.c
+++ b/src/resolve.c
@@ -111,20 +111,9 @@
     }
     pDup->iTable = pEList->a[iCol].iAlias;
   }
-#if 1 /* FIXME */
-  if( pExpr->flags & EP_Collate ){
-    CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
-    if( pColl ){
-      pDup = sqlite3ExprAddCollateString(pParse, pDup, pColl->zName);
-    }
-    pDup->flags |= EP_Collate;
-  }
-#else
-  /* Should be this: */
   if( pExpr->op==TK_COLLATE ){
     pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
   }
-#endif
 
   /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
   ** prevents ExprDelete() from deleting the Expr structure itself,
diff --git a/src/select.c b/src/select.c
index 4582d2b..6a71286 100644
--- a/src/select.c
+++ b/src/select.c
@@ -2337,7 +2337,7 @@
           pColl = sqlite3ExprCollSeq(pParse, pTerm);
         }else{
           pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
-          pTerm->flags |= EP_Collate;
+//          pTerm->flags |= EP_Collate;
         }
         pKeyMerge->aColl[i] = pColl;
         pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
@@ -2540,6 +2540,7 @@
   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
   sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
                          (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
+  sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
 
   /* Release temporary registers
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index b15e0b2..2b58a80 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -2330,6 +2330,7 @@
 #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
 #define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
+#define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
 
 /*
  * Each trigger present in the database schema is stored as an instance of
diff --git a/src/vdbe.c b/src/vdbe.c
index bbccac8..6b78d27 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -422,7 +422,9 @@
 ** Print the value of a register for tracing purposes:
 */
 static void memTracePrint(FILE *out, Mem *p){
-  if( p->flags & MEM_Null ){
+  if( p->flags & MEM_Invalid ){
+    fprintf(out, " undefined");
+  }else if( p->flags & MEM_Null ){
     fprintf(out, " NULL");
   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
     fprintf(out, " si:%lld", p->u.i);
@@ -1077,6 +1079,9 @@
   while( 1 ){
     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
     Deephemeralize(pOut);
+#ifdef SQLITE_DEBUG
+    pOut->pScopyFrom = 0;
+#endif
     REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
     if( (n--)==0 ) break;
     pOut++;
@@ -1887,9 +1892,9 @@
 ** Set the permutation used by the OP_Compare operator to be the array
 ** of integers in P4.
 **
-** The permutation is only valid until the next OP_Permutation, OP_Compare,
-** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
-** immediately prior to the OP_Compare.
+** The permutation is only valid until the next OP_Compare that has
+** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
+** occur immediately prior to the OP_Compare.
 */
 case OP_Permutation: {
   assert( pOp->p4type==P4_INTARRAY );
@@ -1898,7 +1903,7 @@
   break;
 }
 
-/* Opcode: Compare P1 P2 P3 P4 *
+/* Opcode: Compare P1 P2 P3 P4 P5
 **
 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
@@ -1922,6 +1927,7 @@
   CollSeq *pColl;    /* Collating sequence to use on this term */
   int bRev;          /* True for DESCENDING sort order */
 
+  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
   n = pOp->p3;
   pKeyInfo = pOp->p4.pKeyInfo;
   assert( n>0 );