Fossil

Diff
Login

Differences From Artifact [e2e1c87b]:

To Artifact [955cc67a]:


   671    671   **
   672    672   ** See also: [sqlite3_libversion()],
   673    673   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   674    674   ** [sqlite_version()] and [sqlite_source_id()].
   675    675   */
   676    676   #define SQLITE_VERSION        "3.7.15"
   677    677   #define SQLITE_VERSION_NUMBER 3007015
   678         -#define SQLITE_SOURCE_ID      "2012-12-08 22:14:29 92c9ab56b1c67b9468bec57ab1d2c483a69a2810"
          678  +#define SQLITE_SOURCE_ID      "2012-12-05 14:31:13 9f6c68856b694373b7ffb124abd996e519ba5921"
   679    679   
   680    680   /*
   681    681   ** CAPI3REF: Run-Time Library Version Numbers
   682    682   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   683    683   **
   684    684   ** These interfaces provide the same information as the [SQLITE_VERSION],
   685    685   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1419   1419   ** VFS has handled the PRAGMA itself and the parser generates a no-op
  1420   1420   ** prepared statement.  ^If the [SQLITE_FCNTL_PRAGMA] file control returns
  1421   1421   ** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
  1422   1422   ** that the VFS encountered an error while handling the [PRAGMA] and the
  1423   1423   ** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
  1424   1424   ** file control occurs at the beginning of pragma statement analysis and so
  1425   1425   ** it is able to override built-in [PRAGMA] statements.
         1426  +** </ul>
  1426   1427   **
  1427   1428   ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
  1428   1429   ** ^This file-control may be invoked by SQLite on the database file handle
  1429   1430   ** shortly after it is opened in order to provide a custom VFS with access
  1430   1431   ** to the connections busy-handler callback. The argument is of type (void **)
  1431   1432   ** - an array of two (void *) values. The first (void *) actually points
  1432   1433   ** to a function of type (int (*)(void *)). In order to invoke the connections
  1433   1434   ** busy-handler, this function should be invoked with the second (void *) in
  1434   1435   ** the array as the only argument. If it returns non-zero, then the operation
  1435   1436   ** should be retried. If it returns zero, the custom VFS should abandon the
  1436   1437   ** current operation.
  1437         -**
  1438         -** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
  1439         -** ^Application can invoke this file-control to have SQLite generate a
  1440         -** temporary filename using the same algorithm that is followed to generate
  1441         -** temporary filenames for TEMP tables and other internal uses.  The
  1442         -** argument should be a char** which will be filled with the filename
  1443         -** written into memory obtained from [sqlite3_malloc()].  The caller should
  1444         -** invoke [sqlite3_free()] on the result to avoid a memory leak.
  1445         -**
  1446         -** </ul>
  1447   1438   */
  1448   1439   #define SQLITE_FCNTL_LOCKSTATE               1
  1449   1440   #define SQLITE_GET_LOCKPROXYFILE             2
  1450   1441   #define SQLITE_SET_LOCKPROXYFILE             3
  1451   1442   #define SQLITE_LAST_ERRNO                    4
  1452   1443   #define SQLITE_FCNTL_SIZE_HINT               5
  1453   1444   #define SQLITE_FCNTL_CHUNK_SIZE              6
