/ Check-in [6251e438]
Login

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

Overview
Comment:Merge recent enhancements from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | location-function
Files: files | file ages | folders
SHA3-256: 6251e438f2a76170fd1e95aa512a46086ed88ab93b9b97a1dba97c4558689305
User & Date: drh 2017-12-29 13:35:09
Context
2017-12-29
14:33
Enhance location(X) so that it works with indexes and WITHOUT ROWID tables. The function might return an offset to the main table or to an index, depending on whether the column X would be loaded from the main table or from the index. check-in: dd94d6a8 user: drh tags: location-function
13:35
Merge recent enhancements from trunk. check-in: 6251e438 user: drh tags: location-function
12:50
Add test cases for the undocumented behavior of duplicate columns on an INSERT or UPDATE. check-in: f4349c0c user: drh tags: trunk
2017-12-16
20:20
Add an experimental location(X) SQL function that attempt to return the location of the payload within the database for the record that contains column X. location(X) returns NULL if X is not an ordinary table column or if SQLite cannot figure out the location because it is using a covering index. check-in: 51be9558 user: drh tags: location-function
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/expert/expert1.test.

    19     19   #
    20     20   if {![info exists testdir]} {
    21     21     set testdir [file join [file dirname [info script]] .. .. test]
    22     22   }
    23     23   source $testdir/tester.tcl
    24     24   set testprefix expert1
    25     25   
    26         -if {$tcl_platform(platform)=="windows"} {
    27         -  set CMD "sqlite3_expert.exe"
    28         -} else {
    29         -  set CMD ".././sqlite3_expert"
    30         -}
           26  +set CLI [test_binary_name sqlite3]
           27  +set CMD [test_binary_name sqlite3_expert]
    31     28   
    32     29   proc squish {txt} {
    33     30     regsub -all {[[:space:]]+} $txt { }
    34     31   }
    35     32   
    36     33   proc do_setup_rec_test {tn setup sql res} {
    37     34     reset_db
................................................................................
    68     65         }
    69     66   
    70     67         $expert destroy
    71     68   
    72     69         set tst [subst -nocommands {set {} [squish [join {$result}]]}]
    73     70         uplevel [list do_test $tn $tst [string trim [squish $res]]]
    74     71       }
           72  +  }
           73  +  3 {
           74  +    if {![file executable $CLI]} { continue }
           75  +
           76  +    proc do_rec_test {tn sql res} {
           77  +      set res [squish [string trim $res]]
           78  +      set tst [subst -nocommands { 
           79  +        squish [string trim [exec $::CLI test.db ".expert" {$sql;}]]
           80  +      }]
           81  +      uplevel [list do_test $tn $tst $res]
           82  +    }
    75     83     }
    76     84   } {
    77     85   
    78     86     eval $setup
    79     87   
    80     88   
    81     89   do_setup_rec_test $tn.1 { CREATE TABLE t1(a, b, c) } {
................................................................................
   287    295   } {
   288    296     SELECT * FROM t2, t1 WHERE b=? AND d=? AND t2.rowid=t1.rowid
   289    297   } {
   290    298     CREATE INDEX t2_idx_00000064 ON t2(d);
   291    299     0|0|0|SEARCH TABLE t2 USING INDEX t2_idx_00000064 (d=?) 
   292    300     0|1|1|SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?)
   293    301   }
          302  +
          303  +do_setup_rec_test $tn.16 {
          304  +  CREATE TABLE t1(a, b);
          305  +} {
          306  +  SELECT * FROM t1 WHERE b IS NOT NULL;
          307  +} {
          308  +  (no new indexes)
          309  +  0|0|0|SCAN TABLE t1
          310  +}
   294    311   
   295    312   }
   296    313   
   297    314   proc do_candidates_test {tn sql res} {
   298    315     set res [squish [string trim $res]]
   299    316   
   300    317     set expert [sqlite3_expert_new db]

Changes to ext/expert/sqlite3expert.c.

   430    430     ExpertVtab *p = (ExpertVtab*)pVtab;
   431    431     sqlite3_free(p);
   432    432     return SQLITE_OK;
   433    433   }
   434    434   
   435    435   static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
   436    436     ExpertVtab *p = (ExpertVtab*)pVtab;
   437         -  sqlite3 *dbv = p->pExpert->dbv;
   438    437     int rc = SQLITE_OK;
   439    438     int n = 0;
   440    439     IdxScan *pScan;
   441    440     const int opmask = 
   442    441       SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
   443    442       SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
   444    443       SQLITE_INDEX_CONSTRAINT_LE;
................................................................................
   457    456         struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
   458    457         if( pCons->usable 
   459    458          && pCons->iColumn>=0 
   460    459          && p->pTab->aCol[pCons->iColumn].iPk==0
   461    460          && (pCons->op & opmask) 
   462    461         ){
   463    462           IdxConstraint *pNew;
   464         -        const char *zColl = sqlite3_vtab_collation(dbv, i);
          463  +        const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
   465    464           pNew = idxNewConstraint(&rc, zColl);
   466    465           if( pNew ){
   467    466             pNew->iCol = pCons->iColumn;
   468    467             if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
   469    468               pNew->pNext = pScan->pEq;
   470    469               pScan->pEq = pNew;
   471    470             }else{
................................................................................
  1730   1729       pNew->db = db;
  1731   1730       pNew->iSample = 100;
  1732   1731       rc = sqlite3_open(":memory:", &pNew->dbv);
  1733   1732     }
  1734   1733     if( rc==SQLITE_OK ){
  1735   1734       rc = sqlite3_open(":memory:", &pNew->dbm);
  1736   1735       if( rc==SQLITE_OK ){
  1737         -      sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_FULL_EQP, 1, (int*)0);
         1736  +      sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
  1738   1737       }
  1739   1738     }
  1740   1739     
  1741   1740   
  1742   1741     /* Copy the entire schema of database [db] into [dbm]. */
  1743   1742     if( rc==SQLITE_OK ){
  1744   1743       sqlite3_stmt *pSql;

Changes to ext/fts5/fts5_aux.c.

   353    353       if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
   354    354       if( iAdj<0 ) iAdj = 0;
   355    355       *piPos = iAdj;
   356    356     }
   357    357   
   358    358     return rc;
   359    359   }
          360  +
          361  +/*
          362  +** Return the value in pVal interpreted as utf-8 text. Except, if pVal 
          363  +** contains a NULL value, return a pointer to a static string zero
          364  +** bytes in length instead of a NULL pointer.
          365  +*/
          366  +static const char *fts5ValueToText(sqlite3_value *pVal){
          367  +  const char *zRet = (const char*)sqlite3_value_text(pVal);
          368  +  return zRet ? zRet : "";
          369  +}
   360    370   
   361    371   /*
   362    372   ** Implementation of snippet() function.
   363    373   */
   364    374   static void fts5SnippetFunction(
   365    375     const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
   366    376     Fts5Context *pFts,              /* First arg to pass to pApi functions */
................................................................................
   389    399       sqlite3_result_error(pCtx, zErr, -1);
   390    400       return;
   391    401     }
   392    402   
   393    403     nCol = pApi->xColumnCount(pFts);
   394    404     memset(&ctx, 0, sizeof(HighlightContext));
   395    405     iCol = sqlite3_value_int(apVal[0]);
   396         -  ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
   397         -  ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
   398         -  zEllips = (const char*)sqlite3_value_text(apVal[3]);
          406  +  ctx.zOpen = fts5ValueToText(apVal[1]);
          407  +  ctx.zClose = fts5ValueToText(apVal[2]);
          408  +  zEllips = fts5ValueToText(apVal[3]);
   399    409     nToken = sqlite3_value_int(apVal[4]);
   400    410   
   401    411     iBestCol = (iCol>=0 ? iCol : 0);
   402    412     nPhrase = pApi->xPhraseCount(pFts);
   403    413     aSeen = sqlite3_malloc(nPhrase);
   404    414     if( aSeen==0 ){
   405    415       rc = SQLITE_NOMEM;

Changes to ext/fts5/test/fts5af.test.

   170    170       'x a a a a a a a a a a',
   171    171       'a a a a a a a a a a a a a a a a a a a x'
   172    172     );
   173    173   }
   174    174   do_execsql_test 5.1 {
   175    175     SELECT snippet(p1, 0, '[', ']', '...', 6) FROM p1('x');
   176    176   } {{[x] a a a a a...}}
          177  +
          178  +do_execsql_test 5.2 {
          179  +  SELECT snippet(p1, 0, '[', ']', NULL, 6) FROM p1('x');
          180  +} {{[x] a a a a a}}
          181  +do_execsql_test 5.3 {
          182  +  SELECT snippet(p1, 0, NULL, ']', '...', 6) FROM p1('x');
          183  +} {{x] a a a a a...}}
          184  +do_execsql_test 5.4 {
          185  +  SELECT snippet(p1, 0, '[', NULL, '...', 6) FROM p1('x');
          186  +} {{[x a a a a a...}}
   177    187   
   178    188   } ;# foreach_detail_mode 
   179    189   
   180    190   finish_test

