| | @@ -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-15 00:16:30 39408702a989f907261c298bf0947f3e68bd10fe" |
| 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 |
| | @@ -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); |
| | @@ -25102,11 +25131,11 @@ |
| 25102 | 25131 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 25103 | 25132 | } |
| 25104 | 25133 | #endif |
| 25105 | 25134 | |
| 25106 | 25135 | |
| 25107 | | -#ifdef SQLITE_DEBUG |
| 25136 | +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 25108 | 25137 | /* |
| 25109 | 25138 | ** Helper function for printing out trace information from debugging |
| 25110 | 25139 | ** binaries. This returns the string represetation of the supplied |
| 25111 | 25140 | ** integer lock-type. |
| 25112 | 25141 | */ |
| | @@ -25937,18 +25966,18 @@ |
| 25937 | 25966 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 25938 | 25967 | ** even if the locking primitive used is always a write-lock. |
| 25939 | 25968 | */ |
| 25940 | 25969 | int rc = SQLITE_OK; |
| 25941 | 25970 | unixFile *pFile = (unixFile*)id; |
| 25942 | | - unixInodeInfo *pInode = pFile->pInode; |
| 25971 | + unixInodeInfo *pInode; |
| 25943 | 25972 | struct flock lock; |
| 25944 | 25973 | int tErrno = 0; |
| 25945 | 25974 | |
| 25946 | 25975 | assert( pFile ); |
| 25947 | 25976 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 25948 | 25977 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
| 25949 | | - azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
| 25978 | + azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid())); |
| 25950 | 25979 | |
| 25951 | 25980 | /* If there is already a lock of this type or more restrictive on the |
| 25952 | 25981 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
| 25953 | 25982 | ** unixEnterMutex() hasn't been called yet. |
| 25954 | 25983 | */ |
| | @@ -26148,11 +26177,10 @@ |
| 26148 | 26177 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
| 26149 | 26178 | unixFile *pFile = (unixFile*)id; |
| 26150 | 26179 | unixInodeInfo *pInode; |
| 26151 | 26180 | struct flock lock; |
| 26152 | 26181 | int rc = SQLITE_OK; |
| 26153 | | - int h; |
| 26154 | 26182 | |
| 26155 | 26183 | assert( pFile ); |
| 26156 | 26184 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
| 26157 | 26185 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
| 26158 | 26186 | getpid())); |
| | @@ -26160,18 +26188,14 @@ |
| 26160 | 26188 | assert( eFileLock<=SHARED_LOCK ); |
| 26161 | 26189 | if( pFile->eFileLock<=eFileLock ){ |
| 26162 | 26190 | return SQLITE_OK; |
| 26163 | 26191 | } |
| 26164 | 26192 | unixEnterMutex(); |
| 26165 | | - h = pFile->h; |
| 26166 | 26193 | pInode = pFile->pInode; |
| 26167 | 26194 | assert( pInode->nShared!=0 ); |
| 26168 | 26195 | if( pFile->eFileLock>SHARED_LOCK ){ |
| 26169 | 26196 | assert( pInode->eFileLock==pFile->eFileLock ); |
| 26170 | | - SimulateIOErrorBenign(1); |
| 26171 | | - SimulateIOError( h=(-1) ) |
| 26172 | | - SimulateIOErrorBenign(0); |
| 26173 | 26197 | |
| 26174 | 26198 | #ifndef NDEBUG |
| 26175 | 26199 | /* When reducing a lock such that other processes can start |
| 26176 | 26200 | ** reading the database file again, make sure that the |
| 26177 | 26201 | ** transaction counter was updated if any part of the database |
| | @@ -26178,15 +26202,10 @@ |
| 26178 | 26202 | ** file changed. If the transaction counter is not updated, |
| 26179 | 26203 | ** other connections to the same file might not realize that |
| 26180 | 26204 | ** the file has changed and hence might not know to flush their |
| 26181 | 26205 | ** cache. The use of a stale cache can lead to database corruption. |
| 26182 | 26206 | */ |
| 26183 | | -#if 0 |
| 26184 | | - assert( pFile->inNormalWrite==0 |
| 26185 | | - || pFile->dbUpdate==0 |
| 26186 | | - || pFile->transCntrChng==1 ); |
| 26187 | | -#endif |
| 26188 | 26207 | pFile->inNormalWrite = 0; |
| 26189 | 26208 | #endif |
| 26190 | 26209 | |
| 26191 | 26210 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 26192 | 26211 | ** before establishing the readlock - to avoid a race condition we downgrade |
| | @@ -26284,13 +26303,10 @@ |
| 26284 | 26303 | pInode->nShared--; |
| 26285 | 26304 | if( pInode->nShared==0 ){ |
| 26286 | 26305 | lock.l_type = F_UNLCK; |
| 26287 | 26306 | lock.l_whence = SEEK_SET; |
| 26288 | 26307 | lock.l_start = lock.l_len = 0L; |
| 26289 | | - SimulateIOErrorBenign(1); |
| 26290 | | - SimulateIOError( h=(-1) ) |
| 26291 | | - SimulateIOErrorBenign(0); |
| 26292 | 26308 | if( unixFileLock(pFile, &lock)==0 ){ |
| 26293 | 26309 | pInode->eFileLock = NO_LOCK; |
| 26294 | 26310 | }else{ |
| 26295 | 26311 | rc = SQLITE_IOERR_UNLOCK; |
| 26296 | 26312 | pFile->lastErrno = errno; |
| | @@ -28428,20 +28444,19 @@ |
| 28428 | 28444 | rc = SQLITE_NOMEM; |
| 28429 | 28445 | goto shm_open_err; |
| 28430 | 28446 | } |
| 28431 | 28447 | |
| 28432 | 28448 | if( pInode->bProcessLock==0 ){ |
| 28433 | | - pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT, |
| 28434 | | - (sStat.st_mode & 0777)); |
| 28449 | + const char *zRO; |
| 28450 | + int openFlags = O_RDWR | O_CREAT; |
| 28451 | + zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm"); |
| 28452 | + if( zRO && sqlite3GetBoolean(zRO) ){ |
| 28453 | + openFlags = O_RDONLY; |
| 28454 | + pShmNode->isReadonly = 1; |
| 28455 | + } |
| 28456 | + pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
| 28435 | 28457 | 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 | 28458 | if( pShmNode->h<0 ){ |
| 28444 | 28459 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 28445 | 28460 | goto shm_open_err; |
| 28446 | 28461 | } |
| 28447 | 28462 | } |
| | @@ -29122,10 +29137,13 @@ |
| 29122 | 29137 | assert( zFilename==0 || zFilename[0]=='/' |
| 29123 | 29138 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 29124 | 29139 | #else |
| 29125 | 29140 | assert( zFilename==0 || zFilename[0]=='/' ); |
| 29126 | 29141 | #endif |
| 29142 | + |
| 29143 | + /* No locking occurs in temporary files */ |
| 29144 | + assert( zFilename!=0 || noLock ); |
| 29127 | 29145 | |
| 29128 | 29146 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
| 29129 | 29147 | pNew->h = h; |
| 29130 | 29148 | pNew->zPath = zFilename; |
| 29131 | 29149 | if( memcmp(pVfs->zName,"unix-excl",10)==0 ){ |
| | @@ -29224,10 +29242,11 @@ |
| 29224 | 29242 | /* Dotfile locking uses the file path so it needs to be included in |
| 29225 | 29243 | ** the dotlockLockingContext |
| 29226 | 29244 | */ |
| 29227 | 29245 | char *zLockFile; |
| 29228 | 29246 | int nFilename; |
| 29247 | + assert( zFilename!=0 ); |
| 29229 | 29248 | nFilename = (int)strlen(zFilename) + 6; |
| 29230 | 29249 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 29231 | 29250 | if( zLockFile==0 ){ |
| 29232 | 29251 | rc = SQLITE_NOMEM; |
| 29233 | 29252 | }else{ |
| | @@ -29462,12 +29481,20 @@ |
| 29462 | 29481 | ** |
| 29463 | 29482 | ** where NN is a 4 digit decimal number. The NN naming schemes are |
| 29464 | 29483 | ** used by the test_multiplex.c module. |
| 29465 | 29484 | */ |
| 29466 | 29485 | nDb = sqlite3Strlen30(zPath) - 1; |
| 29467 | | - while( nDb>0 && zPath[nDb]!='-' ) nDb--; |
| 29468 | | - if( nDb==0 ) return SQLITE_OK; |
| 29486 | +#ifdef SQLITE_ENABLE_8_3_NAMES |
| 29487 | + while( nDb>0 && zPath[nDb]!='-' && zPath[nDb]!='/' ) nDb--; |
| 29488 | + if( nDb==0 || zPath[nDb]=='/' ) return SQLITE_OK; |
| 29489 | +#else |
| 29490 | + while( zPath[nDb]!='-' ){ |
| 29491 | + assert( nDb>0 ); |
| 29492 | + assert( zPath[nDb]!='\n' ); |
| 29493 | + nDb--; |
| 29494 | + } |
| 29495 | +#endif |
| 29469 | 29496 | memcpy(zDb, zPath, nDb); |
| 29470 | 29497 | zDb[nDb] = '\0'; |
| 29471 | 29498 | |
| 29472 | 29499 | if( 0==osStat(zDb, &sStat) ){ |
| 29473 | 29500 | *pMode = sStat.st_mode & 0777; |
| | @@ -29995,14 +30022,16 @@ |
| 29995 | 30022 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 29996 | 30023 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 29997 | 30024 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 29998 | 30025 | ** proleptic Gregorian calendar. |
| 29999 | 30026 | ** |
| 30000 | | -** On success, return 0. Return 1 if the time and date cannot be found. |
| 30027 | +** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 30028 | +** cannot be found. |
| 30001 | 30029 | */ |
| 30002 | 30030 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 30003 | 30031 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 30032 | + int rc = SQLITE_OK; |
| 30004 | 30033 | #if defined(NO_GETTOD) |
| 30005 | 30034 | time_t t; |
| 30006 | 30035 | time(&t); |
| 30007 | 30036 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
| 30008 | 30037 | #elif OS_VXWORKS |
| | @@ -30009,34 +30038,38 @@ |
| 30009 | 30038 | struct timespec sNow; |
| 30010 | 30039 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 30011 | 30040 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 30012 | 30041 | #else |
| 30013 | 30042 | struct timeval sNow; |
| 30014 | | - gettimeofday(&sNow, 0); |
| 30015 | | - *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 30043 | + if( gettimeofday(&sNow, 0)==0 ){ |
| 30044 | + *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 30045 | + }else{ |
| 30046 | + rc = SQLITE_ERROR; |
| 30047 | + } |
| 30016 | 30048 | #endif |
| 30017 | 30049 | |
| 30018 | 30050 | #ifdef SQLITE_TEST |
| 30019 | 30051 | if( sqlite3_current_time ){ |
| 30020 | 30052 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 30021 | 30053 | } |
| 30022 | 30054 | #endif |
| 30023 | 30055 | UNUSED_PARAMETER(NotUsed); |
| 30024 | | - return 0; |
| 30056 | + return rc; |
| 30025 | 30057 | } |
| 30026 | 30058 | |
| 30027 | 30059 | /* |
| 30028 | 30060 | ** Find the current time (in Universal Coordinated Time). Write the |
| 30029 | 30061 | ** current time and date as a Julian Day number into *prNow and |
| 30030 | 30062 | ** return 0. Return 1 if the time and date cannot be found. |
| 30031 | 30063 | */ |
| 30032 | 30064 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
| 30033 | | - sqlite3_int64 i; |
| 30065 | + sqlite3_int64 i = 0; |
| 30066 | + int rc; |
| 30034 | 30067 | UNUSED_PARAMETER(NotUsed); |
| 30035 | | - unixCurrentTimeInt64(0, &i); |
| 30068 | + rc = unixCurrentTimeInt64(0, &i); |
| 30036 | 30069 | *prNow = i/86400000.0; |
| 30037 | | - return 0; |
| 30070 | + return rc; |
| 30038 | 30071 | } |
| 30039 | 30072 | |
| 30040 | 30073 | /* |
| 30041 | 30074 | ** We added the xGetLastError() method with the intention of providing |
| 30042 | 30075 | ** better low-level error messages when operating-system problems come up |
| | @@ -34169,11 +34202,11 @@ |
| 34169 | 34202 | |
| 34170 | 34203 | if( h==INVALID_HANDLE_VALUE ){ |
| 34171 | 34204 | pFile->lastErrno = GetLastError(); |
| 34172 | 34205 | winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name); |
| 34173 | 34206 | free(zConverted); |
| 34174 | | - if( isReadWrite ){ |
| 34207 | + if( isReadWrite && !isExclusive ){ |
| 34175 | 34208 | return winOpen(pVfs, zName, id, |
| 34176 | 34209 | ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags); |
| 34177 | 34210 | }else{ |
| 34178 | 34211 | return SQLITE_CANTOPEN_BKPT; |
| 34179 | 34212 | } |
| | @@ -34535,11 +34568,11 @@ |
| 34535 | 34568 | } |
| 34536 | 34569 | static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ |
| 34537 | 34570 | UNUSED_PARAMETER(pVfs); |
| 34538 | 34571 | getLastErrorMsg(nBuf, zBufOut); |
| 34539 | 34572 | } |
| 34540 | | -void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){ |
| 34573 | +static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){ |
| 34541 | 34574 | UNUSED_PARAMETER(pVfs); |
| 34542 | 34575 | #if SQLITE_OS_WINCE |
| 34543 | 34576 | /* The GetProcAddressA() routine is only available on wince. */ |
| 34544 | 34577 | return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol); |
| 34545 | 34578 | #else |
| | @@ -34546,11 +34579,11 @@ |
| 34546 | 34579 | /* All other windows platforms expect GetProcAddress() to take |
| 34547 | 34580 | ** an Ansi string regardless of the _UNICODE setting */ |
| 34548 | 34581 | return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol); |
| 34549 | 34582 | #endif |
| 34550 | 34583 | } |
| 34551 | | -void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 34584 | +static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 34552 | 34585 | UNUSED_PARAMETER(pVfs); |
| 34553 | 34586 | FreeLibrary((HANDLE)pHandle); |
| 34554 | 34587 | } |
| 34555 | 34588 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 34556 | 34589 | #define winDlOpen 0 |
| | @@ -34620,11 +34653,12 @@ |
| 34620 | 34653 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 34621 | 34654 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 34622 | 34655 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 34623 | 34656 | ** proleptic Gregorian calendar. |
| 34624 | 34657 | ** |
| 34625 | | -** On success, return 0. Return 1 if the time and date cannot be found. |
| 34658 | +** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 34659 | +** cannot be found. |
| 34626 | 34660 | */ |
| 34627 | 34661 | static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){ |
| 34628 | 34662 | /* FILETIME structure is a 64-bit value representing the number of |
| 34629 | 34663 | 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). |
| 34630 | 34664 | */ |
| | @@ -34640,11 +34674,11 @@ |
| 34640 | 34674 | #if SQLITE_OS_WINCE |
| 34641 | 34675 | SYSTEMTIME time; |
| 34642 | 34676 | GetSystemTime(&time); |
| 34643 | 34677 | /* if SystemTimeToFileTime() fails, it returns zero. */ |
| 34644 | 34678 | if (!SystemTimeToFileTime(&time,&ft)){ |
| 34645 | | - return 1; |
| 34679 | + return SQLITE_ERROR; |
| 34646 | 34680 | } |
| 34647 | 34681 | #else |
| 34648 | 34682 | GetSystemTimeAsFileTime( &ft ); |
| 34649 | 34683 | #endif |
| 34650 | 34684 | |
| | @@ -34656,19 +34690,19 @@ |
| 34656 | 34690 | if( sqlite3_current_time ){ |
| 34657 | 34691 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 34658 | 34692 | } |
| 34659 | 34693 | #endif |
| 34660 | 34694 | UNUSED_PARAMETER(pVfs); |
| 34661 | | - return 0; |
| 34695 | + return SQLITE_OK; |
| 34662 | 34696 | } |
| 34663 | 34697 | |
| 34664 | 34698 | /* |
| 34665 | 34699 | ** Find the current time (in Universal Coordinated Time). Write the |
| 34666 | 34700 | ** current time and date as a Julian Day number into *prNow and |
| 34667 | 34701 | ** return 0. Return 1 if the time and date cannot be found. |
| 34668 | 34702 | */ |
| 34669 | | -int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
| 34703 | +static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
| 34670 | 34704 | int rc; |
| 34671 | 34705 | sqlite3_int64 i; |
| 34672 | 34706 | rc = winCurrentTimeInt64(pVfs, &i); |
| 34673 | 34707 | if( !rc ){ |
| 34674 | 34708 | *prNow = i/86400000.0; |
| | @@ -35789,12 +35823,10 @@ |
| 35789 | 35823 | typedef struct PCache1 PCache1; |
| 35790 | 35824 | typedef struct PgHdr1 PgHdr1; |
| 35791 | 35825 | typedef struct PgFreeslot PgFreeslot; |
| 35792 | 35826 | typedef struct PGroup PGroup; |
| 35793 | 35827 | |
| 35794 | | -typedef struct PGroupBlock PGroupBlock; |
| 35795 | | -typedef struct PGroupBlockList PGroupBlockList; |
| 35796 | 35828 | |
| 35797 | 35829 | /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set |
| 35798 | 35830 | ** of one or more PCaches that are able to recycle each others unpinned |
| 35799 | 35831 | ** pages when they are under memory pressure. A PGroup is an instance of |
| 35800 | 35832 | ** the following object. |
| | @@ -35821,69 +35853,11 @@ |
| 35821 | 35853 | int nMaxPage; /* Sum of nMax for purgeable caches */ |
| 35822 | 35854 | int nMinPage; /* Sum of nMin for purgeable caches */ |
| 35823 | 35855 | int mxPinned; /* nMaxpage + 10 - nMinPage */ |
| 35824 | 35856 | int nCurrentPage; /* Number of purgeable pages allocated */ |
| 35825 | 35857 | 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 |
| 35858 | +}; |
| 35885 | 35859 | |
| 35886 | 35860 | /* Each page cache is an instance of the following object. Every |
| 35887 | 35861 | ** open database file (including each in-memory database and each |
| 35888 | 35862 | ** temporary or transient database) has a single page cache which |
| 35889 | 35863 | ** is an instance of this object. |
| | @@ -35983,21 +35957,10 @@ |
| 35983 | 35957 | ** |
| 35984 | 35958 | ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X ); |
| 35985 | 35959 | */ |
| 35986 | 35960 | #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage) |
| 35987 | 35961 | #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 | 35962 | |
| 36000 | 35963 | /* |
| 36001 | 35964 | ** Macros to enter and leave the PCache LRU mutex. |
| 36002 | 35965 | */ |
| 36003 | 35966 | #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) |
| | @@ -36120,159 +36083,32 @@ |
| 36120 | 36083 | return iSize; |
| 36121 | 36084 | } |
| 36122 | 36085 | } |
| 36123 | 36086 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 36124 | 36087 | |
| 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 | 36088 | /* |
| 36158 | 36089 | ** Allocate a new page object initially associated with cache pCache. |
| 36159 | 36090 | */ |
| 36160 | 36091 | static PgHdr1 *pcache1AllocPage(PCache1 *pCache){ |
| 36161 | 36092 | 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 |
| 36093 | + PgHdr1 *p = 0; |
| 36094 | + void *pPg; |
| 36095 | + |
| 36258 | 36096 | /* The group mutex must be released before pcache1Alloc() is called. This |
| 36259 | 36097 | ** is because it may call sqlite3_release_memory(), which assumes that |
| 36260 | 36098 | ** this mutex is not held. */ |
| 36261 | 36099 | assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 36262 | 36100 | pcache1LeaveMutex(pCache->pGroup); |
| 36263 | 36101 | pPg = pcache1Alloc(nByte); |
| 36264 | 36102 | pcache1EnterMutex(pCache->pGroup); |
| 36103 | + |
| 36265 | 36104 | if( pPg ){ |
| 36266 | 36105 | p = PAGE_TO_PGHDR1(pCache, pPg); |
| 36267 | 36106 | if( pCache->bPurgeable ){ |
| 36268 | 36107 | pCache->pGroup->nCurrentPage++; |
| 36269 | 36108 | } |
| 36270 | | - }else{ |
| 36271 | | - p = 0; |
| 36272 | 36109 | } |
| 36273 | | -#endif |
| 36274 | 36110 | return p; |
| 36275 | 36111 | } |
| 36276 | 36112 | |
| 36277 | 36113 | /* |
| 36278 | 36114 | ** Free a page object allocated by pcache1AllocPage(). |
| | @@ -36282,53 +36118,12 @@ |
| 36282 | 36118 | ** with a NULL pointer, so we mark the NULL test with ALWAYS(). |
| 36283 | 36119 | */ |
| 36284 | 36120 | static void pcache1FreePage(PgHdr1 *p){ |
| 36285 | 36121 | if( ALWAYS(p) ){ |
| 36286 | 36122 | 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 | 36123 | assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); |
| 36328 | | - pcache1Free(pPg); |
| 36329 | | -#endif |
| 36124 | + pcache1Free(PGHDR1_TO_PAGE(p)); |
| 36330 | 36125 | if( pCache->bPurgeable ){ |
| 36331 | 36126 | pCache->pGroup->nCurrentPage--; |
| 36332 | 36127 | } |
| 36333 | 36128 | } |
| 36334 | 36129 | } |
| | @@ -36935,13 +36730,10 @@ |
| 36935 | 36730 | ** been released, the function returns. The return value is the total number |
| 36936 | 36731 | ** of bytes of memory released. |
| 36937 | 36732 | */ |
| 36938 | 36733 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 36939 | 36734 | int nFree = 0; |
| 36940 | | -#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36941 | | - if( pcache1.grp.isBusy ) return 0; |
| 36942 | | -#endif |
| 36943 | 36735 | assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 36944 | 36736 | assert( sqlite3_mutex_notheld(pcache1.mutex) ); |
| 36945 | 36737 | if( pcache1.pStart==0 ){ |
| 36946 | 36738 | PgHdr1 *p; |
| 36947 | 36739 | pcache1EnterMutex(&pcache1.grp); |
| | @@ -38200,12 +37992,12 @@ |
| 38200 | 37992 | i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 38201 | 37993 | char *zFilename; /* Name of the database file */ |
| 38202 | 37994 | char *zJournal; /* Name of the journal file */ |
| 38203 | 37995 | int (*xBusyHandler)(void*); /* Function to call when busy */ |
| 38204 | 37996 | void *pBusyHandlerArg; /* Context argument for xBusyHandler */ |
| 37997 | + int nHit, nMiss; /* Total cache hits and misses */ |
| 38205 | 37998 | #ifdef SQLITE_TEST |
| 38206 | | - int nHit, nMiss; /* Cache hits and missing */ |
| 38207 | 37999 | int nRead, nWrite; /* Database pages read/written */ |
| 38208 | 38000 | #endif |
| 38209 | 38001 | void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ |
| 38210 | 38002 | #ifdef SQLITE_HAS_CODEC |
| 38211 | 38003 | void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ |
| | @@ -40233,11 +40025,10 @@ |
| 40233 | 40025 | needPagerReset = 0; |
| 40234 | 40026 | } |
| 40235 | 40027 | rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0); |
| 40236 | 40028 | if( rc!=SQLITE_OK ){ |
| 40237 | 40029 | if( rc==SQLITE_DONE ){ |
| 40238 | | - rc = SQLITE_OK; |
| 40239 | 40030 | pPager->journalOff = szJ; |
| 40240 | 40031 | break; |
| 40241 | 40032 | }else if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 40242 | 40033 | /* If the journal has been truncated, simply stop reading and |
| 40243 | 40034 | ** processing the journal. This might happen if the journal was |
| | @@ -40495,10 +40286,11 @@ |
| 40495 | 40286 | #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES) |
| 40496 | 40287 | PgHdr *p; /* For looping over pages */ |
| 40497 | 40288 | #endif |
| 40498 | 40289 | |
| 40499 | 40290 | assert( pPager->pWal ); |
| 40291 | + assert( pList ); |
| 40500 | 40292 | #ifdef SQLITE_DEBUG |
| 40501 | 40293 | /* Verify that the page list is in accending order */ |
| 40502 | 40294 | for(p=pList; p && p->pDirty; p=p->pDirty){ |
| 40503 | 40295 | assert( p->pgno < p->pDirty->pgno ); |
| 40504 | 40296 | } |
| | @@ -41699,11 +41491,11 @@ |
| 41699 | 41491 | ** The doNotSpill flag inhibits all cache spilling regardless of whether |
| 41700 | 41492 | ** or not a sync is required. This is set during a rollback. |
| 41701 | 41493 | ** |
| 41702 | 41494 | ** Spilling is also prohibited when in an error state since that could |
| 41703 | 41495 | ** lead to database corruption. In the current implementaton it |
| 41704 | | - ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1 |
| 41496 | + ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1 |
| 41705 | 41497 | ** while in the error state, hence it is impossible for this routine to |
| 41706 | 41498 | ** be called in the error state. Nevertheless, we include a NEVER() |
| 41707 | 41499 | ** test for the error state as a safeguard against future changes. |
| 41708 | 41500 | */ |
| 41709 | 41501 | if( NEVER(pPager->errCode) ) return SQLITE_OK; |
| | @@ -42535,18 +42327,17 @@ |
| 42535 | 42327 | |
| 42536 | 42328 | if( (*ppPage)->pPager && !noContent ){ |
| 42537 | 42329 | /* In this case the pcache already contains an initialized copy of |
| 42538 | 42330 | ** the page. Return without further ado. */ |
| 42539 | 42331 | assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); |
| 42540 | | - PAGER_INCR(pPager->nHit); |
| 42332 | + pPager->nHit++; |
| 42541 | 42333 | return SQLITE_OK; |
| 42542 | 42334 | |
| 42543 | 42335 | }else{ |
| 42544 | 42336 | /* The pager cache has created a new page. Its content needs to |
| 42545 | 42337 | ** be initialized. */ |
| 42546 | 42338 | |
| 42547 | | - PAGER_INCR(pPager->nMiss); |
| 42548 | 42339 | pPg = *ppPage; |
| 42549 | 42340 | pPg->pPager = pPager; |
| 42550 | 42341 | |
| 42551 | 42342 | /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page |
| 42552 | 42343 | ** number greater than this, or the unused locking-page, is requested. */ |
| | @@ -42578,10 +42369,11 @@ |
| 42578 | 42369 | } |
| 42579 | 42370 | memset(pPg->pData, 0, pPager->pageSize); |
| 42580 | 42371 | IOTRACE(("ZERO %p %d\n", pPager, pgno)); |
| 42581 | 42372 | }else{ |
| 42582 | 42373 | assert( pPg->pPager==pPager ); |
| 42374 | + pPager->nMiss++; |
| 42583 | 42375 | rc = readDbPage(pPg); |
| 42584 | 42376 | if( rc!=SQLITE_OK ){ |
| 42585 | 42377 | goto pager_acquire_err; |
| 42586 | 42378 | } |
| 42587 | 42379 | } |
| | @@ -43611,10 +43403,35 @@ |
| 43611 | 43403 | a[9] = pPager->nRead; |
| 43612 | 43404 | a[10] = pPager->nWrite; |
| 43613 | 43405 | return a; |
| 43614 | 43406 | } |
| 43615 | 43407 | #endif |
| 43408 | + |
| 43409 | +/* |
| 43410 | +** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or |
| 43411 | +** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the |
| 43412 | +** current cache hit or miss count, according to the value of eStat. If the |
| 43413 | +** reset parameter is non-zero, the cache hit or miss count is zeroed before |
| 43414 | +** returning. |
| 43415 | +*/ |
| 43416 | +SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){ |
| 43417 | + int *piStat; |
| 43418 | + |
| 43419 | + assert( eStat==SQLITE_DBSTATUS_CACHE_HIT |
| 43420 | + || eStat==SQLITE_DBSTATUS_CACHE_MISS |
| 43421 | + ); |
| 43422 | + if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){ |
| 43423 | + piStat = &pPager->nHit; |
| 43424 | + }else{ |
| 43425 | + piStat = &pPager->nMiss; |
| 43426 | + } |
| 43427 | + |
| 43428 | + *pnVal += *piStat; |
| 43429 | + if( reset ){ |
| 43430 | + *piStat = 0; |
| 43431 | + } |
| 43432 | +} |
| 43616 | 43433 | |
| 43617 | 43434 | /* |
| 43618 | 43435 | ** Return true if this is an in-memory pager. |
| 43619 | 43436 | */ |
| 43620 | 43437 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){ |
| | @@ -46706,11 +46523,11 @@ |
| 46706 | 46523 | */ |
| 46707 | 46524 | if( iRead ){ |
| 46708 | 46525 | int sz; |
| 46709 | 46526 | i64 iOffset; |
| 46710 | 46527 | sz = pWal->hdr.szPage; |
| 46711 | | - sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); |
| 46528 | + sz = (sz&0xfe00) + ((sz&0x0001)<<16); |
| 46712 | 46529 | testcase( sz<=32768 ); |
| 46713 | 46530 | testcase( sz>=65536 ); |
| 46714 | 46531 | iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE; |
| 46715 | 46532 | *pInWal = 1; |
| 46716 | 46533 | /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */ |
| | @@ -50019,21 +49836,23 @@ |
| 50019 | 49836 | */ |
| 50020 | 49837 | if( isMemdb==0 && isTempDb==0 ){ |
| 50021 | 49838 | if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
| 50022 | 49839 | int nFullPathname = pVfs->mxPathname+1; |
| 50023 | 49840 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
| 50024 | | - sqlite3_mutex *mutexShared; |
| 49841 | + MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
| 50025 | 49842 | p->sharable = 1; |
| 50026 | 49843 | if( !zFullPathname ){ |
| 50027 | 49844 | sqlite3_free(p); |
| 50028 | 49845 | return SQLITE_NOMEM; |
| 50029 | 49846 | } |
| 50030 | 49847 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
| 49848 | +#if SQLITE_THREADSAFE |
| 50031 | 49849 | mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); |
| 50032 | 49850 | sqlite3_mutex_enter(mutexOpen); |
| 50033 | 49851 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 50034 | 49852 | sqlite3_mutex_enter(mutexShared); |
| 49853 | +#endif |
| 50035 | 49854 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
| 50036 | 49855 | assert( pBt->nRef>0 ); |
| 50037 | 49856 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 50038 | 49857 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 50039 | 49858 | int iDb; |
| | @@ -50135,13 +49954,13 @@ |
| 50135 | 49954 | |
| 50136 | 49955 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 50137 | 49956 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 50138 | 49957 | */ |
| 50139 | 49958 | if( p->sharable ){ |
| 50140 | | - sqlite3_mutex *mutexShared; |
| 49959 | + MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
| 50141 | 49960 | pBt->nRef = 1; |
| 50142 | | - mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 49961 | + MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);) |
| 50143 | 49962 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
| 50144 | 49963 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
| 50145 | 49964 | if( pBt->mutex==0 ){ |
| 50146 | 49965 | rc = SQLITE_NOMEM; |
| 50147 | 49966 | db->mallocFailed = 0; |
| | @@ -50219,16 +50038,16 @@ |
| 50219 | 50038 | ** true if the BtShared.nRef counter reaches zero and return |
| 50220 | 50039 | ** false if it is still positive. |
| 50221 | 50040 | */ |
| 50222 | 50041 | static int removeFromSharingList(BtShared *pBt){ |
| 50223 | 50042 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 50224 | | - sqlite3_mutex *pMaster; |
| 50043 | + MUTEX_LOGIC( sqlite3_mutex *pMaster; ) |
| 50225 | 50044 | BtShared *pList; |
| 50226 | 50045 | int removed = 0; |
| 50227 | 50046 | |
| 50228 | 50047 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
| 50229 | | - pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 50048 | + MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 50230 | 50049 | sqlite3_mutex_enter(pMaster); |
| 50231 | 50050 | pBt->nRef--; |
| 50232 | 50051 | if( pBt->nRef<=0 ){ |
| 50233 | 50052 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 50234 | 50053 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
| | @@ -52191,25 +52010,59 @@ |
| 52191 | 52010 | offset -= ovflSize; |
| 52192 | 52011 | }else{ |
| 52193 | 52012 | /* Need to read this page properly. It contains some of the |
| 52194 | 52013 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
| 52195 | 52014 | */ |
| 52196 | | - DbPage *pDbPage; |
| 52015 | +#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 52016 | + sqlite3_file *fd; |
| 52017 | +#endif |
| 52197 | 52018 | 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 | | - } |
| 52019 | + if( a + offset > ovflSize ){ |
| 52020 | + a = ovflSize - offset; |
| 52021 | + } |
| 52022 | + |
| 52023 | +#ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 52024 | + /* If all the following are true: |
| 52025 | + ** |
| 52026 | + ** 1) this is a read operation, and |
| 52027 | + ** 2) data is required from the start of this overflow page, and |
| 52028 | + ** 3) the database is file-backed, and |
| 52029 | + ** 4) there is no open write-transaction, and |
| 52030 | + ** 5) the database is not a WAL database, |
| 52031 | + ** |
| 52032 | + ** then data can be read directly from the database file into the |
| 52033 | + ** output buffer, bypassing the page-cache altogether. This speeds |
| 52034 | + ** up loading large records that span many overflow pages. |
| 52035 | + */ |
| 52036 | + if( eOp==0 /* (1) */ |
| 52037 | + && offset==0 /* (2) */ |
| 52038 | + && pBt->inTransaction==TRANS_READ /* (4) */ |
| 52039 | + && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ |
| 52040 | + && pBt->pPage1->aData[19]==0x01 /* (5) */ |
| 52041 | + ){ |
| 52042 | + u8 aSave[4]; |
| 52043 | + u8 *aWrite = &pBuf[-4]; |
| 52044 | + memcpy(aSave, aWrite, 4); |
| 52045 | + rc = sqlite3OsRead(fd, aWrite, a+4, pBt->pageSize * (nextPage-1)); |
| 52046 | + nextPage = get4byte(aWrite); |
| 52047 | + memcpy(aWrite, aSave, 4); |
| 52048 | + }else |
| 52049 | +#endif |
| 52050 | + |
| 52051 | + { |
| 52052 | + DbPage *pDbPage; |
| 52053 | + rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
| 52054 | + if( rc==SQLITE_OK ){ |
| 52055 | + aPayload = sqlite3PagerGetData(pDbPage); |
| 52056 | + nextPage = get4byte(aPayload); |
| 52057 | + rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 52058 | + sqlite3PagerUnref(pDbPage); |
| 52059 | + offset = 0; |
| 52060 | + } |
| 52061 | + } |
| 52062 | + amt -= a; |
| 52063 | + pBuf += a; |
| 52211 | 52064 | } |
| 52212 | 52065 | } |
| 52213 | 52066 | } |
| 52214 | 52067 | |
| 52215 | 52068 | if( rc==SQLITE_OK && amt>0 ){ |
| | @@ -52804,11 +52657,10 @@ |
| 52804 | 52657 | } |
| 52805 | 52658 | } |
| 52806 | 52659 | if( c==0 ){ |
| 52807 | 52660 | if( pPage->intKey && !pPage->leaf ){ |
| 52808 | 52661 | lwr = idx; |
| 52809 | | - upr = lwr - 1; |
| 52810 | 52662 | break; |
| 52811 | 52663 | }else{ |
| 52812 | 52664 | *pRes = 0; |
| 52813 | 52665 | rc = SQLITE_OK; |
| 52814 | 52666 | goto moveto_finish; |
| | @@ -52822,11 +52674,11 @@ |
| 52822 | 52674 | if( lwr>upr ){ |
| 52823 | 52675 | break; |
| 52824 | 52676 | } |
| 52825 | 52677 | pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2); |
| 52826 | 52678 | } |
| 52827 | | - assert( lwr==upr+1 ); |
| 52679 | + assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); |
| 52828 | 52680 | assert( pPage->isInit ); |
| 52829 | 52681 | if( pPage->leaf ){ |
| 52830 | 52682 | chldPg = 0; |
| 52831 | 52683 | }else if( lwr>=pPage->nCell ){ |
| 52832 | 52684 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| | @@ -53087,10 +52939,12 @@ |
| 53087 | 52939 | } |
| 53088 | 52940 | if( rc ){ |
| 53089 | 52941 | pTrunk = 0; |
| 53090 | 52942 | goto end_allocate_page; |
| 53091 | 52943 | } |
| 52944 | + assert( pTrunk!=0 ); |
| 52945 | + assert( pTrunk->aData!=0 ); |
| 53092 | 52946 | |
| 53093 | 52947 | k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */ |
| 53094 | 52948 | if( k==0 && !searchList ){ |
| 53095 | 52949 | /* The trunk has no leaves and the list is not being searched. |
| 53096 | 52950 | ** So extract the trunk page itself and use it as the newly |
| | @@ -54214,17 +54068,19 @@ |
| 54214 | 54068 | ** This is safe because dropping a cell only overwrites the first |
| 54215 | 54069 | ** four bytes of it, and this function does not need the first |
| 54216 | 54070 | ** four bytes of the divider cell. So the pointer is safe to use |
| 54217 | 54071 | ** later on. |
| 54218 | 54072 | ** |
| 54219 | | - ** Unless SQLite is compiled in secure-delete mode. In this case, |
| 54073 | + ** But not if we are in secure-delete mode. In secure-delete mode, |
| 54220 | 54074 | ** the dropCell() routine will overwrite the entire cell with zeroes. |
| 54221 | 54075 | ** In this case, temporarily copy the cell into the aOvflSpace[] |
| 54222 | 54076 | ** buffer. It will be copied out again as soon as the aSpace[] buffer |
| 54223 | 54077 | ** is allocated. */ |
| 54224 | 54078 | if( pBt->secureDelete ){ |
| 54225 | | - int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); |
| 54079 | + int iOff; |
| 54080 | + |
| 54081 | + iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); |
| 54226 | 54082 | if( (iOff+szNew[i])>(int)pBt->usableSize ){ |
| 54227 | 54083 | rc = SQLITE_CORRUPT_BKPT; |
| 54228 | 54084 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
| 54229 | 54085 | goto balance_cleanup; |
| 54230 | 54086 | }else{ |
| | @@ -54640,10 +54496,11 @@ |
| 54640 | 54496 | int isDivider = 0; |
| 54641 | 54497 | while( i==iNextOld ){ |
| 54642 | 54498 | /* Cell i is the cell immediately following the last cell on old |
| 54643 | 54499 | ** sibling page j. If the siblings are not leaf pages of an |
| 54644 | 54500 | ** intkey b-tree, then cell i was a divider cell. */ |
| 54501 | + assert( j+1 < ArraySize(apCopy) ); |
| 54645 | 54502 | pOld = apCopy[++j]; |
| 54646 | 54503 | iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow; |
| 54647 | 54504 | if( pOld->nOverflow ){ |
| 54648 | 54505 | nOverflow = pOld->nOverflow; |
| 54649 | 54506 | iOverflow = i + !leafData + pOld->aOvfl[0].idx; |
| | @@ -56982,18 +56839,18 @@ |
| 56982 | 56839 | /* |
| 56983 | 56840 | ** Release all resources associated with an sqlite3_backup* handle. |
| 56984 | 56841 | */ |
| 56985 | 56842 | SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){ |
| 56986 | 56843 | sqlite3_backup **pp; /* Ptr to head of pagers backup list */ |
| 56987 | | - sqlite3_mutex *mutex; /* Mutex to protect source database */ |
| 56844 | + MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */ |
| 56988 | 56845 | int rc; /* Value to return */ |
| 56989 | 56846 | |
| 56990 | 56847 | /* Enter the mutexes */ |
| 56991 | 56848 | if( p==0 ) return SQLITE_OK; |
| 56992 | 56849 | sqlite3_mutex_enter(p->pSrcDb->mutex); |
| 56993 | 56850 | sqlite3BtreeEnter(p->pSrc); |
| 56994 | | - mutex = p->pSrcDb->mutex; |
| 56851 | + MUTEX_LOGIC( mutex = p->pSrcDb->mutex; ) |
| 56995 | 56852 | if( p->pDestDb ){ |
| 56996 | 56853 | sqlite3_mutex_enter(p->pDestDb->mutex); |
| 56997 | 56854 | } |
| 56998 | 56855 | |
| 56999 | 56856 | /* Detach this backup from the source pager. */ |
| | @@ -57108,13 +56965,21 @@ |
| 57108 | 56965 | ** goes wrong, the transaction on pTo is rolled back. If successful, the |
| 57109 | 56966 | ** transaction is committed before returning. |
| 57110 | 56967 | */ |
| 57111 | 56968 | SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 57112 | 56969 | int rc; |
| 56970 | + sqlite3_file *pFd; /* File descriptor for database pTo */ |
| 57113 | 56971 | sqlite3_backup b; |
| 57114 | 56972 | sqlite3BtreeEnter(pTo); |
| 57115 | 56973 | sqlite3BtreeEnter(pFrom); |
| 56974 | + |
| 56975 | + assert( sqlite3BtreeIsInTrans(pTo) ); |
| 56976 | + pFd = sqlite3PagerFile(sqlite3BtreePager(pTo)); |
| 56977 | + if( pFd->pMethods ){ |
| 56978 | + i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom); |
| 56979 | + sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte); |
| 56980 | + } |
| 57116 | 56981 | |
| 57117 | 56982 | /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set |
| 57118 | 56983 | ** to 0. This is used by the implementations of sqlite3_backup_step() |
| 57119 | 56984 | ** and sqlite3_backup_finish() to detect that they are being called |
| 57120 | 56985 | ** from this function, not directly by the user. |
| | @@ -57137,10 +57002,11 @@ |
| 57137 | 57002 | rc = sqlite3_backup_finish(&b); |
| 57138 | 57003 | if( rc==SQLITE_OK ){ |
| 57139 | 57004 | pTo->pBt->pageSizeFixed = 0; |
| 57140 | 57005 | } |
| 57141 | 57006 | |
| 57007 | + assert( sqlite3BtreeIsInTrans(pTo)==0 ); |
| 57142 | 57008 | sqlite3BtreeLeave(pFrom); |
| 57143 | 57009 | sqlite3BtreeLeave(pTo); |
| 57144 | 57010 | return rc; |
| 57145 | 57011 | } |
| 57146 | 57012 | #endif /* SQLITE_OMIT_VACUUM */ |
| | @@ -58171,15 +58037,15 @@ |
| 58171 | 58037 | *ppVal = 0; |
| 58172 | 58038 | return SQLITE_OK; |
| 58173 | 58039 | } |
| 58174 | 58040 | op = pExpr->op; |
| 58175 | 58041 | |
| 58176 | | - /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2. |
| 58042 | + /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3. |
| 58177 | 58043 | ** The ifdef here is to enable us to achieve 100% branch test coverage even |
| 58178 | | - ** when SQLITE_ENABLE_STAT2 is omitted. |
| 58044 | + ** when SQLITE_ENABLE_STAT3 is omitted. |
| 58179 | 58045 | */ |
| 58180 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 58046 | +#ifdef SQLITE_ENABLE_STAT3 |
| 58181 | 58047 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 58182 | 58048 | #else |
| 58183 | 58049 | if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 58184 | 58050 | #endif |
| 58185 | 58051 | |
| | @@ -58874,12 +58740,12 @@ |
| 58874 | 58740 | /* |
| 58875 | 58741 | ** Change the P2 operand of instruction addr so that it points to |
| 58876 | 58742 | ** the address of the next instruction to be coded. |
| 58877 | 58743 | */ |
| 58878 | 58744 | SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
| 58879 | | - assert( addr>=0 ); |
| 58880 | | - sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 58745 | + assert( addr>=0 || p->db->mallocFailed ); |
| 58746 | + if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 58881 | 58747 | } |
| 58882 | 58748 | |
| 58883 | 58749 | |
| 58884 | 58750 | /* |
| 58885 | 58751 | ** If the input FuncDef structure is ephemeral, then free it. If |
| | @@ -59080,34 +58946,33 @@ |
| 59080 | 58946 | ** Change the comment on the the most recently coded instruction. Or |
| 59081 | 58947 | ** insert a No-op and add the comment to that new instruction. This |
| 59082 | 58948 | ** makes the code easier to read during debugging. None of this happens |
| 59083 | 58949 | ** in a production build. |
| 59084 | 58950 | */ |
| 59085 | | -SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
| 59086 | | - va_list ap; |
| 59087 | | - if( !p ) return; |
| 58951 | +static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ |
| 59088 | 58952 | assert( p->nOp>0 || p->aOp==0 ); |
| 59089 | 58953 | assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
| 59090 | 58954 | if( p->nOp ){ |
| 59091 | | - char **pz = &p->aOp[p->nOp-1].zComment; |
| 58955 | + assert( p->aOp ); |
| 58956 | + sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); |
| 58957 | + p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); |
| 58958 | + } |
| 58959 | +} |
| 58960 | +SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
| 58961 | + va_list ap; |
| 58962 | + if( p ){ |
| 59092 | 58963 | va_start(ap, zFormat); |
| 59093 | | - sqlite3DbFree(p->db, *pz); |
| 59094 | | - *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
| 58964 | + vdbeVComment(p, zFormat, ap); |
| 59095 | 58965 | va_end(ap); |
| 59096 | 58966 | } |
| 59097 | 58967 | } |
| 59098 | 58968 | SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ |
| 59099 | 58969 | 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; |
| 58970 | + if( p ){ |
| 58971 | + sqlite3VdbeAddOp0(p, OP_Noop); |
| 59106 | 58972 | va_start(ap, zFormat); |
| 59107 | | - sqlite3DbFree(p->db, *pz); |
| 59108 | | - *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
| 58973 | + vdbeVComment(p, zFormat, ap); |
| 59109 | 58974 | va_end(ap); |
| 59110 | 58975 | } |
| 59111 | 58976 | } |
| 59112 | 58977 | #endif /* NDEBUG */ |
| 59113 | 58978 | |
| | @@ -59441,11 +59306,11 @@ |
| 59441 | 59306 | SubProgram **apSub = 0; /* Array of sub-vdbes */ |
| 59442 | 59307 | Mem *pSub = 0; /* Memory cell hold array of subprogs */ |
| 59443 | 59308 | sqlite3 *db = p->db; /* The database connection */ |
| 59444 | 59309 | int i; /* Loop counter */ |
| 59445 | 59310 | int rc = SQLITE_OK; /* Return code */ |
| 59446 | | - Mem *pMem = p->pResultSet = &p->aMem[1]; /* First Mem of result set */ |
| 59311 | + Mem *pMem = &p->aMem[1]; /* First Mem of result set */ |
| 59447 | 59312 | |
| 59448 | 59313 | assert( p->explain ); |
| 59449 | 59314 | assert( p->magic==VDBE_MAGIC_RUN ); |
| 59450 | 59315 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); |
| 59451 | 59316 | |
| | @@ -59452,10 +59317,11 @@ |
| 59452 | 59317 | /* Even though this opcode does not use dynamic strings for |
| 59453 | 59318 | ** the result, result columns may become dynamic if the user calls |
| 59454 | 59319 | ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
| 59455 | 59320 | */ |
| 59456 | 59321 | releaseMemArray(pMem, 8); |
| 59322 | + p->pResultSet = 0; |
| 59457 | 59323 | |
| 59458 | 59324 | if( p->rc==SQLITE_NOMEM ){ |
| 59459 | 59325 | /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 59460 | 59326 | ** sqlite3_column_text16() failed. */ |
| 59461 | 59327 | db->mallocFailed = 1; |
| | @@ -59606,10 +59472,11 @@ |
| 59606 | 59472 | pMem->type = SQLITE_NULL; |
| 59607 | 59473 | } |
| 59608 | 59474 | } |
| 59609 | 59475 | |
| 59610 | 59476 | p->nResColumn = 8 - 4*(p->explain-1); |
| 59477 | + p->pResultSet = &p->aMem[1]; |
| 59611 | 59478 | p->rc = SQLITE_OK; |
| 59612 | 59479 | rc = SQLITE_ROW; |
| 59613 | 59480 | } |
| 59614 | 59481 | return rc; |
| 59615 | 59482 | } |
| | @@ -61361,11 +61228,11 @@ |
| 61361 | 61228 | ** than 2GiB are support - anything large must be database corruption. |
| 61362 | 61229 | ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so |
| 61363 | 61230 | ** this code can safely assume that nCellKey is 32-bits |
| 61364 | 61231 | */ |
| 61365 | 61232 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 61366 | | - rc = sqlite3BtreeKeySize(pCur, &nCellKey); |
| 61233 | + VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
| 61367 | 61234 | assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
| 61368 | 61235 | assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); |
| 61369 | 61236 | |
| 61370 | 61237 | /* Read in the complete content of the index entry */ |
| 61371 | 61238 | memset(&m, 0, sizeof(m)); |
| | @@ -61436,11 +61303,11 @@ |
| 61436 | 61303 | int rc; |
| 61437 | 61304 | BtCursor *pCur = pC->pCursor; |
| 61438 | 61305 | Mem m; |
| 61439 | 61306 | |
| 61440 | 61307 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 61441 | | - rc = sqlite3BtreeKeySize(pCur, &nCellKey); |
| 61308 | + VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
| 61442 | 61309 | assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
| 61443 | 61310 | /* nCellKey will always be between 0 and 0xffffffff because of the say |
| 61444 | 61311 | ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
| 61445 | 61312 | if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
| 61446 | 61313 | *res = 0; |
| | @@ -65712,20 +65579,20 @@ |
| 65712 | 65579 | }else if( u.am.pC->cacheStatus==p->cacheCtr ){ |
| 65713 | 65580 | u.am.payloadSize = u.am.pC->payloadSize; |
| 65714 | 65581 | u.am.zRec = (char*)u.am.pC->aRow; |
| 65715 | 65582 | }else if( u.am.pC->isIndex ){ |
| 65716 | 65583 | assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) ); |
| 65717 | | - rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64); |
| 65584 | + VVA_ONLY(rc =) sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64); |
| 65718 | 65585 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| 65719 | 65586 | /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the |
| 65720 | 65587 | ** payload size, so it is impossible for u.am.payloadSize64 to be |
| 65721 | 65588 | ** larger than 32 bits. */ |
| 65722 | 65589 | assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 ); |
| 65723 | 65590 | u.am.payloadSize = (u32)u.am.payloadSize64; |
| 65724 | 65591 | }else{ |
| 65725 | 65592 | assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) ); |
| 65726 | | - rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize); |
| 65593 | + VVA_ONLY(rc =) sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize); |
| 65727 | 65594 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 65728 | 65595 | } |
| 65729 | 65596 | }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){ |
| 65730 | 65597 | u.am.pReg = &aMem[u.am.pC->pseudoTableReg]; |
| 65731 | 65598 | assert( u.am.pReg->flags & MEM_Blob ); |
| | @@ -67773,18 +67640,18 @@ |
| 67773 | 67640 | rc = sqlite3VdbeCursorMoveto(u.bk.pC); |
| 67774 | 67641 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 67775 | 67642 | |
| 67776 | 67643 | if( u.bk.pC->isIndex ){ |
| 67777 | 67644 | assert( !u.bk.pC->isTable ); |
| 67778 | | - rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64); |
| 67645 | + VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64); |
| 67779 | 67646 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| 67780 | 67647 | if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67781 | 67648 | goto too_big; |
| 67782 | 67649 | } |
| 67783 | 67650 | u.bk.n = (u32)u.bk.n64; |
| 67784 | 67651 | }else{ |
| 67785 | | - rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n); |
| 67652 | + VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n); |
| 67786 | 67653 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 67787 | 67654 | if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67788 | 67655 | goto too_big; |
| 67789 | 67656 | } |
| 67790 | 67657 | } |
| | @@ -73417,11 +73284,12 @@ |
| 73417 | 73284 | pNew->flags |= EP_IntValue; |
| 73418 | 73285 | pNew->u.iValue = iValue; |
| 73419 | 73286 | }else{ |
| 73420 | 73287 | int c; |
| 73421 | 73288 | pNew->u.zToken = (char*)&pNew[1]; |
| 73422 | | - memcpy(pNew->u.zToken, pToken->z, pToken->n); |
| 73289 | + assert( pToken->z!=0 || pToken->n==0 ); |
| 73290 | + if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); |
| 73423 | 73291 | pNew->u.zToken[pToken->n] = 0; |
| 73424 | 73292 | if( dequote && nExtra>=3 |
| 73425 | 73293 | && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){ |
| 73426 | 73294 | sqlite3Dequote(pNew->u.zToken); |
| 73427 | 73295 | if( c=='"' ) pNew->flags |= EP_DblQuoted; |
| | @@ -74456,15 +74324,23 @@ |
| 74456 | 74324 | ** ephemeral table. |
| 74457 | 74325 | */ |
| 74458 | 74326 | p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); |
| 74459 | 74327 | if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ |
| 74460 | 74328 | 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 | 74329 | Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ |
| 74464 | | - Table *pTab = p->pSrc->a[0].pTab; /* Table <table>. */ |
| 74330 | + Table *pTab; /* Table <table>. */ |
| 74331 | + Expr *pExpr; /* Expression <column> */ |
| 74332 | + int iCol; /* Index of column <column> */ |
| 74465 | 74333 | int iDb; /* Database idx for pTab */ |
| 74334 | + |
| 74335 | + assert( p ); /* Because of isCandidateForInOpt(p) */ |
| 74336 | + assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 74337 | + assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 74338 | + assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 74339 | + pTab = p->pSrc->a[0].pTab; |
| 74340 | + pExpr = p->pEList->a[0].pExpr; |
| 74341 | + iCol = pExpr->iColumn; |
| 74466 | 74342 | |
| 74467 | 74343 | /* Code an OP_VerifyCookie and OP_TableLock for <table>. */ |
| 74468 | 74344 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 74469 | 74345 | sqlite3CodeVerifySchema(pParse, iDb); |
| 74470 | 74346 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| | @@ -76467,11 +76343,11 @@ |
| 76467 | 76343 | if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){ |
| 76468 | 76344 | return 2; |
| 76469 | 76345 | } |
| 76470 | 76346 | }else if( pA->op!=TK_COLUMN && pA->u.zToken ){ |
| 76471 | 76347 | if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2; |
| 76472 | | - if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
| 76348 | + if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
| 76473 | 76349 | return 2; |
| 76474 | 76350 | } |
| 76475 | 76351 | } |
| 76476 | 76352 | if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1; |
| 76477 | 76353 | if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2; |
| | @@ -77610,10 +77486,112 @@ |
| 77610 | 77486 | ** May you find forgiveness for yourself and forgive others. |
| 77611 | 77487 | ** May you share freely, never taking more than you give. |
| 77612 | 77488 | ** |
| 77613 | 77489 | ************************************************************************* |
| 77614 | 77490 | ** This file contains code associated with the ANALYZE command. |
| 77491 | +** |
| 77492 | +** The ANALYZE command gather statistics about the content of tables |
| 77493 | +** and indices. These statistics are made available to the query planner |
| 77494 | +** to help it make better decisions about how to perform queries. |
| 77495 | +** |
| 77496 | +** The following system tables are or have been supported: |
| 77497 | +** |
| 77498 | +** CREATE TABLE sqlite_stat1(tbl, idx, stat); |
| 77499 | +** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); |
| 77500 | +** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample); |
| 77501 | +** |
| 77502 | +** Additional tables might be added in future releases of SQLite. |
| 77503 | +** The sqlite_stat2 table is not created or used unless the SQLite version |
| 77504 | +** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled |
| 77505 | +** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. |
| 77506 | +** The sqlite_stat2 table is superceded by sqlite_stat3, which is only |
| 77507 | +** created and used by SQLite versions 3.7.9 and later and with |
| 77508 | +** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3 |
| 77509 | +** is a superset of sqlite_stat2. |
| 77510 | +** |
| 77511 | +** Format of sqlite_stat1: |
| 77512 | +** |
| 77513 | +** There is normally one row per index, with the index identified by the |
| 77514 | +** name in the idx column. The tbl column is the name of the table to |
| 77515 | +** which the index belongs. In each such row, the stat column will be |
| 77516 | +** a string consisting of a list of integers. The first integer in this |
| 77517 | +** list is the number of rows in the index and in the table. The second |
| 77518 | +** integer is the average number of rows in the index that have the same |
| 77519 | +** value in the first column of the index. The third integer is the average |
| 77520 | +** number of rows in the index that have the same value for the first two |
| 77521 | +** columns. The N-th integer (for N>1) is the average number of rows in |
| 77522 | +** the index which have the same value for the first N-1 columns. For |
| 77523 | +** a K-column index, there will be K+1 integers in the stat column. If |
| 77524 | +** the index is unique, then the last integer will be 1. |
| 77525 | +** |
| 77526 | +** The list of integers in the stat column can optionally be followed |
| 77527 | +** by the keyword "unordered". The "unordered" keyword, if it is present, |
| 77528 | +** must be separated from the last integer by a single space. If the |
| 77529 | +** "unordered" keyword is present, then the query planner assumes that |
| 77530 | +** the index is unordered and will not use the index for a range query. |
| 77531 | +** |
| 77532 | +** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat |
| 77533 | +** column contains a single integer which is the (estimated) number of |
| 77534 | +** rows in the table identified by sqlite_stat1.tbl. |
| 77535 | +** |
| 77536 | +** Format of sqlite_stat2: |
| 77537 | +** |
| 77538 | +** The sqlite_stat2 is only created and is only used if SQLite is compiled |
| 77539 | +** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between |
| 77540 | +** 3.6.18 and 3.7.8. The "stat2" table contains additional information |
| 77541 | +** about the distribution of keys within an index. The index is identified by |
| 77542 | +** the "idx" column and the "tbl" column is the name of the table to which |
| 77543 | +** the index belongs. There are usually 10 rows in the sqlite_stat2 |
| 77544 | +** table for each index. |
| 77545 | +** |
| 77546 | +** The sqlite_stat2 entries for an index that have sampleno between 0 and 9 |
| 77547 | +** inclusive are samples of the left-most key value in the index taken at |
| 77548 | +** evenly spaced points along the index. Let the number of samples be S |
| 77549 | +** (10 in the standard build) and let C be the number of rows in the index. |
| 77550 | +** Then the sampled rows are given by: |
| 77551 | +** |
| 77552 | +** rownumber = (i*C*2 + C)/(S*2) |
| 77553 | +** |
| 77554 | +** For i between 0 and S-1. Conceptually, the index space is divided into |
| 77555 | +** S uniform buckets and the samples are the middle row from each bucket. |
| 77556 | +** |
| 77557 | +** The format for sqlite_stat2 is recorded here for legacy reference. This |
| 77558 | +** version of SQLite does not support sqlite_stat2. It neither reads nor |
| 77559 | +** writes the sqlite_stat2 table. This version of SQLite only supports |
| 77560 | +** sqlite_stat3. |
| 77561 | +** |
| 77562 | +** Format for sqlite_stat3: |
| 77563 | +** |
| 77564 | +** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is |
| 77565 | +** used to avoid compatibility problems. |
| 77566 | +** |
| 77567 | +** The format of the sqlite_stat3 table is similar to the format of |
| 77568 | +** the sqlite_stat2 table. There are multiple entries for each index. |
| 77569 | +** The idx column names the index and the tbl column is the table of the |
| 77570 | +** index. If the idx and tbl columns are the same, then the sample is |
| 77571 | +** of the INTEGER PRIMARY KEY. The sample column is a value taken from |
| 77572 | +** the left-most column of the index. The nEq column is the approximate |
| 77573 | +** number of entires in the index whose left-most column exactly matches |
| 77574 | +** the sample. nLt is the approximate number of entires whose left-most |
| 77575 | +** column is less than the sample. The nDLt column is the approximate |
| 77576 | +** number of distinct left-most entries in the index that are less than |
| 77577 | +** the sample. |
| 77578 | +** |
| 77579 | +** Future versions of SQLite might change to store a string containing |
| 77580 | +** multiple integers values in the nDLt column of sqlite_stat3. The first |
| 77581 | +** integer will be the number of prior index entires that are distinct in |
| 77582 | +** the left-most column. The second integer will be the number of prior index |
| 77583 | +** entries that are distinct in the first two columns. The third integer |
| 77584 | +** will be the number of prior index entries that are distinct in the first |
| 77585 | +** three columns. And so forth. With that extension, the nDLt field is |
| 77586 | +** similar in function to the sqlite_stat1.stat field. |
| 77587 | +** |
| 77588 | +** There can be an arbitrary number of sqlite_stat3 entries per index. |
| 77589 | +** The ANALYZE command will typically generate sqlite_stat3 tables |
| 77590 | +** that contain between 10 and 40 samples which are distributed across |
| 77591 | +** the key space, though not uniformly, and which include samples with |
| 77592 | +** largest possible nEq values. |
| 77615 | 77593 | */ |
| 77616 | 77594 | #ifndef SQLITE_OMIT_ANALYZE |
| 77617 | 77595 | |
| 77618 | 77596 | /* |
| 77619 | 77597 | ** This routine generates code that opens the sqlite_stat1 table for |
| | @@ -77641,12 +77619,12 @@ |
| 77641 | 77619 | static const struct { |
| 77642 | 77620 | const char *zName; |
| 77643 | 77621 | const char *zCols; |
| 77644 | 77622 | } aTable[] = { |
| 77645 | 77623 | { "sqlite_stat1", "tbl,idx,stat" }, |
| 77646 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 77647 | | - { "sqlite_stat2", "tbl,idx,sampleno,sample" }, |
| 77624 | +#ifdef SQLITE_ENABLE_STAT3 |
| 77625 | + { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" }, |
| 77648 | 77626 | #endif |
| 77649 | 77627 | }; |
| 77650 | 77628 | |
| 77651 | 77629 | int aRoot[] = {0, 0}; |
| 77652 | 77630 | u8 aCreateTbl[] = {0, 0}; |
| | @@ -77658,10 +77636,13 @@ |
| 77658 | 77636 | if( v==0 ) return; |
| 77659 | 77637 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 77660 | 77638 | assert( sqlite3VdbeDb(v)==db ); |
| 77661 | 77639 | pDb = &db->aDb[iDb]; |
| 77662 | 77640 | |
| 77641 | + /* Create new statistic tables if they do not exist, or clear them |
| 77642 | + ** if they do already exist. |
| 77643 | + */ |
| 77663 | 77644 | for(i=0; i<ArraySize(aTable); i++){ |
| 77664 | 77645 | const char *zTab = aTable[i].zName; |
| 77665 | 77646 | Table *pStat; |
| 77666 | 77647 | if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){ |
| 77667 | 77648 | /* The sqlite_stat[12] table does not exist. Create it. Note that a |
| | @@ -77688,17 +77669,237 @@ |
| 77688 | 77669 | sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); |
| 77689 | 77670 | } |
| 77690 | 77671 | } |
| 77691 | 77672 | } |
| 77692 | 77673 | |
| 77693 | | - /* Open the sqlite_stat[12] tables for writing. */ |
| 77674 | + /* Open the sqlite_stat[13] tables for writing. */ |
| 77694 | 77675 | for(i=0; i<ArraySize(aTable); i++){ |
| 77695 | 77676 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb); |
| 77696 | 77677 | sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32); |
| 77697 | 77678 | sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
| 77698 | 77679 | } |
| 77699 | 77680 | } |
| 77681 | + |
| 77682 | +/* |
| 77683 | +** Recommended number of samples for sqlite_stat3 |
| 77684 | +*/ |
| 77685 | +#ifndef SQLITE_STAT3_SAMPLES |
| 77686 | +# define SQLITE_STAT3_SAMPLES 24 |
| 77687 | +#endif |
| 77688 | + |
| 77689 | +/* |
| 77690 | +** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() - |
| 77691 | +** share an instance of the following structure to hold their state |
| 77692 | +** information. |
| 77693 | +*/ |
| 77694 | +typedef struct Stat3Accum Stat3Accum; |
| 77695 | +struct Stat3Accum { |
| 77696 | + tRowcnt nRow; /* Number of rows in the entire table */ |
| 77697 | + tRowcnt nPSample; /* How often to do a periodic sample */ |
| 77698 | + int iMin; /* Index of entry with minimum nEq and hash */ |
| 77699 | + int mxSample; /* Maximum number of samples to accumulate */ |
| 77700 | + int nSample; /* Current number of samples */ |
| 77701 | + u32 iPrn; /* Pseudo-random number used for sampling */ |
| 77702 | + struct Stat3Sample { |
| 77703 | + i64 iRowid; /* Rowid in main table of the key */ |
| 77704 | + tRowcnt nEq; /* sqlite_stat3.nEq */ |
| 77705 | + tRowcnt nLt; /* sqlite_stat3.nLt */ |
| 77706 | + tRowcnt nDLt; /* sqlite_stat3.nDLt */ |
| 77707 | + u8 isPSample; /* True if a periodic sample */ |
| 77708 | + u32 iHash; /* Tiebreaker hash */ |
| 77709 | + } *a; /* An array of samples */ |
| 77710 | +}; |
| 77711 | + |
| 77712 | +#ifdef SQLITE_ENABLE_STAT3 |
| 77713 | +/* |
| 77714 | +** Implementation of the stat3_init(C,S) SQL function. The two parameters |
| 77715 | +** are the number of rows in the table or index (C) and the number of samples |
| 77716 | +** to accumulate (S). |
| 77717 | +** |
| 77718 | +** This routine allocates the Stat3Accum object. |
| 77719 | +** |
| 77720 | +** The return value is the Stat3Accum object (P). |
| 77721 | +*/ |
| 77722 | +static void stat3Init( |
| 77723 | + sqlite3_context *context, |
| 77724 | + int argc, |
| 77725 | + sqlite3_value **argv |
| 77726 | +){ |
| 77727 | + Stat3Accum *p; |
| 77728 | + tRowcnt nRow; |
| 77729 | + int mxSample; |
| 77730 | + int n; |
| 77731 | + |
| 77732 | + UNUSED_PARAMETER(argc); |
| 77733 | + nRow = (tRowcnt)sqlite3_value_int64(argv[0]); |
| 77734 | + mxSample = sqlite3_value_int(argv[1]); |
| 77735 | + n = sizeof(*p) + sizeof(p->a[0])*mxSample; |
| 77736 | + p = sqlite3_malloc( n ); |
| 77737 | + if( p==0 ){ |
| 77738 | + sqlite3_result_error_nomem(context); |
| 77739 | + return; |
| 77740 | + } |
| 77741 | + memset(p, 0, n); |
| 77742 | + p->a = (struct Stat3Sample*)&p[1]; |
| 77743 | + p->nRow = nRow; |
| 77744 | + p->mxSample = mxSample; |
| 77745 | + p->nPSample = p->nRow/(mxSample/3+1) + 1; |
| 77746 | + sqlite3_randomness(sizeof(p->iPrn), &p->iPrn); |
| 77747 | + sqlite3_result_blob(context, p, sizeof(p), sqlite3_free); |
| 77748 | +} |
| 77749 | +static const FuncDef stat3InitFuncdef = { |
| 77750 | + 2, /* nArg */ |
| 77751 | + SQLITE_UTF8, /* iPrefEnc */ |
| 77752 | + 0, /* flags */ |
| 77753 | + 0, /* pUserData */ |
| 77754 | + 0, /* pNext */ |
| 77755 | + stat3Init, /* xFunc */ |
| 77756 | + 0, /* xStep */ |
| 77757 | + 0, /* xFinalize */ |
| 77758 | + "stat3_init", /* zName */ |
| 77759 | + 0, /* pHash */ |
| 77760 | + 0 /* pDestructor */ |
| 77761 | +}; |
| 77762 | + |
| 77763 | + |
| 77764 | +/* |
| 77765 | +** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The |
| 77766 | +** arguments describe a single key instance. This routine makes the |
| 77767 | +** decision about whether or not to retain this key for the sqlite_stat3 |
| 77768 | +** table. |
| 77769 | +** |
| 77770 | +** The return value is NULL. |
| 77771 | +*/ |
| 77772 | +static void stat3Push( |
| 77773 | + sqlite3_context *context, |
| 77774 | + int argc, |
| 77775 | + sqlite3_value **argv |
| 77776 | +){ |
| 77777 | + Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]); |
| 77778 | + tRowcnt nEq = sqlite3_value_int64(argv[0]); |
| 77779 | + tRowcnt nLt = sqlite3_value_int64(argv[1]); |
| 77780 | + tRowcnt nDLt = sqlite3_value_int64(argv[2]); |
| 77781 | + i64 rowid = sqlite3_value_int64(argv[3]); |
| 77782 | + u8 isPSample = 0; |
| 77783 | + u8 doInsert = 0; |
| 77784 | + int iMin = p->iMin; |
| 77785 | + struct Stat3Sample *pSample; |
| 77786 | + int i; |
| 77787 | + u32 h; |
| 77788 | + |
| 77789 | + UNUSED_PARAMETER(context); |
| 77790 | + UNUSED_PARAMETER(argc); |
| 77791 | + if( nEq==0 ) return; |
| 77792 | + h = p->iPrn = p->iPrn*1103515245 + 12345; |
| 77793 | + if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){ |
| 77794 | + doInsert = isPSample = 1; |
| 77795 | + }else if( p->nSample<p->mxSample ){ |
| 77796 | + doInsert = 1; |
| 77797 | + }else{ |
| 77798 | + if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){ |
| 77799 | + doInsert = 1; |
| 77800 | + } |
| 77801 | + } |
| 77802 | + if( !doInsert ) return; |
| 77803 | + if( p->nSample==p->mxSample ){ |
| 77804 | + assert( p->nSample - iMin - 1 >= 0 ); |
| 77805 | + memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1)); |
| 77806 | + pSample = &p->a[p->nSample-1]; |
| 77807 | + }else{ |
| 77808 | + pSample = &p->a[p->nSample++]; |
| 77809 | + } |
| 77810 | + pSample->iRowid = rowid; |
| 77811 | + pSample->nEq = nEq; |
| 77812 | + pSample->nLt = nLt; |
| 77813 | + pSample->nDLt = nDLt; |
| 77814 | + pSample->iHash = h; |
| 77815 | + pSample->isPSample = isPSample; |
| 77816 | + |
| 77817 | + /* Find the new minimum */ |
| 77818 | + if( p->nSample==p->mxSample ){ |
| 77819 | + pSample = p->a; |
| 77820 | + i = 0; |
| 77821 | + while( pSample->isPSample ){ |
| 77822 | + i++; |
| 77823 | + pSample++; |
| 77824 | + assert( i<p->nSample ); |
| 77825 | + } |
| 77826 | + nEq = pSample->nEq; |
| 77827 | + h = pSample->iHash; |
| 77828 | + iMin = i; |
| 77829 | + for(i++, pSample++; i<p->nSample; i++, pSample++){ |
| 77830 | + if( pSample->isPSample ) continue; |
| 77831 | + if( pSample->nEq<nEq |
| 77832 | + || (pSample->nEq==nEq && pSample->iHash<h) |
| 77833 | + ){ |
| 77834 | + iMin = i; |
| 77835 | + nEq = pSample->nEq; |
| 77836 | + h = pSample->iHash; |
| 77837 | + } |
| 77838 | + } |
| 77839 | + p->iMin = iMin; |
| 77840 | + } |
| 77841 | +} |
| 77842 | +static const FuncDef stat3PushFuncdef = { |
| 77843 | + 5, /* nArg */ |
| 77844 | + SQLITE_UTF8, /* iPrefEnc */ |
| 77845 | + 0, /* flags */ |
| 77846 | + 0, /* pUserData */ |
| 77847 | + 0, /* pNext */ |
| 77848 | + stat3Push, /* xFunc */ |
| 77849 | + 0, /* xStep */ |
| 77850 | + 0, /* xFinalize */ |
| 77851 | + "stat3_push", /* zName */ |
| 77852 | + 0, /* pHash */ |
| 77853 | + 0 /* pDestructor */ |
| 77854 | +}; |
| 77855 | + |
| 77856 | +/* |
| 77857 | +** Implementation of the stat3_get(P,N,...) SQL function. This routine is |
| 77858 | +** used to query the results. Content is returned for the Nth sqlite_stat3 |
| 77859 | +** row where N is between 0 and S-1 and S is the number of samples. The |
| 77860 | +** value returned depends on the number of arguments. |
| 77861 | +** |
| 77862 | +** argc==2 result: rowid |
| 77863 | +** argc==3 result: nEq |
| 77864 | +** argc==4 result: nLt |
| 77865 | +** argc==5 result: nDLt |
| 77866 | +*/ |
| 77867 | +static void stat3Get( |
| 77868 | + sqlite3_context *context, |
| 77869 | + int argc, |
| 77870 | + sqlite3_value **argv |
| 77871 | +){ |
| 77872 | + int n = sqlite3_value_int(argv[1]); |
| 77873 | + Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]); |
| 77874 | + |
| 77875 | + assert( p!=0 ); |
| 77876 | + if( p->nSample<=n ) return; |
| 77877 | + switch( argc ){ |
| 77878 | + case 2: sqlite3_result_int64(context, p->a[n].iRowid); break; |
| 77879 | + case 3: sqlite3_result_int64(context, p->a[n].nEq); break; |
| 77880 | + case 4: sqlite3_result_int64(context, p->a[n].nLt); break; |
| 77881 | + default: sqlite3_result_int64(context, p->a[n].nDLt); break; |
| 77882 | + } |
| 77883 | +} |
| 77884 | +static const FuncDef stat3GetFuncdef = { |
| 77885 | + -1, /* nArg */ |
| 77886 | + SQLITE_UTF8, /* iPrefEnc */ |
| 77887 | + 0, /* flags */ |
| 77888 | + 0, /* pUserData */ |
| 77889 | + 0, /* pNext */ |
| 77890 | + stat3Get, /* xFunc */ |
| 77891 | + 0, /* xStep */ |
| 77892 | + 0, /* xFinalize */ |
| 77893 | + "stat3_get", /* zName */ |
| 77894 | + 0, /* pHash */ |
| 77895 | + 0 /* pDestructor */ |
| 77896 | +}; |
| 77897 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 77898 | + |
| 77899 | + |
| 77900 | + |
| 77700 | 77901 | |
| 77701 | 77902 | /* |
| 77702 | 77903 | ** Generate code to do an analysis of all indices associated with |
| 77703 | 77904 | ** a single table. |
| 77704 | 77905 | */ |
| | @@ -77718,24 +77919,31 @@ |
| 77718 | 77919 | int endOfLoop; /* The end of the loop */ |
| 77719 | 77920 | int jZeroRows = -1; /* Jump from here if number of rows is zero */ |
| 77720 | 77921 | int iDb; /* Index of database containing pTab */ |
| 77721 | 77922 | int regTabname = iMem++; /* Register containing table name */ |
| 77722 | 77923 | 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 */ |
| 77924 | + int regStat1 = iMem++; /* The stat column of sqlite_stat1 */ |
| 77925 | +#ifdef SQLITE_ENABLE_STAT3 |
| 77926 | + int regNumEq = regStat1; /* Number of instances. Same as regStat1 */ |
| 77927 | + int regNumLt = iMem++; /* Number of keys less than regSample */ |
| 77928 | + int regNumDLt = iMem++; /* Number of distinct keys less than regSample */ |
| 77929 | + int regSample = iMem++; /* The next sample value */ |
| 77930 | + int regRowid = regSample; /* Rowid of a sample */ |
| 77931 | + int regAccum = iMem++; /* Register to hold Stat3Accum object */ |
| 77932 | + int regLoop = iMem++; /* Loop counter */ |
| 77933 | + int regCount = iMem++; /* Number of rows in the table or index */ |
| 77934 | + int regTemp1 = iMem++; /* Intermediate register */ |
| 77935 | + int regTemp2 = iMem++; /* Intermediate register */ |
| 77936 | + int once = 1; /* One-time initialization */ |
| 77937 | + int shortJump = 0; /* Instruction address */ |
| 77938 | + int iTabCur = pParse->nTab++; /* Table cursor */ |
| 77939 | +#endif |
| 77940 | + int regCol = iMem++; /* Content of a column in analyzed table */ |
| 77725 | 77941 | int regRec = iMem++; /* Register holding completed record */ |
| 77726 | 77942 | 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 |
| 77943 | + int regNewRowid = iMem++; /* Rowid for the inserted record */ |
| 77944 | + |
| 77737 | 77945 | |
| 77738 | 77946 | v = sqlite3GetVdbe(pParse); |
| 77739 | 77947 | if( v==0 || NEVER(pTab==0) ){ |
| 77740 | 77948 | return; |
| 77741 | 77949 | } |
| | @@ -77764,13 +77972,18 @@ |
| 77764 | 77972 | iIdxCur = pParse->nTab++; |
| 77765 | 77973 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); |
| 77766 | 77974 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 77767 | 77975 | int nCol; |
| 77768 | 77976 | KeyInfo *pKey; |
| 77977 | + int addrIfNot = 0; /* address of OP_IfNot */ |
| 77978 | + int *aChngAddr; /* Array of jump instruction addresses */ |
| 77769 | 77979 | |
| 77770 | 77980 | if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; |
| 77981 | + VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName)); |
| 77771 | 77982 | nCol = pIdx->nColumn; |
| 77983 | + aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol); |
| 77984 | + if( aChngAddr==0 ) continue; |
| 77772 | 77985 | pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 77773 | 77986 | if( iMem+1+(nCol*2)>pParse->nMem ){ |
| 77774 | 77987 | pParse->nMem = iMem+1+(nCol*2); |
| 77775 | 77988 | } |
| 77776 | 77989 | |
| | @@ -77781,35 +77994,24 @@ |
| 77781 | 77994 | VdbeComment((v, "%s", pIdx->zName)); |
| 77782 | 77995 | |
| 77783 | 77996 | /* Populate the register containing the index name. */ |
| 77784 | 77997 | sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); |
| 77785 | 77998 | |
| 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 |
| 77999 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78000 | + if( once ){ |
| 78001 | + once = 0; |
| 78002 | + sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); |
| 78003 | + } |
| 78004 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount); |
| 78005 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1); |
| 78006 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq); |
| 78007 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt); |
| 78008 | + sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt); |
| 78009 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum, |
| 78010 | + (char*)&stat3InitFuncdef, P4_FUNCDEF); |
| 78011 | + sqlite3VdbeChangeP5(v, 2); |
| 78012 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 77811 | 78013 | |
| 77812 | 78014 | /* The block of memory cells initialized here is used as follows. |
| 77813 | 78015 | ** |
| 77814 | 78016 | ** iMem: |
| 77815 | 78017 | ** The total number of rows in the table. |
| | @@ -77835,79 +78037,87 @@ |
| 77835 | 78037 | /* Start the analysis loop. This loop runs through all the entries in |
| 77836 | 78038 | ** the index b-tree. */ |
| 77837 | 78039 | endOfLoop = sqlite3VdbeMakeLabel(v); |
| 77838 | 78040 | sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop); |
| 77839 | 78041 | topOfLoop = sqlite3VdbeCurrentAddr(v); |
| 77840 | | - sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); |
| 78042 | + sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */ |
| 77841 | 78043 | |
| 77842 | 78044 | for(i=0; i<nCol; i++){ |
| 77843 | 78045 | CollSeq *pColl; |
| 77844 | 78046 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol); |
| 77845 | 78047 | 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 | 78048 | /* Always record the very first row */ |
| 77878 | | - sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 78049 | + addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 77879 | 78050 | } |
| 77880 | 78051 | assert( pIdx->azColl!=0 ); |
| 77881 | 78052 | assert( pIdx->azColl[i]!=0 ); |
| 77882 | 78053 | pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 77883 | | - sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 77884 | | - (char*)pColl, P4_COLLSEQ); |
| 78054 | + aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 78055 | + (char*)pColl, P4_COLLSEQ); |
| 77885 | 78056 | 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; |
| 78057 | + VdbeComment((v, "jump if column %d changed", i)); |
| 78058 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78059 | + if( i==0 ){ |
| 78060 | + sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1); |
| 78061 | + VdbeComment((v, "incr repeat count")); |
| 78062 | + } |
| 78063 | +#endif |
| 77893 | 78064 | } |
| 77894 | 78065 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop); |
| 77895 | 78066 | for(i=0; i<nCol; i++){ |
| 77896 | | - int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2); |
| 78067 | + sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */ |
| 77897 | 78068 | if( i==0 ){ |
| 77898 | | - sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */ |
| 78069 | + sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */ |
| 78070 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78071 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, |
| 78072 | + (char*)&stat3PushFuncdef, P4_FUNCDEF); |
| 78073 | + sqlite3VdbeChangeP5(v, 5); |
| 78074 | + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid); |
| 78075 | + sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt); |
| 78076 | + sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1); |
| 78077 | + sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq); |
| 78078 | +#endif |
| 77899 | 78079 | } |
| 77900 | | - sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */ |
| 77901 | 78080 | sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1); |
| 77902 | 78081 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1); |
| 77903 | 78082 | } |
| 78083 | + sqlite3DbFree(db, aChngAddr); |
| 77904 | 78084 | |
| 77905 | | - /* End of the analysis loop. */ |
| 78085 | + /* Always jump here after updating the iMem+1...iMem+1+nCol counters */ |
| 77906 | 78086 | sqlite3VdbeResolveLabel(v, endOfLoop); |
| 78087 | + |
| 77907 | 78088 | sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop); |
| 77908 | 78089 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 78090 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78091 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, |
| 78092 | + (char*)&stat3PushFuncdef, P4_FUNCDEF); |
| 78093 | + sqlite3VdbeChangeP5(v, 5); |
| 78094 | + sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop); |
| 78095 | + shortJump = |
| 78096 | + sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1); |
| 78097 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1, |
| 78098 | + (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 78099 | + sqlite3VdbeChangeP5(v, 2); |
| 78100 | + sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1); |
| 78101 | + sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1); |
| 78102 | + sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample); |
| 78103 | + sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample); |
| 78104 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq, |
| 78105 | + (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 78106 | + sqlite3VdbeChangeP5(v, 3); |
| 78107 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt, |
| 78108 | + (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 78109 | + sqlite3VdbeChangeP5(v, 4); |
| 78110 | + sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt, |
| 78111 | + (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 78112 | + sqlite3VdbeChangeP5(v, 5); |
| 78113 | + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0); |
| 78114 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid); |
| 78115 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid); |
| 78116 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump); |
| 78117 | + sqlite3VdbeJumpHere(v, shortJump+2); |
| 78118 | +#endif |
| 77909 | 78119 | |
| 77910 | 78120 | /* Store the results in sqlite_stat1. |
| 77911 | 78121 | ** |
| 77912 | 78122 | ** The result is a single row of the sqlite_stat1 table. The first |
| 77913 | 78123 | ** two columns are the names of the table and index. The third column |
| | @@ -77923,50 +78133,51 @@ |
| 77923 | 78133 | ** |
| 77924 | 78134 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 77925 | 78135 | ** If K>0 then it is always the case the D>0 so division by zero |
| 77926 | 78136 | ** is never possible. |
| 77927 | 78137 | */ |
| 77928 | | - sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); |
| 78138 | + sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1); |
| 77929 | 78139 | if( jZeroRows<0 ){ |
| 77930 | 78140 | jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); |
| 77931 | 78141 | } |
| 77932 | 78142 | for(i=0; i<nCol; i++){ |
| 77933 | 78143 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); |
| 77934 | | - sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 78144 | + sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); |
| 77935 | 78145 | sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); |
| 77936 | 78146 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 77937 | 78147 | sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp); |
| 77938 | 78148 | sqlite3VdbeAddOp1(v, OP_ToInt, regTemp); |
| 77939 | | - sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 78149 | + sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); |
| 77940 | 78150 | } |
| 77941 | 78151 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 77942 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 77943 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 78152 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 78153 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); |
| 77944 | 78154 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 77945 | 78155 | } |
| 77946 | 78156 | |
| 77947 | 78157 | /* If the table has no indices, create a single sqlite_stat1 entry |
| 77948 | 78158 | ** containing NULL as the index name and the row count as the content. |
| 77949 | 78159 | */ |
| 77950 | 78160 | if( pTab->pIndex==0 ){ |
| 77951 | 78161 | sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); |
| 77952 | 78162 | VdbeComment((v, "%s", pTab->zName)); |
| 77953 | | - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno); |
| 78163 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1); |
| 77954 | 78164 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 77955 | | - jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno); |
| 78165 | + jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); |
| 77956 | 78166 | }else{ |
| 77957 | 78167 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 77958 | 78168 | jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto); |
| 77959 | 78169 | } |
| 77960 | 78170 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); |
| 77961 | 78171 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 77962 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 77963 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 78172 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 78173 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); |
| 77964 | 78174 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 77965 | 78175 | if( pParse->nMem<regRec ) pParse->nMem = regRec; |
| 77966 | 78176 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 77967 | 78177 | } |
| 78178 | + |
| 77968 | 78179 | |
| 77969 | 78180 | /* |
| 77970 | 78181 | ** Generate code that will cause the most recent index analysis to |
| 77971 | 78182 | ** be loaded into internal hash tables where is can be used. |
| 77972 | 78183 | */ |
| | @@ -77987,11 +78198,11 @@ |
| 77987 | 78198 | int iStatCur; |
| 77988 | 78199 | int iMem; |
| 77989 | 78200 | |
| 77990 | 78201 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 77991 | 78202 | iStatCur = pParse->nTab; |
| 77992 | | - pParse->nTab += 2; |
| 78203 | + pParse->nTab += 3; |
| 77993 | 78204 | openStatTable(pParse, iDb, iStatCur, 0, 0); |
| 77994 | 78205 | iMem = pParse->nMem+1; |
| 77995 | 78206 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 77996 | 78207 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 77997 | 78208 | Table *pTab = (Table*)sqliteHashData(k); |
| | @@ -78012,11 +78223,11 @@ |
| 78012 | 78223 | assert( pTab!=0 ); |
| 78013 | 78224 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
| 78014 | 78225 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 78015 | 78226 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 78016 | 78227 | iStatCur = pParse->nTab; |
| 78017 | | - pParse->nTab += 2; |
| 78228 | + pParse->nTab += 3; |
| 78018 | 78229 | if( pOnlyIdx ){ |
| 78019 | 78230 | openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); |
| 78020 | 78231 | }else{ |
| 78021 | 78232 | openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); |
| 78022 | 78233 | } |
| | @@ -78117,11 +78328,11 @@ |
| 78117 | 78328 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
| 78118 | 78329 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 78119 | 78330 | Index *pIndex; |
| 78120 | 78331 | Table *pTable; |
| 78121 | 78332 | int i, c, n; |
| 78122 | | - unsigned int v; |
| 78333 | + tRowcnt v; |
| 78123 | 78334 | const char *z; |
| 78124 | 78335 | |
| 78125 | 78336 | assert( argc==3 ); |
| 78126 | 78337 | UNUSED_PARAMETER2(NotUsed, argc); |
| 78127 | 78338 | |
| | @@ -78160,40 +78371,172 @@ |
| 78160 | 78371 | /* |
| 78161 | 78372 | ** If the Index.aSample variable is not NULL, delete the aSample[] array |
| 78162 | 78373 | ** and its contents. |
| 78163 | 78374 | */ |
| 78164 | 78375 | SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ |
| 78165 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 78376 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78166 | 78377 | if( pIdx->aSample ){ |
| 78167 | 78378 | int j; |
| 78168 | | - for(j=0; j<SQLITE_INDEX_SAMPLES; j++){ |
| 78379 | + for(j=0; j<pIdx->nSample; j++){ |
| 78169 | 78380 | IndexSample *p = &pIdx->aSample[j]; |
| 78170 | 78381 | if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ |
| 78171 | 78382 | sqlite3DbFree(db, p->u.z); |
| 78172 | 78383 | } |
| 78173 | 78384 | } |
| 78174 | 78385 | sqlite3DbFree(db, pIdx->aSample); |
| 78386 | + } |
| 78387 | + if( db && db->pnBytesFreed==0 ){ |
| 78388 | + pIdx->nSample = 0; |
| 78389 | + pIdx->aSample = 0; |
| 78175 | 78390 | } |
| 78176 | 78391 | #else |
| 78177 | 78392 | UNUSED_PARAMETER(db); |
| 78178 | 78393 | UNUSED_PARAMETER(pIdx); |
| 78179 | 78394 | #endif |
| 78180 | 78395 | } |
| 78181 | 78396 | |
| 78397 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78182 | 78398 | /* |
| 78183 | | -** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The |
| 78399 | +** Load content from the sqlite_stat3 table into the Index.aSample[] |
| 78400 | +** arrays of all indices. |
| 78401 | +*/ |
| 78402 | +static int loadStat3(sqlite3 *db, const char *zDb){ |
| 78403 | + int rc; /* Result codes from subroutines */ |
| 78404 | + sqlite3_stmt *pStmt = 0; /* An SQL statement being run */ |
| 78405 | + char *zSql; /* Text of the SQL statement */ |
| 78406 | + Index *pPrevIdx = 0; /* Previous index in the loop */ |
| 78407 | + int idx = 0; /* slot in pIdx->aSample[] for next sample */ |
| 78408 | + int eType; /* Datatype of a sample */ |
| 78409 | + IndexSample *pSample; /* A slot in pIdx->aSample[] */ |
| 78410 | + |
| 78411 | + if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){ |
| 78412 | + return SQLITE_OK; |
| 78413 | + } |
| 78414 | + |
| 78415 | + zSql = sqlite3MPrintf(db, |
| 78416 | + "SELECT idx,count(*) FROM %Q.sqlite_stat3" |
| 78417 | + " GROUP BY idx", zDb); |
| 78418 | + if( !zSql ){ |
| 78419 | + return SQLITE_NOMEM; |
| 78420 | + } |
| 78421 | + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 78422 | + sqlite3DbFree(db, zSql); |
| 78423 | + if( rc ) return rc; |
| 78424 | + |
| 78425 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 78426 | + char *zIndex; /* Index name */ |
| 78427 | + Index *pIdx; /* Pointer to the index object */ |
| 78428 | + int nSample; /* Number of samples */ |
| 78429 | + |
| 78430 | + zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 78431 | + if( zIndex==0 ) continue; |
| 78432 | + nSample = sqlite3_column_int(pStmt, 1); |
| 78433 | + pIdx = sqlite3FindIndex(db, zIndex, zDb); |
| 78434 | + if( pIdx==0 ) continue; |
| 78435 | + assert( pIdx->nSample==0 ); |
| 78436 | + pIdx->nSample = nSample; |
| 78437 | + pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) ); |
| 78438 | + pIdx->avgEq = pIdx->aiRowEst[1]; |
| 78439 | + if( pIdx->aSample==0 ){ |
| 78440 | + db->mallocFailed = 1; |
| 78441 | + sqlite3_finalize(pStmt); |
| 78442 | + return SQLITE_NOMEM; |
| 78443 | + } |
| 78444 | + } |
| 78445 | + rc = sqlite3_finalize(pStmt); |
| 78446 | + if( rc ) return rc; |
| 78447 | + |
| 78448 | + zSql = sqlite3MPrintf(db, |
| 78449 | + "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb); |
| 78450 | + if( !zSql ){ |
| 78451 | + return SQLITE_NOMEM; |
| 78452 | + } |
| 78453 | + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 78454 | + sqlite3DbFree(db, zSql); |
| 78455 | + if( rc ) return rc; |
| 78456 | + |
| 78457 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 78458 | + char *zIndex; /* Index name */ |
| 78459 | + Index *pIdx; /* Pointer to the index object */ |
| 78460 | + int i; /* Loop counter */ |
| 78461 | + tRowcnt sumEq; /* Sum of the nEq values */ |
| 78462 | + |
| 78463 | + zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 78464 | + if( zIndex==0 ) continue; |
| 78465 | + pIdx = sqlite3FindIndex(db, zIndex, zDb); |
| 78466 | + if( pIdx==0 ) continue; |
| 78467 | + if( pIdx==pPrevIdx ){ |
| 78468 | + idx++; |
| 78469 | + }else{ |
| 78470 | + pPrevIdx = pIdx; |
| 78471 | + idx = 0; |
| 78472 | + } |
| 78473 | + assert( idx<pIdx->nSample ); |
| 78474 | + pSample = &pIdx->aSample[idx]; |
| 78475 | + pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1); |
| 78476 | + pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2); |
| 78477 | + pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3); |
| 78478 | + if( idx==pIdx->nSample-1 ){ |
| 78479 | + if( pSample->nDLt>0 ){ |
| 78480 | + for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq; |
| 78481 | + pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt; |
| 78482 | + } |
| 78483 | + if( pIdx->avgEq<=0 ) pIdx->avgEq = 1; |
| 78484 | + } |
| 78485 | + eType = sqlite3_column_type(pStmt, 4); |
| 78486 | + pSample->eType = (u8)eType; |
| 78487 | + switch( eType ){ |
| 78488 | + case SQLITE_INTEGER: { |
| 78489 | + pSample->u.i = sqlite3_column_int64(pStmt, 4); |
| 78490 | + break; |
| 78491 | + } |
| 78492 | + case SQLITE_FLOAT: { |
| 78493 | + pSample->u.r = sqlite3_column_double(pStmt, 4); |
| 78494 | + break; |
| 78495 | + } |
| 78496 | + case SQLITE_NULL: { |
| 78497 | + break; |
| 78498 | + } |
| 78499 | + default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); { |
| 78500 | + const char *z = (const char *)( |
| 78501 | + (eType==SQLITE_BLOB) ? |
| 78502 | + sqlite3_column_blob(pStmt, 4): |
| 78503 | + sqlite3_column_text(pStmt, 4) |
| 78504 | + ); |
| 78505 | + int n = z ? sqlite3_column_bytes(pStmt, 4) : 0; |
| 78506 | + pSample->nByte = n; |
| 78507 | + if( n < 1){ |
| 78508 | + pSample->u.z = 0; |
| 78509 | + }else{ |
| 78510 | + pSample->u.z = sqlite3Malloc(n); |
| 78511 | + if( pSample->u.z==0 ){ |
| 78512 | + db->mallocFailed = 1; |
| 78513 | + sqlite3_finalize(pStmt); |
| 78514 | + return SQLITE_NOMEM; |
| 78515 | + } |
| 78516 | + memcpy(pSample->u.z, z, n); |
| 78517 | + } |
| 78518 | + } |
| 78519 | + } |
| 78520 | + } |
| 78521 | + return sqlite3_finalize(pStmt); |
| 78522 | +} |
| 78523 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 78524 | + |
| 78525 | +/* |
| 78526 | +** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The |
| 78184 | 78527 | ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] |
| 78185 | | -** arrays. The contents of sqlite_stat2 are used to populate the |
| 78528 | +** arrays. The contents of sqlite_stat3 are used to populate the |
| 78186 | 78529 | ** Index.aSample[] arrays. |
| 78187 | 78530 | ** |
| 78188 | 78531 | ** 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 |
| 78532 | +** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined |
| 78533 | +** during compilation and the sqlite_stat3 table is present, no data is |
| 78191 | 78534 | ** read from it. |
| 78192 | 78535 | ** |
| 78193 | | -** If SQLITE_ENABLE_STAT2 was defined during compilation and the |
| 78194 | | -** sqlite_stat2 table is not present in the database, SQLITE_ERROR is |
| 78536 | +** If SQLITE_ENABLE_STAT3 was defined during compilation and the |
| 78537 | +** sqlite_stat3 table is not present in the database, SQLITE_ERROR is |
| 78195 | 78538 | ** returned. However, in this case, data is read from the sqlite_stat1 |
| 78196 | 78539 | ** table (if it is present) before returning. |
| 78197 | 78540 | ** |
| 78198 | 78541 | ** If an OOM error occurs, this function always sets db->mallocFailed. |
| 78199 | 78542 | ** This means if the caller does not care about other errors, the return |
| | @@ -78211,12 +78554,14 @@ |
| 78211 | 78554 | /* Clear any prior statistics */ |
| 78212 | 78555 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 78213 | 78556 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
| 78214 | 78557 | Index *pIdx = sqliteHashData(i); |
| 78215 | 78558 | sqlite3DefaultRowEst(pIdx); |
| 78559 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78216 | 78560 | sqlite3DeleteIndexSamples(db, pIdx); |
| 78217 | 78561 | pIdx->aSample = 0; |
| 78562 | +#endif |
| 78218 | 78563 | } |
| 78219 | 78564 | |
| 78220 | 78565 | /* Check to make sure the sqlite_stat1 table exists */ |
| 78221 | 78566 | sInfo.db = db; |
| 78222 | 78567 | sInfo.zDatabase = db->aDb[iDb].zName; |
| | @@ -78224,91 +78569,23 @@ |
| 78224 | 78569 | return SQLITE_ERROR; |
| 78225 | 78570 | } |
| 78226 | 78571 | |
| 78227 | 78572 | /* Load new statistics out of the sqlite_stat1 table */ |
| 78228 | 78573 | zSql = sqlite3MPrintf(db, |
| 78229 | | - "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 78574 | + "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 78230 | 78575 | if( zSql==0 ){ |
| 78231 | 78576 | rc = SQLITE_NOMEM; |
| 78232 | 78577 | }else{ |
| 78233 | 78578 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
| 78234 | 78579 | sqlite3DbFree(db, zSql); |
| 78235 | 78580 | } |
| 78236 | 78581 | |
| 78237 | 78582 | |
| 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 | | - } |
| 78583 | + /* Load the statistics from the sqlite_stat3 table. */ |
| 78584 | +#ifdef SQLITE_ENABLE_STAT3 |
| 78585 | + if( rc==SQLITE_OK ){ |
| 78586 | + rc = loadStat3(db, sInfo.zDatabase); |
| 78310 | 78587 | } |
| 78311 | 78588 | #endif |
| 78312 | 78589 | |
| 78313 | 78590 | if( rc==SQLITE_NOMEM ){ |
| 78314 | 78591 | db->mallocFailed = 1; |
| | @@ -81120,11 +81397,15 @@ |
| 81120 | 81397 | Parse *pParse, /* The parsing context */ |
| 81121 | 81398 | int iDb, /* The database number */ |
| 81122 | 81399 | const char *zType, /* "idx" or "tbl" */ |
| 81123 | 81400 | const char *zName /* Name of index or table */ |
| 81124 | 81401 | ){ |
| 81125 | | - static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" }; |
| 81402 | + static const char *azStatTab[] = { |
| 81403 | + "sqlite_stat1", |
| 81404 | + "sqlite_stat2", |
| 81405 | + "sqlite_stat3", |
| 81406 | + }; |
| 81126 | 81407 | int i; |
| 81127 | 81408 | const char *zDbName = pParse->db->aDb[iDb].zName; |
| 81128 | 81409 | for(i=0; i<ArraySize(azStatTab); i++){ |
| 81129 | 81410 | if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){ |
| 81130 | 81411 | sqlite3NestedParse(pParse, |
| | @@ -81132,10 +81413,80 @@ |
| 81132 | 81413 | zDbName, azStatTab[i], zType, zName |
| 81133 | 81414 | ); |
| 81134 | 81415 | } |
| 81135 | 81416 | } |
| 81136 | 81417 | } |
| 81418 | + |
| 81419 | +/* |
| 81420 | +** Generate code to drop a table. |
| 81421 | +*/ |
| 81422 | +SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ |
| 81423 | + Vdbe *v; |
| 81424 | + sqlite3 *db = pParse->db; |
| 81425 | + Trigger *pTrigger; |
| 81426 | + Db *pDb = &db->aDb[iDb]; |
| 81427 | + |
| 81428 | + v = sqlite3GetVdbe(pParse); |
| 81429 | + assert( v!=0 ); |
| 81430 | + sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 81431 | + |
| 81432 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 81433 | + if( IsVirtual(pTab) ){ |
| 81434 | + sqlite3VdbeAddOp0(v, OP_VBegin); |
| 81435 | + } |
| 81436 | +#endif |
| 81437 | + |
| 81438 | + /* Drop all triggers associated with the table being dropped. Code |
| 81439 | + ** is generated to remove entries from sqlite_master and/or |
| 81440 | + ** sqlite_temp_master if required. |
| 81441 | + */ |
| 81442 | + pTrigger = sqlite3TriggerList(pParse, pTab); |
| 81443 | + while( pTrigger ){ |
| 81444 | + assert( pTrigger->pSchema==pTab->pSchema || |
| 81445 | + pTrigger->pSchema==db->aDb[1].pSchema ); |
| 81446 | + sqlite3DropTriggerPtr(pParse, pTrigger); |
| 81447 | + pTrigger = pTrigger->pNext; |
| 81448 | + } |
| 81449 | + |
| 81450 | +#ifndef SQLITE_OMIT_AUTOINCREMENT |
| 81451 | + /* Remove any entries of the sqlite_sequence table associated with |
| 81452 | + ** the table being dropped. This is done before the table is dropped |
| 81453 | + ** at the btree level, in case the sqlite_sequence table needs to |
| 81454 | + ** move as a result of the drop (can happen in auto-vacuum mode). |
| 81455 | + */ |
| 81456 | + if( pTab->tabFlags & TF_Autoincrement ){ |
| 81457 | + sqlite3NestedParse(pParse, |
| 81458 | + "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
| 81459 | + pDb->zName, pTab->zName |
| 81460 | + ); |
| 81461 | + } |
| 81462 | +#endif |
| 81463 | + |
| 81464 | + /* Drop all SQLITE_MASTER table and index entries that refer to the |
| 81465 | + ** table. The program name loops through the master table and deletes |
| 81466 | + ** every row that refers to a table of the same name as the one being |
| 81467 | + ** dropped. Triggers are handled seperately because a trigger can be |
| 81468 | + ** created in the temp database that refers to a table in another |
| 81469 | + ** database. |
| 81470 | + */ |
| 81471 | + sqlite3NestedParse(pParse, |
| 81472 | + "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
| 81473 | + pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); |
| 81474 | + if( !isView && !IsVirtual(pTab) ){ |
| 81475 | + destroyTable(pParse, pTab); |
| 81476 | + } |
| 81477 | + |
| 81478 | + /* Remove the table entry from SQLite's internal schema and modify |
| 81479 | + ** the schema cookie. |
| 81480 | + */ |
| 81481 | + if( IsVirtual(pTab) ){ |
| 81482 | + sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
| 81483 | + } |
| 81484 | + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 81485 | + sqlite3ChangeCookie(pParse, iDb); |
| 81486 | + sqliteViewResetAll(db, iDb); |
| 81487 | +} |
| 81137 | 81488 | |
| 81138 | 81489 | /* |
| 81139 | 81490 | ** This routine is called to do the work of a DROP TABLE statement. |
| 81140 | 81491 | ** pName is the name of the table to be dropped. |
| 81141 | 81492 | */ |
| | @@ -81201,11 +81552,12 @@ |
| 81201 | 81552 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 81202 | 81553 | goto exit_drop_table; |
| 81203 | 81554 | } |
| 81204 | 81555 | } |
| 81205 | 81556 | #endif |
| 81206 | | - if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 81557 | + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
| 81558 | + && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){ |
| 81207 | 81559 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
| 81208 | 81560 | goto exit_drop_table; |
| 81209 | 81561 | } |
| 81210 | 81562 | |
| 81211 | 81563 | #ifndef SQLITE_OMIT_VIEW |
| | @@ -81225,72 +81577,15 @@ |
| 81225 | 81577 | /* Generate code to remove the table from the master table |
| 81226 | 81578 | ** on disk. |
| 81227 | 81579 | */ |
| 81228 | 81580 | v = sqlite3GetVdbe(pParse); |
| 81229 | 81581 | if( v ){ |
| 81230 | | - Trigger *pTrigger; |
| 81231 | | - Db *pDb = &db->aDb[iDb]; |
| 81232 | 81582 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 81233 | | - |
| 81234 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 81235 | | - if( IsVirtual(pTab) ){ |
| 81236 | | - sqlite3VdbeAddOp0(v, OP_VBegin); |
| 81237 | | - } |
| 81238 | | -#endif |
| 81583 | + sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 81239 | 81584 | 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); |
| 81585 | + sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
| 81586 | + } |
| 81292 | 81587 | |
| 81293 | 81588 | exit_drop_table: |
| 81294 | 81589 | sqlite3SrcListDelete(db, pName); |
| 81295 | 81590 | } |
| 81296 | 81591 | |
| | @@ -81454,17 +81749,19 @@ |
| 81454 | 81749 | */ |
| 81455 | 81750 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 81456 | 81751 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
| 81457 | 81752 | int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
| 81458 | 81753 | int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
| 81459 | | - int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */ |
| 81754 | + int iSorter; /* Cursor opened by OpenSorter (if in use) */ |
| 81460 | 81755 | int addr1; /* Address of top of loop */ |
| 81461 | 81756 | int addr2; /* Address to jump to for next iteration */ |
| 81462 | 81757 | int tnum; /* Root page of index */ |
| 81463 | 81758 | Vdbe *v; /* Generate code into this virtual machine */ |
| 81464 | 81759 | KeyInfo *pKey; /* KeyInfo for index */ |
| 81760 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 81465 | 81761 | int regIdxKey; /* Registers containing the index key */ |
| 81762 | +#endif |
| 81466 | 81763 | int regRecord; /* Register holding assemblied index record */ |
| 81467 | 81764 | sqlite3 *db = pParse->db; /* The database connection */ |
| 81468 | 81765 | int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
| 81469 | 81766 | |
| 81470 | 81767 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| | @@ -81494,21 +81791,22 @@ |
| 81494 | 81791 | |
| 81495 | 81792 | #ifndef SQLITE_OMIT_MERGE_SORT |
| 81496 | 81793 | /* Open the sorter cursor if we are to use one. */ |
| 81497 | 81794 | iSorter = pParse->nTab++; |
| 81498 | 81795 | sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO); |
| 81796 | +#else |
| 81797 | + iSorter = iTab; |
| 81499 | 81798 | #endif |
| 81500 | 81799 | |
| 81501 | 81800 | /* Open the table. Loop through all rows of the table, inserting index |
| 81502 | 81801 | ** records into the sorter. */ |
| 81503 | 81802 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 81504 | 81803 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); |
| 81505 | | - addr2 = addr1 + 1; |
| 81506 | 81804 | regRecord = sqlite3GetTempReg(pParse); |
| 81507 | | - regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); |
| 81508 | 81805 | |
| 81509 | 81806 | #ifndef SQLITE_OMIT_MERGE_SORT |
| 81807 | + sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); |
| 81510 | 81808 | sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
| 81511 | 81809 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); |
| 81512 | 81810 | sqlite3VdbeJumpHere(v, addr1); |
| 81513 | 81811 | addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); |
| 81514 | 81812 | if( pIndex->onError!=OE_None ){ |
| | @@ -81524,10 +81822,12 @@ |
| 81524 | 81822 | } |
| 81525 | 81823 | sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord); |
| 81526 | 81824 | sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1); |
| 81527 | 81825 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 81528 | 81826 | #else |
| 81827 | + regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); |
| 81828 | + addr2 = addr1 + 1; |
| 81529 | 81829 | if( pIndex->onError!=OE_None ){ |
| 81530 | 81830 | const int regRowid = regIdxKey + pIndex->nColumn; |
| 81531 | 81831 | const int j2 = sqlite3VdbeCurrentAddr(v) + 2; |
| 81532 | 81832 | void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey); |
| 81533 | 81833 | |
| | @@ -81621,10 +81921,11 @@ |
| 81621 | 81921 | ** before looking up the table. |
| 81622 | 81922 | */ |
| 81623 | 81923 | assert( pName1 && pName2 ); |
| 81624 | 81924 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 81625 | 81925 | if( iDb<0 ) goto exit_create_index; |
| 81926 | + assert( pName && pName->z ); |
| 81626 | 81927 | |
| 81627 | 81928 | #ifndef SQLITE_OMIT_TEMPDB |
| 81628 | 81929 | /* If the index name was unqualified, check if the the table |
| 81629 | 81930 | ** is a temp table. If so, set the database to 1. Do not do this |
| 81630 | 81931 | ** if initialising a database schema. |
| | @@ -81648,10 +81949,11 @@ |
| 81648 | 81949 | pTblName->a[0].zDatabase); |
| 81649 | 81950 | if( !pTab || db->mallocFailed ) goto exit_create_index; |
| 81650 | 81951 | assert( db->aDb[iDb].pSchema==pTab->pSchema ); |
| 81651 | 81952 | }else{ |
| 81652 | 81953 | assert( pName==0 ); |
| 81954 | + assert( pStart==0 ); |
| 81653 | 81955 | pTab = pParse->pNewTable; |
| 81654 | 81956 | if( !pTab ) goto exit_create_index; |
| 81655 | 81957 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 81656 | 81958 | } |
| 81657 | 81959 | pDb = &db->aDb[iDb]; |
| | @@ -81690,10 +81992,11 @@ |
| 81690 | 81992 | ** own name. |
| 81691 | 81993 | */ |
| 81692 | 81994 | if( pName ){ |
| 81693 | 81995 | zName = sqlite3NameFromToken(db, pName); |
| 81694 | 81996 | if( zName==0 ) goto exit_create_index; |
| 81997 | + assert( pName->z!=0 ); |
| 81695 | 81998 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
| 81696 | 81999 | goto exit_create_index; |
| 81697 | 82000 | } |
| 81698 | 82001 | if( !db->init.busy ){ |
| 81699 | 82002 | if( sqlite3FindTable(db, zName, 0)!=0 ){ |
| | @@ -81769,24 +82072,24 @@ |
| 81769 | 82072 | */ |
| 81770 | 82073 | nName = sqlite3Strlen30(zName); |
| 81771 | 82074 | nCol = pList->nExpr; |
| 81772 | 82075 | pIndex = sqlite3DbMallocZero(db, |
| 81773 | 82076 | sizeof(Index) + /* Index structure */ |
| 82077 | + sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */ |
| 81774 | 82078 | sizeof(int)*nCol + /* Index.aiColumn */ |
| 81775 | | - sizeof(int)*(nCol+1) + /* Index.aiRowEst */ |
| 81776 | 82079 | sizeof(char *)*nCol + /* Index.azColl */ |
| 81777 | 82080 | sizeof(u8)*nCol + /* Index.aSortOrder */ |
| 81778 | 82081 | nName + 1 + /* Index.zName */ |
| 81779 | 82082 | nExtra /* Collation sequence names */ |
| 81780 | 82083 | ); |
| 81781 | 82084 | if( db->mallocFailed ){ |
| 81782 | 82085 | goto exit_create_index; |
| 81783 | 82086 | } |
| 81784 | | - pIndex->azColl = (char**)(&pIndex[1]); |
| 82087 | + pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]); |
| 82088 | + pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]); |
| 81785 | 82089 | pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]); |
| 81786 | | - pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]); |
| 81787 | | - pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]); |
| 82090 | + pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]); |
| 81788 | 82091 | pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]); |
| 81789 | 82092 | zExtra = (char *)(&pIndex->zName[nName+1]); |
| 81790 | 82093 | memcpy(pIndex->zName, zName, nName+1); |
| 81791 | 82094 | pIndex->pTable = pTab; |
| 81792 | 82095 | pIndex->nColumn = pList->nExpr; |
| | @@ -82059,13 +82362,13 @@ |
| 82059 | 82362 | ** Apart from that, we have little to go on besides intuition as to |
| 82060 | 82363 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 82061 | 82364 | ** are based on typical values found in actual indices. |
| 82062 | 82365 | */ |
| 82063 | 82366 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ |
| 82064 | | - unsigned *a = pIdx->aiRowEst; |
| 82367 | + tRowcnt *a = pIdx->aiRowEst; |
| 82065 | 82368 | int i; |
| 82066 | | - unsigned n; |
| 82369 | + tRowcnt n; |
| 82067 | 82370 | assert( a!=0 ); |
| 82068 | 82371 | a[0] = pIdx->pTable->nRowEst; |
| 82069 | 82372 | if( a[0]<10 ) a[0] = 10; |
| 82070 | 82373 | n = 10; |
| 82071 | 82374 | for(i=1; i<=pIdx->nColumn; i++){ |
| | @@ -82545,17 +82848,14 @@ |
| 82545 | 82848 | |
| 82546 | 82849 | /* |
| 82547 | 82850 | ** Commit a transaction |
| 82548 | 82851 | */ |
| 82549 | 82852 | SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){ |
| 82550 | | - sqlite3 *db; |
| 82551 | 82853 | Vdbe *v; |
| 82552 | 82854 | |
| 82553 | 82855 | assert( pParse!=0 ); |
| 82554 | | - db = pParse->db; |
| 82555 | | - assert( db!=0 ); |
| 82556 | | -/* if( db->aDb[0].pBt==0 ) return; */ |
| 82856 | + assert( pParse->db!=0 ); |
| 82557 | 82857 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){ |
| 82558 | 82858 | return; |
| 82559 | 82859 | } |
| 82560 | 82860 | v = sqlite3GetVdbe(pParse); |
| 82561 | 82861 | if( v ){ |
| | @@ -82565,17 +82865,14 @@ |
| 82565 | 82865 | |
| 82566 | 82866 | /* |
| 82567 | 82867 | ** Rollback a transaction |
| 82568 | 82868 | */ |
| 82569 | 82869 | SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){ |
| 82570 | | - sqlite3 *db; |
| 82571 | 82870 | Vdbe *v; |
| 82572 | 82871 | |
| 82573 | 82872 | assert( pParse!=0 ); |
| 82574 | | - db = pParse->db; |
| 82575 | | - assert( db!=0 ); |
| 82576 | | -/* if( db->aDb[0].pBt==0 ) return; */ |
| 82873 | + assert( pParse->db!=0 ); |
| 82577 | 82874 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){ |
| 82578 | 82875 | return; |
| 82579 | 82876 | } |
| 82580 | 82877 | v = sqlite3GetVdbe(pParse); |
| 82581 | 82878 | if( v ){ |
| | @@ -84377,20 +84674,19 @@ |
| 84377 | 84674 | /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 84378 | 84675 | assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
| 84379 | 84676 | if( z2 ){ |
| 84380 | 84677 | z1 = contextMalloc(context, ((i64)n)+1); |
| 84381 | 84678 | if( z1 ){ |
| 84382 | | - memcpy(z1, z2, n+1); |
| 84383 | | - for(i=0; z1[i]; i++){ |
| 84384 | | - z1[i] = (char)sqlite3Toupper(z1[i]); |
| 84679 | + for(i=0; i<n; i++){ |
| 84680 | + z1[i] = (char)sqlite3Toupper(z2[i]); |
| 84385 | 84681 | } |
| 84386 | | - sqlite3_result_text(context, z1, -1, sqlite3_free); |
| 84682 | + sqlite3_result_text(context, z1, n, sqlite3_free); |
| 84387 | 84683 | } |
| 84388 | 84684 | } |
| 84389 | 84685 | } |
| 84390 | 84686 | static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 84391 | | - u8 *z1; |
| 84687 | + char *z1; |
| 84392 | 84688 | const char *z2; |
| 84393 | 84689 | int i, n; |
| 84394 | 84690 | UNUSED_PARAMETER(argc); |
| 84395 | 84691 | z2 = (char*)sqlite3_value_text(argv[0]); |
| 84396 | 84692 | n = sqlite3_value_bytes(argv[0]); |
| | @@ -84397,15 +84693,14 @@ |
| 84397 | 84693 | /* Verify that the call to _bytes() does not invalidate the _text() pointer */ |
| 84398 | 84694 | assert( z2==(char*)sqlite3_value_text(argv[0]) ); |
| 84399 | 84695 | if( z2 ){ |
| 84400 | 84696 | z1 = contextMalloc(context, ((i64)n)+1); |
| 84401 | 84697 | if( z1 ){ |
| 84402 | | - memcpy(z1, z2, n+1); |
| 84403 | | - for(i=0; z1[i]; i++){ |
| 84404 | | - z1[i] = sqlite3Tolower(z1[i]); |
| 84698 | + for(i=0; i<n; i++){ |
| 84699 | + z1[i] = sqlite3Tolower(z2[i]); |
| 84405 | 84700 | } |
| 84406 | | - sqlite3_result_text(context, (char *)z1, -1, sqlite3_free); |
| 84701 | + sqlite3_result_text(context, z1, n, sqlite3_free); |
| 84407 | 84702 | } |
| 84408 | 84703 | } |
| 84409 | 84704 | } |
| 84410 | 84705 | |
| 84411 | 84706 | |
| | @@ -86778,10 +87073,11 @@ |
| 86778 | 87073 | sqlite3SelectDelete(db, pSelect); |
| 86779 | 87074 | if( db->mallocFailed==1 ){ |
| 86780 | 87075 | fkTriggerDelete(db, pTrigger); |
| 86781 | 87076 | return 0; |
| 86782 | 87077 | } |
| 87078 | + assert( pStep!=0 ); |
| 86783 | 87079 | |
| 86784 | 87080 | switch( action ){ |
| 86785 | 87081 | case OE_Restrict: |
| 86786 | 87082 | pStep->op = TK_SELECT; |
| 86787 | 87083 | break; |
| | @@ -88621,10 +88917,13 @@ |
| 88621 | 88917 | */ |
| 88622 | 88918 | if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ |
| 88623 | 88919 | return 0; |
| 88624 | 88920 | } |
| 88625 | 88921 | #endif |
| 88922 | + if( (pParse->db->flags & SQLITE_CountRows)!=0 ){ |
| 88923 | + return 0; |
| 88924 | + } |
| 88626 | 88925 | |
| 88627 | 88926 | /* If we get this far, it means either: |
| 88628 | 88927 | ** |
| 88629 | 88928 | ** * We can always do the transfer if the table contains an |
| 88630 | 88929 | ** an integer primary key |
| | @@ -89698,11 +89997,11 @@ |
| 89698 | 89997 | sqlite3_vfs *pVfs = db->pVfs; |
| 89699 | 89998 | void *handle; |
| 89700 | 89999 | int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); |
| 89701 | 90000 | char *zErrmsg = 0; |
| 89702 | 90001 | void **aHandle; |
| 89703 | | - const int nMsg = 300; |
| 90002 | + int nMsg = 300 + sqlite3Strlen30(zFile); |
| 89704 | 90003 | |
| 89705 | 90004 | if( pzErrMsg ) *pzErrMsg = 0; |
| 89706 | 90005 | |
| 89707 | 90006 | /* Ticket #1863. To avoid a creating security problems for older |
| 89708 | 90007 | ** applications that relink against newer versions of SQLite, the |
| | @@ -89735,10 +90034,11 @@ |
| 89735 | 90034 | } |
| 89736 | 90035 | xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) |
| 89737 | 90036 | sqlite3OsDlSym(pVfs, handle, zProc); |
| 89738 | 90037 | if( xInit==0 ){ |
| 89739 | 90038 | if( pzErrMsg ){ |
| 90039 | + nMsg += sqlite3Strlen30(zProc); |
| 89740 | 90040 | *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); |
| 89741 | 90041 | if( zErrmsg ){ |
| 89742 | 90042 | sqlite3_snprintf(nMsg, zErrmsg, |
| 89743 | 90043 | "no entry point [%s] in shared library [%s]", zProc,zFile); |
| 89744 | 90044 | sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| | @@ -90420,11 +90720,11 @@ |
| 90420 | 90720 | ){ |
| 90421 | 90721 | int iReg; |
| 90422 | 90722 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 90423 | 90723 | sqlite3CodeVerifySchema(pParse, iDb); |
| 90424 | 90724 | iReg = ++pParse->nMem; |
| 90425 | | - if( zLeft[0]=='p' ){ |
| 90725 | + if( sqlite3Tolower(zLeft[0])=='p' ){ |
| 90426 | 90726 | sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); |
| 90427 | 90727 | }else{ |
| 90428 | 90728 | sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight)); |
| 90429 | 90729 | } |
| 90430 | 90730 | sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); |
| | @@ -90486,12 +90786,14 @@ |
| 90486 | 90786 | */ |
| 90487 | 90787 | if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){ |
| 90488 | 90788 | int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ |
| 90489 | 90789 | int ii; /* Loop counter */ |
| 90490 | 90790 | |
| 90491 | | - /* Force the schema to be loaded on all databases. This cases all |
| 90492 | | - ** database files to be opened and the journal_modes set. */ |
| 90791 | + /* Force the schema to be loaded on all databases. This causes all |
| 90792 | + ** database files to be opened and the journal_modes set. This is |
| 90793 | + ** necessary because subsequent processing must know if the databases |
| 90794 | + ** are in WAL mode. */ |
| 90493 | 90795 | if( sqlite3ReadSchema(pParse) ){ |
| 90494 | 90796 | goto pragma_out; |
| 90495 | 90797 | } |
| 90496 | 90798 | |
| 90497 | 90799 | sqlite3VdbeSetNumCols(v, 1); |
| | @@ -91031,11 +91333,11 @@ |
| 91031 | 91333 | { OP_IfNeg, 1, 0, 0}, /* 1 */ |
| 91032 | 91334 | { OP_String8, 0, 3, 0}, /* 2 */ |
| 91033 | 91335 | { OP_ResultRow, 3, 1, 0}, |
| 91034 | 91336 | }; |
| 91035 | 91337 | |
| 91036 | | - int isQuick = (zLeft[0]=='q'); |
| 91338 | + int isQuick = (sqlite3Tolower(zLeft[0])=='q'); |
| 91037 | 91339 | |
| 91038 | 91340 | /* Initialize the VDBE program */ |
| 91039 | 91341 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 91040 | 91342 | pParse->nMem = 6; |
| 91041 | 91343 | sqlite3VdbeSetNumCols(v, 1); |
| | @@ -92406,10 +92708,11 @@ |
| 92406 | 92708 | Select standin; |
| 92407 | 92709 | sqlite3 *db = pParse->db; |
| 92408 | 92710 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); |
| 92409 | 92711 | assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */ |
| 92410 | 92712 | if( pNew==0 ){ |
| 92713 | + assert( db->mallocFailed ); |
| 92411 | 92714 | pNew = &standin; |
| 92412 | 92715 | memset(pNew, 0, sizeof(*pNew)); |
| 92413 | 92716 | } |
| 92414 | 92717 | if( pEList==0 ){ |
| 92415 | 92718 | pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0)); |
| | @@ -92433,10 +92736,11 @@ |
| 92433 | 92736 | if( pNew!=&standin ) sqlite3DbFree(db, pNew); |
| 92434 | 92737 | pNew = 0; |
| 92435 | 92738 | }else{ |
| 92436 | 92739 | assert( pNew->pSrc!=0 || pParse->nErr>0 ); |
| 92437 | 92740 | } |
| 92741 | + assert( pNew!=&standin ); |
| 92438 | 92742 | return pNew; |
| 92439 | 92743 | } |
| 92440 | 92744 | |
| 92441 | 92745 | /* |
| 92442 | 92746 | ** Delete the given Select structure and all of its substructures. |
| | @@ -93611,11 +93915,14 @@ |
| 93611 | 93915 | /* If the column contains an "AS <name>" phrase, use <name> as the name */ |
| 93612 | 93916 | zName = sqlite3DbStrDup(db, zName); |
| 93613 | 93917 | }else{ |
| 93614 | 93918 | Expr *pColExpr = p; /* The expression that is the result column name */ |
| 93615 | 93919 | Table *pTab; /* Table associated with this expression */ |
| 93616 | | - while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight; |
| 93920 | + while( pColExpr->op==TK_DOT ){ |
| 93921 | + pColExpr = pColExpr->pRight; |
| 93922 | + assert( pColExpr!=0 ); |
| 93923 | + } |
| 93617 | 93924 | if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){ |
| 93618 | 93925 | /* For columns use the column name name */ |
| 93619 | 93926 | int iCol = pColExpr->iColumn; |
| 93620 | 93927 | pTab = pColExpr->pTab; |
| 93621 | 93928 | if( iCol<0 ) iCol = pTab->iPKey; |
| | @@ -98609,10 +98916,11 @@ |
| 98609 | 98916 | break; |
| 98610 | 98917 | } |
| 98611 | 98918 | } |
| 98612 | 98919 | } |
| 98613 | 98920 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 98921 | + assert( aRegIdx ); |
| 98614 | 98922 | if( openAll || aRegIdx[i]>0 ){ |
| 98615 | 98923 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 98616 | 98924 | sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb, |
| 98617 | 98925 | (char*)pKey, P4_KEYINFO_HANDOFF); |
| 98618 | 98926 | assert( pParse->nTab>iCur+i+1 ); |
| | @@ -98782,10 +99090,11 @@ |
| 98782 | 99090 | sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); |
| 98783 | 99091 | sqlite3VdbeJumpHere(v, addr); |
| 98784 | 99092 | |
| 98785 | 99093 | /* Close all tables */ |
| 98786 | 99094 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 99095 | + assert( aRegIdx ); |
| 98787 | 99096 | if( openAll || aRegIdx[i]>0 ){ |
| 98788 | 99097 | sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0); |
| 98789 | 99098 | } |
| 98790 | 99099 | } |
| 98791 | 99100 | sqlite3VdbeAddOp2(v, OP_Close, iCur, 0); |
| | @@ -98969,11 +99278,11 @@ |
| 98969 | 99278 | if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ |
| 98970 | 99279 | sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); |
| 98971 | 99280 | return sqlite3_errcode(db); |
| 98972 | 99281 | } |
| 98973 | 99282 | VVA_ONLY( rc = ) sqlite3_step(pStmt); |
| 98974 | | - assert( rc!=SQLITE_ROW ); |
| 99283 | + assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) ); |
| 98975 | 99284 | return vacuumFinalize(db, pStmt, pzErrMsg); |
| 98976 | 99285 | } |
| 98977 | 99286 | |
| 98978 | 99287 | /* |
| 98979 | 99288 | ** Execute zSql on database db. The statement returns exactly |
| | @@ -99187,17 +99496,15 @@ |
| 99187 | 99496 | " WHERE type='view' OR type='trigger'" |
| 99188 | 99497 | " OR (type='table' AND rootpage=0)" |
| 99189 | 99498 | ); |
| 99190 | 99499 | if( rc ) goto end_of_vacuum; |
| 99191 | 99500 | |
| 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. |
| 99501 | + /* At this point, there is a write transaction open on both the |
| 99502 | + ** vacuum database and the main database. Assuming no error occurs, |
| 99503 | + ** both transactions are closed by this block - the main database |
| 99504 | + ** transaction by sqlite3BtreeCopyFile() and the other by an explicit |
| 99505 | + ** call to sqlite3BtreeCommit(). |
| 99199 | 99506 | */ |
| 99200 | 99507 | { |
| 99201 | 99508 | u32 meta; |
| 99202 | 99509 | int i; |
| 99203 | 99510 | |
| | @@ -100457,25 +100764,35 @@ |
| 100457 | 100764 | #define TERM_CODED 0x04 /* This term is already coded */ |
| 100458 | 100765 | #define TERM_COPIED 0x08 /* Has a child */ |
| 100459 | 100766 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 100460 | 100767 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 100461 | 100768 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
| 100462 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 100769 | +#ifdef SQLITE_ENABLE_STAT3 |
| 100463 | 100770 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 100464 | 100771 | #else |
| 100465 | | -# define TERM_VNULL 0x00 /* Disabled if not using stat2 */ |
| 100772 | +# define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 100466 | 100773 | #endif |
| 100467 | 100774 | |
| 100468 | 100775 | /* |
| 100469 | 100776 | ** An instance of the following structure holds all information about a |
| 100470 | 100777 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 100778 | +** |
| 100779 | +** Explanation of pOuter: For a WHERE clause of the form |
| 100780 | +** |
| 100781 | +** a AND ((b AND c) OR (d AND e)) AND f |
| 100782 | +** |
| 100783 | +** There are separate WhereClause objects for the whole clause and for |
| 100784 | +** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 100785 | +** subclauses points to the WhereClause object for the whole clause. |
| 100471 | 100786 | */ |
| 100472 | 100787 | struct WhereClause { |
| 100473 | 100788 | Parse *pParse; /* The parser context */ |
| 100474 | 100789 | WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */ |
| 100475 | 100790 | Bitmask vmask; /* Bitmask identifying virtual table cursors */ |
| 100791 | + WhereClause *pOuter; /* Outer conjunction */ |
| 100476 | 100792 | u8 op; /* Split operator. TK_AND or TK_OR */ |
| 100793 | + u16 wctrlFlags; /* Might include WHERE_AND_ONLY */ |
| 100477 | 100794 | int nTerm; /* Number of terms */ |
| 100478 | 100795 | int nSlot; /* Number of entries in a[] */ |
| 100479 | 100796 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 100480 | 100797 | #if defined(SQLITE_SMALL_STACK) |
| 100481 | 100798 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| | @@ -100600,18 +100917,21 @@ |
| 100600 | 100917 | ** Initialize a preallocated WhereClause structure. |
| 100601 | 100918 | */ |
| 100602 | 100919 | static void whereClauseInit( |
| 100603 | 100920 | WhereClause *pWC, /* The WhereClause to be initialized */ |
| 100604 | 100921 | Parse *pParse, /* The parsing context */ |
| 100605 | | - WhereMaskSet *pMaskSet /* Mapping from table cursor numbers to bitmasks */ |
| 100922 | + WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmasks */ |
| 100923 | + u16 wctrlFlags /* Might include WHERE_AND_ONLY */ |
| 100606 | 100924 | ){ |
| 100607 | 100925 | pWC->pParse = pParse; |
| 100608 | 100926 | pWC->pMaskSet = pMaskSet; |
| 100927 | + pWC->pOuter = 0; |
| 100609 | 100928 | pWC->nTerm = 0; |
| 100610 | 100929 | pWC->nSlot = ArraySize(pWC->aStatic); |
| 100611 | 100930 | pWC->a = pWC->aStatic; |
| 100612 | 100931 | pWC->vmask = 0; |
| 100932 | + pWC->wctrlFlags = wctrlFlags; |
| 100613 | 100933 | } |
| 100614 | 100934 | |
| 100615 | 100935 | /* Forward reference */ |
| 100616 | 100936 | static void whereClauseClear(WhereClause*); |
| 100617 | 100937 | |
| | @@ -100923,40 +101243,42 @@ |
| 100923 | 101243 | ){ |
| 100924 | 101244 | WhereTerm *pTerm; |
| 100925 | 101245 | int k; |
| 100926 | 101246 | assert( iCur>=0 ); |
| 100927 | 101247 | 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; |
| 101248 | + for(; pWC; pWC=pWC->pOuter){ |
| 101249 | + for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){ |
| 101250 | + if( pTerm->leftCursor==iCur |
| 101251 | + && (pTerm->prereqRight & notReady)==0 |
| 101252 | + && pTerm->u.leftColumn==iColumn |
| 101253 | + && (pTerm->eOperator & op)!=0 |
| 101254 | + ){ |
| 101255 | + if( pIdx && pTerm->eOperator!=WO_ISNULL ){ |
| 101256 | + Expr *pX = pTerm->pExpr; |
| 101257 | + CollSeq *pColl; |
| 101258 | + char idxaff; |
| 101259 | + int j; |
| 101260 | + Parse *pParse = pWC->pParse; |
| 101261 | + |
| 101262 | + idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 101263 | + if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue; |
| 101264 | + |
| 101265 | + /* Figure out the collation sequence required from an index for |
| 101266 | + ** it to be useful for optimising expression pX. Store this |
| 101267 | + ** value in variable pColl. |
| 101268 | + */ |
| 101269 | + assert(pX->pLeft); |
| 101270 | + pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| 101271 | + assert(pColl || pParse->nErr); |
| 101272 | + |
| 101273 | + for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 101274 | + if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 101275 | + } |
| 101276 | + if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue; |
| 101277 | + } |
| 101278 | + return pTerm; |
| 101279 | + } |
| 100958 | 101280 | } |
| 100959 | 101281 | } |
| 100960 | 101282 | return 0; |
| 100961 | 101283 | } |
| 100962 | 101284 | |
| | @@ -101029,11 +101351,11 @@ |
| 101029 | 101351 | int iCol = pRight->iColumn; |
| 101030 | 101352 | pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); |
| 101031 | 101353 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 101032 | 101354 | z = (char *)sqlite3_value_text(pVal); |
| 101033 | 101355 | } |
| 101034 | | - sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */ |
| 101356 | + sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-31526-56213 */ |
| 101035 | 101357 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 101036 | 101358 | }else if( op==TK_STRING ){ |
| 101037 | 101359 | z = pRight->u.zToken; |
| 101038 | 101360 | } |
| 101039 | 101361 | if( z ){ |
| | @@ -101047,11 +101369,11 @@ |
| 101047 | 101369 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 101048 | 101370 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 101049 | 101371 | *ppPrefix = pPrefix; |
| 101050 | 101372 | if( op==TK_VARIABLE ){ |
| 101051 | 101373 | Vdbe *v = pParse->pVdbe; |
| 101052 | | - sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */ |
| 101374 | + sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-31526-56213 */ |
| 101053 | 101375 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 101054 | 101376 | /* If the rhs of the LIKE expression is a variable, and the current |
| 101055 | 101377 | ** value of the variable means there is no need to invoke the LIKE |
| 101056 | 101378 | ** function, then no OP_Variable will be added to the program. |
| 101057 | 101379 | ** This causes problems for the sqlite3_bind_parameter_name() |
| | @@ -101216,11 +101538,11 @@ |
| 101216 | 101538 | assert( pExpr->op==TK_OR ); |
| 101217 | 101539 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 101218 | 101540 | if( pOrInfo==0 ) return; |
| 101219 | 101541 | pTerm->wtFlags |= TERM_ORINFO; |
| 101220 | 101542 | pOrWc = &pOrInfo->wc; |
| 101221 | | - whereClauseInit(pOrWc, pWC->pParse, pMaskSet); |
| 101543 | + whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
| 101222 | 101544 | whereSplit(pOrWc, pExpr, TK_OR); |
| 101223 | 101545 | exprAnalyzeAll(pSrc, pOrWc); |
| 101224 | 101546 | if( db->mallocFailed ) return; |
| 101225 | 101547 | assert( pOrWc->nTerm>=2 ); |
| 101226 | 101548 | |
| | @@ -101243,13 +101565,14 @@ |
| 101243 | 101565 | Bitmask b = 0; |
| 101244 | 101566 | pOrTerm->u.pAndInfo = pAndInfo; |
| 101245 | 101567 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 101246 | 101568 | pOrTerm->eOperator = WO_AND; |
| 101247 | 101569 | pAndWC = &pAndInfo->wc; |
| 101248 | | - whereClauseInit(pAndWC, pWC->pParse, pMaskSet); |
| 101570 | + whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags); |
| 101249 | 101571 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 101250 | 101572 | exprAnalyzeAll(pSrc, pAndWC); |
| 101573 | + pAndWC->pOuter = pWC; |
| 101251 | 101574 | testcase( db->mallocFailed ); |
| 101252 | 101575 | if( !db->mallocFailed ){ |
| 101253 | 101576 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 101254 | 101577 | assert( pAndTerm->pExpr ); |
| 101255 | 101578 | if( allowedOp(pAndTerm->pExpr->op) ){ |
| | @@ -101679,12 +102002,12 @@ |
| 101679 | 102002 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 101680 | 102003 | } |
| 101681 | 102004 | } |
| 101682 | 102005 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 101683 | 102006 | |
| 101684 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 101685 | | - /* When sqlite_stat2 histogram data is available an operator of the |
| 102007 | +#ifdef SQLITE_ENABLE_STAT3 |
| 102008 | + /* When sqlite_stat3 histogram data is available an operator of the |
| 101686 | 102009 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 101687 | 102010 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 101688 | 102011 | ** virtual term of that form. |
| 101689 | 102012 | ** |
| 101690 | 102013 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| | @@ -101718,11 +102041,11 @@ |
| 101718 | 102041 | pTerm->nChild = 1; |
| 101719 | 102042 | pTerm->wtFlags |= TERM_COPIED; |
| 101720 | 102043 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 101721 | 102044 | } |
| 101722 | 102045 | } |
| 101723 | | -#endif /* SQLITE_ENABLE_STAT2 */ |
| 102046 | +#endif /* SQLITE_ENABLE_STAT */ |
| 101724 | 102047 | |
| 101725 | 102048 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 101726 | 102049 | ** an index for tables to the left of the join. |
| 101727 | 102050 | */ |
| 101728 | 102051 | pTerm->prereqRight |= extraRight; |
| | @@ -102140,14 +102463,17 @@ |
| 102140 | 102463 | const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */ |
| 102141 | 102464 | const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */ |
| 102142 | 102465 | WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */ |
| 102143 | 102466 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 102144 | 102467 | |
| 102145 | | - /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses |
| 102146 | | - ** are used */ |
| 102468 | + /* The OR-clause optimization is disallowed if the INDEXED BY or |
| 102469 | + ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */ |
| 102147 | 102470 | if( pSrc->notIndexed || pSrc->pIndex!=0 ){ |
| 102148 | 102471 | return; |
| 102472 | + } |
| 102473 | + if( pWC->wctrlFlags & WHERE_AND_ONLY ){ |
| 102474 | + return; |
| 102149 | 102475 | } |
| 102150 | 102476 | |
| 102151 | 102477 | /* Search the WHERE clause terms for a usable WO_OR term. */ |
| 102152 | 102478 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 102153 | 102479 | if( pTerm->eOperator==WO_OR |
| | @@ -102172,10 +102498,11 @@ |
| 102172 | 102498 | bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost); |
| 102173 | 102499 | }else if( pOrTerm->leftCursor==iCur ){ |
| 102174 | 102500 | WhereClause tempWC; |
| 102175 | 102501 | tempWC.pParse = pWC->pParse; |
| 102176 | 102502 | tempWC.pMaskSet = pWC->pMaskSet; |
| 102503 | + tempWC.pOuter = pWC; |
| 102177 | 102504 | tempWC.op = TK_AND; |
| 102178 | 102505 | tempWC.a = pOrTerm; |
| 102179 | 102506 | tempWC.nTerm = 1; |
| 102180 | 102507 | bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost); |
| 102181 | 102508 | }else{ |
| | @@ -102766,71 +103093,89 @@ |
| 102766 | 103093 | */ |
| 102767 | 103094 | bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost); |
| 102768 | 103095 | } |
| 102769 | 103096 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 102770 | 103097 | |
| 103098 | +#ifdef SQLITE_ENABLE_STAT3 |
| 102771 | 103099 | /* |
| 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( |
| 103100 | +** Estimate the location of a particular key among all keys in an |
| 103101 | +** index. Store the results in aStat as follows: |
| 103102 | +** |
| 103103 | +** aStat[0] Est. number of rows less than pVal |
| 103104 | +** aStat[1] Est. number of rows equal to pVal |
| 103105 | +** |
| 103106 | +** Return SQLITE_OK on success. |
| 103107 | +*/ |
| 103108 | +static int whereKeyStats( |
| 102795 | 103109 | Parse *pParse, /* Database connection */ |
| 102796 | 103110 | Index *pIdx, /* Index to consider domain of */ |
| 102797 | 103111 | 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 */ |
| 103112 | + int roundUp, /* Round up if true. Round down if false */ |
| 103113 | + tRowcnt *aStat /* OUT: stats written here */ |
| 102800 | 103114 | ){ |
| 103115 | + tRowcnt n; |
| 103116 | + IndexSample *aSample; |
| 103117 | + int i, eType; |
| 103118 | + int isEq = 0; |
| 103119 | + i64 v; |
| 103120 | + double r, rS; |
| 103121 | + |
| 102801 | 103122 | 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{ |
| 103123 | + assert( pIdx->nSample>0 ); |
| 103124 | + if( pVal==0 ) return SQLITE_ERROR; |
| 103125 | + n = pIdx->aiRowEst[0]; |
| 103126 | + aSample = pIdx->aSample; |
| 103127 | + eType = sqlite3_value_type(pVal); |
| 103128 | + |
| 103129 | + if( eType==SQLITE_INTEGER ){ |
| 103130 | + v = sqlite3_value_int64(pVal); |
| 103131 | + r = (i64)v; |
| 103132 | + for(i=0; i<pIdx->nSample; i++){ |
| 103133 | + if( aSample[i].eType==SQLITE_NULL ) continue; |
| 103134 | + if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 103135 | + if( aSample[i].eType==SQLITE_INTEGER ){ |
| 103136 | + if( aSample[i].u.i>=v ){ |
| 103137 | + isEq = aSample[i].u.i==v; |
| 103138 | + break; |
| 103139 | + } |
| 103140 | + }else{ |
| 103141 | + assert( aSample[i].eType==SQLITE_FLOAT ); |
| 103142 | + if( aSample[i].u.r>=r ){ |
| 103143 | + isEq = aSample[i].u.r==r; |
| 103144 | + break; |
| 103145 | + } |
| 103146 | + } |
| 103147 | + } |
| 103148 | + }else if( eType==SQLITE_FLOAT ){ |
| 103149 | + r = sqlite3_value_double(pVal); |
| 103150 | + for(i=0; i<pIdx->nSample; i++){ |
| 103151 | + if( aSample[i].eType==SQLITE_NULL ) continue; |
| 103152 | + if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 103153 | + if( aSample[i].eType==SQLITE_FLOAT ){ |
| 103154 | + rS = aSample[i].u.r; |
| 103155 | + }else{ |
| 103156 | + rS = aSample[i].u.i; |
| 103157 | + } |
| 103158 | + if( rS>=r ){ |
| 103159 | + isEq = rS==r; |
| 103160 | + break; |
| 103161 | + } |
| 103162 | + } |
| 103163 | + }else if( eType==SQLITE_NULL ){ |
| 103164 | + i = 0; |
| 103165 | + if( aSample[0].eType==SQLITE_NULL ) isEq = 1; |
| 103166 | + }else{ |
| 103167 | + assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 103168 | + for(i=0; i<pIdx->nSample; i++){ |
| 103169 | + if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){ |
| 103170 | + break; |
| 103171 | + } |
| 103172 | + } |
| 103173 | + if( i<pIdx->nSample ){ |
| 102824 | 103174 | sqlite3 *db = pParse->db; |
| 102825 | 103175 | CollSeq *pColl; |
| 102826 | 103176 | 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 | 103177 | if( eType==SQLITE_BLOB ){ |
| 102833 | 103178 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 102834 | 103179 | pColl = db->pDfltColl; |
| 102835 | 103180 | assert( pColl->enc==SQLITE_UTF8 ); |
| 102836 | 103181 | }else{ |
| | @@ -102845,16 +103190,16 @@ |
| 102845 | 103190 | return SQLITE_NOMEM; |
| 102846 | 103191 | } |
| 102847 | 103192 | assert( z && pColl && pColl->xCmp ); |
| 102848 | 103193 | } |
| 102849 | 103194 | n = sqlite3ValueBytes(pVal, pColl->enc); |
| 102850 | | - |
| 102851 | | - for(i=0; i<SQLITE_INDEX_SAMPLES; i++){ |
| 103195 | + |
| 103196 | + for(; i<pIdx->nSample; i++){ |
| 102852 | 103197 | int c; |
| 102853 | 103198 | int eSampletype = aSample[i].eType; |
| 102854 | | - if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue; |
| 102855 | | - if( (eSampletype!=eType) ) break; |
| 103199 | + if( eSampletype<eType ) continue; |
| 103200 | + if( eSampletype!=eType ) break; |
| 102856 | 103201 | #ifndef SQLITE_OMIT_UTF16 |
| 102857 | 103202 | if( pColl->enc!=SQLITE_UTF8 ){ |
| 102858 | 103203 | int nSample; |
| 102859 | 103204 | char *zSample = sqlite3Utf8to16( |
| 102860 | 103205 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| | @@ -102868,20 +103213,51 @@ |
| 102868 | 103213 | }else |
| 102869 | 103214 | #endif |
| 102870 | 103215 | { |
| 102871 | 103216 | c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z); |
| 102872 | 103217 | } |
| 102873 | | - if( c-roundUp>=0 ) break; |
| 103218 | + if( c>=0 ){ |
| 103219 | + if( c==0 ) isEq = 1; |
| 103220 | + break; |
| 103221 | + } |
| 102874 | 103222 | } |
| 102875 | 103223 | } |
| 103224 | + } |
| 102876 | 103225 | |
| 102877 | | - assert( i>=0 && i<=SQLITE_INDEX_SAMPLES ); |
| 102878 | | - *piRegion = i; |
| 103226 | + /* At this point, aSample[i] is the first sample that is greater than |
| 103227 | + ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 103228 | + ** than pVal. If aSample[i]==pVal, then isEq==1. |
| 103229 | + */ |
| 103230 | + if( isEq ){ |
| 103231 | + assert( i<pIdx->nSample ); |
| 103232 | + aStat[0] = aSample[i].nLt; |
| 103233 | + aStat[1] = aSample[i].nEq; |
| 103234 | + }else{ |
| 103235 | + tRowcnt iLower, iUpper, iGap; |
| 103236 | + if( i==0 ){ |
| 103237 | + iLower = 0; |
| 103238 | + iUpper = aSample[0].nLt; |
| 103239 | + }else{ |
| 103240 | + iUpper = i>=pIdx->nSample ? n : aSample[i].nLt; |
| 103241 | + iLower = aSample[i-1].nEq + aSample[i-1].nLt; |
| 103242 | + } |
| 103243 | + aStat[1] = pIdx->avgEq; |
| 103244 | + if( iLower>=iUpper ){ |
| 103245 | + iGap = 0; |
| 103246 | + }else{ |
| 103247 | + iGap = iUpper - iLower; |
| 103248 | + } |
| 103249 | + if( roundUp ){ |
| 103250 | + iGap = (iGap*2)/3; |
| 103251 | + }else{ |
| 103252 | + iGap = iGap/3; |
| 103253 | + } |
| 103254 | + aStat[0] = iLower + iGap; |
| 102879 | 103255 | } |
| 102880 | 103256 | return SQLITE_OK; |
| 102881 | 103257 | } |
| 102882 | | -#endif /* #ifdef SQLITE_ENABLE_STAT2 */ |
| 103258 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 102883 | 103259 | |
| 102884 | 103260 | /* |
| 102885 | 103261 | ** If expression pExpr represents a literal value, set *pp to point to |
| 102886 | 103262 | ** an sqlite3_value structure containing the same value, with affinity |
| 102887 | 103263 | ** aff applied to it, before returning. It is the responsibility of the |
| | @@ -102895,11 +103271,11 @@ |
| 102895 | 103271 | ** |
| 102896 | 103272 | ** If neither of the above apply, set *pp to NULL. |
| 102897 | 103273 | ** |
| 102898 | 103274 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 102899 | 103275 | */ |
| 102900 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103276 | +#ifdef SQLITE_ENABLE_STAT3 |
| 102901 | 103277 | static int valueFromExpr( |
| 102902 | 103278 | Parse *pParse, |
| 102903 | 103279 | Expr *pExpr, |
| 102904 | 103280 | u8 aff, |
| 102905 | 103281 | sqlite3_value **pp |
| | @@ -102906,11 +103282,11 @@ |
| 102906 | 103282 | ){ |
| 102907 | 103283 | if( pExpr->op==TK_VARIABLE |
| 102908 | 103284 | || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) |
| 102909 | 103285 | ){ |
| 102910 | 103286 | int iVar = pExpr->iColumn; |
| 102911 | | - sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */ |
| 103287 | + sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-31526-56213 */ |
| 102912 | 103288 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 102913 | 103289 | return SQLITE_OK; |
| 102914 | 103290 | } |
| 102915 | 103291 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 102916 | 103292 | } |
| | @@ -102943,106 +103319,92 @@ |
| 102943 | 103319 | ** |
| 102944 | 103320 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 102945 | 103321 | ** |
| 102946 | 103322 | ** then nEq should be passed 0. |
| 102947 | 103323 | ** |
| 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. |
| 103324 | +** The returned value is an integer divisor to reduce the estimated |
| 103325 | +** search space. A return value of 1 means that range constraints are |
| 103326 | +** no help at all. A return value of 2 means range constraints are |
| 103327 | +** expected to reduce the search space by half. And so forth... |
| 102954 | 103328 | ** |
| 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. |
| 103329 | +** In the absence of sqlite_stat3 ANALYZE data, each range inequality |
| 103330 | +** reduces the search space by a factor of 4. Hence a single constraint (x>?) |
| 103331 | +** results in a return of 4 and a range constraint (x>? AND x<?) results |
| 103332 | +** in a return of 16. |
| 102959 | 103333 | */ |
| 102960 | 103334 | static int whereRangeScanEst( |
| 102961 | 103335 | Parse *pParse, /* Parsing & code generating context */ |
| 102962 | 103336 | Index *p, /* The index containing the range-compared column; "x" */ |
| 102963 | 103337 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 102964 | 103338 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 102965 | 103339 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 102966 | | - int *piEst /* OUT: Return value */ |
| 103340 | + double *pRangeDiv /* OUT: Reduce search space by this divisor */ |
| 102967 | 103341 | ){ |
| 102968 | 103342 | int rc = SQLITE_OK; |
| 102969 | 103343 | |
| 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; |
| 103344 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103345 | + |
| 103346 | + if( nEq==0 && p->nSample ){ |
| 103347 | + sqlite3_value *pRangeVal; |
| 103348 | + tRowcnt iLower = 0; |
| 103349 | + tRowcnt iUpper = p->aiRowEst[0]; |
| 103350 | + tRowcnt a[2]; |
| 102980 | 103351 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 102981 | 103352 | |
| 102982 | 103353 | if( pLower ){ |
| 102983 | 103354 | Expr *pExpr = pLower->pExpr->pRight; |
| 102984 | | - rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal); |
| 103355 | + rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
| 102985 | 103356 | assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE ); |
| 102986 | | - roundUpLower = (pLower->eOperator==WO_GT) ?1:0; |
| 103357 | + if( rc==SQLITE_OK |
| 103358 | + && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK |
| 103359 | + ){ |
| 103360 | + iLower = a[0]; |
| 103361 | + if( pLower->eOperator==WO_GT ) iLower += a[1]; |
| 103362 | + } |
| 103363 | + sqlite3ValueFree(pRangeVal); |
| 102987 | 103364 | } |
| 102988 | 103365 | if( rc==SQLITE_OK && pUpper ){ |
| 102989 | 103366 | Expr *pExpr = pUpper->pExpr->pRight; |
| 102990 | | - rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal); |
| 103367 | + rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
| 102991 | 103368 | 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: |
| 103369 | + if( rc==SQLITE_OK |
| 103370 | + && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK |
| 103371 | + ){ |
| 103372 | + iUpper = a[0]; |
| 103373 | + if( pUpper->eOperator==WO_LE ) iUpper += a[1]; |
| 103374 | + } |
| 103375 | + sqlite3ValueFree(pRangeVal); |
| 103376 | + } |
| 103377 | + if( rc==SQLITE_OK ){ |
| 103378 | + if( iUpper<=iLower ){ |
| 103379 | + *pRangeDiv = (double)p->aiRowEst[0]; |
| 103380 | + }else{ |
| 103381 | + *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower); |
| 103382 | + } |
| 103383 | + WHERETRACE(("range scan regions: %u..%u div=%g\n", |
| 103384 | + (u32)iLower, (u32)iUpper, *pRangeDiv)); |
| 103385 | + return SQLITE_OK; |
| 103386 | + } |
| 103387 | + } |
| 103026 | 103388 | #else |
| 103027 | 103389 | UNUSED_PARAMETER(pParse); |
| 103028 | 103390 | UNUSED_PARAMETER(p); |
| 103029 | 103391 | UNUSED_PARAMETER(nEq); |
| 103030 | 103392 | #endif |
| 103031 | 103393 | assert( pLower || pUpper ); |
| 103032 | | - *piEst = 100; |
| 103033 | | - if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4; |
| 103034 | | - if( pUpper ) *piEst /= 4; |
| 103394 | + *pRangeDiv = (double)1; |
| 103395 | + if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4; |
| 103396 | + if( pUpper ) *pRangeDiv *= (double)4; |
| 103035 | 103397 | return rc; |
| 103036 | 103398 | } |
| 103037 | 103399 | |
| 103038 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103400 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103039 | 103401 | /* |
| 103040 | 103402 | ** Estimate the number of rows that will be returned based on |
| 103041 | 103403 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 103042 | 103404 | ** the histogram data. This only works when x is the left-most |
| 103043 | | -** column of an index and sqlite_stat2 histogram data is available |
| 103405 | +** column of an index and sqlite_stat3 histogram data is available |
| 103044 | 103406 | ** for that index. When pExpr==NULL that means the constraint is |
| 103045 | 103407 | ** "x IS NULL" instead of "x=VALUE". |
| 103046 | 103408 | ** |
| 103047 | 103409 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 103048 | 103410 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| | @@ -103058,44 +103420,36 @@ |
| 103058 | 103420 | Index *p, /* The index whose left-most column is pTerm */ |
| 103059 | 103421 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 103060 | 103422 | double *pnRow /* Write the revised row estimate here */ |
| 103061 | 103423 | ){ |
| 103062 | 103424 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
| 103063 | | - int iLower, iUpper; /* Range of histogram regions containing pRhs */ |
| 103064 | 103425 | u8 aff; /* Column affinity */ |
| 103065 | 103426 | int rc; /* Subfunction return code */ |
| 103066 | | - double nRowEst; /* New estimate of the number of rows */ |
| 103427 | + tRowcnt a[2]; /* Statistics */ |
| 103067 | 103428 | |
| 103068 | 103429 | assert( p->aSample!=0 ); |
| 103430 | + assert( p->nSample>0 ); |
| 103069 | 103431 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 103070 | 103432 | if( pExpr ){ |
| 103071 | 103433 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| 103072 | 103434 | if( rc ) goto whereEqualScanEst_cancel; |
| 103073 | 103435 | }else{ |
| 103074 | 103436 | pRhs = sqlite3ValueNew(pParse->db); |
| 103075 | 103437 | } |
| 103076 | 103438 | 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 | | - |
| 103439 | + rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 103440 | + if( rc==SQLITE_OK ){ |
| 103441 | + WHERETRACE(("equality scan regions: %d\n", (int)a[1])); |
| 103442 | + *pnRow = a[1]; |
| 103443 | + } |
| 103090 | 103444 | whereEqualScanEst_cancel: |
| 103091 | 103445 | sqlite3ValueFree(pRhs); |
| 103092 | 103446 | return rc; |
| 103093 | 103447 | } |
| 103094 | | -#endif /* defined(SQLITE_ENABLE_STAT2) */ |
| 103448 | +#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 103095 | 103449 | |
| 103096 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103450 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103097 | 103451 | /* |
| 103098 | 103452 | ** Estimate the number of rows that will be returned based on |
| 103099 | 103453 | ** an IN constraint where the right-hand side of the IN operator |
| 103100 | 103454 | ** is a list of values. Example: |
| 103101 | 103455 | ** |
| | @@ -103114,64 +103468,29 @@ |
| 103114 | 103468 | Parse *pParse, /* Parsing & code generating context */ |
| 103115 | 103469 | Index *p, /* The index whose left-most column is pTerm */ |
| 103116 | 103470 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 103117 | 103471 | double *pnRow /* Write the revised row estimate here */ |
| 103118 | 103472 | ){ |
| 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 */ |
| 103473 | + int rc = SQLITE_OK; /* Subfunction return code */ |
| 103474 | + double nEst; /* Number of rows for a single term */ |
| 103475 | + double nRowEst = (double)0; /* New estimate of the number of rows */ |
| 103476 | + int i; /* Loop counter */ |
| 103130 | 103477 | |
| 103131 | 103478 | 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 | | - } |
| 103479 | + for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 103480 | + nEst = p->aiRowEst[0]; |
| 103481 | + rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| 103482 | + nRowEst += nEst; |
| 103153 | 103483 | } |
| 103154 | 103484 | 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 | 103485 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 103165 | 103486 | *pnRow = nRowEst; |
| 103166 | | - WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n", |
| 103167 | | - nSpan, nSingle, nNotFound, nRowEst)); |
| 103487 | + WHERETRACE(("IN row estimate: est=%g\n", nRowEst)); |
| 103168 | 103488 | } |
| 103169 | | - sqlite3ValueFree(pVal); |
| 103170 | 103489 | return rc; |
| 103171 | 103490 | } |
| 103172 | | -#endif /* defined(SQLITE_ENABLE_STAT2) */ |
| 103491 | +#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 103173 | 103492 | |
| 103174 | 103493 | |
| 103175 | 103494 | /* |
| 103176 | 103495 | ** Find the best query plan for accessing a particular table. Write the |
| 103177 | 103496 | ** best query plan and its cost into the WhereCost object supplied as the |
| | @@ -103214,11 +103533,11 @@ |
| 103214 | 103533 | Index *pProbe; /* An index we are evaluating */ |
| 103215 | 103534 | Index *pIdx; /* Copy of pProbe, or zero for IPK index */ |
| 103216 | 103535 | int eqTermMask; /* Current mask of valid equality operators */ |
| 103217 | 103536 | int idxEqTermMask; /* Index mask of valid equality operators */ |
| 103218 | 103537 | Index sPk; /* A fake index object for the primary key */ |
| 103219 | | - unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 103538 | + tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 103220 | 103539 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 103221 | 103540 | int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */ |
| 103222 | 103541 | |
| 103223 | 103542 | /* Initialize the cost to a worst-case value */ |
| 103224 | 103543 | memset(pCost, 0, sizeof(*pCost)); |
| | @@ -103269,14 +103588,14 @@ |
| 103269 | 103588 | } |
| 103270 | 103589 | |
| 103271 | 103590 | /* Loop over all indices looking for the best one to use |
| 103272 | 103591 | */ |
| 103273 | 103592 | for(; pProbe; pIdx=pProbe=pProbe->pNext){ |
| 103274 | | - const unsigned int * const aiRowEst = pProbe->aiRowEst; |
| 103593 | + const tRowcnt * const aiRowEst = pProbe->aiRowEst; |
| 103275 | 103594 | double cost; /* Cost of using pProbe */ |
| 103276 | 103595 | double nRow; /* Estimated number of rows in result set */ |
| 103277 | | - double log10N; /* base-10 logarithm of nRow (inexact) */ |
| 103596 | + double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */ |
| 103278 | 103597 | int rev; /* True to scan in reverse order */ |
| 103279 | 103598 | int wsFlags = 0; |
| 103280 | 103599 | Bitmask used = 0; |
| 103281 | 103600 | |
| 103282 | 103601 | /* The following variables are populated based on the properties of |
| | @@ -103312,18 +103631,16 @@ |
| 103312 | 103631 | ** Set to true if there was at least one "x IN (SELECT ...)" term used |
| 103313 | 103632 | ** in determining the value of nInMul. Note that the RHS of the |
| 103314 | 103633 | ** IN operator must be a SELECT, not a value list, for this variable |
| 103315 | 103634 | ** to be true. |
| 103316 | 103635 | ** |
| 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. |
| 103636 | + ** rangeDiv: |
| 103637 | + ** An estimate of a divisor by which to reduce the search space due |
| 103638 | + ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE |
| 103639 | + ** data, a single inequality reduces the search space to 1/4rd its |
| 103640 | + ** original size (rangeDiv==4). Two inequalities reduce the search |
| 103641 | + ** space to 1/16th of its original size (rangeDiv==16). |
| 103325 | 103642 | ** |
| 103326 | 103643 | ** bSort: |
| 103327 | 103644 | ** Boolean. True if there is an ORDER BY clause that will require an |
| 103328 | 103645 | ** external sort (i.e. scanning the index being evaluated will not |
| 103329 | 103646 | ** correctly order records). |
| | @@ -103344,26 +103661,27 @@ |
| 103344 | 103661 | ** SELECT a, b, c FROM tbl WHERE a = 1; |
| 103345 | 103662 | */ |
| 103346 | 103663 | int nEq; /* Number of == or IN terms matching index */ |
| 103347 | 103664 | int bInEst = 0; /* True if "x IN (SELECT...)" seen */ |
| 103348 | 103665 | int nInMul = 1; /* Number of distinct equalities to lookup */ |
| 103349 | | - int estBound = 100; /* Estimated reduction in search space */ |
| 103666 | + double rangeDiv = (double)1; /* Estimated reduction in search space */ |
| 103350 | 103667 | int nBound = 0; /* Number of range constraints seen */ |
| 103351 | 103668 | int bSort = !!pOrderBy; /* True if external sort required */ |
| 103352 | 103669 | int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */ |
| 103353 | 103670 | int bLookup = 0; /* True if not a covering index */ |
| 103354 | 103671 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 103355 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103672 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103356 | 103673 | WhereTerm *pFirstTerm = 0; /* First term matching the index */ |
| 103357 | 103674 | #endif |
| 103358 | 103675 | |
| 103359 | 103676 | /* Determine the values of nEq and nInMul */ |
| 103360 | 103677 | for(nEq=0; nEq<pProbe->nColumn; nEq++){ |
| 103361 | 103678 | int j = pProbe->aiColumn[nEq]; |
| 103362 | 103679 | pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx); |
| 103363 | 103680 | if( pTerm==0 ) break; |
| 103364 | 103681 | wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ); |
| 103682 | + testcase( pTerm->pWC!=pWC ); |
| 103365 | 103683 | if( pTerm->eOperator & WO_IN ){ |
| 103366 | 103684 | Expr *pExpr = pTerm->pExpr; |
| 103367 | 103685 | wsFlags |= WHERE_COLUMN_IN; |
| 103368 | 103686 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 103369 | 103687 | /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */ |
| | @@ -103374,32 +103692,34 @@ |
| 103374 | 103692 | nInMul *= pExpr->x.pList->nExpr; |
| 103375 | 103693 | } |
| 103376 | 103694 | }else if( pTerm->eOperator & WO_ISNULL ){ |
| 103377 | 103695 | wsFlags |= WHERE_COLUMN_NULL; |
| 103378 | 103696 | } |
| 103379 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103697 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103380 | 103698 | if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm; |
| 103381 | 103699 | #endif |
| 103382 | 103700 | used |= pTerm->prereqRight; |
| 103383 | 103701 | } |
| 103384 | 103702 | |
| 103385 | | - /* Determine the value of estBound. */ |
| 103703 | + /* Determine the value of rangeDiv */ |
| 103386 | 103704 | if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){ |
| 103387 | 103705 | int j = pProbe->aiColumn[nEq]; |
| 103388 | 103706 | if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){ |
| 103389 | 103707 | WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx); |
| 103390 | 103708 | WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx); |
| 103391 | | - whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound); |
| 103709 | + whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv); |
| 103392 | 103710 | if( pTop ){ |
| 103393 | 103711 | nBound = 1; |
| 103394 | 103712 | wsFlags |= WHERE_TOP_LIMIT; |
| 103395 | 103713 | used |= pTop->prereqRight; |
| 103714 | + testcase( pTop->pWC!=pWC ); |
| 103396 | 103715 | } |
| 103397 | 103716 | if( pBtm ){ |
| 103398 | 103717 | nBound++; |
| 103399 | 103718 | wsFlags |= WHERE_BTM_LIMIT; |
| 103400 | 103719 | used |= pBtm->prereqRight; |
| 103720 | + testcase( pBtm->pWC!=pWC ); |
| 103401 | 103721 | } |
| 103402 | 103722 | wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE); |
| 103403 | 103723 | } |
| 103404 | 103724 | }else if( pProbe->onError!=OE_None ){ |
| 103405 | 103725 | testcase( wsFlags & WHERE_COLUMN_IN ); |
| | @@ -103458,32 +103778,34 @@ |
| 103458 | 103778 | if( bInEst && nRow*2>aiRowEst[0] ){ |
| 103459 | 103779 | nRow = aiRowEst[0]/2; |
| 103460 | 103780 | nInMul = (int)(nRow / aiRowEst[nEq]); |
| 103461 | 103781 | } |
| 103462 | 103782 | |
| 103463 | | -#ifdef SQLITE_ENABLE_STAT2 |
| 103783 | +#ifdef SQLITE_ENABLE_STAT3 |
| 103464 | 103784 | /* If the constraint is of the form x=VALUE or x IN (E1,E2,...) |
| 103465 | 103785 | ** and we do not think that values of x are unique and if histogram |
| 103466 | 103786 | ** data is available for column x, then it might be possible |
| 103467 | 103787 | ** to get a better estimate on the number of rows based on |
| 103468 | 103788 | ** VALUE and how common that value is according to the histogram. |
| 103469 | 103789 | */ |
| 103470 | 103790 | if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){ |
| 103791 | + assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 ); |
| 103471 | 103792 | if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){ |
| 103472 | 103793 | testcase( pFirstTerm->eOperator==WO_EQ ); |
| 103473 | 103794 | testcase( pFirstTerm->eOperator==WO_ISNULL ); |
| 103474 | 103795 | whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow); |
| 103475 | | - }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){ |
| 103796 | + }else if( bInEst==0 ){ |
| 103797 | + assert( pFirstTerm->eOperator==WO_IN ); |
| 103476 | 103798 | whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow); |
| 103477 | 103799 | } |
| 103478 | 103800 | } |
| 103479 | | -#endif /* SQLITE_ENABLE_STAT2 */ |
| 103801 | +#endif /* SQLITE_ENABLE_STAT3 */ |
| 103480 | 103802 | |
| 103481 | 103803 | /* Adjust the number of output rows and downward to reflect rows |
| 103482 | 103804 | ** that are excluded by range constraints. |
| 103483 | 103805 | */ |
| 103484 | | - nRow = (nRow * (double)estBound) / (double)100; |
| 103806 | + nRow = nRow/rangeDiv; |
| 103485 | 103807 | if( nRow<1 ) nRow = 1; |
| 103486 | 103808 | |
| 103487 | 103809 | /* Experiments run on real SQLite databases show that the time needed |
| 103488 | 103810 | ** to do a binary search to locate a row in a table or index is roughly |
| 103489 | 103811 | ** log10(N) times the time to move from one row to the next row within |
| | @@ -103608,14 +103930,14 @@ |
| 103608 | 103930 | if( nRow<2 ) nRow = 2; |
| 103609 | 103931 | } |
| 103610 | 103932 | |
| 103611 | 103933 | |
| 103612 | 103934 | WHERETRACE(( |
| 103613 | | - "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 103935 | + "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 103614 | 103936 | " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n", |
| 103615 | 103937 | pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), |
| 103616 | | - nEq, nInMul, estBound, bSort, bLookup, wsFlags, |
| 103938 | + nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags, |
| 103617 | 103939 | notReady, log10N, nRow, cost, used |
| 103618 | 103940 | )); |
| 103619 | 103941 | |
| 103620 | 103942 | /* If this index is the best we have seen so far, then record this |
| 103621 | 103943 | ** index and its cost in the pCost structure. |
| | @@ -104115,11 +104437,12 @@ |
| 104115 | 104437 | */ |
| 104116 | 104438 | static Bitmask codeOneLoopStart( |
| 104117 | 104439 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 104118 | 104440 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 104119 | 104441 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 104120 | | - Bitmask notReady /* Which tables are currently available */ |
| 104442 | + Bitmask notReady, /* Which tables are currently available */ |
| 104443 | + Expr *pWhere /* Complete WHERE clause */ |
| 104121 | 104444 | ){ |
| 104122 | 104445 | int j, k; /* Loop counters */ |
| 104123 | 104446 | int iCur; /* The VDBE cursor for the table */ |
| 104124 | 104447 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 104125 | 104448 | int omitTable; /* True if we use the index only */ |
| | @@ -104597,11 +104920,12 @@ |
| 104597 | 104920 | int regRowset = 0; /* Register for RowSet object */ |
| 104598 | 104921 | int regRowid = 0; /* Register holding rowid */ |
| 104599 | 104922 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 104600 | 104923 | int iRetInit; /* Address of regReturn init */ |
| 104601 | 104924 | int untestedTerms = 0; /* Some terms not completely tested */ |
| 104602 | | - int ii; |
| 104925 | + int ii; /* Loop counter */ |
| 104926 | + Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 104603 | 104927 | |
| 104604 | 104928 | pTerm = pLevel->plan.u.pTerm; |
| 104605 | 104929 | assert( pTerm!=0 ); |
| 104606 | 104930 | assert( pTerm->eOperator==WO_OR ); |
| 104607 | 104931 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| | @@ -104646,18 +104970,33 @@ |
| 104646 | 104970 | regRowset = ++pParse->nMem; |
| 104647 | 104971 | regRowid = ++pParse->nMem; |
| 104648 | 104972 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 104649 | 104973 | } |
| 104650 | 104974 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 104975 | + |
| 104976 | + /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 104977 | + ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 104978 | + ** That way, terms in y that are factored into the disjunction will |
| 104979 | + ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
| 104980 | + */ |
| 104981 | + if( pWC->nTerm>1 ){ |
| 104982 | + pAndExpr = sqlite3ExprAlloc(pParse->db, TK_AND, 0, 0); |
| 104983 | + pAndExpr->pRight = pWhere; |
| 104984 | + } |
| 104651 | 104985 | |
| 104652 | 104986 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 104653 | 104987 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 104654 | 104988 | if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){ |
| 104655 | 104989 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 104990 | + Expr *pOrExpr = pOrTerm->pExpr; |
| 104991 | + if( pAndExpr ){ |
| 104992 | + pAndExpr->pLeft = pOrExpr; |
| 104993 | + pOrExpr = pAndExpr; |
| 104994 | + } |
| 104656 | 104995 | /* Loop through table entries that match term pOrTerm. */ |
| 104657 | | - pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0, 0, |
| 104658 | | - WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE | |
| 104996 | + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 104997 | + WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
| 104659 | 104998 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY); |
| 104660 | 104999 | if( pSubWInfo ){ |
| 104661 | 105000 | explainOneScan( |
| 104662 | 105001 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 104663 | 105002 | ); |
| | @@ -104681,10 +105020,11 @@ |
| 104681 | 105020 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 104682 | 105021 | sqlite3WhereEnd(pSubWInfo); |
| 104683 | 105022 | } |
| 104684 | 105023 | } |
| 104685 | 105024 | } |
| 105025 | + sqlite3DbFree(pParse->db, pAndExpr); |
| 104686 | 105026 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
| 104687 | 105027 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 104688 | 105028 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 104689 | 105029 | |
| 104690 | 105030 | if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab); |
| | @@ -104962,11 +105302,11 @@ |
| 104962 | 105302 | |
| 104963 | 105303 | /* Split the WHERE clause into separate subexpressions where each |
| 104964 | 105304 | ** subexpression is separated by an AND operator. |
| 104965 | 105305 | */ |
| 104966 | 105306 | initMaskSet(pMaskSet); |
| 104967 | | - whereClauseInit(pWC, pParse, pMaskSet); |
| 105307 | + whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags); |
| 104968 | 105308 | sqlite3ExprCodeConstants(pParse, pWhere); |
| 104969 | 105309 | whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
| 104970 | 105310 | |
| 104971 | 105311 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 104972 | 105312 | ** expression and either jump over all of the code or fall thru. |
| | @@ -105201,11 +105541,12 @@ |
| 105201 | 105541 | assert( bestJ>=0 ); |
| 105202 | 105542 | assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) ); |
| 105203 | 105543 | WHERETRACE(("*** Optimizer selects table %d for loop %d" |
| 105204 | 105544 | " with cost=%g and nRow=%g\n", |
| 105205 | 105545 | bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow)); |
| 105206 | | - if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){ |
| 105546 | + /* The ALWAYS() that follows was added to hush up clang scan-build */ |
| 105547 | + if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){ |
| 105207 | 105548 | *ppOrderBy = 0; |
| 105208 | 105549 | } |
| 105209 | 105550 | if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){ |
| 105210 | 105551 | assert( pWInfo->eDistinct==0 ); |
| 105211 | 105552 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| | @@ -105290,11 +105631,11 @@ |
| 105290 | 105631 | int iCur = pTabItem->iCursor; |
| 105291 | 105632 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| 105292 | 105633 | }else |
| 105293 | 105634 | #endif |
| 105294 | 105635 | if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 |
| 105295 | | - && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){ |
| 105636 | + && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
| 105296 | 105637 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 105297 | 105638 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 105298 | 105639 | testcase( pTab->nCol==BMS-1 ); |
| 105299 | 105640 | testcase( pTab->nCol==BMS ); |
| 105300 | 105641 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
| | @@ -105335,11 +105676,11 @@ |
| 105335 | 105676 | */ |
| 105336 | 105677 | notReady = ~(Bitmask)0; |
| 105337 | 105678 | for(i=0; i<nTabList; i++){ |
| 105338 | 105679 | pLevel = &pWInfo->a[i]; |
| 105339 | 105680 | explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags); |
| 105340 | | - notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady); |
| 105681 | + notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady, pWhere); |
| 105341 | 105682 | pWInfo->iContinue = pLevel->addrCont; |
| 105342 | 105683 | } |
| 105343 | 105684 | |
| 105344 | 105685 | #ifdef SQLITE_TEST /* For testing and debugging use only */ |
| 105345 | 105686 | /* Record in the query plan information about the current table |
| | @@ -105470,11 +105811,11 @@ |
| 105470 | 105811 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 105471 | 105812 | Table *pTab = pTabItem->pTab; |
| 105472 | 105813 | assert( pTab!=0 ); |
| 105473 | 105814 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 105474 | 105815 | && pTab->pSelect==0 |
| 105475 | | - && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0 |
| 105816 | + && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
| 105476 | 105817 | ){ |
| 105477 | 105818 | int ws = pLevel->plan.wsFlags; |
| 105478 | 105819 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
| 105479 | 105820 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 105480 | 105821 | } |
| | @@ -108817,11 +109158,13 @@ |
| 108817 | 109158 | sqlite3ParserTOKENTYPE yyminor /* The value for the token */ |
| 108818 | 109159 | sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */ |
| 108819 | 109160 | ){ |
| 108820 | 109161 | YYMINORTYPE yyminorunion; |
| 108821 | 109162 | int yyact; /* The parser action. */ |
| 109163 | +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) |
| 108822 | 109164 | int yyendofinput; /* True if we are at the end of input */ |
| 109165 | +#endif |
| 108823 | 109166 | #ifdef YYERRORSYMBOL |
| 108824 | 109167 | int yyerrorhit = 0; /* True if yymajor has invoked an error */ |
| 108825 | 109168 | #endif |
| 108826 | 109169 | yyParser *yypParser; /* The parser */ |
| 108827 | 109170 | |
| | @@ -108840,11 +109183,13 @@ |
| 108840 | 109183 | yypParser->yyerrcnt = -1; |
| 108841 | 109184 | yypParser->yystack[0].stateno = 0; |
| 108842 | 109185 | yypParser->yystack[0].major = 0; |
| 108843 | 109186 | } |
| 108844 | 109187 | yyminorunion.yy0 = yyminor; |
| 109188 | +#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) |
| 108845 | 109189 | yyendofinput = (yymajor==0); |
| 109190 | +#endif |
| 108846 | 109191 | sqlite3ParserARG_STORE; |
| 108847 | 109192 | |
| 108848 | 109193 | #ifndef NDEBUG |
| 108849 | 109194 | if( yyTraceFILE ){ |
| 108850 | 109195 | fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]); |
| | @@ -108852,11 +109197,10 @@ |
| 108852 | 109197 | #endif |
| 108853 | 109198 | |
| 108854 | 109199 | do{ |
| 108855 | 109200 | yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); |
| 108856 | 109201 | if( yyact<YYNSTATE ){ |
| 108857 | | - assert( !yyendofinput ); /* Impossible to shift the $ token */ |
| 108858 | 109202 | yy_shift(yypParser,yyact,yymajor,&yyminorunion); |
| 108859 | 109203 | yypParser->yyerrcnt--; |
| 108860 | 109204 | yymajor = YYNOCODE; |
| 108861 | 109205 | }else if( yyact < YYNSTATE + YYNRULE ){ |
| 108862 | 109206 | yy_reduce(yypParser,yyact-YYNSTATE); |
| | @@ -110244,11 +110588,11 @@ |
| 110244 | 110588 | ** |
| 110245 | 110589 | ** * Recursive calls to this routine from thread X return immediately |
| 110246 | 110590 | ** without blocking. |
| 110247 | 110591 | */ |
| 110248 | 110592 | SQLITE_API int sqlite3_initialize(void){ |
| 110249 | | - sqlite3_mutex *pMaster; /* The main static mutex */ |
| 110593 | + MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ |
| 110250 | 110594 | int rc; /* Result code */ |
| 110251 | 110595 | |
| 110252 | 110596 | #ifdef SQLITE_OMIT_WSD |
| 110253 | 110597 | rc = sqlite3_wsd_init(4096, 24); |
| 110254 | 110598 | if( rc!=SQLITE_OK ){ |
| | @@ -110278,11 +110622,11 @@ |
| 110278 | 110622 | ** This operation is protected by the STATIC_MASTER mutex. Note that |
| 110279 | 110623 | ** MutexAlloc() is called for a static mutex prior to initializing the |
| 110280 | 110624 | ** malloc subsystem - this implies that the allocation of a static |
| 110281 | 110625 | ** mutex must not require support from the malloc subsystem. |
| 110282 | 110626 | */ |
| 110283 | | - pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 110627 | + MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 110284 | 110628 | sqlite3_mutex_enter(pMaster); |
| 110285 | 110629 | sqlite3GlobalConfig.isMutexInit = 1; |
| 110286 | 110630 | if( !sqlite3GlobalConfig.isMallocInit ){ |
| 110287 | 110631 | rc = sqlite3MallocInit(); |
| 110288 | 110632 | } |
| | @@ -111352,17 +111696,17 @@ |
| 111352 | 111696 | sqlite3 *db, |
| 111353 | 111697 | const char *zName, |
| 111354 | 111698 | int nArg |
| 111355 | 111699 | ){ |
| 111356 | 111700 | int nName = sqlite3Strlen30(zName); |
| 111357 | | - int rc; |
| 111701 | + int rc = SQLITE_OK; |
| 111358 | 111702 | sqlite3_mutex_enter(db->mutex); |
| 111359 | 111703 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 111360 | | - sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 111361 | | - 0, sqlite3InvalidFunction, 0, 0, 0); |
| 111704 | + rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 111705 | + 0, sqlite3InvalidFunction, 0, 0, 0); |
| 111362 | 111706 | } |
| 111363 | | - rc = sqlite3ApiExit(db, SQLITE_OK); |
| 111707 | + rc = sqlite3ApiExit(db, rc); |
| 111364 | 111708 | sqlite3_mutex_leave(db->mutex); |
| 111365 | 111709 | return rc; |
| 111366 | 111710 | } |
| 111367 | 111711 | |
| 111368 | 111712 | #ifndef SQLITE_OMIT_TRACE |
| | @@ -112420,10 +112764,11 @@ |
| 112420 | 112764 | if( db ){ |
| 112421 | 112765 | assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 ); |
| 112422 | 112766 | sqlite3_mutex_leave(db->mutex); |
| 112423 | 112767 | } |
| 112424 | 112768 | rc = sqlite3_errcode(db); |
| 112769 | + assert( db!=0 || rc==SQLITE_NOMEM ); |
| 112425 | 112770 | if( rc==SQLITE_NOMEM ){ |
| 112426 | 112771 | sqlite3_close(db); |
| 112427 | 112772 | db = 0; |
| 112428 | 112773 | }else if( rc!=SQLITE_OK ){ |
| 112429 | 112774 | db->magic = SQLITE_MAGIC_SICK; |
| | @@ -114148,10 +114493,17 @@ |
| 114148 | 114493 | #else |
| 114149 | 114494 | # define TESTONLY(X) |
| 114150 | 114495 | #endif |
| 114151 | 114496 | |
| 114152 | 114497 | #endif /* SQLITE_AMALGAMATION */ |
| 114498 | + |
| 114499 | +#ifdef SQLITE_DEBUG |
| 114500 | +SQLITE_PRIVATE int sqlite3Fts3Corrupt(void); |
| 114501 | +# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt() |
| 114502 | +#else |
| 114503 | +# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB |
| 114504 | +#endif |
| 114153 | 114505 | |
| 114154 | 114506 | typedef struct Fts3Table Fts3Table; |
| 114155 | 114507 | typedef struct Fts3Cursor Fts3Cursor; |
| 114156 | 114508 | typedef struct Fts3Expr Fts3Expr; |
| 114157 | 114509 | typedef struct Fts3Phrase Fts3Phrase; |
| | @@ -114649,11 +115001,11 @@ |
| 114649 | 115001 | char **pp, |
| 114650 | 115002 | char *pStart, |
| 114651 | 115003 | sqlite3_int64 *pVal |
| 114652 | 115004 | ){ |
| 114653 | 115005 | sqlite3_int64 iVal; |
| 114654 | | - char *p = *pp; |
| 115006 | + char *p; |
| 114655 | 115007 | |
| 114656 | 115008 | /* Pointer p now points at the first byte past the varint we are |
| 114657 | 115009 | ** interested in. So, unless the doclist is corrupt, the 0x80 bit is |
| 114658 | 115010 | ** clear on character p[-1]. */ |
| 114659 | 115011 | for(p = (*pp)-2; p>=pStart && *p&0x80; p--); |
| | @@ -115050,11 +115402,11 @@ |
| 115050 | 115402 | ** the output value undefined. Otherwise SQLITE_OK is returned. |
| 115051 | 115403 | ** |
| 115052 | 115404 | ** This function is used when parsing the "prefix=" FTS4 parameter. |
| 115053 | 115405 | */ |
| 115054 | 115406 | static int fts3GobbleInt(const char **pp, int *pnOut){ |
| 115055 | | - const char *p = *pp; /* Iterator pointer */ |
| 115407 | + const char *p; /* Iterator pointer */ |
| 115056 | 115408 | int nInt = 0; /* Output value */ |
| 115057 | 115409 | |
| 115058 | 115410 | for(p=*pp; p[0]>='0' && p[0]<='9'; p++){ |
| 115059 | 115411 | nInt = nInt * 10 + (p[0] - '0'); |
| 115060 | 115412 | } |
| | @@ -115549,11 +115901,11 @@ |
| 115549 | 115901 | if( rc==SQLITE_OK ){ |
| 115550 | 115902 | /* If no row was found and no error has occured, then the %_content |
| 115551 | 115903 | ** table is missing a row that is present in the full-text index. |
| 115552 | 115904 | ** The data structures are corrupt. |
| 115553 | 115905 | */ |
| 115554 | | - rc = SQLITE_CORRUPT_VTAB; |
| 115906 | + rc = FTS_CORRUPT_VTAB; |
| 115555 | 115907 | } |
| 115556 | 115908 | pCsr->isEof = 1; |
| 115557 | 115909 | if( pContext ){ |
| 115558 | 115910 | sqlite3_result_error_code(pContext, rc); |
| 115559 | 115911 | } |
| | @@ -115609,11 +115961,11 @@ |
| 115609 | 115961 | ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). |
| 115610 | 115962 | */ |
| 115611 | 115963 | zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); |
| 115612 | 115964 | zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); |
| 115613 | 115965 | if( zCsr>zEnd ){ |
| 115614 | | - return SQLITE_CORRUPT_VTAB; |
| 115966 | + return FTS_CORRUPT_VTAB; |
| 115615 | 115967 | } |
| 115616 | 115968 | |
| 115617 | 115969 | while( zCsr<zEnd && (piFirst || piLast) ){ |
| 115618 | 115970 | int cmp; /* memcmp() result */ |
| 115619 | 115971 | int nSuffix; /* Size of term suffix */ |
| | @@ -115627,11 +115979,11 @@ |
| 115627 | 115979 | } |
| 115628 | 115980 | isFirstTerm = 0; |
| 115629 | 115981 | zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix); |
| 115630 | 115982 | |
| 115631 | 115983 | if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){ |
| 115632 | | - rc = SQLITE_CORRUPT_VTAB; |
| 115984 | + rc = FTS_CORRUPT_VTAB; |
| 115633 | 115985 | goto finish_scan; |
| 115634 | 115986 | } |
| 115635 | 115987 | if( nPrefix+nSuffix>nAlloc ){ |
| 115636 | 115988 | char *zNew; |
| 115637 | 115989 | nAlloc = (nPrefix+nSuffix) * 2; |
| | @@ -115640,10 +115992,11 @@ |
| 115640 | 115992 | rc = SQLITE_NOMEM; |
| 115641 | 115993 | goto finish_scan; |
| 115642 | 115994 | } |
| 115643 | 115995 | zBuffer = zNew; |
| 115644 | 115996 | } |
| 115997 | + assert( zBuffer ); |
| 115645 | 115998 | memcpy(&zBuffer[nPrefix], zCsr, nSuffix); |
| 115646 | 115999 | nBuffer = nPrefix + nSuffix; |
| 115647 | 116000 | zCsr += nSuffix; |
| 115648 | 116001 | |
| 115649 | 116002 | /* Compare the term we are searching for with the term just loaded from |
| | @@ -117076,11 +117429,11 @@ |
| 117076 | 117429 | ** moves *ppPoslist so that it instead points to the first byte of the |
| 117077 | 117430 | ** same position list. |
| 117078 | 117431 | */ |
| 117079 | 117432 | static void fts3ReversePoslist(char *pStart, char **ppPoslist){ |
| 117080 | 117433 | char *p = &(*ppPoslist)[-2]; |
| 117081 | | - char c; |
| 117434 | + char c = 0; |
| 117082 | 117435 | |
| 117083 | 117436 | while( p>pStart && (c=*p--)==0 ); |
| 117084 | 117437 | while( p>pStart && (*p & 0x80) | c ){ |
| 117085 | 117438 | c = *p--; |
| 117086 | 117439 | } |
| | @@ -118070,11 +118423,11 @@ |
| 118070 | 118423 | while( a<pEnd ){ |
| 118071 | 118424 | a += sqlite3Fts3GetVarint(a, &nByte); |
| 118072 | 118425 | } |
| 118073 | 118426 | if( nDoc==0 || nByte==0 ){ |
| 118074 | 118427 | sqlite3_reset(pStmt); |
| 118075 | | - return SQLITE_CORRUPT_VTAB; |
| 118428 | + return FTS_CORRUPT_VTAB; |
| 118076 | 118429 | } |
| 118077 | 118430 | |
| 118078 | 118431 | pCsr->nDoc = nDoc; |
| 118079 | 118432 | pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz); |
| 118080 | 118433 | assert( pCsr->nRowAvg>0 ); |
| | @@ -118546,12 +118899,15 @@ |
| 118546 | 118899 | } |
| 118547 | 118900 | |
| 118548 | 118901 | aPoslist = pExpr->pRight->pPhrase->doclist.pList; |
| 118549 | 118902 | nToken = pExpr->pRight->pPhrase->nToken; |
| 118550 | 118903 | for(p=pExpr->pLeft; p && res; p=p->pLeft){ |
| 118551 | | - int nNear = p->pParent->nNear; |
| 118552 | | - Fts3Phrase *pPhrase = ( |
| 118904 | + int nNear; |
| 118905 | + Fts3Phrase *pPhrase; |
| 118906 | + assert( p->pParent && p->pParent->pLeft==p ); |
| 118907 | + nNear = p->pParent->nNear; |
| 118908 | + pPhrase = ( |
| 118553 | 118909 | p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase |
| 118554 | 118910 | ); |
| 118555 | 118911 | res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); |
| 118556 | 118912 | } |
| 118557 | 118913 | } |
| | @@ -119037,10 +119393,19 @@ |
| 119037 | 119393 | pPhrase->aToken[i].pSegcsr = 0; |
| 119038 | 119394 | } |
| 119039 | 119395 | } |
| 119040 | 119396 | } |
| 119041 | 119397 | |
| 119398 | +/* |
| 119399 | +** Return SQLITE_CORRUPT_VTAB. |
| 119400 | +*/ |
| 119401 | +#ifdef SQLITE_DEBUG |
| 119402 | +SQLITE_PRIVATE int sqlite3Fts3Corrupt(){ |
| 119403 | + return SQLITE_CORRUPT_VTAB; |
| 119404 | +} |
| 119405 | +#endif |
| 119406 | + |
| 119042 | 119407 | #if !SQLITE_CORE |
| 119043 | 119408 | /* |
| 119044 | 119409 | ** Initialize API pointer table, if required. |
| 119045 | 119410 | */ |
| 119046 | 119411 | SQLITE_API int sqlite3_extension_init( |
| | @@ -119834,12 +120199,16 @@ |
| 119834 | 120199 | p->pPhrase = (Fts3Phrase *)&p[1]; |
| 119835 | 120200 | p->pPhrase->iColumn = pParse->iDefaultCol; |
| 119836 | 120201 | p->pPhrase->nToken = nToken; |
| 119837 | 120202 | |
| 119838 | 120203 | zBuf = (char *)&p->pPhrase->aToken[nToken]; |
| 119839 | | - memcpy(zBuf, zTemp, nTemp); |
| 119840 | | - sqlite3_free(zTemp); |
| 120204 | + if( zTemp ){ |
| 120205 | + memcpy(zBuf, zTemp, nTemp); |
| 120206 | + sqlite3_free(zTemp); |
| 120207 | + }else{ |
| 120208 | + assert( nTemp==0 ); |
| 120209 | + } |
| 119841 | 120210 | |
| 119842 | 120211 | for(jj=0; jj<p->pPhrase->nToken; jj++){ |
| 119843 | 120212 | p->pPhrase->aToken[jj].z = zBuf; |
| 119844 | 120213 | zBuf += p->pPhrase->aToken[jj].n; |
| 119845 | 120214 | } |
| | @@ -122594,11 +122963,11 @@ |
| 122594 | 122963 | sqlite3_bind_int64(pStmt, 1, iDocid); |
| 122595 | 122964 | } |
| 122596 | 122965 | rc = sqlite3_step(pStmt); |
| 122597 | 122966 | if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){ |
| 122598 | 122967 | rc = sqlite3_reset(pStmt); |
| 122599 | | - if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT_VTAB; |
| 122968 | + if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB; |
| 122600 | 122969 | pStmt = 0; |
| 122601 | 122970 | }else{ |
| 122602 | 122971 | rc = SQLITE_OK; |
| 122603 | 122972 | } |
| 122604 | 122973 | } |
| | @@ -123398,11 +123767,11 @@ |
| 123398 | 123767 | pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix); |
| 123399 | 123768 | pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix); |
| 123400 | 123769 | if( nPrefix<0 || nSuffix<=0 |
| 123401 | 123770 | || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] |
| 123402 | 123771 | ){ |
| 123403 | | - return SQLITE_CORRUPT_VTAB; |
| 123772 | + return FTS_CORRUPT_VTAB; |
| 123404 | 123773 | } |
| 123405 | 123774 | |
| 123406 | 123775 | if( nPrefix+nSuffix>pReader->nTermAlloc ){ |
| 123407 | 123776 | int nNew = (nPrefix+nSuffix)*2; |
| 123408 | 123777 | char *zNew = sqlite3_realloc(pReader->zTerm, nNew); |
| | @@ -123428,11 +123797,11 @@ |
| 123428 | 123797 | ** of these statements is untrue, then the data structure is corrupt. |
| 123429 | 123798 | */ |
| 123430 | 123799 | if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] |
| 123431 | 123800 | || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1]) |
| 123432 | 123801 | ){ |
| 123433 | | - return SQLITE_CORRUPT_VTAB; |
| 123802 | + return FTS_CORRUPT_VTAB; |
| 123434 | 123803 | } |
| 123435 | 123804 | return SQLITE_OK; |
| 123436 | 123805 | } |
| 123437 | 123806 | |
| 123438 | 123807 | /* |
| | @@ -125382,11 +125751,10 @@ |
| 125382 | 125751 | sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ |
| 125383 | 125752 | ){ |
| 125384 | 125753 | Fts3Table *p = (Fts3Table *)pVtab; |
| 125385 | 125754 | int rc = SQLITE_OK; /* Return Code */ |
| 125386 | 125755 | int isRemove = 0; /* True for an UPDATE or DELETE */ |
| 125387 | | - sqlite3_int64 iRemove = 0; /* Rowid removed by UPDATE or DELETE */ |
| 125388 | 125756 | u32 *aSzIns = 0; /* Sizes of inserted documents */ |
| 125389 | 125757 | u32 *aSzDel; /* Sizes of deleted documents */ |
| 125390 | 125758 | int nChng = 0; /* Net change in number of documents */ |
| 125391 | 125759 | int bInsertDone = 0; |
| 125392 | 125760 | |
| | @@ -125465,23 +125833,23 @@ |
| 125465 | 125833 | /* If this is a DELETE or UPDATE operation, remove the old record. */ |
| 125466 | 125834 | if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ |
| 125467 | 125835 | assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER ); |
| 125468 | 125836 | rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel); |
| 125469 | 125837 | isRemove = 1; |
| 125470 | | - iRemove = sqlite3_value_int64(apVal[0]); |
| 125471 | 125838 | } |
| 125472 | 125839 | |
| 125473 | 125840 | /* If this is an INSERT or UPDATE operation, insert the new record. */ |
| 125474 | 125841 | if( nArg>1 && rc==SQLITE_OK ){ |
| 125475 | 125842 | if( bInsertDone==0 ){ |
| 125476 | 125843 | rc = fts3InsertData(p, apVal, pRowid); |
| 125477 | | - if( rc==SQLITE_CONSTRAINT ) rc = SQLITE_CORRUPT_VTAB; |
| 125844 | + if( rc==SQLITE_CONSTRAINT ) rc = FTS_CORRUPT_VTAB; |
| 125478 | 125845 | } |
| 125479 | | - if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){ |
| 125846 | + if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){ |
| 125480 | 125847 | rc = fts3PendingTermsDocid(p, *pRowid); |
| 125481 | 125848 | } |
| 125482 | 125849 | if( rc==SQLITE_OK ){ |
| 125850 | + assert( p->iPrevDocid==*pRowid ); |
| 125483 | 125851 | rc = fts3InsertTerms(p, apVal, aSzIns); |
| 125484 | 125852 | } |
| 125485 | 125853 | if( p->bHasDocsize ){ |
| 125486 | 125854 | fts3InsertDocsize(&rc, p, aSzIns); |
| 125487 | 125855 | } |
| | @@ -126371,11 +126739,11 @@ |
| 126371 | 126739 | pStmt = *ppStmt; |
| 126372 | 126740 | assert( sqlite3_data_count(pStmt)==1 ); |
| 126373 | 126741 | |
| 126374 | 126742 | a = sqlite3_column_blob(pStmt, 0); |
| 126375 | 126743 | a += sqlite3Fts3GetVarint(a, &nDoc); |
| 126376 | | - if( nDoc==0 ) return SQLITE_CORRUPT_VTAB; |
| 126744 | + if( nDoc==0 ) return FTS_CORRUPT_VTAB; |
| 126377 | 126745 | *pnDoc = (u32)nDoc; |
| 126378 | 126746 | |
| 126379 | 126747 | if( paLen ) *paLen = a; |
| 126380 | 126748 | return SQLITE_OK; |
| 126381 | 126749 | } |
| | @@ -126950,11 +127318,11 @@ |
| 126950 | 127318 | sqlite3_snprintf(sizeof(aBuffer), aBuffer, |
| 126951 | 127319 | "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart |
| 126952 | 127320 | ); |
| 126953 | 127321 | rc = fts3StringAppend(&res, aBuffer, -1); |
| 126954 | 127322 | }else if( rc==SQLITE_DONE ){ |
| 126955 | | - rc = SQLITE_CORRUPT_VTAB; |
| 127323 | + rc = FTS_CORRUPT_VTAB; |
| 126956 | 127324 | } |
| 126957 | 127325 | } |
| 126958 | 127326 | } |
| 126959 | 127327 | if( rc==SQLITE_DONE ){ |
| 126960 | 127328 | rc = SQLITE_OK; |
| | @@ -128291,11 +128659,12 @@ |
| 128291 | 128659 | pCsr->nConstraint = argc; |
| 128292 | 128660 | if( !pCsr->aConstraint ){ |
| 128293 | 128661 | rc = SQLITE_NOMEM; |
| 128294 | 128662 | }else{ |
| 128295 | 128663 | memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc); |
| 128296 | | - assert( (idxStr==0 && argc==0) || (int)strlen(idxStr)==argc*2 ); |
| 128664 | + assert( (idxStr==0 && argc==0) |
| 128665 | + || (idxStr && (int)strlen(idxStr)==argc*2) ); |
| 128297 | 128666 | for(ii=0; ii<argc; ii++){ |
| 128298 | 128667 | RtreeConstraint *p = &pCsr->aConstraint[ii]; |
| 128299 | 128668 | p->op = idxStr[ii*2]; |
| 128300 | 128669 | p->iCoord = idxStr[ii*2+1]-'a'; |
| 128301 | 128670 | if( p->op==RTREE_MATCH ){ |
| | @@ -128592,11 +128961,14 @@ |
| 128592 | 128961 | int iCell; |
| 128593 | 128962 | sqlite3_int64 iBest = 0; |
| 128594 | 128963 | |
| 128595 | 128964 | float fMinGrowth = 0.0; |
| 128596 | 128965 | float fMinArea = 0.0; |
| 128966 | +#if VARIANT_RSTARTREE_CHOOSESUBTREE |
| 128597 | 128967 | float fMinOverlap = 0.0; |
| 128968 | + float overlap; |
| 128969 | +#endif |
| 128598 | 128970 | |
| 128599 | 128971 | int nCell = NCELL(pNode); |
| 128600 | 128972 | RtreeCell cell; |
| 128601 | 128973 | RtreeNode *pChild; |
| 128602 | 128974 | |
| | @@ -128624,33 +128996,34 @@ |
| 128624 | 128996 | */ |
| 128625 | 128997 | for(iCell=0; iCell<nCell; iCell++){ |
| 128626 | 128998 | int bBest = 0; |
| 128627 | 128999 | float growth; |
| 128628 | 129000 | float area; |
| 128629 | | - float overlap = 0.0; |
| 128630 | 129001 | nodeGetCell(pRtree, pNode, iCell, &cell); |
| 128631 | 129002 | growth = cellGrowth(pRtree, &cell, pCell); |
| 128632 | 129003 | area = cellArea(pRtree, &cell); |
| 128633 | 129004 | |
| 128634 | 129005 | #if VARIANT_RSTARTREE_CHOOSESUBTREE |
| 128635 | 129006 | if( ii==(pRtree->iDepth-1) ){ |
| 128636 | 129007 | overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell); |
| 129008 | + }else{ |
| 129009 | + overlap = 0.0; |
| 128637 | 129010 | } |
| 128638 | 129011 | if( (iCell==0) |
| 128639 | 129012 | || (overlap<fMinOverlap) |
| 128640 | 129013 | || (overlap==fMinOverlap && growth<fMinGrowth) |
| 128641 | 129014 | || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea) |
| 128642 | 129015 | ){ |
| 128643 | 129016 | bBest = 1; |
| 129017 | + fMinOverlap = overlap; |
| 128644 | 129018 | } |
| 128645 | 129019 | #else |
| 128646 | 129020 | if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){ |
| 128647 | 129021 | bBest = 1; |
| 128648 | 129022 | } |
| 128649 | 129023 | #endif |
| 128650 | 129024 | if( bBest ){ |
| 128651 | | - fMinOverlap = overlap; |
| 128652 | 129025 | fMinGrowth = growth; |
| 128653 | 129026 | fMinArea = area; |
| 128654 | 129027 | iBest = cell.iRowid; |
| 128655 | 129028 | } |
| 128656 | 129029 | } |
| 128657 | 129030 | |