Fossil SCM

Update the built-in SQLite to the latest pre-3.7.8 version from the stat3-enhancement branch. And turn on SQLITE_ENABLE_STAT3. This will serve as a test of the stat3 enhancements to SQLite.

drh 2011-08-16 02:43 trunk
Commit ce62a2b11d4e882962c82b8542d9c62f86b4cd73
+1 -1
--- src/main.mk
+++ src/main.mk
@@ -898,11 +898,11 @@
898898
$(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
899899
$(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
900900
901901
$(OBJDIR)/zip.h: $(OBJDIR)/headers
902902
$(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
903
- $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
903
+ $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT3 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
904904
905905
$(OBJDIR)/shell.o: $(SRCDIR)/shell.c $(SRCDIR)/sqlite3.h
906906
$(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
907907
908908
$(OBJDIR)/th.o: $(SRCDIR)/th.c
909909
--- src/main.mk
+++ src/main.mk
@@ -898,11 +898,11 @@
898 $(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
899 $(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
900
901 $(OBJDIR)/zip.h: $(OBJDIR)/headers
902 $(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
903 $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
904
905 $(OBJDIR)/shell.o: $(SRCDIR)/shell.c $(SRCDIR)/sqlite3.h
906 $(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
907
908 $(OBJDIR)/th.o: $(SRCDIR)/th.c
909
--- src/main.mk
+++ src/main.mk
@@ -898,11 +898,11 @@
898 $(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
899 $(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
900
901 $(OBJDIR)/zip.h: $(OBJDIR)/headers
902 $(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
903 $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT3 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
904
905 $(OBJDIR)/shell.o: $(SRCDIR)/shell.c $(SRCDIR)/sqlite3.h
906 $(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
907
908 $(OBJDIR)/th.o: $(SRCDIR)/th.c
909
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -248,11 +248,11 @@
248248
249249
writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
250250
set opt {-DSQLITE_OMIT_LOAD_EXTENSION=1}
251251
append opt " -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4"
252252
#append opt " -DSQLITE_ENABLE_FTS3=1"
253
-append opt " -DSQLITE_ENABLE_STAT2"
253
+append opt " -DSQLITE_ENABLE_STAT3"
254254
append opt " -Dlocaltime=fossil_localtime"
255255
append opt " -DSQLITE_ENABLE_LOCKING_STYLE=0"
256256
set SQLITE_OPTIONS $opt
257257
writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
258258
259259
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -248,11 +248,11 @@
248
249 writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
250 set opt {-DSQLITE_OMIT_LOAD_EXTENSION=1}
251 append opt " -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4"
252 #append opt " -DSQLITE_ENABLE_FTS3=1"
253 append opt " -DSQLITE_ENABLE_STAT2"
254 append opt " -Dlocaltime=fossil_localtime"
255 append opt " -DSQLITE_ENABLE_LOCKING_STYLE=0"
256 set SQLITE_OPTIONS $opt
257 writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
258
259
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -248,11 +248,11 @@
248
249 writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
250 set opt {-DSQLITE_OMIT_LOAD_EXTENSION=1}
251 append opt " -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4"
252 #append opt " -DSQLITE_ENABLE_FTS3=1"
253 append opt " -DSQLITE_ENABLE_STAT3"
254 append opt " -Dlocaltime=fossil_localtime"
255 append opt " -DSQLITE_ENABLE_LOCKING_STYLE=0"
256 set SQLITE_OPTIONS $opt
257 writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
258
259
+1140 -627
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -650,11 +650,11 @@
650650
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651651
** [sqlite_version()] and [sqlite_source_id()].
652652
*/
653653
#define SQLITE_VERSION "3.7.8"
654654
#define SQLITE_VERSION_NUMBER 3007008
655
-#define SQLITE_SOURCE_ID "2011-07-19 18:29:00 ed5f0aad6b21066bacd01521e82c22e96991f400"
655
+#define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
656656
657657
/*
658658
** CAPI3REF: Run-Time Library Version Numbers
659659
** KEYWORDS: sqlite3_version, sqlite3_sourceid
660660
**
@@ -1298,10 +1298,24 @@
12981298
** integers where the first integer i the new retry count and the second
12991299
** integer is the delay. If either integer is negative, then the setting
13001300
** is not changed but instead the prior value of that setting is written
13011301
** into the array entry, allowing the current retry settings to be
13021302
** interrogated. The zDbName parameter is ignored.
1303
+**
1304
+** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1305
+** persistent [WAL | Write AHead Log] setting. By default, the auxiliary
1306
+** write ahead log and shared memory files used for transaction control
1307
+** are automatically deleted when the latest connection to the database
1308
+** closes. Setting persistent WAL mode causes those files to persist after
1309
+** close. Persisting the files is useful when other processes that do not
1310
+** have write permission on the directory containing the database file want
1311
+** to read the database file, as the WAL and shared memory files must exist
1312
+** in order for the database to be readable. The fourth parameter to
1313
+** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1314
+** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1315
+** WAL mode. If the integer is -1, then it is overwritten with the current
1316
+** WAL persistence setting.
13031317
**
13041318
*/
13051319
#define SQLITE_FCNTL_LOCKSTATE 1
13061320
#define SQLITE_GET_LOCKPROXYFILE 2
13071321
#define SQLITE_SET_LOCKPROXYFILE 3
@@ -1309,10 +1323,11 @@
13091323
#define SQLITE_FCNTL_SIZE_HINT 5
13101324
#define SQLITE_FCNTL_CHUNK_SIZE 6
13111325
#define SQLITE_FCNTL_FILE_POINTER 7
13121326
#define SQLITE_FCNTL_SYNC_OMITTED 8
13131327
#define SQLITE_FCNTL_WIN32_AV_RETRY 9
1328
+#define SQLITE_FCNTL_PERSIST_WAL 10
13141329
13151330
/*
13161331
** CAPI3REF: Mutex Handle
13171332
**
13181333
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3382,11 +3397,11 @@
33823397
** a schema change, on the first [sqlite3_step()] call following any change
33833398
** to the [sqlite3_bind_text | bindings] of that [parameter].
33843399
** ^The specific value of WHERE-clause [parameter] might influence the
33853400
** choice of query plan if the parameter is the left-hand side of a [LIKE]
33863401
** or [GLOB] operator or if the parameter is compared to an indexed column
3387
-** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3402
+** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
33883403
** the
33893404
** </li>
33903405
** </ol>
33913406
*/
33923407
SQLITE_API int sqlite3_prepare(
@@ -7696,10 +7711,22 @@
76967711
** is 0x00000000ffffffff. But because of quirks of some compilers, we
76977712
** have to specify the value in the less intuitive manner shown:
76987713
*/
76997714
#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
77007715
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
+
77017728
/*
77027729
** Macros to determine whether the machine is big or little endian,
77037730
** evaluated at runtime.
77047731
*/
77057732
#ifdef SQLITE_AMALGAMATION
@@ -9897,11 +9924,11 @@
98979924
int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
98989925
int nCol; /* Number of columns in this table */
98999926
Column *aCol; /* Information about each column */
99009927
Index *pIndex; /* List of SQL indexes on this table. */
99019928
int tnum; /* Root BTree node for this table (see note above) */
9902
- unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9929
+ tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
99039930
Select *pSelect; /* NULL for tables. Points to definition if a view. */
99049931
u16 nRef; /* Number of pointers to this Table */
99059932
u8 tabFlags; /* Mask of TF_* values */
99069933
u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
99079934
FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10096,35 +10123,43 @@
1009610123
*/
1009710124
struct Index {
1009810125
char *zName; /* Name of this index */
1009910126
int nColumn; /* Number of columns in the table used by this index */
1010010127
int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10101
- unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10128
+ tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
1010210129
Table *pTable; /* The SQL table being indexed */
1010310130
int tnum; /* Page containing root of this index in database file */
1010410131
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1010510132
u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
1010610133
u8 bUnordered; /* Use this index for == or IN queries only */
10134
+ u8 nSample; /* Number of elements in aSample[] */
1010710135
char *zColAff; /* String defining the affinity of each column */
1010810136
Index *pNext; /* The next index associated with the same table */
1010910137
Schema *pSchema; /* Schema containing this index */
1011010138
u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
1011110139
char **azColl; /* Array of collation sequence names for index */
10112
- IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
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
1011310144
};
1011410145
1011510146
/*
1011610147
** Each sample stored in the sqlite_stat2 table is represented in memory
1011710148
** using a structure of this type.
1011810149
*/
1011910150
struct IndexSample {
1012010151
union {
1012110152
char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10122
- double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
10153
+ double r; /* Value if eType is SQLITE_FLOAT */
10154
+ i64 i; /* Value if eType is SQLITE_INTEGER */
1012310155
} u;
1012410156
u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10125
- u8 nByte; /* Size in byte of text or blob. */
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 */
1012610161
};
1012710162
1012810163
/*
1012910164
** Each token coming out of the lexer is an instance of
1013010165
** this structure. Tokens are also used as part of an expression.
@@ -11326,10 +11361,11 @@
1132611361
#else
1132711362
# define sqlite3ViewGetColumnNames(A,B) 0
1132811363
#endif
1132911364
1133011365
SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11366
+SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
1133111367
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
1133211368
#ifndef SQLITE_OMIT_AUTOINCREMENT
1133311369
SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
1133411370
SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
1133511371
#else
@@ -11582,11 +11618,11 @@
1158211618
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
1158311619
void(*)(void*));
1158411620
SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
1158511621
SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
1158611622
SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11587
-#ifdef SQLITE_ENABLE_STAT2
11623
+#ifdef SQLITE_ENABLE_STAT3
1158811624
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
1158911625
#endif
1159011626
SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
1159111627
SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
1159211628
#ifndef SQLITE_AMALGAMATION
@@ -12208,10 +12244,13 @@
1220812244
#ifdef SQLITE_ENABLE_RTREE
1220912245
"ENABLE_RTREE",
1221012246
#endif
1221112247
#ifdef SQLITE_ENABLE_STAT2
1221212248
"ENABLE_STAT2",
12249
+#endif
12250
+#ifdef SQLITE_ENABLE_STAT3
12251
+ "ENABLE_STAT3",
1221312252
#endif
1221412253
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
1221512254
"ENABLE_UNLOCK_NOTIFY",
1221612255
#endif
1221712256
#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
@@ -14403,11 +14442,11 @@
1440314442
DO_OS_MALLOC_TEST(0);
1440414443
/* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
1440514444
** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
1440614445
** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
1440714446
** reaching the VFS. */
14408
- rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f3f, pFlagsOut);
14447
+ rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
1440914448
assert( rc==SQLITE_OK || pFile->pMethods==0 );
1441014449
return rc;
1441114450
}
1441214451
SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
1441314452
return pVfs->xDelete(pVfs, zPath, dirSync);
@@ -20447,11 +20486,11 @@
2044720486
** no longer required.
2044820487
**
2044920488
** If a malloc failure occurs, NULL is returned and the db.mallocFailed
2045020489
** flag set.
2045120490
*/
20452
-#ifdef SQLITE_ENABLE_STAT2
20491
+#ifdef SQLITE_ENABLE_STAT3
2045320492
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
2045420493
Mem m;
2045520494
memset(&m, 0, sizeof(m));
2045620495
m.db = db;
2045720496
sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -21693,26 +21732,32 @@
2169321732
return -x;
2169421733
}
2169521734
2169621735
#ifdef SQLITE_ENABLE_8_3_NAMES
2169721736
/*
21698
-** If SQLITE_ENABLE_8_3_NAME is set at compile-time and if the database
21737
+** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
2169921738
** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
2170021739
** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
2170121740
** three characters, then shorten the suffix on z[] to be the last three
2170221741
** characters of the original suffix.
21742
+**
21743
+** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
21744
+** do the suffix shortening regardless of URI parameter.
2170321745
**
2170421746
** Examples:
2170521747
**
2170621748
** test.db-journal => test.nal
2170721749
** test.db-wal => test.wal
2170821750
** test.db-shm => test.shm
2170921751
*/
2171021752
SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
21753
+#if SQLITE_ENABLE_8_3_NAMES<2
2171121754
const char *zOk;
2171221755
zOk = sqlite3_uri_parameter(zBaseFilename, "8_3_names");
21713
- if( zOk && sqlite3GetBoolean(zOk) ){
21756
+ if( zOk && sqlite3GetBoolean(zOk) )
21757
+#endif
21758
+ {
2171421759
int i, sz;
2171521760
sz = sqlite3Strlen30(z);
2171621761
for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
2171721762
if( z[i]=='.' && ALWAYS(sz>i+4) ) memcpy(&z[i+1], &z[sz-3], 4);
2171821763
}
@@ -22242,14 +22287,17 @@
2224222287
#ifdef MEMORY_DEBUG
2224322288
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
2224422289
#endif
2224522290
2224622291
#ifdef SQLITE_DEBUG
22247
-SQLITE_PRIVATE int sqlite3OSTrace = 0;
22248
-#define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
22292
+# ifndef SQLITE_DEBUG_OS_TRACE
22293
+# define SQLITE_DEBUG_OS_TRACE 0
22294
+# endif
22295
+ int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
22296
+# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
2224922297
#else
22250
-#define OSTRACE(X)
22298
+# define OSTRACE(X)
2225122299
#endif
2225222300
2225322301
/*
2225422302
** Macros for performance tracing. Normally turned off. Only works
2225522303
** on i486 hardware.
@@ -24492,11 +24540,10 @@
2449224540
typedef struct unixFile unixFile;
2449324541
struct unixFile {
2449424542
sqlite3_io_methods const *pMethod; /* Always the first entry */
2449524543
unixInodeInfo *pInode; /* Info about locks on this inode */
2449624544
int h; /* The file descriptor */
24497
- int dirfd; /* File descriptor for the directory */
2449824545
unsigned char eFileLock; /* The type of lock held on this fd */
2449924546
unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
2450024547
int lastErrno; /* The unix errno from last I/O error */
2450124548
void *lockingContext; /* Locking style specific state */
2450224549
UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
@@ -24534,12 +24581,14 @@
2453424581
};
2453524582
2453624583
/*
2453724584
** Allowed values for the unixFile.ctrlFlags bitmask:
2453824585
*/
24539
-#define UNIXFILE_EXCL 0x01 /* Connections from one process only */
24540
-#define UNIXFILE_RDONLY 0x02 /* Connection is read only */
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 */
2454124590
2454224591
/*
2454324592
** Include code that is common to all os_*.c files
2454424593
*/
2454524594
/************** Include os_common.h in the middle of os_unix.c ***************/
@@ -24574,14 +24623,17 @@
2457424623
#ifdef MEMORY_DEBUG
2457524624
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
2457624625
#endif
2457724626
2457824627
#ifdef SQLITE_DEBUG
24579
-SQLITE_PRIVATE int sqlite3OSTrace = 0;
24580
-#define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
24628
+# ifndef SQLITE_DEBUG_OS_TRACE
24629
+# define SQLITE_DEBUG_OS_TRACE 0
24630
+# endif
24631
+ int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
24632
+# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
2458124633
#else
24582
-#define OSTRACE(X)
24634
+# define OSTRACE(X)
2458324635
#endif
2458424636
2458524637
/*
2458624638
** Macros for performance tracing. Normally turned off. Only works
2458724639
** on i486 hardware.
@@ -24786,10 +24838,13 @@
2478624838
*/
2478724839
static int posixOpen(const char *zFile, int flags, int mode){
2478824840
return open(zFile, flags, mode);
2478924841
}
2479024842
24843
+/* Forward reference */
24844
+static int openDirectory(const char*, int*);
24845
+
2479124846
/*
2479224847
** Many system calls are accessed through pointer-to-functions so that
2479324848
** they may be overridden at runtime to facilitate fault injection during
2479424849
** testing and sandboxing. The following array holds the names and pointers
2479524850
** to all overrideable system calls.
@@ -24882,10 +24937,16 @@
2488224937
#else
2488324938
{ "fallocate", (sqlite3_syscall_ptr)0, 0 },
2488424939
#endif
2488524940
#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
2488624941
24942
+ { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
24943
+#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
24944
+
24945
+ { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
24946
+#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
24947
+
2488724948
}; /* End of the overrideable system calls */
2488824949
2488924950
/*
2489024951
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
2489124952
** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -26238,22 +26299,18 @@
2623826299
** even on VxWorks. A mutex will be acquired on VxWorks by the
2623926300
** vxworksReleaseFileId() routine.
2624026301
*/
2624126302
static int closeUnixFile(sqlite3_file *id){
2624226303
unixFile *pFile = (unixFile*)id;
26243
- if( pFile->dirfd>=0 ){
26244
- robust_close(pFile, pFile->dirfd, __LINE__);
26245
- pFile->dirfd=-1;
26246
- }
2624726304
if( pFile->h>=0 ){
2624826305
robust_close(pFile, pFile->h, __LINE__);
2624926306
pFile->h = -1;
2625026307
}
2625126308
#if OS_VXWORKS
2625226309
if( pFile->pId ){
2625326310
if( pFile->isDelete ){
26254
- unlink(pFile->pId->zCanonicalName);
26311
+ osUnlink(pFile->pId->zCanonicalName);
2625526312
}
2625626313
vxworksReleaseFileId(pFile->pId);
2625726314
pFile->pId = 0;
2625826315
}
2625926316
#endif
@@ -26498,11 +26555,11 @@
2649826555
return SQLITE_OK;
2649926556
}
2650026557
2650126558
/* To fully unlock the database, delete the lock file */
2650226559
assert( eFileLock==NO_LOCK );
26503
- if( unlink(zLockFile) ){
26560
+ if( osUnlink(zLockFile) ){
2650426561
int rc = 0;
2650526562
int tErrno = errno;
2650626563
if( ENOENT != tErrno ){
2650726564
rc = SQLITE_IOERR_UNLOCK;
2650826565
}
@@ -27733,10 +27790,54 @@
2773327790
if( OS_VXWORKS && rc!= -1 ){
2773427791
rc = 0;
2773527792
}
2773627793
return rc;
2773727794
}
27795
+
27796
+/*
27797
+** Open a file descriptor to the directory containing file zFilename.
27798
+** If successful, *pFd is set to the opened file descriptor and
27799
+** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
27800
+** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
27801
+** value.
27802
+**
27803
+** The directory file descriptor is used for only one thing - to
27804
+** fsync() a directory to make sure file creation and deletion events
27805
+** are flushed to disk. Such fsyncs are not needed on newer
27806
+** journaling filesystems, but are required on older filesystems.
27807
+**
27808
+** This routine can be overridden using the xSetSysCall interface.
27809
+** The ability to override this routine was added in support of the
27810
+** chromium sandbox. Opening a directory is a security risk (we are
27811
+** told) so making it overrideable allows the chromium sandbox to
27812
+** replace this routine with a harmless no-op. To make this routine
27813
+** a no-op, replace it with a stub that returns SQLITE_OK but leaves
27814
+** *pFd set to a negative number.
27815
+**
27816
+** If SQLITE_OK is returned, the caller is responsible for closing
27817
+** the file descriptor *pFd using close().
27818
+*/
27819
+static int openDirectory(const char *zFilename, int *pFd){
27820
+ int ii;
27821
+ int fd = -1;
27822
+ char zDirname[MAX_PATHNAME+1];
27823
+
27824
+ sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
27825
+ for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
27826
+ if( ii>0 ){
27827
+ zDirname[ii] = '\0';
27828
+ fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
27829
+ if( fd>=0 ){
27830
+#ifdef FD_CLOEXEC
27831
+ osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
27832
+#endif
27833
+ OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
27834
+ }
27835
+ }
27836
+ *pFd = fd;
27837
+ return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
27838
+}
2773827839
2773927840
/*
2774027841
** Make sure all writes to a particular file are committed to disk.
2774127842
**
2774227843
** If dataOnly==0 then both the file itself and its metadata (file
@@ -27774,32 +27875,25 @@
2777427875
SimulateIOError( rc=1 );
2777527876
if( rc ){
2777627877
pFile->lastErrno = errno;
2777727878
return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
2777827879
}
27779
- if( pFile->dirfd>=0 ){
27780
- OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
27880
+
27881
+ /* Also fsync the directory containing the file if the DIRSYNC flag
27882
+ ** is set. This is a one-time occurrance. Many systems (examples: AIX)
27883
+ ** are unable to fsync a directory, so ignore errors on the fsync.
27884
+ */
27885
+ if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
27886
+ int dirfd;
27887
+ OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
2778127888
HAVE_FULLFSYNC, isFullsync));
27782
-#ifndef SQLITE_DISABLE_DIRSYNC
27783
- /* The directory sync is only attempted if full_fsync is
27784
- ** turned off or unavailable. If a full_fsync occurred above,
27785
- ** then the directory sync is superfluous.
27786
- */
27787
- if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
27788
- /*
27789
- ** We have received multiple reports of fsync() returning
27790
- ** errors when applied to directories on certain file systems.
27791
- ** A failed directory sync is not a big deal. So it seems
27792
- ** better to ignore the error. Ticket #1657
27793
- */
27794
- /* pFile->lastErrno = errno; */
27795
- /* return SQLITE_IOERR; */
27796
- }
27797
-#endif
27798
- /* Only need to sync once, so close the directory when we are done */
27799
- robust_close(pFile, pFile->dirfd, __LINE__);
27800
- pFile->dirfd = -1;
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;
2780127895
}
2780227896
return rc;
2780327897
}
2780427898
2780527899
/*
@@ -27884,17 +27978,23 @@
2788427978
** If the user has configured a chunk-size for this file, it could be
2788527979
** that the file needs to be extended at this point. Otherwise, the
2788627980
** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
2788727981
*/
2788827982
static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27889
- if( pFile->szChunk ){
27983
+ { /* preserve indentation of removed "if" */
2789027984
i64 nSize; /* Required file size */
27985
+ i64 szChunk; /* Chunk size */
2789127986
struct stat buf; /* Used to hold return values of fstat() */
2789227987
2789327988
if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
2789427989
27895
- nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
27990
+ szChunk = pFile->szChunk;
27991
+ if( szChunk==0 ){
27992
+ nSize = nByte;
27993
+ }else{
27994
+ nSize = ((nByte+szChunk-1) / szChunk) * szChunk;
27995
+ }
2789627996
if( nSize>(i64)buf.st_size ){
2789727997
2789827998
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
2789927999
/* The code below is handling the return value of osFallocate()
2790028000
** correctly. posix_fallocate() is defined to "returns zero on success,
@@ -27933,25 +28033,37 @@
2793328033
2793428034
/*
2793528035
** Information and control of an open file handle.
2793628036
*/
2793728037
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
28038
+ unixFile *pFile = (unixFile*)id;
2793828039
switch( op ){
2793928040
case SQLITE_FCNTL_LOCKSTATE: {
27940
- *(int*)pArg = ((unixFile*)id)->eFileLock;
28041
+ *(int*)pArg = pFile->eFileLock;
2794128042
return SQLITE_OK;
2794228043
}
2794328044
case SQLITE_LAST_ERRNO: {
27944
- *(int*)pArg = ((unixFile*)id)->lastErrno;
28045
+ *(int*)pArg = pFile->lastErrno;
2794528046
return SQLITE_OK;
2794628047
}
2794728048
case SQLITE_FCNTL_CHUNK_SIZE: {
27948
- ((unixFile*)id)->szChunk = *(int *)pArg;
28049
+ pFile->szChunk = *(int *)pArg;
2794928050
return SQLITE_OK;
2795028051
}
2795128052
case SQLITE_FCNTL_SIZE_HINT: {
27952
- return fcntlSizeHint((unixFile *)id, *(i64 *)pArg);
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;
28059
+ }else if( bPersist==0 ){
28060
+ pFile->ctrlFlags &= ~UNIXFILE_PERSIST_WAL;
28061
+ }else{
28062
+ pFile->ctrlFlags |= UNIXFILE_PERSIST_WAL;
28063
+ }
28064
+ return SQLITE_OK;
2795328065
}
2795428066
#ifndef NDEBUG
2795528067
/* The pager calls this method to signal that it has done
2795628068
** a rollback and that the database is therefore unchanged and
2795728069
** it hence it is OK for the transaction change counter to be
@@ -28632,11 +28744,11 @@
2863228744
** shared-memory file, too */
2863328745
unixEnterMutex();
2863428746
assert( pShmNode->nRef>0 );
2863528747
pShmNode->nRef--;
2863628748
if( pShmNode->nRef==0 ){
28637
- if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename);
28749
+ if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
2863828750
unixShmPurge(pDbFd);
2863928751
}
2864028752
unixLeaveMutex();
2864128753
2864228754
return SQLITE_OK;
@@ -28945,11 +29057,11 @@
2894529057
** Initialize the contents of the unixFile structure pointed to by pId.
2894629058
*/
2894729059
static int fillInUnixFile(
2894829060
sqlite3_vfs *pVfs, /* Pointer to vfs object */
2894929061
int h, /* Open file descriptor of file being opened */
28950
- int dirfd, /* Directory file descriptor */
29062
+ int syncDir, /* True to sync directory on first sync */
2895129063
sqlite3_file *pId, /* Write to the unixFile structure here */
2895229064
const char *zFilename, /* Name of the file being opened */
2895329065
int noLock, /* Omit locking if true */
2895429066
int isDelete, /* Delete on close if true */
2895529067
int isReadOnly /* True if the file is opened read-only */
@@ -28976,20 +29088,22 @@
2897629088
assert( zFilename==0 || zFilename[0]=='/' );
2897729089
#endif
2897829090
2897929091
OSTRACE(("OPEN %-3d %s\n", h, zFilename));
2898029092
pNew->h = h;
28981
- pNew->dirfd = dirfd;
2898229093
pNew->zPath = zFilename;
2898329094
if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
2898429095
pNew->ctrlFlags = UNIXFILE_EXCL;
2898529096
}else{
2898629097
pNew->ctrlFlags = 0;
2898729098
}
2898829099
if( isReadOnly ){
2898929100
pNew->ctrlFlags |= UNIXFILE_RDONLY;
2899029101
}
29102
+ if( syncDir ){
29103
+ pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
29104
+ }
2899129105
2899229106
#if OS_VXWORKS
2899329107
pNew->pId = vxworksFindFileId(zFilename);
2899429108
if( pNew->pId==0 ){
2899529109
noLock = 1;
@@ -29112,56 +29226,24 @@
2911229226
pNew->lastErrno = 0;
2911329227
#if OS_VXWORKS
2911429228
if( rc!=SQLITE_OK ){
2911529229
if( h>=0 ) robust_close(pNew, h, __LINE__);
2911629230
h = -1;
29117
- unlink(zFilename);
29231
+ osUnlink(zFilename);
2911829232
isDelete = 0;
2911929233
}
2912029234
pNew->isDelete = isDelete;
2912129235
#endif
2912229236
if( rc!=SQLITE_OK ){
29123
- if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__);
2912429237
if( h>=0 ) robust_close(pNew, h, __LINE__);
2912529238
}else{
2912629239
pNew->pMethod = pLockingStyle;
2912729240
OpenCounter(+1);
2912829241
}
2912929242
return rc;
2913029243
}
2913129244
29132
-/*
29133
-** Open a file descriptor to the directory containing file zFilename.
29134
-** If successful, *pFd is set to the opened file descriptor and
29135
-** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
29136
-** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
29137
-** value.
29138
-**
29139
-** If SQLITE_OK is returned, the caller is responsible for closing
29140
-** the file descriptor *pFd using close().
29141
-*/
29142
-static int openDirectory(const char *zFilename, int *pFd){
29143
- int ii;
29144
- int fd = -1;
29145
- char zDirname[MAX_PATHNAME+1];
29146
-
29147
- sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
29148
- for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
29149
- if( ii>0 ){
29150
- zDirname[ii] = '\0';
29151
- fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
29152
- if( fd>=0 ){
29153
-#ifdef FD_CLOEXEC
29154
- osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
29155
-#endif
29156
- OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
29157
- }
29158
- }
29159
- *pFd = fd;
29160
- return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
29161
-}
29162
-
2916329245
/*
2916429246
** Return the name of a directory in which to put temporary files.
2916529247
** If no suitable temporary file directory can be found, return NULL.
2916629248
*/
2916729249
static const char *unixTempFileDir(void){
@@ -29272,11 +29354,11 @@
2927229354
** ignored and -1 is returned. The caller will try to open a new file
2927329355
** descriptor on the same path, fail, and return an error to SQLite.
2927429356
**
2927529357
** Even if a subsequent open() call does succeed, the consequences of
2927629358
** not searching for a resusable file descriptor are not dire. */
29277
- if( 0==stat(zPath, &sStat) ){
29359
+ if( 0==osStat(zPath, &sStat) ){
2927829360
unixInodeInfo *pInode;
2927929361
2928029362
unixEnterMutex();
2928129363
pInode = inodeList;
2928229364
while( pInode && (pInode->fileId.dev!=sStat.st_dev
@@ -29348,11 +29430,11 @@
2934829430
while( nDb>0 && zPath[nDb]!='-' ) nDb--;
2934929431
if( nDb==0 ) return SQLITE_OK;
2935029432
memcpy(zDb, zPath, nDb);
2935129433
zDb[nDb] = '\0';
2935229434
29353
- if( 0==stat(zDb, &sStat) ){
29435
+ if( 0==osStat(zDb, &sStat) ){
2935429436
*pMode = sStat.st_mode & 0777;
2935529437
}else{
2935629438
rc = SQLITE_IOERR_FSTAT;
2935729439
}
2935829440
}else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
@@ -29390,11 +29472,10 @@
2939029472
int flags, /* Input flags to control the opening */
2939129473
int *pOutFlags /* Output flags returned to SQLite core */
2939229474
){
2939329475
unixFile *p = (unixFile *)pFile;
2939429476
int fd = -1; /* File descriptor returned by open() */
29395
- int dirfd = -1; /* Directory file descriptor */
2939629477
int openFlags = 0; /* Flags to pass to open() */
2939729478
int eType = flags&0xFFFFFF00; /* Type of file to open */
2939829479
int noLock; /* True to omit locking primitives */
2939929480
int rc = SQLITE_OK; /* Function Return Code */
2940029481
@@ -29409,11 +29490,11 @@
2940929490
2941029491
/* If creating a master or main-file journal, this function will open
2941129492
** a file-descriptor on the directory too. The first time unixSync()
2941229493
** is called the directory file descriptor will be fsync()ed and close()d.
2941329494
*/
29414
- int isOpenDirectory = (isCreate && (
29495
+ int syncDir = (isCreate && (
2941529496
eType==SQLITE_OPEN_MASTER_JOURNAL
2941629497
|| eType==SQLITE_OPEN_MAIN_JOURNAL
2941729498
|| eType==SQLITE_OPEN_WAL
2941829499
));
2941929500
@@ -29463,11 +29544,11 @@
2946329544
}
2946429545
}
2946529546
p->pUnused = pUnused;
2946629547
}else if( !zName ){
2946729548
/* If zName is NULL, the upper layer is requesting a temp file. */
29468
- assert(isDelete && !isOpenDirectory);
29549
+ assert(isDelete && !syncDir);
2946929550
rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
2947029551
if( rc!=SQLITE_OK ){
2947129552
return rc;
2947229553
}
2947329554
zName = zTmpname;
@@ -29519,32 +29600,19 @@
2951929600
2952029601
if( isDelete ){
2952129602
#if OS_VXWORKS
2952229603
zPath = zName;
2952329604
#else
29524
- unlink(zName);
29605
+ osUnlink(zName);
2952529606
#endif
2952629607
}
2952729608
#if SQLITE_ENABLE_LOCKING_STYLE
2952829609
else{
2952929610
p->openFlags = openFlags;
2953029611
}
2953129612
#endif
2953229613
29533
- if( isOpenDirectory ){
29534
- rc = openDirectory(zPath, &dirfd);
29535
- if( rc!=SQLITE_OK ){
29536
- /* It is safe to close fd at this point, because it is guaranteed not
29537
- ** to be open on a database file. If it were open on a database file,
29538
- ** it would not be safe to close as this would release any locks held
29539
- ** on the file by this process. */
29540
- assert( eType!=SQLITE_OPEN_MAIN_DB );
29541
- robust_close(p, fd, __LINE__);
29542
- goto open_finished;
29543
- }
29544
- }
29545
-
2954629614
#ifdef FD_CLOEXEC
2954729615
osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2954829616
#endif
2954929617
2955029618
noLock = eType!=SQLITE_OPEN_MAIN_DB;
@@ -29552,11 +29620,10 @@
2955229620
2955329621
#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
2955429622
struct statfs fsInfo;
2955529623
if( fstatfs(fd, &fsInfo) == -1 ){
2955629624
((unixFile*)pFile)->lastErrno = errno;
29557
- if( dirfd>=0 ) robust_close(p, dirfd, __LINE__);
2955829625
robust_close(p, fd, __LINE__);
2955929626
return SQLITE_IOERR_ACCESS;
2956029627
}
2956129628
if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
2956229629
((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
@@ -29584,21 +29651,18 @@
2958429651
** then the call to close() will cancel those locks. In practice,
2958529652
** we're assuming that statfs() doesn't fail very often. At least
2958629653
** not while other file descriptors opened by the same process on
2958729654
** the same file are working. */
2958829655
p->lastErrno = errno;
29589
- if( dirfd>=0 ){
29590
- robust_close(p, dirfd, __LINE__);
29591
- }
2959229656
robust_close(p, fd, __LINE__);
2959329657
rc = SQLITE_IOERR_ACCESS;
2959429658
goto open_finished;
2959529659
}
2959629660
useProxy = !(fsInfo.f_flags&MNT_LOCAL);
2959729661
}
2959829662
if( useProxy ){
29599
- rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
29663
+ rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
2960029664
isDelete, isReadonly);
2960129665
if( rc==SQLITE_OK ){
2960229666
rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
2960329667
if( rc!=SQLITE_OK ){
2960429668
/* Use unixClose to clean up the resources added in fillInUnixFile
@@ -29612,11 +29676,11 @@
2961229676
goto open_finished;
2961329677
}
2961429678
}
2961529679
#endif
2961629680
29617
- rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
29681
+ rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
2961829682
isDelete, isReadonly);
2961929683
open_finished:
2962029684
if( rc!=SQLITE_OK ){
2962129685
sqlite3_free(p->pUnused);
2962229686
}
@@ -29634,17 +29698,17 @@
2963429698
int dirSync /* If true, fsync() directory after deleting file */
2963529699
){
2963629700
int rc = SQLITE_OK;
2963729701
UNUSED_PARAMETER(NotUsed);
2963829702
SimulateIOError(return SQLITE_IOERR_DELETE);
29639
- if( unlink(zPath)==(-1) && errno!=ENOENT ){
29703
+ if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
2964029704
return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
2964129705
}
2964229706
#ifndef SQLITE_DISABLE_DIRSYNC
2964329707
if( dirSync ){
2964429708
int fd;
29645
- rc = openDirectory(zPath, &fd);
29709
+ rc = osOpenDirectory(zPath, &fd);
2964629710
if( rc==SQLITE_OK ){
2964729711
#if OS_VXWORKS
2964829712
if( fsync(fd)==-1 )
2964929713
#else
2965029714
if( fsync(fd) )
@@ -29693,11 +29757,11 @@
2969329757
assert(!"Invalid flags argument");
2969429758
}
2969529759
*pResOut = (osAccess(zPath, amode)==0);
2969629760
if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
2969729761
struct stat buf;
29698
- if( 0==stat(zPath, &buf) && buf.st_size==0 ){
29762
+ if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
2969929763
*pResOut = 0;
2970029764
}
2970129765
}
2970229766
return SQLITE_OK;
2970329767
}
@@ -30212,11 +30276,10 @@
3021230276
const char *path, /* path for the new unixFile */
3021330277
unixFile **ppFile, /* unixFile created and returned by ref */
3021430278
int islockfile /* if non zero missing dirs will be created */
3021530279
) {
3021630280
int fd = -1;
30217
- int dirfd = -1;
3021830281
unixFile *pNew;
3021930282
int rc = SQLITE_OK;
3022030283
int openFlags = O_RDWR | O_CREAT;
3022130284
sqlite3_vfs dummyVfs;
3022230285
int terrno = 0;
@@ -30277,11 +30340,11 @@
3027730340
dummyVfs.zName = "dummy";
3027830341
pUnused->fd = fd;
3027930342
pUnused->flags = openFlags;
3028030343
pNew->pUnused = pUnused;
3028130344
30282
- rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
30345
+ rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
3028330346
if( rc==SQLITE_OK ){
3028430347
*ppFile = pNew;
3028530348
return SQLITE_OK;
3028630349
}
3028730350
end_create_proxy:
@@ -30391,11 +30454,11 @@
3039130454
conchFile->openFlags = O_RDWR | O_CREAT;
3039230455
3039330456
end_breaklock:
3039430457
if( rc ){
3039530458
if( fd>=0 ){
30396
- unlink(tPath);
30459
+ osUnlink(tPath);
3039730460
robust_close(pFile, fd, __LINE__);
3039830461
}
3039930462
fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
3040030463
}
3040130464
return rc;
@@ -31214,11 +31277,11 @@
3121431277
};
3121531278
unsigned int i; /* Loop counter */
3121631279
3121731280
/* Double-check that the aSyscall[] array has been constructed
3121831281
** correctly. See ticket [bb3a86e890c8e96ab] */
31219
- assert( ArraySize(aSyscall)==16 );
31282
+ assert( ArraySize(aSyscall)==18 );
3122031283
3122131284
/* Register all VFSes defined in the aVfs[] array */
3122231285
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
3122331286
sqlite3_vfs_register(&aVfs[i], i==0);
3122431287
}
@@ -31331,14 +31394,17 @@
3133131394
#ifdef MEMORY_DEBUG
3133231395
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
3133331396
#endif
3133431397
3133531398
#ifdef SQLITE_DEBUG
31336
-SQLITE_PRIVATE int sqlite3OSTrace = 0;
31337
-#define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
31399
+# ifndef SQLITE_DEBUG_OS_TRACE
31400
+# define SQLITE_DEBUG_OS_TRACE 0
31401
+# endif
31402
+ int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
31403
+# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
3133831404
#else
31339
-#define OSTRACE(X)
31405
+# define OSTRACE(X)
3134031406
#endif
3134131407
3134231408
/*
3134331409
** Macros for performance tracing. Normally turned off. Only works
3134431410
** on i486 hardware.
@@ -31546,12 +31612,13 @@
3154631612
typedef struct winFile winFile;
3154731613
struct winFile {
3154831614
const sqlite3_io_methods *pMethod; /*** Must be first ***/
3154931615
sqlite3_vfs *pVfs; /* The VFS used to open this file */
3155031616
HANDLE h; /* Handle for accessing the file */
31551
- unsigned char locktype; /* Type of lock currently held on this file */
31617
+ u8 locktype; /* Type of lock currently held on this file */
3155231618
short sharedLockByte; /* Randomly chosen byte used as a shared lock */
31619
+ u8 bPersistWal; /* True to persist WAL files */
3155331620
DWORD lastErrno; /* The Windows errno from the last I/O error */
3155431621
DWORD sectorSize; /* Sector size of the device file is on */
3155531622
winShm *pShm; /* Instance of shared memory on this file */
3155631623
const char *zPath; /* Full pathname of this file */
3155731624
int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
@@ -32778,29 +32845,39 @@
3277832845
3277932846
/*
3278032847
** Control and query of the open file handle.
3278132848
*/
3278232849
static int winFileControl(sqlite3_file *id, int op, void *pArg){
32850
+ winFile *pFile = (winFile*)id;
3278332851
switch( op ){
3278432852
case SQLITE_FCNTL_LOCKSTATE: {
32785
- *(int*)pArg = ((winFile*)id)->locktype;
32853
+ *(int*)pArg = pFile->locktype;
3278632854
return SQLITE_OK;
3278732855
}
3278832856
case SQLITE_LAST_ERRNO: {
32789
- *(int*)pArg = (int)((winFile*)id)->lastErrno;
32857
+ *(int*)pArg = (int)pFile->lastErrno;
3279032858
return SQLITE_OK;
3279132859
}
3279232860
case SQLITE_FCNTL_CHUNK_SIZE: {
32793
- ((winFile*)id)->szChunk = *(int *)pArg;
32861
+ pFile->szChunk = *(int *)pArg;
3279432862
return SQLITE_OK;
3279532863
}
3279632864
case SQLITE_FCNTL_SIZE_HINT: {
3279732865
sqlite3_int64 sz = *(sqlite3_int64*)pArg;
3279832866
SimulateIOErrorBenign(1);
3279932867
winTruncate(id, sz);
3280032868
SimulateIOErrorBenign(0);
3280132869
return SQLITE_OK;
32870
+ }
32871
+ case SQLITE_FCNTL_PERSIST_WAL: {
32872
+ int bPersist = *(int*)pArg;
32873
+ if( bPersist<0 ){
32874
+ *(int*)pArg = pFile->bPersistWal;
32875
+ }else{
32876
+ pFile->bPersistWal = bPersist!=0;
32877
+ }
32878
+ return SQLITE_OK;
3280232879
}
3280332880
case SQLITE_FCNTL_SYNC_OMITTED: {
3280432881
return SQLITE_OK;
3280532882
}
3280632883
case SQLITE_FCNTL_WIN32_AV_RETRY: {
@@ -33623,10 +33700,11 @@
3362333700
int isTemp = 0;
3362433701
#endif
3362533702
winFile *pFile = (winFile*)id;
3362633703
void *zConverted; /* Filename in OS encoding */
3362733704
const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
33705
+ int cnt = 0;
3362833706
3362933707
/* If argument zPath is a NULL pointer, this function is required to open
3363033708
** a temporary file. Use this buffer to store the file name in.
3363133709
*/
3363233710
char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */
@@ -33742,34 +33820,34 @@
3374233820
#if SQLITE_OS_WINCE
3374333821
dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
3374433822
#endif
3374533823
3374633824
if( isNT() ){
33747
- h = CreateFileW((WCHAR*)zConverted,
33748
- dwDesiredAccess,
33749
- dwShareMode,
33750
- NULL,
33751
- dwCreationDisposition,
33752
- dwFlagsAndAttributes,
33753
- NULL
33754
- );
33825
+ while( (h = CreateFileW((WCHAR*)zConverted,
33826
+ dwDesiredAccess,
33827
+ dwShareMode, NULL,
33828
+ dwCreationDisposition,
33829
+ dwFlagsAndAttributes,
33830
+ NULL))==INVALID_HANDLE_VALUE &&
33831
+ retryIoerr(&cnt) ){}
3375533832
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
3375633833
** Since the ASCII version of these Windows API do not exist for WINCE,
3375733834
** it's important to not reference them for WINCE builds.
3375833835
*/
3375933836
#if SQLITE_OS_WINCE==0
3376033837
}else{
33761
- h = CreateFileA((char*)zConverted,
33762
- dwDesiredAccess,
33763
- dwShareMode,
33764
- NULL,
33765
- dwCreationDisposition,
33766
- dwFlagsAndAttributes,
33767
- NULL
33768
- );
33838
+ while( (h = CreateFileA((char*)zConverted,
33839
+ dwDesiredAccess,
33840
+ dwShareMode, NULL,
33841
+ dwCreationDisposition,
33842
+ dwFlagsAndAttributes,
33843
+ NULL))==INVALID_HANDLE_VALUE &&
33844
+ retryIoerr(&cnt) ){}
3376933845
#endif
3377033846
}
33847
+
33848
+ logIoerr(cnt);
3377133849
3377233850
OSTRACE(("OPEN %d %s 0x%lx %s\n",
3377333851
h, zName, dwDesiredAccess,
3377433852
h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
3377533853
@@ -33895,15 +33973,17 @@
3389533973
zConverted = convertUtf8Filename(zFilename);
3389633974
if( zConverted==0 ){
3389733975
return SQLITE_NOMEM;
3389833976
}
3389933977
if( isNT() ){
33978
+ int cnt = 0;
3390033979
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3390133980
memset(&sAttrData, 0, sizeof(sAttrData));
33902
- if( GetFileAttributesExW((WCHAR*)zConverted,
33981
+ while( !(rc = GetFileAttributesExW((WCHAR*)zConverted,
3390333982
GetFileExInfoStandard,
33904
- &sAttrData) ){
33983
+ &sAttrData)) && retryIoerr(&cnt) ){}
33984
+ if( rc ){
3390533985
/* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
3390633986
** as if it does not exist.
3390733987
*/
3390833988
if( flags==SQLITE_ACCESS_EXISTS
3390933989
&& sAttrData.nFileSizeHigh==0
@@ -33911,10 +33991,11 @@
3391133991
attr = INVALID_FILE_ATTRIBUTES;
3391233992
}else{
3391333993
attr = sAttrData.dwFileAttributes;
3391433994
}
3391533995
}else{
33996
+ logIoerr(cnt);
3391633997
if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
3391733998
winLogError(SQLITE_IOERR_ACCESS, "winAccess", zFilename);
3391833999
free(zConverted);
3391934000
return SQLITE_IOERR_ACCESS;
3392034001
}else{
@@ -33935,11 +34016,12 @@
3393534016
case SQLITE_ACCESS_READ:
3393634017
case SQLITE_ACCESS_EXISTS:
3393734018
rc = attr!=INVALID_FILE_ATTRIBUTES;
3393834019
break;
3393934020
case SQLITE_ACCESS_READWRITE:
33940
- rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
34021
+ rc = attr!=INVALID_FILE_ATTRIBUTES &&
34022
+ (attr & FILE_ATTRIBUTE_READONLY)==0;
3394134023
break;
3394234024
default:
3394334025
assert(!"Invalid flags argument");
3394434026
}
3394534027
*pResOut = rc;
@@ -45519,17 +45601,19 @@
4551945601
**
4552045602
** The EXCLUSIVE lock is not released before returning.
4552145603
*/
4552245604
rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
4552345605
if( rc==SQLITE_OK ){
45606
+ int bPersistWal = -1;
4552445607
if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
4552545608
pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
4552645609
}
4552745610
rc = sqlite3WalCheckpoint(
4552845611
pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
4552945612
);
45530
- if( rc==SQLITE_OK ){
45613
+ sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersistWal);
45614
+ if( rc==SQLITE_OK && bPersistWal!=1 ){
4553145615
isDelete = 1;
4553245616
}
4553345617
}
4553445618
4553545619
walIndexClose(pWal, isDelete);
@@ -57508,15 +57592,15 @@
5750857592
*ppVal = 0;
5750957593
return SQLITE_OK;
5751057594
}
5751157595
op = pExpr->op;
5751257596
57513
- /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
57597
+ /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
5751457598
** The ifdef here is to enable us to achieve 100% branch test coverage even
57515
- ** when SQLITE_ENABLE_STAT2 is omitted.
57599
+ ** when SQLITE_ENABLE_STAT3 is omitted.
5751657600
*/
57517
-#ifdef SQLITE_ENABLE_STAT2
57601
+#ifdef SQLITE_ENABLE_STAT3
5751857602
if( op==TK_REGISTER ) op = pExpr->op2;
5751957603
#else
5752057604
if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
5752157605
#endif
5752257606
@@ -58208,12 +58292,12 @@
5820858292
/*
5820958293
** Change the P2 operand of instruction addr so that it points to
5821058294
** the address of the next instruction to be coded.
5821158295
*/
5821258296
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58213
- assert( addr>=0 );
58214
- sqlite3VdbeChangeP2(p, addr, p->nOp);
58297
+ assert( addr>=0 || p->db->mallocFailed );
58298
+ if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
5821558299
}
5821658300
5821758301
5821858302
/*
5821958303
** If the input FuncDef structure is ephemeral, then free it. If
@@ -63874,10 +63958,15 @@
6387463958
assert( memIsValid(pIn1) );
6387563959
memAboutToChange(p, pOut);
6387663960
u.ac.zMalloc = pOut->zMalloc;
6387763961
pOut->zMalloc = 0;
6387863962
sqlite3VdbeMemMove(pOut, pIn1);
63963
+#ifdef SQLITE_DEBUG
63964
+ if( pOut->pScopyFrom>=&aMem[u.ac.p1] && pOut->pScopyFrom<&aMem[u.ac.p1+pOp->p3] ){
63965
+ pOut->pScopyFrom += u.ac.p1 - pOp->p2;
63966
+ }
63967
+#endif
6387963968
pIn1->zMalloc = u.ac.zMalloc;
6388063969
REGISTER_TRACE(u.ac.p2++, pOut);
6388163970
pIn1++;
6388263971
pOut++;
6388363972
}
@@ -75923,10 +76012,100 @@
7592376012
** May you find forgiveness for yourself and forgive others.
7592476013
** May you share freely, never taking more than you give.
7592576014
**
7592676015
*************************************************************************
7592776016
** 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.
7592876107
*/
7592976108
#ifndef SQLITE_OMIT_ANALYZE
7593076109
7593176110
/*
7593276111
** This routine generates code that opens the sqlite_stat1 table for
@@ -75954,12 +76133,18 @@
7595476133
static const struct {
7595576134
const char *zName;
7595676135
const char *zCols;
7595776136
} aTable[] = {
7595876137
{ "sqlite_stat1", "tbl,idx,stat" },
75959
-#ifdef SQLITE_ENABLE_STAT2
75960
- { "sqlite_stat2", "tbl,idx,sampleno,sample" },
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",
7596176146
#endif
7596276147
};
7596376148
7596476149
int aRoot[] = {0, 0};
7596576150
u8 aCreateTbl[] = {0, 0};
@@ -75971,10 +76156,21 @@
7597176156
if( v==0 ) return;
7597276157
assert( sqlite3BtreeHoldsAllMutexes(db) );
7597376158
assert( sqlite3VdbeDb(v)==db );
7597476159
pDb = &db->aDb[iDb];
7597576160
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
+ */
7597676172
for(i=0; i<ArraySize(aTable); i++){
7597776173
const char *zTab = aTable[i].zName;
7597876174
Table *pStat;
7597976175
if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
7598076176
/* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -76001,17 +76197,238 @@
7600176197
sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
7600276198
}
7600376199
}
7600476200
}
7600576201
76006
- /* Open the sqlite_stat[12] tables for writing. */
76202
+ /* Open the sqlite_stat[13] tables for writing. */
7600776203
for(i=0; i<ArraySize(aTable); i++){
7600876204
sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
7600976205
sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
7601076206
sqlite3VdbeChangeP5(v, aCreateTbl[i]);
7601176207
}
7601276208
}
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
+
7601376430
7601476431
/*
7601576432
** Generate code to do an analysis of all indices associated with
7601676433
** a single table.
7601776434
*/
@@ -76031,24 +76448,31 @@
7603176448
int endOfLoop; /* The end of the loop */
7603276449
int jZeroRows = -1; /* Jump from here if number of rows is zero */
7603376450
int iDb; /* Index of database containing pTab */
7603476451
int regTabname = iMem++; /* Register containing table name */
7603576452
int regIdxname = iMem++; /* Register containing index name */
76036
- int regSampleno = iMem++; /* Register containing next sample number */
76037
- int regCol = iMem++; /* Content of a column analyzed table */
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 */
7603876470
int regRec = iMem++; /* Register holding completed record */
7603976471
int regTemp = iMem++; /* Temporary use register */
76040
- int regRowid = iMem++; /* Rowid for the inserted record */
76041
-
76042
-#ifdef SQLITE_ENABLE_STAT2
76043
- int addr = 0; /* Instruction address */
76044
- int regTemp2 = iMem++; /* Temporary use register */
76045
- int regSamplerecno = iMem++; /* Index of next sample to record */
76046
- int regRecno = iMem++; /* Current sample index */
76047
- int regLast = iMem++; /* Index of last sample to record */
76048
- int regFirst = iMem++; /* Index of first sample to record */
76049
-#endif
76472
+ int regNewRowid = iMem++; /* Rowid for the inserted record */
76473
+
7605076474
7605176475
v = sqlite3GetVdbe(pParse);
7605276476
if( v==0 || NEVER(pTab==0) ){
7605376477
return;
7605476478
}
@@ -76077,17 +76501,22 @@
7607776501
iIdxCur = pParse->nTab++;
7607876502
sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
7607976503
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
7608076504
int nCol;
7608176505
KeyInfo *pKey;
76506
+ int addrIfNot = 0; /* address of OP_IfNot */
76507
+ int *aChngAddr; /* Array of jump instruction addresses */
7608276508
7608376509
if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
76510
+ VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
7608476511
nCol = pIdx->nColumn;
7608576512
pKey = sqlite3IndexKeyinfo(pParse, pIdx);
7608676513
if( iMem+1+(nCol*2)>pParse->nMem ){
7608776514
pParse->nMem = iMem+1+(nCol*2);
7608876515
}
76516
+ aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*pIdx->nColumn);
76517
+ if( aChngAddr==0 ) continue;
7608976518
7609076519
/* Open a cursor to the index to be analyzed. */
7609176520
assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
7609276521
sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
7609376522
(char *)pKey, P4_KEYINFO_HANDOFF);
@@ -76094,35 +76523,24 @@
7609476523
VdbeComment((v, "%s", pIdx->zName));
7609576524
7609676525
/* Populate the register containing the index name. */
7609776526
sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
7609876527
76099
-#ifdef SQLITE_ENABLE_STAT2
76100
-
76101
- /* If this iteration of the loop is generating code to analyze the
76102
- ** first index in the pTab->pIndex list, then register regLast has
76103
- ** not been populated. In this case populate it now. */
76104
- if( pTab->pIndex==pIdx ){
76105
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
76106
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
76107
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
76108
-
76109
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
76110
- sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
76111
- addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
76112
- sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
76113
- sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
76114
- sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
76115
- sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
76116
- sqlite3VdbeJumpHere(v, addr);
76117
- }
76118
-
76119
- /* Zero the regSampleno and regRecno registers. */
76120
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
76121
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
76122
- sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
76123
-#endif
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 */
7612476542
7612576543
/* The block of memory cells initialized here is used as follows.
7612676544
**
7612776545
** iMem:
7612876546
** The total number of rows in the table.
@@ -76148,79 +76566,87 @@
7614876566
/* Start the analysis loop. This loop runs through all the entries in
7614976567
** the index b-tree. */
7615076568
endOfLoop = sqlite3VdbeMakeLabel(v);
7615176569
sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
7615276570
topOfLoop = sqlite3VdbeCurrentAddr(v);
76153
- sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
76571
+ sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
7615476572
7615576573
for(i=0; i<nCol; i++){
7615676574
CollSeq *pColl;
7615776575
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
7615876576
if( i==0 ){
76159
-#ifdef SQLITE_ENABLE_STAT2
76160
- /* Check if the record that cursor iIdxCur points to contains a
76161
- ** value that should be stored in the sqlite_stat2 table. If so,
76162
- ** store it. */
76163
- int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
76164
- assert( regTabname+1==regIdxname
76165
- && regTabname+2==regSampleno
76166
- && regTabname+3==regCol
76167
- );
76168
- sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
76169
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
76170
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
76171
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
76172
-
76173
- /* Calculate new values for regSamplerecno and regSampleno.
76174
- **
76175
- ** sampleno = sampleno + 1
76176
- ** samplerecno = samplerecno+(remaining records)/(remaining samples)
76177
- */
76178
- sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
76179
- sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
76180
- sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
76181
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
76182
- sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
76183
- sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
76184
- sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
76185
-
76186
- sqlite3VdbeJumpHere(v, ne);
76187
- sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
76188
-#endif
76189
-
7619076577
/* Always record the very first row */
76191
- sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
76578
+ addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
7619276579
}
7619376580
assert( pIdx->azColl!=0 );
7619476581
assert( pIdx->azColl[i]!=0 );
7619576582
pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
76196
- sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
76197
- (char*)pColl, P4_COLLSEQ);
76583
+ aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
76584
+ (char*)pColl, P4_COLLSEQ);
7619876585
sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
76199
- }
76200
- if( db->mallocFailed ){
76201
- /* If a malloc failure has occurred, then the result of the expression
76202
- ** passed as the second argument to the call to sqlite3VdbeJumpHere()
76203
- ** below may be negative. Which causes an assert() to fail (or an
76204
- ** out-of-bounds write if SQLITE_DEBUG is not defined). */
76205
- return;
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
7620676593
}
7620776594
sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
7620876595
for(i=0; i<nCol; i++){
76209
- int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
76596
+ sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
7621076597
if( i==0 ){
76211
- sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
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
7621276608
}
76213
- sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
7621476609
sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
7621576610
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
7621676611
}
76612
+ sqlite3DbFree(db, aChngAddr);
7621776613
76218
- /* End of the analysis loop. */
76614
+ /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
7621976615
sqlite3VdbeResolveLabel(v, endOfLoop);
76616
+
7622076617
sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
7622176618
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
7622276648
7622376649
/* Store the results in sqlite_stat1.
7622476650
**
7622576651
** The result is a single row of the sqlite_stat1 table. The first
7622676652
** two columns are the names of the table and index. The third column
@@ -76236,50 +76662,51 @@
7623676662
**
7623776663
** If K==0 then no entry is made into the sqlite_stat1 table.
7623876664
** If K>0 then it is always the case the D>0 so division by zero
7623976665
** is never possible.
7624076666
*/
76241
- sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
76667
+ sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
7624276668
if( jZeroRows<0 ){
7624376669
jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
7624476670
}
7624576671
for(i=0; i<nCol; i++){
7624676672
sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
76247
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
76673
+ sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
7624876674
sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
7624976675
sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
7625076676
sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
7625176677
sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
76252
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
76678
+ sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
7625376679
}
7625476680
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76255
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
76256
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
76681
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76682
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
7625776683
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7625876684
}
7625976685
7626076686
/* If the table has no indices, create a single sqlite_stat1 entry
7626176687
** containing NULL as the index name and the row count as the content.
7626276688
*/
7626376689
if( pTab->pIndex==0 ){
7626476690
sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
7626576691
VdbeComment((v, "%s", pTab->zName));
76266
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
76692
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
7626776693
sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76268
- jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
76694
+ jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
7626976695
}else{
7627076696
sqlite3VdbeJumpHere(v, jZeroRows);
7627176697
jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
7627276698
}
7627376699
sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
7627476700
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76275
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
76276
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
76701
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76702
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
7627776703
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7627876704
if( pParse->nMem<regRec ) pParse->nMem = regRec;
7627976705
sqlite3VdbeJumpHere(v, jZeroRows);
7628076706
}
76707
+
7628176708
7628276709
/*
7628376710
** Generate code that will cause the most recent index analysis to
7628476711
** be loaded into internal hash tables where is can be used.
7628576712
*/
@@ -76300,11 +76727,11 @@
7630076727
int iStatCur;
7630176728
int iMem;
7630276729
7630376730
sqlite3BeginWriteOperation(pParse, 0, iDb);
7630476731
iStatCur = pParse->nTab;
76305
- pParse->nTab += 2;
76732
+ pParse->nTab += 3;
7630676733
openStatTable(pParse, iDb, iStatCur, 0, 0);
7630776734
iMem = pParse->nMem+1;
7630876735
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
7630976736
for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
7631076737
Table *pTab = (Table*)sqliteHashData(k);
@@ -76325,11 +76752,11 @@
7632576752
assert( pTab!=0 );
7632676753
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
7632776754
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
7632876755
sqlite3BeginWriteOperation(pParse, 0, iDb);
7632976756
iStatCur = pParse->nTab;
76330
- pParse->nTab += 2;
76757
+ pParse->nTab += 3;
7633176758
if( pOnlyIdx ){
7633276759
openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
7633376760
}else{
7633476761
openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
7633576762
}
@@ -76430,11 +76857,11 @@
7643076857
static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
7643176858
analysisInfo *pInfo = (analysisInfo*)pData;
7643276859
Index *pIndex;
7643376860
Table *pTable;
7643476861
int i, c, n;
76435
- unsigned int v;
76862
+ tRowcnt v;
7643676863
const char *z;
7643776864
7643876865
assert( argc==3 );
7643976866
UNUSED_PARAMETER2(NotUsed, argc);
7644076867
@@ -76473,40 +76900,172 @@
7647376900
/*
7647476901
** If the Index.aSample variable is not NULL, delete the aSample[] array
7647576902
** and its contents.
7647676903
*/
7647776904
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
76478
-#ifdef SQLITE_ENABLE_STAT2
76905
+#ifdef SQLITE_ENABLE_STAT3
7647976906
if( pIdx->aSample ){
7648076907
int j;
76481
- for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
76908
+ for(j=0; j<pIdx->nSample; j++){
7648276909
IndexSample *p = &pIdx->aSample[j];
7648376910
if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
76484
- sqlite3DbFree(db, p->u.z);
76911
+ sqlite3_free(p->u.z);
7648576912
}
7648676913
}
76487
- sqlite3DbFree(db, pIdx->aSample);
76914
+ sqlite3_free(pIdx->aSample);
7648876915
}
76916
+ UNUSED_PARAMETER(db);
76917
+ pIdx->nSample = 0;
76918
+ pIdx->aSample = 0;
7648976919
#else
7649076920
UNUSED_PARAMETER(db);
7649176921
UNUSED_PARAMETER(pIdx);
7649276922
#endif
7649376923
}
7649476924
76925
+#ifdef SQLITE_ENABLE_STAT3
7649576926
/*
76496
-** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
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
7649777056
** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
76498
-** arrays. The contents of sqlite_stat2 are used to populate the
77057
+** arrays. The contents of sqlite_stat3 are used to populate the
7649977058
** Index.aSample[] arrays.
7650077059
**
7650177060
** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
76502
-** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
76503
-** during compilation and the sqlite_stat2 table is present, no data is
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
7650477063
** read from it.
7650577064
**
76506
-** If SQLITE_ENABLE_STAT2 was defined during compilation and the
76507
-** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
77065
+** If SQLITE_ENABLE_STAT3 was defined during compilation and the
77066
+** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
7650877067
** returned. However, in this case, data is read from the sqlite_stat1
7650977068
** table (if it is present) before returning.
7651077069
**
7651177070
** If an OOM error occurs, this function always sets db->mallocFailed.
7651277071
** This means if the caller does not care about other errors, the return
@@ -76524,12 +77083,14 @@
7652477083
/* Clear any prior statistics */
7652577084
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
7652677085
for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
7652777086
Index *pIdx = sqliteHashData(i);
7652877087
sqlite3DefaultRowEst(pIdx);
77088
+#ifdef SQLITE_ENABLE_STAT3
7652977089
sqlite3DeleteIndexSamples(db, pIdx);
7653077090
pIdx->aSample = 0;
77091
+#endif
7653177092
}
7653277093
7653377094
/* Check to make sure the sqlite_stat1 table exists */
7653477095
sInfo.db = db;
7653577096
sInfo.zDatabase = db->aDb[iDb].zName;
@@ -76537,91 +77098,23 @@
7653777098
return SQLITE_ERROR;
7653877099
}
7653977100
7654077101
/* Load new statistics out of the sqlite_stat1 table */
7654177102
zSql = sqlite3MPrintf(db,
76542
- "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
77103
+ "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
7654377104
if( zSql==0 ){
7654477105
rc = SQLITE_NOMEM;
7654577106
}else{
7654677107
rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
7654777108
sqlite3DbFree(db, zSql);
7654877109
}
7654977110
7655077111
76551
- /* Load the statistics from the sqlite_stat2 table. */
76552
-#ifdef SQLITE_ENABLE_STAT2
76553
- if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
76554
- rc = SQLITE_ERROR;
76555
- }
76556
- if( rc==SQLITE_OK ){
76557
- sqlite3_stmt *pStmt = 0;
76558
-
76559
- zSql = sqlite3MPrintf(db,
76560
- "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
76561
- if( !zSql ){
76562
- rc = SQLITE_NOMEM;
76563
- }else{
76564
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76565
- sqlite3DbFree(db, zSql);
76566
- }
76567
-
76568
- if( rc==SQLITE_OK ){
76569
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
76570
- char *zIndex; /* Index name */
76571
- Index *pIdx; /* Pointer to the index object */
76572
-
76573
- zIndex = (char *)sqlite3_column_text(pStmt, 0);
76574
- pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
76575
- if( pIdx ){
76576
- int iSample = sqlite3_column_int(pStmt, 1);
76577
- if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
76578
- int eType = sqlite3_column_type(pStmt, 2);
76579
-
76580
- if( pIdx->aSample==0 ){
76581
- static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
76582
- pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
76583
- if( pIdx->aSample==0 ){
76584
- db->mallocFailed = 1;
76585
- break;
76586
- }
76587
- memset(pIdx->aSample, 0, sz);
76588
- }
76589
-
76590
- assert( pIdx->aSample );
76591
- {
76592
- IndexSample *pSample = &pIdx->aSample[iSample];
76593
- pSample->eType = (u8)eType;
76594
- if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
76595
- pSample->u.r = sqlite3_column_double(pStmt, 2);
76596
- }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
76597
- const char *z = (const char *)(
76598
- (eType==SQLITE_BLOB) ?
76599
- sqlite3_column_blob(pStmt, 2):
76600
- sqlite3_column_text(pStmt, 2)
76601
- );
76602
- int n = sqlite3_column_bytes(pStmt, 2);
76603
- if( n>24 ){
76604
- n = 24;
76605
- }
76606
- pSample->nByte = (u8)n;
76607
- if( n < 1){
76608
- pSample->u.z = 0;
76609
- }else{
76610
- pSample->u.z = sqlite3DbStrNDup(0, z, n);
76611
- if( pSample->u.z==0 ){
76612
- db->mallocFailed = 1;
76613
- break;
76614
- }
76615
- }
76616
- }
76617
- }
76618
- }
76619
- }
76620
- }
76621
- rc = sqlite3_finalize(pStmt);
76622
- }
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);
7662377116
}
7662477117
#endif
7662577118
7662677119
if( rc==SQLITE_NOMEM ){
7662777120
db->mallocFailed = 1;
@@ -79422,10 +79915,108 @@
7942279915
iDestroyed = iLargest;
7942379916
}
7942479917
}
7942579918
#endif
7942679919
}
79920
+
79921
+/*
79922
+** Remove entries from the sqlite_stat1 and sqlite_stat2 tables
79923
+** after a DROP INDEX or DROP TABLE command.
79924
+*/
79925
+static void sqlite3ClearStatTables(
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
+ "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
+}
7942780018
7942880019
/*
7942980020
** This routine is called to do the work of a DROP TABLE statement.
7943080021
** pName is the name of the table to be dropped.
7943180022
*/
@@ -79491,11 +80082,11 @@
7949180082
if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
7949280083
goto exit_drop_table;
7949380084
}
7949480085
}
7949580086
#endif
79496
- if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
80087
+ if( !pParse->nested && sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
7949780088
sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
7949880089
goto exit_drop_table;
7949980090
}
7950080091
7950180092
#ifndef SQLITE_OMIT_VIEW
@@ -79515,79 +80106,15 @@
7951580106
/* Generate code to remove the table from the master table
7951680107
** on disk.
7951780108
*/
7951880109
v = sqlite3GetVdbe(pParse);
7951980110
if( v ){
79520
- Trigger *pTrigger;
79521
- Db *pDb = &db->aDb[iDb];
7952280111
sqlite3BeginWriteOperation(pParse, 1, iDb);
79523
-
79524
-#ifndef SQLITE_OMIT_VIRTUALTABLE
79525
- if( IsVirtual(pTab) ){
79526
- sqlite3VdbeAddOp0(v, OP_VBegin);
79527
- }
79528
-#endif
7952980112
sqlite3FkDropTable(pParse, pName, pTab);
79530
-
79531
- /* Drop all triggers associated with the table being dropped. Code
79532
- ** is generated to remove entries from sqlite_master and/or
79533
- ** sqlite_temp_master if required.
79534
- */
79535
- pTrigger = sqlite3TriggerList(pParse, pTab);
79536
- while( pTrigger ){
79537
- assert( pTrigger->pSchema==pTab->pSchema ||
79538
- pTrigger->pSchema==db->aDb[1].pSchema );
79539
- sqlite3DropTriggerPtr(pParse, pTrigger);
79540
- pTrigger = pTrigger->pNext;
79541
- }
79542
-
79543
-#ifndef SQLITE_OMIT_AUTOINCREMENT
79544
- /* Remove any entries of the sqlite_sequence table associated with
79545
- ** the table being dropped. This is done before the table is dropped
79546
- ** at the btree level, in case the sqlite_sequence table needs to
79547
- ** move as a result of the drop (can happen in auto-vacuum mode).
79548
- */
79549
- if( pTab->tabFlags & TF_Autoincrement ){
79550
- sqlite3NestedParse(pParse,
79551
- "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
79552
- pDb->zName, pTab->zName
79553
- );
79554
- }
79555
-#endif
79556
-
79557
- /* Drop all SQLITE_MASTER table and index entries that refer to the
79558
- ** table. The program name loops through the master table and deletes
79559
- ** every row that refers to a table of the same name as the one being
79560
- ** dropped. Triggers are handled seperately because a trigger can be
79561
- ** created in the temp database that refers to a table in another
79562
- ** database.
79563
- */
79564
- sqlite3NestedParse(pParse,
79565
- "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
79566
- pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
79567
-
79568
- /* Drop any statistics from the sqlite_stat1 table, if it exists */
79569
- if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
79570
- sqlite3NestedParse(pParse,
79571
- "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb->zName, pTab->zName
79572
- );
79573
- }
79574
-
79575
- if( !isView && !IsVirtual(pTab) ){
79576
- destroyTable(pParse, pTab);
79577
- }
79578
-
79579
- /* Remove the table entry from SQLite's internal schema and modify
79580
- ** the schema cookie.
79581
- */
79582
- if( IsVirtual(pTab) ){
79583
- sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
79584
- }
79585
- sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
79586
- sqlite3ChangeCookie(pParse, iDb);
79587
- }
79588
- sqliteViewResetAll(db, iDb);
80113
+ sqlite3CodeDropTable(pParse, pTab, iDb, isView);
80114
+ sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
80115
+ }
7958980116
7959080117
exit_drop_table:
7959180118
sqlite3SrcListDelete(db, pName);
7959280119
}
7959380120
@@ -80030,24 +80557,24 @@
8003080557
*/
8003180558
nName = sqlite3Strlen30(zName);
8003280559
nCol = pList->nExpr;
8003380560
pIndex = sqlite3DbMallocZero(db,
8003480561
sizeof(Index) + /* Index structure */
80562
+ sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
8003580563
sizeof(int)*nCol + /* Index.aiColumn */
80036
- sizeof(int)*(nCol+1) + /* Index.aiRowEst */
8003780564
sizeof(char *)*nCol + /* Index.azColl */
8003880565
sizeof(u8)*nCol + /* Index.aSortOrder */
8003980566
nName + 1 + /* Index.zName */
8004080567
nExtra /* Collation sequence names */
8004180568
);
8004280569
if( db->mallocFailed ){
8004380570
goto exit_create_index;
8004480571
}
80045
- pIndex->azColl = (char**)(&pIndex[1]);
80572
+ pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
80573
+ pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
8004680574
pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
80047
- pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
80048
- pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
80575
+ pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
8004980576
pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
8005080577
zExtra = (char *)(&pIndex->zName[nName+1]);
8005180578
memcpy(pIndex->zName, zName, nName+1);
8005280579
pIndex->pTable = pTab;
8005380580
pIndex->nColumn = pList->nExpr;
@@ -80320,13 +80847,13 @@
8032080847
** Apart from that, we have little to go on besides intuition as to
8032180848
** how aiRowEst[] should be initialized. The numbers generated here
8032280849
** are based on typical values found in actual indices.
8032380850
*/
8032480851
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
80325
- unsigned *a = pIdx->aiRowEst;
80852
+ tRowcnt *a = pIdx->aiRowEst;
8032680853
int i;
80327
- unsigned n;
80854
+ tRowcnt n;
8032880855
assert( a!=0 );
8032980856
a[0] = pIdx->pTable->nRowEst;
8033080857
if( a[0]<10 ) a[0] = 10;
8033180858
n = 10;
8033280859
for(i=1; i<=pIdx->nColumn; i++){
@@ -80392,19 +80919,13 @@
8039280919
v = sqlite3GetVdbe(pParse);
8039380920
if( v ){
8039480921
sqlite3BeginWriteOperation(pParse, 1, iDb);
8039580922
sqlite3NestedParse(pParse,
8039680923
"DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
80397
- db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
80398
- pIndex->zName
80399
- );
80400
- if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
80401
- sqlite3NestedParse(pParse,
80402
- "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q",
80403
- db->aDb[iDb].zName, pIndex->zName
80404
- );
80405
- }
80924
+ db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
80925
+ );
80926
+ sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
8040680927
sqlite3ChangeCookie(pParse, iDb);
8040780928
destroyRootPage(pParse, pIndex->tnum, iDb);
8040880929
sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
8040980930
}
8041080931
@@ -98638,11 +99159,11 @@
9863899159
#define TERM_CODED 0x04 /* This term is already coded */
9863999160
#define TERM_COPIED 0x08 /* Has a child */
9864099161
#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
9864199162
#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
9864299163
#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
98643
-#ifdef SQLITE_ENABLE_STAT2
99164
+#ifdef SQLITE_ENABLE_STAT3
9864499165
# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
9864599166
#else
9864699167
# define TERM_VNULL 0x00 /* Disabled if not using stat2 */
9864799168
#endif
9864899169
@@ -99852,11 +100373,11 @@
99852100373
pNewTerm->prereqAll = pTerm->prereqAll;
99853100374
}
99854100375
}
99855100376
#endif /* SQLITE_OMIT_VIRTUALTABLE */
99856100377
99857
-#ifdef SQLITE_ENABLE_STAT2
100378
+#ifdef SQLITE_ENABLE_STAT3
99858100379
/* When sqlite_stat2 histogram data is available an operator of the
99859100380
** form "x IS NOT NULL" can sometimes be evaluated more efficiently
99860100381
** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
99861100382
** virtual term of that form.
99862100383
**
@@ -99891,11 +100412,11 @@
99891100412
pTerm->nChild = 1;
99892100413
pTerm->wtFlags |= TERM_COPIED;
99893100414
pNewTerm->prereqAll = pTerm->prereqAll;
99894100415
}
99895100416
}
99896
-#endif /* SQLITE_ENABLE_STAT2 */
100417
+#endif /* SQLITE_ENABLE_STAT */
99897100418
99898100419
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
99899100420
** an index for tables to the left of the join.
99900100421
*/
99901100422
pTerm->prereqRight |= extraRight;
@@ -100662,10 +101183,11 @@
100662101183
if( pTerm->leftCursor != pSrc->iCursor ) continue;
100663101184
assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
100664101185
testcase( pTerm->eOperator==WO_IN );
100665101186
testcase( pTerm->eOperator==WO_ISNULL );
100666101187
if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
101188
+ if( pTerm->wtFlags & TERM_VNULL ) continue;
100667101189
nTerm++;
100668101190
}
100669101191
100670101192
/* If the ORDER BY clause contains only columns in the current
100671101193
** virtual table then allocate space for the aOrderBy part of
@@ -100712,10 +101234,11 @@
100712101234
if( pTerm->leftCursor != pSrc->iCursor ) continue;
100713101235
assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
100714101236
testcase( pTerm->eOperator==WO_IN );
100715101237
testcase( pTerm->eOperator==WO_ISNULL );
100716101238
if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
101239
+ if( pTerm->wtFlags & TERM_VNULL ) continue;
100717101240
pIdxCons[j].iColumn = pTerm->u.leftColumn;
100718101241
pIdxCons[j].iTermOffset = i;
100719101242
pIdxCons[j].op = (u8)pTerm->eOperator;
100720101243
/* The direct assignment in the previous line is possible only because
100721101244
** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
@@ -100938,71 +101461,89 @@
100938101461
*/
100939101462
bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
100940101463
}
100941101464
#endif /* SQLITE_OMIT_VIRTUALTABLE */
100942101465
101466
+#ifdef SQLITE_ENABLE_STAT3
100943101467
/*
100944
-** Argument pIdx is a pointer to an index structure that has an array of
100945
-** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
100946
-** stored in Index.aSample. These samples divide the domain of values stored
100947
-** the index into (SQLITE_INDEX_SAMPLES+1) regions.
100948
-** Region 0 contains all values less than the first sample value. Region
100949
-** 1 contains values between the first and second samples. Region 2 contains
100950
-** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
100951
-** contains values larger than the last sample.
100952
-**
100953
-** If the index contains many duplicates of a single value, then it is
100954
-** possible that two or more adjacent samples can hold the same value.
100955
-** When that is the case, the smallest possible region code is returned
100956
-** when roundUp is false and the largest possible region code is returned
100957
-** when roundUp is true.
100958
-**
100959
-** If successful, this function determines which of the regions value
100960
-** pVal lies in, sets *piRegion to the region index (a value between 0
100961
-** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
100962
-** Or, if an OOM occurs while converting text values between encodings,
100963
-** SQLITE_NOMEM is returned and *piRegion is undefined.
100964
-*/
100965
-#ifdef SQLITE_ENABLE_STAT2
100966
-static int whereRangeRegion(
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(
100967101477
Parse *pParse, /* Database connection */
100968101478
Index *pIdx, /* Index to consider domain of */
100969101479
sqlite3_value *pVal, /* Value to consider */
100970
- int roundUp, /* Return largest valid region if true */
100971
- int *piRegion /* OUT: Region of domain in which value lies */
101480
+ int roundUp, /* Round up if true. Round down if false */
101481
+ tRowcnt *aStat /* OUT: stats written here */
100972101482
){
101483
+ tRowcnt n;
101484
+ IndexSample *aSample;
101485
+ int i, eType;
101486
+ int isEq = 0;
101487
+ i64 v;
101488
+ double r, rS;
101489
+
100973101490
assert( roundUp==0 || roundUp==1 );
100974
- if( ALWAYS(pVal) ){
100975
- IndexSample *aSample = pIdx->aSample;
100976
- int i = 0;
100977
- int eType = sqlite3_value_type(pVal);
100978
-
100979
- if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
100980
- double r = sqlite3_value_double(pVal);
100981
- for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
100982
- if( aSample[i].eType==SQLITE_NULL ) continue;
100983
- if( aSample[i].eType>=SQLITE_TEXT ) break;
100984
- if( roundUp ){
100985
- if( aSample[i].u.r>r ) break;
100986
- }else{
100987
- if( aSample[i].u.r>=r ) break;
100988
- }
100989
- }
100990
- }else if( eType==SQLITE_NULL ){
100991
- i = 0;
100992
- if( roundUp ){
100993
- while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
100994
- }
100995
- }else{
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 ){
100996101542
sqlite3 *db = pParse->db;
100997101543
CollSeq *pColl;
100998101544
const u8 *z;
100999
- int n;
101000
-
101001
- /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
101002
- assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
101003
-
101004101545
if( eType==SQLITE_BLOB ){
101005101546
z = (const u8 *)sqlite3_value_blob(pVal);
101006101547
pColl = db->pDfltColl;
101007101548
assert( pColl->enc==SQLITE_UTF8 );
101008101549
}else{
@@ -101017,16 +101558,16 @@
101017101558
return SQLITE_NOMEM;
101018101559
}
101019101560
assert( z && pColl && pColl->xCmp );
101020101561
}
101021101562
n = sqlite3ValueBytes(pVal, pColl->enc);
101022
-
101023
- for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
101563
+
101564
+ for(; i<pIdx->nSample; i++){
101024101565
int c;
101025101566
int eSampletype = aSample[i].eType;
101026
- if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
101027
- if( (eSampletype!=eType) ) break;
101567
+ if( eSampletype<eType ) continue;
101568
+ if( eSampletype!=eType ) break;
101028101569
#ifndef SQLITE_OMIT_UTF16
101029101570
if( pColl->enc!=SQLITE_UTF8 ){
101030101571
int nSample;
101031101572
char *zSample = sqlite3Utf8to16(
101032101573
db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -101040,20 +101581,52 @@
101040101581
}else
101041101582
#endif
101042101583
{
101043101584
c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
101044101585
}
101045
- if( c-roundUp>=0 ) break;
101586
+ if( c>=0 ){
101587
+ if( c==0 ) isEq = 1;
101588
+ break;
101589
+ }
101046101590
}
101047101591
}
101592
+ }
101048101593
101049
- assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
101050
- *piRegion = i;
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;
101051101624
}
101052101625
return SQLITE_OK;
101053101626
}
101054
-#endif /* #ifdef SQLITE_ENABLE_STAT2 */
101627
+#endif /* SQLITE_ENABLE_STAT3 */
101055101628
101056101629
/*
101057101630
** If expression pExpr represents a literal value, set *pp to point to
101058101631
** an sqlite3_value structure containing the same value, with affinity
101059101632
** aff applied to it, before returning. It is the responsibility of the
@@ -101067,11 +101640,11 @@
101067101640
**
101068101641
** If neither of the above apply, set *pp to NULL.
101069101642
**
101070101643
** If an error occurs, return an error code. Otherwise, SQLITE_OK.
101071101644
*/
101072
-#ifdef SQLITE_ENABLE_STAT2
101645
+#ifdef SQLITE_ENABLE_STAT3
101073101646
static int valueFromExpr(
101074101647
Parse *pParse,
101075101648
Expr *pExpr,
101076101649
u8 aff,
101077101650
sqlite3_value **pp
@@ -101115,106 +101688,92 @@
101115101688
**
101116101689
** ... FROM t1 WHERE a > ? AND a < ? ...
101117101690
**
101118101691
** then nEq should be passed 0.
101119101692
**
101120
-** The returned value is an integer between 1 and 100, inclusive. A return
101121
-** value of 1 indicates that the proposed range scan is expected to visit
101122
-** approximately 1/100th (1%) of the rows selected by the nEq equality
101123
-** constraints (if any). A return value of 100 indicates that it is expected
101124
-** that the range scan will visit every row (100%) selected by the equality
101125
-** constraints.
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...
101126101697
**
101127
-** In the absence of sqlite_stat2 ANALYZE data, each range inequality
101128
-** reduces the search space by 3/4ths. Hence a single constraint (x>?)
101129
-** results in a return of 25 and a range constraint (x>? AND x<?) results
101130
-** in a return of 6.
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.
101131101702
*/
101132101703
static int whereRangeScanEst(
101133101704
Parse *pParse, /* Parsing & code generating context */
101134101705
Index *p, /* The index containing the range-compared column; "x" */
101135101706
int nEq, /* index into p->aCol[] of the range-compared column */
101136101707
WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
101137101708
WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
101138
- int *piEst /* OUT: Return value */
101709
+ double *pRangeDiv /* OUT: Reduce search space by this divisor */
101139101710
){
101140101711
int rc = SQLITE_OK;
101141101712
101142
-#ifdef SQLITE_ENABLE_STAT2
101143
-
101144
- if( nEq==0 && p->aSample ){
101145
- sqlite3_value *pLowerVal = 0;
101146
- sqlite3_value *pUpperVal = 0;
101147
- int iEst;
101148
- int iLower = 0;
101149
- int iUpper = SQLITE_INDEX_SAMPLES;
101150
- int roundUpUpper = 0;
101151
- int roundUpLower = 0;
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];
101152101720
u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101153101721
101154101722
if( pLower ){
101155101723
Expr *pExpr = pLower->pExpr->pRight;
101156
- rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
101724
+ rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
101157101725
assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
101158
- roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
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);
101159101733
}
101160101734
if( rc==SQLITE_OK && pUpper ){
101161101735
Expr *pExpr = pUpper->pExpr->pRight;
101162
- rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
101736
+ rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
101163101737
assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
101164
- roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
101165
- }
101166
-
101167
- if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
101168
- sqlite3ValueFree(pLowerVal);
101169
- sqlite3ValueFree(pUpperVal);
101170
- goto range_est_fallback;
101171
- }else if( pLowerVal==0 ){
101172
- rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
101173
- if( pLower ) iLower = iUpper/2;
101174
- }else if( pUpperVal==0 ){
101175
- rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
101176
- if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
101177
- }else{
101178
- rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
101179
- if( rc==SQLITE_OK ){
101180
- rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
101181
- }
101182
- }
101183
- WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
101184
-
101185
- iEst = iUpper - iLower;
101186
- testcase( iEst==SQLITE_INDEX_SAMPLES );
101187
- assert( iEst<=SQLITE_INDEX_SAMPLES );
101188
- if( iEst<1 ){
101189
- *piEst = 50/SQLITE_INDEX_SAMPLES;
101190
- }else{
101191
- *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
101192
- }
101193
- sqlite3ValueFree(pLowerVal);
101194
- sqlite3ValueFree(pUpperVal);
101195
- return rc;
101196
- }
101197
-range_est_fallback:
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
+ }
101198101757
#else
101199101758
UNUSED_PARAMETER(pParse);
101200101759
UNUSED_PARAMETER(p);
101201101760
UNUSED_PARAMETER(nEq);
101202101761
#endif
101203101762
assert( pLower || pUpper );
101204
- *piEst = 100;
101205
- if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
101206
- if( pUpper ) *piEst /= 4;
101763
+ *pRangeDiv = (double)1;
101764
+ if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
101765
+ if( pUpper ) *pRangeDiv *= (double)4;
101207101766
return rc;
101208101767
}
101209101768
101210
-#ifdef SQLITE_ENABLE_STAT2
101769
+#ifdef SQLITE_ENABLE_STAT3
101211101770
/*
101212101771
** Estimate the number of rows that will be returned based on
101213101772
** an equality constraint x=VALUE and where that VALUE occurs in
101214101773
** the histogram data. This only works when x is the left-most
101215
-** column of an index and sqlite_stat2 histogram data is available
101774
+** column of an index and sqlite_stat3 histogram data is available
101216101775
** for that index. When pExpr==NULL that means the constraint is
101217101776
** "x IS NULL" instead of "x=VALUE".
101218101777
**
101219101778
** Write the estimated row count into *pnRow and return SQLITE_OK.
101220101779
** If unable to make an estimate, leave *pnRow unchanged and return
@@ -101230,14 +101789,13 @@
101230101789
Index *p, /* The index whose left-most column is pTerm */
101231101790
Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
101232101791
double *pnRow /* Write the revised row estimate here */
101233101792
){
101234101793
sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
101235
- int iLower, iUpper; /* Range of histogram regions containing pRhs */
101236101794
u8 aff; /* Column affinity */
101237101795
int rc; /* Subfunction return code */
101238
- double nRowEst; /* New estimate of the number of rows */
101796
+ tRowcnt a[2]; /* Statistics */
101239101797
101240101798
assert( p->aSample!=0 );
101241101799
aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101242101800
if( pExpr ){
101243101801
rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
@@ -101244,30 +101802,22 @@
101244101802
if( rc ) goto whereEqualScanEst_cancel;
101245101803
}else{
101246101804
pRhs = sqlite3ValueNew(pParse->db);
101247101805
}
101248101806
if( pRhs==0 ) return SQLITE_NOTFOUND;
101249
- rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
101250
- if( rc ) goto whereEqualScanEst_cancel;
101251
- rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
101252
- if( rc ) goto whereEqualScanEst_cancel;
101253
- WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
101254
- if( iLower>=iUpper ){
101255
- nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
101256
- if( nRowEst<*pnRow ) *pnRow = nRowEst;
101257
- }else{
101258
- nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
101259
- *pnRow = nRowEst;
101260
- }
101261
-
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
+ }
101262101812
whereEqualScanEst_cancel:
101263101813
sqlite3ValueFree(pRhs);
101264101814
return rc;
101265101815
}
101266
-#endif /* defined(SQLITE_ENABLE_STAT2) */
101816
+#endif /* defined(SQLITE_ENABLE_STAT3) */
101267101817
101268
-#ifdef SQLITE_ENABLE_STAT2
101818
+#ifdef SQLITE_ENABLE_STAT3
101269101819
/*
101270101820
** Estimate the number of rows that will be returned based on
101271101821
** an IN constraint where the right-hand side of the IN operator
101272101822
** is a list of values. Example:
101273101823
**
@@ -101286,64 +101836,29 @@
101286101836
Parse *pParse, /* Parsing & code generating context */
101287101837
Index *p, /* The index whose left-most column is pTerm */
101288101838
ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
101289101839
double *pnRow /* Write the revised row estimate here */
101290101840
){
101291
- sqlite3_value *pVal = 0; /* One value from list */
101292
- int iLower, iUpper; /* Range of histogram regions containing pRhs */
101293
- u8 aff; /* Column affinity */
101294
- int rc = SQLITE_OK; /* Subfunction return code */
101295
- double nRowEst; /* New estimate of the number of rows */
101296
- int nSpan = 0; /* Number of histogram regions spanned */
101297
- int nSingle = 0; /* Histogram regions hit by a single value */
101298
- int nNotFound = 0; /* Count of values that are not constants */
101299
- int i; /* Loop counter */
101300
- u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
101301
- u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
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 */
101302101845
101303101846
assert( p->aSample!=0 );
101304
- aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101305
- memset(aSpan, 0, sizeof(aSpan));
101306
- memset(aSingle, 0, sizeof(aSingle));
101307
- for(i=0; i<pList->nExpr; i++){
101308
- sqlite3ValueFree(pVal);
101309
- rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
101310
- if( rc ) break;
101311
- if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
101312
- nNotFound++;
101313
- continue;
101314
- }
101315
- rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
101316
- if( rc ) break;
101317
- rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
101318
- if( rc ) break;
101319
- if( iLower>=iUpper ){
101320
- aSingle[iLower] = 1;
101321
- }else{
101322
- assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
101323
- while( iLower<iUpper ) aSpan[iLower++] = 1;
101324
- }
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;
101325101851
}
101326101852
if( rc==SQLITE_OK ){
101327
- for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
101328
- if( aSpan[i] ){
101329
- nSpan++;
101330
- }else if( aSingle[i] ){
101331
- nSingle++;
101332
- }
101333
- }
101334
- nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
101335
- + nNotFound*p->aiRowEst[1];
101336101853
if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
101337101854
*pnRow = nRowEst;
101338
- WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
101339
- nSpan, nSingle, nNotFound, nRowEst));
101855
+ WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
101340101856
}
101341
- sqlite3ValueFree(pVal);
101342101857
return rc;
101343101858
}
101344
-#endif /* defined(SQLITE_ENABLE_STAT2) */
101859
+#endif /* defined(SQLITE_ENABLE_STAT3) */
101345101860
101346101861
101347101862
/*
101348101863
** Find the best query plan for accessing a particular table. Write the
101349101864
** best query plan and its cost into the WhereCost object supplied as the
@@ -101386,11 +101901,11 @@
101386101901
Index *pProbe; /* An index we are evaluating */
101387101902
Index *pIdx; /* Copy of pProbe, or zero for IPK index */
101388101903
int eqTermMask; /* Current mask of valid equality operators */
101389101904
int idxEqTermMask; /* Index mask of valid equality operators */
101390101905
Index sPk; /* A fake index object for the primary key */
101391
- unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
101906
+ tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
101392101907
int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
101393101908
int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
101394101909
101395101910
/* Initialize the cost to a worst-case value */
101396101911
memset(pCost, 0, sizeof(*pCost));
@@ -101441,11 +101956,11 @@
101441101956
}
101442101957
101443101958
/* Loop over all indices looking for the best one to use
101444101959
*/
101445101960
for(; pProbe; pIdx=pProbe=pProbe->pNext){
101446
- const unsigned int * const aiRowEst = pProbe->aiRowEst;
101961
+ const tRowcnt * const aiRowEst = pProbe->aiRowEst;
101447101962
double cost; /* Cost of using pProbe */
101448101963
double nRow; /* Estimated number of rows in result set */
101449101964
double log10N; /* base-10 logarithm of nRow (inexact) */
101450101965
int rev; /* True to scan in reverse order */
101451101966
int wsFlags = 0;
@@ -101484,18 +101999,16 @@
101484101999
** Set to true if there was at least one "x IN (SELECT ...)" term used
101485102000
** in determining the value of nInMul. Note that the RHS of the
101486102001
** IN operator must be a SELECT, not a value list, for this variable
101487102002
** to be true.
101488102003
**
101489
- ** estBound:
101490
- ** An estimate on the amount of the table that must be searched. A
101491
- ** value of 100 means the entire table is searched. Range constraints
101492
- ** might reduce this to a value less than 100 to indicate that only
101493
- ** a fraction of the table needs searching. In the absence of
101494
- ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
101495
- ** space to 1/4rd its original size. So an x>? constraint reduces
101496
- ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
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).
101497102010
**
101498102011
** bSort:
101499102012
** Boolean. True if there is an ORDER BY clause that will require an
101500102013
** external sort (i.e. scanning the index being evaluated will not
101501102014
** correctly order records).
@@ -101516,17 +102029,17 @@
101516102029
** SELECT a, b, c FROM tbl WHERE a = 1;
101517102030
*/
101518102031
int nEq; /* Number of == or IN terms matching index */
101519102032
int bInEst = 0; /* True if "x IN (SELECT...)" seen */
101520102033
int nInMul = 1; /* Number of distinct equalities to lookup */
101521
- int estBound = 100; /* Estimated reduction in search space */
102034
+ double rangeDiv = (double)1; /* Estimated reduction in search space */
101522102035
int nBound = 0; /* Number of range constraints seen */
101523102036
int bSort = !!pOrderBy; /* True if external sort required */
101524102037
int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
101525102038
int bLookup = 0; /* True if not a covering index */
101526102039
WhereTerm *pTerm; /* A single term of the WHERE clause */
101527
-#ifdef SQLITE_ENABLE_STAT2
102040
+#ifdef SQLITE_ENABLE_STAT3
101528102041
WhereTerm *pFirstTerm = 0; /* First term matching the index */
101529102042
#endif
101530102043
101531102044
/* Determine the values of nEq and nInMul */
101532102045
for(nEq=0; nEq<pProbe->nColumn; nEq++){
@@ -101546,23 +102059,23 @@
101546102059
nInMul *= pExpr->x.pList->nExpr;
101547102060
}
101548102061
}else if( pTerm->eOperator & WO_ISNULL ){
101549102062
wsFlags |= WHERE_COLUMN_NULL;
101550102063
}
101551
-#ifdef SQLITE_ENABLE_STAT2
102064
+#ifdef SQLITE_ENABLE_STAT3
101552102065
if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
101553102066
#endif
101554102067
used |= pTerm->prereqRight;
101555102068
}
101556102069
101557
- /* Determine the value of estBound. */
102070
+ /* Determine the value of rangeDiv */
101558102071
if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
101559102072
int j = pProbe->aiColumn[nEq];
101560102073
if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
101561102074
WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
101562102075
WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
101563
- whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
102076
+ whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
101564102077
if( pTop ){
101565102078
nBound = 1;
101566102079
wsFlags |= WHERE_TOP_LIMIT;
101567102080
used |= pTop->prereqRight;
101568102081
}
@@ -101630,11 +102143,11 @@
101630102143
if( bInEst && nRow*2>aiRowEst[0] ){
101631102144
nRow = aiRowEst[0]/2;
101632102145
nInMul = (int)(nRow / aiRowEst[nEq]);
101633102146
}
101634102147
101635
-#ifdef SQLITE_ENABLE_STAT2
102148
+#ifdef SQLITE_ENABLE_STAT3
101636102149
/* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
101637102150
** and we do not think that values of x are unique and if histogram
101638102151
** data is available for column x, then it might be possible
101639102152
** to get a better estimate on the number of rows based on
101640102153
** VALUE and how common that value is according to the histogram.
@@ -101646,16 +102159,16 @@
101646102159
whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
101647102160
}else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
101648102161
whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
101649102162
}
101650102163
}
101651
-#endif /* SQLITE_ENABLE_STAT2 */
102164
+#endif /* SQLITE_ENABLE_STAT3 */
101652102165
101653102166
/* Adjust the number of output rows and downward to reflect rows
101654102167
** that are excluded by range constraints.
101655102168
*/
101656
- nRow = (nRow * (double)estBound) / (double)100;
102169
+ nRow = nRow/rangeDiv;
101657102170
if( nRow<1 ) nRow = 1;
101658102171
101659102172
/* Experiments run on real SQLite databases show that the time needed
101660102173
** to do a binary search to locate a row in a table or index is roughly
101661102174
** log10(N) times the time to move from one row to the next row within
@@ -101780,14 +102293,14 @@
101780102293
if( nRow<2 ) nRow = 2;
101781102294
}
101782102295
101783102296
101784102297
WHERETRACE((
101785
- "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
102298
+ "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
101786102299
" notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
101787102300
pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
101788
- nEq, nInMul, estBound, bSort, bLookup, wsFlags,
102301
+ nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
101789102302
notReady, log10N, nRow, cost, used
101790102303
));
101791102304
101792102305
/* If this index is the best we have seen so far, then record this
101793102306
** index and its cost in the pCost structure.
101794102307
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -650,11 +650,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-07-19 18:29:00 ed5f0aad6b21066bacd01521e82c22e96991f400"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -1298,10 +1298,24 @@
1298 ** integers where the first integer i the new retry count and the second
1299 ** integer is the delay. If either integer is negative, then the setting
1300 ** is not changed but instead the prior value of that setting is written
1301 ** into the array entry, allowing the current retry settings to be
1302 ** interrogated. The zDbName parameter is ignored.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1303 **
1304 */
1305 #define SQLITE_FCNTL_LOCKSTATE 1
1306 #define SQLITE_GET_LOCKPROXYFILE 2
1307 #define SQLITE_SET_LOCKPROXYFILE 3
@@ -1309,10 +1323,11 @@
1309 #define SQLITE_FCNTL_SIZE_HINT 5
1310 #define SQLITE_FCNTL_CHUNK_SIZE 6
1311 #define SQLITE_FCNTL_FILE_POINTER 7
1312 #define SQLITE_FCNTL_SYNC_OMITTED 8
1313 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
 
