sqllogictest
Check-in [f003563216]
Not logged in

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

Overview
Comment:Update the built-in SQLite to the latest 3.7.12 beta.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f003563216c39fd1194ee16a8b3258247961de12
User & Date: drh 2012-05-12 02:44:39
Context
2012-05-14
01:48
Update to SQLite version 3.7.12 check-in: e41449241c user: drh tags: trunk
2012-05-12
02:44
Update the built-in SQLite to the latest 3.7.12 beta. check-in: f003563216 user: drh tags: trunk
2012-03-19
15:24
Update to SQLite version 3.7.11-beta check-in: 9e7d09c07c user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/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-19 14:57:49 bc03d99a78e90c02b69037e5f5f81537b5a3ac60"
          660  +#define SQLITE_VERSION        "3.7.12"
          661  +#define SQLITE_VERSION_NUMBER 3007012
          662  +#define SQLITE_SOURCE_ID      "2012-05-12 01:14:27 93061c6e063fddfa6b5d21064a36b58cc97599f1"
   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
................................................................................
  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         }
................................................................................
 21257  21287   /* Convenient short-hand */
 21258  21288   #define UpperToLower sqlite3UpperToLower
 21259  21289   
 21260  21290   /*
 21261  21291   ** Some systems have stricmp().  Others have strcasecmp().  Because
 21262  21292   ** there is no consistency, we will define our own.
 21263  21293   **
 21264         -** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows
 21265         -** applications and extensions to compare the contents of two buffers
 21266         -** containing UTF-8 strings in a case-independent fashion, using the same
 21267         -** definition of case independence that SQLite uses internally when
 21268         -** comparing identifiers.
        21294  +** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
        21295  +** sqlite3_strnicmp() APIs allow applications and extensions to compare
        21296  +** the contents of two buffers containing UTF-8 strings in a
        21297  +** case-independent fashion, using the same definition of "case
        21298  +** independence" that SQLite uses internally when comparing identifiers.
 21269  21299   */
 21270  21300   SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
 21271  21301     register unsigned char *a, *b;
 21272  21302     a = (unsigned char *)zLeft;
 21273  21303     b = (unsigned char *)zRight;
 21274  21304     while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 21275  21305     return UpperToLower[*a] - UpperToLower[*b];
................................................................................
 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 630
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 (1562)
108082 108520   static const YYACTIONTYPE yy_action[] = {
108083         - /*     0 */   310,  328,  574,  573,   15,  172,  187,  596,   56,   56,
       108521  + /*     0 */   312,  330,  575,  574,   15,  171,  186,  597,   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,  235,   52,   52,   52,   51,  235,  627,
       108524  + /*    30 */   590,  584,   56,   56,   56,   56,  316,   54,   54,   54,
       108525  + /*    40 */    54,   53,   53,   52,   52,   52,   51,  235,  301,   57,
       108526  + /*    50 */    58,   48,  582,  581,  583,  583,   55,   55,   56,   56,
       108527  + /*    60 */    56,   56,  571,   54,   54,   54,   54,   53,   53,   52,
       108528  + /*    70 */    52,   52,   51,  235,  312,  597,  328,  198,  197,  196,
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,  235,   53,   53,   52,   52,   52,   51,  235,  492,
       108531  + /*   100 */   597,  441,   66,  237,  590,  584,  597,  299,  365,   59,
       108532  + /*   110 */   482,   67,  362,    1,  621,  353,  621,  623,  622,  516,
       108533  + /*   120 */   515,  442,  443,   57,   58,   48,  582,  581,  583,  583,
       108534  + /*   130 */    55,   55,   56,   56,   56,   56,  608,   54,   54,   54,
       108535  + /*   140 */    54,   53,   53,   52,   52,   52,   51,  235,  312,  225,
       108536  + /*   150 */   618,  617,  616,  176,  138,  283,  386,  278,  385,  168,
       108537  + /*   160 */   492,  554,  623,  622,  623,  622,  274,  441,  597,  412,
       108538  + /*   170 */   553,  607,   67,  215,  597,  621,  506,  507,  590,  584,
       108539  + /*   180 */   444,  571,  621,  391,  415,  620,  619,  442,  338,  623,
       108540  + /*   190 */   622,  392,  601,   92,  224,  193,  671,   57,   58,   48,
       108541  + /*   200 */   582,  581,  583,  583,   55,   55,   56,   56,   56,   56,
       108542  + /*   210 */   672,   54,   54,   54,   54,   53,   53,   52,   52,   52,
       108543  + /*   220 */    51,  235,  312,  262,  234,  233,  505,  132,  486,  510,
       108544  + /*   230 */   620,  619,  620,  619,  399,  438,  487,  566,  621,  389,
       108545  + /*   240 */   412,  384,  440,  532,  439,  602,  339,  571,  504,  603,
       108546  + /*   250 */   203,  533,  590,  584,   45,  415,  548,  620,  619,  383,
       108547  + /*   260 */   380,  379,    6,  601,   92,  526,  261,  572,  572,  595,
       108548  + /*   270 */   378,   57,   58,   48,  582,  581,  583,  583,   55,   55,
       108549  + /*   280 */    56,   56,   56,   56,  600,   54,   54,   54,   54,   53,
       108550  + /*   290 */    53,   52,   52,   52,   51,  235,  312,  466,  326,  561,
       108551  + /*   300 */   593,  593,  593,  282,  311,  399,  412,  568,  493,  214,
       108552  + /*   310 */   400,  623,  622,   68,  281,  560,    7,  623,  622,  167,
       108553  + /*   320 */   577,  415,   65,  408,  575,  574,  590,  584,  673,  601,
       108554  + /*   330 */    92,  559,  410,  520,  579,  579,  306,  425,  623,  622,
       108555  + /*   340 */   208,  572,  572,  467,  519,   57,   58,   48,  582,  581,
       108556  + /*   350 */   583,  583,   55,   55,   56,   56,   56,   56,  576,   54,
       108557  + /*   360 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  235,
       108558  + /*   370 */   312,  399,  412,  317,  600,  412,  350,  549,  578,  620,
       108559  + /*   380 */   619,  423,  204,  175,  571,  620,  619,  415,  551,  355,
       108560  + /*   390 */   415,  257,  349,  256,  174,  601,   74,  354,  601,   91,
       108561  + /*   400 */   590,  584,  237,  467,  621,  547,  620,  619,  164,  623,
       108562  + /*   410 */   622,  544,   62,   39,  251,  580,  600,  348,  555,   57,
       108563  + /*   420 */    58,   48,  582,  581,  583,  583,   55,   55,   56,   56,
       108564  + /*   430 */    56,   56,  565,   54,   54,   54,   54,   53,   53,   52,
       108565  + /*   440 */    52,   52,   51,  235,  312,  554,  182,  145,  531,  292,
       108566  + /*   450 */   231,  257,  349,  256,  553,  221,  412,  181,  248,  549,
       108567  + /*   460 */   250,  412,  237,   50,   47,  146,  412,  410,  412,  579,
       108568  + /*   470 */   579,  415,  549,  567,  590,  584,  415,  620,  619,  601,
       108569  + /*   480 */    16,  415,  189,  415,  601,   92,  268,   36,  544,  601,
       108570  + /*   490 */    92,  601,   73,   57,   58,   48,  582,  581,  583,  583,
       108571  + /*   500 */    55,   55,   56,   56,   56,   56,  187,   54,   54,   54,
       108572  + /*   510 */    54,   53,   53,   52,   52,   52,   51,  235,  312,  564,
       108573  + /*   520 */   397,  410,  528,  579,  579,  561,  395,  535,  201,  145,
       108574  + /*   530 */   264,  320,  266,  388,  375,  410,  249,  579,  579,  236,
       108575  + /*   540 */    40,  560,   38,   50,   47,  146,   19,  606,  590,  584,
       108576  + /*   550 */   422,  948,  563,  948,  549,  496,  472,  559,  602,  403,
       108577  + /*   560 */   422,  947,  603,  947,  562,  167,  200,   57,   58,   48,
       108578  + /*   570 */   582,  581,  583,  583,   55,   55,   56,   56,   56,   56,
       108579  + /*   580 */   156,   54,   54,   54,   54,   53,   53,   52,   52,   52,
       108580  + /*   590 */    51,  235,  312,  262,  412,  426,  511,  208,  347,  492,
       108581  + /*   600 */   600,   50,   47,  146,  410,  426,  579,  579,  621,  415,
       108582  + /*   610 */   430,   67,  199,  143,  621,  166,  474,  601,   69,  317,
       108583  + /*   620 */   475,  435,  590,  584,  274,  234,  233,  958,  184,  420,
       108584  + /*   630 */     2,  600,  358,  552,  366,  470,  336,  476,  476,  368,
       108585  + /*   640 */   359,   57,   58,   48,  582,  581,  583,  583,   55,   55,
       108586  + /*   650 */    56,   56,   56,   56,  354,   54,   54,   54,   54,   53,
       108587  + /*   660 */    53,   52,   52,   52,   51,  235,  312,  412,  572,  572,
       108588  + /*   670 */   271,  412,  558,  874,  361,  213,  143,  412,   50,   47,
       108589  + /*   680 */   146,   30,  415,  540,  355,  334,  415,  412,  629,    2,
       108590  + /*   690 */   601,   94,  415,  513,  601,   97,  590,  584,  621,  621,
       108591  + /*   700 */   601,   95,  415,  306,  424,  630,  628,  332,  173,  269,
       108592  + /*   710 */   601,  104,  171,  546,  597,   57,   58,   48,  582,  581,
       108593  + /*   720 */   583,  583,   55,   55,   56,   56,   56,   56,  412,   54,
       108594  + /*   730 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  235,
       108595  + /*   740 */   312,  412,  275,  415,  412,   22,  329,  340,  423,  544,
       108596  + /*   750 */   217,  601,  103,  568,  407,  214,  415,  621,   35,  415,
       108597  + /*   760 */   412,  540,  412,   12,  601,  105,  237,  601,  106,   11,
       108598  + /*   770 */   590,  584,   50,   47,  146,  415,  621,  415,  353,  325,
       108599  + /*   780 */   164,  129,  597,  601,  133,  601,  134,  207,  287,   57,
       108600  + /*   790 */    58,   48,  582,  581,  583,  583,   55,   55,   56,   56,
       108601  + /*   800 */    56,   56,  412,   54,   54,   54,   54,   53,   53,   52,
       108602  + /*   810 */    52,   52,   51,  235,  312,  412,  275,  415,  412,  275,
       108603  + /*   820 */   275,  600,  275,  544,  367,  601,   98,  542,  412,  170,
       108604  + /*   830 */   415,  621,  524,  415,  621,  621,  169,  621,  601,  102,
       108605  + /*   840 */   604,  601,  101,  415,  590,  584,  352,   21,  500,   32,
       108606  + /*   850 */   166,  601,   93,  223,  318,  147,  324,  323,  272,  322,
       108607  + /*   860 */   166,  205,  285,   57,   58,   48,  582,  581,  583,  583,
       108608  + /*   870 */    55,   55,   56,   56,   56,   56,  412,   54,   54,   54,
       108609  + /*   880 */    54,   53,   53,   52,   52,   52,   51,  235,  312,  412,
       108610  + /*   890 */   262,  415,  412,  538,  360,  600,  360,  202,  142,  601,
       108611  + /*   900 */   100,  376,  412,  166,  415,  621,  540,  415,  360,  621,
       108612  + /*   910 */   141,  621,  601,   77,  230,  601,   96,  415,  590,  584,
       108613  + /*   920 */   341,  621,  530,  621,  529,  601,  137,  471,  382,  166,
       108614  + /*   930 */   460,  600,  170,  453,  427,  307,   28,   57,   58,   48,
       108615  + /*   940 */   582,  581,  583,  583,   55,   55,   56,   56,   56,   56,
       108616  + /*   950 */   412,   54,   54,   54,   54,   53,   53,   52,   52,   52,
       108617  + /*   960 */    51,  235,  312,  412,  503,  415,  412,  212,  459,  321,
       108618  + /*   970 */    35,  211,  127,  601,  136,  428,  412,  308,  415,  412,
       108619  + /*   980 */   273,  415,  222,  126,  255,   27,  601,  135,  377,  601,
       108620  + /*   990 */    76,  415,  590,  584,  415,  262,  220,   51,  235,  601,
       108621  + /*  1000 */    90,  600,  601,   89,  125,  600,  281,  522,  521,  373,
       108622  + /*  1010 */   621,   57,   46,   48,  582,  581,  583,  583,   55,   55,
       108623  + /*  1020 */    56,   56,   56,   56,  412,   54,   54,   54,   54,   53,
       108624  + /*  1030 */    53,   52,   52,   52,   51,  235,  312,  412,  451,  415,
       108625  + /*  1040 */   165,  624,  612,  611,  469,  521,  480,  601,   75,  185,
       108626  + /*  1050 */   412,  122,  415,  412,  369,  412,  621,  621,  621,  477,
       108627  + /*  1060 */   601,   88,  589,  588,  170,  415,  590,  584,  415,  121,
       108628  + /*  1070 */   415,  473,  259,  601,   87,  159,  601,   99,  601,   86,
       108629  + /*  1080 */   628,  332,  120,   14,  586,  585,   58,   48,  582,  581,
       108630  + /*  1090 */   583,  583,   55,   55,   56,   56,   56,   56,  412,   54,
       108631  + /*  1100 */    54,   54,   54,   53,   53,   52,   52,   52,   51,  235,
       108632  + /*  1110 */   312,  587,  412,  415,  412,  465,  262,  262,  610,  605,
       108633  + /*  1120 */   158,  601,   17,  157,  412,  462,   25,  415,  118,  415,
       108634  + /*  1130 */   412,  621,  621,  621,  621,  601,   85,  601,   84,  415,
       108635  + /*  1140 */   590,  584,  411,  262,   24,  415,  117,  601,   83,   10,
       108636  + /*  1150 */   351,    4,  115,  601,   72,  458,  456,  621,  621,  450,
       108637  + /*  1160 */   449,   48,  582,  581,  583,  583,   55,   55,   56,   56,
       108638  + /*  1170 */    56,   56,  253,   54,   54,   54,   54,   53,   53,   52,
       108639  + /*  1180 */    52,   52,   51,  235,   44,  406,  219,    3,  412,  452,
       108640  + /*  1190 */   262,  416,  622,  154,  594,  110,  107,  108,  401,  446,
       108641  + /*  1200 */    64,  557,  409,  415,  412,  621,  437,   44,  406,  621,
       108642  + /*  1210 */     3,  601,   71,  621,  416,  622,  621,  545,  335,  415,
       108643  + /*  1220 */   436,  404,  149,  239,  262,  409,  188,  601,   82,  331,
       108644  + /*  1230 */   541,  566,  621,  218,  421,  539,  229,  139,  124,  621,
       108645  + /*  1240 */   148,  419,  626,  625,  404,  621,  412,  238,  315,  123,
       108646  + /*  1250 */   621,   41,   42,  534,  566,  206,  412,  277,   43,  414,
       108647  + /*  1260 */   413,  415,  509,  595,  314,  615,  180,  245,  614,  601,
       108648  + /*  1270 */    81,  415,  621,  276,   41,   42,  613,  621,  412,  601,
       108649  + /*  1280 */    80,   43,  414,  413,  179,  270,  595,  418,  621,  600,
       108650  + /*  1290 */   484,    8,  177,  415,  593,  593,  593,  592,  591,   13,
       108651  + /*  1300 */   621,  601,   70,  417,  237,  621,   44,  406,  267,    3,
       108652  + /*  1310 */   237,  265,  342,  416,  622,  599,  296,  593,  593,  593,
       108653  + /*  1320 */   592,  591,   13,  621,  409,  412,  621,  412,  396,   34,
       108654  + /*  1330 */   406,  412,    3,   31,   30,  294,  416,  622,  293,  333,
       108655  + /*  1340 */   415,  263,  415,  404,  398,  258,  415,  409,  601,   18,
       108656  + /*  1350 */   601,   79,  363,  566,  601,   78,  621,  295,   60,   37,
       108657  + /*  1360 */   621,  172,  464,  390,  463,  598,  404,  621,  356,  288,
       108658  + /*  1370 */   232,   29,  556,   41,   42,  252,  566,  621,  415,  621,
       108659  + /*  1380 */    43,  414,  413,  621,  455,  595,  601,    9,  183,   61,
       108660  + /*  1390 */   621,  543,  445,  623,  622,  244,   41,   42,  241,  621,
       108661  + /*  1400 */   240,  393,  286,   43,  414,  413,  284,  621,  595,  431,
       108662  + /*  1410 */   621,  291,  537,  621,  536,  621,  593,  593,  593,  592,
       108663  + /*  1420 */   591,   13,  216,  144,  621,  387,  621,  304,  303,  302,
       108664  + /*  1430 */   178,  300,  394,  566,  454,  517,  290,  523,  621,  593,
       108665  + /*  1440 */   593,  593,  592,  591,   13,  518,  327,  621,  150,  279,
       108666  + /*  1450 */   243,  621,  514,  192,  191,  513,  512,  310,  128,  242,
       108667  + /*  1460 */   228,  526,  227,  226,  490,  595,  494,  489,  488,  309,
       108668  + /*  1470 */   194,  164,  152,  163,  467,  374,  481,  372,  153,  162,
       108669  + /*  1480 */   161,  151,  210,  479,  260,  370,   26,  478,  160,  468,
       108670  + /*  1490 */   364,  140,  119,  116,  131,  457,  593,  593,  593,  155,
       108671  + /*  1500 */   114,  346,  345,  113,  112,  111,  448,  109,  130,   23,
       108672  + /*  1510 */   319,   20,  434,  433,  432,   63,  429,  609,  190,  510,
       108673  + /*  1520 */   596,  405,  257,  344,  246,  297,  280,  573,  313,  570,
       108674  + /*  1530 */   195,  499,  498,  237,  497,  495,  381,  289,  461,  247,
       108675  + /*  1540 */   357,  254,  305,  343,    5,  447,  569,  209,  550,  371,
       108676  + /*  1550 */   508,  491,  402,  502,  501,  298,  337,  527,  525,  485,
       108677  + /*  1560 */   483,  235,
108241 108678   };
108242 108679   static const YYCODETYPE yy_lookahead[] = {
108243 108680    /*     0 */    19,  169,  170,  171,   22,   24,   24,   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,   88,   89,   90,   91,   92,    1,
       108683  + /*    30 */    49,   50,   77,   78,   79,   80,  155,   82,   83,   84,
       108684  + /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,   15,   68,
108248 108685    /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
108249 108686    /*    60 */    79,   80,  230,   82,   83,   84,   85,   86,   87,   88,
108250         - /*    70 */    89,   90,   91,   92,   19,   94,   19,   23,   86,   87,
       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,
108256         - /*   130 */    75,   76,   77,   78,   79,   80,   22,   82,   83,   84,
       108689  + /*    90 */    91,   92,   86,   87,   88,   89,   90,   91,   92,  150,
       108690  + /*   100 */    26,  150,   22,  116,   49,   50,   26,  158,  227,   54,
       108691  + /*   110 */   161,  162,  231,   22,  165,  128,  165,   26,   27,    7,
       108692  + /*   120 */     8,  170,  171,   68,   69,   70,   71,   72,   73,   74,
       108693  + /*   130 */    75,   76,   77,   78,   79,   80,   23,   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 */     7,    8,    9,   96,   97,   98,   99,  100,  101,  102,
       108696  + /*   160 */   150,   32,   26,   27,   26,   27,  109,  150,   94,  150,
       108697  + /*   170 */    41,  161,  162,   22,   94,  165,   97,   98,   49,   50,
       108698  + /*   180 */   229,  230,  165,   19,  165,   94,   95,  170,  171,   26,
       108699  + /*   190 */    27,   27,  173,  174,  184,  185,  118,   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,  150,   86,   87,   36,   24,  180,  181,
       108703  + /*   230 */    94,   95,   94,   95,  215,   97,  188,   66,  165,  220,
       108704  + /*   240 */   150,   51,  104,   23,  106,  113,  229,  230,   58,  117,
       108705  + /*   250 */   160,   88,   49,   50,   22,  165,  120,   94,   95,   99,
       108706  + /*   260 */   100,  101,   22,  173,  174,   94,  193,  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,  108,   12,
       108710  + /*   300 */   129,  130,  131,   98,  163,  215,  150,  166,  167,  168,
       108711  + /*   310 */   220,   26,   27,   22,  109,   28,   76,   26,   27,   50,
       108712  + /*   320 */    23,  165,   25,  169,  170,  171,   49,   50,  118,  173,
       108713  + /*   330 */   174,   44,  112,   46,  114,  115,   22,   23,   26,   27,
       108714  + /*   340 */   160,  129,  130,   57,   57,   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,  104,  194,  150,  220,   25,   23,   94,
       108718  + /*   380 */    95,   67,  160,  118,  230,   94,   95,  165,   25,  150,
       108719  + /*   390 */   165,  105,  106,  107,  118,  173,  174,  217,  173,  174,
       108720  + /*   400 */    49,   50,  116,   57,  165,  120,   94,   95,   96,   26,
       108721  + /*   410 */    27,  166,  232,  136,   16,  113,  194,  237,  176,   68,
108285 108722    /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
108286 108723    /*   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,
       108724  + /*   440 */    89,   90,   91,   92,   19,   32,   23,   95,   23,  224,
       108725  + /*   450 */   205,  105,  106,  107,   41,  216,  150,   23,   60,   25,
       108726  + /*   460 */    62,  150,  116,  221,  222,  223,  150,  112,  150,  114,
       108727  + /*   470 */   115,  165,  120,   23,   49,   50,  165,   94,   95,  173,
       108728  + /*   480 */   174,  165,  119,  165,  173,  174,   16,  136,  166,  173,
       108729  + /*   490 */   174,  173,  174,   68,   69,   70,   71,   72,   73,   74,
       108730  + /*   500 */    75,   76,   77,   78,   79,   80,   22,   82,   83,   84,
       108731  + /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   11,
       108732  + /*   520 */   214,  112,   23,  114,  115,   12,  215,  205,   22,   95,
       108733  + /*   530 */    60,  215,   62,   88,   19,  112,  138,  114,  115,  197,
       108734  + /*   540 */   135,   28,  137,  221,  222,  223,  204,  172,   49,   50,
       108735  + /*   550 */    22,   23,   23,   25,  120,  180,   21,   44,  113,   46,
       108736  + /*   560 */    22,   23,  117,   25,   23,   50,  160,   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 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
       108739  + /*   590 */    91,   92,   19,  150,  150,   67,   23,  160,   63,  150,
       108740  + /*   600 */   194,  221,  222,  223,  112,   67,  114,  115,  165,  165,
       108741  + /*   610 */   161,  162,  206,  207,  165,   25,   30,  173,  174,  104,
       108742  + /*   620 */    34,  241,   49,   50,  109,   86,   87,  142,  143,  144,
       108743  + /*   630 */   145,  194,   19,  176,   48,  100,  193,  105,  106,  107,
       108744  + /*   640 */    27,   68,   69,   70,   71,   72,   73,   74,   75,   76,
       108745  + /*   650 */    77,   78,   79,   80,  217,   82,   83,   84,   85,   86,
       108746  + /*   660 */    87,   88,   89,   90,   91,   92,   19,  150,  129,  130,
       108747  + /*   670 */    23,  150,   23,  138,  237,  206,  207,  150,  221,  222,
       108748  + /*   680 */   223,  126,  165,  150,  150,  242,  165,  150,  144,  145,
       108749  + /*   690 */   173,  174,  165,  103,  173,  174,   49,   50,  165,  165,
       108750  + /*   700 */   173,  174,  165,   22,   23,    0,    1,    2,   25,   25,
       108751  + /*   710 */   173,  174,   24,  120,   26,   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,  150,  165,  150,   24,  213,   97,   67,  166,
       108755  + /*   750 */   216,  173,  174,  166,  167,  168,  165,  165,   25,  165,
       108756  + /*   760 */   150,  150,  150,   35,  173,  174,  116,  173,  174,   35,
       108757  + /*   770 */    49,   50,  221,  222,  223,  165,  165,  165,  128,  187,
       108758  + /*   780 */    96,   22,   94,  173,  174,  173,  174,  160,  205,   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,  150,  165,  150,  150,
       108762  + /*   820 */   150,  194,  150,  166,  213,  173,  174,   23,  150,   25,
       108763  + /*   830 */   165,  165,  165,  165,  165,  165,   35,  165,  173,  174,
       108764  + /*   840 */   173,  173,  174,  165,   49,   50,  219,   52,   23,   25,
       108765  + /*   850 */    25,  173,  174,  187,  245,  246,  187,  187,   23,  187,
       108766  + /*   860 */    25,  160,  205,   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 */   150,  165,  150,   27,  150,  194,  150,  160,  118,  173,
       108770  + /*   900 */   174,   23,  150,   25,  165,  165,  150,  165,  150,  165,
       108771  + /*   910 */    39,  165,  173,  174,   52,  173,  174,  165,   49,   50,
       108772  + /*   920 */   219,  165,   23,  165,   23,  173,  174,   23,   52,   25,
       108773  + /*   930 */    23,  194,   25,  193,  247,  248,   22,   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,   29,  165,  150,  160,   23,  213,
       108777  + /*   970 */    25,  160,   22,  173,  174,   23,  150,   25,  165,  150,
       108778  + /*   980 */    23,  165,  238,   22,  238,   22,  173,  174,   52,  173,
       108779  + /*   990 */   174,  165,   49,   50,  165,  150,  238,   91,   92,  173,
       108780  + /*  1000 */   174,  194,  173,  174,   22,  194,  109,  190,  191,   19,
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,  150,  150,  150,  190,  191,   20,  173,  174,   24,
       108785  + /*  1050 */   150,  104,  165,  150,   43,  150,  165,  165,  165,   59,
       108786  + /*  1060 */   173,  174,   49,   50,   25,  165,   49,   50,  165,   53,
       108787  + /*  1070 */   165,   53,  138,  173,  174,  104,  173,  174,  173,  174,
       108788  + /*  1080 */     1,    2,   22,    5,   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,    1,  150,  150,  150,  150,
       108792  + /*  1120 */   118,  173,  174,   35,  150,   27,   76,  165,  108,  165,
       108793  + /*  1130 */   150,  165,  165,  165,  165,  173,  174,  173,  174,  165,
       108794  + /*  1140 */    49,   50,  150,  150,   76,  165,  127,  173,  174,   22,
       108795  + /*  1150 */    25,   22,  119,  173,  174,   23,    1,  165,  165,  193,
       108796  + /*  1160 */   193,   70,   71,   72,   73,   74,   75,   76,   77,   78,
       108797  + /*  1170 */    79,   80,   16,   82,   83,   84,   85,   86,   87,   88,
       108798  + /*  1180 */    89,   90,   91,   92,   19,   20,  193,   22,  150,   20,
       108799  + /*  1190 */   150,   26,   27,  121,  150,  119,  127,  108,  150,  128,
       108800  + /*  1200 */    16,  150,   37,  165,  150,  165,   23,   19,   20,  165,
       108801  + /*  1210 */    22,  173,  174,  165,   26,   27,  165,  150,   65,  165,
       108802  + /*  1220 */    23,   56,   15,  140,  150,   37,   22,  173,  174,    3,
       108803  + /*  1230 */   150,   66,  165,  193,    4,  150,  179,  164,  179,  165,
       108804  + /*  1240 */   246,  146,  147,  149,   56,  165,  150,  152,  249,  154,
       108805  + /*  1250 */   165,   86,   87,   88,   66,  160,  150,  150,   93,   94,
       108806  + /*  1260 */    95,  165,  150,   98,  249,  149,    6,  193,  149,  173,
       108807  + /*  1270 */   174,  165,  165,  150,   86,   87,   13,  165,  150,  173,
       108808  + /*  1280 */   174,   93,   94,   95,  151,  150,   98,  149,  165,  194,
       108809  + /*  1290 */   150,   25,  151,  165,  129,  130,  131,  132,  133,  134,
       108810  + /*  1300 */   165,  173,  174,  159,  116,  165,   19,   20,  150,   22,
       108811  + /*  1310 */   116,  150,  217,   26,   27,  194,  199,  129,  130,  131,
       108812  + /*  1320 */   132,  133,  134,  165,   37,  150,  165,  150,  123,   19,
       108813  + /*  1330 */    20,  150,   22,  124,  126,  201,   26,   27,  202,  244,
       108814  + /*  1340 */   165,  150,  165,   56,  122,  150,  165,   37,  173,  174,
       108815  + /*  1350 */   173,  174,  150,   66,  173,  174,  165,  200,  125,  135,
       108816  + /*  1360 */   165,  118,  150,  150,  150,  203,   56,  165,  150,  210,
       108817  + /*  1370 */   225,  104,  157,   86,   87,  150,   66,  165,  165,  165,
       108818  + /*  1380 */    93,   94,   95,  165,  150,   98,  173,  174,  157,   22,
       108819  + /*  1390 */   165,  211,  150,   26,   27,  150,   86,   87,  150,  165,
       108820  + /*  1400 */   150,  121,  210,   93,   94,   95,  210,  165,   98,  150,
       108821  + /*  1410 */   165,  150,  211,  165,  211,  165,  129,  130,  131,  132,
       108822  + /*  1420 */   133,  134,    5,  150,  165,  104,  165,   10,   11,   12,
       108823  + /*  1430 */    13,   14,  150,   66,   17,  183,  150,  175,  165,  129,
       108824  + /*  1440 */   130,  131,  132,  133,  134,  175,   47,  165,   31,  175,
       108825  + /*  1450 */    33,  165,  177,   86,   87,  103,  175,  178,   22,   42,
       108826  + /*  1460 */   228,   94,   92,  228,  175,   98,  183,  175,  175,  178,
       108827  + /*  1470 */   185,   96,   55,  156,   57,   18,  157,  157,   61,  156,
       108828  + /*  1480 */   156,   64,  157,  157,  235,   45,  135,  236,  156,  189,
       108829  + /*  1490 */   157,   68,  189,   22,  218,  199,  129,  130,  131,  156,
       108830  + /*  1500 */   192,  157,   18,  192,  192,  192,  199,  189,  218,  240,
       108831  + /*  1510 */   157,  240,   40,  157,  157,  243,   38,  153,  196,  181,
       108832  + /*  1520 */   166,  226,  105,  106,  107,  198,  176,  230,  111,  230,
       108833  + /*  1530 */   185,  176,  166,  116,  176,  166,  177,  209,  199,  209,
       108834  + /*  1540 */   239,  239,  148,  209,  196,  199,  166,  233,  208,  234,
       108835  + /*  1550 */   182,  186,  191,  182,  182,  195,  139,  173,  173,  186,
       108836  + /*  1560 */   186,   92,
       108837  +};
       108838  +#define YY_SHIFT_USE_DFLT (-70)
       108839  +#define YY_SHIFT_COUNT (419)
       108840  +#define YY_SHIFT_MIN   (-69)
       108841  +#define YY_SHIFT_MAX   (1484)
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 */  1079, 1188, 1417, 1188, 1287, 1287,  138,  138,  312,  -19,
       108844  + /*    10 */  1287, 1287, 1287, 1287,  346,  383,  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,  383,  383,  539,  539,   74, 1194,   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,    6,  -64,  383,  383,  383,
       108854  + /*   110 */   383,  383,  383,  383,  383,  383,  383,  383,  383,  383,
       108855  + /*   120 */   383,  383,  383,  535,  515,  383,  383,  383,  383,  383,
       108856  + /*   130 */   650,  -13,   74,  906, 1469,  -70,  -70,  -70, 1367,   57,
       108857  + /*   140 */   287,  287,  163,  291,  285,  136,   91,  538,  528,  383,
       108858  + /*   150 */   383,  383,  383,  383,  383,  383,  383,  383,  383,  383,
       108859  + /*   160 */   383,  383,  383,  383,  383,  383,  383,  383,  383,  383,
       108860  + /*   170 */   383,  383,  383,  383,  383,  383,  383,  383,  383,  383,
       108861  + /*   180 */   383,  688,  688,  688,  705, 1194, 1194, 1194,  -70,  -70,
       108862  + /*   190 */   -70,  171,  171,  160,  160,  160,  190,  190,  190,  434,
       108863  + /*   200 */   423,  513,  409,  355,  220,  492,  492,  492,  492,  532,
       108864  + /*   210 */   586,  492,  492,  352,   80,  445,  143,  555,  164,  164,
       108865  + /*   220 */   613,  555,  613,  590,  684,   74,  413,   74,  413,  269,
       108866  + /*   230 */   413,  164,  405,  212,  212,   74,  363,  132,  -18, 1478,
       108867  + /*   240 */  1243, 1243, 1472, 1472, 1243, 1471, 1423, 1208, 1484, 1484,
       108868  + /*   250 */  1484, 1484, 1243, 1457, 1208, 1471, 1423, 1423, 1243, 1457,
       108869  + /*   260 */  1351, 1440, 1243, 1243, 1457, 1243, 1457, 1243, 1457, 1375,
       108870  + /*   270 */  1436, 1321, 1321, 1321, 1399, 1370, 1370, 1436, 1321, 1352,
       108871  + /*   280 */  1321, 1399, 1321, 1321, 1280, 1267, 1280, 1267, 1280, 1267,
       108872  + /*   290 */  1243, 1243, 1224, 1233, 1222, 1209, 1205, 1208, 1194, 1266,
       108873  + /*   300 */  1263, 1263, 1260, 1260, 1260, 1260,  -70,  -70,  -70,  -70,
       108874  + /*   310 */   -70,  -70, 1013,  398,  681,  314,  470,  -28,  952,  240,
       108875  + /*   320 */   945,  907,  904,  878,  835,  825,   79,  112,  205,  804,
       108876  + /*   330 */   297, 1230, 1226, 1204, 1083, 1207, 1153, 1184, 1197, 1183,
       108877  + /*   340 */  1071, 1125, 1069, 1089, 1076, 1169, 1072, 1156, 1155, 1033,
       108878  + /*   350 */  1132, 1129, 1125, 1127, 1019, 1068, 1050, 1020, 1098, 1088,
       108879  + /*   360 */  1002, 1114, 1078, 1060,  971,  934, 1018, 1039, 1016, 1000,
       108880  + /*   370 */  1011,  947, 1025, 1026,  990,  897,  938,  982,  936,  963,
       108881  + /*   380 */   961,  957,  950,  876,  935,  914,  862,  871,  901,  899,
       108882  + /*   390 */   780,  866,  801,  759,  593,  733,  734,  824,  728,  733,
       108883  + /*   400 */   649,  541,  683,  529,  506,  508,  484,  450,  335,  232,
       108884  + /*   410 */   302,  276,  265,  210,   92,   78,  151,  113,   33,   28,
