/ Check-in [4ac2bdfb]
Login

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

Overview
Comment:Additional refinements to Expr handling. Restore compression of trigger expressions. Change Expr.zToken to Expr.u.zToken and added Expr.u.iValue. Remove an unnecessary ExprDup from CHECK constraint processing. And so forth. (CVS 6682)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4ac2bdfbb4230b6ceaae87e738fa61036bbe03cb
User & Date: drh 2009-05-28 01:00:55
Context
2009-05-28
11:05
Workaround the need for internal API sqlite3BtreeCursorDb(). It was only being used for an assert() statement, which can be done differently. (CVS 6683) check-in: 5904af6e user: danielk1977 tags: trunk
01:00
Additional refinements to Expr handling. Restore compression of trigger expressions. Change Expr.zToken to Expr.u.zToken and added Expr.u.iValue. Remove an unnecessary ExprDup from CHECK constraint processing. And so forth. (CVS 6682) check-in: 4ac2bdfb user: drh tags: trunk
2009-05-27
10:31
Simplifications to the Expr object: Remove Expr.span completely and convert Expr.token into a char* Expr.zToken. Also simplify the Token object by removing the Token.dyn and Token.quoted fields. (CVS 6681) check-in: 7cb1c3ba user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that used to generate VDBE code
    13     13   ** that implements the ALTER TABLE command.
    14     14   **
    15         -** $Id: alter.c,v 1.59 2009/05/27 10:31:29 drh Exp $
           15  +** $Id: alter.c,v 1.60 2009/05/28 01:00:55 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** The code in this file only exists if we are not omitting the
    21     21   ** ALTER TABLE logic from the build.
    22     22   */

Changes to src/attach.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the ATTACH and DETACH commands.
    13     13   **
    14         -** $Id: attach.c,v 1.91 2009/05/27 10:31:29 drh Exp $
           14  +** $Id: attach.c,v 1.92 2009/05/28 01:00:55 drh Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   #ifndef SQLITE_OMIT_ATTACH
    19     19   /*
    20     20   ** Resolve an expression that was part of an ATTACH or DETACH statement. This
    21     21   ** is slightly different from resolving a normal SQL expression, because simple
................................................................................
    37     37   static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
    38     38   {
    39     39     int rc = SQLITE_OK;
    40     40     if( pExpr ){
    41     41       if( pExpr->op!=TK_ID ){
    42     42         rc = sqlite3ResolveExprNames(pName, pExpr);
    43     43         if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
    44         -        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->zToken);
           44  +        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
    45     45           return SQLITE_ERROR;
    46     46         }
    47     47       }else{
    48     48         pExpr->op = TK_STRING;
    49     49       }
    50     50     }
    51     51     return rc;
................................................................................
   308    308     ){
   309    309       pParse->nErr++;
   310    310       goto attach_end;
   311    311     }
   312    312   
   313    313   #ifndef SQLITE_OMIT_AUTHORIZATION
   314    314     if( pAuthArg ){
   315         -    char *zAuthArg = pAuthArg->zToken;
          315  +    char *zAuthArg = pAuthArg->u.zToken;
   316    316       if( zAuthArg==0 ){
   317    317         goto attach_end;
   318    318       }
   319    319       rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
   320    320       if(rc!=SQLITE_OK ){
   321    321         goto attach_end;
   322    322       }

Changes to src/build.c.

    18     18   **     CREATE INDEX
    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **
    25         -** $Id: build.c,v 1.545 2009/05/27 10:31:29 drh Exp $
           25  +** $Id: build.c,v 1.546 2009/05/28 01:00:55 drh Exp $
    26     26   */
    27     27   #include "sqliteInt.h"
    28     28   
    29     29   /*
    30     30   ** This routine is called when a new SQL statement is beginning to
    31     31   ** be parsed.  Initialize the pParse structure as needed.
    32     32   */