1314
1315 /*
1316 ** CAPI3REF: Mutex Handle
1317 **
1318 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3382,11 +3397,11 @@
3382 ** a schema change, on the first [sqlite3_step()] call following any change
3383 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3384 ** ^The specific value of WHERE-clause [parameter] might influence the
3385 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3386 ** or [GLOB] operator or if the parameter is compared to an indexed column
3387 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3388 ** the
3389 ** </li>
3390 ** </ol>
3391 */
3392 SQLITE_API int sqlite3_prepare(
@@ -7696,10 +7711,22 @@
7696 ** is 0x00000000ffffffff. But because of quirks of some compilers, we
7697 ** have to specify the value in the less intuitive manner shown:
7698 */
7699 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
7700
 
 
 
 
 
 
 
 
 
 
 
 
7701 /*
7702 ** Macros to determine whether the machine is big or little endian,
7703 ** evaluated at runtime.
7704 */
7705 #ifdef SQLITE_AMALGAMATION
@@ -9897,11 +9924,11 @@
9897 int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
9898 int nCol; /* Number of columns in this table */
9899 Column *aCol; /* Information about each column */
9900 Index *pIndex; /* List of SQL indexes on this table. */
9901 int tnum; /* Root BTree node for this table (see note above) */
9902 unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9903 Select *pSelect; /* NULL for tables. Points to definition if a view. */
9904 u16 nRef; /* Number of pointers to this Table */
9905 u8 tabFlags; /* Mask of TF_* values */
9906 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
9907 FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10096,35 +10123,43 @@
10096 */
10097 struct Index {
10098 char *zName; /* Name of this index */
10099 int nColumn; /* Number of columns in the table used by this index */
10100 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10101 unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10102 Table *pTable; /* The SQL table being indexed */
10103 int tnum; /* Page containing root of this index in database file */
10104 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10105 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
10106 u8 bUnordered; /* Use this index for == or IN queries only */
 
10107 char *zColAff; /* String defining the affinity of each column */
10108 Index *pNext; /* The next index associated with the same table */
10109 Schema *pSchema; /* Schema containing this index */
10110 u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
10111 char **azColl; /* Array of collation sequence names for index */
10112 IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
 
 
 
10113 };
10114
10115 /*
10116 ** Each sample stored in the sqlite_stat2 table is represented in memory
10117 ** using a structure of this type.
10118 */
10119 struct IndexSample {
10120 union {
10121 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10122 double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
 
10123 } u;
10124 u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10125 u8 nByte; /* Size in byte of text or blob. */
 
 
 
10126 };
10127
10128 /*
10129 ** Each token coming out of the lexer is an instance of
10130 ** this structure. Tokens are also used as part of an expression.
@@ -11326,10 +11361,11 @@
11326 #else
11327 # define sqlite3ViewGetColumnNames(A,B) 0
11328 #endif
11329
11330 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
 
11331 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11332 #ifndef SQLITE_OMIT_AUTOINCREMENT
11333 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
11334 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
11335 #else
@@ -11582,11 +11618,11 @@
11582 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11583 void(*)(void*));
11584 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11585 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11586 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11587 #ifdef SQLITE_ENABLE_STAT2
11588 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11589 #endif
11590 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11591 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11592 #ifndef SQLITE_AMALGAMATION
@@ -12208,10 +12244,13 @@
12208 #ifdef SQLITE_ENABLE_RTREE
12209 "ENABLE_RTREE",
12210 #endif
12211 #ifdef SQLITE_ENABLE_STAT2
12212 "ENABLE_STAT2",
 
 
 
12213 #endif
12214 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12215 "ENABLE_UNLOCK_NOTIFY",
12216 #endif
12217 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
@@ -14403,11 +14442,11 @@
14403 DO_OS_MALLOC_TEST(0);
14404 /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
14405 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
14406 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
14407 ** reaching the VFS. */
14408 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f3f, pFlagsOut);
14409 assert( rc==SQLITE_OK || pFile->pMethods==0 );
14410 return rc;
14411 }
14412 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
14413 return pVfs->xDelete(pVfs, zPath, dirSync);
@@ -20447,11 +20486,11 @@
20447 ** no longer required.
20448 **
20449 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20450 ** flag set.
20451 */
20452 #ifdef SQLITE_ENABLE_STAT2
20453 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20454 Mem m;
20455 memset(&m, 0, sizeof(m));
20456 m.db = db;
20457 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -21693,26 +21732,32 @@
21693 return -x;
21694 }
21695
21696 #ifdef SQLITE_ENABLE_8_3_NAMES
21697 /*
21698 ** If SQLITE_ENABLE_8_3_NAME is set at compile-time and if the database
21699 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
21700 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
21701 ** three characters, then shorten the suffix on z[] to be the last three
21702 ** characters of the original suffix.
 
 
 
21703 **
21704 ** Examples:
21705 **
21706 ** test.db-journal => test.nal
21707 ** test.db-wal => test.wal
21708 ** test.db-shm => test.shm
21709 */
21710 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
 