................................................................................
  1456   1447   #define SQLITE_FCNTL_WIN32_AV_RETRY          9
  1457   1448   #define SQLITE_FCNTL_PERSIST_WAL            10
  1458   1449   #define SQLITE_FCNTL_OVERWRITE              11
  1459   1450   #define SQLITE_FCNTL_VFSNAME                12
  1460   1451   #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
  1461   1452   #define SQLITE_FCNTL_PRAGMA                 14
  1462   1453   #define SQLITE_FCNTL_BUSYHANDLER            15
  1463         -#define SQLITE_FCNTL_TEMPFILENAME           16
  1464   1454   
  1465   1455   /*
  1466   1456   ** CAPI3REF: Mutex Handle
  1467   1457   **
  1468   1458   ** The mutex module within SQLite defines [sqlite3_mutex] to be an
  1469   1459   ** abstract type for a mutex object.  The SQLite core never looks
  1470   1460   ** at the internal representation of an [sqlite3_mutex].  It only
................................................................................
 10217  10207   #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 10218  10208   
 10219  10209   /*
 10220  10210   ** A "Collating Sequence" is defined by an instance of the following
 10221  10211   ** structure. Conceptually, a collating sequence consists of a name and
 10222  10212   ** a comparison routine that defines the order of that sequence.
 10223  10213   **
 10224         -** If CollSeq.xCmp is NULL, it means that the
        10214  +** There may two separate implementations of the collation function, one
        10215  +** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
        10216  +** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
        10217  +** native byte order. When a collation sequence is invoked, SQLite selects
        10218  +** the version that will require the least expensive encoding
        10219  +** translations, if any.
        10220  +**
        10221  +** The CollSeq.pUser member variable is an extra parameter that passed in
        10222  +** as the first argument to the UTF-8 comparison function, xCmp.
        10223  +** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
        10224  +** xCmp16.
        10225  +**
        10226  +** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
 10225  10227   ** collating sequence is undefined.  Indices built on an undefined
 10226  10228   ** collating sequence may not be read or written.
 10227  10229   */
 10228  10230   struct CollSeq {
 10229  10231     char *zName;          /* Name of the collating sequence, UTF-8 encoded */
 10230  10232     u8 enc;               /* Text encoding handled by xCmp() */
 10231  10233     void *pUser;          /* First argument to xCmp() */
................................................................................
 10745  10747   
 10746  10748     Expr *pLeft;           /* Left subnode */
 10747  10749     Expr *pRight;          /* Right subnode */
 10748  10750     union {
 10749  10751       ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
 10750  10752       Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
 10751  10753     } x;
        10754  +  CollSeq *pColl;        /* The collation type of the column or 0 */
 10752  10755   
 10753  10756     /* If the EP_Reduced flag is set in the Expr.flags mask, then no
 10754  10757     ** space is allocated for the fields below this point. An attempt to
 10755  10758     ** access them will result in a segfault or malfunction.
 10756  10759     *********************************************************************/
 10757  10760   
 10758  10761   #if SQLITE_MAX_EXPR_DEPTH>0
................................................................................
 10780  10783   #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
 10781  10784   #define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
 10782  10785   #define EP_Error      0x0008  /* Expression contains one or more errors */
 10783  10786   #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
 10784  10787   #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
 10785  10788   #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
 10786  10789   #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
 10787         -#define EP_Collate    0x0100  /* Tree contains a TK_COLLATE opeartor */
        10790  +#define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
 10788  10791   #define EP_FixedDest  0x0200  /* Result needed in a specific register */
 10789  10792   #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
 10790  10793   #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
 10791  10794   #define EP_Hint       0x1000  /* Not used */
 10792  10795   #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
 10793  10796   #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
 10794  10797   #define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
................................................................................
 11397  11400   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 11398  11401   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 11399  11402   #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
 11400  11403   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
 11401  11404   #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 11402  11405   #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
 11403  11406   #define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
 11404         -#define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
 11405  11407   
 11406  11408   /*
 11407  11409    * Each trigger present in the database schema is stored as an instance of
 11408  11410    * struct Trigger. 
 11409  11411    *
 11410  11412    * Pointers to instances of struct Trigger are stored in two ways.
 11411  11413    * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
................................................................................
 12089  12091   SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 12090  12092   SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 12091  12093   SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 12092  12094   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 12093  12095   SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 12094  12096   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 12095  12097   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 12096         -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
 12097         -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 12098         -SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
        12098  +SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
        12099  +SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
 12099  12100   SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
 12100  12101   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 12101  12102   SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 12102  12103   SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
 12103  12104   SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
 12104  12105   SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
 12105  12106   SQLITE_PRIVATE int sqlite3AbsInt32(int);
................................................................................
 26453  26454     }else if( (*pArg)==0 ){
 26454  26455       pFile->ctrlFlags &= ~mask;
 26455  26456     }else{
 26456  26457       pFile->ctrlFlags |= mask;
 26457  26458     }
 26458  26459   }
 26459  26460   
 26460         -/* Forward declaration */
 26461         -static int unixGetTempname(int nBuf, char *zBuf);
 26462         -
 26463  26461   /*
 26464  26462   ** Information and control of an open file handle.
 26465  26463   */
 26466  26464   static int unixFileControl(sqlite3_file *id, int op, void *pArg){
 26467  26465     unixFile *pFile = (unixFile*)id;
 26468  26466     switch( op ){
 26469  26467       case SQLITE_FCNTL_LOCKSTATE: {
................................................................................
 26491  26489       }
 26492  26490       case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
 26493  26491         unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
 26494  26492         return SQLITE_OK;
 26495  26493       }
 26496  26494       case SQLITE_FCNTL_VFSNAME: {
 26497  26495         *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
 26498         -      return SQLITE_OK;
 26499         -    }
 26500         -    case SQLITE_FCNTL_TEMPFILENAME: {
 26501         -      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
 26502         -      if( zTFile ){
 26503         -        unixGetTempname(pFile->pVfs->mxPathname, zTFile);
 26504         -        *(char**)pArg = zTFile;
 26505         -      }
 26506  26496         return SQLITE_OK;
 26507  26497       }
 26508  26498   #ifdef SQLITE_DEBUG
 26509  26499       /* The pager calls this method to signal that it has done
 26510  26500       ** a rollback and that the database is therefore unchanged and
 26511  26501       ** it hence it is OK for the transaction change counter to be
 26512  26502       ** unchanged.
................................................................................
 32790  32780     }else if( (*pArg)==0 ){
 32791  32781       pFile->ctrlFlags &= ~mask;
 32792  32782     }else{
 32793  32783       pFile->ctrlFlags |= mask;
 32794  32784     }
 32795  32785   }
 32796  32786   
 32797         -/* Forward declaration */
 32798         -static int getTempname(int nBuf, char *zBuf);
 32799         -
 32800  32787   /*
 32801  32788   ** Control and query of the open file handle.
 32802  32789   */
 32803  32790   static int winFileControl(sqlite3_file *id, int op, void *pArg){
 32804  32791     winFile *pFile = (winFile*)id;
 32805  32792     switch( op ){
 32806  32793       case SQLITE_FCNTL_LOCKSTATE: {
................................................................................
 32851  32838           a[0] = win32IoerrRetry;
 32852  32839         }
 32853  32840         if( a[1]>0 ){
 32854  32841           win32IoerrRetryDelay = a[1];
 32855  32842         }else{
 32856  32843           a[1] = win32IoerrRetryDelay;
 32857  32844         }
 32858         -      return SQLITE_OK;
 32859         -    }
 32860         -    case SQLITE_FCNTL_TEMPFILENAME: {
 32861         -      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
 32862         -      if( zTFile ){
 32863         -        getTempname(pFile->pVfs->mxPathname, zTFile);
 32864         -        *(char**)pArg = zTFile;
 32865         -      }
 32866  32845         return SQLITE_OK;
 32867  32846       }
 32868  32847     }
 32869  32848     return SQLITE_NOTFOUND;
 32870  32849   }
 32871  32850   
 32872  32851   /*
................................................................................
 59411  59390         int i, j;
 59412  59391         KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
 59413  59392         assert( pKeyInfo->aSortOrder!=0 );
 59414  59393         sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
 59415  59394         i = sqlite3Strlen30(zTemp);
 59416  59395         for(j=0; j<pKeyInfo->nField; j++){
 59417  59396           CollSeq *pColl = pKeyInfo->aColl[j];
 59418         -        const char *zColl = pColl ? pColl->zName : "nil";
 59419         -        int n = sqlite3Strlen30(zColl);
 59420         -        if( i+n>nTemp-6 ){
 59421         -          memcpy(&zTemp[i],",...",4);
 59422         -          break;
        59397  +        if( pColl ){
        59398  +          int n = sqlite3Strlen30(pColl->zName);
        59399  +          if( i+n>nTemp-6 ){
        59400  +            memcpy(&zTemp[i],",...",4);
        59401  +            break;
        59402  +          }
        59403  +          zTemp[i++] = ',';
        59404  +          if( pKeyInfo->aSortOrder[j] ){
        59405  +            zTemp[i++] = '-';
        59406  +          }
        59407  +          memcpy(&zTemp[i], pColl->zName,n+1);
        59408  +          i += n;
        59409  +        }else if( i+4<nTemp-6 ){
        59410  +          memcpy(&zTemp[i],",nil",4);
        59411  +          i += 4;
 59423  59412           }
 59424         -        zTemp[i++] = ',';
 59425         -        if( pKeyInfo->aSortOrder[j] ){
 59426         -          zTemp[i++] = '-';
 59427         -        }
 59428         -        memcpy(&zTemp[i], zColl, n+1);
 59429         -        i += n;
 59430  59413         }
 59431  59414         zTemp[i++] = ')';
 59432  59415         zTemp[i] = 0;
 59433  59416         assert( i<nTemp );
 59434  59417         break;
 59435  59418       }
 59436  59419       case P4_COLLSEQ: {
................................................................................
 63812  63795   #endif
 63813  63796   
 63814  63797   #ifdef SQLITE_DEBUG
 63815  63798   /*
 63816  63799   ** Print the value of a register for tracing purposes:
 63817  63800   */
 63818  63801   static void memTracePrint(FILE *out, Mem *p){
 63819         -  if( p->flags & MEM_Invalid ){
 63820         -    fprintf(out, " undefined");
 63821         -  }else if( p->flags & MEM_Null ){
        63802  +  if( p->flags & MEM_Null ){
 63822  63803       fprintf(out, " NULL");
 63823  63804     }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
 63824  63805       fprintf(out, " si:%lld", p->u.i);
 63825  63806     }else if( p->flags & MEM_Int ){
 63826  63807       fprintf(out, " i:%lld", p->u.i);
 63827  63808   #ifndef SQLITE_OMIT_FLOATING_POINT
 63828  63809     }else if( p->flags & MEM_Real ){
................................................................................
 64996  64977     u.ae.n = pOp->p3;
 64997  64978     pIn1 = &aMem[pOp->p1];
 64998  64979     pOut = &aMem[pOp->p2];
 64999  64980     assert( pOut!=pIn1 );
 65000  64981     while( 1 ){
 65001  64982       sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
 65002  64983       Deephemeralize(pOut);
 65003         -#ifdef SQLITE_DEBUG
 65004         -    pOut->pScopyFrom = 0;
 65005         -#endif
 65006  64984       REGISTER_TRACE(pOp->p2+pOp->p3-u.ae.n, pOut);
 65007  64985       if( (u.ae.n--)==0 ) break;
 65008  64986       pOut++;
 65009  64987       pIn1++;
 65010  64988     }
 65011  64989     break;
 65012  64990   }
................................................................................
 65821  65799   }
 65822  65800   
 65823  65801   /* Opcode: Permutation * * * P4 *
 65824  65802   **
 65825  65803   ** Set the permutation used by the OP_Compare operator to be the array
 65826  65804   ** of integers in P4.
 65827  65805   **
 65828         -** The permutation is only valid until the next OP_Compare that has
 65829         -** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
 65830         -** occur immediately prior to the OP_Compare.
        65806  +** The permutation is only valid until the next OP_Permutation, OP_Compare,
        65807  +** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
        65808  +** immediately prior to the OP_Compare.
 65831  65809   */
 65832  65810   case OP_Permutation: {
 65833  65811     assert( pOp->p4type==P4_INTARRAY );
 65834  65812     assert( pOp->p4.ai );
 65835  65813     aPermute = pOp->p4.ai;
 65836  65814     break;
 65837  65815   }
 65838  65816   
 65839         -/* Opcode: Compare P1 P2 P3 P4 P5
        65817  +/* Opcode: Compare P1 P2 P3 P4 *
 65840  65818   **
 65841  65819   ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
 65842  65820   ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
 65843  65821   ** the comparison for use by the next OP_Jump instruct.
 65844  65822   **
 65845         -** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
 65846         -** determined by the most recent OP_Permutation operator.  If the
 65847         -** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
 65848         -** order.
 65849         -**
 65850  65823   ** P4 is a KeyInfo structure that defines collating sequences and sort
 65851  65824   ** orders for the comparison.  The permutation applies to registers
 65852  65825   ** only.  The KeyInfo elements are used sequentially.
 65853  65826   **
 65854  65827   ** The comparison is a sort comparison, so NULLs compare equal,
 65855  65828   ** NULLs are less than numbers, numbers are less than strings,
 65856  65829   ** and strings are less than blobs.
................................................................................
 65863  65836     int p2;
 65864  65837     const KeyInfo *pKeyInfo;
 65865  65838     int idx;
 65866  65839     CollSeq *pColl;    /* Collating sequence to use on this term */
 65867  65840     int bRev;          /* True for DESCENDING sort order */
 65868  65841   #endif /* local variables moved into u.al */
 65869  65842   
 65870         -  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
 65871  65843     u.al.n = pOp->p3;
 65872  65844     u.al.pKeyInfo = pOp->p4.pKeyInfo;
 65873  65845     assert( u.al.n>0 );
 65874  65846     assert( u.al.pKeyInfo!=0 );
 65875  65847     u.al.p1 = pOp->p1;
 65876  65848     u.al.p2 = pOp->p2;
 65877  65849   #if SQLITE_DEBUG
................................................................................
 72550  72522   **
 72551  72523   **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
 72552  72524   **
 72553  72525   ** The result of random()%5 in the GROUP BY clause is probably different
 72554  72526   ** from the result in the result-set.  We might fix this someday.  Or
 72555  72527   ** then again, we might not...
 72556  72528   **
 72557         -** If the reference is followed by a COLLATE operator, then make sure
 72558         -** the COLLATE operator is preserved.  For example:
 72559         -**
 72560         -**     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
 72561         -**
 72562         -** Should be transformed into:
 72563         -**
 72564         -**     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
 72565         -**
 72566  72529   ** The nSubquery parameter specifies how many levels of subquery the
 72567  72530   ** alias is removed from the original expression.  The usually value is
 72568  72531   ** zero but it might be more if the alias is contained within a subquery
 72569  72532   ** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
 72570  72533   ** structures must be increased by the nSubquery amount.
 72571  72534   */
 72572  72535   static void resolveAlias(
................................................................................
 72582  72545     sqlite3 *db;           /* The database connection */
 72583  72546   
 72584  72547     assert( iCol>=0 && iCol<pEList->nExpr );
 72585  72548     pOrig = pEList->a[iCol].pExpr;
 72586  72549     assert( pOrig!=0 );
 72587  72550     assert( pOrig->flags & EP_Resolved );
 72588  72551     db = pParse->db;
 72589         -  pDup = sqlite3ExprDup(db, pOrig, 0);
 72590         -  if( pDup==0 ) return;
 72591  72552     if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
        72553  +    pDup = sqlite3ExprDup(db, pOrig, 0);
 72592  72554       incrAggFunctionDepth(pDup, nSubquery);
 72593  72555       pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
 72594  72556       if( pDup==0 ) return;
 72595  72557       if( pEList->a[iCol].iAlias==0 ){
 72596  72558         pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
 72597  72559       }
 72598  72560       pDup->iTable = pEList->a[iCol].iAlias;
        72561  +  }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
        72562  +    pDup = sqlite3ExprDup(db, pOrig, 0);
        72563  +    if( pDup==0 ) return;
        72564  +  }else{
        72565  +    char *zToken = pOrig->u.zToken;
        72566  +    assert( zToken!=0 );
        72567  +    pOrig->u.zToken = 0;
        72568  +    pDup = sqlite3ExprDup(db, pOrig, 0);
        72569  +    pOrig->u.zToken = zToken;
        72570  +    if( pDup==0 ) return;
        72571  +    assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
        72572  +    pDup->flags2 |= EP2_MallocedToken;
        72573  +    pDup->u.zToken = sqlite3DbStrDup(db, zToken);
 72599  72574     }
 72600         -  if( pExpr->op==TK_COLLATE ){
 72601         -    pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
        72575  +  if( pExpr->flags & EP_ExpCollate ){
        72576  +    pDup->pColl = pExpr->pColl;
        72577  +    pDup->flags |= EP_ExpCollate;
 72602  72578     }
 72603  72579   
 72604  72580     /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
 72605  72581     ** prevents ExprDelete() from deleting the Expr structure itself,
 72606  72582     ** allowing it to be repopulated by the memcpy() on the following line.
 72607         -  ** The pExpr->u.zToken might point into memory that will be freed by the
 72608         -  ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
 72609         -  ** make a copy of the token before doing the sqlite3DbFree().
 72610  72583     */
 72611  72584     ExprSetProperty(pExpr, EP_Static);
 72612  72585     sqlite3ExprDelete(db, pExpr);
 72613  72586     memcpy(pExpr, pDup, sizeof(*pExpr));
 72614         -  if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
 72615         -    assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
 72616         -    pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
 72617         -    pExpr->flags2 |= EP2_MallocedToken;
 72618         -  }
 72619  72587     sqlite3DbFree(db, pDup);
 72620  72588   }
 72621  72589   
 72622  72590   
 72623  72591   /*
 72624  72592   ** Return TRUE if the name zCol occurs anywhere in the USING clause.
 72625  72593   **
................................................................................
 73298  73266       moreToDo = 0;
 73299  73267       pEList = pSelect->pEList;
 73300  73268       assert( pEList!=0 );
 73301  73269       for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 73302  73270         int iCol = -1;
 73303  73271         Expr *pE, *pDup;
 73304  73272         if( pItem->done ) continue;
 73305         -      pE = sqlite3ExprSkipCollate(pItem->pExpr);
        73273  +      pE = pItem->pExpr;
 73306  73274         if( sqlite3ExprIsInteger(pE, &iCol) ){
 73307  73275           if( iCol<=0 || iCol>pEList->nExpr ){
 73308  73276             resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
 73309  73277             return 1;
 73310  73278           }
 73311  73279         }else{
 73312  73280           iCol = resolveAsName(pParse, pEList, pE);
................................................................................
 73316  73284               assert(pDup);
 73317  73285               iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
 73318  73286             }
 73319  73287             sqlite3ExprDelete(db, pDup);
 73320  73288           }
 73321  73289         }
 73322  73290         if( iCol>0 ){
 73323         -        /* Convert the ORDER BY term into an integer column number iCol,
 73324         -        ** taking care to preserve the COLLATE clause if it exists */
 73325         -        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 73326         -        if( pNew==0 ) return 1;
 73327         -        pNew->flags |= EP_IntValue;
 73328         -        pNew->u.iValue = iCol;
 73329         -        if( pItem->pExpr==pE ){
 73330         -          pItem->pExpr = pNew;
 73331         -        }else{
 73332         -          assert( pItem->pExpr->op==TK_COLLATE );
 73333         -          assert( pItem->pExpr->pLeft==pE );
 73334         -          pItem->pExpr->pLeft = pNew;
 73335         -        }
        73291  +        CollSeq *pColl = pE->pColl;
        73292  +        int flags = pE->flags & EP_ExpCollate;
 73336  73293           sqlite3ExprDelete(db, pE);
        73294  +        pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
        73295  +        if( pE==0 ) return 1;
        73296  +        pE->pColl = pColl;
        73297  +        pE->flags |= EP_IntValue | flags;
        73298  +        pE->u.iValue = iCol;
 73337  73299           pItem->iOrderByCol = (u16)iCol;
 73338  73300           pItem->done = 1;
 73339  73301         }else{
 73340  73302           moreToDo = 1;
 73341  73303         }
 73342  73304       }
 73343  73305       pSelect = pSelect->pNext;
