Check-in Differences
Not logged in

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

Difference From 73bf3080dc01a374 To 13380236a123bb96

2012-05-23
14:03
including the latest EPSG CRS definitions supported by PROJ4 v.4.8.0 check-in: b9a70ef80a user: sandro tags: trunk
2012-05-18
12:02
including the latest SQLite 3.7.12 check-in: 13380236a1 user: sandro tags: trunk
2012-05-15
10:05
fixing a bug in load_shapefile() and load_dbf() [double quoting the table name] check-in: 0076590df5 user: sandro tags: trunk
2012-05-12
02:06
FiFix a typo in the configure.ac script that causes problems detecting a 3.3.0 or later version of GEOS. check-in: 73bf3080dc user: bradh tags: trunk
2012-05-10
15:40
udating the build scripts in order to support Android (as suggested by Marco Bernasocchi, QGIS-Android) check-in: 75bf471ff5 user: sandro tags: trunk

Changes to src/headers/spatialite/sqlite3.h.

   103    103   ** string contains the date and time of the check-in (UTC) and an SHA1
   104    104   ** hash of the entire source tree.
   105    105   **
   106    106   ** See also: [sqlite3_libversion()],
   107    107   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   108    108   ** [sqlite_version()] and [sqlite_source_id()].
   109    109   */
   110         -#define SQLITE_VERSION        "3.7.11"
   111         -#define SQLITE_VERSION_NUMBER 3007011
   112         -#define SQLITE_SOURCE_ID      "2012-03-20 11:35:50 00bb9c9ce4f465e6ac321ced2a9d0062dc364669"
          110  +#define SQLITE_VERSION        "3.7.12"
          111  +#define SQLITE_VERSION_NUMBER 3007012
          112  +#define SQLITE_SOURCE_ID      "2012-05-14 01:41:23 8654aa9540fe9fd210899d83d17f3f407096c004"
   113    113   
   114    114   /*
   115    115   ** CAPI3REF: Run-Time Library Version Numbers
   116    116   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   117    117   **
   118    118   ** These interfaces provide the same information as the [SQLITE_VERSION],
   119    119   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   454    454   #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
   455    455   #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
   456    456   #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
   457    457   #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
   458    458   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
   459    459   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
   460    460   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
          461  +#define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
   461    462   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
   462    463   #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   463    464   #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   464    465   #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
   465    466   
   466    467   /*
   467    468   ** CAPI3REF: Flags For File Open Operations
................................................................................
   767    768   ** integer is the delay.  If either integer is negative, then the setting
   768    769   ** is not changed but instead the prior value of that setting is written
   769    770   ** into the array entry, allowing the current retry settings to be
   770    771   ** interrogated.  The zDbName parameter is ignored.
   771    772   **
   772    773   ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
   773    774   ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
   774         -** persistent [WAL | Write AHead Log] setting.  By default, the auxiliary
          775  +** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
   775    776   ** write ahead log and shared memory files used for transaction control
   776    777   ** are automatically deleted when the latest connection to the database
   777    778   ** closes.  Setting persistent WAL mode causes those files to persist after
   778    779   ** close.  Persisting the files is useful when other processes that do not
   779    780   ** have write permission on the directory containing the database file want
   780    781   ** to read the database file, as the WAL and shared memory files must exist
   781    782   ** in order for the database to be readable.  The fourth parameter to
................................................................................
  1543   1544   ** connection is opened. If it is globally disabled, filenames are
  1544   1545   ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  1545   1546   ** database connection is opened. By default, URI handling is globally
  1546   1547   ** disabled. The default value may be changed by compiling with the
  1547   1548   ** [SQLITE_USE_URI] symbol defined.
  1548   1549   **
  1549   1550   ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
  1550         -** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
         1551  +** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
  1551   1552   ** <dd> These options are obsolete and should not be used by new code.
  1552   1553   ** They are retained for backwards compatibility but are now no-ops.
  1553   1554   ** </dl>
  1554   1555   */
  1555   1556   #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  1556   1557   #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  1557   1558   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
................................................................................
  6002   6003   ** </dd>
  6003   6004   **
  6004   6005   ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
  6005   6006   ** <dd>This parameter returns the number of pager cache misses that have
  6006   6007   ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
  6007   6008   ** is always 0.
  6008   6009   ** </dd>
         6010  +**
         6011  +** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
         6012  +** <dd>This parameter returns the number of dirty cache entries that have
         6013  +** been written to disk. Specifically, the number of pages written to the
         6014  +** wal file in wal mode databases, or the number of pages written to the
         6015  +** database file in rollback mode databases. Any pages written as part of
         6016  +** transaction rollback or database recovery operations are not included.
         6017  +** If an IO or other error occurs while writing a page to disk, the effect
         6018  +** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
         6019  +** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
         6020  +** </dd>
  6009   6021   ** </dl>
  6010   6022   */
  6011   6023   #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  6012   6024   #define SQLITE_DBSTATUS_CACHE_USED           1
  6013   6025   #define SQLITE_DBSTATUS_SCHEMA_USED          2
  6014   6026   #define SQLITE_DBSTATUS_STMT_USED            3
  6015   6027   #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  6016   6028   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  6017   6029   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  6018   6030   #define SQLITE_DBSTATUS_CACHE_HIT            7
  6019   6031   #define SQLITE_DBSTATUS_CACHE_MISS           8
  6020         -#define SQLITE_DBSTATUS_MAX                  8   /* Largest defined DBSTATUS */
         6032  +#define SQLITE_DBSTATUS_CACHE_WRITE          9
         6033  +#define SQLITE_DBSTATUS_MAX                  9   /* Largest defined DBSTATUS */
  6021   6034   
  6022   6035   
  6023   6036   /*
  6024   6037   ** CAPI3REF: Prepared Statement Status
  6025   6038   **
  6026   6039   ** ^(Each prepared statement maintains various
  6027   6040   ** [SQLITE_STMTSTATUS counters] that measure the number
................................................................................
  6969   6982   ** R-Tree geometry query as follows:
  6970   6983   **
  6971   6984   **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
  6972   6985   */
  6973   6986   SQLITE_API int sqlite3_rtree_geometry_callback(
  6974   6987     sqlite3 *db,
  6975   6988     const char *zGeom,
  6976         -  int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
         6989  +#ifdef SQLITE_RTREE_INT_ONLY
         6990  +  int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
         6991  +#else
         6992  +  int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
         6993  +#endif
  6977   6994     void *pContext
  6978   6995   );
  6979   6996   
  6980   6997   
  6981   6998   /*
  6982   6999   ** A pointer to a structure of the following type is passed as the first
  6983   7000   ** argument to callbacks registered using rtree_geometry_callback().

Changes to src/shapefiles/shapefiles.c.

   295    295       int current_row;
   296    296       char **col_name = NULL;
   297    297       unsigned char *blob;
   298    298       int blob_size;
   299    299       char *geom_type;
   300    300       char *txt_dims;
   301    301       char *geo_column = column;
          302  +    char qtable[1024];
          303  +    char *xtable = NULL;
   302    304       if (!geo_column)
   303    305   	geo_column = "Geometry";
          306  +    xtable = gaiaDoubleQuotedSql (table);
          307  +    if (xtable)
          308  +      {
          309  +	  strcpy (qtable, xtable);
          310  +	  free (xtable);
          311  +      }
   304    312   /* checking if TABLE already exists */
   305    313       sprintf (sql,
   306    314   	     "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '%s'",
   307    315   	     table);
   308    316       ret = sqlite3_prepare_v2 (sqlite, sql, strlen (sql), &stmt, NULL);
   309    317       if (ret != SQLITE_OK)
   310    318         {
................................................................................
   448    456   	  else
   449    457   	      sprintf (err_msg, "load shapefile error: <%s>\n", errMsg);
   450    458   	  sqlite3_free (errMsg);
   451    459   	  sqlError = 1;
   452    460   	  goto clean_up;
   453    461         }
   454    462   /* creating the Table */
   455         -    sprintf (sql, "CREATE TABLE %s", table);
          463  +    sprintf (sql, "CREATE TABLE \"%s\"", qtable);
   456    464       strcat (sql, " (\nPK_UID INTEGER PRIMARY KEY AUTOINCREMENT");
   457    465       cnt = 0;
   458    466       dbf_field = shp->Dbf->First;
   459    467       while (dbf_field)
   460    468         {
   461    469   	  strcat (sql, ",\n\"");
   462    470   	  strcat (sql, *(col_name + cnt));
................................................................................
   610    618   		   / LINESTRING/MULTILINESTRING or 
   611    619   		   / POLYGON/MULTIPOLYGON
   612    620   		 */
   613    621   		gaiaShpAnalyze (shp);
   614    622   	    }
   615    623         }
   616    624       /* preparing the INSERT INTO parametrerized statement */
   617         -    sprintf (sql, "INSERT INTO %s (PK_UID,", table);
          625  +    sprintf (sql, "INSERT INTO \"%s\" (PK_UID,", qtable);
   618    626       cnt = 0;
   619    627       dbf_field = shp->Dbf->First;
   620    628       while (dbf_field)
   621    629         {
   622    630   	  /* columns corresponding to some DBF attribute */
   623    631   	  strcat (sql, "\"");
   624    632   	  strcat (sql, *(col_name + cnt++));
................................................................................
   687    695   			case GAIA_DOUBLE_VALUE:
   688    696   			    sqlite3_bind_double (stmt, cnt + 2,
   689    697   						 dbf_field->Value->DblValue);
   690    698   			    break;
   691    699   			case GAIA_TEXT_VALUE:
   692    700   			    sqlite3_bind_text (stmt, cnt + 2,
   693    701   					       dbf_field->Value->TxtValue,
   694         -					       strlen (dbf_field->Value->
   695         -						       TxtValue),
          702  +					       strlen (dbf_field->
          703  +						       Value->TxtValue),
   696    704   					       SQLITE_STATIC);
   697    705   			    break;
   698    706   			default:
   699    707   			    sqlite3_bind_null (stmt, cnt + 2);
   700    708   			    break;
   701    709   			}
   702    710   		  }
................................................................................
  1482   1490       int seed;
  1483   1491       int len;
  1484   1492       int dup;
  1485   1493       int idup;
  1486   1494       int current_row;
  1487   1495       char **col_name = NULL;
  1488   1496       int deleted;
         1497  +    char qtable[1024];
         1498  +    char *xtable = NULL;
         1499  +    xtable = gaiaDoubleQuotedSql (table);
         1500  +    if (xtable)
         1501  +      {
         1502  +	  strcpy (qtable, xtable);
         1503  +	  free (xtable);
         1504  +      }
  1489   1505   /* checking if TABLE already exists */
  1490   1506       sprintf (sql,
  1491   1507   	     "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '%s'",
  1492   1508   	     table);
  1493   1509       ret = sqlite3_prepare_v2 (sqlite, sql, strlen (sql), &stmt, NULL);
  1494   1510       if (ret != SQLITE_OK)
  1495   1511         {
................................................................................
  1595   1611   	  else
  1596   1612   	      sprintf (err_msg, "load DBF error: <%s>\n", errMsg);
  1597   1613   	  sqlite3_free (errMsg);
  1598   1614   	  sqlError = 1;
  1599   1615   	  goto clean_up;
  1600   1616         }
  1601   1617   /* creating the Table */
  1602         -    sprintf (sql, "CREATE TABLE %s", table);
         1618  +    sprintf (sql, "CREATE TABLE \"%s\"", qtable);
  1603   1619       strcat (sql, " (\nPK_UID INTEGER PRIMARY KEY AUTOINCREMENT");
  1604   1620       cnt = 0;
  1605   1621       dbf_field = dbf->Dbf->First;
  1606   1622       while (dbf_field)
  1607   1623         {
  1608   1624   	  strcat (sql, ",\n\"");
  1609   1625   	  strcat (sql, *(col_name + cnt));
................................................................................
  1647   1663   	  else
  1648   1664   	      sprintf (err_msg, "load DBF error: <%s>\n", errMsg);
  1649   1665   	  sqlite3_free (errMsg);
  1650   1666   	  sqlError = 1;
  1651   1667   	  goto clean_up;
  1652   1668         }
  1653   1669       /* preparing the INSERT INTO parametrerized statement */
  1654         -    sprintf (sql, "INSERT INTO %s (PK_UID", table);
         1670  +    sprintf (sql, "INSERT INTO \"%s\" (PK_UID", qtable);
  1655   1671       cnt = 0;
  1656   1672       dbf_field = dbf->Dbf->First;
  1657   1673       while (dbf_field)
  1658   1674         {
  1659   1675   	  /* columns corresponding to some DBF attribute */
  1660   1676   	  strcat (sql, ",\"");
  1661   1677   	  strcat (sql, *(col_name + cnt++));
................................................................................
  1726   1742   			case GAIA_DOUBLE_VALUE:
  1727   1743   			    sqlite3_bind_double (stmt, cnt + 2,
  1728   1744   						 dbf_field->Value->DblValue);
  1729   1745   			    break;
  1730   1746   			case GAIA_TEXT_VALUE:
  1731   1747   			    sqlite3_bind_text (stmt, cnt + 2,
  1732   1748   					       dbf_field->Value->TxtValue,
  1733         -					       strlen (dbf_field->Value->
  1734         -						       TxtValue),
         1749  +					       strlen (dbf_field->
         1750  +						       Value->TxtValue),
  1735   1751   					       SQLITE_STATIC);
  1736   1752   			    break;
  1737   1753   			default:
  1738   1754   			    sqlite3_bind_null (stmt, cnt + 2);
  1739   1755   			    break;
  1740   1756   			}
  1741   1757   		  }

Changes to src/sqlite3/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.7.11.  By combining all the individual C code files into this 
            3  +** version 3.7.12.  By combining all the individual C code files into this 
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
   653    653   ** string contains the date and time of the check-in (UTC) and an SHA1
   654    654   ** hash of the entire source tree.
   655    655   **
   656    656   ** See also: [sqlite3_libversion()],
   657    657   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   658    658   ** [sqlite_version()] and [sqlite_source_id()].
   659    659   */
   660         -#define SQLITE_VERSION        "3.7.11"
   661         -#define SQLITE_VERSION_NUMBER 3007011
   662         -#define SQLITE_SOURCE_ID      "2012-03-20 11:35:50 00bb9c9ce4f465e6ac321ced2a9d0062dc364669"
          660  +#define SQLITE_VERSION        "3.7.12"
          661  +#define SQLITE_VERSION_NUMBER 3007012
          662  +#define SQLITE_SOURCE_ID      "2012-05-14 01:41:23 8654aa9540fe9fd210899d83d17f3f407096c004"
   663    663   
   664    664   /*
   665    665   ** CAPI3REF: Run-Time Library Version Numbers
   666    666   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   667    667   **
   668    668   ** These interfaces provide the same information as the [SQLITE_VERSION],
   669    669   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1004   1004   #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
  1005   1005   #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
  1006   1006   #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
  1007   1007   #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
  1008   1008   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
  1009   1009   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
  1010   1010   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
         1011  +#define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
  1011   1012   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
  1012   1013   #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
  1013   1014   #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
  1014   1015   #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
  1015   1016   
  1016   1017   /*
  1017   1018   ** CAPI3REF: Flags For File Open Operations
................................................................................
  1317   1318   ** integer is the delay.  If either integer is negative, then the setting
  1318   1319   ** is not changed but instead the prior value of that setting is written
  1319   1320   ** into the array entry, allowing the current retry settings to be
  1320   1321   ** interrogated.  The zDbName parameter is ignored.
  1321   1322   **
  1322   1323   ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
  1323   1324   ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
  1324         -** persistent [WAL | Write AHead Log] setting.  By default, the auxiliary
         1325  +** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
  1325   1326   ** write ahead log and shared memory files used for transaction control
  1326   1327   ** are automatically deleted when the latest connection to the database
  1327   1328   ** closes.  Setting persistent WAL mode causes those files to persist after
  1328   1329   ** close.  Persisting the files is useful when other processes that do not
  1329   1330   ** have write permission on the directory containing the database file want
  1330   1331   ** to read the database file, as the WAL and shared memory files must exist
  1331   1332   ** in order for the database to be readable.  The fourth parameter to
................................................................................
  2093   2094   ** connection is opened. If it is globally disabled, filenames are
  2094   2095   ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  2095   2096   ** database connection is opened. By default, URI handling is globally
  2096   2097   ** disabled. The default value may be changed by compiling with the
  2097   2098   ** [SQLITE_USE_URI] symbol defined.
  2098   2099   **
  2099   2100   ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
  2100         -** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
         2101  +** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
  2101   2102   ** <dd> These options are obsolete and should not be used by new code.
  2102   2103   ** They are retained for backwards compatibility but are now no-ops.
  2103   2104   ** </dl>
  2104   2105   */
  2105   2106   #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  2106   2107   #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  2107   2108   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
................................................................................
  6552   6553   ** </dd>
  6553   6554   **
  6554   6555   ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
  6555   6556   ** <dd>This parameter returns the number of pager cache misses that have
  6556   6557   ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
  6557   6558   ** is always 0.
  6558   6559   ** </dd>
         6560  +**
         6561  +** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
         6562  +** <dd>This parameter returns the number of dirty cache entries that have
         6563  +** been written to disk. Specifically, the number of pages written to the
         6564  +** wal file in wal mode databases, or the number of pages written to the
         6565  +** database file in rollback mode databases. Any pages written as part of
         6566  +** transaction rollback or database recovery operations are not included.
         6567  +** If an IO or other error occurs while writing a page to disk, the effect
         6568  +** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
         6569  +** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
         6570  +** </dd>
  6559   6571   ** </dl>
  6560   6572   */
  6561   6573   #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  6562   6574   #define SQLITE_DBSTATUS_CACHE_USED           1
  6563   6575   #define SQLITE_DBSTATUS_SCHEMA_USED          2
  6564   6576   #define SQLITE_DBSTATUS_STMT_USED            3
  6565   6577   #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  6566   6578   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  6567   6579   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  6568   6580   #define SQLITE_DBSTATUS_CACHE_HIT            7
  6569   6581   #define SQLITE_DBSTATUS_CACHE_MISS           8
  6570         -#define SQLITE_DBSTATUS_MAX                  8   /* Largest defined DBSTATUS */
         6582  +#define SQLITE_DBSTATUS_CACHE_WRITE          9
         6583  +#define SQLITE_DBSTATUS_MAX                  9   /* Largest defined DBSTATUS */
  6571   6584   
  6572   6585   
  6573   6586   /*
  6574   6587   ** CAPI3REF: Prepared Statement Status
  6575   6588   **
  6576   6589   ** ^(Each prepared statement maintains various
  6577   6590   ** [SQLITE_STMTSTATUS counters] that measure the number
................................................................................
  7519   7532   ** R-Tree geometry query as follows:
  7520   7533   **
  7521   7534   **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
  7522   7535   */
  7523   7536   SQLITE_API int sqlite3_rtree_geometry_callback(
  7524   7537     sqlite3 *db,
  7525   7538     const char *zGeom,
  7526         -  int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
         7539  +#ifdef SQLITE_RTREE_INT_ONLY
         7540  +  int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
         7541  +#else
         7542  +  int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
         7543  +#endif
  7527   7544     void *pContext
  7528   7545   );
  7529   7546   
  7530   7547   
  7531   7548   /*
  7532   7549   ** A pointer to a structure of the following type is passed as the first
  7533   7550   ** argument to callbacks registered using rtree_geometry_callback().
................................................................................
  9917   9934   struct FuncDestructor {
  9918   9935     int nRef;
  9919   9936     void (*xDestroy)(void *);
  9920   9937     void *pUserData;
  9921   9938   };
  9922   9939   
  9923   9940   /*
  9924         -** Possible values for FuncDef.flags
         9941  +** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
         9942  +** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
         9943  +** are assert() statements in the code to verify this.
  9925   9944   */
  9926   9945   #define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
  9927   9946   #define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
  9928   9947   #define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
  9929   9948   #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
  9930         -#define SQLITE_FUNC_COUNT    0x20 /* Built-in count(*) aggregate */
  9931         -#define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
         9949  +#define SQLITE_FUNC_COUNT    0x10 /* Built-in count(*) aggregate */
         9950  +#define SQLITE_FUNC_COALESCE 0x20 /* Built-in coalesce() or ifnull() function */
         9951  +#define SQLITE_FUNC_LENGTH   0x40 /* Built-in length() function */
         9952  +#define SQLITE_FUNC_TYPEOF   0x80 /* Built-in typeof() function */
  9932   9953   
  9933   9954   /*
  9934   9955   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
  9935   9956   ** used to create the initializers for the FuncDef structures.
  9936   9957   **
  9937   9958   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
  9938   9959   **     Used to create a scalar function definition of a function zName 
................................................................................
  9952   9973   **     that accepts nArg arguments and is implemented by a call to C 
  9953   9974   **     function likeFunc. Argument pArg is cast to a (void *) and made
  9954   9975   **     available as the function user-data (sqlite3_user_data()). The
  9955   9976   **     FuncDef.flags variable is set to the value passed as the flags
  9956   9977   **     parameter.
  9957   9978   */
  9958   9979   #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
  9959         -  {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
         9980  +  {nArg, SQLITE_UTF8, (bNC*SQLITE_FUNC_NEEDCOLL), \
         9981  +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
         9982  +#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
         9983  +  {nArg, SQLITE_UTF8, (bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
  9960   9984      SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
  9961   9985   #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
  9962   9986     {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
  9963   9987      pArg, 0, xFunc, 0, 0, #zName, 0, 0}
  9964   9988   #define LIKEFUNC(zName, nArg, arg, flags) \
  9965   9989     {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
  9966   9990   #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
................................................................................
 10182  10206     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 10183  10207     u16 nRef;            /* Number of pointers to this Table */
 10184  10208     u8 tabFlags;         /* Mask of TF_* values */
 10185  10209     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
 10186  10210     FKey *pFKey;         /* Linked list of all foreign keys in this table */
 10187  10211     char *zColAff;       /* String defining the affinity of each column */
 10188  10212   #ifndef SQLITE_OMIT_CHECK
 10189         -  Expr *pCheck;        /* The AND of all CHECK constraints */
        10213  +  ExprList *pCheck;    /* All CHECK constraints */
 10190  10214   #endif
 10191  10215   #ifndef SQLITE_OMIT_ALTERTABLE
 10192  10216     int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
 10193  10217   #endif
 10194  10218   #ifndef SQLITE_OMIT_VIRTUALTABLE
 10195  10219     VTable *pVTable;     /* List of VTable objects. */
 10196  10220     int nModuleArg;      /* Number of arguments to the module */
................................................................................
 10575  10599                            ** TK_TRIGGER: 1 -> new, 0 -> old */
 10576  10600     ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
 10577  10601                            ** TK_VARIABLE: variable number (always >= 1). */
 10578  10602     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 10579  10603     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 10580  10604     u8 flags2;             /* Second set of flags.  EP2_... */
 10581  10605     u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
        10606  +                         /* If TK_COLUMN, the value of p5 for OP_Column */
 10582  10607     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 10583  10608     Table *pTab;           /* Table for TK_COLUMN expressions. */
 10584  10609   #if SQLITE_MAX_EXPR_DEPTH>0
 10585  10610     int nHeight;           /* Height of the tree headed by this node */
 10586  10611   #endif
 10587  10612   };
 10588  10613   
................................................................................
 10597  10622   #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
 10598  10623   #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
 10599  10624   #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
 10600  10625   #define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
 10601  10626   #define EP_FixedDest  0x0200  /* Result needed in a specific register */
 10602  10627   #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
 10603  10628   #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
 10604         -#define EP_Hint       0x1000  /* Optimizer hint. Not required for correctness */
        10629  +#define EP_Hint       0x1000  /* Not used */
 10605  10630   #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
 10606  10631   #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
 10607  10632   #define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
 10608  10633   
 10609  10634   /*
 10610  10635   ** The following are the meanings of bits in the Expr.flags2 field.
 10611  10636   */
................................................................................
 10911  10936     SrcList *pSrcList;   /* One or more tables used to resolve names */
 10912  10937     ExprList *pEList;    /* Optional list of named expressions */
 10913  10938     int nRef;            /* Number of names resolved by this context */
 10914  10939     int nErr;            /* Number of errors encountered while resolving names */
 10915  10940     u8 allowAgg;         /* Aggregate functions allowed here */
 10916  10941     u8 hasAgg;           /* True if aggregates are seen */
 10917  10942     u8 isCheck;          /* True if resolving names in a CHECK constraint */
 10918         -  int nDepth;          /* Depth of subquery recursion. 1 for no recursion */
 10919  10943     AggInfo *pAggInfo;   /* Information about aggregates at this level */
 10920  10944     NameContext *pNext;  /* Next outer name context.  NULL for outermost */
 10921  10945   };
 10922  10946   
 10923  10947   /*
 10924  10948   ** An instance of the following structure contains all information
 10925  10949   ** needed to generate code for a single SELECT statement.
................................................................................
 11116  11140     yDbMask writeMask;   /* Start a write transaction on these databases */
 11117  11141     yDbMask cookieMask;  /* Bitmask of schema verified databases */
 11118  11142     int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
 11119  11143     int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
 11120  11144     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 11121  11145     int regRoot;         /* Register holding root page number for new objects */
 11122  11146     int nMaxArg;         /* Max args passed to user function by sub-program */
        11147  +  Token constraintName;/* Name of the constraint currently being parsed */
 11123  11148   #ifndef SQLITE_OMIT_SHARED_CACHE
 11124  11149     int nTableLock;        /* Number of locks in aTableLock */
 11125  11150     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 11126  11151   #endif
 11127  11152     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 11128  11153   
 11129  11154     /* Information used while coding trigger programs. */
................................................................................
 11184  11209   */
 11185  11210   struct AuthContext {
 11186  11211     const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
 11187  11212     Parse *pParse;              /* The Parse structure */
 11188  11213   };
 11189  11214   
 11190  11215   /*
 11191         -** Bitfield flags for P5 value in OP_Insert and OP_Delete
        11216  +** Bitfield flags for P5 value in various opcodes.
 11192  11217   */
 11193  11218   #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
 11194  11219   #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
 11195  11220   #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 11196  11221   #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 11197  11222   #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 11198  11223   #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
        11224  +#define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
        11225  +#define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 11199  11226   
 11200  11227   /*
 11201  11228    * Each trigger present in the database schema is stored as an instance of
 11202  11229    * struct Trigger. 
 11203  11230    *
 11204  11231    * Pointers to instances of struct Trigger are stored in two ways.
 11205  11232    * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
................................................................................
 11374  11401   struct Walker {
 11375  11402     int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
 11376  11403     int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
 11377  11404     Parse *pParse;                            /* Parser context.  */
 11378  11405     union {                                   /* Extra data for callback */
 11379  11406       NameContext *pNC;                          /* Naming context */
 11380  11407       int i;                                     /* Integer value */
        11408  +    SrcList *pSrcList;                         /* FROM clause */
 11381  11409     } u;
 11382  11410   };
 11383  11411   
 11384  11412   /* Forward declarations */
 11385  11413   SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 11386  11414   SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 11387  11415   SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
................................................................................
 11675  11703   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 11676  11704   SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
 11677  11705   #endif
 11678  11706   SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
 11679  11707   SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
 11680  11708   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**,ExprList*,u16);
 11681  11709   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 11682         -SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
        11710  +SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
 11683  11711   SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
 11684  11712   SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
 11685  11713   SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
 11686  11714   SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
 11687  11715   SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
 11688  11716   SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
 11689  11717   SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
................................................................................
 11742  11770   SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
 11743  11771   SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
 11744  11772   SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 11745  11773   SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 11746  11774   SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
 11747  11775   SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
 11748  11776   SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
 11749         -SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
        11777  +SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
 11750  11778   SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
 11751  11779   SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 11752  11780   SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
 11753  11781   SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 11754  11782   SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
 11755  11783   SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
 11756  11784   
................................................................................
 13507  13535   
 13508  13536       /*
 13509  13537       ** Set *pCurrent to the total cache hits or misses encountered by all
 13510  13538       ** pagers the database handle is connected to. *pHighwater is always set 
 13511  13539       ** to zero.
 13512  13540       */
 13513  13541       case SQLITE_DBSTATUS_CACHE_HIT:
 13514         -    case SQLITE_DBSTATUS_CACHE_MISS: {
        13542  +    case SQLITE_DBSTATUS_CACHE_MISS:
        13543  +    case SQLITE_DBSTATUS_CACHE_WRITE:{
 13515  13544         int i;
 13516  13545         int nRet = 0;
 13517  13546         assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
        13547  +      assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
 13518  13548   
 13519  13549         for(i=0; i<db->nDb; i++){
 13520  13550           if( db->aDb[i].pBt ){
 13521  13551             Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
 13522  13552             sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
 13523  13553           }
 13524  13554         }
................................................................................
 24968  24998   ** Default permissions when creating a new file
 24969  24999   */
 24970  25000   #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
 24971  25001   # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
 24972  25002   #endif
 24973  25003   
 24974  25004   /*
 24975         - ** Default permissions when creating auto proxy dir
 24976         - */
        25005  +** Default permissions when creating auto proxy dir
        25006  +*/
 24977  25007   #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
 24978  25008   # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
 24979  25009   #endif
 24980  25010   
 24981  25011   /*
 24982  25012   ** Maximum supported path-length.
 24983  25013   */
................................................................................
 25523  25553       if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
 25524  25554     }
 25525  25555     return 0;
 25526  25556   }
 25527  25557   
 25528  25558   /*
 25529  25559   ** Invoke open().  Do so multiple times, until it either succeeds or
 25530         -** files for some reason other than EINTR.
        25560  +** fails for some reason other than EINTR.
 25531  25561   **
 25532  25562   ** If the file creation mode "m" is 0 then set it to the default for
 25533  25563   ** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
 25534  25564   ** 0644) as modified by the system umask.  If m is not 0, then
 25535  25565   ** make the file creation mode be exactly m ignoring the umask.
 25536  25566   **
 25537  25567   ** The m parameter will be non-zero only when creating -wal, -journal,
................................................................................
 25539  25569   ** permissions as their original database, unadulterated by the umask.
 25540  25570   ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
 25541  25571   ** transaction crashes and leaves behind hot journals, then any
 25542  25572   ** process that is able to write to the database will also be able to
 25543  25573   ** recover the hot journals.
 25544  25574   */
 25545  25575   static int robust_open(const char *z, int f, mode_t m){
 25546         -  int rc;
        25576  +  int fd;
 25547  25577     mode_t m2;
 25548  25578     mode_t origM = 0;
 25549  25579     if( m==0 ){
 25550  25580       m2 = SQLITE_DEFAULT_FILE_PERMISSIONS;
 25551  25581     }else{
 25552  25582       m2 = m;
 25553  25583       origM = osUmask(0);
 25554  25584     }
 25555         -  do{ rc = osOpen(z,f,m2); }while( rc<0 && errno==EINTR );
        25585  +  do{
        25586  +#if defined(O_CLOEXEC)
        25587  +    fd = osOpen(z,f|O_CLOEXEC,m2);
        25588  +#else
        25589  +    fd = osOpen(z,f,m2);
        25590  +#endif
        25591  +  }while( fd<0 && errno==EINTR );
 25556  25592     if( m ){
 25557  25593       osUmask(origM);
 25558  25594     }
 25559         -  return rc;
        25595  +#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
        25596  +  if( fd>=0 ) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
        25597  +#endif
        25598  +  return fd;
 25560  25599   }
 25561  25600   
 25562  25601   /*
 25563  25602   ** Helper functions to obtain and relinquish the global mutex. The
 25564  25603   ** global mutex is used to protect the unixInodeInfo and
 25565  25604   ** vxworksFileId objects used by this file, all of which may be 
 25566  25605   ** shared by multiple threads.
................................................................................
 28347  28386   
 28348  28387     sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
 28349  28388     for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
 28350  28389     if( ii>0 ){
 28351  28390       zDirname[ii] = '\0';
 28352  28391       fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
 28353  28392       if( fd>=0 ){
 28354         -#ifdef FD_CLOEXEC
 28355         -      osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
 28356         -#endif
 28357  28393         OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
 28358  28394       }
 28359  28395     }
 28360  28396     *pFd = fd;
 28361  28397     return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
 28362  28398   }
 28363  28399   
................................................................................
 28432  28468     SimulateIOError( return SQLITE_IOERR_TRUNCATE );
 28433  28469   
 28434  28470     /* If the user has configured a chunk-size for this file, truncate the
 28435  28471     ** file so that it consists of an integer number of chunks (i.e. the
 28436  28472     ** actual file size after the operation may be larger than the requested
 28437  28473     ** size).
 28438  28474     */
 28439         -  if( pFile->szChunk ){
        28475  +  if( pFile->szChunk>0 ){
 28440  28476       nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 28441  28477     }
 28442  28478   
 28443  28479     rc = robust_ftruncate(pFile->h, (off_t)nByte);
 28444  28480     if( rc ){
 28445  28481       pFile->lastErrno = errno;
 28446  28482       return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
................................................................................
 30194  30230     }
 30195  30231   #if SQLITE_ENABLE_LOCKING_STYLE
 30196  30232     else{
 30197  30233       p->openFlags = openFlags;
 30198  30234     }
 30199  30235   #endif
 30200  30236   
 30201         -#ifdef FD_CLOEXEC
 30202         -  osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
 30203         -#endif
 30204         -
 30205  30237     noLock = eType!=SQLITE_OPEN_MAIN_DB;
 30206  30238   
 30207  30239     
 30208  30240   #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 30209  30241     if( fstatfs(fd, &fsInfo) == -1 ){
 30210  30242       ((unixFile*)pFile)->lastErrno = errno;
 30211  30243       robust_close(p, fd, __LINE__);
................................................................................
 33725  33757       ){
 33726  33758          osSleep(100);  /* Wait a little before trying again */
 33727  33759       }
 33728  33760       sqlite3_free(pFile->zDeleteOnClose);
 33729  33761     }
 33730  33762   #endif
 33731  33763     OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
        33764  +  if( rc ){
        33765  +    pFile->h = NULL;
        33766  +  }
 33732  33767     OpenCounter(-1);
 33733  33768     return rc ? SQLITE_OK
 33734  33769               : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
 33735  33770                             "winClose", pFile->zPath);
 33736  33771   }
 33737  33772   
 33738  33773   /*
................................................................................
 33742  33777   */
 33743  33778   static int winRead(
 33744  33779     sqlite3_file *id,          /* File to read from */
 33745  33780     void *pBuf,                /* Write content into this buffer */
 33746  33781     int amt,                   /* Number of bytes to read */
 33747  33782     sqlite3_int64 offset       /* Begin reading at this offset */
 33748  33783   ){
        33784  +#if !SQLITE_OS_WINCE
        33785  +  OVERLAPPED overlapped;          /* The offset for ReadFile. */
        33786  +#endif
 33749  33787     winFile *pFile = (winFile*)id;  /* file handle */
 33750  33788     DWORD nRead;                    /* Number of bytes actually read from file */
 33751  33789     int nRetry = 0;                 /* Number of retrys */
 33752  33790   
 33753  33791     assert( id!=0 );
 33754  33792     SimulateIOError(return SQLITE_IOERR_READ);
 33755  33793     OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
 33756  33794   
        33795  +#if SQLITE_OS_WINCE
 33757  33796     if( seekWinFile(pFile, offset) ){
 33758  33797       return SQLITE_FULL;
 33759  33798     }
 33760  33799     while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
        33800  +#else
        33801  +  memset(&overlapped, 0, sizeof(OVERLAPPED));
        33802  +  overlapped.Offset = (LONG)(offset & 0xffffffff);
        33803  +  overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
        33804  +  while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
        33805  +         osGetLastError()!=ERROR_HANDLE_EOF ){
        33806  +#endif
 33761  33807       DWORD lastErrno;
 33762  33808       if( retryIoerr(&nRetry, &lastErrno) ) continue;
 33763  33809       pFile->lastErrno = lastErrno;
 33764  33810       return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
 33765  33811                "winRead", pFile->zPath);
 33766  33812     }
 33767  33813     logIoerr(nRetry);
................................................................................
 33780  33826   */
 33781  33827   static int winWrite(
 33782  33828     sqlite3_file *id,               /* File to write into */
 33783  33829     const void *pBuf,               /* The bytes to be written */
 33784  33830     int amt,                        /* Number of bytes to write */
 33785  33831     sqlite3_int64 offset            /* Offset into the file to begin writing at */
 33786  33832   ){
 33787         -  int rc;                         /* True if error has occured, else false */
        33833  +  int rc = 0;                     /* True if error has occured, else false */
 33788  33834     winFile *pFile = (winFile*)id;  /* File handle */
 33789  33835     int nRetry = 0;                 /* Number of retries */
 33790  33836   
 33791  33837     assert( amt>0 );
 33792  33838     assert( pFile );
 33793  33839     SimulateIOError(return SQLITE_IOERR_WRITE);
 33794  33840     SimulateDiskfullError(return SQLITE_FULL);
 33795  33841   
 33796  33842     OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
 33797  33843   
        33844  +#if SQLITE_OS_WINCE
 33798  33845     rc = seekWinFile(pFile, offset);
 33799  33846     if( rc==0 ){
        33847  +#else
        33848  +  {
        33849  +#endif
        33850  +#if !SQLITE_OS_WINCE
        33851  +    OVERLAPPED overlapped;        /* The offset for WriteFile. */
        33852  +#endif
 33800  33853       u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
 33801  33854       int nRem = amt;               /* Number of bytes yet to be written */
 33802  33855       DWORD nWrite;                 /* Bytes written by each WriteFile() call */
 33803  33856       DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
        33857  +
        33858  +#if !SQLITE_OS_WINCE
        33859  +    memset(&overlapped, 0, sizeof(OVERLAPPED));
        33860  +    overlapped.Offset = (LONG)(offset & 0xffffffff);
        33861  +    overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
        33862  +#endif
 33804  33863   
 33805  33864       while( nRem>0 ){
        33865  +#if SQLITE_OS_WINCE
 33806  33866         if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
        33867  +#else
        33868  +      if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
        33869  +#endif
 33807  33870           if( retryIoerr(&nRetry, &lastErrno) ) continue;
 33808  33871           break;
 33809  33872         }
 33810         -      if( nWrite<=0 ) break;
        33873  +      if( nWrite<=0 ){
        33874  +        lastErrno = osGetLastError();
        33875  +        break;
        33876  +      }
        33877  +#if !SQLITE_OS_WINCE
        33878  +      offset += nWrite;
        33879  +      overlapped.Offset = (LONG)(offset & 0xffffffff);
        33880  +      overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
        33881  +#endif
 33811  33882         aRem += nWrite;
 33812  33883         nRem -= nWrite;
 33813  33884       }
 33814  33885       if( nRem>0 ){
 33815  33886         pFile->lastErrno = lastErrno;
 33816  33887         rc = 1;
 33817  33888       }
................................................................................
 35100  35171     }
 35101  35172     zBuf[j] = 0;
 35102  35173     zBuf[j+1] = 0;
 35103  35174   
 35104  35175     OSTRACE(("TEMP FILENAME: %s\n", zBuf));
 35105  35176     return SQLITE_OK; 
 35106  35177   }
        35178  +
        35179  +/*
        35180  +** Return TRUE if the named file is really a directory.  Return false if
        35181  +** it is something other than a directory, or if there is any kind of memory
        35182  +** allocation failure.
        35183  +*/
        35184  +static int winIsDir(const void *zConverted){
        35185  +  DWORD attr;
        35186  +  int rc = 0;
        35187  +  DWORD lastErrno;
        35188  +
        35189  +  if( isNT() ){
        35190  +    int cnt = 0;
        35191  +    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
        35192  +    memset(&sAttrData, 0, sizeof(sAttrData));
        35193  +    while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
        35194  +                             GetFileExInfoStandard,
        35195  +                             &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
        35196  +    if( !rc ){
        35197  +      return 0; /* Invalid name? */
        35198  +    }
        35199  +    attr = sAttrData.dwFileAttributes;
        35200  +#if SQLITE_OS_WINCE==0
        35201  +  }else{
        35202  +    attr = osGetFileAttributesA((char*)zConverted);
        35203  +#endif
        35204  +  }
        35205  +  return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
        35206  +}
 35107  35207   
 35108  35208   /*
 35109  35209   ** Open a file.
 35110  35210   */
 35111  35211   static int winOpen(
 35112  35212     sqlite3_vfs *pVfs,        /* Not used */
 35113  35213     const char *zName,        /* Name of the file (UTF-8) */
................................................................................
 35206  35306           zUtf8Name[strlen(zUtf8Name)+1]==0 );
 35207  35307   
 35208  35308     /* Convert the filename to the system encoding. */
 35209  35309     zConverted = convertUtf8Filename(zUtf8Name);
 35210  35310     if( zConverted==0 ){
 35211  35311       return SQLITE_IOERR_NOMEM;
 35212  35312     }
        35313  +
        35314  +  if( winIsDir(zConverted) ){
        35315  +    sqlite3_free(zConverted);
        35316  +    return SQLITE_CANTOPEN_ISDIR;
        35317  +  }
 35213  35318   
 35214  35319     if( isReadWrite ){
 35215  35320       dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
 35216  35321     }else{
 35217  35322       dwDesiredAccess = GENERIC_READ;
 35218  35323     }
 35219  35324   
................................................................................
 35256  35361     if( isNT() ){
 35257  35362       while( (h = osCreateFileW((LPCWSTR)zConverted,
 35258  35363                                 dwDesiredAccess,
 35259  35364                                 dwShareMode, NULL,
 35260  35365                                 dwCreationDisposition,
 35261  35366                                 dwFlagsAndAttributes,
 35262  35367                                 NULL))==INVALID_HANDLE_VALUE &&
 35263         -                              retryIoerr(&cnt, &lastErrno) ){}
 35264         -/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
 35265         -** Since the ANSI version of these Windows API do not exist for WINCE,
 35266         -** it's important to not reference them for WINCE builds.
 35267         -*/
        35368  +                              retryIoerr(&cnt, &lastErrno) ){
        35369  +               /* Noop */
        35370  +    }
 35268  35371   #if SQLITE_OS_WINCE==0
 35269  35372     }else{
 35270  35373       while( (h = osCreateFileA((LPCSTR)zConverted,
 35271  35374                                 dwDesiredAccess,
 35272  35375                                 dwShareMode, NULL,
 35273  35376                                 dwCreationDisposition,
 35274  35377                                 dwFlagsAndAttributes,
 35275  35378                                 NULL))==INVALID_HANDLE_VALUE &&
 35276         -                              retryIoerr(&cnt, &lastErrno) ){}
        35379  +                              retryIoerr(&cnt, &lastErrno) ){
        35380  +               /* Noop */
        35381  +    }
 35277  35382   #endif
 35278  35383     }
 35279  35384   
 35280  35385     logIoerr(cnt);
 35281  35386   
 35282  35387     OSTRACE(("OPEN %d %s 0x%lx %s\n", 
 35283  35388              h, zName, dwDesiredAccess, 
................................................................................
 35349  35454   static int winDelete(
 35350  35455     sqlite3_vfs *pVfs,          /* Not used on win32 */
 35351  35456     const char *zFilename,      /* Name of file to delete */
 35352  35457     int syncDir                 /* Not used on win32 */
 35353  35458   ){
 35354  35459     int cnt = 0;
 35355  35460     int rc;
        35461  +  DWORD attr;
 35356  35462     DWORD lastErrno;
 35357  35463     void *zConverted;
 35358  35464     UNUSED_PARAMETER(pVfs);
 35359  35465     UNUSED_PARAMETER(syncDir);
 35360  35466   
 35361  35467     SimulateIOError(return SQLITE_IOERR_DELETE);
 35362  35468     zConverted = convertUtf8Filename(zFilename);
 35363  35469     if( zConverted==0 ){
 35364  35470       return SQLITE_IOERR_NOMEM;
 35365  35471     }
 35366  35472     if( isNT() ){
 35367         -    rc = 1;
 35368         -    while( osGetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
 35369         -         (rc = osDeleteFileW(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
 35370         -    rc = rc ? SQLITE_OK : SQLITE_ERROR;
        35473  +    do {
        35474  +      attr = osGetFileAttributesW(zConverted);
        35475  +      if ( attr==INVALID_FILE_ATTRIBUTES ){
        35476  +        rc = SQLITE_OK; /* Already gone? */
        35477  +        break;
        35478  +      }
        35479  +      if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
        35480  +        rc = SQLITE_ERROR; /* Files only. */
        35481  +        break;
        35482  +      }
        35483  +      if ( osDeleteFileW(zConverted) ){
        35484  +        rc = SQLITE_OK; /* Deleted OK. */
        35485  +        break;
        35486  +      }
        35487  +      if ( !retryIoerr(&cnt, &lastErrno) ){
        35488  +        rc = SQLITE_ERROR; /* No more retries. */
        35489  +        break;
        35490  +      }
        35491  +    } while(1);
 35371  35492   /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
 35372  35493   ** Since the ANSI version of these Windows API do not exist for WINCE,
 35373  35494   ** it's important to not reference them for WINCE builds.
 35374  35495   */
 35375  35496   #if SQLITE_OS_WINCE==0
 35376  35497     }else{
 35377         -    rc = 1;
 35378         -    while( osGetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES &&
 35379         -         (rc = osDeleteFileA(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
 35380         -    rc = rc ? SQLITE_OK : SQLITE_ERROR;
        35498  +    do {
        35499  +      attr = osGetFileAttributesA(zConverted);
        35500  +      if ( attr==INVALID_FILE_ATTRIBUTES ){
        35501  +        rc = SQLITE_OK; /* Already gone? */
        35502  +        break;
        35503  +      }
        35504  +      if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
        35505  +        rc = SQLITE_ERROR; /* Files only. */
        35506  +        break;
        35507  +      }
        35508  +      if ( osDeleteFileA(zConverted) ){
        35509  +        rc = SQLITE_OK; /* Deleted OK. */
        35510  +        break;
        35511  +      }
        35512  +      if ( !retryIoerr(&cnt, &lastErrno) ){
        35513  +        rc = SQLITE_ERROR; /* No more retries. */
        35514  +        break;
        35515  +      }
        35516  +    } while(1);
 35381  35517   #endif
 35382  35518     }
 35383  35519     if( rc ){
 35384  35520       rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
 35385  35521                "winDelete", zFilename);
 35386  35522     }else{
 35387  35523       logIoerr(cnt);
................................................................................
 37935  38071   ** The number of rowset entries per allocation chunk.
 37936  38072   */
 37937  38073   #define ROWSET_ENTRY_PER_CHUNK  \
 37938  38074                          ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
 37939  38075   
 37940  38076   /*
 37941  38077   ** Each entry in a RowSet is an instance of the following object.
        38078  +**
        38079  +** This same object is reused to store a linked list of trees of RowSetEntry
        38080  +** objects.  In that alternative use, pRight points to the next entry
        38081  +** in the list, pLeft points to the tree, and v is unused.  The
        38082  +** RowSet.pForest value points to the head of this forest list.
 37942  38083   */
 37943  38084   struct RowSetEntry {            
 37944  38085     i64 v;                        /* ROWID value for this entry */
 37945  38086     struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
 37946  38087     struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
 37947  38088   };
 37948  38089   
................................................................................
 37964  38105   */
 37965  38106   struct RowSet {
 37966  38107     struct RowSetChunk *pChunk;    /* List of all chunk allocations */
 37967  38108     sqlite3 *db;                   /* The database connection */
 37968  38109     struct RowSetEntry *pEntry;    /* List of entries using pRight */
 37969  38110     struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
 37970  38111     struct RowSetEntry *pFresh;    /* Source of new entry objects */
 37971         -  struct RowSetEntry *pTree;     /* Binary tree of entries */
        38112  +  struct RowSetEntry *pForest;   /* List of binary trees of entries */
 37972  38113     u16 nFresh;                    /* Number of objects on pFresh */
 37973         -  u8 isSorted;                   /* True if pEntry is sorted */
        38114  +  u8 rsFlags;                    /* Various flags */
 37974  38115     u8 iBatch;                     /* Current insert batch */
 37975  38116   };
 37976  38117   
        38118  +/*
        38119  +** Allowed values for RowSet.rsFlags
        38120  +*/
        38121  +#define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
        38122  +#define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
        38123  +
 37977  38124   /*
 37978  38125   ** Turn bulk memory into a RowSet object.  N bytes of memory
 37979  38126   ** are available at pSpace.  The db pointer is used as a memory context
 37980  38127   ** for any subsequent allocations that need to occur.
 37981  38128   ** Return a pointer to the new RowSet object.
 37982  38129   **
 37983  38130   ** It must be the case that N is sufficient to make a Rowset.  If not
................................................................................
 37990  38137     RowSet *p;
 37991  38138     assert( N >= ROUND8(sizeof(*p)) );
 37992  38139     p = pSpace;
 37993  38140     p->pChunk = 0;
 37994  38141     p->db = db;
 37995  38142     p->pEntry = 0;
 37996  38143     p->pLast = 0;
 37997         -  p->pTree = 0;
        38144  +  p->pForest = 0;
 37998  38145     p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
 37999  38146     p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
 38000         -  p->isSorted = 1;
        38147  +  p->rsFlags = ROWSET_SORTED;
 38001  38148     p->iBatch = 0;
 38002  38149     return p;
 38003  38150   }
 38004  38151   
 38005  38152   /*
 38006  38153   ** Deallocate all chunks from a RowSet.  This frees all memory that
 38007  38154   ** the RowSet has allocated over its lifetime.  This routine is
................................................................................
 38013  38160       pNextChunk = pChunk->pNextChunk;
 38014  38161       sqlite3DbFree(p->db, pChunk);
 38015  38162     }
 38016  38163     p->pChunk = 0;
 38017  38164     p->nFresh = 0;
 38018  38165     p->pEntry = 0;
 38019  38166     p->pLast = 0;
 38020         -  p->pTree = 0;
 38021         -  p->isSorted = 1;
        38167  +  p->pForest = 0;
        38168  +  p->rsFlags = ROWSET_SORTED;
        38169  +}
        38170  +
        38171  +/*
        38172  +** Allocate a new RowSetEntry object that is associated with the
        38173  +** given RowSet.  Return a pointer to the new and completely uninitialized
        38174  +** objected.
        38175  +**
        38176  +** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
        38177  +** routine returns NULL.
        38178  +*/
        38179  +static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
        38180  +  assert( p!=0 );
        38181  +  if( p->nFresh==0 ){
        38182  +    struct RowSetChunk *pNew;
        38183  +    pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
        38184  +    if( pNew==0 ){
        38185  +      return 0;
        38186  +    }
        38187  +    pNew->pNextChunk = p->pChunk;
        38188  +    p->pChunk = pNew;
        38189  +    p->pFresh = pNew->aEntry;
        38190  +    p->nFresh = ROWSET_ENTRY_PER_CHUNK;
        38191  +  }
        38192  +  p->nFresh--;
        38193  +  return p->pFresh++;
 38022  38194   }
 38023  38195   
 38024  38196   /*
 38025  38197   ** Insert a new value into a RowSet.
 38026  38198   **
 38027  38199   ** The mallocFailed flag of the database connection is set if a
 38028  38200   ** memory allocation fails.
 38029  38201   */
 38030  38202   SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
 38031  38203     struct RowSetEntry *pEntry;  /* The new entry */
 38032  38204     struct RowSetEntry *pLast;   /* The last prior entry */
 38033         -  assert( p!=0 );
 38034         -  if( p->nFresh==0 ){
 38035         -    struct RowSetChunk *pNew;
 38036         -    pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
 38037         -    if( pNew==0 ){
 38038         -      return;
 38039         -    }
 38040         -    pNew->pNextChunk = p->pChunk;
 38041         -    p->pChunk = pNew;
 38042         -    p->pFresh = pNew->aEntry;
 38043         -    p->nFresh = ROWSET_ENTRY_PER_CHUNK;
 38044         -  }
 38045         -  pEntry = p->pFresh++;
 38046         -  p->nFresh--;
        38205  +
        38206  +  /* This routine is never called after sqlite3RowSetNext() */
        38207  +  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
        38208  +
        38209  +  pEntry = rowSetEntryAlloc(p);
        38210  +  if( pEntry==0 ) return;
 38047  38211     pEntry->v = rowid;
 38048  38212     pEntry->pRight = 0;
 38049  38213     pLast = p->pLast;
 38050  38214     if( pLast ){
 38051         -    if( p->isSorted && rowid<=pLast->v ){
 38052         -      p->isSorted = 0;
        38215  +    if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
        38216  +      p->rsFlags &= ~ROWSET_SORTED;
 38053  38217       }
 38054  38218       pLast->pRight = pEntry;
 38055  38219     }else{
 38056         -    assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
 38057  38220       p->pEntry = pEntry;
 38058  38221     }
 38059  38222     p->pLast = pEntry;
 38060  38223   }
 38061  38224   
 38062  38225   /*
 38063  38226   ** Merge two lists of RowSetEntry objects.  Remove duplicates.
 38064  38227   **
 38065  38228   ** The input lists are connected via pRight pointers and are 
 38066  38229   ** assumed to each already be in sorted order.
 38067  38230   */
 38068         -static struct RowSetEntry *rowSetMerge(
        38231  +static struct RowSetEntry *rowSetEntryMerge(
 38069  38232     struct RowSetEntry *pA,    /* First sorted list to be merged */
 38070  38233     struct RowSetEntry *pB     /* Second sorted list to be merged */
 38071  38234   ){
 38072  38235     struct RowSetEntry head;
 38073  38236     struct RowSetEntry *pTail;
 38074  38237   
 38075  38238     pTail = &head;
................................................................................
 38095  38258       assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
 38096  38259       pTail->pRight = pB;
 38097  38260     }
 38098  38261     return head.pRight;
 38099  38262   }
 38100  38263   
 38101  38264   /*
 38102         -** Sort all elements on the pEntry list of the RowSet into ascending order.
        38265  +** Sort all elements on the list of RowSetEntry objects into order of
        38266  +** increasing v.
 38103  38267   */ 
 38104         -static void rowSetSort(RowSet *p){
        38268  +static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
 38105  38269     unsigned int i;
 38106         -  struct RowSetEntry *pEntry;
 38107         -  struct RowSetEntry *aBucket[40];
        38270  +  struct RowSetEntry *pNext, *aBucket[40];
 38108  38271   
 38109         -  assert( p->isSorted==0 );
 38110  38272     memset(aBucket, 0, sizeof(aBucket));
 38111         -  while( p->pEntry ){
 38112         -    pEntry = p->pEntry;
 38113         -    p->pEntry = pEntry->pRight;
 38114         -    pEntry->pRight = 0;
        38273  +  while( pIn ){
        38274  +    pNext = pIn->pRight;
        38275  +    pIn->pRight = 0;
 38115  38276       for(i=0; aBucket[i]; i++){
 38116         -      pEntry = rowSetMerge(aBucket[i], pEntry);
        38277  +      pIn = rowSetEntryMerge(aBucket[i], pIn);
 38117  38278         aBucket[i] = 0;
 38118  38279       }
 38119         -    aBucket[i] = pEntry;
        38280  +    aBucket[i] = pIn;
        38281  +    pIn = pNext;
 38120  38282     }
 38121         -  pEntry = 0;
        38283  +  pIn = 0;
 38122  38284     for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
 38123         -    pEntry = rowSetMerge(pEntry, aBucket[i]);
        38285  +    pIn = rowSetEntryMerge(pIn, aBucket[i]);
 38124  38286     }
 38125         -  p->pEntry = pEntry;
 38126         -  p->pLast = 0;
 38127         -  p->isSorted = 1;
        38287  +  return pIn;
 38128  38288   }
 38129  38289   
 38130  38290   
 38131  38291   /*
 38132  38292   ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
 38133  38293   ** Convert this tree into a linked list connected by the pRight pointers
 38134  38294   ** and return pointers to the first and last elements of the new list.
................................................................................
 38214  38374       p->pLeft = pLeft;
 38215  38375       p->pRight = rowSetNDeepTree(&pList, iDepth);
 38216  38376     }
 38217  38377     return p;
 38218  38378   }
 38219  38379   
 38220  38380   /*
 38221         -** Convert the list in p->pEntry into a sorted list if it is not
 38222         -** sorted already.  If there is a binary tree on p->pTree, then
 38223         -** convert it into a list too and merge it into the p->pEntry list.
        38381  +** Take all the entries on p->pEntry and on the trees in p->pForest and
        38382  +** sort them all together into one big ordered list on p->pEntry.
        38383  +**
        38384  +** This routine should only be called once in the life of a RowSet.
 38224  38385   */
 38225  38386   static void rowSetToList(RowSet *p){
 38226         -  if( !p->isSorted ){
 38227         -    rowSetSort(p);
        38387  +
        38388  +  /* This routine is called only once */
        38389  +  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
        38390  +
        38391  +  if( (p->rsFlags & ROWSET_SORTED)==0 ){
        38392  +    p->pEntry = rowSetEntrySort(p->pEntry);
 38228  38393     }
 38229         -  if( p->pTree ){
 38230         -    struct RowSetEntry *pHead, *pTail;
 38231         -    rowSetTreeToList(p->pTree, &pHead, &pTail);
 38232         -    p->pTree = 0;
 38233         -    p->pEntry = rowSetMerge(p->pEntry, pHead);
        38394  +
        38395  +  /* While this module could theoretically support it, sqlite3RowSetNext()
        38396  +  ** is never called after sqlite3RowSetText() for the same RowSet.  So
        38397  +  ** there is never a forest to deal with.  Should this change, simply
        38398  +  ** remove the assert() and the #if 0. */
        38399  +  assert( p->pForest==0 );
        38400  +#if 0
        38401  +  while( p->pForest ){
        38402  +    struct RowSetEntry *pTree = p->pForest->pLeft;
        38403  +    if( pTree ){
        38404  +      struct RowSetEntry *pHead, *pTail;
        38405  +      rowSetTreeToList(pTree, &pHead, &pTail);
        38406  +      p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
        38407  +    }
        38408  +    p->pForest = p->pForest->pRight;
 38234  38409     }
        38410  +#endif
        38411  +  p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
 38235  38412   }
 38236  38413   
 38237  38414   /*
 38238  38415   ** Extract the smallest element from the RowSet.
 38239  38416   ** Write the element into *pRowid.  Return 1 on success.  Return
 38240  38417   ** 0 if the RowSet is already empty.
 38241  38418   **
 38242  38419   ** After this routine has been called, the sqlite3RowSetInsert()
 38243  38420   ** routine may not be called again.  
 38244  38421   */
 38245  38422   SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
 38246         -  rowSetToList(p);
        38423  +  assert( p!=0 );
        38424  +
        38425  +  /* Merge the forest into a single sorted list on first call */
        38426  +  if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
        38427  +
        38428  +  /* Return the next entry on the list */
 38247  38429     if( p->pEntry ){
 38248  38430       *pRowid = p->pEntry->v;
 38249  38431       p->pEntry = p->pEntry->pRight;
 38250  38432       if( p->pEntry==0 ){
 38251  38433         sqlite3RowSetClear(p);
 38252  38434       }
 38253  38435       return 1;
................................................................................
 38255  38437       return 0;
 38256  38438     }
 38257  38439   }
 38258  38440   
 38259  38441   /*
 38260  38442   ** Check to see if element iRowid was inserted into the the rowset as
 38261  38443   ** part of any insert batch prior to iBatch.  Return 1 or 0.
        38444  +**
        38445  +** If this is the first test of a new batch and if there exist entires
        38446  +** on pRowSet->pEntry, then sort those entires into the forest at
        38447  +** pRowSet->pForest so that they can be tested.
 38262  38448   */
 38263  38449   SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
 38264         -  struct RowSetEntry *p;
        38450  +  struct RowSetEntry *p, *pTree;
        38451  +
        38452  +  /* This routine is never called after sqlite3RowSetNext() */
        38453  +  assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
        38454  +
        38455  +  /* Sort entries into the forest on the first test of a new batch 
        38456  +  */
 38265  38457     if( iBatch!=pRowSet->iBatch ){
 38266         -    if( pRowSet->pEntry ){
 38267         -      rowSetToList(pRowSet);
 38268         -      pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
        38458  +    p = pRowSet->pEntry;
        38459  +    if( p ){
        38460  +      struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
        38461  +      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
        38462  +        p = rowSetEntrySort(p);
        38463  +      }
        38464  +      for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
        38465  +        ppPrevTree = &pTree->pRight;
        38466  +        if( pTree->pLeft==0 ){
        38467  +          pTree->pLeft = rowSetListToTree(p);
        38468  +          break;
        38469  +        }else{
        38470  +          struct RowSetEntry *pAux, *pTail;
        38471  +          rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
        38472  +          pTree->pLeft = 0;
        38473  +          p = rowSetEntryMerge(pAux, p);
        38474  +        }
        38475  +      }
        38476  +      if( pTree==0 ){
        38477  +        *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
        38478  +        if( pTree ){
        38479  +          pTree->v = 0;
        38480  +          pTree->pRight = 0;
        38481  +          pTree->pLeft = rowSetListToTree(p);
        38482  +        }
        38483  +      }
 38269  38484         pRowSet->pEntry = 0;
 38270  38485         pRowSet->pLast = 0;
        38486  +      pRowSet->rsFlags |= ROWSET_SORTED;
 38271  38487       }
 38272  38488       pRowSet->iBatch = iBatch;
 38273  38489     }
 38274         -  p = pRowSet->pTree;
 38275         -  while( p ){
 38276         -    if( p->v<iRowid ){
 38277         -      p = p->pRight;
 38278         -    }else if( p->v>iRowid ){
 38279         -      p = p->pLeft;
 38280         -    }else{
 38281         -      return 1;
        38490  +
        38491  +  /* Test to see if the iRowid value appears anywhere in the forest.
        38492  +  ** Return 1 if it does and 0 if not.
        38493  +  */
        38494  +  for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
        38495  +    p = pTree->pLeft;
        38496  +    while( p ){
        38497  +      if( p->v<iRowid ){
        38498  +        p = p->pRight;
        38499  +      }else if( p->v>iRowid ){
        38500  +        p = p->pLeft;
        38501  +      }else{
        38502  +        return 1;
        38503  +      }
 38282  38504       }
 38283  38505     }
 38284  38506     return 0;
 38285  38507   }
 38286  38508   
 38287  38509   /************** End of rowset.c **********************************************/
 38288  38510   /************** Begin file pager.c *******************************************/
................................................................................
 39092  39314     int pageSize;               /* Number of bytes in a page */
 39093  39315     Pgno mxPgno;                /* Maximum allowed size of the database */
 39094  39316     i64 journalSizeLimit;       /* Size limit for persistent journal files */
 39095  39317     char *zFilename;            /* Name of the database file */
 39096  39318     char *zJournal;             /* Name of the journal file */
 39097  39319     int (*xBusyHandler)(void*); /* Function to call when busy */
 39098  39320     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 39099         -  int nHit, nMiss;            /* Total cache hits and misses */
        39321  +  int aStat[3];               /* Total cache hits, misses and writes */
 39100  39322   #ifdef SQLITE_TEST
 39101         -  int nRead, nWrite;          /* Database pages read/written */
        39323  +  int nRead;                  /* Database pages read */
 39102  39324   #endif
 39103  39325     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 39104  39326   #ifdef SQLITE_HAS_CODEC
 39105  39327     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
 39106  39328     void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
 39107  39329     void (*xCodecFree)(void*);             /* Destructor for the codec */
 39108  39330     void *pCodec;               /* First argument to xCodec... methods */
................................................................................
 39111  39333     PCache *pPCache;            /* Pointer to page cache object */
 39112  39334   #ifndef SQLITE_OMIT_WAL
 39113  39335     Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
 39114  39336     char *zWal;                 /* File name for write-ahead log */
 39115  39337   #endif
 39116  39338   };
 39117  39339   
        39340  +/*
        39341  +** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
        39342  +** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
        39343  +** or CACHE_WRITE to sqlite3_db_status().
        39344  +*/
        39345  +#define PAGER_STAT_HIT   0
        39346  +#define PAGER_STAT_MISS  1
        39347  +#define PAGER_STAT_WRITE 2
        39348  +
 39118  39349   /*
 39119  39350   ** The following global variables hold counters used for
 39120  39351   ** testing purposes only.  These variables do not exist in
 39121  39352   ** a non-testing build.  These variables are not thread-safe.
 39122  39353   */
 39123  39354   #ifdef SQLITE_TEST
 39124  39355   SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
................................................................................
 41393  41624   static int pagerWalFrames(
 41394  41625     Pager *pPager,                  /* Pager object */
 41395  41626     PgHdr *pList,                   /* List of frames to log */
 41396  41627     Pgno nTruncate,                 /* Database size after this commit */
 41397  41628     int isCommit                    /* True if this is a commit */
 41398  41629   ){
 41399  41630     int rc;                         /* Return code */
        41631  +  int nList;                      /* Number of pages in pList */
 41400  41632   #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
 41401  41633     PgHdr *p;                       /* For looping over pages */
 41402  41634   #endif
 41403  41635   
 41404  41636     assert( pPager->pWal );
 41405  41637     assert( pList );
 41406  41638   #ifdef SQLITE_DEBUG
 41407  41639     /* Verify that the page list is in accending order */
 41408  41640     for(p=pList; p && p->pDirty; p=p->pDirty){
 41409  41641       assert( p->pgno < p->pDirty->pgno );
 41410  41642     }
 41411  41643   #endif
 41412  41644   
        41645  +  assert( pList->pDirty==0 || isCommit );
 41413  41646     if( isCommit ){
 41414  41647       /* If a WAL transaction is being committed, there is no point in writing
 41415  41648       ** any pages with page numbers greater than nTruncate into the WAL file.
 41416  41649       ** They will never be read by any client. So remove them from the pDirty
 41417  41650       ** list here. */
 41418  41651       PgHdr *p;
 41419  41652       PgHdr **ppNext = &pList;
 41420         -    for(p=pList; (*ppNext = p); p=p->pDirty){
 41421         -      if( p->pgno<=nTruncate ) ppNext = &p->pDirty;
        41653  +    nList = 0;
        41654  +    for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
        41655  +      if( p->pgno<=nTruncate ){
        41656  +        ppNext = &p->pDirty;
        41657  +        nList++;
        41658  +      }
 41422  41659       }
 41423  41660       assert( pList );
        41661  +  }else{
        41662  +    nList = 1;
 41424  41663     }
        41664  +  pPager->aStat[PAGER_STAT_WRITE] += nList;
 41425  41665   
 41426  41666     if( pList->pgno==1 ) pager_write_changecounter(pList);
 41427  41667     rc = sqlite3WalFrames(pPager->pWal, 
 41428  41668         pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
 41429  41669     );
 41430  41670     if( rc==SQLITE_OK && pPager->pBackup ){
 41431  41671       PgHdr *p;
................................................................................
 42485  42725         */
 42486  42726         if( pgno==1 ){
 42487  42727           memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
 42488  42728         }
 42489  42729         if( pgno>pPager->dbFileSize ){
 42490  42730           pPager->dbFileSize = pgno;
 42491  42731         }
        42732  +      pPager->aStat[PAGER_STAT_WRITE]++;
 42492  42733   
 42493  42734         /* Update any backup objects copying the contents of this pager. */
 42494  42735         sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
 42495  42736   
 42496  42737         PAGERTRACE(("STORE %d page %d hash(%08x)\n",
 42497  42738                      PAGERID(pPager), pgno, pager_pagehash(pList)));
 42498  42739         IOTRACE(("PGOUT %p %d\n", pPager, pgno));
 42499  42740         PAGER_INCR(sqlite3_pager_writedb_count);
 42500         -      PAGER_INCR(pPager->nWrite);
 42501  42741       }else{
 42502  42742         PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
 42503  42743       }
 42504  42744       pager_set_pagehash(pList);
 42505  42745       pList = pList->pDirty;
 42506  42746     }
 42507  42747   
................................................................................
 43451  43691     assert( (*ppPage)->pgno==pgno );
 43452  43692     assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
 43453  43693   
 43454  43694     if( (*ppPage)->pPager && !noContent ){
 43455  43695       /* In this case the pcache already contains an initialized copy of
 43456  43696       ** the page. Return without further ado.  */
 43457  43697       assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
 43458         -    pPager->nHit++;
        43698  +    pPager->aStat[PAGER_STAT_HIT]++;
 43459  43699       return SQLITE_OK;
 43460  43700   
 43461  43701     }else{
 43462  43702       /* The pager cache has created a new page. Its content needs to 
 43463  43703       ** be initialized.  */
 43464  43704   
 43465  43705       pPg = *ppPage;
................................................................................
 43493  43733           testcase( rc==SQLITE_NOMEM );
 43494  43734           sqlite3EndBenignMalloc();
 43495  43735         }
 43496  43736         memset(pPg->pData, 0, pPager->pageSize);
 43497  43737         IOTRACE(("ZERO %p %d\n", pPager, pgno));
 43498  43738       }else{
 43499  43739         assert( pPg->pPager==pPager );
 43500         -      pPager->nMiss++;
        43740  +      pPager->aStat[PAGER_STAT_MISS]++;
 43501  43741         rc = readDbPage(pPg);
 43502  43742         if( rc!=SQLITE_OK ){
 43503  43743           goto pager_acquire_err;
 43504  43744         }
 43505  43745       }
 43506  43746       pager_set_pagehash(pPg);
 43507  43747     }
................................................................................
 44078  44318         /* If running in direct mode, write the contents of page 1 to the file. */
 44079  44319         if( DIRECT_MODE ){
 44080  44320           const void *zBuf;
 44081  44321           assert( pPager->dbFileSize>0 );
 44082  44322           CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
 44083  44323           if( rc==SQLITE_OK ){
 44084  44324             rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
        44325  +          pPager->aStat[PAGER_STAT_WRITE]++;
 44085  44326           }
 44086  44327           if( rc==SQLITE_OK ){
 44087  44328             pPager->changeCountDone = 1;
 44088  44329           }
 44089  44330         }else{
 44090  44331           pPager->changeCountDone = 1;
 44091  44332         }
................................................................................
 44521  44762     static int a[11];
 44522  44763     a[0] = sqlite3PcacheRefCount(pPager->pPCache);
 44523  44764     a[1] = sqlite3PcachePagecount(pPager->pPCache);
 44524  44765     a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
 44525  44766     a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
 44526  44767     a[4] = pPager->eState;
 44527  44768     a[5] = pPager->errCode;
 44528         -  a[6] = pPager->nHit;
 44529         -  a[7] = pPager->nMiss;
        44769  +  a[6] = pPager->aStat[PAGER_STAT_HIT];
        44770  +  a[7] = pPager->aStat[PAGER_STAT_MISS];
 44530  44771     a[8] = 0;  /* Used to be pPager->nOvfl */
 44531  44772     a[9] = pPager->nRead;
 44532         -  a[10] = pPager->nWrite;
        44773  +  a[10] = pPager->aStat[PAGER_STAT_WRITE];
 44533  44774     return a;
 44534  44775   }
 44535  44776   #endif
 44536  44777   
 44537  44778   /*
 44538  44779   ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 44539  44780   ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
 44540  44781   ** current cache hit or miss count, according to the value of eStat. If the 
 44541  44782   ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 44542  44783   ** returning.
 44543  44784   */
 44544  44785   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 44545         -  int *piStat;
 44546  44786   
 44547  44787     assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 44548  44788          || eStat==SQLITE_DBSTATUS_CACHE_MISS
        44789  +       || eStat==SQLITE_DBSTATUS_CACHE_WRITE
 44549  44790     );
 44550         -  if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){
 44551         -    piStat = &pPager->nHit;
 44552         -  }else{
 44553         -    piStat = &pPager->nMiss;
 44554         -  }
 44555  44791   
 44556         -  *pnVal += *piStat;
        44792  +  assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
        44793  +  assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
        44794  +  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
        44795  +
        44796  +  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
 44557  44797     if( reset ){
 44558         -    *piStat = 0;
        44798  +    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
 44559  44799     }
 44560  44800   }
 44561  44801   
 44562  44802   /*
 44563  44803   ** Return true if this is an in-memory pager.
 44564  44804   */
 44565  44805   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
................................................................................
 49064  49304   #define ISAUTOVACUUM 0
 49065  49305   #endif
 49066  49306   
 49067  49307   
 49068  49308   /*
 49069  49309   ** This structure is passed around through all the sanity checking routines
 49070  49310   ** in order to keep track of some global state information.
        49311  +**
        49312  +** The aRef[] array is allocated so that there is 1 bit for each page in
        49313  +** the database. As the integrity-check proceeds, for each page used in
        49314  +** the database the corresponding bit is set. This allows integrity-check to 
        49315  +** detect pages that are used twice and orphaned pages (both of which 
        49316  +** indicate corruption).
 49071  49317   */
 49072  49318   typedef struct IntegrityCk IntegrityCk;
 49073  49319   struct IntegrityCk {
 49074  49320     BtShared *pBt;    /* The tree being checked out */
 49075  49321     Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
 49076         -  int *anRef;       /* Number of times each page is referenced */
        49322  +  u8 *aPgRef;       /* 1 bit per page in the db (see above) */
 49077  49323     Pgno nPage;       /* Number of pages in the database */
 49078  49324     int mxErr;        /* Stop accumulating errors when this reaches zero */
 49079  49325     int nErr;         /* Number of messages written to zErrMsg so far */
 49080  49326     int mallocFailed; /* A memory allocation error has occurred */
 49081  49327     StrAccum errMsg;  /* Accumulate the error message text here */
 49082  49328   };
 49083  49329   
................................................................................
 56149  56395     /* Assert that the caller has been consistent. If this cursor was opened
 56150  56396     ** expecting an index b-tree, then the caller should be inserting blob
 56151  56397     ** keys with no associated data. If the cursor was opened expecting an
 56152  56398     ** intkey table, the caller should be inserting integer keys with a
 56153  56399     ** blob of associated data.  */
 56154  56400     assert( (pKey==0)==(pCur->pKeyInfo==0) );
 56155  56401   
 56156         -  /* If this is an insert into a table b-tree, invalidate any incrblob 
 56157         -  ** cursors open on the row being replaced (assuming this is a replace
 56158         -  ** operation - if it is not, the following is a no-op).  */
 56159         -  if( pCur->pKeyInfo==0 ){
 56160         -    invalidateIncrblobCursors(p, nKey, 0);
 56161         -  }
 56162         -
 56163  56402     /* Save the positions of any other cursors open on this table.
 56164  56403     **
 56165  56404     ** In some cases, the call to btreeMoveto() below is a no-op. For
 56166  56405     ** example, when inserting data into a table with auto-generated integer
 56167  56406     ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
 56168  56407     ** integer key to use. It then calls this function to actually insert the 
 56169  56408     ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
 56170  56409     ** that the cursor is already where it needs to be and returns without
 56171  56410     ** doing any work. To avoid thwarting these optimizations, it is important
 56172  56411     ** not to clear the cursor here.
 56173  56412     */
 56174  56413     rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 56175  56414     if( rc ) return rc;
        56415  +
        56416  +  /* If this is an insert into a table b-tree, invalidate any incrblob 
        56417  +  ** cursors open on the row being replaced (assuming this is a replace
        56418  +  ** operation - if it is not, the following is a no-op).  */
        56419  +  if( pCur->pKeyInfo==0 ){
        56420  +    invalidateIncrblobCursors(p, nKey, 0);
        56421  +  }
        56422  +
 56176  56423     if( !loc ){
 56177  56424       rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
 56178  56425       if( rc ) return rc;
 56179  56426     }
 56180  56427     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 56181  56428   
 56182  56429     pPage = pCur->apPage[pCur->iPage];
................................................................................
 56279  56526   
 56280  56527     if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
 56281  56528      || NEVER(pCur->eState!=CURSOR_VALID)
 56282  56529     ){
 56283  56530       return SQLITE_ERROR;  /* Something has gone awry. */
 56284  56531     }
 56285  56532   
 56286         -  /* If this is a delete operation to remove a row from a table b-tree,
 56287         -  ** invalidate any incrblob cursors open on the row being deleted.  */
 56288         -  if( pCur->pKeyInfo==0 ){
 56289         -    invalidateIncrblobCursors(p, pCur->info.nKey, 0);
 56290         -  }
 56291         -
 56292  56533     iCellDepth = pCur->iPage;
 56293  56534     iCellIdx = pCur->aiIdx[iCellDepth];
 56294  56535     pPage = pCur->apPage[iCellDepth];
 56295  56536     pCell = findCell(pPage, iCellIdx);
 56296  56537   
 56297  56538     /* If the page containing the entry to delete is not a leaf page, move
 56298  56539     ** the cursor to the largest entry in the tree that is smaller than
................................................................................
 56310  56551     /* Save the positions of any other cursors open on this table before
 56311  56552     ** making any modifications. Make the page containing the entry to be 
 56312  56553     ** deleted writable. Then free any overflow pages associated with the 
 56313  56554     ** entry and finally remove the cell itself from within the page.  
 56314  56555     */
 56315  56556     rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 56316  56557     if( rc ) return rc;
        56558  +
        56559  +  /* If this is a delete operation to remove a row from a table b-tree,
        56560  +  ** invalidate any incrblob cursors open on the row being deleted.  */
        56561  +  if( pCur->pKeyInfo==0 ){
        56562  +    invalidateIncrblobCursors(p, pCur->info.nKey, 0);
        56563  +  }
        56564  +
 56317  56565     rc = sqlite3PagerWrite(pPage->pDbPage);
 56318  56566     if( rc ) return rc;
 56319  56567     rc = clearCell(pPage, pCell);
 56320  56568     dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
 56321  56569     if( rc ) return rc;
 56322  56570   
 56323  56571     /* If the cell deleted was not located on a leaf page, then the cursor
................................................................................
 56591  56839   */
 56592  56840   SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
 56593  56841     int rc;
 56594  56842     BtShared *pBt = p->pBt;
 56595  56843     sqlite3BtreeEnter(p);
 56596  56844     assert( p->inTrans==TRANS_WRITE );
 56597  56845   
 56598         -  /* Invalidate all incrblob cursors open on table iTable (assuming iTable
 56599         -  ** is the root of a table b-tree - if it is not, the following call is
 56600         -  ** a no-op).  */
 56601         -  invalidateIncrblobCursors(p, 0, 1);
 56602         -
 56603  56846     rc = saveAllCursors(pBt, (Pgno)iTable, 0);
        56847  +
 56604  56848     if( SQLITE_OK==rc ){
        56849  +    /* Invalidate all incrblob cursors open on table iTable (assuming iTable
        56850  +    ** is the root of a table b-tree - if it is not, the following call is
        56851  +    ** a no-op).  */
        56852  +    invalidateIncrblobCursors(p, 0, 1);
 56605  56853       rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
 56606  56854     }
 56607  56855     sqlite3BtreeLeave(p);
 56608  56856     return rc;
 56609  56857   }
 56610  56858   
 56611  56859   /*
................................................................................
 56912  57160     if( pCheck->errMsg.mallocFailed ){
 56913  57161       pCheck->mallocFailed = 1;
 56914  57162     }
 56915  57163   }
 56916  57164   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 56917  57165   
 56918  57166   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
        57167  +
        57168  +/*
        57169  +** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
        57170  +** corresponds to page iPg is already set.
        57171  +*/
        57172  +static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
        57173  +  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
        57174  +  return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
        57175  +}
        57176  +
        57177  +/*
        57178  +** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
        57179  +*/
        57180  +static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
        57181  +  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
        57182  +  pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
        57183  +}
        57184  +
        57185  +
 56919  57186   /*
 56920  57187   ** Add 1 to the reference count for page iPage.  If this is the second
 56921  57188   ** reference to the page, add an error message to pCheck->zErrMsg.
 56922  57189   ** Return 1 if there are 2 ore more references to the page and 0 if
 56923  57190   ** if this is the first reference to the page.
 56924  57191   **
 56925  57192   ** Also check that the page number is in bounds.
................................................................................
 56926  57193   */
 56927  57194   static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
 56928  57195     if( iPage==0 ) return 1;
 56929  57196     if( iPage>pCheck->nPage ){
 56930  57197       checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
 56931  57198       return 1;
 56932  57199     }
 56933         -  if( pCheck->anRef[iPage]==1 ){
        57200  +  if( getPageReferenced(pCheck, iPage) ){
 56934  57201       checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
 56935  57202       return 1;
 56936  57203     }
 56937         -  return  (pCheck->anRef[iPage]++)>1;
        57204  +  setPageReferenced(pCheck, iPage);
        57205  +  return 0;
 56938  57206   }
 56939  57207   
 56940  57208   #ifndef SQLITE_OMIT_AUTOVACUUM
 56941  57209   /*
 56942  57210   ** Check that the entry in the pointer-map for page iChild maps to 
 56943  57211   ** page iParent, pointer type ptrType. If not, append an error message
 56944  57212   ** to pCheck.
................................................................................
 57306  57574     sCheck.nErr = 0;
 57307  57575     sCheck.mallocFailed = 0;
 57308  57576     *pnErr = 0;
 57309  57577     if( sCheck.nPage==0 ){
 57310  57578       sqlite3BtreeLeave(p);
 57311  57579       return 0;
 57312  57580     }
 57313         -  sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
 57314         -  if( !sCheck.anRef ){
        57581  +
        57582  +  sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
        57583  +  if( !sCheck.aPgRef ){
 57315  57584       *pnErr = 1;
 57316  57585       sqlite3BtreeLeave(p);
 57317  57586       return 0;
 57318  57587     }
 57319         -  for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
 57320  57588     i = PENDING_BYTE_PAGE(pBt);
 57321         -  if( i<=sCheck.nPage ){
 57322         -    sCheck.anRef[i] = 1;
 57323         -  }
        57589  +  if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
 57324  57590     sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
 57325  57591     sCheck.errMsg.useMalloc = 2;
 57326  57592   
 57327  57593     /* Check the integrity of the freelist
 57328  57594     */
 57329  57595     checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
 57330  57596               get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
................................................................................
 57341  57607       checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
 57342  57608     }
 57343  57609   
 57344  57610     /* Make sure every page in the file is referenced
 57345  57611     */
 57346  57612     for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
 57347  57613   #ifdef SQLITE_OMIT_AUTOVACUUM
 57348         -    if( sCheck.anRef[i]==0 ){
        57614  +    if( getPageReferenced(&sCheck, i)==0 ){
 57349  57615         checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
 57350  57616       }
 57351  57617   #else
 57352  57618       /* If the database supports auto-vacuum, make sure no tables contain
 57353  57619       ** references to pointer-map pages.
 57354  57620       */
 57355         -    if( sCheck.anRef[i]==0 && 
        57621  +    if( getPageReferenced(&sCheck, i)==0 && 
 57356  57622          (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
 57357  57623         checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
 57358  57624       }
 57359         -    if( sCheck.anRef[i]!=0 && 
        57625  +    if( getPageReferenced(&sCheck, i)!=0 && 
 57360  57626          (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
 57361  57627         checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
 57362  57628       }
 57363  57629   #endif
 57364  57630     }
 57365  57631   
 57366  57632     /* Make sure this analysis did not leave any unref() pages.
................................................................................
 57373  57639         nRef, sqlite3PagerRefcount(pBt->pPager)
 57374  57640       );
 57375  57641     }
 57376  57642   
 57377  57643     /* Clean  up and report errors.
 57378  57644     */
 57379  57645     sqlite3BtreeLeave(p);
 57380         -  sqlite3_free(sCheck.anRef);
        57646  +  sqlite3_free(sCheck.aPgRef);
 57381  57647     if( sCheck.mallocFailed ){
 57382  57648       sqlite3StrAccumReset(&sCheck.errMsg);
 57383  57649       *pnErr = sCheck.nErr+1;
 57384  57650       return 0;
 57385  57651     }
 57386  57652     *pnErr = sCheck.nErr;
 57387  57653     if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
................................................................................
 58409  58675   #endif
 58410  58676   }
 58411  58677   
 58412  58678   /*
 58413  58679   ** Make sure pMem->z points to a writable allocation of at least 
 58414  58680   ** n bytes.
 58415  58681   **
 58416         -** If the memory cell currently contains string or blob data
 58417         -** and the third argument passed to this function is true, the 
 58418         -** current content of the cell is preserved. Otherwise, it may
 58419         -** be discarded.  
        58682  +** If the third argument passed to this function is true, then memory
        58683  +** cell pMem must contain a string or blob. In this case the content is
        58684  +** preserved. Otherwise, if the third parameter to this function is false,
        58685  +** any current string or blob value may be discarded.
 58420  58686   **
 58421  58687   ** This function sets the MEM_Dyn flag and clears any xDel callback.
 58422  58688   ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
 58423  58689   ** not set, Mem.n is zeroed.
 58424  58690   */
 58425  58691   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
 58426  58692     assert( 1 >=
 58427  58693       ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
 58428  58694       (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
 58429  58695       ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
 58430  58696       ((pMem->flags&MEM_Static) ? 1 : 0)
 58431  58697     );
 58432  58698     assert( (pMem->flags&MEM_RowSet)==0 );
        58699  +
        58700  +  /* If the preserve flag is set to true, then the memory cell must already
        58701  +  ** contain a valid string or blob value.  */
        58702  +  assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
 58433  58703   
 58434  58704     if( n<32 ) n = 32;
 58435  58705     if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
 58436  58706       if( preserve && pMem->z==pMem->zMalloc ){
 58437  58707         pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 58438  58708         preserve = 0;
 58439  58709       }else{
................................................................................
 60745  61015         */
 60746  61016         if( pOp->p4type==P4_SUBPROGRAM ){
 60747  61017           int nByte = (nSub+1)*sizeof(SubProgram*);
 60748  61018           int j;
 60749  61019           for(j=0; j<nSub; j++){
 60750  61020             if( apSub[j]==pOp->p4.pProgram ) break;
 60751  61021           }
 60752         -        if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
        61022  +        if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
 60753  61023             apSub = (SubProgram **)pSub->z;
 60754  61024             apSub[nSub++] = pOp->p4.pProgram;
 60755  61025             pSub->flags |= MEM_Blob;
 60756  61026             pSub->n = nSub*sizeof(SubProgram*);
 60757  61027           }
 60758  61028         }
 60759  61029       }
................................................................................
 66994  67264   ** if the P4 argument is a P4_MEM use the value of the P4 argument as
 66995  67265   ** the result.
 66996  67266   **
 66997  67267   ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
 66998  67268   ** then the cache of the cursor is reset prior to extracting the column.
 66999  67269   ** The first OP_Column against a pseudo-table after the value of the content
 67000  67270   ** register has changed should have this bit set.
        67271  +**
        67272  +** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
        67273  +** the result is guaranteed to only be used as the argument of a length()
        67274  +** or typeof() function, respectively.  The loading of large blobs can be
        67275  +** skipped for length() and all content loading can be skipped for typeof().
 67001  67276   */
 67002  67277   case OP_Column: {
 67003  67278   #if 0  /* local variables moved into u.an */
 67004  67279     u32 payloadSize;   /* Number of bytes in the record */
 67005  67280     i64 payloadSize64; /* Number of bytes in the record */
 67006  67281     int p1;            /* P1 value of the opcode */
 67007  67282     int p2;            /* column number to retrieve */
................................................................................
 67136  67411         if( u.an.payloadSize <= (u32)u.an.avail ){
 67137  67412           u.an.zRec = u.an.zData;
 67138  67413           u.an.pC->aRow = (u8*)u.an.zData;
 67139  67414         }else{
 67140  67415           u.an.pC->aRow = 0;
 67141  67416         }
 67142  67417       }
 67143         -    /* The following assert is true in all cases accept when
        67418  +    /* The following assert is true in all cases except when
 67144  67419       ** the database file has been corrupted externally.
 67145  67420       **    assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
 67146  67421       u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
 67147  67422   
 67148  67423       /* Make sure a corrupt database has not given us an oversize header.
 67149  67424       ** Do this now to avoid an oversize memory allocation.
 67150  67425       **
................................................................................
 67211  67486           u.an.szField = sqlite3VdbeSerialTypeLen(u.an.t);
 67212  67487           u.an.offset += u.an.szField;
 67213  67488           if( u.an.offset<u.an.szField ){  /* True if u.an.offset overflows */
 67214  67489             u.an.zIdx = &u.an.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
 67215  67490             break;
 67216  67491           }
 67217  67492         }else{
 67218         -        /* If u.an.i is less that u.an.nField, then there are less fields in this
        67493  +        /* If u.an.i is less that u.an.nField, then there are fewer fields in this
 67219  67494           ** record than SetNumColumns indicated there are columns in the
 67220  67495           ** table. Set the u.an.offset for any extra columns not present in
 67221         -        ** the record to 0. This tells code below to store a NULL
 67222         -        ** instead of deserializing a value from the record.
        67496  +        ** the record to 0. This tells code below to store the default value
        67497  +        ** for the column instead of deserializing a value from the record.
 67223  67498           */
 67224  67499           u.an.aOffset[u.an.i] = 0;
 67225  67500         }
 67226  67501       }
 67227  67502       sqlite3VdbeMemRelease(&u.an.sMem);
 67228  67503       u.an.sMem.flags = MEM_Null;
 67229  67504   
................................................................................
 67245  67520     ** then there are not enough fields in the record to satisfy the
 67246  67521     ** request.  In this case, set the value NULL or to P4 if P4 is
 67247  67522     ** a pointer to a Mem object.
 67248  67523     */
 67249  67524     if( u.an.aOffset[u.an.p2] ){
 67250  67525       assert( rc==SQLITE_OK );
 67251  67526       if( u.an.zRec ){
        67527  +      /* This is the common case where the whole row fits on a single page */
 67252  67528         VdbeMemRelease(u.an.pDest);
 67253  67529         sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
 67254  67530       }else{
 67255         -      u.an.len = sqlite3VdbeSerialTypeLen(u.an.aType[u.an.p2]);
 67256         -      sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
 67257         -      rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex, &u.an.sMem);
 67258         -      if( rc!=SQLITE_OK ){
 67259         -        goto op_column_out;
        67531  +      /* This branch happens only when the row overflows onto multiple pages */
        67532  +      u.an.t = u.an.aType[u.an.p2];
        67533  +      if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
        67534  +       && ((u.an.t>=12 && (u.an.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
        67535  +      ){
        67536  +        /* Content is irrelevant for the typeof() function and for
        67537  +        ** the length(X) function if X is a blob.  So we might as well use
        67538  +        ** bogus content rather than reading content from disk.  NULL works
        67539  +        ** for text and blob and whatever is in the u.an.payloadSize64 variable
        67540  +        ** will work for everything else. */
        67541  +        u.an.zData = u.an.t<12 ? (char*)&u.an.payloadSize64 : 0;
        67542  +      }else{
        67543  +        u.an.len = sqlite3VdbeSerialTypeLen(u.an.t);
        67544  +        sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
        67545  +        rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len,  u.an.pC->isIndex,
        67546  +                                     &u.an.sMem);
        67547  +        if( rc!=SQLITE_OK ){
        67548  +          goto op_column_out;
        67549  +        }
        67550  +        u.an.zData = u.an.sMem.z;
 67260  67551         }
 67261         -      u.an.zData = u.an.sMem.z;
 67262         -      sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.aType[u.an.p2], u.an.pDest);
        67552  +      sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.t, u.an.pDest);
 67263  67553       }
 67264  67554       u.an.pDest->enc = encoding;
 67265  67555     }else{
 67266  67556       if( pOp->p4type==P4_MEM ){
 67267  67557         sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
 67268  67558       }else{
 67269  67559         MemSetTypeFlag(u.an.pDest, MEM_Null);
................................................................................
 67591  67881             p->rc = rc = SQLITE_BUSY;
 67592  67882             goto vdbe_return;
 67593  67883           }
 67594  67884           db->isTransactionSavepoint = 0;
 67595  67885           rc = p->rc;
 67596  67886         }else{
 67597  67887           u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
 67598         -        for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
 67599         -          sqlite3BtreeTripAllCursors(db->aDb[u.ar.ii].pBt, SQLITE_ABORT);
        67888  +        if( u.ar.p1==SAVEPOINT_ROLLBACK ){
        67889  +          for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
        67890  +            sqlite3BtreeTripAllCursors(db->aDb[u.ar.ii].pBt, SQLITE_ABORT);
        67891  +          }
 67600  67892           }
 67601  67893           for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
 67602  67894             rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
 67603  67895             if( rc!=SQLITE_OK ){
 67604  67896               goto abort_due_to_error;
 67605  67897             }
 67606  67898           }
................................................................................
 73695  73987   
 73696  73988         testcase( pExpr->op==TK_CONST_FUNC );
 73697  73989         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 73698  73990         zId = pExpr->u.zToken;
 73699  73991         nId = sqlite3Strlen30(zId);
 73700  73992         pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
 73701  73993         if( pDef==0 ){
 73702         -        pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
        73994  +        pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
 73703  73995           if( pDef==0 ){
 73704  73996             no_such_func = 1;
 73705  73997           }else{
 73706  73998             wrong_num_args = 1;
 73707  73999           }
 73708  74000         }else{
 73709  74001           is_agg = pDef->xFunc==0;
................................................................................
 74045  74337   */
 74046  74338   static int resolveOrderGroupBy(
 74047  74339     NameContext *pNC,     /* The name context of the SELECT statement */
 74048  74340     Select *pSelect,      /* The SELECT statement holding pOrderBy */
 74049  74341     ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
 74050  74342     const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
 74051  74343   ){
 74052         -  int i;                         /* Loop counter */
        74344  +  int i, j;                      /* Loop counters */
 74053  74345     int iCol;                      /* Column number */
 74054  74346     struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
 74055  74347     Parse *pParse;                 /* Parsing context */
 74056  74348     int nResult;                   /* Number of terms in the result set */
 74057  74349   
 74058  74350     if( pOrderBy==0 ) return 0;
 74059  74351     nResult = pSelect->pEList->nExpr;
................................................................................
 74081  74373         continue;
 74082  74374       }
 74083  74375   
 74084  74376       /* Otherwise, treat the ORDER BY term as an ordinary expression */
 74085  74377       pItem->iOrderByCol = 0;
 74086  74378       if( sqlite3ResolveExprNames(pNC, pE) ){
 74087  74379         return 1;
        74380  +    }
        74381  +    for(j=0; j<pSelect->pEList->nExpr; j++){
        74382  +      if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr)==0 ){
        74383  +        pItem->iOrderByCol = j+1;
        74384  +      }
 74088  74385       }
 74089  74386     }
 74090  74387     return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
 74091  74388   }
 74092  74389   
 74093  74390   /*
 74094  74391   ** Resolve names in the SELECT statement p and all of its descendents.
................................................................................
 74871  75168   SQLITE_PRIVATE Expr *sqlite3PExpr(
 74872  75169     Parse *pParse,          /* Parsing context */
 74873  75170     int op,                 /* Expression opcode */
 74874  75171     Expr *pLeft,            /* Left operand */
 74875  75172     Expr *pRight,           /* Right operand */
 74876  75173     const Token *pToken     /* Argument token */
 74877  75174   ){
 74878         -  Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
 74879         -  sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
        75175  +  Expr *p;
        75176  +  if( op==TK_AND && pLeft && pRight ){
        75177  +    /* Take advantage of short-circuit false optimization for AND */
        75178  +    p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
        75179  +  }else{
        75180  +    p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
        75181  +    sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
        75182  +  }
 74880  75183     if( p ) {
 74881  75184       sqlite3ExprCheckHeight(pParse, p->nHeight);
 74882  75185     }
 74883  75186     return p;
 74884  75187   }
        75188  +
        75189  +/*
        75190  +** Return 1 if an expression must be FALSE in all cases and 0 if the
        75191  +** expression might be true.  This is an optimization.  If is OK to
        75192  +** return 0 here even if the expression really is always false (a 
        75193  +** false negative).  But it is a bug to return 1 if the expression
        75194  +** might be true in some rare circumstances (a false positive.)
        75195  +**
        75196  +** Note that if the expression is part of conditional for a
        75197  +** LEFT JOIN, then we cannot determine at compile-time whether or not
        75198  +** is it true or false, so always return 0.
        75199  +*/
        75200  +static int exprAlwaysFalse(Expr *p){
        75201  +  int v = 0;
        75202  +  if( ExprHasProperty(p, EP_FromJoin) ) return 0;
        75203  +  if( !sqlite3ExprIsInteger(p, &v) ) return 0;
        75204  +  return v==0;
        75205  +}
 74885  75206   
 74886  75207   /*
 74887  75208   ** Join two expressions using an AND operator.  If either expression is
 74888  75209   ** NULL, then just return the other expression.
        75210  +**
        75211  +** If one side or the other of the AND is known to be false, then instead
        75212  +** of returning an AND expression, just return a constant expression with
        75213  +** a value of false.
 74889  75214   */
 74890  75215   SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
 74891  75216     if( pLeft==0 ){
 74892  75217       return pRight;
 74893  75218     }else if( pRight==0 ){
 74894  75219       return pLeft;
        75220  +  }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
        75221  +    sqlite3ExprDelete(db, pLeft);
        75222  +    sqlite3ExprDelete(db, pRight);
        75223  +    return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
 74895  75224     }else{
 74896  75225       Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
 74897  75226       sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
 74898  75227       return pNew;
 74899  75228     }
 74900  75229   }
 74901  75230   
................................................................................
 76419  76748     /* First replace any existing entry.
 76420  76749     **
 76421  76750     ** Actually, the way the column cache is currently used, we are guaranteed
 76422  76751     ** that the object will never already be in cache.  Verify this guarantee.
 76423  76752     */
 76424  76753   #ifndef NDEBUG
 76425  76754     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 76426         -#if 0 /* This code wold remove the entry from the cache if it existed */
 76427         -    if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){
 76428         -      cacheEntryClear(pParse, p);
 76429         -      p->iLevel = pParse->iCacheLevel;
 76430         -      p->iReg = iReg;
 76431         -      p->lru = pParse->iCacheCnt++;
 76432         -      return;
 76433         -    }
 76434         -#endif
 76435  76755       assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
 76436  76756     }
 76437  76757   #endif
 76438  76758   
 76439  76759     /* Find an empty slot and replace it */
 76440  76760     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 76441  76761       if( p->iReg==0 ){
................................................................................
 76562  76882   ** is called.  If iColumn<0 then code is generated that extracts the rowid.
 76563  76883   */
 76564  76884   SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
 76565  76885     Parse *pParse,   /* Parsing and code generating context */
 76566  76886     Table *pTab,     /* Description of the table we are reading from */
 76567  76887     int iColumn,     /* Index of the table column */
 76568  76888     int iTable,      /* The cursor pointing to the table */
 76569         -  int iReg         /* Store results here */
        76889  +  int iReg,        /* Store results here */
        76890  +  u8 p5            /* P5 value for OP_Column */
 76570  76891   ){
 76571  76892     Vdbe *v = pParse->pVdbe;
 76572  76893     int i;
 76573  76894     struct yColCache *p;
 76574  76895   
 76575  76896     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 76576  76897       if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
................................................................................
 76577  76898         p->lru = pParse->iCacheCnt++;
 76578  76899         sqlite3ExprCachePinRegister(pParse, p->iReg);
 76579  76900         return p->iReg;
 76580  76901       }
 76581  76902     }  
 76582  76903     assert( v!=0 );
 76583  76904     sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
 76584         -  sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
        76905  +  if( p5 ){
        76906  +    sqlite3VdbeChangeP5(v, p5);
        76907  +  }else{   
        76908  +    sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
        76909  +  }
 76585  76910     return iReg;
 76586  76911   }
 76587  76912   
 76588  76913   /*
 76589  76914   ** Clear all column cache entries.
 76590  76915   */
 76591  76916   SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
................................................................................
 76705  77030       case TK_COLUMN: {
 76706  77031         if( pExpr->iTable<0 ){
 76707  77032           /* This only happens when coding check constraints */
 76708  77033           assert( pParse->ckBase>0 );
 76709  77034           inReg = pExpr->iColumn + pParse->ckBase;
 76710  77035         }else{
 76711  77036           inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 76712         -                                 pExpr->iColumn, pExpr->iTable, target);
        77037  +                                 pExpr->iColumn, pExpr->iTable, target,
        77038  +                                 pExpr->op2);
 76713  77039         }
 76714  77040         break;
 76715  77041       }
 76716  77042       case TK_INTEGER: {
 76717  77043         codeInteger(pParse, pExpr, 0, target);
 76718  77044         break;
 76719  77045       }
................................................................................
 76982  77308           sqlite3VdbeResolveLabel(v, endCoalesce);
 76983  77309           break;
 76984  77310         }
 76985  77311   
 76986  77312   
 76987  77313         if( pFarg ){
 76988  77314           r1 = sqlite3GetTempRange(pParse, nFarg);
        77315  +
        77316  +        /* For length() and typeof() functions with a column argument,
        77317  +        ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
        77318  +        ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
        77319  +        ** loading.
        77320  +        */
        77321  +        if( (pDef->flags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
        77322  +          u8 exprOp;
        77323  +          assert( nFarg==1 );
        77324  +          assert( pFarg->a[0].pExpr!=0 );
        77325  +          exprOp = pFarg->a[0].pExpr->op;
        77326  +          if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
        77327  +            assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
        77328  +            assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
        77329  +            testcase( pDef->flags==SQLITE_FUNC_LENGTH );
        77330  +            pFarg->a[0].pExpr->op2 = pDef->flags;
        77331  +          }
        77332  +        }
        77333  +
 76989  77334           sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
 76990  77335           sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
 76991  77336           sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
 76992  77337         }else{
 76993  77338           r1 = 0;
 76994  77339         }
 76995  77340   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 78117  78462     if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
 78118  78463     if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
 78119  78464     if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
 78120  78465     if( ExprHasProperty(pA, EP_IntValue) ){
 78121  78466       if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
 78122  78467         return 2;
 78123  78468       }
 78124         -  }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
        78469  +  }else if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
 78125  78470       if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
 78126  78471       if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 78127  78472         return 2;
 78128  78473       }
 78129  78474     }
 78130  78475     if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
 78131  78476     if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
................................................................................
 78153  78498       Expr *pExprA = pA->a[i].pExpr;
 78154  78499       Expr *pExprB = pB->a[i].pExpr;
 78155  78500       if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
 78156  78501       if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
 78157  78502     }
 78158  78503     return 0;
 78159  78504   }
        78505  +
        78506  +/*
        78507  +** This is the expression callback for sqlite3FunctionUsesOtherSrc().
        78508  +**
        78509  +** Determine if an expression references any table other than one of the
        78510  +** tables in pWalker->u.pSrcList and abort if it does.
        78511  +*/
        78512  +static int exprUsesOtherSrc(Walker *pWalker, Expr *pExpr){
        78513  +  if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
        78514  +    int i;
        78515  +    SrcList *pSrc = pWalker->u.pSrcList;
        78516  +    for(i=0; i<pSrc->nSrc; i++){
        78517  +      if( pExpr->iTable==pSrc->a[i].iCursor ) return WRC_Continue;
        78518  +    }
        78519  +    return WRC_Abort;
        78520  +  }else{
        78521  +    return WRC_Continue;
        78522  +  }
        78523  +}
        78524  +
        78525  +/*
        78526  +** Determine if any of the arguments to the pExpr Function references
        78527  +** any SrcList other than pSrcList.  Return true if they do.  Return
        78528  +** false if pExpr has no argument or has only constant arguments or
        78529  +** only references tables named in pSrcList.
        78530  +*/
        78531  +static int sqlite3FunctionUsesOtherSrc(Expr *pExpr, SrcList *pSrcList){
        78532  +  Walker w;
        78533  +  assert( pExpr->op==TK_AGG_FUNCTION );
        78534  +  memset(&w, 0, sizeof(w));
        78535  +  w.xExprCallback = exprUsesOtherSrc;
        78536  +  w.u.pSrcList = pSrcList;
        78537  +  if( sqlite3WalkExprList(&w, pExpr->x.pList)!=WRC_Continue ) return 1;
        78538  +  return 0;
        78539  +}
 78160  78540   
 78161  78541   /*
 78162  78542   ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
 78163  78543   ** the new element.  Return a negative number if malloc fails.
 78164  78544   */
 78165  78545   static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
 78166  78546     int i;
................................................................................
 78269  78649               break;
 78270  78650             } /* endif pExpr->iTable==pItem->iCursor */
 78271  78651           } /* end loop over pSrcList */
 78272  78652         }
 78273  78653         return WRC_Prune;
 78274  78654       }
 78275  78655       case TK_AGG_FUNCTION: {
 78276         -      /* The pNC->nDepth==0 test causes aggregate functions in subqueries
 78277         -      ** to be ignored */
 78278         -      if( pNC->nDepth==0 ){
        78656  +      if( !sqlite3FunctionUsesOtherSrc(pExpr, pSrcList) ){
 78279  78657           /* Check to see if pExpr is a duplicate of another aggregate 
 78280  78658           ** function that is already in the pAggInfo structure
 78281  78659           */
 78282  78660           struct AggInfo_func *pItem = pAggInfo->aFunc;
 78283  78661           for(i=0; i<pAggInfo->nFunc; i++, pItem++){
 78284  78662             if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
 78285  78663               break;
................................................................................
 78315  78693           return WRC_Prune;
 78316  78694         }
 78317  78695       }
 78318  78696     }
 78319  78697     return WRC_Continue;
 78320  78698   }
 78321  78699   static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
 78322         -  NameContext *pNC = pWalker->u.pNC;
 78323         -  if( pNC->nDepth==0 ){
 78324         -    pNC->nDepth++;
 78325         -    sqlite3WalkSelect(pWalker, pSelect);
 78326         -    pNC->nDepth--;
 78327         -    return WRC_Prune;
 78328         -  }else{
 78329         -    return WRC_Continue;
 78330         -  }
        78700  +  UNUSED_PARAMETER(pWalker);
        78701  +  UNUSED_PARAMETER(pSelect);
        78702  +  return WRC_Continue;
 78331  78703   }
 78332  78704   
 78333  78705   /*
 78334  78706   ** Analyze the given expression looking for aggregate functions and
 78335  78707   ** for variables that need to be added to the pParse->aAgg[] array.
 78336  78708   ** Make additional entries to the pParse->aAgg[] array as necessary.
 78337  78709   **
 78338  78710   ** This routine should only be called after the expression has been
 78339  78711   ** analyzed by sqlite3ResolveExprNames().
 78340  78712   */
 78341  78713   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
 78342  78714     Walker w;
        78715  +  memset(&w, 0, sizeof(w));
 78343  78716     w.xExprCallback = analyzeAggregate;
 78344  78717     w.xSelectCallback = analyzeAggregatesInSelect;
 78345  78718     w.u.pNC = pNC;
 78346  78719     assert( pNC->pSrcList!=0 );
 78347  78720     sqlite3WalkExpr(&w, pExpr);
 78348  78721   }
 78349  78722   
................................................................................
 81729  82102     /* Delete the Table structure itself.
 81730  82103     */
 81731  82104     sqliteDeleteColumnNames(db, pTable);
 81732  82105     sqlite3DbFree(db, pTable->zName);
 81733  82106     sqlite3DbFree(db, pTable->zColAff);
 81734  82107     sqlite3SelectDelete(db, pTable->pSelect);
 81735  82108   #ifndef SQLITE_OMIT_CHECK
 81736         -  sqlite3ExprDelete(db, pTable->pCheck);
        82109  +  sqlite3ExprListDelete(db, pTable->pCheck);
 81737  82110   #endif
 81738  82111   #ifndef SQLITE_OMIT_VIRTUALTABLE
 81739  82112     sqlite3VtabClear(db, pTable);
 81740  82113   #endif
 81741  82114     sqlite3DbFree(db, pTable);
 81742  82115   }
 81743  82116   
................................................................................
 82392  82765   /*
 82393  82766   ** Add a new CHECK constraint to the table currently under construction.
 82394  82767   */
 82395  82768   SQLITE_PRIVATE void sqlite3AddCheckConstraint(
 82396  82769     Parse *pParse,    /* Parsing context */
 82397  82770     Expr *pCheckExpr  /* The check expression */
 82398  82771   ){
 82399         -  sqlite3 *db = pParse->db;
 82400  82772   #ifndef SQLITE_OMIT_CHECK
 82401  82773     Table *pTab = pParse->pNewTable;
 82402  82774     if( pTab && !IN_DECLARE_VTAB ){
 82403         -    pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
        82775  +    pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
        82776  +    if( pParse->constraintName.n ){
        82777  +      sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
        82778  +    }
 82404  82779     }else
 82405  82780   #endif
 82406  82781     {
 82407         -    sqlite3ExprDelete(db, pCheckExpr);
        82782  +    sqlite3ExprDelete(pParse->db, pCheckExpr);
 82408  82783     }
 82409  82784   }
 82410  82785   
 82411  82786   /*
 82412  82787   ** Set the collation function of the most recently parsed table column
 82413  82788   ** to the CollSeq given.
 82414  82789   */
................................................................................
 82670  83045   
 82671  83046   #ifndef SQLITE_OMIT_CHECK
 82672  83047     /* Resolve names in all CHECK constraint expressions.
 82673  83048     */
 82674  83049     if( p->pCheck ){
 82675  83050       SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
 82676  83051       NameContext sNC;                /* Name context for pParse->pNewTable */
        83052  +    ExprList *pList;                /* List of all CHECK constraints */
        83053  +    int i;                          /* Loop counter */
 82677  83054   
 82678  83055       memset(&sNC, 0, sizeof(sNC));
 82679  83056       memset(&sSrc, 0, sizeof(sSrc));
 82680  83057       sSrc.nSrc = 1;
 82681  83058       sSrc.a[0].zName = p->zName;
 82682  83059       sSrc.a[0].pTab = p;
 82683  83060       sSrc.a[0].iCursor = -1;
 82684  83061       sNC.pParse = pParse;
 82685  83062       sNC.pSrcList = &sSrc;
 82686  83063       sNC.isCheck = 1;
 82687         -    if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){
 82688         -      return;
        83064  +    pList = p->pCheck;
        83065  +    for(i=0; i<pList->nExpr; i++){
        83066  +      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
        83067  +        return;
        83068  +      }
 82689  83069       }
 82690  83070     }
 82691  83071   #endif /* !defined(SQLITE_OMIT_CHECK) */
 82692  83072   
 82693  83073     /* If the db->init.busy is 1 it means we are reading the SQL off the
 82694  83074     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
 82695  83075     ** So do not write to the disk again.  Extract the root page number
................................................................................
 84232  84612     }
 84233  84613   
 84234  84614   exit_drop_index:
 84235  84615     sqlite3SrcListDelete(db, pName);
 84236  84616   }
 84237  84617   
 84238  84618   /*
 84239         -** pArray is a pointer to an array of objects.  Each object in the
 84240         -** array is szEntry bytes in size.  This routine allocates a new
 84241         -** object on the end of the array.
        84619  +** pArray is a pointer to an array of objects. Each object in the
        84620  +** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
        84621  +** to extend the array so that there is space for a new object at the end.
 84242  84622   **
 84243         -** *pnEntry is the number of entries already in use.  *pnAlloc is
 84244         -** the previously allocated size of the array.  initSize is the
 84245         -** suggested initial array size allocation.
        84623  +** When this function is called, *pnEntry contains the current size of
        84624  +** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
        84625  +** in total).
 84246  84626   **
 84247         -** The index of the new entry is returned in *pIdx.
        84627  +** If the realloc() is successful (i.e. if no OOM condition occurs), the
        84628  +** space allocated for the new object is zeroed, *pnEntry updated to
        84629  +** reflect the new size of the array and a pointer to the new allocation
        84630  +** returned. *pIdx is set to the index of the new array entry in this case.
 84248  84631   **
 84249         -** This routine returns a pointer to the array of objects.  This
 84250         -** might be the same as the pArray parameter or it might be a different
 84251         -** pointer if the array was resized.
        84632  +** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
        84633  +** unchanged and a copy of pArray returned.
 84252  84634   */
 84253  84635   SQLITE_PRIVATE void *sqlite3ArrayAllocate(
 84254  84636     sqlite3 *db,      /* Connection to notify of malloc failures */
 84255  84637     void *pArray,     /* Array of objects.  Might be reallocated */
 84256  84638     int szEntry,      /* Size of each object in the array */
 84257  84639     int *pnEntry,     /* Number of objects currently in use */
 84258  84640     int *pIdx         /* Write the index of a new slot here */
................................................................................
 85230  85612   }
 85231  85613   
 85232  85614   /* During the search for the best function definition, this procedure
 85233  85615   ** is called to test how well the function passed as the first argument
 85234  85616   ** matches the request for a function with nArg arguments in a system
 85235  85617   ** that uses encoding enc. The value returned indicates how well the
 85236  85618   ** request is matched. A higher value indicates a better match.
        85619  +**
        85620  +** If nArg is -1 that means to only return a match (non-zero) if p->nArg
        85621  +** is also -1.  In other words, we are searching for a function that
        85622  +** takes a variable number of arguments.
        85623  +**
        85624  +** If nArg is -2 that means that we are searching for any function 
        85625  +** regardless of the number of arguments it uses, so return a positive
        85626  +** match score for any
 85237  85627   **
 85238  85628   ** The returned value is always between 0 and 6, as follows:
 85239  85629   **
 85240         -** 0: Not a match, or if nArg<0 and the function is has no implementation.
 85241         -** 1: A variable arguments function that prefers UTF-8 when a UTF-16
 85242         -**    encoding is requested, or vice versa.
 85243         -** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
 85244         -**    requested, or vice versa.
 85245         -** 3: A variable arguments function using the same text encoding.
 85246         -** 4: A function with the exact number of arguments requested that
 85247         -**    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
 85248         -** 5: A function with the exact number of arguments requested that
 85249         -**    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
 85250         -** 6: An exact match.
 85251         -**
 85252         -*/
 85253         -static int matchQuality(FuncDef *p, int nArg, u8 enc){
 85254         -  int match = 0;
 85255         -  if( p->nArg==-1 || p->nArg==nArg 
 85256         -   || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
 85257         -  ){
        85630  +** 0: Not a match.
        85631  +** 1: UTF8/16 conversion required and function takes any number of arguments.
        85632  +** 2: UTF16 byte order change required and function takes any number of args.
        85633  +** 3: encoding matches and function takes any number of arguments
        85634  +** 4: UTF8/16 conversion required - argument count matches exactly
        85635  +** 5: UTF16 byte order conversion required - argument count matches exactly
        85636  +** 6: Perfect match:  encoding and argument count match exactly.
        85637  +**
        85638  +** If nArg==(-2) then any function with a non-null xStep or xFunc is
        85639  +** a perfect match and any function with both xStep and xFunc NULL is
        85640  +** a non-match.
        85641  +*/
        85642  +#define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
        85643  +static int matchQuality(
        85644  +  FuncDef *p,     /* The function we are evaluating for match quality */
        85645  +  int nArg,       /* Desired number of arguments.  (-1)==any */
        85646  +  u8 enc          /* Desired text encoding */
        85647  +){
        85648  +  int match;
        85649  +
        85650  +  /* nArg of -2 is a special case */
        85651  +  if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
        85652  +
        85653  +  /* Wrong number of arguments means "no match" */
        85654  +  if( p->nArg!=nArg && p->nArg>=0 ) return 0;
        85655  +
        85656  +  /* Give a better score to a function with a specific number of arguments
        85657  +  ** than to function that accepts any number of arguments. */
        85658  +  if( p->nArg==nArg ){
        85659  +    match = 4;
        85660  +  }else{
 85258  85661       match = 1;
 85259         -    if( p->nArg==nArg || nArg==-1 ){
 85260         -      match = 4;
 85261         -    }
 85262         -    if( enc==p->iPrefEnc ){
 85263         -      match += 2;
 85264         -    }
 85265         -    else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
 85266         -             (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
 85267         -      match += 1;
 85268         -    }
 85269  85662     }
        85663  +
        85664  +  /* Bonus points if the text encoding matches */
        85665  +  if( enc==p->iPrefEnc ){
        85666  +    match += 2;  /* Exact encoding match */
        85667  +  }else if( (enc & p->iPrefEnc & 2)!=0 ){
        85668  +    match += 1;  /* Both are UTF16, but with different byte orders */
        85669  +  }
        85670  +
 85270  85671     return match;
 85271  85672   }
 85272  85673   
 85273  85674   /*
 85274  85675   ** Search a FuncDefHash for a function with the given name.  Return
 85275  85676   ** a pointer to the matching FuncDef if found, or 0 if there is no match.
 85276  85677   */
................................................................................
 85318  85719   ** Locate a user function given a name, a number of arguments and a flag
 85319  85720   ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
 85320  85721   ** pointer to the FuncDef structure that defines that function, or return
 85321  85722   ** NULL if the function does not exist.
 85322  85723   **
 85323  85724   ** If the createFlag argument is true, then a new (blank) FuncDef
 85324  85725   ** structure is created and liked into the "db" structure if a
 85325         -** no matching function previously existed.  When createFlag is true
 85326         -** and the nArg parameter is -1, then only a function that accepts
 85327         -** any number of arguments will be returned.
        85726  +** no matching function previously existed.
 85328  85727   **
 85329         -** If createFlag is false and nArg is -1, then the first valid
 85330         -** function found is returned.  A function is valid if either xFunc
 85331         -** or xStep is non-zero.
        85728  +** If nArg is -2, then the first valid function found is returned.  A
        85729  +** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
        85730  +** case is used to see if zName is a valid function name for some number
        85731  +** of arguments.  If nArg is -2, then createFlag must be 0.
 85332  85732   **
 85333  85733   ** If createFlag is false, then a function with the required name and
 85334  85734   ** number of arguments may be returned even if the eTextRep flag does not
 85335  85735   ** match that requested.
 85336  85736   */
 85337  85737   SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
 85338  85738     sqlite3 *db,       /* An open database */
 85339  85739     const char *zName, /* Name of the function.  Not null-terminated */
 85340  85740     int nName,         /* Number of characters in the name */
 85341  85741     int nArg,          /* Number of arguments.  -1 means any number */
 85342  85742     u8 enc,            /* Preferred text encoding */
 85343         -  int createFlag     /* Create new entry if true and does not otherwise exist */
        85743  +  u8 createFlag      /* Create new entry if true and does not otherwise exist */
 85344  85744   ){
 85345  85745     FuncDef *p;         /* Iterator variable */
 85346  85746     FuncDef *pBest = 0; /* Best match found so far */
 85347  85747     int bestScore = 0;  /* Score of best match */
 85348  85748     int h;              /* Hash value */
 85349  85749   
 85350         -
        85750  +  assert( nArg>=(-2) );
        85751  +  assert( nArg>=(-1) || createFlag==0 );
 85351  85752     assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
 85352  85753     h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
 85353  85754   
 85354  85755     /* First search for a match amongst the application-defined functions.
 85355  85756     */
 85356  85757     p = functionSearch(&db->aFunc, h, zName, nName);
 85357  85758     while( p ){
................................................................................
 85389  85790       }
 85390  85791     }
 85391  85792   
 85392  85793     /* If the createFlag parameter is true and the search did not reveal an
 85393  85794     ** exact match for the name, number of arguments and encoding, then add a
 85394  85795     ** new entry to the hash table and return it.
 85395  85796     */
 85396         -  if( createFlag && (bestScore<6 || pBest->nArg!=nArg) && 
        85797  +  if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
 85397  85798         (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
 85398  85799       pBest->zName = (char *)&pBest[1];
 85399  85800       pBest->nArg = (u16)nArg;
 85400  85801       pBest->iPrefEnc = enc;
 85401  85802       memcpy(pBest->zName, zName, nName);
 85402  85803       pBest->zName[nName] = 0;
 85403  85804       sqlite3FuncDefInsert(&db->aFunc, pBest);
................................................................................
 85841  86242       /* Collect rowids of every row to be deleted.
 85842  86243       */
 85843  86244       sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
 85844  86245       pWInfo = sqlite3WhereBegin(
 85845  86246           pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK
 85846  86247       );
 85847  86248       if( pWInfo==0 ) goto delete_from_cleanup;
 85848         -    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid);
        86249  +    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid, 0);
 85849  86250       sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
 85850  86251       if( db->flags & SQLITE_CountRows ){
 85851  86252         sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
 85852  86253       }
 85853  86254       sqlite3WhereEnd(pWInfo);
 85854  86255   
 85855  86256       /* Delete every item whose key was written to the list during the
................................................................................
 87661  88062       FUNCTION(trim,               2, 3, 0, trimFunc         ),
 87662  88063       FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
 87663  88064       FUNCTION(min,                0, 0, 1, 0                ),
 87664  88065       AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
 87665  88066       FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
 87666  88067       FUNCTION(max,                0, 1, 1, 0                ),
 87667  88068       AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
 87668         -    FUNCTION(typeof,             1, 0, 0, typeofFunc       ),
 87669         -    FUNCTION(length,             1, 0, 0, lengthFunc       ),
        88069  +    FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
        88070  +    FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
 87670  88071       FUNCTION(substr,             2, 0, 0, substrFunc       ),
 87671  88072       FUNCTION(substr,             3, 0, 0, substrFunc       ),
 87672  88073       FUNCTION(abs,                1, 0, 0, absFunc          ),
 87673  88074   #ifndef SQLITE_OMIT_FLOATING_POINT
 87674  88075       FUNCTION(round,              1, 0, 0, roundFunc        ),
 87675  88076       FUNCTION(round,              2, 0, 0, roundFunc        ),
 87676  88077   #endif
 87677  88078       FUNCTION(upper,              1, 0, 0, upperFunc        ),
 87678  88079       FUNCTION(lower,              1, 0, 0, lowerFunc        ),
 87679  88080       FUNCTION(coalesce,           1, 0, 0, 0                ),
 87680  88081       FUNCTION(coalesce,           0, 0, 0, 0                ),
 87681         -/*  FUNCTION(coalesce,          -1, 0, 0, ifnullFunc       ), */
 87682         -    {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0},
        88082  +    FUNCTION2(coalesce,         -1, 0, 0, ifnullFunc,  SQLITE_FUNC_COALESCE),
 87683  88083       FUNCTION(hex,                1, 0, 0, hexFunc          ),
 87684         -/*  FUNCTION(ifnull,             2, 0, 0, ifnullFunc       ), */
 87685         -    {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0},
        88084  +    FUNCTION2(ifnull,            2, 0, 0, ifnullFunc,  SQLITE_FUNC_COALESCE),
 87686  88085       FUNCTION(random,             0, 0, 0, randomFunc       ),
 87687  88086       FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
 87688  88087       FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
 87689  88088       FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
 87690  88089       FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
 87691  88090       FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
 87692  88091   #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
................................................................................
 90115  90514     int nCol;           /* Number of columns */
 90116  90515     int onError;        /* Conflict resolution strategy */
 90117  90516     int j1;             /* Addresss of jump instruction */
 90118  90517     int j2 = 0, j3;     /* Addresses of jump instructions */
 90119  90518     int regData;        /* Register containing first data column */
 90120  90519     int iCur;           /* Table cursor number */
 90121  90520     Index *pIdx;         /* Pointer to one of the indices */
        90521  +  sqlite3 *db;         /* Database connection */
 90122  90522     int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
 90123  90523     int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
 90124  90524   
        90525  +  db = pParse->db;
 90125  90526     v = sqlite3GetVdbe(pParse);
 90126  90527     assert( v!=0 );
 90127  90528     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
 90128  90529     nCol = pTab->nCol;
 90129  90530     regData = regRowid + 1;
 90130  90531   
 90131  90532     /* Test all NOT NULL constraints.
................................................................................
 90150  90551         case OE_Abort:
 90151  90552           sqlite3MayAbort(pParse);
 90152  90553         case OE_Rollback:
 90153  90554         case OE_Fail: {
 90154  90555           char *zMsg;
 90155  90556           sqlite3VdbeAddOp3(v, OP_HaltIfNull,
 90156  90557                                     SQLITE_CONSTRAINT, onError, regData+i);
 90157         -        zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
        90558  +        zMsg = sqlite3MPrintf(db, "%s.%s may not be NULL",
 90158  90559                                 pTab->zName, pTab->aCol[i].zName);
 90159  90560           sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
 90160  90561           break;
 90161  90562         }
 90162  90563         case OE_Ignore: {
 90163  90564           sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
 90164  90565           break;
................................................................................
 90172  90573         }
 90173  90574       }
 90174  90575     }
 90175  90576   
 90176  90577     /* Test all CHECK constraints
 90177  90578     */
 90178  90579   #ifndef SQLITE_OMIT_CHECK
 90179         -  if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
 90180         -    int allOk = sqlite3VdbeMakeLabel(v);
        90580  +  if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
        90581  +    ExprList *pCheck = pTab->pCheck;
 90181  90582       pParse->ckBase = regData;
 90182         -    sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
 90183  90583       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
 90184         -    if( onError==OE_Ignore ){
 90185         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 90186         -    }else{
 90187         -      if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
 90188         -      sqlite3HaltConstraint(pParse, onError, 0, 0);
        90584  +    for(i=0; i<pCheck->nExpr; i++){
        90585  +      int allOk = sqlite3VdbeMakeLabel(v);
        90586  +      sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
        90587  +      if( onError==OE_Ignore ){
        90588  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
        90589  +      }else{
        90590  +        char *zConsName = pCheck->a[i].zName;
        90591  +        if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
        90592  +        if( zConsName ){
        90593  +          zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
        90594  +        }else{
        90595  +          zConsName = 0;
        90596  +        }
        90597  +        sqlite3HaltConstraint(pParse, onError, zConsName, P4_DYNAMIC);
        90598  +      }
        90599  +      sqlite3VdbeResolveLabel(v, allOk);
 90189  90600       }
 90190         -    sqlite3VdbeResolveLabel(v, allOk);
 90191  90601     }
 90192  90602   #endif /* !defined(SQLITE_OMIT_CHECK) */
 90193  90603   
 90194  90604     /* If we have an INTEGER PRIMARY KEY, make sure the primary key
 90195  90605     ** of the new record does not previously exist.  Except, if this
 90196  90606     ** is an UPDATE and the primary key is not changing, that is OK.
 90197  90607     */
................................................................................
 90239  90649           **
 90240  90650           **   REPLACE INTO t(rowid) VALUES($newrowid)
 90241  90651           **
 90242  90652           ** to run without a statement journal if there are no indexes on the
 90243  90653           ** table.
 90244  90654           */
 90245  90655           Trigger *pTrigger = 0;
 90246         -        if( pParse->db->flags&SQLITE_RecTriggers ){
        90656  +        if( db->flags&SQLITE_RecTriggers ){
 90247  90657             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 90248  90658           }
 90249  90659           if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
 90250  90660             sqlite3MultiWrite(pParse);
 90251  90661             sqlite3GenerateRowDelete(
 90252  90662                 pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
 90253  90663             );
................................................................................
 90328  90738         case OE_Fail: {
 90329  90739           int j;
 90330  90740           StrAccum errMsg;
 90331  90741           const char *zSep;
 90332  90742           char *zErr;
 90333  90743   
 90334  90744           sqlite3StrAccumInit(&errMsg, 0, 0, 200);
 90335         -        errMsg.db = pParse->db;
        90745  +        errMsg.db = db;
 90336  90746           zSep = pIdx->nColumn>1 ? "columns " : "column ";
 90337  90747           for(j=0; j<pIdx->nColumn; j++){
 90338  90748             char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
 90339  90749             sqlite3StrAccumAppend(&errMsg, zSep, -1);
 90340  90750             zSep = ", ";
 90341  90751             sqlite3StrAccumAppend(&errMsg, zCol, -1);
 90342  90752           }
................................................................................
 90352  90762           sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 90353  90763           break;
 90354  90764         }
 90355  90765         default: {
 90356  90766           Trigger *pTrigger = 0;
 90357  90767           assert( onError==OE_Replace );
 90358  90768           sqlite3MultiWrite(pParse);
 90359         -        if( pParse->db->flags&SQLITE_RecTriggers ){
        90769  +        if( db->flags&SQLITE_RecTriggers ){
 90360  90770             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 90361  90771           }
 90362  90772           sqlite3GenerateRowDelete(
 90363  90773               pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
 90364  90774           );
 90365  90775           seenReplace = 1;
 90366  90776           break;
................................................................................
 90682  91092         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
 90683  91093       }
 90684  91094       if( pSrcIdx==0 ){
 90685  91095         return 0;    /* pDestIdx has no corresponding index in pSrc */
 90686  91096       }
 90687  91097     }
 90688  91098   #ifndef SQLITE_OMIT_CHECK
 90689         -  if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
        91099  +  if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck, pDest->pCheck) ){
 90690  91100       return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
 90691  91101     }
 90692  91102   #endif
 90693  91103   #ifndef SQLITE_OMIT_FOREIGN_KEY
 90694  91104     /* Disallow the transfer optimization if the destination table constains
 90695  91105     ** any foreign key constraints.  This is more restrictive than necessary.
 90696  91106     ** But the main beneficiary of the transfer optimization is the VACUUM 
................................................................................
 95755  96165     int cnt;                    /* Index added to make the name unique */
 95756  96166     Column *aCol, *pCol;        /* For looping over result columns */
 95757  96167     int nCol;                   /* Number of columns in the result set */
 95758  96168     Expr *p;                    /* Expression for a single result column */
 95759  96169     char *zName;                /* Column name */
 95760  96170     int nName;                  /* Size of name in zName[] */
 95761  96171   
 95762         -  *pnCol = nCol = pEList ? pEList->nExpr : 0;
 95763         -  aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
 95764         -  if( aCol==0 ) return SQLITE_NOMEM;
        96172  +  if( pEList ){
        96173  +    nCol = pEList->nExpr;
        96174  +    aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
        96175  +    testcase( aCol==0 );
        96176  +  }else{
        96177  +    nCol = 0;
        96178  +    aCol = 0;
        96179  +  }
        96180  +  *pnCol = nCol;
        96181  +  *paCol = aCol;
        96182  +
 95765  96183     for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 95766  96184       /* Get an appropriate name for the column
 95767  96185       */
 95768  96186       p = pEList->a[i].pExpr;
 95769  96187       assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
 95770  96188                  || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
 95771  96189       if( (zName = pEList->a[i].zName)!=0 ){
................................................................................
 97340  97758       }
 97341  97759     }
 97342  97760   
 97343  97761     /***** If we reach this point, flattening is permitted. *****/
 97344  97762   
 97345  97763     /* Authorize the subquery */
 97346  97764     pParse->zAuthContext = pSubitem->zName;
 97347         -  sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
        97765  +  TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
        97766  +  testcase( i==SQLITE_DENY );
 97348  97767     pParse->zAuthContext = zSavedAuthContext;
 97349  97768   
 97350  97769     /* If the sub-query is a compound SELECT statement, then (by restrictions
 97351  97770     ** 17 and 18 above) it must be a UNION ALL and the parent query must 
 97352  97771     ** be of the form:
 97353  97772     **
 97354  97773     **     SELECT <expr-list> FROM (<sub-query>) <where-clause> 
................................................................................
 98725  99144           for(i=0; i<sAggInfo.nColumn; i++){
 98726  99145             struct AggInfo_col *pCol = &sAggInfo.aCol[i];
 98727  99146             if( pCol->iSorterColumn>=j ){
 98728  99147               int r1 = j + regBase;
 98729  99148               int r2;
 98730  99149   
 98731  99150               r2 = sqlite3ExprCodeGetColumn(pParse, 
 98732         -                               pCol->pTab, pCol->iColumn, pCol->iTable, r1);
        99151  +                               pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
 98733  99152               if( r1!=r2 ){
 98734  99153                 sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
 98735  99154               }
 98736  99155               j++;
 98737  99156             }
 98738  99157           }
 98739  99158           regRecord = sqlite3GetTempReg(pParse);
................................................................................
101906 102325   static int vtabCallConstructor(
101907 102326     sqlite3 *db, 
101908 102327     Table *pTab,
101909 102328     Module *pMod,
101910 102329     int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
101911 102330     char **pzErr
101912 102331   ){
101913         -  VtabCtx sCtx;
       102332  +  VtabCtx sCtx, *pPriorCtx;
101914 102333     VTable *pVTable;
101915 102334     int rc;
101916 102335     const char *const*azArg = (const char *const*)pTab->azModuleArg;
101917 102336     int nArg = pTab->nModuleArg;
101918 102337     char *zErr = 0;
101919 102338     char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
101920 102339   
................................................................................
101931 102350     pVTable->pMod = pMod;
101932 102351   
101933 102352     /* Invoke the virtual table constructor */
101934 102353     assert( &db->pVtabCtx );
101935 102354     assert( xConstruct );
101936 102355     sCtx.pTab = pTab;
101937 102356     sCtx.pVTable = pVTable;
       102357  +  pPriorCtx = db->pVtabCtx;
101938 102358     db->pVtabCtx = &sCtx;
101939 102359     rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
101940         -  db->pVtabCtx = 0;
       102360  +  db->pVtabCtx = pPriorCtx;
101941 102361     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
101942 102362   
101943 102363     if( SQLITE_OK!=rc ){
101944 102364       if( zErr==0 ){
101945 102365         *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
101946 102366       }else {
101947 102367         *pzErr = sqlite3MPrintf(db, "%s", zErr);
................................................................................
103214 103634       return 0;
103215 103635     }
103216 103636   #ifdef SQLITE_EBCDIC
103217 103637     if( *pnoCase ) return 0;
103218 103638   #endif
103219 103639     pList = pExpr->x.pList;
103220 103640     pLeft = pList->a[1].pExpr;
103221         -  if( pLeft->op!=TK_COLUMN || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ){
       103641  +  if( pLeft->op!=TK_COLUMN 
       103642  +   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
       103643  +   || IsVirtual(pLeft->pTab)
       103644  +  ){
103222 103645       /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
103223 103646       ** be the name of an indexed column with TEXT affinity. */
103224 103647       return 0;
103225 103648     }
103226 103649     assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
103227 103650   
103228 103651     pRight = pList->a[0].pExpr;
................................................................................
104087 104510     **
104088 104511     **   1. The index is itself UNIQUE, and
104089 104512     **
104090 104513     **   2. All of the columns in the index are either part of the pDistinct
104091 104514     **      list, or else the WHERE clause contains a term of the form "col=X",
104092 104515     **      where X is a constant value. The collation sequences of the
104093 104516     **      comparison and select-list expressions must match those of the index.
       104517  +  **
       104518  +  **   3. All of those index columns for which the WHERE clause does not
       104519  +  **      contain a "col=X" term are subject to a NOT NULL constraint.
104094 104520     */
104095 104521     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
104096 104522       if( pIdx->onError==OE_None ) continue;
104097 104523       for(i=0; i<pIdx->nColumn; i++){
104098 104524         int iCol = pIdx->aiColumn[i];
104099         -      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) 
104100         -       && 0>findIndexCol(pParse, pDistinct, iBase, pIdx, i)
104101         -      ){
104102         -        break;
       104525  +      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
       104526  +        int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
       104527  +        if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){
       104528  +          break;
       104529  +        }
104103 104530         }
104104 104531       }
104105 104532       if( i==pIdx->nColumn ){
104106 104533         /* This index implies that the DISTINCT qualifier is redundant. */
104107 104534         return 1;
104108 104535       }
104109 104536     }
................................................................................
104243 104670     if( j>=nTerm ){
104244 104671       /* All terms of the ORDER BY clause are covered by this index so
104245 104672       ** this index can be used for sorting. */
104246 104673       return 1;
104247 104674     }
104248 104675     if( pIdx->onError!=OE_None && i==pIdx->nColumn
104249 104676         && (wsFlags & WHERE_COLUMN_NULL)==0
104250         -      && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
104251         -    /* All terms of this index match some prefix of the ORDER BY clause
104252         -    ** and the index is UNIQUE and no terms on the tail of the ORDER BY
104253         -    ** clause reference other tables in a join.  If this is all true then
104254         -    ** the order by clause is superfluous.  Not that if the matching
104255         -    ** condition is IS NULL then the result is not necessarily unique
104256         -    ** even on a UNIQUE index, so disallow those cases. */
104257         -    return 1;
       104677  +      && !referencesOtherTables(pOrderBy, pMaskSet, j, base) 
       104678  +  ){
       104679  +    Column *aCol = pIdx->pTable->aCol;
       104680  +
       104681  +    /* All terms of this index match some prefix of the ORDER BY clause,
       104682  +    ** the index is UNIQUE, and no terms on the tail of the ORDER BY
       104683  +    ** refer to other tables in a join. So, assuming that the index entries
       104684  +    ** visited contain no NULL values, then this index delivers rows in
       104685  +    ** the required order.
       104686  +    **
       104687  +    ** It is not possible for any of the first nEqCol index fields to be
       104688  +    ** NULL (since the corresponding "=" operator in the WHERE clause would 
       104689  +    ** not be true). So if all remaining index columns have NOT NULL 
       104690  +    ** constaints attached to them, we can be confident that the visited
       104691  +    ** index entries are free of NULLs.  */
       104692  +    for(i=nEqCol; i<pIdx->nColumn; i++){
       104693  +      if( aCol[pIdx->aiColumn[i]].notNull==0 ) break;
       104694  +    }
       104695  +    return (i==pIdx->nColumn);
104258 104696     }
104259 104697     return 0;
104260 104698   }
104261 104699   
104262 104700   /*
104263 104701   ** Prepare a crude estimate of the logarithm of the input value.
104264 104702   ** The results need not be exact.  This is only used for estimating
................................................................................
106911 107349             explainOneScan(
106912 107350                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
106913 107351             );
106914 107352             if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
106915 107353               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
106916 107354               int r;
106917 107355               r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
106918         -                                         regRowid);
       107356  +                                         regRowid, 0);
106919 107357               sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
106920 107358                                    sqlite3VdbeCurrentAddr(v)+2, r, iSet);
106921 107359             }
106922 107360             sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
106923 107361   
106924 107362             /* The pSubWInfo->untestedTerms flag means that this OR term
106925 107363             ** contained one or more AND term from a notReady table.  The
................................................................................
107824 108262   
107825 108263   /*
107826 108264   ** An instance of this structure is used to store the LIKE,
107827 108265   ** GLOB, NOT LIKE, and NOT GLOB operators.
107828 108266   */
107829 108267   struct LikeOp {
107830 108268     Token eOperator;  /* "like" or "glob" or "regexp" */
107831         -  int not;         /* True if the NOT keyword is present */
       108269  +  int bNot;         /* True if the NOT keyword is present */
107832 108270   };
107833 108271   
107834 108272   /*
107835 108273   ** An instance of the following structure describes the event of a
107836 108274   ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
107837 108275   ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
107838 108276   **
................................................................................
108003 108441   #ifndef YYSTACKDEPTH
108004 108442   #define YYSTACKDEPTH 100
108005 108443   #endif
108006 108444   #define sqlite3ParserARG_SDECL Parse *pParse;
108007 108445   #define sqlite3ParserARG_PDECL ,Parse *pParse
108008 108446   #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
108009 108447   #define sqlite3ParserARG_STORE yypParser->pParse = pParse
108010         -#define YYNSTATE 629
       108448  +#define YYNSTATE 627
108011 108449   #define YYNRULE 327
108012 108450   #define YYFALLBACK 1
108013 108451   #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
108014 108452   #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
108015 108453   #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
108016 108454   
108017 108455   /* The yyzerominor constant is used to initialize instances of
................................................................................
108074 108512   **                     yy_action.  Used to detect hash collisions.
108075 108513   **  yy_shift_ofst[]    For each state, the offset into yy_action for
108076 108514   **                     shifting terminals.
108077 108515   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
108078 108516   **                     shifting non-terminals after a reduce.
108079 108517   **  yy_default[]       Default action for each state.
108080 108518   */
108081         -#define YY_ACTTAB_COUNT (1580)
       108519  +#define YY_ACTTAB_COUNT (1564)
108082 108520   static const YYACTIONTYPE yy_action[] = {
108083         - /*     0 */   310,  328,  574,  573,   15,  172,  187,  596,   56,   56,
       108521  + /*     0 */   309,  955,  184,  417,    2,  171,  624,  594,   56,   56,
108084 108522    /*    10 */    56,   56,   49,   54,   54,   54,   54,   53,   53,   52,
108085         - /*    20 */    52,   52,   51,  234,  622,  621,  626,  622,  621,  299,
108086         - /*    30 */   589,  583,   56,   56,   56,   56,  236,   54,   54,   54,
108087         - /*    40 */    54,   53,   53,   52,   52,   52,   51,  234,  351,   57,
108088         - /*    50 */    58,   48,  581,  580,  582,  582,   55,   55,   56,   56,
108089         - /*    60 */    56,   56,  570,   54,   54,   54,   54,   53,   53,   52,
108090         - /*    70 */    52,   52,   51,  234,  310,  596,  326,  607,  233,  232,
       108523  + /*    20 */    52,   52,   51,  233,  620,  619,  298,  620,  619,  234,
       108524  + /*    30 */   587,  581,   56,   56,   56,   56,   19,   54,   54,   54,
       108525  + /*    40 */    54,   53,   53,   52,   52,   52,   51,  233,  605,   57,
       108526  + /*    50 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
       108527  + /*    60 */    56,   56,  541,   54,   54,   54,   54,   53,   53,   52,
       108528  + /*    70 */    52,   52,   51,  233,  309,  594,  325,  196,  195,  194,
108091 108529    /*    80 */    33,   54,   54,   54,   54,   53,   53,   52,   52,   52,
108092         - /*    90 */    51,  234,  619,  618,  326,  619,  618,  166,  605,  492,
108093         - /*   100 */   381,  378,  377,  235,  589,  583,  554,  495,    1,   59,
108094         - /*   110 */    19,  376,  622,  621,   53,   53,   52,   52,   52,   51,
108095         - /*   120 */   234,  571,  571,   57,   58,   48,  581,  580,  582,  582,
108096         - /*   130 */    55,   55,   56,   56,   56,   56,  215,   54,   54,   54,
108097         - /*   140 */    54,   53,   53,   52,   52,   52,   51,  234,  310,  224,
108098         - /*   150 */    50,   47,  147,  177,  139,  281,  384,  276,  383,  169,
108099         - /*   160 */   408,  553,  578,  578,  622,  621,  272,  224,  439,  550,
108100         - /*   170 */   552,  410,  139,  281,  384,  276,  383,  169,  589,  583,
108101         - /*   180 */   619,  618,  280,  620,  272,  195,  413,  309,  440,  441,
108102         - /*   190 */   567,  491,  214,  279,  560,  600,   92,   57,   58,   48,
108103         - /*   200 */   581,  580,  582,  582,   55,   55,   56,   56,   56,   56,
108104         - /*   210 */   559,   54,   54,   54,   54,   53,   53,   52,   52,   52,
108105         - /*   220 */    51,  234,  310,  464,  233,  232,  558,  133,  519,   50,
108106         - /*   230 */    47,  147,  619,  618,  565,  436,  397,  515,  514,  518,
108107         - /*   240 */   410,  387,  438,  389,  437,  622,  621,  442,  570,  433,
108108         - /*   250 */   203,  390,  589,  583,    6,  413,  166,  670,  250,  381,
108109         - /*   260 */   378,  377,  525,  190,  600,   92,  594,  571,  571,  465,
108110         - /*   270 */   376,   57,   58,   48,  581,  580,  582,  582,   55,   55,
108111         - /*   280 */    56,   56,   56,   56,  599,   54,   54,   54,   54,   53,
108112         - /*   290 */    53,   52,   52,   52,   51,  234,  310,  592,  592,  592,
108113         - /*   300 */   490,  182,  247,  548,  249,  397,  273,  410,    7,  439,
108114         - /*   310 */   398,  606,   67,  619,  618,  620,  472,  256,  347,  255,
108115         - /*   320 */   473,  620,  413,  576,  620,   65,  589,  583,  236,  440,
108116         - /*   330 */   336,  600,   92,   68,  364,  192,  481,  622,  621,  547,
108117         - /*   340 */   622,  621,  560,  323,  207,   57,   58,   48,  581,  580,
108118         - /*   350 */   582,  582,   55,   55,   56,   56,   56,   56,  559,   54,
108119         - /*   360 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  234,
108120         - /*   370 */   310,  410,  397,  146,  558,  531,  401,  348,  599,  166,
108121         - /*   380 */   248,  204,  381,  378,  377,  541,  413,  171,  337,  570,
108122         - /*   390 */   622,  621,   40,  376,   38,  600,   74,  465,  548,  490,
108123         - /*   400 */   589,  583,  532,  350,  579,  619,  618,  297,  619,  618,
108124         - /*   410 */   480,   67,  470,   39,  620,  599,  406,  574,  573,   57,
108125         - /*   420 */    58,   48,  581,  580,  582,  582,   55,   55,   56,   56,
108126         - /*   430 */    56,   56,  577,   54,   54,   54,   54,   53,   53,   52,
108127         - /*   440 */    52,   52,   51,  234,  310,  256,  347,  255,  530,   52,
108128         - /*   450 */    52,   52,   51,  234,  345,  564,  236,  386,  619,  618,
108129         - /*   460 */   957,  185,  418,    2,  408,  410,  578,  578,  198,  197,
108130         - /*   470 */   196,  499,  183,  167,  589,  583,  671,  570,  505,  506,
108131         - /*   480 */   413,  267,  601,  672,  546,  208,  602,   36,  601,  600,
108132         - /*   490 */    91,  468,  602,   57,   58,   48,  581,  580,  582,  582,
108133         - /*   500 */    55,   55,   56,   56,   56,   56,  202,   54,   54,   54,
108134         - /*   510 */    54,   53,   53,   52,   52,   52,   51,  234,  310,  599,
108135         - /*   520 */   157,  408,  527,  578,  578,  263,  490,  265,  410,  873,
108136         - /*   530 */   410,  474,  474,  366,  373,  410,  504,  428,   67,  290,
108137         - /*   540 */   599,  620,  352,  413,  408,  413,  578,  578,  589,  583,
108138         - /*   550 */   413,  382,  600,   92,  600,   16,  543,   62,  503,  600,
108139         - /*   560 */    92,  408,  346,  578,  578,  168,   45,   57,   58,   48,
108140         - /*   570 */   581,  580,  582,  582,   55,   55,   56,   56,   56,   56,
108141         - /*   580 */   200,   54,   54,   54,   54,   53,   53,   52,   52,   52,
108142         - /*   590 */    51,  234,  310,  393,  395,  534,  510,  617,  616,  615,
108143         - /*   600 */   318,  314,  172,   66,  596,  410,  338,  596,  324,  571,
108144         - /*   610 */   571,   50,   47,  147,  599,  629,  627,  330,  539,  315,
108145         - /*   620 */   413,   30,  589,  583,  272,  236,  199,  144,  176,  600,
108146         - /*   630 */    73,  420,  947,  620,  947,  420,  946,  351,  946,  175,
108147         - /*   640 */   596,   57,   58,   48,  581,  580,  582,  582,   55,   55,
108148         - /*   650 */    56,   56,   56,   56,  410,   54,   54,   54,   54,   53,
108149         - /*   660 */    53,   52,   52,   52,   51,  234,  310,  261,  410,  413,
108150         - /*   670 */   269,  208,  596,  363,  410,  596,  424,  360,  600,   69,
108151         - /*   680 */   424,  327,  620,  413,   50,   47,  147,  410,  358,  413,
108152         - /*   690 */   575,  553,  600,   94,  483,  509,  589,  583,  600,   97,
108153         - /*   700 */   552,  484,  413,  620,  188,  599,  551,  563,  596,  566,
108154         - /*   710 */   334,  600,   95,  205,  201,   57,   58,   48,  581,  580,
108155         - /*   720 */   582,  582,   55,   55,   56,   56,   56,   56,  352,   54,
108156         - /*   730 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  234,
108157         - /*   740 */   310,  410,  261,  410,  167,   22,  356,  599,  359,  623,
108158         - /*   750 */    50,   47,  147,  548,  357,  562,  413,  620,  413,  332,
108159         - /*   760 */   523,  270,  410,  167,  620,  600,  104,  600,  103,  603,
108160         - /*   770 */   589,  583,  339,  539,  304,  423,  222,  413,  174,  304,
108161         - /*   780 */   422,  561,  567,  405,  214,  260,  600,  106,  620,   57,
108162         - /*   790 */    58,   48,  581,  580,  582,  582,   55,   55,   56,   56,
108163         - /*   800 */    56,   56,  410,   54,   54,   54,   54,   53,   53,   52,
108164         - /*   810 */    52,   52,   51,  234,  310,  410,  557,  413,  410,  421,
108165         - /*   820 */   273,   35,  512,  146,  421,   12,  600,  107,  213,  144,
108166         - /*   830 */   413,  410,   32,  413,  410,  620,  365,  353,  358,  600,
108167         - /*   840 */   134,   11,  600,  135,  589,  583,  413,   21,  548,  413,
108168         - /*   850 */   316,  148,  620,  620,  170,  600,   98,  223,  600,  102,
108169         - /*   860 */   374,  168,  167,   57,   58,   48,  581,  580,  582,  582,
108170         - /*   870 */    55,   55,   56,   56,   56,   56,  410,   54,   54,   54,
108171         - /*   880 */    54,   53,   53,   52,   52,   52,   51,  234,  310,  410,
108172         - /*   890 */   273,  413,  410,  273,  212,  469,  410,  167,  628,    2,
108173         - /*   900 */   600,  101,  545,  221,  413,  620,  130,  413,  620,  410,
108174         - /*   910 */   539,  413,  537,  600,   93,  315,  600,  100,  589,  583,
108175         - /*   920 */   600,   77,  425,  305,  413,  620,  254,  322,  599,  458,
108176         - /*   930 */   320,  171,  543,  600,   96,  521,  520,   57,   58,   48,
108177         - /*   940 */   581,  580,  582,  582,   55,   55,   56,   56,   56,   56,
108178         - /*   950 */   410,   54,   54,   54,   54,   53,   53,   52,   52,   52,
108179         - /*   960 */    51,  234,  310,  410,  273,  413,  410,  457,  358,   35,
108180         - /*   970 */   426,  230,  306,  319,  600,  138,  467,  520,  413,  620,
108181         - /*   980 */   143,  413,  410,  620,  410,  353,  529,  600,  137,  142,
108182         - /*   990 */   600,  136,  589,  583,  604,  261,  528,  413,  229,  413,
108183         - /*  1000 */   620,  321,  495,   28,  543,  543,  600,   76,  600,   90,
108184         - /*  1010 */   620,   57,   46,   48,  581,  580,  582,  582,   55,   55,
108185         - /*  1020 */    56,   56,   56,   56,  410,   54,   54,   54,   54,   53,
108186         - /*  1030 */    53,   52,   52,   52,   51,  234,  310,  261,  451,  413,
108187         - /*  1040 */   410,  211,  611,  285,  283,  610,  609,  502,  600,   89,
108188         - /*  1050 */   380,  217,  620,  128,  140,  413,  220,  620,  410,  409,
108189         - /*  1060 */   620,  620,  588,  587,  600,   75,  589,  583,  271,  620,
108190         - /*  1070 */    51,  234,  127,  413,  620,  599,  627,  330,   27,  375,
108191         - /*  1080 */   449,  279,  600,   88,  585,  584,   58,   48,  581,  580,
108192         - /*  1090 */   582,  582,   55,   55,   56,   56,   56,   56,  410,   54,
108193         - /*  1100 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  234,
108194         - /*  1110 */   310,  586,  410,  413,  410,  261,  593,  165,  399,  556,
108195         - /*  1120 */   126,  371,  600,   87,  478,  186,  123,  413,  367,  413,
108196         - /*  1130 */   620,  620,  410,  620,  620,  410,  600,   99,  600,   86,
108197         - /*  1140 */   589,  583,  475,  122,  258,  171,  471,  413,  160,  121,
108198         - /*  1150 */   413,   14,  159,  463,   25,   24,  600,   17,  448,  600,
108199         - /*  1160 */    85,   48,  581,  580,  582,  582,   55,   55,   56,   56,
108200         - /*  1170 */    56,   56,  158,   54,   54,   54,   54,   53,   53,   52,
108201         - /*  1180 */    52,   52,   51,  234,   44,  404,  261,    3,  544,  261,
108202         - /*  1190 */   540,  414,  621,  460,  119,  118,  538,  275,   10,  349,
108203         - /*  1200 */     4,  620,  407,  620,  620,  620,  116,   44,  404,  410,
108204         - /*  1210 */     3,  620,  620,  410,  414,  621,  456,  454,  252,  450,
108205         - /*  1220 */   508,  402,  111,  109,  413,  407,  155,  444,  413,  447,
108206         - /*  1230 */   435,  565,  219,  600,   84,  620,  108,  600,   83,   64,
108207         - /*  1240 */   434,  417,  625,  150,  402,  333,  410,  237,  238,  124,
108208         - /*  1250 */   274,   41,   42,  533,  565,  206,  189,  261,   43,  412,
108209         - /*  1260 */   411,  413,  261,  594,  488,  620,  329,  149,  419,  268,
108210         - /*  1270 */   600,   72,  620,  266,   41,   42,  181,  620,  410,  620,
108211         - /*  1280 */   105,   43,  412,  411,  620,  624,  594,  614,  620,  599,
108212         - /*  1290 */   228,  125,  313,  413,  592,  592,  592,  591,  590,   13,
108213         - /*  1300 */   218,  410,  600,   71,  236,  244,   44,  404,  264,    3,
108214         - /*  1310 */   312,  613,  340,  414,  621,  180,  413,  592,  592,  592,
108215         - /*  1320 */   591,  590,   13,  620,  407,  600,   82,  410,  416,   34,
108216         - /*  1330 */   404,  410,    3,  410,  262,  410,  414,  621,  612,  331,
108217         - /*  1340 */   178,  415,  413,  402,    8,  236,  413,  407,  413,  620,
108218         - /*  1350 */   413,  600,   81,  565,  257,  600,   80,  600,   70,  600,
108219         - /*  1360 */    18,  598,  361,  462,  461,   30,  402,  294,   31,  620,
108220         - /*  1370 */   293,  354,  251,   41,   42,  410,  565,  620,  620,  620,
108221         - /*  1380 */    43,  412,  411,  453,  396,  594,  620,  620,  394,   61,
108222         - /*  1390 */   413,  292,  443,  622,  621,  243,   41,   42,  620,  600,
108223         - /*  1400 */    79,  597,  291,   43,  412,  411,   60,  620,  594,  240,
108224         - /*  1410 */   620,  410,  231,   37,  555,  173,  592,  592,  592,  591,
108225         - /*  1420 */   590,   13,  216,  239,  620,  184,  413,  302,  301,  300,
108226         - /*  1430 */   179,  298,  388,  565,  452,  600,   78,  286,  620,  592,
108227         - /*  1440 */   592,  592,  591,  590,   13,  429,   29,  413,  151,  289,
108228         - /*  1450 */   242,  145,  392,  194,  193,  288,  600,    9,  542,  241,
108229         - /*  1460 */   620,  525,  391,  284,  620,  594,  620,  620,  522,  536,
108230         - /*  1470 */   620,  535,  153,  385,  465,  516,  282,  325,  154,  517,
108231         - /*  1480 */   277,  152,  512,  511,  513,  129,  226,  308,  487,  486,
108232         - /*  1490 */   485,  164,  372,  493,  307,  227,  592,  592,  592,  225,
108233         - /*  1500 */   479,  163,  368,  370,  162,  476,  210,  477,   26,  259,
108234         - /*  1510 */   161,  466,  362,  141,  132,  120,  117,  455,  156,  115,
108235         - /*  1520 */   344,  343,  256,  342,  245,  114,  113,  446,  311,  112,
108236         - /*  1530 */    23,  317,  432,  236,  131,  431,  110,  430,   20,  427,
108237         - /*  1540 */   608,  595,  295,   63,  379,  287,  509,  191,  278,  403,
108238         - /*  1550 */   572,  569,  497,  498,  496,  494,  335,  459,  445,  303,
108239         - /*  1560 */   296,  246,  341,  355,    5,  568,  369,  507,  253,  549,
108240         - /*  1570 */   526,  209,  400,  501,  500,  524,  234,  958,  489,  482,
       108530  + /*    90 */    51,  233,  617,  616,  165,  617,  616,  380,  377,  376,
       108531  + /*   100 */   407,  532,  576,  576,  587,  581,  303,  422,  375,   59,
       108532  + /*   110 */    53,   53,   52,   52,   52,   51,  233,   50,   47,  146,
       108533  + /*   120 */   574,  545,   65,   57,   58,   48,  579,  578,  580,  580,
       108534  + /*   130 */    55,   55,   56,   56,   56,   56,  213,   54,   54,   54,
       108535  + /*   140 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  223,
       108536  + /*   150 */   539,  420,  170,  176,  138,  280,  383,  275,  382,  168,
       108537  + /*   160 */   489,  551,  409,  668,  620,  619,  271,  438,  409,  438,
       108538  + /*   170 */   550,  604,   67,  482,  507,  618,  599,  412,  587,  581,
       108539  + /*   180 */   600,  483,  618,  412,  618,  598,   91,  439,  440,  439,
       108540  + /*   190 */   335,  598,   73,  669,  222,  266,  480,   57,   58,   48,
       108541  + /*   200 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
       108542  + /*   210 */   670,   54,   54,   54,   54,   53,   53,   52,   52,   52,
       108543  + /*   220 */    51,  233,  309,  279,  232,  231,    1,  132,  200,  385,
       108544  + /*   230 */   620,  619,  617,  616,  278,  435,  289,  563,  175,  262,
       108545  + /*   240 */   409,  264,  437,  497,  436,  166,  441,  568,  336,  568,
       108546  + /*   250 */   201,  537,  587,  581,  599,  412,  165,  594,  600,  380,
       108547  + /*   260 */   377,  376,  597,  598,   92,  523,  618,  569,  569,  592,
       108548  + /*   270 */   375,   57,   58,   48,  579,  578,  580,  580,   55,   55,
       108549  + /*   280 */    56,   56,   56,   56,  597,   54,   54,   54,   54,   53,
       108550  + /*   290 */    53,   52,   52,   52,   51,  233,  309,  463,  617,  616,
       108551  + /*   300 */   590,  590,  590,  174,  272,  396,  409,  272,  409,  548,
       108552  + /*   310 */   397,  620,  619,   68,  326,  620,  619,  620,  619,  618,
       108553  + /*   320 */   546,  412,  618,  412,  471,  594,  587,  581,  472,  598,
       108554  + /*   330 */    92,  598,   92,   52,   52,   52,   51,  233,  513,  512,
       108555  + /*   340 */   206,  322,  363,  464,  221,   57,   58,   48,  579,  578,
       108556  + /*   350 */   580,  580,   55,   55,   56,   56,   56,   56,  529,   54,
       108557  + /*   360 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
       108558  + /*   370 */   309,  396,  409,  396,  597,  372,  386,  530,  347,  617,
       108559  + /*   380 */   616,  575,  202,  617,  616,  617,  616,  412,  620,  619,
       108560  + /*   390 */   145,  255,  346,  254,  577,  598,   74,  351,   45,  489,
       108561  + /*   400 */   587,  581,  235,  189,  464,  544,  167,  296,  187,  469,
       108562  + /*   410 */   479,   67,   62,   39,  618,  546,  597,  345,  573,   57,
       108563  + /*   420 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
       108564  + /*   430 */    56,   56,    6,   54,   54,   54,   54,   53,   53,   52,
       108565  + /*   440 */    52,   52,   51,  233,  309,  562,  558,  407,  528,  576,
       108566  + /*   450 */   576,  344,  255,  346,  254,  182,  617,  616,  503,  504,
       108567  + /*   460 */   314,  409,  557,  235,  166,  271,  409,  352,  564,  181,
       108568  + /*   470 */   407,  546,  576,  576,  587,  581,  412,  537,  556,  561,
       108569  + /*   480 */   517,  412,  618,  249,  598,   16,    7,   36,  467,  598,
       108570  + /*   490 */    92,  516,  618,   57,   58,   48,  579,  578,  580,  580,
       108571  + /*   500 */    55,   55,   56,   56,   56,   56,  541,   54,   54,   54,
       108572  + /*   510 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  327,
       108573  + /*   520 */   572,  571,  525,  558,  560,  394,  871,  246,  409,  248,
       108574  + /*   530 */   171,  392,  594,  219,  407,  409,  576,  576,  502,  557,
       108575  + /*   540 */   364,  145,  510,  412,  407,  229,  576,  576,  587,  581,
       108576  + /*   550 */   412,  598,   92,  381,  269,  556,  166,  400,  598,   69,
       108577  + /*   560 */   501,  419,  945,  199,  945,  198,  546,   57,   58,   48,
       108578  + /*   570 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
       108579  + /*   580 */   568,   54,   54,   54,   54,   53,   53,   52,   52,   52,
       108580  + /*   590 */    51,  233,  309,  317,  419,  944,  508,  944,  308,  597,
       108581  + /*   600 */   594,  565,  490,  212,  173,  247,  423,  615,  614,  613,
       108582  + /*   610 */   323,  197,  143,  405,  572,  571,  489,   66,   50,   47,
       108583  + /*   620 */   146,  594,  587,  581,  232,  231,  559,  427,   67,  555,
       108584  + /*   630 */    15,  618,  186,  543,  303,  421,   35,  206,  432,  423,
       108585  + /*   640 */   552,   57,   58,   48,  579,  578,  580,  580,   55,   55,
       108586  + /*   650 */    56,   56,   56,   56,  205,   54,   54,   54,   54,   53,
       108587  + /*   660 */    53,   52,   52,   52,   51,  233,  309,  569,  569,  260,
       108588  + /*   670 */   268,  597,   12,  373,  568,  166,  409,  313,  409,  420,
       108589  + /*   680 */   409,  473,  473,  365,  618,   50,   47,  146,  597,  594,
       108590  + /*   690 */   468,  412,  166,  412,  351,  412,  587,  581,   32,  598,
       108591  + /*   700 */    94,  598,   97,  598,   95,  627,  625,  329,  142,   50,
       108592  + /*   710 */    47,  146,  333,  349,  358,   57,   58,   48,  579,  578,
       108593  + /*   720 */   580,  580,   55,   55,   56,   56,   56,   56,  409,   54,
       108594  + /*   730 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
       108595  + /*   740 */   309,  409,  388,  412,  409,   22,  565,  404,  212,  362,
       108596  + /*   750 */   389,  598,  104,  359,  409,  156,  412,  409,  603,  412,
       108597  + /*   760 */   537,  331,  569,  569,  598,  103,  493,  598,  105,  412,
       108598  + /*   770 */   587,  581,  412,  260,  549,  618,   11,  598,  106,  521,
       108599  + /*   780 */   598,  133,  169,  457,  456,  170,   35,  601,  618,   57,
       108600  + /*   790 */    58,   48,  579,  578,  580,  580,   55,   55,   56,   56,
       108601  + /*   800 */    56,   56,  409,   54,   54,   54,   54,   53,   53,   52,
       108602  + /*   810 */    52,   52,   51,  233,  309,  409,  259,  412,  409,   50,
       108603  + /*   820 */    47,  146,  357,  318,  355,  598,  134,  527,  352,  337,
       108604  + /*   830 */   412,  409,  356,  412,  357,  409,  357,  618,  598,   98,
       108605  + /*   840 */   129,  598,  102,  618,  587,  581,  412,   21,  235,  618,
       108606  + /*   850 */   412,  618,  211,  143,  598,  101,   30,  167,  598,   93,
       108607  + /*   860 */   350,  535,  203,   57,   58,   48,  579,  578,  580,  580,
       108608  + /*   870 */    55,   55,   56,   56,   56,   56,  409,   54,   54,   54,
       108609  + /*   880 */    54,   53,   53,   52,   52,   52,   51,  233,  309,  409,
       108610  + /*   890 */   526,  412,  409,  425,  215,  305,  597,  551,  141,  598,
       108611  + /*   900 */   100,   40,  409,   38,  412,  409,  550,  412,  409,  228,
       108612  + /*   910 */   220,  314,  598,   77,  500,  598,   96,  412,  587,  581,
       108613  + /*   920 */   412,  338,  253,  412,  218,  598,  137,  379,  598,  136,
       108614  + /*   930 */    28,  598,  135,  270,  715,  210,  481,   57,   58,   48,
       108615  + /*   940 */   579,  578,  580,  580,   55,   55,   56,   56,   56,   56,
       108616  + /*   950 */   409,   54,   54,   54,   54,   53,   53,   52,   52,   52,
       108617  + /*   960 */    51,  233,  309,  409,  272,  412,  409,  315,  147,  597,
       108618  + /*   970 */   272,  626,    2,  598,   76,  209,  409,  127,  412,  618,
       108619  + /*   980 */   126,  412,  409,  621,  235,  618,  598,   90,  374,  598,
       108620  + /*   990 */    89,  412,  587,  581,   27,  260,  350,  412,  618,  598,
       108621  + /*  1000 */    75,  321,  541,  541,  125,  598,   88,  320,  278,  597,
       108622  + /*  1010 */   618,   57,   46,   48,  579,  578,  580,  580,   55,   55,
       108623  + /*  1020 */    56,   56,   56,   56,  409,   54,   54,   54,   54,   53,
       108624  + /*  1030 */    53,   52,   52,   52,   51,  233,  309,  409,  450,  412,
       108625  + /*  1040 */   164,  284,  282,  272,  609,  424,  304,  598,   87,  370,
       108626  + /*  1050 */   409,  477,  412,  409,  608,  409,  607,  602,  618,  618,
       108627  + /*  1060 */   598,   99,  586,  585,  122,  412,  587,  581,  412,  618,
       108628  + /*  1070 */   412,  618,  618,  598,   86,  366,  598,   17,  598,   85,
       108629  + /*  1080 */   319,  185,  519,  518,  583,  582,   58,   48,  579,  578,
       108630  + /*  1090 */   580,  580,   55,   55,   56,   56,   56,   56,  409,   54,
       108631  + /*  1100 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  233,
       108632  + /*  1110 */   309,  584,  409,  412,  409,  260,  260,  260,  408,  591,
       108633  + /*  1120 */   474,  598,   84,  170,  409,  466,  518,  412,  121,  412,
       108634  + /*  1130 */   618,  618,  618,  618,  618,  598,   83,  598,   72,  412,
       108635  + /*  1140 */   587,  581,   51,  233,  625,  329,  470,  598,   71,  257,
       108636  + /*  1150 */   159,  120,   14,  462,  157,  158,  117,  260,  448,  447,
       108637  + /*  1160 */   446,   48,  579,  578,  580,  580,   55,   55,   56,   56,
       108638  + /*  1170 */    56,   56,  618,   54,   54,   54,   54,   53,   53,   52,
       108639  + /*  1180 */    52,   52,   51,  233,   44,  403,  260,    3,  409,  459,
       108640  + /*  1190 */   260,  413,  619,  118,  398,   10,   25,   24,  554,  348,
       108641  + /*  1200 */   217,  618,  406,  412,  409,  618,    4,   44,  403,  618,
       108642  + /*  1210 */     3,  598,   82,  618,  413,  619,  455,  542,  115,  412,
       108643  + /*  1220 */   538,  401,  536,  274,  506,  406,  251,  598,   81,  216,
       108644  + /*  1230 */   273,  563,  618,  243,  453,  618,  154,  618,  618,  618,
       108645  + /*  1240 */   449,  416,  623,  110,  401,  618,  409,  236,   64,  123,
       108646  + /*  1250 */   487,   41,   42,  531,  563,  204,  409,  267,   43,  411,
       108647  + /*  1260 */   410,  412,  265,  592,  108,  618,  107,  434,  332,  598,
       108648  + /*  1270 */    80,  412,  618,  263,   41,   42,  443,  618,  409,  598,
       108649  + /*  1280 */    70,   43,  411,  410,  433,  261,  592,  149,  618,  597,
       108650  + /*  1290 */   256,  237,  188,  412,  590,  590,  590,  589,  588,   13,
       108651  + /*  1300 */   618,  598,   18,  328,  235,  618,   44,  403,  360,    3,
       108652  + /*  1310 */   418,  461,  339,  413,  619,  227,  124,  590,  590,  590,
       108653  + /*  1320 */   589,  588,   13,  618,  406,  409,  618,  409,  139,   34,
       108654  + /*  1330 */   403,  387,    3,  148,  622,  312,  413,  619,  311,  330,
       108655  + /*  1340 */   412,  460,  412,  401,  180,  353,  412,  406,  598,   79,
       108656  + /*  1350 */   598,   78,  250,  563,  598,    9,  618,  612,  611,  610,
       108657  + /*  1360 */   618,    8,  452,  442,  242,  415,  401,  618,  239,  235,
       108658  + /*  1370 */   179,  238,  428,   41,   42,  288,  563,  618,  618,  618,
       108659  + /*  1380 */    43,  411,  410,  618,  144,  592,  618,  618,  177,   61,
       108660  + /*  1390 */   618,  596,  391,  620,  619,  287,   41,   42,  414,  618,
       108661  + /*  1400 */   293,   30,  393,   43,  411,  410,  292,  618,  592,   31,
       108662  + /*  1410 */   618,  395,  291,   60,  230,   37,  590,  590,  590,  589,
       108663  + /*  1420 */   588,   13,  214,  553,  183,  290,  172,  301,  300,  299,
       108664  + /*  1430 */   178,  297,  595,  563,  451,   29,  285,  390,  540,  590,
       108665  + /*  1440 */   590,  590,  589,  588,   13,  283,  520,  534,  150,  533,
       108666  + /*  1450 */   241,  281,  384,  192,  191,  324,  515,  514,  276,  240,
       108667  + /*  1460 */   510,  523,  307,  511,  128,  592,  509,  225,  226,  486,
       108668  + /*  1470 */   485,  224,  152,  491,  464,  306,  484,  163,  153,  371,
       108669  + /*  1480 */   478,  151,  162,  258,  369,  161,  367,  208,  475,  476,
       108670  + /*  1490 */    26,  160,  465,  140,  361,  131,  590,  590,  590,  116,
       108671  + /*  1500 */   119,  454,  343,  155,  114,  342,  113,  112,  445,  111,
       108672  + /*  1510 */   130,  109,  431,  316,  426,  430,   23,  429,   20,  606,
       108673  + /*  1520 */   190,  507,  255,  341,  244,   63,  294,  593,  310,  570,
       108674  + /*  1530 */   277,  402,  354,  235,  567,  496,  495,  492,  494,  302,
       108675  + /*  1540 */   458,  378,  286,  245,  566,    5,  252,  547,  193,  444,
       108676  + /*  1550 */   233,  340,  207,  524,  368,  505,  334,  522,  499,  399,
       108677  + /*  1560 */   295,  498,  956,  488,
108241 108678   };
108242 108679   static const YYCODETYPE yy_lookahead[] = {
108243         - /*     0 */    19,  169,  170,  171,   22,   24,   24,   26,   77,   78,
       108680  + /*     0 */    19,  142,  143,  144,  145,   24,    1,   26,   77,   78,
108244 108681    /*    10 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
108245         - /*    20 */    89,   90,   91,   92,   26,   27,    1,   26,   27,   15,
108246         - /*    30 */    49,   50,   77,   78,   79,   80,  116,   82,   83,   84,
108247         - /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,  128,   68,
       108682  + /*    20 */    89,   90,   91,   92,   26,   27,   15,   26,   27,  197,
       108683  + /*    30 */    49,   50,   77,   78,   79,   80,  204,   82,   83,   84,
       108684  + /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,   23,   68,
108248 108685    /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
108249         - /*    60 */    79,   80,  230,   82,   83,   84,   85,   86,   87,   88,
108250         - /*    70 */    89,   90,   91,   92,   19,   94,   19,   23,   86,   87,
       108686  + /*    60 */    79,   80,  166,   82,   83,   84,   85,   86,   87,   88,
       108687  + /*    70 */    89,   90,   91,   92,   19,   94,   19,  105,  106,  107,
108251 108688    /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
108252         - /*    90 */    91,   92,   94,   95,   19,   94,   95,   96,  172,  173,
108253         - /*   100 */    99,  100,  101,  197,   49,   50,  177,  181,   22,   54,
108254         - /*   110 */   204,  110,   26,   27,   86,   87,   88,   89,   90,   91,
108255         - /*   120 */    92,  129,  130,   68,   69,   70,   71,   72,   73,   74,
       108689  + /*    90 */    91,   92,   94,   95,   96,   94,   95,   99,  100,  101,
       108690  + /*   100 */   112,  205,  114,  115,   49,   50,   22,   23,  110,   54,
       108691  + /*   110 */    86,   87,   88,   89,   90,   91,   92,  221,  222,  223,
       108692  + /*   120 */    23,  120,   25,   68,   69,   70,   71,   72,   73,   74,
108256 108693    /*   130 */    75,   76,   77,   78,   79,   80,   22,   82,   83,   84,
108257 108694    /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   92,
108258         - /*   150 */   221,  222,  223,   96,   97,   98,   99,  100,  101,  102,
108259         - /*   160 */   112,   32,  114,  115,   26,   27,  109,   92,  150,   25,
108260         - /*   170 */    41,  150,   97,   98,   99,  100,  101,  102,   49,   50,
108261         - /*   180 */    94,   95,   98,  165,  109,   25,  165,  163,  170,  171,
108262         - /*   190 */   166,  167,  168,  109,   12,  174,  175,   68,   69,   70,
       108695  + /*   150 */    23,   67,   25,   96,   97,   98,   99,  100,  101,  102,
       108696  + /*   160 */   150,   32,  150,  118,   26,   27,  109,  150,  150,  150,
       108697  + /*   170 */    41,  161,  162,  180,  181,  165,  113,  165,   49,   50,
       108698  + /*   180 */   117,  188,  165,  165,  165,  173,  174,  170,  171,  170,
       108699  + /*   190 */   171,  173,  174,  118,  184,   16,  186,   68,   69,   70,
108263 108700    /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
108264         - /*   210 */    28,   82,   83,   84,   85,   86,   87,   88,   89,   90,
108265         - /*   220 */    91,   92,   19,   11,   86,   87,   44,   24,   46,  221,
108266         - /*   230 */   222,  223,   94,   95,   66,   97,  215,    7,    8,   57,
108267         - /*   240 */   150,  220,  104,   19,  106,   26,   27,  229,  230,  241,
108268         - /*   250 */   160,   27,   49,   50,   22,  165,   96,  118,   16,   99,
108269         - /*   260 */   100,  101,   94,  119,  174,  175,   98,  129,  130,   57,
       108701  + /*   210 */   118,   82,   83,   84,   85,   86,   87,   88,   89,   90,
       108702  + /*   220 */    91,   92,   19,   98,   86,   87,   22,   24,  160,   88,
       108703  + /*   230 */    26,   27,   94,   95,  109,   97,  224,   66,  118,   60,
       108704  + /*   240 */   150,   62,  104,   23,  106,   25,  229,  230,  229,  230,
       108705  + /*   250 */   160,  150,   49,   50,  113,  165,   96,   26,  117,   99,
       108706  + /*   260 */   100,  101,  194,  173,  174,   94,  165,  129,  130,   98,
108270 108707    /*   270 */   110,   68,   69,   70,   71,   72,   73,   74,   75,   76,
108271 108708    /*   280 */    77,   78,   79,   80,  194,   82,   83,   84,   85,   86,
108272         - /*   290 */    87,   88,   89,   90,   91,   92,   19,  129,  130,  131,
108273         - /*   300 */   150,   23,   60,   25,   62,  215,  150,  150,   76,  150,
108274         - /*   310 */   220,  161,  162,   94,   95,  165,   30,  105,  106,  107,
108275         - /*   320 */    34,  165,  165,   23,  165,   25,   49,   50,  116,  170,
108276         - /*   330 */   171,  174,  175,   22,   48,  185,  186,   26,   27,  120,
108277         - /*   340 */    26,   27,   12,  187,  160,   68,   69,   70,   71,   72,
108278         - /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,   28,   82,
       108709  + /*   290 */    87,   88,   89,   90,   91,   92,   19,   11,   94,   95,
       108710  + /*   300 */   129,  130,  131,  118,  150,  215,  150,  150,  150,   25,
       108711  + /*   310 */   220,   26,   27,   22,  213,   26,   27,   26,   27,  165,
       108712  + /*   320 */    25,  165,  165,  165,   30,   94,   49,   50,   34,  173,
       108713  + /*   330 */   174,  173,  174,   88,   89,   90,   91,   92,    7,    8,
       108714  + /*   340 */   160,  187,   48,   57,  187,   68,   69,   70,   71,   72,
       108715  + /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,   23,   82,
108279 108716    /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
108280         - /*   370 */    19,  150,  215,   95,   44,   23,   46,  220,  194,   96,
108281         - /*   380 */   138,  160,   99,  100,  101,   23,  165,   25,  229,  230,
108282         - /*   390 */    26,   27,  135,  110,  137,  174,  175,   57,  120,  150,
108283         - /*   400 */    49,   50,   88,  219,  113,   94,   95,  158,   94,   95,
108284         - /*   410 */   161,  162,   21,  136,  165,  194,  169,  170,  171,   68,
       108717  + /*   370 */    19,  215,  150,  215,  194,   19,  220,   88,  220,   94,
       108718  + /*   380 */    95,   23,  160,   94,   95,   94,   95,  165,   26,   27,
       108719  + /*   390 */    95,  105,  106,  107,  113,  173,  174,  217,   22,  150,
       108720  + /*   400 */    49,   50,  116,  119,   57,  120,   50,  158,   22,   21,
       108721  + /*   410 */   161,  162,  232,  136,  165,  120,  194,  237,   23,   68,
108285 108722    /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
108286         - /*   430 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
108287         - /*   440 */    89,   90,   91,   92,   19,  105,  106,  107,   23,   88,
108288         - /*   450 */    89,   90,   91,   92,   63,   23,  116,   88,   94,   95,
108289         - /*   460 */   142,  143,  144,  145,  112,  150,  114,  115,  105,  106,
108290         - /*   470 */   107,   23,   23,   25,   49,   50,  118,  230,   97,   98,
108291         - /*   480 */   165,   16,  113,  118,  120,  160,  117,  136,  113,  174,
108292         - /*   490 */   175,  100,  117,   68,   69,   70,   71,   72,   73,   74,
108293         - /*   500 */    75,   76,   77,   78,   79,   80,  160,   82,   83,   84,
108294         - /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  194,
108295         - /*   520 */    25,  112,   23,  114,  115,   60,  150,   62,  150,  138,
108296         - /*   530 */   150,  105,  106,  107,   19,  150,   36,  161,  162,  224,
108297         - /*   540 */   194,  165,  217,  165,  112,  165,  114,  115,   49,   50,
108298         - /*   550 */   165,   51,  174,  175,  174,  175,  166,  232,   58,  174,
108299         - /*   560 */   175,  112,  237,  114,  115,   50,   22,   68,   69,   70,
       108723  + /*   430 */    79,   80,   22,   82,   83,   84,   85,   86,   87,   88,
       108724  + /*   440 */    89,   90,   91,   92,   19,   23,   12,  112,   23,  114,
       108725  + /*   450 */   115,   63,  105,  106,  107,   23,   94,   95,   97,   98,
       108726  + /*   460 */   104,  150,   28,  116,   25,  109,  150,  150,   23,   23,
       108727  + /*   470 */   112,   25,  114,  115,   49,   50,  165,  150,   44,   11,
       108728  + /*   480 */    46,  165,  165,   16,  173,  174,   76,  136,  100,  173,
       108729  + /*   490 */   174,   57,  165,   68,   69,   70,   71,   72,   73,   74,
       108730  + /*   500 */    75,   76,   77,   78,   79,   80,  166,   82,   83,   84,
       108731  + /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  169,
       108732  + /*   520 */   170,  171,   23,   12,   23,  214,  138,   60,  150,   62,
       108733  + /*   530 */    24,  215,   26,  216,  112,  150,  114,  115,   36,   28,
       108734  + /*   540 */   213,   95,  103,  165,  112,  205,  114,  115,   49,   50,
       108735  + /*   550 */   165,  173,  174,   51,   23,   44,   25,   46,  173,  174,
       108736  + /*   560 */    58,   22,   23,   22,   25,  160,  120,   68,   69,   70,
108300 108737    /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
108301         - /*   580 */   160,   82,   83,   84,   85,   86,   87,   88,   89,   90,
108302         - /*   590 */    91,   92,   19,  215,  214,  205,   23,    7,    8,    9,
108303         - /*   600 */   215,  155,   24,   22,   26,  150,   97,   26,  108,  129,
108304         - /*   610 */   130,  221,  222,  223,  194,    0,    1,    2,  150,  104,
108305         - /*   620 */   165,  126,   49,   50,  109,  116,  206,  207,  118,  174,
108306         - /*   630 */   175,   22,   23,  165,   25,   22,   23,  128,   25,  118,
108307         - /*   640 */    26,   68,   69,   70,   71,   72,   73,   74,   75,   76,
108308         - /*   650 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
108309         - /*   660 */    87,   88,   89,   90,   91,   92,   19,  150,  150,  165,
108310         - /*   670 */    23,  160,   94,  227,  150,   94,   67,  231,  174,  175,
108311         - /*   680 */    67,  213,  165,  165,  221,  222,  223,  150,  150,  165,
108312         - /*   690 */    23,   32,  174,  175,  181,  182,   49,   50,  174,  175,
108313         - /*   700 */    41,  188,  165,  165,   22,  194,  177,   11,   94,   23,
108314         - /*   710 */   193,  174,  175,  160,   22,   68,   69,   70,   71,   72,
108315         - /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  217,   82,
       108738  + /*   580 */   230,   82,   83,   84,   85,   86,   87,   88,   89,   90,
       108739  + /*   590 */    91,   92,   19,  215,   22,   23,   23,   25,  163,  194,
       108740  + /*   600 */    94,  166,  167,  168,   25,  138,   67,    7,    8,    9,
       108741  + /*   610 */   108,  206,  207,  169,  170,  171,  150,   22,  221,  222,
       108742  + /*   620 */   223,   26,   49,   50,   86,   87,   23,  161,  162,   23,
       108743  + /*   630 */    22,  165,   24,  120,   22,   23,   25,  160,  241,   67,
       108744  + /*   640 */   176,   68,   69,   70,   71,   72,   73,   74,   75,   76,
       108745  + /*   650 */    77,   78,   79,   80,  160,   82,   83,   84,   85,   86,
       108746  + /*   660 */    87,   88,   89,   90,   91,   92,   19,  129,  130,  150,
       108747  + /*   670 */    23,  194,   35,   23,  230,   25,  150,  155,  150,   67,
       108748  + /*   680 */   150,  105,  106,  107,  165,  221,  222,  223,  194,   94,
       108749  + /*   690 */    23,  165,   25,  165,  217,  165,   49,   50,   25,  173,
       108750  + /*   700 */   174,  173,  174,  173,  174,    0,    1,    2,  118,  221,
       108751  + /*   710 */   222,  223,  193,  219,  237,   68,   69,   70,   71,   72,
       108752  + /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
108316 108753    /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
108317         - /*   740 */    19,  150,  150,  150,   25,   24,   19,  194,  237,  150,
108318         - /*   750 */   221,  222,  223,   25,   27,   23,  165,  165,  165,  242,
108319         - /*   760 */   165,   23,  150,   25,  165,  174,  175,  174,  175,  174,
108320         - /*   770 */    49,   50,  219,  150,   22,   23,  238,  165,   25,   22,
108321         - /*   780 */    23,   23,  166,  167,  168,  193,  174,  175,  165,   68,
       108754  + /*   740 */    19,  150,   19,  165,  150,   24,  166,  167,  168,  227,
       108755  + /*   750 */    27,  173,  174,  231,  150,   25,  165,  150,  172,  165,
       108756  + /*   760 */   150,  242,  129,  130,  173,  174,  180,  173,  174,  165,
       108757  + /*   770 */    49,   50,  165,  150,  176,  165,   35,  173,  174,  165,
       108758  + /*   780 */   173,  174,   35,   23,   23,   25,   25,  173,  165,   68,
108322 108759    /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
108323 108760    /*   800 */    79,   80,  150,   82,   83,   84,   85,   86,   87,   88,
108324         - /*   810 */    89,   90,   91,   92,   19,  150,   23,  165,  150,   67,
108325         - /*   820 */   150,   25,  103,   95,   67,   35,  174,  175,  206,  207,
108326         - /*   830 */   165,  150,   25,  165,  150,  165,  213,  150,  150,  174,
108327         - /*   840 */   175,   35,  174,  175,   49,   50,  165,   52,  120,  165,
108328         - /*   850 */   245,  246,  165,  165,   35,  174,  175,  187,  174,  175,
108329         - /*   860 */    23,   50,   25,   68,   69,   70,   71,   72,   73,   74,
       108761  + /*   810 */    89,   90,   91,   92,   19,  150,  193,  165,  150,  221,
       108762  + /*   820 */   222,  223,  150,  213,   19,  173,  174,   23,  150,   97,
       108763  + /*   830 */   165,  150,   27,  165,  150,  150,  150,  165,  173,  174,
       108764  + /*   840 */    22,  173,  174,  165,   49,   50,  165,   52,  116,  165,
       108765  + /*   850 */   165,  165,  206,  207,  173,  174,  126,   50,  173,  174,
       108766  + /*   860 */   128,   27,  160,   68,   69,   70,   71,   72,   73,   74,
108330 108767    /*   870 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
108331 108768    /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
108332         - /*   890 */   150,  165,  150,  150,  160,   23,  150,   25,  144,  145,
108333         - /*   900 */   174,  175,  120,  216,  165,  165,   22,  165,  165,  150,
108334         - /*   910 */   150,  165,   27,  174,  175,  104,  174,  175,   49,   50,
108335         - /*   920 */   174,  175,  247,  248,  165,  165,  238,  187,  194,   23,
108336         - /*   930 */   187,   25,  166,  174,  175,  190,  191,   68,   69,   70,
       108769  + /*   890 */    23,  165,  150,   23,  216,   25,  194,   32,   39,  173,
       108770  + /*   900 */   174,  135,  150,  137,  165,  150,   41,  165,  150,   52,
       108771  + /*   910 */   238,  104,  173,  174,   29,  173,  174,  165,   49,   50,
       108772  + /*   920 */   165,  219,  238,  165,  238,  173,  174,   52,  173,  174,
       108773  + /*   930 */    22,  173,  174,   23,   23,  160,   25,   68,   69,   70,
108337 108774    /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
108338 108775    /*   950 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
108339         - /*   960 */    91,   92,   19,  150,  150,  165,  150,   23,  150,   25,
108340         - /*   970 */    23,  205,   25,  213,  174,  175,  190,  191,  165,  165,
108341         - /*   980 */   118,  165,  150,  165,  150,  150,   23,  174,  175,   39,
108342         - /*   990 */   174,  175,   49,   50,  173,  150,   23,  165,   52,  165,
108343         - /*  1000 */   165,  187,  181,   22,  166,  166,  174,  175,  174,  175,
       108776  + /*   960 */    91,   92,   19,  150,  150,  165,  150,  245,  246,  194,
       108777  + /*   970 */   150,  144,  145,  173,  174,  160,  150,   22,  165,  165,
       108778  + /*   980 */    22,  165,  150,  150,  116,  165,  173,  174,   52,  173,
       108779  + /*   990 */   174,  165,   49,   50,   22,  150,  128,  165,  165,  173,
       108780  + /*  1000 */   174,  187,  166,  166,   22,  173,  174,  187,  109,  194,
108344 108781    /*  1010 */   165,   68,   69,   70,   71,   72,   73,   74,   75,   76,
108345 108782    /*  1020 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
108346 108783    /*  1030 */    87,   88,   89,   90,   91,   92,   19,  150,  193,  165,
108347         - /*  1040 */   150,  160,  150,  205,  205,  150,  150,   29,  174,  175,
108348         - /*  1050 */    52,  216,  165,   22,  150,  165,  238,  165,  150,  150,
108349         - /*  1060 */   165,  165,   49,   50,  174,  175,   49,   50,   23,  165,
108350         - /*  1070 */    91,   92,   22,  165,  165,  194,    1,    2,   22,   52,
108351         - /*  1080 */   193,  109,  174,  175,   71,   72,   69,   70,   71,   72,
       108784  + /*  1040 */   102,  205,  205,  150,  150,  247,  248,  173,  174,   19,
       108785  + /*  1050 */   150,   20,  165,  150,  150,  150,  150,  150,  165,  165,
       108786  + /*  1060 */   173,  174,   49,   50,  104,  165,   49,   50,  165,  165,
       108787  + /*  1070 */   165,  165,  165,  173,  174,   43,  173,  174,  173,  174,
       108788  + /*  1080 */   187,   24,  190,  191,   71,   72,   69,   70,   71,   72,
108352 108789    /*  1090 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
108353 108790    /*  1100 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
108354         - /*  1110 */    19,   98,  150,  165,  150,  150,  150,  102,  150,  150,
108355         - /*  1120 */    22,   19,  174,  175,   20,   24,  104,  165,   43,  165,
108356         - /*  1130 */   165,  165,  150,  165,  165,  150,  174,  175,  174,  175,
108357         - /*  1140 */    49,   50,   59,   53,  138,   25,   53,  165,  104,   22,
108358         - /*  1150 */   165,    5,  118,    1,   76,   76,  174,  175,  193,  174,
108359         - /*  1160 */   175,   70,   71,   72,   73,   74,   75,   76,   77,   78,
108360         - /*  1170 */    79,   80,   35,   82,   83,   84,   85,   86,   87,   88,
108361         - /*  1180 */    89,   90,   91,   92,   19,   20,  150,   22,  150,  150,
108362         - /*  1190 */   150,   26,   27,   27,  108,  127,  150,  150,   22,   25,
108363         - /*  1200 */    22,  165,   37,  165,  165,  165,  119,   19,   20,  150,
108364         - /*  1210 */    22,  165,  165,  150,   26,   27,   23,    1,   16,   20,
108365         - /*  1220 */   150,   56,  119,  108,  165,   37,  121,  128,  165,  193,
108366         - /*  1230 */    23,   66,  193,  174,  175,  165,  127,  174,  175,   16,
108367         - /*  1240 */    23,  146,  147,   15,   56,   65,  150,  152,  140,  154,
108368         - /*  1250 */   150,   86,   87,   88,   66,  160,   22,  150,   93,   94,
108369         - /*  1260 */    95,  165,  150,   98,  150,  165,    3,  246,    4,  150,
108370         - /*  1270 */   174,  175,  165,  150,   86,   87,    6,  165,  150,  165,
108371         - /*  1280 */   164,   93,   94,   95,  165,  149,   98,  149,  165,  194,
108372         - /*  1290 */   180,  180,  249,  165,  129,  130,  131,  132,  133,  134,
108373         - /*  1300 */   193,  150,  174,  175,  116,  193,   19,   20,  150,   22,
108374         - /*  1310 */   249,  149,  217,   26,   27,  151,  165,  129,  130,  131,
108375         - /*  1320 */   132,  133,  134,  165,   37,  174,  175,  150,  149,   19,
108376         - /*  1330 */    20,  150,   22,  150,  150,  150,   26,   27,   13,  244,
108377         - /*  1340 */   151,  159,  165,   56,   25,  116,  165,   37,  165,  165,
108378         - /*  1350 */   165,  174,  175,   66,  150,  174,  175,  174,  175,  174,
108379         - /*  1360 */   175,  194,  150,  150,  150,  126,   56,  199,  124,  165,
108380         - /*  1370 */   200,  150,  150,   86,   87,  150,   66,  165,  165,  165,
108381         - /*  1380 */    93,   94,   95,  150,  122,   98,  165,  165,  123,   22,
108382         - /*  1390 */   165,  201,  150,   26,   27,  150,   86,   87,  165,  174,
108383         - /*  1400 */   175,  203,  202,   93,   94,   95,  125,  165,   98,  150,
108384         - /*  1410 */   165,  150,  225,  135,  157,  118,  129,  130,  131,  132,
108385         - /*  1420 */   133,  134,    5,  150,  165,  157,  165,   10,   11,   12,
108386         - /*  1430 */    13,   14,  150,   66,   17,  174,  175,  210,  165,  129,
108387         - /*  1440 */   130,  131,  132,  133,  134,  150,  104,  165,   31,  150,
108388         - /*  1450 */    33,  150,  150,   86,   87,  150,  174,  175,  211,   42,
108389         - /*  1460 */   165,   94,  121,  210,  165,   98,  165,  165,  176,  211,
108390         - /*  1470 */   165,  211,   55,  104,   57,  184,  210,   47,   61,  176,
108391         - /*  1480 */   176,   64,  103,  176,  178,   22,   92,  179,  176,  176,
108392         - /*  1490 */   176,  156,   18,  184,  179,  228,  129,  130,  131,  228,
108393         - /*  1500 */   157,  156,   45,  157,  156,  236,  157,  157,  135,  235,
108394         - /*  1510 */   156,  189,  157,   68,  218,  189,   22,  199,  156,  192,
108395         - /*  1520 */   157,   18,  105,  106,  107,  192,  192,  199,  111,  192,
108396         - /*  1530 */   240,  157,   40,  116,  218,  157,  189,  157,  240,   38,
108397         - /*  1540 */   153,  166,  198,  243,  178,  209,  182,  196,  177,  226,
108398         - /*  1550 */   230,  230,  166,  177,  177,  166,  139,  199,  199,  148,
108399         - /*  1560 */   195,  209,  209,  239,  196,  166,  234,  183,  239,  208,
108400         - /*  1570 */   174,  233,  191,  183,  183,  174,   92,  250,  186,  186,
108401         -};
108402         -#define YY_SHIFT_USE_DFLT (-81)
108403         -#define YY_SHIFT_COUNT (417)
108404         -#define YY_SHIFT_MIN   (-80)
108405         -#define YY_SHIFT_MAX   (1503)
       108791  + /*  1110 */    19,   98,  150,  165,  150,  150,  150,  150,  150,  150,
       108792  + /*  1120 */    59,  173,  174,   25,  150,  190,  191,  165,   53,  165,
       108793  + /*  1130 */   165,  165,  165,  165,  165,  173,  174,  173,  174,  165,
       108794  + /*  1140 */    49,   50,   91,   92,    1,    2,   53,  173,  174,  138,
       108795  + /*  1150 */   104,   22,    5,    1,   35,  118,  127,  150,  193,  193,
       108796  + /*  1160 */   193,   70,   71,   72,   73,   74,   75,   76,   77,   78,
       108797  + /*  1170 */    79,   80,  165,   82,   83,   84,   85,   86,   87,   88,
       108798  + /*  1180 */    89,   90,   91,   92,   19,   20,  150,   22,  150,   27,
       108799  + /*  1190 */   150,   26,   27,  108,  150,   22,   76,   76,  150,   25,
       108800  + /*  1200 */   193,  165,   37,  165,  150,  165,   22,   19,   20,  165,
       108801  + /*  1210 */    22,  173,  174,  165,   26,   27,   23,  150,  119,  165,
       108802  + /*  1220 */   150,   56,  150,  150,  150,   37,   16,  173,  174,  193,
       108803  + /*  1230 */   150,   66,  165,  193,    1,  165,  121,  165,  165,  165,
       108804  + /*  1240 */    20,  146,  147,  119,   56,  165,  150,  152,   16,  154,
       108805  + /*  1250 */   150,   86,   87,   88,   66,  160,  150,  150,   93,   94,
       108806  + /*  1260 */    95,  165,  150,   98,  108,  165,  127,   23,   65,  173,
       108807  + /*  1270 */   174,  165,  165,  150,   86,   87,  128,  165,  150,  173,
       108808  + /*  1280 */   174,   93,   94,   95,   23,  150,   98,   15,  165,  194,
       108809  + /*  1290 */   150,  140,   22,  165,  129,  130,  131,  132,  133,  134,
       108810  + /*  1300 */   165,  173,  174,    3,  116,  165,   19,   20,  150,   22,
       108811  + /*  1310 */     4,  150,  217,   26,   27,  179,  179,  129,  130,  131,
       108812  + /*  1320 */   132,  133,  134,  165,   37,  150,  165,  150,  164,   19,
       108813  + /*  1330 */    20,  150,   22,  246,  149,  249,   26,   27,  249,  244,
       108814  + /*  1340 */   165,  150,  165,   56,    6,  150,  165,   37,  173,  174,
       108815  + /*  1350 */   173,  174,  150,   66,  173,  174,  165,  149,  149,   13,
       108816  + /*  1360 */   165,   25,  150,  150,  150,  149,   56,  165,  150,  116,
       108817  + /*  1370 */   151,  150,  150,   86,   87,  150,   66,  165,  165,  165,
       108818  + /*  1380 */    93,   94,   95,  165,  150,   98,  165,  165,  151,   22,
       108819  + /*  1390 */   165,  194,  150,   26,   27,  150,   86,   87,  159,  165,
       108820  + /*  1400 */   199,  126,  123,   93,   94,   95,  200,  165,   98,  124,
       108821  + /*  1410 */   165,  122,  201,  125,  225,  135,  129,  130,  131,  132,
       108822  + /*  1420 */   133,  134,    5,  157,  157,  202,  118,   10,   11,   12,
       108823  + /*  1430 */    13,   14,  203,   66,   17,  104,  210,  121,  211,  129,
       108824  + /*  1440 */   130,  131,  132,  133,  134,  210,  175,  211,   31,  211,
       108825  + /*  1450 */    33,  210,  104,   86,   87,   47,  175,  183,  175,   42,
       108826  + /*  1460 */   103,   94,  178,  177,   22,   98,  175,   92,  228,  175,
       108827  + /*  1470 */   175,  228,   55,  183,   57,  178,  175,  156,   61,   18,
       108828  + /*  1480 */   157,   64,  156,  235,  157,  156,   45,  157,  236,  157,
       108829  + /*  1490 */   135,  156,  189,   68,  157,  218,  129,  130,  131,   22,
       108830  + /*  1500 */   189,  199,  157,  156,  192,   18,  192,  192,  199,  192,
       108831  + /*  1510 */   218,  189,   40,  157,   38,  157,  240,  157,  240,  153,
       108832  + /*  1520 */   196,  181,  105,  106,  107,  243,  198,  166,  111,  230,
       108833  + /*  1530 */   176,  226,  239,  116,  230,  176,  166,  166,  176,  148,
       108834  + /*  1540 */   199,  177,  209,  209,  166,  196,  239,  208,  185,  199,
       108835  + /*  1550 */    92,  209,  233,  173,  234,  182,  139,  173,  182,  191,
       108836  + /*  1560 */   195,  182,  250,  186,
       108837  +};
       108838  +#define YY_SHIFT_USE_DFLT (-70)
       108839  +#define YY_SHIFT_COUNT (416)
       108840  +#define YY_SHIFT_MIN   (-69)
       108841  +#define YY_SHIFT_MAX   (1487)
108406 108842   static const short yy_shift_ofst[] = {
108407         - /*     0 */  1075, 1188, 1417, 1188, 1287, 1287,  138,  138,    1,  -19,
108408         - /*    10 */  1287, 1287, 1287, 1287,  340,   -2,  129,  129,  795, 1165,
       108843  + /*     0 */  1143, 1188, 1417, 1188, 1287, 1287,  138,  138,   -2,  -19,
       108844  + /*    10 */  1287, 1287, 1287, 1287,  347,  362,  129,  129,  795, 1165,
108409 108845    /*    20 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
108410 108846    /*    30 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
108411 108847    /*    40 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1310, 1287,
108412 108848    /*    50 */  1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287, 1287,
108413         - /*    60 */  1287, 1287,  212,   -2,   -2,   -8,   -8,  614, 1229,   55,
       108849  + /*    60 */  1287, 1287,  286,  362,  362,  538,  538,  231, 1253,   55,
108414 108850    /*    70 */   721,  647,  573,  499,  425,  351,  277,  203,  869,  869,
108415 108851    /*    80 */   869,  869,  869,  869,  869,  869,  869,  869,  869,  869,
108416 108852    /*    90 */   869,  869,  869,  943,  869, 1017, 1091, 1091,  -69,  -45,
108417         - /*   100 */   -45,  -45,  -45,  -45,   -1,   57,   28,  361,   -2,   -2,
108418         - /*   110 */    -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,
108419         - /*   120 */    -2,   -2,   -2,   -2,  391,  515,   -2,   -2,   -2,   -2,
108420         - /*   130 */    -2,  509,  -80,  614,  979, 1484,  -81,  -81,  -81, 1367,
108421         - /*   140 */    75,  182,  182,  314,  311,  364,  219,   86,  613,  609,
108422         - /*   150 */    -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,
108423         - /*   160 */    -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,
108424         - /*   170 */    -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,   -2,
108425         - /*   180 */    -2,   -2,  578,  578,  578,  615, 1229, 1229, 1229,  -81,
108426         - /*   190 */   -81,  -81,  160,  168,  168,  283,  500,  500,  500,  278,
108427         - /*   200 */   449,  330,  432,  409,  352,   48,   48,   48,   48,  426,
108428         - /*   210 */   286,   48,   48,  728,  581,  369,  590,  495,  224,  224,
108429         - /*   220 */   727,  495,  727,  719,  614,  659,  614,  659,  811,  659,
108430         - /*   230 */   224,  257,  480,  480,  614,  144,  375,  -18, 1501, 1297,
108431         - /*   240 */  1297, 1492, 1492, 1297, 1494, 1445, 1239, 1503, 1503, 1503,
108432         - /*   250 */  1503, 1297, 1474, 1239, 1494, 1445, 1445, 1297, 1474, 1373,
108433         - /*   260 */  1457, 1297, 1297, 1474, 1297, 1474, 1297, 1474, 1463, 1369,
108434         - /*   270 */  1369, 1369, 1430, 1394, 1394, 1463, 1369, 1379, 1369, 1430,
108435         - /*   280 */  1369, 1369, 1341, 1342, 1341, 1342, 1341, 1342, 1297, 1297,
108436         - /*   290 */  1278, 1281, 1262, 1244, 1265, 1239, 1229, 1319, 1325, 1325,
108437         - /*   300 */  1270, 1270, 1270, 1270,  -81,  -81,  -81,  -81,  -81,  -81,
108438         - /*   310 */  1013,  242,  757,  752,  465,  363,  947,  232,  944,  906,
108439         - /*   320 */   872,  837,  738,  448,  381,  230,   84,  362,  300, 1264,
108440         - /*   330 */  1263, 1234, 1108, 1228, 1180, 1223, 1217, 1207, 1099, 1174,
108441         - /*   340 */  1109, 1115, 1103, 1199, 1105, 1202, 1216, 1087, 1193, 1178,
108442         - /*   350 */  1174, 1176, 1068, 1079, 1078, 1086, 1166, 1137, 1034, 1152,
108443         - /*   360 */  1146, 1127, 1044, 1006, 1093, 1120, 1090, 1083, 1085, 1022,
108444         - /*   370 */  1101, 1104, 1102,  972, 1015, 1098, 1027, 1056, 1050, 1045,
108445         - /*   380 */  1031,  998, 1018,  981,  946,  950,  973,  963,  862,  885,
108446         - /*   390 */   819,  884,  782,  796,  806,  807,  790,  796,  793,  758,
108447         - /*   400 */   753,  732,  692,  696,  682,  686,  667,  544,  291,  521,
108448         - /*   410 */   510,  365,  358,  139,  114,   54,   14,   25,
       108853  + /*   100 */   -45,  -45,  -45,  -45,   -1,   24,  245,  362,  362,  362,
       108854  + /*   110 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
       108855  + /*   120 */   362,  362,  362,  388,  356,  362,  362,  362,  362,  362,
       108856  + /*   130 */   732,  868,  231, 1051, 1458,  -70,  -70,  -70, 1367,   57,
       108857  + /*   140 */   434,  434,  289,  291,  285,    1,  204,  572,  539,  362,
       108858  + /*   150 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
       108859  + /*   160 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
       108860  + /*   170 */   362,  362,  362,  362,  362,  362,  362,  362,  362,  362,
       108861  + /*   180 */   362,  506,  506,  506,  705, 1253, 1253, 1253,  -70,  -70,
       108862  + /*   190 */   -70,  171,  171,  160,  502,  502,  502,  446,  432,  511,
       108863  + /*   200 */   422,  358,  335,  -12,  -12,  -12,  -12,  576,  294,  -12,
       108864  + /*   210 */   -12,  295,  595,  141,  600,  730,  723,  723,  805,  730,
       108865  + /*   220 */   805,  439,  911,  231,  865,  231,  865,  807,  865,  723,
       108866  + /*   230 */   766,  633,  633,  231,  284,   63,  608, 1476, 1308, 1308,
       108867  + /*   240 */  1472, 1472, 1308, 1477, 1425, 1275, 1487, 1487, 1487, 1487,
       108868  + /*   250 */  1308, 1461, 1275, 1477, 1425, 1425, 1308, 1461, 1355, 1441,
       108869  + /*   260 */  1308, 1308, 1461, 1308, 1461, 1308, 1461, 1442, 1348, 1348,
       108870  + /*   270 */  1348, 1408, 1375, 1375, 1442, 1348, 1357, 1348, 1408, 1348,
       108871  + /*   280 */  1348, 1316, 1331, 1316, 1331, 1316, 1331, 1308, 1308, 1280,
       108872  + /*   290 */  1288, 1289, 1285, 1279, 1275, 1253, 1336, 1346, 1346, 1338,
       108873  + /*   300 */  1338, 1338, 1338,  -70,  -70,  -70,  -70,  -70,  -70, 1013,
       108874  + /*   310 */   467,  612,   84,  179,  -28,  870,  410,  761,  760,  667,
       108875  + /*   320 */   650,  531,  220,  361,  331,  125,  127,   97, 1306, 1300,
       108876  + /*   330 */  1270, 1151, 1272, 1203, 1232, 1261, 1244, 1148, 1174, 1139,
       108877  + /*   340 */  1156, 1124, 1220, 1115, 1210, 1233, 1099, 1193, 1184, 1174,
       108878  + /*   350 */  1173, 1029, 1121, 1120, 1085, 1162, 1119, 1037, 1152, 1147,
       108879  + /*   360 */  1129, 1046, 1011, 1093, 1098, 1075, 1061, 1032,  960, 1057,
       108880  + /*   370 */  1031, 1030,  899,  938,  982,  936,  972,  958,  910,  955,
       108881  + /*   380 */   875,  885,  908,  857,  859,  867,  804,  590,  834,  747,
       108882  + /*   390 */   818,  513,  611,  741,  673,  637,  611,  606,  603,  579,
       108883  + /*   400 */   501,  541,  468,  386,  445,  395,  376,  281,  185,  120,
       108884  + /*   410 */    92,   75,   45,  114,   25,   11,    5,
108449 108885   };
108450 108886   #define YY_REDUCE_USE_DFLT (-169)
108451         -#define YY_REDUCE_COUNT (309)
       108887  +#define YY_REDUCE_COUNT (308)
108452 108888   #define YY_REDUCE_MIN   (-168)
108453         -#define YY_REDUCE_MAX   (1411)
       108889  +#define YY_REDUCE_MAX   (1391)
108454 108890   static const short yy_reduce_ofst[] = {
108455         - /*     0 */   318,   90, 1095,  221,  157,   21,  159,   18,  150,  390,
108456         - /*    10 */   385,  378,  380,  315,  325,  249,  529,  -71,    8, 1282,
108457         - /*    20 */  1261, 1225, 1185, 1183, 1181, 1177, 1151, 1128, 1096, 1063,
108458         - /*    30 */  1059,  985,  982,  964,  962,  948,  908,  890,  874,  834,
108459         - /*    40 */   832,  816,  813,  800,  759,  746,  742,  739,  726,  684,
108460         - /*    50 */   681,  668,  665,  652,  612,  593,  591,  537,  524,  518,
108461         - /*    60 */   504,  455,  511,  376,  517,  247, -168,   24,  420,  463,
108462         - /*    70 */   463,  463,  463,  463,  463,  463,  463,  463,  463,  463,
108463         - /*    80 */   463,  463,  463,  463,  463,  463,  463,  463,  463,  463,
108464         - /*    90 */   463,  463,  463,  463,  463,  463,  463,  463,  463,  463,
108465         - /*   100 */   463,  463,  463,  463,  463,  -74,  463,  463, 1112,  835,
108466         - /*   110 */  1107, 1039, 1036,  965,  887,  845,  818,  760,  688,  687,
108467         - /*   120 */   538,  743,  623,  592,  446,  513,  814,  740,  670,  156,
108468         - /*   130 */   468,  553,  184,  616,  463,  463,  463,  463,  463,  595,
108469         - /*   140 */   821,  786,  745,  909, 1305, 1302, 1301, 1299,  675,  675,
108470         - /*   150 */  1295, 1273, 1259, 1245, 1242, 1233, 1222, 1221, 1214, 1213,
108471         - /*   160 */  1212, 1204, 1184, 1158, 1123, 1119, 1114, 1100, 1070, 1047,
108472         - /*   170 */  1046, 1040, 1038,  969,  968,  966,  909,  904,  896,  895,
108473         - /*   180 */   892,  599,  839,  838,  766,  754,  881,  734,  346,  605,
108474         - /*   190 */   622,  -94, 1393, 1401, 1396, 1392, 1391, 1390, 1384, 1361,
108475         - /*   200 */  1365, 1381, 1365, 1365, 1365, 1365, 1365, 1365, 1365, 1332,
108476         - /*   210 */  1338, 1365, 1365, 1361, 1399, 1368, 1411, 1359, 1353, 1352,
108477         - /*   220 */  1329, 1358, 1324, 1366, 1389, 1377, 1386, 1376, 1364, 1371,
108478         - /*   230 */  1336, 1323, 1321, 1320, 1375, 1344, 1351, 1387, 1300, 1380,
108479         - /*   240 */  1378, 1298, 1290, 1374, 1316, 1347, 1328, 1337, 1334, 1333,
108480         - /*   250 */  1327, 1363, 1362, 1318, 1296, 1326, 1322, 1355, 1354, 1269,
108481         - /*   260 */  1274, 1350, 1349, 1348, 1346, 1345, 1343, 1335, 1315, 1314,
108482         - /*   270 */  1313, 1312, 1309, 1271, 1267, 1308, 1307, 1306, 1304, 1291,
108483         - /*   280 */  1303, 1292, 1260, 1266, 1258, 1253, 1247, 1227, 1268, 1257,
108484         - /*   290 */  1187, 1198, 1200, 1190, 1170, 1168, 1167, 1182, 1189, 1164,
108485         - /*   300 */  1179, 1162, 1138, 1136, 1061, 1043, 1021, 1111, 1110, 1116,
       108891  + /*     0 */  -141,   90, 1095,  222,  158,  156,   19,   17,   10, -104,
       108892  + /*    10 */   378,  316,  311,   12,  180,  249,  598,  464,  397, 1181,
       108893  + /*    20 */  1177, 1175, 1128, 1106, 1096, 1054, 1038,  974,  964,  962,
       108894  + /*    30 */   948,  905,  903,  900,  887,  874,  832,  826,  816,  813,
       108895  + /*    40 */   800,  758,  755,  752,  742,  739,  726,  685,  681,  668,
       108896  + /*    50 */   665,  652,  607,  604,  594,  591,  578,  530,  528,  526,
       108897  + /*    60 */   385,   18,  477,  466,  519,  444,  350,  435,  405,  488,
       108898  + /*    70 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
       108899  + /*    80 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
       108900  + /*    90 */   488,  488,  488,  488,  488,  488,  488,  488,  488,  488,
       108901  + /*   100 */   488,  488,  488,  488,  488,  488,  488, 1040,  678, 1036,
       108902  + /*   110 */  1007,  967,  966,  965,  845,  686,  610,  684,  317,  672,
       108903  + /*   120 */   893,  327,  623,  522,   -7,  820,  814,  157,  154,  101,
       108904  + /*   130 */   702,  494,  580,  488,  488,  488,  488,  488,  614,  586,
       108905  + /*   140 */   935,  892,  968, 1245, 1242, 1234, 1225,  798,  798, 1222,
       108906  + /*   150 */  1221, 1218, 1214, 1213, 1212, 1202, 1195, 1191, 1161, 1158,
       108907  + /*   160 */  1140, 1135, 1123, 1112, 1107, 1100, 1080, 1074, 1073, 1072,
       108908  + /*   170 */  1070, 1067, 1048, 1044,  969,  968,  907,  906,  904,  894,
       108909  + /*   180 */   833,  837,  836,  340,  827,  815,  775,   68,  722,  646,
       108910  + /*   190 */  -168, 1384, 1380, 1377, 1379, 1376, 1373, 1339, 1365, 1368,
       108911  + /*   200 */  1365, 1365, 1365, 1365, 1365, 1365, 1365, 1320, 1319, 1365,
       108912  + /*   210 */  1365, 1339, 1378, 1349, 1391, 1350, 1342, 1334, 1307, 1341,
       108913  + /*   220 */  1293, 1364, 1363, 1371, 1362, 1370, 1359, 1340, 1354, 1333,
       108914  + /*   230 */  1305, 1304, 1299, 1361, 1328, 1324, 1366, 1282, 1360, 1358,
       108915  + /*   240 */  1278, 1276, 1356, 1292, 1322, 1309, 1317, 1315, 1314, 1312,
       108916  + /*   250 */  1345, 1347, 1302, 1277, 1311, 1303, 1337, 1335, 1252, 1248,
       108917  + /*   260 */  1332, 1330, 1329, 1327, 1326, 1323, 1321, 1297, 1301, 1295,
       108918  + /*   270 */  1294, 1290, 1243, 1240, 1284, 1291, 1286, 1283, 1274, 1281,
       108919  + /*   280 */  1271, 1238, 1241, 1236, 1235, 1227, 1226, 1267, 1266, 1189,
       108920  + /*   290 */  1229, 1223, 1211, 1206, 1201, 1197, 1239, 1237, 1219, 1216,
       108921  + /*   300 */  1209, 1208, 1185, 1089, 1086, 1087, 1137, 1136, 1164,
108486 108922   };
108487 108923   static const YYACTIONTYPE yy_default[] = {
108488         - /*     0 */   634,  868,  956,  956,  868,  868,  956,  956,  956,  758,
108489         - /*    10 */   956,  956,  956,  866,  956,  956,  786,  786,  930,  956,
108490         - /*    20 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108491         - /*    30 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108492         - /*    40 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108493         - /*    50 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108494         - /*    60 */   956,  956,  956,  956,  956,  956,  956,  673,  762,  792,
108495         - /*    70 */   956,  956,  956,  956,  956,  956,  956,  956,  929,  931,
108496         - /*    80 */   800,  799,  909,  773,  797,  790,  794,  869,  862,  863,
108497         - /*    90 */   861,  865,  870,  956,  793,  829,  846,  828,  840,  845,
108498         - /*   100 */   852,  844,  841,  831,  830,  665,  832,  833,  956,  956,
108499         - /*   110 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108500         - /*   120 */   956,  956,  956,  956,  660,  727,  956,  956,  956,  956,
108501         - /*   130 */   956,  956,  956,  956,  834,  835,  849,  848,  847,  956,
108502         - /*   140 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108503         - /*   150 */   956,  936,  934,  956,  881,  956,  956,  956,  956,  956,
108504         - /*   160 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108505         - /*   170 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108506         - /*   180 */   956,  640,  758,  758,  758,  634,  956,  956,  956,  948,
108507         - /*   190 */   762,  752,  718,  956,  956,  956,  956,  956,  956,  956,
108508         - /*   200 */   956,  956,  956,  956,  956,  802,  741,  919,  921,  956,
108509         - /*   210 */   902,  739,  662,  760,  675,  750,  642,  796,  775,  775,
108510         - /*   220 */   914,  796,  914,  699,  956,  786,  956,  786,  696,  786,
108511         - /*   230 */   775,  864,  956,  956,  956,  759,  750,  956,  941,  766,
108512         - /*   240 */   766,  933,  933,  766,  808,  731,  796,  738,  738,  738,
108513         - /*   250 */   738,  766,  657,  796,  808,  731,  731,  766,  657,  908,
108514         - /*   260 */   906,  766,  766,  657,  766,  657,  766,  657,  874,  729,
108515         - /*   270 */   729,  729,  714,  878,  878,  874,  729,  699,  729,  714,
108516         - /*   280 */   729,  729,  779,  774,  779,  774,  779,  774,  766,  766,
108517         - /*   290 */   956,  791,  780,  789,  787,  796,  956,  717,  650,  650,
108518         - /*   300 */   639,  639,  639,  639,  953,  953,  948,  701,  701,  683,
108519         - /*   310 */   956,  956,  956,  956,  956,  956,  956,  883,  956,  956,
108520         - /*   320 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108521         - /*   330 */   635,  943,  956,  956,  940,  956,  956,  956,  956,  801,
108522         - /*   340 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108523         - /*   350 */   918,  956,  956,  956,  956,  956,  956,  956,  912,  956,
108524         - /*   360 */   956,  956,  956,  956,  956,  905,  904,  956,  956,  956,
108525         - /*   370 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108526         - /*   380 */   956,  956,  956,  956,  956,  956,  956,  956,  956,  956,
108527         - /*   390 */   956,  956,  956,  788,  956,  781,  956,  867,  956,  956,
108528         - /*   400 */   956,  956,  956,  956,  956,  956,  956,  956,  744,  817,
108529         - /*   410 */   956,  816,  820,  815,  667,  956,  648,  956,  631,  636,
108530         - /*   420 */   952,  955,  954,  951,  950,  949,  944,  942,  939,  938,
108531         - /*   430 */   937,  935,  932,  928,  887,  885,  892,  891,  890,  889,
108532         - /*   440 */   888,  886,  884,  882,  803,  798,  795,  927,  880,  740,
108533         - /*   450 */   737,  736,  656,  945,  911,  920,  807,  806,  809,  917,
108534         - /*   460 */   916,  915,  913,  910,  897,  805,  804,  732,  872,  871,
108535         - /*   470 */   659,  901,  900,  899,  903,  907,  898,  768,  658,  655,
108536         - /*   480 */   664,  721,  720,  728,  726,  725,  724,  723,  722,  719,
108537         - /*   490 */   666,  674,  685,  713,  698,  697,  877,  879,  876,  875,
108538         - /*   500 */   706,  705,  711,  710,  709,  708,  707,  704,  703,  702,
108539         - /*   510 */   695,  694,  700,  693,  716,  715,  712,  692,  735,  734,
108540         - /*   520 */   733,  730,  691,  690,  689,  820,  688,  687,  826,  825,
108541         - /*   530 */   813,  856,  755,  754,  753,  765,  764,  777,  776,  811,
108542         - /*   540 */   810,  778,  763,  757,  756,  772,  771,  770,  769,  761,
108543         - /*   550 */   751,  783,  785,  784,  782,  858,  767,  855,  926,  925,
108544         - /*   560 */   924,  923,  922,  860,  859,  827,  824,  678,  679,  895,
108545         - /*   570 */   894,  896,  893,  681,  680,  677,  676,  857,  746,  745,
108546         - /*   580 */   853,  850,  842,  838,  854,  851,  843,  839,  837,  836,
108547         - /*   590 */   822,  821,  819,  818,  814,  823,  669,  747,  743,  742,
108548         - /*   600 */   812,  749,  748,  686,  684,  682,  663,  661,  654,  652,
108549         - /*   610 */   651,  653,  649,  647,  646,  645,  644,  643,  672,  671,
108550         - /*   620 */   670,  668,  667,  641,  638,  637,  633,  632,  630,
       108924  + /*     0 */   632,  866,  954,  954,  866,  866,  954,  954,  954,  756,
       108925  + /*    10 */   954,  954,  954,  864,  954,  954,  784,  784,  928,  954,
       108926  + /*    20 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108927  + /*    30 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108928  + /*    40 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108929  + /*    50 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108930  + /*    60 */   954,  954,  954,  954,  954,  954,  954,  671,  760,  790,
       108931  + /*    70 */   954,  954,  954,  954,  954,  954,  954,  954,  927,  929,
       108932  + /*    80 */   798,  797,  907,  771,  795,  788,  792,  867,  860,  861,
       108933  + /*    90 */   859,  863,  868,  954,  791,  827,  844,  826,  838,  843,
       108934  + /*   100 */   850,  842,  839,  829,  828,  830,  831,  954,  954,  954,
       108935  + /*   110 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108936  + /*   120 */   954,  954,  954,  658,  725,  954,  954,  954,  954,  954,
       108937  + /*   130 */   954,  954,  954,  832,  833,  847,  846,  845,  954,  663,
       108938  + /*   140 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108939  + /*   150 */   934,  932,  954,  879,  954,  954,  954,  954,  954,  954,
       108940  + /*   160 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108941  + /*   170 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108942  + /*   180 */   638,  756,  756,  756,  632,  954,  954,  954,  946,  760,
       108943  + /*   190 */   750,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108944  + /*   200 */   954,  954,  954,  800,  739,  917,  919,  954,  900,  737,
       108945  + /*   210 */   660,  758,  673,  748,  640,  794,  773,  773,  912,  794,
       108946  + /*   220 */   912,  696,  719,  954,  784,  954,  784,  693,  784,  773,
       108947  + /*   230 */   862,  954,  954,  954,  757,  748,  954,  939,  764,  764,
       108948  + /*   240 */   931,  931,  764,  806,  729,  794,  736,  736,  736,  736,
       108949  + /*   250 */   764,  655,  794,  806,  729,  729,  764,  655,  906,  904,
       108950  + /*   260 */   764,  764,  655,  764,  655,  764,  655,  872,  727,  727,
       108951  + /*   270 */   727,  711,  876,  876,  872,  727,  696,  727,  711,  727,
       108952  + /*   280 */   727,  777,  772,  777,  772,  777,  772,  764,  764,  954,
       108953  + /*   290 */   789,  778,  787,  785,  794,  954,  714,  648,  648,  637,
       108954  + /*   300 */   637,  637,  637,  951,  951,  946,  698,  698,  681,  954,
       108955  + /*   310 */   954,  954,  954,  954,  954,  954,  881,  954,  954,  954,
       108956  + /*   320 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  633,
       108957  + /*   330 */   941,  954,  954,  938,  954,  954,  954,  954,  799,  954,
       108958  + /*   340 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  916,
       108959  + /*   350 */   954,  954,  954,  954,  954,  954,  954,  910,  954,  954,
       108960  + /*   360 */   954,  954,  954,  954,  903,  902,  954,  954,  954,  954,
       108961  + /*   370 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108962  + /*   380 */   954,  954,  954,  954,  954,  954,  954,  954,  954,  954,
       108963  + /*   390 */   954,  954,  786,  954,  779,  954,  865,  954,  954,  954,
       108964  + /*   400 */   954,  954,  954,  954,  954,  954,  954,  742,  815,  954,
       108965  + /*   410 */   814,  818,  813,  665,  954,  646,  954,  629,  634,  950,
       108966  + /*   420 */   953,  952,  949,  948,  947,  942,  940,  937,  936,  935,
       108967  + /*   430 */   933,  930,  926,  885,  883,  890,  889,  888,  887,  886,
       108968  + /*   440 */   884,  882,  880,  801,  796,  793,  925,  878,  738,  735,
       108969  + /*   450 */   734,  654,  943,  909,  918,  805,  804,  807,  915,  914,
       108970  + /*   460 */   913,  911,  908,  895,  803,  802,  730,  870,  869,  657,
       108971  + /*   470 */   899,  898,  897,  901,  905,  896,  766,  656,  653,  662,
       108972  + /*   480 */   717,  718,  726,  724,  723,  722,  721,  720,  716,  664,
       108973  + /*   490 */   672,  710,  695,  694,  875,  877,  874,  873,  703,  702,
       108974  + /*   500 */   708,  707,  706,  705,  704,  701,  700,  699,  692,  691,
       108975  + /*   510 */   697,  690,  713,  712,  709,  689,  733,  732,  731,  728,
       108976  + /*   520 */   688,  687,  686,  818,  685,  684,  824,  823,  811,  854,
       108977  + /*   530 */   753,  752,  751,  763,  762,  775,  774,  809,  808,  776,
       108978  + /*   540 */   761,  755,  754,  770,  769,  768,  767,  759,  749,  781,
       108979  + /*   550 */   783,  782,  780,  856,  765,  853,  924,  923,  922,  921,
       108980  + /*   560 */   920,  858,  857,  825,  822,  676,  677,  893,  892,  894,
       108981  + /*   570 */   891,  679,  678,  675,  674,  855,  744,  743,  851,  848,
       108982  + /*   580 */   840,  836,  852,  849,  841,  837,  835,  834,  820,  819,
       108983  + /*   590 */   817,  816,  812,  821,  667,  745,  741,  740,  810,  747,
       108984  + /*   600 */   746,  683,  682,  680,  661,  659,  652,  650,  649,  651,
       108985  + /*   610 */   647,  645,  644,  643,  642,  641,  670,  669,  668,  666,
       108986  + /*   620 */   665,  639,  636,  635,  631,  630,  628,
108551 108987   };
108552 108988   
108553 108989   /* The next table maps tokens into fallback tokens.  If a construct
108554 108990   ** like the following:
108555 108991   ** 
108556 108992   **      %fallback ID X Y Z.
108557 108993   **
................................................................................
108746 109182     "ecmd",          "explain",       "cmdx",          "cmd",         
108747 109183     "transtype",     "trans_opt",     "nm",            "savepoint_opt",
108748 109184     "create_table",  "create_table_args",  "createkw",      "temp",        
108749 109185     "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
108750 109186     "select",        "column",        "columnid",      "type",        
108751 109187     "carglist",      "id",            "ids",           "typetoken",   
108752 109188     "typename",      "signed",        "plus_num",      "minus_num",   
108753         -  "carg",          "ccons",         "term",          "expr",        
108754         -  "onconf",        "sortorder",     "autoinc",       "idxlist_opt", 
108755         -  "refargs",       "defer_subclause",  "refarg",        "refact",      
108756         -  "init_deferred_pred_opt",  "conslist",      "tcons",         "idxlist",     
       109189  +  "ccons",         "term",          "expr",          "onconf",      
       109190  +  "sortorder",     "autoinc",       "idxlist_opt",   "refargs",     
       109191  +  "defer_subclause",  "refarg",        "refact",        "init_deferred_pred_opt",
       109192  +  "conslist",      "tconscomma",    "tcons",         "idxlist",     
108757 109193     "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",   
108758 109194     "ifexists",      "fullname",      "oneselect",     "multiselect_op",
108759 109195     "distinct",      "selcollist",    "from",          "where_opt",   
108760 109196     "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",   
108761 109197     "sclp",          "as",            "seltablist",    "stl_prefix",  
108762 109198     "joinop",        "indexed_opt",   "on_opt",        "using_opt",   
108763 109199     "joinop2",       "inscollist",    "sortlist",      "nexprlist",   
................................................................................
108826 109262    /*  46 */ "typetoken ::= typename",
108827 109263    /*  47 */ "typetoken ::= typename LP signed RP",
108828 109264    /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
108829 109265    /*  49 */ "typename ::= ids",
108830 109266    /*  50 */ "typename ::= typename ids",
108831 109267    /*  51 */ "signed ::= plus_num",
108832 109268    /*  52 */ "signed ::= minus_num",
108833         - /*  53 */ "carglist ::= carglist carg",
       109269  + /*  53 */ "carglist ::= carglist ccons",
108834 109270    /*  54 */ "carglist ::=",
108835         - /*  55 */ "carg ::= CONSTRAINT nm ccons",
108836         - /*  56 */ "carg ::= ccons",
108837         - /*  57 */ "ccons ::= DEFAULT term",
108838         - /*  58 */ "ccons ::= DEFAULT LP expr RP",
108839         - /*  59 */ "ccons ::= DEFAULT PLUS term",
108840         - /*  60 */ "ccons ::= DEFAULT MINUS term",
108841         - /*  61 */ "ccons ::= DEFAULT id",
108842         - /*  62 */ "ccons ::= NULL onconf",
108843         - /*  63 */ "ccons ::= NOT NULL onconf",
108844         - /*  64 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
108845         - /*  65 */ "ccons ::= UNIQUE onconf",
108846         - /*  66 */ "ccons ::= CHECK LP expr RP",
108847         - /*  67 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
108848         - /*  68 */ "ccons ::= defer_subclause",
108849         - /*  69 */ "ccons ::= COLLATE ids",
108850         - /*  70 */ "autoinc ::=",
108851         - /*  71 */ "autoinc ::= AUTOINCR",
108852         - /*  72 */ "refargs ::=",
108853         - /*  73 */ "refargs ::= refargs refarg",
108854         - /*  74 */ "refarg ::= MATCH nm",
108855         - /*  75 */ "refarg ::= ON INSERT refact",
108856         - /*  76 */ "refarg ::= ON DELETE refact",
108857         - /*  77 */ "refarg ::= ON UPDATE refact",
108858         - /*  78 */ "refact ::= SET NULL",
108859         - /*  79 */ "refact ::= SET DEFAULT",
108860         - /*  80 */ "refact ::= CASCADE",
108861         - /*  81 */ "refact ::= RESTRICT",
108862         - /*  82 */ "refact ::= NO ACTION",
108863         - /*  83 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
108864         - /*  84 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
108865         - /*  85 */ "init_deferred_pred_opt ::=",
108866         - /*  86 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
108867         - /*  87 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
108868         - /*  88 */ "conslist_opt ::=",
108869         - /*  89 */ "conslist_opt ::= COMMA conslist",
108870         - /*  90 */ "conslist ::= conslist COMMA tcons",
108871         - /*  91 */ "conslist ::= conslist tcons",
108872         - /*  92 */ "conslist ::= tcons",
       109271  + /*  55 */ "ccons ::= CONSTRAINT nm",
       109272  + /*  56 */ "ccons ::= DEFAULT term",
       109273  + /*  57 */ "ccons ::= DEFAULT LP expr RP",
       109274  + /*  58 */ "ccons ::= DEFAULT PLUS term",
       109275  + /*  59 */ "ccons ::= DEFAULT MINUS term",
       109276  + /*  60 */ "ccons ::= DEFAULT id",
       109277  + /*  61 */ "ccons ::= NULL onconf",
       109278  + /*  62 */ "ccons ::= NOT NULL onconf",
       109279  + /*  63 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
       109280  + /*  64 */ "ccons ::= UNIQUE onconf",
       109281  + /*  65 */ "ccons ::= CHECK LP expr RP",
       109282  + /*  66 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
       109283  + /*  67 */ "ccons ::= defer_subclause",
       109284  + /*  68 */ "ccons ::= COLLATE ids",
       109285  + /*  69 */ "autoinc ::=",
       109286  + /*  70 */ "autoinc ::= AUTOINCR",
       109287  + /*  71 */ "refargs ::=",
       109288  + /*  72 */ "refargs ::= refargs refarg",
       109289  + /*  73 */ "refarg ::= MATCH nm",
       109290  + /*  74 */ "refarg ::= ON INSERT refact",
       109291  + /*  75 */ "refarg ::= ON DELETE refact",
       109292  + /*  76 */ "refarg ::= ON UPDATE refact",
       109293  + /*  77 */ "refact ::= SET NULL",
       109294  + /*  78 */ "refact ::= SET DEFAULT",
       109295  + /*  79 */ "refact ::= CASCADE",
       109296  + /*  80 */ "refact ::= RESTRICT",
       109297  + /*  81 */ "refact ::= NO ACTION",
       109298  + /*  82 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
       109299  + /*  83 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
       109300  + /*  84 */ "init_deferred_pred_opt ::=",
       109301  + /*  85 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
       109302  + /*  86 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
       109303  + /*  87 */ "conslist_opt ::=",
       109304  + /*  88 */ "conslist_opt ::= COMMA conslist",
       109305  + /*  89 */ "conslist ::= conslist tconscomma tcons",
       109306  + /*  90 */ "conslist ::= tcons",
       109307  + /*  91 */ "tconscomma ::= COMMA",
       109308  + /*  92 */ "tconscomma ::=",
108873 109309    /*  93 */ "tcons ::= CONSTRAINT nm",
108874 109310    /*  94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
108875 109311    /*  95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
108876 109312    /*  96 */ "tcons ::= CHECK LP expr RP onconf",
108877 109313    /*  97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
108878 109314    /*  98 */ "defer_subclause_opt ::=",
108879 109315    /*  99 */ "defer_subclause_opt ::= defer_subclause",
................................................................................
109184 109620       */
109185 109621       case 160: /* select */
109186 109622       case 194: /* oneselect */
109187 109623   {
109188 109624   sqlite3SelectDelete(pParse->db, (yypminor->yy159));
109189 109625   }
109190 109626         break;
109191         -    case 174: /* term */
109192         -    case 175: /* expr */
       109627  +    case 173: /* term */
       109628  +    case 174: /* expr */
109193 109629   {
109194 109630   sqlite3ExprDelete(pParse->db, (yypminor->yy342).pExpr);
109195 109631   }
109196 109632         break;
109197         -    case 179: /* idxlist_opt */
       109633  +    case 178: /* idxlist_opt */
109198 109634       case 187: /* idxlist */
109199 109635       case 197: /* selcollist */
109200 109636       case 200: /* groupby_opt */
109201 109637       case 202: /* orderby_opt */
109202 109638       case 204: /* sclp */
109203 109639       case 214: /* sortlist */
109204 109640       case 215: /* nexprlist */
................................................................................
109547 109983     { 167, 6 },
109548 109984     { 168, 1 },
109549 109985     { 168, 2 },
109550 109986     { 169, 1 },
109551 109987     { 169, 1 },
109552 109988     { 164, 2 },
109553 109989     { 164, 0 },
       109990  +  { 172, 2 },
       109991  +  { 172, 2 },
       109992  +  { 172, 4 },
       109993  +  { 172, 3 },
       109994  +  { 172, 3 },
       109995  +  { 172, 2 },
       109996  +  { 172, 2 },
109554 109997     { 172, 3 },
       109998  +  { 172, 5 },
       109999  +  { 172, 2 },
       110000  +  { 172, 4 },
       110001  +  { 172, 4 },
109555 110002     { 172, 1 },
109556         -  { 173, 2 },
109557         -  { 173, 4 },
109558         -  { 173, 3 },
109559         -  { 173, 3 },
109560         -  { 173, 2 },
109561         -  { 173, 2 },
109562         -  { 173, 3 },
109563         -  { 173, 5 },
109564         -  { 173, 2 },
109565         -  { 173, 4 },
109566         -  { 173, 4 },
109567         -  { 173, 1 },
109568         -  { 173, 2 },
109569         -  { 178, 0 },
109570         -  { 178, 1 },
109571         -  { 180, 0 },
109572         -  { 180, 2 },
       110003  +  { 172, 2 },
       110004  +  { 177, 0 },
       110005  +  { 177, 1 },
       110006  +  { 179, 0 },
       110007  +  { 179, 2 },
       110008  +  { 181, 2 },
       110009  +  { 181, 3 },
       110010  +  { 181, 3 },
       110011  +  { 181, 3 },
       110012  +  { 182, 2 },
       110013  +  { 182, 2 },
       110014  +  { 182, 1 },
       110015  +  { 182, 1 },
109573 110016     { 182, 2 },
109574         -  { 182, 3 },
109575         -  { 182, 3 },
109576         -  { 182, 3 },
       110017  +  { 180, 3 },
       110018  +  { 180, 2 },
       110019  +  { 183, 0 },
109577 110020     { 183, 2 },
109578 110021     { 183, 2 },
109579         -  { 183, 1 },
109580         -  { 183, 1 },
109581         -  { 183, 2 },
109582         -  { 181, 3 },
109583         -  { 181, 2 },
109584         -  { 184, 0 },
109585         -  { 184, 2 },
109586         -  { 184, 2 },
109587 110022     { 159, 0 },
109588 110023     { 159, 2 },
109589         -  { 185, 3 },
109590         -  { 185, 2 },
       110024  +  { 184, 3 },
       110025  +  { 184, 1 },
109591 110026     { 185, 1 },
       110027  +  { 185, 0 },
109592 110028     { 186, 2 },
109593 110029     { 186, 7 },
109594 110030     { 186, 5 },
109595 110031     { 186, 5 },
109596 110032     { 186, 10 },
109597 110033     { 188, 0 },
109598 110034     { 188, 1 },
109599         -  { 176, 0 },
109600         -  { 176, 3 },
       110035  +  { 175, 0 },
       110036  +  { 175, 3 },
109601 110037     { 189, 0 },
109602 110038     { 189, 2 },
109603 110039     { 190, 1 },
109604 110040     { 190, 1 },
109605 110041     { 190, 1 },
109606 110042     { 147, 4 },
109607 110043     { 192, 2 },
................................................................................
109647 110083     { 209, 2 },
109648 110084     { 211, 4 },
109649 110085     { 211, 0 },
109650 110086     { 202, 0 },
109651 110087     { 202, 3 },
109652 110088     { 214, 4 },
109653 110089     { 214, 2 },
109654         -  { 177, 1 },
109655         -  { 177, 1 },
109656         -  { 177, 0 },
       110090  +  { 176, 1 },
       110091  +  { 176, 1 },
       110092  +  { 176, 0 },
109657 110093     { 200, 0 },
109658 110094     { 200, 3 },
109659 110095     { 201, 0 },
109660 110096     { 201, 2 },
109661 110097     { 203, 0 },
109662 110098     { 203, 2 },
109663 110099     { 203, 4 },
................................................................................
109675 110111     { 217, 1 },
109676 110112     { 219, 4 },
109677 110113     { 219, 5 },
109678 110114     { 218, 0 },
109679 110115     { 218, 3 },
109680 110116     { 213, 3 },
109681 110117     { 213, 1 },
109682         -  { 175, 1 },
109683         -  { 175, 3 },
       110118  +  { 174, 1 },
       110119  +  { 174, 3 },
       110120  +  { 173, 1 },
       110121  +  { 174, 1 },
109684 110122     { 174, 1 },
109685         -  { 175, 1 },
109686         -  { 175, 1 },
109687         -  { 175, 3 },
109688         -  { 175, 5 },
       110123  +  { 174, 3 },
       110124  +  { 174, 5 },
       110125  +  { 173, 1 },
       110126  +  { 173, 1 },
109689 110127     { 174, 1 },
109690 110128     { 174, 1 },
109691         -  { 175, 1 },
109692         -  { 175, 1 },
109693         -  { 175, 3 },
109694         -  { 175, 6 },
109695         -  { 175, 5 },
109696         -  { 175, 4 },
109697         -  { 174, 1 },
109698         -  { 175, 3 },
109699         -  { 175, 3 },
109700         -  { 175, 3 },
109701         -  { 175, 3 },
109702         -  { 175, 3 },
109703         -  { 175, 3 },
109704         -  { 175, 3 },
109705         -  { 175, 3 },
       110129  +  { 174, 3 },
       110130  +  { 174, 6 },
       110131  +  { 174, 5 },
       110132  +  { 174, 4 },
       110133  +  { 173, 1 },
       110134  +  { 174, 3 },
       110135  +  { 174, 3 },
       110136  +  { 174, 3 },
       110137  +  { 174, 3 },
       110138  +  { 174, 3 },
       110139  +  { 174, 3 },
       110140  +  { 174, 3 },
       110141  +  { 174, 3 },
109706 110142     { 221, 1 },
109707 110143     { 221, 2 },
109708 110144     { 221, 1 },
109709 110145     { 221, 2 },
109710         -  { 175, 3 },
109711         -  { 175, 5 },
109712         -  { 175, 2 },
109713         -  { 175, 3 },
109714         -  { 175, 3 },
109715         -  { 175, 4 },
109716         -  { 175, 2 },
109717         -  { 175, 2 },
109718         -  { 175, 2 },
109719         -  { 175, 2 },
       110146  +  { 174, 3 },
       110147  +  { 174, 5 },
       110148  +  { 174, 2 },
       110149  +  { 174, 3 },
       110150  +  { 174, 3 },
       110151  +  { 174, 4 },
       110152  +  { 174, 2 },
       110153  +  { 174, 2 },
       110154  +  { 174, 2 },
       110155  +  { 174, 2 },
109720 110156     { 222, 1 },
109721 110157     { 222, 2 },
109722         -  { 175, 5 },
       110158  +  { 174, 5 },
109723 110159     { 223, 1 },
109724 110160     { 223, 2 },
109725         -  { 175, 5 },
109726         -  { 175, 3 },
109727         -  { 175, 5 },
109728         -  { 175, 4 },
109729         -  { 175, 4 },
109730         -  { 175, 5 },
       110161  +  { 174, 5 },
       110162  +  { 174, 3 },
       110163  +  { 174, 5 },
       110164  +  { 174, 4 },
       110165  +  { 174, 4 },
       110166  +  { 174, 5 },
109731 110167     { 225, 5 },
109732 110168     { 225, 4 },
109733 110169     { 226, 2 },
109734 110170     { 226, 0 },
109735 110171     { 224, 1 },
109736 110172     { 224, 0 },
109737 110173     { 220, 1 },
109738 110174     { 220, 0 },
109739 110175     { 215, 3 },
109740 110176     { 215, 1 },
109741 110177     { 147, 11 },
109742 110178     { 227, 1 },
109743 110179     { 227, 0 },
109744         -  { 179, 0 },
109745         -  { 179, 3 },
       110180  +  { 178, 0 },
       110181  +  { 178, 3 },
109746 110182     { 187, 5 },
109747 110183     { 187, 3 },
109748 110184     { 228, 0 },
109749 110185     { 228, 2 },
109750 110186     { 147, 4 },
109751 110187     { 147, 1 },
109752 110188     { 147, 2 },
................................................................................
109785 110221     { 239, 3 },
109786 110222     { 239, 2 },
109787 110223     { 237, 7 },
109788 110224     { 237, 5 },
109789 110225     { 237, 5 },
109790 110226     { 237, 5 },
109791 110227     { 237, 1 },
109792         -  { 175, 4 },
109793         -  { 175, 6 },
       110228  +  { 174, 4 },
       110229  +  { 174, 6 },
109794 110230     { 191, 1 },
109795 110231     { 191, 1 },
109796 110232     { 191, 1 },
109797 110233     { 147, 4 },
109798 110234     { 147, 6 },
109799 110235     { 147, 3 },
109800 110236     { 241, 0 },
................................................................................
109933 110369   {
109934 110370     pParse->db->lookaside.bEnabled = 0;
109935 110371     yygotominor.yy0 = yymsp[0].minor.yy0;
109936 110372   }
109937 110373           break;
109938 110374         case 28: /* ifnotexists ::= */
109939 110375         case 31: /* temp ::= */ yytestcase(yyruleno==31);
109940         -      case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
109941         -      case 83: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
109942         -      case 85: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==85);
109943         -      case 87: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==87);
       110376  +      case 69: /* autoinc ::= */ yytestcase(yyruleno==69);
       110377  +      case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==82);
       110378  +      case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
       110379  +      case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
109944 110380         case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
109945 110381         case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
109946 110382         case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
109947 110383         case 121: /* distinct ::= */ yytestcase(yyruleno==121);
109948 110384         case 221: /* between_op ::= BETWEEN */ yytestcase(yyruleno==221);
109949 110385         case 224: /* in_op ::= IN */ yytestcase(yyruleno==224);
109950 110386   {yygotominor.yy392 = 0;}
109951 110387           break;
109952 110388         case 29: /* ifnotexists ::= IF NOT EXISTS */
109953 110389         case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
109954         -      case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
109955         -      case 86: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==86);
       110390  +      case 70: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==70);
       110391  +      case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==85);
109956 110392         case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
109957 110393         case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
109958 110394         case 222: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==222);
109959 110395         case 225: /* in_op ::= NOT IN */ yytestcase(yyruleno==225);
109960 110396   {yygotominor.yy392 = 1;}
109961 110397           break;
109962 110398         case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
................................................................................
109976 110412     yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
109977 110413   }
109978 110414           break;
109979 110415         case 37: /* columnid ::= nm */
109980 110416   {
109981 110417     sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
109982 110418     yygotominor.yy0 = yymsp[0].minor.yy0;
       110419  +  pParse->constraintName.n = 0;
109983 110420   }
109984 110421           break;
109985 110422         case 38: /* id ::= ID */
109986 110423         case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
109987 110424         case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
109988 110425         case 41: /* nm ::= id */ yytestcase(yyruleno==41);
109989 110426         case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
................................................................................
110021 110458     yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
110022 110459     yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
110023 110460   }
110024 110461           break;
110025 110462         case 50: /* typename ::= typename ids */
110026 110463   {yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
110027 110464           break;
110028         -      case 57: /* ccons ::= DEFAULT term */
110029         -      case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
       110465  +      case 55: /* ccons ::= CONSTRAINT nm */
       110466  +      case 93: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
       110467  +{pParse->constraintName = yymsp[0].minor.yy0;}
       110468  +        break;
       110469  +      case 56: /* ccons ::= DEFAULT term */
       110470  +      case 58: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==58);
110030 110471   {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy342);}
110031 110472           break;
110032         -      case 58: /* ccons ::= DEFAULT LP expr RP */
       110473  +      case 57: /* ccons ::= DEFAULT LP expr RP */
110033 110474   {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy342);}
110034 110475           break;
110035         -      case 60: /* ccons ::= DEFAULT MINUS term */
       110476  +      case 59: /* ccons ::= DEFAULT MINUS term */
110036 110477   {
110037 110478     ExprSpan v;
110038 110479     v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy342.pExpr, 0, 0);
110039 110480     v.zStart = yymsp[-1].minor.yy0.z;
110040 110481     v.zEnd = yymsp[0].minor.yy342.zEnd;
110041 110482     sqlite3AddDefaultValue(pParse,&v);
110042 110483   }
110043 110484           break;
110044         -      case 61: /* ccons ::= DEFAULT id */
       110485  +      case 60: /* ccons ::= DEFAULT id */
110045 110486   {
110046 110487     ExprSpan v;
110047 110488     spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
110048 110489     sqlite3AddDefaultValue(pParse,&v);
110049 110490   }
110050 110491           break;
110051         -      case 63: /* ccons ::= NOT NULL onconf */
       110492  +      case 62: /* ccons ::= NOT NULL onconf */
110052 110493   {sqlite3AddNotNull(pParse, yymsp[0].minor.yy392);}
110053 110494           break;
110054         -      case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
       110495  +      case 63: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
110055 110496   {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy392,yymsp[0].minor.yy392,yymsp[-2].minor.yy392);}
110056 110497           break;
110057         -      case 65: /* ccons ::= UNIQUE onconf */
       110498  +      case 64: /* ccons ::= UNIQUE onconf */
110058 110499   {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy392,0,0,0,0);}
110059 110500           break;
110060         -      case 66: /* ccons ::= CHECK LP expr RP */
       110501  +      case 65: /* ccons ::= CHECK LP expr RP */
110061 110502   {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy342.pExpr);}
110062 110503           break;
110063         -      case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */
       110504  +      case 66: /* ccons ::= REFERENCES nm idxlist_opt refargs */
110064 110505   {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy442,yymsp[0].minor.yy392);}
110065 110506           break;
110066         -      case 68: /* ccons ::= defer_subclause */
       110507  +      case 67: /* ccons ::= defer_subclause */
110067 110508   {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy392);}
110068 110509           break;
110069         -      case 69: /* ccons ::= COLLATE ids */
       110510  +      case 68: /* ccons ::= COLLATE ids */
110070 110511   {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
110071 110512           break;
110072         -      case 72: /* refargs ::= */
       110513  +      case 71: /* refargs ::= */
110073 110514   { yygotominor.yy392 = OE_None*0x0101; /* EV: R-19803-45884 */}
110074 110515           break;
110075         -      case 73: /* refargs ::= refargs refarg */
       110516  +      case 72: /* refargs ::= refargs refarg */
110076 110517   { yygotominor.yy392 = (yymsp[-1].minor.yy392 & ~yymsp[0].minor.yy207.mask) | yymsp[0].minor.yy207.value; }
110077 110518           break;
110078         -      case 74: /* refarg ::= MATCH nm */
110079         -      case 75: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==75);
       110519  +      case 73: /* refarg ::= MATCH nm */
       110520  +      case 74: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==74);
110080 110521   { yygotominor.yy207.value = 0;     yygotominor.yy207.mask = 0x000000; }
110081 110522           break;
110082         -      case 76: /* refarg ::= ON DELETE refact */
       110523  +      case 75: /* refarg ::= ON DELETE refact */
110083 110524   { yygotominor.yy207.value = yymsp[0].minor.yy392;     yygotominor.yy207.mask = 0x0000ff; }
110084 110525           break;
110085         -      case 77: /* refarg ::= ON UPDATE refact */
       110526  +      case 76: /* refarg ::= ON UPDATE refact */
110086 110527   { yygotominor.yy207.value = yymsp[0].minor.yy392<<8;  yygotominor.yy207.mask = 0x00ff00; }
110087 110528           break;
110088         -      case 78: /* refact ::= SET NULL */
       110529  +      case 77: /* refact ::= SET NULL */
110089 110530   { yygotominor.yy392 = OE_SetNull;  /* EV: R-33326-45252 */}
110090 110531           break;
110091         -      case 79: /* refact ::= SET DEFAULT */
       110532  +      case 78: /* refact ::= SET DEFAULT */
110092 110533   { yygotominor.yy392 = OE_SetDflt;  /* EV: R-33326-45252 */}
110093 110534           break;
110094         -      case 80: /* refact ::= CASCADE */
       110535  +      case 79: /* refact ::= CASCADE */
110095 110536   { yygotominor.yy392 = OE_Cascade;  /* EV: R-33326-45252 */}
110096 110537           break;
110097         -      case 81: /* refact ::= RESTRICT */
       110538  +      case 80: /* refact ::= RESTRICT */
110098 110539   { yygotominor.yy392 = OE_Restrict; /* EV: R-33326-45252 */}
110099 110540           break;
110100         -      case 82: /* refact ::= NO ACTION */
       110541  +      case 81: /* refact ::= NO ACTION */
110101 110542   { yygotominor.yy392 = OE_None;     /* EV: R-33326-45252 */}
110102 110543           break;
110103         -      case 84: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
       110544  +      case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
110104 110545         case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
110105 110546         case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
110106 110547         case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
110107 110548   {yygotominor.yy392 = yymsp[0].minor.yy392;}
110108 110549           break;
110109         -      case 88: /* conslist_opt ::= */
       110550  +      case 87: /* conslist_opt ::= */
110110 110551   {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
110111 110552           break;
110112         -      case 89: /* conslist_opt ::= COMMA conslist */
       110553  +      case 88: /* conslist_opt ::= COMMA conslist */
110113 110554   {yygotominor.yy0 = yymsp[-1].minor.yy0;}
       110555  +        break;
       110556  +      case 91: /* tconscomma ::= COMMA */
       110557  +{pParse->constraintName.n = 0;}
110114 110558           break;
110115 110559         case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
110116 110560   {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy442,yymsp[0].minor.yy392,yymsp[-2].minor.yy392,0);}
110117 110561           break;
110118 110562         case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
110119 110563   {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy442,yymsp[0].minor.yy392,0,0,0,0);}
110120 110564           break;
................................................................................
110523 110967         case 204: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==204);
110524 110968         case 205: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==205);
110525 110969         case 206: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==206);
110526 110970   {spanBinaryExpr(&yygotominor.yy342,pParse,yymsp[-1].major,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy342);}
110527 110971           break;
110528 110972         case 207: /* likeop ::= LIKE_KW */
110529 110973         case 209: /* likeop ::= MATCH */ yytestcase(yyruleno==209);
110530         -{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.not = 0;}
       110974  +{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.bNot = 0;}
110531 110975           break;
110532 110976         case 208: /* likeop ::= NOT LIKE_KW */
110533 110977         case 210: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==210);
110534         -{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.not = 1;}
       110978  +{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.bNot = 1;}
110535 110979           break;
110536 110980         case 211: /* expr ::= expr likeop expr */
110537 110981   {
110538 110982     ExprList *pList;
110539 110983     pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy342.pExpr);
110540 110984     pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy342.pExpr);
110541 110985     yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy318.eOperator);
110542         -  if( yymsp[-1].minor.yy318.not ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
       110986  +  if( yymsp[-1].minor.yy318.bNot ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
110543 110987     yygotominor.yy342.zStart = yymsp[-2].minor.yy342.zStart;
110544 110988     yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
110545 110989     if( yygotominor.yy342.pExpr ) yygotominor.yy342.pExpr->flags |= EP_InfixFunc;
110546 110990   }
110547 110991           break;
110548 110992         case 212: /* expr ::= expr likeop expr ESCAPE expr */
110549 110993   {
110550 110994     ExprList *pList;
110551 110995     pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr);
110552 110996     pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy342.pExpr);
110553 110997     pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy342.pExpr);
110554 110998     yygotominor.yy342.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy318.eOperator);
110555         -  if( yymsp[-3].minor.yy318.not ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
       110999  +  if( yymsp[-3].minor.yy318.bNot ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
110556 111000     yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
110557 111001     yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
110558 111002     if( yygotominor.yy342.pExpr ) yygotominor.yy342.pExpr->flags |= EP_InfixFunc;
110559 111003   }
110560 111004           break;
110561 111005         case 213: /* expr ::= expr ISNULL|NOTNULL */
110562 111006   {spanUnaryPostfix(&yygotominor.yy342,pParse,yymsp[0].major,&yymsp[-1].minor.yy342,&yymsp[0].minor.yy0);}
................................................................................
110975 111419         /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
110976 111420         /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
110977 111421         /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
110978 111422         /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
110979 111423         /* (44) type ::= */ yytestcase(yyruleno==44);
110980 111424         /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
110981 111425         /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
110982         -      /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
       111426  +      /* (53) carglist ::= carglist ccons */ yytestcase(yyruleno==53);
110983 111427         /* (54) carglist ::= */ yytestcase(yyruleno==54);
110984         -      /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
110985         -      /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
110986         -      /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
110987         -      /* (90) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==90);
110988         -      /* (91) conslist ::= conslist tcons */ yytestcase(yyruleno==91);
110989         -      /* (92) conslist ::= tcons */ yytestcase(yyruleno==92);
110990         -      /* (93) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
       111428  +      /* (61) ccons ::= NULL onconf */ yytestcase(yyruleno==61);
       111429  +      /* (89) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==89);
       111430  +      /* (90) conslist ::= tcons */ yytestcase(yyruleno==90);
       111431  +      /* (92) tconscomma ::= */ yytestcase(yyruleno==92);
110991 111432         /* (277) foreach_clause ::= */ yytestcase(yyruleno==277);
110992 111433         /* (278) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==278);
110993 111434         /* (285) tridxby ::= */ yytestcase(yyruleno==285);
110994 111435         /* (303) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==303);
110995 111436         /* (304) database_kw_opt ::= */ yytestcase(yyruleno==304);
110996 111437         /* (312) kwcolumn_opt ::= */ yytestcase(yyruleno==312);
110997 111438         /* (313) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==313);
................................................................................
115891 116332   **
115892 116333   ** FTS3 used to optionally store character offsets using a compile-time
115893 116334   ** option.  But that functionality is no longer supported.
115894 116335   **
115895 116336   ** A doclist is stored like this:
115896 116337   **
115897 116338   ** array {
115898         -**   varint docid;
       116339  +**   varint docid;          (delta from previous doclist)
115899 116340   **   array {                (position list for column 0)
115900 116341   **     varint position;     (2 more than the delta from previous position)
115901 116342   **   }
115902 116343   **   array {
115903 116344   **     varint POS_COLUMN;   (marks start of position list for new column)
115904 116345   **     varint column;       (index of new column)
115905 116346   **     array {
................................................................................
115922 116363   **   value:     123 5 9 1 1 14 35 0 234 72 0
115923 116364   **
115924 116365   ** The 123 value is the first docid.  For column zero in this document
115925 116366   ** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
115926 116367   ** at D signals the start of a new column; the 1 at E indicates that the
115927 116368   ** new column is column number 1.  There are two positions at 12 and 45
115928 116369   ** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
115929         -** 234 at I is the next docid.  It has one position 72 (72-2) and then
115930         -** terminates with the 0 at K.
       116370  +** 234 at I is the delta to next docid (357).  It has one position 70
       116371  +** (72-2) and then terminates with the 0 at K.
115931 116372   **
115932 116373   ** A "position-list" is the list of positions for multiple columns for
115933 116374   ** a single docid.  A "column-list" is the set of positions for a single
115934 116375   ** column.  Hence, a position-list consists of one or more column-lists,
115935 116376   ** a document record consists of a docid followed by a position-list and
115936 116377   ** a doclist consists of one or more document records.
115937 116378   **
................................................................................
116458 116899   */
116459 116900   #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
116460 116901   
116461 116902   
116462 116903   #ifndef MIN
116463 116904   # define MIN(x,y) ((x)<(y)?(x):(y))
116464 116905   #endif
       116906  +#ifndef MAX
       116907  +# define MAX(x,y) ((x)>(y)?(x):(y))
       116908  +#endif
116465 116909   
116466 116910   /*
116467 116911   ** Maximum length of a varint encoded integer. The varint format is different
116468 116912   ** from that used by SQLite, so the maximum length is 10, not 9.
116469 116913   */
116470 116914   #define FTS3_VARINT_MAX 10
116471 116915   
................................................................................
116512 116956   ** false.
116513 116957   */
116514 116958   #ifdef SQLITE_COVERAGE_TEST
116515 116959   # define ALWAYS(x) (1)
116516 116960   # define NEVER(X)  (0)
116517 116961   #else
116518 116962   # define ALWAYS(x) (x)
116519         -# define NEVER(X)  (x)
       116963  +# define NEVER(x)  (x)
116520 116964   #endif
116521 116965   
116522 116966   /*
116523 116967   ** Internal types used by SQLite.
116524 116968   */
116525 116969   typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
116526 116970   typedef short int i16;            /* 2-byte (or larger) signed integer */
116527 116971   typedef unsigned int u32;         /* 4-byte unsigned integer */
116528 116972   typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
       116973  +typedef sqlite3_int64 i64;        /* 8-byte signed integer */
116529 116974   
116530 116975   /*
116531 116976   ** Macro used to suppress compiler warnings for unused parameters.
116532 116977   */
116533 116978   #define UNUSED_PARAMETER(x) (void)(x)
116534 116979   
116535 116980   /*
................................................................................
116584 117029     const char *zDb;                /* logical database name */
116585 117030     const char *zName;              /* virtual table name */
116586 117031     int nColumn;                    /* number of named columns in virtual table */
116587 117032     char **azColumn;                /* column names.  malloced */
116588 117033     sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
116589 117034     char *zContentTbl;              /* content=xxx option, or NULL */
116590 117035     char *zLanguageid;              /* languageid=xxx option, or NULL */
       117036  +  u8 bAutoincrmerge;              /* True if automerge=1 */
       117037  +  u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
116591 117038   
116592 117039     /* Precompiled statements used by the implementation. Each of these 
116593 117040     ** statements is run and reset within a single virtual table API call. 
116594 117041     */
116595         -  sqlite3_stmt *aStmt[28];
       117042  +  sqlite3_stmt *aStmt[37];
116596 117043   
116597 117044     char *zReadExprlist;
116598 117045     char *zWriteExprlist;
116599 117046   
116600 117047     int nNodeSize;                  /* Soft limit for node size */
       117048  +  u8 bFts4;                       /* True for FTS4, false for FTS3 */
116601 117049     u8 bHasStat;                    /* True if %_stat table exists */
116602 117050     u8 bHasDocsize;                 /* True if %_docsize table exists */
116603 117051     u8 bDescIdx;                    /* True if doclists are in reverse order */
       117052  +  u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
116604 117053     int nPgsz;                      /* Page size for host database */
116605 117054     char *zSegmentsTbl;             /* Name of %_segments table */
116606 117055     sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
116607 117056   
116608         -  /* TODO: Fix the first paragraph of this comment.
116609         -  **
       117057  +  /* 
116610 117058     ** The following array of hash tables is used to buffer pending index 
116611         -  ** updates during transactions. Variable nPendingData estimates the memory 
116612         -  ** size of the pending data, including hash table overhead, not including
116613         -  ** malloc overhead.  When nPendingData exceeds nMaxPendingData, the buffer 
116614         -  ** is flushed automatically. Variable iPrevDocid is the docid of the most 
116615         -  ** recently inserted record.
       117059  +  ** updates during transactions. All pending updates buffered at any one
       117060  +  ** time must share a common language-id (see the FTS4 langid= feature).
       117061  +  ** The current language id is stored in variable iPrevLangid.
116616 117062     **
116617 117063     ** A single FTS4 table may have multiple full-text indexes. For each index
116618 117064     ** there is an entry in the aIndex[] array. Index 0 is an index of all the
116619 117065     ** terms that appear in the document set. Each subsequent index in aIndex[]
116620 117066     ** is an index of prefixes of a specific length.
       117067  +  **
       117068  +  ** Variable nPendingData contains an estimate the memory consumed by the 
       117069  +  ** pending data structures, including hash table overhead, but not including
       117070  +  ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
       117071  +  ** tables are flushed to disk. Variable iPrevDocid is the docid of the most 
       117072  +  ** recently inserted record.
116621 117073     */
116622 117074     int nIndex;                     /* Size of aIndex[] */
116623 117075     struct Fts3Index {
116624 117076       int nPrefix;                  /* Prefix length (0 for main terms index) */
116625 117077       Fts3Hash hPending;            /* Pending terms table for this index */
116626 117078     } *aIndex;
116627 117079     int nMaxPendingData;            /* Max pending data before flush to disk */
................................................................................
116812 117264   SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
116813 117265   
116814 117266   SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
116815 117267   SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
116816 117268   SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
116817 117269   SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
116818 117270   SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
       117271  +SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
116819 117272   
116820 117273   /* Special values interpreted by sqlite3SegReaderCursor() */
116821 117274   #define FTS3_SEGCURSOR_PENDING        -1
116822 117275   #define FTS3_SEGCURSOR_ALL            -2
116823 117276   
116824 117277   SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
116825 117278   SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
................................................................................
116863 117316     /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
116864 117317     char *zTerm;                    /* Pointer to term buffer */
116865 117318     int nTerm;                      /* Size of zTerm in bytes */
116866 117319     char *aDoclist;                 /* Pointer to doclist buffer */
116867 117320     int nDoclist;                   /* Size of aDoclist[] in bytes */
116868 117321   };
116869 117322   
       117323  +SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
       117324  +
116870 117325   /* fts3.c */
116871 117326   SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
116872 117327   SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
116873 117328   SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
116874 117329   SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
116875 117330   SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
116876 117331   SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
116877 117332   SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
116878 117333   SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
       117334  +SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
116879 117335   
116880 117336   /* fts3_tokenizer.c */
116881 117337   SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
116882 117338   SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
116883 117339   SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, 
116884 117340       sqlite3_tokenizer **, char **
116885 117341   );
................................................................................
116911 117367   
116912 117368   SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
116913 117369   
116914 117370   SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
116915 117371       Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
116916 117372   SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
116917 117373       Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
116918         -SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol); 
       117374  +SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); 
116919 117375   SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
116920 117376   SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
116921 117377   
116922 117378   SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
116923 117379   
116924 117380   #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
116925 117381   #endif /* _FTSINT_H */
................................................................................
117200 117656       }
117201 117657   
117202 117658       sqlite3_free(zSql);
117203 117659       sqlite3_free(zCols);
117204 117660       *pRc = rc;
117205 117661     }
117206 117662   }
       117663  +
       117664  +/*
       117665  +** Create the %_stat table if it does not already exist.
       117666  +*/
       117667  +SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
       117668  +  fts3DbExec(pRc, p->db, 
       117669  +      "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
       117670  +          "(id INTEGER PRIMARY KEY, value BLOB);",
       117671  +      p->zDb, p->zName
       117672  +  );
       117673  +  if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
       117674  +}
117207 117675   
117208 117676   /*
117209 117677   ** Create the backing store tables (%_content, %_segments and %_segdir)
117210 117678   ** required by the FTS3 table passed as the only argument. This is done
117211 117679   ** as part of the vtab xCreate() method.
117212 117680   **
117213 117681   ** If the p->bHasDocsize boolean is true (indicating that this is an
................................................................................
117261 117729     );
117262 117730     if( p->bHasDocsize ){
117263 117731       fts3DbExec(&rc, db, 
117264 117732           "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
117265 117733           p->zDb, p->zName
117266 117734       );
117267 117735     }
       117736  +  assert( p->bHasStat==p->bFts4 );
117268 117737     if( p->bHasStat ){
117269         -    fts3DbExec(&rc, db, 
117270         -        "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);",
117271         -        p->zDb, p->zName
117272         -    );
       117738  +    sqlite3Fts3CreateStatTable(&rc, p);
117273 117739     }
117274 117740     return rc;
117275 117741   }
117276 117742   
117277 117743   /*
117278 117744   ** Store the current database page-size in bytes in p->nPgsz.
117279 117745   **
................................................................................
117906 118372     p->nColumn = nCol;
117907 118373     p->nPendingData = 0;
117908 118374     p->azColumn = (char **)&p[1];
117909 118375     p->pTokenizer = pTokenizer;
117910 118376     p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
117911 118377     p->bHasDocsize = (isFts4 && bNoDocsize==0);
117912 118378     p->bHasStat = isFts4;
       118379  +  p->bFts4 = isFts4;
117913 118380     p->bDescIdx = bDescIdx;
       118381  +  p->bAutoincrmerge = 0xff;   /* 0xff means setting unknown */
117914 118382     p->zContentTbl = zContent;
117915 118383     p->zLanguageid = zLanguageid;
117916 118384     zContent = 0;
117917 118385     zLanguageid = 0;
117918 118386     TESTONLY( p->inTransaction = -1 );
117919 118387     TESTONLY( p->mxSavepoint = -1 );
117920 118388   
................................................................................
117958 118426   
117959 118427     /* If this is an xCreate call, create the underlying tables in the 
117960 118428     ** database. TODO: For xConnect(), it could verify that said tables exist.
117961 118429     */
117962 118430     if( isCreate ){
117963 118431       rc = fts3CreateTables(p);
117964 118432     }
       118433  +
       118434  +  /* Check to see if a legacy fts3 table has been "upgraded" by the
       118435  +  ** addition of a %_stat table so that it can use incremental merge.
       118436  +  */
       118437  +  if( !isFts4 && !isCreate ){
       118438  +    int rc2 = SQLITE_OK;
       118439  +    fts3DbExec(&rc2, db, "SELECT 1 FROM %Q.'%q_stat' WHERE id=2",
       118440  +               p->zDb, p->zName);
       118441  +    if( rc2==SQLITE_OK ) p->bHasStat = 1;
       118442  +  }
117965 118443   
117966 118444     /* Figure out the page-size for the database. This is required in order to
117967 118445     ** estimate the cost of loading large doclists from the database.  */
117968 118446     fts3DatabasePageSize(&rc, p);
117969 118447     p->nNodeSize = p->nPgsz-35;
117970 118448   
117971 118449     /* Declare the table schema to SQLite. */
................................................................................
119302 119780   
119303 119781   /*
119304 119782   ** Set up a cursor object for iterating through a full-text index or a 
119305 119783   ** single level therein.
119306 119784   */
119307 119785   SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
119308 119786     Fts3Table *p,                   /* FTS3 table handle */
119309         -  int iLangid,
       119787  +  int iLangid,                    /* Language-id to search */
119310 119788     int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
119311 119789     int iLevel,                     /* Level of segments to scan */
119312 119790     const char *zTerm,              /* Term to query for */
119313 119791     int nTerm,                      /* Size of zTerm in bytes */
119314 119792     int isPrefix,                   /* True for a prefix search */
119315 119793     int isScan,                     /* True to scan from zTerm to EOF */
119316 119794     Fts3MultiSegReader *pCsr       /* Cursor object to populate */
................................................................................
119320 119798         ||  iLevel==FTS3_SEGCURSOR_PENDING 
119321 119799         ||  iLevel>=0
119322 119800     );
119323 119801     assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
119324 119802     assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
119325 119803     assert( isPrefix==0 || isScan==0 );
119326 119804   
119327         -  /* "isScan" is only set to true by the ft4aux module, an ordinary
119328         -  ** full-text tables. */
119329         -  assert( isScan==0 || p->aIndex==0 );
119330         -
119331 119805     memset(pCsr, 0, sizeof(Fts3MultiSegReader));
119332         -
119333 119806     return fts3SegReaderCursor(
119334 119807         p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
119335 119808     );
119336 119809   }
119337 119810   
119338 119811   /*
119339 119812   ** In addition to its current configuration, have the Fts3MultiSegReader
................................................................................
119590 120063         return SQLITE_NOMEM;
119591 120064       }
119592 120065   
119593 120066       pCsr->iLangid = 0;
119594 120067       if( nVal==2 ) pCsr->iLangid = sqlite3_value_int(apVal[1]);
119595 120068   
119596 120069       rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
119597         -        p->azColumn, p->bHasStat, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
       120070  +        p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
119598 120071       );
119599 120072       if( rc!=SQLITE_OK ){
119600 120073         if( rc==SQLITE_ERROR ){
119601 120074           static const char *zErr = "malformed MATCH expression: [%s]";
119602 120075           p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
119603 120076         }
119604 120077         return rc;
................................................................................
119733 120206   }
119734 120207   
119735 120208   /*
119736 120209   ** Implementation of xSync() method. Flush the contents of the pending-terms
119737 120210   ** hash-table to the database.
119738 120211   */
119739 120212   static int fts3SyncMethod(sqlite3_vtab *pVtab){
119740         -  int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
119741         -  sqlite3Fts3SegmentsClose((Fts3Table *)pVtab);
       120213  +
       120214  +  /* Following an incremental-merge operation, assuming that the input
       120215  +  ** segments are not completely consumed (the usual case), they are updated
       120216  +  ** in place to remove the entries that have already been merged. This
       120217  +  ** involves updating the leaf block that contains the smallest unmerged
       120218  +  ** entry and each block (if any) between the leaf and the root node. So
       120219  +  ** if the height of the input segment b-trees is N, and input segments
       120220  +  ** are merged eight at a time, updating the input segments at the end
       120221  +  ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
       120222  +  ** small - often between 0 and 2. So the overhead of the incremental
       120223  +  ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
       120224  +  ** dwarfing the actual productive work accomplished, the incremental merge
       120225  +  ** is only attempted if it will write at least 64 leaf blocks. Hence
       120226  +  ** nMinMerge.
       120227  +  **
       120228  +  ** Of course, updating the input segments also involves deleting a bunch
       120229  +  ** of blocks from the segments table. But this is not considered overhead
       120230  +  ** as it would also be required by a crisis-merge that used the same input 
       120231  +  ** segments.
       120232  +  */
       120233  +  const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
       120234  +
       120235  +  Fts3Table *p = (Fts3Table*)pVtab;
       120236  +  int rc = sqlite3Fts3PendingTermsFlush(p);
       120237  +
       120238  +  if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
       120239  +    int mxLevel = 0;              /* Maximum relative level value in db */
       120240  +    int A;                        /* Incr-merge parameter A */
       120241  +
       120242  +    rc = sqlite3Fts3MaxLevel(p, &mxLevel);
       120243  +    assert( rc==SQLITE_OK || mxLevel==0 );
       120244  +    A = p->nLeafAdd * mxLevel;
       120245  +    A += (A/2);
       120246  +    if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
       120247  +  }
       120248  +  sqlite3Fts3SegmentsClose(p);
119742 120249     return rc;
119743 120250   }
119744 120251   
119745 120252   /*
119746 120253   ** Implementation of xBegin() method. This is a no-op.
119747 120254   */
119748 120255   static int fts3BeginMethod(sqlite3_vtab *pVtab){
119749         -  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
       120256  +  Fts3Table *p = (Fts3Table*)pVtab;
119750 120257     UNUSED_PARAMETER(pVtab);
119751 120258     assert( p->pSegments==0 );
119752 120259     assert( p->nPendingData==0 );
119753 120260     assert( p->inTransaction!=1 );
119754 120261     TESTONLY( p->inTransaction = 1 );
119755 120262     TESTONLY( p->mxSavepoint = -1; );
       120263  +  p->nLeafAdd = 0;
119756 120264     return SQLITE_OK;
119757 120265   }
119758 120266   
119759 120267   /*
119760 120268   ** Implementation of xCommit() method. This is a no-op. The contents of
119761 120269   ** the pending-terms hash-table have already been flushed into the database
119762 120270   ** by fts3SyncMethod().
................................................................................
120043 120551   
120044 120552   /*
120045 120553   ** The xSavepoint() method.
120046 120554   **
120047 120555   ** Flush the contents of the pending-terms table to disk.
120048 120556   */
120049 120557   static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
       120558  +  int rc = SQLITE_OK;
120050 120559     UNUSED_PARAMETER(iSavepoint);
120051 120560     assert( ((Fts3Table *)pVtab)->inTransaction );
120052 120561     assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
120053 120562     TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
120054         -  return fts3SyncMethod(pVtab);
       120563  +  if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
       120564  +    rc = fts3SyncMethod(pVtab);
       120565  +  }
       120566  +  return rc;
120055 120567   }
120056 120568   
120057 120569   /*
120058 120570   ** The xRelease() method.
120059 120571   **
120060 120572   ** This is a no-op.
120061 120573   */
................................................................................
120520 121032   */
120521 121033   SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
120522 121034     int bDescIdx,                   /* True if the doclist is desc */
120523 121035     char *aDoclist,                 /* Pointer to entire doclist */
120524 121036     int nDoclist,                   /* Length of aDoclist in bytes */
120525 121037     char **ppIter,                  /* IN/OUT: Iterator pointer */
120526 121038     sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
120527         -  int *pnList,                    /* IN/OUT: List length pointer */
       121039  +  int *pnList,                    /* OUT: List length pointer */
120528 121040     u8 *pbEof                       /* OUT: End-of-file flag */
120529 121041   ){
120530 121042     char *p = *ppIter;
120531 121043   
120532 121044     assert( nDoclist>0 );
120533 121045     assert( *pbEof==0 );
120534 121046     assert( p || *piDocid==0 );
................................................................................
120566 121078         char *pSave = p;
120567 121079         fts3ReversePoslist(aDoclist, &p);
120568 121080         *pnList = (int)(pSave - p);
120569 121081       }
120570 121082       *ppIter = p;
120571 121083     }
120572 121084   }
       121085  +
       121086  +/*
       121087  +** Iterate forwards through a doclist.
       121088  +*/
       121089  +SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
       121090  +  int bDescIdx,                   /* True if the doclist is desc */
       121091  +  char *aDoclist,                 /* Pointer to entire doclist */
       121092  +  int nDoclist,                   /* Length of aDoclist in bytes */
       121093  +  char **ppIter,                  /* IN/OUT: Iterator pointer */
       121094  +  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
       121095  +  u8 *pbEof                       /* OUT: End-of-file flag */
       121096  +){
       121097  +  char *p = *ppIter;
       121098  +
       121099  +  assert( nDoclist>0 );
       121100  +  assert( *pbEof==0 );
       121101  +  assert( p || *piDocid==0 );
       121102  +  assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
       121103  +
       121104  +  if( p==0 ){
       121105  +    p = aDoclist;
       121106  +    p += sqlite3Fts3GetVarint(p, piDocid);
       121107  +  }else{
       121108  +    fts3PoslistCopy(0, &p);
       121109  +    if( p>=&aDoclist[nDoclist] ){
       121110  +      *pbEof = 1;
       121111  +    }else{
       121112  +      sqlite3_int64 iVar;
       121113  +      p += sqlite3Fts3GetVarint(p, &iVar);
       121114  +      *piDocid += ((bDescIdx ? -1 : 1) * iVar);
       121115  +    }
       121116  +  }
       121117  +
       121118  +  *ppIter = p;
       121119  +}
120573 121120   
120574 121121   /*
120575 121122   ** Attempt to move the phrase iterator to point to the next matching docid. 
120576 121123   ** If an error occurs, return an SQLite error code. Otherwise, return 
120577 121124   ** SQLITE_OK.
120578 121125   **
120579 121126   ** If there is no "next" entry and no error occurs, then *pbEof is set to
................................................................................
120962 121509     int nToken = 0;
120963 121510     int nOr = 0;
120964 121511   
120965 121512     /* Allocate a MultiSegReader for each token in the expression. */
120966 121513     fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
120967 121514   
120968 121515     /* Determine which, if any, tokens in the expression should be deferred. */
120969         -  if( rc==SQLITE_OK && nToken>1 && pTab->bHasStat ){
       121516  +  if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
120970 121517       Fts3TokenAndCost *aTC;
120971 121518       Fts3Expr **apOr;
120972 121519       aTC = (Fts3TokenAndCost *)sqlite3_malloc(
120973 121520           sizeof(Fts3TokenAndCost) * nToken
120974 121521         + sizeof(Fts3Expr *) * nOr * 2
120975 121522       );
120976 121523       apOr = (Fts3Expr **)&aTC[nToken];
................................................................................
121722 122269   ** for 'X' is requested, the buffer returned may contain:
121723 122270   **
121724 122271   **     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
121725 122272   **
121726 122273   ** This function works regardless of whether or not the phrase is deferred,
121727 122274   ** incremental, or neither.
121728 122275   */
121729         -SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(
       122276  +SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
121730 122277     Fts3Cursor *pCsr,               /* FTS3 cursor object */
121731 122278     Fts3Expr *pExpr,                /* Phrase to return doclist for */
121732         -  int iCol                        /* Column to return position list for */
       122279  +  int iCol,                       /* Column to return position list for */
       122280  +  char **ppOut                    /* OUT: Pointer to position list */
121733 122281   ){
121734 122282     Fts3Phrase *pPhrase = pExpr->pPhrase;
121735 122283     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
121736         -  char *pIter = pPhrase->doclist.pList;
       122284  +  char *pIter;
121737 122285     int iThis;
       122286  +  sqlite3_int64 iDocid;
121738 122287   
       122288  +  /* If this phrase is applies specifically to some column other than 
       122289  +  ** column iCol, return a NULL pointer.  */
       122290  +  *ppOut = 0;
121739 122291     assert( iCol>=0 && iCol<pTab->nColumn );
121740         -  if( !pIter 
121741         -   || pExpr->bEof 
121742         -   || pExpr->iDocid!=pCsr->iPrevId
121743         -   || (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) 
121744         -  ){
121745         -    return 0;
121746         -  }
121747         -
121748         -  assert( pPhrase->doclist.nList>0 );
       122292  +  if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
       122293  +    return SQLITE_OK;
       122294  +  }
       122295  +
       122296  +  iDocid = pExpr->iDocid;
       122297  +  pIter = pPhrase->doclist.pList;
       122298  +  if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
       122299  +    int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
       122300  +    int bOr = 0;
       122301  +    u8 bEof = 0;
       122302  +    Fts3Expr *p;
       122303  +
       122304  +    /* Check if this phrase descends from an OR expression node. If not, 
       122305  +    ** return NULL. Otherwise, the entry that corresponds to docid 
       122306  +    ** pCsr->iPrevId may lie earlier in the doclist buffer. */
       122307  +    for(p=pExpr->pParent; p; p=p->pParent){
       122308  +      if( p->eType==FTSQUERY_OR ) bOr = 1;
       122309  +    }
       122310  +    if( bOr==0 ) return SQLITE_OK;
       122311  +
       122312  +    /* This is the descendent of an OR node. In this case we cannot use
       122313  +    ** an incremental phrase. Load the entire doclist for the phrase
       122314  +    ** into memory in this case.  */
       122315  +    if( pPhrase->bIncr ){
       122316  +      int rc = SQLITE_OK;
       122317  +      int bEofSave = pExpr->bEof;
       122318  +      fts3EvalRestart(pCsr, pExpr, &rc);
       122319  +      while( rc==SQLITE_OK && !pExpr->bEof ){
       122320  +        fts3EvalNextRow(pCsr, pExpr, &rc);
       122321  +        if( bEofSave==0 && pExpr->iDocid==iDocid ) break;
       122322  +      }
       122323  +      pIter = pPhrase->doclist.pList;
       122324  +      assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
       122325  +      if( rc!=SQLITE_OK ) return rc;
       122326  +    }
       122327  +
       122328  +    if( pExpr->bEof ){
       122329  +      pIter = 0;
       122330  +      iDocid = 0;
       122331  +    }
       122332  +    bEof = (pPhrase->doclist.nAll==0);
       122333  +    assert( bDescDoclist==0 || bDescDoclist==1 );
       122334  +    assert( pCsr->bDesc==0 || pCsr->bDesc==1 );
       122335  +
       122336  +    if( pCsr->bDesc==bDescDoclist ){
       122337  +      int dummy;
       122338  +      while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
       122339  +        sqlite3Fts3DoclistPrev(
       122340  +            bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
       122341  +            &pIter, &iDocid, &dummy, &bEof
       122342  +        );
       122343  +      }
       122344  +    }else{
       122345  +      while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
       122346  +        sqlite3Fts3DoclistNext(
       122347  +            bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
       122348  +            &pIter, &iDocid, &bEof
       122349  +        );
       122350  +      }
       122351  +    }
       122352  +
       122353  +    if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
       122354  +  }
       122355  +  if( pIter==0 ) return SQLITE_OK;
       122356  +
121749 122357     if( *pIter==0x01 ){
121750 122358       pIter++;
121751 122359       pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
121752 122360     }else{
121753 122361       iThis = 0;
121754 122362     }
121755 122363     while( iThis<iCol ){
121756 122364       fts3ColumnlistCopy(0, &pIter);
121757 122365       if( *pIter==0x00 ) return 0;
121758 122366       pIter++;
121759 122367       pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
121760 122368     }
121761 122369   
121762         -  return ((iCol==iThis)?pIter:0);
       122370  +  *ppOut = ((iCol==iThis)?pIter:0);
       122371  +  return SQLITE_OK;
121763 122372   }
121764 122373   
121765 122374   /*
121766 122375   ** Free all components of the Fts3Phrase structure that were allocated by
121767 122376   ** the eval module. Specifically, this means to free:
121768 122377   **
121769 122378   **   * the contents of pPhrase->doclist, and
................................................................................
121777 122386       memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
121778 122387       for(i=0; i<pPhrase->nToken; i++){
121779 122388         fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
121780 122389         pPhrase->aToken[i].pSegcsr = 0;
121781 122390       }
121782 122391     }
121783 122392   }
       122393  +
121784 122394   
121785 122395   /*
121786 122396   ** Return SQLITE_CORRUPT_VTAB.
121787 122397   */
121788 122398   #ifdef SQLITE_DEBUG
121789 122399   SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
121790 122400     return SQLITE_CORRUPT_VTAB;
................................................................................
125077 125687   
125078 125688   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
125079 125689   
125080 125690   /* #include <string.h> */
125081 125691   /* #include <assert.h> */
125082 125692   /* #include <stdlib.h> */
125083 125693   
       125694  +
       125695  +#define FTS_MAX_APPENDABLE_HEIGHT 16
       125696  +
125084 125697   /*
125085 125698   ** When full-text index nodes are loaded from disk, the buffer that they
125086 125699   ** are loaded into has the following number of bytes of padding at the end 
125087 125700   ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
125088 125701   ** of 920 bytes is allocated for it.
125089 125702   **
125090 125703   ** This means that if we have a pointer into a buffer containing node data,
................................................................................
125115 125728   int test_fts3_node_chunk_threshold = (4*1024)*4;
125116 125729   # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
125117 125730   # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
125118 125731   #else
125119 125732   # define FTS3_NODE_CHUNKSIZE (4*1024) 
125120 125733   # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
125121 125734   #endif
       125735  +
       125736  +/*
       125737  +** The two values that may be meaningfully bound to the :1 parameter in
       125738  +** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
       125739  +*/
       125740  +#define FTS_STAT_DOCTOTAL      0
       125741  +#define FTS_STAT_INCRMERGEHINT 1
       125742  +#define FTS_STAT_AUTOINCRMERGE 2
       125743  +
       125744  +/*
       125745  +** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
       125746  +** and incremental merge operation that takes place. This is used for 
       125747  +** debugging FTS only, it should not usually be turned on in production
       125748  +** systems.
       125749  +*/
       125750  +#ifdef FTS3_LOG_MERGES
       125751  +static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
       125752  +  sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
       125753  +}
       125754  +#else
       125755  +#define fts3LogMerge(x, y)
       125756  +#endif
       125757  +
125122 125758   
125123 125759   typedef struct PendingList PendingList;
125124 125760   typedef struct SegmentNode SegmentNode;
125125 125761   typedef struct SegmentWriter SegmentWriter;
125126 125762   
125127 125763   /*
125128 125764   ** An instance of the following data structure is used to build doclists
................................................................................
125277 125913   #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
125278 125914   #define SQL_DELETE_SEGDIR_LEVEL       16
125279 125915   #define SQL_DELETE_SEGMENTS_RANGE     17
125280 125916   #define SQL_CONTENT_INSERT            18
125281 125917   #define SQL_DELETE_DOCSIZE            19
125282 125918   #define SQL_REPLACE_DOCSIZE           20
125283 125919   #define SQL_SELECT_DOCSIZE            21
125284         -#define SQL_SELECT_DOCTOTAL           22
125285         -#define SQL_REPLACE_DOCTOTAL          23
       125920  +#define SQL_SELECT_STAT               22
       125921  +#define SQL_REPLACE_STAT              23
125286 125922   
125287 125923   #define SQL_SELECT_ALL_PREFIX_LEVEL   24
125288 125924   #define SQL_DELETE_ALL_TERMS_SEGDIR   25
125289         -
125290 125925   #define SQL_DELETE_SEGDIR_RANGE       26
125291         -
125292 125926   #define SQL_SELECT_ALL_LANGID         27
       125927  +#define SQL_FIND_MERGE_LEVEL          28
       125928  +#define SQL_MAX_LEAF_NODE_ESTIMATE    29
       125929  +#define SQL_DELETE_SEGDIR_ENTRY       30
       125930  +#define SQL_SHIFT_SEGDIR_ENTRY        31
       125931  +#define SQL_SELECT_SEGDIR             32
       125932  +#define SQL_CHOMP_SEGDIR              33
       125933  +#define SQL_SEGMENT_IS_APPENDABLE     34
       125934  +#define SQL_SELECT_INDEXES            35
       125935  +#define SQL_SELECT_MXLEVEL            36
125293 125936   
125294 125937   /*
125295 125938   ** This function is used to obtain an SQLite prepared statement handle
125296 125939   ** for the statement identified by the second argument. If successful,
125297 125940   ** *pp is set to the requested statement handle and SQLITE_OK returned.
125298 125941   ** Otherwise, an SQLite error code is returned and *pp is set to 0.
125299 125942   **
................................................................................
125314 125957   /* 2  */  "DELETE FROM %Q.'%q_content'",
125315 125958   /* 3  */  "DELETE FROM %Q.'%q_segments'",
125316 125959   /* 4  */  "DELETE FROM %Q.'%q_segdir'",
125317 125960   /* 5  */  "DELETE FROM %Q.'%q_docsize'",
125318 125961   /* 6  */  "DELETE FROM %Q.'%q_stat'",
125319 125962   /* 7  */  "SELECT %s WHERE rowid=?",
125320 125963   /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
125321         -/* 9  */  "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
       125964  +/* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
125322 125965   /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
125323         -/* 11 */  "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
       125966  +/* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
125324 125967   
125325 125968             /* Return segments in order from oldest to newest.*/ 
125326 125969   /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
125327 125970               "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
125328 125971   /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
125329 125972               "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
125330 125973               "ORDER BY level DESC, idx ASC",
................................................................................
125334 125977   
125335 125978   /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
125336 125979   /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
125337 125980   /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
125338 125981   /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
125339 125982   /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
125340 125983   /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
125341         -/* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=0",
125342         -/* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
       125984  +/* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
       125985  +/* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
125343 125986   /* 24 */  "",
125344 125987   /* 25 */  "",
125345 125988   
125346 125989   /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
125347 125990   /* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'",
125348 125991   
       125992  +/* This statement is used to determine which level to read the input from
       125993  +** when performing an incremental merge. It returns the absolute level number
       125994  +** of the oldest level in the db that contains at least ? segments. Or,
       125995  +** if no level in the FTS index contains more than ? segments, the statement
       125996  +** returns zero rows.  */
       125997  +/* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
       125998  +         "  ORDER BY (level %% 1024) ASC LIMIT 1",
       125999  +
       126000  +/* Estimate the upper limit on the number of leaf nodes in a new segment
       126001  +** created by merging the oldest :2 segments from absolute level :1. See 
       126002  +** function sqlite3Fts3Incrmerge() for details.  */
       126003  +/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
       126004  +         "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
       126005  +
       126006  +/* SQL_DELETE_SEGDIR_ENTRY
       126007  +**   Delete the %_segdir entry on absolute level :1 with index :2.  */
       126008  +/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
       126009  +
       126010  +/* SQL_SHIFT_SEGDIR_ENTRY
       126011  +**   Modify the idx value for the segment with idx=:3 on absolute level :2
       126012  +**   to :1.  */
       126013  +/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
       126014  +
       126015  +/* SQL_SELECT_SEGDIR
       126016  +**   Read a single entry from the %_segdir table. The entry from absolute 
       126017  +**   level :1 with index value :2.  */
       126018  +/* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
       126019  +            "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
       126020  +
       126021  +/* SQL_CHOMP_SEGDIR
       126022  +**   Update the start_block (:1) and root (:2) fields of the %_segdir
       126023  +**   entry located on absolute level :3 with index :4.  */
       126024  +/* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
       126025  +            "WHERE level = ? AND idx = ?",
       126026  +
       126027  +/* SQL_SEGMENT_IS_APPENDABLE
       126028  +**   Return a single row if the segment with end_block=? is appendable. Or
       126029  +**   no rows otherwise.  */
       126030  +/* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
       126031  +
       126032  +/* SQL_SELECT_INDEXES
       126033  +**   Return the list of valid segment indexes for absolute level ?  */
       126034  +/* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
       126035  +
       126036  +/* SQL_SELECT_MXLEVEL
       126037  +**   Return the largest relative level in the FTS index or indexes.  */
       126038  +/* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
125349 126039     };
125350 126040     int rc = SQLITE_OK;
125351 126041     sqlite3_stmt *pStmt;
125352 126042   
125353 126043     assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
125354 126044     assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
125355 126045     
................................................................................
125378 126068       for(i=0; rc==SQLITE_OK && i<nParam; i++){
125379 126069         rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
125380 126070       }
125381 126071     }
125382 126072     *pp = pStmt;
125383 126073     return rc;
125384 126074   }
       126075  +
125385 126076   
125386 126077   static int fts3SelectDocsize(
125387 126078     Fts3Table *pTab,                /* FTS3 table handle */
125388         -  int eStmt,                      /* Either SQL_SELECT_DOCSIZE or DOCTOTAL */
125389 126079     sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
125390 126080     sqlite3_stmt **ppStmt           /* OUT: Statement handle */
125391 126081   ){
125392 126082     sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
125393 126083     int rc;                         /* Return code */
125394 126084   
125395         -  assert( eStmt==SQL_SELECT_DOCSIZE || eStmt==SQL_SELECT_DOCTOTAL );
125396         -
125397         -  rc = fts3SqlStmt(pTab, eStmt, &pStmt, 0);
       126085  +  rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
125398 126086     if( rc==SQLITE_OK ){
125399         -    if( eStmt==SQL_SELECT_DOCSIZE ){
125400         -      sqlite3_bind_int64(pStmt, 1, iDocid);
125401         -    }
       126087  +    sqlite3_bind_int64(pStmt, 1, iDocid);
125402 126088       rc = sqlite3_step(pStmt);
125403 126089       if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
125404 126090         rc = sqlite3_reset(pStmt);
125405 126091         if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
125406 126092         pStmt = 0;
125407 126093       }else{
125408 126094         rc = SQLITE_OK;
................................................................................
125413 126099     return rc;
125414 126100   }
125415 126101   
125416 126102   SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
125417 126103     Fts3Table *pTab,                /* Fts3 table handle */
125418 126104     sqlite3_stmt **ppStmt           /* OUT: Statement handle */
125419 126105   ){
125420         -  return fts3SelectDocsize(pTab, SQL_SELECT_DOCTOTAL, 0, ppStmt);
       126106  +  sqlite3_stmt *pStmt = 0;
       126107  +  int rc;
       126108  +  rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
       126109  +  if( rc==SQLITE_OK ){
       126110  +    sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
       126111  +    if( sqlite3_step(pStmt)!=SQLITE_ROW
       126112  +     || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
       126113  +    ){
       126114  +      rc = sqlite3_reset(pStmt);
       126115  +      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
       126116  +      pStmt = 0;
       126117  +    }
       126118  +  }
       126119  +  *ppStmt = pStmt;
       126120  +  return rc;
125421 126121   }
125422 126122   
125423 126123   SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
125424 126124     Fts3Table *pTab,                /* Fts3 table handle */
125425 126125     sqlite3_int64 iDocid,           /* Docid to read size data for */
125426 126126     sqlite3_stmt **ppStmt           /* OUT: Statement handle */
125427 126127   ){
125428         -  return fts3SelectDocsize(pTab, SQL_SELECT_DOCSIZE, iDocid, ppStmt);
       126128  +  return fts3SelectDocsize(pTab, iDocid, ppStmt);
125429 126129   }
125430 126130   
125431 126131   /*
125432 126132   ** Similar to fts3SqlStmt(). Except, after binding the parameters in
125433 126133   ** array apVal[] to the SQL statement identified by eStmt, the statement
125434 126134   ** is executed.
125435 126135   **
................................................................................
125511 126211   ** Language 1 indexes are allocated immediately following language 0.
125512 126212   **
125513 126213   ** So, for a system with nPrefix prefix indexes configured, the block of
125514 126214   ** absolute levels that corresponds to language-id iLangid and index 
125515 126215   ** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
125516 126216   */
125517 126217   static sqlite3_int64 getAbsoluteLevel(
125518         -  Fts3Table *p, 
125519         -  int iLangid, 
125520         -  int iIndex, 
125521         -  int iLevel
       126218  +  Fts3Table *p,                   /* FTS3 table handle */
       126219  +  int iLangid,                    /* Language id */
       126220  +  int iIndex,                     /* Index in p->aIndex[] */
       126221  +  int iLevel                      /* Level of segments */
125522 126222   ){
125523 126223     sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
125524 126224     assert( iLangid>=0 );
125525 126225     assert( p->nIndex>0 );
125526 126226     assert( iIndex>=0 && iIndex<p->nIndex );
125527 126227   
125528 126228     iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
125529 126229     return iBase + iLevel;
125530 126230   }
125531         -
125532 126231   
125533 126232   /*
125534 126233   ** Set *ppStmt to a statement handle that may be used to iterate through
125535 126234   ** all rows in the %_segdir table, from oldest to newest. If successful,
125536 126235   ** return SQLITE_OK. If an error occurs while preparing the statement, 
125537 126236   ** return an SQLite error code.
125538 126237   **
................................................................................
126091 126790     if( rc==SQLITE_OK ){
126092 126791       /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
126093 126792       ** full, merge all segments in level iLevel into a single iLevel+1
126094 126793       ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
126095 126794       ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
126096 126795       */
126097 126796       if( iNext>=FTS3_MERGE_COUNT ){
       126797  +      fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
126098 126798         rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
126099 126799         *piIdx = 0;
126100 126800       }else{
126101 126801         *piIdx = iNext;
126102 126802       }
126103 126803     }
126104 126804   
................................................................................
126138 126838     char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
126139 126839     int *pnBlob,                    /* OUT: Size of blob data */
126140 126840     int *pnLoad                     /* OUT: Bytes actually loaded */
126141 126841   ){
126142 126842     int rc;                         /* Return code */
126143 126843   
126144 126844     /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
126145         -  assert( pnBlob);
       126845  +  assert( pnBlob );
126146 126846   
126147 126847     if( p->pSegments ){
126148 126848       rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
126149 126849     }else{
126150 126850       if( 0==p->zSegmentsTbl ){
126151 126851         p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
126152 126852         if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
................................................................................
126479 127179   ){
126480 127180     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
126481 127181     int nOvfl = 0;
126482 127182     int ii;
126483 127183     int rc = SQLITE_OK;
126484 127184     int pgsz = p->nPgsz;
126485 127185   
126486         -  assert( p->bHasStat );
       127186  +  assert( p->bFts4 );
126487 127187     assert( pgsz>0 );
126488 127188   
126489 127189     for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
126490 127190       Fts3SegReader *pReader = pMsr->apSegment[ii];
126491 127191       if( !fts3SegReaderIsPending(pReader) 
126492 127192        && !fts3SegReaderIsRootOnly(pReader) 
126493 127193       ){
................................................................................
126836 127536       sqlite3_bind_int64(pStmt, 1, iBlock);
126837 127537       sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
126838 127538       sqlite3_step(pStmt);
126839 127539       rc = sqlite3_reset(pStmt);
126840 127540     }
126841 127541     return rc;
126842 127542   }
       127543  +
       127544  +/*
       127545  +** Find the largest relative level number in the table. If successful, set
       127546  +** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
       127547  +** set *pnMax to zero and return an SQLite error code.
       127548  +*/
       127549  +SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
       127550  +  int rc;
       127551  +  int mxLevel = 0;
       127552  +  sqlite3_stmt *pStmt = 0;
       127553  +
       127554  +  rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
       127555  +  if( rc==SQLITE_OK ){
       127556  +    if( SQLITE_ROW==sqlite3_step(pStmt) ){
       127557  +      mxLevel = sqlite3_column_int(pStmt, 0);
       127558  +    }
       127559  +    rc = sqlite3_reset(pStmt);
       127560  +  }
       127561  +  *pnMax = mxLevel;
       127562  +  return rc;
       127563  +}
126843 127564   
126844 127565   /* 
126845 127566   ** Insert a record into the %_segdir table.
126846 127567   */
126847 127568   static int fts3WriteSegdir(
126848 127569     Fts3Table *p,                   /* Virtual table handle */
126849 127570     sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
................................................................................
127153 127874   
127154 127875     if( nData>0 && nData+nReq>p->nNodeSize ){
127155 127876       int rc;
127156 127877   
127157 127878       /* The current leaf node is full. Write it out to the database. */
127158 127879       rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
127159 127880       if( rc!=SQLITE_OK ) return rc;
       127881  +    p->nLeafAdd++;
127160 127882   
127161 127883       /* Add the current term to the interior node tree. The term added to
127162 127884       ** the interior tree must:
127163 127885       **
127164 127886       **   a) be greater than the largest term on the leaf node just written
127165 127887       **      to the database (still available in pWriter->zTerm), and
127166 127888       **
................................................................................
127261 127983             p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
127262 127984       }
127263 127985     }else{
127264 127986       /* The entire tree fits on the root node. Write it to the segdir table. */
127265 127987       rc = fts3WriteSegdir(
127266 127988           p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
127267 127989     }
       127990  +  p->nLeafAdd++;
127268 127991     return rc;
127269 127992   }
127270 127993   
127271 127994   /*
127272 127995   ** Release all memory held by the SegmentWriter object passed as the 
127273 127996   ** first argument.
127274 127997   */
................................................................................
127341 128064         getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
127342 128065     );
127343 128066     if( SQLITE_ROW==sqlite3_step(pStmt) ){
127344 128067       *pnMax = sqlite3_column_int64(pStmt, 0);
127345 128068     }
127346 128069     return sqlite3_reset(pStmt);
127347 128070   }
       128071  +
       128072  +/*
       128073  +** Delete all entries in the %_segments table associated with the segment
       128074  +** opened with seg-reader pSeg. This function does not affect the contents
       128075  +** of the %_segdir table.
       128076  +*/
       128077  +static int fts3DeleteSegment(
       128078  +  Fts3Table *p,                   /* FTS table handle */
       128079  +  Fts3SegReader *pSeg             /* Segment to delete */
       128080  +){
       128081  +  int rc = SQLITE_OK;             /* Return code */
       128082  +  if( pSeg->iStartBlock ){
       128083  +    sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
       128084  +    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
       128085  +    if( rc==SQLITE_OK ){
       128086  +      sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
       128087  +      sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
       128088  +      sqlite3_step(pDelete);
       128089  +      rc = sqlite3_reset(pDelete);
       128090  +    }
       128091  +  }
       128092  +  return rc;
       128093  +}
127348 128094   
127349 128095   /*
127350 128096   ** This function is used after merging multiple segments into a single large
127351 128097   ** segment to delete the old, now redundant, segment b-trees. Specifically,
127352 128098   ** it:
127353 128099   ** 
127354 128100   **   1) Deletes all %_segments entries for the segments associated with 
................................................................................
127364 128110     Fts3Table *p,                   /* Virtual table handle */
127365 128111     int iLangid,                    /* Language id */
127366 128112     int iIndex,                     /* Index for p->aIndex */
127367 128113     int iLevel,                     /* Level of %_segdir entries to delete */
127368 128114     Fts3SegReader **apSegment,      /* Array of SegReader objects */
127369 128115     int nReader                     /* Size of array apSegment */
127370 128116   ){
127371         -  int rc;                         /* Return Code */
       128117  +  int rc = SQLITE_OK;             /* Return Code */
127372 128118     int i;                          /* Iterator variable */
127373         -  sqlite3_stmt *pDelete;          /* SQL statement to delete rows */
       128119  +  sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
127374 128120   
127375         -  rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
127376 128121     for(i=0; rc==SQLITE_OK && i<nReader; i++){
127377         -    Fts3SegReader *pSegment = apSegment[i];
127378         -    if( pSegment->iStartBlock ){
127379         -      sqlite3_bind_int64(pDelete, 1, pSegment->iStartBlock);
127380         -      sqlite3_bind_int64(pDelete, 2, pSegment->iEndBlock);
127381         -      sqlite3_step(pDelete);
127382         -      rc = sqlite3_reset(pDelete);
127383         -    }
       128122  +    rc = fts3DeleteSegment(p, apSegment[i]);
127384 128123     }
127385 128124     if( rc!=SQLITE_OK ){
127386 128125       return rc;
127387 128126     }
127388 128127   
127389 128128     assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
127390 128129     if( iLevel==FTS3_SEGCURSOR_ALL ){
................................................................................
127952 128691   
127953 128692   /* 
127954 128693   ** Flush the contents of pendingTerms to level 0 segments.
127955 128694   */
127956 128695   SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
127957 128696     int rc = SQLITE_OK;
127958 128697     int i;
       128698  +        
127959 128699     for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
127960 128700       rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
127961 128701       if( rc==SQLITE_DONE ) rc = SQLITE_OK;
127962 128702     }
127963 128703     sqlite3Fts3PendingTermsClear(p);
       128704  +
       128705  +  /* Determine the auto-incr-merge setting if unknown.  If enabled,
       128706  +  ** estimate the number of leaf blocks of content to be written
       128707  +  */
       128708  +  if( rc==SQLITE_OK && p->bHasStat
       128709  +   && p->bAutoincrmerge==0xff && p->nLeafAdd>0
       128710  +  ){
       128711  +    sqlite3_stmt *pStmt = 0;
       128712  +    rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
       128713  +    if( rc==SQLITE_OK ){
       128714  +      sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
       128715  +      rc = sqlite3_step(pStmt);
       128716  +      p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
       128717  +      rc = sqlite3_reset(pStmt);
       128718  +    }
       128719  +  }
127964 128720     return rc;
127965 128721   }
127966 128722   
127967 128723   /*
127968 128724   ** Encode N integers as varints into a blob.
127969 128725   */
127970 128726   static void fts3EncodeIntArray(
................................................................................
128067 128823     if( *pRC ) return;
128068 128824     a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
128069 128825     if( a==0 ){
128070 128826       *pRC = SQLITE_NOMEM;
128071 128827       return;
128072 128828     }
128073 128829     pBlob = (char*)&a[nStat];
128074         -  rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0);
       128830  +  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
128075 128831     if( rc ){
128076 128832       sqlite3_free(a);
128077 128833       *pRC = rc;
128078 128834       return;
128079 128835     }
       128836  +  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
128080 128837     if( sqlite3_step(pStmt)==SQLITE_ROW ){
128081 128838       fts3DecodeIntArray(nStat, a,
128082 128839            sqlite3_column_blob(pStmt, 0),
128083 128840            sqlite3_column_bytes(pStmt, 0));
128084 128841     }else{
128085 128842       memset(a, 0, sizeof(u32)*(nStat) );
128086 128843     }
................................................................................
128096 128853         x = 0;
128097 128854       }else{
128098 128855         x = x + aSzIns[i] - aSzDel[i];
128099 128856       }
128100 128857       a[i+1] = x;
128101 128858     }
128102 128859     fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
128103         -  rc = fts3SqlStmt(p, SQL_REPLACE_DOCTOTAL, &pStmt, 0);
       128860  +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
128104 128861     if( rc ){
128105 128862       sqlite3_free(a);
128106 128863       *pRC = rc;
128107 128864       return;
128108 128865     }
128109         -  sqlite3_bind_blob(pStmt, 1, pBlob, nBlob, SQLITE_STATIC);
       128866  +  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
       128867  +  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
128110 128868     sqlite3_step(pStmt);
128111 128869     *pRC = sqlite3_reset(pStmt);
128112 128870     sqlite3_free(a);
128113 128871   }
128114 128872   
128115 128873   /*
128116 128874   ** Merge the entire database so that there is one segment for each 
................................................................................
128207 128965         }else{
128208 128966           nEntry++;
128209 128967           for(iCol=0; iCol<=p->nColumn; iCol++){
128210 128968             aSzIns[iCol] += aSz[iCol];
128211 128969           }
128212 128970         }
128213 128971       }
128214         -    if( p->bHasStat ){
       128972  +    if( p->bFts4 ){
128215 128973         fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
128216 128974       }
128217 128975       sqlite3_free(aSz);
128218 128976   
128219 128977       if( pStmt ){
128220 128978         int rc2 = sqlite3_finalize(pStmt);
128221 128979         if( rc==SQLITE_OK ){
................................................................................
128223 128981         }
128224 128982       }
128225 128983     }
128226 128984   
128227 128985     return rc;
128228 128986   }
128229 128987   
       128988  +
       128989  +/*
       128990  +** This function opens a cursor used to read the input data for an 
       128991  +** incremental merge operation. Specifically, it opens a cursor to scan
       128992  +** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute 
       128993  +** level iAbsLevel.
       128994  +*/
       128995  +static int fts3IncrmergeCsr(
       128996  +  Fts3Table *p,                   /* FTS3 table handle */
       128997  +  sqlite3_int64 iAbsLevel,        /* Absolute level to open */
       128998  +  int nSeg,                       /* Number of segments to merge */
       128999  +  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
       129000  +){
       129001  +  int rc;                         /* Return Code */
       129002  +  sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */  
       129003  +  int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
       129004  +
       129005  +  /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
       129006  +  memset(pCsr, 0, sizeof(*pCsr));
       129007  +  nByte = sizeof(Fts3SegReader *) * nSeg;
       129008  +  pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
       129009  +
       129010  +  if( pCsr->apSegment==0 ){
       129011  +    rc = SQLITE_NOMEM;
       129012  +  }else{
       129013  +    memset(pCsr->apSegment, 0, nByte);
       129014  +    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
       129015  +  }
       129016  +  if( rc==SQLITE_OK ){
       129017  +    int i;
       129018  +    int rc2;
       129019  +    sqlite3_bind_int64(pStmt, 1, iAbsLevel);
       129020  +    assert( pCsr->nSegment==0 );
       129021  +    for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
       129022  +      rc = sqlite3Fts3SegReaderNew(i, 0,
       129023  +          sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
       129024  +          sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
       129025  +          sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
       129026  +          sqlite3_column_blob(pStmt, 4),         /* segdir.root */
       129027  +          sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
       129028  +          &pCsr->apSegment[i]
       129029  +      );
       129030  +      pCsr->nSegment++;
       129031  +    }
       129032  +    rc2 = sqlite3_reset(pStmt);
       129033  +    if( rc==SQLITE_OK ) rc = rc2;
       129034  +  }
       129035  +
       129036  +  return rc;
       129037  +}
       129038  +
       129039  +typedef struct IncrmergeWriter IncrmergeWriter;
       129040  +typedef struct NodeWriter NodeWriter;
       129041  +typedef struct Blob Blob;
       129042  +typedef struct NodeReader NodeReader;
       129043  +
       129044  +/*
       129045  +** An instance of the following structure is used as a dynamic buffer
       129046  +** to build up nodes or other blobs of data in.
       129047  +**
       129048  +** The function blobGrowBuffer() is used to extend the allocation.
       129049  +*/
       129050  +struct Blob {
       129051  +  char *a;                        /* Pointer to allocation */
       129052  +  int n;                          /* Number of valid bytes of data in a[] */
       129053  +  int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
       129054  +};
       129055  +
       129056  +/*
       129057  +** This structure is used to build up buffers containing segment b-tree 
       129058  +** nodes (blocks).
       129059  +*/
       129060  +struct NodeWriter {
       129061  +  sqlite3_int64 iBlock;           /* Current block id */
       129062  +  Blob key;                       /* Last key written to the current block */
       129063  +  Blob block;                     /* Current block image */
       129064  +};
       129065  +
       129066  +/*
       129067  +** An object of this type contains the state required to create or append
       129068  +** to an appendable b-tree segment.
       129069  +*/
       129070  +struct IncrmergeWriter {
       129071  +  int nLeafEst;                   /* Space allocated for leaf blocks */
       129072  +  int nWork;                      /* Number of leaf pages flushed */
       129073  +  sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
       129074  +  int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
       129075  +  sqlite3_int64 iStart;           /* Block number of first allocated block */
       129076  +  sqlite3_int64 iEnd;             /* Block number of last allocated block */
       129077  +  NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
       129078  +};
       129079  +
       129080  +/*
       129081  +** An object of the following type is used to read data from a single
       129082  +** FTS segment node. See the following functions:
       129083  +**
       129084  +**     nodeReaderInit()
       129085  +**     nodeReaderNext()
       129086  +**     nodeReaderRelease()
       129087  +*/
       129088  +struct NodeReader {
       129089  +  const char *aNode;
       129090  +  int nNode;
       129091  +  int iOff;                       /* Current offset within aNode[] */
       129092  +
       129093  +  /* Output variables. Containing the current node entry. */
       129094  +  sqlite3_int64 iChild;           /* Pointer to child node */
       129095  +  Blob term;                      /* Current term */
       129096  +  const char *aDoclist;           /* Pointer to doclist */
       129097  +  int nDoclist;                   /* Size of doclist in bytes */
       129098  +};
       129099  +
       129100  +/*
       129101  +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
       129102  +** Otherwise, if the allocation at pBlob->a is not already at least nMin
       129103  +** bytes in size, extend (realloc) it to be so.
       129104  +**
       129105  +** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
       129106  +** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
       129107  +** to reflect the new size of the pBlob->a[] buffer.
       129108  +*/
       129109  +static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
       129110  +  if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
       129111  +    int nAlloc = nMin;
       129112  +    char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
       129113  +    if( a ){
       129114  +      pBlob->nAlloc = nAlloc;
       129115  +      pBlob->a = a;
       129116  +    }else{
       129117  +      *pRc = SQLITE_NOMEM;
       129118  +    }
       129119  +  }
       129120  +}
       129121  +
       129122  +/*
       129123  +** Attempt to advance the node-reader object passed as the first argument to
       129124  +** the next entry on the node. 
       129125  +**
       129126  +** Return an error code if an error occurs (SQLITE_NOMEM is possible). 
       129127  +** Otherwise return SQLITE_OK. If there is no next entry on the node
       129128  +** (e.g. because the current entry is the last) set NodeReader->aNode to
       129129  +** NULL to indicate EOF. Otherwise, populate the NodeReader structure output 
       129130  +** variables for the new entry.
       129131  +*/
       129132  +static int nodeReaderNext(NodeReader *p){
       129133  +  int bFirst = (p->term.n==0);    /* True for first term on the node */
       129134  +  int nPrefix = 0;                /* Bytes to copy from previous term */
       129135  +  int nSuffix = 0;                /* Bytes to append to the prefix */
       129136  +  int rc = SQLITE_OK;             /* Return code */
       129137  +
       129138  +  assert( p->aNode );
       129139  +  if( p->iChild && bFirst==0 ) p->iChild++;
       129140  +  if( p->iOff>=p->nNode ){
       129141  +    /* EOF */
       129142  +    p->aNode = 0;
       129143  +  }else{
       129144  +    if( bFirst==0 ){
       129145  +      p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
       129146  +    }
       129147  +    p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
       129148  +
       129149  +    blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
       129150  +    if( rc==SQLITE_OK ){
       129151  +      memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
       129152  +      p->term.n = nPrefix+nSuffix;
       129153  +      p->iOff += nSuffix;
       129154  +      if( p->iChild==0 ){
       129155  +        p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
       129156  +        p->aDoclist = &p->aNode[p->iOff];
       129157  +        p->iOff += p->nDoclist;
       129158  +      }
       129159  +    }
       129160  +  }
       129161  +
       129162  +  assert( p->iOff<=p->nNode );
       129163  +
       129164  +  return rc;
       129165  +}
       129166  +
       129167  +/*
       129168  +** Release all dynamic resources held by node-reader object *p.
       129169  +*/
       129170  +static void nodeReaderRelease(NodeReader *p){
       129171  +  sqlite3_free(p->term.a);
       129172  +}
       129173  +
       129174  +/*
       129175  +** Initialize a node-reader object to read the node in buffer aNode/nNode.
       129176  +**
       129177  +** If successful, SQLITE_OK is returned and the NodeReader object set to 
       129178  +** point to the first entry on the node (if any). Otherwise, an SQLite
       129179  +** error code is returned.
       129180  +*/
       129181  +static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
       129182  +  memset(p, 0, sizeof(NodeReader));
       129183  +  p->aNode = aNode;
       129184  +  p->nNode = nNode;
       129185  +
       129186  +  /* Figure out if this is a leaf or an internal node. */
       129187  +  if( p->aNode[0] ){
       129188  +    /* An internal node. */
       129189  +    p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
       129190  +  }else{
       129191  +    p->iOff = 1;
       129192  +  }
       129193  +
       129194  +  return nodeReaderNext(p);
       129195  +}
       129196  +
       129197  +/*
       129198  +** This function is called while writing an FTS segment each time a leaf o
       129199  +** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
       129200  +** to be greater than the largest key on the node just written, but smaller
       129201  +** than or equal to the first key that will be written to the next leaf
       129202  +** node.
       129203  +**
       129204  +** The block id of the leaf node just written to disk may be found in
       129205  +** (pWriter->aNodeWriter[0].iBlock) when this function is called.
       129206  +*/
       129207  +static int fts3IncrmergePush(
       129208  +  Fts3Table *p,                   /* Fts3 table handle */
       129209  +  IncrmergeWriter *pWriter,       /* Writer object */
       129210  +  const char *zTerm,              /* Term to write to internal node */
       129211  +  int nTerm                       /* Bytes at zTerm */
       129212  +){
       129213  +  sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
       129214  +  int iLayer;
       129215  +
       129216  +  assert( nTerm>0 );
       129217  +  for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
       129218  +    sqlite3_int64 iNextPtr = 0;
       129219  +    NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
       129220  +    int rc = SQLITE_OK;
       129221  +    int nPrefix;
       129222  +    int nSuffix;
       129223  +    int nSpace;
       129224  +
       129225  +    /* Figure out how much space the key will consume if it is written to
       129226  +    ** the current node of layer iLayer. Due to the prefix compression, 
       129227  +    ** the space required changes depending on which node the key is to
       129228  +    ** be added to.  */
       129229  +    nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
       129230  +    nSuffix = nTerm - nPrefix;
       129231  +    nSpace  = sqlite3Fts3VarintLen(nPrefix);
       129232  +    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
       129233  +
       129234  +    if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){ 
       129235  +      /* If the current node of layer iLayer contains zero keys, or if adding
       129236  +      ** the key to it will not cause it to grow to larger than nNodeSize 
       129237  +      ** bytes in size, write the key here.  */
       129238  +
       129239  +      Blob *pBlk = &pNode->block;
       129240  +      if( pBlk->n==0 ){
       129241  +        blobGrowBuffer(pBlk, p->nNodeSize, &rc);
       129242  +        if( rc==SQLITE_OK ){
       129243  +          pBlk->a[0] = (char)iLayer;
       129244  +          pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
       129245  +        }
       129246  +      }
       129247  +      blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
       129248  +      blobGrowBuffer(&pNode->key, nTerm, &rc);
       129249  +
       129250  +      if( rc==SQLITE_OK ){
       129251  +        if( pNode->key.n ){
       129252  +          pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
       129253  +        }
       129254  +        pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
       129255  +        memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
       129256  +        pBlk->n += nSuffix;
       129257  +
       129258  +        memcpy(pNode->key.a, zTerm, nTerm);
       129259  +        pNode->key.n = nTerm;
       129260  +      }
       129261  +    }else{
       129262  +      /* Otherwise, flush the the current node of layer iLayer to disk.
       129263  +      ** Then allocate a new, empty sibling node. The key will be written
       129264  +      ** into the parent of this node. */
       129265  +      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
       129266  +
       129267  +      assert( pNode->block.nAlloc>=p->nNodeSize );
       129268  +      pNode->block.a[0] = (char)iLayer;
       129269  +      pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
       129270  +
       129271  +      iNextPtr = pNode->iBlock;
       129272  +      pNode->iBlock++;
       129273  +      pNode->key.n = 0;
       129274  +    }
       129275  +
       129276  +    if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
       129277  +    iPtr = iNextPtr;
       129278  +  }
       129279  +
       129280  +  assert( 0 );
       129281  +  return 0;
       129282  +}
       129283  +
       129284  +/*
       129285  +** Append a term and (optionally) doclist to the FTS segment node currently
       129286  +** stored in blob *pNode. The node need not contain any terms, but the
       129287  +** header must be written before this function is called.
       129288  +**
       129289  +** A node header is a single 0x00 byte for a leaf node, or a height varint
       129290  +** followed by the left-hand-child varint for an internal node.
       129291  +**
       129292  +** The term to be appended is passed via arguments zTerm/nTerm. For a 
       129293  +** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
       129294  +** node, both aDoclist and nDoclist must be passed 0.
       129295  +**
       129296  +** If the size of the value in blob pPrev is zero, then this is the first
       129297  +** term written to the node. Otherwise, pPrev contains a copy of the 
       129298  +** previous term. Before this function returns, it is updated to contain a
       129299  +** copy of zTerm/nTerm.
       129300  +**
       129301  +** It is assumed that the buffer associated with pNode is already large
       129302  +** enough to accommodate the new entry. The buffer associated with pPrev
       129303  +** is extended by this function if requrired.
       129304  +**
       129305  +** If an error (i.e. OOM condition) occurs, an SQLite error code is
       129306  +** returned. Otherwise, SQLITE_OK.
       129307  +*/
       129308  +static int fts3AppendToNode(
       129309  +  Blob *pNode,                    /* Current node image to append to */
       129310  +  Blob *pPrev,                    /* Buffer containing previous term written */
       129311  +  const char *zTerm,              /* New term to write */
       129312  +  int nTerm,                      /* Size of zTerm in bytes */
       129313  +  const char *aDoclist,           /* Doclist (or NULL) to write */
       129314  +  int nDoclist                    /* Size of aDoclist in bytes */ 
       129315  +){
       129316  +  int rc = SQLITE_OK;             /* Return code */
       129317  +  int bFirst = (pPrev->n==0);     /* True if this is the first term written */
       129318  +  int nPrefix;                    /* Size of term prefix in bytes */
       129319  +  int nSuffix;                    /* Size of term suffix in bytes */
       129320  +
       129321  +  /* Node must have already been started. There must be a doclist for a
       129322  +  ** leaf node, and there must not be a doclist for an internal node.  */
       129323  +  assert( pNode->n>0 );
       129324  +  assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
       129325  +
       129326  +  blobGrowBuffer(pPrev, nTerm, &rc);
       129327  +  if( rc!=SQLITE_OK ) return rc;
       129328  +
       129329  +  nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
       129330  +  nSuffix = nTerm - nPrefix;
       129331  +  memcpy(pPrev->a, zTerm, nTerm);
       129332  +  pPrev->n = nTerm;
       129333  +
       129334  +  if( bFirst==0 ){
       129335  +    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
       129336  +  }
       129337  +  pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
       129338  +  memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
       129339  +  pNode->n += nSuffix;
       129340  +
       129341  +  if( aDoclist ){
       129342  +    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
       129343  +    memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
       129344  +    pNode->n += nDoclist;
       129345  +  }
       129346  +
       129347  +  assert( pNode->n<=pNode->nAlloc );
       129348  +
       129349  +  return SQLITE_OK;
       129350  +}
       129351  +
       129352  +/*
       129353  +** Append the current term and doclist pointed to by cursor pCsr to the
       129354  +** appendable b-tree segment opened for writing by pWriter.
       129355  +**
       129356  +** Return SQLITE_OK if successful, or an SQLite error code otherwise.
       129357  +*/
       129358  +static int fts3IncrmergeAppend(
       129359  +  Fts3Table *p,                   /* Fts3 table handle */
       129360  +  IncrmergeWriter *pWriter,       /* Writer object */
       129361  +  Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
       129362  +){
       129363  +  const char *zTerm = pCsr->zTerm;
       129364  +  int nTerm = pCsr->nTerm;
       129365  +  const char *aDoclist = pCsr->aDoclist;
       129366  +  int nDoclist = pCsr->nDoclist;
       129367  +  int rc = SQLITE_OK;           /* Return code */
       129368  +  int nSpace;                   /* Total space in bytes required on leaf */
       129369  +  int nPrefix;                  /* Size of prefix shared with previous term */
       129370  +  int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
       129371  +  NodeWriter *pLeaf;            /* Object used to write leaf nodes */
       129372  +
       129373  +  pLeaf = &pWriter->aNodeWriter[0];
       129374  +  nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
       129375  +  nSuffix = nTerm - nPrefix;
       129376  +
       129377  +  nSpace  = sqlite3Fts3VarintLen(nPrefix);
       129378  +  nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
       129379  +  nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
       129380  +
       129381  +  /* If the current block is not empty, and if adding this term/doclist
       129382  +  ** to the current block would make it larger than Fts3Table.nNodeSize
       129383  +  ** bytes, write this block out to the database. */
       129384  +  if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
       129385  +    rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
       129386  +    pWriter->nWork++;
       129387  +
       129388  +    /* Add the current term to the parent node. The term added to the 
       129389  +    ** parent must:
       129390  +    **
       129391  +    **   a) be greater than the largest term on the leaf node just written
       129392  +    **      to the database (still available in pLeaf->key), and
       129393  +    **
       129394  +    **   b) be less than or equal to the term about to be added to the new
       129395  +    **      leaf node (zTerm/nTerm).
       129396  +    **
       129397  +    ** In other words, it must be the prefix of zTerm 1 byte longer than
       129398  +    ** the common prefix (if any) of zTerm and pWriter->zTerm.
       129399  +    */
       129400  +    if( rc==SQLITE_OK ){
       129401  +      rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
       129402  +    }
       129403  +
       129404  +    /* Advance to the next output block */
       129405  +    pLeaf->iBlock++;
       129406  +    pLeaf->key.n = 0;
       129407  +    pLeaf->block.n = 0;
       129408  +
       129409  +    nSuffix = nTerm;
       129410  +    nSpace  = 1;
       129411  +    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
       129412  +    nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
       129413  +  }
       129414  +
       129415  +  blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
       129416  +
       129417  +  if( rc==SQLITE_OK ){
       129418  +    if( pLeaf->block.n==0 ){
       129419  +      pLeaf->block.n = 1;
       129420  +      pLeaf->block.a[0] = '\0';
       129421  +    }
       129422  +    rc = fts3AppendToNode(
       129423  +        &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
       129424  +    );
       129425  +  }
       129426  +
       129427  +  return rc;
       129428  +}
       129429  +
       129430  +/*
       129431  +** This function is called to release all dynamic resources held by the
       129432  +** merge-writer object pWriter, and if no error has occurred, to flush
       129433  +** all outstanding node buffers held by pWriter to disk.
       129434  +**
       129435  +** If *pRc is not SQLITE_OK when this function is called, then no attempt
       129436  +** is made to write any data to disk. Instead, this function serves only
       129437  +** to release outstanding resources.
       129438  +**
       129439  +** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
       129440  +** flushing buffers to disk, *pRc is set to an SQLite error code before
       129441  +** returning.
       129442  +*/
       129443  +static void fts3IncrmergeRelease(
       129444  +  Fts3Table *p,                   /* FTS3 table handle */
       129445  +  IncrmergeWriter *pWriter,       /* Merge-writer object */
       129446  +  int *pRc                        /* IN/OUT: Error code */
       129447  +){
       129448  +  int i;                          /* Used to iterate through non-root layers */
       129449  +  int iRoot;                      /* Index of root in pWriter->aNodeWriter */
       129450  +  NodeWriter *pRoot;              /* NodeWriter for root node */
       129451  +  int rc = *pRc;                  /* Error code */
       129452  +
       129453  +  /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment 
       129454  +  ** root node. If the segment fits entirely on a single leaf node, iRoot
       129455  +  ** will be set to 0. If the root node is the parent of the leaves, iRoot
       129456  +  ** will be 1. And so on.  */
       129457  +  for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
       129458  +    NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
       129459  +    if( pNode->block.n>0 ) break;
       129460  +    assert( *pRc || pNode->block.nAlloc==0 );
       129461  +    assert( *pRc || pNode->key.nAlloc==0 );
       129462  +    sqlite3_free(pNode->block.a);
       129463  +    sqlite3_free(pNode->key.a);
       129464  +  }
       129465  +
       129466  +  /* Empty output segment. This is a no-op. */
       129467  +  if( iRoot<0 ) return;
       129468  +
       129469  +  /* The entire output segment fits on a single node. Normally, this means
       129470  +  ** the node would be stored as a blob in the "root" column of the %_segdir
       129471  +  ** table. However, this is not permitted in this case. The problem is that 
       129472  +  ** space has already been reserved in the %_segments table, and so the 
       129473  +  ** start_block and end_block fields of the %_segdir table must be populated. 
       129474  +  ** And, by design or by accident, released versions of FTS cannot handle 
       129475  +  ** segments that fit entirely on the root node with start_block!=0.
       129476  +  **
       129477  +  ** Instead, create a synthetic root node that contains nothing but a 
       129478  +  ** pointer to the single content node. So that the segment consists of a
       129479  +  ** single leaf and a single interior (root) node.
       129480  +  **
       129481  +  ** Todo: Better might be to defer allocating space in the %_segments 
       129482  +  ** table until we are sure it is needed.
       129483  +  */
       129484  +  if( iRoot==0 ){
       129485  +    Blob *pBlock = &pWriter->aNodeWriter[1].block;
       129486  +    blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
       129487  +    if( rc==SQLITE_OK ){
       129488  +      pBlock->a[0] = 0x01;
       129489  +      pBlock->n = 1 + sqlite3Fts3PutVarint(
       129490  +          &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
       129491  +      );
       129492  +    }
       129493  +    iRoot = 1;
       129494  +  }
       129495  +  pRoot = &pWriter->aNodeWriter[iRoot];
       129496  +
       129497  +  /* Flush all currently outstanding nodes to disk. */
       129498  +  for(i=0; i<iRoot; i++){
       129499  +    NodeWriter *pNode = &pWriter->aNodeWriter[i];
       129500  +    if( pNode->block.n>0 && rc==SQLITE_OK ){
       129501  +      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
       129502  +    }
       129503  +    sqlite3_free(pNode->block.a);
       129504  +    sqlite3_free(pNode->key.a);
       129505  +  }
       129506  +
       129507  +  /* Write the %_segdir record. */
       129508  +  if( rc==SQLITE_OK ){
       129509  +    rc = fts3WriteSegdir(p, 
       129510  +        pWriter->iAbsLevel+1,               /* level */
       129511  +        pWriter->iIdx,                      /* idx */
       129512  +        pWriter->iStart,                    /* start_block */
       129513  +        pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
       129514  +        pWriter->iEnd,                      /* end_block */
       129515  +        pRoot->block.a, pRoot->block.n      /* root */
       129516  +    );
       129517  +  }
       129518  +  sqlite3_free(pRoot->block.a);
       129519  +  sqlite3_free(pRoot->key.a);
       129520  +
       129521  +  *pRc = rc;
       129522  +}
       129523  +
       129524  +/*
       129525  +** Compare the term in buffer zLhs (size in bytes nLhs) with that in
       129526  +** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
       129527  +** the other, it is considered to be smaller than the other.
       129528  +**
       129529  +** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
       129530  +** if it is greater.
       129531  +*/
       129532  +static int fts3TermCmp(
       129533  +  const char *zLhs, int nLhs,     /* LHS of comparison */
       129534  +  const char *zRhs, int nRhs      /* RHS of comparison */
       129535  +){
       129536  +  int nCmp = MIN(nLhs, nRhs);
       129537  +  int res;
       129538  +
       129539  +  res = memcmp(zLhs, zRhs, nCmp);
       129540  +  if( res==0 ) res = nLhs - nRhs;
       129541  +
       129542  +  return res;
       129543  +}
       129544  +
       129545  +
       129546  +/*
       129547  +** Query to see if the entry in the %_segments table with blockid iEnd is 
       129548  +** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
       129549  +** returning. Otherwise, set *pbRes to 0. 
       129550  +**
       129551  +** Or, if an error occurs while querying the database, return an SQLite 
       129552  +** error code. The final value of *pbRes is undefined in this case.
       129553  +**
       129554  +** This is used to test if a segment is an "appendable" segment. If it
       129555  +** is, then a NULL entry has been inserted into the %_segments table
       129556  +** with blockid %_segdir.end_block.
       129557  +*/
       129558  +static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
       129559  +  int bRes = 0;                   /* Result to set *pbRes to */
       129560  +  sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
       129561  +  int rc;                         /* Return code */
       129562  +
       129563  +  rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
       129564  +  if( rc==SQLITE_OK ){
       129565  +    sqlite3_bind_int64(pCheck, 1, iEnd);
       129566  +    if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
       129567  +    rc = sqlite3_reset(pCheck);
       129568  +  }
       129569  +  
       129570  +  *pbRes = bRes;
       129571  +  return rc;
       129572  +}
       129573  +
       129574  +/*
       129575  +** This function is called when initializing an incremental-merge operation.
       129576  +** It checks if the existing segment with index value iIdx at absolute level 
       129577  +** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
       129578  +** merge-writer object *pWriter is initialized to write to it.
       129579  +**
       129580  +** An existing segment can be appended to by an incremental merge if:
       129581  +**
       129582  +**   * It was initially created as an appendable segment (with all required
       129583  +**     space pre-allocated), and
       129584  +**
       129585  +**   * The first key read from the input (arguments zKey and nKey) is 
       129586  +**     greater than the largest key currently stored in the potential
       129587  +**     output segment.
       129588  +*/
       129589  +static int fts3IncrmergeLoad(
       129590  +  Fts3Table *p,                   /* Fts3 table handle */
       129591  +  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
       129592  +  int iIdx,                       /* Index of candidate output segment */
       129593  +  const char *zKey,               /* First key to write */
       129594  +  int nKey,                       /* Number of bytes in nKey */
       129595  +  IncrmergeWriter *pWriter        /* Populate this object */
       129596  +){
       129597  +  int rc;                         /* Return code */
       129598  +  sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
       129599  +
       129600  +  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
       129601  +  if( rc==SQLITE_OK ){
       129602  +    sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
       129603  +    sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
       129604  +    sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
       129605  +    const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
       129606  +    int nRoot = 0;                /* Size of aRoot[] in bytes */
       129607  +    int rc2;                      /* Return code from sqlite3_reset() */
       129608  +    int bAppendable = 0;          /* Set to true if segment is appendable */
       129609  +
       129610  +    /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
       129611  +    sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
       129612  +    sqlite3_bind_int(pSelect, 2, iIdx);
       129613  +    if( sqlite3_step(pSelect)==SQLITE_ROW ){
       129614  +      iStart = sqlite3_column_int64(pSelect, 1);
       129615  +      iLeafEnd = sqlite3_column_int64(pSelect, 2);
       129616  +      iEnd = sqlite3_column_int64(pSelect, 3);
       129617  +      nRoot = sqlite3_column_bytes(pSelect, 4);
       129618  +      aRoot = sqlite3_column_blob(pSelect, 4);
       129619  +    }else{
       129620  +      return sqlite3_reset(pSelect);
       129621  +    }
       129622  +
       129623  +    /* Check for the zero-length marker in the %_segments table */
       129624  +    rc = fts3IsAppendable(p, iEnd, &bAppendable);
       129625  +
       129626  +    /* Check that zKey/nKey is larger than the largest key the candidate */
       129627  +    if( rc==SQLITE_OK && bAppendable ){
       129628  +      char *aLeaf = 0;
       129629  +      int nLeaf = 0;
       129630  +
       129631  +      rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
       129632  +      if( rc==SQLITE_OK ){
       129633  +        NodeReader reader;
       129634  +        for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
       129635  +            rc==SQLITE_OK && reader.aNode;
       129636  +            rc = nodeReaderNext(&reader)
       129637  +        ){
       129638  +          assert( reader.aNode );
       129639  +        }
       129640  +        if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
       129641  +          bAppendable = 0;
       129642  +        }
       129643  +        nodeReaderRelease(&reader);
       129644  +      }
       129645  +      sqlite3_free(aLeaf);
       129646  +    }
       129647  +
       129648  +    if( rc==SQLITE_OK && bAppendable ){
       129649  +      /* It is possible to append to this segment. Set up the IncrmergeWriter
       129650  +      ** object to do so.  */
       129651  +      int i;
       129652  +      int nHeight = (int)aRoot[0];
       129653  +      NodeWriter *pNode;
       129654  +
       129655  +      pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
       129656  +      pWriter->iStart = iStart;
       129657  +      pWriter->iEnd = iEnd;
       129658  +      pWriter->iAbsLevel = iAbsLevel;
       129659  +      pWriter->iIdx = iIdx;
       129660  +
       129661  +      for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
       129662  +        pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
       129663  +      }
       129664  +
       129665  +      pNode = &pWriter->aNodeWriter[nHeight];
       129666  +      pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
       129667  +      blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
       129668  +      if( rc==SQLITE_OK ){
       129669  +        memcpy(pNode->block.a, aRoot, nRoot);
       129670  +        pNode->block.n = nRoot;
       129671  +      }
       129672  +
       129673  +      for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
       129674  +        NodeReader reader;
       129675  +        pNode = &pWriter->aNodeWriter[i];
       129676  +
       129677  +        rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
       129678  +        while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
       129679  +        blobGrowBuffer(&pNode->key, reader.term.n, &rc);
       129680  +        if( rc==SQLITE_OK ){
       129681  +          memcpy(pNode->key.a, reader.term.a, reader.term.n);
       129682  +          pNode->key.n = reader.term.n;
       129683  +          if( i>0 ){
       129684  +            char *aBlock = 0;
       129685  +            int nBlock = 0;
       129686  +            pNode = &pWriter->aNodeWriter[i-1];
       129687  +            pNode->iBlock = reader.iChild;
       129688  +            rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
       129689  +            blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
       129690  +            if( rc==SQLITE_OK ){
       129691  +              memcpy(pNode->block.a, aBlock, nBlock);
       129692  +              pNode->block.n = nBlock;
       129693  +            }
       129694  +            sqlite3_free(aBlock);
       129695  +          }
       129696  +        }
       129697  +        nodeReaderRelease(&reader);
       129698  +      }
       129699  +    }
       129700  +
       129701  +    rc2 = sqlite3_reset(pSelect);
       129702  +    if( rc==SQLITE_OK ) rc = rc2;
       129703  +  }
       129704  +
       129705  +  return rc;
       129706  +}
       129707  +
       129708  +/*
       129709  +** Determine the largest segment index value that exists within absolute
       129710  +** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
       129711  +** one before returning SQLITE_OK. Or, if there are no segments at all 
       129712  +** within level iAbsLevel, set *piIdx to zero.
       129713  +**
       129714  +** If an error occurs, return an SQLite error code. The final value of
       129715  +** *piIdx is undefined in this case.
       129716  +*/
       129717  +static int fts3IncrmergeOutputIdx( 
       129718  +  Fts3Table *p,                   /* FTS Table handle */
       129719  +  sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
       129720  +  int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
       129721  +){
       129722  +  int rc;
       129723  +  sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
       129724  +
       129725  +  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
       129726  +  if( rc==SQLITE_OK ){
       129727  +    sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
       129728  +    sqlite3_step(pOutputIdx);
       129729  +    *piIdx = sqlite3_column_int(pOutputIdx, 0);
       129730  +    rc = sqlite3_reset(pOutputIdx);
       129731  +  }
       129732  +
       129733  +  return rc;
       129734  +}
       129735  +
       129736  +/* 
       129737  +** Allocate an appendable output segment on absolute level iAbsLevel+1
       129738  +** with idx value iIdx.
       129739  +**
       129740  +** In the %_segdir table, a segment is defined by the values in three
       129741  +** columns:
       129742  +**
       129743  +**     start_block
       129744  +**     leaves_end_block
       129745  +**     end_block
       129746  +**
       129747  +** When an appendable segment is allocated, it is estimated that the
       129748  +** maximum number of leaf blocks that may be required is the sum of the
       129749  +** number of leaf blocks consumed by the input segments, plus the number
       129750  +** of input segments, multiplied by two. This value is stored in stack 
       129751  +** variable nLeafEst.
       129752  +**
       129753  +** A total of 16*nLeafEst blocks are allocated when an appendable segment
       129754  +** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
       129755  +** array of leaf nodes starts at the first block allocated. The array
       129756  +** of interior nodes that are parents of the leaf nodes start at block
       129757  +** (start_block + (1 + end_block - start_block) / 16). And so on.
       129758  +**
       129759  +** In the actual code below, the value "16" is replaced with the 
       129760  +** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
       129761  +*/
       129762  +static int fts3IncrmergeWriter( 
       129763  +  Fts3Table *p,                   /* Fts3 table handle */
       129764  +  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
       129765  +  int iIdx,                       /* Index of new output segment */
       129766  +  Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
       129767  +  IncrmergeWriter *pWriter        /* Populate this object */
       129768  +){
       129769  +  int rc;                         /* Return Code */
       129770  +  int i;                          /* Iterator variable */
       129771  +  int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
       129772  +  sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
       129773  +  sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
       129774  +
       129775  +  /* Calculate nLeafEst. */
       129776  +  rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
       129777  +  if( rc==SQLITE_OK ){
       129778  +    sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
       129779  +    sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
       129780  +    if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
       129781  +      nLeafEst = sqlite3_column_int(pLeafEst, 0);
       129782  +    }
       129783  +    rc = sqlite3_reset(pLeafEst);
       129784  +  }
       129785  +  if( rc!=SQLITE_OK ) return rc;
       129786  +
       129787  +  /* Calculate the first block to use in the output segment */
       129788  +  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
       129789  +  if( rc==SQLITE_OK ){
       129790  +    if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
       129791  +      pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
       129792  +      pWriter->iEnd = pWriter->iStart - 1;
       129793  +      pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
       129794  +    }
       129795  +    rc = sqlite3_reset(pFirstBlock);
       129796  +  }
       129797  +  if( rc!=SQLITE_OK ) return rc;
       129798  +
       129799  +  /* Insert the marker in the %_segments table to make sure nobody tries
       129800  +  ** to steal the space just allocated. This is also used to identify 
       129801  +  ** appendable segments.  */
       129802  +  rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
       129803  +  if( rc!=SQLITE_OK ) return rc;
       129804  +
       129805  +  pWriter->iAbsLevel = iAbsLevel;
       129806  +  pWriter->nLeafEst = nLeafEst;
       129807  +  pWriter->iIdx = iIdx;
       129808  +
       129809  +  /* Set up the array of NodeWriter objects */
       129810  +  for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
       129811  +    pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
       129812  +  }
       129813  +  return SQLITE_OK;
       129814  +}
       129815  +
       129816  +/*
       129817  +** Remove an entry from the %_segdir table. This involves running the 
       129818  +** following two statements:
       129819  +**
       129820  +**   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
       129821  +**   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
       129822  +**
       129823  +** The DELETE statement removes the specific %_segdir level. The UPDATE 
       129824  +** statement ensures that the remaining segments have contiguously allocated
       129825  +** idx values.
       129826  +*/
       129827  +static int fts3RemoveSegdirEntry(
       129828  +  Fts3Table *p,                   /* FTS3 table handle */
       129829  +  sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
       129830  +  int iIdx                        /* Index of %_segdir entry to delete */
       129831  +){
       129832  +  int rc;                         /* Return code */
       129833  +  sqlite3_stmt *pDelete = 0;      /* DELETE statement */
       129834  +
       129835  +  rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
       129836  +  if( rc==SQLITE_OK ){
       129837  +    sqlite3_bind_int64(pDelete, 1, iAbsLevel);
       129838  +    sqlite3_bind_int(pDelete, 2, iIdx);
       129839  +    sqlite3_step(pDelete);
       129840  +    rc = sqlite3_reset(pDelete);
       129841  +  }
       129842  +
       129843  +  return rc;
       129844  +}
       129845  +
       129846  +/*
       129847  +** One or more segments have just been removed from absolute level iAbsLevel.
       129848  +** Update the 'idx' values of the remaining segments in the level so that
       129849  +** the idx values are a contiguous sequence starting from 0.
       129850  +*/
       129851  +static int fts3RepackSegdirLevel(
       129852  +  Fts3Table *p,                   /* FTS3 table handle */
       129853  +  sqlite3_int64 iAbsLevel         /* Absolute level to repack */
       129854  +){
       129855  +  int rc;                         /* Return code */
       129856  +  int *aIdx = 0;                  /* Array of remaining idx values */
       129857  +  int nIdx = 0;                   /* Valid entries in aIdx[] */
       129858  +  int nAlloc = 0;                 /* Allocated size of aIdx[] */
       129859  +  int i;                          /* Iterator variable */
       129860  +  sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
       129861  +  sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
       129862  +
       129863  +  rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
       129864  +  if( rc==SQLITE_OK ){
       129865  +    int rc2;
       129866  +    sqlite3_bind_int64(pSelect, 1, iAbsLevel);
       129867  +    while( SQLITE_ROW==sqlite3_step(pSelect) ){
       129868  +      if( nIdx>=nAlloc ){
       129869  +        int *aNew;
       129870  +        nAlloc += 16;
       129871  +        aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
       129872  +        if( !aNew ){
       129873  +          rc = SQLITE_NOMEM;
       129874  +          break;
       129875  +        }
       129876  +        aIdx = aNew;
       129877  +      }
       129878  +      aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
       129879  +    }
       129880  +    rc2 = sqlite3_reset(pSelect);
       129881  +    if( rc==SQLITE_OK ) rc = rc2;
       129882  +  }
       129883  +
       129884  +  if( rc==SQLITE_OK ){
       129885  +    rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
       129886  +  }
       129887  +  if( rc==SQLITE_OK ){
       129888  +    sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
       129889  +  }
       129890  +
       129891  +  assert( p->bIgnoreSavepoint==0 );
       129892  +  p->bIgnoreSavepoint = 1;
       129893  +  for(i=0; rc==SQLITE_OK && i<nIdx; i++){
       129894  +    if( aIdx[i]!=i ){
       129895  +      sqlite3_bind_int(pUpdate, 3, aIdx[i]);
       129896  +      sqlite3_bind_int(pUpdate, 1, i);
       129897  +      sqlite3_step(pUpdate);
       129898  +      rc = sqlite3_reset(pUpdate);
       129899  +    }
       129900  +  }
       129901  +  p->bIgnoreSavepoint = 0;
       129902  +
       129903  +  sqlite3_free(aIdx);
       129904  +  return rc;
       129905  +}
       129906  +
       129907  +static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
       129908  +  pNode->a[0] = (char)iHeight;
       129909  +  if( iChild ){
       129910  +    assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
       129911  +    pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
       129912  +  }else{
       129913  +    assert( pNode->nAlloc>=1 );
       129914  +    pNode->n = 1;
       129915  +  }
       129916  +}
       129917  +
       129918  +/*
       129919  +** The first two arguments are a pointer to and the size of a segment b-tree
       129920  +** node. The node may be a leaf or an internal node.
       129921  +**
       129922  +** This function creates a new node image in blob object *pNew by copying
       129923  +** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
       129924  +** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
       129925  +*/
       129926  +static int fts3TruncateNode(
       129927  +  const char *aNode,              /* Current node image */
       129928  +  int nNode,                      /* Size of aNode in bytes */
       129929  +  Blob *pNew,                     /* OUT: Write new node image here */
       129930  +  const char *zTerm,              /* Omit all terms smaller than this */
       129931  +  int nTerm,                      /* Size of zTerm in bytes */
       129932  +  sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
       129933  +){
       129934  +  NodeReader reader;              /* Reader object */
       129935  +  Blob prev = {0, 0, 0};          /* Previous term written to new node */
       129936  +  int rc = SQLITE_OK;             /* Return code */
       129937  +  int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
       129938  +
       129939  +  /* Allocate required output space */
       129940  +  blobGrowBuffer(pNew, nNode, &rc);
       129941  +  if( rc!=SQLITE_OK ) return rc;
       129942  +  pNew->n = 0;
       129943  +
       129944  +  /* Populate new node buffer */
       129945  +  for(rc = nodeReaderInit(&reader, aNode, nNode); 
       129946  +      rc==SQLITE_OK && reader.aNode; 
       129947  +      rc = nodeReaderNext(&reader)
       129948  +  ){
       129949  +    if( pNew->n==0 ){
       129950  +      int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
       129951  +      if( res<0 || (bLeaf==0 && res==0) ) continue;
       129952  +      fts3StartNode(pNew, (int)aNode[0], reader.iChild);
       129953  +      *piBlock = reader.iChild;
       129954  +    }
       129955  +    rc = fts3AppendToNode(
       129956  +        pNew, &prev, reader.term.a, reader.term.n,
       129957  +        reader.aDoclist, reader.nDoclist
       129958  +    );
       129959  +    if( rc!=SQLITE_OK ) break;
       129960  +  }
       129961  +  if( pNew->n==0 ){
       129962  +    fts3StartNode(pNew, (int)aNode[0], reader.iChild);
       129963  +    *piBlock = reader.iChild;
       129964  +  }
       129965  +  assert( pNew->n<=pNew->nAlloc );
       129966  +
       129967  +  nodeReaderRelease(&reader);
       129968  +  sqlite3_free(prev.a);
       129969  +  return rc;
       129970  +}
       129971  +
       129972  +/*
       129973  +** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute 
       129974  +** level iAbsLevel. This may involve deleting entries from the %_segments
       129975  +** table, and modifying existing entries in both the %_segments and %_segdir
       129976  +** tables.
       129977  +**
       129978  +** SQLITE_OK is returned if the segment is updated successfully. Or an
       129979  +** SQLite error code otherwise.
       129980  +*/
       129981  +static int fts3TruncateSegment(
       129982  +  Fts3Table *p,                   /* FTS3 table handle */
       129983  +  sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
       129984  +  int iIdx,                       /* Index within level of segment to modify */
       129985  +  const char *zTerm,              /* Remove terms smaller than this */
       129986  +  int nTerm                      /* Number of bytes in buffer zTerm */
       129987  +){
       129988  +  int rc = SQLITE_OK;             /* Return code */
       129989  +  Blob root = {0,0,0};            /* New root page image */
       129990  +  Blob block = {0,0,0};           /* Buffer used for any other block */
       129991  +  sqlite3_int64 iBlock = 0;       /* Block id */
       129992  +  sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
       129993  +  sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
       129994  +  sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
       129995  +
       129996  +  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
       129997  +  if( rc==SQLITE_OK ){
       129998  +    int rc2;                      /* sqlite3_reset() return code */
       129999  +    sqlite3_bind_int64(pFetch, 1, iAbsLevel);
       130000  +    sqlite3_bind_int(pFetch, 2, iIdx);
       130001  +    if( SQLITE_ROW==sqlite3_step(pFetch) ){
       130002  +      const char *aRoot = sqlite3_column_blob(pFetch, 4);
       130003  +      int nRoot = sqlite3_column_bytes(pFetch, 4);
       130004  +      iOldStart = sqlite3_column_int64(pFetch, 1);
       130005  +      rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
       130006  +    }
       130007  +    rc2 = sqlite3_reset(pFetch);
       130008  +    if( rc==SQLITE_OK ) rc = rc2;
       130009  +  }
       130010  +
       130011  +  while( rc==SQLITE_OK && iBlock ){
       130012  +    char *aBlock = 0;
       130013  +    int nBlock = 0;
       130014  +    iNewStart = iBlock;
       130015  +
       130016  +    rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
       130017  +    if( rc==SQLITE_OK ){
       130018  +      rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
       130019  +    }
       130020  +    if( rc==SQLITE_OK ){
       130021  +      rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
       130022  +    }
       130023  +    sqlite3_free(aBlock);
       130024  +  }
       130025  +
       130026  +  /* Variable iNewStart now contains the first valid leaf node. */
       130027  +  if( rc==SQLITE_OK && iNewStart ){
       130028  +    sqlite3_stmt *pDel = 0;
       130029  +    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
       130030  +    if( rc==SQLITE_OK ){
       130031  +      sqlite3_bind_int64(pDel, 1, iOldStart);
       130032  +      sqlite3_bind_int64(pDel, 2, iNewStart-1);
       130033  +      sqlite3_step(pDel);
       130034  +      rc = sqlite3_reset(pDel);
       130035  +    }
       130036  +  }
       130037  +
       130038  +  if( rc==SQLITE_OK ){
       130039  +    sqlite3_stmt *pChomp = 0;
       130040  +    rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
       130041  +    if( rc==SQLITE_OK ){
       130042  +      sqlite3_bind_int64(pChomp, 1, iNewStart);
       130043  +      sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
       130044  +      sqlite3_bind_int64(pChomp, 3, iAbsLevel);
       130045  +      sqlite3_bind_int(pChomp, 4, iIdx);
       130046  +      sqlite3_step(pChomp);
       130047  +      rc = sqlite3_reset(pChomp);
       130048  +    }
       130049  +  }
       130050  +
       130051  +  sqlite3_free(root.a);
       130052  +  sqlite3_free(block.a);
       130053  +  return rc;
       130054  +}
       130055  +
       130056  +/*
       130057  +** This function is called after an incrmental-merge operation has run to
       130058  +** merge (or partially merge) two or more segments from absolute level
       130059  +** iAbsLevel.
       130060  +**
       130061  +** Each input segment is either removed from the db completely (if all of
       130062  +** its data was copied to the output segment by the incrmerge operation)
       130063  +** or modified in place so that it no longer contains those entries that
       130064  +** have been duplicated in the output segment.
       130065  +*/
       130066  +static int fts3IncrmergeChomp(
       130067  +  Fts3Table *p,                   /* FTS table handle */
       130068  +  sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
       130069  +  Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
       130070  +  int *pnRem                      /* Number of segments not deleted */
       130071  +){
       130072  +  int i;
       130073  +  int nRem = 0;
       130074  +  int rc = SQLITE_OK;
       130075  +
       130076  +  for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
       130077  +    Fts3SegReader *pSeg = 0;
       130078  +    int j;
       130079  +
       130080  +    /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
       130081  +    ** somewhere in the pCsr->apSegment[] array.  */
       130082  +    for(j=0; ALWAYS(j<pCsr->nSegment); j++){
       130083  +      pSeg = pCsr->apSegment[j];
       130084  +      if( pSeg->iIdx==i ) break;
       130085  +    }
       130086  +    assert( j<pCsr->nSegment && pSeg->iIdx==i );
       130087  +
       130088  +    if( pSeg->aNode==0 ){
       130089  +      /* Seg-reader is at EOF. Remove the entire input segment. */
       130090  +      rc = fts3DeleteSegment(p, pSeg);
       130091  +      if( rc==SQLITE_OK ){
       130092  +        rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
       130093  +      }
       130094  +      *pnRem = 0;
       130095  +    }else{
       130096  +      /* The incremental merge did not copy all the data from this 
       130097  +      ** segment to the upper level. The segment is modified in place
       130098  +      ** so that it contains no keys smaller than zTerm/nTerm. */ 
       130099  +      const char *zTerm = pSeg->zTerm;
       130100  +      int nTerm = pSeg->nTerm;
       130101  +      rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
       130102  +      nRem++;
       130103  +    }
       130104  +  }
       130105  +
       130106  +  if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
       130107  +    rc = fts3RepackSegdirLevel(p, iAbsLevel);
       130108  +  }
       130109  +
       130110  +  *pnRem = nRem;
       130111  +  return rc;
       130112  +}
       130113  +
       130114  +/*
       130115  +** Store an incr-merge hint in the database.
       130116  +*/
       130117  +static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
       130118  +  sqlite3_stmt *pReplace = 0;
       130119  +  int rc;                         /* Return code */
       130120  +
       130121  +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
       130122  +  if( rc==SQLITE_OK ){
       130123  +    sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
       130124  +    sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
       130125  +    sqlite3_step(pReplace);
       130126  +    rc = sqlite3_reset(pReplace);
       130127  +  }
       130128  +
       130129  +  return rc;
       130130  +}
       130131  +
       130132  +/*
       130133  +** Load an incr-merge hint from the database. The incr-merge hint, if one 
       130134  +** exists, is stored in the rowid==1 row of the %_stat table.
       130135  +**
       130136  +** If successful, populate blob *pHint with the value read from the %_stat
       130137  +** table and return SQLITE_OK. Otherwise, if an error occurs, return an
       130138  +** SQLite error code.
       130139  +*/
       130140  +static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
       130141  +  sqlite3_stmt *pSelect = 0;
       130142  +  int rc;
       130143  +
       130144  +  pHint->n = 0;
       130145  +  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
       130146  +  if( rc==SQLITE_OK ){
       130147  +    int rc2;
       130148  +    sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
       130149  +    if( SQLITE_ROW==sqlite3_step(pSelect) ){
       130150  +      const char *aHint = sqlite3_column_blob(pSelect, 0);
       130151  +      int nHint = sqlite3_column_bytes(pSelect, 0);
       130152  +      if( aHint ){
       130153  +        blobGrowBuffer(pHint, nHint, &rc);
       130154  +        if( rc==SQLITE_OK ){
       130155  +          memcpy(pHint->a, aHint, nHint);
       130156  +          pHint->n = nHint;
       130157  +        }
       130158  +      }
       130159  +    }
       130160  +    rc2 = sqlite3_reset(pSelect);
       130161  +    if( rc==SQLITE_OK ) rc = rc2;
       130162  +  }
       130163  +
       130164  +  return rc;
       130165  +}
       130166  +
       130167  +/*
       130168  +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
       130169  +** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
       130170  +** consists of two varints, the absolute level number of the input segments 
       130171  +** and the number of input segments.
       130172  +**
       130173  +** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
       130174  +** set *pRc to an SQLite error code before returning.
       130175  +*/
       130176  +static void fts3IncrmergeHintPush(
       130177  +  Blob *pHint,                    /* Hint blob to append to */
       130178  +  i64 iAbsLevel,                  /* First varint to store in hint */
       130179  +  int nInput,                     /* Second varint to store in hint */
       130180  +  int *pRc                        /* IN/OUT: Error code */
       130181  +){
       130182  +  blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
       130183  +  if( *pRc==SQLITE_OK ){
       130184  +    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
       130185  +    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
       130186  +  }
       130187  +}
       130188  +
       130189  +/*
       130190  +** Read the last entry (most recently pushed) from the hint blob *pHint
       130191  +** and then remove the entry. Write the two values read to *piAbsLevel and 
       130192  +** *pnInput before returning.
       130193  +**
       130194  +** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
       130195  +** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
       130196  +*/
       130197  +static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
       130198  +  const int nHint = pHint->n;
       130199  +  int i;
       130200  +
       130201  +  i = pHint->n-2;
       130202  +  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
       130203  +  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
       130204  +
       130205  +  pHint->n = i;
       130206  +  i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
       130207  +  i += sqlite3Fts3GetVarint32(&pHint->a[i], pnInput);
       130208  +  if( i!=nHint ) return SQLITE_CORRUPT_VTAB;
       130209  +
       130210  +  return SQLITE_OK;
       130211  +}
       130212  +
       130213  +
       130214  +/*
       130215  +** Attempt an incremental merge that writes nMerge leaf blocks.
       130216  +**
       130217  +** Incremental merges happen nMin segments at a time. The two
       130218  +** segments to be merged are the nMin oldest segments (the ones with
       130219  +** the smallest indexes) in the highest level that contains at least
       130220  +** nMin segments. Multiple merges might occur in an attempt to write the 
       130221  +** quota of nMerge leaf blocks.
       130222  +*/
       130223  +SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
       130224  +  int rc;                         /* Return code */
       130225  +  int nRem = nMerge;              /* Number of leaf pages yet to  be written */
       130226  +  Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
       130227  +  Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
       130228  +  IncrmergeWriter *pWriter;       /* Writer object */
       130229  +  int nSeg = 0;                   /* Number of input segments */
       130230  +  sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
       130231  +  Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
       130232  +  int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
       130233  +
       130234  +  /* Allocate space for the cursor, filter and writer objects */
       130235  +  const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
       130236  +  pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
       130237  +  if( !pWriter ) return SQLITE_NOMEM;
       130238  +  pFilter = (Fts3SegFilter *)&pWriter[1];
       130239  +  pCsr = (Fts3MultiSegReader *)&pFilter[1];
       130240  +
       130241  +  rc = fts3IncrmergeHintLoad(p, &hint);
       130242  +  while( rc==SQLITE_OK && nRem>0 ){
       130243  +    const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
       130244  +    sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
       130245  +    int bUseHint = 0;             /* True if attempting to append */
       130246  +
       130247  +    /* Search the %_segdir table for the absolute level with the smallest
       130248  +    ** relative level number that contains at least nMin segments, if any.
       130249  +    ** If one is found, set iAbsLevel to the absolute level number and
       130250  +    ** nSeg to nMin. If no level with at least nMin segments can be found, 
       130251  +    ** set nSeg to -1.
       130252  +    */
       130253  +    rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
       130254  +    sqlite3_bind_int(pFindLevel, 1, nMin);
       130255  +    if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
       130256  +      iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
       130257  +      nSeg = nMin;
       130258  +    }else{
       130259  +      nSeg = -1;
       130260  +    }
       130261  +    rc = sqlite3_reset(pFindLevel);
       130262  +
       130263  +    /* If the hint read from the %_stat table is not empty, check if the
       130264  +    ** last entry in it specifies a relative level smaller than or equal
       130265  +    ** to the level identified by the block above (if any). If so, this 
       130266  +    ** iteration of the loop will work on merging at the hinted level.
       130267  +    */
       130268  +    if( rc==SQLITE_OK && hint.n ){
       130269  +      int nHint = hint.n;
       130270  +      sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
       130271  +      int nHintSeg = 0;                     /* Hint number of segments */
       130272  +
       130273  +      rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
       130274  +      if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
       130275  +        iAbsLevel = iHintAbsLevel;
       130276  +        nSeg = nHintSeg;
       130277  +        bUseHint = 1;
       130278  +        bDirtyHint = 1;
       130279  +      }else{
       130280  +        /* This undoes the effect of the HintPop() above - so that no entry
       130281  +        ** is removed from the hint blob.  */
       130282  +        hint.n = nHint;
       130283  +      }
       130284  +    }
       130285  +
       130286  +    /* If nSeg is less that zero, then there is no level with at least
       130287  +    ** nMin segments and no hint in the %_stat table. No work to do.
       130288  +    ** Exit early in this case.  */
       130289  +    if( nSeg<0 ) break;
       130290  +
       130291  +    /* Open a cursor to iterate through the contents of the oldest nSeg 
       130292  +    ** indexes of absolute level iAbsLevel. If this cursor is opened using 
       130293  +    ** the 'hint' parameters, it is possible that there are less than nSeg
       130294  +    ** segments available in level iAbsLevel. In this case, no work is
       130295  +    ** done on iAbsLevel - fall through to the next iteration of the loop 
       130296  +    ** to start work on some other level.  */
       130297  +    memset(pWriter, 0, nAlloc);
       130298  +    pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
       130299  +    if( rc==SQLITE_OK ){
       130300  +      rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
       130301  +    }
       130302  +    if( SQLITE_OK==rc && pCsr->nSegment==nSeg
       130303  +     && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
       130304  +     && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
       130305  +    ){
       130306  +      int iIdx = 0;               /* Largest idx in level (iAbsLevel+1) */
       130307  +      rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
       130308  +      if( rc==SQLITE_OK ){
       130309  +        if( bUseHint && iIdx>0 ){
       130310  +          const char *zKey = pCsr->zTerm;
       130311  +          int nKey = pCsr->nTerm;
       130312  +          rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
       130313  +        }else{
       130314  +          rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
       130315  +        }
       130316  +      }
       130317  +
       130318  +      if( rc==SQLITE_OK && pWriter->nLeafEst ){
       130319  +        fts3LogMerge(nSeg, iAbsLevel);
       130320  +        do {
       130321  +          rc = fts3IncrmergeAppend(p, pWriter, pCsr);
       130322  +          if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
       130323  +          if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
       130324  +        }while( rc==SQLITE_ROW );
       130325  +
       130326  +        /* Update or delete the input segments */
       130327  +        if( rc==SQLITE_OK ){
       130328  +          nRem -= (1 + pWriter->nWork);
       130329  +          rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
       130330  +          if( nSeg!=0 ){
       130331  +            bDirtyHint = 1;
       130332  +            fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
       130333  +          }
       130334  +        }
       130335  +      }
       130336  +
       130337  +      fts3IncrmergeRelease(p, pWriter, &rc);
       130338  +    }
       130339  +
       130340  +    sqlite3Fts3SegReaderFinish(pCsr);
       130341  +  }
       130342  +
       130343  +  /* Write the hint values into the %_stat table for the next incr-merger */
       130344  +  if( bDirtyHint && rc==SQLITE_OK ){
       130345  +    rc = fts3IncrmergeHintStore(p, &hint);
       130346  +  }
       130347  +
       130348  +  sqlite3_free(pWriter);
       130349  +  sqlite3_free(hint.a);
       130350  +  return rc;
       130351  +}
       130352  +
       130353  +/*
       130354  +** Convert the text beginning at *pz into an integer and return
       130355  +** its value.  Advance *pz to point to the first character past
       130356  +** the integer.
       130357  +*/
       130358  +static int fts3Getint(const char **pz){
       130359  +  const char *z = *pz;
       130360  +  int i = 0;
       130361  +  while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
       130362  +  *pz = z;
       130363  +  return i;
       130364  +}
       130365  +
       130366  +/*
       130367  +** Process statements of the form:
       130368  +**
       130369  +**    INSERT INTO table(table) VALUES('merge=A,B');
       130370  +**
       130371  +** A and B are integers that decode to be the number of leaf pages
       130372  +** written for the merge, and the minimum number of segments on a level
       130373  +** before it will be selected for a merge, respectively.
       130374  +*/
       130375  +static int fts3DoIncrmerge(
       130376  +  Fts3Table *p,                   /* FTS3 table handle */
       130377  +  const char *zParam              /* Nul-terminated string containing "A,B" */
       130378  +){
       130379  +  int rc;
       130380  +  int nMin = (FTS3_MERGE_COUNT / 2);
       130381  +  int nMerge = 0;
       130382  +  const char *z = zParam;
       130383  +
       130384  +  /* Read the first integer value */
       130385  +  nMerge = fts3Getint(&z);
       130386  +
       130387  +  /* If the first integer value is followed by a ',',  read the second
       130388  +  ** integer value. */
       130389  +  if( z[0]==',' && z[1]!='\0' ){
       130390  +    z++;
       130391  +    nMin = fts3Getint(&z);
       130392  +  }
       130393  +
       130394  +  if( z[0]!='\0' || nMin<2 ){
       130395  +    rc = SQLITE_ERROR;
       130396  +  }else{
       130397  +    rc = SQLITE_OK;
       130398  +    if( !p->bHasStat ){
       130399  +      assert( p->bFts4==0 );
       130400  +      sqlite3Fts3CreateStatTable(&rc, p);
       130401  +    }
       130402  +    if( rc==SQLITE_OK ){
       130403  +      rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
       130404  +    }
       130405  +    sqlite3Fts3SegmentsClose(p);
       130406  +  }
       130407  +  return rc;
       130408  +}
       130409  +
       130410  +/*
       130411  +** Process statements of the form:
       130412  +**
       130413  +**    INSERT INTO table(table) VALUES('automerge=X');
       130414  +**
       130415  +** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
       130416  +** turn it on.  The setting is persistent.
       130417  +*/
       130418  +static int fts3DoAutoincrmerge(
       130419  +  Fts3Table *p,                   /* FTS3 table handle */
       130420  +  const char *zParam              /* Nul-terminated string containing boolean */
       130421  +){
       130422  +  int rc = SQLITE_OK;
       130423  +  sqlite3_stmt *pStmt = 0;
       130424  +  p->bAutoincrmerge = fts3Getint(&zParam)!=0;
       130425  +  if( !p->bHasStat ){
       130426  +    assert( p->bFts4==0 );
       130427  +    sqlite3Fts3CreateStatTable(&rc, p);
       130428  +    if( rc ) return rc;
       130429  +  }
       130430  +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
       130431  +  if( rc ) return rc;;
       130432  +  sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
       130433  +  sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
       130434  +  sqlite3_step(pStmt);
       130435  +  rc = sqlite3_reset(pStmt);
       130436  +  return rc;
       130437  +}
       130438  +
       130439  +/*
       130440  +** Return a 64-bit checksum for the FTS index entry specified by the
       130441  +** arguments to this function.
       130442  +*/
       130443  +static u64 fts3ChecksumEntry(
       130444  +  const char *zTerm,              /* Pointer to buffer containing term */
       130445  +  int nTerm,                      /* Size of zTerm in bytes */
       130446  +  int iLangid,                    /* Language id for current row */
       130447  +  int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
       130448  +  i64 iDocid,                     /* Docid for current row. */
       130449  +  int iCol,                       /* Column number */
       130450  +  int iPos                        /* Position */
       130451  +){
       130452  +  int i;
       130453  +  u64 ret = (u64)iDocid;
       130454  +
       130455  +  ret += (ret<<3) + iLangid;
       130456  +  ret += (ret<<3) + iIndex;
       130457  +  ret += (ret<<3) + iCol;
       130458  +  ret += (ret<<3) + iPos;
       130459  +  for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
       130460  +
       130461  +  return ret;
       130462  +}
       130463  +
       130464  +/*
       130465  +** Return a checksum of all entries in the FTS index that correspond to
       130466  +** language id iLangid. The checksum is calculated by XORing the checksums
       130467  +** of each individual entry (see fts3ChecksumEntry()) together.
       130468  +**
       130469  +** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
       130470  +** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
       130471  +** return value is undefined in this case.
       130472  +*/
       130473  +static u64 fts3ChecksumIndex(
       130474  +  Fts3Table *p,                   /* FTS3 table handle */
       130475  +  int iLangid,                    /* Language id to return cksum for */
       130476  +  int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
       130477  +  int *pRc                        /* OUT: Return code */
       130478  +){
       130479  +  Fts3SegFilter filter;
       130480  +  Fts3MultiSegReader csr;
       130481  +  int rc;
       130482  +  u64 cksum = 0;
       130483  +
       130484  +  assert( *pRc==SQLITE_OK );
       130485  +
       130486  +  memset(&filter, 0, sizeof(filter));
       130487  +  memset(&csr, 0, sizeof(csr));
       130488  +  filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
       130489  +  filter.flags |= FTS3_SEGMENT_SCAN;
       130490  +
       130491  +  rc = sqlite3Fts3SegReaderCursor(
       130492  +      p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
       130493  +  );
       130494  +  if( rc==SQLITE_OK ){
       130495  +    rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
       130496  +  }
       130497  +
       130498  +  if( rc==SQLITE_OK ){
       130499  +    while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
       130500  +      char *pCsr = csr.aDoclist;
       130501  +      char *pEnd = &pCsr[csr.nDoclist];
       130502  +
       130503  +      i64 iDocid = 0;
       130504  +      i64 iCol = 0;
       130505  +      i64 iPos = 0;
       130506  +
       130507  +      pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
       130508  +      while( pCsr<pEnd ){
       130509  +        i64 iVal = 0;
       130510  +        pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
       130511  +        if( pCsr<pEnd ){
       130512  +          if( iVal==0 || iVal==1 ){
       130513  +            iCol = 0;
       130514  +            iPos = 0;
       130515  +            if( iVal ){
       130516  +              pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
       130517  +            }else{
       130518  +              pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
       130519  +              iDocid += iVal;
       130520  +            }
       130521  +          }else{
       130522  +            iPos += (iVal - 2);
       130523  +            cksum = cksum ^ fts3ChecksumEntry(
       130524  +                csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
       130525  +                (int)iCol, (int)iPos
       130526  +            );
       130527  +          }
       130528  +        }
       130529  +      }
       130530  +    }
       130531  +  }
       130532  +  sqlite3Fts3SegReaderFinish(&csr);
       130533  +
       130534  +  *pRc = rc;
       130535  +  return cksum;
       130536  +}
       130537  +
       130538  +/*
       130539  +** Check if the contents of the FTS index match the current contents of the
       130540  +** content table. If no error occurs and the contents do match, set *pbOk
       130541  +** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
       130542  +** to false before returning.
       130543  +**
       130544  +** If an error occurs (e.g. an OOM or IO error), return an SQLite error 
       130545  +** code. The final value of *pbOk is undefined in this case.
       130546  +*/
       130547  +static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
       130548  +  int rc = SQLITE_OK;             /* Return code */
       130549  +  u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
       130550  +  u64 cksum2 = 0;                 /* Checksum based on %_content contents */
       130551  +  sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
       130552  +
       130553  +  /* This block calculates the checksum according to the FTS index. */
       130554  +  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
       130555  +  if( rc==SQLITE_OK ){
       130556  +    int rc2;
       130557  +    sqlite3_bind_int(pAllLangid, 1, p->nIndex);
       130558  +    while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
       130559  +      int iLangid = sqlite3_column_int(pAllLangid, 0);
       130560  +      int i;
       130561  +      for(i=0; i<p->nIndex; i++){
       130562  +        cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
       130563  +      }
       130564  +    }
       130565  +    rc2 = sqlite3_reset(pAllLangid);
       130566  +    if( rc==SQLITE_OK ) rc = rc2;
       130567  +  }
       130568  +
       130569  +  /* This block calculates the checksum according to the %_content table */
       130570  +  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
       130571  +  if( rc==SQLITE_OK ){
       130572  +    sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
       130573  +    sqlite3_stmt *pStmt = 0;
       130574  +    char *zSql;
       130575  +   
       130576  +    zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
       130577  +    if( !zSql ){
       130578  +      rc = SQLITE_NOMEM;
       130579  +    }else{
       130580  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
       130581  +      sqlite3_free(zSql);
       130582  +    }
       130583  +
       130584  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
       130585  +      i64 iDocid = sqlite3_column_int64(pStmt, 0);
       130586  +      int iLang = langidFromSelect(p, pStmt);
       130587  +      int iCol;
       130588  +
       130589  +      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
       130590  +        const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
       130591  +        int nText = sqlite3_column_bytes(pStmt, iCol+1);
       130592  +        sqlite3_tokenizer_cursor *pT = 0;
       130593  +
       130594  +        rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
       130595  +        while( rc==SQLITE_OK ){
       130596  +          char const *zToken;       /* Buffer containing token */
       130597  +          int nToken;               /* Number of bytes in token */
       130598  +          int iDum1, iDum2;         /* Dummy variables */
       130599  +          int iPos;                 /* Position of token in zText */
       130600  +
       130601  +          rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
       130602  +          if( rc==SQLITE_OK ){
       130603  +            int i;
       130604  +            cksum2 = cksum2 ^ fts3ChecksumEntry(
       130605  +                zToken, nToken, iLang, 0, iDocid, iCol, iPos
       130606  +            );
       130607  +            for(i=1; i<p->nIndex; i++){
       130608  +              if( p->aIndex[i].nPrefix<=nToken ){
       130609  +                cksum2 = cksum2 ^ fts3ChecksumEntry(
       130610  +                  zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
       130611  +                );
       130612  +              }
       130613  +            }
       130614  +          }
       130615  +        }
       130616  +        if( pT ) pModule->xClose(pT);
       130617  +        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
       130618  +      }
       130619  +    }
       130620  +
       130621  +    sqlite3_finalize(pStmt);
       130622  +  }
       130623  +
       130624  +  *pbOk = (cksum1==cksum2);
       130625  +  return rc;
       130626  +}
       130627  +
       130628  +/*
       130629  +** Run the integrity-check. If no error occurs and the current contents of
       130630  +** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
       130631  +** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
       130632  +**
       130633  +** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite 
       130634  +** error code.
       130635  +**
       130636  +** The integrity-check works as follows. For each token and indexed token
       130637  +** prefix in the document set, a 64-bit checksum is calculated (by code
       130638  +** in fts3ChecksumEntry()) based on the following:
       130639  +**
       130640  +**     + The index number (0 for the main index, 1 for the first prefix
       130641  +**       index etc.),
       130642  +**     + The token (or token prefix) text itself, 
       130643  +**     + The language-id of the row it appears in,
       130644  +**     + The docid of the row it appears in,
       130645  +**     + The column it appears in, and
       130646  +**     + The tokens position within that column.
       130647  +**
       130648  +** The checksums for all entries in the index are XORed together to create
       130649  +** a single checksum for the entire index.
       130650  +**
       130651  +** The integrity-check code calculates the same checksum in two ways:
       130652  +**
       130653  +**     1. By scanning the contents of the FTS index, and 
       130654  +**     2. By scanning and tokenizing the content table.
       130655  +**
       130656  +** If the two checksums are identical, the integrity-check is deemed to have
       130657  +** passed.
       130658  +*/
       130659  +static int fts3DoIntegrityCheck(
       130660  +  Fts3Table *p                    /* FTS3 table handle */
       130661  +){
       130662  +  int rc;
       130663  +  int bOk = 0;
       130664  +  rc = fts3IntegrityCheck(p, &bOk);
       130665  +  if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB;
       130666  +  return rc;
       130667  +}
       130668  +
128230 130669   /*
128231 130670   ** Handle a 'special' INSERT of the form:
128232 130671   **
128233 130672   **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
128234 130673   **
128235 130674   ** Argument pVal contains the result of <expr>. Currently the only 
128236 130675   ** meaningful value to insert is the text 'optimize'.
................................................................................
128242 130681   
128243 130682     if( !zVal ){
128244 130683       return SQLITE_NOMEM;
128245 130684     }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
128246 130685       rc = fts3DoOptimize(p, 0);
128247 130686     }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
128248 130687       rc = fts3DoRebuild(p);
       130688  +  }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
       130689  +    rc = fts3DoIntegrityCheck(p);
       130690  +  }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
       130691  +    rc = fts3DoIncrmerge(p, &zVal[6]);
       130692  +  }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
       130693  +    rc = fts3DoAutoincrmerge(p, &zVal[10]);
128249 130694   #ifdef SQLITE_TEST
128250 130695     }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
128251 130696       p->nNodeSize = atoi(&zVal[9]);
128252 130697       rc = SQLITE_OK;
128253 130698     }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
128254 130699       p->nMaxPendingData = atoi(&zVal[11]);
128255 130700       rc = SQLITE_OK;
................................................................................
128437 130882   }
128438 130883   
128439 130884   /*
128440 130885   ** This function does the work for the xUpdate method of FTS3 virtual
128441 130886   ** tables. The schema of the virtual table being:
128442 130887   **
128443 130888   **     CREATE TABLE <table name>( 
128444         -**       <user COLUMns>,
       130889  +**       <user columns>,
128445 130890   **       <table name> HIDDEN, 
128446 130891   **       docid HIDDEN, 
128447 130892   **       <langid> HIDDEN
128448 130893   **     );
128449 130894   **
128450 130895   ** 
128451 130896   */
................................................................................
128569 131014       }
128570 131015       if( p->bHasDocsize ){
128571 131016         fts3InsertDocsize(&rc, p, aSzIns);
128572 131017       }
128573 131018       nChng++;
128574 131019     }
128575 131020   
128576         -  if( p->bHasStat ){
       131021  +  if( p->bFts4 ){
128577 131022       fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
128578 131023     }
128579 131024   
128580 131025    update_out:
128581 131026     sqlite3_free(aSzIns);
128582 131027     sqlite3Fts3SegmentsClose(p);
128583 131028     return rc;
................................................................................
128966 131411   ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
128967 131412   ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
128968 131413   */
128969 131414   static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
128970 131415     SnippetIter *p = (SnippetIter *)ctx;
128971 131416     SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
128972 131417     char *pCsr;
       131418  +  int rc;
128973 131419   
128974 131420     pPhrase->nToken = pExpr->pPhrase->nToken;
128975         -
128976         -  pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
       131421  +  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
       131422  +  assert( rc==SQLITE_OK || pCsr==0 );
128977 131423     if( pCsr ){
128978 131424       int iFirst = 0;
128979 131425       pPhrase->pList = pCsr;
128980 131426       fts3GetDeltaPosition(&pCsr, &iFirst);
128981 131427       assert( iFirst>=0 );
128982 131428       pPhrase->pHead = pCsr;
128983 131429       pPhrase->pTail = pCsr;
128984 131430       pPhrase->iHead = iFirst;
128985 131431       pPhrase->iTail = iFirst;
128986 131432     }else{
128987         -    assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 );
       131433  +    assert( rc!=SQLITE_OK || (
       131434  +       pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 
       131435  +    ));
128988 131436     }
128989 131437   
128990         -  return SQLITE_OK;
       131438  +  return rc;
128991 131439   }
128992 131440   
128993 131441   /*
128994 131442   ** Select the fragment of text consisting of nFragment contiguous tokens 
128995 131443   ** from column iCol that represent the "best" snippet. The best snippet
128996 131444   ** is the snippet with the highest score, where scores are calculated
128997 131445   ** by adding:
................................................................................
129376 131824   ** array that are different for each row returned by the query.
129377 131825   */
129378 131826   static int fts3ExprLocalHitsCb(
129379 131827     Fts3Expr *pExpr,                /* Phrase expression node */
129380 131828     int iPhrase,                    /* Phrase number */
129381 131829     void *pCtx                      /* Pointer to MatchInfo structure */
129382 131830   ){
       131831  +  int rc = SQLITE_OK;
129383 131832     MatchInfo *p = (MatchInfo *)pCtx;
129384 131833     int iStart = iPhrase * p->nCol * 3;
129385 131834     int i;
129386 131835   
129387         -  for(i=0; i<p->nCol; i++){
       131836  +  for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
129388 131837       char *pCsr;
129389         -    pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i);
       131838  +    rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
129390 131839       if( pCsr ){
129391 131840         p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
129392 131841       }else{
129393 131842         p->aMatchinfo[iStart+i*3] = 0;
129394 131843       }
129395 131844     }
129396 131845   
129397         -  return SQLITE_OK;
       131846  +  return rc;
129398 131847   }
129399 131848   
129400 131849   static int fts3MatchinfoCheck(
129401 131850     Fts3Table *pTab, 
129402 131851     char cArg,
129403 131852     char **pzErr
129404 131853   ){
129405 131854     if( (cArg==FTS3_MATCHINFO_NPHRASE)
129406 131855      || (cArg==FTS3_MATCHINFO_NCOL)
129407         -   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat)
129408         -   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat)
       131856  +   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
       131857  +   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
129409 131858      || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
129410 131859      || (cArg==FTS3_MATCHINFO_LCS)
129411 131860      || (cArg==FTS3_MATCHINFO_HITS)
129412 131861     ){
129413 131862       return SQLITE_OK;
129414 131863     }
129415 131864     *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
................................................................................
129551 132000     }
129552 132001   
129553 132002     for(iCol=0; iCol<pInfo->nCol; iCol++){
129554 132003       int nLcs = 0;                 /* LCS value for this column */
129555 132004       int nLive = 0;                /* Number of iterators in aIter not at EOF */
129556 132005   
129557 132006       for(i=0; i<pInfo->nPhrase; i++){
       132007  +      int rc;
129558 132008         LcsIterator *pIt = &aIter[i];
129559         -      pIt->pRead = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol);
       132009  +      rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
       132010  +      if( rc!=SQLITE_OK ) return rc;
129560 132011         if( pIt->pRead ){
129561 132012           pIt->iPos = pIt->iPosOffset;
129562 132013           fts3LcsIteratorAdvance(&aIter[i]);
129563 132014           nLive++;
129564 132015         }
129565 132016       }
129566 132017   
................................................................................
129904 132355   */
129905 132356   static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
129906 132357     TermOffsetCtx *p = (TermOffsetCtx *)ctx;
129907 132358     int nTerm;                      /* Number of tokens in phrase */
129908 132359     int iTerm;                      /* For looping through nTerm phrase terms */
129909 132360     char *pList;                    /* Pointer to position list for phrase */
129910 132361     int iPos = 0;                   /* First position in position-list */
       132362  +  int rc;
129911 132363   
129912 132364     UNUSED_PARAMETER(iPhrase);
129913         -  pList = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
       132365  +  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
129914 132366     nTerm = pExpr->pPhrase->nToken;
129915 132367     if( pList ){
129916 132368       fts3GetDeltaPosition(&pList, &iPos);
129917 132369       assert( iPos>=0 );
129918 132370     }
129919 132371   
129920 132372     for(iTerm=0; iTerm<nTerm; iTerm++){
129921 132373       TermOffset *pT = &p->aTerm[p->iTerm++];
129922 132374       pT->iOff = nTerm-iTerm-1;
129923 132375       pT->pList = pList;
129924 132376       pT->iPos = iPos;
129925 132377     }
129926 132378   
129927         -  return SQLITE_OK;
       132379  +  return rc;
129928 132380   }
129929 132381   
129930 132382   /*
129931 132383   ** Implementation of offsets() function.
129932 132384   */
129933 132385   SQLITE_PRIVATE void sqlite3Fts3Offsets(
129934 132386     sqlite3_context *pCtx,          /* SQLite function call context */
................................................................................
130291 132743     int eCoordType;
130292 132744   };
130293 132745   
130294 132746   /* Possible values for eCoordType: */
130295 132747   #define RTREE_COORD_REAL32 0
130296 132748   #define RTREE_COORD_INT32  1
130297 132749   
       132750  +/*
       132751  +** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
       132752  +** only deal with integer coordinates.  No floating point operations
       132753  +** will be done.
       132754  +*/
       132755  +#ifdef SQLITE_RTREE_INT_ONLY
       132756  +  typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
       132757  +  typedef int RtreeValue;                  /* Low accuracy coordinate */
       132758  +#else
       132759  +  typedef double RtreeDValue;              /* High accuracy coordinate */
       132760  +  typedef float RtreeValue;                /* Low accuracy coordinate */
       132761  +#endif
       132762  +
130298 132763   /*
130299 132764   ** The minimum number of cells allowed for a node is a third of the 
130300 132765   ** maximum. In Gutman's notation:
130301 132766   **
130302 132767   **     m = M/3
130303 132768   **
130304 132769   ** If an R*-tree "Reinsert" operation is required, the same number of
................................................................................
130326 132791     int iCell;                        /* Index of current cell in pNode */
130327 132792     int iStrategy;                    /* Copy of idxNum search parameter */
130328 132793     int nConstraint;                  /* Number of entries in aConstraint */
130329 132794     RtreeConstraint *aConstraint;     /* Search constraints. */
130330 132795   };
130331 132796   
130332 132797   union RtreeCoord {
130333         -  float f;
       132798  +  RtreeValue f;
130334 132799     int i;
130335 132800   };
130336 132801   
130337 132802   /*
130338 132803   ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
130339         -** formatted as a double. This macro assumes that local variable pRtree points
130340         -** to the Rtree structure associated with the RtreeCoord.
       132804  +** formatted as a RtreeDValue (double or int64). This macro assumes that local
       132805  +** variable pRtree points to the Rtree structure associated with the
       132806  +** RtreeCoord.
130341 132807   */
130342         -#define DCOORD(coord) (                           \
130343         -  (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
130344         -    ((double)coord.f) :                           \
130345         -    ((double)coord.i)                             \
130346         -)
       132808  +#ifdef SQLITE_RTREE_INT_ONLY
       132809  +# define DCOORD(coord) ((RtreeDValue)coord.i)
       132810  +#else
       132811  +# define DCOORD(coord) (                           \
       132812  +    (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
       132813  +      ((double)coord.f) :                           \
       132814  +      ((double)coord.i)                             \
       132815  +  )
       132816  +#endif
130347 132817   
130348 132818   /*
130349 132819   ** A search constraint.
130350 132820   */
130351 132821   struct RtreeConstraint {
130352 132822     int iCoord;                     /* Index of constrained coordinate */
130353 132823     int op;                         /* Constraining operation */
130354         -  double rValue;                  /* Constraint value. */
130355         -  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
       132824  +  RtreeDValue rValue;             /* Constraint value. */
       132825  +  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
130356 132826     sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
130357 132827   };
130358 132828   
130359 132829   /* Possible values for RtreeConstraint.op */
130360 132830   #define RTREE_EQ    0x41
130361 132831   #define RTREE_LE    0x42
130362 132832   #define RTREE_LT    0x43
................................................................................
130396 132866   /*
130397 132867   ** An instance of this structure must be supplied as a blob argument to
130398 132868   ** the right-hand-side of an SQL MATCH operator used to constrain an
130399 132869   ** r-tree query.
130400 132870   */
130401 132871   struct RtreeMatchArg {
130402 132872     u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
130403         -  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
       132873  +  int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
130404 132874     void *pContext;
130405 132875     int nParam;
130406         -  double aParam[1];
       132876  +  RtreeDValue aParam[1];
130407 132877   };
130408 132878   
130409 132879   /*
130410 132880   ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
130411 132881   ** a single instance of the following structure is allocated. It is used
130412 132882   ** as the context for the user-function created by by s_r_g_c(). The object
130413 132883   ** is eventually deleted by the destructor mechanism provided by
130414 132884   ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
130415 132885   ** the geometry callback function).
130416 132886   */
130417 132887   struct RtreeGeomCallback {
130418         -  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
       132888  +  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
130419 132889     void *pContext;
130420 132890   };
130421 132891   
130422 132892   #ifndef MAX
130423 132893   # define MAX(x,y) ((x) < (y) ? (y) : (x))
130424 132894   #endif
130425 132895   #ifndef MIN
................................................................................
130977 133447   static int testRtreeGeom(
130978 133448     Rtree *pRtree,                  /* R-Tree object */
130979 133449     RtreeConstraint *pConstraint,   /* MATCH constraint to test */
130980 133450     RtreeCell *pCell,               /* Cell to test */
130981 133451     int *pbRes                      /* OUT: Test result */
130982 133452   ){
130983 133453     int i;
130984         -  double aCoord[RTREE_MAX_DIMENSIONS*2];
       133454  +  RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
130985 133455     int nCoord = pRtree->nDim*2;
130986 133456   
130987 133457     assert( pConstraint->op==RTREE_MATCH );
130988 133458     assert( pConstraint->pGeom );
130989 133459   
130990 133460     for(i=0; i<nCoord; i++){
130991 133461       aCoord[i] = DCOORD(pCell->aCoord[i]);
................................................................................
131007 133477     int ii;
131008 133478     int bRes = 0;
131009 133479     int rc = SQLITE_OK;
131010 133480   
131011 133481     nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
131012 133482     for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
131013 133483       RtreeConstraint *p = &pCursor->aConstraint[ii];
131014         -    double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
131015         -    double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
       133484  +    RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
       133485  +    RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
131016 133486   
131017 133487       assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
131018 133488           || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
131019 133489       );
131020 133490   
131021 133491       switch( p->op ){
131022 133492         case RTREE_LE: case RTREE_LT: 
................................................................................
131060 133530     RtreeCell cell;
131061 133531     int ii;
131062 133532     *pbEof = 0;
131063 133533   
131064 133534     nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
131065 133535     for(ii=0; ii<pCursor->nConstraint; ii++){
131066 133536       RtreeConstraint *p = &pCursor->aConstraint[ii];
131067         -    double coord = DCOORD(cell.aCoord[p->iCoord]);
       133537  +    RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
131068 133538       int res;
131069 133539       assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
131070 133540           || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
131071 133541       );
131072 133542       switch( p->op ){
131073 133543         case RTREE_LE: res = (coord<=p->rValue); break;
131074 133544         case RTREE_LT: res = (coord<p->rValue);  break;
................................................................................
131258 133728   
131259 133729     if( i==0 ){
131260 133730       i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
131261 133731       sqlite3_result_int64(ctx, iRowid);
131262 133732     }else{
131263 133733       RtreeCoord c;
131264 133734       nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
       133735  +#ifndef SQLITE_RTREE_INT_ONLY
131265 133736       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
131266 133737         sqlite3_result_double(ctx, c.f);
131267         -    }else{
       133738  +    }else
       133739  +#endif
       133740  +    {
131268 133741         assert( pRtree->eCoordType==RTREE_COORD_INT32 );
131269 133742         sqlite3_result_int(ctx, c.i);
131270 133743       }
131271 133744     }
131272 133745   
131273 133746     return SQLITE_OK;
131274 133747   }
................................................................................
131307 133780   
131308 133781     /* Check that value is actually a blob. */
131309 133782     if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
131310 133783   
131311 133784     /* Check that the blob is roughly the right size. */
131312 133785     nBlob = sqlite3_value_bytes(pValue);
131313 133786     if( nBlob<(int)sizeof(RtreeMatchArg) 
131314         -   || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
       133787  +   || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
131315 133788     ){
131316 133789       return SQLITE_ERROR;
131317 133790     }
131318 133791   
131319 133792     pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
131320 133793         sizeof(sqlite3_rtree_geometry) + nBlob
131321 133794     );
131322 133795     if( !pGeom ) return SQLITE_NOMEM;
131323 133796     memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
131324 133797     p = (RtreeMatchArg *)&pGeom[1];
131325 133798   
131326 133799     memcpy(p, sqlite3_value_blob(pValue), nBlob);
131327 133800     if( p->magic!=RTREE_GEOMETRY_MAGIC 
131328         -   || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
       133801  +   || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
131329 133802     ){
131330 133803       sqlite3_free(pGeom);
131331 133804       return SQLITE_ERROR;
131332 133805     }
131333 133806   
131334 133807     pGeom->pContext = p->pContext;
131335 133808     pGeom->nParam = p->nParam;
................................................................................
131393 133866               ** an sqlite3_rtree_geometry_callback() SQL user function.
131394 133867               */
131395 133868               rc = deserializeGeometry(argv[ii], p);
131396 133869               if( rc!=SQLITE_OK ){
131397 133870                 break;
131398 133871               }
131399 133872             }else{
       133873  +#ifdef SQLITE_RTREE_INT_ONLY
       133874  +            p->rValue = sqlite3_value_int64(argv[ii]);
       133875  +#else
131400 133876               p->rValue = sqlite3_value_double(argv[ii]);
       133877  +#endif
131401 133878             }
131402 133879           }
131403 133880         }
131404 133881       }
131405 133882     
131406 133883       if( rc==SQLITE_OK ){
131407 133884         pCsr->pNode = 0;
................................................................................
131527 134004     pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
131528 134005     return rc;
131529 134006   }
131530 134007   
131531 134008   /*
131532 134009   ** Return the N-dimensional volumn of the cell stored in *p.
131533 134010   */
131534         -static float cellArea(Rtree *pRtree, RtreeCell *p){
131535         -  float area = 1.0;
       134011  +static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
       134012  +  RtreeDValue area = (RtreeDValue)1;
131536 134013     int ii;
131537 134014     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
131538         -    area = (float)(area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
       134015  +    area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
131539 134016     }
131540 134017     return area;
131541 134018   }
131542 134019   
131543 134020   /*
131544 134021   ** Return the margin length of cell p. The margin length is the sum
131545 134022   ** of the objects size in each dimension.
131546 134023   */
131547         -static float cellMargin(Rtree *pRtree, RtreeCell *p){
131548         -  float margin = 0.0;
       134024  +static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
       134025  +  RtreeDValue margin = (RtreeDValue)0;
131549 134026     int ii;
131550 134027     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
131551         -    margin += (float)(DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
       134028  +    margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
131552 134029     }
131553 134030     return margin;
131554 134031   }
131555 134032   
131556 134033   /*
131557 134034   ** Store the union of cells p1 and p2 in p1.
131558 134035   */
................................................................................
131589 134066     }
131590 134067     return 1;
131591 134068   }
131592 134069   
131593 134070   /*
131594 134071   ** Return the amount cell p would grow by if it were unioned with pCell.
131595 134072   */
131596         -static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
131597         -  float area;
       134073  +static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
       134074  +  RtreeDValue area;
131598 134075     RtreeCell cell;
131599 134076     memcpy(&cell, p, sizeof(RtreeCell));
131600 134077     area = cellArea(pRtree, &cell);
131601 134078     cellUnion(pRtree, &cell, pCell);
131602 134079     return (cellArea(pRtree, &cell)-area);
131603 134080   }
131604 134081   
131605 134082   #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
131606         -static float cellOverlap(
       134083  +static RtreeDValue cellOverlap(
131607 134084     Rtree *pRtree, 
131608 134085     RtreeCell *p, 
131609 134086     RtreeCell *aCell, 
131610 134087     int nCell, 
131611 134088     int iExclude
131612 134089   ){
131613 134090     int ii;
131614         -  float overlap = 0.0;
       134091  +  RtreeDValue overlap = 0.0;
131615 134092     for(ii=0; ii<nCell; ii++){
131616 134093   #if VARIANT_RSTARTREE_CHOOSESUBTREE
131617 134094       if( ii!=iExclude )
131618 134095   #else
131619 134096       assert( iExclude==-1 );
131620 134097       UNUSED_PARAMETER(iExclude);
131621 134098   #endif
131622 134099       {
131623 134100         int jj;
131624         -      float o = 1.0;
       134101  +      RtreeDValue o = (RtreeDValue)1;
131625 134102         for(jj=0; jj<(pRtree->nDim*2); jj+=2){
131626         -        double x1;
131627         -        double x2;
       134103  +        RtreeDValue x1, x2;
131628 134104   
131629 134105           x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
131630 134106           x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
131631 134107   
131632 134108           if( x2<x1 ){
131633 134109             o = 0.0;
131634 134110             break;
131635 134111           }else{
131636         -          o = o * (float)(x2-x1);
       134112  +          o = o * (x2-x1);
131637 134113           }
131638 134114         }
131639 134115         overlap += o;
131640 134116       }
131641 134117     }
131642 134118     return overlap;
131643 134119   }
131644 134120   #endif
131645 134121   
131646 134122   #if VARIANT_RSTARTREE_CHOOSESUBTREE
131647         -static float cellOverlapEnlargement(
       134123  +static RtreeDValue cellOverlapEnlargement(
131648 134124     Rtree *pRtree, 
131649 134125     RtreeCell *p, 
131650 134126     RtreeCell *pInsert, 
131651 134127     RtreeCell *aCell, 
131652 134128     int nCell, 
131653 134129     int iExclude
131654 134130   ){
131655         -  double before;
131656         -  double after;
       134131  +  RtreeDValue before, after;
131657 134132     before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
131658 134133     cellUnion(pRtree, p, pInsert);
131659 134134     after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
131660         -  return (float)(after-before);
       134135  +  return (after-before);
131661 134136   }
131662 134137   #endif
131663 134138   
131664 134139   
131665 134140   /*
131666 134141   ** This function implements the ChooseLeaf algorithm from Gutman[84].
131667 134142   ** ChooseSubTree in r*tree terminology.
................................................................................
131677 134152     RtreeNode *pNode;
131678 134153     rc = nodeAcquire(pRtree, 1, 0, &pNode);
131679 134154   
131680 134155     for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
131681 134156       int iCell;
131682 134157       sqlite3_int64 iBest = 0;
131683 134158   
131684         -    float fMinGrowth = 0.0;
131685         -    float fMinArea = 0.0;
       134159  +    RtreeDValue fMinGrowth = 0.0;
       134160  +    RtreeDValue fMinArea = 0.0;
131686 134161   #if VARIANT_RSTARTREE_CHOOSESUBTREE
131687         -    float fMinOverlap = 0.0;
131688         -    float overlap;
       134162  +    RtreeDValue fMinOverlap = 0.0;
       134163  +    RtreeDValue overlap;
131689 134164   #endif
131690 134165   
131691 134166       int nCell = NCELL(pNode);
131692 134167       RtreeCell cell;
131693 134168       RtreeNode *pChild;
131694 134169   
131695 134170       RtreeCell *aCell = 0;
................................................................................
131712 134187   
131713 134188       /* Select the child node which will be enlarged the least if pCell
131714 134189       ** is inserted into it. Resolve ties by choosing the entry with
131715 134190       ** the smallest area.
131716 134191       */
131717 134192       for(iCell=0; iCell<nCell; iCell++){
131718 134193         int bBest = 0;
131719         -      float growth;
131720         -      float area;
       134194  +      RtreeDValue growth;
       134195  +      RtreeDValue area;
131721 134196         nodeGetCell(pRtree, pNode, iCell, &cell);
131722 134197         growth = cellGrowth(pRtree, &cell, pCell);
131723 134198         area = cellArea(pRtree, &cell);
131724 134199   
131725 134200   #if VARIANT_RSTARTREE_CHOOSESUBTREE
131726 134201         if( ii==(pRtree->iDepth-1) ){
131727 134202           overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
................................................................................
131840 134315     int nCell, 
131841 134316     int *piLeftSeed, 
131842 134317     int *piRightSeed
131843 134318   ){
131844 134319     int i;
131845 134320     int iLeftSeed = 0;
131846 134321     int iRightSeed = 1;
131847         -  float maxNormalInnerWidth = 0.0;
       134322  +  RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
131848 134323   
131849 134324     /* Pick two "seed" cells from the array of cells. The algorithm used
131850 134325     ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
131851 134326     ** indices of the two seed cells in the array are stored in local
131852 134327     ** variables iLeftSeek and iRightSeed.
131853 134328     */
131854 134329     for(i=0; i<pRtree->nDim; i++){
131855         -    float x1 = DCOORD(aCell[0].aCoord[i*2]);
131856         -    float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
131857         -    float x3 = x1;
131858         -    float x4 = x2;
       134330  +    RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
       134331  +    RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
       134332  +    RtreeDValue x3 = x1;
       134333  +    RtreeDValue x4 = x2;
131859 134334       int jj;
131860 134335   
131861 134336       int iCellLeft = 0;
131862 134337       int iCellRight = 0;
131863 134338   
131864 134339       for(jj=1; jj<nCell; jj++){
131865         -      float left = DCOORD(aCell[jj].aCoord[i*2]);
131866         -      float right = DCOORD(aCell[jj].aCoord[i*2+1]);
       134340  +      RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
       134341  +      RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
131867 134342   
131868 134343         if( left<x1 ) x1 = left;
131869 134344         if( right>x4 ) x4 = right;
131870 134345         if( left>x3 ){
131871 134346           x3 = left;
131872 134347           iCellRight = jj;
131873 134348         }
................................................................................
131874 134349         if( right<x2 ){
131875 134350           x2 = right;
131876 134351           iCellLeft = jj;
131877 134352         }
131878 134353       }
131879 134354   
131880 134355       if( x4!=x1 ){
131881         -      float normalwidth = (x3 - x2) / (x4 - x1);
       134356  +      RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
131882 134357         if( normalwidth>maxNormalInnerWidth ){
131883 134358           iLeftSeed = iCellLeft;
131884 134359           iRightSeed = iCellRight;
131885 134360         }
131886 134361       }
131887 134362     }
131888 134363   
................................................................................
131903 134378     RtreeCell *pLeftBox, 
131904 134379     RtreeCell *pRightBox,
131905 134380     int *aiUsed
131906 134381   ){
131907 134382     #define FABS(a) ((a)<0.0?-1.0*(a):(a))
131908 134383   
131909 134384     int iSelect = -1;
131910         -  float fDiff;
       134385  +  RtreeDValue fDiff;
131911 134386     int ii;
131912 134387     for(ii=0; ii<nCell; ii++){
131913 134388       if( aiUsed[ii]==0 ){
131914         -      float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
131915         -      float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
131916         -      float diff = FABS(right-left);
       134389  +      RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
       134390  +      RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
       134391  +      RtreeDValue diff = FABS(right-left);
131917 134392         if( iSelect<0 || diff>fDiff ){
131918 134393           fDiff = diff;
131919 134394           iSelect = ii;
131920 134395         }
131921 134396       }
131922 134397     }
131923 134398     aiUsed[iSelect] = 1;
................................................................................
131936 134411     int *piRightSeed
131937 134412   ){
131938 134413     int ii;
131939 134414     int jj;
131940 134415   
131941 134416     int iLeftSeed = 0;
131942 134417     int iRightSeed = 1;
131943         -  float fWaste = 0.0;
       134418  +  RtreeDValue fWaste = 0.0;
131944 134419   
131945 134420     for(ii=0; ii<nCell; ii++){
131946 134421       for(jj=ii+1; jj<nCell; jj++){
131947         -      float right = cellArea(pRtree, &aCell[jj]);
131948         -      float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
131949         -      float waste = growth - right;
       134422  +      RtreeDValue right = cellArea(pRtree, &aCell[jj]);
       134423  +      RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
       134424  +      RtreeDValue waste = growth - right;
131950 134425   
131951 134426         if( waste>fWaste ){
131952 134427           iLeftSeed = ii;
131953 134428           iRightSeed = jj;
131954 134429           fWaste = waste;
131955 134430         }
131956 134431       }
................................................................................
131977 134452   **
131978 134453   ** The aSpare array is used as temporary working space by the
131979 134454   ** sorting algorithm.
131980 134455   */
131981 134456   static void SortByDistance(
131982 134457     int *aIdx, 
131983 134458     int nIdx, 
131984         -  float *aDistance, 
       134459  +  RtreeDValue *aDistance, 
131985 134460     int *aSpare
131986 134461   ){
131987 134462     if( nIdx>1 ){
131988 134463       int iLeft = 0;
131989 134464       int iRight = 0;
131990 134465   
131991 134466       int nLeft = nIdx/2;
................................................................................
132003 134478         if( iLeft==nLeft ){
132004 134479           aIdx[iLeft+iRight] = aRight[iRight];
132005 134480           iRight++;
132006 134481         }else if( iRight==nRight ){
132007 134482           aIdx[iLeft+iRight] = aLeft[iLeft];
132008 134483           iLeft++;
132009 134484         }else{
132010         -        float fLeft = aDistance[aLeft[iLeft]];
132011         -        float fRight = aDistance[aRight[iRight]];
       134485  +        RtreeDValue fLeft = aDistance[aLeft[iLeft]];
       134486  +        RtreeDValue fRight = aDistance[aRight[iRight]];
132012 134487           if( fLeft<fRight ){
132013 134488             aIdx[iLeft+iRight] = aLeft[iLeft];
132014 134489             iLeft++;
132015 134490           }else{
132016 134491             aIdx[iLeft+iRight] = aRight[iRight];
132017 134492             iRight++;
132018 134493           }
................................................................................
132020 134495       }
132021 134496   
132022 134497   #if 0
132023 134498       /* Check that the sort worked */
132024 134499       {
132025 134500         int jj;
132026 134501         for(jj=1; jj<nIdx; jj++){
132027         -        float left = aDistance[aIdx[jj-1]];
132028         -        float right = aDistance[aIdx[jj]];
       134502  +        RtreeDValue left = aDistance[aIdx[jj-1]];
       134503  +        RtreeDValue right = aDistance[aIdx[jj]];
132029 134504           assert( left<=right );
132030 134505         }
132031 134506       }
132032 134507   #endif
132033 134508     }
132034 134509   }
132035 134510   
................................................................................
132064 134539   
132065 134540       SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
132066 134541       SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
132067 134542   
132068 134543       memcpy(aSpare, aLeft, sizeof(int)*nLeft);
132069 134544       aLeft = aSpare;
132070 134545       while( iLeft<nLeft || iRight<nRight ){
132071         -      double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
132072         -      double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
132073         -      double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
132074         -      double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
       134546  +      RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
       134547  +      RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
       134548  +      RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
       134549  +      RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
132075 134550         if( (iLeft!=nLeft) && ((iRight==nRight)
132076 134551          || (xleft1<xright1)
132077 134552          || (xleft1==xright1 && xleft2<xright2)
132078 134553         )){
132079 134554           aIdx[iLeft+iRight] = aLeft[iLeft];
132080 134555           iLeft++;
132081 134556         }else{
................................................................................
132085 134560       }
132086 134561   
132087 134562   #if 0
132088 134563       /* Check that the sort worked */
132089 134564       {
132090 134565         int jj;
132091 134566         for(jj=1; jj<nIdx; jj++){
132092         -        float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
132093         -        float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
132094         -        float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
132095         -        float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
       134567  +        RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
       134568  +        RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
       134569  +        RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
       134570  +        RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
132096 134571           assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
132097 134572         }
132098 134573       }
132099 134574   #endif
132100 134575     }
132101 134576   }
132102 134577   
................................................................................
132115 134590   ){
132116 134591     int **aaSorted;
132117 134592     int *aSpare;
132118 134593     int ii;
132119 134594   
132120 134595     int iBestDim = 0;
132121 134596     int iBestSplit = 0;
132122         -  float fBestMargin = 0.0;
       134597  +  RtreeDValue fBestMargin = 0.0;
132123 134598   
132124 134599     int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
132125 134600   
132126 134601     aaSorted = (int **)sqlite3_malloc(nByte);
132127 134602     if( !aaSorted ){
132128 134603       return SQLITE_NOMEM;
132129 134604     }
................................................................................
132136 134611       for(jj=0; jj<nCell; jj++){
132137 134612         aaSorted[ii][jj] = jj;
132138 134613       }
132139 134614       SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
132140 134615     }
132141 134616   
132142 134617     for(ii=0; ii<pRtree->nDim; ii++){
132143         -    float margin = 0.0;
132144         -    float fBestOverlap = 0.0;
132145         -    float fBestArea = 0.0;
       134618  +    RtreeDValue margin = 0.0;
       134619  +    RtreeDValue fBestOverlap = 0.0;
       134620  +    RtreeDValue fBestArea = 0.0;
132146 134621       int iBestLeft = 0;
132147 134622       int nLeft;
132148 134623   
132149 134624       for(
132150 134625         nLeft=RTREE_MINCELLS(pRtree); 
132151 134626         nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 
132152 134627         nLeft++
132153 134628       ){
132154 134629         RtreeCell left;
132155 134630         RtreeCell right;
132156 134631         int kk;
132157         -      float overlap;
132158         -      float area;
       134632  +      RtreeDValue overlap;
       134633  +      RtreeDValue area;
132159 134634   
132160 134635         memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
132161 134636         memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
132162 134637         for(kk=1; kk<(nCell-1); kk++){
132163 134638           if( kk<nLeft ){
132164 134639             cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
132165 134640           }else{
................................................................................
132234 134709     nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
132235 134710     aiUsed[iLeftSeed] = 1;
132236 134711     aiUsed[iRightSeed] = 1;
132237 134712   
132238 134713     for(i=nCell-2; i>0; i--){
132239 134714       RtreeCell *pNext;
132240 134715       pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
132241         -    float diff =  
       134716  +    RtreeDValue diff =  
132242 134717         cellGrowth(pRtree, pBboxLeft, pNext) - 
132243 134718         cellGrowth(pRtree, pBboxRight, pNext)
132244 134719       ;
132245 134720       if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
132246 134721        || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
132247 134722       ){
132248 134723         nodeInsertCell(pRtree, pRight, pNext);
................................................................................
132567 135042     RtreeNode *pNode, 
132568 135043     RtreeCell *pCell, 
132569 135044     int iHeight
132570 135045   ){
132571 135046     int *aOrder;
132572 135047     int *aSpare;
132573 135048     RtreeCell *aCell;
132574         -  float *aDistance;
       135049  +  RtreeDValue *aDistance;
132575 135050     int nCell;
132576         -  float aCenterCoord[RTREE_MAX_DIMENSIONS];
       135051  +  RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
132577 135052     int iDim;
132578 135053     int ii;
132579 135054     int rc = SQLITE_OK;
       135055  +  int n;
132580 135056   
132581         -  memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
       135057  +  memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
132582 135058   
132583 135059     nCell = NCELL(pNode)+1;
       135060  +  n = (nCell+1)&(~1);
132584 135061   
132585 135062     /* Allocate the buffers used by this operation. The allocation is
132586 135063     ** relinquished before this function returns.
132587 135064     */
132588         -  aCell = (RtreeCell *)sqlite3_malloc(nCell * (
132589         -    sizeof(RtreeCell) +         /* aCell array */
132590         -    sizeof(int)       +         /* aOrder array */
132591         -    sizeof(int)       +         /* aSpare array */
132592         -    sizeof(float)               /* aDistance array */
       135065  +  aCell = (RtreeCell *)sqlite3_malloc(n * (
       135066  +    sizeof(RtreeCell)     +         /* aCell array */
       135067  +    sizeof(int)           +         /* aOrder array */
       135068  +    sizeof(int)           +         /* aSpare array */
       135069  +    sizeof(RtreeDValue)             /* aDistance array */
132593 135070     ));
132594 135071     if( !aCell ){
132595 135072       return SQLITE_NOMEM;
132596 135073     }
132597         -  aOrder    = (int *)&aCell[nCell];
132598         -  aSpare    = (int *)&aOrder[nCell];
132599         -  aDistance = (float *)&aSpare[nCell];
       135074  +  aOrder    = (int *)&aCell[n];
       135075  +  aSpare    = (int *)&aOrder[n];
       135076  +  aDistance = (RtreeDValue *)&aSpare[n];
132600 135077   
132601 135078     for(ii=0; ii<nCell; ii++){
132602 135079       if( ii==(nCell-1) ){
132603 135080         memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
132604 135081       }else{
132605 135082         nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
132606 135083       }
132607 135084       aOrder[ii] = ii;
132608 135085       for(iDim=0; iDim<pRtree->nDim; iDim++){
132609         -      aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2]);
132610         -      aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2+1]);
       135086  +      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
       135087  +      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
132611 135088       }
132612 135089     }
132613 135090     for(iDim=0; iDim<pRtree->nDim; iDim++){
132614         -    aCenterCoord[iDim] = (float)(aCenterCoord[iDim]/((float)nCell*2.0));
       135091  +    aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
132615 135092     }
132616 135093   
132617 135094     for(ii=0; ii<nCell; ii++){
132618 135095       aDistance[ii] = 0.0;
132619 135096       for(iDim=0; iDim<pRtree->nDim; iDim++){
132620         -      float coord = (float)(DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
132621         -          DCOORD(aCell[ii].aCoord[iDim*2]));
       135097  +      RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
       135098  +                               DCOORD(aCell[ii].aCoord[iDim*2]));
132622 135099         aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
132623 135100       }
132624 135101     }
132625 135102   
132626 135103     SortByDistance(aOrder, nCell, aDistance, aSpare);
132627 135104     nodeZero(pRtree, pNode);
132628 135105   
................................................................................
132856 135333     ** conflict-handling mode specified by the user.
132857 135334     */
132858 135335     if( nData>1 ){
132859 135336       int ii;
132860 135337   
132861 135338       /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
132862 135339       assert( nData==(pRtree->nDim*2 + 3) );
       135340  +#ifndef SQLITE_RTREE_INT_ONLY
132863 135341       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
132864 135342         for(ii=0; ii<(pRtree->nDim*2); ii+=2){
132865         -        cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
132866         -        cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
       135343  +        cell.aCoord[ii].f = (RtreeValue)sqlite3_value_double(azData[ii+3]);
       135344  +        cell.aCoord[ii+1].f = (RtreeValue)sqlite3_value_double(azData[ii+4]);
132867 135345           if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
132868 135346             rc = SQLITE_CONSTRAINT;
132869 135347             goto constraint;
132870 135348           }
132871 135349         }
132872         -    }else{
       135350  +    }else
       135351  +#endif
       135352  +    {
132873 135353         for(ii=0; ii<(pRtree->nDim*2); ii+=2){
132874 135354           cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
132875 135355           cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
132876 135356           if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
132877 135357             rc = SQLITE_CONSTRAINT;
132878 135358             goto constraint;
132879 135359           }
................................................................................
133263 135743       RtreeCell cell;
133264 135744       int jj;
133265 135745   
133266 135746       nodeGetCell(&tree, &node, ii, &cell);
133267 135747       sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
133268 135748       nCell = (int)strlen(zCell);
133269 135749       for(jj=0; jj<tree.nDim*2; jj++){
133270         -      sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
       135750  +#ifndef SQLITE_RTREE_INT_ONLY
       135751  +      sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
       135752  +                       (double)cell.aCoord[jj].f);
       135753  +#else
       135754  +      sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
       135755  +                       cell.aCoord[jj].i);
       135756  +#endif
133271 135757         nCell = (int)strlen(zCell);
133272 135758       }
133273 135759   
133274 135760       if( zText ){
133275 135761         char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
133276 135762         sqlite3_free(zText);
133277 135763         zText = zTextNew;
................................................................................
133305 135791     int rc;
133306 135792   
133307 135793     rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
133308 135794     if( rc==SQLITE_OK ){
133309 135795       rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
133310 135796     }
133311 135797     if( rc==SQLITE_OK ){
       135798  +#ifdef SQLITE_RTREE_INT_ONLY
       135799  +    void *c = (void *)RTREE_COORD_INT32;
       135800  +#else
133312 135801       void *c = (void *)RTREE_COORD_REAL32;
       135802  +#endif
133313 135803       rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
133314 135804     }
133315 135805     if( rc==SQLITE_OK ){
133316 135806       void *c = (void *)RTREE_COORD_INT32;
133317 135807       rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
133318 135808     }
133319 135809   
................................................................................
133339 135829   ** table MATCH operators.
133340 135830   */
133341 135831   static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
133342 135832     RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
133343 135833     RtreeMatchArg *pBlob;
133344 135834     int nBlob;
133345 135835   
133346         -  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
       135836  +  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
133347 135837     pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
133348 135838     if( !pBlob ){
133349 135839       sqlite3_result_error_nomem(ctx);
133350 135840     }else{
133351 135841       int i;
133352 135842       pBlob->magic = RTREE_GEOMETRY_MAGIC;
133353 135843       pBlob->xGeom = pGeomCtx->xGeom;
133354 135844       pBlob->pContext = pGeomCtx->pContext;
133355 135845       pBlob->nParam = nArg;
133356 135846       for(i=0; i<nArg; i++){
       135847  +#ifdef SQLITE_RTREE_INT_ONLY
       135848  +      pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
       135849  +#else
133357 135850         pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
       135851  +#endif
133358 135852       }
133359 135853       sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
133360 135854     }
133361 135855   }
133362 135856   
133363 135857   /*
133364 135858   ** Register a new geometry function for use with the r-tree MATCH operator.
133365 135859   */
133366 135860   SQLITE_API int sqlite3_rtree_geometry_callback(
133367 135861     sqlite3 *db,
133368 135862     const char *zGeom,
133369         -  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
       135863  +  int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
133370 135864     void *pContext
133371 135865   ){
133372 135866     RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
133373 135867   
133374 135868     /* Allocate and populate the context object. */
133375 135869     pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
133376 135870     if( !pGeomCtx ) return SQLITE_NOMEM;
................................................................................
134007 136501   
134008 136502     UChar32 c;
134009 136503     int iInput = 0;
134010 136504     int iOut = 0;
134011 136505   
134012 136506     *ppCursor = 0;
134013 136507   
134014         -  if( nInput<0 ){
       136508  +  if( zInput==0 ){
       136509  +    nInput = 0;
       136510  +    zInput = "";
       136511  +  }else if( nInput<0 ){
134015 136512       nInput = strlen(zInput);
134016 136513     }
134017 136514     nChar = nInput+1;
134018 136515     pCsr = (IcuCursor *)sqlite3_malloc(
134019 136516         sizeof(IcuCursor) +                /* IcuCursor */
134020 136517         nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
134021 136518         (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */

Changes to test/sql_stmt_tests/testFDO.sqlite.

cannot compute difference between binary files

Changes to test/sql_stmt_tests/testFGF.sqlite.

cannot compute difference between binary files