21711 const char *zOk;
21712 zOk = sqlite3_uri_parameter(zBaseFilename, "8_3_names");
21713 if( zOk && sqlite3GetBoolean(zOk) ){
 
 
21714 int i, sz;
21715 sz = sqlite3Strlen30(z);
21716 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
21717 if( z[i]=='.' && ALWAYS(sz>i+4) ) memcpy(&z[i+1], &z[sz-3], 4);
21718 }
@@ -22242,14 +22287,17 @@
22242 #ifdef MEMORY_DEBUG
22243 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22244 #endif
22245
22246 #ifdef SQLITE_DEBUG
22247 SQLITE_PRIVATE int sqlite3OSTrace = 0;
22248 #define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
 
 
 
22249 #else
22250 #define OSTRACE(X)
22251 #endif
22252
22253 /*
22254 ** Macros for performance tracing. Normally turned off. Only works
22255 ** on i486 hardware.
@@ -24492,11 +24540,10 @@
24492 typedef struct unixFile unixFile;
24493 struct unixFile {
24494 sqlite3_io_methods const *pMethod; /* Always the first entry */
24495 unixInodeInfo *pInode; /* Info about locks on this inode */
24496 int h; /* The file descriptor */
24497 int dirfd; /* File descriptor for the directory */
24498 unsigned char eFileLock; /* The type of lock held on this fd */
24499 unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
24500 int lastErrno; /* The unix errno from last I/O error */
24501 void *lockingContext; /* Locking style specific state */
24502 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
@@ -24534,12 +24581,14 @@
24534 };
24535
24536 /*
24537 ** Allowed values for the unixFile.ctrlFlags bitmask:
24538 */
24539 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
24540 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
 
 
24541
24542 /*
24543 ** Include code that is common to all os_*.c files
24544 */
24545 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -24574,14 +24623,17 @@
24574 #ifdef MEMORY_DEBUG
24575 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
24576 #endif
24577
24578 #ifdef SQLITE_DEBUG
24579 SQLITE_PRIVATE int sqlite3OSTrace = 0;
24580 #define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
 
 
 