Changes to ext/misc/shathree.c.

    74     74   };
    75     75   
    76     76   /*
    77     77   ** A single step of the Keccak mixing function for a 1600-bit state
    78     78   */
    79     79   static void KeccakF1600Step(SHA3Context *p){
    80     80     int i;
    81         -  u64 B0, B1, B2, B3, B4;
    82         -  u64 C0, C1, C2, C3, C4;
    83         -  u64 D0, D1, D2, D3, D4;
           81  +  u64 b0, b1, b2, b3, b4;
           82  +  u64 c0, c1, c2, c3, c4;
           83  +  u64 d0, d1, d2, d3, d4;
    84     84     static const u64 RC[] = {
    85     85       0x0000000000000001ULL,  0x0000000000008082ULL,
    86     86       0x800000000000808aULL,  0x8000000080008000ULL,
    87     87       0x000000000000808bULL,  0x0000000080000001ULL,
    88     88       0x8000000080008081ULL,  0x8000000000008009ULL,
    89     89       0x000000000000008aULL,  0x0000000000000088ULL,
    90     90       0x0000000080008009ULL,  0x000000008000000aULL,
................................................................................
    91     91       0x000000008000808bULL,  0x800000000000008bULL,
    92     92       0x8000000000008089ULL,  0x8000000000008003ULL,
    93     93       0x8000000000008002ULL,  0x8000000000000080ULL,
    94     94       0x000000000000800aULL,  0x800000008000000aULL,
    95     95       0x8000000080008081ULL,  0x8000000000008080ULL,
    96     96       0x0000000080000001ULL,  0x8000000080008008ULL
    97     97     };
    98         -# define A00 (p->u.s[0])
    99         -# define A01 (p->u.s[1])
   100         -# define A02 (p->u.s[2])
   101         -# define A03 (p->u.s[3])
   102         -# define A04 (p->u.s[4])
   103         -# define A10 (p->u.s[5])
   104         -# define A11 (p->u.s[6])
   105         -# define A12 (p->u.s[7])
   106         -# define A13 (p->u.s[8])
   107         -# define A14 (p->u.s[9])
   108         -# define A20 (p->u.s[10])
   109         -# define A21 (p->u.s[11])
   110         -# define A22 (p->u.s[12])
   111         -# define A23 (p->u.s[13])
   112         -# define A24 (p->u.s[14])
   113         -# define A30 (p->u.s[15])
   114         -# define A31 (p->u.s[16])
   115         -# define A32 (p->u.s[17])
   116         -# define A33 (p->u.s[18])
   117         -# define A34 (p->u.s[19])
   118         -# define A40 (p->u.s[20])
   119         -# define A41 (p->u.s[21])
   120         -# define A42 (p->u.s[22])
   121         -# define A43 (p->u.s[23])
   122         -# define A44 (p->u.s[24])
           98  +# define a00 (p->u.s[0])
           99  +# define a01 (p->u.s[1])
          100  +# define a02 (p->u.s[2])
          101  +# define a03 (p->u.s[3])
          102  +# define a04 (p->u.s[4])
          103  +# define a10 (p->u.s[5])
          104  +# define a11 (p->u.s[6])
          105  +# define a12 (p->u.s[7])
          106  +# define a13 (p->u.s[8])
          107  +# define a14 (p->u.s[9])
          108  +# define a20 (p->u.s[10])
          109  +# define a21 (p->u.s[11])
          110  +# define a22 (p->u.s[12])
          111  +# define a23 (p->u.s[13])
          112  +# define a24 (p->u.s[14])
          113  +# define a30 (p->u.s[15])
          114  +# define a31 (p->u.s[16])
          115  +# define a32 (p->u.s[17])
          116  +# define a33 (p->u.s[18])
          117  +# define a34 (p->u.s[19])
          118  +# define a40 (p->u.s[20])
          119  +# define a41 (p->u.s[21])
          120  +# define a42 (p->u.s[22])
          121  +# define a43 (p->u.s[23])
          122  +# define a44 (p->u.s[24])
   123    123   # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
   124    124   
   125    125     for(i=0; i<24; i+=4){
   126         -    C0 = A00^A10^A20^A30^A40;
   127         -    C1 = A01^A11^A21^A31^A41;
   128         -    C2 = A02^A12^A22^A32^A42;
   129         -    C3 = A03^A13^A23^A33^A43;
   130         -    C4 = A04^A14^A24^A34^A44;
   131         -    D0 = C4^ROL64(C1, 1);
   132         -    D1 = C0^ROL64(C2, 1);
   133         -    D2 = C1^ROL64(C3, 1);
   134         -    D3 = C2^ROL64(C4, 1);
   135         -    D4 = C3^ROL64(C0, 1);
   136         -
   137         -    B0 = (A00^D0);
   138         -    B1 = ROL64((A11^D1), 44);
   139         -    B2 = ROL64((A22^D2), 43);
   140         -    B3 = ROL64((A33^D3), 21);
   141         -    B4 = ROL64((A44^D4), 14);
   142         -    A00 =   B0 ^((~B1)&  B2 );
   143         -    A00 ^= RC[i];
   144         -    A11 =   B1 ^((~B2)&  B3 );
   145         -    A22 =   B2 ^((~B3)&  B4 );
   146         -    A33 =   B3 ^((~B4)&  B0 );
   147         -    A44 =   B4 ^((~B0)&  B1 );
   148         -
   149         -    B2 = ROL64((A20^D0), 3);
   150         -    B3 = ROL64((A31^D1), 45);
   151         -    B4 = ROL64((A42^D2), 61);
   152         -    B0 = ROL64((A03^D3), 28);
   153         -    B1 = ROL64((A14^D4), 20);
   154         -    A20 =   B0 ^((~B1)&  B2 );
   155         -    A31 =   B1 ^((~B2)&  B3 );
   156         -    A42 =   B2 ^((~B3)&  B4 );
   157         -    A03 =   B3 ^((~B4)&  B0 );
   158         -    A14 =   B4 ^((~B0)&  B1 );
   159         -
   160         -    B4 = ROL64((A40^D0), 18);
   161         -    B0 = ROL64((A01^D1), 1);
   162         -    B1 = ROL64((A12^D2), 6);
   163         -    B2 = ROL64((A23^D3), 25);
   164         -    B3 = ROL64((A34^D4), 8);
   165         -    A40 =   B0 ^((~B1)&  B2 );
   166         -    A01 =   B1 ^((~B2)&  B3 );
   167         -    A12 =   B2 ^((~B3)&  B4 );
   168         -    A23 =   B3 ^((~B4)&  B0 );
   169         -    A34 =   B4 ^((~B0)&  B1 );
   170         -
   171         -    B1 = ROL64((A10^D0), 36);
   172         -    B2 = ROL64((A21^D1), 10);
   173         -    B3 = ROL64((A32^D2), 15);
   174         -    B4 = ROL64((A43^D3), 56);
   175         -    B0 = ROL64((A04^D4), 27);
   176         -    A10 =   B0 ^((~B1)&  B2 );
   177         -    A21 =   B1 ^((~B2)&  B3 );
   178         -    A32 =   B2 ^((~B3)&  B4 );
   179         -    A43 =   B3 ^((~B4)&  B0 );
   180         -    A04 =   B4 ^((~B0)&  B1 );
   181         -
   182         -    B3 = ROL64((A30^D0), 41);
   183         -    B4 = ROL64((A41^D1), 2);
   184         -    B0 = ROL64((A02^D2), 62);
   185         -    B1 = ROL64((A13^D3), 55);
   186         -    B2 = ROL64((A24^D4), 39);
   187         -    A30 =   B0 ^((~B1)&  B2 );
   188         -    A41 =   B1 ^((~B2)&  B3 );
   189         -    A02 =   B2 ^((~B3)&  B4 );
   190         -    A13 =   B3 ^((~B4)&  B0 );
   191         -    A24 =   B4 ^((~B0)&  B1 );
          126  +    c0 = a00^a10^a20^a30^a40;
          127  +    c1 = a01^a11^a21^a31^a41;
          128  +    c2 = a02^a12^a22^a32^a42;
          129  +    c3 = a03^a13^a23^a33^a43;
          130  +    c4 = a04^a14^a24^a34^a44;
          131  +    d0 = c4^ROL64(c1, 1);
          132  +    d1 = c0^ROL64(c2, 1);
          133  +    d2 = c1^ROL64(c3, 1);
          134  +    d3 = c2^ROL64(c4, 1);
          135  +    d4 = c3^ROL64(c0, 1);
          136  +
          137  +    b0 = (a00^d0);
          138  +    b1 = ROL64((a11^d1), 44);
          139  +    b2 = ROL64((a22^d2), 43);
          140  +    b3 = ROL64((a33^d3), 21);
          141  +    b4 = ROL64((a44^d4), 14);
          142  +    a00 =   b0 ^((~b1)&  b2 );
          143  +    a00 ^= RC[i];
          144  +    a11 =   b1 ^((~b2)&  b3 );
          145  +    a22 =   b2 ^((~b3)&  b4 );
          146  +    a33 =   b3 ^((~b4)&  b0 );
          147  +    a44 =   b4 ^((~b0)&  b1 );
          148  +
          149  +    b2 = ROL64((a20^d0), 3);
          150  +    b3 = ROL64((a31^d1), 45);
          151  +    b4 = ROL64((a42^d2), 61);
          152  +    b0 = ROL64((a03^d3), 28);
          153  +    b1 = ROL64((a14^d4), 20);
          154  +    a20 =   b0 ^((~b1)&  b2 );
          155  +    a31 =   b1 ^((~b2)&  b3 );
          156  +    a42 =   b2 ^((~b3)&  b4 );
          157  +    a03 =   b3 ^((~b4)&  b0 );
          158  +    a14 =   b4 ^((~b0)&  b1 );
          159  +
          160  +    b4 = ROL64((a40^d0), 18);
          161  +    b0 = ROL64((a01^d1), 1);
          162  +    b1 = ROL64((a12^d2), 6);
          163  +    b2 = ROL64((a23^d3), 25);
          164  +    b3 = ROL64((a34^d4), 8);
          165  +    a40 =   b0 ^((~b1)&  b2 );
          166  +    a01 =   b1 ^((~b2)&  b3 );
          167  +    a12 =   b2 ^((~b3)&  b4 );
          168  +    a23 =   b3 ^((~b4)&  b0 );
          169  +    a34 =   b4 ^((~b0)&  b1 );
          170  +
          171  +    b1 = ROL64((a10^d0), 36);
          172  +    b2 = ROL64((a21^d1), 10);
          173  +    b3 = ROL64((a32^d2), 15);
          174  +    b4 = ROL64((a43^d3), 56);
          175  +    b0 = ROL64((a04^d4), 27);
          176  +    a10 =   b0 ^((~b1)&  b2 );
          177  +    a21 =   b1 ^((~b2)&  b3 );
          178  +    a32 =   b2 ^((~b3)&  b4 );
          179  +    a43 =   b3 ^((~b4)&  b0 );
          180  +    a04 =   b4 ^((~b0)&  b1 );
          181  +
          182  +    b3 = ROL64((a30^d0), 41);
          183  +    b4 = ROL64((a41^d1), 2);
          184  +    b0 = ROL64((a02^d2), 62);
          185  +    b1 = ROL64((a13^d3), 55);
          186  +    b2 = ROL64((a24^d4), 39);
          187  +    a30 =   b0 ^((~b1)&  b2 );
          188  +    a41 =   b1 ^((~b2)&  b3 );
          189  +    a02 =   b2 ^((~b3)&  b4 );
          190  +    a13 =   b3 ^((~b4)&  b0 );
          191  +    a24 =   b4 ^((~b0)&  b1 );
          192  +
          193  +    c0 = a00^a20^a40^a10^a30;
          194  +    c1 = a11^a31^a01^a21^a41;
          195  +    c2 = a22^a42^a12^a32^a02;
          196  +    c3 = a33^a03^a23^a43^a13;
          197  +    c4 = a44^a14^a34^a04^a24;
          198  +    d0 = c4^ROL64(c1, 1);
          199  +    d1 = c0^ROL64(c2, 1);
          200  +    d2 = c1^ROL64(c3, 1);
          201  +    d3 = c2^ROL64(c4, 1);
          202  +    d4 = c3^ROL64(c0, 1);
          203  +
          204  +    b0 = (a00^d0);
          205  +    b1 = ROL64((a31^d1), 44);
          206  +    b2 = ROL64((a12^d2), 43);
          207  +    b3 = ROL64((a43^d3), 21);
          208  +    b4 = ROL64((a24^d4), 14);
          209  +    a00 =   b0 ^((~b1)&  b2 );
          210  +    a00 ^= RC[i+1];
          211  +    a31 =   b1 ^((~b2)&  b3 );
          212  +    a12 =   b2 ^((~b3)&  b4 );
          213  +    a43 =   b3 ^((~b4)&  b0 );
          214  +    a24 =   b4 ^((~b0)&  b1 );
          215  +
          216  +    b2 = ROL64((a40^d0), 3);
          217  +    b3 = ROL64((a21^d1), 45);
          218  +    b4 = ROL64((a02^d2), 61);
          219  +    b0 = ROL64((a33^d3), 28);
          220  +    b1 = ROL64((a14^d4), 20);
          221  +    a40 =   b0 ^((~b1)&  b2 );
          222  +    a21 =   b1 ^((~b2)&  b3 );
          223  +    a02 =   b2 ^((~b3)&  b4 );
          224  +    a33 =   b3 ^((~b4)&  b0 );
          225  +    a14 =   b4 ^((~b0)&  b1 );
          226  +
          227  +    b4 = ROL64((a30^d0), 18);
          228  +    b0 = ROL64((a11^d1), 1);
          229  +    b1 = ROL64((a42^d2), 6);
          230  +    b2 = ROL64((a23^d3), 25);
          231  +    b3 = ROL64((a04^d4), 8);
          232  +    a30 =   b0 ^((~b1)&  b2 );
          233  +    a11 =   b1 ^((~b2)&  b3 );
          234  +    a42 =   b2 ^((~b3)&  b4 );
          235  +    a23 =   b3 ^((~b4)&  b0 );
          236  +    a04 =   b4 ^((~b0)&  b1 );
          237  +
          238  +    b1 = ROL64((a20^d0), 36);
          239  +    b2 = ROL64((a01^d1), 10);
          240  +    b3 = ROL64((a32^d2), 15);
          241  +    b4 = ROL64((a13^d3), 56);
          242  +    b0 = ROL64((a44^d4), 27);
          243  +    a20 =   b0 ^((~b1)&  b2 );
          244  +    a01 =   b1 ^((~b2)&  b3 );
          245  +    a32 =   b2 ^((~b3)&  b4 );
          246  +    a13 =   b3 ^((~b4)&  b0 );
          247  +    a44 =   b4 ^((~b0)&  b1 );
          248  +
          249  +    b3 = ROL64((a10^d0), 41);
          250  +    b4 = ROL64((a41^d1), 2);
          251  +    b0 = ROL64((a22^d2), 62);
          252  +    b1 = ROL64((a03^d3), 55);
          253  +    b2 = ROL64((a34^d4), 39);
          254  +    a10 =   b0 ^((~b1)&  b2 );
          255  +    a41 =   b1 ^((~b2)&  b3 );
          256  +    a22 =   b2 ^((~b3)&  b4 );
          257  +    a03 =   b3 ^((~b4)&  b0 );
          258  +    a34 =   b4 ^((~b0)&  b1 );
          259  +
          260  +    c0 = a00^a40^a30^a20^a10;
          261  +    c1 = a31^a21^a11^a01^a41;
          262  +    c2 = a12^a02^a42^a32^a22;
          263  +    c3 = a43^a33^a23^a13^a03;
          264  +    c4 = a24^a14^a04^a44^a34;
          265  +    d0 = c4^ROL64(c1, 1);
          266  +    d1 = c0^ROL64(c2, 1);
          267  +    d2 = c1^ROL64(c3, 1);
          268  +    d3 = c2^ROL64(c4, 1);
          269  +    d4 = c3^ROL64(c0, 1);
          270  +
          271  +    b0 = (a00^d0);
          272  +    b1 = ROL64((a21^d1), 44);
          273  +    b2 = ROL64((a42^d2), 43);
          274  +    b3 = ROL64((a13^d3), 21);
          275  +    b4 = ROL64((a34^d4), 14);
          276  +    a00 =   b0 ^((~b1)&  b2 );
          277  +    a00 ^= RC[i+2];
          278  +    a21 =   b1 ^((~b2)&  b3 );
          279  +    a42 =   b2 ^((~b3)&  b4 );
          280  +    a13 =   b3 ^((~b4)&  b0 );
          281  +    a34 =   b4 ^((~b0)&  b1 );
          282  +
          283  +    b2 = ROL64((a30^d0), 3);
          284  +    b3 = ROL64((a01^d1), 45);
          285  +    b4 = ROL64((a22^d2), 61);
          286  +    b0 = ROL64((a43^d3), 28);
          287  +    b1 = ROL64((a14^d4), 20);
          288  +    a30 =   b0 ^((~b1)&  b2 );
          289  +    a01 =   b1 ^((~b2)&  b3 );
          290  +    a22 =   b2 ^((~b3)&  b4 );
          291  +    a43 =   b3 ^((~b4)&  b0 );
          292  +    a14 =   b4 ^((~b0)&  b1 );
          293  +
          294  +    b4 = ROL64((a10^d0), 18);
          295  +    b0 = ROL64((a31^d1), 1);
          296  +    b1 = ROL64((a02^d2), 6);
          297  +    b2 = ROL64((a23^d3), 25);
          298  +    b3 = ROL64((a44^d4), 8);
          299  +    a10 =   b0 ^((~b1)&  b2 );
          300  +    a31 =   b1 ^((~b2)&  b3 );
          301  +    a02 =   b2 ^((~b3)&  b4 );
          302  +    a23 =   b3 ^((~b4)&  b0 );
          303  +    a44 =   b4 ^((~b0)&  b1 );
          304  +
          305  +    b1 = ROL64((a40^d0), 36);
          306  +    b2 = ROL64((a11^d1), 10);
          307  +    b3 = ROL64((a32^d2), 15);
          308  +    b4 = ROL64((a03^d3), 56);
          309  +    b0 = ROL64((a24^d4), 27);
          310  +    a40 =   b0 ^((~b1)&  b2 );
          311  +    a11 =   b1 ^((~b2)&  b3 );
          312  +    a32 =   b2 ^((~b3)&  b4 );
          313  +    a03 =   b3 ^((~b4)&  b0 );
          314  +    a24 =   b4 ^((~b0)&  b1 );
          315  +
          316  +    b3 = ROL64((a20^d0), 41);
          317  +    b4 = ROL64((a41^d1), 2);
          318  +    b0 = ROL64((a12^d2), 62);
          319  +    b1 = ROL64((a33^d3), 55);
          320  +    b2 = ROL64((a04^d4), 39);
          321  +    a20 =   b0 ^((~b1)&  b2 );
          322  +    a41 =   b1 ^((~b2)&  b3 );
          323  +    a12 =   b2 ^((~b3)&  b4 );
          324  +    a33 =   b3 ^((~b4)&  b0 );
          325  +    a04 =   b4 ^((~b0)&  b1 );
          326  +
          327  +    c0 = a00^a30^a10^a40^a20;
          328  +    c1 = a21^a01^a31^a11^a41;
          329  +    c2 = a42^a22^a02^a32^a12;
          330  +    c3 = a13^a43^a23^a03^a33;
          331  +    c4 = a34^a14^a44^a24^a04;
          332  +    d0 = c4^ROL64(c1, 1);
          333  +    d1 = c0^ROL64(c2, 1);
          334  +    d2 = c1^ROL64(c3, 1);
          335  +    d3 = c2^ROL64(c4, 1);
          336  +    d4 = c3^ROL64(c0, 1);
          337  +
          338  +    b0 = (a00^d0);
          339  +    b1 = ROL64((a01^d1), 44);
          340  +    b2 = ROL64((a02^d2), 43);
          341  +    b3 = ROL64((a03^d3), 21);
          342  +    b4 = ROL64((a04^d4), 14);
          343  +    a00 =   b0 ^((~b1)&  b2 );
          344  +    a00 ^= RC[i+3];
          345  +    a01 =   b1 ^((~b2)&  b3 );
          346  +    a02 =   b2 ^((~b3)&  b4 );
          347  +    a03 =   b3 ^((~b4)&  b0 );
          348  +    a04 =   b4 ^((~b0)&  b1 );
          349  +
          350  +    b2 = ROL64((a10^d0), 3);
          351  +    b3 = ROL64((a11^d1), 45);
          352  +    b4 = ROL64((a12^d2), 61);
          353  +    b0 = ROL64((a13^d3), 28);
          354  +    b1 = ROL64((a14^d4), 20);
          355  +    a10 =   b0 ^((~b1)&  b2 );
          356  +    a11 =   b1 ^((~b2)&  b3 );
          357  +    a12 =   b2 ^((~b3)&  b4 );
          358  +    a13 =   b3 ^((~b4)&  b0 );
          359  +    a14 =   b4 ^((~b0)&  b1 );
          360  +
          361  +    b4 = ROL64((a20^d0), 18);
          362  +    b0 = ROL64((a21^d1), 1);
          363  +    b1 = ROL64((a22^d2), 6);
          364  +    b2 = ROL64((a23^d3), 25);
          365  +    b3 = ROL64((a24^d4), 8);
          366  +    a20 =   b0 ^((~b1)&  b2 );
          367  +    a21 =   b1 ^((~b2)&  b3 );
          368  +    a22 =   b2 ^((~b3)&  b4 );
          369  +    a23 =   b3 ^((~b4)&  b0 );
          370  +    a24 =   b4 ^((~b0)&  b1 );
   192    371   
   193         -    C0 = A00^A20^A40^A10^A30;
   194         -    C1 = A11^A31^A01^A21^A41;
   195         -    C2 = A22^A42^A12^A32^A02;
   196         -    C3 = A33^A03^A23^A43^A13;
   197         -    C4 = A44^A14^A34^A04^A24;
   198         -    D0 = C4^ROL64(C1, 1);
   199         -    D1 = C0^ROL64(C2, 1);
   200         -    D2 = C1^ROL64(C3, 1);
   201         -    D3 = C2^ROL64(C4, 1);
   202         -    D4 = C3^ROL64(C0, 1);
   203         -
   204         -    B0 = (A00^D0);
   205         -    B1 = ROL64((A31^D1), 44);
   206         -    B2 = ROL64((A12^D2), 43);
   207         -    B3 = ROL64((A43^D3), 21);
   208         -    B4 = ROL64((A24^D4), 14);
   209         -    A00 =   B0 ^((~B1)&  B2 );
   210         -    A00 ^= RC[i+1];
   211         -    A31 =   B1 ^((~B2)&  B3 );
   212         -    A12 =   B2 ^((~B3)&  B4 );
   213         -    A43 =   B3 ^((~B4)&  B0 );
   214         -    A24 =   B4 ^((~B0)&  B1 );
   215         -
   216         -    B2 = ROL64((A40^D0), 3);
   217         -    B3 = ROL64((A21^D1), 45);
   218         -    B4 = ROL64((A02^D2), 61);
   219         -    B0 = ROL64((A33^D3), 28);
   220         -    B1 = ROL64((A14^D4), 20);
   221         -    A40 =   B0 ^((~B1)&  B2 );
   222         -    A21 =   B1 ^((~B2)&  B3 );
   223         -    A02 =   B2 ^((~B3)&  B4 );
   224         -    A33 =   B3 ^((~B4)&  B0 );
   225         -    A14 =   B4 ^((~B0)&  B1 );
   226         -
   227         -    B4 = ROL64((A30^D0), 18);
   228         -    B0 = ROL64((A11^D1), 1);
   229         -    B1 = ROL64((A42^D2), 6);
   230         -    B2 = ROL64((A23^D3), 25);
   231         -    B3 = ROL64((A04^D4), 8);
   232         -    A30 =   B0 ^((~B1)&  B2 );
   233         -    A11 =   B1 ^((~B2)&  B3 );
   234         -    A42 =   B2 ^((~B3)&  B4 );
   235         -    A23 =   B3 ^((~B4)&  B0 );
   236         -    A04 =   B4 ^((~B0)&  B1 );
   237         -
   238         -    B1 = ROL64((A20^D0), 36);
   239         -    B2 = ROL64((A01^D1), 10);
   240         -    B3 = ROL64((A32^D2), 15);
   241         -    B4 = ROL64((A13^D3), 56);
   242         -    B0 = ROL64((A44^D4), 27);
   243         -    A20 =   B0 ^((~B1)&  B2 );
   244         -    A01 =   B1 ^((~B2)&  B3 );
   245         -    A32 =   B2 ^((~B3)&  B4 );
   246         -    A13 =   B3 ^((~B4)&  B0 );
   247         -    A44 =   B4 ^((~B0)&  B1 );
   248         -
   249         -    B3 = ROL64((A10^D0), 41);
   250         -    B4 = ROL64((A41^D1), 2);
   251         -    B0 = ROL64((A22^D2), 62);
   252         -    B1 = ROL64((A03^D3), 55);
   253         -    B2 = ROL64((A34^D4), 39);
   254         -    A10 =   B0 ^((~B1)&  B2 );
   255         -    A41 =   B1 ^((~B2)&  B3 );
   256         -    A22 =   B2 ^((~B3)&  B4 );
   257         -    A03 =   B3 ^((~B4)&  B0 );
   258         -    A34 =   B4 ^((~B0)&  B1 );
          372  +    b1 = ROL64((a30^d0), 36);
          373  +    b2 = ROL64((a31^d1), 10);
          374  +    b3 = ROL64((a32^d2), 15);
          375  +    b4 = ROL64((a33^d3), 56);
          376  +    b0 = ROL64((a34^d4), 27);
          377  +    a30 =   b0 ^((~b1)&  b2 );
          378  +    a31 =   b1 ^((~b2)&  b3 );
          379  +    a32 =   b2 ^((~b3)&  b4 );
          380  +    a33 =   b3 ^((~b4)&  b0 );
          381  +    a34 =   b4 ^((~b0)&  b1 );
   259    382   
   260         -    C0 = A00^A40^A30^A20^A10;
   261         -    C1 = A31^A21^A11^A01^A41;
   262         -    C2 = A12^A02^A42^A32^A22;
   263         -    C3 = A43^A33^A23^A13^A03;
   264         -    C4 = A24^A14^A04^A44^A34;
   265         -    D0 = C4^ROL64(C1, 1);
   266         -    D1 = C0^ROL64(C2, 1);
   267         -    D2 = C1^ROL64(C3, 1);
   268         -    D3 = C2^ROL64(C4, 1);
   269         -    D4 = C3^ROL64(C0, 1);
   270         -
   271         -    B0 = (A00^D0);
   272         -    B1 = ROL64((A21^D1), 44);
   273         -    B2 = ROL64((A42^D2), 43);
   274         -    B3 = ROL64((A13^D3), 21);
   275         -    B4 = ROL64((A34^D4), 14);
   276         -    A00 =   B0 ^((~B1)&  B2 );
   277         -    A00 ^= RC[i+2];
   278         -    A21 =   B1 ^((~B2)&  B3 );
   279         -    A42 =   B2 ^((~B3)&  B4 );
   280         -    A13 =   B3 ^((~B4)&  B0 );
   281         -    A34 =   B4 ^((~B0)&  B1 );
   282         -
   283         -    B2 = ROL64((A30^D0), 3);
   284         -    B3 = ROL64((A01^D1), 45);
   285         -    B4 = ROL64((A22^D2), 61);
   286         -    B0 = ROL64((A43^D3), 28);
   287         -    B1 = ROL64((A14^D4), 20);
   288         -    A30 =   B0 ^((~B1)&  B2 );
   289         -    A01 =   B1 ^((~B2)&  B3 );
   290         -    A22 =   B2 ^((~B3)&  B4 );
   291         -    A43 =   B3 ^((~B4)&  B0 );
   292         -    A14 =   B4 ^((~B0)&  B1 );
   293         -
   294         -    B4 = ROL64((A10^D0), 18);
   295         -    B0 = ROL64((A31^D1), 1);
   296         -    B1 = ROL64((A02^D2), 6);
   297         -    B2 = ROL64((A23^D3), 25);
   298         -    B3 = ROL64((A44^D4), 8);
   299         -    A10 =   B0 ^((~B1)&  B2 );
   300         -    A31 =   B1 ^((~B2)&  B3 );
   301         -    A02 =   B2 ^((~B3)&  B4 );
   302         -    A23 =   B3 ^((~B4)&  B0 );
   303         -    A44 =   B4 ^((~B0)&  B1 );
   304         -
   305         -    B1 = ROL64((A40^D0), 36);
   306         -    B2 = ROL64((A11^D1), 10);
   307         -    B3 = ROL64((A32^D2), 15);
   308         -    B4 = ROL64((A03^D3), 56);
   309         -    B0 = ROL64((A24^D4), 27);
   310         -    A40 =   B0 ^((~B1)&  B2 );
   311         -    A11 =   B1 ^((~B2)&  B3 );
   312         -    A32 =   B2 ^((~B3)&  B4 );
   313         -    A03 =   B3 ^((~B4)&  B0 );
   314         -    A24 =   B4 ^((~B0)&  B1 );
   315         -
   316         -    B3 = ROL64((A20^D0), 41);
   317         -    B4 = ROL64((A41^D1), 2);
   318         -    B0 = ROL64((A12^D2), 62);
   319         -    B1 = ROL64((A33^D3), 55);
   320         -    B2 = ROL64((A04^D4), 39);
   321         -    A20 =   B0 ^((~B1)&  B2 );
   322         -    A41 =   B1 ^((~B2)&  B3 );
   323         -    A12 =   B2 ^((~B3)&  B4 );
   324         -    A33 =   B3 ^((~B4)&  B0 );
   325         -    A04 =   B4 ^((~B0)&  B1 );
   326         -
   327         -    C0 = A00^A30^A10^A40^A20;
   328         -    C1 = A21^A01^A31^A11^A41;
   329         -    C2 = A42^A22^A02^A32^A12;
   330         -    C3 = A13^A43^A23^A03^A33;
   331         -    C4 = A34^A14^A44^A24^A04;
   332         -    D0 = C4^ROL64(C1, 1);
   333         -    D1 = C0^ROL64(C2, 1);
   334         -    D2 = C1^ROL64(C3, 1);
   335         -    D3 = C2^ROL64(C4, 1);
   336         -    D4 = C3^ROL64(C0, 1);
   337         -
   338         -    B0 = (A00^D0);
   339         -    B1 = ROL64((A01^D1), 44);
   340         -    B2 = ROL64((A02^D2), 43);
   341         -    B3 = ROL64((A03^D3), 21);
   342         -    B4 = ROL64((A04^D4), 14);
   343         -    A00 =   B0 ^((~B1)&  B2 );
   344         -    A00 ^= RC[i+3];
   345         -    A01 =   B1 ^((~B2)&  B3 );
   346         -    A02 =   B2 ^((~B3)&  B4 );
   347         -    A03 =   B3 ^((~B4)&  B0 );
   348         -    A04 =   B4 ^((~B0)&  B1 );
   349         -
   350         -    B2 = ROL64((A10^D0), 3);
   351         -    B3 = ROL64((A11^D1), 45);
   352         -    B4 = ROL64((A12^D2), 61);
   353         -    B0 = ROL64((A13^D3), 28);
   354         -    B1 = ROL64((A14^D4), 20);
   355         -    A10 =   B0 ^((~B1)&  B2 );
   356         -    A11 =   B1 ^((~B2)&  B3 );
   357         -    A12 =   B2 ^((~B3)&  B4 );
   358         -    A13 =   B3 ^((~B4)&  B0 );
   359         -    A14 =   B4 ^((~B0)&  B1 );
   360         -
   361         -    B4 = ROL64((A20^D0), 18);
   362         -    B0 = ROL64((A21^D1), 1);
   363         -    B1 = ROL64((A22^D2), 6);
   364         -    B2 = ROL64((A23^D3), 25);
   365         -    B3 = ROL64((A24^D4), 8);
   366         -    A20 =   B0 ^((~B1)&  B2 );
   367         -    A21 =   B1 ^((~B2)&  B3 );
   368         -    A22 =   B2 ^((~B3)&  B4 );
   369         -    A23 =   B3 ^((~B4)&  B0 );
   370         -    A24 =   B4 ^((~B0)&  B1 );
   371         -
   372         -    B1 = ROL64((A30^D0), 36);
   373         -    B2 = ROL64((A31^D1), 10);
   374         -    B3 = ROL64((A32^D2), 15);
   375         -    B4 = ROL64((A33^D3), 56);
   376         -    B0 = ROL64((A34^D4), 27);
   377         -    A30 =   B0 ^((~B1)&  B2 );
   378         -    A31 =   B1 ^((~B2)&  B3 );
   379         -    A32 =   B2 ^((~B3)&  B4 );
   380         -    A33 =   B3 ^((~B4)&  B0 );
   381         -    A34 =   B4 ^((~B0)&  B1 );
   382         -
   383         -    B3 = ROL64((A40^D0), 41);
   384         -    B4 = ROL64((A41^D1), 2);
   385         -    B0 = ROL64((A42^D2), 62);
   386         -    B1 = ROL64((A43^D3), 55);
   387         -    B2 = ROL64((A44^D4), 39);
   388         -    A40 =   B0 ^((~B1)&  B2 );
   389         -    A41 =   B1 ^((~B2)&  B3 );
   390         -    A42 =   B2 ^((~B3)&  B4 );
   391         -    A43 =   B3 ^((~B4)&  B0 );
   392         -    A44 =   B4 ^((~B0)&  B1 );
          383  +    b3 = ROL64((a40^d0), 41);
          384  +    b4 = ROL64((a41^d1), 2);
          385  +    b0 = ROL64((a42^d2), 62);
          386  +    b1 = ROL64((a43^d3), 55);
          387  +    b2 = ROL64((a44^d4), 39);
          388  +    a40 =   b0 ^((~b1)&  b2 );
          389  +    a41 =   b1 ^((~b2)&  b3 );
          390  +    a42 =   b2 ^((~b3)&  b4 );
          391  +    a43 =   b3 ^((~b4)&  b0 );
          392  +    a44 =   b4 ^((~b0)&  b1 );
   393    393     }
   394    394   }
   395    395   
   396    396   /*
   397    397   ** Initialize a new hash.  iSize determines the size of the hash
   398    398   ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
   399    399   ** can be zero to use the default hash size of 256 bits.

Changes to main.mk.

   688    688   	./mkkeywordhash >keywordhash.h
   689    689   
   690    690   # Source files that go into making shell.c
   691    691   SHELL_SRC = \
   692    692   	$(TOP)/src/shell.c.in \
   693    693   	$(TOP)/ext/misc/shathree.c \
   694    694   	$(TOP)/ext/misc/fileio.c \
   695         -	$(TOP)/ext/misc/completion.c
          695  +	$(TOP)/ext/misc/completion.c \
          696  +	$(TOP)/ext/expert/sqlite3expert.c \
          697  +	$(TOP)/ext/expert/sqlite3expert.h
   696    698   
   697    699   shell.c:	$(SHELL_SRC) $(TOP)/tool/mkshellc.tcl
   698    700   	tclsh $(TOP)/tool/mkshellc.tcl >shell.c
   699    701   
   700    702   
   701    703   
   702    704   # Rules to build the extension objects.

Changes to src/build.c.

  1217   1217   **
  1218   1218   ** Default value expressions must be constant.  Raise an exception if this
  1219   1219   ** is not the case.
  1220   1220   **
  1221   1221   ** This routine is called by the parser while in the middle of
  1222   1222   ** parsing a CREATE TABLE statement.
  1223   1223   */
  1224         -void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
         1224  +void sqlite3AddDefaultValue(
         1225  +  Parse *pParse,           /* Parsing context */
         1226  +  Expr *pExpr,             /* The parsed expression of the default value */
         1227  +  const char *zStart,      /* Start of the default value text */
         1228  +  const char *zEnd         /* First character past end of defaut value text */
         1229  +){
  1225   1230     Table *p;
  1226   1231     Column *pCol;
  1227   1232     sqlite3 *db = pParse->db;
  1228   1233     p = pParse->pNewTable;
  1229   1234     if( p!=0 ){
  1230   1235       pCol = &(p->aCol[p->nCol-1]);
  1231         -    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
         1236  +    if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
  1232   1237         sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
  1233   1238             pCol->zName);
  1234   1239       }else{
  1235   1240         /* A copy of pExpr is used instead of the original, as pExpr contains
  1236         -      ** tokens that point to volatile memory. The 'span' of the expression
  1237         -      ** is required by pragma table_info.
         1241  +      ** tokens that point to volatile memory.	
  1238   1242         */
  1239   1243         Expr x;
  1240   1244         sqlite3ExprDelete(db, pCol->pDflt);
  1241   1245         memset(&x, 0, sizeof(x));
  1242   1246         x.op = TK_SPAN;
  1243         -      x.u.zToken = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
  1244         -                                    (int)(pSpan->zEnd - pSpan->zStart));
  1245         -      x.pLeft = pSpan->pExpr;
         1247  +      x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
         1248  +      x.pLeft = pExpr;
  1246   1249         x.flags = EP_Skip;
  1247   1250         pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
  1248   1251         sqlite3DbFree(db, x.u.zToken);
  1249   1252       }
  1250   1253     }
  1251         -  sqlite3ExprDelete(db, pSpan->pExpr);
         1254  +  sqlite3ExprDelete(db, pExpr);
  1252   1255   }
  1253   1256   
  1254   1257   /*
  1255   1258   ** Backwards Compatibility Hack:
  1256   1259   ** 
  1257   1260   ** Historical versions of SQLite accepted strings as column names in
  1258   1261   ** indexes and PRIMARY KEY constraints and in UNIQUE constraints.  Example:
................................................................................
  1961   1964         assert(pParse->nTab==1);
  1962   1965         sqlite3MayAbort(pParse);
  1963   1966         sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
  1964   1967         sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
  1965   1968         pParse->nTab = 2;
  1966   1969         addrTop = sqlite3VdbeCurrentAddr(v) + 1;
  1967   1970         sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
  1968         -      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
  1969         -      sqlite3Select(pParse, pSelect, &dest);
  1970         -      sqlite3VdbeEndCoroutine(v, regYield);
  1971         -      sqlite3VdbeJumpHere(v, addrTop - 1);
  1972   1971         if( pParse->nErr ) return;
  1973   1972         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
  1974   1973         if( pSelTab==0 ) return;
  1975   1974         assert( p->aCol==0 );
  1976   1975         p->nCol = pSelTab->nCol;
  1977   1976         p->aCol = pSelTab->aCol;
  1978   1977         pSelTab->nCol = 0;
  1979   1978         pSelTab->aCol = 0;
  1980   1979         sqlite3DeleteTable(db, pSelTab);
         1980  +      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
         1981  +      sqlite3Select(pParse, pSelect, &dest);
         1982  +      if( pParse->nErr ) return;
         1983  +      sqlite3VdbeEndCoroutine(v, regYield);
         1984  +      sqlite3VdbeJumpHere(v, addrTop - 1);
  1981   1985         addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
  1982   1986         VdbeCoverage(v);
  1983   1987         sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
  1984   1988         sqlite3TableAffinity(v, p, 0);
  1985   1989         sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
  1986   1990         sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
  1987   1991         sqlite3VdbeGoto(v, addrInsLoop);

Changes to src/expr.c.

  1650   1650   ** pList might be NULL following an OOM error.  But pSpan should never be
  1651   1651   ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
  1652   1652   ** is set.
  1653   1653   */
  1654   1654   void sqlite3ExprListSetSpan(
  1655   1655     Parse *pParse,          /* Parsing context */
  1656   1656     ExprList *pList,        /* List to which to add the span. */
  1657         -  ExprSpan *pSpan         /* The span to be added */
         1657  +  const char *zStart,     /* Start of the span */
         1658  +  const char *zEnd        /* End of the span */
  1658   1659   ){
  1659   1660     sqlite3 *db = pParse->db;
  1660   1661     assert( pList!=0 || db->mallocFailed!=0 );
  1661   1662     if( pList ){
  1662   1663       struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
  1663   1664       assert( pList->nExpr>0 );
  1664         -    assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
  1665   1665       sqlite3DbFree(db, pItem->zSpan);
  1666         -    pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
  1667         -                                    (int)(pSpan->zEnd - pSpan->zStart));
         1666  +    pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd);
  1668   1667     }
  1669   1668   }
  1670   1669   
  1671   1670   /*
  1672   1671   ** If the expression list pEList contains more than iLimit elements,
  1673   1672   ** leave an error message in pParse.
  1674   1673   */

Changes to src/loadext.c.

   492    492   
   493    493     zEntry = zProc ? zProc : "sqlite3_extension_init";
   494    494   
   495    495     handle = sqlite3OsDlOpen(pVfs, zFile);
   496    496   #if SQLITE_OS_UNIX || SQLITE_OS_WIN
   497    497     for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
   498    498       char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
          499  +    int bExists = 0;
   499    500       if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
   500         -    handle = sqlite3OsDlOpen(pVfs, zAltFile);
          501  +    sqlite3OsAccess(pVfs, zAltFile, SQLITE_ACCESS_EXISTS, &bExists);
          502  +    if( bExists )  handle = sqlite3OsDlOpen(pVfs, zAltFile);
   501    503       sqlite3_free(zAltFile);
   502    504     }
   503    505   #endif
   504    506     if( handle==0 ){
   505    507       if( pzErrMsg ){
   506    508         *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
   507    509         if( zErrmsg ){

Changes to src/main.c.

   802    802       case SQLITE_DBCONFIG_LOOKASIDE: {
   803    803         void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
   804    804         int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
   805    805         int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
   806    806         rc = setupLookaside(db, pBuf, sz, cnt);
   807    807         break;
   808    808       }
   809         -    case SQLITE_DBCONFIG_FULL_EQP: {
   810         -      int onoff = va_arg(ap, int);
   811         -      int *pRes = va_arg(ap, int*);
   812         -      if( onoff>0 ){
   813         -        db->bFullEQP = 1;
   814         -      }else if( onoff==0 ){
   815         -        db->bFullEQP = 0;
   816         -      }
   817         -      sqlite3ExpirePreparedStatements(db);
   818         -      if( pRes ){
   819         -        *pRes = db->bFullEQP;
   820         -      }
   821         -      rc = SQLITE_OK;
   822         -      break;
   823         -    }
   824    809       default: {
   825    810         static const struct {
   826    811           int op;      /* The opcode */
   827    812           u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
   828    813         } aFlagOp[] = {
   829    814           { SQLITE_DBCONFIG_ENABLE_FKEY,           SQLITE_ForeignKeys    },
   830    815           { SQLITE_DBCONFIG_ENABLE_TRIGGER,        SQLITE_EnableTrigger  },
   831    816           { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer  },
   832    817           { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension  },
   833    818           { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,      SQLITE_NoCkptOnClose  },
   834    819           { SQLITE_DBCONFIG_ENABLE_QPSG,           SQLITE_EnableQPSG     },
          820  +        { SQLITE_DBCONFIG_TRIGGER_EQP,           SQLITE_TriggerEQP     },
   835    821         };
   836    822         unsigned int i;
   837    823         rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
   838    824         for(i=0; i<ArraySize(aFlagOp); i++){
   839    825           if( aFlagOp[i].op==op ){
   840    826             int onoff = va_arg(ap, int);
   841    827             int *pRes = va_arg(ap, int*);
................................................................................
  3921   3907         db->init.newTnum = va_arg(ap,int);
  3922   3908         if( db->init.busy==0 && db->init.newTnum>0 ){
  3923   3909           sqlite3ResetAllSchemasOfConnection(db);
  3924   3910         }
  3925   3911         sqlite3_mutex_leave(db->mutex);
  3926   3912         break;
  3927   3913       }
         3914  +
         3915  +#if defined(YYCOVERAGE)
         3916  +    /*  sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
         3917  +    **
         3918  +    ** This test control (only available when SQLite is compiled with
         3919  +    ** -DYYCOVERAGE) writes a report onto "out" that shows all
         3920  +    ** state/lookahead combinations in the parser state machine
         3921  +    ** which are never exercised.  If any state is missed, make the
         3922  +    ** return code SQLITE_ERROR.
         3923  +    */
         3924  +    case SQLITE_TESTCTRL_PARSER_COVERAGE: {
         3925  +      FILE *out = va_arg(ap, FILE*);
         3926  +      if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
         3927  +      break;
         3928  +    }
         3929  +#endif /* defined(YYCOVERAGE) */
  3928   3930     }
  3929   3931     va_end(ap);
  3930   3932   #endif /* SQLITE_UNTESTABLE */
  3931   3933     return rc;
  3932   3934   }
  3933   3935   
  3934   3936   /*

Changes to src/malloc.c.

   622    622     zNew = sqlite3DbMallocRawNN(db, n+1);
   623    623     if( zNew ){
   624    624       memcpy(zNew, z, (size_t)n);
   625    625       zNew[n] = 0;
   626    626     }
   627    627     return zNew;
   628    628   }
          629  +
          630  +/*
          631  +** The text between zStart and zEnd represents a phrase within a larger
          632  +** SQL statement.  Make a copy of this phrase in space obtained form
          633  +** sqlite3DbMalloc().  Omit leading and trailing whitespace.
          634  +*/
          635  +char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
          636  +  int n;
          637  +  while( sqlite3Isspace(zStart[0]) ) zStart++;
          638  +  n = (int)(zEnd - zStart);
          639  +  while( n>0 && sqlite3Isspace(zStart[n-1]) ) n--;
          640  +  return sqlite3DbStrNDup(db, zStart, n);
          641  +}
   629    642   
   630    643   /*
   631    644   ** Free any prior content in *pz and replace it with a copy of zNew.
   632    645   */
   633    646   void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
   634    647     sqlite3DbFree(db, *pz);
   635    648     *pz = sqlite3DbStrDup(db, zNew);

Changes to src/parse.y.

   268    268     A.n = (int)(&Y.z[Y.n] - A.z);
   269    269   }
   270    270   %type typename {Token}
   271    271   typename(A) ::= ids(A).
   272    272   typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);}
   273    273   signed ::= plus_num.
   274    274   signed ::= minus_num.
          275  +
          276  +// The scanpt non-terminal takes a value which is a pointer to the
          277  +// input text just past the last token that has been shifted into
          278  +// the parser.  By surrounding some phrase in the grammar with two
          279  +// scanpt non-terminals, we can capture the input text for that phrase.
          280  +// For example:
          281  +//
          282  +//      something ::= .... scanpt(A) phrase scanpt(Z).
          283  +//
          284  +// The text that is parsed as "phrase" is a string starting at A
          285  +// and containing (int)(Z-A) characters.  There might be some extra
          286  +// whitespace on either end of the text, but that can be removed in
          287  +// post-processing, if needed.
          288  +//
          289  +%type scanpt {const char*}
          290  +scanpt(A) ::= . {
          291  +  assert( yyLookahead!=YYNOCODE );
          292  +  A = yyLookaheadToken.z;
          293  +}
   275    294   
   276    295   // "carglist" is a list of additional constraints that come after the
   277    296   // column name and column type in a CREATE TABLE statement.
   278    297   //
   279    298   carglist ::= carglist ccons.
   280    299   carglist ::= .
   281    300   ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
   282         -ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,&X);}
   283         -ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,&X);}
   284         -ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,&X);}
   285         -ccons ::= DEFAULT MINUS(A) term(X).      {
   286         -  ExprSpan v;
   287         -  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0);
   288         -  v.zStart = A.z;
   289         -  v.zEnd = X.zEnd;
   290         -  sqlite3AddDefaultValue(pParse,&v);
          301  +ccons ::= DEFAULT scanpt(A) term(X) scanpt(Z).
          302  +                            {sqlite3AddDefaultValue(pParse,X,A,Z);}
          303  +ccons ::= DEFAULT LP(A) expr(X) RP(Z).
          304  +                            {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);}
          305  +ccons ::= DEFAULT PLUS(A) term(X) scanpt(Z).
          306  +                            {sqlite3AddDefaultValue(pParse,X,A.z,Z);}
          307  +ccons ::= DEFAULT MINUS(A) term(X) scanpt(Z).      {
          308  +  Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
          309  +  sqlite3AddDefaultValue(pParse,p,A.z,Z);
   291    310   }
   292         -ccons ::= DEFAULT id(X).              {
   293         -  ExprSpan v;
   294         -  spanExpr(&v, pParse, TK_STRING, X);
   295         -  sqlite3AddDefaultValue(pParse,&v);
          311  +ccons ::= DEFAULT scanpt id(X).       {
          312  +  Expr *p = tokenExpr(pParse, TK_STRING, X);
          313  +  sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
   296    314   }
   297    315   
   298    316   // In addition to the type name, we also care about the primary key and
   299    317   // UNIQUE constraints.
   300    318   //
   301    319   ccons ::= NULL onconf.
   302    320   ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
   303    321   ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
   304    322                                    {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
   305    323   ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
   306    324                                      SQLITE_IDXTYPE_UNIQUE);}
   307         -ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X.pExpr);}
          325  +ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X);}
   308    326   ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
   309    327                                    {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
   310    328   ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
   311    329   ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
   312    330   
   313    331   // The optional AUTOINCREMENT keyword
   314    332   %type autoinc {int}
................................................................................
   351    369   tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
   352    370   tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
   353    371                                    {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
   354    372   tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
   355    373                                    {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
   356    374                                          SQLITE_IDXTYPE_UNIQUE);}
   357    375   tcons ::= CHECK LP expr(E) RP onconf.
   358         -                                 {sqlite3AddCheckConstraint(pParse,E.pExpr);}
          376  +                                 {sqlite3AddCheckConstraint(pParse,E);}
   359    377   tcons ::= FOREIGN KEY LP eidlist(FA) RP
   360    378             REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
   361    379       sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
   362    380       sqlite3DeferForeignKey(pParse, D);
   363    381   }
   364    382   %type defer_subclause_opt {int}
   365    383   defer_subclause_opt(A) ::= .                    {A = 0;}
