| | @@ -385,23 +385,29 @@ |
| 385 | 385 | /* |
| 386 | 386 | ** Exactly one of the following macros must be defined in order to |
| 387 | 387 | ** specify which memory allocation subsystem to use. |
| 388 | 388 | ** |
| 389 | 389 | ** SQLITE_SYSTEM_MALLOC // Use normal system malloc() |
| 390 | +** SQLITE_WIN32_MALLOC // Use Win32 native heap API |
| 390 | 391 | ** SQLITE_MEMDEBUG // Debugging version of system malloc() |
| 392 | +** |
| 393 | +** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the |
| 394 | +** assert() macro is enabled, each call into the Win32 native heap subsystem |
| 395 | +** will cause HeapValidate to be called. If heap validation should fail, an |
| 396 | +** assertion will be triggered. |
| 391 | 397 | ** |
| 392 | 398 | ** (Historical note: There used to be several other options, but we've |
| 393 | 399 | ** pared it down to just these two.) |
| 394 | 400 | ** |
| 395 | 401 | ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as |
| 396 | 402 | ** the default. |
| 397 | 403 | */ |
| 398 | | -#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1 |
| 404 | +#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1 |
| 399 | 405 | # error "At most one of the following compile-time configuration options\ |
| 400 | | - is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG" |
| 406 | + is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG" |
| 401 | 407 | #endif |
| 402 | | -#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0 |
| 408 | +#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0 |
| 403 | 409 | # define SQLITE_SYSTEM_MALLOC 1 |
| 404 | 410 | #endif |
| 405 | 411 | |
| 406 | 412 | /* |
| 407 | 413 | ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the |
| | @@ -650,11 +656,11 @@ |
| 650 | 656 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 651 | 657 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 652 | 658 | */ |
| 653 | 659 | #define SQLITE_VERSION "3.7.8" |
| 654 | 660 | #define SQLITE_VERSION_NUMBER 3007008 |
| 655 | | -#define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957" |
| 661 | +#define SQLITE_SOURCE_ID "2011-08-29 11:56:14 639cc85a911454bffdcccb33f2976c683953ae64" |
| 656 | 662 | |
| 657 | 663 | /* |
| 658 | 664 | ** CAPI3REF: Run-Time Library Version Numbers |
| 659 | 665 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 660 | 666 | ** |
| | @@ -1751,20 +1757,14 @@ |
| 1751 | 1757 | ** also used during testing of SQLite in order to specify an alternative |
| 1752 | 1758 | ** memory allocator that simulates memory out-of-memory conditions in |
| 1753 | 1759 | ** order to verify that SQLite recovers gracefully from such |
| 1754 | 1760 | ** conditions. |
| 1755 | 1761 | ** |
| 1756 | | -** The xMalloc and xFree methods must work like the |
| 1757 | | -** malloc() and free() functions from the standard C library. |
| 1758 | | -** The xRealloc method must work like realloc() from the standard C library |
| 1759 | | -** with the exception that if the second argument to xRealloc is zero, |
| 1760 | | -** xRealloc must be a no-op - it must not perform any allocation or |
| 1761 | | -** deallocation. ^SQLite guarantees that the second argument to |
| 1762 | +** The xMalloc, xRealloc, and xFree methods must work like the |
| 1763 | +** malloc(), realloc() and free() functions from the standard C library. |
| 1764 | +** ^SQLite guarantees that the second argument to |
| 1762 | 1765 | ** xRealloc is always a value returned by a prior call to xRoundup. |
| 1763 | | -** And so in cases where xRoundup always returns a positive number, |
| 1764 | | -** xRealloc can perform exactly as the standard library realloc() and |
| 1765 | | -** still be in compliance with this specification. |
| 1766 | 1766 | ** |
| 1767 | 1767 | ** xSize should return the allocated size of a memory allocation |
| 1768 | 1768 | ** previously obtained from xMalloc or xRealloc. The allocated size |
| 1769 | 1769 | ** is always at least as big as the requested size but may be larger. |
| 1770 | 1770 | ** |
| | @@ -3397,11 +3397,11 @@ |
| 3397 | 3397 | ** a schema change, on the first [sqlite3_step()] call following any change |
| 3398 | 3398 | ** to the [sqlite3_bind_text | bindings] of that [parameter]. |
| 3399 | 3399 | ** ^The specific value of WHERE-clause [parameter] might influence the |
| 3400 | 3400 | ** choice of query plan if the parameter is the left-hand side of a [LIKE] |
| 3401 | 3401 | ** or [GLOB] operator or if the parameter is compared to an indexed column |
| 3402 | | -** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled. |
| 3402 | +** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled. |
| 3403 | 3403 | ** the |
| 3404 | 3404 | ** </li> |
| 3405 | 3405 | ** </ol> |
| 3406 | 3406 | */ |
| 3407 | 3407 | SQLITE_API int sqlite3_prepare( |
| | @@ -7632,10 +7632,18 @@ |
| 7632 | 7632 | */ |
| 7633 | 7633 | #ifndef SQLITE_TEMP_STORE |
| 7634 | 7634 | # define SQLITE_TEMP_STORE 1 |
| 7635 | 7635 | #endif |
| 7636 | 7636 | |
| 7637 | +/* |
| 7638 | +** If all temporary storage is in-memory, then omit the external merge-sort |
| 7639 | +** logic since it is superfluous. |
| 7640 | +*/ |
| 7641 | +#if SQLITE_TEMP_STORE==3 && !defined(SQLITE_OMIT_MERGE_SORT) |
| 7642 | +# define SQLITE_OMIT_MERGE_SORT |
| 7643 | +#endif |
| 7644 | + |
| 7637 | 7645 | /* |
| 7638 | 7646 | ** GCC does not define the offsetof() macro so we'll have to do it |
| 7639 | 7647 | ** ourselves. |
| 7640 | 7648 | */ |
| 7641 | 7649 | #ifndef offsetof |
| | @@ -7711,22 +7719,10 @@ |
| 7711 | 7719 | ** is 0x00000000ffffffff. But because of quirks of some compilers, we |
| 7712 | 7720 | ** have to specify the value in the less intuitive manner shown: |
| 7713 | 7721 | */ |
| 7714 | 7722 | #define SQLITE_MAX_U32 ((((u64)1)<<32)-1) |
| 7715 | 7723 | |
| 7716 | | -/* |
| 7717 | | -** The datatype used to store estimates of the number of rows in a |
| 7718 | | -** table or index. This is an unsigned integer type. For 99.9% of |
| 7719 | | -** the world, a 32-bit integer is sufficient. But a 64-bit integer |
| 7720 | | -** can be used at compile-time if desired. |
| 7721 | | -*/ |
| 7722 | | -#ifdef SQLITE_64BIT_STATS |
| 7723 | | - typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */ |
| 7724 | | -#else |
| 7725 | | - typedef u32 tRowcnt; /* 32-bit is the default */ |
| 7726 | | -#endif |
| 7727 | | - |
| 7728 | 7724 | /* |
| 7729 | 7725 | ** Macros to determine whether the machine is big or little endian, |
| 7730 | 7726 | ** evaluated at runtime. |
| 7731 | 7727 | */ |
| 7732 | 7728 | #ifdef SQLITE_AMALGAMATION |
| | @@ -7986,10 +7982,11 @@ |
| 7986 | 7982 | #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */ |
| 7987 | 7983 | #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */ |
| 7988 | 7984 | #define BTREE_MEMORY 4 /* This is an in-memory DB */ |
| 7989 | 7985 | #define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */ |
| 7990 | 7986 | #define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */ |
| 7987 | +#define BTREE_SORTER 32 /* Used as accumulator in external merge sort */ |
| 7991 | 7988 | |
| 7992 | 7989 | SQLITE_PRIVATE int sqlite3BtreeClose(Btree*); |
| 7993 | 7990 | SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int); |
| 7994 | 7991 | SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int); |
| 7995 | 7992 | SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*); |
| | @@ -8188,10 +8185,11 @@ |
| 8188 | 8185 | ** or VDBE. The VDBE implements an abstract machine that runs a |
| 8189 | 8186 | ** simple program to access and modify the underlying database. |
| 8190 | 8187 | */ |
| 8191 | 8188 | #ifndef _SQLITE_VDBE_H_ |
| 8192 | 8189 | #define _SQLITE_VDBE_H_ |
| 8190 | +/* #include <stdio.h> */ |
| 8193 | 8191 | |
| 8194 | 8192 | /* |
| 8195 | 8193 | ** A single VDBE is an opaque structure named "Vdbe". Only routines |
| 8196 | 8194 | ** in the source file sqliteVdbe.c are allowed to see the insides |
| 8197 | 8195 | ** of this structure. |
| | @@ -8231,10 +8229,11 @@ |
| 8231 | 8229 | Mem *pMem; /* Used when p4type is P4_MEM */ |
| 8232 | 8230 | VTable *pVtab; /* Used when p4type is P4_VTAB */ |
| 8233 | 8231 | KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ |
| 8234 | 8232 | int *ai; /* Used when p4type is P4_INTARRAY */ |
| 8235 | 8233 | SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ |
| 8234 | + int (*xAdvance)(BtCursor *, int *); |
| 8236 | 8235 | } p4; |
| 8237 | 8236 | #ifdef SQLITE_DEBUG |
| 8238 | 8237 | char *zComment; /* Comment to improve readability */ |
| 8239 | 8238 | #endif |
| 8240 | 8239 | #ifdef VDBE_PROFILE |
| | @@ -8286,10 +8285,11 @@ |
| 8286 | 8285 | #define P4_REAL (-12) /* P4 is a 64-bit floating point value */ |
| 8287 | 8286 | #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ |
| 8288 | 8287 | #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ |
| 8289 | 8288 | #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ |
| 8290 | 8289 | #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ |
| 8290 | +#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */ |
| 8291 | 8291 | |
| 8292 | 8292 | /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure |
| 8293 | 8293 | ** is made. That copy is freed when the Vdbe is finalized. But if the |
| 8294 | 8294 | ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still |
| 8295 | 8295 | ** gets freed when the Vdbe is finalized so it still should be obtained |
| | @@ -8399,89 +8399,89 @@ |
| 8399 | 8399 | #define OP_ReadCookie 35 |
| 8400 | 8400 | #define OP_SetCookie 36 |
| 8401 | 8401 | #define OP_VerifyCookie 37 |
| 8402 | 8402 | #define OP_OpenRead 38 |
| 8403 | 8403 | #define OP_OpenWrite 39 |
| 8404 | | -#define OP_OpenAutoindex 40 |
| 8405 | | -#define OP_OpenEphemeral 41 |
| 8406 | | -#define OP_OpenPseudo 42 |
| 8407 | | -#define OP_Close 43 |
| 8408 | | -#define OP_SeekLt 44 |
| 8409 | | -#define OP_SeekLe 45 |
| 8410 | | -#define OP_SeekGe 46 |
| 8411 | | -#define OP_SeekGt 47 |
| 8412 | | -#define OP_Seek 48 |
| 8413 | | -#define OP_NotFound 49 |
| 8414 | | -#define OP_Found 50 |
| 8415 | | -#define OP_IsUnique 51 |
| 8416 | | -#define OP_NotExists 52 |
| 8417 | | -#define OP_Sequence 53 |
| 8418 | | -#define OP_NewRowid 54 |
| 8419 | | -#define OP_Insert 55 |
| 8420 | | -#define OP_InsertInt 56 |
| 8421 | | -#define OP_Delete 57 |
| 8422 | | -#define OP_ResetCount 58 |
| 8423 | | -#define OP_RowKey 59 |
| 8424 | | -#define OP_RowData 60 |
| 8425 | | -#define OP_Rowid 61 |
| 8426 | | -#define OP_NullRow 62 |
| 8427 | | -#define OP_Last 63 |
| 8428 | | -#define OP_Sort 64 |
| 8429 | | -#define OP_Rewind 65 |
| 8430 | | -#define OP_Prev 66 |
| 8431 | | -#define OP_Next 67 |
| 8432 | | -#define OP_IdxInsert 70 |
| 8433 | | -#define OP_IdxDelete 71 |
| 8434 | | -#define OP_IdxRowid 72 |
| 8435 | | -#define OP_IdxLT 81 |
| 8436 | | -#define OP_IdxGE 92 |
| 8437 | | -#define OP_Destroy 95 |
| 8438 | | -#define OP_Clear 96 |
| 8439 | | -#define OP_CreateIndex 97 |
| 8440 | | -#define OP_CreateTable 98 |
| 8441 | | -#define OP_ParseSchema 99 |
| 8442 | | -#define OP_LoadAnalysis 100 |
| 8443 | | -#define OP_DropTable 101 |
| 8444 | | -#define OP_DropIndex 102 |
| 8445 | | -#define OP_DropTrigger 103 |
| 8446 | | -#define OP_IntegrityCk 104 |
| 8447 | | -#define OP_RowSetAdd 105 |
| 8448 | | -#define OP_RowSetRead 106 |
| 8449 | | -#define OP_RowSetTest 107 |
| 8450 | | -#define OP_Program 108 |
| 8451 | | -#define OP_Param 109 |
| 8452 | | -#define OP_FkCounter 110 |
| 8453 | | -#define OP_FkIfZero 111 |
| 8454 | | -#define OP_MemMax 112 |
| 8455 | | -#define OP_IfPos 113 |
| 8456 | | -#define OP_IfNeg 114 |
| 8457 | | -#define OP_IfZero 115 |
| 8458 | | -#define OP_AggStep 116 |
| 8459 | | -#define OP_AggFinal 117 |
| 8460 | | -#define OP_Checkpoint 118 |
| 8461 | | -#define OP_JournalMode 119 |
| 8462 | | -#define OP_Vacuum 120 |
| 8463 | | -#define OP_IncrVacuum 121 |
| 8464 | | -#define OP_Expire 122 |
| 8465 | | -#define OP_TableLock 123 |
| 8466 | | -#define OP_VBegin 124 |
| 8467 | | -#define OP_VCreate 125 |
| 8468 | | -#define OP_VDestroy 126 |
| 8469 | | -#define OP_VOpen 127 |
| 8470 | | -#define OP_VFilter 128 |
| 8471 | | -#define OP_VColumn 129 |
| 8472 | | -#define OP_VNext 131 |
| 8473 | | -#define OP_VRename 132 |
| 8474 | | -#define OP_VUpdate 133 |
| 8475 | | -#define OP_Pagecount 134 |
| 8476 | | -#define OP_MaxPgcnt 135 |
| 8477 | | -#define OP_Trace 136 |
| 8478 | | -#define OP_Noop 137 |
| 8479 | | -#define OP_Explain 138 |
| 8480 | | - |
| 8481 | | -/* The following opcode values are never used */ |
| 8482 | | -#define OP_NotUsed_139 139 |
| 8404 | +#define OP_OpenSorter 40 |
| 8405 | +#define OP_OpenAutoindex 41 |
| 8406 | +#define OP_OpenEphemeral 42 |
| 8407 | +#define OP_OpenPseudo 43 |
| 8408 | +#define OP_Close 44 |
| 8409 | +#define OP_SeekLt 45 |
| 8410 | +#define OP_SeekLe 46 |
| 8411 | +#define OP_SeekGe 47 |
| 8412 | +#define OP_SeekGt 48 |
| 8413 | +#define OP_Seek 49 |
| 8414 | +#define OP_NotFound 50 |
| 8415 | +#define OP_Found 51 |
| 8416 | +#define OP_IsUnique 52 |
| 8417 | +#define OP_NotExists 53 |
| 8418 | +#define OP_Sequence 54 |
| 8419 | +#define OP_NewRowid 55 |
| 8420 | +#define OP_Insert 56 |
| 8421 | +#define OP_InsertInt 57 |
| 8422 | +#define OP_Delete 58 |
| 8423 | +#define OP_ResetCount 59 |
| 8424 | +#define OP_RowKey 60 |
| 8425 | +#define OP_RowData 61 |
| 8426 | +#define OP_Rowid 62 |
| 8427 | +#define OP_NullRow 63 |
| 8428 | +#define OP_Last 64 |
| 8429 | +#define OP_Sort 65 |
| 8430 | +#define OP_Rewind 66 |
| 8431 | +#define OP_Prev 67 |
| 8432 | +#define OP_Next 70 |
| 8433 | +#define OP_IdxInsert 71 |
| 8434 | +#define OP_IdxDelete 72 |
| 8435 | +#define OP_IdxRowid 81 |
| 8436 | +#define OP_IdxLT 92 |
| 8437 | +#define OP_IdxGE 95 |
| 8438 | +#define OP_Destroy 96 |
| 8439 | +#define OP_Clear 97 |
| 8440 | +#define OP_CreateIndex 98 |
| 8441 | +#define OP_CreateTable 99 |
| 8442 | +#define OP_ParseSchema 100 |
| 8443 | +#define OP_LoadAnalysis 101 |
| 8444 | +#define OP_DropTable 102 |
| 8445 | +#define OP_DropIndex 103 |
| 8446 | +#define OP_DropTrigger 104 |
| 8447 | +#define OP_IntegrityCk 105 |
| 8448 | +#define OP_RowSetAdd 106 |
| 8449 | +#define OP_RowSetRead 107 |
| 8450 | +#define OP_RowSetTest 108 |
| 8451 | +#define OP_Program 109 |
| 8452 | +#define OP_Param 110 |
| 8453 | +#define OP_FkCounter 111 |
| 8454 | +#define OP_FkIfZero 112 |
| 8455 | +#define OP_MemMax 113 |
| 8456 | +#define OP_IfPos 114 |
| 8457 | +#define OP_IfNeg 115 |
| 8458 | +#define OP_IfZero 116 |
| 8459 | +#define OP_AggStep 117 |
| 8460 | +#define OP_AggFinal 118 |
| 8461 | +#define OP_Checkpoint 119 |
| 8462 | +#define OP_JournalMode 120 |
| 8463 | +#define OP_Vacuum 121 |
| 8464 | +#define OP_IncrVacuum 122 |
| 8465 | +#define OP_Expire 123 |
| 8466 | +#define OP_TableLock 124 |
| 8467 | +#define OP_VBegin 125 |
| 8468 | +#define OP_VCreate 126 |
| 8469 | +#define OP_VDestroy 127 |
| 8470 | +#define OP_VOpen 128 |
| 8471 | +#define OP_VFilter 129 |
| 8472 | +#define OP_VColumn 131 |
| 8473 | +#define OP_VNext 132 |
| 8474 | +#define OP_VRename 133 |
| 8475 | +#define OP_VUpdate 134 |
| 8476 | +#define OP_Pagecount 135 |
| 8477 | +#define OP_MaxPgcnt 136 |
| 8478 | +#define OP_Trace 137 |
| 8479 | +#define OP_Noop 138 |
| 8480 | +#define OP_Explain 139 |
| 8481 | + |
| 8482 | +/* The following opcode values are never used */ |
| 8483 | 8483 | #define OP_NotUsed_140 140 |
| 8484 | 8484 | |
| 8485 | 8485 | |
| 8486 | 8486 | /* Properties such as "out2" or "jump" that are specified in |
| 8487 | 8487 | ** comments following the "case" for each opcode in the vdbe.c |
| | @@ -8498,23 +8498,23 @@ |
| 8498 | 8498 | /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\ |
| 8499 | 8499 | /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\ |
| 8500 | 8500 | /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\ |
| 8501 | 8501 | /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\ |
| 8502 | 8502 | /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\ |
| 8503 | | -/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\ |
| 8504 | | -/* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\ |
| 8505 | | -/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\ |
| 8506 | | -/* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\ |
| 8507 | | -/* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ |
| 8508 | | -/* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\ |
| 8509 | | -/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\ |
| 8510 | | -/* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 8511 | | -/* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\ |
| 8512 | | -/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\ |
| 8513 | | -/* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 8514 | | -/* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\ |
| 8515 | | -/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\ |
| 8503 | +/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11,\ |
| 8504 | +/* 48 */ 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02,\ |
| 8505 | +/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,\ |
| 8506 | +/* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x01, 0x08,\ |
| 8507 | +/* 72 */ 0x00, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ |
| 8508 | +/* 80 */ 0x15, 0x02, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\ |
| 8509 | +/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\ |
| 8510 | +/* 96 */ 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\ |
| 8511 | +/* 104 */ 0x00, 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00,\ |
| 8512 | +/* 112 */ 0x01, 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00,\ |
| 8513 | +/* 120 */ 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 8514 | +/* 128 */ 0x00, 0x01, 0x02, 0x00, 0x01, 0x00, 0x00, 0x02,\ |
| 8515 | +/* 136 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\ |
| 8516 | 8516 | /* 144 */ 0x04, 0x04,} |
| 8517 | 8517 | |
| 8518 | 8518 | /************** End of opcodes.h *********************************************/ |
| 8519 | 8519 | /************** Continuing where we left off in vdbe.h ***********************/ |
| 8520 | 8520 | |
| | @@ -8529,13 +8529,13 @@ |
| 8529 | 8529 | SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); |
| 8530 | 8530 | SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); |
| 8531 | 8531 | SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); |
| 8532 | 8532 | SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp); |
| 8533 | 8533 | SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*); |
| 8534 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1); |
| 8535 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2); |
| 8536 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3); |
| 8534 | +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); |
| 8535 | +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); |
| 8536 | +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3); |
| 8537 | 8537 | SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5); |
| 8538 | 8538 | SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr); |
| 8539 | 8539 | SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N); |
| 8540 | 8540 | SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); |
| 8541 | 8541 | SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int); |
| | @@ -8653,10 +8653,11 @@ |
| 8653 | 8653 | ** NOTE: These values must match the corresponding BTREE_ values in btree.h. |
| 8654 | 8654 | */ |
| 8655 | 8655 | #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */ |
| 8656 | 8656 | #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */ |
| 8657 | 8657 | #define PAGER_MEMORY 0x0004 /* In-memory database */ |
| 8658 | +#define PAGER_SORTER 0x0020 /* Accumulator in external merge sort */ |
| 8658 | 8659 | |
| 8659 | 8660 | /* |
| 8660 | 8661 | ** Valid values for the second argument to sqlite3PagerLockingMode(). |
| 8661 | 8662 | */ |
| 8662 | 8663 | #define PAGER_LOCKINGMODE_QUERY -1 |
| | @@ -8748,10 +8749,13 @@ |
| 8748 | 8749 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); |
| 8749 | 8750 | SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); |
| 8750 | 8751 | SQLITE_PRIVATE int sqlite3PagerNosync(Pager*); |
| 8751 | 8752 | SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); |
| 8752 | 8753 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); |
| 8754 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 8755 | +SQLITE_PRIVATE int sqlite3PagerUnderStress(Pager*); |
| 8756 | +#endif |
| 8753 | 8757 | |
| 8754 | 8758 | /* Functions used to truncate the database file. */ |
| 8755 | 8759 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 8756 | 8760 | |
| 8757 | 8761 | #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) |
| | @@ -9924,11 +9928,11 @@ |
| 9924 | 9928 | int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ |
| 9925 | 9929 | int nCol; /* Number of columns in this table */ |
| 9926 | 9930 | Column *aCol; /* Information about each column */ |
| 9927 | 9931 | Index *pIndex; /* List of SQL indexes on this table. */ |
| 9928 | 9932 | int tnum; /* Root BTree node for this table (see note above) */ |
| 9929 | | - tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 9933 | + unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 9930 | 9934 | Select *pSelect; /* NULL for tables. Points to definition if a view. */ |
| 9931 | 9935 | u16 nRef; /* Number of pointers to this Table */ |
| 9932 | 9936 | u8 tabFlags; /* Mask of TF_* values */ |
| 9933 | 9937 | u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ |
| 9934 | 9938 | FKey *pFKey; /* Linked list of all foreign keys in this table */ |
| | @@ -10123,43 +10127,35 @@ |
| 10123 | 10127 | */ |
| 10124 | 10128 | struct Index { |
| 10125 | 10129 | char *zName; /* Name of this index */ |
| 10126 | 10130 | int nColumn; /* Number of columns in the table used by this index */ |
| 10127 | 10131 | int *aiColumn; /* Which columns are used by this index. 1st is 0 */ |
| 10128 | | - tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ |
| 10132 | + unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ |
| 10129 | 10133 | Table *pTable; /* The SQL table being indexed */ |
| 10130 | 10134 | int tnum; /* Page containing root of this index in database file */ |
| 10131 | 10135 | u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 10132 | 10136 | u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ |
| 10133 | 10137 | u8 bUnordered; /* Use this index for == or IN queries only */ |
| 10134 | | - u8 nSample; /* Number of elements in aSample[] */ |
| 10135 | 10138 | char *zColAff; /* String defining the affinity of each column */ |
| 10136 | 10139 | Index *pNext; /* The next index associated with the same table */ |
| 10137 | 10140 | Schema *pSchema; /* Schema containing this index */ |
| 10138 | 10141 | u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */ |
| 10139 | 10142 | char **azColl; /* Array of collation sequence names for index */ |
| 10140 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 10141 | | - tRowcnt avgEq; /* Average nEq value for key values not in aSample */ |
| 10142 | | - IndexSample *aSample; /* Samples of the left-most key */ |
| 10143 | | -#endif |
| 10143 | + IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */ |
| 10144 | 10144 | }; |
| 10145 | 10145 | |
| 10146 | 10146 | /* |
| 10147 | 10147 | ** Each sample stored in the sqlite_stat2 table is represented in memory |
| 10148 | 10148 | ** using a structure of this type. |
| 10149 | 10149 | */ |
| 10150 | 10150 | struct IndexSample { |
| 10151 | 10151 | union { |
| 10152 | 10152 | char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */ |
| 10153 | | - double r; /* Value if eType is SQLITE_FLOAT */ |
| 10154 | | - i64 i; /* Value if eType is SQLITE_INTEGER */ |
| 10153 | + double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */ |
| 10155 | 10154 | } u; |
| 10156 | 10155 | u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */ |
| 10157 | | - u16 nByte; /* Size in byte of text or blob. */ |
| 10158 | | - tRowcnt nEq; /* Est. number of rows where the key equals this sample */ |
| 10159 | | - tRowcnt nLt; /* Est. number of rows where key is less than this sample */ |
| 10160 | | - tRowcnt nDLt; /* Est. number of distinct keys less than this sample */ |
| 10156 | + u8 nByte; /* Size in byte of text or blob. */ |
| 10161 | 10157 | }; |
| 10162 | 10158 | |
| 10163 | 10159 | /* |
| 10164 | 10160 | ** Each token coming out of the lexer is an instance of |
| 10165 | 10161 | ** this structure. Tokens are also used as part of an expression. |
| | @@ -11361,11 +11357,10 @@ |
| 11361 | 11357 | #else |
| 11362 | 11358 | # define sqlite3ViewGetColumnNames(A,B) 0 |
| 11363 | 11359 | #endif |
| 11364 | 11360 | |
| 11365 | 11361 | SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int); |
| 11366 | | -SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int); |
| 11367 | 11362 | SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*); |
| 11368 | 11363 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 11369 | 11364 | SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse); |
| 11370 | 11365 | SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse); |
| 11371 | 11366 | #else |
| | @@ -11618,11 +11613,11 @@ |
| 11618 | 11613 | SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, |
| 11619 | 11614 | void(*)(void*)); |
| 11620 | 11615 | SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*); |
| 11621 | 11616 | SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *); |
| 11622 | 11617 | SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8); |
| 11623 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 11618 | +#ifdef SQLITE_ENABLE_STAT2 |
| 11624 | 11619 | SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *); |
| 11625 | 11620 | #endif |
| 11626 | 11621 | SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **); |
| 11627 | 11622 | SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8); |
| 11628 | 11623 | #ifndef SQLITE_AMALGAMATION |
| | @@ -12245,13 +12240,10 @@ |
| 12245 | 12240 | "ENABLE_RTREE", |
| 12246 | 12241 | #endif |
| 12247 | 12242 | #ifdef SQLITE_ENABLE_STAT2 |
| 12248 | 12243 | "ENABLE_STAT2", |
| 12249 | 12244 | #endif |
| 12250 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 12251 | | - "ENABLE_STAT3", |
| 12252 | | -#endif |
| 12253 | 12245 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 12254 | 12246 | "ENABLE_UNLOCK_NOTIFY", |
| 12255 | 12247 | #endif |
| 12256 | 12248 | #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
| 12257 | 12249 | "ENABLE_UPDATE_DELETE_LIMIT", |
| | @@ -12387,10 +12379,13 @@ |
| 12387 | 12379 | #ifdef SQLITE_OMIT_LOOKASIDE |
| 12388 | 12380 | "OMIT_LOOKASIDE", |
| 12389 | 12381 | #endif |
| 12390 | 12382 | #ifdef SQLITE_OMIT_MEMORYDB |
| 12391 | 12383 | "OMIT_MEMORYDB", |
| 12384 | +#endif |
| 12385 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 12386 | + "OMIT_MERGE_SORT", |
| 12392 | 12387 | #endif |
| 12393 | 12388 | #ifdef SQLITE_OMIT_OR_OPTIMIZATION |
| 12394 | 12389 | "OMIT_OR_OPTIMIZATION", |
| 12395 | 12390 | #endif |
| 12396 | 12391 | #ifdef SQLITE_OMIT_PAGER_PRAGMAS |
| | @@ -12453,10 +12448,13 @@ |
| 12453 | 12448 | #ifdef SQLITE_OMIT_WSD |
| 12454 | 12449 | "OMIT_WSD", |
| 12455 | 12450 | #endif |
| 12456 | 12451 | #ifdef SQLITE_OMIT_XFER_OPT |
| 12457 | 12452 | "OMIT_XFER_OPT", |
| 12453 | +#endif |
| 12454 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 12455 | + "PAGECACHE_BLOCKALLOC", |
| 12458 | 12456 | #endif |
| 12459 | 12457 | #ifdef SQLITE_PERFORMANCE_TRACE |
| 12460 | 12458 | "PERFORMANCE_TRACE", |
| 12461 | 12459 | #endif |
| 12462 | 12460 | #ifdef SQLITE_PROXY_DEBUG |
| | @@ -12574,10 +12572,13 @@ |
| 12574 | 12572 | /* |
| 12575 | 12573 | ** Boolean values |
| 12576 | 12574 | */ |
| 12577 | 12575 | typedef unsigned char Bool; |
| 12578 | 12576 | |
| 12577 | +/* Opaque type used by code in vdbesort.c */ |
| 12578 | +typedef struct VdbeSorter VdbeSorter; |
| 12579 | + |
| 12579 | 12580 | /* |
| 12580 | 12581 | ** A cursor is a pointer into a single BTree within a database file. |
| 12581 | 12582 | ** The cursor can seek to a BTree entry with a particular key, or |
| 12582 | 12583 | ** loop over all entries of the Btree. You can also insert new BTree |
| 12583 | 12584 | ** entries or retrieve the key or data from the entry that the cursor |
| | @@ -12605,10 +12606,11 @@ |
| 12605 | 12606 | sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ |
| 12606 | 12607 | const sqlite3_module *pModule; /* Module for cursor pVtabCursor */ |
| 12607 | 12608 | i64 seqCount; /* Sequence counter */ |
| 12608 | 12609 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 12609 | 12610 | i64 lastRowid; /* Last rowid from a Next or NextIdx operation */ |
| 12611 | + VdbeSorter *pSorter; /* Sorter object for OP_OpenSorter cursors */ |
| 12610 | 12612 | |
| 12611 | 12613 | /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or |
| 12612 | 12614 | ** OP_IsUnique opcode on this cursor. */ |
| 12613 | 12615 | int seekResult; |
| 12614 | 12616 | |
| | @@ -12924,17 +12926,36 @@ |
| 12924 | 12926 | SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*); |
| 12925 | 12927 | SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); |
| 12926 | 12928 | SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*); |
| 12927 | 12929 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); |
| 12928 | 12930 | SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p); |
| 12931 | +#define MemReleaseExt(X) \ |
| 12932 | + if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \ |
| 12933 | + sqlite3VdbeMemReleaseExternal(X); |
| 12929 | 12934 | SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); |
| 12930 | 12935 | SQLITE_PRIVATE const char *sqlite3OpcodeName(int); |
| 12931 | 12936 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 12932 | 12937 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 12933 | 12938 | SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); |
| 12934 | 12939 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); |
| 12935 | 12940 | SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem); |
| 12941 | + |
| 12942 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 12943 | +# define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK |
| 12944 | +# define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK |
| 12945 | +# define sqlite3VdbeSorterClose(Y,Z) |
| 12946 | +# define sqlite3VdbeSorterRowkey(Y,Z) SQLITE_OK |
| 12947 | +# define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK |
| 12948 | +# define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK |
| 12949 | +#else |
| 12950 | +SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *); |
| 12951 | +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, int); |
| 12952 | +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *); |
| 12953 | +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *); |
| 12954 | +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *); |
| 12955 | +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *); |
| 12956 | +#endif |
| 12936 | 12957 | |
| 12937 | 12958 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 |
| 12938 | 12959 | SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*); |
| 12939 | 12960 | SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*); |
| 12940 | 12961 | #else |
| | @@ -13225,10 +13246,12 @@ |
| 13225 | 13246 | ** Astronomical Algorithms, 2nd Edition, 1998 |
| 13226 | 13247 | ** ISBM 0-943396-61-1 |
| 13227 | 13248 | ** Willmann-Bell, Inc |
| 13228 | 13249 | ** Richmond, Virginia (USA) |
| 13229 | 13250 | */ |
| 13251 | +/* #include <stdlib.h> */ |
| 13252 | +/* #include <assert.h> */ |
| 13230 | 13253 | #include <time.h> |
| 13231 | 13254 | |
| 13232 | 13255 | #ifndef SQLITE_OMIT_DATETIME_FUNCS |
| 13233 | 13256 | |
| 13234 | 13257 | |
| | @@ -14978,10 +15001,11 @@ |
| 14978 | 15001 | extern void backtrace_symbols_fd(void*const*,int,int); |
| 14979 | 15002 | #else |
| 14980 | 15003 | # define backtrace(A,B) 1 |
| 14981 | 15004 | # define backtrace_symbols_fd(A,B,C) |
| 14982 | 15005 | #endif |
| 15006 | +/* #include <stdio.h> */ |
| 14983 | 15007 | |
| 14984 | 15008 | /* |
| 14985 | 15009 | ** Each memory allocation looks like this: |
| 14986 | 15010 | ** |
| 14987 | 15011 | ** ------------------------------------------------------------------------ |
| | @@ -18081,10 +18105,11 @@ |
| 18081 | 18105 | ** |
| 18082 | 18106 | ************************************************************************* |
| 18083 | 18107 | ** |
| 18084 | 18108 | ** Memory allocation functions used throughout sqlite. |
| 18085 | 18109 | */ |
| 18110 | +/* #include <stdarg.h> */ |
| 18086 | 18111 | |
| 18087 | 18112 | /* |
| 18088 | 18113 | ** Attempt to release up to n bytes of non-essential memory currently |
| 18089 | 18114 | ** held by SQLite. An example of non-essential memory is memory used to |
| 18090 | 18115 | ** cache database pages that are not currently in use. |
| | @@ -20058,10 +20083,11 @@ |
| 20058 | 20083 | ** BOM or Byte Order Mark: |
| 20059 | 20084 | ** 0xff 0xfe little-endian utf-16 follows |
| 20060 | 20085 | ** 0xfe 0xff big-endian utf-16 follows |
| 20061 | 20086 | ** |
| 20062 | 20087 | */ |
| 20088 | +/* #include <assert.h> */ |
| 20063 | 20089 | |
| 20064 | 20090 | #ifndef SQLITE_AMALGAMATION |
| 20065 | 20091 | /* |
| 20066 | 20092 | ** The following constant value is used by the SQLITE_BIGENDIAN and |
| 20067 | 20093 | ** SQLITE_LITTLEENDIAN macros. |
| | @@ -20486,11 +20512,11 @@ |
| 20486 | 20512 | ** no longer required. |
| 20487 | 20513 | ** |
| 20488 | 20514 | ** If a malloc failure occurs, NULL is returned and the db.mallocFailed |
| 20489 | 20515 | ** flag set. |
| 20490 | 20516 | */ |
| 20491 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 20517 | +#ifdef SQLITE_ENABLE_STAT2 |
| 20492 | 20518 | SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){ |
| 20493 | 20519 | Mem m; |
| 20494 | 20520 | memset(&m, 0, sizeof(m)); |
| 20495 | 20521 | m.db = db; |
| 20496 | 20522 | sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC); |
| | @@ -20600,10 +20626,11 @@ |
| 20600 | 20626 | ** |
| 20601 | 20627 | ** This file contains functions for allocating memory, comparing |
| 20602 | 20628 | ** strings, and stuff like that. |
| 20603 | 20629 | ** |
| 20604 | 20630 | */ |
| 20631 | +/* #include <stdarg.h> */ |
| 20605 | 20632 | #ifdef SQLITE_HAVE_ISNAN |
| 20606 | 20633 | # include <math.h> |
| 20607 | 20634 | #endif |
| 20608 | 20635 | |
| 20609 | 20636 | /* |
| | @@ -21778,10 +21805,11 @@ |
| 21778 | 21805 | ** |
| 21779 | 21806 | ************************************************************************* |
| 21780 | 21807 | ** This is the implementation of generic hash-tables |
| 21781 | 21808 | ** used in SQLite. |
| 21782 | 21809 | */ |
| 21810 | +/* #include <assert.h> */ |
| 21783 | 21811 | |
| 21784 | 21812 | /* Turn bulk memory into a hash table object by initializing the |
| 21785 | 21813 | ** fields of the Hash structure. |
| 21786 | 21814 | ** |
| 21787 | 21815 | ** "pNew" is a pointer to the hash table that is to be initialized. |
| | @@ -22086,52 +22114,52 @@ |
| 22086 | 22114 | /* 35 */ "ReadCookie", |
| 22087 | 22115 | /* 36 */ "SetCookie", |
| 22088 | 22116 | /* 37 */ "VerifyCookie", |
| 22089 | 22117 | /* 38 */ "OpenRead", |
| 22090 | 22118 | /* 39 */ "OpenWrite", |
| 22091 | | - /* 40 */ "OpenAutoindex", |
| 22092 | | - /* 41 */ "OpenEphemeral", |
| 22093 | | - /* 42 */ "OpenPseudo", |
| 22094 | | - /* 43 */ "Close", |
| 22095 | | - /* 44 */ "SeekLt", |
| 22096 | | - /* 45 */ "SeekLe", |
| 22097 | | - /* 46 */ "SeekGe", |
| 22098 | | - /* 47 */ "SeekGt", |
| 22099 | | - /* 48 */ "Seek", |
| 22100 | | - /* 49 */ "NotFound", |
| 22101 | | - /* 50 */ "Found", |
| 22102 | | - /* 51 */ "IsUnique", |
| 22103 | | - /* 52 */ "NotExists", |
| 22104 | | - /* 53 */ "Sequence", |
| 22105 | | - /* 54 */ "NewRowid", |
| 22106 | | - /* 55 */ "Insert", |
| 22107 | | - /* 56 */ "InsertInt", |
| 22108 | | - /* 57 */ "Delete", |
| 22109 | | - /* 58 */ "ResetCount", |
| 22110 | | - /* 59 */ "RowKey", |
| 22111 | | - /* 60 */ "RowData", |
| 22112 | | - /* 61 */ "Rowid", |
| 22113 | | - /* 62 */ "NullRow", |
| 22114 | | - /* 63 */ "Last", |
| 22115 | | - /* 64 */ "Sort", |
| 22116 | | - /* 65 */ "Rewind", |
| 22117 | | - /* 66 */ "Prev", |
| 22118 | | - /* 67 */ "Next", |
| 22119 | + /* 40 */ "OpenSorter", |
| 22120 | + /* 41 */ "OpenAutoindex", |
| 22121 | + /* 42 */ "OpenEphemeral", |
| 22122 | + /* 43 */ "OpenPseudo", |
| 22123 | + /* 44 */ "Close", |
| 22124 | + /* 45 */ "SeekLt", |
| 22125 | + /* 46 */ "SeekLe", |
| 22126 | + /* 47 */ "SeekGe", |
| 22127 | + /* 48 */ "SeekGt", |
| 22128 | + /* 49 */ "Seek", |
| 22129 | + /* 50 */ "NotFound", |
| 22130 | + /* 51 */ "Found", |
| 22131 | + /* 52 */ "IsUnique", |
| 22132 | + /* 53 */ "NotExists", |
| 22133 | + /* 54 */ "Sequence", |
| 22134 | + /* 55 */ "NewRowid", |
| 22135 | + /* 56 */ "Insert", |
| 22136 | + /* 57 */ "InsertInt", |
| 22137 | + /* 58 */ "Delete", |
| 22138 | + /* 59 */ "ResetCount", |
| 22139 | + /* 60 */ "RowKey", |
| 22140 | + /* 61 */ "RowData", |
| 22141 | + /* 62 */ "Rowid", |
| 22142 | + /* 63 */ "NullRow", |
| 22143 | + /* 64 */ "Last", |
| 22144 | + /* 65 */ "Sort", |
| 22145 | + /* 66 */ "Rewind", |
| 22146 | + /* 67 */ "Prev", |
| 22119 | 22147 | /* 68 */ "Or", |
| 22120 | 22148 | /* 69 */ "And", |
| 22121 | | - /* 70 */ "IdxInsert", |
| 22122 | | - /* 71 */ "IdxDelete", |
| 22123 | | - /* 72 */ "IdxRowid", |
| 22149 | + /* 70 */ "Next", |
| 22150 | + /* 71 */ "IdxInsert", |
| 22151 | + /* 72 */ "IdxDelete", |
| 22124 | 22152 | /* 73 */ "IsNull", |
| 22125 | 22153 | /* 74 */ "NotNull", |
| 22126 | 22154 | /* 75 */ "Ne", |
| 22127 | 22155 | /* 76 */ "Eq", |
| 22128 | 22156 | /* 77 */ "Gt", |
| 22129 | 22157 | /* 78 */ "Le", |
| 22130 | 22158 | /* 79 */ "Lt", |
| 22131 | 22159 | /* 80 */ "Ge", |
| 22132 | | - /* 81 */ "IdxLT", |
| 22160 | + /* 81 */ "IdxRowid", |
| 22133 | 22161 | /* 82 */ "BitAnd", |
| 22134 | 22162 | /* 83 */ "BitOr", |
| 22135 | 22163 | /* 84 */ "ShiftLeft", |
| 22136 | 22164 | /* 85 */ "ShiftRight", |
| 22137 | 22165 | /* 86 */ "Add", |
| | @@ -22138,58 +22166,58 @@ |
| 22138 | 22166 | /* 87 */ "Subtract", |
| 22139 | 22167 | /* 88 */ "Multiply", |
| 22140 | 22168 | /* 89 */ "Divide", |
| 22141 | 22169 | /* 90 */ "Remainder", |
| 22142 | 22170 | /* 91 */ "Concat", |
| 22143 | | - /* 92 */ "IdxGE", |
| 22171 | + /* 92 */ "IdxLT", |
| 22144 | 22172 | /* 93 */ "BitNot", |
| 22145 | 22173 | /* 94 */ "String8", |
| 22146 | | - /* 95 */ "Destroy", |
| 22147 | | - /* 96 */ "Clear", |
| 22148 | | - /* 97 */ "CreateIndex", |
| 22149 | | - /* 98 */ "CreateTable", |
| 22150 | | - /* 99 */ "ParseSchema", |
| 22151 | | - /* 100 */ "LoadAnalysis", |
| 22152 | | - /* 101 */ "DropTable", |
| 22153 | | - /* 102 */ "DropIndex", |
| 22154 | | - /* 103 */ "DropTrigger", |
| 22155 | | - /* 104 */ "IntegrityCk", |
| 22156 | | - /* 105 */ "RowSetAdd", |
| 22157 | | - /* 106 */ "RowSetRead", |
| 22158 | | - /* 107 */ "RowSetTest", |
| 22159 | | - /* 108 */ "Program", |
| 22160 | | - /* 109 */ "Param", |
| 22161 | | - /* 110 */ "FkCounter", |
| 22162 | | - /* 111 */ "FkIfZero", |
| 22163 | | - /* 112 */ "MemMax", |
| 22164 | | - /* 113 */ "IfPos", |
| 22165 | | - /* 114 */ "IfNeg", |
| 22166 | | - /* 115 */ "IfZero", |
| 22167 | | - /* 116 */ "AggStep", |
| 22168 | | - /* 117 */ "AggFinal", |
| 22169 | | - /* 118 */ "Checkpoint", |
| 22170 | | - /* 119 */ "JournalMode", |
| 22171 | | - /* 120 */ "Vacuum", |
| 22172 | | - /* 121 */ "IncrVacuum", |
| 22173 | | - /* 122 */ "Expire", |
| 22174 | | - /* 123 */ "TableLock", |
| 22175 | | - /* 124 */ "VBegin", |
| 22176 | | - /* 125 */ "VCreate", |
| 22177 | | - /* 126 */ "VDestroy", |
| 22178 | | - /* 127 */ "VOpen", |
| 22179 | | - /* 128 */ "VFilter", |
| 22180 | | - /* 129 */ "VColumn", |
| 22174 | + /* 95 */ "IdxGE", |
| 22175 | + /* 96 */ "Destroy", |
| 22176 | + /* 97 */ "Clear", |
| 22177 | + /* 98 */ "CreateIndex", |
| 22178 | + /* 99 */ "CreateTable", |
| 22179 | + /* 100 */ "ParseSchema", |
| 22180 | + /* 101 */ "LoadAnalysis", |
| 22181 | + /* 102 */ "DropTable", |
| 22182 | + /* 103 */ "DropIndex", |
| 22183 | + /* 104 */ "DropTrigger", |
| 22184 | + /* 105 */ "IntegrityCk", |
| 22185 | + /* 106 */ "RowSetAdd", |
| 22186 | + /* 107 */ "RowSetRead", |
| 22187 | + /* 108 */ "RowSetTest", |
| 22188 | + /* 109 */ "Program", |
| 22189 | + /* 110 */ "Param", |
| 22190 | + /* 111 */ "FkCounter", |
| 22191 | + /* 112 */ "FkIfZero", |
| 22192 | + /* 113 */ "MemMax", |
| 22193 | + /* 114 */ "IfPos", |
| 22194 | + /* 115 */ "IfNeg", |
| 22195 | + /* 116 */ "IfZero", |
| 22196 | + /* 117 */ "AggStep", |
| 22197 | + /* 118 */ "AggFinal", |
| 22198 | + /* 119 */ "Checkpoint", |
| 22199 | + /* 120 */ "JournalMode", |
| 22200 | + /* 121 */ "Vacuum", |
| 22201 | + /* 122 */ "IncrVacuum", |
| 22202 | + /* 123 */ "Expire", |
| 22203 | + /* 124 */ "TableLock", |
| 22204 | + /* 125 */ "VBegin", |
| 22205 | + /* 126 */ "VCreate", |
| 22206 | + /* 127 */ "VDestroy", |
| 22207 | + /* 128 */ "VOpen", |
| 22208 | + /* 129 */ "VFilter", |
| 22181 | 22209 | /* 130 */ "Real", |
| 22182 | | - /* 131 */ "VNext", |
| 22183 | | - /* 132 */ "VRename", |
| 22184 | | - /* 133 */ "VUpdate", |
| 22185 | | - /* 134 */ "Pagecount", |
| 22186 | | - /* 135 */ "MaxPgcnt", |
| 22187 | | - /* 136 */ "Trace", |
| 22188 | | - /* 137 */ "Noop", |
| 22189 | | - /* 138 */ "Explain", |
| 22190 | | - /* 139 */ "NotUsed_139", |
| 22210 | + /* 131 */ "VColumn", |
| 22211 | + /* 132 */ "VNext", |
| 22212 | + /* 133 */ "VRename", |
| 22213 | + /* 134 */ "VUpdate", |
| 22214 | + /* 135 */ "Pagecount", |
| 22215 | + /* 136 */ "MaxPgcnt", |
| 22216 | + /* 137 */ "Trace", |
| 22217 | + /* 138 */ "Noop", |
| 22218 | + /* 139 */ "Explain", |
| 22191 | 22219 | /* 140 */ "NotUsed_140", |
| 22192 | 22220 | /* 141 */ "ToText", |
| 22193 | 22221 | /* 142 */ "ToBlob", |
| 22194 | 22222 | /* 143 */ "ToNumeric", |
| 22195 | 22223 | /* 144 */ "ToInt", |
| | @@ -24450,10 +24478,11 @@ |
| 24450 | 24478 | */ |
| 24451 | 24479 | #include <sys/types.h> |
| 24452 | 24480 | #include <sys/stat.h> |
| 24453 | 24481 | #include <fcntl.h> |
| 24454 | 24482 | #include <unistd.h> |
| 24483 | +/* #include <time.h> */ |
| 24455 | 24484 | #include <sys/time.h> |
| 24456 | 24485 | #include <errno.h> |
| 24457 | 24486 | #ifndef SQLITE_OMIT_WAL |
| 24458 | 24487 | #include <sys/mman.h> |
| 24459 | 24488 | #endif |
| | @@ -24485,10 +24514,11 @@ |
| 24485 | 24514 | /* |
| 24486 | 24515 | ** If we are to be thread-safe, include the pthreads header and define |
| 24487 | 24516 | ** the SQLITE_UNIX_THREADS macro. |
| 24488 | 24517 | */ |
| 24489 | 24518 | #if SQLITE_THREADSAFE |
| 24519 | +/* # include <pthread.h> */ |
| 24490 | 24520 | # define SQLITE_UNIX_THREADS 1 |
| 24491 | 24521 | #endif |
| 24492 | 24522 | |
| 24493 | 24523 | /* |
| 24494 | 24524 | ** Default permissions when creating a new file |
| | @@ -24584,11 +24614,15 @@ |
| 24584 | 24614 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 24585 | 24615 | */ |
| 24586 | 24616 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 24587 | 24617 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 24588 | 24618 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
| 24589 | | -#define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 24619 | +#ifndef SQLITE_DISABLE_DIRSYNC |
| 24620 | +# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 24621 | +#else |
| 24622 | +# define UNIXFILE_DIRSYNC 0x00 |
| 24623 | +#endif |
| 24590 | 24624 | |
| 24591 | 24625 | /* |
| 24592 | 24626 | ** Include code that is common to all os_*.c files |
| 24593 | 24627 | */ |
| 24594 | 24628 | /************** Include os_common.h in the middle of os_unix.c ***************/ |
| | @@ -27061,15 +27095,16 @@ |
| 27061 | 27095 | */ |
| 27062 | 27096 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 27063 | 27097 | int rc = SQLITE_OK; |
| 27064 | 27098 | int reserved = 0; |
| 27065 | 27099 | unixFile *pFile = (unixFile*)id; |
| 27100 | + afpLockingContext *context; |
| 27066 | 27101 | |
| 27067 | 27102 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 27068 | 27103 | |
| 27069 | 27104 | assert( pFile ); |
| 27070 | | - afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 27105 | + context = (afpLockingContext *) pFile->lockingContext; |
| 27071 | 27106 | if( context->reserved ){ |
| 27072 | 27107 | *pResOut = 1; |
| 27073 | 27108 | return SQLITE_OK; |
| 27074 | 27109 | } |
| 27075 | 27110 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
| | @@ -27205,11 +27240,11 @@ |
| 27205 | 27240 | |
| 27206 | 27241 | /* If control gets to this point, then actually go ahead and make |
| 27207 | 27242 | ** operating system calls for the specified lock. |
| 27208 | 27243 | */ |
| 27209 | 27244 | if( eFileLock==SHARED_LOCK ){ |
| 27210 | | - int lrc1, lrc2, lrc1Errno; |
| 27245 | + int lrc1, lrc2, lrc1Errno = 0; |
| 27211 | 27246 | long lk, mask; |
| 27212 | 27247 | |
| 27213 | 27248 | assert( pInode->nShared==0 ); |
| 27214 | 27249 | assert( pInode->eFileLock==0 ); |
| 27215 | 27250 | |
| | @@ -27579,21 +27614,23 @@ |
| 27579 | 27614 | #if defined(USE_PREAD) |
| 27580 | 27615 | do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
| 27581 | 27616 | #elif defined(USE_PREAD64) |
| 27582 | 27617 | do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR); |
| 27583 | 27618 | #else |
| 27584 | | - newOffset = lseek(id->h, offset, SEEK_SET); |
| 27585 | | - SimulateIOError( newOffset-- ); |
| 27586 | | - if( newOffset!=offset ){ |
| 27587 | | - if( newOffset == -1 ){ |
| 27588 | | - ((unixFile*)id)->lastErrno = errno; |
| 27589 | | - }else{ |
| 27590 | | - ((unixFile*)id)->lastErrno = 0; |
| 27591 | | - } |
| 27592 | | - return -1; |
| 27593 | | - } |
| 27594 | | - do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR ); |
| 27619 | + do{ |
| 27620 | + newOffset = lseek(id->h, offset, SEEK_SET); |
| 27621 | + SimulateIOError( newOffset-- ); |
| 27622 | + if( newOffset!=offset ){ |
| 27623 | + if( newOffset == -1 ){ |
| 27624 | + ((unixFile*)id)->lastErrno = errno; |
| 27625 | + }else{ |
| 27626 | + ((unixFile*)id)->lastErrno = 0; |
| 27627 | + } |
| 27628 | + return -1; |
| 27629 | + } |
| 27630 | + got = osWrite(id->h, pBuf, cnt); |
| 27631 | + }while( got<0 && errno==EINTR ); |
| 27595 | 27632 | #endif |
| 27596 | 27633 | TIMER_END; |
| 27597 | 27634 | if( got<0 ){ |
| 27598 | 27635 | ((unixFile*)id)->lastErrno = errno; |
| 27599 | 27636 | } |
| | @@ -27888,10 +27925,12 @@ |
| 27888 | 27925 | HAVE_FULLFSYNC, isFullsync)); |
| 27889 | 27926 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 27890 | 27927 | if( rc==SQLITE_OK && dirfd>=0 ){ |
| 27891 | 27928 | full_fsync(dirfd, 0, 0); |
| 27892 | 27929 | robust_close(pFile, dirfd, __LINE__); |
| 27930 | + }else if( rc==SQLITE_CANTOPEN ){ |
| 27931 | + rc = SQLITE_OK; |
| 27893 | 27932 | } |
| 27894 | 27933 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
| 27895 | 27934 | } |
| 27896 | 27935 | return rc; |
| 27897 | 27936 | } |
| | @@ -27971,30 +28010,22 @@ |
| 27971 | 28010 | static int proxyFileControl(sqlite3_file*,int,void*); |
| 27972 | 28011 | #endif |
| 27973 | 28012 | |
| 27974 | 28013 | /* |
| 27975 | 28014 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
| 27976 | | -** file-control operation. |
| 27977 | | -** |
| 27978 | | -** If the user has configured a chunk-size for this file, it could be |
| 27979 | | -** that the file needs to be extended at this point. Otherwise, the |
| 27980 | | -** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix. |
| 28015 | +** file-control operation. Enlarge the database to nBytes in size |
| 28016 | +** (rounded up to the next chunk-size). If the database is already |
| 28017 | +** nBytes or larger, this routine is a no-op. |
| 27981 | 28018 | */ |
| 27982 | 28019 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
| 27983 | | - { /* preserve indentation of removed "if" */ |
| 28020 | + if( pFile->szChunk ){ |
| 27984 | 28021 | i64 nSize; /* Required file size */ |
| 27985 | | - i64 szChunk; /* Chunk size */ |
| 27986 | 28022 | struct stat buf; /* Used to hold return values of fstat() */ |
| 27987 | 28023 | |
| 27988 | 28024 | if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
| 27989 | 28025 | |
| 27990 | | - szChunk = pFile->szChunk; |
| 27991 | | - if( szChunk==0 ){ |
| 27992 | | - nSize = nByte; |
| 27993 | | - }else{ |
| 27994 | | - nSize = ((nByte+szChunk-1) / szChunk) * szChunk; |
| 27995 | | - } |
| 28026 | + nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 27996 | 28027 | if( nSize>(i64)buf.st_size ){ |
| 27997 | 28028 | |
| 27998 | 28029 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
| 27999 | 28030 | /* The code below is handling the return value of osFallocate() |
| 28000 | 28031 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| | @@ -28048,11 +28079,15 @@ |
| 28048 | 28079 | case SQLITE_FCNTL_CHUNK_SIZE: { |
| 28049 | 28080 | pFile->szChunk = *(int *)pArg; |
| 28050 | 28081 | return SQLITE_OK; |
| 28051 | 28082 | } |
| 28052 | 28083 | case SQLITE_FCNTL_SIZE_HINT: { |
| 28053 | | - return fcntlSizeHint(pFile, *(i64 *)pArg); |
| 28084 | + int rc; |
| 28085 | + SimulateIOErrorBenign(1); |
| 28086 | + rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 28087 | + SimulateIOErrorBenign(0); |
| 28088 | + return rc; |
| 28054 | 28089 | } |
| 28055 | 28090 | case SQLITE_FCNTL_PERSIST_WAL: { |
| 28056 | 28091 | int bPersist = *(int*)pArg; |
| 28057 | 28092 | if( bPersist<0 ){ |
| 28058 | 28093 | *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0; |
| | @@ -29485,10 +29520,13 @@ |
| 29485 | 29520 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 29486 | 29521 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
| 29487 | 29522 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 29488 | 29523 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 29489 | 29524 | #endif |
| 29525 | +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 29526 | + struct statfs fsInfo; |
| 29527 | +#endif |
| 29490 | 29528 | |
| 29491 | 29529 | /* If creating a master or main-file journal, this function will open |
| 29492 | 29530 | ** a file-descriptor on the directory too. The first time unixSync() |
| 29493 | 29531 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 29494 | 29532 | */ |
| | @@ -29617,11 +29655,10 @@ |
| 29617 | 29655 | |
| 29618 | 29656 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
| 29619 | 29657 | |
| 29620 | 29658 | |
| 29621 | 29659 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 29622 | | - struct statfs fsInfo; |
| 29623 | 29660 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 29624 | 29661 | ((unixFile*)pFile)->lastErrno = errno; |
| 29625 | 29662 | robust_close(p, fd, __LINE__); |
| 29626 | 29663 | return SQLITE_IOERR_ACCESS; |
| 29627 | 29664 | } |
| | @@ -29641,11 +29678,10 @@ |
| 29641 | 29678 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 29642 | 29679 | ** never use proxy, NULL means use proxy for non-local files only. */ |
| 29643 | 29680 | if( envforce!=NULL ){ |
| 29644 | 29681 | useProxy = atoi(envforce)>0; |
| 29645 | 29682 | }else{ |
| 29646 | | - struct statfs fsInfo; |
| 29647 | 29683 | if( statfs(zPath, &fsInfo) == -1 ){ |
| 29648 | 29684 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 29649 | 29685 | ** with fd is a database file, and there are other connections open |
| 29650 | 29686 | ** on that file that are currently holding advisory locks on it, |
| 29651 | 29687 | ** then the call to close() will cancel those locks. In practice, |
| | @@ -29715,10 +29751,12 @@ |
| 29715 | 29751 | #endif |
| 29716 | 29752 | { |
| 29717 | 29753 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
| 29718 | 29754 | } |
| 29719 | 29755 | robust_close(0, fd, __LINE__); |
| 29756 | + }else if( rc==SQLITE_CANTOPEN ){ |
| 29757 | + rc = SQLITE_OK; |
| 29720 | 29758 | } |
| 29721 | 29759 | } |
| 29722 | 29760 | #endif |
| 29723 | 29761 | return rc; |
| 29724 | 29762 | } |
| | @@ -30380,10 +30418,12 @@ |
| 30380 | 30418 | *pError = err; |
| 30381 | 30419 | } |
| 30382 | 30420 | return SQLITE_IOERR; |
| 30383 | 30421 | } |
| 30384 | 30422 | } |
| 30423 | +#else |
| 30424 | + UNUSED_PARAMETER(pError); |
| 30385 | 30425 | #endif |
| 30386 | 30426 | #ifdef SQLITE_TEST |
| 30387 | 30427 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 30388 | 30428 | if( sqlite3_hostid_num != 0){ |
| 30389 | 30429 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| | @@ -30472,10 +30512,11 @@ |
| 30472 | 30512 | unixFile *conchFile = pCtx->conchFile; |
| 30473 | 30513 | int rc = SQLITE_OK; |
| 30474 | 30514 | int nTries = 0; |
| 30475 | 30515 | struct timespec conchModTime; |
| 30476 | 30516 | |
| 30517 | + memset(&conchModTime, 0, sizeof(conchModTime)); |
| 30477 | 30518 | do { |
| 30478 | 30519 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 30479 | 30520 | nTries ++; |
| 30480 | 30521 | if( rc==SQLITE_BUSY ){ |
| 30481 | 30522 | /* If the lock failed (busy): |
| | @@ -30703,15 +30744,16 @@ |
| 30703 | 30744 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 30704 | 30745 | |
| 30705 | 30746 | end_takeconch: |
| 30706 | 30747 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
| 30707 | 30748 | if( rc==SQLITE_OK && pFile->openFlags ){ |
| 30749 | + int fd; |
| 30708 | 30750 | if( pFile->h>=0 ){ |
| 30709 | 30751 | robust_close(pFile, pFile->h, __LINE__); |
| 30710 | 30752 | } |
| 30711 | 30753 | pFile->h = -1; |
| 30712 | | - int fd = robust_open(pCtx->dbPath, pFile->openFlags, |
| 30754 | + fd = robust_open(pCtx->dbPath, pFile->openFlags, |
| 30713 | 30755 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 30714 | 30756 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
| 30715 | 30757 | if( fd>=0 ){ |
| 30716 | 30758 | pFile->h = fd; |
| 30717 | 30759 | }else{ |
| | @@ -31629,10 +31671,80 @@ |
| 31629 | 31671 | winceLock local; /* Locks obtained by this instance of winFile */ |
| 31630 | 31672 | winceLock *shared; /* Global shared lock memory for the file */ |
| 31631 | 31673 | #endif |
| 31632 | 31674 | }; |
| 31633 | 31675 | |
| 31676 | +/* |
| 31677 | + * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the |
| 31678 | + * various Win32 API heap functions instead of our own. |
| 31679 | + */ |
| 31680 | +#ifdef SQLITE_WIN32_MALLOC |
| 31681 | +/* |
| 31682 | + * The initial size of the Win32-specific heap. This value may be zero. |
| 31683 | + */ |
| 31684 | +#ifndef SQLITE_WIN32_HEAP_INIT_SIZE |
| 31685 | +# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \ |
| 31686 | + (SQLITE_DEFAULT_PAGE_SIZE) + 4194304) |
| 31687 | +#endif |
| 31688 | + |
| 31689 | +/* |
| 31690 | + * The maximum size of the Win32-specific heap. This value may be zero. |
| 31691 | + */ |
| 31692 | +#ifndef SQLITE_WIN32_HEAP_MAX_SIZE |
| 31693 | +# define SQLITE_WIN32_HEAP_MAX_SIZE (0) |
| 31694 | +#endif |
| 31695 | + |
| 31696 | +/* |
| 31697 | + * The extra flags to use in calls to the Win32 heap APIs. This value may be |
| 31698 | + * zero for the default behavior. |
| 31699 | + */ |
| 31700 | +#ifndef SQLITE_WIN32_HEAP_FLAGS |
| 31701 | +# define SQLITE_WIN32_HEAP_FLAGS (0) |
| 31702 | +#endif |
| 31703 | + |
| 31704 | +/* |
| 31705 | +** The winMemData structure stores information required by the Win32-specific |
| 31706 | +** sqlite3_mem_methods implementation. |
| 31707 | +*/ |
| 31708 | +typedef struct winMemData winMemData; |
| 31709 | +struct winMemData { |
| 31710 | +#ifndef NDEBUG |
| 31711 | + u32 magic; /* Magic number to detect structure corruption. */ |
| 31712 | +#endif |
| 31713 | + HANDLE hHeap; /* The handle to our heap. */ |
| 31714 | + BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */ |
| 31715 | +}; |
| 31716 | + |
| 31717 | +#ifndef NDEBUG |
| 31718 | +#define WINMEM_MAGIC 0x42b2830b |
| 31719 | +#endif |
| 31720 | + |
| 31721 | +static struct winMemData win_mem_data = { |
| 31722 | +#ifndef NDEBUG |
| 31723 | + WINMEM_MAGIC, |
| 31724 | +#endif |
| 31725 | + NULL, FALSE |
| 31726 | +}; |
| 31727 | + |
| 31728 | +#ifndef NDEBUG |
| 31729 | +#define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC ) |
| 31730 | +#else |
| 31731 | +#define winMemAssertMagic() |
| 31732 | +#endif |
| 31733 | + |
| 31734 | +#define winMemGetHeap() win_mem_data.hHeap |
| 31735 | + |
| 31736 | +static void *winMemMalloc(int nBytes); |
| 31737 | +static void winMemFree(void *pPrior); |
| 31738 | +static void *winMemRealloc(void *pPrior, int nBytes); |
| 31739 | +static int winMemSize(void *p); |
| 31740 | +static int winMemRoundup(int n); |
| 31741 | +static int winMemInit(void *pAppData); |
| 31742 | +static void winMemShutdown(void *pAppData); |
| 31743 | + |
| 31744 | +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void); |
| 31745 | +#endif /* SQLITE_WIN32_MALLOC */ |
| 31634 | 31746 | |
| 31635 | 31747 | /* |
| 31636 | 31748 | ** Forward prototypes. |
| 31637 | 31749 | */ |
| 31638 | 31750 | static int getSectorSize( |
| | @@ -31681,10 +31793,192 @@ |
| 31681 | 31793 | } |
| 31682 | 31794 | return sqlite3_os_type==2; |
| 31683 | 31795 | } |
| 31684 | 31796 | #endif /* SQLITE_OS_WINCE */ |
| 31685 | 31797 | |
| 31798 | +#ifdef SQLITE_WIN32_MALLOC |
| 31799 | +/* |
| 31800 | +** Allocate nBytes of memory. |
| 31801 | +*/ |
| 31802 | +static void *winMemMalloc(int nBytes){ |
| 31803 | + HANDLE hHeap; |
| 31804 | + void *p; |
| 31805 | + |
| 31806 | + winMemAssertMagic(); |
| 31807 | + hHeap = winMemGetHeap(); |
| 31808 | + assert( hHeap!=0 ); |
| 31809 | + assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 31810 | +#ifdef SQLITE_WIN32_MALLOC_VALIDATE |
| 31811 | + assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 31812 | +#endif |
| 31813 | + assert( nBytes>=0 ); |
| 31814 | + p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes); |
| 31815 | + if( !p ){ |
| 31816 | + sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p", |
| 31817 | + nBytes, GetLastError(), (void*)hHeap); |
| 31818 | + } |
| 31819 | + return p; |
| 31820 | +} |
| 31821 | + |
| 31822 | +/* |
| 31823 | +** Free memory. |
| 31824 | +*/ |
| 31825 | +static void winMemFree(void *pPrior){ |
| 31826 | + HANDLE hHeap; |
| 31827 | + |
| 31828 | + winMemAssertMagic(); |
| 31829 | + hHeap = winMemGetHeap(); |
| 31830 | + assert( hHeap!=0 ); |
| 31831 | + assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 31832 | +#ifdef SQLITE_WIN32_MALLOC_VALIDATE |
| 31833 | + assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ); |
| 31834 | +#endif |
| 31835 | + if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */ |
| 31836 | + if( !HeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){ |
| 31837 | + sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p", |
| 31838 | + pPrior, GetLastError(), (void*)hHeap); |
| 31839 | + } |
| 31840 | +} |
| 31841 | + |
| 31842 | +/* |
| 31843 | +** Change the size of an existing memory allocation |
| 31844 | +*/ |
| 31845 | +static void *winMemRealloc(void *pPrior, int nBytes){ |
| 31846 | + HANDLE hHeap; |
| 31847 | + void *p; |
| 31848 | + |
| 31849 | + winMemAssertMagic(); |
| 31850 | + hHeap = winMemGetHeap(); |
| 31851 | + assert( hHeap!=0 ); |
| 31852 | + assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 31853 | +#ifdef SQLITE_WIN32_MALLOC_VALIDATE |
| 31854 | + assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ); |
| 31855 | +#endif |
| 31856 | + assert( nBytes>=0 ); |
| 31857 | + if( !pPrior ){ |
| 31858 | + p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes); |
| 31859 | + }else{ |
| 31860 | + p = HeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes); |
| 31861 | + } |
| 31862 | + if( !p ){ |
| 31863 | + sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p", |
| 31864 | + pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, GetLastError(), |
| 31865 | + (void*)hHeap); |
| 31866 | + } |
| 31867 | + return p; |
| 31868 | +} |
| 31869 | + |
| 31870 | +/* |
| 31871 | +** Return the size of an outstanding allocation, in bytes. |
| 31872 | +*/ |
| 31873 | +static int winMemSize(void *p){ |
| 31874 | + HANDLE hHeap; |
| 31875 | + SIZE_T n; |
| 31876 | + |
| 31877 | + winMemAssertMagic(); |
| 31878 | + hHeap = winMemGetHeap(); |
| 31879 | + assert( hHeap!=0 ); |
| 31880 | + assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 31881 | +#ifdef SQLITE_WIN32_MALLOC_VALIDATE |
| 31882 | + assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 31883 | +#endif |
| 31884 | + if( !p ) return 0; |
| 31885 | + n = HeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p); |
| 31886 | + if( n==(SIZE_T)-1 ){ |
| 31887 | + sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p", |
| 31888 | + p, GetLastError(), (void*)hHeap); |
| 31889 | + return 0; |
| 31890 | + } |
| 31891 | + return (int)n; |
| 31892 | +} |
| 31893 | + |
| 31894 | +/* |
| 31895 | +** Round up a request size to the next valid allocation size. |
| 31896 | +*/ |
| 31897 | +static int winMemRoundup(int n){ |
| 31898 | + return n; |
| 31899 | +} |
| 31900 | + |
| 31901 | +/* |
| 31902 | +** Initialize this module. |
| 31903 | +*/ |
| 31904 | +static int winMemInit(void *pAppData){ |
| 31905 | + winMemData *pWinMemData = (winMemData *)pAppData; |
| 31906 | + |
| 31907 | + if( !pWinMemData ) return SQLITE_ERROR; |
| 31908 | + assert( pWinMemData->magic==WINMEM_MAGIC ); |
| 31909 | + if( !pWinMemData->hHeap ){ |
| 31910 | + pWinMemData->hHeap = HeapCreate(SQLITE_WIN32_HEAP_FLAGS, |
| 31911 | + SQLITE_WIN32_HEAP_INIT_SIZE, |
| 31912 | + SQLITE_WIN32_HEAP_MAX_SIZE); |
| 31913 | + if( !pWinMemData->hHeap ){ |
| 31914 | + sqlite3_log(SQLITE_NOMEM, |
| 31915 | + "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u", |
| 31916 | + GetLastError(), SQLITE_WIN32_HEAP_FLAGS, SQLITE_WIN32_HEAP_INIT_SIZE, |
| 31917 | + SQLITE_WIN32_HEAP_MAX_SIZE); |
| 31918 | + return SQLITE_NOMEM; |
| 31919 | + } |
| 31920 | + pWinMemData->bOwned = TRUE; |
| 31921 | + } |
| 31922 | + assert( pWinMemData->hHeap!=0 ); |
| 31923 | + assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); |
| 31924 | +#ifdef SQLITE_WIN32_MALLOC_VALIDATE |
| 31925 | + assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 31926 | +#endif |
| 31927 | + return SQLITE_OK; |
| 31928 | +} |
| 31929 | + |
| 31930 | +/* |
| 31931 | +** Deinitialize this module. |
| 31932 | +*/ |
| 31933 | +static void winMemShutdown(void *pAppData){ |
| 31934 | + winMemData *pWinMemData = (winMemData *)pAppData; |
| 31935 | + |
| 31936 | + if( !pWinMemData ) return; |
| 31937 | + if( pWinMemData->hHeap ){ |
| 31938 | + assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); |
| 31939 | +#ifdef SQLITE_WIN32_MALLOC_VALIDATE |
| 31940 | + assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 31941 | +#endif |
| 31942 | + if( pWinMemData->bOwned ){ |
| 31943 | + if( !HeapDestroy(pWinMemData->hHeap) ){ |
| 31944 | + sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p", |
| 31945 | + GetLastError(), (void*)pWinMemData->hHeap); |
| 31946 | + } |
| 31947 | + pWinMemData->bOwned = FALSE; |
| 31948 | + } |
| 31949 | + pWinMemData->hHeap = NULL; |
| 31950 | + } |
| 31951 | +} |
| 31952 | + |
| 31953 | +/* |
| 31954 | +** Populate the low-level memory allocation function pointers in |
| 31955 | +** sqlite3GlobalConfig.m with pointers to the routines in this file. The |
| 31956 | +** arguments specify the block of memory to manage. |
| 31957 | +** |
| 31958 | +** This routine is only called by sqlite3_config(), and therefore |
| 31959 | +** is not required to be threadsafe (it is not). |
| 31960 | +*/ |
| 31961 | +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){ |
| 31962 | + static const sqlite3_mem_methods winMemMethods = { |
| 31963 | + winMemMalloc, |
| 31964 | + winMemFree, |
| 31965 | + winMemRealloc, |
| 31966 | + winMemSize, |
| 31967 | + winMemRoundup, |
| 31968 | + winMemInit, |
| 31969 | + winMemShutdown, |
| 31970 | + &win_mem_data |
| 31971 | + }; |
| 31972 | + return &winMemMethods; |
| 31973 | +} |
| 31974 | + |
| 31975 | +SQLITE_PRIVATE void sqlite3MemSetDefault(void){ |
| 31976 | + sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32()); |
| 31977 | +} |
| 31978 | +#endif /* SQLITE_WIN32_MALLOC */ |
| 31979 | + |
| 31686 | 31980 | /* |
| 31687 | 31981 | ** Convert a UTF-8 string to microsoft unicode (UTF-16?). |
| 31688 | 31982 | ** |
| 31689 | 31983 | ** Space to hold the returned string is obtained from malloc. |
| 31690 | 31984 | */ |
| | @@ -31969,10 +32263,11 @@ |
| 31969 | 32263 | */ |
| 31970 | 32264 | /* |
| 31971 | 32265 | ** WindowsCE does not have a localtime() function. So create a |
| 31972 | 32266 | ** substitute. |
| 31973 | 32267 | */ |
| 32268 | +/* #include <time.h> */ |
| 31974 | 32269 | struct tm *__cdecl localtime(const time_t *t) |
| 31975 | 32270 | { |
| 31976 | 32271 | static struct tm y; |
| 31977 | 32272 | FILETIME uTm, lTm; |
| 31978 | 32273 | SYSTEMTIME pTm; |
| | @@ -32860,15 +33155,22 @@ |
| 32860 | 33155 | case SQLITE_FCNTL_CHUNK_SIZE: { |
| 32861 | 33156 | pFile->szChunk = *(int *)pArg; |
| 32862 | 33157 | return SQLITE_OK; |
| 32863 | 33158 | } |
| 32864 | 33159 | case SQLITE_FCNTL_SIZE_HINT: { |
| 32865 | | - sqlite3_int64 sz = *(sqlite3_int64*)pArg; |
| 32866 | | - SimulateIOErrorBenign(1); |
| 32867 | | - winTruncate(id, sz); |
| 32868 | | - SimulateIOErrorBenign(0); |
| 32869 | | - return SQLITE_OK; |
| 33160 | + winFile *pFile = (winFile*)id; |
| 33161 | + sqlite3_int64 oldSz; |
| 33162 | + int rc = winFileSize(id, &oldSz); |
| 33163 | + if( rc==SQLITE_OK ){ |
| 33164 | + sqlite3_int64 newSz = *(sqlite3_int64*)pArg; |
| 33165 | + if( newSz>oldSz ){ |
| 33166 | + SimulateIOErrorBenign(1); |
| 33167 | + rc = winTruncate(id, newSz); |
| 33168 | + SimulateIOErrorBenign(0); |
| 33169 | + } |
| 33170 | + } |
| 33171 | + return rc; |
| 32870 | 33172 | } |
| 32871 | 33173 | case SQLITE_FCNTL_PERSIST_WAL: { |
| 32872 | 33174 | int bPersist = *(int*)pArg; |
| 32873 | 33175 | if( bPersist<0 ){ |
| 32874 | 33176 | *(int*)pArg = pFile->bPersistWal; |
| | @@ -35473,10 +35775,13 @@ |
| 35473 | 35775 | typedef struct PCache1 PCache1; |
| 35474 | 35776 | typedef struct PgHdr1 PgHdr1; |
| 35475 | 35777 | typedef struct PgFreeslot PgFreeslot; |
| 35476 | 35778 | typedef struct PGroup PGroup; |
| 35477 | 35779 | |
| 35780 | +typedef struct PGroupBlock PGroupBlock; |
| 35781 | +typedef struct PGroupBlockList PGroupBlockList; |
| 35782 | + |
| 35478 | 35783 | /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set |
| 35479 | 35784 | ** of one or more PCaches that are able to recycle each others unpinned |
| 35480 | 35785 | ** pages when they are under memory pressure. A PGroup is an instance of |
| 35481 | 35786 | ** the following object. |
| 35482 | 35787 | ** |
| | @@ -35502,12 +35807,70 @@ |
| 35502 | 35807 | int nMaxPage; /* Sum of nMax for purgeable caches */ |
| 35503 | 35808 | int nMinPage; /* Sum of nMin for purgeable caches */ |
| 35504 | 35809 | int mxPinned; /* nMaxpage + 10 - nMinPage */ |
| 35505 | 35810 | int nCurrentPage; /* Number of purgeable pages allocated */ |
| 35506 | 35811 | PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ |
| 35812 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 35813 | + int isBusy; /* Do not run ReleaseMemory() if true */ |
| 35814 | + PGroupBlockList *pBlockList; /* List of block-lists for this group */ |
| 35815 | +#endif |
| 35507 | 35816 | }; |
| 35508 | 35817 | |
| 35818 | +/* |
| 35819 | +** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built, |
| 35820 | +** each PGroup structure has a linked list of the the following starting |
| 35821 | +** at PGroup.pBlockList. There is one entry for each distinct page-size |
| 35822 | +** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes |
| 35823 | +** etc.). Variable PGroupBlockList.nByte is set to the actual allocation |
| 35824 | +** size requested by each pcache, which is the database page-size plus |
| 35825 | +** the various header structures used by the pcache, pager and btree layers. |
| 35826 | +** Usually around (pgsz+200) bytes. |
| 35827 | +** |
| 35828 | +** This size (pgsz+200) bytes is not allocated efficiently by some |
| 35829 | +** implementations of malloc. In particular, some implementations are only |
| 35830 | +** able to allocate blocks of memory chunks of 2^N bytes, where N is some |
| 35831 | +** integer value. Since the page-size is a power of 2, this means we |
| 35832 | +** end up wasting (pgsz-200) bytes in each allocation. |
| 35833 | +** |
| 35834 | +** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks |
| 35835 | +** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes |
| 35836 | +** are requested from malloc allocator. After a block is returned, |
| 35837 | +** sqlite3MallocSize() is used to determine how many (pgsz+200) byte |
| 35838 | +** allocations can fit in the space returned by malloc(). This value may |
| 35839 | +** be more than M. |
| 35840 | +** |
| 35841 | +** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry |
| 35842 | +** contains the number of allocations that will fit in the aData[] space. |
| 35843 | +** nEntry is limited to the number of bits in bitmask mUsed. If a slot |
| 35844 | +** within aData is in use, the corresponding bit in mUsed is set. Thus |
| 35845 | +** when (mUsed+1==(1 << nEntry)) the block is completely full. |
| 35846 | +** |
| 35847 | +** Each time a slot within a block is freed, the block is moved to the start |
| 35848 | +** of the linked-list. And if a block becomes completely full, then it is |
| 35849 | +** moved to the end of the list. As a result, when searching for a free |
| 35850 | +** slot, only the first block in the list need be examined. If it is full, |
| 35851 | +** then it is guaranteed that all blocks are full. |
| 35852 | +*/ |
| 35853 | +struct PGroupBlockList { |
| 35854 | + int nByte; /* Size of each allocation in bytes */ |
| 35855 | + PGroupBlock *pFirst; /* First PGroupBlock in list */ |
| 35856 | + PGroupBlock *pLast; /* Last PGroupBlock in list */ |
| 35857 | + PGroupBlockList *pNext; /* Next block-list attached to group */ |
| 35858 | +}; |
| 35859 | + |
| 35860 | +struct PGroupBlock { |
| 35861 | + Bitmask mUsed; /* Mask of used slots */ |
| 35862 | + int nEntry; /* Maximum number of allocations in aData[] */ |
| 35863 | + u8 *aData; /* Pointer to data block */ |
| 35864 | + PGroupBlock *pNext; /* Next PGroupBlock in list */ |
| 35865 | + PGroupBlock *pPrev; /* Previous PGroupBlock in list */ |
| 35866 | + PGroupBlockList *pList; /* Owner list */ |
| 35867 | +}; |
| 35868 | + |
| 35869 | +/* Minimum value for PGroupBlock.nEntry */ |
| 35870 | +#define PAGECACHE_BLOCKALLOC_MINENTRY 15 |
| 35871 | + |
| 35509 | 35872 | /* Each page cache is an instance of the following object. Every |
| 35510 | 35873 | ** open database file (including each in-memory database and each |
| 35511 | 35874 | ** temporary or transient database) has a single page cache which |
| 35512 | 35875 | ** is an instance of this object. |
| 35513 | 35876 | ** |
| | @@ -35606,10 +35969,21 @@ |
| 35606 | 35969 | ** |
| 35607 | 35970 | ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X ); |
| 35608 | 35971 | */ |
| 35609 | 35972 | #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage) |
| 35610 | 35973 | #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage) |
| 35974 | + |
| 35975 | +/* |
| 35976 | +** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve |
| 35977 | +** a PGroupBlock pointer based on a pointer to a page buffer. |
| 35978 | +*/ |
| 35979 | +#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \ |
| 35980 | + ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock ) |
| 35981 | + |
| 35982 | +#define PAGE_GET_BLOCKPTR(pCache, pPg) \ |
| 35983 | + ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) ) |
| 35984 | + |
| 35611 | 35985 | |
| 35612 | 35986 | /* |
| 35613 | 35987 | ** Macros to enter and leave the PCache LRU mutex. |
| 35614 | 35988 | */ |
| 35615 | 35989 | #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) |
| | @@ -35732,25 +36106,159 @@ |
| 35732 | 36106 | return iSize; |
| 35733 | 36107 | } |
| 35734 | 36108 | } |
| 35735 | 36109 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 35736 | 36110 | |
| 36111 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36112 | +/* |
| 36113 | +** The block pBlock belongs to list pList but is not currently linked in. |
| 36114 | +** Insert it into the start of the list. |
| 36115 | +*/ |
| 36116 | +static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){ |
| 36117 | + pBlock->pPrev = 0; |
| 36118 | + pBlock->pNext = pList->pFirst; |
| 36119 | + pList->pFirst = pBlock; |
| 36120 | + if( pBlock->pNext ){ |
| 36121 | + pBlock->pNext->pPrev = pBlock; |
| 36122 | + }else{ |
| 36123 | + assert( pList->pLast==0 ); |
| 36124 | + pList->pLast = pBlock; |
| 36125 | + } |
| 36126 | +} |
| 36127 | + |
| 36128 | +/* |
| 36129 | +** If there are no blocks in the list headed by pList, remove pList |
| 36130 | +** from the pGroup->pBlockList list and free it with sqlite3_free(). |
| 36131 | +*/ |
| 36132 | +static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){ |
| 36133 | + assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36134 | + if( pList->pFirst==0 ){ |
| 36135 | + PGroupBlockList **pp; |
| 36136 | + for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext); |
| 36137 | + *pp = (*pp)->pNext; |
| 36138 | + sqlite3_free(pList); |
| 36139 | + } |
| 36140 | +} |
| 36141 | +#endif /* SQLITE_PAGECACHE_BLOCKALLOC */ |
| 36142 | + |
| 35737 | 36143 | /* |
| 35738 | 36144 | ** Allocate a new page object initially associated with cache pCache. |
| 35739 | 36145 | */ |
| 35740 | 36146 | static PgHdr1 *pcache1AllocPage(PCache1 *pCache){ |
| 35741 | 36147 | int nByte = sizeof(PgHdr1) + pCache->szPage; |
| 35742 | | - void *pPg = pcache1Alloc(nByte); |
| 36148 | + void *pPg = 0; |
| 35743 | 36149 | PgHdr1 *p; |
| 36150 | + |
| 36151 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36152 | + PGroup *pGroup = pCache->pGroup; |
| 36153 | + PGroupBlockList *pList; |
| 36154 | + PGroupBlock *pBlock; |
| 36155 | + int i; |
| 36156 | + |
| 36157 | + nByte += sizeof(PGroupBlockList *); |
| 36158 | + nByte = ROUND8(nByte); |
| 36159 | + |
| 36160 | + for(pList=pGroup->pBlockList; pList; pList=pList->pNext){ |
| 36161 | + if( pList->nByte==nByte ) break; |
| 36162 | + } |
| 36163 | + if( pList==0 ){ |
| 36164 | + PGroupBlockList *pNew; |
| 36165 | + assert( pGroup->isBusy==0 ); |
| 36166 | + assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36167 | + pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */ |
| 36168 | + pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList)); |
| 36169 | + pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */ |
| 36170 | + if( pNew==0 ){ |
| 36171 | + /* malloc() failure. Return early. */ |
| 36172 | + return 0; |
| 36173 | + } |
| 36174 | +#ifdef SQLITE_DEBUG |
| 36175 | + for(pList=pGroup->pBlockList; pList; pList=pList->pNext){ |
| 36176 | + assert( pList->nByte!=nByte ); |
| 36177 | + } |
| 36178 | +#endif |
| 36179 | + pNew->nByte = nByte; |
| 36180 | + pNew->pNext = pGroup->pBlockList; |
| 36181 | + pGroup->pBlockList = pNew; |
| 36182 | + pList = pNew; |
| 36183 | + } |
| 36184 | + |
| 36185 | + pBlock = pList->pFirst; |
| 36186 | + if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){ |
| 36187 | + int sz; |
| 36188 | + |
| 36189 | + /* Allocate a new block. Try to allocate enough space for the PGroupBlock |
| 36190 | + ** structure and MINENTRY allocations of nByte bytes each. If the |
| 36191 | + ** allocator returns more memory than requested, then more than MINENTRY |
| 36192 | + ** allocations may fit in it. */ |
| 36193 | + assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36194 | + pcache1LeaveMutex(pCache->pGroup); |
| 36195 | + sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte; |
| 36196 | + pBlock = (PGroupBlock *)sqlite3Malloc(sz); |
| 36197 | + pcache1EnterMutex(pCache->pGroup); |
| 36198 | + |
| 36199 | + if( !pBlock ){ |
| 36200 | + freeListIfEmpty(pGroup, pList); |
| 36201 | + return 0; |
| 36202 | + } |
| 36203 | + pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte; |
| 36204 | + if( pBlock->nEntry>=BMS ){ |
| 36205 | + pBlock->nEntry = BMS-1; |
| 36206 | + } |
| 36207 | + pBlock->pList = pList; |
| 36208 | + pBlock->mUsed = 0; |
| 36209 | + pBlock->aData = (u8 *)&pBlock[1]; |
| 36210 | + addBlockToList(pList, pBlock); |
| 36211 | + |
| 36212 | + sz = sqlite3MallocSize(pBlock); |
| 36213 | + sqlite3_mutex_enter(pcache1.mutex); |
| 36214 | + sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 36215 | + sqlite3_mutex_leave(pcache1.mutex); |
| 36216 | + } |
| 36217 | + |
| 36218 | + for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){ |
| 36219 | + if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){ |
| 36220 | + pBlock->mUsed |= ((Bitmask)1<<i); |
| 36221 | + pPg = (void *)&pBlock->aData[pList->nByte * i]; |
| 36222 | + } |
| 36223 | + } |
| 36224 | + assert( pPg ); |
| 36225 | + PAGE_SET_BLOCKPTR(pCache, pPg, pBlock); |
| 36226 | + |
| 36227 | + /* If the block is now full, shift it to the end of the list */ |
| 36228 | + if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){ |
| 36229 | + assert( pList->pFirst==pBlock ); |
| 36230 | + assert( pBlock->pPrev==0 ); |
| 36231 | + assert( pList->pLast->pNext==0 ); |
| 36232 | + pList->pFirst = pBlock->pNext; |
| 36233 | + pList->pFirst->pPrev = 0; |
| 36234 | + pBlock->pPrev = pList->pLast; |
| 36235 | + pBlock->pNext = 0; |
| 36236 | + pList->pLast->pNext = pBlock; |
| 36237 | + pList->pLast = pBlock; |
| 36238 | + } |
| 36239 | + p = PAGE_TO_PGHDR1(pCache, pPg); |
| 36240 | + if( pCache->bPurgeable ){ |
| 36241 | + pCache->pGroup->nCurrentPage++; |
| 36242 | + } |
| 36243 | +#else |
| 36244 | + /* The group mutex must be released before pcache1Alloc() is called. This |
| 36245 | + ** is because it may call sqlite3_release_memory(), which assumes that |
| 36246 | + ** this mutex is not held. */ |
| 36247 | + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 36248 | + pcache1LeaveMutex(pCache->pGroup); |
| 36249 | + pPg = pcache1Alloc(nByte); |
| 36250 | + pcache1EnterMutex(pCache->pGroup); |
| 35744 | 36251 | if( pPg ){ |
| 35745 | 36252 | p = PAGE_TO_PGHDR1(pCache, pPg); |
| 35746 | 36253 | if( pCache->bPurgeable ){ |
| 35747 | 36254 | pCache->pGroup->nCurrentPage++; |
| 35748 | 36255 | } |
| 35749 | 36256 | }else{ |
| 35750 | 36257 | p = 0; |
| 35751 | 36258 | } |
| 36259 | +#endif |
| 35752 | 36260 | return p; |
| 35753 | 36261 | } |
| 35754 | 36262 | |
| 35755 | 36263 | /* |
| 35756 | 36264 | ** Free a page object allocated by pcache1AllocPage(). |
| | @@ -35760,14 +36268,56 @@ |
| 35760 | 36268 | ** with a NULL pointer, so we mark the NULL test with ALWAYS(). |
| 35761 | 36269 | */ |
| 35762 | 36270 | static void pcache1FreePage(PgHdr1 *p){ |
| 35763 | 36271 | if( ALWAYS(p) ){ |
| 35764 | 36272 | PCache1 *pCache = p->pCache; |
| 36273 | + void *pPg = PGHDR1_TO_PAGE(p); |
| 36274 | + |
| 36275 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36276 | + PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg); |
| 36277 | + PGroupBlockList *pList = pBlock->pList; |
| 36278 | + int i = ((u8 *)pPg - pBlock->aData) / pList->nByte; |
| 36279 | + |
| 36280 | + assert( pPg==(void *)&pBlock->aData[i*pList->nByte] ); |
| 36281 | + assert( pBlock->mUsed & ((Bitmask)1<<i) ); |
| 36282 | + pBlock->mUsed &= ~((Bitmask)1<<i); |
| 36283 | + |
| 36284 | + /* Remove the block from the list. If it is completely empty, free it. |
| 36285 | + ** Or if it is not completely empty, re-insert it at the start of the |
| 36286 | + ** list. */ |
| 36287 | + if( pList->pFirst==pBlock ){ |
| 36288 | + pList->pFirst = pBlock->pNext; |
| 36289 | + if( pList->pFirst ) pList->pFirst->pPrev = 0; |
| 36290 | + }else{ |
| 36291 | + pBlock->pPrev->pNext = pBlock->pNext; |
| 36292 | + } |
| 36293 | + if( pList->pLast==pBlock ){ |
| 36294 | + pList->pLast = pBlock->pPrev; |
| 36295 | + if( pList->pLast ) pList->pLast->pNext = 0; |
| 36296 | + }else{ |
| 36297 | + pBlock->pNext->pPrev = pBlock->pPrev; |
| 36298 | + } |
| 36299 | + |
| 36300 | + if( pBlock->mUsed==0 ){ |
| 36301 | + PGroup *pGroup = p->pCache->pGroup; |
| 36302 | + |
| 36303 | + int sz = sqlite3MallocSize(pBlock); |
| 36304 | + sqlite3_mutex_enter(pcache1.mutex); |
| 36305 | + sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz); |
| 36306 | + sqlite3_mutex_leave(pcache1.mutex); |
| 36307 | + freeListIfEmpty(pGroup, pList); |
| 36308 | + sqlite3_free(pBlock); |
| 36309 | + }else{ |
| 36310 | + addBlockToList(pList, pBlock); |
| 36311 | + } |
| 36312 | +#else |
| 36313 | + assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); |
| 36314 | + pcache1Free(pPg); |
| 36315 | +#endif |
| 35765 | 36316 | if( pCache->bPurgeable ){ |
| 35766 | 36317 | pCache->pGroup->nCurrentPage--; |
| 35767 | 36318 | } |
| 35768 | | - pcache1Free(PGHDR1_TO_PAGE(p)); |
| 35769 | 36319 | } |
| 35770 | 36320 | } |
| 35771 | 36321 | |
| 35772 | 36322 | /* |
| 35773 | 36323 | ** Malloc function used by SQLite to obtain space from the buffer configured |
| | @@ -36201,13 +36751,11 @@ |
| 36201 | 36751 | /* Step 5. If a usable page buffer has still not been found, |
| 36202 | 36752 | ** attempt to allocate a new one. |
| 36203 | 36753 | */ |
| 36204 | 36754 | if( !pPage ){ |
| 36205 | 36755 | if( createFlag==1 ) sqlite3BeginBenignMalloc(); |
| 36206 | | - pcache1LeaveMutex(pGroup); |
| 36207 | 36756 | pPage = pcache1AllocPage(pCache); |
| 36208 | | - pcache1EnterMutex(pGroup); |
| 36209 | 36757 | if( createFlag==1 ) sqlite3EndBenignMalloc(); |
| 36210 | 36758 | } |
| 36211 | 36759 | |
| 36212 | 36760 | if( pPage ){ |
| 36213 | 36761 | unsigned int h = iKey % pCache->nHash; |
| | @@ -36373,10 +36921,13 @@ |
| 36373 | 36921 | ** been released, the function returns. The return value is the total number |
| 36374 | 36922 | ** of bytes of memory released. |
| 36375 | 36923 | */ |
| 36376 | 36924 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 36377 | 36925 | int nFree = 0; |
| 36926 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36927 | + if( pcache1.grp.isBusy ) return 0; |
| 36928 | +#endif |
| 36378 | 36929 | assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 36379 | 36930 | assert( sqlite3_mutex_notheld(pcache1.mutex) ); |
| 36380 | 36931 | if( pcache1.pStart==0 ){ |
| 36381 | 36932 | PgHdr1 *p; |
| 36382 | 36933 | pcache1EnterMutex(&pcache1.grp); |
| | @@ -37585,10 +38136,12 @@ |
| 37585 | 38136 | u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */ |
| 37586 | 38137 | u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ |
| 37587 | 38138 | u8 tempFile; /* zFilename is a temporary file */ |
| 37588 | 38139 | u8 readOnly; /* True for a read-only database */ |
| 37589 | 38140 | u8 memDb; /* True to inhibit all file I/O */ |
| 38141 | + u8 hasSeenStress; /* pagerStress() called one or more times */ |
| 38142 | + u8 isSorter; /* True for a PAGER_SORTER */ |
| 37590 | 38143 | |
| 37591 | 38144 | /************************************************************************** |
| 37592 | 38145 | ** The following block contains those class members that change during |
| 37593 | 38146 | ** routine opertion. Class members not in this block are either fixed |
| 37594 | 38147 | ** when the pager is first created or else only change when there is a |
| | @@ -37807,10 +38360,19 @@ |
| 37807 | 38360 | || p->journalMode==PAGER_JOURNALMODE_MEMORY |
| 37808 | 38361 | ); |
| 37809 | 38362 | assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN ); |
| 37810 | 38363 | assert( pagerUseWal(p)==0 ); |
| 37811 | 38364 | } |
| 38365 | + |
| 38366 | + /* A sorter is a temp file that never spills to disk and always has |
| 38367 | + ** the doNotSpill flag set |
| 38368 | + */ |
| 38369 | + if( p->isSorter ){ |
| 38370 | + assert( p->tempFile ); |
| 38371 | + assert( p->doNotSpill ); |
| 38372 | + assert( p->fd->pMethods==0 ); |
| 38373 | + } |
| 37812 | 38374 | |
| 37813 | 38375 | /* If changeCountDone is set, a RESERVED lock or greater must be held |
| 37814 | 38376 | ** on the file. |
| 37815 | 38377 | */ |
| 37816 | 38378 | assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK ); |
| | @@ -40704,10 +41266,11 @@ |
| 40704 | 41266 | ** to the caller. |
| 40705 | 41267 | */ |
| 40706 | 41268 | SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){ |
| 40707 | 41269 | u8 *pTmp = (u8 *)pPager->pTmpSpace; |
| 40708 | 41270 | |
| 41271 | + assert( assert_pager_state(pPager) ); |
| 40709 | 41272 | disable_simulated_io_errors(); |
| 40710 | 41273 | sqlite3BeginBenignMalloc(); |
| 40711 | 41274 | /* pPager->errCode = 0; */ |
| 40712 | 41275 | pPager->exclusiveMode = 0; |
| 40713 | 41276 | #ifndef SQLITE_OMIT_WAL |
| | @@ -41138,10 +41701,11 @@ |
| 41138 | 41701 | ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1 |
| 41139 | 41702 | ** while in the error state, hence it is impossible for this routine to |
| 41140 | 41703 | ** be called in the error state. Nevertheless, we include a NEVER() |
| 41141 | 41704 | ** test for the error state as a safeguard against future changes. |
| 41142 | 41705 | */ |
| 41706 | + pPager->hasSeenStress = 1; |
| 41143 | 41707 | if( NEVER(pPager->errCode) ) return SQLITE_OK; |
| 41144 | 41708 | if( pPager->doNotSpill ) return SQLITE_OK; |
| 41145 | 41709 | if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){ |
| 41146 | 41710 | return SQLITE_OK; |
| 41147 | 41711 | } |
| | @@ -41509,10 +42073,16 @@ |
| 41509 | 42073 | } |
| 41510 | 42074 | /* pPager->xBusyHandler = 0; */ |
| 41511 | 42075 | /* pPager->pBusyHandlerArg = 0; */ |
| 41512 | 42076 | pPager->xReiniter = xReinit; |
| 41513 | 42077 | /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ |
| 42078 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 42079 | + if( flags & PAGER_SORTER ){ |
| 42080 | + pPager->doNotSpill = 1; |
| 42081 | + pPager->isSorter = 1; |
| 42082 | + } |
| 42083 | +#endif |
| 41514 | 42084 | |
| 41515 | 42085 | *ppPager = pPager; |
| 41516 | 42086 | return SQLITE_OK; |
| 41517 | 42087 | } |
| 41518 | 42088 | |
| | @@ -43052,10 +43622,21 @@ |
| 43052 | 43622 | ** Return true if this is an in-memory pager. |
| 43053 | 43623 | */ |
| 43054 | 43624 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){ |
| 43055 | 43625 | return MEMDB; |
| 43056 | 43626 | } |
| 43627 | + |
| 43628 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 43629 | +/* |
| 43630 | +** Return true if the pager has seen a pagerStress callback. |
| 43631 | +*/ |
| 43632 | +SQLITE_PRIVATE int sqlite3PagerUnderStress(Pager *pPager){ |
| 43633 | + assert( pPager->isSorter ); |
| 43634 | + assert( pPager->doNotSpill ); |
| 43635 | + return pPager->hasSeenStress; |
| 43636 | +} |
| 43637 | +#endif |
| 43057 | 43638 | |
| 43058 | 43639 | /* |
| 43059 | 43640 | ** Check that there are at least nSavepoint savepoints open. If there are |
| 43060 | 43641 | ** currently less than nSavepoints open, then open one or more savepoints |
| 43061 | 43642 | ** to make up the difference. If the number of savepoints is already |
| | @@ -49421,15 +50002,26 @@ |
| 49421 | 50002 | assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); |
| 49422 | 50003 | |
| 49423 | 50004 | /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ |
| 49424 | 50005 | assert( (flags & BTREE_SINGLE)==0 || isTempDb ); |
| 49425 | 50006 | |
| 50007 | + /* The BTREE_SORTER flag is only used if SQLITE_OMIT_MERGE_SORT is undef */ |
| 50008 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 50009 | + assert( (flags & BTREE_SORTER)==0 ); |
| 50010 | +#endif |
| 50011 | + |
| 50012 | + /* BTREE_SORTER is always on a BTREE_SINGLE, BTREE_OMIT_JOURNAL */ |
| 50013 | + assert( (flags & BTREE_SORTER)==0 || |
| 50014 | + (flags & (BTREE_SINGLE|BTREE_OMIT_JOURNAL)) |
| 50015 | + ==(BTREE_SINGLE|BTREE_OMIT_JOURNAL) ); |
| 50016 | + |
| 49426 | 50017 | if( db->flags & SQLITE_NoReadlock ){ |
| 49427 | 50018 | flags |= BTREE_NO_READLOCK; |
| 49428 | 50019 | } |
| 49429 | 50020 | if( isMemdb ){ |
| 49430 | 50021 | flags |= BTREE_MEMORY; |
| 50022 | + flags &= ~BTREE_SORTER; |
| 49431 | 50023 | } |
| 49432 | 50024 | if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ |
| 49433 | 50025 | vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; |
| 49434 | 50026 | } |
| 49435 | 50027 | p = sqlite3MallocZero(sizeof(Btree)); |
| | @@ -51155,11 +51747,12 @@ |
| 51155 | 51747 | |
| 51156 | 51748 | if( NEVER(wrFlag && pBt->readOnly) ){ |
| 51157 | 51749 | return SQLITE_READONLY; |
| 51158 | 51750 | } |
| 51159 | 51751 | if( iTable==1 && btreePagecount(pBt)==0 ){ |
| 51160 | | - return SQLITE_EMPTY; |
| 51752 | + assert( wrFlag==0 ); |
| 51753 | + iTable = 0; |
| 51161 | 51754 | } |
| 51162 | 51755 | |
| 51163 | 51756 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 51164 | 51757 | ** variables and link the cursor into the BtShared list. */ |
| 51165 | 51758 | pCur->pgnoRoot = (Pgno)iTable; |
| | @@ -51909,10 +52502,13 @@ |
| 51909 | 52502 | int i; |
| 51910 | 52503 | for(i=1; i<=pCur->iPage; i++){ |
| 51911 | 52504 | releasePage(pCur->apPage[i]); |
| 51912 | 52505 | } |
| 51913 | 52506 | pCur->iPage = 0; |
| 52507 | + }else if( pCur->pgnoRoot==0 ){ |
| 52508 | + pCur->eState = CURSOR_INVALID; |
| 52509 | + return SQLITE_OK; |
| 51914 | 52510 | }else{ |
| 51915 | 52511 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); |
| 51916 | 52512 | if( rc!=SQLITE_OK ){ |
| 51917 | 52513 | pCur->eState = CURSOR_INVALID; |
| 51918 | 52514 | return rc; |
| | @@ -52018,11 +52614,11 @@ |
| 52018 | 52614 | assert( cursorHoldsMutex(pCur) ); |
| 52019 | 52615 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 52020 | 52616 | rc = moveToRoot(pCur); |
| 52021 | 52617 | if( rc==SQLITE_OK ){ |
| 52022 | 52618 | if( pCur->eState==CURSOR_INVALID ){ |
| 52023 | | - assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52619 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52024 | 52620 | *pRes = 1; |
| 52025 | 52621 | }else{ |
| 52026 | 52622 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
| 52027 | 52623 | *pRes = 0; |
| 52028 | 52624 | rc = moveToLeftmost(pCur); |
| | @@ -52057,11 +52653,11 @@ |
| 52057 | 52653 | } |
| 52058 | 52654 | |
| 52059 | 52655 | rc = moveToRoot(pCur); |
| 52060 | 52656 | if( rc==SQLITE_OK ){ |
| 52061 | 52657 | if( CURSOR_INVALID==pCur->eState ){ |
| 52062 | | - assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52658 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52063 | 52659 | *pRes = 1; |
| 52064 | 52660 | }else{ |
| 52065 | 52661 | assert( pCur->eState==CURSOR_VALID ); |
| 52066 | 52662 | *pRes = 0; |
| 52067 | 52663 | rc = moveToRightmost(pCur); |
| | @@ -52130,16 +52726,16 @@ |
| 52130 | 52726 | |
| 52131 | 52727 | rc = moveToRoot(pCur); |
| 52132 | 52728 | if( rc ){ |
| 52133 | 52729 | return rc; |
| 52134 | 52730 | } |
| 52135 | | - assert( pCur->apPage[pCur->iPage] ); |
| 52136 | | - assert( pCur->apPage[pCur->iPage]->isInit ); |
| 52137 | | - assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID ); |
| 52731 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] ); |
| 52732 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit ); |
| 52733 | + assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 ); |
| 52138 | 52734 | if( pCur->eState==CURSOR_INVALID ){ |
| 52139 | 52735 | *pRes = -1; |
| 52140 | | - assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52736 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52141 | 52737 | return SQLITE_OK; |
| 52142 | 52738 | } |
| 52143 | 52739 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
| 52144 | 52740 | for(;;){ |
| 52145 | 52741 | int lwr, upr, idx; |
| | @@ -54964,13 +55560,20 @@ |
| 54964 | 55560 | releasePage(pPage); |
| 54965 | 55561 | } |
| 54966 | 55562 | return rc; |
| 54967 | 55563 | } |
| 54968 | 55564 | SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 55565 | + BtShared *pBt = p->pBt; |
| 54969 | 55566 | int rc; |
| 54970 | 55567 | sqlite3BtreeEnter(p); |
| 54971 | | - rc = btreeDropTable(p, iTable, piMoved); |
| 55568 | + if( (pBt->openFlags&BTREE_SINGLE) ){ |
| 55569 | + pBt->nPage = 0; |
| 55570 | + sqlite3PagerTruncateImage(pBt->pPager, 1); |
| 55571 | + rc = newDatabase(pBt); |
| 55572 | + }else{ |
| 55573 | + rc = btreeDropTable(p, iTable, piMoved); |
| 55574 | + } |
| 54972 | 55575 | sqlite3BtreeLeave(p); |
| 54973 | 55576 | return rc; |
| 54974 | 55577 | } |
| 54975 | 55578 | |
| 54976 | 55579 | |
| | @@ -55045,10 +55648,15 @@ |
| 55045 | 55648 | ** corruption) an SQLite error code is returned. |
| 55046 | 55649 | */ |
| 55047 | 55650 | SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ |
| 55048 | 55651 | i64 nEntry = 0; /* Value to return in *pnEntry */ |
| 55049 | 55652 | int rc; /* Return code */ |
| 55653 | + |
| 55654 | + if( pCur->pgnoRoot==0 ){ |
| 55655 | + *pnEntry = 0; |
| 55656 | + return SQLITE_OK; |
| 55657 | + } |
| 55050 | 55658 | rc = moveToRoot(pCur); |
| 55051 | 55659 | |
| 55052 | 55660 | /* Unless an error occurs, the following loop runs one iteration for each |
| 55053 | 55661 | ** page in the B-Tree structure (not including overflow pages). |
| 55054 | 55662 | */ |
| | @@ -55829,11 +56437,10 @@ |
| 55829 | 56437 | */ |
| 55830 | 56438 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ |
| 55831 | 56439 | BtShared *pBt = pBtree->pBt; |
| 55832 | 56440 | int rc; /* Return code */ |
| 55833 | 56441 | |
| 55834 | | - assert( pBtree->inTrans==TRANS_NONE ); |
| 55835 | 56442 | assert( iVersion==1 || iVersion==2 ); |
| 55836 | 56443 | |
| 55837 | 56444 | /* If setting the version fields to 1, do not automatically open the |
| 55838 | 56445 | ** WAL connection, even if the version fields are currently set to 2. |
| 55839 | 56446 | */ |
| | @@ -56268,106 +56875,110 @@ |
| 56268 | 56875 | /* Update the schema version field in the destination database. This |
| 56269 | 56876 | ** is to make sure that the schema-version really does change in |
| 56270 | 56877 | ** the case where the source and destination databases have the |
| 56271 | 56878 | ** same schema version. |
| 56272 | 56879 | */ |
| 56273 | | - if( rc==SQLITE_DONE |
| 56274 | | - && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK |
| 56275 | | - ){ |
| 56276 | | - int nDestTruncate; |
| 56277 | | - |
| 56278 | | - if( p->pDestDb ){ |
| 56279 | | - sqlite3ResetInternalSchema(p->pDestDb, -1); |
| 56280 | | - } |
| 56281 | | - |
| 56282 | | - /* Set nDestTruncate to the final number of pages in the destination |
| 56283 | | - ** database. The complication here is that the destination page |
| 56284 | | - ** size may be different to the source page size. |
| 56285 | | - ** |
| 56286 | | - ** If the source page size is smaller than the destination page size, |
| 56287 | | - ** round up. In this case the call to sqlite3OsTruncate() below will |
| 56288 | | - ** fix the size of the file. However it is important to call |
| 56289 | | - ** sqlite3PagerTruncateImage() here so that any pages in the |
| 56290 | | - ** destination file that lie beyond the nDestTruncate page mark are |
| 56291 | | - ** journalled by PagerCommitPhaseOne() before they are destroyed |
| 56292 | | - ** by the file truncation. |
| 56293 | | - */ |
| 56294 | | - assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) ); |
| 56295 | | - assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) ); |
| 56296 | | - if( pgszSrc<pgszDest ){ |
| 56297 | | - int ratio = pgszDest/pgszSrc; |
| 56298 | | - nDestTruncate = (nSrcPage+ratio-1)/ratio; |
| 56299 | | - if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){ |
| 56300 | | - nDestTruncate--; |
| 56301 | | - } |
| 56302 | | - }else{ |
| 56303 | | - nDestTruncate = nSrcPage * (pgszSrc/pgszDest); |
| 56304 | | - } |
| 56305 | | - sqlite3PagerTruncateImage(pDestPager, nDestTruncate); |
| 56306 | | - |
| 56307 | | - if( pgszSrc<pgszDest ){ |
| 56308 | | - /* If the source page-size is smaller than the destination page-size, |
| 56309 | | - ** two extra things may need to happen: |
| 56310 | | - ** |
| 56311 | | - ** * The destination may need to be truncated, and |
| 56312 | | - ** |
| 56313 | | - ** * Data stored on the pages immediately following the |
| 56314 | | - ** pending-byte page in the source database may need to be |
| 56315 | | - ** copied into the destination database. |
| 56316 | | - */ |
| 56317 | | - const i64 iSize = (i64)pgszSrc * (i64)nSrcPage; |
| 56318 | | - sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); |
| 56319 | | - i64 iOff; |
| 56320 | | - i64 iEnd; |
| 56321 | | - |
| 56322 | | - assert( pFile ); |
| 56323 | | - assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || ( |
| 56324 | | - nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) |
| 56325 | | - && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest |
| 56326 | | - )); |
| 56327 | | - |
| 56328 | | - /* This call ensures that all data required to recreate the original |
| 56329 | | - ** database has been stored in the journal for pDestPager and the |
| 56330 | | - ** journal synced to disk. So at this point we may safely modify |
| 56331 | | - ** the database file in any way, knowing that if a power failure |
| 56332 | | - ** occurs, the original database will be reconstructed from the |
| 56333 | | - ** journal file. */ |
| 56334 | | - rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); |
| 56335 | | - |
| 56336 | | - /* Write the extra pages and truncate the database file as required. */ |
| 56337 | | - iEnd = MIN(PENDING_BYTE + pgszDest, iSize); |
| 56338 | | - for( |
| 56339 | | - iOff=PENDING_BYTE+pgszSrc; |
| 56340 | | - rc==SQLITE_OK && iOff<iEnd; |
| 56341 | | - iOff+=pgszSrc |
| 56342 | | - ){ |
| 56343 | | - PgHdr *pSrcPg = 0; |
| 56344 | | - const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1); |
| 56345 | | - rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); |
| 56346 | | - if( rc==SQLITE_OK ){ |
| 56347 | | - u8 *zData = sqlite3PagerGetData(pSrcPg); |
| 56348 | | - rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff); |
| 56349 | | - } |
| 56350 | | - sqlite3PagerUnref(pSrcPg); |
| 56351 | | - } |
| 56352 | | - if( rc==SQLITE_OK ){ |
| 56353 | | - rc = backupTruncateFile(pFile, iSize); |
| 56354 | | - } |
| 56355 | | - |
| 56356 | | - /* Sync the database file to disk. */ |
| 56357 | | - if( rc==SQLITE_OK ){ |
| 56358 | | - rc = sqlite3PagerSync(pDestPager); |
| 56359 | | - } |
| 56360 | | - }else{ |
| 56361 | | - rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); |
| 56362 | | - } |
| 56363 | | - |
| 56364 | | - /* Finish committing the transaction to the destination database. */ |
| 56365 | | - if( SQLITE_OK==rc |
| 56366 | | - && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0)) |
| 56367 | | - ){ |
| 56368 | | - rc = SQLITE_DONE; |
| 56880 | + if( rc==SQLITE_DONE ){ |
| 56881 | + rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1); |
| 56882 | + if( rc==SQLITE_OK ){ |
| 56883 | + if( p->pDestDb ){ |
| 56884 | + sqlite3ResetInternalSchema(p->pDestDb, -1); |
| 56885 | + } |
| 56886 | + if( destMode==PAGER_JOURNALMODE_WAL ){ |
| 56887 | + rc = sqlite3BtreeSetVersion(p->pDest, 2); |
| 56888 | + } |
| 56889 | + } |
| 56890 | + if( rc==SQLITE_OK ){ |
| 56891 | + int nDestTruncate; |
| 56892 | + /* Set nDestTruncate to the final number of pages in the destination |
| 56893 | + ** database. The complication here is that the destination page |
| 56894 | + ** size may be different to the source page size. |
| 56895 | + ** |
| 56896 | + ** If the source page size is smaller than the destination page size, |
| 56897 | + ** round up. In this case the call to sqlite3OsTruncate() below will |
| 56898 | + ** fix the size of the file. However it is important to call |
| 56899 | + ** sqlite3PagerTruncateImage() here so that any pages in the |
| 56900 | + ** destination file that lie beyond the nDestTruncate page mark are |
| 56901 | + ** journalled by PagerCommitPhaseOne() before they are destroyed |
| 56902 | + ** by the file truncation. |
| 56903 | + */ |
| 56904 | + assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) ); |
| 56905 | + assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) ); |
| 56906 | + if( pgszSrc<pgszDest ){ |
| 56907 | + int ratio = pgszDest/pgszSrc; |
| 56908 | + nDestTruncate = (nSrcPage+ratio-1)/ratio; |
| 56909 | + if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){ |
| 56910 | + nDestTruncate--; |
| 56911 | + } |
| 56912 | + }else{ |
| 56913 | + nDestTruncate = nSrcPage * (pgszSrc/pgszDest); |
| 56914 | + } |
| 56915 | + sqlite3PagerTruncateImage(pDestPager, nDestTruncate); |
| 56916 | + |
| 56917 | + if( pgszSrc<pgszDest ){ |
| 56918 | + /* If the source page-size is smaller than the destination page-size, |
| 56919 | + ** two extra things may need to happen: |
| 56920 | + ** |
| 56921 | + ** * The destination may need to be truncated, and |
| 56922 | + ** |
| 56923 | + ** * Data stored on the pages immediately following the |
| 56924 | + ** pending-byte page in the source database may need to be |
| 56925 | + ** copied into the destination database. |
| 56926 | + */ |
| 56927 | + const i64 iSize = (i64)pgszSrc * (i64)nSrcPage; |
| 56928 | + sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); |
| 56929 | + i64 iOff; |
| 56930 | + i64 iEnd; |
| 56931 | + |
| 56932 | + assert( pFile ); |
| 56933 | + assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || ( |
| 56934 | + nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) |
| 56935 | + && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest |
| 56936 | + )); |
| 56937 | + |
| 56938 | + /* This call ensures that all data required to recreate the original |
| 56939 | + ** database has been stored in the journal for pDestPager and the |
| 56940 | + ** journal synced to disk. So at this point we may safely modify |
| 56941 | + ** the database file in any way, knowing that if a power failure |
| 56942 | + ** occurs, the original database will be reconstructed from the |
| 56943 | + ** journal file. */ |
| 56944 | + rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); |
| 56945 | + |
| 56946 | + /* Write the extra pages and truncate the database file as required */ |
| 56947 | + iEnd = MIN(PENDING_BYTE + pgszDest, iSize); |
| 56948 | + for( |
| 56949 | + iOff=PENDING_BYTE+pgszSrc; |
| 56950 | + rc==SQLITE_OK && iOff<iEnd; |
| 56951 | + iOff+=pgszSrc |
| 56952 | + ){ |
| 56953 | + PgHdr *pSrcPg = 0; |
| 56954 | + const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1); |
| 56955 | + rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); |
| 56956 | + if( rc==SQLITE_OK ){ |
| 56957 | + u8 *zData = sqlite3PagerGetData(pSrcPg); |
| 56958 | + rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff); |
| 56959 | + } |
| 56960 | + sqlite3PagerUnref(pSrcPg); |
| 56961 | + } |
| 56962 | + if( rc==SQLITE_OK ){ |
| 56963 | + rc = backupTruncateFile(pFile, iSize); |
| 56964 | + } |
| 56965 | + |
| 56966 | + /* Sync the database file to disk. */ |
| 56967 | + if( rc==SQLITE_OK ){ |
| 56968 | + rc = sqlite3PagerSync(pDestPager); |
| 56969 | + } |
| 56970 | + }else{ |
| 56971 | + rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); |
| 56972 | + } |
| 56973 | + |
| 56974 | + /* Finish committing the transaction to the destination database. */ |
| 56975 | + if( SQLITE_OK==rc |
| 56976 | + && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0)) |
| 56977 | + ){ |
| 56978 | + rc = SQLITE_DONE; |
| 56979 | + } |
| 56369 | 56980 | } |
| 56370 | 56981 | } |
| 56371 | 56982 | |
| 56372 | 56983 | /* If bCloseTrans is true, then this function opened a read transaction |
| 56373 | 56984 | ** on the source database. Close the read transaction here. There is |
| | @@ -56831,38 +57442,32 @@ |
| 56831 | 57442 | ** invoking an external callback, free it now. Calling this function |
| 56832 | 57443 | ** does not free any Mem.zMalloc buffer. |
| 56833 | 57444 | */ |
| 56834 | 57445 | SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){ |
| 56835 | 57446 | assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); |
| 56836 | | - testcase( p->flags & MEM_Agg ); |
| 56837 | | - testcase( p->flags & MEM_Dyn ); |
| 56838 | | - testcase( p->flags & MEM_RowSet ); |
| 56839 | | - testcase( p->flags & MEM_Frame ); |
| 56840 | | - if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){ |
| 56841 | | - if( p->flags&MEM_Agg ){ |
| 56842 | | - sqlite3VdbeMemFinalize(p, p->u.pDef); |
| 56843 | | - assert( (p->flags & MEM_Agg)==0 ); |
| 56844 | | - sqlite3VdbeMemRelease(p); |
| 56845 | | - }else if( p->flags&MEM_Dyn && p->xDel ){ |
| 56846 | | - assert( (p->flags&MEM_RowSet)==0 ); |
| 56847 | | - p->xDel((void *)p->z); |
| 56848 | | - p->xDel = 0; |
| 56849 | | - }else if( p->flags&MEM_RowSet ){ |
| 56850 | | - sqlite3RowSetClear(p->u.pRowSet); |
| 56851 | | - }else if( p->flags&MEM_Frame ){ |
| 56852 | | - sqlite3VdbeMemSetNull(p); |
| 56853 | | - } |
| 57447 | + if( p->flags&MEM_Agg ){ |
| 57448 | + sqlite3VdbeMemFinalize(p, p->u.pDef); |
| 57449 | + assert( (p->flags & MEM_Agg)==0 ); |
| 57450 | + sqlite3VdbeMemRelease(p); |
| 57451 | + }else if( p->flags&MEM_Dyn && p->xDel ){ |
| 57452 | + assert( (p->flags&MEM_RowSet)==0 ); |
| 57453 | + p->xDel((void *)p->z); |
| 57454 | + p->xDel = 0; |
| 57455 | + }else if( p->flags&MEM_RowSet ){ |
| 57456 | + sqlite3RowSetClear(p->u.pRowSet); |
| 57457 | + }else if( p->flags&MEM_Frame ){ |
| 57458 | + sqlite3VdbeMemSetNull(p); |
| 56854 | 57459 | } |
| 56855 | 57460 | } |
| 56856 | 57461 | |
| 56857 | 57462 | /* |
| 56858 | 57463 | ** Release any memory held by the Mem. This may leave the Mem in an |
| 56859 | 57464 | ** inconsistent state, for example with (Mem.z==0) and |
| 56860 | 57465 | ** (Mem.type==SQLITE_TEXT). |
| 56861 | 57466 | */ |
| 56862 | 57467 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){ |
| 56863 | | - sqlite3VdbeMemReleaseExternal(p); |
| 57468 | + MemReleaseExt(p); |
| 56864 | 57469 | sqlite3DbFree(p->db, p->zMalloc); |
| 56865 | 57470 | p->z = 0; |
| 56866 | 57471 | p->zMalloc = 0; |
| 56867 | 57472 | p->xDel = 0; |
| 56868 | 57473 | } |
| | @@ -57180,11 +57785,11 @@ |
| 57180 | 57785 | ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z |
| 57181 | 57786 | ** and flags gets srcType (either MEM_Ephem or MEM_Static). |
| 57182 | 57787 | */ |
| 57183 | 57788 | SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ |
| 57184 | 57789 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
| 57185 | | - sqlite3VdbeMemReleaseExternal(pTo); |
| 57790 | + MemReleaseExt(pTo); |
| 57186 | 57791 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 57187 | 57792 | pTo->xDel = 0; |
| 57188 | 57793 | if( (pFrom->flags&MEM_Static)==0 ){ |
| 57189 | 57794 | pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); |
| 57190 | 57795 | assert( srcType==MEM_Ephem || srcType==MEM_Static ); |
| | @@ -57198,11 +57803,11 @@ |
| 57198 | 57803 | */ |
| 57199 | 57804 | SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ |
| 57200 | 57805 | int rc = SQLITE_OK; |
| 57201 | 57806 | |
| 57202 | 57807 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
| 57203 | | - sqlite3VdbeMemReleaseExternal(pTo); |
| 57808 | + MemReleaseExt(pTo); |
| 57204 | 57809 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 57205 | 57810 | pTo->flags &= ~MEM_Dyn; |
| 57206 | 57811 | |
| 57207 | 57812 | if( pTo->flags&(MEM_Str|MEM_Blob) ){ |
| 57208 | 57813 | if( 0==(pFrom->flags&MEM_Static) ){ |
| | @@ -57592,15 +58197,15 @@ |
| 57592 | 58197 | *ppVal = 0; |
| 57593 | 58198 | return SQLITE_OK; |
| 57594 | 58199 | } |
| 57595 | 58200 | op = pExpr->op; |
| 57596 | 58201 | |
| 57597 | | - /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3. |
| 58202 | + /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2. |
| 57598 | 58203 | ** The ifdef here is to enable us to achieve 100% branch test coverage even |
| 57599 | | - ** when SQLITE_ENABLE_STAT3 is omitted. |
| 58204 | + ** when SQLITE_ENABLE_STAT2 is omitted. |
| 57600 | 58205 | */ |
| 57601 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 58206 | +#ifdef SQLITE_ENABLE_STAT2 |
| 57602 | 58207 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 57603 | 58208 | #else |
| 57604 | 58209 | if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 57605 | 58210 | #endif |
| 57606 | 58211 | |
| | @@ -58153,10 +58758,16 @@ |
| 58153 | 58758 | assert( p->nOp - i >= 3 ); |
| 58154 | 58759 | assert( pOp[-1].opcode==OP_Integer ); |
| 58155 | 58760 | n = pOp[-1].p1; |
| 58156 | 58761 | if( n>nMaxArgs ) nMaxArgs = n; |
| 58157 | 58762 | #endif |
| 58763 | + }else if( opcode==OP_Next ){ |
| 58764 | + pOp->p4.xAdvance = sqlite3BtreeNext; |
| 58765 | + pOp->p4type = P4_ADVANCE; |
| 58766 | + }else if( opcode==OP_Prev ){ |
| 58767 | + pOp->p4.xAdvance = sqlite3BtreePrevious; |
| 58768 | + pOp->p4type = P4_ADVANCE; |
| 58158 | 58769 | } |
| 58159 | 58770 | |
| 58160 | 58771 | if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
| 58161 | 58772 | assert( -1-pOp->p2<p->nLabel ); |
| 58162 | 58773 | pOp->p2 = aLabel[-1-pOp->p2]; |
| | @@ -58244,37 +58855,34 @@ |
| 58244 | 58855 | ** Change the value of the P1 operand for a specific instruction. |
| 58245 | 58856 | ** This routine is useful when a large program is loaded from a |
| 58246 | 58857 | ** static array using sqlite3VdbeAddOpList but we want to make a |
| 58247 | 58858 | ** few minor changes to the program. |
| 58248 | 58859 | */ |
| 58249 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
| 58860 | +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ |
| 58250 | 58861 | assert( p!=0 ); |
| 58251 | | - assert( addr>=0 ); |
| 58252 | | - if( p->nOp>addr ){ |
| 58862 | + if( ((u32)p->nOp)>addr ){ |
| 58253 | 58863 | p->aOp[addr].p1 = val; |
| 58254 | 58864 | } |
| 58255 | 58865 | } |
| 58256 | 58866 | |
| 58257 | 58867 | /* |
| 58258 | 58868 | ** Change the value of the P2 operand for a specific instruction. |
| 58259 | 58869 | ** This routine is useful for setting a jump destination. |
| 58260 | 58870 | */ |
| 58261 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
| 58871 | +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ |
| 58262 | 58872 | assert( p!=0 ); |
| 58263 | | - assert( addr>=0 ); |
| 58264 | | - if( p->nOp>addr ){ |
| 58873 | + if( ((u32)p->nOp)>addr ){ |
| 58265 | 58874 | p->aOp[addr].p2 = val; |
| 58266 | 58875 | } |
| 58267 | 58876 | } |
| 58268 | 58877 | |
| 58269 | 58878 | /* |
| 58270 | 58879 | ** Change the value of the P3 operand for a specific instruction. |
| 58271 | 58880 | */ |
| 58272 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ |
| 58881 | +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ |
| 58273 | 58882 | assert( p!=0 ); |
| 58274 | | - assert( addr>=0 ); |
| 58275 | | - if( p->nOp>addr ){ |
| 58883 | + if( ((u32)p->nOp)>addr ){ |
| 58276 | 58884 | p->aOp[addr].p3 = val; |
| 58277 | 58885 | } |
| 58278 | 58886 | } |
| 58279 | 58887 | |
| 58280 | 58888 | /* |
| | @@ -58292,12 +58900,12 @@ |
| 58292 | 58900 | /* |
| 58293 | 58901 | ** Change the P2 operand of instruction addr so that it points to |
| 58294 | 58902 | ** the address of the next instruction to be coded. |
| 58295 | 58903 | */ |
| 58296 | 58904 | SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
| 58297 | | - assert( addr>=0 || p->db->mallocFailed ); |
| 58298 | | - if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 58905 | + assert( addr>=0 ); |
| 58906 | + sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 58299 | 58907 | } |
| 58300 | 58908 | |
| 58301 | 58909 | |
| 58302 | 58910 | /* |
| 58303 | 58911 | ** If the input FuncDef structure is ephemeral, then free it. If |
| | @@ -58661,10 +59269,14 @@ |
| 58661 | 59269 | break; |
| 58662 | 59270 | } |
| 58663 | 59271 | case P4_SUBPROGRAM: { |
| 58664 | 59272 | sqlite3_snprintf(nTemp, zTemp, "program"); |
| 58665 | 59273 | break; |
| 59274 | + } |
| 59275 | + case P4_ADVANCE: { |
| 59276 | + zTemp[0] = 0; |
| 59277 | + break; |
| 58666 | 59278 | } |
| 58667 | 59279 | default: { |
| 58668 | 59280 | zP4 = pOp->p4.z; |
| 58669 | 59281 | if( zP4==0 ){ |
| 58670 | 59282 | zP4 = zTemp; |
| | @@ -59285,10 +59897,11 @@ |
| 59285 | 59897 | */ |
| 59286 | 59898 | SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
| 59287 | 59899 | if( pCx==0 ){ |
| 59288 | 59900 | return; |
| 59289 | 59901 | } |
| 59902 | + sqlite3VdbeSorterClose(p->db, pCx); |
| 59290 | 59903 | if( pCx->pBt ){ |
| 59291 | 59904 | sqlite3BtreeClose(pCx->pBt); |
| 59292 | 59905 | /* The pCx->pCursor will be close automatically, if it exists, by |
| 59293 | 59906 | ** the call above. */ |
| 59294 | 59907 | }else if( pCx->pCursor ){ |
| | @@ -62585,10 +63198,17 @@ |
| 62585 | 63198 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 62586 | 63199 | ** P if required. |
| 62587 | 63200 | */ |
| 62588 | 63201 | #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) |
| 62589 | 63202 | |
| 63203 | +/* Return true if the cursor was opened using the OP_OpenSorter opcode. */ |
| 63204 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 63205 | +# define isSorter(x) 0 |
| 63206 | +#else |
| 63207 | +# define isSorter(x) ((x)->pSorter!=0) |
| 63208 | +#endif |
| 63209 | + |
| 62590 | 63210 | /* |
| 62591 | 63211 | ** Argument pMem points at a register that will be passed to a |
| 62592 | 63212 | ** user-defined function or returned to the user as the result of a query. |
| 62593 | 63213 | ** This routine sets the pMem->type variable used by the sqlite3_value_*() |
| 62594 | 63214 | ** routines. |
| | @@ -63179,10 +63799,11 @@ |
| 63179 | 63799 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 63180 | 63800 | u32 offset; /* Offset into the data */ |
| 63181 | 63801 | u32 szField; /* Number of bytes in the content of a field */ |
| 63182 | 63802 | int szHdr; /* Size of the header size field at start of record */ |
| 63183 | 63803 | int avail; /* Number of bytes of available data */ |
| 63804 | + u32 t; /* A type code from the record header */ |
| 63184 | 63805 | Mem *pReg; /* PseudoTable input register */ |
| 63185 | 63806 | } am; |
| 63186 | 63807 | struct OP_Affinity_stack_vars { |
| 63187 | 63808 | const char *zAffinity; /* The affinity to be applied */ |
| 63188 | 63809 | char cAff; /* A single character of affinity */ |
| | @@ -63337,11 +63958,10 @@ |
| 63337 | 63958 | BtCursor *pCrsr; |
| 63338 | 63959 | int res; |
| 63339 | 63960 | } bl; |
| 63340 | 63961 | struct OP_Next_stack_vars { |
| 63341 | 63962 | VdbeCursor *pC; |
| 63342 | | - BtCursor *pCrsr; |
| 63343 | 63963 | int res; |
| 63344 | 63964 | } bm; |
| 63345 | 63965 | struct OP_IdxInsert_stack_vars { |
| 63346 | 63966 | VdbeCursor *pC; |
| 63347 | 63967 | BtCursor *pCrsr; |
| | @@ -63591,11 +64211,11 @@ |
| 63591 | 64211 | if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ |
| 63592 | 64212 | assert( pOp->p2>0 ); |
| 63593 | 64213 | assert( pOp->p2<=p->nMem ); |
| 63594 | 64214 | pOut = &aMem[pOp->p2]; |
| 63595 | 64215 | memAboutToChange(p, pOut); |
| 63596 | | - sqlite3VdbeMemReleaseExternal(pOut); |
| 64216 | + MemReleaseExt(pOut); |
| 63597 | 64217 | pOut->flags = MEM_Int; |
| 63598 | 64218 | } |
| 63599 | 64219 | |
| 63600 | 64220 | /* Sanity checking on other operands */ |
| 63601 | 64221 | #ifdef SQLITE_DEBUG |
| | @@ -65061,10 +65681,11 @@ |
| 65061 | 65681 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 65062 | 65682 | u32 offset; /* Offset into the data */ |
| 65063 | 65683 | u32 szField; /* Number of bytes in the content of a field */ |
| 65064 | 65684 | int szHdr; /* Size of the header size field at start of record */ |
| 65065 | 65685 | int avail; /* Number of bytes of available data */ |
| 65686 | + u32 t; /* A type code from the record header */ |
| 65066 | 65687 | Mem *pReg; /* PseudoTable input register */ |
| 65067 | 65688 | #endif /* local variables moved into u.am */ |
| 65068 | 65689 | |
| 65069 | 65690 | |
| 65070 | 65691 | u.am.p1 = pOp->p1; |
| | @@ -65073,11 +65694,10 @@ |
| 65073 | 65694 | memset(&u.am.sMem, 0, sizeof(u.am.sMem)); |
| 65074 | 65695 | assert( u.am.p1<p->nCursor ); |
| 65075 | 65696 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 65076 | 65697 | u.am.pDest = &aMem[pOp->p3]; |
| 65077 | 65698 | memAboutToChange(p, u.am.pDest); |
| 65078 | | - MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 65079 | 65699 | u.am.zRec = 0; |
| 65080 | 65700 | |
| 65081 | 65701 | /* This block sets the variable u.am.payloadSize to be the total number of |
| 65082 | 65702 | ** bytes in the record. |
| 65083 | 65703 | ** |
| | @@ -65117,11 +65737,11 @@ |
| 65117 | 65737 | }else{ |
| 65118 | 65738 | assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) ); |
| 65119 | 65739 | rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize); |
| 65120 | 65740 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 65121 | 65741 | } |
| 65122 | | - }else if( u.am.pC->pseudoTableReg>0 ){ |
| 65742 | + }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){ |
| 65123 | 65743 | u.am.pReg = &aMem[u.am.pC->pseudoTableReg]; |
| 65124 | 65744 | assert( u.am.pReg->flags & MEM_Blob ); |
| 65125 | 65745 | assert( memIsValid(u.am.pReg) ); |
| 65126 | 65746 | u.am.payloadSize = u.am.pReg->n; |
| 65127 | 65747 | u.am.zRec = u.am.pReg->z; |
| | @@ -65130,13 +65750,14 @@ |
| 65130 | 65750 | }else{ |
| 65131 | 65751 | /* Consider the row to be NULL */ |
| 65132 | 65752 | u.am.payloadSize = 0; |
| 65133 | 65753 | } |
| 65134 | 65754 | |
| 65135 | | - /* If u.am.payloadSize is 0, then just store a NULL */ |
| 65755 | + /* If u.am.payloadSize is 0, then just store a NULL. This can happen because of |
| 65756 | + ** nullRow or because of a corrupt database. */ |
| 65136 | 65757 | if( u.am.payloadSize==0 ){ |
| 65137 | | - assert( u.am.pDest->flags&MEM_Null ); |
| 65758 | + MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 65138 | 65759 | goto op_column_out; |
| 65139 | 65760 | } |
| 65140 | 65761 | assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 ); |
| 65141 | 65762 | if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 65142 | 65763 | goto too_big; |
| | @@ -65239,12 +65860,18 @@ |
| 65239 | 65860 | ** of the record to the start of the data for the u.am.i-th column |
| 65240 | 65861 | */ |
| 65241 | 65862 | for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){ |
| 65242 | 65863 | if( u.am.zIdx<u.am.zEndHdr ){ |
| 65243 | 65864 | u.am.aOffset[u.am.i] = u.am.offset; |
| 65244 | | - u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]); |
| 65245 | | - u.am.szField = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]); |
| 65865 | + if( u.am.zIdx[0]<0x80 ){ |
| 65866 | + u.am.t = u.am.zIdx[0]; |
| 65867 | + u.am.zIdx++; |
| 65868 | + }else{ |
| 65869 | + u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t); |
| 65870 | + } |
| 65871 | + u.am.aType[u.am.i] = u.am.t; |
| 65872 | + u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t); |
| 65246 | 65873 | u.am.offset += u.am.szField; |
| 65247 | 65874 | if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */ |
| 65248 | 65875 | u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */ |
| 65249 | 65876 | break; |
| 65250 | 65877 | } |
| | @@ -65281,11 +65908,11 @@ |
| 65281 | 65908 | ** a pointer to a Mem object. |
| 65282 | 65909 | */ |
| 65283 | 65910 | if( u.am.aOffset[u.am.p2] ){ |
| 65284 | 65911 | assert( rc==SQLITE_OK ); |
| 65285 | 65912 | if( u.am.zRec ){ |
| 65286 | | - sqlite3VdbeMemReleaseExternal(u.am.pDest); |
| 65913 | + MemReleaseExt(u.am.pDest); |
| 65287 | 65914 | sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest); |
| 65288 | 65915 | }else{ |
| 65289 | 65916 | u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]); |
| 65290 | 65917 | sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest); |
| 65291 | 65918 | rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem); |
| | @@ -65298,11 +65925,11 @@ |
| 65298 | 65925 | u.am.pDest->enc = encoding; |
| 65299 | 65926 | }else{ |
| 65300 | 65927 | if( pOp->p4type==P4_MEM ){ |
| 65301 | 65928 | sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static); |
| 65302 | 65929 | }else{ |
| 65303 | | - assert( u.am.pDest->flags&MEM_Null ); |
| 65930 | + MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 65304 | 65931 | } |
| 65305 | 65932 | } |
| 65306 | 65933 | |
| 65307 | 65934 | /* If we dynamically allocated space to hold the data (in the |
| 65308 | 65935 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
| | @@ -65500,11 +66127,11 @@ |
| 65500 | 66127 | i64 nEntry; |
| 65501 | 66128 | BtCursor *pCrsr; |
| 65502 | 66129 | #endif /* local variables moved into u.ap */ |
| 65503 | 66130 | |
| 65504 | 66131 | u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor; |
| 65505 | | - if( u.ap.pCrsr ){ |
| 66132 | + if( ALWAYS(u.ap.pCrsr) ){ |
| 65506 | 66133 | rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry); |
| 65507 | 66134 | }else{ |
| 65508 | 66135 | u.ap.nEntry = 0; |
| 65509 | 66136 | } |
| 65510 | 66137 | pOut->u.i = u.ap.nEntry; |
| | @@ -66076,19 +66703,13 @@ |
| 66076 | 66703 | u.aw.pCur->nullRow = 1; |
| 66077 | 66704 | u.aw.pCur->isOrdered = 1; |
| 66078 | 66705 | rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor); |
| 66079 | 66706 | u.aw.pCur->pKeyInfo = u.aw.pKeyInfo; |
| 66080 | 66707 | |
| 66081 | | - /* Since it performs no memory allocation or IO, the only values that |
| 66082 | | - ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK. |
| 66083 | | - ** SQLITE_EMPTY is only returned when attempting to open the table |
| 66084 | | - ** rooted at page 1 of a zero-byte database. */ |
| 66085 | | - assert( rc==SQLITE_EMPTY || rc==SQLITE_OK ); |
| 66086 | | - if( rc==SQLITE_EMPTY ){ |
| 66087 | | - u.aw.pCur->pCursor = 0; |
| 66088 | | - rc = SQLITE_OK; |
| 66089 | | - } |
| 66708 | + /* Since it performs no memory allocation or IO, the only value that |
| 66709 | + ** sqlite3BtreeCursor() may return is SQLITE_OK. */ |
| 66710 | + assert( rc==SQLITE_OK ); |
| 66090 | 66711 | |
| 66091 | 66712 | /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of |
| 66092 | 66713 | ** SQLite used to check if the root-page flags were sane at this point |
| 66093 | 66714 | ** and report database corruption if they were not, but this check has |
| 66094 | 66715 | ** since moved into the btree layer. */ |
| | @@ -66095,11 +66716,11 @@ |
| 66095 | 66716 | u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO; |
| 66096 | 66717 | u.aw.pCur->isIndex = !u.aw.pCur->isTable; |
| 66097 | 66718 | break; |
| 66098 | 66719 | } |
| 66099 | 66720 | |
| 66100 | | -/* Opcode: OpenEphemeral P1 P2 * P4 * |
| 66721 | +/* Opcode: OpenEphemeral P1 P2 * P4 P5 |
| 66101 | 66722 | ** |
| 66102 | 66723 | ** Open a new cursor P1 to a transient table. |
| 66103 | 66724 | ** The cursor is always opened read/write even if |
| 66104 | 66725 | ** the main database is read-only. The ephemeral |
| 66105 | 66726 | ** table is deleted automatically when the cursor is closed. |
| | @@ -66112,18 +66733,30 @@ |
| 66112 | 66733 | ** This opcode was once called OpenTemp. But that created |
| 66113 | 66734 | ** confusion because the term "temp table", might refer either |
| 66114 | 66735 | ** to a TEMP table at the SQL level, or to a table opened by |
| 66115 | 66736 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 66116 | 66737 | ** that created confusion with the whole virtual-table idea. |
| 66738 | +** |
| 66739 | +** The P5 parameter can be a mask of the BTREE_* flags defined |
| 66740 | +** in btree.h. These flags control aspects of the operation of |
| 66741 | +** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 66742 | +** added automatically. |
| 66117 | 66743 | */ |
| 66118 | 66744 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
| 66119 | 66745 | ** |
| 66120 | 66746 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 66121 | 66747 | ** different name to distinguish its use. Tables created using |
| 66122 | 66748 | ** by this opcode will be used for automatically created transient |
| 66123 | 66749 | ** indices in joins. |
| 66124 | 66750 | */ |
| 66751 | +/* Opcode: OpenSorter P1 P2 * P4 * |
| 66752 | +** |
| 66753 | +** This opcode works like OP_OpenEphemeral except that it opens |
| 66754 | +** a transient index that is specifically designed to sort large |
| 66755 | +** tables using an external merge-sort algorithm. |
| 66756 | +*/ |
| 66757 | +case OP_OpenSorter: |
| 66125 | 66758 | case OP_OpenAutoindex: |
| 66126 | 66759 | case OP_OpenEphemeral: { |
| 66127 | 66760 | #if 0 /* local variables moved into u.ax */ |
| 66128 | 66761 | VdbeCursor *pCx; |
| 66129 | 66762 | #endif /* local variables moved into u.ax */ |
| | @@ -66133,10 +66766,11 @@ |
| 66133 | 66766 | SQLITE_OPEN_EXCLUSIVE | |
| 66134 | 66767 | SQLITE_OPEN_DELETEONCLOSE | |
| 66135 | 66768 | SQLITE_OPEN_TRANSIENT_DB; |
| 66136 | 66769 | |
| 66137 | 66770 | assert( pOp->p1>=0 ); |
| 66771 | + assert( (pOp->opcode==OP_OpenSorter)==((pOp->p5 & BTREE_SORTER)!=0) ); |
| 66138 | 66772 | u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
| 66139 | 66773 | if( u.ax.pCx==0 ) goto no_mem; |
| 66140 | 66774 | u.ax.pCx->nullRow = 1; |
| 66141 | 66775 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt, |
| 66142 | 66776 | BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); |
| | @@ -66166,10 +66800,15 @@ |
| 66166 | 66800 | u.ax.pCx->isTable = 1; |
| 66167 | 66801 | } |
| 66168 | 66802 | } |
| 66169 | 66803 | u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
| 66170 | 66804 | u.ax.pCx->isIndex = !u.ax.pCx->isTable; |
| 66805 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 66806 | + if( rc==SQLITE_OK && pOp->opcode==OP_OpenSorter ){ |
| 66807 | + rc = sqlite3VdbeSorterInit(db, u.ax.pCx); |
| 66808 | + } |
| 66809 | +#endif |
| 66171 | 66810 | break; |
| 66172 | 66811 | } |
| 66173 | 66812 | |
| 66174 | 66813 | /* Opcode: OpenPseudo P1 P2 P3 * * |
| 66175 | 66814 | ** |
| | @@ -66285,11 +66924,11 @@ |
| 66285 | 66924 | assert( u.az.pC->pseudoTableReg==0 ); |
| 66286 | 66925 | assert( OP_SeekLe == OP_SeekLt+1 ); |
| 66287 | 66926 | assert( OP_SeekGe == OP_SeekLt+2 ); |
| 66288 | 66927 | assert( OP_SeekGt == OP_SeekLt+3 ); |
| 66289 | 66928 | assert( u.az.pC->isOrdered ); |
| 66290 | | - if( u.az.pC->pCursor!=0 ){ |
| 66929 | + if( ALWAYS(u.az.pC->pCursor!=0) ){ |
| 66291 | 66930 | u.az.oc = pOp->opcode; |
| 66292 | 66931 | u.az.pC->nullRow = 0; |
| 66293 | 66932 | if( u.az.pC->isTable ){ |
| 66294 | 66933 | /* The input value in P3 might be of any type: integer, real, string, |
| 66295 | 66934 | ** blob, or NULL. But it needs to be an integer before we can do |
| | @@ -66651,11 +67290,11 @@ |
| 66651 | 67290 | u.bd.pC = p->apCsr[pOp->p1]; |
| 66652 | 67291 | assert( u.bd.pC!=0 ); |
| 66653 | 67292 | assert( u.bd.pC->isTable ); |
| 66654 | 67293 | assert( u.bd.pC->pseudoTableReg==0 ); |
| 66655 | 67294 | u.bd.pCrsr = u.bd.pC->pCursor; |
| 66656 | | - if( u.bd.pCrsr!=0 ){ |
| 67295 | + if( ALWAYS(u.bd.pCrsr!=0) ){ |
| 66657 | 67296 | u.bd.res = 0; |
| 66658 | 67297 | u.bd.iKey = pIn3->u.i; |
| 66659 | 67298 | rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res); |
| 66660 | 67299 | u.bd.pC->lastRowid = pIn3->u.i; |
| 66661 | 67300 | u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0; |
| | @@ -67075,10 +67714,17 @@ |
| 67075 | 67714 | assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey ); |
| 67076 | 67715 | assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData ); |
| 67077 | 67716 | assert( u.bh.pC!=0 ); |
| 67078 | 67717 | assert( u.bh.pC->nullRow==0 ); |
| 67079 | 67718 | assert( u.bh.pC->pseudoTableReg==0 ); |
| 67719 | + |
| 67720 | + if( isSorter(u.bh.pC) ){ |
| 67721 | + assert( pOp->opcode==OP_RowKey ); |
| 67722 | + rc = sqlite3VdbeSorterRowkey(u.bh.pC, pOut); |
| 67723 | + break; |
| 67724 | + } |
| 67725 | + |
| 67080 | 67726 | assert( u.bh.pC->pCursor!=0 ); |
| 67081 | 67727 | u.bh.pCrsr = u.bh.pC->pCursor; |
| 67082 | 67728 | assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) ); |
| 67083 | 67729 | |
| 67084 | 67730 | /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or |
| | @@ -67183,10 +67829,11 @@ |
| 67183 | 67829 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67184 | 67830 | u.bj.pC = p->apCsr[pOp->p1]; |
| 67185 | 67831 | assert( u.bj.pC!=0 ); |
| 67186 | 67832 | u.bj.pC->nullRow = 1; |
| 67187 | 67833 | u.bj.pC->rowidIsValid = 0; |
| 67834 | + assert( u.bj.pC->pCursor || u.bj.pC->pVtabCursor ); |
| 67188 | 67835 | if( u.bj.pC->pCursor ){ |
| 67189 | 67836 | sqlite3BtreeClearCursor(u.bj.pC->pCursor); |
| 67190 | 67837 | } |
| 67191 | 67838 | break; |
| 67192 | 67839 | } |
| | @@ -67208,11 +67855,11 @@ |
| 67208 | 67855 | |
| 67209 | 67856 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67210 | 67857 | u.bk.pC = p->apCsr[pOp->p1]; |
| 67211 | 67858 | assert( u.bk.pC!=0 ); |
| 67212 | 67859 | u.bk.pCrsr = u.bk.pC->pCursor; |
| 67213 | | - if( u.bk.pCrsr==0 ){ |
| 67860 | + if( NEVER(u.bk.pCrsr==0) ){ |
| 67214 | 67861 | u.bk.res = 1; |
| 67215 | 67862 | }else{ |
| 67216 | 67863 | rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res); |
| 67217 | 67864 | } |
| 67218 | 67865 | u.bk.pC->nullRow = (u8)u.bk.res; |
| | @@ -67263,11 +67910,15 @@ |
| 67263 | 67910 | |
| 67264 | 67911 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67265 | 67912 | u.bl.pC = p->apCsr[pOp->p1]; |
| 67266 | 67913 | assert( u.bl.pC!=0 ); |
| 67267 | 67914 | u.bl.res = 1; |
| 67268 | | - if( (u.bl.pCrsr = u.bl.pC->pCursor)!=0 ){ |
| 67915 | + if( isSorter(u.bl.pC) ){ |
| 67916 | + rc = sqlite3VdbeSorterRewind(db, u.bl.pC, &u.bl.res); |
| 67917 | + }else{ |
| 67918 | + u.bl.pCrsr = u.bl.pC->pCursor; |
| 67919 | + assert( u.bl.pCrsr ); |
| 67269 | 67920 | rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res); |
| 67270 | 67921 | u.bl.pC->atFirst = u.bl.res==0 ?1:0; |
| 67271 | 67922 | u.bl.pC->deferredMoveto = 0; |
| 67272 | 67923 | u.bl.pC->cacheStatus = CACHE_STALE; |
| 67273 | 67924 | u.bl.pC->rowidIsValid = 0; |
| | @@ -67278,18 +67929,21 @@ |
| 67278 | 67929 | pc = pOp->p2 - 1; |
| 67279 | 67930 | } |
| 67280 | 67931 | break; |
| 67281 | 67932 | } |
| 67282 | 67933 | |
| 67283 | | -/* Opcode: Next P1 P2 * * P5 |
| 67934 | +/* Opcode: Next P1 P2 * P4 P5 |
| 67284 | 67935 | ** |
| 67285 | 67936 | ** Advance cursor P1 so that it points to the next key/data pair in its |
| 67286 | 67937 | ** table or index. If there are no more key/value pairs then fall through |
| 67287 | 67938 | ** to the following instruction. But if the cursor advance was successful, |
| 67288 | 67939 | ** jump immediately to P2. |
| 67289 | 67940 | ** |
| 67290 | 67941 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 67942 | +** |
| 67943 | +** P4 is always of type P4_ADVANCE. The function pointer points to |
| 67944 | +** sqlite3BtreeNext(). |
| 67291 | 67945 | ** |
| 67292 | 67946 | ** If P5 is positive and the jump is taken, then event counter |
| 67293 | 67947 | ** number P5-1 in the prepared statement is incremented. |
| 67294 | 67948 | ** |
| 67295 | 67949 | ** See also: Prev |
| | @@ -67300,19 +67954,21 @@ |
| 67300 | 67954 | ** table or index. If there is no previous key/value pairs then fall through |
| 67301 | 67955 | ** to the following instruction. But if the cursor backup was successful, |
| 67302 | 67956 | ** jump immediately to P2. |
| 67303 | 67957 | ** |
| 67304 | 67958 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 67959 | +** |
| 67960 | +** P4 is always of type P4_ADVANCE. The function pointer points to |
| 67961 | +** sqlite3BtreePrevious(). |
| 67305 | 67962 | ** |
| 67306 | 67963 | ** If P5 is positive and the jump is taken, then event counter |
| 67307 | 67964 | ** number P5-1 in the prepared statement is incremented. |
| 67308 | 67965 | */ |
| 67309 | 67966 | case OP_Prev: /* jump */ |
| 67310 | 67967 | case OP_Next: { /* jump */ |
| 67311 | 67968 | #if 0 /* local variables moved into u.bm */ |
| 67312 | 67969 | VdbeCursor *pC; |
| 67313 | | - BtCursor *pCrsr; |
| 67314 | 67970 | int res; |
| 67315 | 67971 | #endif /* local variables moved into u.bm */ |
| 67316 | 67972 | |
| 67317 | 67973 | CHECK_FOR_INTERRUPT; |
| 67318 | 67974 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| | @@ -67319,19 +67975,21 @@ |
| 67319 | 67975 | assert( pOp->p5<=ArraySize(p->aCounter) ); |
| 67320 | 67976 | u.bm.pC = p->apCsr[pOp->p1]; |
| 67321 | 67977 | if( u.bm.pC==0 ){ |
| 67322 | 67978 | break; /* See ticket #2273 */ |
| 67323 | 67979 | } |
| 67324 | | - u.bm.pCrsr = u.bm.pC->pCursor; |
| 67325 | | - if( u.bm.pCrsr==0 ){ |
| 67326 | | - u.bm.pC->nullRow = 1; |
| 67327 | | - break; |
| 67328 | | - } |
| 67329 | | - u.bm.res = 1; |
| 67330 | | - assert( u.bm.pC->deferredMoveto==0 ); |
| 67331 | | - rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bm.pCrsr, &u.bm.res) : |
| 67332 | | - sqlite3BtreePrevious(u.bm.pCrsr, &u.bm.res); |
| 67980 | + if( isSorter(u.bm.pC) ){ |
| 67981 | + assert( pOp->opcode==OP_Next ); |
| 67982 | + rc = sqlite3VdbeSorterNext(db, u.bm.pC, &u.bm.res); |
| 67983 | + }else{ |
| 67984 | + u.bm.res = 1; |
| 67985 | + assert( u.bm.pC->deferredMoveto==0 ); |
| 67986 | + assert( u.bm.pC->pCursor ); |
| 67987 | + assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 67988 | + assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
| 67989 | + rc = pOp->p4.xAdvance(u.bm.pC->pCursor, &u.bm.res); |
| 67990 | + } |
| 67333 | 67991 | u.bm.pC->nullRow = (u8)u.bm.res; |
| 67334 | 67992 | u.bm.pC->cacheStatus = CACHE_STALE; |
| 67335 | 67993 | if( u.bm.res==0 ){ |
| 67336 | 67994 | pc = pOp->p2 - 1; |
| 67337 | 67995 | if( pOp->p5 ) p->aCounter[pOp->p5-1]++; |
| | @@ -67373,14 +68031,17 @@ |
| 67373 | 68031 | assert( u.bn.pC->isTable==0 ); |
| 67374 | 68032 | rc = ExpandBlob(pIn2); |
| 67375 | 68033 | if( rc==SQLITE_OK ){ |
| 67376 | 68034 | u.bn.nKey = pIn2->n; |
| 67377 | 68035 | u.bn.zKey = pIn2->z; |
| 67378 | | - rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3, |
| 67379 | | - ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0) |
| 67380 | | - ); |
| 67381 | | - assert( u.bn.pC->deferredMoveto==0 ); |
| 68036 | + rc = sqlite3VdbeSorterWrite(db, u.bn.pC, u.bn.nKey); |
| 68037 | + if( rc==SQLITE_OK ){ |
| 68038 | + rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3, |
| 68039 | + ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0) |
| 68040 | + ); |
| 68041 | + assert( u.bn.pC->deferredMoveto==0 ); |
| 68042 | + } |
| 67382 | 68043 | u.bn.pC->cacheStatus = CACHE_STALE; |
| 67383 | 68044 | } |
| 67384 | 68045 | } |
| 67385 | 68046 | break; |
| 67386 | 68047 | } |
| | @@ -69556,10 +70217,722 @@ |
| 69556 | 70217 | } |
| 69557 | 70218 | |
| 69558 | 70219 | #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |
| 69559 | 70220 | |
| 69560 | 70221 | /************** End of vdbeblob.c ********************************************/ |
| 70222 | +/************** Begin file vdbesort.c ****************************************/ |
| 70223 | +/* |
| 70224 | +** 2011 July 9 |
| 70225 | +** |
| 70226 | +** The author disclaims copyright to this source code. In place of |
| 70227 | +** a legal notice, here is a blessing: |
| 70228 | +** |
| 70229 | +** May you do good and not evil. |
| 70230 | +** May you find forgiveness for yourself and forgive others. |
| 70231 | +** May you share freely, never taking more than you give. |
| 70232 | +** |
| 70233 | +************************************************************************* |
| 70234 | +** This file contains code for the VdbeSorter object, used in concert with |
| 70235 | +** a VdbeCursor to sort large numbers of keys (as may be required, for |
| 70236 | +** example, by CREATE INDEX statements on tables too large to fit in main |
| 70237 | +** memory). |
| 70238 | +*/ |
| 70239 | + |
| 70240 | + |
| 70241 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 70242 | + |
| 70243 | +typedef struct VdbeSorterIter VdbeSorterIter; |
| 70244 | + |
| 70245 | +/* |
| 70246 | +** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES: |
| 70247 | +** |
| 70248 | +** As keys are added to the sorter, they are written to disk in a series |
| 70249 | +** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly |
| 70250 | +** the same as the cache-size allowed for temporary databases. In order |
| 70251 | +** to allow the caller to extract keys from the sorter in sorted order, |
| 70252 | +** all PMAs currently stored on disk must be merged together. This comment |
| 70253 | +** describes the data structure used to do so. The structure supports |
| 70254 | +** merging any number of arrays in a single pass with no redundant comparison |
| 70255 | +** operations. |
| 70256 | +** |
| 70257 | +** The aIter[] array contains an iterator for each of the PMAs being merged. |
| 70258 | +** An aIter[] iterator either points to a valid key or else is at EOF. For |
| 70259 | +** the purposes of the paragraphs below, we assume that the array is actually |
| 70260 | +** N elements in size, where N is the smallest power of 2 greater to or equal |
| 70261 | +** to the number of iterators being merged. The extra aIter[] elements are |
| 70262 | +** treated as if they are empty (always at EOF). |
| 70263 | +** |
| 70264 | +** The aTree[] array is also N elements in size. The value of N is stored in |
| 70265 | +** the VdbeSorter.nTree variable. |
| 70266 | +** |
| 70267 | +** The final (N/2) elements of aTree[] contain the results of comparing |
| 70268 | +** pairs of iterator keys together. Element i contains the result of |
| 70269 | +** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the |
| 70270 | +** aTree element is set to the index of it. |
| 70271 | +** |
| 70272 | +** For the purposes of this comparison, EOF is considered greater than any |
| 70273 | +** other key value. If the keys are equal (only possible with two EOF |
| 70274 | +** values), it doesn't matter which index is stored. |
| 70275 | +** |
| 70276 | +** The (N/4) elements of aTree[] that preceed the final (N/2) described |
| 70277 | +** above contains the index of the smallest of each block of 4 iterators. |
| 70278 | +** And so on. So that aTree[1] contains the index of the iterator that |
| 70279 | +** currently points to the smallest key value. aTree[0] is unused. |
| 70280 | +** |
| 70281 | +** Example: |
| 70282 | +** |
| 70283 | +** aIter[0] -> Banana |
| 70284 | +** aIter[1] -> Feijoa |
| 70285 | +** aIter[2] -> Elderberry |
| 70286 | +** aIter[3] -> Currant |
| 70287 | +** aIter[4] -> Grapefruit |
| 70288 | +** aIter[5] -> Apple |
| 70289 | +** aIter[6] -> Durian |
| 70290 | +** aIter[7] -> EOF |
| 70291 | +** |
| 70292 | +** aTree[] = { X, 5 0, 5 0, 3, 5, 6 } |
| 70293 | +** |
| 70294 | +** The current element is "Apple" (the value of the key indicated by |
| 70295 | +** iterator 5). When the Next() operation is invoked, iterator 5 will |
| 70296 | +** be advanced to the next key in its segment. Say the next key is |
| 70297 | +** "Eggplant": |
| 70298 | +** |
| 70299 | +** aIter[5] -> Eggplant |
| 70300 | +** |
| 70301 | +** The contents of aTree[] are updated first by comparing the new iterator |
| 70302 | +** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator |
| 70303 | +** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree. |
| 70304 | +** The value of iterator 6 - "Durian" - is now smaller than that of iterator |
| 70305 | +** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian), |
| 70306 | +** so the value written into element 1 of the array is 0. As follows: |
| 70307 | +** |
| 70308 | +** aTree[] = { X, 0 0, 6 0, 3, 5, 6 } |
| 70309 | +** |
| 70310 | +** In other words, each time we advance to the next sorter element, log2(N) |
| 70311 | +** key comparison operations are required, where N is the number of segments |
| 70312 | +** being merged (rounded up to the next power of 2). |
| 70313 | +*/ |
| 70314 | +struct VdbeSorter { |
| 70315 | + int nWorking; /* Start a new b-tree after this many pages */ |
| 70316 | + int nBtree; /* Current size of b-tree contents as PMA */ |
| 70317 | + int nTree; /* Used size of aTree/aIter (power of 2) */ |
| 70318 | + VdbeSorterIter *aIter; /* Array of iterators to merge */ |
| 70319 | + int *aTree; /* Current state of incremental merge */ |
| 70320 | + i64 iWriteOff; /* Current write offset within file pTemp1 */ |
| 70321 | + i64 iReadOff; /* Current read offset within file pTemp1 */ |
| 70322 | + sqlite3_file *pTemp1; /* PMA file 1 */ |
| 70323 | + int nPMA; /* Number of PMAs stored in pTemp1 */ |
| 70324 | +}; |
| 70325 | + |
| 70326 | +/* |
| 70327 | +** The following type is an iterator for a PMA. It caches the current key in |
| 70328 | +** variables nKey/aKey. If the iterator is at EOF, pFile==0. |
| 70329 | +*/ |
| 70330 | +struct VdbeSorterIter { |
| 70331 | + i64 iReadOff; /* Current read offset */ |
| 70332 | + i64 iEof; /* 1 byte past EOF for this iterator */ |
| 70333 | + sqlite3_file *pFile; /* File iterator is reading from */ |
| 70334 | + int nAlloc; /* Bytes of space at aAlloc */ |
| 70335 | + u8 *aAlloc; /* Allocated space */ |
| 70336 | + int nKey; /* Number of bytes in key */ |
| 70337 | + u8 *aKey; /* Pointer to current key */ |
| 70338 | +}; |
| 70339 | + |
| 70340 | +/* Minimum allowable value for the VdbeSorter.nWorking variable */ |
| 70341 | +#define SORTER_MIN_WORKING 10 |
| 70342 | + |
| 70343 | +/* Maximum number of segments to merge in a single pass. */ |
| 70344 | +#define SORTER_MAX_MERGE_COUNT 16 |
| 70345 | + |
| 70346 | +/* |
| 70347 | +** Free all memory belonging to the VdbeSorterIter object passed as the second |
| 70348 | +** argument. All structure fields are set to zero before returning. |
| 70349 | +*/ |
| 70350 | +static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){ |
| 70351 | + sqlite3DbFree(db, pIter->aAlloc); |
| 70352 | + memset(pIter, 0, sizeof(VdbeSorterIter)); |
| 70353 | +} |
| 70354 | + |
| 70355 | +/* |
| 70356 | +** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if |
| 70357 | +** no error occurs, or an SQLite error code if one does. |
| 70358 | +*/ |
| 70359 | +static int vdbeSorterIterNext( |
| 70360 | + sqlite3 *db, /* Database handle (for sqlite3DbMalloc() ) */ |
| 70361 | + VdbeSorterIter *pIter /* Iterator to advance */ |
| 70362 | +){ |
| 70363 | + int rc; /* Return Code */ |
| 70364 | + int nRead; /* Number of bytes read */ |
| 70365 | + int nRec; /* Size of record in bytes */ |
| 70366 | + int iOff; /* Size of serialized size varint in bytes */ |
| 70367 | + |
| 70368 | + nRead = pIter->iEof - pIter->iReadOff; |
| 70369 | + if( nRead>5 ) nRead = 5; |
| 70370 | + if( nRead<=0 ){ |
| 70371 | + /* This is an EOF condition */ |
| 70372 | + vdbeSorterIterZero(db, pIter); |
| 70373 | + return SQLITE_OK; |
| 70374 | + } |
| 70375 | + |
| 70376 | + rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff); |
| 70377 | + iOff = getVarint32(pIter->aAlloc, nRec); |
| 70378 | + |
| 70379 | + if( rc==SQLITE_OK && (iOff+nRec)>nRead ){ |
| 70380 | + int nRead2; /* Number of extra bytes to read */ |
| 70381 | + if( (iOff+nRec)>pIter->nAlloc ){ |
| 70382 | + int nNew = pIter->nAlloc*2; |
| 70383 | + while( (iOff+nRec)>nNew ) nNew = nNew*2; |
| 70384 | + pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew); |
| 70385 | + if( !pIter->aAlloc ) return SQLITE_NOMEM; |
| 70386 | + pIter->nAlloc = nNew; |
| 70387 | + } |
| 70388 | + |
| 70389 | + nRead2 = iOff + nRec - nRead; |
| 70390 | + rc = sqlite3OsRead( |
| 70391 | + pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead |
| 70392 | + ); |
| 70393 | + } |
| 70394 | + |
| 70395 | + assert( nRec>0 || rc!=SQLITE_OK ); |
| 70396 | + pIter->iReadOff += iOff+nRec; |
| 70397 | + pIter->nKey = nRec; |
| 70398 | + pIter->aKey = &pIter->aAlloc[iOff]; |
| 70399 | + return rc; |
| 70400 | +} |
| 70401 | + |
| 70402 | +/* |
| 70403 | +** Write a single varint, value iVal, to file-descriptor pFile. Return |
| 70404 | +** SQLITE_OK if successful, or an SQLite error code if some error occurs. |
| 70405 | +** |
| 70406 | +** The value of *piOffset when this function is called is used as the byte |
| 70407 | +** offset in file pFile to write to. Before returning, *piOffset is |
| 70408 | +** incremented by the number of bytes written. |
| 70409 | +*/ |
| 70410 | +static int vdbeSorterWriteVarint( |
| 70411 | + sqlite3_file *pFile, /* File to write to */ |
| 70412 | + i64 iVal, /* Value to write as a varint */ |
| 70413 | + i64 *piOffset /* IN/OUT: Write offset in file pFile */ |
| 70414 | +){ |
| 70415 | + u8 aVarint[9]; /* Buffer large enough for a varint */ |
| 70416 | + int nVarint; /* Number of used bytes in varint */ |
| 70417 | + int rc; /* Result of write() call */ |
| 70418 | + |
| 70419 | + nVarint = sqlite3PutVarint(aVarint, iVal); |
| 70420 | + rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset); |
| 70421 | + *piOffset += nVarint; |
| 70422 | + |
| 70423 | + return rc; |
| 70424 | +} |
| 70425 | + |
| 70426 | +/* |
| 70427 | +** Read a single varint from file-descriptor pFile. Return SQLITE_OK if |
| 70428 | +** successful, or an SQLite error code if some error occurs. |
| 70429 | +** |
| 70430 | +** The value of *piOffset when this function is called is used as the |
| 70431 | +** byte offset in file pFile from whence to read the varint. If successful |
| 70432 | +** (i.e. if no IO error occurs), then *piOffset is set to the offset of |
| 70433 | +** the first byte past the end of the varint before returning. *piVal is |
| 70434 | +** set to the integer value read. If an error occurs, the final values of |
| 70435 | +** both *piOffset and *piVal are undefined. |
| 70436 | +*/ |
| 70437 | +static int vdbeSorterReadVarint( |
| 70438 | + sqlite3_file *pFile, /* File to read from */ |
| 70439 | + i64 iEof, /* Total number of bytes in file */ |
| 70440 | + i64 *piOffset, /* IN/OUT: Read offset in pFile */ |
| 70441 | + i64 *piVal /* OUT: Value read from file */ |
| 70442 | +){ |
| 70443 | + u8 aVarint[9]; /* Buffer large enough for a varint */ |
| 70444 | + i64 iOff = *piOffset; /* Offset in file to read from */ |
| 70445 | + int nRead = 9; /* Number of bytes to read from file */ |
| 70446 | + int rc; /* Return code */ |
| 70447 | + |
| 70448 | + assert( iEof>iOff ); |
| 70449 | + if( (iEof-iOff)<nRead ){ |
| 70450 | + nRead = iEof-iOff; |
| 70451 | + } |
| 70452 | + |
| 70453 | + rc = sqlite3OsRead(pFile, aVarint, nRead, iOff); |
| 70454 | + if( rc==SQLITE_OK ){ |
| 70455 | + *piOffset += getVarint(aVarint, (u64 *)piVal); |
| 70456 | + } |
| 70457 | + |
| 70458 | + return rc; |
| 70459 | +} |
| 70460 | + |
| 70461 | +/* |
| 70462 | +** Initialize iterator pIter to scan through the PMA stored in file pFile |
| 70463 | +** starting at offset iStart and ending at offset iEof-1. This function |
| 70464 | +** leaves the iterator pointing to the first key in the PMA (or EOF if the |
| 70465 | +** PMA is empty). |
| 70466 | +*/ |
| 70467 | +static int vdbeSorterIterInit( |
| 70468 | + sqlite3 *db, /* Database handle */ |
| 70469 | + VdbeSorter *pSorter, /* Sorter object */ |
| 70470 | + i64 iStart, /* Start offset in pFile */ |
| 70471 | + VdbeSorterIter *pIter, /* Iterator to populate */ |
| 70472 | + i64 *pnByte /* IN/OUT: Increment this value by PMA size */ |
| 70473 | +){ |
| 70474 | + int rc; |
| 70475 | + |
| 70476 | + assert( pSorter->iWriteOff>iStart ); |
| 70477 | + assert( pIter->aAlloc==0 ); |
| 70478 | + pIter->pFile = pSorter->pTemp1; |
| 70479 | + pIter->iReadOff = iStart; |
| 70480 | + pIter->nAlloc = 128; |
| 70481 | + pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc); |
| 70482 | + if( !pIter->aAlloc ){ |
| 70483 | + rc = SQLITE_NOMEM; |
| 70484 | + }else{ |
| 70485 | + i64 iEof = pSorter->iWriteOff; /* EOF of file pSorter->pTemp1 */ |
| 70486 | + i64 nByte; /* Total size of PMA in bytes */ |
| 70487 | + rc = vdbeSorterReadVarint(pSorter->pTemp1, iEof, &pIter->iReadOff, &nByte); |
| 70488 | + *pnByte += nByte; |
| 70489 | + pIter->iEof = pIter->iReadOff + nByte; |
| 70490 | + } |
| 70491 | + if( rc==SQLITE_OK ){ |
| 70492 | + rc = vdbeSorterIterNext(db, pIter); |
| 70493 | + } |
| 70494 | + return rc; |
| 70495 | +} |
| 70496 | + |
| 70497 | +/* |
| 70498 | +** This function is called to compare two iterator keys when merging |
| 70499 | +** multiple b-tree segments. Parameter iOut is the index of the aTree[] |
| 70500 | +** value to recalculate. |
| 70501 | +*/ |
| 70502 | +static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){ |
| 70503 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70504 | + int i1; |
| 70505 | + int i2; |
| 70506 | + int iRes; |
| 70507 | + VdbeSorterIter *p1; |
| 70508 | + VdbeSorterIter *p2; |
| 70509 | + |
| 70510 | + assert( iOut<pSorter->nTree && iOut>0 ); |
| 70511 | + |
| 70512 | + if( iOut>=(pSorter->nTree/2) ){ |
| 70513 | + i1 = (iOut - pSorter->nTree/2) * 2; |
| 70514 | + i2 = i1 + 1; |
| 70515 | + }else{ |
| 70516 | + i1 = pSorter->aTree[iOut*2]; |
| 70517 | + i2 = pSorter->aTree[iOut*2+1]; |
| 70518 | + } |
| 70519 | + |
| 70520 | + p1 = &pSorter->aIter[i1]; |
| 70521 | + p2 = &pSorter->aIter[i2]; |
| 70522 | + |
| 70523 | + if( p1->pFile==0 ){ |
| 70524 | + iRes = i2; |
| 70525 | + }else if( p2->pFile==0 ){ |
| 70526 | + iRes = i1; |
| 70527 | + }else{ |
| 70528 | + char aSpace[150]; |
| 70529 | + UnpackedRecord *r1; |
| 70530 | + |
| 70531 | + r1 = sqlite3VdbeRecordUnpack( |
| 70532 | + pCsr->pKeyInfo, p1->nKey, p1->aKey, aSpace, sizeof(aSpace) |
| 70533 | + ); |
| 70534 | + if( r1==0 ) return SQLITE_NOMEM; |
| 70535 | + |
| 70536 | + if( sqlite3VdbeRecordCompare(p2->nKey, p2->aKey, r1)>=0 ){ |
| 70537 | + iRes = i1; |
| 70538 | + }else{ |
| 70539 | + iRes = i2; |
| 70540 | + } |
| 70541 | + sqlite3VdbeDeleteUnpackedRecord(r1); |
| 70542 | + } |
| 70543 | + |
| 70544 | + pSorter->aTree[iOut] = iRes; |
| 70545 | + return SQLITE_OK; |
| 70546 | +} |
| 70547 | + |
| 70548 | +/* |
| 70549 | +** Initialize the temporary index cursor just opened as a sorter cursor. |
| 70550 | +*/ |
| 70551 | +SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){ |
| 70552 | + assert( pCsr->pKeyInfo && pCsr->pBt ); |
| 70553 | + pCsr->pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter)); |
| 70554 | + return (pCsr->pSorter ? SQLITE_OK : SQLITE_NOMEM); |
| 70555 | +} |
| 70556 | + |
| 70557 | +/* |
| 70558 | +** Free any cursor components allocated by sqlite3VdbeSorterXXX routines. |
| 70559 | +*/ |
| 70560 | +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){ |
| 70561 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70562 | + if( pSorter ){ |
| 70563 | + if( pSorter->aIter ){ |
| 70564 | + int i; |
| 70565 | + for(i=0; i<pSorter->nTree; i++){ |
| 70566 | + vdbeSorterIterZero(db, &pSorter->aIter[i]); |
| 70567 | + } |
| 70568 | + sqlite3DbFree(db, pSorter->aIter); |
| 70569 | + } |
| 70570 | + if( pSorter->pTemp1 ){ |
| 70571 | + sqlite3OsCloseFree(pSorter->pTemp1); |
| 70572 | + } |
| 70573 | + sqlite3DbFree(db, pSorter); |
| 70574 | + pCsr->pSorter = 0; |
| 70575 | + } |
| 70576 | +} |
| 70577 | + |
| 70578 | +/* |
| 70579 | +** Allocate space for a file-handle and open a temporary file. If successful, |
| 70580 | +** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK. |
| 70581 | +** Otherwise, set *ppFile to 0 and return an SQLite error code. |
| 70582 | +*/ |
| 70583 | +static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){ |
| 70584 | + int dummy; |
| 70585 | + return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile, |
| 70586 | + SQLITE_OPEN_TEMP_JOURNAL | |
| 70587 | + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 70588 | + SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &dummy |
| 70589 | + ); |
| 70590 | +} |
| 70591 | + |
| 70592 | + |
| 70593 | +/* |
| 70594 | +** Write the current contents of the b-tree to a PMA. Return SQLITE_OK |
| 70595 | +** if successful, or an SQLite error code otherwise. |
| 70596 | +** |
| 70597 | +** The format of a PMA is: |
| 70598 | +** |
| 70599 | +** * A varint. This varint contains the total number of bytes of content |
| 70600 | +** in the PMA (not including the varint itself). |
| 70601 | +** |
| 70602 | +** * One or more records packed end-to-end in order of ascending keys. |
| 70603 | +** Each record consists of a varint followed by a blob of data (the |
| 70604 | +** key). The varint is the number of bytes in the blob of data. |
| 70605 | +*/ |
| 70606 | +static int vdbeSorterBtreeToPMA(sqlite3 *db, VdbeCursor *pCsr){ |
| 70607 | + int rc = SQLITE_OK; /* Return code */ |
| 70608 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70609 | + int res = 0; |
| 70610 | + |
| 70611 | + /* sqlite3BtreeFirst() cannot fail because sorter btrees are always held |
| 70612 | + ** in memory and so an I/O error is not possible. */ |
| 70613 | + rc = sqlite3BtreeFirst(pCsr->pCursor, &res); |
| 70614 | + if( NEVER(rc!=SQLITE_OK) || res ) return rc; |
| 70615 | + assert( pSorter->nBtree>0 ); |
| 70616 | + |
| 70617 | + /* If the first temporary PMA file has not been opened, open it now. */ |
| 70618 | + if( pSorter->pTemp1==0 ){ |
| 70619 | + rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1); |
| 70620 | + assert( rc!=SQLITE_OK || pSorter->pTemp1 ); |
| 70621 | + assert( pSorter->iWriteOff==0 ); |
| 70622 | + assert( pSorter->nPMA==0 ); |
| 70623 | + } |
| 70624 | + |
| 70625 | + if( rc==SQLITE_OK ){ |
| 70626 | + i64 iWriteOff = pSorter->iWriteOff; |
| 70627 | + void *aMalloc = 0; /* Array used to hold a single record */ |
| 70628 | + int nMalloc = 0; /* Allocated size of aMalloc[] in bytes */ |
| 70629 | + |
| 70630 | + pSorter->nPMA++; |
| 70631 | + for( |
| 70632 | + rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nBtree, &iWriteOff); |
| 70633 | + rc==SQLITE_OK && res==0; |
| 70634 | + rc = sqlite3BtreeNext(pCsr->pCursor, &res) |
| 70635 | + ){ |
| 70636 | + i64 nKey; /* Size of this key in bytes */ |
| 70637 | + |
| 70638 | + /* Write the size of the record in bytes to the output file */ |
| 70639 | + (void)sqlite3BtreeKeySize(pCsr->pCursor, &nKey); |
| 70640 | + rc = vdbeSorterWriteVarint(pSorter->pTemp1, nKey, &iWriteOff); |
| 70641 | + |
| 70642 | + /* Make sure the aMalloc[] buffer is large enough for the record */ |
| 70643 | + if( rc==SQLITE_OK && nKey>nMalloc ){ |
| 70644 | + aMalloc = sqlite3DbReallocOrFree(db, aMalloc, nKey); |
| 70645 | + if( !aMalloc ){ |
| 70646 | + rc = SQLITE_NOMEM; |
| 70647 | + }else{ |
| 70648 | + nMalloc = nKey; |
| 70649 | + } |
| 70650 | + } |
| 70651 | + |
| 70652 | + /* Write the record itself to the output file */ |
| 70653 | + if( rc==SQLITE_OK ){ |
| 70654 | + /* sqlite3BtreeKey() cannot fail because sorter btrees held in memory */ |
| 70655 | + rc = sqlite3BtreeKey(pCsr->pCursor, 0, nKey, aMalloc); |
| 70656 | + if( ALWAYS(rc==SQLITE_OK) ){ |
| 70657 | + rc = sqlite3OsWrite(pSorter->pTemp1, aMalloc, nKey, iWriteOff); |
| 70658 | + iWriteOff += nKey; |
| 70659 | + } |
| 70660 | + } |
| 70661 | + |
| 70662 | + if( rc!=SQLITE_OK ) break; |
| 70663 | + } |
| 70664 | + |
| 70665 | + /* This assert verifies that unless an error has occurred, the size of |
| 70666 | + ** the PMA on disk is the same as the expected size stored in |
| 70667 | + ** pSorter->nBtree. */ |
| 70668 | + assert( rc!=SQLITE_OK || pSorter->nBtree==( |
| 70669 | + iWriteOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nBtree) |
| 70670 | + )); |
| 70671 | + |
| 70672 | + pSorter->iWriteOff = iWriteOff; |
| 70673 | + sqlite3DbFree(db, aMalloc); |
| 70674 | + } |
| 70675 | + |
| 70676 | + pSorter->nBtree = 0; |
| 70677 | + return rc; |
| 70678 | +} |
| 70679 | + |
| 70680 | +/* |
| 70681 | +** This function is called on a sorter cursor by the VDBE before each row |
| 70682 | +** is inserted into VdbeCursor.pCsr. Argument nKey is the size of the key, in |
| 70683 | +** bytes, about to be inserted. |
| 70684 | +** |
| 70685 | +** If it is determined that the temporary b-tree accessed via VdbeCursor.pCsr |
| 70686 | +** is large enough, its contents are written to a sorted PMA on disk and the |
| 70687 | +** tree emptied. This prevents the b-tree (which must be small enough to |
| 70688 | +** fit entirely in the cache in order to support efficient inserts) from |
| 70689 | +** growing too large. |
| 70690 | +** |
| 70691 | +** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK. |
| 70692 | +*/ |
| 70693 | +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *db, VdbeCursor *pCsr, int nKey){ |
| 70694 | + int rc = SQLITE_OK; /* Return code */ |
| 70695 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70696 | + if( pSorter ){ |
| 70697 | + Pager *pPager = sqlite3BtreePager(pCsr->pBt); |
| 70698 | + int nPage; /* Current size of temporary file in pages */ |
| 70699 | + |
| 70700 | + /* Sorters never spill to disk */ |
| 70701 | + assert( sqlite3PagerFile(pPager)->pMethods==0 ); |
| 70702 | + |
| 70703 | + /* Determine how many pages the temporary b-tree has grown to */ |
| 70704 | + sqlite3PagerPagecount(pPager, &nPage); |
| 70705 | + |
| 70706 | + /* If pSorter->nWorking is still zero, but the temporary file has been |
| 70707 | + ** created in the file-system, then the most recent insert into the |
| 70708 | + ** current b-tree segment probably caused the cache to overflow (it is |
| 70709 | + ** also possible that sqlite3_release_memory() was called). So set the |
| 70710 | + ** size of the working set to a little less than the current size of the |
| 70711 | + ** file in pages. */ |
| 70712 | + if( pSorter->nWorking==0 && sqlite3PagerUnderStress(pPager) ){ |
| 70713 | + pSorter->nWorking = nPage-5; |
| 70714 | + if( pSorter->nWorking<SORTER_MIN_WORKING ){ |
| 70715 | + pSorter->nWorking = SORTER_MIN_WORKING; |
| 70716 | + } |
| 70717 | + } |
| 70718 | + |
| 70719 | + /* If the number of pages used by the current b-tree segment is greater |
| 70720 | + ** than the size of the working set (VdbeSorter.nWorking), start a new |
| 70721 | + ** segment b-tree. */ |
| 70722 | + if( pSorter->nWorking && nPage>=pSorter->nWorking ){ |
| 70723 | + BtCursor *p = pCsr->pCursor;/* Cursor structure to close and reopen */ |
| 70724 | + int iRoot; /* Root page of new tree */ |
| 70725 | + |
| 70726 | + /* Copy the current contents of the b-tree into a PMA in sorted order. |
| 70727 | + ** Close the currently open b-tree cursor. */ |
| 70728 | + rc = vdbeSorterBtreeToPMA(db, pCsr); |
| 70729 | + sqlite3BtreeCloseCursor(p); |
| 70730 | + |
| 70731 | + if( rc==SQLITE_OK ){ |
| 70732 | + rc = sqlite3BtreeDropTable(pCsr->pBt, 2, 0); |
| 70733 | +#ifdef SQLITE_DEBUG |
| 70734 | + sqlite3PagerPagecount(pPager, &nPage); |
| 70735 | + assert( rc!=SQLITE_OK || nPage==1 ); |
| 70736 | +#endif |
| 70737 | + } |
| 70738 | + if( rc==SQLITE_OK ){ |
| 70739 | + rc = sqlite3BtreeCreateTable(pCsr->pBt, &iRoot, BTREE_BLOBKEY); |
| 70740 | + } |
| 70741 | + if( rc==SQLITE_OK ){ |
| 70742 | + assert( iRoot==2 ); |
| 70743 | + rc = sqlite3BtreeCursor(pCsr->pBt, iRoot, 1, pCsr->pKeyInfo, p); |
| 70744 | + } |
| 70745 | + } |
| 70746 | + |
| 70747 | + pSorter->nBtree += sqlite3VarintLen(nKey) + nKey; |
| 70748 | + } |
| 70749 | + return rc; |
| 70750 | +} |
| 70751 | + |
| 70752 | +/* |
| 70753 | +** Helper function for sqlite3VdbeSorterRewind(). |
| 70754 | +*/ |
| 70755 | +static int vdbeSorterInitMerge( |
| 70756 | + sqlite3 *db, /* Database handle */ |
| 70757 | + VdbeCursor *pCsr, /* Cursor handle for this sorter */ |
| 70758 | + i64 *pnByte /* Sum of bytes in all opened PMAs */ |
| 70759 | +){ |
| 70760 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70761 | + int rc = SQLITE_OK; /* Return code */ |
| 70762 | + int i; /* Used to iterator through aIter[] */ |
| 70763 | + i64 nByte = 0; /* Total bytes in all opened PMAs */ |
| 70764 | + |
| 70765 | + /* Initialize the iterators. */ |
| 70766 | + for(i=0; rc==SQLITE_OK && i<SORTER_MAX_MERGE_COUNT; i++){ |
| 70767 | + VdbeSorterIter *pIter = &pSorter->aIter[i]; |
| 70768 | + rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte); |
| 70769 | + pSorter->iReadOff = pIter->iEof; |
| 70770 | + assert( pSorter->iReadOff<=pSorter->iWriteOff || rc!=SQLITE_OK ); |
| 70771 | + if( pSorter->iReadOff>=pSorter->iWriteOff ) break; |
| 70772 | + } |
| 70773 | + |
| 70774 | + /* Initialize the aTree[] array. */ |
| 70775 | + for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){ |
| 70776 | + rc = vdbeSorterDoCompare(pCsr, i); |
| 70777 | + } |
| 70778 | + |
| 70779 | + *pnByte = nByte; |
| 70780 | + return rc; |
| 70781 | +} |
| 70782 | + |
| 70783 | +/* |
| 70784 | +** Once the sorter has been populated, this function is called to prepare |
| 70785 | +** for iterating through its contents in sorted order. |
| 70786 | +*/ |
| 70787 | +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){ |
| 70788 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70789 | + int rc; /* Return code */ |
| 70790 | + sqlite3_file *pTemp2 = 0; /* Second temp file to use */ |
| 70791 | + i64 iWrite2 = 0; /* Write offset for pTemp2 */ |
| 70792 | + int nIter; /* Number of iterators used */ |
| 70793 | + int nByte; /* Bytes of space required for aIter/aTree */ |
| 70794 | + int N = 2; /* Power of 2 >= nIter */ |
| 70795 | + |
| 70796 | + assert( pSorter ); |
| 70797 | + |
| 70798 | + /* Write the current b-tree to a PMA. Close the b-tree cursor. */ |
| 70799 | + rc = vdbeSorterBtreeToPMA(db, pCsr); |
| 70800 | + sqlite3BtreeCloseCursor(pCsr->pCursor); |
| 70801 | + if( rc!=SQLITE_OK ) return rc; |
| 70802 | + if( pSorter->nPMA==0 ){ |
| 70803 | + *pbEof = 1; |
| 70804 | + return SQLITE_OK; |
| 70805 | + } |
| 70806 | + |
| 70807 | + /* Allocate space for aIter[] and aTree[]. */ |
| 70808 | + nIter = pSorter->nPMA; |
| 70809 | + if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT; |
| 70810 | + assert( nIter>0 ); |
| 70811 | + while( N<nIter ) N += N; |
| 70812 | + nByte = N * (sizeof(int) + sizeof(VdbeSorterIter)); |
| 70813 | + pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte); |
| 70814 | + if( !pSorter->aIter ) return SQLITE_NOMEM; |
| 70815 | + pSorter->aTree = (int *)&pSorter->aIter[N]; |
| 70816 | + pSorter->nTree = N; |
| 70817 | + |
| 70818 | + do { |
| 70819 | + int iNew; /* Index of new, merged, PMA */ |
| 70820 | + |
| 70821 | + for(iNew=0; |
| 70822 | + rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; |
| 70823 | + iNew++ |
| 70824 | + ){ |
| 70825 | + i64 nWrite; /* Number of bytes in new PMA */ |
| 70826 | + |
| 70827 | + /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1, |
| 70828 | + ** initialize an iterator for each of them and break out of the loop. |
| 70829 | + ** These iterators will be incrementally merged as the VDBE layer calls |
| 70830 | + ** sqlite3VdbeSorterNext(). |
| 70831 | + ** |
| 70832 | + ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs, |
| 70833 | + ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs |
| 70834 | + ** are merged into a single PMA that is written to file pTemp2. |
| 70835 | + */ |
| 70836 | + rc = vdbeSorterInitMerge(db, pCsr, &nWrite); |
| 70837 | + assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile ); |
| 70838 | + if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){ |
| 70839 | + break; |
| 70840 | + } |
| 70841 | + |
| 70842 | + /* Open the second temp file, if it is not already open. */ |
| 70843 | + if( pTemp2==0 ){ |
| 70844 | + assert( iWrite2==0 ); |
| 70845 | + rc = vdbeSorterOpenTempFile(db, &pTemp2); |
| 70846 | + } |
| 70847 | + |
| 70848 | + if( rc==SQLITE_OK ){ |
| 70849 | + rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2); |
| 70850 | + } |
| 70851 | + |
| 70852 | + if( rc==SQLITE_OK ){ |
| 70853 | + int bEof = 0; |
| 70854 | + while( rc==SQLITE_OK && bEof==0 ){ |
| 70855 | + int nToWrite; |
| 70856 | + VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ]; |
| 70857 | + assert( pIter->pFile ); |
| 70858 | + nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey); |
| 70859 | + rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2); |
| 70860 | + iWrite2 += nToWrite; |
| 70861 | + if( rc==SQLITE_OK ){ |
| 70862 | + rc = sqlite3VdbeSorterNext(db, pCsr, &bEof); |
| 70863 | + } |
| 70864 | + } |
| 70865 | + } |
| 70866 | + } |
| 70867 | + |
| 70868 | + if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){ |
| 70869 | + break; |
| 70870 | + }else{ |
| 70871 | + sqlite3_file *pTmp = pSorter->pTemp1; |
| 70872 | + pSorter->nPMA = iNew; |
| 70873 | + pSorter->pTemp1 = pTemp2; |
| 70874 | + pTemp2 = pTmp; |
| 70875 | + pSorter->iWriteOff = iWrite2; |
| 70876 | + pSorter->iReadOff = 0; |
| 70877 | + iWrite2 = 0; |
| 70878 | + } |
| 70879 | + }while( rc==SQLITE_OK ); |
| 70880 | + |
| 70881 | + if( pTemp2 ){ |
| 70882 | + sqlite3OsCloseFree(pTemp2); |
| 70883 | + } |
| 70884 | + *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0); |
| 70885 | + return rc; |
| 70886 | +} |
| 70887 | + |
| 70888 | +/* |
| 70889 | +** Advance to the next element in the sorter. |
| 70890 | +*/ |
| 70891 | +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){ |
| 70892 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70893 | + int iPrev = pSorter->aTree[1]; /* Index of iterator to advance */ |
| 70894 | + int i; /* Index of aTree[] to recalculate */ |
| 70895 | + int rc; /* Return code */ |
| 70896 | + |
| 70897 | + rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]); |
| 70898 | + for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){ |
| 70899 | + rc = vdbeSorterDoCompare(pCsr, i); |
| 70900 | + } |
| 70901 | + |
| 70902 | + *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0); |
| 70903 | + return rc; |
| 70904 | +} |
| 70905 | + |
| 70906 | +/* |
| 70907 | +** Copy the current sorter key into the memory cell pOut. |
| 70908 | +*/ |
| 70909 | +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){ |
| 70910 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70911 | + VdbeSorterIter *pIter; |
| 70912 | + |
| 70913 | + pIter = &pSorter->aIter[ pSorter->aTree[1] ]; |
| 70914 | + |
| 70915 | + /* Coverage testing note: As things are currently, this call will always |
| 70916 | + ** succeed. This is because the memory cell passed by the VDBE layer |
| 70917 | + ** happens to be the same one as was used to assemble the keys before they |
| 70918 | + ** were passed to the sorter - meaning it is always large enough for the |
| 70919 | + ** largest key. But this could change very easily, so we leave the call |
| 70920 | + ** to sqlite3VdbeMemGrow() in. */ |
| 70921 | + if( NEVER(sqlite3VdbeMemGrow(pOut, pIter->nKey, 0)) ){ |
| 70922 | + return SQLITE_NOMEM; |
| 70923 | + } |
| 70924 | + pOut->n = pIter->nKey; |
| 70925 | + MemSetTypeFlag(pOut, MEM_Blob); |
| 70926 | + memcpy(pOut->z, pIter->aKey, pIter->nKey); |
| 70927 | + |
| 70928 | + return SQLITE_OK; |
| 70929 | +} |
| 70930 | + |
| 70931 | +#endif /* #ifndef SQLITE_OMIT_MERGE_SORT */ |
| 70932 | + |
| 70933 | +/************** End of vdbesort.c ********************************************/ |
| 69561 | 70934 | /************** Begin file journal.c *****************************************/ |
| 69562 | 70935 | /* |
| 69563 | 70936 | ** 2007 August 22 |
| 69564 | 70937 | ** |
| 69565 | 70938 | ** The author disclaims copyright to this source code. In place of |
| | @@ -70072,10 +71445,12 @@ |
| 70072 | 71445 | ** |
| 70073 | 71446 | ************************************************************************* |
| 70074 | 71447 | ** This file contains routines used for walking the parser tree for |
| 70075 | 71448 | ** an SQL statement. |
| 70076 | 71449 | */ |
| 71450 | +/* #include <stdlib.h> */ |
| 71451 | +/* #include <string.h> */ |
| 70077 | 71452 | |
| 70078 | 71453 | |
| 70079 | 71454 | /* |
| 70080 | 71455 | ** Walk an expression tree. Invoke the callback once for each node |
| 70081 | 71456 | ** of the expression, while decending. (In other words, the callback |
| | @@ -70210,10 +71585,12 @@ |
| 70210 | 71585 | ** |
| 70211 | 71586 | ** This file contains routines used for walking the parser tree and |
| 70212 | 71587 | ** resolve all identifiers by associating them with a particular |
| 70213 | 71588 | ** table and column. |
| 70214 | 71589 | */ |
| 71590 | +/* #include <stdlib.h> */ |
| 71591 | +/* #include <string.h> */ |
| 70215 | 71592 | |
| 70216 | 71593 | /* |
| 70217 | 71594 | ** Turn the pExpr expression into an alias for the iCol-th column of the |
| 70218 | 71595 | ** result set in pEList. |
| 70219 | 71596 | ** |
| | @@ -76012,100 +77389,10 @@ |
| 76012 | 77389 | ** May you find forgiveness for yourself and forgive others. |
| 76013 | 77390 | ** May you share freely, never taking more than you give. |
| 76014 | 77391 | ** |
| 76015 | 77392 | ************************************************************************* |
| 76016 | 77393 | ** This file contains code associated with the ANALYZE command. |
| 76017 | | -** |
| 76018 | | -** The ANALYZE command gather statistics about the content of tables |
| 76019 | | -** and indices. These statistics are made available to the query planner |
| 76020 | | -** to help it make better decisions about how to perform queries. |
| 76021 | | -** |
| 76022 | | -** The following system tables are or have been supported: |
| 76023 | | -** |
| 76024 | | -** CREATE TABLE sqlite_stat1(tbl, idx, stat); |
| 76025 | | -** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); |
| 76026 | | -** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample); |
| 76027 | | -** |
| 76028 | | -** Additional tables might be added in future releases of SQLite. |
| 76029 | | -** The sqlite_stat2 table is not created or used unless the SQLite version |
| 76030 | | -** is between 3.6.18 and 3.7.7, inclusive, and unless SQLite is compiled |
| 76031 | | -** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. |
| 76032 | | -** The sqlite_stat2 table is superceded by sqlite_stat3, which is only |
| 76033 | | -** created and used by SQLite versions after 2011-08-09 with |
| 76034 | | -** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3 |
| 76035 | | -** is a superset of sqlite_stat2. |
| 76036 | | -** |
| 76037 | | -** Format of sqlite_stat1: |
| 76038 | | -** |
| 76039 | | -** There is normally one row per index, with the index identified by the |
| 76040 | | -** name in the idx column. The tbl column is the name of the table to |
| 76041 | | -** which the index belongs. In each such row, the stat column will be |
| 76042 | | -** a string consisting of a list of integers. The first integer in this |
| 76043 | | -** list is the number of rows in the index and in the table. The second |
| 76044 | | -** integer is the average number of rows in the index that have the same |
| 76045 | | -** value in the first column of the index. The third integer is the average |
| 76046 | | -** number of rows in the index that have the same value for the first two |
| 76047 | | -** columns. The N-th integer (for N>1) is the average number of rows in |
| 76048 | | -** the index which have the same value for the first N-1 columns. For |
| 76049 | | -** a K-column index, there will be K+1 integers in the stat column. If |
| 76050 | | -** the index is unique, then the last integer will be 1. |
| 76051 | | -** |
| 76052 | | -** The list of integers in the stat column can optionally be followed |
| 76053 | | -** by the keyword "unordered". The "unordered" keyword, if it is present, |
| 76054 | | -** must be separated from the last integer by a single space. If the |
| 76055 | | -** "unordered" keyword is present, then the query planner assumes that |
| 76056 | | -** the index is unordered and will not use the index for a range query. |
| 76057 | | -** |
| 76058 | | -** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat |
| 76059 | | -** column contains a single integer which is the (estimated) number of |
| 76060 | | -** rows in the table identified by sqlite_stat1.tbl. |
| 76061 | | -** |
| 76062 | | -** Format of sqlite_stat2: |
| 76063 | | -** |
| 76064 | | -** The sqlite_stat2 is only created and is only used if SQLite is compiled |
| 76065 | | -** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between |
| 76066 | | -** 3.6.18 and 3.7.7. The "stat2" table contains additional information |
| 76067 | | -** about the distribution of keys within an index. The index is identified by |
| 76068 | | -** the "idx" column and the "tbl" column is the name of the table to which |
| 76069 | | -** the index belongs. There are usually 10 rows in the sqlite_stat2 |
| 76070 | | -** table for each index. |
| 76071 | | -** |
| 76072 | | -** The sqlite_stat2 entries for an index that have sampleno between 0 and 9 |
| 76073 | | -** inclusive are samples of the left-most key value in the index taken at |
| 76074 | | -** evenly spaced points along the index. Let the number of samples be S |
| 76075 | | -** (10 in the standard build) and let C be the number of rows in the index. |
| 76076 | | -** Then the sampled rows are given by: |
| 76077 | | -** |
| 76078 | | -** rownumber = (i*C*2 + C)/(S*2) |
| 76079 | | -** |
| 76080 | | -** For i between 0 and S-1. Conceptually, the index space is divided into |
| 76081 | | -** S uniform buckets and the samples are the middle row from each bucket. |
| 76082 | | -** |
| 76083 | | -** The format for sqlite_stat2 is recorded here for legacy reference. This |
| 76084 | | -** version of SQLite does not support sqlite_stat2. It neither reads nor |
| 76085 | | -** writes the sqlite_stat2 table. This version of SQLite only supports |
| 76086 | | -** sqlite_stat3. |
| 76087 | | -** |
| 76088 | | -** Format for sqlite_stat3: |
| 76089 | | -** |
| 76090 | | -** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is |
| 76091 | | -** used to avoid compatibility problems. |
| 76092 | | -** |
| 76093 | | -** The format of the sqlite_stat3 table is similar to the format for |
| 76094 | | -** the sqlite_stat2 table, with the following changes: (1) |
| 76095 | | -** The sampleno column is removed. (2) Every sample has nEq, nLt, and nDLt |
| 76096 | | -** columns which hold the approximate number of rows in the table that |
| 76097 | | -** exactly match the sample, the approximate number of rows with values |
| 76098 | | -** less than the sample, and the approximate number of distinct key values |
| 76099 | | -** less than the sample, respectively. (3) The number of samples can vary |
| 76100 | | -** from one table to the next; the sample count does not have to be |
| 76101 | | -** exactly 10 as it is with sqlite_stat2. |
| 76102 | | -** |
| 76103 | | -** The ANALYZE command will typically generate sqlite_stat3 tables |
| 76104 | | -** that contain between 10 and 40 samples which are distributed across |
| 76105 | | -** the key space, though not uniformly, and which include samples with |
| 76106 | | -** largest possible nEq values. |
| 76107 | 77394 | */ |
| 76108 | 77395 | #ifndef SQLITE_OMIT_ANALYZE |
| 76109 | 77396 | |
| 76110 | 77397 | /* |
| 76111 | 77398 | ** This routine generates code that opens the sqlite_stat1 table for |
| | @@ -76133,18 +77420,12 @@ |
| 76133 | 77420 | static const struct { |
| 76134 | 77421 | const char *zName; |
| 76135 | 77422 | const char *zCols; |
| 76136 | 77423 | } aTable[] = { |
| 76137 | 77424 | { "sqlite_stat1", "tbl,idx,stat" }, |
| 76138 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 76139 | | - { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" }, |
| 76140 | | -#endif |
| 76141 | | - }; |
| 76142 | | - static const char *azToDrop[] = { |
| 76143 | | - "sqlite_stat2", |
| 76144 | | -#ifndef SQLITE_ENABLE_STAT3 |
| 76145 | | - "sqlite_stat3", |
| 77425 | +#ifdef SQLITE_ENABLE_STAT2 |
| 77426 | + { "sqlite_stat2", "tbl,idx,sampleno,sample" }, |
| 76146 | 77427 | #endif |
| 76147 | 77428 | }; |
| 76148 | 77429 | |
| 76149 | 77430 | int aRoot[] = {0, 0}; |
| 76150 | 77431 | u8 aCreateTbl[] = {0, 0}; |
| | @@ -76156,21 +77437,10 @@ |
| 76156 | 77437 | if( v==0 ) return; |
| 76157 | 77438 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 76158 | 77439 | assert( sqlite3VdbeDb(v)==db ); |
| 76159 | 77440 | pDb = &db->aDb[iDb]; |
| 76160 | 77441 | |
| 76161 | | - /* Drop all statistics tables that this version of SQLite does not |
| 76162 | | - ** understand. |
| 76163 | | - */ |
| 76164 | | - for(i=0; i<ArraySize(azToDrop); i++){ |
| 76165 | | - Table *pTab = sqlite3FindTable(db, azToDrop[i], pDb->zName); |
| 76166 | | - if( pTab ) sqlite3CodeDropTable(pParse, pTab, iDb, 0); |
| 76167 | | - } |
| 76168 | | - |
| 76169 | | - /* Create new statistic tables if they do not exist, or clear them |
| 76170 | | - ** if they do already exist. |
| 76171 | | - */ |
| 76172 | 77442 | for(i=0; i<ArraySize(aTable); i++){ |
| 76173 | 77443 | const char *zTab = aTable[i].zName; |
| 76174 | 77444 | Table *pStat; |
| 76175 | 77445 | if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){ |
| 76176 | 77446 | /* The sqlite_stat[12] table does not exist. Create it. Note that a |
| | @@ -76197,238 +77467,17 @@ |
| 76197 | 77467 | sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); |
| 76198 | 77468 | } |
| 76199 | 77469 | } |
| 76200 | 77470 | } |
| 76201 | 77471 | |
| 76202 | | - /* Open the sqlite_stat[13] tables for writing. */ |
| 77472 | + /* Open the sqlite_stat[12] tables for writing. */ |
| 76203 | 77473 | for(i=0; i<ArraySize(aTable); i++){ |
| 76204 | 77474 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb); |
| 76205 | 77475 | sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32); |
| 76206 | 77476 | sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
| 76207 | 77477 | } |
| 76208 | 77478 | } |
| 76209 | | - |
| 76210 | | -/* |
| 76211 | | -** Recommended number of samples for sqlite_stat3 |
| 76212 | | -*/ |
| 76213 | | -#ifndef SQLITE_STAT3_SAMPLES |
| 76214 | | -# define SQLITE_STAT3_SAMPLES 24 |
| 76215 | | -#endif |
| 76216 | | - |
| 76217 | | -/* |
| 76218 | | -** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() - |
| 76219 | | -** share an instance of the following structure to hold their state |
| 76220 | | -** information. |
| 76221 | | -*/ |
| 76222 | | -typedef struct Stat3Accum Stat3Accum; |
| 76223 | | -struct Stat3Accum { |
| 76224 | | - tRowcnt nRow; /* Number of rows in the entire table */ |
| 76225 | | - tRowcnt nPSample; /* How often to do a periodic sample */ |
| 76226 | | - int iMin; /* Index of entry with minimum nEq and hash */ |
| 76227 | | - int mxSample; /* Maximum number of samples to accumulate */ |
| 76228 | | - int nSample; /* Current number of samples */ |
| 76229 | | - u32 iPrn; /* Pseudo-random number used for sampling */ |
| 76230 | | - struct Stat3Sample { |
| 76231 | | - i64 iRowid; /* Rowid in main table of the key */ |
| 76232 | | - tRowcnt nEq; /* sqlite_stat3.nEq */ |
| 76233 | | - tRowcnt nLt; /* sqlite_stat3.nLt */ |
| 76234 | | - tRowcnt nDLt; /* sqlite_stat3.nDLt */ |
| 76235 | | - u8 isPSample; /* True if a periodic sample */ |
| 76236 | | - u32 iHash; /* Tiebreaker hash */ |
| 76237 | | - } *a; /* An array of samples */ |
| 76238 | | -}; |
| 76239 | | - |
| 76240 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 76241 | | -/* |
| 76242 | | -** Implementation of the stat3_init(C,S) SQL function. The two parameters |
| 76243 | | -** are the number of rows in the table or index (C) and the number of samples |
| 76244 | | -** to accumulate (S). |
| 76245 | | -** |
| 76246 | | -** This routine allocates the Stat3Accum object. |
| 76247 | | -** |
| 76248 | | -** The return value is the Stat3Accum object (P). |
| 76249 | | -*/ |
| 76250 | | -static void stat3Init( |
| 76251 | | - sqlite3_context *context, |
| 76252 | | - int argc, |
| 76253 | | - sqlite3_value **argv |
| 76254 | | -){ |
| 76255 | | - Stat3Accum *p; |
| 76256 | | - tRowcnt nRow; |
| 76257 | | - int mxSample; |
| 76258 | | - int n; |
| 76259 | | - |
| 76260 | | - UNUSED_PARAMETER(argc); |
| 76261 | | - nRow = (tRowcnt)sqlite3_value_int64(argv[0]); |
| 76262 | | - mxSample = sqlite3_value_int(argv[1]); |
| 76263 | | - n = sizeof(*p) + sizeof(p->a[0])*mxSample; |
| 76264 | | - p = sqlite3_malloc( n ); |
| 76265 | | - if( p==0 ){ |
| 76266 | | - sqlite3_result_error_nomem(context); |
| 76267 | | - return; |
| 76268 | | - } |
| 76269 | | - memset(p, 0, n); |
| 76270 | | - p->a = (struct Stat3Sample*)&p[1]; |
| 76271 | | - p->nRow = nRow; |
| 76272 | | - p->mxSample = mxSample; |
| 76273 | | - p->nPSample = p->nRow/(mxSample/3+1) + 1; |
| 76274 | | - sqlite3_randomness(sizeof(p->iPrn), &p->iPrn); |
| 76275 | | - sqlite3_result_blob(context, p, sizeof(p), sqlite3_free); |
| 76276 | | -} |
| 76277 | | -static const FuncDef stat3InitFuncdef = { |
| 76278 | | - 2, /* nArg */ |
| 76279 | | - SQLITE_UTF8, /* iPrefEnc */ |
| 76280 | | - 0, /* flags */ |
| 76281 | | - 0, /* pUserData */ |
| 76282 | | - 0, /* pNext */ |
| 76283 | | - stat3Init, /* xFunc */ |
| 76284 | | - 0, /* xStep */ |
| 76285 | | - 0, /* xFinalize */ |
| 76286 | | - "stat3_init", /* zName */ |
| 76287 | | - 0, /* pHash */ |
| 76288 | | - 0 /* pDestructor */ |
| 76289 | | -}; |
| 76290 | | - |
| 76291 | | - |
| 76292 | | -/* |
| 76293 | | -** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The |
| 76294 | | -** arguments describe a single key instance. This routine makes the |
| 76295 | | -** decision about whether or not to retain this key for the sqlite_stat3 |
| 76296 | | -** table. |
| 76297 | | -** |
| 76298 | | -** The return value is NULL. |
| 76299 | | -*/ |
| 76300 | | -static void stat3Push( |
| 76301 | | - sqlite3_context *context, |
| 76302 | | - int argc, |
| 76303 | | - sqlite3_value **argv |
| 76304 | | -){ |
| 76305 | | - Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]); |
| 76306 | | - tRowcnt nEq = sqlite3_value_int64(argv[0]); |
| 76307 | | - tRowcnt nLt = sqlite3_value_int64(argv[1]); |
| 76308 | | - tRowcnt nDLt = sqlite3_value_int64(argv[2]); |
| 76309 | | - i64 rowid = sqlite3_value_int64(argv[3]); |
| 76310 | | - u8 isPSample = 0; |
| 76311 | | - u8 doInsert = 0; |
| 76312 | | - int iMin = p->iMin; |
| 76313 | | - struct Stat3Sample *pSample; |
| 76314 | | - int i; |
| 76315 | | - u32 h; |
| 76316 | | - |
| 76317 | | - UNUSED_PARAMETER(context); |
| 76318 | | - UNUSED_PARAMETER(argc); |
| 76319 | | - if( nEq==0 ) return; |
| 76320 | | - h = p->iPrn = p->iPrn*1103515245 + 12345; |
| 76321 | | - if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){ |
| 76322 | | - doInsert = isPSample = 1; |
| 76323 | | - }else if( p->nSample<p->mxSample ){ |
| 76324 | | - doInsert = 1; |
| 76325 | | - }else{ |
| 76326 | | - if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){ |
| 76327 | | - doInsert = 1; |
| 76328 | | - } |
| 76329 | | - } |
| 76330 | | - if( !doInsert ) return; |
| 76331 | | - if( p->nSample==p->mxSample ){ |
| 76332 | | - if( iMin<p->nSample ){ |
| 76333 | | - memcpy(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin)); |
| 76334 | | - } |
| 76335 | | - pSample = &p->a[p->nSample-1]; |
| 76336 | | - }else{ |
| 76337 | | - pSample = &p->a[p->nSample++]; |
| 76338 | | - } |
| 76339 | | - pSample->iRowid = rowid; |
| 76340 | | - pSample->nEq = nEq; |
| 76341 | | - pSample->nLt = nLt; |
| 76342 | | - pSample->nDLt = nDLt; |
| 76343 | | - pSample->iHash = h; |
| 76344 | | - pSample->isPSample = isPSample; |
| 76345 | | - |
| 76346 | | - /* Find the new minimum */ |
| 76347 | | - if( p->nSample==p->mxSample ){ |
| 76348 | | - pSample = p->a; |
| 76349 | | - i = 0; |
| 76350 | | - while( pSample->isPSample ){ |
| 76351 | | - i++; |
| 76352 | | - pSample++; |
| 76353 | | - assert( i<p->nSample ); |
| 76354 | | - } |
| 76355 | | - nEq = pSample->nEq; |
| 76356 | | - h = pSample->iHash; |
| 76357 | | - iMin = i; |
| 76358 | | - for(i++, pSample++; i<p->nSample; i++, pSample++){ |
| 76359 | | - if( pSample->isPSample ) continue; |
| 76360 | | - if( pSample->nEq<nEq |
| 76361 | | - || (pSample->nEq==nEq && pSample->iHash<h) |
| 76362 | | - ){ |
| 76363 | | - iMin = i; |
| 76364 | | - nEq = pSample->nEq; |
| 76365 | | - h = pSample->iHash; |
| 76366 | | - } |
| 76367 | | - } |
| 76368 | | - p->iMin = iMin; |
| 76369 | | - } |
| 76370 | | -} |
| 76371 | | -static const FuncDef stat3PushFuncdef = { |
| 76372 | | - 5, /* nArg */ |
| 76373 | | - SQLITE_UTF8, /* iPrefEnc */ |
| 76374 | | - 0, /* flags */ |
| 76375 | | - 0, /* pUserData */ |
| 76376 | | - 0, /* pNext */ |
| 76377 | | - stat3Push, /* xFunc */ |
| 76378 | | - 0, /* xStep */ |
| 76379 | | - 0, /* xFinalize */ |
| 76380 | | - "stat3_push", /* zName */ |
| 76381 | | - 0, /* pHash */ |
| 76382 | | - 0 /* pDestructor */ |
| 76383 | | -}; |
| 76384 | | - |
| 76385 | | -/* |
| 76386 | | -** Implementation of the stat3_get(P,N,...) SQL function. This routine is |
| 76387 | | -** used to query the results. Content is returned for the Nth sqlite_stat3 |
| 76388 | | -** row where N is between 0 and S-1 and S is the number of samples. The |
| 76389 | | -** value returned depends on the number of arguments. |
| 76390 | | -** |
| 76391 | | -** argc==2 result: rowid |
| 76392 | | -** argc==3 result: nEq |
| 76393 | | -** argc==4 result: nLt |
| 76394 | | -** argc==5 result: nDLt |
| 76395 | | -*/ |
| 76396 | | -static void stat3Get( |
| 76397 | | - sqlite3_context *context, |
| 76398 | | - int argc, |
| 76399 | | - sqlite3_value **argv |
| 76400 | | -){ |
| 76401 | | - int n = sqlite3_value_int(argv[1]); |
| 76402 | | - Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]); |
| 76403 | | - |
| 76404 | | - assert( p!=0 ); |
| 76405 | | - if( p->nSample<=n ) return; |
| 76406 | | - switch( argc ){ |
| 76407 | | - case 2: sqlite3_result_int64(context, p->a[n].iRowid); break; |
| 76408 | | - case 3: sqlite3_result_int64(context, p->a[n].nEq); break; |
| 76409 | | - case 4: sqlite3_result_int64(context, p->a[n].nLt); break; |
| 76410 | | - case 5: sqlite3_result_int64(context, p->a[n].nDLt); break; |
| 76411 | | - } |
| 76412 | | -} |
| 76413 | | -static const FuncDef stat3GetFuncdef = { |
| 76414 | | - -1, /* nArg */ |
| 76415 | | - SQLITE_UTF8, /* iPrefEnc */ |
| 76416 | | - 0, /* flags */ |
| 76417 | | - 0, /* pUserData */ |
| 76418 | | - 0, /* pNext */ |
| 76419 | | - stat3Get, /* xFunc */ |
| 76420 | | - 0, /* xStep */ |
| 76421 | | - 0, /* xFinalize */ |
| 76422 | | - "stat3_get", /* zName */ |
| 76423 | | - 0, /* pHash */ |
| 76424 | | - 0 /* pDestructor */ |
| 76425 | | -}; |
| 76426 | | -#endif /* SQLITE_ENABLE_STAT3 */ |
| 76427 | | - |
| 76428 | | - |
| 76429 | | - |
| 76430 | 77479 | |
| 76431 | 77480 | /* |
| 76432 | 77481 | ** Generate code to do an analysis of all indices associated with |
| 76433 | 77482 | ** a single table. |
| 76434 | 77483 | */ |
| | @@ -76448,31 +77497,24 @@ |
| 76448 | 77497 | int endOfLoop; /* The end of the loop */ |
| 76449 | 77498 | int jZeroRows = -1; /* Jump from here if number of rows is zero */ |
| 76450 | 77499 | int iDb; /* Index of database containing pTab */ |
| 76451 | 77500 | int regTabname = iMem++; /* Register containing table name */ |
| 76452 | 77501 | int regIdxname = iMem++; /* Register containing index name */ |
| 76453 | | - int regStat1 = iMem++; /* The stat column of sqlite_stat1 */ |
| 76454 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 76455 | | - int regNumEq = regStat1; /* Number of instances. Same as regStat1 */ |
| 76456 | | - int regNumLt = iMem++; /* Number of keys less than regSample */ |
| 76457 | | - int regNumDLt = iMem++; /* Number of distinct keys less than regSample */ |
| 76458 | | - int regSample = iMem++; /* The next sample value */ |
| 76459 | | - int regRowid = regSample; /* Rowid of a sample */ |
| 76460 | | - int regAccum = iMem++; /* Register to hold Stat3Accum object */ |
| 76461 | | - int regLoop = iMem++; /* Loop counter */ |
| 76462 | | - int regCount = iMem++; /* Number of rows in the table or index */ |
| 76463 | | - int regTemp1 = iMem++; /* Intermediate register */ |
| 76464 | | - int regTemp2 = iMem++; /* Intermediate register */ |
| 76465 | | - int once = 1; /* One-time initialization */ |
| 76466 | | - int shortJump = 0; /* Instruction address */ |
| 76467 | | - int iTabCur = pParse->nTab++; /* Table cursor */ |
| 76468 | | -#endif |
| 76469 | | - int regCol = iMem++; /* Content of a column in analyzed table */ |
| 77502 | + int regSampleno = iMem++; /* Register containing next sample number */ |
| 77503 | + int regCol = iMem++; /* Content of a column analyzed table */ |
| 76470 | 77504 | int regRec = iMem++; /* Register holding completed record */ |
| 76471 | 77505 | int regTemp = iMem++; /* Temporary use register */ |
| 76472 | | - int regNewRowid = iMem++; /* Rowid for the inserted record */ |
| 77506 | + int regRowid = iMem++; /* Rowid for the inserted record */ |
| 76473 | 77507 | |
| 77508 | +#ifdef SQLITE_ENABLE_STAT2 |
| 77509 | + int addr = 0; /* Instruction address */ |
| 77510 | + int regTemp2 = iMem++; /* Temporary use register */ |
| 77511 | + int regSamplerecno = iMem++; /* Index of next sample to record */ |
| 77512 | + int regRecno = iMem++; /* Current sample index */ |
| 77513 | + int regLast = iMem++; /* Index of last sample to record */ |
| 77514 | + int regFirst = iMem++; /* Index of first sample to record */ |
| 77515 | +#endif |
| 76474 | 77516 | |
| 76475 | 77517 | v = sqlite3GetVdbe(pParse); |
| 76476 | 77518 | if( v==0 || NEVER(pTab==0) ){ |
| 76477 | 77519 | return; |
| 76478 | 77520 | } |
| | @@ -76501,22 +77543,17 @@ |
| 76501 | 77543 | iIdxCur = pParse->nTab++; |
| 76502 | 77544 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); |
| 76503 | 77545 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 76504 | 77546 | int nCol; |
| 76505 | 77547 | KeyInfo *pKey; |
| 76506 | | - int addrIfNot = 0; /* address of OP_IfNot */ |
| 76507 | | - int *aChngAddr; /* Array of jump instruction addresses */ |
| 76508 | 77548 | |
| 76509 | 77549 | if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; |
| 76510 | | - VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName)); |
| 76511 | 77550 | nCol = pIdx->nColumn; |
| 76512 | 77551 | pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 76513 | 77552 | if( iMem+1+(nCol*2)>pParse->nMem ){ |
| 76514 | 77553 | pParse->nMem = iMem+1+(nCol*2); |
| 76515 | 77554 | } |
| 76516 | | - aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*pIdx->nColumn); |
| 76517 | | - if( aChngAddr==0 ) continue; |
| 76518 | 77555 | |
| 76519 | 77556 | /* Open a cursor to the index to be analyzed. */ |
| 76520 | 77557 | assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
| 76521 | 77558 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, |
| 76522 | 77559 | (char *)pKey, P4_KEYINFO_HANDOFF); |
| | @@ -76523,24 +77560,35 @@ |
| 76523 | 77560 | VdbeComment((v, "%s", pIdx->zName)); |
| 76524 | 77561 | |
| 76525 | 77562 | /* Populate the register containing the index name. */ |
| 76526 | 77563 | sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); |
| 76527 | 77564 | |
| 76528 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 76529 | | - if( once ){ |
| 76530 | | - once = 0; |
| 76531 | | - sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); |
| 76532 | | - } |
| 76533 | | - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount); |
| 76534 | | - sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1); |
| 76535 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq); |
| 76536 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt); |
| 76537 | | - sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt); |
| 76538 | | - sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum, |
| 76539 | | - (char*)&stat3InitFuncdef, P4_FUNCDEF); |
| 76540 | | - sqlite3VdbeChangeP5(v, 2); |
| 76541 | | -#endif /* SQLITE_ENABLE_STAT3 */ |
| 77565 | +#ifdef SQLITE_ENABLE_STAT2 |
| 77566 | + |
| 77567 | + /* If this iteration of the loop is generating code to analyze the |
| 77568 | + ** first index in the pTab->pIndex list, then register regLast has |
| 77569 | + ** not been populated. In this case populate it now. */ |
| 77570 | + if( pTab->pIndex==pIdx ){ |
| 77571 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno); |
| 77572 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp); |
| 77573 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2); |
| 77574 | + |
| 77575 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast); |
| 77576 | + sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst); |
| 77577 | + addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast); |
| 77578 | + sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst); |
| 77579 | + sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast); |
| 77580 | + sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2); |
| 77581 | + sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast); |
| 77582 | + sqlite3VdbeJumpHere(v, addr); |
| 77583 | + } |
| 77584 | + |
| 77585 | + /* Zero the regSampleno and regRecno registers. */ |
| 77586 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno); |
| 77587 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno); |
| 77588 | + sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno); |
| 77589 | +#endif |
| 76542 | 77590 | |
| 76543 | 77591 | /* The block of memory cells initialized here is used as follows. |
| 76544 | 77592 | ** |
| 76545 | 77593 | ** iMem: |
| 76546 | 77594 | ** The total number of rows in the table. |
| | @@ -76566,87 +77614,79 @@ |
| 76566 | 77614 | /* Start the analysis loop. This loop runs through all the entries in |
| 76567 | 77615 | ** the index b-tree. */ |
| 76568 | 77616 | endOfLoop = sqlite3VdbeMakeLabel(v); |
| 76569 | 77617 | sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop); |
| 76570 | 77618 | topOfLoop = sqlite3VdbeCurrentAddr(v); |
| 76571 | | - sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */ |
| 77619 | + sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); |
| 76572 | 77620 | |
| 76573 | 77621 | for(i=0; i<nCol; i++){ |
| 76574 | 77622 | CollSeq *pColl; |
| 76575 | 77623 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol); |
| 76576 | 77624 | if( i==0 ){ |
| 77625 | +#ifdef SQLITE_ENABLE_STAT2 |
| 77626 | + /* Check if the record that cursor iIdxCur points to contains a |
| 77627 | + ** value that should be stored in the sqlite_stat2 table. If so, |
| 77628 | + ** store it. */ |
| 77629 | + int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno); |
| 77630 | + assert( regTabname+1==regIdxname |
| 77631 | + && regTabname+2==regSampleno |
| 77632 | + && regTabname+3==regCol |
| 77633 | + ); |
| 77634 | + sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); |
| 77635 | + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0); |
| 77636 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid); |
| 77637 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid); |
| 77638 | + |
| 77639 | + /* Calculate new values for regSamplerecno and regSampleno. |
| 77640 | + ** |
| 77641 | + ** sampleno = sampleno + 1 |
| 77642 | + ** samplerecno = samplerecno+(remaining records)/(remaining samples) |
| 77643 | + */ |
| 77644 | + sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1); |
| 77645 | + sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp); |
| 77646 | + sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 77647 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2); |
| 77648 | + sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2); |
| 77649 | + sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp); |
| 77650 | + sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno); |
| 77651 | + |
| 77652 | + sqlite3VdbeJumpHere(v, ne); |
| 77653 | + sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1); |
| 77654 | +#endif |
| 77655 | + |
| 76577 | 77656 | /* Always record the very first row */ |
| 76578 | | - addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 77657 | + sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 76579 | 77658 | } |
| 76580 | 77659 | assert( pIdx->azColl!=0 ); |
| 76581 | 77660 | assert( pIdx->azColl[i]!=0 ); |
| 76582 | 77661 | pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 76583 | | - aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 76584 | | - (char*)pColl, P4_COLLSEQ); |
| 77662 | + sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 77663 | + (char*)pColl, P4_COLLSEQ); |
| 76585 | 77664 | sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
| 76586 | | - VdbeComment((v, "jump if column %d changed", i)); |
| 76587 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 76588 | | - if( i==0 ){ |
| 76589 | | - sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1); |
| 76590 | | - VdbeComment((v, "incr repeat count")); |
| 76591 | | - } |
| 76592 | | -#endif |
| 77665 | + } |
| 77666 | + if( db->mallocFailed ){ |
| 77667 | + /* If a malloc failure has occurred, then the result of the expression |
| 77668 | + ** passed as the second argument to the call to sqlite3VdbeJumpHere() |
| 77669 | + ** below may be negative. Which causes an assert() to fail (or an |
| 77670 | + ** out-of-bounds write if SQLITE_DEBUG is not defined). */ |
| 77671 | + return; |
| 76593 | 77672 | } |
| 76594 | 77673 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop); |
| 76595 | 77674 | for(i=0; i<nCol; i++){ |
| 76596 | | - sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */ |
| 77675 | + int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2); |
| 76597 | 77676 | if( i==0 ){ |
| 76598 | | - sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */ |
| 76599 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 76600 | | - sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, |
| 76601 | | - (char*)&stat3PushFuncdef, P4_FUNCDEF); |
| 76602 | | - sqlite3VdbeChangeP5(v, 5); |
| 76603 | | - sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid); |
| 76604 | | - sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt); |
| 76605 | | - sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1); |
| 76606 | | - sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq); |
| 76607 | | -#endif |
| 76608 | | - } |
| 77677 | + sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */ |
| 77678 | + } |
| 77679 | + sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */ |
| 76609 | 77680 | sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1); |
| 76610 | 77681 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1); |
| 76611 | 77682 | } |
| 76612 | | - sqlite3DbFree(db, aChngAddr); |
| 76613 | 77683 | |
| 76614 | | - /* Always jump here after updating the iMem+1...iMem+1+nCol counters */ |
| 77684 | + /* End of the analysis loop. */ |
| 76615 | 77685 | sqlite3VdbeResolveLabel(v, endOfLoop); |
| 76616 | | - |
| 76617 | 77686 | sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop); |
| 76618 | 77687 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 76619 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 76620 | | - sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, |
| 76621 | | - (char*)&stat3PushFuncdef, P4_FUNCDEF); |
| 76622 | | - sqlite3VdbeChangeP5(v, 5); |
| 76623 | | - sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop); |
| 76624 | | - shortJump = |
| 76625 | | - sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1); |
| 76626 | | - sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1, |
| 76627 | | - (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 76628 | | - sqlite3VdbeChangeP5(v, 2); |
| 76629 | | - sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1); |
| 76630 | | - sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1); |
| 76631 | | - sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample); |
| 76632 | | - sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample); |
| 76633 | | - sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq, |
| 76634 | | - (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 76635 | | - sqlite3VdbeChangeP5(v, 3); |
| 76636 | | - sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt, |
| 76637 | | - (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 76638 | | - sqlite3VdbeChangeP5(v, 4); |
| 76639 | | - sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt, |
| 76640 | | - (char*)&stat3GetFuncdef, P4_FUNCDEF); |
| 76641 | | - sqlite3VdbeChangeP5(v, 5); |
| 76642 | | - sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0); |
| 76643 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid); |
| 76644 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid); |
| 76645 | | - sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump); |
| 76646 | | - sqlite3VdbeJumpHere(v, shortJump+2); |
| 76647 | | -#endif |
| 76648 | 77688 | |
| 76649 | 77689 | /* Store the results in sqlite_stat1. |
| 76650 | 77690 | ** |
| 76651 | 77691 | ** The result is a single row of the sqlite_stat1 table. The first |
| 76652 | 77692 | ** two columns are the names of the table and index. The third column |
| | @@ -76662,51 +77702,50 @@ |
| 76662 | 77702 | ** |
| 76663 | 77703 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 76664 | 77704 | ** If K>0 then it is always the case the D>0 so division by zero |
| 76665 | 77705 | ** is never possible. |
| 76666 | 77706 | */ |
| 76667 | | - sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1); |
| 77707 | + sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); |
| 76668 | 77708 | if( jZeroRows<0 ){ |
| 76669 | 77709 | jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); |
| 76670 | 77710 | } |
| 76671 | 77711 | for(i=0; i<nCol; i++){ |
| 76672 | 77712 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); |
| 76673 | | - sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); |
| 77713 | + sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 76674 | 77714 | sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); |
| 76675 | 77715 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 76676 | 77716 | sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp); |
| 76677 | 77717 | sqlite3VdbeAddOp1(v, OP_ToInt, regTemp); |
| 76678 | | - sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); |
| 77718 | + sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 76679 | 77719 | } |
| 76680 | 77720 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 76681 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 76682 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); |
| 77721 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 77722 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 76683 | 77723 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 76684 | 77724 | } |
| 76685 | 77725 | |
| 76686 | 77726 | /* If the table has no indices, create a single sqlite_stat1 entry |
| 76687 | 77727 | ** containing NULL as the index name and the row count as the content. |
| 76688 | 77728 | */ |
| 76689 | 77729 | if( pTab->pIndex==0 ){ |
| 76690 | 77730 | sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); |
| 76691 | 77731 | VdbeComment((v, "%s", pTab->zName)); |
| 76692 | | - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1); |
| 77732 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno); |
| 76693 | 77733 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 76694 | | - jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); |
| 77734 | + jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno); |
| 76695 | 77735 | }else{ |
| 76696 | 77736 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 76697 | 77737 | jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto); |
| 76698 | 77738 | } |
| 76699 | 77739 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); |
| 76700 | 77740 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 76701 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 76702 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); |
| 77741 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 77742 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 76703 | 77743 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 76704 | 77744 | if( pParse->nMem<regRec ) pParse->nMem = regRec; |
| 76705 | 77745 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 76706 | 77746 | } |
| 76707 | | - |
| 76708 | 77747 | |
| 76709 | 77748 | /* |
| 76710 | 77749 | ** Generate code that will cause the most recent index analysis to |
| 76711 | 77750 | ** be loaded into internal hash tables where is can be used. |
| 76712 | 77751 | */ |
| | @@ -76727,11 +77766,11 @@ |
| 76727 | 77766 | int iStatCur; |
| 76728 | 77767 | int iMem; |
| 76729 | 77768 | |
| 76730 | 77769 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 76731 | 77770 | iStatCur = pParse->nTab; |
| 76732 | | - pParse->nTab += 3; |
| 77771 | + pParse->nTab += 2; |
| 76733 | 77772 | openStatTable(pParse, iDb, iStatCur, 0, 0); |
| 76734 | 77773 | iMem = pParse->nMem+1; |
| 76735 | 77774 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 76736 | 77775 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 76737 | 77776 | Table *pTab = (Table*)sqliteHashData(k); |
| | @@ -76752,11 +77791,11 @@ |
| 76752 | 77791 | assert( pTab!=0 ); |
| 76753 | 77792 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
| 76754 | 77793 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 76755 | 77794 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 76756 | 77795 | iStatCur = pParse->nTab; |
| 76757 | | - pParse->nTab += 3; |
| 77796 | + pParse->nTab += 2; |
| 76758 | 77797 | if( pOnlyIdx ){ |
| 76759 | 77798 | openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); |
| 76760 | 77799 | }else{ |
| 76761 | 77800 | openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); |
| 76762 | 77801 | } |
| | @@ -76857,11 +77896,11 @@ |
| 76857 | 77896 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
| 76858 | 77897 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 76859 | 77898 | Index *pIndex; |
| 76860 | 77899 | Table *pTable; |
| 76861 | 77900 | int i, c, n; |
| 76862 | | - tRowcnt v; |
| 77901 | + unsigned int v; |
| 76863 | 77902 | const char *z; |
| 76864 | 77903 | |
| 76865 | 77904 | assert( argc==3 ); |
| 76866 | 77905 | UNUSED_PARAMETER2(NotUsed, argc); |
| 76867 | 77906 | |
| | @@ -76900,172 +77939,40 @@ |
| 76900 | 77939 | /* |
| 76901 | 77940 | ** If the Index.aSample variable is not NULL, delete the aSample[] array |
| 76902 | 77941 | ** and its contents. |
| 76903 | 77942 | */ |
| 76904 | 77943 | SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ |
| 76905 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 77944 | +#ifdef SQLITE_ENABLE_STAT2 |
| 76906 | 77945 | if( pIdx->aSample ){ |
| 76907 | 77946 | int j; |
| 76908 | | - for(j=0; j<pIdx->nSample; j++){ |
| 77947 | + for(j=0; j<SQLITE_INDEX_SAMPLES; j++){ |
| 76909 | 77948 | IndexSample *p = &pIdx->aSample[j]; |
| 76910 | 77949 | if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ |
| 76911 | | - sqlite3_free(p->u.z); |
| 77950 | + sqlite3DbFree(db, p->u.z); |
| 76912 | 77951 | } |
| 76913 | 77952 | } |
| 76914 | | - sqlite3_free(pIdx->aSample); |
| 77953 | + sqlite3DbFree(db, pIdx->aSample); |
| 76915 | 77954 | } |
| 76916 | | - UNUSED_PARAMETER(db); |
| 76917 | | - pIdx->nSample = 0; |
| 76918 | | - pIdx->aSample = 0; |
| 76919 | 77955 | #else |
| 76920 | 77956 | UNUSED_PARAMETER(db); |
| 76921 | 77957 | UNUSED_PARAMETER(pIdx); |
| 76922 | 77958 | #endif |
| 76923 | 77959 | } |
| 76924 | 77960 | |
| 76925 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 76926 | | -/* |
| 76927 | | -** Load content from the sqlite_stat3 table into the Index.aSample[] |
| 76928 | | -** arrays of all indices. |
| 76929 | | -*/ |
| 76930 | | -static int loadStat3(sqlite3 *db, const char *zDb){ |
| 76931 | | - int rc; /* Result codes from subroutines */ |
| 76932 | | - sqlite3_stmt *pStmt = 0; /* An SQL statement being run */ |
| 76933 | | - char *zSql; /* Text of the SQL statement */ |
| 76934 | | - Index *pPrevIdx = 0; /* Previous index in the loop */ |
| 76935 | | - int idx = 0; /* slot in pIdx->aSample[] for next sample */ |
| 76936 | | - int eType; /* Datatype of a sample */ |
| 76937 | | - IndexSample *pSample; /* A slot in pIdx->aSample[] */ |
| 76938 | | - |
| 76939 | | - if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){ |
| 76940 | | - return SQLITE_OK; |
| 76941 | | - } |
| 76942 | | - |
| 76943 | | - zSql = sqlite3MPrintf(db, |
| 76944 | | - "SELECT idx,count(*) FROM %Q.sqlite_stat3" |
| 76945 | | - " GROUP BY idx", zDb); |
| 76946 | | - if( !zSql ){ |
| 76947 | | - return SQLITE_NOMEM; |
| 76948 | | - } |
| 76949 | | - rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 76950 | | - sqlite3DbFree(db, zSql); |
| 76951 | | - if( rc ) return rc; |
| 76952 | | - |
| 76953 | | - while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 76954 | | - char *zIndex; /* Index name */ |
| 76955 | | - Index *pIdx; /* Pointer to the index object */ |
| 76956 | | - int nSample; /* Number of samples */ |
| 76957 | | - |
| 76958 | | - zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 76959 | | - if( zIndex==0 ) continue; |
| 76960 | | - nSample = sqlite3_column_int(pStmt, 1); |
| 76961 | | - if( nSample>255 ) continue; |
| 76962 | | - pIdx = sqlite3FindIndex(db, zIndex, zDb); |
| 76963 | | - if( pIdx==0 ) continue; |
| 76964 | | - assert( pIdx->nSample==0 ); |
| 76965 | | - pIdx->nSample = (u8)nSample; |
| 76966 | | - pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) ); |
| 76967 | | - pIdx->avgEq = pIdx->aiRowEst[1]; |
| 76968 | | - if( pIdx->aSample==0 ){ |
| 76969 | | - db->mallocFailed = 1; |
| 76970 | | - sqlite3_finalize(pStmt); |
| 76971 | | - return SQLITE_NOMEM; |
| 76972 | | - } |
| 76973 | | - } |
| 76974 | | - sqlite3_finalize(pStmt); |
| 76975 | | - |
| 76976 | | - zSql = sqlite3MPrintf(db, |
| 76977 | | - "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb); |
| 76978 | | - if( !zSql ){ |
| 76979 | | - return SQLITE_NOMEM; |
| 76980 | | - } |
| 76981 | | - rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 76982 | | - sqlite3DbFree(db, zSql); |
| 76983 | | - if( rc ) return rc; |
| 76984 | | - |
| 76985 | | - while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 76986 | | - char *zIndex; /* Index name */ |
| 76987 | | - Index *pIdx; /* Pointer to the index object */ |
| 76988 | | - int i; /* Loop counter */ |
| 76989 | | - tRowcnt sumEq; /* Sum of the nEq values */ |
| 76990 | | - |
| 76991 | | - zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 76992 | | - if( zIndex==0 ) continue; |
| 76993 | | - pIdx = sqlite3FindIndex(db, zIndex, zDb); |
| 76994 | | - if( pIdx==0 ) continue; |
| 76995 | | - if( pIdx==pPrevIdx ){ |
| 76996 | | - idx++; |
| 76997 | | - }else{ |
| 76998 | | - pPrevIdx = pIdx; |
| 76999 | | - idx = 0; |
| 77000 | | - } |
| 77001 | | - assert( idx<pIdx->nSample ); |
| 77002 | | - pSample = &pIdx->aSample[idx]; |
| 77003 | | - pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1); |
| 77004 | | - pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2); |
| 77005 | | - pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3); |
| 77006 | | - if( idx==pIdx->nSample-1 ){ |
| 77007 | | - if( pSample->nDLt>0 ){ |
| 77008 | | - for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq; |
| 77009 | | - pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt; |
| 77010 | | - } |
| 77011 | | - if( pIdx->avgEq<=0 ) pIdx->avgEq = 1; |
| 77012 | | - } |
| 77013 | | - eType = sqlite3_column_type(pStmt, 4); |
| 77014 | | - pSample->eType = (u8)eType; |
| 77015 | | - switch( eType ){ |
| 77016 | | - case SQLITE_INTEGER: { |
| 77017 | | - pSample->u.i = sqlite3_column_int64(pStmt, 4); |
| 77018 | | - break; |
| 77019 | | - } |
| 77020 | | - case SQLITE_FLOAT: { |
| 77021 | | - pSample->u.r = sqlite3_column_double(pStmt, 4); |
| 77022 | | - break; |
| 77023 | | - } |
| 77024 | | - case SQLITE_NULL: { |
| 77025 | | - break; |
| 77026 | | - } |
| 77027 | | - default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); { |
| 77028 | | - const char *z = (const char *)( |
| 77029 | | - (eType==SQLITE_BLOB) ? |
| 77030 | | - sqlite3_column_blob(pStmt, 4): |
| 77031 | | - sqlite3_column_text(pStmt, 4) |
| 77032 | | - ); |
| 77033 | | - int n = sqlite3_column_bytes(pStmt, 4); |
| 77034 | | - if( n>0xffff ) n = 0xffff; |
| 77035 | | - pSample->nByte = (u16)n; |
| 77036 | | - if( n < 1){ |
| 77037 | | - pSample->u.z = 0; |
| 77038 | | - }else{ |
| 77039 | | - pSample->u.z = sqlite3Malloc(n); |
| 77040 | | - if( pSample->u.z==0 ){ |
| 77041 | | - db->mallocFailed = 1; |
| 77042 | | - sqlite3_finalize(pStmt); |
| 77043 | | - return SQLITE_NOMEM; |
| 77044 | | - } |
| 77045 | | - memcpy(pSample->u.z, z, n); |
| 77046 | | - } |
| 77047 | | - } |
| 77048 | | - } |
| 77049 | | - } |
| 77050 | | - return sqlite3_finalize(pStmt); |
| 77051 | | -} |
| 77052 | | -#endif /* SQLITE_ENABLE_STAT3 */ |
| 77053 | | - |
| 77054 | | -/* |
| 77055 | | -** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The |
| 77961 | +/* |
| 77962 | +** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The |
| 77056 | 77963 | ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] |
| 77057 | | -** arrays. The contents of sqlite_stat3 are used to populate the |
| 77964 | +** arrays. The contents of sqlite_stat2 are used to populate the |
| 77058 | 77965 | ** Index.aSample[] arrays. |
| 77059 | 77966 | ** |
| 77060 | 77967 | ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR |
| 77061 | | -** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined |
| 77062 | | -** during compilation and the sqlite_stat3 table is present, no data is |
| 77968 | +** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined |
| 77969 | +** during compilation and the sqlite_stat2 table is present, no data is |
| 77063 | 77970 | ** read from it. |
| 77064 | 77971 | ** |
| 77065 | | -** If SQLITE_ENABLE_STAT3 was defined during compilation and the |
| 77066 | | -** sqlite_stat3 table is not present in the database, SQLITE_ERROR is |
| 77972 | +** If SQLITE_ENABLE_STAT2 was defined during compilation and the |
| 77973 | +** sqlite_stat2 table is not present in the database, SQLITE_ERROR is |
| 77067 | 77974 | ** returned. However, in this case, data is read from the sqlite_stat1 |
| 77068 | 77975 | ** table (if it is present) before returning. |
| 77069 | 77976 | ** |
| 77070 | 77977 | ** If an OOM error occurs, this function always sets db->mallocFailed. |
| 77071 | 77978 | ** This means if the caller does not care about other errors, the return |
| | @@ -77083,14 +77990,12 @@ |
| 77083 | 77990 | /* Clear any prior statistics */ |
| 77084 | 77991 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 77085 | 77992 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
| 77086 | 77993 | Index *pIdx = sqliteHashData(i); |
| 77087 | 77994 | sqlite3DefaultRowEst(pIdx); |
| 77088 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 77089 | 77995 | sqlite3DeleteIndexSamples(db, pIdx); |
| 77090 | 77996 | pIdx->aSample = 0; |
| 77091 | | -#endif |
| 77092 | 77997 | } |
| 77093 | 77998 | |
| 77094 | 77999 | /* Check to make sure the sqlite_stat1 table exists */ |
| 77095 | 78000 | sInfo.db = db; |
| 77096 | 78001 | sInfo.zDatabase = db->aDb[iDb].zName; |
| | @@ -77098,23 +78003,91 @@ |
| 77098 | 78003 | return SQLITE_ERROR; |
| 77099 | 78004 | } |
| 77100 | 78005 | |
| 77101 | 78006 | /* Load new statistics out of the sqlite_stat1 table */ |
| 77102 | 78007 | zSql = sqlite3MPrintf(db, |
| 77103 | | - "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 78008 | + "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 77104 | 78009 | if( zSql==0 ){ |
| 77105 | 78010 | rc = SQLITE_NOMEM; |
| 77106 | 78011 | }else{ |
| 77107 | 78012 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
| 77108 | 78013 | sqlite3DbFree(db, zSql); |
| 77109 | 78014 | } |
| 77110 | 78015 | |
| 77111 | 78016 | |
| 77112 | | - /* Load the statistics from the sqlite_stat3 table. */ |
| 77113 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 78017 | + /* Load the statistics from the sqlite_stat2 table. */ |
| 78018 | +#ifdef SQLITE_ENABLE_STAT2 |
| 78019 | + if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){ |
| 78020 | + rc = SQLITE_ERROR; |
| 78021 | + } |
| 77114 | 78022 | if( rc==SQLITE_OK ){ |
| 77115 | | - rc = loadStat3(db, sInfo.zDatabase); |
| 78023 | + sqlite3_stmt *pStmt = 0; |
| 78024 | + |
| 78025 | + zSql = sqlite3MPrintf(db, |
| 78026 | + "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase); |
| 78027 | + if( !zSql ){ |
| 78028 | + rc = SQLITE_NOMEM; |
| 78029 | + }else{ |
| 78030 | + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 78031 | + sqlite3DbFree(db, zSql); |
| 78032 | + } |
| 78033 | + |
| 78034 | + if( rc==SQLITE_OK ){ |
| 78035 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 78036 | + char *zIndex; /* Index name */ |
| 78037 | + Index *pIdx; /* Pointer to the index object */ |
| 78038 | + |
| 78039 | + zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 78040 | + pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0; |
| 78041 | + if( pIdx ){ |
| 78042 | + int iSample = sqlite3_column_int(pStmt, 1); |
| 78043 | + if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){ |
| 78044 | + int eType = sqlite3_column_type(pStmt, 2); |
| 78045 | + |
| 78046 | + if( pIdx->aSample==0 ){ |
| 78047 | + static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES; |
| 78048 | + pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz); |
| 78049 | + if( pIdx->aSample==0 ){ |
| 78050 | + db->mallocFailed = 1; |
| 78051 | + break; |
| 78052 | + } |
| 78053 | + memset(pIdx->aSample, 0, sz); |
| 78054 | + } |
| 78055 | + |
| 78056 | + assert( pIdx->aSample ); |
| 78057 | + { |
| 78058 | + IndexSample *pSample = &pIdx->aSample[iSample]; |
| 78059 | + pSample->eType = (u8)eType; |
| 78060 | + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ |
| 78061 | + pSample->u.r = sqlite3_column_double(pStmt, 2); |
| 78062 | + }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ |
| 78063 | + const char *z = (const char *)( |
| 78064 | + (eType==SQLITE_BLOB) ? |
| 78065 | + sqlite3_column_blob(pStmt, 2): |
| 78066 | + sqlite3_column_text(pStmt, 2) |
| 78067 | + ); |
| 78068 | + int n = sqlite3_column_bytes(pStmt, 2); |
| 78069 | + if( n>24 ){ |
| 78070 | + n = 24; |
| 78071 | + } |
| 78072 | + pSample->nByte = (u8)n; |
| 78073 | + if( n < 1){ |
| 78074 | + pSample->u.z = 0; |
| 78075 | + }else{ |
| 78076 | + pSample->u.z = sqlite3DbStrNDup(0, z, n); |
| 78077 | + if( pSample->u.z==0 ){ |
| 78078 | + db->mallocFailed = 1; |
| 78079 | + break; |
| 78080 | + } |
| 78081 | + } |
| 78082 | + } |
| 78083 | + } |
| 78084 | + } |
| 78085 | + } |
| 78086 | + } |
| 78087 | + rc = sqlite3_finalize(pStmt); |
| 78088 | + } |
| 77116 | 78089 | } |
| 77117 | 78090 | #endif |
| 77118 | 78091 | |
| 77119 | 78092 | if( rc==SQLITE_NOMEM ){ |
| 77120 | 78093 | db->mallocFailed = 1; |
| | @@ -79610,11 +80583,11 @@ |
| 79610 | 80583 | Table *p; |
| 79611 | 80584 | int n; |
| 79612 | 80585 | const char *z; |
| 79613 | 80586 | Token sEnd; |
| 79614 | 80587 | DbFixer sFix; |
| 79615 | | - Token *pName; |
| 80588 | + Token *pName = 0; |
| 79616 | 80589 | int iDb; |
| 79617 | 80590 | sqlite3 *db = pParse->db; |
| 79618 | 80591 | |
| 79619 | 80592 | if( pParse->nVar>0 ){ |
| 79620 | 80593 | sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
| | @@ -79926,15 +80899,11 @@ |
| 79926 | 80899 | Parse *pParse, /* The parsing context */ |
| 79927 | 80900 | int iDb, /* The database number */ |
| 79928 | 80901 | const char *zType, /* "idx" or "tbl" */ |
| 79929 | 80902 | const char *zName /* Name of index or table */ |
| 79930 | 80903 | ){ |
| 79931 | | - static const char *azStatTab[] = { |
| 79932 | | - "sqlite_stat1", |
| 79933 | | - "sqlite_stat2", |
| 79934 | | - "sqlite_stat3", |
| 79935 | | - }; |
| 80904 | + static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" }; |
| 79936 | 80905 | int i; |
| 79937 | 80906 | const char *zDbName = pParse->db->aDb[iDb].zName; |
| 79938 | 80907 | for(i=0; i<ArraySize(azStatTab); i++){ |
| 79939 | 80908 | if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){ |
| 79940 | 80909 | sqlite3NestedParse(pParse, |
| | @@ -79941,81 +80910,10 @@ |
| 79941 | 80910 | "DELETE FROM %Q.%s WHERE %s=%Q", |
| 79942 | 80911 | zDbName, azStatTab[i], zType, zName |
| 79943 | 80912 | ); |
| 79944 | 80913 | } |
| 79945 | 80914 | } |
| 79946 | | -} |
| 79947 | | - |
| 79948 | | -/* |
| 79949 | | -** Generate code to drop a table. |
| 79950 | | -*/ |
| 79951 | | -SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ |
| 79952 | | - Vdbe *v; |
| 79953 | | - sqlite3 *db = pParse->db; |
| 79954 | | - Trigger *pTrigger; |
| 79955 | | - Db *pDb = &db->aDb[iDb]; |
| 79956 | | - |
| 79957 | | - v = sqlite3GetVdbe(pParse); |
| 79958 | | - assert( v!=0 ); |
| 79959 | | - sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 79960 | | - |
| 79961 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 79962 | | - if( IsVirtual(pTab) ){ |
| 79963 | | - sqlite3VdbeAddOp0(v, OP_VBegin); |
| 79964 | | - } |
| 79965 | | -#endif |
| 79966 | | - |
| 79967 | | - /* Drop all triggers associated with the table being dropped. Code |
| 79968 | | - ** is generated to remove entries from sqlite_master and/or |
| 79969 | | - ** sqlite_temp_master if required. |
| 79970 | | - */ |
| 79971 | | - pTrigger = sqlite3TriggerList(pParse, pTab); |
| 79972 | | - while( pTrigger ){ |
| 79973 | | - assert( pTrigger->pSchema==pTab->pSchema || |
| 79974 | | - pTrigger->pSchema==db->aDb[1].pSchema ); |
| 79975 | | - sqlite3DropTriggerPtr(pParse, pTrigger); |
| 79976 | | - pTrigger = pTrigger->pNext; |
| 79977 | | - } |
| 79978 | | - |
| 79979 | | -#ifndef SQLITE_OMIT_AUTOINCREMENT |
| 79980 | | - /* Remove any entries of the sqlite_sequence table associated with |
| 79981 | | - ** the table being dropped. This is done before the table is dropped |
| 79982 | | - ** at the btree level, in case the sqlite_sequence table needs to |
| 79983 | | - ** move as a result of the drop (can happen in auto-vacuum mode). |
| 79984 | | - */ |
| 79985 | | - if( pTab->tabFlags & TF_Autoincrement ){ |
| 79986 | | - sqlite3NestedParse(pParse, |
| 79987 | | - "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
| 79988 | | - pDb->zName, pTab->zName |
| 79989 | | - ); |
| 79990 | | - } |
| 79991 | | -#endif |
| 79992 | | - |
| 79993 | | - /* Drop all SQLITE_MASTER table and index entries that refer to the |
| 79994 | | - ** table. The program name loops through the master table and deletes |
| 79995 | | - ** every row that refers to a table of the same name as the one being |
| 79996 | | - ** dropped. Triggers are handled seperately because a trigger can be |
| 79997 | | - ** created in the temp database that refers to a table in another |
| 79998 | | - ** database. |
| 79999 | | - */ |
| 80000 | | - sqlite3NestedParse(pParse, |
| 80001 | | - "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
| 80002 | | - pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); |
| 80003 | | - if( !isView && !IsVirtual(pTab) ){ |
| 80004 | | - destroyTable(pParse, pTab); |
| 80005 | | - } |
| 80006 | | - |
| 80007 | | - /* Remove the table entry from SQLite's internal schema and modify |
| 80008 | | - ** the schema cookie. |
| 80009 | | - */ |
| 80010 | | - if( IsVirtual(pTab) ){ |
| 80011 | | - sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
| 80012 | | - } |
| 80013 | | - sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 80014 | | - sqlite3ChangeCookie(pParse, iDb); |
| 80015 | | - sqliteViewResetAll(db, iDb); |
| 80016 | | - |
| 80017 | 80915 | } |
| 80018 | 80916 | |
| 80019 | 80917 | /* |
| 80020 | 80918 | ** This routine is called to do the work of a DROP TABLE statement. |
| 80021 | 80919 | ** pName is the name of the table to be dropped. |
| | @@ -80082,11 +80980,11 @@ |
| 80082 | 80980 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 80083 | 80981 | goto exit_drop_table; |
| 80084 | 80982 | } |
| 80085 | 80983 | } |
| 80086 | 80984 | #endif |
| 80087 | | - if( !pParse->nested && sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 80985 | + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 80088 | 80986 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
| 80089 | 80987 | goto exit_drop_table; |
| 80090 | 80988 | } |
| 80091 | 80989 | |
| 80092 | 80990 | #ifndef SQLITE_OMIT_VIEW |
| | @@ -80106,15 +81004,72 @@ |
| 80106 | 81004 | /* Generate code to remove the table from the master table |
| 80107 | 81005 | ** on disk. |
| 80108 | 81006 | */ |
| 80109 | 81007 | v = sqlite3GetVdbe(pParse); |
| 80110 | 81008 | if( v ){ |
| 81009 | + Trigger *pTrigger; |
| 81010 | + Db *pDb = &db->aDb[iDb]; |
| 80111 | 81011 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 81012 | + |
| 81013 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 81014 | + if( IsVirtual(pTab) ){ |
| 81015 | + sqlite3VdbeAddOp0(v, OP_VBegin); |
| 81016 | + } |
| 81017 | +#endif |
| 80112 | 81018 | sqlite3FkDropTable(pParse, pName, pTab); |
| 80113 | | - sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
| 81019 | + |
| 81020 | + /* Drop all triggers associated with the table being dropped. Code |
| 81021 | + ** is generated to remove entries from sqlite_master and/or |
| 81022 | + ** sqlite_temp_master if required. |
| 81023 | + */ |
| 81024 | + pTrigger = sqlite3TriggerList(pParse, pTab); |
| 81025 | + while( pTrigger ){ |
| 81026 | + assert( pTrigger->pSchema==pTab->pSchema || |
| 81027 | + pTrigger->pSchema==db->aDb[1].pSchema ); |
| 81028 | + sqlite3DropTriggerPtr(pParse, pTrigger); |
| 81029 | + pTrigger = pTrigger->pNext; |
| 81030 | + } |
| 81031 | + |
| 81032 | +#ifndef SQLITE_OMIT_AUTOINCREMENT |
| 81033 | + /* Remove any entries of the sqlite_sequence table associated with |
| 81034 | + ** the table being dropped. This is done before the table is dropped |
| 81035 | + ** at the btree level, in case the sqlite_sequence table needs to |
| 81036 | + ** move as a result of the drop (can happen in auto-vacuum mode). |
| 81037 | + */ |
| 81038 | + if( pTab->tabFlags & TF_Autoincrement ){ |
| 81039 | + sqlite3NestedParse(pParse, |
| 81040 | + "DELETE FROM %s.sqlite_sequence WHERE name=%Q", |
| 81041 | + pDb->zName, pTab->zName |
| 81042 | + ); |
| 81043 | + } |
| 81044 | +#endif |
| 81045 | + |
| 81046 | + /* Drop all SQLITE_MASTER table and index entries that refer to the |
| 81047 | + ** table. The program name loops through the master table and deletes |
| 81048 | + ** every row that refers to a table of the same name as the one being |
| 81049 | + ** dropped. Triggers are handled seperately because a trigger can be |
| 81050 | + ** created in the temp database that refers to a table in another |
| 81051 | + ** database. |
| 81052 | + */ |
| 81053 | + sqlite3NestedParse(pParse, |
| 81054 | + "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
| 81055 | + pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); |
| 80114 | 81056 | sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 81057 | + if( !isView && !IsVirtual(pTab) ){ |
| 81058 | + destroyTable(pParse, pTab); |
| 81059 | + } |
| 81060 | + |
| 81061 | + /* Remove the table entry from SQLite's internal schema and modify |
| 81062 | + ** the schema cookie. |
| 81063 | + */ |
| 81064 | + if( IsVirtual(pTab) ){ |
| 81065 | + sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
| 81066 | + } |
| 81067 | + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 81068 | + sqlite3ChangeCookie(pParse, iDb); |
| 80115 | 81069 | } |
| 81070 | + sqliteViewResetAll(db, iDb); |
| 80116 | 81071 | |
| 80117 | 81072 | exit_drop_table: |
| 80118 | 81073 | sqlite3SrcListDelete(db, pName); |
| 80119 | 81074 | } |
| 80120 | 81075 | |
| | @@ -80278,18 +81233,28 @@ |
| 80278 | 81233 | */ |
| 80279 | 81234 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 80280 | 81235 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
| 80281 | 81236 | int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
| 80282 | 81237 | int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
| 81238 | + int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */ |
| 80283 | 81239 | int addr1; /* Address of top of loop */ |
| 80284 | 81240 | int tnum; /* Root page of index */ |
| 80285 | 81241 | Vdbe *v; /* Generate code into this virtual machine */ |
| 80286 | 81242 | KeyInfo *pKey; /* KeyInfo for index */ |
| 80287 | 81243 | int regIdxKey; /* Registers containing the index key */ |
| 80288 | 81244 | int regRecord; /* Register holding assemblied index record */ |
| 80289 | 81245 | sqlite3 *db = pParse->db; /* The database connection */ |
| 80290 | 81246 | int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
| 81247 | + |
| 81248 | + /* Set bUseSorter to use OP_OpenSorter, or clear it to insert directly |
| 81249 | + ** into the index. The sorter is used unless either OMIT_MERGE_SORT is |
| 81250 | + ** defined or the system is configured to store temp files in-memory. */ |
| 81251 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 81252 | + static const int bUseSorter = 0; |
| 81253 | +#else |
| 81254 | + const int bUseSorter = !sqlite3TempInMemory(pParse->db); |
| 81255 | +#endif |
| 80291 | 81256 | |
| 80292 | 81257 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 80293 | 81258 | if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
| 80294 | 81259 | db->aDb[iDb].zName ) ){ |
| 80295 | 81260 | return; |
| | @@ -80311,14 +81276,33 @@ |
| 80311 | 81276 | sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, |
| 80312 | 81277 | (char *)pKey, P4_KEYINFO_HANDOFF); |
| 80313 | 81278 | if( memRootPage>=0 ){ |
| 80314 | 81279 | sqlite3VdbeChangeP5(v, 1); |
| 80315 | 81280 | } |
| 81281 | + |
| 81282 | + /* Open the sorter cursor if we are to use one. */ |
| 81283 | + if( bUseSorter ){ |
| 81284 | + iSorter = pParse->nTab++; |
| 81285 | + sqlite3VdbeAddOp4(v, OP_OpenSorter, iSorter, 0, 0, (char*)pKey, P4_KEYINFO); |
| 81286 | + sqlite3VdbeChangeP5(v, BTREE_SORTER); |
| 81287 | + } |
| 81288 | + |
| 81289 | + /* Open the table. Loop through all rows of the table, inserting index |
| 81290 | + ** records into the sorter. */ |
| 80316 | 81291 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 80317 | 81292 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); |
| 80318 | 81293 | regRecord = sqlite3GetTempReg(pParse); |
| 80319 | 81294 | regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); |
| 81295 | + |
| 81296 | + if( bUseSorter ){ |
| 81297 | + sqlite3VdbeAddOp2(v, OP_IdxInsert, iSorter, regRecord); |
| 81298 | + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); |
| 81299 | + sqlite3VdbeJumpHere(v, addr1); |
| 81300 | + addr1 = sqlite3VdbeAddOp2(v, OP_Sort, iSorter, 0); |
| 81301 | + sqlite3VdbeAddOp2(v, OP_RowKey, iSorter, regRecord); |
| 81302 | + } |
| 81303 | + |
| 80320 | 81304 | if( pIndex->onError!=OE_None ){ |
| 80321 | 81305 | const int regRowid = regIdxKey + pIndex->nColumn; |
| 80322 | 81306 | const int j2 = sqlite3VdbeCurrentAddr(v) + 2; |
| 80323 | 81307 | void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey); |
| 80324 | 81308 | |
| | @@ -80333,17 +81317,19 @@ |
| 80333 | 81317 | */ |
| 80334 | 81318 | sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32); |
| 80335 | 81319 | sqlite3HaltConstraint( |
| 80336 | 81320 | pParse, OE_Abort, "indexed columns are not unique", P4_STATIC); |
| 80337 | 81321 | } |
| 80338 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
| 81322 | + sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, bUseSorter); |
| 80339 | 81323 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 80340 | 81324 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 80341 | | - sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); |
| 81325 | + sqlite3VdbeAddOp2(v, OP_Next, iSorter, addr1+1); |
| 80342 | 81326 | sqlite3VdbeJumpHere(v, addr1); |
| 81327 | + |
| 80343 | 81328 | sqlite3VdbeAddOp1(v, OP_Close, iTab); |
| 80344 | 81329 | sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
| 81330 | + sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
| 80345 | 81331 | } |
| 80346 | 81332 | |
| 80347 | 81333 | /* |
| 80348 | 81334 | ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 80349 | 81335 | ** and pTblList is the name of the table that is to be indexed. Both will |
| | @@ -80557,24 +81543,24 @@ |
| 80557 | 81543 | */ |
| 80558 | 81544 | nName = sqlite3Strlen30(zName); |
| 80559 | 81545 | nCol = pList->nExpr; |
| 80560 | 81546 | pIndex = sqlite3DbMallocZero(db, |
| 80561 | 81547 | sizeof(Index) + /* Index structure */ |
| 80562 | | - sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */ |
| 80563 | 81548 | sizeof(int)*nCol + /* Index.aiColumn */ |
| 81549 | + sizeof(int)*(nCol+1) + /* Index.aiRowEst */ |
| 80564 | 81550 | sizeof(char *)*nCol + /* Index.azColl */ |
| 80565 | 81551 | sizeof(u8)*nCol + /* Index.aSortOrder */ |
| 80566 | 81552 | nName + 1 + /* Index.zName */ |
| 80567 | 81553 | nExtra /* Collation sequence names */ |
| 80568 | 81554 | ); |
| 80569 | 81555 | if( db->mallocFailed ){ |
| 80570 | 81556 | goto exit_create_index; |
| 80571 | 81557 | } |
| 80572 | | - pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]); |
| 80573 | | - pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]); |
| 81558 | + pIndex->azColl = (char**)(&pIndex[1]); |
| 80574 | 81559 | pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]); |
| 80575 | | - pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]); |
| 81560 | + pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]); |
| 81561 | + pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]); |
| 80576 | 81562 | pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]); |
| 80577 | 81563 | zExtra = (char *)(&pIndex->zName[nName+1]); |
| 80578 | 81564 | memcpy(pIndex->zName, zName, nName+1); |
| 80579 | 81565 | pIndex->pTable = pTab; |
| 80580 | 81566 | pIndex->nColumn = pList->nExpr; |
| | @@ -80847,13 +81833,13 @@ |
| 80847 | 81833 | ** Apart from that, we have little to go on besides intuition as to |
| 80848 | 81834 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 80849 | 81835 | ** are based on typical values found in actual indices. |
| 80850 | 81836 | */ |
| 80851 | 81837 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ |
| 80852 | | - tRowcnt *a = pIdx->aiRowEst; |
| 81838 | + unsigned *a = pIdx->aiRowEst; |
| 80853 | 81839 | int i; |
| 80854 | | - tRowcnt n; |
| 81840 | + unsigned n; |
| 80855 | 81841 | assert( a!=0 ); |
| 80856 | 81842 | a[0] = pIdx->pTable->nRowEst; |
| 80857 | 81843 | if( a[0]<10 ) a[0] = 10; |
| 80858 | 81844 | n = 10; |
| 80859 | 81845 | for(i=1; i<=pIdx->nColumn; i++){ |
| | @@ -81293,12 +82279,13 @@ |
| 81293 | 82279 | ** The operator is "natural cross join". The A and B operands are stored |
| 81294 | 82280 | ** in p->a[0] and p->a[1], respectively. The parser initially stores the |
| 81295 | 82281 | ** operator with A. This routine shifts that operator over to B. |
| 81296 | 82282 | */ |
| 81297 | 82283 | SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){ |
| 81298 | | - if( p && p->a ){ |
| 82284 | + if( p ){ |
| 81299 | 82285 | int i; |
| 82286 | + assert( p->a || p->nSrc==0 ); |
| 81300 | 82287 | for(i=p->nSrc-1; i>0; i--){ |
| 81301 | 82288 | p->a[i].jointype = p->a[i-1].jointype; |
| 81302 | 82289 | } |
| 81303 | 82290 | p->a[0].jointype = 0; |
| 81304 | 82291 | } |
| | @@ -82850,10 +83837,12 @@ |
| 82850 | 83837 | ** |
| 82851 | 83838 | ** There is only one exported symbol in this file - the function |
| 82852 | 83839 | ** sqliteRegisterBuildinFunctions() found at the bottom of the file. |
| 82853 | 83840 | ** All other code has file scope. |
| 82854 | 83841 | */ |
| 83842 | +/* #include <stdlib.h> */ |
| 83843 | +/* #include <assert.h> */ |
| 82855 | 83844 | |
| 82856 | 83845 | /* |
| 82857 | 83846 | ** Return the collating function associated with a function. |
| 82858 | 83847 | */ |
| 82859 | 83848 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| | @@ -85173,11 +86162,28 @@ |
| 85173 | 86162 | pTo = sqlite3FindTable(db, pFKey->zTo, zDb); |
| 85174 | 86163 | }else{ |
| 85175 | 86164 | pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb); |
| 85176 | 86165 | } |
| 85177 | 86166 | if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){ |
| 86167 | + assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) ); |
| 85178 | 86168 | if( !isIgnoreErrors || db->mallocFailed ) return; |
| 86169 | + if( pTo==0 ){ |
| 86170 | + /* If isIgnoreErrors is true, then a table is being dropped. In this |
| 86171 | + ** case SQLite runs a "DELETE FROM xxx" on the table being dropped |
| 86172 | + ** before actually dropping it in order to check FK constraints. |
| 86173 | + ** If the parent table of an FK constraint on the current table is |
| 86174 | + ** missing, behave as if it is empty. i.e. decrement the relevant |
| 86175 | + ** FK counter for each row of the current table with non-NULL keys. |
| 86176 | + */ |
| 86177 | + Vdbe *v = sqlite3GetVdbe(pParse); |
| 86178 | + int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1; |
| 86179 | + for(i=0; i<pFKey->nCol; i++){ |
| 86180 | + int iReg = pFKey->aCol[i].iFrom + regOld + 1; |
| 86181 | + sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); |
| 86182 | + } |
| 86183 | + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1); |
| 86184 | + } |
| 85179 | 86185 | continue; |
| 85180 | 86186 | } |
| 85181 | 86187 | assert( pFKey->nCol==1 || (aiFree && pIdx) ); |
| 85182 | 86188 | |
| 85183 | 86189 | if( aiFree ){ |
| | @@ -88081,10 +89087,11 @@ |
| 88081 | 89087 | |
| 88082 | 89088 | #endif /* _SQLITE3EXT_H_ */ |
| 88083 | 89089 | |
| 88084 | 89090 | /************** End of sqlite3ext.h ******************************************/ |
| 88085 | 89091 | /************** Continuing where we left off in loadext.c ********************/ |
| 89092 | +/* #include <string.h> */ |
| 88086 | 89093 | |
| 88087 | 89094 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 88088 | 89095 | |
| 88089 | 89096 | /* |
| 88090 | 89097 | ** Some API routines are omitted when various features are |
| | @@ -95650,10 +96657,12 @@ |
| 95650 | 96657 | ** interface routine of sqlite3_exec(). |
| 95651 | 96658 | ** |
| 95652 | 96659 | ** These routines are in a separate files so that they will not be linked |
| 95653 | 96660 | ** if they are not used. |
| 95654 | 96661 | */ |
| 96662 | +/* #include <stdlib.h> */ |
| 96663 | +/* #include <string.h> */ |
| 95655 | 96664 | |
| 95656 | 96665 | #ifndef SQLITE_OMIT_GET_TABLE |
| 95657 | 96666 | |
| 95658 | 96667 | /* |
| 95659 | 96668 | ** This structure is used to pass data from sqlite3_get_table() through |
| | @@ -99159,11 +100168,11 @@ |
| 99159 | 100168 | #define TERM_CODED 0x04 /* This term is already coded */ |
| 99160 | 100169 | #define TERM_COPIED 0x08 /* Has a child */ |
| 99161 | 100170 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 99162 | 100171 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 99163 | 100172 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
| 99164 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 100173 | +#ifdef SQLITE_ENABLE_STAT2 |
| 99165 | 100174 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 99166 | 100175 | #else |
| 99167 | 100176 | # define TERM_VNULL 0x00 /* Disabled if not using stat2 */ |
| 99168 | 100177 | #endif |
| 99169 | 100178 | |
| | @@ -100373,11 +101382,11 @@ |
| 100373 | 101382 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 100374 | 101383 | } |
| 100375 | 101384 | } |
| 100376 | 101385 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 100377 | 101386 | |
| 100378 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 101387 | +#ifdef SQLITE_ENABLE_STAT2 |
| 100379 | 101388 | /* When sqlite_stat2 histogram data is available an operator of the |
| 100380 | 101389 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 100381 | 101390 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 100382 | 101391 | ** virtual term of that form. |
| 100383 | 101392 | ** |
| | @@ -100412,11 +101421,11 @@ |
| 100412 | 101421 | pTerm->nChild = 1; |
| 100413 | 101422 | pTerm->wtFlags |= TERM_COPIED; |
| 100414 | 101423 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 100415 | 101424 | } |
| 100416 | 101425 | } |
| 100417 | | -#endif /* SQLITE_ENABLE_STAT */ |
| 101426 | +#endif /* SQLITE_ENABLE_STAT2 */ |
| 100418 | 101427 | |
| 100419 | 101428 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 100420 | 101429 | ** an index for tables to the left of the join. |
| 100421 | 101430 | */ |
| 100422 | 101431 | pTerm->prereqRight |= extraRight; |
| | @@ -101461,89 +102470,71 @@ |
| 101461 | 102470 | */ |
| 101462 | 102471 | bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost); |
| 101463 | 102472 | } |
| 101464 | 102473 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 101465 | 102474 | |
| 101466 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 101467 | 102475 | /* |
| 101468 | | -** Estimate the location of a particular key among all keys in an |
| 101469 | | -** index. Store the results in aStat as follows: |
| 101470 | | -** |
| 101471 | | -** aStat[0] Est. number of rows less than pVal |
| 101472 | | -** aStat[1] Est. number of rows equal to pVal |
| 101473 | | -** |
| 101474 | | -** Return SQLITE_OK on success. |
| 101475 | | -*/ |
| 101476 | | -static int whereKeyStats( |
| 102476 | +** Argument pIdx is a pointer to an index structure that has an array of |
| 102477 | +** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column |
| 102478 | +** stored in Index.aSample. These samples divide the domain of values stored |
| 102479 | +** the index into (SQLITE_INDEX_SAMPLES+1) regions. |
| 102480 | +** Region 0 contains all values less than the first sample value. Region |
| 102481 | +** 1 contains values between the first and second samples. Region 2 contains |
| 102482 | +** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES |
| 102483 | +** contains values larger than the last sample. |
| 102484 | +** |
| 102485 | +** If the index contains many duplicates of a single value, then it is |
| 102486 | +** possible that two or more adjacent samples can hold the same value. |
| 102487 | +** When that is the case, the smallest possible region code is returned |
| 102488 | +** when roundUp is false and the largest possible region code is returned |
| 102489 | +** when roundUp is true. |
| 102490 | +** |
| 102491 | +** If successful, this function determines which of the regions value |
| 102492 | +** pVal lies in, sets *piRegion to the region index (a value between 0 |
| 102493 | +** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK. |
| 102494 | +** Or, if an OOM occurs while converting text values between encodings, |
| 102495 | +** SQLITE_NOMEM is returned and *piRegion is undefined. |
| 102496 | +*/ |
| 102497 | +#ifdef SQLITE_ENABLE_STAT2 |
| 102498 | +static int whereRangeRegion( |
| 101477 | 102499 | Parse *pParse, /* Database connection */ |
| 101478 | 102500 | Index *pIdx, /* Index to consider domain of */ |
| 101479 | 102501 | sqlite3_value *pVal, /* Value to consider */ |
| 101480 | | - int roundUp, /* Round up if true. Round down if false */ |
| 101481 | | - tRowcnt *aStat /* OUT: stats written here */ |
| 102502 | + int roundUp, /* Return largest valid region if true */ |
| 102503 | + int *piRegion /* OUT: Region of domain in which value lies */ |
| 101482 | 102504 | ){ |
| 101483 | | - tRowcnt n; |
| 101484 | | - IndexSample *aSample; |
| 101485 | | - int i, eType; |
| 101486 | | - int isEq = 0; |
| 101487 | | - i64 v; |
| 101488 | | - double r, rS; |
| 101489 | | - |
| 101490 | 102505 | assert( roundUp==0 || roundUp==1 ); |
| 101491 | | - if( pVal==0 ) return SQLITE_ERROR; |
| 101492 | | - n = pIdx->aiRowEst[0]; |
| 101493 | | - aSample = pIdx->aSample; |
| 101494 | | - i = 0; |
| 101495 | | - eType = sqlite3_value_type(pVal); |
| 101496 | | - |
| 101497 | | - if( eType==SQLITE_INTEGER ){ |
| 101498 | | - v = sqlite3_value_int64(pVal); |
| 101499 | | - r = (i64)v; |
| 101500 | | - for(i=0; i<pIdx->nSample; i++){ |
| 101501 | | - if( aSample[i].eType==SQLITE_NULL ) continue; |
| 101502 | | - if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 101503 | | - if( aSample[i].eType==SQLITE_INTEGER ){ |
| 101504 | | - if( aSample[i].u.i>=v ){ |
| 101505 | | - isEq = aSample[i].u.i==v; |
| 101506 | | - break; |
| 101507 | | - } |
| 101508 | | - }else{ |
| 101509 | | - assert( aSample[i].eType==SQLITE_FLOAT ); |
| 101510 | | - if( aSample[i].u.r>=r ){ |
| 101511 | | - isEq = aSample[i].u.r==r; |
| 101512 | | - break; |
| 101513 | | - } |
| 101514 | | - } |
| 101515 | | - } |
| 101516 | | - }else if( eType==SQLITE_FLOAT ){ |
| 101517 | | - r = sqlite3_value_double(pVal); |
| 101518 | | - for(i=0; i<pIdx->nSample; i++){ |
| 101519 | | - if( aSample[i].eType==SQLITE_NULL ) continue; |
| 101520 | | - if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 101521 | | - if( aSample[i].eType==SQLITE_FLOAT ){ |
| 101522 | | - rS = aSample[i].u.r; |
| 101523 | | - }else{ |
| 101524 | | - rS = aSample[i].u.i; |
| 101525 | | - } |
| 101526 | | - if( rS>=r ){ |
| 101527 | | - isEq = rS==r; |
| 101528 | | - break; |
| 101529 | | - } |
| 101530 | | - } |
| 101531 | | - }else if( eType==SQLITE_NULL ){ |
| 101532 | | - i = 0; |
| 101533 | | - if( pIdx->nSample>=1 && aSample[0].eType==SQLITE_NULL ) isEq = 1; |
| 101534 | | - }else{ |
| 101535 | | - assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 101536 | | - for(i=0; i<pIdx->nSample; i++){ |
| 101537 | | - if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){ |
| 101538 | | - break; |
| 101539 | | - } |
| 101540 | | - } |
| 101541 | | - if( i<pIdx->nSample ){ |
| 102506 | + if( ALWAYS(pVal) ){ |
| 102507 | + IndexSample *aSample = pIdx->aSample; |
| 102508 | + int i = 0; |
| 102509 | + int eType = sqlite3_value_type(pVal); |
| 102510 | + |
| 102511 | + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ |
| 102512 | + double r = sqlite3_value_double(pVal); |
| 102513 | + for(i=0; i<SQLITE_INDEX_SAMPLES; i++){ |
| 102514 | + if( aSample[i].eType==SQLITE_NULL ) continue; |
| 102515 | + if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 102516 | + if( roundUp ){ |
| 102517 | + if( aSample[i].u.r>r ) break; |
| 102518 | + }else{ |
| 102519 | + if( aSample[i].u.r>=r ) break; |
| 102520 | + } |
| 102521 | + } |
| 102522 | + }else if( eType==SQLITE_NULL ){ |
| 102523 | + i = 0; |
| 102524 | + if( roundUp ){ |
| 102525 | + while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++; |
| 102526 | + } |
| 102527 | + }else{ |
| 101542 | 102528 | sqlite3 *db = pParse->db; |
| 101543 | 102529 | CollSeq *pColl; |
| 101544 | 102530 | const u8 *z; |
| 102531 | + int n; |
| 102532 | + |
| 102533 | + /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */ |
| 102534 | + assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 102535 | + |
| 101545 | 102536 | if( eType==SQLITE_BLOB ){ |
| 101546 | 102537 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 101547 | 102538 | pColl = db->pDfltColl; |
| 101548 | 102539 | assert( pColl->enc==SQLITE_UTF8 ); |
| 101549 | 102540 | }else{ |
| | @@ -101558,16 +102549,16 @@ |
| 101558 | 102549 | return SQLITE_NOMEM; |
| 101559 | 102550 | } |
| 101560 | 102551 | assert( z && pColl && pColl->xCmp ); |
| 101561 | 102552 | } |
| 101562 | 102553 | n = sqlite3ValueBytes(pVal, pColl->enc); |
| 101563 | | - |
| 101564 | | - for(; i<pIdx->nSample; i++){ |
| 102554 | + |
| 102555 | + for(i=0; i<SQLITE_INDEX_SAMPLES; i++){ |
| 101565 | 102556 | int c; |
| 101566 | 102557 | int eSampletype = aSample[i].eType; |
| 101567 | | - if( eSampletype<eType ) continue; |
| 101568 | | - if( eSampletype!=eType ) break; |
| 102558 | + if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue; |
| 102559 | + if( (eSampletype!=eType) ) break; |
| 101569 | 102560 | #ifndef SQLITE_OMIT_UTF16 |
| 101570 | 102561 | if( pColl->enc!=SQLITE_UTF8 ){ |
| 101571 | 102562 | int nSample; |
| 101572 | 102563 | char *zSample = sqlite3Utf8to16( |
| 101573 | 102564 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| | @@ -101581,52 +102572,20 @@ |
| 101581 | 102572 | }else |
| 101582 | 102573 | #endif |
| 101583 | 102574 | { |
| 101584 | 102575 | c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z); |
| 101585 | 102576 | } |
| 101586 | | - if( c>=0 ){ |
| 101587 | | - if( c==0 ) isEq = 1; |
| 101588 | | - break; |
| 101589 | | - } |
| 101590 | | - } |
| 101591 | | - } |
| 101592 | | - } |
| 101593 | | - |
| 101594 | | - /* At this point, aSample[i] is the first sample that is greater than |
| 101595 | | - ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 101596 | | - ** than pVal. If aSample[i]==pVal, then isEq==1. |
| 101597 | | - */ |
| 101598 | | - if( isEq ){ |
| 101599 | | - assert( i<pIdx->nSample ); |
| 101600 | | - aStat[0] = aSample[i].nLt; |
| 101601 | | - aStat[1] = aSample[i].nEq; |
| 101602 | | - }else{ |
| 101603 | | - tRowcnt iLower, iUpper, iGap; |
| 101604 | | - if( i==0 ){ |
| 101605 | | - iLower = 0; |
| 101606 | | - iUpper = aSample[0].nLt; |
| 101607 | | - }else{ |
| 101608 | | - iUpper = i>=pIdx->nSample ? n : aSample[i].nLt; |
| 101609 | | - iLower = aSample[i-1].nEq + aSample[i-1].nLt; |
| 101610 | | - } |
| 101611 | | - aStat[1] = pIdx->avgEq; |
| 101612 | | - if( iLower>=iUpper ){ |
| 101613 | | - iGap = 0; |
| 101614 | | - }else{ |
| 101615 | | - iGap = iUpper - iLower; |
| 101616 | | - if( iGap>=aStat[1]/2 ) iGap -= aStat[1]/2; |
| 101617 | | - } |
| 101618 | | - if( roundUp ){ |
| 101619 | | - iGap = (iGap*2)/3; |
| 101620 | | - }else{ |
| 101621 | | - iGap = iGap/3; |
| 101622 | | - } |
| 101623 | | - aStat[0] = iLower + iGap; |
| 102577 | + if( c-roundUp>=0 ) break; |
| 102578 | + } |
| 102579 | + } |
| 102580 | + |
| 102581 | + assert( i>=0 && i<=SQLITE_INDEX_SAMPLES ); |
| 102582 | + *piRegion = i; |
| 101624 | 102583 | } |
| 101625 | 102584 | return SQLITE_OK; |
| 101626 | 102585 | } |
| 101627 | | -#endif /* SQLITE_ENABLE_STAT3 */ |
| 102586 | +#endif /* #ifdef SQLITE_ENABLE_STAT2 */ |
| 101628 | 102587 | |
| 101629 | 102588 | /* |
| 101630 | 102589 | ** If expression pExpr represents a literal value, set *pp to point to |
| 101631 | 102590 | ** an sqlite3_value structure containing the same value, with affinity |
| 101632 | 102591 | ** aff applied to it, before returning. It is the responsibility of the |
| | @@ -101640,11 +102599,11 @@ |
| 101640 | 102599 | ** |
| 101641 | 102600 | ** If neither of the above apply, set *pp to NULL. |
| 101642 | 102601 | ** |
| 101643 | 102602 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 101644 | 102603 | */ |
| 101645 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 102604 | +#ifdef SQLITE_ENABLE_STAT2 |
| 101646 | 102605 | static int valueFromExpr( |
| 101647 | 102606 | Parse *pParse, |
| 101648 | 102607 | Expr *pExpr, |
| 101649 | 102608 | u8 aff, |
| 101650 | 102609 | sqlite3_value **pp |
| | @@ -101688,92 +102647,106 @@ |
| 101688 | 102647 | ** |
| 101689 | 102648 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 101690 | 102649 | ** |
| 101691 | 102650 | ** then nEq should be passed 0. |
| 101692 | 102651 | ** |
| 101693 | | -** The returned value is an integer divisor to reduce the estimated |
| 101694 | | -** search space. A return value of 1 means that range constraints are |
| 101695 | | -** no help at all. A return value of 2 means range constraints are |
| 101696 | | -** expected to reduce the search space by half. And so forth... |
| 102652 | +** The returned value is an integer between 1 and 100, inclusive. A return |
| 102653 | +** value of 1 indicates that the proposed range scan is expected to visit |
| 102654 | +** approximately 1/100th (1%) of the rows selected by the nEq equality |
| 102655 | +** constraints (if any). A return value of 100 indicates that it is expected |
| 102656 | +** that the range scan will visit every row (100%) selected by the equality |
| 102657 | +** constraints. |
| 101697 | 102658 | ** |
| 101698 | | -** In the absence of sqlite_stat3 ANALYZE data, each range inequality |
| 101699 | | -** reduces the search space by a factor of 4. Hence a single constraint (x>?) |
| 101700 | | -** results in a return of 4 and a range constraint (x>? AND x<?) results |
| 101701 | | -** in a return of 16. |
| 102659 | +** In the absence of sqlite_stat2 ANALYZE data, each range inequality |
| 102660 | +** reduces the search space by 3/4ths. Hence a single constraint (x>?) |
| 102661 | +** results in a return of 25 and a range constraint (x>? AND x<?) results |
| 102662 | +** in a return of 6. |
| 101702 | 102663 | */ |
| 101703 | 102664 | static int whereRangeScanEst( |
| 101704 | 102665 | Parse *pParse, /* Parsing & code generating context */ |
| 101705 | 102666 | Index *p, /* The index containing the range-compared column; "x" */ |
| 101706 | 102667 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 101707 | 102668 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 101708 | 102669 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 101709 | | - double *pRangeDiv /* OUT: Reduce search space by this divisor */ |
| 102670 | + int *piEst /* OUT: Return value */ |
| 101710 | 102671 | ){ |
| 101711 | 102672 | int rc = SQLITE_OK; |
| 101712 | 102673 | |
| 101713 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 102674 | +#ifdef SQLITE_ENABLE_STAT2 |
| 101714 | 102675 | |
| 101715 | | - if( nEq==0 && p->nSample ){ |
| 101716 | | - sqlite3_value *pRangeVal; |
| 101717 | | - tRowcnt iLower = 0; |
| 101718 | | - tRowcnt iUpper = p->aiRowEst[0]; |
| 101719 | | - tRowcnt a[2]; |
| 102676 | + if( nEq==0 && p->aSample ){ |
| 102677 | + sqlite3_value *pLowerVal = 0; |
| 102678 | + sqlite3_value *pUpperVal = 0; |
| 102679 | + int iEst; |
| 102680 | + int iLower = 0; |
| 102681 | + int iUpper = SQLITE_INDEX_SAMPLES; |
| 102682 | + int roundUpUpper = 0; |
| 102683 | + int roundUpLower = 0; |
| 101720 | 102684 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 101721 | 102685 | |
| 101722 | 102686 | if( pLower ){ |
| 101723 | 102687 | Expr *pExpr = pLower->pExpr->pRight; |
| 101724 | | - rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
| 102688 | + rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal); |
| 101725 | 102689 | assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE ); |
| 101726 | | - if( rc==SQLITE_OK |
| 101727 | | - && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK |
| 101728 | | - ){ |
| 101729 | | - iLower = a[0]; |
| 101730 | | - if( pLower->eOperator==WO_GT ) iLower += a[1]; |
| 101731 | | - } |
| 101732 | | - sqlite3ValueFree(pRangeVal); |
| 102690 | + roundUpLower = (pLower->eOperator==WO_GT) ?1:0; |
| 101733 | 102691 | } |
| 101734 | 102692 | if( rc==SQLITE_OK && pUpper ){ |
| 101735 | 102693 | Expr *pExpr = pUpper->pExpr->pRight; |
| 101736 | | - rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
| 102694 | + rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal); |
| 101737 | 102695 | assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE ); |
| 101738 | | - if( rc==SQLITE_OK |
| 101739 | | - && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK |
| 101740 | | - ){ |
| 101741 | | - iUpper = a[0]; |
| 101742 | | - if( pUpper->eOperator==WO_LE ) iUpper += a[1]; |
| 101743 | | - } |
| 101744 | | - sqlite3ValueFree(pRangeVal); |
| 101745 | | - } |
| 101746 | | - if( rc==SQLITE_OK ){ |
| 101747 | | - if( iUpper<=iLower ){ |
| 101748 | | - *pRangeDiv = (double)p->aiRowEst[0]; |
| 101749 | | - }else{ |
| 101750 | | - *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower); |
| 101751 | | - } |
| 101752 | | - WHERETRACE(("range scan regions: %u..%u div=%g\n", |
| 101753 | | - (u32)iLower, (u32)iUpper, *pRangeDiv)); |
| 101754 | | - return SQLITE_OK; |
| 101755 | | - } |
| 101756 | | - } |
| 102696 | + roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0; |
| 102697 | + } |
| 102698 | + |
| 102699 | + if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){ |
| 102700 | + sqlite3ValueFree(pLowerVal); |
| 102701 | + sqlite3ValueFree(pUpperVal); |
| 102702 | + goto range_est_fallback; |
| 102703 | + }else if( pLowerVal==0 ){ |
| 102704 | + rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); |
| 102705 | + if( pLower ) iLower = iUpper/2; |
| 102706 | + }else if( pUpperVal==0 ){ |
| 102707 | + rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); |
| 102708 | + if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2; |
| 102709 | + }else{ |
| 102710 | + rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); |
| 102711 | + if( rc==SQLITE_OK ){ |
| 102712 | + rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); |
| 102713 | + } |
| 102714 | + } |
| 102715 | + WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper)); |
| 102716 | + |
| 102717 | + iEst = iUpper - iLower; |
| 102718 | + testcase( iEst==SQLITE_INDEX_SAMPLES ); |
| 102719 | + assert( iEst<=SQLITE_INDEX_SAMPLES ); |
| 102720 | + if( iEst<1 ){ |
| 102721 | + *piEst = 50/SQLITE_INDEX_SAMPLES; |
| 102722 | + }else{ |
| 102723 | + *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES; |
| 102724 | + } |
| 102725 | + sqlite3ValueFree(pLowerVal); |
| 102726 | + sqlite3ValueFree(pUpperVal); |
| 102727 | + return rc; |
| 102728 | + } |
| 102729 | +range_est_fallback: |
| 101757 | 102730 | #else |
| 101758 | 102731 | UNUSED_PARAMETER(pParse); |
| 101759 | 102732 | UNUSED_PARAMETER(p); |
| 101760 | 102733 | UNUSED_PARAMETER(nEq); |
| 101761 | 102734 | #endif |
| 101762 | 102735 | assert( pLower || pUpper ); |
| 101763 | | - *pRangeDiv = (double)1; |
| 101764 | | - if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4; |
| 101765 | | - if( pUpper ) *pRangeDiv *= (double)4; |
| 102736 | + *piEst = 100; |
| 102737 | + if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4; |
| 102738 | + if( pUpper ) *piEst /= 4; |
| 101766 | 102739 | return rc; |
| 101767 | 102740 | } |
| 101768 | 102741 | |
| 101769 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 102742 | +#ifdef SQLITE_ENABLE_STAT2 |
| 101770 | 102743 | /* |
| 101771 | 102744 | ** Estimate the number of rows that will be returned based on |
| 101772 | 102745 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 101773 | 102746 | ** the histogram data. This only works when x is the left-most |
| 101774 | | -** column of an index and sqlite_stat3 histogram data is available |
| 102747 | +** column of an index and sqlite_stat2 histogram data is available |
| 101775 | 102748 | ** for that index. When pExpr==NULL that means the constraint is |
| 101776 | 102749 | ** "x IS NULL" instead of "x=VALUE". |
| 101777 | 102750 | ** |
| 101778 | 102751 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 101779 | 102752 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| | @@ -101789,13 +102762,14 @@ |
| 101789 | 102762 | Index *p, /* The index whose left-most column is pTerm */ |
| 101790 | 102763 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 101791 | 102764 | double *pnRow /* Write the revised row estimate here */ |
| 101792 | 102765 | ){ |
| 101793 | 102766 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
| 102767 | + int iLower, iUpper; /* Range of histogram regions containing pRhs */ |
| 101794 | 102768 | u8 aff; /* Column affinity */ |
| 101795 | 102769 | int rc; /* Subfunction return code */ |
| 101796 | | - tRowcnt a[2]; /* Statistics */ |
| 102770 | + double nRowEst; /* New estimate of the number of rows */ |
| 101797 | 102771 | |
| 101798 | 102772 | assert( p->aSample!=0 ); |
| 101799 | 102773 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 101800 | 102774 | if( pExpr ){ |
| 101801 | 102775 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| | @@ -101802,22 +102776,30 @@ |
| 101802 | 102776 | if( rc ) goto whereEqualScanEst_cancel; |
| 101803 | 102777 | }else{ |
| 101804 | 102778 | pRhs = sqlite3ValueNew(pParse->db); |
| 101805 | 102779 | } |
| 101806 | 102780 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
| 101807 | | - rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 101808 | | - if( rc==SQLITE_OK ){ |
| 101809 | | - WHERETRACE(("equality scan regions: %d\n", (int)a[1])); |
| 101810 | | - *pnRow = a[1]; |
| 102781 | + rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower); |
| 102782 | + if( rc ) goto whereEqualScanEst_cancel; |
| 102783 | + rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper); |
| 102784 | + if( rc ) goto whereEqualScanEst_cancel; |
| 102785 | + WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper)); |
| 102786 | + if( iLower>=iUpper ){ |
| 102787 | + nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2); |
| 102788 | + if( nRowEst<*pnRow ) *pnRow = nRowEst; |
| 102789 | + }else{ |
| 102790 | + nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES; |
| 102791 | + *pnRow = nRowEst; |
| 101811 | 102792 | } |
| 102793 | + |
| 101812 | 102794 | whereEqualScanEst_cancel: |
| 101813 | 102795 | sqlite3ValueFree(pRhs); |
| 101814 | 102796 | return rc; |
| 101815 | 102797 | } |
| 101816 | | -#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 102798 | +#endif /* defined(SQLITE_ENABLE_STAT2) */ |
| 101817 | 102799 | |
| 101818 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 102800 | +#ifdef SQLITE_ENABLE_STAT2 |
| 101819 | 102801 | /* |
| 101820 | 102802 | ** Estimate the number of rows that will be returned based on |
| 101821 | 102803 | ** an IN constraint where the right-hand side of the IN operator |
| 101822 | 102804 | ** is a list of values. Example: |
| 101823 | 102805 | ** |
| | @@ -101836,29 +102818,64 @@ |
| 101836 | 102818 | Parse *pParse, /* Parsing & code generating context */ |
| 101837 | 102819 | Index *p, /* The index whose left-most column is pTerm */ |
| 101838 | 102820 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 101839 | 102821 | double *pnRow /* Write the revised row estimate here */ |
| 101840 | 102822 | ){ |
| 101841 | | - int rc = SQLITE_OK; /* Subfunction return code */ |
| 101842 | | - double nEst; /* Number of rows for a single term */ |
| 101843 | | - double nRowEst = (double)0; /* New estimate of the number of rows */ |
| 101844 | | - int i; /* Loop counter */ |
| 102823 | + sqlite3_value *pVal = 0; /* One value from list */ |
| 102824 | + int iLower, iUpper; /* Range of histogram regions containing pRhs */ |
| 102825 | + u8 aff; /* Column affinity */ |
| 102826 | + int rc = SQLITE_OK; /* Subfunction return code */ |
| 102827 | + double nRowEst; /* New estimate of the number of rows */ |
| 102828 | + int nSpan = 0; /* Number of histogram regions spanned */ |
| 102829 | + int nSingle = 0; /* Histogram regions hit by a single value */ |
| 102830 | + int nNotFound = 0; /* Count of values that are not constants */ |
| 102831 | + int i; /* Loop counter */ |
| 102832 | + u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */ |
| 102833 | + u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */ |
| 101845 | 102834 | |
| 101846 | 102835 | assert( p->aSample!=0 ); |
| 101847 | | - for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 101848 | | - nEst = p->aiRowEst[0]; |
| 101849 | | - rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| 101850 | | - nRowEst += nEst; |
| 102836 | + aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 102837 | + memset(aSpan, 0, sizeof(aSpan)); |
| 102838 | + memset(aSingle, 0, sizeof(aSingle)); |
| 102839 | + for(i=0; i<pList->nExpr; i++){ |
| 102840 | + sqlite3ValueFree(pVal); |
| 102841 | + rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal); |
| 102842 | + if( rc ) break; |
| 102843 | + if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){ |
| 102844 | + nNotFound++; |
| 102845 | + continue; |
| 102846 | + } |
| 102847 | + rc = whereRangeRegion(pParse, p, pVal, 0, &iLower); |
| 102848 | + if( rc ) break; |
| 102849 | + rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper); |
| 102850 | + if( rc ) break; |
| 102851 | + if( iLower>=iUpper ){ |
| 102852 | + aSingle[iLower] = 1; |
| 102853 | + }else{ |
| 102854 | + assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES ); |
| 102855 | + while( iLower<iUpper ) aSpan[iLower++] = 1; |
| 102856 | + } |
| 101851 | 102857 | } |
| 101852 | 102858 | if( rc==SQLITE_OK ){ |
| 102859 | + for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){ |
| 102860 | + if( aSpan[i] ){ |
| 102861 | + nSpan++; |
| 102862 | + }else if( aSingle[i] ){ |
| 102863 | + nSingle++; |
| 102864 | + } |
| 102865 | + } |
| 102866 | + nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES) |
| 102867 | + + nNotFound*p->aiRowEst[1]; |
| 101853 | 102868 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 101854 | 102869 | *pnRow = nRowEst; |
| 101855 | | - WHERETRACE(("IN row estimate: est=%g\n", nRowEst)); |
| 102870 | + WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n", |
| 102871 | + nSpan, nSingle, nNotFound, nRowEst)); |
| 101856 | 102872 | } |
| 102873 | + sqlite3ValueFree(pVal); |
| 101857 | 102874 | return rc; |
| 101858 | 102875 | } |
| 101859 | | -#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 102876 | +#endif /* defined(SQLITE_ENABLE_STAT2) */ |
| 101860 | 102877 | |
| 101861 | 102878 | |
| 101862 | 102879 | /* |
| 101863 | 102880 | ** Find the best query plan for accessing a particular table. Write the |
| 101864 | 102881 | ** best query plan and its cost into the WhereCost object supplied as the |
| | @@ -101901,11 +102918,11 @@ |
| 101901 | 102918 | Index *pProbe; /* An index we are evaluating */ |
| 101902 | 102919 | Index *pIdx; /* Copy of pProbe, or zero for IPK index */ |
| 101903 | 102920 | int eqTermMask; /* Current mask of valid equality operators */ |
| 101904 | 102921 | int idxEqTermMask; /* Index mask of valid equality operators */ |
| 101905 | 102922 | Index sPk; /* A fake index object for the primary key */ |
| 101906 | | - tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 102923 | + unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 101907 | 102924 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 101908 | 102925 | int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */ |
| 101909 | 102926 | |
| 101910 | 102927 | /* Initialize the cost to a worst-case value */ |
| 101911 | 102928 | memset(pCost, 0, sizeof(*pCost)); |
| | @@ -101956,11 +102973,11 @@ |
| 101956 | 102973 | } |
| 101957 | 102974 | |
| 101958 | 102975 | /* Loop over all indices looking for the best one to use |
| 101959 | 102976 | */ |
| 101960 | 102977 | for(; pProbe; pIdx=pProbe=pProbe->pNext){ |
| 101961 | | - const tRowcnt * const aiRowEst = pProbe->aiRowEst; |
| 102978 | + const unsigned int * const aiRowEst = pProbe->aiRowEst; |
| 101962 | 102979 | double cost; /* Cost of using pProbe */ |
| 101963 | 102980 | double nRow; /* Estimated number of rows in result set */ |
| 101964 | 102981 | double log10N; /* base-10 logarithm of nRow (inexact) */ |
| 101965 | 102982 | int rev; /* True to scan in reverse order */ |
| 101966 | 102983 | int wsFlags = 0; |
| | @@ -101999,16 +103016,18 @@ |
| 101999 | 103016 | ** Set to true if there was at least one "x IN (SELECT ...)" term used |
| 102000 | 103017 | ** in determining the value of nInMul. Note that the RHS of the |
| 102001 | 103018 | ** IN operator must be a SELECT, not a value list, for this variable |
| 102002 | 103019 | ** to be true. |
| 102003 | 103020 | ** |
| 102004 | | - ** rangeDiv: |
| 102005 | | - ** An estimate of a divisor by which to reduce the search space due |
| 102006 | | - ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE |
| 102007 | | - ** data, a single inequality reduces the search space to 1/4rd its |
| 102008 | | - ** original size (rangeDiv==4). Two inequalities reduce the search |
| 102009 | | - ** space to 1/16th of its original size (rangeDiv==16). |
| 103021 | + ** estBound: |
| 103022 | + ** An estimate on the amount of the table that must be searched. A |
| 103023 | + ** value of 100 means the entire table is searched. Range constraints |
| 103024 | + ** might reduce this to a value less than 100 to indicate that only |
| 103025 | + ** a fraction of the table needs searching. In the absence of |
| 103026 | + ** sqlite_stat2 ANALYZE data, a single inequality reduces the search |
| 103027 | + ** space to 1/4rd its original size. So an x>? constraint reduces |
| 103028 | + ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6. |
| 102010 | 103029 | ** |
| 102011 | 103030 | ** bSort: |
| 102012 | 103031 | ** Boolean. True if there is an ORDER BY clause that will require an |
| 102013 | 103032 | ** external sort (i.e. scanning the index being evaluated will not |
| 102014 | 103033 | ** correctly order records). |
| | @@ -102029,17 +103048,17 @@ |
| 102029 | 103048 | ** SELECT a, b, c FROM tbl WHERE a = 1; |
| 102030 | 103049 | */ |
| 102031 | 103050 | int nEq; /* Number of == or IN terms matching index */ |
| 102032 | 103051 | int bInEst = 0; /* True if "x IN (SELECT...)" seen */ |
| 102033 | 103052 | int nInMul = 1; /* Number of distinct equalities to lookup */ |
| 102034 | | - double rangeDiv = (double)1; /* Estimated reduction in search space */ |
| 103053 | + int estBound = 100; /* Estimated reduction in search space */ |
| 102035 | 103054 | int nBound = 0; /* Number of range constraints seen */ |
| 102036 | 103055 | int bSort = !!pOrderBy; /* True if external sort required */ |
| 102037 | 103056 | int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */ |
| 102038 | 103057 | int bLookup = 0; /* True if not a covering index */ |
| 102039 | 103058 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 102040 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 103059 | +#ifdef SQLITE_ENABLE_STAT2 |
| 102041 | 103060 | WhereTerm *pFirstTerm = 0; /* First term matching the index */ |
| 102042 | 103061 | #endif |
| 102043 | 103062 | |
| 102044 | 103063 | /* Determine the values of nEq and nInMul */ |
| 102045 | 103064 | for(nEq=0; nEq<pProbe->nColumn; nEq++){ |
| | @@ -102059,23 +103078,23 @@ |
| 102059 | 103078 | nInMul *= pExpr->x.pList->nExpr; |
| 102060 | 103079 | } |
| 102061 | 103080 | }else if( pTerm->eOperator & WO_ISNULL ){ |
| 102062 | 103081 | wsFlags |= WHERE_COLUMN_NULL; |
| 102063 | 103082 | } |
| 102064 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 103083 | +#ifdef SQLITE_ENABLE_STAT2 |
| 102065 | 103084 | if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm; |
| 102066 | 103085 | #endif |
| 102067 | 103086 | used |= pTerm->prereqRight; |
| 102068 | 103087 | } |
| 102069 | 103088 | |
| 102070 | | - /* Determine the value of rangeDiv */ |
| 103089 | + /* Determine the value of estBound. */ |
| 102071 | 103090 | if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){ |
| 102072 | 103091 | int j = pProbe->aiColumn[nEq]; |
| 102073 | 103092 | if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){ |
| 102074 | 103093 | WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx); |
| 102075 | 103094 | WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx); |
| 102076 | | - whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv); |
| 103095 | + whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound); |
| 102077 | 103096 | if( pTop ){ |
| 102078 | 103097 | nBound = 1; |
| 102079 | 103098 | wsFlags |= WHERE_TOP_LIMIT; |
| 102080 | 103099 | used |= pTop->prereqRight; |
| 102081 | 103100 | } |
| | @@ -102143,11 +103162,11 @@ |
| 102143 | 103162 | if( bInEst && nRow*2>aiRowEst[0] ){ |
| 102144 | 103163 | nRow = aiRowEst[0]/2; |
| 102145 | 103164 | nInMul = (int)(nRow / aiRowEst[nEq]); |
| 102146 | 103165 | } |
| 102147 | 103166 | |
| 102148 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 103167 | +#ifdef SQLITE_ENABLE_STAT2 |
| 102149 | 103168 | /* If the constraint is of the form x=VALUE or x IN (E1,E2,...) |
| 102150 | 103169 | ** and we do not think that values of x are unique and if histogram |
| 102151 | 103170 | ** data is available for column x, then it might be possible |
| 102152 | 103171 | ** to get a better estimate on the number of rows based on |
| 102153 | 103172 | ** VALUE and how common that value is according to the histogram. |
| | @@ -102159,16 +103178,16 @@ |
| 102159 | 103178 | whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow); |
| 102160 | 103179 | }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){ |
| 102161 | 103180 | whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow); |
| 102162 | 103181 | } |
| 102163 | 103182 | } |
| 102164 | | -#endif /* SQLITE_ENABLE_STAT3 */ |
| 103183 | +#endif /* SQLITE_ENABLE_STAT2 */ |
| 102165 | 103184 | |
| 102166 | 103185 | /* Adjust the number of output rows and downward to reflect rows |
| 102167 | 103186 | ** that are excluded by range constraints. |
| 102168 | 103187 | */ |
| 102169 | | - nRow = nRow/rangeDiv; |
| 103188 | + nRow = (nRow * (double)estBound) / (double)100; |
| 102170 | 103189 | if( nRow<1 ) nRow = 1; |
| 102171 | 103190 | |
| 102172 | 103191 | /* Experiments run on real SQLite databases show that the time needed |
| 102173 | 103192 | ** to do a binary search to locate a row in a table or index is roughly |
| 102174 | 103193 | ** log10(N) times the time to move from one row to the next row within |
| | @@ -102293,14 +103312,14 @@ |
| 102293 | 103312 | if( nRow<2 ) nRow = 2; |
| 102294 | 103313 | } |
| 102295 | 103314 | |
| 102296 | 103315 | |
| 102297 | 103316 | WHERETRACE(( |
| 102298 | | - "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 103317 | + "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 102299 | 103318 | " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n", |
| 102300 | 103319 | pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), |
| 102301 | | - nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags, |
| 103320 | + nEq, nInMul, estBound, bSort, bLookup, wsFlags, |
| 102302 | 103321 | notReady, log10N, nRow, cost, used |
| 102303 | 103322 | )); |
| 102304 | 103323 | |
| 102305 | 103324 | /* If this index is the best we have seen so far, then record this |
| 102306 | 103325 | ** index and its cost in the pCost structure. |
| | @@ -104227,10 +105246,11 @@ |
| 104227 | 105246 | ** LALR(1) grammar but which are always false in the |
| 104228 | 105247 | ** specific grammar used by SQLite. |
| 104229 | 105248 | */ |
| 104230 | 105249 | /* First off, code is included that follows the "include" declaration |
| 104231 | 105250 | ** in the input grammar file. */ |
| 105251 | +/* #include <stdio.h> */ |
| 104232 | 105252 | |
| 104233 | 105253 | |
| 104234 | 105254 | /* |
| 104235 | 105255 | ** Disable all error recovery processing in the parser push-down |
| 104236 | 105256 | ** automaton. |
| | @@ -105087,10 +106107,11 @@ |
| 105087 | 106107 | #endif |
| 105088 | 106108 | }; |
| 105089 | 106109 | typedef struct yyParser yyParser; |
| 105090 | 106110 | |
| 105091 | 106111 | #ifndef NDEBUG |
| 106112 | +/* #include <stdio.h> */ |
| 105092 | 106113 | static FILE *yyTraceFILE = 0; |
| 105093 | 106114 | static char *yyTracePrompt = 0; |
| 105094 | 106115 | #endif /* NDEBUG */ |
| 105095 | 106116 | |
| 105096 | 106117 | #ifndef NDEBUG |
| | @@ -107662,10 +108683,11 @@ |
| 107662 | 108683 | ** |
| 107663 | 108684 | ** This file contains C code that splits an SQL input string up into |
| 107664 | 108685 | ** individual tokens and sends those tokens one-by-one over to the |
| 107665 | 108686 | ** parser for analysis. |
| 107666 | 108687 | */ |
| 108688 | +/* #include <stdlib.h> */ |
| 107667 | 108689 | |
| 107668 | 108690 | /* |
| 107669 | 108691 | ** The charMap() macro maps alphabetic characters into their |
| 107670 | 108692 | ** lower-case ASCII equivalent. On ASCII machines, this is just |
| 107671 | 108693 | ** an upper-to-lower case map. On EBCDIC machines we also need |
| | @@ -109053,10 +110075,20 @@ |
| 109053 | 110075 | memcpy(&y, &x, 8); |
| 109054 | 110076 | assert( sqlite3IsNaN(y) ); |
| 109055 | 110077 | } |
| 109056 | 110078 | #endif |
| 109057 | 110079 | #endif |
| 110080 | + |
| 110081 | + /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT |
| 110082 | + ** compile-time option. |
| 110083 | + */ |
| 110084 | +#ifdef SQLITE_EXTRA_INIT |
| 110085 | + if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){ |
| 110086 | + int SQLITE_EXTRA_INIT(void); |
| 110087 | + rc = SQLITE_EXTRA_INIT(); |
| 110088 | + } |
| 110089 | +#endif |
| 109058 | 110090 | |
| 109059 | 110091 | return rc; |
| 109060 | 110092 | } |
| 109061 | 110093 | |
| 109062 | 110094 | /* |
| | @@ -113184,10 +114216,16 @@ |
| 113184 | 114216 | |
| 113185 | 114217 | #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE) |
| 113186 | 114218 | # define SQLITE_CORE 1 |
| 113187 | 114219 | #endif |
| 113188 | 114220 | |
| 114221 | +/* #include <assert.h> */ |
| 114222 | +/* #include <stdlib.h> */ |
| 114223 | +/* #include <stddef.h> */ |
| 114224 | +/* #include <stdio.h> */ |
| 114225 | +/* #include <string.h> */ |
| 114226 | +/* #include <stdarg.h> */ |
| 113189 | 114227 | |
| 113190 | 114228 | #ifndef SQLITE_CORE |
| 113191 | 114229 | SQLITE_EXTENSION_INIT1 |
| 113192 | 114230 | #endif |
| 113193 | 114231 | |
| | @@ -117705,10 +118743,12 @@ |
| 117705 | 118743 | ****************************************************************************** |
| 117706 | 118744 | ** |
| 117707 | 118745 | */ |
| 117708 | 118746 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 117709 | 118747 | |
| 118748 | +/* #include <string.h> */ |
| 118749 | +/* #include <assert.h> */ |
| 117710 | 118750 | |
| 117711 | 118751 | typedef struct Fts3auxTable Fts3auxTable; |
| 117712 | 118752 | typedef struct Fts3auxCursor Fts3auxCursor; |
| 117713 | 118753 | |
| 117714 | 118754 | struct Fts3auxTable { |
| | @@ -118243,10 +119283,12 @@ |
| 118243 | 119283 | /* |
| 118244 | 119284 | ** Default span for NEAR operators. |
| 118245 | 119285 | */ |
| 118246 | 119286 | #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10 |
| 118247 | 119287 | |
| 119288 | +/* #include <string.h> */ |
| 119289 | +/* #include <assert.h> */ |
| 118248 | 119290 | |
| 118249 | 119291 | /* |
| 118250 | 119292 | ** isNot: |
| 118251 | 119293 | ** This variable is used by function getNextNode(). When getNextNode() is |
| 118252 | 119294 | ** called, it sets ParseContext.isNot to true if the 'next node' is a |
| | @@ -118944,10 +119986,11 @@ |
| 118944 | 119986 | ** Everything after this point is just test code. |
| 118945 | 119987 | */ |
| 118946 | 119988 | |
| 118947 | 119989 | #ifdef SQLITE_TEST |
| 118948 | 119990 | |
| 119991 | +/* #include <stdio.h> */ |
| 118949 | 119992 | |
| 118950 | 119993 | /* |
| 118951 | 119994 | ** Function to query the hash-table of tokenizers (see README.tokenizers). |
| 118952 | 119995 | */ |
| 118953 | 119996 | static int queryTestTokenizer( |
| | @@ -119154,10 +120197,13 @@ |
| 119154 | 120197 | ** * The FTS3 module is being built into the core of |
| 119155 | 120198 | ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
| 119156 | 120199 | */ |
| 119157 | 120200 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 119158 | 120201 | |
| 120202 | +/* #include <assert.h> */ |
| 120203 | +/* #include <stdlib.h> */ |
| 120204 | +/* #include <string.h> */ |
| 119159 | 120205 | |
| 119160 | 120206 | |
| 119161 | 120207 | /* |
| 119162 | 120208 | ** Malloc and Free functions |
| 119163 | 120209 | */ |
| | @@ -119534,10 +120580,14 @@ |
| 119534 | 120580 | ** * The FTS3 module is being built into the core of |
| 119535 | 120581 | ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
| 119536 | 120582 | */ |
| 119537 | 120583 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 119538 | 120584 | |
| 120585 | +/* #include <assert.h> */ |
| 120586 | +/* #include <stdlib.h> */ |
| 120587 | +/* #include <stdio.h> */ |
| 120588 | +/* #include <string.h> */ |
| 119539 | 120589 | |
| 119540 | 120590 | |
| 119541 | 120591 | /* |
| 119542 | 120592 | ** Class derived from sqlite3_tokenizer |
| 119543 | 120593 | */ |
| | @@ -120177,10 +121227,12 @@ |
| 120177 | 121227 | ** * The FTS3 module is being built into the core of |
| 120178 | 121228 | ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
| 120179 | 121229 | */ |
| 120180 | 121230 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 120181 | 121231 | |
| 121232 | +/* #include <assert.h> */ |
| 121233 | +/* #include <string.h> */ |
| 120182 | 121234 | |
| 120183 | 121235 | /* |
| 120184 | 121236 | ** Implementation of the SQL scalar function for accessing the underlying |
| 120185 | 121237 | ** hash table. This function may be called as follows: |
| 120186 | 121238 | ** |
| | @@ -120352,10 +121404,12 @@ |
| 120352 | 121404 | } |
| 120353 | 121405 | |
| 120354 | 121406 | |
| 120355 | 121407 | #ifdef SQLITE_TEST |
| 120356 | 121408 | |
| 121409 | +/* #include <tcl.h> */ |
| 121410 | +/* #include <string.h> */ |
| 120357 | 121411 | |
| 120358 | 121412 | /* |
| 120359 | 121413 | ** Implementation of a special SQL scalar function for testing tokenizers |
| 120360 | 121414 | ** designed to be used in concert with the Tcl testing framework. This |
| 120361 | 121415 | ** function must be called with two arguments: |
| | @@ -120663,10 +121717,14 @@ |
| 120663 | 121717 | ** * The FTS3 module is being built into the core of |
| 120664 | 121718 | ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
| 120665 | 121719 | */ |
| 120666 | 121720 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 120667 | 121721 | |
| 121722 | +/* #include <assert.h> */ |
| 121723 | +/* #include <stdlib.h> */ |
| 121724 | +/* #include <stdio.h> */ |
| 121725 | +/* #include <string.h> */ |
| 120668 | 121726 | |
| 120669 | 121727 | |
| 120670 | 121728 | typedef struct simple_tokenizer { |
| 120671 | 121729 | sqlite3_tokenizer base; |
| 120672 | 121730 | char delim[128]; /* flag ASCII delimiters */ |
| | @@ -120888,10 +121946,13 @@ |
| 120888 | 121946 | ** code in fts3.c. |
| 120889 | 121947 | */ |
| 120890 | 121948 | |
| 120891 | 121949 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 120892 | 121950 | |
| 121951 | +/* #include <string.h> */ |
| 121952 | +/* #include <assert.h> */ |
| 121953 | +/* #include <stdlib.h> */ |
| 120893 | 121954 | |
| 120894 | 121955 | /* |
| 120895 | 121956 | ** When full-text index nodes are loaded from disk, the buffer that they |
| 120896 | 121957 | ** are loaded into has the following number of bytes of padding at the end |
| 120897 | 121958 | ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer |
| | @@ -124149,10 +125210,12 @@ |
| 124149 | 125210 | ****************************************************************************** |
| 124150 | 125211 | */ |
| 124151 | 125212 | |
| 124152 | 125213 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 124153 | 125214 | |
| 125215 | +/* #include <string.h> */ |
| 125216 | +/* #include <assert.h> */ |
| 124154 | 125217 | |
| 124155 | 125218 | /* |
| 124156 | 125219 | ** Characters that may appear in the second argument to matchinfo(). |
| 124157 | 125220 | */ |
| 124158 | 125221 | #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */ |
| | @@ -125736,10 +126799,12 @@ |
| 125736 | 126799 | #ifndef SQLITE_CORE |
| 125737 | 126800 | SQLITE_EXTENSION_INIT1 |
| 125738 | 126801 | #else |
| 125739 | 126802 | #endif |
| 125740 | 126803 | |
| 126804 | +/* #include <string.h> */ |
| 126805 | +/* #include <assert.h> */ |
| 125741 | 126806 | |
| 125742 | 126807 | #ifndef SQLITE_AMALGAMATION |
| 125743 | 126808 | #include "sqlite3rtree.h" |
| 125744 | 126809 | typedef sqlite3_int64 i64; |
| 125745 | 126810 | typedef unsigned char u8; |
| | @@ -128950,10 +130015,11 @@ |
| 128950 | 130015 | #include <unicode/utypes.h> |
| 128951 | 130016 | #include <unicode/uregex.h> |
| 128952 | 130017 | #include <unicode/ustring.h> |
| 128953 | 130018 | #include <unicode/ucol.h> |
| 128954 | 130019 | |
| 130020 | +/* #include <assert.h> */ |
| 128955 | 130021 | |
| 128956 | 130022 | #ifndef SQLITE_CORE |
| 128957 | 130023 | SQLITE_EXTENSION_INIT1 |
| 128958 | 130024 | #else |
| 128959 | 130025 | #endif |
| | @@ -129429,12 +130495,16 @@ |
| 129429 | 130495 | ** This file implements a tokenizer for fts3 based on the ICU library. |
| 129430 | 130496 | */ |
| 129431 | 130497 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 129432 | 130498 | #ifdef SQLITE_ENABLE_ICU |
| 129433 | 130499 | |
| 130500 | +/* #include <assert.h> */ |
| 130501 | +/* #include <string.h> */ |
| 129434 | 130502 | |
| 129435 | 130503 | #include <unicode/ubrk.h> |
| 130504 | +/* #include <unicode/ucol.h> */ |
| 130505 | +/* #include <unicode/ustring.h> */ |
| 129436 | 130506 | #include <unicode/utf16.h> |
| 129437 | 130507 | |
| 129438 | 130508 | typedef struct IcuTokenizer IcuTokenizer; |
| 129439 | 130509 | typedef struct IcuCursor IcuCursor; |
| 129440 | 130510 | |
| 129441 | 130511 | |