108449 108885   };
108450 108886   #define YY_REDUCE_USE_DFLT (-169)
108451         -#define YY_REDUCE_COUNT (309)
       108887  +#define YY_REDUCE_COUNT (311)
108452 108888   #define YY_REDUCE_MIN   (-168)
108453         -#define YY_REDUCE_MAX   (1411)
       108889  +#define YY_REDUCE_MAX   (1394)
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 */   485,   90, 1095,  222,  156,   19,   17,  -49,   10,  322,
       108892  + /*    10 */   316,  311,  306,  225,  180,  -51,  457,  242,  380, 1213,
       108893  + /*    20 */  1181, 1177, 1175, 1128, 1106, 1096, 1054, 1038,  980,  974,
       108894  + /*    30 */   964,  962,  948,  905,  903,  900,  887,  874,  829,  826,
       108895  + /*    40 */   816,  813,  800,  752,  742,  739,  726,  678,  668,  665,
       108896  + /*    50 */   652,  612,  610,  594,  591,  578,  537,  527,  521,  517,
       108897  + /*    60 */   444,  318,  437,  449,  443,  154, -168,  141,  406,  551,
       108898  + /*    70 */   551,  551,  551,  551,  551,  551,  551,  551,  551,  551,
       108899  + /*    80 */   551,  551,  551,  551,  551,  551,  551,  551,  551,  551,
       108900  + /*    90 */   551,  551,  551,  551,  551,  551,  551,  551,  551,  551,
       108901  + /*   100 */   551,  551,  551,  551,  551,  551,  551, 1074,  534, 1040,
       108902  + /*   110 */   993,  967,  966,  845,  740,  758,  756,  746,  239,  744,
       108903  + /*   120 */   672,  611,   73, -119,   48,  670,  669,  666,  592,  533,
       108904  + /*   130 */   701,  627,  587,  551,  551,  551,  551,  551,  667,  375,
       108905  + /*   140 */   854,  817,  992, 1286, 1282, 1273, 1261,  687,  687, 1259,
       108906  + /*   150 */  1250, 1248, 1245, 1242, 1234, 1225, 1218, 1214, 1212, 1202,
       108907  + /*   160 */  1195, 1191, 1161, 1158, 1140, 1135, 1123, 1112, 1107, 1085,
       108908  + /*   170 */  1080, 1067, 1051, 1048, 1044,  992,  969,  968,  893,  892,
       108909  + /*   180 */   891,  657,  583,  245,  544,  811,  807,  737,  609,  469,
       108910  + /*   190 */   342, 1385, 1384, 1374, 1373, 1365, 1372, 1371, 1368, 1340,
       108911  + /*   200 */  1360, 1361, 1360, 1360, 1360, 1360, 1360, 1360, 1360, 1315,
       108912  + /*   210 */  1314, 1360, 1360, 1340, 1380, 1348, 1394, 1346, 1334, 1330,
       108913  + /*   220 */  1302, 1339, 1301, 1359, 1345, 1369, 1358, 1366, 1355, 1338,
       108914  + /*   230 */  1350, 1328, 1295, 1299, 1297, 1354, 1327, 1322, 1364, 1272,
       108915  + /*   240 */  1357, 1356, 1271, 1269, 1353, 1290, 1318, 1307, 1313, 1312,
       108916  + /*   250 */  1311, 1308, 1344, 1343, 1296, 1276, 1303, 1300, 1333, 1332,
       108917  + /*   260 */  1251, 1249, 1326, 1325, 1324, 1320, 1323, 1319, 1317, 1285,
       108918  + /*   270 */  1291, 1293, 1292, 1289, 1283, 1235, 1232, 1279, 1281, 1275,
       108919  + /*   280 */  1274, 1252, 1270, 1262, 1203, 1196, 1201, 1192, 1180, 1159,
       108920  + /*   290 */  1231, 1215, 1145, 1162, 1136, 1134, 1157, 1117, 1121, 1144,
       108921  + /*   300 */  1141, 1133, 1138, 1119, 1116, 1094, 1015,  999,  994, 1059,
       108922  + /*   310 */  1057, 1073,
108486 108923   };
108487 108924   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,
       108925  + /*     0 */   635,  869,  957,  957,  869,  869,  957,  957,  722,  759,
       108926  + /*    10 */   957,  957,  957,  867,  957,  957,  787,  787,  931,  957,
       108927  + /*    20 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108928  + /*    30 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108929  + /*    40 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108930  + /*    50 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108931  + /*    60 */   957,  957,  957,  957,  957,  957,  957,  674,  763,  793,
       108932  + /*    70 */   957,  957,  957,  957,  957,  957,  957,  957,  930,  932,
       108933  + /*    80 */   801,  800,  910,  774,  798,  791,  795,  870,  863,  864,
       108934  + /*    90 */   862,  866,  871,  957,  794,  830,  847,  829,  841,  846,
       108935  + /*   100 */   853,  845,  842,  832,  831,  833,  834,  957,  957,  957,
       108936  + /*   110 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108937  + /*   120 */   957,  957,  957,  661,  728,  957,  957,  957,  957,  957,
       108938  + /*   130 */   957,  957,  957,  835,  836,  850,  849,  848,  957,  666,
       108939  + /*   140 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108940  + /*   150 */   937,  935,  957,  882,  957,  957,  957,  957,  957,  957,
       108941  + /*   160 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108942  + /*   170 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108943  + /*   180 */   641,  759,  759,  759,  635,  957,  957,  957,  949,  763,
       108944  + /*   190 */   753,  957,  957,  957,  957,  718,  957,  957,  957,  957,
       108945  + /*   200 */   957,  957,  957,  957,  957,  803,  742,  920,  922,  957,
       108946  + /*   210 */   903,  740,  663,  761,  676,  751,  643,  797,  776,  776,
       108947  + /*   220 */   915,  797,  915,  699,  722,  957,  787,  957,  787,  696,
       108948  + /*   230 */   787,  776,  865,  957,  957,  957,  760,  751,  957,  942,
       108949  + /*   240 */   767,  767,  934,  934,  767,  809,  732,  797,  739,  739,
       108950  + /*   250 */   739,  739,  767,  658,  797,  809,  732,  732,  767,  658,
       108951  + /*   260 */   909,  907,  767,  767,  658,  767,  658,  767,  658,  722,
       108952  + /*   270 */   875,  730,  730,  730,  714,  879,  879,  875,  730,  699,
       108953  + /*   280 */   730,  714,  730,  730,  780,  775,  780,  775,  780,  775,
       108954  + /*   290 */   767,  767,  957,  792,  781,  790,  788,  797,  957,  717,
       108955  + /*   300 */   651,  651,  640,  640,  640,  640,  954,  954,  949,  701,
       108956  + /*   310 */   701,  684,  957,  957,  957,  957,  957,  957,  957,  884,
       108957  + /*   320 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108958  + /*   330 */   957,  957,  636,  944,  957,  957,  941,  957,  957,  957,
       108959  + /*   340 */   957,  802,  957,  957,  957,  957,  957,  957,  957,  957,
       108960  + /*   350 */   957,  957,  919,  957,  957,  957,  957,  957,  957,  957,
       108961  + /*   360 */   913,  957,  957,  957,  957,  957,  957,  906,  905,  957,
       108962  + /*   370 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108963  + /*   380 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108964  + /*   390 */   957,  957,  957,  957,  957,  789,  957,  782,  957,  868,
       108965  + /*   400 */   957,  957,  957,  957,  957,  957,  957,  957,  957,  957,
       108966  + /*   410 */   745,  818,  957,  817,  821,  816,  668,  957,  649,  957,
       108967  + /*   420 */   632,  637,  953,  956,  955,  952,  951,  950,  945,  943,
       108968  + /*   430 */   940,  939,  938,  936,  933,  929,  888,  886,  893,  892,
       108969  + /*   440 */   891,  890,  889,  887,  885,  883,  804,  799,  796,  928,
       108970  + /*   450 */   881,  741,  738,  737,  657,  946,  912,  921,  808,  807,
       108971  + /*   460 */   810,  918,  917,  916,  914,  911,  898,  806,  805,  733,
       108972  + /*   470 */   873,  872,  660,  902,  901,  900,  904,  908,  899,  769,
       108973  + /*   480 */   659,  656,  665,  721,  723,  719,  729,  727,  726,  725,
       108974  + /*   490 */   724,  720,  667,  675,  713,  698,  697,  878,  880,  877,
       108975  + /*   500 */   876,  706,  705,  711,  710,  709,  708,  707,  704,  703,
       108976  + /*   510 */   702,  695,  694,  700,  693,  716,  715,  712,  692,  736,
       108977  + /*   520 */   735,  734,  731,  691,  690,  689,  821,  688,  687,  827,
       108978  + /*   530 */   826,  814,  857,  756,  755,  754,  766,  765,  778,  777,
       108979  + /*   540 */   812,  811,  779,  764,  758,  757,  773,  772,  771,  770,
       108980  + /*   550 */   762,  752,  784,  786,  785,  783,  859,  768,  856,  927,
       108981  + /*   560 */   926,  925,  924,  923,  861,  860,  828,  825,  679,  680,
       108982  + /*   570 */   896,  895,  897,  894,  682,  681,  678,  677,  858,  747,
       108983  + /*   580 */   746,  854,  851,  843,  839,  855,  852,  844,  840,  838,
       108984  + /*   590 */   837,  823,  822,  820,  819,  815,  824,  670,  748,  744,
       108985  + /*   600 */   743,  813,  750,  749,  686,  685,  683,  664,  662,  655,
       108986  + /*   610 */   653,  652,  654,  650,  648,  647,  646,  645,  644,  673,
       108987  + /*   620 */   672,  671,  669,  668,  642,  639,  638,  634,  633,  631,
108551 108988   };
108552 108989   
108553 108990   /* The next table maps tokens into fallback tokens.  If a construct
108554 108991   ** like the following:
108555 108992   ** 
108556 108993   **      %fallback ID X Y Z.
108557 108994   **
................................................................................
108746 109183     "ecmd",          "explain",       "cmdx",          "cmd",         
108747 109184     "transtype",     "trans_opt",     "nm",            "savepoint_opt",
108748 109185     "create_table",  "create_table_args",  "createkw",      "temp",        
108749 109186     "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
108750 109187     "select",        "column",        "columnid",      "type",        
108751 109188     "carglist",      "id",            "ids",           "typetoken",   
108752 109189     "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",     
       109190  +  "ccons",         "term",          "expr",          "onconf",      
       109191  +  "sortorder",     "autoinc",       "idxlist_opt",   "refargs",     
       109192  +  "defer_subclause",  "refarg",        "refact",        "init_deferred_pred_opt",
       109193  +  "conslist",      "cname",         "tcons",         "idxlist",     
108757 109194     "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",   
108758 109195     "ifexists",      "fullname",      "oneselect",     "multiselect_op",
108759 109196     "distinct",      "selcollist",    "from",          "where_opt",   
108760 109197     "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",   
108761 109198     "sclp",          "as",            "seltablist",    "stl_prefix",  
108762 109199     "joinop",        "indexed_opt",   "on_opt",        "using_opt",   
108763 109200     "joinop2",       "inscollist",    "sortlist",      "nexprlist",   
................................................................................
108826 109263    /*  46 */ "typetoken ::= typename",
108827 109264    /*  47 */ "typetoken ::= typename LP signed RP",
108828 109265    /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
108829 109266    /*  49 */ "typename ::= ids",
108830 109267    /*  50 */ "typename ::= typename ids",
108831 109268    /*  51 */ "signed ::= plus_num",
108832 109269    /*  52 */ "signed ::= minus_num",
108833         - /*  53 */ "carglist ::= carglist carg",
       109270  + /*  53 */ "carglist ::= carglist ccons",
108834 109271    /*  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",
108873         - /*  93 */ "tcons ::= CONSTRAINT nm",
       109272  + /*  55 */ "ccons ::= CONSTRAINT nm",
       109273  + /*  56 */ "ccons ::= DEFAULT term",
       109274  + /*  57 */ "ccons ::= DEFAULT LP expr RP",
       109275  + /*  58 */ "ccons ::= DEFAULT PLUS term",
       109276  + /*  59 */ "ccons ::= DEFAULT MINUS term",
       109277  + /*  60 */ "ccons ::= DEFAULT id",
       109278  + /*  61 */ "ccons ::= NULL onconf",
       109279  + /*  62 */ "ccons ::= NOT NULL onconf",
       109280  + /*  63 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
       109281  + /*  64 */ "ccons ::= UNIQUE onconf",
       109282  + /*  65 */ "ccons ::= CHECK LP expr RP",
       109283  + /*  66 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
       109284  + /*  67 */ "ccons ::= defer_subclause",
       109285  + /*  68 */ "ccons ::= COLLATE ids",
       109286  + /*  69 */ "autoinc ::=",
       109287  + /*  70 */ "autoinc ::= AUTOINCR",
       109288  + /*  71 */ "refargs ::=",
       109289  + /*  72 */ "refargs ::= refargs refarg",
       109290  + /*  73 */ "refarg ::= MATCH nm",
       109291  + /*  74 */ "refarg ::= ON INSERT refact",
       109292  + /*  75 */ "refarg ::= ON DELETE refact",
       109293  + /*  76 */ "refarg ::= ON UPDATE refact",
       109294  + /*  77 */ "refact ::= SET NULL",
       109295  + /*  78 */ "refact ::= SET DEFAULT",
       109296  + /*  79 */ "refact ::= CASCADE",
       109297  + /*  80 */ "refact ::= RESTRICT",
       109298  + /*  81 */ "refact ::= NO ACTION",
       109299  + /*  82 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
       109300  + /*  83 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
       109301  + /*  84 */ "init_deferred_pred_opt ::=",
       109302  + /*  85 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
       109303  + /*  86 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
       109304  + /*  87 */ "conslist_opt ::=",
       109305  + /*  88 */ "conslist_opt ::= COMMA conslist cname",
       109306  + /*  89 */ "conslist ::= conslist COMMA cname tcons",
       109307  + /*  90 */ "conslist ::= conslist cname tcons",
       109308  + /*  91 */ "conslist ::= cname tcons",
       109309  + /*  92 */ "cname ::=",
       109310  + /*  93 */ "cname ::= CONSTRAINT nm",
108874 109311    /*  94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
108875 109312    /*  95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
108876 109313    /*  96 */ "tcons ::= CHECK LP expr RP onconf",
108877 109314    /*  97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
108878 109315    /*  98 */ "defer_subclause_opt ::=",
108879 109316    /*  99 */ "defer_subclause_opt ::= defer_subclause",
108880 109317    /* 100 */ "onconf ::=",
................................................................................
109184 109621       */
109185 109622       case 160: /* select */
109186 109623       case 194: /* oneselect */
109187 109624   {
109188 109625   sqlite3SelectDelete(pParse->db, (yypminor->yy159));
109189 109626   }
109190 109627         break;
109191         -    case 174: /* term */
109192         -    case 175: /* expr */
       109628  +    case 173: /* term */
       109629  +    case 174: /* expr */
109193 109630   {
109194 109631   sqlite3ExprDelete(pParse->db, (yypminor->yy342).pExpr);
109195 109632   }
109196 109633         break;
109197         -    case 179: /* idxlist_opt */
       109634  +    case 178: /* idxlist_opt */
109198 109635       case 187: /* idxlist */
109199 109636       case 197: /* selcollist */
109200 109637       case 200: /* groupby_opt */
109201 109638       case 202: /* orderby_opt */
109202 109639       case 204: /* sclp */
109203 109640       case 214: /* sortlist */
109204 109641       case 215: /* nexprlist */
................................................................................
109547 109984     { 167, 6 },
109548 109985     { 168, 1 },
109549 109986     { 168, 2 },
109550 109987     { 169, 1 },
109551 109988     { 169, 1 },
109552 109989     { 164, 2 },
109553 109990     { 164, 0 },
       109991  +  { 172, 2 },
       109992  +  { 172, 2 },
       109993  +  { 172, 4 },
       109994  +  { 172, 3 },
       109995  +  { 172, 3 },
       109996  +  { 172, 2 },
       109997  +  { 172, 2 },
109554 109998     { 172, 3 },
       109999  +  { 172, 5 },
       110000  +  { 172, 2 },
       110001  +  { 172, 4 },
       110002  +  { 172, 4 },
109555 110003     { 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 },
       110004  +  { 172, 2 },
       110005  +  { 177, 0 },
       110006  +  { 177, 1 },
       110007  +  { 179, 0 },
       110008  +  { 179, 2 },
       110009  +  { 181, 2 },
       110010  +  { 181, 3 },
       110011  +  { 181, 3 },
       110012  +  { 181, 3 },
       110013  +  { 182, 2 },
       110014  +  { 182, 2 },
       110015  +  { 182, 1 },
       110016  +  { 182, 1 },
109573 110017     { 182, 2 },
109574         -  { 182, 3 },
109575         -  { 182, 3 },
109576         -  { 182, 3 },
       110018  +  { 180, 3 },
       110019  +  { 180, 2 },
       110020  +  { 183, 0 },
109577 110021     { 183, 2 },
109578 110022     { 183, 2 },
109579         -  { 183, 1 },
109580         -  { 183, 1 },
109581         -  { 183, 2 },
109582         -  { 181, 3 },
109583         -  { 181, 2 },
109584         -  { 184, 0 },
       110023  +  { 159, 0 },
       110024  +  { 159, 3 },
       110025  +  { 184, 4 },
       110026  +  { 184, 3 },
109585 110027     { 184, 2 },
109586         -  { 184, 2 },
109587         -  { 159, 0 },
109588         -  { 159, 2 },
109589         -  { 185, 3 },
       110028  +  { 185, 0 },
109590 110029     { 185, 2 },
109591         -  { 185, 1 },
109592         -  { 186, 2 },
109593 110030     { 186, 7 },
109594 110031     { 186, 5 },
109595 110032     { 186, 5 },
109596 110033     { 186, 10 },
109597 110034     { 188, 0 },
109598 110035     { 188, 1 },
109599         -  { 176, 0 },
109600         -  { 176, 3 },
       110036  +  { 175, 0 },
       110037  +  { 175, 3 },
109601 110038     { 189, 0 },
109602 110039     { 189, 2 },
109603 110040     { 190, 1 },
109604 110041     { 190, 1 },
109605 110042     { 190, 1 },
109606 110043     { 147, 4 },
109607 110044     { 192, 2 },
................................................................................
109647 110084     { 209, 2 },
109648 110085     { 211, 4 },
109649 110086     { 211, 0 },
109650 110087     { 202, 0 },
109651 110088     { 202, 3 },
109652 110089     { 214, 4 },
109653 110090     { 214, 2 },
109654         -  { 177, 1 },
109655         -  { 177, 1 },
109656         -  { 177, 0 },
       110091  +  { 176, 1 },
       110092  +  { 176, 1 },
       110093  +  { 176, 0 },
109657 110094     { 200, 0 },
109658 110095     { 200, 3 },
109659 110096     { 201, 0 },
109660 110097     { 201, 2 },
109661 110098     { 203, 0 },
109662 110099     { 203, 2 },
109663 110100     { 203, 4 },
................................................................................
109675 110112     { 217, 1 },
109676 110113     { 219, 4 },
109677 110114     { 219, 5 },
109678 110115     { 218, 0 },
109679 110116     { 218, 3 },
109680 110117     { 213, 3 },
109681 110118     { 213, 1 },
109682         -  { 175, 1 },
109683         -  { 175, 3 },
       110119  +  { 174, 1 },
       110120  +  { 174, 3 },
       110121  +  { 173, 1 },
       110122  +  { 174, 1 },
109684 110123     { 174, 1 },
109685         -  { 175, 1 },
109686         -  { 175, 1 },
109687         -  { 175, 3 },
109688         -  { 175, 5 },
       110124  +  { 174, 3 },
       110125  +  { 174, 5 },
       110126  +  { 173, 1 },
       110127  +  { 173, 1 },
109689 110128     { 174, 1 },
109690 110129     { 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 },
       110130  +  { 174, 3 },
       110131  +  { 174, 6 },
       110132  +  { 174, 5 },
       110133  +  { 174, 4 },
       110134  +  { 173, 1 },
       110135  +  { 174, 3 },
       110136  +  { 174, 3 },
       110137  +  { 174, 3 },
       110138  +  { 174, 3 },
       110139  +  { 174, 3 },
       110140  +  { 174, 3 },
       110141  +  { 174, 3 },
       110142  +  { 174, 3 },
109706 110143     { 221, 1 },
109707 110144     { 221, 2 },
109708 110145     { 221, 1 },
109709 110146     { 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 },
       110147  +  { 174, 3 },
       110148  +  { 174, 5 },
       110149  +  { 174, 2 },
       110150  +  { 174, 3 },
       110151  +  { 174, 3 },
       110152  +  { 174, 4 },
       110153  +  { 174, 2 },
       110154  +  { 174, 2 },
       110155  +  { 174, 2 },
       110156  +  { 174, 2 },
109720 110157     { 222, 1 },
109721 110158     { 222, 2 },
109722         -  { 175, 5 },
       110159  +  { 174, 5 },
109723 110160     { 223, 1 },
109724 110161     { 223, 2 },
109725         -  { 175, 5 },
109726         -  { 175, 3 },
109727         -  { 175, 5 },
109728         -  { 175, 4 },
109729         -  { 175, 4 },
109730         -  { 175, 5 },
       110162  +  { 174, 5 },
       110163  +  { 174, 3 },
       110164  +  { 174, 5 },
       110165  +  { 174, 4 },
       110166  +  { 174, 4 },
       110167  +  { 174, 5 },
109731 110168     { 225, 5 },
109732 110169     { 225, 4 },
109733 110170     { 226, 2 },
109734 110171     { 226, 0 },
109735 110172     { 224, 1 },
109736 110173     { 224, 0 },
109737 110174     { 220, 1 },
109738 110175     { 220, 0 },
109739 110176     { 215, 3 },
109740 110177     { 215, 1 },
109741 110178     { 147, 11 },
109742 110179     { 227, 1 },
109743 110180     { 227, 0 },
109744         -  { 179, 0 },
109745         -  { 179, 3 },
       110181  +  { 178, 0 },
       110182  +  { 178, 3 },
109746 110183     { 187, 5 },
109747 110184     { 187, 3 },
109748 110185     { 228, 0 },
109749 110186     { 228, 2 },
109750 110187     { 147, 4 },
109751 110188     { 147, 1 },
109752 110189     { 147, 2 },
................................................................................
109785 110222     { 239, 3 },
109786 110223     { 239, 2 },
109787 110224     { 237, 7 },
109788 110225     { 237, 5 },
109789 110226     { 237, 5 },
109790 110227     { 237, 5 },
109791 110228     { 237, 1 },
109792         -  { 175, 4 },
109793         -  { 175, 6 },
       110229  +  { 174, 4 },
       110230  +  { 174, 6 },