................................................................................
   545    563   //
   546    564   %type selcollist {ExprList*}
   547    565   %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
   548    566   %type sclp {ExprList*}
   549    567   %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
   550    568   sclp(A) ::= selcollist(A) COMMA.
   551    569   sclp(A) ::= .                                {A = 0;}
   552         -selcollist(A) ::= sclp(A) expr(X) as(Y).     {
   553         -   A = sqlite3ExprListAppend(pParse, A, X.pExpr);
          570  +selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y).     {
          571  +   A = sqlite3ExprListAppend(pParse, A, X);
   554    572      if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
   555         -   sqlite3ExprListSetSpan(pParse,A,&X);
          573  +   sqlite3ExprListSetSpan(pParse,A,B,Z);
   556    574   }
   557         -selcollist(A) ::= sclp(A) STAR. {
          575  +selcollist(A) ::= sclp(A) scanpt STAR. {
   558    576     Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
   559    577     A = sqlite3ExprListAppend(pParse, A, p);
   560    578   }
   561         -selcollist(A) ::= sclp(A) nm(X) DOT STAR. {
          579  +selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR. {
   562    580     Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
   563    581     Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
   564    582     Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
   565    583     A = sqlite3ExprListAppend(pParse,A, pDot);
   566    584   }
   567    585   
   568    586   // An option "AS <id>" phrase that can follow one of the expressions that
................................................................................
   652    670   joinop(X) ::= JOIN_KW(A) nm(B) JOIN.
   653    671                     {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/}
   654    672   joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
   655    673                     {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/}
   656    674   
   657    675   %type on_opt {Expr*}
   658    676   %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
   659         -on_opt(N) ::= ON expr(E).   {N = E.pExpr;}
          677  +on_opt(N) ::= ON expr(E).   {N = E;}
   660    678   on_opt(N) ::= .             {N = 0;}
   661    679   
   662    680   // Note that this block abuses the Token type just a little. If there is
   663    681   // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
   664    682   // there is an INDEXED BY clause, then the token is populated as per normal,
   665    683   // with z pointing to the token data and n containing the number of bytes
   666    684   // in the token.
................................................................................
   689    707   //
   690    708   %type sortlist {ExprList*}
   691    709   %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
   692    710   
   693    711   orderby_opt(A) ::= .                          {A = 0;}
   694    712   orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
   695    713   sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
   696         -  A = sqlite3ExprListAppend(pParse,A,Y.pExpr);
          714  +  A = sqlite3ExprListAppend(pParse,A,Y);
   697    715     sqlite3ExprListSetSortOrder(A,Z);
   698    716   }
   699    717   sortlist(A) ::= expr(Y) sortorder(Z). {
   700         -  A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/
          718  +  A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
   701    719     sqlite3ExprListSetSortOrder(A,Z);
   702    720   }
   703    721   
   704    722   %type sortorder {int}
   705    723   
   706    724   sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
   707    725   sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
................................................................................
   711    729   %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
   712    730   groupby_opt(A) ::= .                      {A = 0;}
   713    731   groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
   714    732   
   715    733   %type having_opt {Expr*}
   716    734   %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
   717    735   having_opt(A) ::= .                {A = 0;}
   718         -having_opt(A) ::= HAVING expr(X).  {A = X.pExpr;}
          736  +having_opt(A) ::= HAVING expr(X).  {A = X;}
   719    737   
   720    738   %type limit_opt {Expr*}
   721    739   
   722    740   // The destructor for limit_opt will never fire in the current grammar.
   723    741   // The limit_opt non-terminal only occurs at the end of a single production
   724    742   // rule for SELECT statements.  As soon as the rule that create the 
   725    743   // limit_opt non-terminal reduces, the SELECT statement rule will also
   726    744   // reduce.  So there is never a limit_opt non-terminal on the stack 
   727    745   // except as a transient.  So there is never anything to destroy.
   728    746   //
   729    747   //%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);}
   730    748   limit_opt(A) ::= .       {A = 0;}
   731    749   limit_opt(A) ::= LIMIT expr(X).
   732         -                         {A = sqlite3PExpr(pParse,TK_LIMIT,X.pExpr,0);}
          750  +                         {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);}
   733    751   limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 
   734         -                         {A = sqlite3PExpr(pParse,TK_LIMIT,X.pExpr,Y.pExpr);}
          752  +                         {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);}
   735    753   limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 
   736         -                         {A = sqlite3PExpr(pParse,TK_LIMIT,Y.pExpr,X.pExpr);}
          754  +                         {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);}
   737    755   
   738    756   /////////////////////////// The DELETE statement /////////////////////////////
   739    757   //
   740    758   %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   741    759   cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W) 
   742    760           orderby_opt(O) limit_opt(L). {
   743    761     sqlite3WithPush(pParse, C, 1);
................................................................................
   753    771   }
   754    772   %endif
   755    773   
   756    774   %type where_opt {Expr*}
   757    775   %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
   758    776   
   759    777   where_opt(A) ::= .                    {A = 0;}
   760         -where_opt(A) ::= WHERE expr(X).       {A = X.pExpr;}
          778  +where_opt(A) ::= WHERE expr(X).       {A = X;}
   761    779   
   762    780   ////////////////////////// The UPDATE command ////////////////////////////////
   763    781   //
   764    782   %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   765    783   cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
   766    784           where_opt(W) orderby_opt(O) limit_opt(L).  {
   767    785     sqlite3WithPush(pParse, C, 1);
................................................................................
   780    798   }
   781    799   %endif
   782    800   
   783    801   %type setlist {ExprList*}
   784    802   %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
   785    803   
   786    804   setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). {
   787         -  A = sqlite3ExprListAppend(pParse, A, Y.pExpr);
          805  +  A = sqlite3ExprListAppend(pParse, A, Y);
   788    806     sqlite3ExprListSetName(pParse, A, &X, 1);
   789    807   }
   790    808   setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). {
   791         -  A = sqlite3ExprListAppendVector(pParse, A, X, Y.pExpr);
          809  +  A = sqlite3ExprListAppendVector(pParse, A, X, Y);
   792    810   }
   793    811   setlist(A) ::= nm(X) EQ expr(Y). {
   794         -  A = sqlite3ExprListAppend(pParse, 0, Y.pExpr);
          812  +  A = sqlite3ExprListAppend(pParse, 0, Y);
   795    813     sqlite3ExprListSetName(pParse, A, &X, 1);
   796    814   }
   797    815   setlist(A) ::= LP idlist(X) RP EQ expr(Y). {
   798         -  A = sqlite3ExprListAppendVector(pParse, 0, X, Y.pExpr);
          816  +  A = sqlite3ExprListAppendVector(pParse, 0, X, Y);
   799    817   }
   800    818   
   801    819   ////////////////////////// The INSERT command /////////////////////////////////
   802    820   //
   803    821   cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S). {
   804    822     sqlite3WithPush(pParse, W, 1);
   805    823     sqlite3Insert(pParse, X, S, F, R);
................................................................................
   825    843       {A = sqlite3IdListAppend(pParse->db,A,&Y);}
   826    844   idlist(A) ::= nm(Y).
   827    845       {A = sqlite3IdListAppend(pParse->db,0,&Y); /*A-overwrites-Y*/}
   828    846   
   829    847   /////////////////////////// Expression Processing /////////////////////////////
   830    848   //
   831    849   
   832         -%type expr {ExprSpan}
   833         -%destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);}
   834         -%type term {ExprSpan}
   835         -%destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);}
          850  +%type expr {Expr*}
          851  +%destructor expr {sqlite3ExprDelete(pParse->db, $$);}
          852  +%type term {Expr*}
          853  +%destructor term {sqlite3ExprDelete(pParse->db, $$);}
   836    854   
   837    855   %include {
   838         -  /* This is a utility routine used to set the ExprSpan.zStart and
   839         -  ** ExprSpan.zEnd values of pOut so that the span covers the complete
   840         -  ** range of text beginning with pStart and going to the end of pEnd.
   841         -  */
   842         -  static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
   843         -    pOut->zStart = pStart->z;
   844         -    pOut->zEnd = &pEnd->z[pEnd->n];
   845         -  }
   846    856   
   847    857     /* Construct a new Expr object from a single identifier.  Use the
   848    858     ** new Expr to populate pOut.  Set the span of pOut to be the identifier
   849    859     ** that created the expression.
   850    860     */
   851         -  static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token t){
          861  +  static Expr *tokenExpr(Parse *pParse, int op, Token t){
   852    862       Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
   853    863       if( p ){
   854    864         memset(p, 0, sizeof(Expr));
   855    865         p->op = (u8)op;
   856    866         p->flags = EP_Leaf;
   857    867         p->iAgg = -1;
   858    868         p->u.zToken = (char*)&p[1];
................................................................................
   862    872           if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
   863    873           sqlite3Dequote(p->u.zToken);
   864    874         }
   865    875   #if SQLITE_MAX_EXPR_DEPTH>0
   866    876         p->nHeight = 1;
   867    877   #endif  
   868    878       }
   869         -    pOut->pExpr = p;
   870         -    pOut->zStart = t.z;
   871         -    pOut->zEnd = &t.z[t.n];
          879  +    return p;
   872    880     }
   873    881   }
   874    882   
   875    883   expr(A) ::= term(A).
   876         -expr(A) ::= LP(B) expr(X) RP(E).
   877         -            {spanSet(&A,&B,&E); /*A-overwrites-B*/  A.pExpr = X.pExpr;}
   878         -expr(A) ::= id(X).          {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/}
   879         -expr(A) ::= JOIN_KW(X).     {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/}
          884  +expr(A) ::= LP expr(X) RP. {A = X;}
          885  +expr(A) ::= id(X).          {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
          886  +expr(A) ::= JOIN_KW(X).     {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/}
   880    887   expr(A) ::= nm(X) DOT nm(Y). {
   881    888     Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
   882    889     Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
   883         -  spanSet(&A,&X,&Y); /*A-overwrites-X*/
   884         -  A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
          890  +  A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
   885    891   }
   886    892   expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
   887    893     Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
   888    894     Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
   889    895     Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
   890    896     Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
   891         -  spanSet(&A,&X,&Z); /*A-overwrites-X*/
   892         -  A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
          897  +  A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
   893    898   }
   894         -term(A) ::= NULL|FLOAT|BLOB(X). {spanExpr(&A,pParse,@X,X); /*A-overwrites-X*/}
   895         -term(A) ::= STRING(X).          {spanExpr(&A,pParse,@X,X); /*A-overwrites-X*/}
          899  +term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
          900  +term(A) ::= STRING(X).          {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/}
   896    901   term(A) ::= INTEGER(X). {
   897         -  A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
   898         -  A.zStart = X.z;
   899         -  A.zEnd = X.z + X.n;
          902  +  A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
   900    903   }
   901    904   expr(A) ::= VARIABLE(X).     {
   902    905     if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
   903    906       u32 n = X.n;
   904         -    spanExpr(&A, pParse, TK_VARIABLE, X);
   905         -    sqlite3ExprAssignVarNumber(pParse, A.pExpr, n);
          907  +    A = tokenExpr(pParse, TK_VARIABLE, X);
          908  +    sqlite3ExprAssignVarNumber(pParse, A, n);
   906    909     }else{
   907    910       /* When doing a nested parse, one can include terms in an expression
   908    911       ** that look like this:   #1 #2 ...  These terms refer to registers
   909    912       ** in the virtual machine.  #N is the N-th register. */
   910    913       Token t = X; /*A-overwrites-X*/
   911    914       assert( t.n>=2 );
   912         -    spanSet(&A, &t, &t);
   913    915       if( pParse->nested==0 ){
   914    916         sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
   915         -      A.pExpr = 0;
          917  +      A = 0;
   916    918       }else{
   917         -      A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
   918         -      if( A.pExpr ) sqlite3GetInt32(&t.z[1], &A.pExpr->iTable);
          919  +      A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
          920  +      if( A ) sqlite3GetInt32(&t.z[1], &A->iTable);
   919    921       }
   920    922     }
   921    923   }
   922    924   expr(A) ::= expr(A) COLLATE ids(C). {
   923         -  A.pExpr = sqlite3ExprAddCollateToken(pParse, A.pExpr, &C, 1);
   924         -  A.zEnd = &C.z[C.n];
          925  +  A = sqlite3ExprAddCollateToken(pParse, A, &C, 1);
   925    926   }
   926    927   %ifndef SQLITE_OMIT_CAST
   927         -expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
   928         -  spanSet(&A,&X,&Y); /*A-overwrites-X*/
   929         -  A.pExpr = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
   930         -  sqlite3ExprAttachSubtrees(pParse->db, A.pExpr, E.pExpr, 0);
          928  +expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. {
          929  +  A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
          930  +  sqlite3ExprAttachSubtrees(pParse->db, A, E, 0);
   931    931   }
   932    932   %endif  SQLITE_OMIT_CAST
   933         -expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). {
          933  +expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP. {
   934    934     if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
   935    935       sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
   936    936     }
   937         -  A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
   938         -  spanSet(&A,&X,&E);
   939         -  if( D==SF_Distinct && A.pExpr ){
   940         -    A.pExpr->flags |= EP_Distinct;
          937  +  A = sqlite3ExprFunction(pParse, Y, &X);
          938  +  if( D==SF_Distinct && A ){
          939  +    A->flags |= EP_Distinct;
   941    940     }
   942    941   }
   943         -expr(A) ::= id(X) LP STAR RP(E). {
   944         -  A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
   945         -  spanSet(&A,&X,&E);
          942  +expr(A) ::= id(X) LP STAR RP. {
          943  +  A = sqlite3ExprFunction(pParse, 0, &X);
   946    944   }
   947    945   term(A) ::= CTIME_KW(OP). {
   948         -  A.pExpr = sqlite3ExprFunction(pParse, 0, &OP);
   949         -  spanSet(&A, &OP, &OP);
          946  +  A = sqlite3ExprFunction(pParse, 0, &OP);
   950    947   }
   951    948   
   952         -%include {
   953         -  /* This routine constructs a binary expression node out of two ExprSpan
   954         -  ** objects and uses the result to populate a new ExprSpan object.
   955         -  */
   956         -  static void spanBinaryExpr(
   957         -    Parse *pParse,      /* The parsing context.  Errors accumulate here */
   958         -    int op,             /* The binary operation */
   959         -    ExprSpan *pLeft,    /* The left operand, and output */
   960         -    ExprSpan *pRight    /* The right operand */
   961         -  ){
   962         -    pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr);
   963         -    pLeft->zEnd = pRight->zEnd;
   964         -  }
   965         -
   966         -  /* If doNot is true, then add a TK_NOT Expr-node wrapper around the
   967         -  ** outside of *ppExpr.
   968         -  */
   969         -  static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){
   970         -    if( doNot ){
   971         -      pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0);
   972         -    }
   973         -  }
   974         -}
   975         -
   976         -expr(A) ::= LP(L) nexprlist(X) COMMA expr(Y) RP(R). {
   977         -  ExprList *pList = sqlite3ExprListAppend(pParse, X, Y.pExpr);
   978         -  A.pExpr = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
   979         -  if( A.pExpr ){
   980         -    A.pExpr->x.pList = pList;
   981         -    spanSet(&A, &L, &R);
          949  +expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
          950  +  ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
          951  +  A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
          952  +  if( A ){
          953  +    A->x.pList = pList;
   982    954     }else{
   983    955       sqlite3ExprListDelete(pParse->db, pList);
   984    956     }
   985    957   }
   986    958   
   987         -expr(A) ::= expr(A) AND(OP) expr(Y).    {spanBinaryExpr(pParse,@OP,&A,&Y);}
   988         -expr(A) ::= expr(A) OR(OP) expr(Y).     {spanBinaryExpr(pParse,@OP,&A,&Y);}
          959  +expr(A) ::= expr(A) AND(OP) expr(Y).    {A=sqlite3PExpr(pParse,@OP,A,Y);}
          960  +expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
   989    961   expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
   990         -                                        {spanBinaryExpr(pParse,@OP,&A,&Y);}
   991         -expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {spanBinaryExpr(pParse,@OP,&A,&Y);}
          962  +                                        {A=sqlite3PExpr(pParse,@OP,A,Y);}
          963  +expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
   992    964   expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
   993         -                                        {spanBinaryExpr(pParse,@OP,&A,&Y);}
          965  +                                        {A=sqlite3PExpr(pParse,@OP,A,Y);}
   994    966   expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
   995         -                                        {spanBinaryExpr(pParse,@OP,&A,&Y);}
          967  +                                        {A=sqlite3PExpr(pParse,@OP,A,Y);}
   996    968   expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
   997         -                                        {spanBinaryExpr(pParse,@OP,&A,&Y);}
   998         -expr(A) ::= expr(A) CONCAT(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);}
          969  +                                        {A=sqlite3PExpr(pParse,@OP,A,Y);}
          970  +expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);}
   999    971   %type likeop {Token}
  1000    972   likeop(A) ::= LIKE_KW|MATCH(A).
  1001    973   likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
  1002    974   expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
  1003    975     ExprList *pList;
  1004    976     int bNot = OP.n & 0x80000000;
  1005    977     OP.n &= 0x7fffffff;
  1006         -  pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
  1007         -  pList = sqlite3ExprListAppend(pParse,pList, A.pExpr);
  1008         -  A.pExpr = sqlite3ExprFunction(pParse, pList, &OP);
  1009         -  exprNot(pParse, bNot, &A);
  1010         -  A.zEnd = Y.zEnd;
  1011         -  if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
          978  +  pList = sqlite3ExprListAppend(pParse,0, Y);
          979  +  pList = sqlite3ExprListAppend(pParse,pList, A);
          980  +  A = sqlite3ExprFunction(pParse, pList, &OP);
          981  +  if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
          982  +  if( A ) A->flags |= EP_InfixFunc;
  1012    983   }
  1013    984   expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E).  [LIKE_KW]  {
  1014    985     ExprList *pList;
  1015    986     int bNot = OP.n & 0x80000000;
  1016    987     OP.n &= 0x7fffffff;
  1017         -  pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
  1018         -  pList = sqlite3ExprListAppend(pParse,pList, A.pExpr);
  1019         -  pList = sqlite3ExprListAppend(pParse,pList, E.pExpr);
  1020         -  A.pExpr = sqlite3ExprFunction(pParse, pList, &OP);
  1021         -  exprNot(pParse, bNot, &A);
  1022         -  A.zEnd = E.zEnd;
  1023         -  if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
          988  +  pList = sqlite3ExprListAppend(pParse,0, Y);
          989  +  pList = sqlite3ExprListAppend(pParse,pList, A);
          990  +  pList = sqlite3ExprListAppend(pParse,pList, E);
          991  +  A = sqlite3ExprFunction(pParse, pList, &OP);
          992  +  if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
          993  +  if( A ) A->flags |= EP_InfixFunc;
  1024    994   }
  1025    995   
  1026         -%include {
  1027         -  /* Construct an expression node for a unary postfix operator
  1028         -  */
  1029         -  static void spanUnaryPostfix(
  1030         -    Parse *pParse,         /* Parsing context to record errors */
  1031         -    int op,                /* The operator */
  1032         -    ExprSpan *pOperand,    /* The operand, and output */
  1033         -    Token *pPostOp         /* The operand token for setting the span */
  1034         -  ){
  1035         -    pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
  1036         -    pOperand->zEnd = &pPostOp->z[pPostOp->n];
  1037         -  }                           
  1038         -}
  1039         -
  1040         -expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {spanUnaryPostfix(pParse,@E,&A,&E);}
  1041         -expr(A) ::= expr(A) NOT NULL(E). {spanUnaryPostfix(pParse,TK_NOTNULL,&A,&E);}
          996  +expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {A = sqlite3PExpr(pParse,@E,A,0);}
          997  +expr(A) ::= expr(A) NOT NULL.    {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
  1042    998   
  1043    999   %include {
  1044   1000     /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
  1045   1001     ** unary TK_ISNULL or TK_NOTNULL expression. */
  1046   1002     static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
  1047   1003       sqlite3 *db = pParse->db;
  1048   1004       if( pA && pY && pY->op==TK_NULL ){
................................................................................
  1056   1012   //    expr1 IS expr2
  1057   1013   //    expr1 IS NOT expr2
  1058   1014   //
  1059   1015   // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL.  If expr2
  1060   1016   // is any other expression, code as TK_IS or TK_ISNOT.
  1061   1017   // 
  1062   1018   expr(A) ::= expr(A) IS expr(Y).     {
  1063         -  spanBinaryExpr(pParse,TK_IS,&A,&Y);
  1064         -  binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_ISNULL);
         1019  +  A = sqlite3PExpr(pParse,TK_IS,A,Y);
         1020  +  binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL);
  1065   1021   }
  1066   1022   expr(A) ::= expr(A) IS NOT expr(Y). {
  1067         -  spanBinaryExpr(pParse,TK_ISNOT,&A,&Y);
  1068         -  binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_NOTNULL);
         1023  +  A = sqlite3PExpr(pParse,TK_ISNOT,A,Y);
         1024  +  binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL);
  1069   1025   }
  1070         -
  1071         -%include {
  1072         -  /* Construct an expression node for a unary prefix operator
  1073         -  */
  1074         -  static void spanUnaryPrefix(
  1075         -    ExprSpan *pOut,        /* Write the new expression node here */
  1076         -    Parse *pParse,         /* Parsing context to record errors */
  1077         -    int op,                /* The operator */
  1078         -    ExprSpan *pOperand,    /* The operand */
  1079         -    Token *pPreOp         /* The operand token for setting the span */
  1080         -  ){
  1081         -    pOut->zStart = pPreOp->z;
  1082         -    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
  1083         -    pOut->zEnd = pOperand->zEnd;
  1084         -  }
  1085         -}
  1086         -
  1087         -
  1088   1026   
  1089   1027   expr(A) ::= NOT(B) expr(X).  
  1090         -              {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/}
         1028  +              {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
  1091   1029   expr(A) ::= BITNOT(B) expr(X).
  1092         -              {spanUnaryPrefix(&A,pParse,@B,&X,&B);/*A-overwrites-B*/}
  1093         -expr(A) ::= MINUS(B) expr(X). [BITNOT]
  1094         -              {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);/*A-overwrites-B*/}
  1095         -expr(A) ::= PLUS(B) expr(X). [BITNOT]
  1096         -              {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);/*A-overwrites-B*/}
         1030  +              {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/}
         1031  +expr(A) ::= MINUS expr(X). [BITNOT]
         1032  +              {A = sqlite3PExpr(pParse, TK_UMINUS, X, 0);}
         1033  +expr(A) ::= PLUS expr(X). [BITNOT]
         1034  +              {A = sqlite3PExpr(pParse, TK_UPLUS, X, 0);}
  1097   1035   
  1098   1036   %type between_op {int}
  1099   1037   between_op(A) ::= BETWEEN.     {A = 0;}
  1100   1038   between_op(A) ::= NOT BETWEEN. {A = 1;}
  1101   1039   expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
  1102         -  ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr);
  1103         -  pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr);
  1104         -  A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, A.pExpr, 0);
  1105         -  if( A.pExpr ){
  1106         -    A.pExpr->x.pList = pList;
         1040  +  ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
         1041  +  pList = sqlite3ExprListAppend(pParse,pList, Y);
         1042  +  A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
         1043  +  if( A ){
         1044  +    A->x.pList = pList;
  1107   1045     }else{
  1108   1046       sqlite3ExprListDelete(pParse->db, pList);
  1109   1047     } 
  1110         -  exprNot(pParse, N, &A);
  1111         -  A.zEnd = Y.zEnd;
         1048  +  if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
  1112   1049   }
  1113   1050   %ifndef SQLITE_OMIT_SUBQUERY
  1114   1051     %type in_op {int}
  1115   1052     in_op(A) ::= IN.      {A = 0;}
  1116   1053     in_op(A) ::= NOT IN.  {A = 1;}
  1117         -  expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP(E). [IN] {
         1054  +  expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
  1118   1055       if( Y==0 ){
  1119   1056         /* Expressions of the form
  1120   1057         **
  1121   1058         **      expr1 IN ()
  1122   1059         **      expr1 NOT IN ()
  1123   1060         **
  1124   1061         ** simplify to constants 0 (false) and 1 (true), respectively,
  1125   1062         ** regardless of the value of expr1.
  1126   1063         */
  1127         -      sqlite3ExprDelete(pParse->db, A.pExpr);
  1128         -      A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
         1064  +      sqlite3ExprDelete(pParse->db, A);
         1065  +      A = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
  1129   1066       }else if( Y->nExpr==1 ){
  1130   1067         /* Expressions of the form:
  1131   1068         **
  1132   1069         **      expr1 IN (?1)
  1133   1070         **      expr1 NOT IN (?2)
  1134   1071         **
  1135   1072         ** with exactly one value on the RHS can be simplified to something
................................................................................
  1148   1085         sqlite3ExprListDelete(pParse->db, Y);
  1149   1086         /* pRHS cannot be NULL because a malloc error would have been detected
  1150   1087         ** before now and control would have never reached this point */
  1151   1088         if( ALWAYS(pRHS) ){
  1152   1089           pRHS->flags &= ~EP_Collate;
  1153   1090           pRHS->flags |= EP_Generic;
  1154   1091         }
  1155         -      A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A.pExpr, pRHS);
         1092  +      A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
  1156   1093       }else{
  1157         -      A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
  1158         -      if( A.pExpr ){
  1159         -        A.pExpr->x.pList = Y;
  1160         -        sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
         1094  +      A = sqlite3PExpr(pParse, TK_IN, A, 0);
         1095  +      if( A ){
         1096  +        A->x.pList = Y;
         1097  +        sqlite3ExprSetHeightAndFlags(pParse, A);
  1161   1098         }else{
  1162   1099           sqlite3ExprListDelete(pParse->db, Y);
  1163   1100         }
  1164         -      exprNot(pParse, N, &A);
         1101  +      if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
  1165   1102       }
  1166         -    A.zEnd = &E.z[E.n];
  1167   1103     }
  1168         -  expr(A) ::= LP(B) select(X) RP(E). {
  1169         -    spanSet(&A,&B,&E); /*A-overwrites-B*/
  1170         -    A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
  1171         -    sqlite3PExprAddSelect(pParse, A.pExpr, X);
         1104  +  expr(A) ::= LP select(X) RP. {
         1105  +    A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
         1106  +    sqlite3PExprAddSelect(pParse, A, X);
  1172   1107     }
  1173         -  expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E).  [IN] {
  1174         -    A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
  1175         -    sqlite3PExprAddSelect(pParse, A.pExpr, Y);
  1176         -    exprNot(pParse, N, &A);
  1177         -    A.zEnd = &E.z[E.n];
         1108  +  expr(A) ::= expr(A) in_op(N) LP select(Y) RP.  [IN] {
         1109  +    A = sqlite3PExpr(pParse, TK_IN, A, 0);
         1110  +    sqlite3PExprAddSelect(pParse, A, Y);
         1111  +    if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
  1178   1112     }
  1179   1113     expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
  1180   1114       SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
  1181   1115       Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
  1182   1116       if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
  1183         -    A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
  1184         -    sqlite3PExprAddSelect(pParse, A.pExpr, pSelect);
  1185         -    exprNot(pParse, N, &A);
  1186         -    A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
         1117  +    A = sqlite3PExpr(pParse, TK_IN, A, 0);
         1118  +    sqlite3PExprAddSelect(pParse, A, pSelect);
         1119  +    if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
  1187   1120     }
  1188         -  expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
         1121  +  expr(A) ::= EXISTS LP select(Y) RP. {
  1189   1122       Expr *p;
  1190         -    spanSet(&A,&B,&E); /*A-overwrites-B*/
  1191         -    p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
         1123  +    p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
  1192   1124       sqlite3PExprAddSelect(pParse, p, Y);
  1193   1125     }
  1194   1126   %endif SQLITE_OMIT_SUBQUERY
  1195   1127   
  1196   1128   /* CASE expressions */
  1197         -expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
  1198         -  spanSet(&A,&C,&E);  /*A-overwrites-C*/
  1199         -  A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0);
  1200         -  if( A.pExpr ){
  1201         -    A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
  1202         -    sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
         1129  +expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
         1130  +  A = sqlite3PExpr(pParse, TK_CASE, X, 0);
         1131  +  if( A ){
         1132  +    A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
         1133  +    sqlite3ExprSetHeightAndFlags(pParse, A);
  1203   1134     }else{
  1204   1135       sqlite3ExprListDelete(pParse->db, Y);
  1205   1136       sqlite3ExprDelete(pParse->db, Z);
  1206   1137     }
  1207   1138   }
  1208   1139   %type case_exprlist {ExprList*}
  1209   1140   %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
  1210   1141   case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
  1211         -  A = sqlite3ExprListAppend(pParse,A, Y.pExpr);
  1212         -  A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
         1142  +  A = sqlite3ExprListAppend(pParse,A, Y);
         1143  +  A = sqlite3ExprListAppend(pParse,A, Z);
  1213   1144   }
  1214   1145   case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
  1215         -  A = sqlite3ExprListAppend(pParse,0, Y.pExpr);
  1216         -  A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
         1146  +  A = sqlite3ExprListAppend(pParse,0, Y);
         1147  +  A = sqlite3ExprListAppend(pParse,A, Z);
  1217   1148   }
  1218   1149   %type case_else {Expr*}
  1219   1150   %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
  1220         -case_else(A) ::=  ELSE expr(X).         {A = X.pExpr;}
         1151  +case_else(A) ::=  ELSE expr(X).         {A = X;}
  1221   1152   case_else(A) ::=  .                     {A = 0;} 
  1222   1153   %type case_operand {Expr*}
  1223   1154   %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
  1224         -case_operand(A) ::= expr(X).            {A = X.pExpr; /*A-overwrites-X*/} 
         1155  +case_operand(A) ::= expr(X).            {A = X; /*A-overwrites-X*/} 
  1225   1156   case_operand(A) ::= .                   {A = 0;} 
  1226   1157   
  1227   1158   %type exprlist {ExprList*}
  1228   1159   %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
  1229   1160   %type nexprlist {ExprList*}
  1230   1161   %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
  1231   1162   
  1232   1163   exprlist(A) ::= nexprlist(A).
  1233   1164   exprlist(A) ::= .                            {A = 0;}
  1234   1165   nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
  1235         -    {A = sqlite3ExprListAppend(pParse,A,Y.pExpr);}
         1166  +    {A = sqlite3ExprListAppend(pParse,A,Y);}
  1236   1167   nexprlist(A) ::= expr(Y).
  1237         -    {A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/}
         1168  +    {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/}
  1238   1169   
  1239   1170   %ifndef SQLITE_OMIT_SUBQUERY
  1240   1171   /* A paren_exprlist is an optional expression list contained inside
  1241   1172   ** of parenthesis */
  1242   1173   %type paren_exprlist {ExprList*}
  1243   1174   %destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
  1244   1175   paren_exprlist(A) ::= .   {A = 0;}
