/ Changes On Branch ticket-71e333e7
Login

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

Changes In Branch ticket-71e333e7 Excluding Merge-Ins

This is equivalent to a diff from d507648d82 to cdbfa66483

2012-12-08
21:51
Refactor collating-sequence handling as a fix for ticket [71e333e7d2e642]. The Expr.pColl field is removed from the Expr object. The COLLATE operator now becomes a separate instance of Expr in the expression tree. The code generator looks up the correct collating function as needed, rather than referring to Expr.pColl. (check-in: 8542e6180d user: drh tags: trunk)
21:36
Adjustments to the collating-sequence refactoring to facilitate full-coverage testing and to fix some minor issues found by TH3. (Closed-Leaf check-in: cdbfa66483 user: drh tags: ticket-71e333e7)
14:16
Make sure WHERE clause constraints A=B and B=A work the same even with COLLATE clauses. (check-in: b3f5366811 user: drh tags: ticket-71e333e7)
2012-12-06
19:01
Add the SQLITE_FCNTL_TEMPFILENAME file control that asks the underlying VFS to return a new temporary filename. Per request from NSS team at Mozilla. (check-in: 1a63b1d5fa user: drh tags: trunk)
15:15
Add a test case that demonstrates ticket [71e333e7d2e642]. (check-in: cc6e0785df user: drh tags: ticket-71e333e7)
04:33
For the sqlite3-all.c target, use backslashes when calling the splitter script via the MSVC makefile. (check-in: d507648d82 user: mistachkin tags: trunk)
04:19
Bring the ancient malloc3.test file closer into relevance with the latest core code. (check-in: 9793a21c13 user: mistachkin tags: trunk)

Changes to src/build.c.

  2686   2686   
  2687   2687     /* Figure out how many bytes of space are required to store explicitly
  2688   2688     ** specified collation sequence names.
  2689   2689     */
  2690   2690     for(i=0; i<pList->nExpr; i++){
  2691   2691       Expr *pExpr = pList->a[i].pExpr;
  2692   2692       if( pExpr ){
  2693         -      CollSeq *pColl = pExpr->pColl;
  2694         -      /* Either pColl!=0 or there was an OOM failure.  But if an OOM
  2695         -      ** failure we have quit before reaching this point. */
  2696         -      if( ALWAYS(pColl) ){
         2693  +      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
         2694  +      if( pColl ){
  2697   2695           nExtra += (1 + sqlite3Strlen30(pColl->zName));
  2698   2696         }
  2699   2697       }
  2700   2698     }
  2701   2699   
  2702   2700     /* 
  2703   2701     ** Allocate the index structure. 
................................................................................
  2752   2750     ** same column more than once cannot be an error because that would 
  2753   2751     ** break backwards compatibility - it needs to be a warning.
  2754   2752     */
  2755   2753     for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
  2756   2754       const char *zColName = pListItem->zName;
  2757   2755       Column *pTabCol;
  2758   2756       int requestedSortOrder;
         2757  +    CollSeq *pColl;                /* Collating sequence */
  2759   2758       char *zColl;                   /* Collation sequence name */
  2760   2759   
  2761   2760       for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
  2762   2761         if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
  2763   2762       }
  2764   2763       if( j>=pTab->nCol ){
  2765   2764         sqlite3ErrorMsg(pParse, "table %s has no column named %s",
  2766   2765           pTab->zName, zColName);
  2767   2766         pParse->checkSchema = 1;
  2768   2767         goto exit_create_index;
  2769   2768       }
  2770   2769       pIndex->aiColumn[i] = j;
  2771         -    /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
  2772         -    ** the way the "idxlist" non-terminal is constructed by the parser,
  2773         -    ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
  2774         -    ** must exist or else there must have been an OOM error.  But if there
  2775         -    ** was an OOM error, we would never reach this point. */
  2776         -    if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
         2770  +    if( pListItem->pExpr
         2771  +     && (pColl = sqlite3ExprCollSeq(pParse, pListItem->pExpr))!=0
         2772  +    ){
  2777   2773         int nColl;
  2778         -      zColl = pListItem->pExpr->pColl->zName;
         2774  +      zColl = pColl->zName;
  2779   2775         nColl = sqlite3Strlen30(zColl) + 1;
  2780   2776         assert( nExtra>=nColl );
  2781   2777         memcpy(zExtra, zColl, nColl);
  2782   2778         zColl = zExtra;
  2783   2779         zExtra += nColl;
  2784   2780         nExtra -= nColl;
  2785   2781       }else{

Changes to src/expr.c.

    27     27   **
    28     28   ** CREATE TABLE t1(a);
    29     29   ** SELECT * FROM t1 WHERE a;
    30     30   ** SELECT a AS b FROM t1 WHERE b;
    31     31   ** SELECT * FROM t1 WHERE (select a from t1);
    32     32   */
    33     33   char sqlite3ExprAffinity(Expr *pExpr){
    34         -  int op = pExpr->op;
           34  +  int op;
           35  +  pExpr = sqlite3ExprSkipCollate(pExpr);
           36  +  op = pExpr->op;
    35     37     if( op==TK_SELECT ){
    36     38       assert( pExpr->flags&EP_xIsSelect );
    37     39       return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
    38     40     }
    39     41   #ifndef SQLITE_OMIT_CAST
    40     42     if( op==TK_CAST ){
    41     43       assert( !ExprHasProperty(pExpr, EP_IntValue) );
................................................................................
    52     54       assert( pExpr->pTab && j<pExpr->pTab->nCol );
    53     55       return pExpr->pTab->aCol[j].affinity;
    54     56     }
    55     57     return pExpr->affinity;
    56     58   }
    57     59   
    58     60   /*
    59         -** Set the explicit collating sequence for an expression to the
    60         -** collating sequence supplied in the second argument.
           61  +** Set the collating sequence for expression pExpr to be the collating
           62  +** sequence named by pToken.   Return a pointer to a new Expr node that
           63  +** implements the COLLATE operator.
           64  +**
           65  +** If a memory allocation error occurs, that fact is recorded in pParse->db
           66  +** and the pExpr parameter is returned unchanged.
    61     67   */
    62         -Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
    63         -  if( pExpr && pColl ){
    64         -    pExpr->pColl = pColl;
    65         -    pExpr->flags |= EP_ExpCollate;
           68  +Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
           69  +  if( pCollName->n>0 ){
           70  +    Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
           71  +    if( pNew ){
           72  +      pNew->pLeft = pExpr;
           73  +      pNew->flags |= EP_Collate;
           74  +      pExpr = pNew;
           75  +    }
           76  +  }
           77  +  return pExpr;
           78  +}
           79  +Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
           80  +  Token s;
           81  +  assert( zC!=0 );
           82  +  s.z = zC;
           83  +  s.n = sqlite3Strlen30(s.z);
           84  +  return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
           85  +}
           86  +
           87  +/*
           88  +** Skip over any TK_COLLATE and/or TK_AS operators at the root of
           89  +** an expression.
           90  +*/
           91  +Expr *sqlite3ExprSkipCollate(Expr *pExpr){
           92  +  while( pExpr && (pExpr->op==TK_COLLATE || pExpr->op==TK_AS) ){
           93  +    pExpr = pExpr->pLeft;
    66     94     }
    67     95     return pExpr;
    68     96   }
    69     97   
    70     98   /*
    71         -** Set the collating sequence for expression pExpr to be the collating
    72         -** sequence named by pToken.   Return a pointer to the revised expression.
    73         -** The collating sequence is marked as "explicit" using the EP_ExpCollate
    74         -** flag.  An explicit collating sequence will override implicit
    75         -** collating sequences.
    76         -*/
    77         -Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
    78         -  char *zColl = 0;            /* Dequoted name of collation sequence */
    79         -  CollSeq *pColl;
    80         -  sqlite3 *db = pParse->db;
    81         -  zColl = sqlite3NameFromToken(db, pCollName);
    82         -  pColl = sqlite3LocateCollSeq(pParse, zColl);
    83         -  sqlite3ExprSetColl(pExpr, pColl);
    84         -  sqlite3DbFree(db, zColl);
    85         -  return pExpr;
    86         -}
    87         -
    88         -/*
    89         -** Return the default collation sequence for the expression pExpr. If
    90         -** there is no default collation type, return 0.
           99  +** Return the collation sequence for the expression pExpr. If
          100  +** there is no defined collating sequence, return NULL.
          101  +**
          102  +** The collating sequence might be determined by a COLLATE operator
          103  +** or by the presence of a column with a defined collating sequence.
          104  +** COLLATE operators take first precedence.  Left operands take
          105  +** precedence over right operands.
    91    106   */
    92    107   CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
          108  +  sqlite3 *db = pParse->db;
    93    109     CollSeq *pColl = 0;
    94    110     Expr *p = pExpr;
    95    111     while( p ){
    96         -    int op;
    97         -    pColl = p->pColl;
    98         -    if( pColl ) break;
    99         -    op = p->op;
   100         -    if( p->pTab!=0 && (
   101         -        op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
   102         -    )){
          112  +    int op = p->op;
          113  +    if( op==TK_CAST || op==TK_UPLUS ){
          114  +      p = p->pLeft;
          115  +      continue;
          116  +    }
          117  +    assert( op!=TK_REGISTER || p->op2!=TK_COLLATE );
          118  +    if( op==TK_COLLATE ){
          119  +      if( db->init.busy ){
          120  +        /* Do not report errors when parsing while the schema */
          121  +        pColl = sqlite3FindCollSeq(db, ENC(db), p->u.zToken, 0);
          122  +      }else{
          123  +        pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
          124  +      }
          125  +      break;
          126  +    }
          127  +    if( p->pTab!=0
          128  +     && (op==TK_AGG_COLUMN || op==TK_COLUMN
          129  +          || op==TK_REGISTER || op==TK_TRIGGER)
          130  +    ){
   103    131         /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
   104    132         ** a TK_COLUMN but was previously evaluated and cached in a register */
   105         -      const char *zColl;
   106    133         int j = p->iColumn;
   107    134         if( j>=0 ){
   108         -        sqlite3 *db = pParse->db;
   109         -        zColl = p->pTab->aCol[j].zColl;
          135  +        const char *zColl = p->pTab->aCol[j].zColl;
   110    136           pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
   111         -        pExpr->pColl = pColl;
   112    137         }
   113    138         break;
   114    139       }
   115         -    if( op!=TK_CAST && op!=TK_UPLUS ){
          140  +    if( p->flags & EP_Collate ){
          141  +      if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
          142  +        p = p->pLeft;
          143  +      }else{
          144  +        p = p->pRight;
          145  +      }
          146  +    }else{
   116    147         break;
   117    148       }
   118         -    p = p->pLeft;
   119    149     }
   120    150     if( sqlite3CheckCollSeq(pParse, pColl) ){ 
   121    151       pColl = 0;
   122    152     }
   123    153     return pColl;
   124    154   }
   125    155   