................................................................................
  1177   1177     Parse *pParse,    /* Parsing context */
  1178   1178     Expr *pCheckExpr  /* The check expression */
  1179   1179   ){
  1180   1180     sqlite3 *db = pParse->db;
  1181   1181   #ifndef SQLITE_OMIT_CHECK
  1182   1182     Table *pTab = pParse->pNewTable;
  1183   1183     if( pTab && !IN_DECLARE_VTAB ){
  1184         -    /* The CHECK expression must be duplicated so that tokens refer
  1185         -    ** to malloced space and not the (ephemeral) text of the CREATE TABLE
  1186         -    ** statement */
  1187         -    pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, 
  1188         -                                  sqlite3ExprDup(db, pCheckExpr, 0));
         1184  +    pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
         1185  +  }else
         1186  +#endif
         1187  +  {
         1188  +    sqlite3ExprDelete(db, pCheckExpr);
  1189   1189     }
  1190         -#endif
  1191         -  sqlite3ExprDelete(db, pCheckExpr);
  1192   1190   }
  1193   1191   
  1194   1192   /*
  1195   1193   ** Set the collation function of the most recently parsed table column
  1196   1194   ** to the CollSeq given.
  1197   1195   */
  1198   1196   void sqlite3AddCollateType(Parse *pParse, Token *pToken){

Changes to src/delete.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** in order to generate code for DELETE FROM statements.
    14     14   **
    15         -** $Id: delete.c,v 1.202 2009/05/27 10:31:29 drh Exp $
           15  +** $Id: delete.c,v 1.203 2009/05/28 01:00:55 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Look up every table that is named in pSrc.  If any table is not found,
    21     21   ** add an error message to pParse->zErrMsg and return NULL.  If all tables
    22     22   ** are found, return a pointer to the last table.

Changes to src/expr.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used for analyzing expressions and
    13     13   ** for generating VDBE code that evaluates expressions in SQLite.
    14     14   **
    15         -** $Id: expr.c,v 1.437 2009/05/27 10:31:29 drh Exp $
           15  +** $Id: expr.c,v 1.438 2009/05/28 01:00:55 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Return the 'affinity' of the expression pExpr if any.
    21     21   **
    22     22   ** If pExpr is a column, a reference to a column via an 'AS' alias,
................................................................................
    36     36     int op = pExpr->op;
    37     37     if( op==TK_SELECT ){
    38     38       assert( pExpr->flags&EP_xIsSelect );
    39     39       return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
    40     40     }
    41     41   #ifndef SQLITE_OMIT_CAST
    42     42     if( op==TK_CAST ){
    43         -    return sqlite3AffinityType(pExpr->zToken);
           43  +    assert( !ExprHasProperty(pExpr, EP_IntValue) );
           44  +    return sqlite3AffinityType(pExpr->u.zToken);
    44     45     }
    45     46   #endif
    46     47     if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
    47     48      && pExpr->pTab!=0
    48     49     ){
    49     50       /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
    50     51       ** a TK_COLUMN but was previously evaluated and cached in a register */
................................................................................
   384    385   ** is responsible for making sure the node eventually gets freed.
   385    386   **
   386    387   ** If dequote is true, then the token (if it exists) is dequoted.
   387    388   ** If dequote is false, no dequoting is performance.  The deQuote
   388    389   ** parameter is ignored if pToken is NULL or if the token does not
   389    390   ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
   390    391   ** then the EP_DblQuoted flag is set on the expression node.
          392  +**
          393  +** Special case:  If op==TK_INTEGER and pToken points to a string that
          394  +** can be translated into a 32-bit integer, then the token is not
          395  +** stored in u.zToken.  Instead, the integer values is written
          396  +** into u.iValue and the EP_IntValue flag is set.  No extra storage
          397  +** is allocated to hold the integer text and the dequote flag is ignored.
   391    398   */
   392    399   Expr *sqlite3ExprAlloc(
   393    400     sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
   394    401     int op,                 /* Expression opcode */
   395    402     const Token *pToken,    /* Token argument.  Might be NULL */
   396    403     int dequote             /* True to dequote */
   397    404   ){
   398    405     Expr *pNew;
   399         -  int nExtra;
          406  +  int nExtra = 0;
          407  +  int iValue;
   400    408   
   401    409     if( pToken ){
   402         -    nExtra = pToken->n+1;
   403         -  }else{
   404         -    nExtra = 0;
          410  +    if( op!=TK_INTEGER || pToken->z==0
          411  +          || sqlite3GetInt32(pToken->z, &iValue)==0 ){
          412  +      nExtra = pToken->n+1;
          413  +    }
   405    414     }
   406    415     pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
   407    416     if( pNew ){
   408    417       pNew->op = (u8)op;
   409    418       pNew->iAgg = -1;
   410    419       if( pToken ){
   411         -      int c;
   412         -      pNew->zToken = (char*)&pNew[1];
   413         -      memcpy(pNew->zToken, pToken->z, pToken->n);
   414         -      pNew->zToken[pToken->n] = 0;
   415         -      if( dequote && nExtra>=3 
   416         -           && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
   417         -        sqlite3Dequote(pNew->zToken);
   418         -        if( c=='"' ) pNew->flags |= EP_DblQuoted;
          420  +      if( nExtra==0 ){
          421  +        pNew->flags |= EP_IntValue;
          422  +        pNew->u.iValue = iValue;
          423  +      }else{
          424  +        int c;
          425  +        pNew->u.zToken = (char*)&pNew[1];
          426  +        memcpy(pNew->u.zToken, pToken->z, pToken->n);
          427  +        pNew->u.zToken[pToken->n] = 0;
          428  +        if( dequote && nExtra>=3 
          429  +             && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
          430  +          sqlite3Dequote(pNew->u.zToken);
          431  +          if( c=='"' ) pNew->flags |= EP_DblQuoted;
          432  +        }
   419    433         }
   420    434       }
   421    435   #if SQLITE_MAX_EXPR_DEPTH>0
   422    436       pNew->nHeight = 1;
   423    437   #endif  
   424    438     }
   425    439     return pNew;
................................................................................
   546    560   ** assigned.
   547    561   */
   548    562   void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
   549    563     sqlite3 *db = pParse->db;
   550    564     const char *z;
   551    565   
   552    566     if( pExpr==0 ) return;
   553         -  z = pExpr->zToken;
          567  +  assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
          568  +  z = pExpr->u.zToken;
   554    569     assert( z!=0 );
   555    570     assert( z[0]!=0 );
   556    571     if( z[1]==0 ){
   557    572       /* Wildcard of the form "?".  Assign the next variable number */
   558    573       assert( z[0]=='?' );
   559    574       pExpr->iTable = ++pParse->nVar;
   560    575     }else if( z[0]=='?' ){
................................................................................
   580    595       */
   581    596       int i;
   582    597       u32 n;
   583    598       n = sqlite3Strlen30(z);
   584    599       for(i=0; i<pParse->nVarExpr; i++){
   585    600         Expr *pE = pParse->apVarExpr[i];
   586    601         assert( pE!=0 );
   587         -      if( memcmp(pE->zToken, z, n)==0 && pE->zToken[n]==0 ){
          602  +      if( memcmp(pE->u.zToken, z, n)==0 && pE->u.zToken[n]==0 ){
   588    603           pExpr->iTable = pE->iTable;
   589    604           break;
   590    605         }
   591    606       }
   592    607       if( i>=pParse->nVarExpr ){
   593    608         pExpr->iTable = ++pParse->nVar;
   594    609         if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
................................................................................
   612    627   }
   613    628   
   614    629   /*
   615    630   ** Clear an expression structure without deleting the structure itself.
   616    631   ** Substructure is deleted.
   617    632   */
   618    633   void sqlite3ExprClear(sqlite3 *db, Expr *p){
          634  +  assert( p!=0 );
   619    635     if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
   620         -    if( ExprHasProperty(p, EP_Reduced) ){
   621         -      /* Subtrees are part of the same memory allocation when EP_Reduced set */
   622         -      if( p->pLeft ) sqlite3ExprClear(db, p->pLeft);
   623         -      if( p->pRight ) sqlite3ExprClear(db, p->pRight);
   624         -    }else{
   625         -      /* Subtrees are separate allocations when EP_Reduced is clear */
   626         -      sqlite3ExprDelete(db, p->pLeft);
   627         -      sqlite3ExprDelete(db, p->pRight);
   628         -      /* Sometimes the zToken is allocated separately */
   629         -      if( p->flags2 & EP2_FreeToken ) sqlite3DbFree(db, p->zToken);
          636  +    sqlite3ExprDelete(db, p->pLeft);
          637  +    sqlite3ExprDelete(db, p->pRight);
          638  +    if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
          639  +      sqlite3DbFree(db, p->u.zToken);
   630    640       }
   631         -    /* x.pSelect and x.pList are always separately allocated */
   632    641       if( ExprHasProperty(p, EP_xIsSelect) ){
   633    642         sqlite3SelectDelete(db, p->x.pSelect);
   634    643       }else{
   635    644         sqlite3ExprListDelete(db, p->x.pList);
   636    645       }
   637    646     }
   638    647   }
................................................................................
   639    648   
   640    649   /*
   641    650   ** Recursively delete an expression tree.
   642    651   */
   643    652   void sqlite3ExprDelete(sqlite3 *db, Expr *p){
   644    653     if( p==0 ) return;
   645    654     sqlite3ExprClear(db, p);
   646         -  sqlite3DbFree(db, p);
          655  +  if( !ExprHasProperty(p, EP_Static) ){
          656  +    sqlite3DbFree(db, p);
          657  +  }
   647    658   }
   648    659   
   649    660   /*
   650    661   ** Return the number of bytes allocated for the expression structure 
   651    662   ** passed as the first argument. This is always one of EXPR_FULLSIZE,
   652    663   ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
   653    664   */
................................................................................
   654    665   static int exprStructSize(Expr *p){
   655    666     if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
   656    667     if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
   657    668     return EXPR_FULLSIZE;
   658    669   }
   659    670   
   660    671   /*
   661         -** sqlite3ExprDup() has been called to create a copy of expression p with
   662         -** the EXPRDUP_XXX flags passed as the second argument. This function 
   663         -** returns the space required for the copy of the Expr structure only.
   664         -** This is always one of EXPR_FULLSIZE, EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
          672  +** The dupedExpr*Size() routines each return the number of bytes required
          673  +** to store a copy of an expression or expression tree.  They differ in
          674  +** how much of the tree is measured.
          675  +**
          676  +**     dupedExprStructSize()     Size of only the Expr structure 
          677  +**     dupedExprNodeSize()       Size of Expr + space for token
          678  +**     dupedExprSize()           Expr + token + subtree components
          679  +**
          680  +***************************************************************************
          681  +**
          682  +** The dupedExprStructSize() function returns two values OR-ed together:  
          683  +** (1) the space required for a copy of the Expr structure only and 
          684  +** (2) the EP_xxx flags that indicate what the structure size should be.
          685  +** The return values is always one of:
          686  +**
          687  +**      EXPR_FULLSIZE
          688  +**      EXPR_REDUCEDSIZE   | EP_Reduced
          689  +**      EXPR_TOKENONLYSIZE | EP_TokenOnly
          690  +**
          691  +** The size of the structure can be found by masking the return value
          692  +** of this routine with 0xfff.  The flags can be found by masking the
          693  +** return value with EP_Reduced|EP_TokenOnly.
          694  +**
          695  +** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
          696  +** (unreduced) Expr objects as they or originally constructed by the parser.
          697  +** During expression analysis, extra information is computed and moved into
          698  +** later parts of teh Expr object and that extra information might get chopped
          699  +** off if the expression is reduced.  Note also that it does not work to
          700  +** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
          701  +** to reduce a pristine expression tree from the parser.  The implementation
          702  +** of dupedExprStructSize() contain multiple assert() statements that attempt
          703  +** to enforce this constraint.
   665    704   */
   666    705   static int dupedExprStructSize(Expr *p, int flags){
   667    706     int nSize;
          707  +  assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
   668    708     if( 0==(flags&EXPRDUP_REDUCE) ){
   669    709       nSize = EXPR_FULLSIZE;
   670         -  }else if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
   671         -    nSize = EXPR_REDUCEDSIZE;
   672    710     }else{
   673         -    nSize = EXPR_TOKENONLYSIZE;
          711  +    assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
          712  +    assert( !ExprHasProperty(p, EP_FromJoin) ); 
          713  +    assert( (p->flags2 & EP2_MallocedToken)==0 );
          714  +    assert( (p->flags2 & EP2_Irreducible)==0 );
          715  +    if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
          716  +      nSize = EXPR_REDUCEDSIZE | EP_Reduced;
          717  +    }else{
          718  +      nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
          719  +    }
   674    720     }
   675    721     return nSize;
   676    722   }
   677    723   
   678    724   /*
   679         -** sqlite3ExprDup() has been called to create a copy of expression p with
   680         -** the EXPRDUP_XXX passed as the second argument. This function returns
   681         -** the space in bytes required to store the copy of the Expr structure
   682         -** and the copies of the Expr.zToken (if applicable)
   683         -** string buffers.
          725  +** This function returns the space in bytes required to store the copy 
          726  +** of the Expr structure and a copy of the Expr.u.zToken string (if that
          727  +** string is defined.)
   684    728   */
   685    729   static int dupedExprNodeSize(Expr *p, int flags){
   686         -  int nByte = dupedExprStructSize(p, flags);
   687         -  if( p->zToken ){
   688         -    nByte += sqlite3Strlen30(p->zToken)+1;
          730  +  int nByte = dupedExprStructSize(p, flags) & 0xfff;
          731  +  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
          732  +    nByte += sqlite3Strlen30(p->u.zToken)+1;
   689    733     }
   690    734     return ROUND8(nByte);
   691    735   }
   692    736   
   693    737   /*
   694    738   ** Return the number of bytes required to create a duplicate of the 
   695    739   ** expression passed as the first argument. The second argument is a
   696    740   ** mask containing EXPRDUP_XXX flags.
   697    741   **
   698    742   ** The value returned includes space to create a copy of the Expr struct
   699         -** itself and the buffer referred to by Expr.zToken, if any.
          743  +** itself and the buffer referred to by Expr.u.zToken, if any.
   700    744   **
   701    745   ** If the EXPRDUP_REDUCE flag is set, then the return value includes 
   702    746   ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 
   703    747   ** and Expr.pRight variables (but not for any structures pointed to or 
   704    748   ** descended from the Expr.x.pList or Expr.x.pSelect variables).
   705    749   */
   706    750   static int dupedExprSize(Expr *p, int flags){
................................................................................
   713    757     }
   714    758     return nByte;
   715    759   }
   716    760   
   717    761   /*
   718    762   ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
   719    763   ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
   720         -** to store the copy of expression p, the copies of p->zToken
          764  +** to store the copy of expression p, the copies of p->u.zToken
   721    765   ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
   722    766   ** if any. Before returning, *pzBuffer is set to the first byte passed the
   723    767   ** portion of the buffer copied into by this function.
   724    768   */
   725    769   static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
   726    770     Expr *pNew = 0;                      /* Value to return */
   727    771     if( p ){
   728    772       const int isReduced = (flags&EXPRDUP_REDUCE);
   729    773       u8 *zAlloc;
          774  +    u32 staticFlag = 0;
   730    775   
   731    776       assert( pzBuffer==0 || isReduced );
   732    777   
   733    778       /* Figure out where to write the new Expr structure. */
   734    779       if( pzBuffer ){
   735    780         zAlloc = *pzBuffer;
          781  +      staticFlag = EP_Static;
   736    782       }else{
   737    783         zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
   738    784       }
   739    785       pNew = (Expr *)zAlloc;
   740    786   
   741    787       if( pNew ){
   742    788         /* Set nNewSize to the size allocated for the structure pointed to
   743    789         ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
   744    790         ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
   745         -      ** by the copy of the p->zToken string (if any).
          791  +      ** by the copy of the p->u.zToken string (if any).
   746    792         */
   747         -      const int nNewSize = dupedExprStructSize(p, flags);
   748         -      const int nToken = (p->zToken ? sqlite3Strlen30(p->zToken) + 1 : 0);
          793  +      const unsigned nStructSize = dupedExprStructSize(p, flags);
          794  +      const int nNewSize = nStructSize & 0xfff;
          795  +      int nToken;
          796  +      if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
          797  +        nToken = sqlite3Strlen30(p->u.zToken) + 1;
          798  +      }else{
          799  +        nToken = 0;
          800  +      }
   749    801         if( isReduced ){
   750    802           assert( ExprHasProperty(p, EP_Reduced)==0 );
   751    803           memcpy(zAlloc, p, nNewSize);
   752    804         }else{
   753    805           int nSize = exprStructSize(p);
   754    806           memcpy(zAlloc, p, nSize);
   755    807           memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
   756    808         }
   757    809   
   758         -      /* Set the EP_Reduced and EP_TokenOnly flags appropriately. */
   759         -      pNew->flags &= ~(EP_Reduced|EP_TokenOnly);
   760         -      switch( nNewSize ){
   761         -        case EXPR_REDUCEDSIZE:   pNew->flags |= EP_Reduced; break;
   762         -        case EXPR_TOKENONLYSIZE: pNew->flags |= EP_TokenOnly; break;
   763         -      }
          810  +      /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
          811  +      pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
          812  +      pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
          813  +      pNew->flags |= staticFlag;
   764    814   
   765         -      /* Copy the p->zToken string, if any. */
          815  +      /* Copy the p->u.zToken string, if any. */
   766    816         if( nToken ){
   767         -        char *zToken = pNew->zToken = (char*)&zAlloc[nNewSize];
   768         -        memcpy(zToken, p->zToken, nToken);
          817  +        char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
          818  +        memcpy(zToken, p->u.zToken, nToken);
   769    819         }
   770    820   
   771    821         if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
   772    822           /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
   773    823           if( ExprHasProperty(p, EP_xIsSelect) ){
   774    824             pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
   775    825           }else{
................................................................................
  1176   1226   ** to fit in a 32-bit integer, return 1 and put the value of the integer
  1177   1227   ** in *pValue.  If the expression is not an integer or if it is too big
  1178   1228   ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
  1179   1229   */
  1180   1230   int sqlite3ExprIsInteger(Expr *p, int *pValue){
  1181   1231     int rc = 0;
  1182   1232     if( p->flags & EP_IntValue ){
  1183         -    *pValue = p->iTable;
         1233  +    *pValue = p->u.iValue;
  1184   1234       return 1;
  1185   1235     }
  1186   1236     switch( p->op ){
  1187   1237       case TK_INTEGER: {
  1188         -      rc = sqlite3GetInt32(p->zToken, pValue);
         1238  +      rc = sqlite3GetInt32(p->u.zToken, pValue);
         1239  +      assert( rc==0 );
  1189   1240         break;
  1190   1241       }
  1191   1242       case TK_UPLUS: {
  1192   1243         rc = sqlite3ExprIsInteger(p->pLeft, pValue);
  1193   1244         break;
  1194   1245       }
  1195   1246       case TK_UMINUS: {
................................................................................
  1199   1250           rc = 1;
  1200   1251         }
  1201   1252         break;
  1202   1253       }
  1203   1254       default: break;
  1204   1255     }
  1205   1256     if( rc ){
         1257  +    assert( ExprHasAnyProperty(p, EP_Reduced|EP_TokenOnly)
         1258  +               || (p->flags2 & EP2_MallocedToken)==0 );
  1206   1259       p->op = TK_INTEGER;
  1207   1260       p->flags |= EP_IntValue;
  1208         -    p->iTable = *pValue;
         1261  +    p->u.iValue = *pValue;
  1209   1262     }
  1210   1263     return rc;
  1211   1264   }
  1212   1265   
  1213   1266   /*
  1214   1267   ** Return TRUE if the given string is a row-id column name.
  1215   1268   */
................................................................................
  1598   1651         }
  1599   1652         sqlite3ExprDelete(pParse->db, pSel->pLimit);
  1600   1653         pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
  1601   1654         if( sqlite3Select(pParse, pSel, &dest) ){
  1602   1655           return;
  1603   1656         }
  1604   1657         pExpr->iColumn = dest.iParm;
         1658  +      ExprSetIrreducible(pExpr);
  1605   1659         break;
  1606   1660       }
  1607   1661     }
  1608   1662   
  1609   1663     if( testAddr ){
  1610   1664       sqlite3VdbeJumpHere(v, testAddr-1);
  1611   1665     }
