| | @@ -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-09-04 01:27:00 6b657ae75035eb10b0ad640998d3c9eadfdffa6e" |
| 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( |
| | @@ -7711,22 +7711,10 @@ |
| 7711 | 7711 | ** is 0x00000000ffffffff. But because of quirks of some compilers, we |
| 7712 | 7712 | ** have to specify the value in the less intuitive manner shown: |
| 7713 | 7713 | */ |
| 7714 | 7714 | #define SQLITE_MAX_U32 ((((u64)1)<<32)-1) |
| 7715 | 7715 | |
| 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 | 7716 | /* |
| 7729 | 7717 | ** Macros to determine whether the machine is big or little endian, |
| 7730 | 7718 | ** evaluated at runtime. |
| 7731 | 7719 | */ |
| 7732 | 7720 | #ifdef SQLITE_AMALGAMATION |
| | @@ -8188,10 +8176,11 @@ |
| 8188 | 8176 | ** or VDBE. The VDBE implements an abstract machine that runs a |
| 8189 | 8177 | ** simple program to access and modify the underlying database. |
| 8190 | 8178 | */ |
| 8191 | 8179 | #ifndef _SQLITE_VDBE_H_ |
| 8192 | 8180 | #define _SQLITE_VDBE_H_ |
| 8181 | +/* #include <stdio.h> */ |
| 8193 | 8182 | |
| 8194 | 8183 | /* |
| 8195 | 8184 | ** A single VDBE is an opaque structure named "Vdbe". Only routines |
| 8196 | 8185 | ** in the source file sqliteVdbe.c are allowed to see the insides |
| 8197 | 8186 | ** of this structure. |
| | @@ -8231,10 +8220,11 @@ |
| 8231 | 8220 | Mem *pMem; /* Used when p4type is P4_MEM */ |
| 8232 | 8221 | VTable *pVtab; /* Used when p4type is P4_VTAB */ |
| 8233 | 8222 | KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ |
| 8234 | 8223 | int *ai; /* Used when p4type is P4_INTARRAY */ |
| 8235 | 8224 | SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ |
| 8225 | + int (*xAdvance)(BtCursor *, int *); |
| 8236 | 8226 | } p4; |
| 8237 | 8227 | #ifdef SQLITE_DEBUG |
| 8238 | 8228 | char *zComment; /* Comment to improve readability */ |
| 8239 | 8229 | #endif |
| 8240 | 8230 | #ifdef VDBE_PROFILE |
| | @@ -8286,10 +8276,11 @@ |
| 8286 | 8276 | #define P4_REAL (-12) /* P4 is a 64-bit floating point value */ |
| 8287 | 8277 | #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ |
| 8288 | 8278 | #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ |
| 8289 | 8279 | #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ |
| 8290 | 8280 | #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ |
| 8281 | +#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */ |
| 8291 | 8282 | |
| 8292 | 8283 | /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure |
| 8293 | 8284 | ** is made. That copy is freed when the Vdbe is finalized. But if the |
| 8294 | 8285 | ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still |
| 8295 | 8286 | ** gets freed when the Vdbe is finalized so it still should be obtained |
| | @@ -8401,88 +8392,90 @@ |
| 8401 | 8392 | #define OP_VerifyCookie 37 |
| 8402 | 8393 | #define OP_OpenRead 38 |
| 8403 | 8394 | #define OP_OpenWrite 39 |
| 8404 | 8395 | #define OP_OpenAutoindex 40 |
| 8405 | 8396 | #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 |
| 8483 | | -#define OP_NotUsed_140 140 |
| 8397 | +#define OP_SorterOpen 42 |
| 8398 | +#define OP_OpenPseudo 43 |
| 8399 | +#define OP_Close 44 |
| 8400 | +#define OP_SeekLt 45 |
| 8401 | +#define OP_SeekLe 46 |
| 8402 | +#define OP_SeekGe 47 |
| 8403 | +#define OP_SeekGt 48 |
| 8404 | +#define OP_Seek 49 |
| 8405 | +#define OP_NotFound 50 |
| 8406 | +#define OP_Found 51 |
| 8407 | +#define OP_IsUnique 52 |
| 8408 | +#define OP_NotExists 53 |
| 8409 | +#define OP_Sequence 54 |
| 8410 | +#define OP_NewRowid 55 |
| 8411 | +#define OP_Insert 56 |
| 8412 | +#define OP_InsertInt 57 |
| 8413 | +#define OP_Delete 58 |
| 8414 | +#define OP_ResetCount 59 |
| 8415 | +#define OP_SorterCompare 60 |
| 8416 | +#define OP_SorterData 61 |
| 8417 | +#define OP_RowKey 62 |
| 8418 | +#define OP_RowData 63 |
| 8419 | +#define OP_Rowid 64 |
| 8420 | +#define OP_NullRow 65 |
| 8421 | +#define OP_Last 66 |
| 8422 | +#define OP_SorterSort 67 |
| 8423 | +#define OP_Sort 70 |
| 8424 | +#define OP_Rewind 71 |
| 8425 | +#define OP_SorterNext 72 |
| 8426 | +#define OP_Prev 81 |
| 8427 | +#define OP_Next 92 |
| 8428 | +#define OP_SorterInsert 95 |
| 8429 | +#define OP_IdxInsert 96 |
| 8430 | +#define OP_IdxDelete 97 |
| 8431 | +#define OP_IdxRowid 98 |
| 8432 | +#define OP_IdxLT 99 |
| 8433 | +#define OP_IdxGE 100 |
| 8434 | +#define OP_Destroy 101 |
| 8435 | +#define OP_Clear 102 |
| 8436 | +#define OP_CreateIndex 103 |
| 8437 | +#define OP_CreateTable 104 |
| 8438 | +#define OP_ParseSchema 105 |
| 8439 | +#define OP_LoadAnalysis 106 |
| 8440 | +#define OP_DropTable 107 |
| 8441 | +#define OP_DropIndex 108 |
| 8442 | +#define OP_DropTrigger 109 |
| 8443 | +#define OP_IntegrityCk 110 |
| 8444 | +#define OP_RowSetAdd 111 |
| 8445 | +#define OP_RowSetRead 112 |
| 8446 | +#define OP_RowSetTest 113 |
| 8447 | +#define OP_Program 114 |
| 8448 | +#define OP_Param 115 |
| 8449 | +#define OP_FkCounter 116 |
| 8450 | +#define OP_FkIfZero 117 |
| 8451 | +#define OP_MemMax 118 |
| 8452 | +#define OP_IfPos 119 |
| 8453 | +#define OP_IfNeg 120 |
| 8454 | +#define OP_IfZero 121 |
| 8455 | +#define OP_AggStep 122 |
| 8456 | +#define OP_AggFinal 123 |
| 8457 | +#define OP_Checkpoint 124 |
| 8458 | +#define OP_JournalMode 125 |
| 8459 | +#define OP_Vacuum 126 |
| 8460 | +#define OP_IncrVacuum 127 |
| 8461 | +#define OP_Expire 128 |
| 8462 | +#define OP_TableLock 129 |
| 8463 | +#define OP_VBegin 131 |
| 8464 | +#define OP_VCreate 132 |
| 8465 | +#define OP_VDestroy 133 |
| 8466 | +#define OP_VOpen 134 |
| 8467 | +#define OP_VFilter 135 |
| 8468 | +#define OP_VColumn 136 |
| 8469 | +#define OP_VNext 137 |
| 8470 | +#define OP_VRename 138 |
| 8471 | +#define OP_VUpdate 139 |
| 8472 | +#define OP_Pagecount 140 |
| 8473 | +#define OP_MaxPgcnt 146 |
| 8474 | +#define OP_Trace 147 |
| 8475 | +#define OP_Noop 148 |
| 8476 | +#define OP_Explain 149 |
| 8484 | 8477 | |
| 8485 | 8478 | |
| 8486 | 8479 | /* Properties such as "out2" or "jump" that are specified in |
| 8487 | 8480 | ** comments following the "case" for each opcode in the vdbe.c |
| 8488 | 8481 | ** are encoded into bitvectors as follows: |
| | @@ -8498,24 +8491,24 @@ |
| 8498 | 8491 | /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\ |
| 8499 | 8492 | /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\ |
| 8500 | 8493 | /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\ |
| 8501 | 8494 | /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\ |
| 8502 | 8495 | /* 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,\ |
| 8496 | +/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11,\ |
| 8497 | +/* 48 */ 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02,\ |
| 8498 | +/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 8499 | +/* 64 */ 0x02, 0x00, 0x01, 0x01, 0x4c, 0x4c, 0x01, 0x01,\ |
| 8500 | +/* 72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ |
| 8508 | 8501 | /* 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,\ |
| 8516 | | -/* 144 */ 0x04, 0x04,} |
| 8502 | +/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x08,\ |
| 8503 | +/* 96 */ 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x02,\ |
| 8504 | +/* 104 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c,\ |
| 8505 | +/* 112 */ 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08, 0x05,\ |
| 8506 | +/* 120 */ 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\ |
| 8507 | +/* 128 */ 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01,\ |
| 8508 | +/* 136 */ 0x00, 0x01, 0x00, 0x00, 0x02, 0x04, 0x04, 0x04,\ |
| 8509 | +/* 144 */ 0x04, 0x04, 0x02, 0x00, 0x00, 0x00,} |
| 8517 | 8510 | |
| 8518 | 8511 | /************** End of opcodes.h *********************************************/ |
| 8519 | 8512 | /************** Continuing where we left off in vdbe.h ***********************/ |
| 8520 | 8513 | |
| 8521 | 8514 | /* |
| | @@ -8529,13 +8522,13 @@ |
| 8529 | 8522 | SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); |
| 8530 | 8523 | SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); |
| 8531 | 8524 | SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); |
| 8532 | 8525 | SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp); |
| 8533 | 8526 | 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); |
| 8527 | +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); |
| 8528 | +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); |
| 8529 | +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3); |
| 8537 | 8530 | SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5); |
| 8538 | 8531 | SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr); |
| 8539 | 8532 | SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N); |
| 8540 | 8533 | SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); |
| 8541 | 8534 | SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int); |
| | @@ -8653,10 +8646,11 @@ |
| 8653 | 8646 | ** NOTE: These values must match the corresponding BTREE_ values in btree.h. |
| 8654 | 8647 | */ |
| 8655 | 8648 | #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */ |
| 8656 | 8649 | #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */ |
| 8657 | 8650 | #define PAGER_MEMORY 0x0004 /* In-memory database */ |
| 8651 | +#define PAGER_SORTER 0x0020 /* Accumulator in external merge sort */ |
| 8658 | 8652 | |
| 8659 | 8653 | /* |
| 8660 | 8654 | ** Valid values for the second argument to sqlite3PagerLockingMode(). |
| 8661 | 8655 | */ |
| 8662 | 8656 | #define PAGER_LOCKINGMODE_QUERY -1 |
| | @@ -9924,11 +9918,11 @@ |
| 9924 | 9918 | int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ |
| 9925 | 9919 | int nCol; /* Number of columns in this table */ |
| 9926 | 9920 | Column *aCol; /* Information about each column */ |
| 9927 | 9921 | Index *pIndex; /* List of SQL indexes on this table. */ |
| 9928 | 9922 | int tnum; /* Root BTree node for this table (see note above) */ |
| 9929 | | - tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 9923 | + unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 9930 | 9924 | Select *pSelect; /* NULL for tables. Points to definition if a view. */ |
| 9931 | 9925 | u16 nRef; /* Number of pointers to this Table */ |
| 9932 | 9926 | u8 tabFlags; /* Mask of TF_* values */ |
| 9933 | 9927 | u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ |
| 9934 | 9928 | FKey *pFKey; /* Linked list of all foreign keys in this table */ |
| | @@ -10123,43 +10117,35 @@ |
| 10123 | 10117 | */ |
| 10124 | 10118 | struct Index { |
| 10125 | 10119 | char *zName; /* Name of this index */ |
| 10126 | 10120 | int nColumn; /* Number of columns in the table used by this index */ |
| 10127 | 10121 | int *aiColumn; /* Which columns are used by this index. 1st is 0 */ |
| 10128 | | - tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ |
| 10122 | + unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ |
| 10129 | 10123 | Table *pTable; /* The SQL table being indexed */ |
| 10130 | 10124 | int tnum; /* Page containing root of this index in database file */ |
| 10131 | 10125 | u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 10132 | 10126 | u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ |
| 10133 | 10127 | u8 bUnordered; /* Use this index for == or IN queries only */ |
| 10134 | | - u8 nSample; /* Number of elements in aSample[] */ |
| 10135 | 10128 | char *zColAff; /* String defining the affinity of each column */ |
| 10136 | 10129 | Index *pNext; /* The next index associated with the same table */ |
| 10137 | 10130 | Schema *pSchema; /* Schema containing this index */ |
| 10138 | 10131 | u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */ |
| 10139 | 10132 | 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 |
| 10133 | + IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */ |
| 10144 | 10134 | }; |
| 10145 | 10135 | |
| 10146 | 10136 | /* |
| 10147 | 10137 | ** Each sample stored in the sqlite_stat2 table is represented in memory |
| 10148 | 10138 | ** using a structure of this type. |
| 10149 | 10139 | */ |
| 10150 | 10140 | struct IndexSample { |
| 10151 | 10141 | union { |
| 10152 | 10142 | 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 */ |
| 10143 | + double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */ |
| 10155 | 10144 | } u; |
| 10156 | 10145 | 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 */ |
| 10146 | + u8 nByte; /* Size in byte of text or blob. */ |
| 10161 | 10147 | }; |
| 10162 | 10148 | |
| 10163 | 10149 | /* |
| 10164 | 10150 | ** Each token coming out of the lexer is an instance of |
| 10165 | 10151 | ** this structure. Tokens are also used as part of an expression. |
| | @@ -10190,10 +10176,11 @@ |
| 10190 | 10176 | u8 directMode; /* Direct rendering mode means take data directly |
| 10191 | 10177 | ** from source tables rather than from accumulators */ |
| 10192 | 10178 | u8 useSortingIdx; /* In direct mode, reference the sorting index rather |
| 10193 | 10179 | ** than the source table */ |
| 10194 | 10180 | int sortingIdx; /* Cursor number of the sorting index */ |
| 10181 | + int sortingIdxPTab; /* Cursor number of pseudo-table */ |
| 10195 | 10182 | ExprList *pGroupBy; /* The group by clause */ |
| 10196 | 10183 | int nSortingColumn; /* Number of columns in the sorting index */ |
| 10197 | 10184 | struct AggInfo_col { /* For each column used in source tables */ |
| 10198 | 10185 | Table *pTab; /* Source table */ |
| 10199 | 10186 | int iTable; /* Cursor number of the source table */ |
| | @@ -10722,10 +10709,11 @@ |
| 10722 | 10709 | #define SF_Resolved 0x0002 /* Identifiers have been resolved */ |
| 10723 | 10710 | #define SF_Aggregate 0x0004 /* Contains aggregate functions */ |
| 10724 | 10711 | #define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */ |
| 10725 | 10712 | #define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */ |
| 10726 | 10713 | #define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */ |
| 10714 | +#define SF_UseSorter 0x0040 /* Sort using a sorter */ |
| 10727 | 10715 | |
| 10728 | 10716 | |
| 10729 | 10717 | /* |
| 10730 | 10718 | ** The results of a select can be distributed in several ways. The |
| 10731 | 10719 | ** "SRT" prefix means "SELECT Result Type". |
| | @@ -11361,11 +11349,10 @@ |
| 11361 | 11349 | #else |
| 11362 | 11350 | # define sqlite3ViewGetColumnNames(A,B) 0 |
| 11363 | 11351 | #endif |
| 11364 | 11352 | |
| 11365 | 11353 | SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int); |
| 11366 | | -SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int); |
| 11367 | 11354 | SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*); |
| 11368 | 11355 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 11369 | 11356 | SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse); |
| 11370 | 11357 | SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse); |
| 11371 | 11358 | #else |
| | @@ -11618,11 +11605,11 @@ |
| 11618 | 11605 | SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, |
| 11619 | 11606 | void(*)(void*)); |
| 11620 | 11607 | SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*); |
| 11621 | 11608 | SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *); |
| 11622 | 11609 | SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8); |
| 11623 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 11610 | +#ifdef SQLITE_ENABLE_STAT2 |
| 11624 | 11611 | SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *); |
| 11625 | 11612 | #endif |
| 11626 | 11613 | SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **); |
| 11627 | 11614 | SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8); |
| 11628 | 11615 | #ifndef SQLITE_AMALGAMATION |
| | @@ -12245,13 +12232,10 @@ |
| 12245 | 12232 | "ENABLE_RTREE", |
| 12246 | 12233 | #endif |
| 12247 | 12234 | #ifdef SQLITE_ENABLE_STAT2 |
| 12248 | 12235 | "ENABLE_STAT2", |
| 12249 | 12236 | #endif |
| 12250 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 12251 | | - "ENABLE_STAT3", |
| 12252 | | -#endif |
| 12253 | 12237 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 12254 | 12238 | "ENABLE_UNLOCK_NOTIFY", |
| 12255 | 12239 | #endif |
| 12256 | 12240 | #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT |
| 12257 | 12241 | "ENABLE_UPDATE_DELETE_LIMIT", |
| | @@ -12275,10 +12259,13 @@ |
| 12275 | 12259 | "INT64_TYPE", |
| 12276 | 12260 | #endif |
| 12277 | 12261 | #ifdef SQLITE_LOCK_TRACE |
| 12278 | 12262 | "LOCK_TRACE", |
| 12279 | 12263 | #endif |
| 12264 | +#ifdef SQLITE_MAX_SCHEMA_RETRY |
| 12265 | + "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY), |
| 12266 | +#endif |
| 12280 | 12267 | #ifdef SQLITE_MEMDEBUG |
| 12281 | 12268 | "MEMDEBUG", |
| 12282 | 12269 | #endif |
| 12283 | 12270 | #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT |
| 12284 | 12271 | "MIXED_ENDIAN_64BIT_FLOAT", |
| | @@ -12387,10 +12374,13 @@ |
| 12387 | 12374 | #ifdef SQLITE_OMIT_LOOKASIDE |
| 12388 | 12375 | "OMIT_LOOKASIDE", |
| 12389 | 12376 | #endif |
| 12390 | 12377 | #ifdef SQLITE_OMIT_MEMORYDB |
| 12391 | 12378 | "OMIT_MEMORYDB", |
| 12379 | +#endif |
| 12380 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 12381 | + "OMIT_MERGE_SORT", |
| 12392 | 12382 | #endif |
| 12393 | 12383 | #ifdef SQLITE_OMIT_OR_OPTIMIZATION |
| 12394 | 12384 | "OMIT_OR_OPTIMIZATION", |
| 12395 | 12385 | #endif |
| 12396 | 12386 | #ifdef SQLITE_OMIT_PAGER_PRAGMAS |
| | @@ -12453,10 +12443,13 @@ |
| 12453 | 12443 | #ifdef SQLITE_OMIT_WSD |
| 12454 | 12444 | "OMIT_WSD", |
| 12455 | 12445 | #endif |
| 12456 | 12446 | #ifdef SQLITE_OMIT_XFER_OPT |
| 12457 | 12447 | "OMIT_XFER_OPT", |
| 12448 | +#endif |
| 12449 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 12450 | + "PAGECACHE_BLOCKALLOC", |
| 12458 | 12451 | #endif |
| 12459 | 12452 | #ifdef SQLITE_PERFORMANCE_TRACE |
| 12460 | 12453 | "PERFORMANCE_TRACE", |
| 12461 | 12454 | #endif |
| 12462 | 12455 | #ifdef SQLITE_PROXY_DEBUG |
| | @@ -12574,10 +12567,13 @@ |
| 12574 | 12567 | /* |
| 12575 | 12568 | ** Boolean values |
| 12576 | 12569 | */ |
| 12577 | 12570 | typedef unsigned char Bool; |
| 12578 | 12571 | |
| 12572 | +/* Opaque type used by code in vdbesort.c */ |
| 12573 | +typedef struct VdbeSorter VdbeSorter; |
| 12574 | + |
| 12579 | 12575 | /* |
| 12580 | 12576 | ** A cursor is a pointer into a single BTree within a database file. |
| 12581 | 12577 | ** The cursor can seek to a BTree entry with a particular key, or |
| 12582 | 12578 | ** loop over all entries of the Btree. You can also insert new BTree |
| 12583 | 12579 | ** entries or retrieve the key or data from the entry that the cursor |
| | @@ -12600,15 +12596,17 @@ |
| 12600 | 12596 | Bool nullRow; /* True if pointing to a row with no data */ |
| 12601 | 12597 | Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ |
| 12602 | 12598 | Bool isTable; /* True if a table requiring integer keys */ |
| 12603 | 12599 | Bool isIndex; /* True if an index containing keys only - no data */ |
| 12604 | 12600 | Bool isOrdered; /* True if the underlying table is BTREE_UNORDERED */ |
| 12601 | + Bool isSorter; /* True if a new-style sorter */ |
| 12605 | 12602 | sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ |
| 12606 | 12603 | const sqlite3_module *pModule; /* Module for cursor pVtabCursor */ |
| 12607 | 12604 | i64 seqCount; /* Sequence counter */ |
| 12608 | 12605 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 12609 | 12606 | i64 lastRowid; /* Last rowid from a Next or NextIdx operation */ |
| 12607 | + VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */ |
| 12610 | 12608 | |
| 12611 | 12609 | /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or |
| 12612 | 12610 | ** OP_IsUnique opcode on this cursor. */ |
| 12613 | 12611 | int seekResult; |
| 12614 | 12612 | |
| | @@ -12924,17 +12922,38 @@ |
| 12924 | 12922 | SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*); |
| 12925 | 12923 | SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); |
| 12926 | 12924 | SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*); |
| 12927 | 12925 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); |
| 12928 | 12926 | SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p); |
| 12927 | +#define MemReleaseExt(X) \ |
| 12928 | + if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \ |
| 12929 | + sqlite3VdbeMemReleaseExternal(X); |
| 12929 | 12930 | SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); |
| 12930 | 12931 | SQLITE_PRIVATE const char *sqlite3OpcodeName(int); |
| 12931 | 12932 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 12932 | 12933 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 12933 | 12934 | SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); |
| 12934 | 12935 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); |
| 12935 | 12936 | SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem); |
| 12937 | + |
| 12938 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 12939 | +# define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK |
| 12940 | +# define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK |
| 12941 | +# define sqlite3VdbeSorterClose(Y,Z) |
| 12942 | +# define sqlite3VdbeSorterRowkey(Y,Z) SQLITE_OK |
| 12943 | +# define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK |
| 12944 | +# define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK |
| 12945 | +# define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK |
| 12946 | +#else |
| 12947 | +SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *); |
| 12948 | +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *); |
| 12949 | +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *); |
| 12950 | +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *); |
| 12951 | +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *); |
| 12952 | +SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *); |
| 12953 | +SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *); |
| 12954 | +#endif |
| 12936 | 12955 | |
| 12937 | 12956 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 |
| 12938 | 12957 | SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*); |
| 12939 | 12958 | SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*); |
| 12940 | 12959 | #else |
| | @@ -13225,10 +13244,12 @@ |
| 13225 | 13244 | ** Astronomical Algorithms, 2nd Edition, 1998 |
| 13226 | 13245 | ** ISBM 0-943396-61-1 |
| 13227 | 13246 | ** Willmann-Bell, Inc |
| 13228 | 13247 | ** Richmond, Virginia (USA) |
| 13229 | 13248 | */ |
| 13249 | +/* #include <stdlib.h> */ |
| 13250 | +/* #include <assert.h> */ |
| 13230 | 13251 | #include <time.h> |
| 13231 | 13252 | |
| 13232 | 13253 | #ifndef SQLITE_OMIT_DATETIME_FUNCS |
| 13233 | 13254 | |
| 13234 | 13255 | |
| | @@ -14978,10 +14999,11 @@ |
| 14978 | 14999 | extern void backtrace_symbols_fd(void*const*,int,int); |
| 14979 | 15000 | #else |
| 14980 | 15001 | # define backtrace(A,B) 1 |
| 14981 | 15002 | # define backtrace_symbols_fd(A,B,C) |
| 14982 | 15003 | #endif |
| 15004 | +/* #include <stdio.h> */ |
| 14983 | 15005 | |
| 14984 | 15006 | /* |
| 14985 | 15007 | ** Each memory allocation looks like this: |
| 14986 | 15008 | ** |
| 14987 | 15009 | ** ------------------------------------------------------------------------ |
| | @@ -18081,10 +18103,11 @@ |
| 18081 | 18103 | ** |
| 18082 | 18104 | ************************************************************************* |
| 18083 | 18105 | ** |
| 18084 | 18106 | ** Memory allocation functions used throughout sqlite. |
| 18085 | 18107 | */ |
| 18108 | +/* #include <stdarg.h> */ |
| 18086 | 18109 | |
| 18087 | 18110 | /* |
| 18088 | 18111 | ** Attempt to release up to n bytes of non-essential memory currently |
| 18089 | 18112 | ** held by SQLite. An example of non-essential memory is memory used to |
| 18090 | 18113 | ** cache database pages that are not currently in use. |
| | @@ -20058,10 +20081,11 @@ |
| 20058 | 20081 | ** BOM or Byte Order Mark: |
| 20059 | 20082 | ** 0xff 0xfe little-endian utf-16 follows |
| 20060 | 20083 | ** 0xfe 0xff big-endian utf-16 follows |
| 20061 | 20084 | ** |
| 20062 | 20085 | */ |
| 20086 | +/* #include <assert.h> */ |
| 20063 | 20087 | |
| 20064 | 20088 | #ifndef SQLITE_AMALGAMATION |
| 20065 | 20089 | /* |
| 20066 | 20090 | ** The following constant value is used by the SQLITE_BIGENDIAN and |
| 20067 | 20091 | ** SQLITE_LITTLEENDIAN macros. |
| | @@ -20486,11 +20510,11 @@ |
| 20486 | 20510 | ** no longer required. |
| 20487 | 20511 | ** |
| 20488 | 20512 | ** If a malloc failure occurs, NULL is returned and the db.mallocFailed |
| 20489 | 20513 | ** flag set. |
| 20490 | 20514 | */ |
| 20491 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 20515 | +#ifdef SQLITE_ENABLE_STAT2 |
| 20492 | 20516 | SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){ |
| 20493 | 20517 | Mem m; |
| 20494 | 20518 | memset(&m, 0, sizeof(m)); |
| 20495 | 20519 | m.db = db; |
| 20496 | 20520 | sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC); |
| | @@ -20600,10 +20624,11 @@ |
| 20600 | 20624 | ** |
| 20601 | 20625 | ** This file contains functions for allocating memory, comparing |
| 20602 | 20626 | ** strings, and stuff like that. |
| 20603 | 20627 | ** |
| 20604 | 20628 | */ |
| 20629 | +/* #include <stdarg.h> */ |
| 20605 | 20630 | #ifdef SQLITE_HAVE_ISNAN |
| 20606 | 20631 | # include <math.h> |
| 20607 | 20632 | #endif |
| 20608 | 20633 | |
| 20609 | 20634 | /* |
| | @@ -21778,10 +21803,11 @@ |
| 21778 | 21803 | ** |
| 21779 | 21804 | ************************************************************************* |
| 21780 | 21805 | ** This is the implementation of generic hash-tables |
| 21781 | 21806 | ** used in SQLite. |
| 21782 | 21807 | */ |
| 21808 | +/* #include <assert.h> */ |
| 21783 | 21809 | |
| 21784 | 21810 | /* Turn bulk memory into a hash table object by initializing the |
| 21785 | 21811 | ** fields of the Hash structure. |
| 21786 | 21812 | ** |
| 21787 | 21813 | ** "pNew" is a pointer to the hash table that is to be initialized. |
| | @@ -22088,50 +22114,50 @@ |
| 22088 | 22114 | /* 37 */ "VerifyCookie", |
| 22089 | 22115 | /* 38 */ "OpenRead", |
| 22090 | 22116 | /* 39 */ "OpenWrite", |
| 22091 | 22117 | /* 40 */ "OpenAutoindex", |
| 22092 | 22118 | /* 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 | + /* 42 */ "SorterOpen", |
| 22120 | + /* 43 */ "OpenPseudo", |
| 22121 | + /* 44 */ "Close", |
| 22122 | + /* 45 */ "SeekLt", |
| 22123 | + /* 46 */ "SeekLe", |
| 22124 | + /* 47 */ "SeekGe", |
| 22125 | + /* 48 */ "SeekGt", |
| 22126 | + /* 49 */ "Seek", |
| 22127 | + /* 50 */ "NotFound", |
| 22128 | + /* 51 */ "Found", |
| 22129 | + /* 52 */ "IsUnique", |
| 22130 | + /* 53 */ "NotExists", |
| 22131 | + /* 54 */ "Sequence", |
| 22132 | + /* 55 */ "NewRowid", |
| 22133 | + /* 56 */ "Insert", |
| 22134 | + /* 57 */ "InsertInt", |
| 22135 | + /* 58 */ "Delete", |
| 22136 | + /* 59 */ "ResetCount", |
| 22137 | + /* 60 */ "SorterCompare", |
| 22138 | + /* 61 */ "SorterData", |
| 22139 | + /* 62 */ "RowKey", |
| 22140 | + /* 63 */ "RowData", |
| 22141 | + /* 64 */ "Rowid", |
| 22142 | + /* 65 */ "NullRow", |
| 22143 | + /* 66 */ "Last", |
| 22144 | + /* 67 */ "SorterSort", |
| 22119 | 22145 | /* 68 */ "Or", |
| 22120 | 22146 | /* 69 */ "And", |
| 22121 | | - /* 70 */ "IdxInsert", |
| 22122 | | - /* 71 */ "IdxDelete", |
| 22123 | | - /* 72 */ "IdxRowid", |
| 22147 | + /* 70 */ "Sort", |
| 22148 | + /* 71 */ "Rewind", |
| 22149 | + /* 72 */ "SorterNext", |
| 22124 | 22150 | /* 73 */ "IsNull", |
| 22125 | 22151 | /* 74 */ "NotNull", |
| 22126 | 22152 | /* 75 */ "Ne", |
| 22127 | 22153 | /* 76 */ "Eq", |
| 22128 | 22154 | /* 77 */ "Gt", |
| 22129 | 22155 | /* 78 */ "Le", |
| 22130 | 22156 | /* 79 */ "Lt", |
| 22131 | 22157 | /* 80 */ "Ge", |
| 22132 | | - /* 81 */ "IdxLT", |
| 22158 | + /* 81 */ "Prev", |
| 22133 | 22159 | /* 82 */ "BitAnd", |
| 22134 | 22160 | /* 83 */ "BitOr", |
| 22135 | 22161 | /* 84 */ "ShiftLeft", |
| 22136 | 22162 | /* 85 */ "ShiftRight", |
| 22137 | 22163 | /* 86 */ "Add", |
| | @@ -22138,64 +22164,68 @@ |
| 22138 | 22164 | /* 87 */ "Subtract", |
| 22139 | 22165 | /* 88 */ "Multiply", |
| 22140 | 22166 | /* 89 */ "Divide", |
| 22141 | 22167 | /* 90 */ "Remainder", |
| 22142 | 22168 | /* 91 */ "Concat", |
| 22143 | | - /* 92 */ "IdxGE", |
| 22169 | + /* 92 */ "Next", |
| 22144 | 22170 | /* 93 */ "BitNot", |
| 22145 | 22171 | /* 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", |
| 22172 | + /* 95 */ "SorterInsert", |
| 22173 | + /* 96 */ "IdxInsert", |
| 22174 | + /* 97 */ "IdxDelete", |
| 22175 | + /* 98 */ "IdxRowid", |
| 22176 | + /* 99 */ "IdxLT", |
| 22177 | + /* 100 */ "IdxGE", |
| 22178 | + /* 101 */ "Destroy", |
| 22179 | + /* 102 */ "Clear", |
| 22180 | + /* 103 */ "CreateIndex", |
| 22181 | + /* 104 */ "CreateTable", |
| 22182 | + /* 105 */ "ParseSchema", |
| 22183 | + /* 106 */ "LoadAnalysis", |
| 22184 | + /* 107 */ "DropTable", |
| 22185 | + /* 108 */ "DropIndex", |
| 22186 | + /* 109 */ "DropTrigger", |
| 22187 | + /* 110 */ "IntegrityCk", |
| 22188 | + /* 111 */ "RowSetAdd", |
| 22189 | + /* 112 */ "RowSetRead", |
| 22190 | + /* 113 */ "RowSetTest", |
| 22191 | + /* 114 */ "Program", |
| 22192 | + /* 115 */ "Param", |
| 22193 | + /* 116 */ "FkCounter", |
| 22194 | + /* 117 */ "FkIfZero", |
| 22195 | + /* 118 */ "MemMax", |
| 22196 | + /* 119 */ "IfPos", |
| 22197 | + /* 120 */ "IfNeg", |
| 22198 | + /* 121 */ "IfZero", |
| 22199 | + /* 122 */ "AggStep", |
| 22200 | + /* 123 */ "AggFinal", |
| 22201 | + /* 124 */ "Checkpoint", |
| 22202 | + /* 125 */ "JournalMode", |
| 22203 | + /* 126 */ "Vacuum", |
| 22204 | + /* 127 */ "IncrVacuum", |
| 22205 | + /* 128 */ "Expire", |
| 22206 | + /* 129 */ "TableLock", |
| 22181 | 22207 | /* 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", |
| 22191 | | - /* 140 */ "NotUsed_140", |
| 22208 | + /* 131 */ "VBegin", |
| 22209 | + /* 132 */ "VCreate", |
| 22210 | + /* 133 */ "VDestroy", |
| 22211 | + /* 134 */ "VOpen", |
| 22212 | + /* 135 */ "VFilter", |
| 22213 | + /* 136 */ "VColumn", |
| 22214 | + /* 137 */ "VNext", |
| 22215 | + /* 138 */ "VRename", |
| 22216 | + /* 139 */ "VUpdate", |
| 22217 | + /* 140 */ "Pagecount", |
| 22192 | 22218 | /* 141 */ "ToText", |
| 22193 | 22219 | /* 142 */ "ToBlob", |
| 22194 | 22220 | /* 143 */ "ToNumeric", |
| 22195 | 22221 | /* 144 */ "ToInt", |
| 22196 | 22222 | /* 145 */ "ToReal", |
| 22223 | + /* 146 */ "MaxPgcnt", |
| 22224 | + /* 147 */ "Trace", |
| 22225 | + /* 148 */ "Noop", |
| 22226 | + /* 149 */ "Explain", |
| 22197 | 22227 | }; |
| 22198 | 22228 | return azName[i]; |
| 22199 | 22229 | } |
| 22200 | 22230 | #endif |
| 22201 | 22231 | |
| | @@ -22286,11 +22316,11 @@ |
| 22286 | 22316 | */ |
| 22287 | 22317 | #ifdef MEMORY_DEBUG |
| 22288 | 22318 | # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 22289 | 22319 | #endif |
| 22290 | 22320 | |
| 22291 | | -#ifdef SQLITE_DEBUG |
| 22321 | +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 22292 | 22322 | # ifndef SQLITE_DEBUG_OS_TRACE |
| 22293 | 22323 | # define SQLITE_DEBUG_OS_TRACE 0 |
| 22294 | 22324 | # endif |
| 22295 | 22325 | int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; |
| 22296 | 22326 | # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X |
| | @@ -24450,10 +24480,11 @@ |
| 24450 | 24480 | */ |
| 24451 | 24481 | #include <sys/types.h> |
| 24452 | 24482 | #include <sys/stat.h> |
| 24453 | 24483 | #include <fcntl.h> |
| 24454 | 24484 | #include <unistd.h> |
| 24485 | +/* #include <time.h> */ |
| 24455 | 24486 | #include <sys/time.h> |
| 24456 | 24487 | #include <errno.h> |
| 24457 | 24488 | #ifndef SQLITE_OMIT_WAL |
| 24458 | 24489 | #include <sys/mman.h> |
| 24459 | 24490 | #endif |
| | @@ -24485,10 +24516,11 @@ |
| 24485 | 24516 | /* |
| 24486 | 24517 | ** If we are to be thread-safe, include the pthreads header and define |
| 24487 | 24518 | ** the SQLITE_UNIX_THREADS macro. |
| 24488 | 24519 | */ |
| 24489 | 24520 | #if SQLITE_THREADSAFE |
| 24521 | +/* # include <pthread.h> */ |
| 24490 | 24522 | # define SQLITE_UNIX_THREADS 1 |
| 24491 | 24523 | #endif |
| 24492 | 24524 | |
| 24493 | 24525 | /* |
| 24494 | 24526 | ** Default permissions when creating a new file |
| | @@ -24584,11 +24616,15 @@ |
| 24584 | 24616 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 24585 | 24617 | */ |
| 24586 | 24618 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 24587 | 24619 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 24588 | 24620 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
| 24589 | | -#define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 24621 | +#ifndef SQLITE_DISABLE_DIRSYNC |
| 24622 | +# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 24623 | +#else |
| 24624 | +# define UNIXFILE_DIRSYNC 0x00 |
| 24625 | +#endif |
| 24590 | 24626 | |
| 24591 | 24627 | /* |
| 24592 | 24628 | ** Include code that is common to all os_*.c files |
| 24593 | 24629 | */ |
| 24594 | 24630 | /************** Include os_common.h in the middle of os_unix.c ***************/ |
| | @@ -24622,11 +24658,11 @@ |
| 24622 | 24658 | */ |
| 24623 | 24659 | #ifdef MEMORY_DEBUG |
| 24624 | 24660 | # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 24625 | 24661 | #endif |
| 24626 | 24662 | |
| 24627 | | -#ifdef SQLITE_DEBUG |
| 24663 | +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 24628 | 24664 | # ifndef SQLITE_DEBUG_OS_TRACE |
| 24629 | 24665 | # define SQLITE_DEBUG_OS_TRACE 0 |
| 24630 | 24666 | # endif |
| 24631 | 24667 | int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; |
| 24632 | 24668 | # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X |
| | @@ -27061,15 +27097,16 @@ |
| 27061 | 27097 | */ |
| 27062 | 27098 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 27063 | 27099 | int rc = SQLITE_OK; |
| 27064 | 27100 | int reserved = 0; |
| 27065 | 27101 | unixFile *pFile = (unixFile*)id; |
| 27102 | + afpLockingContext *context; |
| 27066 | 27103 | |
| 27067 | 27104 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 27068 | 27105 | |
| 27069 | 27106 | assert( pFile ); |
| 27070 | | - afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 27107 | + context = (afpLockingContext *) pFile->lockingContext; |
| 27071 | 27108 | if( context->reserved ){ |
| 27072 | 27109 | *pResOut = 1; |
| 27073 | 27110 | return SQLITE_OK; |
| 27074 | 27111 | } |
| 27075 | 27112 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
| | @@ -27205,11 +27242,11 @@ |
| 27205 | 27242 | |
| 27206 | 27243 | /* If control gets to this point, then actually go ahead and make |
| 27207 | 27244 | ** operating system calls for the specified lock. |
| 27208 | 27245 | */ |
| 27209 | 27246 | if( eFileLock==SHARED_LOCK ){ |
| 27210 | | - int lrc1, lrc2, lrc1Errno; |
| 27247 | + int lrc1, lrc2, lrc1Errno = 0; |
| 27211 | 27248 | long lk, mask; |
| 27212 | 27249 | |
| 27213 | 27250 | assert( pInode->nShared==0 ); |
| 27214 | 27251 | assert( pInode->eFileLock==0 ); |
| 27215 | 27252 | |
| | @@ -27579,21 +27616,23 @@ |
| 27579 | 27616 | #if defined(USE_PREAD) |
| 27580 | 27617 | do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
| 27581 | 27618 | #elif defined(USE_PREAD64) |
| 27582 | 27619 | do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR); |
| 27583 | 27620 | #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 ); |
| 27621 | + do{ |
| 27622 | + newOffset = lseek(id->h, offset, SEEK_SET); |
| 27623 | + SimulateIOError( newOffset-- ); |
| 27624 | + if( newOffset!=offset ){ |
| 27625 | + if( newOffset == -1 ){ |
| 27626 | + ((unixFile*)id)->lastErrno = errno; |
| 27627 | + }else{ |
| 27628 | + ((unixFile*)id)->lastErrno = 0; |
| 27629 | + } |
| 27630 | + return -1; |
| 27631 | + } |
| 27632 | + got = osWrite(id->h, pBuf, cnt); |
| 27633 | + }while( got<0 && errno==EINTR ); |
| 27595 | 27634 | #endif |
| 27596 | 27635 | TIMER_END; |
| 27597 | 27636 | if( got<0 ){ |
| 27598 | 27637 | ((unixFile*)id)->lastErrno = errno; |
| 27599 | 27638 | } |
| | @@ -27679,15 +27718,15 @@ |
| 27679 | 27718 | SQLITE_API int sqlite3_fullsync_count = 0; |
| 27680 | 27719 | #endif |
| 27681 | 27720 | |
| 27682 | 27721 | /* |
| 27683 | 27722 | ** We do not trust systems to provide a working fdatasync(). Some do. |
| 27684 | | -** Others do no. To be safe, we will stick with the (slower) fsync(). |
| 27685 | | -** If you know that your system does support fdatasync() correctly, |
| 27723 | +** Others do no. To be safe, we will stick with the (slightly slower) |
| 27724 | +** fsync(). If you know that your system does support fdatasync() correctly, |
| 27686 | 27725 | ** then simply compile with -Dfdatasync=fdatasync |
| 27687 | 27726 | */ |
| 27688 | | -#if !defined(fdatasync) && !defined(__linux__) |
| 27727 | +#if !defined(fdatasync) |
| 27689 | 27728 | # define fdatasync fsync |
| 27690 | 27729 | #endif |
| 27691 | 27730 | |
| 27692 | 27731 | /* |
| 27693 | 27732 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| | @@ -27888,10 +27927,12 @@ |
| 27888 | 27927 | HAVE_FULLFSYNC, isFullsync)); |
| 27889 | 27928 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 27890 | 27929 | if( rc==SQLITE_OK && dirfd>=0 ){ |
| 27891 | 27930 | full_fsync(dirfd, 0, 0); |
| 27892 | 27931 | robust_close(pFile, dirfd, __LINE__); |
| 27932 | + }else if( rc==SQLITE_CANTOPEN ){ |
| 27933 | + rc = SQLITE_OK; |
| 27893 | 27934 | } |
| 27894 | 27935 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
| 27895 | 27936 | } |
| 27896 | 27937 | return rc; |
| 27897 | 27938 | } |
| | @@ -27971,30 +28012,22 @@ |
| 27971 | 28012 | static int proxyFileControl(sqlite3_file*,int,void*); |
| 27972 | 28013 | #endif |
| 27973 | 28014 | |
| 27974 | 28015 | /* |
| 27975 | 28016 | ** 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. |
| 28017 | +** file-control operation. Enlarge the database to nBytes in size |
| 28018 | +** (rounded up to the next chunk-size). If the database is already |
| 28019 | +** nBytes or larger, this routine is a no-op. |
| 27981 | 28020 | */ |
| 27982 | 28021 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
| 27983 | | - { /* preserve indentation of removed "if" */ |
| 28022 | + if( pFile->szChunk>0 ){ |
| 27984 | 28023 | i64 nSize; /* Required file size */ |
| 27985 | | - i64 szChunk; /* Chunk size */ |
| 27986 | 28024 | struct stat buf; /* Used to hold return values of fstat() */ |
| 27987 | 28025 | |
| 27988 | 28026 | if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
| 27989 | 28027 | |
| 27990 | | - szChunk = pFile->szChunk; |
| 27991 | | - if( szChunk==0 ){ |
| 27992 | | - nSize = nByte; |
| 27993 | | - }else{ |
| 27994 | | - nSize = ((nByte+szChunk-1) / szChunk) * szChunk; |
| 27995 | | - } |
| 28028 | + nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 27996 | 28029 | if( nSize>(i64)buf.st_size ){ |
| 27997 | 28030 | |
| 27998 | 28031 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
| 27999 | 28032 | /* The code below is handling the return value of osFallocate() |
| 28000 | 28033 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| | @@ -28048,11 +28081,15 @@ |
| 28048 | 28081 | case SQLITE_FCNTL_CHUNK_SIZE: { |
| 28049 | 28082 | pFile->szChunk = *(int *)pArg; |
| 28050 | 28083 | return SQLITE_OK; |
| 28051 | 28084 | } |
| 28052 | 28085 | case SQLITE_FCNTL_SIZE_HINT: { |
| 28053 | | - return fcntlSizeHint(pFile, *(i64 *)pArg); |
| 28086 | + int rc; |
| 28087 | + SimulateIOErrorBenign(1); |
| 28088 | + rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 28089 | + SimulateIOErrorBenign(0); |
| 28090 | + return rc; |
| 28054 | 28091 | } |
| 28055 | 28092 | case SQLITE_FCNTL_PERSIST_WAL: { |
| 28056 | 28093 | int bPersist = *(int*)pArg; |
| 28057 | 28094 | if( bPersist<0 ){ |
| 28058 | 28095 | *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0; |
| | @@ -28175,15 +28212,13 @@ |
| 28175 | 28212 | */ |
| 28176 | 28213 | struct unixShm { |
| 28177 | 28214 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 28178 | 28215 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
| 28179 | 28216 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
| 28217 | + u8 id; /* Id of this connection within its unixShmNode */ |
| 28180 | 28218 | u16 sharedMask; /* Mask of shared locks held */ |
| 28181 | 28219 | u16 exclMask; /* Mask of exclusive locks held */ |
| 28182 | | -#ifdef SQLITE_DEBUG |
| 28183 | | - u8 id; /* Id of this connection within its unixShmNode */ |
| 28184 | | -#endif |
| 28185 | 28220 | }; |
| 28186 | 28221 | |
| 28187 | 28222 | /* |
| 28188 | 28223 | ** Constants used for locking |
| 28189 | 28224 | */ |
| | @@ -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{ |
| | @@ -31393,11 +31435,11 @@ |
| 31393 | 31435 | */ |
| 31394 | 31436 | #ifdef MEMORY_DEBUG |
| 31395 | 31437 | # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 31396 | 31438 | #endif |
| 31397 | 31439 | |
| 31398 | | -#ifdef SQLITE_DEBUG |
| 31440 | +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 31399 | 31441 | # ifndef SQLITE_DEBUG_OS_TRACE |
| 31400 | 31442 | # define SQLITE_DEBUG_OS_TRACE 0 |
| 31401 | 31443 | # endif |
| 31402 | 31444 | int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; |
| 31403 | 31445 | # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X |
| | @@ -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; |
| | @@ -32473,11 +32768,11 @@ |
| 32473 | 32768 | /* If the user has configured a chunk-size for this file, truncate the |
| 32474 | 32769 | ** file so that it consists of an integer number of chunks (i.e. the |
| 32475 | 32770 | ** actual file size after the operation may be larger than the requested |
| 32476 | 32771 | ** size). |
| 32477 | 32772 | */ |
| 32478 | | - if( pFile->szChunk ){ |
| 32773 | + if( pFile->szChunk>0 ){ |
| 32479 | 32774 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 32480 | 32775 | } |
| 32481 | 32776 | |
| 32482 | 32777 | /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */ |
| 32483 | 32778 | if( seekWinFile(pFile, nByte) ){ |
| | @@ -32860,14 +33155,23 @@ |
| 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); |
| 33160 | + if( pFile->szChunk>0 ){ |
| 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; |
| 33172 | + } |
| 32869 | 33173 | return SQLITE_OK; |
| 32870 | 33174 | } |
| 32871 | 33175 | case SQLITE_FCNTL_PERSIST_WAL: { |
| 32872 | 33176 | int bPersist = *(int*)pArg; |
| 32873 | 33177 | if( bPersist<0 ){ |
| | @@ -35473,10 +35777,13 @@ |
| 35473 | 35777 | typedef struct PCache1 PCache1; |
| 35474 | 35778 | typedef struct PgHdr1 PgHdr1; |
| 35475 | 35779 | typedef struct PgFreeslot PgFreeslot; |
| 35476 | 35780 | typedef struct PGroup PGroup; |
| 35477 | 35781 | |
| 35782 | +typedef struct PGroupBlock PGroupBlock; |
| 35783 | +typedef struct PGroupBlockList PGroupBlockList; |
| 35784 | + |
| 35478 | 35785 | /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set |
| 35479 | 35786 | ** of one or more PCaches that are able to recycle each others unpinned |
| 35480 | 35787 | ** pages when they are under memory pressure. A PGroup is an instance of |
| 35481 | 35788 | ** the following object. |
| 35482 | 35789 | ** |
| | @@ -35502,12 +35809,70 @@ |
| 35502 | 35809 | int nMaxPage; /* Sum of nMax for purgeable caches */ |
| 35503 | 35810 | int nMinPage; /* Sum of nMin for purgeable caches */ |
| 35504 | 35811 | int mxPinned; /* nMaxpage + 10 - nMinPage */ |
| 35505 | 35812 | int nCurrentPage; /* Number of purgeable pages allocated */ |
| 35506 | 35813 | PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */ |
| 35814 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 35815 | + int isBusy; /* Do not run ReleaseMemory() if true */ |
| 35816 | + PGroupBlockList *pBlockList; /* List of block-lists for this group */ |
| 35817 | +#endif |
| 35507 | 35818 | }; |
| 35508 | 35819 | |
| 35820 | +/* |
| 35821 | +** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built, |
| 35822 | +** each PGroup structure has a linked list of the the following starting |
| 35823 | +** at PGroup.pBlockList. There is one entry for each distinct page-size |
| 35824 | +** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes |
| 35825 | +** etc.). Variable PGroupBlockList.nByte is set to the actual allocation |
| 35826 | +** size requested by each pcache, which is the database page-size plus |
| 35827 | +** the various header structures used by the pcache, pager and btree layers. |
| 35828 | +** Usually around (pgsz+200) bytes. |
| 35829 | +** |
| 35830 | +** This size (pgsz+200) bytes is not allocated efficiently by some |
| 35831 | +** implementations of malloc. In particular, some implementations are only |
| 35832 | +** able to allocate blocks of memory chunks of 2^N bytes, where N is some |
| 35833 | +** integer value. Since the page-size is a power of 2, this means we |
| 35834 | +** end up wasting (pgsz-200) bytes in each allocation. |
| 35835 | +** |
| 35836 | +** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks |
| 35837 | +** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes |
| 35838 | +** are requested from malloc allocator. After a block is returned, |
| 35839 | +** sqlite3MallocSize() is used to determine how many (pgsz+200) byte |
| 35840 | +** allocations can fit in the space returned by malloc(). This value may |
| 35841 | +** be more than M. |
| 35842 | +** |
| 35843 | +** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry |
| 35844 | +** contains the number of allocations that will fit in the aData[] space. |
| 35845 | +** nEntry is limited to the number of bits in bitmask mUsed. If a slot |
| 35846 | +** within aData is in use, the corresponding bit in mUsed is set. Thus |
| 35847 | +** when (mUsed+1==(1 << nEntry)) the block is completely full. |
| 35848 | +** |
| 35849 | +** Each time a slot within a block is freed, the block is moved to the start |
| 35850 | +** of the linked-list. And if a block becomes completely full, then it is |
| 35851 | +** moved to the end of the list. As a result, when searching for a free |
| 35852 | +** slot, only the first block in the list need be examined. If it is full, |
| 35853 | +** then it is guaranteed that all blocks are full. |
| 35854 | +*/ |
| 35855 | +struct PGroupBlockList { |
| 35856 | + int nByte; /* Size of each allocation in bytes */ |
| 35857 | + PGroupBlock *pFirst; /* First PGroupBlock in list */ |
| 35858 | + PGroupBlock *pLast; /* Last PGroupBlock in list */ |
| 35859 | + PGroupBlockList *pNext; /* Next block-list attached to group */ |
| 35860 | +}; |
| 35861 | + |
| 35862 | +struct PGroupBlock { |
| 35863 | + Bitmask mUsed; /* Mask of used slots */ |
| 35864 | + int nEntry; /* Maximum number of allocations in aData[] */ |
| 35865 | + u8 *aData; /* Pointer to data block */ |
| 35866 | + PGroupBlock *pNext; /* Next PGroupBlock in list */ |
| 35867 | + PGroupBlock *pPrev; /* Previous PGroupBlock in list */ |
| 35868 | + PGroupBlockList *pList; /* Owner list */ |
| 35869 | +}; |
| 35870 | + |
| 35871 | +/* Minimum value for PGroupBlock.nEntry */ |
| 35872 | +#define PAGECACHE_BLOCKALLOC_MINENTRY 15 |
| 35873 | + |
| 35509 | 35874 | /* Each page cache is an instance of the following object. Every |
| 35510 | 35875 | ** open database file (including each in-memory database and each |
| 35511 | 35876 | ** temporary or transient database) has a single page cache which |
| 35512 | 35877 | ** is an instance of this object. |
| 35513 | 35878 | ** |
| | @@ -35606,10 +35971,21 @@ |
| 35606 | 35971 | ** |
| 35607 | 35972 | ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X ); |
| 35608 | 35973 | */ |
| 35609 | 35974 | #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage) |
| 35610 | 35975 | #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage) |
| 35976 | + |
| 35977 | +/* |
| 35978 | +** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve |
| 35979 | +** a PGroupBlock pointer based on a pointer to a page buffer. |
| 35980 | +*/ |
| 35981 | +#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \ |
| 35982 | + ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock ) |
| 35983 | + |
| 35984 | +#define PAGE_GET_BLOCKPTR(pCache, pPg) \ |
| 35985 | + ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) ) |
| 35986 | + |
| 35611 | 35987 | |
| 35612 | 35988 | /* |
| 35613 | 35989 | ** Macros to enter and leave the PCache LRU mutex. |
| 35614 | 35990 | */ |
| 35615 | 35991 | #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) |
| | @@ -35732,25 +36108,159 @@ |
| 35732 | 36108 | return iSize; |
| 35733 | 36109 | } |
| 35734 | 36110 | } |
| 35735 | 36111 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 35736 | 36112 | |
| 36113 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36114 | +/* |
| 36115 | +** The block pBlock belongs to list pList but is not currently linked in. |
| 36116 | +** Insert it into the start of the list. |
| 36117 | +*/ |
| 36118 | +static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){ |
| 36119 | + pBlock->pPrev = 0; |
| 36120 | + pBlock->pNext = pList->pFirst; |
| 36121 | + pList->pFirst = pBlock; |
| 36122 | + if( pBlock->pNext ){ |
| 36123 | + pBlock->pNext->pPrev = pBlock; |
| 36124 | + }else{ |
| 36125 | + assert( pList->pLast==0 ); |
| 36126 | + pList->pLast = pBlock; |
| 36127 | + } |
| 36128 | +} |
| 36129 | + |
| 36130 | +/* |
| 36131 | +** If there are no blocks in the list headed by pList, remove pList |
| 36132 | +** from the pGroup->pBlockList list and free it with sqlite3_free(). |
| 36133 | +*/ |
| 36134 | +static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){ |
| 36135 | + assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36136 | + if( pList->pFirst==0 ){ |
| 36137 | + PGroupBlockList **pp; |
| 36138 | + for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext); |
| 36139 | + *pp = (*pp)->pNext; |
| 36140 | + sqlite3_free(pList); |
| 36141 | + } |
| 36142 | +} |
| 36143 | +#endif /* SQLITE_PAGECACHE_BLOCKALLOC */ |
| 36144 | + |
| 35737 | 36145 | /* |
| 35738 | 36146 | ** Allocate a new page object initially associated with cache pCache. |
| 35739 | 36147 | */ |
| 35740 | 36148 | static PgHdr1 *pcache1AllocPage(PCache1 *pCache){ |
| 35741 | 36149 | int nByte = sizeof(PgHdr1) + pCache->szPage; |
| 35742 | | - void *pPg = pcache1Alloc(nByte); |
| 36150 | + void *pPg = 0; |
| 35743 | 36151 | PgHdr1 *p; |
| 36152 | + |
| 36153 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36154 | + PGroup *pGroup = pCache->pGroup; |
| 36155 | + PGroupBlockList *pList; |
| 36156 | + PGroupBlock *pBlock; |
| 36157 | + int i; |
| 36158 | + |
| 36159 | + nByte += sizeof(PGroupBlockList *); |
| 36160 | + nByte = ROUND8(nByte); |
| 36161 | + |
| 36162 | + for(pList=pGroup->pBlockList; pList; pList=pList->pNext){ |
| 36163 | + if( pList->nByte==nByte ) break; |
| 36164 | + } |
| 36165 | + if( pList==0 ){ |
| 36166 | + PGroupBlockList *pNew; |
| 36167 | + assert( pGroup->isBusy==0 ); |
| 36168 | + assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36169 | + pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */ |
| 36170 | + pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList)); |
| 36171 | + pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */ |
| 36172 | + if( pNew==0 ){ |
| 36173 | + /* malloc() failure. Return early. */ |
| 36174 | + return 0; |
| 36175 | + } |
| 36176 | +#ifdef SQLITE_DEBUG |
| 36177 | + for(pList=pGroup->pBlockList; pList; pList=pList->pNext){ |
| 36178 | + assert( pList->nByte!=nByte ); |
| 36179 | + } |
| 36180 | +#endif |
| 36181 | + pNew->nByte = nByte; |
| 36182 | + pNew->pNext = pGroup->pBlockList; |
| 36183 | + pGroup->pBlockList = pNew; |
| 36184 | + pList = pNew; |
| 36185 | + } |
| 36186 | + |
| 36187 | + pBlock = pList->pFirst; |
| 36188 | + if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){ |
| 36189 | + int sz; |
| 36190 | + |
| 36191 | + /* Allocate a new block. Try to allocate enough space for the PGroupBlock |
| 36192 | + ** structure and MINENTRY allocations of nByte bytes each. If the |
| 36193 | + ** allocator returns more memory than requested, then more than MINENTRY |
| 36194 | + ** allocations may fit in it. */ |
| 36195 | + assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 36196 | + pcache1LeaveMutex(pCache->pGroup); |
| 36197 | + sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte; |
| 36198 | + pBlock = (PGroupBlock *)sqlite3Malloc(sz); |
| 36199 | + pcache1EnterMutex(pCache->pGroup); |
| 36200 | + |
| 36201 | + if( !pBlock ){ |
| 36202 | + freeListIfEmpty(pGroup, pList); |
| 36203 | + return 0; |
| 36204 | + } |
| 36205 | + pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte; |
| 36206 | + if( pBlock->nEntry>=BMS ){ |
| 36207 | + pBlock->nEntry = BMS-1; |
| 36208 | + } |
| 36209 | + pBlock->pList = pList; |
| 36210 | + pBlock->mUsed = 0; |
| 36211 | + pBlock->aData = (u8 *)&pBlock[1]; |
| 36212 | + addBlockToList(pList, pBlock); |
| 36213 | + |
| 36214 | + sz = sqlite3MallocSize(pBlock); |
| 36215 | + sqlite3_mutex_enter(pcache1.mutex); |
| 36216 | + sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 36217 | + sqlite3_mutex_leave(pcache1.mutex); |
| 36218 | + } |
| 36219 | + |
| 36220 | + for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){ |
| 36221 | + if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){ |
| 36222 | + pBlock->mUsed |= ((Bitmask)1<<i); |
| 36223 | + pPg = (void *)&pBlock->aData[pList->nByte * i]; |
| 36224 | + } |
| 36225 | + } |
| 36226 | + assert( pPg ); |
| 36227 | + PAGE_SET_BLOCKPTR(pCache, pPg, pBlock); |
| 36228 | + |
| 36229 | + /* If the block is now full, shift it to the end of the list */ |
| 36230 | + if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){ |
| 36231 | + assert( pList->pFirst==pBlock ); |
| 36232 | + assert( pBlock->pPrev==0 ); |
| 36233 | + assert( pList->pLast->pNext==0 ); |
| 36234 | + pList->pFirst = pBlock->pNext; |
| 36235 | + pList->pFirst->pPrev = 0; |
| 36236 | + pBlock->pPrev = pList->pLast; |
| 36237 | + pBlock->pNext = 0; |
| 36238 | + pList->pLast->pNext = pBlock; |
| 36239 | + pList->pLast = pBlock; |
| 36240 | + } |
| 36241 | + p = PAGE_TO_PGHDR1(pCache, pPg); |
| 36242 | + if( pCache->bPurgeable ){ |
| 36243 | + pCache->pGroup->nCurrentPage++; |
| 36244 | + } |
| 36245 | +#else |
| 36246 | + /* The group mutex must be released before pcache1Alloc() is called. This |
| 36247 | + ** is because it may call sqlite3_release_memory(), which assumes that |
| 36248 | + ** this mutex is not held. */ |
| 36249 | + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 36250 | + pcache1LeaveMutex(pCache->pGroup); |
| 36251 | + pPg = pcache1Alloc(nByte); |
| 36252 | + pcache1EnterMutex(pCache->pGroup); |
| 35744 | 36253 | if( pPg ){ |
| 35745 | 36254 | p = PAGE_TO_PGHDR1(pCache, pPg); |
| 35746 | 36255 | if( pCache->bPurgeable ){ |
| 35747 | 36256 | pCache->pGroup->nCurrentPage++; |
| 35748 | 36257 | } |
| 35749 | 36258 | }else{ |
| 35750 | 36259 | p = 0; |
| 35751 | 36260 | } |
| 36261 | +#endif |
| 35752 | 36262 | return p; |
| 35753 | 36263 | } |
| 35754 | 36264 | |
| 35755 | 36265 | /* |
| 35756 | 36266 | ** Free a page object allocated by pcache1AllocPage(). |
| | @@ -35760,14 +36270,56 @@ |
| 35760 | 36270 | ** with a NULL pointer, so we mark the NULL test with ALWAYS(). |
| 35761 | 36271 | */ |
| 35762 | 36272 | static void pcache1FreePage(PgHdr1 *p){ |
| 35763 | 36273 | if( ALWAYS(p) ){ |
| 35764 | 36274 | PCache1 *pCache = p->pCache; |
| 36275 | + void *pPg = PGHDR1_TO_PAGE(p); |
| 36276 | + |
| 36277 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36278 | + PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg); |
| 36279 | + PGroupBlockList *pList = pBlock->pList; |
| 36280 | + int i = ((u8 *)pPg - pBlock->aData) / pList->nByte; |
| 36281 | + |
| 36282 | + assert( pPg==(void *)&pBlock->aData[i*pList->nByte] ); |
| 36283 | + assert( pBlock->mUsed & ((Bitmask)1<<i) ); |
| 36284 | + pBlock->mUsed &= ~((Bitmask)1<<i); |
| 36285 | + |
| 36286 | + /* Remove the block from the list. If it is completely empty, free it. |
| 36287 | + ** Or if it is not completely empty, re-insert it at the start of the |
| 36288 | + ** list. */ |
| 36289 | + if( pList->pFirst==pBlock ){ |
| 36290 | + pList->pFirst = pBlock->pNext; |
| 36291 | + if( pList->pFirst ) pList->pFirst->pPrev = 0; |
| 36292 | + }else{ |
| 36293 | + pBlock->pPrev->pNext = pBlock->pNext; |
| 36294 | + } |
| 36295 | + if( pList->pLast==pBlock ){ |
| 36296 | + pList->pLast = pBlock->pPrev; |
| 36297 | + if( pList->pLast ) pList->pLast->pNext = 0; |
| 36298 | + }else{ |
| 36299 | + pBlock->pNext->pPrev = pBlock->pPrev; |
| 36300 | + } |
| 36301 | + |
| 36302 | + if( pBlock->mUsed==0 ){ |
| 36303 | + PGroup *pGroup = p->pCache->pGroup; |
| 36304 | + |
| 36305 | + int sz = sqlite3MallocSize(pBlock); |
| 36306 | + sqlite3_mutex_enter(pcache1.mutex); |
| 36307 | + sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz); |
| 36308 | + sqlite3_mutex_leave(pcache1.mutex); |
| 36309 | + freeListIfEmpty(pGroup, pList); |
| 36310 | + sqlite3_free(pBlock); |
| 36311 | + }else{ |
| 36312 | + addBlockToList(pList, pBlock); |
| 36313 | + } |
| 36314 | +#else |
| 36315 | + assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); |
| 36316 | + pcache1Free(pPg); |
| 36317 | +#endif |
| 35765 | 36318 | if( pCache->bPurgeable ){ |
| 35766 | 36319 | pCache->pGroup->nCurrentPage--; |
| 35767 | 36320 | } |
| 35768 | | - pcache1Free(PGHDR1_TO_PAGE(p)); |
| 35769 | 36321 | } |
| 35770 | 36322 | } |
| 35771 | 36323 | |
| 35772 | 36324 | /* |
| 35773 | 36325 | ** Malloc function used by SQLite to obtain space from the buffer configured |
| | @@ -36201,13 +36753,11 @@ |
| 36201 | 36753 | /* Step 5. If a usable page buffer has still not been found, |
| 36202 | 36754 | ** attempt to allocate a new one. |
| 36203 | 36755 | */ |
| 36204 | 36756 | if( !pPage ){ |
| 36205 | 36757 | if( createFlag==1 ) sqlite3BeginBenignMalloc(); |
| 36206 | | - pcache1LeaveMutex(pGroup); |
| 36207 | 36758 | pPage = pcache1AllocPage(pCache); |
| 36208 | | - pcache1EnterMutex(pGroup); |
| 36209 | 36759 | if( createFlag==1 ) sqlite3EndBenignMalloc(); |
| 36210 | 36760 | } |
| 36211 | 36761 | |
| 36212 | 36762 | if( pPage ){ |
| 36213 | 36763 | unsigned int h = iKey % pCache->nHash; |
| | @@ -36373,10 +36923,13 @@ |
| 36373 | 36923 | ** been released, the function returns. The return value is the total number |
| 36374 | 36924 | ** of bytes of memory released. |
| 36375 | 36925 | */ |
| 36376 | 36926 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 36377 | 36927 | int nFree = 0; |
| 36928 | +#ifdef SQLITE_PAGECACHE_BLOCKALLOC |
| 36929 | + if( pcache1.grp.isBusy ) return 0; |
| 36930 | +#endif |
| 36378 | 36931 | assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 36379 | 36932 | assert( sqlite3_mutex_notheld(pcache1.mutex) ); |
| 36380 | 36933 | if( pcache1.pStart==0 ){ |
| 36381 | 36934 | PgHdr1 *p; |
| 36382 | 36935 | pcache1EnterMutex(&pcache1.grp); |
| | @@ -37585,10 +38138,11 @@ |
| 37585 | 38138 | u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */ |
| 37586 | 38139 | u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ |
| 37587 | 38140 | u8 tempFile; /* zFilename is a temporary file */ |
| 37588 | 38141 | u8 readOnly; /* True for a read-only database */ |
| 37589 | 38142 | u8 memDb; /* True to inhibit all file I/O */ |
| 38143 | + u8 hasSeenStress; /* pagerStress() called one or more times */ |
| 37590 | 38144 | |
| 37591 | 38145 | /************************************************************************** |
| 37592 | 38146 | ** The following block contains those class members that change during |
| 37593 | 38147 | ** routine opertion. Class members not in this block are either fixed |
| 37594 | 38148 | ** when the pager is first created or else only change when there is a |
| | @@ -40704,10 +41258,11 @@ |
| 40704 | 41258 | ** to the caller. |
| 40705 | 41259 | */ |
| 40706 | 41260 | SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){ |
| 40707 | 41261 | u8 *pTmp = (u8 *)pPager->pTmpSpace; |
| 40708 | 41262 | |
| 41263 | + assert( assert_pager_state(pPager) ); |
| 40709 | 41264 | disable_simulated_io_errors(); |
| 40710 | 41265 | sqlite3BeginBenignMalloc(); |
| 40711 | 41266 | /* pPager->errCode = 0; */ |
| 40712 | 41267 | pPager->exclusiveMode = 0; |
| 40713 | 41268 | #ifndef SQLITE_OMIT_WAL |
| | @@ -41138,10 +41693,11 @@ |
| 41138 | 41693 | ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1 |
| 41139 | 41694 | ** while in the error state, hence it is impossible for this routine to |
| 41140 | 41695 | ** be called in the error state. Nevertheless, we include a NEVER() |
| 41141 | 41696 | ** test for the error state as a safeguard against future changes. |
| 41142 | 41697 | */ |
| 41698 | + pPager->hasSeenStress = 1; |
| 41143 | 41699 | if( NEVER(pPager->errCode) ) return SQLITE_OK; |
| 41144 | 41700 | if( pPager->doNotSpill ) return SQLITE_OK; |
| 41145 | 41701 | if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){ |
| 41146 | 41702 | return SQLITE_OK; |
| 41147 | 41703 | } |
| | @@ -50430,15 +50986,16 @@ |
| 50430 | 50986 | for(i=0; i<nCell; i++){ |
| 50431 | 50987 | u8 *pCell = findCell(pPage, i); |
| 50432 | 50988 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 50433 | 50989 | CellInfo info; |
| 50434 | 50990 | btreeParseCellPtr(pPage, pCell, &info); |
| 50435 | | - if( info.iOverflow ){ |
| 50436 | | - if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 50437 | | - put4byte(&pCell[info.iOverflow], iTo); |
| 50438 | | - break; |
| 50439 | | - } |
| 50991 | + if( info.iOverflow |
| 50992 | + && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage |
| 50993 | + && iFrom==get4byte(&pCell[info.iOverflow]) |
| 50994 | + ){ |
| 50995 | + put4byte(&pCell[info.iOverflow], iTo); |
| 50996 | + break; |
| 50440 | 50997 | } |
| 50441 | 50998 | }else{ |
| 50442 | 50999 | if( get4byte(pCell)==iFrom ){ |
| 50443 | 51000 | put4byte(pCell, iTo); |
| 50444 | 51001 | break; |
| | @@ -51155,11 +51712,12 @@ |
| 51155 | 51712 | |
| 51156 | 51713 | if( NEVER(wrFlag && pBt->readOnly) ){ |
| 51157 | 51714 | return SQLITE_READONLY; |
| 51158 | 51715 | } |
| 51159 | 51716 | if( iTable==1 && btreePagecount(pBt)==0 ){ |
| 51160 | | - return SQLITE_EMPTY; |
| 51717 | + assert( wrFlag==0 ); |
| 51718 | + iTable = 0; |
| 51161 | 51719 | } |
| 51162 | 51720 | |
| 51163 | 51721 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 51164 | 51722 | ** variables and link the cursor into the BtShared list. */ |
| 51165 | 51723 | pCur->pgnoRoot = (Pgno)iTable; |
| | @@ -51909,10 +52467,13 @@ |
| 51909 | 52467 | int i; |
| 51910 | 52468 | for(i=1; i<=pCur->iPage; i++){ |
| 51911 | 52469 | releasePage(pCur->apPage[i]); |
| 51912 | 52470 | } |
| 51913 | 52471 | pCur->iPage = 0; |
| 52472 | + }else if( pCur->pgnoRoot==0 ){ |
| 52473 | + pCur->eState = CURSOR_INVALID; |
| 52474 | + return SQLITE_OK; |
| 51914 | 52475 | }else{ |
| 51915 | 52476 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); |
| 51916 | 52477 | if( rc!=SQLITE_OK ){ |
| 51917 | 52478 | pCur->eState = CURSOR_INVALID; |
| 51918 | 52479 | return rc; |
| | @@ -52018,11 +52579,11 @@ |
| 52018 | 52579 | assert( cursorHoldsMutex(pCur) ); |
| 52019 | 52580 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 52020 | 52581 | rc = moveToRoot(pCur); |
| 52021 | 52582 | if( rc==SQLITE_OK ){ |
| 52022 | 52583 | if( pCur->eState==CURSOR_INVALID ){ |
| 52023 | | - assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52584 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52024 | 52585 | *pRes = 1; |
| 52025 | 52586 | }else{ |
| 52026 | 52587 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
| 52027 | 52588 | *pRes = 0; |
| 52028 | 52589 | rc = moveToLeftmost(pCur); |
| | @@ -52057,11 +52618,11 @@ |
| 52057 | 52618 | } |
| 52058 | 52619 | |
| 52059 | 52620 | rc = moveToRoot(pCur); |
| 52060 | 52621 | if( rc==SQLITE_OK ){ |
| 52061 | 52622 | if( CURSOR_INVALID==pCur->eState ){ |
| 52062 | | - assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52623 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52063 | 52624 | *pRes = 1; |
| 52064 | 52625 | }else{ |
| 52065 | 52626 | assert( pCur->eState==CURSOR_VALID ); |
| 52066 | 52627 | *pRes = 0; |
| 52067 | 52628 | rc = moveToRightmost(pCur); |
| | @@ -52130,16 +52691,16 @@ |
| 52130 | 52691 | |
| 52131 | 52692 | rc = moveToRoot(pCur); |
| 52132 | 52693 | if( rc ){ |
| 52133 | 52694 | return rc; |
| 52134 | 52695 | } |
| 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 ); |
| 52696 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] ); |
| 52697 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit ); |
| 52698 | + assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 ); |
| 52138 | 52699 | if( pCur->eState==CURSOR_INVALID ){ |
| 52139 | 52700 | *pRes = -1; |
| 52140 | | - assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52701 | + assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
| 52141 | 52702 | return SQLITE_OK; |
| 52142 | 52703 | } |
| 52143 | 52704 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
| 52144 | 52705 | for(;;){ |
| 52145 | 52706 | int lwr, upr, idx; |
| | @@ -52862,10 +53423,13 @@ |
| 52862 | 53423 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 52863 | 53424 | btreeParseCellPtr(pPage, pCell, &info); |
| 52864 | 53425 | if( info.iOverflow==0 ){ |
| 52865 | 53426 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
| 52866 | 53427 | } |
| 53428 | + if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){ |
| 53429 | + return SQLITE_CORRUPT; /* Cell extends past end of page */ |
| 53430 | + } |
| 52867 | 53431 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
| 52868 | 53432 | assert( pBt->usableSize > 4 ); |
| 52869 | 53433 | ovflPageSize = pBt->usableSize - 4; |
| 52870 | 53434 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 52871 | 53435 | assert( ovflPgno==0 || nOvfl>0 ); |
| | @@ -55045,10 +55609,15 @@ |
| 55045 | 55609 | ** corruption) an SQLite error code is returned. |
| 55046 | 55610 | */ |
| 55047 | 55611 | SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ |
| 55048 | 55612 | i64 nEntry = 0; /* Value to return in *pnEntry */ |
| 55049 | 55613 | int rc; /* Return code */ |
| 55614 | + |
| 55615 | + if( pCur->pgnoRoot==0 ){ |
| 55616 | + *pnEntry = 0; |
| 55617 | + return SQLITE_OK; |
| 55618 | + } |
| 55050 | 55619 | rc = moveToRoot(pCur); |
| 55051 | 55620 | |
| 55052 | 55621 | /* Unless an error occurs, the following loop runs one iteration for each |
| 55053 | 55622 | ** page in the B-Tree structure (not including overflow pages). |
| 55054 | 55623 | */ |
| | @@ -55829,11 +56398,10 @@ |
| 55829 | 56398 | */ |
| 55830 | 56399 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ |
| 55831 | 56400 | BtShared *pBt = pBtree->pBt; |
| 55832 | 56401 | int rc; /* Return code */ |
| 55833 | 56402 | |
| 55834 | | - assert( pBtree->inTrans==TRANS_NONE ); |
| 55835 | 56403 | assert( iVersion==1 || iVersion==2 ); |
| 55836 | 56404 | |
| 55837 | 56405 | /* If setting the version fields to 1, do not automatically open the |
| 55838 | 56406 | ** WAL connection, even if the version fields are currently set to 2. |
| 55839 | 56407 | */ |
| | @@ -56268,106 +56836,110 @@ |
| 56268 | 56836 | /* Update the schema version field in the destination database. This |
| 56269 | 56837 | ** is to make sure that the schema-version really does change in |
| 56270 | 56838 | ** the case where the source and destination databases have the |
| 56271 | 56839 | ** same schema version. |
| 56272 | 56840 | */ |
| 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; |
| 56841 | + if( rc==SQLITE_DONE ){ |
| 56842 | + rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1); |
| 56843 | + if( rc==SQLITE_OK ){ |
| 56844 | + if( p->pDestDb ){ |
| 56845 | + sqlite3ResetInternalSchema(p->pDestDb, -1); |
| 56846 | + } |
| 56847 | + if( destMode==PAGER_JOURNALMODE_WAL ){ |
| 56848 | + rc = sqlite3BtreeSetVersion(p->pDest, 2); |
| 56849 | + } |
| 56850 | + } |
| 56851 | + if( rc==SQLITE_OK ){ |
| 56852 | + int nDestTruncate; |
| 56853 | + /* Set nDestTruncate to the final number of pages in the destination |
| 56854 | + ** database. The complication here is that the destination page |
| 56855 | + ** size may be different to the source page size. |
| 56856 | + ** |
| 56857 | + ** If the source page size is smaller than the destination page size, |
| 56858 | + ** round up. In this case the call to sqlite3OsTruncate() below will |
| 56859 | + ** fix the size of the file. However it is important to call |
| 56860 | + ** sqlite3PagerTruncateImage() here so that any pages in the |
| 56861 | + ** destination file that lie beyond the nDestTruncate page mark are |
| 56862 | + ** journalled by PagerCommitPhaseOne() before they are destroyed |
| 56863 | + ** by the file truncation. |
| 56864 | + */ |
| 56865 | + assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) ); |
| 56866 | + assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) ); |
| 56867 | + if( pgszSrc<pgszDest ){ |
| 56868 | + int ratio = pgszDest/pgszSrc; |
| 56869 | + nDestTruncate = (nSrcPage+ratio-1)/ratio; |
| 56870 | + if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){ |
| 56871 | + nDestTruncate--; |
| 56872 | + } |
| 56873 | + }else{ |
| 56874 | + nDestTruncate = nSrcPage * (pgszSrc/pgszDest); |
| 56875 | + } |
| 56876 | + sqlite3PagerTruncateImage(pDestPager, nDestTruncate); |
| 56877 | + |
| 56878 | + if( pgszSrc<pgszDest ){ |
| 56879 | + /* If the source page-size is smaller than the destination page-size, |
| 56880 | + ** two extra things may need to happen: |
| 56881 | + ** |
| 56882 | + ** * The destination may need to be truncated, and |
| 56883 | + ** |
| 56884 | + ** * Data stored on the pages immediately following the |
| 56885 | + ** pending-byte page in the source database may need to be |
| 56886 | + ** copied into the destination database. |
| 56887 | + */ |
| 56888 | + const i64 iSize = (i64)pgszSrc * (i64)nSrcPage; |
| 56889 | + sqlite3_file * const pFile = sqlite3PagerFile(pDestPager); |
| 56890 | + i64 iOff; |
| 56891 | + i64 iEnd; |
| 56892 | + |
| 56893 | + assert( pFile ); |
| 56894 | + assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || ( |
| 56895 | + nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) |
| 56896 | + && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest |
| 56897 | + )); |
| 56898 | + |
| 56899 | + /* This call ensures that all data required to recreate the original |
| 56900 | + ** database has been stored in the journal for pDestPager and the |
| 56901 | + ** journal synced to disk. So at this point we may safely modify |
| 56902 | + ** the database file in any way, knowing that if a power failure |
| 56903 | + ** occurs, the original database will be reconstructed from the |
| 56904 | + ** journal file. */ |
| 56905 | + rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1); |
| 56906 | + |
| 56907 | + /* Write the extra pages and truncate the database file as required */ |
| 56908 | + iEnd = MIN(PENDING_BYTE + pgszDest, iSize); |
| 56909 | + for( |
| 56910 | + iOff=PENDING_BYTE+pgszSrc; |
| 56911 | + rc==SQLITE_OK && iOff<iEnd; |
| 56912 | + iOff+=pgszSrc |
| 56913 | + ){ |
| 56914 | + PgHdr *pSrcPg = 0; |
| 56915 | + const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1); |
| 56916 | + rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); |
| 56917 | + if( rc==SQLITE_OK ){ |
| 56918 | + u8 *zData = sqlite3PagerGetData(pSrcPg); |
| 56919 | + rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff); |
| 56920 | + } |
| 56921 | + sqlite3PagerUnref(pSrcPg); |
| 56922 | + } |
| 56923 | + if( rc==SQLITE_OK ){ |
| 56924 | + rc = backupTruncateFile(pFile, iSize); |
| 56925 | + } |
| 56926 | + |
| 56927 | + /* Sync the database file to disk. */ |
| 56928 | + if( rc==SQLITE_OK ){ |
| 56929 | + rc = sqlite3PagerSync(pDestPager); |
| 56930 | + } |
| 56931 | + }else{ |
| 56932 | + rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); |
| 56933 | + } |
| 56934 | + |
| 56935 | + /* Finish committing the transaction to the destination database. */ |
| 56936 | + if( SQLITE_OK==rc |
| 56937 | + && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0)) |
| 56938 | + ){ |
| 56939 | + rc = SQLITE_DONE; |
| 56940 | + } |
| 56369 | 56941 | } |
| 56370 | 56942 | } |
| 56371 | 56943 | |
| 56372 | 56944 | /* If bCloseTrans is true, then this function opened a read transaction |
| 56373 | 56945 | ** on the source database. Close the read transaction here. There is |
| | @@ -56831,38 +57403,32 @@ |
| 56831 | 57403 | ** invoking an external callback, free it now. Calling this function |
| 56832 | 57404 | ** does not free any Mem.zMalloc buffer. |
| 56833 | 57405 | */ |
| 56834 | 57406 | SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){ |
| 56835 | 57407 | 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 | | - } |
| 57408 | + if( p->flags&MEM_Agg ){ |
| 57409 | + sqlite3VdbeMemFinalize(p, p->u.pDef); |
| 57410 | + assert( (p->flags & MEM_Agg)==0 ); |
| 57411 | + sqlite3VdbeMemRelease(p); |
| 57412 | + }else if( p->flags&MEM_Dyn && p->xDel ){ |
| 57413 | + assert( (p->flags&MEM_RowSet)==0 ); |
| 57414 | + p->xDel((void *)p->z); |
| 57415 | + p->xDel = 0; |
| 57416 | + }else if( p->flags&MEM_RowSet ){ |
| 57417 | + sqlite3RowSetClear(p->u.pRowSet); |
| 57418 | + }else if( p->flags&MEM_Frame ){ |
| 57419 | + sqlite3VdbeMemSetNull(p); |
| 56854 | 57420 | } |
| 56855 | 57421 | } |
| 56856 | 57422 | |
| 56857 | 57423 | /* |
| 56858 | 57424 | ** Release any memory held by the Mem. This may leave the Mem in an |
| 56859 | 57425 | ** inconsistent state, for example with (Mem.z==0) and |
| 56860 | 57426 | ** (Mem.type==SQLITE_TEXT). |
| 56861 | 57427 | */ |
| 56862 | 57428 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){ |
| 56863 | | - sqlite3VdbeMemReleaseExternal(p); |
| 57429 | + MemReleaseExt(p); |
| 56864 | 57430 | sqlite3DbFree(p->db, p->zMalloc); |
| 56865 | 57431 | p->z = 0; |
| 56866 | 57432 | p->zMalloc = 0; |
| 56867 | 57433 | p->xDel = 0; |
| 56868 | 57434 | } |
| | @@ -57180,11 +57746,11 @@ |
| 57180 | 57746 | ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z |
| 57181 | 57747 | ** and flags gets srcType (either MEM_Ephem or MEM_Static). |
| 57182 | 57748 | */ |
| 57183 | 57749 | SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ |
| 57184 | 57750 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
| 57185 | | - sqlite3VdbeMemReleaseExternal(pTo); |
| 57751 | + MemReleaseExt(pTo); |
| 57186 | 57752 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 57187 | 57753 | pTo->xDel = 0; |
| 57188 | 57754 | if( (pFrom->flags&MEM_Static)==0 ){ |
| 57189 | 57755 | pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); |
| 57190 | 57756 | assert( srcType==MEM_Ephem || srcType==MEM_Static ); |
| | @@ -57198,11 +57764,11 @@ |
| 57198 | 57764 | */ |
| 57199 | 57765 | SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ |
| 57200 | 57766 | int rc = SQLITE_OK; |
| 57201 | 57767 | |
| 57202 | 57768 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
| 57203 | | - sqlite3VdbeMemReleaseExternal(pTo); |
| 57769 | + MemReleaseExt(pTo); |
| 57204 | 57770 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 57205 | 57771 | pTo->flags &= ~MEM_Dyn; |
| 57206 | 57772 | |
| 57207 | 57773 | if( pTo->flags&(MEM_Str|MEM_Blob) ){ |
| 57208 | 57774 | if( 0==(pFrom->flags&MEM_Static) ){ |
| | @@ -57592,15 +58158,15 @@ |
| 57592 | 58158 | *ppVal = 0; |
| 57593 | 58159 | return SQLITE_OK; |
| 57594 | 58160 | } |
| 57595 | 58161 | op = pExpr->op; |
| 57596 | 58162 | |
| 57597 | | - /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3. |
| 58163 | + /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2. |
| 57598 | 58164 | ** The ifdef here is to enable us to achieve 100% branch test coverage even |
| 57599 | | - ** when SQLITE_ENABLE_STAT3 is omitted. |
| 58165 | + ** when SQLITE_ENABLE_STAT2 is omitted. |
| 57600 | 58166 | */ |
| 57601 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 58167 | +#ifdef SQLITE_ENABLE_STAT2 |
| 57602 | 58168 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 57603 | 58169 | #else |
| 57604 | 58170 | if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 57605 | 58171 | #endif |
| 57606 | 58172 | |
| | @@ -58153,10 +58719,16 @@ |
| 58153 | 58719 | assert( p->nOp - i >= 3 ); |
| 58154 | 58720 | assert( pOp[-1].opcode==OP_Integer ); |
| 58155 | 58721 | n = pOp[-1].p1; |
| 58156 | 58722 | if( n>nMaxArgs ) nMaxArgs = n; |
| 58157 | 58723 | #endif |
| 58724 | + }else if( opcode==OP_Next || opcode==OP_SorterNext ){ |
| 58725 | + pOp->p4.xAdvance = sqlite3BtreeNext; |
| 58726 | + pOp->p4type = P4_ADVANCE; |
| 58727 | + }else if( opcode==OP_Prev ){ |
| 58728 | + pOp->p4.xAdvance = sqlite3BtreePrevious; |
| 58729 | + pOp->p4type = P4_ADVANCE; |
| 58158 | 58730 | } |
| 58159 | 58731 | |
| 58160 | 58732 | if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
| 58161 | 58733 | assert( -1-pOp->p2<p->nLabel ); |
| 58162 | 58734 | pOp->p2 = aLabel[-1-pOp->p2]; |
| | @@ -58244,37 +58816,34 @@ |
| 58244 | 58816 | ** Change the value of the P1 operand for a specific instruction. |
| 58245 | 58817 | ** This routine is useful when a large program is loaded from a |
| 58246 | 58818 | ** static array using sqlite3VdbeAddOpList but we want to make a |
| 58247 | 58819 | ** few minor changes to the program. |
| 58248 | 58820 | */ |
| 58249 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
| 58821 | +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ |
| 58250 | 58822 | assert( p!=0 ); |
| 58251 | | - assert( addr>=0 ); |
| 58252 | | - if( p->nOp>addr ){ |
| 58823 | + if( ((u32)p->nOp)>addr ){ |
| 58253 | 58824 | p->aOp[addr].p1 = val; |
| 58254 | 58825 | } |
| 58255 | 58826 | } |
| 58256 | 58827 | |
| 58257 | 58828 | /* |
| 58258 | 58829 | ** Change the value of the P2 operand for a specific instruction. |
| 58259 | 58830 | ** This routine is useful for setting a jump destination. |
| 58260 | 58831 | */ |
| 58261 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
| 58832 | +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ |
| 58262 | 58833 | assert( p!=0 ); |
| 58263 | | - assert( addr>=0 ); |
| 58264 | | - if( p->nOp>addr ){ |
| 58834 | + if( ((u32)p->nOp)>addr ){ |
| 58265 | 58835 | p->aOp[addr].p2 = val; |
| 58266 | 58836 | } |
| 58267 | 58837 | } |
| 58268 | 58838 | |
| 58269 | 58839 | /* |
| 58270 | 58840 | ** Change the value of the P3 operand for a specific instruction. |
| 58271 | 58841 | */ |
| 58272 | | -SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ |
| 58842 | +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ |
| 58273 | 58843 | assert( p!=0 ); |
| 58274 | | - assert( addr>=0 ); |
| 58275 | | - if( p->nOp>addr ){ |
| 58844 | + if( ((u32)p->nOp)>addr ){ |
| 58276 | 58845 | p->aOp[addr].p3 = val; |
| 58277 | 58846 | } |
| 58278 | 58847 | } |
| 58279 | 58848 | |
| 58280 | 58849 | /* |
| | @@ -58292,12 +58861,12 @@ |
| 58292 | 58861 | /* |
| 58293 | 58862 | ** Change the P2 operand of instruction addr so that it points to |
| 58294 | 58863 | ** the address of the next instruction to be coded. |
| 58295 | 58864 | */ |
| 58296 | 58865 | SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
| 58297 | | - assert( addr>=0 || p->db->mallocFailed ); |
| 58298 | | - if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 58866 | + assert( addr>=0 ); |
| 58867 | + sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 58299 | 58868 | } |
| 58300 | 58869 | |
| 58301 | 58870 | |
| 58302 | 58871 | /* |
| 58303 | 58872 | ** If the input FuncDef structure is ephemeral, then free it. If |
| | @@ -58661,10 +59230,14 @@ |
| 58661 | 59230 | break; |
| 58662 | 59231 | } |
| 58663 | 59232 | case P4_SUBPROGRAM: { |
| 58664 | 59233 | sqlite3_snprintf(nTemp, zTemp, "program"); |
| 58665 | 59234 | break; |
| 59235 | + } |
| 59236 | + case P4_ADVANCE: { |
| 59237 | + zTemp[0] = 0; |
| 59238 | + break; |
| 58666 | 59239 | } |
| 58667 | 59240 | default: { |
| 58668 | 59241 | zP4 = pOp->p4.z; |
| 58669 | 59242 | if( zP4==0 ){ |
| 58670 | 59243 | zP4 = zTemp; |
| | @@ -59285,10 +59858,11 @@ |
| 59285 | 59858 | */ |
| 59286 | 59859 | SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
| 59287 | 59860 | if( pCx==0 ){ |
| 59288 | 59861 | return; |
| 59289 | 59862 | } |
| 59863 | + sqlite3VdbeSorterClose(p->db, pCx); |
| 59290 | 59864 | if( pCx->pBt ){ |
| 59291 | 59865 | sqlite3BtreeClose(pCx->pBt); |
| 59292 | 59866 | /* The pCx->pCursor will be close automatically, if it exists, by |
| 59293 | 59867 | ** the call above. */ |
| 59294 | 59868 | }else if( pCx->pCursor ){ |
| | @@ -62585,10 +63159,17 @@ |
| 62585 | 63159 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 62586 | 63160 | ** P if required. |
| 62587 | 63161 | */ |
| 62588 | 63162 | #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) |
| 62589 | 63163 | |
| 63164 | +/* Return true if the cursor was opened using the OP_OpenSorter opcode. */ |
| 63165 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 63166 | +# define isSorter(x) 0 |
| 63167 | +#else |
| 63168 | +# define isSorter(x) ((x)->pSorter!=0) |
| 63169 | +#endif |
| 63170 | + |
| 62590 | 63171 | /* |
| 62591 | 63172 | ** Argument pMem points at a register that will be passed to a |
| 62592 | 63173 | ** user-defined function or returned to the user as the result of a query. |
| 62593 | 63174 | ** This routine sets the pMem->type variable used by the sqlite3_value_*() |
| 62594 | 63175 | ** routines. |
| | @@ -63179,10 +63760,11 @@ |
| 63179 | 63760 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 63180 | 63761 | u32 offset; /* Offset into the data */ |
| 63181 | 63762 | u32 szField; /* Number of bytes in the content of a field */ |
| 63182 | 63763 | int szHdr; /* Size of the header size field at start of record */ |
| 63183 | 63764 | int avail; /* Number of bytes of available data */ |
| 63765 | + u32 t; /* A type code from the record header */ |
| 63184 | 63766 | Mem *pReg; /* PseudoTable input register */ |
| 63185 | 63767 | } am; |
| 63186 | 63768 | struct OP_Affinity_stack_vars { |
| 63187 | 63769 | const char *zAffinity; /* The affinity to be applied */ |
| 63188 | 63770 | char cAff; /* A single character of affinity */ |
| | @@ -63250,55 +63832,58 @@ |
| 63250 | 63832 | Db *pDb; |
| 63251 | 63833 | } aw; |
| 63252 | 63834 | struct OP_OpenEphemeral_stack_vars { |
| 63253 | 63835 | VdbeCursor *pCx; |
| 63254 | 63836 | } ax; |
| 63255 | | - struct OP_OpenPseudo_stack_vars { |
| 63837 | + struct OP_SorterOpen_stack_vars { |
| 63256 | 63838 | VdbeCursor *pCx; |
| 63257 | 63839 | } ay; |
| 63840 | + struct OP_OpenPseudo_stack_vars { |
| 63841 | + VdbeCursor *pCx; |
| 63842 | + } az; |
| 63258 | 63843 | struct OP_SeekGt_stack_vars { |
| 63259 | 63844 | int res; |
| 63260 | 63845 | int oc; |
| 63261 | 63846 | VdbeCursor *pC; |
| 63262 | 63847 | UnpackedRecord r; |
| 63263 | 63848 | int nField; |
| 63264 | 63849 | i64 iKey; /* The rowid we are to seek to */ |
| 63265 | | - } az; |
| 63850 | + } ba; |
| 63266 | 63851 | struct OP_Seek_stack_vars { |
| 63267 | 63852 | VdbeCursor *pC; |
| 63268 | | - } ba; |
| 63853 | + } bb; |
| 63269 | 63854 | struct OP_Found_stack_vars { |
| 63270 | 63855 | int alreadyExists; |
| 63271 | 63856 | VdbeCursor *pC; |
| 63272 | 63857 | int res; |
| 63273 | 63858 | UnpackedRecord *pIdxKey; |
| 63274 | 63859 | UnpackedRecord r; |
| 63275 | 63860 | char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7]; |
| 63276 | | - } bb; |
| 63861 | + } bc; |
| 63277 | 63862 | struct OP_IsUnique_stack_vars { |
| 63278 | 63863 | u16 ii; |
| 63279 | 63864 | VdbeCursor *pCx; |
| 63280 | 63865 | BtCursor *pCrsr; |
| 63281 | 63866 | u16 nField; |
| 63282 | 63867 | Mem *aMx; |
| 63283 | 63868 | UnpackedRecord r; /* B-Tree index search key */ |
| 63284 | 63869 | i64 R; /* Rowid stored in register P3 */ |
| 63285 | | - } bc; |
| 63870 | + } bd; |
| 63286 | 63871 | struct OP_NotExists_stack_vars { |
| 63287 | 63872 | VdbeCursor *pC; |
| 63288 | 63873 | BtCursor *pCrsr; |
| 63289 | 63874 | int res; |
| 63290 | 63875 | u64 iKey; |
| 63291 | | - } bd; |
| 63876 | + } be; |
| 63292 | 63877 | struct OP_NewRowid_stack_vars { |
| 63293 | 63878 | i64 v; /* The new rowid */ |
| 63294 | 63879 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 63295 | 63880 | int res; /* Result of an sqlite3BtreeLast() */ |
| 63296 | 63881 | int cnt; /* Counter to limit the number of searches */ |
| 63297 | 63882 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
| 63298 | 63883 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
| 63299 | | - } be; |
| 63884 | + } bf; |
| 63300 | 63885 | struct OP_InsertInt_stack_vars { |
| 63301 | 63886 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 63302 | 63887 | Mem *pKey; /* MEM cell holding key for the record */ |
| 63303 | 63888 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
| 63304 | 63889 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
| | @@ -63305,155 +63890,161 @@ |
| 63305 | 63890 | int nZero; /* Number of zero-bytes to append */ |
| 63306 | 63891 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 63307 | 63892 | const char *zDb; /* database name - used by the update hook */ |
| 63308 | 63893 | const char *zTbl; /* Table name - used by the opdate hook */ |
| 63309 | 63894 | int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
| 63310 | | - } bf; |
| 63895 | + } bg; |
| 63311 | 63896 | struct OP_Delete_stack_vars { |
| 63312 | 63897 | i64 iKey; |
| 63313 | 63898 | VdbeCursor *pC; |
| 63314 | | - } bg; |
| 63899 | + } bh; |
| 63900 | + struct OP_SorterCompare_stack_vars { |
| 63901 | + VdbeCursor *pC; |
| 63902 | + int res; |
| 63903 | + } bi; |
| 63904 | + struct OP_SorterData_stack_vars { |
| 63905 | + VdbeCursor *pC; |
| 63906 | + } bj; |
| 63315 | 63907 | struct OP_RowData_stack_vars { |
| 63316 | 63908 | VdbeCursor *pC; |
| 63317 | 63909 | BtCursor *pCrsr; |
| 63318 | 63910 | u32 n; |
| 63319 | 63911 | i64 n64; |
| 63320 | | - } bh; |
| 63912 | + } bk; |
| 63321 | 63913 | struct OP_Rowid_stack_vars { |
| 63322 | 63914 | VdbeCursor *pC; |
| 63323 | 63915 | i64 v; |
| 63324 | 63916 | sqlite3_vtab *pVtab; |
| 63325 | 63917 | const sqlite3_module *pModule; |
| 63326 | | - } bi; |
| 63918 | + } bl; |
| 63327 | 63919 | struct OP_NullRow_stack_vars { |
| 63328 | 63920 | VdbeCursor *pC; |
| 63329 | | - } bj; |
| 63921 | + } bm; |
| 63330 | 63922 | struct OP_Last_stack_vars { |
| 63331 | 63923 | VdbeCursor *pC; |
| 63332 | 63924 | BtCursor *pCrsr; |
| 63333 | 63925 | int res; |
| 63334 | | - } bk; |
| 63926 | + } bn; |
| 63335 | 63927 | struct OP_Rewind_stack_vars { |
| 63336 | 63928 | VdbeCursor *pC; |
| 63337 | 63929 | BtCursor *pCrsr; |
| 63338 | 63930 | int res; |
| 63339 | | - } bl; |
| 63931 | + } bo; |
| 63340 | 63932 | struct OP_Next_stack_vars { |
| 63341 | 63933 | VdbeCursor *pC; |
| 63342 | | - BtCursor *pCrsr; |
| 63343 | 63934 | int res; |
| 63344 | | - } bm; |
| 63935 | + } bp; |
| 63345 | 63936 | struct OP_IdxInsert_stack_vars { |
| 63346 | 63937 | VdbeCursor *pC; |
| 63347 | 63938 | BtCursor *pCrsr; |
| 63348 | 63939 | int nKey; |
| 63349 | 63940 | const char *zKey; |
| 63350 | | - } bn; |
| 63941 | + } bq; |
| 63351 | 63942 | struct OP_IdxDelete_stack_vars { |
| 63352 | 63943 | VdbeCursor *pC; |
| 63353 | 63944 | BtCursor *pCrsr; |
| 63354 | 63945 | int res; |
| 63355 | 63946 | UnpackedRecord r; |
| 63356 | | - } bo; |
| 63947 | + } br; |
| 63357 | 63948 | struct OP_IdxRowid_stack_vars { |
| 63358 | 63949 | BtCursor *pCrsr; |
| 63359 | 63950 | VdbeCursor *pC; |
| 63360 | 63951 | i64 rowid; |
| 63361 | | - } bp; |
| 63952 | + } bs; |
| 63362 | 63953 | struct OP_IdxGE_stack_vars { |
| 63363 | 63954 | VdbeCursor *pC; |
| 63364 | 63955 | int res; |
| 63365 | 63956 | UnpackedRecord r; |
| 63366 | | - } bq; |
| 63957 | + } bt; |
| 63367 | 63958 | struct OP_Destroy_stack_vars { |
| 63368 | 63959 | int iMoved; |
| 63369 | 63960 | int iCnt; |
| 63370 | 63961 | Vdbe *pVdbe; |
| 63371 | 63962 | int iDb; |
| 63372 | | - } br; |
| 63963 | + } bu; |
| 63373 | 63964 | struct OP_Clear_stack_vars { |
| 63374 | 63965 | int nChange; |
| 63375 | | - } bs; |
| 63966 | + } bv; |
| 63376 | 63967 | struct OP_CreateTable_stack_vars { |
| 63377 | 63968 | int pgno; |
| 63378 | 63969 | int flags; |
| 63379 | 63970 | Db *pDb; |
| 63380 | | - } bt; |
| 63971 | + } bw; |
| 63381 | 63972 | struct OP_ParseSchema_stack_vars { |
| 63382 | 63973 | int iDb; |
| 63383 | 63974 | const char *zMaster; |
| 63384 | 63975 | char *zSql; |
| 63385 | 63976 | InitData initData; |
| 63386 | | - } bu; |
| 63977 | + } bx; |
| 63387 | 63978 | struct OP_IntegrityCk_stack_vars { |
| 63388 | 63979 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 63389 | 63980 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
| 63390 | 63981 | int j; /* Loop counter */ |
| 63391 | 63982 | int nErr; /* Number of errors reported */ |
| 63392 | 63983 | char *z; /* Text of the error report */ |
| 63393 | 63984 | Mem *pnErr; /* Register keeping track of errors remaining */ |
| 63394 | | - } bv; |
| 63985 | + } by; |
| 63395 | 63986 | struct OP_RowSetRead_stack_vars { |
| 63396 | 63987 | i64 val; |
| 63397 | | - } bw; |
| 63988 | + } bz; |
| 63398 | 63989 | struct OP_RowSetTest_stack_vars { |
| 63399 | 63990 | int iSet; |
| 63400 | 63991 | int exists; |
| 63401 | | - } bx; |
| 63992 | + } ca; |
| 63402 | 63993 | struct OP_Program_stack_vars { |
| 63403 | 63994 | int nMem; /* Number of memory registers for sub-program */ |
| 63404 | 63995 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 63405 | 63996 | Mem *pRt; /* Register to allocate runtime space */ |
| 63406 | 63997 | Mem *pMem; /* Used to iterate through memory cells */ |
| 63407 | 63998 | Mem *pEnd; /* Last memory cell in new array */ |
| 63408 | 63999 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 63409 | 64000 | SubProgram *pProgram; /* Sub-program to execute */ |
| 63410 | 64001 | void *t; /* Token identifying trigger */ |
| 63411 | | - } by; |
| 64002 | + } cb; |
| 63412 | 64003 | struct OP_Param_stack_vars { |
| 63413 | 64004 | VdbeFrame *pFrame; |
| 63414 | 64005 | Mem *pIn; |
| 63415 | | - } bz; |
| 64006 | + } cc; |
| 63416 | 64007 | struct OP_MemMax_stack_vars { |
| 63417 | 64008 | Mem *pIn1; |
| 63418 | 64009 | VdbeFrame *pFrame; |
| 63419 | | - } ca; |
| 64010 | + } cd; |
| 63420 | 64011 | struct OP_AggStep_stack_vars { |
| 63421 | 64012 | int n; |
| 63422 | 64013 | int i; |
| 63423 | 64014 | Mem *pMem; |
| 63424 | 64015 | Mem *pRec; |
| 63425 | 64016 | sqlite3_context ctx; |
| 63426 | 64017 | sqlite3_value **apVal; |
| 63427 | | - } cb; |
| 64018 | + } ce; |
| 63428 | 64019 | struct OP_AggFinal_stack_vars { |
| 63429 | 64020 | Mem *pMem; |
| 63430 | | - } cc; |
| 64021 | + } cf; |
| 63431 | 64022 | struct OP_Checkpoint_stack_vars { |
| 63432 | 64023 | int i; /* Loop counter */ |
| 63433 | 64024 | int aRes[3]; /* Results */ |
| 63434 | 64025 | Mem *pMem; /* Write results here */ |
| 63435 | | - } cd; |
| 64026 | + } cg; |
| 63436 | 64027 | struct OP_JournalMode_stack_vars { |
| 63437 | 64028 | Btree *pBt; /* Btree to change journal mode of */ |
| 63438 | 64029 | Pager *pPager; /* Pager associated with pBt */ |
| 63439 | 64030 | int eNew; /* New journal mode */ |
| 63440 | 64031 | int eOld; /* The old journal mode */ |
| 63441 | 64032 | const char *zFilename; /* Name of database file for pPager */ |
| 63442 | | - } ce; |
| 64033 | + } ch; |
| 63443 | 64034 | struct OP_IncrVacuum_stack_vars { |
| 63444 | 64035 | Btree *pBt; |
| 63445 | | - } cf; |
| 64036 | + } ci; |
| 63446 | 64037 | struct OP_VBegin_stack_vars { |
| 63447 | 64038 | VTable *pVTab; |
| 63448 | | - } cg; |
| 64039 | + } cj; |
| 63449 | 64040 | struct OP_VOpen_stack_vars { |
| 63450 | 64041 | VdbeCursor *pCur; |
| 63451 | 64042 | sqlite3_vtab_cursor *pVtabCursor; |
| 63452 | 64043 | sqlite3_vtab *pVtab; |
| 63453 | 64044 | sqlite3_module *pModule; |
| 63454 | | - } ch; |
| 64045 | + } ck; |
| 63455 | 64046 | struct OP_VFilter_stack_vars { |
| 63456 | 64047 | int nArg; |
| 63457 | 64048 | int iQuery; |
| 63458 | 64049 | const sqlite3_module *pModule; |
| 63459 | 64050 | Mem *pQuery; |
| | @@ -63462,40 +64053,40 @@ |
| 63462 | 64053 | sqlite3_vtab *pVtab; |
| 63463 | 64054 | VdbeCursor *pCur; |
| 63464 | 64055 | int res; |
| 63465 | 64056 | int i; |
| 63466 | 64057 | Mem **apArg; |
| 63467 | | - } ci; |
| 64058 | + } cl; |
| 63468 | 64059 | struct OP_VColumn_stack_vars { |
| 63469 | 64060 | sqlite3_vtab *pVtab; |
| 63470 | 64061 | const sqlite3_module *pModule; |
| 63471 | 64062 | Mem *pDest; |
| 63472 | 64063 | sqlite3_context sContext; |
| 63473 | | - } cj; |
| 64064 | + } cm; |
| 63474 | 64065 | struct OP_VNext_stack_vars { |
| 63475 | 64066 | sqlite3_vtab *pVtab; |
| 63476 | 64067 | const sqlite3_module *pModule; |
| 63477 | 64068 | int res; |
| 63478 | 64069 | VdbeCursor *pCur; |
| 63479 | | - } ck; |
| 64070 | + } cn; |
| 63480 | 64071 | struct OP_VRename_stack_vars { |
| 63481 | 64072 | sqlite3_vtab *pVtab; |
| 63482 | 64073 | Mem *pName; |
| 63483 | | - } cl; |
| 64074 | + } co; |
| 63484 | 64075 | struct OP_VUpdate_stack_vars { |
| 63485 | 64076 | sqlite3_vtab *pVtab; |
| 63486 | 64077 | sqlite3_module *pModule; |
| 63487 | 64078 | int nArg; |
| 63488 | 64079 | int i; |
| 63489 | 64080 | sqlite_int64 rowid; |
| 63490 | 64081 | Mem **apArg; |
| 63491 | 64082 | Mem *pX; |
| 63492 | | - } cm; |
| 64083 | + } cp; |
| 63493 | 64084 | struct OP_Trace_stack_vars { |
| 63494 | 64085 | char *zTrace; |
| 63495 | 64086 | char *z; |
| 63496 | | - } cn; |
| 64087 | + } cq; |
| 63497 | 64088 | } u; |
| 63498 | 64089 | /* End automatically generated code |
| 63499 | 64090 | ********************************************************************/ |
| 63500 | 64091 | |
| 63501 | 64092 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
| | @@ -63591,11 +64182,11 @@ |
| 63591 | 64182 | if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ |
| 63592 | 64183 | assert( pOp->p2>0 ); |
| 63593 | 64184 | assert( pOp->p2<=p->nMem ); |
| 63594 | 64185 | pOut = &aMem[pOp->p2]; |
| 63595 | 64186 | memAboutToChange(p, pOut); |
| 63596 | | - sqlite3VdbeMemReleaseExternal(pOut); |
| 64187 | + MemReleaseExt(pOut); |
| 63597 | 64188 | pOut->flags = MEM_Int; |
| 63598 | 64189 | } |
| 63599 | 64190 | |
| 63600 | 64191 | /* Sanity checking on other operands */ |
| 63601 | 64192 | #ifdef SQLITE_DEBUG |
| | @@ -65061,10 +65652,11 @@ |
| 65061 | 65652 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 65062 | 65653 | u32 offset; /* Offset into the data */ |
| 65063 | 65654 | u32 szField; /* Number of bytes in the content of a field */ |
| 65064 | 65655 | int szHdr; /* Size of the header size field at start of record */ |
| 65065 | 65656 | int avail; /* Number of bytes of available data */ |
| 65657 | + u32 t; /* A type code from the record header */ |
| 65066 | 65658 | Mem *pReg; /* PseudoTable input register */ |
| 65067 | 65659 | #endif /* local variables moved into u.am */ |
| 65068 | 65660 | |
| 65069 | 65661 | |
| 65070 | 65662 | u.am.p1 = pOp->p1; |
| | @@ -65073,11 +65665,10 @@ |
| 65073 | 65665 | memset(&u.am.sMem, 0, sizeof(u.am.sMem)); |
| 65074 | 65666 | assert( u.am.p1<p->nCursor ); |
| 65075 | 65667 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 65076 | 65668 | u.am.pDest = &aMem[pOp->p3]; |
| 65077 | 65669 | memAboutToChange(p, u.am.pDest); |
| 65078 | | - MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 65079 | 65670 | u.am.zRec = 0; |
| 65080 | 65671 | |
| 65081 | 65672 | /* This block sets the variable u.am.payloadSize to be the total number of |
| 65082 | 65673 | ** bytes in the record. |
| 65083 | 65674 | ** |
| | @@ -65117,11 +65708,11 @@ |
| 65117 | 65708 | }else{ |
| 65118 | 65709 | assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) ); |
| 65119 | 65710 | rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize); |
| 65120 | 65711 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 65121 | 65712 | } |
| 65122 | | - }else if( u.am.pC->pseudoTableReg>0 ){ |
| 65713 | + }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){ |
| 65123 | 65714 | u.am.pReg = &aMem[u.am.pC->pseudoTableReg]; |
| 65124 | 65715 | assert( u.am.pReg->flags & MEM_Blob ); |
| 65125 | 65716 | assert( memIsValid(u.am.pReg) ); |
| 65126 | 65717 | u.am.payloadSize = u.am.pReg->n; |
| 65127 | 65718 | u.am.zRec = u.am.pReg->z; |
| | @@ -65130,13 +65721,14 @@ |
| 65130 | 65721 | }else{ |
| 65131 | 65722 | /* Consider the row to be NULL */ |
| 65132 | 65723 | u.am.payloadSize = 0; |
| 65133 | 65724 | } |
| 65134 | 65725 | |
| 65135 | | - /* If u.am.payloadSize is 0, then just store a NULL */ |
| 65726 | + /* If u.am.payloadSize is 0, then just store a NULL. This can happen because of |
| 65727 | + ** nullRow or because of a corrupt database. */ |
| 65136 | 65728 | if( u.am.payloadSize==0 ){ |
| 65137 | | - assert( u.am.pDest->flags&MEM_Null ); |
| 65729 | + MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 65138 | 65730 | goto op_column_out; |
| 65139 | 65731 | } |
| 65140 | 65732 | assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 ); |
| 65141 | 65733 | if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 65142 | 65734 | goto too_big; |
| | @@ -65239,12 +65831,18 @@ |
| 65239 | 65831 | ** of the record to the start of the data for the u.am.i-th column |
| 65240 | 65832 | */ |
| 65241 | 65833 | for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){ |
| 65242 | 65834 | if( u.am.zIdx<u.am.zEndHdr ){ |
| 65243 | 65835 | 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]); |
| 65836 | + if( u.am.zIdx[0]<0x80 ){ |
| 65837 | + u.am.t = u.am.zIdx[0]; |
| 65838 | + u.am.zIdx++; |
| 65839 | + }else{ |
| 65840 | + u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t); |
| 65841 | + } |
| 65842 | + u.am.aType[u.am.i] = u.am.t; |
| 65843 | + u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t); |
| 65246 | 65844 | u.am.offset += u.am.szField; |
| 65247 | 65845 | if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */ |
| 65248 | 65846 | u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */ |
| 65249 | 65847 | break; |
| 65250 | 65848 | } |
| | @@ -65281,11 +65879,11 @@ |
| 65281 | 65879 | ** a pointer to a Mem object. |
| 65282 | 65880 | */ |
| 65283 | 65881 | if( u.am.aOffset[u.am.p2] ){ |
| 65284 | 65882 | assert( rc==SQLITE_OK ); |
| 65285 | 65883 | if( u.am.zRec ){ |
| 65286 | | - sqlite3VdbeMemReleaseExternal(u.am.pDest); |
| 65884 | + MemReleaseExt(u.am.pDest); |
| 65287 | 65885 | sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest); |
| 65288 | 65886 | }else{ |
| 65289 | 65887 | u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]); |
| 65290 | 65888 | sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest); |
| 65291 | 65889 | rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem); |
| | @@ -65298,11 +65896,11 @@ |
| 65298 | 65896 | u.am.pDest->enc = encoding; |
| 65299 | 65897 | }else{ |
| 65300 | 65898 | if( pOp->p4type==P4_MEM ){ |
| 65301 | 65899 | sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static); |
| 65302 | 65900 | }else{ |
| 65303 | | - assert( u.am.pDest->flags&MEM_Null ); |
| 65901 | + MemSetTypeFlag(u.am.pDest, MEM_Null); |
| 65304 | 65902 | } |
| 65305 | 65903 | } |
| 65306 | 65904 | |
| 65307 | 65905 | /* If we dynamically allocated space to hold the data (in the |
| 65308 | 65906 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
| | @@ -65500,11 +66098,11 @@ |
| 65500 | 66098 | i64 nEntry; |
| 65501 | 66099 | BtCursor *pCrsr; |
| 65502 | 66100 | #endif /* local variables moved into u.ap */ |
| 65503 | 66101 | |
| 65504 | 66102 | u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor; |
| 65505 | | - if( u.ap.pCrsr ){ |
| 66103 | + if( ALWAYS(u.ap.pCrsr) ){ |
| 65506 | 66104 | rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry); |
| 65507 | 66105 | }else{ |
| 65508 | 66106 | u.ap.nEntry = 0; |
| 65509 | 66107 | } |
| 65510 | 66108 | pOut->u.i = u.ap.nEntry; |
| | @@ -66076,19 +66674,13 @@ |
| 66076 | 66674 | u.aw.pCur->nullRow = 1; |
| 66077 | 66675 | u.aw.pCur->isOrdered = 1; |
| 66078 | 66676 | rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor); |
| 66079 | 66677 | u.aw.pCur->pKeyInfo = u.aw.pKeyInfo; |
| 66080 | 66678 | |
| 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 | | - } |
| 66679 | + /* Since it performs no memory allocation or IO, the only value that |
| 66680 | + ** sqlite3BtreeCursor() may return is SQLITE_OK. */ |
| 66681 | + assert( rc==SQLITE_OK ); |
| 66090 | 66682 | |
| 66091 | 66683 | /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of |
| 66092 | 66684 | ** SQLite used to check if the root-page flags were sane at this point |
| 66093 | 66685 | ** and report database corruption if they were not, but this check has |
| 66094 | 66686 | ** since moved into the btree layer. */ |
| | @@ -66095,11 +66687,11 @@ |
| 66095 | 66687 | u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO; |
| 66096 | 66688 | u.aw.pCur->isIndex = !u.aw.pCur->isTable; |
| 66097 | 66689 | break; |
| 66098 | 66690 | } |
| 66099 | 66691 | |
| 66100 | | -/* Opcode: OpenEphemeral P1 P2 * P4 * |
| 66692 | +/* Opcode: OpenEphemeral P1 P2 * P4 P5 |
| 66101 | 66693 | ** |
| 66102 | 66694 | ** Open a new cursor P1 to a transient table. |
| 66103 | 66695 | ** The cursor is always opened read/write even if |
| 66104 | 66696 | ** the main database is read-only. The ephemeral |
| 66105 | 66697 | ** table is deleted automatically when the cursor is closed. |
| | @@ -66112,10 +66704,15 @@ |
| 66112 | 66704 | ** This opcode was once called OpenTemp. But that created |
| 66113 | 66705 | ** confusion because the term "temp table", might refer either |
| 66114 | 66706 | ** to a TEMP table at the SQL level, or to a table opened by |
| 66115 | 66707 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 66116 | 66708 | ** that created confusion with the whole virtual-table idea. |
| 66709 | +** |
| 66710 | +** The P5 parameter can be a mask of the BTREE_* flags defined |
| 66711 | +** in btree.h. These flags control aspects of the operation of |
| 66712 | +** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
| 66713 | +** added automatically. |
| 66117 | 66714 | */ |
| 66118 | 66715 | /* Opcode: OpenAutoindex P1 P2 * P4 * |
| 66119 | 66716 | ** |
| 66120 | 66717 | ** This opcode works the same as OP_OpenEphemeral. It has a |
| 66121 | 66718 | ** different name to distinguish its use. Tables created using |
| | @@ -66168,10 +66765,34 @@ |
| 66168 | 66765 | } |
| 66169 | 66766 | u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
| 66170 | 66767 | u.ax.pCx->isIndex = !u.ax.pCx->isTable; |
| 66171 | 66768 | break; |
| 66172 | 66769 | } |
| 66770 | + |
| 66771 | +/* Opcode: OpenSorter P1 P2 * P4 * |
| 66772 | +** |
| 66773 | +** This opcode works like OP_OpenEphemeral except that it opens |
| 66774 | +** a transient index that is specifically designed to sort large |
| 66775 | +** tables using an external merge-sort algorithm. |
| 66776 | +*/ |
| 66777 | +case OP_SorterOpen: { |
| 66778 | +#if 0 /* local variables moved into u.ay */ |
| 66779 | + VdbeCursor *pCx; |
| 66780 | +#endif /* local variables moved into u.ay */ |
| 66781 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 66782 | + u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); |
| 66783 | + if( u.ay.pCx==0 ) goto no_mem; |
| 66784 | + u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo; |
| 66785 | + u.ay.pCx->pKeyInfo->enc = ENC(p->db); |
| 66786 | + u.ay.pCx->isSorter = 1; |
| 66787 | + rc = sqlite3VdbeSorterInit(db, u.ay.pCx); |
| 66788 | +#else |
| 66789 | + pOp->opcode = OP_OpenEphemeral; |
| 66790 | + pc--; |
| 66791 | +#endif |
| 66792 | + break; |
| 66793 | +} |
| 66173 | 66794 | |
| 66174 | 66795 | /* Opcode: OpenPseudo P1 P2 P3 * * |
| 66175 | 66796 | ** |
| 66176 | 66797 | ** Open a new cursor that points to a fake table that contains a single |
| 66177 | 66798 | ** row of data. The content of that one row in the content of memory |
| | @@ -66185,21 +66806,21 @@ |
| 66185 | 66806 | ** |
| 66186 | 66807 | ** P3 is the number of fields in the records that will be stored by |
| 66187 | 66808 | ** the pseudo-table. |
| 66188 | 66809 | */ |
| 66189 | 66810 | case OP_OpenPseudo: { |
| 66190 | | -#if 0 /* local variables moved into u.ay */ |
| 66811 | +#if 0 /* local variables moved into u.az */ |
| 66191 | 66812 | VdbeCursor *pCx; |
| 66192 | | -#endif /* local variables moved into u.ay */ |
| 66813 | +#endif /* local variables moved into u.az */ |
| 66193 | 66814 | |
| 66194 | 66815 | assert( pOp->p1>=0 ); |
| 66195 | | - u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0); |
| 66196 | | - if( u.ay.pCx==0 ) goto no_mem; |
| 66197 | | - u.ay.pCx->nullRow = 1; |
| 66198 | | - u.ay.pCx->pseudoTableReg = pOp->p2; |
| 66199 | | - u.ay.pCx->isTable = 1; |
| 66200 | | - u.ay.pCx->isIndex = 0; |
| 66816 | + u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0); |
| 66817 | + if( u.az.pCx==0 ) goto no_mem; |
| 66818 | + u.az.pCx->nullRow = 1; |
| 66819 | + u.az.pCx->pseudoTableReg = pOp->p2; |
| 66820 | + u.az.pCx->isTable = 1; |
| 66821 | + u.az.pCx->isIndex = 0; |
| 66201 | 66822 | break; |
| 66202 | 66823 | } |
| 66203 | 66824 | |
| 66204 | 66825 | /* Opcode: Close P1 * * * * |
| 66205 | 66826 | ** |
| | @@ -66267,39 +66888,39 @@ |
| 66267 | 66888 | */ |
| 66268 | 66889 | case OP_SeekLt: /* jump, in3 */ |
| 66269 | 66890 | case OP_SeekLe: /* jump, in3 */ |
| 66270 | 66891 | case OP_SeekGe: /* jump, in3 */ |
| 66271 | 66892 | case OP_SeekGt: { /* jump, in3 */ |
| 66272 | | -#if 0 /* local variables moved into u.az */ |
| 66893 | +#if 0 /* local variables moved into u.ba */ |
| 66273 | 66894 | int res; |
| 66274 | 66895 | int oc; |
| 66275 | 66896 | VdbeCursor *pC; |
| 66276 | 66897 | UnpackedRecord r; |
| 66277 | 66898 | int nField; |
| 66278 | 66899 | i64 iKey; /* The rowid we are to seek to */ |
| 66279 | | -#endif /* local variables moved into u.az */ |
| 66900 | +#endif /* local variables moved into u.ba */ |
| 66280 | 66901 | |
| 66281 | 66902 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 66282 | 66903 | assert( pOp->p2!=0 ); |
| 66283 | | - u.az.pC = p->apCsr[pOp->p1]; |
| 66284 | | - assert( u.az.pC!=0 ); |
| 66285 | | - assert( u.az.pC->pseudoTableReg==0 ); |
| 66904 | + u.ba.pC = p->apCsr[pOp->p1]; |
| 66905 | + assert( u.ba.pC!=0 ); |
| 66906 | + assert( u.ba.pC->pseudoTableReg==0 ); |
| 66286 | 66907 | assert( OP_SeekLe == OP_SeekLt+1 ); |
| 66287 | 66908 | assert( OP_SeekGe == OP_SeekLt+2 ); |
| 66288 | 66909 | assert( OP_SeekGt == OP_SeekLt+3 ); |
| 66289 | | - assert( u.az.pC->isOrdered ); |
| 66290 | | - if( u.az.pC->pCursor!=0 ){ |
| 66291 | | - u.az.oc = pOp->opcode; |
| 66292 | | - u.az.pC->nullRow = 0; |
| 66293 | | - if( u.az.pC->isTable ){ |
| 66910 | + assert( u.ba.pC->isOrdered ); |
| 66911 | + if( ALWAYS(u.ba.pC->pCursor!=0) ){ |
| 66912 | + u.ba.oc = pOp->opcode; |
| 66913 | + u.ba.pC->nullRow = 0; |
| 66914 | + if( u.ba.pC->isTable ){ |
| 66294 | 66915 | /* The input value in P3 might be of any type: integer, real, string, |
| 66295 | 66916 | ** blob, or NULL. But it needs to be an integer before we can do |
| 66296 | 66917 | ** the seek, so covert it. */ |
| 66297 | 66918 | pIn3 = &aMem[pOp->p3]; |
| 66298 | 66919 | applyNumericAffinity(pIn3); |
| 66299 | | - u.az.iKey = sqlite3VdbeIntValue(pIn3); |
| 66300 | | - u.az.pC->rowidIsValid = 0; |
| 66920 | + u.ba.iKey = sqlite3VdbeIntValue(pIn3); |
| 66921 | + u.ba.pC->rowidIsValid = 0; |
| 66301 | 66922 | |
| 66302 | 66923 | /* If the P3 value could not be converted into an integer without |
| 66303 | 66924 | ** loss of information, then special processing is required... */ |
| 66304 | 66925 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 66305 | 66926 | if( (pIn3->flags & MEM_Real)==0 ){ |
| | @@ -66310,105 +66931,105 @@ |
| 66310 | 66931 | } |
| 66311 | 66932 | /* If we reach this point, then the P3 value must be a floating |
| 66312 | 66933 | ** point number. */ |
| 66313 | 66934 | assert( (pIn3->flags & MEM_Real)!=0 ); |
| 66314 | 66935 | |
| 66315 | | - if( u.az.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.az.iKey || pIn3->r>0) ){ |
| 66316 | | - /* The P3 value is too large in magnitude to be expressed as an |
| 66317 | | - ** integer. */ |
| 66318 | | - u.az.res = 1; |
| 66319 | | - if( pIn3->r<0 ){ |
| 66320 | | - if( u.az.oc>=OP_SeekGe ){ assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt ); |
| 66321 | | - rc = sqlite3BtreeFirst(u.az.pC->pCursor, &u.az.res); |
| 66322 | | - if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 66323 | | - } |
| 66324 | | - }else{ |
| 66325 | | - if( u.az.oc<=OP_SeekLe ){ assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe ); |
| 66326 | | - rc = sqlite3BtreeLast(u.az.pC->pCursor, &u.az.res); |
| 66327 | | - if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 66328 | | - } |
| 66329 | | - } |
| 66330 | | - if( u.az.res ){ |
| 66331 | | - pc = pOp->p2 - 1; |
| 66332 | | - } |
| 66333 | | - break; |
| 66334 | | - }else if( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekGe ){ |
| 66335 | | - /* Use the ceiling() function to convert real->int */ |
| 66336 | | - if( pIn3->r > (double)u.az.iKey ) u.az.iKey++; |
| 66337 | | - }else{ |
| 66338 | | - /* Use the floor() function to convert real->int */ |
| 66339 | | - assert( u.az.oc==OP_SeekLe || u.az.oc==OP_SeekGt ); |
| 66340 | | - if( pIn3->r < (double)u.az.iKey ) u.az.iKey--; |
| 66341 | | - } |
| 66342 | | - } |
| 66343 | | - rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, 0, (u64)u.az.iKey, 0, &u.az.res); |
| 66344 | | - if( rc!=SQLITE_OK ){ |
| 66345 | | - goto abort_due_to_error; |
| 66346 | | - } |
| 66347 | | - if( u.az.res==0 ){ |
| 66348 | | - u.az.pC->rowidIsValid = 1; |
| 66349 | | - u.az.pC->lastRowid = u.az.iKey; |
| 66350 | | - } |
| 66351 | | - }else{ |
| 66352 | | - u.az.nField = pOp->p4.i; |
| 66353 | | - assert( pOp->p4type==P4_INT32 ); |
| 66354 | | - assert( u.az.nField>0 ); |
| 66355 | | - u.az.r.pKeyInfo = u.az.pC->pKeyInfo; |
| 66356 | | - u.az.r.nField = (u16)u.az.nField; |
| 66357 | | - |
| 66358 | | - /* The next line of code computes as follows, only faster: |
| 66359 | | - ** if( u.az.oc==OP_SeekGt || u.az.oc==OP_SeekLe ){ |
| 66360 | | - ** u.az.r.flags = UNPACKED_INCRKEY; |
| 66361 | | - ** }else{ |
| 66362 | | - ** u.az.r.flags = 0; |
| 66363 | | - ** } |
| 66364 | | - */ |
| 66365 | | - u.az.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.az.oc - OP_SeekLt))); |
| 66366 | | - assert( u.az.oc!=OP_SeekGt || u.az.r.flags==UNPACKED_INCRKEY ); |
| 66367 | | - assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY ); |
| 66368 | | - assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 ); |
| 66369 | | - assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 ); |
| 66370 | | - |
| 66371 | | - u.az.r.aMem = &aMem[pOp->p3]; |
| 66372 | | -#ifdef SQLITE_DEBUG |
| 66373 | | - { int i; for(i=0; i<u.az.r.nField; i++) assert( memIsValid(&u.az.r.aMem[i]) ); } |
| 66374 | | -#endif |
| 66375 | | - ExpandBlob(u.az.r.aMem); |
| 66376 | | - rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res); |
| 66377 | | - if( rc!=SQLITE_OK ){ |
| 66378 | | - goto abort_due_to_error; |
| 66379 | | - } |
| 66380 | | - u.az.pC->rowidIsValid = 0; |
| 66381 | | - } |
| 66382 | | - u.az.pC->deferredMoveto = 0; |
| 66383 | | - u.az.pC->cacheStatus = CACHE_STALE; |
| 66384 | | -#ifdef SQLITE_TEST |
| 66385 | | - sqlite3_search_count++; |
| 66386 | | -#endif |
| 66387 | | - if( u.az.oc>=OP_SeekGe ){ assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt ); |
| 66388 | | - if( u.az.res<0 || (u.az.res==0 && u.az.oc==OP_SeekGt) ){ |
| 66389 | | - rc = sqlite3BtreeNext(u.az.pC->pCursor, &u.az.res); |
| 66390 | | - if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 66391 | | - u.az.pC->rowidIsValid = 0; |
| 66392 | | - }else{ |
| 66393 | | - u.az.res = 0; |
| 66394 | | - } |
| 66395 | | - }else{ |
| 66396 | | - assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe ); |
| 66397 | | - if( u.az.res>0 || (u.az.res==0 && u.az.oc==OP_SeekLt) ){ |
| 66398 | | - rc = sqlite3BtreePrevious(u.az.pC->pCursor, &u.az.res); |
| 66399 | | - if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 66400 | | - u.az.pC->rowidIsValid = 0; |
| 66401 | | - }else{ |
| 66402 | | - /* u.az.res might be negative because the table is empty. Check to |
| 66403 | | - ** see if this is the case. |
| 66404 | | - */ |
| 66405 | | - u.az.res = sqlite3BtreeEof(u.az.pC->pCursor); |
| 66406 | | - } |
| 66407 | | - } |
| 66408 | | - assert( pOp->p2>0 ); |
| 66409 | | - if( u.az.res ){ |
| 66936 | + if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){ |
| 66937 | + /* The P3 value is too large in magnitude to be expressed as an |
| 66938 | + ** integer. */ |
| 66939 | + u.ba.res = 1; |
| 66940 | + if( pIn3->r<0 ){ |
| 66941 | + if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt ); |
| 66942 | + rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res); |
| 66943 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 66944 | + } |
| 66945 | + }else{ |
| 66946 | + if( u.ba.oc<=OP_SeekLe ){ assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe ); |
| 66947 | + rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res); |
| 66948 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 66949 | + } |
| 66950 | + } |
| 66951 | + if( u.ba.res ){ |
| 66952 | + pc = pOp->p2 - 1; |
| 66953 | + } |
| 66954 | + break; |
| 66955 | + }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){ |
| 66956 | + /* Use the ceiling() function to convert real->int */ |
| 66957 | + if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++; |
| 66958 | + }else{ |
| 66959 | + /* Use the floor() function to convert real->int */ |
| 66960 | + assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt ); |
| 66961 | + if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--; |
| 66962 | + } |
| 66963 | + } |
| 66964 | + rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res); |
| 66965 | + if( rc!=SQLITE_OK ){ |
| 66966 | + goto abort_due_to_error; |
| 66967 | + } |
| 66968 | + if( u.ba.res==0 ){ |
| 66969 | + u.ba.pC->rowidIsValid = 1; |
| 66970 | + u.ba.pC->lastRowid = u.ba.iKey; |
| 66971 | + } |
| 66972 | + }else{ |
| 66973 | + u.ba.nField = pOp->p4.i; |
| 66974 | + assert( pOp->p4type==P4_INT32 ); |
| 66975 | + assert( u.ba.nField>0 ); |
| 66976 | + u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo; |
| 66977 | + u.ba.r.nField = (u16)u.ba.nField; |
| 66978 | + |
| 66979 | + /* The next line of code computes as follows, only faster: |
| 66980 | + ** if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){ |
| 66981 | + ** u.ba.r.flags = UNPACKED_INCRKEY; |
| 66982 | + ** }else{ |
| 66983 | + ** u.ba.r.flags = 0; |
| 66984 | + ** } |
| 66985 | + */ |
| 66986 | + u.ba.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.ba.oc - OP_SeekLt))); |
| 66987 | + assert( u.ba.oc!=OP_SeekGt || u.ba.r.flags==UNPACKED_INCRKEY ); |
| 66988 | + assert( u.ba.oc!=OP_SeekLe || u.ba.r.flags==UNPACKED_INCRKEY ); |
| 66989 | + assert( u.ba.oc!=OP_SeekGe || u.ba.r.flags==0 ); |
| 66990 | + assert( u.ba.oc!=OP_SeekLt || u.ba.r.flags==0 ); |
| 66991 | + |
| 66992 | + u.ba.r.aMem = &aMem[pOp->p3]; |
| 66993 | +#ifdef SQLITE_DEBUG |
| 66994 | + { int i; for(i=0; i<u.ba.r.nField; i++) assert( memIsValid(&u.ba.r.aMem[i]) ); } |
| 66995 | +#endif |
| 66996 | + ExpandBlob(u.ba.r.aMem); |
| 66997 | + rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res); |
| 66998 | + if( rc!=SQLITE_OK ){ |
| 66999 | + goto abort_due_to_error; |
| 67000 | + } |
| 67001 | + u.ba.pC->rowidIsValid = 0; |
| 67002 | + } |
| 67003 | + u.ba.pC->deferredMoveto = 0; |
| 67004 | + u.ba.pC->cacheStatus = CACHE_STALE; |
| 67005 | +#ifdef SQLITE_TEST |
| 67006 | + sqlite3_search_count++; |
| 67007 | +#endif |
| 67008 | + if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt ); |
| 67009 | + if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){ |
| 67010 | + rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res); |
| 67011 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 67012 | + u.ba.pC->rowidIsValid = 0; |
| 67013 | + }else{ |
| 67014 | + u.ba.res = 0; |
| 67015 | + } |
| 67016 | + }else{ |
| 67017 | + assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe ); |
| 67018 | + if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){ |
| 67019 | + rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res); |
| 67020 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 67021 | + u.ba.pC->rowidIsValid = 0; |
| 67022 | + }else{ |
| 67023 | + /* u.ba.res might be negative because the table is empty. Check to |
| 67024 | + ** see if this is the case. |
| 67025 | + */ |
| 67026 | + u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor); |
| 67027 | + } |
| 67028 | + } |
| 67029 | + assert( pOp->p2>0 ); |
| 67030 | + if( u.ba.res ){ |
| 66410 | 67031 | pc = pOp->p2 - 1; |
| 66411 | 67032 | } |
| 66412 | 67033 | }else{ |
| 66413 | 67034 | /* This happens when attempting to open the sqlite3_master table |
| 66414 | 67035 | ** for read access returns SQLITE_EMPTY. In this case always |
| | @@ -66427,24 +67048,24 @@ |
| 66427 | 67048 | ** This is actually a deferred seek. Nothing actually happens until |
| 66428 | 67049 | ** the cursor is used to read a record. That way, if no reads |
| 66429 | 67050 | ** occur, no unnecessary I/O happens. |
| 66430 | 67051 | */ |
| 66431 | 67052 | case OP_Seek: { /* in2 */ |
| 66432 | | -#if 0 /* local variables moved into u.ba */ |
| 67053 | +#if 0 /* local variables moved into u.bb */ |
| 66433 | 67054 | VdbeCursor *pC; |
| 66434 | | -#endif /* local variables moved into u.ba */ |
| 67055 | +#endif /* local variables moved into u.bb */ |
| 66435 | 67056 | |
| 66436 | 67057 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 66437 | | - u.ba.pC = p->apCsr[pOp->p1]; |
| 66438 | | - assert( u.ba.pC!=0 ); |
| 66439 | | - if( ALWAYS(u.ba.pC->pCursor!=0) ){ |
| 66440 | | - assert( u.ba.pC->isTable ); |
| 66441 | | - u.ba.pC->nullRow = 0; |
| 67058 | + u.bb.pC = p->apCsr[pOp->p1]; |
| 67059 | + assert( u.bb.pC!=0 ); |
| 67060 | + if( ALWAYS(u.bb.pC->pCursor!=0) ){ |
| 67061 | + assert( u.bb.pC->isTable ); |
| 67062 | + u.bb.pC->nullRow = 0; |
| 66442 | 67063 | pIn2 = &aMem[pOp->p2]; |
| 66443 | | - u.ba.pC->movetoTarget = sqlite3VdbeIntValue(pIn2); |
| 66444 | | - u.ba.pC->rowidIsValid = 0; |
| 66445 | | - u.ba.pC->deferredMoveto = 1; |
| 67064 | + u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2); |
| 67065 | + u.bb.pC->rowidIsValid = 0; |
| 67066 | + u.bb.pC->deferredMoveto = 1; |
| 66446 | 67067 | } |
| 66447 | 67068 | break; |
| 66448 | 67069 | } |
| 66449 | 67070 | |
| 66450 | 67071 | |
| | @@ -66472,66 +67093,66 @@ |
| 66472 | 67093 | ** |
| 66473 | 67094 | ** See also: Found, NotExists, IsUnique |
| 66474 | 67095 | */ |
| 66475 | 67096 | case OP_NotFound: /* jump, in3 */ |
| 66476 | 67097 | case OP_Found: { /* jump, in3 */ |
| 66477 | | -#if 0 /* local variables moved into u.bb */ |
| 67098 | +#if 0 /* local variables moved into u.bc */ |
| 66478 | 67099 | int alreadyExists; |
| 66479 | 67100 | VdbeCursor *pC; |
| 66480 | 67101 | int res; |
| 66481 | 67102 | UnpackedRecord *pIdxKey; |
| 66482 | 67103 | UnpackedRecord r; |
| 66483 | 67104 | char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7]; |
| 66484 | | -#endif /* local variables moved into u.bb */ |
| 67105 | +#endif /* local variables moved into u.bc */ |
| 66485 | 67106 | |
| 66486 | 67107 | #ifdef SQLITE_TEST |
| 66487 | 67108 | sqlite3_found_count++; |
| 66488 | 67109 | #endif |
| 66489 | 67110 | |
| 66490 | | - u.bb.alreadyExists = 0; |
| 67111 | + u.bc.alreadyExists = 0; |
| 66491 | 67112 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 66492 | 67113 | assert( pOp->p4type==P4_INT32 ); |
| 66493 | | - u.bb.pC = p->apCsr[pOp->p1]; |
| 66494 | | - assert( u.bb.pC!=0 ); |
| 67114 | + u.bc.pC = p->apCsr[pOp->p1]; |
| 67115 | + assert( u.bc.pC!=0 ); |
| 66495 | 67116 | pIn3 = &aMem[pOp->p3]; |
| 66496 | | - if( ALWAYS(u.bb.pC->pCursor!=0) ){ |
| 67117 | + if( ALWAYS(u.bc.pC->pCursor!=0) ){ |
| 66497 | 67118 | |
| 66498 | | - assert( u.bb.pC->isTable==0 ); |
| 67119 | + assert( u.bc.pC->isTable==0 ); |
| 66499 | 67120 | if( pOp->p4.i>0 ){ |
| 66500 | | - u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo; |
| 66501 | | - u.bb.r.nField = (u16)pOp->p4.i; |
| 66502 | | - u.bb.r.aMem = pIn3; |
| 67121 | + u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo; |
| 67122 | + u.bc.r.nField = (u16)pOp->p4.i; |
| 67123 | + u.bc.r.aMem = pIn3; |
| 66503 | 67124 | #ifdef SQLITE_DEBUG |
| 66504 | | - { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); } |
| 67125 | + { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); } |
| 66505 | 67126 | #endif |
| 66506 | | - u.bb.r.flags = UNPACKED_PREFIX_MATCH; |
| 66507 | | - u.bb.pIdxKey = &u.bb.r; |
| 67127 | + u.bc.r.flags = UNPACKED_PREFIX_MATCH; |
| 67128 | + u.bc.pIdxKey = &u.bc.r; |
| 66508 | 67129 | }else{ |
| 66509 | 67130 | assert( pIn3->flags & MEM_Blob ); |
| 66510 | 67131 | assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ |
| 66511 | | - u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z, |
| 66512 | | - u.bb.aTempRec, sizeof(u.bb.aTempRec)); |
| 66513 | | - if( u.bb.pIdxKey==0 ){ |
| 67132 | + u.bc.pIdxKey = sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z, |
| 67133 | + u.bc.aTempRec, sizeof(u.bc.aTempRec)); |
| 67134 | + if( u.bc.pIdxKey==0 ){ |
| 66514 | 67135 | goto no_mem; |
| 66515 | 67136 | } |
| 66516 | | - u.bb.pIdxKey->flags |= UNPACKED_PREFIX_MATCH; |
| 67137 | + u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH; |
| 66517 | 67138 | } |
| 66518 | | - rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, u.bb.pIdxKey, 0, 0, &u.bb.res); |
| 67139 | + rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res); |
| 66519 | 67140 | if( pOp->p4.i==0 ){ |
| 66520 | | - sqlite3VdbeDeleteUnpackedRecord(u.bb.pIdxKey); |
| 67141 | + sqlite3VdbeDeleteUnpackedRecord(u.bc.pIdxKey); |
| 66521 | 67142 | } |
| 66522 | 67143 | if( rc!=SQLITE_OK ){ |
| 66523 | 67144 | break; |
| 66524 | 67145 | } |
| 66525 | | - u.bb.alreadyExists = (u.bb.res==0); |
| 66526 | | - u.bb.pC->deferredMoveto = 0; |
| 66527 | | - u.bb.pC->cacheStatus = CACHE_STALE; |
| 67146 | + u.bc.alreadyExists = (u.bc.res==0); |
| 67147 | + u.bc.pC->deferredMoveto = 0; |
| 67148 | + u.bc.pC->cacheStatus = CACHE_STALE; |
| 66528 | 67149 | } |
| 66529 | 67150 | if( pOp->opcode==OP_Found ){ |
| 66530 | | - if( u.bb.alreadyExists ) pc = pOp->p2 - 1; |
| 67151 | + if( u.bc.alreadyExists ) pc = pOp->p2 - 1; |
| 66531 | 67152 | }else{ |
| 66532 | | - if( !u.bb.alreadyExists ) pc = pOp->p2 - 1; |
| 67153 | + if( !u.bc.alreadyExists ) pc = pOp->p2 - 1; |
| 66533 | 67154 | } |
| 66534 | 67155 | break; |
| 66535 | 67156 | } |
| 66536 | 67157 | |
| 66537 | 67158 | /* Opcode: IsUnique P1 P2 P3 P4 * |
| | @@ -66559,67 +67180,67 @@ |
| 66559 | 67180 | ** instruction. |
| 66560 | 67181 | ** |
| 66561 | 67182 | ** See also: NotFound, NotExists, Found |
| 66562 | 67183 | */ |
| 66563 | 67184 | case OP_IsUnique: { /* jump, in3 */ |
| 66564 | | -#if 0 /* local variables moved into u.bc */ |
| 67185 | +#if 0 /* local variables moved into u.bd */ |
| 66565 | 67186 | u16 ii; |
| 66566 | 67187 | VdbeCursor *pCx; |
| 66567 | 67188 | BtCursor *pCrsr; |
| 66568 | 67189 | u16 nField; |
| 66569 | 67190 | Mem *aMx; |
| 66570 | 67191 | UnpackedRecord r; /* B-Tree index search key */ |
| 66571 | 67192 | i64 R; /* Rowid stored in register P3 */ |
| 66572 | | -#endif /* local variables moved into u.bc */ |
| 67193 | +#endif /* local variables moved into u.bd */ |
| 66573 | 67194 | |
| 66574 | 67195 | pIn3 = &aMem[pOp->p3]; |
| 66575 | | - u.bc.aMx = &aMem[pOp->p4.i]; |
| 67196 | + u.bd.aMx = &aMem[pOp->p4.i]; |
| 66576 | 67197 | /* Assert that the values of parameters P1 and P4 are in range. */ |
| 66577 | 67198 | assert( pOp->p4type==P4_INT32 ); |
| 66578 | 67199 | assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem ); |
| 66579 | 67200 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 66580 | 67201 | |
| 66581 | 67202 | /* Find the index cursor. */ |
| 66582 | | - u.bc.pCx = p->apCsr[pOp->p1]; |
| 66583 | | - assert( u.bc.pCx->deferredMoveto==0 ); |
| 66584 | | - u.bc.pCx->seekResult = 0; |
| 66585 | | - u.bc.pCx->cacheStatus = CACHE_STALE; |
| 66586 | | - u.bc.pCrsr = u.bc.pCx->pCursor; |
| 67203 | + u.bd.pCx = p->apCsr[pOp->p1]; |
| 67204 | + assert( u.bd.pCx->deferredMoveto==0 ); |
| 67205 | + u.bd.pCx->seekResult = 0; |
| 67206 | + u.bd.pCx->cacheStatus = CACHE_STALE; |
| 67207 | + u.bd.pCrsr = u.bd.pCx->pCursor; |
| 66587 | 67208 | |
| 66588 | 67209 | /* If any of the values are NULL, take the jump. */ |
| 66589 | | - u.bc.nField = u.bc.pCx->pKeyInfo->nField; |
| 66590 | | - for(u.bc.ii=0; u.bc.ii<u.bc.nField; u.bc.ii++){ |
| 66591 | | - if( u.bc.aMx[u.bc.ii].flags & MEM_Null ){ |
| 67210 | + u.bd.nField = u.bd.pCx->pKeyInfo->nField; |
| 67211 | + for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){ |
| 67212 | + if( u.bd.aMx[u.bd.ii].flags & MEM_Null ){ |
| 66592 | 67213 | pc = pOp->p2 - 1; |
| 66593 | | - u.bc.pCrsr = 0; |
| 67214 | + u.bd.pCrsr = 0; |
| 66594 | 67215 | break; |
| 66595 | 67216 | } |
| 66596 | 67217 | } |
| 66597 | | - assert( (u.bc.aMx[u.bc.nField].flags & MEM_Null)==0 ); |
| 67218 | + assert( (u.bd.aMx[u.bd.nField].flags & MEM_Null)==0 ); |
| 66598 | 67219 | |
| 66599 | | - if( u.bc.pCrsr!=0 ){ |
| 67220 | + if( u.bd.pCrsr!=0 ){ |
| 66600 | 67221 | /* Populate the index search key. */ |
| 66601 | | - u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo; |
| 66602 | | - u.bc.r.nField = u.bc.nField + 1; |
| 66603 | | - u.bc.r.flags = UNPACKED_PREFIX_SEARCH; |
| 66604 | | - u.bc.r.aMem = u.bc.aMx; |
| 67222 | + u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo; |
| 67223 | + u.bd.r.nField = u.bd.nField + 1; |
| 67224 | + u.bd.r.flags = UNPACKED_PREFIX_SEARCH; |
| 67225 | + u.bd.r.aMem = u.bd.aMx; |
| 66605 | 67226 | #ifdef SQLITE_DEBUG |
| 66606 | | - { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); } |
| 67227 | + { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); } |
| 66607 | 67228 | #endif |
| 66608 | 67229 | |
| 66609 | | - /* Extract the value of u.bc.R from register P3. */ |
| 67230 | + /* Extract the value of u.bd.R from register P3. */ |
| 66610 | 67231 | sqlite3VdbeMemIntegerify(pIn3); |
| 66611 | | - u.bc.R = pIn3->u.i; |
| 67232 | + u.bd.R = pIn3->u.i; |
| 66612 | 67233 | |
| 66613 | 67234 | /* Search the B-Tree index. If no conflicting record is found, jump |
| 66614 | 67235 | ** to P2. Otherwise, copy the rowid of the conflicting record to |
| 66615 | 67236 | ** register P3 and fall through to the next instruction. */ |
| 66616 | | - rc = sqlite3BtreeMovetoUnpacked(u.bc.pCrsr, &u.bc.r, 0, 0, &u.bc.pCx->seekResult); |
| 66617 | | - if( (u.bc.r.flags & UNPACKED_PREFIX_SEARCH) || u.bc.r.rowid==u.bc.R ){ |
| 67237 | + rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult); |
| 67238 | + if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){ |
| 66618 | 67239 | pc = pOp->p2 - 1; |
| 66619 | 67240 | }else{ |
| 66620 | | - pIn3->u.i = u.bc.r.rowid; |
| 67241 | + pIn3->u.i = u.bd.r.rowid; |
| 66621 | 67242 | } |
| 66622 | 67243 | } |
| 66623 | 67244 | break; |
| 66624 | 67245 | } |
| 66625 | 67246 | |
| | @@ -66636,46 +67257,46 @@ |
| 66636 | 67257 | ** P1 is an index. |
| 66637 | 67258 | ** |
| 66638 | 67259 | ** See also: Found, NotFound, IsUnique |
| 66639 | 67260 | */ |
| 66640 | 67261 | case OP_NotExists: { /* jump, in3 */ |
| 66641 | | -#if 0 /* local variables moved into u.bd */ |
| 67262 | +#if 0 /* local variables moved into u.be */ |
| 66642 | 67263 | VdbeCursor *pC; |
| 66643 | 67264 | BtCursor *pCrsr; |
| 66644 | 67265 | int res; |
| 66645 | 67266 | u64 iKey; |
| 66646 | | -#endif /* local variables moved into u.bd */ |
| 67267 | +#endif /* local variables moved into u.be */ |
| 66647 | 67268 | |
| 66648 | 67269 | pIn3 = &aMem[pOp->p3]; |
| 66649 | 67270 | assert( pIn3->flags & MEM_Int ); |
| 66650 | 67271 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 66651 | | - u.bd.pC = p->apCsr[pOp->p1]; |
| 66652 | | - assert( u.bd.pC!=0 ); |
| 66653 | | - assert( u.bd.pC->isTable ); |
| 66654 | | - assert( u.bd.pC->pseudoTableReg==0 ); |
| 66655 | | - u.bd.pCrsr = u.bd.pC->pCursor; |
| 66656 | | - if( u.bd.pCrsr!=0 ){ |
| 66657 | | - u.bd.res = 0; |
| 66658 | | - u.bd.iKey = pIn3->u.i; |
| 66659 | | - rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res); |
| 66660 | | - u.bd.pC->lastRowid = pIn3->u.i; |
| 66661 | | - u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0; |
| 66662 | | - u.bd.pC->nullRow = 0; |
| 66663 | | - u.bd.pC->cacheStatus = CACHE_STALE; |
| 66664 | | - u.bd.pC->deferredMoveto = 0; |
| 66665 | | - if( u.bd.res!=0 ){ |
| 67272 | + u.be.pC = p->apCsr[pOp->p1]; |
| 67273 | + assert( u.be.pC!=0 ); |
| 67274 | + assert( u.be.pC->isTable ); |
| 67275 | + assert( u.be.pC->pseudoTableReg==0 ); |
| 67276 | + u.be.pCrsr = u.be.pC->pCursor; |
| 67277 | + if( ALWAYS(u.be.pCrsr!=0) ){ |
| 67278 | + u.be.res = 0; |
| 67279 | + u.be.iKey = pIn3->u.i; |
| 67280 | + rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res); |
| 67281 | + u.be.pC->lastRowid = pIn3->u.i; |
| 67282 | + u.be.pC->rowidIsValid = u.be.res==0 ?1:0; |
| 67283 | + u.be.pC->nullRow = 0; |
| 67284 | + u.be.pC->cacheStatus = CACHE_STALE; |
| 67285 | + u.be.pC->deferredMoveto = 0; |
| 67286 | + if( u.be.res!=0 ){ |
| 66666 | 67287 | pc = pOp->p2 - 1; |
| 66667 | | - assert( u.bd.pC->rowidIsValid==0 ); |
| 67288 | + assert( u.be.pC->rowidIsValid==0 ); |
| 66668 | 67289 | } |
| 66669 | | - u.bd.pC->seekResult = u.bd.res; |
| 67290 | + u.be.pC->seekResult = u.be.res; |
| 66670 | 67291 | }else{ |
| 66671 | 67292 | /* This happens when an attempt to open a read cursor on the |
| 66672 | 67293 | ** sqlite_master table returns SQLITE_EMPTY. |
| 66673 | 67294 | */ |
| 66674 | 67295 | pc = pOp->p2 - 1; |
| 66675 | | - assert( u.bd.pC->rowidIsValid==0 ); |
| 66676 | | - u.bd.pC->seekResult = 0; |
| 67296 | + assert( u.be.pC->rowidIsValid==0 ); |
| 67297 | + u.be.pC->seekResult = 0; |
| 66677 | 67298 | } |
| 66678 | 67299 | break; |
| 66679 | 67300 | } |
| 66680 | 67301 | |
| 66681 | 67302 | /* Opcode: Sequence P1 P2 * * * |
| | @@ -66706,25 +67327,25 @@ |
| 66706 | 67327 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
| 66707 | 67328 | ** generated record number. This P3 mechanism is used to help implement the |
| 66708 | 67329 | ** AUTOINCREMENT feature. |
| 66709 | 67330 | */ |
| 66710 | 67331 | case OP_NewRowid: { /* out2-prerelease */ |
| 66711 | | -#if 0 /* local variables moved into u.be */ |
| 67332 | +#if 0 /* local variables moved into u.bf */ |
| 66712 | 67333 | i64 v; /* The new rowid */ |
| 66713 | 67334 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 66714 | 67335 | int res; /* Result of an sqlite3BtreeLast() */ |
| 66715 | 67336 | int cnt; /* Counter to limit the number of searches */ |
| 66716 | 67337 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
| 66717 | 67338 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
| 66718 | | -#endif /* local variables moved into u.be */ |
| 67339 | +#endif /* local variables moved into u.bf */ |
| 66719 | 67340 | |
| 66720 | | - u.be.v = 0; |
| 66721 | | - u.be.res = 0; |
| 67341 | + u.bf.v = 0; |
| 67342 | + u.bf.res = 0; |
| 66722 | 67343 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 66723 | | - u.be.pC = p->apCsr[pOp->p1]; |
| 66724 | | - assert( u.be.pC!=0 ); |
| 66725 | | - if( NEVER(u.be.pC->pCursor==0) ){ |
| 67344 | + u.bf.pC = p->apCsr[pOp->p1]; |
| 67345 | + assert( u.bf.pC!=0 ); |
| 67346 | + if( NEVER(u.bf.pC->pCursor==0) ){ |
| 66726 | 67347 | /* The zero initialization above is all that is needed */ |
| 66727 | 67348 | }else{ |
| 66728 | 67349 | /* The next rowid or record number (different terms for the same |
| 66729 | 67350 | ** thing) is obtained in a two-step algorithm. |
| 66730 | 67351 | ** |
| | @@ -66736,11 +67357,11 @@ |
| 66736 | 67357 | ** The second algorithm is to select a rowid at random and see if |
| 66737 | 67358 | ** it already exists in the table. If it does not exist, we have |
| 66738 | 67359 | ** succeeded. If the random rowid does exist, we select a new one |
| 66739 | 67360 | ** and try again, up to 100 times. |
| 66740 | 67361 | */ |
| 66741 | | - assert( u.be.pC->isTable ); |
| 67362 | + assert( u.bf.pC->isTable ); |
| 66742 | 67363 | |
| 66743 | 67364 | #ifdef SQLITE_32BIT_ROWID |
| 66744 | 67365 | # define MAX_ROWID 0x7fffffff |
| 66745 | 67366 | #else |
| 66746 | 67367 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| | @@ -66748,101 +67369,101 @@ |
| 66748 | 67369 | ** to provide the constant while making all compilers happy. |
| 66749 | 67370 | */ |
| 66750 | 67371 | # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
| 66751 | 67372 | #endif |
| 66752 | 67373 | |
| 66753 | | - if( !u.be.pC->useRandomRowid ){ |
| 66754 | | - u.be.v = sqlite3BtreeGetCachedRowid(u.be.pC->pCursor); |
| 66755 | | - if( u.be.v==0 ){ |
| 66756 | | - rc = sqlite3BtreeLast(u.be.pC->pCursor, &u.be.res); |
| 67374 | + if( !u.bf.pC->useRandomRowid ){ |
| 67375 | + u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor); |
| 67376 | + if( u.bf.v==0 ){ |
| 67377 | + rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res); |
| 66757 | 67378 | if( rc!=SQLITE_OK ){ |
| 66758 | 67379 | goto abort_due_to_error; |
| 66759 | 67380 | } |
| 66760 | | - if( u.be.res ){ |
| 66761 | | - u.be.v = 1; /* IMP: R-61914-48074 */ |
| 67381 | + if( u.bf.res ){ |
| 67382 | + u.bf.v = 1; /* IMP: R-61914-48074 */ |
| 66762 | 67383 | }else{ |
| 66763 | | - assert( sqlite3BtreeCursorIsValid(u.be.pC->pCursor) ); |
| 66764 | | - rc = sqlite3BtreeKeySize(u.be.pC->pCursor, &u.be.v); |
| 67384 | + assert( sqlite3BtreeCursorIsValid(u.bf.pC->pCursor) ); |
| 67385 | + rc = sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v); |
| 66765 | 67386 | assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */ |
| 66766 | | - if( u.be.v==MAX_ROWID ){ |
| 66767 | | - u.be.pC->useRandomRowid = 1; |
| 67387 | + if( u.bf.v==MAX_ROWID ){ |
| 67388 | + u.bf.pC->useRandomRowid = 1; |
| 66768 | 67389 | }else{ |
| 66769 | | - u.be.v++; /* IMP: R-29538-34987 */ |
| 67390 | + u.bf.v++; /* IMP: R-29538-34987 */ |
| 66770 | 67391 | } |
| 66771 | 67392 | } |
| 66772 | 67393 | } |
| 66773 | 67394 | |
| 66774 | 67395 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 66775 | 67396 | if( pOp->p3 ){ |
| 66776 | 67397 | /* Assert that P3 is a valid memory cell. */ |
| 66777 | 67398 | assert( pOp->p3>0 ); |
| 66778 | 67399 | if( p->pFrame ){ |
| 66779 | | - for(u.be.pFrame=p->pFrame; u.be.pFrame->pParent; u.be.pFrame=u.be.pFrame->pParent); |
| 67400 | + for(u.bf.pFrame=p->pFrame; u.bf.pFrame->pParent; u.bf.pFrame=u.bf.pFrame->pParent); |
| 66780 | 67401 | /* Assert that P3 is a valid memory cell. */ |
| 66781 | | - assert( pOp->p3<=u.be.pFrame->nMem ); |
| 66782 | | - u.be.pMem = &u.be.pFrame->aMem[pOp->p3]; |
| 67402 | + assert( pOp->p3<=u.bf.pFrame->nMem ); |
| 67403 | + u.bf.pMem = &u.bf.pFrame->aMem[pOp->p3]; |
| 66783 | 67404 | }else{ |
| 66784 | 67405 | /* Assert that P3 is a valid memory cell. */ |
| 66785 | 67406 | assert( pOp->p3<=p->nMem ); |
| 66786 | | - u.be.pMem = &aMem[pOp->p3]; |
| 66787 | | - memAboutToChange(p, u.be.pMem); |
| 66788 | | - } |
| 66789 | | - assert( memIsValid(u.be.pMem) ); |
| 66790 | | - |
| 66791 | | - REGISTER_TRACE(pOp->p3, u.be.pMem); |
| 66792 | | - sqlite3VdbeMemIntegerify(u.be.pMem); |
| 66793 | | - assert( (u.be.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 66794 | | - if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){ |
| 67407 | + u.bf.pMem = &aMem[pOp->p3]; |
| 67408 | + memAboutToChange(p, u.bf.pMem); |
| 67409 | + } |
| 67410 | + assert( memIsValid(u.bf.pMem) ); |
| 67411 | + |
| 67412 | + REGISTER_TRACE(pOp->p3, u.bf.pMem); |
| 67413 | + sqlite3VdbeMemIntegerify(u.bf.pMem); |
| 67414 | + assert( (u.bf.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
| 67415 | + if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){ |
| 66795 | 67416 | rc = SQLITE_FULL; /* IMP: R-12275-61338 */ |
| 66796 | 67417 | goto abort_due_to_error; |
| 66797 | 67418 | } |
| 66798 | | - if( u.be.v<u.be.pMem->u.i+1 ){ |
| 66799 | | - u.be.v = u.be.pMem->u.i + 1; |
| 67419 | + if( u.bf.v<u.bf.pMem->u.i+1 ){ |
| 67420 | + u.bf.v = u.bf.pMem->u.i + 1; |
| 66800 | 67421 | } |
| 66801 | | - u.be.pMem->u.i = u.be.v; |
| 67422 | + u.bf.pMem->u.i = u.bf.v; |
| 66802 | 67423 | } |
| 66803 | 67424 | #endif |
| 66804 | 67425 | |
| 66805 | | - sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0); |
| 67426 | + sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0); |
| 66806 | 67427 | } |
| 66807 | | - if( u.be.pC->useRandomRowid ){ |
| 67428 | + if( u.bf.pC->useRandomRowid ){ |
| 66808 | 67429 | /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
| 66809 | 67430 | ** largest possible integer (9223372036854775807) then the database |
| 66810 | 67431 | ** engine starts picking positive candidate ROWIDs at random until |
| 66811 | 67432 | ** it finds one that is not previously used. */ |
| 66812 | 67433 | assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
| 66813 | 67434 | ** an AUTOINCREMENT table. */ |
| 66814 | 67435 | /* on the first attempt, simply do one more than previous */ |
| 66815 | | - u.be.v = lastRowid; |
| 66816 | | - u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 66817 | | - u.be.v++; /* ensure non-zero */ |
| 66818 | | - u.be.cnt = 0; |
| 66819 | | - while( ((rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v, |
| 66820 | | - 0, &u.be.res))==SQLITE_OK) |
| 66821 | | - && (u.be.res==0) |
| 66822 | | - && (++u.be.cnt<100)){ |
| 67436 | + u.bf.v = lastRowid; |
| 67437 | + u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 67438 | + u.bf.v++; /* ensure non-zero */ |
| 67439 | + u.bf.cnt = 0; |
| 67440 | + while( ((rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v, |
| 67441 | + 0, &u.bf.res))==SQLITE_OK) |
| 67442 | + && (u.bf.res==0) |
| 67443 | + && (++u.bf.cnt<100)){ |
| 66823 | 67444 | /* collision - try another random rowid */ |
| 66824 | | - sqlite3_randomness(sizeof(u.be.v), &u.be.v); |
| 66825 | | - if( u.be.cnt<5 ){ |
| 67445 | + sqlite3_randomness(sizeof(u.bf.v), &u.bf.v); |
| 67446 | + if( u.bf.cnt<5 ){ |
| 66826 | 67447 | /* try "small" random rowids for the initial attempts */ |
| 66827 | | - u.be.v &= 0xffffff; |
| 67448 | + u.bf.v &= 0xffffff; |
| 66828 | 67449 | }else{ |
| 66829 | | - u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 67450 | + u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */ |
| 66830 | 67451 | } |
| 66831 | | - u.be.v++; /* ensure non-zero */ |
| 67452 | + u.bf.v++; /* ensure non-zero */ |
| 66832 | 67453 | } |
| 66833 | | - if( rc==SQLITE_OK && u.be.res==0 ){ |
| 67454 | + if( rc==SQLITE_OK && u.bf.res==0 ){ |
| 66834 | 67455 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
| 66835 | 67456 | goto abort_due_to_error; |
| 66836 | 67457 | } |
| 66837 | | - assert( u.be.v>0 ); /* EV: R-40812-03570 */ |
| 67458 | + assert( u.bf.v>0 ); /* EV: R-40812-03570 */ |
| 66838 | 67459 | } |
| 66839 | | - u.be.pC->rowidIsValid = 0; |
| 66840 | | - u.be.pC->deferredMoveto = 0; |
| 66841 | | - u.be.pC->cacheStatus = CACHE_STALE; |
| 67460 | + u.bf.pC->rowidIsValid = 0; |
| 67461 | + u.bf.pC->deferredMoveto = 0; |
| 67462 | + u.bf.pC->cacheStatus = CACHE_STALE; |
| 66842 | 67463 | } |
| 66843 | | - pOut->u.i = u.be.v; |
| 67464 | + pOut->u.i = u.bf.v; |
| 66844 | 67465 | break; |
| 66845 | 67466 | } |
| 66846 | 67467 | |
| 66847 | 67468 | /* Opcode: Insert P1 P2 P3 P4 P5 |
| 66848 | 67469 | ** |
| | @@ -66888,74 +67509,74 @@ |
| 66888 | 67509 | ** This works exactly like OP_Insert except that the key is the |
| 66889 | 67510 | ** integer value P3, not the value of the integer stored in register P3. |
| 66890 | 67511 | */ |
| 66891 | 67512 | case OP_Insert: |
| 66892 | 67513 | case OP_InsertInt: { |
| 66893 | | -#if 0 /* local variables moved into u.bf */ |
| 67514 | +#if 0 /* local variables moved into u.bg */ |
| 66894 | 67515 | Mem *pData; /* MEM cell holding data for the record to be inserted */ |
| 66895 | 67516 | Mem *pKey; /* MEM cell holding key for the record */ |
| 66896 | 67517 | i64 iKey; /* The integer ROWID or key for the record to be inserted */ |
| 66897 | 67518 | VdbeCursor *pC; /* Cursor to table into which insert is written */ |
| 66898 | 67519 | int nZero; /* Number of zero-bytes to append */ |
| 66899 | 67520 | int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
| 66900 | 67521 | const char *zDb; /* database name - used by the update hook */ |
| 66901 | 67522 | const char *zTbl; /* Table name - used by the opdate hook */ |
| 66902 | 67523 | int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
| 66903 | | -#endif /* local variables moved into u.bf */ |
| 67524 | +#endif /* local variables moved into u.bg */ |
| 66904 | 67525 | |
| 66905 | | - u.bf.pData = &aMem[pOp->p2]; |
| 67526 | + u.bg.pData = &aMem[pOp->p2]; |
| 66906 | 67527 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 66907 | | - assert( memIsValid(u.bf.pData) ); |
| 66908 | | - u.bf.pC = p->apCsr[pOp->p1]; |
| 66909 | | - assert( u.bf.pC!=0 ); |
| 66910 | | - assert( u.bf.pC->pCursor!=0 ); |
| 66911 | | - assert( u.bf.pC->pseudoTableReg==0 ); |
| 66912 | | - assert( u.bf.pC->isTable ); |
| 66913 | | - REGISTER_TRACE(pOp->p2, u.bf.pData); |
| 67528 | + assert( memIsValid(u.bg.pData) ); |
| 67529 | + u.bg.pC = p->apCsr[pOp->p1]; |
| 67530 | + assert( u.bg.pC!=0 ); |
| 67531 | + assert( u.bg.pC->pCursor!=0 ); |
| 67532 | + assert( u.bg.pC->pseudoTableReg==0 ); |
| 67533 | + assert( u.bg.pC->isTable ); |
| 67534 | + REGISTER_TRACE(pOp->p2, u.bg.pData); |
| 66914 | 67535 | |
| 66915 | 67536 | if( pOp->opcode==OP_Insert ){ |
| 66916 | | - u.bf.pKey = &aMem[pOp->p3]; |
| 66917 | | - assert( u.bf.pKey->flags & MEM_Int ); |
| 66918 | | - assert( memIsValid(u.bf.pKey) ); |
| 66919 | | - REGISTER_TRACE(pOp->p3, u.bf.pKey); |
| 66920 | | - u.bf.iKey = u.bf.pKey->u.i; |
| 67537 | + u.bg.pKey = &aMem[pOp->p3]; |
| 67538 | + assert( u.bg.pKey->flags & MEM_Int ); |
| 67539 | + assert( memIsValid(u.bg.pKey) ); |
| 67540 | + REGISTER_TRACE(pOp->p3, u.bg.pKey); |
| 67541 | + u.bg.iKey = u.bg.pKey->u.i; |
| 66921 | 67542 | }else{ |
| 66922 | 67543 | assert( pOp->opcode==OP_InsertInt ); |
| 66923 | | - u.bf.iKey = pOp->p3; |
| 67544 | + u.bg.iKey = pOp->p3; |
| 66924 | 67545 | } |
| 66925 | 67546 | |
| 66926 | 67547 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
| 66927 | | - if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bf.iKey; |
| 66928 | | - if( u.bf.pData->flags & MEM_Null ){ |
| 66929 | | - u.bf.pData->z = 0; |
| 66930 | | - u.bf.pData->n = 0; |
| 66931 | | - }else{ |
| 66932 | | - assert( u.bf.pData->flags & (MEM_Blob|MEM_Str) ); |
| 66933 | | - } |
| 66934 | | - u.bf.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bf.pC->seekResult : 0); |
| 66935 | | - if( u.bf.pData->flags & MEM_Zero ){ |
| 66936 | | - u.bf.nZero = u.bf.pData->u.nZero; |
| 66937 | | - }else{ |
| 66938 | | - u.bf.nZero = 0; |
| 66939 | | - } |
| 66940 | | - sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, 0); |
| 66941 | | - rc = sqlite3BtreeInsert(u.bf.pC->pCursor, 0, u.bf.iKey, |
| 66942 | | - u.bf.pData->z, u.bf.pData->n, u.bf.nZero, |
| 66943 | | - pOp->p5 & OPFLAG_APPEND, u.bf.seekResult |
| 67548 | + if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bg.iKey; |
| 67549 | + if( u.bg.pData->flags & MEM_Null ){ |
| 67550 | + u.bg.pData->z = 0; |
| 67551 | + u.bg.pData->n = 0; |
| 67552 | + }else{ |
| 67553 | + assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) ); |
| 67554 | + } |
| 67555 | + u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0); |
| 67556 | + if( u.bg.pData->flags & MEM_Zero ){ |
| 67557 | + u.bg.nZero = u.bg.pData->u.nZero; |
| 67558 | + }else{ |
| 67559 | + u.bg.nZero = 0; |
| 67560 | + } |
| 67561 | + sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0); |
| 67562 | + rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey, |
| 67563 | + u.bg.pData->z, u.bg.pData->n, u.bg.nZero, |
| 67564 | + pOp->p5 & OPFLAG_APPEND, u.bg.seekResult |
| 66944 | 67565 | ); |
| 66945 | | - u.bf.pC->rowidIsValid = 0; |
| 66946 | | - u.bf.pC->deferredMoveto = 0; |
| 66947 | | - u.bf.pC->cacheStatus = CACHE_STALE; |
| 67566 | + u.bg.pC->rowidIsValid = 0; |
| 67567 | + u.bg.pC->deferredMoveto = 0; |
| 67568 | + u.bg.pC->cacheStatus = CACHE_STALE; |
| 66948 | 67569 | |
| 66949 | 67570 | /* Invoke the update-hook if required. */ |
| 66950 | 67571 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 66951 | | - u.bf.zDb = db->aDb[u.bf.pC->iDb].zName; |
| 66952 | | - u.bf.zTbl = pOp->p4.z; |
| 66953 | | - u.bf.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
| 66954 | | - assert( u.bf.pC->isTable ); |
| 66955 | | - db->xUpdateCallback(db->pUpdateArg, u.bf.op, u.bf.zDb, u.bf.zTbl, u.bf.iKey); |
| 66956 | | - assert( u.bf.pC->iDb>=0 ); |
| 67572 | + u.bg.zDb = db->aDb[u.bg.pC->iDb].zName; |
| 67573 | + u.bg.zTbl = pOp->p4.z; |
| 67574 | + u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
| 67575 | + assert( u.bg.pC->isTable ); |
| 67576 | + db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey); |
| 67577 | + assert( u.bg.pC->iDb>=0 ); |
| 66957 | 67578 | } |
| 66958 | 67579 | break; |
| 66959 | 67580 | } |
| 66960 | 67581 | |
| 66961 | 67582 | /* Opcode: Delete P1 P2 * P4 * |
| | @@ -66977,51 +67598,51 @@ |
| 66977 | 67598 | ** pointing to. The update hook will be invoked, if it exists. |
| 66978 | 67599 | ** If P4 is not NULL then the P1 cursor must have been positioned |
| 66979 | 67600 | ** using OP_NotFound prior to invoking this opcode. |
| 66980 | 67601 | */ |
| 66981 | 67602 | case OP_Delete: { |
| 66982 | | -#if 0 /* local variables moved into u.bg */ |
| 67603 | +#if 0 /* local variables moved into u.bh */ |
| 66983 | 67604 | i64 iKey; |
| 66984 | 67605 | VdbeCursor *pC; |
| 66985 | | -#endif /* local variables moved into u.bg */ |
| 67606 | +#endif /* local variables moved into u.bh */ |
| 66986 | 67607 | |
| 66987 | | - u.bg.iKey = 0; |
| 67608 | + u.bh.iKey = 0; |
| 66988 | 67609 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 66989 | | - u.bg.pC = p->apCsr[pOp->p1]; |
| 66990 | | - assert( u.bg.pC!=0 ); |
| 66991 | | - assert( u.bg.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
| 67610 | + u.bh.pC = p->apCsr[pOp->p1]; |
| 67611 | + assert( u.bh.pC!=0 ); |
| 67612 | + assert( u.bh.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
| 66992 | 67613 | |
| 66993 | | - /* If the update-hook will be invoked, set u.bg.iKey to the rowid of the |
| 67614 | + /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the |
| 66994 | 67615 | ** row being deleted. |
| 66995 | 67616 | */ |
| 66996 | 67617 | if( db->xUpdateCallback && pOp->p4.z ){ |
| 66997 | | - assert( u.bg.pC->isTable ); |
| 66998 | | - assert( u.bg.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ |
| 66999 | | - u.bg.iKey = u.bg.pC->lastRowid; |
| 67618 | + assert( u.bh.pC->isTable ); |
| 67619 | + assert( u.bh.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ |
| 67620 | + u.bh.iKey = u.bh.pC->lastRowid; |
| 67000 | 67621 | } |
| 67001 | 67622 | |
| 67002 | 67623 | /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or |
| 67003 | 67624 | ** OP_Column on the same table without any intervening operations that |
| 67004 | | - ** might move or invalidate the cursor. Hence cursor u.bg.pC is always pointing |
| 67625 | + ** might move or invalidate the cursor. Hence cursor u.bh.pC is always pointing |
| 67005 | 67626 | ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation |
| 67006 | 67627 | ** below is always a no-op and cannot fail. We will run it anyhow, though, |
| 67007 | 67628 | ** to guard against future changes to the code generator. |
| 67008 | 67629 | **/ |
| 67009 | | - assert( u.bg.pC->deferredMoveto==0 ); |
| 67010 | | - rc = sqlite3VdbeCursorMoveto(u.bg.pC); |
| 67630 | + assert( u.bh.pC->deferredMoveto==0 ); |
| 67631 | + rc = sqlite3VdbeCursorMoveto(u.bh.pC); |
| 67011 | 67632 | if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 67012 | 67633 | |
| 67013 | | - sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0); |
| 67014 | | - rc = sqlite3BtreeDelete(u.bg.pC->pCursor); |
| 67015 | | - u.bg.pC->cacheStatus = CACHE_STALE; |
| 67634 | + sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0); |
| 67635 | + rc = sqlite3BtreeDelete(u.bh.pC->pCursor); |
| 67636 | + u.bh.pC->cacheStatus = CACHE_STALE; |
| 67016 | 67637 | |
| 67017 | 67638 | /* Invoke the update-hook if required. */ |
| 67018 | 67639 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| 67019 | | - const char *zDb = db->aDb[u.bg.pC->iDb].zName; |
| 67640 | + const char *zDb = db->aDb[u.bh.pC->iDb].zName; |
| 67020 | 67641 | const char *zTbl = pOp->p4.z; |
| 67021 | | - db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bg.iKey); |
| 67022 | | - assert( u.bg.pC->iDb>=0 ); |
| 67642 | + db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey); |
| 67643 | + assert( u.bh.pC->iDb>=0 ); |
| 67023 | 67644 | } |
| 67024 | 67645 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
| 67025 | 67646 | break; |
| 67026 | 67647 | } |
| 67027 | 67648 | /* Opcode: ResetCount * * * * * |
| | @@ -67034,10 +67655,53 @@ |
| 67034 | 67655 | case OP_ResetCount: { |
| 67035 | 67656 | sqlite3VdbeSetChanges(db, p->nChange); |
| 67036 | 67657 | p->nChange = 0; |
| 67037 | 67658 | break; |
| 67038 | 67659 | } |
| 67660 | + |
| 67661 | +/* Opcode: SorterCompare P1 P2 P3 |
| 67662 | +** |
| 67663 | +** P1 is a sorter cursor. This instruction compares the record blob in |
| 67664 | +** register P3 with the entry that the sorter cursor currently points to. |
| 67665 | +** If, excluding the rowid fields at the end, the two records are a match, |
| 67666 | +** fall through to the next instruction. Otherwise, jump to instruction P2. |
| 67667 | +*/ |
| 67668 | +case OP_SorterCompare: { |
| 67669 | +#if 0 /* local variables moved into u.bi */ |
| 67670 | + VdbeCursor *pC; |
| 67671 | + int res; |
| 67672 | +#endif /* local variables moved into u.bi */ |
| 67673 | + |
| 67674 | + u.bi.pC = p->apCsr[pOp->p1]; |
| 67675 | + assert( isSorter(u.bi.pC) ); |
| 67676 | + pIn3 = &aMem[pOp->p3]; |
| 67677 | + rc = sqlite3VdbeSorterCompare(u.bi.pC, pIn3, &u.bi.res); |
| 67678 | + if( u.bi.res ){ |
| 67679 | + pc = pOp->p2-1; |
| 67680 | + } |
| 67681 | + break; |
| 67682 | +}; |
| 67683 | + |
| 67684 | +/* Opcode: SorterData P1 P2 * * * |
| 67685 | +** |
| 67686 | +** Write into register P2 the current sorter data for sorter cursor P1. |
| 67687 | +*/ |
| 67688 | +case OP_SorterData: { |
| 67689 | +#if 0 /* local variables moved into u.bj */ |
| 67690 | + VdbeCursor *pC; |
| 67691 | +#endif /* local variables moved into u.bj */ |
| 67692 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 67693 | + pOut = &aMem[pOp->p2]; |
| 67694 | + u.bj.pC = p->apCsr[pOp->p1]; |
| 67695 | + assert( u.bj.pC->isSorter ); |
| 67696 | + rc = sqlite3VdbeSorterRowkey(u.bj.pC, pOut); |
| 67697 | +#else |
| 67698 | + pOp->opcode = OP_RowKey; |
| 67699 | + pc--; |
| 67700 | +#endif |
| 67701 | + break; |
| 67702 | +} |
| 67039 | 67703 | |
| 67040 | 67704 | /* Opcode: RowData P1 P2 * * * |
| 67041 | 67705 | ** |
| 67042 | 67706 | ** Write into register P2 the complete row data for cursor P1. |
| 67043 | 67707 | ** There is no interpretation of the data. |
| | @@ -67057,65 +67721,67 @@ |
| 67057 | 67721 | ** If the P1 cursor must be pointing to a valid row (not a NULL row) |
| 67058 | 67722 | ** of a real table, not a pseudo-table. |
| 67059 | 67723 | */ |
| 67060 | 67724 | case OP_RowKey: |
| 67061 | 67725 | case OP_RowData: { |
| 67062 | | -#if 0 /* local variables moved into u.bh */ |
| 67726 | +#if 0 /* local variables moved into u.bk */ |
| 67063 | 67727 | VdbeCursor *pC; |
| 67064 | 67728 | BtCursor *pCrsr; |
| 67065 | 67729 | u32 n; |
| 67066 | 67730 | i64 n64; |
| 67067 | | -#endif /* local variables moved into u.bh */ |
| 67731 | +#endif /* local variables moved into u.bk */ |
| 67068 | 67732 | |
| 67069 | 67733 | pOut = &aMem[pOp->p2]; |
| 67070 | 67734 | memAboutToChange(p, pOut); |
| 67071 | 67735 | |
| 67072 | 67736 | /* Note that RowKey and RowData are really exactly the same instruction */ |
| 67073 | 67737 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67074 | | - u.bh.pC = p->apCsr[pOp->p1]; |
| 67075 | | - assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey ); |
| 67076 | | - assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData ); |
| 67077 | | - assert( u.bh.pC!=0 ); |
| 67078 | | - assert( u.bh.pC->nullRow==0 ); |
| 67079 | | - assert( u.bh.pC->pseudoTableReg==0 ); |
| 67080 | | - assert( u.bh.pC->pCursor!=0 ); |
| 67081 | | - u.bh.pCrsr = u.bh.pC->pCursor; |
| 67082 | | - assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) ); |
| 67738 | + u.bk.pC = p->apCsr[pOp->p1]; |
| 67739 | + assert( u.bk.pC->isSorter==0 ); |
| 67740 | + assert( u.bk.pC->isTable || pOp->opcode!=OP_RowData ); |
| 67741 | + assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData ); |
| 67742 | + assert( u.bk.pC!=0 ); |
| 67743 | + assert( u.bk.pC->nullRow==0 ); |
| 67744 | + assert( u.bk.pC->pseudoTableReg==0 ); |
| 67745 | + assert( !u.bk.pC->isSorter ); |
| 67746 | + assert( u.bk.pC->pCursor!=0 ); |
| 67747 | + u.bk.pCrsr = u.bk.pC->pCursor; |
| 67748 | + assert( sqlite3BtreeCursorIsValid(u.bk.pCrsr) ); |
| 67083 | 67749 | |
| 67084 | 67750 | /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or |
| 67085 | 67751 | ** OP_Rewind/Op_Next with no intervening instructions that might invalidate |
| 67086 | 67752 | ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always |
| 67087 | 67753 | ** a no-op and can never fail. But we leave it in place as a safety. |
| 67088 | 67754 | */ |
| 67089 | | - assert( u.bh.pC->deferredMoveto==0 ); |
| 67090 | | - rc = sqlite3VdbeCursorMoveto(u.bh.pC); |
| 67091 | | - if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 67092 | | - |
| 67093 | | - if( u.bh.pC->isIndex ){ |
| 67094 | | - assert( !u.bh.pC->isTable ); |
| 67095 | | - rc = sqlite3BtreeKeySize(u.bh.pCrsr, &u.bh.n64); |
| 67096 | | - assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| 67097 | | - if( u.bh.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67098 | | - goto too_big; |
| 67099 | | - } |
| 67100 | | - u.bh.n = (u32)u.bh.n64; |
| 67101 | | - }else{ |
| 67102 | | - rc = sqlite3BtreeDataSize(u.bh.pCrsr, &u.bh.n); |
| 67103 | | - assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 67104 | | - if( u.bh.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67105 | | - goto too_big; |
| 67106 | | - } |
| 67107 | | - } |
| 67108 | | - if( sqlite3VdbeMemGrow(pOut, u.bh.n, 0) ){ |
| 67109 | | - goto no_mem; |
| 67110 | | - } |
| 67111 | | - pOut->n = u.bh.n; |
| 67112 | | - MemSetTypeFlag(pOut, MEM_Blob); |
| 67113 | | - if( u.bh.pC->isIndex ){ |
| 67114 | | - rc = sqlite3BtreeKey(u.bh.pCrsr, 0, u.bh.n, pOut->z); |
| 67115 | | - }else{ |
| 67116 | | - rc = sqlite3BtreeData(u.bh.pCrsr, 0, u.bh.n, pOut->z); |
| 67755 | + assert( u.bk.pC->deferredMoveto==0 ); |
| 67756 | + rc = sqlite3VdbeCursorMoveto(u.bk.pC); |
| 67757 | + if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 67758 | + |
| 67759 | + if( u.bk.pC->isIndex ){ |
| 67760 | + assert( !u.bk.pC->isTable ); |
| 67761 | + rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64); |
| 67762 | + assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| 67763 | + if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67764 | + goto too_big; |
| 67765 | + } |
| 67766 | + u.bk.n = (u32)u.bk.n64; |
| 67767 | + }else{ |
| 67768 | + rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n); |
| 67769 | + assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 67770 | + if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67771 | + goto too_big; |
| 67772 | + } |
| 67773 | + } |
| 67774 | + if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){ |
| 67775 | + goto no_mem; |
| 67776 | + } |
| 67777 | + pOut->n = u.bk.n; |
| 67778 | + MemSetTypeFlag(pOut, MEM_Blob); |
| 67779 | + if( u.bk.pC->isIndex ){ |
| 67780 | + rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z); |
| 67781 | + }else{ |
| 67782 | + rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z); |
| 67117 | 67783 | } |
| 67118 | 67784 | pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ |
| 67119 | 67785 | UPDATE_MAX_BLOBSIZE(pOut); |
| 67120 | 67786 | break; |
| 67121 | 67787 | } |
| | @@ -67128,46 +67794,46 @@ |
| 67128 | 67794 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 67129 | 67795 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 67130 | 67796 | ** one opcode now works for both table types. |
| 67131 | 67797 | */ |
| 67132 | 67798 | case OP_Rowid: { /* out2-prerelease */ |
| 67133 | | -#if 0 /* local variables moved into u.bi */ |
| 67799 | +#if 0 /* local variables moved into u.bl */ |
| 67134 | 67800 | VdbeCursor *pC; |
| 67135 | 67801 | i64 v; |
| 67136 | 67802 | sqlite3_vtab *pVtab; |
| 67137 | 67803 | const sqlite3_module *pModule; |
| 67138 | | -#endif /* local variables moved into u.bi */ |
| 67804 | +#endif /* local variables moved into u.bl */ |
| 67139 | 67805 | |
| 67140 | 67806 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67141 | | - u.bi.pC = p->apCsr[pOp->p1]; |
| 67142 | | - assert( u.bi.pC!=0 ); |
| 67143 | | - assert( u.bi.pC->pseudoTableReg==0 ); |
| 67144 | | - if( u.bi.pC->nullRow ){ |
| 67807 | + u.bl.pC = p->apCsr[pOp->p1]; |
| 67808 | + assert( u.bl.pC!=0 ); |
| 67809 | + assert( u.bl.pC->pseudoTableReg==0 ); |
| 67810 | + if( u.bl.pC->nullRow ){ |
| 67145 | 67811 | pOut->flags = MEM_Null; |
| 67146 | 67812 | break; |
| 67147 | | - }else if( u.bi.pC->deferredMoveto ){ |
| 67148 | | - u.bi.v = u.bi.pC->movetoTarget; |
| 67813 | + }else if( u.bl.pC->deferredMoveto ){ |
| 67814 | + u.bl.v = u.bl.pC->movetoTarget; |
| 67149 | 67815 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 67150 | | - }else if( u.bi.pC->pVtabCursor ){ |
| 67151 | | - u.bi.pVtab = u.bi.pC->pVtabCursor->pVtab; |
| 67152 | | - u.bi.pModule = u.bi.pVtab->pModule; |
| 67153 | | - assert( u.bi.pModule->xRowid ); |
| 67154 | | - rc = u.bi.pModule->xRowid(u.bi.pC->pVtabCursor, &u.bi.v); |
| 67155 | | - importVtabErrMsg(p, u.bi.pVtab); |
| 67816 | + }else if( u.bl.pC->pVtabCursor ){ |
| 67817 | + u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab; |
| 67818 | + u.bl.pModule = u.bl.pVtab->pModule; |
| 67819 | + assert( u.bl.pModule->xRowid ); |
| 67820 | + rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v); |
| 67821 | + importVtabErrMsg(p, u.bl.pVtab); |
| 67156 | 67822 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 67157 | 67823 | }else{ |
| 67158 | | - assert( u.bi.pC->pCursor!=0 ); |
| 67159 | | - rc = sqlite3VdbeCursorMoveto(u.bi.pC); |
| 67824 | + assert( u.bl.pC->pCursor!=0 ); |
| 67825 | + rc = sqlite3VdbeCursorMoveto(u.bl.pC); |
| 67160 | 67826 | if( rc ) goto abort_due_to_error; |
| 67161 | | - if( u.bi.pC->rowidIsValid ){ |
| 67162 | | - u.bi.v = u.bi.pC->lastRowid; |
| 67827 | + if( u.bl.pC->rowidIsValid ){ |
| 67828 | + u.bl.v = u.bl.pC->lastRowid; |
| 67163 | 67829 | }else{ |
| 67164 | | - rc = sqlite3BtreeKeySize(u.bi.pC->pCursor, &u.bi.v); |
| 67830 | + rc = sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v); |
| 67165 | 67831 | assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */ |
| 67166 | 67832 | } |
| 67167 | 67833 | } |
| 67168 | | - pOut->u.i = u.bi.v; |
| 67834 | + pOut->u.i = u.bl.v; |
| 67169 | 67835 | break; |
| 67170 | 67836 | } |
| 67171 | 67837 | |
| 67172 | 67838 | /* Opcode: NullRow P1 * * * * |
| 67173 | 67839 | ** |
| | @@ -67174,21 +67840,22 @@ |
| 67174 | 67840 | ** Move the cursor P1 to a null row. Any OP_Column operations |
| 67175 | 67841 | ** that occur while the cursor is on the null row will always |
| 67176 | 67842 | ** write a NULL. |
| 67177 | 67843 | */ |
| 67178 | 67844 | case OP_NullRow: { |
| 67179 | | -#if 0 /* local variables moved into u.bj */ |
| 67845 | +#if 0 /* local variables moved into u.bm */ |
| 67180 | 67846 | VdbeCursor *pC; |
| 67181 | | -#endif /* local variables moved into u.bj */ |
| 67847 | +#endif /* local variables moved into u.bm */ |
| 67182 | 67848 | |
| 67183 | 67849 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67184 | | - u.bj.pC = p->apCsr[pOp->p1]; |
| 67185 | | - assert( u.bj.pC!=0 ); |
| 67186 | | - u.bj.pC->nullRow = 1; |
| 67187 | | - u.bj.pC->rowidIsValid = 0; |
| 67188 | | - if( u.bj.pC->pCursor ){ |
| 67189 | | - sqlite3BtreeClearCursor(u.bj.pC->pCursor); |
| 67850 | + u.bm.pC = p->apCsr[pOp->p1]; |
| 67851 | + assert( u.bm.pC!=0 ); |
| 67852 | + u.bm.pC->nullRow = 1; |
| 67853 | + u.bm.pC->rowidIsValid = 0; |
| 67854 | + assert( u.bm.pC->pCursor || u.bm.pC->pVtabCursor ); |
| 67855 | + if( u.bm.pC->pCursor ){ |
| 67856 | + sqlite3BtreeClearCursor(u.bm.pC->pCursor); |
| 67190 | 67857 | } |
| 67191 | 67858 | break; |
| 67192 | 67859 | } |
| 67193 | 67860 | |
| 67194 | 67861 | /* Opcode: Last P1 P2 * * * |
| | @@ -67198,30 +67865,30 @@ |
| 67198 | 67865 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 67199 | 67866 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 67200 | 67867 | ** to the following instruction. |
| 67201 | 67868 | */ |
| 67202 | 67869 | case OP_Last: { /* jump */ |
| 67203 | | -#if 0 /* local variables moved into u.bk */ |
| 67870 | +#if 0 /* local variables moved into u.bn */ |
| 67204 | 67871 | VdbeCursor *pC; |
| 67205 | 67872 | BtCursor *pCrsr; |
| 67206 | 67873 | int res; |
| 67207 | | -#endif /* local variables moved into u.bk */ |
| 67874 | +#endif /* local variables moved into u.bn */ |
| 67208 | 67875 | |
| 67209 | 67876 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67210 | | - u.bk.pC = p->apCsr[pOp->p1]; |
| 67211 | | - assert( u.bk.pC!=0 ); |
| 67212 | | - u.bk.pCrsr = u.bk.pC->pCursor; |
| 67213 | | - if( u.bk.pCrsr==0 ){ |
| 67214 | | - u.bk.res = 1; |
| 67877 | + u.bn.pC = p->apCsr[pOp->p1]; |
| 67878 | + assert( u.bn.pC!=0 ); |
| 67879 | + u.bn.pCrsr = u.bn.pC->pCursor; |
| 67880 | + if( NEVER(u.bn.pCrsr==0) ){ |
| 67881 | + u.bn.res = 1; |
| 67215 | 67882 | }else{ |
| 67216 | | - rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res); |
| 67883 | + rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res); |
| 67217 | 67884 | } |
| 67218 | | - u.bk.pC->nullRow = (u8)u.bk.res; |
| 67219 | | - u.bk.pC->deferredMoveto = 0; |
| 67220 | | - u.bk.pC->rowidIsValid = 0; |
| 67221 | | - u.bk.pC->cacheStatus = CACHE_STALE; |
| 67222 | | - if( pOp->p2>0 && u.bk.res ){ |
| 67885 | + u.bn.pC->nullRow = (u8)u.bn.res; |
| 67886 | + u.bn.pC->deferredMoveto = 0; |
| 67887 | + u.bn.pC->rowidIsValid = 0; |
| 67888 | + u.bn.pC->cacheStatus = CACHE_STALE; |
| 67889 | + if( pOp->p2>0 && u.bn.res ){ |
| 67223 | 67890 | pc = pOp->p2 - 1; |
| 67224 | 67891 | } |
| 67225 | 67892 | break; |
| 67226 | 67893 | } |
| 67227 | 67894 | |
| | @@ -67236,10 +67903,14 @@ |
| 67236 | 67903 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 67237 | 67904 | ** rewinding so that the global variable will be incremented and |
| 67238 | 67905 | ** regression tests can determine whether or not the optimizer is |
| 67239 | 67906 | ** correctly optimizing out sorts. |
| 67240 | 67907 | */ |
| 67908 | +case OP_SorterSort: /* jump */ |
| 67909 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 67910 | + pOp->opcode = OP_Sort; |
| 67911 | +#endif |
| 67241 | 67912 | case OP_Sort: { /* jump */ |
| 67242 | 67913 | #ifdef SQLITE_TEST |
| 67243 | 67914 | sqlite3_sort_count++; |
| 67244 | 67915 | sqlite3_search_count--; |
| 67245 | 67916 | #endif |
| | @@ -67253,43 +67924,51 @@ |
| 67253 | 67924 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 67254 | 67925 | ** If P2 is 0 or if the table or index is not empty, fall through |
| 67255 | 67926 | ** to the following instruction. |
| 67256 | 67927 | */ |
| 67257 | 67928 | case OP_Rewind: { /* jump */ |
| 67258 | | -#if 0 /* local variables moved into u.bl */ |
| 67929 | +#if 0 /* local variables moved into u.bo */ |
| 67259 | 67930 | VdbeCursor *pC; |
| 67260 | 67931 | BtCursor *pCrsr; |
| 67261 | 67932 | int res; |
| 67262 | | -#endif /* local variables moved into u.bl */ |
| 67933 | +#endif /* local variables moved into u.bo */ |
| 67263 | 67934 | |
| 67264 | 67935 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67265 | | - u.bl.pC = p->apCsr[pOp->p1]; |
| 67266 | | - assert( u.bl.pC!=0 ); |
| 67267 | | - u.bl.res = 1; |
| 67268 | | - if( (u.bl.pCrsr = u.bl.pC->pCursor)!=0 ){ |
| 67269 | | - rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res); |
| 67270 | | - u.bl.pC->atFirst = u.bl.res==0 ?1:0; |
| 67271 | | - u.bl.pC->deferredMoveto = 0; |
| 67272 | | - u.bl.pC->cacheStatus = CACHE_STALE; |
| 67273 | | - u.bl.pC->rowidIsValid = 0; |
| 67274 | | - } |
| 67275 | | - u.bl.pC->nullRow = (u8)u.bl.res; |
| 67936 | + u.bo.pC = p->apCsr[pOp->p1]; |
| 67937 | + assert( u.bo.pC!=0 ); |
| 67938 | + assert( u.bo.pC->isSorter==(pOp->opcode==OP_SorterSort) ); |
| 67939 | + u.bo.res = 1; |
| 67940 | + if( isSorter(u.bo.pC) ){ |
| 67941 | + rc = sqlite3VdbeSorterRewind(db, u.bo.pC, &u.bo.res); |
| 67942 | + }else{ |
| 67943 | + u.bo.pCrsr = u.bo.pC->pCursor; |
| 67944 | + assert( u.bo.pCrsr ); |
| 67945 | + rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res); |
| 67946 | + u.bo.pC->atFirst = u.bo.res==0 ?1:0; |
| 67947 | + u.bo.pC->deferredMoveto = 0; |
| 67948 | + u.bo.pC->cacheStatus = CACHE_STALE; |
| 67949 | + u.bo.pC->rowidIsValid = 0; |
| 67950 | + } |
| 67951 | + u.bo.pC->nullRow = (u8)u.bo.res; |
| 67276 | 67952 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 67277 | | - if( u.bl.res ){ |
| 67953 | + if( u.bo.res ){ |
| 67278 | 67954 | pc = pOp->p2 - 1; |
| 67279 | 67955 | } |
| 67280 | 67956 | break; |
| 67281 | 67957 | } |
| 67282 | 67958 | |
| 67283 | | -/* Opcode: Next P1 P2 * * P5 |
| 67959 | +/* Opcode: Next P1 P2 * P4 P5 |
| 67284 | 67960 | ** |
| 67285 | 67961 | ** Advance cursor P1 so that it points to the next key/data pair in its |
| 67286 | 67962 | ** table or index. If there are no more key/value pairs then fall through |
| 67287 | 67963 | ** to the following instruction. But if the cursor advance was successful, |
| 67288 | 67964 | ** jump immediately to P2. |
| 67289 | 67965 | ** |
| 67290 | 67966 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 67967 | +** |
| 67968 | +** P4 is always of type P4_ADVANCE. The function pointer points to |
| 67969 | +** sqlite3BtreeNext(). |
| 67291 | 67970 | ** |
| 67292 | 67971 | ** If P5 is positive and the jump is taken, then event counter |
| 67293 | 67972 | ** number P5-1 in the prepared statement is incremented. |
| 67294 | 67973 | ** |
| 67295 | 67974 | ** See also: Prev |
| | @@ -67300,48 +67979,57 @@ |
| 67300 | 67979 | ** table or index. If there is no previous key/value pairs then fall through |
| 67301 | 67980 | ** to the following instruction. But if the cursor backup was successful, |
| 67302 | 67981 | ** jump immediately to P2. |
| 67303 | 67982 | ** |
| 67304 | 67983 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 67984 | +** |
| 67985 | +** P4 is always of type P4_ADVANCE. The function pointer points to |
| 67986 | +** sqlite3BtreePrevious(). |
| 67305 | 67987 | ** |
| 67306 | 67988 | ** If P5 is positive and the jump is taken, then event counter |
| 67307 | 67989 | ** number P5-1 in the prepared statement is incremented. |
| 67308 | 67990 | */ |
| 67991 | +case OP_SorterNext: /* jump */ |
| 67992 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 67993 | + pOp->opcode = OP_Next; |
| 67994 | +#endif |
| 67309 | 67995 | case OP_Prev: /* jump */ |
| 67310 | 67996 | case OP_Next: { /* jump */ |
| 67311 | | -#if 0 /* local variables moved into u.bm */ |
| 67997 | +#if 0 /* local variables moved into u.bp */ |
| 67312 | 67998 | VdbeCursor *pC; |
| 67313 | | - BtCursor *pCrsr; |
| 67314 | 67999 | int res; |
| 67315 | | -#endif /* local variables moved into u.bm */ |
| 68000 | +#endif /* local variables moved into u.bp */ |
| 67316 | 68001 | |
| 67317 | 68002 | CHECK_FOR_INTERRUPT; |
| 67318 | 68003 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67319 | 68004 | assert( pOp->p5<=ArraySize(p->aCounter) ); |
| 67320 | | - u.bm.pC = p->apCsr[pOp->p1]; |
| 67321 | | - if( u.bm.pC==0 ){ |
| 68005 | + u.bp.pC = p->apCsr[pOp->p1]; |
| 68006 | + if( u.bp.pC==0 ){ |
| 67322 | 68007 | break; /* See ticket #2273 */ |
| 67323 | 68008 | } |
| 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); |
| 67333 | | - u.bm.pC->nullRow = (u8)u.bm.res; |
| 67334 | | - u.bm.pC->cacheStatus = CACHE_STALE; |
| 67335 | | - if( u.bm.res==0 ){ |
| 68009 | + assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterNext) ); |
| 68010 | + if( isSorter(u.bp.pC) ){ |
| 68011 | + assert( pOp->opcode==OP_SorterNext ); |
| 68012 | + rc = sqlite3VdbeSorterNext(db, u.bp.pC, &u.bp.res); |
| 68013 | + }else{ |
| 68014 | + u.bp.res = 1; |
| 68015 | + assert( u.bp.pC->deferredMoveto==0 ); |
| 68016 | + assert( u.bp.pC->pCursor ); |
| 68017 | + assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
| 68018 | + assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
| 68019 | + rc = pOp->p4.xAdvance(u.bp.pC->pCursor, &u.bp.res); |
| 68020 | + } |
| 68021 | + u.bp.pC->nullRow = (u8)u.bp.res; |
| 68022 | + u.bp.pC->cacheStatus = CACHE_STALE; |
| 68023 | + if( u.bp.res==0 ){ |
| 67336 | 68024 | pc = pOp->p2 - 1; |
| 67337 | 68025 | if( pOp->p5 ) p->aCounter[pOp->p5-1]++; |
| 67338 | 68026 | #ifdef SQLITE_TEST |
| 67339 | 68027 | sqlite3_search_count++; |
| 67340 | 68028 | #endif |
| 67341 | 68029 | } |
| 67342 | | - u.bm.pC->rowidIsValid = 0; |
| 68030 | + u.bp.pC->rowidIsValid = 0; |
| 67343 | 68031 | break; |
| 67344 | 68032 | } |
| 67345 | 68033 | |
| 67346 | 68034 | /* Opcode: IdxInsert P1 P2 P3 * P5 |
| 67347 | 68035 | ** |
| | @@ -67353,35 +68041,44 @@ |
| 67353 | 68041 | ** insert is likely to be an append. |
| 67354 | 68042 | ** |
| 67355 | 68043 | ** This instruction only works for indices. The equivalent instruction |
| 67356 | 68044 | ** for tables is OP_Insert. |
| 67357 | 68045 | */ |
| 68046 | +case OP_SorterInsert: /* in2 */ |
| 68047 | +#ifdef SQLITE_OMIT_MERGE_SORT |
| 68048 | + pOp->opcode = OP_IdxInsert; |
| 68049 | +#endif |
| 67358 | 68050 | case OP_IdxInsert: { /* in2 */ |
| 67359 | | -#if 0 /* local variables moved into u.bn */ |
| 68051 | +#if 0 /* local variables moved into u.bq */ |
| 67360 | 68052 | VdbeCursor *pC; |
| 67361 | 68053 | BtCursor *pCrsr; |
| 67362 | 68054 | int nKey; |
| 67363 | 68055 | const char *zKey; |
| 67364 | | -#endif /* local variables moved into u.bn */ |
| 68056 | +#endif /* local variables moved into u.bq */ |
| 67365 | 68057 | |
| 67366 | 68058 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67367 | | - u.bn.pC = p->apCsr[pOp->p1]; |
| 67368 | | - assert( u.bn.pC!=0 ); |
| 68059 | + u.bq.pC = p->apCsr[pOp->p1]; |
| 68060 | + assert( u.bq.pC!=0 ); |
| 68061 | + assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterInsert) ); |
| 67369 | 68062 | pIn2 = &aMem[pOp->p2]; |
| 67370 | 68063 | assert( pIn2->flags & MEM_Blob ); |
| 67371 | | - u.bn.pCrsr = u.bn.pC->pCursor; |
| 67372 | | - if( ALWAYS(u.bn.pCrsr!=0) ){ |
| 67373 | | - assert( u.bn.pC->isTable==0 ); |
| 68064 | + u.bq.pCrsr = u.bq.pC->pCursor; |
| 68065 | + if( ALWAYS(u.bq.pCrsr!=0) ){ |
| 68066 | + assert( u.bq.pC->isTable==0 ); |
| 67374 | 68067 | rc = ExpandBlob(pIn2); |
| 67375 | 68068 | if( rc==SQLITE_OK ){ |
| 67376 | | - u.bn.nKey = pIn2->n; |
| 67377 | | - 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 ); |
| 67382 | | - u.bn.pC->cacheStatus = CACHE_STALE; |
| 68069 | + if( isSorter(u.bq.pC) ){ |
| 68070 | + rc = sqlite3VdbeSorterWrite(db, u.bq.pC, pIn2); |
| 68071 | + }else{ |
| 68072 | + u.bq.nKey = pIn2->n; |
| 68073 | + u.bq.zKey = pIn2->z; |
| 68074 | + rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3, |
| 68075 | + ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0) |
| 68076 | + ); |
| 68077 | + assert( u.bq.pC->deferredMoveto==0 ); |
| 68078 | + u.bq.pC->cacheStatus = CACHE_STALE; |
| 68079 | + } |
| 67383 | 68080 | } |
| 67384 | 68081 | } |
| 67385 | 68082 | break; |
| 67386 | 68083 | } |
| 67387 | 68084 | |
| | @@ -67390,37 +68087,37 @@ |
| 67390 | 68087 | ** The content of P3 registers starting at register P2 form |
| 67391 | 68088 | ** an unpacked index key. This opcode removes that entry from the |
| 67392 | 68089 | ** index opened by cursor P1. |
| 67393 | 68090 | */ |
| 67394 | 68091 | case OP_IdxDelete: { |
| 67395 | | -#if 0 /* local variables moved into u.bo */ |
| 68092 | +#if 0 /* local variables moved into u.br */ |
| 67396 | 68093 | VdbeCursor *pC; |
| 67397 | 68094 | BtCursor *pCrsr; |
| 67398 | 68095 | int res; |
| 67399 | 68096 | UnpackedRecord r; |
| 67400 | | -#endif /* local variables moved into u.bo */ |
| 68097 | +#endif /* local variables moved into u.br */ |
| 67401 | 68098 | |
| 67402 | 68099 | assert( pOp->p3>0 ); |
| 67403 | 68100 | assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 ); |
| 67404 | 68101 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67405 | | - u.bo.pC = p->apCsr[pOp->p1]; |
| 67406 | | - assert( u.bo.pC!=0 ); |
| 67407 | | - u.bo.pCrsr = u.bo.pC->pCursor; |
| 67408 | | - if( ALWAYS(u.bo.pCrsr!=0) ){ |
| 67409 | | - u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo; |
| 67410 | | - u.bo.r.nField = (u16)pOp->p3; |
| 67411 | | - u.bo.r.flags = 0; |
| 67412 | | - u.bo.r.aMem = &aMem[pOp->p2]; |
| 68102 | + u.br.pC = p->apCsr[pOp->p1]; |
| 68103 | + assert( u.br.pC!=0 ); |
| 68104 | + u.br.pCrsr = u.br.pC->pCursor; |
| 68105 | + if( ALWAYS(u.br.pCrsr!=0) ){ |
| 68106 | + u.br.r.pKeyInfo = u.br.pC->pKeyInfo; |
| 68107 | + u.br.r.nField = (u16)pOp->p3; |
| 68108 | + u.br.r.flags = 0; |
| 68109 | + u.br.r.aMem = &aMem[pOp->p2]; |
| 67413 | 68110 | #ifdef SQLITE_DEBUG |
| 67414 | | - { int i; for(i=0; i<u.bo.r.nField; i++) assert( memIsValid(&u.bo.r.aMem[i]) ); } |
| 68111 | + { int i; for(i=0; i<u.br.r.nField; i++) assert( memIsValid(&u.br.r.aMem[i]) ); } |
| 67415 | 68112 | #endif |
| 67416 | | - rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res); |
| 67417 | | - if( rc==SQLITE_OK && u.bo.res==0 ){ |
| 67418 | | - rc = sqlite3BtreeDelete(u.bo.pCrsr); |
| 68113 | + rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &u.br.r, 0, 0, &u.br.res); |
| 68114 | + if( rc==SQLITE_OK && u.br.res==0 ){ |
| 68115 | + rc = sqlite3BtreeDelete(u.br.pCrsr); |
| 67419 | 68116 | } |
| 67420 | | - assert( u.bo.pC->deferredMoveto==0 ); |
| 67421 | | - u.bo.pC->cacheStatus = CACHE_STALE; |
| 68117 | + assert( u.br.pC->deferredMoveto==0 ); |
| 68118 | + u.br.pC->cacheStatus = CACHE_STALE; |
| 67422 | 68119 | } |
| 67423 | 68120 | break; |
| 67424 | 68121 | } |
| 67425 | 68122 | |
| 67426 | 68123 | /* Opcode: IdxRowid P1 P2 * * * |
| | @@ -67430,32 +68127,32 @@ |
| 67430 | 68127 | ** the rowid of the table entry to which this index entry points. |
| 67431 | 68128 | ** |
| 67432 | 68129 | ** See also: Rowid, MakeRecord. |
| 67433 | 68130 | */ |
| 67434 | 68131 | case OP_IdxRowid: { /* out2-prerelease */ |
| 67435 | | -#if 0 /* local variables moved into u.bp */ |
| 68132 | +#if 0 /* local variables moved into u.bs */ |
| 67436 | 68133 | BtCursor *pCrsr; |
| 67437 | 68134 | VdbeCursor *pC; |
| 67438 | 68135 | i64 rowid; |
| 67439 | | -#endif /* local variables moved into u.bp */ |
| 68136 | +#endif /* local variables moved into u.bs */ |
| 67440 | 68137 | |
| 67441 | 68138 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67442 | | - u.bp.pC = p->apCsr[pOp->p1]; |
| 67443 | | - assert( u.bp.pC!=0 ); |
| 67444 | | - u.bp.pCrsr = u.bp.pC->pCursor; |
| 68139 | + u.bs.pC = p->apCsr[pOp->p1]; |
| 68140 | + assert( u.bs.pC!=0 ); |
| 68141 | + u.bs.pCrsr = u.bs.pC->pCursor; |
| 67445 | 68142 | pOut->flags = MEM_Null; |
| 67446 | | - if( ALWAYS(u.bp.pCrsr!=0) ){ |
| 67447 | | - rc = sqlite3VdbeCursorMoveto(u.bp.pC); |
| 68143 | + if( ALWAYS(u.bs.pCrsr!=0) ){ |
| 68144 | + rc = sqlite3VdbeCursorMoveto(u.bs.pC); |
| 67448 | 68145 | if( NEVER(rc) ) goto abort_due_to_error; |
| 67449 | | - assert( u.bp.pC->deferredMoveto==0 ); |
| 67450 | | - assert( u.bp.pC->isTable==0 ); |
| 67451 | | - if( !u.bp.pC->nullRow ){ |
| 67452 | | - rc = sqlite3VdbeIdxRowid(db, u.bp.pCrsr, &u.bp.rowid); |
| 68146 | + assert( u.bs.pC->deferredMoveto==0 ); |
| 68147 | + assert( u.bs.pC->isTable==0 ); |
| 68148 | + if( !u.bs.pC->nullRow ){ |
| 68149 | + rc = sqlite3VdbeIdxRowid(db, u.bs.pCrsr, &u.bs.rowid); |
| 67453 | 68150 | if( rc!=SQLITE_OK ){ |
| 67454 | 68151 | goto abort_due_to_error; |
| 67455 | 68152 | } |
| 67456 | | - pOut->u.i = u.bp.rowid; |
| 68153 | + pOut->u.i = u.bs.rowid; |
| 67457 | 68154 | pOut->flags = MEM_Int; |
| 67458 | 68155 | } |
| 67459 | 68156 | } |
| 67460 | 68157 | break; |
| 67461 | 68158 | } |
| | @@ -67486,43 +68183,43 @@ |
| 67486 | 68183 | ** If P5 is non-zero then the key value is increased by an epsilon prior |
| 67487 | 68184 | ** to the comparison. This makes the opcode work like IdxLE. |
| 67488 | 68185 | */ |
| 67489 | 68186 | case OP_IdxLT: /* jump */ |
| 67490 | 68187 | case OP_IdxGE: { /* jump */ |
| 67491 | | -#if 0 /* local variables moved into u.bq */ |
| 68188 | +#if 0 /* local variables moved into u.bt */ |
| 67492 | 68189 | VdbeCursor *pC; |
| 67493 | 68190 | int res; |
| 67494 | 68191 | UnpackedRecord r; |
| 67495 | | -#endif /* local variables moved into u.bq */ |
| 68192 | +#endif /* local variables moved into u.bt */ |
| 67496 | 68193 | |
| 67497 | 68194 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 67498 | | - u.bq.pC = p->apCsr[pOp->p1]; |
| 67499 | | - assert( u.bq.pC!=0 ); |
| 67500 | | - assert( u.bq.pC->isOrdered ); |
| 67501 | | - if( ALWAYS(u.bq.pC->pCursor!=0) ){ |
| 67502 | | - assert( u.bq.pC->deferredMoveto==0 ); |
| 68195 | + u.bt.pC = p->apCsr[pOp->p1]; |
| 68196 | + assert( u.bt.pC!=0 ); |
| 68197 | + assert( u.bt.pC->isOrdered ); |
| 68198 | + if( ALWAYS(u.bt.pC->pCursor!=0) ){ |
| 68199 | + assert( u.bt.pC->deferredMoveto==0 ); |
| 67503 | 68200 | assert( pOp->p5==0 || pOp->p5==1 ); |
| 67504 | 68201 | assert( pOp->p4type==P4_INT32 ); |
| 67505 | | - u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo; |
| 67506 | | - u.bq.r.nField = (u16)pOp->p4.i; |
| 68202 | + u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo; |
| 68203 | + u.bt.r.nField = (u16)pOp->p4.i; |
| 67507 | 68204 | if( pOp->p5 ){ |
| 67508 | | - u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 68205 | + u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID; |
| 67509 | 68206 | }else{ |
| 67510 | | - u.bq.r.flags = UNPACKED_IGNORE_ROWID; |
| 68207 | + u.bt.r.flags = UNPACKED_IGNORE_ROWID; |
| 67511 | 68208 | } |
| 67512 | | - u.bq.r.aMem = &aMem[pOp->p3]; |
| 68209 | + u.bt.r.aMem = &aMem[pOp->p3]; |
| 67513 | 68210 | #ifdef SQLITE_DEBUG |
| 67514 | | - { int i; for(i=0; i<u.bq.r.nField; i++) assert( memIsValid(&u.bq.r.aMem[i]) ); } |
| 68211 | + { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); } |
| 67515 | 68212 | #endif |
| 67516 | | - rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res); |
| 68213 | + rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res); |
| 67517 | 68214 | if( pOp->opcode==OP_IdxLT ){ |
| 67518 | | - u.bq.res = -u.bq.res; |
| 68215 | + u.bt.res = -u.bt.res; |
| 67519 | 68216 | }else{ |
| 67520 | 68217 | assert( pOp->opcode==OP_IdxGE ); |
| 67521 | | - u.bq.res++; |
| 68218 | + u.bt.res++; |
| 67522 | 68219 | } |
| 67523 | | - if( u.bq.res>0 ){ |
| 68220 | + if( u.bt.res>0 ){ |
| 67524 | 68221 | pc = pOp->p2 - 1 ; |
| 67525 | 68222 | } |
| 67526 | 68223 | } |
| 67527 | 68224 | break; |
| 67528 | 68225 | } |
| | @@ -67546,43 +68243,43 @@ |
| 67546 | 68243 | ** If AUTOVACUUM is disabled then a zero is stored in register P2. |
| 67547 | 68244 | ** |
| 67548 | 68245 | ** See also: Clear |
| 67549 | 68246 | */ |
| 67550 | 68247 | case OP_Destroy: { /* out2-prerelease */ |
| 67551 | | -#if 0 /* local variables moved into u.br */ |
| 68248 | +#if 0 /* local variables moved into u.bu */ |
| 67552 | 68249 | int iMoved; |
| 67553 | 68250 | int iCnt; |
| 67554 | 68251 | Vdbe *pVdbe; |
| 67555 | 68252 | int iDb; |
| 67556 | | -#endif /* local variables moved into u.br */ |
| 68253 | +#endif /* local variables moved into u.bu */ |
| 67557 | 68254 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 67558 | | - u.br.iCnt = 0; |
| 67559 | | - for(u.br.pVdbe=db->pVdbe; u.br.pVdbe; u.br.pVdbe = u.br.pVdbe->pNext){ |
| 67560 | | - if( u.br.pVdbe->magic==VDBE_MAGIC_RUN && u.br.pVdbe->inVtabMethod<2 && u.br.pVdbe->pc>=0 ){ |
| 67561 | | - u.br.iCnt++; |
| 68255 | + u.bu.iCnt = 0; |
| 68256 | + for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){ |
| 68257 | + if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){ |
| 68258 | + u.bu.iCnt++; |
| 67562 | 68259 | } |
| 67563 | 68260 | } |
| 67564 | 68261 | #else |
| 67565 | | - u.br.iCnt = db->activeVdbeCnt; |
| 68262 | + u.bu.iCnt = db->activeVdbeCnt; |
| 67566 | 68263 | #endif |
| 67567 | 68264 | pOut->flags = MEM_Null; |
| 67568 | | - if( u.br.iCnt>1 ){ |
| 68265 | + if( u.bu.iCnt>1 ){ |
| 67569 | 68266 | rc = SQLITE_LOCKED; |
| 67570 | 68267 | p->errorAction = OE_Abort; |
| 67571 | 68268 | }else{ |
| 67572 | | - u.br.iDb = pOp->p3; |
| 67573 | | - assert( u.br.iCnt==1 ); |
| 67574 | | - assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 ); |
| 67575 | | - rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved); |
| 68269 | + u.bu.iDb = pOp->p3; |
| 68270 | + assert( u.bu.iCnt==1 ); |
| 68271 | + assert( (p->btreeMask & (((yDbMask)1)<<u.bu.iDb))!=0 ); |
| 68272 | + rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved); |
| 67576 | 68273 | pOut->flags = MEM_Int; |
| 67577 | | - pOut->u.i = u.br.iMoved; |
| 68274 | + pOut->u.i = u.bu.iMoved; |
| 67578 | 68275 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 67579 | | - if( rc==SQLITE_OK && u.br.iMoved!=0 ){ |
| 67580 | | - sqlite3RootPageMoved(db, u.br.iDb, u.br.iMoved, pOp->p1); |
| 68276 | + if( rc==SQLITE_OK && u.bu.iMoved!=0 ){ |
| 68277 | + sqlite3RootPageMoved(db, u.bu.iDb, u.bu.iMoved, pOp->p1); |
| 67581 | 68278 | /* All OP_Destroy operations occur on the same btree */ |
| 67582 | | - assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.br.iDb+1 ); |
| 67583 | | - resetSchemaOnFault = u.br.iDb+1; |
| 68279 | + assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bu.iDb+1 ); |
| 68280 | + resetSchemaOnFault = u.bu.iDb+1; |
| 67584 | 68281 | } |
| 67585 | 68282 | #endif |
| 67586 | 68283 | } |
| 67587 | 68284 | break; |
| 67588 | 68285 | } |
| | @@ -67604,25 +68301,25 @@ |
| 67604 | 68301 | ** also incremented by the number of rows in the table being cleared. |
| 67605 | 68302 | ** |
| 67606 | 68303 | ** See also: Destroy |
| 67607 | 68304 | */ |
| 67608 | 68305 | case OP_Clear: { |
| 67609 | | -#if 0 /* local variables moved into u.bs */ |
| 68306 | +#if 0 /* local variables moved into u.bv */ |
| 67610 | 68307 | int nChange; |
| 67611 | | -#endif /* local variables moved into u.bs */ |
| 68308 | +#endif /* local variables moved into u.bv */ |
| 67612 | 68309 | |
| 67613 | | - u.bs.nChange = 0; |
| 68310 | + u.bv.nChange = 0; |
| 67614 | 68311 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 ); |
| 67615 | 68312 | rc = sqlite3BtreeClearTable( |
| 67616 | | - db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0) |
| 68313 | + db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0) |
| 67617 | 68314 | ); |
| 67618 | 68315 | if( pOp->p3 ){ |
| 67619 | | - p->nChange += u.bs.nChange; |
| 68316 | + p->nChange += u.bv.nChange; |
| 67620 | 68317 | if( pOp->p3>0 ){ |
| 67621 | 68318 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 67622 | 68319 | memAboutToChange(p, &aMem[pOp->p3]); |
| 67623 | | - aMem[pOp->p3].u.i += u.bs.nChange; |
| 68320 | + aMem[pOp->p3].u.i += u.bv.nChange; |
| 67624 | 68321 | } |
| 67625 | 68322 | } |
| 67626 | 68323 | break; |
| 67627 | 68324 | } |
| 67628 | 68325 | |
| | @@ -67648,29 +68345,29 @@ |
| 67648 | 68345 | ** |
| 67649 | 68346 | ** See documentation on OP_CreateTable for additional information. |
| 67650 | 68347 | */ |
| 67651 | 68348 | case OP_CreateIndex: /* out2-prerelease */ |
| 67652 | 68349 | case OP_CreateTable: { /* out2-prerelease */ |
| 67653 | | -#if 0 /* local variables moved into u.bt */ |
| 68350 | +#if 0 /* local variables moved into u.bw */ |
| 67654 | 68351 | int pgno; |
| 67655 | 68352 | int flags; |
| 67656 | 68353 | Db *pDb; |
| 67657 | | -#endif /* local variables moved into u.bt */ |
| 68354 | +#endif /* local variables moved into u.bw */ |
| 67658 | 68355 | |
| 67659 | | - u.bt.pgno = 0; |
| 68356 | + u.bw.pgno = 0; |
| 67660 | 68357 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 67661 | 68358 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 ); |
| 67662 | | - u.bt.pDb = &db->aDb[pOp->p1]; |
| 67663 | | - assert( u.bt.pDb->pBt!=0 ); |
| 68359 | + u.bw.pDb = &db->aDb[pOp->p1]; |
| 68360 | + assert( u.bw.pDb->pBt!=0 ); |
| 67664 | 68361 | if( pOp->opcode==OP_CreateTable ){ |
| 67665 | | - /* u.bt.flags = BTREE_INTKEY; */ |
| 67666 | | - u.bt.flags = BTREE_INTKEY; |
| 68362 | + /* u.bw.flags = BTREE_INTKEY; */ |
| 68363 | + u.bw.flags = BTREE_INTKEY; |
| 67667 | 68364 | }else{ |
| 67668 | | - u.bt.flags = BTREE_BLOBKEY; |
| 68365 | + u.bw.flags = BTREE_BLOBKEY; |
| 67669 | 68366 | } |
| 67670 | | - rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags); |
| 67671 | | - pOut->u.i = u.bt.pgno; |
| 68367 | + rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags); |
| 68368 | + pOut->u.i = u.bw.pgno; |
| 67672 | 68369 | break; |
| 67673 | 68370 | } |
| 67674 | 68371 | |
| 67675 | 68372 | /* Opcode: ParseSchema P1 * * P4 * |
| 67676 | 68373 | ** |
| | @@ -67679,48 +68376,48 @@ |
| 67679 | 68376 | ** |
| 67680 | 68377 | ** This opcode invokes the parser to create a new virtual machine, |
| 67681 | 68378 | ** then runs the new virtual machine. It is thus a re-entrant opcode. |
| 67682 | 68379 | */ |
| 67683 | 68380 | case OP_ParseSchema: { |
| 67684 | | -#if 0 /* local variables moved into u.bu */ |
| 68381 | +#if 0 /* local variables moved into u.bx */ |
| 67685 | 68382 | int iDb; |
| 67686 | 68383 | const char *zMaster; |
| 67687 | 68384 | char *zSql; |
| 67688 | 68385 | InitData initData; |
| 67689 | | -#endif /* local variables moved into u.bu */ |
| 68386 | +#endif /* local variables moved into u.bx */ |
| 67690 | 68387 | |
| 67691 | 68388 | /* Any prepared statement that invokes this opcode will hold mutexes |
| 67692 | 68389 | ** on every btree. This is a prerequisite for invoking |
| 67693 | 68390 | ** sqlite3InitCallback(). |
| 67694 | 68391 | */ |
| 67695 | 68392 | #ifdef SQLITE_DEBUG |
| 67696 | | - for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){ |
| 67697 | | - assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) ); |
| 68393 | + for(u.bx.iDb=0; u.bx.iDb<db->nDb; u.bx.iDb++){ |
| 68394 | + assert( u.bx.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) ); |
| 67698 | 68395 | } |
| 67699 | 68396 | #endif |
| 67700 | 68397 | |
| 67701 | | - u.bu.iDb = pOp->p1; |
| 67702 | | - assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb ); |
| 67703 | | - assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) ); |
| 68398 | + u.bx.iDb = pOp->p1; |
| 68399 | + assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb ); |
| 68400 | + assert( DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) ); |
| 67704 | 68401 | /* Used to be a conditional */ { |
| 67705 | | - u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb); |
| 67706 | | - u.bu.initData.db = db; |
| 67707 | | - u.bu.initData.iDb = pOp->p1; |
| 67708 | | - u.bu.initData.pzErrMsg = &p->zErrMsg; |
| 67709 | | - u.bu.zSql = sqlite3MPrintf(db, |
| 68402 | + u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb); |
| 68403 | + u.bx.initData.db = db; |
| 68404 | + u.bx.initData.iDb = pOp->p1; |
| 68405 | + u.bx.initData.pzErrMsg = &p->zErrMsg; |
| 68406 | + u.bx.zSql = sqlite3MPrintf(db, |
| 67710 | 68407 | "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", |
| 67711 | | - db->aDb[u.bu.iDb].zName, u.bu.zMaster, pOp->p4.z); |
| 67712 | | - if( u.bu.zSql==0 ){ |
| 68408 | + db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z); |
| 68409 | + if( u.bx.zSql==0 ){ |
| 67713 | 68410 | rc = SQLITE_NOMEM; |
| 67714 | 68411 | }else{ |
| 67715 | 68412 | assert( db->init.busy==0 ); |
| 67716 | 68413 | db->init.busy = 1; |
| 67717 | | - u.bu.initData.rc = SQLITE_OK; |
| 68414 | + u.bx.initData.rc = SQLITE_OK; |
| 67718 | 68415 | assert( !db->mallocFailed ); |
| 67719 | | - rc = sqlite3_exec(db, u.bu.zSql, sqlite3InitCallback, &u.bu.initData, 0); |
| 67720 | | - if( rc==SQLITE_OK ) rc = u.bu.initData.rc; |
| 67721 | | - sqlite3DbFree(db, u.bu.zSql); |
| 68416 | + rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0); |
| 68417 | + if( rc==SQLITE_OK ) rc = u.bx.initData.rc; |
| 68418 | + sqlite3DbFree(db, u.bx.zSql); |
| 67722 | 68419 | db->init.busy = 0; |
| 67723 | 68420 | } |
| 67724 | 68421 | } |
| 67725 | 68422 | if( rc==SQLITE_NOMEM ){ |
| 67726 | 68423 | goto no_mem; |
| | @@ -67799,45 +68496,45 @@ |
| 67799 | 68496 | ** file, not the main database file. |
| 67800 | 68497 | ** |
| 67801 | 68498 | ** This opcode is used to implement the integrity_check pragma. |
| 67802 | 68499 | */ |
| 67803 | 68500 | case OP_IntegrityCk: { |
| 67804 | | -#if 0 /* local variables moved into u.bv */ |
| 68501 | +#if 0 /* local variables moved into u.by */ |
| 67805 | 68502 | int nRoot; /* Number of tables to check. (Number of root pages.) */ |
| 67806 | 68503 | int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
| 67807 | 68504 | int j; /* Loop counter */ |
| 67808 | 68505 | int nErr; /* Number of errors reported */ |
| 67809 | 68506 | char *z; /* Text of the error report */ |
| 67810 | 68507 | Mem *pnErr; /* Register keeping track of errors remaining */ |
| 67811 | | -#endif /* local variables moved into u.bv */ |
| 68508 | +#endif /* local variables moved into u.by */ |
| 67812 | 68509 | |
| 67813 | | - u.bv.nRoot = pOp->p2; |
| 67814 | | - assert( u.bv.nRoot>0 ); |
| 67815 | | - u.bv.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bv.nRoot+1) ); |
| 67816 | | - if( u.bv.aRoot==0 ) goto no_mem; |
| 68510 | + u.by.nRoot = pOp->p2; |
| 68511 | + assert( u.by.nRoot>0 ); |
| 68512 | + u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) ); |
| 68513 | + if( u.by.aRoot==0 ) goto no_mem; |
| 67817 | 68514 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 67818 | | - u.bv.pnErr = &aMem[pOp->p3]; |
| 67819 | | - assert( (u.bv.pnErr->flags & MEM_Int)!=0 ); |
| 67820 | | - assert( (u.bv.pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
| 68515 | + u.by.pnErr = &aMem[pOp->p3]; |
| 68516 | + assert( (u.by.pnErr->flags & MEM_Int)!=0 ); |
| 68517 | + assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
| 67821 | 68518 | pIn1 = &aMem[pOp->p1]; |
| 67822 | | - for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){ |
| 67823 | | - u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]); |
| 68519 | + for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){ |
| 68520 | + u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]); |
| 67824 | 68521 | } |
| 67825 | | - u.bv.aRoot[u.bv.j] = 0; |
| 68522 | + u.by.aRoot[u.by.j] = 0; |
| 67826 | 68523 | assert( pOp->p5<db->nDb ); |
| 67827 | 68524 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 ); |
| 67828 | | - u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot, |
| 67829 | | - (int)u.bv.pnErr->u.i, &u.bv.nErr); |
| 67830 | | - sqlite3DbFree(db, u.bv.aRoot); |
| 67831 | | - u.bv.pnErr->u.i -= u.bv.nErr; |
| 68525 | + u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot, |
| 68526 | + (int)u.by.pnErr->u.i, &u.by.nErr); |
| 68527 | + sqlite3DbFree(db, u.by.aRoot); |
| 68528 | + u.by.pnErr->u.i -= u.by.nErr; |
| 67832 | 68529 | sqlite3VdbeMemSetNull(pIn1); |
| 67833 | | - if( u.bv.nErr==0 ){ |
| 67834 | | - assert( u.bv.z==0 ); |
| 67835 | | - }else if( u.bv.z==0 ){ |
| 68530 | + if( u.by.nErr==0 ){ |
| 68531 | + assert( u.by.z==0 ); |
| 68532 | + }else if( u.by.z==0 ){ |
| 67836 | 68533 | goto no_mem; |
| 67837 | 68534 | }else{ |
| 67838 | | - sqlite3VdbeMemSetStr(pIn1, u.bv.z, -1, SQLITE_UTF8, sqlite3_free); |
| 68535 | + sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free); |
| 67839 | 68536 | } |
| 67840 | 68537 | UPDATE_MAX_BLOBSIZE(pIn1); |
| 67841 | 68538 | sqlite3VdbeChangeEncoding(pIn1, encoding); |
| 67842 | 68539 | break; |
| 67843 | 68540 | } |
| | @@ -67867,24 +68564,24 @@ |
| 67867 | 68564 | ** Extract the smallest value from boolean index P1 and put that value into |
| 67868 | 68565 | ** register P3. Or, if boolean index P1 is initially empty, leave P3 |
| 67869 | 68566 | ** unchanged and jump to instruction P2. |
| 67870 | 68567 | */ |
| 67871 | 68568 | case OP_RowSetRead: { /* jump, in1, out3 */ |
| 67872 | | -#if 0 /* local variables moved into u.bw */ |
| 68569 | +#if 0 /* local variables moved into u.bz */ |
| 67873 | 68570 | i64 val; |
| 67874 | | -#endif /* local variables moved into u.bw */ |
| 68571 | +#endif /* local variables moved into u.bz */ |
| 67875 | 68572 | CHECK_FOR_INTERRUPT; |
| 67876 | 68573 | pIn1 = &aMem[pOp->p1]; |
| 67877 | 68574 | if( (pIn1->flags & MEM_RowSet)==0 |
| 67878 | | - || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bw.val)==0 |
| 68575 | + || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bz.val)==0 |
| 67879 | 68576 | ){ |
| 67880 | 68577 | /* The boolean index is empty */ |
| 67881 | 68578 | sqlite3VdbeMemSetNull(pIn1); |
| 67882 | 68579 | pc = pOp->p2 - 1; |
| 67883 | 68580 | }else{ |
| 67884 | 68581 | /* A value was pulled from the index */ |
| 67885 | | - sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bw.val); |
| 68582 | + sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bz.val); |
| 67886 | 68583 | } |
| 67887 | 68584 | break; |
| 67888 | 68585 | } |
| 67889 | 68586 | |
| 67890 | 68587 | /* Opcode: RowSetTest P1 P2 P3 P4 |
| | @@ -67909,18 +68606,18 @@ |
| 67909 | 68606 | ** inserted, there is no need to search to see if the same value was |
| 67910 | 68607 | ** previously inserted as part of set X (only if it was previously |
| 67911 | 68608 | ** inserted as part of some other set). |
| 67912 | 68609 | */ |
| 67913 | 68610 | case OP_RowSetTest: { /* jump, in1, in3 */ |
| 67914 | | -#if 0 /* local variables moved into u.bx */ |
| 68611 | +#if 0 /* local variables moved into u.ca */ |
| 67915 | 68612 | int iSet; |
| 67916 | 68613 | int exists; |
| 67917 | | -#endif /* local variables moved into u.bx */ |
| 68614 | +#endif /* local variables moved into u.ca */ |
| 67918 | 68615 | |
| 67919 | 68616 | pIn1 = &aMem[pOp->p1]; |
| 67920 | 68617 | pIn3 = &aMem[pOp->p3]; |
| 67921 | | - u.bx.iSet = pOp->p4.i; |
| 68618 | + u.ca.iSet = pOp->p4.i; |
| 67922 | 68619 | assert( pIn3->flags&MEM_Int ); |
| 67923 | 68620 | |
| 67924 | 68621 | /* If there is anything other than a rowset object in memory cell P1, |
| 67925 | 68622 | ** delete it now and initialize P1 with an empty rowset |
| 67926 | 68623 | */ |
| | @@ -67928,21 +68625,21 @@ |
| 67928 | 68625 | sqlite3VdbeMemSetRowSet(pIn1); |
| 67929 | 68626 | if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
| 67930 | 68627 | } |
| 67931 | 68628 | |
| 67932 | 68629 | assert( pOp->p4type==P4_INT32 ); |
| 67933 | | - assert( u.bx.iSet==-1 || u.bx.iSet>=0 ); |
| 67934 | | - if( u.bx.iSet ){ |
| 67935 | | - u.bx.exists = sqlite3RowSetTest(pIn1->u.pRowSet, |
| 67936 | | - (u8)(u.bx.iSet>=0 ? u.bx.iSet & 0xf : 0xff), |
| 68630 | + assert( u.ca.iSet==-1 || u.ca.iSet>=0 ); |
| 68631 | + if( u.ca.iSet ){ |
| 68632 | + u.ca.exists = sqlite3RowSetTest(pIn1->u.pRowSet, |
| 68633 | + (u8)(u.ca.iSet>=0 ? u.ca.iSet & 0xf : 0xff), |
| 67937 | 68634 | pIn3->u.i); |
| 67938 | | - if( u.bx.exists ){ |
| 68635 | + if( u.ca.exists ){ |
| 67939 | 68636 | pc = pOp->p2 - 1; |
| 67940 | 68637 | break; |
| 67941 | 68638 | } |
| 67942 | 68639 | } |
| 67943 | | - if( u.bx.iSet>=0 ){ |
| 68640 | + if( u.ca.iSet>=0 ){ |
| 67944 | 68641 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
| 67945 | 68642 | } |
| 67946 | 68643 | break; |
| 67947 | 68644 | } |
| 67948 | 68645 | |
| | @@ -67961,25 +68658,25 @@ |
| 67961 | 68658 | ** memory required by the sub-vdbe at runtime. |
| 67962 | 68659 | ** |
| 67963 | 68660 | ** P4 is a pointer to the VM containing the trigger program. |
| 67964 | 68661 | */ |
| 67965 | 68662 | case OP_Program: { /* jump */ |
| 67966 | | -#if 0 /* local variables moved into u.by */ |
| 68663 | +#if 0 /* local variables moved into u.cb */ |
| 67967 | 68664 | int nMem; /* Number of memory registers for sub-program */ |
| 67968 | 68665 | int nByte; /* Bytes of runtime space required for sub-program */ |
| 67969 | 68666 | Mem *pRt; /* Register to allocate runtime space */ |
| 67970 | 68667 | Mem *pMem; /* Used to iterate through memory cells */ |
| 67971 | 68668 | Mem *pEnd; /* Last memory cell in new array */ |
| 67972 | 68669 | VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
| 67973 | 68670 | SubProgram *pProgram; /* Sub-program to execute */ |
| 67974 | 68671 | void *t; /* Token identifying trigger */ |
| 67975 | | -#endif /* local variables moved into u.by */ |
| 68672 | +#endif /* local variables moved into u.cb */ |
| 67976 | 68673 | |
| 67977 | | - u.by.pProgram = pOp->p4.pProgram; |
| 67978 | | - u.by.pRt = &aMem[pOp->p3]; |
| 67979 | | - assert( memIsValid(u.by.pRt) ); |
| 67980 | | - assert( u.by.pProgram->nOp>0 ); |
| 68674 | + u.cb.pProgram = pOp->p4.pProgram; |
| 68675 | + u.cb.pRt = &aMem[pOp->p3]; |
| 68676 | + assert( memIsValid(u.cb.pRt) ); |
| 68677 | + assert( u.cb.pProgram->nOp>0 ); |
| 67981 | 68678 | |
| 67982 | 68679 | /* If the p5 flag is clear, then recursive invocation of triggers is |
| 67983 | 68680 | ** disabled for backwards compatibility (p5 is set if this sub-program |
| 67984 | 68681 | ** is really a trigger, not a foreign key action, and the flag set |
| 67985 | 68682 | ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
| | @@ -67989,79 +68686,79 @@ |
| 67989 | 68686 | ** SubProgram (if the trigger may be executed with more than one different |
| 67990 | 68687 | ** ON CONFLICT algorithm). SubProgram structures associated with a |
| 67991 | 68688 | ** single trigger all have the same value for the SubProgram.token |
| 67992 | 68689 | ** variable. */ |
| 67993 | 68690 | if( pOp->p5 ){ |
| 67994 | | - u.by.t = u.by.pProgram->token; |
| 67995 | | - for(u.by.pFrame=p->pFrame; u.by.pFrame && u.by.pFrame->token!=u.by.t; u.by.pFrame=u.by.pFrame->pParent); |
| 67996 | | - if( u.by.pFrame ) break; |
| 68691 | + u.cb.t = u.cb.pProgram->token; |
| 68692 | + for(u.cb.pFrame=p->pFrame; u.cb.pFrame && u.cb.pFrame->token!=u.cb.t; u.cb.pFrame=u.cb.pFrame->pParent); |
| 68693 | + if( u.cb.pFrame ) break; |
| 67997 | 68694 | } |
| 67998 | 68695 | |
| 67999 | 68696 | if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
| 68000 | 68697 | rc = SQLITE_ERROR; |
| 68001 | 68698 | sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion"); |
| 68002 | 68699 | break; |
| 68003 | 68700 | } |
| 68004 | 68701 | |
| 68005 | | - /* Register u.by.pRt is used to store the memory required to save the state |
| 68702 | + /* Register u.cb.pRt is used to store the memory required to save the state |
| 68006 | 68703 | ** of the current program, and the memory required at runtime to execute |
| 68007 | | - ** the trigger program. If this trigger has been fired before, then u.by.pRt |
| 68704 | + ** the trigger program. If this trigger has been fired before, then u.cb.pRt |
| 68008 | 68705 | ** is already allocated. Otherwise, it must be initialized. */ |
| 68009 | | - if( (u.by.pRt->flags&MEM_Frame)==0 ){ |
| 68706 | + if( (u.cb.pRt->flags&MEM_Frame)==0 ){ |
| 68010 | 68707 | /* SubProgram.nMem is set to the number of memory cells used by the |
| 68011 | 68708 | ** program stored in SubProgram.aOp. As well as these, one memory |
| 68012 | 68709 | ** cell is required for each cursor used by the program. Set local |
| 68013 | | - ** variable u.by.nMem (and later, VdbeFrame.nChildMem) to this value. |
| 68710 | + ** variable u.cb.nMem (and later, VdbeFrame.nChildMem) to this value. |
| 68014 | 68711 | */ |
| 68015 | | - u.by.nMem = u.by.pProgram->nMem + u.by.pProgram->nCsr; |
| 68016 | | - u.by.nByte = ROUND8(sizeof(VdbeFrame)) |
| 68017 | | - + u.by.nMem * sizeof(Mem) |
| 68018 | | - + u.by.pProgram->nCsr * sizeof(VdbeCursor *); |
| 68019 | | - u.by.pFrame = sqlite3DbMallocZero(db, u.by.nByte); |
| 68020 | | - if( !u.by.pFrame ){ |
| 68712 | + u.cb.nMem = u.cb.pProgram->nMem + u.cb.pProgram->nCsr; |
| 68713 | + u.cb.nByte = ROUND8(sizeof(VdbeFrame)) |
| 68714 | + + u.cb.nMem * sizeof(Mem) |
| 68715 | + + u.cb.pProgram->nCsr * sizeof(VdbeCursor *); |
| 68716 | + u.cb.pFrame = sqlite3DbMallocZero(db, u.cb.nByte); |
| 68717 | + if( !u.cb.pFrame ){ |
| 68021 | 68718 | goto no_mem; |
| 68022 | 68719 | } |
| 68023 | | - sqlite3VdbeMemRelease(u.by.pRt); |
| 68024 | | - u.by.pRt->flags = MEM_Frame; |
| 68025 | | - u.by.pRt->u.pFrame = u.by.pFrame; |
| 68026 | | - |
| 68027 | | - u.by.pFrame->v = p; |
| 68028 | | - u.by.pFrame->nChildMem = u.by.nMem; |
| 68029 | | - u.by.pFrame->nChildCsr = u.by.pProgram->nCsr; |
| 68030 | | - u.by.pFrame->pc = pc; |
| 68031 | | - u.by.pFrame->aMem = p->aMem; |
| 68032 | | - u.by.pFrame->nMem = p->nMem; |
| 68033 | | - u.by.pFrame->apCsr = p->apCsr; |
| 68034 | | - u.by.pFrame->nCursor = p->nCursor; |
| 68035 | | - u.by.pFrame->aOp = p->aOp; |
| 68036 | | - u.by.pFrame->nOp = p->nOp; |
| 68037 | | - u.by.pFrame->token = u.by.pProgram->token; |
| 68038 | | - |
| 68039 | | - u.by.pEnd = &VdbeFrameMem(u.by.pFrame)[u.by.pFrame->nChildMem]; |
| 68040 | | - for(u.by.pMem=VdbeFrameMem(u.by.pFrame); u.by.pMem!=u.by.pEnd; u.by.pMem++){ |
| 68041 | | - u.by.pMem->flags = MEM_Null; |
| 68042 | | - u.by.pMem->db = db; |
| 68720 | + sqlite3VdbeMemRelease(u.cb.pRt); |
| 68721 | + u.cb.pRt->flags = MEM_Frame; |
| 68722 | + u.cb.pRt->u.pFrame = u.cb.pFrame; |
| 68723 | + |
| 68724 | + u.cb.pFrame->v = p; |
| 68725 | + u.cb.pFrame->nChildMem = u.cb.nMem; |
| 68726 | + u.cb.pFrame->nChildCsr = u.cb.pProgram->nCsr; |
| 68727 | + u.cb.pFrame->pc = pc; |
| 68728 | + u.cb.pFrame->aMem = p->aMem; |
| 68729 | + u.cb.pFrame->nMem = p->nMem; |
| 68730 | + u.cb.pFrame->apCsr = p->apCsr; |
| 68731 | + u.cb.pFrame->nCursor = p->nCursor; |
| 68732 | + u.cb.pFrame->aOp = p->aOp; |
| 68733 | + u.cb.pFrame->nOp = p->nOp; |
| 68734 | + u.cb.pFrame->token = u.cb.pProgram->token; |
| 68735 | + |
| 68736 | + u.cb.pEnd = &VdbeFrameMem(u.cb.pFrame)[u.cb.pFrame->nChildMem]; |
| 68737 | + for(u.cb.pMem=VdbeFrameMem(u.cb.pFrame); u.cb.pMem!=u.cb.pEnd; u.cb.pMem++){ |
| 68738 | + u.cb.pMem->flags = MEM_Null; |
| 68739 | + u.cb.pMem->db = db; |
| 68043 | 68740 | } |
| 68044 | 68741 | }else{ |
| 68045 | | - u.by.pFrame = u.by.pRt->u.pFrame; |
| 68046 | | - assert( u.by.pProgram->nMem+u.by.pProgram->nCsr==u.by.pFrame->nChildMem ); |
| 68047 | | - assert( u.by.pProgram->nCsr==u.by.pFrame->nChildCsr ); |
| 68048 | | - assert( pc==u.by.pFrame->pc ); |
| 68742 | + u.cb.pFrame = u.cb.pRt->u.pFrame; |
| 68743 | + assert( u.cb.pProgram->nMem+u.cb.pProgram->nCsr==u.cb.pFrame->nChildMem ); |
| 68744 | + assert( u.cb.pProgram->nCsr==u.cb.pFrame->nChildCsr ); |
| 68745 | + assert( pc==u.cb.pFrame->pc ); |
| 68049 | 68746 | } |
| 68050 | 68747 | |
| 68051 | 68748 | p->nFrame++; |
| 68052 | | - u.by.pFrame->pParent = p->pFrame; |
| 68053 | | - u.by.pFrame->lastRowid = lastRowid; |
| 68054 | | - u.by.pFrame->nChange = p->nChange; |
| 68749 | + u.cb.pFrame->pParent = p->pFrame; |
| 68750 | + u.cb.pFrame->lastRowid = lastRowid; |
| 68751 | + u.cb.pFrame->nChange = p->nChange; |
| 68055 | 68752 | p->nChange = 0; |
| 68056 | | - p->pFrame = u.by.pFrame; |
| 68057 | | - p->aMem = aMem = &VdbeFrameMem(u.by.pFrame)[-1]; |
| 68058 | | - p->nMem = u.by.pFrame->nChildMem; |
| 68059 | | - p->nCursor = (u16)u.by.pFrame->nChildCsr; |
| 68753 | + p->pFrame = u.cb.pFrame; |
| 68754 | + p->aMem = aMem = &VdbeFrameMem(u.cb.pFrame)[-1]; |
| 68755 | + p->nMem = u.cb.pFrame->nChildMem; |
| 68756 | + p->nCursor = (u16)u.cb.pFrame->nChildCsr; |
| 68060 | 68757 | p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; |
| 68061 | | - p->aOp = aOp = u.by.pProgram->aOp; |
| 68062 | | - p->nOp = u.by.pProgram->nOp; |
| 68758 | + p->aOp = aOp = u.cb.pProgram->aOp; |
| 68759 | + p->nOp = u.cb.pProgram->nOp; |
| 68063 | 68760 | pc = -1; |
| 68064 | 68761 | |
| 68065 | 68762 | break; |
| 68066 | 68763 | } |
| 68067 | 68764 | |
| | @@ -68076,17 +68773,17 @@ |
| 68076 | 68773 | ** The address of the cell in the parent frame is determined by adding |
| 68077 | 68774 | ** the value of the P1 argument to the value of the P1 argument to the |
| 68078 | 68775 | ** calling OP_Program instruction. |
| 68079 | 68776 | */ |
| 68080 | 68777 | case OP_Param: { /* out2-prerelease */ |
| 68081 | | -#if 0 /* local variables moved into u.bz */ |
| 68778 | +#if 0 /* local variables moved into u.cc */ |
| 68082 | 68779 | VdbeFrame *pFrame; |
| 68083 | 68780 | Mem *pIn; |
| 68084 | | -#endif /* local variables moved into u.bz */ |
| 68085 | | - u.bz.pFrame = p->pFrame; |
| 68086 | | - u.bz.pIn = &u.bz.pFrame->aMem[pOp->p1 + u.bz.pFrame->aOp[u.bz.pFrame->pc].p1]; |
| 68087 | | - sqlite3VdbeMemShallowCopy(pOut, u.bz.pIn, MEM_Ephem); |
| 68781 | +#endif /* local variables moved into u.cc */ |
| 68782 | + u.cc.pFrame = p->pFrame; |
| 68783 | + u.cc.pIn = &u.cc.pFrame->aMem[pOp->p1 + u.cc.pFrame->aOp[u.cc.pFrame->pc].p1]; |
| 68784 | + sqlite3VdbeMemShallowCopy(pOut, u.cc.pIn, MEM_Ephem); |
| 68088 | 68785 | break; |
| 68089 | 68786 | } |
| 68090 | 68787 | |
| 68091 | 68788 | #endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
| 68092 | 68789 | |
| | @@ -68138,26 +68835,26 @@ |
| 68138 | 68835 | ** |
| 68139 | 68836 | ** This instruction throws an error if the memory cell is not initially |
| 68140 | 68837 | ** an integer. |
| 68141 | 68838 | */ |
| 68142 | 68839 | case OP_MemMax: { /* in2 */ |
| 68143 | | -#if 0 /* local variables moved into u.ca */ |
| 68840 | +#if 0 /* local variables moved into u.cd */ |
| 68144 | 68841 | Mem *pIn1; |
| 68145 | 68842 | VdbeFrame *pFrame; |
| 68146 | | -#endif /* local variables moved into u.ca */ |
| 68843 | +#endif /* local variables moved into u.cd */ |
| 68147 | 68844 | if( p->pFrame ){ |
| 68148 | | - for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent); |
| 68149 | | - u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1]; |
| 68845 | + for(u.cd.pFrame=p->pFrame; u.cd.pFrame->pParent; u.cd.pFrame=u.cd.pFrame->pParent); |
| 68846 | + u.cd.pIn1 = &u.cd.pFrame->aMem[pOp->p1]; |
| 68150 | 68847 | }else{ |
| 68151 | | - u.ca.pIn1 = &aMem[pOp->p1]; |
| 68848 | + u.cd.pIn1 = &aMem[pOp->p1]; |
| 68152 | 68849 | } |
| 68153 | | - assert( memIsValid(u.ca.pIn1) ); |
| 68154 | | - sqlite3VdbeMemIntegerify(u.ca.pIn1); |
| 68850 | + assert( memIsValid(u.cd.pIn1) ); |
| 68851 | + sqlite3VdbeMemIntegerify(u.cd.pIn1); |
| 68155 | 68852 | pIn2 = &aMem[pOp->p2]; |
| 68156 | 68853 | sqlite3VdbeMemIntegerify(pIn2); |
| 68157 | | - if( u.ca.pIn1->u.i<pIn2->u.i){ |
| 68158 | | - u.ca.pIn1->u.i = pIn2->u.i; |
| 68854 | + if( u.cd.pIn1->u.i<pIn2->u.i){ |
| 68855 | + u.cd.pIn1->u.i = pIn2->u.i; |
| 68159 | 68856 | } |
| 68160 | 68857 | break; |
| 68161 | 68858 | } |
| 68162 | 68859 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 68163 | 68860 | |
| | @@ -68220,54 +68917,54 @@ |
| 68220 | 68917 | ** |
| 68221 | 68918 | ** The P5 arguments are taken from register P2 and its |
| 68222 | 68919 | ** successors. |
| 68223 | 68920 | */ |
| 68224 | 68921 | case OP_AggStep: { |
| 68225 | | -#if 0 /* local variables moved into u.cb */ |
| 68922 | +#if 0 /* local variables moved into u.ce */ |
| 68226 | 68923 | int n; |
| 68227 | 68924 | int i; |
| 68228 | 68925 | Mem *pMem; |
| 68229 | 68926 | Mem *pRec; |
| 68230 | 68927 | sqlite3_context ctx; |
| 68231 | 68928 | sqlite3_value **apVal; |
| 68232 | | -#endif /* local variables moved into u.cb */ |
| 68233 | | - |
| 68234 | | - u.cb.n = pOp->p5; |
| 68235 | | - assert( u.cb.n>=0 ); |
| 68236 | | - u.cb.pRec = &aMem[pOp->p2]; |
| 68237 | | - u.cb.apVal = p->apArg; |
| 68238 | | - assert( u.cb.apVal || u.cb.n==0 ); |
| 68239 | | - for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){ |
| 68240 | | - assert( memIsValid(u.cb.pRec) ); |
| 68241 | | - u.cb.apVal[u.cb.i] = u.cb.pRec; |
| 68242 | | - memAboutToChange(p, u.cb.pRec); |
| 68243 | | - sqlite3VdbeMemStoreType(u.cb.pRec); |
| 68244 | | - } |
| 68245 | | - u.cb.ctx.pFunc = pOp->p4.pFunc; |
| 68929 | +#endif /* local variables moved into u.ce */ |
| 68930 | + |
| 68931 | + u.ce.n = pOp->p5; |
| 68932 | + assert( u.ce.n>=0 ); |
| 68933 | + u.ce.pRec = &aMem[pOp->p2]; |
| 68934 | + u.ce.apVal = p->apArg; |
| 68935 | + assert( u.ce.apVal || u.ce.n==0 ); |
| 68936 | + for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){ |
| 68937 | + assert( memIsValid(u.ce.pRec) ); |
| 68938 | + u.ce.apVal[u.ce.i] = u.ce.pRec; |
| 68939 | + memAboutToChange(p, u.ce.pRec); |
| 68940 | + sqlite3VdbeMemStoreType(u.ce.pRec); |
| 68941 | + } |
| 68942 | + u.ce.ctx.pFunc = pOp->p4.pFunc; |
| 68246 | 68943 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 68247 | | - u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3]; |
| 68248 | | - u.cb.pMem->n++; |
| 68249 | | - u.cb.ctx.s.flags = MEM_Null; |
| 68250 | | - u.cb.ctx.s.z = 0; |
| 68251 | | - u.cb.ctx.s.zMalloc = 0; |
| 68252 | | - u.cb.ctx.s.xDel = 0; |
| 68253 | | - u.cb.ctx.s.db = db; |
| 68254 | | - u.cb.ctx.isError = 0; |
| 68255 | | - u.cb.ctx.pColl = 0; |
| 68256 | | - if( u.cb.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
| 68944 | + u.ce.ctx.pMem = u.ce.pMem = &aMem[pOp->p3]; |
| 68945 | + u.ce.pMem->n++; |
| 68946 | + u.ce.ctx.s.flags = MEM_Null; |
| 68947 | + u.ce.ctx.s.z = 0; |
| 68948 | + u.ce.ctx.s.zMalloc = 0; |
| 68949 | + u.ce.ctx.s.xDel = 0; |
| 68950 | + u.ce.ctx.s.db = db; |
| 68951 | + u.ce.ctx.isError = 0; |
| 68952 | + u.ce.ctx.pColl = 0; |
| 68953 | + if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ |
| 68257 | 68954 | assert( pOp>p->aOp ); |
| 68258 | 68955 | assert( pOp[-1].p4type==P4_COLLSEQ ); |
| 68259 | 68956 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 68260 | | - u.cb.ctx.pColl = pOp[-1].p4.pColl; |
| 68957 | + u.ce.ctx.pColl = pOp[-1].p4.pColl; |
| 68261 | 68958 | } |
| 68262 | | - (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */ |
| 68263 | | - if( u.cb.ctx.isError ){ |
| 68264 | | - sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s)); |
| 68265 | | - rc = u.cb.ctx.isError; |
| 68959 | + (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal); /* IMP: R-24505-23230 */ |
| 68960 | + if( u.ce.ctx.isError ){ |
| 68961 | + sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s)); |
| 68962 | + rc = u.ce.ctx.isError; |
| 68266 | 68963 | } |
| 68267 | 68964 | |
| 68268 | | - sqlite3VdbeMemRelease(&u.cb.ctx.s); |
| 68965 | + sqlite3VdbeMemRelease(&u.ce.ctx.s); |
| 68269 | 68966 | |
| 68270 | 68967 | break; |
| 68271 | 68968 | } |
| 68272 | 68969 | |
| 68273 | 68970 | /* Opcode: AggFinal P1 P2 * P4 * |
| | @@ -68281,23 +68978,23 @@ |
| 68281 | 68978 | ** functions that can take varying numbers of arguments. The |
| 68282 | 68979 | ** P4 argument is only needed for the degenerate case where |
| 68283 | 68980 | ** the step function was not previously called. |
| 68284 | 68981 | */ |
| 68285 | 68982 | case OP_AggFinal: { |
| 68286 | | -#if 0 /* local variables moved into u.cc */ |
| 68983 | +#if 0 /* local variables moved into u.cf */ |
| 68287 | 68984 | Mem *pMem; |
| 68288 | | -#endif /* local variables moved into u.cc */ |
| 68985 | +#endif /* local variables moved into u.cf */ |
| 68289 | 68986 | assert( pOp->p1>0 && pOp->p1<=p->nMem ); |
| 68290 | | - u.cc.pMem = &aMem[pOp->p1]; |
| 68291 | | - assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
| 68292 | | - rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc); |
| 68987 | + u.cf.pMem = &aMem[pOp->p1]; |
| 68988 | + assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
| 68989 | + rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc); |
| 68293 | 68990 | if( rc ){ |
| 68294 | | - sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem)); |
| 68991 | + sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem)); |
| 68295 | 68992 | } |
| 68296 | | - sqlite3VdbeChangeEncoding(u.cc.pMem, encoding); |
| 68297 | | - UPDATE_MAX_BLOBSIZE(u.cc.pMem); |
| 68298 | | - if( sqlite3VdbeMemTooBig(u.cc.pMem) ){ |
| 68993 | + sqlite3VdbeChangeEncoding(u.cf.pMem, encoding); |
| 68994 | + UPDATE_MAX_BLOBSIZE(u.cf.pMem); |
| 68995 | + if( sqlite3VdbeMemTooBig(u.cf.pMem) ){ |
| 68299 | 68996 | goto too_big; |
| 68300 | 68997 | } |
| 68301 | 68998 | break; |
| 68302 | 68999 | } |
| 68303 | 69000 | |
| | @@ -68312,29 +69009,29 @@ |
| 68312 | 69009 | ** in the WAL that have been checkpointed after the checkpoint |
| 68313 | 69010 | ** completes into mem[P3+2]. However on an error, mem[P3+1] and |
| 68314 | 69011 | ** mem[P3+2] are initialized to -1. |
| 68315 | 69012 | */ |
| 68316 | 69013 | case OP_Checkpoint: { |
| 68317 | | -#if 0 /* local variables moved into u.cd */ |
| 69014 | +#if 0 /* local variables moved into u.cg */ |
| 68318 | 69015 | int i; /* Loop counter */ |
| 68319 | 69016 | int aRes[3]; /* Results */ |
| 68320 | 69017 | Mem *pMem; /* Write results here */ |
| 68321 | | -#endif /* local variables moved into u.cd */ |
| 69018 | +#endif /* local variables moved into u.cg */ |
| 68322 | 69019 | |
| 68323 | | - u.cd.aRes[0] = 0; |
| 68324 | | - u.cd.aRes[1] = u.cd.aRes[2] = -1; |
| 69020 | + u.cg.aRes[0] = 0; |
| 69021 | + u.cg.aRes[1] = u.cg.aRes[2] = -1; |
| 68325 | 69022 | assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
| 68326 | 69023 | || pOp->p2==SQLITE_CHECKPOINT_FULL |
| 68327 | 69024 | || pOp->p2==SQLITE_CHECKPOINT_RESTART |
| 68328 | 69025 | ); |
| 68329 | | - rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]); |
| 69026 | + rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cg.aRes[1], &u.cg.aRes[2]); |
| 68330 | 69027 | if( rc==SQLITE_BUSY ){ |
| 68331 | 69028 | rc = SQLITE_OK; |
| 68332 | | - u.cd.aRes[0] = 1; |
| 69029 | + u.cg.aRes[0] = 1; |
| 68333 | 69030 | } |
| 68334 | | - for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){ |
| 68335 | | - sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]); |
| 69031 | + for(u.cg.i=0, u.cg.pMem = &aMem[pOp->p3]; u.cg.i<3; u.cg.i++, u.cg.pMem++){ |
| 69032 | + sqlite3VdbeMemSetInt64(u.cg.pMem, (i64)u.cg.aRes[u.cg.i]); |
| 68336 | 69033 | } |
| 68337 | 69034 | break; |
| 68338 | 69035 | }; |
| 68339 | 69036 | #endif |
| 68340 | 69037 | |
| | @@ -68349,95 +69046,95 @@ |
| 68349 | 69046 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 68350 | 69047 | ** |
| 68351 | 69048 | ** Write a string containing the final journal-mode to register P2. |
| 68352 | 69049 | */ |
| 68353 | 69050 | case OP_JournalMode: { /* out2-prerelease */ |
| 68354 | | -#if 0 /* local variables moved into u.ce */ |
| 69051 | +#if 0 /* local variables moved into u.ch */ |
| 68355 | 69052 | Btree *pBt; /* Btree to change journal mode of */ |
| 68356 | 69053 | Pager *pPager; /* Pager associated with pBt */ |
| 68357 | 69054 | int eNew; /* New journal mode */ |
| 68358 | 69055 | int eOld; /* The old journal mode */ |
| 68359 | 69056 | const char *zFilename; /* Name of database file for pPager */ |
| 68360 | | -#endif /* local variables moved into u.ce */ |
| 68361 | | - |
| 68362 | | - u.ce.eNew = pOp->p3; |
| 68363 | | - assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE |
| 68364 | | - || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE |
| 68365 | | - || u.ce.eNew==PAGER_JOURNALMODE_PERSIST |
| 68366 | | - || u.ce.eNew==PAGER_JOURNALMODE_OFF |
| 68367 | | - || u.ce.eNew==PAGER_JOURNALMODE_MEMORY |
| 68368 | | - || u.ce.eNew==PAGER_JOURNALMODE_WAL |
| 68369 | | - || u.ce.eNew==PAGER_JOURNALMODE_QUERY |
| 69057 | +#endif /* local variables moved into u.ch */ |
| 69058 | + |
| 69059 | + u.ch.eNew = pOp->p3; |
| 69060 | + assert( u.ch.eNew==PAGER_JOURNALMODE_DELETE |
| 69061 | + || u.ch.eNew==PAGER_JOURNALMODE_TRUNCATE |
| 69062 | + || u.ch.eNew==PAGER_JOURNALMODE_PERSIST |
| 69063 | + || u.ch.eNew==PAGER_JOURNALMODE_OFF |
| 69064 | + || u.ch.eNew==PAGER_JOURNALMODE_MEMORY |
| 69065 | + || u.ch.eNew==PAGER_JOURNALMODE_WAL |
| 69066 | + || u.ch.eNew==PAGER_JOURNALMODE_QUERY |
| 68370 | 69067 | ); |
| 68371 | 69068 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 68372 | 69069 | |
| 68373 | | - u.ce.pBt = db->aDb[pOp->p1].pBt; |
| 68374 | | - u.ce.pPager = sqlite3BtreePager(u.ce.pBt); |
| 68375 | | - u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager); |
| 68376 | | - if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld; |
| 68377 | | - if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld; |
| 69070 | + u.ch.pBt = db->aDb[pOp->p1].pBt; |
| 69071 | + u.ch.pPager = sqlite3BtreePager(u.ch.pBt); |
| 69072 | + u.ch.eOld = sqlite3PagerGetJournalMode(u.ch.pPager); |
| 69073 | + if( u.ch.eNew==PAGER_JOURNALMODE_QUERY ) u.ch.eNew = u.ch.eOld; |
| 69074 | + if( !sqlite3PagerOkToChangeJournalMode(u.ch.pPager) ) u.ch.eNew = u.ch.eOld; |
| 68378 | 69075 | |
| 68379 | 69076 | #ifndef SQLITE_OMIT_WAL |
| 68380 | | - u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager); |
| 69077 | + u.ch.zFilename = sqlite3PagerFilename(u.ch.pPager); |
| 68381 | 69078 | |
| 68382 | 69079 | /* Do not allow a transition to journal_mode=WAL for a database |
| 68383 | 69080 | ** in temporary storage or if the VFS does not support shared memory |
| 68384 | 69081 | */ |
| 68385 | | - if( u.ce.eNew==PAGER_JOURNALMODE_WAL |
| 68386 | | - && (u.ce.zFilename[0]==0 /* Temp file */ |
| 68387 | | - || !sqlite3PagerWalSupported(u.ce.pPager)) /* No shared-memory support */ |
| 69082 | + if( u.ch.eNew==PAGER_JOURNALMODE_WAL |
| 69083 | + && (u.ch.zFilename[0]==0 /* Temp file */ |
| 69084 | + || !sqlite3PagerWalSupported(u.ch.pPager)) /* No shared-memory support */ |
| 68388 | 69085 | ){ |
| 68389 | | - u.ce.eNew = u.ce.eOld; |
| 69086 | + u.ch.eNew = u.ch.eOld; |
| 68390 | 69087 | } |
| 68391 | 69088 | |
| 68392 | | - if( (u.ce.eNew!=u.ce.eOld) |
| 68393 | | - && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL) |
| 69089 | + if( (u.ch.eNew!=u.ch.eOld) |
| 69090 | + && (u.ch.eOld==PAGER_JOURNALMODE_WAL || u.ch.eNew==PAGER_JOURNALMODE_WAL) |
| 68394 | 69091 | ){ |
| 68395 | 69092 | if( !db->autoCommit || db->activeVdbeCnt>1 ){ |
| 68396 | 69093 | rc = SQLITE_ERROR; |
| 68397 | 69094 | sqlite3SetString(&p->zErrMsg, db, |
| 68398 | 69095 | "cannot change %s wal mode from within a transaction", |
| 68399 | | - (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 69096 | + (u.ch.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
| 68400 | 69097 | ); |
| 68401 | 69098 | break; |
| 68402 | 69099 | }else{ |
| 68403 | 69100 | |
| 68404 | | - if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){ |
| 69101 | + if( u.ch.eOld==PAGER_JOURNALMODE_WAL ){ |
| 68405 | 69102 | /* If leaving WAL mode, close the log file. If successful, the call |
| 68406 | 69103 | ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
| 68407 | 69104 | ** file. An EXCLUSIVE lock may still be held on the database file |
| 68408 | 69105 | ** after a successful return. |
| 68409 | 69106 | */ |
| 68410 | | - rc = sqlite3PagerCloseWal(u.ce.pPager); |
| 69107 | + rc = sqlite3PagerCloseWal(u.ch.pPager); |
| 68411 | 69108 | if( rc==SQLITE_OK ){ |
| 68412 | | - sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew); |
| 69109 | + sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew); |
| 68413 | 69110 | } |
| 68414 | | - }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 69111 | + }else if( u.ch.eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 68415 | 69112 | /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 68416 | 69113 | ** as an intermediate */ |
| 68417 | | - sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF); |
| 69114 | + sqlite3PagerSetJournalMode(u.ch.pPager, PAGER_JOURNALMODE_OFF); |
| 68418 | 69115 | } |
| 68419 | 69116 | |
| 68420 | 69117 | /* Open a transaction on the database file. Regardless of the journal |
| 68421 | 69118 | ** mode, this transaction always uses a rollback journal. |
| 68422 | 69119 | */ |
| 68423 | | - assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 ); |
| 69120 | + assert( sqlite3BtreeIsInTrans(u.ch.pBt)==0 ); |
| 68424 | 69121 | if( rc==SQLITE_OK ){ |
| 68425 | | - rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
| 69122 | + rc = sqlite3BtreeSetVersion(u.ch.pBt, (u.ch.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
| 68426 | 69123 | } |
| 68427 | 69124 | } |
| 68428 | 69125 | } |
| 68429 | 69126 | #endif /* ifndef SQLITE_OMIT_WAL */ |
| 68430 | 69127 | |
| 68431 | 69128 | if( rc ){ |
| 68432 | | - u.ce.eNew = u.ce.eOld; |
| 69129 | + u.ch.eNew = u.ch.eOld; |
| 68433 | 69130 | } |
| 68434 | | - u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew); |
| 69131 | + u.ch.eNew = sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew); |
| 68435 | 69132 | |
| 68436 | 69133 | pOut = &aMem[pOp->p2]; |
| 68437 | 69134 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 68438 | | - pOut->z = (char *)sqlite3JournalModename(u.ce.eNew); |
| 69135 | + pOut->z = (char *)sqlite3JournalModename(u.ch.eNew); |
| 68439 | 69136 | pOut->n = sqlite3Strlen30(pOut->z); |
| 68440 | 69137 | pOut->enc = SQLITE_UTF8; |
| 68441 | 69138 | sqlite3VdbeChangeEncoding(pOut, encoding); |
| 68442 | 69139 | break; |
| 68443 | 69140 | }; |
| | @@ -68462,18 +69159,18 @@ |
| 68462 | 69159 | ** Perform a single step of the incremental vacuum procedure on |
| 68463 | 69160 | ** the P1 database. If the vacuum has finished, jump to instruction |
| 68464 | 69161 | ** P2. Otherwise, fall through to the next instruction. |
| 68465 | 69162 | */ |
| 68466 | 69163 | case OP_IncrVacuum: { /* jump */ |
| 68467 | | -#if 0 /* local variables moved into u.cf */ |
| 69164 | +#if 0 /* local variables moved into u.ci */ |
| 68468 | 69165 | Btree *pBt; |
| 68469 | | -#endif /* local variables moved into u.cf */ |
| 69166 | +#endif /* local variables moved into u.ci */ |
| 68470 | 69167 | |
| 68471 | 69168 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 68472 | 69169 | assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 ); |
| 68473 | | - u.cf.pBt = db->aDb[pOp->p1].pBt; |
| 68474 | | - rc = sqlite3BtreeIncrVacuum(u.cf.pBt); |
| 69170 | + u.ci.pBt = db->aDb[pOp->p1].pBt; |
| 69171 | + rc = sqlite3BtreeIncrVacuum(u.ci.pBt); |
| 68475 | 69172 | if( rc==SQLITE_DONE ){ |
| 68476 | 69173 | pc = pOp->p2 - 1; |
| 68477 | 69174 | rc = SQLITE_OK; |
| 68478 | 69175 | } |
| 68479 | 69176 | break; |
| | @@ -68539,16 +69236,16 @@ |
| 68539 | 69236 | ** Also, whether or not P4 is set, check that this is not being called from |
| 68540 | 69237 | ** within a callback to a virtual table xSync() method. If it is, the error |
| 68541 | 69238 | ** code will be set to SQLITE_LOCKED. |
| 68542 | 69239 | */ |
| 68543 | 69240 | case OP_VBegin: { |
| 68544 | | -#if 0 /* local variables moved into u.cg */ |
| 69241 | +#if 0 /* local variables moved into u.cj */ |
| 68545 | 69242 | VTable *pVTab; |
| 68546 | | -#endif /* local variables moved into u.cg */ |
| 68547 | | - u.cg.pVTab = pOp->p4.pVtab; |
| 68548 | | - rc = sqlite3VtabBegin(db, u.cg.pVTab); |
| 68549 | | - if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab); |
| 69243 | +#endif /* local variables moved into u.cj */ |
| 69244 | + u.cj.pVTab = pOp->p4.pVtab; |
| 69245 | + rc = sqlite3VtabBegin(db, u.cj.pVTab); |
| 69246 | + if( u.cj.pVTab ) importVtabErrMsg(p, u.cj.pVTab->pVtab); |
| 68550 | 69247 | break; |
| 68551 | 69248 | } |
| 68552 | 69249 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 68553 | 69250 | |
| 68554 | 69251 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -68583,36 +69280,36 @@ |
| 68583 | 69280 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
| 68584 | 69281 | ** P1 is a cursor number. This opcode opens a cursor to the virtual |
| 68585 | 69282 | ** table and stores that cursor in P1. |
| 68586 | 69283 | */ |
| 68587 | 69284 | case OP_VOpen: { |
| 68588 | | -#if 0 /* local variables moved into u.ch */ |
| 69285 | +#if 0 /* local variables moved into u.ck */ |
| 68589 | 69286 | VdbeCursor *pCur; |
| 68590 | 69287 | sqlite3_vtab_cursor *pVtabCursor; |
| 68591 | 69288 | sqlite3_vtab *pVtab; |
| 68592 | 69289 | sqlite3_module *pModule; |
| 68593 | | -#endif /* local variables moved into u.ch */ |
| 68594 | | - |
| 68595 | | - u.ch.pCur = 0; |
| 68596 | | - u.ch.pVtabCursor = 0; |
| 68597 | | - u.ch.pVtab = pOp->p4.pVtab->pVtab; |
| 68598 | | - u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule; |
| 68599 | | - assert(u.ch.pVtab && u.ch.pModule); |
| 68600 | | - rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor); |
| 68601 | | - importVtabErrMsg(p, u.ch.pVtab); |
| 69290 | +#endif /* local variables moved into u.ck */ |
| 69291 | + |
| 69292 | + u.ck.pCur = 0; |
| 69293 | + u.ck.pVtabCursor = 0; |
| 69294 | + u.ck.pVtab = pOp->p4.pVtab->pVtab; |
| 69295 | + u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule; |
| 69296 | + assert(u.ck.pVtab && u.ck.pModule); |
| 69297 | + rc = u.ck.pModule->xOpen(u.ck.pVtab, &u.ck.pVtabCursor); |
| 69298 | + importVtabErrMsg(p, u.ck.pVtab); |
| 68602 | 69299 | if( SQLITE_OK==rc ){ |
| 68603 | 69300 | /* Initialize sqlite3_vtab_cursor base class */ |
| 68604 | | - u.ch.pVtabCursor->pVtab = u.ch.pVtab; |
| 69301 | + u.ck.pVtabCursor->pVtab = u.ck.pVtab; |
| 68605 | 69302 | |
| 68606 | 69303 | /* Initialise vdbe cursor object */ |
| 68607 | | - u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0); |
| 68608 | | - if( u.ch.pCur ){ |
| 68609 | | - u.ch.pCur->pVtabCursor = u.ch.pVtabCursor; |
| 68610 | | - u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule; |
| 69304 | + u.ck.pCur = allocateCursor(p, pOp->p1, 0, -1, 0); |
| 69305 | + if( u.ck.pCur ){ |
| 69306 | + u.ck.pCur->pVtabCursor = u.ck.pVtabCursor; |
| 69307 | + u.ck.pCur->pModule = u.ck.pVtabCursor->pVtab->pModule; |
| 68611 | 69308 | }else{ |
| 68612 | 69309 | db->mallocFailed = 1; |
| 68613 | | - u.ch.pModule->xClose(u.ch.pVtabCursor); |
| 69310 | + u.ck.pModule->xClose(u.ck.pVtabCursor); |
| 68614 | 69311 | } |
| 68615 | 69312 | } |
| 68616 | 69313 | break; |
| 68617 | 69314 | } |
| 68618 | 69315 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| | @@ -68635,11 +69332,11 @@ |
| 68635 | 69332 | ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
| 68636 | 69333 | ** |
| 68637 | 69334 | ** A jump is made to P2 if the result set after filtering would be empty. |
| 68638 | 69335 | */ |
| 68639 | 69336 | case OP_VFilter: { /* jump */ |
| 68640 | | -#if 0 /* local variables moved into u.ci */ |
| 69337 | +#if 0 /* local variables moved into u.cl */ |
| 68641 | 69338 | int nArg; |
| 68642 | 69339 | int iQuery; |
| 68643 | 69340 | const sqlite3_module *pModule; |
| 68644 | 69341 | Mem *pQuery; |
| 68645 | 69342 | Mem *pArgc; |
| | @@ -68647,49 +69344,49 @@ |
| 68647 | 69344 | sqlite3_vtab *pVtab; |
| 68648 | 69345 | VdbeCursor *pCur; |
| 68649 | 69346 | int res; |
| 68650 | 69347 | int i; |
| 68651 | 69348 | Mem **apArg; |
| 68652 | | -#endif /* local variables moved into u.ci */ |
| 68653 | | - |
| 68654 | | - u.ci.pQuery = &aMem[pOp->p3]; |
| 68655 | | - u.ci.pArgc = &u.ci.pQuery[1]; |
| 68656 | | - u.ci.pCur = p->apCsr[pOp->p1]; |
| 68657 | | - assert( memIsValid(u.ci.pQuery) ); |
| 68658 | | - REGISTER_TRACE(pOp->p3, u.ci.pQuery); |
| 68659 | | - assert( u.ci.pCur->pVtabCursor ); |
| 68660 | | - u.ci.pVtabCursor = u.ci.pCur->pVtabCursor; |
| 68661 | | - u.ci.pVtab = u.ci.pVtabCursor->pVtab; |
| 68662 | | - u.ci.pModule = u.ci.pVtab->pModule; |
| 69349 | +#endif /* local variables moved into u.cl */ |
| 69350 | + |
| 69351 | + u.cl.pQuery = &aMem[pOp->p3]; |
| 69352 | + u.cl.pArgc = &u.cl.pQuery[1]; |
| 69353 | + u.cl.pCur = p->apCsr[pOp->p1]; |
| 69354 | + assert( memIsValid(u.cl.pQuery) ); |
| 69355 | + REGISTER_TRACE(pOp->p3, u.cl.pQuery); |
| 69356 | + assert( u.cl.pCur->pVtabCursor ); |
| 69357 | + u.cl.pVtabCursor = u.cl.pCur->pVtabCursor; |
| 69358 | + u.cl.pVtab = u.cl.pVtabCursor->pVtab; |
| 69359 | + u.cl.pModule = u.cl.pVtab->pModule; |
| 68663 | 69360 | |
| 68664 | 69361 | /* Grab the index number and argc parameters */ |
| 68665 | | - assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int ); |
| 68666 | | - u.ci.nArg = (int)u.ci.pArgc->u.i; |
| 68667 | | - u.ci.iQuery = (int)u.ci.pQuery->u.i; |
| 69362 | + assert( (u.cl.pQuery->flags&MEM_Int)!=0 && u.cl.pArgc->flags==MEM_Int ); |
| 69363 | + u.cl.nArg = (int)u.cl.pArgc->u.i; |
| 69364 | + u.cl.iQuery = (int)u.cl.pQuery->u.i; |
| 68668 | 69365 | |
| 68669 | 69366 | /* Invoke the xFilter method */ |
| 68670 | 69367 | { |
| 68671 | | - u.ci.res = 0; |
| 68672 | | - u.ci.apArg = p->apArg; |
| 68673 | | - for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){ |
| 68674 | | - u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1]; |
| 68675 | | - sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]); |
| 69368 | + u.cl.res = 0; |
| 69369 | + u.cl.apArg = p->apArg; |
| 69370 | + for(u.cl.i = 0; u.cl.i<u.cl.nArg; u.cl.i++){ |
| 69371 | + u.cl.apArg[u.cl.i] = &u.cl.pArgc[u.cl.i+1]; |
| 69372 | + sqlite3VdbeMemStoreType(u.cl.apArg[u.cl.i]); |
| 68676 | 69373 | } |
| 68677 | 69374 | |
| 68678 | 69375 | p->inVtabMethod = 1; |
| 68679 | | - rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg); |
| 69376 | + rc = u.cl.pModule->xFilter(u.cl.pVtabCursor, u.cl.iQuery, pOp->p4.z, u.cl.nArg, u.cl.apArg); |
| 68680 | 69377 | p->inVtabMethod = 0; |
| 68681 | | - importVtabErrMsg(p, u.ci.pVtab); |
| 69378 | + importVtabErrMsg(p, u.cl.pVtab); |
| 68682 | 69379 | if( rc==SQLITE_OK ){ |
| 68683 | | - u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor); |
| 69380 | + u.cl.res = u.cl.pModule->xEof(u.cl.pVtabCursor); |
| 68684 | 69381 | } |
| 68685 | 69382 | |
| 68686 | | - if( u.ci.res ){ |
| 69383 | + if( u.cl.res ){ |
| 68687 | 69384 | pc = pOp->p2 - 1; |
| 68688 | 69385 | } |
| 68689 | 69386 | } |
| 68690 | | - u.ci.pCur->nullRow = 0; |
| 69387 | + u.cl.pCur->nullRow = 0; |
| 68691 | 69388 | |
| 68692 | 69389 | break; |
| 68693 | 69390 | } |
| 68694 | 69391 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 68695 | 69392 | |
| | @@ -68699,55 +69396,55 @@ |
| 68699 | 69396 | ** Store the value of the P2-th column of |
| 68700 | 69397 | ** the row of the virtual-table that the |
| 68701 | 69398 | ** P1 cursor is pointing to into register P3. |
| 68702 | 69399 | */ |
| 68703 | 69400 | case OP_VColumn: { |
| 68704 | | -#if 0 /* local variables moved into u.cj */ |
| 69401 | +#if 0 /* local variables moved into u.cm */ |
| 68705 | 69402 | sqlite3_vtab *pVtab; |
| 68706 | 69403 | const sqlite3_module *pModule; |
| 68707 | 69404 | Mem *pDest; |
| 68708 | 69405 | sqlite3_context sContext; |
| 68709 | | -#endif /* local variables moved into u.cj */ |
| 69406 | +#endif /* local variables moved into u.cm */ |
| 68710 | 69407 | |
| 68711 | 69408 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
| 68712 | 69409 | assert( pCur->pVtabCursor ); |
| 68713 | 69410 | assert( pOp->p3>0 && pOp->p3<=p->nMem ); |
| 68714 | | - u.cj.pDest = &aMem[pOp->p3]; |
| 68715 | | - memAboutToChange(p, u.cj.pDest); |
| 69411 | + u.cm.pDest = &aMem[pOp->p3]; |
| 69412 | + memAboutToChange(p, u.cm.pDest); |
| 68716 | 69413 | if( pCur->nullRow ){ |
| 68717 | | - sqlite3VdbeMemSetNull(u.cj.pDest); |
| 69414 | + sqlite3VdbeMemSetNull(u.cm.pDest); |
| 68718 | 69415 | break; |
| 68719 | 69416 | } |
| 68720 | | - u.cj.pVtab = pCur->pVtabCursor->pVtab; |
| 68721 | | - u.cj.pModule = u.cj.pVtab->pModule; |
| 68722 | | - assert( u.cj.pModule->xColumn ); |
| 68723 | | - memset(&u.cj.sContext, 0, sizeof(u.cj.sContext)); |
| 69417 | + u.cm.pVtab = pCur->pVtabCursor->pVtab; |
| 69418 | + u.cm.pModule = u.cm.pVtab->pModule; |
| 69419 | + assert( u.cm.pModule->xColumn ); |
| 69420 | + memset(&u.cm.sContext, 0, sizeof(u.cm.sContext)); |
| 68724 | 69421 | |
| 68725 | 69422 | /* The output cell may already have a buffer allocated. Move |
| 68726 | | - ** the current contents to u.cj.sContext.s so in case the user-function |
| 69423 | + ** the current contents to u.cm.sContext.s so in case the user-function |
| 68727 | 69424 | ** can use the already allocated buffer instead of allocating a |
| 68728 | 69425 | ** new one. |
| 68729 | 69426 | */ |
| 68730 | | - sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest); |
| 68731 | | - MemSetTypeFlag(&u.cj.sContext.s, MEM_Null); |
| 69427 | + sqlite3VdbeMemMove(&u.cm.sContext.s, u.cm.pDest); |
| 69428 | + MemSetTypeFlag(&u.cm.sContext.s, MEM_Null); |
| 68732 | 69429 | |
| 68733 | | - rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2); |
| 68734 | | - importVtabErrMsg(p, u.cj.pVtab); |
| 68735 | | - if( u.cj.sContext.isError ){ |
| 68736 | | - rc = u.cj.sContext.isError; |
| 69430 | + rc = u.cm.pModule->xColumn(pCur->pVtabCursor, &u.cm.sContext, pOp->p2); |
| 69431 | + importVtabErrMsg(p, u.cm.pVtab); |
| 69432 | + if( u.cm.sContext.isError ){ |
| 69433 | + rc = u.cm.sContext.isError; |
| 68737 | 69434 | } |
| 68738 | 69435 | |
| 68739 | 69436 | /* Copy the result of the function to the P3 register. We |
| 68740 | 69437 | ** do this regardless of whether or not an error occurred to ensure any |
| 68741 | | - ** dynamic allocation in u.cj.sContext.s (a Mem struct) is released. |
| 69438 | + ** dynamic allocation in u.cm.sContext.s (a Mem struct) is released. |
| 68742 | 69439 | */ |
| 68743 | | - sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding); |
| 68744 | | - sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s); |
| 68745 | | - REGISTER_TRACE(pOp->p3, u.cj.pDest); |
| 68746 | | - UPDATE_MAX_BLOBSIZE(u.cj.pDest); |
| 69440 | + sqlite3VdbeChangeEncoding(&u.cm.sContext.s, encoding); |
| 69441 | + sqlite3VdbeMemMove(u.cm.pDest, &u.cm.sContext.s); |
| 69442 | + REGISTER_TRACE(pOp->p3, u.cm.pDest); |
| 69443 | + UPDATE_MAX_BLOBSIZE(u.cm.pDest); |
| 68747 | 69444 | |
| 68748 | | - if( sqlite3VdbeMemTooBig(u.cj.pDest) ){ |
| 69445 | + if( sqlite3VdbeMemTooBig(u.cm.pDest) ){ |
| 68749 | 69446 | goto too_big; |
| 68750 | 69447 | } |
| 68751 | 69448 | break; |
| 68752 | 69449 | } |
| 68753 | 69450 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| | @@ -68758,42 +69455,42 @@ |
| 68758 | 69455 | ** Advance virtual table P1 to the next row in its result set and |
| 68759 | 69456 | ** jump to instruction P2. Or, if the virtual table has reached |
| 68760 | 69457 | ** the end of its result set, then fall through to the next instruction. |
| 68761 | 69458 | */ |
| 68762 | 69459 | case OP_VNext: { /* jump */ |
| 68763 | | -#if 0 /* local variables moved into u.ck */ |
| 69460 | +#if 0 /* local variables moved into u.cn */ |
| 68764 | 69461 | sqlite3_vtab *pVtab; |
| 68765 | 69462 | const sqlite3_module *pModule; |
| 68766 | 69463 | int res; |
| 68767 | 69464 | VdbeCursor *pCur; |
| 68768 | | -#endif /* local variables moved into u.ck */ |
| 69465 | +#endif /* local variables moved into u.cn */ |
| 68769 | 69466 | |
| 68770 | | - u.ck.res = 0; |
| 68771 | | - u.ck.pCur = p->apCsr[pOp->p1]; |
| 68772 | | - assert( u.ck.pCur->pVtabCursor ); |
| 68773 | | - if( u.ck.pCur->nullRow ){ |
| 69467 | + u.cn.res = 0; |
| 69468 | + u.cn.pCur = p->apCsr[pOp->p1]; |
| 69469 | + assert( u.cn.pCur->pVtabCursor ); |
| 69470 | + if( u.cn.pCur->nullRow ){ |
| 68774 | 69471 | break; |
| 68775 | 69472 | } |
| 68776 | | - u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab; |
| 68777 | | - u.ck.pModule = u.ck.pVtab->pModule; |
| 68778 | | - assert( u.ck.pModule->xNext ); |
| 69473 | + u.cn.pVtab = u.cn.pCur->pVtabCursor->pVtab; |
| 69474 | + u.cn.pModule = u.cn.pVtab->pModule; |
| 69475 | + assert( u.cn.pModule->xNext ); |
| 68779 | 69476 | |
| 68780 | 69477 | /* Invoke the xNext() method of the module. There is no way for the |
| 68781 | 69478 | ** underlying implementation to return an error if one occurs during |
| 68782 | 69479 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 68783 | 69480 | ** data is available) and the error code returned when xColumn or |
| 68784 | 69481 | ** some other method is next invoked on the save virtual table cursor. |
| 68785 | 69482 | */ |
| 68786 | 69483 | p->inVtabMethod = 1; |
| 68787 | | - rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor); |
| 69484 | + rc = u.cn.pModule->xNext(u.cn.pCur->pVtabCursor); |
| 68788 | 69485 | p->inVtabMethod = 0; |
| 68789 | | - importVtabErrMsg(p, u.ck.pVtab); |
| 69486 | + importVtabErrMsg(p, u.cn.pVtab); |
| 68790 | 69487 | if( rc==SQLITE_OK ){ |
| 68791 | | - u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor); |
| 69488 | + u.cn.res = u.cn.pModule->xEof(u.cn.pCur->pVtabCursor); |
| 68792 | 69489 | } |
| 68793 | 69490 | |
| 68794 | | - if( !u.ck.res ){ |
| 69491 | + if( !u.cn.res ){ |
| 68795 | 69492 | /* If there is data, jump to P2 */ |
| 68796 | 69493 | pc = pOp->p2 - 1; |
| 68797 | 69494 | } |
| 68798 | 69495 | break; |
| 68799 | 69496 | } |
| | @@ -68805,23 +69502,23 @@ |
| 68805 | 69502 | ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
| 68806 | 69503 | ** This opcode invokes the corresponding xRename method. The value |
| 68807 | 69504 | ** in register P1 is passed as the zName argument to the xRename method. |
| 68808 | 69505 | */ |
| 68809 | 69506 | case OP_VRename: { |
| 68810 | | -#if 0 /* local variables moved into u.cl */ |
| 69507 | +#if 0 /* local variables moved into u.co */ |
| 68811 | 69508 | sqlite3_vtab *pVtab; |
| 68812 | 69509 | Mem *pName; |
| 68813 | | -#endif /* local variables moved into u.cl */ |
| 68814 | | - |
| 68815 | | - u.cl.pVtab = pOp->p4.pVtab->pVtab; |
| 68816 | | - u.cl.pName = &aMem[pOp->p1]; |
| 68817 | | - assert( u.cl.pVtab->pModule->xRename ); |
| 68818 | | - assert( memIsValid(u.cl.pName) ); |
| 68819 | | - REGISTER_TRACE(pOp->p1, u.cl.pName); |
| 68820 | | - assert( u.cl.pName->flags & MEM_Str ); |
| 68821 | | - rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z); |
| 68822 | | - importVtabErrMsg(p, u.cl.pVtab); |
| 69510 | +#endif /* local variables moved into u.co */ |
| 69511 | + |
| 69512 | + u.co.pVtab = pOp->p4.pVtab->pVtab; |
| 69513 | + u.co.pName = &aMem[pOp->p1]; |
| 69514 | + assert( u.co.pVtab->pModule->xRename ); |
| 69515 | + assert( memIsValid(u.co.pName) ); |
| 69516 | + REGISTER_TRACE(pOp->p1, u.co.pName); |
| 69517 | + assert( u.co.pName->flags & MEM_Str ); |
| 69518 | + rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z); |
| 69519 | + importVtabErrMsg(p, u.co.pVtab); |
| 68823 | 69520 | p->expired = 0; |
| 68824 | 69521 | |
| 68825 | 69522 | break; |
| 68826 | 69523 | } |
| 68827 | 69524 | #endif |
| | @@ -68849,45 +69546,45 @@ |
| 68849 | 69546 | ** P1 is a boolean flag. If it is set to true and the xUpdate call |
| 68850 | 69547 | ** is successful, then the value returned by sqlite3_last_insert_rowid() |
| 68851 | 69548 | ** is set to the value of the rowid for the row just inserted. |
| 68852 | 69549 | */ |
| 68853 | 69550 | case OP_VUpdate: { |
| 68854 | | -#if 0 /* local variables moved into u.cm */ |
| 69551 | +#if 0 /* local variables moved into u.cp */ |
| 68855 | 69552 | sqlite3_vtab *pVtab; |
| 68856 | 69553 | sqlite3_module *pModule; |
| 68857 | 69554 | int nArg; |
| 68858 | 69555 | int i; |
| 68859 | 69556 | sqlite_int64 rowid; |
| 68860 | 69557 | Mem **apArg; |
| 68861 | 69558 | Mem *pX; |
| 68862 | | -#endif /* local variables moved into u.cm */ |
| 69559 | +#endif /* local variables moved into u.cp */ |
| 68863 | 69560 | |
| 68864 | 69561 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 68865 | 69562 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 68866 | 69563 | ); |
| 68867 | | - u.cm.pVtab = pOp->p4.pVtab->pVtab; |
| 68868 | | - u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule; |
| 68869 | | - u.cm.nArg = pOp->p2; |
| 69564 | + u.cp.pVtab = pOp->p4.pVtab->pVtab; |
| 69565 | + u.cp.pModule = (sqlite3_module *)u.cp.pVtab->pModule; |
| 69566 | + u.cp.nArg = pOp->p2; |
| 68870 | 69567 | assert( pOp->p4type==P4_VTAB ); |
| 68871 | | - if( ALWAYS(u.cm.pModule->xUpdate) ){ |
| 69568 | + if( ALWAYS(u.cp.pModule->xUpdate) ){ |
| 68872 | 69569 | u8 vtabOnConflict = db->vtabOnConflict; |
| 68873 | | - u.cm.apArg = p->apArg; |
| 68874 | | - u.cm.pX = &aMem[pOp->p3]; |
| 68875 | | - for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){ |
| 68876 | | - assert( memIsValid(u.cm.pX) ); |
| 68877 | | - memAboutToChange(p, u.cm.pX); |
| 68878 | | - sqlite3VdbeMemStoreType(u.cm.pX); |
| 68879 | | - u.cm.apArg[u.cm.i] = u.cm.pX; |
| 68880 | | - u.cm.pX++; |
| 69570 | + u.cp.apArg = p->apArg; |
| 69571 | + u.cp.pX = &aMem[pOp->p3]; |
| 69572 | + for(u.cp.i=0; u.cp.i<u.cp.nArg; u.cp.i++){ |
| 69573 | + assert( memIsValid(u.cp.pX) ); |
| 69574 | + memAboutToChange(p, u.cp.pX); |
| 69575 | + sqlite3VdbeMemStoreType(u.cp.pX); |
| 69576 | + u.cp.apArg[u.cp.i] = u.cp.pX; |
| 69577 | + u.cp.pX++; |
| 68881 | 69578 | } |
| 68882 | 69579 | db->vtabOnConflict = pOp->p5; |
| 68883 | | - rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid); |
| 69580 | + rc = u.cp.pModule->xUpdate(u.cp.pVtab, u.cp.nArg, u.cp.apArg, &u.cp.rowid); |
| 68884 | 69581 | db->vtabOnConflict = vtabOnConflict; |
| 68885 | | - importVtabErrMsg(p, u.cm.pVtab); |
| 69582 | + importVtabErrMsg(p, u.cp.pVtab); |
| 68886 | 69583 | if( rc==SQLITE_OK && pOp->p1 ){ |
| 68887 | | - assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) ); |
| 68888 | | - db->lastRowid = lastRowid = u.cm.rowid; |
| 69584 | + assert( u.cp.nArg>1 && u.cp.apArg[0] && (u.cp.apArg[0]->flags&MEM_Null) ); |
| 69585 | + db->lastRowid = lastRowid = u.cp.rowid; |
| 68889 | 69586 | } |
| 68890 | 69587 | if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
| 68891 | 69588 | if( pOp->p5==OE_Ignore ){ |
| 68892 | 69589 | rc = SQLITE_OK; |
| 68893 | 69590 | }else{ |
| | @@ -68943,25 +69640,25 @@ |
| 68943 | 69640 | ** |
| 68944 | 69641 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 68945 | 69642 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
| 68946 | 69643 | */ |
| 68947 | 69644 | case OP_Trace: { |
| 68948 | | -#if 0 /* local variables moved into u.cn */ |
| 69645 | +#if 0 /* local variables moved into u.cq */ |
| 68949 | 69646 | char *zTrace; |
| 68950 | 69647 | char *z; |
| 68951 | | -#endif /* local variables moved into u.cn */ |
| 69648 | +#endif /* local variables moved into u.cq */ |
| 68952 | 69649 | |
| 68953 | | - if( db->xTrace && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){ |
| 68954 | | - u.cn.z = sqlite3VdbeExpandSql(p, u.cn.zTrace); |
| 68955 | | - db->xTrace(db->pTraceArg, u.cn.z); |
| 68956 | | - sqlite3DbFree(db, u.cn.z); |
| 69650 | + if( db->xTrace && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){ |
| 69651 | + u.cq.z = sqlite3VdbeExpandSql(p, u.cq.zTrace); |
| 69652 | + db->xTrace(db->pTraceArg, u.cq.z); |
| 69653 | + sqlite3DbFree(db, u.cq.z); |
| 68957 | 69654 | } |
| 68958 | 69655 | #ifdef SQLITE_DEBUG |
| 68959 | 69656 | if( (db->flags & SQLITE_SqlTrace)!=0 |
| 68960 | | - && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 69657 | + && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 68961 | 69658 | ){ |
| 68962 | | - sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace); |
| 69659 | + sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace); |
| 68963 | 69660 | } |
| 68964 | 69661 | #endif /* SQLITE_DEBUG */ |
| 68965 | 69662 | break; |
| 68966 | 69663 | } |
| 68967 | 69664 | #endif |
| | @@ -69556,10 +70253,930 @@ |
| 69556 | 70253 | } |
| 69557 | 70254 | |
| 69558 | 70255 | #endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |
| 69559 | 70256 | |
| 69560 | 70257 | /************** End of vdbeblob.c ********************************************/ |
| 70258 | +/************** Begin file vdbesort.c ****************************************/ |
| 70259 | +/* |
| 70260 | +** 2011 July 9 |
| 70261 | +** |
| 70262 | +** The author disclaims copyright to this source code. In place of |
| 70263 | +** a legal notice, here is a blessing: |
| 70264 | +** |
| 70265 | +** May you do good and not evil. |
| 70266 | +** May you find forgiveness for yourself and forgive others. |
| 70267 | +** May you share freely, never taking more than you give. |
| 70268 | +** |
| 70269 | +************************************************************************* |
| 70270 | +** This file contains code for the VdbeSorter object, used in concert with |
| 70271 | +** a VdbeCursor to sort large numbers of keys (as may be required, for |
| 70272 | +** example, by CREATE INDEX statements on tables too large to fit in main |
| 70273 | +** memory). |
| 70274 | +*/ |
| 70275 | + |
| 70276 | + |
| 70277 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 70278 | + |
| 70279 | +typedef struct VdbeSorterIter VdbeSorterIter; |
| 70280 | +typedef struct SorterRecord SorterRecord; |
| 70281 | + |
| 70282 | +/* |
| 70283 | +** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES: |
| 70284 | +** |
| 70285 | +** As keys are added to the sorter, they are written to disk in a series |
| 70286 | +** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly |
| 70287 | +** the same as the cache-size allowed for temporary databases. In order |
| 70288 | +** to allow the caller to extract keys from the sorter in sorted order, |
| 70289 | +** all PMAs currently stored on disk must be merged together. This comment |
| 70290 | +** describes the data structure used to do so. The structure supports |
| 70291 | +** merging any number of arrays in a single pass with no redundant comparison |
| 70292 | +** operations. |
| 70293 | +** |
| 70294 | +** The aIter[] array contains an iterator for each of the PMAs being merged. |
| 70295 | +** An aIter[] iterator either points to a valid key or else is at EOF. For |
| 70296 | +** the purposes of the paragraphs below, we assume that the array is actually |
| 70297 | +** N elements in size, where N is the smallest power of 2 greater to or equal |
| 70298 | +** to the number of iterators being merged. The extra aIter[] elements are |
| 70299 | +** treated as if they are empty (always at EOF). |
| 70300 | +** |
| 70301 | +** The aTree[] array is also N elements in size. The value of N is stored in |
| 70302 | +** the VdbeSorter.nTree variable. |
| 70303 | +** |
| 70304 | +** The final (N/2) elements of aTree[] contain the results of comparing |
| 70305 | +** pairs of iterator keys together. Element i contains the result of |
| 70306 | +** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the |
| 70307 | +** aTree element is set to the index of it. |
| 70308 | +** |
| 70309 | +** For the purposes of this comparison, EOF is considered greater than any |
| 70310 | +** other key value. If the keys are equal (only possible with two EOF |
| 70311 | +** values), it doesn't matter which index is stored. |
| 70312 | +** |
| 70313 | +** The (N/4) elements of aTree[] that preceed the final (N/2) described |
| 70314 | +** above contains the index of the smallest of each block of 4 iterators. |
| 70315 | +** And so on. So that aTree[1] contains the index of the iterator that |
| 70316 | +** currently points to the smallest key value. aTree[0] is unused. |
| 70317 | +** |
| 70318 | +** Example: |
| 70319 | +** |
| 70320 | +** aIter[0] -> Banana |
| 70321 | +** aIter[1] -> Feijoa |
| 70322 | +** aIter[2] -> Elderberry |
| 70323 | +** aIter[3] -> Currant |
| 70324 | +** aIter[4] -> Grapefruit |
| 70325 | +** aIter[5] -> Apple |
| 70326 | +** aIter[6] -> Durian |
| 70327 | +** aIter[7] -> EOF |
| 70328 | +** |
| 70329 | +** aTree[] = { X, 5 0, 5 0, 3, 5, 6 } |
| 70330 | +** |
| 70331 | +** The current element is "Apple" (the value of the key indicated by |
| 70332 | +** iterator 5). When the Next() operation is invoked, iterator 5 will |
| 70333 | +** be advanced to the next key in its segment. Say the next key is |
| 70334 | +** "Eggplant": |
| 70335 | +** |
| 70336 | +** aIter[5] -> Eggplant |
| 70337 | +** |
| 70338 | +** The contents of aTree[] are updated first by comparing the new iterator |
| 70339 | +** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator |
| 70340 | +** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree. |
| 70341 | +** The value of iterator 6 - "Durian" - is now smaller than that of iterator |
| 70342 | +** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian), |
| 70343 | +** so the value written into element 1 of the array is 0. As follows: |
| 70344 | +** |
| 70345 | +** aTree[] = { X, 0 0, 6 0, 3, 5, 6 } |
| 70346 | +** |
| 70347 | +** In other words, each time we advance to the next sorter element, log2(N) |
| 70348 | +** key comparison operations are required, where N is the number of segments |
| 70349 | +** being merged (rounded up to the next power of 2). |
| 70350 | +*/ |
| 70351 | +struct VdbeSorter { |
| 70352 | + int nInMemory; /* Current size of pRecord list as PMA */ |
| 70353 | + int nTree; /* Used size of aTree/aIter (power of 2) */ |
| 70354 | + VdbeSorterIter *aIter; /* Array of iterators to merge */ |
| 70355 | + int *aTree; /* Current state of incremental merge */ |
| 70356 | + i64 iWriteOff; /* Current write offset within file pTemp1 */ |
| 70357 | + i64 iReadOff; /* Current read offset within file pTemp1 */ |
| 70358 | + sqlite3_file *pTemp1; /* PMA file 1 */ |
| 70359 | + int nPMA; /* Number of PMAs stored in pTemp1 */ |
| 70360 | + SorterRecord *pRecord; /* Head of in-memory record list */ |
| 70361 | + int mnPmaSize; /* Minimum PMA size, in bytes */ |
| 70362 | + int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */ |
| 70363 | + char *aSpace; /* Space for UnpackRecord() */ |
| 70364 | + int nSpace; /* Size of aSpace in bytes */ |
| 70365 | +}; |
| 70366 | + |
| 70367 | +/* |
| 70368 | +** The following type is an iterator for a PMA. It caches the current key in |
| 70369 | +** variables nKey/aKey. If the iterator is at EOF, pFile==0. |
| 70370 | +*/ |
| 70371 | +struct VdbeSorterIter { |
| 70372 | + i64 iReadOff; /* Current read offset */ |
| 70373 | + i64 iEof; /* 1 byte past EOF for this iterator */ |
| 70374 | + sqlite3_file *pFile; /* File iterator is reading from */ |
| 70375 | + int nAlloc; /* Bytes of space at aAlloc */ |
| 70376 | + u8 *aAlloc; /* Allocated space */ |
| 70377 | + int nKey; /* Number of bytes in key */ |
| 70378 | + u8 *aKey; /* Pointer to current key */ |
| 70379 | +}; |
| 70380 | + |
| 70381 | +/* |
| 70382 | +** A structure to store a single record. All in-memory records are connected |
| 70383 | +** together into a linked list headed at VdbeSorter.pRecord using the |
| 70384 | +** SorterRecord.pNext pointer. |
| 70385 | +*/ |
| 70386 | +struct SorterRecord { |
| 70387 | + void *pVal; |
| 70388 | + int nVal; |
| 70389 | + SorterRecord *pNext; |
| 70390 | +}; |
| 70391 | + |
| 70392 | +/* Minimum allowable value for the VdbeSorter.nWorking variable */ |
| 70393 | +#define SORTER_MIN_WORKING 10 |
| 70394 | + |
| 70395 | +/* Maximum number of segments to merge in a single pass. */ |
| 70396 | +#define SORTER_MAX_MERGE_COUNT 16 |
| 70397 | + |
| 70398 | +/* |
| 70399 | +** Free all memory belonging to the VdbeSorterIter object passed as the second |
| 70400 | +** argument. All structure fields are set to zero before returning. |
| 70401 | +*/ |
| 70402 | +static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){ |
| 70403 | + sqlite3DbFree(db, pIter->aAlloc); |
| 70404 | + memset(pIter, 0, sizeof(VdbeSorterIter)); |
| 70405 | +} |
| 70406 | + |
| 70407 | +/* |
| 70408 | +** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if |
| 70409 | +** no error occurs, or an SQLite error code if one does. |
| 70410 | +*/ |
| 70411 | +static int vdbeSorterIterNext( |
| 70412 | + sqlite3 *db, /* Database handle (for sqlite3DbMalloc() ) */ |
| 70413 | + VdbeSorterIter *pIter /* Iterator to advance */ |
| 70414 | +){ |
| 70415 | + int rc; /* Return Code */ |
| 70416 | + int nRead; /* Number of bytes read */ |
| 70417 | + int nRec = 0; /* Size of record in bytes */ |
| 70418 | + int iOff = 0; /* Size of serialized size varint in bytes */ |
| 70419 | + |
| 70420 | + nRead = pIter->iEof - pIter->iReadOff; |
| 70421 | + if( nRead>5 ) nRead = 5; |
| 70422 | + if( nRead<=0 ){ |
| 70423 | + /* This is an EOF condition */ |
| 70424 | + vdbeSorterIterZero(db, pIter); |
| 70425 | + return SQLITE_OK; |
| 70426 | + } |
| 70427 | + |
| 70428 | + rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff); |
| 70429 | + if( rc==SQLITE_OK ){ |
| 70430 | + iOff = getVarint32(pIter->aAlloc, nRec); |
| 70431 | + if( (iOff+nRec)>nRead ){ |
| 70432 | + int nRead2; /* Number of extra bytes to read */ |
| 70433 | + if( (iOff+nRec)>pIter->nAlloc ){ |
| 70434 | + int nNew = pIter->nAlloc*2; |
| 70435 | + while( (iOff+nRec)>nNew ) nNew = nNew*2; |
| 70436 | + pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew); |
| 70437 | + if( !pIter->aAlloc ) return SQLITE_NOMEM; |
| 70438 | + pIter->nAlloc = nNew; |
| 70439 | + } |
| 70440 | + |
| 70441 | + nRead2 = iOff + nRec - nRead; |
| 70442 | + rc = sqlite3OsRead( |
| 70443 | + pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead |
| 70444 | + ); |
| 70445 | + } |
| 70446 | + } |
| 70447 | + |
| 70448 | + assert( rc!=SQLITE_OK || nRec>0 ); |
| 70449 | + pIter->iReadOff += iOff+nRec; |
| 70450 | + pIter->nKey = nRec; |
| 70451 | + pIter->aKey = &pIter->aAlloc[iOff]; |
| 70452 | + return rc; |
| 70453 | +} |
| 70454 | + |
| 70455 | +/* |
| 70456 | +** Write a single varint, value iVal, to file-descriptor pFile. Return |
| 70457 | +** SQLITE_OK if successful, or an SQLite error code if some error occurs. |
| 70458 | +** |
| 70459 | +** The value of *piOffset when this function is called is used as the byte |
| 70460 | +** offset in file pFile to write to. Before returning, *piOffset is |
| 70461 | +** incremented by the number of bytes written. |
| 70462 | +*/ |
| 70463 | +static int vdbeSorterWriteVarint( |
| 70464 | + sqlite3_file *pFile, /* File to write to */ |
| 70465 | + i64 iVal, /* Value to write as a varint */ |
| 70466 | + i64 *piOffset /* IN/OUT: Write offset in file pFile */ |
| 70467 | +){ |
| 70468 | + u8 aVarint[9]; /* Buffer large enough for a varint */ |
| 70469 | + int nVarint; /* Number of used bytes in varint */ |
| 70470 | + int rc; /* Result of write() call */ |
| 70471 | + |
| 70472 | + nVarint = sqlite3PutVarint(aVarint, iVal); |
| 70473 | + rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset); |
| 70474 | + *piOffset += nVarint; |
| 70475 | + |
| 70476 | + return rc; |
| 70477 | +} |
| 70478 | + |
| 70479 | +/* |
| 70480 | +** Read a single varint from file-descriptor pFile. Return SQLITE_OK if |
| 70481 | +** successful, or an SQLite error code if some error occurs. |
| 70482 | +** |
| 70483 | +** The value of *piOffset when this function is called is used as the |
| 70484 | +** byte offset in file pFile from whence to read the varint. If successful |
| 70485 | +** (i.e. if no IO error occurs), then *piOffset is set to the offset of |
| 70486 | +** the first byte past the end of the varint before returning. *piVal is |
| 70487 | +** set to the integer value read. If an error occurs, the final values of |
| 70488 | +** both *piOffset and *piVal are undefined. |
| 70489 | +*/ |
| 70490 | +static int vdbeSorterReadVarint( |
| 70491 | + sqlite3_file *pFile, /* File to read from */ |
| 70492 | + i64 *piOffset, /* IN/OUT: Read offset in pFile */ |
| 70493 | + i64 *piVal /* OUT: Value read from file */ |
| 70494 | +){ |
| 70495 | + u8 aVarint[9]; /* Buffer large enough for a varint */ |
| 70496 | + i64 iOff = *piOffset; /* Offset in file to read from */ |
| 70497 | + int rc; /* Return code */ |
| 70498 | + |
| 70499 | + rc = sqlite3OsRead(pFile, aVarint, 9, iOff); |
| 70500 | + if( rc==SQLITE_OK ){ |
| 70501 | + *piOffset += getVarint(aVarint, (u64 *)piVal); |
| 70502 | + } |
| 70503 | + |
| 70504 | + return rc; |
| 70505 | +} |
| 70506 | + |
| 70507 | +/* |
| 70508 | +** Initialize iterator pIter to scan through the PMA stored in file pFile |
| 70509 | +** starting at offset iStart and ending at offset iEof-1. This function |
| 70510 | +** leaves the iterator pointing to the first key in the PMA (or EOF if the |
| 70511 | +** PMA is empty). |
| 70512 | +*/ |
| 70513 | +static int vdbeSorterIterInit( |
| 70514 | + sqlite3 *db, /* Database handle */ |
| 70515 | + VdbeSorter *pSorter, /* Sorter object */ |
| 70516 | + i64 iStart, /* Start offset in pFile */ |
| 70517 | + VdbeSorterIter *pIter, /* Iterator to populate */ |
| 70518 | + i64 *pnByte /* IN/OUT: Increment this value by PMA size */ |
| 70519 | +){ |
| 70520 | + int rc; |
| 70521 | + |
| 70522 | + assert( pSorter->iWriteOff>iStart ); |
| 70523 | + assert( pIter->aAlloc==0 ); |
| 70524 | + pIter->pFile = pSorter->pTemp1; |
| 70525 | + pIter->iReadOff = iStart; |
| 70526 | + pIter->nAlloc = 128; |
| 70527 | + pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc); |
| 70528 | + if( !pIter->aAlloc ){ |
| 70529 | + rc = SQLITE_NOMEM; |
| 70530 | + }else{ |
| 70531 | + i64 nByte; /* Total size of PMA in bytes */ |
| 70532 | + rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte); |
| 70533 | + *pnByte += nByte; |
| 70534 | + pIter->iEof = pIter->iReadOff + nByte; |
| 70535 | + } |
| 70536 | + if( rc==SQLITE_OK ){ |
| 70537 | + rc = vdbeSorterIterNext(db, pIter); |
| 70538 | + } |
| 70539 | + return rc; |
| 70540 | +} |
| 70541 | + |
| 70542 | + |
| 70543 | +/* |
| 70544 | +** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, |
| 70545 | +** size nKey2 bytes). Argument pKeyInfo supplies the collation functions |
| 70546 | +** used by the comparison. If an error occurs, return an SQLite error code. |
| 70547 | +** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive |
| 70548 | +** value, depending on whether key1 is smaller, equal to or larger than key2. |
| 70549 | +** |
| 70550 | +** If the bOmitRowid argument is non-zero, assume both keys end in a rowid |
| 70551 | +** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid |
| 70552 | +** is true and key1 contains even a single NULL value, it is considered to |
| 70553 | +** be less than key2. Even if key2 also contains NULL values. |
| 70554 | +** |
| 70555 | +** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace |
| 70556 | +** has been allocated and contains an unpacked record that is used as key2. |
| 70557 | +*/ |
| 70558 | +static int vdbeSorterCompare( |
| 70559 | + VdbeCursor *pCsr, /* Cursor object (for pKeyInfo) */ |
| 70560 | + int bOmitRowid, /* Ignore rowid field at end of keys */ |
| 70561 | + void *pKey1, int nKey1, /* Left side of comparison */ |
| 70562 | + void *pKey2, int nKey2, /* Right side of comparison */ |
| 70563 | + int *pRes /* OUT: Result of comparison */ |
| 70564 | +){ |
| 70565 | + KeyInfo *pKeyInfo = pCsr->pKeyInfo; |
| 70566 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70567 | + char *aSpace = pSorter->aSpace; |
| 70568 | + int nSpace = pSorter->nSpace; |
| 70569 | + UnpackedRecord *r2; |
| 70570 | + int i; |
| 70571 | + |
| 70572 | + if( aSpace==0 ){ |
| 70573 | + nSpace = ROUND8(sizeof(UnpackedRecord))+(pKeyInfo->nField+1)*sizeof(Mem); |
| 70574 | + aSpace = (char *)sqlite3Malloc(nSpace); |
| 70575 | + if( aSpace==0 ) return SQLITE_NOMEM; |
| 70576 | + pSorter->aSpace = aSpace; |
| 70577 | + pSorter->nSpace = nSpace; |
| 70578 | + } |
| 70579 | + |
| 70580 | + if( pKey2 ){ |
| 70581 | + /* This call cannot fail. As the memory is already allocated. */ |
| 70582 | + r2 = sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, aSpace, nSpace); |
| 70583 | + assert( r2 && (r2->flags & UNPACKED_NEED_FREE)==0 ); |
| 70584 | + assert( r2==(UnpackedRecord*)aSpace ); |
| 70585 | + }else{ |
| 70586 | + r2 = (UnpackedRecord *)aSpace; |
| 70587 | + assert( !bOmitRowid ); |
| 70588 | + } |
| 70589 | + |
| 70590 | + if( bOmitRowid ){ |
| 70591 | + for(i=0; i<r2->nField-1; i++){ |
| 70592 | + if( r2->aMem[i].flags & MEM_Null ){ |
| 70593 | + *pRes = -1; |
| 70594 | + return SQLITE_OK; |
| 70595 | + } |
| 70596 | + } |
| 70597 | + r2->flags |= UNPACKED_PREFIX_MATCH; |
| 70598 | + r2->nField--; |
| 70599 | + assert( r2->nField>0 ); |
| 70600 | + } |
| 70601 | + |
| 70602 | + *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2); |
| 70603 | + return SQLITE_OK; |
| 70604 | +} |
| 70605 | + |
| 70606 | +/* |
| 70607 | +** This function is called to compare two iterator keys when merging |
| 70608 | +** multiple b-tree segments. Parameter iOut is the index of the aTree[] |
| 70609 | +** value to recalculate. |
| 70610 | +*/ |
| 70611 | +static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){ |
| 70612 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70613 | + int i1; |
| 70614 | + int i2; |
| 70615 | + int iRes; |
| 70616 | + VdbeSorterIter *p1; |
| 70617 | + VdbeSorterIter *p2; |
| 70618 | + |
| 70619 | + assert( iOut<pSorter->nTree && iOut>0 ); |
| 70620 | + |
| 70621 | + if( iOut>=(pSorter->nTree/2) ){ |
| 70622 | + i1 = (iOut - pSorter->nTree/2) * 2; |
| 70623 | + i2 = i1 + 1; |
| 70624 | + }else{ |
| 70625 | + i1 = pSorter->aTree[iOut*2]; |
| 70626 | + i2 = pSorter->aTree[iOut*2+1]; |
| 70627 | + } |
| 70628 | + |
| 70629 | + p1 = &pSorter->aIter[i1]; |
| 70630 | + p2 = &pSorter->aIter[i2]; |
| 70631 | + |
| 70632 | + if( p1->pFile==0 ){ |
| 70633 | + iRes = i2; |
| 70634 | + }else if( p2->pFile==0 ){ |
| 70635 | + iRes = i1; |
| 70636 | + }else{ |
| 70637 | + int res; |
| 70638 | + int rc; |
| 70639 | + assert( pCsr->pSorter->aSpace!=0 ); /* allocated in vdbeSorterMerge() */ |
| 70640 | + rc = vdbeSorterCompare( |
| 70641 | + pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res |
| 70642 | + ); |
| 70643 | + /* The vdbeSorterCompare() call cannot fail since pCsr->pSorter->aSpace |
| 70644 | + ** has already been allocated. */ |
| 70645 | + assert( rc==SQLITE_OK ); |
| 70646 | + |
| 70647 | + if( res<=0 ){ |
| 70648 | + iRes = i1; |
| 70649 | + }else{ |
| 70650 | + iRes = i2; |
| 70651 | + } |
| 70652 | + } |
| 70653 | + |
| 70654 | + pSorter->aTree[iOut] = iRes; |
| 70655 | + return SQLITE_OK; |
| 70656 | +} |
| 70657 | + |
| 70658 | +/* |
| 70659 | +** Initialize the temporary index cursor just opened as a sorter cursor. |
| 70660 | +*/ |
| 70661 | +SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){ |
| 70662 | + int pgsz; /* Page size of main database */ |
| 70663 | + int mxCache; /* Cache size */ |
| 70664 | + VdbeSorter *pSorter; /* The new sorter */ |
| 70665 | + |
| 70666 | + assert( pCsr->pKeyInfo && pCsr->pBt==0 ); |
| 70667 | + pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter)); |
| 70668 | + if( pSorter==0 ){ |
| 70669 | + return SQLITE_NOMEM; |
| 70670 | + } |
| 70671 | + |
| 70672 | + if( !sqlite3TempInMemory(db) ){ |
| 70673 | + pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt); |
| 70674 | + pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz; |
| 70675 | + mxCache = db->aDb[0].pSchema->cache_size; |
| 70676 | + if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING; |
| 70677 | + pSorter->mxPmaSize = mxCache * pgsz; |
| 70678 | + } |
| 70679 | + |
| 70680 | + return SQLITE_OK; |
| 70681 | +} |
| 70682 | + |
| 70683 | +/* |
| 70684 | +** Free the list of sorted records starting at pRecord. |
| 70685 | +*/ |
| 70686 | +static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){ |
| 70687 | + SorterRecord *p; |
| 70688 | + SorterRecord *pNext; |
| 70689 | + for(p=pRecord; p; p=pNext){ |
| 70690 | + pNext = p->pNext; |
| 70691 | + sqlite3DbFree(db, p); |
| 70692 | + } |
| 70693 | +} |
| 70694 | + |
| 70695 | +/* |
| 70696 | +** Free any cursor components allocated by sqlite3VdbeSorterXXX routines. |
| 70697 | +*/ |
| 70698 | +SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){ |
| 70699 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70700 | + if( pSorter ){ |
| 70701 | + if( pSorter->aIter ){ |
| 70702 | + int i; |
| 70703 | + for(i=0; i<pSorter->nTree; i++){ |
| 70704 | + vdbeSorterIterZero(db, &pSorter->aIter[i]); |
| 70705 | + } |
| 70706 | + sqlite3DbFree(db, pSorter->aIter); |
| 70707 | + } |
| 70708 | + if( pSorter->pTemp1 ){ |
| 70709 | + sqlite3OsCloseFree(pSorter->pTemp1); |
| 70710 | + } |
| 70711 | + vdbeSorterRecordFree(db, pSorter->pRecord); |
| 70712 | + sqlite3_free(pSorter->aSpace); |
| 70713 | + sqlite3DbFree(db, pSorter); |
| 70714 | + pCsr->pSorter = 0; |
| 70715 | + } |
| 70716 | +} |
| 70717 | + |
| 70718 | +/* |
| 70719 | +** Allocate space for a file-handle and open a temporary file. If successful, |
| 70720 | +** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK. |
| 70721 | +** Otherwise, set *ppFile to 0 and return an SQLite error code. |
| 70722 | +*/ |
| 70723 | +static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){ |
| 70724 | + int dummy; |
| 70725 | + return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile, |
| 70726 | + SQLITE_OPEN_TEMP_JOURNAL | |
| 70727 | + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 70728 | + SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &dummy |
| 70729 | + ); |
| 70730 | +} |
| 70731 | + |
| 70732 | +/* |
| 70733 | +** Attemp to merge the two sorted lists p1 and p2 into a single list. If no |
| 70734 | +** error occurs set *ppOut to the head of the new list and return SQLITE_OK. |
| 70735 | +*/ |
| 70736 | +static int vdbeSorterMerge( |
| 70737 | + sqlite3 *db, /* Database handle */ |
| 70738 | + VdbeCursor *pCsr, /* For pKeyInfo */ |
| 70739 | + SorterRecord *p1, /* First list to merge */ |
| 70740 | + SorterRecord *p2, /* Second list to merge */ |
| 70741 | + SorterRecord **ppOut /* OUT: Head of merged list */ |
| 70742 | +){ |
| 70743 | + int rc = SQLITE_OK; |
| 70744 | + SorterRecord *pFinal = 0; |
| 70745 | + SorterRecord **pp = &pFinal; |
| 70746 | + void *pVal2 = p2 ? p2->pVal : 0; |
| 70747 | + |
| 70748 | + while( p1 && p2 ){ |
| 70749 | + int res; |
| 70750 | + rc = vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res); |
| 70751 | + if( rc!=SQLITE_OK ){ |
| 70752 | + *pp = 0; |
| 70753 | + vdbeSorterRecordFree(db, p1); |
| 70754 | + vdbeSorterRecordFree(db, p2); |
| 70755 | + vdbeSorterRecordFree(db, pFinal); |
| 70756 | + *ppOut = 0; |
| 70757 | + return rc; |
| 70758 | + } |
| 70759 | + if( res<=0 ){ |
| 70760 | + *pp = p1; |
| 70761 | + pp = &p1->pNext; |
| 70762 | + p1 = p1->pNext; |
| 70763 | + pVal2 = 0; |
| 70764 | + }else{ |
| 70765 | + *pp = p2; |
| 70766 | + pp = &p2->pNext; |
| 70767 | + p2 = p2->pNext; |
| 70768 | + if( p2==0 ) break; |
| 70769 | + pVal2 = p2->pVal; |
| 70770 | + } |
| 70771 | + } |
| 70772 | + *pp = p1 ? p1 : p2; |
| 70773 | + |
| 70774 | + *ppOut = pFinal; |
| 70775 | + return SQLITE_OK; |
| 70776 | +} |
| 70777 | + |
| 70778 | +/* |
| 70779 | +** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK |
| 70780 | +** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error |
| 70781 | +** occurs. |
| 70782 | +*/ |
| 70783 | +static int vdbeSorterSort(sqlite3 *db, VdbeCursor *pCsr){ |
| 70784 | + int rc = SQLITE_OK; |
| 70785 | + int i; |
| 70786 | + SorterRecord **aSlot; |
| 70787 | + SorterRecord *p; |
| 70788 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70789 | + |
| 70790 | + aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *)); |
| 70791 | + if( !aSlot ){ |
| 70792 | + return SQLITE_NOMEM; |
| 70793 | + } |
| 70794 | + |
| 70795 | + p = pSorter->pRecord; |
| 70796 | + while( p ){ |
| 70797 | + SorterRecord *pNext = p->pNext; |
| 70798 | + p->pNext = 0; |
| 70799 | + for(i=0; rc==SQLITE_OK && aSlot[i]; i++){ |
| 70800 | + rc = vdbeSorterMerge(db, pCsr, p, aSlot[i], &p); |
| 70801 | + aSlot[i] = 0; |
| 70802 | + } |
| 70803 | + if( rc!=SQLITE_OK ){ |
| 70804 | + vdbeSorterRecordFree(db, pNext); |
| 70805 | + break; |
| 70806 | + } |
| 70807 | + aSlot[i] = p; |
| 70808 | + p = pNext; |
| 70809 | + } |
| 70810 | + |
| 70811 | + p = 0; |
| 70812 | + for(i=0; i<64; i++){ |
| 70813 | + if( rc==SQLITE_OK ){ |
| 70814 | + rc = vdbeSorterMerge(db, pCsr, p, aSlot[i], &p); |
| 70815 | + }else{ |
| 70816 | + vdbeSorterRecordFree(db, aSlot[i]); |
| 70817 | + } |
| 70818 | + } |
| 70819 | + pSorter->pRecord = p; |
| 70820 | + |
| 70821 | + sqlite3_free(aSlot); |
| 70822 | + return rc; |
| 70823 | +} |
| 70824 | + |
| 70825 | + |
| 70826 | +/* |
| 70827 | +** Write the current contents of the in-memory linked-list to a PMA. Return |
| 70828 | +** SQLITE_OK if successful, or an SQLite error code otherwise. |
| 70829 | +** |
| 70830 | +** The format of a PMA is: |
| 70831 | +** |
| 70832 | +** * A varint. This varint contains the total number of bytes of content |
| 70833 | +** in the PMA (not including the varint itself). |
| 70834 | +** |
| 70835 | +** * One or more records packed end-to-end in order of ascending keys. |
| 70836 | +** Each record consists of a varint followed by a blob of data (the |
| 70837 | +** key). The varint is the number of bytes in the blob of data. |
| 70838 | +*/ |
| 70839 | +static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){ |
| 70840 | + int rc = SQLITE_OK; /* Return code */ |
| 70841 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70842 | + |
| 70843 | + if( pSorter->nInMemory==0 ){ |
| 70844 | + assert( pSorter->pRecord==0 ); |
| 70845 | + return rc; |
| 70846 | + } |
| 70847 | + |
| 70848 | + rc = vdbeSorterSort(db, pCsr); |
| 70849 | + |
| 70850 | + /* If the first temporary PMA file has not been opened, open it now. */ |
| 70851 | + if( rc==SQLITE_OK && pSorter->pTemp1==0 ){ |
| 70852 | + rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1); |
| 70853 | + assert( rc!=SQLITE_OK || pSorter->pTemp1 ); |
| 70854 | + assert( pSorter->iWriteOff==0 ); |
| 70855 | + assert( pSorter->nPMA==0 ); |
| 70856 | + } |
| 70857 | + |
| 70858 | + if( rc==SQLITE_OK ){ |
| 70859 | + i64 iOff = pSorter->iWriteOff; |
| 70860 | + SorterRecord *p; |
| 70861 | + SorterRecord *pNext = 0; |
| 70862 | + static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 70863 | + |
| 70864 | + pSorter->nPMA++; |
| 70865 | + rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff); |
| 70866 | + for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){ |
| 70867 | + pNext = p->pNext; |
| 70868 | + rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff); |
| 70869 | + |
| 70870 | + if( rc==SQLITE_OK ){ |
| 70871 | + rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff); |
| 70872 | + iOff += p->nVal; |
| 70873 | + } |
| 70874 | + |
| 70875 | + sqlite3DbFree(db, p); |
| 70876 | + } |
| 70877 | + |
| 70878 | + /* This assert verifies that unless an error has occurred, the size of |
| 70879 | + ** the PMA on disk is the same as the expected size stored in |
| 70880 | + ** pSorter->nInMemory. */ |
| 70881 | + assert( rc!=SQLITE_OK || pSorter->nInMemory==( |
| 70882 | + iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory) |
| 70883 | + )); |
| 70884 | + |
| 70885 | + pSorter->iWriteOff = iOff; |
| 70886 | + if( rc==SQLITE_OK ){ |
| 70887 | + /* Terminate each file with 8 extra bytes so that from any offset |
| 70888 | + ** in the file we can always read 9 bytes without a SHORT_READ error */ |
| 70889 | + rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff); |
| 70890 | + } |
| 70891 | + pSorter->pRecord = p; |
| 70892 | + } |
| 70893 | + |
| 70894 | + return rc; |
| 70895 | +} |
| 70896 | + |
| 70897 | +/* |
| 70898 | +** Add a record to the sorter. |
| 70899 | +*/ |
| 70900 | +SQLITE_PRIVATE int sqlite3VdbeSorterWrite( |
| 70901 | + sqlite3 *db, /* Database handle */ |
| 70902 | + VdbeCursor *pCsr, /* Sorter cursor */ |
| 70903 | + Mem *pVal /* Memory cell containing record */ |
| 70904 | +){ |
| 70905 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70906 | + int rc = SQLITE_OK; /* Return Code */ |
| 70907 | + SorterRecord *pNew; /* New list element */ |
| 70908 | + |
| 70909 | + assert( pSorter ); |
| 70910 | + pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n; |
| 70911 | + |
| 70912 | + pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord)); |
| 70913 | + if( pNew==0 ){ |
| 70914 | + rc = SQLITE_NOMEM; |
| 70915 | + }else{ |
| 70916 | + pNew->pVal = (void *)&pNew[1]; |
| 70917 | + memcpy(pNew->pVal, pVal->z, pVal->n); |
| 70918 | + pNew->nVal = pVal->n; |
| 70919 | + pNew->pNext = pSorter->pRecord; |
| 70920 | + pSorter->pRecord = pNew; |
| 70921 | + } |
| 70922 | + |
| 70923 | + /* See if the contents of the sorter should now be written out. They |
| 70924 | + ** are written out when either of the following are true: |
| 70925 | + ** |
| 70926 | + ** * The total memory allocated for the in-memory list is greater |
| 70927 | + ** than (page-size * cache-size), or |
| 70928 | + ** |
| 70929 | + ** * The total memory allocated for the in-memory list is greater |
| 70930 | + ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true. |
| 70931 | + */ |
| 70932 | + if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && ( |
| 70933 | + (pSorter->nInMemory>pSorter->mxPmaSize) |
| 70934 | + || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull()) |
| 70935 | + )){ |
| 70936 | + rc = vdbeSorterListToPMA(db, pCsr); |
| 70937 | + pSorter->nInMemory = 0; |
| 70938 | + } |
| 70939 | + |
| 70940 | + return rc; |
| 70941 | +} |
| 70942 | + |
| 70943 | +/* |
| 70944 | +** Helper function for sqlite3VdbeSorterRewind(). |
| 70945 | +*/ |
| 70946 | +static int vdbeSorterInitMerge( |
| 70947 | + sqlite3 *db, /* Database handle */ |
| 70948 | + VdbeCursor *pCsr, /* Cursor handle for this sorter */ |
| 70949 | + i64 *pnByte /* Sum of bytes in all opened PMAs */ |
| 70950 | +){ |
| 70951 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70952 | + int rc = SQLITE_OK; /* Return code */ |
| 70953 | + int i; /* Used to iterator through aIter[] */ |
| 70954 | + i64 nByte = 0; /* Total bytes in all opened PMAs */ |
| 70955 | + |
| 70956 | + /* Initialize the iterators. */ |
| 70957 | + for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){ |
| 70958 | + VdbeSorterIter *pIter = &pSorter->aIter[i]; |
| 70959 | + rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte); |
| 70960 | + pSorter->iReadOff = pIter->iEof; |
| 70961 | + assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff ); |
| 70962 | + if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break; |
| 70963 | + } |
| 70964 | + |
| 70965 | + /* Initialize the aTree[] array. */ |
| 70966 | + for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){ |
| 70967 | + rc = vdbeSorterDoCompare(pCsr, i); |
| 70968 | + } |
| 70969 | + |
| 70970 | + *pnByte = nByte; |
| 70971 | + return rc; |
| 70972 | +} |
| 70973 | + |
| 70974 | +/* |
| 70975 | +** Once the sorter has been populated, this function is called to prepare |
| 70976 | +** for iterating through its contents in sorted order. |
| 70977 | +*/ |
| 70978 | +SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){ |
| 70979 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 70980 | + int rc; /* Return code */ |
| 70981 | + sqlite3_file *pTemp2 = 0; /* Second temp file to use */ |
| 70982 | + i64 iWrite2 = 0; /* Write offset for pTemp2 */ |
| 70983 | + int nIter; /* Number of iterators used */ |
| 70984 | + int nByte; /* Bytes of space required for aIter/aTree */ |
| 70985 | + int N = 2; /* Power of 2 >= nIter */ |
| 70986 | + |
| 70987 | + assert( pSorter ); |
| 70988 | + |
| 70989 | + /* If no data has been written to disk, then do not do so now. Instead, |
| 70990 | + ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly |
| 70991 | + ** from the in-memory list. */ |
| 70992 | + if( pSorter->nPMA==0 ){ |
| 70993 | + *pbEof = !pSorter->pRecord; |
| 70994 | + assert( pSorter->aTree==0 ); |
| 70995 | + return vdbeSorterSort(db, pCsr); |
| 70996 | + } |
| 70997 | + |
| 70998 | + /* Write the current b-tree to a PMA. Close the b-tree cursor. */ |
| 70999 | + rc = vdbeSorterListToPMA(db, pCsr); |
| 71000 | + if( rc!=SQLITE_OK ) return rc; |
| 71001 | + |
| 71002 | + /* Allocate space for aIter[] and aTree[]. */ |
| 71003 | + nIter = pSorter->nPMA; |
| 71004 | + if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT; |
| 71005 | + assert( nIter>0 ); |
| 71006 | + while( N<nIter ) N += N; |
| 71007 | + nByte = N * (sizeof(int) + sizeof(VdbeSorterIter)); |
| 71008 | + pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte); |
| 71009 | + if( !pSorter->aIter ) return SQLITE_NOMEM; |
| 71010 | + pSorter->aTree = (int *)&pSorter->aIter[N]; |
| 71011 | + pSorter->nTree = N; |
| 71012 | + |
| 71013 | + do { |
| 71014 | + int iNew; /* Index of new, merged, PMA */ |
| 71015 | + |
| 71016 | + for(iNew=0; |
| 71017 | + rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; |
| 71018 | + iNew++ |
| 71019 | + ){ |
| 71020 | + i64 nWrite; /* Number of bytes in new PMA */ |
| 71021 | + |
| 71022 | + /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1, |
| 71023 | + ** initialize an iterator for each of them and break out of the loop. |
| 71024 | + ** These iterators will be incrementally merged as the VDBE layer calls |
| 71025 | + ** sqlite3VdbeSorterNext(). |
| 71026 | + ** |
| 71027 | + ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs, |
| 71028 | + ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs |
| 71029 | + ** are merged into a single PMA that is written to file pTemp2. |
| 71030 | + */ |
| 71031 | + rc = vdbeSorterInitMerge(db, pCsr, &nWrite); |
| 71032 | + assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile ); |
| 71033 | + if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){ |
| 71034 | + break; |
| 71035 | + } |
| 71036 | + |
| 71037 | + /* Open the second temp file, if it is not already open. */ |
| 71038 | + if( pTemp2==0 ){ |
| 71039 | + assert( iWrite2==0 ); |
| 71040 | + rc = vdbeSorterOpenTempFile(db, &pTemp2); |
| 71041 | + } |
| 71042 | + |
| 71043 | + if( rc==SQLITE_OK ){ |
| 71044 | + rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2); |
| 71045 | + } |
| 71046 | + |
| 71047 | + if( rc==SQLITE_OK ){ |
| 71048 | + int bEof = 0; |
| 71049 | + while( rc==SQLITE_OK && bEof==0 ){ |
| 71050 | + int nToWrite; |
| 71051 | + VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ]; |
| 71052 | + assert( pIter->pFile ); |
| 71053 | + nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey); |
| 71054 | + rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2); |
| 71055 | + iWrite2 += nToWrite; |
| 71056 | + if( rc==SQLITE_OK ){ |
| 71057 | + rc = sqlite3VdbeSorterNext(db, pCsr, &bEof); |
| 71058 | + } |
| 71059 | + } |
| 71060 | + } |
| 71061 | + } |
| 71062 | + |
| 71063 | + if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){ |
| 71064 | + break; |
| 71065 | + }else{ |
| 71066 | + sqlite3_file *pTmp = pSorter->pTemp1; |
| 71067 | + pSorter->nPMA = iNew; |
| 71068 | + pSorter->pTemp1 = pTemp2; |
| 71069 | + pTemp2 = pTmp; |
| 71070 | + pSorter->iWriteOff = iWrite2; |
| 71071 | + pSorter->iReadOff = 0; |
| 71072 | + iWrite2 = 0; |
| 71073 | + } |
| 71074 | + }while( rc==SQLITE_OK ); |
| 71075 | + |
| 71076 | + if( pTemp2 ){ |
| 71077 | + sqlite3OsCloseFree(pTemp2); |
| 71078 | + } |
| 71079 | + *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0); |
| 71080 | + return rc; |
| 71081 | +} |
| 71082 | + |
| 71083 | +/* |
| 71084 | +** Advance to the next element in the sorter. |
| 71085 | +*/ |
| 71086 | +SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){ |
| 71087 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 71088 | + int rc; /* Return code */ |
| 71089 | + |
| 71090 | + if( pSorter->aTree ){ |
| 71091 | + int iPrev = pSorter->aTree[1];/* Index of iterator to advance */ |
| 71092 | + int i; /* Index of aTree[] to recalculate */ |
| 71093 | + |
| 71094 | + rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]); |
| 71095 | + for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){ |
| 71096 | + rc = vdbeSorterDoCompare(pCsr, i); |
| 71097 | + } |
| 71098 | + |
| 71099 | + *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0); |
| 71100 | + }else{ |
| 71101 | + SorterRecord *pFree = pSorter->pRecord; |
| 71102 | + pSorter->pRecord = pFree->pNext; |
| 71103 | + pFree->pNext = 0; |
| 71104 | + vdbeSorterRecordFree(db, pFree); |
| 71105 | + *pbEof = !pSorter->pRecord; |
| 71106 | + rc = SQLITE_OK; |
| 71107 | + } |
| 71108 | + return rc; |
| 71109 | +} |
| 71110 | + |
| 71111 | +/* |
| 71112 | +** Return a pointer to a buffer owned by the sorter that contains the |
| 71113 | +** current key. |
| 71114 | +*/ |
| 71115 | +static void *vdbeSorterRowkey( |
| 71116 | + VdbeSorter *pSorter, /* Sorter object */ |
| 71117 | + int *pnKey /* OUT: Size of current key in bytes */ |
| 71118 | +){ |
| 71119 | + void *pKey; |
| 71120 | + if( pSorter->aTree ){ |
| 71121 | + VdbeSorterIter *pIter; |
| 71122 | + pIter = &pSorter->aIter[ pSorter->aTree[1] ]; |
| 71123 | + *pnKey = pIter->nKey; |
| 71124 | + pKey = pIter->aKey; |
| 71125 | + }else{ |
| 71126 | + *pnKey = pSorter->pRecord->nVal; |
| 71127 | + pKey = pSorter->pRecord->pVal; |
| 71128 | + } |
| 71129 | + return pKey; |
| 71130 | +} |
| 71131 | + |
| 71132 | +/* |
| 71133 | +** Copy the current sorter key into the memory cell pOut. |
| 71134 | +*/ |
| 71135 | +SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){ |
| 71136 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 71137 | + void *pKey; int nKey; /* Sorter key to copy into pOut */ |
| 71138 | + |
| 71139 | + pKey = vdbeSorterRowkey(pSorter, &nKey); |
| 71140 | + if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){ |
| 71141 | + return SQLITE_NOMEM; |
| 71142 | + } |
| 71143 | + pOut->n = nKey; |
| 71144 | + MemSetTypeFlag(pOut, MEM_Blob); |
| 71145 | + memcpy(pOut->z, pKey, nKey); |
| 71146 | + |
| 71147 | + return SQLITE_OK; |
| 71148 | +} |
| 71149 | + |
| 71150 | +/* |
| 71151 | +** Compare the key in memory cell pVal with the key that the sorter cursor |
| 71152 | +** passed as the first argument currently points to. For the purposes of |
| 71153 | +** the comparison, ignore the rowid field at the end of each record. |
| 71154 | +** |
| 71155 | +** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM). |
| 71156 | +** Otherwise, set *pRes to a negative, zero or positive value if the |
| 71157 | +** key in pVal is smaller than, equal to or larger than the current sorter |
| 71158 | +** key. |
| 71159 | +*/ |
| 71160 | +SQLITE_PRIVATE int sqlite3VdbeSorterCompare( |
| 71161 | + VdbeCursor *pCsr, /* Sorter cursor */ |
| 71162 | + Mem *pVal, /* Value to compare to current sorter key */ |
| 71163 | + int *pRes /* OUT: Result of comparison */ |
| 71164 | +){ |
| 71165 | + int rc; |
| 71166 | + VdbeSorter *pSorter = pCsr->pSorter; |
| 71167 | + void *pKey; int nKey; /* Sorter key to compare pVal with */ |
| 71168 | + |
| 71169 | + pKey = vdbeSorterRowkey(pSorter, &nKey); |
| 71170 | + rc = vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes); |
| 71171 | + assert( rc!=SQLITE_OK || pVal->db->mallocFailed || (*pRes)<=0 ); |
| 71172 | + return rc; |
| 71173 | +} |
| 71174 | + |
| 71175 | +#endif /* #ifndef SQLITE_OMIT_MERGE_SORT */ |
| 71176 | + |
| 71177 | +/************** End of vdbesort.c ********************************************/ |
| 69561 | 71178 | /************** Begin file journal.c *****************************************/ |
| 69562 | 71179 | /* |
| 69563 | 71180 | ** 2007 August 22 |
| 69564 | 71181 | ** |
| 69565 | 71182 | ** The author disclaims copyright to this source code. In place of |
| | @@ -70072,10 +71689,12 @@ |
| 70072 | 71689 | ** |
| 70073 | 71690 | ************************************************************************* |
| 70074 | 71691 | ** This file contains routines used for walking the parser tree for |
| 70075 | 71692 | ** an SQL statement. |
| 70076 | 71693 | */ |
| 71694 | +/* #include <stdlib.h> */ |
| 71695 | +/* #include <string.h> */ |
| 70077 | 71696 | |
| 70078 | 71697 | |
| 70079 | 71698 | /* |
| 70080 | 71699 | ** Walk an expression tree. Invoke the callback once for each node |
| 70081 | 71700 | ** of the expression, while decending. (In other words, the callback |
| | @@ -70210,10 +71829,12 @@ |
| 70210 | 71829 | ** |
| 70211 | 71830 | ** This file contains routines used for walking the parser tree and |
| 70212 | 71831 | ** resolve all identifiers by associating them with a particular |
| 70213 | 71832 | ** table and column. |
| 70214 | 71833 | */ |
| 71834 | +/* #include <stdlib.h> */ |
| 71835 | +/* #include <string.h> */ |
| 70215 | 71836 | |
| 70216 | 71837 | /* |
| 70217 | 71838 | ** Turn the pExpr expression into an alias for the iCol-th column of the |
| 70218 | 71839 | ** result set in pEList. |
| 70219 | 71840 | ** |
| | @@ -73700,11 +75321,11 @@ |
| 73700 | 75321 | if( !pAggInfo->directMode ){ |
| 73701 | 75322 | assert( pCol->iMem>0 ); |
| 73702 | 75323 | inReg = pCol->iMem; |
| 73703 | 75324 | break; |
| 73704 | 75325 | }else if( pAggInfo->useSortingIdx ){ |
| 73705 | | - sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, |
| 75326 | + sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, |
| 73706 | 75327 | pCol->iSorterColumn, target); |
| 73707 | 75328 | break; |
| 73708 | 75329 | } |
| 73709 | 75330 | /* Otherwise, fall thru into the TK_COLUMN case */ |
| 73710 | 75331 | } |
| | @@ -76012,100 +77633,10 @@ |
| 76012 | 77633 | ** May you find forgiveness for yourself and forgive others. |
| 76013 | 77634 | ** May you share freely, never taking more than you give. |
| 76014 | 77635 | ** |
| 76015 | 77636 | ************************************************************************* |
| 76016 | 77637 | ** 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 | 77638 | */ |
| 76108 | 77639 | #ifndef SQLITE_OMIT_ANALYZE |
| 76109 | 77640 | |
| 76110 | 77641 | /* |
| 76111 | 77642 | ** This routine generates code that opens the sqlite_stat1 table for |
| | @@ -76133,18 +77664,12 @@ |
| 76133 | 77664 | static const struct { |
| 76134 | 77665 | const char *zName; |
| 76135 | 77666 | const char *zCols; |
| 76136 | 77667 | } aTable[] = { |
| 76137 | 77668 | { "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", |
| 77669 | +#ifdef SQLITE_ENABLE_STAT2 |
| 77670 | + { "sqlite_stat2", "tbl,idx,sampleno,sample" }, |
| 76146 | 77671 | #endif |
| 76147 | 77672 | }; |
| 76148 | 77673 | |
| 76149 | 77674 | int aRoot[] = {0, 0}; |
| 76150 | 77675 | u8 aCreateTbl[] = {0, 0}; |
| | @@ -76156,21 +77681,10 @@ |
| 76156 | 77681 | if( v==0 ) return; |
| 76157 | 77682 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 76158 | 77683 | assert( sqlite3VdbeDb(v)==db ); |
| 76159 | 77684 | pDb = &db->aDb[iDb]; |
| 76160 | 77685 | |
| 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 | 77686 | for(i=0; i<ArraySize(aTable); i++){ |
| 76173 | 77687 | const char *zTab = aTable[i].zName; |
| 76174 | 77688 | Table *pStat; |
| 76175 | 77689 | if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){ |
| 76176 | 77690 | /* The sqlite_stat[12] table does not exist. Create it. Note that a |
| | @@ -76197,238 +77711,17 @@ |
| 76197 | 77711 | sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); |
| 76198 | 77712 | } |
| 76199 | 77713 | } |
| 76200 | 77714 | } |
| 76201 | 77715 | |
| 76202 | | - /* Open the sqlite_stat[13] tables for writing. */ |
| 77716 | + /* Open the sqlite_stat[12] tables for writing. */ |
| 76203 | 77717 | for(i=0; i<ArraySize(aTable); i++){ |
| 76204 | 77718 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb); |
| 76205 | 77719 | sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32); |
| 76206 | 77720 | sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
| 76207 | 77721 | } |
| 76208 | 77722 | } |
| 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 | 77723 | |
| 76431 | 77724 | /* |
| 76432 | 77725 | ** Generate code to do an analysis of all indices associated with |
| 76433 | 77726 | ** a single table. |
| 76434 | 77727 | */ |
| | @@ -76448,31 +77741,24 @@ |
| 76448 | 77741 | int endOfLoop; /* The end of the loop */ |
| 76449 | 77742 | int jZeroRows = -1; /* Jump from here if number of rows is zero */ |
| 76450 | 77743 | int iDb; /* Index of database containing pTab */ |
| 76451 | 77744 | int regTabname = iMem++; /* Register containing table name */ |
| 76452 | 77745 | 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 */ |
| 77746 | + int regSampleno = iMem++; /* Register containing next sample number */ |
| 77747 | + int regCol = iMem++; /* Content of a column analyzed table */ |
| 76470 | 77748 | int regRec = iMem++; /* Register holding completed record */ |
| 76471 | 77749 | int regTemp = iMem++; /* Temporary use register */ |
| 76472 | | - int regNewRowid = iMem++; /* Rowid for the inserted record */ |
| 77750 | + int regRowid = iMem++; /* Rowid for the inserted record */ |
| 76473 | 77751 | |
| 77752 | +#ifdef SQLITE_ENABLE_STAT2 |
| 77753 | + int addr = 0; /* Instruction address */ |
| 77754 | + int regTemp2 = iMem++; /* Temporary use register */ |
| 77755 | + int regSamplerecno = iMem++; /* Index of next sample to record */ |
| 77756 | + int regRecno = iMem++; /* Current sample index */ |
| 77757 | + int regLast = iMem++; /* Index of last sample to record */ |
| 77758 | + int regFirst = iMem++; /* Index of first sample to record */ |
| 77759 | +#endif |
| 76474 | 77760 | |
| 76475 | 77761 | v = sqlite3GetVdbe(pParse); |
| 76476 | 77762 | if( v==0 || NEVER(pTab==0) ){ |
| 76477 | 77763 | return; |
| 76478 | 77764 | } |
| | @@ -76501,22 +77787,17 @@ |
| 76501 | 77787 | iIdxCur = pParse->nTab++; |
| 76502 | 77788 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); |
| 76503 | 77789 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 76504 | 77790 | int nCol; |
| 76505 | 77791 | KeyInfo *pKey; |
| 76506 | | - int addrIfNot = 0; /* address of OP_IfNot */ |
| 76507 | | - int *aChngAddr; /* Array of jump instruction addresses */ |
| 76508 | 77792 | |
| 76509 | 77793 | if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; |
| 76510 | | - VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName)); |
| 76511 | 77794 | nCol = pIdx->nColumn; |
| 76512 | 77795 | pKey = sqlite3IndexKeyinfo(pParse, pIdx); |
| 76513 | 77796 | if( iMem+1+(nCol*2)>pParse->nMem ){ |
| 76514 | 77797 | pParse->nMem = iMem+1+(nCol*2); |
| 76515 | 77798 | } |
| 76516 | | - aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*pIdx->nColumn); |
| 76517 | | - if( aChngAddr==0 ) continue; |
| 76518 | 77799 | |
| 76519 | 77800 | /* Open a cursor to the index to be analyzed. */ |
| 76520 | 77801 | assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
| 76521 | 77802 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, |
| 76522 | 77803 | (char *)pKey, P4_KEYINFO_HANDOFF); |
| | @@ -76523,24 +77804,35 @@ |
| 76523 | 77804 | VdbeComment((v, "%s", pIdx->zName)); |
| 76524 | 77805 | |
| 76525 | 77806 | /* Populate the register containing the index name. */ |
| 76526 | 77807 | sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); |
| 76527 | 77808 | |
| 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 */ |
| 77809 | +#ifdef SQLITE_ENABLE_STAT2 |
| 77810 | + |
| 77811 | + /* If this iteration of the loop is generating code to analyze the |
| 77812 | + ** first index in the pTab->pIndex list, then register regLast has |
| 77813 | + ** not been populated. In this case populate it now. */ |
| 77814 | + if( pTab->pIndex==pIdx ){ |
| 77815 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno); |
| 77816 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp); |
| 77817 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2); |
| 77818 | + |
| 77819 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast); |
| 77820 | + sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst); |
| 77821 | + addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast); |
| 77822 | + sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst); |
| 77823 | + sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast); |
| 77824 | + sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2); |
| 77825 | + sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast); |
| 77826 | + sqlite3VdbeJumpHere(v, addr); |
| 77827 | + } |
| 77828 | + |
| 77829 | + /* Zero the regSampleno and regRecno registers. */ |
| 77830 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno); |
| 77831 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno); |
| 77832 | + sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno); |
| 77833 | +#endif |
| 76542 | 77834 | |
| 76543 | 77835 | /* The block of memory cells initialized here is used as follows. |
| 76544 | 77836 | ** |
| 76545 | 77837 | ** iMem: |
| 76546 | 77838 | ** The total number of rows in the table. |
| | @@ -76566,87 +77858,79 @@ |
| 76566 | 77858 | /* Start the analysis loop. This loop runs through all the entries in |
| 76567 | 77859 | ** the index b-tree. */ |
| 76568 | 77860 | endOfLoop = sqlite3VdbeMakeLabel(v); |
| 76569 | 77861 | sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop); |
| 76570 | 77862 | topOfLoop = sqlite3VdbeCurrentAddr(v); |
| 76571 | | - sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */ |
| 77863 | + sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); |
| 76572 | 77864 | |
| 76573 | 77865 | for(i=0; i<nCol; i++){ |
| 76574 | 77866 | CollSeq *pColl; |
| 76575 | 77867 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol); |
| 76576 | 77868 | if( i==0 ){ |
| 77869 | +#ifdef SQLITE_ENABLE_STAT2 |
| 77870 | + /* Check if the record that cursor iIdxCur points to contains a |
| 77871 | + ** value that should be stored in the sqlite_stat2 table. If so, |
| 77872 | + ** store it. */ |
| 77873 | + int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno); |
| 77874 | + assert( regTabname+1==regIdxname |
| 77875 | + && regTabname+2==regSampleno |
| 77876 | + && regTabname+3==regCol |
| 77877 | + ); |
| 77878 | + sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); |
| 77879 | + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0); |
| 77880 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid); |
| 77881 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid); |
| 77882 | + |
| 77883 | + /* Calculate new values for regSamplerecno and regSampleno. |
| 77884 | + ** |
| 77885 | + ** sampleno = sampleno + 1 |
| 77886 | + ** samplerecno = samplerecno+(remaining records)/(remaining samples) |
| 77887 | + */ |
| 77888 | + sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1); |
| 77889 | + sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp); |
| 77890 | + sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 77891 | + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2); |
| 77892 | + sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2); |
| 77893 | + sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp); |
| 77894 | + sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno); |
| 77895 | + |
| 77896 | + sqlite3VdbeJumpHere(v, ne); |
| 77897 | + sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1); |
| 77898 | +#endif |
| 77899 | + |
| 76577 | 77900 | /* Always record the very first row */ |
| 76578 | | - addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 77901 | + sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1); |
| 76579 | 77902 | } |
| 76580 | 77903 | assert( pIdx->azColl!=0 ); |
| 76581 | 77904 | assert( pIdx->azColl[i]!=0 ); |
| 76582 | 77905 | pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 76583 | | - aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 76584 | | - (char*)pColl, P4_COLLSEQ); |
| 77906 | + sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, |
| 77907 | + (char*)pColl, P4_COLLSEQ); |
| 76585 | 77908 | 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 |
| 77909 | + } |
| 77910 | + if( db->mallocFailed ){ |
| 77911 | + /* If a malloc failure has occurred, then the result of the expression |
| 77912 | + ** passed as the second argument to the call to sqlite3VdbeJumpHere() |
| 77913 | + ** below may be negative. Which causes an assert() to fail (or an |
| 77914 | + ** out-of-bounds write if SQLITE_DEBUG is not defined). */ |
| 77915 | + return; |
| 76593 | 77916 | } |
| 76594 | 77917 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop); |
| 76595 | 77918 | for(i=0; i<nCol; i++){ |
| 76596 | | - sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */ |
| 77919 | + int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2); |
| 76597 | 77920 | 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 | | - } |
| 77921 | + sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */ |
| 77922 | + } |
| 77923 | + sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */ |
| 76609 | 77924 | sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1); |
| 76610 | 77925 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1); |
| 76611 | 77926 | } |
| 76612 | | - sqlite3DbFree(db, aChngAddr); |
| 76613 | 77927 | |
| 76614 | | - /* Always jump here after updating the iMem+1...iMem+1+nCol counters */ |
| 77928 | + /* End of the analysis loop. */ |
| 76615 | 77929 | sqlite3VdbeResolveLabel(v, endOfLoop); |
| 76616 | | - |
| 76617 | 77930 | sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop); |
| 76618 | 77931 | 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 | 77932 | |
| 76649 | 77933 | /* Store the results in sqlite_stat1. |
| 76650 | 77934 | ** |
| 76651 | 77935 | ** The result is a single row of the sqlite_stat1 table. The first |
| 76652 | 77936 | ** two columns are the names of the table and index. The third column |
| | @@ -76662,51 +77946,50 @@ |
| 76662 | 77946 | ** |
| 76663 | 77947 | ** If K==0 then no entry is made into the sqlite_stat1 table. |
| 76664 | 77948 | ** If K>0 then it is always the case the D>0 so division by zero |
| 76665 | 77949 | ** is never possible. |
| 76666 | 77950 | */ |
| 76667 | | - sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1); |
| 77951 | + sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); |
| 76668 | 77952 | if( jZeroRows<0 ){ |
| 76669 | 77953 | jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); |
| 76670 | 77954 | } |
| 76671 | 77955 | for(i=0; i<nCol; i++){ |
| 76672 | 77956 | sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0); |
| 76673 | | - sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); |
| 77957 | + sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 76674 | 77958 | sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp); |
| 76675 | 77959 | sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1); |
| 76676 | 77960 | sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp); |
| 76677 | 77961 | sqlite3VdbeAddOp1(v, OP_ToInt, regTemp); |
| 76678 | | - sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1); |
| 77962 | + sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno); |
| 76679 | 77963 | } |
| 76680 | 77964 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 76681 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 76682 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); |
| 77965 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 77966 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 76683 | 77967 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 76684 | 77968 | } |
| 76685 | 77969 | |
| 76686 | 77970 | /* If the table has no indices, create a single sqlite_stat1 entry |
| 76687 | 77971 | ** containing NULL as the index name and the row count as the content. |
| 76688 | 77972 | */ |
| 76689 | 77973 | if( pTab->pIndex==0 ){ |
| 76690 | 77974 | sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); |
| 76691 | 77975 | VdbeComment((v, "%s", pTab->zName)); |
| 76692 | | - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1); |
| 77976 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno); |
| 76693 | 77977 | sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); |
| 76694 | | - jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); |
| 77978 | + jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno); |
| 76695 | 77979 | }else{ |
| 76696 | 77980 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 76697 | 77981 | jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto); |
| 76698 | 77982 | } |
| 76699 | 77983 | sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); |
| 76700 | 77984 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); |
| 76701 | | - sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 76702 | | - sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid); |
| 77985 | + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); |
| 77986 | + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); |
| 76703 | 77987 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 76704 | 77988 | if( pParse->nMem<regRec ) pParse->nMem = regRec; |
| 76705 | 77989 | sqlite3VdbeJumpHere(v, jZeroRows); |
| 76706 | 77990 | } |
| 76707 | | - |
| 76708 | 77991 | |
| 76709 | 77992 | /* |
| 76710 | 77993 | ** Generate code that will cause the most recent index analysis to |
| 76711 | 77994 | ** be loaded into internal hash tables where is can be used. |
| 76712 | 77995 | */ |
| | @@ -76727,11 +78010,11 @@ |
| 76727 | 78010 | int iStatCur; |
| 76728 | 78011 | int iMem; |
| 76729 | 78012 | |
| 76730 | 78013 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 76731 | 78014 | iStatCur = pParse->nTab; |
| 76732 | | - pParse->nTab += 3; |
| 78015 | + pParse->nTab += 2; |
| 76733 | 78016 | openStatTable(pParse, iDb, iStatCur, 0, 0); |
| 76734 | 78017 | iMem = pParse->nMem+1; |
| 76735 | 78018 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 76736 | 78019 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 76737 | 78020 | Table *pTab = (Table*)sqliteHashData(k); |
| | @@ -76752,11 +78035,11 @@ |
| 76752 | 78035 | assert( pTab!=0 ); |
| 76753 | 78036 | assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
| 76754 | 78037 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 76755 | 78038 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 76756 | 78039 | iStatCur = pParse->nTab; |
| 76757 | | - pParse->nTab += 3; |
| 78040 | + pParse->nTab += 2; |
| 76758 | 78041 | if( pOnlyIdx ){ |
| 76759 | 78042 | openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); |
| 76760 | 78043 | }else{ |
| 76761 | 78044 | openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); |
| 76762 | 78045 | } |
| | @@ -76857,11 +78140,11 @@ |
| 76857 | 78140 | static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
| 76858 | 78141 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 76859 | 78142 | Index *pIndex; |
| 76860 | 78143 | Table *pTable; |
| 76861 | 78144 | int i, c, n; |
| 76862 | | - tRowcnt v; |
| 78145 | + unsigned int v; |
| 76863 | 78146 | const char *z; |
| 76864 | 78147 | |
| 76865 | 78148 | assert( argc==3 ); |
| 76866 | 78149 | UNUSED_PARAMETER2(NotUsed, argc); |
| 76867 | 78150 | |
| | @@ -76900,172 +78183,40 @@ |
| 76900 | 78183 | /* |
| 76901 | 78184 | ** If the Index.aSample variable is not NULL, delete the aSample[] array |
| 76902 | 78185 | ** and its contents. |
| 76903 | 78186 | */ |
| 76904 | 78187 | SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ |
| 76905 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 78188 | +#ifdef SQLITE_ENABLE_STAT2 |
| 76906 | 78189 | if( pIdx->aSample ){ |
| 76907 | 78190 | int j; |
| 76908 | | - for(j=0; j<pIdx->nSample; j++){ |
| 78191 | + for(j=0; j<SQLITE_INDEX_SAMPLES; j++){ |
| 76909 | 78192 | IndexSample *p = &pIdx->aSample[j]; |
| 76910 | 78193 | if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ |
| 76911 | | - sqlite3_free(p->u.z); |
| 78194 | + sqlite3DbFree(db, p->u.z); |
| 76912 | 78195 | } |
| 76913 | 78196 | } |
| 76914 | | - sqlite3_free(pIdx->aSample); |
| 78197 | + sqlite3DbFree(db, pIdx->aSample); |
| 76915 | 78198 | } |
| 76916 | | - UNUSED_PARAMETER(db); |
| 76917 | | - pIdx->nSample = 0; |
| 76918 | | - pIdx->aSample = 0; |
| 76919 | 78199 | #else |
| 76920 | 78200 | UNUSED_PARAMETER(db); |
| 76921 | 78201 | UNUSED_PARAMETER(pIdx); |
| 76922 | 78202 | #endif |
| 76923 | 78203 | } |
| 76924 | 78204 | |
| 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 |
| 78205 | +/* |
| 78206 | +** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The |
| 77056 | 78207 | ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] |
| 77057 | | -** arrays. The contents of sqlite_stat3 are used to populate the |
| 78208 | +** arrays. The contents of sqlite_stat2 are used to populate the |
| 77058 | 78209 | ** Index.aSample[] arrays. |
| 77059 | 78210 | ** |
| 77060 | 78211 | ** 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 |
| 78212 | +** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined |
| 78213 | +** during compilation and the sqlite_stat2 table is present, no data is |
| 77063 | 78214 | ** read from it. |
| 77064 | 78215 | ** |
| 77065 | | -** If SQLITE_ENABLE_STAT3 was defined during compilation and the |
| 77066 | | -** sqlite_stat3 table is not present in the database, SQLITE_ERROR is |
| 78216 | +** If SQLITE_ENABLE_STAT2 was defined during compilation and the |
| 78217 | +** sqlite_stat2 table is not present in the database, SQLITE_ERROR is |
| 77067 | 78218 | ** returned. However, in this case, data is read from the sqlite_stat1 |
| 77068 | 78219 | ** table (if it is present) before returning. |
| 77069 | 78220 | ** |
| 77070 | 78221 | ** If an OOM error occurs, this function always sets db->mallocFailed. |
| 77071 | 78222 | ** This means if the caller does not care about other errors, the return |
| | @@ -77083,14 +78234,12 @@ |
| 77083 | 78234 | /* Clear any prior statistics */ |
| 77084 | 78235 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 77085 | 78236 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
| 77086 | 78237 | Index *pIdx = sqliteHashData(i); |
| 77087 | 78238 | sqlite3DefaultRowEst(pIdx); |
| 77088 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 77089 | 78239 | sqlite3DeleteIndexSamples(db, pIdx); |
| 77090 | 78240 | pIdx->aSample = 0; |
| 77091 | | -#endif |
| 77092 | 78241 | } |
| 77093 | 78242 | |
| 77094 | 78243 | /* Check to make sure the sqlite_stat1 table exists */ |
| 77095 | 78244 | sInfo.db = db; |
| 77096 | 78245 | sInfo.zDatabase = db->aDb[iDb].zName; |
| | @@ -77098,23 +78247,91 @@ |
| 77098 | 78247 | return SQLITE_ERROR; |
| 77099 | 78248 | } |
| 77100 | 78249 | |
| 77101 | 78250 | /* Load new statistics out of the sqlite_stat1 table */ |
| 77102 | 78251 | zSql = sqlite3MPrintf(db, |
| 77103 | | - "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 78252 | + "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
| 77104 | 78253 | if( zSql==0 ){ |
| 77105 | 78254 | rc = SQLITE_NOMEM; |
| 77106 | 78255 | }else{ |
| 77107 | 78256 | rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
| 77108 | 78257 | sqlite3DbFree(db, zSql); |
| 77109 | 78258 | } |
| 77110 | 78259 | |
| 77111 | 78260 | |
| 77112 | | - /* Load the statistics from the sqlite_stat3 table. */ |
| 77113 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 78261 | + /* Load the statistics from the sqlite_stat2 table. */ |
| 78262 | +#ifdef SQLITE_ENABLE_STAT2 |
| 78263 | + if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){ |
| 78264 | + rc = SQLITE_ERROR; |
| 78265 | + } |
| 77114 | 78266 | if( rc==SQLITE_OK ){ |
| 77115 | | - rc = loadStat3(db, sInfo.zDatabase); |
| 78267 | + sqlite3_stmt *pStmt = 0; |
| 78268 | + |
| 78269 | + zSql = sqlite3MPrintf(db, |
| 78270 | + "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase); |
| 78271 | + if( !zSql ){ |
| 78272 | + rc = SQLITE_NOMEM; |
| 78273 | + }else{ |
| 78274 | + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 78275 | + sqlite3DbFree(db, zSql); |
| 78276 | + } |
| 78277 | + |
| 78278 | + if( rc==SQLITE_OK ){ |
| 78279 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 78280 | + char *zIndex; /* Index name */ |
| 78281 | + Index *pIdx; /* Pointer to the index object */ |
| 78282 | + |
| 78283 | + zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 78284 | + pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0; |
| 78285 | + if( pIdx ){ |
| 78286 | + int iSample = sqlite3_column_int(pStmt, 1); |
| 78287 | + if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){ |
| 78288 | + int eType = sqlite3_column_type(pStmt, 2); |
| 78289 | + |
| 78290 | + if( pIdx->aSample==0 ){ |
| 78291 | + static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES; |
| 78292 | + pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz); |
| 78293 | + if( pIdx->aSample==0 ){ |
| 78294 | + db->mallocFailed = 1; |
| 78295 | + break; |
| 78296 | + } |
| 78297 | + memset(pIdx->aSample, 0, sz); |
| 78298 | + } |
| 78299 | + |
| 78300 | + assert( pIdx->aSample ); |
| 78301 | + { |
| 78302 | + IndexSample *pSample = &pIdx->aSample[iSample]; |
| 78303 | + pSample->eType = (u8)eType; |
| 78304 | + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ |
| 78305 | + pSample->u.r = sqlite3_column_double(pStmt, 2); |
| 78306 | + }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ |
| 78307 | + const char *z = (const char *)( |
| 78308 | + (eType==SQLITE_BLOB) ? |
| 78309 | + sqlite3_column_blob(pStmt, 2): |
| 78310 | + sqlite3_column_text(pStmt, 2) |
| 78311 | + ); |
| 78312 | + int n = sqlite3_column_bytes(pStmt, 2); |
| 78313 | + if( n>24 ){ |
| 78314 | + n = 24; |
| 78315 | + } |
| 78316 | + pSample->nByte = (u8)n; |
| 78317 | + if( n < 1){ |
| 78318 | + pSample->u.z = 0; |
| 78319 | + }else{ |
| 78320 | + pSample->u.z = sqlite3DbStrNDup(0, z, n); |
| 78321 | + if( pSample->u.z==0 ){ |
| 78322 | + db->mallocFailed = 1; |
| 78323 | + break; |
| 78324 | + } |
| 78325 | + } |
| 78326 | + } |
| 78327 | + } |
| 78328 | + } |
| 78329 | + } |
| 78330 | + } |
| 78331 | + rc = sqlite3_finalize(pStmt); |
| 78332 | + } |
| 77116 | 78333 | } |
| 77117 | 78334 | #endif |
| 77118 | 78335 | |
| 77119 | 78336 | if( rc==SQLITE_NOMEM ){ |
| 77120 | 78337 | db->mallocFailed = 1; |
| | @@ -79610,11 +80827,11 @@ |
| 79610 | 80827 | Table *p; |
| 79611 | 80828 | int n; |
| 79612 | 80829 | const char *z; |
| 79613 | 80830 | Token sEnd; |
| 79614 | 80831 | DbFixer sFix; |
| 79615 | | - Token *pName; |
| 80832 | + Token *pName = 0; |
| 79616 | 80833 | int iDb; |
| 79617 | 80834 | sqlite3 *db = pParse->db; |
| 79618 | 80835 | |
| 79619 | 80836 | if( pParse->nVar>0 ){ |
| 79620 | 80837 | sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
| | @@ -79926,15 +81143,11 @@ |
| 79926 | 81143 | Parse *pParse, /* The parsing context */ |
| 79927 | 81144 | int iDb, /* The database number */ |
| 79928 | 81145 | const char *zType, /* "idx" or "tbl" */ |
| 79929 | 81146 | const char *zName /* Name of index or table */ |
| 79930 | 81147 | ){ |
| 79931 | | - static const char *azStatTab[] = { |
| 79932 | | - "sqlite_stat1", |
| 79933 | | - "sqlite_stat2", |
| 79934 | | - "sqlite_stat3", |
| 79935 | | - }; |
| 81148 | + static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" }; |
| 79936 | 81149 | int i; |
| 79937 | 81150 | const char *zDbName = pParse->db->aDb[iDb].zName; |
| 79938 | 81151 | for(i=0; i<ArraySize(azStatTab); i++){ |
| 79939 | 81152 | if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){ |
| 79940 | 81153 | sqlite3NestedParse(pParse, |
| | @@ -79941,81 +81154,10 @@ |
| 79941 | 81154 | "DELETE FROM %Q.%s WHERE %s=%Q", |
| 79942 | 81155 | zDbName, azStatTab[i], zType, zName |
| 79943 | 81156 | ); |
| 79944 | 81157 | } |
| 79945 | 81158 | } |
| 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 | 81159 | } |
| 80018 | 81160 | |
| 80019 | 81161 | /* |
| 80020 | 81162 | ** This routine is called to do the work of a DROP TABLE statement. |
| 80021 | 81163 | ** pName is the name of the table to be dropped. |
| | @@ -80082,11 +81224,11 @@ |
| 80082 | 81224 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 80083 | 81225 | goto exit_drop_table; |
| 80084 | 81226 | } |
| 80085 | 81227 | } |
| 80086 | 81228 | #endif |
| 80087 | | - if( !pParse->nested && sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 81229 | + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 80088 | 81230 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
| 80089 | 81231 | goto exit_drop_table; |
| 80090 | 81232 | } |
| 80091 | 81233 | |
| 80092 | 81234 | #ifndef SQLITE_OMIT_VIEW |
| | @@ -80106,15 +81248,72 @@ |
| 80106 | 81248 | /* Generate code to remove the table from the master table |
| 80107 | 81249 | ** on disk. |
| 80108 | 81250 | */ |
| 80109 | 81251 | v = sqlite3GetVdbe(pParse); |
| 80110 | 81252 | if( v ){ |
| 81253 | + Trigger *pTrigger; |
| 81254 | + Db *pDb = &db->aDb[iDb]; |
| 80111 | 81255 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 81256 | + |
| 81257 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 81258 | + if( IsVirtual(pTab) ){ |
| 81259 | + sqlite3VdbeAddOp0(v, OP_VBegin); |
| 81260 | + } |
| 81261 | +#endif |
| 80112 | 81262 | sqlite3FkDropTable(pParse, pName, pTab); |
| 80113 | | - sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
| 81263 | + |
| 81264 | + /* Drop all triggers associated with the table being dropped. Code |
| 81265 | + ** is generated to remove entries from sqlite_master and/or |
| 81266 | + ** sqlite_temp_master if required. |
| 81267 | + */ |
| 81268 | + pTrigger = sqlite3TriggerList(pParse, pTab); |
| 81269 | + while( pTrigger ){ |
| 81270 | + assert( pTrigger->pSchema==pTab->pSchema || |
| 81271 | + pTrigger->pSchema==db->aDb[1].pSchema ); |
| 81272 | + sqlite3DropTriggerPtr(pParse, pTrigger); |
| 81273 | + pTrigger = pTrigger->pNext; |
| 81274 | + } |
| 81275 | + |
| 81276 | +#ifndef SQLITE_OMIT_AUTOINCREMENT |
| 81277 | + /* Remove any entries of the sqlite_sequence table associated with |
| 81278 | + ** the table being dropped. This is done before the table is dropped |
| 81279 | + ** at the btree level, in case the sqlite_sequence table needs to |
| 81280 | + ** move as a result of the drop (can happen in auto-vacuum mode). |
| 81281 | + */ |
| 81282 | + if( pTab->tabFlags & TF_Autoincrement ){ |
| 81283 | + sqlite3NestedParse(pParse, |
| 81284 | + "DELETE FROM %s.sqlite_sequence WHERE name=%Q", |
| 81285 | + pDb->zName, pTab->zName |
| 81286 | + ); |
| 81287 | + } |
| 81288 | +#endif |
| 81289 | + |
| 81290 | + /* Drop all SQLITE_MASTER table and index entries that refer to the |
| 81291 | + ** table. The program name loops through the master table and deletes |
| 81292 | + ** every row that refers to a table of the same name as the one being |
| 81293 | + ** dropped. Triggers are handled seperately because a trigger can be |
| 81294 | + ** created in the temp database that refers to a table in another |
| 81295 | + ** database. |
| 81296 | + */ |
| 81297 | + sqlite3NestedParse(pParse, |
| 81298 | + "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
| 81299 | + pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); |
| 80114 | 81300 | sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 81301 | + if( !isView && !IsVirtual(pTab) ){ |
| 81302 | + destroyTable(pParse, pTab); |
| 81303 | + } |
| 81304 | + |
| 81305 | + /* Remove the table entry from SQLite's internal schema and modify |
| 81306 | + ** the schema cookie. |
| 81307 | + */ |
| 81308 | + if( IsVirtual(pTab) ){ |
| 81309 | + sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
| 81310 | + } |
| 81311 | + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 81312 | + sqlite3ChangeCookie(pParse, iDb); |
| 80115 | 81313 | } |
| 81314 | + sqliteViewResetAll(db, iDb); |
| 80116 | 81315 | |
| 80117 | 81316 | exit_drop_table: |
| 80118 | 81317 | sqlite3SrcListDelete(db, pName); |
| 80119 | 81318 | } |
| 80120 | 81319 | |
| | @@ -80278,11 +81477,13 @@ |
| 80278 | 81477 | */ |
| 80279 | 81478 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 80280 | 81479 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
| 80281 | 81480 | int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
| 80282 | 81481 | int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
| 81482 | + int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */ |
| 80283 | 81483 | int addr1; /* Address of top of loop */ |
| 81484 | + int addr2; /* Address to jump to for next iteration */ |
| 80284 | 81485 | int tnum; /* Root page of index */ |
| 80285 | 81486 | Vdbe *v; /* Generate code into this virtual machine */ |
| 80286 | 81487 | KeyInfo *pKey; /* KeyInfo for index */ |
| 80287 | 81488 | int regIdxKey; /* Registers containing the index key */ |
| 80288 | 81489 | int regRecord; /* Register holding assemblied index record */ |
| | @@ -80311,14 +81512,45 @@ |
| 80311 | 81512 | sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, |
| 80312 | 81513 | (char *)pKey, P4_KEYINFO_HANDOFF); |
| 80313 | 81514 | if( memRootPage>=0 ){ |
| 80314 | 81515 | sqlite3VdbeChangeP5(v, 1); |
| 80315 | 81516 | } |
| 81517 | + |
| 81518 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 81519 | + /* Open the sorter cursor if we are to use one. */ |
| 81520 | + iSorter = pParse->nTab++; |
| 81521 | + sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO); |
| 81522 | +#endif |
| 81523 | + |
| 81524 | + /* Open the table. Loop through all rows of the table, inserting index |
| 81525 | + ** records into the sorter. */ |
| 80316 | 81526 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 80317 | 81527 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); |
| 81528 | + addr2 = addr1 + 1; |
| 80318 | 81529 | regRecord = sqlite3GetTempReg(pParse); |
| 80319 | 81530 | regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); |
| 81531 | + |
| 81532 | +#ifndef SQLITE_OMIT_MERGE_SORT |
| 81533 | + sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
| 81534 | + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); |
| 81535 | + sqlite3VdbeJumpHere(v, addr1); |
| 81536 | + addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); |
| 81537 | + if( pIndex->onError!=OE_None ){ |
| 81538 | + int j2 = sqlite3VdbeCurrentAddr(v) + 3; |
| 81539 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, j2); |
| 81540 | + addr2 = sqlite3VdbeCurrentAddr(v); |
| 81541 | + sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord); |
| 81542 | + sqlite3HaltConstraint( |
| 81543 | + pParse, OE_Abort, "indexed columns are not unique", P4_STATIC |
| 81544 | + ); |
| 81545 | + }else{ |
| 81546 | + addr2 = sqlite3VdbeCurrentAddr(v); |
| 81547 | + } |
| 81548 | + sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord); |
| 81549 | + sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1); |
| 81550 | + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 81551 | +#else |
| 80320 | 81552 | if( pIndex->onError!=OE_None ){ |
| 80321 | 81553 | const int regRowid = regIdxKey + pIndex->nColumn; |
| 80322 | 81554 | const int j2 = sqlite3VdbeCurrentAddr(v) + 2; |
| 80323 | 81555 | void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey); |
| 80324 | 81556 | |
| | @@ -80333,17 +81565,20 @@ |
| 80333 | 81565 | */ |
| 80334 | 81566 | sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32); |
| 80335 | 81567 | sqlite3HaltConstraint( |
| 80336 | 81568 | pParse, OE_Abort, "indexed columns are not unique", P4_STATIC); |
| 80337 | 81569 | } |
| 80338 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
| 81570 | + sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0); |
| 80339 | 81571 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 81572 | +#endif |
| 80340 | 81573 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 80341 | | - sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); |
| 81574 | + sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); |
| 80342 | 81575 | sqlite3VdbeJumpHere(v, addr1); |
| 81576 | + |
| 80343 | 81577 | sqlite3VdbeAddOp1(v, OP_Close, iTab); |
| 80344 | 81578 | sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
| 81579 | + sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
| 80345 | 81580 | } |
| 80346 | 81581 | |
| 80347 | 81582 | /* |
| 80348 | 81583 | ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 80349 | 81584 | ** and pTblList is the name of the table that is to be indexed. Both will |
| | @@ -80557,24 +81792,24 @@ |
| 80557 | 81792 | */ |
| 80558 | 81793 | nName = sqlite3Strlen30(zName); |
| 80559 | 81794 | nCol = pList->nExpr; |
| 80560 | 81795 | pIndex = sqlite3DbMallocZero(db, |
| 80561 | 81796 | sizeof(Index) + /* Index structure */ |
| 80562 | | - sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */ |
| 80563 | 81797 | sizeof(int)*nCol + /* Index.aiColumn */ |
| 81798 | + sizeof(int)*(nCol+1) + /* Index.aiRowEst */ |
| 80564 | 81799 | sizeof(char *)*nCol + /* Index.azColl */ |
| 80565 | 81800 | sizeof(u8)*nCol + /* Index.aSortOrder */ |
| 80566 | 81801 | nName + 1 + /* Index.zName */ |
| 80567 | 81802 | nExtra /* Collation sequence names */ |
| 80568 | 81803 | ); |
| 80569 | 81804 | if( db->mallocFailed ){ |
| 80570 | 81805 | goto exit_create_index; |
| 80571 | 81806 | } |
| 80572 | | - pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]); |
| 80573 | | - pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]); |
| 81807 | + pIndex->azColl = (char**)(&pIndex[1]); |
| 80574 | 81808 | pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]); |
| 80575 | | - pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]); |
| 81809 | + pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]); |
| 81810 | + pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]); |
| 80576 | 81811 | pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]); |
| 80577 | 81812 | zExtra = (char *)(&pIndex->zName[nName+1]); |
| 80578 | 81813 | memcpy(pIndex->zName, zName, nName+1); |
| 80579 | 81814 | pIndex->pTable = pTab; |
| 80580 | 81815 | pIndex->nColumn = pList->nExpr; |
| | @@ -80847,13 +82082,13 @@ |
| 80847 | 82082 | ** Apart from that, we have little to go on besides intuition as to |
| 80848 | 82083 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 80849 | 82084 | ** are based on typical values found in actual indices. |
| 80850 | 82085 | */ |
| 80851 | 82086 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ |
| 80852 | | - tRowcnt *a = pIdx->aiRowEst; |
| 82087 | + unsigned *a = pIdx->aiRowEst; |
| 80853 | 82088 | int i; |
| 80854 | | - tRowcnt n; |
| 82089 | + unsigned n; |
| 80855 | 82090 | assert( a!=0 ); |
| 80856 | 82091 | a[0] = pIdx->pTable->nRowEst; |
| 80857 | 82092 | if( a[0]<10 ) a[0] = 10; |
| 80858 | 82093 | n = 10; |
| 80859 | 82094 | for(i=1; i<=pIdx->nColumn; i++){ |
| | @@ -81293,12 +82528,13 @@ |
| 81293 | 82528 | ** The operator is "natural cross join". The A and B operands are stored |
| 81294 | 82529 | ** in p->a[0] and p->a[1], respectively. The parser initially stores the |
| 81295 | 82530 | ** operator with A. This routine shifts that operator over to B. |
| 81296 | 82531 | */ |
| 81297 | 82532 | SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){ |
| 81298 | | - if( p && p->a ){ |
| 82533 | + if( p ){ |
| 81299 | 82534 | int i; |
| 82535 | + assert( p->a || p->nSrc==0 ); |
| 81300 | 82536 | for(i=p->nSrc-1; i>0; i--){ |
| 81301 | 82537 | p->a[i].jointype = p->a[i-1].jointype; |
| 81302 | 82538 | } |
| 81303 | 82539 | p->a[0].jointype = 0; |
| 81304 | 82540 | } |
| | @@ -82850,10 +84086,12 @@ |
| 82850 | 84086 | ** |
| 82851 | 84087 | ** There is only one exported symbol in this file - the function |
| 82852 | 84088 | ** sqliteRegisterBuildinFunctions() found at the bottom of the file. |
| 82853 | 84089 | ** All other code has file scope. |
| 82854 | 84090 | */ |
| 84091 | +/* #include <stdlib.h> */ |
| 84092 | +/* #include <assert.h> */ |
| 82855 | 84093 | |
| 82856 | 84094 | /* |
| 82857 | 84095 | ** Return the collating function associated with a function. |
| 82858 | 84096 | */ |
| 82859 | 84097 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| | @@ -85173,11 +86411,28 @@ |
| 85173 | 86411 | pTo = sqlite3FindTable(db, pFKey->zTo, zDb); |
| 85174 | 86412 | }else{ |
| 85175 | 86413 | pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb); |
| 85176 | 86414 | } |
| 85177 | 86415 | if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){ |
| 86416 | + assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) ); |
| 85178 | 86417 | if( !isIgnoreErrors || db->mallocFailed ) return; |
| 86418 | + if( pTo==0 ){ |
| 86419 | + /* If isIgnoreErrors is true, then a table is being dropped. In this |
| 86420 | + ** case SQLite runs a "DELETE FROM xxx" on the table being dropped |
| 86421 | + ** before actually dropping it in order to check FK constraints. |
| 86422 | + ** If the parent table of an FK constraint on the current table is |
| 86423 | + ** missing, behave as if it is empty. i.e. decrement the relevant |
| 86424 | + ** FK counter for each row of the current table with non-NULL keys. |
| 86425 | + */ |
| 86426 | + Vdbe *v = sqlite3GetVdbe(pParse); |
| 86427 | + int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1; |
| 86428 | + for(i=0; i<pFKey->nCol; i++){ |
| 86429 | + int iReg = pFKey->aCol[i].iFrom + regOld + 1; |
| 86430 | + sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); |
| 86431 | + } |
| 86432 | + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1); |
| 86433 | + } |
| 85179 | 86434 | continue; |
| 85180 | 86435 | } |
| 85181 | 86436 | assert( pFKey->nCol==1 || (aiFree && pIdx) ); |
| 85182 | 86437 | |
| 85183 | 86438 | if( aiFree ){ |
| | @@ -88081,10 +89336,11 @@ |
| 88081 | 89336 | |
| 88082 | 89337 | #endif /* _SQLITE3EXT_H_ */ |
| 88083 | 89338 | |
| 88084 | 89339 | /************** End of sqlite3ext.h ******************************************/ |
| 88085 | 89340 | /************** Continuing where we left off in loadext.c ********************/ |
| 89341 | +/* #include <string.h> */ |
| 88086 | 89342 | |
| 88087 | 89343 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 88088 | 89344 | |
| 88089 | 89345 | /* |
| 88090 | 89346 | ** Some API routines are omitted when various features are |
| | @@ -91527,16 +92783,22 @@ |
| 91527 | 92783 | ){ |
| 91528 | 92784 | Vdbe *v = pParse->pVdbe; |
| 91529 | 92785 | int nExpr = pOrderBy->nExpr; |
| 91530 | 92786 | int regBase = sqlite3GetTempRange(pParse, nExpr+2); |
| 91531 | 92787 | int regRecord = sqlite3GetTempReg(pParse); |
| 92788 | + int op; |
| 91532 | 92789 | sqlite3ExprCacheClear(pParse); |
| 91533 | 92790 | sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0); |
| 91534 | 92791 | sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr); |
| 91535 | 92792 | sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1); |
| 91536 | 92793 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord); |
| 91537 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord); |
| 92794 | + if( pSelect->selFlags & SF_UseSorter ){ |
| 92795 | + op = OP_SorterInsert; |
| 92796 | + }else{ |
| 92797 | + op = OP_IdxInsert; |
| 92798 | + } |
| 92799 | + sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord); |
| 91538 | 92800 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 91539 | 92801 | sqlite3ReleaseTempRange(pParse, regBase, nExpr+2); |
| 91540 | 92802 | if( pSelect->iLimit ){ |
| 91541 | 92803 | int addr1, addr2; |
| 91542 | 92804 | int iLimit; |
| | @@ -92001,13 +93263,24 @@ |
| 92001 | 93263 | sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn); |
| 92002 | 93264 | regRowid = 0; |
| 92003 | 93265 | }else{ |
| 92004 | 93266 | regRowid = sqlite3GetTempReg(pParse); |
| 92005 | 93267 | } |
| 92006 | | - addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); |
| 92007 | | - codeOffset(v, p, addrContinue); |
| 92008 | | - sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow); |
| 93268 | + if( p->selFlags & SF_UseSorter ){ |
| 93269 | + int regSortOut = ++pParse->nMem; |
| 93270 | + int ptab2 = pParse->nTab++; |
| 93271 | + sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2); |
| 93272 | + addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); |
| 93273 | + codeOffset(v, p, addrContinue); |
| 93274 | + sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut); |
| 93275 | + sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow); |
| 93276 | + sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE); |
| 93277 | + }else{ |
| 93278 | + addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); |
| 93279 | + codeOffset(v, p, addrContinue); |
| 93280 | + sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow); |
| 93281 | + } |
| 92009 | 93282 | switch( eDest ){ |
| 92010 | 93283 | case SRT_Table: |
| 92011 | 93284 | case SRT_EphemTab: { |
| 92012 | 93285 | testcase( eDest==SRT_Table ); |
| 92013 | 93286 | testcase( eDest==SRT_EphemTab ); |
| | @@ -92056,11 +93329,15 @@ |
| 92056 | 93329 | sqlite3ReleaseTempReg(pParse, regRowid); |
| 92057 | 93330 | |
| 92058 | 93331 | /* The bottom of the loop |
| 92059 | 93332 | */ |
| 92060 | 93333 | sqlite3VdbeResolveLabel(v, addrContinue); |
| 92061 | | - sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); |
| 93334 | + if( p->selFlags & SF_UseSorter ){ |
| 93335 | + sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); |
| 93336 | + }else{ |
| 93337 | + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); |
| 93338 | + } |
| 92062 | 93339 | sqlite3VdbeResolveLabel(v, addrBreak); |
| 92063 | 93340 | if( eDest==SRT_Output || eDest==SRT_Coroutine ){ |
| 92064 | 93341 | sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0); |
| 92065 | 93342 | } |
| 92066 | 93343 | } |
| | @@ -95022,10 +96299,14 @@ |
| 95022 | 96299 | /* Set the limiter. |
| 95023 | 96300 | */ |
| 95024 | 96301 | iEnd = sqlite3VdbeMakeLabel(v); |
| 95025 | 96302 | p->nSelectRow = (double)LARGEST_INT64; |
| 95026 | 96303 | computeLimitRegisters(pParse, p, iEnd); |
| 96304 | + if( p->iLimit==0 && addrSortIndex>=0 ){ |
| 96305 | + sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen; |
| 96306 | + p->selFlags |= SF_UseSorter; |
| 96307 | + } |
| 95027 | 96308 | |
| 95028 | 96309 | /* Open a virtual index to use for the distinct set. |
| 95029 | 96310 | */ |
| 95030 | 96311 | if( p->selFlags & SF_Distinct ){ |
| 95031 | 96312 | KeyInfo *pKeyInfo; |
| | @@ -95057,11 +96338,11 @@ |
| 95057 | 96338 | } |
| 95058 | 96339 | |
| 95059 | 96340 | if( pWInfo->eDistinct ){ |
| 95060 | 96341 | VdbeOp *pOp; /* No longer required OpenEphemeral instr. */ |
| 95061 | 96342 | |
| 95062 | | - assert( addrDistinctIndex>0 ); |
| 96343 | + assert( addrDistinctIndex>=0 ); |
| 95063 | 96344 | pOp = sqlite3VdbeGetOp(v, addrDistinctIndex); |
| 95064 | 96345 | |
| 95065 | 96346 | assert( isDistinct ); |
| 95066 | 96347 | assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED |
| 95067 | 96348 | || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE |
| | @@ -95116,10 +96397,12 @@ |
| 95116 | 96397 | ** one row of the input to the aggregator has been |
| 95117 | 96398 | ** processed */ |
| 95118 | 96399 | int iAbortFlag; /* Mem address which causes query abort if positive */ |
| 95119 | 96400 | int groupBySort; /* Rows come from source in GROUP BY order */ |
| 95120 | 96401 | int addrEnd; /* End of processing for this SELECT */ |
| 96402 | + int sortPTab = 0; /* Pseudotable used to decode sorting results */ |
| 96403 | + int sortOut = 0; /* Output register from the sorter */ |
| 95121 | 96404 | |
| 95122 | 96405 | /* Remove any and all aliases between the result set and the |
| 95123 | 96406 | ** GROUP BY clause. |
| 95124 | 96407 | */ |
| 95125 | 96408 | if( pGroupBy ){ |
| | @@ -95177,16 +96460,16 @@ |
| 95177 | 96460 | int addrReset; /* Subroutine for resetting the accumulator */ |
| 95178 | 96461 | int regReset; /* Return address register for reset subroutine */ |
| 95179 | 96462 | |
| 95180 | 96463 | /* If there is a GROUP BY clause we might need a sorting index to |
| 95181 | 96464 | ** implement it. Allocate that sorting index now. If it turns out |
| 95182 | | - ** that we do not need it after all, the OpenEphemeral instruction |
| 96465 | + ** that we do not need it after all, the OP_SorterOpen instruction |
| 95183 | 96466 | ** will be converted into a Noop. |
| 95184 | 96467 | */ |
| 95185 | 96468 | sAggInfo.sortingIdx = pParse->nTab++; |
| 95186 | 96469 | pKeyInfo = keyInfoFromExprList(pParse, pGroupBy); |
| 95187 | | - addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, |
| 96470 | + addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, |
| 95188 | 96471 | sAggInfo.sortingIdx, sAggInfo.nSortingColumn, |
| 95189 | 96472 | 0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF); |
| 95190 | 96473 | |
| 95191 | 96474 | /* Initialize memory locations used by GROUP BY aggregate processing |
| 95192 | 96475 | */ |
| | @@ -95263,15 +96546,18 @@ |
| 95263 | 96546 | j++; |
| 95264 | 96547 | } |
| 95265 | 96548 | } |
| 95266 | 96549 | regRecord = sqlite3GetTempReg(pParse); |
| 95267 | 96550 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); |
| 95268 | | - sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord); |
| 96551 | + sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord); |
| 95269 | 96552 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 95270 | 96553 | sqlite3ReleaseTempRange(pParse, regBase, nCol); |
| 95271 | 96554 | sqlite3WhereEnd(pWInfo); |
| 95272 | | - sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd); |
| 96555 | + sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++; |
| 96556 | + sortOut = sqlite3GetTempReg(pParse); |
| 96557 | + sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol); |
| 96558 | + sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd); |
| 95273 | 96559 | VdbeComment((v, "GROUP BY sort")); |
| 95274 | 96560 | sAggInfo.useSortingIdx = 1; |
| 95275 | 96561 | sqlite3ExprCacheClear(pParse); |
| 95276 | 96562 | } |
| 95277 | 96563 | |
| | @@ -95280,13 +96566,17 @@ |
| 95280 | 96566 | ** Then compare the current GROUP BY terms against the GROUP BY terms |
| 95281 | 96567 | ** from the previous row currently stored in a0, a1, a2... |
| 95282 | 96568 | */ |
| 95283 | 96569 | addrTopOfLoop = sqlite3VdbeCurrentAddr(v); |
| 95284 | 96570 | sqlite3ExprCacheClear(pParse); |
| 96571 | + if( groupBySort ){ |
| 96572 | + sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut); |
| 96573 | + } |
| 95285 | 96574 | for(j=0; j<pGroupBy->nExpr; j++){ |
| 95286 | 96575 | if( groupBySort ){ |
| 95287 | | - sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j); |
| 96576 | + sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j); |
| 96577 | + if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE); |
| 95288 | 96578 | }else{ |
| 95289 | 96579 | sAggInfo.directMode = 1; |
| 95290 | 96580 | sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j); |
| 95291 | 96581 | } |
| 95292 | 96582 | } |
| | @@ -95321,11 +96611,11 @@ |
| 95321 | 96611 | VdbeComment((v, "indicate data in accumulator")); |
| 95322 | 96612 | |
| 95323 | 96613 | /* End of the loop |
| 95324 | 96614 | */ |
| 95325 | 96615 | if( groupBySort ){ |
| 95326 | | - sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop); |
| 96616 | + sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop); |
| 95327 | 96617 | }else{ |
| 95328 | 96618 | sqlite3WhereEnd(pWInfo); |
| 95329 | 96619 | sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1); |
| 95330 | 96620 | } |
| 95331 | 96621 | |
| | @@ -95650,10 +96940,12 @@ |
| 95650 | 96940 | ** interface routine of sqlite3_exec(). |
| 95651 | 96941 | ** |
| 95652 | 96942 | ** These routines are in a separate files so that they will not be linked |
| 95653 | 96943 | ** if they are not used. |
| 95654 | 96944 | */ |
| 96945 | +/* #include <stdlib.h> */ |
| 96946 | +/* #include <string.h> */ |
| 95655 | 96947 | |
| 95656 | 96948 | #ifndef SQLITE_OMIT_GET_TABLE |
| 95657 | 96949 | |
| 95658 | 96950 | /* |
| 95659 | 96951 | ** This structure is used to pass data from sqlite3_get_table() through |
| | @@ -99159,11 +100451,11 @@ |
| 99159 | 100451 | #define TERM_CODED 0x04 /* This term is already coded */ |
| 99160 | 100452 | #define TERM_COPIED 0x08 /* Has a child */ |
| 99161 | 100453 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 99162 | 100454 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 99163 | 100455 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
| 99164 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 100456 | +#ifdef SQLITE_ENABLE_STAT2 |
| 99165 | 100457 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 99166 | 100458 | #else |
| 99167 | 100459 | # define TERM_VNULL 0x00 /* Disabled if not using stat2 */ |
| 99168 | 100460 | #endif |
| 99169 | 100461 | |
| | @@ -100373,11 +101665,11 @@ |
| 100373 | 101665 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 100374 | 101666 | } |
| 100375 | 101667 | } |
| 100376 | 101668 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 100377 | 101669 | |
| 100378 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 101670 | +#ifdef SQLITE_ENABLE_STAT2 |
| 100379 | 101671 | /* When sqlite_stat2 histogram data is available an operator of the |
| 100380 | 101672 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 100381 | 101673 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 100382 | 101674 | ** virtual term of that form. |
| 100383 | 101675 | ** |
| | @@ -100412,11 +101704,11 @@ |
| 100412 | 101704 | pTerm->nChild = 1; |
| 100413 | 101705 | pTerm->wtFlags |= TERM_COPIED; |
| 100414 | 101706 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 100415 | 101707 | } |
| 100416 | 101708 | } |
| 100417 | | -#endif /* SQLITE_ENABLE_STAT */ |
| 101709 | +#endif /* SQLITE_ENABLE_STAT2 */ |
| 100418 | 101710 | |
| 100419 | 101711 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 100420 | 101712 | ** an index for tables to the left of the join. |
| 100421 | 101713 | */ |
| 100422 | 101714 | pTerm->prereqRight |= extraRight; |
| | @@ -101461,89 +102753,71 @@ |
| 101461 | 102753 | */ |
| 101462 | 102754 | bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost); |
| 101463 | 102755 | } |
| 101464 | 102756 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 101465 | 102757 | |
| 101466 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 101467 | 102758 | /* |
| 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( |
| 102759 | +** Argument pIdx is a pointer to an index structure that has an array of |
| 102760 | +** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column |
| 102761 | +** stored in Index.aSample. These samples divide the domain of values stored |
| 102762 | +** the index into (SQLITE_INDEX_SAMPLES+1) regions. |
| 102763 | +** Region 0 contains all values less than the first sample value. Region |
| 102764 | +** 1 contains values between the first and second samples. Region 2 contains |
| 102765 | +** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES |
| 102766 | +** contains values larger than the last sample. |
| 102767 | +** |
| 102768 | +** If the index contains many duplicates of a single value, then it is |
| 102769 | +** possible that two or more adjacent samples can hold the same value. |
| 102770 | +** When that is the case, the smallest possible region code is returned |
| 102771 | +** when roundUp is false and the largest possible region code is returned |
| 102772 | +** when roundUp is true. |
| 102773 | +** |
| 102774 | +** If successful, this function determines which of the regions value |
| 102775 | +** pVal lies in, sets *piRegion to the region index (a value between 0 |
| 102776 | +** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK. |
| 102777 | +** Or, if an OOM occurs while converting text values between encodings, |
| 102778 | +** SQLITE_NOMEM is returned and *piRegion is undefined. |
| 102779 | +*/ |
| 102780 | +#ifdef SQLITE_ENABLE_STAT2 |
| 102781 | +static int whereRangeRegion( |
| 101477 | 102782 | Parse *pParse, /* Database connection */ |
| 101478 | 102783 | Index *pIdx, /* Index to consider domain of */ |
| 101479 | 102784 | sqlite3_value *pVal, /* Value to consider */ |
| 101480 | | - int roundUp, /* Round up if true. Round down if false */ |
| 101481 | | - tRowcnt *aStat /* OUT: stats written here */ |
| 102785 | + int roundUp, /* Return largest valid region if true */ |
| 102786 | + int *piRegion /* OUT: Region of domain in which value lies */ |
| 101482 | 102787 | ){ |
| 101483 | | - tRowcnt n; |
| 101484 | | - IndexSample *aSample; |
| 101485 | | - int i, eType; |
| 101486 | | - int isEq = 0; |
| 101487 | | - i64 v; |
| 101488 | | - double r, rS; |
| 101489 | | - |
| 101490 | 102788 | 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 ){ |
| 102789 | + if( ALWAYS(pVal) ){ |
| 102790 | + IndexSample *aSample = pIdx->aSample; |
| 102791 | + int i = 0; |
| 102792 | + int eType = sqlite3_value_type(pVal); |
| 102793 | + |
| 102794 | + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ |
| 102795 | + double r = sqlite3_value_double(pVal); |
| 102796 | + for(i=0; i<SQLITE_INDEX_SAMPLES; i++){ |
| 102797 | + if( aSample[i].eType==SQLITE_NULL ) continue; |
| 102798 | + if( aSample[i].eType>=SQLITE_TEXT ) break; |
| 102799 | + if( roundUp ){ |
| 102800 | + if( aSample[i].u.r>r ) break; |
| 102801 | + }else{ |
| 102802 | + if( aSample[i].u.r>=r ) break; |
| 102803 | + } |
| 102804 | + } |
| 102805 | + }else if( eType==SQLITE_NULL ){ |
| 102806 | + i = 0; |
| 102807 | + if( roundUp ){ |
| 102808 | + while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++; |
| 102809 | + } |
| 102810 | + }else{ |
| 101542 | 102811 | sqlite3 *db = pParse->db; |
| 101543 | 102812 | CollSeq *pColl; |
| 101544 | 102813 | const u8 *z; |
| 102814 | + int n; |
| 102815 | + |
| 102816 | + /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */ |
| 102817 | + assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 102818 | + |
| 101545 | 102819 | if( eType==SQLITE_BLOB ){ |
| 101546 | 102820 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 101547 | 102821 | pColl = db->pDfltColl; |
| 101548 | 102822 | assert( pColl->enc==SQLITE_UTF8 ); |
| 101549 | 102823 | }else{ |
| | @@ -101558,16 +102832,16 @@ |
| 101558 | 102832 | return SQLITE_NOMEM; |
| 101559 | 102833 | } |
| 101560 | 102834 | assert( z && pColl && pColl->xCmp ); |
| 101561 | 102835 | } |
| 101562 | 102836 | n = sqlite3ValueBytes(pVal, pColl->enc); |
| 101563 | | - |
| 101564 | | - for(; i<pIdx->nSample; i++){ |
| 102837 | + |
| 102838 | + for(i=0; i<SQLITE_INDEX_SAMPLES; i++){ |
| 101565 | 102839 | int c; |
| 101566 | 102840 | int eSampletype = aSample[i].eType; |
| 101567 | | - if( eSampletype<eType ) continue; |
| 101568 | | - if( eSampletype!=eType ) break; |
| 102841 | + if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue; |
| 102842 | + if( (eSampletype!=eType) ) break; |
| 101569 | 102843 | #ifndef SQLITE_OMIT_UTF16 |
| 101570 | 102844 | if( pColl->enc!=SQLITE_UTF8 ){ |
| 101571 | 102845 | int nSample; |
| 101572 | 102846 | char *zSample = sqlite3Utf8to16( |
| 101573 | 102847 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| | @@ -101581,52 +102855,20 @@ |
| 101581 | 102855 | }else |
| 101582 | 102856 | #endif |
| 101583 | 102857 | { |
| 101584 | 102858 | c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z); |
| 101585 | 102859 | } |
| 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; |
| 102860 | + if( c-roundUp>=0 ) break; |
| 102861 | + } |
| 102862 | + } |
| 102863 | + |
| 102864 | + assert( i>=0 && i<=SQLITE_INDEX_SAMPLES ); |
| 102865 | + *piRegion = i; |
| 101624 | 102866 | } |
| 101625 | 102867 | return SQLITE_OK; |
| 101626 | 102868 | } |
| 101627 | | -#endif /* SQLITE_ENABLE_STAT3 */ |
| 102869 | +#endif /* #ifdef SQLITE_ENABLE_STAT2 */ |
| 101628 | 102870 | |
| 101629 | 102871 | /* |
| 101630 | 102872 | ** If expression pExpr represents a literal value, set *pp to point to |
| 101631 | 102873 | ** an sqlite3_value structure containing the same value, with affinity |
| 101632 | 102874 | ** aff applied to it, before returning. It is the responsibility of the |
| | @@ -101640,11 +102882,11 @@ |
| 101640 | 102882 | ** |
| 101641 | 102883 | ** If neither of the above apply, set *pp to NULL. |
| 101642 | 102884 | ** |
| 101643 | 102885 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 101644 | 102886 | */ |
| 101645 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 102887 | +#ifdef SQLITE_ENABLE_STAT2 |
| 101646 | 102888 | static int valueFromExpr( |
| 101647 | 102889 | Parse *pParse, |
| 101648 | 102890 | Expr *pExpr, |
| 101649 | 102891 | u8 aff, |
| 101650 | 102892 | sqlite3_value **pp |
| | @@ -101688,92 +102930,106 @@ |
| 101688 | 102930 | ** |
| 101689 | 102931 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 101690 | 102932 | ** |
| 101691 | 102933 | ** then nEq should be passed 0. |
| 101692 | 102934 | ** |
| 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... |
| 102935 | +** The returned value is an integer between 1 and 100, inclusive. A return |
| 102936 | +** value of 1 indicates that the proposed range scan is expected to visit |
| 102937 | +** approximately 1/100th (1%) of the rows selected by the nEq equality |
| 102938 | +** constraints (if any). A return value of 100 indicates that it is expected |
| 102939 | +** that the range scan will visit every row (100%) selected by the equality |
| 102940 | +** constraints. |
| 101697 | 102941 | ** |
| 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. |
| 102942 | +** In the absence of sqlite_stat2 ANALYZE data, each range inequality |
| 102943 | +** reduces the search space by 3/4ths. Hence a single constraint (x>?) |
| 102944 | +** results in a return of 25 and a range constraint (x>? AND x<?) results |
| 102945 | +** in a return of 6. |
| 101702 | 102946 | */ |
| 101703 | 102947 | static int whereRangeScanEst( |
| 101704 | 102948 | Parse *pParse, /* Parsing & code generating context */ |
| 101705 | 102949 | Index *p, /* The index containing the range-compared column; "x" */ |
| 101706 | 102950 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 101707 | 102951 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 101708 | 102952 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 101709 | | - double *pRangeDiv /* OUT: Reduce search space by this divisor */ |
| 102953 | + int *piEst /* OUT: Return value */ |
| 101710 | 102954 | ){ |
| 101711 | 102955 | int rc = SQLITE_OK; |
| 101712 | 102956 | |
| 101713 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 102957 | +#ifdef SQLITE_ENABLE_STAT2 |
| 101714 | 102958 | |
| 101715 | | - if( nEq==0 && p->nSample ){ |
| 101716 | | - sqlite3_value *pRangeVal; |
| 101717 | | - tRowcnt iLower = 0; |
| 101718 | | - tRowcnt iUpper = p->aiRowEst[0]; |
| 101719 | | - tRowcnt a[2]; |
| 102959 | + if( nEq==0 && p->aSample ){ |
| 102960 | + sqlite3_value *pLowerVal = 0; |
| 102961 | + sqlite3_value *pUpperVal = 0; |
| 102962 | + int iEst; |
| 102963 | + int iLower = 0; |
| 102964 | + int iUpper = SQLITE_INDEX_SAMPLES; |
| 102965 | + int roundUpUpper = 0; |
| 102966 | + int roundUpLower = 0; |
| 101720 | 102967 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 101721 | 102968 | |
| 101722 | 102969 | if( pLower ){ |
| 101723 | 102970 | Expr *pExpr = pLower->pExpr->pRight; |
| 101724 | | - rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
| 102971 | + rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal); |
| 101725 | 102972 | 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); |
| 102973 | + roundUpLower = (pLower->eOperator==WO_GT) ?1:0; |
| 101733 | 102974 | } |
| 101734 | 102975 | if( rc==SQLITE_OK && pUpper ){ |
| 101735 | 102976 | Expr *pExpr = pUpper->pExpr->pRight; |
| 101736 | | - rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
| 102977 | + rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal); |
| 101737 | 102978 | 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 | | - } |
| 102979 | + roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0; |
| 102980 | + } |
| 102981 | + |
| 102982 | + if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){ |
| 102983 | + sqlite3ValueFree(pLowerVal); |
| 102984 | + sqlite3ValueFree(pUpperVal); |
| 102985 | + goto range_est_fallback; |
| 102986 | + }else if( pLowerVal==0 ){ |
| 102987 | + rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); |
| 102988 | + if( pLower ) iLower = iUpper/2; |
| 102989 | + }else if( pUpperVal==0 ){ |
| 102990 | + rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); |
| 102991 | + if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2; |
| 102992 | + }else{ |
| 102993 | + rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); |
| 102994 | + if( rc==SQLITE_OK ){ |
| 102995 | + rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); |
| 102996 | + } |
| 102997 | + } |
| 102998 | + WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper)); |
| 102999 | + |
| 103000 | + iEst = iUpper - iLower; |
| 103001 | + testcase( iEst==SQLITE_INDEX_SAMPLES ); |
| 103002 | + assert( iEst<=SQLITE_INDEX_SAMPLES ); |
| 103003 | + if( iEst<1 ){ |
| 103004 | + *piEst = 50/SQLITE_INDEX_SAMPLES; |
| 103005 | + }else{ |
| 103006 | + *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES; |
| 103007 | + } |
| 103008 | + sqlite3ValueFree(pLowerVal); |
| 103009 | + sqlite3ValueFree(pUpperVal); |
| 103010 | + return rc; |
| 103011 | + } |
| 103012 | +range_est_fallback: |
| 101757 | 103013 | #else |
| 101758 | 103014 | UNUSED_PARAMETER(pParse); |
| 101759 | 103015 | UNUSED_PARAMETER(p); |
| 101760 | 103016 | UNUSED_PARAMETER(nEq); |
| 101761 | 103017 | #endif |
| 101762 | 103018 | assert( pLower || pUpper ); |
| 101763 | | - *pRangeDiv = (double)1; |
| 101764 | | - if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4; |
| 101765 | | - if( pUpper ) *pRangeDiv *= (double)4; |
| 103019 | + *piEst = 100; |
| 103020 | + if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4; |
| 103021 | + if( pUpper ) *piEst /= 4; |
| 101766 | 103022 | return rc; |
| 101767 | 103023 | } |
| 101768 | 103024 | |
| 101769 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 103025 | +#ifdef SQLITE_ENABLE_STAT2 |
| 101770 | 103026 | /* |
| 101771 | 103027 | ** Estimate the number of rows that will be returned based on |
| 101772 | 103028 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 101773 | 103029 | ** the histogram data. This only works when x is the left-most |
| 101774 | | -** column of an index and sqlite_stat3 histogram data is available |
| 103030 | +** column of an index and sqlite_stat2 histogram data is available |
| 101775 | 103031 | ** for that index. When pExpr==NULL that means the constraint is |
| 101776 | 103032 | ** "x IS NULL" instead of "x=VALUE". |
| 101777 | 103033 | ** |
| 101778 | 103034 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 101779 | 103035 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| | @@ -101789,13 +103045,14 @@ |
| 101789 | 103045 | Index *p, /* The index whose left-most column is pTerm */ |
| 101790 | 103046 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 101791 | 103047 | double *pnRow /* Write the revised row estimate here */ |
| 101792 | 103048 | ){ |
| 101793 | 103049 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
| 103050 | + int iLower, iUpper; /* Range of histogram regions containing pRhs */ |
| 101794 | 103051 | u8 aff; /* Column affinity */ |
| 101795 | 103052 | int rc; /* Subfunction return code */ |
| 101796 | | - tRowcnt a[2]; /* Statistics */ |
| 103053 | + double nRowEst; /* New estimate of the number of rows */ |
| 101797 | 103054 | |
| 101798 | 103055 | assert( p->aSample!=0 ); |
| 101799 | 103056 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 101800 | 103057 | if( pExpr ){ |
| 101801 | 103058 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| | @@ -101802,22 +103059,30 @@ |
| 101802 | 103059 | if( rc ) goto whereEqualScanEst_cancel; |
| 101803 | 103060 | }else{ |
| 101804 | 103061 | pRhs = sqlite3ValueNew(pParse->db); |
| 101805 | 103062 | } |
| 101806 | 103063 | 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]; |
| 103064 | + rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower); |
| 103065 | + if( rc ) goto whereEqualScanEst_cancel; |
| 103066 | + rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper); |
| 103067 | + if( rc ) goto whereEqualScanEst_cancel; |
| 103068 | + WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper)); |
| 103069 | + if( iLower>=iUpper ){ |
| 103070 | + nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2); |
| 103071 | + if( nRowEst<*pnRow ) *pnRow = nRowEst; |
| 103072 | + }else{ |
| 103073 | + nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES; |
| 103074 | + *pnRow = nRowEst; |
| 101811 | 103075 | } |
| 103076 | + |
| 101812 | 103077 | whereEqualScanEst_cancel: |
| 101813 | 103078 | sqlite3ValueFree(pRhs); |
| 101814 | 103079 | return rc; |
| 101815 | 103080 | } |
| 101816 | | -#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 103081 | +#endif /* defined(SQLITE_ENABLE_STAT2) */ |
| 101817 | 103082 | |
| 101818 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 103083 | +#ifdef SQLITE_ENABLE_STAT2 |
| 101819 | 103084 | /* |
| 101820 | 103085 | ** Estimate the number of rows that will be returned based on |
| 101821 | 103086 | ** an IN constraint where the right-hand side of the IN operator |
| 101822 | 103087 | ** is a list of values. Example: |
| 101823 | 103088 | ** |
| | @@ -101836,29 +103101,64 @@ |
| 101836 | 103101 | Parse *pParse, /* Parsing & code generating context */ |
| 101837 | 103102 | Index *p, /* The index whose left-most column is pTerm */ |
| 101838 | 103103 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 101839 | 103104 | double *pnRow /* Write the revised row estimate here */ |
| 101840 | 103105 | ){ |
| 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 */ |
| 103106 | + sqlite3_value *pVal = 0; /* One value from list */ |
| 103107 | + int iLower, iUpper; /* Range of histogram regions containing pRhs */ |
| 103108 | + u8 aff; /* Column affinity */ |
| 103109 | + int rc = SQLITE_OK; /* Subfunction return code */ |
| 103110 | + double nRowEst; /* New estimate of the number of rows */ |
| 103111 | + int nSpan = 0; /* Number of histogram regions spanned */ |
| 103112 | + int nSingle = 0; /* Histogram regions hit by a single value */ |
| 103113 | + int nNotFound = 0; /* Count of values that are not constants */ |
| 103114 | + int i; /* Loop counter */ |
| 103115 | + u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */ |
| 103116 | + u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */ |
| 101845 | 103117 | |
| 101846 | 103118 | 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; |
| 103119 | + aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
| 103120 | + memset(aSpan, 0, sizeof(aSpan)); |
| 103121 | + memset(aSingle, 0, sizeof(aSingle)); |
| 103122 | + for(i=0; i<pList->nExpr; i++){ |
| 103123 | + sqlite3ValueFree(pVal); |
| 103124 | + rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal); |
| 103125 | + if( rc ) break; |
| 103126 | + if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){ |
| 103127 | + nNotFound++; |
| 103128 | + continue; |
| 103129 | + } |
| 103130 | + rc = whereRangeRegion(pParse, p, pVal, 0, &iLower); |
| 103131 | + if( rc ) break; |
| 103132 | + rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper); |
| 103133 | + if( rc ) break; |
| 103134 | + if( iLower>=iUpper ){ |
| 103135 | + aSingle[iLower] = 1; |
| 103136 | + }else{ |
| 103137 | + assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES ); |
| 103138 | + while( iLower<iUpper ) aSpan[iLower++] = 1; |
| 103139 | + } |
| 101851 | 103140 | } |
| 101852 | 103141 | if( rc==SQLITE_OK ){ |
| 103142 | + for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){ |
| 103143 | + if( aSpan[i] ){ |
| 103144 | + nSpan++; |
| 103145 | + }else if( aSingle[i] ){ |
| 103146 | + nSingle++; |
| 103147 | + } |
| 103148 | + } |
| 103149 | + nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES) |
| 103150 | + + nNotFound*p->aiRowEst[1]; |
| 101853 | 103151 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 101854 | 103152 | *pnRow = nRowEst; |
| 101855 | | - WHERETRACE(("IN row estimate: est=%g\n", nRowEst)); |
| 103153 | + WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n", |
| 103154 | + nSpan, nSingle, nNotFound, nRowEst)); |
| 101856 | 103155 | } |
| 103156 | + sqlite3ValueFree(pVal); |
| 101857 | 103157 | return rc; |
| 101858 | 103158 | } |
| 101859 | | -#endif /* defined(SQLITE_ENABLE_STAT3) */ |
| 103159 | +#endif /* defined(SQLITE_ENABLE_STAT2) */ |
| 101860 | 103160 | |
| 101861 | 103161 | |
| 101862 | 103162 | /* |
| 101863 | 103163 | ** Find the best query plan for accessing a particular table. Write the |
| 101864 | 103164 | ** best query plan and its cost into the WhereCost object supplied as the |
| | @@ -101901,11 +103201,11 @@ |
| 101901 | 103201 | Index *pProbe; /* An index we are evaluating */ |
| 101902 | 103202 | Index *pIdx; /* Copy of pProbe, or zero for IPK index */ |
| 101903 | 103203 | int eqTermMask; /* Current mask of valid equality operators */ |
| 101904 | 103204 | int idxEqTermMask; /* Index mask of valid equality operators */ |
| 101905 | 103205 | Index sPk; /* A fake index object for the primary key */ |
| 101906 | | - tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 103206 | + unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 101907 | 103207 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 101908 | 103208 | int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */ |
| 101909 | 103209 | |
| 101910 | 103210 | /* Initialize the cost to a worst-case value */ |
| 101911 | 103211 | memset(pCost, 0, sizeof(*pCost)); |
| | @@ -101956,11 +103256,11 @@ |
| 101956 | 103256 | } |
| 101957 | 103257 | |
| 101958 | 103258 | /* Loop over all indices looking for the best one to use |
| 101959 | 103259 | */ |
| 101960 | 103260 | for(; pProbe; pIdx=pProbe=pProbe->pNext){ |
| 101961 | | - const tRowcnt * const aiRowEst = pProbe->aiRowEst; |
| 103261 | + const unsigned int * const aiRowEst = pProbe->aiRowEst; |
| 101962 | 103262 | double cost; /* Cost of using pProbe */ |
| 101963 | 103263 | double nRow; /* Estimated number of rows in result set */ |
| 101964 | 103264 | double log10N; /* base-10 logarithm of nRow (inexact) */ |
| 101965 | 103265 | int rev; /* True to scan in reverse order */ |
| 101966 | 103266 | int wsFlags = 0; |
| | @@ -101999,16 +103299,18 @@ |
| 101999 | 103299 | ** Set to true if there was at least one "x IN (SELECT ...)" term used |
| 102000 | 103300 | ** in determining the value of nInMul. Note that the RHS of the |
| 102001 | 103301 | ** IN operator must be a SELECT, not a value list, for this variable |
| 102002 | 103302 | ** to be true. |
| 102003 | 103303 | ** |
| 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). |
| 103304 | + ** estBound: |
| 103305 | + ** An estimate on the amount of the table that must be searched. A |
| 103306 | + ** value of 100 means the entire table is searched. Range constraints |
| 103307 | + ** might reduce this to a value less than 100 to indicate that only |
| 103308 | + ** a fraction of the table needs searching. In the absence of |
| 103309 | + ** sqlite_stat2 ANALYZE data, a single inequality reduces the search |
| 103310 | + ** space to 1/4rd its original size. So an x>? constraint reduces |
| 103311 | + ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6. |
| 102010 | 103312 | ** |
| 102011 | 103313 | ** bSort: |
| 102012 | 103314 | ** Boolean. True if there is an ORDER BY clause that will require an |
| 102013 | 103315 | ** external sort (i.e. scanning the index being evaluated will not |
| 102014 | 103316 | ** correctly order records). |
| | @@ -102029,17 +103331,17 @@ |
| 102029 | 103331 | ** SELECT a, b, c FROM tbl WHERE a = 1; |
| 102030 | 103332 | */ |
| 102031 | 103333 | int nEq; /* Number of == or IN terms matching index */ |
| 102032 | 103334 | int bInEst = 0; /* True if "x IN (SELECT...)" seen */ |
| 102033 | 103335 | int nInMul = 1; /* Number of distinct equalities to lookup */ |
| 102034 | | - double rangeDiv = (double)1; /* Estimated reduction in search space */ |
| 103336 | + int estBound = 100; /* Estimated reduction in search space */ |
| 102035 | 103337 | int nBound = 0; /* Number of range constraints seen */ |
| 102036 | 103338 | int bSort = !!pOrderBy; /* True if external sort required */ |
| 102037 | 103339 | int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */ |
| 102038 | 103340 | int bLookup = 0; /* True if not a covering index */ |
| 102039 | 103341 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 102040 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 103342 | +#ifdef SQLITE_ENABLE_STAT2 |
| 102041 | 103343 | WhereTerm *pFirstTerm = 0; /* First term matching the index */ |
| 102042 | 103344 | #endif |
| 102043 | 103345 | |
| 102044 | 103346 | /* Determine the values of nEq and nInMul */ |
| 102045 | 103347 | for(nEq=0; nEq<pProbe->nColumn; nEq++){ |
| | @@ -102059,23 +103361,23 @@ |
| 102059 | 103361 | nInMul *= pExpr->x.pList->nExpr; |
| 102060 | 103362 | } |
| 102061 | 103363 | }else if( pTerm->eOperator & WO_ISNULL ){ |
| 102062 | 103364 | wsFlags |= WHERE_COLUMN_NULL; |
| 102063 | 103365 | } |
| 102064 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 103366 | +#ifdef SQLITE_ENABLE_STAT2 |
| 102065 | 103367 | if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm; |
| 102066 | 103368 | #endif |
| 102067 | 103369 | used |= pTerm->prereqRight; |
| 102068 | 103370 | } |
| 102069 | 103371 | |
| 102070 | | - /* Determine the value of rangeDiv */ |
| 103372 | + /* Determine the value of estBound. */ |
| 102071 | 103373 | if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){ |
| 102072 | 103374 | int j = pProbe->aiColumn[nEq]; |
| 102073 | 103375 | if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){ |
| 102074 | 103376 | WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx); |
| 102075 | 103377 | WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx); |
| 102076 | | - whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv); |
| 103378 | + whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound); |
| 102077 | 103379 | if( pTop ){ |
| 102078 | 103380 | nBound = 1; |
| 102079 | 103381 | wsFlags |= WHERE_TOP_LIMIT; |
| 102080 | 103382 | used |= pTop->prereqRight; |
| 102081 | 103383 | } |
| | @@ -102143,11 +103445,11 @@ |
| 102143 | 103445 | if( bInEst && nRow*2>aiRowEst[0] ){ |
| 102144 | 103446 | nRow = aiRowEst[0]/2; |
| 102145 | 103447 | nInMul = (int)(nRow / aiRowEst[nEq]); |
| 102146 | 103448 | } |
| 102147 | 103449 | |
| 102148 | | -#ifdef SQLITE_ENABLE_STAT3 |
| 103450 | +#ifdef SQLITE_ENABLE_STAT2 |
| 102149 | 103451 | /* If the constraint is of the form x=VALUE or x IN (E1,E2,...) |
| 102150 | 103452 | ** and we do not think that values of x are unique and if histogram |
| 102151 | 103453 | ** data is available for column x, then it might be possible |
| 102152 | 103454 | ** to get a better estimate on the number of rows based on |
| 102153 | 103455 | ** VALUE and how common that value is according to the histogram. |
| | @@ -102159,16 +103461,16 @@ |
| 102159 | 103461 | whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow); |
| 102160 | 103462 | }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){ |
| 102161 | 103463 | whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow); |
| 102162 | 103464 | } |
| 102163 | 103465 | } |
| 102164 | | -#endif /* SQLITE_ENABLE_STAT3 */ |
| 103466 | +#endif /* SQLITE_ENABLE_STAT2 */ |
| 102165 | 103467 | |
| 102166 | 103468 | /* Adjust the number of output rows and downward to reflect rows |
| 102167 | 103469 | ** that are excluded by range constraints. |
| 102168 | 103470 | */ |
| 102169 | | - nRow = nRow/rangeDiv; |
| 103471 | + nRow = (nRow * (double)estBound) / (double)100; |
| 102170 | 103472 | if( nRow<1 ) nRow = 1; |
| 102171 | 103473 | |
| 102172 | 103474 | /* Experiments run on real SQLite databases show that the time needed |
| 102173 | 103475 | ** to do a binary search to locate a row in a table or index is roughly |
| 102174 | 103476 | ** log10(N) times the time to move from one row to the next row within |
| | @@ -102293,14 +103595,14 @@ |
| 102293 | 103595 | if( nRow<2 ) nRow = 2; |
| 102294 | 103596 | } |
| 102295 | 103597 | |
| 102296 | 103598 | |
| 102297 | 103599 | WHERETRACE(( |
| 102298 | | - "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 103600 | + "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 102299 | 103601 | " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n", |
| 102300 | 103602 | pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), |
| 102301 | | - nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags, |
| 103603 | + nEq, nInMul, estBound, bSort, bLookup, wsFlags, |
| 102302 | 103604 | notReady, log10N, nRow, cost, used |
| 102303 | 103605 | )); |
| 102304 | 103606 | |
| 102305 | 103607 | /* If this index is the best we have seen so far, then record this |
| 102306 | 103608 | ** index and its cost in the pCost structure. |
| | @@ -104227,10 +105529,11 @@ |
| 104227 | 105529 | ** LALR(1) grammar but which are always false in the |
| 104228 | 105530 | ** specific grammar used by SQLite. |
| 104229 | 105531 | */ |
| 104230 | 105532 | /* First off, code is included that follows the "include" declaration |
| 104231 | 105533 | ** in the input grammar file. */ |
| 105534 | +/* #include <stdio.h> */ |
| 104232 | 105535 | |
| 104233 | 105536 | |
| 104234 | 105537 | /* |
| 104235 | 105538 | ** Disable all error recovery processing in the parser push-down |
| 104236 | 105539 | ** automaton. |
| | @@ -105087,10 +106390,11 @@ |
| 105087 | 106390 | #endif |
| 105088 | 106391 | }; |
| 105089 | 106392 | typedef struct yyParser yyParser; |
| 105090 | 106393 | |
| 105091 | 106394 | #ifndef NDEBUG |
| 106395 | +/* #include <stdio.h> */ |
| 105092 | 106396 | static FILE *yyTraceFILE = 0; |
| 105093 | 106397 | static char *yyTracePrompt = 0; |
| 105094 | 106398 | #endif /* NDEBUG */ |
| 105095 | 106399 | |
| 105096 | 106400 | #ifndef NDEBUG |
| | @@ -107662,10 +108966,11 @@ |
| 107662 | 108966 | ** |
| 107663 | 108967 | ** This file contains C code that splits an SQL input string up into |
| 107664 | 108968 | ** individual tokens and sends those tokens one-by-one over to the |
| 107665 | 108969 | ** parser for analysis. |
| 107666 | 108970 | */ |
| 108971 | +/* #include <stdlib.h> */ |
| 107667 | 108972 | |
| 107668 | 108973 | /* |
| 107669 | 108974 | ** The charMap() macro maps alphabetic characters into their |
| 107670 | 108975 | ** lower-case ASCII equivalent. On ASCII machines, this is just |
| 107671 | 108976 | ** an upper-to-lower case map. On EBCDIC machines we also need |
| | @@ -109053,10 +110358,20 @@ |
| 109053 | 110358 | memcpy(&y, &x, 8); |
| 109054 | 110359 | assert( sqlite3IsNaN(y) ); |
| 109055 | 110360 | } |
| 109056 | 110361 | #endif |
| 109057 | 110362 | #endif |
| 110363 | + |
| 110364 | + /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT |
| 110365 | + ** compile-time option. |
| 110366 | + */ |
| 110367 | +#ifdef SQLITE_EXTRA_INIT |
| 110368 | + if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){ |
| 110369 | + int SQLITE_EXTRA_INIT(void); |
| 110370 | + rc = SQLITE_EXTRA_INIT(); |
| 110371 | + } |
| 110372 | +#endif |
| 109058 | 110373 | |
| 109059 | 110374 | return rc; |
| 109060 | 110375 | } |
| 109061 | 110376 | |
| 109062 | 110377 | /* |
| | @@ -113184,10 +114499,16 @@ |
| 113184 | 114499 | |
| 113185 | 114500 | #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE) |
| 113186 | 114501 | # define SQLITE_CORE 1 |
| 113187 | 114502 | #endif |
| 113188 | 114503 | |
| 114504 | +/* #include <assert.h> */ |
| 114505 | +/* #include <stdlib.h> */ |
| 114506 | +/* #include <stddef.h> */ |
| 114507 | +/* #include <stdio.h> */ |
| 114508 | +/* #include <string.h> */ |
| 114509 | +/* #include <stdarg.h> */ |
| 113189 | 114510 | |
| 113190 | 114511 | #ifndef SQLITE_CORE |
| 113191 | 114512 | SQLITE_EXTENSION_INIT1 |
| 113192 | 114513 | #endif |
| 113193 | 114514 | |
| | @@ -117705,10 +119026,12 @@ |
| 117705 | 119026 | ****************************************************************************** |
| 117706 | 119027 | ** |
| 117707 | 119028 | */ |
| 117708 | 119029 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 117709 | 119030 | |
| 119031 | +/* #include <string.h> */ |
| 119032 | +/* #include <assert.h> */ |
| 117710 | 119033 | |
| 117711 | 119034 | typedef struct Fts3auxTable Fts3auxTable; |
| 117712 | 119035 | typedef struct Fts3auxCursor Fts3auxCursor; |
| 117713 | 119036 | |
| 117714 | 119037 | struct Fts3auxTable { |
| | @@ -118243,10 +119566,12 @@ |
| 118243 | 119566 | /* |
| 118244 | 119567 | ** Default span for NEAR operators. |
| 118245 | 119568 | */ |
| 118246 | 119569 | #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10 |
| 118247 | 119570 | |
| 119571 | +/* #include <string.h> */ |
| 119572 | +/* #include <assert.h> */ |
| 118248 | 119573 | |
| 118249 | 119574 | /* |
| 118250 | 119575 | ** isNot: |
| 118251 | 119576 | ** This variable is used by function getNextNode(). When getNextNode() is |
| 118252 | 119577 | ** called, it sets ParseContext.isNot to true if the 'next node' is a |
| | @@ -118944,10 +120269,11 @@ |
| 118944 | 120269 | ** Everything after this point is just test code. |
| 118945 | 120270 | */ |
| 118946 | 120271 | |
| 118947 | 120272 | #ifdef SQLITE_TEST |
| 118948 | 120273 | |
| 120274 | +/* #include <stdio.h> */ |
| 118949 | 120275 | |
| 118950 | 120276 | /* |
| 118951 | 120277 | ** Function to query the hash-table of tokenizers (see README.tokenizers). |
| 118952 | 120278 | */ |
| 118953 | 120279 | static int queryTestTokenizer( |
| | @@ -119154,10 +120480,13 @@ |
| 119154 | 120480 | ** * The FTS3 module is being built into the core of |
| 119155 | 120481 | ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
| 119156 | 120482 | */ |
| 119157 | 120483 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 119158 | 120484 | |
| 120485 | +/* #include <assert.h> */ |
| 120486 | +/* #include <stdlib.h> */ |
| 120487 | +/* #include <string.h> */ |
| 119159 | 120488 | |
| 119160 | 120489 | |
| 119161 | 120490 | /* |
| 119162 | 120491 | ** Malloc and Free functions |
| 119163 | 120492 | */ |
| | @@ -119534,10 +120863,14 @@ |
| 119534 | 120863 | ** * The FTS3 module is being built into the core of |
| 119535 | 120864 | ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
| 119536 | 120865 | */ |
| 119537 | 120866 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 119538 | 120867 | |
| 120868 | +/* #include <assert.h> */ |
| 120869 | +/* #include <stdlib.h> */ |
| 120870 | +/* #include <stdio.h> */ |
| 120871 | +/* #include <string.h> */ |
| 119539 | 120872 | |
| 119540 | 120873 | |
| 119541 | 120874 | /* |
| 119542 | 120875 | ** Class derived from sqlite3_tokenizer |
| 119543 | 120876 | */ |
| | @@ -120177,10 +121510,12 @@ |
| 120177 | 121510 | ** * The FTS3 module is being built into the core of |
| 120178 | 121511 | ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
| 120179 | 121512 | */ |
| 120180 | 121513 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 120181 | 121514 | |
| 121515 | +/* #include <assert.h> */ |
| 121516 | +/* #include <string.h> */ |
| 120182 | 121517 | |
| 120183 | 121518 | /* |
| 120184 | 121519 | ** Implementation of the SQL scalar function for accessing the underlying |
| 120185 | 121520 | ** hash table. This function may be called as follows: |
| 120186 | 121521 | ** |
| | @@ -120352,10 +121687,12 @@ |
| 120352 | 121687 | } |
| 120353 | 121688 | |
| 120354 | 121689 | |
| 120355 | 121690 | #ifdef SQLITE_TEST |
| 120356 | 121691 | |
| 121692 | +/* #include <tcl.h> */ |
| 121693 | +/* #include <string.h> */ |
| 120357 | 121694 | |
| 120358 | 121695 | /* |
| 120359 | 121696 | ** Implementation of a special SQL scalar function for testing tokenizers |
| 120360 | 121697 | ** designed to be used in concert with the Tcl testing framework. This |
| 120361 | 121698 | ** function must be called with two arguments: |
| | @@ -120663,10 +122000,14 @@ |
| 120663 | 122000 | ** * The FTS3 module is being built into the core of |
| 120664 | 122001 | ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). |
| 120665 | 122002 | */ |
| 120666 | 122003 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 120667 | 122004 | |
| 122005 | +/* #include <assert.h> */ |
| 122006 | +/* #include <stdlib.h> */ |
| 122007 | +/* #include <stdio.h> */ |
| 122008 | +/* #include <string.h> */ |
| 120668 | 122009 | |
| 120669 | 122010 | |
| 120670 | 122011 | typedef struct simple_tokenizer { |
| 120671 | 122012 | sqlite3_tokenizer base; |
| 120672 | 122013 | char delim[128]; /* flag ASCII delimiters */ |
| | @@ -120888,10 +122229,13 @@ |
| 120888 | 122229 | ** code in fts3.c. |
| 120889 | 122230 | */ |
| 120890 | 122231 | |
| 120891 | 122232 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 120892 | 122233 | |
| 122234 | +/* #include <string.h> */ |
| 122235 | +/* #include <assert.h> */ |
| 122236 | +/* #include <stdlib.h> */ |
| 120893 | 122237 | |
| 120894 | 122238 | /* |
| 120895 | 122239 | ** When full-text index nodes are loaded from disk, the buffer that they |
| 120896 | 122240 | ** are loaded into has the following number of bytes of padding at the end |
| 120897 | 122241 | ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer |
| | @@ -124149,10 +125493,12 @@ |
| 124149 | 125493 | ****************************************************************************** |
| 124150 | 125494 | */ |
| 124151 | 125495 | |
| 124152 | 125496 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 124153 | 125497 | |
| 125498 | +/* #include <string.h> */ |
| 125499 | +/* #include <assert.h> */ |
| 124154 | 125500 | |
| 124155 | 125501 | /* |
| 124156 | 125502 | ** Characters that may appear in the second argument to matchinfo(). |
| 124157 | 125503 | */ |
| 124158 | 125504 | #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */ |
| | @@ -125736,10 +127082,12 @@ |
| 125736 | 127082 | #ifndef SQLITE_CORE |
| 125737 | 127083 | SQLITE_EXTENSION_INIT1 |
| 125738 | 127084 | #else |
| 125739 | 127085 | #endif |
| 125740 | 127086 | |
| 127087 | +/* #include <string.h> */ |
| 127088 | +/* #include <assert.h> */ |
| 125741 | 127089 | |
| 125742 | 127090 | #ifndef SQLITE_AMALGAMATION |
| 125743 | 127091 | #include "sqlite3rtree.h" |
| 125744 | 127092 | typedef sqlite3_int64 i64; |
| 125745 | 127093 | typedef unsigned char u8; |
| | @@ -128950,10 +130298,11 @@ |
| 128950 | 130298 | #include <unicode/utypes.h> |
| 128951 | 130299 | #include <unicode/uregex.h> |
| 128952 | 130300 | #include <unicode/ustring.h> |
| 128953 | 130301 | #include <unicode/ucol.h> |
| 128954 | 130302 | |
| 130303 | +/* #include <assert.h> */ |
| 128955 | 130304 | |
| 128956 | 130305 | #ifndef SQLITE_CORE |
| 128957 | 130306 | SQLITE_EXTENSION_INIT1 |
| 128958 | 130307 | #else |
| 128959 | 130308 | #endif |
| | @@ -129429,12 +130778,16 @@ |
| 129429 | 130778 | ** This file implements a tokenizer for fts3 based on the ICU library. |
| 129430 | 130779 | */ |
| 129431 | 130780 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) |
| 129432 | 130781 | #ifdef SQLITE_ENABLE_ICU |
| 129433 | 130782 | |
| 130783 | +/* #include <assert.h> */ |
| 130784 | +/* #include <string.h> */ |
| 129434 | 130785 | |
| 129435 | 130786 | #include <unicode/ubrk.h> |
| 130787 | +/* #include <unicode/ucol.h> */ |
| 130788 | +/* #include <unicode/ustring.h> */ |
| 129436 | 130789 | #include <unicode/utf16.h> |
| 129437 | 130790 | |
| 129438 | 130791 | typedef struct IcuTokenizer IcuTokenizer; |
| 129439 | 130792 | typedef struct IcuCursor IcuCursor; |
| 129440 | 130793 | |
| 129441 | 130794 | |