................................................................................
   215    245   CollSeq *sqlite3BinaryCompareCollSeq(
   216    246     Parse *pParse, 
   217    247     Expr *pLeft, 
   218    248     Expr *pRight
   219    249   ){
   220    250     CollSeq *pColl;
   221    251     assert( pLeft );
   222         -  if( pLeft->flags & EP_ExpCollate ){
   223         -    assert( pLeft->pColl );
   224         -    pColl = pLeft->pColl;
   225         -  }else if( pRight && pRight->flags & EP_ExpCollate ){
   226         -    assert( pRight->pColl );
   227         -    pColl = pRight->pColl;
          252  +  if( pLeft->flags & EP_Collate ){
          253  +    pColl = sqlite3ExprCollSeq(pParse, pLeft);
          254  +  }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
          255  +    pColl = sqlite3ExprCollSeq(pParse, pRight);
   228    256     }else{
   229    257       pColl = sqlite3ExprCollSeq(pParse, pLeft);
   230    258       if( !pColl ){
   231    259         pColl = sqlite3ExprCollSeq(pParse, pRight);
   232    260       }
   233    261     }
   234    262     return pColl;
................................................................................
   450    478     if( pRoot==0 ){
   451    479       assert( db->mallocFailed );
   452    480       sqlite3ExprDelete(db, pLeft);
   453    481       sqlite3ExprDelete(db, pRight);
   454    482     }else{
   455    483       if( pRight ){
   456    484         pRoot->pRight = pRight;
   457         -      if( pRight->flags & EP_ExpCollate ){
   458         -        pRoot->flags |= EP_ExpCollate;
   459         -        pRoot->pColl = pRight->pColl;
   460         -      }
          485  +      pRoot->flags |= EP_Collate & pRight->flags;
   461    486       }
   462    487       if( pLeft ){
   463    488         pRoot->pLeft = pLeft;
   464         -      if( pLeft->flags & EP_ExpCollate ){
   465         -        pRoot->flags |= EP_ExpCollate;
   466         -        pRoot->pColl = pLeft->pColl;
   467         -      }
          489  +      pRoot->flags |= EP_Collate & pLeft->flags;
   468    490       }
   469    491       exprSetHeight(pRoot);
   470    492     }
   471    493   }
   472    494   
   473    495   /*
   474    496   ** Allocate a Expr node which joins as many as two subtrees.
................................................................................
   718    740     if( 0==(flags&EXPRDUP_REDUCE) ){
   719    741       nSize = EXPR_FULLSIZE;
   720    742     }else{
   721    743       assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
   722    744       assert( !ExprHasProperty(p, EP_FromJoin) ); 
   723    745       assert( (p->flags2 & EP2_MallocedToken)==0 );
   724    746       assert( (p->flags2 & EP2_Irreducible)==0 );
   725         -    if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
          747  +    if( p->pLeft || p->pRight || p->x.pList ){
   726    748         nSize = EXPR_REDUCEDSIZE | EP_Reduced;
   727    749       }else{
   728    750         nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
   729    751       }
   730    752     }
   731    753     return nSize;
   732    754   }
................................................................................
  2742   2764         testcase( regFree2==0 );
  2743   2765         codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
  2744   2766         sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
  2745   2767         sqlite3ReleaseTempReg(pParse, r3);
  2746   2768         sqlite3ReleaseTempReg(pParse, r4);
  2747   2769         break;
  2748   2770       }
         2771  +    case TK_COLLATE: 
  2749   2772       case TK_UPLUS: {
  2750   2773         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  2751   2774         break;
  2752   2775       }
  2753   2776   
  2754   2777       case TK_TRIGGER: {
  2755   2778         /* If the opcode is TK_TRIGGER, then the expression is a reference
................................................................................
  3110   3133   
  3111   3134       case TK_UMINUS:  zUniOp = "UMINUS"; break;
  3112   3135       case TK_UPLUS:   zUniOp = "UPLUS";  break;
  3113   3136       case TK_BITNOT:  zUniOp = "BITNOT"; break;
  3114   3137       case TK_NOT:     zUniOp = "NOT";    break;
  3115   3138       case TK_ISNULL:  zUniOp = "ISNULL"; break;
  3116   3139       case TK_NOTNULL: zUniOp = "NOTNULL"; break;
         3140  +
         3141  +    case TK_COLLATE: {
         3142  +      sqlite3ExplainExpr(pOut, pExpr->pLeft);
         3143  +      sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
         3144  +      break;
         3145  +    }
  3117   3146   
  3118   3147       case TK_AGG_FUNCTION:
  3119   3148       case TK_CONST_FUNC:
  3120   3149       case TK_FUNCTION: {
  3121   3150         ExprList *pFarg;       /* List of function arguments */
  3122   3151         if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
  3123   3152           pFarg = 0;