................................................................................
  1658   1712   ** The z[] string will probably not be zero-terminated.  But the 
  1659   1713   ** z[n] character is guaranteed to be something that does not look
  1660   1714   ** like the continuation of the number.
  1661   1715   */
  1662   1716   static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){
  1663   1717     const char *z;
  1664   1718     if( pExpr->flags & EP_IntValue ){
  1665         -    int i = pExpr->iTable;
         1719  +    int i = pExpr->u.iValue;
  1666   1720       if( negFlag ) i = -i;
  1667   1721       sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
  1668         -  }else if( (z = pExpr->zToken)!=0 ){
         1722  +  }else if( (z = pExpr->u.zToken)!=0 ){
  1669   1723       int i;
  1670         -    int n = sqlite3Strlen30(pExpr->zToken);
         1724  +    int n = sqlite3Strlen30(pExpr->u.zToken);
  1671   1725       assert( !sqlite3Isdigit(z[n]) );
  1672   1726       if( sqlite3GetInt32(z, &i) ){
  1673   1727         if( negFlag ) i = -i;
  1674   1728         sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
  1675   1729       }else if( sqlite3FitsIn64Bits(z, negFlag) ){
  1676   1730         i64 value;
  1677   1731         char *zV;
................................................................................
  2074   2128         break;
  2075   2129       }
  2076   2130       case TK_INTEGER: {
  2077   2131         codeInteger(v, pExpr, 0, target);
  2078   2132         break;
  2079   2133       }
  2080   2134       case TK_FLOAT: {
  2081         -      codeReal(v, pExpr->zToken, 0, target);
         2135  +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2136  +      codeReal(v, pExpr->u.zToken, 0, target);
  2082   2137         break;
  2083   2138       }
  2084   2139       case TK_STRING: {
  2085         -      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->zToken, 0);
         2140  +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2141  +      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
  2086   2142         break;
  2087   2143       }
  2088   2144       case TK_NULL: {
  2089   2145         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
  2090   2146         break;
  2091   2147       }
  2092   2148   #ifndef SQLITE_OMIT_BLOB_LITERAL
  2093   2149       case TK_BLOB: {
  2094   2150         int n;
  2095   2151         const char *z;
  2096   2152         char *zBlob;
  2097         -      assert( pExpr->zToken[0]=='x' || pExpr->zToken[0]=='X' );
  2098         -      assert( pExpr->zToken[1]=='\'' );
  2099         -      z = &pExpr->zToken[2];
         2153  +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2154  +      assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
         2155  +      assert( pExpr->u.zToken[1]=='\'' );
         2156  +      z = &pExpr->u.zToken[2];
  2100   2157         n = sqlite3Strlen30(z) - 1;
  2101   2158         assert( z[n]=='\'' );
  2102   2159         zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
  2103   2160         sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
  2104   2161         break;
  2105   2162       }
  2106   2163   #endif
  2107   2164       case TK_VARIABLE: {
  2108   2165         int iPrior;
  2109   2166         VdbeOp *pOp;
  2110         -      assert( pExpr->zToken!=0 );
  2111         -      assert( pExpr->zToken[0]!=0 );
  2112         -      if( pExpr->zToken[1]==0
         2167  +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2168  +      assert( pExpr->u.zToken!=0 );
         2169  +      assert( pExpr->u.zToken[0]!=0 );
         2170  +      if( pExpr->u.zToken[1]==0
  2113   2171            && (iPrior = sqlite3VdbeCurrentAddr(v)-1)>=0
  2114   2172            && (pOp = sqlite3VdbeGetOp(v, iPrior))->opcode==OP_Variable
  2115   2173            && pOp->p1+pOp->p3==pExpr->iTable
  2116   2174            && pOp->p2+pOp->p3==target
  2117   2175            && pOp->p4.z==0
  2118   2176         ){
  2119   2177           /* If the previous instruction was a copy of the previous unnamed
................................................................................
  2120   2178           ** parameter into the previous register, then simply increment the
  2121   2179           ** repeat count on the prior instruction rather than making a new
  2122   2180           ** instruction.
  2123   2181           */
  2124   2182           pOp->p3++;
  2125   2183         }else{
  2126   2184           sqlite3VdbeAddOp3(v, OP_Variable, pExpr->iTable, target, 1);
  2127         -        if( pExpr->zToken[1]!=0 ){
  2128         -          sqlite3VdbeChangeP4(v, -1, pExpr->zToken, 0);
         2185  +        if( pExpr->u.zToken[1]!=0 ){
         2186  +          sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0);
  2129   2187           }
  2130   2188         }
  2131   2189         break;
  2132   2190       }
  2133   2191       case TK_REGISTER: {
  2134   2192         inReg = pExpr->iTable;
  2135   2193         break;
................................................................................
  2139   2197         break;
  2140   2198       }
  2141   2199   #ifndef SQLITE_OMIT_CAST
  2142   2200       case TK_CAST: {
  2143   2201         /* Expressions of the form:   CAST(pLeft AS token) */
  2144   2202         int aff, to_op;
  2145   2203         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  2146         -      aff = sqlite3AffinityType(pExpr->zToken);
         2204  +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2205  +      aff = sqlite3AffinityType(pExpr->u.zToken);
  2147   2206         to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
  2148   2207         assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
  2149   2208         assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
  2150   2209         assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
  2151   2210         assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
  2152   2211         assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
  2153   2212         testcase( to_op==OP_ToText );
................................................................................
  2232   2291         testcase( regFree2==0 );
  2233   2292         break;
  2234   2293       }
  2235   2294       case TK_UMINUS: {
  2236   2295         Expr *pLeft = pExpr->pLeft;
  2237   2296         assert( pLeft );
  2238   2297         if( pLeft->op==TK_FLOAT ){
  2239         -        codeReal(v, pLeft->zToken, 1, target);
         2298  +        assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2299  +        codeReal(v, pLeft->u.zToken, 1, target);
  2240   2300         }else if( pLeft->op==TK_INTEGER ){
  2241   2301           codeInteger(v, pLeft, 1, target);
  2242   2302         }else{
  2243   2303           regFree1 = r1 = sqlite3GetTempReg(pParse);
  2244   2304           sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
  2245   2305           r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
  2246   2306           sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
................................................................................
  2275   2335         sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
  2276   2336         sqlite3VdbeJumpHere(v, addr);
  2277   2337         break;
  2278   2338       }
  2279   2339       case TK_AGG_FUNCTION: {
  2280   2340         AggInfo *pInfo = pExpr->pAggInfo;
  2281   2341         if( pInfo==0 ){
  2282         -        sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->zToken);
         2342  +        assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2343  +        sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
  2283   2344         }else{
  2284   2345           inReg = pInfo->aFunc[pExpr->iAgg].iMem;
  2285   2346         }
  2286   2347         break;
  2287   2348       }
  2288   2349       case TK_CONST_FUNC:
  2289   2350       case TK_FUNCTION: {
................................................................................
  2302   2363         testcase( op==TK_FUNCTION );
  2303   2364         if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
  2304   2365           pFarg = 0;
  2305   2366         }else{
  2306   2367           pFarg = pExpr->x.pList;
  2307   2368         }
  2308   2369         nFarg = pFarg ? pFarg->nExpr : 0;
  2309         -      zId = pExpr->zToken;
         2370  +      assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2371  +      zId = pExpr->u.zToken;
  2310   2372         nId = sqlite3Strlen30(zId);
  2311   2373         pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
  2312   2374         assert( pDef!=0 );
  2313   2375         if( pFarg ){
  2314   2376           r1 = sqlite3GetTempRange(pParse, nFarg);
  2315   2377           sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
  2316   2378         }else{
................................................................................
  2585   2647                          "RAISE() may only be used within a trigger-program");
  2586   2648           return 0;
  2587   2649         }
  2588   2650         if( pExpr->affinity!=OE_Ignore ){
  2589   2651            assert( pExpr->affinity==OE_Rollback ||
  2590   2652                    pExpr->affinity == OE_Abort ||
  2591   2653                    pExpr->affinity == OE_Fail );
         2654  +         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2592   2655            sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->affinity, 0,
  2593         -                           pExpr->zToken, 0);
         2656  +                           pExpr->u.zToken, 0);
  2594   2657         } else {
  2595   2658            assert( pExpr->affinity == OE_Ignore );
  2596   2659            sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
  2597   2660            sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
  2598   2661            VdbeComment((v, "raise(IGNORE)"));
  2599   2662         }
  2600   2663         break;