................................................................................
  1384   1315   
  1385   1316   foreach_clause ::= .
  1386   1317   foreach_clause ::= FOR EACH ROW.
  1387   1318   
  1388   1319   %type when_clause {Expr*}
  1389   1320   %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
  1390   1321   when_clause(A) ::= .             { A = 0; }
  1391         -when_clause(A) ::= WHEN expr(X). { A = X.pExpr; }
         1322  +when_clause(A) ::= WHEN expr(X). { A = X; }
  1392   1323   
  1393   1324   %type trigger_cmd_list {TriggerStep*}
  1394   1325   %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
  1395   1326   trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. {
  1396   1327     assert( A!=0 );
  1397   1328     A->pLast->pNext = X;
  1398   1329     A->pLast = X;
................................................................................
  1433   1364   
  1434   1365   
  1435   1366   
  1436   1367   %type trigger_cmd {TriggerStep*}
  1437   1368   %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
  1438   1369   // UPDATE 
  1439   1370   trigger_cmd(A) ::=
  1440         -   UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z).  
  1441         -   {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R);}
         1371  +   UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z) scanpt(E).  
         1372  +   {A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R, B.z, E);}
  1442   1373   
  1443   1374   // INSERT
  1444         -trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) idlist_opt(F) select(S).
  1445         -   {A = sqlite3TriggerInsertStep(pParse->db, &X, F, S, R);/*A-overwrites-R*/}
         1375  +trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO
         1376  +                      trnm(X) idlist_opt(F) select(S) scanpt(Z).
         1377  +   {A = sqlite3TriggerInsertStep(pParse->db,&X,F,S,R,B,Z);/*A-overwrites-R*/}
  1446   1378   
  1447   1379   // DELETE
  1448         -trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y).
  1449         -   {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
         1380  +trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E).
         1381  +   {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y, B.z, E);}
  1450   1382   
  1451   1383   // SELECT
  1452         -trigger_cmd(A) ::= select(X).
  1453         -   {A = sqlite3TriggerSelectStep(pParse->db, X); /*A-overwrites-X*/}
         1384  +trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
         1385  +   {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
  1454   1386   
  1455   1387   // The special RAISE expression that may occur in trigger programs
  1456         -expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
  1457         -  spanSet(&A,&X,&Y);  /*A-overwrites-X*/
  1458         -  A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
  1459         -  if( A.pExpr ){
  1460         -    A.pExpr->affinity = OE_Ignore;
         1388  +expr(A) ::= RAISE LP IGNORE RP.  {
         1389  +  A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
         1390  +  if( A ){
         1391  +    A->affinity = OE_Ignore;
  1461   1392     }
  1462   1393   }
  1463         -expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
  1464         -  spanSet(&A,&X,&Y);  /*A-overwrites-X*/
  1465         -  A.pExpr = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 
  1466         -  if( A.pExpr ) {
  1467         -    A.pExpr->affinity = (char)T;
         1394  +expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP.  {
         1395  +  A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 
         1396  +  if( A ) {
         1397  +    A->affinity = (char)T;
  1468   1398     }
  1469   1399   }
  1470   1400   %endif  !SQLITE_OMIT_TRIGGER
  1471   1401   
  1472   1402   %type raisetype {int}
  1473   1403   raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
  1474   1404   raisetype(A) ::= ABORT.     {A = OE_Abort;}
................................................................................
  1481   1411     sqlite3DropTrigger(pParse,X,NOERR);
  1482   1412   }
  1483   1413   %endif  !SQLITE_OMIT_TRIGGER
  1484   1414   
  1485   1415   //////////////////////// ATTACH DATABASE file AS name /////////////////////////
  1486   1416   %ifndef SQLITE_OMIT_ATTACH
  1487   1417   cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
  1488         -  sqlite3Attach(pParse, F.pExpr, D.pExpr, K);
         1418  +  sqlite3Attach(pParse, F, D, K);
  1489   1419   }
  1490   1420   cmd ::= DETACH database_kw_opt expr(D). {
  1491         -  sqlite3Detach(pParse, D.pExpr);
         1421  +  sqlite3Detach(pParse, D);
  1492   1422   }
  1493   1423   
  1494   1424   %type key_opt {Expr*}
  1495   1425   %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
  1496   1426   key_opt(A) ::= .                     { A = 0; }
  1497         -key_opt(A) ::= KEY expr(X).          { A = X.pExpr; }
         1427  +key_opt(A) ::= KEY expr(X).          { A = X; }
  1498   1428   
  1499   1429   database_kw_opt ::= DATABASE.
  1500   1430   database_kw_opt ::= .
  1501   1431   %endif SQLITE_OMIT_ATTACH
  1502   1432   
  1503   1433   ////////////////////////// REINDEX collation //////////////////////////////////
  1504   1434   %ifndef SQLITE_OMIT_REINDEX

Changes to src/select.c.

  1377   1377     char const *zOrigDb = 0;
  1378   1378     char const *zOrigTab = 0;
  1379   1379     char const *zOrigCol = 0;
  1380   1380   #endif
  1381   1381   
  1382   1382     assert( pExpr!=0 );
  1383   1383     assert( pNC->pSrcList!=0 );
         1384  +  assert( pExpr->op!=TK_AGG_COLUMN );  /* This routine runes before aggregates
         1385  +                                       ** are processed */
  1384   1386     switch( pExpr->op ){
  1385         -    case TK_AGG_COLUMN:
  1386   1387       case TK_COLUMN: {
  1387   1388         /* The expression is a column. Locate the table the column is being
  1388   1389         ** extracted from in NameContext.pSrcList. This table may be real
  1389   1390         ** database table or a subquery.
  1390   1391         */
  1391   1392         Table *pTab = 0;            /* Table structure column is extracted from */
  1392   1393         Select *pS = 0;             /* Select the column is extracted from */
  1393   1394         int iCol = pExpr->iColumn;  /* Index of column in pTab */
  1394         -      testcase( pExpr->op==TK_AGG_COLUMN );
  1395         -      testcase( pExpr->op==TK_COLUMN );
  1396   1395         while( pNC && !pTab ){
  1397   1396           SrcList *pTabList = pNC->pSrcList;
  1398   1397           for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
  1399   1398           if( j<pTabList->nSrc ){
  1400   1399             pTab = pTabList->a[j].pTab;
  1401   1400             pS = pTabList->a[j].pSelect;
  1402   1401           }else{
................................................................................
  1592   1591       return;
  1593   1592     }
  1594   1593   #endif
  1595   1594   
  1596   1595     if( pParse->colNamesSet || db->mallocFailed ) return;
  1597   1596     /* Column names are determined by the left-most term of a compound select */
  1598   1597     while( pSelect->pPrior ) pSelect = pSelect->pPrior;
         1598  +  SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
  1599   1599     pTabList = pSelect->pSrc;
  1600   1600     pEList = pSelect->pEList;
  1601   1601     assert( v!=0 );
  1602   1602     assert( pTabList!=0 );
  1603   1603     pParse->colNamesSet = 1;
  1604   1604     fullName = (db->flags & SQLITE_FullColNames)!=0;
  1605   1605     srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
................................................................................
  1700   1700         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1701   1701       }else{
  1702   1702         Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
  1703   1703         while( pColExpr->op==TK_DOT ){
  1704   1704           pColExpr = pColExpr->pRight;
  1705   1705           assert( pColExpr!=0 );
  1706   1706         }
  1707         -      if( (pColExpr->op==TK_COLUMN || pColExpr->op==TK_AGG_COLUMN)
  1708         -       && pColExpr->pTab!=0 
  1709         -      ){
         1707  +      assert( pColExpr->op!=TK_AGG_COLUMN );
         1708  +      if( pColExpr->op==TK_COLUMN ){
  1710   1709           /* For columns use the column name name */
  1711   1710           int iCol = pColExpr->iColumn;
  1712   1711           Table *pTab = pColExpr->pTab;
         1712  +        assert( pTab!=0 );
  1713   1713           if( iCol<0 ) iCol = pTab->iPKey;
  1714   1714           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
  1715   1715         }else if( pColExpr->op==TK_ID ){
  1716   1716           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
  1717   1717           zName = pColExpr->u.zToken;
  1718   1718         }else{
  1719   1719           /* Use the original text of the column expression as its name */

Changes to src/shell.c.in.

   792    792   */
   793    793   #define SQLITE_EXTENSION_INIT1
   794    794   #define SQLITE_EXTENSION_INIT2(X) (void)(X)
   795    795   
   796    796   INCLUDE ../ext/misc/shathree.c
   797    797   INCLUDE ../ext/misc/fileio.c
   798    798   INCLUDE ../ext/misc/completion.c
          799  +INCLUDE ../ext/expert/sqlite3expert.h
          800  +INCLUDE ../ext/expert/sqlite3expert.c
   799    801   
   800    802   #if defined(SQLITE_ENABLE_SESSION)
   801    803   /*
   802    804   ** State information for a single open session
   803    805   */
   804    806   typedef struct OpenSession OpenSession;
   805    807   struct OpenSession {
................................................................................
   817    819   typedef struct SavedModeInfo SavedModeInfo;
   818    820   struct SavedModeInfo {
   819    821     int valid;          /* Is there legit data in here? */
   820    822     int mode;           /* Mode prior to ".explain on" */
   821    823     int showHeader;     /* The ".header" setting prior to ".explain on" */
   822    824     int colWidth[100];  /* Column widths prior to ".explain on" */
   823    825   };
          826  +
          827  +typedef struct ExpertInfo ExpertInfo;
          828  +struct ExpertInfo {
          829  +  sqlite3expert *pExpert;
          830  +  int bVerbose;
          831  +};
   824    832   
   825    833   /*
   826    834   ** State information about the database connection is contained in an
   827    835   ** instance of the following structure.
   828    836   */
   829    837   typedef struct ShellState ShellState;
   830    838   struct ShellState {
................................................................................
   862    870     int *aiIndent;         /* Array of indents used in MODE_Explain */
   863    871     int nIndent;           /* Size of array aiIndent[] */
   864    872     int iIndent;           /* Index of current op in aiIndent[] */
   865    873   #if defined(SQLITE_ENABLE_SESSION)
   866    874     int nSession;             /* Number of active sessions */
   867    875     OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
   868    876   #endif
          877  +  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
   869    878   };
          879  +
          880  +/* Allowed values for ShellState.autoEQP
          881  +*/
          882  +#define AUTOEQP_off      0
          883  +#define AUTOEQP_on       1
          884  +#define AUTOEQP_trigger  2
          885  +#define AUTOEQP_full     3
   870    886   
   871    887   /*
   872    888   ** These are the allowed shellFlgs values
   873    889   */
   874    890   #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
   875    891   #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
   876    892   #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
................................................................................
  2244   2260       }else{
  2245   2261         do{
  2246   2262           rc = sqlite3_step(pStmt);
  2247   2263         } while( rc == SQLITE_ROW );
  2248   2264       }
  2249   2265     }
  2250   2266   }
         2267  +
         2268  +/*
         2269  +** This function is called to process SQL if the previous shell command
         2270  +** was ".expert". It passes the SQL in the second argument directly to
         2271  +** the sqlite3expert object.
         2272  +**
         2273  +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
         2274  +** code. In this case, (*pzErr) may be set to point to a buffer containing
         2275  +** an English language error message. It is the responsibility of the
         2276  +** caller to eventually free this buffer using sqlite3_free().
         2277  +*/
         2278  +static int expertHandleSQL(
         2279  +  ShellState *pState, 
         2280  +  const char *zSql, 
         2281  +  char **pzErr
         2282  +){
         2283  +  assert( pState->expert.pExpert );
         2284  +  assert( pzErr==0 || *pzErr==0 );
         2285  +  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
         2286  +}
         2287  +
         2288  +/*
         2289  +** This function is called either to silently clean up the object
         2290  +** created by the ".expert" command (if bCancel==1), or to generate a 
         2291  +** report from it and then clean it up (if bCancel==0).
         2292  +**
         2293  +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
         2294  +** code. In this case, (*pzErr) may be set to point to a buffer containing
         2295  +** an English language error message. It is the responsibility of the
         2296  +** caller to eventually free this buffer using sqlite3_free().
         2297  +*/
         2298  +static int expertFinish(
         2299  +  ShellState *pState,
         2300  +  int bCancel,
         2301  +  char **pzErr
         2302  +){
         2303  +  int rc = SQLITE_OK;
         2304  +  sqlite3expert *p = pState->expert.pExpert;
         2305  +  assert( p );
         2306  +  assert( bCancel || pzErr==0 || *pzErr==0 );
         2307  +  if( bCancel==0 ){
         2308  +    FILE *out = pState->out;
         2309  +    int bVerbose = pState->expert.bVerbose;
         2310  +
         2311  +    rc = sqlite3_expert_analyze(p, pzErr);
         2312  +    if( rc==SQLITE_OK ){
         2313  +      int nQuery = sqlite3_expert_count(p);
         2314  +      int i;
         2315  +
         2316  +      if( bVerbose ){
         2317  +        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
         2318  +        raw_printf(out, "-- Candidates -----------------------------\n");
         2319  +        raw_printf(out, "%s\n", zCand);
         2320  +      }
         2321  +      for(i=0; i<nQuery; i++){
         2322  +        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
         2323  +        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
         2324  +        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
         2325  +        if( zIdx==0 ) zIdx = "(no new indexes)\n";
         2326  +        if( bVerbose ){
         2327  +          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
         2328  +          raw_printf(out, "%s\n\n", zSql);
         2329  +        }
         2330  +        raw_printf(out, "%s\n", zIdx);
         2331  +        raw_printf(out, "%s\n", zEQP);
         2332  +      }
         2333  +    }
         2334  +  }
         2335  +  sqlite3_expert_destroy(p);
         2336  +  pState->expert.pExpert = 0;
         2337  +  return rc;
         2338  +}
         2339  +
  2251   2340   
  2252   2341   /*
  2253   2342   ** Execute a statement or set of statements.  Print
  2254   2343   ** any result rows/columns depending on the current mode
  2255   2344   ** set via the supplied callback.
  2256   2345   **
  2257   2346   ** This is very similar to SQLite's built-in sqlite3_exec()
................................................................................
  2270   2359     int rc = SQLITE_OK;             /* Return Code */
  2271   2360     int rc2;
  2272   2361     const char *zLeftover;          /* Tail of unprocessed SQL */
  2273   2362   
  2274   2363     if( pzErrMsg ){
  2275   2364       *pzErrMsg = NULL;
  2276   2365     }
         2366  +
         2367  +  if( pArg->expert.pExpert ){
         2368  +    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
         2369  +    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
         2370  +  }
  2277   2371   
  2278   2372     while( zSql[0] && (SQLITE_OK == rc) ){
  2279   2373       static const char *zStmtSql;
  2280   2374       rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
  2281   2375       if( SQLITE_OK != rc ){
  2282   2376         if( pzErrMsg ){
  2283   2377           *pzErrMsg = save_err_msg(db);
................................................................................
  2304   2398           utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
  2305   2399         }
  2306   2400   
  2307   2401         /* Show the EXPLAIN QUERY PLAN if .eqp is on */
  2308   2402         if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
  2309   2403           sqlite3_stmt *pExplain;
  2310   2404           char *zEQP;
         2405  +        int triggerEQP = 0;
  2311   2406           disable_debug_trace_modes();
         2407  +        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
         2408  +        if( pArg->autoEQP>=AUTOEQP_trigger ){
         2409  +          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
         2410  +        }
  2312   2411           zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
  2313   2412           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
  2314   2413           if( rc==SQLITE_OK ){
  2315   2414             while( sqlite3_step(pExplain)==SQLITE_ROW ){
  2316   2415               raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
  2317   2416               raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
  2318   2417               raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
  2319   2418               utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
  2320   2419             }
  2321   2420           }
  2322   2421           sqlite3_finalize(pExplain);
  2323   2422           sqlite3_free(zEQP);
  2324         -        if( pArg->autoEQP>=2 ){
         2423  +        if( pArg->autoEQP>=AUTOEQP_full ){
  2325   2424             /* Also do an EXPLAIN for ".eqp full" mode */
  2326   2425             zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
  2327   2426             rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
  2328   2427             if( rc==SQLITE_OK ){
  2329   2428               pArg->cMode = MODE_Explain;
  2330   2429               explain_data_prepare(pArg, pExplain);
  2331   2430               exec_prepared_stmt(pArg, pExplain, xCallback);
  2332   2431               explain_data_delete(pArg);
  2333   2432             }
  2334   2433             sqlite3_finalize(pExplain);
  2335   2434             sqlite3_free(zEQP);
  2336   2435           }
         2436  +        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, triggerEQP, 0);
  2337   2437           restore_debug_trace_modes();
  2338   2438         }
  2339   2439   
  2340   2440         if( pArg ){
  2341   2441           pArg->cMode = pArg->mode;
  2342   2442           if( pArg->autoExplain
  2343   2443            && sqlite3_column_count(pStmt)==8
................................................................................
  2688   2788     ".dbinfo ?DB?           Show status information about the database\n"
  2689   2789     ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
  2690   2790     "                         If TABLE specified, only dump tables matching\n"
  2691   2791     "                         LIKE pattern TABLE.\n"
  2692   2792     ".echo on|off           Turn command echo on or off\n"
  2693   2793     ".eqp on|off|full       Enable or disable automatic EXPLAIN QUERY PLAN\n"
  2694   2794     ".exit                  Exit this program\n"
         2795  +  ".expert                EXPERIMENTAL. Suggest indexes for specified queries\n"
  2695   2796   /* Because explain mode comes on automatically now, the ".explain" mode
  2696   2797   ** is removed from the help screen.  It is still supported for legacy, however */
  2697   2798   /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
  2698   2799     ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
  2699   2800     ".headers on|off        Turn display of headers on or off\n"
  2700   2801     ".help                  Show this message\n"
  2701   2802     ".import FILE TABLE     Import data from FILE into TABLE\n"
................................................................................
  4064   4165    usage:
  4065   4166     raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
  4066   4167     raw_printf(stderr, "Where sub-commands are:\n");
  4067   4168     raw_printf(stderr, "    fkey-indexes\n");
  4068   4169     return SQLITE_ERROR;
  4069   4170   }
  4070   4171   
         4172  +/*
         4173  +** Implementation of ".expert" dot command.
         4174  +*/
         4175  +static int expertDotCommand(
         4176  +  ShellState *pState,             /* Current shell tool state */
         4177  +  char **azArg,                   /* Array of arguments passed to dot command */
         4178  +  int nArg                        /* Number of entries in azArg[] */
         4179  +){
         4180  +  int rc = SQLITE_OK;
         4181  +  char *zErr = 0;
         4182  +  int i;
         4183  +  int iSample = 0;
         4184  +
         4185  +  assert( pState->expert.pExpert==0 );
         4186  +  memset(&pState->expert, 0, sizeof(ExpertInfo));
         4187  +
         4188  +  for(i=1; rc==SQLITE_OK && i<nArg; i++){
         4189  +    char *z = azArg[i];
         4190  +    int n;
         4191  +    if( z[0]=='-' && z[1]=='-' ) z++;
         4192  +    n = strlen(z);
         4193  +    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
         4194  +      pState->expert.bVerbose = 1;
         4195  +    }
         4196  +    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
         4197  +      if( i==(nArg-1) ){
         4198  +        raw_printf(stderr, "option requires an argument: %s\n", z);
         4199  +        rc = SQLITE_ERROR;
         4200  +      }else{
         4201  +        iSample = (int)integerValue(azArg[++i]);
         4202  +        if( iSample<0 || iSample>100 ){
         4203  +          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
         4204  +          rc = SQLITE_ERROR;
         4205  +        }
         4206  +      }
         4207  +    }
         4208  +    else{
         4209  +      raw_printf(stderr, "unknown option: %s\n", z);
         4210  +      rc = SQLITE_ERROR;
         4211  +    }
         4212  +  }
         4213  +
         4214  +  if( rc==SQLITE_OK ){
         4215  +    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
         4216  +    if( pState->expert.pExpert==0 ){
         4217  +      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
         4218  +      rc = SQLITE_ERROR;
         4219  +    }else{
         4220  +      sqlite3_expert_config(
         4221  +          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
         4222  +      );
         4223  +    }
         4224  +  }
         4225  +
         4226  +  return rc;
         4227  +}
         4228  +
         4229  +
  4071   4230   
  4072   4231   /*
  4073   4232   ** If an input line begins with "." then invoke this routine to
  4074   4233   ** process that line.
  4075   4234   **
  4076   4235   ** Return 1 on error, 2 to exit, and 0 otherwise.
  4077   4236   */
  4078   4237   static int do_meta_command(char *zLine, ShellState *p){
  4079   4238     int h = 1;
  4080   4239     int nArg = 0;
  4081   4240     int n, c;
  4082   4241     int rc = 0;
  4083   4242     char *azArg[50];
         4243  +
         4244  +  if( p->expert.pExpert ){
         4245  +    expertFinish(p, 1, 0);
         4246  +  }
  4084   4247   
  4085   4248     /* Parse the input line into tokens.
  4086   4249     */
  4087   4250     while( zLine[h] && nArg<ArraySize(azArg) ){
  4088   4251       while( IsSpace(zLine[h]) ){ h++; }
  4089   4252       if( zLine[h]==0 ) break;
  4090   4253       if( zLine[h]=='\'' || zLine[h]=='"' ){
................................................................................
  4393   4556         rc = 1;
  4394   4557       }
  4395   4558     }else
  4396   4559   
  4397   4560     if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
  4398   4561       if( nArg==2 ){
  4399   4562         if( strcmp(azArg[1],"full")==0 ){
  4400         -        p->autoEQP = 2;
         4563  +        p->autoEQP = AUTOEQP_full;
         4564  +      }else if( strcmp(azArg[1],"trigger")==0 ){
         4565  +        p->autoEQP = AUTOEQP_trigger;
  4401   4566         }else{
  4402   4567           p->autoEQP = booleanValue(azArg[1]);
  4403   4568         }
  4404   4569       }else{
  4405         -      raw_printf(stderr, "Usage: .eqp on|off|full\n");
         4570  +      raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
  4406   4571         rc = 1;
  4407   4572       }
  4408   4573     }else
  4409   4574   
  4410   4575     if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
  4411   4576       if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
  4412   4577       rc = 2;
................................................................................
  4431   4596         if( p->mode==MODE_Explain ) p->mode = p->normalMode;
  4432   4597         p->autoExplain = 0;
  4433   4598       }else if( val==99 ){
  4434   4599         if( p->mode==MODE_Explain ) p->mode = p->normalMode;
  4435   4600         p->autoExplain = 1;
  4436   4601       }
  4437   4602     }else
         4603  +
         4604  +  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
         4605  +    open_db(p, 0);
         4606  +    expertDotCommand(p, azArg, nArg);
         4607  +  }else
  4438   4608   
  4439   4609     if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
  4440   4610       ShellState data;
  4441   4611       char *zErrMsg = 0;
  4442   4612       int doStats = 0;
  4443   4613       memcpy(&data, p, sizeof(data));
  4444   4614       data.showHeader = 0;
................................................................................
  5746   5916       }
  5747   5917       x = system(zCmd);
  5748   5918       sqlite3_free(zCmd);
  5749   5919       if( x ) raw_printf(stderr, "System command returns %d\n", x);
  5750   5920     }else
  5751   5921   
  5752   5922     if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
  5753         -    static const char *azBool[] = { "off", "on", "full", "unk" };
         5923  +    static const char *azBool[] = { "off", "on", "trigger", "full"};
  5754   5924       int i;
  5755   5925       if( nArg!=1 ){
  5756   5926         raw_printf(stderr, "Usage: .show\n");
  5757   5927         rc = 1;
  5758   5928         goto meta_command_exit;
  5759   5929       }
  5760   5930       utf8_printf(p->out, "%12.12s: %s\n","echo",
................................................................................
  5934   6104         { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
  5935   6105   #ifdef SQLITE_N_KEYWORD
  5936   6106         { "iskeyword",          SQLITE_TESTCTRL_ISKEYWORD,     "IDENTIFIER"         },
  5937   6107   #endif
  5938   6108         { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
  5939   6109         { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
  5940   6110         { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
         6111  +#ifdef YYCOVERAGE
         6112  +      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
         6113  +#endif
  5941   6114         { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
  5942   6115         { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
  5943   6116         { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
  5944   6117         { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
  5945   6118         { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
  5946   6119       };
  5947   6120       int testctrl = -1;
................................................................................
  6059   6232               rc2 = sqlite3_test_control(testctrl, p->db,
  6060   6233                             azArg[2],
  6061   6234                             integerValue(azArg[3]),
  6062   6235                             integerValue(azArg[4]));
  6063   6236               isOk = 3;
  6064   6237             }
  6065   6238             break;
         6239  +
         6240  +#ifdef YYCOVERAGE
         6241  +        case SQLITE_TESTCTRL_PARSER_COVERAGE:
         6242  +          if( nArg==2 ){
         6243  +            sqlite3_test_control(testctrl, p->out);
         6244  +            isOk = 3;
         6245  +          }
         6246  +#endif
  6066   6247         }
  6067   6248       }
  6068   6249       if( isOk==0 && iCtrl>=0 ){
  6069   6250         utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
  6070   6251         rc = 1;
  6071   6252       }else if( isOk==1 ){
  6072   6253         raw_printf(p->out, "%d\n", rc2);
................................................................................
  6898   7079       }else if( strcmp(z,"-header")==0 ){
  6899   7080         data.showHeader = 1;
  6900   7081       }else if( strcmp(z,"-noheader")==0 ){
  6901   7082         data.showHeader = 0;
  6902   7083       }else if( strcmp(z,"-echo")==0 ){
  6903   7084         ShellSetFlag(&data, SHFLG_Echo);
  6904   7085       }else if( strcmp(z,"-eqp")==0 ){
  6905         -      data.autoEQP = 1;
         7086  +      data.autoEQP = AUTOEQP_on;
  6906   7087       }else if( strcmp(z,"-eqpfull")==0 ){
  6907         -      data.autoEQP = 2;
         7088  +      data.autoEQP = AUTOEQP_full;
  6908   7089       }else if( strcmp(z,"-stats")==0 ){
  6909   7090         data.statsOn = 1;
  6910   7091       }else if( strcmp(z,"-scanstats")==0 ){
  6911   7092         data.scanstatsOn = 1;
  6912   7093       }else if( strcmp(z,"-backslash")==0 ){
  6913   7094         /* Undocumented command-line option: -backslash
  6914   7095         ** Causes C-style backslash escapes to be evaluated in SQL statements

Changes to src/sqlite.h.in.

  2066   2066   ** a single SQL query statement will always use the same algorithm regardless
  2067   2067   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  2068   2068   ** that look at the values of bound parameters, which can make some queries
  2069   2069   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  2070   2070   ** the QPSG active, SQLite will always use the same query plan in the field as
  2071   2071   ** was used during testing in the lab.
  2072   2072   ** </dd>
  2073         -** <dt>SQLITE_DBCONFIG_FULL_EQP</dt>
         2073  +** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  2074   2074   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  2075   2075   ** include output for any operations performed by trigger programs. This
  2076   2076   ** option is used to set or clear (the default) a flag that governs this
  2077   2077   ** behavior. The first parameter passed to this operation is an integer -
  2078   2078   ** non-zero to enable output for trigger programs, or zero to disable it.
  2079   2079   ** The second parameter is a pointer to an integer into which is written 
  2080   2080   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
................................................................................
  2086   2086   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  2087   2087   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  2088   2088   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  2089   2089   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  2090   2090   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  2091   2091   #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
  2092   2092   #define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
  2093         -#define SQLITE_DBCONFIG_FULL_EQP              1008 /* int int* */
         2093  +#define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
         2094  +#define SQLITE_DBCONFIG_MAX                   1008 /* Largest DBCONFIG */
  2094   2095   
  2095   2096   /*
  2096   2097   ** CAPI3REF: Enable Or Disable Extended Result Codes
  2097   2098   ** METHOD: sqlite3
  2098   2099   **
  2099   2100   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  2100   2101   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  7037   7038   #define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  7038   7039   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  7039   7040   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  7040   7041   #define SQLITE_TESTCTRL_BYTEORDER               22
  7041   7042   #define SQLITE_TESTCTRL_ISINIT                  23
  7042   7043   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  7043   7044   #define SQLITE_TESTCTRL_IMPOSTER                25
  7044         -#define SQLITE_TESTCTRL_LAST                    25
         7045  +#define SQLITE_TESTCTRL_PARSER_COVERAGE         26
         7046  +#define SQLITE_TESTCTRL_LAST                    26  /* Largest TESTCTRL */
  7045   7047   
  7046   7048   /*
  7047   7049   ** CAPI3REF: SQLite Runtime Status
  7048   7050   **
  7049   7051   ** ^These interfaces are used to retrieve runtime status information
  7050   7052   ** about the performance of SQLite, and optionally to reset various
  7051   7053   ** highwater marks.  ^The first argument is an integer code for
................................................................................
  8297   8299   
  8298   8300   /*
  8299   8301   ** CAPI3REF: Determine The Collation For a Virtual Table Constraint
  8300   8302   **
  8301   8303   ** This function may only be called from within a call to the [xBestIndex]
  8302   8304   ** method of a [virtual table implementation]. 
  8303   8305   **
  8304         -** The first argument must be the database handle with which the virtual 
  8305         -** table is associated (the one passed to the [xConnect] or [xCreate] method 
  8306         -** to create the sqlite3_vtab object. The second argument must be an index
  8307         -** into the aConstraint[] array belonging to the sqlite3_index_info structure
  8308         -** passed to xBestIndex. This function returns a pointer to a buffer 
         8306  +** The first argument must be the sqlite3_index_info object that is the
         8307  +** first parameter to the xBestIndex() method. The second argument must be
         8308  +** an index into the aConstraint[] array belonging to the sqlite3_index_info
         8309  +** structure passed to xBestIndex. This function returns a pointer to a buffer 
  8309   8310   ** containing the name of the collation sequence for the corresponding
  8310   8311   ** constraint.
  8311   8312   */
  8312         -SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3*, int);
         8313  +SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
  8313   8314   
  8314   8315   /*
  8315   8316   ** CAPI3REF: Conflict resolution modes
  8316   8317   ** KEYWORDS: {conflict resolution mode}
  8317   8318   **
  8318   8319   ** These constants are returned by [sqlite3_vtab_on_conflict()] to
  8319   8320   ** inform a [virtual table] implementation what the [ON CONFLICT] mode

Changes to src/sqliteInt.h.

  1059   1059   typedef struct Bitvec Bitvec;
  1060   1060   typedef struct CollSeq CollSeq;
  1061   1061   typedef struct Column Column;
  1062   1062   typedef struct Db Db;
  1063   1063   typedef struct Schema Schema;
  1064   1064   typedef struct Expr Expr;
  1065   1065   typedef struct ExprList ExprList;
  1066         -typedef struct ExprSpan ExprSpan;
  1067   1066   typedef struct FKey FKey;
  1068   1067   typedef struct FuncDestructor FuncDestructor;
  1069   1068   typedef struct FuncDef FuncDef;
  1070   1069   typedef struct FuncDefHash FuncDefHash;
  1071   1070   typedef struct IdList IdList;
  1072   1071   typedef struct Index Index;
  1073   1072   typedef struct IndexSample IndexSample;
................................................................................
  1352   1351     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
  1353   1352     u8 suppressErr;               /* Do not issue error messages if true */
  1354   1353     u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
  1355   1354     u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
  1356   1355     u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
  1357   1356     u8 skipBtreeMutex;            /* True if no shared-cache backends */
  1358   1357     u8 nSqlExec;                  /* Number of pending OP_SqlExec opcodes */
  1359         -  u8 bFullEQP;                  /* Include triggers in EQP output */
  1360   1358     int nextPagesize;             /* Pagesize after VACUUM if >0 */
  1361   1359     u32 magic;                    /* Magic number for detect library misuse */
  1362   1360     int nChange;                  /* Value returned by sqlite3_changes() */
  1363   1361     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
  1364   1362     int aLimit[SQLITE_N_LIMIT];   /* Limits */
  1365   1363     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
  1366   1364     struct sqlite3InitInfo {      /* Information used during initialization */
................................................................................
  1417   1415     unsigned nProgressOps;        /* Number of opcodes for progress callback */
  1418   1416   #endif
  1419   1417   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1420   1418     int nVTrans;                  /* Allocated size of aVTrans */
  1421   1419     Hash aModule;                 /* populated by sqlite3_create_module() */
  1422   1420     VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
  1423   1421     VTable **aVTrans;             /* Virtual tables with open transactions */
  1424         -  VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
  1425         -  void *pBestIndexCtx;          /* For sqlite3_vtab_collation() */
         1422  +  VTable *pDisconnect;          /* Disconnect these in next sqlite3_prepare() */
  1426   1423   #endif
  1427   1424     Hash aFunc;                   /* Hash table of connection functions */
  1428   1425     Hash aCollSeq;                /* All collating sequences */
  1429   1426     BusyHandler busyHandler;      /* Busy callback */
  1430   1427     Db aDbStatic[2];              /* Static space for the 2 default backends */
  1431   1428     Savepoint *pSavepoint;        /* List of active savepoints */
  1432   1429     int busyTimeout;              /* Busy handler timeout, in msec */
................................................................................
  1493   1490   #define SQLITE_LoadExtension  0x00010000  /* Enable load_extension */
  1494   1491   #define SQLITE_LoadExtFunc    0x00020000  /* Enable load_extension() SQL func */
  1495   1492   #define SQLITE_EnableTrigger  0x00040000  /* True to enable triggers */
  1496   1493   #define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
  1497   1494   #define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
  1498   1495   #define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
  1499   1496   #define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
  1500         -#define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee */
         1497  +#define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
         1498  +#define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
         1499  +
  1501   1500   /* Flags used only if debugging */
  1502   1501   #ifdef SQLITE_DEBUG
  1503   1502   #define SQLITE_SqlTrace       0x08000000  /* Debug print SQL as it executes */
  1504   1503   #define SQLITE_VdbeListing    0x10000000  /* Debug listings of VDBE programs */
  1505   1504   #define SQLITE_VdbeTrace      0x20000000  /* True to trace VDBE execution */
  1506   1505   #define SQLITE_VdbeAddopTrace 0x40000000  /* Trace sqlite3VdbeAddOp() calls */
  1507   1506   #define SQLITE_VdbeEQP        0x80000000  /* Debug EXPLAIN QUERY PLAN */
................................................................................
  2501   2500           u16 iAlias;           /* Index into Parse.aAlias[] for zName */
  2502   2501         } x;
  2503   2502         int iConstExprReg;      /* Register in which Expr value is cached */
  2504   2503       } u;
  2505   2504     } a[1];                  /* One slot for each expression in the list */
  2506   2505   };
  2507   2506   
  2508         -/*
  2509         -** An instance of this structure is used by the parser to record both
  2510         -** the parse tree for an expression and the span of input text for an
  2511         -** expression.
  2512         -*/
  2513         -struct ExprSpan {
  2514         -  Expr *pExpr;          /* The expression parse tree */
  2515         -  const char *zStart;   /* First character of input text */
  2516         -  const char *zEnd;     /* One character past the end of input text */
  2517         -};
  2518         -
  2519   2507   /*
  2520   2508   ** An instance of this structure can hold a simple list of identifiers,
  2521   2509   ** such as the list "a,b,c" in the following statements:
  2522   2510   **
  2523   2511   **      INSERT INTO t(a,b,c) VALUES ...;
  2524   2512   **      CREATE INDEX idx ON t(a,b,c);
  2525   2513   **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
................................................................................
  3212   3200     u8 orconf;           /* OE_Rollback etc. */
  3213   3201     Trigger *pTrig;      /* The trigger that this step is a part of */
  3214   3202     Select *pSelect;     /* SELECT statement or RHS of INSERT INTO SELECT ... */
  3215   3203     char *zTarget;       /* Target table for DELETE, UPDATE, INSERT */
  3216   3204     Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
  3217   3205     ExprList *pExprList; /* SET clause for UPDATE. */
  3218   3206     IdList *pIdList;     /* Column names for INSERT */
         3207  +  char *zSpan;         /* Original SQL text of this command */
  3219   3208     TriggerStep *pNext;  /* Next in the link-list */
  3220   3209     TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
  3221   3210   };
  3222   3211   
  3223   3212   /*
  3224   3213   ** The following structure contains information used by the sqliteFix...
  3225   3214   ** routines as they walk the parse tree to make database references
................................................................................
  3522   3511   void *sqlite3Malloc(u64);
  3523   3512   void *sqlite3MallocZero(u64);
  3524   3513   void *sqlite3DbMallocZero(sqlite3*, u64);
  3525   3514   void *sqlite3DbMallocRaw(sqlite3*, u64);
  3526   3515   void *sqlite3DbMallocRawNN(sqlite3*, u64);
  3527   3516   char *sqlite3DbStrDup(sqlite3*,const char*);
  3528   3517   char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
         3518  +char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
  3529   3519   void *sqlite3Realloc(void*, u64);
  3530   3520   void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
  3531   3521   void *sqlite3DbRealloc(sqlite3 *, void *, u64);
  3532   3522   void sqlite3DbFree(sqlite3*, void*);
  3533   3523   void sqlite3DbFreeNN(sqlite3*, void*);
  3534   3524   int sqlite3MallocSize(void*);
  3535   3525   int sqlite3DbMallocSize(sqlite3*, void*);
................................................................................
  3660   3650   Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
  3661   3651   void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
  3662   3652   void sqlite3ExprDelete(sqlite3*, Expr*);
  3663   3653   ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  3664   3654   ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
  3665   3655   void sqlite3ExprListSetSortOrder(ExprList*,int);
  3666   3656   void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
  3667         -void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
         3657  +void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
  3668   3658   void sqlite3ExprListDelete(sqlite3*, ExprList*);
  3669   3659   u32 sqlite3ExprListFlags(const ExprList*);
  3670   3660   int sqlite3Init(sqlite3*, char**);
  3671   3661   int sqlite3InitCallback(void*, int, char**, char**);
  3672   3662   void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
  3673   3663   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3674   3664   Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
................................................................................
  3690   3680   #else
  3691   3681   # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
  3692   3682   #endif
  3693   3683   void sqlite3AddColumn(Parse*,Token*,Token*);
  3694   3684   void sqlite3AddNotNull(Parse*, int);
  3695   3685   void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  3696   3686   void sqlite3AddCheckConstraint(Parse*, Expr*);
  3697         -void sqlite3AddDefaultValue(Parse*,ExprSpan*);
         3687  +void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
  3698   3688   void sqlite3AddCollateType(Parse*, Token*);
  3699   3689   void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
  3700   3690   int sqlite3ParseUri(const char*,const char*,unsigned int*,
  3701   3691                       sqlite3_vfs**,char**,char **);
  3702   3692   Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
  3703   3693   
  3704   3694   #ifdef SQLITE_UNTESTABLE
................................................................................
  3911   3901     Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
  3912   3902     Trigger *sqlite3TriggerList(Parse *, Table *);
  3913   3903     void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
  3914   3904                               int, int, int);
  3915   3905     void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
  3916   3906     void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
  3917   3907     void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
  3918         -  TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
         3908  +  TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
         3909  +                                        const char*,const char*);
  3919   3910     TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
  3920         -                                        Select*,u8);
  3921         -  TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
  3922         -  TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
         3911  +                                        Select*,u8,const char*,const char*);
         3912  +  TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8,
         3913  +                                        const char*,const char*);
         3914  +  TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*,
         3915  +                                        const char*,const char*);
  3923   3916     void sqlite3DeleteTrigger(sqlite3*, Trigger*);
  3924   3917     void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
  3925   3918     u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
  3926   3919   # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
  3927   3920   # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
  3928   3921   #else
  3929   3922   # define sqlite3TriggersExist(B,C,D,E,F) 0
................................................................................
  4345   4338     #define sqlite3ConnectionUnlocked(x)
  4346   4339     #define sqlite3ConnectionClosed(x)
  4347   4340   #endif
  4348   4341   
  4349   4342   #ifdef SQLITE_DEBUG
  4350   4343     void sqlite3ParserTrace(FILE*, char *);
  4351   4344   #endif
         4345  +#if defined(YYCOVERAGE)
         4346  +  int sqlite3ParserCoverage(FILE*);
         4347  +#endif
  4352   4348   
  4353   4349   /*
  4354   4350   ** If the SQLITE_ENABLE IOTRACE exists then the global variable
  4355   4351   ** sqlite3IoTrace is a pointer to a printf-like routine used to
  4356   4352   ** print I/O tracing messages.
  4357   4353   */
  4358   4354   #ifdef SQLITE_ENABLE_IOTRACE

Changes to src/treeview.c.

   503    503     if( pList==0 ){
   504    504       sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
   505    505     }else{
   506    506       int i;
   507    507       sqlite3TreeViewLine(pView, "%s", zLabel);
   508    508       for(i=0; i<pList->nExpr; i++){
   509    509         int j = pList->a[i].u.x.iOrderByCol;
   510         -      if( j ){
          510  +      char *zName = pList->a[i].zName;
          511  +      if( j || zName ){
   511    512           sqlite3TreeViewPush(pView, 0);
          513  +      }
          514  +      if( zName ){
          515  +        sqlite3TreeViewLine(pView, "AS %s", zName);
          516  +      }
          517  +      if( j ){
   512    518           sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
   513    519         }
   514    520         sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
   515         -      if( j ) sqlite3TreeViewPop(pView);
          521  +      if( j || zName ){
          522  +        sqlite3TreeViewPop(pView);
          523  +      }
   516    524       }
   517    525     }
   518    526   }
   519    527   void sqlite3TreeViewExprList(
   520    528     TreeView *pView,
   521    529     const ExprList *pList,
   522    530     u8 moreToFollow,

Changes to src/trigger.c.

    21     21       TriggerStep * pTmp = pTriggerStep;
    22     22       pTriggerStep = pTriggerStep->pNext;
    23     23   
    24     24       sqlite3ExprDelete(db, pTmp->pWhere);
    25     25       sqlite3ExprListDelete(db, pTmp->pExprList);
    26     26       sqlite3SelectDelete(db, pTmp->pSelect);
    27     27       sqlite3IdListDelete(db, pTmp->pIdList);
           28  +    sqlite3DbFree(db, pTmp->zSpan);
    28     29   
    29     30       sqlite3DbFree(db, pTmp);
    30     31     }
    31     32   }
    32     33   
    33     34   /*
    34     35   ** Given table pTab, return a list of all the triggers attached to 
................................................................................
   335    336   
   336    337   triggerfinish_cleanup:
   337    338     sqlite3DeleteTrigger(db, pTrig);
   338    339     assert( !pParse->pNewTrigger );
   339    340     sqlite3DeleteTriggerStep(db, pStepList);
   340    341   }
   341    342   
          343  +/*
          344  +** Duplicate a range of text from an SQL statement, then convert all
          345  +** whitespace characters into ordinary space characters.
          346  +*/
          347  +static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
          348  +  char *z = sqlite3DbSpanDup(db, zStart, zEnd);
          349  +  int i;
          350  +  if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
          351  +  return z;
          352  +}    
          353  +
   342    354   /*
   343    355   ** Turn a SELECT statement (that the pSelect parameter points to) into
   344    356   ** a trigger step.  Return a pointer to a TriggerStep structure.
   345    357   **
   346    358   ** The parser calls this routine when it finds a SELECT statement in
   347    359   ** body of a TRIGGER.  
   348    360   */
   349         -TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
          361  +TriggerStep *sqlite3TriggerSelectStep(
          362  +  sqlite3 *db,                /* Database connection */
          363  +  Select *pSelect,            /* The SELECT statement */
          364  +  const char *zStart,         /* Start of SQL text */
          365  +  const char *zEnd            /* End of SQL text */
          366  +){
   350    367     TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   351    368     if( pTriggerStep==0 ) {
   352    369       sqlite3SelectDelete(db, pSelect);
   353    370       return 0;
   354    371     }
   355    372     pTriggerStep->op = TK_SELECT;
   356    373     pTriggerStep->pSelect = pSelect;
   357    374     pTriggerStep->orconf = OE_Default;
          375  +  pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
   358    376     return pTriggerStep;
   359    377   }
   360    378   
   361    379   /*
   362    380   ** Allocate space to hold a new trigger step.  The allocated space
   363    381   ** holds both the TriggerStep object and the TriggerStep.target.z string.
   364    382   **
   365    383   ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
   366    384   */
   367    385   static TriggerStep *triggerStepAllocate(
   368    386     sqlite3 *db,                /* Database connection */
   369    387     u8 op,                      /* Trigger opcode */
   370         -  Token *pName                /* The target name */
          388  +  Token *pName,               /* The target name */
          389  +  const char *zStart,         /* Start of SQL text */
          390  +  const char *zEnd            /* End of SQL text */
   371    391   ){
   372    392     TriggerStep *pTriggerStep;
   373    393   
   374    394     pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
   375    395     if( pTriggerStep ){
   376    396       char *z = (char*)&pTriggerStep[1];
   377    397       memcpy(z, pName->z, pName->n);
   378    398       sqlite3Dequote(z);
   379    399       pTriggerStep->zTarget = z;
   380    400       pTriggerStep->op = op;
          401  +    pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
   381    402     }
   382    403     return pTriggerStep;
   383    404   }
   384    405   
   385    406   /*
   386    407   ** Build a trigger step out of an INSERT statement.  Return a pointer
   387    408   ** to the new trigger step.
................................................................................
   390    411   ** body of a trigger.
   391    412   */
   392    413   TriggerStep *sqlite3TriggerInsertStep(
   393    414     sqlite3 *db,        /* The database connection */
   394    415     Token *pTableName,  /* Name of the table into which we insert */
   395    416     IdList *pColumn,    /* List of columns in pTableName to insert into */
   396    417     Select *pSelect,    /* A SELECT statement that supplies values */
   397         -  u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
          418  +  u8 orconf,          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
          419  +  const char *zStart, /* Start of SQL text */
          420  +  const char *zEnd    /* End of SQL text */
   398    421   ){
   399    422     TriggerStep *pTriggerStep;
   400    423   
   401    424     assert(pSelect != 0 || db->mallocFailed);
   402    425   
   403         -  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
          426  +  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName, zStart, zEnd);
   404    427     if( pTriggerStep ){
   405    428       pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
   406    429       pTriggerStep->pIdList = pColumn;
   407    430       pTriggerStep->orconf = orconf;
   408    431     }else{
   409    432       sqlite3IdListDelete(db, pColumn);
   410    433     }