................................................................................
  3329   3358   static int evalConstExpr(Walker *pWalker, Expr *pExpr){
  3330   3359     Parse *pParse = pWalker->pParse;
  3331   3360     switch( pExpr->op ){
  3332   3361       case TK_IN:
  3333   3362       case TK_REGISTER: {
  3334   3363         return WRC_Prune;
  3335   3364       }
         3365  +    case TK_COLLATE: {
         3366  +      return WRC_Continue;
         3367  +    }
  3336   3368       case TK_FUNCTION:
  3337   3369       case TK_AGG_FUNCTION:
  3338   3370       case TK_CONST_FUNC: {
  3339   3371         /* The arguments to a function have a fixed destination.
  3340   3372         ** Mark them this way to avoid generated unneeded OP_SCopy
  3341   3373         ** instructions. 
  3342   3374         */
................................................................................
  3350   3382           }
  3351   3383         }
  3352   3384         break;
  3353   3385       }
  3354   3386     }
  3355   3387     if( isAppropriateForFactoring(pExpr) ){
  3356   3388       int r1 = ++pParse->nMem;
  3357         -    int r2;
  3358         -    r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
  3359         -    if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
         3389  +    int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
         3390  +    /* If r2!=r1, it means that register r1 is never used.  That is harmless
         3391  +    ** but suboptimal, so we want to know about the situation to fix it.
         3392  +    ** Hence the following assert: */
         3393  +    assert( r2==r1 );
  3360   3394       pExpr->op2 = pExpr->op;
  3361   3395       pExpr->op = TK_REGISTER;
  3362   3396       pExpr->iTable = r2;
  3363   3397       return WRC_Prune;
  3364   3398     }
  3365   3399     return WRC_Continue;
  3366   3400   }
................................................................................
  3769   3803     }
  3770   3804     assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
  3771   3805     assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
  3772   3806     if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
  3773   3807       return 2;
  3774   3808     }
  3775   3809     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
  3776         -  if( pA->op!=pB->op ) return 2;
         3810  +  if( pA->op!=pB->op ){
         3811  +    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB)<2 ){
         3812  +      return 1;
         3813  +    }
         3814  +    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft)<2 ){
         3815  +      return 1;
         3816  +    }
         3817  +    return 2;
         3818  +  }
  3777   3819     if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
  3778   3820     if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
  3779   3821     if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
  3780   3822     if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
  3781   3823     if( ExprHasProperty(pA, EP_IntValue) ){
  3782   3824       if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
  3783   3825         return 2;
  3784   3826       }
  3785   3827     }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
  3786   3828       if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
  3787   3829       if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
  3788         -      return 2;
         3830  +      return pA->op==TK_COLLATE ? 1 : 2;
  3789   3831       }
  3790   3832     }
  3791         -  if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
  3792         -  if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
  3793   3833     return 0;
  3794   3834   }
  3795   3835   
  3796   3836   /*
  3797   3837   ** Compare two ExprList objects.  Return 0 if they are identical and 
  3798   3838   ** non-zero if they differ in any way.
  3799   3839   **

Changes to src/fkey.c.

   507    507   
   508    508       pLeft = sqlite3Expr(db, TK_REGISTER, 0);
   509    509       if( pLeft ){
   510    510         /* Set the collation sequence and affinity of the LHS of each TK_EQ
   511    511         ** expression to the parent key column defaults.  */
   512    512         if( pIdx ){
   513    513           Column *pCol;
          514  +        const char *zColl;
   514    515           iCol = pIdx->aiColumn[i];
   515    516           pCol = &pTab->aCol[iCol];
   516    517           if( pTab->iPKey==iCol ) iCol = -1;
   517    518           pLeft->iTable = regData+iCol+1;
   518    519           pLeft->affinity = pCol->affinity;
   519         -        pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
          520  +        zColl = pCol->zColl;
          521  +        if( zColl==0 ) zColl = db->pDfltColl->zName;
          522  +        pLeft = sqlite3ExprAddCollateString(pParse, pLeft, zColl);
   520    523         }else{
   521    524           pLeft->iTable = regData;
   522    525           pLeft->affinity = SQLITE_AFF_INTEGER;
   523    526         }
   524    527       }
   525    528       iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
   526    529       assert( iCol>=0 );

Changes to src/insert.c.

  1270   1270   #ifndef SQLITE_OMIT_CHECK
  1271   1271     if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
  1272   1272       ExprList *pCheck = pTab->pCheck;
  1273   1273       pParse->ckBase = regData;
  1274   1274       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
  1275   1275       for(i=0; i<pCheck->nExpr; i++){
  1276   1276         int allOk = sqlite3VdbeMakeLabel(v);
  1277         -      Expr *pDup = sqlite3ExprDup(db, pCheck->a[i].pExpr, 0);
  1278         -      if( !db->mallocFailed ){
  1279         -        assert( pDup!=0 );
  1280         -        sqlite3ExprIfTrue(pParse, pDup, allOk, SQLITE_JUMPIFNULL);
  1281         -        if( onError==OE_Ignore ){
  1282         -          sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
         1277  +      sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
         1278  +      if( onError==OE_Ignore ){
         1279  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
         1280  +      }else{
         1281  +        char *zConsName = pCheck->a[i].zName;
         1282  +        if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
         1283  +        if( zConsName ){
         1284  +          zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
  1283   1285           }else{
  1284         -          char *zConsName = pCheck->a[i].zName;
  1285         -          if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
  1286         -          if( zConsName ){
  1287         -            zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
  1288         -          }else{
  1289         -            zConsName = 0;
  1290         -          }
  1291         -          sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC);
         1286  +          zConsName = 0;
  1292   1287           }
  1293         -        sqlite3VdbeResolveLabel(v, allOk);
         1288  +        sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC);
  1294   1289         }
  1295         -      sqlite3ExprDelete(db, pDup);
         1290  +      sqlite3VdbeResolveLabel(v, allOk);
  1296   1291       }
  1297   1292     }
  1298   1293   #endif /* !defined(SQLITE_OMIT_CHECK) */
  1299   1294   
  1300   1295     /* If we have an INTEGER PRIMARY KEY, make sure the primary key
  1301   1296     ** of the new record does not previously exist.  Except, if this
  1302   1297     ** is an UPDATE and the primary key is not changing, that is OK.

Changes to src/parse.y.

   811    811   }
   812    812   expr(A) ::= VARIABLE(X).     {
   813    813     spanExpr(&A, pParse, TK_VARIABLE, &X);
   814    814     sqlite3ExprAssignVarNumber(pParse, A.pExpr);
   815    815     spanSet(&A, &X, &X);
   816    816   }
   817    817   expr(A) ::= expr(E) COLLATE ids(C). {
   818         -  A.pExpr = sqlite3ExprSetCollByToken(pParse, E.pExpr, &C);
          818  +  A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C);
   819    819     A.zStart = E.zStart;
   820    820     A.zEnd = &C.z[C.n];
   821    821   }
   822    822   %ifndef SQLITE_OMIT_CAST
   823    823   expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
   824    824     A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T);
   825    825     spanSet(&A,&X,&Y);
................................................................................
  1136   1136   %destructor idxlist {sqlite3ExprListDelete(pParse->db, $$);}
  1137   1137   %type idxlist_opt {ExprList*}
  1138   1138   %destructor idxlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
  1139   1139   
  1140   1140   idxlist_opt(A) ::= .                         {A = 0;}
  1141   1141   idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
  1142   1142   idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z).  {
  1143         -  Expr *p = 0;
  1144         -  if( C.n>0 ){
  1145         -    p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
  1146         -    sqlite3ExprSetCollByToken(pParse, p, &C);
  1147         -  }
         1143  +  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C);
  1148   1144     A = sqlite3ExprListAppend(pParse,X, p);
  1149   1145     sqlite3ExprListSetName(pParse,A,&Y,1);
  1150   1146     sqlite3ExprListCheckLength(pParse, A, "index");
  1151   1147     if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
  1152   1148   }
  1153   1149   idxlist(A) ::= nm(Y) collate(C) sortorder(Z). {
  1154         -  Expr *p = 0;
  1155         -  if( C.n>0 ){
  1156         -    p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
  1157         -    sqlite3ExprSetCollByToken(pParse, p, &C);
  1158         -  }
         1150  +  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C);
  1159   1151     A = sqlite3ExprListAppend(pParse,0, p);
  1160   1152     sqlite3ExprListSetName(pParse, A, &Y, 1);
  1161   1153     sqlite3ExprListCheckLength(pParse, A, "index");
  1162   1154     if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
  1163   1155   }
  1164   1156   
  1165   1157   %type collate {Token}

Changes to src/resolve.c.

    63     63   ** Is equivalent to:
    64     64   **
    65     65   **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
    66     66   **
    67     67   ** The result of random()%5 in the GROUP BY clause is probably different
    68     68   ** from the result in the result-set.  We might fix this someday.  Or
    69     69   ** then again, we might not...
           70  +**
           71  +** If the reference is followed by a COLLATE operator, then make sure
           72  +** the COLLATE operator is preserved.  For example:
           73  +**
           74  +**     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
           75  +**
           76  +** Should be transformed into:
           77  +**
           78  +**     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
    70     79   **
    71     80   ** The nSubquery parameter specifies how many levels of subquery the
    72     81   ** alias is removed from the original expression.  The usually value is
    73     82   ** zero but it might be more if the alias is contained within a subquery
    74     83   ** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
    75     84   ** structures must be increased by the nSubquery amount.
    76     85   */