109794 110231     { 191, 1 },
109795 110232     { 191, 1 },
109796 110233     { 191, 1 },
109797 110234     { 147, 4 },
109798 110235     { 147, 6 },
109799 110236     { 147, 3 },
109800 110237     { 241, 0 },
................................................................................
109933 110370   {
109934 110371     pParse->db->lookaside.bEnabled = 0;
109935 110372     yygotominor.yy0 = yymsp[0].minor.yy0;
109936 110373   }
109937 110374           break;
109938 110375         case 28: /* ifnotexists ::= */
109939 110376         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);
       110377  +      case 69: /* autoinc ::= */ yytestcase(yyruleno==69);
       110378  +      case 82: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==82);
       110379  +      case 84: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==84);
       110380  +      case 86: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==86);
109944 110381         case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
109945 110382         case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
109946 110383         case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
109947 110384         case 121: /* distinct ::= */ yytestcase(yyruleno==121);
109948 110385         case 221: /* between_op ::= BETWEEN */ yytestcase(yyruleno==221);
109949 110386         case 224: /* in_op ::= IN */ yytestcase(yyruleno==224);
109950 110387   {yygotominor.yy392 = 0;}
109951 110388           break;
109952 110389         case 29: /* ifnotexists ::= IF NOT EXISTS */
109953 110390         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);
       110391  +      case 70: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==70);
       110392  +      case 85: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==85);
109956 110393         case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
109957 110394         case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
109958 110395         case 222: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==222);
109959 110396         case 225: /* in_op ::= NOT IN */ yytestcase(yyruleno==225);
109960 110397   {yygotominor.yy392 = 1;}
109961 110398           break;
109962 110399         case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
................................................................................
109976 110413     yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
109977 110414   }
109978 110415           break;
109979 110416         case 37: /* columnid ::= nm */
109980 110417   {
109981 110418     sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
109982 110419     yygotominor.yy0 = yymsp[0].minor.yy0;
       110420  +  pParse->constraintName.n = 0;
109983 110421   }
109984 110422           break;
109985 110423         case 38: /* id ::= ID */
109986 110424         case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
109987 110425         case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
109988 110426         case 41: /* nm ::= id */ yytestcase(yyruleno==41);
109989 110427         case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
................................................................................
110021 110459     yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
110022 110460     yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
110023 110461   }
110024 110462           break;
110025 110463         case 50: /* typename ::= typename ids */
110026 110464   {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 110465           break;
110028         -      case 57: /* ccons ::= DEFAULT term */
110029         -      case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
       110466  +      case 55: /* ccons ::= CONSTRAINT nm */
       110467  +      case 93: /* cname ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
       110468  +{pParse->constraintName = yymsp[0].minor.yy0;}
       110469  +        break;
       110470  +      case 56: /* ccons ::= DEFAULT term */
       110471  +      case 58: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==58);
110030 110472   {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy342);}
110031 110473           break;
110032         -      case 58: /* ccons ::= DEFAULT LP expr RP */
       110474  +      case 57: /* ccons ::= DEFAULT LP expr RP */
110033 110475   {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy342);}
110034 110476           break;
110035         -      case 60: /* ccons ::= DEFAULT MINUS term */
       110477  +      case 59: /* ccons ::= DEFAULT MINUS term */
110036 110478   {
110037 110479     ExprSpan v;
110038 110480     v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy342.pExpr, 0, 0);
110039 110481     v.zStart = yymsp[-1].minor.yy0.z;
110040 110482     v.zEnd = yymsp[0].minor.yy342.zEnd;
110041 110483     sqlite3AddDefaultValue(pParse,&v);
110042 110484   }
110043 110485           break;
110044         -      case 61: /* ccons ::= DEFAULT id */
       110486  +      case 60: /* ccons ::= DEFAULT id */
110045 110487   {
110046 110488     ExprSpan v;
110047 110489     spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
110048 110490     sqlite3AddDefaultValue(pParse,&v);
110049 110491   }
110050 110492           break;
110051         -      case 63: /* ccons ::= NOT NULL onconf */
       110493  +      case 62: /* ccons ::= NOT NULL onconf */
110052 110494   {sqlite3AddNotNull(pParse, yymsp[0].minor.yy392);}
110053 110495           break;
110054         -      case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
       110496  +      case 63: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
110055 110497   {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy392,yymsp[0].minor.yy392,yymsp[-2].minor.yy392);}
110056 110498           break;
110057         -      case 65: /* ccons ::= UNIQUE onconf */
       110499  +      case 64: /* ccons ::= UNIQUE onconf */
110058 110500   {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy392,0,0,0,0);}
110059 110501           break;
110060         -      case 66: /* ccons ::= CHECK LP expr RP */
       110502  +      case 65: /* ccons ::= CHECK LP expr RP */
110061 110503   {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy342.pExpr);}
110062 110504           break;
110063         -      case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */
       110505  +      case 66: /* ccons ::= REFERENCES nm idxlist_opt refargs */
110064 110506   {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy442,yymsp[0].minor.yy392);}
110065 110507           break;
110066         -      case 68: /* ccons ::= defer_subclause */
       110508  +      case 67: /* ccons ::= defer_subclause */
110067 110509   {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy392);}
110068 110510           break;
110069         -      case 69: /* ccons ::= COLLATE ids */
       110511  +      case 68: /* ccons ::= COLLATE ids */
110070 110512   {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
110071 110513           break;
110072         -      case 72: /* refargs ::= */
       110514  +      case 71: /* refargs ::= */
110073 110515   { yygotominor.yy392 = OE_None*0x0101; /* EV: R-19803-45884 */}
110074 110516           break;
110075         -      case 73: /* refargs ::= refargs refarg */
       110517  +      case 72: /* refargs ::= refargs refarg */
110076 110518   { yygotominor.yy392 = (yymsp[-1].minor.yy392 & ~yymsp[0].minor.yy207.mask) | yymsp[0].minor.yy207.value; }
110077 110519           break;
110078         -      case 74: /* refarg ::= MATCH nm */
110079         -      case 75: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==75);
       110520  +      case 73: /* refarg ::= MATCH nm */
       110521  +      case 74: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==74);
110080 110522   { yygotominor.yy207.value = 0;     yygotominor.yy207.mask = 0x000000; }
110081 110523           break;
110082         -      case 76: /* refarg ::= ON DELETE refact */
       110524  +      case 75: /* refarg ::= ON DELETE refact */
110083 110525   { yygotominor.yy207.value = yymsp[0].minor.yy392;     yygotominor.yy207.mask = 0x0000ff; }
110084 110526           break;
110085         -      case 77: /* refarg ::= ON UPDATE refact */
       110527  +      case 76: /* refarg ::= ON UPDATE refact */
110086 110528   { yygotominor.yy207.value = yymsp[0].minor.yy392<<8;  yygotominor.yy207.mask = 0x00ff00; }
110087 110529           break;
110088         -      case 78: /* refact ::= SET NULL */
       110530  +      case 77: /* refact ::= SET NULL */
110089 110531   { yygotominor.yy392 = OE_SetNull;  /* EV: R-33326-45252 */}
110090 110532           break;
110091         -      case 79: /* refact ::= SET DEFAULT */
       110533  +      case 78: /* refact ::= SET DEFAULT */
110092 110534   { yygotominor.yy392 = OE_SetDflt;  /* EV: R-33326-45252 */}
110093 110535           break;
110094         -      case 80: /* refact ::= CASCADE */
       110536  +      case 79: /* refact ::= CASCADE */
110095 110537   { yygotominor.yy392 = OE_Cascade;  /* EV: R-33326-45252 */}
110096 110538           break;
110097         -      case 81: /* refact ::= RESTRICT */
       110539  +      case 80: /* refact ::= RESTRICT */
110098 110540   { yygotominor.yy392 = OE_Restrict; /* EV: R-33326-45252 */}
110099 110541           break;
110100         -      case 82: /* refact ::= NO ACTION */
       110542  +      case 81: /* refact ::= NO ACTION */
110101 110543   { yygotominor.yy392 = OE_None;     /* EV: R-33326-45252 */}
110102 110544           break;
110103         -      case 84: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
       110545  +      case 83: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
110104 110546         case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
110105 110547         case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
110106 110548         case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
110107 110549   {yygotominor.yy392 = yymsp[0].minor.yy392;}
110108 110550           break;
110109         -      case 88: /* conslist_opt ::= */
       110551  +      case 87: /* conslist_opt ::= */
110110 110552   {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
110111 110553           break;
110112         -      case 89: /* conslist_opt ::= COMMA conslist */
110113         -{yygotominor.yy0 = yymsp[-1].minor.yy0;}
       110554  +      case 88: /* conslist_opt ::= COMMA conslist cname */
       110555  +{yygotominor.yy0 = yymsp[-2].minor.yy0;}
       110556  +        break;
       110557  +      case 92: /* cname ::= */
       110558  +{pParse->constraintName.n = 0;}
110114 110559           break;
110115 110560         case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
110116 110561   {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy442,yymsp[0].minor.yy392,yymsp[-2].minor.yy392,0);}
110117 110562           break;
110118 110563         case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
110119 110564   {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy442,yymsp[0].minor.yy392,0,0,0,0);}
110120 110565           break;
................................................................................
110523 110968         case 204: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==204);
110524 110969         case 205: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==205);
110525 110970         case 206: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==206);
110526 110971   {spanBinaryExpr(&yygotominor.yy342,pParse,yymsp[-1].major,&yymsp[-2].minor.yy342,&yymsp[0].minor.yy342);}
110527 110972           break;
110528 110973         case 207: /* likeop ::= LIKE_KW */
110529 110974         case 209: /* likeop ::= MATCH */ yytestcase(yyruleno==209);
110530         -{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.not = 0;}
       110975  +{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.bNot = 0;}
110531 110976           break;
110532 110977         case 208: /* likeop ::= NOT LIKE_KW */
110533 110978         case 210: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==210);
110534         -{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.not = 1;}
       110979  +{yygotominor.yy318.eOperator = yymsp[0].minor.yy0; yygotominor.yy318.bNot = 1;}
110535 110980           break;
110536 110981         case 211: /* expr ::= expr likeop expr */
110537 110982   {
110538 110983     ExprList *pList;
110539 110984     pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy342.pExpr);
110540 110985     pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy342.pExpr);
110541 110986     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);
       110987  +  if( yymsp[-1].minor.yy318.bNot ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
110543 110988     yygotominor.yy342.zStart = yymsp[-2].minor.yy342.zStart;
110544 110989     yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
110545 110990     if( yygotominor.yy342.pExpr ) yygotominor.yy342.pExpr->flags |= EP_InfixFunc;
110546 110991   }
110547 110992           break;
110548 110993         case 212: /* expr ::= expr likeop expr ESCAPE expr */
110549 110994   {
110550 110995     ExprList *pList;
110551 110996     pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy342.pExpr);
110552 110997     pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy342.pExpr);
110553 110998     pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy342.pExpr);
110554 110999     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);
       111000  +  if( yymsp[-3].minor.yy318.bNot ) yygotominor.yy342.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy342.pExpr, 0, 0);
110556 111001     yygotominor.yy342.zStart = yymsp[-4].minor.yy342.zStart;
110557 111002     yygotominor.yy342.zEnd = yymsp[0].minor.yy342.zEnd;
110558 111003     if( yygotominor.yy342.pExpr ) yygotominor.yy342.pExpr->flags |= EP_InfixFunc;
110559 111004   }
110560 111005           break;
110561 111006         case 213: /* expr ::= expr ISNULL|NOTNULL */
110562 111007   {spanUnaryPostfix(&yygotominor.yy342,pParse,yymsp[0].major,&yymsp[-1].minor.yy342,&yymsp[0].minor.yy0);}
................................................................................
110975 111420         /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
110976 111421         /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
110977 111422         /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
110978 111423         /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
110979 111424         /* (44) type ::= */ yytestcase(yyruleno==44);
110980 111425         /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
110981 111426         /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
110982         -      /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
       111427  +      /* (53) carglist ::= carglist ccons */ yytestcase(yyruleno==53);
110983 111428         /* (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);
       111429  +      /* (61) ccons ::= NULL onconf */ yytestcase(yyruleno==61);
       111430  +      /* (89) conslist ::= conslist COMMA cname tcons */ yytestcase(yyruleno==89);
       111431  +      /* (90) conslist ::= conslist cname tcons */ yytestcase(yyruleno==90);
       111432  +      /* (91) conslist ::= cname tcons */ yytestcase(yyruleno==91);
110991 111433         /* (277) foreach_clause ::= */ yytestcase(yyruleno==277);
110992 111434         /* (278) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==278);
110993 111435         /* (285) tridxby ::= */ yytestcase(yyruleno==285);
110994 111436         /* (303) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==303);
110995 111437         /* (304) database_kw_opt ::= */ yytestcase(yyruleno==304);
110996 111438         /* (312) kwcolumn_opt ::= */ yytestcase(yyruleno==312);
110997 111439         /* (313) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==313);
................................................................................
115891 116333   **
115892 116334   ** FTS3 used to optionally store character offsets using a compile-time
115893 116335   ** option.  But that functionality is no longer supported.
115894 116336   **
115895 116337   ** A doclist is stored like this:
115896 116338   **
115897 116339   ** array {
115898         -**   varint docid;
       116340  +**   varint docid;          (delta from previous doclist)
115899 116341   **   array {                (position list for column 0)
115900 116342   **     varint position;     (2 more than the delta from previous position)
115901 116343   **   }
115902 116344   **   array {
115903 116345   **     varint POS_COLUMN;   (marks start of position list for new column)
115904 116346   **     varint column;       (index of new column)
115905 116347   **     array {
................................................................................
115922 116364   **   value:     123 5 9 1 1 14 35 0 234 72 0
115923 116365   **
115924 116366   ** The 123 value is the first docid.  For column zero in this document
115925 116367   ** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
115926 116368   ** at D signals the start of a new column; the 1 at E indicates that the
115927 116369   ** new column is column number 1.  There are two positions at 12 and 45
115928 116370   ** (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.
       116371  +** 234 at I is the delta to next docid (357).  It has one position 70
       116372  +** (72-2) and then terminates with the 0 at K.
115931 116373   **
115932 116374   ** A "position-list" is the list of positions for multiple columns for
115933 116375   ** a single docid.  A "column-list" is the set of positions for a single
115934 116376   ** column.  Hence, a position-list consists of one or more column-lists,
115935 116377   ** a document record consists of a docid followed by a position-list and
115936 116378   ** a doclist consists of one or more document records.
115937 116379   **
................................................................................
116458 116900   */
116459 116901   #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
116460 116902   
116461 116903   
116462 116904   #ifndef MIN
116463 116905   # define MIN(x,y) ((x)<(y)?(x):(y))
116464 116906   #endif
       116907  +#ifndef MAX
       116908  +# define MAX(x,y) ((x)>(y)?(x):(y))
       116909  +#endif
116465 116910   
116466 116911   /*
116467 116912   ** Maximum length of a varint encoded integer. The varint format is different
116468 116913   ** from that used by SQLite, so the maximum length is 10, not 9.
116469 116914   */
116470 116915   #define FTS3_VARINT_MAX 10
116471 116916   
................................................................................
116512 116957   ** false.
116513 116958   */
116514 116959   #ifdef SQLITE_COVERAGE_TEST
116515 116960   # define ALWAYS(x) (1)
116516 116961   # define NEVER(X)  (0)
116517 116962   #else
116518 116963   # define ALWAYS(x) (x)
116519         -# define NEVER(X)  (x)
       116964  +# define NEVER(x)  (x)
116520 116965   #endif
116521 116966   
116522 116967   /*
116523 116968   ** Internal types used by SQLite.
116524 116969   */
116525 116970   typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
116526 116971   typedef short int i16;            /* 2-byte (or larger) signed integer */
116527 116972   typedef unsigned int u32;         /* 4-byte unsigned integer */
116528 116973   typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
       116974  +typedef sqlite3_int64 i64;        /* 8-byte signed integer */
116529 116975   
116530 116976   /*
116531 116977   ** Macro used to suppress compiler warnings for unused parameters.
116532 116978   */
116533 116979   #define UNUSED_PARAMETER(x) (void)(x)
116534 116980   
116535 116981   /*
................................................................................
116584 117030     const char *zDb;                /* logical database name */
116585 117031     const char *zName;              /* virtual table name */
116586 117032     int nColumn;                    /* number of named columns in virtual table */
116587 117033     char **azColumn;                /* column names.  malloced */
116588 117034     sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
116589 117035     char *zContentTbl;              /* content=xxx option, or NULL */
116590 117036     char *zLanguageid;              /* languageid=xxx option, or NULL */
       117037  +  u8 bAutoincrmerge;              /* True if automerge=1 */
       117038  +  u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
116591 117039   
116592 117040     /* Precompiled statements used by the implementation. Each of these 
116593 117041     ** statements is run and reset within a single virtual table API call. 
116594 117042     */
116595         -  sqlite3_stmt *aStmt[28];
       117043  +  sqlite3_stmt *aStmt[37];
116596 117044   
116597 117045     char *zReadExprlist;
116598 117046     char *zWriteExprlist;
116599 117047   
116600 117048     int nNodeSize;                  /* Soft limit for node size */
       117049  +  u8 bFts4;                       /* True for FTS4, false for FTS3 */
116601 117050     u8 bHasStat;                    /* True if %_stat table exists */
116602 117051     u8 bHasDocsize;                 /* True if %_docsize table exists */
116603 117052     u8 bDescIdx;                    /* True if doclists are in reverse order */
       117053  +  u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
116604 117054     int nPgsz;                      /* Page size for host database */
116605 117055     char *zSegmentsTbl;             /* Name of %_segments table */
116606 117056     sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
116607 117057   
116608         -  /* TODO: Fix the first paragraph of this comment.
116609         -  **
       117058  +  /* 
116610 117059     ** 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.
       117060  +  ** updates during transactions. All pending updates buffered at any one
       117061  +  ** time must share a common language-id (see the FTS4 langid= feature).
       117062  +  ** The current language id is stored in variable iPrevLangid.
116616 117063     **
116617 117064     ** A single FTS4 table may have multiple full-text indexes. For each index
116618 117065     ** there is an entry in the aIndex[] array. Index 0 is an index of all the
116619 117066     ** terms that appear in the document set. Each subsequent index in aIndex[]
116620 117067     ** is an index of prefixes of a specific length.
       117068  +  **
       117069  +  ** Variable nPendingData contains an estimate the memory consumed by the 
       117070  +  ** pending data structures, including hash table overhead, but not including
       117071  +  ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
       117072  +  ** tables are flushed to disk. Variable iPrevDocid is the docid of the most 
       117073  +  ** recently inserted record.
116621 117074     */
116622 117075     int nIndex;                     /* Size of aIndex[] */
116623 117076     struct Fts3Index {
116624 117077       int nPrefix;                  /* Prefix length (0 for main terms index) */
116625 117078       Fts3Hash hPending;            /* Pending terms table for this index */
116626 117079     } *aIndex;
116627 117080     int nMaxPendingData;            /* Max pending data before flush to disk */
................................................................................
116812 117265   SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
116813 117266   
116814 117267   SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
116815 117268   SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
116816 117269   SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
116817 117270   SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
116818 117271   SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
       117272  +SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
116819 117273   
116820 117274   /* Special values interpreted by sqlite3SegReaderCursor() */
116821 117275   #define FTS3_SEGCURSOR_PENDING        -1
116822 117276   #define FTS3_SEGCURSOR_ALL            -2
116823 117277   
116824 117278   SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
116825 117279   SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
................................................................................
116863 117317     /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
116864 117318     char *zTerm;                    /* Pointer to term buffer */
116865 117319     int nTerm;                      /* Size of zTerm in bytes */
116866 117320     char *aDoclist;                 /* Pointer to doclist buffer */
116867 117321     int nDoclist;                   /* Size of aDoclist[] in bytes */
116868 117322   };
116869 117323   
       117324  +SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
       117325  +
116870 117326   /* fts3.c */
116871 117327   SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
116872 117328   SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
116873 117329   SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
116874 117330   SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
116875 117331   SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
116876 117332   SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
116877 117333   SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
116878 117334   SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
       117335  +SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
116879 117336   
116880 117337   /* fts3_tokenizer.c */
116881 117338   SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
116882 117339   SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
116883 117340   SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, 
116884 117341       sqlite3_tokenizer **, char **
116885 117342   );
................................................................................
116911 117368   
116912 117369   SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
116913 117370   
116914 117371   SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
116915 117372       Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
116916 117373   SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
116917 117374       Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
116918         -SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol); 
       117375  +SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); 
116919 117376   SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
116920 117377   SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
116921 117378   
116922 117379   SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
116923 117380   
116924 117381   #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
116925 117382   #endif /* _FTSINT_H */
................................................................................
117200 117657       }
117201 117658   
117202 117659       sqlite3_free(zSql);
117203 117660       sqlite3_free(zCols);
117204 117661       *pRc = rc;
117205 117662     }
117206 117663   }
       117664  +
       117665  +/*
       117666  +** Create the %_stat table if it does not already exist.
       117667  +*/
       117668  +SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
       117669  +  fts3DbExec(pRc, p->db, 
       117670  +      "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
       117671  +          "(id INTEGER PRIMARY KEY, value BLOB);",
       117672  +      p->zDb, p->zName
       117673  +  );
       117674  +  if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
       117675  +}
117207 117676   
117208 117677   /*
117209 117678   ** Create the backing store tables (%_content, %_segments and %_segdir)
117210 117679   ** required by the FTS3 table passed as the only argument. This is done
117211 117680   ** as part of the vtab xCreate() method.
117212 117681   **
117213 117682   ** If the p->bHasDocsize boolean is true (indicating that this is an
................................................................................
117261 117730     );
117262 117731     if( p->bHasDocsize ){
117263 117732       fts3DbExec(&rc, db, 
117264 117733           "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
117265 117734           p->zDb, p->zName
117266 117735       );
117267 117736     }
       117737  +  assert( p->bHasStat==p->bFts4 );
117268 117738     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         -    );
       117739  +    sqlite3Fts3CreateStatTable(&rc, p);
117273 117740     }
117274 117741     return rc;
117275 117742   }
117276 117743   
117277 117744   /*
117278 117745   ** Store the current database page-size in bytes in p->nPgsz.
117279 117746   **
................................................................................
117906 118373     p->nColumn = nCol;
117907 118374     p->nPendingData = 0;
117908 118375     p->azColumn = (char **)&p[1];
117909 118376     p->pTokenizer = pTokenizer;
117910 118377     p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
117911 118378     p->bHasDocsize = (isFts4 && bNoDocsize==0);
117912 118379     p->bHasStat = isFts4;
       118380  +  p->bFts4 = isFts4;
117913 118381     p->bDescIdx = bDescIdx;
       118382  +  p->bAutoincrmerge = 0xff;   /* 0xff means setting unknown */
117914 118383     p->zContentTbl = zContent;
117915 118384     p->zLanguageid = zLanguageid;
117916 118385     zContent = 0;
117917 118386     zLanguageid = 0;
117918 118387     TESTONLY( p->inTransaction = -1 );
117919 118388     TESTONLY( p->mxSavepoint = -1 );
117920 118389   
................................................................................
117958 118427   
117959 118428     /* If this is an xCreate call, create the underlying tables in the 
117960 118429     ** database. TODO: For xConnect(), it could verify that said tables exist.
117961 118430     */
117962 118431     if( isCreate ){
117963 118432       rc = fts3CreateTables(p);
117964 118433     }
       118434  +
       118435  +  /* Check to see if a legacy fts3 table has been "upgraded" by the
       118436  +  ** addition of a %_stat table so that it can use incremental merge.
       118437  +  */
       118438  +  if( !isFts4 && !isCreate ){
       118439  +    int rc2 = SQLITE_OK;
       118440  +    fts3DbExec(&rc2, db, "SELECT 1 FROM %Q.'%q_stat' WHERE id=2",
       118441  +               p->zDb, p->zName);
       118442  +    if( rc2==SQLITE_OK ) p->bHasStat = 1;
       118443  +  }
117965 118444   
117966 118445     /* Figure out the page-size for the database. This is required in order to
117967 118446     ** estimate the cost of loading large doclists from the database.  */
117968 118447     fts3DatabasePageSize(&rc, p);
117969 118448     p->nNodeSize = p->nPgsz-35;
117970 118449   
117971 118450     /* Declare the table schema to SQLite. */
................................................................................
119302 119781   
119303 119782   /*
119304 119783   ** Set up a cursor object for iterating through a full-text index or a 
119305 119784   ** single level therein.
119306 119785   */
119307 119786   SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
119308 119787     Fts3Table *p,                   /* FTS3 table handle */
119309         -  int iLangid,
       119788  +  int iLangid,                    /* Language-id to search */
119310 119789     int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
119311 119790     int iLevel,                     /* Level of segments to scan */
119312 119791     const char *zTerm,              /* Term to query for */
119313 119792     int nTerm,                      /* Size of zTerm in bytes */
119314 119793     int isPrefix,                   /* True for a prefix search */
119315 119794     int isScan,                     /* True to scan from zTerm to EOF */
119316 119795     Fts3MultiSegReader *pCsr       /* Cursor object to populate */
................................................................................
119320 119799         ||  iLevel==FTS3_SEGCURSOR_PENDING 
119321 119800         ||  iLevel>=0
119322 119801     );
119323 119802     assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
119324 119803     assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
119325 119804     assert( isPrefix==0 || isScan==0 );
119326 119805   
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 119806     memset(pCsr, 0, sizeof(Fts3MultiSegReader));
119332         -
119333 119807     return fts3SegReaderCursor(
119334 119808         p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
119335 119809     );
119336 119810   }
119337 119811   
119338 119812   /*
119339 119813   ** In addition to its current configuration, have the Fts3MultiSegReader
................................................................................
119590 120064         return SQLITE_NOMEM;
119591 120065       }
119592 120066   
119593 120067       pCsr->iLangid = 0;
119594 120068       if( nVal==2 ) pCsr->iLangid = sqlite3_value_int(apVal[1]);
119595 120069   
119596 120070       rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
119597         -        p->azColumn, p->bHasStat, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
       120071  +        p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
119598 120072       );
119599 120073       if( rc!=SQLITE_OK ){
119600 120074         if( rc==SQLITE_ERROR ){
119601 120075           static const char *zErr = "malformed MATCH expression: [%s]";
119602 120076           p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
119603 120077         }
119604 120078         return rc;
................................................................................
119733 120207   }
119734 120208   
119735 120209   /*
119736 120210   ** Implementation of xSync() method. Flush the contents of the pending-terms
119737 120211   ** hash-table to the database.
119738 120212   */
119739 120213   static int fts3SyncMethod(sqlite3_vtab *pVtab){
119740         -  int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
119741         -  sqlite3Fts3SegmentsClose((Fts3Table *)pVtab);
       120214  +
       120215  +  /* Following an incremental-merge operation, assuming that the input
       120216  +  ** segments are not completely consumed (the usual case), they are updated
       120217  +  ** in place to remove the entries that have already been merged. This
       120218  +  ** involves updating the leaf block that contains the smallest unmerged
       120219  +  ** entry and each block (if any) between the leaf and the root node. So
       120220  +  ** if the height of the input segment b-trees is N, and input segments
       120221  +  ** are merged eight at a time, updating the input segments at the end
       120222  +  ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
       120223  +  ** small - often between 0 and 2. So the overhead of the incremental
       120224  +  ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
       120225  +  ** dwarfing the actual productive work accomplished, the incremental merge
       120226  +  ** is only attempted if it will write at least 64 leaf blocks. Hence
       120227  +  ** nMinMerge.
       120228  +  **
       120229  +  ** Of course, updating the input segments also involves deleting a bunch
       120230  +  ** of blocks from the segments table. But this is not considered overhead
       120231  +  ** as it would also be required by a crisis-merge that used the same input 
       120232  +  ** segments.
       120233  +  */
       120234  +  const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
       120235  +
       120236  +  Fts3Table *p = (Fts3Table*)pVtab;
       120237  +  int rc = sqlite3Fts3PendingTermsFlush(p);
       120238  +
       120239  +  if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
       120240  +    int mxLevel = 0;              /* Maximum relative level value in db */
       120241  +    int A;                        /* Incr-merge parameter A */
       120242  +
       120243  +    rc = sqlite3Fts3MaxLevel(p, &mxLevel);
       120244  +    assert( rc==SQLITE_OK || mxLevel==0 );
       120245  +    A = p->nLeafAdd * mxLevel;
       120246  +    A += (A/2);
       120247  +    if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
       120248  +  }
       120249  +  sqlite3Fts3SegmentsClose(p);