................................................................................
   419    442   ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
   420    443   */
   421    444   TriggerStep *sqlite3TriggerUpdateStep(
   422    445     sqlite3 *db,         /* The database connection */
   423    446     Token *pTableName,   /* Name of the table to be updated */
   424    447     ExprList *pEList,    /* The SET clause: list of column and new values */
   425    448     Expr *pWhere,        /* The WHERE clause */
   426         -  u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
          449  +  u8 orconf,           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
          450  +  const char *zStart,  /* Start of SQL text */
          451  +  const char *zEnd     /* End of SQL text */
   427    452   ){
   428    453     TriggerStep *pTriggerStep;
   429    454   
   430         -  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
          455  +  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName, zStart, zEnd);
   431    456     if( pTriggerStep ){
   432    457       pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
   433    458       pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
   434    459       pTriggerStep->orconf = orconf;
   435    460     }
   436    461     sqlite3ExprListDelete(db, pEList);
   437    462     sqlite3ExprDelete(db, pWhere);
................................................................................
   442    467   ** Construct a trigger step that implements a DELETE statement and return
   443    468   ** a pointer to that trigger step.  The parser calls this routine when it
   444    469   ** sees a DELETE statement inside the body of a CREATE TRIGGER.
   445    470   */
   446    471   TriggerStep *sqlite3TriggerDeleteStep(
   447    472     sqlite3 *db,            /* Database connection */
   448    473     Token *pTableName,      /* The table from which rows are deleted */
   449         -  Expr *pWhere            /* The WHERE clause */
          474  +  Expr *pWhere,           /* The WHERE clause */
          475  +  const char *zStart,     /* Start of SQL text */
          476  +  const char *zEnd        /* End of SQL text */
   450    477   ){
   451    478     TriggerStep *pTriggerStep;
   452    479   
   453         -  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
          480  +  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName, zStart, zEnd);
   454    481     if( pTriggerStep ){
   455    482       pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
   456    483       pTriggerStep->orconf = OE_Default;
   457    484     }
   458    485     sqlite3ExprDelete(db, pWhere);
   459    486     return pTriggerStep;
   460    487   }