................................................................................
    87     96     sqlite3 *db;           /* The database connection */
    88     97   
    89     98     assert( iCol>=0 && iCol<pEList->nExpr );
    90     99     pOrig = pEList->a[iCol].pExpr;
    91    100     assert( pOrig!=0 );
    92    101     assert( pOrig->flags & EP_Resolved );
    93    102     db = pParse->db;
          103  +  pDup = sqlite3ExprDup(db, pOrig, 0);
          104  +  if( pDup==0 ) return;
    94    105     if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
    95         -    pDup = sqlite3ExprDup(db, pOrig, 0);
    96    106       incrAggFunctionDepth(pDup, nSubquery);
    97    107       pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
    98    108       if( pDup==0 ) return;
    99    109       if( pEList->a[iCol].iAlias==0 ){
   100    110         pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
   101    111       }
   102    112       pDup->iTable = pEList->a[iCol].iAlias;
   103         -  }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
   104         -    pDup = sqlite3ExprDup(db, pOrig, 0);
   105         -    if( pDup==0 ) return;
   106         -  }else{
   107         -    char *zToken = pOrig->u.zToken;
   108         -    assert( zToken!=0 );
   109         -    pOrig->u.zToken = 0;
   110         -    pDup = sqlite3ExprDup(db, pOrig, 0);
   111         -    pOrig->u.zToken = zToken;
   112         -    if( pDup==0 ) return;
   113         -    assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
   114         -    pDup->flags2 |= EP2_MallocedToken;
   115         -    pDup->u.zToken = sqlite3DbStrDup(db, zToken);
   116    113     }
   117         -  if( pExpr->flags & EP_ExpCollate ){
   118         -    pDup->pColl = pExpr->pColl;
   119         -    pDup->flags |= EP_ExpCollate;
          114  +  if( pExpr->op==TK_COLLATE ){
          115  +    pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
   120    116     }
   121    117   
   122    118     /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
   123    119     ** prevents ExprDelete() from deleting the Expr structure itself,
   124    120     ** allowing it to be repopulated by the memcpy() on the following line.
          121  +  ** The pExpr->u.zToken might point into memory that will be freed by the
          122  +  ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
          123  +  ** make a copy of the token before doing the sqlite3DbFree().
   125    124     */
   126    125     ExprSetProperty(pExpr, EP_Static);
   127    126     sqlite3ExprDelete(db, pExpr);
   128    127     memcpy(pExpr, pDup, sizeof(*pExpr));
          128  +  if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
          129  +    assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
          130  +    pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
          131  +    pExpr->flags2 |= EP2_MallocedToken;
          132  +  }
   129    133     sqlite3DbFree(db, pDup);
   130    134   }
   131    135   
   132    136   
   133    137   /*
   134    138   ** Return TRUE if the name zCol occurs anywhere in the USING clause.
   135    139   **
................................................................................
   808    812       moreToDo = 0;
   809    813       pEList = pSelect->pEList;
   810    814       assert( pEList!=0 );
   811    815       for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
   812    816         int iCol = -1;
   813    817         Expr *pE, *pDup;
   814    818         if( pItem->done ) continue;
   815         -      pE = pItem->pExpr;
          819  +      pE = sqlite3ExprSkipCollate(pItem->pExpr);
   816    820         if( sqlite3ExprIsInteger(pE, &iCol) ){
   817    821           if( iCol<=0 || iCol>pEList->nExpr ){
   818    822             resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
   819    823             return 1;
   820    824           }
   821    825         }else{
   822    826           iCol = resolveAsName(pParse, pEList, pE);
................................................................................
   826    830               assert(pDup);
   827    831               iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
   828    832             }
   829    833             sqlite3ExprDelete(db, pDup);
   830    834           }
   831    835         }
   832    836         if( iCol>0 ){
   833         -        CollSeq *pColl = pE->pColl;
   834         -        int flags = pE->flags & EP_ExpCollate;
          837  +        /* Convert the ORDER BY term into an integer column number iCol,
          838  +        ** taking care to preserve the COLLATE clause if it exists */
          839  +        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
          840  +        if( pNew==0 ) return 1;
          841  +        pNew->flags |= EP_IntValue;
          842  +        pNew->u.iValue = iCol;
          843  +        if( pItem->pExpr==pE ){
          844  +          pItem->pExpr = pNew;
          845  +        }else{
          846  +          assert( pItem->pExpr->op==TK_COLLATE );
          847  +          assert( pItem->pExpr->pLeft==pE );
          848  +          pItem->pExpr->pLeft = pNew;
          849  +        }
   835    850           sqlite3ExprDelete(db, pE);
   836         -        pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
   837         -        if( pE==0 ) return 1;
   838         -        pE->pColl = pColl;
   839         -        pE->flags |= EP_IntValue | flags;
   840         -        pE->u.iValue = iCol;
   841    851           pItem->iOrderByCol = (u16)iCol;
   842    852           pItem->done = 1;
   843    853         }else{
   844    854           moreToDo = 1;
   845    855         }
   846    856       }
   847    857       pSelect = pSelect->pNext;