24581 #else
24582 #define OSTRACE(X)
24583 #endif
24584
24585 /*
24586 ** Macros for performance tracing. Normally turned off. Only works
24587 ** on i486 hardware.
@@ -24786,10 +24838,13 @@
24786 */
24787 static int posixOpen(const char *zFile, int flags, int mode){
24788 return open(zFile, flags, mode);
24789 }
24790
 
 
 
24791 /*
24792 ** Many system calls are accessed through pointer-to-functions so that
24793 ** they may be overridden at runtime to facilitate fault injection during
24794 ** testing and sandboxing. The following array holds the names and pointers
24795 ** to all overrideable system calls.
@@ -24882,10 +24937,16 @@
24882 #else
24883 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
24884 #endif
24885 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
24886
 
 
 
 
 
 
24887 }; /* End of the overrideable system calls */
24888
24889 /*
24890 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
24891 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -26238,22 +26299,18 @@
26238 ** even on VxWorks. A mutex will be acquired on VxWorks by the
26239 ** vxworksReleaseFileId() routine.
26240 */
26241 static int closeUnixFile(sqlite3_file *id){
26242 unixFile *pFile = (unixFile*)id;
26243 if( pFile->dirfd>=0 ){
26244 robust_close(pFile, pFile->dirfd, __LINE__);
26245 pFile->dirfd=-1;
26246 }
26247 if( pFile->h>=0 ){
26248 robust_close(pFile, pFile->h, __LINE__);
26249 pFile->h = -1;
26250 }
26251 #if OS_VXWORKS
26252 if( pFile->pId ){
26253 if( pFile->isDelete ){
26254 unlink(pFile->pId->zCanonicalName);
26255 }
26256 vxworksReleaseFileId(pFile->pId);
26257 pFile->pId = 0;
26258 }
26259 #endif
@@ -26498,11 +26555,11 @@
26498 return SQLITE_OK;
26499 }
26500
26501 /* To fully unlock the database, delete the lock file */
26502 assert( eFileLock==NO_LOCK );
26503 if( unlink(zLockFile) ){
26504 int rc = 0;
26505 int tErrno = errno;
26506 if( ENOENT != tErrno ){
26507 rc = SQLITE_IOERR_UNLOCK;
26508 }
@@ -27733,10 +27790,54 @@
27733 if( OS_VXWORKS && rc!= -1 ){
27734 rc = 0;
27735 }
27736 return rc;
27737 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27738
27739 /*
27740 ** Make sure all writes to a particular file are committed to disk.
27741 **
27742 ** If dataOnly==0 then both the file itself and its metadata (file
@@ -27774,32 +27875,25 @@
27774 SimulateIOError( rc=1 );
27775 if( rc ){
27776 pFile->lastErrno = errno;
27777 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
27778 }
27779 if( pFile->dirfd>=0 ){
27780 OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
 
 
 
 
 
 
27781 HAVE_FULLFSYNC, isFullsync));
27782 #ifndef SQLITE_DISABLE_DIRSYNC
27783 /* The directory sync is only attempted if full_fsync is
27784 ** turned off or unavailable. If a full_fsync occurred above,
27785 ** then the directory sync is superfluous.
27786 */
27787 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
27788 /*
27789 ** We have received multiple reports of fsync() returning
27790 ** errors when applied to directories on certain file systems.
27791 ** A failed directory sync is not a big deal. So it seems
27792 ** better to ignore the error. Ticket #1657
27793 */
27794 /* pFile->lastErrno = errno; */
27795 /* return SQLITE_IOERR; */
27796 }
27797 #endif
27798 /* Only need to sync once, so close the directory when we are done */
27799 robust_close(pFile, pFile->dirfd, __LINE__);
27800 pFile->dirfd = -1;
27801 }
27802 return rc;
27803 }
27804
27805 /*
@@ -27884,17 +27978,23 @@
27884 ** If the user has configured a chunk-size for this file, it could be
27885 ** that the file needs to be extended at this point. Otherwise, the
27886 ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
27887 */
27888 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27889 if( pFile->szChunk ){
27890 i64 nSize; /* Required file size */
 
27891 struct stat buf; /* Used to hold return values of fstat() */
27892
27893 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
27894
27895 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
 
 
 
 
 
27896 if( nSize>(i64)buf.st_size ){
27897
27898 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
27899 /* The code below is handling the return value of osFallocate()
27900 ** correctly. posix_fallocate() is defined to "returns zero on success,
@@ -27933,25 +28033,37 @@
27933
27934 /*
27935 ** Information and control of an open file handle.
27936 */
27937 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
 
27938 switch( op ){
27939 case SQLITE_FCNTL_LOCKSTATE: {
27940 *(int*)pArg = ((unixFile*)id)->eFileLock;
27941 return SQLITE_OK;
27942 }
27943 case SQLITE_LAST_ERRNO: {
27944 *(int*)pArg = ((unixFile*)id)->lastErrno;
27945 return SQLITE_OK;
27946 }
27947 case SQLITE_FCNTL_CHUNK_SIZE: {
27948 ((unixFile*)id)->szChunk = *(int *)pArg;
27949 return SQLITE_OK;
27950 }
27951 case SQLITE_FCNTL_SIZE_HINT: {
27952 return fcntlSizeHint((unixFile *)id, *(i64 *)pArg);
 
 
 
 
 
 
 
 
 
 
 
27953 }
27954 #ifndef NDEBUG
27955 /* The pager calls this method to signal that it has done
27956 ** a rollback and that the database is therefore unchanged and
27957 ** it hence it is OK for the transaction change counter to be
@@ -28632,11 +28744,11 @@
28632 ** shared-memory file, too */
28633 unixEnterMutex();
28634 assert( pShmNode->nRef>0 );
28635 pShmNode->nRef--;
28636 if( pShmNode->nRef==0 ){
28637 if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename);
28638 unixShmPurge(pDbFd);
28639 }
28640 unixLeaveMutex();
28641
28642 return SQLITE_OK;
@@ -28945,11 +29057,11 @@
28945 ** Initialize the contents of the unixFile structure pointed to by pId.
28946 */
28947 static int fillInUnixFile(
28948 sqlite3_vfs *pVfs, /* Pointer to vfs object */
28949 int h, /* Open file descriptor of file being opened */
28950 int dirfd, /* Directory file descriptor */
28951 sqlite3_file *pId, /* Write to the unixFile structure here */
28952 const char *zFilename, /* Name of the file being opened */
28953 int noLock, /* Omit locking if true */
28954 int isDelete, /* Delete on close if true */
28955 int isReadOnly /* True if the file is opened read-only */
@@ -28976,20 +29088,22 @@
28976 assert( zFilename==0 || zFilename[0]=='/' );
28977 #endif
28978
28979 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
28980 pNew->h = h;
28981 pNew->dirfd = dirfd;
28982 pNew->zPath = zFilename;
28983 if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
28984 pNew->ctrlFlags = UNIXFILE_EXCL;
28985 }else{
28986 pNew->ctrlFlags = 0;
28987 }
28988 if( isReadOnly ){
28989 pNew->ctrlFlags |= UNIXFILE_RDONLY;
28990 }
 
 
 
28991
28992 #if OS_VXWORKS
28993 pNew->pId = vxworksFindFileId(zFilename);
28994 if( pNew->pId==0 ){
28995 noLock = 1;
@@ -29112,56 +29226,24 @@
29112 pNew->lastErrno = 0;
29113 #if OS_VXWORKS
29114 if( rc!=SQLITE_OK ){
29115 if( h>=0 ) robust_close(pNew, h, __LINE__);
29116 h = -1;
29117 unlink(zFilename);
29118 isDelete = 0;
29119 }
29120 pNew->isDelete = isDelete;
29121 #endif
29122 if( rc!=SQLITE_OK ){
29123 if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__);
29124 if( h>=0 ) robust_close(pNew, h, __LINE__);
29125 }else{
29126 pNew->pMethod = pLockingStyle;
29127 OpenCounter(+1);
29128 }
29129 return rc;
29130 }
29131
29132 /*
29133 ** Open a file descriptor to the directory containing file zFilename.
29134 ** If successful, *pFd is set to the opened file descriptor and
29135 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
29136 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
29137 ** value.
29138 **
29139 ** If SQLITE_OK is returned, the caller is responsible for closing
29140 ** the file descriptor *pFd using close().
29141 */
29142 static int openDirectory(const char *zFilename, int *pFd){
29143 int ii;
29144 int fd = -1;
29145 char zDirname[MAX_PATHNAME+1];
29146
29147 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
29148 for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
29149 if( ii>0 ){
29150 zDirname[ii] = '\0';
29151 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
29152 if( fd>=0 ){
29153 #ifdef FD_CLOEXEC
29154 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
29155 #endif
29156 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
29157 }
29158 }
29159 *pFd = fd;
29160 return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
29161 }
29162
29163 /*
29164 ** Return the name of a directory in which to put temporary files.
29165 ** If no suitable temporary file directory can be found, return NULL.
29166 */
29167 static const char *unixTempFileDir(void){
@@ -29272,11 +29354,11 @@
29272 ** ignored and -1 is returned. The caller will try to open a new file
29273 ** descriptor on the same path, fail, and return an error to SQLite.
29274 **
29275 ** Even if a subsequent open() call does succeed, the consequences of
29276 ** not searching for a resusable file descriptor are not dire. */
29277 if( 0==stat(zPath, &sStat) ){
29278 unixInodeInfo *pInode;
29279
29280 unixEnterMutex();
29281 pInode = inodeList;
29282 while( pInode && (pInode->fileId.dev!=sStat.st_dev
@@ -29348,11 +29430,11 @@
29348 while( nDb>0 && zPath[nDb]!='-' ) nDb--;
29349 if( nDb==0 ) return SQLITE_OK;
29350 memcpy(zDb, zPath, nDb);
29351 zDb[nDb] = '\0';
29352
29353 if( 0==stat(zDb, &sStat) ){
29354 *pMode = sStat.st_mode & 0777;
29355 }else{
29356 rc = SQLITE_IOERR_FSTAT;
29357 }
29358 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
@@ -29390,11 +29472,10 @@
29390 int flags, /* Input flags to control the opening */
29391 int *pOutFlags /* Output flags returned to SQLite core */
29392 ){
29393 unixFile *p = (unixFile *)pFile;
29394 int fd = -1; /* File descriptor returned by open() */
29395 int dirfd = -1; /* Directory file descriptor */
29396 int openFlags = 0; /* Flags to pass to open() */
29397 int eType = flags&0xFFFFFF00; /* Type of file to open */
29398 int noLock; /* True to omit locking primitives */
29399 int rc = SQLITE_OK; /* Function Return Code */
29400
@@ -29409,11 +29490,11 @@
29409
29410 /* If creating a master or main-file journal, this function will open
29411 ** a file-descriptor on the directory too. The first time unixSync()
29412 ** is called the directory file descriptor will be fsync()ed and close()d.
29413 */
29414 int isOpenDirectory = (isCreate && (
29415 eType==SQLITE_OPEN_MASTER_JOURNAL
29416 || eType==SQLITE_OPEN_MAIN_JOURNAL
29417 || eType==SQLITE_OPEN_WAL
29418 ));
29419
@@ -29463,11 +29544,11 @@
29463 }
29464 }
29465 p->pUnused = pUnused;
29466 }else if( !zName ){
29467 /* If zName is NULL, the upper layer is requesting a temp file. */
29468 assert(isDelete && !isOpenDirectory);
29469 rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
29470 if( rc!=SQLITE_OK ){
29471 return rc;
29472 }
29473 zName = zTmpname;
@@ -29519,32 +29600,19 @@
29519
29520 if( isDelete ){
29521 #if OS_VXWORKS
29522 zPath = zName;
29523 #else
29524 unlink(zName);
29525 #endif
29526 }
29527 #if SQLITE_ENABLE_LOCKING_STYLE
29528 else{
29529 p->openFlags = openFlags;
29530 }
29531 #endif
29532
29533 if( isOpenDirectory ){
29534 rc = openDirectory(zPath, &dirfd);
29535 if( rc!=SQLITE_OK ){
29536 /* It is safe to close fd at this point, because it is guaranteed not
29537 ** to be open on a database file. If it were open on a database file,
29538 ** it would not be safe to close as this would release any locks held
29539 ** on the file by this process. */
29540 assert( eType!=SQLITE_OPEN_MAIN_DB );
29541 robust_close(p, fd, __LINE__);
29542 goto open_finished;
29543 }
29544 }
29545
29546 #ifdef FD_CLOEXEC
29547 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
29548 #endif
29549
29550 noLock = eType!=SQLITE_OPEN_MAIN_DB;
@@ -29552,11 +29620,10 @@
29552
29553 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29554 struct statfs fsInfo;
29555 if( fstatfs(fd, &fsInfo) == -1 ){
29556 ((unixFile*)pFile)->lastErrno = errno;
29557 if( dirfd>=0 ) robust_close(p, dirfd, __LINE__);
29558 robust_close(p, fd, __LINE__);
29559 return SQLITE_IOERR_ACCESS;
29560 }
29561 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
29562 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
@@ -29584,21 +29651,18 @@
29584 ** then the call to close() will cancel those locks. In practice,
29585 ** we're assuming that statfs() doesn't fail very often. At least
29586 ** not while other file descriptors opened by the same process on
29587 ** the same file are working. */
29588 p->lastErrno = errno;
29589 if( dirfd>=0 ){
29590 robust_close(p, dirfd, __LINE__);
29591 }
29592 robust_close(p, fd, __LINE__);
29593 rc = SQLITE_IOERR_ACCESS;
29594 goto open_finished;
29595 }
29596 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
29597 }
29598 if( useProxy ){
29599 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
29600 isDelete, isReadonly);
29601 if( rc==SQLITE_OK ){
29602 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
29603 if( rc!=SQLITE_OK ){
29604 /* Use unixClose to clean up the resources added in fillInUnixFile
@@ -29612,11 +29676,11 @@
29612 goto open_finished;
29613 }
29614 }
29615 #endif
29616
29617 rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
29618 isDelete, isReadonly);
29619 open_finished:
29620 if( rc!=SQLITE_OK ){
29621 sqlite3_free(p->pUnused);
29622 }
@@ -29634,17 +29698,17 @@
29634 int dirSync /* If true, fsync() directory after deleting file */
29635 ){
29636 int rc = SQLITE_OK;
29637 UNUSED_PARAMETER(NotUsed);
29638 SimulateIOError(return SQLITE_IOERR_DELETE);
29639 if( unlink(zPath)==(-1) && errno!=ENOENT ){
29640 return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29641 }
29642 #ifndef SQLITE_DISABLE_DIRSYNC
29643 if( dirSync ){
29644 int fd;
29645 rc = openDirectory(zPath, &fd);
29646 if( rc==SQLITE_OK ){
29647 #if OS_VXWORKS
29648 if( fsync(fd)==-1 )
29649 #else
29650 if( fsync(fd) )
@@ -29693,11 +29757,11 @@
29693 assert(!"Invalid flags argument");
29694 }
29695 *pResOut = (osAccess(zPath, amode)==0);
29696 if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
29697 struct stat buf;
29698 if( 0==stat(zPath, &buf) && buf.st_size==0 ){
29699 *pResOut = 0;
29700 }
29701 }
29702 return SQLITE_OK;
29703 }
@@ -30212,11 +30276,10 @@
30212 const char *path, /* path for the new unixFile */
30213 unixFile **ppFile, /* unixFile created and returned by ref */
30214 int islockfile /* if non zero missing dirs will be created */
30215 ) {
30216 int fd = -1;
30217 int dirfd = -1;
30218 unixFile *pNew;
30219 int rc = SQLITE_OK;
30220 int openFlags = O_RDWR | O_CREAT;
30221 sqlite3_vfs dummyVfs;
30222 int terrno = 0;
@@ -30277,11 +30340,11 @@
30277 dummyVfs.zName = "dummy";
30278 pUnused->fd = fd;
30279 pUnused->flags = openFlags;
30280 pNew->pUnused = pUnused;
30281
30282 rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
30283 if( rc==SQLITE_OK ){
30284 *ppFile = pNew;
30285 return SQLITE_OK;
30286 }
30287 end_create_proxy:
@@ -30391,11 +30454,11 @@
30391 conchFile->openFlags = O_RDWR | O_CREAT;
30392
30393 end_breaklock:
30394 if( rc ){
30395 if( fd>=0 ){
30396 unlink(tPath);
30397 robust_close(pFile, fd, __LINE__);
30398 }
30399 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
30400 }
30401 return rc;
@@ -31214,11 +31277,11 @@
31214 };
31215 unsigned int i; /* Loop counter */
31216
31217 /* Double-check that the aSyscall[] array has been constructed
31218 ** correctly. See ticket [bb3a86e890c8e96ab] */
31219 assert( ArraySize(aSyscall)==16 );
31220
31221 /* Register all VFSes defined in the aVfs[] array */
31222 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
31223 sqlite3_vfs_register(&aVfs[i], i==0);
31224 }
@@ -31331,14 +31394,17 @@
31331 #ifdef MEMORY_DEBUG
31332 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
31333 #endif
31334
31335 #ifdef SQLITE_DEBUG
31336 SQLITE_PRIVATE int sqlite3OSTrace = 0;
31337 #define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
 
 
 