................................................................................
   700    727       **   END;
   701    728       **
   702    729       **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
   703    730       **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
   704    731       */
   705    732       pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
   706    733       assert( pParse->okConstFactor==0 );
          734  +
          735  +#ifndef SQLITE_OMIT_TRACE
          736  +    if( pStep->zSpan ){
          737  +      sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
          738  +                        sqlite3MPrintf(db, "-- %s", pStep->zSpan),
          739  +                        P4_DYNAMIC);
          740  +    }
          741  +#endif
   707    742   
   708    743       switch( pStep->op ){
   709    744         case TK_UPDATE: {
   710    745           sqlite3Update(pParse, 
   711    746             targetSrcList(pParse, pStep),
   712    747             sqlite3ExprListDup(db, pStep->pExprList, 0), 
   713    748             sqlite3ExprDup(db, pStep->pWhere, 0), 
................................................................................
   841    876         (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
   842    877           (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
   843    878           (pTrigger->op==TK_INSERT ? "INSERT" : ""),
   844    879           (pTrigger->op==TK_DELETE ? "DELETE" : ""),
   845    880         pTab->zName
   846    881       ));
   847    882   #ifndef SQLITE_OMIT_TRACE
   848         -    sqlite3VdbeChangeP4(v, -1, 
   849         -      sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
   850         -    );
          883  +    if( pTrigger->zName ){
          884  +      sqlite3VdbeChangeP4(v, -1, 
          885  +        sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
          886  +      );
          887  +    }
   851    888   #endif
   852    889   
   853    890       /* If one was specified, code the WHEN clause. If it evaluates to false
   854    891       ** (or NULL) the sub-vdbe is immediately halted by jumping to the 
   855    892       ** OP_Halt inserted at the end of the program.  */
   856    893       if( pTrigger->pWhen ){
   857    894         pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);

Changes to src/util.c.

   315    315       return 1;
   316    316     }
   317    317     a = (unsigned char *)zLeft;
   318    318     b = (unsigned char *)zRight;
   319    319     while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
   320    320     return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
   321    321   }
          322  +
          323  +/*
          324  +** Compute 10 to the E-th power.  Examples:  E==1 results in 10.
          325  +** E==2 results in 100.  E==50 results in 1.0e50.
          326  +**
          327  +** This routine only works for values of E between 1 and 341.
          328  +*/
          329  +static LONGDOUBLE_TYPE sqlite3Pow10(int E){
          330  +  LONGDOUBLE_TYPE x = 10.0;
          331  +  LONGDOUBLE_TYPE r = 1.0;
          332  +  while(1){
          333  +    if( E & 1 ) r *= x;
          334  +    E >>= 1;
          335  +    if( E==0 ) break;
          336  +    x *= x;
          337  +  }
          338  +  return r; 
          339  +}
   322    340   
   323    341   /*
   324    342   ** The string z[] is an text representation of a real number.
   325    343   ** Convert this string to a double and write it into *pResult.
   326    344   **
   327    345   ** The string z[] is length bytes in length (bytes, not characters) and
   328    346   ** uses the encoding enc.  The string is not necessarily zero-terminated.
................................................................................
   471    489   
   472    490       /* adjust the sign of significand */
   473    491       s = sign<0 ? -s : s;
   474    492   
   475    493       if( e==0 ){                                         /*OPTIMIZATION-IF-TRUE*/
   476    494         result = (double)s;
   477    495       }else{
   478         -      LONGDOUBLE_TYPE scale = 1.0;
   479    496         /* attempt to handle extremely small/large numbers better */
   480    497         if( e>307 ){                                      /*OPTIMIZATION-IF-TRUE*/
   481    498           if( e<342 ){                                    /*OPTIMIZATION-IF-TRUE*/
   482         -          while( e%308 ) { scale *= 1.0e+1; e -= 1; }
          499  +          LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
   483    500             if( esign<0 ){
   484    501               result = s / scale;
   485    502               result /= 1.0e+308;
   486    503             }else{
   487    504               result = s * scale;
   488    505               result *= 1.0e+308;
   489    506             }
................................................................................
   495    512               result = INFINITY*s;
   496    513   #else
   497    514               result = 1e308*1e308*s;  /* Infinity */
   498    515   #endif
   499    516             }
   500    517           }
   501    518         }else{
   502         -        /* 1.0e+22 is the largest power of 10 than can be 
   503         -        ** represented exactly. */
   504         -        while( e%22 ) { scale *= 1.0e+1; e -= 1; }
   505         -        while( e>0 ) { scale *= 1.0e+22; e -= 22; }
          519  +        LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
   506    520           if( esign<0 ){
   507    521             result = s / scale;
   508    522           }else{
   509    523             result = s * scale;
   510    524           }
   511    525         }
   512    526       }

Changes to src/vdbe.c.

  7032   7032     }
  7033   7033   
  7034   7034     REGISTER_TRACE(pOp->p3, pOut);
  7035   7035     UPDATE_MAX_BLOBSIZE(pOut);
  7036   7036     break;
  7037   7037   }
  7038   7038   
  7039         -
         7039  +/* Opcode: Trace P1 P2 * P4 *
         7040  +**
         7041  +** Write P4 on the statement trace output if statement tracing is
         7042  +** enabled.
         7043  +**
         7044  +** Operand P1 must be 0x7fffffff and P2 must positive.
         7045  +*/
  7040   7046   /* Opcode: Init P1 P2 P3 P4 *
  7041   7047   ** Synopsis: Start at P2
  7042   7048   **
  7043   7049   ** Programs contain a single instance of this opcode as the very first
  7044   7050   ** opcode.
  7045   7051   **
  7046   7052   ** If tracing is enabled (by the sqlite3_trace()) interface, then
................................................................................
  7051   7057   **
  7052   7058   ** Increment the value of P1 so that OP_Once opcodes will jump the
  7053   7059   ** first time they are evaluated for this run.
  7054   7060   **
  7055   7061   ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
  7056   7062   ** error is encountered.
  7057   7063   */
         7064  +case OP_Trace:
  7058   7065   case OP_Init: {          /* jump */
  7059   7066     char *zTrace;
  7060   7067     int i;
  7061   7068   
  7062   7069     /* If the P4 argument is not NULL, then it must be an SQL comment string.
  7063   7070     ** The "--" string is broken up to prevent false-positives with srcck1.c.
  7064   7071     **
................................................................................
  7065   7072     ** This assert() provides evidence for:
  7066   7073     ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
  7067   7074     ** would have been returned by the legacy sqlite3_trace() interface by
  7068   7075     ** using the X argument when X begins with "--" and invoking
  7069   7076     ** sqlite3_expanded_sql(P) otherwise.
  7070   7077     */
  7071   7078     assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
  7072         -  assert( pOp==p->aOp );  /* Always instruction 0 */
         7079  +
         7080  +  /* OP_Init is always instruction 0 */
         7081  +  assert( pOp==p->aOp || pOp->opcode==OP_Trace );
  7073   7082   
  7074   7083   #ifndef SQLITE_OMIT_TRACE
  7075   7084     if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
  7076   7085      && !p->doingRerun
  7077   7086      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
  7078   7087     ){
  7079   7088   #ifndef SQLITE_OMIT_DEPRECATED
................................................................................
  7108   7117     ){
  7109   7118       sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
  7110   7119     }
  7111   7120   #endif /* SQLITE_DEBUG */
  7112   7121   #endif /* SQLITE_OMIT_TRACE */
  7113   7122     assert( pOp->p2>0 );
  7114   7123     if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
         7124  +    if( pOp->opcode==OP_Trace ) break;
  7115   7125       for(i=1; i<p->nOp; i++){
  7116   7126         if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
  7117   7127       }
  7118   7128       pOp->p1 = 0;
  7119   7129     }
  7120   7130     pOp->p1++;
  7121   7131     p->aCounter[SQLITE_STMTSTATUS_RUN]++;

Changes to src/vdbeaux.c.

  1635   1635     int nSub = 0;                        /* Number of sub-vdbes seen so far */
  1636   1636     SubProgram **apSub = 0;              /* Array of sub-vdbes */
  1637   1637     Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
  1638   1638     sqlite3 *db = p->db;                 /* The database connection */
  1639   1639     int i;                               /* Loop counter */
  1640   1640     int rc = SQLITE_OK;                  /* Return code */
  1641   1641     Mem *pMem = &p->aMem[1];             /* First Mem of result set */
  1642         -  int bFull = (p->explain==1 || db->bFullEQP);
         1642  +  int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
  1643   1643     Op *pOp = 0;
  1644   1644   
  1645   1645     assert( p->explain );
  1646   1646     assert( p->magic==VDBE_MAGIC_RUN );
  1647   1647     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
  1648   1648   
  1649   1649     /* Even though this opcode does not use dynamic strings for
................................................................................
  1664   1664     ** listing has finished and sqlite3_step() should return SQLITE_DONE.
  1665   1665     ** nRow is the sum of the number of rows in the main program, plus
  1666   1666     ** the sum of the number of rows in all trigger subprograms encountered
  1667   1667     ** so far.  The nRow value will increase as new trigger subprograms are
  1668   1668     ** encountered, but p->pc will eventually catch up to nRow.
  1669   1669     */
  1670   1670     nRow = p->nOp;
  1671         -  if( bFull ){
         1671  +  if( bListSubprogs ){
  1672   1672       /* The first 8 memory cells are used for the result set.  So we will
  1673   1673       ** commandeer the 9th cell to use as storage for an array of pointers
  1674   1674       ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
  1675   1675       ** cells.  */
  1676   1676       assert( p->nMem>9 );
  1677   1677       pSub = &p->aMem[9];
  1678   1678       if( pSub->flags&MEM_Blob ){
................................................................................
  1709   1709       }
  1710   1710   
  1711   1711       /* When an OP_Program opcode is encounter (the only opcode that has
  1712   1712       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
  1713   1713       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
  1714   1714       ** has not already been seen.
  1715   1715       */
  1716         -    if( bFull && pOp->p4type==P4_SUBPROGRAM ){
         1716  +    if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM ){
  1717   1717         int nByte = (nSub+1)*sizeof(SubProgram*);
  1718   1718         int j;
  1719   1719         for(j=0; j<nSub; j++){
  1720   1720           if( apSub[j]==pOp->p4.pProgram ) break;
  1721   1721         }
  1722   1722         if( j==nSub ){
  1723   1723           p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);

Changes to src/where.c.

    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     19   #include "sqliteInt.h"
    20     20   #include "whereInt.h"
           21  +
           22  +/*
           23  +** Extra information appended to the end of sqlite3_index_info but not
           24  +** visible to the xBestIndex function, at least not directly.  The
           25  +** sqlite3_vtab_collation() interface knows how to reach it, however.
           26  +**
           27  +** This object is not an API and can be changed from one release to the
           28  +** next.  As long as allocateIndexInfo() and sqlite3_vtab_collation()
           29  +** agree on the structure, all will be well.
           30  +*/
           31  +typedef struct HiddenIndexInfo HiddenIndexInfo;
           32  +struct HiddenIndexInfo {
           33  +  WhereClause *pWC;   /* The Where clause being analyzed */
           34  +  Parse *pParse;      /* The parsing context */
           35  +};
    21     36   
    22     37   /* Forward declaration of methods */
    23     38   static int whereLoopResize(sqlite3*, WhereLoop*, int);
    24     39   
    25     40   /* Test variable that can be set to enable WHERE tracing */
    26     41   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
    27     42   /***/ int sqlite3WhereTrace = 0;
................................................................................
   837    852   #ifndef SQLITE_OMIT_VIRTUALTABLE
   838    853   /*
   839    854   ** Allocate and populate an sqlite3_index_info structure. It is the 
   840    855   ** responsibility of the caller to eventually release the structure
   841    856   ** by passing the pointer returned by this function to sqlite3_free().
   842    857   */
   843    858   static sqlite3_index_info *allocateIndexInfo(
   844         -  Parse *pParse,
   845         -  WhereClause *pWC,
          859  +  Parse *pParse,                  /* The parsing context */
          860  +  WhereClause *pWC,               /* The WHERE clause being analyzed */
   846    861     Bitmask mUnusable,              /* Ignore terms with these prereqs */
   847         -  struct SrcList_item *pSrc,
   848         -  ExprList *pOrderBy,
          862  +  struct SrcList_item *pSrc,      /* The FROM clause term that is the vtab */
          863  +  ExprList *pOrderBy,             /* The ORDER BY clause */
   849    864     u16 *pmNoOmit                   /* Mask of terms not to omit */
   850    865   ){
   851    866     int i, j;
   852    867     int nTerm;
   853    868     struct sqlite3_index_constraint *pIdxCons;
   854    869     struct sqlite3_index_orderby *pIdxOrderBy;
   855    870     struct sqlite3_index_constraint_usage *pUsage;
          871  +  struct HiddenIndexInfo *pHidden;
   856    872     WhereTerm *pTerm;
   857    873     int nOrderBy;
   858    874     sqlite3_index_info *pIdxInfo;
   859    875     u16 mNoOmit = 0;
   860    876   
   861    877     /* Count the number of possible WHERE clause constraints referring
   862    878     ** to this virtual table */
................................................................................
   890    906       }
   891    907     }
   892    908   
   893    909     /* Allocate the sqlite3_index_info structure
   894    910     */
   895    911     pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
   896    912                              + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
   897         -                           + sizeof(*pIdxOrderBy)*nOrderBy );
          913  +                           + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
   898    914     if( pIdxInfo==0 ){
   899    915       sqlite3ErrorMsg(pParse, "out of memory");
   900    916       return 0;
   901    917     }
   902    918   
   903    919     /* Initialize the structure.  The sqlite3_index_info structure contains
   904    920     ** many fields that are declared "const" to prevent xBestIndex from
   905    921     ** changing them.  We have to do some funky casting in order to
   906    922     ** initialize those fields.
   907    923     */
   908         -  pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
          924  +  pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
          925  +  pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
   909    926     pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
   910    927     pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
   911    928     *(int*)&pIdxInfo->nConstraint = nTerm;
   912    929     *(int*)&pIdxInfo->nOrderBy = nOrderBy;
   913    930     *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
   914    931     *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
   915    932     *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
   916    933                                                                      pUsage;
   917    934   
          935  +  pHidden->pWC = pWC;
          936  +  pHidden->pParse = pParse;
   918    937     for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
   919    938       u16 op;
   920    939       if( pTerm->leftCursor != pSrc->iCursor ) continue;
   921    940       if( pTerm->prereqRight & mUnusable ) continue;
   922    941       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
   923    942       testcase( pTerm->eOperator & WO_IN );
   924    943       testcase( pTerm->eOperator & WO_IS );
................................................................................
  3134   3153     WHERETRACE(0xffff, ("  bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
  3135   3154                         *pbIn, (sqlite3_uint64)mPrereq,
  3136   3155                         (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
  3137   3156   
  3138   3157     return rc;
  3139   3158   }
  3140   3159   
  3141         -
  3142         -/*
  3143         -** Context object used to pass information from whereLoopAddVirtual()
  3144         -** to sqlite3_vtab_collation().
  3145         -*/
  3146         -struct BestIndexCtx {
  3147         -  WhereClause *pWC;
  3148         -  sqlite3_index_info *pIdxInfo;
  3149         -  Parse *pParse;
  3150         -};
  3151         -
  3152   3160   /*
  3153   3161   ** If this function is invoked from within an xBestIndex() callback, it
  3154   3162   ** returns a pointer to a buffer containing the name of the collation
  3155   3163   ** sequence associated with element iCons of the sqlite3_index_info.aConstraint
  3156   3164   ** array. Or, if iCons is out of range or there is no active xBestIndex
  3157   3165   ** call, return NULL.
  3158   3166   */
  3159         -const char *sqlite3_vtab_collation(sqlite3 *db, int iCons){
  3160         -  struct BestIndexCtx *p = (struct BestIndexCtx*)db->pBestIndexCtx;
         3167  +const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
         3168  +  HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
  3161   3169     const char *zRet = 0;
  3162         -  if( p && iCons>=0 && iCons<p->pIdxInfo->nConstraint ){
  3163         -    int iTerm = p->pIdxInfo->aConstraint[iCons].iTermOffset;
  3164         -    Expr *pX = p->pWC->a[iTerm].pExpr;
  3165         -    CollSeq *pC = sqlite3BinaryCompareCollSeq(p->pParse,pX->pLeft,pX->pRight);
         3170  +  if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
         3171  +    CollSeq *pC = 0;
         3172  +    int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
         3173  +    Expr *pX = pHidden->pWC->a[iTerm].pExpr;
         3174  +    if( pX->pLeft ){
         3175  +      pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight);
         3176  +    }
  3166   3177       zRet = (pC ? pC->zName : "BINARY");
  3167   3178     }
  3168   3179     return zRet;
  3169   3180   }
  3170   3181   
  3171   3182   /*
  3172   3183   ** Add all WhereLoop objects for a table of the join identified by
................................................................................
  3205   3216     struct SrcList_item *pSrc;   /* The FROM clause term to search */
  3206   3217     sqlite3_index_info *p;       /* Object to pass to xBestIndex() */
  3207   3218     int nConstraint;             /* Number of constraints in p */
  3208   3219     int bIn;                     /* True if plan uses IN(...) operator */
  3209   3220     WhereLoop *pNew;
  3210   3221     Bitmask mBest;               /* Tables used by best possible plan */
  3211   3222     u16 mNoOmit;
  3212         -  struct BestIndexCtx bic;
  3213         -  void *pSaved;
  3214   3223   
  3215   3224     assert( (mPrereq & mUnusable)==0 );
  3216   3225     pWInfo = pBuilder->pWInfo;
  3217   3226     pParse = pWInfo->pParse;
  3218   3227     pWC = pBuilder->pWC;
  3219   3228     pNew = pBuilder->pNew;
  3220   3229     pSrc = &pWInfo->pTabList->a[pNew->iTab];
................................................................................
  3228   3237     pNew->u.vtab.needFree = 0;
  3229   3238     nConstraint = p->nConstraint;
  3230   3239     if( whereLoopResize(pParse->db, pNew, nConstraint) ){
  3231   3240       sqlite3DbFree(pParse->db, p);
  3232   3241       return SQLITE_NOMEM_BKPT;
  3233   3242     }
  3234   3243   
  3235         -  bic.pWC = pWC;
  3236         -  bic.pIdxInfo = p;
  3237         -  bic.pParse = pParse;
  3238         -  pSaved = pParse->db->pBestIndexCtx;
  3239         -  pParse->db->pBestIndexCtx = (void*)&bic;
  3240         -
  3241   3244     /* First call xBestIndex() with all constraints usable. */
  3242   3245     WHERETRACE(0x40, ("  VirtualOne: all usable\n"));
  3243   3246     rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
  3244   3247   
  3245   3248     /* If the call to xBestIndex() with all terms enabled produced a plan
  3246   3249     ** that does not require any source tables (IOW: a plan with mBest==0),
  3247   3250     ** then there is no point in making any further calls to xBestIndex() 
................................................................................
  3310   3313         rc = whereLoopAddVirtualOne(
  3311   3314             pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
  3312   3315       }
  3313   3316     }
  3314   3317   
  3315   3318     if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
  3316   3319     sqlite3DbFreeNN(pParse->db, p);
  3317         -  pParse->db->pBestIndexCtx = pSaved;
  3318   3320     return rc;
  3319   3321   }
  3320   3322   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  3321   3323   
  3322   3324   /*
  3323   3325   ** Add WhereLoop entries to handle OR terms.  This works for either
  3324   3326   ** btrees or virtual tables.

Changes to src/wherecode.c.

   124    124     WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
   125    125     int iLevel,                     /* Value for "level" column of output */
   126    126     int iFrom,                      /* Value for "from" column of output */
   127    127     u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
   128    128   ){
   129    129     int ret = 0;
   130    130   #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
   131         -  if( pParse->explain==2 )
          131  +  if( sqlite3ParseToplevel(pParse)->explain==2 )
   132    132   #endif
   133    133     {
   134    134       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
   135    135       Vdbe *v = pParse->pVdbe;      /* VM being constructed */
   136    136       sqlite3 *db = pParse->db;     /* Database handle */
   137    137       int iId = pParse->iSelectId;  /* Select id (left-most output column) */
   138    138       int isSearch;                 /* True for a SEARCH. False for SCAN. */

Changes to test/capi2.test.

   180    180     set rc [catch {
   181    181         sqlite3_prepare $DB {select bogus from sqlite_master;;;x;} -1 TAIL
   182    182     } msg]
   183    183     lappend rc $msg $TAIL
   184    184   } {1 {(1) no such column: bogus} {;;x;}}
   185    185   do_test capi2-3.6 {
   186    186     set rc [catch {
   187         -      sqlite3_prepare $DB {select 5/0} -1 TAIL
          187  +      sqlite3_prepare $DB {select 5/0;} -1 TAIL
   188    188     } VM]
   189    189     lappend rc $TAIL
   190    190   } {0 {}}
   191    191   do_test capi2-3.7 {
   192    192     list [sqlite3_step $VM] \
   193    193          [sqlite3_column_count $VM] \
   194    194          [get_row_values $VM] \

Changes to test/colname.test.

   373    373   } {a 1 n 3}
   374    374   do_test colname-9.211 {
   375    375     execsql2 {SELECT t1.a AS n, v3.a FROM t1 JOIN v3}
   376    376   } {n 1 a 3}
   377    377   do_test colname-9.210 {
   378    378     execsql2 {SELECT t1.a, v3.a AS n FROM t1 JOIN v3}
   379    379   } {a 1 n 3}
          380  +
          381  +# 2017-12-23:  Ticket https://www.sqlite.org/src/info/3b4450072511e621
          382  +# Inconsistent column names in CREATE TABLE AS
          383  +#
          384  +# Verify that the names of columns in the created table of a CREATE TABLE AS
          385  +# are the same as the names of result columns in the SELECT statement.
          386  +#
          387  +do_execsql_test colname-9.300 {
          388  +  DROP TABLE IF EXISTS t1;
          389  +  DROP TABLE IF EXISTS t2;
          390  +  CREATE TABLE t1(aaa INT);
          391  +  INSERT INTO t1(aaa) VALUES(123);
          392  +}
          393  +do_test colname-9.310 {
          394  +  execsql2 {SELECT BBb FROM (SELECT aaa AS Bbb FROM t1)}
          395  +} {Bbb 123}
          396  +do_execsql_test colname-9.320 {
          397  +  CREATE TABLE t2 AS SELECT BBb FROM (SELECT aaa AS Bbb FROM t1);
          398  +  SELECT name FROM pragma_table_info('t2');
          399  +} {Bbb}
          400  +
          401  +# Issue detected by OSSFuzz on 2017-12-24 (Christmas Eve)
          402  +# caused by check-in https://sqlite.org/src/info/6b2ff26c25
          403  +#
          404  +# Prior to being fixed, the following CREATE TABLE was dereferencing
          405  +# a NULL pointer and segfaulting.
          406  +#
          407  +do_catchsql_test colname-9.400 {
          408  +  CREATE TABLE t4 AS SELECT #0;
          409  +} {1 {near "#0": syntax error}}
          410  +
          411  +# Issue detected by OSSFuzz on 2017-12-25 (Christmas Day)
          412  +# also caused by check-in https://sqlite.org/src/info/6b2ff26c25
          413  +#
          414  +# Prior to being fixed, the following CREATE TABLE caused an
          415  +# assertion fault.
          416  +#
          417  +do_catchsql_test colname-9.410 {
          418  +  CREATE TABLE t5 AS SELECT RAISE(abort,a);
          419  +} {1 {RAISE() may only be used within a trigger-program}}
   380    420   
   381    421   # Make sure the quotation marks get removed from the column names
   382    422   # when constructing a new table from an aggregate SELECT.
   383    423   # Email from Juergen Palm on 2017-07-11.
   384    424   #
   385    425   do_execsql_test colname-10.100 {
   386    426     DROP TABLE IF EXISTS t1;

Changes to test/fkey1.test.

   167    167   # would have been the parent of the new row being inserted. Causing an
   168    168   # FK violation.
   169    169   #
   170    170   do_catchsql_test fkey1-5.2 {
   171    171     INSERT OR REPLACE INTO t11 VALUES (2, 3);
   172    172   } {1 {FOREIGN KEY constraint failed}}
   173    173   
          174  +# Make sure sqlite3_trace() output works with triggers used to implement
          175  +# FK constraints
          176  +#
          177  +proc sqltrace {txt} {
          178  +  global traceoutput
          179  +  lappend traceoutput $txt
          180  +}
          181  +do_test fkey1-5.2.1 {
          182  +  unset -nocomplain traceoutput
          183  +  db trace sqltrace
          184  +  catch {db eval {INSERT OR REPLACE INTO t11 VALUES(2,3);}}
          185  +  set traceoutput
          186  +} {{INSERT OR REPLACE INTO t11 VALUES(2,3);} {INSERT OR REPLACE INTO t11 VALUES(2,3);} {INSERT OR REPLACE INTO t11 VALUES(2,3);}}
          187  +
   174    188   # A similar test to the above.
   175    189   do_execsql_test fkey1-5.3 {
   176    190     CREATE TABLE Foo (
   177    191       Id INTEGER PRIMARY KEY, 
   178    192       ParentId INTEGER REFERENCES Foo(Id) ON DELETE CASCADE, C1
   179    193     );
   180    194     INSERT OR REPLACE INTO Foo(Id, ParentId, C1) VALUES (1, null, 'A');

Changes to test/misc1.test.

   717    717   # was obtained from sqlite3ExprListDup().
   718    718   #
   719    719   do_execsql_test misc1-26.0 {
   720    720     DROP TABLE IF EXISTS abc;
   721    721     CREATE TABLE abc(a, b, c);
   722    722     SELECT randomblob(min(max(coalesce(EXISTS (SELECT 1 FROM ( SELECT (SELECT 2147483647) NOT IN (SELECT 2147483649 UNION ALL SELECT DISTINCT -1) IN (SELECT 2147483649), 'fault', (SELECT ALL -1 INTERSECT SELECT 'experiments') IN (SELECT ALL 56.1 ORDER BY 'experiments' DESC) FROM (SELECT DISTINCT 2147483648, 'hardware' UNION ALL SELECT -2147483648, 'experiments' ORDER BY 2147483648 LIMIT 1 OFFSET 123456789.1234567899) GROUP BY (SELECT ALL 0 INTERSECT SELECT 'in') IN (SELECT DISTINCT 'experiments' ORDER BY zeroblob(1000) LIMIT 56.1 OFFSET -456) HAVING EXISTS (SELECT 'fault' EXCEPT    SELECT DISTINCT 56.1) UNION SELECT 'The', 'The', 2147483649 UNION ALL SELECT DISTINCT 'hardware', 'first', 'experiments' ORDER BY 'hardware' LIMIT 123456789.1234567899 OFFSET -2147483647)) NOT IN (SELECT (SELECT DISTINCT (SELECT 'The') FROM abc ORDER BY EXISTS (SELECT -1 INTERSECT SELECT ALL NULL) ASC) IN (SELECT DISTINCT EXISTS (SELECT ALL 123456789.1234567899 ORDER BY 1 ASC, NULL DESC) FROM sqlite_master INTERSECT SELECT 456)), (SELECT ALL 'injection' UNION ALL SELECT ALL (SELECT DISTINCT 'first' UNION     SELECT DISTINCT 'The') FROM (SELECT 456, 'in', 2147483649))),1), 500)), 'first', EXISTS (SELECT DISTINCT 456 FROM abc ORDER BY 'experiments' DESC) FROM abc;
   723    723   } {}
          724  +
          725  +# 2017-12-29
          726  +#
          727  +# The following behaviors (duplicate column names on an INSERT or UPDATE)
          728  +# are undocumented.  These tests are added to ensure that historical behavior
          729  +# does not change accidentally.
          730  +#
          731  +# For duplication columns on an INSERT, the first value is used.
          732  +# For duplication columns on an UPDATE, the last value is used.
          733  +#
          734  +do_execsql_test misc1-27.0 {
          735  +  CREATE TABLE dup1(a,b,c);
          736  +  INSERT INTO dup1(a,b,c,a,b,c) VALUES(1,2,3,4,5,6);
          737  +  SELECT a,b,c FROM dup1;
          738  +} {1 2 3}
          739  +do_execsql_test misc1-27.1 {
          740  +  UPDATE dup1 SET a=7, b=8, c=9, a=10, b=11, c=12;
          741  +  SELECT a,b,c FROM dup1;
          742  +} {10 11 12}
          743  +
   724    744   
   725    745   finish_test

Changes to test/speedtest1.c.

    28     28     "  --serialized        Set serialized threading mode\n"
    29     29     "  --singlethread      Set single-threaded mode - disables all mutexing\n"
    30     30     "  --sqlonly           No-op.  Only show the SQL that would have been run.\n"
    31     31     "  --shrink-memory     Invoke sqlite3_db_release_memory() frequently.\n"
    32     32     "  --size N            Relative test size.  Default=100\n"
    33     33     "  --stats             Show statistics at the end\n"
    34     34     "  --temp N            N from 0 to 9.  0: no temp table. 9: all temp tables\n"
    35         -  "  --testset T         Run test-set T (main, cte, rtree, orm, debug)\n"
           35  +  "  --testset T         Run test-set T (main, cte, rtree, orm, fp, debug)\n"
    36     36     "  --trace             Turn on SQL tracing\n"
    37     37     "  --threads N         Use up to N threads for sorting\n"
    38     38     "  --utf16be           Set text encoding to UTF-16BE\n"
    39     39     "  --utf16le           Set text encoding to UTF-16LE\n"
    40     40     "  --verify            Run additional verification steps.\n"
    41     41     "  --without-rowid     Use WITHOUT ROWID where appropriate\n"
    42     42   ;
................................................................................
  1116   1116       "SELECT count(x), avg(x) FROM (\n"
  1117   1117       "  SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1\n"
  1118   1118       ");",
  1119   1119       nElem, nElem
  1120   1120     );
  1121   1121     speedtest1_run();
  1122   1122     speedtest1_end_test();
         1123  +}
  1123   1124   
         1125  +/*
         1126  +** Compute a pseudo-random floating point ascii number.
         1127  +*/
         1128  +void speedtest1_random_ascii_fp(char *zFP){
         1129  +  int x = speedtest1_random();
         1130  +  int y = speedtest1_random();
         1131  +  int z;
         1132  +  z = y%10;
         1133  +  if( z<0 ) z = -z;
         1134  +  y /= 10;
         1135  +  sqlite3_snprintf(100,zFP,"%d.%de%d",y,z,x%200);
         1136  +}
         1137  +
         1138  +/*
         1139  +** A testset for floating-point numbers.
         1140  +*/
         1141  +void testset_fp(void){
         1142  +  int n;
         1143  +  int i;
         1144  +  char zFP1[100];
         1145  +  char zFP2[100];
         1146  +  
         1147  +  n = g.szTest*5000;
         1148  +  speedtest1_begin_test(100, "Fill a table with %d FP values", n*2);
         1149  +  speedtest1_exec("BEGIN");
         1150  +  speedtest1_exec("CREATE%s TABLE t1(a REAL %s, b REAL %s);",
         1151  +                  isTemp(1), g.zNN, g.zNN);
         1152  +  speedtest1_prepare("INSERT INTO t1 VALUES(?1,?2); -- %d times", n);
         1153  +  for(i=1; i<=n; i++){
         1154  +    speedtest1_random_ascii_fp(zFP1);
         1155  +    speedtest1_random_ascii_fp(zFP2);
         1156  +    sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC);
         1157  +    sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC);
         1158  +    speedtest1_run();
         1159  +  }
         1160  +  speedtest1_exec("COMMIT");
         1161  +  speedtest1_end_test();
         1162  +
         1163  +  n = g.szTest/25 + 2;
         1164  +  speedtest1_begin_test(110, "%d range queries", n);
         1165  +  speedtest1_prepare("SELECT sum(b) FROM t1 WHERE a BETWEEN ?1 AND ?2");
         1166  +  for(i=1; i<=n; i++){
         1167  +    speedtest1_random_ascii_fp(zFP1);
         1168  +    speedtest1_random_ascii_fp(zFP2);
         1169  +    sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC);
         1170  +    sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC);
         1171  +    speedtest1_run();
         1172  +  }
         1173  +  speedtest1_end_test();
         1174  +
         1175  +  speedtest1_begin_test(120, "CREATE INDEX three times");
         1176  +  speedtest1_exec("BEGIN;");
         1177  +  speedtest1_exec("CREATE INDEX t1a ON t1(a);");
         1178  +  speedtest1_exec("CREATE INDEX t1b ON t1(b);");
         1179  +  speedtest1_exec("CREATE INDEX t1ab ON t1(a,b);");
         1180  +  speedtest1_exec("COMMIT;");
         1181  +  speedtest1_end_test();
         1182  +
         1183  +  n = g.szTest/3 + 2;
         1184  +  speedtest1_begin_test(130, "%d indexed range queries", n);
         1185  +  speedtest1_prepare("SELECT sum(b) FROM t1 WHERE a BETWEEN ?1 AND ?2");
         1186  +  for(i=1; i<=n; i++){
         1187  +    speedtest1_random_ascii_fp(zFP1);
         1188  +    speedtest1_random_ascii_fp(zFP2);
         1189  +    sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC);
         1190  +    sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC);
         1191  +    speedtest1_run();
         1192  +  }
         1193  +  speedtest1_end_test();
  1124   1194   }
  1125   1195   
  1126   1196   #ifdef SQLITE_ENABLE_RTREE
  1127   1197   /* Generate two numbers between 1 and mx.  The first number is less than
  1128   1198   ** the second.  Usually the numbers are near each other but can sometimes
  1129   1199   ** be far apart.
  1130   1200   */