................................................................................
 73434  73396         /* If an AS-name match is found, mark this ORDER BY column as being
 73435  73397         ** a copy of the iCol-th result-set column.  The subsequent call to
 73436  73398         ** sqlite3ResolveOrderGroupBy() will convert the expression to a
 73437  73399         ** copy of the iCol-th result-set expression. */
 73438  73400         pItem->iOrderByCol = (u16)iCol;
 73439  73401         continue;
 73440  73402       }
 73441         -    if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){
        73403  +    if( sqlite3ExprIsInteger(pE, &iCol) ){
 73442  73404         /* The ORDER BY term is an integer constant.  Again, set the column
 73443  73405         ** number so that sqlite3ResolveOrderGroupBy() will convert the
 73444  73406         ** order-by term to a copy of the result-set expression */
 73445         -      if( iCol<1 || iCol>0xffff ){
        73407  +      if( iCol<1 ){
 73446  73408           resolveOutOfRangeError(pParse, zType, i+1, nResult);
 73447  73409           return 1;
 73448  73410         }
 73449  73411         pItem->iOrderByCol = (u16)iCol;
 73450  73412         continue;
 73451  73413       }
 73452  73414   
................................................................................
 73792  73754   **
 73793  73755   ** CREATE TABLE t1(a);
 73794  73756   ** SELECT * FROM t1 WHERE a;
 73795  73757   ** SELECT a AS b FROM t1 WHERE b;
 73796  73758   ** SELECT * FROM t1 WHERE (select a from t1);
 73797  73759   */
 73798  73760   SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
 73799         -  int op;
 73800         -  pExpr = sqlite3ExprSkipCollate(pExpr);
 73801         -  op = pExpr->op;
        73761  +  int op = pExpr->op;
 73802  73762     if( op==TK_SELECT ){
 73803  73763       assert( pExpr->flags&EP_xIsSelect );
 73804  73764       return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
 73805  73765     }
 73806  73766   #ifndef SQLITE_OMIT_CAST
 73807  73767     if( op==TK_CAST ){
 73808  73768       assert( !ExprHasProperty(pExpr, EP_IntValue) );
................................................................................
 73819  73779       assert( pExpr->pTab && j<pExpr->pTab->nCol );
 73820  73780       return pExpr->pTab->aCol[j].affinity;
 73821  73781     }
 73822  73782     return pExpr->affinity;
 73823  73783   }
 73824  73784   
 73825  73785   /*
 73826         -** Set the collating sequence for expression pExpr to be the collating
 73827         -** sequence named by pToken.   Return a pointer to a new Expr node that
 73828         -** implements the COLLATE operator.
 73829         -**
 73830         -** If a memory allocation error occurs, that fact is recorded in pParse->db
 73831         -** and the pExpr parameter is returned unchanged.
        73786  +** Set the explicit collating sequence for an expression to the
        73787  +** collating sequence supplied in the second argument.
 73832  73788   */
 73833         -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
 73834         -  if( pCollName->n>0 ){
 73835         -    Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
 73836         -    if( pNew ){
 73837         -      pNew->pLeft = pExpr;
 73838         -      pNew->flags |= EP_Collate;
 73839         -      pExpr = pNew;
 73840         -    }
        73789  +SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
        73790  +  if( pExpr && pColl ){
        73791  +    pExpr->pColl = pColl;
        73792  +    pExpr->flags |= EP_ExpCollate;
 73841  73793     }
 73842  73794     return pExpr;
 73843  73795   }
 73844         -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
 73845         -  Token s;
 73846         -  assert( zC!=0 );
 73847         -  s.z = zC;
 73848         -  s.n = sqlite3Strlen30(s.z);
 73849         -  return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
 73850         -}
 73851  73796   
 73852  73797   /*
 73853         -** Skip over any TK_COLLATE and/or TK_AS operators at the root of
 73854         -** an expression.
        73798  +** Set the collating sequence for expression pExpr to be the collating
        73799  +** sequence named by pToken.   Return a pointer to the revised expression.
        73800  +** The collating sequence is marked as "explicit" using the EP_ExpCollate
        73801  +** flag.  An explicit collating sequence will override implicit
        73802  +** collating sequences.
 73855  73803   */
 73856         -SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
 73857         -  while( pExpr && (pExpr->op==TK_COLLATE || pExpr->op==TK_AS) ){
 73858         -    pExpr = pExpr->pLeft;
 73859         -  }
        73804  +SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
        73805  +  char *zColl = 0;            /* Dequoted name of collation sequence */
        73806  +  CollSeq *pColl;
        73807  +  sqlite3 *db = pParse->db;
        73808  +  zColl = sqlite3NameFromToken(db, pCollName);
        73809  +  pColl = sqlite3LocateCollSeq(pParse, zColl);
        73810  +  sqlite3ExprSetColl(pExpr, pColl);
        73811  +  sqlite3DbFree(db, zColl);
 73860  73812     return pExpr;
 73861  73813   }
 73862  73814   
 73863  73815   /*
 73864         -** Return the collation sequence for the expression pExpr. If
 73865         -** there is no defined collating sequence, return NULL.
 73866         -**
 73867         -** The collating sequence might be determined by a COLLATE operator
 73868         -** or by the presence of a column with a defined collating sequence.
 73869         -** COLLATE operators take first precedence.  Left operands take
 73870         -** precedence over right operands.
        73816  +** Return the default collation sequence for the expression pExpr. If
        73817  +** there is no default collation type, return 0.
 73871  73818   */
 73872  73819   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
 73873         -  sqlite3 *db = pParse->db;
 73874  73820     CollSeq *pColl = 0;
 73875  73821     Expr *p = pExpr;
 73876  73822     while( p ){
 73877         -    int op = p->op;
 73878         -    if( op==TK_CAST || op==TK_UPLUS ){
 73879         -      p = p->pLeft;
 73880         -      continue;
 73881         -    }
 73882         -    assert( op!=TK_REGISTER || p->op2!=TK_COLLATE );
 73883         -    if( op==TK_COLLATE ){
 73884         -      if( db->init.busy ){
 73885         -        /* Do not report errors when parsing while the schema */
 73886         -        pColl = sqlite3FindCollSeq(db, ENC(db), p->u.zToken, 0);
 73887         -      }else{
 73888         -        pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
 73889         -      }
 73890         -      break;
 73891         -    }
 73892         -    if( p->pTab!=0
 73893         -     && (op==TK_AGG_COLUMN || op==TK_COLUMN
 73894         -          || op==TK_REGISTER || op==TK_TRIGGER)
 73895         -    ){
        73823  +    int op;
        73824  +    pColl = p->pColl;
        73825  +    if( pColl ) break;
        73826  +    op = p->op;
        73827  +    if( p->pTab!=0 && (
        73828  +        op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
        73829  +    )){
 73896  73830         /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
 73897  73831         ** a TK_COLUMN but was previously evaluated and cached in a register */
        73832  +      const char *zColl;
 73898  73833         int j = p->iColumn;
 73899  73834         if( j>=0 ){
 73900         -        const char *zColl = p->pTab->aCol[j].zColl;
        73835  +        sqlite3 *db = pParse->db;
        73836  +        zColl = p->pTab->aCol[j].zColl;
 73901  73837           pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
        73838  +        pExpr->pColl = pColl;
 73902  73839         }
 73903  73840         break;
 73904  73841       }
 73905         -    if( p->flags & EP_Collate ){
 73906         -      if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
 73907         -        p = p->pLeft;
 73908         -      }else{
 73909         -        p = p->pRight;
 73910         -      }
 73911         -    }else{
        73842  +    if( op!=TK_CAST && op!=TK_UPLUS ){
 73912  73843         break;
 73913  73844       }
        73845  +    p = p->pLeft;
 73914  73846     }
 73915  73847     if( sqlite3CheckCollSeq(pParse, pColl) ){ 
 73916  73848       pColl = 0;
 73917  73849     }
 73918  73850     return pColl;
 73919  73851   }
 73920  73852   