119742 120250     return rc;
119743 120251   }
119744 120252   
119745 120253   /*
119746 120254   ** Implementation of xBegin() method. This is a no-op.
119747 120255   */
119748 120256   static int fts3BeginMethod(sqlite3_vtab *pVtab){
119749         -  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
       120257  +  Fts3Table *p = (Fts3Table*)pVtab;
119750 120258     UNUSED_PARAMETER(pVtab);
119751 120259     assert( p->pSegments==0 );
119752 120260     assert( p->nPendingData==0 );
119753 120261     assert( p->inTransaction!=1 );
119754 120262     TESTONLY( p->inTransaction = 1 );
119755 120263     TESTONLY( p->mxSavepoint = -1; );
       120264  +  p->nLeafAdd = 0;
119756 120265     return SQLITE_OK;
119757 120266   }
119758 120267   
119759 120268   /*
119760 120269   ** Implementation of xCommit() method. This is a no-op. The contents of
119761 120270   ** the pending-terms hash-table have already been flushed into the database
119762 120271   ** by fts3SyncMethod().
................................................................................
120043 120552   
120044 120553   /*
120045 120554   ** The xSavepoint() method.
120046 120555   **
120047 120556   ** Flush the contents of the pending-terms table to disk.
120048 120557   */
120049 120558   static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
       120559  +  int rc = SQLITE_OK;
120050 120560     UNUSED_PARAMETER(iSavepoint);
120051 120561     assert( ((Fts3Table *)pVtab)->inTransaction );
120052 120562     assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
120053 120563     TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
120054         -  return fts3SyncMethod(pVtab);
       120564  +  if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
       120565  +    rc = fts3SyncMethod(pVtab);
       120566  +  }
       120567  +  return rc;
120055 120568   }
120056 120569   
120057 120570   /*
120058 120571   ** The xRelease() method.
120059 120572   **
120060 120573   ** This is a no-op.
120061 120574   */
................................................................................
120520 121033   */
120521 121034   SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
120522 121035     int bDescIdx,                   /* True if the doclist is desc */
120523 121036     char *aDoclist,                 /* Pointer to entire doclist */
120524 121037     int nDoclist,                   /* Length of aDoclist in bytes */
120525 121038     char **ppIter,                  /* IN/OUT: Iterator pointer */
120526 121039     sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
120527         -  int *pnList,                    /* IN/OUT: List length pointer */
       121040  +  int *pnList,                    /* OUT: List length pointer */
120528 121041     u8 *pbEof                       /* OUT: End-of-file flag */
120529 121042   ){
120530 121043     char *p = *ppIter;
120531 121044   
120532 121045     assert( nDoclist>0 );
120533 121046     assert( *pbEof==0 );
120534 121047     assert( p || *piDocid==0 );
................................................................................
120566 121079         char *pSave = p;
120567 121080         fts3ReversePoslist(aDoclist, &p);
120568 121081         *pnList = (int)(pSave - p);
120569 121082       }
120570 121083       *ppIter = p;
120571 121084     }
120572 121085   }
       121086  +
       121087  +/*
       121088  +** Iterate forwards through a doclist.
       121089  +*/
       121090  +SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
       121091  +  int bDescIdx,                   /* True if the doclist is desc */
       121092  +  char *aDoclist,                 /* Pointer to entire doclist */
       121093  +  int nDoclist,                   /* Length of aDoclist in bytes */
       121094  +  char **ppIter,                  /* IN/OUT: Iterator pointer */
       121095  +  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
       121096  +  u8 *pbEof                       /* OUT: End-of-file flag */
       121097  +){
       121098  +  char *p = *ppIter;
       121099  +
       121100  +  assert( nDoclist>0 );
       121101  +  assert( *pbEof==0 );
       121102  +  assert( p || *piDocid==0 );
       121103  +  assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
       121104  +
       121105  +  if( p==0 ){
       121106  +    p = aDoclist;
       121107  +    p += sqlite3Fts3GetVarint(p, piDocid);
       121108  +  }else{
       121109  +    fts3PoslistCopy(0, &p);
       121110  +    if( p>=&aDoclist[nDoclist] ){
       121111  +      *pbEof = 1;
       121112  +    }else{
       121113  +      sqlite3_int64 iVar;
       121114  +      p += sqlite3Fts3GetVarint(p, &iVar);
       121115  +      *piDocid += ((bDescIdx ? -1 : 1) * iVar);
       121116  +    }
       121117  +  }
       121118  +
       121119  +  *ppIter = p;
       121120  +}
120573 121121   
120574 121122   /*
120575 121123   ** Attempt to move the phrase iterator to point to the next matching docid. 
120576 121124   ** If an error occurs, return an SQLite error code. Otherwise, return 
120577 121125   ** SQLITE_OK.
120578 121126   **
120579 121127   ** If there is no "next" entry and no error occurs, then *pbEof is set to
................................................................................
120962 121510     int nToken = 0;
120963 121511     int nOr = 0;
120964 121512   
120965 121513     /* Allocate a MultiSegReader for each token in the expression. */
120966 121514     fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
120967 121515   
120968 121516     /* Determine which, if any, tokens in the expression should be deferred. */
120969         -  if( rc==SQLITE_OK && nToken>1 && pTab->bHasStat ){
       121517  +  if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
120970 121518       Fts3TokenAndCost *aTC;
120971 121519       Fts3Expr **apOr;
120972 121520       aTC = (Fts3TokenAndCost *)sqlite3_malloc(
120973 121521           sizeof(Fts3TokenAndCost) * nToken
120974 121522         + sizeof(Fts3Expr *) * nOr * 2
120975 121523       );
120976 121524       apOr = (Fts3Expr **)&aTC[nToken];
................................................................................
121722 122270   ** for 'X' is requested, the buffer returned may contain:
121723 122271   **
121724 122272   **     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
121725 122273   **
121726 122274   ** This function works regardless of whether or not the phrase is deferred,
121727 122275   ** incremental, or neither.
121728 122276   */
121729         -SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(
       122277  +SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
121730 122278     Fts3Cursor *pCsr,               /* FTS3 cursor object */
121731 122279     Fts3Expr *pExpr,                /* Phrase to return doclist for */
121732         -  int iCol                        /* Column to return position list for */
       122280  +  int iCol,                       /* Column to return position list for */
       122281  +  char **ppOut                    /* OUT: Pointer to position list */
121733 122282   ){
121734 122283     Fts3Phrase *pPhrase = pExpr->pPhrase;
121735 122284     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
121736         -  char *pIter = pPhrase->doclist.pList;
       122285  +  char *pIter;
121737 122286     int iThis;
       122287  +  sqlite3_int64 iDocid;
121738 122288   
       122289  +  /* If this phrase is applies specifically to some column other than 
       122290  +  ** column iCol, return a NULL pointer.  */
       122291  +  *ppOut = 0;
121739 122292     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 );
       122293  +  if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
       122294  +    return SQLITE_OK;
       122295  +  }
       122296  +
       122297  +  iDocid = pExpr->iDocid;
       122298  +  pIter = pPhrase->doclist.pList;
       122299  +  if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
       122300  +    int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
       122301  +    int bOr = 0;
       122302  +    u8 bEof = 0;
       122303  +    Fts3Expr *p;
       122304  +
       122305  +    /* Check if this phrase descends from an OR expression node. If not, 
       122306  +    ** return NULL. Otherwise, the entry that corresponds to docid 
       122307  +    ** pCsr->iPrevId may lie earlier in the doclist buffer. */
       122308  +    for(p=pExpr->pParent; p; p=p->pParent){
       122309  +      if( p->eType==FTSQUERY_OR ) bOr = 1;
       122310  +    }
       122311  +    if( bOr==0 ) return SQLITE_OK;
       122312  +
       122313  +    /* This is the descendent of an OR node. In this case we cannot use
       122314  +    ** an incremental phrase. Load the entire doclist for the phrase
       122315  +    ** into memory in this case.  */
       122316  +    if( pPhrase->bIncr ){
       122317  +      int rc = SQLITE_OK;
       122318  +      int bEofSave = pExpr->bEof;
       122319  +      fts3EvalRestart(pCsr, pExpr, &rc);
       122320  +      while( rc==SQLITE_OK && !pExpr->bEof ){
       122321  +        fts3EvalNextRow(pCsr, pExpr, &rc);
       122322  +        if( bEofSave==0 && pExpr->iDocid==iDocid ) break;
       122323  +      }
       122324  +      pIter = pPhrase->doclist.pList;
       122325  +      assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
       122326  +      if( rc!=SQLITE_OK ) return rc;
       122327  +    }
       122328  +
       122329  +    if( pExpr->bEof ){
       122330  +      pIter = 0;
       122331  +      iDocid = 0;
       122332  +    }
       122333  +    bEof = (pPhrase->doclist.nAll==0);
       122334  +    assert( bDescDoclist==0 || bDescDoclist==1 );
       122335  +    assert( pCsr->bDesc==0 || pCsr->bDesc==1 );
       122336  +
       122337  +    if( pCsr->bDesc==bDescDoclist ){
       122338  +      int dummy;
       122339  +      while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
       122340  +        sqlite3Fts3DoclistPrev(
       122341  +            bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
       122342  +            &pIter, &iDocid, &dummy, &bEof
       122343  +        );
       122344  +      }
       122345  +    }else{
       122346  +      while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
       122347  +        sqlite3Fts3DoclistNext(
       122348  +            bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
       122349  +            &pIter, &iDocid, &bEof
       122350  +        );
       122351  +      }
       122352  +    }
       122353  +
       122354  +    if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
       122355  +  }
       122356  +  if( pIter==0 ) return SQLITE_OK;
       122357  +
121749 122358     if( *pIter==0x01 ){
121750 122359       pIter++;
121751 122360       pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
121752 122361     }else{
121753 122362       iThis = 0;
121754 122363     }
121755 122364     while( iThis<iCol ){
121756 122365       fts3ColumnlistCopy(0, &pIter);
121757 122366       if( *pIter==0x00 ) return 0;
121758 122367       pIter++;
121759 122368       pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
121760 122369     }
121761 122370   
121762         -  return ((iCol==iThis)?pIter:0);
       122371  +  *ppOut = ((iCol==iThis)?pIter:0);
       122372  +  return SQLITE_OK;
121763 122373   }
121764 122374   
121765 122375   /*
121766 122376   ** Free all components of the Fts3Phrase structure that were allocated by
121767 122377   ** the eval module. Specifically, this means to free:
121768 122378   **
121769 122379   **   * the contents of pPhrase->doclist, and
................................................................................
121777 122387       memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
121778 122388       for(i=0; i<pPhrase->nToken; i++){
121779 122389         fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
121780 122390         pPhrase->aToken[i].pSegcsr = 0;
121781 122391       }
121782 122392     }
121783 122393   }
       122394  +
121784 122395   
121785 122396   /*
121786 122397   ** Return SQLITE_CORRUPT_VTAB.
121787 122398   */
121788 122399   #ifdef SQLITE_DEBUG
121789 122400   SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
121790 122401     return SQLITE_CORRUPT_VTAB;
................................................................................
125077 125688   
125078 125689   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
125079 125690   
125080 125691   /* #include <string.h> */
125081 125692   /* #include <assert.h> */
125082 125693   /* #include <stdlib.h> */
125083 125694   
       125695  +
       125696  +#define FTS_MAX_APPENDABLE_HEIGHT 16
       125697  +
125084 125698   /*
125085 125699   ** When full-text index nodes are loaded from disk, the buffer that they
125086 125700   ** are loaded into has the following number of bytes of padding at the end 
125087 125701   ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
125088 125702   ** of 920 bytes is allocated for it.
125089 125703   **
125090 125704   ** This means that if we have a pointer into a buffer containing node data,
................................................................................
125115 125729   int test_fts3_node_chunk_threshold = (4*1024)*4;
125116 125730   # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
125117 125731   # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
125118 125732   #else
125119 125733   # define FTS3_NODE_CHUNKSIZE (4*1024) 
125120 125734   # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
125121 125735   #endif
       125736  +
       125737  +/*
       125738  +** The two values that may be meaningfully bound to the :1 parameter in
       125739  +** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
       125740  +*/
       125741  +#define FTS_STAT_DOCTOTAL      0
       125742  +#define FTS_STAT_INCRMERGEHINT 1
       125743  +#define FTS_STAT_AUTOINCRMERGE 2
       125744  +
       125745  +/*
       125746  +** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
       125747  +** and incremental merge operation that takes place. This is used for 
       125748  +** debugging FTS only, it should not usually be turned on in production
       125749  +** systems.
       125750  +*/
       125751  +#ifdef FTS3_LOG_MERGES
       125752  +static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
       125753  +  sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
       125754  +}
       125755  +#else
       125756  +#define fts3LogMerge(x, y)
       125757  +#endif
       125758  +
125122 125759   
125123 125760   typedef struct PendingList PendingList;
125124 125761   typedef struct SegmentNode SegmentNode;
125125 125762   typedef struct SegmentWriter SegmentWriter;
125126 125763   
125127 125764   /*
125128 125765   ** An instance of the following data structure is used to build doclists
................................................................................
125277 125914   #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
125278 125915   #define SQL_DELETE_SEGDIR_LEVEL       16
125279 125916   #define SQL_DELETE_SEGMENTS_RANGE     17
125280 125917   #define SQL_CONTENT_INSERT            18
125281 125918   #define SQL_DELETE_DOCSIZE            19
125282 125919   #define SQL_REPLACE_DOCSIZE           20
125283 125920   #define SQL_SELECT_DOCSIZE            21
125284         -#define SQL_SELECT_DOCTOTAL           22
125285         -#define SQL_REPLACE_DOCTOTAL          23
       125921  +#define SQL_SELECT_STAT               22
       125922  +#define SQL_REPLACE_STAT              23
125286 125923   
125287 125924   #define SQL_SELECT_ALL_PREFIX_LEVEL   24
125288 125925   #define SQL_DELETE_ALL_TERMS_SEGDIR   25
125289         -
125290 125926   #define SQL_DELETE_SEGDIR_RANGE       26
125291         -
125292 125927   #define SQL_SELECT_ALL_LANGID         27
       125928  +#define SQL_FIND_MERGE_LEVEL          28
       125929  +#define SQL_MAX_LEAF_NODE_ESTIMATE    29
       125930  +#define SQL_DELETE_SEGDIR_ENTRY       30
       125931  +#define SQL_SHIFT_SEGDIR_ENTRY        31
       125932  +#define SQL_SELECT_SEGDIR             32
       125933  +#define SQL_CHOMP_SEGDIR              33
       125934  +#define SQL_SEGMENT_IS_APPENDABLE     34
       125935  +#define SQL_SELECT_INDEXES            35
       125936  +#define SQL_SELECT_MXLEVEL            36
125293 125937   
125294 125938   /*
125295 125939   ** This function is used to obtain an SQLite prepared statement handle
125296 125940   ** for the statement identified by the second argument. If successful,
125297 125941   ** *pp is set to the requested statement handle and SQLITE_OK returned.
125298 125942   ** Otherwise, an SQLite error code is returned and *pp is set to 0.
125299 125943   **
................................................................................
125314 125958   /* 2  */  "DELETE FROM %Q.'%q_content'",
125315 125959   /* 3  */  "DELETE FROM %Q.'%q_segments'",
125316 125960   /* 4  */  "DELETE FROM %Q.'%q_segdir'",
125317 125961   /* 5  */  "DELETE FROM %Q.'%q_docsize'",
125318 125962   /* 6  */  "DELETE FROM %Q.'%q_stat'",
125319 125963   /* 7  */  "SELECT %s WHERE rowid=?",
125320 125964   /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
125321         -/* 9  */  "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
       125965  +/* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
125322 125966   /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
125323         -/* 11 */  "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
       125967  +/* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
125324 125968   
125325 125969             /* Return segments in order from oldest to newest.*/ 
125326 125970   /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
125327 125971               "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
125328 125972   /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
125329 125973               "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
125330 125974               "ORDER BY level DESC, idx ASC",
................................................................................
125334 125978   
125335 125979   /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
125336 125980   /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
125337 125981   /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
125338 125982   /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
125339 125983   /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
125340 125984   /* 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,?)",
       125985  +/* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
       125986  +/* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
125343 125987   /* 24 */  "",
125344 125988   /* 25 */  "",
125345 125989   
125346 125990   /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
125347 125991   /* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'",
125348 125992   
       125993  +/* This statement is used to determine which level to read the input from
       125994  +** when performing an incremental merge. It returns the absolute level number
       125995  +** of the oldest level in the db that contains at least ? segments. Or,
       125996  +** if no level in the FTS index contains more than ? segments, the statement
       125997  +** returns zero rows.  */
       125998  +/* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
       125999  +         "  ORDER BY (level %% 1024) ASC LIMIT 1",
       126000  +
       126001  +/* Estimate the upper limit on the number of leaf nodes in a new segment
       126002  +** created by merging the oldest :2 segments from absolute level :1. See 
       126003  +** function sqlite3Fts3Incrmerge() for details.  */
       126004  +/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
       126005  +         "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
       126006  +
       126007  +/* SQL_DELETE_SEGDIR_ENTRY
       126008  +**   Delete the %_segdir entry on absolute level :1 with index :2.  */
       126009  +/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
       126010  +
       126011  +/* SQL_SHIFT_SEGDIR_ENTRY
       126012  +**   Modify the idx value for the segment with idx=:3 on absolute level :2
       126013  +**   to :1.  */
       126014  +/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
       126015  +
       126016  +/* SQL_SELECT_SEGDIR
       126017  +**   Read a single entry from the %_segdir table. The entry from absolute 
       126018  +**   level :1 with index value :2.  */
       126019  +/* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
       126020  +            "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
       126021  +
       126022  +/* SQL_CHOMP_SEGDIR
       126023  +**   Update the start_block (:1) and root (:2) fields of the %_segdir
       126024  +**   entry located on absolute level :3 with index :4.  */
       126025  +/* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
       126026  +            "WHERE level = ? AND idx = ?",
       126027  +
       126028  +/* SQL_SEGMENT_IS_APPENDABLE
       126029  +**   Return a single row if the segment with end_block=? is appendable. Or
       126030  +**   no rows otherwise.  */
       126031  +/* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
       126032  +
       126033  +/* SQL_SELECT_INDEXES
       126034  +**   Return the list of valid segment indexes for absolute level ?  */
       126035  +/* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
       126036  +
       126037  +/* SQL_SELECT_MXLEVEL
       126038  +**   Return the largest relative level in the FTS index or indexes.  */
       126039  +/* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
125349 126040     };
125350 126041     int rc = SQLITE_OK;
125351 126042     sqlite3_stmt *pStmt;
125352 126043   
125353 126044     assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
125354 126045     assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
125355 126046     
................................................................................
125378 126069       for(i=0; rc==SQLITE_OK && i<nParam; i++){
125379 126070         rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
125380 126071       }
125381 126072     }
125382 126073     *pp = pStmt;
125383 126074     return rc;
125384 126075   }
       126076  +
125385 126077   
125386 126078   static int fts3SelectDocsize(
125387 126079     Fts3Table *pTab,                /* FTS3 table handle */
125388         -  int eStmt,                      /* Either SQL_SELECT_DOCSIZE or DOCTOTAL */
125389 126080     sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
125390 126081     sqlite3_stmt **ppStmt           /* OUT: Statement handle */
125391 126082   ){
125392 126083     sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
125393 126084     int rc;                         /* Return code */
125394 126085   
125395         -  assert( eStmt==SQL_SELECT_DOCSIZE || eStmt==SQL_SELECT_DOCTOTAL );
125396         -
125397         -  rc = fts3SqlStmt(pTab, eStmt, &pStmt, 0);
       126086  +  rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
125398 126087     if( rc==SQLITE_OK ){
125399         -    if( eStmt==SQL_SELECT_DOCSIZE ){
125400         -      sqlite3_bind_int64(pStmt, 1, iDocid);
125401         -    }
       126088  +    sqlite3_bind_int64(pStmt, 1, iDocid);
125402 126089       rc = sqlite3_step(pStmt);
125403 126090       if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
125404 126091         rc = sqlite3_reset(pStmt);
125405 126092         if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
125406 126093         pStmt = 0;
125407 126094       }else{
125408 126095         rc = SQLITE_OK;
................................................................................
125413 126100     return rc;
125414 126101   }
125415 126102   
125416 126103   SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
125417 126104     Fts3Table *pTab,                /* Fts3 table handle */
125418 126105     sqlite3_stmt **ppStmt           /* OUT: Statement handle */
125419 126106   ){
125420         -  return fts3SelectDocsize(pTab, SQL_SELECT_DOCTOTAL, 0, ppStmt);
       126107  +  sqlite3_stmt *pStmt = 0;
       126108  +  int rc;
       126109  +  rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
       126110  +  if( rc==SQLITE_OK ){
       126111  +    sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
       126112  +    if( sqlite3_step(pStmt)!=SQLITE_ROW
       126113  +     || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
       126114  +    ){
       126115  +      rc = sqlite3_reset(pStmt);
       126116  +      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
       126117  +      pStmt = 0;
       126118  +    }
       126119  +  }
       126120  +  *ppStmt = pStmt;
       126121  +  return rc;
125421 126122   }
125422 126123   
125423 126124   SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
125424 126125     Fts3Table *pTab,                /* Fts3 table handle */
125425 126126     sqlite3_int64 iDocid,           /* Docid to read size data for */
125426 126127     sqlite3_stmt **ppStmt           /* OUT: Statement handle */
125427 126128   ){
125428         -  return fts3SelectDocsize(pTab, SQL_SELECT_DOCSIZE, iDocid, ppStmt);
       126129  +  return fts3SelectDocsize(pTab, iDocid, ppStmt);
125429 126130   }
125430 126131   
125431 126132   /*
125432 126133   ** Similar to fts3SqlStmt(). Except, after binding the parameters in
125433 126134   ** array apVal[] to the SQL statement identified by eStmt, the statement
125434 126135   ** is executed.
125435 126136   **
................................................................................
125511 126212   ** Language 1 indexes are allocated immediately following language 0.
125512 126213   **
125513 126214   ** So, for a system with nPrefix prefix indexes configured, the block of
125514 126215   ** absolute levels that corresponds to language-id iLangid and index 
125515 126216   ** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
125516 126217   */
125517 126218   static sqlite3_int64 getAbsoluteLevel(
125518         -  Fts3Table *p, 
125519         -  int iLangid, 
125520         -  int iIndex, 
125521         -  int iLevel
       126219  +  Fts3Table *p,                   /* FTS3 table handle */
       126220  +  int iLangid,                    /* Language id */
       126221  +  int iIndex,                     /* Index in p->aIndex[] */
       126222  +  int iLevel                      /* Level of segments */
125522 126223   ){
125523 126224     sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
125524 126225     assert( iLangid>=0 );
125525 126226     assert( p->nIndex>0 );
125526 126227     assert( iIndex>=0 && iIndex<p->nIndex );
125527 126228   
125528 126229     iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
125529 126230     return iBase + iLevel;
125530 126231   }
125531         -
125532 126232   
125533 126233   /*
125534 126234   ** Set *ppStmt to a statement handle that may be used to iterate through
125535 126235   ** all rows in the %_segdir table, from oldest to newest. If successful,
125536 126236   ** return SQLITE_OK. If an error occurs while preparing the statement, 
125537 126237   ** return an SQLite error code.
125538 126238   **
................................................................................
126091 126791     if( rc==SQLITE_OK ){
126092 126792       /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
126093 126793       ** full, merge all segments in level iLevel into a single iLevel+1
126094 126794       ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
126095 126795       ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
126096 126796       */
126097 126797       if( iNext>=FTS3_MERGE_COUNT ){
       126798  +      fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
126098 126799         rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
126099 126800         *piIdx = 0;
126100 126801       }else{
126101 126802         *piIdx = iNext;
126102 126803       }
126103 126804     }
126104 126805   
................................................................................
126138 126839     char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
126139 126840     int *pnBlob,                    /* OUT: Size of blob data */
126140 126841     int *pnLoad                     /* OUT: Bytes actually loaded */
126141 126842   ){
126142 126843     int rc;                         /* Return code */
126143 126844   
126144 126845     /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
126145         -  assert( pnBlob);
       126846  +  assert( pnBlob );
126146 126847   
126147 126848     if( p->pSegments ){
126148 126849       rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
126149 126850     }else{
126150 126851       if( 0==p->zSegmentsTbl ){
126151 126852         p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
126152 126853         if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
................................................................................
126479 127180   ){
126480 127181     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
126481 127182     int nOvfl = 0;
126482 127183     int ii;
126483 127184     int rc = SQLITE_OK;
126484 127185     int pgsz = p->nPgsz;
126485 127186   
126486         -  assert( p->bHasStat );
       127187  +  assert( p->bFts4 );
126487 127188     assert( pgsz>0 );
126488 127189   
126489 127190     for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
126490 127191       Fts3SegReader *pReader = pMsr->apSegment[ii];
126491 127192       if( !fts3SegReaderIsPending(pReader) 
126492 127193        && !fts3SegReaderIsRootOnly(pReader) 
126493 127194       ){
................................................................................
126836 127537       sqlite3_bind_int64(pStmt, 1, iBlock);
126837 127538       sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
126838 127539       sqlite3_step(pStmt);
126839 127540       rc = sqlite3_reset(pStmt);
126840 127541     }
126841 127542     return rc;
126842 127543   }
       127544  +
       127545  +/*
       127546  +** Find the largest relative level number in the table. If successful, set
       127547  +** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
       127548  +** set *pnMax to zero and return an SQLite error code.
       127549  +*/
       127550  +SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
       127551  +  int rc;
       127552  +  int mxLevel = 0;
       127553  +  sqlite3_stmt *pStmt = 0;
       127554  +
       127555  +  rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
       127556  +  if( rc==SQLITE_OK ){
       127557  +    if( SQLITE_ROW==sqlite3_step(pStmt) ){
       127558  +      mxLevel = sqlite3_column_int(pStmt, 0);
       127559  +    }
       127560  +    rc = sqlite3_reset(pStmt);
       127561  +  }
       127562  +  *pnMax = mxLevel;
       127563  +  return rc;
       127564  +}
126843 127565   
126844 127566   /* 
126845 127567   ** Insert a record into the %_segdir table.
126846 127568   */
126847 127569   static int fts3WriteSegdir(
126848 127570     Fts3Table *p,                   /* Virtual table handle */
126849 127571     sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
................................................................................
127153 127875   
127154 127876     if( nData>0 && nData+nReq>p->nNodeSize ){
127155 127877       int rc;
127156 127878   
127157 127879       /* The current leaf node is full. Write it out to the database. */
127158 127880       rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
127159 127881       if( rc!=SQLITE_OK ) return rc;
       127882  +    p->nLeafAdd++;
127160 127883   
127161 127884       /* Add the current term to the interior node tree. The term added to
127162 127885       ** the interior tree must:
127163 127886       **
127164 127887       **   a) be greater than the largest term on the leaf node just written
127165 127888       **      to the database (still available in pWriter->zTerm), and
127166 127889       **
................................................................................
127261 127984             p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
127262 127985       }
127263 127986     }else{
127264 127987       /* The entire tree fits on the root node. Write it to the segdir table. */
127265 127988       rc = fts3WriteSegdir(
127266 127989           p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
127267 127990     }
       127991  +  p->nLeafAdd++;
127268 127992     return rc;
127269 127993   }
127270 127994   
127271 127995   /*
127272 127996   ** Release all memory held by the SegmentWriter object passed as the 
127273 127997   ** first argument.
127274 127998   */
................................................................................
127341 128065         getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
127342 128066     );
127343 128067     if( SQLITE_ROW==sqlite3_step(pStmt) ){
127344 128068       *pnMax = sqlite3_column_int64(pStmt, 0);
127345 128069     }
127346 128070     return sqlite3_reset(pStmt);
127347 128071   }
       128072  +
       128073  +/*
       128074  +** Delete all entries in the %_segments table associated with the segment
       128075  +** opened with seg-reader pSeg. This function does not affect the contents
       128076  +** of the %_segdir table.
       128077  +*/
       128078  +static int fts3DeleteSegment(
       128079  +  Fts3Table *p,                   /* FTS table handle */
       128080  +  Fts3SegReader *pSeg             /* Segment to delete */
       128081  +){
       128082  +  int rc = SQLITE_OK;             /* Return code */
       128083  +  if( pSeg->iStartBlock ){
       128084  +    sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
       128085  +    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
       128086  +    if( rc==SQLITE_OK ){
       128087  +      sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
       128088  +      sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
       128089  +      sqlite3_step(pDelete);
       128090  +      rc = sqlite3_reset(pDelete);
       128091  +    }
       128092  +  }
       128093  +  return rc;
       128094  +}