................................................................................
  3111   3174   ** an incorrect TRUE could lead to a malfunction.
  3112   3175   */
  3113   3176   int sqlite3ExprCompare(Expr *pA, Expr *pB){
  3114   3177     int i;
  3115   3178     if( pA==0||pB==0 ){
  3116   3179       return pB==pA;
  3117   3180     }
         3181  +  assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
         3182  +  assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
  3118   3183     if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
  3119   3184       return 0;
  3120   3185     }
  3121   3186     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
  3122   3187     if( pA->op!=pB->op ) return 0;
  3123   3188     if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
  3124   3189     if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
................................................................................
  3131   3196         if( !sqlite3ExprCompare(pExprA, pExprB) ) return 0;
  3132   3197       }
  3133   3198     }else if( pA->x.pList || pB->x.pList ){
  3134   3199       return 0;
  3135   3200     }
  3136   3201   
  3137   3202     if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
  3138         -  if( pA->op!=TK_COLUMN && pA->zToken ){
  3139         -    if( pB->zToken==0 ) return 0;
  3140         -    if( sqlite3StrICmp(pA->zToken,pB->zToken)!=0 ){
         3203  +  if( ExprHasProperty(pA, EP_IntValue) ){
         3204  +    if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
         3205  +      return 0;
         3206  +    }
         3207  +  }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
         3208  +    if( ExprHasProperty(pB, EP_IntValue) || pB->u.zToken==0 ) return 0;
         3209  +    if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){
  3141   3210         return 0;
  3142   3211       }
  3143   3212     }
  3144   3213     return 1;
  3145   3214   }
  3146   3215   
  3147   3216   
................................................................................
  3200   3269         testcase( pExpr->op==TK_COLUMN );
  3201   3270         /* Check to see if the column is in one of the tables in the FROM
  3202   3271         ** clause of the aggregate query */
  3203   3272         if( pSrcList ){
  3204   3273           struct SrcList_item *pItem = pSrcList->a;
  3205   3274           for(i=0; i<pSrcList->nSrc; i++, pItem++){
  3206   3275             struct AggInfo_col *pCol;
         3276  +          assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
  3207   3277             if( pExpr->iTable==pItem->iCursor ){
  3208   3278               /* If we reach this point, it means that pExpr refers to a table
  3209   3279               ** that is in the FROM clause of the aggregate query.  
  3210   3280               **
  3211   3281               ** Make an entry for the column in pAggInfo->aCol[] if there
  3212   3282               ** is not an entry there already.
  3213   3283               */
................................................................................
  3248   3318                 }
  3249   3319               }
  3250   3320               /* There is now an entry for pExpr in pAggInfo->aCol[] (either
  3251   3321               ** because it was there before or because we just created it).
  3252   3322               ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
  3253   3323               ** pAggInfo->aCol[] entry.
  3254   3324               */
         3325  +            ExprSetIrreducible(pExpr);
  3255   3326               pExpr->pAggInfo = pAggInfo;
  3256   3327               pExpr->op = TK_AGG_COLUMN;
  3257   3328               pExpr->iAgg = k;
  3258   3329               break;
  3259   3330             } /* endif pExpr->iTable==pItem->iCursor */
  3260   3331           } /* end loop over pSrcList */
  3261   3332         }