................................................................................
   938    948         /* If an AS-name match is found, mark this ORDER BY column as being
   939    949         ** a copy of the iCol-th result-set column.  The subsequent call to
   940    950         ** sqlite3ResolveOrderGroupBy() will convert the expression to a
   941    951         ** copy of the iCol-th result-set expression. */
   942    952         pItem->iOrderByCol = (u16)iCol;
   943    953         continue;
   944    954       }
   945         -    if( sqlite3ExprIsInteger(pE, &iCol) ){
          955  +    if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){
   946    956         /* The ORDER BY term is an integer constant.  Again, set the column
   947    957         ** number so that sqlite3ResolveOrderGroupBy() will convert the
   948    958         ** order-by term to a copy of the result-set expression */
   949         -      if( iCol<1 ){
          959  +      if( iCol<1 || iCol>0xffff ){
   950    960           resolveOutOfRangeError(pParse, zType, i+1, nResult);
   951    961           return 1;
   952    962         }
   953    963         pItem->iOrderByCol = (u16)iCol;
   954    964         continue;
   955    965       }
   956    966   

Changes to src/select.c.

  1331   1331     }
  1332   1332     *pnCol = nCol;
  1333   1333     *paCol = aCol;
  1334   1334   
  1335   1335     for(i=0, pCol=aCol; i<nCol; i++, pCol++){
  1336   1336       /* Get an appropriate name for the column
  1337   1337       */
  1338         -    p = pEList->a[i].pExpr;
         1338  +    p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
  1339   1339       assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
  1340   1340                  || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
  1341   1341       if( (zName = pEList->a[i].zName)!=0 ){
  1342   1342         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1343   1343         zName = sqlite3DbStrDup(db, zName);
  1344   1344       }else{
  1345   1345         Expr *pColExpr = p;  /* The expression that is the result column name */
................................................................................
  2329   2329       if( pKeyMerge ){
  2330   2330         pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
  2331   2331         pKeyMerge->nField = (u16)nOrderBy;
  2332   2332         pKeyMerge->enc = ENC(db);
  2333   2333         for(i=0; i<nOrderBy; i++){
  2334   2334           CollSeq *pColl;
  2335   2335           Expr *pTerm = pOrderBy->a[i].pExpr;
  2336         -        if( pTerm->flags & EP_ExpCollate ){
  2337         -          pColl = pTerm->pColl;
         2336  +        if( pTerm->flags & EP_Collate ){
         2337  +          pColl = sqlite3ExprCollSeq(pParse, pTerm);
  2338   2338           }else{
  2339   2339             pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
  2340         -          pTerm->flags |= EP_ExpCollate;
  2341         -          pTerm->pColl = pColl;
         2340  +          if( pColl==0 ) pColl = db->pDfltColl;
         2341  +          pOrderBy->a[i].pExpr =
         2342  +             sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
  2342   2343           }
  2343   2344           pKeyMerge->aColl[i] = pColl;
  2344   2345           pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
  2345   2346         }
  2346   2347       }
  2347   2348     }else{
  2348   2349       pKeyMerge = 0;
................................................................................
  2537   2538   
  2538   2539     /* Implement the main merge loop
  2539   2540     */
  2540   2541     sqlite3VdbeResolveLabel(v, labelCmpr);
  2541   2542     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
  2542   2543     sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
  2543   2544                            (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
         2545  +  sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
  2544   2546     sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
  2545   2547   
  2546   2548     /* Release temporary registers
  2547   2549     */
  2548   2550     if( regPrev ){
  2549   2551       sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
  2550   2552     }
................................................................................
  2604   2606       if( pExpr->iColumn<0 ){
  2605   2607         pExpr->op = TK_NULL;
  2606   2608       }else{
  2607   2609         Expr *pNew;
  2608   2610         assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
  2609   2611         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
  2610   2612         pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
  2611         -      if( pNew && pExpr->pColl ){
  2612         -        pNew->pColl = pExpr->pColl;
  2613         -      }
  2614   2613         sqlite3ExprDelete(db, pExpr);
  2615   2614         pExpr = pNew;
  2616   2615       }
  2617   2616     }else{
  2618   2617       pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
  2619   2618       pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
  2620   2619       if( ExprHasProperty(pExpr, EP_xIsSelect) ){

Changes to src/sqliteInt.h.

  1674   1674   
  1675   1675     Expr *pLeft;           /* Left subnode */
  1676   1676     Expr *pRight;          /* Right subnode */
  1677   1677     union {
  1678   1678       ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
  1679   1679       Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
  1680   1680     } x;
  1681         -  CollSeq *pColl;        /* The collation type of the column or 0 */
  1682   1681   
  1683   1682     /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  1684   1683     ** space is allocated for the fields below this point. An attempt to
  1685   1684     ** access them will result in a segfault or malfunction.
  1686   1685     *********************************************************************/
  1687   1686   
  1688   1687   #if SQLITE_MAX_EXPR_DEPTH>0
................................................................................
  1710   1709   #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
  1711   1710   #define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
  1712   1711   #define EP_Error      0x0008  /* Expression contains one or more errors */
  1713   1712   #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
  1714   1713   #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
  1715   1714   #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
  1716   1715   #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
  1717         -#define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
         1716  +#define EP_Collate    0x0100  /* Tree contains a TK_COLLATE opeartor */
  1718   1717   #define EP_FixedDest  0x0200  /* Result needed in a specific register */
  1719   1718   #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
  1720   1719   #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
  1721   1720   #define EP_Hint       0x1000  /* Not used */
  1722   1721   #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
  1723   1722   #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
  1724   1723   #define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
................................................................................
  2327   2326   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
  2328   2327   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
  2329   2328   #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
  2330   2329   #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
  2331   2330   #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
  2332   2331   #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
  2333   2332   #define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
         2333  +#define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
  2334   2334   
  2335   2335   /*
  2336   2336    * Each trigger present in the database schema is stored as an instance of
  2337   2337    * struct Trigger. 
  2338   2338    *
  2339   2339    * Pointers to instances of struct Trigger are stored in two ways.
  2340   2340    * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
................................................................................
  3018   3018   u8 sqlite3HexToInt(int h);
  3019   3019   int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  3020   3020   const char *sqlite3ErrStr(int);
  3021   3021   int sqlite3ReadSchema(Parse *pParse);
  3022   3022   CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  3023   3023   CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
  3024   3024   CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
  3025         -Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
  3026         -Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
         3025  +Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
         3026  +Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
         3027  +Expr *sqlite3ExprSkipCollate(Expr*);
  3027   3028   int sqlite3CheckCollSeq(Parse *, CollSeq *);
  3028   3029   int sqlite3CheckObjectName(Parse *, const char *);
  3029   3030   void sqlite3VdbeSetChanges(sqlite3 *, int);
  3030   3031   int sqlite3AddInt64(i64*,i64);
  3031   3032   int sqlite3SubInt64(i64*,i64);
  3032   3033   int sqlite3MulInt64(i64*,i64);
  3033   3034   int sqlite3AbsInt32(int);

Changes to src/vdbe.c.

   418    418   #endif
   419    419   
   420    420   #ifdef SQLITE_DEBUG
   421    421   /*
   422    422   ** Print the value of a register for tracing purposes:
   423    423   */
   424    424   static void memTracePrint(FILE *out, Mem *p){
   425         -  if( p->flags & MEM_Null ){
          425  +  if( p->flags & MEM_Invalid ){
          426  +    fprintf(out, " undefined");
          427  +  }else if( p->flags & MEM_Null ){
   426    428       fprintf(out, " NULL");
   427    429     }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
   428    430       fprintf(out, " si:%lld", p->u.i);
   429    431     }else if( p->flags & MEM_Int ){
   430    432       fprintf(out, " i:%lld", p->u.i);
   431    433   #ifndef SQLITE_OMIT_FLOATING_POINT
   432    434     }else if( p->flags & MEM_Real ){
................................................................................
  1073   1075     n = pOp->p3;
  1074   1076     pIn1 = &aMem[pOp->p1];
  1075   1077     pOut = &aMem[pOp->p2];
  1076   1078     assert( pOut!=pIn1 );
  1077   1079     while( 1 ){
  1078   1080       sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1079   1081       Deephemeralize(pOut);
         1082  +#ifdef SQLITE_DEBUG
         1083  +    pOut->pScopyFrom = 0;
         1084  +#endif
  1080   1085       REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
  1081   1086       if( (n--)==0 ) break;
  1082   1087       pOut++;
  1083   1088       pIn1++;
  1084   1089     }
  1085   1090     break;
  1086   1091   }
................................................................................
  1883   1888   }
  1884   1889   
  1885   1890   /* Opcode: Permutation * * * P4 *
  1886   1891   **
  1887   1892   ** Set the permutation used by the OP_Compare operator to be the array
  1888   1893   ** of integers in P4.
  1889   1894   **
  1890         -** The permutation is only valid until the next OP_Permutation, OP_Compare,
  1891         -** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
  1892         -** immediately prior to the OP_Compare.
         1895  +** The permutation is only valid until the next OP_Compare that has
         1896  +** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
         1897  +** occur immediately prior to the OP_Compare.
  1893   1898   */
  1894   1899   case OP_Permutation: {
  1895   1900     assert( pOp->p4type==P4_INTARRAY );
  1896   1901     assert( pOp->p4.ai );
  1897   1902     aPermute = pOp->p4.ai;
  1898   1903     break;
  1899   1904   }
  1900   1905   
  1901         -/* Opcode: Compare P1 P2 P3 P4 *
         1906  +/* Opcode: Compare P1 P2 P3 P4 P5
  1902   1907   **
  1903   1908   ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
  1904   1909   ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
  1905   1910   ** the comparison for use by the next OP_Jump instruct.
         1911  +**
         1912  +** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
         1913  +** determined by the most recent OP_Permutation operator.  If the
         1914  +** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
         1915  +** order.
  1906   1916   **
  1907   1917   ** P4 is a KeyInfo structure that defines collating sequences and sort
  1908   1918   ** orders for the comparison.  The permutation applies to registers
  1909   1919   ** only.  The KeyInfo elements are used sequentially.
  1910   1920   **
  1911   1921   ** The comparison is a sort comparison, so NULLs compare equal,
  1912   1922   ** NULLs are less than numbers, numbers are less than strings,
................................................................................
  1918   1928     int p1;
  1919   1929     int p2;
  1920   1930     const KeyInfo *pKeyInfo;
  1921   1931     int idx;
  1922   1932     CollSeq *pColl;    /* Collating sequence to use on this term */
  1923   1933     int bRev;          /* True for DESCENDING sort order */
  1924   1934   
         1935  +  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
  1925   1936     n = pOp->p3;
  1926   1937     pKeyInfo = pOp->p4.pKeyInfo;
  1927   1938     assert( n>0 );
  1928   1939     assert( pKeyInfo!=0 );
  1929   1940     p1 = pOp->p1;
  1930   1941     p2 = pOp->p2;
  1931   1942   #if SQLITE_DEBUG

Changes to src/vdbeaux.c.

   862    862         int i, j;
   863    863         KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
   864    864         assert( pKeyInfo->aSortOrder!=0 );
   865    865         sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
   866    866         i = sqlite3Strlen30(zTemp);
   867    867         for(j=0; j<pKeyInfo->nField; j++){
   868    868           CollSeq *pColl = pKeyInfo->aColl[j];
   869         -        if( pColl ){
   870         -          int n = sqlite3Strlen30(pColl->zName);
   871         -          if( i+n>nTemp-6 ){
   872         -            memcpy(&zTemp[i],",...",4);
   873         -            break;
   874         -          }
   875         -          zTemp[i++] = ',';
   876         -          if( pKeyInfo->aSortOrder[j] ){
   877         -            zTemp[i++] = '-';
   878         -          }
   879         -          memcpy(&zTemp[i], pColl->zName,n+1);
   880         -          i += n;
   881         -        }else if( i+4<nTemp-6 ){
   882         -          memcpy(&zTemp[i],",nil",4);
   883         -          i += 4;
          869  +        const char *zColl = pColl ? pColl->zName : "nil";
          870  +        int n = sqlite3Strlen30(zColl);
          871  +        if( i+n>nTemp-6 ){
          872  +          memcpy(&zTemp[i],",...",4);
          873  +          break;
   884    874           }
          875  +        zTemp[i++] = ',';
          876  +        if( pKeyInfo->aSortOrder[j] ){
          877  +          zTemp[i++] = '-';
          878  +        }
          879  +        memcpy(&zTemp[i], zColl, n+1);
          880  +        i += n;
   885    881         }
   886    882         zTemp[i++] = ')';
   887    883         zTemp[i] = 0;
   888    884         assert( i<nTemp );
   889    885         break;
   890    886       }
   891    887       case P4_COLLSEQ: {

Changes to src/where.c.

   559    559   */
   560    560   #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
   561    561   
   562    562   /*
   563    563   ** Commute a comparison operator.  Expressions of the form "X op Y"
   564    564   ** are converted into "Y op X".
   565    565   **
   566         -** If a collation sequence is associated with either the left or right
          566  +** If left/right precendence rules come into play when determining the
          567  +** collating
   567    568   ** side of the comparison, it remains associated with the same side after
   568    569   ** the commutation. So "Y collate NOCASE op X" becomes 
   569         -** "X collate NOCASE op Y". This is because any collation sequence on
          570  +** "X op Y". This is because any collation sequence on
   570    571   ** the left hand side of a comparison overrides any collation sequence 
   571         -** attached to the right. For the same reason the EP_ExpCollate flag
          572  +** attached to the right. For the same reason the EP_Collate flag
   572    573   ** is not commuted.
   573    574   */
   574    575   static void exprCommute(Parse *pParse, Expr *pExpr){
   575         -  u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
   576         -  u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
          576  +  u16 expRight = (pExpr->pRight->flags & EP_Collate);
          577  +  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
   577    578     assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
   578         -  pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
   579         -  pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
   580         -  SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
   581         -  pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
   582         -  pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
          579  +  if( expRight==expLeft ){
          580  +    /* Either X and Y both have COLLATE operator or neither do */
          581  +    if( expRight ){
          582  +      /* Both X and Y have COLLATE operators.  Make sure X is always
          583  +      ** used by clearing the EP_Collate flag from Y. */
          584  +      pExpr->pRight->flags &= ~EP_Collate;
          585  +    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
          586  +      /* Neither X nor Y have COLLATE operators, but X has a non-default
          587  +      ** collating sequence.  So add the EP_Collate marker on X to cause
          588  +      ** it to be searched first. */
          589  +      pExpr->pLeft->flags |= EP_Collate;
          590  +    }
          591  +  }
   583    592     SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
   584    593     if( pExpr->op>=TK_GT ){
   585    594       assert( TK_LT==TK_GT+2 );
   586    595       assert( TK_GE==TK_LE+2 );
   587    596       assert( TK_GT>TK_EQ );
   588    597       assert( TK_GT<TK_LE );
   589    598       assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
................................................................................
   652    661     
   653    662             /* Figure out the collation sequence required from an index for
   654    663             ** it to be useful for optimising expression pX. Store this
   655    664             ** value in variable pColl.
   656    665             */
   657    666             assert(pX->pLeft);
   658    667             pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
   659         -          assert(pColl || pParse->nErr);
          668  +          if( pColl==0 ) pColl = pParse->db->pDfltColl;
   660    669     
   661    670             for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
   662    671               if( NEVER(j>=pIdx->nColumn) ) return 0;
   663    672             }
   664         -          if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
          673  +          if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
   665    674           }
   666    675           return pTerm;
   667    676         }
   668    677       }
   669    678     }
   670    679     return 0;
   671    680   }
................................................................................
  1175   1184     sqlite3 *db = pParse->db;        /* Database connection */
  1176   1185   
  1177   1186     if( db->mallocFailed ){
  1178   1187       return;
  1179   1188     }
  1180   1189     pTerm = &pWC->a[idxTerm];
  1181   1190     pMaskSet = pWC->pMaskSet;
  1182         -  pExpr = pTerm->pExpr;
         1191  +  pExpr = sqlite3ExprSkipCollate(pTerm->pExpr);
  1183   1192     prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
  1184   1193     op = pExpr->op;
  1185   1194     if( op==TK_IN ){
  1186   1195       assert( pExpr->pRight==0 );
  1187   1196       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1188   1197         pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
  1189   1198       }else{
................................................................................
  1202   1211                          ** on left table of a LEFT JOIN.  Ticket #3015 */
  1203   1212     }
  1204   1213     pTerm->prereqAll = prereqAll;
  1205   1214     pTerm->leftCursor = -1;
  1206   1215     pTerm->iParent = -1;
  1207   1216     pTerm->eOperator = 0;
  1208   1217     if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
  1209         -    Expr *pLeft = pExpr->pLeft;
  1210         -    Expr *pRight = pExpr->pRight;
         1218  +    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
         1219  +    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
  1211   1220       if( pLeft->op==TK_COLUMN ){
  1212   1221         pTerm->leftCursor = pLeft->iTable;
  1213   1222         pTerm->u.leftColumn = pLeft->iColumn;
  1214   1223         pTerm->eOperator = operatorMask(op);
  1215   1224       }
  1216   1225       if( pRight && pRight->op==TK_COLUMN ){
  1217   1226         WhereTerm *pNew;
................................................................................
  1231   1240           pTerm->nChild = 1;
  1232   1241           pTerm->wtFlags |= TERM_COPIED;
  1233   1242         }else{
  1234   1243           pDup = pExpr;
  1235   1244           pNew = pTerm;
  1236   1245         }
  1237   1246         exprCommute(pParse, pDup);
  1238         -      pLeft = pDup->pLeft;
         1247  +      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
  1239   1248         pNew->leftCursor = pLeft->iTable;
  1240   1249         pNew->u.leftColumn = pLeft->iColumn;
  1241   1250         testcase( (prereqLeft | extraRight) != prereqLeft );
  1242   1251         pNew->prereqRight = prereqLeft | extraRight;
  1243   1252         pNew->prereqAll = prereqAll;
  1244   1253         pNew->eOperator = operatorMask(pDup->op);
  1245   1254       }
................................................................................
  1310   1319     ){
  1311   1320       Expr *pLeft;       /* LHS of LIKE/GLOB operator */
  1312   1321       Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
  1313   1322       Expr *pNewExpr1;
  1314   1323       Expr *pNewExpr2;
  1315   1324       int idxNew1;
  1316   1325       int idxNew2;
  1317         -    CollSeq *pColl;    /* Collating sequence to use */
         1326  +    Token sCollSeqName;  /* Name of collating sequence */
  1318   1327   
  1319   1328       pLeft = pExpr->x.pList->a[1].pExpr;
  1320   1329       pStr2 = sqlite3ExprDup(db, pStr1, 0);
  1321   1330       if( !db->mallocFailed ){
  1322   1331         u8 c, *pC;       /* Last character before the first wildcard */
  1323   1332         pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
  1324   1333         c = *pC;
................................................................................
  1332   1341           if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
  1333   1342   
  1334   1343   
  1335   1344           c = sqlite3UpperToLower[c];
  1336   1345         }
  1337   1346         *pC = c + 1;
  1338   1347       }
  1339         -    pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
         1348  +    sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
         1349  +    sCollSeqName.n = 6;
         1350  +    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
  1340   1351       pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
  1341         -                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
  1342         -                     pStr1, 0);
         1352  +           sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
         1353  +           pStr1, 0);
  1343   1354       idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
  1344   1355       testcase( idxNew1==0 );
  1345   1356       exprAnalyze(pSrc, pWC, idxNew1);
         1357  +    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
  1346   1358       pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
  1347         -                     sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
  1348         -                     pStr2, 0);
         1359  +           sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
         1360  +           pStr2, 0);
  1349   1361       idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
  1350   1362       testcase( idxNew2==0 );
  1351   1363       exprAnalyze(pSrc, pWC, idxNew2);
  1352   1364       pTerm = &pWC->a[idxTerm];
  1353   1365       if( isComplete ){
  1354   1366         pWC->a[idxNew1].iParent = idxTerm;
  1355   1367         pWC->a[idxNew2].iParent = idxTerm;
................................................................................
  1459   1471     Index *pIdx,                    /* Index to match column of */
  1460   1472     int iCol                        /* Column of index to match */
  1461   1473   ){
  1462   1474     int i;
  1463   1475     const char *zColl = pIdx->azColl[iCol];
  1464   1476   
  1465   1477     for(i=0; i<pList->nExpr; i++){
  1466         -    Expr *p = pList->a[i].pExpr;
         1478  +    Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
  1467   1479       if( p->op==TK_COLUMN
  1468   1480        && p->iColumn==pIdx->aiColumn[iCol]
  1469   1481        && p->iTable==iBase
  1470   1482       ){
  1471         -      CollSeq *pColl = sqlite3ExprCollSeq(pParse, p);
         1483  +      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
  1472   1484         if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
  1473   1485           return i;
  1474   1486         }
  1475   1487       }
  1476   1488     }
  1477   1489   
  1478   1490     return -1;