127348 128095   
127349 128096   /*
127350 128097   ** This function is used after merging multiple segments into a single large
127351 128098   ** segment to delete the old, now redundant, segment b-trees. Specifically,
127352 128099   ** it:
127353 128100   ** 
127354 128101   **   1) Deletes all %_segments entries for the segments associated with 
................................................................................
127364 128111     Fts3Table *p,                   /* Virtual table handle */
127365 128112     int iLangid,                    /* Language id */
127366 128113     int iIndex,                     /* Index for p->aIndex */
127367 128114     int iLevel,                     /* Level of %_segdir entries to delete */
127368 128115     Fts3SegReader **apSegment,      /* Array of SegReader objects */
127369 128116     int nReader                     /* Size of array apSegment */
127370 128117   ){
127371         -  int rc;                         /* Return Code */
       128118  +  int rc = SQLITE_OK;             /* Return Code */
127372 128119     int i;                          /* Iterator variable */
127373         -  sqlite3_stmt *pDelete;          /* SQL statement to delete rows */
       128120  +  sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
127374 128121   
127375         -  rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
127376 128122     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         -    }
       128123  +    rc = fts3DeleteSegment(p, apSegment[i]);
127384 128124     }
127385 128125     if( rc!=SQLITE_OK ){
127386 128126       return rc;
127387 128127     }
127388 128128   
127389 128129     assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
127390 128130     if( iLevel==FTS3_SEGCURSOR_ALL ){
................................................................................
127952 128692   
127953 128693   /* 
127954 128694   ** Flush the contents of pendingTerms to level 0 segments.
127955 128695   */
127956 128696   SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
127957 128697     int rc = SQLITE_OK;
127958 128698     int i;
       128699  +        
127959 128700     for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
127960 128701       rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
127961 128702       if( rc==SQLITE_DONE ) rc = SQLITE_OK;
127962 128703     }
127963 128704     sqlite3Fts3PendingTermsClear(p);
       128705  +
       128706  +  /* Determine the auto-incr-merge setting if unknown.  If enabled,
       128707  +  ** estimate the number of leaf blocks of content to be written
       128708  +  */
       128709  +  if( rc==SQLITE_OK && p->bHasStat
       128710  +   && p->bAutoincrmerge==0xff && p->nLeafAdd>0
       128711  +  ){
       128712  +    sqlite3_stmt *pStmt = 0;
       128713  +    rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
       128714  +    if( rc==SQLITE_OK ){
       128715  +      sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
       128716  +      rc = sqlite3_step(pStmt);
       128717  +      p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
       128718  +      rc = sqlite3_reset(pStmt);
       128719  +    }
       128720  +  }
127964 128721     return rc;
127965 128722   }
127966 128723   
127967 128724   /*
127968 128725   ** Encode N integers as varints into a blob.
127969 128726   */
127970 128727   static void fts3EncodeIntArray(
................................................................................
128067 128824     if( *pRC ) return;
128068 128825     a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
128069 128826     if( a==0 ){
128070 128827       *pRC = SQLITE_NOMEM;
128071 128828       return;
128072 128829     }
128073 128830     pBlob = (char*)&a[nStat];
128074         -  rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0);
       128831  +  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
128075 128832     if( rc ){
128076 128833       sqlite3_free(a);
128077 128834       *pRC = rc;
128078 128835       return;
128079 128836     }
       128837  +  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
128080 128838     if( sqlite3_step(pStmt)==SQLITE_ROW ){
128081 128839       fts3DecodeIntArray(nStat, a,
128082 128840            sqlite3_column_blob(pStmt, 0),
128083 128841            sqlite3_column_bytes(pStmt, 0));
128084 128842     }else{
128085 128843       memset(a, 0, sizeof(u32)*(nStat) );
128086 128844     }
................................................................................
128096 128854         x = 0;
128097 128855       }else{
128098 128856         x = x + aSzIns[i] - aSzDel[i];
128099 128857       }
128100 128858       a[i+1] = x;
128101 128859     }
128102 128860     fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
128103         -  rc = fts3SqlStmt(p, SQL_REPLACE_DOCTOTAL, &pStmt, 0);
       128861  +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
128104 128862     if( rc ){
128105 128863       sqlite3_free(a);
128106 128864       *pRC = rc;
128107 128865       return;
128108 128866     }
128109         -  sqlite3_bind_blob(pStmt, 1, pBlob, nBlob, SQLITE_STATIC);
       128867  +  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
       128868  +  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