31338 #else
31339 #define OSTRACE(X)
31340 #endif
31341
31342 /*
31343 ** Macros for performance tracing. Normally turned off. Only works
31344 ** on i486 hardware.
@@ -31546,12 +31612,13 @@
31546 typedef struct winFile winFile;
31547 struct winFile {
31548 const sqlite3_io_methods *pMethod; /*** Must be first ***/
31549 sqlite3_vfs *pVfs; /* The VFS used to open this file */
31550 HANDLE h; /* Handle for accessing the file */
31551 unsigned char locktype; /* Type of lock currently held on this file */
31552 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
 
31553 DWORD lastErrno; /* The Windows errno from the last I/O error */
31554 DWORD sectorSize; /* Sector size of the device file is on */
31555 winShm *pShm; /* Instance of shared memory on this file */
31556 const char *zPath; /* Full pathname of this file */
31557 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
@@ -32778,29 +32845,39 @@
32778
32779 /*
32780 ** Control and query of the open file handle.
32781 */
32782 static int winFileControl(sqlite3_file *id, int op, void *pArg){
 
32783 switch( op ){
32784 case SQLITE_FCNTL_LOCKSTATE: {
32785 *(int*)pArg = ((winFile*)id)->locktype;
32786 return SQLITE_OK;
32787 }
32788 case SQLITE_LAST_ERRNO: {
32789 *(int*)pArg = (int)((winFile*)id)->lastErrno;
32790 return SQLITE_OK;
32791 }
32792 case SQLITE_FCNTL_CHUNK_SIZE: {
32793 ((winFile*)id)->szChunk = *(int *)pArg;
32794 return SQLITE_OK;
32795 }
32796 case SQLITE_FCNTL_SIZE_HINT: {
32797 sqlite3_int64 sz = *(sqlite3_int64*)pArg;
32798 SimulateIOErrorBenign(1);
32799 winTruncate(id, sz);
32800 SimulateIOErrorBenign(0);
32801 return SQLITE_OK;
 
 
 
 
 
 
 
 
 
32802 }
32803 case SQLITE_FCNTL_SYNC_OMITTED: {
32804 return SQLITE_OK;
32805 }
32806 case SQLITE_FCNTL_WIN32_AV_RETRY: {
@@ -33623,10 +33700,11 @@
33623 int isTemp = 0;
33624 #endif
33625 winFile *pFile = (winFile*)id;
33626 void *zConverted; /* Filename in OS encoding */
33627 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
 
33628
33629 /* If argument zPath is a NULL pointer, this function is required to open
33630 ** a temporary file. Use this buffer to store the file name in.
33631 */
33632 char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */
@@ -33742,34 +33820,34 @@
33742 #if SQLITE_OS_WINCE
33743 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
33744 #endif
33745
33746 if( isNT() ){
33747 h = CreateFileW((WCHAR*)zConverted,
33748 dwDesiredAccess,
33749 dwShareMode,
33750 NULL,
33751 dwCreationDisposition,
33752 dwFlagsAndAttributes,
33753 NULL
33754 );
33755 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33756 ** Since the ASCII version of these Windows API do not exist for WINCE,
33757 ** it's important to not reference them for WINCE builds.
33758 */
33759 #if SQLITE_OS_WINCE==0
33760 }else{
33761 h = CreateFileA((char*)zConverted,
33762 dwDesiredAccess,
33763 dwShareMode,
33764 NULL,
33765 dwCreationDisposition,
33766 dwFlagsAndAttributes,
33767 NULL
33768 );
33769 #endif
33770 }
 
 
33771
33772 OSTRACE(("OPEN %d %s 0x%lx %s\n",
33773 h, zName, dwDesiredAccess,
33774 h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
33775
@@ -33895,15 +33973,17 @@
33895 zConverted = convertUtf8Filename(zFilename);
33896 if( zConverted==0 ){
33897 return SQLITE_NOMEM;
33898 }
33899 if( isNT() ){
 
33900 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
33901 memset(&sAttrData, 0, sizeof(sAttrData));
33902 if( GetFileAttributesExW((WCHAR*)zConverted,
33903 GetFileExInfoStandard,
33904 &sAttrData) ){
 
33905 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
33906 ** as if it does not exist.
33907 */
33908 if( flags==SQLITE_ACCESS_EXISTS
33909 && sAttrData.nFileSizeHigh==0
@@ -33911,10 +33991,11 @@
33911 attr = INVALID_FILE_ATTRIBUTES;
33912 }else{
33913 attr = sAttrData.dwFileAttributes;
33914 }
33915 }else{
 
33916 if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
33917 winLogError(SQLITE_IOERR_ACCESS, "winAccess", zFilename);
33918 free(zConverted);
33919 return SQLITE_IOERR_ACCESS;
33920 }else{
@@ -33935,11 +34016,12 @@
33935 case SQLITE_ACCESS_READ:
33936 case SQLITE_ACCESS_EXISTS:
33937 rc = attr!=INVALID_FILE_ATTRIBUTES;
33938 break;
33939 case SQLITE_ACCESS_READWRITE:
33940 rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
 
33941 break;
33942 default:
33943 assert(!"Invalid flags argument");
33944 }
33945 *pResOut = rc;
@@ -45519,17 +45601,19 @@
45519 **
45520 ** The EXCLUSIVE lock is not released before returning.
45521 */
45522 rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
45523 if( rc==SQLITE_OK ){
 
45524 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
45525 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
45526 }
45527 rc = sqlite3WalCheckpoint(
45528 pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
45529 );
45530 if( rc==SQLITE_OK ){
 
45531 isDelete = 1;
45532 }
45533 }
45534
45535 walIndexClose(pWal, isDelete);
@@ -57508,15 +57592,15 @@
57508 *ppVal = 0;
57509 return SQLITE_OK;
57510 }
57511 op = pExpr->op;
57512
57513 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
57514 ** The ifdef here is to enable us to achieve 100% branch test coverage even
57515 ** when SQLITE_ENABLE_STAT2 is omitted.
57516 */
57517 #ifdef SQLITE_ENABLE_STAT2
57518 if( op==TK_REGISTER ) op = pExpr->op2;
57519 #else
57520 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
57521 #endif
57522
@@ -58208,12 +58292,12 @@
58208 /*
58209 ** Change the P2 operand of instruction addr so that it points to
58210 ** the address of the next instruction to be coded.
58211 */
58212 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58213 assert( addr>=0 );
58214 sqlite3VdbeChangeP2(p, addr, p->nOp);
58215 }
58216
58217
58218 /*
58219 ** If the input FuncDef structure is ephemeral, then free it. If
@@ -63874,10 +63958,15 @@
63874 assert( memIsValid(pIn1) );
63875 memAboutToChange(p, pOut);
63876 u.ac.zMalloc = pOut->zMalloc;
63877 pOut->zMalloc = 0;
63878 sqlite3VdbeMemMove(pOut, pIn1);
 
 
 
 
 
63879 pIn1->zMalloc = u.ac.zMalloc;
63880 REGISTER_TRACE(u.ac.p2++, pOut);
63881 pIn1++;
63882 pOut++;
63883 }
@@ -75923,10 +76012,100 @@
75923 ** May you find forgiveness for yourself and forgive others.
75924 ** May you share freely, never taking more than you give.
75925 **
75926 *************************************************************************
75927 ** This file contains code associated with the ANALYZE command.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75928 */
75929 #ifndef SQLITE_OMIT_ANALYZE
75930
75931 /*
75932 ** This routine generates code that opens the sqlite_stat1 table for
@@ -75954,12 +76133,18 @@
75954 static const struct {
75955 const char *zName;
75956 const char *zCols;
75957 } aTable[] = {
75958 { "sqlite_stat1", "tbl,idx,stat" },
75959 #ifdef SQLITE_ENABLE_STAT2
75960 { "sqlite_stat2", "tbl,idx,sampleno,sample" },
 
 
 
 
 
 
75961 #endif
75962 };
75963
75964 int aRoot[] = {0, 0};
75965 u8 aCreateTbl[] = {0, 0};
@@ -75971,10 +76156,21 @@
75971 if( v==0 ) return;
75972 assert( sqlite3BtreeHoldsAllMutexes(db) );
75973 assert( sqlite3VdbeDb(v)==db );
75974 pDb = &db->aDb[iDb];
75975
 
 
 
 
 
 
 
 
 
 
 
75976 for(i=0; i<ArraySize(aTable); i++){
75977 const char *zTab = aTable[i].zName;
75978 Table *pStat;
75979 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
75980 /* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -76001,17 +76197,238 @@
76001 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
76002 }
76003 }
76004 }
76005
76006 /* Open the sqlite_stat[12] tables for writing. */
76007 for(i=0; i<ArraySize(aTable); i++){
76008 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
76009 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
76010 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
76011 }
76012 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76013
76014 /*
76015 ** Generate code to do an analysis of all indices associated with
76016 ** a single table.
76017 */
@@ -76031,24 +76448,31 @@
76031 int endOfLoop; /* The end of the loop */
76032 int jZeroRows = -1; /* Jump from here if number of rows is zero */
76033 int iDb; /* Index of database containing pTab */
76034 int regTabname = iMem++; /* Register containing table name */
76035 int regIdxname = iMem++; /* Register containing index name */
76036 int regSampleno = iMem++; /* Register containing next sample number */
76037 int regCol = iMem++; /* Content of a column analyzed table */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76038 int regRec = iMem++; /* Register holding completed record */
76039 int regTemp = iMem++; /* Temporary use register */
76040 int regRowid = iMem++; /* Rowid for the inserted record */
76041
76042 #ifdef SQLITE_ENABLE_STAT2
76043 int addr = 0; /* Instruction address */
76044 int regTemp2 = iMem++; /* Temporary use register */
76045 int regSamplerecno = iMem++; /* Index of next sample to record */
76046 int regRecno = iMem++; /* Current sample index */
76047 int regLast = iMem++; /* Index of last sample to record */
76048 int regFirst = iMem++; /* Index of first sample to record */
76049 #endif
76050
76051 v = sqlite3GetVdbe(pParse);
76052 if( v==0 || NEVER(pTab==0) ){
76053 return;
76054 }
@@ -76077,17 +76501,22 @@
76077 iIdxCur = pParse->nTab++;
76078 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
76079 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
76080 int nCol;
76081 KeyInfo *pKey;
 
 
76082
76083 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
 
76084 nCol = pIdx->nColumn;
76085 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
76086 if( iMem+1+(nCol*2)>pParse->nMem ){
76087 pParse->nMem = iMem+1+(nCol*2);
76088 }
 
 
76089
76090 /* Open a cursor to the index to be analyzed. */
76091 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
76092 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
76093 (char *)pKey, P4_KEYINFO_HANDOFF);
@@ -76094,35 +76523,24 @@
76094 VdbeComment((v, "%s", pIdx->zName));
76095
76096 /* Populate the register containing the index name. */
76097 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
76098
76099 #ifdef SQLITE_ENABLE_STAT2
76100
76101 /* If this iteration of the loop is generating code to analyze the
76102 ** first index in the pTab->pIndex list, then register regLast has
76103 ** not been populated. In this case populate it now. */
76104 if( pTab->pIndex==pIdx ){
76105 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
76106 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
76107 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
76108
76109 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
76110 sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
76111 addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
76112 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
76113 sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
76114 sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
76115 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
76116 sqlite3VdbeJumpHere(v, addr);
76117 }
76118
76119 /* Zero the regSampleno and regRecno registers. */
76120 sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
76121 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
76122 sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
76123 #endif
76124
76125 /* The block of memory cells initialized here is used as follows.
76126 **
76127 ** iMem:
76128 ** The total number of rows in the table.
@@ -76148,79 +76566,87 @@
76148 /* Start the analysis loop. This loop runs through all the entries in
76149 ** the index b-tree. */
76150 endOfLoop = sqlite3VdbeMakeLabel(v);
76151 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
76152 topOfLoop = sqlite3VdbeCurrentAddr(v);
76153 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
76154
76155 for(i=0; i<nCol; i++){
76156 CollSeq *pColl;
76157 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
76158 if( i==0 ){
76159 #ifdef SQLITE_ENABLE_STAT2
76160 /* Check if the record that cursor iIdxCur points to contains a
76161 ** value that should be stored in the sqlite_stat2 table. If so,
76162 ** store it. */
76163 int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
76164 assert( regTabname+1==regIdxname
76165 && regTabname+2==regSampleno
76166 && regTabname+3==regCol
76167 );
76168 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
76169 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
76170 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
76171 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
76172
76173 /* Calculate new values for regSamplerecno and regSampleno.
76174 **
76175 ** sampleno = sampleno + 1
76176 ** samplerecno = samplerecno+(remaining records)/(remaining samples)
76177 */
76178 sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
76179 sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
76180 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
76181 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
76182 sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
76183 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
76184 sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
76185
76186 sqlite3VdbeJumpHere(v, ne);
76187 sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
76188 #endif
76189
76190 /* Always record the very first row */
76191 sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
76192 }
76193 assert( pIdx->azColl!=0 );
76194 assert( pIdx->azColl[i]!=0 );
76195 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
76196 sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
76197 (char*)pColl, P4_COLLSEQ);
76198 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
76199 }
76200 if( db->mallocFailed ){
76201 /* If a malloc failure has occurred, then the result of the expression
76202 ** passed as the second argument to the call to sqlite3VdbeJumpHere()
76203 ** below may be negative. Which causes an assert() to fail (or an
76204 ** out-of-bounds write if SQLITE_DEBUG is not defined). */
76205 return;
76206 }
76207 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
76208 for(i=0; i<nCol; i++){
76209 int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
76210 if( i==0 ){
76211 sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
 
 
 
 
 
 
 
 
 
76212 }
76213 sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
76214 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
76215 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
76216 }
 
76217
76218 /* End of the analysis loop. */
76219 sqlite3VdbeResolveLabel(v, endOfLoop);
 
76220 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
76221 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76222
76223 /* Store the results in sqlite_stat1.
76224 **
76225 ** The result is a single row of the sqlite_stat1 table. The first
76226 ** two columns are the names of the table and index. The third column
@@ -76236,50 +76662,51 @@
76236 **
76237 ** If K==0 then no entry is made into the sqlite_stat1 table.
76238 ** If K>0 then it is always the case the D>0 so division by zero
76239 ** is never possible.
76240 */
76241 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
76242 if( jZeroRows<0 ){
76243 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
76244 }
76245 for(i=0; i<nCol; i++){
76246 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
76247 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
76248 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
76249 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
76250 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
76251 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
76252 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
76253 }
76254 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76255 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
76256 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
76257 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
76258 }
76259
76260 /* If the table has no indices, create a single sqlite_stat1 entry
76261 ** containing NULL as the index name and the row count as the content.
76262 */
76263 if( pTab->pIndex==0 ){
76264 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
76265 VdbeComment((v, "%s", pTab->zName));
76266 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
76267 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76268 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
76269 }else{
76270 sqlite3VdbeJumpHere(v, jZeroRows);
76271 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
76272 }
76273 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
76274 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76275 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
76276 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
76277 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
76278 if( pParse->nMem<regRec ) pParse->nMem = regRec;
76279 sqlite3VdbeJumpHere(v, jZeroRows);
76280 }
 