................................................................................
  1511   1523     ** can be ignored. If it does not, and the column does not belong to the
  1512   1524     ** same table as index pIdx, return early. Finally, if there is no
  1513   1525     ** matching "col=X" expression and the column is on the same table as pIdx,
  1514   1526     ** set the corresponding bit in variable mask.
  1515   1527     */
  1516   1528     for(i=0; i<pDistinct->nExpr; i++){
  1517   1529       WhereTerm *pTerm;
  1518         -    Expr *p = pDistinct->a[i].pExpr;
         1530  +    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
  1519   1531       if( p->op!=TK_COLUMN ) return 0;
  1520   1532       pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
  1521   1533       if( pTerm ){
  1522   1534         Expr *pX = pTerm->pExpr;
  1523   1535         CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
  1524   1536         CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
  1525   1537         if( p1==p2 ) continue;
................................................................................
  1563   1575     pTab = pTabList->a[0].pTab;
  1564   1576   
  1565   1577     /* If any of the expressions is an IPK column on table iBase, then return 
  1566   1578     ** true. Note: The (p->iTable==iBase) part of this test may be false if the
  1567   1579     ** current SELECT is a correlated sub-query.
  1568   1580     */
  1569   1581     for(i=0; i<pDistinct->nExpr; i++){
  1570         -    Expr *p = pDistinct->a[i].pExpr;
         1582  +    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
  1571   1583       if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
  1572   1584     }
  1573   1585   
  1574   1586     /* Loop through all indices on the table, checking each to see if it makes
  1575   1587     ** the DISTINCT qualifier redundant. It does so if:
  1576   1588     **
  1577   1589     **   1. The index is itself UNIQUE, and
................................................................................
  2849   2861       int isMatch;            /* ORDER BY term matches the index term */
  2850   2862       const char *zColl;      /* Name of collating sequence for i-th index term */
  2851   2863       WhereTerm *pConstraint; /* A constraint in the WHERE clause */
  2852   2864   
  2853   2865       /* If the next term of the ORDER BY clause refers to anything other than
  2854   2866       ** a column in the "base" table, then this index will not be of any
  2855   2867       ** further use in handling the ORDER BY. */
  2856         -    pOBExpr = pOBItem->pExpr;
         2868  +    pOBExpr = sqlite3ExprSkipCollate(pOBItem->pExpr);
  2857   2869       if( pOBExpr->op!=TK_COLUMN || pOBExpr->iTable!=base ){
  2858   2870         break;
  2859   2871       }
  2860   2872   
  2861   2873       /* Find column number and collating sequence for the next entry
  2862   2874       ** in the index */
  2863   2875       if( pIdx->zName && i<pIdx->nColumn ){
................................................................................
  2875   2887       }
  2876   2888   
  2877   2889       /* Check to see if the column number and collating sequence of the
  2878   2890       ** index match the column number and collating sequence of the ORDER BY
  2879   2891       ** clause entry.  Set isMatch to 1 if they both match. */
  2880   2892       if( pOBExpr->iColumn==iColumn ){
  2881   2893         if( zColl ){
  2882         -        pColl = sqlite3ExprCollSeq(pParse, pOBExpr);
         2894  +        pColl = sqlite3ExprCollSeq(pParse, pOBItem->pExpr);
  2883   2895           if( !pColl ) pColl = db->pDfltColl;
  2884   2896           isMatch = sqlite3StrICmp(pColl->zName, zColl)==0;
  2885   2897         }else{
  2886   2898           isMatch = 1;
  2887   2899         }
  2888   2900       }else{
  2889   2901         isMatch = 0;

Changes to test/collate1.test.

    71     71   } {}
    72     72   do_test collate1-1.1 {
    73     73     execsql {
    74     74       SELECT c2 FROM collate1t1 ORDER BY 1;
    75     75     }
    76     76   } {{} 0x119 0x2D}
    77     77   do_test collate1-1.2 {
           78  +breakpoint
    78     79     execsql {
    79     80       SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex;
    80     81     }
    81     82   } {{} 0x2D 0x119}
    82     83   do_test collate1-1.3 {
    83     84     execsql {
    84     85       SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex DESC;

Changes to test/distinct.test.

   164    164     2   "b, a FROM t1"                                       {}      {B A b a}
   165    165     3   "a, b, c FROM t1"                                    {hash}  {a b c A B C}
   166    166     4   "a, b, c FROM t1 ORDER BY a, b, c"                   {btree} {A B C a b c}
   167    167     5   "b FROM t1 WHERE a = 'a'"                            {}      {b}
   168    168     6   "b FROM t1"                                          {hash}  {b B}
   169    169     7   "a FROM t1"                                          {}      {A a}
   170    170     8   "b COLLATE nocase FROM t1"                           {}      {b}
   171         -  9   "b COLLATE nocase FROM t1 ORDER BY b COLLATE nocase" {}      {B}
          171  +  9   "b COLLATE nocase FROM t1 ORDER BY b COLLATE nocase" {}      {b}
   172    172   } {
   173    173     do_execsql_test    2.$tn.1 "SELECT DISTINCT $sql" $res
   174    174     do_temptables_test 2.$tn.2 "SELECT DISTINCT $sql" $temptables
   175    175   }
   176    176   
   177    177   do_execsql_test 2.A {
   178    178     SELECT (SELECT DISTINCT o.a FROM t1 AS i) FROM t1 AS o ORDER BY rowid;

Changes to test/interrupt.test.

   162    162   for {set i 1} {$i<$max_count-5} {incr i 1} {
   163    163     do_test interrupt-4.$i.1 {
   164    164       set ::sqlite_interrupt_count $::i
   165    165       catchsql $sql
   166    166     } {1 interrupted}
   167    167   }
   168    168   
          169  +if {0} {  # This doesn't work anymore since the collation factor is
          170  +          # no longer called during schema parsing.
   169    171   # Interrupt during parsing
   170    172   #
   171    173   do_test interrupt-5.1 {
   172    174     proc fake_interrupt {args} {
   173    175       db collate fake_collation no-op
   174    176       sqlite3_interrupt db
   175    177       return SQLITE_OK
   176    178     }
   177    179     db collation_needed fake_interrupt
   178    180     catchsql {
   179    181       CREATE INDEX fake ON fake1(a COLLATE fake_collation, b, c DESC);
   180    182     }
   181    183   } {1 interrupt}
   182         -
          184  +}
   183    185   finish_test

Changes to test/shared9.test.

   131    131     set ::invoked_mycollate_db1 0
   132    132     db2 eval {
   133    133       INSERT INTO t1 VALUES('two');
   134    134     }
   135    135     db2 close
   136    136     set ::invoked_mycollate_db1
   137    137   } {0}
          138  +
          139  +forcedelete test.db test.db2
          140  +sqlite3 db1 test.db
          141  +sqlite3 db2 test.db
          142  +db1 collate mycollate mycollate_db1
          143  +db2 collate mycollate mycollate_db2
          144  +
          145  +do_test 2.13 {
          146  +  set ::invoked_mycollate_db1 0
          147  +  db1 eval {
          148  +    CREATE TABLE t1(a, CHECK (a COLLATE mycollate IN ('one', 'two', 'three')));
          149  +    INSERT INTO t1 VALUES('one');
          150  +  }
          151  +  db1 close
          152  +  set ::invoked_mycollate_db1
          153  +} {1}
          154  +do_test 2.14 {
          155  +  set ::invoked_mycollate_db1 0
          156  +  db2 eval {
          157  +    INSERT INTO t1 VALUES('two');
          158  +  }
          159  +  db2 close
          160  +  set ::invoked_mycollate_db1
          161  +} {0}
   138    162   
   139    163   #-------------------------------------------------------------------------
   140    164   # This test verifies that a bug causing a busy-handler belonging to one
   141    165   # shared-cache connection to be executed as a result of an sqlite3_step()
   142    166   # on another has been fixed.
   143    167   #
   144    168   forcedelete test.db test.db2
................................................................................
   200    224   } {}
   201    225     
   202    226   db1 close
   203    227   db2 close
   204    228   
   205    229   sqlite3_enable_shared_cache $::enable_shared_cache
   206    230   finish_test
   207         -

Changes to test/tkt2822.test.

   269    269       CREATE TABLE t7(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,
   270    270                       a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25);
   271    271     }
   272    272     catchsql {
   273    273       SELECT * FROM t7 ORDER BY 0;
   274    274     }
   275    275   } {1 {1st ORDER BY term out of range - should be between 1 and 25}}
   276         -do_test tkt2822-7.2 {
          276  +do_test tkt2822-7.2.1 {
   277    277     catchsql {
   278    278       SELECT * FROM t7 ORDER BY 1, 0;
          279  +  }
          280  +} {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
          281  +do_test tkt2822-7.2.2 {
          282  +  catchsql {
          283  +    SELECT * FROM t7 ORDER BY 1, 26;
          284  +  }
          285  +} {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
          286  +do_test tkt2822-7.2.3 {
          287  +  catchsql {
          288  +    SELECT * FROM t7 ORDER BY 1, 65536;
   279    289     }
   280    290   } {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
   281    291   do_test tkt2822-7.3 {
   282    292     catchsql {
   283    293       SELECT * FROM t7 ORDER BY 1, 2, 0;
   284    294     }
   285    295   } {1 {3rd ORDER BY term out of range - should be between 1 and 25}}