128110 128869     sqlite3_step(pStmt);
128111 128870     *pRC = sqlite3_reset(pStmt);
128112 128871     sqlite3_free(a);
128113 128872   }
128114 128873   
128115 128874   /*
128116 128875   ** Merge the entire database so that there is one segment for each 
................................................................................
128207 128966         }else{
128208 128967           nEntry++;
128209 128968           for(iCol=0; iCol<=p->nColumn; iCol++){
128210 128969             aSzIns[iCol] += aSz[iCol];
128211 128970           }
128212 128971         }
128213 128972       }
128214         -    if( p->bHasStat ){
       128973  +    if( p->bFts4 ){
128215 128974         fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
128216 128975       }
128217 128976       sqlite3_free(aSz);
128218 128977   
128219 128978       if( pStmt ){
128220 128979         int rc2 = sqlite3_finalize(pStmt);
128221 128980         if( rc==SQLITE_OK ){
................................................................................
128223 128982         }
128224 128983       }
128225 128984     }
128226 128985   
128227 128986     return rc;
128228 128987   }
128229 128988   
       128989  +
       128990  +/*
       128991  +** This function opens a cursor used to read the input data for an 
       128992  +** incremental merge operation. Specifically, it opens a cursor to scan
       128993  +** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute 
       128994  +** level iAbsLevel.
       128995  +*/
       128996  +static int fts3IncrmergeCsr(
       128997  +  Fts3Table *p,                   /* FTS3 table handle */
       128998  +  sqlite3_int64 iAbsLevel,        /* Absolute level to open */
       128999  +  int nSeg,                       /* Number of segments to merge */
       129000  +  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
       129001  +){
       129002  +  int rc;                         /* Return Code */
       129003  +  sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */  
       129004  +  int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
       129005  +
       129006  +  /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
       129007  +  memset(pCsr, 0, sizeof(*pCsr));
       129008  +  nByte = sizeof(Fts3SegReader *) * nSeg;
       129009  +  pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
       129010  +
       129011  +  if( pCsr->apSegment==0 ){
       129012  +    rc = SQLITE_NOMEM;
       129013  +  }else{
       129014  +    memset(pCsr->apSegment, 0, nByte);
       129015  +    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
       129016  +  }
       129017  +  if( rc==SQLITE_OK ){
       129018  +    int i;
       129019  +    int rc2;
       129020  +    sqlite3_bind_int64(pStmt, 1, iAbsLevel);
       129021  +    assert( pCsr->nSegment==0 );
       129022  +    for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
       129023  +      rc = sqlite3Fts3SegReaderNew(i, 0,
       129024  +          sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
       129025  +          sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
       129026  +          sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
       129027  +          sqlite3_column_blob(pStmt, 4),         /* segdir.root */
       129028  +          sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
       129029  +          &pCsr->apSegment[i]
       129030  +      );
       129031  +      pCsr->nSegment++;
       129032  +    }
       129033  +    rc2 = sqlite3_reset(pStmt);
       129034  +    if( rc==SQLITE_OK ) rc = rc2;
       129035  +  }
       129036  +
       129037  +  return rc;
       129038  +}
       129039  +
       129040  +typedef struct IncrmergeWriter IncrmergeWriter;
       129041  +typedef struct NodeWriter NodeWriter;
       129042  +typedef struct Blob Blob;
       129043  +typedef struct NodeReader NodeReader;
       129044  +
       129045  +/*
       129046  +** An instance of the following structure is used as a dynamic buffer
       129047  +** to build up nodes or other blobs of data in.
       129048  +**
       129049  +** The function blobGrowBuffer() is used to extend the allocation.
       129050  +*/
       129051  +struct Blob {
       129052  +  char *a;                        /* Pointer to allocation */
       129053  +  int n;                          /* Number of valid bytes of data in a[] */
       129054  +  int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
       129055  +};
       129056  +
       129057  +/*
       129058  +** This structure is used to build up buffers containing segment b-tree 
       129059  +** nodes (blocks).
       129060  +*/
       129061  +struct NodeWriter {
       129062  +  sqlite3_int64 iBlock;           /* Current block id */
       129063  +  Blob key;                       /* Last key written to the current block */
       129064  +  Blob block;                     /* Current block image */
       129065  +};
       129066  +
       129067  +/*
       129068  +** An object of this type contains the state required to create or append
       129069  +** to an appendable b-tree segment.
       129070  +*/
       129071  +struct IncrmergeWriter {
       129072  +  int nLeafEst;                   /* Space allocated for leaf blocks */
       129073  +  int nWork;                      /* Number of leaf pages flushed */
       129074  +  sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
       129075  +  int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
       129076  +  sqlite3_int64 iStart;           /* Block number of first allocated block */
       129077  +  sqlite3_int64 iEnd;             /* Block number of last allocated block */
       129078  +  NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
       129079  +};
       129080  +
       129081  +/*
       129082  +** An object of the following type is used to read data from a single
       129083  +** FTS segment node. See the following functions:
       129084  +**
       129085  +**     nodeReaderInit()
       129086  +**     nodeReaderNext()
       129087  +**     nodeReaderRelease()
       129088  +*/
       129089  +struct NodeReader {
       129090  +  const char *aNode;
       129091  +  int nNode;
       129092  +  int iOff;                       /* Current offset within aNode[] */
       129093  +
       129094  +  /* Output variables. Containing the current node entry. */
       129095  +  sqlite3_int64 iChild;           /* Pointer to child node */
       129096  +  Blob term;                      /* Current term */
       129097  +  const char *aDoclist;           /* Pointer to doclist */
       129098  +  int nDoclist;                   /* Size of doclist in bytes */
       129099  +};
       129100  +
       129101  +/*
       129102  +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
       129103  +** Otherwise, if the allocation at pBlob->a is not already at least nMin
       129104  +** bytes in size, extend (realloc) it to be so.
       129105  +**
       129106  +** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
       129107  +** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
       129108  +** to reflect the new size of the pBlob->a[] buffer.
       129109  +*/
       129110  +static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
       129111  +  if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
       129112  +    int nAlloc = nMin;
       129113  +    char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
       129114  +    if( a ){
       129115  +      pBlob->nAlloc = nAlloc;
       129116  +      pBlob->a = a;
       129117  +    }else{
       129118  +      *pRc = SQLITE_NOMEM;
       129119  +    }
       129120  +  }
       129121  +}
       129122  +
       129123  +/*
       129124  +** Attempt to advance the node-reader object passed as the first argument to
       129125  +** the next entry on the node. 
       129126  +**
       129127  +** Return an error code if an error occurs (SQLITE_NOMEM is possible). 
       129128  +** Otherwise return SQLITE_OK. If there is no next entry on the node
       129129  +** (e.g. because the current entry is the last) set NodeReader->aNode to
       129130  +** NULL to indicate EOF. Otherwise, populate the NodeReader structure output 
       129131  +** variables for the new entry.
       129132  +*/
       129133  +static int nodeReaderNext(NodeReader *p){
       129134  +  int bFirst = (p->term.n==0);    /* True for first term on the node */
       129135  +  int nPrefix = 0;                /* Bytes to copy from previous term */
       129136  +  int nSuffix = 0;                /* Bytes to append to the prefix */
       129137  +  int rc = SQLITE_OK;             /* Return code */
       129138  +
       129139  +  assert( p->aNode );
       129140  +  if( p->iChild && bFirst==0 ) p->iChild++;
       129141  +  if( p->iOff>=p->nNode ){
       129142  +    /* EOF */
       129143  +    p->aNode = 0;
       129144  +  }else{
       129145  +    if( bFirst==0 ){
       129146  +      p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
       129147  +    }
       129148  +    p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
       129149  +
       129150  +    blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
       129151  +    if( rc==SQLITE_OK ){
       129152  +      memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
       129153  +      p->term.n = nPrefix+nSuffix;
       129154  +      p->iOff += nSuffix;
       129155  +      if( p->iChild==0 ){
       129156  +        p->iOff += sqlite3Fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
       129157  +        p->aDoclist = &p->aNode[p->iOff];
       129158  +        p->iOff += p->nDoclist;
       129159  +      }
       129160  +    }
       129161  +  }
       129162  +
       129163  +  assert( p->iOff<=p->nNode );
       129164  +
       129165  +  return rc;
       129166  +}
       129167  +
       129168  +/*
       129169  +** Release all dynamic resources held by node-reader object *p.
       129170  +*/
       129171  +static void nodeReaderRelease(NodeReader *p){
       129172  +  sqlite3_free(p->term.a);
       129173  +}
       129174  +
       129175  +/*
       129176  +** Initialize a node-reader object to read the node in buffer aNode/nNode.
       129177  +**
       129178  +** If successful, SQLITE_OK is returned and the NodeReader object set to 
       129179  +** point to the first entry on the node (if any). Otherwise, an SQLite
       129180  +** error code is returned.
       129181  +*/
       129182  +static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
       129183  +  memset(p, 0, sizeof(NodeReader));
       129184  +  p->aNode = aNode;
       129185  +  p->nNode = nNode;
       129186  +
       129187  +  /* Figure out if this is a leaf or an internal node. */
       129188  +  if( p->aNode[0] ){
       129189  +    /* An internal node. */
       129190  +    p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
       129191  +  }else{
       129192  +    p->iOff = 1;
       129193  +  }
       129194  +
       129195  +  return nodeReaderNext(p);
       129196  +}
       129197  +
       129198  +/*
       129199  +** This function is called while writing an FTS segment each time a leaf o
       129200  +** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
       129201  +** to be greater than the largest key on the node just written, but smaller
       129202  +** than or equal to the first key that will be written to the next leaf
       129203  +** node.
       129204  +**
       129205  +** The block id of the leaf node just written to disk may be found in
       129206  +** (pWriter->aNodeWriter[0].iBlock) when this function is called.
       129207  +*/
       129208  +static int fts3IncrmergePush(
       129209  +  Fts3Table *p,                   /* Fts3 table handle */
       129210  +  IncrmergeWriter *pWriter,       /* Writer object */
       129211  +  const char *zTerm,              /* Term to write to internal node */
       129212  +  int nTerm                       /* Bytes at zTerm */
       129213  +){
       129214  +  sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
       129215  +  int iLayer;
       129216  +
       129217  +  assert( nTerm>0 );
       129218  +  for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
       129219  +    sqlite3_int64 iNextPtr = 0;
       129220  +    NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
       129221  +    int rc = SQLITE_OK;
       129222  +    int nPrefix;
       129223  +    int nSuffix;
       129224  +    int nSpace;
       129225  +
       129226  +    /* Figure out how much space the key will consume if it is written to
       129227  +    ** the current node of layer iLayer. Due to the prefix compression, 
       129228  +    ** the space required changes depending on which node the key is to
       129229  +    ** be added to.  */
       129230  +    nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
       129231  +    nSuffix = nTerm - nPrefix;
       129232  +    nSpace  = sqlite3Fts3VarintLen(nPrefix);
       129233  +    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
       129234  +
       129235  +    if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){ 
       129236  +      /* If the current node of layer iLayer contains zero keys, or if adding
       129237  +      ** the key to it will not cause it to grow to larger than nNodeSize 
       129238  +      ** bytes in size, write the key here.  */
       129239  +
       129240  +      Blob *pBlk = &pNode->block;
       129241  +      if( pBlk->n==0 ){
       129242  +        blobGrowBuffer(pBlk, p->nNodeSize, &rc);
       129243  +        if( rc==SQLITE_OK ){
       129244  +          pBlk->a[0] = (char)iLayer;
       129245  +          pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
       129246  +        }
       129247  +      }
       129248  +      blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
       129249  +      blobGrowBuffer(&pNode->key, nTerm, &rc);
       129250  +
       129251  +      if( rc==SQLITE_OK ){
       129252  +        if( pNode->key.n ){
       129253  +          pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
       129254  +        }
       129255  +        pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
       129256  +        memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
       129257  +        pBlk->n += nSuffix;
       129258  +
       129259  +        memcpy(pNode->key.a, zTerm, nTerm);
       129260  +        pNode->key.n = nTerm;
       129261  +      }
       129262  +    }else{
       129263  +      /* Otherwise, flush the the current node of layer iLayer to disk.
       129264  +      ** Then allocate a new, empty sibling node. The key will be written
       129265  +      ** into the parent of this node. */
       129266  +      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
       129267  +
       129268  +      assert( pNode->block.nAlloc>=p->nNodeSize );
       129269  +      pNode->block.a[0] = (char)iLayer;
       129270  +      pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
       129271  +
       129272  +      iNextPtr = pNode->iBlock;
       129273  +      pNode->iBlock++;
       129274  +      pNode->key.n = 0;
       129275  +    }
       129276  +
       129277  +    if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
       129278  +    iPtr = iNextPtr;
       129279  +  }
       129280  +
       129281  +  assert( 0 );
       129282  +  return 0;
       129283  +}
       129284  +
       129285  +/*
       129286  +** Append a term and (optionally) doclist to the FTS segment node currently
       129287  +** stored in blob *pNode. The node need not contain any terms, but the
       129288  +** header must be written before this function is called.
       129289  +**
       129290  +** A node header is a single 0x00 byte for a leaf node, or a height varint
       129291  +** followed by the left-hand-child varint for an internal node.
       129292  +**
       129293  +** The term to be appended is passed via arguments zTerm/nTerm. For a 
       129294  +** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
       129295  +** node, both aDoclist and nDoclist must be passed 0.
       129296  +**
       129297  +** If the size of the value in blob pPrev is zero, then this is the first
       129298  +** term written to the node. Otherwise, pPrev contains a copy of the 
       129299  +** previous term. Before this function returns, it is updated to contain a
       129300  +** copy of zTerm/nTerm.
       129301  +**
       129302  +** It is assumed that the buffer associated with pNode is already large
       129303  +** enough to accommodate the new entry. The buffer associated with pPrev
       129304  +** is extended by this function if requrired.
       129305  +**
       129306  +** If an error (i.e. OOM condition) occurs, an SQLite error code is
       129307  +** returned. Otherwise, SQLITE_OK.
       129308  +*/
       129309  +static int fts3AppendToNode(
       129310  +  Blob *pNode,                    /* Current node image to append to */
       129311  +  Blob *pPrev,                    /* Buffer containing previous term written */
       129312  +  const char *zTerm,              /* New term to write */
       129313  +  int nTerm,                      /* Size of zTerm in bytes */
       129314  +  const char *aDoclist,           /* Doclist (or NULL) to write */
       129315  +  int nDoclist                    /* Size of aDoclist in bytes */ 
       129316  +){
       129317  +  int rc = SQLITE_OK;             /* Return code */
       129318  +  int bFirst = (pPrev->n==0);     /* True if this is the first term written */
       129319  +  int nPrefix;                    /* Size of term prefix in bytes */
       129320  +  int nSuffix;                    /* Size of term suffix in bytes */
       129321  +
       129322  +  /* Node must have already been started. There must be a doclist for a
       129323  +  ** leaf node, and there must not be a doclist for an internal node.  */
       129324  +  assert( pNode->n>0 );
       129325  +  assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
       129326  +
       129327  +  blobGrowBuffer(pPrev, nTerm, &rc);
       129328  +  if( rc!=SQLITE_OK ) return rc;
       129329  +
       129330  +  nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
       129331  +  nSuffix = nTerm - nPrefix;
       129332  +  memcpy(pPrev->a, zTerm, nTerm);
       129333  +  pPrev->n = nTerm;
       129334  +
       129335  +  if( bFirst==0 ){
       129336  +    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
       129337  +  }
       129338  +  pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
       129339  +  memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
       129340  +  pNode->n += nSuffix;
       129341  +
       129342  +  if( aDoclist ){
       129343  +    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
       129344  +    memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
       129345  +    pNode->n += nDoclist;
       129346  +  }
       129347  +
       129348  +  assert( pNode->n<=pNode->nAlloc );
       129349  +
       129350  +  return SQLITE_OK;
       129351  +}
       129352  +
       129353  +/*
       129354  +** Append the current term and doclist pointed to by cursor pCsr to the
       129355  +** appendable b-tree segment opened for writing by pWriter.
       129356  +**
       129357  +** Return SQLITE_OK if successful, or an SQLite error code otherwise.
       129358  +*/
       129359  +static int fts3IncrmergeAppend(
       129360  +  Fts3Table *p,                   /* Fts3 table handle */
       129361  +  IncrmergeWriter *pWriter,       /* Writer object */
       129362  +  Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
       129363  +){
       129364  +  const char *zTerm = pCsr->zTerm;
       129365  +  int nTerm = pCsr->nTerm;
       129366  +  const char *aDoclist = pCsr->aDoclist;
       129367  +  int nDoclist = pCsr->nDoclist;
       129368  +  int rc = SQLITE_OK;           /* Return code */
       129369  +  int nSpace;                   /* Total space in bytes required on leaf */
       129370  +  int nPrefix;                  /* Size of prefix shared with previous term */
       129371  +  int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
       129372  +  NodeWriter *pLeaf;            /* Object used to write leaf nodes */
       129373  +
       129374  +  pLeaf = &pWriter->aNodeWriter[0];
       129375  +  nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
       129376  +  nSuffix = nTerm - nPrefix;
       129377  +
       129378  +  nSpace  = sqlite3Fts3VarintLen(nPrefix);
       129379  +  nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
       129380  +  nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
       129381  +
       129382  +  /* If the current block is not empty, and if adding this term/doclist
       129383  +  ** to the current block would make it larger than Fts3Table.nNodeSize
       129384  +  ** bytes, write this block out to the database. */
       129385  +  if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
       129386  +    rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
       129387  +    pWriter->nWork++;
       129388  +
       129389  +    /* Add the current term to the parent node. The term added to the 
       129390  +    ** parent must:
       129391  +    **
       129392  +    **   a) be greater than the largest term on the leaf node just written
       129393  +    **      to the database (still available in pLeaf->key), and
       129394  +    **
       129395  +    **   b) be less than or equal to the term about to be added to the new
       129396  +    **      leaf node (zTerm/nTerm).
       129397  +    **
       129398  +    ** In other words, it must be the prefix of zTerm 1 byte longer than
       129399  +    ** the common prefix (if any) of zTerm and pWriter->zTerm.
       129400  +    */
       129401  +    if( rc==SQLITE_OK ){
       129402  +      rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
       129403  +    }
       129404  +
       129405  +    /* Advance to the next output block */
       129406  +    pLeaf->iBlock++;
       129407  +    pLeaf->key.n = 0;
       129408  +    pLeaf->block.n = 0;
       129409  +
       129410  +    nSuffix = nTerm;
       129411  +    nSpace  = 1;
       129412  +    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
       129413  +    nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
       129414  +  }
       129415  +
       129416  +  blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
       129417  +
       129418  +  if( rc==SQLITE_OK ){
       129419  +    if( pLeaf->block.n==0 ){
       129420  +      pLeaf->block.n = 1;
       129421  +      pLeaf->block.a[0] = '\0';
       129422  +    }
       129423  +    rc = fts3AppendToNode(
       129424  +        &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
       129425  +    );
       129426  +  }
       129427  +
       129428  +  return rc;
       129429  +}
       129430  +
       129431  +/*
       129432  +** This function is called to release all dynamic resources held by the
       129433  +** merge-writer object pWriter, and if no error has occurred, to flush
       129434  +** all outstanding node buffers held by pWriter to disk.
       129435  +**
       129436  +** If *pRc is not SQLITE_OK when this function is called, then no attempt
       129437  +** is made to write any data to disk. Instead, this function serves only
       129438  +** to release outstanding resources.
       129439  +**
       129440  +** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
       129441  +** flushing buffers to disk, *pRc is set to an SQLite error code before
       129442  +** returning.
       129443  +*/
       129444  +static void fts3IncrmergeRelease(
       129445  +  Fts3Table *p,                   /* FTS3 table handle */
       129446  +  IncrmergeWriter *pWriter,       /* Merge-writer object */
       129447  +  int *pRc                        /* IN/OUT: Error code */
       129448  +){
       129449  +  int i;                          /* Used to iterate through non-root layers */
       129450  +  int iRoot;                      /* Index of root in pWriter->aNodeWriter */
       129451  +  NodeWriter *pRoot;              /* NodeWriter for root node */
       129452  +  int rc = *pRc;                  /* Error code */
       129453  +
       129454  +  /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment 
       129455  +  ** root node. If the segment fits entirely on a single leaf node, iRoot
       129456  +  ** will be set to 0. If the root node is the parent of the leaves, iRoot
       129457  +  ** will be 1. And so on.  */
       129458  +  for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
       129459  +    NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
       129460  +    if( pNode->block.n>0 ) break;
       129461  +    assert( *pRc || pNode->block.nAlloc==0 );
       129462  +    assert( *pRc || pNode->key.nAlloc==0 );
       129463  +    sqlite3_free(pNode->block.a);
       129464  +    sqlite3_free(pNode->key.a);
       129465  +  }
       129466  +
       129467  +  /* Empty output segment. This is a no-op. */
       129468  +  if( iRoot<0 ) return;
       129469  +
       129470  +  /* The entire output segment fits on a single node. Normally, this means
       129471  +  ** the node would be stored as a blob in the "root" column of the %_segdir
       129472  +  ** table. However, this is not permitted in this case. The problem is that 
       129473  +  ** space has already been reserved in the %_segments table, and so the 
       129474  +  ** start_block and end_block fields of the %_segdir table must be populated. 
       129475  +  ** And, by design or by accident, released versions of FTS cannot handle 
       129476  +  ** segments that fit entirely on the root node with start_block!=0.
       129477  +  **
       129478  +  ** Instead, create a synthetic root node that contains nothing but a 
       129479  +  ** pointer to the single content node. So that the segment consists of a
       129480  +  ** single leaf and a single interior (root) node.
       129481  +  **
       129482  +  ** Todo: Better might be to defer allocating space in the %_segments 
       129483  +  ** table until we are sure it is needed.
       129484  +  */
       129485  +  if( iRoot==0 ){
       129486  +    Blob *pBlock = &pWriter->aNodeWriter[1].block;
       129487  +    blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
       129488  +    if( rc==SQLITE_OK ){
       129489  +      pBlock->a[0] = 0x01;
       129490  +      pBlock->n = 1 + sqlite3Fts3PutVarint(
       129491  +          &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
       129492  +      );
       129493  +    }
       129494  +    iRoot = 1;
       129495  +  }
       129496  +  pRoot = &pWriter->aNodeWriter[iRoot];
       129497  +
       129498  +  /* Flush all currently outstanding nodes to disk. */
       129499  +  for(i=0; i<iRoot; i++){
       129500  +    NodeWriter *pNode = &pWriter->aNodeWriter[i];
       129501  +    if( pNode->block.n>0 && rc==SQLITE_OK ){
       129502  +      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
       129503  +    }
       129504  +    sqlite3_free(pNode->block.a);
       129505  +    sqlite3_free(pNode->key.a);
       129506  +  }
       129507  +
       129508  +  /* Write the %_segdir record. */
       129509  +  if( rc==SQLITE_OK ){
       129510  +    rc = fts3WriteSegdir(p, 
       129511  +        pWriter->iAbsLevel+1,               /* level */
       129512  +        pWriter->iIdx,                      /* idx */
       129513  +        pWriter->iStart,                    /* start_block */
       129514  +        pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
       129515  +        pWriter->iEnd,                      /* end_block */
       129516  +        pRoot->block.a, pRoot->block.n      /* root */
       129517  +    );
       129518  +  }
       129519  +  sqlite3_free(pRoot->block.a);
       129520  +  sqlite3_free(pRoot->key.a);
       129521  +
       129522  +  *pRc = rc;
       129523  +}
       129524  +
       129525  +/*
       129526  +** Compare the term in buffer zLhs (size in bytes nLhs) with that in
       129527  +** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
       129528  +** the other, it is considered to be smaller than the other.
       129529  +**
       129530  +** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
       129531  +** if it is greater.
       129532  +*/
       129533  +static int fts3TermCmp(
       129534  +  const char *zLhs, int nLhs,     /* LHS of comparison */
       129535  +  const char *zRhs, int nRhs      /* RHS of comparison */
       129536  +){
       129537  +  int nCmp = MIN(nLhs, nRhs);
       129538  +  int res;
       129539  +
       129540  +  res = memcmp(zLhs, zRhs, nCmp);
       129541  +  if( res==0 ) res = nLhs - nRhs;
       129542  +
       129543  +  return res;
       129544  +}
       129545  +
       129546  +
       129547  +/*
       129548  +** Query to see if the entry in the %_segments table with blockid iEnd is 
       129549  +** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
       129550  +** returning. Otherwise, set *pbRes to 0. 
       129551  +**
       129552  +** Or, if an error occurs while querying the database, return an SQLite 
       129553  +** error code. The final value of *pbRes is undefined in this case.
       129554  +**
       129555  +** This is used to test if a segment is an "appendable" segment. If it
       129556  +** is, then a NULL entry has been inserted into the %_segments table
       129557  +** with blockid %_segdir.end_block.
       129558  +*/
       129559  +static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
       129560  +  int bRes = 0;                   /* Result to set *pbRes to */
       129561  +  sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
       129562  +  int rc;                         /* Return code */
       129563  +
       129564  +  rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
       129565  +  if( rc==SQLITE_OK ){
       129566  +    sqlite3_bind_int64(pCheck, 1, iEnd);
       129567  +    if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
       129568  +    rc = sqlite3_reset(pCheck);
       129569  +  }
       129570  +  
       129571  +  *pbRes = bRes;
       129572  +  return rc;
       129573  +}
       129574  +
       129575  +/*
       129576  +** This function is called when initializing an incremental-merge operation.
       129577  +** It checks if the existing segment with index value iIdx at absolute level 
       129578  +** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
       129579  +** merge-writer object *pWriter is initialized to write to it.
       129580  +**
       129581  +** An existing segment can be appended to by an incremental merge if:
       129582  +**
       129583  +**   * It was initially created as an appendable segment (with all required
       129584  +**     space pre-allocated), and
       129585  +**
       129586  +**   * The first key read from the input (arguments zKey and nKey) is 
       129587  +**     greater than the largest key currently stored in the potential
       129588  +**     output segment.
       129589  +*/
       129590  +static int fts3IncrmergeLoad(
       129591  +  Fts3Table *p,                   /* Fts3 table handle */
       129592  +  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
       129593  +  int iIdx,                       /* Index of candidate output segment */
       129594  +  const char *zKey,               /* First key to write */
       129595  +  int nKey,                       /* Number of bytes in nKey */
       129596  +  IncrmergeWriter *pWriter        /* Populate this object */
       129597  +){
       129598  +  int rc;                         /* Return code */
       129599  +  sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
       129600  +
       129601  +  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
       129602  +  if( rc==SQLITE_OK ){
       129603  +    sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
       129604  +    sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
       129605  +    sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
       129606  +    const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
       129607  +    int nRoot = 0;                /* Size of aRoot[] in bytes */
       129608  +    int rc2;                      /* Return code from sqlite3_reset() */
       129609  +    int bAppendable = 0;          /* Set to true if segment is appendable */
       129610  +
       129611  +    /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
       129612  +    sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
       129613  +    sqlite3_bind_int(pSelect, 2, iIdx);
       129614  +    if( sqlite3_step(pSelect)==SQLITE_ROW ){
       129615  +      iStart = sqlite3_column_int64(pSelect, 1);
       129616  +      iLeafEnd = sqlite3_column_int64(pSelect, 2);
       129617  +      iEnd = sqlite3_column_int64(pSelect, 3);
       129618  +      nRoot = sqlite3_column_bytes(pSelect, 4);
       129619  +      aRoot = sqlite3_column_blob(pSelect, 4);
       129620  +    }else{
       129621  +      return sqlite3_reset(pSelect);
       129622  +    }
       129623  +
       129624  +    /* Check for the zero-length marker in the %_segments table */
       129625  +    rc = fts3IsAppendable(p, iEnd, &bAppendable);
       129626  +
       129627  +    /* Check that zKey/nKey is larger than the largest key the candidate */
       129628  +    if( rc==SQLITE_OK && bAppendable ){
       129629  +      char *aLeaf = 0;
       129630  +      int nLeaf = 0;
       129631  +
       129632  +      rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
       129633  +      if( rc==SQLITE_OK ){
       129634  +        NodeReader reader;
       129635  +        for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
       129636  +            rc==SQLITE_OK && reader.aNode;
       129637  +            rc = nodeReaderNext(&reader)
       129638  +        ){
       129639  +          assert( reader.aNode );
       129640  +        }
       129641  +        if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
       129642  +          bAppendable = 0;
       129643  +        }
       129644  +        nodeReaderRelease(&reader);
       129645  +      }
       129646  +      sqlite3_free(aLeaf);
       129647  +    }
       129648  +
       129649  +    if( rc==SQLITE_OK && bAppendable ){
       129650  +      /* It is possible to append to this segment. Set up the IncrmergeWriter
       129651  +      ** object to do so.  */
       129652  +      int i;
       129653  +      int nHeight = (int)aRoot[0];
       129654  +      NodeWriter *pNode;
       129655  +
       129656  +      pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
       129657  +      pWriter->iStart = iStart;
       129658  +      pWriter->iEnd = iEnd;
       129659  +      pWriter->iAbsLevel = iAbsLevel;
       129660  +      pWriter->iIdx = iIdx;
       129661  +
       129662  +      for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
       129663  +        pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
       129664  +      }
       129665  +
       129666  +      pNode = &pWriter->aNodeWriter[nHeight];
       129667  +      pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
       129668  +      blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
       129669  +      if( rc==SQLITE_OK ){
       129670  +        memcpy(pNode->block.a, aRoot, nRoot);
       129671  +        pNode->block.n = nRoot;
       129672  +      }
       129673  +
       129674  +      for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
       129675  +        NodeReader reader;
       129676  +        pNode = &pWriter->aNodeWriter[i];
       129677  +
       129678  +        rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
       129679  +        while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
       129680  +        blobGrowBuffer(&pNode->key, reader.term.n, &rc);
       129681  +        if( rc==SQLITE_OK ){
       129682  +          memcpy(pNode->key.a, reader.term.a, reader.term.n);
       129683  +          pNode->key.n = reader.term.n;
       129684  +          if( i>0 ){
       129685  +            char *aBlock = 0;
       129686  +            int nBlock = 0;
       129687  +            pNode = &pWriter->aNodeWriter[i-1];
       129688  +            pNode->iBlock = reader.iChild;
       129689  +            rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
       129690  +            blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
       129691  +            if( rc==SQLITE_OK ){
       129692  +              memcpy(pNode->block.a, aBlock, nBlock);
       129693  +              pNode->block.n = nBlock;
       129694  +            }
       129695  +            sqlite3_free(aBlock);
       129696  +          }
       129697  +        }
       129698  +        nodeReaderRelease(&reader);
       129699  +      }
       129700  +    }
       129701  +
       129702  +    rc2 = sqlite3_reset(pSelect);
       129703  +    if( rc==SQLITE_OK ) rc = rc2;
       129704  +  }
       129705  +
       129706  +  return rc;
       129707  +}
       129708  +
       129709  +/*
       129710  +** Determine the largest segment index value that exists within absolute
       129711  +** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
       129712  +** one before returning SQLITE_OK. Or, if there are no segments at all 
       129713  +** within level iAbsLevel, set *piIdx to zero.
       129714  +**
       129715  +** If an error occurs, return an SQLite error code. The final value of
       129716  +** *piIdx is undefined in this case.
       129717  +*/
       129718  +static int fts3IncrmergeOutputIdx( 
       129719  +  Fts3Table *p,                   /* FTS Table handle */
       129720  +  sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
       129721  +  int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
       129722  +){
       129723  +  int rc;
       129724  +  sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
       129725  +
       129726  +  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
       129727  +  if( rc==SQLITE_OK ){
       129728  +    sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
       129729  +    sqlite3_step(pOutputIdx);
       129730  +    *piIdx = sqlite3_column_int(pOutputIdx, 0);
       129731  +    rc = sqlite3_reset(pOutputIdx);
       129732  +  }
       129733  +
       129734  +  return rc;
       129735  +}
       129736  +
       129737  +/* 
       129738  +** Allocate an appendable output segment on absolute level iAbsLevel+1
       129739  +** with idx value iIdx.
       129740  +**
       129741  +** In the %_segdir table, a segment is defined by the values in three
       129742  +** columns:
       129743  +**
       129744  +**     start_block
       129745  +**     leaves_end_block
       129746  +**     end_block
       129747  +**
       129748  +** When an appendable segment is allocated, it is estimated that the
       129749  +** maximum number of leaf blocks that may be required is the sum of the
       129750  +** number of leaf blocks consumed by the input segments, plus the number
       129751  +** of input segments, multiplied by two. This value is stored in stack 
       129752  +** variable nLeafEst.
       129753  +**
       129754  +** A total of 16*nLeafEst blocks are allocated when an appendable segment
       129755  +** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
       129756  +** array of leaf nodes starts at the first block allocated. The array
       129757  +** of interior nodes that are parents of the leaf nodes start at block
       129758  +** (start_block + (1 + end_block - start_block) / 16). And so on.
       129759  +**
       129760  +** In the actual code below, the value "16" is replaced with the 
       129761  +** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
       129762  +*/
       129763  +static int fts3IncrmergeWriter( 
       129764  +  Fts3Table *p,                   /* Fts3 table handle */
       129765  +  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
       129766  +  int iIdx,                       /* Index of new output segment */
       129767  +  Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
       129768  +  IncrmergeWriter *pWriter        /* Populate this object */
       129769  +){
       129770  +  int rc;                         /* Return Code */
       129771  +  int i;                          /* Iterator variable */
       129772  +  int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
       129773  +  sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
       129774  +  sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
       129775  +
       129776  +  /* Calculate nLeafEst. */
       129777  +  rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
       129778  +  if( rc==SQLITE_OK ){
       129779  +    sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
       129780  +    sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
       129781  +    if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
       129782  +      nLeafEst = sqlite3_column_int(pLeafEst, 0);
       129783  +    }
       129784  +    rc = sqlite3_reset(pLeafEst);
       129785  +  }
       129786  +  if( rc!=SQLITE_OK ) return rc;
       129787  +
       129788  +  /* Calculate the first block to use in the output segment */
       129789  +  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
       129790  +  if( rc==SQLITE_OK ){
       129791  +    if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
       129792  +      pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
       129793  +      pWriter->iEnd = pWriter->iStart - 1;
       129794  +      pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
       129795  +    }
       129796  +    rc = sqlite3_reset(pFirstBlock);
       129797  +  }
       129798  +  if( rc!=SQLITE_OK ) return rc;
       129799  +
       129800  +  /* Insert the marker in the %_segments table to make sure nobody tries
       129801  +  ** to steal the space just allocated. This is also used to identify 
       129802  +  ** appendable segments.  */
       129803  +  rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
       129804  +  if( rc!=SQLITE_OK ) return rc;
       129805  +
       129806  +  pWriter->iAbsLevel = iAbsLevel;
       129807  +  pWriter->nLeafEst = nLeafEst;
       129808  +  pWriter->iIdx = iIdx;
       129809  +
       129810  +  /* Set up the array of NodeWriter objects */
       129811  +  for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
       129812  +    pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
       129813  +  }
       129814  +  return SQLITE_OK;
       129815  +}
       129816  +
       129817  +/*
       129818  +** Remove an entry from the %_segdir table. This involves running the 
       129819  +** following two statements:
       129820  +**
       129821  +**   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
       129822  +**   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
       129823  +**
       129824  +** The DELETE statement removes the specific %_segdir level. The UPDATE 
       129825  +** statement ensures that the remaining segments have contiguously allocated
       129826  +** idx values.
       129827  +*/
       129828  +static int fts3RemoveSegdirEntry(
       129829  +  Fts3Table *p,                   /* FTS3 table handle */
       129830  +  sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
       129831  +  int iIdx                        /* Index of %_segdir entry to delete */
       129832  +){
       129833  +  int rc;                         /* Return code */
       129834  +  sqlite3_stmt *pDelete = 0;      /* DELETE statement */
       129835  +
       129836  +  rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
       129837  +  if( rc==SQLITE_OK ){
       129838  +    sqlite3_bind_int64(pDelete, 1, iAbsLevel);
       129839  +    sqlite3_bind_int(pDelete, 2, iIdx);
       129840  +    sqlite3_step(pDelete);
       129841  +    rc = sqlite3_reset(pDelete);
       129842  +  }
       129843  +
       129844  +  return rc;
       129845  +}
       129846  +
       129847  +/*
       129848  +** One or more segments have just been removed from absolute level iAbsLevel.
       129849  +** Update the 'idx' values of the remaining segments in the level so that
       129850  +** the idx values are a contiguous sequence starting from 0.
       129851  +*/
       129852  +static int fts3RepackSegdirLevel(
       129853  +  Fts3Table *p,                   /* FTS3 table handle */
       129854  +  sqlite3_int64 iAbsLevel         /* Absolute level to repack */
       129855  +){
       129856  +  int rc;                         /* Return code */
       129857  +  int *aIdx = 0;                  /* Array of remaining idx values */
       129858  +  int nIdx = 0;                   /* Valid entries in aIdx[] */
       129859  +  int nAlloc = 0;                 /* Allocated size of aIdx[] */
       129860  +  int i;                          /* Iterator variable */
       129861  +  sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
       129862  +  sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
       129863  +
       129864  +  rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
       129865  +  if( rc==SQLITE_OK ){
       129866  +    int rc2;
       129867  +    sqlite3_bind_int64(pSelect, 1, iAbsLevel);
       129868  +    while( SQLITE_ROW==sqlite3_step(pSelect) ){
       129869  +      if( nIdx>=nAlloc ){
       129870  +        int *aNew;
       129871  +        nAlloc += 16;
       129872  +        aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
       129873  +        if( !aNew ){
       129874  +          rc = SQLITE_NOMEM;
       129875  +          break;
       129876  +        }
       129877  +        aIdx = aNew;
       129878  +      }
       129879  +      aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
       129880  +    }
       129881  +    rc2 = sqlite3_reset(pSelect);
       129882  +    if( rc==SQLITE_OK ) rc = rc2;
       129883  +  }
       129884  +
       129885  +  if( rc==SQLITE_OK ){
       129886  +    rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
       129887  +  }
       129888  +  if( rc==SQLITE_OK ){
       129889  +    sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
       129890  +  }
       129891  +
       129892  +  assert( p->bIgnoreSavepoint==0 );
       129893  +  p->bIgnoreSavepoint = 1;
       129894  +  for(i=0; rc==SQLITE_OK && i<nIdx; i++){
       129895  +    if( aIdx[i]!=i ){
       129896  +      sqlite3_bind_int(pUpdate, 3, aIdx[i]);
       129897  +      sqlite3_bind_int(pUpdate, 1, i);
       129898  +      sqlite3_step(pUpdate);
       129899  +      rc = sqlite3_reset(pUpdate);
       129900  +    }
       129901  +  }
       129902  +  p->bIgnoreSavepoint = 0;
       129903  +
       129904  +  sqlite3_free(aIdx);
       129905  +  return rc;
       129906  +}
       129907  +
       129908  +static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
       129909  +  pNode->a[0] = (char)iHeight;
       129910  +  if( iChild ){
       129911  +    assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
       129912  +    pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
       129913  +  }else{
       129914  +    assert( pNode->nAlloc>=1 );
       129915  +    pNode->n = 1;
       129916  +  }
       129917  +}
       129918  +
       129919  +/*
       129920  +** The first two arguments are a pointer to and the size of a segment b-tree
       129921  +** node. The node may be a leaf or an internal node.
       129922  +**
       129923  +** This function creates a new node image in blob object *pNew by copying
       129924  +** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
       129925  +** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
       129926  +*/
       129927  +static int fts3TruncateNode(
       129928  +  const char *aNode,              /* Current node image */
       129929  +  int nNode,                      /* Size of aNode in bytes */
       129930  +  Blob *pNew,                     /* OUT: Write new node image here */
       129931  +  const char *zTerm,              /* Omit all terms smaller than this */
       129932  +  int nTerm,                      /* Size of zTerm in bytes */
       129933  +  sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
       129934  +){
       129935  +  NodeReader reader;              /* Reader object */
       129936  +  Blob prev = {0, 0, 0};          /* Previous term written to new node */
       129937  +  int rc = SQLITE_OK;             /* Return code */
       129938  +  int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
       129939  +
       129940  +  /* Allocate required output space */
       129941  +  blobGrowBuffer(pNew, nNode, &rc);
       129942  +  if( rc!=SQLITE_OK ) return rc;
       129943  +  pNew->n = 0;
       129944  +
       129945  +  /* Populate new node buffer */
       129946  +  for(rc = nodeReaderInit(&reader, aNode, nNode); 
       129947  +      rc==SQLITE_OK && reader.aNode; 
       129948  +      rc = nodeReaderNext(&reader)
       129949  +  ){
       129950  +    if( pNew->n==0 ){
       129951  +      int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
       129952  +      if( res<0 || (bLeaf==0 && res==0) ) continue;
       129953  +      fts3StartNode(pNew, (int)aNode[0], reader.iChild);
       129954  +      *piBlock = reader.iChild;
       129955  +    }
       129956  +    rc = fts3AppendToNode(
       129957  +        pNew, &prev, reader.term.a, reader.term.n,
       129958  +        reader.aDoclist, reader.nDoclist
       129959  +    );
       129960  +    if( rc!=SQLITE_OK ) break;
       129961  +  }
       129962  +  if( pNew->n==0 ){
       129963  +    fts3StartNode(pNew, (int)aNode[0], reader.iChild);
       129964  +    *piBlock = reader.iChild;
       129965  +  }
       129966  +  assert( pNew->n<=pNew->nAlloc );
       129967  +
       129968  +  nodeReaderRelease(&reader);
       129969  +  sqlite3_free(prev.a);
       129970  +  return rc;
       129971  +}
       129972  +
       129973  +/*
       129974  +** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute 
       129975  +** level iAbsLevel. This may involve deleting entries from the %_segments
       129976  +** table, and modifying existing entries in both the %_segments and %_segdir
       129977  +** tables.
       129978  +**
       129979  +** SQLITE_OK is returned if the segment is updated successfully. Or an
       129980  +** SQLite error code otherwise.
       129981  +*/
       129982  +static int fts3TruncateSegment(
       129983  +  Fts3Table *p,                   /* FTS3 table handle */
       129984  +  sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
       129985  +  int iIdx,                       /* Index within level of segment to modify */
       129986  +  const char *zTerm,              /* Remove terms smaller than this */
       129987  +  int nTerm                      /* Number of bytes in buffer zTerm */
       129988  +){
       129989  +  int rc = SQLITE_OK;             /* Return code */
       129990  +  Blob root = {0,0,0};            /* New root page image */
       129991  +  Blob block = {0,0,0};           /* Buffer used for any other block */
       129992  +  sqlite3_int64 iBlock = 0;       /* Block id */
       129993  +  sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
       129994  +  sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
       129995  +  sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
       129996  +
       129997  +  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
       129998  +  if( rc==SQLITE_OK ){
       129999  +    int rc2;                      /* sqlite3_reset() return code */
       130000  +    sqlite3_bind_int64(pFetch, 1, iAbsLevel);
       130001  +    sqlite3_bind_int(pFetch, 2, iIdx);
       130002  +    if( SQLITE_ROW==sqlite3_step(pFetch) ){
       130003  +      const char *aRoot = sqlite3_column_blob(pFetch, 4);
       130004  +      int nRoot = sqlite3_column_bytes(pFetch, 4);
       130005  +      iOldStart = sqlite3_column_int64(pFetch, 1);
       130006  +      rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
       130007  +    }
       130008  +    rc2 = sqlite3_reset(pFetch);
       130009  +    if( rc==SQLITE_OK ) rc = rc2;
       130010  +  }
       130011  +
       130012  +  while( rc==SQLITE_OK && iBlock ){
       130013  +    char *aBlock = 0;
       130014  +    int nBlock = 0;
       130015  +    iNewStart = iBlock;
       130016  +
       130017  +    rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
       130018  +    if( rc==SQLITE_OK ){
       130019  +      rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
       130020  +    }
       130021  +    if( rc==SQLITE_OK ){
       130022  +      rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
       130023  +    }
       130024  +    sqlite3_free(aBlock);
       130025  +  }
       130026  +
       130027  +  /* Variable iNewStart now contains the first valid leaf node. */
       130028  +  if( rc==SQLITE_OK && iNewStart ){
       130029  +    sqlite3_stmt *pDel = 0;
       130030  +    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
       130031  +    if( rc==SQLITE_OK ){
       130032  +      sqlite3_bind_int64(pDel, 1, iOldStart);
       130033  +      sqlite3_bind_int64(pDel, 2, iNewStart-1);
       130034  +      sqlite3_step(pDel);
       130035  +      rc = sqlite3_reset(pDel);
       130036  +    }
       130037  +  }
       130038  +
       130039  +  if( rc==SQLITE_OK ){
       130040  +    sqlite3_stmt *pChomp = 0;
       130041  +    rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
       130042  +    if( rc==SQLITE_OK ){
       130043  +      sqlite3_bind_int64(pChomp, 1, iNewStart);
       130044  +      sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
       130045  +      sqlite3_bind_int64(pChomp, 3, iAbsLevel);
       130046  +      sqlite3_bind_int(pChomp, 4, iIdx);
       130047  +      sqlite3_step(pChomp);
       130048  +      rc = sqlite3_reset(pChomp);
       130049  +    }
       130050  +  }
       130051  +
       130052  +  sqlite3_free(root.a);
       130053  +  sqlite3_free(block.a);
       130054  +  return rc;
       130055  +}
       130056  +
       130057  +/*
       130058  +** This function is called after an incrmental-merge operation has run to
       130059  +** merge (or partially merge) two or more segments from absolute level
       130060  +** iAbsLevel.
       130061  +**
       130062  +** Each input segment is either removed from the db completely (if all of
       130063  +** its data was copied to the output segment by the incrmerge operation)
       130064  +** or modified in place so that it no longer contains those entries that
       130065  +** have been duplicated in the output segment.
       130066  +*/
       130067  +static int fts3IncrmergeChomp(
       130068  +  Fts3Table *p,                   /* FTS table handle */
       130069  +  sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
       130070  +  Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
       130071  +  int *pnRem                      /* Number of segments not deleted */
       130072  +){
       130073  +  int i;
       130074  +  int nRem = 0;
       130075  +  int rc = SQLITE_OK;
       130076  +
       130077  +  for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
       130078  +    Fts3SegReader *pSeg = 0;
       130079  +    int j;
       130080  +
       130081  +    /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
       130082  +    ** somewhere in the pCsr->apSegment[] array.  */
       130083  +    for(j=0; ALWAYS(j<pCsr->nSegment); j++){
       130084  +      pSeg = pCsr->apSegment[j];
       130085  +      if( pSeg->iIdx==i ) break;
       130086  +    }
       130087  +    assert( j<pCsr->nSegment && pSeg->iIdx==i );
       130088  +
       130089  +    if( pSeg->aNode==0 ){
       130090  +      /* Seg-reader is at EOF. Remove the entire input segment. */
       130091  +      rc = fts3DeleteSegment(p, pSeg);
       130092  +      if( rc==SQLITE_OK ){
       130093  +        rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
       130094  +      }
       130095  +      *pnRem = 0;
       130096  +    }else{
       130097  +      /* The incremental merge did not copy all the data from this 
       130098  +      ** segment to the upper level. The segment is modified in place
       130099  +      ** so that it contains no keys smaller than zTerm/nTerm. */ 
       130100  +      const char *zTerm = pSeg->zTerm;
       130101  +      int nTerm = pSeg->nTerm;
       130102  +      rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
       130103  +      nRem++;
       130104  +    }
       130105  +  }
       130106  +
       130107  +  if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
       130108  +    rc = fts3RepackSegdirLevel(p, iAbsLevel);
       130109  +  }
       130110  +
       130111  +  *pnRem = nRem;
       130112  +  return rc;
       130113  +}
       130114  +
       130115  +/*
       130116  +** Store an incr-merge hint in the database.
       130117  +*/
       130118  +static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
       130119  +  sqlite3_stmt *pReplace = 0;
       130120  +  int rc;                         /* Return code */
       130121  +
       130122  +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
       130123  +  if( rc==SQLITE_OK ){
       130124  +    sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
       130125  +    sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
       130126  +    sqlite3_step(pReplace);
       130127  +    rc = sqlite3_reset(pReplace);
       130128  +  }
       130129  +
       130130  +  return rc;
       130131  +}
       130132  +
       130133  +/*
       130134  +** Load an incr-merge hint from the database. The incr-merge hint, if one 
       130135  +** exists, is stored in the rowid==1 row of the %_stat table.
       130136  +**
       130137  +** If successful, populate blob *pHint with the value read from the %_stat
       130138  +** table and return SQLITE_OK. Otherwise, if an error occurs, return an
       130139  +** SQLite error code.
       130140  +*/
       130141  +static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
       130142  +  sqlite3_stmt *pSelect = 0;
       130143  +  int rc;
       130144  +
       130145  +  pHint->n = 0;
       130146  +  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
       130147  +  if( rc==SQLITE_OK ){
       130148  +    int rc2;
       130149  +    sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
       130150  +    if( SQLITE_ROW==sqlite3_step(pSelect) ){
       130151  +      const char *aHint = sqlite3_column_blob(pSelect, 0);
       130152  +      int nHint = sqlite3_column_bytes(pSelect, 0);
       130153  +      if( aHint ){
       130154  +        blobGrowBuffer(pHint, nHint, &rc);
       130155  +        if( rc==SQLITE_OK ){
       130156  +          memcpy(pHint->a, aHint, nHint);
       130157  +          pHint->n = nHint;
       130158  +        }
       130159  +      }
       130160  +    }
       130161  +    rc2 = sqlite3_reset(pSelect);
       130162  +    if( rc==SQLITE_OK ) rc = rc2;
       130163  +  }
       130164  +
       130165  +  return rc;
       130166  +}
       130167  +
       130168  +/*
       130169  +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
       130170  +** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
       130171  +** consists of two varints, the absolute level number of the input segments 
       130172  +** and the number of input segments.
       130173  +**
       130174  +** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
       130175  +** set *pRc to an SQLite error code before returning.
       130176  +*/
       130177  +static void fts3IncrmergeHintPush(
       130178  +  Blob *pHint,                    /* Hint blob to append to */
       130179  +  i64 iAbsLevel,                  /* First varint to store in hint */
       130180  +  int nInput,                     /* Second varint to store in hint */
       130181  +  int *pRc                        /* IN/OUT: Error code */
       130182  +){
       130183  +  blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
       130184  +  if( *pRc==SQLITE_OK ){
       130185  +    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
       130186  +    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
       130187  +  }
       130188  +}
       130189  +
       130190  +/*
       130191  +** Read the last entry (most recently pushed) from the hint blob *pHint
       130192  +** and then remove the entry. Write the two values read to *piAbsLevel and 
       130193  +** *pnInput before returning.
       130194  +**
       130195  +** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
       130196  +** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
       130197  +*/
       130198  +static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
       130199  +  const int nHint = pHint->n;
       130200  +  int i;
       130201  +
       130202  +  i = pHint->n-2;
       130203  +  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
       130204  +  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
       130205  +
       130206  +  pHint->n = i;
       130207  +  i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
       130208  +  i += sqlite3Fts3GetVarint32(&pHint->a[i], pnInput);
       130209  +  if( i!=nHint ) return SQLITE_CORRUPT_VTAB;
       130210  +
       130211  +  return SQLITE_OK;
       130212  +}
       130213  +
       130214  +
       130215  +/*
       130216  +** Attempt an incremental merge that writes nMerge leaf blocks.
       130217  +**
       130218  +** Incremental merges happen nMin segments at a time. The two
       130219  +** segments to be merged are the nMin oldest segments (the ones with
       130220  +** the smallest indexes) in the highest level that contains at least
       130221  +** nMin segments. Multiple merges might occur in an attempt to write the 
       130222  +** quota of nMerge leaf blocks.
       130223  +*/
       130224  +SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
       130225  +  int rc;                         /* Return code */
       130226  +  int nRem = nMerge;              /* Number of leaf pages yet to  be written */
       130227  +  Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
       130228  +  Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
       130229  +  IncrmergeWriter *pWriter;       /* Writer object */
       130230  +  int nSeg = 0;                   /* Number of input segments */
       130231  +  sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
       130232  +  Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
       130233  +  int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
       130234  +
       130235  +  /* Allocate space for the cursor, filter and writer objects */
       130236  +  const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
       130237  +  pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
       130238  +  if( !pWriter ) return SQLITE_NOMEM;
       130239  +  pFilter = (Fts3SegFilter *)&pWriter[1];
       130240  +  pCsr = (Fts3MultiSegReader *)&pFilter[1];
       130241  +
       130242  +  rc = fts3IncrmergeHintLoad(p, &hint);
       130243  +  while( rc==SQLITE_OK && nRem>0 ){
       130244  +    const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
       130245  +    sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
       130246  +    int bUseHint = 0;             /* True if attempting to append */
       130247  +
       130248  +    /* Search the %_segdir table for the absolute level with the smallest
       130249  +    ** relative level number that contains at least nMin segments, if any.
       130250  +    ** If one is found, set iAbsLevel to the absolute level number and
       130251  +    ** nSeg to nMin. If no level with at least nMin segments can be found, 
       130252  +    ** set nSeg to -1.
       130253  +    */
       130254  +    rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
       130255  +    sqlite3_bind_int(pFindLevel, 1, nMin);
       130256  +    if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
       130257  +      iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
       130258  +      nSeg = nMin;
       130259  +    }else{
       130260  +      nSeg = -1;
       130261  +    }
       130262  +    rc = sqlite3_reset(pFindLevel);
       130263  +
       130264  +    /* If the hint read from the %_stat table is not empty, check if the
       130265  +    ** last entry in it specifies a relative level smaller than or equal
       130266  +    ** to the level identified by the block above (if any). If so, this 
       130267  +    ** iteration of the loop will work on merging at the hinted level.
       130268  +    */
       130269  +    if( rc==SQLITE_OK && hint.n ){
       130270  +      int nHint = hint.n;
       130271  +      sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
       130272  +      int nHintSeg = 0;                     /* Hint number of segments */
       130273  +
       130274  +      rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
       130275  +      if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
       130276  +        iAbsLevel = iHintAbsLevel;
       130277  +        nSeg = nHintSeg;
       130278  +        bUseHint = 1;
       130279  +        bDirtyHint = 1;
       130280  +      }else{
       130281  +        /* This undoes the effect of the HintPop() above - so that no entry
       130282  +        ** is removed from the hint blob.  */
       130283  +        hint.n = nHint;
       130284  +      }
       130285  +    }
       130286  +
       130287  +    /* If nSeg is less that zero, then there is no level with at least
       130288  +    ** nMin segments and no hint in the %_stat table. No work to do.
       130289  +    ** Exit early in this case.  */
       130290  +    if( nSeg<0 ) break;
       130291  +
       130292  +    /* Open a cursor to iterate through the contents of the oldest nSeg 
       130293  +    ** indexes of absolute level iAbsLevel. If this cursor is opened using 
       130294  +    ** the 'hint' parameters, it is possible that there are less than nSeg
       130295  +    ** segments available in level iAbsLevel. In this case, no work is
       130296  +    ** done on iAbsLevel - fall through to the next iteration of the loop 
       130297  +    ** to start work on some other level.  */
       130298  +    memset(pWriter, 0, nAlloc);
       130299  +    pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
       130300  +    if( rc==SQLITE_OK ){
       130301  +      rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
       130302  +    }
       130303  +    if( SQLITE_OK==rc && pCsr->nSegment==nSeg
       130304  +     && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
       130305  +     && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
       130306  +    ){
       130307  +      int iIdx = 0;               /* Largest idx in level (iAbsLevel+1) */
       130308  +      rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
       130309  +      if( rc==SQLITE_OK ){
       130310  +        if( bUseHint && iIdx>0 ){
       130311  +          const char *zKey = pCsr->zTerm;
       130312  +          int nKey = pCsr->nTerm;
       130313  +          rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
       130314  +        }else{
       130315  +          rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
       130316  +        }
       130317  +      }
       130318  +
       130319  +      if( rc==SQLITE_OK && pWriter->nLeafEst ){
       130320  +        fts3LogMerge(nSeg, iAbsLevel);
       130321  +        do {
       130322  +          rc = fts3IncrmergeAppend(p, pWriter, pCsr);
       130323  +          if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
       130324  +          if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
       130325  +        }while( rc==SQLITE_ROW );
       130326  +
       130327  +        /* Update or delete the input segments */
       130328  +        if( rc==SQLITE_OK ){
       130329  +          nRem -= (1 + pWriter->nWork);
       130330  +          rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
       130331  +          if( nSeg!=0 ){
       130332  +            bDirtyHint = 1;
       130333  +            fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
       130334  +          }
       130335  +        }
       130336  +      }
       130337  +
       130338  +      fts3IncrmergeRelease(p, pWriter, &rc);
       130339  +    }
       130340  +
       130341  +    sqlite3Fts3SegReaderFinish(pCsr);
       130342  +  }
       130343  +
       130344  +  /* Write the hint values into the %_stat table for the next incr-merger */
       130345  +  if( bDirtyHint && rc==SQLITE_OK ){
       130346  +    rc = fts3IncrmergeHintStore(p, &hint);
       130347  +  }
       130348  +
       130349  +  sqlite3_free(pWriter);
       130350  +  sqlite3_free(hint.a);
       130351  +  return rc;
       130352  +}
       130353  +
       130354  +/*
       130355  +** Convert the text beginning at *pz into an integer and return
       130356  +** its value.  Advance *pz to point to the first character past
       130357  +** the integer.
       130358  +*/
       130359  +static int fts3Getint(const char **pz){
       130360  +  const char *z = *pz;
       130361  +  int i = 0;
       130362  +  while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
       130363  +  *pz = z;
       130364  +  return i;
       130365  +}
       130366  +
       130367  +/*
       130368  +** Process statements of the form:
       130369  +**
       130370  +**    INSERT INTO table(table) VALUES('merge=A,B');
       130371  +**
       130372  +** A and B are integers that decode to be the number of leaf pages
       130373  +** written for the merge, and the minimum number of segments on a level
       130374  +** before it will be selected for a merge, respectively.
       130375  +*/
       130376  +static int fts3DoIncrmerge(
       130377  +  Fts3Table *p,                   /* FTS3 table handle */
       130378  +  const char *zParam              /* Nul-terminated string containing "A,B" */
       130379  +){
       130380  +  int rc;
       130381  +  int nMin = (FTS3_MERGE_COUNT / 2);
       130382  +  int nMerge = 0;
       130383  +  const char *z = zParam;
       130384  +
       130385  +  /* Read the first integer value */
       130386  +  nMerge = fts3Getint(&z);
       130387  +
       130388  +  /* If the first integer value is followed by a ',',  read the second
       130389  +  ** integer value. */
       130390  +  if( z[0]==',' && z[1]!='\0' ){
       130391  +    z++;
       130392  +    nMin = fts3Getint(&z);
       130393  +  }
       130394  +
       130395  +  if( z[0]!='\0' || nMin<2 ){
       130396  +    rc = SQLITE_ERROR;
       130397  +  }else{
       130398  +    rc = SQLITE_OK;
       130399  +    if( !p->bHasStat ){
       130400  +      assert( p->bFts4==0 );
       130401  +      sqlite3Fts3CreateStatTable(&rc, p);
       130402  +    }
       130403  +    if( rc==SQLITE_OK ){
       130404  +      rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
       130405  +    }
       130406  +    sqlite3Fts3SegmentsClose(p);
       130407  +  }
       130408  +  return rc;
       130409  +}
       130410  +
       130411  +/*
       130412  +** Process statements of the form:
       130413  +**
       130414  +**    INSERT INTO table(table) VALUES('automerge=X');
       130415  +**
       130416  +** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
       130417  +** turn it on.  The setting is persistent.
       130418  +*/
       130419  +static int fts3DoAutoincrmerge(
       130420  +  Fts3Table *p,                   /* FTS3 table handle */
       130421  +  const char *zParam              /* Nul-terminated string containing boolean */
       130422  +){
       130423  +  int rc = SQLITE_OK;
       130424  +  sqlite3_stmt *pStmt = 0;
       130425  +  p->bAutoincrmerge = fts3Getint(&zParam)!=0;
       130426  +  if( !p->bHasStat ){
       130427  +    assert( p->bFts4==0 );
       130428  +    sqlite3Fts3CreateStatTable(&rc, p);
       130429  +    if( rc ) return rc;
       130430  +  }
       130431  +  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
       130432  +  if( rc ) return rc;;
       130433  +  sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
       130434  +  sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
       130435  +  sqlite3_step(pStmt);
       130436  +  rc = sqlite3_reset(pStmt);
       130437  +  return rc;
       130438  +}
       130439  +
       130440  +/*
       130441  +** Return a 64-bit checksum for the FTS index entry specified by the
       130442  +** arguments to this function.
       130443  +*/
       130444  +static u64 fts3ChecksumEntry(
       130445  +  const char *zTerm,              /* Pointer to buffer containing term */
       130446  +  int nTerm,                      /* Size of zTerm in bytes */
       130447  +  int iLangid,                    /* Language id for current row */
       130448  +  int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
       130449  +  i64 iDocid,                     /* Docid for current row. */
       130450  +  int iCol,                       /* Column number */
       130451  +  int iPos                        /* Position */
       130452  +){
       130453  +  int i;
       130454  +  u64 ret = (u64)iDocid;
       130455  +
       130456  +  ret += (ret<<3) + iLangid;
       130457  +  ret += (ret<<3) + iIndex;
       130458  +  ret += (ret<<3) + iCol;
       130459  +  ret += (ret<<3) + iPos;
       130460  +  for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
       130461  +
       130462  +  return ret;
       130463  +}
       130464  +
       130465  +/*
       130466  +** Return a checksum of all entries in the FTS index that correspond to
       130467  +** language id iLangid. The checksum is calculated by XORing the checksums
       130468  +** of each individual entry (see fts3ChecksumEntry()) together.
       130469  +**
       130470  +** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
       130471  +** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
       130472  +** return value is undefined in this case.
       130473  +*/
       130474  +static u64 fts3ChecksumIndex(
       130475  +  Fts3Table *p,                   /* FTS3 table handle */
       130476  +  int iLangid,                    /* Language id to return cksum for */
       130477  +  int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
       130478  +  int *pRc                        /* OUT: Return code */
       130479  +){
       130480  +  Fts3SegFilter filter;
       130481  +  Fts3MultiSegReader csr;
       130482  +  int rc;
       130483  +  u64 cksum = 0;
       130484  +
       130485  +  assert( *pRc==SQLITE_OK );
       130486  +
       130487  +  memset(&filter, 0, sizeof(filter));
       130488  +  memset(&csr, 0, sizeof(csr));
       130489  +  filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
       130490  +  filter.flags |= FTS3_SEGMENT_SCAN;
       130491  +
       130492  +  rc = sqlite3Fts3SegReaderCursor(
       130493  +      p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
       130494  +  );
       130495  +  if( rc==SQLITE_OK ){
       130496  +    rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
       130497  +  }
       130498  +
       130499  +  if( rc==SQLITE_OK ){
       130500  +    while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
       130501  +      char *pCsr = csr.aDoclist;
       130502  +      char *pEnd = &pCsr[csr.nDoclist];
       130503  +
       130504  +      i64 iDocid = 0;
       130505  +      i64 iCol = 0;
       130506  +      i64 iPos = 0;
       130507  +
       130508  +      pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
       130509  +      while( pCsr<pEnd ){
       130510  +        i64 iVal = 0;
       130511  +        pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
       130512  +        if( pCsr<pEnd ){
       130513  +          if( iVal==0 || iVal==1 ){
       130514  +            iCol = 0;
       130515  +            iPos = 0;
       130516  +            if( iVal ){
       130517  +              pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
       130518  +            }else{
       130519  +              pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
       130520  +              iDocid += iVal;
       130521  +            }
       130522  +          }else{
       130523  +            iPos += (iVal - 2);
       130524  +            cksum = cksum ^ fts3ChecksumEntry(
       130525  +                csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
       130526  +                (int)iCol, (int)iPos
       130527  +            );
       130528  +          }
       130529  +        }
       130530  +      }
       130531  +    }
       130532  +  }
       130533  +  sqlite3Fts3SegReaderFinish(&csr);
       130534  +
       130535  +  *pRc = rc;
       130536  +  return cksum;
       130537  +}
       130538  +
       130539  +/*
       130540  +** Check if the contents of the FTS index match the current contents of the
       130541  +** content table. If no error occurs and the contents do match, set *pbOk
       130542  +** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
       130543  +** to false before returning.
       130544  +**
       130545  +** If an error occurs (e.g. an OOM or IO error), return an SQLite error 
       130546  +** code. The final value of *pbOk is undefined in this case.
       130547  +*/
       130548  +static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
       130549  +  int rc = SQLITE_OK;             /* Return code */
       130550  +  u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
       130551  +  u64 cksum2 = 0;                 /* Checksum based on %_content contents */
       130552  +  sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
       130553  +
       130554  +  /* This block calculates the checksum according to the FTS index. */
       130555  +  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
       130556  +  if( rc==SQLITE_OK ){
       130557  +    int rc2;
       130558  +    sqlite3_bind_int(pAllLangid, 1, p->nIndex);
       130559  +    while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
       130560  +      int iLangid = sqlite3_column_int(pAllLangid, 0);
       130561  +      int i;
       130562  +      for(i=0; i<p->nIndex; i++){
       130563  +        cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
       130564  +      }
       130565  +    }
       130566  +    rc2 = sqlite3_reset(pAllLangid);
       130567  +    if( rc==SQLITE_OK ) rc = rc2;
       130568  +  }
       130569  +
       130570  +  /* This block calculates the checksum according to the %_content table */
       130571  +  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
       130572  +  if( rc==SQLITE_OK ){
       130573  +    sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
       130574  +    sqlite3_stmt *pStmt = 0;
       130575  +    char *zSql;
       130576  +   
       130577  +    zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
       130578  +    if( !zSql ){
       130579  +      rc = SQLITE_NOMEM;
       130580  +    }else{
       130581  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
       130582  +      sqlite3_free(zSql);
       130583  +    }
       130584  +
       130585  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
       130586  +      i64 iDocid = sqlite3_column_int64(pStmt, 0);
       130587  +      int iLang = langidFromSelect(p, pStmt);
       130588  +      int iCol;
       130589  +
       130590  +      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
       130591  +        const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
       130592  +        int nText = sqlite3_column_bytes(pStmt, iCol+1);
       130593  +        sqlite3_tokenizer_cursor *pT = 0;
       130594  +
       130595  +        rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
       130596  +        while( rc==SQLITE_OK ){
       130597  +          char const *zToken;       /* Buffer containing token */
       130598  +          int nToken;               /* Number of bytes in token */
       130599  +          int iDum1, iDum2;         /* Dummy variables */
       130600  +          int iPos;                 /* Position of token in zText */
       130601  +
       130602  +          rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
       130603  +          if( rc==SQLITE_OK ){
       130604  +            int i;
       130605  +            cksum2 = cksum2 ^ fts3ChecksumEntry(
       130606  +                zToken, nToken, iLang, 0, iDocid, iCol, iPos
       130607  +            );
       130608  +            for(i=1; i<p->nIndex; i++){
       130609  +              if( p->aIndex[i].nPrefix<=nToken ){
       130610  +                cksum2 = cksum2 ^ fts3ChecksumEntry(
       130611  +                  zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
       130612  +                );
       130613  +              }
       130614  +            }
       130615  +          }
       130616  +        }
       130617  +        if( pT ) pModule->xClose(pT);
       130618  +        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
       130619  +      }
       130620  +    }
       130621  +
       130622  +    sqlite3_finalize(pStmt);
       130623  +  }
       130624  +
       130625  +  *pbOk = (cksum1==cksum2);
       130626  +  return rc;
       130627  +}
       130628  +
       130629  +/*
       130630  +** Run the integrity-check. If no error occurs and the current contents of
       130631  +** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
       130632  +** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
       130633  +**
       130634  +** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite 
       130635  +** error code.
       130636  +**
       130637  +** The integrity-check works as follows. For each token and indexed token
       130638  +** prefix in the document set, a 64-bit checksum is calculated (by code
       130639  +** in fts3ChecksumEntry()) based on the following:
       130640  +**
       130641  +**     + The index number (0 for the main index, 1 for the first prefix
       130642  +**       index etc.),
       130643  +**     + The token (or token prefix) text itself, 
       130644  +**     + The language-id of the row it appears in,
       130645  +**     + The docid of the row it appears in,
       130646  +**     + The column it appears in, and
       130647  +**     + The tokens position within that column.
       130648  +**
       130649  +** The checksums for all entries in the index are XORed together to create
       130650  +** a single checksum for the entire index.
       130651  +**
       130652  +** The integrity-check code calculates the same checksum in two ways:
       130653  +**
       130654  +**     1. By scanning the contents of the FTS index, and 
       130655  +**     2. By scanning and tokenizing the content table.
       130656  +**
       130657  +** If the two checksums are identical, the integrity-check is deemed to have
       130658  +** passed.
       130659  +*/
       130660  +static int fts3DoIntegrityCheck(
       130661  +  Fts3Table *p                    /* FTS3 table handle */
       130662  +){
       130663  +  int rc;
       130664  +  int bOk = 0;
       130665  +  rc = fts3IntegrityCheck(p, &bOk);
       130666  +  if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB;
       130667  +  return rc;
       130668  +}
       130669  +