................................................................................
  1869   1939       testset_main();
  1870   1940     }else if( strcmp(zTSet,"debug1")==0 ){
  1871   1941       testset_debug1();
  1872   1942     }else if( strcmp(zTSet,"orm")==0 ){
  1873   1943       testset_orm();
  1874   1944     }else if( strcmp(zTSet,"cte")==0 ){
  1875   1945       testset_cte();
         1946  +  }else if( strcmp(zTSet,"fp")==0 ){
         1947  +    testset_fp();
  1876   1948     }else if( strcmp(zTSet,"rtree")==0 ){
  1877   1949   #ifdef SQLITE_ENABLE_RTREE
  1878   1950       testset_rtree(6, 147);
  1879   1951   #else
  1880   1952       fatal_error("compile with -DSQLITE_ENABLE_RTREE to enable "
  1881   1953                   "the R-Tree tests\n");
  1882   1954   #endif
  1883   1955     }else{
  1884         -    fatal_error("unknown testset: \"%s\"\nChoices: main debug1 cte rtree\n",
         1956  +    fatal_error("unknown testset: \"%s\"\nChoices: main debug1 cte rtree fp\n",
  1885   1957                    zTSet);
  1886   1958     }
  1887   1959     speedtest1_final();
  1888   1960   
  1889   1961     if( showStats ){
  1890   1962       sqlite3_exec(g.db, "PRAGMA compile_options", xCompileOptions, 0, 0);
  1891   1963     }

Changes to test/tester.tcl.

  2267   2267     eval sqlite3_config_pagecache $::old_pagecache_config
  2268   2268     unset ::old_pagecache_config 
  2269   2269     sqlite3_initialize
  2270   2270     autoinstall_test_functions
  2271   2271     sqlite3 db test.db
  2272   2272   }
  2273   2273   
  2274         -proc test_find_binary {nm} {
         2274  +proc test_binary_name {nm} {
  2275   2275     if {$::tcl_platform(platform)=="windows"} {
  2276   2276       set ret "$nm.exe"
  2277   2277     } else {
  2278   2278       set ret $nm
  2279   2279     }
  2280         -  set ret [file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]]
         2280  +  file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]
         2281  +}
         2282  +
         2283  +proc test_find_binary {nm} {
         2284  +  set ret [test_binary_name $nm]
  2281   2285     if {![file executable $ret]} {
  2282   2286       finish_test
  2283   2287       return ""
  2284   2288     }
  2285   2289     return $ret
  2286   2290   }
  2287   2291   

Changes to test/trace.test.

   193    193       proc trace_proc cmd {
   194    194         lappend ::TRACE_OUT [string trim $cmd]
   195    195       }
   196    196       db eval {
   197    197         UPDATE t1 SET a=a+1;
   198    198       }
   199    199       set TRACE_OUT
   200         -  } {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2}}
          200  +  } {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'}}
   201    201   }
   202    202   
   203    203   # With 3.6.21, we add the ability to expand host parameters in the trace
   204    204   # output.  Test this feature.
   205    205   #
   206    206   do_test trace-6.1 {
   207    207     set ::t6int [expr {3+3}]

Changes to tool/lemon.c.

   380    380     struct rule *rule;       /* List of all rules */
   381    381     struct rule *startRule;  /* First rule */
   382    382     int nstate;              /* Number of states */
   383    383     int nxstate;             /* nstate with tail degenerate states removed */
   384    384     int nrule;               /* Number of rules */
   385    385     int nsymbol;             /* Number of terminal and nonterminal symbols */
   386    386     int nterminal;           /* Number of terminal symbols */
          387  +  int minShiftReduce;      /* Minimum shift-reduce action value */
          388  +  int errAction;           /* Error action value */
          389  +  int accAction;           /* Accept action value */
          390  +  int noAction;            /* No-op action value */
          391  +  int minReduce;           /* Minimum reduce action */
          392  +  int maxAction;           /* Maximum action value of any kind */
   387    393     struct symbol **symbols; /* Sorted array of pointers to symbols */
   388    394     int errorcnt;            /* Number of errors */
   389    395     struct symbol *errsym;   /* The error symbol */
   390    396     struct symbol *wildcard; /* Token that matches anything */
   391    397     char *name;              /* Name of the generated parser */
   392    398     char *arg;               /* Declaration of the 3th argument to parser */
   393    399     char *tokentype;         /* Type of terminal symbols in the parser stack */
................................................................................
   403    409     char *tokendest;         /* Code to execute to destroy token data */
   404    410     char *vardest;           /* Code for the default non-terminal destructor */
   405    411     char *filename;          /* Name of the input file */
   406    412     char *outname;           /* Name of the current output file */
   407    413     char *tokenprefix;       /* A prefix added to token names in the .h file */
   408    414     int nconflict;           /* Number of parsing conflicts */
   409    415     int nactiontab;          /* Number of entries in the yy_action[] table */
          416  +  int nlookaheadtab;       /* Number of entries in yy_lookahead[] */
   410    417     int tablesize;           /* Total table size of all tables in bytes */
   411    418     int basisflag;           /* Print only basis configurations */
   412    419     int has_fallback;        /* True if any %fallback is seen in the grammar */
   413    420     int nolinenosflag;       /* True if #line statements should not be printed */
   414    421     char *argv0;             /* Name of the program */
   415    422   };
   416    423   
................................................................................
   579    586       *aAction,                  /* The yy_action[] table under construction */
   580    587       *aLookahead;               /* A single new transaction set */
   581    588     int mnLookahead;             /* Minimum aLookahead[].lookahead */
   582    589     int mnAction;                /* Action associated with mnLookahead */
   583    590     int mxLookahead;             /* Maximum aLookahead[].lookahead */
   584    591     int nLookahead;              /* Used slots in aLookahead[] */
   585    592     int nLookaheadAlloc;         /* Slots allocated in aLookahead[] */
          593  +  int nterminal;               /* Number of terminal symbols */
          594  +  int nsymbol;                 /* total number of symbols */
   586    595   };
   587    596   
   588    597   /* Return the number of entries in the yy_action table */
   589         -#define acttab_size(X) ((X)->nAction)
          598  +#define acttab_lookahead_size(X) ((X)->nAction)
   590    599   
   591    600   /* The value for the N-th entry in yy_action */
   592    601   #define acttab_yyaction(X,N)  ((X)->aAction[N].action)
   593    602   
   594    603   /* The value for the N-th entry in yy_lookahead */
   595    604   #define acttab_yylookahead(X,N)  ((X)->aAction[N].lookahead)
   596    605   
................................................................................
   598    607   void acttab_free(acttab *p){
   599    608     free( p->aAction );
   600    609     free( p->aLookahead );
   601    610     free( p );
   602    611   }
   603    612   
   604    613   /* Allocate a new acttab structure */
   605         -acttab *acttab_alloc(void){
          614  +acttab *acttab_alloc(int nsymbol, int nterminal){
   606    615     acttab *p = (acttab *) calloc( 1, sizeof(*p) );
   607    616     if( p==0 ){
   608    617       fprintf(stderr,"Unable to allocate memory for a new acttab.");
   609    618       exit(1);
   610    619     }
   611    620     memset(p, 0, sizeof(*p));
          621  +  p->nsymbol = nsymbol;
          622  +  p->nterminal = nterminal;
   612    623     return p;
   613    624   }
   614    625   
   615    626   /* Add a new action to the current transaction set.
   616    627   **
   617    628   ** This routine is called once for each lookahead for a particular
   618    629   ** state.
................................................................................
   645    656   
   646    657   /*
   647    658   ** Add the transaction set built up with prior calls to acttab_action()
   648    659   ** into the current action table.  Then reset the transaction set back
   649    660   ** to an empty set in preparation for a new round of acttab_action() calls.
   650    661   **
   651    662   ** Return the offset into the action table of the new transaction.
          663  +**
          664  +** If the makeItSafe parameter is true, then the offset is chosen so that
          665  +** it is impossible to overread the yy_lookaside[] table regardless of
          666  +** the lookaside token.  This is done for the terminal symbols, as they
          667  +** come from external inputs and can contain syntax errors.  When makeItSafe
          668  +** is false, there is more flexibility in selecting offsets, resulting in
          669  +** a smaller table.  For non-terminal symbols, which are never syntax errors,
          670  +** makeItSafe can be false.
   652    671   */
   653         -int acttab_insert(acttab *p){
   654         -  int i, j, k, n;
          672  +int acttab_insert(acttab *p, int makeItSafe){
          673  +  int i, j, k, n, end;
   655    674     assert( p->nLookahead>0 );
   656    675   
   657    676     /* Make sure we have enough space to hold the expanded action table
   658    677     ** in the worst case.  The worst case occurs if the transaction set
   659    678     ** must be appended to the current action table
   660    679     */
   661         -  n = p->mxLookahead + 1;
          680  +  n = p->nsymbol + 1;
   662    681     if( p->nAction + n >= p->nActionAlloc ){
   663    682       int oldAlloc = p->nActionAlloc;
   664    683       p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
   665    684       p->aAction = (struct lookahead_action *) realloc( p->aAction,
   666    685                             sizeof(p->aAction[0])*p->nActionAlloc);
   667    686       if( p->aAction==0 ){
   668    687         fprintf(stderr,"malloc failed\n");
................................................................................
   676    695   
   677    696     /* Scan the existing action table looking for an offset that is a
   678    697     ** duplicate of the current transaction set.  Fall out of the loop
   679    698     ** if and when the duplicate is found.
   680    699     **
   681    700     ** i is the index in p->aAction[] where p->mnLookahead is inserted.
   682    701     */
   683         -  for(i=p->nAction-1; i>=0; i--){
          702  +  end = makeItSafe ? p->mnLookahead : 0;
          703  +  for(i=p->nAction-1; i>=end; i--){
   684    704       if( p->aAction[i].lookahead==p->mnLookahead ){
   685    705         /* All lookaheads and actions in the aLookahead[] transaction
   686    706         ** must match against the candidate aAction[i] entry. */
   687    707         if( p->aAction[i].action!=p->mnAction ) continue;
   688    708         for(j=0; j<p->nLookahead; j++){
   689    709           k = p->aLookahead[j].lookahead - p->mnLookahead + i;
   690    710           if( k<0 || k>=p->nAction ) break;
................................................................................
   706    726       }
   707    727     }
   708    728   
   709    729     /* If no existing offsets exactly match the current transaction, find an
   710    730     ** an empty offset in the aAction[] table in which we can add the
   711    731     ** aLookahead[] transaction.
   712    732     */
   713         -  if( i<0 ){
          733  +  if( i<end ){
   714    734       /* Look for holes in the aAction[] table that fit the current
   715    735       ** aLookahead[] transaction.  Leave i set to the offset of the hole.
   716    736       ** If no holes are found, i is left at p->nAction, which means the
   717    737       ** transaction will be appended. */
   718         -    for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){
          738  +    i = makeItSafe ? p->mnLookahead : 0;
          739  +    for(; i<p->nActionAlloc - p->mxLookahead; i++){
   719    740         if( p->aAction[i].lookahead<0 ){
   720    741           for(j=0; j<p->nLookahead; j++){
   721    742             k = p->aLookahead[j].lookahead - p->mnLookahead + i;
   722    743             if( k<0 ) break;
   723    744             if( p->aAction[k].lookahead>=0 ) break;
   724    745           }
   725    746           if( j<p->nLookahead ) continue;
................................................................................
   729    750           if( j==p->nAction ){
   730    751             break;  /* Fits in empty slots */
   731    752           }
   732    753         }
   733    754       }
   734    755     }
   735    756     /* Insert transaction set at index i. */
          757  +#if 0
          758  +  printf("Acttab:");
          759  +  for(j=0; j<p->nLookahead; j++){
          760  +    printf(" %d", p->aLookahead[j].lookahead);
          761  +  }
          762  +  printf(" inserted at %d\n", i);
          763  +#endif
   736    764     for(j=0; j<p->nLookahead; j++){
   737    765       k = p->aLookahead[j].lookahead - p->mnLookahead + i;
   738    766       p->aAction[k] = p->aLookahead[j];
   739    767       if( k>=p->nAction ) p->nAction = k+1;
   740    768     }
          769  +  if( makeItSafe && i+p->nterminal>=p->nAction ) p->nAction = i+p->nterminal+1;
   741    770     p->nLookahead = 0;
   742    771   
   743    772     /* Return the offset that is added to the lookahead in order to get the
   744    773     ** index into yy_action of the action */
   745    774     return i - p->mnLookahead;
   746    775   }
          776  +
          777  +/*
          778  +** Return the size of the action table without the trailing syntax error
          779  +** entries.
          780  +*/
          781  +int acttab_action_size(acttab *p){
          782  +  int n = p->nAction;
          783  +  while( n>0 && p->aAction[n-1].lookahead<0 ){ n--; }
          784  +  return n;
          785  +}
   747    786   
   748    787   /********************** From the file "build.c" *****************************/
   749    788   /*
   750    789   ** Routines to construction the finite state machine for the LEMON
   751    790   ** parser generator.
   752    791   */
   753    792   
................................................................................
  1714   1753       stats_line("terminal symbols", lem.nterminal);
  1715   1754       stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal);
  1716   1755       stats_line("total symbols", lem.nsymbol);
  1717   1756       stats_line("rules", lem.nrule);
  1718   1757       stats_line("states", lem.nxstate);
  1719   1758       stats_line("conflicts", lem.nconflict);
  1720   1759       stats_line("action table entries", lem.nactiontab);
         1760  +    stats_line("lookahead table entries", lem.nlookaheadtab);
  1721   1761       stats_line("total table size (bytes)", lem.tablesize);
  1722   1762     }
  1723   1763     if( lem.nconflict > 0 ){
  1724   1764       fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
  1725   1765     }
  1726   1766   
  1727   1767     /* return 0 on success, 1 on failure. */
................................................................................
  3015   3055     if( fp==0 && *mode=='w' ){
  3016   3056       fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
  3017   3057       lemp->errorcnt++;
  3018   3058       return 0;
  3019   3059     }
  3020   3060     return fp;
  3021   3061   }
         3062  +
         3063  +/* Print the text of a rule
         3064  +*/
         3065  +void rule_print(FILE *out, struct rule *rp){
         3066  +  int i, j;
         3067  +  fprintf(out, "%s",rp->lhs->name);
         3068  +  /*    if( rp->lhsalias ) fprintf(out,"(%s)",rp->lhsalias); */
         3069  +  fprintf(out," ::=");
         3070  +  for(i=0; i<rp->nrhs; i++){
         3071  +    struct symbol *sp = rp->rhs[i];
         3072  +    if( sp->type==MULTITERMINAL ){
         3073  +      fprintf(out," %s", sp->subsym[0]->name);
         3074  +      for(j=1; j<sp->nsubsym; j++){
         3075  +        fprintf(out,"|%s", sp->subsym[j]->name);
         3076  +      }
         3077  +    }else{
         3078  +      fprintf(out," %s", sp->name);
         3079  +    }
         3080  +    /* if( rp->rhsalias[i] ) fprintf(out,"(%s)",rp->rhsalias[i]); */
         3081  +  }
         3082  +}
  3022   3083   
  3023   3084   /* Duplicate the input file without comments and without actions
  3024   3085   ** on rules */
  3025   3086   void Reprint(struct lemon *lemp)
  3026   3087   {
  3027   3088     struct rule *rp;
  3028   3089     struct symbol *sp;
................................................................................
  3043   3104         sp = lemp->symbols[j];
  3044   3105         assert( sp->index==j );
  3045   3106         printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
  3046   3107       }
  3047   3108       printf("\n");
  3048   3109     }
  3049   3110     for(rp=lemp->rule; rp; rp=rp->next){
  3050         -    printf("%s",rp->lhs->name);
  3051         -    /*    if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
  3052         -    printf(" ::=");
  3053         -    for(i=0; i<rp->nrhs; i++){
  3054         -      sp = rp->rhs[i];
  3055         -      if( sp->type==MULTITERMINAL ){
  3056         -        printf(" %s", sp->subsym[0]->name);
  3057         -        for(j=1; j<sp->nsubsym; j++){
  3058         -          printf("|%s", sp->subsym[j]->name);
  3059         -        }
  3060         -      }else{
  3061         -        printf(" %s", sp->name);
  3062         -      }
  3063         -      /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
  3064         -    }
         3111  +    rule_print(stdout, rp);
  3065   3112       printf(".");
  3066   3113       if( rp->precsym ) printf(" [%s]",rp->precsym->name);
  3067   3114       /* if( rp->code ) printf("\n    %s",rp->code); */
  3068   3115       printf("\n");
  3069   3116     }
  3070   3117   }
  3071   3118   
................................................................................
  3317   3364   */
  3318   3365   PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
  3319   3366   {
  3320   3367     int act;
  3321   3368     switch( ap->type ){
  3322   3369       case SHIFT:  act = ap->x.stp->statenum;                        break;
  3323   3370       case SHIFTREDUCE: {
  3324         -      act = ap->x.rp->iRule + lemp->nstate;
  3325   3371         /* Since a SHIFT is inherient after a prior REDUCE, convert any
  3326   3372         ** SHIFTREDUCE action with a nonterminal on the LHS into a simple
  3327   3373         ** REDUCE action: */
  3328         -      if( ap->sp->index>=lemp->nterminal ) act += lemp->nrule;
         3374  +      if( ap->sp->index>=lemp->nterminal ){
         3375  +        act = lemp->minReduce + ap->x.rp->iRule;
         3376  +      }else{
         3377  +        act = lemp->minShiftReduce + ap->x.rp->iRule;
         3378  +      }
  3329   3379         break;
  3330   3380       }
  3331         -    case REDUCE: act = ap->x.rp->iRule + lemp->nstate+lemp->nrule; break;
  3332         -    case ERROR:  act = lemp->nstate + lemp->nrule*2;               break;
  3333         -    case ACCEPT: act = lemp->nstate + lemp->nrule*2 + 1;           break;
         3381  +    case REDUCE: act = lemp->minReduce + ap->x.rp->iRule;          break;
         3382  +    case ERROR:  act = lemp->errAction;                            break;
         3383  +    case ACCEPT: act = lemp->accAction;                            break;
  3334   3384       default:     act = -1; break;
  3335   3385     }
  3336   3386     return act;
  3337   3387   }
  3338   3388   
  3339   3389   #define LINESIZE 1000
  3340   3390   /* The next cluster of routines are for reading the template file
................................................................................
  4034   4084     int szActionType;     /* sizeof(YYACTIONTYPE) */
  4035   4085     int szCodeType;       /* sizeof(YYCODETYPE)   */
  4036   4086     const char *name;
  4037   4087     int mnTknOfst, mxTknOfst;
  4038   4088     int mnNtOfst, mxNtOfst;
  4039   4089     struct axset *ax;
  4040   4090   
         4091  +  lemp->minShiftReduce = lemp->nstate;
         4092  +  lemp->errAction = lemp->minShiftReduce + lemp->nrule;
         4093  +  lemp->accAction = lemp->errAction + 1;
         4094  +  lemp->noAction = lemp->accAction + 1;
         4095  +  lemp->minReduce = lemp->noAction + 1;
         4096  +  lemp->maxAction = lemp->minReduce + lemp->nrule;
         4097  +
  4041   4098     in = tplt_open(lemp);
  4042   4099     if( in==0 ) return;
  4043   4100     out = file_open(lemp,".c","wb");
  4044   4101     if( out==0 ){
  4045   4102       fclose(in);
  4046   4103       return;
  4047   4104     }
................................................................................
  4072   4129     tplt_xfer(lemp->name,in,out,&lineno);
  4073   4130   
  4074   4131     /* Generate the defines */
  4075   4132     fprintf(out,"#define YYCODETYPE %s\n",
  4076   4133       minimum_size_type(0, lemp->nsymbol+1, &szCodeType)); lineno++;
  4077   4134     fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1);  lineno++;
  4078   4135     fprintf(out,"#define YYACTIONTYPE %s\n",
  4079         -    minimum_size_type(0,lemp->nstate+lemp->nrule*2+5,&szActionType)); lineno++;
         4136  +    minimum_size_type(0,lemp->maxAction,&szActionType)); lineno++;
  4080   4137     if( lemp->wildcard ){
  4081   4138       fprintf(out,"#define YYWILDCARD %d\n",
  4082   4139          lemp->wildcard->index); lineno++;
  4083   4140     }
  4084   4141     print_stack_union(out,lemp,&lineno,mhflag);
  4085   4142     fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
  4086   4143     if( lemp->stacksize ){
................................................................................
  4140   4197     }
  4141   4198     mxTknOfst = mnTknOfst = 0;
  4142   4199     mxNtOfst = mnNtOfst = 0;
  4143   4200     /* In an effort to minimize the action table size, use the heuristic
  4144   4201     ** of placing the largest action sets first */
  4145   4202     for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i;
  4146   4203     qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare);
  4147         -  pActtab = acttab_alloc();
         4204  +  pActtab = acttab_alloc(lemp->nsymbol, lemp->nterminal);
  4148   4205     for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){
  4149   4206       stp = ax[i].stp;
  4150   4207       if( ax[i].isTkn ){
  4151   4208         for(ap=stp->ap; ap; ap=ap->next){
  4152   4209           int action;
  4153   4210           if( ap->sp->index>=lemp->nterminal ) continue;
  4154   4211           action = compute_action(lemp, ap);
  4155   4212           if( action<0 ) continue;
  4156   4213           acttab_action(pActtab, ap->sp->index, action);
  4157   4214         }
  4158         -      stp->iTknOfst = acttab_insert(pActtab);
         4215  +      stp->iTknOfst = acttab_insert(pActtab, 1);
  4159   4216         if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
  4160   4217         if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
  4161   4218       }else{
  4162   4219         for(ap=stp->ap; ap; ap=ap->next){
  4163   4220           int action;
  4164   4221           if( ap->sp->index<lemp->nterminal ) continue;
  4165   4222           if( ap->sp->index==lemp->nsymbol ) continue;
  4166   4223           action = compute_action(lemp, ap);
  4167   4224           if( action<0 ) continue;
  4168   4225           acttab_action(pActtab, ap->sp->index, action);
  4169   4226         }
  4170         -      stp->iNtOfst = acttab_insert(pActtab);
         4227  +      stp->iNtOfst = acttab_insert(pActtab, 0);
  4171   4228         if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
  4172   4229         if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
  4173   4230       }
  4174   4231   #if 0  /* Uncomment for a trace of how the yy_action[] table fills out */
  4175   4232       { int jj, nn;
  4176   4233         for(jj=nn=0; jj<pActtab->nAction; jj++){
  4177   4234           if( pActtab->aAction[jj].action<0 ) nn++;
................................................................................
  4196   4253       }
  4197   4254     }
  4198   4255   
  4199   4256     /* Finish rendering the constants now that the action table has
  4200   4257     ** been computed */
  4201   4258     fprintf(out,"#define YYNSTATE             %d\n",lemp->nxstate);  lineno++;
  4202   4259     fprintf(out,"#define YYNRULE              %d\n",lemp->nrule);  lineno++;
         4260  +  fprintf(out,"#define YYNTOKEN             %d\n",lemp->nterminal); lineno++;
  4203   4261     fprintf(out,"#define YY_MAX_SHIFT         %d\n",lemp->nxstate-1); lineno++;
  4204         -  fprintf(out,"#define YY_MIN_SHIFTREDUCE   %d\n",lemp->nstate); lineno++;
  4205         -  i = lemp->nstate + lemp->nrule;
         4262  +  i = lemp->minShiftReduce;
         4263  +  fprintf(out,"#define YY_MIN_SHIFTREDUCE   %d\n",i); lineno++;
         4264  +  i += lemp->nrule;
  4206   4265     fprintf(out,"#define YY_MAX_SHIFTREDUCE   %d\n", i-1); lineno++;
  4207         -  fprintf(out,"#define YY_MIN_REDUCE        %d\n", i); lineno++;
  4208         -  i = lemp->nstate + lemp->nrule*2;
         4266  +  fprintf(out,"#define YY_ERROR_ACTION      %d\n", lemp->errAction); lineno++;
         4267  +  fprintf(out,"#define YY_ACCEPT_ACTION     %d\n", lemp->accAction); lineno++;
         4268  +  fprintf(out,"#define YY_NO_ACTION         %d\n", lemp->noAction); lineno++;
         4269  +  fprintf(out,"#define YY_MIN_REDUCE        %d\n", lemp->minReduce); lineno++;
         4270  +  i = lemp->minReduce + lemp->nrule;
  4209   4271     fprintf(out,"#define YY_MAX_REDUCE        %d\n", i-1); lineno++;
  4210         -  fprintf(out,"#define YY_ERROR_ACTION      %d\n", i); lineno++;
  4211         -  fprintf(out,"#define YY_ACCEPT_ACTION     %d\n", i+1); lineno++;
  4212         -  fprintf(out,"#define YY_NO_ACTION         %d\n", i+2); lineno++;
  4213   4272     tplt_xfer(lemp->name,in,out,&lineno);
  4214   4273   
  4215   4274     /* Now output the action table and its associates:
  4216   4275     **
  4217   4276     **  yy_action[]        A single table containing all actions.
  4218   4277     **  yy_lookahead[]     A table containing the lookahead for each entry in
  4219   4278     **                     yy_action.  Used to detect hash collisions.
................................................................................
  4221   4280     **                     shifting terminals.
  4222   4281     **  yy_reduce_ofst[]   For each state, the offset into yy_action for
  4223   4282     **                     shifting non-terminals after a reduce.
  4224   4283     **  yy_default[]       Default action for each state.
  4225   4284     */
  4226   4285   
  4227   4286     /* Output the yy_action table */
  4228         -  lemp->nactiontab = n = acttab_size(pActtab);
         4287  +  lemp->nactiontab = n = acttab_action_size(pActtab);
  4229   4288     lemp->tablesize += n*szActionType;
  4230   4289     fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
  4231   4290     fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
  4232   4291     for(i=j=0; i<n; i++){
  4233   4292       int action = acttab_yyaction(pActtab, i);
  4234         -    if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
         4293  +    if( action<0 ) action = lemp->noAction;
  4235   4294       if( j==0 ) fprintf(out," /* %5d */ ", i);
  4236   4295       fprintf(out, " %4d,", action);
  4237   4296       if( j==9 || i==n-1 ){
  4238   4297         fprintf(out, "\n"); lineno++;
  4239   4298         j = 0;
  4240   4299       }else{
  4241   4300         j++;
  4242   4301       }
  4243   4302     }
  4244   4303     fprintf(out, "};\n"); lineno++;
  4245   4304   
  4246   4305     /* Output the yy_lookahead table */
         4306  +  lemp->nlookaheadtab = n = acttab_lookahead_size(pActtab);
  4247   4307     lemp->tablesize += n*szCodeType;
  4248   4308     fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
  4249   4309     for(i=j=0; i<n; i++){
  4250   4310       int la = acttab_yylookahead(pActtab, i);
  4251   4311       if( la<0 ) la = lemp->nsymbol;
  4252   4312       if( j==0 ) fprintf(out," /* %5d */ ", i);
  4253   4313       fprintf(out, " %4d,", la);
................................................................................
  4259   4319       }
  4260   4320     }
  4261   4321     fprintf(out, "};\n"); lineno++;
  4262   4322   
  4263   4323     /* Output the yy_shift_ofst[] table */
  4264   4324     n = lemp->nxstate;
  4265   4325     while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
  4266         -  fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", lemp->nactiontab); lineno++;
  4267   4326     fprintf(out, "#define YY_SHIFT_COUNT    (%d)\n", n-1); lineno++;
  4268   4327     fprintf(out, "#define YY_SHIFT_MIN      (%d)\n", mnTknOfst); lineno++;
  4269   4328     fprintf(out, "#define YY_SHIFT_MAX      (%d)\n", mxTknOfst); lineno++;
  4270   4329     fprintf(out, "static const %s yy_shift_ofst[] = {\n",
  4271   4330          minimum_size_type(mnTknOfst, lemp->nterminal+lemp->nactiontab, &sz));
  4272   4331          lineno++;
  4273   4332     lemp->tablesize += n*sz;