................................................................................
 74010  73942   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
 74011  73943     Parse *pParse, 
 74012  73944     Expr *pLeft, 
 74013  73945     Expr *pRight
 74014  73946   ){
 74015  73947     CollSeq *pColl;
 74016  73948     assert( pLeft );
 74017         -  if( pLeft->flags & EP_Collate ){
 74018         -    pColl = sqlite3ExprCollSeq(pParse, pLeft);
 74019         -  }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
 74020         -    pColl = sqlite3ExprCollSeq(pParse, pRight);
        73949  +  if( pLeft->flags & EP_ExpCollate ){
        73950  +    assert( pLeft->pColl );
        73951  +    pColl = pLeft->pColl;
        73952  +  }else if( pRight && pRight->flags & EP_ExpCollate ){
        73953  +    assert( pRight->pColl );
        73954  +    pColl = pRight->pColl;
 74021  73955     }else{
 74022  73956       pColl = sqlite3ExprCollSeq(pParse, pLeft);
 74023  73957       if( !pColl ){
 74024  73958         pColl = sqlite3ExprCollSeq(pParse, pRight);
 74025  73959       }
 74026  73960     }
 74027  73961     return pColl;
................................................................................
 74243  74177     if( pRoot==0 ){
 74244  74178       assert( db->mallocFailed );
 74245  74179       sqlite3ExprDelete(db, pLeft);
 74246  74180       sqlite3ExprDelete(db, pRight);
 74247  74181     }else{
 74248  74182       if( pRight ){
 74249  74183         pRoot->pRight = pRight;
 74250         -      pRoot->flags |= EP_Collate & pRight->flags;
        74184  +      if( pRight->flags & EP_ExpCollate ){
        74185  +        pRoot->flags |= EP_ExpCollate;
        74186  +        pRoot->pColl = pRight->pColl;
        74187  +      }
 74251  74188       }
 74252  74189       if( pLeft ){
 74253  74190         pRoot->pLeft = pLeft;
 74254         -      pRoot->flags |= EP_Collate & pLeft->flags;
        74191  +      if( pLeft->flags & EP_ExpCollate ){
        74192  +        pRoot->flags |= EP_ExpCollate;
        74193  +        pRoot->pColl = pLeft->pColl;
        74194  +      }
 74255  74195       }
 74256  74196       exprSetHeight(pRoot);
 74257  74197     }
 74258  74198   }
 74259  74199   
 74260  74200   /*
 74261  74201   ** Allocate a Expr node which joins as many as two subtrees.
................................................................................
 74505  74445     if( 0==(flags&EXPRDUP_REDUCE) ){
 74506  74446       nSize = EXPR_FULLSIZE;
 74507  74447     }else{
 74508  74448       assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
 74509  74449       assert( !ExprHasProperty(p, EP_FromJoin) ); 
 74510  74450       assert( (p->flags2 & EP2_MallocedToken)==0 );
 74511  74451       assert( (p->flags2 & EP2_Irreducible)==0 );
 74512         -    if( p->pLeft || p->pRight || p->x.pList ){
        74452  +    if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
 74513  74453         nSize = EXPR_REDUCEDSIZE | EP_Reduced;
 74514  74454       }else{
 74515  74455         nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
 74516  74456       }
 74517  74457     }
 74518  74458     return nSize;
 74519  74459   }
................................................................................
 76529  76469         testcase( regFree2==0 );
 76530  76470         codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
 76531  76471         sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
 76532  76472         sqlite3ReleaseTempReg(pParse, r3);
 76533  76473         sqlite3ReleaseTempReg(pParse, r4);
 76534  76474         break;
 76535  76475       }
 76536         -    case TK_COLLATE: 
 76537  76476       case TK_UPLUS: {
 76538  76477         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 76539  76478         break;
 76540  76479       }
 76541  76480   
 76542  76481       case TK_TRIGGER: {
 76543  76482         /* If the opcode is TK_TRIGGER, then the expression is a reference
................................................................................
 76899  76838       case TK_UMINUS:  zUniOp = "UMINUS"; break;
 76900  76839       case TK_UPLUS:   zUniOp = "UPLUS";  break;
 76901  76840       case TK_BITNOT:  zUniOp = "BITNOT"; break;
 76902  76841       case TK_NOT:     zUniOp = "NOT";    break;
 76903  76842       case TK_ISNULL:  zUniOp = "ISNULL"; break;
 76904  76843       case TK_NOTNULL: zUniOp = "NOTNULL"; break;
 76905  76844   
 76906         -    case TK_COLLATE: {
 76907         -      sqlite3ExplainExpr(pOut, pExpr->pLeft);
 76908         -      sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
 76909         -      break;
 76910         -    }
 76911         -
 76912  76845       case TK_AGG_FUNCTION:
 76913  76846       case TK_CONST_FUNC:
 76914  76847       case TK_FUNCTION: {
 76915  76848         ExprList *pFarg;       /* List of function arguments */
 76916  76849         if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
 76917  76850           pFarg = 0;
 76918  76851         }else{
................................................................................
 77123  77056   static int evalConstExpr(Walker *pWalker, Expr *pExpr){
 77124  77057     Parse *pParse = pWalker->pParse;
 77125  77058     switch( pExpr->op ){
 77126  77059       case TK_IN:
 77127  77060       case TK_REGISTER: {
 77128  77061         return WRC_Prune;
 77129  77062       }
 77130         -    case TK_COLLATE: {
 77131         -      return WRC_Continue;
 77132         -    }
 77133  77063       case TK_FUNCTION:
 77134  77064       case TK_AGG_FUNCTION:
 77135  77065       case TK_CONST_FUNC: {
 77136  77066         /* The arguments to a function have a fixed destination.
 77137  77067         ** Mark them this way to avoid generated unneeded OP_SCopy
 77138  77068         ** instructions. 
 77139  77069         */
................................................................................
 77147  77077           }
 77148  77078         }
 77149  77079         break;
 77150  77080       }
 77151  77081     }
 77152  77082     if( isAppropriateForFactoring(pExpr) ){
 77153  77083       int r1 = ++pParse->nMem;
 77154         -    int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 77155         -    /* If r2!=r1, it means that register r1 is never used.  That is harmless
 77156         -    ** but suboptimal, so we want to know about the situation to fix it.
 77157         -    ** Hence the following assert: */
 77158         -    assert( r2==r1 );
        77084  +    int r2;
        77085  +    r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
        77086  +    if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
 77159  77087       pExpr->op2 = pExpr->op;
 77160  77088       pExpr->op = TK_REGISTER;
 77161  77089       pExpr->iTable = r2;
 77162  77090       return WRC_Prune;
 77163  77091     }
 77164  77092     return WRC_Continue;
 77165  77093   }