................................................................................
  3280   3351             u8 enc = ENC(pParse->db);
  3281   3352             i = addAggInfoFunc(pParse->db, pAggInfo);
  3282   3353             if( i>=0 ){
  3283   3354               assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  3284   3355               pItem = &pAggInfo->aFunc[i];
  3285   3356               pItem->pExpr = pExpr;
  3286   3357               pItem->iMem = ++pParse->nMem;
         3358  +            assert( !ExprHasProperty(pExpr, EP_IntValue) );
  3287   3359               pItem->pFunc = sqlite3FindFunction(pParse->db,
  3288         -                   pExpr->zToken, sqlite3Strlen30(pExpr->zToken),
         3360  +                   pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
  3289   3361                      pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
  3290   3362               if( pExpr->flags & EP_Distinct ){
  3291   3363                 pItem->iDistinct = pParse->nTab++;
  3292   3364               }else{
  3293   3365                 pItem->iDistinct = -1;
  3294   3366               }
  3295   3367             }
  3296   3368           }
  3297   3369           /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
  3298   3370           */
         3371  +        assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
         3372  +        ExprSetIrreducible(pExpr);
  3299   3373           pExpr->iAgg = i;
  3300   3374           pExpr->pAggInfo = pAggInfo;
  3301   3375           return WRC_Prune;
  3302   3376         }
  3303   3377       }
  3304   3378     }
  3305   3379     return WRC_Continue;

Changes to src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.235 2009/05/27 10:31:29 drh Exp $
           19  +** $Id: func.c,v 1.236 2009/05/28 01:00:55 drh Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #include <stdlib.h>
    23     23   #include <assert.h>
    24     24   #include "vdbeInt.h"
    25     25   
    26     26   /*
................................................................................
  1364   1364     if( pExpr->op!=TK_FUNCTION 
  1365   1365      || !pExpr->x.pList 
  1366   1366      || pExpr->x.pList->nExpr!=2
  1367   1367     ){
  1368   1368       return 0;
  1369   1369     }
  1370   1370     assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  1371         -  pDef = sqlite3FindFunction(db, pExpr->zToken, sqlite3Strlen30(pExpr->zToken),
         1371  +  pDef = sqlite3FindFunction(db, pExpr->u.zToken, 
         1372  +                             sqlite3Strlen30(pExpr->u.zToken),
  1372   1373                                2, SQLITE_UTF8, 0);
  1373   1374     if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
  1374   1375       return 0;
  1375   1376     }
  1376   1377   
  1377   1378     /* The memcpy() statement assumes that the wildcard characters are
  1378   1379     ** the first three statements in the compareInfo structure.  The

Changes to src/parse.y.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains SQLite's grammar for SQL.  Process this file
    13     13   ** using the lemon parser generator to generate C code that runs
    14     14   ** the parser.  Lemon will also generate a header file containing
    15     15   ** numeric codes for all of the tokens.
    16     16   **
    17         -** @(#) $Id: parse.y,v 1.277 2009/05/27 10:31:29 drh Exp $
           17  +** @(#) $Id: parse.y,v 1.278 2009/05/28 01:00:55 drh Exp $
    18     18   */
    19     19   
    20     20   // All token codes are small integers with #defines that begin with "TK_"
    21     21   %token_prefix TK_
    22     22   
    23     23   // The type of the data attached to each token is Token.  This is also the
    24     24   // default type for non-terminals.

Changes to src/pragma.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the PRAGMA command.
    13     13   **
    14         -** $Id: pragma.c,v 1.210 2009/05/27 10:31:29 drh Exp $
           14  +** $Id: pragma.c,v 1.211 2009/05/28 01:00:55 drh Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   /* Ignore this whole file if pragmas are disabled
    19     19   */
    20     20   #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
    21     21   