76281
76282 /*
76283 ** Generate code that will cause the most recent index analysis to
76284 ** be loaded into internal hash tables where is can be used.
76285 */
@@ -76300,11 +76727,11 @@
76300 int iStatCur;
76301 int iMem;
76302
76303 sqlite3BeginWriteOperation(pParse, 0, iDb);
76304 iStatCur = pParse->nTab;
76305 pParse->nTab += 2;
76306 openStatTable(pParse, iDb, iStatCur, 0, 0);
76307 iMem = pParse->nMem+1;
76308 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
76309 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
76310 Table *pTab = (Table*)sqliteHashData(k);
@@ -76325,11 +76752,11 @@
76325 assert( pTab!=0 );
76326 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
76327 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
76328 sqlite3BeginWriteOperation(pParse, 0, iDb);
76329 iStatCur = pParse->nTab;
76330 pParse->nTab += 2;
76331 if( pOnlyIdx ){
76332 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
76333 }else{
76334 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
76335 }
@@ -76430,11 +76857,11 @@
76430 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
76431 analysisInfo *pInfo = (analysisInfo*)pData;
76432 Index *pIndex;
76433 Table *pTable;
76434 int i, c, n;
76435 unsigned int v;
76436 const char *z;
76437
76438 assert( argc==3 );
76439 UNUSED_PARAMETER2(NotUsed, argc);
76440
@@ -76473,40 +76900,172 @@
76473 /*
76474 ** If the Index.aSample variable is not NULL, delete the aSample[] array
76475 ** and its contents.
76476 */
76477 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
76478 #ifdef SQLITE_ENABLE_STAT2
76479 if( pIdx->aSample ){
76480 int j;
76481 for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
76482 IndexSample *p = &pIdx->aSample[j];
76483 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
76484 sqlite3DbFree(db, p->u.z);
76485 }
76486 }
76487 sqlite3DbFree(db, pIdx->aSample);
76488 }
 
 
 
76489 #else
76490 UNUSED_PARAMETER(db);
76491 UNUSED_PARAMETER(pIdx);
76492 #endif
76493 }
76494
 
76495 /*
76496 ** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76497 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
76498 ** arrays. The contents of sqlite_stat2 are used to populate the
76499 ** Index.aSample[] arrays.
76500 **
76501 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
76502 ** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
76503 ** during compilation and the sqlite_stat2 table is present, no data is
76504 ** read from it.
76505 **
76506 ** If SQLITE_ENABLE_STAT2 was defined during compilation and the
76507 ** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
76508 ** returned. However, in this case, data is read from the sqlite_stat1
76509 ** table (if it is present) before returning.
76510 **
76511 ** If an OOM error occurs, this function always sets db->mallocFailed.
76512 ** This means if the caller does not care about other errors, the return
@@ -76524,12 +77083,14 @@
76524 /* Clear any prior statistics */
76525 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
76526 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
76527 Index *pIdx = sqliteHashData(i);
76528 sqlite3DefaultRowEst(pIdx);
 
76529 sqlite3DeleteIndexSamples(db, pIdx);
76530 pIdx->aSample = 0;
 
76531 }
76532
76533 /* Check to make sure the sqlite_stat1 table exists */
76534 sInfo.db = db;
76535 sInfo.zDatabase = db->aDb[iDb].zName;
@@ -76537,91 +77098,23 @@
76537 return SQLITE_ERROR;
76538 }
76539
76540 /* Load new statistics out of the sqlite_stat1 table */
76541 zSql = sqlite3MPrintf(db,
76542 "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
76543 if( zSql==0 ){
76544 rc = SQLITE_NOMEM;
76545 }else{
76546 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
76547 sqlite3DbFree(db, zSql);
76548 }
76549
76550
76551 /* Load the statistics from the sqlite_stat2 table. */
76552 #ifdef SQLITE_ENABLE_STAT2
76553 if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
76554 rc = SQLITE_ERROR;
76555 }
76556 if( rc==SQLITE_OK ){
76557 sqlite3_stmt *pStmt = 0;
76558
76559 zSql = sqlite3MPrintf(db,
76560 "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
76561 if( !zSql ){
76562 rc = SQLITE_NOMEM;
76563 }else{
76564 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76565 sqlite3DbFree(db, zSql);
76566 }
76567
76568 if( rc==SQLITE_OK ){
76569 while( sqlite3_step(pStmt)==SQLITE_ROW ){
76570 char *zIndex; /* Index name */
76571 Index *pIdx; /* Pointer to the index object */
76572
76573 zIndex = (char *)sqlite3_column_text(pStmt, 0);
76574 pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
76575 if( pIdx ){
76576 int iSample = sqlite3_column_int(pStmt, 1);
76577 if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
76578 int eType = sqlite3_column_type(pStmt, 2);
76579
76580 if( pIdx->aSample==0 ){
76581 static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
76582 pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
76583 if( pIdx->aSample==0 ){
76584 db->mallocFailed = 1;
76585 break;
76586 }
76587 memset(pIdx->aSample, 0, sz);
76588 }
76589
76590 assert( pIdx->aSample );
76591 {
76592 IndexSample *pSample = &pIdx->aSample[iSample];
76593 pSample->eType = (u8)eType;
76594 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
76595 pSample->u.r = sqlite3_column_double(pStmt, 2);
76596 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
76597 const char *z = (const char *)(
76598 (eType==SQLITE_BLOB) ?
76599 sqlite3_column_blob(pStmt, 2):
76600 sqlite3_column_text(pStmt, 2)
76601 );
76602 int n = sqlite3_column_bytes(pStmt, 2);
76603 if( n>24 ){
76604 n = 24;
76605 }
76606 pSample->nByte = (u8)n;
76607 if( n < 1){
76608 pSample->u.z = 0;
76609 }else{
76610 pSample->u.z = sqlite3DbStrNDup(0, z, n);
76611 if( pSample->u.z==0 ){
76612 db->mallocFailed = 1;
76613 break;
76614 }
76615 }
76616 }
76617 }
76618 }
76619 }
76620 }
76621 rc = sqlite3_finalize(pStmt);
76622 }
76623 }
76624 #endif
76625
76626 if( rc==SQLITE_NOMEM ){
76627 db->mallocFailed = 1;
@@ -79422,10 +79915,108 @@
79422 iDestroyed = iLargest;
79423 }
79424 }
79425 #endif
79426 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79427
79428 /*
79429 ** This routine is called to do the work of a DROP TABLE statement.
79430 ** pName is the name of the table to be dropped.
79431 */
@@ -79491,11 +80082,11 @@
79491 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
79492 goto exit_drop_table;
79493 }
79494 }
79495 #endif
79496 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
79497 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
79498 goto exit_drop_table;
79499 }
79500
79501 #ifndef SQLITE_OMIT_VIEW
@@ -79515,79 +80106,15 @@
79515 /* Generate code to remove the table from the master table
79516 ** on disk.
79517 */
79518 v = sqlite3GetVdbe(pParse);
79519 if( v ){
79520 Trigger *pTrigger;
79521 Db *pDb = &db->aDb[iDb];
79522 sqlite3BeginWriteOperation(pParse, 1, iDb);
79523
79524 #ifndef SQLITE_OMIT_VIRTUALTABLE
79525 if( IsVirtual(pTab) ){
79526 sqlite3VdbeAddOp0(v, OP_VBegin);
79527 }
79528 #endif
79529 sqlite3FkDropTable(pParse, pName, pTab);
79530
79531 /* Drop all triggers associated with the table being dropped. Code
79532 ** is generated to remove entries from sqlite_master and/or
79533 ** sqlite_temp_master if required.
79534 */
79535 pTrigger = sqlite3TriggerList(pParse, pTab);
79536 while( pTrigger ){
79537 assert( pTrigger->pSchema==pTab->pSchema ||
79538 pTrigger->pSchema==db->aDb[1].pSchema );
79539 sqlite3DropTriggerPtr(pParse, pTrigger);
79540 pTrigger = pTrigger->pNext;
79541 }
79542
79543 #ifndef SQLITE_OMIT_AUTOINCREMENT
79544 /* Remove any entries of the sqlite_sequence table associated with
79545 ** the table being dropped. This is done before the table is dropped
79546 ** at the btree level, in case the sqlite_sequence table needs to
79547 ** move as a result of the drop (can happen in auto-vacuum mode).
79548 */
79549 if( pTab->tabFlags & TF_Autoincrement ){
79550 sqlite3NestedParse(pParse,
79551 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
79552 pDb->zName, pTab->zName
79553 );
79554 }
79555 #endif
79556
79557 /* Drop all SQLITE_MASTER table and index entries that refer to the
79558 ** table. The program name loops through the master table and deletes
79559 ** every row that refers to a table of the same name as the one being
79560 ** dropped. Triggers are handled seperately because a trigger can be
79561 ** created in the temp database that refers to a table in another
79562 ** database.
79563 */
79564 sqlite3NestedParse(pParse,
79565 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
79566 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
79567
79568 /* Drop any statistics from the sqlite_stat1 table, if it exists */
79569 if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
79570 sqlite3NestedParse(pParse,
79571 "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb->zName, pTab->zName
79572 );
79573 }
79574
79575 if( !isView && !IsVirtual(pTab) ){
79576 destroyTable(pParse, pTab);
79577 }
79578
79579 /* Remove the table entry from SQLite's internal schema and modify
79580 ** the schema cookie.
79581 */
79582 if( IsVirtual(pTab) ){
79583 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
79584 }
79585 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
79586 sqlite3ChangeCookie(pParse, iDb);
79587 }
79588 sqliteViewResetAll(db, iDb);
79589
79590 exit_drop_table:
79591 sqlite3SrcListDelete(db, pName);
79592 }
79593
@@ -80030,24 +80557,24 @@
80030 */
80031 nName = sqlite3Strlen30(zName);
80032 nCol = pList->nExpr;
80033 pIndex = sqlite3DbMallocZero(db,
80034 sizeof(Index) + /* Index structure */
 
80035 sizeof(int)*nCol + /* Index.aiColumn */
80036 sizeof(int)*(nCol+1) + /* Index.aiRowEst */
80037 sizeof(char *)*nCol + /* Index.azColl */
80038 sizeof(u8)*nCol + /* Index.aSortOrder */
80039 nName + 1 + /* Index.zName */
80040 nExtra /* Collation sequence names */
80041 );
80042 if( db->mallocFailed ){
80043 goto exit_create_index;
80044 }
80045 pIndex->azColl = (char**)(&pIndex[1]);
 
80046 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
80047 pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
80048 pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
80049 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
80050 zExtra = (char *)(&pIndex->zName[nName+1]);
80051 memcpy(pIndex->zName, zName, nName+1);
80052 pIndex->pTable = pTab;
80053 pIndex->nColumn = pList->nExpr;
@@ -80320,13 +80847,13 @@
80320 ** Apart from that, we have little to go on besides intuition as to
80321 ** how aiRowEst[] should be initialized. The numbers generated here
80322 ** are based on typical values found in actual indices.
80323 */
80324 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
80325 unsigned *a = pIdx->aiRowEst;
80326 int i;
80327 unsigned n;
80328 assert( a!=0 );
80329 a[0] = pIdx->pTable->nRowEst;
80330 if( a[0]<10 ) a[0] = 10;
80331 n = 10;
80332 for(i=1; i<=pIdx->nColumn; i++){
@@ -80392,19 +80919,13 @@
80392 v = sqlite3GetVdbe(pParse);
80393 if( v ){
80394 sqlite3BeginWriteOperation(pParse, 1, iDb);
80395 sqlite3NestedParse(pParse,
80396 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
80397 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
80398 pIndex->zName
80399 );
80400 if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){
80401 sqlite3NestedParse(pParse,
80402 "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q",
80403 db->aDb[iDb].zName, pIndex->zName
80404 );
80405 }
80406 sqlite3ChangeCookie(pParse, iDb);
80407 destroyRootPage(pParse, pIndex->tnum, iDb);
80408 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
80409 }
80410
@@ -98638,11 +99159,11 @@
98638 #define TERM_CODED 0x04 /* This term is already coded */
98639 #define TERM_COPIED 0x08 /* Has a child */
98640 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
98641 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
98642 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
98643 #ifdef SQLITE_ENABLE_STAT2
98644 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
98645 #else
98646 # define TERM_VNULL 0x00 /* Disabled if not using stat2 */
98647 #endif
98648
@@ -99852,11 +100373,11 @@
99852 pNewTerm->prereqAll = pTerm->prereqAll;
99853 }
99854 }
99855 #endif /* SQLITE_OMIT_VIRTUALTABLE */
99856
99857 #ifdef SQLITE_ENABLE_STAT2
99858 /* When sqlite_stat2 histogram data is available an operator of the
99859 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
99860 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
99861 ** virtual term of that form.
99862 **
@@ -99891,11 +100412,11 @@
99891 pTerm->nChild = 1;
99892 pTerm->wtFlags |= TERM_COPIED;
99893 pNewTerm->prereqAll = pTerm->prereqAll;
99894 }
99895 }
99896 #endif /* SQLITE_ENABLE_STAT2 */
99897
99898 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
99899 ** an index for tables to the left of the join.
99900 */
99901 pTerm->prereqRight |= extraRight;
@@ -100662,10 +101183,11 @@
100662 if( pTerm->leftCursor != pSrc->iCursor ) continue;
100663 assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
100664 testcase( pTerm->eOperator==WO_IN );
100665 testcase( pTerm->eOperator==WO_ISNULL );
100666 if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
 
100667 nTerm++;
100668 }
100669
100670 /* If the ORDER BY clause contains only columns in the current
100671 ** virtual table then allocate space for the aOrderBy part of
@@ -100712,10 +101234,11 @@
100712 if( pTerm->leftCursor != pSrc->iCursor ) continue;
100713 assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
100714 testcase( pTerm->eOperator==WO_IN );
100715 testcase( pTerm->eOperator==WO_ISNULL );
100716 if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
 
100717 pIdxCons[j].iColumn = pTerm->u.leftColumn;
100718 pIdxCons[j].iTermOffset = i;
100719 pIdxCons[j].op = (u8)pTerm->eOperator;
100720 /* The direct assignment in the previous line is possible only because
100721 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
@@ -100938,71 +101461,89 @@
100938 */
100939 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
100940 }
100941 #endif /* SQLITE_OMIT_VIRTUALTABLE */
100942
 
100943 /*
100944 ** Argument pIdx is a pointer to an index structure that has an array of
100945 ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
100946 ** stored in Index.aSample. These samples divide the domain of values stored
100947 ** the index into (SQLITE_INDEX_SAMPLES+1) regions.
100948 ** Region 0 contains all values less than the first sample value. Region
100949 ** 1 contains values between the first and second samples. Region 2 contains
100950 ** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
100951 ** contains values larger than the last sample.
100952 **
100953 ** If the index contains many duplicates of a single value, then it is
100954 ** possible that two or more adjacent samples can hold the same value.
100955 ** When that is the case, the smallest possible region code is returned
100956 ** when roundUp is false and the largest possible region code is returned
100957 ** when roundUp is true.
100958 **
100959 ** If successful, this function determines which of the regions value
100960 ** pVal lies in, sets *piRegion to the region index (a value between 0
100961 ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
100962 ** Or, if an OOM occurs while converting text values between encodings,
100963 ** SQLITE_NOMEM is returned and *piRegion is undefined.
100964 */
100965 #ifdef SQLITE_ENABLE_STAT2
100966 static int whereRangeRegion(
100967 Parse *pParse, /* Database connection */
100968 Index *pIdx, /* Index to consider domain of */
100969 sqlite3_value *pVal, /* Value to consider */
100970 int roundUp, /* Return largest valid region if true */
100971 int *piRegion /* OUT: Region of domain in which value lies */
100972 ){
 
 
 
 
 
 
 
100973 assert( roundUp==0 || roundUp==1 );
100974 if( ALWAYS(pVal) ){
100975 IndexSample *aSample = pIdx->aSample;
100976 int i = 0;
100977 int eType = sqlite3_value_type(pVal);
100978
100979 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
100980 double r = sqlite3_value_double(pVal);
100981 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
100982 if( aSample[i].eType==SQLITE_NULL ) continue;
100983 if( aSample[i].eType>=SQLITE_TEXT ) break;
100984 if( roundUp ){
100985 if( aSample[i].u.r>r ) break;
100986 }else{
100987 if( aSample[i].u.r>=r ) break;
100988 }
100989 }
100990 }else if( eType==SQLITE_NULL ){
100991 i = 0;
100992 if( roundUp ){
100993 while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
100994 }
100995 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100996 sqlite3 *db = pParse->db;
100997 CollSeq *pColl;
100998 const u8 *z;
100999 int n;
101000
101001 /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
101002 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
101003
101004 if( eType==SQLITE_BLOB ){
101005 z = (const u8 *)sqlite3_value_blob(pVal);
101006 pColl = db->pDfltColl;
101007 assert( pColl->enc==SQLITE_UTF8 );
101008 }else{
@@ -101017,16 +101558,16 @@
101017 return SQLITE_NOMEM;
101018 }
101019 assert( z && pColl && pColl->xCmp );
101020 }
101021 n = sqlite3ValueBytes(pVal, pColl->enc);
101022
101023 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
101024 int c;
101025 int eSampletype = aSample[i].eType;
101026 if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
101027 if( (eSampletype!=eType) ) break;
101028 #ifndef SQLITE_OMIT_UTF16
101029 if( pColl->enc!=SQLITE_UTF8 ){
101030 int nSample;
101031 char *zSample = sqlite3Utf8to16(
101032 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -101040,20 +101581,52 @@
101040 }else
101041 #endif
101042 {
101043 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
101044 }
101045 if( c-roundUp>=0 ) break;
 
 
 
101046 }
101047 }
 
