Fossil SCM

Merge latest trunk.

dmitry 2011-08-31 09:55 symlinks merge
Commit 40ed431ca59e0a154d78b876039f730d5889d473
+2380 -1310
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -385,23 +385,29 @@
385385
/*
386386
** Exactly one of the following macros must be defined in order to
387387
** specify which memory allocation subsystem to use.
388388
**
389389
** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
390
+** SQLITE_WIN32_MALLOC // Use Win32 native heap API
390391
** 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.
391397
**
392398
** (Historical note: There used to be several other options, but we've
393399
** pared it down to just these two.)
394400
**
395401
** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
396402
** the default.
397403
*/
398
-#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1
404
+#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
399405
# 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"
401407
#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
403409
# define SQLITE_SYSTEM_MALLOC 1
404410
#endif
405411
406412
/*
407413
** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -650,11 +656,11 @@
650656
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651657
** [sqlite_version()] and [sqlite_source_id()].
652658
*/
653659
#define SQLITE_VERSION "3.7.8"
654660
#define SQLITE_VERSION_NUMBER 3007008
655
-#define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
661
+#define SQLITE_SOURCE_ID "2011-08-29 11:56:14 639cc85a911454bffdcccb33f2976c683953ae64"
656662
657663
/*
658664
** CAPI3REF: Run-Time Library Version Numbers
659665
** KEYWORDS: sqlite3_version, sqlite3_sourceid
660666
**
@@ -1751,20 +1757,14 @@
17511757
** also used during testing of SQLite in order to specify an alternative
17521758
** memory allocator that simulates memory out-of-memory conditions in
17531759
** order to verify that SQLite recovers gracefully from such
17541760
** conditions.
17551761
**
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
17621765
** 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.
17661766
**
17671767
** xSize should return the allocated size of a memory allocation
17681768
** previously obtained from xMalloc or xRealloc. The allocated size
17691769
** is always at least as big as the requested size but may be larger.
17701770
**
@@ -3397,11 +3397,11 @@
33973397
** a schema change, on the first [sqlite3_step()] call following any change
33983398
** to the [sqlite3_bind_text | bindings] of that [parameter].
33993399
** ^The specific value of WHERE-clause [parameter] might influence the
34003400
** choice of query plan if the parameter is the left-hand side of a [LIKE]
34013401
** 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.
34033403
** the
34043404
** </li>
34053405
** </ol>
34063406
*/
34073407
SQLITE_API int sqlite3_prepare(
@@ -7632,10 +7632,18 @@
76327632
*/
76337633
#ifndef SQLITE_TEMP_STORE
76347634
# define SQLITE_TEMP_STORE 1
76357635
#endif
76367636
7637
+/*
7638
+** If all temporary storage is in-memory, then omit the external merge-sort
7639
+** logic since it is superfluous.
7640
+*/
7641
+#if SQLITE_TEMP_STORE==3 && !defined(SQLITE_OMIT_MERGE_SORT)
7642
+# define SQLITE_OMIT_MERGE_SORT
7643
+#endif
7644
+
76377645
/*
76387646
** GCC does not define the offsetof() macro so we'll have to do it
76397647
** ourselves.
76407648
*/
76417649
#ifndef offsetof
@@ -7711,22 +7719,10 @@
77117719
** is 0x00000000ffffffff. But because of quirks of some compilers, we
77127720
** have to specify the value in the less intuitive manner shown:
77137721
*/
77147722
#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
77157723
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
-
77287724
/*
77297725
** Macros to determine whether the machine is big or little endian,
77307726
** evaluated at runtime.
77317727
*/
77327728
#ifdef SQLITE_AMALGAMATION
@@ -7986,10 +7982,11 @@
79867982
#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
79877983
#define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */
79887984
#define BTREE_MEMORY 4 /* This is an in-memory DB */
79897985
#define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */
79907986
#define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */
7987
+#define BTREE_SORTER 32 /* Used as accumulator in external merge sort */
79917988
79927989
SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
79937990
SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
79947991
SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
79957992
SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
@@ -8188,10 +8185,11 @@
81888185
** or VDBE. The VDBE implements an abstract machine that runs a
81898186
** simple program to access and modify the underlying database.
81908187
*/
81918188
#ifndef _SQLITE_VDBE_H_
81928189
#define _SQLITE_VDBE_H_
8190
+/* #include <stdio.h> */
81938191
81948192
/*
81958193
** A single VDBE is an opaque structure named "Vdbe". Only routines
81968194
** in the source file sqliteVdbe.c are allowed to see the insides
81978195
** of this structure.
@@ -8231,10 +8229,11 @@
82318229
Mem *pMem; /* Used when p4type is P4_MEM */
82328230
VTable *pVtab; /* Used when p4type is P4_VTAB */
82338231
KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
82348232
int *ai; /* Used when p4type is P4_INTARRAY */
82358233
SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
8234
+ int (*xAdvance)(BtCursor *, int *);
82368235
} p4;
82378236
#ifdef SQLITE_DEBUG
82388237
char *zComment; /* Comment to improve readability */
82398238
#endif
82408239
#ifdef VDBE_PROFILE
@@ -8286,10 +8285,11 @@
82868285
#define P4_REAL (-12) /* P4 is a 64-bit floating point value */
82878286
#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
82888287
#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
82898288
#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
82908289
#define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
8290
+#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
82918291
82928292
/* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
82938293
** is made. That copy is freed when the Vdbe is finalized. But if the
82948294
** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
82958295
** gets freed when the Vdbe is finalized so it still should be obtained
@@ -8399,89 +8399,89 @@
83998399
#define OP_ReadCookie 35
84008400
#define OP_SetCookie 36
84018401
#define OP_VerifyCookie 37
84028402
#define OP_OpenRead 38
84038403
#define OP_OpenWrite 39
8404
-#define OP_OpenAutoindex 40
8405
-#define OP_OpenEphemeral 41
8406
-#define OP_OpenPseudo 42
8407
-#define OP_Close 43
8408
-#define OP_SeekLt 44
8409
-#define OP_SeekLe 45
8410
-#define OP_SeekGe 46
8411
-#define OP_SeekGt 47
8412
-#define OP_Seek 48
8413
-#define OP_NotFound 49
8414
-#define OP_Found 50
8415
-#define OP_IsUnique 51
8416
-#define OP_NotExists 52
8417
-#define OP_Sequence 53
8418
-#define OP_NewRowid 54
8419
-#define OP_Insert 55
8420
-#define OP_InsertInt 56
8421
-#define OP_Delete 57
8422
-#define OP_ResetCount 58
8423
-#define OP_RowKey 59
8424
-#define OP_RowData 60
8425
-#define OP_Rowid 61
8426
-#define OP_NullRow 62
8427
-#define OP_Last 63
8428
-#define OP_Sort 64
8429
-#define OP_Rewind 65
8430
-#define OP_Prev 66
8431
-#define OP_Next 67
8432
-#define OP_IdxInsert 70
8433
-#define OP_IdxDelete 71
8434
-#define OP_IdxRowid 72
8435
-#define OP_IdxLT 81
8436
-#define OP_IdxGE 92
8437
-#define OP_Destroy 95
8438
-#define OP_Clear 96
8439
-#define OP_CreateIndex 97
8440
-#define OP_CreateTable 98
8441
-#define OP_ParseSchema 99
8442
-#define OP_LoadAnalysis 100
8443
-#define OP_DropTable 101
8444
-#define OP_DropIndex 102
8445
-#define OP_DropTrigger 103
8446
-#define OP_IntegrityCk 104
8447
-#define OP_RowSetAdd 105
8448
-#define OP_RowSetRead 106
8449
-#define OP_RowSetTest 107
8450
-#define OP_Program 108
8451
-#define OP_Param 109
8452
-#define OP_FkCounter 110
8453
-#define OP_FkIfZero 111
8454
-#define OP_MemMax 112
8455
-#define OP_IfPos 113
8456
-#define OP_IfNeg 114
8457
-#define OP_IfZero 115
8458
-#define OP_AggStep 116
8459
-#define OP_AggFinal 117
8460
-#define OP_Checkpoint 118
8461
-#define OP_JournalMode 119
8462
-#define OP_Vacuum 120
8463
-#define OP_IncrVacuum 121
8464
-#define OP_Expire 122
8465
-#define OP_TableLock 123
8466
-#define OP_VBegin 124
8467
-#define OP_VCreate 125
8468
-#define OP_VDestroy 126
8469
-#define OP_VOpen 127
8470
-#define OP_VFilter 128
8471
-#define OP_VColumn 129
8472
-#define OP_VNext 131
8473
-#define OP_VRename 132
8474
-#define OP_VUpdate 133
8475
-#define OP_Pagecount 134
8476
-#define OP_MaxPgcnt 135
8477
-#define OP_Trace 136
8478
-#define OP_Noop 137
8479
-#define OP_Explain 138
8480
-
8481
-/* The following opcode values are never used */
8482
-#define OP_NotUsed_139 139
8404
+#define OP_OpenSorter 40
8405
+#define OP_OpenAutoindex 41
8406
+#define OP_OpenEphemeral 42
8407
+#define OP_OpenPseudo 43
8408
+#define OP_Close 44
8409
+#define OP_SeekLt 45
8410
+#define OP_SeekLe 46
8411
+#define OP_SeekGe 47
8412
+#define OP_SeekGt 48
8413
+#define OP_Seek 49
8414
+#define OP_NotFound 50
8415
+#define OP_Found 51
8416
+#define OP_IsUnique 52
8417
+#define OP_NotExists 53
8418
+#define OP_Sequence 54
8419
+#define OP_NewRowid 55
8420
+#define OP_Insert 56
8421
+#define OP_InsertInt 57
8422
+#define OP_Delete 58
8423
+#define OP_ResetCount 59
8424
+#define OP_RowKey 60
8425
+#define OP_RowData 61
8426
+#define OP_Rowid 62
8427
+#define OP_NullRow 63
8428
+#define OP_Last 64
8429
+#define OP_Sort 65
8430
+#define OP_Rewind 66
8431
+#define OP_Prev 67
8432
+#define OP_Next 70
8433
+#define OP_IdxInsert 71
8434
+#define OP_IdxDelete 72
8435
+#define OP_IdxRowid 81
8436
+#define OP_IdxLT 92
8437
+#define OP_IdxGE 95
8438
+#define OP_Destroy 96
8439
+#define OP_Clear 97
8440
+#define OP_CreateIndex 98
8441
+#define OP_CreateTable 99
8442
+#define OP_ParseSchema 100
8443
+#define OP_LoadAnalysis 101
8444
+#define OP_DropTable 102
8445
+#define OP_DropIndex 103
8446
+#define OP_DropTrigger 104
8447
+#define OP_IntegrityCk 105
8448
+#define OP_RowSetAdd 106
8449
+#define OP_RowSetRead 107
8450
+#define OP_RowSetTest 108
8451
+#define OP_Program 109
8452
+#define OP_Param 110
8453
+#define OP_FkCounter 111
8454
+#define OP_FkIfZero 112
8455
+#define OP_MemMax 113
8456
+#define OP_IfPos 114
8457
+#define OP_IfNeg 115
8458
+#define OP_IfZero 116
8459
+#define OP_AggStep 117
8460
+#define OP_AggFinal 118
8461
+#define OP_Checkpoint 119
8462
+#define OP_JournalMode 120
8463
+#define OP_Vacuum 121
8464
+#define OP_IncrVacuum 122
8465
+#define OP_Expire 123
8466
+#define OP_TableLock 124
8467
+#define OP_VBegin 125
8468
+#define OP_VCreate 126
8469
+#define OP_VDestroy 127
8470
+#define OP_VOpen 128
8471
+#define OP_VFilter 129
8472
+#define OP_VColumn 131
8473
+#define OP_VNext 132
8474
+#define OP_VRename 133
8475
+#define OP_VUpdate 134
8476
+#define OP_Pagecount 135
8477
+#define OP_MaxPgcnt 136
8478
+#define OP_Trace 137
8479
+#define OP_Noop 138
8480
+#define OP_Explain 139
8481
+
8482
+/* The following opcode values are never used */
84838483
#define OP_NotUsed_140 140
84848484
84858485
84868486
/* Properties such as "out2" or "jump" that are specified in
84878487
** comments following the "case" for each opcode in the vdbe.c
@@ -8498,23 +8498,23 @@
84988498
/* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
84998499
/* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
85008500
/* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
85018501
/* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
85028502
/* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
8503
-/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
8504
-/* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
8505
-/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
8506
-/* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\
8507
-/* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8508
-/* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8509
-/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
8510
-/* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
8511
-/* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
8512
-/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8513
-/* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8514
-/* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\
8515
-/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
8503
+/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11,\
8504
+/* 48 */ 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02,\
8505
+/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,\
8506
+/* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x01, 0x08,\
8507
+/* 72 */ 0x00, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8508
+/* 80 */ 0x15, 0x02, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8509
+/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
8510
+/* 96 */ 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\
8511
+/* 104 */ 0x00, 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00,\
8512
+/* 112 */ 0x01, 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00,\
8513
+/* 120 */ 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
8514
+/* 128 */ 0x00, 0x01, 0x02, 0x00, 0x01, 0x00, 0x00, 0x02,\
8515
+/* 136 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
85168516
/* 144 */ 0x04, 0x04,}
85178517
85188518
/************** End of opcodes.h *********************************************/
85198519
/************** Continuing where we left off in vdbe.h ***********************/
85208520
@@ -8529,13 +8529,13 @@
85298529
SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
85308530
SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
85318531
SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
85328532
SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
85338533
SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
8534
-SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
8535
-SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
8536
-SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
8534
+SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
8535
+SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
8536
+SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
85378537
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
85388538
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
85398539
SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
85408540
SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
85418541
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
@@ -8653,10 +8653,11 @@
86538653
** NOTE: These values must match the corresponding BTREE_ values in btree.h.
86548654
*/
86558655
#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
86568656
#define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
86578657
#define PAGER_MEMORY 0x0004 /* In-memory database */
8658
+#define PAGER_SORTER 0x0020 /* Accumulator in external merge sort */
86588659
86598660
/*
86608661
** Valid values for the second argument to sqlite3PagerLockingMode().
86618662
*/
86628663
#define PAGER_LOCKINGMODE_QUERY -1
@@ -8748,10 +8749,13 @@
87488749
SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
87498750
SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
87508751
SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
87518752
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
87528753
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
8754
+#ifndef SQLITE_OMIT_MERGE_SORT
8755
+SQLITE_PRIVATE int sqlite3PagerUnderStress(Pager*);
8756
+#endif
87538757
87548758
/* Functions used to truncate the database file. */
87558759
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
87568760
87578761
#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -9924,11 +9928,11 @@
99249928
int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
99259929
int nCol; /* Number of columns in this table */
99269930
Column *aCol; /* Information about each column */
99279931
Index *pIndex; /* List of SQL indexes on this table. */
99289932
int tnum; /* Root BTree node for this table (see note above) */
9929
- tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9933
+ unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
99309934
Select *pSelect; /* NULL for tables. Points to definition if a view. */
99319935
u16 nRef; /* Number of pointers to this Table */
99329936
u8 tabFlags; /* Mask of TF_* values */
99339937
u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
99349938
FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10123,43 +10127,35 @@
1012310127
*/
1012410128
struct Index {
1012510129
char *zName; /* Name of this index */
1012610130
int nColumn; /* Number of columns in the table used by this index */
1012710131
int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10128
- tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10132
+ unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
1012910133
Table *pTable; /* The SQL table being indexed */
1013010134
int tnum; /* Page containing root of this index in database file */
1013110135
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1013210136
u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
1013310137
u8 bUnordered; /* Use this index for == or IN queries only */
10134
- u8 nSample; /* Number of elements in aSample[] */
1013510138
char *zColAff; /* String defining the affinity of each column */
1013610139
Index *pNext; /* The next index associated with the same table */
1013710140
Schema *pSchema; /* Schema containing this index */
1013810141
u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
1013910142
char **azColl; /* Array of collation sequence names for index */
10140
-#ifdef SQLITE_ENABLE_STAT3
10141
- tRowcnt avgEq; /* Average nEq value for key values not in aSample */
10142
- IndexSample *aSample; /* Samples of the left-most key */
10143
-#endif
10143
+ IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
1014410144
};
1014510145
1014610146
/*
1014710147
** Each sample stored in the sqlite_stat2 table is represented in memory
1014810148
** using a structure of this type.
1014910149
*/
1015010150
struct IndexSample {
1015110151
union {
1015210152
char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10153
- double r; /* Value if eType is SQLITE_FLOAT */
10154
- i64 i; /* Value if eType is SQLITE_INTEGER */
10153
+ double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
1015510154
} u;
1015610155
u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10157
- u16 nByte; /* Size in byte of text or blob. */
10158
- tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10159
- tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10160
- tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
10156
+ u8 nByte; /* Size in byte of text or blob. */
1016110157
};
1016210158
1016310159
/*
1016410160
** Each token coming out of the lexer is an instance of
1016510161
** this structure. Tokens are also used as part of an expression.
@@ -11361,11 +11357,10 @@
1136111357
#else
1136211358
# define sqlite3ViewGetColumnNames(A,B) 0
1136311359
#endif
1136411360
1136511361
SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11366
-SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
1136711362
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
1136811363
#ifndef SQLITE_OMIT_AUTOINCREMENT
1136911364
SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
1137011365
SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
1137111366
#else
@@ -11618,11 +11613,11 @@
1161811613
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
1161911614
void(*)(void*));
1162011615
SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
1162111616
SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
1162211617
SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11623
-#ifdef SQLITE_ENABLE_STAT3
11618
+#ifdef SQLITE_ENABLE_STAT2
1162411619
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
1162511620
#endif
1162611621
SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
1162711622
SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
1162811623
#ifndef SQLITE_AMALGAMATION
@@ -12245,13 +12240,10 @@
1224512240
"ENABLE_RTREE",
1224612241
#endif
1224712242
#ifdef SQLITE_ENABLE_STAT2
1224812243
"ENABLE_STAT2",
1224912244
#endif
12250
-#ifdef SQLITE_ENABLE_STAT3
12251
- "ENABLE_STAT3",
12252
-#endif
1225312245
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
1225412246
"ENABLE_UNLOCK_NOTIFY",
1225512247
#endif
1225612248
#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
1225712249
"ENABLE_UPDATE_DELETE_LIMIT",
@@ -12387,10 +12379,13 @@
1238712379
#ifdef SQLITE_OMIT_LOOKASIDE
1238812380
"OMIT_LOOKASIDE",
1238912381
#endif
1239012382
#ifdef SQLITE_OMIT_MEMORYDB
1239112383
"OMIT_MEMORYDB",
12384
+#endif
12385
+#ifdef SQLITE_OMIT_MERGE_SORT
12386
+ "OMIT_MERGE_SORT",
1239212387
#endif
1239312388
#ifdef SQLITE_OMIT_OR_OPTIMIZATION
1239412389
"OMIT_OR_OPTIMIZATION",
1239512390
#endif
1239612391
#ifdef SQLITE_OMIT_PAGER_PRAGMAS
@@ -12453,10 +12448,13 @@
1245312448
#ifdef SQLITE_OMIT_WSD
1245412449
"OMIT_WSD",
1245512450
#endif
1245612451
#ifdef SQLITE_OMIT_XFER_OPT
1245712452
"OMIT_XFER_OPT",
12453
+#endif
12454
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
12455
+ "PAGECACHE_BLOCKALLOC",
1245812456
#endif
1245912457
#ifdef SQLITE_PERFORMANCE_TRACE
1246012458
"PERFORMANCE_TRACE",
1246112459
#endif
1246212460
#ifdef SQLITE_PROXY_DEBUG
@@ -12574,10 +12572,13 @@
1257412572
/*
1257512573
** Boolean values
1257612574
*/
1257712575
typedef unsigned char Bool;
1257812576
12577
+/* Opaque type used by code in vdbesort.c */
12578
+typedef struct VdbeSorter VdbeSorter;
12579
+
1257912580
/*
1258012581
** A cursor is a pointer into a single BTree within a database file.
1258112582
** The cursor can seek to a BTree entry with a particular key, or
1258212583
** loop over all entries of the Btree. You can also insert new BTree
1258312584
** entries or retrieve the key or data from the entry that the cursor
@@ -12605,10 +12606,11 @@
1260512606
sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
1260612607
const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
1260712608
i64 seqCount; /* Sequence counter */
1260812609
i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
1260912610
i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
12611
+ VdbeSorter *pSorter; /* Sorter object for OP_OpenSorter cursors */
1261012612
1261112613
/* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
1261212614
** OP_IsUnique opcode on this cursor. */
1261312615
int seekResult;
1261412616
@@ -12924,17 +12926,36 @@
1292412926
SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
1292512927
SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
1292612928
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
1292712929
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
1292812930
SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12931
+#define MemReleaseExt(X) \
12932
+ if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
12933
+ sqlite3VdbeMemReleaseExternal(X);
1292912934
SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
1293012935
SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
1293112936
SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
1293212937
SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
1293312938
SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
1293412939
SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
1293512940
SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12941
+
12942
+#ifdef SQLITE_OMIT_MERGE_SORT
12943
+# define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK
12944
+# define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK
12945
+# define sqlite3VdbeSorterClose(Y,Z)
12946
+# define sqlite3VdbeSorterRowkey(Y,Z) SQLITE_OK
12947
+# define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK
12948
+# define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK
12949
+#else
12950
+SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
12951
+SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, int);
12952
+SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
12953
+SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
12954
+SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
12955
+SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
12956
+#endif
1293612957
1293712958
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
1293812959
SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
1293912960
SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
1294012961
#else
@@ -13225,10 +13246,12 @@
1322513246
** Astronomical Algorithms, 2nd Edition, 1998
1322613247
** ISBM 0-943396-61-1
1322713248
** Willmann-Bell, Inc
1322813249
** Richmond, Virginia (USA)
1322913250
*/
13251
+/* #include <stdlib.h> */
13252
+/* #include <assert.h> */
1323013253
#include <time.h>
1323113254
1323213255
#ifndef SQLITE_OMIT_DATETIME_FUNCS
1323313256
1323413257
@@ -14978,10 +15001,11 @@
1497815001
extern void backtrace_symbols_fd(void*const*,int,int);
1497915002
#else
1498015003
# define backtrace(A,B) 1
1498115004
# define backtrace_symbols_fd(A,B,C)
1498215005
#endif
15006
+/* #include <stdio.h> */
1498315007
1498415008
/*
1498515009
** Each memory allocation looks like this:
1498615010
**
1498715011
** ------------------------------------------------------------------------
@@ -18081,10 +18105,11 @@
1808118105
**
1808218106
*************************************************************************
1808318107
**
1808418108
** Memory allocation functions used throughout sqlite.
1808518109
*/
18110
+/* #include <stdarg.h> */
1808618111
1808718112
/*
1808818113
** Attempt to release up to n bytes of non-essential memory currently
1808918114
** held by SQLite. An example of non-essential memory is memory used to
1809018115
** cache database pages that are not currently in use.
@@ -20058,10 +20083,11 @@
2005820083
** BOM or Byte Order Mark:
2005920084
** 0xff 0xfe little-endian utf-16 follows
2006020085
** 0xfe 0xff big-endian utf-16 follows
2006120086
**
2006220087
*/
20088
+/* #include <assert.h> */
2006320089
2006420090
#ifndef SQLITE_AMALGAMATION
2006520091
/*
2006620092
** The following constant value is used by the SQLITE_BIGENDIAN and
2006720093
** SQLITE_LITTLEENDIAN macros.
@@ -20486,11 +20512,11 @@
2048620512
** no longer required.
2048720513
**
2048820514
** If a malloc failure occurs, NULL is returned and the db.mallocFailed
2048920515
** flag set.
2049020516
*/
20491
-#ifdef SQLITE_ENABLE_STAT3
20517
+#ifdef SQLITE_ENABLE_STAT2
2049220518
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
2049320519
Mem m;
2049420520
memset(&m, 0, sizeof(m));
2049520521
m.db = db;
2049620522
sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -20600,10 +20626,11 @@
2060020626
**
2060120627
** This file contains functions for allocating memory, comparing
2060220628
** strings, and stuff like that.
2060320629
**
2060420630
*/
20631
+/* #include <stdarg.h> */
2060520632
#ifdef SQLITE_HAVE_ISNAN
2060620633
# include <math.h>
2060720634
#endif
2060820635
2060920636
/*
@@ -21778,10 +21805,11 @@
2177821805
**
2177921806
*************************************************************************
2178021807
** This is the implementation of generic hash-tables
2178121808
** used in SQLite.
2178221809
*/
21810
+/* #include <assert.h> */
2178321811
2178421812
/* Turn bulk memory into a hash table object by initializing the
2178521813
** fields of the Hash structure.
2178621814
**
2178721815
** "pNew" is a pointer to the hash table that is to be initialized.
@@ -22086,52 +22114,52 @@
2208622114
/* 35 */ "ReadCookie",
2208722115
/* 36 */ "SetCookie",
2208822116
/* 37 */ "VerifyCookie",
2208922117
/* 38 */ "OpenRead",
2209022118
/* 39 */ "OpenWrite",
22091
- /* 40 */ "OpenAutoindex",
22092
- /* 41 */ "OpenEphemeral",
22093
- /* 42 */ "OpenPseudo",
22094
- /* 43 */ "Close",
22095
- /* 44 */ "SeekLt",
22096
- /* 45 */ "SeekLe",
22097
- /* 46 */ "SeekGe",
22098
- /* 47 */ "SeekGt",
22099
- /* 48 */ "Seek",
22100
- /* 49 */ "NotFound",
22101
- /* 50 */ "Found",
22102
- /* 51 */ "IsUnique",
22103
- /* 52 */ "NotExists",
22104
- /* 53 */ "Sequence",
22105
- /* 54 */ "NewRowid",
22106
- /* 55 */ "Insert",
22107
- /* 56 */ "InsertInt",
22108
- /* 57 */ "Delete",
22109
- /* 58 */ "ResetCount",
22110
- /* 59 */ "RowKey",
22111
- /* 60 */ "RowData",
22112
- /* 61 */ "Rowid",
22113
- /* 62 */ "NullRow",
22114
- /* 63 */ "Last",
22115
- /* 64 */ "Sort",
22116
- /* 65 */ "Rewind",
22117
- /* 66 */ "Prev",
22118
- /* 67 */ "Next",
22119
+ /* 40 */ "OpenSorter",
22120
+ /* 41 */ "OpenAutoindex",
22121
+ /* 42 */ "OpenEphemeral",
22122
+ /* 43 */ "OpenPseudo",
22123
+ /* 44 */ "Close",
22124
+ /* 45 */ "SeekLt",
22125
+ /* 46 */ "SeekLe",
22126
+ /* 47 */ "SeekGe",
22127
+ /* 48 */ "SeekGt",
22128
+ /* 49 */ "Seek",
22129
+ /* 50 */ "NotFound",
22130
+ /* 51 */ "Found",
22131
+ /* 52 */ "IsUnique",
22132
+ /* 53 */ "NotExists",
22133
+ /* 54 */ "Sequence",
22134
+ /* 55 */ "NewRowid",
22135
+ /* 56 */ "Insert",
22136
+ /* 57 */ "InsertInt",
22137
+ /* 58 */ "Delete",
22138
+ /* 59 */ "ResetCount",
22139
+ /* 60 */ "RowKey",
22140
+ /* 61 */ "RowData",
22141
+ /* 62 */ "Rowid",
22142
+ /* 63 */ "NullRow",
22143
+ /* 64 */ "Last",
22144
+ /* 65 */ "Sort",
22145
+ /* 66 */ "Rewind",
22146
+ /* 67 */ "Prev",
2211922147
/* 68 */ "Or",
2212022148
/* 69 */ "And",
22121
- /* 70 */ "IdxInsert",
22122
- /* 71 */ "IdxDelete",
22123
- /* 72 */ "IdxRowid",
22149
+ /* 70 */ "Next",
22150
+ /* 71 */ "IdxInsert",
22151
+ /* 72 */ "IdxDelete",
2212422152
/* 73 */ "IsNull",
2212522153
/* 74 */ "NotNull",
2212622154
/* 75 */ "Ne",
2212722155
/* 76 */ "Eq",
2212822156
/* 77 */ "Gt",
2212922157
/* 78 */ "Le",
2213022158
/* 79 */ "Lt",
2213122159
/* 80 */ "Ge",
22132
- /* 81 */ "IdxLT",
22160
+ /* 81 */ "IdxRowid",
2213322161
/* 82 */ "BitAnd",
2213422162
/* 83 */ "BitOr",
2213522163
/* 84 */ "ShiftLeft",
2213622164
/* 85 */ "ShiftRight",
2213722165
/* 86 */ "Add",
@@ -22138,58 +22166,58 @@
2213822166
/* 87 */ "Subtract",
2213922167
/* 88 */ "Multiply",
2214022168
/* 89 */ "Divide",
2214122169
/* 90 */ "Remainder",
2214222170
/* 91 */ "Concat",
22143
- /* 92 */ "IdxGE",
22171
+ /* 92 */ "IdxLT",
2214422172
/* 93 */ "BitNot",
2214522173
/* 94 */ "String8",
22146
- /* 95 */ "Destroy",
22147
- /* 96 */ "Clear",
22148
- /* 97 */ "CreateIndex",
22149
- /* 98 */ "CreateTable",
22150
- /* 99 */ "ParseSchema",
22151
- /* 100 */ "LoadAnalysis",
22152
- /* 101 */ "DropTable",
22153
- /* 102 */ "DropIndex",
22154
- /* 103 */ "DropTrigger",
22155
- /* 104 */ "IntegrityCk",
22156
- /* 105 */ "RowSetAdd",
22157
- /* 106 */ "RowSetRead",
22158
- /* 107 */ "RowSetTest",
22159
- /* 108 */ "Program",
22160
- /* 109 */ "Param",
22161
- /* 110 */ "FkCounter",
22162
- /* 111 */ "FkIfZero",
22163
- /* 112 */ "MemMax",
22164
- /* 113 */ "IfPos",
22165
- /* 114 */ "IfNeg",
22166
- /* 115 */ "IfZero",
22167
- /* 116 */ "AggStep",
22168
- /* 117 */ "AggFinal",
22169
- /* 118 */ "Checkpoint",
22170
- /* 119 */ "JournalMode",
22171
- /* 120 */ "Vacuum",
22172
- /* 121 */ "IncrVacuum",
22173
- /* 122 */ "Expire",
22174
- /* 123 */ "TableLock",
22175
- /* 124 */ "VBegin",
22176
- /* 125 */ "VCreate",
22177
- /* 126 */ "VDestroy",
22178
- /* 127 */ "VOpen",
22179
- /* 128 */ "VFilter",
22180
- /* 129 */ "VColumn",
22174
+ /* 95 */ "IdxGE",
22175
+ /* 96 */ "Destroy",
22176
+ /* 97 */ "Clear",
22177
+ /* 98 */ "CreateIndex",
22178
+ /* 99 */ "CreateTable",
22179
+ /* 100 */ "ParseSchema",
22180
+ /* 101 */ "LoadAnalysis",
22181
+ /* 102 */ "DropTable",
22182
+ /* 103 */ "DropIndex",
22183
+ /* 104 */ "DropTrigger",
22184
+ /* 105 */ "IntegrityCk",
22185
+ /* 106 */ "RowSetAdd",
22186
+ /* 107 */ "RowSetRead",
22187
+ /* 108 */ "RowSetTest",
22188
+ /* 109 */ "Program",
22189
+ /* 110 */ "Param",
22190
+ /* 111 */ "FkCounter",
22191
+ /* 112 */ "FkIfZero",
22192
+ /* 113 */ "MemMax",
22193
+ /* 114 */ "IfPos",
22194
+ /* 115 */ "IfNeg",
22195
+ /* 116 */ "IfZero",
22196
+ /* 117 */ "AggStep",
22197
+ /* 118 */ "AggFinal",
22198
+ /* 119 */ "Checkpoint",
22199
+ /* 120 */ "JournalMode",
22200
+ /* 121 */ "Vacuum",
22201
+ /* 122 */ "IncrVacuum",
22202
+ /* 123 */ "Expire",
22203
+ /* 124 */ "TableLock",
22204
+ /* 125 */ "VBegin",
22205
+ /* 126 */ "VCreate",
22206
+ /* 127 */ "VDestroy",
22207
+ /* 128 */ "VOpen",
22208
+ /* 129 */ "VFilter",
2218122209
/* 130 */ "Real",
22182
- /* 131 */ "VNext",
22183
- /* 132 */ "VRename",
22184
- /* 133 */ "VUpdate",
22185
- /* 134 */ "Pagecount",
22186
- /* 135 */ "MaxPgcnt",
22187
- /* 136 */ "Trace",
22188
- /* 137 */ "Noop",
22189
- /* 138 */ "Explain",
22190
- /* 139 */ "NotUsed_139",
22210
+ /* 131 */ "VColumn",
22211
+ /* 132 */ "VNext",
22212
+ /* 133 */ "VRename",
22213
+ /* 134 */ "VUpdate",
22214
+ /* 135 */ "Pagecount",
22215
+ /* 136 */ "MaxPgcnt",
22216
+ /* 137 */ "Trace",
22217
+ /* 138 */ "Noop",
22218
+ /* 139 */ "Explain",
2219122219
/* 140 */ "NotUsed_140",
2219222220
/* 141 */ "ToText",
2219322221
/* 142 */ "ToBlob",
2219422222
/* 143 */ "ToNumeric",
2219522223
/* 144 */ "ToInt",
@@ -24450,10 +24478,11 @@
2445024478
*/
2445124479
#include <sys/types.h>
2445224480
#include <sys/stat.h>
2445324481
#include <fcntl.h>
2445424482
#include <unistd.h>
24483
+/* #include <time.h> */
2445524484
#include <sys/time.h>
2445624485
#include <errno.h>
2445724486
#ifndef SQLITE_OMIT_WAL
2445824487
#include <sys/mman.h>
2445924488
#endif
@@ -24485,10 +24514,11 @@
2448524514
/*
2448624515
** If we are to be thread-safe, include the pthreads header and define
2448724516
** the SQLITE_UNIX_THREADS macro.
2448824517
*/
2448924518
#if SQLITE_THREADSAFE
24519
+/* # include <pthread.h> */
2449024520
# define SQLITE_UNIX_THREADS 1
2449124521
#endif
2449224522
2449324523
/*
2449424524
** Default permissions when creating a new file
@@ -24584,11 +24614,15 @@
2458424614
** Allowed values for the unixFile.ctrlFlags bitmask:
2458524615
*/
2458624616
#define UNIXFILE_EXCL 0x01 /* Connections from one process only */
2458724617
#define UNIXFILE_RDONLY 0x02 /* Connection is read only */
2458824618
#define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
24589
-#define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
24619
+#ifndef SQLITE_DISABLE_DIRSYNC
24620
+# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
24621
+#else
24622
+# define UNIXFILE_DIRSYNC 0x00
24623
+#endif
2459024624
2459124625
/*
2459224626
** Include code that is common to all os_*.c files
2459324627
*/
2459424628
/************** Include os_common.h in the middle of os_unix.c ***************/
@@ -27061,15 +27095,16 @@
2706127095
*/
2706227096
static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
2706327097
int rc = SQLITE_OK;
2706427098
int reserved = 0;
2706527099
unixFile *pFile = (unixFile*)id;
27100
+ afpLockingContext *context;
2706627101
2706727102
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2706827103
2706927104
assert( pFile );
27070
- afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
27105
+ context = (afpLockingContext *) pFile->lockingContext;
2707127106
if( context->reserved ){
2707227107
*pResOut = 1;
2707327108
return SQLITE_OK;
2707427109
}
2707527110
unixEnterMutex(); /* Because pFile->pInode is shared across threads */
@@ -27205,11 +27240,11 @@
2720527240
2720627241
/* If control gets to this point, then actually go ahead and make
2720727242
** operating system calls for the specified lock.
2720827243
*/
2720927244
if( eFileLock==SHARED_LOCK ){
27210
- int lrc1, lrc2, lrc1Errno;
27245
+ int lrc1, lrc2, lrc1Errno = 0;
2721127246
long lk, mask;
2721227247
2721327248
assert( pInode->nShared==0 );
2721427249
assert( pInode->eFileLock==0 );
2721527250
@@ -27579,21 +27614,23 @@
2757927614
#if defined(USE_PREAD)
2758027615
do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
2758127616
#elif defined(USE_PREAD64)
2758227617
do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
2758327618
#else
27584
- newOffset = lseek(id->h, offset, SEEK_SET);
27585
- SimulateIOError( newOffset-- );
27586
- if( newOffset!=offset ){
27587
- if( newOffset == -1 ){
27588
- ((unixFile*)id)->lastErrno = errno;
27589
- }else{
27590
- ((unixFile*)id)->lastErrno = 0;
27591
- }
27592
- return -1;
27593
- }
27594
- do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27619
+ do{
27620
+ newOffset = lseek(id->h, offset, SEEK_SET);
27621
+ SimulateIOError( newOffset-- );
27622
+ if( newOffset!=offset ){
27623
+ if( newOffset == -1 ){
27624
+ ((unixFile*)id)->lastErrno = errno;
27625
+ }else{
27626
+ ((unixFile*)id)->lastErrno = 0;
27627
+ }
27628
+ return -1;
27629
+ }
27630
+ got = osWrite(id->h, pBuf, cnt);
27631
+ }while( got<0 && errno==EINTR );
2759527632
#endif
2759627633
TIMER_END;
2759727634
if( got<0 ){
2759827635
((unixFile*)id)->lastErrno = errno;
2759927636
}
@@ -27888,10 +27925,12 @@
2788827925
HAVE_FULLFSYNC, isFullsync));
2788927926
rc = osOpenDirectory(pFile->zPath, &dirfd);
2789027927
if( rc==SQLITE_OK && dirfd>=0 ){
2789127928
full_fsync(dirfd, 0, 0);
2789227929
robust_close(pFile, dirfd, __LINE__);
27930
+ }else if( rc==SQLITE_CANTOPEN ){
27931
+ rc = SQLITE_OK;
2789327932
}
2789427933
pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
2789527934
}
2789627935
return rc;
2789727936
}
@@ -27971,30 +28010,22 @@
2797128010
static int proxyFileControl(sqlite3_file*,int,void*);
2797228011
#endif
2797328012
2797428013
/*
2797528014
** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
27976
-** file-control operation.
27977
-**
27978
-** If the user has configured a chunk-size for this file, it could be
27979
-** that the file needs to be extended at this point. Otherwise, the
27980
-** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
28015
+** file-control operation. Enlarge the database to nBytes in size
28016
+** (rounded up to the next chunk-size). If the database is already
28017
+** nBytes or larger, this routine is a no-op.
2798128018
*/
2798228019
static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27983
- { /* preserve indentation of removed "if" */
28020
+ if( pFile->szChunk ){
2798428021
i64 nSize; /* Required file size */
27985
- i64 szChunk; /* Chunk size */
2798628022
struct stat buf; /* Used to hold return values of fstat() */
2798728023
2798828024
if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
2798928025
27990
- szChunk = pFile->szChunk;
27991
- if( szChunk==0 ){
27992
- nSize = nByte;
27993
- }else{
27994
- nSize = ((nByte+szChunk-1) / szChunk) * szChunk;
27995
- }
28026
+ nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
2799628027
if( nSize>(i64)buf.st_size ){
2799728028
2799828029
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
2799928030
/* The code below is handling the return value of osFallocate()
2800028031
** correctly. posix_fallocate() is defined to "returns zero on success,
@@ -28048,11 +28079,15 @@
2804828079
case SQLITE_FCNTL_CHUNK_SIZE: {
2804928080
pFile->szChunk = *(int *)pArg;
2805028081
return SQLITE_OK;
2805128082
}
2805228083
case SQLITE_FCNTL_SIZE_HINT: {
28053
- return fcntlSizeHint(pFile, *(i64 *)pArg);
28084
+ int rc;
28085
+ SimulateIOErrorBenign(1);
28086
+ rc = fcntlSizeHint(pFile, *(i64 *)pArg);
28087
+ SimulateIOErrorBenign(0);
28088
+ return rc;
2805428089
}
2805528090
case SQLITE_FCNTL_PERSIST_WAL: {
2805628091
int bPersist = *(int*)pArg;
2805728092
if( bPersist<0 ){
2805828093
*(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
@@ -29485,10 +29520,13 @@
2948529520
int isReadonly = (flags & SQLITE_OPEN_READONLY);
2948629521
int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
2948729522
#if SQLITE_ENABLE_LOCKING_STYLE
2948829523
int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
2948929524
#endif
29525
+#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29526
+ struct statfs fsInfo;
29527
+#endif
2949029528
2949129529
/* If creating a master or main-file journal, this function will open
2949229530
** a file-descriptor on the directory too. The first time unixSync()
2949329531
** is called the directory file descriptor will be fsync()ed and close()d.
2949429532
*/
@@ -29617,11 +29655,10 @@
2961729655
2961829656
noLock = eType!=SQLITE_OPEN_MAIN_DB;
2961929657
2962029658
2962129659
#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29622
- struct statfs fsInfo;
2962329660
if( fstatfs(fd, &fsInfo) == -1 ){
2962429661
((unixFile*)pFile)->lastErrno = errno;
2962529662
robust_close(p, fd, __LINE__);
2962629663
return SQLITE_IOERR_ACCESS;
2962729664
}
@@ -29641,11 +29678,10 @@
2964129678
/* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
2964229679
** never use proxy, NULL means use proxy for non-local files only. */
2964329680
if( envforce!=NULL ){
2964429681
useProxy = atoi(envforce)>0;
2964529682
}else{
29646
- struct statfs fsInfo;
2964729683
if( statfs(zPath, &fsInfo) == -1 ){
2964829684
/* In theory, the close(fd) call is sub-optimal. If the file opened
2964929685
** with fd is a database file, and there are other connections open
2965029686
** on that file that are currently holding advisory locks on it,
2965129687
** then the call to close() will cancel those locks. In practice,
@@ -29715,10 +29751,12 @@
2971529751
#endif
2971629752
{
2971729753
rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
2971829754
}
2971929755
robust_close(0, fd, __LINE__);
29756
+ }else if( rc==SQLITE_CANTOPEN ){
29757
+ rc = SQLITE_OK;
2972029758
}
2972129759
}
2972229760
#endif
2972329761
return rc;
2972429762
}
@@ -30380,10 +30418,12 @@
3038030418
*pError = err;
3038130419
}
3038230420
return SQLITE_IOERR;
3038330421
}
3038430422
}
30423
+#else
30424
+ UNUSED_PARAMETER(pError);
3038530425
#endif
3038630426
#ifdef SQLITE_TEST
3038730427
/* simulate multiple hosts by creating unique hostid file paths */
3038830428
if( sqlite3_hostid_num != 0){
3038930429
pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
@@ -30472,10 +30512,11 @@
3047230512
unixFile *conchFile = pCtx->conchFile;
3047330513
int rc = SQLITE_OK;
3047430514
int nTries = 0;
3047530515
struct timespec conchModTime;
3047630516
30517
+ memset(&conchModTime, 0, sizeof(conchModTime));
3047730518
do {
3047830519
rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
3047930520
nTries ++;
3048030521
if( rc==SQLITE_BUSY ){
3048130522
/* If the lock failed (busy):
@@ -30703,15 +30744,16 @@
3070330744
conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
3070430745
3070530746
end_takeconch:
3070630747
OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
3070730748
if( rc==SQLITE_OK && pFile->openFlags ){
30749
+ int fd;
3070830750
if( pFile->h>=0 ){
3070930751
robust_close(pFile, pFile->h, __LINE__);
3071030752
}
3071130753
pFile->h = -1;
30712
- int fd = robust_open(pCtx->dbPath, pFile->openFlags,
30754
+ fd = robust_open(pCtx->dbPath, pFile->openFlags,
3071330755
SQLITE_DEFAULT_FILE_PERMISSIONS);
3071430756
OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
3071530757
if( fd>=0 ){
3071630758
pFile->h = fd;
3071730759
}else{
@@ -31629,10 +31671,80 @@
3162931671
winceLock local; /* Locks obtained by this instance of winFile */
3163031672
winceLock *shared; /* Global shared lock memory for the file */
3163131673
#endif
3163231674
};
3163331675
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 */
3163431746
3163531747
/*
3163631748
** Forward prototypes.
3163731749
*/
3163831750
static int getSectorSize(
@@ -31681,10 +31793,192 @@
3168131793
}
3168231794
return sqlite3_os_type==2;
3168331795
}
3168431796
#endif /* SQLITE_OS_WINCE */
3168531797
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
+
3168631980
/*
3168731981
** Convert a UTF-8 string to microsoft unicode (UTF-16?).
3168831982
**
3168931983
** Space to hold the returned string is obtained from malloc.
3169031984
*/
@@ -31969,10 +32263,11 @@
3196932263
*/
3197032264
/*
3197132265
** WindowsCE does not have a localtime() function. So create a
3197232266
** substitute.
3197332267
*/
32268
+/* #include <time.h> */
3197432269
struct tm *__cdecl localtime(const time_t *t)
3197532270
{
3197632271
static struct tm y;
3197732272
FILETIME uTm, lTm;
3197832273
SYSTEMTIME pTm;
@@ -32860,15 +33155,22 @@
3286033155
case SQLITE_FCNTL_CHUNK_SIZE: {
3286133156
pFile->szChunk = *(int *)pArg;
3286233157
return SQLITE_OK;
3286333158
}
3286433159
case SQLITE_FCNTL_SIZE_HINT: {
32865
- sqlite3_int64 sz = *(sqlite3_int64*)pArg;
32866
- SimulateIOErrorBenign(1);
32867
- winTruncate(id, sz);
32868
- SimulateIOErrorBenign(0);
32869
- return SQLITE_OK;
33160
+ winFile *pFile = (winFile*)id;
33161
+ sqlite3_int64 oldSz;
33162
+ int rc = winFileSize(id, &oldSz);
33163
+ if( rc==SQLITE_OK ){
33164
+ sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
33165
+ if( newSz>oldSz ){
33166
+ SimulateIOErrorBenign(1);
33167
+ rc = winTruncate(id, newSz);
33168
+ SimulateIOErrorBenign(0);
33169
+ }
33170
+ }
33171
+ return rc;
3287033172
}
3287133173
case SQLITE_FCNTL_PERSIST_WAL: {
3287233174
int bPersist = *(int*)pArg;
3287333175
if( bPersist<0 ){
3287433176
*(int*)pArg = pFile->bPersistWal;
@@ -35473,10 +35775,13 @@
3547335775
typedef struct PCache1 PCache1;
3547435776
typedef struct PgHdr1 PgHdr1;
3547535777
typedef struct PgFreeslot PgFreeslot;
3547635778
typedef struct PGroup PGroup;
3547735779
35780
+typedef struct PGroupBlock PGroupBlock;
35781
+typedef struct PGroupBlockList PGroupBlockList;
35782
+
3547835783
/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
3547935784
** of one or more PCaches that are able to recycle each others unpinned
3548035785
** pages when they are under memory pressure. A PGroup is an instance of
3548135786
** the following object.
3548235787
**
@@ -35502,12 +35807,70 @@
3550235807
int nMaxPage; /* Sum of nMax for purgeable caches */
3550335808
int nMinPage; /* Sum of nMin for purgeable caches */
3550435809
int mxPinned; /* nMaxpage + 10 - nMinPage */
3550535810
int nCurrentPage; /* Number of purgeable pages allocated */
3550635811
PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
35812
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
35813
+ int isBusy; /* Do not run ReleaseMemory() if true */
35814
+ PGroupBlockList *pBlockList; /* List of block-lists for this group */
35815
+#endif
3550735816
};
3550835817
35818
+/*
35819
+** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
35820
+** each PGroup structure has a linked list of the the following starting
35821
+** at PGroup.pBlockList. There is one entry for each distinct page-size
35822
+** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
35823
+** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
35824
+** size requested by each pcache, which is the database page-size plus
35825
+** the various header structures used by the pcache, pager and btree layers.
35826
+** Usually around (pgsz+200) bytes.
35827
+**
35828
+** This size (pgsz+200) bytes is not allocated efficiently by some
35829
+** implementations of malloc. In particular, some implementations are only
35830
+** able to allocate blocks of memory chunks of 2^N bytes, where N is some
35831
+** integer value. Since the page-size is a power of 2, this means we
35832
+** end up wasting (pgsz-200) bytes in each allocation.
35833
+**
35834
+** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
35835
+** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes
35836
+** are requested from malloc allocator. After a block is returned,
35837
+** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
35838
+** allocations can fit in the space returned by malloc(). This value may
35839
+** be more than M.
35840
+**
35841
+** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
35842
+** contains the number of allocations that will fit in the aData[] space.
35843
+** nEntry is limited to the number of bits in bitmask mUsed. If a slot
35844
+** within aData is in use, the corresponding bit in mUsed is set. Thus
35845
+** when (mUsed+1==(1 << nEntry)) the block is completely full.
35846
+**
35847
+** Each time a slot within a block is freed, the block is moved to the start
35848
+** of the linked-list. And if a block becomes completely full, then it is
35849
+** moved to the end of the list. As a result, when searching for a free
35850
+** slot, only the first block in the list need be examined. If it is full,
35851
+** then it is guaranteed that all blocks are full.
35852
+*/
35853
+struct PGroupBlockList {
35854
+ int nByte; /* Size of each allocation in bytes */
35855
+ PGroupBlock *pFirst; /* First PGroupBlock in list */
35856
+ PGroupBlock *pLast; /* Last PGroupBlock in list */
35857
+ PGroupBlockList *pNext; /* Next block-list attached to group */
35858
+};
35859
+
35860
+struct PGroupBlock {
35861
+ Bitmask mUsed; /* Mask of used slots */
35862
+ int nEntry; /* Maximum number of allocations in aData[] */
35863
+ u8 *aData; /* Pointer to data block */
35864
+ PGroupBlock *pNext; /* Next PGroupBlock in list */
35865
+ PGroupBlock *pPrev; /* Previous PGroupBlock in list */
35866
+ PGroupBlockList *pList; /* Owner list */
35867
+};
35868
+
35869
+/* Minimum value for PGroupBlock.nEntry */
35870
+#define PAGECACHE_BLOCKALLOC_MINENTRY 15
35871
+
3550935872
/* Each page cache is an instance of the following object. Every
3551035873
** open database file (including each in-memory database and each
3551135874
** temporary or transient database) has a single page cache which
3551235875
** is an instance of this object.
3551335876
**
@@ -35606,10 +35969,21 @@
3560635969
**
3560735970
** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
3560835971
*/
3560935972
#define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
3561035973
#define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35974
+
35975
+/*
35976
+** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve
35977
+** a PGroupBlock pointer based on a pointer to a page buffer.
35978
+*/
35979
+#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
35980
+ ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
35981
+
35982
+#define PAGE_GET_BLOCKPTR(pCache, pPg) \
35983
+ ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
35984
+
3561135985
3561235986
/*
3561335987
** Macros to enter and leave the PCache LRU mutex.
3561435988
*/
3561535989
#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -35732,25 +36106,159 @@
3573236106
return iSize;
3573336107
}
3573436108
}
3573536109
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
3573636110
36111
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36112
+/*
36113
+** The block pBlock belongs to list pList but is not currently linked in.
36114
+** Insert it into the start of the list.
36115
+*/
36116
+static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
36117
+ pBlock->pPrev = 0;
36118
+ pBlock->pNext = pList->pFirst;
36119
+ pList->pFirst = pBlock;
36120
+ if( pBlock->pNext ){
36121
+ pBlock->pNext->pPrev = pBlock;
36122
+ }else{
36123
+ assert( pList->pLast==0 );
36124
+ pList->pLast = pBlock;
36125
+ }
36126
+}
36127
+
36128
+/*
36129
+** If there are no blocks in the list headed by pList, remove pList
36130
+** from the pGroup->pBlockList list and free it with sqlite3_free().
36131
+*/
36132
+static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
36133
+ assert( sqlite3_mutex_held(pGroup->mutex) );
36134
+ if( pList->pFirst==0 ){
36135
+ PGroupBlockList **pp;
36136
+ for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
36137
+ *pp = (*pp)->pNext;
36138
+ sqlite3_free(pList);
36139
+ }
36140
+}
36141
+#endif /* SQLITE_PAGECACHE_BLOCKALLOC */
36142
+
3573736143
/*
3573836144
** Allocate a new page object initially associated with cache pCache.
3573936145
*/
3574036146
static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
3574136147
int nByte = sizeof(PgHdr1) + pCache->szPage;
35742
- void *pPg = pcache1Alloc(nByte);
36148
+ void *pPg = 0;
3574336149
PgHdr1 *p;
36150
+
36151
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36152
+ PGroup *pGroup = pCache->pGroup;
36153
+ PGroupBlockList *pList;
36154
+ PGroupBlock *pBlock;
36155
+ int i;
36156
+
36157
+ nByte += sizeof(PGroupBlockList *);
36158
+ nByte = ROUND8(nByte);
36159
+
36160
+ for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36161
+ if( pList->nByte==nByte ) break;
36162
+ }
36163
+ if( pList==0 ){
36164
+ PGroupBlockList *pNew;
36165
+ assert( pGroup->isBusy==0 );
36166
+ assert( sqlite3_mutex_held(pGroup->mutex) );
36167
+ pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */
36168
+ pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
36169
+ pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */
36170
+ if( pNew==0 ){
36171
+ /* malloc() failure. Return early. */
36172
+ return 0;
36173
+ }
36174
+#ifdef SQLITE_DEBUG
36175
+ for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36176
+ assert( pList->nByte!=nByte );
36177
+ }
36178
+#endif
36179
+ pNew->nByte = nByte;
36180
+ pNew->pNext = pGroup->pBlockList;
36181
+ pGroup->pBlockList = pNew;
36182
+ pList = pNew;
36183
+ }
36184
+
36185
+ pBlock = pList->pFirst;
36186
+ if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
36187
+ int sz;
36188
+
36189
+ /* Allocate a new block. Try to allocate enough space for the PGroupBlock
36190
+ ** structure and MINENTRY allocations of nByte bytes each. If the
36191
+ ** allocator returns more memory than requested, then more than MINENTRY
36192
+ ** allocations may fit in it. */
36193
+ assert( sqlite3_mutex_held(pGroup->mutex) );
36194
+ pcache1LeaveMutex(pCache->pGroup);
36195
+ sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
36196
+ pBlock = (PGroupBlock *)sqlite3Malloc(sz);
36197
+ pcache1EnterMutex(pCache->pGroup);
36198
+
36199
+ if( !pBlock ){
36200
+ freeListIfEmpty(pGroup, pList);
36201
+ return 0;
36202
+ }
36203
+ pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
36204
+ if( pBlock->nEntry>=BMS ){
36205
+ pBlock->nEntry = BMS-1;
36206
+ }
36207
+ pBlock->pList = pList;
36208
+ pBlock->mUsed = 0;
36209
+ pBlock->aData = (u8 *)&pBlock[1];
36210
+ addBlockToList(pList, pBlock);
36211
+
36212
+ sz = sqlite3MallocSize(pBlock);
36213
+ sqlite3_mutex_enter(pcache1.mutex);
36214
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
36215
+ sqlite3_mutex_leave(pcache1.mutex);
36216
+ }
36217
+
36218
+ for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
36219
+ if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
36220
+ pBlock->mUsed |= ((Bitmask)1<<i);
36221
+ pPg = (void *)&pBlock->aData[pList->nByte * i];
36222
+ }
36223
+ }
36224
+ assert( pPg );
36225
+ PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
36226
+
36227
+ /* If the block is now full, shift it to the end of the list */
36228
+ if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
36229
+ assert( pList->pFirst==pBlock );
36230
+ assert( pBlock->pPrev==0 );
36231
+ assert( pList->pLast->pNext==0 );
36232
+ pList->pFirst = pBlock->pNext;
36233
+ pList->pFirst->pPrev = 0;
36234
+ pBlock->pPrev = pList->pLast;
36235
+ pBlock->pNext = 0;
36236
+ pList->pLast->pNext = pBlock;
36237
+ pList->pLast = pBlock;
36238
+ }
36239
+ p = PAGE_TO_PGHDR1(pCache, pPg);
36240
+ if( pCache->bPurgeable ){
36241
+ pCache->pGroup->nCurrentPage++;
36242
+ }
36243
+#else
36244
+ /* The group mutex must be released before pcache1Alloc() is called. This
36245
+ ** is because it may call sqlite3_release_memory(), which assumes that
36246
+ ** this mutex is not held. */
36247
+ assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36248
+ pcache1LeaveMutex(pCache->pGroup);
36249
+ pPg = pcache1Alloc(nByte);
36250
+ pcache1EnterMutex(pCache->pGroup);
3574436251
if( pPg ){
3574536252
p = PAGE_TO_PGHDR1(pCache, pPg);
3574636253
if( pCache->bPurgeable ){
3574736254
pCache->pGroup->nCurrentPage++;
3574836255
}
3574936256
}else{
3575036257
p = 0;
3575136258
}
36259
+#endif
3575236260
return p;
3575336261
}
3575436262
3575536263
/*
3575636264
** Free a page object allocated by pcache1AllocPage().
@@ -35760,14 +36268,56 @@
3576036268
** with a NULL pointer, so we mark the NULL test with ALWAYS().
3576136269
*/
3576236270
static void pcache1FreePage(PgHdr1 *p){
3576336271
if( ALWAYS(p) ){
3576436272
PCache1 *pCache = p->pCache;
36273
+ void *pPg = PGHDR1_TO_PAGE(p);
36274
+
36275
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36276
+ PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
36277
+ PGroupBlockList *pList = pBlock->pList;
36278
+ int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
36279
+
36280
+ assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
36281
+ assert( pBlock->mUsed & ((Bitmask)1<<i) );
36282
+ pBlock->mUsed &= ~((Bitmask)1<<i);
36283
+
36284
+ /* Remove the block from the list. If it is completely empty, free it.
36285
+ ** Or if it is not completely empty, re-insert it at the start of the
36286
+ ** list. */
36287
+ if( pList->pFirst==pBlock ){
36288
+ pList->pFirst = pBlock->pNext;
36289
+ if( pList->pFirst ) pList->pFirst->pPrev = 0;
36290
+ }else{
36291
+ pBlock->pPrev->pNext = pBlock->pNext;
36292
+ }
36293
+ if( pList->pLast==pBlock ){
36294
+ pList->pLast = pBlock->pPrev;
36295
+ if( pList->pLast ) pList->pLast->pNext = 0;
36296
+ }else{
36297
+ pBlock->pNext->pPrev = pBlock->pPrev;
36298
+ }
36299
+
36300
+ if( pBlock->mUsed==0 ){
36301
+ PGroup *pGroup = p->pCache->pGroup;
36302
+
36303
+ int sz = sqlite3MallocSize(pBlock);
36304
+ sqlite3_mutex_enter(pcache1.mutex);
36305
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
36306
+ sqlite3_mutex_leave(pcache1.mutex);
36307
+ freeListIfEmpty(pGroup, pList);
36308
+ sqlite3_free(pBlock);
36309
+ }else{
36310
+ addBlockToList(pList, pBlock);
36311
+ }
36312
+#else
36313
+ assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36314
+ pcache1Free(pPg);
36315
+#endif
3576536316
if( pCache->bPurgeable ){
3576636317
pCache->pGroup->nCurrentPage--;
3576736318
}
35768
- pcache1Free(PGHDR1_TO_PAGE(p));
3576936319
}
3577036320
}
3577136321
3577236322
/*
3577336323
** Malloc function used by SQLite to obtain space from the buffer configured
@@ -36201,13 +36751,11 @@
3620136751
/* Step 5. If a usable page buffer has still not been found,
3620236752
** attempt to allocate a new one.
3620336753
*/
3620436754
if( !pPage ){
3620536755
if( createFlag==1 ) sqlite3BeginBenignMalloc();
36206
- pcache1LeaveMutex(pGroup);
3620736756
pPage = pcache1AllocPage(pCache);
36208
- pcache1EnterMutex(pGroup);
3620936757
if( createFlag==1 ) sqlite3EndBenignMalloc();
3621036758
}
3621136759
3621236760
if( pPage ){
3621336761
unsigned int h = iKey % pCache->nHash;
@@ -36373,10 +36921,13 @@
3637336921
** been released, the function returns. The return value is the total number
3637436922
** of bytes of memory released.
3637536923
*/
3637636924
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
3637736925
int nFree = 0;
36926
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36927
+ if( pcache1.grp.isBusy ) return 0;
36928
+#endif
3637836929
assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
3637936930
assert( sqlite3_mutex_notheld(pcache1.mutex) );
3638036931
if( pcache1.pStart==0 ){
3638136932
PgHdr1 *p;
3638236933
pcache1EnterMutex(&pcache1.grp);
@@ -37585,10 +38136,12 @@
3758538136
u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
3758638137
u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
3758738138
u8 tempFile; /* zFilename is a temporary file */
3758838139
u8 readOnly; /* True for a read-only database */
3758938140
u8 memDb; /* True to inhibit all file I/O */
38141
+ u8 hasSeenStress; /* pagerStress() called one or more times */
38142
+ u8 isSorter; /* True for a PAGER_SORTER */
3759038143
3759138144
/**************************************************************************
3759238145
** The following block contains those class members that change during
3759338146
** routine opertion. Class members not in this block are either fixed
3759438147
** when the pager is first created or else only change when there is a
@@ -37807,10 +38360,19 @@
3780738360
|| p->journalMode==PAGER_JOURNALMODE_MEMORY
3780838361
);
3780938362
assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
3781038363
assert( pagerUseWal(p)==0 );
3781138364
}
38365
+
38366
+ /* A sorter is a temp file that never spills to disk and always has
38367
+ ** the doNotSpill flag set
38368
+ */
38369
+ if( p->isSorter ){
38370
+ assert( p->tempFile );
38371
+ assert( p->doNotSpill );
38372
+ assert( p->fd->pMethods==0 );
38373
+ }
3781238374
3781338375
/* If changeCountDone is set, a RESERVED lock or greater must be held
3781438376
** on the file.
3781538377
*/
3781638378
assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
@@ -40704,10 +41266,11 @@
4070441266
** to the caller.
4070541267
*/
4070641268
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
4070741269
u8 *pTmp = (u8 *)pPager->pTmpSpace;
4070841270
41271
+ assert( assert_pager_state(pPager) );
4070941272
disable_simulated_io_errors();
4071041273
sqlite3BeginBenignMalloc();
4071141274
/* pPager->errCode = 0; */
4071241275
pPager->exclusiveMode = 0;
4071341276
#ifndef SQLITE_OMIT_WAL
@@ -41138,10 +41701,11 @@
4113841701
** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
4113941702
** while in the error state, hence it is impossible for this routine to
4114041703
** be called in the error state. Nevertheless, we include a NEVER()
4114141704
** test for the error state as a safeguard against future changes.
4114241705
*/
41706
+ pPager->hasSeenStress = 1;
4114341707
if( NEVER(pPager->errCode) ) return SQLITE_OK;
4114441708
if( pPager->doNotSpill ) return SQLITE_OK;
4114541709
if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
4114641710
return SQLITE_OK;
4114741711
}
@@ -41509,10 +42073,16 @@
4150942073
}
4151042074
/* pPager->xBusyHandler = 0; */
4151142075
/* pPager->pBusyHandlerArg = 0; */
4151242076
pPager->xReiniter = xReinit;
4151342077
/* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
42078
+#ifndef SQLITE_OMIT_MERGE_SORT
42079
+ if( flags & PAGER_SORTER ){
42080
+ pPager->doNotSpill = 1;
42081
+ pPager->isSorter = 1;
42082
+ }
42083
+#endif
4151442084
4151542085
*ppPager = pPager;
4151642086
return SQLITE_OK;
4151742087
}
4151842088
@@ -43052,10 +43622,21 @@
4305243622
** Return true if this is an in-memory pager.
4305343623
*/
4305443624
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
4305543625
return MEMDB;
4305643626
}
43627
+
43628
+#ifndef SQLITE_OMIT_MERGE_SORT
43629
+/*
43630
+** Return true if the pager has seen a pagerStress callback.
43631
+*/
43632
+SQLITE_PRIVATE int sqlite3PagerUnderStress(Pager *pPager){
43633
+ assert( pPager->isSorter );
43634
+ assert( pPager->doNotSpill );
43635
+ return pPager->hasSeenStress;
43636
+}
43637
+#endif
4305743638
4305843639
/*
4305943640
** Check that there are at least nSavepoint savepoints open. If there are
4306043641
** currently less than nSavepoints open, then open one or more savepoints
4306143642
** to make up the difference. If the number of savepoints is already
@@ -49421,15 +50002,26 @@
4942150002
assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
4942250003
4942350004
/* A BTREE_SINGLE database is always a temporary and/or ephemeral */
4942450005
assert( (flags & BTREE_SINGLE)==0 || isTempDb );
4942550006
50007
+ /* The BTREE_SORTER flag is only used if SQLITE_OMIT_MERGE_SORT is undef */
50008
+#ifdef SQLITE_OMIT_MERGE_SORT
50009
+ assert( (flags & BTREE_SORTER)==0 );
50010
+#endif
50011
+
50012
+ /* BTREE_SORTER is always on a BTREE_SINGLE, BTREE_OMIT_JOURNAL */
50013
+ assert( (flags & BTREE_SORTER)==0 ||
50014
+ (flags & (BTREE_SINGLE|BTREE_OMIT_JOURNAL))
50015
+ ==(BTREE_SINGLE|BTREE_OMIT_JOURNAL) );
50016
+
4942650017
if( db->flags & SQLITE_NoReadlock ){
4942750018
flags |= BTREE_NO_READLOCK;
4942850019
}
4942950020
if( isMemdb ){
4943050021
flags |= BTREE_MEMORY;
50022
+ flags &= ~BTREE_SORTER;
4943150023
}
4943250024
if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
4943350025
vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
4943450026
}
4943550027
p = sqlite3MallocZero(sizeof(Btree));
@@ -51155,11 +51747,12 @@
5115551747
5115651748
if( NEVER(wrFlag && pBt->readOnly) ){
5115751749
return SQLITE_READONLY;
5115851750
}
5115951751
if( iTable==1 && btreePagecount(pBt)==0 ){
51160
- return SQLITE_EMPTY;
51752
+ assert( wrFlag==0 );
51753
+ iTable = 0;
5116151754
}
5116251755
5116351756
/* Now that no other errors can occur, finish filling in the BtCursor
5116451757
** variables and link the cursor into the BtShared list. */
5116551758
pCur->pgnoRoot = (Pgno)iTable;
@@ -51909,10 +52502,13 @@
5190952502
int i;
5191052503
for(i=1; i<=pCur->iPage; i++){
5191152504
releasePage(pCur->apPage[i]);
5191252505
}
5191352506
pCur->iPage = 0;
52507
+ }else if( pCur->pgnoRoot==0 ){
52508
+ pCur->eState = CURSOR_INVALID;
52509
+ return SQLITE_OK;
5191452510
}else{
5191552511
rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
5191652512
if( rc!=SQLITE_OK ){
5191752513
pCur->eState = CURSOR_INVALID;
5191852514
return rc;
@@ -52018,11 +52614,11 @@
5201852614
assert( cursorHoldsMutex(pCur) );
5201952615
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5202052616
rc = moveToRoot(pCur);
5202152617
if( rc==SQLITE_OK ){
5202252618
if( pCur->eState==CURSOR_INVALID ){
52023
- assert( pCur->apPage[pCur->iPage]->nCell==0 );
52619
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5202452620
*pRes = 1;
5202552621
}else{
5202652622
assert( pCur->apPage[pCur->iPage]->nCell>0 );
5202752623
*pRes = 0;
5202852624
rc = moveToLeftmost(pCur);
@@ -52057,11 +52653,11 @@
5205752653
}
5205852654
5205952655
rc = moveToRoot(pCur);
5206052656
if( rc==SQLITE_OK ){
5206152657
if( CURSOR_INVALID==pCur->eState ){
52062
- assert( pCur->apPage[pCur->iPage]->nCell==0 );
52658
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5206352659
*pRes = 1;
5206452660
}else{
5206552661
assert( pCur->eState==CURSOR_VALID );
5206652662
*pRes = 0;
5206752663
rc = moveToRightmost(pCur);
@@ -52130,16 +52726,16 @@
5213052726
5213152727
rc = moveToRoot(pCur);
5213252728
if( rc ){
5213352729
return rc;
5213452730
}
52135
- assert( pCur->apPage[pCur->iPage] );
52136
- assert( pCur->apPage[pCur->iPage]->isInit );
52137
- assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID );
52731
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
52732
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
52733
+ assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
5213852734
if( pCur->eState==CURSOR_INVALID ){
5213952735
*pRes = -1;
52140
- assert( pCur->apPage[pCur->iPage]->nCell==0 );
52736
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5214152737
return SQLITE_OK;
5214252738
}
5214352739
assert( pCur->apPage[0]->intKey || pIdxKey );
5214452740
for(;;){
5214552741
int lwr, upr, idx;
@@ -54964,13 +55560,20 @@
5496455560
releasePage(pPage);
5496555561
}
5496655562
return rc;
5496755563
}
5496855564
SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
55565
+ BtShared *pBt = p->pBt;
5496955566
int rc;
5497055567
sqlite3BtreeEnter(p);
54971
- rc = btreeDropTable(p, iTable, piMoved);
55568
+ if( (pBt->openFlags&BTREE_SINGLE) ){
55569
+ pBt->nPage = 0;
55570
+ sqlite3PagerTruncateImage(pBt->pPager, 1);
55571
+ rc = newDatabase(pBt);
55572
+ }else{
55573
+ rc = btreeDropTable(p, iTable, piMoved);
55574
+ }
5497255575
sqlite3BtreeLeave(p);
5497355576
return rc;
5497455577
}
5497555578
5497655579
@@ -55045,10 +55648,15 @@
5504555648
** corruption) an SQLite error code is returned.
5504655649
*/
5504755650
SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
5504855651
i64 nEntry = 0; /* Value to return in *pnEntry */
5504955652
int rc; /* Return code */
55653
+
55654
+ if( pCur->pgnoRoot==0 ){
55655
+ *pnEntry = 0;
55656
+ return SQLITE_OK;
55657
+ }
5505055658
rc = moveToRoot(pCur);
5505155659
5505255660
/* Unless an error occurs, the following loop runs one iteration for each
5505355661
** page in the B-Tree structure (not including overflow pages).
5505455662
*/
@@ -55829,11 +56437,10 @@
5582956437
*/
5583056438
SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
5583156439
BtShared *pBt = pBtree->pBt;
5583256440
int rc; /* Return code */
5583356441
55834
- assert( pBtree->inTrans==TRANS_NONE );
5583556442
assert( iVersion==1 || iVersion==2 );
5583656443
5583756444
/* If setting the version fields to 1, do not automatically open the
5583856445
** WAL connection, even if the version fields are currently set to 2.
5583956446
*/
@@ -56268,106 +56875,110 @@
5626856875
/* Update the schema version field in the destination database. This
5626956876
** is to make sure that the schema-version really does change in
5627056877
** the case where the source and destination databases have the
5627156878
** same schema version.
5627256879
*/
56273
- if( rc==SQLITE_DONE
56274
- && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
56275
- ){
56276
- int nDestTruncate;
56277
-
56278
- if( p->pDestDb ){
56279
- sqlite3ResetInternalSchema(p->pDestDb, -1);
56280
- }
56281
-
56282
- /* Set nDestTruncate to the final number of pages in the destination
56283
- ** database. The complication here is that the destination page
56284
- ** size may be different to the source page size.
56285
- **
56286
- ** If the source page size is smaller than the destination page size,
56287
- ** round up. In this case the call to sqlite3OsTruncate() below will
56288
- ** fix the size of the file. However it is important to call
56289
- ** sqlite3PagerTruncateImage() here so that any pages in the
56290
- ** destination file that lie beyond the nDestTruncate page mark are
56291
- ** journalled by PagerCommitPhaseOne() before they are destroyed
56292
- ** by the file truncation.
56293
- */
56294
- assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56295
- assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56296
- if( pgszSrc<pgszDest ){
56297
- int ratio = pgszDest/pgszSrc;
56298
- nDestTruncate = (nSrcPage+ratio-1)/ratio;
56299
- if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56300
- nDestTruncate--;
56301
- }
56302
- }else{
56303
- nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56304
- }
56305
- sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56306
-
56307
- if( pgszSrc<pgszDest ){
56308
- /* If the source page-size is smaller than the destination page-size,
56309
- ** two extra things may need to happen:
56310
- **
56311
- ** * The destination may need to be truncated, and
56312
- **
56313
- ** * Data stored on the pages immediately following the
56314
- ** pending-byte page in the source database may need to be
56315
- ** copied into the destination database.
56316
- */
56317
- const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56318
- sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56319
- i64 iOff;
56320
- i64 iEnd;
56321
-
56322
- assert( pFile );
56323
- assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56324
- nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56325
- && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56326
- ));
56327
-
56328
- /* This call ensures that all data required to recreate the original
56329
- ** database has been stored in the journal for pDestPager and the
56330
- ** journal synced to disk. So at this point we may safely modify
56331
- ** the database file in any way, knowing that if a power failure
56332
- ** occurs, the original database will be reconstructed from the
56333
- ** journal file. */
56334
- rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56335
-
56336
- /* Write the extra pages and truncate the database file as required. */
56337
- iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56338
- for(
56339
- iOff=PENDING_BYTE+pgszSrc;
56340
- rc==SQLITE_OK && iOff<iEnd;
56341
- iOff+=pgszSrc
56342
- ){
56343
- PgHdr *pSrcPg = 0;
56344
- const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56345
- rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56346
- if( rc==SQLITE_OK ){
56347
- u8 *zData = sqlite3PagerGetData(pSrcPg);
56348
- rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56349
- }
56350
- sqlite3PagerUnref(pSrcPg);
56351
- }
56352
- if( rc==SQLITE_OK ){
56353
- rc = backupTruncateFile(pFile, iSize);
56354
- }
56355
-
56356
- /* Sync the database file to disk. */
56357
- if( rc==SQLITE_OK ){
56358
- rc = sqlite3PagerSync(pDestPager);
56359
- }
56360
- }else{
56361
- rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56362
- }
56363
-
56364
- /* Finish committing the transaction to the destination database. */
56365
- if( SQLITE_OK==rc
56366
- && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56367
- ){
56368
- rc = SQLITE_DONE;
56880
+ if( rc==SQLITE_DONE ){
56881
+ rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
56882
+ if( rc==SQLITE_OK ){
56883
+ if( p->pDestDb ){
56884
+ sqlite3ResetInternalSchema(p->pDestDb, -1);
56885
+ }
56886
+ if( destMode==PAGER_JOURNALMODE_WAL ){
56887
+ rc = sqlite3BtreeSetVersion(p->pDest, 2);
56888
+ }
56889
+ }
56890
+ if( rc==SQLITE_OK ){
56891
+ int nDestTruncate;
56892
+ /* Set nDestTruncate to the final number of pages in the destination
56893
+ ** database. The complication here is that the destination page
56894
+ ** size may be different to the source page size.
56895
+ **
56896
+ ** If the source page size is smaller than the destination page size,
56897
+ ** round up. In this case the call to sqlite3OsTruncate() below will
56898
+ ** fix the size of the file. However it is important to call
56899
+ ** sqlite3PagerTruncateImage() here so that any pages in the
56900
+ ** destination file that lie beyond the nDestTruncate page mark are
56901
+ ** journalled by PagerCommitPhaseOne() before they are destroyed
56902
+ ** by the file truncation.
56903
+ */
56904
+ assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56905
+ assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56906
+ if( pgszSrc<pgszDest ){
56907
+ int ratio = pgszDest/pgszSrc;
56908
+ nDestTruncate = (nSrcPage+ratio-1)/ratio;
56909
+ if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56910
+ nDestTruncate--;
56911
+ }
56912
+ }else{
56913
+ nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56914
+ }
56915
+ sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56916
+
56917
+ if( pgszSrc<pgszDest ){
56918
+ /* If the source page-size is smaller than the destination page-size,
56919
+ ** two extra things may need to happen:
56920
+ **
56921
+ ** * The destination may need to be truncated, and
56922
+ **
56923
+ ** * Data stored on the pages immediately following the
56924
+ ** pending-byte page in the source database may need to be
56925
+ ** copied into the destination database.
56926
+ */
56927
+ const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56928
+ sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56929
+ i64 iOff;
56930
+ i64 iEnd;
56931
+
56932
+ assert( pFile );
56933
+ assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56934
+ nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56935
+ && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56936
+ ));
56937
+
56938
+ /* This call ensures that all data required to recreate the original
56939
+ ** database has been stored in the journal for pDestPager and the
56940
+ ** journal synced to disk. So at this point we may safely modify
56941
+ ** the database file in any way, knowing that if a power failure
56942
+ ** occurs, the original database will be reconstructed from the
56943
+ ** journal file. */
56944
+ rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56945
+
56946
+ /* Write the extra pages and truncate the database file as required */
56947
+ iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56948
+ for(
56949
+ iOff=PENDING_BYTE+pgszSrc;
56950
+ rc==SQLITE_OK && iOff<iEnd;
56951
+ iOff+=pgszSrc
56952
+ ){
56953
+ PgHdr *pSrcPg = 0;
56954
+ const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56955
+ rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56956
+ if( rc==SQLITE_OK ){
56957
+ u8 *zData = sqlite3PagerGetData(pSrcPg);
56958
+ rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56959
+ }
56960
+ sqlite3PagerUnref(pSrcPg);
56961
+ }
56962
+ if( rc==SQLITE_OK ){
56963
+ rc = backupTruncateFile(pFile, iSize);
56964
+ }
56965
+
56966
+ /* Sync the database file to disk. */
56967
+ if( rc==SQLITE_OK ){
56968
+ rc = sqlite3PagerSync(pDestPager);
56969
+ }
56970
+ }else{
56971
+ rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56972
+ }
56973
+
56974
+ /* Finish committing the transaction to the destination database. */
56975
+ if( SQLITE_OK==rc
56976
+ && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56977
+ ){
56978
+ rc = SQLITE_DONE;
56979
+ }
5636956980
}
5637056981
}
5637156982
5637256983
/* If bCloseTrans is true, then this function opened a read transaction
5637356984
** on the source database. Close the read transaction here. There is
@@ -56831,38 +57442,32 @@
5683157442
** invoking an external callback, free it now. Calling this function
5683257443
** does not free any Mem.zMalloc buffer.
5683357444
*/
5683457445
SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
5683557446
assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
56836
- testcase( p->flags & MEM_Agg );
56837
- testcase( p->flags & MEM_Dyn );
56838
- testcase( p->flags & MEM_RowSet );
56839
- testcase( p->flags & MEM_Frame );
56840
- if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
56841
- if( p->flags&MEM_Agg ){
56842
- sqlite3VdbeMemFinalize(p, p->u.pDef);
56843
- assert( (p->flags & MEM_Agg)==0 );
56844
- sqlite3VdbeMemRelease(p);
56845
- }else if( p->flags&MEM_Dyn && p->xDel ){
56846
- assert( (p->flags&MEM_RowSet)==0 );
56847
- p->xDel((void *)p->z);
56848
- p->xDel = 0;
56849
- }else if( p->flags&MEM_RowSet ){
56850
- sqlite3RowSetClear(p->u.pRowSet);
56851
- }else if( p->flags&MEM_Frame ){
56852
- sqlite3VdbeMemSetNull(p);
56853
- }
57447
+ if( p->flags&MEM_Agg ){
57448
+ sqlite3VdbeMemFinalize(p, p->u.pDef);
57449
+ assert( (p->flags & MEM_Agg)==0 );
57450
+ sqlite3VdbeMemRelease(p);
57451
+ }else if( p->flags&MEM_Dyn && p->xDel ){
57452
+ assert( (p->flags&MEM_RowSet)==0 );
57453
+ p->xDel((void *)p->z);
57454
+ p->xDel = 0;
57455
+ }else if( p->flags&MEM_RowSet ){
57456
+ sqlite3RowSetClear(p->u.pRowSet);
57457
+ }else if( p->flags&MEM_Frame ){
57458
+ sqlite3VdbeMemSetNull(p);
5685457459
}
5685557460
}
5685657461
5685757462
/*
5685857463
** Release any memory held by the Mem. This may leave the Mem in an
5685957464
** inconsistent state, for example with (Mem.z==0) and
5686057465
** (Mem.type==SQLITE_TEXT).
5686157466
*/
5686257467
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
56863
- sqlite3VdbeMemReleaseExternal(p);
57468
+ MemReleaseExt(p);
5686457469
sqlite3DbFree(p->db, p->zMalloc);
5686557470
p->z = 0;
5686657471
p->zMalloc = 0;
5686757472
p->xDel = 0;
5686857473
}
@@ -57180,11 +57785,11 @@
5718057785
** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
5718157786
** and flags gets srcType (either MEM_Ephem or MEM_Static).
5718257787
*/
5718357788
SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
5718457789
assert( (pFrom->flags & MEM_RowSet)==0 );
57185
- sqlite3VdbeMemReleaseExternal(pTo);
57790
+ MemReleaseExt(pTo);
5718657791
memcpy(pTo, pFrom, MEMCELLSIZE);
5718757792
pTo->xDel = 0;
5718857793
if( (pFrom->flags&MEM_Static)==0 ){
5718957794
pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
5719057795
assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57198,11 +57803,11 @@
5719857803
*/
5719957804
SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
5720057805
int rc = SQLITE_OK;
5720157806
5720257807
assert( (pFrom->flags & MEM_RowSet)==0 );
57203
- sqlite3VdbeMemReleaseExternal(pTo);
57808
+ MemReleaseExt(pTo);
5720457809
memcpy(pTo, pFrom, MEMCELLSIZE);
5720557810
pTo->flags &= ~MEM_Dyn;
5720657811
5720757812
if( pTo->flags&(MEM_Str|MEM_Blob) ){
5720857813
if( 0==(pFrom->flags&MEM_Static) ){
@@ -57592,15 +58197,15 @@
5759258197
*ppVal = 0;
5759358198
return SQLITE_OK;
5759458199
}
5759558200
op = pExpr->op;
5759658201
57597
- /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
58202
+ /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
5759858203
** The ifdef here is to enable us to achieve 100% branch test coverage even
57599
- ** when SQLITE_ENABLE_STAT3 is omitted.
58204
+ ** when SQLITE_ENABLE_STAT2 is omitted.
5760058205
*/
57601
-#ifdef SQLITE_ENABLE_STAT3
58206
+#ifdef SQLITE_ENABLE_STAT2
5760258207
if( op==TK_REGISTER ) op = pExpr->op2;
5760358208
#else
5760458209
if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
5760558210
#endif
5760658211
@@ -58153,10 +58758,16 @@
5815358758
assert( p->nOp - i >= 3 );
5815458759
assert( pOp[-1].opcode==OP_Integer );
5815558760
n = pOp[-1].p1;
5815658761
if( n>nMaxArgs ) nMaxArgs = n;
5815758762
#endif
58763
+ }else if( opcode==OP_Next ){
58764
+ pOp->p4.xAdvance = sqlite3BtreeNext;
58765
+ pOp->p4type = P4_ADVANCE;
58766
+ }else if( opcode==OP_Prev ){
58767
+ pOp->p4.xAdvance = sqlite3BtreePrevious;
58768
+ pOp->p4type = P4_ADVANCE;
5815858769
}
5815958770
5816058771
if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
5816158772
assert( -1-pOp->p2<p->nLabel );
5816258773
pOp->p2 = aLabel[-1-pOp->p2];
@@ -58244,37 +58855,34 @@
5824458855
** Change the value of the P1 operand for a specific instruction.
5824558856
** This routine is useful when a large program is loaded from a
5824658857
** static array using sqlite3VdbeAddOpList but we want to make a
5824758858
** few minor changes to the program.
5824858859
*/
58249
-SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
58860
+SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
5825058861
assert( p!=0 );
58251
- assert( addr>=0 );
58252
- if( p->nOp>addr ){
58862
+ if( ((u32)p->nOp)>addr ){
5825358863
p->aOp[addr].p1 = val;
5825458864
}
5825558865
}
5825658866
5825758867
/*
5825858868
** Change the value of the P2 operand for a specific instruction.
5825958869
** This routine is useful for setting a jump destination.
5826058870
*/
58261
-SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
58871
+SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
5826258872
assert( p!=0 );
58263
- assert( addr>=0 );
58264
- if( p->nOp>addr ){
58873
+ if( ((u32)p->nOp)>addr ){
5826558874
p->aOp[addr].p2 = val;
5826658875
}
5826758876
}
5826858877
5826958878
/*
5827058879
** Change the value of the P3 operand for a specific instruction.
5827158880
*/
58272
-SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
58881
+SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
5827358882
assert( p!=0 );
58274
- assert( addr>=0 );
58275
- if( p->nOp>addr ){
58883
+ if( ((u32)p->nOp)>addr ){
5827658884
p->aOp[addr].p3 = val;
5827758885
}
5827858886
}
5827958887
5828058888
/*
@@ -58292,12 +58900,12 @@
5829258900
/*
5829358901
** Change the P2 operand of instruction addr so that it points to
5829458902
** the address of the next instruction to be coded.
5829558903
*/
5829658904
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58297
- assert( addr>=0 || p->db->mallocFailed );
58298
- if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
58905
+ assert( addr>=0 );
58906
+ sqlite3VdbeChangeP2(p, addr, p->nOp);
5829958907
}
5830058908
5830158909
5830258910
/*
5830358911
** If the input FuncDef structure is ephemeral, then free it. If
@@ -58661,10 +59269,14 @@
5866159269
break;
5866259270
}
5866359271
case P4_SUBPROGRAM: {
5866459272
sqlite3_snprintf(nTemp, zTemp, "program");
5866559273
break;
59274
+ }
59275
+ case P4_ADVANCE: {
59276
+ zTemp[0] = 0;
59277
+ break;
5866659278
}
5866759279
default: {
5866859280
zP4 = pOp->p4.z;
5866959281
if( zP4==0 ){
5867059282
zP4 = zTemp;
@@ -59285,10 +59897,11 @@
5928559897
*/
5928659898
SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
5928759899
if( pCx==0 ){
5928859900
return;
5928959901
}
59902
+ sqlite3VdbeSorterClose(p->db, pCx);
5929059903
if( pCx->pBt ){
5929159904
sqlite3BtreeClose(pCx->pBt);
5929259905
/* The pCx->pCursor will be close automatically, if it exists, by
5929359906
** the call above. */
5929459907
}else if( pCx->pCursor ){
@@ -62585,10 +63198,17 @@
6258563198
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
6258663199
** P if required.
6258763200
*/
6258863201
#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
6258963202
63203
+/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
63204
+#ifdef SQLITE_OMIT_MERGE_SORT
63205
+# define isSorter(x) 0
63206
+#else
63207
+# define isSorter(x) ((x)->pSorter!=0)
63208
+#endif
63209
+
6259063210
/*
6259163211
** Argument pMem points at a register that will be passed to a
6259263212
** user-defined function or returned to the user as the result of a query.
6259363213
** This routine sets the pMem->type variable used by the sqlite3_value_*()
6259463214
** routines.
@@ -63179,10 +63799,11 @@
6317963799
u8 *zEndHdr; /* Pointer to first byte after the header */
6318063800
u32 offset; /* Offset into the data */
6318163801
u32 szField; /* Number of bytes in the content of a field */
6318263802
int szHdr; /* Size of the header size field at start of record */
6318363803
int avail; /* Number of bytes of available data */
63804
+ u32 t; /* A type code from the record header */
6318463805
Mem *pReg; /* PseudoTable input register */
6318563806
} am;
6318663807
struct OP_Affinity_stack_vars {
6318763808
const char *zAffinity; /* The affinity to be applied */
6318863809
char cAff; /* A single character of affinity */
@@ -63337,11 +63958,10 @@
6333763958
BtCursor *pCrsr;
6333863959
int res;
6333963960
} bl;
6334063961
struct OP_Next_stack_vars {
6334163962
VdbeCursor *pC;
63342
- BtCursor *pCrsr;
6334363963
int res;
6334463964
} bm;
6334563965
struct OP_IdxInsert_stack_vars {
6334663966
VdbeCursor *pC;
6334763967
BtCursor *pCrsr;
@@ -63591,11 +64211,11 @@
6359164211
if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
6359264212
assert( pOp->p2>0 );
6359364213
assert( pOp->p2<=p->nMem );
6359464214
pOut = &aMem[pOp->p2];
6359564215
memAboutToChange(p, pOut);
63596
- sqlite3VdbeMemReleaseExternal(pOut);
64216
+ MemReleaseExt(pOut);
6359764217
pOut->flags = MEM_Int;
6359864218
}
6359964219
6360064220
/* Sanity checking on other operands */
6360164221
#ifdef SQLITE_DEBUG
@@ -65061,10 +65681,11 @@
6506165681
u8 *zEndHdr; /* Pointer to first byte after the header */
6506265682
u32 offset; /* Offset into the data */
6506365683
u32 szField; /* Number of bytes in the content of a field */
6506465684
int szHdr; /* Size of the header size field at start of record */
6506565685
int avail; /* Number of bytes of available data */
65686
+ u32 t; /* A type code from the record header */
6506665687
Mem *pReg; /* PseudoTable input register */
6506765688
#endif /* local variables moved into u.am */
6506865689
6506965690
6507065691
u.am.p1 = pOp->p1;
@@ -65073,11 +65694,10 @@
6507365694
memset(&u.am.sMem, 0, sizeof(u.am.sMem));
6507465695
assert( u.am.p1<p->nCursor );
6507565696
assert( pOp->p3>0 && pOp->p3<=p->nMem );
6507665697
u.am.pDest = &aMem[pOp->p3];
6507765698
memAboutToChange(p, u.am.pDest);
65078
- MemSetTypeFlag(u.am.pDest, MEM_Null);
6507965699
u.am.zRec = 0;
6508065700
6508165701
/* This block sets the variable u.am.payloadSize to be the total number of
6508265702
** bytes in the record.
6508365703
**
@@ -65117,11 +65737,11 @@
6511765737
}else{
6511865738
assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
6511965739
rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
6512065740
assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
6512165741
}
65122
- }else if( u.am.pC->pseudoTableReg>0 ){
65742
+ }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
6512365743
u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
6512465744
assert( u.am.pReg->flags & MEM_Blob );
6512565745
assert( memIsValid(u.am.pReg) );
6512665746
u.am.payloadSize = u.am.pReg->n;
6512765747
u.am.zRec = u.am.pReg->z;
@@ -65130,13 +65750,14 @@
6513065750
}else{
6513165751
/* Consider the row to be NULL */
6513265752
u.am.payloadSize = 0;
6513365753
}
6513465754
65135
- /* If u.am.payloadSize is 0, then just store a NULL */
65755
+ /* If u.am.payloadSize is 0, then just store a NULL. This can happen because of
65756
+ ** nullRow or because of a corrupt database. */
6513665757
if( u.am.payloadSize==0 ){
65137
- assert( u.am.pDest->flags&MEM_Null );
65758
+ MemSetTypeFlag(u.am.pDest, MEM_Null);
6513865759
goto op_column_out;
6513965760
}
6514065761
assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
6514165762
if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
6514265763
goto too_big;
@@ -65239,12 +65860,18 @@
6523965860
** of the record to the start of the data for the u.am.i-th column
6524065861
*/
6524165862
for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
6524265863
if( u.am.zIdx<u.am.zEndHdr ){
6524365864
u.am.aOffset[u.am.i] = u.am.offset;
65244
- u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]);
65245
- u.am.szField = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]);
65865
+ if( u.am.zIdx[0]<0x80 ){
65866
+ u.am.t = u.am.zIdx[0];
65867
+ u.am.zIdx++;
65868
+ }else{
65869
+ u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t);
65870
+ }
65871
+ u.am.aType[u.am.i] = u.am.t;
65872
+ u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t);
6524665873
u.am.offset += u.am.szField;
6524765874
if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */
6524865875
u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
6524965876
break;
6525065877
}
@@ -65281,11 +65908,11 @@
6528165908
** a pointer to a Mem object.
6528265909
*/
6528365910
if( u.am.aOffset[u.am.p2] ){
6528465911
assert( rc==SQLITE_OK );
6528565912
if( u.am.zRec ){
65286
- sqlite3VdbeMemReleaseExternal(u.am.pDest);
65913
+ MemReleaseExt(u.am.pDest);
6528765914
sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
6528865915
}else{
6528965916
u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
6529065917
sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
6529165918
rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
@@ -65298,11 +65925,11 @@
6529865925
u.am.pDest->enc = encoding;
6529965926
}else{
6530065927
if( pOp->p4type==P4_MEM ){
6530165928
sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
6530265929
}else{
65303
- assert( u.am.pDest->flags&MEM_Null );
65930
+ MemSetTypeFlag(u.am.pDest, MEM_Null);
6530465931
}
6530565932
}
6530665933
6530765934
/* If we dynamically allocated space to hold the data (in the
6530865935
** sqlite3VdbeMemFromBtree() call above) then transfer control of that
@@ -65500,11 +66127,11 @@
6550066127
i64 nEntry;
6550166128
BtCursor *pCrsr;
6550266129
#endif /* local variables moved into u.ap */
6550366130
6550466131
u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
65505
- if( u.ap.pCrsr ){
66132
+ if( ALWAYS(u.ap.pCrsr) ){
6550666133
rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
6550766134
}else{
6550866135
u.ap.nEntry = 0;
6550966136
}
6551066137
pOut->u.i = u.ap.nEntry;
@@ -66076,19 +66703,13 @@
6607666703
u.aw.pCur->nullRow = 1;
6607766704
u.aw.pCur->isOrdered = 1;
6607866705
rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
6607966706
u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
6608066707
66081
- /* Since it performs no memory allocation or IO, the only values that
66082
- ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK.
66083
- ** SQLITE_EMPTY is only returned when attempting to open the table
66084
- ** rooted at page 1 of a zero-byte database. */
66085
- assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
66086
- if( rc==SQLITE_EMPTY ){
66087
- u.aw.pCur->pCursor = 0;
66088
- rc = SQLITE_OK;
66089
- }
66708
+ /* Since it performs no memory allocation or IO, the only value that
66709
+ ** sqlite3BtreeCursor() may return is SQLITE_OK. */
66710
+ assert( rc==SQLITE_OK );
6609066711
6609166712
/* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
6609266713
** SQLite used to check if the root-page flags were sane at this point
6609366714
** and report database corruption if they were not, but this check has
6609466715
** since moved into the btree layer. */
@@ -66095,11 +66716,11 @@
6609566716
u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
6609666717
u.aw.pCur->isIndex = !u.aw.pCur->isTable;
6609766718
break;
6609866719
}
6609966720
66100
-/* Opcode: OpenEphemeral P1 P2 * P4 *
66721
+/* Opcode: OpenEphemeral P1 P2 * P4 P5
6610166722
**
6610266723
** Open a new cursor P1 to a transient table.
6610366724
** The cursor is always opened read/write even if
6610466725
** the main database is read-only. The ephemeral
6610566726
** table is deleted automatically when the cursor is closed.
@@ -66112,18 +66733,30 @@
6611266733
** This opcode was once called OpenTemp. But that created
6611366734
** confusion because the term "temp table", might refer either
6611466735
** to a TEMP table at the SQL level, or to a table opened by
6611566736
** this opcode. Then this opcode was call OpenVirtual. But
6611666737
** that created confusion with the whole virtual-table idea.
66738
+**
66739
+** The P5 parameter can be a mask of the BTREE_* flags defined
66740
+** in btree.h. These flags control aspects of the operation of
66741
+** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
66742
+** added automatically.
6611766743
*/
6611866744
/* Opcode: OpenAutoindex P1 P2 * P4 *
6611966745
**
6612066746
** This opcode works the same as OP_OpenEphemeral. It has a
6612166747
** different name to distinguish its use. Tables created using
6612266748
** by this opcode will be used for automatically created transient
6612366749
** indices in joins.
6612466750
*/
66751
+/* Opcode: OpenSorter P1 P2 * P4 *
66752
+**
66753
+** This opcode works like OP_OpenEphemeral except that it opens
66754
+** a transient index that is specifically designed to sort large
66755
+** tables using an external merge-sort algorithm.
66756
+*/
66757
+case OP_OpenSorter:
6612566758
case OP_OpenAutoindex:
6612666759
case OP_OpenEphemeral: {
6612766760
#if 0 /* local variables moved into u.ax */
6612866761
VdbeCursor *pCx;
6612966762
#endif /* local variables moved into u.ax */
@@ -66133,10 +66766,11 @@
6613366766
SQLITE_OPEN_EXCLUSIVE |
6613466767
SQLITE_OPEN_DELETEONCLOSE |
6613566768
SQLITE_OPEN_TRANSIENT_DB;
6613666769
6613766770
assert( pOp->p1>=0 );
66771
+ assert( (pOp->opcode==OP_OpenSorter)==((pOp->p5 & BTREE_SORTER)!=0) );
6613866772
u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
6613966773
if( u.ax.pCx==0 ) goto no_mem;
6614066774
u.ax.pCx->nullRow = 1;
6614166775
rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt,
6614266776
BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
@@ -66166,10 +66800,15 @@
6616666800
u.ax.pCx->isTable = 1;
6616766801
}
6616866802
}
6616966803
u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
6617066804
u.ax.pCx->isIndex = !u.ax.pCx->isTable;
66805
+#ifndef SQLITE_OMIT_MERGE_SORT
66806
+ if( rc==SQLITE_OK && pOp->opcode==OP_OpenSorter ){
66807
+ rc = sqlite3VdbeSorterInit(db, u.ax.pCx);
66808
+ }
66809
+#endif
6617166810
break;
6617266811
}
6617366812
6617466813
/* Opcode: OpenPseudo P1 P2 P3 * *
6617566814
**
@@ -66285,11 +66924,11 @@
6628566924
assert( u.az.pC->pseudoTableReg==0 );
6628666925
assert( OP_SeekLe == OP_SeekLt+1 );
6628766926
assert( OP_SeekGe == OP_SeekLt+2 );
6628866927
assert( OP_SeekGt == OP_SeekLt+3 );
6628966928
assert( u.az.pC->isOrdered );
66290
- if( u.az.pC->pCursor!=0 ){
66929
+ if( ALWAYS(u.az.pC->pCursor!=0) ){
6629166930
u.az.oc = pOp->opcode;
6629266931
u.az.pC->nullRow = 0;
6629366932
if( u.az.pC->isTable ){
6629466933
/* The input value in P3 might be of any type: integer, real, string,
6629566934
** blob, or NULL. But it needs to be an integer before we can do
@@ -66651,11 +67290,11 @@
6665167290
u.bd.pC = p->apCsr[pOp->p1];
6665267291
assert( u.bd.pC!=0 );
6665367292
assert( u.bd.pC->isTable );
6665467293
assert( u.bd.pC->pseudoTableReg==0 );
6665567294
u.bd.pCrsr = u.bd.pC->pCursor;
66656
- if( u.bd.pCrsr!=0 ){
67295
+ if( ALWAYS(u.bd.pCrsr!=0) ){
6665767296
u.bd.res = 0;
6665867297
u.bd.iKey = pIn3->u.i;
6665967298
rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res);
6666067299
u.bd.pC->lastRowid = pIn3->u.i;
6666167300
u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0;
@@ -67075,10 +67714,17 @@
6707567714
assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey );
6707667715
assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData );
6707767716
assert( u.bh.pC!=0 );
6707867717
assert( u.bh.pC->nullRow==0 );
6707967718
assert( u.bh.pC->pseudoTableReg==0 );
67719
+
67720
+ if( isSorter(u.bh.pC) ){
67721
+ assert( pOp->opcode==OP_RowKey );
67722
+ rc = sqlite3VdbeSorterRowkey(u.bh.pC, pOut);
67723
+ break;
67724
+ }
67725
+
6708067726
assert( u.bh.pC->pCursor!=0 );
6708167727
u.bh.pCrsr = u.bh.pC->pCursor;
6708267728
assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) );
6708367729
6708467730
/* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
@@ -67183,10 +67829,11 @@
6718367829
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6718467830
u.bj.pC = p->apCsr[pOp->p1];
6718567831
assert( u.bj.pC!=0 );
6718667832
u.bj.pC->nullRow = 1;
6718767833
u.bj.pC->rowidIsValid = 0;
67834
+ assert( u.bj.pC->pCursor || u.bj.pC->pVtabCursor );
6718867835
if( u.bj.pC->pCursor ){
6718967836
sqlite3BtreeClearCursor(u.bj.pC->pCursor);
6719067837
}
6719167838
break;
6719267839
}
@@ -67208,11 +67855,11 @@
6720867855
6720967856
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6721067857
u.bk.pC = p->apCsr[pOp->p1];
6721167858
assert( u.bk.pC!=0 );
6721267859
u.bk.pCrsr = u.bk.pC->pCursor;
67213
- if( u.bk.pCrsr==0 ){
67860
+ if( NEVER(u.bk.pCrsr==0) ){
6721467861
u.bk.res = 1;
6721567862
}else{
6721667863
rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
6721767864
}
6721867865
u.bk.pC->nullRow = (u8)u.bk.res;
@@ -67263,11 +67910,15 @@
6726367910
6726467911
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6726567912
u.bl.pC = p->apCsr[pOp->p1];
6726667913
assert( u.bl.pC!=0 );
6726767914
u.bl.res = 1;
67268
- if( (u.bl.pCrsr = u.bl.pC->pCursor)!=0 ){
67915
+ if( isSorter(u.bl.pC) ){
67916
+ rc = sqlite3VdbeSorterRewind(db, u.bl.pC, &u.bl.res);
67917
+ }else{
67918
+ u.bl.pCrsr = u.bl.pC->pCursor;
67919
+ assert( u.bl.pCrsr );
6726967920
rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res);
6727067921
u.bl.pC->atFirst = u.bl.res==0 ?1:0;
6727167922
u.bl.pC->deferredMoveto = 0;
6727267923
u.bl.pC->cacheStatus = CACHE_STALE;
6727367924
u.bl.pC->rowidIsValid = 0;
@@ -67278,18 +67929,21 @@
6727867929
pc = pOp->p2 - 1;
6727967930
}
6728067931
break;
6728167932
}
6728267933
67283
-/* Opcode: Next P1 P2 * * P5
67934
+/* Opcode: Next P1 P2 * P4 P5
6728467935
**
6728567936
** Advance cursor P1 so that it points to the next key/data pair in its
6728667937
** table or index. If there are no more key/value pairs then fall through
6728767938
** to the following instruction. But if the cursor advance was successful,
6728867939
** jump immediately to P2.
6728967940
**
6729067941
** The P1 cursor must be for a real table, not a pseudo-table.
67942
+**
67943
+** P4 is always of type P4_ADVANCE. The function pointer points to
67944
+** sqlite3BtreeNext().
6729167945
**
6729267946
** If P5 is positive and the jump is taken, then event counter
6729367947
** number P5-1 in the prepared statement is incremented.
6729467948
**
6729567949
** See also: Prev
@@ -67300,19 +67954,21 @@
6730067954
** table or index. If there is no previous key/value pairs then fall through
6730167955
** to the following instruction. But if the cursor backup was successful,
6730267956
** jump immediately to P2.
6730367957
**
6730467958
** The P1 cursor must be for a real table, not a pseudo-table.
67959
+**
67960
+** P4 is always of type P4_ADVANCE. The function pointer points to
67961
+** sqlite3BtreePrevious().
6730567962
**
6730667963
** If P5 is positive and the jump is taken, then event counter
6730767964
** number P5-1 in the prepared statement is incremented.
6730867965
*/
6730967966
case OP_Prev: /* jump */
6731067967
case OP_Next: { /* jump */
6731167968
#if 0 /* local variables moved into u.bm */
6731267969
VdbeCursor *pC;
67313
- BtCursor *pCrsr;
6731467970
int res;
6731567971
#endif /* local variables moved into u.bm */
6731667972
6731767973
CHECK_FOR_INTERRUPT;
6731867974
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
@@ -67319,19 +67975,21 @@
6731967975
assert( pOp->p5<=ArraySize(p->aCounter) );
6732067976
u.bm.pC = p->apCsr[pOp->p1];
6732167977
if( u.bm.pC==0 ){
6732267978
break; /* See ticket #2273 */
6732367979
}
67324
- u.bm.pCrsr = u.bm.pC->pCursor;
67325
- if( u.bm.pCrsr==0 ){
67326
- u.bm.pC->nullRow = 1;
67327
- break;
67328
- }
67329
- u.bm.res = 1;
67330
- assert( u.bm.pC->deferredMoveto==0 );
67331
- rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bm.pCrsr, &u.bm.res) :
67332
- sqlite3BtreePrevious(u.bm.pCrsr, &u.bm.res);
67980
+ if( isSorter(u.bm.pC) ){
67981
+ assert( pOp->opcode==OP_Next );
67982
+ rc = sqlite3VdbeSorterNext(db, u.bm.pC, &u.bm.res);
67983
+ }else{
67984
+ u.bm.res = 1;
67985
+ assert( u.bm.pC->deferredMoveto==0 );
67986
+ assert( u.bm.pC->pCursor );
67987
+ assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
67988
+ assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
67989
+ rc = pOp->p4.xAdvance(u.bm.pC->pCursor, &u.bm.res);
67990
+ }
6733367991
u.bm.pC->nullRow = (u8)u.bm.res;
6733467992
u.bm.pC->cacheStatus = CACHE_STALE;
6733567993
if( u.bm.res==0 ){
6733667994
pc = pOp->p2 - 1;
6733767995
if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
@@ -67373,14 +68031,17 @@
6737368031
assert( u.bn.pC->isTable==0 );
6737468032
rc = ExpandBlob(pIn2);
6737568033
if( rc==SQLITE_OK ){
6737668034
u.bn.nKey = pIn2->n;
6737768035
u.bn.zKey = pIn2->z;
67378
- rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
67379
- ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
67380
- );
67381
- assert( u.bn.pC->deferredMoveto==0 );
68036
+ rc = sqlite3VdbeSorterWrite(db, u.bn.pC, u.bn.nKey);
68037
+ if( rc==SQLITE_OK ){
68038
+ rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
68039
+ ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
68040
+ );
68041
+ assert( u.bn.pC->deferredMoveto==0 );
68042
+ }
6738268043
u.bn.pC->cacheStatus = CACHE_STALE;
6738368044
}
6738468045
}
6738568046
break;
6738668047
}
@@ -69556,10 +70217,722 @@
6955670217
}
6955770218
6955870219
#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
6955970220
6956070221
/************** End of vdbeblob.c ********************************************/
70222
+/************** Begin file vdbesort.c ****************************************/
70223
+/*
70224
+** 2011 July 9
70225
+**
70226
+** The author disclaims copyright to this source code. In place of
70227
+** a legal notice, here is a blessing:
70228
+**
70229
+** May you do good and not evil.
70230
+** May you find forgiveness for yourself and forgive others.
70231
+** May you share freely, never taking more than you give.
70232
+**
70233
+*************************************************************************
70234
+** This file contains code for the VdbeSorter object, used in concert with
70235
+** a VdbeCursor to sort large numbers of keys (as may be required, for
70236
+** example, by CREATE INDEX statements on tables too large to fit in main
70237
+** memory).
70238
+*/
70239
+
70240
+
70241
+#ifndef SQLITE_OMIT_MERGE_SORT
70242
+
70243
+typedef struct VdbeSorterIter VdbeSorterIter;
70244
+
70245
+/*
70246
+** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
70247
+**
70248
+** As keys are added to the sorter, they are written to disk in a series
70249
+** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
70250
+** the same as the cache-size allowed for temporary databases. In order
70251
+** to allow the caller to extract keys from the sorter in sorted order,
70252
+** all PMAs currently stored on disk must be merged together. This comment
70253
+** describes the data structure used to do so. The structure supports
70254
+** merging any number of arrays in a single pass with no redundant comparison
70255
+** operations.
70256
+**
70257
+** The aIter[] array contains an iterator for each of the PMAs being merged.
70258
+** An aIter[] iterator either points to a valid key or else is at EOF. For
70259
+** the purposes of the paragraphs below, we assume that the array is actually
70260
+** N elements in size, where N is the smallest power of 2 greater to or equal
70261
+** to the number of iterators being merged. The extra aIter[] elements are
70262
+** treated as if they are empty (always at EOF).
70263
+**
70264
+** The aTree[] array is also N elements in size. The value of N is stored in
70265
+** the VdbeSorter.nTree variable.
70266
+**
70267
+** The final (N/2) elements of aTree[] contain the results of comparing
70268
+** pairs of iterator keys together. Element i contains the result of
70269
+** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
70270
+** aTree element is set to the index of it.
70271
+**
70272
+** For the purposes of this comparison, EOF is considered greater than any
70273
+** other key value. If the keys are equal (only possible with two EOF
70274
+** values), it doesn't matter which index is stored.
70275
+**
70276
+** The (N/4) elements of aTree[] that preceed the final (N/2) described
70277
+** above contains the index of the smallest of each block of 4 iterators.
70278
+** And so on. So that aTree[1] contains the index of the iterator that
70279
+** currently points to the smallest key value. aTree[0] is unused.
70280
+**
70281
+** Example:
70282
+**
70283
+** aIter[0] -> Banana
70284
+** aIter[1] -> Feijoa
70285
+** aIter[2] -> Elderberry
70286
+** aIter[3] -> Currant
70287
+** aIter[4] -> Grapefruit
70288
+** aIter[5] -> Apple
70289
+** aIter[6] -> Durian
70290
+** aIter[7] -> EOF
70291
+**
70292
+** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
70293
+**
70294
+** The current element is "Apple" (the value of the key indicated by
70295
+** iterator 5). When the Next() operation is invoked, iterator 5 will
70296
+** be advanced to the next key in its segment. Say the next key is
70297
+** "Eggplant":
70298
+**
70299
+** aIter[5] -> Eggplant
70300
+**
70301
+** The contents of aTree[] are updated first by comparing the new iterator
70302
+** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
70303
+** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
70304
+** The value of iterator 6 - "Durian" - is now smaller than that of iterator
70305
+** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
70306
+** so the value written into element 1 of the array is 0. As follows:
70307
+**
70308
+** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
70309
+**
70310
+** In other words, each time we advance to the next sorter element, log2(N)
70311
+** key comparison operations are required, where N is the number of segments
70312
+** being merged (rounded up to the next power of 2).
70313
+*/
70314
+struct VdbeSorter {
70315
+ int nWorking; /* Start a new b-tree after this many pages */
70316
+ int nBtree; /* Current size of b-tree contents as PMA */
70317
+ int nTree; /* Used size of aTree/aIter (power of 2) */
70318
+ VdbeSorterIter *aIter; /* Array of iterators to merge */
70319
+ int *aTree; /* Current state of incremental merge */
70320
+ i64 iWriteOff; /* Current write offset within file pTemp1 */
70321
+ i64 iReadOff; /* Current read offset within file pTemp1 */
70322
+ sqlite3_file *pTemp1; /* PMA file 1 */
70323
+ int nPMA; /* Number of PMAs stored in pTemp1 */
70324
+};
70325
+
70326
+/*
70327
+** The following type is an iterator for a PMA. It caches the current key in
70328
+** variables nKey/aKey. If the iterator is at EOF, pFile==0.
70329
+*/
70330
+struct VdbeSorterIter {
70331
+ i64 iReadOff; /* Current read offset */
70332
+ i64 iEof; /* 1 byte past EOF for this iterator */
70333
+ sqlite3_file *pFile; /* File iterator is reading from */
70334
+ int nAlloc; /* Bytes of space at aAlloc */
70335
+ u8 *aAlloc; /* Allocated space */
70336
+ int nKey; /* Number of bytes in key */
70337
+ u8 *aKey; /* Pointer to current key */
70338
+};
70339
+
70340
+/* Minimum allowable value for the VdbeSorter.nWorking variable */
70341
+#define SORTER_MIN_WORKING 10
70342
+
70343
+/* Maximum number of segments to merge in a single pass. */
70344
+#define SORTER_MAX_MERGE_COUNT 16
70345
+
70346
+/*
70347
+** Free all memory belonging to the VdbeSorterIter object passed as the second
70348
+** argument. All structure fields are set to zero before returning.
70349
+*/
70350
+static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
70351
+ sqlite3DbFree(db, pIter->aAlloc);
70352
+ memset(pIter, 0, sizeof(VdbeSorterIter));
70353
+}
70354
+
70355
+/*
70356
+** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
70357
+** no error occurs, or an SQLite error code if one does.
70358
+*/
70359
+static int vdbeSorterIterNext(
70360
+ sqlite3 *db, /* Database handle (for sqlite3DbMalloc() ) */
70361
+ VdbeSorterIter *pIter /* Iterator to advance */
70362
+){
70363
+ int rc; /* Return Code */
70364
+ int nRead; /* Number of bytes read */
70365
+ int nRec; /* Size of record in bytes */
70366
+ int iOff; /* Size of serialized size varint in bytes */
70367
+
70368
+ nRead = pIter->iEof - pIter->iReadOff;
70369
+ if( nRead>5 ) nRead = 5;
70370
+ if( nRead<=0 ){
70371
+ /* This is an EOF condition */
70372
+ vdbeSorterIterZero(db, pIter);
70373
+ return SQLITE_OK;
70374
+ }
70375
+
70376
+ rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
70377
+ iOff = getVarint32(pIter->aAlloc, nRec);
70378
+
70379
+ if( rc==SQLITE_OK && (iOff+nRec)>nRead ){
70380
+ int nRead2; /* Number of extra bytes to read */
70381
+ if( (iOff+nRec)>pIter->nAlloc ){
70382
+ int nNew = pIter->nAlloc*2;
70383
+ while( (iOff+nRec)>nNew ) nNew = nNew*2;
70384
+ pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
70385
+ if( !pIter->aAlloc ) return SQLITE_NOMEM;
70386
+ pIter->nAlloc = nNew;
70387
+ }
70388
+
70389
+ nRead2 = iOff + nRec - nRead;
70390
+ rc = sqlite3OsRead(
70391
+ pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
70392
+ );
70393
+ }
70394
+
70395
+ assert( nRec>0 || rc!=SQLITE_OK );
70396
+ pIter->iReadOff += iOff+nRec;
70397
+ pIter->nKey = nRec;
70398
+ pIter->aKey = &pIter->aAlloc[iOff];
70399
+ return rc;
70400
+}
70401
+
70402
+/*
70403
+** Write a single varint, value iVal, to file-descriptor pFile. Return
70404
+** SQLITE_OK if successful, or an SQLite error code if some error occurs.
70405
+**
70406
+** The value of *piOffset when this function is called is used as the byte
70407
+** offset in file pFile to write to. Before returning, *piOffset is
70408
+** incremented by the number of bytes written.
70409
+*/
70410
+static int vdbeSorterWriteVarint(
70411
+ sqlite3_file *pFile, /* File to write to */
70412
+ i64 iVal, /* Value to write as a varint */
70413
+ i64 *piOffset /* IN/OUT: Write offset in file pFile */
70414
+){
70415
+ u8 aVarint[9]; /* Buffer large enough for a varint */
70416
+ int nVarint; /* Number of used bytes in varint */
70417
+ int rc; /* Result of write() call */
70418
+
70419
+ nVarint = sqlite3PutVarint(aVarint, iVal);
70420
+ rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
70421
+ *piOffset += nVarint;
70422
+
70423
+ return rc;
70424
+}
70425
+
70426
+/*
70427
+** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
70428
+** successful, or an SQLite error code if some error occurs.
70429
+**
70430
+** The value of *piOffset when this function is called is used as the
70431
+** byte offset in file pFile from whence to read the varint. If successful
70432
+** (i.e. if no IO error occurs), then *piOffset is set to the offset of
70433
+** the first byte past the end of the varint before returning. *piVal is
70434
+** set to the integer value read. If an error occurs, the final values of
70435
+** both *piOffset and *piVal are undefined.
70436
+*/
70437
+static int vdbeSorterReadVarint(
70438
+ sqlite3_file *pFile, /* File to read from */
70439
+ i64 iEof, /* Total number of bytes in file */
70440
+ i64 *piOffset, /* IN/OUT: Read offset in pFile */
70441
+ i64 *piVal /* OUT: Value read from file */
70442
+){
70443
+ u8 aVarint[9]; /* Buffer large enough for a varint */
70444
+ i64 iOff = *piOffset; /* Offset in file to read from */
70445
+ int nRead = 9; /* Number of bytes to read from file */
70446
+ int rc; /* Return code */
70447
+
70448
+ assert( iEof>iOff );
70449
+ if( (iEof-iOff)<nRead ){
70450
+ nRead = iEof-iOff;
70451
+ }
70452
+
70453
+ rc = sqlite3OsRead(pFile, aVarint, nRead, iOff);
70454
+ if( rc==SQLITE_OK ){
70455
+ *piOffset += getVarint(aVarint, (u64 *)piVal);
70456
+ }
70457
+
70458
+ return rc;
70459
+}
70460
+
70461
+/*
70462
+** Initialize iterator pIter to scan through the PMA stored in file pFile
70463
+** starting at offset iStart and ending at offset iEof-1. This function
70464
+** leaves the iterator pointing to the first key in the PMA (or EOF if the
70465
+** PMA is empty).
70466
+*/
70467
+static int vdbeSorterIterInit(
70468
+ sqlite3 *db, /* Database handle */
70469
+ VdbeSorter *pSorter, /* Sorter object */
70470
+ i64 iStart, /* Start offset in pFile */
70471
+ VdbeSorterIter *pIter, /* Iterator to populate */
70472
+ i64 *pnByte /* IN/OUT: Increment this value by PMA size */
70473
+){
70474
+ int rc;
70475
+
70476
+ assert( pSorter->iWriteOff>iStart );
70477
+ assert( pIter->aAlloc==0 );
70478
+ pIter->pFile = pSorter->pTemp1;
70479
+ pIter->iReadOff = iStart;
70480
+ pIter->nAlloc = 128;
70481
+ pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
70482
+ if( !pIter->aAlloc ){
70483
+ rc = SQLITE_NOMEM;
70484
+ }else{
70485
+ i64 iEof = pSorter->iWriteOff; /* EOF of file pSorter->pTemp1 */
70486
+ i64 nByte; /* Total size of PMA in bytes */
70487
+ rc = vdbeSorterReadVarint(pSorter->pTemp1, iEof, &pIter->iReadOff, &nByte);
70488
+ *pnByte += nByte;
70489
+ pIter->iEof = pIter->iReadOff + nByte;
70490
+ }
70491
+ if( rc==SQLITE_OK ){
70492
+ rc = vdbeSorterIterNext(db, pIter);
70493
+ }
70494
+ return rc;
70495
+}
70496
+
70497
+/*
70498
+** This function is called to compare two iterator keys when merging
70499
+** multiple b-tree segments. Parameter iOut is the index of the aTree[]
70500
+** value to recalculate.
70501
+*/
70502
+static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
70503
+ VdbeSorter *pSorter = pCsr->pSorter;
70504
+ int i1;
70505
+ int i2;
70506
+ int iRes;
70507
+ VdbeSorterIter *p1;
70508
+ VdbeSorterIter *p2;
70509
+
70510
+ assert( iOut<pSorter->nTree && iOut>0 );
70511
+
70512
+ if( iOut>=(pSorter->nTree/2) ){
70513
+ i1 = (iOut - pSorter->nTree/2) * 2;
70514
+ i2 = i1 + 1;
70515
+ }else{
70516
+ i1 = pSorter->aTree[iOut*2];
70517
+ i2 = pSorter->aTree[iOut*2+1];
70518
+ }
70519
+
70520
+ p1 = &pSorter->aIter[i1];
70521
+ p2 = &pSorter->aIter[i2];
70522
+
70523
+ if( p1->pFile==0 ){
70524
+ iRes = i2;
70525
+ }else if( p2->pFile==0 ){
70526
+ iRes = i1;
70527
+ }else{
70528
+ char aSpace[150];
70529
+ UnpackedRecord *r1;
70530
+
70531
+ r1 = sqlite3VdbeRecordUnpack(
70532
+ pCsr->pKeyInfo, p1->nKey, p1->aKey, aSpace, sizeof(aSpace)
70533
+ );
70534
+ if( r1==0 ) return SQLITE_NOMEM;
70535
+
70536
+ if( sqlite3VdbeRecordCompare(p2->nKey, p2->aKey, r1)>=0 ){
70537
+ iRes = i1;
70538
+ }else{
70539
+ iRes = i2;
70540
+ }
70541
+ sqlite3VdbeDeleteUnpackedRecord(r1);
70542
+ }
70543
+
70544
+ pSorter->aTree[iOut] = iRes;
70545
+ return SQLITE_OK;
70546
+}
70547
+
70548
+/*
70549
+** Initialize the temporary index cursor just opened as a sorter cursor.
70550
+*/
70551
+SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
70552
+ assert( pCsr->pKeyInfo && pCsr->pBt );
70553
+ pCsr->pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
70554
+ return (pCsr->pSorter ? SQLITE_OK : SQLITE_NOMEM);
70555
+}
70556
+
70557
+/*
70558
+** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
70559
+*/
70560
+SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
70561
+ VdbeSorter *pSorter = pCsr->pSorter;
70562
+ if( pSorter ){
70563
+ if( pSorter->aIter ){
70564
+ int i;
70565
+ for(i=0; i<pSorter->nTree; i++){
70566
+ vdbeSorterIterZero(db, &pSorter->aIter[i]);
70567
+ }
70568
+ sqlite3DbFree(db, pSorter->aIter);
70569
+ }
70570
+ if( pSorter->pTemp1 ){
70571
+ sqlite3OsCloseFree(pSorter->pTemp1);
70572
+ }
70573
+ sqlite3DbFree(db, pSorter);
70574
+ pCsr->pSorter = 0;
70575
+ }
70576
+}
70577
+
70578
+/*
70579
+** Allocate space for a file-handle and open a temporary file. If successful,
70580
+** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
70581
+** Otherwise, set *ppFile to 0 and return an SQLite error code.
70582
+*/
70583
+static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
70584
+ int dummy;
70585
+ return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
70586
+ SQLITE_OPEN_TEMP_JOURNAL |
70587
+ SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
70588
+ SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &dummy
70589
+ );
70590
+}
70591
+
70592
+
70593
+/*
70594
+** Write the current contents of the b-tree to a PMA. Return SQLITE_OK
70595
+** if successful, or an SQLite error code otherwise.
70596
+**
70597
+** The format of a PMA is:
70598
+**
70599
+** * A varint. This varint contains the total number of bytes of content
70600
+** in the PMA (not including the varint itself).
70601
+**
70602
+** * One or more records packed end-to-end in order of ascending keys.
70603
+** Each record consists of a varint followed by a blob of data (the
70604
+** key). The varint is the number of bytes in the blob of data.
70605
+*/
70606
+static int vdbeSorterBtreeToPMA(sqlite3 *db, VdbeCursor *pCsr){
70607
+ int rc = SQLITE_OK; /* Return code */
70608
+ VdbeSorter *pSorter = pCsr->pSorter;
70609
+ int res = 0;
70610
+
70611
+ /* sqlite3BtreeFirst() cannot fail because sorter btrees are always held
70612
+ ** in memory and so an I/O error is not possible. */
70613
+ rc = sqlite3BtreeFirst(pCsr->pCursor, &res);
70614
+ if( NEVER(rc!=SQLITE_OK) || res ) return rc;
70615
+ assert( pSorter->nBtree>0 );
70616
+
70617
+ /* If the first temporary PMA file has not been opened, open it now. */
70618
+ if( pSorter->pTemp1==0 ){
70619
+ rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
70620
+ assert( rc!=SQLITE_OK || pSorter->pTemp1 );
70621
+ assert( pSorter->iWriteOff==0 );
70622
+ assert( pSorter->nPMA==0 );
70623
+ }
70624
+
70625
+ if( rc==SQLITE_OK ){
70626
+ i64 iWriteOff = pSorter->iWriteOff;
70627
+ void *aMalloc = 0; /* Array used to hold a single record */
70628
+ int nMalloc = 0; /* Allocated size of aMalloc[] in bytes */
70629
+
70630
+ pSorter->nPMA++;
70631
+ for(
70632
+ rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nBtree, &iWriteOff);
70633
+ rc==SQLITE_OK && res==0;
70634
+ rc = sqlite3BtreeNext(pCsr->pCursor, &res)
70635
+ ){
70636
+ i64 nKey; /* Size of this key in bytes */
70637
+
70638
+ /* Write the size of the record in bytes to the output file */
70639
+ (void)sqlite3BtreeKeySize(pCsr->pCursor, &nKey);
70640
+ rc = vdbeSorterWriteVarint(pSorter->pTemp1, nKey, &iWriteOff);
70641
+
70642
+ /* Make sure the aMalloc[] buffer is large enough for the record */
70643
+ if( rc==SQLITE_OK && nKey>nMalloc ){
70644
+ aMalloc = sqlite3DbReallocOrFree(db, aMalloc, nKey);
70645
+ if( !aMalloc ){
70646
+ rc = SQLITE_NOMEM;
70647
+ }else{
70648
+ nMalloc = nKey;
70649
+ }
70650
+ }
70651
+
70652
+ /* Write the record itself to the output file */
70653
+ if( rc==SQLITE_OK ){
70654
+ /* sqlite3BtreeKey() cannot fail because sorter btrees held in memory */
70655
+ rc = sqlite3BtreeKey(pCsr->pCursor, 0, nKey, aMalloc);
70656
+ if( ALWAYS(rc==SQLITE_OK) ){
70657
+ rc = sqlite3OsWrite(pSorter->pTemp1, aMalloc, nKey, iWriteOff);
70658
+ iWriteOff += nKey;
70659
+ }
70660
+ }
70661
+
70662
+ if( rc!=SQLITE_OK ) break;
70663
+ }
70664
+
70665
+ /* This assert verifies that unless an error has occurred, the size of
70666
+ ** the PMA on disk is the same as the expected size stored in
70667
+ ** pSorter->nBtree. */
70668
+ assert( rc!=SQLITE_OK || pSorter->nBtree==(
70669
+ iWriteOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nBtree)
70670
+ ));
70671
+
70672
+ pSorter->iWriteOff = iWriteOff;
70673
+ sqlite3DbFree(db, aMalloc);
70674
+ }
70675
+
70676
+ pSorter->nBtree = 0;
70677
+ return rc;
70678
+}
70679
+
70680
+/*
70681
+** This function is called on a sorter cursor by the VDBE before each row
70682
+** is inserted into VdbeCursor.pCsr. Argument nKey is the size of the key, in
70683
+** bytes, about to be inserted.
70684
+**
70685
+** If it is determined that the temporary b-tree accessed via VdbeCursor.pCsr
70686
+** is large enough, its contents are written to a sorted PMA on disk and the
70687
+** tree emptied. This prevents the b-tree (which must be small enough to
70688
+** fit entirely in the cache in order to support efficient inserts) from
70689
+** growing too large.
70690
+**
70691
+** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
70692
+*/
70693
+SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *db, VdbeCursor *pCsr, int nKey){
70694
+ int rc = SQLITE_OK; /* Return code */
70695
+ VdbeSorter *pSorter = pCsr->pSorter;
70696
+ if( pSorter ){
70697
+ Pager *pPager = sqlite3BtreePager(pCsr->pBt);
70698
+ int nPage; /* Current size of temporary file in pages */
70699
+
70700
+ /* Sorters never spill to disk */
70701
+ assert( sqlite3PagerFile(pPager)->pMethods==0 );
70702
+
70703
+ /* Determine how many pages the temporary b-tree has grown to */
70704
+ sqlite3PagerPagecount(pPager, &nPage);
70705
+
70706
+ /* If pSorter->nWorking is still zero, but the temporary file has been
70707
+ ** created in the file-system, then the most recent insert into the
70708
+ ** current b-tree segment probably caused the cache to overflow (it is
70709
+ ** also possible that sqlite3_release_memory() was called). So set the
70710
+ ** size of the working set to a little less than the current size of the
70711
+ ** file in pages. */
70712
+ if( pSorter->nWorking==0 && sqlite3PagerUnderStress(pPager) ){
70713
+ pSorter->nWorking = nPage-5;
70714
+ if( pSorter->nWorking<SORTER_MIN_WORKING ){
70715
+ pSorter->nWorking = SORTER_MIN_WORKING;
70716
+ }
70717
+ }
70718
+
70719
+ /* If the number of pages used by the current b-tree segment is greater
70720
+ ** than the size of the working set (VdbeSorter.nWorking), start a new
70721
+ ** segment b-tree. */
70722
+ if( pSorter->nWorking && nPage>=pSorter->nWorking ){
70723
+ BtCursor *p = pCsr->pCursor;/* Cursor structure to close and reopen */
70724
+ int iRoot; /* Root page of new tree */
70725
+
70726
+ /* Copy the current contents of the b-tree into a PMA in sorted order.
70727
+ ** Close the currently open b-tree cursor. */
70728
+ rc = vdbeSorterBtreeToPMA(db, pCsr);
70729
+ sqlite3BtreeCloseCursor(p);
70730
+
70731
+ if( rc==SQLITE_OK ){
70732
+ rc = sqlite3BtreeDropTable(pCsr->pBt, 2, 0);
70733
+#ifdef SQLITE_DEBUG
70734
+ sqlite3PagerPagecount(pPager, &nPage);
70735
+ assert( rc!=SQLITE_OK || nPage==1 );
70736
+#endif
70737
+ }
70738
+ if( rc==SQLITE_OK ){
70739
+ rc = sqlite3BtreeCreateTable(pCsr->pBt, &iRoot, BTREE_BLOBKEY);
70740
+ }
70741
+ if( rc==SQLITE_OK ){
70742
+ assert( iRoot==2 );
70743
+ rc = sqlite3BtreeCursor(pCsr->pBt, iRoot, 1, pCsr->pKeyInfo, p);
70744
+ }
70745
+ }
70746
+
70747
+ pSorter->nBtree += sqlite3VarintLen(nKey) + nKey;
70748
+ }
70749
+ return rc;
70750
+}
70751
+
70752
+/*
70753
+** Helper function for sqlite3VdbeSorterRewind().
70754
+*/
70755
+static int vdbeSorterInitMerge(
70756
+ sqlite3 *db, /* Database handle */
70757
+ VdbeCursor *pCsr, /* Cursor handle for this sorter */
70758
+ i64 *pnByte /* Sum of bytes in all opened PMAs */
70759
+){
70760
+ VdbeSorter *pSorter = pCsr->pSorter;
70761
+ int rc = SQLITE_OK; /* Return code */
70762
+ int i; /* Used to iterator through aIter[] */
70763
+ i64 nByte = 0; /* Total bytes in all opened PMAs */
70764
+
70765
+ /* Initialize the iterators. */
70766
+ for(i=0; rc==SQLITE_OK && i<SORTER_MAX_MERGE_COUNT; i++){
70767
+ VdbeSorterIter *pIter = &pSorter->aIter[i];
70768
+ rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
70769
+ pSorter->iReadOff = pIter->iEof;
70770
+ assert( pSorter->iReadOff<=pSorter->iWriteOff || rc!=SQLITE_OK );
70771
+ if( pSorter->iReadOff>=pSorter->iWriteOff ) break;
70772
+ }
70773
+
70774
+ /* Initialize the aTree[] array. */
70775
+ for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
70776
+ rc = vdbeSorterDoCompare(pCsr, i);
70777
+ }
70778
+
70779
+ *pnByte = nByte;
70780
+ return rc;
70781
+}
70782
+
70783
+/*
70784
+** Once the sorter has been populated, this function is called to prepare
70785
+** for iterating through its contents in sorted order.
70786
+*/
70787
+SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
70788
+ VdbeSorter *pSorter = pCsr->pSorter;
70789
+ int rc; /* Return code */
70790
+ sqlite3_file *pTemp2 = 0; /* Second temp file to use */
70791
+ i64 iWrite2 = 0; /* Write offset for pTemp2 */
70792
+ int nIter; /* Number of iterators used */
70793
+ int nByte; /* Bytes of space required for aIter/aTree */
70794
+ int N = 2; /* Power of 2 >= nIter */
70795
+
70796
+ assert( pSorter );
70797
+
70798
+ /* Write the current b-tree to a PMA. Close the b-tree cursor. */
70799
+ rc = vdbeSorterBtreeToPMA(db, pCsr);
70800
+ sqlite3BtreeCloseCursor(pCsr->pCursor);
70801
+ if( rc!=SQLITE_OK ) return rc;
70802
+ if( pSorter->nPMA==0 ){
70803
+ *pbEof = 1;
70804
+ return SQLITE_OK;
70805
+ }
70806
+
70807
+ /* Allocate space for aIter[] and aTree[]. */
70808
+ nIter = pSorter->nPMA;
70809
+ if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
70810
+ assert( nIter>0 );
70811
+ while( N<nIter ) N += N;
70812
+ nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
70813
+ pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
70814
+ if( !pSorter->aIter ) return SQLITE_NOMEM;
70815
+ pSorter->aTree = (int *)&pSorter->aIter[N];
70816
+ pSorter->nTree = N;
70817
+
70818
+ do {
70819
+ int iNew; /* Index of new, merged, PMA */
70820
+
70821
+ for(iNew=0;
70822
+ rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA;
70823
+ iNew++
70824
+ ){
70825
+ i64 nWrite; /* Number of bytes in new PMA */
70826
+
70827
+ /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
70828
+ ** initialize an iterator for each of them and break out of the loop.
70829
+ ** These iterators will be incrementally merged as the VDBE layer calls
70830
+ ** sqlite3VdbeSorterNext().
70831
+ **
70832
+ ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
70833
+ ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
70834
+ ** are merged into a single PMA that is written to file pTemp2.
70835
+ */
70836
+ rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
70837
+ assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
70838
+ if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
70839
+ break;
70840
+ }
70841
+
70842
+ /* Open the second temp file, if it is not already open. */
70843
+ if( pTemp2==0 ){
70844
+ assert( iWrite2==0 );
70845
+ rc = vdbeSorterOpenTempFile(db, &pTemp2);
70846
+ }
70847
+
70848
+ if( rc==SQLITE_OK ){
70849
+ rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
70850
+ }
70851
+
70852
+ if( rc==SQLITE_OK ){
70853
+ int bEof = 0;
70854
+ while( rc==SQLITE_OK && bEof==0 ){
70855
+ int nToWrite;
70856
+ VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
70857
+ assert( pIter->pFile );
70858
+ nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
70859
+ rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
70860
+ iWrite2 += nToWrite;
70861
+ if( rc==SQLITE_OK ){
70862
+ rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
70863
+ }
70864
+ }
70865
+ }
70866
+ }
70867
+
70868
+ if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
70869
+ break;
70870
+ }else{
70871
+ sqlite3_file *pTmp = pSorter->pTemp1;
70872
+ pSorter->nPMA = iNew;
70873
+ pSorter->pTemp1 = pTemp2;
70874
+ pTemp2 = pTmp;
70875
+ pSorter->iWriteOff = iWrite2;
70876
+ pSorter->iReadOff = 0;
70877
+ iWrite2 = 0;
70878
+ }
70879
+ }while( rc==SQLITE_OK );
70880
+
70881
+ if( pTemp2 ){
70882
+ sqlite3OsCloseFree(pTemp2);
70883
+ }
70884
+ *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
70885
+ return rc;
70886
+}
70887
+
70888
+/*
70889
+** Advance to the next element in the sorter.
70890
+*/
70891
+SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
70892
+ VdbeSorter *pSorter = pCsr->pSorter;
70893
+ int iPrev = pSorter->aTree[1]; /* Index of iterator to advance */
70894
+ int i; /* Index of aTree[] to recalculate */
70895
+ int rc; /* Return code */
70896
+
70897
+ rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
70898
+ for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
70899
+ rc = vdbeSorterDoCompare(pCsr, i);
70900
+ }
70901
+
70902
+ *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
70903
+ return rc;
70904
+}
70905
+
70906
+/*
70907
+** Copy the current sorter key into the memory cell pOut.
70908
+*/
70909
+SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
70910
+ VdbeSorter *pSorter = pCsr->pSorter;
70911
+ VdbeSorterIter *pIter;
70912
+
70913
+ pIter = &pSorter->aIter[ pSorter->aTree[1] ];
70914
+
70915
+ /* Coverage testing note: As things are currently, this call will always
70916
+ ** succeed. This is because the memory cell passed by the VDBE layer
70917
+ ** happens to be the same one as was used to assemble the keys before they
70918
+ ** were passed to the sorter - meaning it is always large enough for the
70919
+ ** largest key. But this could change very easily, so we leave the call
70920
+ ** to sqlite3VdbeMemGrow() in. */
70921
+ if( NEVER(sqlite3VdbeMemGrow(pOut, pIter->nKey, 0)) ){
70922
+ return SQLITE_NOMEM;
70923
+ }
70924
+ pOut->n = pIter->nKey;
70925
+ MemSetTypeFlag(pOut, MEM_Blob);
70926
+ memcpy(pOut->z, pIter->aKey, pIter->nKey);
70927
+
70928
+ return SQLITE_OK;
70929
+}
70930
+
70931
+#endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
70932
+
70933
+/************** End of vdbesort.c ********************************************/
6956170934
/************** Begin file journal.c *****************************************/
6956270935
/*
6956370936
** 2007 August 22
6956470937
**
6956570938
** The author disclaims copyright to this source code. In place of
@@ -70072,10 +71445,12 @@
7007271445
**
7007371446
*************************************************************************
7007471447
** This file contains routines used for walking the parser tree for
7007571448
** an SQL statement.
7007671449
*/
71450
+/* #include <stdlib.h> */
71451
+/* #include <string.h> */
7007771452
7007871453
7007971454
/*
7008071455
** Walk an expression tree. Invoke the callback once for each node
7008171456
** of the expression, while decending. (In other words, the callback
@@ -70210,10 +71585,12 @@
7021071585
**
7021171586
** This file contains routines used for walking the parser tree and
7021271587
** resolve all identifiers by associating them with a particular
7021371588
** table and column.
7021471589
*/
71590
+/* #include <stdlib.h> */
71591
+/* #include <string.h> */
7021571592
7021671593
/*
7021771594
** Turn the pExpr expression into an alias for the iCol-th column of the
7021871595
** result set in pEList.
7021971596
**
@@ -76012,100 +77389,10 @@
7601277389
** May you find forgiveness for yourself and forgive others.
7601377390
** May you share freely, never taking more than you give.
7601477391
**
7601577392
*************************************************************************
7601677393
** 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.
7610777394
*/
7610877395
#ifndef SQLITE_OMIT_ANALYZE
7610977396
7611077397
/*
7611177398
** This routine generates code that opens the sqlite_stat1 table for
@@ -76133,18 +77420,12 @@
7613377420
static const struct {
7613477421
const char *zName;
7613577422
const char *zCols;
7613677423
} aTable[] = {
7613777424
{ "sqlite_stat1", "tbl,idx,stat" },
76138
-#ifdef SQLITE_ENABLE_STAT3
76139
- { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
76140
-#endif
76141
- };
76142
- static const char *azToDrop[] = {
76143
- "sqlite_stat2",
76144
-#ifndef SQLITE_ENABLE_STAT3
76145
- "sqlite_stat3",
77425
+#ifdef SQLITE_ENABLE_STAT2
77426
+ { "sqlite_stat2", "tbl,idx,sampleno,sample" },
7614677427
#endif
7614777428
};
7614877429
7614977430
int aRoot[] = {0, 0};
7615077431
u8 aCreateTbl[] = {0, 0};
@@ -76156,21 +77437,10 @@
7615677437
if( v==0 ) return;
7615777438
assert( sqlite3BtreeHoldsAllMutexes(db) );
7615877439
assert( sqlite3VdbeDb(v)==db );
7615977440
pDb = &db->aDb[iDb];
7616077441
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
- */
7617277442
for(i=0; i<ArraySize(aTable); i++){
7617377443
const char *zTab = aTable[i].zName;
7617477444
Table *pStat;
7617577445
if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
7617677446
/* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -76197,238 +77467,17 @@
7619777467
sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
7619877468
}
7619977469
}
7620077470
}
7620177471
76202
- /* Open the sqlite_stat[13] tables for writing. */
77472
+ /* Open the sqlite_stat[12] tables for writing. */
7620377473
for(i=0; i<ArraySize(aTable); i++){
7620477474
sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
7620577475
sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
7620677476
sqlite3VdbeChangeP5(v, aCreateTbl[i]);
7620777477
}
7620877478
}
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
-
7643077479
7643177480
/*
7643277481
** Generate code to do an analysis of all indices associated with
7643377482
** a single table.
7643477483
*/
@@ -76448,31 +77497,24 @@
7644877497
int endOfLoop; /* The end of the loop */
7644977498
int jZeroRows = -1; /* Jump from here if number of rows is zero */
7645077499
int iDb; /* Index of database containing pTab */
7645177500
int regTabname = iMem++; /* Register containing table name */
7645277501
int regIdxname = iMem++; /* Register containing index name */
76453
- int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
76454
-#ifdef SQLITE_ENABLE_STAT3
76455
- int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
76456
- int regNumLt = iMem++; /* Number of keys less than regSample */
76457
- int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
76458
- int regSample = iMem++; /* The next sample value */
76459
- int regRowid = regSample; /* Rowid of a sample */
76460
- int regAccum = iMem++; /* Register to hold Stat3Accum object */
76461
- int regLoop = iMem++; /* Loop counter */
76462
- int regCount = iMem++; /* Number of rows in the table or index */
76463
- int regTemp1 = iMem++; /* Intermediate register */
76464
- int regTemp2 = iMem++; /* Intermediate register */
76465
- int once = 1; /* One-time initialization */
76466
- int shortJump = 0; /* Instruction address */
76467
- int iTabCur = pParse->nTab++; /* Table cursor */
76468
-#endif
76469
- int regCol = iMem++; /* Content of a column in analyzed table */
77502
+ int regSampleno = iMem++; /* Register containing next sample number */
77503
+ int regCol = iMem++; /* Content of a column analyzed table */
7647077504
int regRec = iMem++; /* Register holding completed record */
7647177505
int regTemp = iMem++; /* Temporary use register */
76472
- int regNewRowid = iMem++; /* Rowid for the inserted record */
77506
+ int regRowid = iMem++; /* Rowid for the inserted record */
7647377507
77508
+#ifdef SQLITE_ENABLE_STAT2
77509
+ int addr = 0; /* Instruction address */
77510
+ int regTemp2 = iMem++; /* Temporary use register */
77511
+ int regSamplerecno = iMem++; /* Index of next sample to record */
77512
+ int regRecno = iMem++; /* Current sample index */
77513
+ int regLast = iMem++; /* Index of last sample to record */
77514
+ int regFirst = iMem++; /* Index of first sample to record */
77515
+#endif
7647477516
7647577517
v = sqlite3GetVdbe(pParse);
7647677518
if( v==0 || NEVER(pTab==0) ){
7647777519
return;
7647877520
}
@@ -76501,22 +77543,17 @@
7650177543
iIdxCur = pParse->nTab++;
7650277544
sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
7650377545
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
7650477546
int nCol;
7650577547
KeyInfo *pKey;
76506
- int addrIfNot = 0; /* address of OP_IfNot */
76507
- int *aChngAddr; /* Array of jump instruction addresses */
7650877548
7650977549
if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
76510
- VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
7651177550
nCol = pIdx->nColumn;
7651277551
pKey = sqlite3IndexKeyinfo(pParse, pIdx);
7651377552
if( iMem+1+(nCol*2)>pParse->nMem ){
7651477553
pParse->nMem = iMem+1+(nCol*2);
7651577554
}
76516
- aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*pIdx->nColumn);
76517
- if( aChngAddr==0 ) continue;
7651877555
7651977556
/* Open a cursor to the index to be analyzed. */
7652077557
assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
7652177558
sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
7652277559
(char *)pKey, P4_KEYINFO_HANDOFF);
@@ -76523,24 +77560,35 @@
7652377560
VdbeComment((v, "%s", pIdx->zName));
7652477561
7652577562
/* Populate the register containing the index name. */
7652677563
sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
7652777564
76528
-#ifdef SQLITE_ENABLE_STAT3
76529
- if( once ){
76530
- once = 0;
76531
- sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
76532
- }
76533
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
76534
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
76535
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
76536
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
76537
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
76538
- sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
76539
- (char*)&stat3InitFuncdef, P4_FUNCDEF);
76540
- sqlite3VdbeChangeP5(v, 2);
76541
-#endif /* SQLITE_ENABLE_STAT3 */
77565
+#ifdef SQLITE_ENABLE_STAT2
77566
+
77567
+ /* If this iteration of the loop is generating code to analyze the
77568
+ ** first index in the pTab->pIndex list, then register regLast has
77569
+ ** not been populated. In this case populate it now. */
77570
+ if( pTab->pIndex==pIdx ){
77571
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
77572
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
77573
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
77574
+
77575
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
77576
+ sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
77577
+ addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
77578
+ sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
77579
+ sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
77580
+ sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
77581
+ sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
77582
+ sqlite3VdbeJumpHere(v, addr);
77583
+ }
77584
+
77585
+ /* Zero the regSampleno and regRecno registers. */
77586
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
77587
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
77588
+ sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
77589
+#endif
7654277590
7654377591
/* The block of memory cells initialized here is used as follows.
7654477592
**
7654577593
** iMem:
7654677594
** The total number of rows in the table.
@@ -76566,87 +77614,79 @@
7656677614
/* Start the analysis loop. This loop runs through all the entries in
7656777615
** the index b-tree. */
7656877616
endOfLoop = sqlite3VdbeMakeLabel(v);
7656977617
sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
7657077618
topOfLoop = sqlite3VdbeCurrentAddr(v);
76571
- sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
77619
+ sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
7657277620
7657377621
for(i=0; i<nCol; i++){
7657477622
CollSeq *pColl;
7657577623
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
7657677624
if( i==0 ){
77625
+#ifdef SQLITE_ENABLE_STAT2
77626
+ /* Check if the record that cursor iIdxCur points to contains a
77627
+ ** value that should be stored in the sqlite_stat2 table. If so,
77628
+ ** store it. */
77629
+ int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
77630
+ assert( regTabname+1==regIdxname
77631
+ && regTabname+2==regSampleno
77632
+ && regTabname+3==regCol
77633
+ );
77634
+ sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
77635
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
77636
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
77637
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
77638
+
77639
+ /* Calculate new values for regSamplerecno and regSampleno.
77640
+ **
77641
+ ** sampleno = sampleno + 1
77642
+ ** samplerecno = samplerecno+(remaining records)/(remaining samples)
77643
+ */
77644
+ sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
77645
+ sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
77646
+ sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77647
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
77648
+ sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
77649
+ sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
77650
+ sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
77651
+
77652
+ sqlite3VdbeJumpHere(v, ne);
77653
+ sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
77654
+#endif
77655
+
7657777656
/* Always record the very first row */
76578
- addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
77657
+ sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
7657977658
}
7658077659
assert( pIdx->azColl!=0 );
7658177660
assert( pIdx->azColl[i]!=0 );
7658277661
pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
76583
- aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
76584
- (char*)pColl, P4_COLLSEQ);
77662
+ sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
77663
+ (char*)pColl, P4_COLLSEQ);
7658577664
sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
76586
- VdbeComment((v, "jump if column %d changed", i));
76587
-#ifdef SQLITE_ENABLE_STAT3
76588
- if( i==0 ){
76589
- sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
76590
- VdbeComment((v, "incr repeat count"));
76591
- }
76592
-#endif
77665
+ }
77666
+ if( db->mallocFailed ){
77667
+ /* If a malloc failure has occurred, then the result of the expression
77668
+ ** passed as the second argument to the call to sqlite3VdbeJumpHere()
77669
+ ** below may be negative. Which causes an assert() to fail (or an
77670
+ ** out-of-bounds write if SQLITE_DEBUG is not defined). */
77671
+ return;
7659377672
}
7659477673
sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
7659577674
for(i=0; i<nCol; i++){
76596
- sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
77675
+ int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
7659777676
if( i==0 ){
76598
- sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
76599
-#ifdef SQLITE_ENABLE_STAT3
76600
- sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
76601
- (char*)&stat3PushFuncdef, P4_FUNCDEF);
76602
- sqlite3VdbeChangeP5(v, 5);
76603
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
76604
- sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
76605
- sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
76606
- sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
76607
-#endif
76608
- }
77677
+ sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
77678
+ }
77679
+ sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
7660977680
sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
7661077681
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
7661177682
}
76612
- sqlite3DbFree(db, aChngAddr);
7661377683
76614
- /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
77684
+ /* End of the analysis loop. */
7661577685
sqlite3VdbeResolveLabel(v, endOfLoop);
76616
-
7661777686
sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
7661877687
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
7664877688
7664977689
/* Store the results in sqlite_stat1.
7665077690
**
7665177691
** The result is a single row of the sqlite_stat1 table. The first
7665277692
** two columns are the names of the table and index. The third column
@@ -76662,51 +77702,50 @@
7666277702
**
7666377703
** If K==0 then no entry is made into the sqlite_stat1 table.
7666477704
** If K>0 then it is always the case the D>0 so division by zero
7666577705
** is never possible.
7666677706
*/
76667
- sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
77707
+ sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
7666877708
if( jZeroRows<0 ){
7666977709
jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
7667077710
}
7667177711
for(i=0; i<nCol; i++){
7667277712
sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
76673
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
77713
+ sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
7667477714
sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
7667577715
sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
7667677716
sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
7667777717
sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
76678
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
77718
+ sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
7667977719
}
7668077720
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76681
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76682
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
77721
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77722
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
7668377723
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7668477724
}
7668577725
7668677726
/* If the table has no indices, create a single sqlite_stat1 entry
7668777727
** containing NULL as the index name and the row count as the content.
7668877728
*/
7668977729
if( pTab->pIndex==0 ){
7669077730
sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
7669177731
VdbeComment((v, "%s", pTab->zName));
76692
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
77732
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
7669377733
sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76694
- jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
77734
+ jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
7669577735
}else{
7669677736
sqlite3VdbeJumpHere(v, jZeroRows);
7669777737
jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
7669877738
}
7669977739
sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
7670077740
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76701
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76702
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
77741
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77742
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
7670377743
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7670477744
if( pParse->nMem<regRec ) pParse->nMem = regRec;
7670577745
sqlite3VdbeJumpHere(v, jZeroRows);
7670677746
}
76707
-
7670877747
7670977748
/*
7671077749
** Generate code that will cause the most recent index analysis to
7671177750
** be loaded into internal hash tables where is can be used.
7671277751
*/
@@ -76727,11 +77766,11 @@
7672777766
int iStatCur;
7672877767
int iMem;
7672977768
7673077769
sqlite3BeginWriteOperation(pParse, 0, iDb);
7673177770
iStatCur = pParse->nTab;
76732
- pParse->nTab += 3;
77771
+ pParse->nTab += 2;
7673377772
openStatTable(pParse, iDb, iStatCur, 0, 0);
7673477773
iMem = pParse->nMem+1;
7673577774
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
7673677775
for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
7673777776
Table *pTab = (Table*)sqliteHashData(k);
@@ -76752,11 +77791,11 @@
7675277791
assert( pTab!=0 );
7675377792
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
7675477793
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
7675577794
sqlite3BeginWriteOperation(pParse, 0, iDb);
7675677795
iStatCur = pParse->nTab;
76757
- pParse->nTab += 3;
77796
+ pParse->nTab += 2;
7675877797
if( pOnlyIdx ){
7675977798
openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
7676077799
}else{
7676177800
openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
7676277801
}
@@ -76857,11 +77896,11 @@
7685777896
static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
7685877897
analysisInfo *pInfo = (analysisInfo*)pData;
7685977898
Index *pIndex;
7686077899
Table *pTable;
7686177900
int i, c, n;
76862
- tRowcnt v;
77901
+ unsigned int v;
7686377902
const char *z;
7686477903
7686577904
assert( argc==3 );
7686677905
UNUSED_PARAMETER2(NotUsed, argc);
7686777906
@@ -76900,172 +77939,40 @@
7690077939
/*
7690177940
** If the Index.aSample variable is not NULL, delete the aSample[] array
7690277941
** and its contents.
7690377942
*/
7690477943
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
76905
-#ifdef SQLITE_ENABLE_STAT3
77944
+#ifdef SQLITE_ENABLE_STAT2
7690677945
if( pIdx->aSample ){
7690777946
int j;
76908
- for(j=0; j<pIdx->nSample; j++){
77947
+ for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
7690977948
IndexSample *p = &pIdx->aSample[j];
7691077949
if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
76911
- sqlite3_free(p->u.z);
77950
+ sqlite3DbFree(db, p->u.z);
7691277951
}
7691377952
}
76914
- sqlite3_free(pIdx->aSample);
77953
+ sqlite3DbFree(db, pIdx->aSample);
7691577954
}
76916
- UNUSED_PARAMETER(db);
76917
- pIdx->nSample = 0;
76918
- pIdx->aSample = 0;
7691977955
#else
7692077956
UNUSED_PARAMETER(db);
7692177957
UNUSED_PARAMETER(pIdx);
7692277958
#endif
7692377959
}
7692477960
76925
-#ifdef SQLITE_ENABLE_STAT3
76926
-/*
76927
-** Load content from the sqlite_stat3 table into the Index.aSample[]
76928
-** arrays of all indices.
76929
-*/
76930
-static int loadStat3(sqlite3 *db, const char *zDb){
76931
- int rc; /* Result codes from subroutines */
76932
- sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
76933
- char *zSql; /* Text of the SQL statement */
76934
- Index *pPrevIdx = 0; /* Previous index in the loop */
76935
- int idx = 0; /* slot in pIdx->aSample[] for next sample */
76936
- int eType; /* Datatype of a sample */
76937
- IndexSample *pSample; /* A slot in pIdx->aSample[] */
76938
-
76939
- if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
76940
- return SQLITE_OK;
76941
- }
76942
-
76943
- zSql = sqlite3MPrintf(db,
76944
- "SELECT idx,count(*) FROM %Q.sqlite_stat3"
76945
- " GROUP BY idx", zDb);
76946
- if( !zSql ){
76947
- return SQLITE_NOMEM;
76948
- }
76949
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76950
- sqlite3DbFree(db, zSql);
76951
- if( rc ) return rc;
76952
-
76953
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
76954
- char *zIndex; /* Index name */
76955
- Index *pIdx; /* Pointer to the index object */
76956
- int nSample; /* Number of samples */
76957
-
76958
- zIndex = (char *)sqlite3_column_text(pStmt, 0);
76959
- if( zIndex==0 ) continue;
76960
- nSample = sqlite3_column_int(pStmt, 1);
76961
- if( nSample>255 ) continue;
76962
- pIdx = sqlite3FindIndex(db, zIndex, zDb);
76963
- if( pIdx==0 ) continue;
76964
- assert( pIdx->nSample==0 );
76965
- pIdx->nSample = (u8)nSample;
76966
- pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
76967
- pIdx->avgEq = pIdx->aiRowEst[1];
76968
- if( pIdx->aSample==0 ){
76969
- db->mallocFailed = 1;
76970
- sqlite3_finalize(pStmt);
76971
- return SQLITE_NOMEM;
76972
- }
76973
- }
76974
- sqlite3_finalize(pStmt);
76975
-
76976
- zSql = sqlite3MPrintf(db,
76977
- "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
76978
- if( !zSql ){
76979
- return SQLITE_NOMEM;
76980
- }
76981
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76982
- sqlite3DbFree(db, zSql);
76983
- if( rc ) return rc;
76984
-
76985
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
76986
- char *zIndex; /* Index name */
76987
- Index *pIdx; /* Pointer to the index object */
76988
- int i; /* Loop counter */
76989
- tRowcnt sumEq; /* Sum of the nEq values */
76990
-
76991
- zIndex = (char *)sqlite3_column_text(pStmt, 0);
76992
- if( zIndex==0 ) continue;
76993
- pIdx = sqlite3FindIndex(db, zIndex, zDb);
76994
- if( pIdx==0 ) continue;
76995
- if( pIdx==pPrevIdx ){
76996
- idx++;
76997
- }else{
76998
- pPrevIdx = pIdx;
76999
- idx = 0;
77000
- }
77001
- assert( idx<pIdx->nSample );
77002
- pSample = &pIdx->aSample[idx];
77003
- pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
77004
- pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
77005
- pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
77006
- if( idx==pIdx->nSample-1 ){
77007
- if( pSample->nDLt>0 ){
77008
- for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
77009
- pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
77010
- }
77011
- if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
77012
- }
77013
- eType = sqlite3_column_type(pStmt, 4);
77014
- pSample->eType = (u8)eType;
77015
- switch( eType ){
77016
- case SQLITE_INTEGER: {
77017
- pSample->u.i = sqlite3_column_int64(pStmt, 4);
77018
- break;
77019
- }
77020
- case SQLITE_FLOAT: {
77021
- pSample->u.r = sqlite3_column_double(pStmt, 4);
77022
- break;
77023
- }
77024
- case SQLITE_NULL: {
77025
- break;
77026
- }
77027
- default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
77028
- const char *z = (const char *)(
77029
- (eType==SQLITE_BLOB) ?
77030
- sqlite3_column_blob(pStmt, 4):
77031
- sqlite3_column_text(pStmt, 4)
77032
- );
77033
- int n = sqlite3_column_bytes(pStmt, 4);
77034
- if( n>0xffff ) n = 0xffff;
77035
- pSample->nByte = (u16)n;
77036
- if( n < 1){
77037
- pSample->u.z = 0;
77038
- }else{
77039
- pSample->u.z = sqlite3Malloc(n);
77040
- if( pSample->u.z==0 ){
77041
- db->mallocFailed = 1;
77042
- sqlite3_finalize(pStmt);
77043
- return SQLITE_NOMEM;
77044
- }
77045
- memcpy(pSample->u.z, z, n);
77046
- }
77047
- }
77048
- }
77049
- }
77050
- return sqlite3_finalize(pStmt);
77051
-}
77052
-#endif /* SQLITE_ENABLE_STAT3 */
77053
-
77054
-/*
77055
-** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
77961
+/*
77962
+** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
7705677963
** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
77057
-** arrays. The contents of sqlite_stat3 are used to populate the
77964
+** arrays. The contents of sqlite_stat2 are used to populate the
7705877965
** Index.aSample[] arrays.
7705977966
**
7706077967
** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
77061
-** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
77062
-** during compilation and the sqlite_stat3 table is present, no data is
77968
+** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
77969
+** during compilation and the sqlite_stat2 table is present, no data is
7706377970
** read from it.
7706477971
**
77065
-** If SQLITE_ENABLE_STAT3 was defined during compilation and the
77066
-** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
77972
+** If SQLITE_ENABLE_STAT2 was defined during compilation and the
77973
+** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
7706777974
** returned. However, in this case, data is read from the sqlite_stat1
7706877975
** table (if it is present) before returning.
7706977976
**
7707077977
** If an OOM error occurs, this function always sets db->mallocFailed.
7707177978
** This means if the caller does not care about other errors, the return
@@ -77083,14 +77990,12 @@
7708377990
/* Clear any prior statistics */
7708477991
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
7708577992
for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
7708677993
Index *pIdx = sqliteHashData(i);
7708777994
sqlite3DefaultRowEst(pIdx);
77088
-#ifdef SQLITE_ENABLE_STAT3
7708977995
sqlite3DeleteIndexSamples(db, pIdx);
7709077996
pIdx->aSample = 0;
77091
-#endif
7709277997
}
7709377998
7709477999
/* Check to make sure the sqlite_stat1 table exists */
7709578000
sInfo.db = db;
7709678001
sInfo.zDatabase = db->aDb[iDb].zName;
@@ -77098,23 +78003,91 @@
7709878003
return SQLITE_ERROR;
7709978004
}
7710078005
7710178006
/* Load new statistics out of the sqlite_stat1 table */
7710278007
zSql = sqlite3MPrintf(db,
77103
- "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
78008
+ "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
7710478009
if( zSql==0 ){
7710578010
rc = SQLITE_NOMEM;
7710678011
}else{
7710778012
rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
7710878013
sqlite3DbFree(db, zSql);
7710978014
}
7711078015
7711178016
77112
- /* Load the statistics from the sqlite_stat3 table. */
77113
-#ifdef SQLITE_ENABLE_STAT3
78017
+ /* Load the statistics from the sqlite_stat2 table. */
78018
+#ifdef SQLITE_ENABLE_STAT2
78019
+ if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
78020
+ rc = SQLITE_ERROR;
78021
+ }
7711478022
if( rc==SQLITE_OK ){
77115
- rc = loadStat3(db, sInfo.zDatabase);
78023
+ sqlite3_stmt *pStmt = 0;
78024
+
78025
+ zSql = sqlite3MPrintf(db,
78026
+ "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
78027
+ if( !zSql ){
78028
+ rc = SQLITE_NOMEM;
78029
+ }else{
78030
+ rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78031
+ sqlite3DbFree(db, zSql);
78032
+ }
78033
+
78034
+ if( rc==SQLITE_OK ){
78035
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
78036
+ char *zIndex; /* Index name */
78037
+ Index *pIdx; /* Pointer to the index object */
78038
+
78039
+ zIndex = (char *)sqlite3_column_text(pStmt, 0);
78040
+ pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
78041
+ if( pIdx ){
78042
+ int iSample = sqlite3_column_int(pStmt, 1);
78043
+ if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
78044
+ int eType = sqlite3_column_type(pStmt, 2);
78045
+
78046
+ if( pIdx->aSample==0 ){
78047
+ static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
78048
+ pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
78049
+ if( pIdx->aSample==0 ){
78050
+ db->mallocFailed = 1;
78051
+ break;
78052
+ }
78053
+ memset(pIdx->aSample, 0, sz);
78054
+ }
78055
+
78056
+ assert( pIdx->aSample );
78057
+ {
78058
+ IndexSample *pSample = &pIdx->aSample[iSample];
78059
+ pSample->eType = (u8)eType;
78060
+ if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
78061
+ pSample->u.r = sqlite3_column_double(pStmt, 2);
78062
+ }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
78063
+ const char *z = (const char *)(
78064
+ (eType==SQLITE_BLOB) ?
78065
+ sqlite3_column_blob(pStmt, 2):
78066
+ sqlite3_column_text(pStmt, 2)
78067
+ );
78068
+ int n = sqlite3_column_bytes(pStmt, 2);
78069
+ if( n>24 ){
78070
+ n = 24;
78071
+ }
78072
+ pSample->nByte = (u8)n;
78073
+ if( n < 1){
78074
+ pSample->u.z = 0;
78075
+ }else{
78076
+ pSample->u.z = sqlite3DbStrNDup(0, z, n);
78077
+ if( pSample->u.z==0 ){
78078
+ db->mallocFailed = 1;
78079
+ break;
78080
+ }
78081
+ }
78082
+ }
78083
+ }
78084
+ }
78085
+ }
78086
+ }
78087
+ rc = sqlite3_finalize(pStmt);
78088
+ }
7711678089
}
7711778090
#endif
7711878091
7711978092
if( rc==SQLITE_NOMEM ){
7712078093
db->mallocFailed = 1;
@@ -79610,11 +80583,11 @@
7961080583
Table *p;
7961180584
int n;
7961280585
const char *z;
7961380586
Token sEnd;
7961480587
DbFixer sFix;
79615
- Token *pName;
80588
+ Token *pName = 0;
7961680589
int iDb;
7961780590
sqlite3 *db = pParse->db;
7961880591
7961980592
if( pParse->nVar>0 ){
7962080593
sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
@@ -79926,15 +80899,11 @@
7992680899
Parse *pParse, /* The parsing context */
7992780900
int iDb, /* The database number */
7992880901
const char *zType, /* "idx" or "tbl" */
7992980902
const char *zName /* Name of index or table */
7993080903
){
79931
- static const char *azStatTab[] = {
79932
- "sqlite_stat1",
79933
- "sqlite_stat2",
79934
- "sqlite_stat3",
79935
- };
80904
+ static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
7993680905
int i;
7993780906
const char *zDbName = pParse->db->aDb[iDb].zName;
7993880907
for(i=0; i<ArraySize(azStatTab); i++){
7993980908
if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
7994080909
sqlite3NestedParse(pParse,
@@ -79941,81 +80910,10 @@
7994180910
"DELETE FROM %Q.%s WHERE %s=%Q",
7994280911
zDbName, azStatTab[i], zType, zName
7994380912
);
7994480913
}
7994580914
}
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
-
8001780915
}
8001880916
8001980917
/*
8002080918
** This routine is called to do the work of a DROP TABLE statement.
8002180919
** pName is the name of the table to be dropped.
@@ -80082,11 +80980,11 @@
8008280980
if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
8008380981
goto exit_drop_table;
8008480982
}
8008580983
}
8008680984
#endif
80087
- if( !pParse->nested && sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
80985
+ if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
8008880986
sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
8008980987
goto exit_drop_table;
8009080988
}
8009180989
8009280990
#ifndef SQLITE_OMIT_VIEW
@@ -80106,15 +81004,72 @@
8010681004
/* Generate code to remove the table from the master table
8010781005
** on disk.
8010881006
*/
8010981007
v = sqlite3GetVdbe(pParse);
8011081008
if( v ){
81009
+ Trigger *pTrigger;
81010
+ Db *pDb = &db->aDb[iDb];
8011181011
sqlite3BeginWriteOperation(pParse, 1, iDb);
81012
+
81013
+#ifndef SQLITE_OMIT_VIRTUALTABLE
81014
+ if( IsVirtual(pTab) ){
81015
+ sqlite3VdbeAddOp0(v, OP_VBegin);
81016
+ }
81017
+#endif
8011281018
sqlite3FkDropTable(pParse, pName, pTab);
80113
- sqlite3CodeDropTable(pParse, pTab, iDb, isView);
81019
+
81020
+ /* Drop all triggers associated with the table being dropped. Code
81021
+ ** is generated to remove entries from sqlite_master and/or
81022
+ ** sqlite_temp_master if required.
81023
+ */
81024
+ pTrigger = sqlite3TriggerList(pParse, pTab);
81025
+ while( pTrigger ){
81026
+ assert( pTrigger->pSchema==pTab->pSchema ||
81027
+ pTrigger->pSchema==db->aDb[1].pSchema );
81028
+ sqlite3DropTriggerPtr(pParse, pTrigger);
81029
+ pTrigger = pTrigger->pNext;
81030
+ }
81031
+
81032
+#ifndef SQLITE_OMIT_AUTOINCREMENT
81033
+ /* Remove any entries of the sqlite_sequence table associated with
81034
+ ** the table being dropped. This is done before the table is dropped
81035
+ ** at the btree level, in case the sqlite_sequence table needs to
81036
+ ** move as a result of the drop (can happen in auto-vacuum mode).
81037
+ */
81038
+ if( pTab->tabFlags & TF_Autoincrement ){
81039
+ sqlite3NestedParse(pParse,
81040
+ "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
81041
+ pDb->zName, pTab->zName
81042
+ );
81043
+ }
81044
+#endif
81045
+
81046
+ /* Drop all SQLITE_MASTER table and index entries that refer to the
81047
+ ** table. The program name loops through the master table and deletes
81048
+ ** every row that refers to a table of the same name as the one being
81049
+ ** dropped. Triggers are handled seperately because a trigger can be
81050
+ ** created in the temp database that refers to a table in another
81051
+ ** database.
81052
+ */
81053
+ sqlite3NestedParse(pParse,
81054
+ "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81055
+ pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
8011481056
sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
81057
+ if( !isView && !IsVirtual(pTab) ){
81058
+ destroyTable(pParse, pTab);
81059
+ }
81060
+
81061
+ /* Remove the table entry from SQLite's internal schema and modify
81062
+ ** the schema cookie.
81063
+ */
81064
+ if( IsVirtual(pTab) ){
81065
+ sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81066
+ }
81067
+ sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81068
+ sqlite3ChangeCookie(pParse, iDb);
8011581069
}
81070
+ sqliteViewResetAll(db, iDb);
8011681071
8011781072
exit_drop_table:
8011881073
sqlite3SrcListDelete(db, pName);
8011981074
}
8012081075
@@ -80278,18 +81233,28 @@
8027881233
*/
8027981234
static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
8028081235
Table *pTab = pIndex->pTable; /* The table that is indexed */
8028181236
int iTab = pParse->nTab++; /* Btree cursor used for pTab */
8028281237
int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
81238
+ int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */
8028381239
int addr1; /* Address of top of loop */
8028481240
int tnum; /* Root page of index */
8028581241
Vdbe *v; /* Generate code into this virtual machine */
8028681242
KeyInfo *pKey; /* KeyInfo for index */
8028781243
int regIdxKey; /* Registers containing the index key */
8028881244
int regRecord; /* Register holding assemblied index record */
8028981245
sqlite3 *db = pParse->db; /* The database connection */
8029081246
int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
81247
+
81248
+ /* Set bUseSorter to use OP_OpenSorter, or clear it to insert directly
81249
+ ** into the index. The sorter is used unless either OMIT_MERGE_SORT is
81250
+ ** defined or the system is configured to store temp files in-memory. */
81251
+#ifdef SQLITE_OMIT_MERGE_SORT
81252
+ static const int bUseSorter = 0;
81253
+#else
81254
+ const int bUseSorter = !sqlite3TempInMemory(pParse->db);
81255
+#endif
8029181256
8029281257
#ifndef SQLITE_OMIT_AUTHORIZATION
8029381258
if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
8029481259
db->aDb[iDb].zName ) ){
8029581260
return;
@@ -80311,14 +81276,33 @@
8031181276
sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
8031281277
(char *)pKey, P4_KEYINFO_HANDOFF);
8031381278
if( memRootPage>=0 ){
8031481279
sqlite3VdbeChangeP5(v, 1);
8031581280
}
81281
+
81282
+ /* Open the sorter cursor if we are to use one. */
81283
+ if( bUseSorter ){
81284
+ iSorter = pParse->nTab++;
81285
+ sqlite3VdbeAddOp4(v, OP_OpenSorter, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
81286
+ sqlite3VdbeChangeP5(v, BTREE_SORTER);
81287
+ }
81288
+
81289
+ /* Open the table. Loop through all rows of the table, inserting index
81290
+ ** records into the sorter. */
8031681291
sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
8031781292
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
8031881293
regRecord = sqlite3GetTempReg(pParse);
8031981294
regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81295
+
81296
+ if( bUseSorter ){
81297
+ sqlite3VdbeAddOp2(v, OP_IdxInsert, iSorter, regRecord);
81298
+ sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81299
+ sqlite3VdbeJumpHere(v, addr1);
81300
+ addr1 = sqlite3VdbeAddOp2(v, OP_Sort, iSorter, 0);
81301
+ sqlite3VdbeAddOp2(v, OP_RowKey, iSorter, regRecord);
81302
+ }
81303
+
8032081304
if( pIndex->onError!=OE_None ){
8032181305
const int regRowid = regIdxKey + pIndex->nColumn;
8032281306
const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
8032381307
void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
8032481308
@@ -80333,17 +81317,19 @@
8033381317
*/
8033481318
sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
8033581319
sqlite3HaltConstraint(
8033681320
pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
8033781321
}
80338
- sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
81322
+ sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, bUseSorter);
8033981323
sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
8034081324
sqlite3ReleaseTempReg(pParse, regRecord);
80341
- sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81325
+ sqlite3VdbeAddOp2(v, OP_Next, iSorter, addr1+1);
8034281326
sqlite3VdbeJumpHere(v, addr1);
81327
+
8034381328
sqlite3VdbeAddOp1(v, OP_Close, iTab);
8034481329
sqlite3VdbeAddOp1(v, OP_Close, iIdx);
81330
+ sqlite3VdbeAddOp1(v, OP_Close, iSorter);
8034581331
}
8034681332
8034781333
/*
8034881334
** Create a new index for an SQL table. pName1.pName2 is the name of the index
8034981335
** and pTblList is the name of the table that is to be indexed. Both will
@@ -80557,24 +81543,24 @@
8055781543
*/
8055881544
nName = sqlite3Strlen30(zName);
8055981545
nCol = pList->nExpr;
8056081546
pIndex = sqlite3DbMallocZero(db,
8056181547
sizeof(Index) + /* Index structure */
80562
- sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
8056381548
sizeof(int)*nCol + /* Index.aiColumn */
81549
+ sizeof(int)*(nCol+1) + /* Index.aiRowEst */
8056481550
sizeof(char *)*nCol + /* Index.azColl */
8056581551
sizeof(u8)*nCol + /* Index.aSortOrder */
8056681552
nName + 1 + /* Index.zName */
8056781553
nExtra /* Collation sequence names */
8056881554
);
8056981555
if( db->mallocFailed ){
8057081556
goto exit_create_index;
8057181557
}
80572
- pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
80573
- pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
81558
+ pIndex->azColl = (char**)(&pIndex[1]);
8057481559
pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
80575
- pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
81560
+ pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
81561
+ pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
8057681562
pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
8057781563
zExtra = (char *)(&pIndex->zName[nName+1]);
8057881564
memcpy(pIndex->zName, zName, nName+1);
8057981565
pIndex->pTable = pTab;
8058081566
pIndex->nColumn = pList->nExpr;
@@ -80847,13 +81833,13 @@
8084781833
** Apart from that, we have little to go on besides intuition as to
8084881834
** how aiRowEst[] should be initialized. The numbers generated here
8084981835
** are based on typical values found in actual indices.
8085081836
*/
8085181837
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
80852
- tRowcnt *a = pIdx->aiRowEst;
81838
+ unsigned *a = pIdx->aiRowEst;
8085381839
int i;
80854
- tRowcnt n;
81840
+ unsigned n;
8085581841
assert( a!=0 );
8085681842
a[0] = pIdx->pTable->nRowEst;
8085781843
if( a[0]<10 ) a[0] = 10;
8085881844
n = 10;
8085981845
for(i=1; i<=pIdx->nColumn; i++){
@@ -81293,12 +82279,13 @@
8129382279
** The operator is "natural cross join". The A and B operands are stored
8129482280
** in p->a[0] and p->a[1], respectively. The parser initially stores the
8129582281
** operator with A. This routine shifts that operator over to B.
8129682282
*/
8129782283
SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
81298
- if( p && p->a ){
82284
+ if( p ){
8129982285
int i;
82286
+ assert( p->a || p->nSrc==0 );
8130082287
for(i=p->nSrc-1; i>0; i--){
8130182288
p->a[i].jointype = p->a[i-1].jointype;
8130282289
}
8130382290
p->a[0].jointype = 0;
8130482291
}
@@ -82850,10 +83837,12 @@
8285083837
**
8285183838
** There is only one exported symbol in this file - the function
8285283839
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
8285383840
** All other code has file scope.
8285483841
*/
83842
+/* #include <stdlib.h> */
83843
+/* #include <assert.h> */
8285583844
8285683845
/*
8285783846
** Return the collating function associated with a function.
8285883847
*/
8285983848
static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -85173,11 +86162,28 @@
8517386162
pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
8517486163
}else{
8517586164
pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
8517686165
}
8517786166
if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
86167
+ assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
8517886168
if( !isIgnoreErrors || db->mallocFailed ) return;
86169
+ if( pTo==0 ){
86170
+ /* If isIgnoreErrors is true, then a table is being dropped. In this
86171
+ ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
86172
+ ** before actually dropping it in order to check FK constraints.
86173
+ ** If the parent table of an FK constraint on the current table is
86174
+ ** missing, behave as if it is empty. i.e. decrement the relevant
86175
+ ** FK counter for each row of the current table with non-NULL keys.
86176
+ */
86177
+ Vdbe *v = sqlite3GetVdbe(pParse);
86178
+ int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
86179
+ for(i=0; i<pFKey->nCol; i++){
86180
+ int iReg = pFKey->aCol[i].iFrom + regOld + 1;
86181
+ sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
86182
+ }
86183
+ sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
86184
+ }
8517986185
continue;
8518086186
}
8518186187
assert( pFKey->nCol==1 || (aiFree && pIdx) );
8518286188
8518386189
if( aiFree ){
@@ -88081,10 +89087,11 @@
8808189087
8808289088
#endif /* _SQLITE3EXT_H_ */
8808389089
8808489090
/************** End of sqlite3ext.h ******************************************/
8808589091
/************** Continuing where we left off in loadext.c ********************/
89092
+/* #include <string.h> */
8808689093
8808789094
#ifndef SQLITE_OMIT_LOAD_EXTENSION
8808889095
8808989096
/*
8809089097
** Some API routines are omitted when various features are
@@ -95650,10 +96657,12 @@
9565096657
** interface routine of sqlite3_exec().
9565196658
**
9565296659
** These routines are in a separate files so that they will not be linked
9565396660
** if they are not used.
9565496661
*/
96662
+/* #include <stdlib.h> */
96663
+/* #include <string.h> */
9565596664
9565696665
#ifndef SQLITE_OMIT_GET_TABLE
9565796666
9565896667
/*
9565996668
** This structure is used to pass data from sqlite3_get_table() through
@@ -99159,11 +100168,11 @@
99159100168
#define TERM_CODED 0x04 /* This term is already coded */
99160100169
#define TERM_COPIED 0x08 /* Has a child */
99161100170
#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
99162100171
#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
99163100172
#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
99164
-#ifdef SQLITE_ENABLE_STAT3
100173
+#ifdef SQLITE_ENABLE_STAT2
99165100174
# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
99166100175
#else
99167100176
# define TERM_VNULL 0x00 /* Disabled if not using stat2 */
99168100177
#endif
99169100178
@@ -100373,11 +101382,11 @@
100373101382
pNewTerm->prereqAll = pTerm->prereqAll;
100374101383
}
100375101384
}
100376101385
#endif /* SQLITE_OMIT_VIRTUALTABLE */
100377101386
100378
-#ifdef SQLITE_ENABLE_STAT3
101387
+#ifdef SQLITE_ENABLE_STAT2
100379101388
/* When sqlite_stat2 histogram data is available an operator of the
100380101389
** form "x IS NOT NULL" can sometimes be evaluated more efficiently
100381101390
** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
100382101391
** virtual term of that form.
100383101392
**
@@ -100412,11 +101421,11 @@
100412101421
pTerm->nChild = 1;
100413101422
pTerm->wtFlags |= TERM_COPIED;
100414101423
pNewTerm->prereqAll = pTerm->prereqAll;
100415101424
}
100416101425
}
100417
-#endif /* SQLITE_ENABLE_STAT */
101426
+#endif /* SQLITE_ENABLE_STAT2 */
100418101427
100419101428
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
100420101429
** an index for tables to the left of the join.
100421101430
*/
100422101431
pTerm->prereqRight |= extraRight;
@@ -101461,89 +102470,71 @@
101461102470
*/
101462102471
bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
101463102472
}
101464102473
#endif /* SQLITE_OMIT_VIRTUALTABLE */
101465102474
101466
-#ifdef SQLITE_ENABLE_STAT3
101467102475
/*
101468
-** Estimate the location of a particular key among all keys in an
101469
-** index. Store the results in aStat as follows:
101470
-**
101471
-** aStat[0] Est. number of rows less than pVal
101472
-** aStat[1] Est. number of rows equal to pVal
101473
-**
101474
-** Return SQLITE_OK on success.
101475
-*/
101476
-static int whereKeyStats(
102476
+** Argument pIdx is a pointer to an index structure that has an array of
102477
+** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
102478
+** stored in Index.aSample. These samples divide the domain of values stored
102479
+** the index into (SQLITE_INDEX_SAMPLES+1) regions.
102480
+** Region 0 contains all values less than the first sample value. Region
102481
+** 1 contains values between the first and second samples. Region 2 contains
102482
+** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
102483
+** contains values larger than the last sample.
102484
+**
102485
+** If the index contains many duplicates of a single value, then it is
102486
+** possible that two or more adjacent samples can hold the same value.
102487
+** When that is the case, the smallest possible region code is returned
102488
+** when roundUp is false and the largest possible region code is returned
102489
+** when roundUp is true.
102490
+**
102491
+** If successful, this function determines which of the regions value
102492
+** pVal lies in, sets *piRegion to the region index (a value between 0
102493
+** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
102494
+** Or, if an OOM occurs while converting text values between encodings,
102495
+** SQLITE_NOMEM is returned and *piRegion is undefined.
102496
+*/
102497
+#ifdef SQLITE_ENABLE_STAT2
102498
+static int whereRangeRegion(
101477102499
Parse *pParse, /* Database connection */
101478102500
Index *pIdx, /* Index to consider domain of */
101479102501
sqlite3_value *pVal, /* Value to consider */
101480
- int roundUp, /* Round up if true. Round down if false */
101481
- tRowcnt *aStat /* OUT: stats written here */
102502
+ int roundUp, /* Return largest valid region if true */
102503
+ int *piRegion /* OUT: Region of domain in which value lies */
101482102504
){
101483
- tRowcnt n;
101484
- IndexSample *aSample;
101485
- int i, eType;
101486
- int isEq = 0;
101487
- i64 v;
101488
- double r, rS;
101489
-
101490102505
assert( roundUp==0 || roundUp==1 );
101491
- if( pVal==0 ) return SQLITE_ERROR;
101492
- n = pIdx->aiRowEst[0];
101493
- aSample = pIdx->aSample;
101494
- i = 0;
101495
- eType = sqlite3_value_type(pVal);
101496
-
101497
- if( eType==SQLITE_INTEGER ){
101498
- v = sqlite3_value_int64(pVal);
101499
- r = (i64)v;
101500
- for(i=0; i<pIdx->nSample; i++){
101501
- if( aSample[i].eType==SQLITE_NULL ) continue;
101502
- if( aSample[i].eType>=SQLITE_TEXT ) break;
101503
- if( aSample[i].eType==SQLITE_INTEGER ){
101504
- if( aSample[i].u.i>=v ){
101505
- isEq = aSample[i].u.i==v;
101506
- break;
101507
- }
101508
- }else{
101509
- assert( aSample[i].eType==SQLITE_FLOAT );
101510
- if( aSample[i].u.r>=r ){
101511
- isEq = aSample[i].u.r==r;
101512
- break;
101513
- }
101514
- }
101515
- }
101516
- }else if( eType==SQLITE_FLOAT ){
101517
- r = sqlite3_value_double(pVal);
101518
- for(i=0; i<pIdx->nSample; i++){
101519
- if( aSample[i].eType==SQLITE_NULL ) continue;
101520
- if( aSample[i].eType>=SQLITE_TEXT ) break;
101521
- if( aSample[i].eType==SQLITE_FLOAT ){
101522
- rS = aSample[i].u.r;
101523
- }else{
101524
- rS = aSample[i].u.i;
101525
- }
101526
- if( rS>=r ){
101527
- isEq = rS==r;
101528
- break;
101529
- }
101530
- }
101531
- }else if( eType==SQLITE_NULL ){
101532
- i = 0;
101533
- if( pIdx->nSample>=1 && aSample[0].eType==SQLITE_NULL ) isEq = 1;
101534
- }else{
101535
- assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
101536
- for(i=0; i<pIdx->nSample; i++){
101537
- if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
101538
- break;
101539
- }
101540
- }
101541
- if( i<pIdx->nSample ){
102506
+ if( ALWAYS(pVal) ){
102507
+ IndexSample *aSample = pIdx->aSample;
102508
+ int i = 0;
102509
+ int eType = sqlite3_value_type(pVal);
102510
+
102511
+ if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
102512
+ double r = sqlite3_value_double(pVal);
102513
+ for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102514
+ if( aSample[i].eType==SQLITE_NULL ) continue;
102515
+ if( aSample[i].eType>=SQLITE_TEXT ) break;
102516
+ if( roundUp ){
102517
+ if( aSample[i].u.r>r ) break;
102518
+ }else{
102519
+ if( aSample[i].u.r>=r ) break;
102520
+ }
102521
+ }
102522
+ }else if( eType==SQLITE_NULL ){
102523
+ i = 0;
102524
+ if( roundUp ){
102525
+ while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
102526
+ }
102527
+ }else{
101542102528
sqlite3 *db = pParse->db;
101543102529
CollSeq *pColl;
101544102530
const u8 *z;
102531
+ int n;
102532
+
102533
+ /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
102534
+ assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
102535
+
101545102536
if( eType==SQLITE_BLOB ){
101546102537
z = (const u8 *)sqlite3_value_blob(pVal);
101547102538
pColl = db->pDfltColl;
101548102539
assert( pColl->enc==SQLITE_UTF8 );
101549102540
}else{
@@ -101558,16 +102549,16 @@
101558102549
return SQLITE_NOMEM;
101559102550
}
101560102551
assert( z && pColl && pColl->xCmp );
101561102552
}
101562102553
n = sqlite3ValueBytes(pVal, pColl->enc);
101563
-
101564
- for(; i<pIdx->nSample; i++){
102554
+
102555
+ for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
101565102556
int c;
101566102557
int eSampletype = aSample[i].eType;
101567
- if( eSampletype<eType ) continue;
101568
- if( eSampletype!=eType ) break;
102558
+ if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
102559
+ if( (eSampletype!=eType) ) break;
101569102560
#ifndef SQLITE_OMIT_UTF16
101570102561
if( pColl->enc!=SQLITE_UTF8 ){
101571102562
int nSample;
101572102563
char *zSample = sqlite3Utf8to16(
101573102564
db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -101581,52 +102572,20 @@
101581102572
}else
101582102573
#endif
101583102574
{
101584102575
c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
101585102576
}
101586
- if( c>=0 ){
101587
- if( c==0 ) isEq = 1;
101588
- break;
101589
- }
101590
- }
101591
- }
101592
- }
101593
-
101594
- /* At this point, aSample[i] is the first sample that is greater than
101595
- ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
101596
- ** than pVal. If aSample[i]==pVal, then isEq==1.
101597
- */
101598
- if( isEq ){
101599
- assert( i<pIdx->nSample );
101600
- aStat[0] = aSample[i].nLt;
101601
- aStat[1] = aSample[i].nEq;
101602
- }else{
101603
- tRowcnt iLower, iUpper, iGap;
101604
- if( i==0 ){
101605
- iLower = 0;
101606
- iUpper = aSample[0].nLt;
101607
- }else{
101608
- iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
101609
- iLower = aSample[i-1].nEq + aSample[i-1].nLt;
101610
- }
101611
- aStat[1] = pIdx->avgEq;
101612
- if( iLower>=iUpper ){
101613
- iGap = 0;
101614
- }else{
101615
- iGap = iUpper - iLower;
101616
- if( iGap>=aStat[1]/2 ) iGap -= aStat[1]/2;
101617
- }
101618
- if( roundUp ){
101619
- iGap = (iGap*2)/3;
101620
- }else{
101621
- iGap = iGap/3;
101622
- }
101623
- aStat[0] = iLower + iGap;
102577
+ if( c-roundUp>=0 ) break;
102578
+ }
102579
+ }
102580
+
102581
+ assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
102582
+ *piRegion = i;
101624102583
}
101625102584
return SQLITE_OK;
101626102585
}
101627
-#endif /* SQLITE_ENABLE_STAT3 */
102586
+#endif /* #ifdef SQLITE_ENABLE_STAT2 */
101628102587
101629102588
/*
101630102589
** If expression pExpr represents a literal value, set *pp to point to
101631102590
** an sqlite3_value structure containing the same value, with affinity
101632102591
** aff applied to it, before returning. It is the responsibility of the
@@ -101640,11 +102599,11 @@
101640102599
**
101641102600
** If neither of the above apply, set *pp to NULL.
101642102601
**
101643102602
** If an error occurs, return an error code. Otherwise, SQLITE_OK.
101644102603
*/
101645
-#ifdef SQLITE_ENABLE_STAT3
102604
+#ifdef SQLITE_ENABLE_STAT2
101646102605
static int valueFromExpr(
101647102606
Parse *pParse,
101648102607
Expr *pExpr,
101649102608
u8 aff,
101650102609
sqlite3_value **pp
@@ -101688,92 +102647,106 @@
101688102647
**
101689102648
** ... FROM t1 WHERE a > ? AND a < ? ...
101690102649
**
101691102650
** then nEq should be passed 0.
101692102651
**
101693
-** The returned value is an integer divisor to reduce the estimated
101694
-** search space. A return value of 1 means that range constraints are
101695
-** no help at all. A return value of 2 means range constraints are
101696
-** expected to reduce the search space by half. And so forth...
102652
+** The returned value is an integer between 1 and 100, inclusive. A return
102653
+** value of 1 indicates that the proposed range scan is expected to visit
102654
+** approximately 1/100th (1%) of the rows selected by the nEq equality
102655
+** constraints (if any). A return value of 100 indicates that it is expected
102656
+** that the range scan will visit every row (100%) selected by the equality
102657
+** constraints.
101697102658
**
101698
-** In the absence of sqlite_stat3 ANALYZE data, each range inequality
101699
-** reduces the search space by a factor of 4. Hence a single constraint (x>?)
101700
-** results in a return of 4 and a range constraint (x>? AND x<?) results
101701
-** in a return of 16.
102659
+** In the absence of sqlite_stat2 ANALYZE data, each range inequality
102660
+** reduces the search space by 3/4ths. Hence a single constraint (x>?)
102661
+** results in a return of 25 and a range constraint (x>? AND x<?) results
102662
+** in a return of 6.
101702102663
*/
101703102664
static int whereRangeScanEst(
101704102665
Parse *pParse, /* Parsing & code generating context */
101705102666
Index *p, /* The index containing the range-compared column; "x" */
101706102667
int nEq, /* index into p->aCol[] of the range-compared column */
101707102668
WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
101708102669
WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
101709
- double *pRangeDiv /* OUT: Reduce search space by this divisor */
102670
+ int *piEst /* OUT: Return value */
101710102671
){
101711102672
int rc = SQLITE_OK;
101712102673
101713
-#ifdef SQLITE_ENABLE_STAT3
102674
+#ifdef SQLITE_ENABLE_STAT2
101714102675
101715
- if( nEq==0 && p->nSample ){
101716
- sqlite3_value *pRangeVal;
101717
- tRowcnt iLower = 0;
101718
- tRowcnt iUpper = p->aiRowEst[0];
101719
- tRowcnt a[2];
102676
+ if( nEq==0 && p->aSample ){
102677
+ sqlite3_value *pLowerVal = 0;
102678
+ sqlite3_value *pUpperVal = 0;
102679
+ int iEst;
102680
+ int iLower = 0;
102681
+ int iUpper = SQLITE_INDEX_SAMPLES;
102682
+ int roundUpUpper = 0;
102683
+ int roundUpLower = 0;
101720102684
u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101721102685
101722102686
if( pLower ){
101723102687
Expr *pExpr = pLower->pExpr->pRight;
101724
- rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
102688
+ rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
101725102689
assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
101726
- if( rc==SQLITE_OK
101727
- && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
101728
- ){
101729
- iLower = a[0];
101730
- if( pLower->eOperator==WO_GT ) iLower += a[1];
101731
- }
101732
- sqlite3ValueFree(pRangeVal);
102690
+ roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
101733102691
}
101734102692
if( rc==SQLITE_OK && pUpper ){
101735102693
Expr *pExpr = pUpper->pExpr->pRight;
101736
- rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
102694
+ rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
101737102695
assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
101738
- if( rc==SQLITE_OK
101739
- && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
101740
- ){
101741
- iUpper = a[0];
101742
- if( pUpper->eOperator==WO_LE ) iUpper += a[1];
101743
- }
101744
- sqlite3ValueFree(pRangeVal);
101745
- }
101746
- if( rc==SQLITE_OK ){
101747
- if( iUpper<=iLower ){
101748
- *pRangeDiv = (double)p->aiRowEst[0];
101749
- }else{
101750
- *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
101751
- }
101752
- WHERETRACE(("range scan regions: %u..%u div=%g\n",
101753
- (u32)iLower, (u32)iUpper, *pRangeDiv));
101754
- return SQLITE_OK;
101755
- }
101756
- }
102696
+ roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
102697
+ }
102698
+
102699
+ if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
102700
+ sqlite3ValueFree(pLowerVal);
102701
+ sqlite3ValueFree(pUpperVal);
102702
+ goto range_est_fallback;
102703
+ }else if( pLowerVal==0 ){
102704
+ rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
102705
+ if( pLower ) iLower = iUpper/2;
102706
+ }else if( pUpperVal==0 ){
102707
+ rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
102708
+ if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
102709
+ }else{
102710
+ rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
102711
+ if( rc==SQLITE_OK ){
102712
+ rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
102713
+ }
102714
+ }
102715
+ WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
102716
+
102717
+ iEst = iUpper - iLower;
102718
+ testcase( iEst==SQLITE_INDEX_SAMPLES );
102719
+ assert( iEst<=SQLITE_INDEX_SAMPLES );
102720
+ if( iEst<1 ){
102721
+ *piEst = 50/SQLITE_INDEX_SAMPLES;
102722
+ }else{
102723
+ *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
102724
+ }
102725
+ sqlite3ValueFree(pLowerVal);
102726
+ sqlite3ValueFree(pUpperVal);
102727
+ return rc;
102728
+ }
102729
+range_est_fallback:
101757102730
#else
101758102731
UNUSED_PARAMETER(pParse);
101759102732
UNUSED_PARAMETER(p);
101760102733
UNUSED_PARAMETER(nEq);
101761102734
#endif
101762102735
assert( pLower || pUpper );
101763
- *pRangeDiv = (double)1;
101764
- if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
101765
- if( pUpper ) *pRangeDiv *= (double)4;
102736
+ *piEst = 100;
102737
+ if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
102738
+ if( pUpper ) *piEst /= 4;
101766102739
return rc;
101767102740
}
101768102741
101769
-#ifdef SQLITE_ENABLE_STAT3
102742
+#ifdef SQLITE_ENABLE_STAT2
101770102743
/*
101771102744
** Estimate the number of rows that will be returned based on
101772102745
** an equality constraint x=VALUE and where that VALUE occurs in
101773102746
** the histogram data. This only works when x is the left-most
101774
-** column of an index and sqlite_stat3 histogram data is available
102747
+** column of an index and sqlite_stat2 histogram data is available
101775102748
** for that index. When pExpr==NULL that means the constraint is
101776102749
** "x IS NULL" instead of "x=VALUE".
101777102750
**
101778102751
** Write the estimated row count into *pnRow and return SQLITE_OK.
101779102752
** If unable to make an estimate, leave *pnRow unchanged and return
@@ -101789,13 +102762,14 @@
101789102762
Index *p, /* The index whose left-most column is pTerm */
101790102763
Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
101791102764
double *pnRow /* Write the revised row estimate here */
101792102765
){
101793102766
sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
102767
+ int iLower, iUpper; /* Range of histogram regions containing pRhs */
101794102768
u8 aff; /* Column affinity */
101795102769
int rc; /* Subfunction return code */
101796
- tRowcnt a[2]; /* Statistics */
102770
+ double nRowEst; /* New estimate of the number of rows */
101797102771
101798102772
assert( p->aSample!=0 );
101799102773
aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101800102774
if( pExpr ){
101801102775
rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
@@ -101802,22 +102776,30 @@
101802102776
if( rc ) goto whereEqualScanEst_cancel;
101803102777
}else{
101804102778
pRhs = sqlite3ValueNew(pParse->db);
101805102779
}
101806102780
if( pRhs==0 ) return SQLITE_NOTFOUND;
101807
- rc = whereKeyStats(pParse, p, pRhs, 0, a);
101808
- if( rc==SQLITE_OK ){
101809
- WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
101810
- *pnRow = a[1];
102781
+ rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
102782
+ if( rc ) goto whereEqualScanEst_cancel;
102783
+ rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
102784
+ if( rc ) goto whereEqualScanEst_cancel;
102785
+ WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
102786
+ if( iLower>=iUpper ){
102787
+ nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
102788
+ if( nRowEst<*pnRow ) *pnRow = nRowEst;
102789
+ }else{
102790
+ nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
102791
+ *pnRow = nRowEst;
101811102792
}
102793
+
101812102794
whereEqualScanEst_cancel:
101813102795
sqlite3ValueFree(pRhs);
101814102796
return rc;
101815102797
}
101816
-#endif /* defined(SQLITE_ENABLE_STAT3) */
102798
+#endif /* defined(SQLITE_ENABLE_STAT2) */
101817102799
101818
-#ifdef SQLITE_ENABLE_STAT3
102800
+#ifdef SQLITE_ENABLE_STAT2
101819102801
/*
101820102802
** Estimate the number of rows that will be returned based on
101821102803
** an IN constraint where the right-hand side of the IN operator
101822102804
** is a list of values. Example:
101823102805
**
@@ -101836,29 +102818,64 @@
101836102818
Parse *pParse, /* Parsing & code generating context */
101837102819
Index *p, /* The index whose left-most column is pTerm */
101838102820
ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
101839102821
double *pnRow /* Write the revised row estimate here */
101840102822
){
101841
- int rc = SQLITE_OK; /* Subfunction return code */
101842
- double nEst; /* Number of rows for a single term */
101843
- double nRowEst = (double)0; /* New estimate of the number of rows */
101844
- int i; /* Loop counter */
102823
+ sqlite3_value *pVal = 0; /* One value from list */
102824
+ int iLower, iUpper; /* Range of histogram regions containing pRhs */
102825
+ u8 aff; /* Column affinity */
102826
+ int rc = SQLITE_OK; /* Subfunction return code */
102827
+ double nRowEst; /* New estimate of the number of rows */
102828
+ int nSpan = 0; /* Number of histogram regions spanned */
102829
+ int nSingle = 0; /* Histogram regions hit by a single value */
102830
+ int nNotFound = 0; /* Count of values that are not constants */
102831
+ int i; /* Loop counter */
102832
+ u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
102833
+ u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
101845102834
101846102835
assert( p->aSample!=0 );
101847
- for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
101848
- nEst = p->aiRowEst[0];
101849
- rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
101850
- nRowEst += nEst;
102836
+ aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102837
+ memset(aSpan, 0, sizeof(aSpan));
102838
+ memset(aSingle, 0, sizeof(aSingle));
102839
+ for(i=0; i<pList->nExpr; i++){
102840
+ sqlite3ValueFree(pVal);
102841
+ rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
102842
+ if( rc ) break;
102843
+ if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
102844
+ nNotFound++;
102845
+ continue;
102846
+ }
102847
+ rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
102848
+ if( rc ) break;
102849
+ rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
102850
+ if( rc ) break;
102851
+ if( iLower>=iUpper ){
102852
+ aSingle[iLower] = 1;
102853
+ }else{
102854
+ assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
102855
+ while( iLower<iUpper ) aSpan[iLower++] = 1;
102856
+ }
101851102857
}
101852102858
if( rc==SQLITE_OK ){
102859
+ for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
102860
+ if( aSpan[i] ){
102861
+ nSpan++;
102862
+ }else if( aSingle[i] ){
102863
+ nSingle++;
102864
+ }
102865
+ }
102866
+ nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
102867
+ + nNotFound*p->aiRowEst[1];
101853102868
if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
101854102869
*pnRow = nRowEst;
101855
- WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
102870
+ WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
102871
+ nSpan, nSingle, nNotFound, nRowEst));
101856102872
}
102873
+ sqlite3ValueFree(pVal);
101857102874
return rc;
101858102875
}
101859
-#endif /* defined(SQLITE_ENABLE_STAT3) */
102876
+#endif /* defined(SQLITE_ENABLE_STAT2) */
101860102877
101861102878
101862102879
/*
101863102880
** Find the best query plan for accessing a particular table. Write the
101864102881
** best query plan and its cost into the WhereCost object supplied as the
@@ -101901,11 +102918,11 @@
101901102918
Index *pProbe; /* An index we are evaluating */
101902102919
Index *pIdx; /* Copy of pProbe, or zero for IPK index */
101903102920
int eqTermMask; /* Current mask of valid equality operators */
101904102921
int idxEqTermMask; /* Index mask of valid equality operators */
101905102922
Index sPk; /* A fake index object for the primary key */
101906
- tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
102923
+ unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
101907102924
int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
101908102925
int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
101909102926
101910102927
/* Initialize the cost to a worst-case value */
101911102928
memset(pCost, 0, sizeof(*pCost));
@@ -101956,11 +102973,11 @@
101956102973
}
101957102974
101958102975
/* Loop over all indices looking for the best one to use
101959102976
*/
101960102977
for(; pProbe; pIdx=pProbe=pProbe->pNext){
101961
- const tRowcnt * const aiRowEst = pProbe->aiRowEst;
102978
+ const unsigned int * const aiRowEst = pProbe->aiRowEst;
101962102979
double cost; /* Cost of using pProbe */
101963102980
double nRow; /* Estimated number of rows in result set */
101964102981
double log10N; /* base-10 logarithm of nRow (inexact) */
101965102982
int rev; /* True to scan in reverse order */
101966102983
int wsFlags = 0;
@@ -101999,16 +103016,18 @@
101999103016
** Set to true if there was at least one "x IN (SELECT ...)" term used
102000103017
** in determining the value of nInMul. Note that the RHS of the
102001103018
** IN operator must be a SELECT, not a value list, for this variable
102002103019
** to be true.
102003103020
**
102004
- ** rangeDiv:
102005
- ** An estimate of a divisor by which to reduce the search space due
102006
- ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE
102007
- ** data, a single inequality reduces the search space to 1/4rd its
102008
- ** original size (rangeDiv==4). Two inequalities reduce the search
102009
- ** space to 1/16th of its original size (rangeDiv==16).
103021
+ ** estBound:
103022
+ ** An estimate on the amount of the table that must be searched. A
103023
+ ** value of 100 means the entire table is searched. Range constraints
103024
+ ** might reduce this to a value less than 100 to indicate that only
103025
+ ** a fraction of the table needs searching. In the absence of
103026
+ ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
103027
+ ** space to 1/4rd its original size. So an x>? constraint reduces
103028
+ ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
102010103029
**
102011103030
** bSort:
102012103031
** Boolean. True if there is an ORDER BY clause that will require an
102013103032
** external sort (i.e. scanning the index being evaluated will not
102014103033
** correctly order records).
@@ -102029,17 +103048,17 @@
102029103048
** SELECT a, b, c FROM tbl WHERE a = 1;
102030103049
*/
102031103050
int nEq; /* Number of == or IN terms matching index */
102032103051
int bInEst = 0; /* True if "x IN (SELECT...)" seen */
102033103052
int nInMul = 1; /* Number of distinct equalities to lookup */
102034
- double rangeDiv = (double)1; /* Estimated reduction in search space */
103053
+ int estBound = 100; /* Estimated reduction in search space */
102035103054
int nBound = 0; /* Number of range constraints seen */
102036103055
int bSort = !!pOrderBy; /* True if external sort required */
102037103056
int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
102038103057
int bLookup = 0; /* True if not a covering index */
102039103058
WhereTerm *pTerm; /* A single term of the WHERE clause */
102040
-#ifdef SQLITE_ENABLE_STAT3
103059
+#ifdef SQLITE_ENABLE_STAT2
102041103060
WhereTerm *pFirstTerm = 0; /* First term matching the index */
102042103061
#endif
102043103062
102044103063
/* Determine the values of nEq and nInMul */
102045103064
for(nEq=0; nEq<pProbe->nColumn; nEq++){
@@ -102059,23 +103078,23 @@
102059103078
nInMul *= pExpr->x.pList->nExpr;
102060103079
}
102061103080
}else if( pTerm->eOperator & WO_ISNULL ){
102062103081
wsFlags |= WHERE_COLUMN_NULL;
102063103082
}
102064
-#ifdef SQLITE_ENABLE_STAT3
103083
+#ifdef SQLITE_ENABLE_STAT2
102065103084
if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
102066103085
#endif
102067103086
used |= pTerm->prereqRight;
102068103087
}
102069103088
102070
- /* Determine the value of rangeDiv */
103089
+ /* Determine the value of estBound. */
102071103090
if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
102072103091
int j = pProbe->aiColumn[nEq];
102073103092
if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
102074103093
WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
102075103094
WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
102076
- whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
103095
+ whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
102077103096
if( pTop ){
102078103097
nBound = 1;
102079103098
wsFlags |= WHERE_TOP_LIMIT;
102080103099
used |= pTop->prereqRight;
102081103100
}
@@ -102143,11 +103162,11 @@
102143103162
if( bInEst && nRow*2>aiRowEst[0] ){
102144103163
nRow = aiRowEst[0]/2;
102145103164
nInMul = (int)(nRow / aiRowEst[nEq]);
102146103165
}
102147103166
102148
-#ifdef SQLITE_ENABLE_STAT3
103167
+#ifdef SQLITE_ENABLE_STAT2
102149103168
/* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
102150103169
** and we do not think that values of x are unique and if histogram
102151103170
** data is available for column x, then it might be possible
102152103171
** to get a better estimate on the number of rows based on
102153103172
** VALUE and how common that value is according to the histogram.
@@ -102159,16 +103178,16 @@
102159103178
whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
102160103179
}else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
102161103180
whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
102162103181
}
102163103182
}
102164
-#endif /* SQLITE_ENABLE_STAT3 */
103183
+#endif /* SQLITE_ENABLE_STAT2 */
102165103184
102166103185
/* Adjust the number of output rows and downward to reflect rows
102167103186
** that are excluded by range constraints.
102168103187
*/
102169
- nRow = nRow/rangeDiv;
103188
+ nRow = (nRow * (double)estBound) / (double)100;
102170103189
if( nRow<1 ) nRow = 1;
102171103190
102172103191
/* Experiments run on real SQLite databases show that the time needed
102173103192
** to do a binary search to locate a row in a table or index is roughly
102174103193
** log10(N) times the time to move from one row to the next row within
@@ -102293,14 +103312,14 @@
102293103312
if( nRow<2 ) nRow = 2;
102294103313
}
102295103314
102296103315
102297103316
WHERETRACE((
102298
- "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103317
+ "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
102299103318
" notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
102300103319
pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
102301
- nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
103320
+ nEq, nInMul, estBound, bSort, bLookup, wsFlags,
102302103321
notReady, log10N, nRow, cost, used
102303103322
));
102304103323
102305103324
/* If this index is the best we have seen so far, then record this
102306103325
** index and its cost in the pCost structure.
@@ -104227,10 +105246,11 @@
104227105246
** LALR(1) grammar but which are always false in the
104228105247
** specific grammar used by SQLite.
104229105248
*/
104230105249
/* First off, code is included that follows the "include" declaration
104231105250
** in the input grammar file. */
105251
+/* #include <stdio.h> */
104232105252
104233105253
104234105254
/*
104235105255
** Disable all error recovery processing in the parser push-down
104236105256
** automaton.
@@ -105087,10 +106107,11 @@
105087106107
#endif
105088106108
};
105089106109
typedef struct yyParser yyParser;
105090106110
105091106111
#ifndef NDEBUG
106112
+/* #include <stdio.h> */
105092106113
static FILE *yyTraceFILE = 0;
105093106114
static char *yyTracePrompt = 0;
105094106115
#endif /* NDEBUG */
105095106116
105096106117
#ifndef NDEBUG
@@ -107662,10 +108683,11 @@
107662108683
**
107663108684
** This file contains C code that splits an SQL input string up into
107664108685
** individual tokens and sends those tokens one-by-one over to the
107665108686
** parser for analysis.
107666108687
*/
108688
+/* #include <stdlib.h> */
107667108689
107668108690
/*
107669108691
** The charMap() macro maps alphabetic characters into their
107670108692
** lower-case ASCII equivalent. On ASCII machines, this is just
107671108693
** an upper-to-lower case map. On EBCDIC machines we also need
@@ -109053,10 +110075,20 @@
109053110075
memcpy(&y, &x, 8);
109054110076
assert( sqlite3IsNaN(y) );
109055110077
}
109056110078
#endif
109057110079
#endif
110080
+
110081
+ /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
110082
+ ** compile-time option.
110083
+ */
110084
+#ifdef SQLITE_EXTRA_INIT
110085
+ if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
110086
+ int SQLITE_EXTRA_INIT(void);
110087
+ rc = SQLITE_EXTRA_INIT();
110088
+ }
110089
+#endif
109058110090
109059110091
return rc;
109060110092
}
109061110093
109062110094
/*
@@ -113184,10 +114216,16 @@
113184114216
113185114217
#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
113186114218
# define SQLITE_CORE 1
113187114219
#endif
113188114220
114221
+/* #include <assert.h> */
114222
+/* #include <stdlib.h> */
114223
+/* #include <stddef.h> */
114224
+/* #include <stdio.h> */
114225
+/* #include <string.h> */
114226
+/* #include <stdarg.h> */
113189114227
113190114228
#ifndef SQLITE_CORE
113191114229
SQLITE_EXTENSION_INIT1
113192114230
#endif
113193114231
@@ -117705,10 +118743,12 @@
117705118743
******************************************************************************
117706118744
**
117707118745
*/
117708118746
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
117709118747
118748
+/* #include <string.h> */
118749
+/* #include <assert.h> */
117710118750
117711118751
typedef struct Fts3auxTable Fts3auxTable;
117712118752
typedef struct Fts3auxCursor Fts3auxCursor;
117713118753
117714118754
struct Fts3auxTable {
@@ -118243,10 +119283,12 @@
118243119283
/*
118244119284
** Default span for NEAR operators.
118245119285
*/
118246119286
#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
118247119287
119288
+/* #include <string.h> */
119289
+/* #include <assert.h> */
118248119290
118249119291
/*
118250119292
** isNot:
118251119293
** This variable is used by function getNextNode(). When getNextNode() is
118252119294
** called, it sets ParseContext.isNot to true if the 'next node' is a
@@ -118944,10 +119986,11 @@
118944119986
** Everything after this point is just test code.
118945119987
*/
118946119988
118947119989
#ifdef SQLITE_TEST
118948119990
119991
+/* #include <stdio.h> */
118949119992
118950119993
/*
118951119994
** Function to query the hash-table of tokenizers (see README.tokenizers).
118952119995
*/
118953119996
static int queryTestTokenizer(
@@ -119154,10 +120197,13 @@
119154120197
** * The FTS3 module is being built into the core of
119155120198
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119156120199
*/
119157120200
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119158120201
120202
+/* #include <assert.h> */
120203
+/* #include <stdlib.h> */
120204
+/* #include <string.h> */
119159120205
119160120206
119161120207
/*
119162120208
** Malloc and Free functions
119163120209
*/
@@ -119534,10 +120580,14 @@
119534120580
** * The FTS3 module is being built into the core of
119535120581
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119536120582
*/
119537120583
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119538120584
120585
+/* #include <assert.h> */
120586
+/* #include <stdlib.h> */
120587
+/* #include <stdio.h> */
120588
+/* #include <string.h> */
119539120589
119540120590
119541120591
/*
119542120592
** Class derived from sqlite3_tokenizer
119543120593
*/
@@ -120177,10 +121227,12 @@
120177121227
** * The FTS3 module is being built into the core of
120178121228
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120179121229
*/
120180121230
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120181121231
121232
+/* #include <assert.h> */
121233
+/* #include <string.h> */
120182121234
120183121235
/*
120184121236
** Implementation of the SQL scalar function for accessing the underlying
120185121237
** hash table. This function may be called as follows:
120186121238
**
@@ -120352,10 +121404,12 @@
120352121404
}
120353121405
120354121406
120355121407
#ifdef SQLITE_TEST
120356121408
121409
+/* #include <tcl.h> */
121410
+/* #include <string.h> */
120357121411
120358121412
/*
120359121413
** Implementation of a special SQL scalar function for testing tokenizers
120360121414
** designed to be used in concert with the Tcl testing framework. This
120361121415
** function must be called with two arguments:
@@ -120663,10 +121717,14 @@
120663121717
** * The FTS3 module is being built into the core of
120664121718
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120665121719
*/
120666121720
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120667121721
121722
+/* #include <assert.h> */
121723
+/* #include <stdlib.h> */
121724
+/* #include <stdio.h> */
121725
+/* #include <string.h> */
120668121726
120669121727
120670121728
typedef struct simple_tokenizer {
120671121729
sqlite3_tokenizer base;
120672121730
char delim[128]; /* flag ASCII delimiters */
@@ -120888,10 +121946,13 @@
120888121946
** code in fts3.c.
120889121947
*/
120890121948
120891121949
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120892121950
121951
+/* #include <string.h> */
121952
+/* #include <assert.h> */
121953
+/* #include <stdlib.h> */
120893121954
120894121955
/*
120895121956
** When full-text index nodes are loaded from disk, the buffer that they
120896121957
** are loaded into has the following number of bytes of padding at the end
120897121958
** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
@@ -124149,10 +125210,12 @@
124149125210
******************************************************************************
124150125211
*/
124151125212
124152125213
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
124153125214
125215
+/* #include <string.h> */
125216
+/* #include <assert.h> */
124154125217
124155125218
/*
124156125219
** Characters that may appear in the second argument to matchinfo().
124157125220
*/
124158125221
#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
@@ -125736,10 +126799,12 @@
125736126799
#ifndef SQLITE_CORE
125737126800
SQLITE_EXTENSION_INIT1
125738126801
#else
125739126802
#endif
125740126803
126804
+/* #include <string.h> */
126805
+/* #include <assert.h> */
125741126806
125742126807
#ifndef SQLITE_AMALGAMATION
125743126808
#include "sqlite3rtree.h"
125744126809
typedef sqlite3_int64 i64;
125745126810
typedef unsigned char u8;
@@ -128950,10 +130015,11 @@
128950130015
#include <unicode/utypes.h>
128951130016
#include <unicode/uregex.h>
128952130017
#include <unicode/ustring.h>
128953130018
#include <unicode/ucol.h>
128954130019
130020
+/* #include <assert.h> */
128955130021
128956130022
#ifndef SQLITE_CORE
128957130023
SQLITE_EXTENSION_INIT1
128958130024
#else
128959130025
#endif
@@ -129429,12 +130495,16 @@
129429130495
** This file implements a tokenizer for fts3 based on the ICU library.
129430130496
*/
129431130497
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
129432130498
#ifdef SQLITE_ENABLE_ICU
129433130499
130500
+/* #include <assert.h> */
130501
+/* #include <string.h> */
129434130502
129435130503
#include <unicode/ubrk.h>
130504
+/* #include <unicode/ucol.h> */
130505
+/* #include <unicode/ustring.h> */
129436130506
#include <unicode/utf16.h>
129437130507
129438130508
typedef struct IcuTokenizer IcuTokenizer;
129439130509
typedef struct IcuCursor IcuCursor;
129440130510
129441130511
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -385,23 +385,29 @@
385 /*
386 ** Exactly one of the following macros must be defined in order to
387 ** specify which memory allocation subsystem to use.
388 **
389 ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
 
390 ** SQLITE_MEMDEBUG // Debugging version of system malloc()
 
 
 
 
 
391 **
392 ** (Historical note: There used to be several other options, but we've
393 ** pared it down to just these two.)
394 **
395 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
396 ** the default.
397 */
398 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1
399 # error "At most one of the following compile-time configuration options\
400 is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG"
401 #endif
402 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0
403 # define SQLITE_SYSTEM_MALLOC 1
404 #endif
405
406 /*
407 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -650,11 +656,11 @@
650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651 ** [sqlite_version()] and [sqlite_source_id()].
652 */
653 #define SQLITE_VERSION "3.7.8"
654 #define SQLITE_VERSION_NUMBER 3007008
655 #define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -1751,20 +1757,14 @@
1751 ** also used during testing of SQLite in order to specify an alternative
1752 ** memory allocator that simulates memory out-of-memory conditions in
1753 ** order to verify that SQLite recovers gracefully from such
1754 ** conditions.
1755 **
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 ** 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 **
1767 ** xSize should return the allocated size of a memory allocation
1768 ** previously obtained from xMalloc or xRealloc. The allocated size
1769 ** is always at least as big as the requested size but may be larger.
1770 **
@@ -3397,11 +3397,11 @@
3397 ** a schema change, on the first [sqlite3_step()] call following any change
3398 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3399 ** ^The specific value of WHERE-clause [parameter] might influence the
3400 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
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.
3403 ** the
3404 ** </li>
3405 ** </ol>
3406 */
3407 SQLITE_API int sqlite3_prepare(
@@ -7632,10 +7632,18 @@
7632 */
7633 #ifndef SQLITE_TEMP_STORE
7634 # define SQLITE_TEMP_STORE 1
7635 #endif
7636
 
 
 
 
 
 
 
 
7637 /*
7638 ** GCC does not define the offsetof() macro so we'll have to do it
7639 ** ourselves.
7640 */
7641 #ifndef offsetof
@@ -7711,22 +7719,10 @@
7711 ** is 0x00000000ffffffff. But because of quirks of some compilers, we
7712 ** have to specify the value in the less intuitive manner shown:
7713 */
7714 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
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 /*
7729 ** Macros to determine whether the machine is big or little endian,
7730 ** evaluated at runtime.
7731 */
7732 #ifdef SQLITE_AMALGAMATION
@@ -7986,10 +7982,11 @@
7986 #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
7987 #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */
7988 #define BTREE_MEMORY 4 /* This is an in-memory DB */
7989 #define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */
7990 #define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */
 
7991
7992 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
7993 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
7994 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
7995 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
@@ -8188,10 +8185,11 @@
8188 ** or VDBE. The VDBE implements an abstract machine that runs a
8189 ** simple program to access and modify the underlying database.
8190 */
8191 #ifndef _SQLITE_VDBE_H_
8192 #define _SQLITE_VDBE_H_
 
8193
8194 /*
8195 ** A single VDBE is an opaque structure named "Vdbe". Only routines
8196 ** in the source file sqliteVdbe.c are allowed to see the insides
8197 ** of this structure.
@@ -8231,10 +8229,11 @@
8231 Mem *pMem; /* Used when p4type is P4_MEM */
8232 VTable *pVtab; /* Used when p4type is P4_VTAB */
8233 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
8234 int *ai; /* Used when p4type is P4_INTARRAY */
8235 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
 
8236 } p4;
8237 #ifdef SQLITE_DEBUG
8238 char *zComment; /* Comment to improve readability */
8239 #endif
8240 #ifdef VDBE_PROFILE
@@ -8286,10 +8285,11 @@
8286 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
8287 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
8288 #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
8289 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
8290 #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
 
8291
8292 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
8293 ** is made. That copy is freed when the Vdbe is finalized. But if the
8294 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
8295 ** gets freed when the Vdbe is finalized so it still should be obtained
@@ -8399,89 +8399,89 @@
8399 #define OP_ReadCookie 35
8400 #define OP_SetCookie 36
8401 #define OP_VerifyCookie 37
8402 #define OP_OpenRead 38
8403 #define OP_OpenWrite 39
8404 #define OP_OpenAutoindex 40
8405 #define OP_OpenEphemeral 41
8406 #define OP_OpenPseudo 42
8407 #define OP_Close 43
8408 #define OP_SeekLt 44
8409 #define OP_SeekLe 45
8410 #define OP_SeekGe 46
8411 #define OP_SeekGt 47
8412 #define OP_Seek 48
8413 #define OP_NotFound 49
8414 #define OP_Found 50
8415 #define OP_IsUnique 51
8416 #define OP_NotExists 52
8417 #define OP_Sequence 53
8418 #define OP_NewRowid 54
8419 #define OP_Insert 55
8420 #define OP_InsertInt 56
8421 #define OP_Delete 57
8422 #define OP_ResetCount 58
8423 #define OP_RowKey 59
8424 #define OP_RowData 60
8425 #define OP_Rowid 61
8426 #define OP_NullRow 62
8427 #define OP_Last 63
8428 #define OP_Sort 64
8429 #define OP_Rewind 65
8430 #define OP_Prev 66
8431 #define OP_Next 67
8432 #define OP_IdxInsert 70
8433 #define OP_IdxDelete 71
8434 #define OP_IdxRowid 72
8435 #define OP_IdxLT 81
8436 #define OP_IdxGE 92
8437 #define OP_Destroy 95
8438 #define OP_Clear 96
8439 #define OP_CreateIndex 97
8440 #define OP_CreateTable 98
8441 #define OP_ParseSchema 99
8442 #define OP_LoadAnalysis 100
8443 #define OP_DropTable 101
8444 #define OP_DropIndex 102
8445 #define OP_DropTrigger 103
8446 #define OP_IntegrityCk 104
8447 #define OP_RowSetAdd 105
8448 #define OP_RowSetRead 106
8449 #define OP_RowSetTest 107
8450 #define OP_Program 108
8451 #define OP_Param 109
8452 #define OP_FkCounter 110
8453 #define OP_FkIfZero 111
8454 #define OP_MemMax 112
8455 #define OP_IfPos 113
8456 #define OP_IfNeg 114
8457 #define OP_IfZero 115
8458 #define OP_AggStep 116
8459 #define OP_AggFinal 117
8460 #define OP_Checkpoint 118
8461 #define OP_JournalMode 119
8462 #define OP_Vacuum 120
8463 #define OP_IncrVacuum 121
8464 #define OP_Expire 122
8465 #define OP_TableLock 123
8466 #define OP_VBegin 124
8467 #define OP_VCreate 125
8468 #define OP_VDestroy 126
8469 #define OP_VOpen 127
8470 #define OP_VFilter 128
8471 #define OP_VColumn 129
8472 #define OP_VNext 131
8473 #define OP_VRename 132
8474 #define OP_VUpdate 133
8475 #define OP_Pagecount 134
8476 #define OP_MaxPgcnt 135
8477 #define OP_Trace 136
8478 #define OP_Noop 137
8479 #define OP_Explain 138
8480
8481 /* The following opcode values are never used */
8482 #define OP_NotUsed_139 139
8483 #define OP_NotUsed_140 140
8484
8485
8486 /* Properties such as "out2" or "jump" that are specified in
8487 ** comments following the "case" for each opcode in the vdbe.c
@@ -8498,23 +8498,23 @@
8498 /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8499 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8500 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8501 /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8502 /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
8503 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
8504 /* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
8505 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
8506 /* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\
8507 /* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8508 /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8509 /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
8510 /* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
8511 /* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
8512 /* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8513 /* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8514 /* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\
8515 /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
8516 /* 144 */ 0x04, 0x04,}
8517
8518 /************** End of opcodes.h *********************************************/
8519 /************** Continuing where we left off in vdbe.h ***********************/
8520
@@ -8529,13 +8529,13 @@
8529 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
8530 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
8531 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
8532 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
8533 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
8534 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
8535 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
8536 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
8537 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
8538 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
8539 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
8540 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
8541 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
@@ -8653,10 +8653,11 @@
8653 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
8654 */
8655 #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
8656 #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
8657 #define PAGER_MEMORY 0x0004 /* In-memory database */
 
8658
8659 /*
8660 ** Valid values for the second argument to sqlite3PagerLockingMode().
8661 */
8662 #define PAGER_LOCKINGMODE_QUERY -1
@@ -8748,10 +8749,13 @@
8748 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
8749 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
8750 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
8751 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
8752 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
 
 
 
8753
8754 /* Functions used to truncate the database file. */
8755 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
8756
8757 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -9924,11 +9928,11 @@
9924 int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
9925 int nCol; /* Number of columns in this table */
9926 Column *aCol; /* Information about each column */
9927 Index *pIndex; /* List of SQL indexes on this table. */
9928 int tnum; /* Root BTree node for this table (see note above) */
9929 tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9930 Select *pSelect; /* NULL for tables. Points to definition if a view. */
9931 u16 nRef; /* Number of pointers to this Table */
9932 u8 tabFlags; /* Mask of TF_* values */
9933 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
9934 FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10123,43 +10127,35 @@
10123 */
10124 struct Index {
10125 char *zName; /* Name of this index */
10126 int nColumn; /* Number of columns in the table used by this index */
10127 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10128 tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10129 Table *pTable; /* The SQL table being indexed */
10130 int tnum; /* Page containing root of this index in database file */
10131 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10132 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
10133 u8 bUnordered; /* Use this index for == or IN queries only */
10134 u8 nSample; /* Number of elements in aSample[] */
10135 char *zColAff; /* String defining the affinity of each column */
10136 Index *pNext; /* The next index associated with the same table */
10137 Schema *pSchema; /* Schema containing this index */
10138 u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
10139 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
10144 };
10145
10146 /*
10147 ** Each sample stored in the sqlite_stat2 table is represented in memory
10148 ** using a structure of this type.
10149 */
10150 struct IndexSample {
10151 union {
10152 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10153 double r; /* Value if eType is SQLITE_FLOAT */
10154 i64 i; /* Value if eType is SQLITE_INTEGER */
10155 } u;
10156 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 */
10161 };
10162
10163 /*
10164 ** Each token coming out of the lexer is an instance of
10165 ** this structure. Tokens are also used as part of an expression.
@@ -11361,11 +11357,10 @@
11361 #else
11362 # define sqlite3ViewGetColumnNames(A,B) 0
11363 #endif
11364
11365 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11366 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
11367 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11368 #ifndef SQLITE_OMIT_AUTOINCREMENT
11369 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
11370 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
11371 #else
@@ -11618,11 +11613,11 @@
11618 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11619 void(*)(void*));
11620 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11621 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11622 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11623 #ifdef SQLITE_ENABLE_STAT3
11624 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11625 #endif
11626 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11627 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11628 #ifndef SQLITE_AMALGAMATION
@@ -12245,13 +12240,10 @@
12245 "ENABLE_RTREE",
12246 #endif
12247 #ifdef SQLITE_ENABLE_STAT2
12248 "ENABLE_STAT2",
12249 #endif
12250 #ifdef SQLITE_ENABLE_STAT3
12251 "ENABLE_STAT3",
12252 #endif
12253 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12254 "ENABLE_UNLOCK_NOTIFY",
12255 #endif
12256 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
12257 "ENABLE_UPDATE_DELETE_LIMIT",
@@ -12387,10 +12379,13 @@
12387 #ifdef SQLITE_OMIT_LOOKASIDE
12388 "OMIT_LOOKASIDE",
12389 #endif
12390 #ifdef SQLITE_OMIT_MEMORYDB
12391 "OMIT_MEMORYDB",
 
 
 
12392 #endif
12393 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
12394 "OMIT_OR_OPTIMIZATION",
12395 #endif
12396 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
@@ -12453,10 +12448,13 @@
12453 #ifdef SQLITE_OMIT_WSD
12454 "OMIT_WSD",
12455 #endif
12456 #ifdef SQLITE_OMIT_XFER_OPT
12457 "OMIT_XFER_OPT",
 
 
 
12458 #endif
12459 #ifdef SQLITE_PERFORMANCE_TRACE
12460 "PERFORMANCE_TRACE",
12461 #endif
12462 #ifdef SQLITE_PROXY_DEBUG
@@ -12574,10 +12572,13 @@
12574 /*
12575 ** Boolean values
12576 */
12577 typedef unsigned char Bool;
12578
 
 
 
12579 /*
12580 ** A cursor is a pointer into a single BTree within a database file.
12581 ** The cursor can seek to a BTree entry with a particular key, or
12582 ** loop over all entries of the Btree. You can also insert new BTree
12583 ** entries or retrieve the key or data from the entry that the cursor
@@ -12605,10 +12606,11 @@
12605 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
12606 const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
12607 i64 seqCount; /* Sequence counter */
12608 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
12609 i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
 
12610
12611 /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
12612 ** OP_IsUnique opcode on this cursor. */
12613 int seekResult;
12614
@@ -12924,17 +12926,36 @@
12924 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
12925 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
12926 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
12927 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
12928 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
 
 
 
12929 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
12930 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
12931 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12932 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12933 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12934 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12935 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12936
12937 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
12938 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
12939 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
12940 #else
@@ -13225,10 +13246,12 @@
13225 ** Astronomical Algorithms, 2nd Edition, 1998
13226 ** ISBM 0-943396-61-1
13227 ** Willmann-Bell, Inc
13228 ** Richmond, Virginia (USA)
13229 */
 
 
13230 #include <time.h>
13231
13232 #ifndef SQLITE_OMIT_DATETIME_FUNCS
13233
13234
@@ -14978,10 +15001,11 @@
14978 extern void backtrace_symbols_fd(void*const*,int,int);
14979 #else
14980 # define backtrace(A,B) 1
14981 # define backtrace_symbols_fd(A,B,C)
14982 #endif
 
14983
14984 /*
14985 ** Each memory allocation looks like this:
14986 **
14987 ** ------------------------------------------------------------------------
@@ -18081,10 +18105,11 @@
18081 **
18082 *************************************************************************
18083 **
18084 ** Memory allocation functions used throughout sqlite.
18085 */
 
18086
18087 /*
18088 ** Attempt to release up to n bytes of non-essential memory currently
18089 ** held by SQLite. An example of non-essential memory is memory used to
18090 ** cache database pages that are not currently in use.
@@ -20058,10 +20083,11 @@
20058 ** BOM or Byte Order Mark:
20059 ** 0xff 0xfe little-endian utf-16 follows
20060 ** 0xfe 0xff big-endian utf-16 follows
20061 **
20062 */
 
20063
20064 #ifndef SQLITE_AMALGAMATION
20065 /*
20066 ** The following constant value is used by the SQLITE_BIGENDIAN and
20067 ** SQLITE_LITTLEENDIAN macros.
@@ -20486,11 +20512,11 @@
20486 ** no longer required.
20487 **
20488 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20489 ** flag set.
20490 */
20491 #ifdef SQLITE_ENABLE_STAT3
20492 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20493 Mem m;
20494 memset(&m, 0, sizeof(m));
20495 m.db = db;
20496 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -20600,10 +20626,11 @@
20600 **
20601 ** This file contains functions for allocating memory, comparing
20602 ** strings, and stuff like that.
20603 **
20604 */
 
20605 #ifdef SQLITE_HAVE_ISNAN
20606 # include <math.h>
20607 #endif
20608
20609 /*
@@ -21778,10 +21805,11 @@
21778 **
21779 *************************************************************************
21780 ** This is the implementation of generic hash-tables
21781 ** used in SQLite.
21782 */
 
21783
21784 /* Turn bulk memory into a hash table object by initializing the
21785 ** fields of the Hash structure.
21786 **
21787 ** "pNew" is a pointer to the hash table that is to be initialized.
@@ -22086,52 +22114,52 @@
22086 /* 35 */ "ReadCookie",
22087 /* 36 */ "SetCookie",
22088 /* 37 */ "VerifyCookie",
22089 /* 38 */ "OpenRead",
22090 /* 39 */ "OpenWrite",
22091 /* 40 */ "OpenAutoindex",
22092 /* 41 */ "OpenEphemeral",
22093 /* 42 */ "OpenPseudo",
22094 /* 43 */ "Close",
22095 /* 44 */ "SeekLt",
22096 /* 45 */ "SeekLe",
22097 /* 46 */ "SeekGe",
22098 /* 47 */ "SeekGt",
22099 /* 48 */ "Seek",
22100 /* 49 */ "NotFound",
22101 /* 50 */ "Found",
22102 /* 51 */ "IsUnique",
22103 /* 52 */ "NotExists",
22104 /* 53 */ "Sequence",
22105 /* 54 */ "NewRowid",
22106 /* 55 */ "Insert",
22107 /* 56 */ "InsertInt",
22108 /* 57 */ "Delete",
22109 /* 58 */ "ResetCount",
22110 /* 59 */ "RowKey",
22111 /* 60 */ "RowData",
22112 /* 61 */ "Rowid",
22113 /* 62 */ "NullRow",
22114 /* 63 */ "Last",
22115 /* 64 */ "Sort",
22116 /* 65 */ "Rewind",
22117 /* 66 */ "Prev",
22118 /* 67 */ "Next",
22119 /* 68 */ "Or",
22120 /* 69 */ "And",
22121 /* 70 */ "IdxInsert",
22122 /* 71 */ "IdxDelete",
22123 /* 72 */ "IdxRowid",
22124 /* 73 */ "IsNull",
22125 /* 74 */ "NotNull",
22126 /* 75 */ "Ne",
22127 /* 76 */ "Eq",
22128 /* 77 */ "Gt",
22129 /* 78 */ "Le",
22130 /* 79 */ "Lt",
22131 /* 80 */ "Ge",
22132 /* 81 */ "IdxLT",
22133 /* 82 */ "BitAnd",
22134 /* 83 */ "BitOr",
22135 /* 84 */ "ShiftLeft",
22136 /* 85 */ "ShiftRight",
22137 /* 86 */ "Add",
@@ -22138,58 +22166,58 @@
22138 /* 87 */ "Subtract",
22139 /* 88 */ "Multiply",
22140 /* 89 */ "Divide",
22141 /* 90 */ "Remainder",
22142 /* 91 */ "Concat",
22143 /* 92 */ "IdxGE",
22144 /* 93 */ "BitNot",
22145 /* 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",
22181 /* 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",
22192 /* 141 */ "ToText",
22193 /* 142 */ "ToBlob",
22194 /* 143 */ "ToNumeric",
22195 /* 144 */ "ToInt",
@@ -24450,10 +24478,11 @@
24450 */
24451 #include <sys/types.h>
24452 #include <sys/stat.h>
24453 #include <fcntl.h>
24454 #include <unistd.h>
 
24455 #include <sys/time.h>
24456 #include <errno.h>
24457 #ifndef SQLITE_OMIT_WAL
24458 #include <sys/mman.h>
24459 #endif
@@ -24485,10 +24514,11 @@
24485 /*
24486 ** If we are to be thread-safe, include the pthreads header and define
24487 ** the SQLITE_UNIX_THREADS macro.
24488 */
24489 #if SQLITE_THREADSAFE
 
24490 # define SQLITE_UNIX_THREADS 1
24491 #endif
24492
24493 /*
24494 ** Default permissions when creating a new file
@@ -24584,11 +24614,15 @@
24584 ** Allowed values for the unixFile.ctrlFlags bitmask:
24585 */
24586 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
24587 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
24588 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
24589 #define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
 
 
 
 
24590
24591 /*
24592 ** Include code that is common to all os_*.c files
24593 */
24594 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -27061,15 +27095,16 @@
27061 */
27062 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
27063 int rc = SQLITE_OK;
27064 int reserved = 0;
27065 unixFile *pFile = (unixFile*)id;
 
27066
27067 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
27068
27069 assert( pFile );
27070 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
27071 if( context->reserved ){
27072 *pResOut = 1;
27073 return SQLITE_OK;
27074 }
27075 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
@@ -27205,11 +27240,11 @@
27205
27206 /* If control gets to this point, then actually go ahead and make
27207 ** operating system calls for the specified lock.
27208 */
27209 if( eFileLock==SHARED_LOCK ){
27210 int lrc1, lrc2, lrc1Errno;
27211 long lk, mask;
27212
27213 assert( pInode->nShared==0 );
27214 assert( pInode->eFileLock==0 );
27215
@@ -27579,21 +27614,23 @@
27579 #if defined(USE_PREAD)
27580 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27581 #elif defined(USE_PREAD64)
27582 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
27583 #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 );
 
 
27595 #endif
27596 TIMER_END;
27597 if( got<0 ){
27598 ((unixFile*)id)->lastErrno = errno;
27599 }
@@ -27888,10 +27925,12 @@
27888 HAVE_FULLFSYNC, isFullsync));
27889 rc = osOpenDirectory(pFile->zPath, &dirfd);
27890 if( rc==SQLITE_OK && dirfd>=0 ){
27891 full_fsync(dirfd, 0, 0);
27892 robust_close(pFile, dirfd, __LINE__);
 
 
27893 }
27894 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
27895 }
27896 return rc;
27897 }
@@ -27971,30 +28010,22 @@
27971 static int proxyFileControl(sqlite3_file*,int,void*);
27972 #endif
27973
27974 /*
27975 ** 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.
27981 */
27982 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27983 { /* preserve indentation of removed "if" */
27984 i64 nSize; /* Required file size */
27985 i64 szChunk; /* Chunk size */
27986 struct stat buf; /* Used to hold return values of fstat() */
27987
27988 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
27989
27990 szChunk = pFile->szChunk;
27991 if( szChunk==0 ){
27992 nSize = nByte;
27993 }else{
27994 nSize = ((nByte+szChunk-1) / szChunk) * szChunk;
27995 }
27996 if( nSize>(i64)buf.st_size ){
27997
27998 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
27999 /* The code below is handling the return value of osFallocate()
28000 ** correctly. posix_fallocate() is defined to "returns zero on success,
@@ -28048,11 +28079,15 @@
28048 case SQLITE_FCNTL_CHUNK_SIZE: {
28049 pFile->szChunk = *(int *)pArg;
28050 return SQLITE_OK;
28051 }
28052 case SQLITE_FCNTL_SIZE_HINT: {
28053 return fcntlSizeHint(pFile, *(i64 *)pArg);
 
 
 
 
28054 }
28055 case SQLITE_FCNTL_PERSIST_WAL: {
28056 int bPersist = *(int*)pArg;
28057 if( bPersist<0 ){
28058 *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
@@ -29485,10 +29520,13 @@
29485 int isReadonly = (flags & SQLITE_OPEN_READONLY);
29486 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
29487 #if SQLITE_ENABLE_LOCKING_STYLE
29488 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
29489 #endif
 
 
 
29490
29491 /* If creating a master or main-file journal, this function will open
29492 ** a file-descriptor on the directory too. The first time unixSync()
29493 ** is called the directory file descriptor will be fsync()ed and close()d.
29494 */
@@ -29617,11 +29655,10 @@
29617
29618 noLock = eType!=SQLITE_OPEN_MAIN_DB;
29619
29620
29621 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29622 struct statfs fsInfo;
29623 if( fstatfs(fd, &fsInfo) == -1 ){
29624 ((unixFile*)pFile)->lastErrno = errno;
29625 robust_close(p, fd, __LINE__);
29626 return SQLITE_IOERR_ACCESS;
29627 }
@@ -29641,11 +29678,10 @@
29641 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
29642 ** never use proxy, NULL means use proxy for non-local files only. */
29643 if( envforce!=NULL ){
29644 useProxy = atoi(envforce)>0;
29645 }else{
29646 struct statfs fsInfo;
29647 if( statfs(zPath, &fsInfo) == -1 ){
29648 /* In theory, the close(fd) call is sub-optimal. If the file opened
29649 ** with fd is a database file, and there are other connections open
29650 ** on that file that are currently holding advisory locks on it,
29651 ** then the call to close() will cancel those locks. In practice,
@@ -29715,10 +29751,12 @@
29715 #endif
29716 {
29717 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29718 }
29719 robust_close(0, fd, __LINE__);
 
 
29720 }
29721 }
29722 #endif
29723 return rc;
29724 }
@@ -30380,10 +30418,12 @@
30380 *pError = err;
30381 }
30382 return SQLITE_IOERR;
30383 }
30384 }
 
 
30385 #endif
30386 #ifdef SQLITE_TEST
30387 /* simulate multiple hosts by creating unique hostid file paths */
30388 if( sqlite3_hostid_num != 0){
30389 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
@@ -30472,10 +30512,11 @@
30472 unixFile *conchFile = pCtx->conchFile;
30473 int rc = SQLITE_OK;
30474 int nTries = 0;
30475 struct timespec conchModTime;
30476
 
30477 do {
30478 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30479 nTries ++;
30480 if( rc==SQLITE_BUSY ){
30481 /* If the lock failed (busy):
@@ -30703,15 +30744,16 @@
30703 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30704
30705 end_takeconch:
30706 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
30707 if( rc==SQLITE_OK && pFile->openFlags ){
 
30708 if( pFile->h>=0 ){
30709 robust_close(pFile, pFile->h, __LINE__);
30710 }
30711 pFile->h = -1;
30712 int fd = robust_open(pCtx->dbPath, pFile->openFlags,
30713 SQLITE_DEFAULT_FILE_PERMISSIONS);
30714 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
30715 if( fd>=0 ){
30716 pFile->h = fd;
30717 }else{
@@ -31629,10 +31671,80 @@
31629 winceLock local; /* Locks obtained by this instance of winFile */
31630 winceLock *shared; /* Global shared lock memory for the file */
31631 #endif
31632 };
31633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31634
31635 /*
31636 ** Forward prototypes.
31637 */
31638 static int getSectorSize(
@@ -31681,10 +31793,192 @@
31681 }
31682 return sqlite3_os_type==2;
31683 }
31684 #endif /* SQLITE_OS_WINCE */
31685
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31686 /*
31687 ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
31688 **
31689 ** Space to hold the returned string is obtained from malloc.
31690 */
@@ -31969,10 +32263,11 @@
31969 */
31970 /*
31971 ** WindowsCE does not have a localtime() function. So create a
31972 ** substitute.
31973 */
 
31974 struct tm *__cdecl localtime(const time_t *t)
31975 {
31976 static struct tm y;
31977 FILETIME uTm, lTm;
31978 SYSTEMTIME pTm;
@@ -32860,15 +33155,22 @@
32860 case SQLITE_FCNTL_CHUNK_SIZE: {
32861 pFile->szChunk = *(int *)pArg;
32862 return SQLITE_OK;
32863 }
32864 case SQLITE_FCNTL_SIZE_HINT: {
32865 sqlite3_int64 sz = *(sqlite3_int64*)pArg;
32866 SimulateIOErrorBenign(1);
32867 winTruncate(id, sz);
32868 SimulateIOErrorBenign(0);
32869 return SQLITE_OK;
 
 
 
 
 
 
 
32870 }
32871 case SQLITE_FCNTL_PERSIST_WAL: {
32872 int bPersist = *(int*)pArg;
32873 if( bPersist<0 ){
32874 *(int*)pArg = pFile->bPersistWal;
@@ -35473,10 +35775,13 @@
35473 typedef struct PCache1 PCache1;
35474 typedef struct PgHdr1 PgHdr1;
35475 typedef struct PgFreeslot PgFreeslot;
35476 typedef struct PGroup PGroup;
35477
 
 
 
35478 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
35479 ** of one or more PCaches that are able to recycle each others unpinned
35480 ** pages when they are under memory pressure. A PGroup is an instance of
35481 ** the following object.
35482 **
@@ -35502,12 +35807,70 @@
35502 int nMaxPage; /* Sum of nMax for purgeable caches */
35503 int nMinPage; /* Sum of nMin for purgeable caches */
35504 int mxPinned; /* nMaxpage + 10 - nMinPage */
35505 int nCurrentPage; /* Number of purgeable pages allocated */
35506 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
 
 
 
 
35507 };
35508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35509 /* Each page cache is an instance of the following object. Every
35510 ** open database file (including each in-memory database and each
35511 ** temporary or transient database) has a single page cache which
35512 ** is an instance of this object.
35513 **
@@ -35606,10 +35969,21 @@
35606 **
35607 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35608 */
35609 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35610 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
 
 
 
 
 
 
 
 
 
 
 
35611
35612 /*
35613 ** Macros to enter and leave the PCache LRU mutex.
35614 */
35615 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -35732,25 +36106,159 @@
35732 return iSize;
35733 }
35734 }
35735 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
35736
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35737 /*
35738 ** Allocate a new page object initially associated with cache pCache.
35739 */
35740 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
35741 int nByte = sizeof(PgHdr1) + pCache->szPage;
35742 void *pPg = pcache1Alloc(nByte);
35743 PgHdr1 *p;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35744 if( pPg ){
35745 p = PAGE_TO_PGHDR1(pCache, pPg);
35746 if( pCache->bPurgeable ){
35747 pCache->pGroup->nCurrentPage++;
35748 }
35749 }else{
35750 p = 0;
35751 }
 
35752 return p;
35753 }
35754
35755 /*
35756 ** Free a page object allocated by pcache1AllocPage().
@@ -35760,14 +36268,56 @@
35760 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
35761 */
35762 static void pcache1FreePage(PgHdr1 *p){
35763 if( ALWAYS(p) ){
35764 PCache1 *pCache = p->pCache;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35765 if( pCache->bPurgeable ){
35766 pCache->pGroup->nCurrentPage--;
35767 }
35768 pcache1Free(PGHDR1_TO_PAGE(p));
35769 }
35770 }
35771
35772 /*
35773 ** Malloc function used by SQLite to obtain space from the buffer configured
@@ -36201,13 +36751,11 @@
36201 /* Step 5. If a usable page buffer has still not been found,
36202 ** attempt to allocate a new one.
36203 */
36204 if( !pPage ){
36205 if( createFlag==1 ) sqlite3BeginBenignMalloc();
36206 pcache1LeaveMutex(pGroup);
36207 pPage = pcache1AllocPage(pCache);
36208 pcache1EnterMutex(pGroup);
36209 if( createFlag==1 ) sqlite3EndBenignMalloc();
36210 }
36211
36212 if( pPage ){
36213 unsigned int h = iKey % pCache->nHash;
@@ -36373,10 +36921,13 @@
36373 ** been released, the function returns. The return value is the total number
36374 ** of bytes of memory released.
36375 */
36376 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
36377 int nFree = 0;
 
 
 
36378 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36379 assert( sqlite3_mutex_notheld(pcache1.mutex) );
36380 if( pcache1.pStart==0 ){
36381 PgHdr1 *p;
36382 pcache1EnterMutex(&pcache1.grp);
@@ -37585,10 +38136,12 @@
37585 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
37586 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
37587 u8 tempFile; /* zFilename is a temporary file */
37588 u8 readOnly; /* True for a read-only database */
37589 u8 memDb; /* True to inhibit all file I/O */
 
 
37590
37591 /**************************************************************************
37592 ** The following block contains those class members that change during
37593 ** routine opertion. Class members not in this block are either fixed
37594 ** when the pager is first created or else only change when there is a
@@ -37807,10 +38360,19 @@
37807 || p->journalMode==PAGER_JOURNALMODE_MEMORY
37808 );
37809 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
37810 assert( pagerUseWal(p)==0 );
37811 }
 
 
 
 
 
 
 
 
 
37812
37813 /* If changeCountDone is set, a RESERVED lock or greater must be held
37814 ** on the file.
37815 */
37816 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
@@ -40704,10 +41266,11 @@
40704 ** to the caller.
40705 */
40706 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
40707 u8 *pTmp = (u8 *)pPager->pTmpSpace;
40708
 
40709 disable_simulated_io_errors();
40710 sqlite3BeginBenignMalloc();
40711 /* pPager->errCode = 0; */
40712 pPager->exclusiveMode = 0;
40713 #ifndef SQLITE_OMIT_WAL
@@ -41138,10 +41701,11 @@
41138 ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
41139 ** while in the error state, hence it is impossible for this routine to
41140 ** be called in the error state. Nevertheless, we include a NEVER()
41141 ** test for the error state as a safeguard against future changes.
41142 */
 
41143 if( NEVER(pPager->errCode) ) return SQLITE_OK;
41144 if( pPager->doNotSpill ) return SQLITE_OK;
41145 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
41146 return SQLITE_OK;
41147 }
@@ -41509,10 +42073,16 @@
41509 }
41510 /* pPager->xBusyHandler = 0; */
41511 /* pPager->pBusyHandlerArg = 0; */
41512 pPager->xReiniter = xReinit;
41513 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
 
 
 
 
 
 
41514
41515 *ppPager = pPager;
41516 return SQLITE_OK;
41517 }
41518
@@ -43052,10 +43622,21 @@
43052 ** Return true if this is an in-memory pager.
43053 */
43054 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
43055 return MEMDB;
43056 }
 
 
 
 
 
 
 
 
 
 
 
43057
43058 /*
43059 ** Check that there are at least nSavepoint savepoints open. If there are
43060 ** currently less than nSavepoints open, then open one or more savepoints
43061 ** to make up the difference. If the number of savepoints is already
@@ -49421,15 +50002,26 @@
49421 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
49422
49423 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
49424 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
49425
 
 
 
 
 
 
 
 
 
 
49426 if( db->flags & SQLITE_NoReadlock ){
49427 flags |= BTREE_NO_READLOCK;
49428 }
49429 if( isMemdb ){
49430 flags |= BTREE_MEMORY;
 
49431 }
49432 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
49433 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
49434 }
49435 p = sqlite3MallocZero(sizeof(Btree));
@@ -51155,11 +51747,12 @@
51155
51156 if( NEVER(wrFlag && pBt->readOnly) ){
51157 return SQLITE_READONLY;
51158 }
51159 if( iTable==1 && btreePagecount(pBt)==0 ){
51160 return SQLITE_EMPTY;
 
51161 }
51162
51163 /* Now that no other errors can occur, finish filling in the BtCursor
51164 ** variables and link the cursor into the BtShared list. */
51165 pCur->pgnoRoot = (Pgno)iTable;
@@ -51909,10 +52502,13 @@
51909 int i;
51910 for(i=1; i<=pCur->iPage; i++){
51911 releasePage(pCur->apPage[i]);
51912 }
51913 pCur->iPage = 0;
 
 
 
51914 }else{
51915 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
51916 if( rc!=SQLITE_OK ){
51917 pCur->eState = CURSOR_INVALID;
51918 return rc;
@@ -52018,11 +52614,11 @@
52018 assert( cursorHoldsMutex(pCur) );
52019 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52020 rc = moveToRoot(pCur);
52021 if( rc==SQLITE_OK ){
52022 if( pCur->eState==CURSOR_INVALID ){
52023 assert( pCur->apPage[pCur->iPage]->nCell==0 );
52024 *pRes = 1;
52025 }else{
52026 assert( pCur->apPage[pCur->iPage]->nCell>0 );
52027 *pRes = 0;
52028 rc = moveToLeftmost(pCur);
@@ -52057,11 +52653,11 @@
52057 }
52058
52059 rc = moveToRoot(pCur);
52060 if( rc==SQLITE_OK ){
52061 if( CURSOR_INVALID==pCur->eState ){
52062 assert( pCur->apPage[pCur->iPage]->nCell==0 );
52063 *pRes = 1;
52064 }else{
52065 assert( pCur->eState==CURSOR_VALID );
52066 *pRes = 0;
52067 rc = moveToRightmost(pCur);
@@ -52130,16 +52726,16 @@
52130
52131 rc = moveToRoot(pCur);
52132 if( rc ){
52133 return rc;
52134 }
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 );
52138 if( pCur->eState==CURSOR_INVALID ){
52139 *pRes = -1;
52140 assert( pCur->apPage[pCur->iPage]->nCell==0 );
52141 return SQLITE_OK;
52142 }
52143 assert( pCur->apPage[0]->intKey || pIdxKey );
52144 for(;;){
52145 int lwr, upr, idx;
@@ -54964,13 +55560,20 @@
54964 releasePage(pPage);
54965 }
54966 return rc;
54967 }
54968 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
 
54969 int rc;
54970 sqlite3BtreeEnter(p);
54971 rc = btreeDropTable(p, iTable, piMoved);
 
 
 
 
 
 
54972 sqlite3BtreeLeave(p);
54973 return rc;
54974 }
54975
54976
@@ -55045,10 +55648,15 @@
55045 ** corruption) an SQLite error code is returned.
55046 */
55047 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
55048 i64 nEntry = 0; /* Value to return in *pnEntry */
55049 int rc; /* Return code */
 
 
 
 
 
55050 rc = moveToRoot(pCur);
55051
55052 /* Unless an error occurs, the following loop runs one iteration for each
55053 ** page in the B-Tree structure (not including overflow pages).
55054 */
@@ -55829,11 +56437,10 @@
55829 */
55830 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
55831 BtShared *pBt = pBtree->pBt;
55832 int rc; /* Return code */
55833
55834 assert( pBtree->inTrans==TRANS_NONE );
55835 assert( iVersion==1 || iVersion==2 );
55836
55837 /* If setting the version fields to 1, do not automatically open the
55838 ** WAL connection, even if the version fields are currently set to 2.
55839 */
@@ -56268,106 +56875,110 @@
56268 /* Update the schema version field in the destination database. This
56269 ** is to make sure that the schema-version really does change in
56270 ** the case where the source and destination databases have the
56271 ** same schema version.
56272 */
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;
 
 
 
 
56369 }
56370 }
56371
56372 /* If bCloseTrans is true, then this function opened a read transaction
56373 ** on the source database. Close the read transaction here. There is
@@ -56831,38 +57442,32 @@
56831 ** invoking an external callback, free it now. Calling this function
56832 ** does not free any Mem.zMalloc buffer.
56833 */
56834 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
56835 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 }
56854 }
56855 }
56856
56857 /*
56858 ** Release any memory held by the Mem. This may leave the Mem in an
56859 ** inconsistent state, for example with (Mem.z==0) and
56860 ** (Mem.type==SQLITE_TEXT).
56861 */
56862 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
56863 sqlite3VdbeMemReleaseExternal(p);
56864 sqlite3DbFree(p->db, p->zMalloc);
56865 p->z = 0;
56866 p->zMalloc = 0;
56867 p->xDel = 0;
56868 }
@@ -57180,11 +57785,11 @@
57180 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
57181 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
57182 */
57183 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
57184 assert( (pFrom->flags & MEM_RowSet)==0 );
57185 sqlite3VdbeMemReleaseExternal(pTo);
57186 memcpy(pTo, pFrom, MEMCELLSIZE);
57187 pTo->xDel = 0;
57188 if( (pFrom->flags&MEM_Static)==0 ){
57189 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
57190 assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57198,11 +57803,11 @@
57198 */
57199 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
57200 int rc = SQLITE_OK;
57201
57202 assert( (pFrom->flags & MEM_RowSet)==0 );
57203 sqlite3VdbeMemReleaseExternal(pTo);
57204 memcpy(pTo, pFrom, MEMCELLSIZE);
57205 pTo->flags &= ~MEM_Dyn;
57206
57207 if( pTo->flags&(MEM_Str|MEM_Blob) ){
57208 if( 0==(pFrom->flags&MEM_Static) ){
@@ -57592,15 +58197,15 @@
57592 *ppVal = 0;
57593 return SQLITE_OK;
57594 }
57595 op = pExpr->op;
57596
57597 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
57598 ** The ifdef here is to enable us to achieve 100% branch test coverage even
57599 ** when SQLITE_ENABLE_STAT3 is omitted.
57600 */
57601 #ifdef SQLITE_ENABLE_STAT3
57602 if( op==TK_REGISTER ) op = pExpr->op2;
57603 #else
57604 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
57605 #endif
57606
@@ -58153,10 +58758,16 @@
58153 assert( p->nOp - i >= 3 );
58154 assert( pOp[-1].opcode==OP_Integer );
58155 n = pOp[-1].p1;
58156 if( n>nMaxArgs ) nMaxArgs = n;
58157 #endif
 
 
 
 
 
 
58158 }
58159
58160 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
58161 assert( -1-pOp->p2<p->nLabel );
58162 pOp->p2 = aLabel[-1-pOp->p2];
@@ -58244,37 +58855,34 @@
58244 ** Change the value of the P1 operand for a specific instruction.
58245 ** This routine is useful when a large program is loaded from a
58246 ** static array using sqlite3VdbeAddOpList but we want to make a
58247 ** few minor changes to the program.
58248 */
58249 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
58250 assert( p!=0 );
58251 assert( addr>=0 );
58252 if( p->nOp>addr ){
58253 p->aOp[addr].p1 = val;
58254 }
58255 }
58256
58257 /*
58258 ** Change the value of the P2 operand for a specific instruction.
58259 ** This routine is useful for setting a jump destination.
58260 */
58261 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
58262 assert( p!=0 );
58263 assert( addr>=0 );
58264 if( p->nOp>addr ){
58265 p->aOp[addr].p2 = val;
58266 }
58267 }
58268
58269 /*
58270 ** Change the value of the P3 operand for a specific instruction.
58271 */
58272 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
58273 assert( p!=0 );
58274 assert( addr>=0 );
58275 if( p->nOp>addr ){
58276 p->aOp[addr].p3 = val;
58277 }
58278 }
58279
58280 /*
@@ -58292,12 +58900,12 @@
58292 /*
58293 ** Change the P2 operand of instruction addr so that it points to
58294 ** the address of the next instruction to be coded.
58295 */
58296 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58297 assert( addr>=0 || p->db->mallocFailed );
58298 if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
58299 }
58300
58301
58302 /*
58303 ** If the input FuncDef structure is ephemeral, then free it. If
@@ -58661,10 +59269,14 @@
58661 break;
58662 }
58663 case P4_SUBPROGRAM: {
58664 sqlite3_snprintf(nTemp, zTemp, "program");
58665 break;
 
 
 
 
58666 }
58667 default: {
58668 zP4 = pOp->p4.z;
58669 if( zP4==0 ){
58670 zP4 = zTemp;
@@ -59285,10 +59897,11 @@
59285 */
59286 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
59287 if( pCx==0 ){
59288 return;
59289 }
 
59290 if( pCx->pBt ){
59291 sqlite3BtreeClose(pCx->pBt);
59292 /* The pCx->pCursor will be close automatically, if it exists, by
59293 ** the call above. */
59294 }else if( pCx->pCursor ){
@@ -62585,10 +63198,17 @@
62585 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
62586 ** P if required.
62587 */
62588 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
62589
 
 
 
 
 
 
 
62590 /*
62591 ** Argument pMem points at a register that will be passed to a
62592 ** user-defined function or returned to the user as the result of a query.
62593 ** This routine sets the pMem->type variable used by the sqlite3_value_*()
62594 ** routines.
@@ -63179,10 +63799,11 @@
63179 u8 *zEndHdr; /* Pointer to first byte after the header */
63180 u32 offset; /* Offset into the data */
63181 u32 szField; /* Number of bytes in the content of a field */
63182 int szHdr; /* Size of the header size field at start of record */
63183 int avail; /* Number of bytes of available data */
 
63184 Mem *pReg; /* PseudoTable input register */
63185 } am;
63186 struct OP_Affinity_stack_vars {
63187 const char *zAffinity; /* The affinity to be applied */
63188 char cAff; /* A single character of affinity */
@@ -63337,11 +63958,10 @@
63337 BtCursor *pCrsr;
63338 int res;
63339 } bl;
63340 struct OP_Next_stack_vars {
63341 VdbeCursor *pC;
63342 BtCursor *pCrsr;
63343 int res;
63344 } bm;
63345 struct OP_IdxInsert_stack_vars {
63346 VdbeCursor *pC;
63347 BtCursor *pCrsr;
@@ -63591,11 +64211,11 @@
63591 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
63592 assert( pOp->p2>0 );
63593 assert( pOp->p2<=p->nMem );
63594 pOut = &aMem[pOp->p2];
63595 memAboutToChange(p, pOut);
63596 sqlite3VdbeMemReleaseExternal(pOut);
63597 pOut->flags = MEM_Int;
63598 }
63599
63600 /* Sanity checking on other operands */
63601 #ifdef SQLITE_DEBUG
@@ -65061,10 +65681,11 @@
65061 u8 *zEndHdr; /* Pointer to first byte after the header */
65062 u32 offset; /* Offset into the data */
65063 u32 szField; /* Number of bytes in the content of a field */
65064 int szHdr; /* Size of the header size field at start of record */
65065 int avail; /* Number of bytes of available data */
 
65066 Mem *pReg; /* PseudoTable input register */
65067 #endif /* local variables moved into u.am */
65068
65069
65070 u.am.p1 = pOp->p1;
@@ -65073,11 +65694,10 @@
65073 memset(&u.am.sMem, 0, sizeof(u.am.sMem));
65074 assert( u.am.p1<p->nCursor );
65075 assert( pOp->p3>0 && pOp->p3<=p->nMem );
65076 u.am.pDest = &aMem[pOp->p3];
65077 memAboutToChange(p, u.am.pDest);
65078 MemSetTypeFlag(u.am.pDest, MEM_Null);
65079 u.am.zRec = 0;
65080
65081 /* This block sets the variable u.am.payloadSize to be the total number of
65082 ** bytes in the record.
65083 **
@@ -65117,11 +65737,11 @@
65117 }else{
65118 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65119 rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65120 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65121 }
65122 }else if( u.am.pC->pseudoTableReg>0 ){
65123 u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65124 assert( u.am.pReg->flags & MEM_Blob );
65125 assert( memIsValid(u.am.pReg) );
65126 u.am.payloadSize = u.am.pReg->n;
65127 u.am.zRec = u.am.pReg->z;
@@ -65130,13 +65750,14 @@
65130 }else{
65131 /* Consider the row to be NULL */
65132 u.am.payloadSize = 0;
65133 }
65134
65135 /* If u.am.payloadSize is 0, then just store a NULL */
 
65136 if( u.am.payloadSize==0 ){
65137 assert( u.am.pDest->flags&MEM_Null );
65138 goto op_column_out;
65139 }
65140 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65141 if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65142 goto too_big;
@@ -65239,12 +65860,18 @@
65239 ** of the record to the start of the data for the u.am.i-th column
65240 */
65241 for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
65242 if( u.am.zIdx<u.am.zEndHdr ){
65243 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]);
 
 
 
 
 
 
65246 u.am.offset += u.am.szField;
65247 if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */
65248 u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
65249 break;
65250 }
@@ -65281,11 +65908,11 @@
65281 ** a pointer to a Mem object.
65282 */
65283 if( u.am.aOffset[u.am.p2] ){
65284 assert( rc==SQLITE_OK );
65285 if( u.am.zRec ){
65286 sqlite3VdbeMemReleaseExternal(u.am.pDest);
65287 sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
65288 }else{
65289 u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
65290 sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
65291 rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
@@ -65298,11 +65925,11 @@
65298 u.am.pDest->enc = encoding;
65299 }else{
65300 if( pOp->p4type==P4_MEM ){
65301 sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
65302 }else{
65303 assert( u.am.pDest->flags&MEM_Null );
65304 }
65305 }
65306
65307 /* If we dynamically allocated space to hold the data (in the
65308 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
@@ -65500,11 +66127,11 @@
65500 i64 nEntry;
65501 BtCursor *pCrsr;
65502 #endif /* local variables moved into u.ap */
65503
65504 u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
65505 if( u.ap.pCrsr ){
65506 rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
65507 }else{
65508 u.ap.nEntry = 0;
65509 }
65510 pOut->u.i = u.ap.nEntry;
@@ -66076,19 +66703,13 @@
66076 u.aw.pCur->nullRow = 1;
66077 u.aw.pCur->isOrdered = 1;
66078 rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
66079 u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
66080
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 }
66090
66091 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
66092 ** SQLite used to check if the root-page flags were sane at this point
66093 ** and report database corruption if they were not, but this check has
66094 ** since moved into the btree layer. */
@@ -66095,11 +66716,11 @@
66095 u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66096 u.aw.pCur->isIndex = !u.aw.pCur->isTable;
66097 break;
66098 }
66099
66100 /* Opcode: OpenEphemeral P1 P2 * P4 *
66101 **
66102 ** Open a new cursor P1 to a transient table.
66103 ** The cursor is always opened read/write even if
66104 ** the main database is read-only. The ephemeral
66105 ** table is deleted automatically when the cursor is closed.
@@ -66112,18 +66733,30 @@
66112 ** This opcode was once called OpenTemp. But that created
66113 ** confusion because the term "temp table", might refer either
66114 ** to a TEMP table at the SQL level, or to a table opened by
66115 ** this opcode. Then this opcode was call OpenVirtual. But
66116 ** that created confusion with the whole virtual-table idea.
 
 
 
 
 
66117 */
66118 /* Opcode: OpenAutoindex P1 P2 * P4 *
66119 **
66120 ** This opcode works the same as OP_OpenEphemeral. It has a
66121 ** different name to distinguish its use. Tables created using
66122 ** by this opcode will be used for automatically created transient
66123 ** indices in joins.
66124 */
 
 
 
 
 
 
 
66125 case OP_OpenAutoindex:
66126 case OP_OpenEphemeral: {
66127 #if 0 /* local variables moved into u.ax */
66128 VdbeCursor *pCx;
66129 #endif /* local variables moved into u.ax */
@@ -66133,10 +66766,11 @@
66133 SQLITE_OPEN_EXCLUSIVE |
66134 SQLITE_OPEN_DELETEONCLOSE |
66135 SQLITE_OPEN_TRANSIENT_DB;
66136
66137 assert( pOp->p1>=0 );
 
66138 u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66139 if( u.ax.pCx==0 ) goto no_mem;
66140 u.ax.pCx->nullRow = 1;
66141 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt,
66142 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
@@ -66166,10 +66800,15 @@
66166 u.ax.pCx->isTable = 1;
66167 }
66168 }
66169 u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66170 u.ax.pCx->isIndex = !u.ax.pCx->isTable;
 
 
 
 
 
66171 break;
66172 }
66173
66174 /* Opcode: OpenPseudo P1 P2 P3 * *
66175 **
@@ -66285,11 +66924,11 @@
66285 assert( u.az.pC->pseudoTableReg==0 );
66286 assert( OP_SeekLe == OP_SeekLt+1 );
66287 assert( OP_SeekGe == OP_SeekLt+2 );
66288 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 ){
66294 /* The input value in P3 might be of any type: integer, real, string,
66295 ** blob, or NULL. But it needs to be an integer before we can do
@@ -66651,11 +67290,11 @@
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;
@@ -67075,10 +67714,17 @@
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) );
67083
67084 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
@@ -67183,10 +67829,11 @@
67183 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);
67190 }
67191 break;
67192 }
@@ -67208,11 +67855,11 @@
67208
67209 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;
67215 }else{
67216 rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
67217 }
67218 u.bk.pC->nullRow = (u8)u.bk.res;
@@ -67263,11 +67910,15 @@
67263
67264 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;
@@ -67278,18 +67929,21 @@
67278 pc = pOp->p2 - 1;
67279 }
67280 break;
67281 }
67282
67283 /* Opcode: Next P1 P2 * * P5
67284 **
67285 ** Advance cursor P1 so that it points to the next key/data pair in its
67286 ** table or index. If there are no more key/value pairs then fall through
67287 ** to the following instruction. But if the cursor advance was successful,
67288 ** jump immediately to P2.
67289 **
67290 ** The P1 cursor must be for a real table, not a pseudo-table.
 
 
 
67291 **
67292 ** If P5 is positive and the jump is taken, then event counter
67293 ** number P5-1 in the prepared statement is incremented.
67294 **
67295 ** See also: Prev
@@ -67300,19 +67954,21 @@
67300 ** table or index. If there is no previous key/value pairs then fall through
67301 ** to the following instruction. But if the cursor backup was successful,
67302 ** jump immediately to P2.
67303 **
67304 ** The P1 cursor must be for a real table, not a pseudo-table.
 
 
 
67305 **
67306 ** If P5 is positive and the jump is taken, then event counter
67307 ** number P5-1 in the prepared statement is incremented.
67308 */
67309 case OP_Prev: /* jump */
67310 case OP_Next: { /* jump */
67311 #if 0 /* local variables moved into u.bm */
67312 VdbeCursor *pC;
67313 BtCursor *pCrsr;
67314 int res;
67315 #endif /* local variables moved into u.bm */
67316
67317 CHECK_FOR_INTERRUPT;
67318 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
@@ -67319,19 +67975,21 @@
67319 assert( pOp->p5<=ArraySize(p->aCounter) );
67320 u.bm.pC = p->apCsr[pOp->p1];
67321 if( u.bm.pC==0 ){
67322 break; /* See ticket #2273 */
67323 }
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 ){
67336 pc = pOp->p2 - 1;
67337 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
@@ -67373,14 +68031,17 @@
67373 assert( u.bn.pC->isTable==0 );
67374 rc = ExpandBlob(pIn2);
67375 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;
67383 }
67384 }
67385 break;
67386 }
@@ -69556,10 +70217,722 @@
69556 }
69557
69558 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
69559
69560 /************** End of vdbeblob.c ********************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69561 /************** Begin file journal.c *****************************************/
69562 /*
69563 ** 2007 August 22
69564 **
69565 ** The author disclaims copyright to this source code. In place of
@@ -70072,10 +71445,12 @@
70072 **
70073 *************************************************************************
70074 ** This file contains routines used for walking the parser tree for
70075 ** an SQL statement.
70076 */
 
 
70077
70078
70079 /*
70080 ** Walk an expression tree. Invoke the callback once for each node
70081 ** of the expression, while decending. (In other words, the callback
@@ -70210,10 +71585,12 @@
70210 **
70211 ** This file contains routines used for walking the parser tree and
70212 ** resolve all identifiers by associating them with a particular
70213 ** table and column.
70214 */
 
 
70215
70216 /*
70217 ** Turn the pExpr expression into an alias for the iCol-th column of the
70218 ** result set in pEList.
70219 **
@@ -76012,100 +77389,10 @@
76012 ** May you find forgiveness for yourself and forgive others.
76013 ** May you share freely, never taking more than you give.
76014 **
76015 *************************************************************************
76016 ** 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 */
76108 #ifndef SQLITE_OMIT_ANALYZE
76109
76110 /*
76111 ** This routine generates code that opens the sqlite_stat1 table for
@@ -76133,18 +77420,12 @@
76133 static const struct {
76134 const char *zName;
76135 const char *zCols;
76136 } aTable[] = {
76137 { "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",
76146 #endif
76147 };
76148
76149 int aRoot[] = {0, 0};
76150 u8 aCreateTbl[] = {0, 0};
@@ -76156,21 +77437,10 @@
76156 if( v==0 ) return;
76157 assert( sqlite3BtreeHoldsAllMutexes(db) );
76158 assert( sqlite3VdbeDb(v)==db );
76159 pDb = &db->aDb[iDb];
76160
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 for(i=0; i<ArraySize(aTable); i++){
76173 const char *zTab = aTable[i].zName;
76174 Table *pStat;
76175 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
76176 /* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -76197,238 +77467,17 @@
76197 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
76198 }
76199 }
76200 }
76201
76202 /* Open the sqlite_stat[13] tables for writing. */
76203 for(i=0; i<ArraySize(aTable); i++){
76204 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
76205 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
76206 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
76207 }
76208 }
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
76431 /*
76432 ** Generate code to do an analysis of all indices associated with
76433 ** a single table.
76434 */
@@ -76448,31 +77497,24 @@
76448 int endOfLoop; /* The end of the loop */
76449 int jZeroRows = -1; /* Jump from here if number of rows is zero */
76450 int iDb; /* Index of database containing pTab */
76451 int regTabname = iMem++; /* Register containing table name */
76452 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 */
76470 int regRec = iMem++; /* Register holding completed record */
76471 int regTemp = iMem++; /* Temporary use register */
76472 int regNewRowid = iMem++; /* Rowid for the inserted record */
76473
 
 
 
 
 
 
 
 
76474
76475 v = sqlite3GetVdbe(pParse);
76476 if( v==0 || NEVER(pTab==0) ){
76477 return;
76478 }
@@ -76501,22 +77543,17 @@
76501 iIdxCur = pParse->nTab++;
76502 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
76503 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
76504 int nCol;
76505 KeyInfo *pKey;
76506 int addrIfNot = 0; /* address of OP_IfNot */
76507 int *aChngAddr; /* Array of jump instruction addresses */
76508
76509 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
76510 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
76511 nCol = pIdx->nColumn;
76512 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
76513 if( iMem+1+(nCol*2)>pParse->nMem ){
76514 pParse->nMem = iMem+1+(nCol*2);
76515 }
76516 aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*pIdx->nColumn);
76517 if( aChngAddr==0 ) continue;
76518
76519 /* Open a cursor to the index to be analyzed. */
76520 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
76521 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
76522 (char *)pKey, P4_KEYINFO_HANDOFF);
@@ -76523,24 +77560,35 @@
76523 VdbeComment((v, "%s", pIdx->zName));
76524
76525 /* Populate the register containing the index name. */
76526 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
76527
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 */
 
 
 
 
 
 
 
 
 
 
 
76542
76543 /* The block of memory cells initialized here is used as follows.
76544 **
76545 ** iMem:
76546 ** The total number of rows in the table.
@@ -76566,87 +77614,79 @@
76566 /* Start the analysis loop. This loop runs through all the entries in
76567 ** the index b-tree. */
76568 endOfLoop = sqlite3VdbeMakeLabel(v);
76569 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
76570 topOfLoop = sqlite3VdbeCurrentAddr(v);
76571 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
76572
76573 for(i=0; i<nCol; i++){
76574 CollSeq *pColl;
76575 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
76576 if( i==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76577 /* Always record the very first row */
76578 addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
76579 }
76580 assert( pIdx->azColl!=0 );
76581 assert( pIdx->azColl[i]!=0 );
76582 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
76583 aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
76584 (char*)pColl, P4_COLLSEQ);
76585 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
76593 }
76594 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
76595 for(i=0; i<nCol; i++){
76596 sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
76597 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 }
76609 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
76610 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
76611 }
76612 sqlite3DbFree(db, aChngAddr);
76613
76614 /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
76615 sqlite3VdbeResolveLabel(v, endOfLoop);
76616
76617 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
76618 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
76649 /* Store the results in sqlite_stat1.
76650 **
76651 ** The result is a single row of the sqlite_stat1 table. The first
76652 ** two columns are the names of the table and index. The third column
@@ -76662,51 +77702,50 @@
76662 **
76663 ** If K==0 then no entry is made into the sqlite_stat1 table.
76664 ** If K>0 then it is always the case the D>0 so division by zero
76665 ** is never possible.
76666 */
76667 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
76668 if( jZeroRows<0 ){
76669 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
76670 }
76671 for(i=0; i<nCol; i++){
76672 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
76673 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
76674 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
76675 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
76676 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
76677 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
76678 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
76679 }
76680 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76681 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76682 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
76683 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
76684 }
76685
76686 /* If the table has no indices, create a single sqlite_stat1 entry
76687 ** containing NULL as the index name and the row count as the content.
76688 */
76689 if( pTab->pIndex==0 ){
76690 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
76691 VdbeComment((v, "%s", pTab->zName));
76692 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
76693 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76694 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
76695 }else{
76696 sqlite3VdbeJumpHere(v, jZeroRows);
76697 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
76698 }
76699 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
76700 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76701 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76702 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
76703 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
76704 if( pParse->nMem<regRec ) pParse->nMem = regRec;
76705 sqlite3VdbeJumpHere(v, jZeroRows);
76706 }
76707
76708
76709 /*
76710 ** Generate code that will cause the most recent index analysis to
76711 ** be loaded into internal hash tables where is can be used.
76712 */
@@ -76727,11 +77766,11 @@
76727 int iStatCur;
76728 int iMem;
76729
76730 sqlite3BeginWriteOperation(pParse, 0, iDb);
76731 iStatCur = pParse->nTab;
76732 pParse->nTab += 3;
76733 openStatTable(pParse, iDb, iStatCur, 0, 0);
76734 iMem = pParse->nMem+1;
76735 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
76736 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
76737 Table *pTab = (Table*)sqliteHashData(k);
@@ -76752,11 +77791,11 @@
76752 assert( pTab!=0 );
76753 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
76754 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
76755 sqlite3BeginWriteOperation(pParse, 0, iDb);
76756 iStatCur = pParse->nTab;
76757 pParse->nTab += 3;
76758 if( pOnlyIdx ){
76759 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
76760 }else{
76761 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
76762 }
@@ -76857,11 +77896,11 @@
76857 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
76858 analysisInfo *pInfo = (analysisInfo*)pData;
76859 Index *pIndex;
76860 Table *pTable;
76861 int i, c, n;
76862 tRowcnt v;
76863 const char *z;
76864
76865 assert( argc==3 );
76866 UNUSED_PARAMETER2(NotUsed, argc);
76867
@@ -76900,172 +77939,40 @@
76900 /*
76901 ** If the Index.aSample variable is not NULL, delete the aSample[] array
76902 ** and its contents.
76903 */
76904 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
76905 #ifdef SQLITE_ENABLE_STAT3
76906 if( pIdx->aSample ){
76907 int j;
76908 for(j=0; j<pIdx->nSample; j++){
76909 IndexSample *p = &pIdx->aSample[j];
76910 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
76911 sqlite3_free(p->u.z);
76912 }
76913 }
76914 sqlite3_free(pIdx->aSample);
76915 }
76916 UNUSED_PARAMETER(db);
76917 pIdx->nSample = 0;
76918 pIdx->aSample = 0;
76919 #else
76920 UNUSED_PARAMETER(db);
76921 UNUSED_PARAMETER(pIdx);
76922 #endif
76923 }
76924
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
77056 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
77057 ** arrays. The contents of sqlite_stat3 are used to populate the
77058 ** Index.aSample[] arrays.
77059 **
77060 ** 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
77063 ** read from it.
77064 **
77065 ** If SQLITE_ENABLE_STAT3 was defined during compilation and the
77066 ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
77067 ** returned. However, in this case, data is read from the sqlite_stat1
77068 ** table (if it is present) before returning.
77069 **
77070 ** If an OOM error occurs, this function always sets db->mallocFailed.
77071 ** This means if the caller does not care about other errors, the return
@@ -77083,14 +77990,12 @@
77083 /* Clear any prior statistics */
77084 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77085 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
77086 Index *pIdx = sqliteHashData(i);
77087 sqlite3DefaultRowEst(pIdx);
77088 #ifdef SQLITE_ENABLE_STAT3
77089 sqlite3DeleteIndexSamples(db, pIdx);
77090 pIdx->aSample = 0;
77091 #endif
77092 }
77093
77094 /* Check to make sure the sqlite_stat1 table exists */
77095 sInfo.db = db;
77096 sInfo.zDatabase = db->aDb[iDb].zName;
@@ -77098,23 +78003,91 @@
77098 return SQLITE_ERROR;
77099 }
77100
77101 /* Load new statistics out of the sqlite_stat1 table */
77102 zSql = sqlite3MPrintf(db,
77103 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
77104 if( zSql==0 ){
77105 rc = SQLITE_NOMEM;
77106 }else{
77107 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
77108 sqlite3DbFree(db, zSql);
77109 }
77110
77111
77112 /* Load the statistics from the sqlite_stat3 table. */
77113 #ifdef SQLITE_ENABLE_STAT3
 
 
 
77114 if( rc==SQLITE_OK ){
77115 rc = loadStat3(db, sInfo.zDatabase);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77116 }
77117 #endif
77118
77119 if( rc==SQLITE_NOMEM ){
77120 db->mallocFailed = 1;
@@ -79610,11 +80583,11 @@
79610 Table *p;
79611 int n;
79612 const char *z;
79613 Token sEnd;
79614 DbFixer sFix;
79615 Token *pName;
79616 int iDb;
79617 sqlite3 *db = pParse->db;
79618
79619 if( pParse->nVar>0 ){
79620 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
@@ -79926,15 +80899,11 @@
79926 Parse *pParse, /* The parsing context */
79927 int iDb, /* The database number */
79928 const char *zType, /* "idx" or "tbl" */
79929 const char *zName /* Name of index or table */
79930 ){
79931 static const char *azStatTab[] = {
79932 "sqlite_stat1",
79933 "sqlite_stat2",
79934 "sqlite_stat3",
79935 };
79936 int i;
79937 const char *zDbName = pParse->db->aDb[iDb].zName;
79938 for(i=0; i<ArraySize(azStatTab); i++){
79939 if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
79940 sqlite3NestedParse(pParse,
@@ -79941,81 +80910,10 @@
79941 "DELETE FROM %Q.%s WHERE %s=%Q",
79942 zDbName, azStatTab[i], zType, zName
79943 );
79944 }
79945 }
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 }
80018
80019 /*
80020 ** This routine is called to do the work of a DROP TABLE statement.
80021 ** pName is the name of the table to be dropped.
@@ -80082,11 +80980,11 @@
80082 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
80083 goto exit_drop_table;
80084 }
80085 }
80086 #endif
80087 if( !pParse->nested && sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
80088 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
80089 goto exit_drop_table;
80090 }
80091
80092 #ifndef SQLITE_OMIT_VIEW
@@ -80106,15 +81004,72 @@
80106 /* Generate code to remove the table from the master table
80107 ** on disk.
80108 */
80109 v = sqlite3GetVdbe(pParse);
80110 if( v ){
 
 
80111 sqlite3BeginWriteOperation(pParse, 1, iDb);
 
 
 
 
 
 
80112 sqlite3FkDropTable(pParse, pName, pTab);
80113 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80114 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
 
 
 
 
 
 
 
 
 
 
 
 
80115 }
 
80116
80117 exit_drop_table:
80118 sqlite3SrcListDelete(db, pName);
80119 }
80120
@@ -80278,18 +81233,28 @@
80278 */
80279 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
80280 Table *pTab = pIndex->pTable; /* The table that is indexed */
80281 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
80282 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
 
80283 int addr1; /* Address of top of loop */
80284 int tnum; /* Root page of index */
80285 Vdbe *v; /* Generate code into this virtual machine */
80286 KeyInfo *pKey; /* KeyInfo for index */
80287 int regIdxKey; /* Registers containing the index key */
80288 int regRecord; /* Register holding assemblied index record */
80289 sqlite3 *db = pParse->db; /* The database connection */
80290 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 
 
 
 
 
 
 
 
 
80291
80292 #ifndef SQLITE_OMIT_AUTHORIZATION
80293 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
80294 db->aDb[iDb].zName ) ){
80295 return;
@@ -80311,14 +81276,33 @@
80311 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
80312 (char *)pKey, P4_KEYINFO_HANDOFF);
80313 if( memRootPage>=0 ){
80314 sqlite3VdbeChangeP5(v, 1);
80315 }
 
 
 
 
 
 
 
 
 
 
80316 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
80317 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
80318 regRecord = sqlite3GetTempReg(pParse);
80319 regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 
 
 
 
 
 
 
 
 
80320 if( pIndex->onError!=OE_None ){
80321 const int regRowid = regIdxKey + pIndex->nColumn;
80322 const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
80323 void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
80324
@@ -80333,17 +81317,19 @@
80333 */
80334 sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
80335 sqlite3HaltConstraint(
80336 pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
80337 }
80338 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
80339 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
80340 sqlite3ReleaseTempReg(pParse, regRecord);
80341 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
80342 sqlite3VdbeJumpHere(v, addr1);
 
80343 sqlite3VdbeAddOp1(v, OP_Close, iTab);
80344 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
 
80345 }
80346
80347 /*
80348 ** Create a new index for an SQL table. pName1.pName2 is the name of the index
80349 ** and pTblList is the name of the table that is to be indexed. Both will
@@ -80557,24 +81543,24 @@
80557 */
80558 nName = sqlite3Strlen30(zName);
80559 nCol = pList->nExpr;
80560 pIndex = sqlite3DbMallocZero(db,
80561 sizeof(Index) + /* Index structure */
80562 sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
80563 sizeof(int)*nCol + /* Index.aiColumn */
 
80564 sizeof(char *)*nCol + /* Index.azColl */
80565 sizeof(u8)*nCol + /* Index.aSortOrder */
80566 nName + 1 + /* Index.zName */
80567 nExtra /* Collation sequence names */
80568 );
80569 if( db->mallocFailed ){
80570 goto exit_create_index;
80571 }
80572 pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
80573 pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
80574 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
80575 pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
 
80576 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
80577 zExtra = (char *)(&pIndex->zName[nName+1]);
80578 memcpy(pIndex->zName, zName, nName+1);
80579 pIndex->pTable = pTab;
80580 pIndex->nColumn = pList->nExpr;
@@ -80847,13 +81833,13 @@
80847 ** Apart from that, we have little to go on besides intuition as to
80848 ** how aiRowEst[] should be initialized. The numbers generated here
80849 ** are based on typical values found in actual indices.
80850 */
80851 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
80852 tRowcnt *a = pIdx->aiRowEst;
80853 int i;
80854 tRowcnt n;
80855 assert( a!=0 );
80856 a[0] = pIdx->pTable->nRowEst;
80857 if( a[0]<10 ) a[0] = 10;
80858 n = 10;
80859 for(i=1; i<=pIdx->nColumn; i++){
@@ -81293,12 +82279,13 @@
81293 ** The operator is "natural cross join". The A and B operands are stored
81294 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
81295 ** operator with A. This routine shifts that operator over to B.
81296 */
81297 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
81298 if( p && p->a ){
81299 int i;
 
81300 for(i=p->nSrc-1; i>0; i--){
81301 p->a[i].jointype = p->a[i-1].jointype;
81302 }
81303 p->a[0].jointype = 0;
81304 }
@@ -82850,10 +83837,12 @@
82850 **
82851 ** There is only one exported symbol in this file - the function
82852 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
82853 ** All other code has file scope.
82854 */
 
 
82855
82856 /*
82857 ** Return the collating function associated with a function.
82858 */
82859 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -85173,11 +86162,28 @@
85173 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
85174 }else{
85175 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
85176 }
85177 if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
 
85178 if( !isIgnoreErrors || db->mallocFailed ) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85179 continue;
85180 }
85181 assert( pFKey->nCol==1 || (aiFree && pIdx) );
85182
85183 if( aiFree ){
@@ -88081,10 +89087,11 @@
88081
88082 #endif /* _SQLITE3EXT_H_ */
88083
88084 /************** End of sqlite3ext.h ******************************************/
88085 /************** Continuing where we left off in loadext.c ********************/
 
88086
88087 #ifndef SQLITE_OMIT_LOAD_EXTENSION
88088
88089 /*
88090 ** Some API routines are omitted when various features are
@@ -95650,10 +96657,12 @@
95650 ** interface routine of sqlite3_exec().
95651 **
95652 ** These routines are in a separate files so that they will not be linked
95653 ** if they are not used.
95654 */
 
 
95655
95656 #ifndef SQLITE_OMIT_GET_TABLE
95657
95658 /*
95659 ** This structure is used to pass data from sqlite3_get_table() through
@@ -99159,11 +100168,11 @@
99159 #define TERM_CODED 0x04 /* This term is already coded */
99160 #define TERM_COPIED 0x08 /* Has a child */
99161 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
99162 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
99163 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
99164 #ifdef SQLITE_ENABLE_STAT3
99165 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
99166 #else
99167 # define TERM_VNULL 0x00 /* Disabled if not using stat2 */
99168 #endif
99169
@@ -100373,11 +101382,11 @@
100373 pNewTerm->prereqAll = pTerm->prereqAll;
100374 }
100375 }
100376 #endif /* SQLITE_OMIT_VIRTUALTABLE */
100377
100378 #ifdef SQLITE_ENABLE_STAT3
100379 /* When sqlite_stat2 histogram data is available an operator of the
100380 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
100381 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
100382 ** virtual term of that form.
100383 **
@@ -100412,11 +101421,11 @@
100412 pTerm->nChild = 1;
100413 pTerm->wtFlags |= TERM_COPIED;
100414 pNewTerm->prereqAll = pTerm->prereqAll;
100415 }
100416 }
100417 #endif /* SQLITE_ENABLE_STAT */
100418
100419 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
100420 ** an index for tables to the left of the join.
100421 */
100422 pTerm->prereqRight |= extraRight;
@@ -101461,89 +102470,71 @@
101461 */
101462 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
101463 }
101464 #endif /* SQLITE_OMIT_VIRTUALTABLE */
101465
101466 #ifdef SQLITE_ENABLE_STAT3
101467 /*
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(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101477 Parse *pParse, /* Database connection */
101478 Index *pIdx, /* Index to consider domain of */
101479 sqlite3_value *pVal, /* Value to consider */
101480 int roundUp, /* Round up if true. Round down if false */
101481 tRowcnt *aStat /* OUT: stats written here */
101482 ){
101483 tRowcnt n;
101484 IndexSample *aSample;
101485 int i, eType;
101486 int isEq = 0;
101487 i64 v;
101488 double r, rS;
101489
101490 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 ){
101542 sqlite3 *db = pParse->db;
101543 CollSeq *pColl;
101544 const u8 *z;
 
 
 
 
 
101545 if( eType==SQLITE_BLOB ){
101546 z = (const u8 *)sqlite3_value_blob(pVal);
101547 pColl = db->pDfltColl;
101548 assert( pColl->enc==SQLITE_UTF8 );
101549 }else{
@@ -101558,16 +102549,16 @@
101558 return SQLITE_NOMEM;
101559 }
101560 assert( z && pColl && pColl->xCmp );
101561 }
101562 n = sqlite3ValueBytes(pVal, pColl->enc);
101563
101564 for(; i<pIdx->nSample; i++){
101565 int c;
101566 int eSampletype = aSample[i].eType;
101567 if( eSampletype<eType ) continue;
101568 if( eSampletype!=eType ) break;
101569 #ifndef SQLITE_OMIT_UTF16
101570 if( pColl->enc!=SQLITE_UTF8 ){
101571 int nSample;
101572 char *zSample = sqlite3Utf8to16(
101573 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -101581,52 +102572,20 @@
101581 }else
101582 #endif
101583 {
101584 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
101585 }
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;
101624 }
101625 return SQLITE_OK;
101626 }
101627 #endif /* SQLITE_ENABLE_STAT3 */
101628
101629 /*
101630 ** If expression pExpr represents a literal value, set *pp to point to
101631 ** an sqlite3_value structure containing the same value, with affinity
101632 ** aff applied to it, before returning. It is the responsibility of the
@@ -101640,11 +102599,11 @@
101640 **
101641 ** If neither of the above apply, set *pp to NULL.
101642 **
101643 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
101644 */
101645 #ifdef SQLITE_ENABLE_STAT3
101646 static int valueFromExpr(
101647 Parse *pParse,
101648 Expr *pExpr,
101649 u8 aff,
101650 sqlite3_value **pp
@@ -101688,92 +102647,106 @@
101688 **
101689 ** ... FROM t1 WHERE a > ? AND a < ? ...
101690 **
101691 ** then nEq should be passed 0.
101692 **
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...
 
 
101697 **
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.
101702 */
101703 static int whereRangeScanEst(
101704 Parse *pParse, /* Parsing & code generating context */
101705 Index *p, /* The index containing the range-compared column; "x" */
101706 int nEq, /* index into p->aCol[] of the range-compared column */
101707 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
101708 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
101709 double *pRangeDiv /* OUT: Reduce search space by this divisor */
101710 ){
101711 int rc = SQLITE_OK;
101712
101713 #ifdef SQLITE_ENABLE_STAT3
101714
101715 if( nEq==0 && p->nSample ){
101716 sqlite3_value *pRangeVal;
101717 tRowcnt iLower = 0;
101718 tRowcnt iUpper = p->aiRowEst[0];
101719 tRowcnt a[2];
 
 
 
101720 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101721
101722 if( pLower ){
101723 Expr *pExpr = pLower->pExpr->pRight;
101724 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
101725 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);
101733 }
101734 if( rc==SQLITE_OK && pUpper ){
101735 Expr *pExpr = pUpper->pExpr->pRight;
101736 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
101737 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 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101757 #else
101758 UNUSED_PARAMETER(pParse);
101759 UNUSED_PARAMETER(p);
101760 UNUSED_PARAMETER(nEq);
101761 #endif
101762 assert( pLower || pUpper );
101763 *pRangeDiv = (double)1;
101764 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
101765 if( pUpper ) *pRangeDiv *= (double)4;
101766 return rc;
101767 }
101768
101769 #ifdef SQLITE_ENABLE_STAT3
101770 /*
101771 ** Estimate the number of rows that will be returned based on
101772 ** an equality constraint x=VALUE and where that VALUE occurs in
101773 ** the histogram data. This only works when x is the left-most
101774 ** column of an index and sqlite_stat3 histogram data is available
101775 ** for that index. When pExpr==NULL that means the constraint is
101776 ** "x IS NULL" instead of "x=VALUE".
101777 **
101778 ** Write the estimated row count into *pnRow and return SQLITE_OK.
101779 ** If unable to make an estimate, leave *pnRow unchanged and return
@@ -101789,13 +102762,14 @@
101789 Index *p, /* The index whose left-most column is pTerm */
101790 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
101791 double *pnRow /* Write the revised row estimate here */
101792 ){
101793 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
 
101794 u8 aff; /* Column affinity */
101795 int rc; /* Subfunction return code */
101796 tRowcnt a[2]; /* Statistics */
101797
101798 assert( p->aSample!=0 );
101799 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101800 if( pExpr ){
101801 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
@@ -101802,22 +102776,30 @@
101802 if( rc ) goto whereEqualScanEst_cancel;
101803 }else{
101804 pRhs = sqlite3ValueNew(pParse->db);
101805 }
101806 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];
 
 
 
 
 
 
 
101811 }
 
101812 whereEqualScanEst_cancel:
101813 sqlite3ValueFree(pRhs);
101814 return rc;
101815 }
101816 #endif /* defined(SQLITE_ENABLE_STAT3) */
101817
101818 #ifdef SQLITE_ENABLE_STAT3
101819 /*
101820 ** Estimate the number of rows that will be returned based on
101821 ** an IN constraint where the right-hand side of the IN operator
101822 ** is a list of values. Example:
101823 **
@@ -101836,29 +102818,64 @@
101836 Parse *pParse, /* Parsing & code generating context */
101837 Index *p, /* The index whose left-most column is pTerm */
101838 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
101839 double *pnRow /* Write the revised row estimate here */
101840 ){
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 */
 
 
 
 
 
 
 
101845
101846 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;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101851 }
101852 if( rc==SQLITE_OK ){
 
 
 
 
 
 
 
 
 
101853 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
101854 *pnRow = nRowEst;
101855 WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
 
101856 }
 
101857 return rc;
101858 }
101859 #endif /* defined(SQLITE_ENABLE_STAT3) */
101860
101861
101862 /*
101863 ** Find the best query plan for accessing a particular table. Write the
101864 ** best query plan and its cost into the WhereCost object supplied as the
@@ -101901,11 +102918,11 @@
101901 Index *pProbe; /* An index we are evaluating */
101902 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
101903 int eqTermMask; /* Current mask of valid equality operators */
101904 int idxEqTermMask; /* Index mask of valid equality operators */
101905 Index sPk; /* A fake index object for the primary key */
101906 tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
101907 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
101908 int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
101909
101910 /* Initialize the cost to a worst-case value */
101911 memset(pCost, 0, sizeof(*pCost));
@@ -101956,11 +102973,11 @@
101956 }
101957
101958 /* Loop over all indices looking for the best one to use
101959 */
101960 for(; pProbe; pIdx=pProbe=pProbe->pNext){
101961 const tRowcnt * const aiRowEst = pProbe->aiRowEst;
101962 double cost; /* Cost of using pProbe */
101963 double nRow; /* Estimated number of rows in result set */
101964 double log10N; /* base-10 logarithm of nRow (inexact) */
101965 int rev; /* True to scan in reverse order */
101966 int wsFlags = 0;
@@ -101999,16 +103016,18 @@
101999 ** Set to true if there was at least one "x IN (SELECT ...)" term used
102000 ** in determining the value of nInMul. Note that the RHS of the
102001 ** IN operator must be a SELECT, not a value list, for this variable
102002 ** to be true.
102003 **
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).
 
 
102010 **
102011 ** bSort:
102012 ** Boolean. True if there is an ORDER BY clause that will require an
102013 ** external sort (i.e. scanning the index being evaluated will not
102014 ** correctly order records).
@@ -102029,17 +103048,17 @@
102029 ** SELECT a, b, c FROM tbl WHERE a = 1;
102030 */
102031 int nEq; /* Number of == or IN terms matching index */
102032 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
102033 int nInMul = 1; /* Number of distinct equalities to lookup */
102034 double rangeDiv = (double)1; /* Estimated reduction in search space */
102035 int nBound = 0; /* Number of range constraints seen */
102036 int bSort = !!pOrderBy; /* True if external sort required */
102037 int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
102038 int bLookup = 0; /* True if not a covering index */
102039 WhereTerm *pTerm; /* A single term of the WHERE clause */
102040 #ifdef SQLITE_ENABLE_STAT3
102041 WhereTerm *pFirstTerm = 0; /* First term matching the index */
102042 #endif
102043
102044 /* Determine the values of nEq and nInMul */
102045 for(nEq=0; nEq<pProbe->nColumn; nEq++){
@@ -102059,23 +103078,23 @@
102059 nInMul *= pExpr->x.pList->nExpr;
102060 }
102061 }else if( pTerm->eOperator & WO_ISNULL ){
102062 wsFlags |= WHERE_COLUMN_NULL;
102063 }
102064 #ifdef SQLITE_ENABLE_STAT3
102065 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
102066 #endif
102067 used |= pTerm->prereqRight;
102068 }
102069
102070 /* Determine the value of rangeDiv */
102071 if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
102072 int j = pProbe->aiColumn[nEq];
102073 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
102074 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
102075 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
102076 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
102077 if( pTop ){
102078 nBound = 1;
102079 wsFlags |= WHERE_TOP_LIMIT;
102080 used |= pTop->prereqRight;
102081 }
@@ -102143,11 +103162,11 @@
102143 if( bInEst && nRow*2>aiRowEst[0] ){
102144 nRow = aiRowEst[0]/2;
102145 nInMul = (int)(nRow / aiRowEst[nEq]);
102146 }
102147
102148 #ifdef SQLITE_ENABLE_STAT3
102149 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
102150 ** and we do not think that values of x are unique and if histogram
102151 ** data is available for column x, then it might be possible
102152 ** to get a better estimate on the number of rows based on
102153 ** VALUE and how common that value is according to the histogram.
@@ -102159,16 +103178,16 @@
102159 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
102160 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
102161 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
102162 }
102163 }
102164 #endif /* SQLITE_ENABLE_STAT3 */
102165
102166 /* Adjust the number of output rows and downward to reflect rows
102167 ** that are excluded by range constraints.
102168 */
102169 nRow = nRow/rangeDiv;
102170 if( nRow<1 ) nRow = 1;
102171
102172 /* Experiments run on real SQLite databases show that the time needed
102173 ** to do a binary search to locate a row in a table or index is roughly
102174 ** log10(N) times the time to move from one row to the next row within
@@ -102293,14 +103312,14 @@
102293 if( nRow<2 ) nRow = 2;
102294 }
102295
102296
102297 WHERETRACE((
102298 "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
102299 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
102300 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
102301 nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
102302 notReady, log10N, nRow, cost, used
102303 ));
102304
102305 /* If this index is the best we have seen so far, then record this
102306 ** index and its cost in the pCost structure.
@@ -104227,10 +105246,11 @@
104227 ** LALR(1) grammar but which are always false in the
104228 ** specific grammar used by SQLite.
104229 */
104230 /* First off, code is included that follows the "include" declaration
104231 ** in the input grammar file. */
 
104232
104233
104234 /*
104235 ** Disable all error recovery processing in the parser push-down
104236 ** automaton.
@@ -105087,10 +106107,11 @@
105087 #endif
105088 };
105089 typedef struct yyParser yyParser;
105090
105091 #ifndef NDEBUG
 
105092 static FILE *yyTraceFILE = 0;
105093 static char *yyTracePrompt = 0;
105094 #endif /* NDEBUG */
105095
105096 #ifndef NDEBUG
@@ -107662,10 +108683,11 @@
107662 **
107663 ** This file contains C code that splits an SQL input string up into
107664 ** individual tokens and sends those tokens one-by-one over to the
107665 ** parser for analysis.
107666 */
 
107667
107668 /*
107669 ** The charMap() macro maps alphabetic characters into their
107670 ** lower-case ASCII equivalent. On ASCII machines, this is just
107671 ** an upper-to-lower case map. On EBCDIC machines we also need
@@ -109053,10 +110075,20 @@
109053 memcpy(&y, &x, 8);
109054 assert( sqlite3IsNaN(y) );
109055 }
109056 #endif
109057 #endif
 
 
 
 
 
 
 
 
 
 
109058
109059 return rc;
109060 }
109061
109062 /*
@@ -113184,10 +114216,16 @@
113184
113185 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
113186 # define SQLITE_CORE 1
113187 #endif
113188
 
 
 
 
 
 
113189
113190 #ifndef SQLITE_CORE
113191 SQLITE_EXTENSION_INIT1
113192 #endif
113193
@@ -117705,10 +118743,12 @@
117705 ******************************************************************************
117706 **
117707 */
117708 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
117709
 
 
117710
117711 typedef struct Fts3auxTable Fts3auxTable;
117712 typedef struct Fts3auxCursor Fts3auxCursor;
117713
117714 struct Fts3auxTable {
@@ -118243,10 +119283,12 @@
118243 /*
118244 ** Default span for NEAR operators.
118245 */
118246 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
118247
 
 
118248
118249 /*
118250 ** isNot:
118251 ** This variable is used by function getNextNode(). When getNextNode() is
118252 ** called, it sets ParseContext.isNot to true if the 'next node' is a
@@ -118944,10 +119986,11 @@
118944 ** Everything after this point is just test code.
118945 */
118946
118947 #ifdef SQLITE_TEST
118948
 
118949
118950 /*
118951 ** Function to query the hash-table of tokenizers (see README.tokenizers).
118952 */
118953 static int queryTestTokenizer(
@@ -119154,10 +120197,13 @@
119154 ** * The FTS3 module is being built into the core of
119155 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119156 */
119157 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119158
 
 
 
119159
119160
119161 /*
119162 ** Malloc and Free functions
119163 */
@@ -119534,10 +120580,14 @@
119534 ** * The FTS3 module is being built into the core of
119535 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119536 */
119537 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119538
 
 
 
 
119539
119540
119541 /*
119542 ** Class derived from sqlite3_tokenizer
119543 */
@@ -120177,10 +121227,12 @@
120177 ** * The FTS3 module is being built into the core of
120178 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120179 */
120180 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120181
 
 
120182
120183 /*
120184 ** Implementation of the SQL scalar function for accessing the underlying
120185 ** hash table. This function may be called as follows:
120186 **
@@ -120352,10 +121404,12 @@
120352 }
120353
120354
120355 #ifdef SQLITE_TEST
120356
 
 
120357
120358 /*
120359 ** Implementation of a special SQL scalar function for testing tokenizers
120360 ** designed to be used in concert with the Tcl testing framework. This
120361 ** function must be called with two arguments:
@@ -120663,10 +121717,14 @@
120663 ** * The FTS3 module is being built into the core of
120664 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120665 */
120666 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120667
 
 
 
 
120668
120669
120670 typedef struct simple_tokenizer {
120671 sqlite3_tokenizer base;
120672 char delim[128]; /* flag ASCII delimiters */
@@ -120888,10 +121946,13 @@
120888 ** code in fts3.c.
120889 */
120890
120891 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120892
 
 
 
120893
120894 /*
120895 ** When full-text index nodes are loaded from disk, the buffer that they
120896 ** are loaded into has the following number of bytes of padding at the end
120897 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
@@ -124149,10 +125210,12 @@
124149 ******************************************************************************
124150 */
124151
124152 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
124153
 
 
124154
124155 /*
124156 ** Characters that may appear in the second argument to matchinfo().
124157 */
124158 #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
@@ -125736,10 +126799,12 @@
125736 #ifndef SQLITE_CORE
125737 SQLITE_EXTENSION_INIT1
125738 #else
125739 #endif
125740
 
 
125741
125742 #ifndef SQLITE_AMALGAMATION
125743 #include "sqlite3rtree.h"
125744 typedef sqlite3_int64 i64;
125745 typedef unsigned char u8;
@@ -128950,10 +130015,11 @@
128950 #include <unicode/utypes.h>
128951 #include <unicode/uregex.h>
128952 #include <unicode/ustring.h>
128953 #include <unicode/ucol.h>
128954
 
128955
128956 #ifndef SQLITE_CORE
128957 SQLITE_EXTENSION_INIT1
128958 #else
128959 #endif
@@ -129429,12 +130495,16 @@
129429 ** This file implements a tokenizer for fts3 based on the ICU library.
129430 */
129431 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
129432 #ifdef SQLITE_ENABLE_ICU
129433
 
 
129434
129435 #include <unicode/ubrk.h>
 
 
129436 #include <unicode/utf16.h>
129437
129438 typedef struct IcuTokenizer IcuTokenizer;
129439 typedef struct IcuCursor IcuCursor;
129440
129441
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -385,23 +385,29 @@
385 /*
386 ** Exactly one of the following macros must be defined in order to
387 ** specify which memory allocation subsystem to use.
388 **
389 ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
390 ** SQLITE_WIN32_MALLOC // Use Win32 native heap API
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.
397 **
398 ** (Historical note: There used to be several other options, but we've
399 ** pared it down to just these two.)
400 **
401 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
402 ** the default.
403 */
404 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
405 # error "At most one of the following compile-time configuration options\
406 is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
407 #endif
408 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
409 # define SQLITE_SYSTEM_MALLOC 1
410 #endif
411
412 /*
413 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -650,11 +656,11 @@
656 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
657 ** [sqlite_version()] and [sqlite_source_id()].
658 */
659 #define SQLITE_VERSION "3.7.8"
660 #define SQLITE_VERSION_NUMBER 3007008
661 #define SQLITE_SOURCE_ID "2011-08-29 11:56:14 639cc85a911454bffdcccb33f2976c683953ae64"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -1751,20 +1757,14 @@
1757 ** also used during testing of SQLite in order to specify an alternative
1758 ** memory allocator that simulates memory out-of-memory conditions in
1759 ** order to verify that SQLite recovers gracefully from such
1760 ** conditions.
1761 **
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
 
 
 
1765 ** xRealloc is always a value returned by a prior call to xRoundup.
 
 
 
1766 **
1767 ** xSize should return the allocated size of a memory allocation
1768 ** previously obtained from xMalloc or xRealloc. The allocated size
1769 ** is always at least as big as the requested size but may be larger.
1770 **
@@ -3397,11 +3397,11 @@
3397 ** a schema change, on the first [sqlite3_step()] call following any change
3398 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3399 ** ^The specific value of WHERE-clause [parameter] might influence the
3400 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3401 ** or [GLOB] operator or if the parameter is compared to an indexed column
3402 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3403 ** the
3404 ** </li>
3405 ** </ol>
3406 */
3407 SQLITE_API int sqlite3_prepare(
@@ -7632,10 +7632,18 @@
7632 */
7633 #ifndef SQLITE_TEMP_STORE
7634 # define SQLITE_TEMP_STORE 1
7635 #endif
7636
7637 /*
7638 ** If all temporary storage is in-memory, then omit the external merge-sort
7639 ** logic since it is superfluous.
7640 */
7641 #if SQLITE_TEMP_STORE==3 && !defined(SQLITE_OMIT_MERGE_SORT)
7642 # define SQLITE_OMIT_MERGE_SORT
7643 #endif
7644
7645 /*
7646 ** GCC does not define the offsetof() macro so we'll have to do it
7647 ** ourselves.
7648 */
7649 #ifndef offsetof
@@ -7711,22 +7719,10 @@
7719 ** is 0x00000000ffffffff. But because of quirks of some compilers, we
7720 ** have to specify the value in the less intuitive manner shown:
7721 */
7722 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
7723
 
 
 
 
 
 
 
 
 
 
 
 
7724 /*
7725 ** Macros to determine whether the machine is big or little endian,
7726 ** evaluated at runtime.
7727 */
7728 #ifdef SQLITE_AMALGAMATION
@@ -7986,10 +7982,11 @@
7982 #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
7983 #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */
7984 #define BTREE_MEMORY 4 /* This is an in-memory DB */
7985 #define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */
7986 #define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */
7987 #define BTREE_SORTER 32 /* Used as accumulator in external merge sort */
7988
7989 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
7990 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
7991 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
7992 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
@@ -8188,10 +8185,11 @@
8185 ** or VDBE. The VDBE implements an abstract machine that runs a
8186 ** simple program to access and modify the underlying database.
8187 */
8188 #ifndef _SQLITE_VDBE_H_
8189 #define _SQLITE_VDBE_H_
8190 /* #include <stdio.h> */
8191
8192 /*
8193 ** A single VDBE is an opaque structure named "Vdbe". Only routines
8194 ** in the source file sqliteVdbe.c are allowed to see the insides
8195 ** of this structure.
@@ -8231,10 +8229,11 @@
8229 Mem *pMem; /* Used when p4type is P4_MEM */
8230 VTable *pVtab; /* Used when p4type is P4_VTAB */
8231 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
8232 int *ai; /* Used when p4type is P4_INTARRAY */
8233 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
8234 int (*xAdvance)(BtCursor *, int *);
8235 } p4;
8236 #ifdef SQLITE_DEBUG
8237 char *zComment; /* Comment to improve readability */
8238 #endif
8239 #ifdef VDBE_PROFILE
@@ -8286,10 +8285,11 @@
8285 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
8286 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
8287 #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
8288 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
8289 #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
8290 #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
8291
8292 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
8293 ** is made. That copy is freed when the Vdbe is finalized. But if the
8294 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
8295 ** gets freed when the Vdbe is finalized so it still should be obtained
@@ -8399,89 +8399,89 @@
8399 #define OP_ReadCookie 35
8400 #define OP_SetCookie 36
8401 #define OP_VerifyCookie 37
8402 #define OP_OpenRead 38
8403 #define OP_OpenWrite 39
8404 #define OP_OpenSorter 40
8405 #define OP_OpenAutoindex 41
8406 #define OP_OpenEphemeral 42
8407 #define OP_OpenPseudo 43
8408 #define OP_Close 44
8409 #define OP_SeekLt 45
8410 #define OP_SeekLe 46
8411 #define OP_SeekGe 47
8412 #define OP_SeekGt 48
8413 #define OP_Seek 49
8414 #define OP_NotFound 50
8415 #define OP_Found 51
8416 #define OP_IsUnique 52
8417 #define OP_NotExists 53
8418 #define OP_Sequence 54
8419 #define OP_NewRowid 55
8420 #define OP_Insert 56
8421 #define OP_InsertInt 57
8422 #define OP_Delete 58
8423 #define OP_ResetCount 59
8424 #define OP_RowKey 60
8425 #define OP_RowData 61
8426 #define OP_Rowid 62
8427 #define OP_NullRow 63
8428 #define OP_Last 64
8429 #define OP_Sort 65
8430 #define OP_Rewind 66
8431 #define OP_Prev 67
8432 #define OP_Next 70
8433 #define OP_IdxInsert 71
8434 #define OP_IdxDelete 72
8435 #define OP_IdxRowid 81
8436 #define OP_IdxLT 92
8437 #define OP_IdxGE 95
8438 #define OP_Destroy 96
8439 #define OP_Clear 97
8440 #define OP_CreateIndex 98
8441 #define OP_CreateTable 99
8442 #define OP_ParseSchema 100
8443 #define OP_LoadAnalysis 101
8444 #define OP_DropTable 102
8445 #define OP_DropIndex 103
8446 #define OP_DropTrigger 104
8447 #define OP_IntegrityCk 105
8448 #define OP_RowSetAdd 106
8449 #define OP_RowSetRead 107
8450 #define OP_RowSetTest 108
8451 #define OP_Program 109
8452 #define OP_Param 110
8453 #define OP_FkCounter 111
8454 #define OP_FkIfZero 112
8455 #define OP_MemMax 113
8456 #define OP_IfPos 114
8457 #define OP_IfNeg 115
8458 #define OP_IfZero 116
8459 #define OP_AggStep 117
8460 #define OP_AggFinal 118
8461 #define OP_Checkpoint 119
8462 #define OP_JournalMode 120
8463 #define OP_Vacuum 121
8464 #define OP_IncrVacuum 122
8465 #define OP_Expire 123
8466 #define OP_TableLock 124
8467 #define OP_VBegin 125
8468 #define OP_VCreate 126
8469 #define OP_VDestroy 127
8470 #define OP_VOpen 128
8471 #define OP_VFilter 129
8472 #define OP_VColumn 131
8473 #define OP_VNext 132
8474 #define OP_VRename 133
8475 #define OP_VUpdate 134
8476 #define OP_Pagecount 135
8477 #define OP_MaxPgcnt 136
8478 #define OP_Trace 137
8479 #define OP_Noop 138
8480 #define OP_Explain 139
8481
8482 /* The following opcode values are never used */
8483 #define OP_NotUsed_140 140
8484
8485
8486 /* Properties such as "out2" or "jump" that are specified in
8487 ** comments following the "case" for each opcode in the vdbe.c
@@ -8498,23 +8498,23 @@
8498 /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8499 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8500 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8501 /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8502 /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
8503 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11,\
8504 /* 48 */ 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02,\
8505 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,\
8506 /* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x01, 0x08,\
8507 /* 72 */ 0x00, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8508 /* 80 */ 0x15, 0x02, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8509 /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
8510 /* 96 */ 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\
8511 /* 104 */ 0x00, 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00,\
8512 /* 112 */ 0x01, 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00,\
8513 /* 120 */ 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
8514 /* 128 */ 0x00, 0x01, 0x02, 0x00, 0x01, 0x00, 0x00, 0x02,\
8515 /* 136 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
8516 /* 144 */ 0x04, 0x04,}
8517
8518 /************** End of opcodes.h *********************************************/
8519 /************** Continuing where we left off in vdbe.h ***********************/
8520
@@ -8529,13 +8529,13 @@
8529 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
8530 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
8531 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
8532 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
8533 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
8534 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
8535 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
8536 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
8537 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
8538 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
8539 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
8540 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
8541 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
@@ -8653,10 +8653,11 @@
8653 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
8654 */
8655 #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
8656 #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
8657 #define PAGER_MEMORY 0x0004 /* In-memory database */
8658 #define PAGER_SORTER 0x0020 /* Accumulator in external merge sort */
8659
8660 /*
8661 ** Valid values for the second argument to sqlite3PagerLockingMode().
8662 */
8663 #define PAGER_LOCKINGMODE_QUERY -1
@@ -8748,10 +8749,13 @@
8749 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
8750 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
8751 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
8752 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
8753 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
8754 #ifndef SQLITE_OMIT_MERGE_SORT
8755 SQLITE_PRIVATE int sqlite3PagerUnderStress(Pager*);
8756 #endif
8757
8758 /* Functions used to truncate the database file. */
8759 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
8760
8761 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -9924,11 +9928,11 @@
9928 int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
9929 int nCol; /* Number of columns in this table */
9930 Column *aCol; /* Information about each column */
9931 Index *pIndex; /* List of SQL indexes on this table. */
9932 int tnum; /* Root BTree node for this table (see note above) */
9933 unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9934 Select *pSelect; /* NULL for tables. Points to definition if a view. */
9935 u16 nRef; /* Number of pointers to this Table */
9936 u8 tabFlags; /* Mask of TF_* values */
9937 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
9938 FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10123,43 +10127,35 @@
10127 */
10128 struct Index {
10129 char *zName; /* Name of this index */
10130 int nColumn; /* Number of columns in the table used by this index */
10131 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10132 unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10133 Table *pTable; /* The SQL table being indexed */
10134 int tnum; /* Page containing root of this index in database file */
10135 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10136 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
10137 u8 bUnordered; /* Use this index for == or IN queries only */
 
10138 char *zColAff; /* String defining the affinity of each column */
10139 Index *pNext; /* The next index associated with the same table */
10140 Schema *pSchema; /* Schema containing this index */
10141 u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
10142 char **azColl; /* Array of collation sequence names for index */
10143 IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
 
 
 
10144 };
10145
10146 /*
10147 ** Each sample stored in the sqlite_stat2 table is represented in memory
10148 ** using a structure of this type.
10149 */
10150 struct IndexSample {
10151 union {
10152 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10153 double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
 
10154 } u;
10155 u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10156 u8 nByte; /* Size in byte of text or blob. */
 
 
 
10157 };
10158
10159 /*
10160 ** Each token coming out of the lexer is an instance of
10161 ** this structure. Tokens are also used as part of an expression.
@@ -11361,11 +11357,10 @@
11357 #else
11358 # define sqlite3ViewGetColumnNames(A,B) 0
11359 #endif
11360
11361 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
 
11362 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11363 #ifndef SQLITE_OMIT_AUTOINCREMENT
11364 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
11365 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
11366 #else
@@ -11618,11 +11613,11 @@
11613 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11614 void(*)(void*));
11615 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11616 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11617 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11618 #ifdef SQLITE_ENABLE_STAT2
11619 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11620 #endif
11621 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11622 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11623 #ifndef SQLITE_AMALGAMATION
@@ -12245,13 +12240,10 @@
12240 "ENABLE_RTREE",
12241 #endif
12242 #ifdef SQLITE_ENABLE_STAT2
12243 "ENABLE_STAT2",
12244 #endif
 
 
 
12245 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12246 "ENABLE_UNLOCK_NOTIFY",
12247 #endif
12248 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
12249 "ENABLE_UPDATE_DELETE_LIMIT",
@@ -12387,10 +12379,13 @@
12379 #ifdef SQLITE_OMIT_LOOKASIDE
12380 "OMIT_LOOKASIDE",
12381 #endif
12382 #ifdef SQLITE_OMIT_MEMORYDB
12383 "OMIT_MEMORYDB",
12384 #endif
12385 #ifdef SQLITE_OMIT_MERGE_SORT
12386 "OMIT_MERGE_SORT",
12387 #endif
12388 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
12389 "OMIT_OR_OPTIMIZATION",
12390 #endif
12391 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
@@ -12453,10 +12448,13 @@
12448 #ifdef SQLITE_OMIT_WSD
12449 "OMIT_WSD",
12450 #endif
12451 #ifdef SQLITE_OMIT_XFER_OPT
12452 "OMIT_XFER_OPT",
12453 #endif
12454 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
12455 "PAGECACHE_BLOCKALLOC",
12456 #endif
12457 #ifdef SQLITE_PERFORMANCE_TRACE
12458 "PERFORMANCE_TRACE",
12459 #endif
12460 #ifdef SQLITE_PROXY_DEBUG
@@ -12574,10 +12572,13 @@
12572 /*
12573 ** Boolean values
12574 */
12575 typedef unsigned char Bool;
12576
12577 /* Opaque type used by code in vdbesort.c */
12578 typedef struct VdbeSorter VdbeSorter;
12579
12580 /*
12581 ** A cursor is a pointer into a single BTree within a database file.
12582 ** The cursor can seek to a BTree entry with a particular key, or
12583 ** loop over all entries of the Btree. You can also insert new BTree
12584 ** entries or retrieve the key or data from the entry that the cursor
@@ -12605,10 +12606,11 @@
12606 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
12607 const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
12608 i64 seqCount; /* Sequence counter */
12609 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
12610 i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
12611 VdbeSorter *pSorter; /* Sorter object for OP_OpenSorter cursors */
12612
12613 /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
12614 ** OP_IsUnique opcode on this cursor. */
12615 int seekResult;
12616
@@ -12924,17 +12926,36 @@
12926 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
12927 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
12928 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
12929 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
12930 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12931 #define MemReleaseExt(X) \
12932 if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
12933 sqlite3VdbeMemReleaseExternal(X);
12934 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
12935 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
12936 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12937 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12938 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12939 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12940 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12941
12942 #ifdef SQLITE_OMIT_MERGE_SORT
12943 # define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK
12944 # define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK
12945 # define sqlite3VdbeSorterClose(Y,Z)
12946 # define sqlite3VdbeSorterRowkey(Y,Z) SQLITE_OK
12947 # define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK
12948 # define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK
12949 #else
12950 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
12951 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, int);
12952 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
12953 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
12954 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
12955 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
12956 #endif
12957
12958 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
12959 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
12960 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
12961 #else
@@ -13225,10 +13246,12 @@
13246 ** Astronomical Algorithms, 2nd Edition, 1998
13247 ** ISBM 0-943396-61-1
13248 ** Willmann-Bell, Inc
13249 ** Richmond, Virginia (USA)
13250 */
13251 /* #include <stdlib.h> */
13252 /* #include <assert.h> */
13253 #include <time.h>
13254
13255 #ifndef SQLITE_OMIT_DATETIME_FUNCS
13256
13257
@@ -14978,10 +15001,11 @@
15001 extern void backtrace_symbols_fd(void*const*,int,int);
15002 #else
15003 # define backtrace(A,B) 1
15004 # define backtrace_symbols_fd(A,B,C)
15005 #endif
15006 /* #include <stdio.h> */
15007
15008 /*
15009 ** Each memory allocation looks like this:
15010 **
15011 ** ------------------------------------------------------------------------
@@ -18081,10 +18105,11 @@
18105 **
18106 *************************************************************************
18107 **
18108 ** Memory allocation functions used throughout sqlite.
18109 */
18110 /* #include <stdarg.h> */
18111
18112 /*
18113 ** Attempt to release up to n bytes of non-essential memory currently
18114 ** held by SQLite. An example of non-essential memory is memory used to
18115 ** cache database pages that are not currently in use.
@@ -20058,10 +20083,11 @@
20083 ** BOM or Byte Order Mark:
20084 ** 0xff 0xfe little-endian utf-16 follows
20085 ** 0xfe 0xff big-endian utf-16 follows
20086 **
20087 */
20088 /* #include <assert.h> */
20089
20090 #ifndef SQLITE_AMALGAMATION
20091 /*
20092 ** The following constant value is used by the SQLITE_BIGENDIAN and
20093 ** SQLITE_LITTLEENDIAN macros.
@@ -20486,11 +20512,11 @@
20512 ** no longer required.
20513 **
20514 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20515 ** flag set.
20516 */
20517 #ifdef SQLITE_ENABLE_STAT2
20518 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20519 Mem m;
20520 memset(&m, 0, sizeof(m));
20521 m.db = db;
20522 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -20600,10 +20626,11 @@
20626 **
20627 ** This file contains functions for allocating memory, comparing
20628 ** strings, and stuff like that.
20629 **
20630 */
20631 /* #include <stdarg.h> */
20632 #ifdef SQLITE_HAVE_ISNAN
20633 # include <math.h>
20634 #endif
20635
20636 /*
@@ -21778,10 +21805,11 @@
21805 **
21806 *************************************************************************
21807 ** This is the implementation of generic hash-tables
21808 ** used in SQLite.
21809 */
21810 /* #include <assert.h> */
21811
21812 /* Turn bulk memory into a hash table object by initializing the
21813 ** fields of the Hash structure.
21814 **
21815 ** "pNew" is a pointer to the hash table that is to be initialized.
@@ -22086,52 +22114,52 @@
22114 /* 35 */ "ReadCookie",
22115 /* 36 */ "SetCookie",
22116 /* 37 */ "VerifyCookie",
22117 /* 38 */ "OpenRead",
22118 /* 39 */ "OpenWrite",
22119 /* 40 */ "OpenSorter",
22120 /* 41 */ "OpenAutoindex",
22121 /* 42 */ "OpenEphemeral",
22122 /* 43 */ "OpenPseudo",
22123 /* 44 */ "Close",
22124 /* 45 */ "SeekLt",
22125 /* 46 */ "SeekLe",
22126 /* 47 */ "SeekGe",
22127 /* 48 */ "SeekGt",
22128 /* 49 */ "Seek",
22129 /* 50 */ "NotFound",
22130 /* 51 */ "Found",
22131 /* 52 */ "IsUnique",
22132 /* 53 */ "NotExists",
22133 /* 54 */ "Sequence",
22134 /* 55 */ "NewRowid",
22135 /* 56 */ "Insert",
22136 /* 57 */ "InsertInt",
22137 /* 58 */ "Delete",
22138 /* 59 */ "ResetCount",
22139 /* 60 */ "RowKey",
22140 /* 61 */ "RowData",
22141 /* 62 */ "Rowid",
22142 /* 63 */ "NullRow",
22143 /* 64 */ "Last",
22144 /* 65 */ "Sort",
22145 /* 66 */ "Rewind",
22146 /* 67 */ "Prev",
22147 /* 68 */ "Or",
22148 /* 69 */ "And",
22149 /* 70 */ "Next",
22150 /* 71 */ "IdxInsert",
22151 /* 72 */ "IdxDelete",
22152 /* 73 */ "IsNull",
22153 /* 74 */ "NotNull",
22154 /* 75 */ "Ne",
22155 /* 76 */ "Eq",
22156 /* 77 */ "Gt",
22157 /* 78 */ "Le",
22158 /* 79 */ "Lt",
22159 /* 80 */ "Ge",
22160 /* 81 */ "IdxRowid",
22161 /* 82 */ "BitAnd",
22162 /* 83 */ "BitOr",
22163 /* 84 */ "ShiftLeft",
22164 /* 85 */ "ShiftRight",
22165 /* 86 */ "Add",
@@ -22138,58 +22166,58 @@
22166 /* 87 */ "Subtract",
22167 /* 88 */ "Multiply",
22168 /* 89 */ "Divide",
22169 /* 90 */ "Remainder",
22170 /* 91 */ "Concat",
22171 /* 92 */ "IdxLT",
22172 /* 93 */ "BitNot",
22173 /* 94 */ "String8",
22174 /* 95 */ "IdxGE",
22175 /* 96 */ "Destroy",
22176 /* 97 */ "Clear",
22177 /* 98 */ "CreateIndex",
22178 /* 99 */ "CreateTable",
22179 /* 100 */ "ParseSchema",
22180 /* 101 */ "LoadAnalysis",
22181 /* 102 */ "DropTable",
22182 /* 103 */ "DropIndex",
22183 /* 104 */ "DropTrigger",
22184 /* 105 */ "IntegrityCk",
22185 /* 106 */ "RowSetAdd",
22186 /* 107 */ "RowSetRead",
22187 /* 108 */ "RowSetTest",
22188 /* 109 */ "Program",
22189 /* 110 */ "Param",
22190 /* 111 */ "FkCounter",
22191 /* 112 */ "FkIfZero",
22192 /* 113 */ "MemMax",
22193 /* 114 */ "IfPos",
22194 /* 115 */ "IfNeg",
22195 /* 116 */ "IfZero",
22196 /* 117 */ "AggStep",
22197 /* 118 */ "AggFinal",
22198 /* 119 */ "Checkpoint",
22199 /* 120 */ "JournalMode",
22200 /* 121 */ "Vacuum",
22201 /* 122 */ "IncrVacuum",
22202 /* 123 */ "Expire",
22203 /* 124 */ "TableLock",
22204 /* 125 */ "VBegin",
22205 /* 126 */ "VCreate",
22206 /* 127 */ "VDestroy",
22207 /* 128 */ "VOpen",
22208 /* 129 */ "VFilter",
22209 /* 130 */ "Real",
22210 /* 131 */ "VColumn",
22211 /* 132 */ "VNext",
22212 /* 133 */ "VRename",
22213 /* 134 */ "VUpdate",
22214 /* 135 */ "Pagecount",
22215 /* 136 */ "MaxPgcnt",
22216 /* 137 */ "Trace",
22217 /* 138 */ "Noop",
22218 /* 139 */ "Explain",
22219 /* 140 */ "NotUsed_140",
22220 /* 141 */ "ToText",
22221 /* 142 */ "ToBlob",
22222 /* 143 */ "ToNumeric",
22223 /* 144 */ "ToInt",
@@ -24450,10 +24478,11 @@
24478 */
24479 #include <sys/types.h>
24480 #include <sys/stat.h>
24481 #include <fcntl.h>
24482 #include <unistd.h>
24483 /* #include <time.h> */
24484 #include <sys/time.h>
24485 #include <errno.h>
24486 #ifndef SQLITE_OMIT_WAL
24487 #include <sys/mman.h>
24488 #endif
@@ -24485,10 +24514,11 @@
24514 /*
24515 ** If we are to be thread-safe, include the pthreads header and define
24516 ** the SQLITE_UNIX_THREADS macro.
24517 */
24518 #if SQLITE_THREADSAFE
24519 /* # include <pthread.h> */
24520 # define SQLITE_UNIX_THREADS 1
24521 #endif
24522
24523 /*
24524 ** Default permissions when creating a new file
@@ -24584,11 +24614,15 @@
24614 ** Allowed values for the unixFile.ctrlFlags bitmask:
24615 */
24616 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
24617 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
24618 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
24619 #ifndef SQLITE_DISABLE_DIRSYNC
24620 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
24621 #else
24622 # define UNIXFILE_DIRSYNC 0x00
24623 #endif
24624
24625 /*
24626 ** Include code that is common to all os_*.c files
24627 */
24628 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -27061,15 +27095,16 @@
27095 */
27096 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
27097 int rc = SQLITE_OK;
27098 int reserved = 0;
27099 unixFile *pFile = (unixFile*)id;
27100 afpLockingContext *context;
27101
27102 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
27103
27104 assert( pFile );
27105 context = (afpLockingContext *) pFile->lockingContext;
27106 if( context->reserved ){
27107 *pResOut = 1;
27108 return SQLITE_OK;
27109 }
27110 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
@@ -27205,11 +27240,11 @@
27240
27241 /* If control gets to this point, then actually go ahead and make
27242 ** operating system calls for the specified lock.
27243 */
27244 if( eFileLock==SHARED_LOCK ){
27245 int lrc1, lrc2, lrc1Errno = 0;
27246 long lk, mask;
27247
27248 assert( pInode->nShared==0 );
27249 assert( pInode->eFileLock==0 );
27250
@@ -27579,21 +27614,23 @@
27614 #if defined(USE_PREAD)
27615 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27616 #elif defined(USE_PREAD64)
27617 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
27618 #else
27619 do{
27620 newOffset = lseek(id->h, offset, SEEK_SET);
27621 SimulateIOError( newOffset-- );
27622 if( newOffset!=offset ){
27623 if( newOffset == -1 ){
27624 ((unixFile*)id)->lastErrno = errno;
27625 }else{
27626 ((unixFile*)id)->lastErrno = 0;
27627 }
27628 return -1;
27629 }
27630 got = osWrite(id->h, pBuf, cnt);
27631 }while( got<0 && errno==EINTR );
27632 #endif
27633 TIMER_END;
27634 if( got<0 ){
27635 ((unixFile*)id)->lastErrno = errno;
27636 }
@@ -27888,10 +27925,12 @@
27925 HAVE_FULLFSYNC, isFullsync));
27926 rc = osOpenDirectory(pFile->zPath, &dirfd);
27927 if( rc==SQLITE_OK && dirfd>=0 ){
27928 full_fsync(dirfd, 0, 0);
27929 robust_close(pFile, dirfd, __LINE__);
27930 }else if( rc==SQLITE_CANTOPEN ){
27931 rc = SQLITE_OK;
27932 }
27933 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
27934 }
27935 return rc;
27936 }
@@ -27971,30 +28010,22 @@
28010 static int proxyFileControl(sqlite3_file*,int,void*);
28011 #endif
28012
28013 /*
28014 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
28015 ** file-control operation. Enlarge the database to nBytes in size
28016 ** (rounded up to the next chunk-size). If the database is already
28017 ** nBytes or larger, this routine is a no-op.
 
 
28018 */
28019 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
28020 if( pFile->szChunk ){
28021 i64 nSize; /* Required file size */
 
28022 struct stat buf; /* Used to hold return values of fstat() */
28023
28024 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
28025
28026 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
 
 
 
 
 
28027 if( nSize>(i64)buf.st_size ){
28028
28029 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
28030 /* The code below is handling the return value of osFallocate()
28031 ** correctly. posix_fallocate() is defined to "returns zero on success,
@@ -28048,11 +28079,15 @@
28079 case SQLITE_FCNTL_CHUNK_SIZE: {
28080 pFile->szChunk = *(int *)pArg;
28081 return SQLITE_OK;
28082 }
28083 case SQLITE_FCNTL_SIZE_HINT: {
28084 int rc;
28085 SimulateIOErrorBenign(1);
28086 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
28087 SimulateIOErrorBenign(0);
28088 return rc;
28089 }
28090 case SQLITE_FCNTL_PERSIST_WAL: {
28091 int bPersist = *(int*)pArg;
28092 if( bPersist<0 ){
28093 *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
@@ -29485,10 +29520,13 @@
29520 int isReadonly = (flags & SQLITE_OPEN_READONLY);
29521 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
29522 #if SQLITE_ENABLE_LOCKING_STYLE
29523 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
29524 #endif
29525 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29526 struct statfs fsInfo;
29527 #endif
29528
29529 /* If creating a master or main-file journal, this function will open
29530 ** a file-descriptor on the directory too. The first time unixSync()
29531 ** is called the directory file descriptor will be fsync()ed and close()d.
29532 */
@@ -29617,11 +29655,10 @@
29655
29656 noLock = eType!=SQLITE_OPEN_MAIN_DB;
29657
29658
29659 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 
29660 if( fstatfs(fd, &fsInfo) == -1 ){
29661 ((unixFile*)pFile)->lastErrno = errno;
29662 robust_close(p, fd, __LINE__);
29663 return SQLITE_IOERR_ACCESS;
29664 }
@@ -29641,11 +29678,10 @@
29678 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
29679 ** never use proxy, NULL means use proxy for non-local files only. */
29680 if( envforce!=NULL ){
29681 useProxy = atoi(envforce)>0;
29682 }else{
 
29683 if( statfs(zPath, &fsInfo) == -1 ){
29684 /* In theory, the close(fd) call is sub-optimal. If the file opened
29685 ** with fd is a database file, and there are other connections open
29686 ** on that file that are currently holding advisory locks on it,
29687 ** then the call to close() will cancel those locks. In practice,
@@ -29715,10 +29751,12 @@
29751 #endif
29752 {
29753 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29754 }
29755 robust_close(0, fd, __LINE__);
29756 }else if( rc==SQLITE_CANTOPEN ){
29757 rc = SQLITE_OK;
29758 }
29759 }
29760 #endif
29761 return rc;
29762 }
@@ -30380,10 +30418,12 @@
30418 *pError = err;
30419 }
30420 return SQLITE_IOERR;
30421 }
30422 }
30423 #else
30424 UNUSED_PARAMETER(pError);
30425 #endif
30426 #ifdef SQLITE_TEST
30427 /* simulate multiple hosts by creating unique hostid file paths */
30428 if( sqlite3_hostid_num != 0){
30429 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
@@ -30472,10 +30512,11 @@
30512 unixFile *conchFile = pCtx->conchFile;
30513 int rc = SQLITE_OK;
30514 int nTries = 0;
30515 struct timespec conchModTime;
30516
30517 memset(&conchModTime, 0, sizeof(conchModTime));
30518 do {
30519 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30520 nTries ++;
30521 if( rc==SQLITE_BUSY ){
30522 /* If the lock failed (busy):
@@ -30703,15 +30744,16 @@
30744 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30745
30746 end_takeconch:
30747 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
30748 if( rc==SQLITE_OK && pFile->openFlags ){
30749 int fd;
30750 if( pFile->h>=0 ){
30751 robust_close(pFile, pFile->h, __LINE__);
30752 }
30753 pFile->h = -1;
30754 fd = robust_open(pCtx->dbPath, pFile->openFlags,
30755 SQLITE_DEFAULT_FILE_PERMISSIONS);
30756 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
30757 if( fd>=0 ){
30758 pFile->h = fd;
30759 }else{
@@ -31629,10 +31671,80 @@
31671 winceLock local; /* Locks obtained by this instance of winFile */
31672 winceLock *shared; /* Global shared lock memory for the file */
31673 #endif
31674 };
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 */
31746
31747 /*
31748 ** Forward prototypes.
31749 */
31750 static int getSectorSize(
@@ -31681,10 +31793,192 @@
31793 }
31794 return sqlite3_os_type==2;
31795 }
31796 #endif /* SQLITE_OS_WINCE */
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
31980 /*
31981 ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
31982 **
31983 ** Space to hold the returned string is obtained from malloc.
31984 */
@@ -31969,10 +32263,11 @@
32263 */
32264 /*
32265 ** WindowsCE does not have a localtime() function. So create a
32266 ** substitute.
32267 */
32268 /* #include <time.h> */
32269 struct tm *__cdecl localtime(const time_t *t)
32270 {
32271 static struct tm y;
32272 FILETIME uTm, lTm;
32273 SYSTEMTIME pTm;
@@ -32860,15 +33155,22 @@
33155 case SQLITE_FCNTL_CHUNK_SIZE: {
33156 pFile->szChunk = *(int *)pArg;
33157 return SQLITE_OK;
33158 }
33159 case SQLITE_FCNTL_SIZE_HINT: {
33160 winFile *pFile = (winFile*)id;
33161 sqlite3_int64 oldSz;
33162 int rc = winFileSize(id, &oldSz);
33163 if( rc==SQLITE_OK ){
33164 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
33165 if( newSz>oldSz ){
33166 SimulateIOErrorBenign(1);
33167 rc = winTruncate(id, newSz);
33168 SimulateIOErrorBenign(0);
33169 }
33170 }
33171 return rc;
33172 }
33173 case SQLITE_FCNTL_PERSIST_WAL: {
33174 int bPersist = *(int*)pArg;
33175 if( bPersist<0 ){
33176 *(int*)pArg = pFile->bPersistWal;
@@ -35473,10 +35775,13 @@
35775 typedef struct PCache1 PCache1;
35776 typedef struct PgHdr1 PgHdr1;
35777 typedef struct PgFreeslot PgFreeslot;
35778 typedef struct PGroup PGroup;
35779
35780 typedef struct PGroupBlock PGroupBlock;
35781 typedef struct PGroupBlockList PGroupBlockList;
35782
35783 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
35784 ** of one or more PCaches that are able to recycle each others unpinned
35785 ** pages when they are under memory pressure. A PGroup is an instance of
35786 ** the following object.
35787 **
@@ -35502,12 +35807,70 @@
35807 int nMaxPage; /* Sum of nMax for purgeable caches */
35808 int nMinPage; /* Sum of nMin for purgeable caches */
35809 int mxPinned; /* nMaxpage + 10 - nMinPage */
35810 int nCurrentPage; /* Number of purgeable pages allocated */
35811 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
35812 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
35813 int isBusy; /* Do not run ReleaseMemory() if true */
35814 PGroupBlockList *pBlockList; /* List of block-lists for this group */
35815 #endif
35816 };
35817
35818 /*
35819 ** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
35820 ** each PGroup structure has a linked list of the the following starting
35821 ** at PGroup.pBlockList. There is one entry for each distinct page-size
35822 ** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
35823 ** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
35824 ** size requested by each pcache, which is the database page-size plus
35825 ** the various header structures used by the pcache, pager and btree layers.
35826 ** Usually around (pgsz+200) bytes.
35827 **
35828 ** This size (pgsz+200) bytes is not allocated efficiently by some
35829 ** implementations of malloc. In particular, some implementations are only
35830 ** able to allocate blocks of memory chunks of 2^N bytes, where N is some
35831 ** integer value. Since the page-size is a power of 2, this means we
35832 ** end up wasting (pgsz-200) bytes in each allocation.
35833 **
35834 ** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
35835 ** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes
35836 ** are requested from malloc allocator. After a block is returned,
35837 ** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
35838 ** allocations can fit in the space returned by malloc(). This value may
35839 ** be more than M.
35840 **
35841 ** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
35842 ** contains the number of allocations that will fit in the aData[] space.
35843 ** nEntry is limited to the number of bits in bitmask mUsed. If a slot
35844 ** within aData is in use, the corresponding bit in mUsed is set. Thus
35845 ** when (mUsed+1==(1 << nEntry)) the block is completely full.
35846 **
35847 ** Each time a slot within a block is freed, the block is moved to the start
35848 ** of the linked-list. And if a block becomes completely full, then it is
35849 ** moved to the end of the list. As a result, when searching for a free
35850 ** slot, only the first block in the list need be examined. If it is full,
35851 ** then it is guaranteed that all blocks are full.
35852 */
35853 struct PGroupBlockList {
35854 int nByte; /* Size of each allocation in bytes */
35855 PGroupBlock *pFirst; /* First PGroupBlock in list */
35856 PGroupBlock *pLast; /* Last PGroupBlock in list */
35857 PGroupBlockList *pNext; /* Next block-list attached to group */
35858 };
35859
35860 struct PGroupBlock {
35861 Bitmask mUsed; /* Mask of used slots */
35862 int nEntry; /* Maximum number of allocations in aData[] */
35863 u8 *aData; /* Pointer to data block */
35864 PGroupBlock *pNext; /* Next PGroupBlock in list */
35865 PGroupBlock *pPrev; /* Previous PGroupBlock in list */
35866 PGroupBlockList *pList; /* Owner list */
35867 };
35868
35869 /* Minimum value for PGroupBlock.nEntry */
35870 #define PAGECACHE_BLOCKALLOC_MINENTRY 15
35871
35872 /* Each page cache is an instance of the following object. Every
35873 ** open database file (including each in-memory database and each
35874 ** temporary or transient database) has a single page cache which
35875 ** is an instance of this object.
35876 **
@@ -35606,10 +35969,21 @@
35969 **
35970 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35971 */
35972 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35973 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35974
35975 /*
35976 ** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve
35977 ** a PGroupBlock pointer based on a pointer to a page buffer.
35978 */
35979 #define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
35980 ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
35981
35982 #define PAGE_GET_BLOCKPTR(pCache, pPg) \
35983 ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
35984
35985
35986 /*
35987 ** Macros to enter and leave the PCache LRU mutex.
35988 */
35989 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -35732,25 +36106,159 @@
36106 return iSize;
36107 }
36108 }
36109 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
36110
36111 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36112 /*
36113 ** The block pBlock belongs to list pList but is not currently linked in.
36114 ** Insert it into the start of the list.
36115 */
36116 static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
36117 pBlock->pPrev = 0;
36118 pBlock->pNext = pList->pFirst;
36119 pList->pFirst = pBlock;
36120 if( pBlock->pNext ){
36121 pBlock->pNext->pPrev = pBlock;
36122 }else{
36123 assert( pList->pLast==0 );
36124 pList->pLast = pBlock;
36125 }
36126 }
36127
36128 /*
36129 ** If there are no blocks in the list headed by pList, remove pList
36130 ** from the pGroup->pBlockList list and free it with sqlite3_free().
36131 */
36132 static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
36133 assert( sqlite3_mutex_held(pGroup->mutex) );
36134 if( pList->pFirst==0 ){
36135 PGroupBlockList **pp;
36136 for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
36137 *pp = (*pp)->pNext;
36138 sqlite3_free(pList);
36139 }
36140 }
36141 #endif /* SQLITE_PAGECACHE_BLOCKALLOC */
36142
36143 /*
36144 ** Allocate a new page object initially associated with cache pCache.
36145 */
36146 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
36147 int nByte = sizeof(PgHdr1) + pCache->szPage;
36148 void *pPg = 0;
36149 PgHdr1 *p;
36150
36151 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36152 PGroup *pGroup = pCache->pGroup;
36153 PGroupBlockList *pList;
36154 PGroupBlock *pBlock;
36155 int i;
36156
36157 nByte += sizeof(PGroupBlockList *);
36158 nByte = ROUND8(nByte);
36159
36160 for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36161 if( pList->nByte==nByte ) break;
36162 }
36163 if( pList==0 ){
36164 PGroupBlockList *pNew;
36165 assert( pGroup->isBusy==0 );
36166 assert( sqlite3_mutex_held(pGroup->mutex) );
36167 pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */
36168 pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
36169 pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */
36170 if( pNew==0 ){
36171 /* malloc() failure. Return early. */
36172 return 0;
36173 }
36174 #ifdef SQLITE_DEBUG
36175 for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36176 assert( pList->nByte!=nByte );
36177 }
36178 #endif
36179 pNew->nByte = nByte;
36180 pNew->pNext = pGroup->pBlockList;
36181 pGroup->pBlockList = pNew;
36182 pList = pNew;
36183 }
36184
36185 pBlock = pList->pFirst;
36186 if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
36187 int sz;
36188
36189 /* Allocate a new block. Try to allocate enough space for the PGroupBlock
36190 ** structure and MINENTRY allocations of nByte bytes each. If the
36191 ** allocator returns more memory than requested, then more than MINENTRY
36192 ** allocations may fit in it. */
36193 assert( sqlite3_mutex_held(pGroup->mutex) );
36194 pcache1LeaveMutex(pCache->pGroup);
36195 sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
36196 pBlock = (PGroupBlock *)sqlite3Malloc(sz);
36197 pcache1EnterMutex(pCache->pGroup);
36198
36199 if( !pBlock ){
36200 freeListIfEmpty(pGroup, pList);
36201 return 0;
36202 }
36203 pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
36204 if( pBlock->nEntry>=BMS ){
36205 pBlock->nEntry = BMS-1;
36206 }
36207 pBlock->pList = pList;
36208 pBlock->mUsed = 0;
36209 pBlock->aData = (u8 *)&pBlock[1];
36210 addBlockToList(pList, pBlock);
36211
36212 sz = sqlite3MallocSize(pBlock);
36213 sqlite3_mutex_enter(pcache1.mutex);
36214 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
36215 sqlite3_mutex_leave(pcache1.mutex);
36216 }
36217
36218 for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
36219 if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
36220 pBlock->mUsed |= ((Bitmask)1<<i);
36221 pPg = (void *)&pBlock->aData[pList->nByte * i];
36222 }
36223 }
36224 assert( pPg );
36225 PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
36226
36227 /* If the block is now full, shift it to the end of the list */
36228 if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
36229 assert( pList->pFirst==pBlock );
36230 assert( pBlock->pPrev==0 );
36231 assert( pList->pLast->pNext==0 );
36232 pList->pFirst = pBlock->pNext;
36233 pList->pFirst->pPrev = 0;
36234 pBlock->pPrev = pList->pLast;
36235 pBlock->pNext = 0;
36236 pList->pLast->pNext = pBlock;
36237 pList->pLast = pBlock;
36238 }
36239 p = PAGE_TO_PGHDR1(pCache, pPg);
36240 if( pCache->bPurgeable ){
36241 pCache->pGroup->nCurrentPage++;
36242 }
36243 #else
36244 /* The group mutex must be released before pcache1Alloc() is called. This
36245 ** is because it may call sqlite3_release_memory(), which assumes that
36246 ** this mutex is not held. */
36247 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36248 pcache1LeaveMutex(pCache->pGroup);
36249 pPg = pcache1Alloc(nByte);
36250 pcache1EnterMutex(pCache->pGroup);
36251 if( pPg ){
36252 p = PAGE_TO_PGHDR1(pCache, pPg);
36253 if( pCache->bPurgeable ){
36254 pCache->pGroup->nCurrentPage++;
36255 }
36256 }else{
36257 p = 0;
36258 }
36259 #endif
36260 return p;
36261 }
36262
36263 /*
36264 ** Free a page object allocated by pcache1AllocPage().
@@ -35760,14 +36268,56 @@
36268 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
36269 */
36270 static void pcache1FreePage(PgHdr1 *p){
36271 if( ALWAYS(p) ){
36272 PCache1 *pCache = p->pCache;
36273 void *pPg = PGHDR1_TO_PAGE(p);
36274
36275 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36276 PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
36277 PGroupBlockList *pList = pBlock->pList;
36278 int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
36279
36280 assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
36281 assert( pBlock->mUsed & ((Bitmask)1<<i) );
36282 pBlock->mUsed &= ~((Bitmask)1<<i);
36283
36284 /* Remove the block from the list. If it is completely empty, free it.
36285 ** Or if it is not completely empty, re-insert it at the start of the
36286 ** list. */
36287 if( pList->pFirst==pBlock ){
36288 pList->pFirst = pBlock->pNext;
36289 if( pList->pFirst ) pList->pFirst->pPrev = 0;
36290 }else{
36291 pBlock->pPrev->pNext = pBlock->pNext;
36292 }
36293 if( pList->pLast==pBlock ){
36294 pList->pLast = pBlock->pPrev;
36295 if( pList->pLast ) pList->pLast->pNext = 0;
36296 }else{
36297 pBlock->pNext->pPrev = pBlock->pPrev;
36298 }
36299
36300 if( pBlock->mUsed==0 ){
36301 PGroup *pGroup = p->pCache->pGroup;
36302
36303 int sz = sqlite3MallocSize(pBlock);
36304 sqlite3_mutex_enter(pcache1.mutex);
36305 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
36306 sqlite3_mutex_leave(pcache1.mutex);
36307 freeListIfEmpty(pGroup, pList);
36308 sqlite3_free(pBlock);
36309 }else{
36310 addBlockToList(pList, pBlock);
36311 }
36312 #else
36313 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36314 pcache1Free(pPg);
36315 #endif
36316 if( pCache->bPurgeable ){
36317 pCache->pGroup->nCurrentPage--;
36318 }
 
36319 }
36320 }
36321
36322 /*
36323 ** Malloc function used by SQLite to obtain space from the buffer configured
@@ -36201,13 +36751,11 @@
36751 /* Step 5. If a usable page buffer has still not been found,
36752 ** attempt to allocate a new one.
36753 */
36754 if( !pPage ){
36755 if( createFlag==1 ) sqlite3BeginBenignMalloc();
 
36756 pPage = pcache1AllocPage(pCache);
 
36757 if( createFlag==1 ) sqlite3EndBenignMalloc();
36758 }
36759
36760 if( pPage ){
36761 unsigned int h = iKey % pCache->nHash;
@@ -36373,10 +36921,13 @@
36921 ** been released, the function returns. The return value is the total number
36922 ** of bytes of memory released.
36923 */
36924 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
36925 int nFree = 0;
36926 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36927 if( pcache1.grp.isBusy ) return 0;
36928 #endif
36929 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36930 assert( sqlite3_mutex_notheld(pcache1.mutex) );
36931 if( pcache1.pStart==0 ){
36932 PgHdr1 *p;
36933 pcache1EnterMutex(&pcache1.grp);
@@ -37585,10 +38136,12 @@
38136 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
38137 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
38138 u8 tempFile; /* zFilename is a temporary file */
38139 u8 readOnly; /* True for a read-only database */
38140 u8 memDb; /* True to inhibit all file I/O */
38141 u8 hasSeenStress; /* pagerStress() called one or more times */
38142 u8 isSorter; /* True for a PAGER_SORTER */
38143
38144 /**************************************************************************
38145 ** The following block contains those class members that change during
38146 ** routine opertion. Class members not in this block are either fixed
38147 ** when the pager is first created or else only change when there is a
@@ -37807,10 +38360,19 @@
38360 || p->journalMode==PAGER_JOURNALMODE_MEMORY
38361 );
38362 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
38363 assert( pagerUseWal(p)==0 );
38364 }
38365
38366 /* A sorter is a temp file that never spills to disk and always has
38367 ** the doNotSpill flag set
38368 */
38369 if( p->isSorter ){
38370 assert( p->tempFile );
38371 assert( p->doNotSpill );
38372 assert( p->fd->pMethods==0 );
38373 }
38374
38375 /* If changeCountDone is set, a RESERVED lock or greater must be held
38376 ** on the file.
38377 */
38378 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
@@ -40704,10 +41266,11 @@
41266 ** to the caller.
41267 */
41268 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
41269 u8 *pTmp = (u8 *)pPager->pTmpSpace;
41270
41271 assert( assert_pager_state(pPager) );
41272 disable_simulated_io_errors();
41273 sqlite3BeginBenignMalloc();
41274 /* pPager->errCode = 0; */
41275 pPager->exclusiveMode = 0;
41276 #ifndef SQLITE_OMIT_WAL
@@ -41138,10 +41701,11 @@
41701 ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
41702 ** while in the error state, hence it is impossible for this routine to
41703 ** be called in the error state. Nevertheless, we include a NEVER()
41704 ** test for the error state as a safeguard against future changes.
41705 */
41706 pPager->hasSeenStress = 1;
41707 if( NEVER(pPager->errCode) ) return SQLITE_OK;
41708 if( pPager->doNotSpill ) return SQLITE_OK;
41709 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
41710 return SQLITE_OK;
41711 }
@@ -41509,10 +42073,16 @@
42073 }
42074 /* pPager->xBusyHandler = 0; */
42075 /* pPager->pBusyHandlerArg = 0; */
42076 pPager->xReiniter = xReinit;
42077 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
42078 #ifndef SQLITE_OMIT_MERGE_SORT
42079 if( flags & PAGER_SORTER ){
42080 pPager->doNotSpill = 1;
42081 pPager->isSorter = 1;
42082 }
42083 #endif
42084
42085 *ppPager = pPager;
42086 return SQLITE_OK;
42087 }
42088
@@ -43052,10 +43622,21 @@
43622 ** Return true if this is an in-memory pager.
43623 */
43624 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
43625 return MEMDB;
43626 }
43627
43628 #ifndef SQLITE_OMIT_MERGE_SORT
43629 /*
43630 ** Return true if the pager has seen a pagerStress callback.
43631 */
43632 SQLITE_PRIVATE int sqlite3PagerUnderStress(Pager *pPager){
43633 assert( pPager->isSorter );
43634 assert( pPager->doNotSpill );
43635 return pPager->hasSeenStress;
43636 }
43637 #endif
43638
43639 /*
43640 ** Check that there are at least nSavepoint savepoints open. If there are
43641 ** currently less than nSavepoints open, then open one or more savepoints
43642 ** to make up the difference. If the number of savepoints is already
@@ -49421,15 +50002,26 @@
50002 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
50003
50004 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
50005 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
50006
50007 /* The BTREE_SORTER flag is only used if SQLITE_OMIT_MERGE_SORT is undef */
50008 #ifdef SQLITE_OMIT_MERGE_SORT
50009 assert( (flags & BTREE_SORTER)==0 );
50010 #endif
50011
50012 /* BTREE_SORTER is always on a BTREE_SINGLE, BTREE_OMIT_JOURNAL */
50013 assert( (flags & BTREE_SORTER)==0 ||
50014 (flags & (BTREE_SINGLE|BTREE_OMIT_JOURNAL))
50015 ==(BTREE_SINGLE|BTREE_OMIT_JOURNAL) );
50016
50017 if( db->flags & SQLITE_NoReadlock ){
50018 flags |= BTREE_NO_READLOCK;
50019 }
50020 if( isMemdb ){
50021 flags |= BTREE_MEMORY;
50022 flags &= ~BTREE_SORTER;
50023 }
50024 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
50025 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
50026 }
50027 p = sqlite3MallocZero(sizeof(Btree));
@@ -51155,11 +51747,12 @@
51747
51748 if( NEVER(wrFlag && pBt->readOnly) ){
51749 return SQLITE_READONLY;
51750 }
51751 if( iTable==1 && btreePagecount(pBt)==0 ){
51752 assert( wrFlag==0 );
51753 iTable = 0;
51754 }
51755
51756 /* Now that no other errors can occur, finish filling in the BtCursor
51757 ** variables and link the cursor into the BtShared list. */
51758 pCur->pgnoRoot = (Pgno)iTable;
@@ -51909,10 +52502,13 @@
52502 int i;
52503 for(i=1; i<=pCur->iPage; i++){
52504 releasePage(pCur->apPage[i]);
52505 }
52506 pCur->iPage = 0;
52507 }else if( pCur->pgnoRoot==0 ){
52508 pCur->eState = CURSOR_INVALID;
52509 return SQLITE_OK;
52510 }else{
52511 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
52512 if( rc!=SQLITE_OK ){
52513 pCur->eState = CURSOR_INVALID;
52514 return rc;
@@ -52018,11 +52614,11 @@
52614 assert( cursorHoldsMutex(pCur) );
52615 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52616 rc = moveToRoot(pCur);
52617 if( rc==SQLITE_OK ){
52618 if( pCur->eState==CURSOR_INVALID ){
52619 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52620 *pRes = 1;
52621 }else{
52622 assert( pCur->apPage[pCur->iPage]->nCell>0 );
52623 *pRes = 0;
52624 rc = moveToLeftmost(pCur);
@@ -52057,11 +52653,11 @@
52653 }
52654
52655 rc = moveToRoot(pCur);
52656 if( rc==SQLITE_OK ){
52657 if( CURSOR_INVALID==pCur->eState ){
52658 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52659 *pRes = 1;
52660 }else{
52661 assert( pCur->eState==CURSOR_VALID );
52662 *pRes = 0;
52663 rc = moveToRightmost(pCur);
@@ -52130,16 +52726,16 @@
52726
52727 rc = moveToRoot(pCur);
52728 if( rc ){
52729 return rc;
52730 }
52731 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
52732 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
52733 assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
52734 if( pCur->eState==CURSOR_INVALID ){
52735 *pRes = -1;
52736 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52737 return SQLITE_OK;
52738 }
52739 assert( pCur->apPage[0]->intKey || pIdxKey );
52740 for(;;){
52741 int lwr, upr, idx;
@@ -54964,13 +55560,20 @@
55560 releasePage(pPage);
55561 }
55562 return rc;
55563 }
55564 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
55565 BtShared *pBt = p->pBt;
55566 int rc;
55567 sqlite3BtreeEnter(p);
55568 if( (pBt->openFlags&BTREE_SINGLE) ){
55569 pBt->nPage = 0;
55570 sqlite3PagerTruncateImage(pBt->pPager, 1);
55571 rc = newDatabase(pBt);
55572 }else{
55573 rc = btreeDropTable(p, iTable, piMoved);
55574 }
55575 sqlite3BtreeLeave(p);
55576 return rc;
55577 }
55578
55579
@@ -55045,10 +55648,15 @@
55648 ** corruption) an SQLite error code is returned.
55649 */
55650 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
55651 i64 nEntry = 0; /* Value to return in *pnEntry */
55652 int rc; /* Return code */
55653
55654 if( pCur->pgnoRoot==0 ){
55655 *pnEntry = 0;
55656 return SQLITE_OK;
55657 }
55658 rc = moveToRoot(pCur);
55659
55660 /* Unless an error occurs, the following loop runs one iteration for each
55661 ** page in the B-Tree structure (not including overflow pages).
55662 */
@@ -55829,11 +56437,10 @@
56437 */
56438 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
56439 BtShared *pBt = pBtree->pBt;
56440 int rc; /* Return code */
56441
 
56442 assert( iVersion==1 || iVersion==2 );
56443
56444 /* If setting the version fields to 1, do not automatically open the
56445 ** WAL connection, even if the version fields are currently set to 2.
56446 */
@@ -56268,106 +56875,110 @@
56875 /* Update the schema version field in the destination database. This
56876 ** is to make sure that the schema-version really does change in
56877 ** the case where the source and destination databases have the
56878 ** same schema version.
56879 */
56880 if( rc==SQLITE_DONE ){
56881 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
56882 if( rc==SQLITE_OK ){
56883 if( p->pDestDb ){
56884 sqlite3ResetInternalSchema(p->pDestDb, -1);
56885 }
56886 if( destMode==PAGER_JOURNALMODE_WAL ){
56887 rc = sqlite3BtreeSetVersion(p->pDest, 2);
56888 }
56889 }
56890 if( rc==SQLITE_OK ){
56891 int nDestTruncate;
56892 /* Set nDestTruncate to the final number of pages in the destination
56893 ** database. The complication here is that the destination page
56894 ** size may be different to the source page size.
56895 **
56896 ** If the source page size is smaller than the destination page size,
56897 ** round up. In this case the call to sqlite3OsTruncate() below will
56898 ** fix the size of the file. However it is important to call
56899 ** sqlite3PagerTruncateImage() here so that any pages in the
56900 ** destination file that lie beyond the nDestTruncate page mark are
56901 ** journalled by PagerCommitPhaseOne() before they are destroyed
56902 ** by the file truncation.
56903 */
56904 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56905 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56906 if( pgszSrc<pgszDest ){
56907 int ratio = pgszDest/pgszSrc;
56908 nDestTruncate = (nSrcPage+ratio-1)/ratio;
56909 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56910 nDestTruncate--;
56911 }
56912 }else{
56913 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56914 }
56915 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56916
56917 if( pgszSrc<pgszDest ){
56918 /* If the source page-size is smaller than the destination page-size,
56919 ** two extra things may need to happen:
56920 **
56921 ** * The destination may need to be truncated, and
56922 **
56923 ** * Data stored on the pages immediately following the
56924 ** pending-byte page in the source database may need to be
56925 ** copied into the destination database.
56926 */
56927 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56928 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56929 i64 iOff;
56930 i64 iEnd;
56931
56932 assert( pFile );
56933 assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56934 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56935 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56936 ));
56937
56938 /* This call ensures that all data required to recreate the original
56939 ** database has been stored in the journal for pDestPager and the
56940 ** journal synced to disk. So at this point we may safely modify
56941 ** the database file in any way, knowing that if a power failure
56942 ** occurs, the original database will be reconstructed from the
56943 ** journal file. */
56944 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56945
56946 /* Write the extra pages and truncate the database file as required */
56947 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56948 for(
56949 iOff=PENDING_BYTE+pgszSrc;
56950 rc==SQLITE_OK && iOff<iEnd;
56951 iOff+=pgszSrc
56952 ){
56953 PgHdr *pSrcPg = 0;
56954 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56955 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56956 if( rc==SQLITE_OK ){
56957 u8 *zData = sqlite3PagerGetData(pSrcPg);
56958 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56959 }
56960 sqlite3PagerUnref(pSrcPg);
56961 }
56962 if( rc==SQLITE_OK ){
56963 rc = backupTruncateFile(pFile, iSize);
56964 }
56965
56966 /* Sync the database file to disk. */
56967 if( rc==SQLITE_OK ){
56968 rc = sqlite3PagerSync(pDestPager);
56969 }
56970 }else{
56971 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56972 }
56973
56974 /* Finish committing the transaction to the destination database. */
56975 if( SQLITE_OK==rc
56976 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56977 ){
56978 rc = SQLITE_DONE;
56979 }
56980 }
56981 }
56982
56983 /* If bCloseTrans is true, then this function opened a read transaction
56984 ** on the source database. Close the read transaction here. There is
@@ -56831,38 +57442,32 @@
57442 ** invoking an external callback, free it now. Calling this function
57443 ** does not free any Mem.zMalloc buffer.
57444 */
57445 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
57446 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
57447 if( p->flags&MEM_Agg ){
57448 sqlite3VdbeMemFinalize(p, p->u.pDef);
57449 assert( (p->flags & MEM_Agg)==0 );
57450 sqlite3VdbeMemRelease(p);
57451 }else if( p->flags&MEM_Dyn && p->xDel ){
57452 assert( (p->flags&MEM_RowSet)==0 );
57453 p->xDel((void *)p->z);
57454 p->xDel = 0;
57455 }else if( p->flags&MEM_RowSet ){
57456 sqlite3RowSetClear(p->u.pRowSet);
57457 }else if( p->flags&MEM_Frame ){
57458 sqlite3VdbeMemSetNull(p);
 
 
 
 
 
 
57459 }
57460 }
57461
57462 /*
57463 ** Release any memory held by the Mem. This may leave the Mem in an
57464 ** inconsistent state, for example with (Mem.z==0) and
57465 ** (Mem.type==SQLITE_TEXT).
57466 */
57467 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
57468 MemReleaseExt(p);
57469 sqlite3DbFree(p->db, p->zMalloc);
57470 p->z = 0;
57471 p->zMalloc = 0;
57472 p->xDel = 0;
57473 }
@@ -57180,11 +57785,11 @@
57785 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
57786 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
57787 */
57788 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
57789 assert( (pFrom->flags & MEM_RowSet)==0 );
57790 MemReleaseExt(pTo);
57791 memcpy(pTo, pFrom, MEMCELLSIZE);
57792 pTo->xDel = 0;
57793 if( (pFrom->flags&MEM_Static)==0 ){
57794 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
57795 assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57198,11 +57803,11 @@
57803 */
57804 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
57805 int rc = SQLITE_OK;
57806
57807 assert( (pFrom->flags & MEM_RowSet)==0 );
57808 MemReleaseExt(pTo);
57809 memcpy(pTo, pFrom, MEMCELLSIZE);
57810 pTo->flags &= ~MEM_Dyn;
57811
57812 if( pTo->flags&(MEM_Str|MEM_Blob) ){
57813 if( 0==(pFrom->flags&MEM_Static) ){
@@ -57592,15 +58197,15 @@
58197 *ppVal = 0;
58198 return SQLITE_OK;
58199 }
58200 op = pExpr->op;
58201
58202 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
58203 ** The ifdef here is to enable us to achieve 100% branch test coverage even
58204 ** when SQLITE_ENABLE_STAT2 is omitted.
58205 */
58206 #ifdef SQLITE_ENABLE_STAT2
58207 if( op==TK_REGISTER ) op = pExpr->op2;
58208 #else
58209 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
58210 #endif
58211
@@ -58153,10 +58758,16 @@
58758 assert( p->nOp - i >= 3 );
58759 assert( pOp[-1].opcode==OP_Integer );
58760 n = pOp[-1].p1;
58761 if( n>nMaxArgs ) nMaxArgs = n;
58762 #endif
58763 }else if( opcode==OP_Next ){
58764 pOp->p4.xAdvance = sqlite3BtreeNext;
58765 pOp->p4type = P4_ADVANCE;
58766 }else if( opcode==OP_Prev ){
58767 pOp->p4.xAdvance = sqlite3BtreePrevious;
58768 pOp->p4type = P4_ADVANCE;
58769 }
58770
58771 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
58772 assert( -1-pOp->p2<p->nLabel );
58773 pOp->p2 = aLabel[-1-pOp->p2];
@@ -58244,37 +58855,34 @@
58855 ** Change the value of the P1 operand for a specific instruction.
58856 ** This routine is useful when a large program is loaded from a
58857 ** static array using sqlite3VdbeAddOpList but we want to make a
58858 ** few minor changes to the program.
58859 */
58860 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
58861 assert( p!=0 );
58862 if( ((u32)p->nOp)>addr ){
 
58863 p->aOp[addr].p1 = val;
58864 }
58865 }
58866
58867 /*
58868 ** Change the value of the P2 operand for a specific instruction.
58869 ** This routine is useful for setting a jump destination.
58870 */
58871 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
58872 assert( p!=0 );
58873 if( ((u32)p->nOp)>addr ){
 
58874 p->aOp[addr].p2 = val;
58875 }
58876 }
58877
58878 /*
58879 ** Change the value of the P3 operand for a specific instruction.
58880 */
58881 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
58882 assert( p!=0 );
58883 if( ((u32)p->nOp)>addr ){
 
58884 p->aOp[addr].p3 = val;
58885 }
58886 }
58887
58888 /*
@@ -58292,12 +58900,12 @@
58900 /*
58901 ** Change the P2 operand of instruction addr so that it points to
58902 ** the address of the next instruction to be coded.
58903 */
58904 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58905 assert( addr>=0 );
58906 sqlite3VdbeChangeP2(p, addr, p->nOp);
58907 }
58908
58909
58910 /*
58911 ** If the input FuncDef structure is ephemeral, then free it. If
@@ -58661,10 +59269,14 @@
59269 break;
59270 }
59271 case P4_SUBPROGRAM: {
59272 sqlite3_snprintf(nTemp, zTemp, "program");
59273 break;
59274 }
59275 case P4_ADVANCE: {
59276 zTemp[0] = 0;
59277 break;
59278 }
59279 default: {
59280 zP4 = pOp->p4.z;
59281 if( zP4==0 ){
59282 zP4 = zTemp;
@@ -59285,10 +59897,11 @@
59897 */
59898 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
59899 if( pCx==0 ){
59900 return;
59901 }
59902 sqlite3VdbeSorterClose(p->db, pCx);
59903 if( pCx->pBt ){
59904 sqlite3BtreeClose(pCx->pBt);
59905 /* The pCx->pCursor will be close automatically, if it exists, by
59906 ** the call above. */
59907 }else if( pCx->pCursor ){
@@ -62585,10 +63198,17 @@
63198 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
63199 ** P if required.
63200 */
63201 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
63202
63203 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
63204 #ifdef SQLITE_OMIT_MERGE_SORT
63205 # define isSorter(x) 0
63206 #else
63207 # define isSorter(x) ((x)->pSorter!=0)
63208 #endif
63209
63210 /*
63211 ** Argument pMem points at a register that will be passed to a
63212 ** user-defined function or returned to the user as the result of a query.
63213 ** This routine sets the pMem->type variable used by the sqlite3_value_*()
63214 ** routines.
@@ -63179,10 +63799,11 @@
63799 u8 *zEndHdr; /* Pointer to first byte after the header */
63800 u32 offset; /* Offset into the data */
63801 u32 szField; /* Number of bytes in the content of a field */
63802 int szHdr; /* Size of the header size field at start of record */
63803 int avail; /* Number of bytes of available data */
63804 u32 t; /* A type code from the record header */
63805 Mem *pReg; /* PseudoTable input register */
63806 } am;
63807 struct OP_Affinity_stack_vars {
63808 const char *zAffinity; /* The affinity to be applied */
63809 char cAff; /* A single character of affinity */
@@ -63337,11 +63958,10 @@
63958 BtCursor *pCrsr;
63959 int res;
63960 } bl;
63961 struct OP_Next_stack_vars {
63962 VdbeCursor *pC;
 
63963 int res;
63964 } bm;
63965 struct OP_IdxInsert_stack_vars {
63966 VdbeCursor *pC;
63967 BtCursor *pCrsr;
@@ -63591,11 +64211,11 @@
64211 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
64212 assert( pOp->p2>0 );
64213 assert( pOp->p2<=p->nMem );
64214 pOut = &aMem[pOp->p2];
64215 memAboutToChange(p, pOut);
64216 MemReleaseExt(pOut);
64217 pOut->flags = MEM_Int;
64218 }
64219
64220 /* Sanity checking on other operands */
64221 #ifdef SQLITE_DEBUG
@@ -65061,10 +65681,11 @@
65681 u8 *zEndHdr; /* Pointer to first byte after the header */
65682 u32 offset; /* Offset into the data */
65683 u32 szField; /* Number of bytes in the content of a field */
65684 int szHdr; /* Size of the header size field at start of record */
65685 int avail; /* Number of bytes of available data */
65686 u32 t; /* A type code from the record header */
65687 Mem *pReg; /* PseudoTable input register */
65688 #endif /* local variables moved into u.am */
65689
65690
65691 u.am.p1 = pOp->p1;
@@ -65073,11 +65694,10 @@
65694 memset(&u.am.sMem, 0, sizeof(u.am.sMem));
65695 assert( u.am.p1<p->nCursor );
65696 assert( pOp->p3>0 && pOp->p3<=p->nMem );
65697 u.am.pDest = &aMem[pOp->p3];
65698 memAboutToChange(p, u.am.pDest);
 
65699 u.am.zRec = 0;
65700
65701 /* This block sets the variable u.am.payloadSize to be the total number of
65702 ** bytes in the record.
65703 **
@@ -65117,11 +65737,11 @@
65737 }else{
65738 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65739 rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65740 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65741 }
65742 }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
65743 u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65744 assert( u.am.pReg->flags & MEM_Blob );
65745 assert( memIsValid(u.am.pReg) );
65746 u.am.payloadSize = u.am.pReg->n;
65747 u.am.zRec = u.am.pReg->z;
@@ -65130,13 +65750,14 @@
65750 }else{
65751 /* Consider the row to be NULL */
65752 u.am.payloadSize = 0;
65753 }
65754
65755 /* If u.am.payloadSize is 0, then just store a NULL. This can happen because of
65756 ** nullRow or because of a corrupt database. */
65757 if( u.am.payloadSize==0 ){
65758 MemSetTypeFlag(u.am.pDest, MEM_Null);
65759 goto op_column_out;
65760 }
65761 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65762 if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65763 goto too_big;
@@ -65239,12 +65860,18 @@
65860 ** of the record to the start of the data for the u.am.i-th column
65861 */
65862 for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
65863 if( u.am.zIdx<u.am.zEndHdr ){
65864 u.am.aOffset[u.am.i] = u.am.offset;
65865 if( u.am.zIdx[0]<0x80 ){
65866 u.am.t = u.am.zIdx[0];
65867 u.am.zIdx++;
65868 }else{
65869 u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t);
65870 }
65871 u.am.aType[u.am.i] = u.am.t;
65872 u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t);
65873 u.am.offset += u.am.szField;
65874 if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */
65875 u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
65876 break;
65877 }
@@ -65281,11 +65908,11 @@
65908 ** a pointer to a Mem object.
65909 */
65910 if( u.am.aOffset[u.am.p2] ){
65911 assert( rc==SQLITE_OK );
65912 if( u.am.zRec ){
65913 MemReleaseExt(u.am.pDest);
65914 sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
65915 }else{
65916 u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
65917 sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
65918 rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
@@ -65298,11 +65925,11 @@
65925 u.am.pDest->enc = encoding;
65926 }else{
65927 if( pOp->p4type==P4_MEM ){
65928 sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
65929 }else{
65930 MemSetTypeFlag(u.am.pDest, MEM_Null);
65931 }
65932 }
65933
65934 /* If we dynamically allocated space to hold the data (in the
65935 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
@@ -65500,11 +66127,11 @@
66127 i64 nEntry;
66128 BtCursor *pCrsr;
66129 #endif /* local variables moved into u.ap */
66130
66131 u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
66132 if( ALWAYS(u.ap.pCrsr) ){
66133 rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
66134 }else{
66135 u.ap.nEntry = 0;
66136 }
66137 pOut->u.i = u.ap.nEntry;
@@ -66076,19 +66703,13 @@
66703 u.aw.pCur->nullRow = 1;
66704 u.aw.pCur->isOrdered = 1;
66705 rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
66706 u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
66707
66708 /* Since it performs no memory allocation or IO, the only value that
66709 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
66710 assert( rc==SQLITE_OK );
 
 
 
 
 
 
66711
66712 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
66713 ** SQLite used to check if the root-page flags were sane at this point
66714 ** and report database corruption if they were not, but this check has
66715 ** since moved into the btree layer. */
@@ -66095,11 +66716,11 @@
66716 u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66717 u.aw.pCur->isIndex = !u.aw.pCur->isTable;
66718 break;
66719 }
66720
66721 /* Opcode: OpenEphemeral P1 P2 * P4 P5
66722 **
66723 ** Open a new cursor P1 to a transient table.
66724 ** The cursor is always opened read/write even if
66725 ** the main database is read-only. The ephemeral
66726 ** table is deleted automatically when the cursor is closed.
@@ -66112,18 +66733,30 @@
66733 ** This opcode was once called OpenTemp. But that created
66734 ** confusion because the term "temp table", might refer either
66735 ** to a TEMP table at the SQL level, or to a table opened by
66736 ** this opcode. Then this opcode was call OpenVirtual. But
66737 ** that created confusion with the whole virtual-table idea.
66738 **
66739 ** The P5 parameter can be a mask of the BTREE_* flags defined
66740 ** in btree.h. These flags control aspects of the operation of
66741 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
66742 ** added automatically.
66743 */
66744 /* Opcode: OpenAutoindex P1 P2 * P4 *
66745 **
66746 ** This opcode works the same as OP_OpenEphemeral. It has a
66747 ** different name to distinguish its use. Tables created using
66748 ** by this opcode will be used for automatically created transient
66749 ** indices in joins.
66750 */
66751 /* Opcode: OpenSorter P1 P2 * P4 *
66752 **
66753 ** This opcode works like OP_OpenEphemeral except that it opens
66754 ** a transient index that is specifically designed to sort large
66755 ** tables using an external merge-sort algorithm.
66756 */
66757 case OP_OpenSorter:
66758 case OP_OpenAutoindex:
66759 case OP_OpenEphemeral: {
66760 #if 0 /* local variables moved into u.ax */
66761 VdbeCursor *pCx;
66762 #endif /* local variables moved into u.ax */
@@ -66133,10 +66766,11 @@
66766 SQLITE_OPEN_EXCLUSIVE |
66767 SQLITE_OPEN_DELETEONCLOSE |
66768 SQLITE_OPEN_TRANSIENT_DB;
66769
66770 assert( pOp->p1>=0 );
66771 assert( (pOp->opcode==OP_OpenSorter)==((pOp->p5 & BTREE_SORTER)!=0) );
66772 u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66773 if( u.ax.pCx==0 ) goto no_mem;
66774 u.ax.pCx->nullRow = 1;
66775 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt,
66776 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
@@ -66166,10 +66800,15 @@
66800 u.ax.pCx->isTable = 1;
66801 }
66802 }
66803 u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66804 u.ax.pCx->isIndex = !u.ax.pCx->isTable;
66805 #ifndef SQLITE_OMIT_MERGE_SORT
66806 if( rc==SQLITE_OK && pOp->opcode==OP_OpenSorter ){
66807 rc = sqlite3VdbeSorterInit(db, u.ax.pCx);
66808 }
66809 #endif
66810 break;
66811 }
66812
66813 /* Opcode: OpenPseudo P1 P2 P3 * *
66814 **
@@ -66285,11 +66924,11 @@
66924 assert( u.az.pC->pseudoTableReg==0 );
66925 assert( OP_SeekLe == OP_SeekLt+1 );
66926 assert( OP_SeekGe == OP_SeekLt+2 );
66927 assert( OP_SeekGt == OP_SeekLt+3 );
66928 assert( u.az.pC->isOrdered );
66929 if( ALWAYS(u.az.pC->pCursor!=0) ){
66930 u.az.oc = pOp->opcode;
66931 u.az.pC->nullRow = 0;
66932 if( u.az.pC->isTable ){
66933 /* The input value in P3 might be of any type: integer, real, string,
66934 ** blob, or NULL. But it needs to be an integer before we can do
@@ -66651,11 +67290,11 @@
67290 u.bd.pC = p->apCsr[pOp->p1];
67291 assert( u.bd.pC!=0 );
67292 assert( u.bd.pC->isTable );
67293 assert( u.bd.pC->pseudoTableReg==0 );
67294 u.bd.pCrsr = u.bd.pC->pCursor;
67295 if( ALWAYS(u.bd.pCrsr!=0) ){
67296 u.bd.res = 0;
67297 u.bd.iKey = pIn3->u.i;
67298 rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res);
67299 u.bd.pC->lastRowid = pIn3->u.i;
67300 u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0;
@@ -67075,10 +67714,17 @@
67714 assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey );
67715 assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData );
67716 assert( u.bh.pC!=0 );
67717 assert( u.bh.pC->nullRow==0 );
67718 assert( u.bh.pC->pseudoTableReg==0 );
67719
67720 if( isSorter(u.bh.pC) ){
67721 assert( pOp->opcode==OP_RowKey );
67722 rc = sqlite3VdbeSorterRowkey(u.bh.pC, pOut);
67723 break;
67724 }
67725
67726 assert( u.bh.pC->pCursor!=0 );
67727 u.bh.pCrsr = u.bh.pC->pCursor;
67728 assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) );
67729
67730 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
@@ -67183,10 +67829,11 @@
67829 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67830 u.bj.pC = p->apCsr[pOp->p1];
67831 assert( u.bj.pC!=0 );
67832 u.bj.pC->nullRow = 1;
67833 u.bj.pC->rowidIsValid = 0;
67834 assert( u.bj.pC->pCursor || u.bj.pC->pVtabCursor );
67835 if( u.bj.pC->pCursor ){
67836 sqlite3BtreeClearCursor(u.bj.pC->pCursor);
67837 }
67838 break;
67839 }
@@ -67208,11 +67855,11 @@
67855
67856 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67857 u.bk.pC = p->apCsr[pOp->p1];
67858 assert( u.bk.pC!=0 );
67859 u.bk.pCrsr = u.bk.pC->pCursor;
67860 if( NEVER(u.bk.pCrsr==0) ){
67861 u.bk.res = 1;
67862 }else{
67863 rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
67864 }
67865 u.bk.pC->nullRow = (u8)u.bk.res;
@@ -67263,11 +67910,15 @@
67910
67911 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67912 u.bl.pC = p->apCsr[pOp->p1];
67913 assert( u.bl.pC!=0 );
67914 u.bl.res = 1;
67915 if( isSorter(u.bl.pC) ){
67916 rc = sqlite3VdbeSorterRewind(db, u.bl.pC, &u.bl.res);
67917 }else{
67918 u.bl.pCrsr = u.bl.pC->pCursor;
67919 assert( u.bl.pCrsr );
67920 rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res);
67921 u.bl.pC->atFirst = u.bl.res==0 ?1:0;
67922 u.bl.pC->deferredMoveto = 0;
67923 u.bl.pC->cacheStatus = CACHE_STALE;
67924 u.bl.pC->rowidIsValid = 0;
@@ -67278,18 +67929,21 @@
67929 pc = pOp->p2 - 1;
67930 }
67931 break;
67932 }
67933
67934 /* Opcode: Next P1 P2 * P4 P5
67935 **
67936 ** Advance cursor P1 so that it points to the next key/data pair in its
67937 ** table or index. If there are no more key/value pairs then fall through
67938 ** to the following instruction. But if the cursor advance was successful,
67939 ** jump immediately to P2.
67940 **
67941 ** The P1 cursor must be for a real table, not a pseudo-table.
67942 **
67943 ** P4 is always of type P4_ADVANCE. The function pointer points to
67944 ** sqlite3BtreeNext().
67945 **
67946 ** If P5 is positive and the jump is taken, then event counter
67947 ** number P5-1 in the prepared statement is incremented.
67948 **
67949 ** See also: Prev
@@ -67300,19 +67954,21 @@
67954 ** table or index. If there is no previous key/value pairs then fall through
67955 ** to the following instruction. But if the cursor backup was successful,
67956 ** jump immediately to P2.
67957 **
67958 ** The P1 cursor must be for a real table, not a pseudo-table.
67959 **
67960 ** P4 is always of type P4_ADVANCE. The function pointer points to
67961 ** sqlite3BtreePrevious().
67962 **
67963 ** If P5 is positive and the jump is taken, then event counter
67964 ** number P5-1 in the prepared statement is incremented.
67965 */
67966 case OP_Prev: /* jump */
67967 case OP_Next: { /* jump */
67968 #if 0 /* local variables moved into u.bm */
67969 VdbeCursor *pC;
 
67970 int res;
67971 #endif /* local variables moved into u.bm */
67972
67973 CHECK_FOR_INTERRUPT;
67974 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
@@ -67319,19 +67975,21 @@
67975 assert( pOp->p5<=ArraySize(p->aCounter) );
67976 u.bm.pC = p->apCsr[pOp->p1];
67977 if( u.bm.pC==0 ){
67978 break; /* See ticket #2273 */
67979 }
67980 if( isSorter(u.bm.pC) ){
67981 assert( pOp->opcode==OP_Next );
67982 rc = sqlite3VdbeSorterNext(db, u.bm.pC, &u.bm.res);
67983 }else{
67984 u.bm.res = 1;
67985 assert( u.bm.pC->deferredMoveto==0 );
67986 assert( u.bm.pC->pCursor );
67987 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
67988 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
67989 rc = pOp->p4.xAdvance(u.bm.pC->pCursor, &u.bm.res);
67990 }
67991 u.bm.pC->nullRow = (u8)u.bm.res;
67992 u.bm.pC->cacheStatus = CACHE_STALE;
67993 if( u.bm.res==0 ){
67994 pc = pOp->p2 - 1;
67995 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
@@ -67373,14 +68031,17 @@
68031 assert( u.bn.pC->isTable==0 );
68032 rc = ExpandBlob(pIn2);
68033 if( rc==SQLITE_OK ){
68034 u.bn.nKey = pIn2->n;
68035 u.bn.zKey = pIn2->z;
68036 rc = sqlite3VdbeSorterWrite(db, u.bn.pC, u.bn.nKey);
68037 if( rc==SQLITE_OK ){
68038 rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
68039 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
68040 );
68041 assert( u.bn.pC->deferredMoveto==0 );
68042 }
68043 u.bn.pC->cacheStatus = CACHE_STALE;
68044 }
68045 }
68046 break;
68047 }
@@ -69556,10 +70217,722 @@
70217 }
70218
70219 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
70220
70221 /************** End of vdbeblob.c ********************************************/
70222 /************** Begin file vdbesort.c ****************************************/
70223 /*
70224 ** 2011 July 9
70225 **
70226 ** The author disclaims copyright to this source code. In place of
70227 ** a legal notice, here is a blessing:
70228 **
70229 ** May you do good and not evil.
70230 ** May you find forgiveness for yourself and forgive others.
70231 ** May you share freely, never taking more than you give.
70232 **
70233 *************************************************************************
70234 ** This file contains code for the VdbeSorter object, used in concert with
70235 ** a VdbeCursor to sort large numbers of keys (as may be required, for
70236 ** example, by CREATE INDEX statements on tables too large to fit in main
70237 ** memory).
70238 */
70239
70240
70241 #ifndef SQLITE_OMIT_MERGE_SORT
70242
70243 typedef struct VdbeSorterIter VdbeSorterIter;
70244
70245 /*
70246 ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
70247 **
70248 ** As keys are added to the sorter, they are written to disk in a series
70249 ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
70250 ** the same as the cache-size allowed for temporary databases. In order
70251 ** to allow the caller to extract keys from the sorter in sorted order,
70252 ** all PMAs currently stored on disk must be merged together. This comment
70253 ** describes the data structure used to do so. The structure supports
70254 ** merging any number of arrays in a single pass with no redundant comparison
70255 ** operations.
70256 **
70257 ** The aIter[] array contains an iterator for each of the PMAs being merged.
70258 ** An aIter[] iterator either points to a valid key or else is at EOF. For
70259 ** the purposes of the paragraphs below, we assume that the array is actually
70260 ** N elements in size, where N is the smallest power of 2 greater to or equal
70261 ** to the number of iterators being merged. The extra aIter[] elements are
70262 ** treated as if they are empty (always at EOF).
70263 **
70264 ** The aTree[] array is also N elements in size. The value of N is stored in
70265 ** the VdbeSorter.nTree variable.
70266 **
70267 ** The final (N/2) elements of aTree[] contain the results of comparing
70268 ** pairs of iterator keys together. Element i contains the result of
70269 ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
70270 ** aTree element is set to the index of it.
70271 **
70272 ** For the purposes of this comparison, EOF is considered greater than any
70273 ** other key value. If the keys are equal (only possible with two EOF
70274 ** values), it doesn't matter which index is stored.
70275 **
70276 ** The (N/4) elements of aTree[] that preceed the final (N/2) described
70277 ** above contains the index of the smallest of each block of 4 iterators.
70278 ** And so on. So that aTree[1] contains the index of the iterator that
70279 ** currently points to the smallest key value. aTree[0] is unused.
70280 **
70281 ** Example:
70282 **
70283 ** aIter[0] -> Banana
70284 ** aIter[1] -> Feijoa
70285 ** aIter[2] -> Elderberry
70286 ** aIter[3] -> Currant
70287 ** aIter[4] -> Grapefruit
70288 ** aIter[5] -> Apple
70289 ** aIter[6] -> Durian
70290 ** aIter[7] -> EOF
70291 **
70292 ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
70293 **
70294 ** The current element is "Apple" (the value of the key indicated by
70295 ** iterator 5). When the Next() operation is invoked, iterator 5 will
70296 ** be advanced to the next key in its segment. Say the next key is
70297 ** "Eggplant":
70298 **
70299 ** aIter[5] -> Eggplant
70300 **
70301 ** The contents of aTree[] are updated first by comparing the new iterator
70302 ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
70303 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
70304 ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
70305 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
70306 ** so the value written into element 1 of the array is 0. As follows:
70307 **
70308 ** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
70309 **
70310 ** In other words, each time we advance to the next sorter element, log2(N)
70311 ** key comparison operations are required, where N is the number of segments
70312 ** being merged (rounded up to the next power of 2).
70313 */
70314 struct VdbeSorter {
70315 int nWorking; /* Start a new b-tree after this many pages */
70316 int nBtree; /* Current size of b-tree contents as PMA */
70317 int nTree; /* Used size of aTree/aIter (power of 2) */
70318 VdbeSorterIter *aIter; /* Array of iterators to merge */
70319 int *aTree; /* Current state of incremental merge */
70320 i64 iWriteOff; /* Current write offset within file pTemp1 */
70321 i64 iReadOff; /* Current read offset within file pTemp1 */
70322 sqlite3_file *pTemp1; /* PMA file 1 */
70323 int nPMA; /* Number of PMAs stored in pTemp1 */
70324 };
70325
70326 /*
70327 ** The following type is an iterator for a PMA. It caches the current key in
70328 ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
70329 */
70330 struct VdbeSorterIter {
70331 i64 iReadOff; /* Current read offset */
70332 i64 iEof; /* 1 byte past EOF for this iterator */
70333 sqlite3_file *pFile; /* File iterator is reading from */
70334 int nAlloc; /* Bytes of space at aAlloc */
70335 u8 *aAlloc; /* Allocated space */
70336 int nKey; /* Number of bytes in key */
70337 u8 *aKey; /* Pointer to current key */
70338 };
70339
70340 /* Minimum allowable value for the VdbeSorter.nWorking variable */
70341 #define SORTER_MIN_WORKING 10
70342
70343 /* Maximum number of segments to merge in a single pass. */
70344 #define SORTER_MAX_MERGE_COUNT 16
70345
70346 /*
70347 ** Free all memory belonging to the VdbeSorterIter object passed as the second
70348 ** argument. All structure fields are set to zero before returning.
70349 */
70350 static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
70351 sqlite3DbFree(db, pIter->aAlloc);
70352 memset(pIter, 0, sizeof(VdbeSorterIter));
70353 }
70354
70355 /*
70356 ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
70357 ** no error occurs, or an SQLite error code if one does.
70358 */
70359 static int vdbeSorterIterNext(
70360 sqlite3 *db, /* Database handle (for sqlite3DbMalloc() ) */
70361 VdbeSorterIter *pIter /* Iterator to advance */
70362 ){
70363 int rc; /* Return Code */
70364 int nRead; /* Number of bytes read */
70365 int nRec; /* Size of record in bytes */
70366 int iOff; /* Size of serialized size varint in bytes */
70367
70368 nRead = pIter->iEof - pIter->iReadOff;
70369 if( nRead>5 ) nRead = 5;
70370 if( nRead<=0 ){
70371 /* This is an EOF condition */
70372 vdbeSorterIterZero(db, pIter);
70373 return SQLITE_OK;
70374 }
70375
70376 rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
70377 iOff = getVarint32(pIter->aAlloc, nRec);
70378
70379 if( rc==SQLITE_OK && (iOff+nRec)>nRead ){
70380 int nRead2; /* Number of extra bytes to read */
70381 if( (iOff+nRec)>pIter->nAlloc ){
70382 int nNew = pIter->nAlloc*2;
70383 while( (iOff+nRec)>nNew ) nNew = nNew*2;
70384 pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
70385 if( !pIter->aAlloc ) return SQLITE_NOMEM;
70386 pIter->nAlloc = nNew;
70387 }
70388
70389 nRead2 = iOff + nRec - nRead;
70390 rc = sqlite3OsRead(
70391 pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
70392 );
70393 }
70394
70395 assert( nRec>0 || rc!=SQLITE_OK );
70396 pIter->iReadOff += iOff+nRec;
70397 pIter->nKey = nRec;
70398 pIter->aKey = &pIter->aAlloc[iOff];
70399 return rc;
70400 }
70401
70402 /*
70403 ** Write a single varint, value iVal, to file-descriptor pFile. Return
70404 ** SQLITE_OK if successful, or an SQLite error code if some error occurs.
70405 **
70406 ** The value of *piOffset when this function is called is used as the byte
70407 ** offset in file pFile to write to. Before returning, *piOffset is
70408 ** incremented by the number of bytes written.
70409 */
70410 static int vdbeSorterWriteVarint(
70411 sqlite3_file *pFile, /* File to write to */
70412 i64 iVal, /* Value to write as a varint */
70413 i64 *piOffset /* IN/OUT: Write offset in file pFile */
70414 ){
70415 u8 aVarint[9]; /* Buffer large enough for a varint */
70416 int nVarint; /* Number of used bytes in varint */
70417 int rc; /* Result of write() call */
70418
70419 nVarint = sqlite3PutVarint(aVarint, iVal);
70420 rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
70421 *piOffset += nVarint;
70422
70423 return rc;
70424 }
70425
70426 /*
70427 ** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
70428 ** successful, or an SQLite error code if some error occurs.
70429 **
70430 ** The value of *piOffset when this function is called is used as the
70431 ** byte offset in file pFile from whence to read the varint. If successful
70432 ** (i.e. if no IO error occurs), then *piOffset is set to the offset of
70433 ** the first byte past the end of the varint before returning. *piVal is
70434 ** set to the integer value read. If an error occurs, the final values of
70435 ** both *piOffset and *piVal are undefined.
70436 */
70437 static int vdbeSorterReadVarint(
70438 sqlite3_file *pFile, /* File to read from */
70439 i64 iEof, /* Total number of bytes in file */
70440 i64 *piOffset, /* IN/OUT: Read offset in pFile */
70441 i64 *piVal /* OUT: Value read from file */
70442 ){
70443 u8 aVarint[9]; /* Buffer large enough for a varint */
70444 i64 iOff = *piOffset; /* Offset in file to read from */
70445 int nRead = 9; /* Number of bytes to read from file */
70446 int rc; /* Return code */
70447
70448 assert( iEof>iOff );
70449 if( (iEof-iOff)<nRead ){
70450 nRead = iEof-iOff;
70451 }
70452
70453 rc = sqlite3OsRead(pFile, aVarint, nRead, iOff);
70454 if( rc==SQLITE_OK ){
70455 *piOffset += getVarint(aVarint, (u64 *)piVal);
70456 }
70457
70458 return rc;
70459 }
70460
70461 /*
70462 ** Initialize iterator pIter to scan through the PMA stored in file pFile
70463 ** starting at offset iStart and ending at offset iEof-1. This function
70464 ** leaves the iterator pointing to the first key in the PMA (or EOF if the
70465 ** PMA is empty).
70466 */
70467 static int vdbeSorterIterInit(
70468 sqlite3 *db, /* Database handle */
70469 VdbeSorter *pSorter, /* Sorter object */
70470 i64 iStart, /* Start offset in pFile */
70471 VdbeSorterIter *pIter, /* Iterator to populate */
70472 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
70473 ){
70474 int rc;
70475
70476 assert( pSorter->iWriteOff>iStart );
70477 assert( pIter->aAlloc==0 );
70478 pIter->pFile = pSorter->pTemp1;
70479 pIter->iReadOff = iStart;
70480 pIter->nAlloc = 128;
70481 pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
70482 if( !pIter->aAlloc ){
70483 rc = SQLITE_NOMEM;
70484 }else{
70485 i64 iEof = pSorter->iWriteOff; /* EOF of file pSorter->pTemp1 */
70486 i64 nByte; /* Total size of PMA in bytes */
70487 rc = vdbeSorterReadVarint(pSorter->pTemp1, iEof, &pIter->iReadOff, &nByte);
70488 *pnByte += nByte;
70489 pIter->iEof = pIter->iReadOff + nByte;
70490 }
70491 if( rc==SQLITE_OK ){
70492 rc = vdbeSorterIterNext(db, pIter);
70493 }
70494 return rc;
70495 }
70496
70497 /*
70498 ** This function is called to compare two iterator keys when merging
70499 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
70500 ** value to recalculate.
70501 */
70502 static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
70503 VdbeSorter *pSorter = pCsr->pSorter;
70504 int i1;
70505 int i2;
70506 int iRes;
70507 VdbeSorterIter *p1;
70508 VdbeSorterIter *p2;
70509
70510 assert( iOut<pSorter->nTree && iOut>0 );
70511
70512 if( iOut>=(pSorter->nTree/2) ){
70513 i1 = (iOut - pSorter->nTree/2) * 2;
70514 i2 = i1 + 1;
70515 }else{
70516 i1 = pSorter->aTree[iOut*2];
70517 i2 = pSorter->aTree[iOut*2+1];
70518 }
70519
70520 p1 = &pSorter->aIter[i1];
70521 p2 = &pSorter->aIter[i2];
70522
70523 if( p1->pFile==0 ){
70524 iRes = i2;
70525 }else if( p2->pFile==0 ){
70526 iRes = i1;
70527 }else{
70528 char aSpace[150];
70529 UnpackedRecord *r1;
70530
70531 r1 = sqlite3VdbeRecordUnpack(
70532 pCsr->pKeyInfo, p1->nKey, p1->aKey, aSpace, sizeof(aSpace)
70533 );
70534 if( r1==0 ) return SQLITE_NOMEM;
70535
70536 if( sqlite3VdbeRecordCompare(p2->nKey, p2->aKey, r1)>=0 ){
70537 iRes = i1;
70538 }else{
70539 iRes = i2;
70540 }
70541 sqlite3VdbeDeleteUnpackedRecord(r1);
70542 }
70543
70544 pSorter->aTree[iOut] = iRes;
70545 return SQLITE_OK;
70546 }
70547
70548 /*
70549 ** Initialize the temporary index cursor just opened as a sorter cursor.
70550 */
70551 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
70552 assert( pCsr->pKeyInfo && pCsr->pBt );
70553 pCsr->pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
70554 return (pCsr->pSorter ? SQLITE_OK : SQLITE_NOMEM);
70555 }
70556
70557 /*
70558 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
70559 */
70560 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
70561 VdbeSorter *pSorter = pCsr->pSorter;
70562 if( pSorter ){
70563 if( pSorter->aIter ){
70564 int i;
70565 for(i=0; i<pSorter->nTree; i++){
70566 vdbeSorterIterZero(db, &pSorter->aIter[i]);
70567 }
70568 sqlite3DbFree(db, pSorter->aIter);
70569 }
70570 if( pSorter->pTemp1 ){
70571 sqlite3OsCloseFree(pSorter->pTemp1);
70572 }
70573 sqlite3DbFree(db, pSorter);
70574 pCsr->pSorter = 0;
70575 }
70576 }
70577
70578 /*
70579 ** Allocate space for a file-handle and open a temporary file. If successful,
70580 ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
70581 ** Otherwise, set *ppFile to 0 and return an SQLite error code.
70582 */
70583 static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
70584 int dummy;
70585 return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
70586 SQLITE_OPEN_TEMP_JOURNAL |
70587 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
70588 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &dummy
70589 );
70590 }
70591
70592
70593 /*
70594 ** Write the current contents of the b-tree to a PMA. Return SQLITE_OK
70595 ** if successful, or an SQLite error code otherwise.
70596 **
70597 ** The format of a PMA is:
70598 **
70599 ** * A varint. This varint contains the total number of bytes of content
70600 ** in the PMA (not including the varint itself).
70601 **
70602 ** * One or more records packed end-to-end in order of ascending keys.
70603 ** Each record consists of a varint followed by a blob of data (the
70604 ** key). The varint is the number of bytes in the blob of data.
70605 */
70606 static int vdbeSorterBtreeToPMA(sqlite3 *db, VdbeCursor *pCsr){
70607 int rc = SQLITE_OK; /* Return code */
70608 VdbeSorter *pSorter = pCsr->pSorter;
70609 int res = 0;
70610
70611 /* sqlite3BtreeFirst() cannot fail because sorter btrees are always held
70612 ** in memory and so an I/O error is not possible. */
70613 rc = sqlite3BtreeFirst(pCsr->pCursor, &res);
70614 if( NEVER(rc!=SQLITE_OK) || res ) return rc;
70615 assert( pSorter->nBtree>0 );
70616
70617 /* If the first temporary PMA file has not been opened, open it now. */
70618 if( pSorter->pTemp1==0 ){
70619 rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
70620 assert( rc!=SQLITE_OK || pSorter->pTemp1 );
70621 assert( pSorter->iWriteOff==0 );
70622 assert( pSorter->nPMA==0 );
70623 }
70624
70625 if( rc==SQLITE_OK ){
70626 i64 iWriteOff = pSorter->iWriteOff;
70627 void *aMalloc = 0; /* Array used to hold a single record */
70628 int nMalloc = 0; /* Allocated size of aMalloc[] in bytes */
70629
70630 pSorter->nPMA++;
70631 for(
70632 rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nBtree, &iWriteOff);
70633 rc==SQLITE_OK && res==0;
70634 rc = sqlite3BtreeNext(pCsr->pCursor, &res)
70635 ){
70636 i64 nKey; /* Size of this key in bytes */
70637
70638 /* Write the size of the record in bytes to the output file */
70639 (void)sqlite3BtreeKeySize(pCsr->pCursor, &nKey);
70640 rc = vdbeSorterWriteVarint(pSorter->pTemp1, nKey, &iWriteOff);
70641
70642 /* Make sure the aMalloc[] buffer is large enough for the record */
70643 if( rc==SQLITE_OK && nKey>nMalloc ){
70644 aMalloc = sqlite3DbReallocOrFree(db, aMalloc, nKey);
70645 if( !aMalloc ){
70646 rc = SQLITE_NOMEM;
70647 }else{
70648 nMalloc = nKey;
70649 }
70650 }
70651
70652 /* Write the record itself to the output file */
70653 if( rc==SQLITE_OK ){
70654 /* sqlite3BtreeKey() cannot fail because sorter btrees held in memory */
70655 rc = sqlite3BtreeKey(pCsr->pCursor, 0, nKey, aMalloc);
70656 if( ALWAYS(rc==SQLITE_OK) ){
70657 rc = sqlite3OsWrite(pSorter->pTemp1, aMalloc, nKey, iWriteOff);
70658 iWriteOff += nKey;
70659 }
70660 }
70661
70662 if( rc!=SQLITE_OK ) break;
70663 }
70664
70665 /* This assert verifies that unless an error has occurred, the size of
70666 ** the PMA on disk is the same as the expected size stored in
70667 ** pSorter->nBtree. */
70668 assert( rc!=SQLITE_OK || pSorter->nBtree==(
70669 iWriteOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nBtree)
70670 ));
70671
70672 pSorter->iWriteOff = iWriteOff;
70673 sqlite3DbFree(db, aMalloc);
70674 }
70675
70676 pSorter->nBtree = 0;
70677 return rc;
70678 }
70679
70680 /*
70681 ** This function is called on a sorter cursor by the VDBE before each row
70682 ** is inserted into VdbeCursor.pCsr. Argument nKey is the size of the key, in
70683 ** bytes, about to be inserted.
70684 **
70685 ** If it is determined that the temporary b-tree accessed via VdbeCursor.pCsr
70686 ** is large enough, its contents are written to a sorted PMA on disk and the
70687 ** tree emptied. This prevents the b-tree (which must be small enough to
70688 ** fit entirely in the cache in order to support efficient inserts) from
70689 ** growing too large.
70690 **
70691 ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
70692 */
70693 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *db, VdbeCursor *pCsr, int nKey){
70694 int rc = SQLITE_OK; /* Return code */
70695 VdbeSorter *pSorter = pCsr->pSorter;
70696 if( pSorter ){
70697 Pager *pPager = sqlite3BtreePager(pCsr->pBt);
70698 int nPage; /* Current size of temporary file in pages */
70699
70700 /* Sorters never spill to disk */
70701 assert( sqlite3PagerFile(pPager)->pMethods==0 );
70702
70703 /* Determine how many pages the temporary b-tree has grown to */
70704 sqlite3PagerPagecount(pPager, &nPage);
70705
70706 /* If pSorter->nWorking is still zero, but the temporary file has been
70707 ** created in the file-system, then the most recent insert into the
70708 ** current b-tree segment probably caused the cache to overflow (it is
70709 ** also possible that sqlite3_release_memory() was called). So set the
70710 ** size of the working set to a little less than the current size of the
70711 ** file in pages. */
70712 if( pSorter->nWorking==0 && sqlite3PagerUnderStress(pPager) ){
70713 pSorter->nWorking = nPage-5;
70714 if( pSorter->nWorking<SORTER_MIN_WORKING ){
70715 pSorter->nWorking = SORTER_MIN_WORKING;
70716 }
70717 }
70718
70719 /* If the number of pages used by the current b-tree segment is greater
70720 ** than the size of the working set (VdbeSorter.nWorking), start a new
70721 ** segment b-tree. */
70722 if( pSorter->nWorking && nPage>=pSorter->nWorking ){
70723 BtCursor *p = pCsr->pCursor;/* Cursor structure to close and reopen */
70724 int iRoot; /* Root page of new tree */
70725
70726 /* Copy the current contents of the b-tree into a PMA in sorted order.
70727 ** Close the currently open b-tree cursor. */
70728 rc = vdbeSorterBtreeToPMA(db, pCsr);
70729 sqlite3BtreeCloseCursor(p);
70730
70731 if( rc==SQLITE_OK ){
70732 rc = sqlite3BtreeDropTable(pCsr->pBt, 2, 0);
70733 #ifdef SQLITE_DEBUG
70734 sqlite3PagerPagecount(pPager, &nPage);
70735 assert( rc!=SQLITE_OK || nPage==1 );
70736 #endif
70737 }
70738 if( rc==SQLITE_OK ){
70739 rc = sqlite3BtreeCreateTable(pCsr->pBt, &iRoot, BTREE_BLOBKEY);
70740 }
70741 if( rc==SQLITE_OK ){
70742 assert( iRoot==2 );
70743 rc = sqlite3BtreeCursor(pCsr->pBt, iRoot, 1, pCsr->pKeyInfo, p);
70744 }
70745 }
70746
70747 pSorter->nBtree += sqlite3VarintLen(nKey) + nKey;
70748 }
70749 return rc;
70750 }
70751
70752 /*
70753 ** Helper function for sqlite3VdbeSorterRewind().
70754 */
70755 static int vdbeSorterInitMerge(
70756 sqlite3 *db, /* Database handle */
70757 VdbeCursor *pCsr, /* Cursor handle for this sorter */
70758 i64 *pnByte /* Sum of bytes in all opened PMAs */
70759 ){
70760 VdbeSorter *pSorter = pCsr->pSorter;
70761 int rc = SQLITE_OK; /* Return code */
70762 int i; /* Used to iterator through aIter[] */
70763 i64 nByte = 0; /* Total bytes in all opened PMAs */
70764
70765 /* Initialize the iterators. */
70766 for(i=0; rc==SQLITE_OK && i<SORTER_MAX_MERGE_COUNT; i++){
70767 VdbeSorterIter *pIter = &pSorter->aIter[i];
70768 rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
70769 pSorter->iReadOff = pIter->iEof;
70770 assert( pSorter->iReadOff<=pSorter->iWriteOff || rc!=SQLITE_OK );
70771 if( pSorter->iReadOff>=pSorter->iWriteOff ) break;
70772 }
70773
70774 /* Initialize the aTree[] array. */
70775 for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
70776 rc = vdbeSorterDoCompare(pCsr, i);
70777 }
70778
70779 *pnByte = nByte;
70780 return rc;
70781 }
70782
70783 /*
70784 ** Once the sorter has been populated, this function is called to prepare
70785 ** for iterating through its contents in sorted order.
70786 */
70787 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
70788 VdbeSorter *pSorter = pCsr->pSorter;
70789 int rc; /* Return code */
70790 sqlite3_file *pTemp2 = 0; /* Second temp file to use */
70791 i64 iWrite2 = 0; /* Write offset for pTemp2 */
70792 int nIter; /* Number of iterators used */
70793 int nByte; /* Bytes of space required for aIter/aTree */
70794 int N = 2; /* Power of 2 >= nIter */
70795
70796 assert( pSorter );
70797
70798 /* Write the current b-tree to a PMA. Close the b-tree cursor. */
70799 rc = vdbeSorterBtreeToPMA(db, pCsr);
70800 sqlite3BtreeCloseCursor(pCsr->pCursor);
70801 if( rc!=SQLITE_OK ) return rc;
70802 if( pSorter->nPMA==0 ){
70803 *pbEof = 1;
70804 return SQLITE_OK;
70805 }
70806
70807 /* Allocate space for aIter[] and aTree[]. */
70808 nIter = pSorter->nPMA;
70809 if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
70810 assert( nIter>0 );
70811 while( N<nIter ) N += N;
70812 nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
70813 pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
70814 if( !pSorter->aIter ) return SQLITE_NOMEM;
70815 pSorter->aTree = (int *)&pSorter->aIter[N];
70816 pSorter->nTree = N;
70817
70818 do {
70819 int iNew; /* Index of new, merged, PMA */
70820
70821 for(iNew=0;
70822 rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA;
70823 iNew++
70824 ){
70825 i64 nWrite; /* Number of bytes in new PMA */
70826
70827 /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
70828 ** initialize an iterator for each of them and break out of the loop.
70829 ** These iterators will be incrementally merged as the VDBE layer calls
70830 ** sqlite3VdbeSorterNext().
70831 **
70832 ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
70833 ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
70834 ** are merged into a single PMA that is written to file pTemp2.
70835 */
70836 rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
70837 assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
70838 if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
70839 break;
70840 }
70841
70842 /* Open the second temp file, if it is not already open. */
70843 if( pTemp2==0 ){
70844 assert( iWrite2==0 );
70845 rc = vdbeSorterOpenTempFile(db, &pTemp2);
70846 }
70847
70848 if( rc==SQLITE_OK ){
70849 rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
70850 }
70851
70852 if( rc==SQLITE_OK ){
70853 int bEof = 0;
70854 while( rc==SQLITE_OK && bEof==0 ){
70855 int nToWrite;
70856 VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
70857 assert( pIter->pFile );
70858 nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
70859 rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
70860 iWrite2 += nToWrite;
70861 if( rc==SQLITE_OK ){
70862 rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
70863 }
70864 }
70865 }
70866 }
70867
70868 if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
70869 break;
70870 }else{
70871 sqlite3_file *pTmp = pSorter->pTemp1;
70872 pSorter->nPMA = iNew;
70873 pSorter->pTemp1 = pTemp2;
70874 pTemp2 = pTmp;
70875 pSorter->iWriteOff = iWrite2;
70876 pSorter->iReadOff = 0;
70877 iWrite2 = 0;
70878 }
70879 }while( rc==SQLITE_OK );
70880
70881 if( pTemp2 ){
70882 sqlite3OsCloseFree(pTemp2);
70883 }
70884 *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
70885 return rc;
70886 }
70887
70888 /*
70889 ** Advance to the next element in the sorter.
70890 */
70891 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
70892 VdbeSorter *pSorter = pCsr->pSorter;
70893 int iPrev = pSorter->aTree[1]; /* Index of iterator to advance */
70894 int i; /* Index of aTree[] to recalculate */
70895 int rc; /* Return code */
70896
70897 rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
70898 for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
70899 rc = vdbeSorterDoCompare(pCsr, i);
70900 }
70901
70902 *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
70903 return rc;
70904 }
70905
70906 /*
70907 ** Copy the current sorter key into the memory cell pOut.
70908 */
70909 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
70910 VdbeSorter *pSorter = pCsr->pSorter;
70911 VdbeSorterIter *pIter;
70912
70913 pIter = &pSorter->aIter[ pSorter->aTree[1] ];
70914
70915 /* Coverage testing note: As things are currently, this call will always
70916 ** succeed. This is because the memory cell passed by the VDBE layer
70917 ** happens to be the same one as was used to assemble the keys before they
70918 ** were passed to the sorter - meaning it is always large enough for the
70919 ** largest key. But this could change very easily, so we leave the call
70920 ** to sqlite3VdbeMemGrow() in. */
70921 if( NEVER(sqlite3VdbeMemGrow(pOut, pIter->nKey, 0)) ){
70922 return SQLITE_NOMEM;
70923 }
70924 pOut->n = pIter->nKey;
70925 MemSetTypeFlag(pOut, MEM_Blob);
70926 memcpy(pOut->z, pIter->aKey, pIter->nKey);
70927
70928 return SQLITE_OK;
70929 }
70930
70931 #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
70932
70933 /************** End of vdbesort.c ********************************************/
70934 /************** Begin file journal.c *****************************************/
70935 /*
70936 ** 2007 August 22
70937 **
70938 ** The author disclaims copyright to this source code. In place of
@@ -70072,10 +71445,12 @@
71445 **
71446 *************************************************************************
71447 ** This file contains routines used for walking the parser tree for
71448 ** an SQL statement.
71449 */
71450 /* #include <stdlib.h> */
71451 /* #include <string.h> */
71452
71453
71454 /*
71455 ** Walk an expression tree. Invoke the callback once for each node
71456 ** of the expression, while decending. (In other words, the callback
@@ -70210,10 +71585,12 @@
71585 **
71586 ** This file contains routines used for walking the parser tree and
71587 ** resolve all identifiers by associating them with a particular
71588 ** table and column.
71589 */
71590 /* #include <stdlib.h> */
71591 /* #include <string.h> */
71592
71593 /*
71594 ** Turn the pExpr expression into an alias for the iCol-th column of the
71595 ** result set in pEList.
71596 **
@@ -76012,100 +77389,10 @@
77389 ** May you find forgiveness for yourself and forgive others.
77390 ** May you share freely, never taking more than you give.
77391 **
77392 *************************************************************************
77393 ** This file contains code associated with the ANALYZE command.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77394 */
77395 #ifndef SQLITE_OMIT_ANALYZE
77396
77397 /*
77398 ** This routine generates code that opens the sqlite_stat1 table for
@@ -76133,18 +77420,12 @@
77420 static const struct {
77421 const char *zName;
77422 const char *zCols;
77423 } aTable[] = {
77424 { "sqlite_stat1", "tbl,idx,stat" },
77425 #ifdef SQLITE_ENABLE_STAT2
77426 { "sqlite_stat2", "tbl,idx,sampleno,sample" },
 
 
 
 
 
 
77427 #endif
77428 };
77429
77430 int aRoot[] = {0, 0};
77431 u8 aCreateTbl[] = {0, 0};
@@ -76156,21 +77437,10 @@
77437 if( v==0 ) return;
77438 assert( sqlite3BtreeHoldsAllMutexes(db) );
77439 assert( sqlite3VdbeDb(v)==db );
77440 pDb = &db->aDb[iDb];
77441
 
 
 
 
 
 
 
 
 
 
 
77442 for(i=0; i<ArraySize(aTable); i++){
77443 const char *zTab = aTable[i].zName;
77444 Table *pStat;
77445 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
77446 /* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -76197,238 +77467,17 @@
77467 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
77468 }
77469 }
77470 }
77471
77472 /* Open the sqlite_stat[12] tables for writing. */
77473 for(i=0; i<ArraySize(aTable); i++){
77474 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
77475 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
77476 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
77477 }
77478 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77479
77480 /*
77481 ** Generate code to do an analysis of all indices associated with
77482 ** a single table.
77483 */
@@ -76448,31 +77497,24 @@
77497 int endOfLoop; /* The end of the loop */
77498 int jZeroRows = -1; /* Jump from here if number of rows is zero */
77499 int iDb; /* Index of database containing pTab */
77500 int regTabname = iMem++; /* Register containing table name */
77501 int regIdxname = iMem++; /* Register containing index name */
77502 int regSampleno = iMem++; /* Register containing next sample number */
77503 int regCol = iMem++; /* Content of a column analyzed table */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77504 int regRec = iMem++; /* Register holding completed record */
77505 int regTemp = iMem++; /* Temporary use register */
77506 int regRowid = iMem++; /* Rowid for the inserted record */
77507
77508 #ifdef SQLITE_ENABLE_STAT2
77509 int addr = 0; /* Instruction address */
77510 int regTemp2 = iMem++; /* Temporary use register */
77511 int regSamplerecno = iMem++; /* Index of next sample to record */
77512 int regRecno = iMem++; /* Current sample index */
77513 int regLast = iMem++; /* Index of last sample to record */
77514 int regFirst = iMem++; /* Index of first sample to record */
77515 #endif
77516
77517 v = sqlite3GetVdbe(pParse);
77518 if( v==0 || NEVER(pTab==0) ){
77519 return;
77520 }
@@ -76501,22 +77543,17 @@
77543 iIdxCur = pParse->nTab++;
77544 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
77545 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
77546 int nCol;
77547 KeyInfo *pKey;
 
 
77548
77549 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
 
77550 nCol = pIdx->nColumn;
77551 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
77552 if( iMem+1+(nCol*2)>pParse->nMem ){
77553 pParse->nMem = iMem+1+(nCol*2);
77554 }
 
 
77555
77556 /* Open a cursor to the index to be analyzed. */
77557 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
77558 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
77559 (char *)pKey, P4_KEYINFO_HANDOFF);
@@ -76523,24 +77560,35 @@
77560 VdbeComment((v, "%s", pIdx->zName));
77561
77562 /* Populate the register containing the index name. */
77563 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
77564
77565 #ifdef SQLITE_ENABLE_STAT2
77566
77567 /* If this iteration of the loop is generating code to analyze the
77568 ** first index in the pTab->pIndex list, then register regLast has
77569 ** not been populated. In this case populate it now. */
77570 if( pTab->pIndex==pIdx ){
77571 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
77572 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
77573 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
77574
77575 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
77576 sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
77577 addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
77578 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
77579 sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
77580 sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
77581 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
77582 sqlite3VdbeJumpHere(v, addr);
77583 }
77584
77585 /* Zero the regSampleno and regRecno registers. */
77586 sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
77587 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
77588 sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
77589 #endif
77590
77591 /* The block of memory cells initialized here is used as follows.
77592 **
77593 ** iMem:
77594 ** The total number of rows in the table.
@@ -76566,87 +77614,79 @@
77614 /* Start the analysis loop. This loop runs through all the entries in
77615 ** the index b-tree. */
77616 endOfLoop = sqlite3VdbeMakeLabel(v);
77617 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
77618 topOfLoop = sqlite3VdbeCurrentAddr(v);
77619 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
77620
77621 for(i=0; i<nCol; i++){
77622 CollSeq *pColl;
77623 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
77624 if( i==0 ){
77625 #ifdef SQLITE_ENABLE_STAT2
77626 /* Check if the record that cursor iIdxCur points to contains a
77627 ** value that should be stored in the sqlite_stat2 table. If so,
77628 ** store it. */
77629 int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
77630 assert( regTabname+1==regIdxname
77631 && regTabname+2==regSampleno
77632 && regTabname+3==regCol
77633 );
77634 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
77635 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
77636 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
77637 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
77638
77639 /* Calculate new values for regSamplerecno and regSampleno.
77640 **
77641 ** sampleno = sampleno + 1
77642 ** samplerecno = samplerecno+(remaining records)/(remaining samples)
77643 */
77644 sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
77645 sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
77646 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77647 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
77648 sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
77649 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
77650 sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
77651
77652 sqlite3VdbeJumpHere(v, ne);
77653 sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
77654 #endif
77655
77656 /* Always record the very first row */
77657 sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
77658 }
77659 assert( pIdx->azColl!=0 );
77660 assert( pIdx->azColl[i]!=0 );
77661 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
77662 sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
77663 (char*)pColl, P4_COLLSEQ);
77664 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
77665 }
77666 if( db->mallocFailed ){
77667 /* If a malloc failure has occurred, then the result of the expression
77668 ** passed as the second argument to the call to sqlite3VdbeJumpHere()
77669 ** below may be negative. Which causes an assert() to fail (or an
77670 ** out-of-bounds write if SQLITE_DEBUG is not defined). */
77671 return;
77672 }
77673 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
77674 for(i=0; i<nCol; i++){
77675 int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
77676 if( i==0 ){
77677 sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
77678 }
77679 sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
 
 
 
 
 
 
 
 
77680 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
77681 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
77682 }
 
77683
77684 /* End of the analysis loop. */
77685 sqlite3VdbeResolveLabel(v, endOfLoop);
 
77686 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
77687 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77688
77689 /* Store the results in sqlite_stat1.
77690 **
77691 ** The result is a single row of the sqlite_stat1 table. The first
77692 ** two columns are the names of the table and index. The third column
@@ -76662,51 +77702,50 @@
77702 **
77703 ** If K==0 then no entry is made into the sqlite_stat1 table.
77704 ** If K>0 then it is always the case the D>0 so division by zero
77705 ** is never possible.
77706 */
77707 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
77708 if( jZeroRows<0 ){
77709 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
77710 }
77711 for(i=0; i<nCol; i++){
77712 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
77713 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
77714 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
77715 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77716 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
77717 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
77718 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
77719 }
77720 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77721 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77722 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
77723 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77724 }
77725
77726 /* If the table has no indices, create a single sqlite_stat1 entry
77727 ** containing NULL as the index name and the row count as the content.
77728 */
77729 if( pTab->pIndex==0 ){
77730 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
77731 VdbeComment((v, "%s", pTab->zName));
77732 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
77733 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
77734 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
77735 }else{
77736 sqlite3VdbeJumpHere(v, jZeroRows);
77737 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
77738 }
77739 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
77740 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77741 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77742 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
77743 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77744 if( pParse->nMem<regRec ) pParse->nMem = regRec;
77745 sqlite3VdbeJumpHere(v, jZeroRows);
77746 }
 
77747
77748 /*
77749 ** Generate code that will cause the most recent index analysis to
77750 ** be loaded into internal hash tables where is can be used.
77751 */
@@ -76727,11 +77766,11 @@
77766 int iStatCur;
77767 int iMem;
77768
77769 sqlite3BeginWriteOperation(pParse, 0, iDb);
77770 iStatCur = pParse->nTab;
77771 pParse->nTab += 2;
77772 openStatTable(pParse, iDb, iStatCur, 0, 0);
77773 iMem = pParse->nMem+1;
77774 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77775 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
77776 Table *pTab = (Table*)sqliteHashData(k);
@@ -76752,11 +77791,11 @@
77791 assert( pTab!=0 );
77792 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
77793 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
77794 sqlite3BeginWriteOperation(pParse, 0, iDb);
77795 iStatCur = pParse->nTab;
77796 pParse->nTab += 2;
77797 if( pOnlyIdx ){
77798 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
77799 }else{
77800 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
77801 }
@@ -76857,11 +77896,11 @@
77896 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
77897 analysisInfo *pInfo = (analysisInfo*)pData;
77898 Index *pIndex;
77899 Table *pTable;
77900 int i, c, n;
77901 unsigned int v;
77902 const char *z;
77903
77904 assert( argc==3 );
77905 UNUSED_PARAMETER2(NotUsed, argc);
77906
@@ -76900,172 +77939,40 @@
77939 /*
77940 ** If the Index.aSample variable is not NULL, delete the aSample[] array
77941 ** and its contents.
77942 */
77943 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
77944 #ifdef SQLITE_ENABLE_STAT2
77945 if( pIdx->aSample ){
77946 int j;
77947 for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
77948 IndexSample *p = &pIdx->aSample[j];
77949 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
77950 sqlite3DbFree(db, p->u.z);
77951 }
77952 }
77953 sqlite3DbFree(db, pIdx->aSample);
77954 }
 
 
 
77955 #else
77956 UNUSED_PARAMETER(db);
77957 UNUSED_PARAMETER(pIdx);
77958 #endif
77959 }
77960
77961 /*
77962 ** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77963 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
77964 ** arrays. The contents of sqlite_stat2 are used to populate the
77965 ** Index.aSample[] arrays.
77966 **
77967 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
77968 ** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
77969 ** during compilation and the sqlite_stat2 table is present, no data is
77970 ** read from it.
77971 **
77972 ** If SQLITE_ENABLE_STAT2 was defined during compilation and the
77973 ** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
77974 ** returned. However, in this case, data is read from the sqlite_stat1
77975 ** table (if it is present) before returning.
77976 **
77977 ** If an OOM error occurs, this function always sets db->mallocFailed.
77978 ** This means if the caller does not care about other errors, the return
@@ -77083,14 +77990,12 @@
77990 /* Clear any prior statistics */
77991 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77992 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
77993 Index *pIdx = sqliteHashData(i);
77994 sqlite3DefaultRowEst(pIdx);
 
77995 sqlite3DeleteIndexSamples(db, pIdx);
77996 pIdx->aSample = 0;
 
77997 }
77998
77999 /* Check to make sure the sqlite_stat1 table exists */
78000 sInfo.db = db;
78001 sInfo.zDatabase = db->aDb[iDb].zName;
@@ -77098,23 +78003,91 @@
78003 return SQLITE_ERROR;
78004 }
78005
78006 /* Load new statistics out of the sqlite_stat1 table */
78007 zSql = sqlite3MPrintf(db,
78008 "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
78009 if( zSql==0 ){
78010 rc = SQLITE_NOMEM;
78011 }else{
78012 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
78013 sqlite3DbFree(db, zSql);
78014 }
78015
78016
78017 /* Load the statistics from the sqlite_stat2 table. */
78018 #ifdef SQLITE_ENABLE_STAT2
78019 if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
78020 rc = SQLITE_ERROR;
78021 }
78022 if( rc==SQLITE_OK ){
78023 sqlite3_stmt *pStmt = 0;
78024
78025 zSql = sqlite3MPrintf(db,
78026 "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
78027 if( !zSql ){
78028 rc = SQLITE_NOMEM;
78029 }else{
78030 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78031 sqlite3DbFree(db, zSql);
78032 }
78033
78034 if( rc==SQLITE_OK ){
78035 while( sqlite3_step(pStmt)==SQLITE_ROW ){
78036 char *zIndex; /* Index name */
78037 Index *pIdx; /* Pointer to the index object */
78038
78039 zIndex = (char *)sqlite3_column_text(pStmt, 0);
78040 pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
78041 if( pIdx ){
78042 int iSample = sqlite3_column_int(pStmt, 1);
78043 if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
78044 int eType = sqlite3_column_type(pStmt, 2);
78045
78046 if( pIdx->aSample==0 ){
78047 static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
78048 pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
78049 if( pIdx->aSample==0 ){
78050 db->mallocFailed = 1;
78051 break;
78052 }
78053 memset(pIdx->aSample, 0, sz);
78054 }
78055
78056 assert( pIdx->aSample );
78057 {
78058 IndexSample *pSample = &pIdx->aSample[iSample];
78059 pSample->eType = (u8)eType;
78060 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
78061 pSample->u.r = sqlite3_column_double(pStmt, 2);
78062 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
78063 const char *z = (const char *)(
78064 (eType==SQLITE_BLOB) ?
78065 sqlite3_column_blob(pStmt, 2):
78066 sqlite3_column_text(pStmt, 2)
78067 );
78068 int n = sqlite3_column_bytes(pStmt, 2);
78069 if( n>24 ){
78070 n = 24;
78071 }
78072 pSample->nByte = (u8)n;
78073 if( n < 1){
78074 pSample->u.z = 0;
78075 }else{
78076 pSample->u.z = sqlite3DbStrNDup(0, z, n);
78077 if( pSample->u.z==0 ){
78078 db->mallocFailed = 1;
78079 break;
78080 }
78081 }
78082 }
78083 }
78084 }
78085 }
78086 }
78087 rc = sqlite3_finalize(pStmt);
78088 }
78089 }
78090 #endif
78091
78092 if( rc==SQLITE_NOMEM ){
78093 db->mallocFailed = 1;
@@ -79610,11 +80583,11 @@
80583 Table *p;
80584 int n;
80585 const char *z;
80586 Token sEnd;
80587 DbFixer sFix;
80588 Token *pName = 0;
80589 int iDb;
80590 sqlite3 *db = pParse->db;
80591
80592 if( pParse->nVar>0 ){
80593 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
@@ -79926,15 +80899,11 @@
80899 Parse *pParse, /* The parsing context */
80900 int iDb, /* The database number */
80901 const char *zType, /* "idx" or "tbl" */
80902 const char *zName /* Name of index or table */
80903 ){
80904 static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
 
 
 
 
80905 int i;
80906 const char *zDbName = pParse->db->aDb[iDb].zName;
80907 for(i=0; i<ArraySize(azStatTab); i++){
80908 if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
80909 sqlite3NestedParse(pParse,
@@ -79941,81 +80910,10 @@
80910 "DELETE FROM %Q.%s WHERE %s=%Q",
80911 zDbName, azStatTab[i], zType, zName
80912 );
80913 }
80914 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80915 }
80916
80917 /*
80918 ** This routine is called to do the work of a DROP TABLE statement.
80919 ** pName is the name of the table to be dropped.
@@ -80082,11 +80980,11 @@
80980 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
80981 goto exit_drop_table;
80982 }
80983 }
80984 #endif
80985 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
80986 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
80987 goto exit_drop_table;
80988 }
80989
80990 #ifndef SQLITE_OMIT_VIEW
@@ -80106,15 +81004,72 @@
81004 /* Generate code to remove the table from the master table
81005 ** on disk.
81006 */
81007 v = sqlite3GetVdbe(pParse);
81008 if( v ){
81009 Trigger *pTrigger;
81010 Db *pDb = &db->aDb[iDb];
81011 sqlite3BeginWriteOperation(pParse, 1, iDb);
81012
81013 #ifndef SQLITE_OMIT_VIRTUALTABLE
81014 if( IsVirtual(pTab) ){
81015 sqlite3VdbeAddOp0(v, OP_VBegin);
81016 }
81017 #endif
81018 sqlite3FkDropTable(pParse, pName, pTab);
81019
81020 /* Drop all triggers associated with the table being dropped. Code
81021 ** is generated to remove entries from sqlite_master and/or
81022 ** sqlite_temp_master if required.
81023 */
81024 pTrigger = sqlite3TriggerList(pParse, pTab);
81025 while( pTrigger ){
81026 assert( pTrigger->pSchema==pTab->pSchema ||
81027 pTrigger->pSchema==db->aDb[1].pSchema );
81028 sqlite3DropTriggerPtr(pParse, pTrigger);
81029 pTrigger = pTrigger->pNext;
81030 }
81031
81032 #ifndef SQLITE_OMIT_AUTOINCREMENT
81033 /* Remove any entries of the sqlite_sequence table associated with
81034 ** the table being dropped. This is done before the table is dropped
81035 ** at the btree level, in case the sqlite_sequence table needs to
81036 ** move as a result of the drop (can happen in auto-vacuum mode).
81037 */
81038 if( pTab->tabFlags & TF_Autoincrement ){
81039 sqlite3NestedParse(pParse,
81040 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
81041 pDb->zName, pTab->zName
81042 );
81043 }
81044 #endif
81045
81046 /* Drop all SQLITE_MASTER table and index entries that refer to the
81047 ** table. The program name loops through the master table and deletes
81048 ** every row that refers to a table of the same name as the one being
81049 ** dropped. Triggers are handled seperately because a trigger can be
81050 ** created in the temp database that refers to a table in another
81051 ** database.
81052 */
81053 sqlite3NestedParse(pParse,
81054 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81055 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
81056 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
81057 if( !isView && !IsVirtual(pTab) ){
81058 destroyTable(pParse, pTab);
81059 }
81060
81061 /* Remove the table entry from SQLite's internal schema and modify
81062 ** the schema cookie.
81063 */
81064 if( IsVirtual(pTab) ){
81065 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81066 }
81067 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81068 sqlite3ChangeCookie(pParse, iDb);
81069 }
81070 sqliteViewResetAll(db, iDb);
81071
81072 exit_drop_table:
81073 sqlite3SrcListDelete(db, pName);
81074 }
81075
@@ -80278,18 +81233,28 @@
81233 */
81234 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
81235 Table *pTab = pIndex->pTable; /* The table that is indexed */
81236 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
81237 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
81238 int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */
81239 int addr1; /* Address of top of loop */
81240 int tnum; /* Root page of index */
81241 Vdbe *v; /* Generate code into this virtual machine */
81242 KeyInfo *pKey; /* KeyInfo for index */
81243 int regIdxKey; /* Registers containing the index key */
81244 int regRecord; /* Register holding assemblied index record */
81245 sqlite3 *db = pParse->db; /* The database connection */
81246 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
81247
81248 /* Set bUseSorter to use OP_OpenSorter, or clear it to insert directly
81249 ** into the index. The sorter is used unless either OMIT_MERGE_SORT is
81250 ** defined or the system is configured to store temp files in-memory. */
81251 #ifdef SQLITE_OMIT_MERGE_SORT
81252 static const int bUseSorter = 0;
81253 #else
81254 const int bUseSorter = !sqlite3TempInMemory(pParse->db);
81255 #endif
81256
81257 #ifndef SQLITE_OMIT_AUTHORIZATION
81258 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
81259 db->aDb[iDb].zName ) ){
81260 return;
@@ -80311,14 +81276,33 @@
81276 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
81277 (char *)pKey, P4_KEYINFO_HANDOFF);
81278 if( memRootPage>=0 ){
81279 sqlite3VdbeChangeP5(v, 1);
81280 }
81281
81282 /* Open the sorter cursor if we are to use one. */
81283 if( bUseSorter ){
81284 iSorter = pParse->nTab++;
81285 sqlite3VdbeAddOp4(v, OP_OpenSorter, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
81286 sqlite3VdbeChangeP5(v, BTREE_SORTER);
81287 }
81288
81289 /* Open the table. Loop through all rows of the table, inserting index
81290 ** records into the sorter. */
81291 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
81292 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
81293 regRecord = sqlite3GetTempReg(pParse);
81294 regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81295
81296 if( bUseSorter ){
81297 sqlite3VdbeAddOp2(v, OP_IdxInsert, iSorter, regRecord);
81298 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81299 sqlite3VdbeJumpHere(v, addr1);
81300 addr1 = sqlite3VdbeAddOp2(v, OP_Sort, iSorter, 0);
81301 sqlite3VdbeAddOp2(v, OP_RowKey, iSorter, regRecord);
81302 }
81303
81304 if( pIndex->onError!=OE_None ){
81305 const int regRowid = regIdxKey + pIndex->nColumn;
81306 const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
81307 void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
81308
@@ -80333,17 +81317,19 @@
81317 */
81318 sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
81319 sqlite3HaltConstraint(
81320 pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
81321 }
81322 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, bUseSorter);
81323 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81324 sqlite3ReleaseTempReg(pParse, regRecord);
81325 sqlite3VdbeAddOp2(v, OP_Next, iSorter, addr1+1);
81326 sqlite3VdbeJumpHere(v, addr1);
81327
81328 sqlite3VdbeAddOp1(v, OP_Close, iTab);
81329 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
81330 sqlite3VdbeAddOp1(v, OP_Close, iSorter);
81331 }
81332
81333 /*
81334 ** Create a new index for an SQL table. pName1.pName2 is the name of the index
81335 ** and pTblList is the name of the table that is to be indexed. Both will
@@ -80557,24 +81543,24 @@
81543 */
81544 nName = sqlite3Strlen30(zName);
81545 nCol = pList->nExpr;
81546 pIndex = sqlite3DbMallocZero(db,
81547 sizeof(Index) + /* Index structure */
 
81548 sizeof(int)*nCol + /* Index.aiColumn */
81549 sizeof(int)*(nCol+1) + /* Index.aiRowEst */
81550 sizeof(char *)*nCol + /* Index.azColl */
81551 sizeof(u8)*nCol + /* Index.aSortOrder */
81552 nName + 1 + /* Index.zName */
81553 nExtra /* Collation sequence names */
81554 );
81555 if( db->mallocFailed ){
81556 goto exit_create_index;
81557 }
81558 pIndex->azColl = (char**)(&pIndex[1]);
 
81559 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
81560 pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
81561 pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
81562 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
81563 zExtra = (char *)(&pIndex->zName[nName+1]);
81564 memcpy(pIndex->zName, zName, nName+1);
81565 pIndex->pTable = pTab;
81566 pIndex->nColumn = pList->nExpr;
@@ -80847,13 +81833,13 @@
81833 ** Apart from that, we have little to go on besides intuition as to
81834 ** how aiRowEst[] should be initialized. The numbers generated here
81835 ** are based on typical values found in actual indices.
81836 */
81837 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
81838 unsigned *a = pIdx->aiRowEst;
81839 int i;
81840 unsigned n;
81841 assert( a!=0 );
81842 a[0] = pIdx->pTable->nRowEst;
81843 if( a[0]<10 ) a[0] = 10;
81844 n = 10;
81845 for(i=1; i<=pIdx->nColumn; i++){
@@ -81293,12 +82279,13 @@
82279 ** The operator is "natural cross join". The A and B operands are stored
82280 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
82281 ** operator with A. This routine shifts that operator over to B.
82282 */
82283 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
82284 if( p ){
82285 int i;
82286 assert( p->a || p->nSrc==0 );
82287 for(i=p->nSrc-1; i>0; i--){
82288 p->a[i].jointype = p->a[i-1].jointype;
82289 }
82290 p->a[0].jointype = 0;
82291 }
@@ -82850,10 +83837,12 @@
83837 **
83838 ** There is only one exported symbol in this file - the function
83839 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
83840 ** All other code has file scope.
83841 */
83842 /* #include <stdlib.h> */
83843 /* #include <assert.h> */
83844
83845 /*
83846 ** Return the collating function associated with a function.
83847 */
83848 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -85173,11 +86162,28 @@
86162 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
86163 }else{
86164 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
86165 }
86166 if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
86167 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
86168 if( !isIgnoreErrors || db->mallocFailed ) return;
86169 if( pTo==0 ){
86170 /* If isIgnoreErrors is true, then a table is being dropped. In this
86171 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
86172 ** before actually dropping it in order to check FK constraints.
86173 ** If the parent table of an FK constraint on the current table is
86174 ** missing, behave as if it is empty. i.e. decrement the relevant
86175 ** FK counter for each row of the current table with non-NULL keys.
86176 */
86177 Vdbe *v = sqlite3GetVdbe(pParse);
86178 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
86179 for(i=0; i<pFKey->nCol; i++){
86180 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
86181 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
86182 }
86183 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
86184 }
86185 continue;
86186 }
86187 assert( pFKey->nCol==1 || (aiFree && pIdx) );
86188
86189 if( aiFree ){
@@ -88081,10 +89087,11 @@
89087
89088 #endif /* _SQLITE3EXT_H_ */
89089
89090 /************** End of sqlite3ext.h ******************************************/
89091 /************** Continuing where we left off in loadext.c ********************/
89092 /* #include <string.h> */
89093
89094 #ifndef SQLITE_OMIT_LOAD_EXTENSION
89095
89096 /*
89097 ** Some API routines are omitted when various features are
@@ -95650,10 +96657,12 @@
96657 ** interface routine of sqlite3_exec().
96658 **
96659 ** These routines are in a separate files so that they will not be linked
96660 ** if they are not used.
96661 */
96662 /* #include <stdlib.h> */
96663 /* #include <string.h> */
96664
96665 #ifndef SQLITE_OMIT_GET_TABLE
96666
96667 /*
96668 ** This structure is used to pass data from sqlite3_get_table() through
@@ -99159,11 +100168,11 @@
100168 #define TERM_CODED 0x04 /* This term is already coded */
100169 #define TERM_COPIED 0x08 /* Has a child */
100170 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
100171 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
100172 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
100173 #ifdef SQLITE_ENABLE_STAT2
100174 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
100175 #else
100176 # define TERM_VNULL 0x00 /* Disabled if not using stat2 */
100177 #endif
100178
@@ -100373,11 +101382,11 @@
101382 pNewTerm->prereqAll = pTerm->prereqAll;
101383 }
101384 }
101385 #endif /* SQLITE_OMIT_VIRTUALTABLE */
101386
101387 #ifdef SQLITE_ENABLE_STAT2
101388 /* When sqlite_stat2 histogram data is available an operator of the
101389 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
101390 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
101391 ** virtual term of that form.
101392 **
@@ -100412,11 +101421,11 @@
101421 pTerm->nChild = 1;
101422 pTerm->wtFlags |= TERM_COPIED;
101423 pNewTerm->prereqAll = pTerm->prereqAll;
101424 }
101425 }
101426 #endif /* SQLITE_ENABLE_STAT2 */
101427
101428 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
101429 ** an index for tables to the left of the join.
101430 */
101431 pTerm->prereqRight |= extraRight;
@@ -101461,89 +102470,71 @@
102470 */
102471 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
102472 }
102473 #endif /* SQLITE_OMIT_VIRTUALTABLE */
102474
 
102475 /*
102476 ** Argument pIdx is a pointer to an index structure that has an array of
102477 ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
102478 ** stored in Index.aSample. These samples divide the domain of values stored
102479 ** the index into (SQLITE_INDEX_SAMPLES+1) regions.
102480 ** Region 0 contains all values less than the first sample value. Region
102481 ** 1 contains values between the first and second samples. Region 2 contains
102482 ** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
102483 ** contains values larger than the last sample.
102484 **
102485 ** If the index contains many duplicates of a single value, then it is
102486 ** possible that two or more adjacent samples can hold the same value.
102487 ** When that is the case, the smallest possible region code is returned
102488 ** when roundUp is false and the largest possible region code is returned
102489 ** when roundUp is true.
102490 **
102491 ** If successful, this function determines which of the regions value
102492 ** pVal lies in, sets *piRegion to the region index (a value between 0
102493 ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
102494 ** Or, if an OOM occurs while converting text values between encodings,
102495 ** SQLITE_NOMEM is returned and *piRegion is undefined.
102496 */
102497 #ifdef SQLITE_ENABLE_STAT2
102498 static int whereRangeRegion(
102499 Parse *pParse, /* Database connection */
102500 Index *pIdx, /* Index to consider domain of */
102501 sqlite3_value *pVal, /* Value to consider */
102502 int roundUp, /* Return largest valid region if true */
102503 int *piRegion /* OUT: Region of domain in which value lies */
102504 ){
 
 
 
 
 
 
 
102505 assert( roundUp==0 || roundUp==1 );
102506 if( ALWAYS(pVal) ){
102507 IndexSample *aSample = pIdx->aSample;
102508 int i = 0;
102509 int eType = sqlite3_value_type(pVal);
102510
102511 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
102512 double r = sqlite3_value_double(pVal);
102513 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102514 if( aSample[i].eType==SQLITE_NULL ) continue;
102515 if( aSample[i].eType>=SQLITE_TEXT ) break;
102516 if( roundUp ){
102517 if( aSample[i].u.r>r ) break;
102518 }else{
102519 if( aSample[i].u.r>=r ) break;
102520 }
102521 }
102522 }else if( eType==SQLITE_NULL ){
102523 i = 0;
102524 if( roundUp ){
102525 while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
102526 }
102527 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102528 sqlite3 *db = pParse->db;
102529 CollSeq *pColl;
102530 const u8 *z;
102531 int n;
102532
102533 /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
102534 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
102535
102536 if( eType==SQLITE_BLOB ){
102537 z = (const u8 *)sqlite3_value_blob(pVal);
102538 pColl = db->pDfltColl;
102539 assert( pColl->enc==SQLITE_UTF8 );
102540 }else{
@@ -101558,16 +102549,16 @@
102549 return SQLITE_NOMEM;
102550 }
102551 assert( z && pColl && pColl->xCmp );
102552 }
102553 n = sqlite3ValueBytes(pVal, pColl->enc);
102554
102555 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102556 int c;
102557 int eSampletype = aSample[i].eType;
102558 if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
102559 if( (eSampletype!=eType) ) break;
102560 #ifndef SQLITE_OMIT_UTF16
102561 if( pColl->enc!=SQLITE_UTF8 ){
102562 int nSample;
102563 char *zSample = sqlite3Utf8to16(
102564 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -101581,52 +102572,20 @@
102572 }else
102573 #endif
102574 {
102575 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
102576 }
102577 if( c-roundUp>=0 ) break;
102578 }
102579 }
102580
102581 assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
102582 *piRegion = i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102583 }
102584 return SQLITE_OK;
102585 }
102586 #endif /* #ifdef SQLITE_ENABLE_STAT2 */
102587
102588 /*
102589 ** If expression pExpr represents a literal value, set *pp to point to
102590 ** an sqlite3_value structure containing the same value, with affinity
102591 ** aff applied to it, before returning. It is the responsibility of the
@@ -101640,11 +102599,11 @@
102599 **
102600 ** If neither of the above apply, set *pp to NULL.
102601 **
102602 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
102603 */
102604 #ifdef SQLITE_ENABLE_STAT2
102605 static int valueFromExpr(
102606 Parse *pParse,
102607 Expr *pExpr,
102608 u8 aff,
102609 sqlite3_value **pp
@@ -101688,92 +102647,106 @@
102647 **
102648 ** ... FROM t1 WHERE a > ? AND a < ? ...
102649 **
102650 ** then nEq should be passed 0.
102651 **
102652 ** The returned value is an integer between 1 and 100, inclusive. A return
102653 ** value of 1 indicates that the proposed range scan is expected to visit
102654 ** approximately 1/100th (1%) of the rows selected by the nEq equality
102655 ** constraints (if any). A return value of 100 indicates that it is expected
102656 ** that the range scan will visit every row (100%) selected by the equality
102657 ** constraints.
102658 **
102659 ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
102660 ** reduces the search space by 3/4ths. Hence a single constraint (x>?)
102661 ** results in a return of 25 and a range constraint (x>? AND x<?) results
102662 ** in a return of 6.
102663 */
102664 static int whereRangeScanEst(
102665 Parse *pParse, /* Parsing & code generating context */
102666 Index *p, /* The index containing the range-compared column; "x" */
102667 int nEq, /* index into p->aCol[] of the range-compared column */
102668 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
102669 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
102670 int *piEst /* OUT: Return value */
102671 ){
102672 int rc = SQLITE_OK;
102673
102674 #ifdef SQLITE_ENABLE_STAT2
102675
102676 if( nEq==0 && p->aSample ){
102677 sqlite3_value *pLowerVal = 0;
102678 sqlite3_value *pUpperVal = 0;
102679 int iEst;
102680 int iLower = 0;
102681 int iUpper = SQLITE_INDEX_SAMPLES;
102682 int roundUpUpper = 0;
102683 int roundUpLower = 0;
102684 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102685
102686 if( pLower ){
102687 Expr *pExpr = pLower->pExpr->pRight;
102688 rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
102689 assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
102690 roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
 
 
 
 
 
 
102691 }
102692 if( rc==SQLITE_OK && pUpper ){
102693 Expr *pExpr = pUpper->pExpr->pRight;
102694 rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
102695 assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
102696 roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
102697 }
102698
102699 if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
102700 sqlite3ValueFree(pLowerVal);
102701 sqlite3ValueFree(pUpperVal);
102702 goto range_est_fallback;
102703 }else if( pLowerVal==0 ){
102704 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
102705 if( pLower ) iLower = iUpper/2;
102706 }else if( pUpperVal==0 ){
102707 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
102708 if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
102709 }else{
102710 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
102711 if( rc==SQLITE_OK ){
102712 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
102713 }
102714 }
102715 WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
102716
102717 iEst = iUpper - iLower;
102718 testcase( iEst==SQLITE_INDEX_SAMPLES );
102719 assert( iEst<=SQLITE_INDEX_SAMPLES );
102720 if( iEst<1 ){
102721 *piEst = 50/SQLITE_INDEX_SAMPLES;
102722 }else{
102723 *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
102724 }
102725 sqlite3ValueFree(pLowerVal);
102726 sqlite3ValueFree(pUpperVal);
102727 return rc;
102728 }
102729 range_est_fallback:
102730 #else
102731 UNUSED_PARAMETER(pParse);
102732 UNUSED_PARAMETER(p);
102733 UNUSED_PARAMETER(nEq);
102734 #endif
102735 assert( pLower || pUpper );
102736 *piEst = 100;
102737 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
102738 if( pUpper ) *piEst /= 4;
102739 return rc;
102740 }
102741
102742 #ifdef SQLITE_ENABLE_STAT2
102743 /*
102744 ** Estimate the number of rows that will be returned based on
102745 ** an equality constraint x=VALUE and where that VALUE occurs in
102746 ** the histogram data. This only works when x is the left-most
102747 ** column of an index and sqlite_stat2 histogram data is available
102748 ** for that index. When pExpr==NULL that means the constraint is
102749 ** "x IS NULL" instead of "x=VALUE".
102750 **
102751 ** Write the estimated row count into *pnRow and return SQLITE_OK.
102752 ** If unable to make an estimate, leave *pnRow unchanged and return
@@ -101789,13 +102762,14 @@
102762 Index *p, /* The index whose left-most column is pTerm */
102763 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
102764 double *pnRow /* Write the revised row estimate here */
102765 ){
102766 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
102767 int iLower, iUpper; /* Range of histogram regions containing pRhs */
102768 u8 aff; /* Column affinity */
102769 int rc; /* Subfunction return code */
102770 double nRowEst; /* New estimate of the number of rows */
102771
102772 assert( p->aSample!=0 );
102773 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102774 if( pExpr ){
102775 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
@@ -101802,22 +102776,30 @@
102776 if( rc ) goto whereEqualScanEst_cancel;
102777 }else{
102778 pRhs = sqlite3ValueNew(pParse->db);
102779 }
102780 if( pRhs==0 ) return SQLITE_NOTFOUND;
102781 rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
102782 if( rc ) goto whereEqualScanEst_cancel;
102783 rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
102784 if( rc ) goto whereEqualScanEst_cancel;
102785 WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
102786 if( iLower>=iUpper ){
102787 nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
102788 if( nRowEst<*pnRow ) *pnRow = nRowEst;
102789 }else{
102790 nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
102791 *pnRow = nRowEst;
102792 }
102793
102794 whereEqualScanEst_cancel:
102795 sqlite3ValueFree(pRhs);
102796 return rc;
102797 }
102798 #endif /* defined(SQLITE_ENABLE_STAT2) */
102799
102800 #ifdef SQLITE_ENABLE_STAT2
102801 /*
102802 ** Estimate the number of rows that will be returned based on
102803 ** an IN constraint where the right-hand side of the IN operator
102804 ** is a list of values. Example:
102805 **
@@ -101836,29 +102818,64 @@
102818 Parse *pParse, /* Parsing & code generating context */
102819 Index *p, /* The index whose left-most column is pTerm */
102820 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
102821 double *pnRow /* Write the revised row estimate here */
102822 ){
102823 sqlite3_value *pVal = 0; /* One value from list */
102824 int iLower, iUpper; /* Range of histogram regions containing pRhs */
102825 u8 aff; /* Column affinity */
102826 int rc = SQLITE_OK; /* Subfunction return code */
102827 double nRowEst; /* New estimate of the number of rows */
102828 int nSpan = 0; /* Number of histogram regions spanned */
102829 int nSingle = 0; /* Histogram regions hit by a single value */
102830 int nNotFound = 0; /* Count of values that are not constants */
102831 int i; /* Loop counter */
102832 u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
102833 u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
102834
102835 assert( p->aSample!=0 );
102836 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102837 memset(aSpan, 0, sizeof(aSpan));
102838 memset(aSingle, 0, sizeof(aSingle));
102839 for(i=0; i<pList->nExpr; i++){
102840 sqlite3ValueFree(pVal);
102841 rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
102842 if( rc ) break;
102843 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
102844 nNotFound++;
102845 continue;
102846 }
102847 rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
102848 if( rc ) break;
102849 rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
102850 if( rc ) break;
102851 if( iLower>=iUpper ){
102852 aSingle[iLower] = 1;
102853 }else{
102854 assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
102855 while( iLower<iUpper ) aSpan[iLower++] = 1;
102856 }
102857 }
102858 if( rc==SQLITE_OK ){
102859 for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
102860 if( aSpan[i] ){
102861 nSpan++;
102862 }else if( aSingle[i] ){
102863 nSingle++;
102864 }
102865 }
102866 nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
102867 + nNotFound*p->aiRowEst[1];
102868 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
102869 *pnRow = nRowEst;
102870 WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
102871 nSpan, nSingle, nNotFound, nRowEst));
102872 }
102873 sqlite3ValueFree(pVal);
102874 return rc;
102875 }
102876 #endif /* defined(SQLITE_ENABLE_STAT2) */
102877
102878
102879 /*
102880 ** Find the best query plan for accessing a particular table. Write the
102881 ** best query plan and its cost into the WhereCost object supplied as the
@@ -101901,11 +102918,11 @@
102918 Index *pProbe; /* An index we are evaluating */
102919 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
102920 int eqTermMask; /* Current mask of valid equality operators */
102921 int idxEqTermMask; /* Index mask of valid equality operators */
102922 Index sPk; /* A fake index object for the primary key */
102923 unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
102924 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
102925 int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
102926
102927 /* Initialize the cost to a worst-case value */
102928 memset(pCost, 0, sizeof(*pCost));
@@ -101956,11 +102973,11 @@
102973 }
102974
102975 /* Loop over all indices looking for the best one to use
102976 */
102977 for(; pProbe; pIdx=pProbe=pProbe->pNext){
102978 const unsigned int * const aiRowEst = pProbe->aiRowEst;
102979 double cost; /* Cost of using pProbe */
102980 double nRow; /* Estimated number of rows in result set */
102981 double log10N; /* base-10 logarithm of nRow (inexact) */
102982 int rev; /* True to scan in reverse order */
102983 int wsFlags = 0;
@@ -101999,16 +103016,18 @@
103016 ** Set to true if there was at least one "x IN (SELECT ...)" term used
103017 ** in determining the value of nInMul. Note that the RHS of the
103018 ** IN operator must be a SELECT, not a value list, for this variable
103019 ** to be true.
103020 **
103021 ** estBound:
103022 ** An estimate on the amount of the table that must be searched. A
103023 ** value of 100 means the entire table is searched. Range constraints
103024 ** might reduce this to a value less than 100 to indicate that only
103025 ** a fraction of the table needs searching. In the absence of
103026 ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
103027 ** space to 1/4rd its original size. So an x>? constraint reduces
103028 ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
103029 **
103030 ** bSort:
103031 ** Boolean. True if there is an ORDER BY clause that will require an
103032 ** external sort (i.e. scanning the index being evaluated will not
103033 ** correctly order records).
@@ -102029,17 +103048,17 @@
103048 ** SELECT a, b, c FROM tbl WHERE a = 1;
103049 */
103050 int nEq; /* Number of == or IN terms matching index */
103051 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
103052 int nInMul = 1; /* Number of distinct equalities to lookup */
103053 int estBound = 100; /* Estimated reduction in search space */
103054 int nBound = 0; /* Number of range constraints seen */
103055 int bSort = !!pOrderBy; /* True if external sort required */
103056 int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
103057 int bLookup = 0; /* True if not a covering index */
103058 WhereTerm *pTerm; /* A single term of the WHERE clause */
103059 #ifdef SQLITE_ENABLE_STAT2
103060 WhereTerm *pFirstTerm = 0; /* First term matching the index */
103061 #endif
103062
103063 /* Determine the values of nEq and nInMul */
103064 for(nEq=0; nEq<pProbe->nColumn; nEq++){
@@ -102059,23 +103078,23 @@
103078 nInMul *= pExpr->x.pList->nExpr;
103079 }
103080 }else if( pTerm->eOperator & WO_ISNULL ){
103081 wsFlags |= WHERE_COLUMN_NULL;
103082 }
103083 #ifdef SQLITE_ENABLE_STAT2
103084 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103085 #endif
103086 used |= pTerm->prereqRight;
103087 }
103088
103089 /* Determine the value of estBound. */
103090 if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103091 int j = pProbe->aiColumn[nEq];
103092 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103093 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103094 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103095 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
103096 if( pTop ){
103097 nBound = 1;
103098 wsFlags |= WHERE_TOP_LIMIT;
103099 used |= pTop->prereqRight;
103100 }
@@ -102143,11 +103162,11 @@
103162 if( bInEst && nRow*2>aiRowEst[0] ){
103163 nRow = aiRowEst[0]/2;
103164 nInMul = (int)(nRow / aiRowEst[nEq]);
103165 }
103166
103167 #ifdef SQLITE_ENABLE_STAT2
103168 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
103169 ** and we do not think that values of x are unique and if histogram
103170 ** data is available for column x, then it might be possible
103171 ** to get a better estimate on the number of rows based on
103172 ** VALUE and how common that value is according to the histogram.
@@ -102159,16 +103178,16 @@
103178 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
103179 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
103180 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
103181 }
103182 }
103183 #endif /* SQLITE_ENABLE_STAT2 */
103184
103185 /* Adjust the number of output rows and downward to reflect rows
103186 ** that are excluded by range constraints.
103187 */
103188 nRow = (nRow * (double)estBound) / (double)100;
103189 if( nRow<1 ) nRow = 1;
103190
103191 /* Experiments run on real SQLite databases show that the time needed
103192 ** to do a binary search to locate a row in a table or index is roughly
103193 ** log10(N) times the time to move from one row to the next row within
@@ -102293,14 +103312,14 @@
103312 if( nRow<2 ) nRow = 2;
103313 }
103314
103315
103316 WHERETRACE((
103317 "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103318 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
103319 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
103320 nEq, nInMul, estBound, bSort, bLookup, wsFlags,
103321 notReady, log10N, nRow, cost, used
103322 ));
103323
103324 /* If this index is the best we have seen so far, then record this
103325 ** index and its cost in the pCost structure.
@@ -104227,10 +105246,11 @@
105246 ** LALR(1) grammar but which are always false in the
105247 ** specific grammar used by SQLite.
105248 */
105249 /* First off, code is included that follows the "include" declaration
105250 ** in the input grammar file. */
105251 /* #include <stdio.h> */
105252
105253
105254 /*
105255 ** Disable all error recovery processing in the parser push-down
105256 ** automaton.
@@ -105087,10 +106107,11 @@
106107 #endif
106108 };
106109 typedef struct yyParser yyParser;
106110
106111 #ifndef NDEBUG
106112 /* #include <stdio.h> */
106113 static FILE *yyTraceFILE = 0;
106114 static char *yyTracePrompt = 0;
106115 #endif /* NDEBUG */
106116
106117 #ifndef NDEBUG
@@ -107662,10 +108683,11 @@
108683 **
108684 ** This file contains C code that splits an SQL input string up into
108685 ** individual tokens and sends those tokens one-by-one over to the
108686 ** parser for analysis.
108687 */
108688 /* #include <stdlib.h> */
108689
108690 /*
108691 ** The charMap() macro maps alphabetic characters into their
108692 ** lower-case ASCII equivalent. On ASCII machines, this is just
108693 ** an upper-to-lower case map. On EBCDIC machines we also need
@@ -109053,10 +110075,20 @@
110075 memcpy(&y, &x, 8);
110076 assert( sqlite3IsNaN(y) );
110077 }
110078 #endif
110079 #endif
110080
110081 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
110082 ** compile-time option.
110083 */
110084 #ifdef SQLITE_EXTRA_INIT
110085 if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
110086 int SQLITE_EXTRA_INIT(void);
110087 rc = SQLITE_EXTRA_INIT();
110088 }
110089 #endif
110090
110091 return rc;
110092 }
110093
110094 /*
@@ -113184,10 +114216,16 @@
114216
114217 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
114218 # define SQLITE_CORE 1
114219 #endif
114220
114221 /* #include <assert.h> */
114222 /* #include <stdlib.h> */
114223 /* #include <stddef.h> */
114224 /* #include <stdio.h> */
114225 /* #include <string.h> */
114226 /* #include <stdarg.h> */
114227
114228 #ifndef SQLITE_CORE
114229 SQLITE_EXTENSION_INIT1
114230 #endif
114231
@@ -117705,10 +118743,12 @@
118743 ******************************************************************************
118744 **
118745 */
118746 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
118747
118748 /* #include <string.h> */
118749 /* #include <assert.h> */
118750
118751 typedef struct Fts3auxTable Fts3auxTable;
118752 typedef struct Fts3auxCursor Fts3auxCursor;
118753
118754 struct Fts3auxTable {
@@ -118243,10 +119283,12 @@
119283 /*
119284 ** Default span for NEAR operators.
119285 */
119286 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
119287
119288 /* #include <string.h> */
119289 /* #include <assert.h> */
119290
119291 /*
119292 ** isNot:
119293 ** This variable is used by function getNextNode(). When getNextNode() is
119294 ** called, it sets ParseContext.isNot to true if the 'next node' is a
@@ -118944,10 +119986,11 @@
119986 ** Everything after this point is just test code.
119987 */
119988
119989 #ifdef SQLITE_TEST
119990
119991 /* #include <stdio.h> */
119992
119993 /*
119994 ** Function to query the hash-table of tokenizers (see README.tokenizers).
119995 */
119996 static int queryTestTokenizer(
@@ -119154,10 +120197,13 @@
120197 ** * The FTS3 module is being built into the core of
120198 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120199 */
120200 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120201
120202 /* #include <assert.h> */
120203 /* #include <stdlib.h> */
120204 /* #include <string.h> */
120205
120206
120207 /*
120208 ** Malloc and Free functions
120209 */
@@ -119534,10 +120580,14 @@
120580 ** * The FTS3 module is being built into the core of
120581 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120582 */
120583 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120584
120585 /* #include <assert.h> */
120586 /* #include <stdlib.h> */
120587 /* #include <stdio.h> */
120588 /* #include <string.h> */
120589
120590
120591 /*
120592 ** Class derived from sqlite3_tokenizer
120593 */
@@ -120177,10 +121227,12 @@
121227 ** * The FTS3 module is being built into the core of
121228 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
121229 */
121230 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
121231
121232 /* #include <assert.h> */
121233 /* #include <string.h> */
121234
121235 /*
121236 ** Implementation of the SQL scalar function for accessing the underlying
121237 ** hash table. This function may be called as follows:
121238 **
@@ -120352,10 +121404,12 @@
121404 }
121405
121406
121407 #ifdef SQLITE_TEST
121408
121409 /* #include <tcl.h> */
121410 /* #include <string.h> */
121411
121412 /*
121413 ** Implementation of a special SQL scalar function for testing tokenizers
121414 ** designed to be used in concert with the Tcl testing framework. This
121415 ** function must be called with two arguments:
@@ -120663,10 +121717,14 @@
121717 ** * The FTS3 module is being built into the core of
121718 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
121719 */
121720 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
121721
121722 /* #include <assert.h> */
121723 /* #include <stdlib.h> */
121724 /* #include <stdio.h> */
121725 /* #include <string.h> */
121726
121727
121728 typedef struct simple_tokenizer {
121729 sqlite3_tokenizer base;
121730 char delim[128]; /* flag ASCII delimiters */
@@ -120888,10 +121946,13 @@
121946 ** code in fts3.c.
121947 */
121948
121949 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
121950
121951 /* #include <string.h> */
121952 /* #include <assert.h> */
121953 /* #include <stdlib.h> */
121954
121955 /*
121956 ** When full-text index nodes are loaded from disk, the buffer that they
121957 ** are loaded into has the following number of bytes of padding at the end
121958 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
@@ -124149,10 +125210,12 @@
125210 ******************************************************************************
125211 */
125212
125213 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
125214
125215 /* #include <string.h> */
125216 /* #include <assert.h> */
125217
125218 /*
125219 ** Characters that may appear in the second argument to matchinfo().
125220 */
125221 #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
@@ -125736,10 +126799,12 @@
126799 #ifndef SQLITE_CORE
126800 SQLITE_EXTENSION_INIT1
126801 #else
126802 #endif
126803
126804 /* #include <string.h> */
126805 /* #include <assert.h> */
126806
126807 #ifndef SQLITE_AMALGAMATION
126808 #include "sqlite3rtree.h"
126809 typedef sqlite3_int64 i64;
126810 typedef unsigned char u8;
@@ -128950,10 +130015,11 @@
130015 #include <unicode/utypes.h>
130016 #include <unicode/uregex.h>
130017 #include <unicode/ustring.h>
130018 #include <unicode/ucol.h>
130019
130020 /* #include <assert.h> */
130021
130022 #ifndef SQLITE_CORE
130023 SQLITE_EXTENSION_INIT1
130024 #else
130025 #endif
@@ -129429,12 +130495,16 @@
130495 ** This file implements a tokenizer for fts3 based on the ICU library.
130496 */
130497 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
130498 #ifdef SQLITE_ENABLE_ICU
130499
130500 /* #include <assert.h> */
130501 /* #include <string.h> */
130502
130503 #include <unicode/ubrk.h>
130504 /* #include <unicode/ucol.h> */
130505 /* #include <unicode/ustring.h> */
130506 #include <unicode/utf16.h>
130507
130508 typedef struct IcuTokenizer IcuTokenizer;
130509 typedef struct IcuCursor IcuCursor;
130510
130511
+5 -11
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.8"
111111
#define SQLITE_VERSION_NUMBER 3007008
112
-#define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
112
+#define SQLITE_SOURCE_ID "2011-08-29 11:56:14 639cc85a911454bffdcccb33f2976c683953ae64"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -1208,20 +1208,14 @@
12081208
** also used during testing of SQLite in order to specify an alternative
12091209
** memory allocator that simulates memory out-of-memory conditions in
12101210
** order to verify that SQLite recovers gracefully from such
12111211
** conditions.
12121212
**
1213
-** The xMalloc and xFree methods must work like the
1214
-** malloc() and free() functions from the standard C library.
1215
-** The xRealloc method must work like realloc() from the standard C library
1216
-** with the exception that if the second argument to xRealloc is zero,
1217
-** xRealloc must be a no-op - it must not perform any allocation or
1218
-** deallocation. ^SQLite guarantees that the second argument to
1213
+** The xMalloc, xRealloc, and xFree methods must work like the
1214
+** malloc(), realloc() and free() functions from the standard C library.
1215
+** ^SQLite guarantees that the second argument to
12191216
** xRealloc is always a value returned by a prior call to xRoundup.
1220
-** And so in cases where xRoundup always returns a positive number,
1221
-** xRealloc can perform exactly as the standard library realloc() and
1222
-** still be in compliance with this specification.
12231217
**
12241218
** xSize should return the allocated size of a memory allocation
12251219
** previously obtained from xMalloc or xRealloc. The allocated size
12261220
** is always at least as big as the requested size but may be larger.
12271221
**
@@ -2854,11 +2848,11 @@
28542848
** a schema change, on the first [sqlite3_step()] call following any change
28552849
** to the [sqlite3_bind_text | bindings] of that [parameter].
28562850
** ^The specific value of WHERE-clause [parameter] might influence the
28572851
** choice of query plan if the parameter is the left-hand side of a [LIKE]
28582852
** or [GLOB] operator or if the parameter is compared to an indexed column
2859
-** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
2853
+** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
28602854
** the
28612855
** </li>
28622856
** </ol>
28632857
*/
28642858
SQLITE_API int sqlite3_prepare(
28652859
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.8"
111 #define SQLITE_VERSION_NUMBER 3007008
112 #define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -1208,20 +1208,14 @@
1208 ** also used during testing of SQLite in order to specify an alternative
1209 ** memory allocator that simulates memory out-of-memory conditions in
1210 ** order to verify that SQLite recovers gracefully from such
1211 ** conditions.
1212 **
1213 ** The xMalloc and xFree methods must work like the
1214 ** malloc() and free() functions from the standard C library.
1215 ** The xRealloc method must work like realloc() from the standard C library
1216 ** with the exception that if the second argument to xRealloc is zero,
1217 ** xRealloc must be a no-op - it must not perform any allocation or
1218 ** deallocation. ^SQLite guarantees that the second argument to
1219 ** xRealloc is always a value returned by a prior call to xRoundup.
1220 ** And so in cases where xRoundup always returns a positive number,
1221 ** xRealloc can perform exactly as the standard library realloc() and
1222 ** still be in compliance with this specification.
1223 **
1224 ** xSize should return the allocated size of a memory allocation
1225 ** previously obtained from xMalloc or xRealloc. The allocated size
1226 ** is always at least as big as the requested size but may be larger.
1227 **
@@ -2854,11 +2848,11 @@
2854 ** a schema change, on the first [sqlite3_step()] call following any change
2855 ** to the [sqlite3_bind_text | bindings] of that [parameter].
2856 ** ^The specific value of WHERE-clause [parameter] might influence the
2857 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
2858 ** or [GLOB] operator or if the parameter is compared to an indexed column
2859 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
2860 ** the
2861 ** </li>
2862 ** </ol>
2863 */
2864 SQLITE_API int sqlite3_prepare(
2865
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.8"
111 #define SQLITE_VERSION_NUMBER 3007008
112 #define SQLITE_SOURCE_ID "2011-08-29 11:56:14 639cc85a911454bffdcccb33f2976c683953ae64"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -1208,20 +1208,14 @@
1208 ** also used during testing of SQLite in order to specify an alternative
1209 ** memory allocator that simulates memory out-of-memory conditions in
1210 ** order to verify that SQLite recovers gracefully from such
1211 ** conditions.
1212 **
1213 ** The xMalloc, xRealloc, and xFree methods must work like the
1214 ** malloc(), realloc() and free() functions from the standard C library.
1215 ** ^SQLite guarantees that the second argument to
 
 
 
1216 ** xRealloc is always a value returned by a prior call to xRoundup.
 
 
 
1217 **
1218 ** xSize should return the allocated size of a memory allocation
1219 ** previously obtained from xMalloc or xRealloc. The allocated size
1220 ** is always at least as big as the requested size but may be larger.
1221 **
@@ -2854,11 +2848,11 @@
2848 ** a schema change, on the first [sqlite3_step()] call following any change
2849 ** to the [sqlite3_bind_text | bindings] of that [parameter].
2850 ** ^The specific value of WHERE-clause [parameter] might influence the
2851 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
2852 ** or [GLOB] operator or if the parameter is compared to an indexed column
2853 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
2854 ** the
2855 ** </li>
2856 ** </ol>
2857 */
2858 SQLITE_API int sqlite3_prepare(
2859
--- src/timeline.c
+++ src/timeline.c
@@ -1312,11 +1312,10 @@
13121312
zFree = sqlite3_mprintf("[%.10s] %s%s", zUuid, zPrefix, zCom);
13131313
nLine += comment_print(zFree, 9, 79);
13141314
sqlite3_free(zFree);
13151315
13161316
if(showfiles){
1317
- int inUl = 0;
13181317
if( !fchngQueryInit ){
13191318
db_prepare(&fchngQuery,
13201319
"SELECT (pid==0) AS isnew,"
13211320
" (fid==0) AS isdel,"
13221321
" (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
@@ -1331,12 +1330,10 @@
13311330
db_bind_int(&fchngQuery, ":mid", rid);
13321331
while( db_step(&fchngQuery)==SQLITE_ROW ){
13331332
const char *zFilename = db_column_text(&fchngQuery, 2);
13341333
int isNew = db_column_int(&fchngQuery, 0);
13351334
int isDel = db_column_int(&fchngQuery, 1);
1336
- const char *zOld = db_column_text(&fchngQuery, 4);
1337
- const char *zNew = db_column_text(&fchngQuery, 3);
13381335
if( isNew ){
13391336
fossil_print(" ADDED %s\n",zFilename);
13401337
}else if( isDel ){
13411338
fossil_print(" DELETED %s\n",zFilename);
13421339
}else{
13431340
--- src/timeline.c
+++ src/timeline.c
@@ -1312,11 +1312,10 @@
1312 zFree = sqlite3_mprintf("[%.10s] %s%s", zUuid, zPrefix, zCom);
1313 nLine += comment_print(zFree, 9, 79);
1314 sqlite3_free(zFree);
1315
1316 if(showfiles){
1317 int inUl = 0;
1318 if( !fchngQueryInit ){
1319 db_prepare(&fchngQuery,
1320 "SELECT (pid==0) AS isnew,"
1321 " (fid==0) AS isdel,"
1322 " (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
@@ -1331,12 +1330,10 @@
1331 db_bind_int(&fchngQuery, ":mid", rid);
1332 while( db_step(&fchngQuery)==SQLITE_ROW ){
1333 const char *zFilename = db_column_text(&fchngQuery, 2);
1334 int isNew = db_column_int(&fchngQuery, 0);
1335 int isDel = db_column_int(&fchngQuery, 1);
1336 const char *zOld = db_column_text(&fchngQuery, 4);
1337 const char *zNew = db_column_text(&fchngQuery, 3);
1338 if( isNew ){
1339 fossil_print(" ADDED %s\n",zFilename);
1340 }else if( isDel ){
1341 fossil_print(" DELETED %s\n",zFilename);
1342 }else{
1343
--- src/timeline.c
+++ src/timeline.c
@@ -1312,11 +1312,10 @@
1312 zFree = sqlite3_mprintf("[%.10s] %s%s", zUuid, zPrefix, zCom);
1313 nLine += comment_print(zFree, 9, 79);
1314 sqlite3_free(zFree);
1315
1316 if(showfiles){
 
1317 if( !fchngQueryInit ){
1318 db_prepare(&fchngQuery,
1319 "SELECT (pid==0) AS isnew,"
1320 " (fid==0) AS isdel,"
1321 " (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
@@ -1331,12 +1330,10 @@
1330 db_bind_int(&fchngQuery, ":mid", rid);
1331 while( db_step(&fchngQuery)==SQLITE_ROW ){
1332 const char *zFilename = db_column_text(&fchngQuery, 2);
1333 int isNew = db_column_int(&fchngQuery, 0);
1334 int isDel = db_column_int(&fchngQuery, 1);
 
 
1335 if( isNew ){
1336 fossil_print(" ADDED %s\n",zFilename);
1337 }else if( isDel ){
1338 fossil_print(" DELETED %s\n",zFilename);
1339 }else{
1340
+1 -1
--- src/tkt.c
+++ src/tkt.c
@@ -101,11 +101,11 @@
101101
const char *zName;
102102
Stmt q;
103103
int i, n, size, j;
104104
105105
zName = PD("name","-none-");
106
- db_prepare(&q, "SELECT datetime(tkt_mtime) AS tkt_datetime, *"
106
+ db_prepare(&q, "SELECT datetime(tkt_mtime,'localtime') AS tkt_datetime, *"
107107
" FROM ticket WHERE tkt_uuid GLOB '%q*'", zName);
108108
if( db_step(&q)==SQLITE_ROW ){
109109
n = db_column_count(&q);
110110
for(i=0; i<n; i++){
111111
const char *zVal = db_column_text(&q, i);
112112
--- src/tkt.c
+++ src/tkt.c
@@ -101,11 +101,11 @@
101 const char *zName;
102 Stmt q;
103 int i, n, size, j;
104
105 zName = PD("name","-none-");
106 db_prepare(&q, "SELECT datetime(tkt_mtime) AS tkt_datetime, *"
107 " FROM ticket WHERE tkt_uuid GLOB '%q*'", zName);
108 if( db_step(&q)==SQLITE_ROW ){
109 n = db_column_count(&q);
110 for(i=0; i<n; i++){
111 const char *zVal = db_column_text(&q, i);
112
--- src/tkt.c
+++ src/tkt.c
@@ -101,11 +101,11 @@
101 const char *zName;
102 Stmt q;
103 int i, n, size, j;
104
105 zName = PD("name","-none-");
106 db_prepare(&q, "SELECT datetime(tkt_mtime,'localtime') AS tkt_datetime, *"
107 " FROM ticket WHERE tkt_uuid GLOB '%q*'", zName);
108 if( db_step(&q)==SQLITE_ROW ){
109 n = db_column_count(&q);
110 for(i=0; i<n; i++){
111 const char *zVal = db_column_text(&q, i);
112
+7 -1
--- src/user.c
+++ src/user.c
@@ -312,11 +312,17 @@
312312
*/
313313
void user_select(void){
314314
Stmt s;
315315
316316
if( g.userUid ) return;
317
- if( attempt_user(g.zLogin) ) return;
317
+ if( g.zLogin ){
318
+ if( attempt_user(g.zLogin)==0 ){
319
+ fossil_fatal("no such user: %s", g.zLogin);
320
+ }else{
321
+ return;
322
+ }
323
+ }
318324
319325
if( g.localOpen && attempt_user(db_lget("default-user",0)) ) return;
320326
321327
if( attempt_user(db_get("default-user", 0)) ) return;
322328
323329
--- src/user.c
+++ src/user.c
@@ -312,11 +312,17 @@
312 */
313 void user_select(void){
314 Stmt s;
315
316 if( g.userUid ) return;
317 if( attempt_user(g.zLogin) ) return;
 
 
 
 
 
 
318
319 if( g.localOpen && attempt_user(db_lget("default-user",0)) ) return;
320
321 if( attempt_user(db_get("default-user", 0)) ) return;
322
323
--- src/user.c
+++ src/user.c
@@ -312,11 +312,17 @@
312 */
313 void user_select(void){
314 Stmt s;
315
316 if( g.userUid ) return;
317 if( g.zLogin ){
318 if( attempt_user(g.zLogin)==0 ){
319 fossil_fatal("no such user: %s", g.zLogin);
320 }else{
321 return;
322 }
323 }
324
325 if( g.localOpen && attempt_user(db_lget("default-user",0)) ) return;
326
327 if( attempt_user(db_get("default-user", 0)) ) return;
328
329

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button