................................................................................
  4284   4343       }else{
  4285   4344         j++;
  4286   4345       }
  4287   4346     }
  4288   4347     fprintf(out, "};\n"); lineno++;
  4289   4348   
  4290   4349     /* Output the yy_reduce_ofst[] table */
  4291         -  fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
  4292   4350     n = lemp->nxstate;
  4293   4351     while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
  4294   4352     fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
  4295   4353     fprintf(out, "#define YY_REDUCE_MIN   (%d)\n", mnNtOfst); lineno++;
  4296   4354     fprintf(out, "#define YY_REDUCE_MAX   (%d)\n", mxNtOfst); lineno++;
  4297   4355     fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
  4298   4356             minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++;
................................................................................
  4316   4374     /* Output the default action table */
  4317   4375     fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
  4318   4376     n = lemp->nxstate;
  4319   4377     lemp->tablesize += n*szActionType;
  4320   4378     for(i=j=0; i<n; i++){
  4321   4379       stp = lemp->sorted[i];
  4322   4380       if( j==0 ) fprintf(out," /* %5d */ ", i);
  4323         -    fprintf(out, " %4d,", stp->iDfltReduce+lemp->nstate+lemp->nrule);
         4381  +    if( stp->iDfltReduce<0 ){
         4382  +      fprintf(out, " %4d,", lemp->errAction);
         4383  +    }else{
         4384  +      fprintf(out, " %4d,", stp->iDfltReduce + lemp->minReduce);
         4385  +    }
  4324   4386       if( j==9 || i==n-1 ){
  4325   4387         fprintf(out, "\n"); lineno++;
  4326   4388         j = 0;
  4327   4389       }else{
  4328   4390         j++;
  4329   4391       }
  4330   4392     }
................................................................................
  4350   4412     }
  4351   4413     tplt_xfer(lemp->name, in, out, &lineno);
  4352   4414   
  4353   4415     /* Generate a table containing the symbolic name of every symbol
  4354   4416     */
  4355   4417     for(i=0; i<lemp->nsymbol; i++){
  4356   4418       lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name);
  4357         -    fprintf(out,"  %-15s",line);
  4358         -    if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
         4419  +    fprintf(out,"  /* %4d */ \"%s\",\n",i, lemp->symbols[i]->name); lineno++;
  4359   4420     }
  4360         -  if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
  4361   4421     tplt_xfer(lemp->name,in,out,&lineno);
  4362   4422   
  4363   4423     /* Generate a table containing a text string that describes every
  4364   4424     ** rule in the rule set of the grammar.  This information is used
  4365   4425     ** when tracing REDUCE actions.
  4366   4426     */
  4367   4427     for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
................................................................................
  4397   4457       struct symbol *dflt_sp = 0;
  4398   4458       int once = 1;
  4399   4459       for(i=0; i<lemp->nsymbol; i++){
  4400   4460         struct symbol *sp = lemp->symbols[i];
  4401   4461         if( sp==0 || sp->type==TERMINAL ||
  4402   4462             sp->index<=0 || sp->destructor!=0 ) continue;
  4403   4463         if( once ){
  4404         -        fprintf(out, "      /* Default NON-TERMINAL Destructor */\n"); lineno++;
         4464  +        fprintf(out, "      /* Default NON-TERMINAL Destructor */\n");lineno++;
  4405   4465           once = 0;
  4406   4466         }
  4407   4467         fprintf(out,"    case %d: /* %s */\n", sp->index, sp->name); lineno++;
  4408   4468         dflt_sp = sp;
  4409   4469       }
  4410   4470       if( dflt_sp!=0 ){
  4411   4471         emit_destructor_code(out,dflt_sp,lemp,&lineno);
................................................................................
  4440   4500     tplt_xfer(lemp->name,in,out,&lineno);
  4441   4501   
  4442   4502     /* Generate the table of rule information
  4443   4503     **
  4444   4504     ** Note: This code depends on the fact that rules are number
  4445   4505     ** sequentually beginning with 0.
  4446   4506     */
  4447         -  for(rp=lemp->rule; rp; rp=rp->next){
  4448         -    fprintf(out,"  { %d, %d },\n",rp->lhs->index,-rp->nrhs); lineno++;
         4507  +  for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
         4508  +    fprintf(out,"  { %4d, %4d }, /* (%d) ",rp->lhs->index,-rp->nrhs,i);
         4509  +    rule_print(out, rp);
         4510  +    fprintf(out," */\n"); lineno++;
  4449   4511     }
  4450   4512     tplt_xfer(lemp->name,in,out,&lineno);
  4451   4513   
  4452   4514     /* Generate code which execution during each REDUCE action */
  4453   4515     i = 0;
  4454   4516     for(rp=lemp->rule; rp; rp=rp->next){
  4455   4517       i += translate_code(lemp, rp);
................................................................................
  4707   4769     int i;
  4708   4770     struct state *stp;
  4709   4771     struct action *ap;
  4710   4772   
  4711   4773     for(i=0; i<lemp->nstate; i++){
  4712   4774       stp = lemp->sorted[i];
  4713   4775       stp->nTknAct = stp->nNtAct = 0;
  4714         -    stp->iDfltReduce = lemp->nrule;  /* Init dflt action to "syntax error" */
         4776  +    stp->iDfltReduce = -1; /* Init dflt action to "syntax error" */
  4715   4777       stp->iTknOfst = NO_OFFSET;
  4716   4778       stp->iNtOfst = NO_OFFSET;
  4717   4779       for(ap=stp->ap; ap; ap=ap->next){
  4718   4780         int iAction = compute_action(lemp,ap);
  4719   4781         if( iAction>=0 ){
  4720   4782           if( ap->sp->index<lemp->nterminal ){
  4721   4783             stp->nTknAct++;
  4722   4784           }else if( ap->sp->index<lemp->nsymbol ){
  4723   4785             stp->nNtAct++;
  4724   4786           }else{
  4725   4787             assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp );
  4726         -          stp->iDfltReduce = iAction - lemp->nstate - lemp->nrule;
         4788  +          stp->iDfltReduce = iAction;
  4727   4789           }
  4728   4790         }
  4729   4791       }
  4730   4792     }
  4731   4793     qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
  4732   4794           stateResortCompare);
  4733   4795     for(i=0; i<lemp->nstate; i++){

Changes to tool/lempar.c.

    68     68   **    ParseARG_PDECL     A parameter declaration for the %extra_argument
    69     69   **    ParseARG_STORE     Code to store %extra_argument into yypParser
    70     70   **    ParseARG_FETCH     Code to extract %extra_argument from yypParser
    71     71   **    YYERRORSYMBOL      is the code number of the error symbol.  If not
    72     72   **                       defined, then do no error processing.
    73     73   **    YYNSTATE           the combined number of states.
    74     74   **    YYNRULE            the number of rules in the grammar
           75  +**    YYNTOKEN           Number of terminal symbols
    75     76   **    YY_MAX_SHIFT       Maximum value for shift actions
    76     77   **    YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
    77     78   **    YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
    78         -**    YY_MIN_REDUCE      Minimum value for reduce actions
    79         -**    YY_MAX_REDUCE      Maximum value for reduce actions
    80     79   **    YY_ERROR_ACTION    The yy_action[] code for syntax error
    81     80   **    YY_ACCEPT_ACTION   The yy_action[] code for accept
    82     81   **    YY_NO_ACTION       The yy_action[] code for no-op
           82  +**    YY_MIN_REDUCE      Minimum value for reduce actions
           83  +**    YY_MAX_REDUCE      Maximum value for reduce actions
    83     84   */
    84     85   #ifndef INTERFACE
    85     86   # define INTERFACE 1
    86     87   #endif
    87     88   /************* Begin control #defines *****************************************/
    88     89   %%
    89     90   /************* End control #defines *******************************************/
................................................................................
   111    112   **
   112    113   **   0 <= N <= YY_MAX_SHIFT             Shift N.  That is, push the lookahead
   113    114   **                                      token onto the stack and goto state N.
   114    115   **
   115    116   **   N between YY_MIN_SHIFTREDUCE       Shift to an arbitrary state then
   116    117   **     and YY_MAX_SHIFTREDUCE           reduce by rule N-YY_MIN_SHIFTREDUCE.
   117    118   **
   118         -**   N between YY_MIN_REDUCE            Reduce by rule N-YY_MIN_REDUCE
   119         -**     and YY_MAX_REDUCE
   120         -**
   121    119   **   N == YY_ERROR_ACTION               A syntax error has occurred.
   122    120   **
   123    121   **   N == YY_ACCEPT_ACTION              The parser accepts its input.
   124    122   **
   125    123   **   N == YY_NO_ACTION                  No such action.  Denotes unused
   126    124   **                                      slots in the yy_action[] table.
          125  +**
          126  +**   N between YY_MIN_REDUCE            Reduce by rule N-YY_MIN_REDUCE
          127  +**     and YY_MAX_REDUCE
   127    128   **
   128    129   ** The action table is constructed as a single large table named yy_action[].
   129    130   ** Given state S and lookahead X, the action is computed as either:
   130    131   **
   131    132   **    (A)   N = yy_action[ yy_shift_ofst[S] + X ]
   132    133   **    (B)   N = yy_default[S]
   133    134   **
   134         -** The (A) formula is preferred.  The B formula is used instead if:
   135         -**    (1)  The yy_shift_ofst[S]+X value is out of range, or
   136         -**    (2)  yy_lookahead[yy_shift_ofst[S]+X] is not equal to X, or
   137         -**    (3)  yy_shift_ofst[S] equal YY_SHIFT_USE_DFLT.
   138         -** (Implementation note: YY_SHIFT_USE_DFLT is chosen so that
   139         -** YY_SHIFT_USE_DFLT+X will be out of range for all possible lookaheads X.
   140         -** Hence only tests (1) and (2) need to be evaluated.)
          135  +** The (A) formula is preferred.  The B formula is used instead if
          136  +** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
   141    137   **
   142    138   ** The formulas above are for computing the action when the lookahead is
   143    139   ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
   144    140   ** a reduce action) then the yy_reduce_ofst[] array is used in place of
   145         -** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
   146         -** YY_SHIFT_USE_DFLT.
          141  +** the yy_shift_ofst[] array.
   147    142   **
   148    143   ** The following are the tables generated in this section:
   149    144   **
   150    145   **  yy_action[]        A single table containing all actions.
   151    146   **  yy_lookahead[]     A table containing the lookahead for each entry in
   152    147   **                     yy_action.  Used to detect hash collisions.
   153    148   **  yy_shift_ofst[]    For each state, the offset into yy_action for
................................................................................
   255    250     yyTraceFILE = TraceFILE;
   256    251     yyTracePrompt = zTracePrompt;
   257    252     if( yyTraceFILE==0 ) yyTracePrompt = 0;
   258    253     else if( yyTracePrompt==0 ) yyTraceFILE = 0;
   259    254   }
   260    255   #endif /* NDEBUG */
   261    256   
   262         -#ifndef NDEBUG
          257  +#if defined(YYCOVERAGE) || !defined(NDEBUG)
   263    258   /* For tracing shifts, the names of all terminals and nonterminals
   264    259   ** are required.  The following table supplies these names */
   265    260   static const char *const yyTokenName[] = { 
   266    261   %%
   267    262   };
   268         -#endif /* NDEBUG */
          263  +#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
   269    264   
   270    265   #ifndef NDEBUG
   271    266   /* For tracing reduce actions, the names of all rules are required.
   272    267   */
   273    268   static const char *const yyRuleName[] = {
   274    269   %%
   275    270   };
................................................................................
   457    452   #ifdef YYTRACKMAXSTACKDEPTH
   458    453   int ParseStackPeak(void *p){
   459    454     yyParser *pParser = (yyParser*)p;
   460    455     return pParser->yyhwm;
   461    456   }
   462    457   #endif
   463    458   
          459  +/* This array of booleans keeps track of the parser statement
          460  +** coverage.  The element yycoverage[X][Y] is set when the parser
          461  +** is in state X and has a lookahead token Y.  In a well-tested
          462  +** systems, every element of this matrix should end up being set.
          463  +*/
          464  +#if defined(YYCOVERAGE)
          465  +static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
          466  +#endif
          467  +
          468  +/*
          469  +** Write into out a description of every state/lookahead combination that
          470  +**
          471  +**   (1)  has not been used by the parser, and
          472  +**   (2)  is not a syntax error.
          473  +**
          474  +** Return the number of missed state/lookahead combinations.
          475  +*/
          476  +#if defined(YYCOVERAGE)
          477  +int ParseCoverage(FILE *out){
          478  +  int stateno, iLookAhead, i;
          479  +  int nMissed = 0;
          480  +  for(stateno=0; stateno<YYNSTATE; stateno++){
          481  +    i = yy_shift_ofst[stateno];
          482  +    for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
          483  +      if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
          484  +      if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
          485  +      if( out ){
          486  +        fprintf(out,"State %d lookahead %s %s\n", stateno,
          487  +                yyTokenName[iLookAhead],
          488  +                yycoverage[stateno][iLookAhead] ? "ok" : "missed");
          489  +      }
          490  +    }
          491  +  }
          492  +  return nMissed;
          493  +}
          494  +#endif
          495  +
   464    496   /*
   465    497   ** Find the appropriate action for a parser given the terminal
   466    498   ** look-ahead token iLookAhead.
   467    499   */
   468    500   static unsigned int yy_find_shift_action(
   469    501     yyParser *pParser,        /* The parser */
   470    502     YYCODETYPE iLookAhead     /* The look-ahead token */
   471    503   ){
   472    504     int i;
   473    505     int stateno = pParser->yytos->stateno;
   474    506    
   475         -  if( stateno>=YY_MIN_REDUCE ) return stateno;
          507  +  if( stateno>YY_MAX_SHIFT ) return stateno;
   476    508     assert( stateno <= YY_SHIFT_COUNT );
          509  +#if defined(YYCOVERAGE)
          510  +  yycoverage[stateno][iLookAhead] = 1;
          511  +#endif
   477    512     do{
   478    513       i = yy_shift_ofst[stateno];
          514  +    assert( i>=0 && i+YYNTOKEN<=sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
   479    515       assert( iLookAhead!=YYNOCODE );
          516  +    assert( iLookAhead < YYNTOKEN );
   480    517       i += iLookAhead;
   481         -    if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
          518  +    if( yy_lookahead[i]!=iLookAhead ){
   482    519   #ifdef YYFALLBACK
   483    520         YYCODETYPE iFallback;            /* Fallback token */
   484    521         if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
   485    522                && (iFallback = yyFallback[iLookAhead])!=0 ){
   486    523   #ifndef NDEBUG
   487    524           if( yyTraceFILE ){
   488    525             fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
................................................................................
   537    574     if( stateno>YY_REDUCE_COUNT ){
   538    575       return yy_default[stateno];
   539    576     }
   540    577   #else
   541    578     assert( stateno<=YY_REDUCE_COUNT );
   542    579   #endif
   543    580     i = yy_reduce_ofst[stateno];
   544         -  assert( i!=YY_REDUCE_USE_DFLT );
   545    581     assert( iLookAhead!=YYNOCODE );
   546    582     i += iLookAhead;
   547    583   #ifdef YYERRORSYMBOL
   548    584     if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
   549    585       return yy_default[stateno];
   550    586     }
   551    587   #else
................................................................................
   574    610      ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
   575    611   }
   576    612   
   577    613   /*
   578    614   ** Print tracing information for a SHIFT action
   579    615   */
   580    616   #ifndef NDEBUG
   581         -static void yyTraceShift(yyParser *yypParser, int yyNewState){
          617  +static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
   582    618     if( yyTraceFILE ){
   583    619       if( yyNewState<YYNSTATE ){
   584         -      fprintf(yyTraceFILE,"%sShift '%s', go to state %d\n",
   585         -         yyTracePrompt,yyTokenName[yypParser->yytos->major],
          620  +      fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
          621  +         yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
   586    622            yyNewState);
   587    623       }else{
   588         -      fprintf(yyTraceFILE,"%sShift '%s'\n",
   589         -         yyTracePrompt,yyTokenName[yypParser->yytos->major]);
          624  +      fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
          625  +         yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
          626  +         yyNewState - YY_MIN_REDUCE);
   590    627       }
   591    628     }
   592    629   }
   593    630   #else
   594         -# define yyTraceShift(X,Y)
          631  +# define yyTraceShift(X,Y,Z)
   595    632   #endif
   596    633   
   597    634   /*
   598    635   ** Perform a shift action.
   599    636   */
   600    637   static void yy_shift(
   601    638     yyParser *yypParser,          /* The parser to be shifted */
................................................................................
   629    666     if( yyNewState > YY_MAX_SHIFT ){
   630    667       yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
   631    668     }
   632    669     yytos = yypParser->yytos;
   633    670     yytos->stateno = (YYACTIONTYPE)yyNewState;
   634    671     yytos->major = (YYCODETYPE)yyMajor;
   635    672     yytos->minor.yy0 = yyMinor;
   636         -  yyTraceShift(yypParser, yyNewState);
          673  +  yyTraceShift(yypParser, yyNewState, "Shift");
   637    674   }
   638    675   
   639    676   /* The following table contains information about every rule that
   640    677   ** is used during the reduce.
   641    678   */
   642    679   static const struct {
   643    680     YYCODETYPE lhs;       /* Symbol on the left-hand side of the rule */
................................................................................
   669    706     yyStackEntry *yymsp;            /* The top of the parser's stack */
   670    707     int yysize;                     /* Amount to pop the stack */
   671    708     ParseARG_FETCH;
   672    709     yymsp = yypParser->yytos;
   673    710   #ifndef NDEBUG
   674    711     if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
   675    712       yysize = yyRuleInfo[yyruleno].nrhs;
   676         -    fprintf(yyTraceFILE, "%sReduce [%s], go to state %d.\n", yyTracePrompt,
   677         -      yyRuleName[yyruleno], yymsp[yysize].stateno);
          713  +    if( yysize ){
          714  +      fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
          715  +        yyTracePrompt,
          716  +        yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno);
          717  +    }else{
          718  +      fprintf(yyTraceFILE, "%sReduce %d [%s].\n",
          719  +        yyTracePrompt, yyruleno, yyRuleName[yyruleno]);
          720  +    }
   678    721     }
   679    722   #endif /* NDEBUG */
   680    723   
   681    724     /* Check that the stack is large enough to grow by a single entry
   682    725     ** if the RHS of the rule is empty.  This ensures that there is room
   683    726     ** enough on the stack to push the LHS value */
   684    727     if( yyRuleInfo[yyruleno].nrhs==0 ){
................................................................................
   725    768     /* There are no SHIFTREDUCE actions on nonterminals because the table
   726    769     ** generator has simplified them to pure REDUCE actions. */
   727    770     assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
   728    771   
   729    772     /* It is not possible for a REDUCE to be followed by an error */
   730    773     assert( yyact!=YY_ERROR_ACTION );
   731    774   
   732         -  if( yyact==YY_ACCEPT_ACTION ){
   733         -    yypParser->yytos += yysize;
   734         -    yy_accept(yypParser);
   735         -  }else{
   736         -    yymsp += yysize+1;
   737         -    yypParser->yytos = yymsp;
   738         -    yymsp->stateno = (YYACTIONTYPE)yyact;
   739         -    yymsp->major = (YYCODETYPE)yygoto;
   740         -    yyTraceShift(yypParser, yyact);
   741         -  }
          775  +  yymsp += yysize+1;
          776  +  yypParser->yytos = yymsp;
          777  +  yymsp->stateno = (YYACTIONTYPE)yyact;
          778  +  yymsp->major = (YYCODETYPE)yygoto;
          779  +  yyTraceShift(yypParser, yyact, "... then shift");
   742    780   }
   743    781   
   744    782   /*
   745    783   ** The following code executes when the parse fails
   746    784   */
   747    785   #ifndef YYNOERRORRECOVERY
   748    786   static void yy_parse_failed(
................................................................................
   844    882   #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
   845    883     yyendofinput = (yymajor==0);
   846    884   #endif
   847    885     ParseARG_STORE;
   848    886   
   849    887   #ifndef NDEBUG
   850    888     if( yyTraceFILE ){
   851         -    fprintf(yyTraceFILE,"%sInput '%s'\n",yyTracePrompt,yyTokenName[yymajor]);
          889  +    int stateno = yypParser->yytos->stateno;
          890  +    if( stateno < YY_MIN_REDUCE ){
          891  +      fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
          892  +              yyTracePrompt,yyTokenName[yymajor],stateno);
          893  +    }else{
          894  +      fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
          895  +              yyTracePrompt,yyTokenName[yymajor],stateno-YY_MIN_REDUCE);
          896  +    }
   852    897     }
   853    898   #endif
   854    899   
   855    900     do{
   856    901       yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
   857         -    if( yyact <= YY_MAX_SHIFTREDUCE ){
          902  +    if( yyact >= YY_MIN_REDUCE ){
          903  +      yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,yyminor);
          904  +    }else if( yyact <= YY_MAX_SHIFTREDUCE ){
   858    905         yy_shift(yypParser,yyact,yymajor,yyminor);
   859    906   #ifndef YYNOERRORRECOVERY
   860    907         yypParser->yyerrcnt--;
   861    908   #endif
   862    909         yymajor = YYNOCODE;
   863         -    }else if( yyact <= YY_MAX_REDUCE ){
   864         -      yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,yyminor);
          910  +    }else if( yyact==YY_ACCEPT_ACTION ){
          911  +      yypParser->yytos--;
          912  +      yy_accept(yypParser);
          913  +      return;
   865    914       }else{
   866    915         assert( yyact == YY_ERROR_ACTION );
   867    916         yyminorunion.yy0 = yyminor;
   868    917   #ifdef YYERRORSYMBOL
   869    918         int yymx;
   870    919   #endif
   871    920   #ifndef NDEBUG

Changes to tool/speed-check.sh.

   113    113       --rtree)
   114    114           SPEEDTEST_OPTS="$SPEEDTEST_OPTS --testset rtree"
   115    115           CC_OPTS="$CC_OPTS -DSQLITE_ENABLE_RTREE"
   116    116           ;;
   117    117       --orm)
   118    118           SPEEDTEST_OPTS="$SPEEDTEST_OPTS --testset orm"
   119    119           ;;
          120  +    --cte)
          121  +        SPEEDTEST_OPTS="$SPEEDTEST_OPTS --testset cte"
          122  +        ;;
          123  +    --fp)
          124  +        SPEEDTEST_OPTS="$SPEEDTEST_OPTS --testset fp"
          125  +        ;;
   120    126       -*)
   121    127           CC_OPTS="$CC_OPTS $1"
   122    128           ;;
   123    129       *)
   124    130   	BASELINE=$1
   125    131           ;;
   126    132     esac