101048
101049 assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
101050 *piRegion = i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101051 }
101052 return SQLITE_OK;
101053 }
101054 #endif /* #ifdef SQLITE_ENABLE_STAT2 */
101055
101056 /*
101057 ** If expression pExpr represents a literal value, set *pp to point to
101058 ** an sqlite3_value structure containing the same value, with affinity
101059 ** aff applied to it, before returning. It is the responsibility of the
@@ -101067,11 +101640,11 @@
101067 **
101068 ** If neither of the above apply, set *pp to NULL.
101069 **
101070 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
101071 */
101072 #ifdef SQLITE_ENABLE_STAT2
101073 static int valueFromExpr(
101074 Parse *pParse,
101075 Expr *pExpr,
101076 u8 aff,
101077 sqlite3_value **pp
@@ -101115,106 +101688,92 @@
101115 **
101116 ** ... FROM t1 WHERE a > ? AND a < ? ...
101117 **
101118 ** then nEq should be passed 0.
101119 **
101120 ** The returned value is an integer between 1 and 100, inclusive. A return
101121 ** value of 1 indicates that the proposed range scan is expected to visit
101122 ** approximately 1/100th (1%) of the rows selected by the nEq equality
101123 ** constraints (if any). A return value of 100 indicates that it is expected
101124 ** that the range scan will visit every row (100%) selected by the equality
101125 ** constraints.
101126 **
101127 ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
101128 ** reduces the search space by 3/4ths. Hence a single constraint (x>?)
101129 ** results in a return of 25 and a range constraint (x>? AND x<?) results
101130 ** in a return of 6.
101131 */
101132 static int whereRangeScanEst(
101133 Parse *pParse, /* Parsing & code generating context */
101134 Index *p, /* The index containing the range-compared column; "x" */
101135 int nEq, /* index into p->aCol[] of the range-compared column */
101136 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
101137 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
101138 int *piEst /* OUT: Return value */
101139 ){
101140 int rc = SQLITE_OK;
101141
101142 #ifdef SQLITE_ENABLE_STAT2
101143
101144 if( nEq==0 && p->aSample ){
101145 sqlite3_value *pLowerVal = 0;
101146 sqlite3_value *pUpperVal = 0;
101147 int iEst;
101148 int iLower = 0;
101149 int iUpper = SQLITE_INDEX_SAMPLES;
101150 int roundUpUpper = 0;
101151 int roundUpLower = 0;
101152 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101153
101154 if( pLower ){
101155 Expr *pExpr = pLower->pExpr->pRight;
101156 rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
101157 assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
101158 roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
 
 
 
 
 
 
101159 }
101160 if( rc==SQLITE_OK && pUpper ){
101161 Expr *pExpr = pUpper->pExpr->pRight;
101162 rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
101163 assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
101164 roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
101165 }
101166
101167 if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
101168 sqlite3ValueFree(pLowerVal);
101169 sqlite3ValueFree(pUpperVal);
101170 goto range_est_fallback;
101171 }else if( pLowerVal==0 ){
101172 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
101173 if( pLower ) iLower = iUpper/2;
101174 }else if( pUpperVal==0 ){
101175 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
101176 if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
101177 }else{
101178 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
101179 if( rc==SQLITE_OK ){
101180 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
101181 }
101182 }
101183 WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
101184
101185 iEst = iUpper - iLower;
101186 testcase( iEst==SQLITE_INDEX_SAMPLES );
101187 assert( iEst<=SQLITE_INDEX_SAMPLES );
101188 if( iEst<1 ){
101189 *piEst = 50/SQLITE_INDEX_SAMPLES;
101190 }else{
101191 *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
101192 }
101193 sqlite3ValueFree(pLowerVal);
101194 sqlite3ValueFree(pUpperVal);
101195 return rc;
101196 }
101197 range_est_fallback:
101198 #else
101199 UNUSED_PARAMETER(pParse);
101200 UNUSED_PARAMETER(p);
101201 UNUSED_PARAMETER(nEq);
101202 #endif
101203 assert( pLower || pUpper );
101204 *piEst = 100;
101205 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
101206 if( pUpper ) *piEst /= 4;
101207 return rc;
101208 }
101209
101210 #ifdef SQLITE_ENABLE_STAT2
101211 /*
101212 ** Estimate the number of rows that will be returned based on
101213 ** an equality constraint x=VALUE and where that VALUE occurs in
101214 ** the histogram data. This only works when x is the left-most
101215 ** column of an index and sqlite_stat2 histogram data is available
101216 ** for that index. When pExpr==NULL that means the constraint is
101217 ** "x IS NULL" instead of "x=VALUE".
101218 **
101219 ** Write the estimated row count into *pnRow and return SQLITE_OK.
101220 ** If unable to make an estimate, leave *pnRow unchanged and return
@@ -101230,14 +101789,13 @@
101230 Index *p, /* The index whose left-most column is pTerm */
101231 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
101232 double *pnRow /* Write the revised row estimate here */
101233 ){
101234 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
101235 int iLower, iUpper; /* Range of histogram regions containing pRhs */
101236 u8 aff; /* Column affinity */
101237 int rc; /* Subfunction return code */
101238 double nRowEst; /* New estimate of the number of rows */
101239
101240 assert( p->aSample!=0 );
101241 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101242 if( pExpr ){
101243 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
@@ -101244,30 +101802,22 @@
101244 if( rc ) goto whereEqualScanEst_cancel;
101245 }else{
101246 pRhs = sqlite3ValueNew(pParse->db);
101247 }
101248 if( pRhs==0 ) return SQLITE_NOTFOUND;
101249 rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
101250 if( rc ) goto whereEqualScanEst_cancel;
101251 rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
101252 if( rc ) goto whereEqualScanEst_cancel;
101253 WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
101254 if( iLower>=iUpper ){
101255 nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
101256 if( nRowEst<*pnRow ) *pnRow = nRowEst;
101257 }else{
101258 nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
101259 *pnRow = nRowEst;
101260 }
101261
101262 whereEqualScanEst_cancel:
101263 sqlite3ValueFree(pRhs);
101264 return rc;
101265 }
101266 #endif /* defined(SQLITE_ENABLE_STAT2) */
101267
101268 #ifdef SQLITE_ENABLE_STAT2
101269 /*
101270 ** Estimate the number of rows that will be returned based on
101271 ** an IN constraint where the right-hand side of the IN operator
101272 ** is a list of values. Example:
101273 **
@@ -101286,64 +101836,29 @@
101286 Parse *pParse, /* Parsing & code generating context */
101287 Index *p, /* The index whose left-most column is pTerm */
101288 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
101289 double *pnRow /* Write the revised row estimate here */
101290 ){
101291 sqlite3_value *pVal = 0; /* One value from list */
101292 int iLower, iUpper; /* Range of histogram regions containing pRhs */
101293 u8 aff; /* Column affinity */
101294 int rc = SQLITE_OK; /* Subfunction return code */
101295 double nRowEst; /* New estimate of the number of rows */
101296 int nSpan = 0; /* Number of histogram regions spanned */
101297 int nSingle = 0; /* Histogram regions hit by a single value */
101298 int nNotFound = 0; /* Count of values that are not constants */
101299 int i; /* Loop counter */
101300 u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
101301 u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
101302
101303 assert( p->aSample!=0 );
101304 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101305 memset(aSpan, 0, sizeof(aSpan));
101306 memset(aSingle, 0, sizeof(aSingle));
101307 for(i=0; i<pList->nExpr; i++){
101308 sqlite3ValueFree(pVal);
101309 rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
101310 if( rc ) break;
101311 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
101312 nNotFound++;
101313 continue;
101314 }
101315 rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
101316 if( rc ) break;
101317 rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
101318 if( rc ) break;
101319 if( iLower>=iUpper ){
101320 aSingle[iLower] = 1;
101321 }else{
101322 assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
101323 while( iLower<iUpper ) aSpan[iLower++] = 1;
101324 }
101325 }
101326 if( rc==SQLITE_OK ){
101327 for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
101328 if( aSpan[i] ){
101329 nSpan++;
101330 }else if( aSingle[i] ){
101331 nSingle++;
101332 }
101333 }
101334 nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
101335 + nNotFound*p->aiRowEst[1];
101336 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
101337 *pnRow = nRowEst;
101338 WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
101339 nSpan, nSingle, nNotFound, nRowEst));
101340 }
101341 sqlite3ValueFree(pVal);
101342 return rc;
101343 }
101344 #endif /* defined(SQLITE_ENABLE_STAT2) */
101345
101346
101347 /*
101348 ** Find the best query plan for accessing a particular table. Write the
101349 ** best query plan and its cost into the WhereCost object supplied as the
@@ -101386,11 +101901,11 @@
101386 Index *pProbe; /* An index we are evaluating */
101387 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
101388 int eqTermMask; /* Current mask of valid equality operators */
101389 int idxEqTermMask; /* Index mask of valid equality operators */
101390 Index sPk; /* A fake index object for the primary key */
101391 unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
101392 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
101393 int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
101394
101395 /* Initialize the cost to a worst-case value */
101396 memset(pCost, 0, sizeof(*pCost));
@@ -101441,11 +101956,11 @@
101441 }
101442
101443 /* Loop over all indices looking for the best one to use
101444 */
101445 for(; pProbe; pIdx=pProbe=pProbe->pNext){
101446 const unsigned int * const aiRowEst = pProbe->aiRowEst;
101447 double cost; /* Cost of using pProbe */
101448 double nRow; /* Estimated number of rows in result set */
101449 double log10N; /* base-10 logarithm of nRow (inexact) */
101450 int rev; /* True to scan in reverse order */
101451 int wsFlags = 0;
@@ -101484,18 +101999,16 @@
101484 ** Set to true if there was at least one "x IN (SELECT ...)" term used
101485 ** in determining the value of nInMul. Note that the RHS of the
101486 ** IN operator must be a SELECT, not a value list, for this variable
101487 ** to be true.
101488 **
101489 ** estBound:
101490 ** An estimate on the amount of the table that must be searched. A
101491 ** value of 100 means the entire table is searched. Range constraints
101492 ** might reduce this to a value less than 100 to indicate that only
101493 ** a fraction of the table needs searching. In the absence of
101494 ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
101495 ** space to 1/4rd its original size. So an x>? constraint reduces
101496 ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
101497 **
101498 ** bSort:
101499 ** Boolean. True if there is an ORDER BY clause that will require an
101500 ** external sort (i.e. scanning the index being evaluated will not
101501 ** correctly order records).
@@ -101516,17 +102029,17 @@
101516 ** SELECT a, b, c FROM tbl WHERE a = 1;
101517 */
101518 int nEq; /* Number of == or IN terms matching index */
101519 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
101520 int nInMul = 1; /* Number of distinct equalities to lookup */
101521 int estBound = 100; /* Estimated reduction in search space */
101522 int nBound = 0; /* Number of range constraints seen */
101523 int bSort = !!pOrderBy; /* True if external sort required */
101524 int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
101525 int bLookup = 0; /* True if not a covering index */
101526 WhereTerm *pTerm; /* A single term of the WHERE clause */
101527 #ifdef SQLITE_ENABLE_STAT2
101528 WhereTerm *pFirstTerm = 0; /* First term matching the index */
101529 #endif
101530
101531 /* Determine the values of nEq and nInMul */
101532 for(nEq=0; nEq<pProbe->nColumn; nEq++){
@@ -101546,23 +102059,23 @@
101546 nInMul *= pExpr->x.pList->nExpr;
101547 }
101548 }else if( pTerm->eOperator & WO_ISNULL ){
101549 wsFlags |= WHERE_COLUMN_NULL;
101550 }
101551 #ifdef SQLITE_ENABLE_STAT2
101552 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
101553 #endif
101554 used |= pTerm->prereqRight;
101555 }
101556
101557 /* Determine the value of estBound. */
101558 if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
101559 int j = pProbe->aiColumn[nEq];
101560 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
101561 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
101562 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
101563 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
101564 if( pTop ){
101565 nBound = 1;
101566 wsFlags |= WHERE_TOP_LIMIT;
101567 used |= pTop->prereqRight;
101568 }
@@ -101630,11 +102143,11 @@
101630 if( bInEst && nRow*2>aiRowEst[0] ){
101631 nRow = aiRowEst[0]/2;
101632 nInMul = (int)(nRow / aiRowEst[nEq]);
101633 }
101634
101635 #ifdef SQLITE_ENABLE_STAT2
101636 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
101637 ** and we do not think that values of x are unique and if histogram
101638 ** data is available for column x, then it might be possible
101639 ** to get a better estimate on the number of rows based on
101640 ** VALUE and how common that value is according to the histogram.
@@ -101646,16 +102159,16 @@
101646 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
101647 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
101648 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
101649 }
101650 }
101651 #endif /* SQLITE_ENABLE_STAT2 */
101652
101653 /* Adjust the number of output rows and downward to reflect rows
101654 ** that are excluded by range constraints.
101655 */
101656 nRow = (nRow * (double)estBound) / (double)100;
101657 if( nRow<1 ) nRow = 1;
101658
101659 /* Experiments run on real SQLite databases show that the time needed
101660 ** to do a binary search to locate a row in a table or index is roughly
101661 ** log10(N) times the time to move from one row to the next row within
@@ -101780,14 +102293,14 @@
101780 if( nRow<2 ) nRow = 2;
101781 }
101782
101783
101784 WHERETRACE((
101785 "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
101786 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
101787 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
101788 nEq, nInMul, estBound, bSort, bLookup, wsFlags,
101789 notReady, log10N, nRow, cost, used
101790 ));
101791
101792 /* If this index is the best we have seen so far, then record this
101793 ** index and its cost in the pCost structure.
101794
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -650,11 +650,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 **
@@ -1298,10 +1298,24 @@
1298 ** integers where the first integer i the new retry count and the second
1299 ** integer is the delay. If either integer is negative, then the setting
1300 ** is not changed but instead the prior value of that setting is written
1301 ** into the array entry, allowing the current retry settings to be
1302 ** interrogated. The zDbName parameter is ignored.
1303 **
1304 ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1305 ** persistent [WAL | Write AHead Log] setting. By default, the auxiliary
1306 ** write ahead log and shared memory files used for transaction control
1307 ** are automatically deleted when the latest connection to the database
1308 ** closes. Setting persistent WAL mode causes those files to persist after
1309 ** close. Persisting the files is useful when other processes that do not
1310 ** have write permission on the directory containing the database file want
1311 ** to read the database file, as the WAL and shared memory files must exist
1312 ** in order for the database to be readable. The fourth parameter to
1313 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1314 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1315 ** WAL mode. If the integer is -1, then it is overwritten with the current
1316 ** WAL persistence setting.
1317 **
1318 */
1319 #define SQLITE_FCNTL_LOCKSTATE 1
1320 #define SQLITE_GET_LOCKPROXYFILE 2
1321 #define SQLITE_SET_LOCKPROXYFILE 3
@@ -1309,10 +1323,11 @@
1323 #define SQLITE_FCNTL_SIZE_HINT 5
1324 #define SQLITE_FCNTL_CHUNK_SIZE 6
1325 #define SQLITE_FCNTL_FILE_POINTER 7
1326 #define SQLITE_FCNTL_SYNC_OMITTED 8
1327 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
1328 #define SQLITE_FCNTL_PERSIST_WAL 10
1329
1330 /*
1331 ** CAPI3REF: Mutex Handle
1332 **
1333 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3382,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(
@@ -7696,10 +7711,22 @@
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
@@ -9897,11 +9924,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 */
@@ -10096,35 +10123,43 @@
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.
@@ -11326,10 +11361,11 @@
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
@@ -11582,11 +11618,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
@@ -12208,10 +12244,13 @@
12244 #ifdef SQLITE_ENABLE_RTREE
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
@@ -14403,11 +14442,11 @@
14442 DO_OS_MALLOC_TEST(0);
14443 /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
14444 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
14445 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
14446 ** reaching the VFS. */
14447 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
14448 assert( rc==SQLITE_OK || pFile->pMethods==0 );
14449 return rc;
14450 }
14451 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
14452 return pVfs->xDelete(pVfs, zPath, dirSync);
@@ -20447,11 +20486,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);
@@ -21693,26 +21732,32 @@
21732 return -x;
21733 }
21734
21735 #ifdef SQLITE_ENABLE_8_3_NAMES
21736 /*
21737 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
21738 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
21739 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
21740 ** three characters, then shorten the suffix on z[] to be the last three
21741 ** characters of the original suffix.
21742 **
21743 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
21744 ** do the suffix shortening regardless of URI parameter.
21745 **
21746 ** Examples:
21747 **
21748 ** test.db-journal => test.nal
21749 ** test.db-wal => test.wal
21750 ** test.db-shm => test.shm
21751 */
21752 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
21753 #if SQLITE_ENABLE_8_3_NAMES<2
21754 const char *zOk;
21755 zOk = sqlite3_uri_parameter(zBaseFilename, "8_3_names");
21756 if( zOk && sqlite3GetBoolean(zOk) )
21757 #endif
21758 {
21759 int i, sz;
21760 sz = sqlite3Strlen30(z);
21761 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
21762 if( z[i]=='.' && ALWAYS(sz>i+4) ) memcpy(&z[i+1], &z[sz-3], 4);
21763 }
@@ -22242,14 +22287,17 @@
22287 #ifdef MEMORY_DEBUG
22288 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22289 #endif
22290
22291 #ifdef SQLITE_DEBUG
22292 # ifndef SQLITE_DEBUG_OS_TRACE
22293 # define SQLITE_DEBUG_OS_TRACE 0
22294 # endif
22295 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
22296 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
22297 #else
22298 # define OSTRACE(X)
22299 #endif
22300
22301 /*
22302 ** Macros for performance tracing. Normally turned off. Only works
22303 ** on i486 hardware.
@@ -24492,11 +24540,10 @@
24540 typedef struct unixFile unixFile;
24541 struct unixFile {
24542 sqlite3_io_methods const *pMethod; /* Always the first entry */
24543 unixInodeInfo *pInode; /* Info about locks on this inode */
24544 int h; /* The file descriptor */
 