................................................................................
 77568  77496     }
 77569  77497     assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
 77570  77498     assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
 77571  77499     if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
 77572  77500       return 2;
 77573  77501     }
 77574  77502     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
 77575         -  if( pA->op!=pB->op ){
 77576         -    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB)<2 ){
 77577         -      return 1;
 77578         -    }
 77579         -    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft)<2 ){
 77580         -      return 1;
 77581         -    }
 77582         -    return 2;
 77583         -  }
        77503  +  if( pA->op!=pB->op ) return 2;
 77584  77504     if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
 77585  77505     if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
 77586  77506     if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
 77587  77507     if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
 77588  77508     if( ExprHasProperty(pA, EP_IntValue) ){
 77589  77509       if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
 77590  77510         return 2;
 77591  77511       }
 77592  77512     }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
 77593  77513       if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
 77594  77514       if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 77595         -      return pA->op==TK_COLLATE ? 1 : 2;
        77515  +      return 2;
 77596  77516       }
 77597  77517     }
        77518  +  if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
        77519  +  if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
 77598  77520     return 0;
 77599  77521   }
 77600  77522   
 77601  77523   /*
 77602  77524   ** Compare two ExprList objects.  Return 0 if they are identical and 
 77603  77525   ** non-zero if they differ in any way.
 77604  77526   **
................................................................................
 80841  80763   ** Note that if an error occurred, it might be the case that
 80842  80764   ** no VDBE code was generated.
 80843  80765   */
 80844  80766   SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
 80845  80767     sqlite3 *db;
 80846  80768     Vdbe *v;
 80847  80769   
 80848         -  assert( pParse->pToplevel==0 );
 80849  80770     db = pParse->db;
 80850  80771     if( db->mallocFailed ) return;
 80851  80772     if( pParse->nested ) return;
 80852  80773     if( pParse->nErr ) return;
 80853  80774   
 80854  80775     /* Begin by generating some termination code at the end of the
 80855  80776     ** vdbe program
................................................................................
 83405  83326   
 83406  83327     /* Figure out how many bytes of space are required to store explicitly
 83407  83328     ** specified collation sequence names.
 83408  83329     */
 83409  83330     for(i=0; i<pList->nExpr; i++){
 83410  83331       Expr *pExpr = pList->a[i].pExpr;
 83411  83332       if( pExpr ){
 83412         -      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
 83413         -      if( pColl ){
        83333  +      CollSeq *pColl = pExpr->pColl;
        83334  +      /* Either pColl!=0 or there was an OOM failure.  But if an OOM
        83335  +      ** failure we have quit before reaching this point. */
        83336  +      if( ALWAYS(pColl) ){
 83414  83337           nExtra += (1 + sqlite3Strlen30(pColl->zName));
 83415  83338         }
 83416  83339       }
 83417  83340     }
 83418  83341   
 83419  83342     /* 
 83420  83343     ** Allocate the index structure. 
................................................................................
 83469  83392     ** same column more than once cannot be an error because that would 
 83470  83393     ** break backwards compatibility - it needs to be a warning.
 83471  83394     */
 83472  83395     for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
 83473  83396       const char *zColName = pListItem->zName;
 83474  83397       Column *pTabCol;
 83475  83398       int requestedSortOrder;
 83476         -    CollSeq *pColl;                /* Collating sequence */
 83477  83399       char *zColl;                   /* Collation sequence name */
 83478  83400   
 83479  83401       for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
 83480  83402         if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
 83481  83403       }
 83482  83404       if( j>=pTab->nCol ){
 83483  83405         sqlite3ErrorMsg(pParse, "table %s has no column named %s",
 83484  83406           pTab->zName, zColName);
 83485  83407         pParse->checkSchema = 1;
 83486  83408         goto exit_create_index;
 83487  83409       }
 83488  83410       pIndex->aiColumn[i] = j;
 83489         -    if( pListItem->pExpr
 83490         -     && (pColl = sqlite3ExprCollSeq(pParse, pListItem->pExpr))!=0
 83491         -    ){
        83411  +    /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
        83412  +    ** the way the "idxlist" non-terminal is constructed by the parser,
        83413  +    ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
        83414  +    ** must exist or else there must have been an OOM error.  But if there
        83415  +    ** was an OOM error, we would never reach this point. */
        83416  +    if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
 83492  83417         int nColl;
 83493         -      zColl = pColl->zName;
        83418  +      zColl = pListItem->pExpr->pColl->zName;
 83494  83419         nColl = sqlite3Strlen30(zColl) + 1;
 83495  83420         assert( nExtra>=nColl );
 83496  83421         memcpy(zExtra, zColl, nColl);
 83497  83422         zColl = zExtra;
 83498  83423         zExtra += nColl;
 83499  83424         nExtra -= nColl;
 83500  83425       }else{
................................................................................
 84300  84225   ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
 84301  84226   ** schema on any databases.  This can be used to position the OP_Goto
 84302  84227   ** early in the code, before we know if any database tables will be used.
 84303  84228   */
 84304  84229   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
 84305  84230     Parse *pToplevel = sqlite3ParseToplevel(pParse);
 84306  84231   
 84307         -#ifndef SQLITE_OMIT_TRIGGER
 84308         -  if( pToplevel!=pParse ){
 84309         -    /* This branch is taken if a trigger is currently being coded. In this
 84310         -    ** case, set cookieGoto to a non-zero value to show that this function
 84311         -    ** has been called. This is used by the sqlite3ExprCodeConstants()
 84312         -    ** function. */
 84313         -    pParse->cookieGoto = -1;
 84314         -  }
 84315         -#endif
 84316  84232     if( pToplevel->cookieGoto==0 ){
 84317  84233       Vdbe *v = sqlite3GetVdbe(pToplevel);
 84318  84234       if( v==0 ) return;  /* This only happens if there was a prior error */
 84319  84235       pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
 84320  84236     }
 84321  84237     if( iDb>=0 ){
 84322  84238       sqlite3 *db = pToplevel->db;
................................................................................
 87888  87804   
 87889  87805       pLeft = sqlite3Expr(db, TK_REGISTER, 0);
 87890  87806       if( pLeft ){
 87891  87807         /* Set the collation sequence and affinity of the LHS of each TK_EQ
 87892  87808         ** expression to the parent key column defaults.  */
 87893  87809         if( pIdx ){
 87894  87810           Column *pCol;
 87895         -        const char *zColl;
 87896  87811           iCol = pIdx->aiColumn[i];
 87897  87812           pCol = &pTab->aCol[iCol];
 87898  87813           if( pTab->iPKey==iCol ) iCol = -1;
 87899  87814           pLeft->iTable = regData+iCol+1;
 87900  87815           pLeft->affinity = pCol->affinity;
 87901         -        zColl = pCol->zColl;
 87902         -        if( zColl==0 ) zColl = db->pDfltColl->zName;
 87903         -        pLeft = sqlite3ExprAddCollateString(pParse, pLeft, zColl);
        87816  +        pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
 87904  87817         }else{
 87905  87818           pLeft->iTable = regData;
 87906  87819           pLeft->affinity = SQLITE_AFF_INTEGER;
 87907  87820         }
 87908  87821       }
 87909  87822       iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
 87910  87823       assert( iCol>=0 );
................................................................................
 89876  89789   #ifndef SQLITE_OMIT_CHECK
 89877  89790     if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
 89878  89791       ExprList *pCheck = pTab->pCheck;
 89879  89792       pParse->ckBase = regData;
 89880  89793       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
 89881  89794       for(i=0; i<pCheck->nExpr; i++){
 89882  89795         int allOk = sqlite3VdbeMakeLabel(v);
 89883         -      sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
 89884         -      if( onError==OE_Ignore ){
 89885         -        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 89886         -      }else{
 89887         -        char *zConsName = pCheck->a[i].zName;
 89888         -        if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
 89889         -        if( zConsName ){
 89890         -          zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
        89796  +      Expr *pDup = sqlite3ExprDup(db, pCheck->a[i].pExpr, 0);
        89797  +      if( !db->mallocFailed ){
        89798  +        assert( pDup!=0 );
        89799  +        sqlite3ExprIfTrue(pParse, pDup, allOk, SQLITE_JUMPIFNULL);
        89800  +        if( onError==OE_Ignore ){
        89801  +          sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 89891  89802           }else{
 89892         -          zConsName = 0;
        89803  +          char *zConsName = pCheck->a[i].zName;
        89804  +          if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
        89805  +          if( zConsName ){
        89806  +            zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
        89807  +          }else{
        89808  +            zConsName = 0;
        89809  +          }
        89810  +          sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC);
 89893  89811           }
 89894         -        sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC);
        89812  +        sqlite3VdbeResolveLabel(v, allOk);
 89895  89813         }
 89896         -      sqlite3VdbeResolveLabel(v, allOk);
        89814  +      sqlite3ExprDelete(db, pDup);
 89897  89815       }
 89898  89816     }
 89899  89817   #endif /* !defined(SQLITE_OMIT_CHECK) */
 89900  89818   
 89901  89819     /* If we have an INTEGER PRIMARY KEY, make sure the primary key
 89902  89820     ** of the new record does not previously exist.  Except, if this
 89903  89821     ** is an UPDATE and the primary key is not changing, that is OK.
................................................................................
 95616  95534     }
 95617  95535     *pnCol = nCol;
 95618  95536     *paCol = aCol;
 95619  95537   
 95620  95538     for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 95621  95539       /* Get an appropriate name for the column
 95622  95540       */
 95623         -    p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
        95541  +    p = pEList->a[i].pExpr;
 95624  95542       assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
 95625  95543                  || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
 95626  95544       if( (zName = pEList->a[i].zName)!=0 ){
 95627  95545         /* If the column contains an "AS <name>" phrase, use <name> as the name */
 95628  95546         zName = sqlite3DbStrDup(db, zName);
 95629  95547       }else{
 95630  95548         Expr *pColExpr = p;  /* The expression that is the result column name */
................................................................................
 96614  96532       if( pKeyMerge ){
 96615  96533         pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
 96616  96534         pKeyMerge->nField = (u16)nOrderBy;
 96617  96535         pKeyMerge->enc = ENC(db);
 96618  96536         for(i=0; i<nOrderBy; i++){
 96619  96537           CollSeq *pColl;
 96620  96538           Expr *pTerm = pOrderBy->a[i].pExpr;
 96621         -        if( pTerm->flags & EP_Collate ){
 96622         -          pColl = sqlite3ExprCollSeq(pParse, pTerm);
        96539  +        if( pTerm->flags & EP_ExpCollate ){
        96540  +          pColl = pTerm->pColl;
 96623  96541           }else{
 96624  96542             pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
 96625         -          if( pColl==0 ) pColl = db->pDfltColl;
 96626         -          pOrderBy->a[i].pExpr =
 96627         -             sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
        96543  +          pTerm->flags |= EP_ExpCollate;
        96544  +          pTerm->pColl = pColl;
 96628  96545           }
 96629  96546           pKeyMerge->aColl[i] = pColl;
 96630  96547           pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
 96631  96548         }
 96632  96549       }
 96633  96550     }else{
 96634  96551       pKeyMerge = 0;
................................................................................
 96823  96740   
 96824  96741     /* Implement the main merge loop
 96825  96742     */
 96826  96743     sqlite3VdbeResolveLabel(v, labelCmpr);
 96827  96744     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
 96828  96745     sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
 96829  96746                            (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
 96830         -  sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
 96831  96747     sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
 96832  96748   
 96833  96749     /* Release temporary registers
 96834  96750     */
 96835  96751     if( regPrev ){
 96836  96752       sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
 96837  96753     }
................................................................................
 96891  96807       if( pExpr->iColumn<0 ){
 96892  96808         pExpr->op = TK_NULL;
 96893  96809       }else{
 96894  96810         Expr *pNew;
 96895  96811         assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
 96896  96812         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 96897  96813         pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
        96814  +      if( pNew && pExpr->pColl ){
        96815  +        pNew->pColl = pExpr->pColl;
        96816  +      }
 96898  96817         sqlite3ExprDelete(db, pExpr);
 96899  96818         pExpr = pNew;
 96900  96819       }
 96901  96820     }else{
 96902  96821       pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
 96903  96822       pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
 96904  96823       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
 98230  98149         /* Implement a co-routine that will return a single row of the result
 98231  98150         ** set on each invocation.
 98232  98151         */
 98233  98152         int addrTop;
 98234  98153         int addrEof;
 98235  98154         pItem->regReturn = ++pParse->nMem;
 98236  98155         addrEof = ++pParse->nMem;
 98237         -      /* Before coding the OP_Goto to jump to the start of the main routine,
 98238         -      ** ensure that the jump to the verify-schema routine has already
 98239         -      ** been coded. Otherwise, the verify-schema would likely be coded as 
 98240         -      ** part of the co-routine. If the main routine then accessed the 
 98241         -      ** database before invoking the co-routine for the first time (for 
 98242         -      ** example to initialize a LIMIT register from a sub-select), it would 
 98243         -      ** be doing so without having verified the schema version and obtained 
 98244         -      ** the required db locks. See ticket d6b36be38.  */
 98245         -      sqlite3CodeVerifySchema(pParse, -1);
 98246  98156         sqlite3VdbeAddOp0(v, OP_Goto);
 98247  98157         addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
 98248  98158         sqlite3VdbeChangeP5(v, 1);
 98249  98159         VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
 98250  98160         pItem->addrFillSub = addrTop;
 98251  98161         sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
 98252  98162         sqlite3VdbeChangeP5(v, 1);
................................................................................
 99916  99826       **   END;
 99917  99827       **
 99918  99828       **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
 99919  99829       **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
 99920  99830       */
 99921  99831       pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
 99922  99832   
 99923         -    /* Clear the cookieGoto flag. When coding triggers, the cookieGoto 
 99924         -    ** variable is used as a flag to indicate to sqlite3ExprCodeConstants()
 99925         -    ** that it is not safe to refactor constants (this happens after the
 99926         -    ** start of the first loop in the SQL statement is coded - at that 
 99927         -    ** point code may be conditionally executed, so it is no longer safe to 
 99928         -    ** initialize constant register values).  */
 99929         -    assert( pParse->cookieGoto==0 || pParse->cookieGoto==-1 );
 99930         -    pParse->cookieGoto = 0;
 99931         -
 99932  99833       switch( pStep->op ){
 99933  99834         case TK_UPDATE: {
 99934  99835           sqlite3Update(pParse, 
 99935  99836             targetSrcList(pParse, pStep),
 99936  99837             sqlite3ExprListDup(db, pStep->pExprList, 0), 
 99937  99838             sqlite3ExprDup(db, pStep->pWhere, 0), 
 99938  99839             pParse->eOrconf
................................................................................
103010 102911   */
103011 102912   #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
103012 102913   
103013 102914   /*
103014 102915   ** Commute a comparison operator.  Expressions of the form "X op Y"
103015 102916   ** are converted into "Y op X".
103016 102917   **
103017         -** If left/right precendence rules come into play when determining the
103018         -** collating
       102918  +** If a collation sequence is associated with either the left or right
103019 102919   ** side of the comparison, it remains associated with the same side after
103020 102920   ** the commutation. So "Y collate NOCASE op X" becomes 
103021         -** "X op Y". This is because any collation sequence on
       102921  +** "X collate NOCASE op Y". This is because any collation sequence on
103022 102922   ** the left hand side of a comparison overrides any collation sequence 
103023         -** attached to the right. For the same reason the EP_Collate flag
       102923  +** attached to the right. For the same reason the EP_ExpCollate flag
103024 102924   ** is not commuted.
103025 102925   */
103026 102926   static void exprCommute(Parse *pParse, Expr *pExpr){
103027         -  u16 expRight = (pExpr->pRight->flags & EP_Collate);
103028         -  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
       102927  +  u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
       102928  +  u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
103029 102929     assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
103030         -  if( expRight==expLeft ){
103031         -    /* Either X and Y both have COLLATE operator or neither do */
103032         -    if( expRight ){
103033         -      /* Both X and Y have COLLATE operators.  Make sure X is always
103034         -      ** used by clearing the EP_Collate flag from Y. */
103035         -      pExpr->pRight->flags &= ~EP_Collate;
103036         -    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
103037         -      /* Neither X nor Y have COLLATE operators, but X has a non-default
103038         -      ** collating sequence.  So add the EP_Collate marker on X to cause
103039         -      ** it to be searched first. */
103040         -      pExpr->pLeft->flags |= EP_Collate;
103041         -    }
103042         -  }
       102930  +  pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
       102931  +  pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
       102932  +  SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
       102933  +  pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
       102934  +  pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
103043 102935     SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
103044 102936     if( pExpr->op>=TK_GT ){
103045 102937       assert( TK_LT==TK_GT+2 );
103046 102938       assert( TK_GE==TK_LE+2 );
103047 102939       assert( TK_GT>TK_EQ );
103048 102940       assert( TK_GT<TK_LE );
103049 102941       assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
................................................................................
103112 103004     
103113 103005             /* Figure out the collation sequence required from an index for
103114 103006             ** it to be useful for optimising expression pX. Store this
103115 103007             ** value in variable pColl.
103116 103008             */
103117 103009             assert(pX->pLeft);
103118 103010             pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
103119         -          if( pColl==0 ) pColl = pParse->db->pDfltColl;
       103011  +          assert(pColl || pParse->nErr);
103120 103012     
103121 103013             for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
103122 103014               if( NEVER(j>=pIdx->nColumn) ) return 0;
103123 103015             }
103124         -          if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
       103016  +          if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
103125 103017           }
103126 103018           return pTerm;
103127 103019         }
103128 103020       }
103129 103021     }
103130 103022     return 0;
103131 103023   }
................................................................................
103635 103527     sqlite3 *db = pParse->db;        /* Database connection */
103636 103528   
103637 103529     if( db->mallocFailed ){
103638 103530       return;
103639 103531     }
103640 103532     pTerm = &pWC->a[idxTerm];
103641 103533     pMaskSet = pWC->pMaskSet;
103642         -  pExpr = sqlite3ExprSkipCollate(pTerm->pExpr);
       103534  +  pExpr = pTerm->pExpr;
103643 103535     prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
103644 103536     op = pExpr->op;
103645 103537     if( op==TK_IN ){
103646 103538       assert( pExpr->pRight==0 );
103647 103539       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
103648 103540         pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
103649 103541       }else{
................................................................................
103662 103554                          ** on left table of a LEFT JOIN.  Ticket #3015 */
103663 103555     }
103664 103556     pTerm->prereqAll = prereqAll;
103665 103557     pTerm->leftCursor = -1;
103666 103558     pTerm->iParent = -1;
103667 103559     pTerm->eOperator = 0;
103668 103560     if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
103669         -    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
103670         -    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
       103561  +    Expr *pLeft = pExpr->pLeft;
       103562  +    Expr *pRight = pExpr->pRight;
103671 103563       if( pLeft->op==TK_COLUMN ){
103672 103564         pTerm->leftCursor = pLeft->iTable;
103673 103565         pTerm->u.leftColumn = pLeft->iColumn;
103674 103566         pTerm->eOperator = operatorMask(op);
103675 103567       }
103676 103568       if( pRight && pRight->op==TK_COLUMN ){
103677 103569         WhereTerm *pNew;
................................................................................
103691 103583           pTerm->nChild = 1;
103692 103584           pTerm->wtFlags |= TERM_COPIED;
103693 103585         }else{
103694 103586           pDup = pExpr;
103695 103587           pNew = pTerm;
103696 103588         }
103697 103589         exprCommute(pParse, pDup);
103698         -      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
       103590  +      pLeft = pDup->pLeft;
103699 103591         pNew->leftCursor = pLeft->iTable;
103700 103592         pNew->u.leftColumn = pLeft->iColumn;
103701 103593         testcase( (prereqLeft | extraRight) != prereqLeft );
103702 103594         pNew->prereqRight = prereqLeft | extraRight;
103703 103595         pNew->prereqAll = prereqAll;
103704 103596         pNew->eOperator = operatorMask(pDup->op);
103705 103597       }
................................................................................
103770 103662     ){
103771 103663       Expr *pLeft;       /* LHS of LIKE/GLOB operator */
103772 103664       Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
103773 103665       Expr *pNewExpr1;
103774 103666       Expr *pNewExpr2;
103775 103667       int idxNew1;
103776 103668       int idxNew2;
103777         -    Token sCollSeqName;  /* Name of collating sequence */
       103669  +    CollSeq *pColl;    /* Collating sequence to use */
103778 103670   
103779 103671       pLeft = pExpr->x.pList->a[1].pExpr;
103780 103672       pStr2 = sqlite3ExprDup(db, pStr1, 0);
103781 103673       if( !db->mallocFailed ){
103782 103674         u8 c, *pC;       /* Last character before the first wildcard */
103783 103675         pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
103784 103676         c = *pC;
................................................................................
103792 103684           if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
103793 103685   
103794 103686   
103795 103687           c = sqlite3UpperToLower[c];
103796 103688         }
103797 103689         *pC = c + 1;
103798 103690       }
103799         -    sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
103800         -    sCollSeqName.n = 6;
103801         -    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
       103691  +    pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
103802 103692       pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
103803         -           sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
103804         -           pStr1, 0);
       103693  +                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
       103694  +                     pStr1, 0);