128230 130670   /*
128231 130671   ** Handle a 'special' INSERT of the form:
128232 130672   **
128233 130673   **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
128234 130674   **
128235 130675   ** Argument pVal contains the result of <expr>. Currently the only 
128236 130676   ** meaningful value to insert is the text 'optimize'.
................................................................................
128242 130682   
128243 130683     if( !zVal ){
128244 130684       return SQLITE_NOMEM;
128245 130685     }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
128246 130686       rc = fts3DoOptimize(p, 0);
128247 130687     }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
128248 130688       rc = fts3DoRebuild(p);
       130689  +  }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
       130690  +    rc = fts3DoIntegrityCheck(p);
       130691  +  }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
       130692  +    rc = fts3DoIncrmerge(p, &zVal[6]);
       130693  +  }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
       130694  +    rc = fts3DoAutoincrmerge(p, &zVal[10]);
128249 130695   #ifdef SQLITE_TEST
128250 130696     }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
128251 130697       p->nNodeSize = atoi(&zVal[9]);
128252 130698       rc = SQLITE_OK;
128253 130699     }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
128254 130700       p->nMaxPendingData = atoi(&zVal[11]);
128255 130701       rc = SQLITE_OK;
................................................................................
128437 130883   }
128438 130884   
128439 130885   /*
128440 130886   ** This function does the work for the xUpdate method of FTS3 virtual
128441 130887   ** tables. The schema of the virtual table being:
128442 130888   **
128443 130889   **     CREATE TABLE <table name>( 
128444         -**       <user COLUMns>,
       130890  +**       <user columns>,
128445 130891   **       <table name> HIDDEN, 
128446 130892   **       docid HIDDEN, 
128447 130893   **       <langid> HIDDEN
128448 130894   **     );
128449 130895   **
128450 130896   ** 
128451 130897   */
................................................................................
128569 131015       }
128570 131016       if( p->bHasDocsize ){
128571 131017         fts3InsertDocsize(&rc, p, aSzIns);
128572 131018       }
128573 131019       nChng++;
128574 131020     }
128575 131021   
128576         -  if( p->bHasStat ){
       131022  +  if( p->bFts4 ){
128577 131023       fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
128578 131024     }
128579 131025   
128580 131026    update_out:
128581 131027     sqlite3_free(aSzIns);
128582 131028     sqlite3Fts3SegmentsClose(p);
128583 131029     return rc;
................................................................................
128966 131412   ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
128967 131413   ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
128968 131414   */
128969 131415   static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
128970 131416     SnippetIter *p = (SnippetIter *)ctx;
128971 131417     SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
128972 131418     char *pCsr;
       131419  +  int rc;
128973 131420   
128974 131421     pPhrase->nToken = pExpr->pPhrase->nToken;
128975         -
128976         -  pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
       131422  +  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
       131423  +  assert( rc==SQLITE_OK || pCsr==0 );
128977 131424     if( pCsr ){
128978 131425       int iFirst = 0;
128979 131426       pPhrase->pList = pCsr;
128980 131427       fts3GetDeltaPosition(&pCsr, &iFirst);
128981 131428       assert( iFirst>=0 );
128982 131429       pPhrase->pHead = pCsr;
128983 131430       pPhrase->pTail = pCsr;
128984 131431       pPhrase->iHead = iFirst;
128985 131432       pPhrase->iTail = iFirst;
128986 131433     }else{
128987         -    assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 );
       131434  +    assert( rc!=SQLITE_OK || (
       131435  +       pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 
       131436  +    ));
128988 131437     }
128989 131438   
128990         -  return SQLITE_OK;
       131439  +  return rc;
128991 131440   }
128992 131441   
128993 131442   /*
128994 131443   ** Select the fragment of text consisting of nFragment contiguous tokens 
128995 131444   ** from column iCol that represent the "best" snippet. The best snippet
128996 131445   ** is the snippet with the highest score, where scores are calculated
128997 131446   ** by adding:
................................................................................
129376 131825   ** array that are different for each row returned by the query.
129377 131826   */
129378 131827   static int fts3ExprLocalHitsCb(
129379 131828     Fts3Expr *pExpr,                /* Phrase expression node */
129380 131829     int iPhrase,                    /* Phrase number */
129381 131830     void *pCtx                      /* Pointer to MatchInfo structure */
129382 131831   ){
       131832  +  int rc = SQLITE_OK;
129383 131833     MatchInfo *p = (MatchInfo *)pCtx;
129384 131834     int iStart = iPhrase * p->nCol * 3;
129385 131835     int i;
129386 131836   
129387         -  for(i=0; i<p->nCol; i++){
       131837  +  for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
129388 131838       char *pCsr;
129389         -    pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i);
       131839  +    rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
129390 131840       if( pCsr ){
129391 131841         p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
129392 131842       }else{
129393 131843         p->aMatchinfo[iStart+i*3] = 0;
129394 131844       }
129395 131845     }
129396 131846   
129397         -  return SQLITE_OK;
       131847  +  return rc;
129398 131848   }
129399 131849   
129400 131850   static int fts3MatchinfoCheck(
129401 131851     Fts3Table *pTab, 
129402 131852     char cArg,
129403 131853     char **pzErr
129404 131854   ){
129405 131855     if( (cArg==FTS3_MATCHINFO_NPHRASE)
129406 131856      || (cArg==FTS3_MATCHINFO_NCOL)
129407         -   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat)
129408         -   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat)
       131857  +   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
       131858  +   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
129409 131859      || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
129410 131860      || (cArg==FTS3_MATCHINFO_LCS)
129411 131861      || (cArg==FTS3_MATCHINFO_HITS)
129412 131862     ){
129413 131863       return SQLITE_OK;
129414 131864     }
129415 131865     *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
................................................................................
129551 132001     }
129552 132002   
129553 132003     for(iCol=0; iCol<pInfo->nCol; iCol++){
129554 132004       int nLcs = 0;                 /* LCS value for this column */
129555 132005       int nLive = 0;                /* Number of iterators in aIter not at EOF */
129556 132006   
129557 132007       for(i=0; i<pInfo->nPhrase; i++){
       132008  +      int rc;
129558 132009         LcsIterator *pIt = &aIter[i];
129559         -      pIt->pRead = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol);
       132010  +      rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
       132011  +      if( rc!=SQLITE_OK ) return rc;
129560 132012         if( pIt->pRead ){
129561 132013           pIt->iPos = pIt->iPosOffset;
129562 132014           fts3LcsIteratorAdvance(&aIter[i]);
129563 132015           nLive++;
129564 132016         }
129565 132017       }
129566 132018   
................................................................................
129904 132356   */
129905 132357   static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
129906 132358     TermOffsetCtx *p = (TermOffsetCtx *)ctx;
129907 132359     int nTerm;                      /* Number of tokens in phrase */
129908 132360     int iTerm;                      /* For looping through nTerm phrase terms */
129909 132361     char *pList;                    /* Pointer to position list for phrase */
129910 132362     int iPos = 0;                   /* First position in position-list */
       132363  +  int rc;
129911 132364   
129912 132365     UNUSED_PARAMETER(iPhrase);
129913         -  pList = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
       132366  +  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
129914 132367     nTerm = pExpr->pPhrase->nToken;
129915 132368     if( pList ){
129916 132369       fts3GetDeltaPosition(&pList, &iPos);
129917 132370       assert( iPos>=0 );
129918 132371     }
129919 132372   
129920 132373     for(iTerm=0; iTerm<nTerm; iTerm++){
129921 132374       TermOffset *pT = &p->aTerm[p->iTerm++];
129922 132375       pT->iOff = nTerm-iTerm-1;
129923 132376       pT->pList = pList;
129924 132377       pT->iPos = iPos;
129925 132378     }
129926 132379   
129927         -  return SQLITE_OK;
       132380  +  return rc;
129928 132381   }
129929 132382   
129930 132383   /*
129931 132384   ** Implementation of offsets() function.
129932 132385   */
129933 132386   SQLITE_PRIVATE void sqlite3Fts3Offsets(
129934 132387     sqlite3_context *pCtx,          /* SQLite function call context */
................................................................................
130291 132744     int eCoordType;
130292 132745   };
130293 132746   
130294 132747   /* Possible values for eCoordType: */
130295 132748   #define RTREE_COORD_REAL32 0
130296 132749   #define RTREE_COORD_INT32  1
130297 132750   
       132751  +/*
       132752  +** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
       132753  +** only deal with integer coordinates.  No floating point operations
       132754  +** will be done.
       132755  +*/
       132756  +#ifdef SQLITE_RTREE_INT_ONLY
       132757  +  typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
       132758  +  typedef int RtreeValue;                  /* Low accuracy coordinate */
       132759  +#else
       132760  +  typedef double RtreeDValue;              /* High accuracy coordinate */
       132761  +  typedef float RtreeValue;                /* Low accuracy coordinate */
       132762  +#endif
       132763  +
130298 132764   /*
130299 132765   ** The minimum number of cells allowed for a node is a third of the 
130300 132766   ** maximum. In Gutman's notation:
130301 132767   **
130302 132768   **     m = M/3
130303 132769   **
130304 132770   ** If an R*-tree "Reinsert" operation is required, the same number of
................................................................................
130326 132792     int iCell;                        /* Index of current cell in pNode */
130327 132793     int iStrategy;                    /* Copy of idxNum search parameter */
130328 132794     int nConstraint;                  /* Number of entries in aConstraint */
130329 132795     RtreeConstraint *aConstraint;     /* Search constraints. */
130330 132796   };
130331 132797   
130332 132798   union RtreeCoord {
130333         -  float f;
       132799  +  RtreeValue f;
130334 132800     int i;
130335 132801   };
130336 132802   
130337 132803   /*
130338 132804   ** 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.
       132805  +** formatted as a RtreeDValue (double or int64). This macro assumes that local
       132806  +** variable pRtree points to the Rtree structure associated with the
       132807  +** RtreeCoord.
130341 132808   */
130342         -#define DCOORD(coord) (                           \
130343         -  (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
130344         -    ((double)coord.f) :                           \
130345         -    ((double)coord.i)                             \
130346         -)
       132809  +#ifdef SQLITE_RTREE_INT_ONLY
       132810  +# define DCOORD(coord) ((RtreeDValue)coord.i)
       132811  +#else
       132812  +# define DCOORD(coord) (                           \
       132813  +    (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
       132814  +      ((double)coord.f) :                           \
       132815  +      ((double)coord.i)                             \
       132816  +  )
       132817  +#endif
130347 132818   
130348 132819   /*
130349 132820   ** A search constraint.
130350 132821   */
130351 132822   struct RtreeConstraint {
130352 132823     int iCoord;                     /* Index of constrained coordinate */
130353 132824     int op;                         /* Constraining operation */
130354         -  double rValue;                  /* Constraint value. */
130355         -  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
       132825  +  RtreeDValue rValue;             /* Constraint value. */
       132826  +  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
130356 132827     sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
130357 132828   };
130358 132829   
130359 132830   /* Possible values for RtreeConstraint.op */
130360 132831   #define RTREE_EQ    0x41
130361 132832   #define RTREE_LE    0x42
130362 132833   #define RTREE_LT    0x43
................................................................................
130396 132867   /*
130397 132868   ** An instance of this structure must be supplied as a blob argument to
130398 132869   ** the right-hand-side of an SQL MATCH operator used to constrain an
130399 132870   ** r-tree query.
130400 132871   */
130401 132872   struct RtreeMatchArg {
130402 132873     u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
130403         -  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
       132874  +  int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
130404 132875     void *pContext;
130405 132876     int nParam;
130406         -  double aParam[1];
       132877  +  RtreeDValue aParam[1];
130407 132878   };
130408 132879   
130409 132880   /*
130410 132881   ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
130411 132882   ** a single instance of the following structure is allocated. It is used
130412 132883   ** as the context for the user-function created by by s_r_g_c(). The object
130413 132884   ** is eventually deleted by the destructor mechanism provided by
130414 132885   ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
130415 132886   ** the geometry callback function).
130416 132887   */
130417 132888   struct RtreeGeomCallback {
130418         -  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
       132889  +  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
130419 132890     void *pContext;
130420 132891   };
130421 132892   
130422 132893   #ifndef MAX
130423 132894   # define MAX(x,y) ((x) < (y) ? (y) : (x))
130424 132895   #endif
130425 132896   #ifndef MIN
................................................................................
130977 133448   static int testRtreeGeom(
130978 133449     Rtree *pRtree,                  /* R-Tree object */
130979 133450     RtreeConstraint *pConstraint,   /* MATCH constraint to test */
130980 133451     RtreeCell *pCell,               /* Cell to test */
130981 133452     int *pbRes                      /* OUT: Test result */
130982 133453   ){
130983 133454     int i;
130984         -  double aCoord[RTREE_MAX_DIMENSIONS*2];
       133455  +  RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
130985 133456     int nCoord = pRtree->nDim*2;
130986 133457   
130987 133458     assert( pConstraint->op==RTREE_MATCH );
130988 133459     assert( pConstraint->pGeom );
130989 133460   
130990 133461     for(i=0; i<nCoord; i++){
130991 133462       aCoord[i] = DCOORD(pCell->aCoord[i]);
................................................................................
131007 133478     int ii;
131008 133479     int bRes = 0;
131009 133480     int rc = SQLITE_OK;
131010 133481   
131011 133482     nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
131012 133483     for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
131013 133484       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]);
       133485  +    RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
       133486  +    RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