Changes to src/resolve.c.

    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains routines used for walking the parser tree and
    14     14   ** resolve all identifiers by associating them with a particular
    15     15   ** table and column.
    16     16   **
    17         -** $Id: resolve.c,v 1.23 2009/05/27 10:31:29 drh Exp $
           17  +** $Id: resolve.c,v 1.24 2009/05/28 01:00:55 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <stdlib.h>
    21     21   #include <string.h>
    22     22   
    23     23   /*
    24     24   ** Turn the pExpr expression into an alias for the iCol-th column of the
................................................................................
    68     68       pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
    69     69       if( pDup==0 ) return;
    70     70       if( pEList->a[iCol].iAlias==0 ){
    71     71         pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
    72     72       }
    73     73       pDup->iTable = pEList->a[iCol].iAlias;
    74     74     }else{
    75         -    char *zToken = pOrig->zToken;
    76         -    pOrig->zToken = 0;
           75  +    char *zToken = pOrig->u.zToken;
           76  +    pOrig->u.zToken = 0;
    77     77       pDup = sqlite3ExprDup(db, pOrig, 0);
    78         -    pOrig->zToken = zToken;
           78  +    pOrig->u.zToken = zToken;
    79     79       if( pDup==0 ) return;
    80     80       if( zToken ){
    81     81         assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
    82         -      pDup->flags2 |= EP2_FreeToken;
    83         -      pDup->zToken = sqlite3DbStrDup(db, zToken);
           82  +      pDup->flags2 |= EP2_MallocedToken;
           83  +      pDup->u.zToken = sqlite3DbStrDup(db, zToken);
    84     84       }
    85     85     }
    86     86     if( pExpr->flags & EP_ExpCollate ){
    87     87       pDup->pColl = pExpr->pColl;
    88     88       pDup->flags |= EP_ExpCollate;
    89     89     }
    90     90     sqlite3ExprClear(db, pExpr);
................................................................................
   134    134     struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
   135    135     struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
   136    136     NameContext *pTopNC = pNC;        /* First namecontext in the list */
   137    137     Schema *pSchema = 0;              /* Schema of the expression */
   138    138   
   139    139     assert( pNC );     /* the name context cannot be NULL. */
   140    140     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
          141  +  assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
   141    142   
   142    143     /* Initialize the node to no-match */
   143    144     pExpr->iTable = -1;
   144    145     pExpr->pTab = 0;
          146  +  ExprSetIrreducible(pExpr);
   145    147   
   146    148     /* Start at the inner-most context and move outward until a match is found */
   147    149     while( pNC && cnt==0 ){
   148    150       ExprList *pEList;
   149    151       SrcList *pSrcList = pNC->pSrcList;
   150    152   
   151    153       if( pSrcList ){
................................................................................
   435    437         break;
   436    438       }
   437    439   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
   438    440   
   439    441       /* A lone identifier is the name of a column.
   440    442       */
   441    443       case TK_ID: {
   442         -      lookupName(pParse, 0, 0, pExpr->zToken, pNC, pExpr);
          444  +      lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
   443    445         return WRC_Prune;
   444    446       }
   445    447     
   446    448       /* A table name and column name:     ID.ID
   447    449       ** Or a database, table and column:  ID.ID.ID
   448    450       */
   449    451       case TK_DOT: {
................................................................................
   452    454         const char *zDb;
   453    455         Expr *pRight;
   454    456   
   455    457         /* if( pSrcList==0 ) break; */
   456    458         pRight = pExpr->pRight;
   457    459         if( pRight->op==TK_ID ){
   458    460           zDb = 0;
   459         -        zTable = pExpr->pLeft->zToken;
   460         -        zColumn = pRight->zToken;
          461  +        zTable = pExpr->pLeft->u.zToken;
          462  +        zColumn = pRight->u.zToken;
   461    463         }else{
   462    464           assert( pRight->op==TK_DOT );
   463         -        zDb = pExpr->pLeft->zToken;
   464         -        zTable = pRight->pLeft->zToken;
   465         -        zColumn = pRight->pRight->zToken;
          465  +        zDb = pExpr->pLeft->u.zToken;
          466  +        zTable = pRight->pLeft->u.zToken;
          467  +        zColumn = pRight->pRight->u.zToken;
   466    468         }
   467    469         lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
   468    470         return WRC_Prune;
   469    471       }
   470    472   
   471    473       /* Resolve function names
   472    474       */
................................................................................
   480    482         int auth;                   /* Authorization to use the function */
   481    483         int nId;                    /* Number of characters in function name */
   482    484         const char *zId;            /* The function name. */
   483    485         FuncDef *pDef;              /* Information about the function */
   484    486         u8 enc = ENC(pParse->db);   /* The database encoding */
   485    487   
   486    488         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   487         -      zId = pExpr->zToken;
          489  +      zId = pExpr->u.zToken;
   488    490         nId = sqlite3Strlen30(zId);
   489    491         pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
   490    492         if( pDef==0 ){
   491    493           pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
   492    494           if( pDef==0 ){
   493    495             no_such_func = 1;
   494    496           }else{
................................................................................
   582    584   static int resolveAsName(
   583    585     Parse *pParse,     /* Parsing context for error messages */
   584    586     ExprList *pEList,  /* List of expressions to scan */
   585    587     Expr *pE           /* Expression we are trying to match */
   586    588   ){
   587    589     int i;             /* Loop counter */
   588    590   
   589         -  if( pE->op==TK_ID || (pE->op==TK_STRING && pE->zToken[0]!='\'') ){
   590         -    char *zCol = pE->zToken;
          591  +  if( pE->op==TK_ID || (pE->op==TK_STRING && pE->u.zToken[0]!='\'') ){
          592  +    char *zCol = pE->u.zToken;
   591    593       for(i=0; i<pEList->nExpr; i++){
   592    594         char *zAs = pEList->a[i].zName;
   593    595         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
   594    596           return i+1;
   595    597         }
   596    598       }
   597    599     }
................................................................................
   744    746           CollSeq *pColl = pE->pColl;
   745    747           int flags = pE->flags & EP_ExpCollate;
   746    748           sqlite3ExprDelete(db, pE);
   747    749           pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
   748    750           if( pE==0 ) return 1;
   749    751           pE->pColl = pColl;
   750    752           pE->flags |= EP_IntValue | flags;
   751         -        pE->iTable = iCol;
          753  +        pE->u.iValue = iCol;
   752    754           pItem->iCol = (u16)iCol;
   753    755           pItem->done = 1;
   754    756         }else{
   755    757           moreToDo = 1;
   756    758         }
   757    759       }
   758    760       pSelect = pSelect->pNext;

Changes to src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.519 2009/05/27 10:31:29 drh Exp $
           15  +** $Id: select.c,v 1.520 2009/05/28 01:00:55 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Delete all the content of a Select structure but do not deallocate
    22     22   ** the select structure itself.
................................................................................
   228    228     }
   229    229     pE2b = sqlite3CreateIdExpr(pParse, zAlias2);
   230    230     pE1c = sqlite3PExpr(pParse, TK_DOT, pE1b, pE1a, 0);
   231    231     pE2c = sqlite3PExpr(pParse, TK_DOT, pE2b, pE2a, 0);
   232    232     pE = sqlite3PExpr(pParse, TK_EQ, pE1c, pE2c, 0);
   233    233     if( pE && isOuterJoin ){
   234    234       ExprSetProperty(pE, EP_FromJoin);
          235  +    assert( !ExprHasAnyProperty(pE, EP_TokenOnly|EP_Reduced) );
          236  +    ExprSetIrreducible(pE);
   235    237       pE->iRightJoinTable = iRightJoinTable;
   236    238     }
   237    239     *ppExpr = sqlite3ExprAnd(pParse->db,*ppExpr, pE);
   238    240   }
   239    241   
   240    242   /*
   241    243   ** Set the EP_FromJoin property on all terms of the given expression.
................................................................................
   262    264   ** defer the handling of t1.x=5, it will be processed immediately
   263    265   ** after the t1 loop and rows with t1.x!=5 will never appear in
   264    266   ** the output, which is incorrect.
   265    267   */
   266    268   static void setJoinExpr(Expr *p, int iTable){
   267    269     while( p ){
   268    270       ExprSetProperty(p, EP_FromJoin);
          271  +    assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
          272  +    ExprSetIrreducible(p);
   269    273       p->iRightJoinTable = iTable;
   270    274       setJoinExpr(p->pLeft, iTable);
   271    275       p = p->pRight;
   272    276     } 
   273    277   }
   274    278   
   275    279   /*
................................................................................
  1123   1127     *pnCol = nCol = pEList->nExpr;
  1124   1128     aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
  1125   1129     if( aCol==0 ) return SQLITE_NOMEM;
  1126   1130     for(i=0, pCol=aCol; i<nCol; i++, pCol++){
  1127   1131       /* Get an appropriate name for the column
  1128   1132       */
  1129   1133       p = pEList->a[i].pExpr;
  1130         -    assert( p->pRight==0 || p->pRight->zToken==0 || p->pRight->zToken[0]!=0 );
         1134  +    assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
         1135  +               || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
  1131   1136       if( (zName = pEList->a[i].zName)!=0 ){
  1132   1137         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1133   1138         zName = sqlite3DbStrDup(db, zName);
  1134   1139       }else{
  1135   1140         Expr *pColExpr = p;  /* The expression that is the result column name */
  1136   1141         Table *pTab;         /* Table associated with this expression */
  1137   1142         while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
................................................................................
  1139   1144           /* For columns use the column name name */
  1140   1145           int iCol = pColExpr->iColumn;
  1141   1146           pTab = pColExpr->pTab;
  1142   1147           if( iCol<0 ) iCol = pTab->iPKey;
  1143   1148           zName = sqlite3MPrintf(db, "%s",
  1144   1149                    iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
  1145   1150         }else if( pColExpr->op==TK_ID ){
  1146         -        zName = sqlite3MPrintf(db, "%s", pColExpr->zToken);
         1151  +        assert( !ExprHasProperty(pColExpr, EP_IntValue) );
         1152  +        zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
  1147   1153         }else{
  1148   1154           /* Use the original text of the column expression as its name */
  1149   1155           zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
  1150   1156         }
  1151   1157       }
  1152   1158       if( db->mallocFailed ){
  1153   1159         sqlite3DbFree(db, zName);
................................................................................
  2044   2050           assert( pItem->iCol>0 );
  2045   2051           if( pItem->iCol==i ) break;
  2046   2052         }
  2047   2053         if( j==nOrderBy ){
  2048   2054           Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
  2049   2055           if( pNew==0 ) return SQLITE_NOMEM;
  2050   2056           pNew->flags |= EP_IntValue;
  2051         -        pNew->iTable = i;
         2057  +        pNew->u.iValue = i;
  2052   2058           pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
  2053   2059           pOrderBy->a[nOrderBy++].iCol = (u16)i;
  2054   2060         }
  2055   2061       }
  2056   2062     }
  2057   2063   
  2058   2064     /* Compute the comparison permutation and keyinfo that is used with
................................................................................
  2892   2898     if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
  2893   2899     pExpr = pEList->a[0].pExpr;
  2894   2900     if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
  2895   2901     if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
  2896   2902     pEList = pExpr->x.pList;
  2897   2903     if( pEList==0 || pEList->nExpr!=1 ) return 0;
  2898   2904     if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
  2899         -  if( sqlite3StrICmp(pExpr->zToken,"min")==0 ){
         2905  +  assert( !ExprHasProperty(pExpr, EP_IntValue) );
         2906  +  if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
  2900   2907       return WHERE_ORDERBY_MIN;
  2901         -  }else if( sqlite3StrICmp(pExpr->zToken,"max")==0 ){
         2908  +  }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
  2902   2909       return WHERE_ORDERBY_MAX;
  2903   2910     }
  2904   2911     return WHERE_ORDERBY_NORMAL;
  2905   2912   }
  2906   2913   
  2907   2914   /*
  2908   2915   ** The select statement passed as the first argument is an aggregate query.
................................................................................
  3115   3122         }else{
  3116   3123           /* This expression is a "*" or a "TABLE.*" and needs to be
  3117   3124           ** expanded. */
  3118   3125           int tableSeen = 0;      /* Set to 1 when TABLE matches */
  3119   3126           char *zTName;            /* text of name of TABLE */
  3120   3127           if( pE->op==TK_DOT ){
  3121   3128             assert( pE->pLeft!=0 );
  3122         -          zTName = pE->pLeft->zToken;
         3129  +          assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
         3130  +          zTName = pE->pLeft->u.zToken;
  3123   3131           }else{
  3124   3132             zTName = 0;
  3125   3133           }
  3126   3134           for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  3127   3135             Table *pTab = pFrom->pTab;
  3128   3136             char *zTabName = pFrom->zAlias;
  3129   3137             if( zTabName==0 ){
................................................................................
  4139   4147   ** during the execution of complex SELECT statements.
  4140   4148   **
  4141   4149   ** These routine are not called anywhere from within the normal
  4142   4150   ** code base.  Then are intended to be called from within the debugger
  4143   4151   ** or from temporary "printf" statements inserted for debugging.
  4144   4152   */
  4145   4153   void sqlite3PrintExpr(Expr *p){
  4146         -  if( p->zToken ){
  4147         -    sqlite3DebugPrintf("(%s", p->zToken);
         4154  +  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
         4155  +    sqlite3DebugPrintf("(%s", p->u.zToken);
  4148   4156     }else{
  4149   4157       sqlite3DebugPrintf("(%d", p->op);
  4150   4158     }
  4151   4159     if( p->pLeft ){
  4152   4160       sqlite3DebugPrintf(" ");
  4153   4161       sqlite3PrintExpr(p->pLeft);
  4154   4162     }

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.876 2009/05/27 10:31:29 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.877 2009/05/28 01:00:55 drh Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Include the configuration header output by 'configure' if we're using the
    21     21   ** autoconf-based build
................................................................................
  1460   1460   **
  1461   1461   ** ALLOCATION NOTES:
  1462   1462   **
  1463   1463   ** Expr objects can use a lot of memory space in database schema.  To
  1464   1464   ** help reduce memory requirements, sometimes an Expr object will be
  1465   1465   ** truncated.  And to reduce the number of memory allocations, sometimes
  1466   1466   ** two or more Expr objects will be stored in a single memory allocation,
  1467         -** together with Expr.token strings.
         1467  +** together with Expr.zToken strings.
  1468   1468   **
  1469   1469   ** If the EP_Reduced and EP_TokenOnly flags are set when
  1470   1470   ** an Expr object is truncated.  When EP_Reduced is set, then all
  1471   1471   ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
  1472   1472   ** are contained within the same memory allocation.  Note, however, that
  1473   1473   ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
  1474   1474   ** allocated, regardless of whether or not EP_Reduced is set.
  1475   1475   */
  1476   1476   struct Expr {
  1477   1477     u8 op;                 /* Operation performed by this node */
  1478   1478     char affinity;         /* The affinity of the column or 0 if not a column */
  1479   1479     u16 flags;             /* Various flags.  EP_* See below */
  1480         -  char *zToken;          /* Token value. Zero terminated and dequoted */
         1480  +  union {
         1481  +    char *zToken;          /* Token value. Zero terminated and dequoted */
         1482  +    int iValue;            /* Integer value if EP_IntValue */
         1483  +  } u;
  1481   1484   
  1482   1485     /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  1483   1486     ** space is allocated for the fields below this point. An attempt to
  1484   1487     ** access them will result in a segfault or malfunction. 
  1485   1488     *********************************************************************/
  1486   1489   
  1487   1490     Expr *pLeft;           /* Left subnode */
................................................................................
  1494   1497   
  1495   1498     /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  1496   1499     ** space is allocated for the fields below this point. An attempt to
  1497   1500     ** access them will result in a segfault or malfunction.
  1498   1501     *********************************************************************/
  1499   1502   
  1500   1503     int iTable;            /* TK_COLUMN: cursor number of table holding column
  1501         -                         ** TK_REGISTER: register number 
  1502         -                         ** EP_IntValue: integer value */
         1504  +                         ** TK_REGISTER: register number */
  1503   1505     i16 iColumn;           /* TK_COLUMN: column index.  -1 for rowid */
  1504   1506     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  1505   1507     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
  1506   1508     u16 flags2;            /* Second set of flags.  EP2_... */
  1507   1509     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  1508   1510     Table *pTab;           /* Table for TK_COLUMN expressions. */
  1509   1511   #if SQLITE_MAX_EXPR_DEPTH>0
................................................................................
  1521   1523   #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
  1522   1524   #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
  1523   1525   #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
  1524   1526   #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
  1525   1527   #define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
  1526   1528   #define EP_AnyAff     0x0200  /* Can take a cached column of any affinity */
  1527   1529   #define EP_FixedDest  0x0400  /* Result needed in a specific register */
  1528         -#define EP_IntValue   0x0800  /* Integer value contained in iTable */
         1530  +#define EP_IntValue   0x0800  /* Integer value contained in u.iValue */
  1529   1531   #define EP_xIsSelect  0x1000  /* x.pSelect is valid (otherwise x.pList is) */
  1530   1532   
  1531   1533   #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
  1532   1534   #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
  1533   1535   #define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
  1534   1536   
  1535   1537   /*
  1536   1538   ** The following are the meanings of bits in the Expr.flags2 field.
  1537   1539   */
  1538         -#define EP2_FreeToken 0x0001  /* Need to call sqlite3DbFree() on Expr.zToken */
         1540  +#define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
         1541  +#define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
         1542  +
         1543  +/*
         1544  +** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
         1545  +** flag on an expression structure.  This flag is used for VV&A only.  The
         1546  +** routine is implemented as a macro that only works when in debugging mode,
         1547  +** so as not to burden production code.
         1548  +*/
         1549  +#ifdef SQLITE_DEBUG
         1550  +# define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
         1551  +#else
         1552  +# define ExprSetIrreducible(X)
         1553  +#endif
  1539   1554   
  1540   1555   /*
  1541   1556   ** These macros can be used to test, set, or clear bits in the 
  1542   1557   ** Expr.flags field.
  1543   1558   */
  1544   1559   #define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
  1545   1560   #define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)

Changes to src/tokenize.c.

    11     11   *************************************************************************
    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that splits an SQL input string up into
    15     15   ** individual tokens and sends those tokens one-by-one over to the
    16     16   ** parser for analysis.
    17     17   **
    18         -** $Id: tokenize.c,v 1.157 2009/05/27 10:31:29 drh Exp $
           18  +** $Id: tokenize.c,v 1.158 2009/05/28 01:00:55 drh Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include <stdlib.h>
    22     22   
    23     23   /*
    24     24   ** The charMap() macro maps alphabetic characters into their
    25     25   ** lower-case ASCII equivalent.  On ASCII machines, this is just

Changes to src/trigger.c.

     6      6   **    May you do good and not evil.
     7      7   **    May you find forgiveness for yourself and forgive others.
     8      8   **    May you share freely, never taking more than you give.
     9      9   **
    10     10   *************************************************************************
    11     11   **
    12     12   **
    13         -** $Id: trigger.c,v 1.140 2009/05/27 10:31:29 drh Exp $
           13  +** $Id: trigger.c,v 1.141 2009/05/28 01:00:55 drh Exp $
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   
    17     17   #ifndef SQLITE_OMIT_TRIGGER
    18     18   /*
    19     19   ** Delete a linked list of TriggerStep structures.
    20     20   */
................................................................................
   371    371     TriggerStep *pTriggerStep;
   372    372   
   373    373     assert(pEList == 0 || pSelect == 0);
   374    374     assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
   375    375   
   376    376     pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
   377    377     if( pTriggerStep ){
   378         -    pTriggerStep->pSelect = pSelect;
          378  +    pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
   379    379       pTriggerStep->pIdList = pColumn;
   380         -    pTriggerStep->pExprList = pEList;
          380  +    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
   381    381       pTriggerStep->orconf = orconf;
   382    382     }else{
   383    383       sqlite3IdListDelete(db, pColumn);
   384         -    sqlite3ExprListDelete(db, pEList);
   385         -    sqlite3SelectDelete(db, pSelect);
   386    384     }
          385  +  sqlite3ExprListDelete(db, pEList);
          386  +  sqlite3SelectDelete(db, pSelect);
   387    387   
   388    388     return pTriggerStep;
   389    389   }
   390    390   
   391    391   /*
   392    392   ** Construct a trigger step that implements an UPDATE statement and return
   393    393   ** a pointer to that trigger step.  The parser calls this routine when it
................................................................................
   399    399     ExprList *pEList,    /* The SET clause: list of column and new values */
   400    400     Expr *pWhere,        /* The WHERE clause */
   401    401     int orconf           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
   402    402   ){
   403    403     TriggerStep *pTriggerStep;
   404    404   
   405    405     pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
   406         -  if( pTriggerStep==0 ){
   407         -     sqlite3ExprListDelete(db, pEList);
   408         -     sqlite3ExprDelete(db, pWhere);
   409         -     return 0;
          406  +  if( pTriggerStep ){
          407  +    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
          408  +    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
          409  +    pTriggerStep->orconf = orconf;
   410    410     }
   411         -  pTriggerStep->pExprList = pEList;
   412         -  pTriggerStep->pWhere = pWhere;
   413         -  pTriggerStep->orconf = orconf;
          411  +  sqlite3ExprListDelete(db, pEList);
          412  +  sqlite3ExprDelete(db, pWhere);
   414    413     return pTriggerStep;
   415    414   }
   416    415   
   417    416   /*
   418    417   ** Construct a trigger step that implements a DELETE statement and return
   419    418   ** a pointer to that trigger step.  The parser calls this routine when it
   420    419   ** sees a DELETE statement inside the body of a CREATE TRIGGER.
................................................................................
   423    422     sqlite3 *db,            /* Database connection */
   424    423     Token *pTableName,      /* The table from which rows are deleted */
   425    424     Expr *pWhere            /* The WHERE clause */
   426    425   ){
   427    426     TriggerStep *pTriggerStep;
   428    427   
   429    428     pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
   430         -  if( pTriggerStep==0 ){
   431         -    sqlite3ExprDelete(db, pWhere);
   432         -    return 0;
          429  +  if( pTriggerStep ){
          430  +    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
          431  +    pTriggerStep->orconf = OE_Default;
   433    432     }
   434         -  pTriggerStep->pWhere = pWhere;
   435         -  pTriggerStep->orconf = OE_Default;
   436         -
          433  +  sqlite3ExprDelete(db, pWhere);
   437    434     return pTriggerStep;
   438    435   }
   439    436   
   440    437   /* 
   441    438   ** Recursively delete a Trigger structure
   442    439   */
   443    440   void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
................................................................................
   636    633   ** trigger is in TEMP in which case it can refer to any other database it
   637    634   ** wants.
   638    635   */
   639    636   static SrcList *targetSrcList(
   640    637     Parse *pParse,       /* The parsing context */
   641    638     TriggerStep *pStep   /* The trigger containing the target token */
   642    639   ){
   643         -  Token sDb;           /* Dummy database name token */
   644    640     int iDb;             /* Index of the database to use */
   645    641     SrcList *pSrc;       /* SrcList to be returned */
   646    642   
   647    643     pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
   648    644     if( pSrc ){
   649    645       assert( pSrc->nSrc>0 );
   650    646       assert( pSrc->a!=0 );

Changes to src/update.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle UPDATE statements.
    14     14   **
    15         -** $Id: update.c,v 1.201 2009/05/27 10:31:29 drh Exp $
           15  +** $Id: update.c,v 1.202 2009/05/28 01:00:55 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   #ifndef SQLITE_OMIT_VIRTUALTABLE
    20     20   /* Forward declaration */
    21     21   static void updateVirtualTable(
    22     22     Parse *pParse,       /* The parsing context */

Changes to src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.255 2009/05/27 10:31:29 drh Exp $
           17  +** $Id: util.c,v 1.256 2009/05/28 01:00:55 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <stdarg.h>
    21     21   #ifdef SQLITE_HAVE_ISNAN
    22     22   # include <math.h>
    23     23   #endif
    24     24   

Changes to src/vdbemem.c.

    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains code use to manipulate "Mem" structure.  A "Mem"
    14     14   ** stores a single value in the VDBE.  Mem is an opaque structure visible
    15     15   ** only within the VDBE.  Interface routines refer to a Mem using the
    16     16   ** name sqlite_value
    17     17   **
    18         -** $Id: vdbemem.c,v 1.145 2009/05/27 10:31:29 drh Exp $
           18  +** $Id: vdbemem.c,v 1.146 2009/05/28 01:00:55 drh Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "vdbeInt.h"
    22     22   
    23     23   /*
    24     24   ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
    25     25   ** P if required.
................................................................................
   970    970     if( !pExpr ){
   971    971       *ppVal = 0;
   972    972       return SQLITE_OK;
   973    973     }
   974    974     op = pExpr->op;
   975    975   
   976    976     if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
   977         -    zVal = sqlite3DbStrDup(db, pExpr->zToken);
   978    977       pVal = sqlite3ValueNew(db);
   979         -    if( !zVal || !pVal ) goto no_mem;
   980         -    sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
          978  +    if( pVal==0 ) goto no_mem;
          979  +    if( ExprHasProperty(pExpr, EP_IntValue) ){
          980  +      sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue);
          981  +    }else{
          982  +      zVal = sqlite3DbStrDup(db, pExpr->u.zToken);
          983  +      if( zVal==0 ) goto no_mem;
          984  +      sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
          985  +    }
   981    986       if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
   982    987         sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
   983    988       }else{
   984    989         sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
   985    990       }
   986    991       if( enc!=SQLITE_UTF8 ){
   987    992         sqlite3VdbeChangeEncoding(pVal, enc);
................................................................................
   992    997         /* (double)-1 In case of SQLITE_OMIT_FLOATING_POINT... */
   993    998         pVal->r = (double)-1 * pVal->r;
   994    999       }
   995   1000     }
   996   1001   #ifndef SQLITE_OMIT_BLOB_LITERAL
   997   1002     else if( op==TK_BLOB ){
   998   1003       int nVal;
   999         -    assert( pExpr->zToken[0]=='x' || pExpr->zToken[0]=='X' );
  1000         -    assert( pExpr->zToken[1]=='\'' );
         1004  +    assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
         1005  +    assert( pExpr->u.zToken[1]=='\'' );
  1001   1006       pVal = sqlite3ValueNew(db);
  1002   1007       if( !pVal ) goto no_mem;
  1003         -    zVal = &pExpr->zToken[2];
         1008  +    zVal = &pExpr->u.zToken[2];
  1004   1009       nVal = sqlite3Strlen30(zVal)-1;
  1005   1010       assert( zVal[nVal]=='\'' );
  1006   1011       sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
  1007   1012                            0, SQLITE_DYNAMIC);
  1008   1013     }
  1009   1014   #endif
  1010   1015   

Changes to src/walker.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used for walking the parser tree for
    13     13   ** an SQL statement.
    14     14   **
    15         -** $Id: walker.c,v 1.5 2009/05/27 10:31:29 drh Exp $
           15  +** $Id: walker.c,v 1.6 2009/05/28 01:00:55 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <stdlib.h>
    19     19   #include <string.h>
    20     20   
    21     21   
    22     22   /*

Changes to src/where.c.

    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  This module is responsible for
    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   **
    19         -** $Id: where.c,v 1.398 2009/05/27 10:31:29 drh Exp $
           19  +** $Id: where.c,v 1.399 2009/05/28 01:00:55 drh Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   
    23     23   /*
    24     24   ** Trace output macros
    25     25   */
    26     26   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
................................................................................
   654    654       /* No collation is defined for the ROWID.  Use the default. */
   655    655       pColl = db->pDfltColl;
   656    656     }
   657    657     if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) &&
   658    658         (pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){
   659    659       return 0;
   660    660     }
   661         -  z = pRight->zToken;
          661  +  z = pRight->u.zToken;
   662    662     cnt = 0;
   663    663     if( z ){
   664    664       while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
   665    665         cnt++;
   666    666       }
   667    667     }
   668    668     if( cnt==0 || c==0 || 255==(u8)z[cnt-1] ){
................................................................................
   687    687     Expr *pExpr      /* Test this expression */
   688    688   ){
   689    689     ExprList *pList;
   690    690   
   691    691     if( pExpr->op!=TK_FUNCTION ){
   692    692       return 0;
   693    693     }
   694         -  if( sqlite3StrICmp(pExpr->zToken,"match")!=0 ){
          694  +  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
   695    695       return 0;
   696    696     }
   697    697     pList = pExpr->x.pList;
   698    698     if( pList->nExpr!=2 ){
   699    699       return 0;
   700    700     }
   701    701     if( pList->a[1].pExpr->op != TK_COLUMN ){
................................................................................
  1157   1157       Expr *pLeft, *pRight;
  1158   1158       Expr *pStr1, *pStr2;
  1159   1159       Expr *pNewExpr1, *pNewExpr2;
  1160   1160       int idxNew1, idxNew2;
  1161   1161   
  1162   1162       pLeft = pExpr->x.pList->a[1].pExpr;
  1163   1163       pRight = pExpr->x.pList->a[0].pExpr;
  1164         -    pStr1 = sqlite3Expr(db, TK_STRING, pRight->zToken);
  1165         -    if( pStr1 ) pStr1->zToken[nPattern] = 0;
         1164  +    pStr1 = sqlite3Expr(db, TK_STRING, pRight->u.zToken);
         1165  +    if( pStr1 ) pStr1->u.zToken[nPattern] = 0;
  1166   1166       pStr2 = sqlite3ExprDup(db, pStr1, 0);
  1167   1167       if( !db->mallocFailed ){
  1168   1168         u8 c, *pC;
  1169         -      pC = (u8*)&pStr2->zToken[nPattern-1];
         1169  +      pC = (u8*)&pStr2->u.zToken[nPattern-1];
  1170   1170         c = *pC;
  1171   1171         if( noCase ){
  1172   1172           if( c=='@' ) isComplete = 0;
  1173   1173           c = sqlite3UpperToLower[c];
  1174   1174         }
  1175   1175         *pC = c + 1;
  1176   1176       }

Changes to test/attach.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this script is testing the ATTACH and DETACH commands
    13     13   # and related functionality.
    14     14   #
    15         -# $Id: attach.test,v 1.50 2009/05/27 10:31:30 drh Exp $
           15  +# $Id: attach.test,v 1.51 2009/05/28 01:00:56 drh Exp $
    16     16   #
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
    20     20   
    21     21   ifcapable !attach {
    22     22     finish_test

Changes to test/select1.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this file is testing the SELECT statement.
    13     13   #
    14         -# $Id: select1.test,v 1.69 2009/05/27 10:31:30 drh Exp $
           14  +# $Id: select1.test,v 1.70 2009/05/28 01:00:56 drh Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   # Try to select on a non-existant table.
    20     20   #
    21     21   do_test select1-1.1 {

Changes to test/tkt3508.test.

     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library. 
    12     12   #
    13         -# $Id: tkt3508.test,v 1.4 2009/05/27 10:31:30 drh Exp $
           13  +# $Id: tkt3508.test,v 1.5 2009/05/28 01:00:56 drh Exp $
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17     17   
    18     18   do_test tkt3508-1.1 {
    19     19     catchsql {
    20     20       CREATE TABLE modificationsTmp (