Fossil

Diff
Login

Differences From Artifact [955cc67a]:

To Artifact [e2e1c87b]:


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