131016 133487   
131017 133488       assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
131018 133489           || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
131019 133490       );
131020 133491   
131021 133492       switch( p->op ){
131022 133493         case RTREE_LE: case RTREE_LT: 
................................................................................
131060 133531     RtreeCell cell;
131061 133532     int ii;
131062 133533     *pbEof = 0;
131063 133534   
131064 133535     nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
131065 133536     for(ii=0; ii<pCursor->nConstraint; ii++){
131066 133537       RtreeConstraint *p = &pCursor->aConstraint[ii];
131067         -    double coord = DCOORD(cell.aCoord[p->iCoord]);
       133538  +    RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
131068 133539       int res;
131069 133540       assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
131070 133541           || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
131071 133542       );
131072 133543       switch( p->op ){
131073 133544         case RTREE_LE: res = (coord<=p->rValue); break;
131074 133545         case RTREE_LT: res = (coord<p->rValue);  break;
................................................................................
131258 133729   
131259 133730     if( i==0 ){
131260 133731       i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
131261 133732       sqlite3_result_int64(ctx, iRowid);
131262 133733     }else{
131263 133734       RtreeCoord c;
131264 133735       nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
       133736  +#ifndef SQLITE_RTREE_INT_ONLY
131265 133737       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
131266 133738         sqlite3_result_double(ctx, c.f);
131267         -    }else{
       133739  +    }else
       133740  +#endif
       133741  +    {
131268 133742         assert( pRtree->eCoordType==RTREE_COORD_INT32 );
131269 133743         sqlite3_result_int(ctx, c.i);
131270 133744       }
131271 133745     }
131272 133746   
131273 133747     return SQLITE_OK;
131274 133748   }
................................................................................
131307 133781   
131308 133782     /* Check that value is actually a blob. */
131309 133783     if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
131310 133784   
131311 133785     /* Check that the blob is roughly the right size. */
131312 133786     nBlob = sqlite3_value_bytes(pValue);
131313 133787     if( nBlob<(int)sizeof(RtreeMatchArg) 
131314         -   || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
       133788  +   || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
131315 133789     ){
131316 133790       return SQLITE_ERROR;
131317 133791     }
131318 133792   
131319 133793     pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
131320 133794         sizeof(sqlite3_rtree_geometry) + nBlob
131321 133795     );
131322 133796     if( !pGeom ) return SQLITE_NOMEM;
131323 133797     memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
131324 133798     p = (RtreeMatchArg *)&pGeom[1];
131325 133799   
131326 133800     memcpy(p, sqlite3_value_blob(pValue), nBlob);
131327 133801     if( p->magic!=RTREE_GEOMETRY_MAGIC 
131328         -   || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
       133802  +   || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
131329 133803     ){
131330 133804       sqlite3_free(pGeom);
131331 133805       return SQLITE_ERROR;
131332 133806     }
131333 133807   
131334 133808     pGeom->pContext = p->pContext;
131335 133809     pGeom->nParam = p->nParam;
................................................................................
131393 133867               ** an sqlite3_rtree_geometry_callback() SQL user function.
131394 133868               */
131395 133869               rc = deserializeGeometry(argv[ii], p);
131396 133870               if( rc!=SQLITE_OK ){
131397 133871                 break;
131398 133872               }
131399 133873             }else{
       133874  +#ifdef SQLITE_RTREE_INT_ONLY
       133875  +            p->rValue = sqlite3_value_int64(argv[ii]);
       133876  +#else
131400 133877               p->rValue = sqlite3_value_double(argv[ii]);
       133878  +#endif
131401 133879             }
131402 133880           }
131403 133881         }
131404 133882       }
131405 133883     
131406 133884       if( rc==SQLITE_OK ){
131407 133885         pCsr->pNode = 0;
................................................................................
131527 134005     pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
131528 134006     return rc;
131529 134007   }
131530 134008   
131531 134009   /*
131532 134010   ** Return the N-dimensional volumn of the cell stored in *p.
131533 134011   */
131534         -static float cellArea(Rtree *pRtree, RtreeCell *p){
131535         -  float area = 1.0;
       134012  +static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
       134013  +  RtreeDValue area = (RtreeDValue)1;
131536 134014     int ii;
131537 134015     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
131538         -    area = (float)(area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
       134016  +    area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
131539 134017     }
131540 134018     return area;
131541 134019   }
131542 134020   
131543 134021   /*
131544 134022   ** Return the margin length of cell p. The margin length is the sum
131545 134023   ** of the objects size in each dimension.
131546 134024   */
131547         -static float cellMargin(Rtree *pRtree, RtreeCell *p){
131548         -  float margin = 0.0;
       134025  +static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
       134026  +  RtreeDValue margin = (RtreeDValue)0;
131549 134027     int ii;
131550 134028     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
131551         -    margin += (float)(DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
       134029  +    margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
131552 134030     }
131553 134031     return margin;
131554 134032   }
131555 134033   
131556 134034   /*
131557 134035   ** Store the union of cells p1 and p2 in p1.
131558 134036   */
................................................................................
131589 134067     }
131590 134068     return 1;
131591 134069   }
131592 134070   
131593 134071   /*
131594 134072   ** Return the amount cell p would grow by if it were unioned with pCell.
131595 134073   */
131596         -static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
131597         -  float area;
       134074  +static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
       134075  +  RtreeDValue area;
131598 134076     RtreeCell cell;
131599 134077     memcpy(&cell, p, sizeof(RtreeCell));
131600 134078     area = cellArea(pRtree, &cell);
131601 134079     cellUnion(pRtree, &cell, pCell);
131602 134080     return (cellArea(pRtree, &cell)-area);
131603 134081   }
131604 134082   
131605 134083   #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
131606         -static float cellOverlap(
       134084  +static RtreeDValue cellOverlap(
131607 134085     Rtree *pRtree, 
131608 134086     RtreeCell *p, 
131609 134087     RtreeCell *aCell, 
131610 134088     int nCell, 
131611 134089     int iExclude
131612 134090   ){
131613 134091     int ii;
131614         -  float overlap = 0.0;
       134092  +  RtreeDValue overlap = 0.0;
131615 134093     for(ii=0; ii<nCell; ii++){
131616 134094   #if VARIANT_RSTARTREE_CHOOSESUBTREE
131617 134095       if( ii!=iExclude )
131618 134096   #else
131619 134097       assert( iExclude==-1 );
131620 134098       UNUSED_PARAMETER(iExclude);
131621 134099   #endif
131622 134100       {
131623 134101         int jj;
131624         -      float o = 1.0;
       134102  +      RtreeDValue o = (RtreeDValue)1;
131625 134103         for(jj=0; jj<(pRtree->nDim*2); jj+=2){
131626         -        double x1;
131627         -        double x2;
       134104  +        RtreeDValue x1, x2;
131628 134105   
131629 134106           x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
131630 134107           x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
131631 134108   
131632 134109           if( x2<x1 ){
131633 134110             o = 0.0;
131634 134111             break;
131635 134112           }else{
131636         -          o = o * (float)(x2-x1);
       134113  +          o = o * (x2-x1);
131637 134114           }
131638 134115         }
131639 134116         overlap += o;
131640 134117       }
131641 134118     }
131642 134119     return overlap;
131643 134120   }
131644 134121   #endif
131645 134122   
131646 134123   #if VARIANT_RSTARTREE_CHOOSESUBTREE
131647         -static float cellOverlapEnlargement(
       134124  +static RtreeDValue cellOverlapEnlargement(
131648 134125     Rtree *pRtree, 
131649 134126     RtreeCell *p, 
131650 134127     RtreeCell *pInsert, 
131651 134128     RtreeCell *aCell, 
131652 134129     int nCell, 
131653 134130     int iExclude
131654 134131   ){
131655         -  double before;
131656         -  double after;
       134132  +  RtreeDValue before, after;
131657 134133     before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
131658 134134     cellUnion(pRtree, p, pInsert);
131659 134135     after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
131660         -  return (float)(after-before);
       134136  +  return (after-before);
131661 134137   }
131662 134138   #endif
131663 134139   
131664 134140   
131665 134141   /*
131666 134142   ** This function implements the ChooseLeaf algorithm from Gutman[84].
131667 134143   ** ChooseSubTree in r*tree terminology.
................................................................................
131677 134153     RtreeNode *pNode;
131678 134154     rc = nodeAcquire(pRtree, 1, 0, &pNode);
131679 134155   
131680 134156     for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
131681 134157       int iCell;
131682 134158       sqlite3_int64 iBest = 0;
131683 134159   
131684         -    float fMinGrowth = 0.0;
131685         -    float fMinArea = 0.0;
       134160  +    RtreeDValue fMinGrowth = 0.0;
       134161  +    RtreeDValue fMinArea = 0.0;
131686 134162   #if VARIANT_RSTARTREE_CHOOSESUBTREE
131687         -    float fMinOverlap = 0.0;
131688         -    float overlap;
       134163  +    RtreeDValue fMinOverlap = 0.0;
       134164  +    RtreeDValue overlap;
131689 134165   #endif
131690 134166   
131691 134167       int nCell = NCELL(pNode);
131692 134168       RtreeCell cell;
131693 134169       RtreeNode *pChild;
131694 134170   
131695 134171       RtreeCell *aCell = 0;
................................................................................
131712 134188   
131713 134189       /* Select the child node which will be enlarged the least if pCell
131714 134190       ** is inserted into it. Resolve ties by choosing the entry with
131715 134191       ** the smallest area.
131716 134192       */
131717 134193       for(iCell=0; iCell<nCell; iCell++){
131718 134194         int bBest = 0;
131719         -      float growth;
131720         -      float area;
       134195  +      RtreeDValue growth;
       134196  +      RtreeDValue area;
131721 134197         nodeGetCell(pRtree, pNode, iCell, &cell);
131722 134198         growth = cellGrowth(pRtree, &cell, pCell);
131723 134199         area = cellArea(pRtree, &cell);
131724 134200   
131725 134201   #if VARIANT_RSTARTREE_CHOOSESUBTREE
131726 134202         if( ii==(pRtree->iDepth-1) ){
131727 134203           overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
................................................................................
131840 134316     int nCell, 
131841 134317     int *piLeftSeed, 
131842 134318     int *piRightSeed
131843 134319   ){
131844 134320     int i;
131845 134321     int iLeftSeed = 0;
131846 134322     int iRightSeed = 1;
131847         -  float maxNormalInnerWidth = 0.0;
       134323  +  RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
131848 134324   
131849 134325     /* Pick two "seed" cells from the array of cells. The algorithm used
131850 134326     ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
131851 134327     ** indices of the two seed cells in the array are stored in local
131852 134328     ** variables iLeftSeek and iRightSeed.
131853 134329     */
131854 134330     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;
       134331  +    RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
       134332  +    RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
       134333  +    RtreeDValue x3 = x1;
       134334  +    RtreeDValue x4 = x2;
131859 134335       int jj;
131860 134336   
131861 134337       int iCellLeft = 0;
131862 134338       int iCellRight = 0;
131863 134339   
131864 134340       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]);
       134341  +      RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
       134342  +      RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
131867 134343   
131868 134344         if( left<x1 ) x1 = left;
131869 134345         if( right>x4 ) x4 = right;
131870 134346         if( left>x3 ){
131871 134347           x3 = left;
131872 134348           iCellRight = jj;
131873 134349         }
................................................................................
131874 134350         if( right<x2 ){
131875 134351           x2 = right;
131876 134352           iCellLeft = jj;
131877 134353         }
131878 134354       }
131879 134355   
131880 134356       if( x4!=x1 ){
131881         -      float normalwidth = (x3 - x2) / (x4 - x1);
       134357  +      RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
131882 134358         if( normalwidth>maxNormalInnerWidth ){
131883 134359           iLeftSeed = iCellLeft;
131884 134360           iRightSeed = iCellRight;
131885 134361         }
131886 134362       }
131887 134363     }
131888 134364   
................................................................................
131903 134379     RtreeCell *pLeftBox, 
131904 134380     RtreeCell *pRightBox,
131905 134381     int *aiUsed
131906 134382   ){
131907 134383     #define FABS(a) ((a)<0.0?-1.0*(a):(a))
131908 134384   
131909 134385     int iSelect = -1;
131910         -  float fDiff;
       134386  +  RtreeDValue fDiff;
131911 134387     int ii;
131912 134388     for(ii=0; ii<nCell; ii++){
131913 134389       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);
       134390  +      RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
       134391  +      RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
       134392  +      RtreeDValue diff = FABS(right-left);
131917 134393         if( iSelect<0 || diff>fDiff ){
131918 134394           fDiff = diff;
131919 134395           iSelect = ii;
131920 134396         }
131921 134397       }
131922 134398     }
131923 134399     aiUsed[iSelect] = 1;
................................................................................
131936 134412     int *piRightSeed
131937 134413   ){
131938 134414     int ii;
131939 134415     int jj;
131940 134416   
131941 134417     int iLeftSeed = 0;
131942 134418     int iRightSeed = 1;
131943         -  float fWaste = 0.0;
       134419  +  RtreeDValue fWaste = 0.0;
131944 134420   
131945 134421     for(ii=0; ii<nCell; ii++){
131946 134422       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;
       134423  +      RtreeDValue right = cellArea(pRtree, &aCell[jj]);
       134424  +      RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
       134425  +      RtreeDValue waste = growth - right;
131950 134426   
131951 134427         if( waste>fWaste ){
131952 134428           iLeftSeed = ii;
131953 134429           iRightSeed = jj;
131954 134430           fWaste = waste;
131955 134431         }
131956 134432       }
................................................................................
131977 134453   **
131978 134454   ** The aSpare array is used as temporary working space by the
131979 134455   ** sorting algorithm.
131980 134456   */
131981 134457   static void SortByDistance(
131982 134458     int *aIdx, 
131983 134459     int nIdx, 
131984         -  float *aDistance, 
       134460  +  RtreeDValue *aDistance, 
131985 134461     int *aSpare
131986 134462   ){
131987 134463     if( nIdx>1 ){
131988 134464       int iLeft = 0;
131989 134465       int iRight = 0;
131990 134466   
131991 134467       int nLeft = nIdx/2;
................................................................................
132003 134479         if( iLeft==nLeft ){
132004 134480           aIdx[iLeft+iRight] = aRight[iRight];
132005 134481           iRight++;
132006 134482         }else if( iRight==nRight ){
132007 134483           aIdx[iLeft+iRight] = aLeft[iLeft];
132008 134484           iLeft++;
132009 134485         }else{
132010         -        float fLeft = aDistance[aLeft[iLeft]];
132011         -        float fRight = aDistance[aRight[iRight]];
       134486  +        RtreeDValue fLeft = aDistance[aLeft[iLeft]];
       134487  +        RtreeDValue fRight = aDistance[aRight[iRight]];
132012 134488           if( fLeft<fRight ){
132013 134489             aIdx[iLeft+iRight] = aLeft[iLeft];
132014 134490             iLeft++;
132015 134491           }else{
132016 134492             aIdx[iLeft+iRight] = aRight[iRight];
132017 134493             iRight++;
132018 134494           }
................................................................................
132020 134496       }
132021 134497   
132022 134498   #if 0
132023 134499       /* Check that the sort worked */
132024 134500       {
132025 134501         int jj;
132026 134502         for(jj=1; jj<nIdx; jj++){
132027         -        float left = aDistance[aIdx[jj-1]];
132028         -        float right = aDistance[aIdx[jj]];
       134503  +        RtreeDValue left = aDistance[aIdx[jj-1]];
       134504  +        RtreeDValue right = aDistance[aIdx[jj]];
132029 134505           assert( left<=right );
132030 134506         }
132031 134507       }
132032 134508   #endif
132033 134509     }
132034 134510   }
132035 134511   
................................................................................
132064 134540   
132065 134541       SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
132066 134542       SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
132067 134543   
132068 134544       memcpy(aSpare, aLeft, sizeof(int)*nLeft);
132069 134545       aLeft = aSpare;
132070 134546       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]);
       134547  +      RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
       134548  +      RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
       134549  +      RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
       134550  +      RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
132075 134551         if( (iLeft!=nLeft) && ((iRight==nRight)
132076 134552          || (xleft1<xright1)
132077 134553          || (xleft1==xright1 && xleft2<xright2)
132078 134554         )){
132079 134555           aIdx[iLeft+iRight] = aLeft[iLeft];
132080 134556           iLeft++;
132081 134557         }else{
................................................................................
132085 134561       }
132086 134562   
132087 134563   #if 0
132088 134564       /* Check that the sort worked */
132089 134565       {
132090 134566         int jj;
132091 134567         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];
       134568  +        RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
       134569  +        RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
       134570  +        RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
       134571  +        RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
132096 134572           assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
132097 134573         }
132098 134574       }
132099 134575   #endif
132100 134576     }
132101 134577   }
132102 134578   
................................................................................
132115 134591   ){
132116 134592     int **aaSorted;
132117 134593     int *aSpare;
132118 134594     int ii;
132119 134595   
132120 134596     int iBestDim = 0;
132121 134597     int iBestSplit = 0;
132122         -  float fBestMargin = 0.0;
       134598  +  RtreeDValue fBestMargin = 0.0;
132123 134599   
132124 134600     int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
132125 134601   
132126 134602     aaSorted = (int **)sqlite3_malloc(nByte);
132127 134603     if( !aaSorted ){
132128 134604       return SQLITE_NOMEM;
132129 134605     }
................................................................................
132136 134612       for(jj=0; jj<nCell; jj++){
132137 134613         aaSorted[ii][jj] = jj;
132138 134614       }
132139 134615       SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
132140 134616     }
132141 134617   
132142 134618     for(ii=0; ii<pRtree->nDim; ii++){
132143         -    float margin = 0.0;
132144         -    float fBestOverlap = 0.0;
132145         -    float fBestArea = 0.0;
       134619  +    RtreeDValue margin = 0.0;
       134620  +    RtreeDValue fBestOverlap = 0.0;
       134621  +    RtreeDValue fBestArea = 0.0;
132146 134622       int iBestLeft = 0;
132147 134623       int nLeft;
132148 134624   
132149 134625       for(
132150 134626         nLeft=RTREE_MINCELLS(pRtree); 
132151 134627         nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 
132152 134628         nLeft++
132153 134629       ){
132154 134630         RtreeCell left;
132155 134631         RtreeCell right;
132156 134632         int kk;
132157         -      float overlap;
132158         -      float area;
       134633  +      RtreeDValue overlap;
       134634  +      RtreeDValue area;
132159 134635   
132160 134636         memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
132161 134637         memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
132162 134638         for(kk=1; kk<(nCell-1); kk++){
132163 134639           if( kk<nLeft ){
132164 134640             cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
132165 134641           }else{
................................................................................
132234 134710     nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
132235 134711     aiUsed[iLeftSeed] = 1;
132236 134712     aiUsed[iRightSeed] = 1;
132237 134713   
132238 134714     for(i=nCell-2; i>0; i--){
132239 134715       RtreeCell *pNext;
132240 134716       pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
132241         -    float diff =  
       134717  +    RtreeDValue diff =  
132242 134718         cellGrowth(pRtree, pBboxLeft, pNext) - 
132243 134719         cellGrowth(pRtree, pBboxRight, pNext)
132244 134720       ;
132245 134721       if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
132246 134722        || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
132247 134723       ){
132248 134724         nodeInsertCell(pRtree, pRight, pNext);
................................................................................
132567 135043     RtreeNode *pNode, 
132568 135044     RtreeCell *pCell, 
132569 135045     int iHeight
132570 135046   ){
132571 135047     int *aOrder;
132572 135048     int *aSpare;
132573 135049     RtreeCell *aCell;
132574         -  float *aDistance;
       135050  +  RtreeDValue *aDistance;
132575 135051     int nCell;
132576         -  float aCenterCoord[RTREE_MAX_DIMENSIONS];
       135052  +  RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
132577 135053     int iDim;
132578 135054     int ii;
132579 135055     int rc = SQLITE_OK;
       135056  +  int n;
132580 135057   
132581         -  memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
       135058  +  memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
132582 135059   
132583 135060     nCell = NCELL(pNode)+1;
       135061  +  n = (nCell+1)&(~1);
132584 135062   
132585 135063     /* Allocate the buffers used by this operation. The allocation is
132586 135064     ** relinquished before this function returns.
132587 135065     */
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 */
       135066  +  aCell = (RtreeCell *)sqlite3_malloc(n * (
       135067  +    sizeof(RtreeCell)     +         /* aCell array */
       135068  +    sizeof(int)           +         /* aOrder array */
       135069  +    sizeof(int)           +         /* aSpare array */
       135070  +    sizeof(RtreeDValue)             /* aDistance array */
132593 135071     ));
132594 135072     if( !aCell ){
132595 135073       return SQLITE_NOMEM;
132596 135074     }
132597         -  aOrder    = (int *)&aCell[nCell];
132598         -  aSpare    = (int *)&aOrder[nCell];
132599         -  aDistance = (float *)&aSpare[nCell];
       135075  +  aOrder    = (int *)&aCell[n];
       135076  +  aSpare    = (int *)&aOrder[n];
       135077  +  aDistance = (RtreeDValue *)&aSpare[n];
132600 135078   
132601 135079     for(ii=0; ii<nCell; ii++){
132602 135080       if( ii==(nCell-1) ){
132603 135081         memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
132604 135082       }else{
132605 135083         nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
132606 135084       }
132607 135085       aOrder[ii] = ii;
132608 135086       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]);
       135087  +      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
       135088  +      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
132611 135089       }
132612 135090     }
132613 135091     for(iDim=0; iDim<pRtree->nDim; iDim++){
132614         -    aCenterCoord[iDim] = (float)(aCenterCoord[iDim]/((float)nCell*2.0));
       135092  +    aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
132615 135093     }
132616 135094   
132617 135095     for(ii=0; ii<nCell; ii++){
132618 135096       aDistance[ii] = 0.0;
132619 135097       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]));
       135098  +      RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
       135099  +                               DCOORD(aCell[ii].aCoord[iDim*2]));
132622 135100         aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
132623 135101       }
132624 135102     }
132625 135103   
132626 135104     SortByDistance(aOrder, nCell, aDistance, aSpare);
132627 135105     nodeZero(pRtree, pNode);
132628 135106   
................................................................................
132856 135334     ** conflict-handling mode specified by the user.
132857 135335     */
132858 135336     if( nData>1 ){
132859 135337       int ii;
132860 135338   
132861 135339       /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
132862 135340       assert( nData==(pRtree->nDim*2 + 3) );
       135341  +#ifndef SQLITE_RTREE_INT_ONLY
132863 135342       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
132864 135343         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]);
       135344  +        cell.aCoord[ii].f = (RtreeValue)sqlite3_value_double(azData[ii+3]);
       135345  +        cell.aCoord[ii+1].f = (RtreeValue)sqlite3_value_double(azData[ii+4]);
132867 135346           if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
132868 135347             rc = SQLITE_CONSTRAINT;
132869 135348             goto constraint;
132870 135349           }
132871 135350         }
132872         -    }else{
       135351  +    }else
       135352  +#endif
       135353  +    {
132873 135354         for(ii=0; ii<(pRtree->nDim*2); ii+=2){
132874 135355           cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
132875 135356           cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
132876 135357           if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
132877 135358             rc = SQLITE_CONSTRAINT;
132878 135359             goto constraint;
132879 135360           }
................................................................................
133263 135744       RtreeCell cell;
133264 135745       int jj;
133265 135746   
133266 135747       nodeGetCell(&tree, &node, ii, &cell);
133267 135748       sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
133268 135749       nCell = (int)strlen(zCell);
133269 135750       for(jj=0; jj<tree.nDim*2; jj++){
133270         -      sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
       135751  +#ifndef SQLITE_RTREE_INT_ONLY
       135752  +      sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
       135753  +                       (double)cell.aCoord[jj].f);
       135754  +#else
       135755  +      sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
       135756  +                       cell.aCoord[jj].i);
       135757  +#endif
133271 135758         nCell = (int)strlen(zCell);
133272 135759       }
133273 135760   
133274 135761       if( zText ){
133275 135762         char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
133276 135763         sqlite3_free(zText);
133277 135764         zText = zTextNew;
................................................................................
133305 135792     int rc;
133306 135793   
133307 135794     rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
133308 135795     if( rc==SQLITE_OK ){
133309 135796       rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
133310 135797     }
133311 135798     if( rc==SQLITE_OK ){
       135799  +#ifdef SQLITE_RTREE_INT_ONLY
       135800  +    void *c = (void *)RTREE_COORD_INT32;
       135801  +#else
133312 135802       void *c = (void *)RTREE_COORD_REAL32;
       135803  +#endif
133313 135804       rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
133314 135805     }
133315 135806     if( rc==SQLITE_OK ){
133316 135807       void *c = (void *)RTREE_COORD_INT32;
133317 135808       rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
133318 135809     }
133319 135810   
................................................................................
133339 135830   ** table MATCH operators.
133340 135831   */
133341 135832   static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
133342 135833     RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
133343 135834     RtreeMatchArg *pBlob;
133344 135835     int nBlob;
133345 135836   
133346         -  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
       135837  +  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
133347 135838     pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
133348 135839     if( !pBlob ){
133349 135840       sqlite3_result_error_nomem(ctx);
133350 135841     }else{
133351 135842       int i;
133352 135843       pBlob->magic = RTREE_GEOMETRY_MAGIC;
133353 135844       pBlob->xGeom = pGeomCtx->xGeom;
133354 135845       pBlob->pContext = pGeomCtx->pContext;
133355 135846       pBlob->nParam = nArg;
133356 135847       for(i=0; i<nArg; i++){
       135848  +#ifdef SQLITE_RTREE_INT_ONLY
       135849  +      pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
       135850  +#else
133357 135851         pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
       135852  +#endif
133358 135853       }
133359 135854       sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
133360 135855     }
133361 135856   }
133362 135857   
133363 135858   /*
133364 135859   ** Register a new geometry function for use with the r-tree MATCH operator.
133365 135860   */
133366 135861   SQLITE_API int sqlite3_rtree_geometry_callback(
133367 135862     sqlite3 *db,
133368 135863     const char *zGeom,
133369         -  int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
       135864  +  int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
133370 135865     void *pContext
133371 135866   ){
133372 135867     RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
133373 135868   
133374 135869     /* Allocate and populate the context object. */
133375 135870     pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
133376 135871     if( !pGeomCtx ) return SQLITE_NOMEM;
................................................................................
134007 136502   
134008 136503     UChar32 c;
134009 136504     int iInput = 0;
134010 136505     int iOut = 0;
134011 136506   
134012 136507     *ppCursor = 0;
134013 136508   
134014         -  if( nInput<0 ){
       136509  +  if( zInput==0 ){
       136510  +    nInput = 0;
       136511  +    zInput = "";
       136512  +  }else if( nInput<0 ){
134015 136513       nInput = strlen(zInput);
134016 136514     }
134017 136515     nChar = nInput+1;
134018 136516     pCsr = (IcuCursor *)sqlite3_malloc(
134019 136517         sizeof(IcuCursor) +                /* IcuCursor */
134020 136518         nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
134021 136519         (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */

Changes to src/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-19 14:57:49 bc03d99a78e90c02b69037e5f5f81537b5a3ac60"
          110  +#define SQLITE_VERSION        "3.7.12"
          111  +#define SQLITE_VERSION_NUMBER 3007012
          112  +#define SQLITE_SOURCE_ID      "2012-05-12 01:14:27 93061c6e063fddfa6b5d21064a36b58cc97599f1"
   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
................................................................................
  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().