103805 103695       idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
103806 103696       testcase( idxNew1==0 );
103807 103697       exprAnalyze(pSrc, pWC, idxNew1);
103808         -    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
103809 103698       pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
103810         -           sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
103811         -           pStr2, 0);
       103699  +                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
       103700  +                     pStr2, 0);
103812 103701       idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
103813 103702       testcase( idxNew2==0 );
103814 103703       exprAnalyze(pSrc, pWC, idxNew2);
103815 103704       pTerm = &pWC->a[idxTerm];
103816 103705       if( isComplete ){
103817 103706         pWC->a[idxNew1].iParent = idxTerm;
103818 103707         pWC->a[idxNew2].iParent = idxTerm;
................................................................................
103922 103811     Index *pIdx,                    /* Index to match column of */
103923 103812     int iCol                        /* Column of index to match */
103924 103813   ){
103925 103814     int i;
103926 103815     const char *zColl = pIdx->azColl[iCol];
103927 103816   
103928 103817     for(i=0; i<pList->nExpr; i++){
103929         -    Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
       103818  +    Expr *p = pList->a[i].pExpr;
103930 103819       if( p->op==TK_COLUMN
103931 103820        && p->iColumn==pIdx->aiColumn[iCol]
103932 103821        && p->iTable==iBase
103933 103822       ){
103934         -      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
       103823  +      CollSeq *pColl = sqlite3ExprCollSeq(pParse, p);
103935 103824         if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
103936 103825           return i;
103937 103826         }
103938 103827       }
103939 103828     }
103940 103829   
103941 103830     return -1;
................................................................................
103974 103863     ** can be ignored. If it does not, and the column does not belong to the
103975 103864     ** same table as index pIdx, return early. Finally, if there is no
103976 103865     ** matching "col=X" expression and the column is on the same table as pIdx,
103977 103866     ** set the corresponding bit in variable mask.
103978 103867     */
103979 103868     for(i=0; i<pDistinct->nExpr; i++){
103980 103869       WhereTerm *pTerm;
103981         -    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
       103870  +    Expr *p = pDistinct->a[i].pExpr;
103982 103871       if( p->op!=TK_COLUMN ) return 0;
103983 103872       pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
103984 103873       if( pTerm ){
103985 103874         Expr *pX = pTerm->pExpr;
103986 103875         CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
103987 103876         CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
103988 103877         if( p1==p2 ) continue;
................................................................................
104026 103915     pTab = pTabList->a[0].pTab;
104027 103916   
104028 103917     /* If any of the expressions is an IPK column on table iBase, then return 
104029 103918     ** true. Note: The (p->iTable==iBase) part of this test may be false if the
104030 103919     ** current SELECT is a correlated sub-query.
104031 103920     */
104032 103921     for(i=0; i<pDistinct->nExpr; i++){
104033         -    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
       103922  +    Expr *p = pDistinct->a[i].pExpr;
104034 103923       if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
104035 103924     }
104036 103925   
104037 103926     /* Loop through all indices on the table, checking each to see if it makes
104038 103927     ** the DISTINCT qualifier redundant. It does so if:
104039 103928     **
104040 103929     **   1. The index is itself UNIQUE, and
................................................................................
105312 105201       int isMatch;            /* ORDER BY term matches the index term */
105313 105202       const char *zColl;      /* Name of collating sequence for i-th index term */
105314 105203       WhereTerm *pConstraint; /* A constraint in the WHERE clause */
105315 105204   
105316 105205       /* If the next term of the ORDER BY clause refers to anything other than
105317 105206       ** a column in the "base" table, then this index will not be of any
105318 105207       ** further use in handling the ORDER BY. */
105319         -    pOBExpr = sqlite3ExprSkipCollate(pOBItem->pExpr);
       105208  +    pOBExpr = pOBItem->pExpr;
105320 105209       if( pOBExpr->op!=TK_COLUMN || pOBExpr->iTable!=base ){
105321 105210         break;
105322 105211       }
105323 105212   
105324 105213       /* Find column number and collating sequence for the next entry
105325 105214       ** in the index */
105326 105215       if( pIdx->zName && i<pIdx->nColumn ){
................................................................................
105338 105227       }
105339 105228   
105340 105229       /* Check to see if the column number and collating sequence of the
105341 105230       ** index match the column number and collating sequence of the ORDER BY
105342 105231       ** clause entry.  Set isMatch to 1 if they both match. */
105343 105232       if( pOBExpr->iColumn==iColumn ){
105344 105233         if( zColl ){
105345         -        pColl = sqlite3ExprCollSeq(pParse, pOBItem->pExpr);
       105234  +        pColl = sqlite3ExprCollSeq(pParse, pOBExpr);
105346 105235           if( !pColl ) pColl = db->pDfltColl;
105347 105236           isMatch = sqlite3StrICmp(pColl->zName, zColl)==0;
105348 105237         }else{
105349 105238           isMatch = 1;
105350 105239         }
105351 105240       }else{
105352 105241         isMatch = 0;
................................................................................
105479 105368     Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
105480 105369     int eqTermMask;             /* Current mask of valid equality operators */
105481 105370     int idxEqTermMask;          /* Index mask of valid equality operators */
105482 105371     Index sPk;                  /* A fake index object for the primary key */
105483 105372     tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
105484 105373     int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
105485 105374     int wsFlagMask;             /* Allowed flags in p->cost.plan.wsFlag */
105486         -  int nPriorSat;              /* ORDER BY terms satisfied by outer loops */
105487         -  int nOrderBy;               /* Number of ORDER BY terms */
105488         -  char bSortInit;             /* Initializer for bSort in inner loop */
105489         -  char bDistInit;             /* Initializer for bDist in inner loop */
105490         -
105491 105375   
105492 105376     /* Initialize the cost to a worst-case value */
105493 105377     memset(&p->cost, 0, sizeof(p->cost));
105494 105378     p->cost.rCost = SQLITE_BIG_DBL;
105495 105379   
105496 105380     /* If the pSrc table is the right table of a LEFT JOIN then we may not
105497 105381     ** use an index to satisfy IS NULL constraints on that table.  This is
................................................................................
105533 105417       wsFlagMask = ~(
105534 105418           WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
105535 105419       );
105536 105420       eqTermMask = WO_EQ|WO_IN;
105537 105421       pIdx = 0;
105538 105422     }
105539 105423   
105540         -  nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
105541         -  if( p->i ){
105542         -    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
105543         -    bSortInit = nPriorSat<nOrderBy;
105544         -    bDistInit = 0;
105545         -  }else{
105546         -    nPriorSat = 0;
105547         -    bSortInit = nOrderBy>0;
105548         -    bDistInit = p->pDistinct!=0;
105549         -  }
105550         -
105551 105424     /* Loop over all indices looking for the best one to use
105552 105425     */
105553 105426     for(; pProbe; pIdx=pProbe=pProbe->pNext){
105554 105427       const tRowcnt * const aiRowEst = pProbe->aiRowEst;
105555 105428       WhereCost pc;               /* Cost of using pProbe */
105556 105429       double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
105557 105430   
................................................................................
105621 105494       **             SELECT a, b    FROM tbl WHERE a = 1;
105622 105495       **             SELECT a, b, c FROM tbl WHERE a = 1;
105623 105496       */
105624 105497       int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
105625 105498       int nInMul = 1;               /* Number of distinct equalities to lookup */
105626 105499       double rangeDiv = (double)1;  /* Estimated reduction in search space */
105627 105500       int nBound = 0;               /* Number of range constraints seen */
105628         -    char bSort = bSortInit;       /* True if external sort required */
105629         -    char bDist = bDistInit;       /* True if index cannot help with DISTINCT */
105630         -    char bLookup = 0;             /* True if not a covering index */
       105501  +    int bSort;                    /* True if external sort required */
       105502  +    int bDist;                    /* True if index cannot help with DISTINCT */
       105503  +    int bLookup = 0;              /* True if not a covering index */
       105504  +    int nPriorSat;                /* ORDER BY terms satisfied by outer loops */
       105505  +    int nOrderBy;                 /* Number of ORDER BY terms */
105631 105506       WhereTerm *pTerm;             /* A single term of the WHERE clause */
105632 105507   #ifdef SQLITE_ENABLE_STAT3
105633 105508       WhereTerm *pFirstTerm = 0;    /* First term matching the index */
105634 105509   #endif
105635 105510   
105636 105511       WHERETRACE((
105637 105512         "   %s(%s):\n",
105638 105513         pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk")
105639 105514       ));
105640 105515       memset(&pc, 0, sizeof(pc));
105641         -    pc.plan.nOBSat = nPriorSat;
       105516  +    nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
       105517  +    if( p->i ){
       105518  +      nPriorSat = pc.plan.nOBSat = p->aLevel[p->i-1].plan.nOBSat;
       105519  +      bSort = nPriorSat<nOrderBy;
       105520  +      bDist = 0;
       105521  +    }else{
       105522  +      nPriorSat = pc.plan.nOBSat = 0;
       105523  +      bSort = nOrderBy>0;
       105524  +      bDist = p->pDistinct!=0;
       105525  +    }
105642 105526   
105643 105527       /* Determine the values of pc.plan.nEq and nInMul */
105644 105528       for(pc.plan.nEq=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
105645 105529         int j = pProbe->aiColumn[pc.plan.nEq];
105646 105530         pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
105647 105531         if( pTerm==0 ) break;
105648 105532         pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
................................................................................
110668 110552     spanExpr(&yygotominor.yy342, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
110669 110553     sqlite3ExprAssignVarNumber(pParse, yygotominor.yy342.pExpr);
110670 110554     spanSet(&yygotominor.yy342, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
110671 110555   }
110672 110556           break;
110673 110557         case 194: /* expr ::= expr COLLATE ids */
110674 110558   {
110675         -  yygotominor.yy342.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy342.pExpr, &yymsp[0].minor.yy0);
       110559  +  yygotominor.yy342.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy342.pExpr, &yymsp[0].minor.yy0);
110676 110560     yygotominor.yy342.zStart = yymsp[-2].minor.yy342.zStart;
110677 110561     yygotominor.yy342.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
110678 110562   }
110679 110563           break;
110680 110564         case 195: /* expr ::= CAST LP expr AS typetoken RP */
110681 110565   {
110682 110566     yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy342.pExpr, 0, &yymsp[-1].minor.yy0);
................................................................................
110927 110811   {yygotominor.yy392 = OE_Abort;}
110928 110812           break;
110929 110813         case 244: /* uniqueflag ::= */
110930 110814   {yygotominor.yy392 = OE_None;}
110931 110815           break;
110932 110816         case 247: /* idxlist ::= idxlist COMMA nm collate sortorder */
110933 110817   {
110934         -  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
       110818  +  Expr *p = 0;
       110819  +  if( yymsp[-1].minor.yy0.n>0 ){
       110820  +    p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
       110821  +    sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
       110822  +  }
110935 110823     yygotominor.yy442 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy442, p);
110936 110824     sqlite3ExprListSetName(pParse,yygotominor.yy442,&yymsp[-2].minor.yy0,1);
110937 110825     sqlite3ExprListCheckLength(pParse, yygotominor.yy442, "index");
110938 110826     if( yygotominor.yy442 ) yygotominor.yy442->a[yygotominor.yy442->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy392;
110939 110827   }
110940 110828           break;
110941 110829         case 248: /* idxlist ::= nm collate sortorder */
110942 110830   {
110943         -  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
       110831  +  Expr *p = 0;
       110832  +  if( yymsp[-1].minor.yy0.n>0 ){
       110833  +    p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
       110834  +    sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
       110835  +  }
110944 110836     yygotominor.yy442 = sqlite3ExprListAppend(pParse,0, p);
110945 110837     sqlite3ExprListSetName(pParse, yygotominor.yy442, &yymsp[-2].minor.yy0, 1);
110946 110838     sqlite3ExprListCheckLength(pParse, yygotominor.yy442, "index");
110947 110839     if( yygotominor.yy442 ) yygotominor.yy442->a[yygotominor.yy442->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy392;
110948 110840   }
110949 110841           break;
110950 110842         case 249: /* collate ::= */