| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.7.8. By combining all the individual C code files into this |
| 3 | +** version 3.7.9. 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. |
| | @@ -394,11 +394,11 @@ |
| 394 | 394 | ** assert() macro is enabled, each call into the Win32 native heap subsystem |
| 395 | 395 | ** will cause HeapValidate to be called. If heap validation should fail, an |
| 396 | 396 | ** assertion will be triggered. |
| 397 | 397 | ** |
| 398 | 398 | ** (Historical note: There used to be several other options, but we've |
| 399 | | -** pared it down to just these two.) |
| 399 | +** pared it down to just these three.) |
| 400 | 400 | ** |
| 401 | 401 | ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as |
| 402 | 402 | ** the default. |
| 403 | 403 | */ |
| 404 | 404 | #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1 |
| | @@ -654,13 +654,13 @@ |
| 654 | 654 | ** |
| 655 | 655 | ** See also: [sqlite3_libversion()], |
| 656 | 656 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 657 | 657 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 658 | 658 | */ |
| 659 | | -#define SQLITE_VERSION "3.7.8" |
| 660 | | -#define SQLITE_VERSION_NUMBER 3007008 |
| 661 | | -#define SQLITE_SOURCE_ID "2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177" |
| 659 | +#define SQLITE_VERSION "3.7.9" |
| 660 | +#define SQLITE_VERSION_NUMBER 3007009 |
| 661 | +#define SQLITE_SOURCE_ID "2011-10-20 00:55:54 4344483f7d7f64dffadde0053e6c745948db9486" |
| 662 | 662 | |
| 663 | 663 | /* |
| 664 | 664 | ** CAPI3REF: Run-Time Library Version Numbers |
| 665 | 665 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 666 | 666 | ** |
| | @@ -1318,11 +1318,15 @@ |
| 1318 | 1318 | ** in order for the database to be readable. The fourth parameter to |
| 1319 | 1319 | ** [sqlite3_file_control()] for this opcode should be a pointer to an integer. |
| 1320 | 1320 | ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent |
| 1321 | 1321 | ** WAL mode. If the integer is -1, then it is overwritten with the current |
| 1322 | 1322 | ** WAL persistence setting. |
| 1323 | | -** |
| 1323 | +** |
| 1324 | +** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening |
| 1325 | +** a write transaction to indicate that, unless it is rolled back for some |
| 1326 | +** reason, the entire database file will be overwritten by the current |
| 1327 | +** transaction. This is used by VACUUM operations. |
| 1324 | 1328 | */ |
| 1325 | 1329 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 1326 | 1330 | #define SQLITE_GET_LOCKPROXYFILE 2 |
| 1327 | 1331 | #define SQLITE_SET_LOCKPROXYFILE 3 |
| 1328 | 1332 | #define SQLITE_LAST_ERRNO 4 |
| | @@ -1330,10 +1334,11 @@ |
| 1330 | 1334 | #define SQLITE_FCNTL_CHUNK_SIZE 6 |
| 1331 | 1335 | #define SQLITE_FCNTL_FILE_POINTER 7 |
| 1332 | 1336 | #define SQLITE_FCNTL_SYNC_OMITTED 8 |
| 1333 | 1337 | #define SQLITE_FCNTL_WIN32_AV_RETRY 9 |
| 1334 | 1338 | #define SQLITE_FCNTL_PERSIST_WAL 10 |
| 1339 | +#define SQLITE_FCNTL_OVERWRITE 11 |
| 1335 | 1340 | |
| 1336 | 1341 | /* |
| 1337 | 1342 | ** CAPI3REF: Mutex Handle |
| 1338 | 1343 | ** |
| 1339 | 1344 | ** The mutex module within SQLite defines [sqlite3_mutex] to be an |
| | @@ -1946,12 +1951,12 @@ |
| 1946 | 1951 | ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or |
| 1947 | 1952 | ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory |
| 1948 | 1953 | ** allocator is engaged to handle all of SQLites memory allocation needs. |
| 1949 | 1954 | ** The first pointer (the memory pointer) must be aligned to an 8-byte |
| 1950 | 1955 | ** boundary or subsequent behavior of SQLite will be undefined. |
| 1951 | | -** The minimum allocation size is capped at 2^12. Reasonable values |
| 1952 | | -** for the minimum allocation size are 2^5 through 2^8.</dd> |
| 1956 | +** The minimum allocation size is capped at 2**12. Reasonable values |
| 1957 | +** for the minimum allocation size are 2**5 through 2**8.</dd> |
| 1953 | 1958 | ** |
| 1954 | 1959 | ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> |
| 1955 | 1960 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1956 | 1961 | ** instance of the [sqlite3_mutex_methods] structure. The argument specifies |
| 1957 | 1962 | ** alternative low-level mutex routines to be used in place |
| | @@ -3346,11 +3351,12 @@ |
| 3346 | 3351 | ** zSql string ends at either the first '\000' or '\u0000' character or |
| 3347 | 3352 | ** the nByte-th byte, whichever comes first. If the caller knows |
| 3348 | 3353 | ** that the supplied string is nul-terminated, then there is a small |
| 3349 | 3354 | ** performance advantage to be gained by passing an nByte parameter that |
| 3350 | 3355 | ** is equal to the number of bytes in the input string <i>including</i> |
| 3351 | | -** the nul-terminator bytes. |
| 3356 | +** the nul-terminator bytes as this saves SQLite from having to |
| 3357 | +** make a copy of the input string. |
| 3352 | 3358 | ** |
| 3353 | 3359 | ** ^If pzTail is not NULL then *pzTail is made to point to the first byte |
| 3354 | 3360 | ** past the end of the first SQL statement in zSql. These routines only |
| 3355 | 3361 | ** compile the first statement in zSql, so *pzTail is left pointing to |
| 3356 | 3362 | ** what remains uncompiled. |
| | @@ -3397,11 +3403,11 @@ |
| 3397 | 3403 | ** a schema change, on the first [sqlite3_step()] call following any change |
| 3398 | 3404 | ** to the [sqlite3_bind_text | bindings] of that [parameter]. |
| 3399 | 3405 | ** ^The specific value of WHERE-clause [parameter] might influence the |
| 3400 | 3406 | ** choice of query plan if the parameter is the left-hand side of a [LIKE] |
| 3401 | 3407 | ** or [GLOB] operator or if the parameter is compared to an indexed column |
| 3402 | | -** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled. |
| 3408 | +** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled. |
| 3403 | 3409 | ** the |
| 3404 | 3410 | ** </li> |
| 3405 | 3411 | ** </ol> |
| 3406 | 3412 | */ |
| 3407 | 3413 | SQLITE_API int sqlite3_prepare( |
| | @@ -3567,10 +3573,17 @@ |
| 3567 | 3573 | ** ^(In those routines that have a fourth argument, its value is the |
| 3568 | 3574 | ** number of bytes in the parameter. To be clear: the value is the |
| 3569 | 3575 | ** number of <u>bytes</u> in the value, not the number of characters.)^ |
| 3570 | 3576 | ** ^If the fourth parameter is negative, the length of the string is |
| 3571 | 3577 | ** the number of bytes up to the first zero terminator. |
| 3578 | +** If a non-negative fourth parameter is provided to sqlite3_bind_text() |
| 3579 | +** or sqlite3_bind_text16() then that parameter must be the byte offset |
| 3580 | +** where the NUL terminator would occur assuming the string were NUL |
| 3581 | +** terminated. If any NUL characters occur at byte offsets less than |
| 3582 | +** the value of the fourth parameter then the resulting string value will |
| 3583 | +** contain embedded NULs. The result of expressions involving strings |
| 3584 | +** with embedded NULs is undefined. |
| 3572 | 3585 | ** |
| 3573 | 3586 | ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and |
| 3574 | 3587 | ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or |
| 3575 | 3588 | ** string after SQLite has finished with it. ^The destructor is called |
| 3576 | 3589 | ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(), |
| | @@ -3900,10 +3913,16 @@ |
| 3900 | 3913 | ** current row of the result set of [prepared statement] P. |
| 3901 | 3914 | ** ^If prepared statement P does not have results ready to return |
| 3902 | 3915 | ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of |
| 3903 | 3916 | ** interfaces) then sqlite3_data_count(P) returns 0. |
| 3904 | 3917 | ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. |
| 3918 | +** ^The sqlite3_data_count(P) routine returns 0 if the previous call to |
| 3919 | +** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P) |
| 3920 | +** will return non-zero if previous call to [sqlite3_step](P) returned |
| 3921 | +** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum] |
| 3922 | +** where it always returns zero since each step of that multi-step |
| 3923 | +** pragma returns 0 columns of data. |
| 3905 | 3924 | ** |
| 3906 | 3925 | ** See also: [sqlite3_column_count()] |
| 3907 | 3926 | */ |
| 3908 | 3927 | SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); |
| 3909 | 3928 | |
| | @@ -4579,11 +4598,16 @@ |
| 4579 | 4598 | ** is negative, then SQLite takes result text from the 2nd parameter |
| 4580 | 4599 | ** through the first zero character. |
| 4581 | 4600 | ** ^If the 3rd parameter to the sqlite3_result_text* interfaces |
| 4582 | 4601 | ** is non-negative, then as many bytes (not characters) of the text |
| 4583 | 4602 | ** pointed to by the 2nd parameter are taken as the application-defined |
| 4584 | | -** function result. |
| 4603 | +** function result. If the 3rd parameter is non-negative, then it |
| 4604 | +** must be the byte offset into the string where the NUL terminator would |
| 4605 | +** appear if the string where NUL terminated. If any NUL characters occur |
| 4606 | +** in the string at a byte offset that is less than the value of the 3rd |
| 4607 | +** parameter, then the resulting string will contain embedded NULs and the |
| 4608 | +** result of expressions operating on strings with embedded NULs is undefined. |
| 4585 | 4609 | ** ^If the 4th parameter to the sqlite3_result_text* interfaces |
| 4586 | 4610 | ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that |
| 4587 | 4611 | ** function as the destructor on the text or BLOB result when it has |
| 4588 | 4612 | ** finished using that result. |
| 4589 | 4613 | ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to |
| | @@ -6362,20 +6386,34 @@ |
| 6362 | 6386 | ** <dd>This parameter returns the approximate number of of bytes of heap |
| 6363 | 6387 | ** and lookaside memory used by all prepared statements associated with |
| 6364 | 6388 | ** the database connection.)^ |
| 6365 | 6389 | ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0. |
| 6366 | 6390 | ** </dd> |
| 6391 | +** |
| 6392 | +** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt> |
| 6393 | +** <dd>This parameter returns the number of pager cache hits that have |
| 6394 | +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT |
| 6395 | +** is always 0. |
| 6396 | +** </dd> |
| 6397 | +** |
| 6398 | +** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt> |
| 6399 | +** <dd>This parameter returns the number of pager cache misses that have |
| 6400 | +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS |
| 6401 | +** is always 0. |
| 6402 | +** </dd> |
| 6367 | 6403 | ** </dl> |
| 6368 | 6404 | */ |
| 6369 | 6405 | #define SQLITE_DBSTATUS_LOOKASIDE_USED 0 |
| 6370 | 6406 | #define SQLITE_DBSTATUS_CACHE_USED 1 |
| 6371 | 6407 | #define SQLITE_DBSTATUS_SCHEMA_USED 2 |
| 6372 | 6408 | #define SQLITE_DBSTATUS_STMT_USED 3 |
| 6373 | 6409 | #define SQLITE_DBSTATUS_LOOKASIDE_HIT 4 |
| 6374 | 6410 | #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5 |
| 6375 | 6411 | #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6 |
| 6376 | | -#define SQLITE_DBSTATUS_MAX 6 /* Largest defined DBSTATUS */ |
| 6412 | +#define SQLITE_DBSTATUS_CACHE_HIT 7 |
| 6413 | +#define SQLITE_DBSTATUS_CACHE_MISS 8 |
| 6414 | +#define SQLITE_DBSTATUS_MAX 8 /* Largest defined DBSTATUS */ |
| 6377 | 6415 | |
| 6378 | 6416 | |
| 6379 | 6417 | /* |
| 6380 | 6418 | ** CAPI3REF: Prepared Statement Status |
| 6381 | 6419 | ** |
| | @@ -6425,11 +6463,10 @@ |
| 6425 | 6463 | ** <dd>^This is the number of rows inserted into transient indices that |
| 6426 | 6464 | ** were created automatically in order to help joins run faster. |
| 6427 | 6465 | ** A non-zero value in this counter may indicate an opportunity to |
| 6428 | 6466 | ** improvement performance by adding permanent indices that do not |
| 6429 | 6467 | ** need to be reinitialized each time the statement is run.</dd> |
| 6430 | | -** |
| 6431 | 6468 | ** </dl> |
| 6432 | 6469 | */ |
| 6433 | 6470 | #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 |
| 6434 | 6471 | #define SQLITE_STMTSTATUS_SORT 2 |
| 6435 | 6472 | #define SQLITE_STMTSTATUS_AUTOINDEX 3 |
| | @@ -7711,10 +7748,22 @@ |
| 7711 | 7748 | ** is 0x00000000ffffffff. But because of quirks of some compilers, we |
| 7712 | 7749 | ** have to specify the value in the less intuitive manner shown: |
| 7713 | 7750 | */ |
| 7714 | 7751 | #define SQLITE_MAX_U32 ((((u64)1)<<32)-1) |
| 7715 | 7752 | |
| 7753 | +/* |
| 7754 | +** The datatype used to store estimates of the number of rows in a |
| 7755 | +** table or index. This is an unsigned integer type. For 99.9% of |
| 7756 | +** the world, a 32-bit integer is sufficient. But a 64-bit integer |
| 7757 | +** can be used at compile-time if desired. |
| 7758 | +*/ |
| 7759 | +#ifdef SQLITE_64BIT_STATS |
| 7760 | + typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */ |
| 7761 | +#else |
| 7762 | + typedef u32 tRowcnt; /* 32-bit is the default */ |
| 7763 | +#endif |
| 7764 | + |
| 7716 | 7765 | /* |
| 7717 | 7766 | ** Macros to determine whether the machine is big or little endian, |
| 7718 | 7767 | ** evaluated at runtime. |
| 7719 | 7768 | */ |
| 7720 | 7769 | #ifdef SQLITE_AMALGAMATION |
| | @@ -8742,10 +8791,11 @@ |
| 8742 | 8791 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); |
| 8743 | 8792 | SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); |
| 8744 | 8793 | SQLITE_PRIVATE int sqlite3PagerNosync(Pager*); |
| 8745 | 8794 | SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); |
| 8746 | 8795 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); |
| 8796 | +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *); |
| 8747 | 8797 | |
| 8748 | 8798 | /* Functions used to truncate the database file. */ |
| 8749 | 8799 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 8750 | 8800 | |
| 8751 | 8801 | #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) |
| | @@ -9278,18 +9328,21 @@ |
| 9278 | 9328 | /* |
| 9279 | 9329 | ** If this is a no-op implementation, implement everything as macros. |
| 9280 | 9330 | */ |
| 9281 | 9331 | #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) |
| 9282 | 9332 | #define sqlite3_mutex_free(X) |
| 9283 | | -#define sqlite3_mutex_enter(X) |
| 9333 | +#define sqlite3_mutex_enter(X) |
| 9284 | 9334 | #define sqlite3_mutex_try(X) SQLITE_OK |
| 9285 | | -#define sqlite3_mutex_leave(X) |
| 9335 | +#define sqlite3_mutex_leave(X) |
| 9286 | 9336 | #define sqlite3_mutex_held(X) ((void)(X),1) |
| 9287 | 9337 | #define sqlite3_mutex_notheld(X) ((void)(X),1) |
| 9288 | 9338 | #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8) |
| 9289 | 9339 | #define sqlite3MutexInit() SQLITE_OK |
| 9290 | 9340 | #define sqlite3MutexEnd() |
| 9341 | +#define MUTEX_LOGIC(X) |
| 9342 | +#else |
| 9343 | +#define MUTEX_LOGIC(X) X |
| 9291 | 9344 | #endif /* defined(SQLITE_MUTEX_OMIT) */ |
| 9292 | 9345 | |
| 9293 | 9346 | /************** End of mutex.h ***********************************************/ |
| 9294 | 9347 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 9295 | 9348 | |
| | @@ -9918,11 +9971,11 @@ |
| 9918 | 9971 | int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ |
| 9919 | 9972 | int nCol; /* Number of columns in this table */ |
| 9920 | 9973 | Column *aCol; /* Information about each column */ |
| 9921 | 9974 | Index *pIndex; /* List of SQL indexes on this table. */ |
| 9922 | 9975 | int tnum; /* Root BTree node for this table (see note above) */ |
| 9923 | | - unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 9976 | + tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 9924 | 9977 | Select *pSelect; /* NULL for tables. Points to definition if a view. */ |
| 9925 | 9978 | u16 nRef; /* Number of pointers to this Table */ |
| 9926 | 9979 | u8 tabFlags; /* Mask of TF_* values */ |
| 9927 | 9980 | u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ |
| 9928 | 9981 | FKey *pFKey; /* Linked list of all foreign keys in this table */ |
| | @@ -10117,11 +10170,11 @@ |
| 10117 | 10170 | */ |
| 10118 | 10171 | struct Index { |
| 10119 | 10172 | char *zName; /* Name of this index */ |
| 10120 | 10173 | int nColumn; /* Number of columns in the table used by this index */ |
| 10121 | 10174 | int *aiColumn; /* Which columns are used by this index. 1st is 0 */ |
| 10122 | | - unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ |
| 10175 | + tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ |
| 10123 | 10176 | Table *pTable; /* The SQL table being indexed */ |
| 10124 | 10177 | int tnum; /* Page containing root of this index in database file */ |
| 10125 | 10178 | u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 10126 | 10179 | u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ |
| 10127 | 10180 | u8 bUnordered; /* Use this index for == or IN queries only */ |
| | @@ -10128,24 +10181,32 @@ |
| 10128 | 10181 | char *zColAff; /* String defining the affinity of each column */ |
| 10129 | 10182 | Index *pNext; /* The next index associated with the same table */ |
| 10130 | 10183 | Schema *pSchema; /* Schema containing this index */ |
| 10131 | 10184 | u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */ |
| 10132 | 10185 | char **azColl; /* Array of collation sequence names for index */ |
| 10133 | | - IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */ |
| 10186 | +#ifdef SQLITE_ENABLE_STAT3 |
| 10187 | + int nSample; /* Number of elements in aSample[] */ |
| 10188 | + tRowcnt avgEq; /* Average nEq value for key values not in aSample */ |
| 10189 | + IndexSample *aSample; /* Samples of the left-most key */ |
| 10190 | +#endif |
| 10134 | 10191 | }; |
| 10135 | 10192 | |
| 10136 | 10193 | /* |
| 10137 | 10194 | ** Each sample stored in the sqlite_stat2 table is represented in memory |
| 10138 | 10195 | ** using a structure of this type. |
| 10139 | 10196 | */ |
| 10140 | 10197 | struct IndexSample { |
| 10141 | 10198 | union { |
| 10142 | 10199 | char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */ |
| 10143 | | - double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */ |
| 10200 | + double r; /* Value if eType is SQLITE_FLOAT */ |
| 10201 | + i64 i; /* Value if eType is SQLITE_INTEGER */ |
| 10144 | 10202 | } u; |
| 10145 | 10203 | u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */ |
| 10146 | | - u8 nByte; /* Size in byte of text or blob. */ |
| 10204 | + int nByte; /* Size in byte of text or blob. */ |
| 10205 | + tRowcnt nEq; /* Est. number of rows where the key equals this sample */ |
| 10206 | + tRowcnt nLt; /* Est. number of rows where key is less than this sample */ |
| 10207 | + tRowcnt nDLt; /* Est. number of distinct keys less than this sample */ |
| 10147 | 10208 | }; |
| 10148 | 10209 | |
| 10149 | 10210 | /* |
| 10150 | 10211 | ** Each token coming out of the lexer is an instance of |
| 10151 | 10212 | ** this structure. Tokens are also used as part of an expression. |
| | @@ -10593,14 +10654,14 @@ |
| 10593 | 10654 | #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ |
| 10594 | 10655 | #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */ |
| 10595 | 10656 | #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */ |
| 10596 | 10657 | #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ |
| 10597 | 10658 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 10598 | | -#define WHERE_OMIT_OPEN 0x0010 /* Table cursors are already open */ |
| 10599 | | -#define WHERE_OMIT_CLOSE 0x0020 /* Omit close of table & index cursors */ |
| 10600 | | -#define WHERE_FORCE_TABLE 0x0040 /* Do not use an index-only search */ |
| 10601 | | -#define WHERE_ONETABLE_ONLY 0x0080 /* Only code the 1st table in pTabList */ |
| 10659 | +#define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ |
| 10660 | +#define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ |
| 10661 | +#define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ |
| 10662 | +#define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */ |
| 10602 | 10663 | |
| 10603 | 10664 | /* |
| 10604 | 10665 | ** The WHERE clause processing routine has two halves. The |
| 10605 | 10666 | ** first part does the start of the WHERE loop and the second |
| 10606 | 10667 | ** half does the tail of the WHERE loop. An instance of |
| | @@ -11350,10 +11411,11 @@ |
| 11350 | 11411 | #else |
| 11351 | 11412 | # define sqlite3ViewGetColumnNames(A,B) 0 |
| 11352 | 11413 | #endif |
| 11353 | 11414 | |
| 11354 | 11415 | SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int); |
| 11416 | +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int); |
| 11355 | 11417 | SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*); |
| 11356 | 11418 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 11357 | 11419 | SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse); |
| 11358 | 11420 | SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse); |
| 11359 | 11421 | #else |
| | @@ -11606,11 +11668,11 @@ |
| 11606 | 11668 | SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, |
| 11607 | 11669 | void(*)(void*)); |
| 11608 | 11670 | SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*); |
| 11609 | 11671 | SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *); |
| 11610 | 11672 | SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8); |
| 11611 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 11673 | +#ifdef SQLITE_ENABLE_STAT3 |
| 11612 | 11674 | SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *); |
| 11613 | 11675 | #endif |
| 11614 | 11676 | SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **); |
| 11615 | 11677 | SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8); |
| 11616 | 11678 | #ifndef SQLITE_AMALGAMATION |
| | @@ -11708,19 +11770,21 @@ |
| 11708 | 11770 | # define sqlite3VtabInSync(db) 0 |
| 11709 | 11771 | # define sqlite3VtabLock(X) |
| 11710 | 11772 | # define sqlite3VtabUnlock(X) |
| 11711 | 11773 | # define sqlite3VtabUnlockList(X) |
| 11712 | 11774 | # define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK |
| 11775 | +# define sqlite3GetVTable(X,Y) ((VTable*)0) |
| 11713 | 11776 | #else |
| 11714 | 11777 | SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*); |
| 11715 | 11778 | SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **); |
| 11716 | 11779 | SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db); |
| 11717 | 11780 | SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db); |
| 11718 | 11781 | SQLITE_PRIVATE void sqlite3VtabLock(VTable *); |
| 11719 | 11782 | SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *); |
| 11720 | 11783 | SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*); |
| 11721 | 11784 | SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int); |
| 11785 | +SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*); |
| 11722 | 11786 | # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0) |
| 11723 | 11787 | #endif |
| 11724 | 11788 | SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*); |
| 11725 | 11789 | SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*); |
| 11726 | 11790 | SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*); |
| | @@ -11736,11 +11800,10 @@ |
| 11736 | 11800 | SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); |
| 11737 | 11801 | SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*); |
| 11738 | 11802 | SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*); |
| 11739 | 11803 | SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *); |
| 11740 | 11804 | SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*); |
| 11741 | | -SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*); |
| 11742 | 11805 | SQLITE_PRIVATE const char *sqlite3JournalModename(int); |
| 11743 | 11806 | SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*); |
| 11744 | 11807 | SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int); |
| 11745 | 11808 | |
| 11746 | 11809 | /* Declarations for functions in fkey.c. All of these are replaced by |
| | @@ -12232,10 +12295,13 @@ |
| 12232 | 12295 | #ifdef SQLITE_ENABLE_RTREE |
| 12233 | 12296 | "ENABLE_RTREE", |
| 12234 | 12297 | #endif |
| 12235 | 12298 | #ifdef SQLITE_ENABLE_STAT2 |
| 12236 | 12299 | "ENABLE_STAT2", |
| 12300 | +#endif |
| 12301 | +#ifdef SQLITE_ENABLE_STAT3 |
| 12302 | + "ENABLE_STAT3", |
| 12237 | 12303 | #endif |
| 12238 | 12304 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 12239 | 12305 | "ENABLE_UNLOCK_NOTIFY", |
| 12240 | 12306 | #endif |
| 12241 | 12307 | #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
| | @@ -12445,13 +12511,10 @@ |
| 12445 | 12511 | "OMIT_WSD", |
| 12446 | 12512 | #endif |
| 12447 | 12513 | #ifdef SQLITE_OMIT_XFER_OPT |
| 12448 | 12514 | "OMIT_XFER_OPT", |
| 12449 | 12515 | #endif |
| 12450 | | -#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 12451 | | - "PAGECACHE_BLOCKALLOC", |
| 12452 | | -#endif |
| 12453 | 12516 | #ifdef SQLITE_PERFORMANCE_TRACE |
| 12454 | 12517 | "PERFORMANCE_TRACE", |
| 12455 | 12518 | #endif |
| 12456 | 12519 | #ifdef SQLITE_PROXY_DEBUG |
| 12457 | 12520 | "PROXY_DEBUG", |
| | @@ -13189,10 +13252,32 @@ |
| 13189 | 13252 | *pHighwater = 0; |
| 13190 | 13253 | *pCurrent = nByte; |
| 13191 | 13254 | |
| 13192 | 13255 | break; |
| 13193 | 13256 | } |
| 13257 | + |
| 13258 | + /* |
| 13259 | + ** Set *pCurrent to the total cache hits or misses encountered by all |
| 13260 | + ** pagers the database handle is connected to. *pHighwater is always set |
| 13261 | + ** to zero. |
| 13262 | + */ |
| 13263 | + case SQLITE_DBSTATUS_CACHE_HIT: |
| 13264 | + case SQLITE_DBSTATUS_CACHE_MISS: { |
| 13265 | + int i; |
| 13266 | + int nRet = 0; |
| 13267 | + assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 ); |
| 13268 | + |
| 13269 | + for(i=0; i<db->nDb; i++){ |
| 13270 | + if( db->aDb[i].pBt ){ |
| 13271 | + Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); |
| 13272 | + sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); |
| 13273 | + } |
| 13274 | + } |
| 13275 | + *pHighwater = 0; |
| 13276 | + *pCurrent = nRet; |
| 13277 | + break; |
| 13278 | + } |
| 13194 | 13279 | |
| 13195 | 13280 | default: { |
| 13196 | 13281 | rc = SQLITE_ERROR; |
| 13197 | 13282 | } |
| 13198 | 13283 | } |
| | @@ -13490,16 +13575,22 @@ |
| 13490 | 13575 | } |
| 13491 | 13576 | return 0; |
| 13492 | 13577 | } |
| 13493 | 13578 | |
| 13494 | 13579 | /* |
| 13495 | | -** Set the time to the current time reported by the VFS |
| 13580 | +** Set the time to the current time reported by the VFS. |
| 13581 | +** |
| 13582 | +** Return the number of errors. |
| 13496 | 13583 | */ |
| 13497 | | -static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ |
| 13584 | +static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ |
| 13498 | 13585 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 13499 | | - sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD); |
| 13500 | | - p->validJD = 1; |
| 13586 | + if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){ |
| 13587 | + p->validJD = 1; |
| 13588 | + return 0; |
| 13589 | + }else{ |
| 13590 | + return 1; |
| 13591 | + } |
| 13501 | 13592 | } |
| 13502 | 13593 | |
| 13503 | 13594 | /* |
| 13504 | 13595 | ** Attempt to parse the given string into a Julian Day Number. Return |
| 13505 | 13596 | ** the number of errors. |
| | @@ -13525,12 +13616,11 @@ |
| 13525 | 13616 | if( parseYyyyMmDd(zDate,p)==0 ){ |
| 13526 | 13617 | return 0; |
| 13527 | 13618 | }else if( parseHhMmSs(zDate, p)==0 ){ |
| 13528 | 13619 | return 0; |
| 13529 | 13620 | }else if( sqlite3StrICmp(zDate,"now")==0){ |
| 13530 | | - setDateTimeToCurrent(context, p); |
| 13531 | | - return 0; |
| 13621 | + return setDateTimeToCurrent(context, p); |
| 13532 | 13622 | }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){ |
| 13533 | 13623 | p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); |
| 13534 | 13624 | p->validJD = 1; |
| 13535 | 13625 | return 0; |
| 13536 | 13626 | } |
| | @@ -13953,12 +14043,13 @@ |
| 13953 | 14043 | int i; |
| 13954 | 14044 | const unsigned char *z; |
| 13955 | 14045 | int eType; |
| 13956 | 14046 | memset(p, 0, sizeof(*p)); |
| 13957 | 14047 | if( argc==0 ){ |
| 13958 | | - setDateTimeToCurrent(context, p); |
| 13959 | | - }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT |
| 14048 | + return setDateTimeToCurrent(context, p); |
| 14049 | + } |
| 14050 | + if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT |
| 13960 | 14051 | || eType==SQLITE_INTEGER ){ |
| 13961 | 14052 | p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5); |
| 13962 | 14053 | p->validJD = 1; |
| 13963 | 14054 | }else{ |
| 13964 | 14055 | z = sqlite3_value_text(argv[0]); |
| | @@ -14266,35 +14357,32 @@ |
| 14266 | 14357 | ){ |
| 14267 | 14358 | time_t t; |
| 14268 | 14359 | char *zFormat = (char *)sqlite3_user_data(context); |
| 14269 | 14360 | sqlite3 *db; |
| 14270 | 14361 | sqlite3_int64 iT; |
| 14362 | + struct tm *pTm; |
| 14363 | + struct tm sNow; |
| 14271 | 14364 | char zBuf[20]; |
| 14272 | 14365 | |
| 14273 | 14366 | UNUSED_PARAMETER(argc); |
| 14274 | 14367 | UNUSED_PARAMETER(argv); |
| 14275 | 14368 | |
| 14276 | 14369 | db = sqlite3_context_db_handle(context); |
| 14277 | | - sqlite3OsCurrentTimeInt64(db->pVfs, &iT); |
| 14370 | + if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return; |
| 14278 | 14371 | t = iT/1000 - 10000*(sqlite3_int64)21086676; |
| 14279 | 14372 | #ifdef HAVE_GMTIME_R |
| 14280 | | - { |
| 14281 | | - struct tm sNow; |
| 14282 | | - gmtime_r(&t, &sNow); |
| 14373 | + pTm = gmtime_r(&t, &sNow); |
| 14374 | +#else |
| 14375 | + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 14376 | + pTm = gmtime(&t); |
| 14377 | + if( pTm ) memcpy(&sNow, pTm, sizeof(sNow)); |
| 14378 | + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 14379 | +#endif |
| 14380 | + if( pTm ){ |
| 14283 | 14381 | strftime(zBuf, 20, zFormat, &sNow); |
| 14284 | | - } |
| 14285 | | -#else |
| 14286 | | - { |
| 14287 | | - struct tm *pTm; |
| 14288 | | - sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 14289 | | - pTm = gmtime(&t); |
| 14290 | | - strftime(zBuf, 20, zFormat, pTm); |
| 14291 | | - sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 14292 | | - } |
| 14293 | | -#endif |
| 14294 | | - |
| 14295 | | - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 14382 | + sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 14383 | + } |
| 14296 | 14384 | } |
| 14297 | 14385 | #endif |
| 14298 | 14386 | |
| 14299 | 14387 | /* |
| 14300 | 14388 | ** This function registered all of the above C functions as SQL |
| | @@ -14625,16 +14713,16 @@ |
| 14625 | 14713 | ** Register a VFS with the system. It is harmless to register the same |
| 14626 | 14714 | ** VFS multiple times. The new VFS becomes the default if makeDflt is |
| 14627 | 14715 | ** true. |
| 14628 | 14716 | */ |
| 14629 | 14717 | SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ |
| 14630 | | - sqlite3_mutex *mutex = 0; |
| 14718 | + MUTEX_LOGIC(sqlite3_mutex *mutex;) |
| 14631 | 14719 | #ifndef SQLITE_OMIT_AUTOINIT |
| 14632 | 14720 | int rc = sqlite3_initialize(); |
| 14633 | 14721 | if( rc ) return rc; |
| 14634 | 14722 | #endif |
| 14635 | | - mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 14723 | + MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 14636 | 14724 | sqlite3_mutex_enter(mutex); |
| 14637 | 14725 | vfsUnlink(pVfs); |
| 14638 | 14726 | if( makeDflt || vfsList==0 ){ |
| 14639 | 14727 | pVfs->pNext = vfsList; |
| 14640 | 14728 | vfsList = pVfs; |
| | @@ -18878,52 +18966,14 @@ |
| 18878 | 18966 | ** an historical reference. Most of the "enhancements" have been backed |
| 18879 | 18967 | ** out so that the functionality is now the same as standard printf(). |
| 18880 | 18968 | ** |
| 18881 | 18969 | ************************************************************************** |
| 18882 | 18970 | ** |
| 18883 | | -** The following modules is an enhanced replacement for the "printf" subroutines |
| 18884 | | -** found in the standard C library. The following enhancements are |
| 18885 | | -** supported: |
| 18886 | | -** |
| 18887 | | -** + Additional functions. The standard set of "printf" functions |
| 18888 | | -** includes printf, fprintf, sprintf, vprintf, vfprintf, and |
| 18889 | | -** vsprintf. This module adds the following: |
| 18890 | | -** |
| 18891 | | -** * snprintf -- Works like sprintf, but has an extra argument |
| 18892 | | -** which is the size of the buffer written to. |
| 18893 | | -** |
| 18894 | | -** * mprintf -- Similar to sprintf. Writes output to memory |
| 18895 | | -** obtained from malloc. |
| 18896 | | -** |
| 18897 | | -** * xprintf -- Calls a function to dispose of output. |
| 18898 | | -** |
| 18899 | | -** * nprintf -- No output, but returns the number of characters |
| 18900 | | -** that would have been output by printf. |
| 18901 | | -** |
| 18902 | | -** * A v- version (ex: vsnprintf) of every function is also |
| 18903 | | -** supplied. |
| 18904 | | -** |
| 18905 | | -** + A few extensions to the formatting notation are supported: |
| 18906 | | -** |
| 18907 | | -** * The "=" flag (similar to "-") causes the output to be |
| 18908 | | -** be centered in the appropriately sized field. |
| 18909 | | -** |
| 18910 | | -** * The %b field outputs an integer in binary notation. |
| 18911 | | -** |
| 18912 | | -** * The %c field now accepts a precision. The character output |
| 18913 | | -** is repeated by the number of times the precision specifies. |
| 18914 | | -** |
| 18915 | | -** * The %' field works like %c, but takes as its character the |
| 18916 | | -** next character of the format string, instead of the next |
| 18917 | | -** argument. For example, printf("%.78'-") prints 78 minus |
| 18918 | | -** signs, the same as printf("%.78c",'-'). |
| 18919 | | -** |
| 18920 | | -** + When compiled using GCC on a SPARC, this version of printf is |
| 18921 | | -** faster than the library printf for SUN OS 4.1. |
| 18922 | | -** |
| 18923 | | -** + All functions are fully reentrant. |
| 18924 | | -** |
| 18971 | +** This file contains code for a set of "printf"-like routines. These |
| 18972 | +** routines format strings much like the printf() from the standard C |
| 18973 | +** library, though the implementation here has enhancements to support |
| 18974 | +** SQLlite. |
| 18925 | 18975 | */ |
| 18926 | 18976 | |
| 18927 | 18977 | /* |
| 18928 | 18978 | ** Conversion types fall into various categories as defined by the |
| 18929 | 18979 | ** following enumeration. |
| | @@ -19057,47 +19107,19 @@ |
| 19057 | 19107 | } |
| 19058 | 19108 | } |
| 19059 | 19109 | |
| 19060 | 19110 | /* |
| 19061 | 19111 | ** On machines with a small stack size, you can redefine the |
| 19062 | | -** SQLITE_PRINT_BUF_SIZE to be less than 350. |
| 19112 | +** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. |
| 19063 | 19113 | */ |
| 19064 | 19114 | #ifndef SQLITE_PRINT_BUF_SIZE |
| 19065 | | -# if defined(SQLITE_SMALL_STACK) |
| 19066 | | -# define SQLITE_PRINT_BUF_SIZE 50 |
| 19067 | | -# else |
| 19068 | | -# define SQLITE_PRINT_BUF_SIZE 350 |
| 19069 | | -# endif |
| 19115 | +# define SQLITE_PRINT_BUF_SIZE 70 |
| 19070 | 19116 | #endif |
| 19071 | 19117 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
| 19072 | 19118 | |
| 19073 | 19119 | /* |
| 19074 | | -** The root program. All variations call this core. |
| 19075 | | -** |
| 19076 | | -** INPUTS: |
| 19077 | | -** func This is a pointer to a function taking three arguments |
| 19078 | | -** 1. A pointer to anything. Same as the "arg" parameter. |
| 19079 | | -** 2. A pointer to the list of characters to be output |
| 19080 | | -** (Note, this list is NOT null terminated.) |
| 19081 | | -** 3. An integer number of characters to be output. |
| 19082 | | -** (Note: This number might be zero.) |
| 19083 | | -** |
| 19084 | | -** arg This is the pointer to anything which will be passed as the |
| 19085 | | -** first argument to "func". Use it for whatever you like. |
| 19086 | | -** |
| 19087 | | -** fmt This is the format string, as in the usual print. |
| 19088 | | -** |
| 19089 | | -** ap This is a pointer to a list of arguments. Same as in |
| 19090 | | -** vfprint. |
| 19091 | | -** |
| 19092 | | -** OUTPUTS: |
| 19093 | | -** The return value is the total number of characters sent to |
| 19094 | | -** the function "func". Returns -1 on a error. |
| 19095 | | -** |
| 19096 | | -** Note that the order in which automatic variables are declared below |
| 19097 | | -** seems to make a big difference in determining how fast this beast |
| 19098 | | -** will run. |
| 19120 | +** Render a string given by "fmt" into the StrAccum object. |
| 19099 | 19121 | */ |
| 19100 | 19122 | SQLITE_PRIVATE void sqlite3VXPrintf( |
| 19101 | 19123 | StrAccum *pAccum, /* Accumulate results here */ |
| 19102 | 19124 | int useExtended, /* Allow extended %-conversions */ |
| 19103 | 19125 | const char *fmt, /* Format string */ |
| | @@ -19116,27 +19138,27 @@ |
| 19116 | 19138 | etByte flag_altform2; /* True if "!" flag is present */ |
| 19117 | 19139 | etByte flag_zeropad; /* True if field width constant starts with zero */ |
| 19118 | 19140 | etByte flag_long; /* True if "l" flag is present */ |
| 19119 | 19141 | etByte flag_longlong; /* True if the "ll" flag is present */ |
| 19120 | 19142 | etByte done; /* Loop termination flag */ |
| 19143 | + etByte xtype = 0; /* Conversion paradigm */ |
| 19144 | + char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 19121 | 19145 | sqlite_uint64 longvalue; /* Value for integer types */ |
| 19122 | 19146 | LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
| 19123 | 19147 | const et_info *infop; /* Pointer to the appropriate info structure */ |
| 19124 | | - char buf[etBUFSIZE]; /* Conversion buffer */ |
| 19125 | | - char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 19126 | | - etByte xtype = 0; /* Conversion paradigm */ |
| 19127 | | - char *zExtra; /* Extra memory used for etTCLESCAPE conversions */ |
| 19148 | + char *zOut; /* Rendering buffer */ |
| 19149 | + int nOut; /* Size of the rendering buffer */ |
| 19150 | + char *zExtra; /* Malloced memory used by some conversion */ |
| 19128 | 19151 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 19129 | 19152 | int exp, e2; /* exponent of real numbers */ |
| 19153 | + int nsd; /* Number of significant digits returned */ |
| 19130 | 19154 | double rounder; /* Used for rounding floating point values */ |
| 19131 | 19155 | etByte flag_dp; /* True if decimal point should be shown */ |
| 19132 | 19156 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 19133 | | - etByte flag_exp; /* True to force display of the exponent */ |
| 19134 | | - int nsd; /* Number of significant digits returned */ |
| 19135 | 19157 | #endif |
| 19158 | + char buf[etBUFSIZE]; /* Conversion buffer */ |
| 19136 | 19159 | |
| 19137 | | - length = 0; |
| 19138 | 19160 | bufpt = 0; |
| 19139 | 19161 | for(; (c=(*fmt))!=0; ++fmt){ |
| 19140 | 19162 | if( c!='%' ){ |
| 19141 | 19163 | int amt; |
| 19142 | 19164 | bufpt = (char *)fmt; |
| | @@ -19177,13 +19199,10 @@ |
| 19177 | 19199 | while( c>='0' && c<='9' ){ |
| 19178 | 19200 | width = width*10 + c - '0'; |
| 19179 | 19201 | c = *++fmt; |
| 19180 | 19202 | } |
| 19181 | 19203 | } |
| 19182 | | - if( width > etBUFSIZE-10 ){ |
| 19183 | | - width = etBUFSIZE-10; |
| 19184 | | - } |
| 19185 | 19204 | /* Get the precision */ |
| 19186 | 19205 | if( c=='.' ){ |
| 19187 | 19206 | precision = 0; |
| 19188 | 19207 | c = *++fmt; |
| 19189 | 19208 | if( c=='*' ){ |
| | @@ -19226,16 +19245,10 @@ |
| 19226 | 19245 | break; |
| 19227 | 19246 | } |
| 19228 | 19247 | } |
| 19229 | 19248 | zExtra = 0; |
| 19230 | 19249 | |
| 19231 | | - |
| 19232 | | - /* Limit the precision to prevent overflowing buf[] during conversion */ |
| 19233 | | - if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){ |
| 19234 | | - precision = etBUFSIZE-40; |
| 19235 | | - } |
| 19236 | | - |
| 19237 | 19250 | /* |
| 19238 | 19251 | ** At this point, variables are initialized as follows: |
| 19239 | 19252 | ** |
| 19240 | 19253 | ** flag_alternateform TRUE if a '#' is present. |
| 19241 | 19254 | ** flag_altform2 TRUE if a '!' is present. |
| | @@ -19296,20 +19309,30 @@ |
| 19296 | 19309 | } |
| 19297 | 19310 | if( longvalue==0 ) flag_alternateform = 0; |
| 19298 | 19311 | if( flag_zeropad && precision<width-(prefix!=0) ){ |
| 19299 | 19312 | precision = width-(prefix!=0); |
| 19300 | 19313 | } |
| 19301 | | - bufpt = &buf[etBUFSIZE-1]; |
| 19314 | + if( precision<etBUFSIZE-10 ){ |
| 19315 | + nOut = etBUFSIZE; |
| 19316 | + zOut = buf; |
| 19317 | + }else{ |
| 19318 | + nOut = precision + 10; |
| 19319 | + zOut = zExtra = sqlite3Malloc( nOut ); |
| 19320 | + if( zOut==0 ){ |
| 19321 | + pAccum->mallocFailed = 1; |
| 19322 | + return; |
| 19323 | + } |
| 19324 | + } |
| 19325 | + bufpt = &zOut[nOut-1]; |
| 19302 | 19326 | if( xtype==etORDINAL ){ |
| 19303 | 19327 | static const char zOrd[] = "thstndrd"; |
| 19304 | 19328 | int x = (int)(longvalue % 10); |
| 19305 | 19329 | if( x>=4 || (longvalue/10)%10==1 ){ |
| 19306 | 19330 | x = 0; |
| 19307 | 19331 | } |
| 19308 | | - buf[etBUFSIZE-3] = zOrd[x*2]; |
| 19309 | | - buf[etBUFSIZE-2] = zOrd[x*2+1]; |
| 19310 | | - bufpt -= 2; |
| 19332 | + *(--bufpt) = zOrd[x*2+1]; |
| 19333 | + *(--bufpt) = zOrd[x*2]; |
| 19311 | 19334 | } |
| 19312 | 19335 | { |
| 19313 | 19336 | register const char *cset; /* Use registers for speed */ |
| 19314 | 19337 | register int base; |
| 19315 | 19338 | cset = &aDigits[infop->charset]; |
| | @@ -19317,11 +19340,11 @@ |
| 19317 | 19340 | do{ /* Convert to ascii */ |
| 19318 | 19341 | *(--bufpt) = cset[longvalue%base]; |
| 19319 | 19342 | longvalue = longvalue/base; |
| 19320 | 19343 | }while( longvalue>0 ); |
| 19321 | 19344 | } |
| 19322 | | - length = (int)(&buf[etBUFSIZE-1]-bufpt); |
| 19345 | + length = (int)(&zOut[nOut-1]-bufpt); |
| 19323 | 19346 | for(idx=precision-length; idx>0; idx--){ |
| 19324 | 19347 | *(--bufpt) = '0'; /* Zero pad */ |
| 19325 | 19348 | } |
| 19326 | 19349 | if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
| 19327 | 19350 | if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
| | @@ -19328,21 +19351,20 @@ |
| 19328 | 19351 | const char *pre; |
| 19329 | 19352 | char x; |
| 19330 | 19353 | pre = &aPrefix[infop->prefix]; |
| 19331 | 19354 | for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
| 19332 | 19355 | } |
| 19333 | | - length = (int)(&buf[etBUFSIZE-1]-bufpt); |
| 19356 | + length = (int)(&zOut[nOut-1]-bufpt); |
| 19334 | 19357 | break; |
| 19335 | 19358 | case etFLOAT: |
| 19336 | 19359 | case etEXP: |
| 19337 | 19360 | case etGENERIC: |
| 19338 | 19361 | realvalue = va_arg(ap,double); |
| 19339 | 19362 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 19340 | 19363 | length = 0; |
| 19341 | 19364 | #else |
| 19342 | 19365 | if( precision<0 ) precision = 6; /* Set default precision */ |
| 19343 | | - if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10; |
| 19344 | 19366 | if( realvalue<0.0 ){ |
| 19345 | 19367 | realvalue = -realvalue; |
| 19346 | 19368 | prefix = '-'; |
| 19347 | 19369 | }else{ |
| 19348 | 19370 | if( flag_plussign ) prefix = '+'; |
| | @@ -19386,11 +19408,10 @@ |
| 19386 | 19408 | bufpt = buf; |
| 19387 | 19409 | /* |
| 19388 | 19410 | ** If the field type is etGENERIC, then convert to either etEXP |
| 19389 | 19411 | ** or etFLOAT, as appropriate. |
| 19390 | 19412 | */ |
| 19391 | | - flag_exp = xtype==etEXP; |
| 19392 | 19413 | if( xtype!=etFLOAT ){ |
| 19393 | 19414 | realvalue += rounder; |
| 19394 | 19415 | if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 19395 | 19416 | } |
| 19396 | 19417 | if( xtype==etGENERIC ){ |
| | @@ -19407,10 +19428,18 @@ |
| 19407 | 19428 | if( xtype==etEXP ){ |
| 19408 | 19429 | e2 = 0; |
| 19409 | 19430 | }else{ |
| 19410 | 19431 | e2 = exp; |
| 19411 | 19432 | } |
| 19433 | + if( e2+precision+width > etBUFSIZE - 15 ){ |
| 19434 | + bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 ); |
| 19435 | + if( bufpt==0 ){ |
| 19436 | + pAccum->mallocFailed = 1; |
| 19437 | + return; |
| 19438 | + } |
| 19439 | + } |
| 19440 | + zOut = bufpt; |
| 19412 | 19441 | nsd = 0; |
| 19413 | 19442 | flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; |
| 19414 | 19443 | /* The sign in front of the number */ |
| 19415 | 19444 | if( prefix ){ |
| 19416 | 19445 | *(bufpt++) = prefix; |
| | @@ -19438,21 +19467,21 @@ |
| 19438 | 19467 | *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 19439 | 19468 | } |
| 19440 | 19469 | /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 19441 | 19470 | if( flag_rtz && flag_dp ){ |
| 19442 | 19471 | while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
| 19443 | | - assert( bufpt>buf ); |
| 19472 | + assert( bufpt>zOut ); |
| 19444 | 19473 | if( bufpt[-1]=='.' ){ |
| 19445 | 19474 | if( flag_altform2 ){ |
| 19446 | 19475 | *(bufpt++) = '0'; |
| 19447 | 19476 | }else{ |
| 19448 | 19477 | *(--bufpt) = 0; |
| 19449 | 19478 | } |
| 19450 | 19479 | } |
| 19451 | 19480 | } |
| 19452 | 19481 | /* Add the "eNNN" suffix */ |
| 19453 | | - if( flag_exp || xtype==etEXP ){ |
| 19482 | + if( xtype==etEXP ){ |
| 19454 | 19483 | *(bufpt++) = aDigits[infop->charset]; |
| 19455 | 19484 | if( exp<0 ){ |
| 19456 | 19485 | *(bufpt++) = '-'; exp = -exp; |
| 19457 | 19486 | }else{ |
| 19458 | 19487 | *(bufpt++) = '+'; |
| | @@ -19467,12 +19496,12 @@ |
| 19467 | 19496 | *bufpt = 0; |
| 19468 | 19497 | |
| 19469 | 19498 | /* The converted number is in buf[] and zero terminated. Output it. |
| 19470 | 19499 | ** Note that the number is in the usual order, not reversed as with |
| 19471 | 19500 | ** integer conversions. */ |
| 19472 | | - length = (int)(bufpt-buf); |
| 19473 | | - bufpt = buf; |
| 19501 | + length = (int)(bufpt-zOut); |
| 19502 | + bufpt = zOut; |
| 19474 | 19503 | |
| 19475 | 19504 | /* Special case: Add leading zeros if the flag_zeropad flag is |
| 19476 | 19505 | ** set and we are not left justified */ |
| 19477 | 19506 | if( flag_zeropad && !flag_leftjustify && length < width){ |
| 19478 | 19507 | int i; |
| | @@ -19606,13 +19635,11 @@ |
| 19606 | 19635 | nspace = width-length; |
| 19607 | 19636 | if( nspace>0 ){ |
| 19608 | 19637 | appendSpace(pAccum, nspace); |
| 19609 | 19638 | } |
| 19610 | 19639 | } |
| 19611 | | - if( zExtra ){ |
| 19612 | | - sqlite3_free(zExtra); |
| 19613 | | - } |
| 19640 | + sqlite3_free(zExtra); |
| 19614 | 19641 | }/* End for loop over the format string */ |
| 19615 | 19642 | } /* End of function */ |
| 19616 | 19643 | |
| 19617 | 19644 | /* |
| 19618 | 19645 | ** Append N bytes of text from z to the StrAccum object. |
| | @@ -19622,10 +19649,11 @@ |
| 19622 | 19649 | if( p->tooBig | p->mallocFailed ){ |
| 19623 | 19650 | testcase(p->tooBig); |
| 19624 | 19651 | testcase(p->mallocFailed); |
| 19625 | 19652 | return; |
| 19626 | 19653 | } |
| 19654 | + assert( p->zText!=0 || p->nChar==0 ); |
| 19627 | 19655 | if( N<0 ){ |
| 19628 | 19656 | N = sqlite3Strlen30(z); |
| 19629 | 19657 | } |
| 19630 | 19658 | if( N==0 || NEVER(z==0) ){ |
| 19631 | 19659 | return; |
| | @@ -19653,19 +19681,20 @@ |
| 19653 | 19681 | zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 19654 | 19682 | }else{ |
| 19655 | 19683 | zNew = sqlite3_realloc(zOld, p->nAlloc); |
| 19656 | 19684 | } |
| 19657 | 19685 | if( zNew ){ |
| 19658 | | - if( zOld==0 ) memcpy(zNew, p->zText, p->nChar); |
| 19686 | + if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 19659 | 19687 | p->zText = zNew; |
| 19660 | 19688 | }else{ |
| 19661 | 19689 | p->mallocFailed = 1; |
| 19662 | 19690 | sqlite3StrAccumReset(p); |
| 19663 | 19691 | return; |
| 19664 | 19692 | } |
| 19665 | 19693 | } |
| 19666 | 19694 | } |
| 19695 | + assert( p->zText ); |
| 19667 | 19696 | memcpy(&p->zText[p->nChar], z, N); |
| 19668 | 19697 | p->nChar += N; |
| 19669 | 19698 | } |
| 19670 | 19699 | |
| 19671 | 19700 | /* |
| | @@ -20511,11 +20540,11 @@ |
| 20511 | 20540 | ** no longer required. |
| 20512 | 20541 | ** |
| 20513 | 20542 | ** If a malloc failure occurs, NULL is returned and the db.mallocFailed |
| 20514 | 20543 | ** flag set. |
| 20515 | 20544 | */ |
| 20516 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 20545 | +#ifdef SQLITE_ENABLE_STAT3 |
| 20517 | 20546 | SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){ |
| 20518 | 20547 | Mem m; |
| 20519 | 20548 | memset(&m, 0, sizeof(m)); |
| 20520 | 20549 | m.db = db; |
| 20521 | 20550 | sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC); |
| | @@ -20940,11 +20969,11 @@ |
| 20940 | 20969 | }else if( *z=='+' ){ |
| 20941 | 20970 | z+=incr; |
| 20942 | 20971 | } |
| 20943 | 20972 | /* copy digits to exponent */ |
| 20944 | 20973 | while( z<zEnd && sqlite3Isdigit(*z) ){ |
| 20945 | | - e = e*10 + (*z - '0'); |
| 20974 | + e = e<10000 ? (e*10 + (*z - '0')) : 10000; |
| 20946 | 20975 | z+=incr; |
| 20947 | 20976 | eValid = 1; |
| 20948 | 20977 | } |
| 20949 | 20978 | } |
| 20950 | 20979 | |
| | @@ -20991,10 +21020,16 @@ |
| 20991 | 21020 | result /= 1.0e+308; |
| 20992 | 21021 | }else{ |
| 20993 | 21022 | result = s * scale; |
| 20994 | 21023 | result *= 1.0e+308; |
| 20995 | 21024 | } |
| 21025 | + }else if( e>=342 ){ |
| 21026 | + if( esign<0 ){ |
| 21027 | + result = 0.0*s; |
| 21028 | + }else{ |
| 21029 | + result = 1e308*1e308*s; /* Infinity */ |
| 21030 | + } |
| 20996 | 21031 | }else{ |
| 20997 | 21032 | /* 1.0e+22 is the largest power of 10 than can be |
| 20998 | 21033 | ** represented exactly. */ |
| 20999 | 21034 | while( e%22 ) { scale *= 1.0e+1; e -= 1; } |
| 21000 | 21035 | while( e>0 ) { scale *= 1.0e+22; e -= 22; } |
| | @@ -25102,11 +25137,11 @@ |
| 25102 | 25137 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 25103 | 25138 | } |
| 25104 | 25139 | #endif |
| 25105 | 25140 | |
| 25106 | 25141 | |
| 25107 | | -#ifdef SQLITE_DEBUG |
| 25142 | +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 25108 | 25143 | /* |
| 25109 | 25144 | ** Helper function for printing out trace information from debugging |
| 25110 | 25145 | ** binaries. This returns the string represetation of the supplied |
| 25111 | 25146 | ** integer lock-type. |
| 25112 | 25147 | */ |
| | @@ -25937,18 +25972,18 @@ |
| 25937 | 25972 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 25938 | 25973 | ** even if the locking primitive used is always a write-lock. |
| 25939 | 25974 | */ |
| 25940 | 25975 | int rc = SQLITE_OK; |
| 25941 | 25976 | unixFile *pFile = (unixFile*)id; |
| 25942 | | - unixInodeInfo *pInode = pFile->pInode; |
| 25977 | + unixInodeInfo *pInode; |
| 25943 | 25978 | struct flock lock; |
| 25944 | 25979 | int tErrno = 0; |
| 25945 | 25980 | |
| 25946 | 25981 | assert( pFile ); |
| 25947 | 25982 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 25948 | 25983 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
| 25949 | | - azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
| 25984 | + azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid())); |
| 25950 | 25985 | |
| 25951 | 25986 | /* If there is already a lock of this type or more restrictive on the |
| 25952 | 25987 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
| 25953 | 25988 | ** unixEnterMutex() hasn't been called yet. |
| 25954 | 25989 | */ |
| | @@ -26148,11 +26183,10 @@ |
| 26148 | 26183 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
| 26149 | 26184 | unixFile *pFile = (unixFile*)id; |
| 26150 | 26185 | unixInodeInfo *pInode; |
| 26151 | 26186 | struct flock lock; |
| 26152 | 26187 | int rc = SQLITE_OK; |
| 26153 | | - int h; |
| 26154 | 26188 | |
| 26155 | 26189 | assert( pFile ); |
| 26156 | 26190 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
| 26157 | 26191 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
| 26158 | 26192 | getpid())); |
| | @@ -26160,18 +26194,14 @@ |
| 26160 | 26194 | assert( eFileLock<=SHARED_LOCK ); |
| 26161 | 26195 | if( pFile->eFileLock<=eFileLock ){ |
| 26162 | 26196 | return SQLITE_OK; |
| 26163 | 26197 | } |
| 26164 | 26198 | unixEnterMutex(); |
| 26165 | | - h = pFile->h; |
| 26166 | 26199 | pInode = pFile->pInode; |
| 26167 | 26200 | assert( pInode->nShared!=0 ); |
| 26168 | 26201 | if( pFile->eFileLock>SHARED_LOCK ){ |
| 26169 | 26202 | assert( pInode->eFileLock==pFile->eFileLock ); |
| 26170 | | - SimulateIOErrorBenign(1); |
| 26171 | | - SimulateIOError( h=(-1) ) |
| 26172 | | - SimulateIOErrorBenign(0); |
| 26173 | 26203 | |
| 26174 | 26204 | #ifndef NDEBUG |
| 26175 | 26205 | /* When reducing a lock such that other processes can start |
| 26176 | 26206 | ** reading the database file again, make sure that the |
| 26177 | 26207 | ** transaction counter was updated if any part of the database |
| | @@ -26178,15 +26208,10 @@ |
| 26178 | 26208 | ** file changed. If the transaction counter is not updated, |
| 26179 | 26209 | ** other connections to the same file might not realize that |
| 26180 | 26210 | ** the file has changed and hence might not know to flush their |
| 26181 | 26211 | ** cache. The use of a stale cache can lead to database corruption. |
| 26182 | 26212 | */ |
| 26183 | | -#if 0 |
| 26184 | | - assert( pFile->inNormalWrite==0 |
| 26185 | | - || pFile->dbUpdate==0 |
| 26186 | | - || pFile->transCntrChng==1 ); |
| 26187 | | -#endif |
| 26188 | 26213 | pFile->inNormalWrite = 0; |
| 26189 | 26214 | #endif |
| 26190 | 26215 | |
| 26191 | 26216 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 26192 | 26217 | ** before establishing the readlock - to avoid a race condition we downgrade |
| | @@ -26284,13 +26309,10 @@ |
| 26284 | 26309 | pInode->nShared--; |
| 26285 | 26310 | if( pInode->nShared==0 ){ |
| 26286 | 26311 | lock.l_type = F_UNLCK; |
| 26287 | 26312 | lock.l_whence = SEEK_SET; |
| 26288 | 26313 | lock.l_start = lock.l_len = 0L; |
| 26289 | | - SimulateIOErrorBenign(1); |
| 26290 | | - SimulateIOError( h=(-1) ) |
| 26291 | | - SimulateIOErrorBenign(0); |
| 26292 | 26314 | if( unixFileLock(pFile, &lock)==0 ){ |
| 26293 | 26315 | pInode->eFileLock = NO_LOCK; |
| 26294 | 26316 | }else{ |
| 26295 | 26317 | rc = SQLITE_IOERR_UNLOCK; |
| 26296 | 26318 | pFile->lastErrno = errno; |
| | @@ -28428,20 +28450,19 @@ |
| 28428 | 28450 | rc = SQLITE_NOMEM; |
| 28429 | 28451 | goto shm_open_err; |
| 28430 | 28452 | } |
| 28431 | 28453 | |
| 28432 | 28454 | if( pInode->bProcessLock==0 ){ |
| 28433 | | - pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT, |
| 28434 | | - (sStat.st_mode & 0777)); |
| 28455 | + const char *zRO; |
| 28456 | + int openFlags = O_RDWR | O_CREAT; |
| 28457 | + zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm"); |
| 28458 | + if( zRO && sqlite3GetBoolean(zRO) ){ |
| 28459 | + openFlags = O_RDONLY; |
| 28460 | + pShmNode->isReadonly = 1; |
| 28461 | + } |
| 28462 | + pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
| 28435 | 28463 | if( pShmNode->h<0 ){ |
| 28436 | | - const char *zRO; |
| 28437 | | - zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm"); |
| 28438 | | - if( zRO && sqlite3GetBoolean(zRO) ){ |
| 28439 | | - pShmNode->h = robust_open(zShmFilename, O_RDONLY, |
| 28440 | | - (sStat.st_mode & 0777)); |
| 28441 | | - pShmNode->isReadonly = 1; |
| 28442 | | - } |
| 28443 | 28464 | if( pShmNode->h<0 ){ |
| 28444 | 28465 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 28445 | 28466 | goto shm_open_err; |
| 28446 | 28467 | } |
| 28447 | 28468 | } |
| | @@ -29122,10 +29143,13 @@ |
| 29122 | 29143 | assert( zFilename==0 || zFilename[0]=='/' |
| 29123 | 29144 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 29124 | 29145 | #else |
| 29125 | 29146 | assert( zFilename==0 || zFilename[0]=='/' ); |
| 29126 | 29147 | #endif |
| 29148 | + |
| 29149 | + /* No locking occurs in temporary files */ |
| 29150 | + assert( zFilename!=0 || noLock ); |
| 29127 | 29151 | |
| 29128 | 29152 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
| 29129 | 29153 | pNew->h = h; |
| 29130 | 29154 | pNew->zPath = zFilename; |
| 29131 | 29155 | if( memcmp(pVfs->zName,"unix-excl",10)==0 ){ |
| | @@ -29224,10 +29248,11 @@ |
| 29224 | 29248 | /* Dotfile locking uses the file path so it needs to be included in |
| 29225 | 29249 | ** the dotlockLockingContext |
| 29226 | 29250 | */ |
| 29227 | 29251 | char *zLockFile; |
| 29228 | 29252 | int nFilename; |
| 29253 | + assert( zFilename!=0 ); |
| 29229 | 29254 | nFilename = (int)strlen(zFilename) + 6; |
| 29230 | 29255 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 29231 | 29256 | if( zLockFile==0 ){ |
| 29232 | 29257 | rc = SQLITE_NOMEM; |
| 29233 | 29258 | }else{ |
| | @@ -29462,12 +29487,20 @@ |
| 29462 | 29487 | ** |
| 29463 | 29488 | ** where NN is a 4 digit decimal number. The NN naming schemes are |
| 29464 | 29489 | ** used by the test_multiplex.c module. |
| 29465 | 29490 | */ |
| 29466 | 29491 | nDb = sqlite3Strlen30(zPath) - 1; |
| 29467 | | - while( nDb>0 && zPath[nDb]!='-' ) nDb--; |
| 29468 | | - if( nDb==0 ) return SQLITE_OK; |
| 29492 | +#ifdef SQLITE_ENABLE_8_3_NAMES |
| 29493 | + while( nDb>0 && zPath[nDb]!='-' && zPath[nDb]!='/' ) nDb--; |
| 29494 | + if( nDb==0 || zPath[nDb]=='/' ) return SQLITE_OK; |
| 29495 | +#else |
| 29496 | + while( zPath[nDb]!='-' ){ |
| 29497 | + assert( nDb>0 ); |
| 29498 | + assert( zPath[nDb]!='\n' ); |
| 29499 | + nDb--; |
| 29500 | + } |
| 29501 | +#endif |
| 29469 | 29502 | memcpy(zDb, zPath, nDb); |
| 29470 | 29503 | zDb[nDb] = '\0'; |
| 29471 | 29504 | |
| 29472 | 29505 | if( 0==osStat(zDb, &sStat) ){ |
| 29473 | 29506 | *pMode = sStat.st_mode & 0777; |
| | @@ -29995,14 +30028,16 @@ |
| 29995 | 30028 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 29996 | 30029 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 29997 | 30030 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 29998 | 30031 | ** proleptic Gregorian calendar. |
| 29999 | 30032 | ** |
| 30000 | | -** On success, return 0. Return 1 if the time and date cannot be found. |
| 30033 | +** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 30034 | +** cannot be found. |
| 30001 | 30035 | */ |
| 30002 | 30036 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 30003 | 30037 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 30038 | + int rc = SQLITE_OK; |
| 30004 | 30039 | #if defined(NO_GETTOD) |
| 30005 | 30040 | time_t t; |
| 30006 | 30041 | time(&t); |
| 30007 | 30042 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
| 30008 | 30043 | #elif OS_VXWORKS |
| | @@ -30009,34 +30044,38 @@ |
| 30009 | 30044 | struct timespec sNow; |
| 30010 | 30045 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 30011 | 30046 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 30012 | 30047 | #else |
| 30013 | 30048 | struct timeval sNow; |
| 30014 | | - gettimeofday(&sNow, 0); |
| 30015 | | - *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 30049 | + if( gettimeofday(&sNow, 0)==0 ){ |
| 30050 | + *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 30051 | + }else{ |
| 30052 | + rc = SQLITE_ERROR; |
| 30053 | + } |
| 30016 | 30054 | #endif |
| 30017 | 30055 | |
| 30018 | 30056 | #ifdef SQLITE_TEST |
| 30019 | 30057 | if( sqlite3_current_time ){ |
| 30020 | 30058 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 30021 | 30059 | } |
| 30022 | 30060 | #endif |
| 30023 | 30061 | UNUSED_PARAMETER(NotUsed); |
| 30024 | | - return 0; |
| 30062 | + return rc; |
| 30025 | 30063 | } |
| 30026 | 30064 | |
| 30027 | 30065 | /* |
| 30028 | 30066 | ** Find the current time (in Universal Coordinated Time). Write the |
| 30029 | 30067 | ** current time and date as a Julian Day number into *prNow and |
| 30030 | 30068 | ** return 0. Return 1 if the time and date cannot be found. |
| 30031 | 30069 | */ |
| 30032 | 30070 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
| 30033 | | - sqlite3_int64 i; |
| 30071 | + sqlite3_int64 i = 0; |
| 30072 | + int rc; |
| 30034 | 30073 | UNUSED_PARAMETER(NotUsed); |
| 30035 | | - unixCurrentTimeInt64(0, &i); |
| 30074 | + rc = unixCurrentTimeInt64(0, &i); |
| 30036 | 30075 | *prNow = i/86400000.0; |
| 30037 | | - return 0; |
| 30076 | + return rc; |
| 30038 | 30077 | } |
| 30039 | 30078 | |
| 30040 | 30079 | /* |
| 30041 | 30080 | ** We added the xGetLastError() method with the intention of providing |
| 30042 | 30081 | ** better low-level error messages when operating-system problems come up |
| | @@ -34169,11 +34208,11 @@ |
| 34169 | 34208 | |
| 34170 | 34209 | if( h==INVALID_HANDLE_VALUE ){ |
| 34171 | 34210 | pFile->lastErrno = GetLastError(); |
| 34172 | 34211 | winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name); |
| 34173 | 34212 | free(zConverted); |
| 34174 | | - if( isReadWrite ){ |
| 34213 | + if( isReadWrite && !isExclusive ){ |
| 34175 | 34214 | return winOpen(pVfs, zName, id, |
| 34176 | 34215 | ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags); |
| 34177 | 34216 | }else{ |
| 34178 | 34217 | return SQLITE_CANTOPEN_BKPT; |
| 34179 | 34218 | } |
| | @@ -34535,11 +34574,11 @@ |
| 34535 | 34574 | } |
| 34536 | 34575 | static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ |
| 34537 | 34576 | UNUSED_PARAMETER(pVfs); |
| 34538 | 34577 | getLastErrorMsg(nBuf, zBufOut); |
| 34539 | 34578 | } |
| 34540 | | -void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){ |
| 34579 | +static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){ |
| 34541 | 34580 | UNUSED_PARAMETER(pVfs); |
| 34542 | 34581 | #if SQLITE_OS_WINCE |
| 34543 | 34582 | /* The GetProcAddressA() routine is only available on wince. */ |
| 34544 | 34583 | return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol); |
| 34545 | 34584 | #else |
| | @@ -34546,11 +34585,11 @@ |
| 34546 | 34585 | /* All other windows platforms expect GetProcAddress() to take |
| 34547 | 34586 | ** an Ansi string regardless of the _UNICODE setting */ |
| 34548 | 34587 | return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol); |
| 34549 | 34588 | #endif |
| 34550 | 34589 | } |
| 34551 | | -void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 34590 | +static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 34552 | 34591 | UNUSED_PARAMETER(pVfs); |
| 34553 | 34592 | FreeLibrary((HANDLE)pHandle); |
| 34554 | 34593 | } |
| 34555 | 34594 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 34556 | 34595 | #define winDlOpen 0 |
| | @@ -34620,11 +34659,12 @@ |
| 34620 | 34659 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 34621 | 34660 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 34622 | 34661 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 34623 | 34662 | ** proleptic Gregorian calendar. |
| 34624 | 34663 | ** |
| 34625 | | -** On success, return 0. Return 1 if the time and date cannot be found. |
| 34664 | +** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 34665 | +** cannot be found. |
| 34626 | 34666 | */ |
| 34627 | 34667 | static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){ |
| 34628 | 34668 | /* FILETIME structure is a 64-bit value representing the number of |
| 34629 | 34669 | 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). |
| 34630 | 34670 | */ |
| | @@ -34640,11 +34680,11 @@ |
| 34640 | 34680 | #if SQLITE_OS_WINCE |
| 34641 | 34681 | SYSTEMTIME time; |
| 34642 | 34682 | GetSystemTime(&time); |
| 34643 | 34683 | /* if SystemTimeToFileTime() fails, it returns zero. */ |
| 34644 | 34684 | if (!SystemTimeToFileTime(&time,&ft)){ |
| 34645 | | - return 1; |
| 34685 | + return SQLITE_ERROR; |
| 34646 | 34686 | } |
| 34647 | 34687 | #else |
| 34648 | 34688 | GetSystemTimeAsFileTime( &ft ); |
| 34649 | 34689 | #endif |
| 34650 | 34690 | |
| | @@ -34656,19 +34696,19 @@ |
| 34656 | 34696 | if( sqlite3_current_time ){ |
| 34657 | 34697 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 34658 | 34698 | } |
| 34659 | 34699 | #endif |
| 34660 | 34700 | UNUSED_PARAMETER(pVfs); |
| 34661 | | - return 0; |
| 34701 | + return SQLITE_OK; |
| 34662 | 34702 | } |
| 34663 | 34703 | |
| 34664 | 34704 | /* |
| 34665 | 34705 | ** Find the current time (in Universal Coordinated Time). Write the |
| 34666 | 34706 | ** current time and date as a Julian Day number into *prNow and |
| 34667 | 34707 | ** return 0. Return 1 if the time and date cannot be found. |
| 34668 | 34708 | */ |
| 34669 | | -int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
| 34709 | +static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
| 34670 | 34710 | int rc; |
| 34671 | 34711 | sqlite3_int64 i; |
| 34672 | 34712 | rc = winCurrentTimeInt64(pVfs, &i); |
| 34673 | 34713 | if( !rc ){ |
| 34674 | 34714 | *prNow = i/86400000.0; |
| | @@ -35789,12 +35829,10 @@ |
| 35789 | 35829 | typedef struct PCache1 PCache1; |
| 35790 | 35830 | typedef struct PgHdr1 PgHdr1; |
| 35791 | 35831 | typedef struct PgFreeslot PgFreeslot; |
| 35792 | 35832 | typedef struct PGroup PGroup; |
| 35793 | 35833 | |
| 35794 | | -typedef struct PGroupBlock PGroupBlock; |
| 35795 | | -typedef struct PGroupBlockList PGroupBlockList; |
| 35796 | 35834 | |
| 35797 | 35835 | /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set |
| 35798 | 35836 | ** of one or more PCaches that are able to recycle each others unpinned |
| 35799 | 35837 | ** pages when they are under memory pressure. A PGroup is an instance of |
| 35800 | 35838 | ** the following object. |
| | @@ -35821,69 +35859,11 @@ |
| 35821 | 35859 | int nMaxPage; /* Sum of nMax for purgeable caches */ |
| 35822 | 35860 | int nMinPage; /* Sum of nMin for purgeable caches */ |
| 35823 | 35861 | int mxPinned; /* nMaxpage + 10 - nMinPage */ |
| 35824 | 35862 | int nCurrentPage; /* Number of purgeable pages allocated */ |
| 35825 | 35863 | PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ |
| 35826 | | -#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 35827 | | - int isBusy; /* Do not run ReleaseMemory() if true */ |
| 35828 | | - PGroupBlockList *pBlockList; /* List of block-lists for this group */ |
| 35829 | | -#endif |
| 35830 | | -}; |
| 35831 | | - |
| 35832 | | -/* |
| 35833 | | -** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built, |
| 35834 | | -** each PGroup structure has a linked list of the the following starting |
| 35835 | | -** at PGroup.pBlockList. There is one entry for each distinct page-size |
| 35836 | | -** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes |
| 35837 | | -** etc.). Variable PGroupBlockList.nByte is set to the actual allocation |
| 35838 | | -** size requested by each pcache, which is the database page-size plus |
| 35839 | | -** the various header structures used by the pcache, pager and btree layers. |
| 35840 | | -** Usually around (pgsz+200) bytes. |
| 35841 | | -** |
| 35842 | | -** This size (pgsz+200) bytes is not allocated efficiently by some |
| 35843 | | -** implementations of malloc. In particular, some implementations are only |
| 35844 | | -** able to allocate blocks of memory chunks of 2^N bytes, where N is some |
| 35845 | | -** integer value. Since the page-size is a power of 2, this means we |
| 35846 | | -** end up wasting (pgsz-200) bytes in each allocation. |
| 35847 | | -** |
| 35848 | | -** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks |
| 35849 | | -** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes |
| 35850 | | -** are requested from malloc allocator. After a block is returned, |
| 35851 | | -** sqlite3MallocSize() is used to determine how many (pgsz+200) byte |
| 35852 | | -** allocations can fit in the space returned by malloc(). This value may |
| 35853 | | -** be more than M. |
| 35854 | | -** |
| 35855 | | -** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry |
| 35856 | | -** contains the number of allocations that will fit in the aData[] space. |
| 35857 | | -** nEntry is limited to the number of bits in bitmask mUsed. If a slot |
| 35858 | | -** within aData is in use, the corresponding bit in mUsed is set. Thus |
| 35859 | | -** when (mUsed+1==(1 << nEntry)) the block is completely full. |
| 35860 | | -** |
| 35861 | | -** Each time a slot within a block is freed, the block is moved to the start |
| 35862 | | -** of the linked-list. And if a block becomes completely full, then it is |
| 35863 | | -** moved to the end of the list. As a result, when searching for a free |
| 35864 | | -** slot, only the first block in the list need be examined. If it is full, |
| 35865 | | -** then it is guaranteed that all blocks are full. |
| 35866 | | -*/ |
| 35867 | | -struct PGroupBlockList { |
| 35868 | | - int nByte; /* Size of each allocation in bytes */ |
| 35869 | | - PGroupBlock *pFirst; /* First PGroupBlock in list */ |
| 35870 | | - PGroupBlock *pLast; /* Last PGroupBlock in list */ |
| 35871 | | - PGroupBlockList *pNext; /* Next block-list attached to group */ |
| 35872 | | -}; |
| 35873 | | - |
| 35874 | | -struct PGroupBlock { |
| 35875 | | - Bitmask mUsed; /* Mask of used slots */ |
| 35876 | | - int nEntry; /* Maximum number of allocations in aData[] */ |
| 35877 | | - u8 *aData; /* Pointer to data block */ |
| 35878 | | - PGroupBlock *pNext; /* Next PGroupBlock in list */ |
| 35879 | | - PGroupBlock *pPrev; /* Previous PGroupBlock in list */ |
| 35880 | | - PGroupBlockList *pList; /* Owner list */ |
| 35881 | | -}; |
| 35882 | | - |
| 35883 | | -/* Minimum value for PGroupBlock.nEntry */ |
| 35884 | | -#define PAGECACHE_BLOCKALLOC_MINENTRY 15 |
| 35864 | +}; |
| 35885 | 35865 | |
| 35886 | 35866 | /* Each page cache is an instance of the following object. Every |
| 35887 | 35867 | ** open database file (including each in-memory database and each |
| 35888 | 35868 | ** temporary or transient database) has a single page cache which |
| 35889 | 35869 | ** is an instance of this object. |
| | @@ -35983,21 +35963,10 @@ |
| 35983 | 35963 | ** |
| 35984 | 35964 | ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X ); |
| 35985 | 35965 | */ |
| 35986 | 35966 | #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage) |
| 35987 | 35967 | #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage) |
| 35988 | | - |
| 35989 | | -/* |
| 35990 | | -** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve |
| 35991 | | -** a PGroupBlock pointer based on a pointer to a page buffer. |
| 35992 | | -*/ |
| 35993 | | -#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \ |
| 35994 | | - ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock ) |
| 35995 | | - |
| 35996 | | -#define PAGE_GET_BLOCKPTR(pCache, pPg) \ |
| 35997 | | - ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) ) |
| 35998 | | - |
| 35999 | 35968 | |
| 36000 | 35969 | /* |
| 36001 | 35970 | ** Macros to enter and leave the PCache LRU mutex. |
| 36002 | 35971 | */ |
| 36003 | 35972 | #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) |
| | @@ -36120,159 +36089,32 @@ |
| 36120 | 36089 | return iSize; |
| 36121 | 36090 | } |
| 36122 | 36091 | } |
| 36123 | 36092 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 36124 | 36093 | |
| 36125 | | -#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36126 | | -/* |
| 36127 | | -** The block pBlock belongs to list pList but is not currently linked in. |
| 36128 | | -** Insert it into the start of the list. |
| 36129 | | -*/ |
| 36130 | | -static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){ |
| 36131 | | - pBlock->pPrev = 0; |
| 36132 | | - pBlock->pNext = pList->pFirst; |
| 36133 | | - pList->pFirst = pBlock; |
| 36134 | | - if( pBlock->pNext ){ |
| 36135 | | - pBlock->pNext->pPrev = pBlock; |
| 36136 | | - }else{ |
| 36137 | | - assert( pList->pLast==0 ); |
| 36138 | | - pList->pLast = pBlock; |
| 36139 | | - } |
| 36140 | | -} |
| 36141 | | - |
| 36142 | | -/* |
| 36143 | | -** If there are no blocks in the list headed by pList, remove pList |
| 36144 | | -** from the pGroup->pBlockList list and free it with sqlite3_free(). |
| 36145 | | -*/ |
| 36146 | | -static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){ |
| 36147 | | - assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36148 | | - if( pList->pFirst==0 ){ |
| 36149 | | - PGroupBlockList **pp; |
| 36150 | | - for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext); |
| 36151 | | - *pp = (*pp)->pNext; |
| 36152 | | - sqlite3_free(pList); |
| 36153 | | - } |
| 36154 | | -} |
| 36155 | | -#endif /* SQLITE_PAGECACHE_BLOCKALLOC */ |
| 36156 | | - |
| 36157 | 36094 | /* |
| 36158 | 36095 | ** Allocate a new page object initially associated with cache pCache. |
| 36159 | 36096 | */ |
| 36160 | 36097 | static PgHdr1 *pcache1AllocPage(PCache1 *pCache){ |
| 36161 | 36098 | int nByte = sizeof(PgHdr1) + pCache->szPage; |
| 36162 | | - void *pPg = 0; |
| 36163 | | - PgHdr1 *p; |
| 36164 | | - |
| 36165 | | -#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36166 | | - PGroup *pGroup = pCache->pGroup; |
| 36167 | | - PGroupBlockList *pList; |
| 36168 | | - PGroupBlock *pBlock; |
| 36169 | | - int i; |
| 36170 | | - |
| 36171 | | - nByte += sizeof(PGroupBlockList *); |
| 36172 | | - nByte = ROUND8(nByte); |
| 36173 | | - |
| 36174 | | - for(pList=pGroup->pBlockList; pList; pList=pList->pNext){ |
| 36175 | | - if( pList->nByte==nByte ) break; |
| 36176 | | - } |
| 36177 | | - if( pList==0 ){ |
| 36178 | | - PGroupBlockList *pNew; |
| 36179 | | - assert( pGroup->isBusy==0 ); |
| 36180 | | - assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36181 | | - pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */ |
| 36182 | | - pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList)); |
| 36183 | | - pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */ |
| 36184 | | - if( pNew==0 ){ |
| 36185 | | - /* malloc() failure. Return early. */ |
| 36186 | | - return 0; |
| 36187 | | - } |
| 36188 | | -#ifdef SQLITE_DEBUG |
| 36189 | | - for(pList=pGroup->pBlockList; pList; pList=pList->pNext){ |
| 36190 | | - assert( pList->nByte!=nByte ); |
| 36191 | | - } |
| 36192 | | -#endif |
| 36193 | | - pNew->nByte = nByte; |
| 36194 | | - pNew->pNext = pGroup->pBlockList; |
| 36195 | | - pGroup->pBlockList = pNew; |
| 36196 | | - pList = pNew; |
| 36197 | | - } |
| 36198 | | - |
| 36199 | | - pBlock = pList->pFirst; |
| 36200 | | - if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){ |
| 36201 | | - int sz; |
| 36202 | | - |
| 36203 | | - /* Allocate a new block. Try to allocate enough space for the PGroupBlock |
| 36204 | | - ** structure and MINENTRY allocations of nByte bytes each. If the |
| 36205 | | - ** allocator returns more memory than requested, then more than MINENTRY |
| 36206 | | - ** allocations may fit in it. */ |
| 36207 | | - assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36208 | | - pcache1LeaveMutex(pCache->pGroup); |
| 36209 | | - sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte; |
| 36210 | | - pBlock = (PGroupBlock *)sqlite3Malloc(sz); |
| 36211 | | - pcache1EnterMutex(pCache->pGroup); |
| 36212 | | - |
| 36213 | | - if( !pBlock ){ |
| 36214 | | - freeListIfEmpty(pGroup, pList); |
| 36215 | | - return 0; |
| 36216 | | - } |
| 36217 | | - pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte; |
| 36218 | | - if( pBlock->nEntry>=BMS ){ |
| 36219 | | - pBlock->nEntry = BMS-1; |
| 36220 | | - } |
| 36221 | | - pBlock->pList = pList; |
| 36222 | | - pBlock->mUsed = 0; |
| 36223 | | - pBlock->aData = (u8 *)&pBlock[1]; |
| 36224 | | - addBlockToList(pList, pBlock); |
| 36225 | | - |
| 36226 | | - sz = sqlite3MallocSize(pBlock); |
| 36227 | | - sqlite3_mutex_enter(pcache1.mutex); |
| 36228 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 36229 | | - sqlite3_mutex_leave(pcache1.mutex); |
| 36230 | | - } |
| 36231 | | - |
| 36232 | | - for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){ |
| 36233 | | - if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){ |
| 36234 | | - pBlock->mUsed |= ((Bitmask)1<<i); |
| 36235 | | - pPg = (void *)&pBlock->aData[pList->nByte * i]; |
| 36236 | | - } |
| 36237 | | - } |
| 36238 | | - assert( pPg ); |
| 36239 | | - PAGE_SET_BLOCKPTR(pCache, pPg, pBlock); |
| 36240 | | - |
| 36241 | | - /* If the block is now full, shift it to the end of the list */ |
| 36242 | | - if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){ |
| 36243 | | - assert( pList->pFirst==pBlock ); |
| 36244 | | - assert( pBlock->pPrev==0 ); |
| 36245 | | - assert( pList->pLast->pNext==0 ); |
| 36246 | | - pList->pFirst = pBlock->pNext; |
| 36247 | | - pList->pFirst->pPrev = 0; |
| 36248 | | - pBlock->pPrev = pList->pLast; |
| 36249 | | - pBlock->pNext = 0; |
| 36250 | | - pList->pLast->pNext = pBlock; |
| 36251 | | - pList->pLast = pBlock; |
| 36252 | | - } |
| 36253 | | - p = PAGE_TO_PGHDR1(pCache, pPg); |
| 36254 | | - if( pCache->bPurgeable ){ |
| 36255 | | - pCache->pGroup->nCurrentPage++; |
| 36256 | | - } |
| 36257 | | -#else |
| 36099 | + PgHdr1 *p = 0; |
| 36100 | + void *pPg; |
| 36101 | + |
| 36258 | 36102 | /* The group mutex must be released before pcache1Alloc() is called. This |
| 36259 | 36103 | ** is because it may call sqlite3_release_memory(), which assumes that |
| 36260 | 36104 | ** this mutex is not held. */ |
| 36261 | 36105 | assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 36262 | 36106 | pcache1LeaveMutex(pCache->pGroup); |
| 36263 | 36107 | pPg = pcache1Alloc(nByte); |
| 36264 | 36108 | pcache1EnterMutex(pCache->pGroup); |
| 36109 | + |
| 36265 | 36110 | if( pPg ){ |
| 36266 | 36111 | p = PAGE_TO_PGHDR1(pCache, pPg); |
| 36267 | 36112 | if( pCache->bPurgeable ){ |
| 36268 | 36113 | pCache->pGroup->nCurrentPage++; |
| 36269 | 36114 | } |
| 36270 | | - }else{ |
| 36271 | | - p = 0; |
| 36272 | 36115 | } |
| 36273 | | -#endif |
| 36274 | 36116 | return p; |
| 36275 | 36117 | } |
| 36276 | 36118 | |
| 36277 | 36119 | /* |
| 36278 | 36120 | ** Free a page object allocated by pcache1AllocPage(). |
| | @@ -36282,53 +36124,12 @@ |
| 36282 | 36124 | ** with a NULL pointer, so we mark the NULL test with ALWAYS(). |
| 36283 | 36125 | */ |
| 36284 | 36126 | static void pcache1FreePage(PgHdr1 *p){ |
| 36285 | 36127 | if( ALWAYS(p) ){ |
| 36286 | 36128 | PCache1 *pCache = p->pCache; |
| 36287 | | - void *pPg = PGHDR1_TO_PAGE(p); |
| 36288 | | - |
| 36289 | | -#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36290 | | - PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg); |
| 36291 | | - PGroupBlockList *pList = pBlock->pList; |
| 36292 | | - int i = ((u8 *)pPg - pBlock->aData) / pList->nByte; |
| 36293 | | - |
| 36294 | | - assert( pPg==(void *)&pBlock->aData[i*pList->nByte] ); |
| 36295 | | - assert( pBlock->mUsed & ((Bitmask)1<<i) ); |
| 36296 | | - pBlock->mUsed &= ~((Bitmask)1<<i); |
| 36297 | | - |
| 36298 | | - /* Remove the block from the list. If it is completely empty, free it. |
| 36299 | | - ** Or if it is not completely empty, re-insert it at the start of the |
| 36300 | | - ** list. */ |
| 36301 | | - if( pList->pFirst==pBlock ){ |
| 36302 | | - pList->pFirst = pBlock->pNext; |
| 36303 | | - if( pList->pFirst ) pList->pFirst->pPrev = 0; |
| 36304 | | - }else{ |
| 36305 | | - pBlock->pPrev->pNext = pBlock->pNext; |
| 36306 | | - } |
| 36307 | | - if( pList->pLast==pBlock ){ |
| 36308 | | - pList->pLast = pBlock->pPrev; |
| 36309 | | - if( pList->pLast ) pList->pLast->pNext = 0; |
| 36310 | | - }else{ |
| 36311 | | - pBlock->pNext->pPrev = pBlock->pPrev; |
| 36312 | | - } |
| 36313 | | - |
| 36314 | | - if( pBlock->mUsed==0 ){ |
| 36315 | | - PGroup *pGroup = p->pCache->pGroup; |
| 36316 | | - |
| 36317 | | - int sz = sqlite3MallocSize(pBlock); |
| 36318 | | - sqlite3_mutex_enter(pcache1.mutex); |
| 36319 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz); |
| 36320 | | - sqlite3_mutex_leave(pcache1.mutex); |
| 36321 | | - freeListIfEmpty(pGroup, pList); |
| 36322 | | - sqlite3_free(pBlock); |
| 36323 | | - }else{ |
| 36324 | | - addBlockToList(pList, pBlock); |
| 36325 | | - } |
| 36326 | | -#else |
| 36327 | 36129 | assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); |
| 36328 | | - pcache1Free(pPg); |
| 36329 | | -#endif |
| 36130 | + pcache1Free(PGHDR1_TO_PAGE(p)); |
| 36330 | 36131 | if( pCache->bPurgeable ){ |
| 36331 | 36132 | pCache->pGroup->nCurrentPage--; |
| 36332 | 36133 | } |
| 36333 | 36134 | } |
| 36334 | 36135 | } |
| | @@ -36935,13 +36736,10 @@ |
| 36935 | 36736 | ** been released, the function returns. The return value is the total number |
| 36936 | 36737 | ** of bytes of memory released. |
| 36937 | 36738 | */ |
| 36938 | 36739 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 36939 | 36740 | int nFree = 0; |
| 36940 | | -#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36941 | | - if( pcache1.grp.isBusy ) return 0; |
| 36942 | | -#endif |
| 36943 | 36741 | assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 36944 | 36742 | assert( sqlite3_mutex_notheld(pcache1.mutex) ); |
| 36945 | 36743 | if( pcache1.pStart==0 ){ |
| 36946 | 36744 | PgHdr1 *p; |
| 36947 | 36745 | pcache1EnterMutex(&pcache1.grp); |
| | @@ -38200,12 +37998,12 @@ |
| 38200 | 37998 | i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 38201 | 37999 | char *zFilename; /* Name of the database file */ |
| 38202 | 38000 | char *zJournal; /* Name of the journal file */ |
| 38203 | 38001 | int (*xBusyHandler)(void*); /* Function to call when busy */ |
| 38204 | 38002 | void *pBusyHandlerArg; /* Context argument for xBusyHandler */ |
| 38003 | + int nHit, nMiss; /* Total cache hits and misses */ |
| 38205 | 38004 | #ifdef SQLITE_TEST |
| 38206 | | - int nHit, nMiss; /* Cache hits and missing */ |
| 38207 | 38005 | int nRead, nWrite; /* Database pages read/written */ |
| 38208 | 38006 | #endif |
| 38209 | 38007 | void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ |
| 38210 | 38008 | #ifdef SQLITE_HAS_CODEC |
| 38211 | 38009 | void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ |
| | @@ -40233,11 +40031,10 @@ |
| 40233 | 40031 | needPagerReset = 0; |
| 40234 | 40032 | } |
| 40235 | 40033 | rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0); |
| 40236 | 40034 | if( rc!=SQLITE_OK ){ |
| 40237 | 40035 | if( rc==SQLITE_DONE ){ |
| 40238 | | - rc = SQLITE_OK; |
| 40239 | 40036 | pPager->journalOff = szJ; |
| 40240 | 40037 | break; |
| 40241 | 40038 | }else if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 40242 | 40039 | /* If the journal has been truncated, simply stop reading and |
| 40243 | 40040 | ** processing the journal. This might happen if the journal was |
| | @@ -40495,10 +40292,11 @@ |
| 40495 | 40292 | #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES) |
| 40496 | 40293 | PgHdr *p; /* For looping over pages */ |
| 40497 | 40294 | #endif |
| 40498 | 40295 | |
| 40499 | 40296 | assert( pPager->pWal ); |
| 40297 | + assert( pList ); |
| 40500 | 40298 | #ifdef SQLITE_DEBUG |
| 40501 | 40299 | /* Verify that the page list is in accending order */ |
| 40502 | 40300 | for(p=pList; p && p->pDirty; p=p->pDirty){ |
| 40503 | 40301 | assert( p->pgno < p->pDirty->pgno ); |
| 40504 | 40302 | } |
| | @@ -41699,11 +41497,11 @@ |
| 41699 | 41497 | ** The doNotSpill flag inhibits all cache spilling regardless of whether |
| 41700 | 41498 | ** or not a sync is required. This is set during a rollback. |
| 41701 | 41499 | ** |
| 41702 | 41500 | ** Spilling is also prohibited when in an error state since that could |
| 41703 | 41501 | ** lead to database corruption. In the current implementaton it |
| 41704 | | - ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1 |
| 41502 | + ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1 |
| 41705 | 41503 | ** while in the error state, hence it is impossible for this routine to |
| 41706 | 41504 | ** be called in the error state. Nevertheless, we include a NEVER() |
| 41707 | 41505 | ** test for the error state as a safeguard against future changes. |
| 41708 | 41506 | */ |
| 41709 | 41507 | if( NEVER(pPager->errCode) ) return SQLITE_OK; |
| | @@ -42535,18 +42333,17 @@ |
| 42535 | 42333 | |
| 42536 | 42334 | if( (*ppPage)->pPager && !noContent ){ |
| 42537 | 42335 | /* In this case the pcache already contains an initialized copy of |
| 42538 | 42336 | ** the page. Return without further ado. */ |
| 42539 | 42337 | assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); |
| 42540 | | - PAGER_INCR(pPager->nHit); |
| 42338 | + pPager->nHit++; |
| 42541 | 42339 | return SQLITE_OK; |
| 42542 | 42340 | |
| 42543 | 42341 | }else{ |
| 42544 | 42342 | /* The pager cache has created a new page. Its content needs to |
| 42545 | 42343 | ** be initialized. */ |
| 42546 | 42344 | |
| 42547 | | - PAGER_INCR(pPager->nMiss); |
| 42548 | 42345 | pPg = *ppPage; |
| 42549 | 42346 | pPg->pPager = pPager; |
| 42550 | 42347 | |
| 42551 | 42348 | /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page |
| 42552 | 42349 | ** number greater than this, or the unused locking-page, is requested. */ |
| | @@ -42578,10 +42375,11 @@ |
| 42578 | 42375 | } |
| 42579 | 42376 | memset(pPg->pData, 0, pPager->pageSize); |
| 42580 | 42377 | IOTRACE(("ZERO %p %d\n", pPager, pgno)); |
| 42581 | 42378 | }else{ |
| 42582 | 42379 | assert( pPg->pPager==pPager ); |
| 42380 | + pPager->nMiss++; |
| 42583 | 42381 | rc = readDbPage(pPg); |
| 42584 | 42382 | if( rc!=SQLITE_OK ){ |
| 42585 | 42383 | goto pager_acquire_err; |
| 42586 | 42384 | } |
| 42587 | 42385 | } |
| | @@ -43611,10 +43409,35 @@ |
| 43611 | 43409 | a[9] = pPager->nRead; |
| 43612 | 43410 | a[10] = pPager->nWrite; |
| 43613 | 43411 | return a; |
| 43614 | 43412 | } |
| 43615 | 43413 | #endif |
| 43414 | + |
| 43415 | +/* |
| 43416 | +** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or |
| 43417 | +** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the |
| 43418 | +** current cache hit or miss count, according to the value of eStat. If the |
| 43419 | +** reset parameter is non-zero, the cache hit or miss count is zeroed before |
| 43420 | +** returning. |
| 43421 | +*/ |
| 43422 | +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){ |
| 43423 | + int *piStat; |
| 43424 | + |
| 43425 | + assert( eStat==SQLITE_DBSTATUS_CACHE_HIT |
| 43426 | + || eStat==SQLITE_DBSTATUS_CACHE_MISS |
| 43427 | + ); |
| 43428 | + if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){ |
| 43429 | + piStat = &pPager->nHit; |
| 43430 | + }else{ |
| 43431 | + piStat = &pPager->nMiss; |
| 43432 | + } |
| 43433 | + |
| 43434 | + *pnVal += *piStat; |
| 43435 | + if( reset ){ |
| 43436 | + *piStat = 0; |
| 43437 | + } |
| 43438 | +} |
| 43616 | 43439 | |
| 43617 | 43440 | /* |
| 43618 | 43441 | ** Return true if this is an in-memory pager. |
| 43619 | 43442 | */ |
| 43620 | 43443 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){ |
| | @@ -46706,11 +46529,11 @@ |
| 46706 | 46529 | */ |
| 46707 | 46530 | if( iRead ){ |
| 46708 | 46531 | int sz; |
| 46709 | 46532 | i64 iOffset; |
| 46710 | 46533 | sz = pWal->hdr.szPage; |
| 46711 | | - sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); |
| 46534 | + sz = (sz&0xfe00) + ((sz&0x0001)<<16); |
| 46712 | 46535 | testcase( sz<=32768 ); |
| 46713 | 46536 | testcase( sz>=65536 ); |
| 46714 | 46537 | iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE; |
| 46715 | 46538 | *pInWal = 1; |
| 46716 | 46539 | /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */ |
| | @@ -50019,21 +49842,23 @@ |
| 50019 | 49842 | */ |
| 50020 | 49843 | if( isMemdb==0 && isTempDb==0 ){ |
| 50021 | 49844 | if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
| 50022 | 49845 | int nFullPathname = pVfs->mxPathname+1; |
| 50023 | 49846 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
| 50024 | | - sqlite3_mutex *mutexShared; |
| 49847 | + MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
| 50025 | 49848 | p->sharable = 1; |
| 50026 | 49849 | if( !zFullPathname ){ |
| 50027 | 49850 | sqlite3_free(p); |
| 50028 | 49851 | return SQLITE_NOMEM; |
| 50029 | 49852 | } |
| 50030 | 49853 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
| 49854 | +#if SQLITE_THREADSAFE |
| 50031 | 49855 | mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); |
| 50032 | 49856 | sqlite3_mutex_enter(mutexOpen); |
| 50033 | 49857 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 50034 | 49858 | sqlite3_mutex_enter(mutexShared); |
| 49859 | +#endif |
| 50035 | 49860 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
| 50036 | 49861 | assert( pBt->nRef>0 ); |
| 50037 | 49862 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 50038 | 49863 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 50039 | 49864 | int iDb; |
| | @@ -50135,13 +49960,13 @@ |
| 50135 | 49960 | |
| 50136 | 49961 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 50137 | 49962 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 50138 | 49963 | */ |
| 50139 | 49964 | if( p->sharable ){ |
| 50140 | | - sqlite3_mutex *mutexShared; |
| 49965 | + MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
| 50141 | 49966 | pBt->nRef = 1; |
| 50142 | | - mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 49967 | + MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);) |
| 50143 | 49968 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
| 50144 | 49969 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
| 50145 | 49970 | if( pBt->mutex==0 ){ |
| 50146 | 49971 | rc = SQLITE_NOMEM; |
| 50147 | 49972 | db->mallocFailed = 0; |
| | @@ -50219,16 +50044,16 @@ |
| 50219 | 50044 | ** true if the BtShared.nRef counter reaches zero and return |
| 50220 | 50045 | ** false if it is still positive. |
| 50221 | 50046 | */ |
| 50222 | 50047 | static int removeFromSharingList(BtShared *pBt){ |
| 50223 | 50048 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 50224 | | - sqlite3_mutex *pMaster; |
| 50049 | + MUTEX_LOGIC( sqlite3_mutex *pMaster; ) |
| 50225 | 50050 | BtShared *pList; |
| 50226 | 50051 | int removed = 0; |
| 50227 | 50052 | |
| 50228 | 50053 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
| 50229 | | - pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 50054 | + MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 50230 | 50055 | sqlite3_mutex_enter(pMaster); |
| 50231 | 50056 | pBt->nRef--; |
| 50232 | 50057 | if( pBt->nRef<=0 ){ |
| 50233 | 50058 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 50234 | 50059 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
| | @@ -52191,25 +52016,59 @@ |
| 52191 | 52016 | offset -= ovflSize; |
| 52192 | 52017 | }else{ |
| 52193 | 52018 | /* Need to read this page properly. It contains some of the |
| 52194 | 52019 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
| 52195 | 52020 | */ |
| 52196 | | - DbPage *pDbPage; |
| 52021 | +#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 52022 | + sqlite3_file *fd; |
| 52023 | +#endif |
| 52197 | 52024 | int a = amt; |
| 52198 | | - rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
| 52199 | | - if( rc==SQLITE_OK ){ |
| 52200 | | - aPayload = sqlite3PagerGetData(pDbPage); |
| 52201 | | - nextPage = get4byte(aPayload); |
| 52202 | | - if( a + offset > ovflSize ){ |
| 52203 | | - a = ovflSize - offset; |
| 52204 | | - } |
| 52205 | | - rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 52206 | | - sqlite3PagerUnref(pDbPage); |
| 52207 | | - offset = 0; |
| 52208 | | - amt -= a; |
| 52209 | | - pBuf += a; |
| 52210 | | - } |
| 52025 | + if( a + offset > ovflSize ){ |
| 52026 | + a = ovflSize - offset; |
| 52027 | + } |
| 52028 | + |
| 52029 | +#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 52030 | + /* If all the following are true: |
| 52031 | + ** |
| 52032 | + ** 1) this is a read operation, and |
| 52033 | + ** 2) data is required from the start of this overflow page, and |
| 52034 | + ** 3) the database is file-backed, and |
| 52035 | + ** 4) there is no open write-transaction, and |
| 52036 | + ** 5) the database is not a WAL database, |
| 52037 | + ** |
| 52038 | + ** then data can be read directly from the database file into the |
| 52039 | + ** output buffer, bypassing the page-cache altogether. This speeds |
| 52040 | + ** up loading large records that span many overflow pages. |
| 52041 | + */ |
| 52042 | + if( eOp==0 /* (1) */ |
| 52043 | + && offset==0 /* (2) */ |
| 52044 | + && pBt->inTransaction==TRANS_READ /* (4) */ |
| 52045 | + && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ |
| 52046 | + && pBt->pPage1->aData[19]==0x01 /* (5) */ |
| 52047 | + ){ |
| 52048 | + u8 aSave[4]; |
| 52049 | + u8 *aWrite = &pBuf[-4]; |
| 52050 | + memcpy(aSave, aWrite, 4); |
| 52051 | + rc = sqlite3OsRead(fd, aWrite, a+4, pBt->pageSize * (nextPage-1)); |
| 52052 | + nextPage = get4byte(aWrite); |
| 52053 | + memcpy(aWrite, aSave, 4); |
| 52054 | + }else |
| 52055 | +#endif |
| 52056 | + |
| 52057 | + { |
| 52058 | + DbPage *pDbPage; |
| 52059 | + rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
| 52060 | + if( rc==SQLITE_OK ){ |
| 52061 | + aPayload = sqlite3PagerGetData(pDbPage); |
| 52062 | + nextPage = get4byte(aPayload); |
| 52063 | + rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 52064 | + sqlite3PagerUnref(pDbPage); |
| 52065 | + offset = 0; |
| 52066 | + } |
| 52067 | + } |
| 52068 | + amt -= a; |
| 52069 | + pBuf += a; |
| 52211 | 52070 | } |
| 52212 | 52071 | } |
| 52213 | 52072 | } |
| 52214 | 52073 | |
| 52215 | 52074 | if( rc==SQLITE_OK && amt>0 ){ |
| | @@ -52804,11 +52663,10 @@ |
| 52804 | 52663 | } |
| 52805 | 52664 | } |
| 52806 | 52665 | if( c==0 ){ |
| 52807 | 52666 | if( pPage->intKey && !pPage->leaf ){ |
| 52808 | 52667 | lwr = idx; |
| 52809 | | - upr = lwr - 1; |
| 52810 | 52668 | break; |
| 52811 | 52669 | }else{ |
| 52812 | 52670 | *pRes = 0; |
| 52813 | 52671 | rc = SQLITE_OK; |
| 52814 | 52672 | goto moveto_finish; |
| | @@ -52822,11 +52680,11 @@ |
| 52822 | 52680 | if( lwr>upr ){ |
| 52823 | 52681 | break; |
| 52824 | 52682 | } |
| 52825 | 52683 | pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2); |
| 52826 | 52684 | } |
| 52827 | | - assert( lwr==upr+1 ); |
| 52685 | + assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); |
| 52828 | 52686 | assert( pPage->isInit ); |
| 52829 | 52687 | if( pPage->leaf ){ |
| 52830 | 52688 | chldPg = 0; |
| 52831 | 52689 | }else if( lwr>=pPage->nCell ){ |
| 52832 | 52690 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| | @@ -53087,10 +52945,12 @@ |
| 53087 | 52945 | } |
| 53088 | 52946 | if( rc ){ |
| 53089 | 52947 | pTrunk = 0; |
| 53090 | 52948 | goto end_allocate_page; |
| 53091 | 52949 | } |
| 52950 | + assert( pTrunk!=0 ); |
| 52951 | + assert( pTrunk->aData!=0 ); |
| 53092 | 52952 | |
| 53093 | 52953 | k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */ |
| 53094 | 52954 | if( k==0 && !searchList ){ |
| 53095 | 52955 | /* The trunk has no leaves and the list is not being searched. |
| 53096 | 52956 | ** So extract the trunk page itself and use it as the newly |
| | @@ -54214,17 +54074,19 @@ |
| 54214 | 54074 | ** This is safe because dropping a cell only overwrites the first |
| 54215 | 54075 | ** four bytes of it, and this function does not need the first |
| 54216 | 54076 | ** four bytes of the divider cell. So the pointer is safe to use |
| 54217 | 54077 | ** later on. |
| 54218 | 54078 | ** |
| 54219 | | - ** Unless SQLite is compiled in secure-delete mode. In this case, |
| 54079 | + ** But not if we are in secure-delete mode. In secure-delete mode, |
| 54220 | 54080 | ** the dropCell() routine will overwrite the entire cell with zeroes. |
| 54221 | 54081 | ** In this case, temporarily copy the cell into the aOvflSpace[] |
| 54222 | 54082 | ** buffer. It will be copied out again as soon as the aSpace[] buffer |
| 54223 | 54083 | ** is allocated. */ |
| 54224 | 54084 | if( pBt->secureDelete ){ |
| 54225 | | - int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); |
| 54085 | + int iOff; |
| 54086 | + |
| 54087 | + iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); |
| 54226 | 54088 | if( (iOff+szNew[i])>(int)pBt->usableSize ){ |
| 54227 | 54089 | rc = SQLITE_CORRUPT_BKPT; |
| 54228 | 54090 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
| 54229 | 54091 | goto balance_cleanup; |
| 54230 | 54092 | }else{ |
| | @@ -54640,10 +54502,11 @@ |
| 54640 | 54502 | int isDivider = 0; |
| 54641 | 54503 | while( i==iNextOld ){ |
| 54642 | 54504 | /* Cell i is the cell immediately following the last cell on old |
| 54643 | 54505 | ** sibling page j. If the siblings are not leaf pages of an |
| 54644 | 54506 | ** intkey b-tree, then cell i was a divider cell. */ |
| 54507 | + assert( j+1 < ArraySize(apCopy) ); |
| 54645 | 54508 | pOld = apCopy[++j]; |
| 54646 | 54509 | iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow; |
| 54647 | 54510 | if( pOld->nOverflow ){ |
| 54648 | 54511 | nOverflow = pOld->nOverflow; |
| 54649 | 54512 | iOverflow = i + !leafData + pOld->aOvfl[0].idx; |
| | @@ -56982,18 +56845,18 @@ |
| 56982 | 56845 | /* |
| 56983 | 56846 | ** Release all resources associated with an sqlite3_backup* handle. |
| 56984 | 56847 | */ |
| 56985 | 56848 | SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){ |
| 56986 | 56849 | sqlite3_backup **pp; /* Ptr to head of pagers backup list */ |
| 56987 | | - sqlite3_mutex *mutex; /* Mutex to protect source database */ |
| 56850 | + MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */ |
| 56988 | 56851 | int rc; /* Value to return */ |
| 56989 | 56852 | |
| 56990 | 56853 | /* Enter the mutexes */ |
| 56991 | 56854 | if( p==0 ) return SQLITE_OK; |
| 56992 | 56855 | sqlite3_mutex_enter(p->pSrcDb->mutex); |
| 56993 | 56856 | sqlite3BtreeEnter(p->pSrc); |
| 56994 | | - mutex = p->pSrcDb->mutex; |
| 56857 | + MUTEX_LOGIC( mutex = p->pSrcDb->mutex; ) |
| 56995 | 56858 | if( p->pDestDb ){ |
| 56996 | 56859 | sqlite3_mutex_enter(p->pDestDb->mutex); |
| 56997 | 56860 | } |
| 56998 | 56861 | |
| 56999 | 56862 | /* Detach this backup from the source pager. */ |
| | @@ -57108,13 +56971,21 @@ |
| 57108 | 56971 | ** goes wrong, the transaction on pTo is rolled back. If successful, the |
| 57109 | 56972 | ** transaction is committed before returning. |
| 57110 | 56973 | */ |
| 57111 | 56974 | SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 57112 | 56975 | int rc; |
| 56976 | + sqlite3_file *pFd; /* File descriptor for database pTo */ |
| 57113 | 56977 | sqlite3_backup b; |
| 57114 | 56978 | sqlite3BtreeEnter(pTo); |
| 57115 | 56979 | sqlite3BtreeEnter(pFrom); |
| 56980 | + |
| 56981 | + assert( sqlite3BtreeIsInTrans(pTo) ); |
| 56982 | + pFd = sqlite3PagerFile(sqlite3BtreePager(pTo)); |
| 56983 | + if( pFd->pMethods ){ |
| 56984 | + i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom); |
| 56985 | + sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte); |
| 56986 | + } |
| 57116 | 56987 | |
| 57117 | 56988 | /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set |
| 57118 | 56989 | ** to 0. This is used by the implementations of sqlite3_backup_step() |
| 57119 | 56990 | ** and sqlite3_backup_finish() to detect that they are being called |
| 57120 | 56991 | ** from this function, not directly by the user. |
| | @@ -57137,10 +57008,11 @@ |
| 57137 | 57008 | rc = sqlite3_backup_finish(&b); |
| 57138 | 57009 | if( rc==SQLITE_OK ){ |
| 57139 | 57010 | pTo->pBt->pageSizeFixed = 0; |
| 57140 | 57011 | } |
| 57141 | 57012 | |
| 57013 | + assert( sqlite3BtreeIsInTrans(pTo)==0 ); |
| 57142 | 57014 | sqlite3BtreeLeave(pFrom); |
| 57143 | 57015 | sqlite3BtreeLeave(pTo); |
| 57144 | 57016 | return rc; |
| 57145 | 57017 | } |
| 57146 | 57018 | #endif /* SQLITE_OMIT_VACUUM */ |
| | @@ -58171,15 +58043,15 @@ |
| 58171 | 58043 | *ppVal = 0; |
| 58172 | 58044 | return SQLITE_OK; |
| 58173 | 58045 | } |
| 58174 | 58046 | op = pExpr->op; |
| 58175 | 58047 | |
| 58176 | | - /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2. |
| 58048 | + /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3. |
| 58177 | 58049 | ** The ifdef here is to enable us to achieve 100% branch test coverage even |
| 58178 | | - ** when SQLITE_ENABLE_STAT2 is omitted. |
| 58050 | + ** when SQLITE_ENABLE_STAT3 is omitted. |
| 58179 | 58051 | */ |
| 58180 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 58052 | +#ifdef SQLITE_ENABLE_STAT3 |
| 58181 | 58053 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 58182 | 58054 | #else |
| 58183 | 58055 | if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 58184 | 58056 | #endif |
| 58185 | 58057 | |
| | @@ -58874,12 +58746,12 @@ |
| 58874 | 58746 | /* |
| 58875 | 58747 | ** Change the P2 operand of instruction addr so that it points to |
| 58876 | 58748 | ** the address of the next instruction to be coded. |
| 58877 | 58749 | */ |
| 58878 | 58750 | SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
| 58879 | | - assert( addr>=0 ); |
| 58880 | | - sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 58751 | + assert( addr>=0 || p->db->mallocFailed ); |
| 58752 | + if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 58881 | 58753 | } |
| 58882 | 58754 | |
| 58883 | 58755 | |
| 58884 | 58756 | /* |
| 58885 | 58757 | ** If the input FuncDef structure is ephemeral, then free it. If |
| | @@ -59080,34 +58952,33 @@ |
| 59080 | 58952 | ** Change the comment on the the most recently coded instruction. Or |
| 59081 | 58953 | ** insert a No-op and add the comment to that new instruction. This |
| 59082 | 58954 | ** makes the code easier to read during debugging. None of this happens |
| 59083 | 58955 | ** in a production build. |
| 59084 | 58956 | */ |
| 59085 | | -SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
| 59086 | | - va_list ap; |
| 59087 | | - if( !p ) return; |
| 58957 | +static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ |
| 59088 | 58958 | assert( p->nOp>0 || p->aOp==0 ); |
| 59089 | 58959 | assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
| 59090 | 58960 | if( p->nOp ){ |
| 59091 | | - char **pz = &p->aOp[p->nOp-1].zComment; |
| 58961 | + assert( p->aOp ); |
| 58962 | + sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); |
| 58963 | + p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); |
| 58964 | + } |
| 58965 | +} |
| 58966 | +SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
| 58967 | + va_list ap; |
| 58968 | + if( p ){ |
| 59092 | 58969 | va_start(ap, zFormat); |
| 59093 | | - sqlite3DbFree(p->db, *pz); |
| 59094 | | - *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
| 58970 | + vdbeVComment(p, zFormat, ap); |
| 59095 | 58971 | va_end(ap); |
| 59096 | 58972 | } |
| 59097 | 58973 | } |
| 59098 | 58974 | SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ |
| 59099 | 58975 | va_list ap; |
| 59100 | | - if( !p ) return; |
| 59101 | | - sqlite3VdbeAddOp0(p, OP_Noop); |
| 59102 | | - assert( p->nOp>0 || p->aOp==0 ); |
| 59103 | | - assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
| 59104 | | - if( p->nOp ){ |
| 59105 | | - char **pz = &p->aOp[p->nOp-1].zComment; |
| 58976 | + if( p ){ |
| 58977 | + sqlite3VdbeAddOp0(p, OP_Noop); |
| 59106 | 58978 | va_start(ap, zFormat); |
| 59107 | | - sqlite3DbFree(p->db, *pz); |
| 59108 | | - *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
| 58979 | + vdbeVComment(p, zFormat, ap); |
| 59109 | 58980 | va_end(ap); |
| 59110 | 58981 | } |
| 59111 | 58982 | } |
| 59112 | 58983 | #endif /* NDEBUG */ |
| 59113 | 58984 | |
| | @@ -59441,11 +59312,11 @@ |
| 59441 | 59312 | SubProgram **apSub = 0; /* Array of sub-vdbes */ |
| 59442 | 59313 | Mem *pSub = 0; /* Memory cell hold array of subprogs */ |
| 59443 | 59314 | sqlite3 *db = p->db; /* The database connection */ |
| 59444 | 59315 | int i; /* Loop counter */ |
| 59445 | 59316 | int rc = SQLITE_OK; /* Return code */ |
| 59446 | | - Mem *pMem = p->pResultSet = &p->aMem[1]; /* First Mem of result set */ |
| 59317 | + Mem *pMem = &p->aMem[1]; /* First Mem of result set */ |
| 59447 | 59318 | |
| 59448 | 59319 | assert( p->explain ); |
| 59449 | 59320 | assert( p->magic==VDBE_MAGIC_RUN ); |
| 59450 | 59321 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); |
| 59451 | 59322 | |
| | @@ -59452,10 +59323,11 @@ |
| 59452 | 59323 | /* Even though this opcode does not use dynamic strings for |
| 59453 | 59324 | ** the result, result columns may become dynamic if the user calls |
| 59454 | 59325 | ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
| 59455 | 59326 | */ |
| 59456 | 59327 | releaseMemArray(pMem, 8); |
| 59328 | + p->pResultSet = 0; |
| 59457 | 59329 | |
| 59458 | 59330 | if( p->rc==SQLITE_NOMEM ){ |
| 59459 | 59331 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 59460 | 59332 | ** sqlite3_column_text16() failed. */ |
| 59461 | 59333 | db->mallocFailed = 1; |
| | @@ -59606,10 +59478,11 @@ |
| 59606 | 59478 | pMem->type = SQLITE_NULL; |
| 59607 | 59479 | } |
| 59608 | 59480 | } |
| 59609 | 59481 | |
| 59610 | 59482 | p->nResColumn = 8 - 4*(p->explain-1); |
| 59483 | + p->pResultSet = &p->aMem[1]; |
| 59611 | 59484 | p->rc = SQLITE_OK; |
| 59612 | 59485 | rc = SQLITE_ROW; |
| 59613 | 59486 | } |
| 59614 | 59487 | return rc; |
| 59615 | 59488 | } |
| | @@ -61361,11 +61234,11 @@ |
| 61361 | 61234 | ** than 2GiB are support - anything large must be database corruption. |
| 61362 | 61235 | ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so |
| 61363 | 61236 | ** this code can safely assume that nCellKey is 32-bits |
| 61364 | 61237 | */ |
| 61365 | 61238 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 61366 | | - rc = sqlite3BtreeKeySize(pCur, &nCellKey); |
| 61239 | + VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
| 61367 | 61240 | assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
| 61368 | 61241 | assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); |
| 61369 | 61242 | |
| 61370 | 61243 | /* Read in the complete content of the index entry */ |
| 61371 | 61244 | memset(&m, 0, sizeof(m)); |
| | @@ -61436,11 +61309,11 @@ |
| 61436 | 61309 | int rc; |
| 61437 | 61310 | BtCursor *pCur = pC->pCursor; |
| 61438 | 61311 | Mem m; |
| 61439 | 61312 | |
| 61440 | 61313 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 61441 | | - rc = sqlite3BtreeKeySize(pCur, &nCellKey); |
| 61314 | + VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
| 61442 | 61315 | assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
| 61443 | 61316 | /* nCellKey will always be between 0 and 0xffffffff because of the say |
| 61444 | 61317 | ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
| 61445 | 61318 | if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
| 61446 | 61319 | *res = 0; |
| | @@ -65712,20 +65585,20 @@ |
| 65712 | 65585 | }else if( u.am.pC->cacheStatus==p->cacheCtr ){ |
| 65713 | 65586 | u.am.payloadSize = u.am.pC->payloadSize; |
| 65714 | 65587 | u.am.zRec = (char*)u.am.pC->aRow; |
| 65715 | 65588 | }else if( u.am.pC->isIndex ){ |
| 65716 | 65589 | assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) ); |
| 65717 | | - rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64); |
| 65590 | + VVA_ONLY(rc =) sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64); |
| 65718 | 65591 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| 65719 | 65592 | /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the |
| 65720 | 65593 | ** payload size, so it is impossible for u.am.payloadSize64 to be |
| 65721 | 65594 | ** larger than 32 bits. */ |
| 65722 | 65595 | assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 ); |
| 65723 | 65596 | u.am.payloadSize = (u32)u.am.payloadSize64; |
| 65724 | 65597 | }else{ |
| 65725 | 65598 | assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) ); |
| 65726 | | - rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize); |
| 65599 | + VVA_ONLY(rc =) sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize); |
| 65727 | 65600 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 65728 | 65601 | } |
| 65729 | 65602 | }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){ |
| 65730 | 65603 | u.am.pReg = &aMem[u.am.pC->pseudoTableReg]; |
| 65731 | 65604 | assert( u.am.pReg->flags & MEM_Blob ); |
| | @@ -67773,18 +67646,18 @@ |
| 67773 | 67646 | rc = sqlite3VdbeCursorMoveto(u.bk.pC); |
| 67774 | 67647 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 67775 | 67648 | |
| 67776 | 67649 | if( u.bk.pC->isIndex ){ |
| 67777 | 67650 | assert( !u.bk.pC->isTable ); |
| 67778 | | - rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64); |
| 67651 | + VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64); |
| 67779 | 67652 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| 67780 | 67653 | if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67781 | 67654 | goto too_big; |
| 67782 | 67655 | } |
| 67783 | 67656 | u.bk.n = (u32)u.bk.n64; |
| 67784 | 67657 | }else{ |
| 67785 | | - rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n); |
| 67658 | + VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n); |
| 67786 | 67659 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 67787 | 67660 | if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67788 | 67661 | goto too_big; |
| 67789 | 67662 | } |
| 67790 | 67663 | } |
| | @@ -69095,11 +68968,11 @@ |
| 69095 | 68968 | |
| 69096 | 68969 | /* Do not allow a transition to journal_mode=WAL for a database |
| 69097 | 68970 | ** in temporary storage or if the VFS does not support shared memory |
| 69098 | 68971 | */ |
| 69099 | 68972 | if( u.ch.eNew==PAGER_JOURNALMODE_WAL |
| 69100 | | - && (u.ch.zFilename[0]==0 /* Temp file */ |
| 68973 | + && (sqlite3Strlen30(u.ch.zFilename)==0 /* Temp file */ |
| 69101 | 68974 | || !sqlite3PagerWalSupported(u.ch.pPager)) /* No shared-memory support */ |
| 69102 | 68975 | ){ |
| 69103 | 68976 | u.ch.eNew = u.ch.eOld; |
| 69104 | 68977 | } |
| 69105 | 68978 | |
| | @@ -69530,14 +69403,19 @@ |
| 69530 | 69403 | u.co.pName = &aMem[pOp->p1]; |
| 69531 | 69404 | assert( u.co.pVtab->pModule->xRename ); |
| 69532 | 69405 | assert( memIsValid(u.co.pName) ); |
| 69533 | 69406 | REGISTER_TRACE(pOp->p1, u.co.pName); |
| 69534 | 69407 | assert( u.co.pName->flags & MEM_Str ); |
| 69535 | | - rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z); |
| 69536 | | - importVtabErrMsg(p, u.co.pVtab); |
| 69537 | | - p->expired = 0; |
| 69538 | | - |
| 69408 | + testcase( u.co.pName->enc==SQLITE_UTF8 ); |
| 69409 | + testcase( u.co.pName->enc==SQLITE_UTF16BE ); |
| 69410 | + testcase( u.co.pName->enc==SQLITE_UTF16LE ); |
| 69411 | + rc = sqlite3VdbeChangeEncoding(u.co.pName, SQLITE_UTF8); |
| 69412 | + if( rc==SQLITE_OK ){ |
| 69413 | + rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z); |
| 69414 | + importVtabErrMsg(p, u.co.pVtab); |
| 69415 | + p->expired = 0; |
| 69416 | + } |
| 69539 | 69417 | break; |
| 69540 | 69418 | } |
| 69541 | 69419 | #endif |
| 69542 | 69420 | |
| 69543 | 69421 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -71891,10 +71769,28 @@ |
| 71891 | 71769 | ExprSetProperty(pExpr, EP_Static); |
| 71892 | 71770 | sqlite3ExprDelete(db, pExpr); |
| 71893 | 71771 | memcpy(pExpr, pDup, sizeof(*pExpr)); |
| 71894 | 71772 | sqlite3DbFree(db, pDup); |
| 71895 | 71773 | } |
| 71774 | + |
| 71775 | + |
| 71776 | +/* |
| 71777 | +** Return TRUE if the name zCol occurs anywhere in the USING clause. |
| 71778 | +** |
| 71779 | +** Return FALSE if the USING clause is NULL or if it does not contain |
| 71780 | +** zCol. |
| 71781 | +*/ |
| 71782 | +static int nameInUsingClause(IdList *pUsing, const char *zCol){ |
| 71783 | + if( pUsing ){ |
| 71784 | + int k; |
| 71785 | + for(k=0; k<pUsing->nId; k++){ |
| 71786 | + if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1; |
| 71787 | + } |
| 71788 | + } |
| 71789 | + return 0; |
| 71790 | +} |
| 71791 | + |
| 71896 | 71792 | |
| 71897 | 71793 | /* |
| 71898 | 71794 | ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up |
| 71899 | 71795 | ** that name in the set of source tables in pSrcList and make the pExpr |
| 71900 | 71796 | ** expression node refer back to that source column. The following changes |
| | @@ -71983,38 +71879,25 @@ |
| 71983 | 71879 | pSchema = pTab->pSchema; |
| 71984 | 71880 | pMatch = pItem; |
| 71985 | 71881 | } |
| 71986 | 71882 | for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ |
| 71987 | 71883 | if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
| 71988 | | - IdList *pUsing; |
| 71884 | + /* If there has been exactly one prior match and this match |
| 71885 | + ** is for the right-hand table of a NATURAL JOIN or is in a |
| 71886 | + ** USING clause, then skip this match. |
| 71887 | + */ |
| 71888 | + if( cnt==1 ){ |
| 71889 | + if( pItem->jointype & JT_NATURAL ) continue; |
| 71890 | + if( nameInUsingClause(pItem->pUsing, zCol) ) continue; |
| 71891 | + } |
| 71989 | 71892 | cnt++; |
| 71990 | 71893 | pExpr->iTable = pItem->iCursor; |
| 71991 | 71894 | pExpr->pTab = pTab; |
| 71992 | 71895 | pMatch = pItem; |
| 71993 | 71896 | pSchema = pTab->pSchema; |
| 71994 | 71897 | /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ |
| 71995 | 71898 | pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; |
| 71996 | | - if( i<pSrcList->nSrc-1 ){ |
| 71997 | | - if( pItem[1].jointype & JT_NATURAL ){ |
| 71998 | | - /* If this match occurred in the left table of a natural join, |
| 71999 | | - ** then skip the right table to avoid a duplicate match */ |
| 72000 | | - pItem++; |
| 72001 | | - i++; |
| 72002 | | - }else if( (pUsing = pItem[1].pUsing)!=0 ){ |
| 72003 | | - /* If this match occurs on a column that is in the USING clause |
| 72004 | | - ** of a join, skip the search of the right table of the join |
| 72005 | | - ** to avoid a duplicate match there. */ |
| 72006 | | - int k; |
| 72007 | | - for(k=0; k<pUsing->nId; k++){ |
| 72008 | | - if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ |
| 72009 | | - pItem++; |
| 72010 | | - i++; |
| 72011 | | - break; |
| 72012 | | - } |
| 72013 | | - } |
| 72014 | | - } |
| 72015 | | - } |
| 72016 | 71899 | break; |
| 72017 | 71900 | } |
| 72018 | 71901 | } |
| 72019 | 71902 | } |
| 72020 | 71903 | } |
| | @@ -73417,11 +73300,12 @@ |
| 73417 | 73300 | pNew->flags |= EP_IntValue; |
| 73418 | 73301 | pNew->u.iValue = iValue; |
| 73419 | 73302 | }else{ |
| 73420 | 73303 | int c; |
| 73421 | 73304 | pNew->u.zToken = (char*)&pNew[1]; |
| 73422 | | - memcpy(pNew->u.zToken, pToken->z, pToken->n); |
| 73305 | + assert( pToken->z!=0 || pToken->n==0 ); |
| 73306 | + if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); |
| 73423 | 73307 | pNew->u.zToken[pToken->n] = 0; |
| 73424 | 73308 | if( dequote && nExtra>=3 |
| 73425 | 73309 | && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){ |
| 73426 | 73310 | sqlite3Dequote(pNew->u.zToken); |
| 73427 | 73311 | if( c=='"' ) pNew->flags |= EP_DblQuoted; |
| | @@ -74456,15 +74340,23 @@ |
| 74456 | 74340 | ** ephemeral table. |
| 74457 | 74341 | */ |
| 74458 | 74342 | p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); |
| 74459 | 74343 | if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ |
| 74460 | 74344 | sqlite3 *db = pParse->db; /* Database connection */ |
| 74461 | | - Expr *pExpr = p->pEList->a[0].pExpr; /* Expression <column> */ |
| 74462 | | - int iCol = pExpr->iColumn; /* Index of column <column> */ |
| 74463 | 74345 | Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ |
| 74464 | | - Table *pTab = p->pSrc->a[0].pTab; /* Table <table>. */ |
| 74346 | + Table *pTab; /* Table <table>. */ |
| 74347 | + Expr *pExpr; /* Expression <column> */ |
| 74348 | + int iCol; /* Index of column <column> */ |
| 74465 | 74349 | int iDb; /* Database idx for pTab */ |
| 74350 | + |
| 74351 | + assert( p ); /* Because of isCandidateForInOpt(p) */ |
| 74352 | + assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 74353 | + assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 74354 | + assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 74355 | + pTab = p->pSrc->a[0].pTab; |
| 74356 | + pExpr = p->pEList->a[0].pExpr; |
| 74357 | + iCol = pExpr->iColumn; |
| 74466 | 74358 | |
| 74467 | 74359 | /* Code an OP_VerifyCookie and OP_TableLock for <table>. */ |
| 74468 | 74360 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 74469 | 74361 | sqlite3CodeVerifySchema(pParse, iDb); |
| 74470 | 74362 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| | @@ -76467,11 +76359,11 @@ |
| 76467 | 76359 | if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){ |
| 76468 | 76360 | return 2; |
| 76469 | 76361 | } |
| 76470 | 76362 | }else if( pA->op!=TK_COLUMN && pA->u.zToken ){ |
| 76471 | 76363 | if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2; |
| 76472 | | - if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
| 76364 | + if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
| 76473 | 76365 | return 2; |
| 76474 | 76366 | } |
| 76475 | 76367 | } |
| 76476 | 76368 | if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1; |
| 76477 | 76369 | if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2; |
| | @@ -77610,10 +77502,112 @@ |
| 77610 | 77502 | ** May you find forgiveness for yourself and forgive others. |
| 77611 | 77503 | ** May you share freely, never taking more than you give. |
| 77612 | 77504 | ** |
| 77613 | 77505 | ************************************************************************* |
| 77614 | 77506 | ** This file contains code associated with the ANALYZE command. |
| 77507 | +** |
| 77508 | +** The ANALYZE command gather statistics about the content of tables |
| 77509 | +** and indices. These statistics are made available to the query planner |
| 77510 | +** to help it make better decisions about how to perform queries. |
| 77511 | +** |
| 77512 | +** The following system tables are or have been supported: |
| 77513 | +** |
| 77514 | +** CREATE TABLE sqlite_stat1(tbl, idx, stat); |
| 77515 | +** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); |
| 77516 | +** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample); |
| 77517 | +** |
| 77518 | +** Additional tables might be added in future releases of SQLite. |
| 77519 | +** The sqlite_stat2 table is not created or used unless the SQLite version |
| 77520 | +** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled |
| 77521 | +** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. |
| 77522 | +** The sqlite_stat2 table is superceded by sqlite_stat3, which is only |
| 77523 | +** created and used by SQLite versions 3.7.9 and later and with |
| 77524 | +** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3 |
| 77525 | +** is a superset of sqlite_stat2. |
| 77526 | +** |
| 77527 | +** Format of sqlite_stat1: |
| 77528 | +** |
| 77529 | +** There is normally one row per index, with the index identified by the |
| 77530 | +** name in the idx column. The tbl column is the name of the table to |
| 77531 | +** which the index belongs. In each such row, the stat column will be |
| 77532 | +** a string consisting of a list of integers. The first integer in this |
| 77533 | +** list is the number of rows in the index and in the table. The second |
| 77534 | +** integer is the average number of rows in the index that have the same |
| 77535 | +** value in the first column of the index. The third integer is the average |
| 77536 | +** number of rows in the index that have the same value for the first two |
| 77537 | +** columns. The N-th integer (for N>1) is the average number of rows in |
| 77538 | +** the index which have the same value for the first N-1 columns. For |
| 77539 | +** a K-column index, there will be K+1 integers in the stat column. If |
| 77540 | +** the index is unique, then the last integer will be 1. |
| 77541 | +** |
| 77542 | +** The list of integers in the stat column can optionally be followed |
| 77543 | +** by the keyword "unordered". The "unordered" keyword, if it is present, |
| 77544 | +** must be separated from the last integer by a single space. If the |
| 77545 | +** "unordered" keyword is present, then the query planner assumes that |
| 77546 | +** the index is unordered and will not use the index for a range query. |
| 77547 | +** |
| 77548 | +** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat |
| 77549 | +** column contains a single integer which is the (estimated) number of |
| 77550 | +** rows in the table identified by sqlite_stat1.tbl. |
| 77551 | +** |
| 77552 | +** Format of sqlite_stat2: |
| 77553 | +** |
| 77554 | +** The sqlite_stat2 is only created and is only used if SQLite is compiled |
| 77555 | +** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between |
| 77556 | +** 3.6.18 and 3.7.8. The "stat2" table contains additional information |
| 77557 | +** about the distribution of keys within an index. The index is identified by |
| 77558 | +** the "idx" column and the "tbl" column is the name of the table to which |
| 77559 | +** the index belongs. There are usually 10 rows in the sqlite_stat2 |
| 77560 | +** table for each index. |
| 77561 | +** |
| 77562 | +** The sqlite_stat2 entries for an index that have sampleno between 0 and 9 |
| 77563 | +** inclusive are samples of the left-most key value in the index taken at |
| 77564 | +** evenly spaced points along the index. Let the number of samples be S |
| 77565 | +** (10 in the standard build) and let C be the number of rows in the index. |
| 77566 | +** Then the sampled rows are given by: |
| 77567 | +** |
| 77568 | +** rownumber = (i*C*2 + C)/(S*2) |
| 77569 | +** |
| 77570 | +** For i between 0 and S-1. Conceptually, the index space is divided into |
| 77571 | +** S uniform buckets and the samples are the middle row from each bucket. |
| 77572 | +** |
| 77573 | +** The format for sqlite_stat2 is recorded here for legacy reference. This |
| 77574 | +** version of SQLite does not support sqlite_stat2. It neither reads nor |
| 77575 | +** writes the sqlite_stat2 table. This version of SQLite only supports |
| 77576 | +** sqlite_stat3. |
| 77577 | +** |
| 77578 | +** Format for sqlite_stat3: |
| 77579 | +** |
| 77580 | +** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is |
| 77581 | +** used to avoid compatibility problems. |
| 77582 | +** |
| 77583 | +** The format of the sqlite_stat3 table is similar to the format of |
| 77584 | +** the sqlite_stat2 table. There are multiple entries for each index. |
| 77585 | +** The idx column names the index and the tbl column is the table of the |
| 77586 | +** index. If the idx and tbl columns are the same, then the sample is |
| 77587 | +** of the INTEGER PRIMARY KEY. The sample column is a value taken from |
| 77588 | +** the left-most column of the index. The nEq column is the approximate |
| 77589 | +** number of entires in the index whose left-most column exactly matches |
| 77590 | +** the sample. nLt is the approximate number of entires whose left-most |
| 77591 | +** column is less than the sample. The nDLt column is the approximate |
| 77592 | +** number of distinct left-most entries in the index that are less than |
| 77593 | +** the sample. |
| 77594 | +** |
| 77595 | +** Future versions of SQLite might change to store a string containing |
| 77596 | +** multiple integers values in the nDLt column of sqlite_stat3. The first |
| 77597 | +** integer will be the number of prior index entires that are distinct in |
| 77598 | +** the left-most column. The second integer will be the number of prior index |
| 77599 | +** entries that are distinct in the first two columns. The third integer |
| 77600 | +** will be the number of prior index entries that are distinct in the first |
| 77601 | +** three columns. And so forth. With that extension, the nDLt field is |
| 77602 | +** similar in function to the sqlite_stat1.stat field. |
| 77603 | +** |
| 77604 | +** There can be an arbitrary number of sqlite_stat3 entries per index. |
| 77605 | +** The ANALYZE command will typically generate sqlite_stat3 tables |
| 77606 | +** that contain between 10 and 40 samples which are distributed across |
| 77607 | +** the key space, though not uniformly, and which include samples with |
| 77608 | +** largest possible nEq values. |
| 77615 | 77609 | */ |
| 77616 | 77610 | #ifndef SQLITE_OMIT_ANALYZE |
| 77617 | 77611 | |
| 77618 | 77612 | /* |
| 77619 | 77613 | ** This routine generates code that opens the sqlite_stat1 table for |
| | @@ -77641,12 +77635,12 @@ |
| 77641 | 77635 | static const struct { |
| 77642 | 77636 | const char *zName; |
| 77643 | 77637 | const char *zCols; |
| 77644 | 77638 | } aTable[] = { |
| 77645 | 77639 | { "sqlite_stat1", "tbl,idx,stat" }, |
| 77646 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 77647 | | - { "sqlite_stat2", "tbl,idx,sampleno,sample" }, |
| 77640 | +#ifdef SQLITE_ENABLE_STAT3 |
| 77641 | + { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" }, |
| 77648 | 77642 | #endif |
| 77649 | 77643 | }; |
| 77650 | 77644 | |
| 77651 | 77645 | int aRoot[] = {0, 0}; |
| 77652 | 77646 | u8 aCreateTbl[] = {0, 0}; |
| | @@ -77658,10 +77652,13 @@ |
| 77658 | 77652 | if( v==0 ) return; |
| 77659 | 77653 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 77660 | 77654 | assert( sqlite3VdbeDb(v)==db ); |
| 77661 | 77655 | pDb = &db->aDb[iDb]; |
| 77662 | 77656 | |
| 77657 | + /* Create new statistic tables if they do not exist, or clear them |
| 77658 | + ** if they do already exist. |
| 77659 | + */ |
| 77663 | 77660 | for(i=0; i<ArraySize(aTable); i++){ |
| 77664 | 77661 | const char *zTab = aTable[i].zName; |
| 77665 | 77662 | Table *pStat; |
| 77666 | 77663 | if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){ |
| 77667 | 77664 | /* The sqlite_stat[12] table does not exist. Create it. Note that a |
| | @@ -77688,17 +77685,237 @@ |
| 77688 | 77685 | sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); |
| 77689 | 77686 | } |
| 77690 | 77687 | } |
| 77691 | 77688 | } |
| 77692 | 77689 | |
| 77693 | | - /* Open the sqlite_stat[12] tables for writing. */ |
| 77690 | + /* Open the sqlite_stat[13] tables for writing. */ |
| 77694 | 77691 | for(i=0; i<ArraySize(aTable); i++){ |
| 77695 | 77692 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb); |
| 77696 | 77693 | sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32); |
| 77697 | 77694 | sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
| 77698 | 77695 | } |
| 77699 | 77696 | } |
| 77697 | + |
| 77698 | +/* |
| 77699 | +** Recommended number of samples for sqlite_stat3 |
| 77700 | +*/ |
| 77701 | +#ifndef SQLITE_STAT3_SAMPLES |
| 77702 | +# define SQLITE_STAT3_SAMPLES 24 |
| 77703 | +#endif |
| 77704 | + |
| 77705 | +/* |
| 77706 | +** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() - |
| 77707 | +** share an instance of the following structure to hold their state |
| 77708 | +** information. |
| 77709 | +*/ |
| 77710 | +typedef struct Stat3Accum Stat3Accum; |
| 77711 | +struct Stat3Accum { |
| 77712 | + tRowcnt nRow; /* Number of rows in the entire table */ |
| 77713 | + tRowcnt nPSample; /* How often to do a periodic sample */ |
| 77714 | + int iMin; /* Index of entry with minimum nEq and hash */ |
| 77715 | + int mxSample; /* Maximum number of samples to accumulate */ |
| 77716 | + int nSample; /* Current number of samples */ |
| 77717 | + u32 iPrn; /* Pseudo-random number used for sampling */ |
| 77718 | + struct Stat3Sample { |
| 77719 | + i64 iRowid; /* Rowid in main table of the key */ |
| 77720 | + tRowcnt nEq; /* sqlite_stat3.nEq */ |
| 77721 | + tRowcnt nLt; /* sqlite_stat3.nLt */ |
| 77722 | + tRowcnt nDLt; /* sqlite_stat3.nDLt */ |
| 77723 | + u8 isPSample; /* True if a periodic sample */ |
| 77724 | + u32 iHash; /* Tiebreaker hash */ |
| 77725 | + } *a; /* An array of samples */ |
| 77726 | +}; |
| 77727 | + |
| 77728 | +#ifdef SQLITE_ENABLE_STAT3 |
| 77729 | +/* |
| 77730 | +** Implementation of the stat3_init(C,S) SQL function. The two parameters |
| 77731 | +** are the number of rows in the table or index (C) and the number of samples |
| 77732 | +** to accumulate (S). |
| 77733 | +** |
| 77734 | +** This routine allocates the Stat3Accum object. |
| 77735 | +** |
| 77736 | +** The return value is the Stat3Accum object (P). |
| 77737 | +*/ |
| 77738 | +static void stat3Init( |
| 77739 | + sqlite3_context *context, |
| 77740 | + int argc, |
| 77741 | + sqlite3_value **argv |
| 77742 | +){ |
| 77743 | + Stat3Accum *p; |
| 77744 | + tRowcnt nRow; |
| 77745 | + int mxSample; |
| 77746 | + int n; |
| 77747 | + |
| 77748 | + UNUSED_PARAMETER(argc); |
| 77749 | + nRow = (tRowcnt)sqlite3_value_int64(argv[0]); |
| 77750 | + mxSample = sqlite3_value_int(argv[1]); |
| 77751 | + n = sizeof(*p) + sizeof(p->a[0])*mxSample; |
| 77752 | + p = sqlite3_malloc( n ); |
| 77753 | + if( p==0 ){ |
| 77754 | + sqlite3_result_error_nomem(context); |
| 77755 | + return; |
| 77756 | + } |
| 77757 | + memset(p, 0, n); |
| 77758 | + p->a = (struct Stat3Sample*)&p[1]; |
| 77759 | + p->nRow = nRow; |
| 77760 | + p->mxSample = mxSample; |
| 77761 | + p->nPSample = p->nRow/(mxSample/3+1) + 1; |
| 77762 | + sqlite3_randomness(sizeof(p->iPrn), &p->iPrn); |
| 77763 | + sqlite3_result_blob(context, p, sizeof(p), sqlite3_free); |
| 77764 | +} |
| 77765 | +static const FuncDef stat3InitFuncdef = { |
| 77766 | + 2, /* nArg */ |
| 77767 | + SQLITE_UTF8, /* iPrefEnc */ |
| 77768 | + 0, /* flags */ |
| 77769 | + 0, /* pUserData */ |
| 77770 | + 0, /* pNext */ |
| 77771 | + stat3Init, /* xFunc */ |
| 77772 | + 0, /* xStep */ |
| 77773 | + 0, /* xFinalize */ |
| 77774 | + "stat3_init", /* zName */ |
| 77775 | + 0, /* pHash */ |
| 77776 | + 0 /* pDestructor */ |
| 77777 | +}; |
| 77778 | + |
| 77779 | + |
| 77780 | +/* |
| 77781 | +** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The |
| 77782 | +** arguments describe a single key instance. This routine makes the |
| 77783 | +** decision about whether or not to retain this key for the sqlite_stat3 |
| 77784 | +** table. |
| 77785 | +** |
| 77786 | +** The return value is NULL. |
| 77787 | +*/ |
| 77788 | +static void stat3Push( |
| 77789 | + sqlite3_context *context, |
| 77790 | + int argc, |
| 77791 | + sqlite3_value **argv |
| 77792 | +){ |
| 77793 | + Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]); |
| 77794 | + tRowcnt nEq = sqlite3_value_int64(argv[0]); |
| 77795 | + tRowcnt nLt = sqlite3_value_int64(argv[1]); |
| 77796 | + tRowcnt nDLt = sqlite3_value_int64(argv[2]); |
| 77797 | + i64 rowid = sqlite3_value_int64(argv[3]); |
| 77798 | + u8 isPSample = 0; |
| 77799 | + u8 doInsert = 0; |
| 77800 | + int iMin = p->iMin; |
| 77801 | + struct Stat3Sample *pSample; |
| 77802 | + int i; |
| 77803 | + u32 h; |
| 77804 | + |
| 77805 | + UNUSED_PARAMETER(context); |
| 77806 | + UNUSED_PARAMETER(argc); |
| 77807 | + if( nEq==0 ) return; |
| 77808 | + h = p->iPrn = p->iPrn*1103515245 + 12345; |
| 77809 | + if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){ |
| 77810 | + doInsert = isPSample = 1; |
| 77811 | + }else if( p->nSample<p->mxSample ){ |
| 77812 | + doInsert = 1; |
| 77813 | + }else{ |
| 77814 | + if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){ |
| 77815 | + doInsert = 1; |
| 77816 | + } |
| 77817 | + } |
| 77818 | + if( !doInsert ) return; |
| 77819 | + if( p->nSample==p->mxSample ){ |
| 77820 | + assert( p->nSample - iMin - 1 >= 0 ); |
| 77821 | + memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1)); |
| 77822 | + pSample = &p->a[p->nSample-1]; |
| 77823 | + }else{ |
| 77824 | + pSample = &p->a[p->nSample++]; |
| 77825 | + } |
| 77826 | + pSample->iRowid = rowid; |
| 77827 | + pSample->nEq = nEq; |
| 77828 | + pSample->nLt = nLt; |
| 77829 | + pSample->nDLt = nDLt; |
| 77830 | + pSample->iHash = h; |
| 77831 | + pSample->isPSample = isPSample; |
| 77832 | + |
| 77833 | + /* Find the new minimum */ |
| 77834 | + if( p->nSample==p->mxSample ){ |
| 77835 | + pSample = p->a; |
| 77836 | + i = 0; |
| 77837 | + while( pSample->isPSample ){ |
| 77838 | + i++; |
| 77839 | + pSample++; |
| 77840 | + assert( i<p->nSample ); |
| 77841 | + } |
| 77842 | + nEq = pSample->nEq; |
| 77843 | + h = pSample->iHash; |
| 77844 | + iMin = i; |
| 77845 | + for(i++, pSample++; i<p->nSample; i++, pSample++){ |
| 77846 | + if( pSample->isPSample ) continue; |
| 77847 | + if( pSample->nEq<nEq |
| 77848 | + || (pSample->nEq==nEq && pSample->iHash<h) |
| 77849 | + ){ |
| 77850 | + iMin = i; |
| 77851 | + nEq = pSample->nEq; |
| 77852 | + h = pSample->iHash; |
| 77853 | + } |
| 77854 | + } |
| 77855 | + p->iMin = iMin; |
| 77856 | + } |
| 77857 | +} |
| 77858 | +static const FuncDef stat3PushFuncdef = { |
| 77859 | + 5, /* nArg */ |
| 77860 | + SQLITE_UTF8, /* iPrefEnc */ |
| 77861 | + 0, /* flags */ |
| 77862 | + 0, /* pUserData */ |
| 77863 | + 0, /* pNext */ |
| 77864 | + stat3Push, /* xFunc */ |
| 77865 | + 0, /* xStep */ |
| 77866 | + 0, /* xFinalize */ |
| 77867 | + "stat3_push", /* zName */ |
| 77868 | + 0, /* pHash */ |
| 77869 | + 0 /* pDestructor */ |
| 77870 | +}; |
| 77871 | + |
| 77872 | +/* |
| 77873 | +** Implementation of the stat3_get(P,N,...) SQL function. This routine is |
| 77874 | +** used to query the results. Content is returned for the Nth sqlite_stat3 |
| 77875 | +** row where N is between 0 and S-1 and S is the number of samples. The |
| 77876 | +** value returned depends on the number of arguments. |
| 77877 | +** |
| 77878 | +** argc==2 result: rowid |
| 77879 | +** argc==3 result: nEq |
| 77880 | +** argc==4 result: nLt |
| 77881 | +** argc==5 result: nDLt |
| 77882 | +*/ |
| 77883 | +static void stat3Get( |
| 77884 | + sqlite3_context *context, |
| 77885 | + int argc, |
| 77886 | + sqlite3_value **argv |
| 77887 | +){ |
| 77888 | + int n = sqlite3_value_int(argv[1]); |
| 77889 | + Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]); |
| 77890 | + |
| 77891 | + assert( p!=0 ); |
| 77892 | + if( p->nSample<=n ) return; |
| 77893 | + switch( argc ){ |
| 77894 | + case 2: sqlite3_result_int64(context, p->a[n].iRowid); break; |
| 77895 | + case 3: sqlite3_result_int64(context, p->a[n].nEq); break; |
| 77896 | + case 4: sqlite3_result_int64(context, p->a[n].nLt); break; |
| 77897 | + default: sqlite3_result_int64(context, p->a[n].nDLt); break; |
| 77898 | + } |
| 77899 | +} |
| 77900 | +static const FuncDef stat3GetFuncdef = { |
| 77901 | + -1, /* nArg */ |
| 77902 | + SQLITE_UTF8, /* iPrefEnc */ |
| 77903 | + 0, /* flags */ |
| 77904 | + 0, /* pUserData */ |
| 77905 | + 0, /* pNext */ |
| 77906 | + stat3Get, /* xFunc */ |
| 77907 | + 0, /* xStep */ |
| 77908 | + 0, /* xFinalize */ |
| 77909 | + "stat3_get", /* zName */ |
| 77910 | + 0, /* pHash */ |
| 77911 | + 0 /* pDestructor */ |
| 77912 | +}; |
| 77913 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 77914 | + |
| 77915 | + |
| 77916 | + |
| 77700 | 77917 | |
| 77701 | 77918 | /* |
| 77702 | 77919 | ** Generate code to do an analysis of all indices associated with |
| 77703 | 77920 | ** a single table. |
| 77704 | 77921 | */ |
| | @@ -77718,24 +77935,31 @@ |
| 77718 | 77935 | int endOfLoop; /* The end of the loop */ |
| 77719 | 77936 | int jZeroRows = -1; /* Jump from here if number of rows is zero */ |
| 77720 | 77937 | int iDb; /* Index of database containing pTab */ |
| 77721 | 77938 | int regTabname = iMem++; /* Register containing table name */ |
| 77722 | 77939 | int regIdxname = iMem++; /* Register containing index name */ |
| 77723 | | - int regSampleno = iMem++; /* Register containing next sample number */ |
| 77724 | | - int regCol = iMem++; /* Content of a column analyzed table */ |
| 77940 | + int regStat1 = iMem++; /* The stat column of sqlite_stat1 */ |
| 77941 | +#ifdef SQLITE_ENABLE_STAT3 |
| 77942 | + int regNumEq = regStat1; /* Number of instances. Same as regStat1 */ |
| 77943 | + int regNumLt = iMem++; /* Number of keys less than regSample */ |
| 77944 | + int regNumDLt = iMem++; /* Number of distinct keys less than regSample */ |
| 77945 | + int regSample = iMem++; /* The next sample value */ |
| 77946 | + int regRowid = regSample; /* Rowid of a sample */ |
| 77947 | + int regAccum = iMem++; /* Register to hold Stat3Accum object */ |
| 77948 | + int regLoop = iMem++; /* Loop counter */ |
| 77949 | + int regCount = iMem++; /* Number of rows in the table or index */ |
| 77950 | + int regTemp1 = iMem++; /* Intermediate register */ |
| 77951 | + int regTemp2 = iMem++; /* Intermediate register */ |
| 77952 | + int once = 1; /* One-time initialization */ |
| 77953 | + int shortJump = 0; /* Instruction address */ |
| 77954 | + int iTabCur = pParse->nTab++; /* Table cursor */ |
| 77955 | +#endif |
| 77956 | + int regCol = iMem++; /* Content of a column in analyzed table */ |
| 77725 | 77957 | int regRec = iMem++; /* Register holding completed record */ |
| 77726 | 77958 | int regTemp = iMem++; /* Temporary use register */ |
| 77727 | | - int regRowid = iMem++; /* Rowid for the inserted record */ |
| 77728 | | - |
| 77729 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 77730 | | - int addr = 0; /* Instruction address */ |
| 77731 | | - int regTemp2 = iMem++; /* Temporary use register */ |
| 77732 | | - int regSamplerecno = iMem++; /* Index of next sample to record */ |
| 77733 | | - int regRecno = iMem++; /* Current sample index */ |
| 77734 | | - int regLast = iMem++; /* Index of last sample to record */ |
| 77735 | | - int regFirst = iMem++; /* Index of first sample to record */ |
| 77736 | | -#endif |
| 77959 | + int regNewRowid = iMem++; /* Rowid for the inserted record */ |
| 77960 | + |
| 77737 | 77961 | |
| 77738 | 77962 | v = sqlite3GetVdbe(pParse); |
| 77739 | 77963 | if( v==0 || NEVER(pTab==0) ){ |
| 77740 | 77964 | return; |
| 77741 | 77965 | } |
| | @@ -77764,13 +77988,18 @@ |
| 77764 | 77988 | iIdxCur = pParse->nTab++; |
| 77765 | 77989 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); |
| 77766 | 77990 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 77767 | 77991 | int nCol; |
| 77768 | 77992 | KeyInfo *pKey; |
| 77993 | + int addrIfNot = 0; /* address of OP_IfNot */ |
| 77994 | + int *aChngAddr; /* Array of jump instruction addresses */ |
| 77769 | 77995 | |
| 77770 | 77996 | if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; |
| 77997 | + VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName)); |
| 77771 | 77998 | nCol = pIdx->nColumn; |
| 77999 | + aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol); |
| 78000 | + if( aChngAddr==0 ) continue; |
| 77772 | 78001 | pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 77773 | 78002 | if( iMem+1+(nCol*2)>pParse->nMem ){ |
| 77774 | 78003 | pParse->nMem = iMem+1+(nCol*2); |
| 77775 | 78004 | } |
| 77776 | 78005 | |
| | @@ -77781,35 +78010,24 @@ |
| 77781 | 78010 | VdbeComment((v, "%s", pIdx->zName)); |
| 77782 | 78011 | |
| 77783 | 78012 | /* Populate the register containing the index name. */ |
| 77784 | 78013 | sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); |
| 77785 | 78014 | |
| 77786 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 77787 | | - |
| 77788 | | - /* If this iteration of the loop is generating code to analyze the |
| 77789 | | - ** first index in the pTab->pIndex list, then register regLast has |
| 77790 | | - ** not been populated. In this case populate it now. */ |
| 77791 | | - if( pTab->pIndex==pIdx ){ |
| 77792 | | - sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno); |
| 77793 | | - sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp); |
| 77794 | | - sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2); |
| 77795 | | - |
| 77796 | | - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast); |
| 77797 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst); |
| 77798 | | - addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast); |
| 77799 | | - sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst); |
| 77800 | | - sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast); |
| 77801 | | - sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2); |
| 77802 | | - sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast); |
| 77803 | | - sqlite3VdbeJumpHere(v, addr); |
| 77804 | | - } |
| 77805 | | - |
| 77806 | | - /* Zero the regSampleno and regRecno registers. */ |
| 77807 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno); |
| 77808 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno); |
| 77809 | | - sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno); |
| 77810 | | -#endif |
| 78015 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78016 | + if( once ){ |
| 78017 | + once = 0; |
| 78018 | + sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); |
| 78019 | + } |
| 78020 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount); |
| 78021 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1); |
| 78022 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq); |
| 78023 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt); |
| 78024 | + sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt); |
| 78025 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum, |
| 78026 | + (char*)&stat3InitFuncdef, P4_FUNCDEF); |
| 78027 | + sqlite3VdbeChangeP5(v, 2); |
| 78028 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 77811 | 78029 | |
| 77812 | 78030 | /* The block of memory cells initialized here is used as follows. |
| 77813 | 78031 | ** |
| 77814 | 78032 | ** iMem: |
| 77815 | 78033 | ** The total number of rows in the table. |
| | @@ -77835,79 +78053,87 @@ |
| 77835 | 78053 | /* Start the analysis loop. This loop runs through all the entries in |
| 77836 | 78054 | ** the index b-tree. */ |
| 77837 | 78055 | endOfLoop = sqlite3VdbeMakeLabel(v); |
| 77838 | 78056 | sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop); |
| 77839 | 78057 | topOfLoop = sqlite3VdbeCurrentAddr(v); |
| 77840 | | - sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); |
| 78058 | + sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */ |
| 77841 | 78059 | |
| 77842 | 78060 | for(i=0; i<nCol; i++){ |
| 77843 | 78061 | CollSeq *pColl; |
| 77844 | 78062 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol); |
| 77845 | 78063 | if( i==0 ){ |
| 77846 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 77847 | | - /* Check if the record that cursor iIdxCur points to contains a |
| 77848 | | - ** value that should be stored in the sqlite_stat2 table. If so, |
| 77849 | | - ** store it. */ |
| 77850 | | - int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno); |
| 77851 | | - assert( regTabname+1==regIdxname |
| 77852 | | - && regTabname+2==regSampleno |
| 77853 | | - && regTabname+3==regCol |
| 77854 | | - ); |
| 77855 | | - sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); |
| 77856 | | - sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0); |
| 77857 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid); |
| 77858 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid); |
| 77859 | | - |
| 77860 | | - /* Calculate new values for regSamplerecno and regSampleno. |
| 77861 | | - ** |
| 77862 | | - ** sampleno = sampleno + 1 |
| 77863 | | - ** samplerecno = samplerecno+(remaining records)/(remaining samples) |
| 77864 | | - */ |
| 77865 | | - sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1); |
| 77866 | | - sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp); |
| 77867 | | - sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 77868 | | - sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2); |
| 77869 | | - sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2); |
| 77870 | | - sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp); |
| 77871 | | - sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno); |
| 77872 | | - |
| 77873 | | - sqlite3VdbeJumpHere(v, ne); |
| 77874 | | - sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1); |
| 77875 | | -#endif |
| 77876 | | - |
| 77877 | 78064 | /* Always record the very first row */ |
| 77878 | | - sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 78065 | + addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 77879 | 78066 | } |
| 77880 | 78067 | assert( pIdx->azColl!=0 ); |
| 77881 | 78068 | assert( pIdx->azColl[i]!=0 ); |
| 77882 | 78069 | pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 77883 | | - sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 77884 | | - (char*)pColl, P4_COLLSEQ); |
| 78070 | + aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 78071 | + (char*)pColl, P4_COLLSEQ); |
| 77885 | 78072 | sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
| 77886 | | - } |
| 77887 | | - if( db->mallocFailed ){ |
| 77888 | | - /* If a malloc failure has occurred, then the result of the expression |
| 77889 | | - ** passed as the second argument to the call to sqlite3VdbeJumpHere() |
| 77890 | | - ** below may be negative. Which causes an assert() to fail (or an |
| 77891 | | - ** out-of-bounds write if SQLITE_DEBUG is not defined). */ |
| 77892 | | - return; |
| 78073 | + VdbeComment((v, "jump if column %d changed", i)); |
| 78074 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78075 | + if( i==0 ){ |
| 78076 | + sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1); |
| 78077 | + VdbeComment((v, "incr repeat count")); |
| 78078 | + } |
| 78079 | +#endif |
| 77893 | 78080 | } |
| 77894 | 78081 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop); |
| 77895 | 78082 | for(i=0; i<nCol; i++){ |
| 77896 | | - int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2); |
| 78083 | + sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */ |
| 77897 | 78084 | if( i==0 ){ |
| 77898 | | - sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */ |
| 78085 | + sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */ |
| 78086 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78087 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, |
| 78088 | + (char*)&stat3PushFuncdef, P4_FUNCDEF); |
| 78089 | + sqlite3VdbeChangeP5(v, 5); |
| 78090 | + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid); |
| 78091 | + sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt); |
| 78092 | + sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1); |
| 78093 | + sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq); |
| 78094 | +#endif |
| 77899 | 78095 | } |
| 77900 | | - sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */ |
| 77901 | 78096 | sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1); |
| 77902 | 78097 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1); |
| 77903 | 78098 | } |
| 78099 | + sqlite3DbFree(db, aChngAddr); |
| 77904 | 78100 | |
| 77905 | | - /* End of the analysis loop. */ |
| 78101 | + /* Always jump here after updating the iMem+1...iMem+1+nCol counters */ |
| 77906 | 78102 | sqlite3VdbeResolveLabel(v, endOfLoop); |
| 78103 | + |
| 77907 | 78104 | sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop); |
| 77908 | 78105 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 78106 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78107 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, |
| 78108 | + (char*)&stat3PushFuncdef, P4_FUNCDEF); |
| 78109 | + sqlite3VdbeChangeP5(v, 5); |
| 78110 | + sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop); |
| 78111 | + shortJump = |
| 78112 | + sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1); |
| 78113 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1, |
| 78114 | + (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 78115 | + sqlite3VdbeChangeP5(v, 2); |
| 78116 | + sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1); |
| 78117 | + sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1); |
| 78118 | + sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample); |
| 78119 | + sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample); |
| 78120 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq, |
| 78121 | + (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 78122 | + sqlite3VdbeChangeP5(v, 3); |
| 78123 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt, |
| 78124 | + (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 78125 | + sqlite3VdbeChangeP5(v, 4); |
| 78126 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt, |
| 78127 | + (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 78128 | + sqlite3VdbeChangeP5(v, 5); |
| 78129 | + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0); |
| 78130 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid); |
| 78131 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid); |
| 78132 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump); |
| 78133 | + sqlite3VdbeJumpHere(v, shortJump+2); |
| 78134 | +#endif |
| 77909 | 78135 | |
| 77910 | 78136 | /* Store the results in sqlite_stat1. |
| 77911 | 78137 | ** |
| 77912 | 78138 | ** The result is a single row of the sqlite_stat1 table. The first |
| 77913 | 78139 | ** two columns are the names of the table and index. The third column |
| | @@ -77923,50 +78149,51 @@ |
| 77923 | 78149 | ** |
| 77924 | 78150 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 77925 | 78151 | ** If K>0 then it is always the case the D>0 so division by zero |
| 77926 | 78152 | ** is never possible. |
| 77927 | 78153 | */ |
| 77928 | | - sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); |
| 78154 | + sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1); |
| 77929 | 78155 | if( jZeroRows<0 ){ |
| 77930 | 78156 | jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); |
| 77931 | 78157 | } |
| 77932 | 78158 | for(i=0; i<nCol; i++){ |
| 77933 | 78159 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); |
| 77934 | | - sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 78160 | + sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); |
| 77935 | 78161 | sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); |
| 77936 | 78162 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 77937 | 78163 | sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp); |
| 77938 | 78164 | sqlite3VdbeAddOp1(v, OP_ToInt, regTemp); |
| 77939 | | - sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 78165 | + sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); |
| 77940 | 78166 | } |
| 77941 | 78167 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 77942 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 77943 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 78168 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 78169 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); |
| 77944 | 78170 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 77945 | 78171 | } |
| 77946 | 78172 | |
| 77947 | 78173 | /* If the table has no indices, create a single sqlite_stat1 entry |
| 77948 | 78174 | ** containing NULL as the index name and the row count as the content. |
| 77949 | 78175 | */ |
| 77950 | 78176 | if( pTab->pIndex==0 ){ |
| 77951 | 78177 | sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); |
| 77952 | 78178 | VdbeComment((v, "%s", pTab->zName)); |
| 77953 | | - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno); |
| 78179 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1); |
| 77954 | 78180 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 77955 | | - jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno); |
| 78181 | + jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); |
| 77956 | 78182 | }else{ |
| 77957 | 78183 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 77958 | 78184 | jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto); |
| 77959 | 78185 | } |
| 77960 | 78186 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); |
| 77961 | 78187 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 77962 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 77963 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 78188 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 78189 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); |
| 77964 | 78190 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 77965 | 78191 | if( pParse->nMem<regRec ) pParse->nMem = regRec; |
| 77966 | 78192 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 77967 | 78193 | } |
| 78194 | + |
| 77968 | 78195 | |
| 77969 | 78196 | /* |
| 77970 | 78197 | ** Generate code that will cause the most recent index analysis to |
| 77971 | 78198 | ** be loaded into internal hash tables where is can be used. |
| 77972 | 78199 | */ |
| | @@ -77987,11 +78214,11 @@ |
| 77987 | 78214 | int iStatCur; |
| 77988 | 78215 | int iMem; |
| 77989 | 78216 | |
| 77990 | 78217 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 77991 | 78218 | iStatCur = pParse->nTab; |
| 77992 | | - pParse->nTab += 2; |
| 78219 | + pParse->nTab += 3; |
| 77993 | 78220 | openStatTable(pParse, iDb, iStatCur, 0, 0); |
| 77994 | 78221 | iMem = pParse->nMem+1; |
| 77995 | 78222 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 77996 | 78223 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 77997 | 78224 | Table *pTab = (Table*)sqliteHashData(k); |
| | @@ -78012,11 +78239,11 @@ |
| 78012 | 78239 | assert( pTab!=0 ); |
| 78013 | 78240 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
| 78014 | 78241 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 78015 | 78242 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 78016 | 78243 | iStatCur = pParse->nTab; |
| 78017 | | - pParse->nTab += 2; |
| 78244 | + pParse->nTab += 3; |
| 78018 | 78245 | if( pOnlyIdx ){ |
| 78019 | 78246 | openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); |
| 78020 | 78247 | }else{ |
| 78021 | 78248 | openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); |
| 78022 | 78249 | } |
| | @@ -78117,11 +78344,11 @@ |
| 78117 | 78344 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
| 78118 | 78345 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 78119 | 78346 | Index *pIndex; |
| 78120 | 78347 | Table *pTable; |
| 78121 | 78348 | int i, c, n; |
| 78122 | | - unsigned int v; |
| 78349 | + tRowcnt v; |
| 78123 | 78350 | const char *z; |
| 78124 | 78351 | |
| 78125 | 78352 | assert( argc==3 ); |
| 78126 | 78353 | UNUSED_PARAMETER2(NotUsed, argc); |
| 78127 | 78354 | |
| | @@ -78160,40 +78387,172 @@ |
| 78160 | 78387 | /* |
| 78161 | 78388 | ** If the Index.aSample variable is not NULL, delete the aSample[] array |
| 78162 | 78389 | ** and its contents. |
| 78163 | 78390 | */ |
| 78164 | 78391 | SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ |
| 78165 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 78392 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78166 | 78393 | if( pIdx->aSample ){ |
| 78167 | 78394 | int j; |
| 78168 | | - for(j=0; j<SQLITE_INDEX_SAMPLES; j++){ |
| 78395 | + for(j=0; j<pIdx->nSample; j++){ |
| 78169 | 78396 | IndexSample *p = &pIdx->aSample[j]; |
| 78170 | 78397 | if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ |
| 78171 | 78398 | sqlite3DbFree(db, p->u.z); |
| 78172 | 78399 | } |
| 78173 | 78400 | } |
| 78174 | 78401 | sqlite3DbFree(db, pIdx->aSample); |
| 78402 | + } |
| 78403 | + if( db && db->pnBytesFreed==0 ){ |
| 78404 | + pIdx->nSample = 0; |
| 78405 | + pIdx->aSample = 0; |
| 78175 | 78406 | } |
| 78176 | 78407 | #else |
| 78177 | 78408 | UNUSED_PARAMETER(db); |
| 78178 | 78409 | UNUSED_PARAMETER(pIdx); |
| 78179 | 78410 | #endif |
| 78180 | 78411 | } |
| 78181 | 78412 | |
| 78413 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78182 | 78414 | /* |
| 78183 | | -** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The |
| 78415 | +** Load content from the sqlite_stat3 table into the Index.aSample[] |
| 78416 | +** arrays of all indices. |
| 78417 | +*/ |
| 78418 | +static int loadStat3(sqlite3 *db, const char *zDb){ |
| 78419 | + int rc; /* Result codes from subroutines */ |
| 78420 | + sqlite3_stmt *pStmt = 0; /* An SQL statement being run */ |
| 78421 | + char *zSql; /* Text of the SQL statement */ |
| 78422 | + Index *pPrevIdx = 0; /* Previous index in the loop */ |
| 78423 | + int idx = 0; /* slot in pIdx->aSample[] for next sample */ |
| 78424 | + int eType; /* Datatype of a sample */ |
| 78425 | + IndexSample *pSample; /* A slot in pIdx->aSample[] */ |
| 78426 | + |
| 78427 | + if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){ |
| 78428 | + return SQLITE_OK; |
| 78429 | + } |
| 78430 | + |
| 78431 | + zSql = sqlite3MPrintf(db, |
| 78432 | + "SELECT idx,count(*) FROM %Q.sqlite_stat3" |
| 78433 | + " GROUP BY idx", zDb); |
| 78434 | + if( !zSql ){ |
| 78435 | + return SQLITE_NOMEM; |
| 78436 | + } |
| 78437 | + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 78438 | + sqlite3DbFree(db, zSql); |
| 78439 | + if( rc ) return rc; |
| 78440 | + |
| 78441 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 78442 | + char *zIndex; /* Index name */ |
| 78443 | + Index *pIdx; /* Pointer to the index object */ |
| 78444 | + int nSample; /* Number of samples */ |
| 78445 | + |
| 78446 | + zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 78447 | + if( zIndex==0 ) continue; |
| 78448 | + nSample = sqlite3_column_int(pStmt, 1); |
| 78449 | + pIdx = sqlite3FindIndex(db, zIndex, zDb); |
| 78450 | + if( pIdx==0 ) continue; |
| 78451 | + assert( pIdx->nSample==0 ); |
| 78452 | + pIdx->nSample = nSample; |
| 78453 | + pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) ); |
| 78454 | + pIdx->avgEq = pIdx->aiRowEst[1]; |
| 78455 | + if( pIdx->aSample==0 ){ |
| 78456 | + db->mallocFailed = 1; |
| 78457 | + sqlite3_finalize(pStmt); |
| 78458 | + return SQLITE_NOMEM; |
| 78459 | + } |
| 78460 | + } |
| 78461 | + rc = sqlite3_finalize(pStmt); |
| 78462 | + if( rc ) return rc; |
| 78463 | + |
| 78464 | + zSql = sqlite3MPrintf(db, |
| 78465 | + "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb); |
| 78466 | + if( !zSql ){ |
| 78467 | + return SQLITE_NOMEM; |
| 78468 | + } |
| 78469 | + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 78470 | + sqlite3DbFree(db, zSql); |
| 78471 | + if( rc ) return rc; |
| 78472 | + |
| 78473 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 78474 | + char *zIndex; /* Index name */ |
| 78475 | + Index *pIdx; /* Pointer to the index object */ |
| 78476 | + int i; /* Loop counter */ |
| 78477 | + tRowcnt sumEq; /* Sum of the nEq values */ |
| 78478 | + |
| 78479 | + zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 78480 | + if( zIndex==0 ) continue; |
| 78481 | + pIdx = sqlite3FindIndex(db, zIndex, zDb); |
| 78482 | + if( pIdx==0 ) continue; |
| 78483 | + if( pIdx==pPrevIdx ){ |
| 78484 | + idx++; |
| 78485 | + }else{ |
| 78486 | + pPrevIdx = pIdx; |
| 78487 | + idx = 0; |
| 78488 | + } |
| 78489 | + assert( idx<pIdx->nSample ); |
| 78490 | + pSample = &pIdx->aSample[idx]; |
| 78491 | + pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1); |
| 78492 | + pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2); |
| 78493 | + pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3); |
| 78494 | + if( idx==pIdx->nSample-1 ){ |
| 78495 | + if( pSample->nDLt>0 ){ |
| 78496 | + for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq; |
| 78497 | + pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt; |
| 78498 | + } |
| 78499 | + if( pIdx->avgEq<=0 ) pIdx->avgEq = 1; |
| 78500 | + } |
| 78501 | + eType = sqlite3_column_type(pStmt, 4); |
| 78502 | + pSample->eType = (u8)eType; |
| 78503 | + switch( eType ){ |
| 78504 | + case SQLITE_INTEGER: { |
| 78505 | + pSample->u.i = sqlite3_column_int64(pStmt, 4); |
| 78506 | + break; |
| 78507 | + } |
| 78508 | + case SQLITE_FLOAT: { |
| 78509 | + pSample->u.r = sqlite3_column_double(pStmt, 4); |
| 78510 | + break; |
| 78511 | + } |
| 78512 | + case SQLITE_NULL: { |
| 78513 | + break; |
| 78514 | + } |
| 78515 | + default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); { |
| 78516 | + const char *z = (const char *)( |
| 78517 | + (eType==SQLITE_BLOB) ? |
| 78518 | + sqlite3_column_blob(pStmt, 4): |
| 78519 | + sqlite3_column_text(pStmt, 4) |
| 78520 | + ); |
| 78521 | + int n = z ? sqlite3_column_bytes(pStmt, 4) : 0; |
| 78522 | + pSample->nByte = n; |
| 78523 | + if( n < 1){ |
| 78524 | + pSample->u.z = 0; |
| 78525 | + }else{ |
| 78526 | + pSample->u.z = sqlite3Malloc(n); |
| 78527 | + if( pSample->u.z==0 ){ |
| 78528 | + db->mallocFailed = 1; |
| 78529 | + sqlite3_finalize(pStmt); |
| 78530 | + return SQLITE_NOMEM; |
| 78531 | + } |
| 78532 | + memcpy(pSample->u.z, z, n); |
| 78533 | + } |
| 78534 | + } |
| 78535 | + } |
| 78536 | + } |
| 78537 | + return sqlite3_finalize(pStmt); |
| 78538 | +} |
| 78539 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 78540 | + |
| 78541 | +/* |
| 78542 | +** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The |
| 78184 | 78543 | ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] |
| 78185 | | -** arrays. The contents of sqlite_stat2 are used to populate the |
| 78544 | +** arrays. The contents of sqlite_stat3 are used to populate the |
| 78186 | 78545 | ** Index.aSample[] arrays. |
| 78187 | 78546 | ** |
| 78188 | 78547 | ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR |
| 78189 | | -** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined |
| 78190 | | -** during compilation and the sqlite_stat2 table is present, no data is |
| 78548 | +** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined |
| 78549 | +** during compilation and the sqlite_stat3 table is present, no data is |
| 78191 | 78550 | ** read from it. |
| 78192 | 78551 | ** |
| 78193 | | -** If SQLITE_ENABLE_STAT2 was defined during compilation and the |
| 78194 | | -** sqlite_stat2 table is not present in the database, SQLITE_ERROR is |
| 78552 | +** If SQLITE_ENABLE_STAT3 was defined during compilation and the |
| 78553 | +** sqlite_stat3 table is not present in the database, SQLITE_ERROR is |
| 78195 | 78554 | ** returned. However, in this case, data is read from the sqlite_stat1 |
| 78196 | 78555 | ** table (if it is present) before returning. |
| 78197 | 78556 | ** |
| 78198 | 78557 | ** If an OOM error occurs, this function always sets db->mallocFailed. |
| 78199 | 78558 | ** This means if the caller does not care about other errors, the return |
| | @@ -78211,12 +78570,14 @@ |
| 78211 | 78570 | /* Clear any prior statistics */ |
| 78212 | 78571 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 78213 | 78572 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
| 78214 | 78573 | Index *pIdx = sqliteHashData(i); |
| 78215 | 78574 | sqlite3DefaultRowEst(pIdx); |
| 78575 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78216 | 78576 | sqlite3DeleteIndexSamples(db, pIdx); |
| 78217 | 78577 | pIdx->aSample = 0; |
| 78578 | +#endif |
| 78218 | 78579 | } |
| 78219 | 78580 | |
| 78220 | 78581 | /* Check to make sure the sqlite_stat1 table exists */ |
| 78221 | 78582 | sInfo.db = db; |
| 78222 | 78583 | sInfo.zDatabase = db->aDb[iDb].zName; |
| | @@ -78224,91 +78585,23 @@ |
| 78224 | 78585 | return SQLITE_ERROR; |
| 78225 | 78586 | } |
| 78226 | 78587 | |
| 78227 | 78588 | /* Load new statistics out of the sqlite_stat1 table */ |
| 78228 | 78589 | zSql = sqlite3MPrintf(db, |
| 78229 | | - "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 78590 | + "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 78230 | 78591 | if( zSql==0 ){ |
| 78231 | 78592 | rc = SQLITE_NOMEM; |
| 78232 | 78593 | }else{ |
| 78233 | 78594 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
| 78234 | 78595 | sqlite3DbFree(db, zSql); |
| 78235 | 78596 | } |
| 78236 | 78597 | |
| 78237 | 78598 | |
| 78238 | | - /* Load the statistics from the sqlite_stat2 table. */ |
| 78239 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 78240 | | - if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){ |
| 78241 | | - rc = SQLITE_ERROR; |
| 78242 | | - } |
| 78243 | | - if( rc==SQLITE_OK ){ |
| 78244 | | - sqlite3_stmt *pStmt = 0; |
| 78245 | | - |
| 78246 | | - zSql = sqlite3MPrintf(db, |
| 78247 | | - "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase); |
| 78248 | | - if( !zSql ){ |
| 78249 | | - rc = SQLITE_NOMEM; |
| 78250 | | - }else{ |
| 78251 | | - rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 78252 | | - sqlite3DbFree(db, zSql); |
| 78253 | | - } |
| 78254 | | - |
| 78255 | | - if( rc==SQLITE_OK ){ |
| 78256 | | - while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 78257 | | - char *zIndex; /* Index name */ |
| 78258 | | - Index *pIdx; /* Pointer to the index object */ |
| 78259 | | - |
| 78260 | | - zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 78261 | | - pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0; |
| 78262 | | - if( pIdx ){ |
| 78263 | | - int iSample = sqlite3_column_int(pStmt, 1); |
| 78264 | | - if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){ |
| 78265 | | - int eType = sqlite3_column_type(pStmt, 2); |
| 78266 | | - |
| 78267 | | - if( pIdx->aSample==0 ){ |
| 78268 | | - static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES; |
| 78269 | | - pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz); |
| 78270 | | - if( pIdx->aSample==0 ){ |
| 78271 | | - db->mallocFailed = 1; |
| 78272 | | - break; |
| 78273 | | - } |
| 78274 | | - memset(pIdx->aSample, 0, sz); |
| 78275 | | - } |
| 78276 | | - |
| 78277 | | - assert( pIdx->aSample ); |
| 78278 | | - { |
| 78279 | | - IndexSample *pSample = &pIdx->aSample[iSample]; |
| 78280 | | - pSample->eType = (u8)eType; |
| 78281 | | - if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ |
| 78282 | | - pSample->u.r = sqlite3_column_double(pStmt, 2); |
| 78283 | | - }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ |
| 78284 | | - const char *z = (const char *)( |
| 78285 | | - (eType==SQLITE_BLOB) ? |
| 78286 | | - sqlite3_column_blob(pStmt, 2): |
| 78287 | | - sqlite3_column_text(pStmt, 2) |
| 78288 | | - ); |
| 78289 | | - int n = sqlite3_column_bytes(pStmt, 2); |
| 78290 | | - if( n>24 ){ |
| 78291 | | - n = 24; |
| 78292 | | - } |
| 78293 | | - pSample->nByte = (u8)n; |
| 78294 | | - if( n < 1){ |
| 78295 | | - pSample->u.z = 0; |
| 78296 | | - }else{ |
| 78297 | | - pSample->u.z = sqlite3DbStrNDup(0, z, n); |
| 78298 | | - if( pSample->u.z==0 ){ |
| 78299 | | - db->mallocFailed = 1; |
| 78300 | | - break; |
| 78301 | | - } |
| 78302 | | - } |
| 78303 | | - } |
| 78304 | | - } |
| 78305 | | - } |
| 78306 | | - } |
| 78307 | | - } |
| 78308 | | - rc = sqlite3_finalize(pStmt); |
| 78309 | | - } |
| 78599 | + /* Load the statistics from the sqlite_stat3 table. */ |
| 78600 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78601 | + if( rc==SQLITE_OK ){ |
| 78602 | + rc = loadStat3(db, sInfo.zDatabase); |
| 78310 | 78603 | } |
| 78311 | 78604 | #endif |
| 78312 | 78605 | |
| 78313 | 78606 | if( rc==SQLITE_NOMEM ){ |
| 78314 | 78607 | db->mallocFailed = 1; |
| | @@ -81120,11 +81413,15 @@ |
| 81120 | 81413 | Parse *pParse, /* The parsing context */ |
| 81121 | 81414 | int iDb, /* The database number */ |
| 81122 | 81415 | const char *zType, /* "idx" or "tbl" */ |
| 81123 | 81416 | const char *zName /* Name of index or table */ |
| 81124 | 81417 | ){ |
| 81125 | | - static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" }; |
| 81418 | + static const char *azStatTab[] = { |
| 81419 | + "sqlite_stat1", |
| 81420 | + "sqlite_stat2", |
| 81421 | + "sqlite_stat3", |
| 81422 | + }; |
| 81126 | 81423 | int i; |
| 81127 | 81424 | const char *zDbName = pParse->db->aDb[iDb].zName; |
| 81128 | 81425 | for(i=0; i<ArraySize(azStatTab); i++){ |
| 81129 | 81426 | if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){ |
| 81130 | 81427 | sqlite3NestedParse(pParse, |
| | @@ -81132,10 +81429,80 @@ |
| 81132 | 81429 | zDbName, azStatTab[i], zType, zName |
| 81133 | 81430 | ); |
| 81134 | 81431 | } |
| 81135 | 81432 | } |
| 81136 | 81433 | } |
| 81434 | + |
| 81435 | +/* |
| 81436 | +** Generate code to drop a table. |
| 81437 | +*/ |
| 81438 | +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ |
| 81439 | + Vdbe *v; |
| 81440 | + sqlite3 *db = pParse->db; |
| 81441 | + Trigger *pTrigger; |
| 81442 | + Db *pDb = &db->aDb[iDb]; |
| 81443 | + |
| 81444 | + v = sqlite3GetVdbe(pParse); |
| 81445 | + assert( v!=0 ); |
| 81446 | + sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 81447 | + |
| 81448 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 81449 | + if( IsVirtual(pTab) ){ |
| 81450 | + sqlite3VdbeAddOp0(v, OP_VBegin); |
| 81451 | + } |
| 81452 | +#endif |
| 81453 | + |
| 81454 | + /* Drop all triggers associated with the table being dropped. Code |
| 81455 | + ** is generated to remove entries from sqlite_master and/or |
| 81456 | + ** sqlite_temp_master if required. |
| 81457 | + */ |
| 81458 | + pTrigger = sqlite3TriggerList(pParse, pTab); |
| 81459 | + while( pTrigger ){ |
| 81460 | + assert( pTrigger->pSchema==pTab->pSchema || |
| 81461 | + pTrigger->pSchema==db->aDb[1].pSchema ); |
| 81462 | + sqlite3DropTriggerPtr(pParse, pTrigger); |
| 81463 | + pTrigger = pTrigger->pNext; |
| 81464 | + } |
| 81465 | + |
| 81466 | +#ifndef SQLITE_OMIT_AUTOINCREMENT |
| 81467 | + /* Remove any entries of the sqlite_sequence table associated with |
| 81468 | + ** the table being dropped. This is done before the table is dropped |
| 81469 | + ** at the btree level, in case the sqlite_sequence table needs to |
| 81470 | + ** move as a result of the drop (can happen in auto-vacuum mode). |
| 81471 | + */ |
| 81472 | + if( pTab->tabFlags & TF_Autoincrement ){ |
| 81473 | + sqlite3NestedParse(pParse, |
| 81474 | + "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
| 81475 | + pDb->zName, pTab->zName |
| 81476 | + ); |
| 81477 | + } |
| 81478 | +#endif |
| 81479 | + |
| 81480 | + /* Drop all SQLITE_MASTER table and index entries that refer to the |
| 81481 | + ** table. The program name loops through the master table and deletes |
| 81482 | + ** every row that refers to a table of the same name as the one being |
| 81483 | + ** dropped. Triggers are handled seperately because a trigger can be |
| 81484 | + ** created in the temp database that refers to a table in another |
| 81485 | + ** database. |
| 81486 | + */ |
| 81487 | + sqlite3NestedParse(pParse, |
| 81488 | + "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
| 81489 | + pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); |
| 81490 | + if( !isView && !IsVirtual(pTab) ){ |
| 81491 | + destroyTable(pParse, pTab); |
| 81492 | + } |
| 81493 | + |
| 81494 | + /* Remove the table entry from SQLite's internal schema and modify |
| 81495 | + ** the schema cookie. |
| 81496 | + */ |
| 81497 | + if( IsVirtual(pTab) ){ |
| 81498 | + sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
| 81499 | + } |
| 81500 | + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 81501 | + sqlite3ChangeCookie(pParse, iDb); |
| 81502 | + sqliteViewResetAll(db, iDb); |
| 81503 | +} |
| 81137 | 81504 | |
| 81138 | 81505 | /* |
| 81139 | 81506 | ** This routine is called to do the work of a DROP TABLE statement. |
| 81140 | 81507 | ** pName is the name of the table to be dropped. |
| 81141 | 81508 | */ |
| | @@ -81201,11 +81568,12 @@ |
| 81201 | 81568 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 81202 | 81569 | goto exit_drop_table; |
| 81203 | 81570 | } |
| 81204 | 81571 | } |
| 81205 | 81572 | #endif |
| 81206 | | - if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 81573 | + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
| 81574 | + && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){ |
| 81207 | 81575 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
| 81208 | 81576 | goto exit_drop_table; |
| 81209 | 81577 | } |
| 81210 | 81578 | |
| 81211 | 81579 | #ifndef SQLITE_OMIT_VIEW |
| | @@ -81225,72 +81593,15 @@ |
| 81225 | 81593 | /* Generate code to remove the table from the master table |
| 81226 | 81594 | ** on disk. |
| 81227 | 81595 | */ |
| 81228 | 81596 | v = sqlite3GetVdbe(pParse); |
| 81229 | 81597 | if( v ){ |
| 81230 | | - Trigger *pTrigger; |
| 81231 | | - Db *pDb = &db->aDb[iDb]; |
| 81232 | 81598 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 81233 | | - |
| 81234 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 81235 | | - if( IsVirtual(pTab) ){ |
| 81236 | | - sqlite3VdbeAddOp0(v, OP_VBegin); |
| 81237 | | - } |
| 81238 | | -#endif |
| 81599 | + sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 81239 | 81600 | sqlite3FkDropTable(pParse, pName, pTab); |
| 81240 | | - |
| 81241 | | - /* Drop all triggers associated with the table being dropped. Code |
| 81242 | | - ** is generated to remove entries from sqlite_master and/or |
| 81243 | | - ** sqlite_temp_master if required. |
| 81244 | | - */ |
| 81245 | | - pTrigger = sqlite3TriggerList(pParse, pTab); |
| 81246 | | - while( pTrigger ){ |
| 81247 | | - assert( pTrigger->pSchema==pTab->pSchema || |
| 81248 | | - pTrigger->pSchema==db->aDb[1].pSchema ); |
| 81249 | | - sqlite3DropTriggerPtr(pParse, pTrigger); |
| 81250 | | - pTrigger = pTrigger->pNext; |
| 81251 | | - } |
| 81252 | | - |
| 81253 | | -#ifndef SQLITE_OMIT_AUTOINCREMENT |
| 81254 | | - /* Remove any entries of the sqlite_sequence table associated with |
| 81255 | | - ** the table being dropped. This is done before the table is dropped |
| 81256 | | - ** at the btree level, in case the sqlite_sequence table needs to |
| 81257 | | - ** move as a result of the drop (can happen in auto-vacuum mode). |
| 81258 | | - */ |
| 81259 | | - if( pTab->tabFlags & TF_Autoincrement ){ |
| 81260 | | - sqlite3NestedParse(pParse, |
| 81261 | | - "DELETE FROM %s.sqlite_sequence WHERE name=%Q", |
| 81262 | | - pDb->zName, pTab->zName |
| 81263 | | - ); |
| 81264 | | - } |
| 81265 | | -#endif |
| 81266 | | - |
| 81267 | | - /* Drop all SQLITE_MASTER table and index entries that refer to the |
| 81268 | | - ** table. The program name loops through the master table and deletes |
| 81269 | | - ** every row that refers to a table of the same name as the one being |
| 81270 | | - ** dropped. Triggers are handled seperately because a trigger can be |
| 81271 | | - ** created in the temp database that refers to a table in another |
| 81272 | | - ** database. |
| 81273 | | - */ |
| 81274 | | - sqlite3NestedParse(pParse, |
| 81275 | | - "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
| 81276 | | - pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); |
| 81277 | | - sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 81278 | | - if( !isView && !IsVirtual(pTab) ){ |
| 81279 | | - destroyTable(pParse, pTab); |
| 81280 | | - } |
| 81281 | | - |
| 81282 | | - /* Remove the table entry from SQLite's internal schema and modify |
| 81283 | | - ** the schema cookie. |
| 81284 | | - */ |
| 81285 | | - if( IsVirtual(pTab) ){ |
| 81286 | | - sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
| 81287 | | - } |
| 81288 | | - sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 81289 | | - sqlite3ChangeCookie(pParse, iDb); |
| 81290 | | - } |
| 81291 | | - sqliteViewResetAll(db, iDb); |
| 81601 | + sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
| 81602 | + } |
| 81292 | 81603 | |
| 81293 | 81604 | exit_drop_table: |
| 81294 | 81605 | sqlite3SrcListDelete(db, pName); |
| 81295 | 81606 | } |
| 81296 | 81607 | |
| | @@ -81454,17 +81765,19 @@ |
| 81454 | 81765 | */ |
| 81455 | 81766 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 81456 | 81767 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
| 81457 | 81768 | int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
| 81458 | 81769 | int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
| 81459 | | - int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */ |
| 81770 | + int iSorter; /* Cursor opened by OpenSorter (if in use) */ |
| 81460 | 81771 | int addr1; /* Address of top of loop */ |
| 81461 | 81772 | int addr2; /* Address to jump to for next iteration */ |
| 81462 | 81773 | int tnum; /* Root page of index */ |
| 81463 | 81774 | Vdbe *v; /* Generate code into this virtual machine */ |
| 81464 | 81775 | KeyInfo *pKey; /* KeyInfo for index */ |
| 81776 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 81465 | 81777 | int regIdxKey; /* Registers containing the index key */ |
| 81778 | +#endif |
| 81466 | 81779 | int regRecord; /* Register holding assemblied index record */ |
| 81467 | 81780 | sqlite3 *db = pParse->db; /* The database connection */ |
| 81468 | 81781 | int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
| 81469 | 81782 | |
| 81470 | 81783 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| | @@ -81494,21 +81807,22 @@ |
| 81494 | 81807 | |
| 81495 | 81808 | #ifndef SQLITE_OMIT_MERGE_SORT |
| 81496 | 81809 | /* Open the sorter cursor if we are to use one. */ |
| 81497 | 81810 | iSorter = pParse->nTab++; |
| 81498 | 81811 | sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO); |
| 81812 | +#else |
| 81813 | + iSorter = iTab; |
| 81499 | 81814 | #endif |
| 81500 | 81815 | |
| 81501 | 81816 | /* Open the table. Loop through all rows of the table, inserting index |
| 81502 | 81817 | ** records into the sorter. */ |
| 81503 | 81818 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 81504 | 81819 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); |
| 81505 | | - addr2 = addr1 + 1; |
| 81506 | 81820 | regRecord = sqlite3GetTempReg(pParse); |
| 81507 | | - regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); |
| 81508 | 81821 | |
| 81509 | 81822 | #ifndef SQLITE_OMIT_MERGE_SORT |
| 81823 | + sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); |
| 81510 | 81824 | sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
| 81511 | 81825 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); |
| 81512 | 81826 | sqlite3VdbeJumpHere(v, addr1); |
| 81513 | 81827 | addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); |
| 81514 | 81828 | if( pIndex->onError!=OE_None ){ |
| | @@ -81524,10 +81838,12 @@ |
| 81524 | 81838 | } |
| 81525 | 81839 | sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord); |
| 81526 | 81840 | sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1); |
| 81527 | 81841 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 81528 | 81842 | #else |
| 81843 | + regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); |
| 81844 | + addr2 = addr1 + 1; |
| 81529 | 81845 | if( pIndex->onError!=OE_None ){ |
| 81530 | 81846 | const int regRowid = regIdxKey + pIndex->nColumn; |
| 81531 | 81847 | const int j2 = sqlite3VdbeCurrentAddr(v) + 2; |
| 81532 | 81848 | void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey); |
| 81533 | 81849 | |
| | @@ -81621,10 +81937,11 @@ |
| 81621 | 81937 | ** before looking up the table. |
| 81622 | 81938 | */ |
| 81623 | 81939 | assert( pName1 && pName2 ); |
| 81624 | 81940 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 81625 | 81941 | if( iDb<0 ) goto exit_create_index; |
| 81942 | + assert( pName && pName->z ); |
| 81626 | 81943 | |
| 81627 | 81944 | #ifndef SQLITE_OMIT_TEMPDB |
| 81628 | 81945 | /* If the index name was unqualified, check if the the table |
| 81629 | 81946 | ** is a temp table. If so, set the database to 1. Do not do this |
| 81630 | 81947 | ** if initialising a database schema. |
| | @@ -81648,10 +81965,11 @@ |
| 81648 | 81965 | pTblName->a[0].zDatabase); |
| 81649 | 81966 | if( !pTab || db->mallocFailed ) goto exit_create_index; |
| 81650 | 81967 | assert( db->aDb[iDb].pSchema==pTab->pSchema ); |
| 81651 | 81968 | }else{ |
| 81652 | 81969 | assert( pName==0 ); |
| 81970 | + assert( pStart==0 ); |
| 81653 | 81971 | pTab = pParse->pNewTable; |
| 81654 | 81972 | if( !pTab ) goto exit_create_index; |
| 81655 | 81973 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 81656 | 81974 | } |
| 81657 | 81975 | pDb = &db->aDb[iDb]; |
| | @@ -81690,10 +82008,11 @@ |
| 81690 | 82008 | ** own name. |
| 81691 | 82009 | */ |
| 81692 | 82010 | if( pName ){ |
| 81693 | 82011 | zName = sqlite3NameFromToken(db, pName); |
| 81694 | 82012 | if( zName==0 ) goto exit_create_index; |
| 82013 | + assert( pName->z!=0 ); |
| 81695 | 82014 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
| 81696 | 82015 | goto exit_create_index; |
| 81697 | 82016 | } |
| 81698 | 82017 | if( !db->init.busy ){ |
| 81699 | 82018 | if( sqlite3FindTable(db, zName, 0)!=0 ){ |
| | @@ -81769,24 +82088,24 @@ |
| 81769 | 82088 | */ |
| 81770 | 82089 | nName = sqlite3Strlen30(zName); |
| 81771 | 82090 | nCol = pList->nExpr; |
| 81772 | 82091 | pIndex = sqlite3DbMallocZero(db, |
| 81773 | 82092 | sizeof(Index) + /* Index structure */ |
| 82093 | + sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */ |
| 81774 | 82094 | sizeof(int)*nCol + /* Index.aiColumn */ |
| 81775 | | - sizeof(int)*(nCol+1) + /* Index.aiRowEst */ |
| 81776 | 82095 | sizeof(char *)*nCol + /* Index.azColl */ |
| 81777 | 82096 | sizeof(u8)*nCol + /* Index.aSortOrder */ |
| 81778 | 82097 | nName + 1 + /* Index.zName */ |
| 81779 | 82098 | nExtra /* Collation sequence names */ |
| 81780 | 82099 | ); |
| 81781 | 82100 | if( db->mallocFailed ){ |
| 81782 | 82101 | goto exit_create_index; |
| 81783 | 82102 | } |
| 81784 | | - pIndex->azColl = (char**)(&pIndex[1]); |
| 82103 | + pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]); |
| 82104 | + pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]); |
| 81785 | 82105 | pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]); |
| 81786 | | - pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]); |
| 81787 | | - pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]); |
| 82106 | + pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]); |
| 81788 | 82107 | pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]); |
| 81789 | 82108 | zExtra = (char *)(&pIndex->zName[nName+1]); |
| 81790 | 82109 | memcpy(pIndex->zName, zName, nName+1); |
| 81791 | 82110 | pIndex->pTable = pTab; |
| 81792 | 82111 | pIndex->nColumn = pList->nExpr; |
| | @@ -82059,13 +82378,13 @@ |
| 82059 | 82378 | ** Apart from that, we have little to go on besides intuition as to |
| 82060 | 82379 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 82061 | 82380 | ** are based on typical values found in actual indices. |
| 82062 | 82381 | */ |
| 82063 | 82382 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ |
| 82064 | | - unsigned *a = pIdx->aiRowEst; |
| 82383 | + tRowcnt *a = pIdx->aiRowEst; |
| 82065 | 82384 | int i; |
| 82066 | | - unsigned n; |
| 82385 | + tRowcnt n; |
| 82067 | 82386 | assert( a!=0 ); |
| 82068 | 82387 | a[0] = pIdx->pTable->nRowEst; |
| 82069 | 82388 | if( a[0]<10 ) a[0] = 10; |
| 82070 | 82389 | n = 10; |
| 82071 | 82390 | for(i=1; i<=pIdx->nColumn; i++){ |
| | @@ -82545,17 +82864,14 @@ |
| 82545 | 82864 | |
| 82546 | 82865 | /* |
| 82547 | 82866 | ** Commit a transaction |
| 82548 | 82867 | */ |
| 82549 | 82868 | SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){ |
| 82550 | | - sqlite3 *db; |
| 82551 | 82869 | Vdbe *v; |
| 82552 | 82870 | |
| 82553 | 82871 | assert( pParse!=0 ); |
| 82554 | | - db = pParse->db; |
| 82555 | | - assert( db!=0 ); |
| 82556 | | -/* if( db->aDb[0].pBt==0 ) return; */ |
| 82872 | + assert( pParse->db!=0 ); |
| 82557 | 82873 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){ |
| 82558 | 82874 | return; |
| 82559 | 82875 | } |
| 82560 | 82876 | v = sqlite3GetVdbe(pParse); |
| 82561 | 82877 | if( v ){ |
| | @@ -82565,17 +82881,14 @@ |
| 82565 | 82881 | |
| 82566 | 82882 | /* |
| 82567 | 82883 | ** Rollback a transaction |
| 82568 | 82884 | */ |
| 82569 | 82885 | SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){ |
| 82570 | | - sqlite3 *db; |
| 82571 | 82886 | Vdbe *v; |
| 82572 | 82887 | |
| 82573 | 82888 | assert( pParse!=0 ); |
| 82574 | | - db = pParse->db; |
| 82575 | | - assert( db!=0 ); |
| 82576 | | -/* if( db->aDb[0].pBt==0 ) return; */ |
| 82889 | + assert( pParse->db!=0 ); |
| 82577 | 82890 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){ |
| 82578 | 82891 | return; |
| 82579 | 82892 | } |
| 82580 | 82893 | v = sqlite3GetVdbe(pParse); |
| 82581 | 82894 | if( v ){ |
| | @@ -84377,20 +84690,19 @@ |
| 84377 | 84690 | /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 84378 | 84691 | assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
| 84379 | 84692 | if( z2 ){ |
| 84380 | 84693 | z1 = contextMalloc(context, ((i64)n)+1); |
| 84381 | 84694 | if( z1 ){ |
| 84382 | | - memcpy(z1, z2, n+1); |
| 84383 | | - for(i=0; z1[i]; i++){ |
| 84384 | | - z1[i] = (char)sqlite3Toupper(z1[i]); |
| 84695 | + for(i=0; i<n; i++){ |
| 84696 | + z1[i] = (char)sqlite3Toupper(z2[i]); |
| 84385 | 84697 | } |
| 84386 | | - sqlite3_result_text(context, z1, -1, sqlite3_free); |
| 84698 | + sqlite3_result_text(context, z1, n, sqlite3_free); |
| 84387 | 84699 | } |
| 84388 | 84700 | } |
| 84389 | 84701 | } |
| 84390 | 84702 | static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 84391 | | - u8 *z1; |
| 84703 | + char *z1; |
| 84392 | 84704 | const char *z2; |
| 84393 | 84705 | int i, n; |
| 84394 | 84706 | UNUSED_PARAMETER(argc); |
| 84395 | 84707 | z2 = (char*)sqlite3_value_text(argv[0]); |
| 84396 | 84708 | n = sqlite3_value_bytes(argv[0]); |
| | @@ -84397,15 +84709,14 @@ |
| 84397 | 84709 | /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 84398 | 84710 | assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
| 84399 | 84711 | if( z2 ){ |
| 84400 | 84712 | z1 = contextMalloc(context, ((i64)n)+1); |
| 84401 | 84713 | if( z1 ){ |
| 84402 | | - memcpy(z1, z2, n+1); |
| 84403 | | - for(i=0; z1[i]; i++){ |
| 84404 | | - z1[i] = sqlite3Tolower(z1[i]); |
| 84714 | + for(i=0; i<n; i++){ |
| 84715 | + z1[i] = sqlite3Tolower(z2[i]); |
| 84405 | 84716 | } |
| 84406 | | - sqlite3_result_text(context, (char *)z1, -1, sqlite3_free); |
| 84717 | + sqlite3_result_text(context, z1, n, sqlite3_free); |
| 84407 | 84718 | } |
| 84408 | 84719 | } |
| 84409 | 84720 | } |
| 84410 | 84721 | |
| 84411 | 84722 | |
| | @@ -86778,10 +87089,11 @@ |
| 86778 | 87089 | sqlite3SelectDelete(db, pSelect); |
| 86779 | 87090 | if( db->mallocFailed==1 ){ |
| 86780 | 87091 | fkTriggerDelete(db, pTrigger); |
| 86781 | 87092 | return 0; |
| 86782 | 87093 | } |
| 87094 | + assert( pStep!=0 ); |
| 86783 | 87095 | |
| 86784 | 87096 | switch( action ){ |
| 86785 | 87097 | case OE_Restrict: |
| 86786 | 87098 | pStep->op = TK_SELECT; |
| 86787 | 87099 | break; |
| | @@ -88621,10 +88933,13 @@ |
| 88621 | 88933 | */ |
| 88622 | 88934 | if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ |
| 88623 | 88935 | return 0; |
| 88624 | 88936 | } |
| 88625 | 88937 | #endif |
| 88938 | + if( (pParse->db->flags & SQLITE_CountRows)!=0 ){ |
| 88939 | + return 0; |
| 88940 | + } |
| 88626 | 88941 | |
| 88627 | 88942 | /* If we get this far, it means either: |
| 88628 | 88943 | ** |
| 88629 | 88944 | ** * We can always do the transfer if the table contains an |
| 88630 | 88945 | ** an integer primary key |
| | @@ -89698,11 +90013,11 @@ |
| 89698 | 90013 | sqlite3_vfs *pVfs = db->pVfs; |
| 89699 | 90014 | void *handle; |
| 89700 | 90015 | int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); |
| 89701 | 90016 | char *zErrmsg = 0; |
| 89702 | 90017 | void **aHandle; |
| 89703 | | - const int nMsg = 300; |
| 90018 | + int nMsg = 300 + sqlite3Strlen30(zFile); |
| 89704 | 90019 | |
| 89705 | 90020 | if( pzErrMsg ) *pzErrMsg = 0; |
| 89706 | 90021 | |
| 89707 | 90022 | /* Ticket #1863. To avoid a creating security problems for older |
| 89708 | 90023 | ** applications that relink against newer versions of SQLite, the |
| | @@ -89735,10 +90050,11 @@ |
| 89735 | 90050 | } |
| 89736 | 90051 | xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) |
| 89737 | 90052 | sqlite3OsDlSym(pVfs, handle, zProc); |
| 89738 | 90053 | if( xInit==0 ){ |
| 89739 | 90054 | if( pzErrMsg ){ |
| 90055 | + nMsg += sqlite3Strlen30(zProc); |
| 89740 | 90056 | *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); |
| 89741 | 90057 | if( zErrmsg ){ |
| 89742 | 90058 | sqlite3_snprintf(nMsg, zErrmsg, |
| 89743 | 90059 | "no entry point [%s] in shared library [%s]", zProc,zFile); |
| 89744 | 90060 | sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| | @@ -90420,11 +90736,11 @@ |
| 90420 | 90736 | ){ |
| 90421 | 90737 | int iReg; |
| 90422 | 90738 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 90423 | 90739 | sqlite3CodeVerifySchema(pParse, iDb); |
| 90424 | 90740 | iReg = ++pParse->nMem; |
| 90425 | | - if( zLeft[0]=='p' ){ |
| 90741 | + if( sqlite3Tolower(zLeft[0])=='p' ){ |
| 90426 | 90742 | sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); |
| 90427 | 90743 | }else{ |
| 90428 | 90744 | sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight)); |
| 90429 | 90745 | } |
| 90430 | 90746 | sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); |
| | @@ -90486,12 +90802,14 @@ |
| 90486 | 90802 | */ |
| 90487 | 90803 | if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){ |
| 90488 | 90804 | int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ |
| 90489 | 90805 | int ii; /* Loop counter */ |
| 90490 | 90806 | |
| 90491 | | - /* Force the schema to be loaded on all databases. This cases all |
| 90492 | | - ** database files to be opened and the journal_modes set. */ |
| 90807 | + /* Force the schema to be loaded on all databases. This causes all |
| 90808 | + ** database files to be opened and the journal_modes set. This is |
| 90809 | + ** necessary because subsequent processing must know if the databases |
| 90810 | + ** are in WAL mode. */ |
| 90493 | 90811 | if( sqlite3ReadSchema(pParse) ){ |
| 90494 | 90812 | goto pragma_out; |
| 90495 | 90813 | } |
| 90496 | 90814 | |
| 90497 | 90815 | sqlite3VdbeSetNumCols(v, 1); |
| | @@ -91031,11 +91349,11 @@ |
| 91031 | 91349 | { OP_IfNeg, 1, 0, 0}, /* 1 */ |
| 91032 | 91350 | { OP_String8, 0, 3, 0}, /* 2 */ |
| 91033 | 91351 | { OP_ResultRow, 3, 1, 0}, |
| 91034 | 91352 | }; |
| 91035 | 91353 | |
| 91036 | | - int isQuick = (zLeft[0]=='q'); |
| 91354 | + int isQuick = (sqlite3Tolower(zLeft[0])=='q'); |
| 91037 | 91355 | |
| 91038 | 91356 | /* Initialize the VDBE program */ |
| 91039 | 91357 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 91040 | 91358 | pParse->nMem = 6; |
| 91041 | 91359 | sqlite3VdbeSetNumCols(v, 1); |
| | @@ -92406,10 +92724,11 @@ |
| 92406 | 92724 | Select standin; |
| 92407 | 92725 | sqlite3 *db = pParse->db; |
| 92408 | 92726 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); |
| 92409 | 92727 | assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */ |
| 92410 | 92728 | if( pNew==0 ){ |
| 92729 | + assert( db->mallocFailed ); |
| 92411 | 92730 | pNew = &standin; |
| 92412 | 92731 | memset(pNew, 0, sizeof(*pNew)); |
| 92413 | 92732 | } |
| 92414 | 92733 | if( pEList==0 ){ |
| 92415 | 92734 | pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0)); |
| | @@ -92433,10 +92752,11 @@ |
| 92433 | 92752 | if( pNew!=&standin ) sqlite3DbFree(db, pNew); |
| 92434 | 92753 | pNew = 0; |
| 92435 | 92754 | }else{ |
| 92436 | 92755 | assert( pNew->pSrc!=0 || pParse->nErr>0 ); |
| 92437 | 92756 | } |
| 92757 | + assert( pNew!=&standin ); |
| 92438 | 92758 | return pNew; |
| 92439 | 92759 | } |
| 92440 | 92760 | |
| 92441 | 92761 | /* |
| 92442 | 92762 | ** Delete the given Select structure and all of its substructures. |
| | @@ -93611,11 +93931,14 @@ |
| 93611 | 93931 | /* If the column contains an "AS <name>" phrase, use <name> as the name */ |
| 93612 | 93932 | zName = sqlite3DbStrDup(db, zName); |
| 93613 | 93933 | }else{ |
| 93614 | 93934 | Expr *pColExpr = p; /* The expression that is the result column name */ |
| 93615 | 93935 | Table *pTab; /* Table associated with this expression */ |
| 93616 | | - while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight; |
| 93936 | + while( pColExpr->op==TK_DOT ){ |
| 93937 | + pColExpr = pColExpr->pRight; |
| 93938 | + assert( pColExpr!=0 ); |
| 93939 | + } |
| 93617 | 93940 | if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){ |
| 93618 | 93941 | /* For columns use the column name name */ |
| 93619 | 93942 | int iCol = pColExpr->iColumn; |
| 93620 | 93943 | pTab = pColExpr->pTab; |
| 93621 | 93944 | if( iCol<0 ) iCol = pTab->iPKey; |
| | @@ -98609,10 +98932,11 @@ |
| 98609 | 98932 | break; |
| 98610 | 98933 | } |
| 98611 | 98934 | } |
| 98612 | 98935 | } |
| 98613 | 98936 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 98937 | + assert( aRegIdx ); |
| 98614 | 98938 | if( openAll || aRegIdx[i]>0 ){ |
| 98615 | 98939 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 98616 | 98940 | sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb, |
| 98617 | 98941 | (char*)pKey, P4_KEYINFO_HANDOFF); |
| 98618 | 98942 | assert( pParse->nTab>iCur+i+1 ); |
| | @@ -98782,10 +99106,11 @@ |
| 98782 | 99106 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); |
| 98783 | 99107 | sqlite3VdbeJumpHere(v, addr); |
| 98784 | 99108 | |
| 98785 | 99109 | /* Close all tables */ |
| 98786 | 99110 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 99111 | + assert( aRegIdx ); |
| 98787 | 99112 | if( openAll || aRegIdx[i]>0 ){ |
| 98788 | 99113 | sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0); |
| 98789 | 99114 | } |
| 98790 | 99115 | } |
| 98791 | 99116 | sqlite3VdbeAddOp2(v, OP_Close, iCur, 0); |
| | @@ -98969,11 +99294,11 @@ |
| 98969 | 99294 | if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ |
| 98970 | 99295 | sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); |
| 98971 | 99296 | return sqlite3_errcode(db); |
| 98972 | 99297 | } |
| 98973 | 99298 | VVA_ONLY( rc = ) sqlite3_step(pStmt); |
| 98974 | | - assert( rc!=SQLITE_ROW ); |
| 99299 | + assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) ); |
| 98975 | 99300 | return vacuumFinalize(db, pStmt, pzErrMsg); |
| 98976 | 99301 | } |
| 98977 | 99302 | |
| 98978 | 99303 | /* |
| 98979 | 99304 | ** Execute zSql on database db. The statement returns exactly |
| | @@ -99187,17 +99512,15 @@ |
| 99187 | 99512 | " WHERE type='view' OR type='trigger'" |
| 99188 | 99513 | " OR (type='table' AND rootpage=0)" |
| 99189 | 99514 | ); |
| 99190 | 99515 | if( rc ) goto end_of_vacuum; |
| 99191 | 99516 | |
| 99192 | | - /* At this point, unless the main db was completely empty, there is now a |
| 99193 | | - ** transaction open on the vacuum database, but not on the main database. |
| 99194 | | - ** Open a btree level transaction on the main database. This allows a |
| 99195 | | - ** call to sqlite3BtreeCopyFile(). The main database btree level |
| 99196 | | - ** transaction is then committed, so the SQL level never knows it was |
| 99197 | | - ** opened for writing. This way, the SQL transaction used to create the |
| 99198 | | - ** temporary database never needs to be committed. |
| 99517 | + /* At this point, there is a write transaction open on both the |
| 99518 | + ** vacuum database and the main database. Assuming no error occurs, |
| 99519 | + ** both transactions are closed by this block - the main database |
| 99520 | + ** transaction by sqlite3BtreeCopyFile() and the other by an explicit |
| 99521 | + ** call to sqlite3BtreeCommit(). |
| 99199 | 99522 | */ |
| 99200 | 99523 | { |
| 99201 | 99524 | u32 meta; |
| 99202 | 99525 | int i; |
| 99203 | 99526 | |
| | @@ -100457,25 +100780,35 @@ |
| 100457 | 100780 | #define TERM_CODED 0x04 /* This term is already coded */ |
| 100458 | 100781 | #define TERM_COPIED 0x08 /* Has a child */ |
| 100459 | 100782 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 100460 | 100783 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 100461 | 100784 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
| 100462 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 100785 | +#ifdef SQLITE_ENABLE_STAT3 |
| 100463 | 100786 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 100464 | 100787 | #else |
| 100465 | | -# define TERM_VNULL 0x00 /* Disabled if not using stat2 */ |
| 100788 | +# define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 100466 | 100789 | #endif |
| 100467 | 100790 | |
| 100468 | 100791 | /* |
| 100469 | 100792 | ** An instance of the following structure holds all information about a |
| 100470 | 100793 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 100794 | +** |
| 100795 | +** Explanation of pOuter: For a WHERE clause of the form |
| 100796 | +** |
| 100797 | +** a AND ((b AND c) OR (d AND e)) AND f |
| 100798 | +** |
| 100799 | +** There are separate WhereClause objects for the whole clause and for |
| 100800 | +** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 100801 | +** subclauses points to the WhereClause object for the whole clause. |
| 100471 | 100802 | */ |
| 100472 | 100803 | struct WhereClause { |
| 100473 | 100804 | Parse *pParse; /* The parser context */ |
| 100474 | 100805 | WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */ |
| 100475 | 100806 | Bitmask vmask; /* Bitmask identifying virtual table cursors */ |
| 100807 | + WhereClause *pOuter; /* Outer conjunction */ |
| 100476 | 100808 | u8 op; /* Split operator. TK_AND or TK_OR */ |
| 100809 | + u16 wctrlFlags; /* Might include WHERE_AND_ONLY */ |
| 100477 | 100810 | int nTerm; /* Number of terms */ |
| 100478 | 100811 | int nSlot; /* Number of entries in a[] */ |
| 100479 | 100812 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 100480 | 100813 | #if defined(SQLITE_SMALL_STACK) |
| 100481 | 100814 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| | @@ -100600,18 +100933,21 @@ |
| 100600 | 100933 | ** Initialize a preallocated WhereClause structure. |
| 100601 | 100934 | */ |
| 100602 | 100935 | static void whereClauseInit( |
| 100603 | 100936 | WhereClause *pWC, /* The WhereClause to be initialized */ |
| 100604 | 100937 | Parse *pParse, /* The parsing context */ |
| 100605 | | - WhereMaskSet *pMaskSet /* Mapping from table cursor numbers to bitmasks */ |
| 100938 | + WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmasks */ |
| 100939 | + u16 wctrlFlags /* Might include WHERE_AND_ONLY */ |
| 100606 | 100940 | ){ |
| 100607 | 100941 | pWC->pParse = pParse; |
| 100608 | 100942 | pWC->pMaskSet = pMaskSet; |
| 100943 | + pWC->pOuter = 0; |
| 100609 | 100944 | pWC->nTerm = 0; |
| 100610 | 100945 | pWC->nSlot = ArraySize(pWC->aStatic); |
| 100611 | 100946 | pWC->a = pWC->aStatic; |
| 100612 | 100947 | pWC->vmask = 0; |
| 100948 | + pWC->wctrlFlags = wctrlFlags; |
| 100613 | 100949 | } |
| 100614 | 100950 | |
| 100615 | 100951 | /* Forward reference */ |
| 100616 | 100952 | static void whereClauseClear(WhereClause*); |
| 100617 | 100953 | |
| | @@ -100923,40 +101259,42 @@ |
| 100923 | 101259 | ){ |
| 100924 | 101260 | WhereTerm *pTerm; |
| 100925 | 101261 | int k; |
| 100926 | 101262 | assert( iCur>=0 ); |
| 100927 | 101263 | op &= WO_ALL; |
| 100928 | | - for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){ |
| 100929 | | - if( pTerm->leftCursor==iCur |
| 100930 | | - && (pTerm->prereqRight & notReady)==0 |
| 100931 | | - && pTerm->u.leftColumn==iColumn |
| 100932 | | - && (pTerm->eOperator & op)!=0 |
| 100933 | | - ){ |
| 100934 | | - if( pIdx && pTerm->eOperator!=WO_ISNULL ){ |
| 100935 | | - Expr *pX = pTerm->pExpr; |
| 100936 | | - CollSeq *pColl; |
| 100937 | | - char idxaff; |
| 100938 | | - int j; |
| 100939 | | - Parse *pParse = pWC->pParse; |
| 100940 | | - |
| 100941 | | - idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 100942 | | - if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue; |
| 100943 | | - |
| 100944 | | - /* Figure out the collation sequence required from an index for |
| 100945 | | - ** it to be useful for optimising expression pX. Store this |
| 100946 | | - ** value in variable pColl. |
| 100947 | | - */ |
| 100948 | | - assert(pX->pLeft); |
| 100949 | | - pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| 100950 | | - assert(pColl || pParse->nErr); |
| 100951 | | - |
| 100952 | | - for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 100953 | | - if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 100954 | | - } |
| 100955 | | - if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue; |
| 100956 | | - } |
| 100957 | | - return pTerm; |
| 101264 | + for(; pWC; pWC=pWC->pOuter){ |
| 101265 | + for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){ |
| 101266 | + if( pTerm->leftCursor==iCur |
| 101267 | + && (pTerm->prereqRight & notReady)==0 |
| 101268 | + && pTerm->u.leftColumn==iColumn |
| 101269 | + && (pTerm->eOperator & op)!=0 |
| 101270 | + ){ |
| 101271 | + if( pIdx && pTerm->eOperator!=WO_ISNULL ){ |
| 101272 | + Expr *pX = pTerm->pExpr; |
| 101273 | + CollSeq *pColl; |
| 101274 | + char idxaff; |
| 101275 | + int j; |
| 101276 | + Parse *pParse = pWC->pParse; |
| 101277 | + |
| 101278 | + idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 101279 | + if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue; |
| 101280 | + |
| 101281 | + /* Figure out the collation sequence required from an index for |
| 101282 | + ** it to be useful for optimising expression pX. Store this |
| 101283 | + ** value in variable pColl. |
| 101284 | + */ |
| 101285 | + assert(pX->pLeft); |
| 101286 | + pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| 101287 | + assert(pColl || pParse->nErr); |
| 101288 | + |
| 101289 | + for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 101290 | + if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 101291 | + } |
| 101292 | + if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue; |
| 101293 | + } |
| 101294 | + return pTerm; |
| 101295 | + } |
| 100958 | 101296 | } |
| 100959 | 101297 | } |
| 100960 | 101298 | return 0; |
| 100961 | 101299 | } |
| 100962 | 101300 | |
| | @@ -101029,11 +101367,11 @@ |
| 101029 | 101367 | int iCol = pRight->iColumn; |
| 101030 | 101368 | pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); |
| 101031 | 101369 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 101032 | 101370 | z = (char *)sqlite3_value_text(pVal); |
| 101033 | 101371 | } |
| 101034 | | - sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */ |
| 101372 | + sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-31526-56213 */ |
| 101035 | 101373 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 101036 | 101374 | }else if( op==TK_STRING ){ |
| 101037 | 101375 | z = pRight->u.zToken; |
| 101038 | 101376 | } |
| 101039 | 101377 | if( z ){ |
| | @@ -101047,11 +101385,11 @@ |
| 101047 | 101385 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 101048 | 101386 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 101049 | 101387 | *ppPrefix = pPrefix; |
| 101050 | 101388 | if( op==TK_VARIABLE ){ |
| 101051 | 101389 | Vdbe *v = pParse->pVdbe; |
| 101052 | | - sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */ |
| 101390 | + sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-31526-56213 */ |
| 101053 | 101391 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 101054 | 101392 | /* If the rhs of the LIKE expression is a variable, and the current |
| 101055 | 101393 | ** value of the variable means there is no need to invoke the LIKE |
| 101056 | 101394 | ** function, then no OP_Variable will be added to the program. |
| 101057 | 101395 | ** This causes problems for the sqlite3_bind_parameter_name() |
| | @@ -101216,11 +101554,11 @@ |
| 101216 | 101554 | assert( pExpr->op==TK_OR ); |
| 101217 | 101555 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 101218 | 101556 | if( pOrInfo==0 ) return; |
| 101219 | 101557 | pTerm->wtFlags |= TERM_ORINFO; |
| 101220 | 101558 | pOrWc = &pOrInfo->wc; |
| 101221 | | - whereClauseInit(pOrWc, pWC->pParse, pMaskSet); |
| 101559 | + whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
| 101222 | 101560 | whereSplit(pOrWc, pExpr, TK_OR); |
| 101223 | 101561 | exprAnalyzeAll(pSrc, pOrWc); |
| 101224 | 101562 | if( db->mallocFailed ) return; |
| 101225 | 101563 | assert( pOrWc->nTerm>=2 ); |
| 101226 | 101564 | |
| | @@ -101243,13 +101581,14 @@ |
| 101243 | 101581 | Bitmask b = 0; |
| 101244 | 101582 | pOrTerm->u.pAndInfo = pAndInfo; |
| 101245 | 101583 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 101246 | 101584 | pOrTerm->eOperator = WO_AND; |
| 101247 | 101585 | pAndWC = &pAndInfo->wc; |
| 101248 | | - whereClauseInit(pAndWC, pWC->pParse, pMaskSet); |
| 101586 | + whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
| 101249 | 101587 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 101250 | 101588 | exprAnalyzeAll(pSrc, pAndWC); |
| 101589 | + pAndWC->pOuter = pWC; |
| 101251 | 101590 | testcase( db->mallocFailed ); |
| 101252 | 101591 | if( !db->mallocFailed ){ |
| 101253 | 101592 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 101254 | 101593 | assert( pAndTerm->pExpr ); |
| 101255 | 101594 | if( allowedOp(pAndTerm->pExpr->op) ){ |
| | @@ -101679,12 +102018,12 @@ |
| 101679 | 102018 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 101680 | 102019 | } |
| 101681 | 102020 | } |
| 101682 | 102021 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 101683 | 102022 | |
| 101684 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 101685 | | - /* When sqlite_stat2 histogram data is available an operator of the |
| 102023 | +#ifdef SQLITE_ENABLE_STAT3 |
| 102024 | + /* When sqlite_stat3 histogram data is available an operator of the |
| 101686 | 102025 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 101687 | 102026 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 101688 | 102027 | ** virtual term of that form. |
| 101689 | 102028 | ** |
| 101690 | 102029 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| | @@ -101718,11 +102057,11 @@ |
| 101718 | 102057 | pTerm->nChild = 1; |
| 101719 | 102058 | pTerm->wtFlags |= TERM_COPIED; |
| 101720 | 102059 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 101721 | 102060 | } |
| 101722 | 102061 | } |
| 101723 | | -#endif /* SQLITE_ENABLE_STAT2 */ |
| 102062 | +#endif /* SQLITE_ENABLE_STAT */ |
| 101724 | 102063 | |
| 101725 | 102064 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 101726 | 102065 | ** an index for tables to the left of the join. |
| 101727 | 102066 | */ |
| 101728 | 102067 | pTerm->prereqRight |= extraRight; |
| | @@ -102140,14 +102479,17 @@ |
| 102140 | 102479 | const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */ |
| 102141 | 102480 | const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */ |
| 102142 | 102481 | WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */ |
| 102143 | 102482 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 102144 | 102483 | |
| 102145 | | - /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses |
| 102146 | | - ** are used */ |
| 102484 | + /* The OR-clause optimization is disallowed if the INDEXED BY or |
| 102485 | + ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */ |
| 102147 | 102486 | if( pSrc->notIndexed || pSrc->pIndex!=0 ){ |
| 102148 | 102487 | return; |
| 102488 | + } |
| 102489 | + if( pWC->wctrlFlags & WHERE_AND_ONLY ){ |
| 102490 | + return; |
| 102149 | 102491 | } |
| 102150 | 102492 | |
| 102151 | 102493 | /* Search the WHERE clause terms for a usable WO_OR term. */ |
| 102152 | 102494 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 102153 | 102495 | if( pTerm->eOperator==WO_OR |
| | @@ -102172,12 +102514,14 @@ |
| 102172 | 102514 | bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost); |
| 102173 | 102515 | }else if( pOrTerm->leftCursor==iCur ){ |
| 102174 | 102516 | WhereClause tempWC; |
| 102175 | 102517 | tempWC.pParse = pWC->pParse; |
| 102176 | 102518 | tempWC.pMaskSet = pWC->pMaskSet; |
| 102519 | + tempWC.pOuter = pWC; |
| 102177 | 102520 | tempWC.op = TK_AND; |
| 102178 | 102521 | tempWC.a = pOrTerm; |
| 102522 | + tempWC.wctrlFlags = 0; |
| 102179 | 102523 | tempWC.nTerm = 1; |
| 102180 | 102524 | bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost); |
| 102181 | 102525 | }else{ |
| 102182 | 102526 | continue; |
| 102183 | 102527 | } |
| | @@ -102766,71 +103110,89 @@ |
| 102766 | 103110 | */ |
| 102767 | 103111 | bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost); |
| 102768 | 103112 | } |
| 102769 | 103113 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 102770 | 103114 | |
| 103115 | +#ifdef SQLITE_ENABLE_STAT3 |
| 102771 | 103116 | /* |
| 102772 | | -** Argument pIdx is a pointer to an index structure that has an array of |
| 102773 | | -** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column |
| 102774 | | -** stored in Index.aSample. These samples divide the domain of values stored |
| 102775 | | -** the index into (SQLITE_INDEX_SAMPLES+1) regions. |
| 102776 | | -** Region 0 contains all values less than the first sample value. Region |
| 102777 | | -** 1 contains values between the first and second samples. Region 2 contains |
| 102778 | | -** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES |
| 102779 | | -** contains values larger than the last sample. |
| 102780 | | -** |
| 102781 | | -** If the index contains many duplicates of a single value, then it is |
| 102782 | | -** possible that two or more adjacent samples can hold the same value. |
| 102783 | | -** When that is the case, the smallest possible region code is returned |
| 102784 | | -** when roundUp is false and the largest possible region code is returned |
| 102785 | | -** when roundUp is true. |
| 102786 | | -** |
| 102787 | | -** If successful, this function determines which of the regions value |
| 102788 | | -** pVal lies in, sets *piRegion to the region index (a value between 0 |
| 102789 | | -** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK. |
| 102790 | | -** Or, if an OOM occurs while converting text values between encodings, |
| 102791 | | -** SQLITE_NOMEM is returned and *piRegion is undefined. |
| 102792 | | -*/ |
| 102793 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 102794 | | -static int whereRangeRegion( |
| 103117 | +** Estimate the location of a particular key among all keys in an |
| 103118 | +** index. Store the results in aStat as follows: |
| 103119 | +** |
| 103120 | +** aStat[0] Est. number of rows less than pVal |
| 103121 | +** aStat[1] Est. number of rows equal to pVal |
| 103122 | +** |
| 103123 | +** Return SQLITE_OK on success. |
| 103124 | +*/ |
| 103125 | +static int whereKeyStats( |
| 102795 | 103126 | Parse *pParse, /* Database connection */ |
| 102796 | 103127 | Index *pIdx, /* Index to consider domain of */ |
| 102797 | 103128 | sqlite3_value *pVal, /* Value to consider */ |
| 102798 | | - int roundUp, /* Return largest valid region if true */ |
| 102799 | | - int *piRegion /* OUT: Region of domain in which value lies */ |
| 103129 | + int roundUp, /* Round up if true. Round down if false */ |
| 103130 | + tRowcnt *aStat /* OUT: stats written here */ |
| 102800 | 103131 | ){ |
| 103132 | + tRowcnt n; |
| 103133 | + IndexSample *aSample; |
| 103134 | + int i, eType; |
| 103135 | + int isEq = 0; |
| 103136 | + i64 v; |
| 103137 | + double r, rS; |
| 103138 | + |
| 102801 | 103139 | assert( roundUp==0 || roundUp==1 ); |
| 102802 | | - if( ALWAYS(pVal) ){ |
| 102803 | | - IndexSample *aSample = pIdx->aSample; |
| 102804 | | - int i = 0; |
| 102805 | | - int eType = sqlite3_value_type(pVal); |
| 102806 | | - |
| 102807 | | - if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ |
| 102808 | | - double r = sqlite3_value_double(pVal); |
| 102809 | | - for(i=0; i<SQLITE_INDEX_SAMPLES; i++){ |
| 102810 | | - if( aSample[i].eType==SQLITE_NULL ) continue; |
| 102811 | | - if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 102812 | | - if( roundUp ){ |
| 102813 | | - if( aSample[i].u.r>r ) break; |
| 102814 | | - }else{ |
| 102815 | | - if( aSample[i].u.r>=r ) break; |
| 102816 | | - } |
| 102817 | | - } |
| 102818 | | - }else if( eType==SQLITE_NULL ){ |
| 102819 | | - i = 0; |
| 102820 | | - if( roundUp ){ |
| 102821 | | - while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++; |
| 102822 | | - } |
| 102823 | | - }else{ |
| 103140 | + assert( pIdx->nSample>0 ); |
| 103141 | + if( pVal==0 ) return SQLITE_ERROR; |
| 103142 | + n = pIdx->aiRowEst[0]; |
| 103143 | + aSample = pIdx->aSample; |
| 103144 | + eType = sqlite3_value_type(pVal); |
| 103145 | + |
| 103146 | + if( eType==SQLITE_INTEGER ){ |
| 103147 | + v = sqlite3_value_int64(pVal); |
| 103148 | + r = (i64)v; |
| 103149 | + for(i=0; i<pIdx->nSample; i++){ |
| 103150 | + if( aSample[i].eType==SQLITE_NULL ) continue; |
| 103151 | + if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 103152 | + if( aSample[i].eType==SQLITE_INTEGER ){ |
| 103153 | + if( aSample[i].u.i>=v ){ |
| 103154 | + isEq = aSample[i].u.i==v; |
| 103155 | + break; |
| 103156 | + } |
| 103157 | + }else{ |
| 103158 | + assert( aSample[i].eType==SQLITE_FLOAT ); |
| 103159 | + if( aSample[i].u.r>=r ){ |
| 103160 | + isEq = aSample[i].u.r==r; |
| 103161 | + break; |
| 103162 | + } |
| 103163 | + } |
| 103164 | + } |
| 103165 | + }else if( eType==SQLITE_FLOAT ){ |
| 103166 | + r = sqlite3_value_double(pVal); |
| 103167 | + for(i=0; i<pIdx->nSample; i++){ |
| 103168 | + if( aSample[i].eType==SQLITE_NULL ) continue; |
| 103169 | + if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 103170 | + if( aSample[i].eType==SQLITE_FLOAT ){ |
| 103171 | + rS = aSample[i].u.r; |
| 103172 | + }else{ |
| 103173 | + rS = aSample[i].u.i; |
| 103174 | + } |
| 103175 | + if( rS>=r ){ |
| 103176 | + isEq = rS==r; |
| 103177 | + break; |
| 103178 | + } |
| 103179 | + } |
| 103180 | + }else if( eType==SQLITE_NULL ){ |
| 103181 | + i = 0; |
| 103182 | + if( aSample[0].eType==SQLITE_NULL ) isEq = 1; |
| 103183 | + }else{ |
| 103184 | + assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 103185 | + for(i=0; i<pIdx->nSample; i++){ |
| 103186 | + if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){ |
| 103187 | + break; |
| 103188 | + } |
| 103189 | + } |
| 103190 | + if( i<pIdx->nSample ){ |
| 102824 | 103191 | sqlite3 *db = pParse->db; |
| 102825 | 103192 | CollSeq *pColl; |
| 102826 | 103193 | const u8 *z; |
| 102827 | | - int n; |
| 102828 | | - |
| 102829 | | - /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */ |
| 102830 | | - assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 102831 | | - |
| 102832 | 103194 | if( eType==SQLITE_BLOB ){ |
| 102833 | 103195 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 102834 | 103196 | pColl = db->pDfltColl; |
| 102835 | 103197 | assert( pColl->enc==SQLITE_UTF8 ); |
| 102836 | 103198 | }else{ |
| | @@ -102845,16 +103207,16 @@ |
| 102845 | 103207 | return SQLITE_NOMEM; |
| 102846 | 103208 | } |
| 102847 | 103209 | assert( z && pColl && pColl->xCmp ); |
| 102848 | 103210 | } |
| 102849 | 103211 | n = sqlite3ValueBytes(pVal, pColl->enc); |
| 102850 | | - |
| 102851 | | - for(i=0; i<SQLITE_INDEX_SAMPLES; i++){ |
| 103212 | + |
| 103213 | + for(; i<pIdx->nSample; i++){ |
| 102852 | 103214 | int c; |
| 102853 | 103215 | int eSampletype = aSample[i].eType; |
| 102854 | | - if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue; |
| 102855 | | - if( (eSampletype!=eType) ) break; |
| 103216 | + if( eSampletype<eType ) continue; |
| 103217 | + if( eSampletype!=eType ) break; |
| 102856 | 103218 | #ifndef SQLITE_OMIT_UTF16 |
| 102857 | 103219 | if( pColl->enc!=SQLITE_UTF8 ){ |
| 102858 | 103220 | int nSample; |
| 102859 | 103221 | char *zSample = sqlite3Utf8to16( |
| 102860 | 103222 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| | @@ -102868,20 +103230,51 @@ |
| 102868 | 103230 | }else |
| 102869 | 103231 | #endif |
| 102870 | 103232 | { |
| 102871 | 103233 | c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z); |
| 102872 | 103234 | } |
| 102873 | | - if( c-roundUp>=0 ) break; |
| 103235 | + if( c>=0 ){ |
| 103236 | + if( c==0 ) isEq = 1; |
| 103237 | + break; |
| 103238 | + } |
| 102874 | 103239 | } |
| 102875 | 103240 | } |
| 103241 | + } |
| 102876 | 103242 | |
| 102877 | | - assert( i>=0 && i<=SQLITE_INDEX_SAMPLES ); |
| 102878 | | - *piRegion = i; |
| 103243 | + /* At this point, aSample[i] is the first sample that is greater than |
| 103244 | + ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 103245 | + ** than pVal. If aSample[i]==pVal, then isEq==1. |
| 103246 | + */ |
| 103247 | + if( isEq ){ |
| 103248 | + assert( i<pIdx->nSample ); |
| 103249 | + aStat[0] = aSample[i].nLt; |
| 103250 | + aStat[1] = aSample[i].nEq; |
| 103251 | + }else{ |
| 103252 | + tRowcnt iLower, iUpper, iGap; |
| 103253 | + if( i==0 ){ |
| 103254 | + iLower = 0; |
| 103255 | + iUpper = aSample[0].nLt; |
| 103256 | + }else{ |
| 103257 | + iUpper = i>=pIdx->nSample ? n : aSample[i].nLt; |
| 103258 | + iLower = aSample[i-1].nEq + aSample[i-1].nLt; |
| 103259 | + } |
| 103260 | + aStat[1] = pIdx->avgEq; |
| 103261 | + if( iLower>=iUpper ){ |
| 103262 | + iGap = 0; |
| 103263 | + }else{ |
| 103264 | + iGap = iUpper - iLower; |
| 103265 | + } |
| 103266 | + if( roundUp ){ |
| 103267 | + iGap = (iGap*2)/3; |
| 103268 | + }else{ |
| 103269 | + iGap = iGap/3; |
| 103270 | + } |
| 103271 | + aStat[0] = iLower + iGap; |
| 102879 | 103272 | } |
| 102880 | 103273 | return SQLITE_OK; |
| 102881 | 103274 | } |
| 102882 | | -#endif /* #ifdef SQLITE_ENABLE_STAT2 */ |
| 103275 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 102883 | 103276 | |
| 102884 | 103277 | /* |
| 102885 | 103278 | ** If expression pExpr represents a literal value, set *pp to point to |
| 102886 | 103279 | ** an sqlite3_value structure containing the same value, with affinity |
| 102887 | 103280 | ** aff applied to it, before returning. It is the responsibility of the |
| | @@ -102895,11 +103288,11 @@ |
| 102895 | 103288 | ** |
| 102896 | 103289 | ** If neither of the above apply, set *pp to NULL. |
| 102897 | 103290 | ** |
| 102898 | 103291 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 102899 | 103292 | */ |
| 102900 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103293 | +#ifdef SQLITE_ENABLE_STAT3 |
| 102901 | 103294 | static int valueFromExpr( |
| 102902 | 103295 | Parse *pParse, |
| 102903 | 103296 | Expr *pExpr, |
| 102904 | 103297 | u8 aff, |
| 102905 | 103298 | sqlite3_value **pp |
| | @@ -102906,11 +103299,11 @@ |
| 102906 | 103299 | ){ |
| 102907 | 103300 | if( pExpr->op==TK_VARIABLE |
| 102908 | 103301 | || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) |
| 102909 | 103302 | ){ |
| 102910 | 103303 | int iVar = pExpr->iColumn; |
| 102911 | | - sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */ |
| 103304 | + sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-31526-56213 */ |
| 102912 | 103305 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 102913 | 103306 | return SQLITE_OK; |
| 102914 | 103307 | } |
| 102915 | 103308 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 102916 | 103309 | } |
| | @@ -102943,106 +103336,92 @@ |
| 102943 | 103336 | ** |
| 102944 | 103337 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 102945 | 103338 | ** |
| 102946 | 103339 | ** then nEq should be passed 0. |
| 102947 | 103340 | ** |
| 102948 | | -** The returned value is an integer between 1 and 100, inclusive. A return |
| 102949 | | -** value of 1 indicates that the proposed range scan is expected to visit |
| 102950 | | -** approximately 1/100th (1%) of the rows selected by the nEq equality |
| 102951 | | -** constraints (if any). A return value of 100 indicates that it is expected |
| 102952 | | -** that the range scan will visit every row (100%) selected by the equality |
| 102953 | | -** constraints. |
| 103341 | +** The returned value is an integer divisor to reduce the estimated |
| 103342 | +** search space. A return value of 1 means that range constraints are |
| 103343 | +** no help at all. A return value of 2 means range constraints are |
| 103344 | +** expected to reduce the search space by half. And so forth... |
| 102954 | 103345 | ** |
| 102955 | | -** In the absence of sqlite_stat2 ANALYZE data, each range inequality |
| 102956 | | -** reduces the search space by 3/4ths. Hence a single constraint (x>?) |
| 102957 | | -** results in a return of 25 and a range constraint (x>? AND x<?) results |
| 102958 | | -** in a return of 6. |
| 103346 | +** In the absence of sqlite_stat3 ANALYZE data, each range inequality |
| 103347 | +** reduces the search space by a factor of 4. Hence a single constraint (x>?) |
| 103348 | +** results in a return of 4 and a range constraint (x>? AND x<?) results |
| 103349 | +** in a return of 16. |
| 102959 | 103350 | */ |
| 102960 | 103351 | static int whereRangeScanEst( |
| 102961 | 103352 | Parse *pParse, /* Parsing & code generating context */ |
| 102962 | 103353 | Index *p, /* The index containing the range-compared column; "x" */ |
| 102963 | 103354 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 102964 | 103355 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 102965 | 103356 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 102966 | | - int *piEst /* OUT: Return value */ |
| 103357 | + double *pRangeDiv /* OUT: Reduce search space by this divisor */ |
| 102967 | 103358 | ){ |
| 102968 | 103359 | int rc = SQLITE_OK; |
| 102969 | 103360 | |
| 102970 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 102971 | | - |
| 102972 | | - if( nEq==0 && p->aSample ){ |
| 102973 | | - sqlite3_value *pLowerVal = 0; |
| 102974 | | - sqlite3_value *pUpperVal = 0; |
| 102975 | | - int iEst; |
| 102976 | | - int iLower = 0; |
| 102977 | | - int iUpper = SQLITE_INDEX_SAMPLES; |
| 102978 | | - int roundUpUpper = 0; |
| 102979 | | - int roundUpLower = 0; |
| 103361 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103362 | + |
| 103363 | + if( nEq==0 && p->nSample ){ |
| 103364 | + sqlite3_value *pRangeVal; |
| 103365 | + tRowcnt iLower = 0; |
| 103366 | + tRowcnt iUpper = p->aiRowEst[0]; |
| 103367 | + tRowcnt a[2]; |
| 102980 | 103368 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 102981 | 103369 | |
| 102982 | 103370 | if( pLower ){ |
| 102983 | 103371 | Expr *pExpr = pLower->pExpr->pRight; |
| 102984 | | - rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal); |
| 103372 | + rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
| 102985 | 103373 | assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE ); |
| 102986 | | - roundUpLower = (pLower->eOperator==WO_GT) ?1:0; |
| 103374 | + if( rc==SQLITE_OK |
| 103375 | + && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK |
| 103376 | + ){ |
| 103377 | + iLower = a[0]; |
| 103378 | + if( pLower->eOperator==WO_GT ) iLower += a[1]; |
| 103379 | + } |
| 103380 | + sqlite3ValueFree(pRangeVal); |
| 102987 | 103381 | } |
| 102988 | 103382 | if( rc==SQLITE_OK && pUpper ){ |
| 102989 | 103383 | Expr *pExpr = pUpper->pExpr->pRight; |
| 102990 | | - rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal); |
| 103384 | + rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
| 102991 | 103385 | assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE ); |
| 102992 | | - roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0; |
| 102993 | | - } |
| 102994 | | - |
| 102995 | | - if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){ |
| 102996 | | - sqlite3ValueFree(pLowerVal); |
| 102997 | | - sqlite3ValueFree(pUpperVal); |
| 102998 | | - goto range_est_fallback; |
| 102999 | | - }else if( pLowerVal==0 ){ |
| 103000 | | - rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); |
| 103001 | | - if( pLower ) iLower = iUpper/2; |
| 103002 | | - }else if( pUpperVal==0 ){ |
| 103003 | | - rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); |
| 103004 | | - if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2; |
| 103005 | | - }else{ |
| 103006 | | - rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); |
| 103007 | | - if( rc==SQLITE_OK ){ |
| 103008 | | - rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); |
| 103009 | | - } |
| 103010 | | - } |
| 103011 | | - WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper)); |
| 103012 | | - |
| 103013 | | - iEst = iUpper - iLower; |
| 103014 | | - testcase( iEst==SQLITE_INDEX_SAMPLES ); |
| 103015 | | - assert( iEst<=SQLITE_INDEX_SAMPLES ); |
| 103016 | | - if( iEst<1 ){ |
| 103017 | | - *piEst = 50/SQLITE_INDEX_SAMPLES; |
| 103018 | | - }else{ |
| 103019 | | - *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES; |
| 103020 | | - } |
| 103021 | | - sqlite3ValueFree(pLowerVal); |
| 103022 | | - sqlite3ValueFree(pUpperVal); |
| 103023 | | - return rc; |
| 103024 | | - } |
| 103025 | | -range_est_fallback: |
| 103386 | + if( rc==SQLITE_OK |
| 103387 | + && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK |
| 103388 | + ){ |
| 103389 | + iUpper = a[0]; |
| 103390 | + if( pUpper->eOperator==WO_LE ) iUpper += a[1]; |
| 103391 | + } |
| 103392 | + sqlite3ValueFree(pRangeVal); |
| 103393 | + } |
| 103394 | + if( rc==SQLITE_OK ){ |
| 103395 | + if( iUpper<=iLower ){ |
| 103396 | + *pRangeDiv = (double)p->aiRowEst[0]; |
| 103397 | + }else{ |
| 103398 | + *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower); |
| 103399 | + } |
| 103400 | + WHERETRACE(("range scan regions: %u..%u div=%g\n", |
| 103401 | + (u32)iLower, (u32)iUpper, *pRangeDiv)); |
| 103402 | + return SQLITE_OK; |
| 103403 | + } |
| 103404 | + } |
| 103026 | 103405 | #else |
| 103027 | 103406 | UNUSED_PARAMETER(pParse); |
| 103028 | 103407 | UNUSED_PARAMETER(p); |
| 103029 | 103408 | UNUSED_PARAMETER(nEq); |
| 103030 | 103409 | #endif |
| 103031 | 103410 | assert( pLower || pUpper ); |
| 103032 | | - *piEst = 100; |
| 103033 | | - if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4; |
| 103034 | | - if( pUpper ) *piEst /= 4; |
| 103411 | + *pRangeDiv = (double)1; |
| 103412 | + if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4; |
| 103413 | + if( pUpper ) *pRangeDiv *= (double)4; |
| 103035 | 103414 | return rc; |
| 103036 | 103415 | } |
| 103037 | 103416 | |
| 103038 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103417 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103039 | 103418 | /* |
| 103040 | 103419 | ** Estimate the number of rows that will be returned based on |
| 103041 | 103420 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 103042 | 103421 | ** the histogram data. This only works when x is the left-most |
| 103043 | | -** column of an index and sqlite_stat2 histogram data is available |
| 103422 | +** column of an index and sqlite_stat3 histogram data is available |
| 103044 | 103423 | ** for that index. When pExpr==NULL that means the constraint is |
| 103045 | 103424 | ** "x IS NULL" instead of "x=VALUE". |
| 103046 | 103425 | ** |
| 103047 | 103426 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 103048 | 103427 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| | @@ -103058,44 +103437,36 @@ |
| 103058 | 103437 | Index *p, /* The index whose left-most column is pTerm */ |
| 103059 | 103438 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 103060 | 103439 | double *pnRow /* Write the revised row estimate here */ |
| 103061 | 103440 | ){ |
| 103062 | 103441 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
| 103063 | | - int iLower, iUpper; /* Range of histogram regions containing pRhs */ |
| 103064 | 103442 | u8 aff; /* Column affinity */ |
| 103065 | 103443 | int rc; /* Subfunction return code */ |
| 103066 | | - double nRowEst; /* New estimate of the number of rows */ |
| 103444 | + tRowcnt a[2]; /* Statistics */ |
| 103067 | 103445 | |
| 103068 | 103446 | assert( p->aSample!=0 ); |
| 103447 | + assert( p->nSample>0 ); |
| 103069 | 103448 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 103070 | 103449 | if( pExpr ){ |
| 103071 | 103450 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| 103072 | 103451 | if( rc ) goto whereEqualScanEst_cancel; |
| 103073 | 103452 | }else{ |
| 103074 | 103453 | pRhs = sqlite3ValueNew(pParse->db); |
| 103075 | 103454 | } |
| 103076 | 103455 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
| 103077 | | - rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower); |
| 103078 | | - if( rc ) goto whereEqualScanEst_cancel; |
| 103079 | | - rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper); |
| 103080 | | - if( rc ) goto whereEqualScanEst_cancel; |
| 103081 | | - WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper)); |
| 103082 | | - if( iLower>=iUpper ){ |
| 103083 | | - nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2); |
| 103084 | | - if( nRowEst<*pnRow ) *pnRow = nRowEst; |
| 103085 | | - }else{ |
| 103086 | | - nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES; |
| 103087 | | - *pnRow = nRowEst; |
| 103088 | | - } |
| 103089 | | - |
| 103456 | + rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 103457 | + if( rc==SQLITE_OK ){ |
| 103458 | + WHERETRACE(("equality scan regions: %d\n", (int)a[1])); |
| 103459 | + *pnRow = a[1]; |
| 103460 | + } |
| 103090 | 103461 | whereEqualScanEst_cancel: |
| 103091 | 103462 | sqlite3ValueFree(pRhs); |
| 103092 | 103463 | return rc; |
| 103093 | 103464 | } |
| 103094 | | -#endif /* defined(SQLITE_ENABLE_STAT2) */ |
| 103465 | +#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 103095 | 103466 | |
| 103096 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103467 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103097 | 103468 | /* |
| 103098 | 103469 | ** Estimate the number of rows that will be returned based on |
| 103099 | 103470 | ** an IN constraint where the right-hand side of the IN operator |
| 103100 | 103471 | ** is a list of values. Example: |
| 103101 | 103472 | ** |
| | @@ -103114,64 +103485,29 @@ |
| 103114 | 103485 | Parse *pParse, /* Parsing & code generating context */ |
| 103115 | 103486 | Index *p, /* The index whose left-most column is pTerm */ |
| 103116 | 103487 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 103117 | 103488 | double *pnRow /* Write the revised row estimate here */ |
| 103118 | 103489 | ){ |
| 103119 | | - sqlite3_value *pVal = 0; /* One value from list */ |
| 103120 | | - int iLower, iUpper; /* Range of histogram regions containing pRhs */ |
| 103121 | | - u8 aff; /* Column affinity */ |
| 103122 | | - int rc = SQLITE_OK; /* Subfunction return code */ |
| 103123 | | - double nRowEst; /* New estimate of the number of rows */ |
| 103124 | | - int nSpan = 0; /* Number of histogram regions spanned */ |
| 103125 | | - int nSingle = 0; /* Histogram regions hit by a single value */ |
| 103126 | | - int nNotFound = 0; /* Count of values that are not constants */ |
| 103127 | | - int i; /* Loop counter */ |
| 103128 | | - u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */ |
| 103129 | | - u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */ |
| 103490 | + int rc = SQLITE_OK; /* Subfunction return code */ |
| 103491 | + double nEst; /* Number of rows for a single term */ |
| 103492 | + double nRowEst = (double)0; /* New estimate of the number of rows */ |
| 103493 | + int i; /* Loop counter */ |
| 103130 | 103494 | |
| 103131 | 103495 | assert( p->aSample!=0 ); |
| 103132 | | - aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 103133 | | - memset(aSpan, 0, sizeof(aSpan)); |
| 103134 | | - memset(aSingle, 0, sizeof(aSingle)); |
| 103135 | | - for(i=0; i<pList->nExpr; i++){ |
| 103136 | | - sqlite3ValueFree(pVal); |
| 103137 | | - rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal); |
| 103138 | | - if( rc ) break; |
| 103139 | | - if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){ |
| 103140 | | - nNotFound++; |
| 103141 | | - continue; |
| 103142 | | - } |
| 103143 | | - rc = whereRangeRegion(pParse, p, pVal, 0, &iLower); |
| 103144 | | - if( rc ) break; |
| 103145 | | - rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper); |
| 103146 | | - if( rc ) break; |
| 103147 | | - if( iLower>=iUpper ){ |
| 103148 | | - aSingle[iLower] = 1; |
| 103149 | | - }else{ |
| 103150 | | - assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES ); |
| 103151 | | - while( iLower<iUpper ) aSpan[iLower++] = 1; |
| 103152 | | - } |
| 103496 | + for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 103497 | + nEst = p->aiRowEst[0]; |
| 103498 | + rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| 103499 | + nRowEst += nEst; |
| 103153 | 103500 | } |
| 103154 | 103501 | if( rc==SQLITE_OK ){ |
| 103155 | | - for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){ |
| 103156 | | - if( aSpan[i] ){ |
| 103157 | | - nSpan++; |
| 103158 | | - }else if( aSingle[i] ){ |
| 103159 | | - nSingle++; |
| 103160 | | - } |
| 103161 | | - } |
| 103162 | | - nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES) |
| 103163 | | - + nNotFound*p->aiRowEst[1]; |
| 103164 | 103502 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 103165 | 103503 | *pnRow = nRowEst; |
| 103166 | | - WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n", |
| 103167 | | - nSpan, nSingle, nNotFound, nRowEst)); |
| 103504 | + WHERETRACE(("IN row estimate: est=%g\n", nRowEst)); |
| 103168 | 103505 | } |
| 103169 | | - sqlite3ValueFree(pVal); |
| 103170 | 103506 | return rc; |
| 103171 | 103507 | } |
| 103172 | | -#endif /* defined(SQLITE_ENABLE_STAT2) */ |
| 103508 | +#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 103173 | 103509 | |
| 103174 | 103510 | |
| 103175 | 103511 | /* |
| 103176 | 103512 | ** Find the best query plan for accessing a particular table. Write the |
| 103177 | 103513 | ** best query plan and its cost into the WhereCost object supplied as the |
| | @@ -103214,11 +103550,11 @@ |
| 103214 | 103550 | Index *pProbe; /* An index we are evaluating */ |
| 103215 | 103551 | Index *pIdx; /* Copy of pProbe, or zero for IPK index */ |
| 103216 | 103552 | int eqTermMask; /* Current mask of valid equality operators */ |
| 103217 | 103553 | int idxEqTermMask; /* Index mask of valid equality operators */ |
| 103218 | 103554 | Index sPk; /* A fake index object for the primary key */ |
| 103219 | | - unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 103555 | + tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 103220 | 103556 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 103221 | 103557 | int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */ |
| 103222 | 103558 | |
| 103223 | 103559 | /* Initialize the cost to a worst-case value */ |
| 103224 | 103560 | memset(pCost, 0, sizeof(*pCost)); |
| | @@ -103269,14 +103605,14 @@ |
| 103269 | 103605 | } |
| 103270 | 103606 | |
| 103271 | 103607 | /* Loop over all indices looking for the best one to use |
| 103272 | 103608 | */ |
| 103273 | 103609 | for(; pProbe; pIdx=pProbe=pProbe->pNext){ |
| 103274 | | - const unsigned int * const aiRowEst = pProbe->aiRowEst; |
| 103610 | + const tRowcnt * const aiRowEst = pProbe->aiRowEst; |
| 103275 | 103611 | double cost; /* Cost of using pProbe */ |
| 103276 | 103612 | double nRow; /* Estimated number of rows in result set */ |
| 103277 | | - double log10N; /* base-10 logarithm of nRow (inexact) */ |
| 103613 | + double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */ |
| 103278 | 103614 | int rev; /* True to scan in reverse order */ |
| 103279 | 103615 | int wsFlags = 0; |
| 103280 | 103616 | Bitmask used = 0; |
| 103281 | 103617 | |
| 103282 | 103618 | /* The following variables are populated based on the properties of |
| | @@ -103312,18 +103648,16 @@ |
| 103312 | 103648 | ** Set to true if there was at least one "x IN (SELECT ...)" term used |
| 103313 | 103649 | ** in determining the value of nInMul. Note that the RHS of the |
| 103314 | 103650 | ** IN operator must be a SELECT, not a value list, for this variable |
| 103315 | 103651 | ** to be true. |
| 103316 | 103652 | ** |
| 103317 | | - ** estBound: |
| 103318 | | - ** An estimate on the amount of the table that must be searched. A |
| 103319 | | - ** value of 100 means the entire table is searched. Range constraints |
| 103320 | | - ** might reduce this to a value less than 100 to indicate that only |
| 103321 | | - ** a fraction of the table needs searching. In the absence of |
| 103322 | | - ** sqlite_stat2 ANALYZE data, a single inequality reduces the search |
| 103323 | | - ** space to 1/4rd its original size. So an x>? constraint reduces |
| 103324 | | - ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6. |
| 103653 | + ** rangeDiv: |
| 103654 | + ** An estimate of a divisor by which to reduce the search space due |
| 103655 | + ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE |
| 103656 | + ** data, a single inequality reduces the search space to 1/4rd its |
| 103657 | + ** original size (rangeDiv==4). Two inequalities reduce the search |
| 103658 | + ** space to 1/16th of its original size (rangeDiv==16). |
| 103325 | 103659 | ** |
| 103326 | 103660 | ** bSort: |
| 103327 | 103661 | ** Boolean. True if there is an ORDER BY clause that will require an |
| 103328 | 103662 | ** external sort (i.e. scanning the index being evaluated will not |
| 103329 | 103663 | ** correctly order records). |
| | @@ -103344,26 +103678,27 @@ |
| 103344 | 103678 | ** SELECT a, b, c FROM tbl WHERE a = 1; |
| 103345 | 103679 | */ |
| 103346 | 103680 | int nEq; /* Number of == or IN terms matching index */ |
| 103347 | 103681 | int bInEst = 0; /* True if "x IN (SELECT...)" seen */ |
| 103348 | 103682 | int nInMul = 1; /* Number of distinct equalities to lookup */ |
| 103349 | | - int estBound = 100; /* Estimated reduction in search space */ |
| 103683 | + double rangeDiv = (double)1; /* Estimated reduction in search space */ |
| 103350 | 103684 | int nBound = 0; /* Number of range constraints seen */ |
| 103351 | 103685 | int bSort = !!pOrderBy; /* True if external sort required */ |
| 103352 | 103686 | int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */ |
| 103353 | 103687 | int bLookup = 0; /* True if not a covering index */ |
| 103354 | 103688 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 103355 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103689 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103356 | 103690 | WhereTerm *pFirstTerm = 0; /* First term matching the index */ |
| 103357 | 103691 | #endif |
| 103358 | 103692 | |
| 103359 | 103693 | /* Determine the values of nEq and nInMul */ |
| 103360 | 103694 | for(nEq=0; nEq<pProbe->nColumn; nEq++){ |
| 103361 | 103695 | int j = pProbe->aiColumn[nEq]; |
| 103362 | 103696 | pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx); |
| 103363 | 103697 | if( pTerm==0 ) break; |
| 103364 | 103698 | wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ); |
| 103699 | + testcase( pTerm->pWC!=pWC ); |
| 103365 | 103700 | if( pTerm->eOperator & WO_IN ){ |
| 103366 | 103701 | Expr *pExpr = pTerm->pExpr; |
| 103367 | 103702 | wsFlags |= WHERE_COLUMN_IN; |
| 103368 | 103703 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 103369 | 103704 | /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */ |
| | @@ -103374,32 +103709,34 @@ |
| 103374 | 103709 | nInMul *= pExpr->x.pList->nExpr; |
| 103375 | 103710 | } |
| 103376 | 103711 | }else if( pTerm->eOperator & WO_ISNULL ){ |
| 103377 | 103712 | wsFlags |= WHERE_COLUMN_NULL; |
| 103378 | 103713 | } |
| 103379 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103714 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103380 | 103715 | if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm; |
| 103381 | 103716 | #endif |
| 103382 | 103717 | used |= pTerm->prereqRight; |
| 103383 | 103718 | } |
| 103384 | 103719 | |
| 103385 | | - /* Determine the value of estBound. */ |
| 103720 | + /* Determine the value of rangeDiv */ |
| 103386 | 103721 | if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){ |
| 103387 | 103722 | int j = pProbe->aiColumn[nEq]; |
| 103388 | 103723 | if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){ |
| 103389 | 103724 | WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx); |
| 103390 | 103725 | WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx); |
| 103391 | | - whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound); |
| 103726 | + whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv); |
| 103392 | 103727 | if( pTop ){ |
| 103393 | 103728 | nBound = 1; |
| 103394 | 103729 | wsFlags |= WHERE_TOP_LIMIT; |
| 103395 | 103730 | used |= pTop->prereqRight; |
| 103731 | + testcase( pTop->pWC!=pWC ); |
| 103396 | 103732 | } |
| 103397 | 103733 | if( pBtm ){ |
| 103398 | 103734 | nBound++; |
| 103399 | 103735 | wsFlags |= WHERE_BTM_LIMIT; |
| 103400 | 103736 | used |= pBtm->prereqRight; |
| 103737 | + testcase( pBtm->pWC!=pWC ); |
| 103401 | 103738 | } |
| 103402 | 103739 | wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE); |
| 103403 | 103740 | } |
| 103404 | 103741 | }else if( pProbe->onError!=OE_None ){ |
| 103405 | 103742 | testcase( wsFlags & WHERE_COLUMN_IN ); |
| | @@ -103458,32 +103795,34 @@ |
| 103458 | 103795 | if( bInEst && nRow*2>aiRowEst[0] ){ |
| 103459 | 103796 | nRow = aiRowEst[0]/2; |
| 103460 | 103797 | nInMul = (int)(nRow / aiRowEst[nEq]); |
| 103461 | 103798 | } |
| 103462 | 103799 | |
| 103463 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103800 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103464 | 103801 | /* If the constraint is of the form x=VALUE or x IN (E1,E2,...) |
| 103465 | 103802 | ** and we do not think that values of x are unique and if histogram |
| 103466 | 103803 | ** data is available for column x, then it might be possible |
| 103467 | 103804 | ** to get a better estimate on the number of rows based on |
| 103468 | 103805 | ** VALUE and how common that value is according to the histogram. |
| 103469 | 103806 | */ |
| 103470 | 103807 | if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){ |
| 103808 | + assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 ); |
| 103471 | 103809 | if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){ |
| 103472 | 103810 | testcase( pFirstTerm->eOperator==WO_EQ ); |
| 103473 | 103811 | testcase( pFirstTerm->eOperator==WO_ISNULL ); |
| 103474 | 103812 | whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow); |
| 103475 | | - }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){ |
| 103813 | + }else if( bInEst==0 ){ |
| 103814 | + assert( pFirstTerm->eOperator==WO_IN ); |
| 103476 | 103815 | whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow); |
| 103477 | 103816 | } |
| 103478 | 103817 | } |
| 103479 | | -#endif /* SQLITE_ENABLE_STAT2 */ |
| 103818 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 103480 | 103819 | |
| 103481 | 103820 | /* Adjust the number of output rows and downward to reflect rows |
| 103482 | 103821 | ** that are excluded by range constraints. |
| 103483 | 103822 | */ |
| 103484 | | - nRow = (nRow * (double)estBound) / (double)100; |
| 103823 | + nRow = nRow/rangeDiv; |
| 103485 | 103824 | if( nRow<1 ) nRow = 1; |
| 103486 | 103825 | |
| 103487 | 103826 | /* Experiments run on real SQLite databases show that the time needed |
| 103488 | 103827 | ** to do a binary search to locate a row in a table or index is roughly |
| 103489 | 103828 | ** log10(N) times the time to move from one row to the next row within |
| | @@ -103608,14 +103947,14 @@ |
| 103608 | 103947 | if( nRow<2 ) nRow = 2; |
| 103609 | 103948 | } |
| 103610 | 103949 | |
| 103611 | 103950 | |
| 103612 | 103951 | WHERETRACE(( |
| 103613 | | - "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 103952 | + "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 103614 | 103953 | " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n", |
| 103615 | 103954 | pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), |
| 103616 | | - nEq, nInMul, estBound, bSort, bLookup, wsFlags, |
| 103955 | + nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags, |
| 103617 | 103956 | notReady, log10N, nRow, cost, used |
| 103618 | 103957 | )); |
| 103619 | 103958 | |
| 103620 | 103959 | /* If this index is the best we have seen so far, then record this |
| 103621 | 103960 | ** index and its cost in the pCost structure. |
| | @@ -104115,11 +104454,12 @@ |
| 104115 | 104454 | */ |
| 104116 | 104455 | static Bitmask codeOneLoopStart( |
| 104117 | 104456 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 104118 | 104457 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 104119 | 104458 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 104120 | | - Bitmask notReady /* Which tables are currently available */ |
| 104459 | + Bitmask notReady, /* Which tables are currently available */ |
| 104460 | + Expr *pWhere /* Complete WHERE clause */ |
| 104121 | 104461 | ){ |
| 104122 | 104462 | int j, k; /* Loop counters */ |
| 104123 | 104463 | int iCur; /* The VDBE cursor for the table */ |
| 104124 | 104464 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 104125 | 104465 | int omitTable; /* True if we use the index only */ |
| | @@ -104597,11 +104937,12 @@ |
| 104597 | 104937 | int regRowset = 0; /* Register for RowSet object */ |
| 104598 | 104938 | int regRowid = 0; /* Register holding rowid */ |
| 104599 | 104939 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 104600 | 104940 | int iRetInit; /* Address of regReturn init */ |
| 104601 | 104941 | int untestedTerms = 0; /* Some terms not completely tested */ |
| 104602 | | - int ii; |
| 104942 | + int ii; /* Loop counter */ |
| 104943 | + Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 104603 | 104944 | |
| 104604 | 104945 | pTerm = pLevel->plan.u.pTerm; |
| 104605 | 104946 | assert( pTerm!=0 ); |
| 104606 | 104947 | assert( pTerm->eOperator==WO_OR ); |
| 104607 | 104948 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| | @@ -104646,18 +104987,33 @@ |
| 104646 | 104987 | regRowset = ++pParse->nMem; |
| 104647 | 104988 | regRowid = ++pParse->nMem; |
| 104648 | 104989 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 104649 | 104990 | } |
| 104650 | 104991 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 104992 | + |
| 104993 | + /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 104994 | + ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 104995 | + ** That way, terms in y that are factored into the disjunction will |
| 104996 | + ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
| 104997 | + */ |
| 104998 | + if( pWC->nTerm>1 ){ |
| 104999 | + pAndExpr = sqlite3ExprAlloc(pParse->db, TK_AND, 0, 0); |
| 105000 | + pAndExpr->pRight = pWhere; |
| 105001 | + } |
| 104651 | 105002 | |
| 104652 | 105003 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 104653 | 105004 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 104654 | 105005 | if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){ |
| 104655 | 105006 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 105007 | + Expr *pOrExpr = pOrTerm->pExpr; |
| 105008 | + if( pAndExpr ){ |
| 105009 | + pAndExpr->pLeft = pOrExpr; |
| 105010 | + pOrExpr = pAndExpr; |
| 105011 | + } |
| 104656 | 105012 | /* Loop through table entries that match term pOrTerm. */ |
| 104657 | | - pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0, 0, |
| 104658 | | - WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE | |
| 105013 | + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 105014 | + WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
| 104659 | 105015 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY); |
| 104660 | 105016 | if( pSubWInfo ){ |
| 104661 | 105017 | explainOneScan( |
| 104662 | 105018 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 104663 | 105019 | ); |
| | @@ -104681,10 +105037,11 @@ |
| 104681 | 105037 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 104682 | 105038 | sqlite3WhereEnd(pSubWInfo); |
| 104683 | 105039 | } |
| 104684 | 105040 | } |
| 104685 | 105041 | } |
| 105042 | + sqlite3DbFree(pParse->db, pAndExpr); |
| 104686 | 105043 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
| 104687 | 105044 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 104688 | 105045 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 104689 | 105046 | |
| 104690 | 105047 | if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab); |
| | @@ -104962,11 +105319,11 @@ |
| 104962 | 105319 | |
| 104963 | 105320 | /* Split the WHERE clause into separate subexpressions where each |
| 104964 | 105321 | ** subexpression is separated by an AND operator. |
| 104965 | 105322 | */ |
| 104966 | 105323 | initMaskSet(pMaskSet); |
| 104967 | | - whereClauseInit(pWC, pParse, pMaskSet); |
| 105324 | + whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags); |
| 104968 | 105325 | sqlite3ExprCodeConstants(pParse, pWhere); |
| 104969 | 105326 | whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
| 104970 | 105327 | |
| 104971 | 105328 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 104972 | 105329 | ** expression and either jump over all of the code or fall thru. |
| | @@ -105201,11 +105558,12 @@ |
| 105201 | 105558 | assert( bestJ>=0 ); |
| 105202 | 105559 | assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) ); |
| 105203 | 105560 | WHERETRACE(("*** Optimizer selects table %d for loop %d" |
| 105204 | 105561 | " with cost=%g and nRow=%g\n", |
| 105205 | 105562 | bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow)); |
| 105206 | | - if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){ |
| 105563 | + /* The ALWAYS() that follows was added to hush up clang scan-build */ |
| 105564 | + if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){ |
| 105207 | 105565 | *ppOrderBy = 0; |
| 105208 | 105566 | } |
| 105209 | 105567 | if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){ |
| 105210 | 105568 | assert( pWInfo->eDistinct==0 ); |
| 105211 | 105569 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| | @@ -105290,11 +105648,11 @@ |
| 105290 | 105648 | int iCur = pTabItem->iCursor; |
| 105291 | 105649 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| 105292 | 105650 | }else |
| 105293 | 105651 | #endif |
| 105294 | 105652 | if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 |
| 105295 | | - && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){ |
| 105653 | + && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
| 105296 | 105654 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 105297 | 105655 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 105298 | 105656 | testcase( pTab->nCol==BMS-1 ); |
| 105299 | 105657 | testcase( pTab->nCol==BMS ); |
| 105300 | 105658 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
| | @@ -105335,11 +105693,11 @@ |
| 105335 | 105693 | */ |
| 105336 | 105694 | notReady = ~(Bitmask)0; |
| 105337 | 105695 | for(i=0; i<nTabList; i++){ |
| 105338 | 105696 | pLevel = &pWInfo->a[i]; |
| 105339 | 105697 | explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags); |
| 105340 | | - notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady); |
| 105698 | + notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady, pWhere); |
| 105341 | 105699 | pWInfo->iContinue = pLevel->addrCont; |
| 105342 | 105700 | } |
| 105343 | 105701 | |
| 105344 | 105702 | #ifdef SQLITE_TEST /* For testing and debugging use only */ |
| 105345 | 105703 | /* Record in the query plan information about the current table |
| | @@ -105470,11 +105828,11 @@ |
| 105470 | 105828 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 105471 | 105829 | Table *pTab = pTabItem->pTab; |
| 105472 | 105830 | assert( pTab!=0 ); |
| 105473 | 105831 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 105474 | 105832 | && pTab->pSelect==0 |
| 105475 | | - && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0 |
| 105833 | + && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
| 105476 | 105834 | ){ |
| 105477 | 105835 | int ws = pLevel->plan.wsFlags; |
| 105478 | 105836 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
| 105479 | 105837 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 105480 | 105838 | } |
| | @@ -108817,11 +109175,13 @@ |
| 108817 | 109175 | sqlite3ParserTOKENTYPE yyminor /* The value for the token */ |
| 108818 | 109176 | sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */ |
| 108819 | 109177 | ){ |
| 108820 | 109178 | YYMINORTYPE yyminorunion; |
| 108821 | 109179 | int yyact; /* The parser action. */ |
| 109180 | +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) |
| 108822 | 109181 | int yyendofinput; /* True if we are at the end of input */ |
| 109182 | +#endif |
| 108823 | 109183 | #ifdef YYERRORSYMBOL |
| 108824 | 109184 | int yyerrorhit = 0; /* True if yymajor has invoked an error */ |
| 108825 | 109185 | #endif |
| 108826 | 109186 | yyParser *yypParser; /* The parser */ |
| 108827 | 109187 | |
| | @@ -108840,11 +109200,13 @@ |
| 108840 | 109200 | yypParser->yyerrcnt = -1; |
| 108841 | 109201 | yypParser->yystack[0].stateno = 0; |
| 108842 | 109202 | yypParser->yystack[0].major = 0; |
| 108843 | 109203 | } |
| 108844 | 109204 | yyminorunion.yy0 = yyminor; |
| 109205 | +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) |
| 108845 | 109206 | yyendofinput = (yymajor==0); |
| 109207 | +#endif |
| 108846 | 109208 | sqlite3ParserARG_STORE; |
| 108847 | 109209 | |
| 108848 | 109210 | #ifndef NDEBUG |
| 108849 | 109211 | if( yyTraceFILE ){ |
| 108850 | 109212 | fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]); |
| | @@ -108852,11 +109214,10 @@ |
| 108852 | 109214 | #endif |
| 108853 | 109215 | |
| 108854 | 109216 | do{ |
| 108855 | 109217 | yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); |
| 108856 | 109218 | if( yyact<YYNSTATE ){ |
| 108857 | | - assert( !yyendofinput ); /* Impossible to shift the $ token */ |
| 108858 | 109219 | yy_shift(yypParser,yyact,yymajor,&yyminorunion); |
| 108859 | 109220 | yypParser->yyerrcnt--; |
| 108860 | 109221 | yymajor = YYNOCODE; |
| 108861 | 109222 | }else if( yyact < YYNSTATE + YYNRULE ){ |
| 108862 | 109223 | yy_reduce(yypParser,yyact-YYNSTATE); |
| | @@ -110244,11 +110605,11 @@ |
| 110244 | 110605 | ** |
| 110245 | 110606 | ** * Recursive calls to this routine from thread X return immediately |
| 110246 | 110607 | ** without blocking. |
| 110247 | 110608 | */ |
| 110248 | 110609 | SQLITE_API int sqlite3_initialize(void){ |
| 110249 | | - sqlite3_mutex *pMaster; /* The main static mutex */ |
| 110610 | + MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ |
| 110250 | 110611 | int rc; /* Result code */ |
| 110251 | 110612 | |
| 110252 | 110613 | #ifdef SQLITE_OMIT_WSD |
| 110253 | 110614 | rc = sqlite3_wsd_init(4096, 24); |
| 110254 | 110615 | if( rc!=SQLITE_OK ){ |
| | @@ -110278,11 +110639,11 @@ |
| 110278 | 110639 | ** This operation is protected by the STATIC_MASTER mutex. Note that |
| 110279 | 110640 | ** MutexAlloc() is called for a static mutex prior to initializing the |
| 110280 | 110641 | ** malloc subsystem - this implies that the allocation of a static |
| 110281 | 110642 | ** mutex must not require support from the malloc subsystem. |
| 110282 | 110643 | */ |
| 110283 | | - pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 110644 | + MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 110284 | 110645 | sqlite3_mutex_enter(pMaster); |
| 110285 | 110646 | sqlite3GlobalConfig.isMutexInit = 1; |
| 110286 | 110647 | if( !sqlite3GlobalConfig.isMallocInit ){ |
| 110287 | 110648 | rc = sqlite3MallocInit(); |
| 110288 | 110649 | } |
| | @@ -111352,17 +111713,17 @@ |
| 111352 | 111713 | sqlite3 *db, |
| 111353 | 111714 | const char *zName, |
| 111354 | 111715 | int nArg |
| 111355 | 111716 | ){ |
| 111356 | 111717 | int nName = sqlite3Strlen30(zName); |
| 111357 | | - int rc; |
| 111718 | + int rc = SQLITE_OK; |
| 111358 | 111719 | sqlite3_mutex_enter(db->mutex); |
| 111359 | 111720 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 111360 | | - sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 111361 | | - 0, sqlite3InvalidFunction, 0, 0, 0); |
| 111721 | + rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 111722 | + 0, sqlite3InvalidFunction, 0, 0, 0); |
| 111362 | 111723 | } |
| 111363 | | - rc = sqlite3ApiExit(db, SQLITE_OK); |
| 111724 | + rc = sqlite3ApiExit(db, rc); |
| 111364 | 111725 | sqlite3_mutex_leave(db->mutex); |
| 111365 | 111726 | return rc; |
| 111366 | 111727 | } |
| 111367 | 111728 | |
| 111368 | 111729 | #ifndef SQLITE_OMIT_TRACE |
| | @@ -112420,10 +112781,11 @@ |
| 112420 | 112781 | if( db ){ |
| 112421 | 112782 | assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 ); |
| 112422 | 112783 | sqlite3_mutex_leave(db->mutex); |
| 112423 | 112784 | } |
| 112424 | 112785 | rc = sqlite3_errcode(db); |
| 112786 | + assert( db!=0 || rc==SQLITE_NOMEM ); |
| 112425 | 112787 | if( rc==SQLITE_NOMEM ){ |
| 112426 | 112788 | sqlite3_close(db); |
| 112427 | 112789 | db = 0; |
| 112428 | 112790 | }else if( rc!=SQLITE_OK ){ |
| 112429 | 112791 | db->magic = SQLITE_MAGIC_SICK; |
| | @@ -114148,10 +114510,17 @@ |
| 114148 | 114510 | #else |
| 114149 | 114511 | # define TESTONLY(X) |
| 114150 | 114512 | #endif |
| 114151 | 114513 | |
| 114152 | 114514 | #endif /* SQLITE_AMALGAMATION */ |
| 114515 | + |
| 114516 | +#ifdef SQLITE_DEBUG |
| 114517 | +SQLITE_PRIVATE int sqlite3Fts3Corrupt(void); |
| 114518 | +# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt() |
| 114519 | +#else |
| 114520 | +# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB |
| 114521 | +#endif |
| 114153 | 114522 | |
| 114154 | 114523 | typedef struct Fts3Table Fts3Table; |
| 114155 | 114524 | typedef struct Fts3Cursor Fts3Cursor; |
| 114156 | 114525 | typedef struct Fts3Expr Fts3Expr; |
| 114157 | 114526 | typedef struct Fts3Phrase Fts3Phrase; |
| | @@ -114176,10 +114545,11 @@ |
| 114176 | 114545 | const char *zDb; /* logical database name */ |
| 114177 | 114546 | const char *zName; /* virtual table name */ |
| 114178 | 114547 | int nColumn; /* number of named columns in virtual table */ |
| 114179 | 114548 | char **azColumn; /* column names. malloced */ |
| 114180 | 114549 | sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */ |
| 114550 | + char *zContentTbl; /* content=xxx option, or NULL */ |
| 114181 | 114551 | |
| 114182 | 114552 | /* Precompiled statements used by the implementation. Each of these |
| 114183 | 114553 | ** statements is run and reset within a single virtual table API call. |
| 114184 | 114554 | */ |
| 114185 | 114555 | sqlite3_stmt *aStmt[27]; |
| | @@ -114216,11 +114586,11 @@ |
| 114216 | 114586 | } *aIndex; |
| 114217 | 114587 | int nMaxPendingData; /* Max pending data before flush to disk */ |
| 114218 | 114588 | int nPendingData; /* Current bytes of pending data */ |
| 114219 | 114589 | sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */ |
| 114220 | 114590 | |
| 114221 | | -#if defined(SQLITE_DEBUG) |
| 114591 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| 114222 | 114592 | /* State variables used for validating that the transaction control |
| 114223 | 114593 | ** methods of the virtual table are called at appropriate times. These |
| 114224 | 114594 | ** values do not contribution to the FTS computation; they are used for |
| 114225 | 114595 | ** verifying the SQLite core. |
| 114226 | 114596 | */ |
| | @@ -114301,10 +114671,11 @@ |
| 114301 | 114671 | */ |
| 114302 | 114672 | struct Fts3PhraseToken { |
| 114303 | 114673 | char *z; /* Text of the token */ |
| 114304 | 114674 | int n; /* Number of bytes in buffer z */ |
| 114305 | 114675 | int isPrefix; /* True if token ends with a "*" character */ |
| 114676 | + int bFirst; /* True if token must appear at position 0 */ |
| 114306 | 114677 | |
| 114307 | 114678 | /* Variables above this point are populated when the expression is |
| 114308 | 114679 | ** parsed (by code in fts3_expr.c). Below this point the variables are |
| 114309 | 114680 | ** used when evaluating the expression. */ |
| 114310 | 114681 | Fts3DeferredToken *pDeferred; /* Deferred token object for this token */ |
| | @@ -114419,10 +114790,11 @@ |
| 114419 | 114790 | #define FTS3_SEGMENT_REQUIRE_POS 0x00000001 |
| 114420 | 114791 | #define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002 |
| 114421 | 114792 | #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004 |
| 114422 | 114793 | #define FTS3_SEGMENT_PREFIX 0x00000008 |
| 114423 | 114794 | #define FTS3_SEGMENT_SCAN 0x00000010 |
| 114795 | +#define FTS3_SEGMENT_FIRST 0x00000020 |
| 114424 | 114796 | |
| 114425 | 114797 | /* Type passed as 4th argument to SegmentReaderIterate() */ |
| 114426 | 114798 | struct Fts3SegFilter { |
| 114427 | 114799 | const char *zTerm; |
| 114428 | 114800 | int nTerm; |
| | @@ -114458,12 +114830,12 @@ |
| 114458 | 114830 | SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *); |
| 114459 | 114831 | SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *); |
| 114460 | 114832 | SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64); |
| 114461 | 114833 | SQLITE_PRIVATE void sqlite3Fts3Dequote(char *); |
| 114462 | 114834 | SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*); |
| 114463 | | - |
| 114464 | 114835 | SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *); |
| 114836 | +SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *); |
| 114465 | 114837 | |
| 114466 | 114838 | /* fts3_tokenizer.c */ |
| 114467 | 114839 | SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *); |
| 114468 | 114840 | SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *); |
| 114469 | 114841 | SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, |
| | @@ -114478,11 +114850,11 @@ |
| 114478 | 114850 | ); |
| 114479 | 114851 | SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *); |
| 114480 | 114852 | |
| 114481 | 114853 | /* fts3_expr.c */ |
| 114482 | 114854 | SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, |
| 114483 | | - char **, int, int, const char *, int, Fts3Expr ** |
| 114855 | + char **, int, int, int, const char *, int, Fts3Expr ** |
| 114484 | 114856 | ); |
| 114485 | 114857 | SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *); |
| 114486 | 114858 | #ifdef SQLITE_TEST |
| 114487 | 114859 | SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db); |
| 114488 | 114860 | SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db); |
| | @@ -114649,11 +115021,11 @@ |
| 114649 | 115021 | char **pp, |
| 114650 | 115022 | char *pStart, |
| 114651 | 115023 | sqlite3_int64 *pVal |
| 114652 | 115024 | ){ |
| 114653 | 115025 | sqlite3_int64 iVal; |
| 114654 | | - char *p = *pp; |
| 115026 | + char *p; |
| 114655 | 115027 | |
| 114656 | 115028 | /* Pointer p now points at the first byte past the varint we are |
| 114657 | 115029 | ** interested in. So, unless the doclist is corrupt, the 0x80 bit is |
| 114658 | 115030 | ** clear on character p[-1]. */ |
| 114659 | 115031 | for(p = (*pp)-2; p>=pStart && *p&0x80; p--); |
| | @@ -114679,10 +115051,11 @@ |
| 114679 | 115051 | sqlite3_finalize(p->aStmt[i]); |
| 114680 | 115052 | } |
| 114681 | 115053 | sqlite3_free(p->zSegmentsTbl); |
| 114682 | 115054 | sqlite3_free(p->zReadExprlist); |
| 114683 | 115055 | sqlite3_free(p->zWriteExprlist); |
| 115056 | + sqlite3_free(p->zContentTbl); |
| 114684 | 115057 | |
| 114685 | 115058 | /* Invoke the tokenizer destructor to free the tokenizer. */ |
| 114686 | 115059 | p->pTokenizer->pModule->xDestroy(p->pTokenizer); |
| 114687 | 115060 | |
| 114688 | 115061 | sqlite3_free(p); |
| | @@ -114718,20 +115091,23 @@ |
| 114718 | 115091 | |
| 114719 | 115092 | /* |
| 114720 | 115093 | ** The xDestroy() virtual table method. |
| 114721 | 115094 | */ |
| 114722 | 115095 | static int fts3DestroyMethod(sqlite3_vtab *pVtab){ |
| 114723 | | - int rc = SQLITE_OK; /* Return code */ |
| 114724 | 115096 | Fts3Table *p = (Fts3Table *)pVtab; |
| 114725 | | - sqlite3 *db = p->db; |
| 115097 | + int rc = SQLITE_OK; /* Return code */ |
| 115098 | + const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */ |
| 115099 | + sqlite3 *db = p->db; /* Database handle */ |
| 114726 | 115100 | |
| 114727 | 115101 | /* Drop the shadow tables */ |
| 114728 | | - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", p->zDb, p->zName); |
| 114729 | | - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", p->zDb,p->zName); |
| 114730 | | - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", p->zDb, p->zName); |
| 114731 | | - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", p->zDb, p->zName); |
| 114732 | | - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", p->zDb, p->zName); |
| 115102 | + if( p->zContentTbl==0 ){ |
| 115103 | + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName); |
| 115104 | + } |
| 115105 | + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName); |
| 115106 | + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName); |
| 115107 | + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName); |
| 115108 | + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName); |
| 114733 | 115109 | |
| 114734 | 115110 | /* If everything has worked, invoke fts3DisconnectMethod() to free the |
| 114735 | 115111 | ** memory associated with the Fts3Table structure and return SQLITE_OK. |
| 114736 | 115112 | ** Otherwise, return an SQLite error code. |
| 114737 | 115113 | */ |
| | @@ -114789,27 +115165,31 @@ |
| 114789 | 115165 | ** %_stat tables required by FTS4. |
| 114790 | 115166 | */ |
| 114791 | 115167 | static int fts3CreateTables(Fts3Table *p){ |
| 114792 | 115168 | int rc = SQLITE_OK; /* Return code */ |
| 114793 | 115169 | int i; /* Iterator variable */ |
| 114794 | | - char *zContentCols; /* Columns of %_content table */ |
| 114795 | 115170 | sqlite3 *db = p->db; /* The database connection */ |
| 114796 | 115171 | |
| 114797 | | - /* Create a list of user columns for the content table */ |
| 114798 | | - zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY"); |
| 114799 | | - for(i=0; zContentCols && i<p->nColumn; i++){ |
| 114800 | | - char *z = p->azColumn[i]; |
| 114801 | | - zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z); |
| 114802 | | - } |
| 114803 | | - if( zContentCols==0 ) rc = SQLITE_NOMEM; |
| 114804 | | - |
| 114805 | | - /* Create the content table */ |
| 114806 | | - fts3DbExec(&rc, db, |
| 114807 | | - "CREATE TABLE %Q.'%q_content'(%s)", |
| 114808 | | - p->zDb, p->zName, zContentCols |
| 114809 | | - ); |
| 114810 | | - sqlite3_free(zContentCols); |
| 115172 | + if( p->zContentTbl==0 ){ |
| 115173 | + char *zContentCols; /* Columns of %_content table */ |
| 115174 | + |
| 115175 | + /* Create a list of user columns for the content table */ |
| 115176 | + zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY"); |
| 115177 | + for(i=0; zContentCols && i<p->nColumn; i++){ |
| 115178 | + char *z = p->azColumn[i]; |
| 115179 | + zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z); |
| 115180 | + } |
| 115181 | + if( zContentCols==0 ) rc = SQLITE_NOMEM; |
| 115182 | + |
| 115183 | + /* Create the content table */ |
| 115184 | + fts3DbExec(&rc, db, |
| 115185 | + "CREATE TABLE %Q.'%q_content'(%s)", |
| 115186 | + p->zDb, p->zName, zContentCols |
| 115187 | + ); |
| 115188 | + sqlite3_free(zContentCols); |
| 115189 | + } |
| 115190 | + |
| 114811 | 115191 | /* Create other tables */ |
| 114812 | 115192 | fts3DbExec(&rc, db, |
| 114813 | 115193 | "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);", |
| 114814 | 115194 | p->zDb, p->zName |
| 114815 | 115195 | ); |
| | @@ -114956,12 +115336,12 @@ |
| 114956 | 115336 | } |
| 114957 | 115337 | return zRet; |
| 114958 | 115338 | } |
| 114959 | 115339 | |
| 114960 | 115340 | /* |
| 114961 | | -** Return a list of comma separated SQL expressions that could be used |
| 114962 | | -** in a SELECT statement such as the following: |
| 115341 | +** Return a list of comma separated SQL expressions and a FROM clause that |
| 115342 | +** could be used in a SELECT statement such as the following: |
| 114963 | 115343 | ** |
| 114964 | 115344 | ** SELECT <list of expressions> FROM %_content AS x ... |
| 114965 | 115345 | ** |
| 114966 | 115346 | ** to return the docid, followed by each column of text data in order |
| 114967 | 115347 | ** from left to write. If parameter zFunc is not NULL, then instead of |
| | @@ -114968,11 +115348,11 @@ |
| 114968 | 115348 | ** being returned directly each column of text data is passed to an SQL |
| 114969 | 115349 | ** function named zFunc first. For example, if zFunc is "unzip" and the |
| 114970 | 115350 | ** table has the three user-defined columns "a", "b", and "c", the following |
| 114971 | 115351 | ** string is returned: |
| 114972 | 115352 | ** |
| 114973 | | -** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')" |
| 115353 | +** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x" |
| 114974 | 115354 | ** |
| 114975 | 115355 | ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It |
| 114976 | 115356 | ** is the responsibility of the caller to eventually free it. |
| 114977 | 115357 | ** |
| 114978 | 115358 | ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and |
| | @@ -114984,20 +115364,32 @@ |
| 114984 | 115364 | char *zRet = 0; |
| 114985 | 115365 | char *zFree = 0; |
| 114986 | 115366 | char *zFunction; |
| 114987 | 115367 | int i; |
| 114988 | 115368 | |
| 114989 | | - if( !zFunc ){ |
| 114990 | | - zFunction = ""; |
| 115369 | + if( p->zContentTbl==0 ){ |
| 115370 | + if( !zFunc ){ |
| 115371 | + zFunction = ""; |
| 115372 | + }else{ |
| 115373 | + zFree = zFunction = fts3QuoteId(zFunc); |
| 115374 | + } |
| 115375 | + fts3Appendf(pRc, &zRet, "docid"); |
| 115376 | + for(i=0; i<p->nColumn; i++){ |
| 115377 | + fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]); |
| 115378 | + } |
| 115379 | + sqlite3_free(zFree); |
| 114991 | 115380 | }else{ |
| 114992 | | - zFree = zFunction = fts3QuoteId(zFunc); |
| 115381 | + fts3Appendf(pRc, &zRet, "rowid"); |
| 115382 | + for(i=0; i<p->nColumn; i++){ |
| 115383 | + fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]); |
| 115384 | + } |
| 114993 | 115385 | } |
| 114994 | | - fts3Appendf(pRc, &zRet, "docid"); |
| 114995 | | - for(i=0; i<p->nColumn; i++){ |
| 114996 | | - fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]); |
| 114997 | | - } |
| 114998 | | - sqlite3_free(zFree); |
| 115386 | + fts3Appendf(pRc, &zRet, "FROM '%q'.'%q%s' AS x", |
| 115387 | + p->zDb, |
| 115388 | + (p->zContentTbl ? p->zContentTbl : p->zName), |
| 115389 | + (p->zContentTbl ? "" : "_content") |
| 115390 | + ); |
| 114999 | 115391 | return zRet; |
| 115000 | 115392 | } |
| 115001 | 115393 | |
| 115002 | 115394 | /* |
| 115003 | 115395 | ** Return a list of N comma separated question marks, where N is the number |
| | @@ -115050,11 +115442,11 @@ |
| 115050 | 115442 | ** the output value undefined. Otherwise SQLITE_OK is returned. |
| 115051 | 115443 | ** |
| 115052 | 115444 | ** This function is used when parsing the "prefix=" FTS4 parameter. |
| 115053 | 115445 | */ |
| 115054 | 115446 | static int fts3GobbleInt(const char **pp, int *pnOut){ |
| 115055 | | - const char *p = *pp; /* Iterator pointer */ |
| 115447 | + const char *p; /* Iterator pointer */ |
| 115056 | 115448 | int nInt = 0; /* Output value */ |
| 115057 | 115449 | |
| 115058 | 115450 | for(p=*pp; p[0]>='0' && p[0]<='9'; p++){ |
| 115059 | 115451 | nInt = nInt * 10 + (p[0] - '0'); |
| 115060 | 115452 | } |
| | @@ -115116,10 +115508,95 @@ |
| 115116 | 115508 | } |
| 115117 | 115509 | } |
| 115118 | 115510 | |
| 115119 | 115511 | return SQLITE_OK; |
| 115120 | 115512 | } |
| 115513 | + |
| 115514 | +/* |
| 115515 | +** This function is called when initializing an FTS4 table that uses the |
| 115516 | +** content=xxx option. It determines the number of and names of the columns |
| 115517 | +** of the new FTS4 table. |
| 115518 | +** |
| 115519 | +** The third argument passed to this function is the value passed to the |
| 115520 | +** config=xxx option (i.e. "xxx"). This function queries the database for |
| 115521 | +** a table of that name. If found, the output variables are populated |
| 115522 | +** as follows: |
| 115523 | +** |
| 115524 | +** *pnCol: Set to the number of columns table xxx has, |
| 115525 | +** |
| 115526 | +** *pnStr: Set to the total amount of space required to store a copy |
| 115527 | +** of each columns name, including the nul-terminator. |
| 115528 | +** |
| 115529 | +** *pazCol: Set to point to an array of *pnCol strings. Each string is |
| 115530 | +** the name of the corresponding column in table xxx. The array |
| 115531 | +** and its contents are allocated using a single allocation. It |
| 115532 | +** is the responsibility of the caller to free this allocation |
| 115533 | +** by eventually passing the *pazCol value to sqlite3_free(). |
| 115534 | +** |
| 115535 | +** If the table cannot be found, an error code is returned and the output |
| 115536 | +** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is |
| 115537 | +** returned (and the output variables are undefined). |
| 115538 | +*/ |
| 115539 | +static int fts3ContentColumns( |
| 115540 | + sqlite3 *db, /* Database handle */ |
| 115541 | + const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */ |
| 115542 | + const char *zTbl, /* Name of content table */ |
| 115543 | + const char ***pazCol, /* OUT: Malloc'd array of column names */ |
| 115544 | + int *pnCol, /* OUT: Size of array *pazCol */ |
| 115545 | + int *pnStr /* OUT: Bytes of string content */ |
| 115546 | +){ |
| 115547 | + int rc = SQLITE_OK; /* Return code */ |
| 115548 | + char *zSql; /* "SELECT *" statement on zTbl */ |
| 115549 | + sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */ |
| 115550 | + |
| 115551 | + zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl); |
| 115552 | + if( !zSql ){ |
| 115553 | + rc = SQLITE_NOMEM; |
| 115554 | + }else{ |
| 115555 | + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 115556 | + } |
| 115557 | + sqlite3_free(zSql); |
| 115558 | + |
| 115559 | + if( rc==SQLITE_OK ){ |
| 115560 | + const char **azCol; /* Output array */ |
| 115561 | + int nStr = 0; /* Size of all column names (incl. 0x00) */ |
| 115562 | + int nCol; /* Number of table columns */ |
| 115563 | + int i; /* Used to iterate through columns */ |
| 115564 | + |
| 115565 | + /* Loop through the returned columns. Set nStr to the number of bytes of |
| 115566 | + ** space required to store a copy of each column name, including the |
| 115567 | + ** nul-terminator byte. */ |
| 115568 | + nCol = sqlite3_column_count(pStmt); |
| 115569 | + for(i=0; i<nCol; i++){ |
| 115570 | + const char *zCol = sqlite3_column_name(pStmt, i); |
| 115571 | + nStr += strlen(zCol) + 1; |
| 115572 | + } |
| 115573 | + |
| 115574 | + /* Allocate and populate the array to return. */ |
| 115575 | + azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr); |
| 115576 | + if( azCol==0 ){ |
| 115577 | + rc = SQLITE_NOMEM; |
| 115578 | + }else{ |
| 115579 | + char *p = (char *)&azCol[nCol]; |
| 115580 | + for(i=0; i<nCol; i++){ |
| 115581 | + const char *zCol = sqlite3_column_name(pStmt, i); |
| 115582 | + int n = strlen(zCol)+1; |
| 115583 | + memcpy(p, zCol, n); |
| 115584 | + azCol[i] = p; |
| 115585 | + p += n; |
| 115586 | + } |
| 115587 | + } |
| 115588 | + sqlite3_finalize(pStmt); |
| 115589 | + |
| 115590 | + /* Set the output variables. */ |
| 115591 | + *pnCol = nCol; |
| 115592 | + *pnStr = nStr; |
| 115593 | + *pazCol = azCol; |
| 115594 | + } |
| 115595 | + |
| 115596 | + return rc; |
| 115597 | +} |
| 115121 | 115598 | |
| 115122 | 115599 | /* |
| 115123 | 115600 | ** This function is the implementation of both the xConnect and xCreate |
| 115124 | 115601 | ** methods of the FTS3 virtual table. |
| 115125 | 115602 | ** |
| | @@ -115161,10 +115638,11 @@ |
| 115161 | 115638 | int bNoDocsize = 0; /* True to omit %_docsize table */ |
| 115162 | 115639 | int bDescIdx = 0; /* True to store descending indexes */ |
| 115163 | 115640 | char *zPrefix = 0; /* Prefix parameter value (or NULL) */ |
| 115164 | 115641 | char *zCompress = 0; /* compress=? parameter (or NULL) */ |
| 115165 | 115642 | char *zUncompress = 0; /* uncompress=? parameter (or NULL) */ |
| 115643 | + char *zContent = 0; /* content=? parameter (or NULL) */ |
| 115166 | 115644 | |
| 115167 | 115645 | assert( strlen(argv[0])==4 ); |
| 115168 | 115646 | assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4) |
| 115169 | 115647 | || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4) |
| 115170 | 115648 | ); |
| | @@ -115204,17 +115682,17 @@ |
| 115204 | 115682 | /* Check if it is an FTS4 special argument. */ |
| 115205 | 115683 | else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){ |
| 115206 | 115684 | struct Fts4Option { |
| 115207 | 115685 | const char *zOpt; |
| 115208 | 115686 | int nOpt; |
| 115209 | | - char **pzVar; |
| 115210 | 115687 | } aFts4Opt[] = { |
| 115211 | | - { "matchinfo", 9, 0 }, /* 0 -> MATCHINFO */ |
| 115212 | | - { "prefix", 6, 0 }, /* 1 -> PREFIX */ |
| 115213 | | - { "compress", 8, 0 }, /* 2 -> COMPRESS */ |
| 115214 | | - { "uncompress", 10, 0 }, /* 3 -> UNCOMPRESS */ |
| 115215 | | - { "order", 5, 0 } /* 4 -> ORDER */ |
| 115688 | + { "matchinfo", 9 }, /* 0 -> MATCHINFO */ |
| 115689 | + { "prefix", 6 }, /* 1 -> PREFIX */ |
| 115690 | + { "compress", 8 }, /* 2 -> COMPRESS */ |
| 115691 | + { "uncompress", 10 }, /* 3 -> UNCOMPRESS */ |
| 115692 | + { "order", 5 }, /* 4 -> ORDER */ |
| 115693 | + { "content", 7 } /* 5 -> CONTENT */ |
| 115216 | 115694 | }; |
| 115217 | 115695 | |
| 115218 | 115696 | int iOpt; |
| 115219 | 115697 | if( !zVal ){ |
| 115220 | 115698 | rc = SQLITE_NOMEM; |
| | @@ -115256,17 +115734,24 @@ |
| 115256 | 115734 | zVal = 0; |
| 115257 | 115735 | break; |
| 115258 | 115736 | |
| 115259 | 115737 | case 4: /* ORDER */ |
| 115260 | 115738 | if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) |
| 115261 | | - && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 3)) |
| 115739 | + && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) |
| 115262 | 115740 | ){ |
| 115263 | 115741 | *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal); |
| 115264 | 115742 | rc = SQLITE_ERROR; |
| 115265 | 115743 | } |
| 115266 | 115744 | bDescIdx = (zVal[0]=='d' || zVal[0]=='D'); |
| 115267 | 115745 | break; |
| 115746 | + |
| 115747 | + default: /* CONTENT */ |
| 115748 | + assert( iOpt==5 ); |
| 115749 | + sqlite3_free(zUncompress); |
| 115750 | + zContent = zVal; |
| 115751 | + zVal = 0; |
| 115752 | + break; |
| 115268 | 115753 | } |
| 115269 | 115754 | } |
| 115270 | 115755 | sqlite3_free(zVal); |
| 115271 | 115756 | } |
| 115272 | 115757 | } |
| | @@ -115275,10 +115760,30 @@ |
| 115275 | 115760 | else { |
| 115276 | 115761 | nString += (int)(strlen(z) + 1); |
| 115277 | 115762 | aCol[nCol++] = z; |
| 115278 | 115763 | } |
| 115279 | 115764 | } |
| 115765 | + |
| 115766 | + /* If a content=xxx option was specified, the following: |
| 115767 | + ** |
| 115768 | + ** 1. Ignore any compress= and uncompress= options. |
| 115769 | + ** |
| 115770 | + ** 2. If no column names were specified as part of the CREATE VIRTUAL |
| 115771 | + ** TABLE statement, use all columns from the content table. |
| 115772 | + */ |
| 115773 | + if( rc==SQLITE_OK && zContent ){ |
| 115774 | + sqlite3_free(zCompress); |
| 115775 | + sqlite3_free(zUncompress); |
| 115776 | + zCompress = 0; |
| 115777 | + zUncompress = 0; |
| 115778 | + if( nCol==0 ){ |
| 115779 | + sqlite3_free((void*)aCol); |
| 115780 | + aCol = 0; |
| 115781 | + rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString); |
| 115782 | + } |
| 115783 | + assert( rc!=SQLITE_OK || nCol>0 ); |
| 115784 | + } |
| 115280 | 115785 | if( rc!=SQLITE_OK ) goto fts3_init_out; |
| 115281 | 115786 | |
| 115282 | 115787 | if( nCol==0 ){ |
| 115283 | 115788 | assert( nString==0 ); |
| 115284 | 115789 | aCol[0] = "content"; |
| | @@ -115319,10 +115824,12 @@ |
| 115319 | 115824 | p->pTokenizer = pTokenizer; |
| 115320 | 115825 | p->nMaxPendingData = FTS3_MAX_PENDING_DATA; |
| 115321 | 115826 | p->bHasDocsize = (isFts4 && bNoDocsize==0); |
| 115322 | 115827 | p->bHasStat = isFts4; |
| 115323 | 115828 | p->bDescIdx = bDescIdx; |
| 115829 | + p->zContentTbl = zContent; |
| 115830 | + zContent = 0; |
| 115324 | 115831 | TESTONLY( p->inTransaction = -1 ); |
| 115325 | 115832 | TESTONLY( p->mxSavepoint = -1 ); |
| 115326 | 115833 | |
| 115327 | 115834 | p->aIndex = (struct Fts3Index *)&p->azColumn[nCol]; |
| 115328 | 115835 | memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex); |
| | @@ -115380,10 +115887,11 @@ |
| 115380 | 115887 | fts3_init_out: |
| 115381 | 115888 | sqlite3_free(zPrefix); |
| 115382 | 115889 | sqlite3_free(aIndex); |
| 115383 | 115890 | sqlite3_free(zCompress); |
| 115384 | 115891 | sqlite3_free(zUncompress); |
| 115892 | + sqlite3_free(zContent); |
| 115385 | 115893 | sqlite3_free((void *)aCol); |
| 115386 | 115894 | if( rc!=SQLITE_OK ){ |
| 115387 | 115895 | if( p ){ |
| 115388 | 115896 | fts3DisconnectMethod((sqlite3_vtab *)p); |
| 115389 | 115897 | }else if( pTokenizer ){ |
| | @@ -115530,40 +116038,69 @@ |
| 115530 | 116038 | sqlite3_free(pCsr->aMatchinfo); |
| 115531 | 116039 | assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| 115532 | 116040 | sqlite3_free(pCsr); |
| 115533 | 116041 | return SQLITE_OK; |
| 115534 | 116042 | } |
| 116043 | + |
| 116044 | +/* |
| 116045 | +** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then |
| 116046 | +** compose and prepare an SQL statement of the form: |
| 116047 | +** |
| 116048 | +** "SELECT <columns> FROM %_content WHERE rowid = ?" |
| 116049 | +** |
| 116050 | +** (or the equivalent for a content=xxx table) and set pCsr->pStmt to |
| 116051 | +** it. If an error occurs, return an SQLite error code. |
| 116052 | +** |
| 116053 | +** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK. |
| 116054 | +*/ |
| 116055 | +static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){ |
| 116056 | + int rc = SQLITE_OK; |
| 116057 | + if( pCsr->pStmt==0 ){ |
| 116058 | + Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; |
| 116059 | + char *zSql; |
| 116060 | + zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist); |
| 116061 | + if( !zSql ) return SQLITE_NOMEM; |
| 116062 | + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); |
| 116063 | + sqlite3_free(zSql); |
| 116064 | + } |
| 116065 | + *ppStmt = pCsr->pStmt; |
| 116066 | + return rc; |
| 116067 | +} |
| 115535 | 116068 | |
| 115536 | 116069 | /* |
| 115537 | 116070 | ** Position the pCsr->pStmt statement so that it is on the row |
| 115538 | 116071 | ** of the %_content table that contains the last match. Return |
| 115539 | 116072 | ** SQLITE_OK on success. |
| 115540 | 116073 | */ |
| 115541 | 116074 | static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ |
| 116075 | + int rc = SQLITE_OK; |
| 115542 | 116076 | if( pCsr->isRequireSeek ){ |
| 115543 | | - sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); |
| 115544 | | - pCsr->isRequireSeek = 0; |
| 115545 | | - if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ |
| 115546 | | - return SQLITE_OK; |
| 115547 | | - }else{ |
| 115548 | | - int rc = sqlite3_reset(pCsr->pStmt); |
| 115549 | | - if( rc==SQLITE_OK ){ |
| 115550 | | - /* If no row was found and no error has occured, then the %_content |
| 115551 | | - ** table is missing a row that is present in the full-text index. |
| 115552 | | - ** The data structures are corrupt. |
| 115553 | | - */ |
| 115554 | | - rc = SQLITE_CORRUPT_VTAB; |
| 115555 | | - } |
| 115556 | | - pCsr->isEof = 1; |
| 115557 | | - if( pContext ){ |
| 115558 | | - sqlite3_result_error_code(pContext, rc); |
| 115559 | | - } |
| 115560 | | - return rc; |
| 115561 | | - } |
| 115562 | | - }else{ |
| 115563 | | - return SQLITE_OK; |
| 115564 | | - } |
| 116077 | + sqlite3_stmt *pStmt = 0; |
| 116078 | + |
| 116079 | + rc = fts3CursorSeekStmt(pCsr, &pStmt); |
| 116080 | + if( rc==SQLITE_OK ){ |
| 116081 | + sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); |
| 116082 | + pCsr->isRequireSeek = 0; |
| 116083 | + if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ |
| 116084 | + return SQLITE_OK; |
| 116085 | + }else{ |
| 116086 | + rc = sqlite3_reset(pCsr->pStmt); |
| 116087 | + if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){ |
| 116088 | + /* If no row was found and no error has occured, then the %_content |
| 116089 | + ** table is missing a row that is present in the full-text index. |
| 116090 | + ** The data structures are corrupt. */ |
| 116091 | + rc = FTS_CORRUPT_VTAB; |
| 116092 | + pCsr->isEof = 1; |
| 116093 | + } |
| 116094 | + } |
| 116095 | + } |
| 116096 | + } |
| 116097 | + |
| 116098 | + if( rc!=SQLITE_OK && pContext ){ |
| 116099 | + sqlite3_result_error_code(pContext, rc); |
| 116100 | + } |
| 116101 | + return rc; |
| 115565 | 116102 | } |
| 115566 | 116103 | |
| 115567 | 116104 | /* |
| 115568 | 116105 | ** This function is used to process a single interior node when searching |
| 115569 | 116106 | ** a b-tree for a term or term prefix. The node data is passed to this |
| | @@ -115609,11 +116146,11 @@ |
| 115609 | 116146 | ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). |
| 115610 | 116147 | */ |
| 115611 | 116148 | zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); |
| 115612 | 116149 | zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); |
| 115613 | 116150 | if( zCsr>zEnd ){ |
| 115614 | | - return SQLITE_CORRUPT_VTAB; |
| 116151 | + return FTS_CORRUPT_VTAB; |
| 115615 | 116152 | } |
| 115616 | 116153 | |
| 115617 | 116154 | while( zCsr<zEnd && (piFirst || piLast) ){ |
| 115618 | 116155 | int cmp; /* memcmp() result */ |
| 115619 | 116156 | int nSuffix; /* Size of term suffix */ |
| | @@ -115627,11 +116164,11 @@ |
| 115627 | 116164 | } |
| 115628 | 116165 | isFirstTerm = 0; |
| 115629 | 116166 | zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix); |
| 115630 | 116167 | |
| 115631 | 116168 | if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){ |
| 115632 | | - rc = SQLITE_CORRUPT_VTAB; |
| 116169 | + rc = FTS_CORRUPT_VTAB; |
| 115633 | 116170 | goto finish_scan; |
| 115634 | 116171 | } |
| 115635 | 116172 | if( nPrefix+nSuffix>nAlloc ){ |
| 115636 | 116173 | char *zNew; |
| 115637 | 116174 | nAlloc = (nPrefix+nSuffix) * 2; |
| | @@ -115640,10 +116177,11 @@ |
| 115640 | 116177 | rc = SQLITE_NOMEM; |
| 115641 | 116178 | goto finish_scan; |
| 115642 | 116179 | } |
| 115643 | 116180 | zBuffer = zNew; |
| 115644 | 116181 | } |
| 116182 | + assert( zBuffer ); |
| 115645 | 116183 | memcpy(&zBuffer[nPrefix], zCsr, nSuffix); |
| 115646 | 116184 | nBuffer = nPrefix + nSuffix; |
| 115647 | 116185 | zCsr += nSuffix; |
| 115648 | 116186 | |
| 115649 | 116187 | /* Compare the term we are searching for with the term just loaded from |
| | @@ -115998,20 +116536,20 @@ |
| 115998 | 116536 | int isSaveLeft, /* Save the left position */ |
| 115999 | 116537 | int isExact, /* If *pp1 is exactly nTokens before *pp2 */ |
| 116000 | 116538 | char **pp1, /* IN/OUT: Left input list */ |
| 116001 | 116539 | char **pp2 /* IN/OUT: Right input list */ |
| 116002 | 116540 | ){ |
| 116003 | | - char *p = (pp ? *pp : 0); |
| 116541 | + char *p = *pp; |
| 116004 | 116542 | char *p1 = *pp1; |
| 116005 | 116543 | char *p2 = *pp2; |
| 116006 | 116544 | int iCol1 = 0; |
| 116007 | 116545 | int iCol2 = 0; |
| 116008 | 116546 | |
| 116009 | 116547 | /* Never set both isSaveLeft and isExact for the same invocation. */ |
| 116010 | 116548 | assert( isSaveLeft==0 || isExact==0 ); |
| 116011 | 116549 | |
| 116012 | | - assert( *p1!=0 && *p2!=0 ); |
| 116550 | + assert( p!=0 && *p1!=0 && *p2!=0 ); |
| 116013 | 116551 | if( *p1==POS_COLUMN ){ |
| 116014 | 116552 | p1++; |
| 116015 | 116553 | p1 += sqlite3Fts3GetVarint32(p1, &iCol1); |
| 116016 | 116554 | } |
| 116017 | 116555 | if( *p2==POS_COLUMN ){ |
| | @@ -116024,11 +116562,11 @@ |
| 116024 | 116562 | char *pSave = p; |
| 116025 | 116563 | sqlite3_int64 iPrev = 0; |
| 116026 | 116564 | sqlite3_int64 iPos1 = 0; |
| 116027 | 116565 | sqlite3_int64 iPos2 = 0; |
| 116028 | 116566 | |
| 116029 | | - if( pp && iCol1 ){ |
| 116567 | + if( iCol1 ){ |
| 116030 | 116568 | *p++ = POS_COLUMN; |
| 116031 | 116569 | p += sqlite3Fts3PutVarint(p, iCol1); |
| 116032 | 116570 | } |
| 116033 | 116571 | |
| 116034 | 116572 | assert( *p1!=POS_END && *p1!=POS_COLUMN ); |
| | @@ -116039,20 +116577,14 @@ |
| 116039 | 116577 | while( 1 ){ |
| 116040 | 116578 | if( iPos2==iPos1+nToken |
| 116041 | 116579 | || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) |
| 116042 | 116580 | ){ |
| 116043 | 116581 | sqlite3_int64 iSave; |
| 116044 | | - if( !pp ){ |
| 116045 | | - fts3PoslistCopy(0, &p2); |
| 116046 | | - fts3PoslistCopy(0, &p1); |
| 116047 | | - *pp1 = p1; |
| 116048 | | - *pp2 = p2; |
| 116049 | | - return 1; |
| 116050 | | - } |
| 116051 | 116582 | iSave = isSaveLeft ? iPos1 : iPos2; |
| 116052 | 116583 | fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2; |
| 116053 | 116584 | pSave = 0; |
| 116585 | + assert( p ); |
| 116054 | 116586 | } |
| 116055 | 116587 | if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){ |
| 116056 | 116588 | if( (*p2&0xFE)==0 ) break; |
| 116057 | 116589 | fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; |
| 116058 | 116590 | }else{ |
| | @@ -116097,11 +116629,11 @@ |
| 116097 | 116629 | |
| 116098 | 116630 | fts3PoslistCopy(0, &p2); |
| 116099 | 116631 | fts3PoslistCopy(0, &p1); |
| 116100 | 116632 | *pp1 = p1; |
| 116101 | 116633 | *pp2 = p2; |
| 116102 | | - if( !pp || *pp==p ){ |
| 116634 | + if( *pp==p ){ |
| 116103 | 116635 | return 0; |
| 116104 | 116636 | } |
| 116105 | 116637 | *p++ = 0x00; |
| 116106 | 116638 | *pp = p; |
| 116107 | 116639 | return 1; |
| | @@ -116399,10 +116931,60 @@ |
| 116399 | 116931 | } |
| 116400 | 116932 | } |
| 116401 | 116933 | |
| 116402 | 116934 | *pnRight = p - aOut; |
| 116403 | 116935 | } |
| 116936 | + |
| 116937 | +/* |
| 116938 | +** Argument pList points to a position list nList bytes in size. This |
| 116939 | +** function checks to see if the position list contains any entries for |
| 116940 | +** a token in position 0 (of any column). If so, it writes argument iDelta |
| 116941 | +** to the output buffer pOut, followed by a position list consisting only |
| 116942 | +** of the entries from pList at position 0, and terminated by an 0x00 byte. |
| 116943 | +** The value returned is the number of bytes written to pOut (if any). |
| 116944 | +*/ |
| 116945 | +SQLITE_PRIVATE int sqlite3Fts3FirstFilter( |
| 116946 | + sqlite3_int64 iDelta, /* Varint that may be written to pOut */ |
| 116947 | + char *pList, /* Position list (no 0x00 term) */ |
| 116948 | + int nList, /* Size of pList in bytes */ |
| 116949 | + char *pOut /* Write output here */ |
| 116950 | +){ |
| 116951 | + int nOut = 0; |
| 116952 | + int bWritten = 0; /* True once iDelta has been written */ |
| 116953 | + char *p = pList; |
| 116954 | + char *pEnd = &pList[nList]; |
| 116955 | + |
| 116956 | + if( *p!=0x01 ){ |
| 116957 | + if( *p==0x02 ){ |
| 116958 | + nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta); |
| 116959 | + pOut[nOut++] = 0x02; |
| 116960 | + bWritten = 1; |
| 116961 | + } |
| 116962 | + fts3ColumnlistCopy(0, &p); |
| 116963 | + } |
| 116964 | + |
| 116965 | + while( p<pEnd && *p==0x01 ){ |
| 116966 | + sqlite3_int64 iCol; |
| 116967 | + p++; |
| 116968 | + p += sqlite3Fts3GetVarint(p, &iCol); |
| 116969 | + if( *p==0x02 ){ |
| 116970 | + if( bWritten==0 ){ |
| 116971 | + nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta); |
| 116972 | + bWritten = 1; |
| 116973 | + } |
| 116974 | + pOut[nOut++] = 0x01; |
| 116975 | + nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol); |
| 116976 | + pOut[nOut++] = 0x02; |
| 116977 | + } |
| 116978 | + fts3ColumnlistCopy(0, &p); |
| 116979 | + } |
| 116980 | + if( bWritten ){ |
| 116981 | + pOut[nOut++] = 0x00; |
| 116982 | + } |
| 116983 | + |
| 116984 | + return nOut; |
| 116985 | +} |
| 116404 | 116986 | |
| 116405 | 116987 | |
| 116406 | 116988 | /* |
| 116407 | 116989 | ** Merge all doclists in the TermSelect.aaOutput[] array into a single |
| 116408 | 116990 | ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all |
| | @@ -116756,10 +117338,11 @@ |
| 116756 | 117338 | pSegcsr = pTok->pSegcsr; |
| 116757 | 117339 | memset(&tsc, 0, sizeof(TermSelect)); |
| 116758 | 117340 | |
| 116759 | 117341 | filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS |
| 116760 | 117342 | | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0) |
| 117343 | + | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0) |
| 116761 | 117344 | | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0); |
| 116762 | 117345 | filter.iCol = iColumn; |
| 116763 | 117346 | filter.zTerm = pTok->z; |
| 116764 | 117347 | filter.nTerm = pTok->n; |
| 116765 | 117348 | |
| | @@ -116896,12 +117479,12 @@ |
| 116896 | 117479 | |
| 116897 | 117480 | if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ |
| 116898 | 117481 | return SQLITE_NOMEM; |
| 116899 | 117482 | } |
| 116900 | 117483 | |
| 116901 | | - rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn, |
| 116902 | | - iCol, zQuery, -1, &pCsr->pExpr |
| 117484 | + rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->bHasStat, |
| 117485 | + p->nColumn, iCol, zQuery, -1, &pCsr->pExpr |
| 116903 | 117486 | ); |
| 116904 | 117487 | if( rc!=SQLITE_OK ){ |
| 116905 | 117488 | if( rc==SQLITE_ERROR ){ |
| 116906 | 117489 | static const char *zErr = "malformed MATCH expression: [%s]"; |
| 116907 | 117490 | p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery); |
| | @@ -116924,26 +117507,27 @@ |
| 116924 | 117507 | ** statement loops through all rows of the %_content table. For a |
| 116925 | 117508 | ** full-text query or docid lookup, the statement retrieves a single |
| 116926 | 117509 | ** row by docid. |
| 116927 | 117510 | */ |
| 116928 | 117511 | if( idxNum==FTS3_FULLSCAN_SEARCH ){ |
| 116929 | | - const char *zSort = (pCsr->bDesc ? "DESC" : "ASC"); |
| 116930 | | - const char *zTmpl = "SELECT %s FROM %Q.'%q_content' AS x ORDER BY docid %s"; |
| 116931 | | - zSql = sqlite3_mprintf(zTmpl, p->zReadExprlist, p->zDb, p->zName, zSort); |
| 116932 | | - }else{ |
| 116933 | | - const char *zTmpl = "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?"; |
| 116934 | | - zSql = sqlite3_mprintf(zTmpl, p->zReadExprlist, p->zDb, p->zName); |
| 116935 | | - } |
| 116936 | | - if( !zSql ) return SQLITE_NOMEM; |
| 116937 | | - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); |
| 116938 | | - sqlite3_free(zSql); |
| 116939 | | - if( rc!=SQLITE_OK ) return rc; |
| 116940 | | - |
| 116941 | | - if( idxNum==FTS3_DOCID_SEARCH ){ |
| 116942 | | - rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]); |
| 116943 | | - if( rc!=SQLITE_OK ) return rc; |
| 116944 | | - } |
| 117512 | + zSql = sqlite3_mprintf( |
| 117513 | + "SELECT %s ORDER BY rowid %s", |
| 117514 | + p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC") |
| 117515 | + ); |
| 117516 | + if( zSql ){ |
| 117517 | + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); |
| 117518 | + sqlite3_free(zSql); |
| 117519 | + }else{ |
| 117520 | + rc = SQLITE_NOMEM; |
| 117521 | + } |
| 117522 | + }else if( idxNum==FTS3_DOCID_SEARCH ){ |
| 117523 | + rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt); |
| 117524 | + if( rc==SQLITE_OK ){ |
| 117525 | + rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]); |
| 117526 | + } |
| 117527 | + } |
| 117528 | + if( rc!=SQLITE_OK ) return rc; |
| 116945 | 117529 | |
| 116946 | 117530 | return fts3NextMethod(pCursor); |
| 116947 | 117531 | } |
| 116948 | 117532 | |
| 116949 | 117533 | /* |
| | @@ -116992,11 +117576,11 @@ |
| 116992 | 117576 | ** Return a blob which is a pointer to the cursor. |
| 116993 | 117577 | */ |
| 116994 | 117578 | sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT); |
| 116995 | 117579 | }else{ |
| 116996 | 117580 | rc = fts3CursorSeek(0, pCsr); |
| 116997 | | - if( rc==SQLITE_OK ){ |
| 117581 | + if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){ |
| 116998 | 117582 | sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1)); |
| 116999 | 117583 | } |
| 117000 | 117584 | } |
| 117001 | 117585 | |
| 117002 | 117586 | assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); |
| | @@ -117076,11 +117660,11 @@ |
| 117076 | 117660 | ** moves *ppPoslist so that it instead points to the first byte of the |
| 117077 | 117661 | ** same position list. |
| 117078 | 117662 | */ |
| 117079 | 117663 | static void fts3ReversePoslist(char *pStart, char **ppPoslist){ |
| 117080 | 117664 | char *p = &(*ppPoslist)[-2]; |
| 117081 | | - char c; |
| 117665 | + char c = 0; |
| 117082 | 117666 | |
| 117083 | 117667 | while( p>pStart && (c=*p--)==0 ); |
| 117084 | 117668 | while( p>pStart && (*p & 0x80) | c ){ |
| 117085 | 117669 | c = *p--; |
| 117086 | 117670 | } |
| | @@ -117285,19 +117869,26 @@ |
| 117285 | 117869 | ){ |
| 117286 | 117870 | Fts3Table *p = (Fts3Table *)pVtab; |
| 117287 | 117871 | sqlite3 *db = p->db; /* Database connection */ |
| 117288 | 117872 | int rc; /* Return Code */ |
| 117289 | 117873 | |
| 117874 | + /* As it happens, the pending terms table is always empty here. This is |
| 117875 | + ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction |
| 117876 | + ** always opens a savepoint transaction. And the xSavepoint() method |
| 117877 | + ** flushes the pending terms table. But leave the (no-op) call to |
| 117878 | + ** PendingTermsFlush() in in case that changes. |
| 117879 | + */ |
| 117880 | + assert( p->nPendingData==0 ); |
| 117290 | 117881 | rc = sqlite3Fts3PendingTermsFlush(p); |
| 117291 | | - if( rc!=SQLITE_OK ){ |
| 117292 | | - return rc; |
| 117882 | + |
| 117883 | + if( p->zContentTbl==0 ){ |
| 117884 | + fts3DbExec(&rc, db, |
| 117885 | + "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", |
| 117886 | + p->zDb, p->zName, zName |
| 117887 | + ); |
| 117293 | 117888 | } |
| 117294 | 117889 | |
| 117295 | | - fts3DbExec(&rc, db, |
| 117296 | | - "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", |
| 117297 | | - p->zDb, p->zName, zName |
| 117298 | | - ); |
| 117299 | 117890 | if( p->bHasDocsize ){ |
| 117300 | 117891 | fts3DbExec(&rc, db, |
| 117301 | 117892 | "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';", |
| 117302 | 117893 | p->zDb, p->zName, zName |
| 117303 | 117894 | ); |
| | @@ -117652,25 +118243,24 @@ |
| 117652 | 118243 | ** |
| 117653 | 118244 | ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code. |
| 117654 | 118245 | */ |
| 117655 | 118246 | static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){ |
| 117656 | 118247 | int iToken; /* Used to iterate through phrase tokens */ |
| 117657 | | - int rc = SQLITE_OK; /* Return code */ |
| 117658 | 118248 | char *aPoslist = 0; /* Position list for deferred tokens */ |
| 117659 | 118249 | int nPoslist = 0; /* Number of bytes in aPoslist */ |
| 117660 | 118250 | int iPrev = -1; /* Token number of previous deferred token */ |
| 117661 | 118251 | |
| 117662 | 118252 | assert( pPhrase->doclist.bFreeList==0 ); |
| 117663 | 118253 | |
| 117664 | | - for(iToken=0; rc==SQLITE_OK && iToken<pPhrase->nToken; iToken++){ |
| 118254 | + for(iToken=0; iToken<pPhrase->nToken; iToken++){ |
| 117665 | 118255 | Fts3PhraseToken *pToken = &pPhrase->aToken[iToken]; |
| 117666 | 118256 | Fts3DeferredToken *pDeferred = pToken->pDeferred; |
| 117667 | 118257 | |
| 117668 | 118258 | if( pDeferred ){ |
| 117669 | 118259 | char *pList; |
| 117670 | 118260 | int nList; |
| 117671 | | - rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList); |
| 118261 | + int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList); |
| 117672 | 118262 | if( rc!=SQLITE_OK ) return rc; |
| 117673 | 118263 | |
| 117674 | 118264 | if( pList==0 ){ |
| 117675 | 118265 | sqlite3_free(aPoslist); |
| 117676 | 118266 | pPhrase->doclist.pList = 0; |
| | @@ -117767,10 +118357,11 @@ |
| 117767 | 118357 | if( pCsr->bDesc==pTab->bDescIdx |
| 117768 | 118358 | && bOptOk==1 |
| 117769 | 118359 | && p->nToken==1 |
| 117770 | 118360 | && pFirst->pSegcsr |
| 117771 | 118361 | && pFirst->pSegcsr->bLookup |
| 118362 | + && pFirst->bFirst==0 |
| 117772 | 118363 | ){ |
| 117773 | 118364 | /* Use the incremental approach. */ |
| 117774 | 118365 | int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn); |
| 117775 | 118366 | rc = sqlite3Fts3MsrIncrStart( |
| 117776 | 118367 | pTab, pFirst->pSegcsr, iCol, pFirst->z, pFirst->n); |
| | @@ -117996,11 +118587,11 @@ |
| 117996 | 118587 | Fts3Expr *pExpr, /* Expression to consider */ |
| 117997 | 118588 | Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */ |
| 117998 | 118589 | Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */ |
| 117999 | 118590 | int *pRc /* IN/OUT: Error code */ |
| 118000 | 118591 | ){ |
| 118001 | | - if( *pRc==SQLITE_OK && pExpr ){ |
| 118592 | + if( *pRc==SQLITE_OK ){ |
| 118002 | 118593 | if( pExpr->eType==FTSQUERY_PHRASE ){ |
| 118003 | 118594 | Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 118004 | 118595 | int i; |
| 118005 | 118596 | for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){ |
| 118006 | 118597 | Fts3TokenAndCost *pTC = (*ppTC)++; |
| | @@ -118010,10 +118601,15 @@ |
| 118010 | 118601 | pTC->pToken = &pPhrase->aToken[i]; |
| 118011 | 118602 | pTC->iCol = pPhrase->iColumn; |
| 118012 | 118603 | *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl); |
| 118013 | 118604 | } |
| 118014 | 118605 | }else if( pExpr->eType!=FTSQUERY_NOT ){ |
| 118606 | + assert( pExpr->eType==FTSQUERY_OR |
| 118607 | + || pExpr->eType==FTSQUERY_AND |
| 118608 | + || pExpr->eType==FTSQUERY_NEAR |
| 118609 | + ); |
| 118610 | + assert( pExpr->pLeft && pExpr->pRight ); |
| 118015 | 118611 | if( pExpr->eType==FTSQUERY_OR ){ |
| 118016 | 118612 | pRoot = pExpr->pLeft; |
| 118017 | 118613 | **ppOr = pRoot; |
| 118018 | 118614 | (*ppOr)++; |
| 118019 | 118615 | } |
| | @@ -118070,11 +118666,11 @@ |
| 118070 | 118666 | while( a<pEnd ){ |
| 118071 | 118667 | a += sqlite3Fts3GetVarint(a, &nByte); |
| 118072 | 118668 | } |
| 118073 | 118669 | if( nDoc==0 || nByte==0 ){ |
| 118074 | 118670 | sqlite3_reset(pStmt); |
| 118075 | | - return SQLITE_CORRUPT_VTAB; |
| 118671 | + return FTS_CORRUPT_VTAB; |
| 118076 | 118672 | } |
| 118077 | 118673 | |
| 118078 | 118674 | pCsr->nDoc = nDoc; |
| 118079 | 118675 | pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz); |
| 118080 | 118676 | assert( pCsr->nRowAvg>0 ); |
| | @@ -118113,10 +118709,19 @@ |
| 118113 | 118709 | int nOvfl = 0; /* Total overflow pages used by doclists */ |
| 118114 | 118710 | int nToken = 0; /* Total number of tokens in cluster */ |
| 118115 | 118711 | |
| 118116 | 118712 | int nMinEst = 0; /* The minimum count for any phrase so far. */ |
| 118117 | 118713 | int nLoad4 = 1; /* (Phrases that will be loaded)^4. */ |
| 118714 | + |
| 118715 | + /* Tokens are never deferred for FTS tables created using the content=xxx |
| 118716 | + ** option. The reason being that it is not guaranteed that the content |
| 118717 | + ** table actually contains the same data as the index. To prevent this from |
| 118718 | + ** causing any problems, the deferred token optimization is completely |
| 118719 | + ** disabled for content=xxx tables. */ |
| 118720 | + if( pTab->zContentTbl ){ |
| 118721 | + return SQLITE_OK; |
| 118722 | + } |
| 118118 | 118723 | |
| 118119 | 118724 | /* Count the tokens in this AND/NEAR cluster. If none of the doclists |
| 118120 | 118725 | ** associated with the tokens spill onto overflow pages, or if there is |
| 118121 | 118726 | ** only 1 token, exit early. No tokens to defer in this case. */ |
| 118122 | 118727 | for(ii=0; ii<nTC; ii++){ |
| | @@ -118176,11 +118781,15 @@ |
| 118176 | 118781 | Fts3PhraseToken *pToken = pTC->pToken; |
| 118177 | 118782 | rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol); |
| 118178 | 118783 | fts3SegReaderCursorFree(pToken->pSegcsr); |
| 118179 | 118784 | pToken->pSegcsr = 0; |
| 118180 | 118785 | }else{ |
| 118181 | | - nLoad4 = nLoad4*4; |
| 118786 | + /* Set nLoad4 to the value of (4^nOther) for the next iteration of the |
| 118787 | + ** for-loop. Except, limit the value to 2^24 to prevent it from |
| 118788 | + ** overflowing the 32-bit integer it is stored in. */ |
| 118789 | + if( ii<12 ) nLoad4 = nLoad4*4; |
| 118790 | + |
| 118182 | 118791 | if( ii==0 || pTC->pPhrase->nToken>1 ){ |
| 118183 | 118792 | /* Either this is the cheapest token in the entire query, or it is |
| 118184 | 118793 | ** part of a multi-token phrase. Either way, the entire doclist will |
| 118185 | 118794 | ** (eventually) be loaded into memory. It may as well be now. */ |
| 118186 | 118795 | Fts3PhraseToken *pToken = pTC->pToken; |
| | @@ -118546,12 +119155,15 @@ |
| 118546 | 119155 | } |
| 118547 | 119156 | |
| 118548 | 119157 | aPoslist = pExpr->pRight->pPhrase->doclist.pList; |
| 118549 | 119158 | nToken = pExpr->pRight->pPhrase->nToken; |
| 118550 | 119159 | for(p=pExpr->pLeft; p && res; p=p->pLeft){ |
| 118551 | | - int nNear = p->pParent->nNear; |
| 118552 | | - Fts3Phrase *pPhrase = ( |
| 119160 | + int nNear; |
| 119161 | + Fts3Phrase *pPhrase; |
| 119162 | + assert( p->pParent && p->pParent->pLeft==p ); |
| 119163 | + nNear = p->pParent->nNear; |
| 119164 | + pPhrase = ( |
| 118553 | 119165 | p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase |
| 118554 | 119166 | ); |
| 118555 | 119167 | res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); |
| 118556 | 119168 | } |
| 118557 | 119169 | } |
| | @@ -119037,10 +119649,19 @@ |
| 119037 | 119649 | pPhrase->aToken[i].pSegcsr = 0; |
| 119038 | 119650 | } |
| 119039 | 119651 | } |
| 119040 | 119652 | } |
| 119041 | 119653 | |
| 119654 | +/* |
| 119655 | +** Return SQLITE_CORRUPT_VTAB. |
| 119656 | +*/ |
| 119657 | +#ifdef SQLITE_DEBUG |
| 119658 | +SQLITE_PRIVATE int sqlite3Fts3Corrupt(){ |
| 119659 | + return SQLITE_CORRUPT_VTAB; |
| 119660 | +} |
| 119661 | +#endif |
| 119662 | + |
| 119042 | 119663 | #if !SQLITE_CORE |
| 119043 | 119664 | /* |
| 119044 | 119665 | ** Initialize API pointer table, if required. |
| 119045 | 119666 | */ |
| 119046 | 119667 | SQLITE_API int sqlite3_extension_init( |
| | @@ -119625,10 +120246,11 @@ |
| 119625 | 120246 | */ |
| 119626 | 120247 | typedef struct ParseContext ParseContext; |
| 119627 | 120248 | struct ParseContext { |
| 119628 | 120249 | sqlite3_tokenizer *pTokenizer; /* Tokenizer module */ |
| 119629 | 120250 | const char **azCol; /* Array of column names for fts3 table */ |
| 120251 | + int bFts4; /* True to allow FTS4-only syntax */ |
| 119630 | 120252 | int nCol; /* Number of entries in azCol[] */ |
| 119631 | 120253 | int iDefaultCol; /* Default column to query */ |
| 119632 | 120254 | int isNot; /* True if getNextNode() sees a unary - */ |
| 119633 | 120255 | sqlite3_context *pCtx; /* Write error message here */ |
| 119634 | 120256 | int nNest; /* Number of nested brackets */ |
| | @@ -119712,13 +120334,25 @@ |
| 119712 | 120334 | |
| 119713 | 120335 | if( iEnd<n && z[iEnd]=='*' ){ |
| 119714 | 120336 | pRet->pPhrase->aToken[0].isPrefix = 1; |
| 119715 | 120337 | iEnd++; |
| 119716 | 120338 | } |
| 119717 | | - if( !sqlite3_fts3_enable_parentheses && iStart>0 && z[iStart-1]=='-' ){ |
| 119718 | | - pParse->isNot = 1; |
| 120339 | + |
| 120340 | + while( 1 ){ |
| 120341 | + if( !sqlite3_fts3_enable_parentheses |
| 120342 | + && iStart>0 && z[iStart-1]=='-' |
| 120343 | + ){ |
| 120344 | + pParse->isNot = 1; |
| 120345 | + iStart--; |
| 120346 | + }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){ |
| 120347 | + pRet->pPhrase->aToken[0].bFirst = 1; |
| 120348 | + iStart--; |
| 120349 | + }else{ |
| 120350 | + break; |
| 120351 | + } |
| 119719 | 120352 | } |
| 120353 | + |
| 119720 | 120354 | } |
| 119721 | 120355 | nConsumed = iEnd; |
| 119722 | 120356 | } |
| 119723 | 120357 | |
| 119724 | 120358 | pModule->xClose(pCursor); |
| | @@ -119813,10 +120447,11 @@ |
| 119813 | 120447 | memcpy(&zTemp[nTemp], zByte, nByte); |
| 119814 | 120448 | nTemp += nByte; |
| 119815 | 120449 | |
| 119816 | 120450 | pToken->n = nByte; |
| 119817 | 120451 | pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*'); |
| 120452 | + pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^'); |
| 119818 | 120453 | nToken = ii+1; |
| 119819 | 120454 | } |
| 119820 | 120455 | } |
| 119821 | 120456 | |
| 119822 | 120457 | pModule->xClose(pCursor); |
| | @@ -119834,12 +120469,16 @@ |
| 119834 | 120469 | p->pPhrase = (Fts3Phrase *)&p[1]; |
| 119835 | 120470 | p->pPhrase->iColumn = pParse->iDefaultCol; |
| 119836 | 120471 | p->pPhrase->nToken = nToken; |
| 119837 | 120472 | |
| 119838 | 120473 | zBuf = (char *)&p->pPhrase->aToken[nToken]; |
| 119839 | | - memcpy(zBuf, zTemp, nTemp); |
| 119840 | | - sqlite3_free(zTemp); |
| 120474 | + if( zTemp ){ |
| 120475 | + memcpy(zBuf, zTemp, nTemp); |
| 120476 | + sqlite3_free(zTemp); |
| 120477 | + }else{ |
| 120478 | + assert( nTemp==0 ); |
| 120479 | + } |
| 119841 | 120480 | |
| 119842 | 120481 | for(jj=0; jj<p->pPhrase->nToken; jj++){ |
| 119843 | 120482 | p->pPhrase->aToken[jj].z = zBuf; |
| 119844 | 120483 | zBuf += p->pPhrase->aToken[jj].n; |
| 119845 | 120484 | } |
| | @@ -120260,10 +120899,11 @@ |
| 120260 | 120899 | ** match any table column. |
| 120261 | 120900 | */ |
| 120262 | 120901 | SQLITE_PRIVATE int sqlite3Fts3ExprParse( |
| 120263 | 120902 | sqlite3_tokenizer *pTokenizer, /* Tokenizer module */ |
| 120264 | 120903 | char **azCol, /* Array of column names for fts3 table */ |
| 120904 | + int bFts4, /* True to allow FTS4-only syntax */ |
| 120265 | 120905 | int nCol, /* Number of entries in azCol[] */ |
| 120266 | 120906 | int iDefaultCol, /* Default column to query */ |
| 120267 | 120907 | const char *z, int n, /* Text of MATCH query */ |
| 120268 | 120908 | Fts3Expr **ppExpr /* OUT: Parsed query structure */ |
| 120269 | 120909 | ){ |
| | @@ -120273,10 +120913,11 @@ |
| 120273 | 120913 | sParse.pTokenizer = pTokenizer; |
| 120274 | 120914 | sParse.azCol = (const char **)azCol; |
| 120275 | 120915 | sParse.nCol = nCol; |
| 120276 | 120916 | sParse.iDefaultCol = iDefaultCol; |
| 120277 | 120917 | sParse.nNest = 0; |
| 120918 | + sParse.bFts4 = bFts4; |
| 120278 | 120919 | if( z==0 ){ |
| 120279 | 120920 | *ppExpr = 0; |
| 120280 | 120921 | return SQLITE_OK; |
| 120281 | 120922 | } |
| 120282 | 120923 | if( n<0 ){ |
| | @@ -120462,11 +121103,11 @@ |
| 120462 | 121103 | for(ii=0; ii<nCol; ii++){ |
| 120463 | 121104 | azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]); |
| 120464 | 121105 | } |
| 120465 | 121106 | |
| 120466 | 121107 | rc = sqlite3Fts3ExprParse( |
| 120467 | | - pTokenizer, azCol, nCol, nCol, zExpr, nExpr, &pExpr |
| 121108 | + pTokenizer, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr |
| 120468 | 121109 | ); |
| 120469 | 121110 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){ |
| 120470 | 121111 | sqlite3_result_error(context, "Error parsing expression", -1); |
| 120471 | 121112 | }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){ |
| 120472 | 121113 | sqlite3_result_error_nomem(context); |
| | @@ -122509,11 +123150,11 @@ |
| 122509 | 123150 | /* 2 */ "DELETE FROM %Q.'%q_content'", |
| 122510 | 123151 | /* 3 */ "DELETE FROM %Q.'%q_segments'", |
| 122511 | 123152 | /* 4 */ "DELETE FROM %Q.'%q_segdir'", |
| 122512 | 123153 | /* 5 */ "DELETE FROM %Q.'%q_docsize'", |
| 122513 | 123154 | /* 6 */ "DELETE FROM %Q.'%q_stat'", |
| 122514 | | -/* 7 */ "SELECT %s FROM %Q.'%q_content' AS x WHERE rowid=?", |
| 123155 | +/* 7 */ "SELECT %s WHERE rowid=?", |
| 122515 | 123156 | /* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1", |
| 122516 | 123157 | /* 9 */ "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)", |
| 122517 | 123158 | /* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)", |
| 122518 | 123159 | /* 11 */ "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)", |
| 122519 | 123160 | |
| | @@ -122551,11 +123192,11 @@ |
| 122551 | 123192 | if( !pStmt ){ |
| 122552 | 123193 | char *zSql; |
| 122553 | 123194 | if( eStmt==SQL_CONTENT_INSERT ){ |
| 122554 | 123195 | zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist); |
| 122555 | 123196 | }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){ |
| 122556 | | - zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist, p->zDb, p->zName); |
| 123197 | + zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist); |
| 122557 | 123198 | }else{ |
| 122558 | 123199 | zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName); |
| 122559 | 123200 | } |
| 122560 | 123201 | if( !zSql ){ |
| 122561 | 123202 | rc = SQLITE_NOMEM; |
| | @@ -122594,11 +123235,11 @@ |
| 122594 | 123235 | sqlite3_bind_int64(pStmt, 1, iDocid); |
| 122595 | 123236 | } |
| 122596 | 123237 | rc = sqlite3_step(pStmt); |
| 122597 | 123238 | if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){ |
| 122598 | 123239 | rc = sqlite3_reset(pStmt); |
| 122599 | | - if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT_VTAB; |
| 123240 | + if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB; |
| 122600 | 123241 | pStmt = 0; |
| 122601 | 123242 | }else{ |
| 122602 | 123243 | rc = SQLITE_OK; |
| 122603 | 123244 | } |
| 122604 | 123245 | } |
| | @@ -122662,21 +123303,28 @@ |
| 122662 | 123303 | ** We try to avoid this because if FTS3 returns any error when committing |
| 122663 | 123304 | ** a transaction, the whole transaction will be rolled back. And this is |
| 122664 | 123305 | ** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can |
| 122665 | 123306 | ** still happen if the user reads data directly from the %_segments or |
| 122666 | 123307 | ** %_segdir tables instead of going through FTS3 though. |
| 123308 | +** |
| 123309 | +** This reasoning does not apply to a content=xxx table. |
| 122667 | 123310 | */ |
| 122668 | 123311 | SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){ |
| 122669 | 123312 | int rc; /* Return code */ |
| 122670 | 123313 | sqlite3_stmt *pStmt; /* Statement used to obtain lock */ |
| 122671 | 123314 | |
| 122672 | | - rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0); |
| 122673 | | - if( rc==SQLITE_OK ){ |
| 122674 | | - sqlite3_bind_null(pStmt, 1); |
| 122675 | | - sqlite3_step(pStmt); |
| 122676 | | - rc = sqlite3_reset(pStmt); |
| 123315 | + if( p->zContentTbl==0 ){ |
| 123316 | + rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0); |
| 123317 | + if( rc==SQLITE_OK ){ |
| 123318 | + sqlite3_bind_null(pStmt, 1); |
| 123319 | + sqlite3_step(pStmt); |
| 123320 | + rc = sqlite3_reset(pStmt); |
| 123321 | + } |
| 123322 | + }else{ |
| 123323 | + rc = SQLITE_OK; |
| 122677 | 123324 | } |
| 123325 | + |
| 122678 | 123326 | return rc; |
| 122679 | 123327 | } |
| 122680 | 123328 | |
| 122681 | 123329 | /* |
| 122682 | 123330 | ** Set *ppStmt to a statement handle that may be used to iterate through |
| | @@ -123032,10 +123680,22 @@ |
| 123032 | 123680 | sqlite3_value **apVal, /* Array of values to insert */ |
| 123033 | 123681 | sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */ |
| 123034 | 123682 | ){ |
| 123035 | 123683 | int rc; /* Return code */ |
| 123036 | 123684 | sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */ |
| 123685 | + |
| 123686 | + if( p->zContentTbl ){ |
| 123687 | + sqlite3_value *pRowid = apVal[p->nColumn+3]; |
| 123688 | + if( sqlite3_value_type(pRowid)==SQLITE_NULL ){ |
| 123689 | + pRowid = apVal[1]; |
| 123690 | + } |
| 123691 | + if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){ |
| 123692 | + return SQLITE_CONSTRAINT; |
| 123693 | + } |
| 123694 | + *piDocid = sqlite3_value_int64(pRowid); |
| 123695 | + return SQLITE_OK; |
| 123696 | + } |
| 123037 | 123697 | |
| 123038 | 123698 | /* Locate the statement handle used to insert data into the %_content |
| 123039 | 123699 | ** table. The SQL for this statement is: |
| 123040 | 123700 | ** |
| 123041 | 123701 | ** INSERT INTO %_content VALUES(?, ?, ?, ...) |
| | @@ -123083,18 +123743,20 @@ |
| 123083 | 123743 | |
| 123084 | 123744 | /* |
| 123085 | 123745 | ** Remove all data from the FTS3 table. Clear the hash table containing |
| 123086 | 123746 | ** pending terms. |
| 123087 | 123747 | */ |
| 123088 | | -static int fts3DeleteAll(Fts3Table *p){ |
| 123748 | +static int fts3DeleteAll(Fts3Table *p, int bContent){ |
| 123089 | 123749 | int rc = SQLITE_OK; /* Return code */ |
| 123090 | 123750 | |
| 123091 | 123751 | /* Discard the contents of the pending-terms hash table. */ |
| 123092 | 123752 | sqlite3Fts3PendingTermsClear(p); |
| 123093 | 123753 | |
| 123094 | | - /* Delete everything from the %_content, %_segments and %_segdir tables. */ |
| 123095 | | - fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0); |
| 123754 | + /* Delete everything from the shadow tables. Except, leave %_content as |
| 123755 | + ** is if bContent is false. */ |
| 123756 | + assert( p->zContentTbl==0 || bContent==0 ); |
| 123757 | + if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0); |
| 123096 | 123758 | fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0); |
| 123097 | 123759 | fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0); |
| 123098 | 123760 | if( p->bHasDocsize ){ |
| 123099 | 123761 | fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0); |
| 123100 | 123762 | } |
| | @@ -123398,11 +124060,11 @@ |
| 123398 | 124060 | pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix); |
| 123399 | 124061 | pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix); |
| 123400 | 124062 | if( nPrefix<0 || nSuffix<=0 |
| 123401 | 124063 | || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] |
| 123402 | 124064 | ){ |
| 123403 | | - return SQLITE_CORRUPT_VTAB; |
| 124065 | + return FTS_CORRUPT_VTAB; |
| 123404 | 124066 | } |
| 123405 | 124067 | |
| 123406 | 124068 | if( nPrefix+nSuffix>pReader->nTermAlloc ){ |
| 123407 | 124069 | int nNew = (nPrefix+nSuffix)*2; |
| 123408 | 124070 | char *zNew = sqlite3_realloc(pReader->zTerm, nNew); |
| | @@ -123428,11 +124090,11 @@ |
| 123428 | 124090 | ** of these statements is untrue, then the data structure is corrupt. |
| 123429 | 124091 | */ |
| 123430 | 124092 | if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] |
| 123431 | 124093 | || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1]) |
| 123432 | 124094 | ){ |
| 123433 | | - return SQLITE_CORRUPT_VTAB; |
| 124095 | + return FTS_CORRUPT_VTAB; |
| 123434 | 124096 | } |
| 123435 | 124097 | return SQLITE_OK; |
| 123436 | 124098 | } |
| 123437 | 124099 | |
| 123438 | 124100 | /* |
| | @@ -124378,16 +125040,22 @@ |
| 124378 | 125040 | ** error occurs, an SQLite error code is returned. |
| 124379 | 125041 | */ |
| 124380 | 125042 | static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){ |
| 124381 | 125043 | sqlite3_stmt *pStmt; |
| 124382 | 125044 | int rc; |
| 124383 | | - rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid); |
| 124384 | | - if( rc==SQLITE_OK ){ |
| 124385 | | - if( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 124386 | | - *pisEmpty = sqlite3_column_int(pStmt, 0); |
| 125045 | + if( p->zContentTbl ){ |
| 125046 | + /* If using the content=xxx option, assume the table is never empty */ |
| 125047 | + *pisEmpty = 0; |
| 125048 | + rc = SQLITE_OK; |
| 125049 | + }else{ |
| 125050 | + rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid); |
| 125051 | + if( rc==SQLITE_OK ){ |
| 125052 | + if( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 125053 | + *pisEmpty = sqlite3_column_int(pStmt, 0); |
| 125054 | + } |
| 125055 | + rc = sqlite3_reset(pStmt); |
| 124387 | 125056 | } |
| 124388 | | - rc = sqlite3_reset(pStmt); |
| 124389 | 125057 | } |
| 124390 | 125058 | return rc; |
| 124391 | 125059 | } |
| 124392 | 125060 | |
| 124393 | 125061 | /* |
| | @@ -124735,10 +125403,11 @@ |
| 124735 | 125403 | int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY); |
| 124736 | 125404 | int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS); |
| 124737 | 125405 | int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER); |
| 124738 | 125406 | int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX); |
| 124739 | 125407 | int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN); |
| 125408 | + int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST); |
| 124740 | 125409 | |
| 124741 | 125410 | Fts3SegReader **apSegment = pCsr->apSegment; |
| 124742 | 125411 | int nSegment = pCsr->nSegment; |
| 124743 | 125412 | Fts3SegFilter *pFilter = pCsr->pFilter; |
| 124744 | 125413 | int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = ( |
| | @@ -124794,10 +125463,11 @@ |
| 124794 | 125463 | } |
| 124795 | 125464 | |
| 124796 | 125465 | assert( isIgnoreEmpty || (isRequirePos && !isColFilter) ); |
| 124797 | 125466 | if( nMerge==1 |
| 124798 | 125467 | && !isIgnoreEmpty |
| 125468 | + && !isFirst |
| 124799 | 125469 | && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0) |
| 124800 | 125470 | ){ |
| 124801 | 125471 | pCsr->nDoclist = apSegment[0]->nDoclist; |
| 124802 | 125472 | if( fts3SegReaderIsPending(apSegment[0]) ){ |
| 124803 | 125473 | rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist); |
| | @@ -124859,16 +125529,28 @@ |
| 124859 | 125529 | if( !aNew ){ |
| 124860 | 125530 | return SQLITE_NOMEM; |
| 124861 | 125531 | } |
| 124862 | 125532 | pCsr->aBuffer = aNew; |
| 124863 | 125533 | } |
| 124864 | | - nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta); |
| 124865 | | - iPrev = iDocid; |
| 124866 | | - if( isRequirePos ){ |
| 124867 | | - memcpy(&pCsr->aBuffer[nDoclist], pList, nList); |
| 124868 | | - nDoclist += nList; |
| 124869 | | - pCsr->aBuffer[nDoclist++] = '\0'; |
| 125534 | + |
| 125535 | + if( isFirst ){ |
| 125536 | + char *a = &pCsr->aBuffer[nDoclist]; |
| 125537 | + int nWrite; |
| 125538 | + |
| 125539 | + nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a); |
| 125540 | + if( nWrite ){ |
| 125541 | + iPrev = iDocid; |
| 125542 | + nDoclist += nWrite; |
| 125543 | + } |
| 125544 | + }else{ |
| 125545 | + nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta); |
| 125546 | + iPrev = iDocid; |
| 125547 | + if( isRequirePos ){ |
| 125548 | + memcpy(&pCsr->aBuffer[nDoclist], pList, nList); |
| 125549 | + nDoclist += nList; |
| 125550 | + pCsr->aBuffer[nDoclist++] = '\0'; |
| 125551 | + } |
| 124870 | 125552 | } |
| 124871 | 125553 | } |
| 124872 | 125554 | |
| 124873 | 125555 | fts3SegReaderSort(apSegment, nMerge, j, xCmp); |
| 124874 | 125556 | } |
| | @@ -125040,13 +125722,13 @@ |
| 125040 | 125722 | ** Insert the sizes (in tokens) for each column of the document |
| 125041 | 125723 | ** with docid equal to p->iPrevDocid. The sizes are encoded as |
| 125042 | 125724 | ** a blob of varints. |
| 125043 | 125725 | */ |
| 125044 | 125726 | static void fts3InsertDocsize( |
| 125045 | | - int *pRC, /* Result code */ |
| 125046 | | - Fts3Table *p, /* Table into which to insert */ |
| 125047 | | - u32 *aSz /* Sizes of each column */ |
| 125727 | + int *pRC, /* Result code */ |
| 125728 | + Fts3Table *p, /* Table into which to insert */ |
| 125729 | + u32 *aSz /* Sizes of each column, in tokens */ |
| 125048 | 125730 | ){ |
| 125049 | 125731 | char *pBlob; /* The BLOB encoding of the document size */ |
| 125050 | 125732 | int nBlob; /* Number of bytes in the BLOB */ |
| 125051 | 125733 | sqlite3_stmt *pStmt; /* Statement used to insert the encoding */ |
| 125052 | 125734 | int rc; /* Result code from subfunctions */ |
| | @@ -125163,10 +125845,90 @@ |
| 125163 | 125845 | sqlite3Fts3SegmentsClose(p); |
| 125164 | 125846 | sqlite3Fts3PendingTermsClear(p); |
| 125165 | 125847 | |
| 125166 | 125848 | return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc; |
| 125167 | 125849 | } |
| 125850 | + |
| 125851 | +/* |
| 125852 | +** This function is called when the user executes the following statement: |
| 125853 | +** |
| 125854 | +** INSERT INTO <tbl>(<tbl>) VALUES('rebuild'); |
| 125855 | +** |
| 125856 | +** The entire FTS index is discarded and rebuilt. If the table is one |
| 125857 | +** created using the content=xxx option, then the new index is based on |
| 125858 | +** the current contents of the xxx table. Otherwise, it is rebuilt based |
| 125859 | +** on the contents of the %_content table. |
| 125860 | +*/ |
| 125861 | +static int fts3DoRebuild(Fts3Table *p){ |
| 125862 | + int rc; /* Return Code */ |
| 125863 | + |
| 125864 | + rc = fts3DeleteAll(p, 0); |
| 125865 | + if( rc==SQLITE_OK ){ |
| 125866 | + u32 *aSz = 0; |
| 125867 | + u32 *aSzIns = 0; |
| 125868 | + u32 *aSzDel = 0; |
| 125869 | + sqlite3_stmt *pStmt = 0; |
| 125870 | + int nEntry = 0; |
| 125871 | + |
| 125872 | + /* Compose and prepare an SQL statement to loop through the content table */ |
| 125873 | + char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist); |
| 125874 | + if( !zSql ){ |
| 125875 | + rc = SQLITE_NOMEM; |
| 125876 | + }else{ |
| 125877 | + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 125878 | + sqlite3_free(zSql); |
| 125879 | + } |
| 125880 | + |
| 125881 | + if( rc==SQLITE_OK ){ |
| 125882 | + int nByte = sizeof(u32) * (p->nColumn+1)*3; |
| 125883 | + aSz = (u32 *)sqlite3_malloc(nByte); |
| 125884 | + if( aSz==0 ){ |
| 125885 | + rc = SQLITE_NOMEM; |
| 125886 | + }else{ |
| 125887 | + memset(aSz, 0, nByte); |
| 125888 | + aSzIns = &aSz[p->nColumn+1]; |
| 125889 | + aSzDel = &aSzIns[p->nColumn+1]; |
| 125890 | + } |
| 125891 | + } |
| 125892 | + |
| 125893 | + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 125894 | + int iCol; |
| 125895 | + rc = fts3PendingTermsDocid(p, sqlite3_column_int64(pStmt, 0)); |
| 125896 | + aSz[p->nColumn] = 0; |
| 125897 | + for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){ |
| 125898 | + const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1); |
| 125899 | + rc = fts3PendingTermsAdd(p, z, iCol, &aSz[iCol]); |
| 125900 | + aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1); |
| 125901 | + } |
| 125902 | + if( p->bHasDocsize ){ |
| 125903 | + fts3InsertDocsize(&rc, p, aSz); |
| 125904 | + } |
| 125905 | + if( rc!=SQLITE_OK ){ |
| 125906 | + sqlite3_finalize(pStmt); |
| 125907 | + pStmt = 0; |
| 125908 | + }else{ |
| 125909 | + nEntry++; |
| 125910 | + for(iCol=0; iCol<=p->nColumn; iCol++){ |
| 125911 | + aSzIns[iCol] += aSz[iCol]; |
| 125912 | + } |
| 125913 | + } |
| 125914 | + } |
| 125915 | + if( p->bHasStat ){ |
| 125916 | + fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry); |
| 125917 | + } |
| 125918 | + sqlite3_free(aSz); |
| 125919 | + |
| 125920 | + if( pStmt ){ |
| 125921 | + int rc2 = sqlite3_finalize(pStmt); |
| 125922 | + if( rc==SQLITE_OK ){ |
| 125923 | + rc = rc2; |
| 125924 | + } |
| 125925 | + } |
| 125926 | + } |
| 125927 | + |
| 125928 | + return rc; |
| 125929 | +} |
| 125168 | 125930 | |
| 125169 | 125931 | /* |
| 125170 | 125932 | ** Handle a 'special' INSERT of the form: |
| 125171 | 125933 | ** |
| 125172 | 125934 | ** "INSERT INTO tbl(tbl) VALUES(<expr>)" |
| | @@ -125181,10 +125943,12 @@ |
| 125181 | 125943 | |
| 125182 | 125944 | if( !zVal ){ |
| 125183 | 125945 | return SQLITE_NOMEM; |
| 125184 | 125946 | }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){ |
| 125185 | 125947 | rc = fts3DoOptimize(p, 0); |
| 125948 | + }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){ |
| 125949 | + rc = fts3DoRebuild(p); |
| 125186 | 125950 | #ifdef SQLITE_TEST |
| 125187 | 125951 | }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){ |
| 125188 | 125952 | p->nNodeSize = atoi(&zVal[9]); |
| 125189 | 125953 | rc = SQLITE_OK; |
| 125190 | 125954 | }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){ |
| | @@ -125261,10 +126025,11 @@ |
| 125261 | 126025 | pTC->pTokenizer = pT; |
| 125262 | 126026 | rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos); |
| 125263 | 126027 | for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){ |
| 125264 | 126028 | Fts3PhraseToken *pPT = pDef->pToken; |
| 125265 | 126029 | if( (pDef->iCol>=p->nColumn || pDef->iCol==i) |
| 126030 | + && (pPT->bFirst==0 || iPos==0) |
| 125266 | 126031 | && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken)) |
| 125267 | 126032 | && (0==memcmp(zToken, pPT->z, pPT->n)) |
| 125268 | 126033 | ){ |
| 125269 | 126034 | fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc); |
| 125270 | 126035 | } |
| | @@ -125352,18 +126117,22 @@ |
| 125352 | 126117 | if( rc==SQLITE_OK ){ |
| 125353 | 126118 | if( isEmpty ){ |
| 125354 | 126119 | /* Deleting this row means the whole table is empty. In this case |
| 125355 | 126120 | ** delete the contents of all three tables and throw away any |
| 125356 | 126121 | ** data in the pendingTerms hash table. */ |
| 125357 | | - rc = fts3DeleteAll(p); |
| 126122 | + rc = fts3DeleteAll(p, 1); |
| 125358 | 126123 | *pnDoc = *pnDoc - 1; |
| 125359 | 126124 | }else{ |
| 125360 | 126125 | sqlite3_int64 iRemove = sqlite3_value_int64(pRowid); |
| 125361 | 126126 | rc = fts3PendingTermsDocid(p, iRemove); |
| 125362 | 126127 | fts3DeleteTerms(&rc, p, pRowid, aSzDel); |
| 125363 | | - fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid); |
| 125364 | | - if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1; |
| 126128 | + if( p->zContentTbl==0 ){ |
| 126129 | + fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid); |
| 126130 | + if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1; |
| 126131 | + }else{ |
| 126132 | + *pnDoc = *pnDoc - 1; |
| 126133 | + } |
| 125365 | 126134 | if( p->bHasDocsize ){ |
| 125366 | 126135 | fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid); |
| 125367 | 126136 | } |
| 125368 | 126137 | } |
| 125369 | 126138 | } |
| | @@ -125382,11 +126151,10 @@ |
| 125382 | 126151 | sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ |
| 125383 | 126152 | ){ |
| 125384 | 126153 | Fts3Table *p = (Fts3Table *)pVtab; |
| 125385 | 126154 | int rc = SQLITE_OK; /* Return Code */ |
| 125386 | 126155 | int isRemove = 0; /* True for an UPDATE or DELETE */ |
| 125387 | | - sqlite3_int64 iRemove = 0; /* Rowid removed by UPDATE or DELETE */ |
| 125388 | 126156 | u32 *aSzIns = 0; /* Sizes of inserted documents */ |
| 125389 | 126157 | u32 *aSzDel; /* Sizes of deleted documents */ |
| 125390 | 126158 | int nChng = 0; /* Net change in number of documents */ |
| 125391 | 126159 | int bInsertDone = 0; |
| 125392 | 126160 | |
| | @@ -125420,11 +126188,11 @@ |
| 125420 | 126188 | ** should be deleted from the database before inserting the new row. Or, |
| 125421 | 126189 | ** if the on-conflict mode is other than REPLACE, then this method must |
| 125422 | 126190 | ** detect the conflict and return SQLITE_CONSTRAINT before beginning to |
| 125423 | 126191 | ** modify the database file. |
| 125424 | 126192 | */ |
| 125425 | | - if( nArg>1 ){ |
| 126193 | + if( nArg>1 && p->zContentTbl==0 ){ |
| 125426 | 126194 | /* Find the value object that holds the new rowid value. */ |
| 125427 | 126195 | sqlite3_value *pNewRowid = apVal[3+p->nColumn]; |
| 125428 | 126196 | if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){ |
| 125429 | 126197 | pNewRowid = apVal[1]; |
| 125430 | 126198 | } |
| | @@ -125465,23 +126233,25 @@ |
| 125465 | 126233 | /* If this is a DELETE or UPDATE operation, remove the old record. */ |
| 125466 | 126234 | if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ |
| 125467 | 126235 | assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER ); |
| 125468 | 126236 | rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel); |
| 125469 | 126237 | isRemove = 1; |
| 125470 | | - iRemove = sqlite3_value_int64(apVal[0]); |
| 125471 | 126238 | } |
| 125472 | 126239 | |
| 125473 | 126240 | /* If this is an INSERT or UPDATE operation, insert the new record. */ |
| 125474 | 126241 | if( nArg>1 && rc==SQLITE_OK ){ |
| 125475 | 126242 | if( bInsertDone==0 ){ |
| 125476 | 126243 | rc = fts3InsertData(p, apVal, pRowid); |
| 125477 | | - if( rc==SQLITE_CONSTRAINT ) rc = SQLITE_CORRUPT_VTAB; |
| 126244 | + if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){ |
| 126245 | + rc = FTS_CORRUPT_VTAB; |
| 126246 | + } |
| 125478 | 126247 | } |
| 125479 | | - if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){ |
| 126248 | + if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){ |
| 125480 | 126249 | rc = fts3PendingTermsDocid(p, *pRowid); |
| 125481 | 126250 | } |
| 125482 | 126251 | if( rc==SQLITE_OK ){ |
| 126252 | + assert( p->iPrevDocid==*pRowid ); |
| 125483 | 126253 | rc = fts3InsertTerms(p, apVal, aSzIns); |
| 125484 | 126254 | } |
| 125485 | 126255 | if( p->bHasDocsize ){ |
| 125486 | 126256 | fts3InsertDocsize(&rc, p, aSzIns); |
| 125487 | 126257 | } |
| | @@ -125891,10 +126661,11 @@ |
| 125891 | 126661 | pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol); |
| 125892 | 126662 | if( pCsr ){ |
| 125893 | 126663 | int iFirst = 0; |
| 125894 | 126664 | pPhrase->pList = pCsr; |
| 125895 | 126665 | fts3GetDeltaPosition(&pCsr, &iFirst); |
| 126666 | + assert( iFirst>=0 ); |
| 125896 | 126667 | pPhrase->pHead = pCsr; |
| 125897 | 126668 | pPhrase->pTail = pCsr; |
| 125898 | 126669 | pPhrase->iHead = iFirst; |
| 125899 | 126670 | pPhrase->iTail = iFirst; |
| 125900 | 126671 | }else{ |
| | @@ -126371,11 +127142,11 @@ |
| 126371 | 127142 | pStmt = *ppStmt; |
| 126372 | 127143 | assert( sqlite3_data_count(pStmt)==1 ); |
| 126373 | 127144 | |
| 126374 | 127145 | a = sqlite3_column_blob(pStmt, 0); |
| 126375 | 127146 | a += sqlite3Fts3GetVarint(a, &nDoc); |
| 126376 | | - if( nDoc==0 ) return SQLITE_CORRUPT_VTAB; |
| 127147 | + if( nDoc==0 ) return FTS_CORRUPT_VTAB; |
| 126377 | 127148 | *pnDoc = (u32)nDoc; |
| 126378 | 127149 | |
| 126379 | 127150 | if( paLen ) *paLen = a; |
| 126380 | 127151 | return SQLITE_OK; |
| 126381 | 127152 | } |
| | @@ -126932,11 +127703,11 @@ |
| 126932 | 127703 | } |
| 126933 | 127704 | } |
| 126934 | 127705 | |
| 126935 | 127706 | if( !pTerm ){ |
| 126936 | 127707 | /* All offsets for this column have been gathered. */ |
| 126937 | | - break; |
| 127708 | + rc = SQLITE_DONE; |
| 126938 | 127709 | }else{ |
| 126939 | 127710 | assert( iCurrent<=iMinPos ); |
| 126940 | 127711 | if( 0==(0xFE&*pTerm->pList) ){ |
| 126941 | 127712 | pTerm->pList = 0; |
| 126942 | 127713 | }else{ |
| | @@ -126949,12 +127720,12 @@ |
| 126949 | 127720 | char aBuffer[64]; |
| 126950 | 127721 | sqlite3_snprintf(sizeof(aBuffer), aBuffer, |
| 126951 | 127722 | "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart |
| 126952 | 127723 | ); |
| 126953 | 127724 | rc = fts3StringAppend(&res, aBuffer, -1); |
| 126954 | | - }else if( rc==SQLITE_DONE ){ |
| 126955 | | - rc = SQLITE_CORRUPT_VTAB; |
| 127725 | + }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){ |
| 127726 | + rc = FTS_CORRUPT_VTAB; |
| 126956 | 127727 | } |
| 126957 | 127728 | } |
| 126958 | 127729 | } |
| 126959 | 127730 | if( rc==SQLITE_DONE ){ |
| 126960 | 127731 | rc = SQLITE_OK; |
| | @@ -128291,11 +129062,12 @@ |
| 128291 | 129062 | pCsr->nConstraint = argc; |
| 128292 | 129063 | if( !pCsr->aConstraint ){ |
| 128293 | 129064 | rc = SQLITE_NOMEM; |
| 128294 | 129065 | }else{ |
| 128295 | 129066 | memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc); |
| 128296 | | - assert( (idxStr==0 && argc==0) || (int)strlen(idxStr)==argc*2 ); |
| 129067 | + assert( (idxStr==0 && argc==0) |
| 129068 | + || (idxStr && (int)strlen(idxStr)==argc*2) ); |
| 128297 | 129069 | for(ii=0; ii<argc; ii++){ |
| 128298 | 129070 | RtreeConstraint *p = &pCsr->aConstraint[ii]; |
| 128299 | 129071 | p->op = idxStr[ii*2]; |
| 128300 | 129072 | p->iCoord = idxStr[ii*2+1]-'a'; |
| 128301 | 129073 | if( p->op==RTREE_MATCH ){ |
| | @@ -128592,11 +129364,14 @@ |
| 128592 | 129364 | int iCell; |
| 128593 | 129365 | sqlite3_int64 iBest = 0; |
| 128594 | 129366 | |
| 128595 | 129367 | float fMinGrowth = 0.0; |
| 128596 | 129368 | float fMinArea = 0.0; |
| 129369 | +#if VARIANT_RSTARTREE_CHOOSESUBTREE |
| 128597 | 129370 | float fMinOverlap = 0.0; |
| 129371 | + float overlap; |
| 129372 | +#endif |
| 128598 | 129373 | |
| 128599 | 129374 | int nCell = NCELL(pNode); |
| 128600 | 129375 | RtreeCell cell; |
| 128601 | 129376 | RtreeNode *pChild; |
| 128602 | 129377 | |
| | @@ -128624,33 +129399,34 @@ |
| 128624 | 129399 | */ |
| 128625 | 129400 | for(iCell=0; iCell<nCell; iCell++){ |
| 128626 | 129401 | int bBest = 0; |
| 128627 | 129402 | float growth; |
| 128628 | 129403 | float area; |
| 128629 | | - float overlap = 0.0; |
| 128630 | 129404 | nodeGetCell(pRtree, pNode, iCell, &cell); |
| 128631 | 129405 | growth = cellGrowth(pRtree, &cell, pCell); |
| 128632 | 129406 | area = cellArea(pRtree, &cell); |
| 128633 | 129407 | |
| 128634 | 129408 | #if VARIANT_RSTARTREE_CHOOSESUBTREE |
| 128635 | 129409 | if( ii==(pRtree->iDepth-1) ){ |
| 128636 | 129410 | overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell); |
| 129411 | + }else{ |
| 129412 | + overlap = 0.0; |
| 128637 | 129413 | } |
| 128638 | 129414 | if( (iCell==0) |
| 128639 | 129415 | || (overlap<fMinOverlap) |
| 128640 | 129416 | || (overlap==fMinOverlap && growth<fMinGrowth) |
| 128641 | 129417 | || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea) |
| 128642 | 129418 | ){ |
| 128643 | 129419 | bBest = 1; |
| 129420 | + fMinOverlap = overlap; |
| 128644 | 129421 | } |
| 128645 | 129422 | #else |
| 128646 | 129423 | if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){ |
| 128647 | 129424 | bBest = 1; |
| 128648 | 129425 | } |
| 128649 | 129426 | #endif |
| 128650 | 129427 | if( bBest ){ |
| 128651 | | - fMinOverlap = overlap; |
| 128652 | 129428 | fMinGrowth = growth; |
| 128653 | 129429 | fMinArea = area; |
| 128654 | 129430 | iBest = cell.iRowid; |
| 128655 | 129431 | } |
| 128656 | 129432 | } |
| 128657 | 129433 | |