24545 unsigned char eFileLock; /* The type of lock held on this fd */
24546 unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
24547 int lastErrno; /* The unix errno from last I/O error */
24548 void *lockingContext; /* Locking style specific state */
24549 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
@@ -24534,12 +24581,14 @@
24581 };
24582
24583 /*
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 ***************/
@@ -24574,14 +24623,17 @@
24623 #ifdef MEMORY_DEBUG
24624 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
24625 #endif
24626
24627 #ifdef SQLITE_DEBUG
24628 # ifndef SQLITE_DEBUG_OS_TRACE
24629 # define SQLITE_DEBUG_OS_TRACE 0
24630 # endif
24631 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
24632 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
24633 #else
24634 # define OSTRACE(X)
24635 #endif
24636
24637 /*
24638 ** Macros for performance tracing. Normally turned off. Only works
24639 ** on i486 hardware.
@@ -24786,10 +24838,13 @@
24838 */
24839 static int posixOpen(const char *zFile, int flags, int mode){
24840 return open(zFile, flags, mode);
24841 }
24842
24843 /* Forward reference */
24844 static int openDirectory(const char*, int*);
24845
24846 /*
24847 ** Many system calls are accessed through pointer-to-functions so that
24848 ** they may be overridden at runtime to facilitate fault injection during
24849 ** testing and sandboxing. The following array holds the names and pointers
24850 ** to all overrideable system calls.
@@ -24882,10 +24937,16 @@
24937 #else
24938 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
24939 #endif
24940 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
24941
24942 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
24943 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
24944
24945 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
24946 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
24947
24948 }; /* End of the overrideable system calls */
24949
24950 /*
24951 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
24952 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -26238,22 +26299,18 @@
26299 ** even on VxWorks. A mutex will be acquired on VxWorks by the
26300 ** vxworksReleaseFileId() routine.
26301 */
26302 static int closeUnixFile(sqlite3_file *id){
26303 unixFile *pFile = (unixFile*)id;
 
 
 
 
26304 if( pFile->h>=0 ){
26305 robust_close(pFile, pFile->h, __LINE__);
26306 pFile->h = -1;
26307 }
26308 #if OS_VXWORKS
26309 if( pFile->pId ){
26310 if( pFile->isDelete ){
26311 osUnlink(pFile->pId->zCanonicalName);
26312 }
26313 vxworksReleaseFileId(pFile->pId);
26314 pFile->pId = 0;
26315 }
26316 #endif
@@ -26498,11 +26555,11 @@
26555 return SQLITE_OK;
26556 }
26557
26558 /* To fully unlock the database, delete the lock file */
26559 assert( eFileLock==NO_LOCK );
26560 if( osUnlink(zLockFile) ){
26561 int rc = 0;
26562 int tErrno = errno;
26563 if( ENOENT != tErrno ){
26564 rc = SQLITE_IOERR_UNLOCK;
26565 }
@@ -27733,10 +27790,54 @@
27790 if( OS_VXWORKS && rc!= -1 ){
27791 rc = 0;
27792 }
27793 return rc;
27794 }
27795
27796 /*
27797 ** Open a file descriptor to the directory containing file zFilename.
27798 ** If successful, *pFd is set to the opened file descriptor and
27799 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
27800 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
27801 ** value.
27802 **
27803 ** The directory file descriptor is used for only one thing - to
27804 ** fsync() a directory to make sure file creation and deletion events
27805 ** are flushed to disk. Such fsyncs are not needed on newer
27806 ** journaling filesystems, but are required on older filesystems.
27807 **
27808 ** This routine can be overridden using the xSetSysCall interface.
27809 ** The ability to override this routine was added in support of the
27810 ** chromium sandbox. Opening a directory is a security risk (we are
27811 ** told) so making it overrideable allows the chromium sandbox to
27812 ** replace this routine with a harmless no-op. To make this routine
27813 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
27814 ** *pFd set to a negative number.
27815 **
27816 ** If SQLITE_OK is returned, the caller is responsible for closing
27817 ** the file descriptor *pFd using close().
27818 */
27819 static int openDirectory(const char *zFilename, int *pFd){
27820 int ii;
27821 int fd = -1;
27822 char zDirname[MAX_PATHNAME+1];
27823
27824 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
27825 for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
27826 if( ii>0 ){
27827 zDirname[ii] = '\0';
27828 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
27829 if( fd>=0 ){
27830 #ifdef FD_CLOEXEC
27831 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
27832 #endif
27833 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
27834 }
27835 }
27836 *pFd = fd;
27837 return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
27838 }
27839
27840 /*
27841 ** Make sure all writes to a particular file are committed to disk.
27842 **
27843 ** If dataOnly==0 then both the file itself and its metadata (file
@@ -27774,32 +27875,25 @@
27875 SimulateIOError( rc=1 );
27876 if( rc ){
27877 pFile->lastErrno = errno;
27878 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
27879 }
27880
27881 /* Also fsync the directory containing the file if the DIRSYNC flag
27882 ** is set. This is a one-time occurrance. Many systems (examples: AIX)
27883 ** are unable to fsync a directory, so ignore errors on the fsync.
27884 */
27885 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
27886 int dirfd;
27887 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
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 }
27898
27899 /*
@@ -27884,17 +27978,23 @@
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,
@@ -27933,25 +28033,37 @@
28033
28034 /*
28035 ** Information and control of an open file handle.
28036 */
28037 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
28038 unixFile *pFile = (unixFile*)id;
28039 switch( op ){
28040 case SQLITE_FCNTL_LOCKSTATE: {
28041 *(int*)pArg = pFile->eFileLock;
28042 return SQLITE_OK;
28043 }
28044 case SQLITE_LAST_ERRNO: {
28045 *(int*)pArg = pFile->lastErrno;
28046 return SQLITE_OK;
28047 }
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;
28059 }else if( bPersist==0 ){
28060 pFile->ctrlFlags &= ~UNIXFILE_PERSIST_WAL;
28061 }else{
28062 pFile->ctrlFlags |= UNIXFILE_PERSIST_WAL;
28063 }
28064 return SQLITE_OK;
28065 }
28066 #ifndef NDEBUG
28067 /* The pager calls this method to signal that it has done
28068 ** a rollback and that the database is therefore unchanged and
28069 ** it hence it is OK for the transaction change counter to be
@@ -28632,11 +28744,11 @@
28744 ** shared-memory file, too */
28745 unixEnterMutex();
28746 assert( pShmNode->nRef>0 );
28747 pShmNode->nRef--;
28748 if( pShmNode->nRef==0 ){
28749 if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
28750 unixShmPurge(pDbFd);
28751 }
28752 unixLeaveMutex();
28753
28754 return SQLITE_OK;
@@ -28945,11 +29057,11 @@
29057 ** Initialize the contents of the unixFile structure pointed to by pId.
29058 */
29059 static int fillInUnixFile(
29060 sqlite3_vfs *pVfs, /* Pointer to vfs object */
29061 int h, /* Open file descriptor of file being opened */
29062 int syncDir, /* True to sync directory on first sync */
29063 sqlite3_file *pId, /* Write to the unixFile structure here */
29064 const char *zFilename, /* Name of the file being opened */
29065 int noLock, /* Omit locking if true */
29066 int isDelete, /* Delete on close if true */
29067 int isReadOnly /* True if the file is opened read-only */
@@ -28976,20 +29088,22 @@
29088 assert( zFilename==0 || zFilename[0]=='/' );
29089 #endif
29090
29091 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
29092 pNew->h = h;
 
29093 pNew->zPath = zFilename;
29094 if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
29095 pNew->ctrlFlags = UNIXFILE_EXCL;
29096 }else{
29097 pNew->ctrlFlags = 0;
29098 }
29099 if( isReadOnly ){
29100 pNew->ctrlFlags |= UNIXFILE_RDONLY;
29101 }
29102 if( syncDir ){
29103 pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
29104 }
29105
29106 #if OS_VXWORKS
29107 pNew->pId = vxworksFindFileId(zFilename);
29108 if( pNew->pId==0 ){
29109 noLock = 1;
@@ -29112,56 +29226,24 @@
29226 pNew->lastErrno = 0;
29227 #if OS_VXWORKS
29228 if( rc!=SQLITE_OK ){
29229 if( h>=0 ) robust_close(pNew, h, __LINE__);
29230 h = -1;
29231 osUnlink(zFilename);
29232 isDelete = 0;
29233 }
29234 pNew->isDelete = isDelete;
29235 #endif
29236 if( rc!=SQLITE_OK ){
 
29237 if( h>=0 ) robust_close(pNew, h, __LINE__);
29238 }else{
29239 pNew->pMethod = pLockingStyle;
29240 OpenCounter(+1);
29241 }
29242 return rc;
29243 }
29244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29245 /*
29246 ** Return the name of a directory in which to put temporary files.
29247 ** If no suitable temporary file directory can be found, return NULL.
29248 */
29249 static const char *unixTempFileDir(void){
@@ -29272,11 +29354,11 @@
29354 ** ignored and -1 is returned. The caller will try to open a new file
29355 ** descriptor on the same path, fail, and return an error to SQLite.
29356 **
29357 ** Even if a subsequent open() call does succeed, the consequences of
29358 ** not searching for a resusable file descriptor are not dire. */
29359 if( 0==osStat(zPath, &sStat) ){
29360 unixInodeInfo *pInode;
29361
29362 unixEnterMutex();
29363 pInode = inodeList;
29364 while( pInode && (pInode->fileId.dev!=sStat.st_dev
@@ -29348,11 +29430,11 @@
29430 while( nDb>0 && zPath[nDb]!='-' ) nDb--;
29431 if( nDb==0 ) return SQLITE_OK;
29432 memcpy(zDb, zPath, nDb);
29433 zDb[nDb] = '\0';
29434
29435 if( 0==osStat(zDb, &sStat) ){
29436 *pMode = sStat.st_mode & 0777;
29437 }else{
29438 rc = SQLITE_IOERR_FSTAT;
29439 }
29440 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
@@ -29390,11 +29472,10 @@
29472 int flags, /* Input flags to control the opening */
29473 int *pOutFlags /* Output flags returned to SQLite core */
29474 ){
29475 unixFile *p = (unixFile *)pFile;
29476 int fd = -1; /* File descriptor returned by open() */
 
29477 int openFlags = 0; /* Flags to pass to open() */
29478 int eType = flags&0xFFFFFF00; /* Type of file to open */
29479 int noLock; /* True to omit locking primitives */
29480 int rc = SQLITE_OK; /* Function Return Code */
29481
@@ -29409,11 +29490,11 @@
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 */
29495 int syncDir = (isCreate && (
29496 eType==SQLITE_OPEN_MASTER_JOURNAL
29497 || eType==SQLITE_OPEN_MAIN_JOURNAL
29498 || eType==SQLITE_OPEN_WAL
29499 ));
29500
@@ -29463,11 +29544,11 @@
29544 }
29545 }
29546 p->pUnused = pUnused;
29547 }else if( !zName ){
29548 /* If zName is NULL, the upper layer is requesting a temp file. */
29549 assert(isDelete && !syncDir);
29550 rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
29551 if( rc!=SQLITE_OK ){
29552 return rc;
29553 }
29554 zName = zTmpname;
@@ -29519,32 +29600,19 @@
29600
29601 if( isDelete ){
29602 #if OS_VXWORKS
29603 zPath = zName;
29604 #else
29605 osUnlink(zName);
29606 #endif
29607 }
29608 #if SQLITE_ENABLE_LOCKING_STYLE
29609 else{
29610 p->openFlags = openFlags;
29611 }
29612 #endif
29613
 
 
 
 
 
 
 
 
 
 
 
 
 
29614 #ifdef FD_CLOEXEC
29615 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
29616 #endif
29617
29618 noLock = eType!=SQLITE_OPEN_MAIN_DB;
@@ -29552,11 +29620,10 @@
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 }
29628 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
29629 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
@@ -29584,21 +29651,18 @@
29651 ** then the call to close() will cancel those locks. In practice,
29652 ** we're assuming that statfs() doesn't fail very often. At least
29653 ** not while other file descriptors opened by the same process on
29654 ** the same file are working. */
29655 p->lastErrno = errno;
 
 
 
29656 robust_close(p, fd, __LINE__);
29657 rc = SQLITE_IOERR_ACCESS;
29658 goto open_finished;
29659 }
29660 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
29661 }
29662 if( useProxy ){
29663 rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
29664 isDelete, isReadonly);
29665 if( rc==SQLITE_OK ){
29666 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
29667 if( rc!=SQLITE_OK ){
29668 /* Use unixClose to clean up the resources added in fillInUnixFile
@@ -29612,11 +29676,11 @@
29676 goto open_finished;
29677 }
29678 }
29679 #endif
29680
29681 rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
29682 isDelete, isReadonly);
29683 open_finished:
29684 if( rc!=SQLITE_OK ){
29685 sqlite3_free(p->pUnused);
29686 }
@@ -29634,17 +29698,17 @@
29698 int dirSync /* If true, fsync() directory after deleting file */
29699 ){
29700 int rc = SQLITE_OK;
29701 UNUSED_PARAMETER(NotUsed);
29702 SimulateIOError(return SQLITE_IOERR_DELETE);
29703 if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
29704 return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29705 }
29706 #ifndef SQLITE_DISABLE_DIRSYNC
29707 if( dirSync ){
29708 int fd;
29709 rc = osOpenDirectory(zPath, &fd);
29710 if( rc==SQLITE_OK ){
29711 #if OS_VXWORKS
29712 if( fsync(fd)==-1 )
29713 #else
29714 if( fsync(fd) )
@@ -29693,11 +29757,11 @@
29757 assert(!"Invalid flags argument");
29758 }
29759 *pResOut = (osAccess(zPath, amode)==0);
29760 if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
29761 struct stat buf;
29762 if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
29763 *pResOut = 0;
29764 }
29765 }
29766 return SQLITE_OK;
29767 }
@@ -30212,11 +30276,10 @@
30276 const char *path, /* path for the new unixFile */
30277 unixFile **ppFile, /* unixFile created and returned by ref */
30278 int islockfile /* if non zero missing dirs will be created */
30279 ) {
30280 int fd = -1;
 
30281 unixFile *pNew;
30282 int rc = SQLITE_OK;
30283 int openFlags = O_RDWR | O_CREAT;
30284 sqlite3_vfs dummyVfs;
30285 int terrno = 0;
@@ -30277,11 +30340,11 @@
30340 dummyVfs.zName = "dummy";
30341 pUnused->fd = fd;
30342 pUnused->flags = openFlags;
30343 pNew->pUnused = pUnused;
30344
30345 rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
30346 if( rc==SQLITE_OK ){
30347 *ppFile = pNew;
30348 return SQLITE_OK;
30349 }
30350 end_create_proxy:
@@ -30391,11 +30454,11 @@
30454 conchFile->openFlags = O_RDWR | O_CREAT;
30455
30456 end_breaklock:
30457 if( rc ){
30458 if( fd>=0 ){
30459 osUnlink(tPath);
30460 robust_close(pFile, fd, __LINE__);
30461 }
30462 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
30463 }
30464 return rc;
@@ -31214,11 +31277,11 @@
31277 };
31278 unsigned int i; /* Loop counter */
31279
31280 /* Double-check that the aSyscall[] array has been constructed
31281 ** correctly. See ticket [bb3a86e890c8e96ab] */
31282 assert( ArraySize(aSyscall)==18 );
31283
31284 /* Register all VFSes defined in the aVfs[] array */
31285 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
31286 sqlite3_vfs_register(&aVfs[i], i==0);
31287 }
@@ -31331,14 +31394,17 @@
31394 #ifdef MEMORY_DEBUG
31395 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
31396 #endif
31397
31398 #ifdef SQLITE_DEBUG
31399 # ifndef SQLITE_DEBUG_OS_TRACE
31400 # define SQLITE_DEBUG_OS_TRACE 0
31401 # endif
31402 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
31403 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
31404 #else
31405 # define OSTRACE(X)
31406 #endif
31407
31408 /*
31409 ** Macros for performance tracing. Normally turned off. Only works
31410 ** on i486 hardware.
@@ -31546,12 +31612,13 @@
31612 typedef struct winFile winFile;
31613 struct winFile {
31614 const sqlite3_io_methods *pMethod; /*** Must be first ***/
31615 sqlite3_vfs *pVfs; /* The VFS used to open this file */
31616 HANDLE h; /* Handle for accessing the file */
31617 u8 locktype; /* Type of lock currently held on this file */
31618 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
31619 u8 bPersistWal; /* True to persist WAL files */
31620 DWORD lastErrno; /* The Windows errno from the last I/O error */
31621 DWORD sectorSize; /* Sector size of the device file is on */
31622 winShm *pShm; /* Instance of shared memory on this file */
31623 const char *zPath; /* Full pathname of this file */
31624 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
@@ -32778,29 +32845,39 @@
32845
32846 /*
32847 ** Control and query of the open file handle.
32848 */
32849 static int winFileControl(sqlite3_file *id, int op, void *pArg){
32850 winFile *pFile = (winFile*)id;
32851 switch( op ){
32852 case SQLITE_FCNTL_LOCKSTATE: {
32853 *(int*)pArg = pFile->locktype;
32854 return SQLITE_OK;
32855 }
32856 case SQLITE_LAST_ERRNO: {
32857 *(int*)pArg = (int)pFile->lastErrno;
32858 return SQLITE_OK;
32859 }
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;
32875 }else{
32876 pFile->bPersistWal = bPersist!=0;
32877 }
32878 return SQLITE_OK;
32879 }
32880 case SQLITE_FCNTL_SYNC_OMITTED: {
32881 return SQLITE_OK;
32882 }
32883 case SQLITE_FCNTL_WIN32_AV_RETRY: {
@@ -33623,10 +33700,11 @@
33700 int isTemp = 0;
33701 #endif
33702 winFile *pFile = (winFile*)id;
33703 void *zConverted; /* Filename in OS encoding */
33704 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
33705 int cnt = 0;
33706
33707 /* If argument zPath is a NULL pointer, this function is required to open
33708 ** a temporary file. Use this buffer to store the file name in.
33709 */
33710 char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */
@@ -33742,34 +33820,34 @@
33820 #if SQLITE_OS_WINCE
33821 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
33822 #endif
33823
33824 if( isNT() ){
33825 while( (h = CreateFileW((WCHAR*)zConverted,
33826 dwDesiredAccess,
33827 dwShareMode, NULL,
33828 dwCreationDisposition,
33829 dwFlagsAndAttributes,
33830 NULL))==INVALID_HANDLE_VALUE &&
33831 retryIoerr(&cnt) ){}
 
33832 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33833 ** Since the ASCII version of these Windows API do not exist for WINCE,
33834 ** it's important to not reference them for WINCE builds.
33835 */
33836 #if SQLITE_OS_WINCE==0
33837 }else{
33838 while( (h = CreateFileA((char*)zConverted,
33839 dwDesiredAccess,
33840 dwShareMode, NULL,
33841 dwCreationDisposition,
33842 dwFlagsAndAttributes,
33843 NULL))==INVALID_HANDLE_VALUE &&
33844 retryIoerr(&cnt) ){}
 
33845 #endif
33846 }
33847
33848 logIoerr(cnt);
33849
33850 OSTRACE(("OPEN %d %s 0x%lx %s\n",
33851 h, zName, dwDesiredAccess,
33852 h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
33853
@@ -33895,15 +33973,17 @@
33973 zConverted = convertUtf8Filename(zFilename);
33974 if( zConverted==0 ){
33975 return SQLITE_NOMEM;
33976 }
33977 if( isNT() ){
33978 int cnt = 0;
33979 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
33980 memset(&sAttrData, 0, sizeof(sAttrData));
33981 while( !(rc = GetFileAttributesExW((WCHAR*)zConverted,
33982 GetFileExInfoStandard,
33983 &sAttrData)) && retryIoerr(&cnt) ){}
33984 if( rc ){
33985 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
33986 ** as if it does not exist.
33987 */
33988 if( flags==SQLITE_ACCESS_EXISTS
33989 && sAttrData.nFileSizeHigh==0
@@ -33911,10 +33991,11 @@
33991 attr = INVALID_FILE_ATTRIBUTES;
33992 }else{
33993 attr = sAttrData.dwFileAttributes;
33994 }
33995 }else{
33996 logIoerr(cnt);
33997 if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
33998 winLogError(SQLITE_IOERR_ACCESS, "winAccess", zFilename);
33999 free(zConverted);
34000 return SQLITE_IOERR_ACCESS;
34001 }else{
@@ -33935,11 +34016,12 @@
34016 case SQLITE_ACCESS_READ:
34017 case SQLITE_ACCESS_EXISTS:
34018 rc = attr!=INVALID_FILE_ATTRIBUTES;
34019 break;
34020 case SQLITE_ACCESS_READWRITE:
34021 rc = attr!=INVALID_FILE_ATTRIBUTES &&
34022 (attr & FILE_ATTRIBUTE_READONLY)==0;
34023 break;
34024 default:
34025 assert(!"Invalid flags argument");
34026 }
34027 *pResOut = rc;
@@ -45519,17 +45601,19 @@
45601 **
45602 ** The EXCLUSIVE lock is not released before returning.
45603 */
45604 rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
45605 if( rc==SQLITE_OK ){
45606 int bPersistWal = -1;
45607 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
45608 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
45609 }
45610 rc = sqlite3WalCheckpoint(
45611 pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
45612 );
45613 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersistWal);
45614 if( rc==SQLITE_OK && bPersistWal!=1 ){
45615 isDelete = 1;
45616 }
45617 }
45618
45619 walIndexClose(pWal, isDelete);
@@ -57508,15 +57592,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
@@ -58208,12 +58292,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
@@ -63874,10 +63958,15 @@
63958 assert( memIsValid(pIn1) );
63959 memAboutToChange(p, pOut);
63960 u.ac.zMalloc = pOut->zMalloc;
63961 pOut->zMalloc = 0;
63962 sqlite3VdbeMemMove(pOut, pIn1);
63963 #ifdef SQLITE_DEBUG
63964 if( pOut->pScopyFrom>=&aMem[u.ac.p1] && pOut->pScopyFrom<&aMem[u.ac.p1+pOp->p3] ){
63965 pOut->pScopyFrom += u.ac.p1 - pOp->p2;
63966 }
63967 #endif
63968 pIn1->zMalloc = u.ac.zMalloc;
63969 REGISTER_TRACE(u.ac.p2++, pOut);
63970 pIn1++;
63971 pOut++;
63972 }
@@ -75923,10 +76012,100 @@
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
@@ -75954,12 +76133,18 @@
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};
@@ -75971,10 +76156,21 @@
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
@@ -76001,17 +76197,238 @@
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 */
@@ -76031,24 +76448,31 @@
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 }
@@ -76077,17 +76501,22 @@
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);
@@ -76094,35 +76523,24 @@
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.
@@ -76148,79 +76566,87 @@
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
@@ -76236,50 +76662,51 @@
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 */
@@ -76300,11 +76727,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);
@@ -76325,11 +76752,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 }
@@ -76430,11 +76857,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
@@ -76473,40 +76900,172 @@
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
@@ -76524,12 +77083,14 @@
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;
@@ -76537,91 +77098,23 @@
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;
@@ -79422,10 +79915,108 @@
79915 iDestroyed = iLargest;
79916 }
79917 }
79918 #endif
79919 }
79920
79921 /*
79922 ** Remove entries from the sqlite_stat1 and sqlite_stat2 tables
79923 ** after a DROP INDEX or DROP TABLE command.
79924 */
79925 static void sqlite3ClearStatTables(
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 "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.
80022 */
@@ -79491,11 +80082,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
@@ -79515,79 +80106,15 @@
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
@@ -80030,24 +80557,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;
@@ -80320,13 +80847,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++){
@@ -80392,19 +80919,13 @@
80919 v = sqlite3GetVdbe(pParse);
80920 if( v ){
80921 sqlite3BeginWriteOperation(pParse, 1, iDb);
80922 sqlite3NestedParse(pParse,
80923 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
80924 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
80925 );
80926 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
 
 
 
 
 
 
80927 sqlite3ChangeCookie(pParse, iDb);
80928 destroyRootPage(pParse, pIndex->tnum, iDb);
80929 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
80930 }
80931
@@ -98638,11 +99159,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
@@ -99852,11 +100373,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 **
@@ -99891,11 +100412,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;
@@ -100662,10 +101183,11 @@
101183 if( pTerm->leftCursor != pSrc->iCursor ) continue;
101184 assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
101185 testcase( pTerm->eOperator==WO_IN );
101186 testcase( pTerm->eOperator==WO_ISNULL );
101187 if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
101188 if( pTerm->wtFlags & TERM_VNULL ) continue;
101189 nTerm++;
101190 }
101191
101192 /* If the ORDER BY clause contains only columns in the current
101193 ** virtual table then allocate space for the aOrderBy part of
@@ -100712,10 +101234,11 @@
101234 if( pTerm->leftCursor != pSrc->iCursor ) continue;
101235 assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
101236 testcase( pTerm->eOperator==WO_IN );
101237 testcase( pTerm->eOperator==WO_ISNULL );
101238 if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
101239 if( pTerm->wtFlags & TERM_VNULL ) continue;
101240 pIdxCons[j].iColumn = pTerm->u.leftColumn;
101241 pIdxCons[j].iTermOffset = i;
101242 pIdxCons[j].op = (u8)pTerm->eOperator;
101243 /* The direct assignment in the previous line is possible only because
101244 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
@@ -100938,71 +101461,89 @@
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{
@@ -101017,16 +101558,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
@@ -101040,20 +101581,52 @@
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
@@ -101067,11 +101640,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
@@ -101115,106 +101688,92 @@
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
@@ -101230,14 +101789,13 @@
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);
@@ -101244,30 +101802,22 @@
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 **
@@ -101286,64 +101836,29 @@
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
@@ -101386,11 +101901,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));
@@ -101441,11 +101956,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;
@@ -101484,18 +101999,16 @@
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).
@@ -101516,17 +102029,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++){
@@ -101546,23 +102059,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 }
@@ -101630,11 +102143,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.
@@ -101646,16 +102159,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
@@ -101780,14 +102293,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.
102307
+17 -2
--- 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-07-19 18:29:00 ed5f0aad6b21066bacd01521e82c22e96991f400"
112
+#define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -755,10 +755,24 @@
755755
** integers where the first integer i the new retry count and the second
756756
** integer is the delay. If either integer is negative, then the setting
757757
** is not changed but instead the prior value of that setting is written
758758
** into the array entry, allowing the current retry settings to be
759759
** interrogated. The zDbName parameter is ignored.
760
+**
761
+** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
762
+** persistent [WAL | Write AHead Log] setting. By default, the auxiliary
763
+** write ahead log and shared memory files used for transaction control
764
+** are automatically deleted when the latest connection to the database
765
+** closes. Setting persistent WAL mode causes those files to persist after
766
+** close. Persisting the files is useful when other processes that do not
767
+** have write permission on the directory containing the database file want
768
+** to read the database file, as the WAL and shared memory files must exist
769
+** in order for the database to be readable. The fourth parameter to
770
+** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
771
+** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
772
+** WAL mode. If the integer is -1, then it is overwritten with the current
773
+** WAL persistence setting.
760774
**
761775
*/
762776
#define SQLITE_FCNTL_LOCKSTATE 1
763777
#define SQLITE_GET_LOCKPROXYFILE 2
764778
#define SQLITE_SET_LOCKPROXYFILE 3
@@ -766,10 +780,11 @@
766780
#define SQLITE_FCNTL_SIZE_HINT 5
767781
#define SQLITE_FCNTL_CHUNK_SIZE 6
768782
#define SQLITE_FCNTL_FILE_POINTER 7
769783
#define SQLITE_FCNTL_SYNC_OMITTED 8
770784
#define SQLITE_FCNTL_WIN32_AV_RETRY 9
785
+#define SQLITE_FCNTL_PERSIST_WAL 10
771786
772787
/*
773788
** CAPI3REF: Mutex Handle
774789
**
775790
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2839,11 +2854,11 @@
28392854
** a schema change, on the first [sqlite3_step()] call following any change
28402855
** to the [sqlite3_bind_text | bindings] of that [parameter].
28412856
** ^The specific value of WHERE-clause [parameter] might influence the
28422857
** choice of query plan if the parameter is the left-hand side of a [LIKE]
28432858
** or [GLOB] operator or if the parameter is compared to an indexed column
2844
-** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
2859
+** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
28452860
** the
28462861
** </li>
28472862
** </ol>
28482863
*/
28492864
SQLITE_API int sqlite3_prepare(
28502865
--- 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-07-19 18:29:00 ed5f0aad6b21066bacd01521e82c22e96991f400"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -755,10 +755,24 @@
755 ** integers where the first integer i the new retry count and the second
756 ** integer is the delay. If either integer is negative, then the setting
757 ** is not changed but instead the prior value of that setting is written
758 ** into the array entry, allowing the current retry settings to be
759 ** interrogated. The zDbName parameter is ignored.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760 **
761 */
762 #define SQLITE_FCNTL_LOCKSTATE 1
763 #define SQLITE_GET_LOCKPROXYFILE 2
764 #define SQLITE_SET_LOCKPROXYFILE 3
@@ -766,10 +780,11 @@
766 #define SQLITE_FCNTL_SIZE_HINT 5
767 #define SQLITE_FCNTL_CHUNK_SIZE 6
768 #define SQLITE_FCNTL_FILE_POINTER 7
769 #define SQLITE_FCNTL_SYNC_OMITTED 8
770 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
 
771
772 /*
773 ** CAPI3REF: Mutex Handle
774 **
775 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2839,11 +2854,11 @@
2839 ** a schema change, on the first [sqlite3_step()] call following any change
2840 ** to the [sqlite3_bind_text | bindings] of that [parameter].
2841 ** ^The specific value of WHERE-clause [parameter] might influence the
2842 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
2843 ** or [GLOB] operator or if the parameter is compared to an indexed column
2844 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
2845 ** the
2846 ** </li>
2847 ** </ol>
2848 */
2849 SQLITE_API int sqlite3_prepare(
2850
--- 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 **
@@ -755,10 +755,24 @@
755 ** integers where the first integer i the new retry count and the second
756 ** integer is the delay. If either integer is negative, then the setting
757 ** is not changed but instead the prior value of that setting is written
758 ** into the array entry, allowing the current retry settings to be
759 ** interrogated. The zDbName parameter is ignored.
760 **
761 ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
762 ** persistent [WAL | Write AHead Log] setting. By default, the auxiliary
763 ** write ahead log and shared memory files used for transaction control
764 ** are automatically deleted when the latest connection to the database
765 ** closes. Setting persistent WAL mode causes those files to persist after
766 ** close. Persisting the files is useful when other processes that do not
767 ** have write permission on the directory containing the database file want
768 ** to read the database file, as the WAL and shared memory files must exist
769 ** in order for the database to be readable. The fourth parameter to
770 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
771 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
772 ** WAL mode. If the integer is -1, then it is overwritten with the current
773 ** WAL persistence setting.
774 **
775 */
776 #define SQLITE_FCNTL_LOCKSTATE 1
777 #define SQLITE_GET_LOCKPROXYFILE 2
778 #define SQLITE_SET_LOCKPROXYFILE 3
@@ -766,10 +780,11 @@
780 #define SQLITE_FCNTL_SIZE_HINT 5
781 #define SQLITE_FCNTL_CHUNK_SIZE 6
782 #define SQLITE_FCNTL_FILE_POINTER 7
783 #define SQLITE_FCNTL_SYNC_OMITTED 8
784 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
785 #define SQLITE_FCNTL_PERSIST_WAL 10
786
787 /*
788 ** CAPI3REF: Mutex Handle
789 **
790 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2839,11 +2854,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
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -20,11 +20,11 @@
2020
CFLAGS = -o
2121
BCC = $(DMDIR)\bin\dmc $(CFLAGS)
2222
TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(SSL) $(INCL)
2323
LIBS = $(DMDIR)\extra\lib\ zlib wsock32
2424
25
-SQLITE_OPTIONS = -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0
25
+SQLITE_OPTIONS = -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT3 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0
2626
2727
SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c glob_.c graph_.c gzip_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c path_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
2828
2929
OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\glob$O $(OBJDIR)\graph$O $(OBJDIR)\gzip$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\path$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\tar$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
3030
3131
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -20,11 +20,11 @@
20 CFLAGS = -o
21 BCC = $(DMDIR)\bin\dmc $(CFLAGS)
22 TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(SSL) $(INCL)
23 LIBS = $(DMDIR)\extra\lib\ zlib wsock32
24
25 SQLITE_OPTIONS = -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0
26
27 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c glob_.c graph_.c gzip_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c path_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
28
29 OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\glob$O $(OBJDIR)\graph$O $(OBJDIR)\gzip$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\path$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\tar$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
30
31
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -20,11 +20,11 @@
20 CFLAGS = -o
21 BCC = $(DMDIR)\bin\dmc $(CFLAGS)
22 TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(SSL) $(INCL)
23 LIBS = $(DMDIR)\extra\lib\ zlib wsock32
24
25 SQLITE_OPTIONS = -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT3 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0
26
27 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c glob_.c graph_.c gzip_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c path_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
28
29 OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\glob$O $(OBJDIR)\graph$O $(OBJDIR)\gzip$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\path$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\tar$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
30
31
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -968,11 +968,11 @@
968968
$(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
969969
$(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
970970
971971
zip.h: $(OBJDIR)/headers
972972
$(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
973
- $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
973
+ $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT3 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
974974
975975
$(OBJDIR)/shell.o: $(SRCDIR)/shell.c $(SRCDIR)/sqlite3.h
976976
$(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
977977
978978
$(OBJDIR)/th.o: $(SRCDIR)/th.c
979979
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -968,11 +968,11 @@
968 $(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
969 $(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
970
971 zip.h: $(OBJDIR)/headers
972 $(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
973 $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
974
975 $(OBJDIR)/shell.o: $(SRCDIR)/shell.c $(SRCDIR)/sqlite3.h
976 $(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
977
978 $(OBJDIR)/th.o: $(SRCDIR)/th.c
979
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -968,11 +968,11 @@
968 $(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
969 $(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
970
971 zip.h: $(OBJDIR)/headers
972 $(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
973 $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT3 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
974
975 $(OBJDIR)/shell.o: $(SRCDIR)/shell.c $(SRCDIR)/sqlite3.h
976 $(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
977
978 $(OBJDIR)/th.o: $(SRCDIR)/th.c
979
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,11 +34,11 @@
3434
BCC = $(CC) $(CFLAGS)
3535
TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
3636
LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
3737
LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
3838
39
-SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT2 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
39
+SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
4040
4141
SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c glob_.c graph_.c gzip_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c path_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
4242
4343
OBJ = $(OX)\add$O $(OX)\allrepo$O $(OX)\attach$O $(OX)\bag$O $(OX)\bisect$O $(OX)\blob$O $(OX)\branch$O $(OX)\browse$O $(OX)\captcha$O $(OX)\cgi$O $(OX)\checkin$O $(OX)\checkout$O $(OX)\clearsign$O $(OX)\clone$O $(OX)\comformat$O $(OX)\configure$O $(OX)\content$O $(OX)\db$O $(OX)\delta$O $(OX)\deltacmd$O $(OX)\descendants$O $(OX)\diff$O $(OX)\diffcmd$O $(OX)\doc$O $(OX)\encode$O $(OX)\event$O $(OX)\export$O $(OX)\file$O $(OX)\finfo$O $(OX)\glob$O $(OX)\graph$O $(OX)\gzip$O $(OX)\http$O $(OX)\http_socket$O $(OX)\http_ssl$O $(OX)\http_transport$O $(OX)\import$O $(OX)\info$O $(OX)\leaf$O $(OX)\login$O $(OX)\main$O $(OX)\manifest$O $(OX)\md5$O $(OX)\merge$O $(OX)\merge3$O $(OX)\name$O $(OX)\path$O $(OX)\pivot$O $(OX)\popen$O $(OX)\pqueue$O $(OX)\printf$O $(OX)\rebuild$O $(OX)\report$O $(OX)\rss$O $(OX)\schema$O $(OX)\search$O $(OX)\setup$O $(OX)\sha1$O $(OX)\shun$O $(OX)\skins$O $(OX)\sqlcmd$O $(OX)\stash$O $(OX)\stat$O $(OX)\style$O $(OX)\sync$O $(OX)\tag$O $(OX)\tar$O $(OX)\th_main$O $(OX)\timeline$O $(OX)\tkt$O $(OX)\tktsetup$O $(OX)\undo$O $(OX)\update$O $(OX)\url$O $(OX)\user$O $(OX)\verify$O $(OX)\vfile$O $(OX)\wiki$O $(OX)\wikiformat$O $(OX)\winhttp$O $(OX)\xfer$O $(OX)\zip$O $(OX)\shell$O $(OX)\sqlite3$O $(OX)\th$O $(OX)\th_lang$O
4444
4545
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,11 +34,11 @@
34 BCC = $(CC) $(CFLAGS)
35 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
36 LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
37 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
38
39 SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT2 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
40
41 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c glob_.c graph_.c gzip_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c path_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
42
43 OBJ = $(OX)\add$O $(OX)\allrepo$O $(OX)\attach$O $(OX)\bag$O $(OX)\bisect$O $(OX)\blob$O $(OX)\branch$O $(OX)\browse$O $(OX)\captcha$O $(OX)\cgi$O $(OX)\checkin$O $(OX)\checkout$O $(OX)\clearsign$O $(OX)\clone$O $(OX)\comformat$O $(OX)\configure$O $(OX)\content$O $(OX)\db$O $(OX)\delta$O $(OX)\deltacmd$O $(OX)\descendants$O $(OX)\diff$O $(OX)\diffcmd$O $(OX)\doc$O $(OX)\encode$O $(OX)\event$O $(OX)\export$O $(OX)\file$O $(OX)\finfo$O $(OX)\glob$O $(OX)\graph$O $(OX)\gzip$O $(OX)\http$O $(OX)\http_socket$O $(OX)\http_ssl$O $(OX)\http_transport$O $(OX)\import$O $(OX)\info$O $(OX)\leaf$O $(OX)\login$O $(OX)\main$O $(OX)\manifest$O $(OX)\md5$O $(OX)\merge$O $(OX)\merge3$O $(OX)\name$O $(OX)\path$O $(OX)\pivot$O $(OX)\popen$O $(OX)\pqueue$O $(OX)\printf$O $(OX)\rebuild$O $(OX)\report$O $(OX)\rss$O $(OX)\schema$O $(OX)\search$O $(OX)\setup$O $(OX)\sha1$O $(OX)\shun$O $(OX)\skins$O $(OX)\sqlcmd$O $(OX)\stash$O $(OX)\stat$O $(OX)\style$O $(OX)\sync$O $(OX)\tag$O $(OX)\tar$O $(OX)\th_main$O $(OX)\timeline$O $(OX)\tkt$O $(OX)\tktsetup$O $(OX)\undo$O $(OX)\update$O $(OX)\url$O $(OX)\user$O $(OX)\verify$O $(OX)\vfile$O $(OX)\wiki$O $(OX)\wikiformat$O $(OX)\winhttp$O $(OX)\xfer$O $(OX)\zip$O $(OX)\shell$O $(OX)\sqlite3$O $(OX)\th$O $(OX)\th_lang$O
44
45
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,11 +34,11 @@
34 BCC = $(CC) $(CFLAGS)
35 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(SSL) $(INCL)
36 LIBS = $(ZLIB) ws2_32.lib advapi32.lib $(SSLLIB)
37 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
38
39 SQLITE_OPTIONS = /DSQLITE_OMIT_LOAD_EXTENSION=1 /DSQLITE_THREADSAFE=0 /DSQLITE_DEFAULT_FILE_FORMAT=4 /DSQLITE_ENABLE_STAT3 /Dlocaltime=fossil_localtime /DSQLITE_ENABLE_LOCKING_STYLE=0
40
41 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c glob_.c graph_.c gzip_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c path_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c tar_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
42
43 OBJ = $(OX)\add$O $(OX)\allrepo$O $(OX)\attach$O $(OX)\bag$O $(OX)\bisect$O $(OX)\blob$O $(OX)\branch$O $(OX)\browse$O $(OX)\captcha$O $(OX)\cgi$O $(OX)\checkin$O $(OX)\checkout$O $(OX)\clearsign$O $(OX)\clone$O $(OX)\comformat$O $(OX)\configure$O $(OX)\content$O $(OX)\db$O $(OX)\delta$O $(OX)\deltacmd$O $(OX)\descendants$O $(OX)\diff$O $(OX)\diffcmd$O $(OX)\doc$O $(OX)\encode$O $(OX)\event$O $(OX)\export$O $(OX)\file$O $(OX)\finfo$O $(OX)\glob$O $(OX)\graph$O $(OX)\gzip$O $(OX)\http$O $(OX)\http_socket$O $(OX)\http_ssl$O $(OX)\http_transport$O $(OX)\import$O $(OX)\info$O $(OX)\leaf$O $(OX)\login$O $(OX)\main$O $(OX)\manifest$O $(OX)\md5$O $(OX)\merge$O $(OX)\merge3$O $(OX)\name$O $(OX)\path$O $(OX)\pivot$O $(OX)\popen$O $(OX)\pqueue$O $(OX)\printf$O $(OX)\rebuild$O $(OX)\report$O $(OX)\rss$O $(OX)\schema$O $(OX)\search$O $(OX)\setup$O $(OX)\sha1$O $(OX)\shun$O $(OX)\skins$O $(OX)\sqlcmd$O $(OX)\stash$O $(OX)\stat$O $(OX)\style$O $(OX)\sync$O $(OX)\tag$O $(OX)\tar$O $(OX)\th_main$O $(OX)\timeline$O $(OX)\tkt$O $(OX)\tktsetup$O $(OX)\undo$O $(OX)\update$O $(OX)\url$O $(OX)\user$O $(OX)\verify$O $(OX)\vfile$O $(OX)\wiki$O $(OX)\wikiformat$O $(OX)\winhttp$O $(OX)\xfer$O $(OX)\zip$O $(OX)\shell$O $(OX)\sqlite3$O $(OX)\th$O $(OX)\th_lang$O
44
45

Keyboard Shortcuts

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