Fossil SCM

Update the built-in SQLite to the latest 3.8.11 alpha version.

drh 2015-06-30 15:18 UTC trunk
Commit 0fc8b9df0c4e27b6b7de7283772c67a273f4e9d0
3 files changed +34 -32 +4545 -3669 +3 -3
+34 -32
--- src/shell.c
+++ src/shell.c
@@ -99,32 +99,30 @@
9999
100100
101101
#if defined(_WIN32) || defined(WIN32)
102102
# include <io.h>
103103
# include <fcntl.h>
104
-#define isatty(h) _isatty(h)
105
-#ifndef access
106
-# define access(f,m) _access((f),(m))
107
-#endif
108
-#undef popen
109
-#define popen _popen
110
-#undef pclose
111
-#define pclose _pclose
112
-#else
113
-/* Make sure isatty() has a prototype.
114
-*/
115
-extern int isatty(int);
116
-
117
-#if !defined(__RTP__) && !defined(_WRS_KERNEL)
118
- /* popen and pclose are not C89 functions and so are sometimes omitted from
119
- ** the <stdio.h> header */
120
- extern FILE *popen(const char*,const char*);
121
- extern int pclose(FILE*);
122
-#else
123
-# define SQLITE_OMIT_POPEN 1
124
-#endif
125
-
104
+# define isatty(h) _isatty(h)
105
+# ifndef access
106
+# define access(f,m) _access((f),(m))
107
+# endif
108
+# undef popen
109
+# define popen _popen
110
+# undef pclose
111
+# define pclose _pclose
112
+#else
113
+ /* Make sure isatty() has a prototype. */
114
+ extern int isatty(int);
115
+
116
+# if !defined(__RTP__) && !defined(_WRS_KERNEL)
117
+ /* popen and pclose are not C89 functions and so are
118
+ ** sometimes omitted from the <stdio.h> header */
119
+ extern FILE *popen(const char*,const char*);
120
+ extern int pclose(FILE*);
121
+# else
122
+# define SQLITE_OMIT_POPEN 1
123
+# endif
126124
#endif
127125
128126
#if defined(_WIN32_WCE)
129127
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
130128
* thus we always assume that we have a console. That can be
@@ -1329,11 +1327,14 @@
13291327
*/
13301328
static void display_scanstats(
13311329
sqlite3 *db, /* Database to query */
13321330
ShellState *pArg /* Pointer to ShellState */
13331331
){
1334
-#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1332
+#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
1333
+ UNUSED_PARAMETER(db);
1334
+ UNUSED_PARAMETER(pArg);
1335
+#else
13351336
int i, k, n, mx;
13361337
fprintf(pArg->out, "-------- scanstats --------\n");
13371338
mx = 0;
13381339
for(k=0; k<=mx; k++){
13391340
double rEstLoop = 1.0;
@@ -1864,10 +1865,11 @@
18641865
const char *zName;
18651866
FILE *in;
18661867
long nIn;
18671868
void *pBuf;
18681869
1870
+ UNUSED_PARAMETER(argc);
18691871
zName = (const char*)sqlite3_value_text(argv[0]);
18701872
if( zName==0 ) return;
18711873
in = fopen(zName, "rb");
18721874
if( in==0 ) return;
18731875
fseek(in, 0, SEEK_END);
@@ -1896,10 +1898,11 @@
18961898
FILE *out;
18971899
const char *z;
18981900
sqlite3_int64 rc;
18991901
const char *zFile;
19001902
1903
+ UNUSED_PARAMETER(argc);
19011904
zFile = (const char*)sqlite3_value_text(argv[0]);
19021905
if( zFile==0 ) return;
19031906
out = fopen(zFile, "wb");
19041907
if( out==0 ) return;
19051908
z = (const char*)sqlite3_value_blob(argv[1]);
@@ -2575,11 +2578,11 @@
25752578
if( i==1 ) i = 65536;
25762579
fprintf(p->out, "%-20s %d\n", "database page size:", i);
25772580
fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
25782581
fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
25792582
fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
2580
- for(i=0; i<sizeof(aField)/sizeof(aField[0]); i++){
2583
+ for(i=0; i<ArraySize(aField); i++){
25812584
int ofst = aField[i].ofst;
25822585
unsigned int val = get4byteInt(aHdr + ofst);
25832586
fprintf(p->out, "%-20s %u", aField[i].zName, val);
25842587
switch( ofst ){
25852588
case 56: {
@@ -2595,11 +2598,11 @@
25952598
}else if( strcmp(zDb,"temp")==0 ){
25962599
zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
25972600
}else{
25982601
zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
25992602
}
2600
- for(i=0; i<sizeof(aQuery)/sizeof(aQuery[0]); i++){
2603
+ for(i=0; i<ArraySize(aQuery); i++){
26012604
char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
26022605
int val = db_int(p, zSql);
26032606
sqlite3_free(zSql);
26042607
fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
26052608
}
@@ -3226,11 +3229,11 @@
32263229
{ "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
32273230
};
32283231
int i, n2;
32293232
open_db(p, 0);
32303233
if( nArg==1 ){
3231
- for(i=0; i<sizeof(aLimit)/sizeof(aLimit[0]); i++){
3234
+ for(i=0; i<ArraySize(aLimit); i++){
32323235
printf("%20s %d\n", aLimit[i].zLimitName,
32333236
sqlite3_limit(p->db, aLimit[i].limitCode, -1));
32343237
}
32353238
}else if( nArg>3 ){
32363239
fprintf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
@@ -3237,11 +3240,11 @@
32373240
rc = 1;
32383241
goto meta_command_exit;
32393242
}else{
32403243
int iLimit = -1;
32413244
n2 = strlen30(azArg[1]);
3242
- for(i=0; i<sizeof(aLimit)/sizeof(aLimit[0]); i++){
3245
+ for(i=0; i<ArraySize(aLimit); i++){
32433246
if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
32443247
if( iLimit<0 ){
32453248
iLimit = i;
32463249
}else{
32473250
fprintf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
@@ -3347,13 +3350,12 @@
33473350
if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
33483351
sqlite3 *savedDb = p->db;
33493352
const char *zSavedFilename = p->zDbFilename;
33503353
char *zNewFilename = 0;
33513354
p->db = 0;
3352
- if( nArg>=2 ){
3353
- p->zDbFilename = zNewFilename = sqlite3_mprintf("%s", azArg[1]);
3354
- }
3355
+ if( nArg>=2 ) zNewFilename = sqlite3_mprintf("%s", azArg[1]);
3356
+ p->zDbFilename = zNewFilename;
33553357
open_db(p, 1);
33563358
if( p->db!=0 ){
33573359
sqlite3_close(savedDb);
33583360
sqlite3_free(p->zFreeOnClose);
33593361
p->zFreeOnClose = zNewFilename;
@@ -3813,11 +3815,11 @@
38133815
open_db(p, 0);
38143816
38153817
/* convert testctrl text option to value. allow any unique prefix
38163818
** of the option name, or a numerical value. */
38173819
n2 = strlen30(azArg[1]);
3818
- for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
3820
+ for(i=0; i<ArraySize(aCtrl); i++){
38193821
if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
38203822
if( testctrl<0 ){
38213823
testctrl = aCtrl[i].ctrlCode;
38223824
}else{
38233825
fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
@@ -4790,11 +4792,11 @@
47904792
nHistory = strlen30(zHome) + 20;
47914793
if( (zHistory = malloc(nHistory))!=0 ){
47924794
sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
47934795
}
47944796
}
4795
- if( zHistory ) shell_read_history(zHistory);
4797
+ if( zHistory ){ shell_read_history(zHistory); }
47964798
rc = process_input(&data, 0);
47974799
if( zHistory ){
47984800
shell_stifle_history(100);
47994801
shell_write_history(zHistory);
48004802
free(zHistory);
48014803
--- src/shell.c
+++ src/shell.c
@@ -99,32 +99,30 @@
99
100
101 #if defined(_WIN32) || defined(WIN32)
102 # include <io.h>
103 # include <fcntl.h>
104 #define isatty(h) _isatty(h)
105 #ifndef access
106 # define access(f,m) _access((f),(m))
107 #endif
108 #undef popen
109 #define popen _popen
110 #undef pclose
111 #define pclose _pclose
112 #else
113 /* Make sure isatty() has a prototype.
114 */
115 extern int isatty(int);
116
117 #if !defined(__RTP__) && !defined(_WRS_KERNEL)
118 /* popen and pclose are not C89 functions and so are sometimes omitted from
119 ** the <stdio.h> header */
120 extern FILE *popen(const char*,const char*);
121 extern int pclose(FILE*);
122 #else
123 # define SQLITE_OMIT_POPEN 1
124 #endif
125
126 #endif
127
128 #if defined(_WIN32_WCE)
129 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
130 * thus we always assume that we have a console. That can be
@@ -1329,11 +1327,14 @@
1329 */
1330 static void display_scanstats(
1331 sqlite3 *db, /* Database to query */
1332 ShellState *pArg /* Pointer to ShellState */
1333 ){
1334 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 
 
 
1335 int i, k, n, mx;
1336 fprintf(pArg->out, "-------- scanstats --------\n");
1337 mx = 0;
1338 for(k=0; k<=mx; k++){
1339 double rEstLoop = 1.0;
@@ -1864,10 +1865,11 @@
1864 const char *zName;
1865 FILE *in;
1866 long nIn;
1867 void *pBuf;
1868
 
1869 zName = (const char*)sqlite3_value_text(argv[0]);
1870 if( zName==0 ) return;
1871 in = fopen(zName, "rb");
1872 if( in==0 ) return;
1873 fseek(in, 0, SEEK_END);
@@ -1896,10 +1898,11 @@
1896 FILE *out;
1897 const char *z;
1898 sqlite3_int64 rc;
1899 const char *zFile;
1900
 
1901 zFile = (const char*)sqlite3_value_text(argv[0]);
1902 if( zFile==0 ) return;
1903 out = fopen(zFile, "wb");
1904 if( out==0 ) return;
1905 z = (const char*)sqlite3_value_blob(argv[1]);
@@ -2575,11 +2578,11 @@
2575 if( i==1 ) i = 65536;
2576 fprintf(p->out, "%-20s %d\n", "database page size:", i);
2577 fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
2578 fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
2579 fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
2580 for(i=0; i<sizeof(aField)/sizeof(aField[0]); i++){
2581 int ofst = aField[i].ofst;
2582 unsigned int val = get4byteInt(aHdr + ofst);
2583 fprintf(p->out, "%-20s %u", aField[i].zName, val);
2584 switch( ofst ){
2585 case 56: {
@@ -2595,11 +2598,11 @@
2595 }else if( strcmp(zDb,"temp")==0 ){
2596 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
2597 }else{
2598 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
2599 }
2600 for(i=0; i<sizeof(aQuery)/sizeof(aQuery[0]); i++){
2601 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
2602 int val = db_int(p, zSql);
2603 sqlite3_free(zSql);
2604 fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
2605 }
@@ -3226,11 +3229,11 @@
3226 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
3227 };
3228 int i, n2;
3229 open_db(p, 0);
3230 if( nArg==1 ){
3231 for(i=0; i<sizeof(aLimit)/sizeof(aLimit[0]); i++){
3232 printf("%20s %d\n", aLimit[i].zLimitName,
3233 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
3234 }
3235 }else if( nArg>3 ){
3236 fprintf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
@@ -3237,11 +3240,11 @@
3237 rc = 1;
3238 goto meta_command_exit;
3239 }else{
3240 int iLimit = -1;
3241 n2 = strlen30(azArg[1]);
3242 for(i=0; i<sizeof(aLimit)/sizeof(aLimit[0]); i++){
3243 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
3244 if( iLimit<0 ){
3245 iLimit = i;
3246 }else{
3247 fprintf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
@@ -3347,13 +3350,12 @@
3347 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
3348 sqlite3 *savedDb = p->db;
3349 const char *zSavedFilename = p->zDbFilename;
3350 char *zNewFilename = 0;
3351 p->db = 0;
3352 if( nArg>=2 ){
3353 p->zDbFilename = zNewFilename = sqlite3_mprintf("%s", azArg[1]);
3354 }
3355 open_db(p, 1);
3356 if( p->db!=0 ){
3357 sqlite3_close(savedDb);
3358 sqlite3_free(p->zFreeOnClose);
3359 p->zFreeOnClose = zNewFilename;
@@ -3813,11 +3815,11 @@
3813 open_db(p, 0);
3814
3815 /* convert testctrl text option to value. allow any unique prefix
3816 ** of the option name, or a numerical value. */
3817 n2 = strlen30(azArg[1]);
3818 for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
3819 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
3820 if( testctrl<0 ){
3821 testctrl = aCtrl[i].ctrlCode;
3822 }else{
3823 fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
@@ -4790,11 +4792,11 @@
4790 nHistory = strlen30(zHome) + 20;
4791 if( (zHistory = malloc(nHistory))!=0 ){
4792 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
4793 }
4794 }
4795 if( zHistory ) shell_read_history(zHistory);
4796 rc = process_input(&data, 0);
4797 if( zHistory ){
4798 shell_stifle_history(100);
4799 shell_write_history(zHistory);
4800 free(zHistory);
4801
--- src/shell.c
+++ src/shell.c
@@ -99,32 +99,30 @@
99
100
101 #if defined(_WIN32) || defined(WIN32)
102 # include <io.h>
103 # include <fcntl.h>
104 # define isatty(h) _isatty(h)
105 # ifndef access
106 # define access(f,m) _access((f),(m))
107 # endif
108 # undef popen
109 # define popen _popen
110 # undef pclose
111 # define pclose _pclose
112 #else
113 /* Make sure isatty() has a prototype. */
114 extern int isatty(int);
115
116 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
117 /* popen and pclose are not C89 functions and so are
118 ** sometimes omitted from the <stdio.h> header */
119 extern FILE *popen(const char*,const char*);
120 extern int pclose(FILE*);
121 # else
122 # define SQLITE_OMIT_POPEN 1
123 # endif
 
 
124 #endif
125
126 #if defined(_WIN32_WCE)
127 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
128 * thus we always assume that we have a console. That can be
@@ -1329,11 +1327,14 @@
1327 */
1328 static void display_scanstats(
1329 sqlite3 *db, /* Database to query */
1330 ShellState *pArg /* Pointer to ShellState */
1331 ){
1332 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
1333 UNUSED_PARAMETER(db);
1334 UNUSED_PARAMETER(pArg);
1335 #else
1336 int i, k, n, mx;
1337 fprintf(pArg->out, "-------- scanstats --------\n");
1338 mx = 0;
1339 for(k=0; k<=mx; k++){
1340 double rEstLoop = 1.0;
@@ -1864,10 +1865,11 @@
1865 const char *zName;
1866 FILE *in;
1867 long nIn;
1868 void *pBuf;
1869
1870 UNUSED_PARAMETER(argc);
1871 zName = (const char*)sqlite3_value_text(argv[0]);
1872 if( zName==0 ) return;
1873 in = fopen(zName, "rb");
1874 if( in==0 ) return;
1875 fseek(in, 0, SEEK_END);
@@ -1896,10 +1898,11 @@
1898 FILE *out;
1899 const char *z;
1900 sqlite3_int64 rc;
1901 const char *zFile;
1902
1903 UNUSED_PARAMETER(argc);
1904 zFile = (const char*)sqlite3_value_text(argv[0]);
1905 if( zFile==0 ) return;
1906 out = fopen(zFile, "wb");
1907 if( out==0 ) return;
1908 z = (const char*)sqlite3_value_blob(argv[1]);
@@ -2575,11 +2578,11 @@
2578 if( i==1 ) i = 65536;
2579 fprintf(p->out, "%-20s %d\n", "database page size:", i);
2580 fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
2581 fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
2582 fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
2583 for(i=0; i<ArraySize(aField); i++){
2584 int ofst = aField[i].ofst;
2585 unsigned int val = get4byteInt(aHdr + ofst);
2586 fprintf(p->out, "%-20s %u", aField[i].zName, val);
2587 switch( ofst ){
2588 case 56: {
@@ -2595,11 +2598,11 @@
2598 }else if( strcmp(zDb,"temp")==0 ){
2599 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
2600 }else{
2601 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
2602 }
2603 for(i=0; i<ArraySize(aQuery); i++){
2604 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
2605 int val = db_int(p, zSql);
2606 sqlite3_free(zSql);
2607 fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
2608 }
@@ -3226,11 +3229,11 @@
3229 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
3230 };
3231 int i, n2;
3232 open_db(p, 0);
3233 if( nArg==1 ){
3234 for(i=0; i<ArraySize(aLimit); i++){
3235 printf("%20s %d\n", aLimit[i].zLimitName,
3236 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
3237 }
3238 }else if( nArg>3 ){
3239 fprintf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
@@ -3237,11 +3240,11 @@
3240 rc = 1;
3241 goto meta_command_exit;
3242 }else{
3243 int iLimit = -1;
3244 n2 = strlen30(azArg[1]);
3245 for(i=0; i<ArraySize(aLimit); i++){
3246 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
3247 if( iLimit<0 ){
3248 iLimit = i;
3249 }else{
3250 fprintf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
@@ -3347,13 +3350,12 @@
3350 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
3351 sqlite3 *savedDb = p->db;
3352 const char *zSavedFilename = p->zDbFilename;
3353 char *zNewFilename = 0;
3354 p->db = 0;
3355 if( nArg>=2 ) zNewFilename = sqlite3_mprintf("%s", azArg[1]);
3356 p->zDbFilename = zNewFilename;
 
3357 open_db(p, 1);
3358 if( p->db!=0 ){
3359 sqlite3_close(savedDb);
3360 sqlite3_free(p->zFreeOnClose);
3361 p->zFreeOnClose = zNewFilename;
@@ -3813,11 +3815,11 @@
3815 open_db(p, 0);
3816
3817 /* convert testctrl text option to value. allow any unique prefix
3818 ** of the option name, or a numerical value. */
3819 n2 = strlen30(azArg[1]);
3820 for(i=0; i<ArraySize(aCtrl); i++){
3821 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
3822 if( testctrl<0 ){
3823 testctrl = aCtrl[i].ctrlCode;
3824 }else{
3825 fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
@@ -4790,11 +4792,11 @@
4792 nHistory = strlen30(zHome) + 20;
4793 if( (zHistory = malloc(nHistory))!=0 ){
4794 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
4795 }
4796 }
4797 if( zHistory ){ shell_read_history(zHistory); }
4798 rc = process_input(&data, 0);
4799 if( zHistory ){
4800 shell_stifle_history(100);
4801 shell_write_history(zHistory);
4802 free(zHistory);
4803
+4545 -3669
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -155,10 +155,17 @@
155155
# ifndef _FILE_OFFSET_BITS
156156
# define _FILE_OFFSET_BITS 64
157157
# endif
158158
# define _LARGEFILE_SOURCE 1
159159
#endif
160
+
161
+/* What version of GCC is being used. 0 means GCC is not being used */
162
+#ifdef __GNUC__
163
+# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
164
+#else
165
+# define GCC_VERSION 0
166
+#endif
160167
161168
/* Needed for various definitions... */
162169
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
163170
# define _GNU_SOURCE
164171
#endif
@@ -228,11 +235,11 @@
228235
** to experimental interfaces but reserve the right to make minor changes
229236
** if experience from use "in the wild" suggest such changes are prudent.
230237
**
231238
** The official C-language API documentation for SQLite is derived
232239
** from comments in this file. This file is the authoritative source
233
-** on how SQLite interfaces are suppose to operate.
240
+** on how SQLite interfaces are supposed to operate.
234241
**
235242
** The name of this file under configuration management is "sqlite.h.in".
236243
** The makefile makes some minor changes to this file (such as inserting
237244
** the version number) and changes its name to "sqlite3.h" as
238245
** part of the build process.
@@ -318,11 +325,11 @@
318325
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
319326
** [sqlite_version()] and [sqlite_source_id()].
320327
*/
321328
#define SQLITE_VERSION "3.8.11"
322329
#define SQLITE_VERSION_NUMBER 3008011
323
-#define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417"
330
+#define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899"
324331
325332
/*
326333
** CAPI3REF: Run-Time Library Version Numbers
327334
** KEYWORDS: sqlite3_version, sqlite3_sourceid
328335
**
@@ -1161,11 +1168,11 @@
11611168
** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
11621169
** opcode causes the xFileControl method to swap the file handle with the one
11631170
** pointed to by the pArg argument. This capability is used during testing
11641171
** and only needs to be supported when SQLITE_TEST is defined.
11651172
**
1166
-* <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1173
+** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
11671174
** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
11681175
** be advantageous to block on the next WAL lock if the lock is not immediately
11691176
** available. The WAL subsystem issues this signal during rare
11701177
** circumstances in order to fix a problem with priority inversion.
11711178
** Applications should <em>not</em> use this file-control.
@@ -9689,17 +9696,18 @@
96899696
u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */
96909697
u8 p5; /* Fifth parameter is an unsigned character */
96919698
int p1; /* First operand */
96929699
int p2; /* Second parameter (often the jump destination) */
96939700
int p3; /* The third parameter */
9694
- union { /* fourth parameter */
9701
+ union p4union { /* fourth parameter */
96959702
int i; /* Integer value if p4type==P4_INT32 */
96969703
void *p; /* Generic pointer */
96979704
char *z; /* Pointer to data for string (char array) types */
96989705
i64 *pI64; /* Used when p4type is P4_INT64 */
96999706
double *pReal; /* Used when p4type is P4_REAL */
97009707
FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
9708
+ sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
97019709
CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
97029710
Mem *pMem; /* Used when p4type is P4_MEM */
97039711
VTable *pVtab; /* Used when p4type is P4_VTAB */
97049712
KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
97059713
int *ai; /* Used when p4type is P4_INTARRAY */
@@ -9762,10 +9770,11 @@
97629770
#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
97639771
#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
97649772
#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
97659773
#define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
97669774
#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
9775
+#define P4_FUNCCTX (-20) /* P4 is a pointer to an sqlite3_context object */
97679776
97689777
/* Error message codes for OP_Halt */
97699778
#define P5_ConstraintNotNull 1
97709779
#define P5_ConstraintUnique 2
97719780
#define P5_ConstraintCheck 3
@@ -9804,46 +9813,46 @@
98049813
*/
98059814
/************** Include opcodes.h in the middle of vdbe.h ********************/
98069815
/************** Begin file opcodes.h *****************************************/
98079816
/* Automatically generated. Do not edit */
98089817
/* See the mkopcodeh.awk script for details */
9809
-#define OP_Function 1 /* synopsis: r[P3]=func(r[P2@P5]) */
9810
-#define OP_Savepoint 2
9811
-#define OP_AutoCommit 3
9812
-#define OP_Transaction 4
9813
-#define OP_SorterNext 5
9814
-#define OP_PrevIfOpen 6
9815
-#define OP_NextIfOpen 7
9816
-#define OP_Prev 8
9817
-#define OP_Next 9
9818
-#define OP_AggStep 10 /* synopsis: accum=r[P3] step(r[P2@P5]) */
9819
-#define OP_Checkpoint 11
9820
-#define OP_JournalMode 12
9821
-#define OP_Vacuum 13
9822
-#define OP_VFilter 14 /* synopsis: iplan=r[P3] zplan='P4' */
9823
-#define OP_VUpdate 15 /* synopsis: data=r[P3@P2] */
9824
-#define OP_Goto 16
9825
-#define OP_Gosub 17
9826
-#define OP_Return 18
9818
+#define OP_Savepoint 1
9819
+#define OP_AutoCommit 2
9820
+#define OP_Transaction 3
9821
+#define OP_SorterNext 4
9822
+#define OP_PrevIfOpen 5
9823
+#define OP_NextIfOpen 6
9824
+#define OP_Prev 7
9825
+#define OP_Next 8
9826
+#define OP_Checkpoint 9
9827
+#define OP_JournalMode 10
9828
+#define OP_Vacuum 11
9829
+#define OP_VFilter 12 /* synopsis: iplan=r[P3] zplan='P4' */
9830
+#define OP_VUpdate 13 /* synopsis: data=r[P3@P2] */
9831
+#define OP_Goto 14
9832
+#define OP_Gosub 15
9833
+#define OP_Return 16
9834
+#define OP_InitCoroutine 17
9835
+#define OP_EndCoroutine 18
98279836
#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
9828
-#define OP_InitCoroutine 20
9829
-#define OP_EndCoroutine 21
9830
-#define OP_Yield 22
9831
-#define OP_HaltIfNull 23 /* synopsis: if r[P3]=null halt */
9832
-#define OP_Halt 24
9833
-#define OP_Integer 25 /* synopsis: r[P2]=P1 */
9834
-#define OP_Int64 26 /* synopsis: r[P2]=P4 */
9835
-#define OP_String 27 /* synopsis: r[P2]='P4' (len=P1) */
9836
-#define OP_Null 28 /* synopsis: r[P2..P3]=NULL */
9837
-#define OP_SoftNull 29 /* synopsis: r[P1]=NULL */
9838
-#define OP_Blob 30 /* synopsis: r[P2]=P4 (len=P1) */
9839
-#define OP_Variable 31 /* synopsis: r[P2]=parameter(P1,P4) */
9840
-#define OP_Move 32 /* synopsis: r[P2@P3]=r[P1@P3] */
9841
-#define OP_Copy 33 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
9842
-#define OP_SCopy 34 /* synopsis: r[P2]=r[P1] */
9843
-#define OP_ResultRow 35 /* synopsis: output=r[P1@P2] */
9844
-#define OP_CollSeq 36
9837
+#define OP_Yield 20
9838
+#define OP_HaltIfNull 21 /* synopsis: if r[P3]=null halt */
9839
+#define OP_Halt 22
9840
+#define OP_Integer 23 /* synopsis: r[P2]=P1 */
9841
+#define OP_Int64 24 /* synopsis: r[P2]=P4 */
9842
+#define OP_String 25 /* synopsis: r[P2]='P4' (len=P1) */
9843
+#define OP_Null 26 /* synopsis: r[P2..P3]=NULL */
9844
+#define OP_SoftNull 27 /* synopsis: r[P1]=NULL */
9845
+#define OP_Blob 28 /* synopsis: r[P2]=P4 (len=P1) */
9846
+#define OP_Variable 29 /* synopsis: r[P2]=parameter(P1,P4) */
9847
+#define OP_Move 30 /* synopsis: r[P2@P3]=r[P1@P3] */
9848
+#define OP_Copy 31 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
9849
+#define OP_SCopy 32 /* synopsis: r[P2]=r[P1] */
9850
+#define OP_ResultRow 33 /* synopsis: output=r[P1@P2] */
9851
+#define OP_CollSeq 34
9852
+#define OP_Function0 35 /* synopsis: r[P3]=func(r[P2@P5]) */
9853
+#define OP_Function 36 /* synopsis: r[P3]=func(r[P2@P5]) */
98459854
#define OP_AddImm 37 /* synopsis: r[P1]=r[P1]+P2 */
98469855
#define OP_MustBeInt 38
98479856
#define OP_RealAffinity 39
98489857
#define OP_Cast 40 /* synopsis: affinity(r[P1]) */
98499858
#define OP_Permutation 41
@@ -9865,33 +9874,33 @@
98659874
#define OP_OpenEphemeral 57 /* synopsis: nColumn=P2 */
98669875
#define OP_SorterOpen 58
98679876
#define OP_SequenceTest 59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
98689877
#define OP_OpenPseudo 60 /* synopsis: P3 columns in r[P2] */
98699878
#define OP_Close 61
9870
-#define OP_SeekLT 62 /* synopsis: key=r[P3@P4] */
9871
-#define OP_SeekLE 63 /* synopsis: key=r[P3@P4] */
9872
-#define OP_SeekGE 64 /* synopsis: key=r[P3@P4] */
9873
-#define OP_SeekGT 65 /* synopsis: key=r[P3@P4] */
9874
-#define OP_Seek 66 /* synopsis: intkey=r[P2] */
9875
-#define OP_NoConflict 67 /* synopsis: key=r[P3@P4] */
9876
-#define OP_NotFound 68 /* synopsis: key=r[P3@P4] */
9877
-#define OP_Found 69 /* synopsis: key=r[P3@P4] */
9878
-#define OP_NotExists 70 /* synopsis: intkey=r[P3] */
9879
+#define OP_ColumnsUsed 62
9880
+#define OP_SeekLT 63 /* synopsis: key=r[P3@P4] */
9881
+#define OP_SeekLE 64 /* synopsis: key=r[P3@P4] */
9882
+#define OP_SeekGE 65 /* synopsis: key=r[P3@P4] */
9883
+#define OP_SeekGT 66 /* synopsis: key=r[P3@P4] */
9884
+#define OP_Seek 67 /* synopsis: intkey=r[P2] */
9885
+#define OP_NoConflict 68 /* synopsis: key=r[P3@P4] */
9886
+#define OP_NotFound 69 /* synopsis: key=r[P3@P4] */
9887
+#define OP_Found 70 /* synopsis: key=r[P3@P4] */
98799888
#define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
98809889
#define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9881
-#define OP_Sequence 73 /* synopsis: r[P2]=cursor[P1].ctr++ */
9882
-#define OP_NewRowid 74 /* synopsis: r[P2]=rowid */
9883
-#define OP_Insert 75 /* synopsis: intkey=r[P3] data=r[P2] */
9890
+#define OP_NotExists 73 /* synopsis: intkey=r[P3] */
9891
+#define OP_Sequence 74 /* synopsis: r[P2]=cursor[P1].ctr++ */
9892
+#define OP_NewRowid 75 /* synopsis: r[P2]=rowid */
98849893
#define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
98859894
#define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
98869895
#define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
98879896
#define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
98889897
#define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
98899898
#define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
98909899
#define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
98919900
#define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9892
-#define OP_InsertInt 84 /* synopsis: intkey=P3 data=r[P2] */
9901
+#define OP_Insert 84 /* synopsis: intkey=r[P3] data=r[P2] */
98939902
#define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
98949903
#define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
98959904
#define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
98969905
#define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
98979906
#define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -9898,73 +9907,76 @@
98989907
#define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
98999908
#define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
99009909
#define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
99019910
#define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
99029911
#define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9903
-#define OP_Delete 95
9912
+#define OP_InsertInt 95 /* synopsis: intkey=P3 data=r[P2] */
99049913
#define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
99059914
#define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
9906
-#define OP_ResetCount 98
9907
-#define OP_SorterCompare 99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
9908
-#define OP_SorterData 100 /* synopsis: r[P2]=data */
9909
-#define OP_RowKey 101 /* synopsis: r[P2]=key */
9910
-#define OP_RowData 102 /* synopsis: r[P2]=data */
9911
-#define OP_Rowid 103 /* synopsis: r[P2]=rowid */
9912
-#define OP_NullRow 104
9913
-#define OP_Last 105
9914
-#define OP_SorterSort 106
9915
-#define OP_Sort 107
9916
-#define OP_Rewind 108
9917
-#define OP_SorterInsert 109
9918
-#define OP_IdxInsert 110 /* synopsis: key=r[P2] */
9919
-#define OP_IdxDelete 111 /* synopsis: key=r[P2@P3] */
9920
-#define OP_IdxRowid 112 /* synopsis: r[P2]=rowid */
9921
-#define OP_IdxLE 113 /* synopsis: key=r[P3@P4] */
9922
-#define OP_IdxGT 114 /* synopsis: key=r[P3@P4] */
9923
-#define OP_IdxLT 115 /* synopsis: key=r[P3@P4] */
9924
-#define OP_IdxGE 116 /* synopsis: key=r[P3@P4] */
9925
-#define OP_Destroy 117
9926
-#define OP_Clear 118
9927
-#define OP_ResetSorter 119
9928
-#define OP_CreateIndex 120 /* synopsis: r[P2]=root iDb=P1 */
9929
-#define OP_CreateTable 121 /* synopsis: r[P2]=root iDb=P1 */
9930
-#define OP_ParseSchema 122
9931
-#define OP_LoadAnalysis 123
9932
-#define OP_DropTable 124
9933
-#define OP_DropIndex 125
9934
-#define OP_DropTrigger 126
9935
-#define OP_IntegrityCk 127
9936
-#define OP_RowSetAdd 128 /* synopsis: rowset(P1)=r[P2] */
9937
-#define OP_RowSetRead 129 /* synopsis: r[P3]=rowset(P1) */
9938
-#define OP_RowSetTest 130 /* synopsis: if r[P3] in rowset(P1) goto P2 */
9939
-#define OP_Program 131
9940
-#define OP_Param 132
9915
+#define OP_Delete 98
9916
+#define OP_ResetCount 99
9917
+#define OP_SorterCompare 100 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
9918
+#define OP_SorterData 101 /* synopsis: r[P2]=data */
9919
+#define OP_RowKey 102 /* synopsis: r[P2]=key */
9920
+#define OP_RowData 103 /* synopsis: r[P2]=data */
9921
+#define OP_Rowid 104 /* synopsis: r[P2]=rowid */
9922
+#define OP_NullRow 105
9923
+#define OP_Last 106
9924
+#define OP_SorterSort 107
9925
+#define OP_Sort 108
9926
+#define OP_Rewind 109
9927
+#define OP_SorterInsert 110
9928
+#define OP_IdxInsert 111 /* synopsis: key=r[P2] */
9929
+#define OP_IdxDelete 112 /* synopsis: key=r[P2@P3] */
9930
+#define OP_IdxRowid 113 /* synopsis: r[P2]=rowid */
9931
+#define OP_IdxLE 114 /* synopsis: key=r[P3@P4] */
9932
+#define OP_IdxGT 115 /* synopsis: key=r[P3@P4] */
9933
+#define OP_IdxLT 116 /* synopsis: key=r[P3@P4] */
9934
+#define OP_IdxGE 117 /* synopsis: key=r[P3@P4] */
9935
+#define OP_Destroy 118
9936
+#define OP_Clear 119
9937
+#define OP_ResetSorter 120
9938
+#define OP_CreateIndex 121 /* synopsis: r[P2]=root iDb=P1 */
9939
+#define OP_CreateTable 122 /* synopsis: r[P2]=root iDb=P1 */
9940
+#define OP_ParseSchema 123
9941
+#define OP_LoadAnalysis 124
9942
+#define OP_DropTable 125
9943
+#define OP_DropIndex 126
9944
+#define OP_DropTrigger 127
9945
+#define OP_IntegrityCk 128
9946
+#define OP_RowSetAdd 129 /* synopsis: rowset(P1)=r[P2] */
9947
+#define OP_RowSetRead 130 /* synopsis: r[P3]=rowset(P1) */
9948
+#define OP_RowSetTest 131 /* synopsis: if r[P3] in rowset(P1) goto P2 */
9949
+#define OP_Program 132
99419950
#define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
9942
-#define OP_FkCounter 134 /* synopsis: fkctr[P1]+=P2 */
9943
-#define OP_FkIfZero 135 /* synopsis: if fkctr[P1]==0 goto P2 */
9944
-#define OP_MemMax 136 /* synopsis: r[P1]=max(r[P1],r[P2]) */
9945
-#define OP_IfPos 137 /* synopsis: if r[P1]>0 goto P2 */
9946
-#define OP_IfNeg 138 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */
9947
-#define OP_IfNotZero 139 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */
9948
-#define OP_DecrJumpZero 140 /* synopsis: if (--r[P1])==0 goto P2 */
9949
-#define OP_JumpZeroIncr 141 /* synopsis: if (r[P1]++)==0 ) goto P2 */
9950
-#define OP_AggFinal 142 /* synopsis: accum=r[P1] N=P2 */
9951
-#define OP_IncrVacuum 143
9952
-#define OP_Expire 144
9953
-#define OP_TableLock 145 /* synopsis: iDb=P1 root=P2 write=P3 */
9954
-#define OP_VBegin 146
9955
-#define OP_VCreate 147
9956
-#define OP_VDestroy 148
9957
-#define OP_VOpen 149
9958
-#define OP_VColumn 150 /* synopsis: r[P3]=vcolumn(P2) */
9959
-#define OP_VNext 151
9960
-#define OP_VRename 152
9961
-#define OP_Pagecount 153
9962
-#define OP_MaxPgcnt 154
9963
-#define OP_Init 155 /* synopsis: Start at P2 */
9964
-#define OP_Noop 156
9965
-#define OP_Explain 157
9951
+#define OP_Param 134
9952
+#define OP_FkCounter 135 /* synopsis: fkctr[P1]+=P2 */
9953
+#define OP_FkIfZero 136 /* synopsis: if fkctr[P1]==0 goto P2 */
9954
+#define OP_MemMax 137 /* synopsis: r[P1]=max(r[P1],r[P2]) */
9955
+#define OP_IfPos 138 /* synopsis: if r[P1]>0 goto P2 */
9956
+#define OP_IfNeg 139 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */
9957
+#define OP_IfNotZero 140 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */
9958
+#define OP_DecrJumpZero 141 /* synopsis: if (--r[P1])==0 goto P2 */
9959
+#define OP_JumpZeroIncr 142 /* synopsis: if (r[P1]++)==0 ) goto P2 */
9960
+#define OP_AggStep0 143 /* synopsis: accum=r[P3] step(r[P2@P5]) */
9961
+#define OP_AggStep 144 /* synopsis: accum=r[P3] step(r[P2@P5]) */
9962
+#define OP_AggFinal 145 /* synopsis: accum=r[P1] N=P2 */
9963
+#define OP_IncrVacuum 146
9964
+#define OP_Expire 147
9965
+#define OP_TableLock 148 /* synopsis: iDb=P1 root=P2 write=P3 */
9966
+#define OP_VBegin 149
9967
+#define OP_VCreate 150
9968
+#define OP_VDestroy 151
9969
+#define OP_VOpen 152
9970
+#define OP_VColumn 153 /* synopsis: r[P3]=vcolumn(P2) */
9971
+#define OP_VNext 154
9972
+#define OP_VRename 155
9973
+#define OP_Pagecount 156
9974
+#define OP_MaxPgcnt 157
9975
+#define OP_Init 158 /* synopsis: Start at P2 */
9976
+#define OP_Noop 159
9977
+#define OP_Explain 160
99669978
99679979
99689980
/* Properties such as "out2" or "jump" that are specified in
99699981
** comments following the "case" for each opcode in the vdbe.c
99709982
** are encoded into bitvectors as follows:
@@ -9974,30 +9986,31 @@
99749986
#define OPFLG_IN2 0x0004 /* in2: P2 is an input */
99759987
#define OPFLG_IN3 0x0008 /* in3: P3 is an input */
99769988
#define OPFLG_OUT2 0x0010 /* out2: P2 is an output */
99779989
#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */
99789990
#define OPFLG_INITIALIZER {\
9979
-/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
9980
-/* 8 */ 0x01, 0x01, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
9981
-/* 16 */ 0x01, 0x01, 0x02, 0x12, 0x01, 0x02, 0x03, 0x08,\
9982
-/* 24 */ 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10,\
9983
-/* 32 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x03, 0x02,\
9991
+/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,\
9992
+/* 8 */ 0x01, 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01,\
9993
+/* 16 */ 0x02, 0x01, 0x02, 0x12, 0x03, 0x08, 0x00, 0x10,\
9994
+/* 24 */ 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00,\
9995
+/* 32 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02,\
99849996
/* 40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
99859997
/* 48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\
9986
-/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09,\
9987
-/* 64 */ 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x26,\
9988
-/* 72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
9998
+/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\
9999
+/* 64 */ 0x09, 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x26,\
10000
+/* 72 */ 0x26, 0x09, 0x10, 0x10, 0x03, 0x03, 0x0b, 0x0b,\
998910001
/* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
999010002
/* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
9991
-/* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
9992
-/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
9993
-/* 112 */ 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00, 0x00,\
9994
-/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
9995
-/* 128 */ 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00, 0x01,\
9996
-/* 136 */ 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x01,\
9997
-/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\
9998
-/* 152 */ 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,}
10003
+/* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
10004
+/* 104 */ 0x10, 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04,\
10005
+/* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\
10006
+/* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
10007
+/* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\
10008
+/* 136 */ 0x01, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00,\
10009
+/* 144 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
10010
+/* 152 */ 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01, 0x00,\
10011
+/* 160 */ 0x00,}
999910012
1000010013
/************** End of opcodes.h *********************************************/
1000110014
/************** Continuing where we left off in vdbe.h ***********************/
1000210015
1000310016
/*
@@ -10008,10 +10021,11 @@
1000810021
SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
1000910022
SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
1001010023
SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
1001110024
SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
1001210025
SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
10026
+SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
1001310027
SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
1001410028
SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
1001510029
SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
1001610030
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
1001710031
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
@@ -10400,18 +10414,18 @@
1040010414
PgHdr *pDirtyNext; /* Next element in list of dirty pages */
1040110415
PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
1040210416
};
1040310417
1040410418
/* Bit values for PgHdr.flags */
10405
-#define PGHDR_DIRTY 0x002 /* Page has changed */
10406
-#define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
10407
- ** writing this page to the database */
10408
-#define PGHDR_NEED_READ 0x008 /* Content is unread */
10409
-#define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
10410
-#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
10411
-
10412
-#define PGHDR_MMAP 0x040 /* This is an mmap page object */
10419
+#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
10420
+#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
10421
+#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
10422
+#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
10423
+ ** writing this page to the database */
10424
+#define PGHDR_NEED_READ 0x010 /* Content is unread */
10425
+#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
10426
+#define PGHDR_MMAP 0x040 /* This is an mmap page object */
1041310427
1041410428
/* Initialize and shutdown the page cache subsystem */
1041510429
SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
1041610430
SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
1041710431
@@ -11439,13 +11453,13 @@
1143911453
** But rather than start with 0 or 1, we begin with 'A'. That way,
1144011454
** when multiple affinity types are concatenated into a string and
1144111455
** used as the P4 operand, they will be more readable.
1144211456
**
1144311457
** Note also that the numeric types are grouped together so that testing
11444
-** for a numeric type is a single comparison. And the NONE type is first.
11458
+** for a numeric type is a single comparison. And the BLOB type is first.
1144511459
*/
11446
-#define SQLITE_AFF_NONE 'A'
11460
+#define SQLITE_AFF_BLOB 'A'
1144711461
#define SQLITE_AFF_TEXT 'B'
1144811462
#define SQLITE_AFF_NUMERIC 'C'
1144911463
#define SQLITE_AFF_INTEGER 'D'
1145011464
#define SQLITE_AFF_REAL 'E'
1145111465
@@ -12198,11 +12212,11 @@
1219812212
#endif
1219912213
int iCursor; /* The VDBE cursor number used to access this table */
1220012214
Expr *pOn; /* The ON clause of a join */
1220112215
IdList *pUsing; /* The USING clause of a join */
1220212216
Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
12203
- char *zIndex; /* Identifier from "INDEXED BY <zIndex>" clause */
12217
+ char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
1220412218
Index *pIndex; /* Index structure corresponding to zIndex, if any */
1220512219
} a[1]; /* One entry for each identifier on the list */
1220612220
};
1220712221
1220812222
/*
@@ -13004,11 +13018,13 @@
1300413018
# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
1300513019
# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
1300613020
# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
1300713021
# define sqlite3Tolower(x) tolower((unsigned char)(x))
1300813022
#endif
13023
+#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1300913024
SQLITE_PRIVATE int sqlite3IsIdChar(u8);
13025
+#endif
1301013026
1301113027
/*
1301213028
** Internal function prototypes
1301313029
*/
1301413030
#define sqlite3StrICmp sqlite3_stricmp
@@ -13032,11 +13048,13 @@
1303213048
SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
1303313049
SQLITE_PRIVATE void sqlite3ScratchFree(void*);
1303413050
SQLITE_PRIVATE void *sqlite3PageMalloc(int);
1303513051
SQLITE_PRIVATE void sqlite3PageFree(void*);
1303613052
SQLITE_PRIVATE void sqlite3MemSetDefault(void);
13053
+#ifndef SQLITE_OMIT_BUILTIN_TEST
1303713054
SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
13055
+#endif
1303813056
SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
1303913057
1304013058
/*
1304113059
** On systems with ample stack space and that support alloca(), make
1304213060
** use of alloca() to obtain space for large automatic objects. By default,
@@ -13108,14 +13126,10 @@
1310813126
#if defined(SQLITE_TEST)
1310913127
SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
1311013128
#endif
1311113129
1311213130
#if defined(SQLITE_DEBUG)
13113
-SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView*,u8);
13114
-SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView*);
13115
-SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView*, const char*, ...);
13116
-SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView*, const char*, u8);
1311713131
SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
1311813132
SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
1311913133
SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
1312013134
#endif
1312113135
@@ -13176,15 +13190,18 @@
1317613190
SQLITE_PRIVATE int sqlite3FaultSim(int);
1317713191
#endif
1317813192
1317913193
SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
1318013194
SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
13195
+SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
1318113196
SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
1318213197
SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
1318313198
SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
1318413199
SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
13200
+#ifndef SQLITE_OMIT_BUILTIN_TEST
1318513201
SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
13202
+#endif
1318613203
1318713204
SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
1318813205
SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
1318913206
SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
1319013207
SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
@@ -13268,10 +13285,11 @@
1326813285
SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
1326913286
#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
1327013287
#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
1327113288
SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
1327213289
SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
13290
+SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
1327313291
SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
1327413292
SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
1327513293
SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
1327613294
SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
1327713295
SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
@@ -13284,12 +13302,14 @@
1328413302
SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
1328513303
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
1328613304
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
1328713305
SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
1328813306
SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
13307
+#ifndef SQLITE_OMIT_BUILTIN_TEST
1328913308
SQLITE_PRIVATE void sqlite3PrngSaveState(void);
1329013309
SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
13310
+#endif
1329113311
SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
1329213312
SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
1329313313
SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
1329413314
SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
1329513315
SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
@@ -13503,10 +13523,11 @@
1350313523
SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
1350413524
SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
1350513525
SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
1350613526
SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
1350713527
SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
13528
+SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
1350813529
SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
1350913530
SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
1351013531
SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
1351113532
SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
1351213533
SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
@@ -14620,10 +14641,13 @@
1462014641
Pgno pgnoRoot; /* Root page of the open btree cursor */
1462114642
sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
1462214643
i64 seqCount; /* Sequence counter */
1462314644
i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
1462414645
VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
14646
+#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
14647
+ u64 maskUsed; /* Mask of columns used by this cursor */
14648
+#endif
1462514649
1462614650
/* Cached information about the header for the data record that the
1462714651
** cursor is currently pointing to. Only valid if cacheStatus matches
1462814652
** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
1462914653
** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
@@ -14813,18 +14837,20 @@
1481314837
**
1481414838
** This structure is defined inside of vdbeInt.h because it uses substructures
1481514839
** (Mem) which are only defined there.
1481614840
*/
1481714841
struct sqlite3_context {
14818
- Mem *pOut; /* The return value is stored here */
14819
- FuncDef *pFunc; /* Pointer to function information */
14820
- Mem *pMem; /* Memory cell used to store aggregate context */
14821
- Vdbe *pVdbe; /* The VM that owns this context */
14822
- int iOp; /* Instruction number of OP_Function */
14823
- int isError; /* Error code returned by the function. */
14824
- u8 skipFlag; /* Skip accumulator loading if true */
14825
- u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
14842
+ Mem *pOut; /* The return value is stored here */
14843
+ FuncDef *pFunc; /* Pointer to function information */
14844
+ Mem *pMem; /* Memory cell used to store aggregate context */
14845
+ Vdbe *pVdbe; /* The VM that owns this context */
14846
+ int iOp; /* Instruction number of OP_Function */
14847
+ int isError; /* Error code returned by the function. */
14848
+ u8 skipFlag; /* Skip accumulator loading if true */
14849
+ u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
14850
+ u8 argc; /* Number of arguments */
14851
+ sqlite3_value *argv[1]; /* Argument set */
1482614852
};
1482714853
1482814854
/*
1482914855
** An Explain object accumulates indented output which is helpful
1483014856
** in describing recursive data structures.
@@ -19193,13 +19219,18 @@
1919319219
if( sqlite3GlobalConfig.bCoreMutex ){
1919419220
pFrom = sqlite3DefaultMutex();
1919519221
}else{
1919619222
pFrom = sqlite3NoopMutex();
1919719223
}
19198
- memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
19199
- memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
19200
- sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
19224
+ pTo->xMutexInit = pFrom->xMutexInit;
19225
+ pTo->xMutexEnd = pFrom->xMutexEnd;
19226
+ pTo->xMutexFree = pFrom->xMutexFree;
19227
+ pTo->xMutexEnter = pFrom->xMutexEnter;
19228
+ pTo->xMutexTry = pFrom->xMutexTry;
19229
+ pTo->xMutexLeave = pFrom->xMutexLeave;
19230
+ pTo->xMutexHeld = pFrom->xMutexHeld;
19231
+ pTo->xMutexNotheld = pFrom->xMutexNotheld;
1920119232
pTo->xMutexAlloc = pFrom->xMutexAlloc;
1920219233
}
1920319234
rc = sqlite3GlobalConfig.mutex.xMutexInit();
1920419235
1920519236
#ifdef SQLITE_DEBUG
@@ -21353,21 +21384,20 @@
2135321384
**
2135421385
** The returned value is normally a copy of the second argument to this
2135521386
** function. However, if a malloc() failure has occurred since the previous
2135621387
** invocation SQLITE_NOMEM is returned instead.
2135721388
**
21358
-** If the first argument, db, is not NULL and a malloc() error has occurred,
21359
-** then the connection error-code (the value returned by sqlite3_errcode())
21360
-** is set to SQLITE_NOMEM.
21389
+** If an OOM as occurred, then the connection error-code (the value
21390
+** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
2136121391
*/
2136221392
SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
21363
- /* If the db handle is not NULL, then we must hold the connection handle
21364
- ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
21393
+ /* If the db handle must hold the connection handle mutex here.
21394
+ ** Otherwise the read (and possible write) of db->mallocFailed
2136521395
** is unsafe, as is the call to sqlite3Error().
2136621396
*/
21367
- assert( !db || sqlite3_mutex_held(db->mutex) );
21368
- if( db==0 ) return rc & 0xff;
21397
+ assert( db!=0 );
21398
+ assert( sqlite3_mutex_held(db->mutex) );
2136921399
if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
2137021400
return apiOomError(db);
2137121401
}
2137221402
return rc & db->errMask;
2137321403
}
@@ -21374,21 +21404,18 @@
2137421404
2137521405
/************** End of malloc.c **********************************************/
2137621406
/************** Begin file printf.c ******************************************/
2137721407
/*
2137821408
** The "printf" code that follows dates from the 1980's. It is in
21379
-** the public domain. The original comments are included here for
21380
-** completeness. They are very out-of-date but might be useful as
21381
-** an historical reference. Most of the "enhancements" have been backed
21382
-** out so that the functionality is now the same as standard printf().
21409
+** the public domain.
2138321410
**
2138421411
**************************************************************************
2138521412
**
2138621413
** This file contains code for a set of "printf"-like routines. These
2138721414
** routines format strings much like the printf() from the standard C
2138821415
** library, though the implementation here has enhancements to support
21389
-** SQLlite.
21416
+** SQLite.
2139021417
*/
2139121418
2139221419
/*
2139321420
** Conversion types fall into various categories as defined by the
2139421421
** following enumeration.
@@ -22431,26 +22458,49 @@
2243122458
fprintf(stdout,"%s", zBuf);
2243222459
fflush(stdout);
2243322460
}
2243422461
#endif
2243522462
22463
+
22464
+/*
22465
+** variable-argument wrapper around sqlite3VXPrintf().
22466
+*/
22467
+SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
22468
+ va_list ap;
22469
+ va_start(ap,zFormat);
22470
+ sqlite3VXPrintf(p, bFlags, zFormat, ap);
22471
+ va_end(ap);
22472
+}
22473
+
22474
+/************** End of printf.c **********************************************/
22475
+/************** Begin file treeview.c ****************************************/
22476
+/*
22477
+** 2015-06-08
22478
+**
22479
+** The author disclaims copyright to this source code. In place of
22480
+** a legal notice, here is a blessing:
22481
+**
22482
+** May you do good and not evil.
22483
+** May you find forgiveness for yourself and forgive others.
22484
+** May you share freely, never taking more than you give.
22485
+**
22486
+*************************************************************************
22487
+**
22488
+** This file contains C code to implement the TreeView debugging routines.
22489
+** These routines print a parse tree to standard output for debugging and
22490
+** analysis.
22491
+**
22492
+** The interfaces in this file is only available when compiling
22493
+** with SQLITE_DEBUG.
22494
+*/
2243622495
#ifdef SQLITE_DEBUG
22437
-/*************************************************************************
22438
-** Routines for implementing the "TreeView" display of hierarchical
22439
-** data structures for debugging.
22440
-**
22441
-** The main entry points (coded elsewhere) are:
22442
-** sqlite3TreeViewExpr(0, pExpr, 0);
22443
-** sqlite3TreeViewExprList(0, pList, 0, 0);
22444
-** sqlite3TreeViewSelect(0, pSelect, 0);
22445
-** Insert calls to those routines while debugging in order to display
22446
-** a diagram of Expr, ExprList, and Select objects.
22447
-**
22496
+
22497
+/*
22498
+** Add a new subitem to the tree. The moreToFollow flag indicates that this
22499
+** is not the last item in the tree.
2244822500
*/
22449
-/* Add a new subitem to the tree. The moreToFollow flag indicates that this
22450
-** is not the last item in the tree. */
22451
-SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
22501
+static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
2245222502
if( p==0 ){
2245322503
p = sqlite3_malloc64( sizeof(*p) );
2245422504
if( p==0 ) return 0;
2245522505
memset(p, 0, sizeof(*p));
2245622506
}else{
@@ -22458,19 +22508,25 @@
2245822508
}
2245922509
assert( moreToFollow==0 || moreToFollow==1 );
2246022510
if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
2246122511
return p;
2246222512
}
22463
-/* Finished with one layer of the tree */
22464
-SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView *p){
22513
+
22514
+/*
22515
+** Finished with one layer of the tree
22516
+*/
22517
+static void sqlite3TreeViewPop(TreeView *p){
2246522518
if( p==0 ) return;
2246622519
p->iLevel--;
2246722520
if( p->iLevel<0 ) sqlite3_free(p);
2246822521
}
22469
-/* Generate a single line of output for the tree, with a prefix that contains
22470
-** all the appropriate tree lines */
22471
-SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
22522
+
22523
+/*
22524
+** Generate a single line of output for the tree, with a prefix that contains
22525
+** all the appropriate tree lines
22526
+*/
22527
+static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
2247222528
va_list ap;
2247322529
int i;
2247422530
StrAccum acc;
2247522531
char zBuf[500];
2247622532
sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
@@ -22486,28 +22542,371 @@
2248622542
if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
2248722543
sqlite3StrAccumFinish(&acc);
2248822544
fprintf(stdout,"%s", zBuf);
2248922545
fflush(stdout);
2249022546
}
22491
-/* Shorthand for starting a new tree item that consists of a single label */
22492
-SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
22493
- p = sqlite3TreeViewPush(p, moreToFollow);
22547
+
22548
+/*
22549
+** Shorthand for starting a new tree item that consists of a single label
22550
+*/
22551
+static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
22552
+ p = sqlite3TreeViewPush(p, moreFollows);
2249422553
sqlite3TreeViewLine(p, "%s", zLabel);
2249522554
}
22555
+
22556
+
22557
+/*
22558
+** Generate a human-readable description of a the Select object.
22559
+*/
22560
+SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
22561
+ int n = 0;
22562
+ pView = sqlite3TreeViewPush(pView, moreToFollow);
22563
+ sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
22564
+ ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
22565
+ ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
22566
+ );
22567
+ if( p->pSrc && p->pSrc->nSrc ) n++;
22568
+ if( p->pWhere ) n++;
22569
+ if( p->pGroupBy ) n++;
22570
+ if( p->pHaving ) n++;
22571
+ if( p->pOrderBy ) n++;
22572
+ if( p->pLimit ) n++;
22573
+ if( p->pOffset ) n++;
22574
+ if( p->pPrior ) n++;
22575
+ sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
22576
+ if( p->pSrc && p->pSrc->nSrc ){
22577
+ int i;
22578
+ pView = sqlite3TreeViewPush(pView, (n--)>0);
22579
+ sqlite3TreeViewLine(pView, "FROM");
22580
+ for(i=0; i<p->pSrc->nSrc; i++){
22581
+ struct SrcList_item *pItem = &p->pSrc->a[i];
22582
+ StrAccum x;
22583
+ char zLine[100];
22584
+ sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
22585
+ sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
22586
+ if( pItem->zDatabase ){
22587
+ sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
22588
+ }else if( pItem->zName ){
22589
+ sqlite3XPrintf(&x, 0, " %s", pItem->zName);
22590
+ }
22591
+ if( pItem->pTab ){
22592
+ sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
22593
+ }
22594
+ if( pItem->zAlias ){
22595
+ sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
22596
+ }
22597
+ if( pItem->jointype & JT_LEFT ){
22598
+ sqlite3XPrintf(&x, 0, " LEFT-JOIN");
22599
+ }
22600
+ sqlite3StrAccumFinish(&x);
22601
+ sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
22602
+ if( pItem->pSelect ){
22603
+ sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
22604
+ }
22605
+ sqlite3TreeViewPop(pView);
22606
+ }
22607
+ sqlite3TreeViewPop(pView);
22608
+ }
22609
+ if( p->pWhere ){
22610
+ sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
22611
+ sqlite3TreeViewExpr(pView, p->pWhere, 0);
22612
+ sqlite3TreeViewPop(pView);
22613
+ }
22614
+ if( p->pGroupBy ){
22615
+ sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
22616
+ }
22617
+ if( p->pHaving ){
22618
+ sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
22619
+ sqlite3TreeViewExpr(pView, p->pHaving, 0);
22620
+ sqlite3TreeViewPop(pView);
22621
+ }
22622
+ if( p->pOrderBy ){
22623
+ sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
22624
+ }
22625
+ if( p->pLimit ){
22626
+ sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
22627
+ sqlite3TreeViewExpr(pView, p->pLimit, 0);
22628
+ sqlite3TreeViewPop(pView);
22629
+ }
22630
+ if( p->pOffset ){
22631
+ sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
22632
+ sqlite3TreeViewExpr(pView, p->pOffset, 0);
22633
+ sqlite3TreeViewPop(pView);
22634
+ }
22635
+ if( p->pPrior ){
22636
+ const char *zOp = "UNION";
22637
+ switch( p->op ){
22638
+ case TK_ALL: zOp = "UNION ALL"; break;
22639
+ case TK_INTERSECT: zOp = "INTERSECT"; break;
22640
+ case TK_EXCEPT: zOp = "EXCEPT"; break;
22641
+ }
22642
+ sqlite3TreeViewItem(pView, zOp, (n--)>0);
22643
+ sqlite3TreeViewSelect(pView, p->pPrior, 0);
22644
+ sqlite3TreeViewPop(pView);
22645
+ }
22646
+ sqlite3TreeViewPop(pView);
22647
+}
22648
+
22649
+/*
22650
+** Generate a human-readable explanation of an expression tree.
22651
+*/
22652
+SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
22653
+ const char *zBinOp = 0; /* Binary operator */
22654
+ const char *zUniOp = 0; /* Unary operator */
22655
+ char zFlgs[30];
22656
+ pView = sqlite3TreeViewPush(pView, moreToFollow);
22657
+ if( pExpr==0 ){
22658
+ sqlite3TreeViewLine(pView, "nil");
22659
+ sqlite3TreeViewPop(pView);
22660
+ return;
22661
+ }
22662
+ if( pExpr->flags ){
22663
+ sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
22664
+ }else{
22665
+ zFlgs[0] = 0;
22666
+ }
22667
+ switch( pExpr->op ){
22668
+ case TK_AGG_COLUMN: {
22669
+ sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
22670
+ pExpr->iTable, pExpr->iColumn, zFlgs);
22671
+ break;
22672
+ }
22673
+ case TK_COLUMN: {
22674
+ if( pExpr->iTable<0 ){
22675
+ /* This only happens when coding check constraints */
22676
+ sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
22677
+ }else{
22678
+ sqlite3TreeViewLine(pView, "{%d:%d}%s",
22679
+ pExpr->iTable, pExpr->iColumn, zFlgs);
22680
+ }
22681
+ break;
22682
+ }
22683
+ case TK_INTEGER: {
22684
+ if( pExpr->flags & EP_IntValue ){
22685
+ sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
22686
+ }else{
22687
+ sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
22688
+ }
22689
+ break;
22690
+ }
22691
+#ifndef SQLITE_OMIT_FLOATING_POINT
22692
+ case TK_FLOAT: {
22693
+ sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
22694
+ break;
22695
+ }
22696
+#endif
22697
+ case TK_STRING: {
22698
+ sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
22699
+ break;
22700
+ }
22701
+ case TK_NULL: {
22702
+ sqlite3TreeViewLine(pView,"NULL");
22703
+ break;
22704
+ }
22705
+#ifndef SQLITE_OMIT_BLOB_LITERAL
22706
+ case TK_BLOB: {
22707
+ sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
22708
+ break;
22709
+ }
22710
+#endif
22711
+ case TK_VARIABLE: {
22712
+ sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
22713
+ pExpr->u.zToken, pExpr->iColumn);
22714
+ break;
22715
+ }
22716
+ case TK_REGISTER: {
22717
+ sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
22718
+ break;
22719
+ }
22720
+ case TK_AS: {
22721
+ sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
22722
+ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22723
+ break;
22724
+ }
22725
+ case TK_ID: {
22726
+ sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
22727
+ break;
22728
+ }
22729
+#ifndef SQLITE_OMIT_CAST
22730
+ case TK_CAST: {
22731
+ /* Expressions of the form: CAST(pLeft AS token) */
22732
+ sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
22733
+ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22734
+ break;
22735
+ }
22736
+#endif /* SQLITE_OMIT_CAST */
22737
+ case TK_LT: zBinOp = "LT"; break;
22738
+ case TK_LE: zBinOp = "LE"; break;
22739
+ case TK_GT: zBinOp = "GT"; break;
22740
+ case TK_GE: zBinOp = "GE"; break;
22741
+ case TK_NE: zBinOp = "NE"; break;
22742
+ case TK_EQ: zBinOp = "EQ"; break;
22743
+ case TK_IS: zBinOp = "IS"; break;
22744
+ case TK_ISNOT: zBinOp = "ISNOT"; break;
22745
+ case TK_AND: zBinOp = "AND"; break;
22746
+ case TK_OR: zBinOp = "OR"; break;
22747
+ case TK_PLUS: zBinOp = "ADD"; break;
22748
+ case TK_STAR: zBinOp = "MUL"; break;
22749
+ case TK_MINUS: zBinOp = "SUB"; break;
22750
+ case TK_REM: zBinOp = "REM"; break;
22751
+ case TK_BITAND: zBinOp = "BITAND"; break;
22752
+ case TK_BITOR: zBinOp = "BITOR"; break;
22753
+ case TK_SLASH: zBinOp = "DIV"; break;
22754
+ case TK_LSHIFT: zBinOp = "LSHIFT"; break;
22755
+ case TK_RSHIFT: zBinOp = "RSHIFT"; break;
22756
+ case TK_CONCAT: zBinOp = "CONCAT"; break;
22757
+ case TK_DOT: zBinOp = "DOT"; break;
22758
+
22759
+ case TK_UMINUS: zUniOp = "UMINUS"; break;
22760
+ case TK_UPLUS: zUniOp = "UPLUS"; break;
22761
+ case TK_BITNOT: zUniOp = "BITNOT"; break;
22762
+ case TK_NOT: zUniOp = "NOT"; break;
22763
+ case TK_ISNULL: zUniOp = "ISNULL"; break;
22764
+ case TK_NOTNULL: zUniOp = "NOTNULL"; break;
22765
+
22766
+ case TK_COLLATE: {
22767
+ sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
22768
+ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22769
+ break;
22770
+ }
22771
+
22772
+ case TK_AGG_FUNCTION:
22773
+ case TK_FUNCTION: {
22774
+ ExprList *pFarg; /* List of function arguments */
22775
+ if( ExprHasProperty(pExpr, EP_TokenOnly) ){
22776
+ pFarg = 0;
22777
+ }else{
22778
+ pFarg = pExpr->x.pList;
22779
+ }
22780
+ if( pExpr->op==TK_AGG_FUNCTION ){
22781
+ sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
22782
+ pExpr->op2, pExpr->u.zToken);
22783
+ }else{
22784
+ sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
22785
+ }
22786
+ if( pFarg ){
22787
+ sqlite3TreeViewExprList(pView, pFarg, 0, 0);
22788
+ }
22789
+ break;
22790
+ }
22791
+#ifndef SQLITE_OMIT_SUBQUERY
22792
+ case TK_EXISTS: {
22793
+ sqlite3TreeViewLine(pView, "EXISTS-expr");
22794
+ sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22795
+ break;
22796
+ }
22797
+ case TK_SELECT: {
22798
+ sqlite3TreeViewLine(pView, "SELECT-expr");
22799
+ sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22800
+ break;
22801
+ }
22802
+ case TK_IN: {
22803
+ sqlite3TreeViewLine(pView, "IN");
22804
+ sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22805
+ if( ExprHasProperty(pExpr, EP_xIsSelect) ){
22806
+ sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22807
+ }else{
22808
+ sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
22809
+ }
22810
+ break;
22811
+ }
22812
+#endif /* SQLITE_OMIT_SUBQUERY */
22813
+
22814
+ /*
22815
+ ** x BETWEEN y AND z
22816
+ **
22817
+ ** This is equivalent to
22818
+ **
22819
+ ** x>=y AND x<=z
22820
+ **
22821
+ ** X is stored in pExpr->pLeft.
22822
+ ** Y is stored in pExpr->pList->a[0].pExpr.
22823
+ ** Z is stored in pExpr->pList->a[1].pExpr.
22824
+ */
22825
+ case TK_BETWEEN: {
22826
+ Expr *pX = pExpr->pLeft;
22827
+ Expr *pY = pExpr->x.pList->a[0].pExpr;
22828
+ Expr *pZ = pExpr->x.pList->a[1].pExpr;
22829
+ sqlite3TreeViewLine(pView, "BETWEEN");
22830
+ sqlite3TreeViewExpr(pView, pX, 1);
22831
+ sqlite3TreeViewExpr(pView, pY, 1);
22832
+ sqlite3TreeViewExpr(pView, pZ, 0);
22833
+ break;
22834
+ }
22835
+ case TK_TRIGGER: {
22836
+ /* If the opcode is TK_TRIGGER, then the expression is a reference
22837
+ ** to a column in the new.* or old.* pseudo-tables available to
22838
+ ** trigger programs. In this case Expr.iTable is set to 1 for the
22839
+ ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
22840
+ ** is set to the column of the pseudo-table to read, or to -1 to
22841
+ ** read the rowid field.
22842
+ */
22843
+ sqlite3TreeViewLine(pView, "%s(%d)",
22844
+ pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
22845
+ break;
22846
+ }
22847
+ case TK_CASE: {
22848
+ sqlite3TreeViewLine(pView, "CASE");
22849
+ sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22850
+ sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
22851
+ break;
22852
+ }
22853
+#ifndef SQLITE_OMIT_TRIGGER
22854
+ case TK_RAISE: {
22855
+ const char *zType = "unk";
22856
+ switch( pExpr->affinity ){
22857
+ case OE_Rollback: zType = "rollback"; break;
22858
+ case OE_Abort: zType = "abort"; break;
22859
+ case OE_Fail: zType = "fail"; break;
22860
+ case OE_Ignore: zType = "ignore"; break;
22861
+ }
22862
+ sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
22863
+ break;
22864
+ }
22865
+#endif
22866
+ default: {
22867
+ sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
22868
+ break;
22869
+ }
22870
+ }
22871
+ if( zBinOp ){
22872
+ sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
22873
+ sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22874
+ sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
22875
+ }else if( zUniOp ){
22876
+ sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
22877
+ sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22878
+ }
22879
+ sqlite3TreeViewPop(pView);
22880
+}
22881
+
22882
+/*
22883
+** Generate a human-readable explanation of an expression list.
22884
+*/
22885
+SQLITE_PRIVATE void sqlite3TreeViewExprList(
22886
+ TreeView *pView,
22887
+ const ExprList *pList,
22888
+ u8 moreToFollow,
22889
+ const char *zLabel
22890
+){
22891
+ int i;
22892
+ pView = sqlite3TreeViewPush(pView, moreToFollow);
22893
+ if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
22894
+ if( pList==0 ){
22895
+ sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
22896
+ }else{
22897
+ sqlite3TreeViewLine(pView, "%s", zLabel);
22898
+ for(i=0; i<pList->nExpr; i++){
22899
+ sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
22900
+ }
22901
+ }
22902
+ sqlite3TreeViewPop(pView);
22903
+}
22904
+
2249622905
#endif /* SQLITE_DEBUG */
2249722906
22498
-/*
22499
-** variable-argument wrapper around sqlite3VXPrintf().
22500
-*/
22501
-SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
22502
- va_list ap;
22503
- va_start(ap,zFormat);
22504
- sqlite3VXPrintf(p, bFlags, zFormat, ap);
22505
- va_end(ap);
22506
-}
22507
-
22508
-/************** End of printf.c **********************************************/
22907
+/************** End of treeview.c ********************************************/
2250922908
/************** Begin file random.c ******************************************/
2251022909
/*
2251122910
** 2001 September 15
2251222911
**
2251322912
** The author disclaims copyright to this source code. In place of
@@ -23544,14 +23943,12 @@
2354423943
** The value returned will never be negative. Nor will it ever be greater
2354523944
** than the actual length of the string. For very long strings (greater
2354623945
** than 1GiB) the value returned might be less than the true string length.
2354723946
*/
2354823947
SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
23549
- const char *z2 = z;
2355023948
if( z==0 ) return 0;
23551
- while( *z2 ){ z2++; }
23552
- return 0x3fffffff & (int)(z2 - z);
23949
+ return 0x3fffffff & (int)strlen(z);
2355323950
}
2355423951
2355523952
/*
2355623953
** Set the current error code to err_code and clear any prior error message.
2355723954
*/
@@ -24519,18 +24916,35 @@
2451924916
2452024917
/*
2452124918
** Read or write a four-byte big-endian integer value.
2452224919
*/
2452324920
SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
24921
+#if SQLITE_BYTEORDER==4321
24922
+ u32 x;
24923
+ memcpy(&x,p,4);
24924
+ return x;
24925
+#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
24926
+ u32 x;
24927
+ memcpy(&x,p,4);
24928
+ return __builtin_bswap32(x);
24929
+#else
2452424930
testcase( p[0]&0x80 );
2452524931
return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
24932
+#endif
2452624933
}
2452724934
SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
24935
+#if SQLITE_BYTEORDER==4321
24936
+ memcpy(p,&v,4);
24937
+#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
24938
+ u32 x = __builtin_bswap32(v);
24939
+ memcpy(p,&x,4);
24940
+#else
2452824941
p[0] = (u8)(v>>24);
2452924942
p[1] = (u8)(v>>16);
2453024943
p[2] = (u8)(v>>8);
2453124944
p[3] = (u8)v;
24945
+#endif
2453224946
}
2453324947
2453424948
2453524949
2453624950
/*
@@ -25094,46 +25508,46 @@
2509425508
#else
2509525509
# define OpHelp(X)
2509625510
#endif
2509725511
SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
2509825512
static const char *const azName[] = { "?",
25099
- /* 1 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
25100
- /* 2 */ "Savepoint" OpHelp(""),
25101
- /* 3 */ "AutoCommit" OpHelp(""),
25102
- /* 4 */ "Transaction" OpHelp(""),
25103
- /* 5 */ "SorterNext" OpHelp(""),
25104
- /* 6 */ "PrevIfOpen" OpHelp(""),
25105
- /* 7 */ "NextIfOpen" OpHelp(""),
25106
- /* 8 */ "Prev" OpHelp(""),
25107
- /* 9 */ "Next" OpHelp(""),
25108
- /* 10 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
25109
- /* 11 */ "Checkpoint" OpHelp(""),
25110
- /* 12 */ "JournalMode" OpHelp(""),
25111
- /* 13 */ "Vacuum" OpHelp(""),
25112
- /* 14 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
25113
- /* 15 */ "VUpdate" OpHelp("data=r[P3@P2]"),
25114
- /* 16 */ "Goto" OpHelp(""),
25115
- /* 17 */ "Gosub" OpHelp(""),
25116
- /* 18 */ "Return" OpHelp(""),
25513
+ /* 1 */ "Savepoint" OpHelp(""),
25514
+ /* 2 */ "AutoCommit" OpHelp(""),
25515
+ /* 3 */ "Transaction" OpHelp(""),
25516
+ /* 4 */ "SorterNext" OpHelp(""),
25517
+ /* 5 */ "PrevIfOpen" OpHelp(""),
25518
+ /* 6 */ "NextIfOpen" OpHelp(""),
25519
+ /* 7 */ "Prev" OpHelp(""),
25520
+ /* 8 */ "Next" OpHelp(""),
25521
+ /* 9 */ "Checkpoint" OpHelp(""),
25522
+ /* 10 */ "JournalMode" OpHelp(""),
25523
+ /* 11 */ "Vacuum" OpHelp(""),
25524
+ /* 12 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
25525
+ /* 13 */ "VUpdate" OpHelp("data=r[P3@P2]"),
25526
+ /* 14 */ "Goto" OpHelp(""),
25527
+ /* 15 */ "Gosub" OpHelp(""),
25528
+ /* 16 */ "Return" OpHelp(""),
25529
+ /* 17 */ "InitCoroutine" OpHelp(""),
25530
+ /* 18 */ "EndCoroutine" OpHelp(""),
2511725531
/* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
25118
- /* 20 */ "InitCoroutine" OpHelp(""),
25119
- /* 21 */ "EndCoroutine" OpHelp(""),
25120
- /* 22 */ "Yield" OpHelp(""),
25121
- /* 23 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
25122
- /* 24 */ "Halt" OpHelp(""),
25123
- /* 25 */ "Integer" OpHelp("r[P2]=P1"),
25124
- /* 26 */ "Int64" OpHelp("r[P2]=P4"),
25125
- /* 27 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
25126
- /* 28 */ "Null" OpHelp("r[P2..P3]=NULL"),
25127
- /* 29 */ "SoftNull" OpHelp("r[P1]=NULL"),
25128
- /* 30 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
25129
- /* 31 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
25130
- /* 32 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
25131
- /* 33 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
25132
- /* 34 */ "SCopy" OpHelp("r[P2]=r[P1]"),
25133
- /* 35 */ "ResultRow" OpHelp("output=r[P1@P2]"),
25134
- /* 36 */ "CollSeq" OpHelp(""),
25532
+ /* 20 */ "Yield" OpHelp(""),
25533
+ /* 21 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
25534
+ /* 22 */ "Halt" OpHelp(""),
25535
+ /* 23 */ "Integer" OpHelp("r[P2]=P1"),
25536
+ /* 24 */ "Int64" OpHelp("r[P2]=P4"),
25537
+ /* 25 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
25538
+ /* 26 */ "Null" OpHelp("r[P2..P3]=NULL"),
25539
+ /* 27 */ "SoftNull" OpHelp("r[P1]=NULL"),
25540
+ /* 28 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
25541
+ /* 29 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
25542
+ /* 30 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
25543
+ /* 31 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
25544
+ /* 32 */ "SCopy" OpHelp("r[P2]=r[P1]"),
25545
+ /* 33 */ "ResultRow" OpHelp("output=r[P1@P2]"),
25546
+ /* 34 */ "CollSeq" OpHelp(""),
25547
+ /* 35 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
25548
+ /* 36 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
2513525549
/* 37 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
2513625550
/* 38 */ "MustBeInt" OpHelp(""),
2513725551
/* 39 */ "RealAffinity" OpHelp(""),
2513825552
/* 40 */ "Cast" OpHelp("affinity(r[P1])"),
2513925553
/* 41 */ "Permutation" OpHelp(""),
@@ -25155,33 +25569,33 @@
2515525569
/* 57 */ "OpenEphemeral" OpHelp("nColumn=P2"),
2515625570
/* 58 */ "SorterOpen" OpHelp(""),
2515725571
/* 59 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
2515825572
/* 60 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
2515925573
/* 61 */ "Close" OpHelp(""),
25160
- /* 62 */ "SeekLT" OpHelp("key=r[P3@P4]"),
25161
- /* 63 */ "SeekLE" OpHelp("key=r[P3@P4]"),
25162
- /* 64 */ "SeekGE" OpHelp("key=r[P3@P4]"),
25163
- /* 65 */ "SeekGT" OpHelp("key=r[P3@P4]"),
25164
- /* 66 */ "Seek" OpHelp("intkey=r[P2]"),
25165
- /* 67 */ "NoConflict" OpHelp("key=r[P3@P4]"),
25166
- /* 68 */ "NotFound" OpHelp("key=r[P3@P4]"),
25167
- /* 69 */ "Found" OpHelp("key=r[P3@P4]"),
25168
- /* 70 */ "NotExists" OpHelp("intkey=r[P3]"),
25574
+ /* 62 */ "ColumnsUsed" OpHelp(""),
25575
+ /* 63 */ "SeekLT" OpHelp("key=r[P3@P4]"),
25576
+ /* 64 */ "SeekLE" OpHelp("key=r[P3@P4]"),
25577
+ /* 65 */ "SeekGE" OpHelp("key=r[P3@P4]"),
25578
+ /* 66 */ "SeekGT" OpHelp("key=r[P3@P4]"),
25579
+ /* 67 */ "Seek" OpHelp("intkey=r[P2]"),
25580
+ /* 68 */ "NoConflict" OpHelp("key=r[P3@P4]"),
25581
+ /* 69 */ "NotFound" OpHelp("key=r[P3@P4]"),
25582
+ /* 70 */ "Found" OpHelp("key=r[P3@P4]"),
2516925583
/* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
2517025584
/* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
25171
- /* 73 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
25172
- /* 74 */ "NewRowid" OpHelp("r[P2]=rowid"),
25173
- /* 75 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
25585
+ /* 73 */ "NotExists" OpHelp("intkey=r[P3]"),
25586
+ /* 74 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
25587
+ /* 75 */ "NewRowid" OpHelp("r[P2]=rowid"),
2517425588
/* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
2517525589
/* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
2517625590
/* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
2517725591
/* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
2517825592
/* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
2517925593
/* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
2518025594
/* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
2518125595
/* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
25182
- /* 84 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
25596
+ /* 84 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
2518325597
/* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
2518425598
/* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
2518525599
/* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
2518625600
/* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
2518725601
/* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -25188,73 +25602,76 @@
2518825602
/* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
2518925603
/* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
2519025604
/* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
2519125605
/* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
2519225606
/* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
25193
- /* 95 */ "Delete" OpHelp(""),
25607
+ /* 95 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
2519425608
/* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
2519525609
/* 97 */ "String8" OpHelp("r[P2]='P4'"),
25196
- /* 98 */ "ResetCount" OpHelp(""),
25197
- /* 99 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
25198
- /* 100 */ "SorterData" OpHelp("r[P2]=data"),
25199
- /* 101 */ "RowKey" OpHelp("r[P2]=key"),
25200
- /* 102 */ "RowData" OpHelp("r[P2]=data"),
25201
- /* 103 */ "Rowid" OpHelp("r[P2]=rowid"),
25202
- /* 104 */ "NullRow" OpHelp(""),
25203
- /* 105 */ "Last" OpHelp(""),
25204
- /* 106 */ "SorterSort" OpHelp(""),
25205
- /* 107 */ "Sort" OpHelp(""),
25206
- /* 108 */ "Rewind" OpHelp(""),
25207
- /* 109 */ "SorterInsert" OpHelp(""),
25208
- /* 110 */ "IdxInsert" OpHelp("key=r[P2]"),
25209
- /* 111 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
25210
- /* 112 */ "IdxRowid" OpHelp("r[P2]=rowid"),
25211
- /* 113 */ "IdxLE" OpHelp("key=r[P3@P4]"),
25212
- /* 114 */ "IdxGT" OpHelp("key=r[P3@P4]"),
25213
- /* 115 */ "IdxLT" OpHelp("key=r[P3@P4]"),
25214
- /* 116 */ "IdxGE" OpHelp("key=r[P3@P4]"),
25215
- /* 117 */ "Destroy" OpHelp(""),
25216
- /* 118 */ "Clear" OpHelp(""),
25217
- /* 119 */ "ResetSorter" OpHelp(""),
25218
- /* 120 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
25219
- /* 121 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
25220
- /* 122 */ "ParseSchema" OpHelp(""),
25221
- /* 123 */ "LoadAnalysis" OpHelp(""),
25222
- /* 124 */ "DropTable" OpHelp(""),
25223
- /* 125 */ "DropIndex" OpHelp(""),
25224
- /* 126 */ "DropTrigger" OpHelp(""),
25225
- /* 127 */ "IntegrityCk" OpHelp(""),
25226
- /* 128 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
25227
- /* 129 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
25228
- /* 130 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
25229
- /* 131 */ "Program" OpHelp(""),
25230
- /* 132 */ "Param" OpHelp(""),
25610
+ /* 98 */ "Delete" OpHelp(""),
25611
+ /* 99 */ "ResetCount" OpHelp(""),
25612
+ /* 100 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
25613
+ /* 101 */ "SorterData" OpHelp("r[P2]=data"),
25614
+ /* 102 */ "RowKey" OpHelp("r[P2]=key"),
25615
+ /* 103 */ "RowData" OpHelp("r[P2]=data"),
25616
+ /* 104 */ "Rowid" OpHelp("r[P2]=rowid"),
25617
+ /* 105 */ "NullRow" OpHelp(""),
25618
+ /* 106 */ "Last" OpHelp(""),
25619
+ /* 107 */ "SorterSort" OpHelp(""),
25620
+ /* 108 */ "Sort" OpHelp(""),
25621
+ /* 109 */ "Rewind" OpHelp(""),
25622
+ /* 110 */ "SorterInsert" OpHelp(""),
25623
+ /* 111 */ "IdxInsert" OpHelp("key=r[P2]"),
25624
+ /* 112 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
25625
+ /* 113 */ "IdxRowid" OpHelp("r[P2]=rowid"),
25626
+ /* 114 */ "IdxLE" OpHelp("key=r[P3@P4]"),
25627
+ /* 115 */ "IdxGT" OpHelp("key=r[P3@P4]"),
25628
+ /* 116 */ "IdxLT" OpHelp("key=r[P3@P4]"),
25629
+ /* 117 */ "IdxGE" OpHelp("key=r[P3@P4]"),
25630
+ /* 118 */ "Destroy" OpHelp(""),
25631
+ /* 119 */ "Clear" OpHelp(""),
25632
+ /* 120 */ "ResetSorter" OpHelp(""),
25633
+ /* 121 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
25634
+ /* 122 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
25635
+ /* 123 */ "ParseSchema" OpHelp(""),
25636
+ /* 124 */ "LoadAnalysis" OpHelp(""),
25637
+ /* 125 */ "DropTable" OpHelp(""),
25638
+ /* 126 */ "DropIndex" OpHelp(""),
25639
+ /* 127 */ "DropTrigger" OpHelp(""),
25640
+ /* 128 */ "IntegrityCk" OpHelp(""),
25641
+ /* 129 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
25642
+ /* 130 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
25643
+ /* 131 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
25644
+ /* 132 */ "Program" OpHelp(""),
2523125645
/* 133 */ "Real" OpHelp("r[P2]=P4"),
25232
- /* 134 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
25233
- /* 135 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
25234
- /* 136 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
25235
- /* 137 */ "IfPos" OpHelp("if r[P1]>0 goto P2"),
25236
- /* 138 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
25237
- /* 139 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"),
25238
- /* 140 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
25239
- /* 141 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"),
25240
- /* 142 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
25241
- /* 143 */ "IncrVacuum" OpHelp(""),
25242
- /* 144 */ "Expire" OpHelp(""),
25243
- /* 145 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
25244
- /* 146 */ "VBegin" OpHelp(""),
25245
- /* 147 */ "VCreate" OpHelp(""),
25246
- /* 148 */ "VDestroy" OpHelp(""),
25247
- /* 149 */ "VOpen" OpHelp(""),
25248
- /* 150 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
25249
- /* 151 */ "VNext" OpHelp(""),
25250
- /* 152 */ "VRename" OpHelp(""),
25251
- /* 153 */ "Pagecount" OpHelp(""),
25252
- /* 154 */ "MaxPgcnt" OpHelp(""),
25253
- /* 155 */ "Init" OpHelp("Start at P2"),
25254
- /* 156 */ "Noop" OpHelp(""),
25255
- /* 157 */ "Explain" OpHelp(""),
25646
+ /* 134 */ "Param" OpHelp(""),
25647
+ /* 135 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
25648
+ /* 136 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
25649
+ /* 137 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
25650
+ /* 138 */ "IfPos" OpHelp("if r[P1]>0 goto P2"),
25651
+ /* 139 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
25652
+ /* 140 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"),
25653
+ /* 141 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
25654
+ /* 142 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"),
25655
+ /* 143 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
25656
+ /* 144 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
25657
+ /* 145 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
25658
+ /* 146 */ "IncrVacuum" OpHelp(""),
25659
+ /* 147 */ "Expire" OpHelp(""),
25660
+ /* 148 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
25661
+ /* 149 */ "VBegin" OpHelp(""),
25662
+ /* 150 */ "VCreate" OpHelp(""),
25663
+ /* 151 */ "VDestroy" OpHelp(""),
25664
+ /* 152 */ "VOpen" OpHelp(""),
25665
+ /* 153 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
25666
+ /* 154 */ "VNext" OpHelp(""),
25667
+ /* 155 */ "VRename" OpHelp(""),
25668
+ /* 156 */ "Pagecount" OpHelp(""),
25669
+ /* 157 */ "MaxPgcnt" OpHelp(""),
25670
+ /* 158 */ "Init" OpHelp("Start at P2"),
25671
+ /* 159 */ "Noop" OpHelp(""),
25672
+ /* 160 */ "Explain" OpHelp(""),
2525625673
};
2525725674
return azName[i];
2525825675
}
2525925676
#endif
2526025677
@@ -38989,14 +39406,14 @@
3898939406
/*
3899039407
** Check to see if the i-th bit is set. Return true or false.
3899139408
** If p is NULL (if the bitmap has not been created) or if
3899239409
** i is out of range, then return false.
3899339410
*/
38994
-SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
38995
- if( p==0 ) return 0;
38996
- if( i>p->iSize || i==0 ) return 0;
39411
+SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
39412
+ assert( p!=0 );
3899739413
i--;
39414
+ if( i>=p->iSize ) return 0;
3899839415
while( p->iDivisor ){
3899939416
u32 bin = i/p->iDivisor;
3900039417
i = i%p->iDivisor;
3900139418
p = p->u.apSub[bin];
3900239419
if (!p) {
@@ -39011,10 +39428,13 @@
3901139428
if( p->u.aHash[h]==i ) return 1;
3901239429
h = (h+1) % BITVEC_NINT;
3901339430
}
3901439431
return 0;
3901539432
}
39433
+}
39434
+SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
39435
+ return p!=0 && sqlite3BitvecTestNotNull(p,i);
3901639436
}
3901739437
3901839438
/*
3901939439
** Set the i-th bit. Return 0 on success and an error code if
3902039440
** anything goes wrong.
@@ -39300,11 +39720,10 @@
3930039720
u8 bPurgeable; /* True if pages are on backing store */
3930139721
u8 eCreate; /* eCreate value for for xFetch() */
3930239722
int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
3930339723
void *pStress; /* Argument to xStress */
3930439724
sqlite3_pcache *pCache; /* Pluggable cache module */
39305
- PgHdr *pPage1; /* Reference to page 1 */
3930639725
};
3930739726
3930839727
/********************************** Linked List Management ********************/
3930939728
3931039729
/* Allowed values for second argument to pcacheManageDirtyList() */
@@ -39378,13 +39797,10 @@
3937839797
** Wrapper around the pluggable caches xUnpin method. If the cache is
3937939798
** being used for an in-memory database, this function is a no-op.
3938039799
*/
3938139800
static void pcacheUnpin(PgHdr *p){
3938239801
if( p->pCache->bPurgeable ){
39383
- if( p->pgno==1 ){
39384
- p->pCache->pPage1 = 0;
39385
- }
3938639802
sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
3938739803
}
3938839804
}
3938939805
3939039806
/*
@@ -39473,11 +39889,10 @@
3947339889
sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
3947439890
if( pCache->pCache ){
3947539891
sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
3947639892
}
3947739893
pCache->pCache = pNew;
39478
- pCache->pPage1 = 0;
3947939894
pCache->szPage = szPage;
3948039895
}
3948139896
return SQLITE_OK;
3948239897
}
3948339898
@@ -39598,17 +40013,18 @@
3959840013
){
3959940014
PgHdr *pPgHdr;
3960040015
assert( pPage!=0 );
3960140016
pPgHdr = (PgHdr*)pPage->pExtra;
3960240017
assert( pPgHdr->pPage==0 );
39603
- memset(pPgHdr, 0, sizeof(PgHdr));
40018
+ memset(pPgHdr, 0, sizeof(PgHdr));
3960440019
pPgHdr->pPage = pPage;
3960540020
pPgHdr->pData = pPage->pBuf;
3960640021
pPgHdr->pExtra = (void *)&pPgHdr[1];
3960740022
memset(pPgHdr->pExtra, 0, pCache->szExtra);
3960840023
pPgHdr->pCache = pCache;
3960940024
pPgHdr->pgno = pgno;
40025
+ pPgHdr->flags = PGHDR_CLEAN;
3961040026
return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
3961140027
}
3961240028
3961340029
/*
3961440030
** This routine converts the sqlite3_pcache_page object returned by
@@ -39621,23 +40037,20 @@
3962140037
Pgno pgno, /* Page number obtained */
3962240038
sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
3962340039
){
3962440040
PgHdr *pPgHdr;
3962540041
39626
- if( pPage==0 ) return 0;
40042
+ assert( pPage!=0 );
3962740043
pPgHdr = (PgHdr *)pPage->pExtra;
3962840044
3962940045
if( !pPgHdr->pPage ){
3963040046
return pcacheFetchFinishWithInit(pCache, pgno, pPage);
3963140047
}
3963240048
if( 0==pPgHdr->nRef ){
3963340049
pCache->nRef++;
3963440050
}
3963540051
pPgHdr->nRef++;
39636
- if( pgno==1 ){
39637
- pCache->pPage1 = pPgHdr;
39638
- }
3963940052
return pPgHdr;
3964040053
}
3964140054
3964240055
/*
3964340056
** Decrement the reference count on a page. If the page is clean and the
@@ -39646,11 +40059,11 @@
3964640059
SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
3964740060
assert( p->nRef>0 );
3964840061
p->nRef--;
3964940062
if( p->nRef==0 ){
3965040063
p->pCache->nRef--;
39651
- if( (p->flags&PGHDR_DIRTY)==0 ){
40064
+ if( p->flags&PGHDR_CLEAN ){
3965240065
pcacheUnpin(p);
3965340066
}else if( p->pDirtyPrev!=0 ){
3965440067
/* Move the page to the head of the dirty list. */
3965540068
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
3965640069
}
@@ -39674,37 +40087,39 @@
3967440087
assert( p->nRef==1 );
3967540088
if( p->flags&PGHDR_DIRTY ){
3967640089
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
3967740090
}
3967840091
p->pCache->nRef--;
39679
- if( p->pgno==1 ){
39680
- p->pCache->pPage1 = 0;
39681
- }
3968240092
sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
3968340093
}
3968440094
3968540095
/*
3968640096
** Make sure the page is marked as dirty. If it isn't dirty already,
3968740097
** make it so.
3968840098
*/
3968940099
SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
39690
- p->flags &= ~PGHDR_DONT_WRITE;
3969140100
assert( p->nRef>0 );
39692
- if( 0==(p->flags & PGHDR_DIRTY) ){
39693
- p->flags |= PGHDR_DIRTY;
39694
- pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
40101
+ if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){
40102
+ p->flags &= ~PGHDR_DONT_WRITE;
40103
+ if( p->flags & PGHDR_CLEAN ){
40104
+ p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
40105
+ assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
40106
+ pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
40107
+ }
3969540108
}
3969640109
}
3969740110
3969840111
/*
3969940112
** Make sure the page is marked as clean. If it isn't clean already,
3970040113
** make it so.
3970140114
*/
3970240115
SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
3970340116
if( (p->flags & PGHDR_DIRTY) ){
40117
+ assert( (p->flags & PGHDR_CLEAN)==0 );
3970440118
pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
39705
- p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
40119
+ p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
40120
+ p->flags |= PGHDR_CLEAN;
3970640121
if( p->nRef==0 ){
3970740122
pcacheUnpin(p);
3970840123
}
3970940124
}
3971040125
}
@@ -39767,13 +40182,18 @@
3976740182
if( ALWAYS(p->pgno>pgno) ){
3976840183
assert( p->flags&PGHDR_DIRTY );
3976940184
sqlite3PcacheMakeClean(p);
3977040185
}
3977140186
}
39772
- if( pgno==0 && pCache->pPage1 ){
39773
- memset(pCache->pPage1->pData, 0, pCache->szPage);
39774
- pgno = 1;
40187
+ if( pgno==0 && pCache->nRef ){
40188
+ sqlite3_pcache_page *pPage1;
40189
+ pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
40190
+ if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
40191
+ ** pCache->nRef>0 */
40192
+ memset(pPage1->pBuf, 0, pCache->szPage);
40193
+ pgno = 1;
40194
+ }
3977540195
}
3977640196
sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
3977740197
}
3977840198
}
3977940199
@@ -40092,12 +40512,19 @@
4009240512
#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
4009340513
4009440514
/*
4009540515
** Macros to enter and leave the PCache LRU mutex.
4009640516
*/
40097
-#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
40098
-#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
40517
+#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
40518
+# define pcache1EnterMutex(X) assert((X)->mutex==0)
40519
+# define pcache1LeaveMutex(X) assert((X)->mutex==0)
40520
+# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
40521
+#else
40522
+# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
40523
+# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
40524
+# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
40525
+#endif
4009940526
4010040527
/******************************************************************************/
4010140528
/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
4010240529
4010340530
/*
@@ -40369,45 +40796,45 @@
4036940796
** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
4037040797
** LRU list, then this function is a no-op.
4037140798
**
4037240799
** The PGroup mutex must be held when this function is called.
4037340800
*/
40374
-static void pcache1PinPage(PgHdr1 *pPage){
40801
+static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
4037540802
PCache1 *pCache;
40376
- PGroup *pGroup;
4037740803
4037840804
assert( pPage!=0 );
4037940805
assert( pPage->isPinned==0 );
4038040806
pCache = pPage->pCache;
40381
- pGroup = pCache->pGroup;
40382
- assert( pPage->pLruNext || pPage==pGroup->pLruTail );
40383
- assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
40384
- assert( sqlite3_mutex_held(pGroup->mutex) );
40807
+ assert( pPage->pLruNext || pPage==pCache->pGroup->pLruTail );
40808
+ assert( pPage->pLruPrev || pPage==pCache->pGroup->pLruHead );
40809
+ assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
4038540810
if( pPage->pLruPrev ){
4038640811
pPage->pLruPrev->pLruNext = pPage->pLruNext;
4038740812
}else{
40388
- pGroup->pLruHead = pPage->pLruNext;
40813
+ pCache->pGroup->pLruHead = pPage->pLruNext;
4038940814
}
4039040815
if( pPage->pLruNext ){
4039140816
pPage->pLruNext->pLruPrev = pPage->pLruPrev;
4039240817
}else{
40393
- pGroup->pLruTail = pPage->pLruPrev;
40818
+ pCache->pGroup->pLruTail = pPage->pLruPrev;
4039440819
}
4039540820
pPage->pLruNext = 0;
4039640821
pPage->pLruPrev = 0;
4039740822
pPage->isPinned = 1;
4039840823
pCache->nRecyclable--;
40824
+ return pPage;
4039940825
}
4040040826
4040140827
4040240828
/*
4040340829
** Remove the page supplied as an argument from the hash table
4040440830
** (PCache1.apHash structure) that it is currently stored in.
40831
+** Also free the page if freePage is true.
4040540832
**
4040640833
** The PGroup mutex must be held when this function is called.
4040740834
*/
40408
-static void pcache1RemoveFromHash(PgHdr1 *pPage){
40835
+static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
4040940836
unsigned int h;
4041040837
PCache1 *pCache = pPage->pCache;
4041140838
PgHdr1 **pp;
4041240839
4041340840
assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
@@ -40414,10 +40841,11 @@
4041440841
h = pPage->iKey % pCache->nHash;
4041540842
for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
4041640843
*pp = (*pp)->pNext;
4041740844
4041840845
pCache->nPage--;
40846
+ if( freeFlag ) pcache1FreePage(pPage);
4041940847
}
4042040848
4042140849
/*
4042240850
** If there are currently more than nMaxPage pages allocated, try
4042340851
** to recycle pages to reduce the number allocated to nMaxPage.
@@ -40427,12 +40855,11 @@
4042740855
while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
4042840856
PgHdr1 *p = pGroup->pLruTail;
4042940857
assert( p->pCache->pGroup==pGroup );
4043040858
assert( p->isPinned==0 );
4043140859
pcache1PinPage(p);
40432
- pcache1RemoveFromHash(p);
40433
- pcache1FreePage(p);
40860
+ pcache1RemoveFromHash(p, 1);
4043440861
}
4043540862
}
4043640863
4043740864
/*
4043840865
** Discard all pages from cache pCache with a page number (key value)
@@ -40474,14 +40901,16 @@
4047440901
*/
4047540902
static int pcache1Init(void *NotUsed){
4047640903
UNUSED_PARAMETER(NotUsed);
4047740904
assert( pcache1.isInit==0 );
4047840905
memset(&pcache1, 0, sizeof(pcache1));
40906
+#if SQLITE_THREADSAFE
4047940907
if( sqlite3GlobalConfig.bCoreMutex ){
4048040908
pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
4048140909
pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
4048240910
}
40911
+#endif
4048340912
pcache1.grp.mxPinned = 10;
4048440913
pcache1.isInit = 1;
4048540914
return SQLITE_OK;
4048640915
}
4048740916
@@ -40650,11 +41079,11 @@
4065041079
|| pcache1UnderMemoryPressure(pCache)
4065141080
)){
4065241081
PCache1 *pOther;
4065341082
pPage = pGroup->pLruTail;
4065441083
assert( pPage->isPinned==0 );
40655
- pcache1RemoveFromHash(pPage);
41084
+ pcache1RemoveFromHash(pPage, 0);
4065641085
pcache1PinPage(pPage);
4065741086
pOther = pPage->pCache;
4065841087
4065941088
/* We want to verify that szPage and szExtra are the same for pOther
4066041089
** and pCache. Assert that we can verify this by comparing sums. */
@@ -40673,13 +41102,13 @@
4067341102
4067441103
/* Step 5. If a usable page buffer has still not been found,
4067541104
** attempt to allocate a new one.
4067641105
*/
4067741106
if( !pPage ){
40678
- if( createFlag==1 ) sqlite3BeginBenignMalloc();
41107
+ if( createFlag==1 ){ sqlite3BeginBenignMalloc(); }
4067941108
pPage = pcache1AllocPage(pCache);
40680
- if( createFlag==1 ) sqlite3EndBenignMalloc();
41109
+ if( createFlag==1 ){ sqlite3EndBenignMalloc(); }
4068141110
}
4068241111
4068341112
if( pPage ){
4068441113
unsigned int h = iKey % pCache->nHash;
4068541114
pCache->nPage++;
@@ -40749,41 +41178,81 @@
4074941178
** then attempt to recycle a page from the LRU list. If it is the right
4075041179
** size, return the recycled buffer. Otherwise, free the buffer and
4075141180
** proceed to step 5.
4075241181
**
4075341182
** 5. Otherwise, allocate and return a new page buffer.
41183
+**
41184
+** There are two versions of this routine. pcache1FetchWithMutex() is
41185
+** the general case. pcache1FetchNoMutex() is a faster implementation for
41186
+** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
41187
+** invokes the appropriate routine.
4075441188
*/
40755
-static sqlite3_pcache_page *pcache1Fetch(
41189
+static PgHdr1 *pcache1FetchNoMutex(
4075641190
sqlite3_pcache *p,
4075741191
unsigned int iKey,
4075841192
int createFlag
4075941193
){
4076041194
PCache1 *pCache = (PCache1 *)p;
4076141195
PgHdr1 *pPage = 0;
4076241196
40763
- assert( offsetof(PgHdr1,page)==0 );
40764
- assert( pCache->bPurgeable || createFlag!=1 );
40765
- assert( pCache->bPurgeable || pCache->nMin==0 );
40766
- assert( pCache->bPurgeable==0 || pCache->nMin==10 );
40767
- assert( pCache->nMin==0 || pCache->bPurgeable );
40768
- assert( pCache->nHash>0 );
40769
- pcache1EnterMutex(pCache->pGroup);
40770
-
4077141197
/* Step 1: Search the hash table for an existing entry. */
4077241198
pPage = pCache->apHash[iKey % pCache->nHash];
4077341199
while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
4077441200
4077541201
/* Step 2: Abort if no existing page is found and createFlag is 0 */
4077641202
if( pPage ){
40777
- if( !pPage->isPinned ) pcache1PinPage(pPage);
41203
+ if( !pPage->isPinned ){
41204
+ return pcache1PinPage(pPage);
41205
+ }else{
41206
+ return pPage;
41207
+ }
4077841208
}else if( createFlag ){
4077941209
/* Steps 3, 4, and 5 implemented by this subroutine */
40780
- pPage = pcache1FetchStage2(pCache, iKey, createFlag);
41210
+ return pcache1FetchStage2(pCache, iKey, createFlag);
41211
+ }else{
41212
+ return 0;
4078141213
}
41214
+}
41215
+#if PCACHE1_MIGHT_USE_GROUP_MUTEX
41216
+static PgHdr1 *pcache1FetchWithMutex(
41217
+ sqlite3_pcache *p,
41218
+ unsigned int iKey,
41219
+ int createFlag
41220
+){
41221
+ PCache1 *pCache = (PCache1 *)p;
41222
+ PgHdr1 *pPage;
41223
+
41224
+ pcache1EnterMutex(pCache->pGroup);
41225
+ pPage = pcache1FetchNoMutex(p, iKey, createFlag);
4078241226
assert( pPage==0 || pCache->iMaxKey>=iKey );
4078341227
pcache1LeaveMutex(pCache->pGroup);
40784
- return (sqlite3_pcache_page*)pPage;
41228
+ return pPage;
41229
+}
41230
+#endif
41231
+static sqlite3_pcache_page *pcache1Fetch(
41232
+ sqlite3_pcache *p,
41233
+ unsigned int iKey,
41234
+ int createFlag
41235
+){
41236
+#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
41237
+ PCache1 *pCache = (PCache1 *)p;
41238
+#endif
41239
+
41240
+ assert( offsetof(PgHdr1,page)==0 );
41241
+ assert( pCache->bPurgeable || createFlag!=1 );
41242
+ assert( pCache->bPurgeable || pCache->nMin==0 );
41243
+ assert( pCache->bPurgeable==0 || pCache->nMin==10 );
41244
+ assert( pCache->nMin==0 || pCache->bPurgeable );
41245
+ assert( pCache->nHash>0 );
41246
+#if PCACHE1_MIGHT_USE_GROUP_MUTEX
41247
+ if( pCache->pGroup->mutex ){
41248
+ return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
41249
+ }else
41250
+#endif
41251
+ {
41252
+ return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
41253
+ }
4078541254
}
4078641255
4078741256
4078841257
/*
4078941258
** Implementation of the sqlite3_pcache.xUnpin method.
@@ -40808,12 +41277,11 @@
4080841277
assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
4080941278
assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
4081041279
assert( pPage->isPinned==1 );
4081141280
4081241281
if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
40813
- pcache1RemoveFromHash(pPage);
40814
- pcache1FreePage(pPage);
41282
+ pcache1RemoveFromHash(pPage, 1);
4081541283
}else{
4081641284
/* Add the page to the PGroup LRU list. */
4081741285
if( pGroup->pLruHead ){
4081841286
pGroup->pLruHead->pLruPrev = pPage;
4081941287
pPage->pLruNext = pGroup->pLruHead;
@@ -40963,12 +41431,11 @@
4096341431
#ifdef SQLITE_PCACHE_SEPARATE_HEADER
4096441432
nFree += sqlite3MemSize(p);
4096541433
#endif
4096641434
assert( p->isPinned==0 );
4096741435
pcache1PinPage(p);
40968
- pcache1RemoveFromHash(p);
40969
- pcache1FreePage(p);
41436
+ pcache1RemoveFromHash(p, 1);
4097041437
}
4097141438
pcache1LeaveMutex(&pcache1.grp);
4097241439
}
4097341440
return nFree;
4097441441
}
@@ -42105,13 +42572,13 @@
4210542572
};
4210642573
4210742574
/*
4210842575
** Bits of the Pager.doNotSpill flag. See further description below.
4210942576
*/
42110
-#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
42111
-#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
42112
-#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
42577
+#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
42578
+#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
42579
+#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
4211342580
4211442581
/*
4211542582
** An open page cache is an instance of struct Pager. A description of
4211642583
** some of the more important member variables follows:
4211742584
**
@@ -42189,15 +42656,15 @@
4218942656
** comes up during savepoint rollback that requires the pcache module
4219042657
** to allocate a new page to prevent the journal file from being written
4219142658
** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
4219242659
** case is a user preference.
4219342660
**
42194
-** If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress()
42195
-** is permitted, but syncing the journal file is not. This flag is set
42196
-** by sqlite3PagerWrite() when the file-system sector-size is larger than
42197
-** the database page-size in order to prevent a journal sync from happening
42198
-** in between the journalling of two pages on the same sector.
42661
+** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
42662
+** pagerStress() is permitted, but syncing the journal file is not.
42663
+** This flag is set by sqlite3PagerWrite() when the file-system sector-size
42664
+** is larger than the database page-size in order to prevent a journal sync
42665
+** from happening in between the journalling of two pages on the same sector.
4219942666
**
4220042667
** subjInMemory
4220142668
**
4220242669
** This is a boolean variable. If true, then any required sub-journal
4220342670
** is opened as an in-memory journal file. If false, then in-memory
@@ -42296,11 +42763,11 @@
4229642763
u8 changeCountDone; /* Set after incrementing the change-counter */
4229742764
u8 setMaster; /* True if a m-j name has been written to jrnl */
4229842765
u8 doNotSpill; /* Do not spill the cache when non-zero */
4229942766
u8 subjInMemory; /* True to use in-memory sub-journals */
4230042767
u8 bUseFetch; /* True to use xFetch() */
42301
- u8 hasBeenUsed; /* True if any content previously read from this pager*/
42768
+ u8 hasBeenUsed; /* True if any content previously read */
4230242769
Pgno dbSize; /* Number of pages in the database */
4230342770
Pgno dbOrigSize; /* dbSize before the current transaction */
4230442771
Pgno dbFileSize; /* Number of pages in the database file */
4230542772
Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
4230642773
int errCode; /* One of several kinds of errors */
@@ -42457,11 +42924,11 @@
4245742924
**
4245842925
** instead of
4245942926
**
4246042927
** if( pPager->jfd->pMethods ){ ...
4246142928
*/
42462
-#define isOpen(pFd) ((pFd)->pMethods)
42929
+#define isOpen(pFd) ((pFd)->pMethods!=0)
4246342930
4246442931
/*
4246542932
** Return true if this pager uses a write-ahead log instead of the usual
4246642933
** rollback journal. Otherwise false.
4246742934
*/
@@ -42680,23 +43147,25 @@
4268043147
PagerSavepoint *p;
4268143148
Pgno pgno = pPg->pgno;
4268243149
int i;
4268343150
for(i=0; i<pPager->nSavepoint; i++){
4268443151
p = &pPager->aSavepoint[i];
42685
- if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
43152
+ if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
4268643153
return 1;
4268743154
}
4268843155
}
4268943156
return 0;
4269043157
}
4269143158
43159
+#ifdef SQLITE_DEBUG
4269243160
/*
4269343161
** Return true if the page is already in the journal file.
4269443162
*/
4269543163
static int pageInJournal(Pager *pPager, PgHdr *pPg){
4269643164
return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
4269743165
}
43166
+#endif
4269843167
4269943168
/*
4270043169
** Read a 32-bit integer from the given file descriptor. Store the integer
4270143170
** that is read in *pRes. Return SQLITE_OK if everything worked, or an
4270243171
** error code is something goes wrong.
@@ -43304,11 +43773,12 @@
4330443773
*/
4330543774
if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
4330643775
|| (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
4330743776
|| (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
4330843777
|| (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
43309
- || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
43778
+ || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
43779
+ iHdrOff+4+nMaster+8)))
4331043780
){
4331143781
return rc;
4331243782
}
4331343783
pPager->journalOff += (nMaster+20);
4331443784
@@ -43864,11 +44334,11 @@
4386444334
if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
4386544335
return SQLITE_DONE;
4386644336
}
4386744337
}
4386844338
43869
- /* If this page has already been played by before during the current
44339
+ /* If this page has already been played back before during the current
4387044340
** rollback, then don't bother to play it back again.
4387144341
*/
4387244342
if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
4387344343
return rc;
4387444344
}
@@ -45966,12 +46436,10 @@
4596646436
return rc;
4596746437
}
4596846438
4596946439
/*
4597046440
** Append a record of the current state of page pPg to the sub-journal.
45971
-** It is the callers responsibility to use subjRequiresPage() to check
45972
-** that it is really required before calling this function.
4597346441
**
4597446442
** If successful, set the bit corresponding to pPg->pgno in the bitvecs
4597546443
** for all open savepoints before returning.
4597646444
**
4597746445
** This function returns SQLITE_OK if everything is successful, an IO
@@ -46013,10 +46481,17 @@
4601346481
pPager->nSubRec++;
4601446482
assert( pPager->nSavepoint>0 );
4601546483
rc = addToSavepointBitvecs(pPager, pPg->pgno);
4601646484
}
4601746485
return rc;
46486
+}
46487
+static int subjournalPageIfRequired(PgHdr *pPg){
46488
+ if( subjRequiresPage(pPg) ){
46489
+ return subjournalPage(pPg);
46490
+ }else{
46491
+ return SQLITE_OK;
46492
+ }
4601846493
}
4601946494
4602046495
/*
4602146496
** This function is called by the pcache layer when it has reached some
4602246497
** soft memory limit. The first argument is a pointer to a Pager object
@@ -46071,13 +46546,11 @@
4607146546
}
4607246547
4607346548
pPg->pDirty = 0;
4607446549
if( pagerUseWal(pPager) ){
4607546550
/* Write a single frame for this page to the log. */
46076
- if( subjRequiresPage(pPg) ){
46077
- rc = subjournalPage(pPg);
46078
- }
46551
+ rc = subjournalPageIfRequired(pPg);
4607946552
if( rc==SQLITE_OK ){
4608046553
rc = pagerWalFrames(pPager, pPg, 0, 0);
4608146554
}
4608246555
}else{
4608346556
@@ -46086,43 +46559,10 @@
4608646559
|| pPager->eState==PAGER_WRITER_CACHEMOD
4608746560
){
4608846561
rc = syncJournal(pPager, 1);
4608946562
}
4609046563
46091
- /* If the page number of this page is larger than the current size of
46092
- ** the database image, it may need to be written to the sub-journal.
46093
- ** This is because the call to pager_write_pagelist() below will not
46094
- ** actually write data to the file in this case.
46095
- **
46096
- ** Consider the following sequence of events:
46097
- **
46098
- ** BEGIN;
46099
- ** <journal page X>
46100
- ** <modify page X>
46101
- ** SAVEPOINT sp;
46102
- ** <shrink database file to Y pages>
46103
- ** pagerStress(page X)
46104
- ** ROLLBACK TO sp;
46105
- **
46106
- ** If (X>Y), then when pagerStress is called page X will not be written
46107
- ** out to the database file, but will be dropped from the cache. Then,
46108
- ** following the "ROLLBACK TO sp" statement, reading page X will read
46109
- ** data from the database file. This will be the copy of page X as it
46110
- ** was when the transaction started, not as it was when "SAVEPOINT sp"
46111
- ** was executed.
46112
- **
46113
- ** The solution is to write the current data for page X into the
46114
- ** sub-journal file now (if it is not already there), so that it will
46115
- ** be restored to its current value when the "ROLLBACK TO sp" is
46116
- ** executed.
46117
- */
46118
- if( NEVER(
46119
- rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
46120
- ) ){
46121
- rc = subjournalPage(pPg);
46122
- }
46123
-
4612446564
/* Write the contents of the page out to the database file. */
4612546565
if( rc==SQLITE_OK ){
4612646566
assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
4612746567
rc = pager_write_pagelist(pPager, pPg);
4612846568
}
@@ -46374,11 +46814,11 @@
4637446814
** This branch also runs for files marked as immutable.
4637546815
*/
4637646816
act_like_temp_file:
4637746817
tempFile = 1;
4637846818
pPager->eState = PAGER_READER; /* Pretend we already have a lock */
46379
- pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE locking mode */
46819
+ pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
4638046820
pPager->noLock = 1; /* Do no locking */
4638146821
readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
4638246822
}
4638346823
4638446824
/* The following call to PagerSetPagesize() serves to set the value of
@@ -46393,11 +46833,11 @@
4639346833
/* Initialize the PCache object. */
4639446834
if( rc==SQLITE_OK ){
4639546835
assert( nExtra<1000 );
4639646836
nExtra = ROUND8(nExtra);
4639746837
rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
46398
- !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
46838
+ !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
4639946839
}
4640046840
4640146841
/* If an error occurred above, free the Pager structure and close the file.
4640246842
*/
4640346843
if( rc!=SQLITE_OK ){
@@ -46780,11 +47220,11 @@
4678047220
** see if the database has been modified. If the database has changed,
4678147221
** flush the cache. The pPager->hasBeenUsed flag prevents this from
4678247222
** occurring on the very first access to a file, in order to save a
4678347223
** single unnecessary sqlite3OsRead() call at the start-up.
4678447224
**
46785
- ** Database changes is detected by looking at 15 bytes beginning
47225
+ ** Database changes are detected by looking at 15 bytes beginning
4678647226
** at offset 24 into the file. The first 4 of these 16 bytes are
4678747227
** a 32-bit counter that is incremented with each change. The
4678847228
** other bytes change randomly with each file change when
4678947229
** a codec is in use.
4679047230
**
@@ -46988,13 +47428,18 @@
4698847428
sqlite3_pcache_page *pBase;
4698947429
pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
4699047430
if( pBase==0 ){
4699147431
rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
4699247432
if( rc!=SQLITE_OK ) goto pager_acquire_err;
47433
+ if( pBase==0 ){
47434
+ pPg = *ppPage = 0;
47435
+ rc = SQLITE_NOMEM;
47436
+ goto pager_acquire_err;
47437
+ }
4699347438
}
4699447439
pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
46995
- if( pPg==0 ) rc = SQLITE_NOMEM;
47440
+ assert( pPg!=0 );
4699647441
}
4699747442
}
4699847443
4699947444
if( rc!=SQLITE_OK ){
4700047445
/* Either the call to sqlite3PcacheFetch() returned an error or the
@@ -47001,14 +47446,15 @@
4700147446
** pager was already in the error-state when this function was called.
4700247447
** Set pPg to 0 and jump to the exception handler. */
4700347448
pPg = 0;
4700447449
goto pager_acquire_err;
4700547450
}
47006
- assert( (*ppPage)->pgno==pgno );
47007
- assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
47451
+ assert( pPg==(*ppPage) );
47452
+ assert( pPg->pgno==pgno );
47453
+ assert( pPg->pPager==pPager || pPg->pPager==0 );
4700847454
47009
- if( (*ppPage)->pPager && !noContent ){
47455
+ if( pPg->pPager && !noContent ){
4701047456
/* In this case the pcache already contains an initialized copy of
4701147457
** the page. Return without further ado. */
4701247458
assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
4701347459
pPager->aStat[PAGER_STAT_HIT]++;
4701447460
return SQLITE_OK;
@@ -47015,11 +47461,10 @@
4701547461
4701647462
}else{
4701747463
/* The pager cache has created a new page. Its content needs to
4701847464
** be initialized. */
4701947465
47020
- pPg = *ppPage;
4702147466
pPg->pPager = pPager;
4702247467
4702347468
/* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
4702447469
** number greater than this, or the unused locking-page, is requested. */
4702547470
if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
@@ -47094,10 +47539,11 @@
4709447539
assert( pPager!=0 );
4709547540
assert( pgno!=0 );
4709647541
assert( pPager->pPCache!=0 );
4709747542
pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
4709847543
assert( pPage==0 || pPager->hasBeenUsed );
47544
+ if( pPage==0 ) return 0;
4709947545
return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
4710047546
}
4710147547
4710247548
/*
4710347549
** Release a page reference.
@@ -47296,10 +47742,63 @@
4729647742
}
4729747743
4729847744
PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
4729947745
return rc;
4730047746
}
47747
+
47748
+/*
47749
+** Write page pPg onto the end of the rollback journal.
47750
+*/
47751
+static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
47752
+ Pager *pPager = pPg->pPager;
47753
+ int rc;
47754
+ u32 cksum;
47755
+ char *pData2;
47756
+ i64 iOff = pPager->journalOff;
47757
+
47758
+ /* We should never write to the journal file the page that
47759
+ ** contains the database locks. The following assert verifies
47760
+ ** that we do not. */
47761
+ assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
47762
+
47763
+ assert( pPager->journalHdr<=pPager->journalOff );
47764
+ CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
47765
+ cksum = pager_cksum(pPager, (u8*)pData2);
47766
+
47767
+ /* Even if an IO or diskfull error occurs while journalling the
47768
+ ** page in the block above, set the need-sync flag for the page.
47769
+ ** Otherwise, when the transaction is rolled back, the logic in
47770
+ ** playback_one_page() will think that the page needs to be restored
47771
+ ** in the database file. And if an IO error occurs while doing so,
47772
+ ** then corruption may follow.
47773
+ */
47774
+ pPg->flags |= PGHDR_NEED_SYNC;
47775
+
47776
+ rc = write32bits(pPager->jfd, iOff, pPg->pgno);
47777
+ if( rc!=SQLITE_OK ) return rc;
47778
+ rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
47779
+ if( rc!=SQLITE_OK ) return rc;
47780
+ rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
47781
+ if( rc!=SQLITE_OK ) return rc;
47782
+
47783
+ IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
47784
+ pPager->journalOff, pPager->pageSize));
47785
+ PAGER_INCR(sqlite3_pager_writej_count);
47786
+ PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
47787
+ PAGERID(pPager), pPg->pgno,
47788
+ ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
47789
+
47790
+ pPager->journalOff += 8 + pPager->pageSize;
47791
+ pPager->nRec++;
47792
+ assert( pPager->pInJournal!=0 );
47793
+ rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
47794
+ testcase( rc==SQLITE_NOMEM );
47795
+ assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
47796
+ rc |= addToSavepointBitvecs(pPager, pPg->pgno);
47797
+ assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
47798
+ return rc;
47799
+}
4730147800
4730247801
/*
4730347802
** Mark a single data page as writeable. The page is written into the
4730447803
** main journal or sub-journal as required. If the page is written into
4730547804
** one of the journals, the corresponding bit is set in the
@@ -47307,11 +47806,10 @@
4730747806
** of any open savepoints as appropriate.
4730847807
*/
4730947808
static int pager_write(PgHdr *pPg){
4731047809
Pager *pPager = pPg->pPager;
4731147810
int rc = SQLITE_OK;
47312
- int inJournal;
4731347811
4731447812
/* This routine is not called unless a write-transaction has already
4731547813
** been started. The journal file may or may not be open at this point.
4731647814
** It is never called in the ERROR state.
4731747815
*/
@@ -47320,11 +47818,10 @@
4732047818
|| pPager->eState==PAGER_WRITER_DBMOD
4732147819
);
4732247820
assert( assert_pager_state(pPager) );
4732347821
assert( pPager->errCode==0 );
4732447822
assert( pPager->readOnly==0 );
47325
-
4732647823
CHECK_PAGE(pPg);
4732747824
4732847825
/* The journal file needs to be opened. Higher level routines have already
4732947826
** obtained the necessary locks to begin the write-transaction, but the
4733047827
** rollback journal might not yet be open. Open it now if this is the case.
@@ -47339,95 +47836,52 @@
4733947836
if( rc!=SQLITE_OK ) return rc;
4734047837
}
4734147838
assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
4734247839
assert( assert_pager_state(pPager) );
4734347840
47344
- /* Mark the page as dirty. If the page has already been written
47345
- ** to the journal then we can return right away.
47346
- */
47347
- sqlite3PcacheMakeDirty(pPg);
47348
- inJournal = pageInJournal(pPager, pPg);
47349
- if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
47350
- assert( !pagerUseWal(pPager) );
47351
- }else{
47352
-
47353
- /* The transaction journal now exists and we have a RESERVED or an
47354
- ** EXCLUSIVE lock on the main database file. Write the current page to
47355
- ** the transaction journal if it is not there already.
47356
- */
47357
- if( !inJournal && !pagerUseWal(pPager) ){
47358
- assert( pagerUseWal(pPager)==0 );
47359
- if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
47360
- u32 cksum;
47361
- char *pData2;
47362
- i64 iOff = pPager->journalOff;
47363
-
47364
- /* We should never write to the journal file the page that
47365
- ** contains the database locks. The following assert verifies
47366
- ** that we do not. */
47367
- assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
47368
-
47369
- assert( pPager->journalHdr<=pPager->journalOff );
47370
- CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
47371
- cksum = pager_cksum(pPager, (u8*)pData2);
47372
-
47373
- /* Even if an IO or diskfull error occurs while journalling the
47374
- ** page in the block above, set the need-sync flag for the page.
47375
- ** Otherwise, when the transaction is rolled back, the logic in
47376
- ** playback_one_page() will think that the page needs to be restored
47377
- ** in the database file. And if an IO error occurs while doing so,
47378
- ** then corruption may follow.
47379
- */
47380
- pPg->flags |= PGHDR_NEED_SYNC;
47381
-
47382
- rc = write32bits(pPager->jfd, iOff, pPg->pgno);
47383
- if( rc!=SQLITE_OK ) return rc;
47384
- rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
47385
- if( rc!=SQLITE_OK ) return rc;
47386
- rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
47387
- if( rc!=SQLITE_OK ) return rc;
47388
-
47389
- IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
47390
- pPager->journalOff, pPager->pageSize));
47391
- PAGER_INCR(sqlite3_pager_writej_count);
47392
- PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
47393
- PAGERID(pPager), pPg->pgno,
47394
- ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
47395
-
47396
- pPager->journalOff += 8 + pPager->pageSize;
47397
- pPager->nRec++;
47398
- assert( pPager->pInJournal!=0 );
47399
- rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
47400
- testcase( rc==SQLITE_NOMEM );
47401
- assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
47402
- rc |= addToSavepointBitvecs(pPager, pPg->pgno);
47403
- if( rc!=SQLITE_OK ){
47404
- assert( rc==SQLITE_NOMEM );
47405
- return rc;
47406
- }
47407
- }else{
47408
- if( pPager->eState!=PAGER_WRITER_DBMOD ){
47409
- pPg->flags |= PGHDR_NEED_SYNC;
47410
- }
47411
- PAGERTRACE(("APPEND %d page %d needSync=%d\n",
47412
- PAGERID(pPager), pPg->pgno,
47413
- ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
47414
- }
47415
- }
47416
-
47417
- /* If the statement journal is open and the page is not in it,
47418
- ** then write the current page to the statement journal. Note that
47419
- ** the statement journal format differs from the standard journal format
47420
- ** in that it omits the checksums and the header.
47421
- */
47422
- if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
47423
- rc = subjournalPage(pPg);
47424
- }
47425
- }
47426
-
47427
- /* Update the database size and return.
47428
- */
47841
+ /* Mark the page that is about to be modified as dirty. */
47842
+ sqlite3PcacheMakeDirty(pPg);
47843
+
47844
+ /* If a rollback journal is in use, them make sure the page that is about
47845
+ ** to change is in the rollback journal, or if the page is a new page off
47846
+ ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
47847
+ */
47848
+ assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
47849
+ if( pPager->pInJournal!=0
47850
+ && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
47851
+ ){
47852
+ assert( pagerUseWal(pPager)==0 );
47853
+ if( pPg->pgno<=pPager->dbOrigSize ){
47854
+ rc = pagerAddPageToRollbackJournal(pPg);
47855
+ if( rc!=SQLITE_OK ){
47856
+ return rc;
47857
+ }
47858
+ }else{
47859
+ if( pPager->eState!=PAGER_WRITER_DBMOD ){
47860
+ pPg->flags |= PGHDR_NEED_SYNC;
47861
+ }
47862
+ PAGERTRACE(("APPEND %d page %d needSync=%d\n",
47863
+ PAGERID(pPager), pPg->pgno,
47864
+ ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
47865
+ }
47866
+ }
47867
+
47868
+ /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
47869
+ ** and before writing the page into the rollback journal. Wait until now,
47870
+ ** after the page has been successfully journalled, before setting the
47871
+ ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
47872
+ */
47873
+ pPg->flags |= PGHDR_WRITEABLE;
47874
+
47875
+ /* If the statement journal is open and the page is not in it,
47876
+ ** then write the page into the statement journal.
47877
+ */
47878
+ if( pPager->nSavepoint>0 ){
47879
+ rc = subjournalPageIfRequired(pPg);
47880
+ }
47881
+
47882
+ /* Update the database size and return. */
4742947883
if( pPager->dbSize<pPg->pgno ){
4743047884
pPager->dbSize = pPg->pgno;
4743147885
}
4743247886
return rc;
4743347887
}
@@ -47438,21 +47892,21 @@
4743847892
** all bytes of a sector are written together by hardware. Hence, all bytes of
4743947893
** a sector need to be journalled in case of a power loss in the middle of
4744047894
** a write.
4744147895
**
4744247896
** Usually, the sector size is less than or equal to the page size, in which
47443
-** case pages can be individually written. This routine only runs in the exceptional
47444
-** case where the page size is smaller than the sector size.
47897
+** case pages can be individually written. This routine only runs in the
47898
+** exceptional case where the page size is smaller than the sector size.
4744547899
*/
4744647900
static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
47447
- int rc = SQLITE_OK; /* Return code */
47448
- Pgno nPageCount; /* Total number of pages in database file */
47449
- Pgno pg1; /* First page of the sector pPg is located on. */
47450
- int nPage = 0; /* Number of pages starting at pg1 to journal */
47451
- int ii; /* Loop counter */
47452
- int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
47453
- Pager *pPager = pPg->pPager; /* The pager that owns pPg */
47901
+ int rc = SQLITE_OK; /* Return code */
47902
+ Pgno nPageCount; /* Total number of pages in database file */
47903
+ Pgno pg1; /* First page of the sector pPg is located on. */
47904
+ int nPage = 0; /* Number of pages starting at pg1 to journal */
47905
+ int ii; /* Loop counter */
47906
+ int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
47907
+ Pager *pPager = pPg->pPager; /* The pager that owns pPg */
4745447908
Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4745547909
4745647910
/* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
4745747911
** a journal header to be written between the pages journaled by
4745847912
** this function.
@@ -47536,15 +47990,19 @@
4753647990
**
4753747991
** If an error occurs, SQLITE_NOMEM or an IO error code is returned
4753847992
** as appropriate. Otherwise, SQLITE_OK.
4753947993
*/
4754047994
SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
47995
+ Pager *pPager = pPg->pPager;
4754147996
assert( (pPg->flags & PGHDR_MMAP)==0 );
47542
- assert( pPg->pPager->eState>=PAGER_WRITER_LOCKED );
47543
- assert( pPg->pPager->eState!=PAGER_ERROR );
47544
- assert( assert_pager_state(pPg->pPager) );
47545
- if( pPg->pPager->sectorSize > (u32)pPg->pPager->pageSize ){
47997
+ assert( pPager->eState>=PAGER_WRITER_LOCKED );
47998
+ assert( pPager->eState!=PAGER_ERROR );
47999
+ assert( assert_pager_state(pPager) );
48000
+ if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
48001
+ if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
48002
+ return SQLITE_OK;
48003
+ }else if( pPager->sectorSize > (u32)pPager->pageSize ){
4754648004
return pagerWriteLargeSector(pPg);
4754748005
}else{
4754848006
return pager_write(pPg);
4754948007
}
4755048008
}
@@ -47554,11 +48012,11 @@
4755448012
** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
4755548013
** to change the content of the page.
4755648014
*/
4755748015
#ifndef NDEBUG
4755848016
SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
47559
- return pPg->flags&PGHDR_DIRTY;
48017
+ return pPg->flags & PGHDR_WRITEABLE;
4756048018
}
4756148019
#endif
4756248020
4756348021
/*
4756448022
** A call to this routine tells the pager that it is not necessary to
@@ -47578,10 +48036,11 @@
4757848036
Pager *pPager = pPg->pPager;
4757948037
if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
4758048038
PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
4758148039
IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
4758248040
pPg->flags |= PGHDR_DONT_WRITE;
48041
+ pPg->flags &= ~PGHDR_WRITEABLE;
4758348042
pager_set_pagehash(pPg);
4758448043
}
4758548044
}
4758648045
4758748046
/*
@@ -48132,58 +48591,66 @@
4813248591
**
4813348592
** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
4813448593
** occurs while opening the sub-journal file, then an IO error code is
4813548594
** returned. Otherwise, SQLITE_OK.
4813648595
*/
48137
-SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
48596
+static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
4813848597
int rc = SQLITE_OK; /* Return code */
4813948598
int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
48599
+ int ii; /* Iterator variable */
48600
+ PagerSavepoint *aNew; /* New Pager.aSavepoint array */
4814048601
4814148602
assert( pPager->eState>=PAGER_WRITER_LOCKED );
48603
+ assert( assert_pager_state(pPager) );
48604
+ assert( nSavepoint>nCurrent && pPager->useJournal );
48605
+
48606
+ /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
48607
+ ** if the allocation fails. Otherwise, zero the new portion in case a
48608
+ ** malloc failure occurs while populating it in the for(...) loop below.
48609
+ */
48610
+ aNew = (PagerSavepoint *)sqlite3Realloc(
48611
+ pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
48612
+ );
48613
+ if( !aNew ){
48614
+ return SQLITE_NOMEM;
48615
+ }
48616
+ memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
48617
+ pPager->aSavepoint = aNew;
48618
+
48619
+ /* Populate the PagerSavepoint structures just allocated. */
48620
+ for(ii=nCurrent; ii<nSavepoint; ii++){
48621
+ aNew[ii].nOrig = pPager->dbSize;
48622
+ if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
48623
+ aNew[ii].iOffset = pPager->journalOff;
48624
+ }else{
48625
+ aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
48626
+ }
48627
+ aNew[ii].iSubRec = pPager->nSubRec;
48628
+ aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
48629
+ if( !aNew[ii].pInSavepoint ){
48630
+ return SQLITE_NOMEM;
48631
+ }
48632
+ if( pagerUseWal(pPager) ){
48633
+ sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
48634
+ }
48635
+ pPager->nSavepoint = ii+1;
48636
+ }
48637
+ assert( pPager->nSavepoint==nSavepoint );
48638
+ assertTruncateConstraint(pPager);
48639
+ return rc;
48640
+}
48641
+SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
48642
+ assert( pPager->eState>=PAGER_WRITER_LOCKED );
4814248643
assert( assert_pager_state(pPager) );
4814348644
48144
- if( nSavepoint>nCurrent && pPager->useJournal ){
48145
- int ii; /* Iterator variable */
48146
- PagerSavepoint *aNew; /* New Pager.aSavepoint array */
48147
-
48148
- /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
48149
- ** if the allocation fails. Otherwise, zero the new portion in case a
48150
- ** malloc failure occurs while populating it in the for(...) loop below.
48151
- */
48152
- aNew = (PagerSavepoint *)sqlite3Realloc(
48153
- pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
48154
- );
48155
- if( !aNew ){
48156
- return SQLITE_NOMEM;
48157
- }
48158
- memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
48159
- pPager->aSavepoint = aNew;
48160
-
48161
- /* Populate the PagerSavepoint structures just allocated. */
48162
- for(ii=nCurrent; ii<nSavepoint; ii++){
48163
- aNew[ii].nOrig = pPager->dbSize;
48164
- if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
48165
- aNew[ii].iOffset = pPager->journalOff;
48166
- }else{
48167
- aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
48168
- }
48169
- aNew[ii].iSubRec = pPager->nSubRec;
48170
- aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
48171
- if( !aNew[ii].pInSavepoint ){
48172
- return SQLITE_NOMEM;
48173
- }
48174
- if( pagerUseWal(pPager) ){
48175
- sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
48176
- }
48177
- pPager->nSavepoint = ii+1;
48178
- }
48179
- assert( pPager->nSavepoint==nSavepoint );
48180
- assertTruncateConstraint(pPager);
48181
- }
48182
-
48183
- return rc;
48184
-}
48645
+ if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
48646
+ return pagerOpenSavepoint(pPager, nSavepoint);
48647
+ }else{
48648
+ return SQLITE_OK;
48649
+ }
48650
+}
48651
+
4818548652
4818648653
/*
4818748654
** This function is called to rollback or release (commit) a savepoint.
4818848655
** The savepoint to release or rollback need not be the most recently
4818948656
** created savepoint.
@@ -48410,13 +48877,12 @@
4841048877
**
4841148878
** subjournalPage() may need to allocate space to store pPg->pgno into
4841248879
** one or more savepoint bitvecs. This is the reason this function
4841348880
** may return SQLITE_NOMEM.
4841448881
*/
48415
- if( pPg->flags&PGHDR_DIRTY
48416
- && subjRequiresPage(pPg)
48417
- && SQLITE_OK!=(rc = subjournalPage(pPg))
48882
+ if( (pPg->flags & PGHDR_DIRTY)!=0
48883
+ && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
4841848884
){
4841948885
return rc;
4842048886
}
4842148887
4842248888
PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
@@ -52330,10 +52796,11 @@
5233052796
#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
5233152797
5233252798
/* Forward declarations */
5233352799
typedef struct MemPage MemPage;
5233452800
typedef struct BtLock BtLock;
52801
+typedef struct CellInfo CellInfo;
5233552802
5233652803
/*
5233752804
** This is a magic string that appears at the beginning of every
5233852805
** SQLite database in order to identify the file as a real database.
5233952806
**
@@ -52393,11 +52860,14 @@
5239352860
u8 *apOvfl[5]; /* Pointers to the body of overflow cells */
5239452861
BtShared *pBt; /* Pointer to BtShared that this page is part of */
5239552862
u8 *aData; /* Pointer to disk image of the page data */
5239652863
u8 *aDataEnd; /* One byte past the end of usable data */
5239752864
u8 *aCellIdx; /* The cell index area */
52865
+ u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
5239852866
DbPage *pDbPage; /* Pager page handle */
52867
+ u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
52868
+ void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
5239952869
Pgno pgno; /* Page number for this page */
5240052870
};
5240152871
5240252872
/*
5240352873
** The in-memory image of a disk page has the auxiliary information appended
@@ -52449,10 +52919,11 @@
5244952919
sqlite3 *db; /* The database connection holding this btree */
5245052920
BtShared *pBt; /* Sharable content of this btree */
5245152921
u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
5245252922
u8 sharable; /* True if we can share pBt with another db */
5245352923
u8 locked; /* True if db currently has pBt locked */
52924
+ u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
5245452925
int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
5245552926
int nBackup; /* Number of backup operations reading this btree */
5245652927
u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
5245752928
Btree *pNext; /* List of other sharable Btrees from the same db */
5245852929
Btree *pPrev; /* Back pointer of the same list */
@@ -52559,11 +53030,10 @@
5255953030
/*
5256053031
** An instance of the following structure is used to hold information
5256153032
** about a cell. The parseCellPtr() function fills in this structure
5256253033
** based on information extract from the raw disk page.
5256353034
*/
52564
-typedef struct CellInfo CellInfo;
5256553035
struct CellInfo {
5256653036
i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
5256753037
u8 *pPayload; /* Pointer to the start of payload */
5256853038
u32 nPayload; /* Bytes of payload */
5256953039
u16 nLocal; /* Amount of payload held locally, not on overflow */
@@ -52602,24 +53072,30 @@
5260253072
** eState==FAULT: Cursor fault with skipNext as error code.
5260353073
*/
5260453074
struct BtCursor {
5260553075
Btree *pBtree; /* The Btree to which this cursor belongs */
5260653076
BtShared *pBt; /* The BtShared this cursor points to */
52607
- BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
52608
- struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
53077
+ BtCursor *pNext; /* Forms a linked list of all cursors */
5260953078
Pgno *aOverflow; /* Cache of overflow page locations */
5261053079
CellInfo info; /* A parse of the cell we are pointing at */
5261153080
i64 nKey; /* Size of pKey, or last integer key */
5261253081
void *pKey; /* Saved key that was cursor last known position */
5261353082
Pgno pgnoRoot; /* The root page of this tree */
5261453083
int nOvflAlloc; /* Allocated size of aOverflow[] array */
5261553084
int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
5261653085
** Error code if eState==CURSOR_FAULT */
5261753086
u8 curFlags; /* zero or more BTCF_* flags defined below */
53087
+ u8 curPagerFlags; /* Flags to send to sqlite3PagerAcquire() */
5261853088
u8 eState; /* One of the CURSOR_XXX constants (see below) */
52619
- u8 hints; /* As configured by CursorSetHints() */
52620
- i16 iPage; /* Index of current page in apPage */
53089
+ u8 hints; /* As configured by CursorSetHints() */
53090
+ /* All fields above are zeroed when the cursor is allocated. See
53091
+ ** sqlite3BtreeCursorZero(). Fields that follow must be manually
53092
+ ** initialized. */
53093
+ i8 iPage; /* Index of current page in apPage */
53094
+ u8 curIntKey; /* Value of apPage[0]->intKey */
53095
+ struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
53096
+ void *padding1; /* Make object size a multiple of 16 */
5262153097
u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */
5262253098
MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
5262353099
};
5262453100
5262553101
/*
@@ -52628,10 +53104,11 @@
5262853104
#define BTCF_WriteFlag 0x01 /* True if a write cursor */
5262953105
#define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
5263053106
#define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
5263153107
#define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */
5263253108
#define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
53109
+#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */
5263353110
5263453111
/*
5263553112
** Potential values for BtCursor.eState.
5263653113
**
5263753114
** CURSOR_INVALID:
@@ -52780,10 +53257,23 @@
5278053257
#define get2byte(x) ((x)[0]<<8 | (x)[1])
5278153258
#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
5278253259
#define get4byte sqlite3Get4byte
5278353260
#define put4byte sqlite3Put4byte
5278453261
53262
+/*
53263
+** get2byteAligned(), unlike get2byte(), requires that its argument point to a
53264
+** two-byte aligned address. get2bytea() is only used for accessing the
53265
+** cell addresses in a btree header.
53266
+*/
53267
+#if SQLITE_BYTEORDER==4321
53268
+# define get2byteAligned(x) (*(u16*)(x))
53269
+#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
53270
+# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
53271
+#else
53272
+# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
53273
+#endif
53274
+
5278553275
/************** End of btreeInt.h ********************************************/
5278653276
/************** Continuing where we left off in btmutex.c ********************/
5278753277
#ifndef SQLITE_OMIT_SHARED_CACHE
5278853278
#if SQLITE_THREADSAFE
5278953279
@@ -53559,17 +54049,19 @@
5355954049
Btree *pBtree, /* The database file to check */
5356054050
i64 iRow, /* The rowid that might be changing */
5356154051
int isClearTable /* True if all rows are being deleted */
5356254052
){
5356354053
BtCursor *p;
53564
- BtShared *pBt = pBtree->pBt;
54054
+ if( pBtree->hasIncrblobCur==0 ) return;
5356554055
assert( sqlite3BtreeHoldsMutex(pBtree) );
53566
- for(p=pBt->pCursor; p; p=p->pNext){
53567
- if( (p->curFlags & BTCF_Incrblob)!=0
53568
- && (isClearTable || p->info.nKey==iRow)
53569
- ){
53570
- p->eState = CURSOR_INVALID;
54056
+ pBtree->hasIncrblobCur = 0;
54057
+ for(p=pBtree->pBt->pCursor; p; p=p->pNext){
54058
+ if( (p->curFlags & BTCF_Incrblob)!=0 ){
54059
+ pBtree->hasIncrblobCur = 1;
54060
+ if( isClearTable || p->info.nKey==iRow ){
54061
+ p->eState = CURSOR_INVALID;
54062
+ }
5357154063
}
5357254064
}
5357354065
}
5357454066
5357554067
#else
@@ -53687,11 +54179,11 @@
5368754179
** stores the integer key in pCur->nKey. In this case this value is
5368854180
** all that is required. Otherwise, if pCur is not open on an intKey
5368954181
** table, then malloc space for and store the pCur->nKey bytes of key
5369054182
** data.
5369154183
*/
53692
- if( 0==pCur->apPage[0]->intKey ){
54184
+ if( 0==pCur->curIntKey ){
5369354185
void *pKey = sqlite3Malloc( pCur->nKey );
5369454186
if( pKey ){
5369554187
rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
5369654188
if( rc==SQLITE_OK ){
5369754189
pCur->pKey = pKey;
@@ -53700,11 +54192,11 @@
5370054192
}
5370154193
}else{
5370254194
rc = SQLITE_NOMEM;
5370354195
}
5370454196
}
53705
- assert( !pCur->apPage[0]->intKey || !pCur->pKey );
54197
+ assert( !pCur->curIntKey || !pCur->pKey );
5370654198
5370754199
if( rc==SQLITE_OK ){
5370854200
btreeReleaseAllCursorPages(pCur);
5370954201
pCur->eState = CURSOR_REQUIRESEEK;
5371054202
}
@@ -53721,10 +54213,19 @@
5372154213
** the table with root-page iRoot. "Saving the cursor position" means that
5372254214
** the location in the btree is remembered in such a way that it can be
5372354215
** moved back to the same spot after the btree has been modified. This
5372454216
** routine is called just before cursor pExcept is used to modify the
5372554217
** table, for example in BtreeDelete() or BtreeInsert().
54218
+**
54219
+** If there are two or more cursors on the same btree, then all such
54220
+** cursors should have their BTCF_Multiple flag set. The btreeCursor()
54221
+** routine enforces that rule. This routine only needs to be called in
54222
+** the uncommon case when pExpect has the BTCF_Multiple flag set.
54223
+**
54224
+** If pExpect!=NULL and if no other cursors are found on the same root-page,
54225
+** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
54226
+** pointless call to this routine.
5372654227
**
5372754228
** Implementation note: This routine merely checks to see if any cursors
5372854229
** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
5372954230
** event that cursors are in need to being saved.
5373054231
*/
@@ -53733,11 +54234,13 @@
5373354234
assert( sqlite3_mutex_held(pBt->mutex) );
5373454235
assert( pExcept==0 || pExcept->pBt==pBt );
5373554236
for(p=pBt->pCursor; p; p=p->pNext){
5373654237
if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
5373754238
}
53738
- return p ? saveCursorsOnList(p, iRoot, pExcept) : SQLITE_OK;
54239
+ if( p ) return saveCursorsOnList(p, iRoot, pExcept);
54240
+ if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
54241
+ return SQLITE_OK;
5373954242
}
5374054243
5374154244
/* This helper routine to saveAllCursors does the actual work of saving
5374254245
** the cursors if and when a cursor is found that actually requires saving.
5374354246
** The common case is that no cursors need to be saved, so this routine is
@@ -54020,71 +54523,184 @@
5402054523
5402154524
/*
5402254525
** Given a btree page and a cell index (0 means the first cell on
5402354526
** the page, 1 means the second cell, and so forth) return a pointer
5402454527
** to the cell content.
54528
+**
54529
+** findCellPastPtr() does the same except it skips past the initial
54530
+** 4-byte child pointer found on interior pages, if there is one.
5402554531
**
5402654532
** This routine works only for pages that do not contain overflow cells.
5402754533
*/
5402854534
#define findCell(P,I) \
54029
- ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
54030
-#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
54535
+ ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
54536
+#define findCellPastPtr(P,I) \
54537
+ ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
5403154538
5403254539
5403354540
/*
54034
-** This a more complex version of findCell() that works for
54035
-** pages that do contain overflow cells.
54541
+** This is common tail processing for btreeParseCellPtr() and
54542
+** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
54543
+** on a single B-tree page. Make necessary adjustments to the CellInfo
54544
+** structure.
5403654545
*/
54037
-static u8 *findOverflowCell(MemPage *pPage, int iCell){
54038
- int i;
54546
+static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
54547
+ MemPage *pPage, /* Page containing the cell */
54548
+ u8 *pCell, /* Pointer to the cell text. */
54549
+ CellInfo *pInfo /* Fill in this structure */
54550
+){
54551
+ /* If the payload will not fit completely on the local page, we have
54552
+ ** to decide how much to store locally and how much to spill onto
54553
+ ** overflow pages. The strategy is to minimize the amount of unused
54554
+ ** space on overflow pages while keeping the amount of local storage
54555
+ ** in between minLocal and maxLocal.
54556
+ **
54557
+ ** Warning: changing the way overflow payload is distributed in any
54558
+ ** way will result in an incompatible file format.
54559
+ */
54560
+ int minLocal; /* Minimum amount of payload held locally */
54561
+ int maxLocal; /* Maximum amount of payload held locally */
54562
+ int surplus; /* Overflow payload available for local storage */
54563
+
54564
+ minLocal = pPage->minLocal;
54565
+ maxLocal = pPage->maxLocal;
54566
+ surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
54567
+ testcase( surplus==maxLocal );
54568
+ testcase( surplus==maxLocal+1 );
54569
+ if( surplus <= maxLocal ){
54570
+ pInfo->nLocal = (u16)surplus;
54571
+ }else{
54572
+ pInfo->nLocal = (u16)minLocal;
54573
+ }
54574
+ pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
54575
+ pInfo->nSize = pInfo->iOverflow + 4;
54576
+}
54577
+
54578
+/*
54579
+** The following routines are implementations of the MemPage.xParseCell()
54580
+** method.
54581
+**
54582
+** Parse a cell content block and fill in the CellInfo structure.
54583
+**
54584
+** btreeParseCellPtr() => table btree leaf nodes
54585
+** btreeParseCellNoPayload() => table btree internal nodes
54586
+** btreeParseCellPtrIndex() => index btree nodes
54587
+**
54588
+** There is also a wrapper function btreeParseCell() that works for
54589
+** all MemPage types and that references the cell by index rather than
54590
+** by pointer.
54591
+*/
54592
+static void btreeParseCellPtrNoPayload(
54593
+ MemPage *pPage, /* Page containing the cell */
54594
+ u8 *pCell, /* Pointer to the cell text. */
54595
+ CellInfo *pInfo /* Fill in this structure */
54596
+){
5403954597
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54040
- for(i=pPage->nOverflow-1; i>=0; i--){
54041
- int k;
54042
- k = pPage->aiOvfl[i];
54043
- if( k<=iCell ){
54044
- if( k==iCell ){
54045
- return pPage->apOvfl[i];
54046
- }
54047
- iCell--;
54048
- }
54049
- }
54050
- return findCell(pPage, iCell);
54051
-}
54052
-
54053
-/*
54054
-** Parse a cell content block and fill in the CellInfo structure. There
54055
-** are two versions of this function. btreeParseCell() takes a
54056
-** cell index as the second argument and btreeParseCellPtr()
54057
-** takes a pointer to the body of the cell as its second argument.
54058
-*/
54598
+ assert( pPage->leaf==0 );
54599
+ assert( pPage->noPayload );
54600
+ assert( pPage->childPtrSize==4 );
54601
+ pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
54602
+ pInfo->nPayload = 0;
54603
+ pInfo->nLocal = 0;
54604
+ pInfo->iOverflow = 0;
54605
+ pInfo->pPayload = 0;
54606
+ return;
54607
+}
5405954608
static void btreeParseCellPtr(
54609
+ MemPage *pPage, /* Page containing the cell */
54610
+ u8 *pCell, /* Pointer to the cell text. */
54611
+ CellInfo *pInfo /* Fill in this structure */
54612
+){
54613
+ u8 *pIter; /* For scanning through pCell */
54614
+ u32 nPayload; /* Number of bytes of cell payload */
54615
+ u64 iKey; /* Extracted Key value */
54616
+
54617
+ assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54618
+ assert( pPage->leaf==0 || pPage->leaf==1 );
54619
+ assert( pPage->intKeyLeaf || pPage->noPayload );
54620
+ assert( pPage->noPayload==0 );
54621
+ assert( pPage->intKeyLeaf );
54622
+ assert( pPage->childPtrSize==0 );
54623
+ pIter = pCell;
54624
+
54625
+ /* The next block of code is equivalent to:
54626
+ **
54627
+ ** pIter += getVarint32(pIter, nPayload);
54628
+ **
54629
+ ** The code is inlined to avoid a function call.
54630
+ */
54631
+ nPayload = *pIter;
54632
+ if( nPayload>=0x80 ){
54633
+ u8 *pEnd = &pIter[8];
54634
+ nPayload &= 0x7f;
54635
+ do{
54636
+ nPayload = (nPayload<<7) | (*++pIter & 0x7f);
54637
+ }while( (*pIter)>=0x80 && pIter<pEnd );
54638
+ }
54639
+ pIter++;
54640
+
54641
+ /* The next block of code is equivalent to:
54642
+ **
54643
+ ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
54644
+ **
54645
+ ** The code is inlined to avoid a function call.
54646
+ */
54647
+ iKey = *pIter;
54648
+ if( iKey>=0x80 ){
54649
+ u8 *pEnd = &pIter[7];
54650
+ iKey &= 0x7f;
54651
+ while(1){
54652
+ iKey = (iKey<<7) | (*++pIter & 0x7f);
54653
+ if( (*pIter)<0x80 ) break;
54654
+ if( pIter>=pEnd ){
54655
+ iKey = (iKey<<8) | *++pIter;
54656
+ break;
54657
+ }
54658
+ }
54659
+ }
54660
+ pIter++;
54661
+
54662
+ pInfo->nKey = *(i64*)&iKey;
54663
+ pInfo->nPayload = nPayload;
54664
+ pInfo->pPayload = pIter;
54665
+ testcase( nPayload==pPage->maxLocal );
54666
+ testcase( nPayload==pPage->maxLocal+1 );
54667
+ if( nPayload<=pPage->maxLocal ){
54668
+ /* This is the (easy) common case where the entire payload fits
54669
+ ** on the local page. No overflow is required.
54670
+ */
54671
+ pInfo->nSize = nPayload + (u16)(pIter - pCell);
54672
+ if( pInfo->nSize<4 ) pInfo->nSize = 4;
54673
+ pInfo->nLocal = (u16)nPayload;
54674
+ pInfo->iOverflow = 0;
54675
+ }else{
54676
+ btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
54677
+ }
54678
+}
54679
+static void btreeParseCellPtrIndex(
5406054680
MemPage *pPage, /* Page containing the cell */
5406154681
u8 *pCell, /* Pointer to the cell text. */
5406254682
CellInfo *pInfo /* Fill in this structure */
5406354683
){
5406454684
u8 *pIter; /* For scanning through pCell */
5406554685
u32 nPayload; /* Number of bytes of cell payload */
5406654686
5406754687
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5406854688
assert( pPage->leaf==0 || pPage->leaf==1 );
54069
- if( pPage->intKeyLeaf ){
54070
- assert( pPage->childPtrSize==0 );
54071
- pIter = pCell + getVarint32(pCell, nPayload);
54072
- pIter += getVarint(pIter, (u64*)&pInfo->nKey);
54073
- }else if( pPage->noPayload ){
54074
- assert( pPage->childPtrSize==4 );
54075
- pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
54076
- pInfo->nPayload = 0;
54077
- pInfo->nLocal = 0;
54078
- pInfo->iOverflow = 0;
54079
- pInfo->pPayload = 0;
54080
- return;
54081
- }else{
54082
- pIter = pCell + pPage->childPtrSize;
54083
- pIter += getVarint32(pIter, nPayload);
54084
- pInfo->nKey = nPayload;
54085
- }
54689
+ assert( pPage->intKeyLeaf==0 );
54690
+ assert( pPage->noPayload==0 );
54691
+ pIter = pCell + pPage->childPtrSize;
54692
+ nPayload = *pIter;
54693
+ if( nPayload>=0x80 ){
54694
+ u8 *pEnd = &pIter[8];
54695
+ nPayload &= 0x7f;
54696
+ do{
54697
+ nPayload = (nPayload<<7) | (*++pIter & 0x7f);
54698
+ }while( *(pIter)>=0x80 && pIter<pEnd );
54699
+ }
54700
+ pIter++;
54701
+ pInfo->nKey = nPayload;
5408654702
pInfo->nPayload = nPayload;
5408754703
pInfo->pPayload = pIter;
5408854704
testcase( nPayload==pPage->maxLocal );
5408954705
testcase( nPayload==pPage->maxLocal+1 );
5409054706
if( nPayload<=pPage->maxLocal ){
@@ -54094,50 +54710,32 @@
5409454710
pInfo->nSize = nPayload + (u16)(pIter - pCell);
5409554711
if( pInfo->nSize<4 ) pInfo->nSize = 4;
5409654712
pInfo->nLocal = (u16)nPayload;
5409754713
pInfo->iOverflow = 0;
5409854714
}else{
54099
- /* If the payload will not fit completely on the local page, we have
54100
- ** to decide how much to store locally and how much to spill onto
54101
- ** overflow pages. The strategy is to minimize the amount of unused
54102
- ** space on overflow pages while keeping the amount of local storage
54103
- ** in between minLocal and maxLocal.
54104
- **
54105
- ** Warning: changing the way overflow payload is distributed in any
54106
- ** way will result in an incompatible file format.
54107
- */
54108
- int minLocal; /* Minimum amount of payload held locally */
54109
- int maxLocal; /* Maximum amount of payload held locally */
54110
- int surplus; /* Overflow payload available for local storage */
54111
-
54112
- minLocal = pPage->minLocal;
54113
- maxLocal = pPage->maxLocal;
54114
- surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
54115
- testcase( surplus==maxLocal );
54116
- testcase( surplus==maxLocal+1 );
54117
- if( surplus <= maxLocal ){
54118
- pInfo->nLocal = (u16)surplus;
54119
- }else{
54120
- pInfo->nLocal = (u16)minLocal;
54121
- }
54122
- pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
54123
- pInfo->nSize = pInfo->iOverflow + 4;
54715
+ btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
5412454716
}
5412554717
}
5412654718
static void btreeParseCell(
5412754719
MemPage *pPage, /* Page containing the cell */
5412854720
int iCell, /* The cell index. First cell is 0 */
5412954721
CellInfo *pInfo /* Fill in this structure */
5413054722
){
54131
- btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo);
54723
+ pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
5413254724
}
5413354725
5413454726
/*
54727
+** The following routines are implementations of the MemPage.xCellSize
54728
+** method.
54729
+**
5413554730
** Compute the total number of bytes that a Cell needs in the cell
5413654731
** data area of the btree-page. The return number includes the cell
5413754732
** data header and the local payload, but not any overflow page or
5413854733
** the space used by the cell pointer.
54734
+**
54735
+** cellSizePtrNoPayload() => table internal nodes
54736
+** cellSizePtr() => all index nodes & table leaf nodes
5413954737
*/
5414054738
static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
5414154739
u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
5414254740
u8 *pEnd; /* End mark for a varint */
5414354741
u32 nSize; /* Size value to return */
@@ -54146,22 +54744,17 @@
5414654744
/* The value returned by this function should always be the same as
5414754745
** the (CellInfo.nSize) value found by doing a full parse of the
5414854746
** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
5414954747
** this function verifies that this invariant is not violated. */
5415054748
CellInfo debuginfo;
54151
- btreeParseCellPtr(pPage, pCell, &debuginfo);
54749
+ pPage->xParseCell(pPage, pCell, &debuginfo);
5415254750
#endif
5415354751
54154
- if( pPage->noPayload ){
54155
- pEnd = &pIter[9];
54156
- while( (*pIter++)&0x80 && pIter<pEnd );
54157
- assert( pPage->childPtrSize==4 );
54158
- return (u16)(pIter - pCell);
54159
- }
54752
+ assert( pPage->noPayload==0 );
5416054753
nSize = *pIter;
5416154754
if( nSize>=0x80 ){
54162
- pEnd = &pIter[9];
54755
+ pEnd = &pIter[8];
5416354756
nSize &= 0x7f;
5416454757
do{
5416554758
nSize = (nSize<<7) | (*++pIter & 0x7f);
5416654759
}while( *(pIter)>=0x80 && pIter<pEnd );
5416754760
}
@@ -54189,16 +54782,36 @@
5418954782
nSize += 4 + (u16)(pIter - pCell);
5419054783
}
5419154784
assert( nSize==debuginfo.nSize || CORRUPT_DB );
5419254785
return (u16)nSize;
5419354786
}
54787
+static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
54788
+ u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
54789
+ u8 *pEnd; /* End mark for a varint */
54790
+
54791
+#ifdef SQLITE_DEBUG
54792
+ /* The value returned by this function should always be the same as
54793
+ ** the (CellInfo.nSize) value found by doing a full parse of the
54794
+ ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
54795
+ ** this function verifies that this invariant is not violated. */
54796
+ CellInfo debuginfo;
54797
+ pPage->xParseCell(pPage, pCell, &debuginfo);
54798
+#endif
54799
+
54800
+ assert( pPage->childPtrSize==4 );
54801
+ pEnd = pIter + 9;
54802
+ while( (*pIter++)&0x80 && pIter<pEnd );
54803
+ assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
54804
+ return (u16)(pIter - pCell);
54805
+}
54806
+
5419454807
5419554808
#ifdef SQLITE_DEBUG
5419654809
/* This variation on cellSizePtr() is used inside of assert() statements
5419754810
** only. */
5419854811
static u16 cellSize(MemPage *pPage, int iCell){
54199
- return cellSizePtr(pPage, findCell(pPage, iCell));
54812
+ return pPage->xCellSize(pPage, findCell(pPage, iCell));
5420054813
}
5420154814
#endif
5420254815
5420354816
#ifndef SQLITE_OMIT_AUTOVACUUM
5420454817
/*
@@ -54208,11 +54821,11 @@
5420854821
*/
5420954822
static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
5421054823
CellInfo info;
5421154824
if( *pRC ) return;
5421254825
assert( pCell!=0 );
54213
- btreeParseCellPtr(pPage, pCell, &info);
54826
+ pPage->xParseCell(pPage, pCell, &info);
5421454827
if( info.iOverflow ){
5421554828
Pgno ovfl = get4byte(&pCell[info.iOverflow]);
5421654829
ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
5421754830
}
5421854831
}
@@ -54272,11 +54885,11 @@
5427254885
*/
5427354886
if( pc<iCellFirst || pc>iCellLast ){
5427454887
return SQLITE_CORRUPT_BKPT;
5427554888
}
5427654889
assert( pc>=iCellFirst && pc<=iCellLast );
54277
- size = cellSizePtr(pPage, &src[pc]);
54890
+ size = pPage->xCellSize(pPage, &src[pc]);
5427854891
cbrk -= size;
5427954892
if( cbrk<iCellFirst || pc+size>usableSize ){
5428054893
return SQLITE_CORRUPT_BKPT;
5428154894
}
5428254895
assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
@@ -54314,22 +54927,24 @@
5431454927
** If no suitable space can be found on the free-list, return NULL.
5431554928
**
5431654929
** This function may detect corruption within pPg. If corruption is
5431754930
** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
5431854931
**
54319
-** If a slot of at least nByte bytes is found but cannot be used because
54320
-** there are already at least 60 fragmented bytes on the page, return NULL.
54321
-** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true.
54932
+** Slots on the free list that are between 1 and 3 bytes larger than nByte
54933
+** will be ignored if adding the extra space to the fragmentation count
54934
+** causes the fragmentation count to exceed 60.
5432254935
*/
54323
-static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){
54936
+static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
5432454937
const int hdr = pPg->hdrOffset;
5432554938
u8 * const aData = pPg->aData;
54326
- int iAddr;
54327
- int pc;
54939
+ int iAddr = hdr + 1;
54940
+ int pc = get2byte(&aData[iAddr]);
54941
+ int x;
5432854942
int usableSize = pPg->pBt->usableSize;
5432954943
54330
- for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){
54944
+ assert( pc>0 );
54945
+ do{
5433154946
int size; /* Size of the free slot */
5433254947
/* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
5433354948
** increasing offset. */
5433454949
if( pc>usableSize-4 || pc<iAddr+4 ){
5433554950
*pRc = SQLITE_CORRUPT_BKPT;
@@ -54337,36 +54952,35 @@
5433754952
}
5433854953
/* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
5433954954
** freeblock form a big-endian integer which is the size of the freeblock
5434054955
** in bytes, including the 4-byte header. */
5434154956
size = get2byte(&aData[pc+2]);
54342
- if( size>=nByte ){
54343
- int x = size - nByte;
54957
+ if( (x = size - nByte)>=0 ){
5434454958
testcase( x==4 );
5434554959
testcase( x==3 );
54346
- if( x<4 ){
54960
+ if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
54961
+ *pRc = SQLITE_CORRUPT_BKPT;
54962
+ return 0;
54963
+ }else if( x<4 ){
5434754964
/* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
5434854965
** number of bytes in fragments may not exceed 60. */
54349
- if( aData[hdr+7]>=60 ){
54350
- if( pbDefrag ) *pbDefrag = 1;
54351
- return 0;
54352
- }
54966
+ if( aData[hdr+7]>57 ) return 0;
54967
+
5435354968
/* Remove the slot from the free-list. Update the number of
5435454969
** fragmented bytes within the page. */
5435554970
memcpy(&aData[iAddr], &aData[pc], 2);
5435654971
aData[hdr+7] += (u8)x;
54357
- }else if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
54358
- *pRc = SQLITE_CORRUPT_BKPT;
54359
- return 0;
5436054972
}else{
5436154973
/* The slot remains on the free-list. Reduce its size to account
5436254974
** for the portion used by the new allocation. */
5436354975
put2byte(&aData[pc+2], x);
5436454976
}
5436554977
return &aData[pc + x];
5436654978
}
54367
- }
54979
+ iAddr = pc;
54980
+ pc = get2byte(&aData[pc]);
54981
+ }while( pc );
5436854982
5436954983
return 0;
5437054984
}
5437154985
5437254986
/*
@@ -54403,42 +55017,43 @@
5440355017
/* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
5440455018
** and the reserved space is zero (the usual value for reserved space)
5440555019
** then the cell content offset of an empty page wants to be 65536.
5440655020
** However, that integer is too large to be stored in a 2-byte unsigned
5440755021
** integer, so a value of 0 is used in its place. */
54408
- top = get2byteNotZero(&data[hdr+5]);
54409
- if( gap>top || NEVER((u32)top>pPage->pBt->usableSize) ){
54410
- /* The NEVER() is because a oversize "top" value will be blocked from
54411
- ** reaching this point by btreeInitPage() or btreeGetUnusedPage() */
54412
- return SQLITE_CORRUPT_BKPT;
55022
+ top = get2byte(&data[hdr+5]);
55023
+ assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
55024
+ if( gap>top ){
55025
+ if( top==0 && pPage->pBt->usableSize==65536 ){
55026
+ top = 65536;
55027
+ }else{
55028
+ return SQLITE_CORRUPT_BKPT;
55029
+ }
5441355030
}
5441455031
5441555032
/* If there is enough space between gap and top for one more cell pointer
5441655033
** array entry offset, and if the freelist is not empty, then search the
5441755034
** freelist looking for a free slot big enough to satisfy the request.
5441855035
*/
5441955036
testcase( gap+2==top );
5442055037
testcase( gap+1==top );
5442155038
testcase( gap==top );
54422
- if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){
54423
- int bDefrag = 0;
54424
- u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag);
54425
- if( rc ) return rc;
54426
- if( bDefrag ) goto defragment_page;
55039
+ if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
55040
+ u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
5442755041
if( pSpace ){
5442855042
assert( pSpace>=data && (pSpace - data)<65536 );
5442955043
*pIdx = (int)(pSpace - data);
5443055044
return SQLITE_OK;
55045
+ }else if( rc ){
55046
+ return rc;
5443155047
}
5443255048
}
5443355049
5443455050
/* The request could not be fulfilled using a freelist slot. Check
5443555051
** to see if defragmentation is necessary.
5443655052
*/
5443755053
testcase( gap+2+nByte==top );
5443855054
if( gap+2+nByte>top ){
54439
- defragment_page:
5444055055
assert( pPage->nCell>0 || CORRUPT_DB );
5444155056
rc = defragmentPage(pPage);
5444255057
if( rc ) return rc;
5444355058
top = get2byteNotZero(&data[hdr+5]);
5444455059
assert( gap+nByte<=top );
@@ -54510,18 +55125,19 @@
5451055125
if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
5451155126
assert( iFreeBlk>iPtr || iFreeBlk==0 );
5451255127
5451355128
/* At this point:
5451455129
** iFreeBlk: First freeblock after iStart, or zero if none
54515
- ** iPtr: The address of a pointer iFreeBlk
55130
+ ** iPtr: The address of a pointer to iFreeBlk
5451655131
**
5451755132
** Check to see if iFreeBlk should be coalesced onto the end of iStart.
5451855133
*/
5451955134
if( iFreeBlk && iEnd+3>=iFreeBlk ){
5452055135
nFrag = iFreeBlk - iEnd;
5452155136
if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
5452255137
iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
55138
+ if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT;
5452355139
iSize = iEnd - iStart;
5452455140
iFreeBlk = get2byte(&data[iFreeBlk]);
5452555141
}
5452655142
5452755143
/* If iPtr is another freeblock (that is, if iPtr is not the freelist
@@ -54575,21 +55191,30 @@
5457555191
assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
5457655192
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5457755193
pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
5457855194
flagByte &= ~PTF_LEAF;
5457955195
pPage->childPtrSize = 4-4*pPage->leaf;
55196
+ pPage->xCellSize = cellSizePtr;
5458055197
pBt = pPage->pBt;
5458155198
if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
5458255199
/* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
5458355200
** table b-tree page. */
5458455201
assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
5458555202
/* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
5458655203
** table b-tree page. */
5458755204
assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
5458855205
pPage->intKey = 1;
54589
- pPage->intKeyLeaf = pPage->leaf;
54590
- pPage->noPayload = !pPage->leaf;
55206
+ if( pPage->leaf ){
55207
+ pPage->intKeyLeaf = 1;
55208
+ pPage->noPayload = 0;
55209
+ pPage->xParseCell = btreeParseCellPtr;
55210
+ }else{
55211
+ pPage->intKeyLeaf = 0;
55212
+ pPage->noPayload = 1;
55213
+ pPage->xCellSize = cellSizePtrNoPayload;
55214
+ pPage->xParseCell = btreeParseCellPtrNoPayload;
55215
+ }
5459155216
pPage->maxLocal = pBt->maxLeaf;
5459255217
pPage->minLocal = pBt->minLeaf;
5459355218
}else if( flagByte==PTF_ZERODATA ){
5459455219
/* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
5459555220
** index b-tree page. */
@@ -54598,10 +55223,11 @@
5459855223
** index b-tree page. */
5459955224
assert( (PTF_ZERODATA|PTF_LEAF)==10 );
5460055225
pPage->intKey = 0;
5460155226
pPage->intKeyLeaf = 0;
5460255227
pPage->noPayload = 0;
55228
+ pPage->xParseCell = btreeParseCellPtrIndex;
5460355229
pPage->maxLocal = pBt->maxLocal;
5460455230
pPage->minLocal = pBt->minLocal;
5460555231
}else{
5460655232
/* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
5460755233
** an error. */
@@ -54653,10 +55279,11 @@
5465355279
pPage->nOverflow = 0;
5465455280
usableSize = pBt->usableSize;
5465555281
pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
5465655282
pPage->aDataEnd = &data[usableSize];
5465755283
pPage->aCellIdx = &data[cellOffset];
55284
+ pPage->aDataOfst = &data[pPage->childPtrSize];
5465855285
/* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
5465955286
** the start of the cell content area. A zero value for this integer is
5466055287
** interpreted as 65536. */
5466155288
top = get2byteNotZero(&data[hdr+5]);
5466255289
/* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
@@ -54686,17 +55313,17 @@
5468655313
int i; /* Index into the cell pointer array */
5468755314
int sz; /* Size of a cell */
5468855315
5468955316
if( !pPage->leaf ) iCellLast--;
5469055317
for(i=0; i<pPage->nCell; i++){
54691
- pc = get2byte(&data[cellOffset+i*2]);
55318
+ pc = get2byteAligned(&data[cellOffset+i*2]);
5469255319
testcase( pc==iCellFirst );
5469355320
testcase( pc==iCellLast );
5469455321
if( pc<iCellFirst || pc>iCellLast ){
5469555322
return SQLITE_CORRUPT_BKPT;
5469655323
}
54697
- sz = cellSizePtr(pPage, &data[pc]);
55324
+ sz = pPage->xCellSize(pPage, &data[pc]);
5469855325
testcase( pc+sz==usableSize );
5469955326
if( pc+sz>usableSize ){
5470055327
return SQLITE_CORRUPT_BKPT;
5470155328
}
5470255329
}
@@ -54772,10 +55399,11 @@
5477255399
pPage->nFree = (u16)(pBt->usableSize - first);
5477355400
decodeFlags(pPage, flags);
5477455401
pPage->cellOffset = first;
5477555402
pPage->aDataEnd = &data[pBt->usableSize];
5477655403
pPage->aCellIdx = &data[first];
55404
+ pPage->aDataOfst = &data[pPage->childPtrSize];
5477755405
pPage->nOverflow = 0;
5477855406
assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
5477955407
pPage->maskPage = (u16)(pBt->pageSize - 1);
5478055408
pPage->nCell = 0;
5478155409
pPage->isInit = 1;
@@ -54790,11 +55418,11 @@
5479055418
MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
5479155419
pPage->aData = sqlite3PagerGetData(pDbPage);
5479255420
pPage->pDbPage = pDbPage;
5479355421
pPage->pBt = pBt;
5479455422
pPage->pgno = pgno;
54795
- pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
55423
+ pPage->hdrOffset = pgno==1 ? 100 : 0;
5479655424
return pPage;
5479755425
}
5479855426
5479955427
/*
5480055428
** Get a page from the pager. Initialize the MemPage.pBt and
@@ -54851,58 +55479,86 @@
5485155479
assert( ((p->pBt->nPage)&0x8000000)==0 );
5485255480
return btreePagecount(p->pBt);
5485355481
}
5485455482
5485555483
/*
54856
-** Get a page from the pager and initialize it. This routine is just a
54857
-** convenience wrapper around separate calls to btreeGetPage() and
54858
-** btreeInitPage().
55484
+** Get a page from the pager and initialize it.
5485955485
**
54860
-** If an error occurs, then the value *ppPage is set to is undefined. It
55486
+** If pCur!=0 then the page is being fetched as part of a moveToChild()
55487
+** call. Do additional sanity checking on the page in this case.
55488
+** And if the fetch fails, this routine must decrement pCur->iPage.
55489
+**
55490
+** The page is fetched as read-write unless pCur is not NULL and is
55491
+** a read-only cursor.
55492
+**
55493
+** If an error occurs, then *ppPage is undefined. It
5486155494
** may remain unchanged, or it may be set to an invalid value.
5486255495
*/
5486355496
static int getAndInitPage(
5486455497
BtShared *pBt, /* The database file */
5486555498
Pgno pgno, /* Number of the page to get */
5486655499
MemPage **ppPage, /* Write the page pointer here */
54867
- int bReadonly /* PAGER_GET_READONLY or 0 */
55500
+ BtCursor *pCur, /* Cursor to receive the page, or NULL */
55501
+ int bReadOnly /* True for a read-only page */
5486855502
){
5486955503
int rc;
55504
+ DbPage *pDbPage;
5487055505
assert( sqlite3_mutex_held(pBt->mutex) );
54871
- assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
55506
+ assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] );
55507
+ assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
55508
+ assert( pCur==0 || pCur->iPage>0 );
5487255509
5487355510
if( pgno>btreePagecount(pBt) ){
5487455511
rc = SQLITE_CORRUPT_BKPT;
54875
- }else{
54876
- rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
54877
- if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
54878
- rc = btreeInitPage(*ppPage);
54879
- if( rc!=SQLITE_OK ){
54880
- releasePage(*ppPage);
54881
- }
55512
+ goto getAndInitPage_error;
55513
+ }
55514
+ rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
55515
+ if( rc ){
55516
+ goto getAndInitPage_error;
55517
+ }
55518
+ *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
55519
+ if( (*ppPage)->isInit==0 ){
55520
+ rc = btreeInitPage(*ppPage);
55521
+ if( rc!=SQLITE_OK ){
55522
+ releasePage(*ppPage);
55523
+ goto getAndInitPage_error;
5488255524
}
5488355525
}
5488455526
55527
+ /* If obtaining a child page for a cursor, we must verify that the page is
55528
+ ** compatible with the root page. */
55529
+ if( pCur
55530
+ && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey)
55531
+ ){
55532
+ rc = SQLITE_CORRUPT_BKPT;
55533
+ releasePage(*ppPage);
55534
+ goto getAndInitPage_error;
55535
+ }
55536
+ return SQLITE_OK;
55537
+
55538
+getAndInitPage_error:
55539
+ if( pCur ) pCur->iPage--;
5488555540
testcase( pgno==0 );
5488655541
assert( pgno!=0 || rc==SQLITE_CORRUPT );
5488755542
return rc;
5488855543
}
5488955544
5489055545
/*
5489155546
** Release a MemPage. This should be called once for each prior
5489255547
** call to btreeGetPage.
5489355548
*/
55549
+static void releasePageNotNull(MemPage *pPage){
55550
+ assert( pPage->aData );
55551
+ assert( pPage->pBt );
55552
+ assert( pPage->pDbPage!=0 );
55553
+ assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
55554
+ assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
55555
+ assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55556
+ sqlite3PagerUnrefNotNull(pPage->pDbPage);
55557
+}
5489455558
static void releasePage(MemPage *pPage){
54895
- if( pPage ){
54896
- assert( pPage->aData );
54897
- assert( pPage->pBt );
54898
- assert( pPage->pDbPage!=0 );
54899
- assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
54900
- assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
54901
- assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54902
- sqlite3PagerUnrefNotNull(pPage->pDbPage);
54903
- }
55559
+ if( pPage ) releasePageNotNull(pPage);
5490455560
}
5490555561
5490655562
/*
5490755563
** Get an unused page.
5490855564
**
@@ -55873,11 +56529,11 @@
5587356529
if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
5587456530
MemPage *pPage1 = pBt->pPage1;
5587556531
assert( pPage1->aData );
5587656532
assert( sqlite3PagerRefcount(pBt->pPager)==1 );
5587756533
pBt->pPage1 = 0;
55878
- releasePage(pPage1);
56534
+ releasePageNotNull(pPage1);
5587956535
}
5588056536
}
5588156537
5588256538
/*
5588356539
** If pBt points to an empty file then convert that empty file
@@ -56188,11 +56844,11 @@
5618856844
5618956845
for(i=0; i<nCell; i++){
5619056846
u8 *pCell = findCell(pPage, i);
5619156847
if( eType==PTRMAP_OVERFLOW1 ){
5619256848
CellInfo info;
56193
- btreeParseCellPtr(pPage, pCell, &info);
56849
+ pPage->xParseCell(pPage, pCell, &info);
5619456850
if( info.iOverflow
5619556851
&& pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
5619656852
&& iFrom==get4byte(&pCell[info.iOverflow])
5619756853
){
5619856854
put4byte(&pCell[info.iOverflow], iTo);
@@ -56929,10 +57585,11 @@
5692957585
int wrFlag, /* 1 to write. 0 read-only */
5693057586
struct KeyInfo *pKeyInfo, /* First arg to comparison function */
5693157587
BtCursor *pCur /* Space for new cursor */
5693257588
){
5693357589
BtShared *pBt = p->pBt; /* Shared b-tree handle */
57590
+ BtCursor *pX; /* Looping over other all cursors */
5693457591
5693557592
assert( sqlite3BtreeHoldsMutex(p) );
5693657593
assert( wrFlag==0 || wrFlag==1 );
5693757594
5693857595
/* The following assert statements verify that if this is a sharable
@@ -56944,14 +57601,12 @@
5694457601
5694557602
/* Assert that the caller has opened the required transaction. */
5694657603
assert( p->inTrans>TRANS_NONE );
5694757604
assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
5694857605
assert( pBt->pPage1 && pBt->pPage1->aData );
57606
+ assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
5694957607
56950
- if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
56951
- return SQLITE_READONLY;
56952
- }
5695357608
if( wrFlag ){
5695457609
allocateTempSpace(pBt);
5695557610
if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
5695657611
}
5695757612
if( iTable==1 && btreePagecount(pBt)==0 ){
@@ -56966,14 +57621,20 @@
5696657621
pCur->pKeyInfo = pKeyInfo;
5696757622
pCur->pBtree = p;
5696857623
pCur->pBt = pBt;
5696957624
assert( wrFlag==0 || wrFlag==BTCF_WriteFlag );
5697057625
pCur->curFlags = wrFlag;
57626
+ pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
57627
+ /* If there are two or more cursors on the same btree, then all such
57628
+ ** cursors *must* have the BTCF_Multiple flag set. */
57629
+ for(pX=pBt->pCursor; pX; pX=pX->pNext){
57630
+ if( pX->pgnoRoot==(Pgno)iTable ){
57631
+ pX->curFlags |= BTCF_Multiple;
57632
+ pCur->curFlags |= BTCF_Multiple;
57633
+ }
57634
+ }
5697157635
pCur->pNext = pBt->pCursor;
56972
- if( pCur->pNext ){
56973
- pCur->pNext->pPrev = pCur;
56974
- }
5697557636
pBt->pCursor = pCur;
5697657637
pCur->eState = CURSOR_INVALID;
5697757638
return SQLITE_OK;
5697857639
}
5697957640
SQLITE_PRIVATE int sqlite3BtreeCursor(
@@ -57027,17 +57688,22 @@
5702757688
if( pBtree ){
5702857689
int i;
5702957690
BtShared *pBt = pCur->pBt;
5703057691
sqlite3BtreeEnter(pBtree);
5703157692
sqlite3BtreeClearCursor(pCur);
57032
- if( pCur->pPrev ){
57033
- pCur->pPrev->pNext = pCur->pNext;
57034
- }else{
57693
+ assert( pBt->pCursor!=0 );
57694
+ if( pBt->pCursor==pCur ){
5703557695
pBt->pCursor = pCur->pNext;
57036
- }
57037
- if( pCur->pNext ){
57038
- pCur->pNext->pPrev = pCur->pPrev;
57696
+ }else{
57697
+ BtCursor *pPrev = pBt->pCursor;
57698
+ do{
57699
+ if( pPrev->pNext==pCur ){
57700
+ pPrev->pNext = pCur->pNext;
57701
+ break;
57702
+ }
57703
+ pPrev = pPrev->pNext;
57704
+ }while( ALWAYS(pPrev) );
5703957705
}
5704057706
for(i=0; i<=pCur->iPage; i++){
5704157707
releasePage(pCur->apPage[i]);
5704257708
}
5704357709
unlockBtreeIfUnused(pBt);
@@ -57053,17 +57719,10 @@
5705357719
** BtCursor.info structure. If it is not already valid, call
5705457720
** btreeParseCell() to fill it in.
5705557721
**
5705657722
** BtCursor.info is a cache of the information in the current cell.
5705757723
** Using this cache reduces the number of calls to btreeParseCell().
57058
-**
57059
-** 2007-06-25: There is a bug in some versions of MSVC that cause the
57060
-** compiler to crash when getCellInfo() is implemented as a macro.
57061
-** But there is a measureable speed advantage to using the macro on gcc
57062
-** (when less compiler optimizations like -Os or -O0 are used and the
57063
-** compiler is not doing aggressive inlining.) So we use a real function
57064
-** for MSVC and a macro for everything else. Ticket #2457.
5706557724
*/
5706657725
#ifndef NDEBUG
5706757726
static void assertCellInfo(BtCursor *pCur){
5706857727
CellInfo info;
5706957728
int iPage = pCur->iPage;
@@ -57072,32 +57731,19 @@
5707257731
assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
5707357732
}
5707457733
#else
5707557734
#define assertCellInfo(x)
5707657735
#endif
57077
-#ifdef _MSC_VER
57078
- /* Use a real function in MSVC to work around bugs in that compiler. */
57079
- static void getCellInfo(BtCursor *pCur){
57080
- if( pCur->info.nSize==0 ){
57081
- int iPage = pCur->iPage;
57082
- btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
57083
- pCur->curFlags |= BTCF_ValidNKey;
57084
- }else{
57085
- assertCellInfo(pCur);
57086
- }
57087
- }
57088
-#else /* if not _MSC_VER */
57089
- /* Use a macro in all other compilers so that the function is inlined */
57090
-#define getCellInfo(pCur) \
57091
- if( pCur->info.nSize==0 ){ \
57092
- int iPage = pCur->iPage; \
57093
- btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
57094
- pCur->curFlags |= BTCF_ValidNKey; \
57095
- }else{ \
57096
- assertCellInfo(pCur); \
57097
- }
57098
-#endif /* _MSC_VER */
57736
+static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
57737
+ if( pCur->info.nSize==0 ){
57738
+ int iPage = pCur->iPage;
57739
+ pCur->curFlags |= BTCF_ValidNKey;
57740
+ btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
57741
+ }else{
57742
+ assertCellInfo(pCur);
57743
+ }
57744
+}
5709957745
5710057746
#ifndef NDEBUG /* The next routine used only within assert() statements */
5710157747
/*
5710257748
** Return true if the given BtCursor is valid. A valid cursor is one
5710357749
** that is currently pointing to a row in a (non-empty) table.
@@ -57599,35 +58245,25 @@
5759958245
** the new child page does not match the flags field of the parent (i.e.
5760058246
** if an intkey page appears to be the parent of a non-intkey page, or
5760158247
** vice-versa).
5760258248
*/
5760358249
static int moveToChild(BtCursor *pCur, u32 newPgno){
57604
- int rc;
57605
- int i = pCur->iPage;
57606
- MemPage *pNewPage;
5760758250
BtShared *pBt = pCur->pBt;
5760858251
5760958252
assert( cursorHoldsMutex(pCur) );
5761058253
assert( pCur->eState==CURSOR_VALID );
5761158254
assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
5761258255
assert( pCur->iPage>=0 );
5761358256
if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
5761458257
return SQLITE_CORRUPT_BKPT;
5761558258
}
57616
- rc = getAndInitPage(pBt, newPgno, &pNewPage,
57617
- (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
57618
- if( rc ) return rc;
57619
- pCur->apPage[i+1] = pNewPage;
57620
- pCur->aiIdx[i+1] = 0;
57621
- pCur->iPage++;
57622
-
5762358259
pCur->info.nSize = 0;
5762458260
pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
57625
- if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
57626
- return SQLITE_CORRUPT_BKPT;
57627
- }
57628
- return SQLITE_OK;
58261
+ pCur->iPage++;
58262
+ pCur->aiIdx[pCur->iPage] = 0;
58263
+ return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
58264
+ pCur, pCur->curPagerFlags);
5762958265
}
5763058266
5763158267
#if SQLITE_DEBUG
5763258268
/*
5763358269
** Page pParent is an internal (non-leaf) tree page. This function
@@ -57667,15 +58303,13 @@
5766758303
pCur->apPage[pCur->iPage-1],
5766858304
pCur->aiIdx[pCur->iPage-1],
5766958305
pCur->apPage[pCur->iPage]->pgno
5767058306
);
5767158307
testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
57672
-
57673
- releasePage(pCur->apPage[pCur->iPage]);
57674
- pCur->iPage--;
5767558308
pCur->info.nSize = 0;
5767658309
pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
58310
+ releasePageNotNull(pCur->apPage[pCur->iPage--]);
5767758311
}
5767858312
5767958313
/*
5768058314
** Move the cursor to point to the root page of its b-tree structure.
5768158315
**
@@ -57712,22 +58346,27 @@
5771258346
}
5771358347
sqlite3BtreeClearCursor(pCur);
5771458348
}
5771558349
5771658350
if( pCur->iPage>=0 ){
57717
- while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
58351
+ while( pCur->iPage ){
58352
+ assert( pCur->apPage[pCur->iPage]!=0 );
58353
+ releasePageNotNull(pCur->apPage[pCur->iPage--]);
58354
+ }
5771858355
}else if( pCur->pgnoRoot==0 ){
5771958356
pCur->eState = CURSOR_INVALID;
5772058357
return SQLITE_OK;
5772158358
}else{
58359
+ assert( pCur->iPage==(-1) );
5772258360
rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
57723
- (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
58361
+ 0, pCur->curPagerFlags);
5772458362
if( rc!=SQLITE_OK ){
5772558363
pCur->eState = CURSOR_INVALID;
5772658364
return rc;
5772758365
}
5772858366
pCur->iPage = 0;
58367
+ pCur->curIntKey = pCur->apPage[0]->intKey;
5772958368
}
5773058369
pRoot = pCur->apPage[0];
5773158370
assert( pRoot->pgno==pCur->pgnoRoot );
5773258371
5773358372
/* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
@@ -57926,11 +58565,11 @@
5792658565
assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
5792758566
5792858567
/* If the cursor is already positioned at the point we are trying
5792958568
** to move to, then just return without doing any work */
5793058569
if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
57931
- && pCur->apPage[0]->intKey
58570
+ && pCur->curIntKey
5793258571
){
5793358572
if( pCur->info.nKey==intKey ){
5793458573
*pRes = 0;
5793558574
return SQLITE_OK;
5793658575
}
@@ -57961,11 +58600,12 @@
5796158600
if( pCur->eState==CURSOR_INVALID ){
5796258601
*pRes = -1;
5796358602
assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5796458603
return SQLITE_OK;
5796558604
}
57966
- assert( pCur->apPage[0]->intKey || pIdxKey );
58605
+ assert( pCur->apPage[0]->intKey==pCur->curIntKey );
58606
+ assert( pCur->curIntKey || pIdxKey );
5796758607
for(;;){
5796858608
int lwr, upr, idx, c;
5796958609
Pgno chldPg;
5797058610
MemPage *pPage = pCur->apPage[pCur->iPage];
5797158611
u8 *pCell; /* Pointer to current cell in pPage */
@@ -57984,11 +58624,11 @@
5798458624
idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
5798558625
pCur->aiIdx[pCur->iPage] = (u16)idx;
5798658626
if( xRecordCompare==0 ){
5798758627
for(;;){
5798858628
i64 nCellKey;
57989
- pCell = findCell(pPage, idx) + pPage->childPtrSize;
58629
+ pCell = findCellPastPtr(pPage, idx);
5799058630
if( pPage->intKeyLeaf ){
5799158631
while( 0x80 <= *(pCell++) ){
5799258632
if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
5799358633
}
5799458634
}
@@ -58017,11 +58657,11 @@
5801758657
idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
5801858658
}
5801958659
}else{
5802058660
for(;;){
5802158661
int nCell; /* Size of the pCell cell in bytes */
58022
- pCell = findCell(pPage, idx) + pPage->childPtrSize;
58662
+ pCell = findCellPastPtr(pPage, idx);
5802358663
5802458664
/* The maximum supported page-size is 65536 bytes. This means that
5802558665
** the maximum number of record bytes stored on an index B-Tree
5802658666
** page is less than 16384 bytes and may be stored as a 2-byte
5802758667
** varint. This information is used to attempt to avoid parsing
@@ -58053,11 +58693,11 @@
5805358693
** up to two varints past the end of the buffer. An extra 18
5805458694
** bytes of padding is allocated at the end of the buffer in
5805558695
** case this happens. */
5805658696
void *pCellKey;
5805758697
u8 * const pCellBody = pCell - pPage->childPtrSize;
58058
- btreeParseCellPtr(pPage, pCellBody, &pCur->info);
58698
+ pPage->xParseCell(pPage, pCellBody, &pCur->info);
5805958699
nCell = (int)pCur->info.nKey;
5806058700
testcase( nCell<0 ); /* True if key size is 2^32 or more */
5806158701
testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
5806258702
testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
5806358703
testcase( nCell==2 ); /* Minimum legal index key size */
@@ -58356,12 +58996,11 @@
5835658996
** has already been called on the new page.) The new page has also
5835758997
** been referenced and the calling routine is responsible for calling
5835858998
** sqlite3PagerUnref() on the new page when it is done.
5835958999
**
5836059000
** SQLITE_OK is returned on success. Any other return value indicates
58361
-** an error. *ppPage and *pPgno are undefined in the event of an error.
58362
-** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
59001
+** an error. *ppPage is set to NULL in the event of an error.
5836359002
**
5836459003
** If the "nearby" parameter is not 0, then an effort is made to
5836559004
** locate a page close to the page number "nearby". This can be used in an
5836659005
** attempt to keep related pages close to each other in the database file,
5836759006
** which in turn can make database access faster.
@@ -58400,10 +59039,11 @@
5840059039
}
5840159040
if( n>0 ){
5840259041
/* There are pages on the freelist. Reuse one of those pages. */
5840359042
Pgno iTrunk;
5840459043
u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
59044
+ u32 nSearch = 0; /* Count of the number of search attempts */
5840559045
5840659046
/* If eMode==BTALLOC_EXACT and a query of the pointer-map
5840759047
** shows that the page 'nearby' is somewhere on the free-list, then
5840859048
** the entire-list will be searched for that page.
5840959049
*/
@@ -58448,11 +59088,11 @@
5844859088
** stores the page number of the first page of the freelist, or zero if
5844959089
** the freelist is empty. */
5845059090
iTrunk = get4byte(&pPage1->aData[32]);
5845159091
}
5845259092
testcase( iTrunk==mxPage );
58453
- if( iTrunk>mxPage ){
59093
+ if( iTrunk>mxPage || nSearch++ > n ){
5845459094
rc = SQLITE_CORRUPT_BKPT;
5845559095
}else{
5845659096
rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
5845759097
}
5845859098
if( rc ){
@@ -58601,10 +59241,11 @@
5860159241
rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
5860259242
if( rc==SQLITE_OK ){
5860359243
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5860459244
if( rc!=SQLITE_OK ){
5860559245
releasePage(*ppPage);
59246
+ *ppPage = 0;
5860659247
}
5860759248
}
5860859249
searchList = 0;
5860959250
}
5861059251
}
@@ -58842,11 +59483,11 @@
5884259483
int rc;
5884359484
int nOvfl;
5884459485
u32 ovflPageSize;
5884559486
5884659487
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
58847
- btreeParseCellPtr(pPage, pCell, &info);
59488
+ pPage->xParseCell(pPage, pCell, &info);
5884859489
*pnSize = info.nSize;
5884959490
if( info.iOverflow==0 ){
5885059491
return SQLITE_OK; /* No overflow pages. Return without doing anything */
5885159492
}
5885259493
if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
@@ -58954,13 +59595,11 @@
5895459595
if( pPage->intKey ){
5895559596
pSrc = pData;
5895659597
nSrc = nData;
5895759598
nData = 0;
5895859599
}else{
58959
- if( NEVER(nKey>0x7fffffff || pKey==0) ){
58960
- return SQLITE_CORRUPT_BKPT;
58961
- }
59600
+ assert( nKey<=0x7fffffff && pKey!=0 );
5896259601
nPayload = (int)nKey;
5896359602
pSrc = pKey;
5896459603
nSrc = (int)nKey;
5896559604
}
5896659605
if( nPayload<=pPage->maxLocal ){
@@ -58996,11 +59635,11 @@
5899659635
** were computed correctly.
5899759636
*/
5899859637
#if SQLITE_DEBUG
5899959638
{
5900059639
CellInfo info;
59001
- btreeParseCellPtr(pPage, pCell, &info);
59640
+ pPage->xParseCell(pPage, pCell, &info);
5900259641
assert( nHeader=(int)(info.pPayload - pCell) );
5900359642
assert( info.nKey==nKey );
5900459643
assert( *pnSize == info.nSize );
5900559644
assert( spaceLeft == info.nLocal );
5900659645
assert( pPrior == &pCell[info.iOverflow] );
@@ -59166,14 +59805,12 @@
5916659805
Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
5916759806
int *pRC /* Read and write return code from here */
5916859807
){
5916959808
int idx = 0; /* Where to write new cell content in data[] */
5917059809
int j; /* Loop counter */
59171
- int end; /* First byte past the last cell pointer in data[] */
59172
- int ins; /* Index in data[] where new cell pointer is inserted */
59173
- int cellOffset; /* Address of first cell pointer in data[] */
5917459810
u8 *data; /* The content of the whole page */
59811
+ u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
5917559812
5917659813
if( *pRC ) return;
5917759814
5917859815
assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
5917959816
assert( MX_CELL(pPage->pBt)<=10921 );
@@ -59184,11 +59821,11 @@
5918459821
/* The cell should normally be sized correctly. However, when moving a
5918559822
** malformed cell from a leaf page to an interior page, if the cell size
5918659823
** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
5918759824
** might be less than 8 (leaf-size + pointer) on the interior node. Hence
5918859825
** the term after the || in the following assert(). */
59189
- assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
59826
+ assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
5919059827
if( pPage->nOverflow || sz+2>pPage->nFree ){
5919159828
if( pTemp ){
5919259829
memcpy(pTemp, pCell, sz);
5919359830
pCell = pTemp;
5919459831
}
@@ -59197,36 +59834,46 @@
5919759834
}
5919859835
j = pPage->nOverflow++;
5919959836
assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
5920059837
pPage->apOvfl[j] = pCell;
5920159838
pPage->aiOvfl[j] = (u16)i;
59839
+
59840
+ /* When multiple overflows occur, they are always sequential and in
59841
+ ** sorted order. This invariants arise because multiple overflows can
59842
+ ** only occur when inserting divider cells into the parent page during
59843
+ ** balancing, and the dividers are adjacent and sorted.
59844
+ */
59845
+ assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
59846
+ assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
5920259847
}else{
5920359848
int rc = sqlite3PagerWrite(pPage->pDbPage);
5920459849
if( rc!=SQLITE_OK ){
5920559850
*pRC = rc;
5920659851
return;
5920759852
}
5920859853
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5920959854
data = pPage->aData;
59210
- cellOffset = pPage->cellOffset;
59211
- end = cellOffset + 2*pPage->nCell;
59212
- ins = cellOffset + 2*i;
59855
+ assert( &data[pPage->cellOffset]==pPage->aCellIdx );
5921359856
rc = allocateSpace(pPage, sz, &idx);
5921459857
if( rc ){ *pRC = rc; return; }
59215
- /* The allocateSpace() routine guarantees the following two properties
59216
- ** if it returns success */
59217
- assert( idx >= end+2 );
59858
+ /* The allocateSpace() routine guarantees the following properties
59859
+ ** if it returns successfully */
59860
+ assert( idx >= 0 );
59861
+ assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
5921859862
assert( idx+sz <= (int)pPage->pBt->usableSize );
59219
- pPage->nCell++;
5922059863
pPage->nFree -= (u16)(2 + sz);
5922159864
memcpy(&data[idx], pCell, sz);
5922259865
if( iChild ){
5922359866
put4byte(&data[idx], iChild);
5922459867
}
59225
- memmove(&data[ins+2], &data[ins], end-ins);
59226
- put2byte(&data[ins], idx);
59227
- put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
59868
+ pIns = pPage->aCellIdx + i*2;
59869
+ memmove(pIns+2, pIns, 2*(pPage->nCell - i));
59870
+ put2byte(pIns, idx);
59871
+ pPage->nCell++;
59872
+ /* increment the cell count */
59873
+ if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
59874
+ assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
5922859875
#ifndef SQLITE_OMIT_AUTOVACUUM
5922959876
if( pPage->pBt->autoVacuum ){
5923059877
/* The cell may contain a pointer to an overflow page. If so, write
5923159878
** the entry for the overflow page into the pointer map.
5923259879
*/
@@ -59233,10 +59880,56 @@
5923359880
ptrmapPutOvflPtr(pPage, pCell, pRC);
5923459881
}
5923559882
#endif
5923659883
}
5923759884
}
59885
+
59886
+/*
59887
+** A CellArray object contains a cache of pointers and sizes for a
59888
+** consecutive sequence of cells that might be held multiple pages.
59889
+*/
59890
+typedef struct CellArray CellArray;
59891
+struct CellArray {
59892
+ int nCell; /* Number of cells in apCell[] */
59893
+ MemPage *pRef; /* Reference page */
59894
+ u8 **apCell; /* All cells begin balanced */
59895
+ u16 *szCell; /* Local size of all cells in apCell[] */
59896
+};
59897
+
59898
+/*
59899
+** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
59900
+** computed.
59901
+*/
59902
+static void populateCellCache(CellArray *p, int idx, int N){
59903
+ assert( idx>=0 && idx+N<=p->nCell );
59904
+ while( N>0 ){
59905
+ assert( p->apCell[idx]!=0 );
59906
+ if( p->szCell[idx]==0 ){
59907
+ p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
59908
+ }else{
59909
+ assert( CORRUPT_DB ||
59910
+ p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
59911
+ }
59912
+ idx++;
59913
+ N--;
59914
+ }
59915
+}
59916
+
59917
+/*
59918
+** Return the size of the Nth element of the cell array
59919
+*/
59920
+static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
59921
+ assert( N>=0 && N<p->nCell );
59922
+ assert( p->szCell[N]==0 );
59923
+ p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
59924
+ return p->szCell[N];
59925
+}
59926
+static u16 cachedCellSize(CellArray *p, int N){
59927
+ assert( N>=0 && N<p->nCell );
59928
+ if( p->szCell[N] ) return p->szCell[N];
59929
+ return computeCellSize(p, N);
59930
+}
5923859931
5923959932
/*
5924059933
** Array apCell[] contains pointers to nCell b-tree page cells. The
5924159934
** szCell[] array contains the size in bytes of each cell. This function
5924259935
** replaces the current contents of page pPg with the contents of the cell
@@ -59247,11 +59940,11 @@
5924759940
** such cells before overwriting the page data.
5924859941
**
5924959942
** The MemPage.nFree field is invalidated by this function. It is the
5925059943
** responsibility of the caller to set it correctly.
5925159944
*/
59252
-static void rebuildPage(
59945
+static int rebuildPage(
5925359946
MemPage *pPg, /* Edit this page */
5925459947
int nCell, /* Final number of cells on page */
5925559948
u8 **apCell, /* Array of cells */
5925659949
u16 *szCell /* Array of cell sizes */
5925759950
){
@@ -59272,15 +59965,16 @@
5927259965
u8 *pCell = apCell[i];
5927359966
if( pCell>aData && pCell<pEnd ){
5927459967
pCell = &pTmp[pCell - aData];
5927559968
}
5927659969
pData -= szCell[i];
59277
- memcpy(pData, pCell, szCell[i]);
5927859970
put2byte(pCellptr, (pData - aData));
5927959971
pCellptr += 2;
59280
- assert( szCell[i]==cellSizePtr(pPg, pCell) || CORRUPT_DB );
59281
- testcase( szCell[i]==cellSizePtr(pPg,pCell) );
59972
+ if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
59973
+ memcpy(pData, pCell, szCell[i]);
59974
+ assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
59975
+ testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
5928259976
}
5928359977
5928459978
/* The pPg->nFree field is now set incorrectly. The caller will fix it. */
5928559979
pPg->nCell = nCell;
5928659980
pPg->nOverflow = 0;
@@ -59287,10 +59981,11 @@
5928759981
5928859982
put2byte(&aData[hdr+1], 0);
5928959983
put2byte(&aData[hdr+3], pPg->nCell);
5929059984
put2byte(&aData[hdr+5], pData - aData);
5929159985
aData[hdr+7] = 0x00;
59986
+ return SQLITE_OK;
5929259987
}
5929359988
5929459989
/*
5929559990
** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
5929659991
** contains the size in bytes of each such cell. This function attempts to
@@ -59319,29 +60014,29 @@
5931960014
static int pageInsertArray(
5932060015
MemPage *pPg, /* Page to add cells to */
5932160016
u8 *pBegin, /* End of cell-pointer array */
5932260017
u8 **ppData, /* IN/OUT: Page content -area pointer */
5932360018
u8 *pCellptr, /* Pointer to cell-pointer area */
60019
+ int iFirst, /* Index of first cell to add */
5932460020
int nCell, /* Number of cells to add to pPg */
59325
- u8 **apCell, /* Array of cells */
59326
- u16 *szCell /* Array of cell sizes */
60021
+ CellArray *pCArray /* Array of cells */
5932760022
){
5932860023
int i;
5932960024
u8 *aData = pPg->aData;
5933060025
u8 *pData = *ppData;
59331
- const int bFreelist = aData[1] || aData[2];
60026
+ int iEnd = iFirst + nCell;
5933260027
assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
59333
- for(i=0; i<nCell; i++){
59334
- int sz = szCell[i];
59335
- int rc;
60028
+ for(i=iFirst; i<iEnd; i++){
60029
+ int sz, rc;
5933660030
u8 *pSlot;
59337
- if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){
60031
+ sz = cachedCellSize(pCArray, i);
60032
+ if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
5933860033
pData -= sz;
5933960034
if( pData<pBegin ) return 1;
5934060035
pSlot = pData;
5934160036
}
59342
- memcpy(pSlot, apCell[i], sz);
60037
+ memcpy(pSlot, pCArray->apCell[i], sz);
5934360038
put2byte(pCellptr, (pSlot - aData));
5934460039
pCellptr += 2;
5934560040
}
5934660041
*ppData = pData;
5934760042
return 0;
@@ -59356,26 +60051,31 @@
5935660051
**
5935760052
** This function returns the total number of cells added to the free-list.
5935860053
*/
5935960054
static int pageFreeArray(
5936060055
MemPage *pPg, /* Page to edit */
60056
+ int iFirst, /* First cell to delete */
5936160057
int nCell, /* Cells to delete */
59362
- u8 **apCell, /* Array of cells */
59363
- u16 *szCell /* Array of cell sizes */
60058
+ CellArray *pCArray /* Array of cells */
5936460059
){
5936560060
u8 * const aData = pPg->aData;
5936660061
u8 * const pEnd = &aData[pPg->pBt->usableSize];
5936760062
u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
5936860063
int nRet = 0;
5936960064
int i;
60065
+ int iEnd = iFirst + nCell;
5937060066
u8 *pFree = 0;
5937160067
int szFree = 0;
5937260068
59373
- for(i=0; i<nCell; i++){
59374
- u8 *pCell = apCell[i];
60069
+ for(i=iFirst; i<iEnd; i++){
60070
+ u8 *pCell = pCArray->apCell[i];
5937560071
if( pCell>=pStart && pCell<pEnd ){
59376
- int sz = szCell[i];
60072
+ int sz;
60073
+ /* No need to use cachedCellSize() here. The sizes of all cells that
60074
+ ** are to be freed have already been computing while deciding which
60075
+ ** cells need freeing */
60076
+ sz = pCArray->szCell[i]; assert( sz>0 );
5937760077
if( pFree!=(pCell + sz) ){
5937860078
if( pFree ){
5937960079
assert( pFree>aData && (pFree - aData)<65536 );
5938060080
freeSpace(pPg, (u16)(pFree - aData), szFree);
5938160081
}
@@ -59406,17 +60106,16 @@
5940660106
** the correct cells after being balanced.
5940760107
**
5940860108
** The pPg->nFree field is invalid when this function returns. It is the
5940960109
** responsibility of the caller to set it correctly.
5941060110
*/
59411
-static void editPage(
60111
+static int editPage(
5941260112
MemPage *pPg, /* Edit this page */
5941360113
int iOld, /* Index of first cell currently on page */
5941460114
int iNew, /* Index of new first cell on page */
5941560115
int nNew, /* Final number of cells on page */
59416
- u8 **apCell, /* Array of cells */
59417
- u16 *szCell /* Array of cell sizes */
60116
+ CellArray *pCArray /* Array of cells and sizes */
5941860117
){
5941960118
u8 * const aData = pPg->aData;
5942060119
const int hdr = pPg->hdrOffset;
5942160120
u8 *pBegin = &pPg->aCellIdx[nNew * 2];
5942260121
int nCell = pPg->nCell; /* Cells stored on pPg */
@@ -59431,20 +60130,16 @@
5943160130
memcpy(pTmp, aData, pPg->pBt->usableSize);
5943260131
#endif
5943360132
5943460133
/* Remove cells from the start and end of the page */
5943560134
if( iOld<iNew ){
59436
- int nShift = pageFreeArray(
59437
- pPg, iNew-iOld, &apCell[iOld], &szCell[iOld]
59438
- );
60135
+ int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
5943960136
memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
5944060137
nCell -= nShift;
5944160138
}
5944260139
if( iNewEnd < iOldEnd ){
59443
- nCell -= pageFreeArray(
59444
- pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd]
59445
- );
60140
+ nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
5944660141
}
5944760142
5944860143
pData = &aData[get2byteNotZero(&aData[hdr+5])];
5944960144
if( pData<pBegin ) goto editpage_fail;
5945060145
@@ -59454,11 +60149,11 @@
5945460149
assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
5945560150
pCellptr = pPg->aCellIdx;
5945660151
memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
5945760152
if( pageInsertArray(
5945860153
pPg, pBegin, &pData, pCellptr,
59459
- nAdd, &apCell[iNew], &szCell[iNew]
60154
+ iNew, nAdd, pCArray
5946060155
) ) goto editpage_fail;
5946160156
nCell += nAdd;
5946260157
}
5946360158
5946460159
/* Add any overflow cells */
@@ -59468,20 +60163,20 @@
5946860163
pCellptr = &pPg->aCellIdx[iCell * 2];
5946960164
memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
5947060165
nCell++;
5947160166
if( pageInsertArray(
5947260167
pPg, pBegin, &pData, pCellptr,
59473
- 1, &apCell[iCell + iNew], &szCell[iCell + iNew]
60168
+ iCell+iNew, 1, pCArray
5947460169
) ) goto editpage_fail;
5947560170
}
5947660171
}
5947760172
5947860173
/* Append cells to the end of the page */
5947960174
pCellptr = &pPg->aCellIdx[nCell*2];
5948060175
if( pageInsertArray(
5948160176
pPg, pBegin, &pData, pCellptr,
59482
- nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell]
60177
+ iNew+nCell, nNew-nCell, pCArray
5948360178
) ) goto editpage_fail;
5948460179
5948560180
pPg->nCell = nNew;
5948660181
pPg->nOverflow = 0;
5948760182
@@ -59488,23 +60183,25 @@
5948860183
put2byte(&aData[hdr+3], pPg->nCell);
5948960184
put2byte(&aData[hdr+5], pData - aData);
5949060185
5949160186
#ifdef SQLITE_DEBUG
5949260187
for(i=0; i<nNew && !CORRUPT_DB; i++){
59493
- u8 *pCell = apCell[i+iNew];
59494
- int iOff = get2byte(&pPg->aCellIdx[i*2]);
60188
+ u8 *pCell = pCArray->apCell[i+iNew];
60189
+ int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
5949560190
if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
5949660191
pCell = &pTmp[pCell - aData];
5949760192
}
59498
- assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) );
60193
+ assert( 0==memcmp(pCell, &aData[iOff],
60194
+ pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
5949960195
}
5950060196
#endif
5950160197
59502
- return;
60198
+ return SQLITE_OK;
5950360199
editpage_fail:
5950460200
/* Unable to edit this page. Rebuild it from scratch instead. */
59505
- rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]);
60201
+ populateCellCache(pCArray, iNew, nNew);
60202
+ return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
5950660203
}
5950760204
5950860205
/*
5950960206
** The following parameters determine how many adjacent pages get involved
5951060207
** in a balancing operation. NN is the number of neighbors on either side
@@ -59566,17 +60263,18 @@
5956660263
5956760264
if( rc==SQLITE_OK ){
5956860265
5956960266
u8 *pOut = &pSpace[4];
5957060267
u8 *pCell = pPage->apOvfl[0];
59571
- u16 szCell = cellSizePtr(pPage, pCell);
60268
+ u16 szCell = pPage->xCellSize(pPage, pCell);
5957260269
u8 *pStop;
5957360270
5957460271
assert( sqlite3PagerIswriteable(pNew->pDbPage) );
5957560272
assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
5957660273
zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
59577
- rebuildPage(pNew, 1, &pCell, &szCell);
60274
+ rc = rebuildPage(pNew, 1, &pCell, &szCell);
60275
+ if( NEVER(rc) ) return rc;
5957860276
pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
5957960277
5958060278
/* If this is an auto-vacuum database, update the pointer map
5958160279
** with entries for the new page, and any pointer from the
5958260280
** cell on the page to an overflow page. If either of these
@@ -59645,11 +60343,11 @@
5964560343
for(j=0; j<pPage->nCell; j++){
5964660344
CellInfo info;
5964760345
u8 *z;
5964860346
5964960347
z = findCell(pPage, j);
59650
- btreeParseCellPtr(pPage, z, &info);
60348
+ pPage->xParseCell(pPage, z, &info);
5965160349
if( info.iOverflow ){
5965260350
Pgno ovfl = get4byte(&z[info.iOverflow]);
5965360351
ptrmapGet(pBt, ovfl, &e, &n);
5965460352
assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
5965560353
}
@@ -59776,11 +60474,10 @@
5977660474
u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
5977760475
int isRoot, /* True if pParent is a root-page */
5977860476
int bBulk /* True if this call is part of a bulk load */
5977960477
){
5978060478
BtShared *pBt; /* The whole database */
59781
- int nCell = 0; /* Number of cells in apCell[] */
5978260479
int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
5978360480
int nNew = 0; /* Number of pages in apNew[] */
5978460481
int nOld; /* Number of pages in apOld[] */
5978560482
int i, j, k; /* Loop counters */
5978660483
int nxDiv; /* Next divider slot in pParent->aCell[] */
@@ -59787,31 +60484,31 @@
5978760484
int rc = SQLITE_OK; /* The return code */
5978860485
u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
5978960486
int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
5979060487
int usableSpace; /* Bytes in pPage beyond the header */
5979160488
int pageFlags; /* Value of pPage->aData[0] */
59792
- int subtotal; /* Subtotal of bytes in cells on one page */
5979360489
int iSpace1 = 0; /* First unused byte of aSpace1[] */
5979460490
int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
5979560491
int szScratch; /* Size of scratch memory requested */
5979660492
MemPage *apOld[NB]; /* pPage and up to two siblings */
5979760493
MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
5979860494
u8 *pRight; /* Location in parent of right-sibling pointer */
5979960495
u8 *apDiv[NB-1]; /* Divider cells in pParent */
59800
- int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
59801
- int cntOld[NB+2]; /* Old index in aCell[] after i-th page */
60496
+ int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
60497
+ int cntOld[NB+2]; /* Old index in b.apCell[] */
5980260498
int szNew[NB+2]; /* Combined size of cells placed on i-th page */
59803
- u8 **apCell = 0; /* All cells begin balanced */
59804
- u16 *szCell; /* Local size of all cells in apCell[] */
5980560499
u8 *aSpace1; /* Space for copies of dividers cells */
5980660500
Pgno pgno; /* Temp var to store a page number in */
5980760501
u8 abDone[NB+2]; /* True after i'th new page is populated */
5980860502
Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
5980960503
Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
5981060504
u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
60505
+ CellArray b; /* Parsed information on cells being balanced */
5981160506
5981260507
memset(abDone, 0, sizeof(abDone));
60508
+ b.nCell = 0;
60509
+ b.apCell = 0;
5981360510
pBt = pParent->pBt;
5981460511
assert( sqlite3_mutex_held(pBt->mutex) );
5981560512
assert( sqlite3PagerIswriteable(pParent->pDbPage) );
5981660513
5981760514
#if 0
@@ -59861,11 +60558,11 @@
5986160558
}else{
5986260559
pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
5986360560
}
5986460561
pgno = get4byte(pRight);
5986560562
while( 1 ){
59866
- rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
60563
+ rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
5986760564
if( rc ){
5986860565
memset(apOld, 0, (i+1)*sizeof(MemPage*));
5986960566
goto balance_cleanup;
5987060567
}
5987160568
nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
@@ -59872,16 +60569,16 @@
5987260569
if( (i--)==0 ) break;
5987360570
5987460571
if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
5987560572
apDiv[i] = pParent->apOvfl[0];
5987660573
pgno = get4byte(apDiv[i]);
59877
- szNew[i] = cellSizePtr(pParent, apDiv[i]);
60574
+ szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
5987860575
pParent->nOverflow = 0;
5987960576
}else{
5988060577
apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
5988160578
pgno = get4byte(apDiv[i]);
59882
- szNew[i] = cellSizePtr(pParent, apDiv[i]);
60579
+ szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
5988360580
5988460581
/* Drop the cell from the parent page. apDiv[i] still points to
5988560582
** the cell within the parent, even though it has been dropped.
5988660583
** This is safe because dropping a cell only overwrites the first
5988760584
** four bytes of it, and this function does not need the first
@@ -59916,142 +60613,205 @@
5991660613
5991760614
/*
5991860615
** Allocate space for memory structures
5991960616
*/
5992060617
szScratch =
59921
- nMaxCells*sizeof(u8*) /* apCell */
59922
- + nMaxCells*sizeof(u16) /* szCell */
60618
+ nMaxCells*sizeof(u8*) /* b.apCell */
60619
+ + nMaxCells*sizeof(u16) /* b.szCell */
5992360620
+ pBt->pageSize; /* aSpace1 */
5992460621
5992560622
/* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
5992660623
** that is more than 6 times the database page size. */
5992760624
assert( szScratch<=6*(int)pBt->pageSize );
59928
- apCell = sqlite3ScratchMalloc( szScratch );
59929
- if( apCell==0 ){
60625
+ b.apCell = sqlite3ScratchMalloc( szScratch );
60626
+ if( b.apCell==0 ){
5993060627
rc = SQLITE_NOMEM;
5993160628
goto balance_cleanup;
5993260629
}
59933
- szCell = (u16*)&apCell[nMaxCells];
59934
- aSpace1 = (u8*)&szCell[nMaxCells];
60630
+ b.szCell = (u16*)&b.apCell[nMaxCells];
60631
+ aSpace1 = (u8*)&b.szCell[nMaxCells];
5993560632
assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
5993660633
5993760634
/*
5993860635
** Load pointers to all cells on sibling pages and the divider cells
59939
- ** into the local apCell[] array. Make copies of the divider cells
60636
+ ** into the local b.apCell[] array. Make copies of the divider cells
5994060637
** into space obtained from aSpace1[]. The divider cells have already
5994160638
** been removed from pParent.
5994260639
**
5994360640
** If the siblings are on leaf pages, then the child pointers of the
5994460641
** divider cells are stripped from the cells before they are copied
59945
- ** into aSpace1[]. In this way, all cells in apCell[] are without
60642
+ ** into aSpace1[]. In this way, all cells in b.apCell[] are without
5994660643
** child pointers. If siblings are not leaves, then all cell in
59947
- ** apCell[] include child pointers. Either way, all cells in apCell[]
60644
+ ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
5994860645
** are alike.
5994960646
**
5995060647
** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
5995160648
** leafData: 1 if pPage holds key+data and pParent holds only keys.
5995260649
*/
59953
- leafCorrection = apOld[0]->leaf*4;
59954
- leafData = apOld[0]->intKeyLeaf;
60650
+ b.pRef = apOld[0];
60651
+ leafCorrection = b.pRef->leaf*4;
60652
+ leafData = b.pRef->intKeyLeaf;
5995560653
for(i=0; i<nOld; i++){
59956
- int limit;
5995760654
MemPage *pOld = apOld[i];
60655
+ int limit = pOld->nCell;
60656
+ u8 *aData = pOld->aData;
60657
+ u16 maskPage = pOld->maskPage;
60658
+ u8 *piCell = aData + pOld->cellOffset;
60659
+ u8 *piEnd;
5995860660
5995960661
/* Verify that all sibling pages are of the same "type" (table-leaf,
5996060662
** table-interior, index-leaf, or index-interior).
5996160663
*/
5996260664
if( pOld->aData[0]!=apOld[0]->aData[0] ){
5996360665
rc = SQLITE_CORRUPT_BKPT;
5996460666
goto balance_cleanup;
5996560667
}
5996660668
59967
- limit = pOld->nCell+pOld->nOverflow;
59968
- if( pOld->nOverflow>0 ){
59969
- for(j=0; j<limit; j++){
59970
- assert( nCell<nMaxCells );
59971
- apCell[nCell] = findOverflowCell(pOld, j);
59972
- szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
59973
- nCell++;
59974
- }
59975
- }else{
59976
- u8 *aData = pOld->aData;
59977
- u16 maskPage = pOld->maskPage;
59978
- u16 cellOffset = pOld->cellOffset;
59979
- for(j=0; j<limit; j++){
59980
- assert( nCell<nMaxCells );
59981
- apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
59982
- szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
59983
- nCell++;
59984
- }
59985
- }
59986
- cntOld[i] = nCell;
60669
+ /* Load b.apCell[] with pointers to all cells in pOld. If pOld
60670
+ ** constains overflow cells, include them in the b.apCell[] array
60671
+ ** in the correct spot.
60672
+ **
60673
+ ** Note that when there are multiple overflow cells, it is always the
60674
+ ** case that they are sequential and adjacent. This invariant arises
60675
+ ** because multiple overflows can only occurs when inserting divider
60676
+ ** cells into a parent on a prior balance, and divider cells are always
60677
+ ** adjacent and are inserted in order. There is an assert() tagged
60678
+ ** with "NOTE 1" in the overflow cell insertion loop to prove this
60679
+ ** invariant.
60680
+ **
60681
+ ** This must be done in advance. Once the balance starts, the cell
60682
+ ** offset section of the btree page will be overwritten and we will no
60683
+ ** long be able to find the cells if a pointer to each cell is not saved
60684
+ ** first.
60685
+ */
60686
+ memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit);
60687
+ if( pOld->nOverflow>0 ){
60688
+ memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow);
60689
+ limit = pOld->aiOvfl[0];
60690
+ for(j=0; j<limit; j++){
60691
+ b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
60692
+ piCell += 2;
60693
+ b.nCell++;
60694
+ }
60695
+ for(k=0; k<pOld->nOverflow; k++){
60696
+ assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
60697
+ b.apCell[b.nCell] = pOld->apOvfl[k];
60698
+ b.nCell++;
60699
+ }
60700
+ }
60701
+ piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
60702
+ while( piCell<piEnd ){
60703
+ assert( b.nCell<nMaxCells );
60704
+ b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
60705
+ piCell += 2;
60706
+ b.nCell++;
60707
+ }
60708
+
60709
+ cntOld[i] = b.nCell;
5998760710
if( i<nOld-1 && !leafData){
5998860711
u16 sz = (u16)szNew[i];
5998960712
u8 *pTemp;
59990
- assert( nCell<nMaxCells );
59991
- szCell[nCell] = sz;
60713
+ assert( b.nCell<nMaxCells );
60714
+ b.szCell[b.nCell] = sz;
5999260715
pTemp = &aSpace1[iSpace1];
5999360716
iSpace1 += sz;
5999460717
assert( sz<=pBt->maxLocal+23 );
5999560718
assert( iSpace1 <= (int)pBt->pageSize );
5999660719
memcpy(pTemp, apDiv[i], sz);
59997
- apCell[nCell] = pTemp+leafCorrection;
60720
+ b.apCell[b.nCell] = pTemp+leafCorrection;
5999860721
assert( leafCorrection==0 || leafCorrection==4 );
59999
- szCell[nCell] = szCell[nCell] - leafCorrection;
60722
+ b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
6000060723
if( !pOld->leaf ){
6000160724
assert( leafCorrection==0 );
6000260725
assert( pOld->hdrOffset==0 );
6000360726
/* The right pointer of the child page pOld becomes the left
6000460727
** pointer of the divider cell */
60005
- memcpy(apCell[nCell], &pOld->aData[8], 4);
60728
+ memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
6000660729
}else{
6000760730
assert( leafCorrection==4 );
60008
- while( szCell[nCell]<4 ){
60731
+ while( b.szCell[b.nCell]<4 ){
6000960732
/* Do not allow any cells smaller than 4 bytes. If a smaller cell
6001060733
** does exist, pad it with 0x00 bytes. */
60011
- assert( szCell[nCell]==3 || CORRUPT_DB );
60012
- assert( apCell[nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
60734
+ assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
60735
+ assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
6001360736
aSpace1[iSpace1++] = 0x00;
60014
- szCell[nCell]++;
60737
+ b.szCell[b.nCell]++;
6001560738
}
6001660739
}
60017
- nCell++;
60740
+ b.nCell++;
6001860741
}
6001960742
}
6002060743
6002160744
/*
60022
- ** Figure out the number of pages needed to hold all nCell cells.
60745
+ ** Figure out the number of pages needed to hold all b.nCell cells.
6002360746
** Store this number in "k". Also compute szNew[] which is the total
6002460747
** size of all cells on the i-th page and cntNew[] which is the index
60025
- ** in apCell[] of the cell that divides page i from page i+1.
60026
- ** cntNew[k] should equal nCell.
60748
+ ** in b.apCell[] of the cell that divides page i from page i+1.
60749
+ ** cntNew[k] should equal b.nCell.
6002760750
**
6002860751
** Values computed by this block:
6002960752
**
6003060753
** k: The total number of sibling pages
6003160754
** szNew[i]: Spaced used on the i-th sibling page.
60032
- ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
60755
+ ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
6003360756
** the right of the i-th sibling page.
6003460757
** usableSpace: Number of bytes of space available on each sibling.
6003560758
**
6003660759
*/
6003760760
usableSpace = pBt->usableSize - 12 + leafCorrection;
60038
- for(subtotal=k=i=0; i<nCell; i++){
60039
- assert( i<nMaxCells );
60040
- subtotal += szCell[i] + 2;
60041
- if( subtotal > usableSpace ){
60042
- szNew[k] = subtotal - szCell[i] - 2;
60043
- cntNew[k] = i;
60044
- if( leafData ){ i--; }
60045
- subtotal = 0;
60046
- k++;
60047
- if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
60048
- }
60049
- }
60050
- szNew[k] = subtotal;
60051
- cntNew[k] = nCell;
60052
- k++;
60761
+ for(i=0; i<nOld; i++){
60762
+ MemPage *p = apOld[i];
60763
+ szNew[i] = usableSpace - p->nFree;
60764
+ if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
60765
+ for(j=0; j<p->nOverflow; j++){
60766
+ szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
60767
+ }
60768
+ cntNew[i] = cntOld[i];
60769
+ }
60770
+ k = nOld;
60771
+ for(i=0; i<k; i++){
60772
+ int sz;
60773
+ while( szNew[i]>usableSpace ){
60774
+ if( i+1>=k ){
60775
+ k = i+2;
60776
+ if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
60777
+ szNew[k-1] = 0;
60778
+ cntNew[k-1] = b.nCell;
60779
+ }
60780
+ sz = 2 + cachedCellSize(&b, cntNew[i]-1);
60781
+ szNew[i] -= sz;
60782
+ if( !leafData ){
60783
+ if( cntNew[i]<b.nCell ){
60784
+ sz = 2 + cachedCellSize(&b, cntNew[i]);
60785
+ }else{
60786
+ sz = 0;
60787
+ }
60788
+ }
60789
+ szNew[i+1] += sz;
60790
+ cntNew[i]--;
60791
+ }
60792
+ while( cntNew[i]<b.nCell ){
60793
+ sz = 2 + cachedCellSize(&b, cntNew[i]);
60794
+ if( szNew[i]+sz>usableSpace ) break;
60795
+ szNew[i] += sz;
60796
+ cntNew[i]++;
60797
+ if( !leafData ){
60798
+ if( cntNew[i]<b.nCell ){
60799
+ sz = 2 + cachedCellSize(&b, cntNew[i]);
60800
+ }else{
60801
+ sz = 0;
60802
+ }
60803
+ }
60804
+ szNew[i+1] -= sz;
60805
+ }
60806
+ if( cntNew[i]>=b.nCell ){
60807
+ k = i+1;
60808
+ }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
60809
+ rc = SQLITE_CORRUPT_BKPT;
60810
+ goto balance_cleanup;
60811
+ }
60812
+ }
6005360813
6005460814
/*
6005560815
** The packing computed by the previous block is biased toward the siblings
6005660816
** on the left side (siblings with smaller keys). The left siblings are
6005760817
** always nearly full, while the right-most sibling might be nearly empty.
@@ -60068,23 +60828,31 @@
6006860828
int r; /* Index of right-most cell in left sibling */
6006960829
int d; /* Index of first cell to the left of right sibling */
6007060830
6007160831
r = cntNew[i-1] - 1;
6007260832
d = r + 1 - leafData;
60073
- assert( d<nMaxCells );
60074
- assert( r<nMaxCells );
60075
- while( szRight==0
60076
- || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2))
60077
- ){
60078
- szRight += szCell[d] + 2;
60079
- szLeft -= szCell[r] + 2;
60080
- cntNew[i-1]--;
60081
- r = cntNew[i-1] - 1;
60082
- d = r + 1 - leafData;
60083
- }
60833
+ (void)cachedCellSize(&b, d);
60834
+ do{
60835
+ assert( d<nMaxCells );
60836
+ assert( r<nMaxCells );
60837
+ (void)cachedCellSize(&b, r);
60838
+ if( szRight!=0
60839
+ && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+2)) ){
60840
+ break;
60841
+ }
60842
+ szRight += b.szCell[d] + 2;
60843
+ szLeft -= b.szCell[r] + 2;
60844
+ cntNew[i-1] = r;
60845
+ r--;
60846
+ d--;
60847
+ }while( r>=0 );
6008460848
szNew[i] = szRight;
6008560849
szNew[i-1] = szLeft;
60850
+ if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
60851
+ rc = SQLITE_CORRUPT_BKPT;
60852
+ goto balance_cleanup;
60853
+ }
6008660854
}
6008760855
6008860856
/* Sanity check: For a non-corrupt database file one of the follwing
6008960857
** must be true:
6009060858
** (1) We found one or more cells (cntNew[0])>0), or
@@ -60116,11 +60884,11 @@
6011660884
rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
6011760885
if( rc ) goto balance_cleanup;
6011860886
zeroPage(pNew, pageFlags);
6011960887
apNew[i] = pNew;
6012060888
nNew++;
60121
- cntOld[i] = nCell;
60889
+ cntOld[i] = b.nCell;
6012260890
6012360891
/* Set the pointer-map entry for the new sibling page. */
6012460892
if( ISAUTOVACUUM ){
6012560893
ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
6012660894
if( rc!=SQLITE_OK ){
@@ -60221,12 +60989,12 @@
6022160989
int cntOldNext = pNew->nCell + pNew->nOverflow;
6022260990
int usableSize = pBt->usableSize;
6022360991
int iNew = 0;
6022460992
int iOld = 0;
6022560993
60226
- for(i=0; i<nCell; i++){
60227
- u8 *pCell = apCell[i];
60994
+ for(i=0; i<b.nCell; i++){
60995
+ u8 *pCell = b.apCell[i];
6022860996
if( i==cntOldNext ){
6022960997
MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
6023060998
cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
6023160999
aOld = pOld->aData;
6023261000
}
@@ -60247,13 +61015,14 @@
6024761015
|| pCell>=&aOld[usableSize]
6024861016
){
6024961017
if( !leafCorrection ){
6025061018
ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
6025161019
}
60252
- if( szCell[i]>pNew->minLocal ){
61020
+ if( cachedCellSize(&b,i)>pNew->minLocal ){
6025361021
ptrmapPutOvflPtr(pNew, pCell, &rc);
6025461022
}
61023
+ if( rc ) goto balance_cleanup;
6025561024
}
6025661025
}
6025761026
}
6025861027
6025961028
/* Insert new divider cells into pParent. */
@@ -60263,24 +61032,25 @@
6026361032
int sz;
6026461033
MemPage *pNew = apNew[i];
6026561034
j = cntNew[i];
6026661035
6026761036
assert( j<nMaxCells );
60268
- pCell = apCell[j];
60269
- sz = szCell[j] + leafCorrection;
61037
+ assert( b.apCell[j]!=0 );
61038
+ pCell = b.apCell[j];
61039
+ sz = b.szCell[j] + leafCorrection;
6027061040
pTemp = &aOvflSpace[iOvflSpace];
6027161041
if( !pNew->leaf ){
6027261042
memcpy(&pNew->aData[8], pCell, 4);
6027361043
}else if( leafData ){
6027461044
/* If the tree is a leaf-data tree, and the siblings are leaves,
60275
- ** then there is no divider cell in apCell[]. Instead, the divider
61045
+ ** then there is no divider cell in b.apCell[]. Instead, the divider
6027661046
** cell consists of the integer key for the right-most cell of
6027761047
** the sibling-page assembled above only.
6027861048
*/
6027961049
CellInfo info;
6028061050
j--;
60281
- btreeParseCellPtr(pNew, apCell[j], &info);
61051
+ pNew->xParseCell(pNew, b.apCell[j], &info);
6028261052
pCell = pTemp;
6028361053
sz = 4 + putVarint(&pCell[4], info.nKey);
6028461054
pTemp = 0;
6028561055
}else{
6028661056
pCell -= 4;
@@ -60293,13 +61063,13 @@
6029361063
**
6029461064
** Note that this can never happen in an SQLite data file, as all
6029561065
** cells are at least 4 bytes. It only happens in b-trees used
6029661066
** to evaluate "IN (SELECT ...)" and similar clauses.
6029761067
*/
60298
- if( szCell[j]==4 ){
61068
+ if( b.szCell[j]==4 ){
6029961069
assert(leafCorrection==4);
60300
- sz = cellSizePtr(pParent, pCell);
61070
+ sz = pParent->xCellSize(pParent, pCell);
6030161071
}
6030261072
}
6030361073
iOvflSpace += sz;
6030461074
assert( sz<=pBt->maxLocal+23 );
6030561075
assert( iOvflSpace <= (int)pBt->pageSize );
@@ -60351,16 +61121,17 @@
6035161121
6035261122
if( iPg==0 ){
6035361123
iNew = iOld = 0;
6035461124
nNewCell = cntNew[0];
6035561125
}else{
60356
- iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell;
61126
+ iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
6035761127
iNew = cntNew[iPg-1] + !leafData;
6035861128
nNewCell = cntNew[iPg] - iNew;
6035961129
}
6036061130
60361
- editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell);
61131
+ rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
61132
+ if( rc ) goto balance_cleanup;
6036261133
abDone[iPg]++;
6036361134
apNew[iPg]->nFree = usableSpace-szNew[iPg];
6036461135
assert( apNew[iPg]->nOverflow==0 );
6036561136
assert( apNew[iPg]->nCell==nNewCell );
6036661137
}
@@ -60407,11 +61178,11 @@
6040761178
}
6040861179
}
6040961180
6041061181
assert( pParent->isInit );
6041161182
TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
60412
- nOld, nNew, nCell));
61183
+ nOld, nNew, b.nCell));
6041361184
6041461185
/* Free any old pages that were not reused as new pages.
6041561186
*/
6041661187
for(i=nNew; i<nOld; i++){
6041761188
freePage(apOld[i], &rc);
@@ -60430,11 +61201,11 @@
6043061201
6043161202
/*
6043261203
** Cleanup before returning.
6043361204
*/
6043461205
balance_cleanup:
60435
- sqlite3ScratchFree(apCell);
61206
+ sqlite3ScratchFree(b.apCell);
6043661207
for(i=0; i<nOld; i++){
6043761208
releasePage(apOld[i]);
6043861209
}
6043961210
for(i=0; i<nNew; i++){
6044061211
releasePage(apNew[i]);
@@ -60705,28 +61476,32 @@
6070561476
** data into the intkey B-Tree. In this case btreeMoveto() recognizes
6070661477
** that the cursor is already where it needs to be and returns without
6070761478
** doing any work. To avoid thwarting these optimizations, it is important
6070861479
** not to clear the cursor here.
6070961480
*/
60710
- rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
60711
- if( rc ) return rc;
61481
+ if( pCur->curFlags & BTCF_Multiple ){
61482
+ rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
61483
+ if( rc ) return rc;
61484
+ }
6071261485
6071361486
if( pCur->pKeyInfo==0 ){
61487
+ assert( pKey==0 );
6071461488
/* If this is an insert into a table b-tree, invalidate any incrblob
6071561489
** cursors open on the row being replaced */
6071661490
invalidateIncrblobCursors(p, nKey, 0);
6071761491
6071861492
/* If the cursor is currently on the last row and we are appending a
60719
- ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
60720
- ** call */
61493
+ ** new row onto the end, set the "loc" to avoid an unnecessary
61494
+ ** btreeMoveto() call */
6072161495
if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
6072261496
&& pCur->info.nKey==nKey-1 ){
60723
- loc = -1;
61497
+ loc = -1;
61498
+ }else if( loc==0 ){
61499
+ rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc);
61500
+ if( rc ) return rc;
6072461501
}
60725
- }
60726
-
60727
- if( !loc ){
61502
+ }else if( loc==0 ){
6072861503
rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
6072961504
if( rc ) return rc;
6073061505
}
6073161506
assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
6073261507
@@ -60740,11 +61515,11 @@
6074061515
assert( pPage->isInit );
6074161516
newCell = pBt->pTmpSpace;
6074261517
assert( newCell!=0 );
6074361518
rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
6074461519
if( rc ) goto end_insert;
60745
- assert( szNew==cellSizePtr(pPage, newCell) );
61520
+ assert( szNew==pPage->xCellSize(pPage, newCell) );
6074661521
assert( szNew <= MX_CELL_SIZE(pBt) );
6074761522
idx = pCur->aiIdx[pCur->iPage];
6074861523
if( loc==0 ){
6074961524
u16 szOld;
6075061525
assert( idx<pPage->nCell );
@@ -60824,16 +61599,12 @@
6082461599
assert( pBt->inTransaction==TRANS_WRITE );
6082561600
assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
6082661601
assert( pCur->curFlags & BTCF_WriteFlag );
6082761602
assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
6082861603
assert( !hasReadConflicts(p, pCur->pgnoRoot) );
60829
-
60830
- if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
60831
- || NEVER(pCur->eState!=CURSOR_VALID)
60832
- ){
60833
- return SQLITE_ERROR; /* Something has gone awry. */
60834
- }
61604
+ assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
61605
+ assert( pCur->eState==CURSOR_VALID );
6083561606
6083661607
iCellDepth = pCur->iPage;
6083761608
iCellIdx = pCur->aiIdx[iCellDepth];
6083861609
pPage = pCur->apPage[iCellDepth];
6083961610
pCell = findCell(pPage, iCellIdx);
@@ -60854,12 +61625,14 @@
6085461625
/* Save the positions of any other cursors open on this table before
6085561626
** making any modifications. Make the page containing the entry to be
6085661627
** deleted writable. Then free any overflow pages associated with the
6085761628
** entry and finally remove the cell itself from within the page.
6085861629
*/
60859
- rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
60860
- if( rc ) return rc;
61630
+ if( pCur->curFlags & BTCF_Multiple ){
61631
+ rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
61632
+ if( rc ) return rc;
61633
+ }
6086161634
6086261635
/* If this is a delete operation to remove a row from a table b-tree,
6086361636
** invalidate any incrblob cursors open on the row being deleted. */
6086461637
if( pCur->pKeyInfo==0 ){
6086561638
invalidateIncrblobCursors(p, pCur->info.nKey, 0);
@@ -60882,11 +61655,11 @@
6088261655
Pgno n = pCur->apPage[iCellDepth+1]->pgno;
6088361656
unsigned char *pTmp;
6088461657
6088561658
pCell = findCell(pLeaf, pLeaf->nCell-1);
6088661659
if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
60887
- nCell = cellSizePtr(pLeaf, pCell);
61660
+ nCell = pLeaf->xCellSize(pLeaf, pCell);
6088861661
assert( MX_CELL_SIZE(pBt) >= nCell );
6088961662
pTmp = pBt->pTmpSpace;
6089061663
assert( pTmp!=0 );
6089161664
rc = sqlite3PagerWrite(pLeaf->pDbPage);
6089261665
insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
@@ -61104,11 +61877,11 @@
6110461877
6110561878
assert( sqlite3_mutex_held(pBt->mutex) );
6110661879
if( pgno>btreePagecount(pBt) ){
6110761880
return SQLITE_CORRUPT_BKPT;
6110861881
}
61109
- rc = getAndInitPage(pBt, pgno, &pPage, 0);
61882
+ rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
6111061883
if( rc ) return rc;
6111161884
if( pPage->bBusy ){
6111261885
rc = SQLITE_CORRUPT_BKPT;
6111361886
goto cleardatabasepage_out;
6111461887
}
@@ -61776,11 +62549,11 @@
6177662549
*/
6177762550
pCheck->zPfx = "On tree page %d cell %d: ";
6177862551
pCheck->v1 = iPage;
6177962552
pCheck->v2 = i;
6178062553
pCell = findCell(pPage,i);
61781
- btreeParseCellPtr(pPage, pCell, &info);
62554
+ pPage->xParseCell(pPage, pCell, &info);
6178262555
sz = info.nPayload;
6178362556
/* For intKey pages, check that the keys are in order.
6178462557
*/
6178562558
if( pPage->intKey ){
6178662559
if( i==0 ){
@@ -61891,14 +62664,14 @@
6189162664
** immediately follows the b-tree page header. */
6189262665
cellStart = hdr + 12 - 4*pPage->leaf;
6189362666
/* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
6189462667
** integer offsets to the cell contents. */
6189562668
for(i=0; i<nCell; i++){
61896
- int pc = get2byte(&data[cellStart+i*2]);
62669
+ int pc = get2byteAligned(&data[cellStart+i*2]);
6189762670
u32 size = 65536;
6189862671
if( pc<=usableSize-4 ){
61899
- size = cellSizePtr(pPage, &data[pc]);
62672
+ size = pPage->xCellSize(pPage, &data[pc]);
6190062673
}
6190162674
if( (int)(pc+size-1)>=usableSize ){
6190262675
pCheck->zPfx = 0;
6190362676
checkAppendMsg(pCheck,
6190462677
"Corruption detected in cell %d on page %d",i,iPage);
@@ -62292,10 +63065,11 @@
6229263065
/*
6229363066
** Mark this cursor as an incremental blob cursor.
6229463067
*/
6229563068
SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
6229663069
pCur->curFlags |= BTCF_Incrblob;
63070
+ pCur->pBtree->hasIncrblobCur = 1;
6229763071
}
6229863072
#endif
6229963073
6230063074
/*
6230163075
** Set both the "read version" (single byte at byte offset 18) and
@@ -63739,11 +64513,11 @@
6373964513
** used (for example) to implement the SQL "cast()" operator.
6374064514
*/
6374164515
SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
6374264516
if( pMem->flags & MEM_Null ) return;
6374364517
switch( aff ){
63744
- case SQLITE_AFF_NONE: { /* Really a cast to BLOB */
64518
+ case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
6374564519
if( (pMem->flags & MEM_Blob)==0 ){
6374664520
sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
6374764521
assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
6374864522
MemSetTypeFlag(pMem, MEM_Blob);
6374964523
}else{
@@ -63928,14 +64702,19 @@
6392864702
** Make an shallow copy of pFrom into pTo. Prior contents of
6392964703
** pTo are freed. The pFrom->z field is not duplicated. If
6393064704
** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
6393164705
** and flags gets srcType (either MEM_Ephem or MEM_Static).
6393264706
*/
64707
+static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
64708
+ vdbeMemClearExternAndSetNull(pTo);
64709
+ assert( !VdbeMemDynamic(pTo) );
64710
+ sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
64711
+}
6393364712
SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
6393464713
assert( (pFrom->flags & MEM_RowSet)==0 );
6393564714
assert( pTo->db==pFrom->db );
63936
- if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
64715
+ if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
6393764716
memcpy(pTo, pFrom, MEMCELLSIZE);
6393864717
if( (pFrom->flags&MEM_Static)==0 ){
6393964718
pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
6394064719
assert( srcType==MEM_Ephem || srcType==MEM_Static );
6394164720
pTo->flags |= srcType;
@@ -64097,10 +64876,36 @@
6409764876
** destroyed.
6409864877
**
6409964878
** If this routine fails for any reason (malloc returns NULL or unable
6410064879
** to read from the disk) then the pMem is left in an inconsistent state.
6410164880
*/
64881
+static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
64882
+ BtCursor *pCur, /* Cursor pointing at record to retrieve. */
64883
+ u32 offset, /* Offset from the start of data to return bytes from. */
64884
+ u32 amt, /* Number of bytes to return. */
64885
+ int key, /* If true, retrieve from the btree key, not data. */
64886
+ Mem *pMem /* OUT: Return data in this Mem structure. */
64887
+){
64888
+ int rc;
64889
+ pMem->flags = MEM_Null;
64890
+ if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
64891
+ if( key ){
64892
+ rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
64893
+ }else{
64894
+ rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
64895
+ }
64896
+ if( rc==SQLITE_OK ){
64897
+ pMem->z[amt] = 0;
64898
+ pMem->z[amt+1] = 0;
64899
+ pMem->flags = MEM_Blob|MEM_Term;
64900
+ pMem->n = (int)amt;
64901
+ }else{
64902
+ sqlite3VdbeMemRelease(pMem);
64903
+ }
64904
+ }
64905
+ return rc;
64906
+}
6410264907
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
6410364908
BtCursor *pCur, /* Cursor pointing at record to retrieve. */
6410464909
u32 offset, /* Offset from the start of data to return bytes from. */
6410564910
u32 amt, /* Number of bytes to return. */
6410664911
int key, /* If true, retrieve from the btree key, not data. */
@@ -64126,26 +64931,11 @@
6412664931
if( offset+amt<=available ){
6412764932
pMem->z = &zData[offset];
6412864933
pMem->flags = MEM_Blob|MEM_Ephem;
6412964934
pMem->n = (int)amt;
6413064935
}else{
64131
- pMem->flags = MEM_Null;
64132
- if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
64133
- if( key ){
64134
- rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
64135
- }else{
64136
- rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
64137
- }
64138
- if( rc==SQLITE_OK ){
64139
- pMem->z[amt] = 0;
64140
- pMem->z[amt+1] = 0;
64141
- pMem->flags = MEM_Blob|MEM_Term;
64142
- pMem->n = (int)amt;
64143
- }else{
64144
- sqlite3VdbeMemRelease(pMem);
64145
- }
64146
- }
64936
+ rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem);
6414764937
}
6414864938
6414964939
return rc;
6415064940
}
6415164941
@@ -64462,11 +65252,11 @@
6446265252
}else{
6446365253
zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
6446465254
if( zVal==0 ) goto no_mem;
6446565255
sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
6446665256
}
64467
- if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
65257
+ if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
6446865258
sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
6446965259
}else{
6447065260
sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
6447165261
}
6447265262
if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
@@ -64829,23 +65619,32 @@
6482965619
sqlite3VdbeMemRelease((Mem *)v);
6483065620
sqlite3DbFree(((Mem*)v)->db, v);
6483165621
}
6483265622
6483365623
/*
64834
-** Return the number of bytes in the sqlite3_value object assuming
64835
-** that it uses the encoding "enc"
65624
+** The sqlite3ValueBytes() routine returns the number of bytes in the
65625
+** sqlite3_value object assuming that it uses the encoding "enc".
65626
+** The valueBytes() routine is a helper function.
6483665627
*/
65628
+static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
65629
+ return valueToText(pVal, enc)!=0 ? pVal->n : 0;
65630
+}
6483765631
SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
6483865632
Mem *p = (Mem*)pVal;
64839
- if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
65633
+ assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
65634
+ if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
65635
+ return p->n;
65636
+ }
65637
+ if( (p->flags & MEM_Blob)!=0 ){
6484065638
if( p->flags & MEM_Zero ){
6484165639
return p->n + p->u.nZero;
6484265640
}else{
6484365641
return p->n;
6484465642
}
6484565643
}
64846
- return 0;
65644
+ if( p->flags & MEM_Null ) return 0;
65645
+ return valueBytes(pVal, enc);
6484765646
}
6484865647
6484965648
/************** End of vdbemem.c *********************************************/
6485065649
/************** Begin file vdbeaux.c *****************************************/
6485165650
/*
@@ -65078,10 +65877,27 @@
6507865877
){
6507965878
int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
6508065879
sqlite3VdbeChangeP4(p, addr, zP4, p4type);
6508165880
return addr;
6508265881
}
65882
+
65883
+/*
65884
+** Add an opcode that includes the p4 value with a P4_INT64 type.
65885
+*/
65886
+SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
65887
+ Vdbe *p, /* Add the opcode to this VM */
65888
+ int op, /* The new opcode */
65889
+ int p1, /* The P1 operand */
65890
+ int p2, /* The P2 operand */
65891
+ int p3, /* The P3 operand */
65892
+ const u8 *zP4, /* The P4 operand */
65893
+ int p4type /* P4 operand type */
65894
+){
65895
+ char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
65896
+ if( p4copy ) memcpy(p4copy, zP4, 8);
65897
+ return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
65898
+}
6508365899
6508465900
/*
6508565901
** Add an OP_ParseSchema opcode. This routine is broken out from
6508665902
** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
6508765903
** as having been used.
@@ -65243,10 +66059,11 @@
6524366059
** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
6524466060
** * OP_Destroy
6524566061
** * OP_VUpdate
6524666062
** * OP_VRename
6524766063
** * OP_FkCounter with P2==0 (immediate foreign key constraint)
66064
+** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
6524866065
**
6524966066
** Then check that the value of Parse.mayAbort is true if an
6525066067
** ABORT may be thrown, or false otherwise. Return true if it does
6525166068
** match, or false otherwise. This function is intended to be used as
6525266069
** part of an assert statement in the compiler. Similar to:
@@ -65254,10 +66071,12 @@
6525466071
** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
6525566072
*/
6525666073
SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
6525766074
int hasAbort = 0;
6525866075
int hasFkCounter = 0;
66076
+ int hasCreateTable = 0;
66077
+ int hasInitCoroutine = 0;
6525966078
Op *pOp;
6526066079
VdbeOpIter sIter;
6526166080
memset(&sIter, 0, sizeof(sIter));
6526266081
sIter.v = v;
6526366082
@@ -65268,10 +66087,12 @@
6526866087
&& ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
6526966088
){
6527066089
hasAbort = 1;
6527166090
break;
6527266091
}
66092
+ if( opcode==OP_CreateTable ) hasCreateTable = 1;
66093
+ if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
6527366094
#ifndef SQLITE_OMIT_FOREIGN_KEY
6527466095
if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
6527566096
hasFkCounter = 1;
6527666097
}
6527766098
#endif
@@ -65281,11 +66102,12 @@
6528166102
/* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
6528266103
** If malloc failed, then the while() loop above may not have iterated
6528366104
** through all opcodes and hasAbort may be set incorrectly. Return
6528466105
** true for this case to prevent the assert() in the callers frame
6528566106
** from failing. */
65286
- return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter );
66107
+ return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
66108
+ || (hasCreateTable && hasInitCoroutine) );
6528766109
}
6528866110
#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
6528966111
6529066112
/*
6529166113
** Loop through the program looking for P2 values that are negative
@@ -65312,15 +66134,10 @@
6531266134
u8 opcode = pOp->opcode;
6531366135
6531466136
/* NOTE: Be sure to update mkopcodeh.awk when adding or removing
6531566137
** cases from this switch! */
6531666138
switch( opcode ){
65317
- case OP_Function:
65318
- case OP_AggStep: {
65319
- if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
65320
- break;
65321
- }
6532266139
case OP_Transaction: {
6532366140
if( pOp->p2!=0 ) p->readOnly = 0;
6532466141
/* fall thru */
6532566142
}
6532666143
case OP_AutoCommit:
@@ -65560,10 +66377,14 @@
6556066377
*/
6556166378
static void freeP4(sqlite3 *db, int p4type, void *p4){
6556266379
if( p4 ){
6556366380
assert( db );
6556466381
switch( p4type ){
66382
+ case P4_FUNCCTX: {
66383
+ freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
66384
+ /* Fall through into the next case */
66385
+ }
6556566386
case P4_REAL:
6556666387
case P4_INT64:
6556766388
case P4_DYNAMIC:
6556866389
case P4_INTARRAY: {
6556966390
sqlite3DbFree(db, p4);
@@ -65944,10 +66765,17 @@
6594466765
case P4_FUNCDEF: {
6594566766
FuncDef *pDef = pOp->p4.pFunc;
6594666767
sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
6594766768
break;
6594866769
}
66770
+#ifdef SQLITE_DEBUG
66771
+ case P4_FUNCCTX: {
66772
+ FuncDef *pDef = pOp->p4.pCtx->pFunc;
66773
+ sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
66774
+ break;
66775
+ }
66776
+#endif
6594966777
case P4_INT64: {
6595066778
sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
6595166779
break;
6595266780
}
6595366781
case P4_INT32: {
@@ -66064,24 +66892,27 @@
6606466892
6606566893
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
6606666894
/*
6606766895
** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
6606866896
*/
66069
-SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
66897
+static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
6607066898
int i;
6607166899
sqlite3 *db;
6607266900
Db *aDb;
6607366901
int nDb;
66074
- if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
6607566902
db = p->db;
6607666903
aDb = db->aDb;
6607766904
nDb = db->nDb;
6607866905
for(i=0; i<nDb; i++){
6607966906
if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
6608066907
sqlite3BtreeLeave(aDb[i].pBt);
6608166908
}
6608266909
}
66910
+}
66911
+SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
66912
+ if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
66913
+ vdbeLeave(p);
6608366914
}
6608466915
#endif
6608566916
6608666917
#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
6608766918
/*
@@ -67775,19 +68606,25 @@
6777568606
n += pMem->u.nZero;
6777668607
}
6777768608
return ((n*2) + 12 + ((flags&MEM_Str)!=0));
6777868609
}
6777968610
68611
+/*
68612
+** The sizes for serial types less than 12
68613
+*/
68614
+static const u8 sqlite3SmallTypeSizes[] = {
68615
+ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0
68616
+};
68617
+
6778068618
/*
6778168619
** Return the length of the data corresponding to the supplied serial-type.
6778268620
*/
6778368621
SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
6778468622
if( serial_type>=12 ){
6778568623
return (serial_type-12)/2;
6778668624
}else{
67787
- static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
67788
- return aSize[serial_type];
68625
+ return sqlite3SmallTypeSizes[serial_type];
6778968626
}
6779068627
}
6779168628
6779268629
/*
6779368630
** If we are on an architecture with mixed-endian floating
@@ -67867,11 +68704,11 @@
6786768704
memcpy(&v, &pMem->u.r, sizeof(v));
6786868705
swapMixedEndianFloat(v);
6786968706
}else{
6787068707
v = pMem->u.i;
6787168708
}
67872
- len = i = sqlite3VdbeSerialTypeLen(serial_type);
68709
+ len = i = sqlite3SmallTypeSizes[serial_type];
6787368710
assert( i>0 );
6787468711
do{
6787568712
buf[--i] = (u8)(v&0xFF);
6787668713
v >>= 8;
6787768714
}while( i );
@@ -68896,11 +69733,11 @@
6889669733
testcase( typeRowid==8 );
6889769734
testcase( typeRowid==9 );
6889869735
if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
6889969736
goto idx_rowid_corruption;
6890069737
}
68901
- lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
69738
+ lenRowid = sqlite3SmallTypeSizes[typeRowid];
6890269739
testcase( (u32)m.n==szHdr+lenRowid );
6890369740
if( unlikely((u32)m.n<szHdr+lenRowid) ){
6890469741
goto idx_rowid_corruption;
6890569742
}
6890669743
@@ -71122,11 +71959,11 @@
7112271959
** an integer representation is more space efficient on disk.
7112371960
**
7112471961
** SQLITE_AFF_TEXT:
7112571962
** Convert pRec to a text representation.
7112671963
**
71127
-** SQLITE_AFF_NONE:
71964
+** SQLITE_AFF_BLOB:
7112871965
** No-op. pRec is unchanged.
7112971966
*/
7113071967
static void applyAffinity(
7113171968
Mem *pRec, /* The value to apply affinity to */
7113271969
char affinity, /* The affinity to be applied */
@@ -71523,17 +72360,13 @@
7152372360
db->busyHandler.nBusy = 0;
7152472361
if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
7152572362
sqlite3VdbeIOTraceSql(p);
7152672363
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
7152772364
if( db->xProgress ){
72365
+ u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
7152872366
assert( 0 < db->nProgressOps );
71529
- nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
71530
- if( nProgressLimit==0 ){
71531
- nProgressLimit = db->nProgressOps;
71532
- }else{
71533
- nProgressLimit %= (unsigned)db->nProgressOps;
71534
- }
72367
+ nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
7153572368
}
7153672369
#endif
7153772370
#ifdef SQLITE_DEBUG
7153872371
sqlite3BeginBenignMalloc();
7153972372
if( p->pc==0
@@ -72491,14 +73324,14 @@
7249173324
sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
7249273325
}
7249373326
break;
7249473327
}
7249573328
72496
-/* Opcode: Function P1 P2 P3 P4 P5
73329
+/* Opcode: Function0 P1 P2 P3 P4 P5
7249773330
** Synopsis: r[P3]=func(r[P2@P5])
7249873331
**
72499
-** Invoke a user function (P4 is a pointer to a Function structure that
73332
+** Invoke a user function (P4 is a pointer to a FuncDef object that
7250073333
** defines the function) with P5 arguments taken from register P2 and
7250173334
** successors. The result of the function is stored in register P3.
7250273335
** Register P3 must not be one of the function inputs.
7250373336
**
7250473337
** P1 is a 32-bit bitmask indicating whether or not each argument to the
@@ -72506,63 +73339,104 @@
7250673339
** argument was constant then bit 0 of P1 is set. This is used to determine
7250773340
** whether meta data associated with a user function argument using the
7250873341
** sqlite3_set_auxdata() API may be safely retained until the next
7250973342
** invocation of this opcode.
7251073343
**
72511
-** See also: AggStep and AggFinal
73344
+** See also: Function, AggStep, AggFinal
7251273345
*/
72513
-case OP_Function: {
72514
- int i;
72515
- Mem *pArg;
72516
- sqlite3_context ctx;
72517
- sqlite3_value **apVal;
73346
+/* Opcode: Function P1 P2 P3 P4 P5
73347
+** Synopsis: r[P3]=func(r[P2@P5])
73348
+**
73349
+** Invoke a user function (P4 is a pointer to an sqlite3_context object that
73350
+** contains a pointer to the function to be run) with P5 arguments taken
73351
+** from register P2 and successors. The result of the function is stored
73352
+** in register P3. Register P3 must not be one of the function inputs.
73353
+**
73354
+** P1 is a 32-bit bitmask indicating whether or not each argument to the
73355
+** function was determined to be constant at compile time. If the first
73356
+** argument was constant then bit 0 of P1 is set. This is used to determine
73357
+** whether meta data associated with a user function argument using the
73358
+** sqlite3_set_auxdata() API may be safely retained until the next
73359
+** invocation of this opcode.
73360
+**
73361
+** SQL functions are initially coded as OP_Function0 with P4 pointing
73362
+** to a FuncDef object. But on first evaluation, the P4 operand is
73363
+** automatically converted into an sqlite3_context object and the operation
73364
+** changed to this OP_Function opcode. In this way, the initialization of
73365
+** the sqlite3_context object occurs only once, rather than once for each
73366
+** evaluation of the function.
73367
+**
73368
+** See also: Function0, AggStep, AggFinal
73369
+*/
73370
+case OP_Function0: {
7251873371
int n;
72519
-
72520
- n = pOp->p5;
72521
- apVal = p->apArg;
72522
- assert( apVal || n==0 );
72523
- assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
72524
- ctx.pOut = &aMem[pOp->p3];
72525
- memAboutToChange(p, ctx.pOut);
72526
-
72527
- assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
72528
- assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
72529
- pArg = &aMem[pOp->p2];
72530
- for(i=0; i<n; i++, pArg++){
72531
- assert( memIsValid(pArg) );
72532
- apVal[i] = pArg;
72533
- Deephemeralize(pArg);
72534
- REGISTER_TRACE(pOp->p2+i, pArg);
72535
- }
73372
+ sqlite3_context *pCtx;
7253673373
7253773374
assert( pOp->p4type==P4_FUNCDEF );
72538
- ctx.pFunc = pOp->p4.pFunc;
72539
- ctx.iOp = (int)(pOp - aOp);
72540
- ctx.pVdbe = p;
72541
- MemSetTypeFlag(ctx.pOut, MEM_Null);
72542
- ctx.fErrorOrAux = 0;
73375
+ n = pOp->p5;
73376
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
73377
+ assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
73378
+ assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
73379
+ pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
73380
+ if( pCtx==0 ) goto no_mem;
73381
+ pCtx->pOut = 0;
73382
+ pCtx->pFunc = pOp->p4.pFunc;
73383
+ pCtx->iOp = (int)(pOp - aOp);
73384
+ pCtx->pVdbe = p;
73385
+ pCtx->argc = n;
73386
+ pOp->p4type = P4_FUNCCTX;
73387
+ pOp->p4.pCtx = pCtx;
73388
+ pOp->opcode = OP_Function;
73389
+ /* Fall through into OP_Function */
73390
+}
73391
+case OP_Function: {
73392
+ int i;
73393
+ sqlite3_context *pCtx;
73394
+
73395
+ assert( pOp->p4type==P4_FUNCCTX );
73396
+ pCtx = pOp->p4.pCtx;
73397
+
73398
+ /* If this function is inside of a trigger, the register array in aMem[]
73399
+ ** might change from one evaluation to the next. The next block of code
73400
+ ** checks to see if the register array has changed, and if so it
73401
+ ** reinitializes the relavant parts of the sqlite3_context object */
73402
+ pOut = &aMem[pOp->p3];
73403
+ if( pCtx->pOut != pOut ){
73404
+ pCtx->pOut = pOut;
73405
+ for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
73406
+ }
73407
+
73408
+ memAboutToChange(p, pCtx->pOut);
73409
+#ifdef SQLITE_DEBUG
73410
+ for(i=0; i<pCtx->argc; i++){
73411
+ assert( memIsValid(pCtx->argv[i]) );
73412
+ REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
73413
+ }
73414
+#endif
73415
+ MemSetTypeFlag(pCtx->pOut, MEM_Null);
73416
+ pCtx->fErrorOrAux = 0;
7254373417
db->lastRowid = lastRowid;
72544
- (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
73418
+ (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */
7254573419
lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
7254673420
7254773421
/* If the function returned an error, throw an exception */
72548
- if( ctx.fErrorOrAux ){
72549
- if( ctx.isError ){
72550
- sqlite3VdbeError(p, "%s", sqlite3_value_text(ctx.pOut));
72551
- rc = ctx.isError;
73422
+ if( pCtx->fErrorOrAux ){
73423
+ if( pCtx->isError ){
73424
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
73425
+ rc = pCtx->isError;
7255273426
}
72553
- sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
73427
+ sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1);
7255473428
}
7255573429
7255673430
/* Copy the result of the function into register P3 */
72557
- sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
72558
- if( sqlite3VdbeMemTooBig(ctx.pOut) ){
72559
- goto too_big;
73431
+ if( pOut->flags & (MEM_Str|MEM_Blob) ){
73432
+ sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
73433
+ if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
7256073434
}
7256173435
72562
- REGISTER_TRACE(pOp->p3, ctx.pOut);
72563
- UPDATE_MAX_BLOBSIZE(ctx.pOut);
73436
+ REGISTER_TRACE(pOp->p3, pCtx->pOut);
73437
+ UPDATE_MAX_BLOBSIZE(pCtx->pOut);
7256473438
break;
7256573439
}
7256673440
7256773441
/* Opcode: BitAnd P1 P2 P3 * *
7256873442
** Synopsis: r[P3]=r[P1]&r[P2]
@@ -72721,13 +73595,13 @@
7272173595
** </ul>
7272273596
**
7272373597
** A NULL value is not changed by this routine. It remains NULL.
7272473598
*/
7272573599
case OP_Cast: { /* in1 */
72726
- assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL );
73600
+ assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
7272773601
testcase( pOp->p2==SQLITE_AFF_TEXT );
72728
- testcase( pOp->p2==SQLITE_AFF_NONE );
73602
+ testcase( pOp->p2==SQLITE_AFF_BLOB );
7272973603
testcase( pOp->p2==SQLITE_AFF_NUMERIC );
7273073604
testcase( pOp->p2==SQLITE_AFF_INTEGER );
7273173605
testcase( pOp->p2==SQLITE_AFF_REAL );
7273273606
pIn1 = &aMem[pOp->p1];
7273373607
memAboutToChange(p, pIn1);
@@ -73534,11 +74408,11 @@
7353474408
** field of the index key.
7353574409
**
7353674410
** The mapping from character to affinity is given by the SQLITE_AFF_
7353774411
** macros defined in sqliteInt.h.
7353874412
**
73539
-** If P4 is NULL then all index fields have the affinity NONE.
74413
+** If P4 is NULL then all index fields have the affinity BLOB.
7354074414
*/
7354174415
case OP_MakeRecord: {
7354274416
u8 *zNewRecord; /* A buffer to hold the data for the new record */
7354374417
Mem *pRec; /* The new record */
7354474418
u64 nData; /* Number of bytes of data space */
@@ -74451,10 +75325,30 @@
7445175325
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7445275326
sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
7445375327
p->apCsr[pOp->p1] = 0;
7445475328
break;
7445575329
}
75330
+
75331
+#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
75332
+/* Opcode: ColumnsUsed P1 * * P4 *
75333
+**
75334
+** This opcode (which only exists if SQLite was compiled with
75335
+** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
75336
+** table or index for cursor P1 are used. P4 is a 64-bit integer
75337
+** (P4_INT64) in which the first 63 bits are one for each of the
75338
+** first 63 columns of the table or index that are actually used
75339
+** by the cursor. The high-order bit is set if any column after
75340
+** the 64th is used.
75341
+*/
75342
+case OP_ColumnsUsed: {
75343
+ VdbeCursor *pC;
75344
+ pC = p->apCsr[pOp->p1];
75345
+ assert( pC->pCursor );
75346
+ pC->maskUsed = *(u64*)pOp->p4.pI64;
75347
+ break;
75348
+}
75349
+#endif
7445675350
7445775351
/* Opcode: SeekGE P1 P2 P3 P4 *
7445875352
** Synopsis: key=r[P3@P4]
7445975353
**
7446075354
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -74940,13 +75834,12 @@
7494075834
res = 0;
7494175835
pOut = out2Prerelease(p, pOp);
7494275836
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7494375837
pC = p->apCsr[pOp->p1];
7494475838
assert( pC!=0 );
74945
- if( NEVER(pC->pCursor==0) ){
74946
- /* The zero initialization above is all that is needed */
74947
- }else{
75839
+ assert( pC->pCursor!=0 );
75840
+ {
7494875841
/* The next rowid or record number (different terms for the same
7494975842
** thing) is obtained in a two-step algorithm.
7495075843
**
7495175844
** First we attempt to find the largest existing rowid and add one
7495275845
** to that. But if the largest existing rowid is already the maximum
@@ -75681,32 +76574,30 @@
7568176574
** for tables is OP_Insert.
7568276575
*/
7568376576
case OP_SorterInsert: /* in2 */
7568476577
case OP_IdxInsert: { /* in2 */
7568576578
VdbeCursor *pC;
75686
- BtCursor *pCrsr;
7568776579
int nKey;
7568876580
const char *zKey;
7568976581
7569076582
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7569176583
pC = p->apCsr[pOp->p1];
7569276584
assert( pC!=0 );
7569376585
assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
7569476586
pIn2 = &aMem[pOp->p2];
7569576587
assert( pIn2->flags & MEM_Blob );
75696
- pCrsr = pC->pCursor;
7569776588
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
75698
- assert( pCrsr!=0 );
76589
+ assert( pC->pCursor!=0 );
7569976590
assert( pC->isTable==0 );
7570076591
rc = ExpandBlob(pIn2);
7570176592
if( rc==SQLITE_OK ){
75702
- if( isSorter(pC) ){
76593
+ if( pOp->opcode==OP_SorterInsert ){
7570376594
rc = sqlite3VdbeSorterWrite(pC, pIn2);
7570476595
}else{
7570576596
nKey = pIn2->n;
7570676597
zKey = pIn2->z;
75707
- rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
76598
+ rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, "", 0, 0, pOp->p3,
7570876599
((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
7570976600
);
7571076601
assert( pC->deferredMoveto==0 );
7571176602
pC->cacheStatus = CACHE_STALE;
7571276603
}
@@ -76622,61 +77513,105 @@
7662277513
VdbeBranchTaken(pIn1->u.i==0, 2);
7662377514
if( (pIn1->u.i++)==0 ) goto jump_to_p2;
7662477515
break;
7662577516
}
7662677517
76627
-/* Opcode: AggStep * P2 P3 P4 P5
77518
+/* Opcode: AggStep0 * P2 P3 P4 P5
7662877519
** Synopsis: accum=r[P3] step(r[P2@P5])
7662977520
**
7663077521
** Execute the step function for an aggregate. The
7663177522
** function has P5 arguments. P4 is a pointer to the FuncDef
76632
-** structure that specifies the function. Use register
76633
-** P3 as the accumulator.
77523
+** structure that specifies the function. Register P3 is the
77524
+** accumulator.
77525
+**
77526
+** The P5 arguments are taken from register P2 and its
77527
+** successors.
77528
+*/
77529
+/* Opcode: AggStep * P2 P3 P4 P5
77530
+** Synopsis: accum=r[P3] step(r[P2@P5])
77531
+**
77532
+** Execute the step function for an aggregate. The
77533
+** function has P5 arguments. P4 is a pointer to an sqlite3_context
77534
+** object that is used to run the function. Register P3 is
77535
+** as the accumulator.
7663477536
**
7663577537
** The P5 arguments are taken from register P2 and its
7663677538
** successors.
77539
+**
77540
+** This opcode is initially coded as OP_AggStep0. On first evaluation,
77541
+** the FuncDef stored in P4 is converted into an sqlite3_context and
77542
+** the opcode is changed. In this way, the initialization of the
77543
+** sqlite3_context only happens once, instead of on each call to the
77544
+** step function.
7663777545
*/
76638
-case OP_AggStep: {
77546
+case OP_AggStep0: {
7663977547
int n;
76640
- int i;
76641
- Mem *pMem;
76642
- Mem *pRec;
76643
- Mem t;
76644
- sqlite3_context ctx;
76645
- sqlite3_value **apVal;
77548
+ sqlite3_context *pCtx;
7664677549
77550
+ assert( pOp->p4type==P4_FUNCDEF );
7664777551
n = pOp->p5;
76648
- assert( n>=0 );
76649
- pRec = &aMem[pOp->p2];
76650
- apVal = p->apArg;
76651
- assert( apVal || n==0 );
76652
- for(i=0; i<n; i++, pRec++){
76653
- assert( memIsValid(pRec) );
76654
- apVal[i] = pRec;
76655
- memAboutToChange(p, pRec);
76656
- }
76657
- ctx.pFunc = pOp->p4.pFunc;
7665877552
assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
76659
- ctx.pMem = pMem = &aMem[pOp->p3];
77553
+ assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
77554
+ assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
77555
+ pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
77556
+ if( pCtx==0 ) goto no_mem;
77557
+ pCtx->pMem = 0;
77558
+ pCtx->pFunc = pOp->p4.pFunc;
77559
+ pCtx->iOp = (int)(pOp - aOp);
77560
+ pCtx->pVdbe = p;
77561
+ pCtx->argc = n;
77562
+ pOp->p4type = P4_FUNCCTX;
77563
+ pOp->p4.pCtx = pCtx;
77564
+ pOp->opcode = OP_AggStep;
77565
+ /* Fall through into OP_AggStep */
77566
+}
77567
+case OP_AggStep: {
77568
+ int i;
77569
+ sqlite3_context *pCtx;
77570
+ Mem *pMem;
77571
+ Mem t;
77572
+
77573
+ assert( pOp->p4type==P4_FUNCCTX );
77574
+ pCtx = pOp->p4.pCtx;
77575
+ pMem = &aMem[pOp->p3];
77576
+
77577
+ /* If this function is inside of a trigger, the register array in aMem[]
77578
+ ** might change from one evaluation to the next. The next block of code
77579
+ ** checks to see if the register array has changed, and if so it
77580
+ ** reinitializes the relavant parts of the sqlite3_context object */
77581
+ if( pCtx->pMem != pMem ){
77582
+ pCtx->pMem = pMem;
77583
+ for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
77584
+ }
77585
+
77586
+#ifdef SQLITE_DEBUG
77587
+ for(i=0; i<pCtx->argc; i++){
77588
+ assert( memIsValid(pCtx->argv[i]) );
77589
+ REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
77590
+ }
77591
+#endif
77592
+
7666077593
pMem->n++;
7666177594
sqlite3VdbeMemInit(&t, db, MEM_Null);
76662
- ctx.pOut = &t;
76663
- ctx.isError = 0;
76664
- ctx.pVdbe = p;
76665
- ctx.iOp = (int)(pOp - aOp);
76666
- ctx.skipFlag = 0;
76667
- (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
76668
- if( ctx.isError ){
76669
- sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
76670
- rc = ctx.isError;
76671
- }
76672
- if( ctx.skipFlag ){
77595
+ pCtx->pOut = &t;
77596
+ pCtx->fErrorOrAux = 0;
77597
+ pCtx->skipFlag = 0;
77598
+ (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
77599
+ if( pCtx->fErrorOrAux ){
77600
+ if( pCtx->isError ){
77601
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
77602
+ rc = pCtx->isError;
77603
+ }
77604
+ sqlite3VdbeMemRelease(&t);
77605
+ }else{
77606
+ assert( t.flags==MEM_Null );
77607
+ }
77608
+ if( pCtx->skipFlag ){
7667377609
assert( pOp[-1].opcode==OP_CollSeq );
7667477610
i = pOp[-1].p1;
7667577611
if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
7667677612
}
76677
- sqlite3VdbeMemRelease(&t);
7667877613
break;
7667977614
}
7668077615
7668177616
/* Opcode: AggFinal P1 P2 * P4 *
7668277617
** Synopsis: accum=r[P1] N=P2
@@ -82721,10 +83656,17 @@
8272183656
"the GROUP BY clause");
8272283657
return WRC_Abort;
8272383658
}
8272483659
}
8272583660
}
83661
+
83662
+ /* If this is part of a compound SELECT, check that it has the right
83663
+ ** number of expressions in the select list. */
83664
+ if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
83665
+ sqlite3SelectWrongNumTermsError(pParse, p->pNext);
83666
+ return WRC_Abort;
83667
+ }
8272683668
8272783669
/* Advance to the next term of the compound
8272883670
*/
8272983671
p = p->pPrior;
8273083672
nCompound++;
@@ -83089,17 +84031,17 @@
8308984031
** affinity, use that. Otherwise use no affinity.
8309084032
*/
8309184033
if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
8309284034
return SQLITE_AFF_NUMERIC;
8309384035
}else{
83094
- return SQLITE_AFF_NONE;
84036
+ return SQLITE_AFF_BLOB;
8309584037
}
8309684038
}else if( !aff1 && !aff2 ){
8309784039
/* Neither side of the comparison is a column. Compare the
8309884040
** results directly.
8309984041
*/
83100
- return SQLITE_AFF_NONE;
84042
+ return SQLITE_AFF_BLOB;
8310184043
}else{
8310284044
/* One side is a column, the other is not. Use the columns affinity. */
8310384045
assert( aff1==0 || aff2==0 );
8310484046
return (aff1 + aff2);
8310584047
}
@@ -83119,11 +84061,11 @@
8311984061
if( pExpr->pRight ){
8312084062
aff = sqlite3CompareAffinity(pExpr->pRight, aff);
8312184063
}else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
8312284064
aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
8312384065
}else if( !aff ){
83124
- aff = SQLITE_AFF_NONE;
84066
+ aff = SQLITE_AFF_BLOB;
8312584067
}
8312684068
return aff;
8312784069
}
8312884070
8312984071
/*
@@ -83133,11 +84075,11 @@
8313384075
** the comparison in pExpr.
8313484076
*/
8313584077
SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
8313684078
char aff = comparisonAffinity(pExpr);
8313784079
switch( aff ){
83138
- case SQLITE_AFF_NONE:
84080
+ case SQLITE_AFF_BLOB:
8313984081
return 1;
8314084082
case SQLITE_AFF_TEXT:
8314184083
return idx_affinity==SQLITE_AFF_TEXT;
8314284084
default:
8314384085
return sqlite3IsNumericAffinity(idx_affinity);
@@ -83939,11 +84881,11 @@
8393984881
pNewItem->addrFillSub = pOldItem->addrFillSub;
8394084882
pNewItem->regReturn = pOldItem->regReturn;
8394184883
pNewItem->isCorrelated = pOldItem->isCorrelated;
8394284884
pNewItem->viaCoroutine = pOldItem->viaCoroutine;
8394384885
pNewItem->isRecursive = pOldItem->isRecursive;
83944
- pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
84886
+ pNewItem->zIndexedBy = sqlite3DbStrDup(db, pOldItem->zIndexedBy);
8394584887
pNewItem->notIndexed = pOldItem->notIndexed;
8394684888
pNewItem->pIndex = pOldItem->pIndex;
8394784889
pTab = pNewItem->pTab = pOldItem->pTab;
8394884890
if( pTab ){
8394984891
pTab->nRef++;
@@ -84166,11 +85108,11 @@
8416685108
**
8416785109
** These callback routines are used to implement the following:
8416885110
**
8416985111
** sqlite3ExprIsConstant() pWalker->eCode==1
8417085112
** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
84171
-** sqlite3ExprRefOneTableOnly() pWalker->eCode==3
85113
+** sqlite3ExprIsTableConstant() pWalker->eCode==3
8417285114
** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
8417385115
**
8417485116
** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
8417585117
** is found to not be a constant.
8417685118
**
@@ -84274,11 +85216,11 @@
8427485216
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
8427585217
return exprIsConst(p, 2, 0);
8427685218
}
8427785219
8427885220
/*
84279
-** Walk an expression tree. Return non-zero if the expression constant
85221
+** Walk an expression tree. Return non-zero if the expression is constant
8428085222
** for any single row of the table with cursor iCur. In other words, the
8428185223
** expression must not refer to any non-deterministic function nor any
8428285224
** table other than iCur.
8428385225
*/
8428485226
SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
@@ -84380,11 +85322,11 @@
8438085322
** is harmless. A false positive, however, can result in the wrong
8438185323
** answer.
8438285324
*/
8438385325
SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
8438485326
u8 op;
84385
- if( aff==SQLITE_AFF_NONE ) return 1;
85327
+ if( aff==SQLITE_AFF_BLOB ) return 1;
8438685328
while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
8438785329
op = p->op;
8438885330
if( op==TK_REGISTER ) op = p->op2;
8438985331
switch( op ){
8439085332
case TK_INTEGER: {
@@ -84831,11 +85773,11 @@
8483185773
ExprList *pList = pExpr->x.pList;
8483285774
struct ExprList_item *pItem;
8483385775
int r1, r2, r3;
8483485776
8483585777
if( !affinity ){
84836
- affinity = SQLITE_AFF_NONE;
85778
+ affinity = SQLITE_AFF_BLOB;
8483785779
}
8483885780
if( pKeyInfo ){
8483985781
assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
8484085782
pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
8484185783
}
@@ -85106,21 +86048,10 @@
8510686048
sqlite3ExprCachePop(pParse);
8510786049
VdbeComment((v, "end IN expr"));
8510886050
}
8510986051
#endif /* SQLITE_OMIT_SUBQUERY */
8511086052
85111
-/*
85112
-** Duplicate an 8-byte value
85113
-*/
85114
-static char *dup8bytes(Vdbe *v, const char *in){
85115
- char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
85116
- if( out ){
85117
- memcpy(out, in, 8);
85118
- }
85119
- return out;
85120
-}
85121
-
8512286053
#ifndef SQLITE_OMIT_FLOATING_POINT
8512386054
/*
8512486055
** Generate an instruction that will put the floating point
8512586056
** value described by z[0..n-1] into register iMem.
8512686057
**
@@ -85129,16 +86060,14 @@
8512986060
** like the continuation of the number.
8513086061
*/
8513186062
static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
8513286063
if( ALWAYS(z!=0) ){
8513386064
double value;
85134
- char *zV;
8513586065
sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
8513686066
assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
8513786067
if( negateFlag ) value = -value;
85138
- zV = dup8bytes(v, (char*)&value);
85139
- sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
86068
+ sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
8514086069
}
8514186070
}
8514286071
#endif
8514386072
8514486073
@@ -85160,14 +86089,12 @@
8516086089
i64 value;
8516186090
const char *z = pExpr->u.zToken;
8516286091
assert( z!=0 );
8516386092
c = sqlite3DecOrHexToI64(z, &value);
8516486093
if( c==0 || (c==2 && negFlag) ){
85165
- char *zV;
8516686094
if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
85167
- zV = dup8bytes(v, (char*)&value);
85168
- sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
86095
+ sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
8516986096
}else{
8517086097
#ifdef SQLITE_OMIT_FLOATING_POINT
8517186098
sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
8517286099
#else
8517386100
#ifndef SQLITE_OMIT_HEX_INTEGER
@@ -85768,11 +86695,11 @@
8576886695
/* The UNLIKELY() function is a no-op. The result is the value
8576986696
** of the first argument.
8577086697
*/
8577186698
if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
8577286699
assert( nFarg>=1 );
85773
- sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
86700
+ inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
8577486701
break;
8577586702
}
8577686703
8577786704
for(i=0; i<nFarg; i++){
8577886705
if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
@@ -85838,11 +86765,11 @@
8583886765
#endif
8583986766
if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
8584086767
if( !pColl ) pColl = db->pDfltColl;
8584186768
sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
8584286769
}
85843
- sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
86770
+ sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
8584486771
(char*)pDef, P4_FUNCDEF);
8584586772
sqlite3VdbeChangeP5(v, (u8)nFarg);
8584686773
if( nFarg && constMask==0 ){
8584786774
sqlite3ReleaseTempRange(pParse, r1, nFarg);
8584886775
}
@@ -86209,272 +87136,10 @@
8620987136
iMem = ++pParse->nMem;
8621087137
sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
8621187138
exprToRegister(pExpr, iMem);
8621287139
}
8621387140
86214
-#ifdef SQLITE_DEBUG
86215
-/*
86216
-** Generate a human-readable explanation of an expression tree.
86217
-*/
86218
-SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
86219
- const char *zBinOp = 0; /* Binary operator */
86220
- const char *zUniOp = 0; /* Unary operator */
86221
- pView = sqlite3TreeViewPush(pView, moreToFollow);
86222
- if( pExpr==0 ){
86223
- sqlite3TreeViewLine(pView, "nil");
86224
- sqlite3TreeViewPop(pView);
86225
- return;
86226
- }
86227
- switch( pExpr->op ){
86228
- case TK_AGG_COLUMN: {
86229
- sqlite3TreeViewLine(pView, "AGG{%d:%d}",
86230
- pExpr->iTable, pExpr->iColumn);
86231
- break;
86232
- }
86233
- case TK_COLUMN: {
86234
- if( pExpr->iTable<0 ){
86235
- /* This only happens when coding check constraints */
86236
- sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
86237
- }else{
86238
- sqlite3TreeViewLine(pView, "{%d:%d}",
86239
- pExpr->iTable, pExpr->iColumn);
86240
- }
86241
- break;
86242
- }
86243
- case TK_INTEGER: {
86244
- if( pExpr->flags & EP_IntValue ){
86245
- sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
86246
- }else{
86247
- sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
86248
- }
86249
- break;
86250
- }
86251
-#ifndef SQLITE_OMIT_FLOATING_POINT
86252
- case TK_FLOAT: {
86253
- sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
86254
- break;
86255
- }
86256
-#endif
86257
- case TK_STRING: {
86258
- sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
86259
- break;
86260
- }
86261
- case TK_NULL: {
86262
- sqlite3TreeViewLine(pView,"NULL");
86263
- break;
86264
- }
86265
-#ifndef SQLITE_OMIT_BLOB_LITERAL
86266
- case TK_BLOB: {
86267
- sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
86268
- break;
86269
- }
86270
-#endif
86271
- case TK_VARIABLE: {
86272
- sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
86273
- pExpr->u.zToken, pExpr->iColumn);
86274
- break;
86275
- }
86276
- case TK_REGISTER: {
86277
- sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
86278
- break;
86279
- }
86280
- case TK_AS: {
86281
- sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
86282
- sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
86283
- break;
86284
- }
86285
- case TK_ID: {
86286
- sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
86287
- break;
86288
- }
86289
-#ifndef SQLITE_OMIT_CAST
86290
- case TK_CAST: {
86291
- /* Expressions of the form: CAST(pLeft AS token) */
86292
- sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
86293
- sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
86294
- break;
86295
- }
86296
-#endif /* SQLITE_OMIT_CAST */
86297
- case TK_LT: zBinOp = "LT"; break;
86298
- case TK_LE: zBinOp = "LE"; break;
86299
- case TK_GT: zBinOp = "GT"; break;
86300
- case TK_GE: zBinOp = "GE"; break;
86301
- case TK_NE: zBinOp = "NE"; break;
86302
- case TK_EQ: zBinOp = "EQ"; break;
86303
- case TK_IS: zBinOp = "IS"; break;
86304
- case TK_ISNOT: zBinOp = "ISNOT"; break;
86305
- case TK_AND: zBinOp = "AND"; break;
86306
- case TK_OR: zBinOp = "OR"; break;
86307
- case TK_PLUS: zBinOp = "ADD"; break;
86308
- case TK_STAR: zBinOp = "MUL"; break;
86309
- case TK_MINUS: zBinOp = "SUB"; break;
86310
- case TK_REM: zBinOp = "REM"; break;
86311
- case TK_BITAND: zBinOp = "BITAND"; break;
86312
- case TK_BITOR: zBinOp = "BITOR"; break;
86313
- case TK_SLASH: zBinOp = "DIV"; break;
86314
- case TK_LSHIFT: zBinOp = "LSHIFT"; break;
86315
- case TK_RSHIFT: zBinOp = "RSHIFT"; break;
86316
- case TK_CONCAT: zBinOp = "CONCAT"; break;
86317
- case TK_DOT: zBinOp = "DOT"; break;
86318
-
86319
- case TK_UMINUS: zUniOp = "UMINUS"; break;
86320
- case TK_UPLUS: zUniOp = "UPLUS"; break;
86321
- case TK_BITNOT: zUniOp = "BITNOT"; break;
86322
- case TK_NOT: zUniOp = "NOT"; break;
86323
- case TK_ISNULL: zUniOp = "ISNULL"; break;
86324
- case TK_NOTNULL: zUniOp = "NOTNULL"; break;
86325
-
86326
- case TK_COLLATE: {
86327
- sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
86328
- sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
86329
- break;
86330
- }
86331
-
86332
- case TK_AGG_FUNCTION:
86333
- case TK_FUNCTION: {
86334
- ExprList *pFarg; /* List of function arguments */
86335
- if( ExprHasProperty(pExpr, EP_TokenOnly) ){
86336
- pFarg = 0;
86337
- }else{
86338
- pFarg = pExpr->x.pList;
86339
- }
86340
- if( pExpr->op==TK_AGG_FUNCTION ){
86341
- sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
86342
- pExpr->op2, pExpr->u.zToken);
86343
- }else{
86344
- sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
86345
- }
86346
- if( pFarg ){
86347
- sqlite3TreeViewExprList(pView, pFarg, 0, 0);
86348
- }
86349
- break;
86350
- }
86351
-#ifndef SQLITE_OMIT_SUBQUERY
86352
- case TK_EXISTS: {
86353
- sqlite3TreeViewLine(pView, "EXISTS-expr");
86354
- sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
86355
- break;
86356
- }
86357
- case TK_SELECT: {
86358
- sqlite3TreeViewLine(pView, "SELECT-expr");
86359
- sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
86360
- break;
86361
- }
86362
- case TK_IN: {
86363
- sqlite3TreeViewLine(pView, "IN");
86364
- sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
86365
- if( ExprHasProperty(pExpr, EP_xIsSelect) ){
86366
- sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
86367
- }else{
86368
- sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
86369
- }
86370
- break;
86371
- }
86372
-#endif /* SQLITE_OMIT_SUBQUERY */
86373
-
86374
- /*
86375
- ** x BETWEEN y AND z
86376
- **
86377
- ** This is equivalent to
86378
- **
86379
- ** x>=y AND x<=z
86380
- **
86381
- ** X is stored in pExpr->pLeft.
86382
- ** Y is stored in pExpr->pList->a[0].pExpr.
86383
- ** Z is stored in pExpr->pList->a[1].pExpr.
86384
- */
86385
- case TK_BETWEEN: {
86386
- Expr *pX = pExpr->pLeft;
86387
- Expr *pY = pExpr->x.pList->a[0].pExpr;
86388
- Expr *pZ = pExpr->x.pList->a[1].pExpr;
86389
- sqlite3TreeViewLine(pView, "BETWEEN");
86390
- sqlite3TreeViewExpr(pView, pX, 1);
86391
- sqlite3TreeViewExpr(pView, pY, 1);
86392
- sqlite3TreeViewExpr(pView, pZ, 0);
86393
- break;
86394
- }
86395
- case TK_TRIGGER: {
86396
- /* If the opcode is TK_TRIGGER, then the expression is a reference
86397
- ** to a column in the new.* or old.* pseudo-tables available to
86398
- ** trigger programs. In this case Expr.iTable is set to 1 for the
86399
- ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
86400
- ** is set to the column of the pseudo-table to read, or to -1 to
86401
- ** read the rowid field.
86402
- */
86403
- sqlite3TreeViewLine(pView, "%s(%d)",
86404
- pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
86405
- break;
86406
- }
86407
- case TK_CASE: {
86408
- sqlite3TreeViewLine(pView, "CASE");
86409
- sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
86410
- sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
86411
- break;
86412
- }
86413
-#ifndef SQLITE_OMIT_TRIGGER
86414
- case TK_RAISE: {
86415
- const char *zType = "unk";
86416
- switch( pExpr->affinity ){
86417
- case OE_Rollback: zType = "rollback"; break;
86418
- case OE_Abort: zType = "abort"; break;
86419
- case OE_Fail: zType = "fail"; break;
86420
- case OE_Ignore: zType = "ignore"; break;
86421
- }
86422
- sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
86423
- break;
86424
- }
86425
-#endif
86426
- default: {
86427
- sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
86428
- break;
86429
- }
86430
- }
86431
- if( zBinOp ){
86432
- sqlite3TreeViewLine(pView, "%s", zBinOp);
86433
- sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
86434
- sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
86435
- }else if( zUniOp ){
86436
- sqlite3TreeViewLine(pView, "%s", zUniOp);
86437
- sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
86438
- }
86439
- sqlite3TreeViewPop(pView);
86440
-}
86441
-#endif /* SQLITE_DEBUG */
86442
-
86443
-#ifdef SQLITE_DEBUG
86444
-/*
86445
-** Generate a human-readable explanation of an expression list.
86446
-*/
86447
-SQLITE_PRIVATE void sqlite3TreeViewExprList(
86448
- TreeView *pView,
86449
- const ExprList *pList,
86450
- u8 moreToFollow,
86451
- const char *zLabel
86452
-){
86453
- int i;
86454
- pView = sqlite3TreeViewPush(pView, moreToFollow);
86455
- if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
86456
- if( pList==0 ){
86457
- sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
86458
- }else{
86459
- sqlite3TreeViewLine(pView, "%s", zLabel);
86460
- for(i=0; i<pList->nExpr; i++){
86461
- sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
86462
-#if 0
86463
- if( pList->a[i].zName ){
86464
- sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
86465
- }
86466
- if( pList->a[i].bSpanIsTab ){
86467
- sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
86468
- }
86469
-#endif
86470
- }
86471
- }
86472
- sqlite3TreeViewPop(pView);
86473
-}
86474
-#endif /* SQLITE_DEBUG */
86475
-
8647687141
/*
8647787142
** Generate code that pushes the value of every element of the given
8647887143
** expression list into a sequence of registers beginning at target.
8647987144
**
8648087145
** Return the number of elements evaluated.
@@ -86861,10 +87526,25 @@
8686187526
}
8686287527
}
8686387528
sqlite3ReleaseTempReg(pParse, regFree1);
8686487529
sqlite3ReleaseTempReg(pParse, regFree2);
8686587530
}
87531
+
87532
+/*
87533
+** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
87534
+** code generation, and that copy is deleted after code generation. This
87535
+** ensures that the original pExpr is unchanged.
87536
+*/
87537
+SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
87538
+ sqlite3 *db = pParse->db;
87539
+ Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
87540
+ if( db->mallocFailed==0 ){
87541
+ sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
87542
+ }
87543
+ sqlite3ExprDelete(db, pCopy);
87544
+}
87545
+
8686687546
8686787547
/*
8686887548
** Do a deep comparison of two expression trees. Return 0 if the two
8686987549
** expressions are completely identical. Return 1 if they differ only
8687087550
** by a COLLATE operator at the top level. Return 2 if there are differences
@@ -88014,11 +88694,11 @@
8801488694
** can handle (i.e. not CURRENT_TIME etc.)
8801588695
*/
8801688696
if( pDflt ){
8801788697
sqlite3_value *pVal = 0;
8801888698
int rc;
88019
- rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal);
88699
+ rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
8802088700
assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
8802188701
if( rc!=SQLITE_OK ){
8802288702
db->mallocFailed = 1;
8802388703
return;
8802488704
}
@@ -89099,11 +89779,11 @@
8909989779
#elif SQLITE_DEBUG
8910089780
assert( iParam==STAT_GET_STAT1 );
8910189781
#else
8910289782
UNUSED_PARAMETER( iParam );
8910389783
#endif
89104
- sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
89784
+ sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4, regOut);
8910589785
sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
8910689786
sqlite3VdbeChangeP5(v, 1 + IsStat34);
8910789787
}
8910889788
8910989789
/*
@@ -89254,11 +89934,11 @@
8925489934
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8925589935
sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
8925689936
#endif
8925789937
sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
8925889938
sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
89259
- sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
89939
+ sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4+1, regStat4);
8926089940
sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
8926189941
sqlite3VdbeChangeP5(v, 2+IsStat34);
8926289942
8926389943
/* Implementation of the following:
8926489944
**
@@ -89350,11 +90030,11 @@
8935090030
sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
8935190031
sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
8935290032
}
8935390033
#endif
8935490034
assert( regChng==(regStat4+1) );
89355
- sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
90035
+ sqlite3VdbeAddOp3(v, OP_Function0, 1, regStat4, regTemp);
8935690036
sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
8935790037
sqlite3VdbeChangeP5(v, 2+IsStat34);
8935890038
sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
8935990039
8936090040
/* Add the entry to the stat1 table. */
@@ -90409,11 +91089,11 @@
9040991089
sqlite3ExprCode(pParse, pDbname, regArgs+1);
9041091090
sqlite3ExprCode(pParse, pKey, regArgs+2);
9041191091
9041291092
assert( v || db->mallocFailed );
9041391093
if( v ){
90414
- sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
91094
+ sqlite3VdbeAddOp3(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3);
9041591095
assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
9041691096
sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
9041791097
sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
9041891098
9041991099
/* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
@@ -91874,11 +92554,11 @@
9187492554
*/
9187592555
if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
9187692556
int j1;
9187792557
int fileFormat;
9187892558
int reg1, reg2, reg3;
91879
- sqlite3BeginWriteOperation(pParse, 0, iDb);
92559
+ sqlite3BeginWriteOperation(pParse, 1, iDb);
9188092560
9188192561
#ifndef SQLITE_OMIT_VIRTUALTABLE
9188292562
if( isVirtual ){
9188392563
sqlite3VdbeAddOp0(v, OP_VBegin);
9188492564
}
@@ -91990,14 +92670,14 @@
9199092670
pCol = &p->aCol[p->nCol];
9199192671
memset(pCol, 0, sizeof(p->aCol[0]));
9199292672
pCol->zName = z;
9199392673
9199492674
/* If there is no type specified, columns have the default affinity
91995
- ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
92675
+ ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will
9199692676
** be called next to set pCol->affinity correctly.
9199792677
*/
91998
- pCol->affinity = SQLITE_AFF_NONE;
92678
+ pCol->affinity = SQLITE_AFF_BLOB;
9199992679
pCol->szEst = 1;
9200092680
p->nCol++;
9200192681
}
9200292682
9200392683
/*
@@ -92028,11 +92708,11 @@
9202892708
** --------------------------------
9202992709
** 'INT' | SQLITE_AFF_INTEGER
9203092710
** 'CHAR' | SQLITE_AFF_TEXT
9203192711
** 'CLOB' | SQLITE_AFF_TEXT
9203292712
** 'TEXT' | SQLITE_AFF_TEXT
92033
-** 'BLOB' | SQLITE_AFF_NONE
92713
+** 'BLOB' | SQLITE_AFF_BLOB
9203492714
** 'REAL' | SQLITE_AFF_REAL
9203592715
** 'FLOA' | SQLITE_AFF_REAL
9203692716
** 'DOUB' | SQLITE_AFF_REAL
9203792717
**
9203892718
** If none of the substrings in the above table are found,
@@ -92054,11 +92734,11 @@
9205492734
aff = SQLITE_AFF_TEXT;
9205592735
}else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
9205692736
aff = SQLITE_AFF_TEXT;
9205792737
}else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
9205892738
&& (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
92059
- aff = SQLITE_AFF_NONE;
92739
+ aff = SQLITE_AFF_BLOB;
9206092740
if( zIn[0]=='(' ) zChar = zIn;
9206192741
#ifndef SQLITE_OMIT_FLOATING_POINT
9206292742
}else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
9206392743
&& aff==SQLITE_AFF_NUMERIC ){
9206492744
aff = SQLITE_AFF_REAL;
@@ -92446,11 +93126,11 @@
9244693126
k = sqlite3Strlen30(zStmt);
9244793127
identPut(zStmt, &k, p->zName);
9244893128
zStmt[k++] = '(';
9244993129
for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
9245093130
static const char * const azType[] = {
92451
- /* SQLITE_AFF_NONE */ "",
93131
+ /* SQLITE_AFF_BLOB */ "",
9245293132
/* SQLITE_AFF_TEXT */ " TEXT",
9245393133
/* SQLITE_AFF_NUMERIC */ " NUM",
9245493134
/* SQLITE_AFF_INTEGER */ " INT",
9245593135
/* SQLITE_AFF_REAL */ " REAL"
9245693136
};
@@ -92459,21 +93139,21 @@
9245993139
9246093140
sqlite3_snprintf(n-k, &zStmt[k], zSep);
9246193141
k += sqlite3Strlen30(&zStmt[k]);
9246293142
zSep = zSep2;
9246393143
identPut(zStmt, &k, pCol->zName);
92464
- assert( pCol->affinity-SQLITE_AFF_NONE >= 0 );
92465
- assert( pCol->affinity-SQLITE_AFF_NONE < ArraySize(azType) );
92466
- testcase( pCol->affinity==SQLITE_AFF_NONE );
93144
+ assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
93145
+ assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
93146
+ testcase( pCol->affinity==SQLITE_AFF_BLOB );
9246793147
testcase( pCol->affinity==SQLITE_AFF_TEXT );
9246893148
testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
9246993149
testcase( pCol->affinity==SQLITE_AFF_INTEGER );
9247093150
testcase( pCol->affinity==SQLITE_AFF_REAL );
9247193151
92472
- zType = azType[pCol->affinity - SQLITE_AFF_NONE];
93152
+ zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
9247393153
len = sqlite3Strlen30(zType);
92474
- assert( pCol->affinity==SQLITE_AFF_NONE
93154
+ assert( pCol->affinity==SQLITE_AFF_BLOB
9247593155
|| pCol->affinity==sqlite3AffinityType(zType, 0) );
9247693156
memcpy(&zStmt[k], zType, len);
9247793157
k += len;
9247893158
assert( k<=n );
9247993159
}
@@ -92822,10 +93502,11 @@
9282293502
9282393503
regYield = ++pParse->nMem;
9282493504
regRec = ++pParse->nMem;
9282593505
regRowid = ++pParse->nMem;
9282693506
assert(pParse->nTab==1);
93507
+ sqlite3MayAbort(pParse);
9282793508
sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
9282893509
sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
9282993510
pParse->nTab = 2;
9283093511
addrTop = sqlite3VdbeCurrentAddr(v) + 1;
9283193512
sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
@@ -94599,11 +95280,11 @@
9459995280
if( pList==0 ) return;
9460095281
for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
9460195282
sqlite3DbFree(db, pItem->zDatabase);
9460295283
sqlite3DbFree(db, pItem->zName);
9460395284
sqlite3DbFree(db, pItem->zAlias);
94604
- sqlite3DbFree(db, pItem->zIndex);
95285
+ sqlite3DbFree(db, pItem->zIndexedBy);
9460595286
sqlite3DeleteTable(db, pItem->pTab);
9460695287
sqlite3SelectDelete(db, pItem->pSelect);
9460795288
sqlite3ExprDelete(db, pItem->pOn);
9460895289
sqlite3IdListDelete(db, pItem->pUsing);
9460995290
}
@@ -94672,17 +95353,17 @@
9467295353
*/
9467395354
SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
9467495355
assert( pIndexedBy!=0 );
9467595356
if( p && ALWAYS(p->nSrc>0) ){
9467695357
struct SrcList_item *pItem = &p->a[p->nSrc-1];
94677
- assert( pItem->notIndexed==0 && pItem->zIndex==0 );
95358
+ assert( pItem->notIndexed==0 && pItem->zIndexedBy==0 );
9467895359
if( pIndexedBy->n==1 && !pIndexedBy->z ){
9467995360
/* A "NOT INDEXED" clause was supplied. See parse.y
9468095361
** construct "indexed_opt" for details. */
9468195362
pItem->notIndexed = 1;
9468295363
}else{
94683
- pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
95364
+ pItem->zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
9468495365
}
9468595366
}
9468695367
}
9468795368
9468895369
/*
@@ -96502,12 +97183,12 @@
9650297183
if( piPartIdxLabel ){
9650397184
if( pIdx->pPartIdxWhere ){
9650497185
*piPartIdxLabel = sqlite3VdbeMakeLabel(v);
9650597186
pParse->iPartIdxTab = iDataCur;
9650697187
sqlite3ExprCachePush(pParse);
96507
- sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
96508
- SQLITE_JUMPIFNULL);
97188
+ sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
97189
+ SQLITE_JUMPIFNULL);
9650997190
}else{
9651097191
*piPartIdxLabel = 0;
9651197192
}
9651297193
}
9651397194
nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
@@ -97119,21 +97800,19 @@
9711997800
u8 noCase;
9712097801
};
9712197802
9712297803
/*
9712397804
** For LIKE and GLOB matching on EBCDIC machines, assume that every
97124
-** character is exactly one byte in size. Also, all characters are
97125
-** able to participate in upper-case-to-lower-case mappings in EBCDIC
97126
-** whereas only characters less than 0x80 do in ASCII.
97805
+** character is exactly one byte in size. Also, provde the Utf8Read()
97806
+** macro for fast reading of the next character in the common case where
97807
+** the next character is ASCII.
9712797808
*/
9712897809
#if defined(SQLITE_EBCDIC)
9712997810
# define sqlite3Utf8Read(A) (*((*A)++))
97130
-# define GlobUpperToLower(A) A = sqlite3UpperToLower[A]
97131
-# define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A]
97811
+# define Utf8Read(A) (*(A++))
9713297812
#else
97133
-# define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; }
97134
-# define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A]
97813
+# define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
9713597814
#endif
9713697815
9713797816
static const struct compareInfo globInfo = { '*', '?', '[', 0 };
9713897817
/* The correct SQL-92 behavior is for the LIKE operator to ignore
9713997818
** case. Thus 'a' LIKE 'A' would be true. */
@@ -97171,11 +97850,11 @@
9717197850
*** '_' Matches any one character
9717297851
**
9717397852
** Ec Where E is the "esc" character and c is any other
9717497853
** character, including '%', '_', and esc, match exactly c.
9717597854
**
97176
-** The comments through this routine usually assume glob matching.
97855
+** The comments within this routine usually assume glob matching.
9717797856
**
9717897857
** This routine is usually quick, but can be N**2 in the worst case.
9717997858
*/
9718097859
static int patternCompare(
9718197860
const u8 *zPattern, /* The glob pattern */
@@ -97195,17 +97874,16 @@
9719597874
** the other, never both. Hence the single variable matchOther is used
9719697875
** to store the one we have to look for.
9719797876
*/
9719897877
matchOther = esc ? esc : pInfo->matchSet;
9719997878
97200
- while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
97879
+ while( (c = Utf8Read(zPattern))!=0 ){
9720197880
if( c==matchAll ){ /* Match "*" */
9720297881
/* Skip over multiple "*" characters in the pattern. If there
9720397882
** are also "?" characters, skip those as well, but consume a
9720497883
** single character of the input string for each "?" skipped */
97205
- while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
97206
- || c == matchOne ){
97884
+ while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
9720797885
if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
9720897886
return 0;
9720997887
}
9721097888
}
9721197889
if( c==0 ){
@@ -97246,11 +97924,11 @@
9724697924
while( (c2 = *(zString++))!=0 ){
9724797925
if( c2!=c && c2!=cx ) continue;
9724897926
if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
9724997927
}
9725097928
}else{
97251
- while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
97929
+ while( (c2 = Utf8Read(zString))!=0 ){
9725297930
if( c2!=c ) continue;
9725397931
if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
9725497932
}
9725597933
}
9725697934
return 0;
@@ -97292,11 +97970,11 @@
9729297970
return 0;
9729397971
}
9729497972
continue;
9729597973
}
9729697974
}
97297
- c2 = sqlite3Utf8Read(&zString);
97975
+ c2 = Utf8Read(zString);
9729897976
if( c==c2 ) continue;
9729997977
if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
9730097978
continue;
9730197979
}
9730297980
if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
@@ -99806,11 +100484,11 @@
99806100484
** pIdx. A column affinity string has one character for each column in
99807100485
** the table, according to the affinity of the column:
99808100486
**
99809100487
** Character Column affinity
99810100488
** ------------------------------
99811
-** 'A' NONE
100489
+** 'A' BLOB
99812100490
** 'B' TEXT
99813100491
** 'C' NUMERIC
99814100492
** 'D' INTEGER
99815100493
** 'F' REAL
99816100494
**
@@ -99849,23 +100527,23 @@
99849100527
return pIdx->zColAff;
99850100528
}
99851100529
99852100530
/*
99853100531
** Compute the affinity string for table pTab, if it has not already been
99854
-** computed. As an optimization, omit trailing SQLITE_AFF_NONE affinities.
100532
+** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
99855100533
**
99856
-** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and
100534
+** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
99857100535
** if iReg>0 then code an OP_Affinity opcode that will set the affinities
99858100536
** for register iReg and following. Or if affinities exists and iReg==0,
99859100537
** then just set the P4 operand of the previous opcode (which should be
99860100538
** an OP_MakeRecord) to the affinity string.
99861100539
**
99862100540
** A column affinity string has one character per column:
99863100541
**
99864100542
** Character Column affinity
99865100543
** ------------------------------
99866
-** 'A' NONE
100544
+** 'A' BLOB
99867100545
** 'B' TEXT
99868100546
** 'C' NUMERIC
99869100547
** 'D' INTEGER
99870100548
** 'E' REAL
99871100549
*/
@@ -99883,11 +100561,11 @@
99883100561
for(i=0; i<pTab->nCol; i++){
99884100562
zColAff[i] = pTab->aCol[i].affinity;
99885100563
}
99886100564
do{
99887100565
zColAff[i--] = 0;
99888
- }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
100566
+ }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
99889100567
pTab->zColAff = zColAff;
99890100568
}
99891100569
i = sqlite3Strlen30(zColAff);
99892100570
if( i ){
99893100571
if( iReg ){
@@ -101131,12 +101809,12 @@
101131101809
101132101810
/* Skip partial indices for which the WHERE clause is not true */
101133101811
if( pIdx->pPartIdxWhere ){
101134101812
sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
101135101813
pParse->ckBase = regNewData+1;
101136
- sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
101137
- SQLITE_JUMPIFNULL);
101814
+ sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
101815
+ SQLITE_JUMPIFNULL);
101138101816
pParse->ckBase = 0;
101139101817
}
101140101818
101141101819
/* Create a record for this index entry as it should appear after
101142101820
** the insert or update. Store that record in the aRegIdx[ix] register
@@ -102242,11 +102920,12 @@
102242102920
void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
102243102921
void(*)(void*));
102244102922
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
102245102923
void(*)(void*), unsigned char);
102246102924
int (*strglob)(const char*,const char*);
102247
- sqlite3_value (*value_dup)(const sqlite3_value*);
102925
+ /* Version 3.8.11 and later */
102926
+ sqlite3_value *(*value_dup)(const sqlite3_value*);
102248102927
void (*value_free)(sqlite3_value*);
102249102928
};
102250102929
102251102930
/*
102252102931
** The following macros redefine the API routines so that they are
@@ -102882,11 +103561,14 @@
102882103561
sqlite3_msize,
102883103562
sqlite3_realloc64,
102884103563
sqlite3_reset_auto_extension,
102885103564
sqlite3_result_blob64,
102886103565
sqlite3_result_text64,
102887
- sqlite3_strglob
103566
+ sqlite3_strglob,
103567
+ /* Version 3.8.11 and later */
103568
+ (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
103569
+ sqlite3_value_free
102888103570
};
102889103571
102890103572
/*
102891103573
** Attempt to load an SQLite extension library contained in the file
102892103574
** zFile. The entry point is zProc. zProc may be 0 in which case a
@@ -106617,11 +107299,12 @@
106617107299
*/
106618107300
#if SELECTTRACE_ENABLED
106619107301
/***/ int sqlite3SelectTrace = 0;
106620107302
# define SELECTTRACE(K,P,S,X) \
106621107303
if(sqlite3SelectTrace&(K)) \
106622
- sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\
107304
+ sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
107305
+ (S)->zSelName,(S)),\
106623107306
sqlite3DebugPrintf X
106624107307
#else
106625107308
# define SELECTTRACE(K,P,S,X)
106626107309
#endif
106627107310
@@ -106961,10 +107644,16 @@
106961107644
while( p ){
106962107645
ExprSetProperty(p, EP_FromJoin);
106963107646
assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
106964107647
ExprSetVVAProperty(p, EP_NoReduce);
106965107648
p->iRightJoinTable = (i16)iTable;
107649
+ if( p->op==TK_FUNCTION && p->x.pList ){
107650
+ int i;
107651
+ for(i=0; i<p->x.pList->nExpr; i++){
107652
+ setJoinExpr(p->x.pList->a[i].pExpr, iTable);
107653
+ }
107654
+ }
106966107655
setJoinExpr(p->pLeft, iTable);
106967107656
p = p->pRight;
106968107657
}
106969107658
}
106970107659
@@ -107370,11 +108059,12 @@
107370108059
break;
107371108060
}
107372108061
107373108062
default: {
107374108063
assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
107375
- codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
108064
+ codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
108065
+ regResult);
107376108066
break;
107377108067
}
107378108068
}
107379108069
if( pSort==0 ){
107380108070
codeOffset(v, p->iOffset, iContinue);
@@ -107423,11 +108113,12 @@
107423108113
** on an ephemeral index. If the current row is already present
107424108114
** in the index, do not write it to the output. If not, add the
107425108115
** current row to the index and proceed with writing it to the
107426108116
** output table as well. */
107427108117
int addr = sqlite3VdbeCurrentAddr(v) + 4;
107428
- sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
108118
+ sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
108119
+ VdbeCoverage(v);
107429108120
sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
107430108121
assert( pSort==0 );
107431108122
}
107432108123
#endif
107433108124
if( pSort ){
@@ -107906,32 +108597,31 @@
107906108597
** This routine has either 3 or 6 parameters depending on whether or not
107907108598
** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
107908108599
*/
107909108600
#ifdef SQLITE_ENABLE_COLUMN_METADATA
107910108601
# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
108602
+#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
108603
+# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
108604
+#endif
107911108605
static const char *columnTypeImpl(
107912108606
NameContext *pNC,
107913108607
Expr *pExpr,
108608
+#ifdef SQLITE_ENABLE_COLUMN_METADATA
107914108609
const char **pzOrigDb,
107915108610
const char **pzOrigTab,
107916108611
const char **pzOrigCol,
108612
+#endif
107917108613
u8 *pEstWidth
107918108614
){
108615
+ char const *zType = 0;
108616
+ int j;
108617
+ u8 estWidth = 1;
108618
+#ifdef SQLITE_ENABLE_COLUMN_METADATA
107919108619
char const *zOrigDb = 0;
107920108620
char const *zOrigTab = 0;
107921108621
char const *zOrigCol = 0;
107922
-#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
107923
-# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
107924
-static const char *columnTypeImpl(
107925
- NameContext *pNC,
107926
- Expr *pExpr,
107927
- u8 *pEstWidth
107928
-){
107929
-#endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
107930
- char const *zType = 0;
107931
- int j;
107932
- u8 estWidth = 1;
108622
+#endif
107933108623
107934108624
if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
107935108625
switch( pExpr->op ){
107936108626
case TK_AGG_COLUMN:
107937108627
case TK_COLUMN: {
@@ -107980,14 +108670,17 @@
107980108670
if( pS ){
107981108671
/* The "table" is actually a sub-select or a view in the FROM clause
107982108672
** of the SELECT statement. Return the declaration type and origin
107983108673
** data for the result-set column of the sub-select.
107984108674
*/
107985
- if( iCol>=0 && iCol<pS->pEList->nExpr ){
108675
+ if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
107986108676
/* If iCol is less than zero, then the expression requests the
107987108677
** rowid of the sub-select or view. This expression is legal (see
107988108678
** test case misc2.2.2) - it always evaluates to NULL.
108679
+ **
108680
+ ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been
108681
+ ** caught already by name resolution.
107989108682
*/
107990108683
NameContext sNC;
107991108684
Expr *p = pS->pEList->a[iCol].pExpr;
107992108685
sNC.pSrcList = pS->pSrc;
107993108686
sNC.pNext = pNC;
@@ -108301,15 +108994,16 @@
108301108994
sNC.pSrcList = pSelect->pSrc;
108302108995
a = pSelect->pEList->a;
108303108996
for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
108304108997
p = a[i].pExpr;
108305108998
if( pCol->zType==0 ){
108306
- pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
108999
+ pCol->zType = sqlite3DbStrDup(db,
109000
+ columnType(&sNC, p,0,0,0, &pCol->szEst));
108307109001
}
108308109002
szAll += pCol->szEst;
108309109003
pCol->affinity = sqlite3ExprAffinity(p);
108310
- if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
109004
+ if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
108311109005
pColl = sqlite3ExprCollSeq(pParse, p);
108312109006
if( pColl && pCol->zColl==0 ){
108313109007
pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
108314109008
}
108315109009
}
@@ -108461,11 +109155,14 @@
108461109155
pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
108462109156
}else{
108463109157
pRet = 0;
108464109158
}
108465109159
assert( iCol>=0 );
108466
- if( pRet==0 && iCol<p->pEList->nExpr ){
109160
+ /* iCol must be less than p->pEList->nExpr. Otherwise an error would
109161
+ ** have been thrown during name resolution and we would not have gotten
109162
+ ** this far */
109163
+ if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
108467109164
pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
108468109165
}
108469109166
return pRet;
108470109167
}
108471109168
@@ -108680,11 +109377,11 @@
108680109377
108681109378
/*
108682109379
** Error message for when two or more terms of a compound select have different
108683109380
** size result sets.
108684109381
*/
108685
-static void selectWrongNumTermsError(Parse *pParse, Select *p){
109382
+SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
108686109383
if( p->selFlags & SF_Values ){
108687109384
sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
108688109385
}else{
108689109386
sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
108690109387
" do not have the same number of result columns", selectOpName(p->op));
@@ -108706,23 +109403,19 @@
108706109403
Parse *pParse, /* Parsing context */
108707109404
Select *p, /* The right-most of SELECTs to be coded */
108708109405
SelectDest *pDest /* What to do with query results */
108709109406
){
108710109407
Select *pPrior;
108711
- int nExpr = p->pEList->nExpr;
108712109408
int nRow = 1;
108713109409
int rc = 0;
108714109410
assert( p->selFlags & SF_MultiValue );
108715109411
do{
108716109412
assert( p->selFlags & SF_Values );
108717109413
assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
108718109414
assert( p->pLimit==0 );
108719109415
assert( p->pOffset==0 );
108720
- if( p->pEList->nExpr!=nExpr ){
108721
- selectWrongNumTermsError(pParse, p);
108722
- return 1;
108723
- }
109416
+ assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
108724109417
if( p->pPrior==0 ) break;
108725109418
assert( p->pPrior->pNext==p );
108726109419
p = p->pPrior;
108727109420
nRow++;
108728109421
}while(1);
@@ -108827,15 +109520,11 @@
108827109520
108828109521
/* Make sure all SELECTs in the statement have the same number of elements
108829109522
** in their result sets.
108830109523
*/
108831109524
assert( p->pEList && pPrior->pEList );
108832
- if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
108833
- selectWrongNumTermsError(pParse, p);
108834
- rc = 1;
108835
- goto multi_select_end;
108836
- }
109525
+ assert( p->pEList->nExpr==pPrior->pEList->nExpr );
108837109526
108838109527
#ifndef SQLITE_OMIT_CTE
108839109528
if( p->selFlags & SF_Recursive ){
108840109529
generateWithRecursiveQuery(pParse, p, &dest);
108841109530
}else
@@ -109450,13 +110139,11 @@
109450110139
aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
109451110140
if( aPermute ){
109452110141
struct ExprList_item *pItem;
109453110142
for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
109454110143
assert( pItem->u.x.iOrderByCol>0 );
109455
- /* assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ) is also true
109456
- ** but only for well-formed SELECT statements. */
109457
- testcase( pItem->u.x.iOrderByCol > p->pEList->nExpr );
110144
+ assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
109458110145
aPermute[i] = pItem->u.x.iOrderByCol - 1;
109459110146
}
109460110147
pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
109461110148
}else{
109462110149
pKeyMerge = 0;
@@ -109811,12 +110498,12 @@
109811110498
** (9) The subquery does not use LIMIT or the outer query does not use
109812110499
** aggregates.
109813110500
**
109814110501
** (**) Restriction (10) was removed from the code on 2005-02-05 but we
109815110502
** accidently carried the comment forward until 2014-09-15. Original
109816
-** text: "The subquery does not use aggregates or the outer query does not
109817
-** use LIMIT."
110503
+** text: "The subquery does not use aggregates or the outer query
110504
+** does not use LIMIT."
109818110505
**
109819110506
** (11) The subquery and the outer query do not both have ORDER BY clauses.
109820110507
**
109821110508
** (**) Not implemented. Subsumed into restriction (3). Was previously
109822110509
** a separate restriction deriving from ticket #350.
@@ -110022,14 +110709,14 @@
110022110709
}
110023110710
for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
110024110711
testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
110025110712
testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
110026110713
assert( pSub->pSrc!=0 );
110714
+ assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
110027110715
if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
110028110716
|| (pSub1->pPrior && pSub1->op!=TK_ALL)
110029110717
|| pSub1->pSrc->nSrc<1
110030
- || pSub->pEList->nExpr!=pSub1->pEList->nExpr
110031110718
){
110032110719
return 0;
110033110720
}
110034110721
testcase( pSub1->pSrc->nSrc>1 );
110035110722
}
@@ -110305,16 +110992,83 @@
110305110992
*/
110306110993
sqlite3SelectDelete(db, pSub1);
110307110994
110308110995
#if SELECTTRACE_ENABLED
110309110996
if( sqlite3SelectTrace & 0x100 ){
110310
- sqlite3DebugPrintf("After flattening:\n");
110997
+ SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
110311110998
sqlite3TreeViewSelect(0, p, 0);
110312110999
}
110313111000
#endif
110314111001
110315111002
return 1;
111003
+}
111004
+#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
111005
+
111006
+
111007
+
111008
+#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
111009
+/*
111010
+** Make copies of relevant WHERE clause terms of the outer query into
111011
+** the WHERE clause of subquery. Example:
111012
+**
111013
+** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
111014
+**
111015
+** Transformed into:
111016
+**
111017
+** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
111018
+** WHERE x=5 AND y=10;
111019
+**
111020
+** The hope is that the terms added to the inner query will make it more
111021
+** efficient.
111022
+**
111023
+** Do not attempt this optimization if:
111024
+**
111025
+** (1) The inner query is an aggregate. (In that case, we'd really want
111026
+** to copy the outer WHERE-clause terms onto the HAVING clause of the
111027
+** inner query. But they probably won't help there so do not bother.)
111028
+**
111029
+** (2) The inner query is the recursive part of a common table expression.
111030
+**
111031
+** (3) The inner query has a LIMIT clause (since the changes to the WHERE
111032
+** close would change the meaning of the LIMIT).
111033
+**
111034
+** (4) The inner query is the right operand of a LEFT JOIN. (The caller
111035
+** enforces this restriction since this routine does not have enough
111036
+** information to know.)
111037
+**
111038
+** Return 0 if no changes are made and non-zero if one or more WHERE clause
111039
+** terms are duplicated into the subquery.
111040
+*/
111041
+static int pushDownWhereTerms(
111042
+ sqlite3 *db, /* The database connection (for malloc()) */
111043
+ Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
111044
+ Expr *pWhere, /* The WHERE clause of the outer query */
111045
+ int iCursor /* Cursor number of the subquery */
111046
+){
111047
+ Expr *pNew;
111048
+ int nChng = 0;
111049
+ if( pWhere==0 ) return 0;
111050
+ if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
111051
+ return 0; /* restrictions (1) and (2) */
111052
+ }
111053
+ if( pSubq->pLimit!=0 ){
111054
+ return 0; /* restriction (3) */
111055
+ }
111056
+ while( pWhere->op==TK_AND ){
111057
+ nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor);
111058
+ pWhere = pWhere->pLeft;
111059
+ }
111060
+ if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
111061
+ nChng++;
111062
+ while( pSubq ){
111063
+ pNew = sqlite3ExprDup(db, pWhere, 0);
111064
+ pNew = substExpr(db, pNew, iCursor, pSubq->pEList);
111065
+ pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew);
111066
+ pSubq = pSubq->pPrior;
111067
+ }
111068
+ }
111069
+ return nChng;
110316111070
}
110317111071
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
110318111072
110319111073
/*
110320111074
** Based on the contents of the AggInfo structure indicated by the first
@@ -110397,20 +111151,20 @@
110397111151
** was such a clause and the named index cannot be found, return
110398111152
** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
110399111153
** pFrom->pIndex and return SQLITE_OK.
110400111154
*/
110401111155
SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
110402
- if( pFrom->pTab && pFrom->zIndex ){
111156
+ if( pFrom->pTab && pFrom->zIndexedBy ){
110403111157
Table *pTab = pFrom->pTab;
110404
- char *zIndex = pFrom->zIndex;
111158
+ char *zIndexedBy = pFrom->zIndexedBy;
110405111159
Index *pIdx;
110406111160
for(pIdx=pTab->pIndex;
110407
- pIdx && sqlite3StrICmp(pIdx->zName, zIndex);
111161
+ pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
110408111162
pIdx=pIdx->pNext
110409111163
);
110410111164
if( !pIdx ){
110411
- sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
111165
+ sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
110412111166
pParse->checkSchema = 1;
110413111167
return SQLITE_ERROR;
110414111168
}
110415111169
pFrom->pIndex = pIdx;
110416111170
}
@@ -111210,11 +111964,11 @@
111210111964
pColl = pParse->db->pDfltColl;
111211111965
}
111212111966
if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
111213111967
sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
111214111968
}
111215
- sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
111969
+ sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem,
111216111970
(void*)pF->pFunc, P4_FUNCDEF);
111217111971
sqlite3VdbeChangeP5(v, (u8)nArg);
111218111972
sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
111219111973
sqlite3ReleaseTempRange(pParse, regAgg, nArg);
111220111974
if( addrNext ){
@@ -111343,47 +112097,84 @@
111343112097
}
111344112098
sqlite3SelectPrep(pParse, p, 0);
111345112099
memset(&sSort, 0, sizeof(sSort));
111346112100
sSort.pOrderBy = p->pOrderBy;
111347112101
pTabList = p->pSrc;
111348
- pEList = p->pEList;
111349112102
if( pParse->nErr || db->mallocFailed ){
111350112103
goto select_end;
111351112104
}
112105
+ assert( p->pEList!=0 );
111352112106
isAgg = (p->selFlags & SF_Aggregate)!=0;
111353
- assert( pEList!=0 );
111354112107
#if SELECTTRACE_ENABLED
111355112108
if( sqlite3SelectTrace & 0x100 ){
111356112109
SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
111357112110
sqlite3TreeViewSelect(0, p, 0);
111358112111
}
111359112112
#endif
111360112113
111361112114
111362
- /* Begin generating code.
111363
- */
111364
- v = sqlite3GetVdbe(pParse);
111365
- if( v==0 ) goto select_end;
111366
-
111367112115
/* If writing to memory or generating a set
111368112116
** only a single column may be output.
111369112117
*/
111370112118
#ifndef SQLITE_OMIT_SUBQUERY
111371
- if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
112119
+ if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){
111372112120
goto select_end;
111373112121
}
111374112122
#endif
112123
+
112124
+ /* Try to flatten subqueries in the FROM clause up into the main query
112125
+ */
112126
+#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
112127
+ for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
112128
+ struct SrcList_item *pItem = &pTabList->a[i];
112129
+ Select *pSub = pItem->pSelect;
112130
+ int isAggSub;
112131
+ if( pSub==0 ) continue;
112132
+ isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
112133
+ if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
112134
+ /* This subquery can be absorbed into its parent. */
112135
+ if( isAggSub ){
112136
+ isAgg = 1;
112137
+ p->selFlags |= SF_Aggregate;
112138
+ }
112139
+ i = -1;
112140
+ }
112141
+ pTabList = p->pSrc;
112142
+ if( db->mallocFailed ) goto select_end;
112143
+ if( !IgnorableOrderby(pDest) ){
112144
+ sSort.pOrderBy = p->pOrderBy;
112145
+ }
112146
+ }
112147
+#endif
112148
+
112149
+ /* Get a pointer the VDBE under construction, allocating a new VDBE if one
112150
+ ** does not already exist */
112151
+ v = sqlite3GetVdbe(pParse);
112152
+ if( v==0 ) goto select_end;
112153
+
112154
+#ifndef SQLITE_OMIT_COMPOUND_SELECT
112155
+ /* Handle compound SELECT statements using the separate multiSelect()
112156
+ ** procedure.
112157
+ */
112158
+ if( p->pPrior ){
112159
+ rc = multiSelect(pParse, p, pDest);
112160
+ explainSetInteger(pParse->iSelectId, iRestoreSelectId);
112161
+#if SELECTTRACE_ENABLED
112162
+ SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
112163
+ pParse->nSelectIndent--;
112164
+#endif
112165
+ return rc;
112166
+ }
112167
+#endif
111375112168
111376112169
/* Generate code for all sub-queries in the FROM clause
111377112170
*/
111378112171
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
111379
- for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
112172
+ for(i=0; i<pTabList->nSrc; i++){
111380112173
struct SrcList_item *pItem = &pTabList->a[i];
111381112174
SelectDest dest;
111382112175
Select *pSub = pItem->pSelect;
111383
- int isAggSub;
111384
-
111385112176
if( pSub==0 ) continue;
111386112177
111387112178
/* Sometimes the code for a subquery will be generated more than
111388112179
** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
111389112180
** for example. In that case, do not regenerate the code to manifest
@@ -111404,21 +112195,29 @@
111404112195
** more conservative than necessary, but much easier than enforcing
111405112196
** an exact limit.
111406112197
*/
111407112198
pParse->nHeight += sqlite3SelectExprHeight(p);
111408112199
111409
- isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
111410
- if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
111411
- /* This subquery can be absorbed into its parent. */
111412
- if( isAggSub ){
111413
- isAgg = 1;
111414
- p->selFlags |= SF_Aggregate;
111415
- }
111416
- i = -1;
111417
- }else if( pTabList->nSrc==1
111418
- && (p->selFlags & SF_All)==0
111419
- && OptimizationEnabled(db, SQLITE_SubqCoroutine)
112200
+ /* Make copies of constant WHERE-clause terms in the outer query down
112201
+ ** inside the subquery. This can help the subquery to run more efficiently.
112202
+ */
112203
+ if( (pItem->jointype & JT_OUTER)==0
112204
+ && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
112205
+ ){
112206
+#if SELECTTRACE_ENABLED
112207
+ if( sqlite3SelectTrace & 0x100 ){
112208
+ SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
112209
+ sqlite3TreeViewSelect(0, p, 0);
112210
+ }
112211
+#endif
112212
+ }
112213
+
112214
+ /* Generate code to implement the subquery
112215
+ */
112216
+ if( pTabList->nSrc==1
112217
+ && (p->selFlags & SF_All)==0
112218
+ && OptimizationEnabled(db, SQLITE_SubqCoroutine)
111420112219
){
111421112220
/* Implement a co-routine that will return a single row of the result
111422112221
** set on each invocation.
111423112222
*/
111424112223
int addrTop = sqlite3VdbeCurrentAddr(v)+1;
@@ -111465,37 +112264,27 @@
111465112264
retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
111466112265
VdbeComment((v, "end %s", pItem->pTab->zName));
111467112266
sqlite3VdbeChangeP1(v, topAddr, retAddr);
111468112267
sqlite3ClearTempRegCache(pParse);
111469112268
}
111470
- if( /*pParse->nErr ||*/ db->mallocFailed ){
111471
- goto select_end;
111472
- }
112269
+ if( db->mallocFailed ) goto select_end;
111473112270
pParse->nHeight -= sqlite3SelectExprHeight(p);
111474
- pTabList = p->pSrc;
111475
- if( !IgnorableOrderby(pDest) ){
111476
- sSort.pOrderBy = p->pOrderBy;
111477
- }
111478112271
}
112272
+#endif
112273
+
112274
+ /* Various elements of the SELECT copied into local variables for
112275
+ ** convenience */
111479112276
pEList = p->pEList;
111480
-#endif
111481112277
pWhere = p->pWhere;
111482112278
pGroupBy = p->pGroupBy;
111483112279
pHaving = p->pHaving;
111484112280
sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
111485112281
111486
-#ifndef SQLITE_OMIT_COMPOUND_SELECT
111487
- /* If there is are a sequence of queries, do the earlier ones first.
111488
- */
111489
- if( p->pPrior ){
111490
- rc = multiSelect(pParse, p, pDest);
111491
- explainSetInteger(pParse->iSelectId, iRestoreSelectId);
111492112282
#if SELECTTRACE_ENABLED
111493
- SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
111494
- pParse->nSelectIndent--;
111495
-#endif
111496
- return rc;
112283
+ if( sqlite3SelectTrace & 0x400 ){
112284
+ SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
112285
+ sqlite3TreeViewSelect(0, p, 0);
111497112286
}
111498112287
#endif
111499112288
111500112289
/* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
111501112290
** if the select-list is the same as the ORDER BY list, then this query
@@ -111511,27 +112300,27 @@
111511112300
** used for both the ORDER BY and DISTINCT processing. As originally
111512112301
** written the query must use a temp-table for at least one of the ORDER
111513112302
** BY and DISTINCT, and an index or separate temp-table for the other.
111514112303
*/
111515112304
if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
111516
- && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0
112305
+ && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
111517112306
){
111518112307
p->selFlags &= ~SF_Distinct;
111519
- p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
111520
- pGroupBy = p->pGroupBy;
112308
+ pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
111521112309
/* Notice that even thought SF_Distinct has been cleared from p->selFlags,
111522112310
** the sDistinct.isTnct is still set. Hence, isTnct represents the
111523112311
** original setting of the SF_Distinct flag, not the current setting */
111524112312
assert( sDistinct.isTnct );
111525112313
}
111526112314
111527
- /* If there is an ORDER BY clause, then this sorting
111528
- ** index might end up being unused if the data can be
111529
- ** extracted in pre-sorted order. If that is the case, then the
111530
- ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
111531
- ** we figure out that the sorting index is not needed. The addrSortIndex
111532
- ** variable is used to facilitate that change.
112315
+ /* If there is an ORDER BY clause, then create an ephemeral index to
112316
+ ** do the sorting. But this sorting ephemeral index might end up
112317
+ ** being unused if the data can be extracted in pre-sorted order.
112318
+ ** If that is the case, then the OP_OpenEphemeral instruction will be
112319
+ ** changed to an OP_Noop once we figure out that the sorting index is
112320
+ ** not needed. The sSort.addrSortIndex variable is used to facilitate
112321
+ ** that change.
111533112322
*/
111534112323
if( sSort.pOrderBy ){
111535112324
KeyInfo *pKeyInfo;
111536112325
pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
111537112326
sSort.iECursor = pParse->nTab++;
@@ -111558,18 +112347,18 @@
111558112347
if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
111559112348
sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
111560112349
sSort.sortFlags |= SORTFLAG_UseSorter;
111561112350
}
111562112351
111563
- /* Open a virtual index to use for the distinct set.
112352
+ /* Open an ephemeral index to use for the distinct set.
111564112353
*/
111565112354
if( p->selFlags & SF_Distinct ){
111566112355
sDistinct.tabTnct = pParse->nTab++;
111567112356
sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
111568
- sDistinct.tabTnct, 0, 0,
111569
- (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
111570
- P4_KEYINFO);
112357
+ sDistinct.tabTnct, 0, 0,
112358
+ (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
112359
+ P4_KEYINFO);
111571112360
sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
111572112361
sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
111573112362
}else{
111574112363
sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
111575112364
}
@@ -111643,15 +112432,14 @@
111643112432
if( p->nSelectRow>100 ) p->nSelectRow = 100;
111644112433
}else{
111645112434
p->nSelectRow = 1;
111646112435
}
111647112436
111648
-
111649112437
/* If there is both a GROUP BY and an ORDER BY clause and they are
111650112438
** identical, then it may be possible to disable the ORDER BY clause
111651112439
** on the grounds that the GROUP BY will cause elements to come out
111652
- ** in the correct order. It also may not - the GROUP BY may use a
112440
+ ** in the correct order. It also may not - the GROUP BY might use a
111653112441
** database index that causes rows to be grouped together as required
111654112442
** but not actually sorted. Either way, record the fact that the
111655112443
** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
111656112444
** variable. */
111657112445
if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
@@ -111825,11 +112613,12 @@
111825112613
** from the previous row currently stored in a0, a1, a2...
111826112614
*/
111827112615
addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
111828112616
sqlite3ExprCacheClear(pParse);
111829112617
if( groupBySort ){
111830
- sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab);
112618
+ sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
112619
+ sortOut, sortPTab);
111831112620
}
111832112621
for(j=0; j<pGroupBy->nExpr; j++){
111833112622
if( groupBySort ){
111834112623
sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
111835112624
}else{
@@ -111897,11 +112686,12 @@
111897112686
sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
111898112687
VdbeComment((v, "set abort flag"));
111899112688
sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
111900112689
sqlite3VdbeResolveLabel(v, addrOutputRow);
111901112690
addrOutputRow = sqlite3VdbeCurrentAddr(v);
111902
- sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
112691
+ sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
112692
+ VdbeCoverage(v);
111903112693
VdbeComment((v, "Groupby result generator entry point"));
111904112694
sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
111905112695
finalizeAggFunctions(pParse, &sAggInfo);
111906112696
sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
111907112697
selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
@@ -112061,11 +112851,12 @@
112061112851
112062112852
/* If there is an ORDER BY clause, then we need to sort the results
112063112853
** and send them to the callback one by one.
112064112854
*/
112065112855
if( sSort.pOrderBy ){
112066
- explainTempTable(pParse, sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
112856
+ explainTempTable(pParse,
112857
+ sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
112067112858
generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
112068112859
}
112069112860
112070112861
/* Jump here to skip this query
112071112862
*/
@@ -112094,104 +112885,10 @@
112094112885
pParse->nSelectIndent--;
112095112886
#endif
112096112887
return rc;
112097112888
}
112098112889
112099
-#ifdef SQLITE_DEBUG
112100
-/*
112101
-** Generate a human-readable description of a the Select object.
112102
-*/
112103
-SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
112104
- int n = 0;
112105
- pView = sqlite3TreeViewPush(pView, moreToFollow);
112106
- sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
112107
- ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
112108
- ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
112109
- );
112110
- if( p->pSrc && p->pSrc->nSrc ) n++;
112111
- if( p->pWhere ) n++;
112112
- if( p->pGroupBy ) n++;
112113
- if( p->pHaving ) n++;
112114
- if( p->pOrderBy ) n++;
112115
- if( p->pLimit ) n++;
112116
- if( p->pOffset ) n++;
112117
- if( p->pPrior ) n++;
112118
- sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
112119
- if( p->pSrc && p->pSrc->nSrc ){
112120
- int i;
112121
- pView = sqlite3TreeViewPush(pView, (n--)>0);
112122
- sqlite3TreeViewLine(pView, "FROM");
112123
- for(i=0; i<p->pSrc->nSrc; i++){
112124
- struct SrcList_item *pItem = &p->pSrc->a[i];
112125
- StrAccum x;
112126
- char zLine[100];
112127
- sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
112128
- sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
112129
- if( pItem->zDatabase ){
112130
- sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
112131
- }else if( pItem->zName ){
112132
- sqlite3XPrintf(&x, 0, " %s", pItem->zName);
112133
- }
112134
- if( pItem->pTab ){
112135
- sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
112136
- }
112137
- if( pItem->zAlias ){
112138
- sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
112139
- }
112140
- if( pItem->jointype & JT_LEFT ){
112141
- sqlite3XPrintf(&x, 0, " LEFT-JOIN");
112142
- }
112143
- sqlite3StrAccumFinish(&x);
112144
- sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
112145
- if( pItem->pSelect ){
112146
- sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
112147
- }
112148
- sqlite3TreeViewPop(pView);
112149
- }
112150
- sqlite3TreeViewPop(pView);
112151
- }
112152
- if( p->pWhere ){
112153
- sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
112154
- sqlite3TreeViewExpr(pView, p->pWhere, 0);
112155
- sqlite3TreeViewPop(pView);
112156
- }
112157
- if( p->pGroupBy ){
112158
- sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
112159
- }
112160
- if( p->pHaving ){
112161
- sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
112162
- sqlite3TreeViewExpr(pView, p->pHaving, 0);
112163
- sqlite3TreeViewPop(pView);
112164
- }
112165
- if( p->pOrderBy ){
112166
- sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
112167
- }
112168
- if( p->pLimit ){
112169
- sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
112170
- sqlite3TreeViewExpr(pView, p->pLimit, 0);
112171
- sqlite3TreeViewPop(pView);
112172
- }
112173
- if( p->pOffset ){
112174
- sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
112175
- sqlite3TreeViewExpr(pView, p->pOffset, 0);
112176
- sqlite3TreeViewPop(pView);
112177
- }
112178
- if( p->pPrior ){
112179
- const char *zOp = "UNION";
112180
- switch( p->op ){
112181
- case TK_ALL: zOp = "UNION ALL"; break;
112182
- case TK_INTERSECT: zOp = "INTERSECT"; break;
112183
- case TK_EXCEPT: zOp = "EXCEPT"; break;
112184
- }
112185
- sqlite3TreeViewItem(pView, zOp, (n--)>0);
112186
- sqlite3TreeViewSelect(pView, p->pPrior, 0);
112187
- sqlite3TreeViewPop(pView);
112188
- }
112189
- sqlite3TreeViewPop(pView);
112190
-}
112191
-#endif /* SQLITE_DEBUG */
112192
-
112193112890
/************** End of select.c **********************************************/
112194112891
/************** Begin file table.c *******************************************/
112195112892
/*
112196112893
** 2001 September 15
112197112894
**
@@ -115499,24 +116196,25 @@
115499116196
** The array is cleared after invoking the callbacks.
115500116197
*/
115501116198
static void callFinaliser(sqlite3 *db, int offset){
115502116199
int i;
115503116200
if( db->aVTrans ){
116201
+ VTable **aVTrans = db->aVTrans;
116202
+ db->aVTrans = 0;
115504116203
for(i=0; i<db->nVTrans; i++){
115505
- VTable *pVTab = db->aVTrans[i];
116204
+ VTable *pVTab = aVTrans[i];
115506116205
sqlite3_vtab *p = pVTab->pVtab;
115507116206
if( p ){
115508116207
int (*x)(sqlite3_vtab *);
115509116208
x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
115510116209
if( x ) x(p);
115511116210
}
115512116211
pVTab->iSavepoint = 0;
115513116212
sqlite3VtabUnlock(pVTab);
115514116213
}
115515
- sqlite3DbFree(db, db->aVTrans);
116214
+ sqlite3DbFree(db, aVTrans);
115516116215
db->nVTrans = 0;
115517
- db->aVTrans = 0;
115518116216
}
115519116217
}
115520116218
115521116219
/*
115522116220
** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
@@ -115812,13 +116510,13 @@
115812116510
}
115813116511
115814116512
#endif /* SQLITE_OMIT_VIRTUALTABLE */
115815116513
115816116514
/************** End of vtab.c ************************************************/
115817
-/************** Begin file where.c *******************************************/
116515
+/************** Begin file wherecode.c ***************************************/
115818116516
/*
115819
-** 2001 September 15
116517
+** 2015-06-06
115820116518
**
115821116519
** The author disclaims copyright to this source code. In place of
115822116520
** a legal notice, here is a blessing:
115823116521
**
115824116522
** May you do good and not evil.
@@ -115825,17 +116523,18 @@
115825116523
** May you find forgiveness for yourself and forgive others.
115826116524
** May you share freely, never taking more than you give.
115827116525
**
115828116526
*************************************************************************
115829116527
** This module contains C code that generates VDBE code used to process
115830
-** the WHERE clause of SQL statements. This module is responsible for
115831
-** generating the code that loops through a table looking for applicable
115832
-** rows. Indices are selected and used to speed the search when doing
115833
-** so is applicable. Because this module is responsible for selecting
115834
-** indices, you might also think of this module as the "query optimizer".
116528
+** the WHERE clause of SQL statements.
116529
+**
116530
+** This file was split off from where.c on 2015-06-06 in order to reduce the
116531
+** size of where.c and make it easier to edit. This file contains the routines
116532
+** that actually generate the bulk of the WHERE loop code. The original where.c
116533
+** file retains the code that does query planning and analysis.
115835116534
*/
115836
-/************** Include whereInt.h in the middle of where.c ******************/
116535
+/************** Include whereInt.h in the middle of wherecode.c **************/
115837116536
/************** Begin file whereInt.h ****************************************/
115838116537
/*
115839116538
** 2013-11-12
115840116539
**
115841116540
** The author disclaims copyright to this source code. In place of
@@ -115854,11 +116553,11 @@
115854116553
115855116554
/*
115856116555
** Trace output macros
115857116556
*/
115858116557
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
115859
-/***/ int sqlite3WhereTrace = 0;
116558
+/***/ int sqlite3WhereTrace;
115860116559
#endif
115861116560
#if defined(SQLITE_DEBUG) \
115862116561
&& (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
115863116562
# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
115864116563
# define WHERETRACE_ENABLED 1
@@ -115996,14 +116695,10 @@
115996116695
struct WhereOrSet {
115997116696
u16 n; /* Number of valid a[] entries */
115998116697
WhereOrCost a[N_OR_COST]; /* Set of best costs */
115999116698
};
116000116699
116001
-
116002
-/* Forward declaration of methods */
116003
-static int whereLoopResize(sqlite3*, WhereLoop*, int);
116004
-
116005116700
/*
116006116701
** Each instance of this object holds a sequence of WhereLoop objects
116007116702
** that implement some or all of a query plan.
116008116703
**
116009116704
** Think of each WhereLoop object as a node in a graph with arcs
@@ -116207,10 +116902,15 @@
116207116902
struct WhereMaskSet {
116208116903
int n; /* Number of assigned cursor values */
116209116904
int ix[BMS]; /* Cursor assigned to each bit */
116210116905
};
116211116906
116907
+/*
116908
+** Initialize a WhereMaskSet object
116909
+*/
116910
+#define initMaskSet(P) (P)->n=0
116911
+
116212116912
/*
116213116913
** This object is a convenience wrapper holding all information needed
116214116914
** to construct WhereLoop objects for a particular query.
116215116915
*/
116216116916
struct WhereLoopBuilder {
@@ -116257,10 +116957,66 @@
116257116957
int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
116258116958
WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
116259116959
WhereClause sWC; /* Decomposition of the WHERE clause */
116260116960
WhereLevel a[1]; /* Information about each nest loop in WHERE */
116261116961
};
116962
+
116963
+/*
116964
+** Private interfaces - callable only by other where.c routines.
116965
+**
116966
+** where.c:
116967
+*/
116968
+SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
116969
+SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
116970
+ WhereClause *pWC, /* The WHERE clause to be searched */
116971
+ int iCur, /* Cursor number of LHS */
116972
+ int iColumn, /* Column number of LHS */
116973
+ Bitmask notReady, /* RHS must not overlap with this mask */
116974
+ u32 op, /* Mask of WO_xx values describing operator */
116975
+ Index *pIdx /* Must be compatible with this index, if not NULL */
116976
+);
116977
+
116978
+/* wherecode.c: */
116979
+#ifndef SQLITE_OMIT_EXPLAIN
116980
+SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
116981
+ Parse *pParse, /* Parse context */
116982
+ SrcList *pTabList, /* Table list this loop refers to */
116983
+ WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
116984
+ int iLevel, /* Value for "level" column of output */
116985
+ int iFrom, /* Value for "from" column of output */
116986
+ u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
116987
+);
116988
+#else
116989
+# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
116990
+#endif /* SQLITE_OMIT_EXPLAIN */
116991
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
116992
+SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
116993
+ Vdbe *v, /* Vdbe to add scanstatus entry to */
116994
+ SrcList *pSrclist, /* FROM clause pLvl reads data from */
116995
+ WhereLevel *pLvl, /* Level to add scanstatus() entry for */
116996
+ int addrExplain /* Address of OP_Explain (or 0) */
116997
+);
116998
+#else
116999
+# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
117000
+#endif
117001
+SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
117002
+ WhereInfo *pWInfo, /* Complete information about the WHERE clause */
117003
+ int iLevel, /* Which level of pWInfo->a[] should be coded */
117004
+ Bitmask notReady /* Which tables are currently available */
117005
+);
117006
+
117007
+/* whereexpr.c: */
117008
+SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
117009
+SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
117010
+SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
117011
+SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
117012
+SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
117013
+SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
117014
+
117015
+
117016
+
117017
+
116262117018
116263117019
/*
116264117020
** Bitmasks for the operators on WhereTerm objects. These are all
116265117021
** operators that are of interest to the query planner. An
116266117022
** OR-ed combination of these values can be used when searching for
@@ -116307,176 +117063,1532 @@
116307117063
#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
116308117064
#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
116309117065
#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
116310117066
116311117067
/************** End of whereInt.h ********************************************/
116312
-/************** Continuing where we left off in where.c **********************/
116313
-
116314
-/*
116315
-** Return the estimated number of output rows from a WHERE clause
116316
-*/
116317
-SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
116318
- return sqlite3LogEstToInt(pWInfo->nRowOut);
116319
-}
116320
-
116321
-/*
116322
-** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
116323
-** WHERE clause returns outputs for DISTINCT processing.
116324
-*/
116325
-SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
116326
- return pWInfo->eDistinct;
116327
-}
116328
-
116329
-/*
116330
-** Return TRUE if the WHERE clause returns rows in ORDER BY order.
116331
-** Return FALSE if the output needs to be sorted.
116332
-*/
116333
-SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
116334
- return pWInfo->nOBSat;
116335
-}
116336
-
116337
-/*
116338
-** Return the VDBE address or label to jump to in order to continue
116339
-** immediately with the next row of a WHERE clause.
116340
-*/
116341
-SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
116342
- assert( pWInfo->iContinue!=0 );
116343
- return pWInfo->iContinue;
116344
-}
116345
-
116346
-/*
116347
-** Return the VDBE address or label to jump to in order to break
116348
-** out of a WHERE loop.
116349
-*/
116350
-SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
116351
- return pWInfo->iBreak;
116352
-}
116353
-
116354
-/*
116355
-** Return TRUE if an UPDATE or DELETE statement can operate directly on
116356
-** the rowids returned by a WHERE clause. Return FALSE if doing an
116357
-** UPDATE or DELETE might change subsequent WHERE clause results.
116358
-**
116359
-** If the ONEPASS optimization is used (if this routine returns true)
116360
-** then also write the indices of open cursors used by ONEPASS
116361
-** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
116362
-** table and iaCur[1] gets the cursor used by an auxiliary index.
116363
-** Either value may be -1, indicating that cursor is not used.
116364
-** Any cursors returned will have been opened for writing.
116365
-**
116366
-** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
116367
-** unable to use the ONEPASS optimization.
116368
-*/
116369
-SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
116370
- memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
116371
- return pWInfo->okOnePass;
116372
-}
116373
-
116374
-/*
116375
-** Move the content of pSrc into pDest
116376
-*/
116377
-static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
116378
- pDest->n = pSrc->n;
116379
- memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
116380
-}
116381
-
116382
-/*
116383
-** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
116384
-**
116385
-** The new entry might overwrite an existing entry, or it might be
116386
-** appended, or it might be discarded. Do whatever is the right thing
116387
-** so that pSet keeps the N_OR_COST best entries seen so far.
116388
-*/
116389
-static int whereOrInsert(
116390
- WhereOrSet *pSet, /* The WhereOrSet to be updated */
116391
- Bitmask prereq, /* Prerequisites of the new entry */
116392
- LogEst rRun, /* Run-cost of the new entry */
116393
- LogEst nOut /* Number of outputs for the new entry */
116394
-){
116395
- u16 i;
116396
- WhereOrCost *p;
116397
- for(i=pSet->n, p=pSet->a; i>0; i--, p++){
116398
- if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
116399
- goto whereOrInsert_done;
116400
- }
116401
- if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
116402
- return 0;
116403
- }
116404
- }
116405
- if( pSet->n<N_OR_COST ){
116406
- p = &pSet->a[pSet->n++];
116407
- p->nOut = nOut;
116408
- }else{
116409
- p = pSet->a;
116410
- for(i=1; i<pSet->n; i++){
116411
- if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
116412
- }
116413
- if( p->rRun<=rRun ) return 0;
116414
- }
116415
-whereOrInsert_done:
116416
- p->prereq = prereq;
116417
- p->rRun = rRun;
116418
- if( p->nOut>nOut ) p->nOut = nOut;
116419
- return 1;
116420
-}
116421
-
116422
-/*
116423
-** Initialize a preallocated WhereClause structure.
116424
-*/
116425
-static void whereClauseInit(
116426
- WhereClause *pWC, /* The WhereClause to be initialized */
116427
- WhereInfo *pWInfo /* The WHERE processing context */
116428
-){
116429
- pWC->pWInfo = pWInfo;
116430
- pWC->pOuter = 0;
116431
- pWC->nTerm = 0;
116432
- pWC->nSlot = ArraySize(pWC->aStatic);
116433
- pWC->a = pWC->aStatic;
116434
-}
116435
-
116436
-/* Forward reference */
116437
-static void whereClauseClear(WhereClause*);
117068
+/************** Continuing where we left off in wherecode.c ******************/
117069
+
117070
+#ifndef SQLITE_OMIT_EXPLAIN
117071
+/*
117072
+** This routine is a helper for explainIndexRange() below
117073
+**
117074
+** pStr holds the text of an expression that we are building up one term
117075
+** at a time. This routine adds a new term to the end of the expression.
117076
+** Terms are separated by AND so add the "AND" text for second and subsequent
117077
+** terms only.
117078
+*/
117079
+static void explainAppendTerm(
117080
+ StrAccum *pStr, /* The text expression being built */
117081
+ int iTerm, /* Index of this term. First is zero */
117082
+ const char *zColumn, /* Name of the column */
117083
+ const char *zOp /* Name of the operator */
117084
+){
117085
+ if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
117086
+ sqlite3StrAccumAppendAll(pStr, zColumn);
117087
+ sqlite3StrAccumAppend(pStr, zOp, 1);
117088
+ sqlite3StrAccumAppend(pStr, "?", 1);
117089
+}
117090
+
117091
+/*
117092
+** Argument pLevel describes a strategy for scanning table pTab. This
117093
+** function appends text to pStr that describes the subset of table
117094
+** rows scanned by the strategy in the form of an SQL expression.
117095
+**
117096
+** For example, if the query:
117097
+**
117098
+** SELECT * FROM t1 WHERE a=1 AND b>2;
117099
+**
117100
+** is run and there is an index on (a, b), then this function returns a
117101
+** string similar to:
117102
+**
117103
+** "a=? AND b>?"
117104
+*/
117105
+static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
117106
+ Index *pIndex = pLoop->u.btree.pIndex;
117107
+ u16 nEq = pLoop->u.btree.nEq;
117108
+ u16 nSkip = pLoop->nSkip;
117109
+ int i, j;
117110
+ Column *aCol = pTab->aCol;
117111
+ i16 *aiColumn = pIndex->aiColumn;
117112
+
117113
+ if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
117114
+ sqlite3StrAccumAppend(pStr, " (", 2);
117115
+ for(i=0; i<nEq; i++){
117116
+ char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
117117
+ if( i>=nSkip ){
117118
+ explainAppendTerm(pStr, i, z, "=");
117119
+ }else{
117120
+ if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
117121
+ sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
117122
+ }
117123
+ }
117124
+
117125
+ j = i;
117126
+ if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
117127
+ char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
117128
+ explainAppendTerm(pStr, i++, z, ">");
117129
+ }
117130
+ if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
117131
+ char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
117132
+ explainAppendTerm(pStr, i, z, "<");
117133
+ }
117134
+ sqlite3StrAccumAppend(pStr, ")", 1);
117135
+}
117136
+
117137
+/*
117138
+** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
117139
+** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
117140
+** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
117141
+** is added to the output to describe the table scan strategy in pLevel.
117142
+**
117143
+** If an OP_Explain opcode is added to the VM, its address is returned.
117144
+** Otherwise, if no OP_Explain is coded, zero is returned.
117145
+*/
117146
+SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
117147
+ Parse *pParse, /* Parse context */
117148
+ SrcList *pTabList, /* Table list this loop refers to */
117149
+ WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
117150
+ int iLevel, /* Value for "level" column of output */
117151
+ int iFrom, /* Value for "from" column of output */
117152
+ u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
117153
+){
117154
+ int ret = 0;
117155
+#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
117156
+ if( pParse->explain==2 )
117157
+#endif
117158
+ {
117159
+ struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
117160
+ Vdbe *v = pParse->pVdbe; /* VM being constructed */
117161
+ sqlite3 *db = pParse->db; /* Database handle */
117162
+ int iId = pParse->iSelectId; /* Select id (left-most output column) */
117163
+ int isSearch; /* True for a SEARCH. False for SCAN. */
117164
+ WhereLoop *pLoop; /* The controlling WhereLoop object */
117165
+ u32 flags; /* Flags that describe this loop */
117166
+ char *zMsg; /* Text to add to EQP output */
117167
+ StrAccum str; /* EQP output string */
117168
+ char zBuf[100]; /* Initial space for EQP output string */
117169
+
117170
+ pLoop = pLevel->pWLoop;
117171
+ flags = pLoop->wsFlags;
117172
+ if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
117173
+
117174
+ isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
117175
+ || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
117176
+ || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
117177
+
117178
+ sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
117179
+ sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
117180
+ if( pItem->pSelect ){
117181
+ sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
117182
+ }else{
117183
+ sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
117184
+ }
117185
+
117186
+ if( pItem->zAlias ){
117187
+ sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
117188
+ }
117189
+ if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
117190
+ const char *zFmt = 0;
117191
+ Index *pIdx;
117192
+
117193
+ assert( pLoop->u.btree.pIndex!=0 );
117194
+ pIdx = pLoop->u.btree.pIndex;
117195
+ assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
117196
+ if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
117197
+ if( isSearch ){
117198
+ zFmt = "PRIMARY KEY";
117199
+ }
117200
+ }else if( flags & WHERE_PARTIALIDX ){
117201
+ zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
117202
+ }else if( flags & WHERE_AUTO_INDEX ){
117203
+ zFmt = "AUTOMATIC COVERING INDEX";
117204
+ }else if( flags & WHERE_IDX_ONLY ){
117205
+ zFmt = "COVERING INDEX %s";
117206
+ }else{
117207
+ zFmt = "INDEX %s";
117208
+ }
117209
+ if( zFmt ){
117210
+ sqlite3StrAccumAppend(&str, " USING ", 7);
117211
+ sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
117212
+ explainIndexRange(&str, pLoop, pItem->pTab);
117213
+ }
117214
+ }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
117215
+ const char *zRange;
117216
+ if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
117217
+ zRange = "(rowid=?)";
117218
+ }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
117219
+ zRange = "(rowid>? AND rowid<?)";
117220
+ }else if( flags&WHERE_BTM_LIMIT ){
117221
+ zRange = "(rowid>?)";
117222
+ }else{
117223
+ assert( flags&WHERE_TOP_LIMIT);
117224
+ zRange = "(rowid<?)";
117225
+ }
117226
+ sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
117227
+ sqlite3StrAccumAppendAll(&str, zRange);
117228
+ }
117229
+#ifndef SQLITE_OMIT_VIRTUALTABLE
117230
+ else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
117231
+ sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
117232
+ pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
117233
+ }
117234
+#endif
117235
+#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
117236
+ if( pLoop->nOut>=10 ){
117237
+ sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
117238
+ }else{
117239
+ sqlite3StrAccumAppend(&str, " (~1 row)", 9);
117240
+ }
117241
+#endif
117242
+ zMsg = sqlite3StrAccumFinish(&str);
117243
+ ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
117244
+ }
117245
+ return ret;
117246
+}
117247
+#endif /* SQLITE_OMIT_EXPLAIN */
117248
+
117249
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
117250
+/*
117251
+** Configure the VM passed as the first argument with an
117252
+** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
117253
+** implement level pLvl. Argument pSrclist is a pointer to the FROM
117254
+** clause that the scan reads data from.
117255
+**
117256
+** If argument addrExplain is not 0, it must be the address of an
117257
+** OP_Explain instruction that describes the same loop.
117258
+*/
117259
+SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
117260
+ Vdbe *v, /* Vdbe to add scanstatus entry to */
117261
+ SrcList *pSrclist, /* FROM clause pLvl reads data from */
117262
+ WhereLevel *pLvl, /* Level to add scanstatus() entry for */
117263
+ int addrExplain /* Address of OP_Explain (or 0) */
117264
+){
117265
+ const char *zObj = 0;
117266
+ WhereLoop *pLoop = pLvl->pWLoop;
117267
+ if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
117268
+ zObj = pLoop->u.btree.pIndex->zName;
117269
+ }else{
117270
+ zObj = pSrclist->a[pLvl->iFrom].zName;
117271
+ }
117272
+ sqlite3VdbeScanStatus(
117273
+ v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
117274
+ );
117275
+}
117276
+#endif
117277
+
117278
+
117279
+/*
117280
+** Disable a term in the WHERE clause. Except, do not disable the term
117281
+** if it controls a LEFT OUTER JOIN and it did not originate in the ON
117282
+** or USING clause of that join.
117283
+**
117284
+** Consider the term t2.z='ok' in the following queries:
117285
+**
117286
+** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
117287
+** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
117288
+** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
117289
+**
117290
+** The t2.z='ok' is disabled in the in (2) because it originates
117291
+** in the ON clause. The term is disabled in (3) because it is not part
117292
+** of a LEFT OUTER JOIN. In (1), the term is not disabled.
117293
+**
117294
+** Disabling a term causes that term to not be tested in the inner loop
117295
+** of the join. Disabling is an optimization. When terms are satisfied
117296
+** by indices, we disable them to prevent redundant tests in the inner
117297
+** loop. We would get the correct results if nothing were ever disabled,
117298
+** but joins might run a little slower. The trick is to disable as much
117299
+** as we can without disabling too much. If we disabled in (1), we'd get
117300
+** the wrong answer. See ticket #813.
117301
+**
117302
+** If all the children of a term are disabled, then that term is also
117303
+** automatically disabled. In this way, terms get disabled if derived
117304
+** virtual terms are tested first. For example:
117305
+**
117306
+** x GLOB 'abc*' AND x>='abc' AND x<'acd'
117307
+** \___________/ \______/ \_____/
117308
+** parent child1 child2
117309
+**
117310
+** Only the parent term was in the original WHERE clause. The child1
117311
+** and child2 terms were added by the LIKE optimization. If both of
117312
+** the virtual child terms are valid, then testing of the parent can be
117313
+** skipped.
117314
+**
117315
+** Usually the parent term is marked as TERM_CODED. But if the parent
117316
+** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
117317
+** The TERM_LIKECOND marking indicates that the term should be coded inside
117318
+** a conditional such that is only evaluated on the second pass of a
117319
+** LIKE-optimization loop, when scanning BLOBs instead of strings.
117320
+*/
117321
+static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
117322
+ int nLoop = 0;
117323
+ while( pTerm
117324
+ && (pTerm->wtFlags & TERM_CODED)==0
117325
+ && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
117326
+ && (pLevel->notReady & pTerm->prereqAll)==0
117327
+ ){
117328
+ if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
117329
+ pTerm->wtFlags |= TERM_LIKECOND;
117330
+ }else{
117331
+ pTerm->wtFlags |= TERM_CODED;
117332
+ }
117333
+ if( pTerm->iParent<0 ) break;
117334
+ pTerm = &pTerm->pWC->a[pTerm->iParent];
117335
+ pTerm->nChild--;
117336
+ if( pTerm->nChild!=0 ) break;
117337
+ nLoop++;
117338
+ }
117339
+}
117340
+
117341
+/*
117342
+** Code an OP_Affinity opcode to apply the column affinity string zAff
117343
+** to the n registers starting at base.
117344
+**
117345
+** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
117346
+** beginning and end of zAff are ignored. If all entries in zAff are
117347
+** SQLITE_AFF_BLOB, then no code gets generated.
117348
+**
117349
+** This routine makes its own copy of zAff so that the caller is free
117350
+** to modify zAff after this routine returns.
117351
+*/
117352
+static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
117353
+ Vdbe *v = pParse->pVdbe;
117354
+ if( zAff==0 ){
117355
+ assert( pParse->db->mallocFailed );
117356
+ return;
117357
+ }
117358
+ assert( v!=0 );
117359
+
117360
+ /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
117361
+ ** and end of the affinity string.
117362
+ */
117363
+ while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
117364
+ n--;
117365
+ base++;
117366
+ zAff++;
117367
+ }
117368
+ while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
117369
+ n--;
117370
+ }
117371
+
117372
+ /* Code the OP_Affinity opcode if there is anything left to do. */
117373
+ if( n>0 ){
117374
+ sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
117375
+ sqlite3VdbeChangeP4(v, -1, zAff, n);
117376
+ sqlite3ExprCacheAffinityChange(pParse, base, n);
117377
+ }
117378
+}
117379
+
117380
+
117381
+/*
117382
+** Generate code for a single equality term of the WHERE clause. An equality
117383
+** term can be either X=expr or X IN (...). pTerm is the term to be
117384
+** coded.
117385
+**
117386
+** The current value for the constraint is left in register iReg.
117387
+**
117388
+** For a constraint of the form X=expr, the expression is evaluated and its
117389
+** result is left on the stack. For constraints of the form X IN (...)
117390
+** this routine sets up a loop that will iterate over all values of X.
117391
+*/
117392
+static int codeEqualityTerm(
117393
+ Parse *pParse, /* The parsing context */
117394
+ WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
117395
+ WhereLevel *pLevel, /* The level of the FROM clause we are working on */
117396
+ int iEq, /* Index of the equality term within this level */
117397
+ int bRev, /* True for reverse-order IN operations */
117398
+ int iTarget /* Attempt to leave results in this register */
117399
+){
117400
+ Expr *pX = pTerm->pExpr;
117401
+ Vdbe *v = pParse->pVdbe;
117402
+ int iReg; /* Register holding results */
117403
+
117404
+ assert( iTarget>0 );
117405
+ if( pX->op==TK_EQ || pX->op==TK_IS ){
117406
+ iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
117407
+ }else if( pX->op==TK_ISNULL ){
117408
+ iReg = iTarget;
117409
+ sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
117410
+#ifndef SQLITE_OMIT_SUBQUERY
117411
+ }else{
117412
+ int eType;
117413
+ int iTab;
117414
+ struct InLoop *pIn;
117415
+ WhereLoop *pLoop = pLevel->pWLoop;
117416
+
117417
+ if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
117418
+ && pLoop->u.btree.pIndex!=0
117419
+ && pLoop->u.btree.pIndex->aSortOrder[iEq]
117420
+ ){
117421
+ testcase( iEq==0 );
117422
+ testcase( bRev );
117423
+ bRev = !bRev;
117424
+ }
117425
+ assert( pX->op==TK_IN );
117426
+ iReg = iTarget;
117427
+ eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
117428
+ if( eType==IN_INDEX_INDEX_DESC ){
117429
+ testcase( bRev );
117430
+ bRev = !bRev;
117431
+ }
117432
+ iTab = pX->iTable;
117433
+ sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
117434
+ VdbeCoverageIf(v, bRev);
117435
+ VdbeCoverageIf(v, !bRev);
117436
+ assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
117437
+ pLoop->wsFlags |= WHERE_IN_ABLE;
117438
+ if( pLevel->u.in.nIn==0 ){
117439
+ pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
117440
+ }
117441
+ pLevel->u.in.nIn++;
117442
+ pLevel->u.in.aInLoop =
117443
+ sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
117444
+ sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
117445
+ pIn = pLevel->u.in.aInLoop;
117446
+ if( pIn ){
117447
+ pIn += pLevel->u.in.nIn - 1;
117448
+ pIn->iCur = iTab;
117449
+ if( eType==IN_INDEX_ROWID ){
117450
+ pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
117451
+ }else{
117452
+ pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
117453
+ }
117454
+ pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
117455
+ sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
117456
+ }else{
117457
+ pLevel->u.in.nIn = 0;
117458
+ }
117459
+#endif
117460
+ }
117461
+ disableTerm(pLevel, pTerm);
117462
+ return iReg;
117463
+}
117464
+
117465
+/*
117466
+** Generate code that will evaluate all == and IN constraints for an
117467
+** index scan.
117468
+**
117469
+** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
117470
+** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
117471
+** The index has as many as three equality constraints, but in this
117472
+** example, the third "c" value is an inequality. So only two
117473
+** constraints are coded. This routine will generate code to evaluate
117474
+** a==5 and b IN (1,2,3). The current values for a and b will be stored
117475
+** in consecutive registers and the index of the first register is returned.
117476
+**
117477
+** In the example above nEq==2. But this subroutine works for any value
117478
+** of nEq including 0. If nEq==0, this routine is nearly a no-op.
117479
+** The only thing it does is allocate the pLevel->iMem memory cell and
117480
+** compute the affinity string.
117481
+**
117482
+** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
117483
+** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
117484
+** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
117485
+** occurs after the nEq quality constraints.
117486
+**
117487
+** This routine allocates a range of nEq+nExtraReg memory cells and returns
117488
+** the index of the first memory cell in that range. The code that
117489
+** calls this routine will use that memory range to store keys for
117490
+** start and termination conditions of the loop.
117491
+** key value of the loop. If one or more IN operators appear, then
117492
+** this routine allocates an additional nEq memory cells for internal
117493
+** use.
117494
+**
117495
+** Before returning, *pzAff is set to point to a buffer containing a
117496
+** copy of the column affinity string of the index allocated using
117497
+** sqlite3DbMalloc(). Except, entries in the copy of the string associated
117498
+** with equality constraints that use BLOB or NONE affinity are set to
117499
+** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
117500
+**
117501
+** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
117502
+** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
117503
+**
117504
+** In the example above, the index on t1(a) has TEXT affinity. But since
117505
+** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
117506
+** no conversion should be attempted before using a t2.b value as part of
117507
+** a key to search the index. Hence the first byte in the returned affinity
117508
+** string in this example would be set to SQLITE_AFF_BLOB.
117509
+*/
117510
+static int codeAllEqualityTerms(
117511
+ Parse *pParse, /* Parsing context */
117512
+ WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
117513
+ int bRev, /* Reverse the order of IN operators */
117514
+ int nExtraReg, /* Number of extra registers to allocate */
117515
+ char **pzAff /* OUT: Set to point to affinity string */
117516
+){
117517
+ u16 nEq; /* The number of == or IN constraints to code */
117518
+ u16 nSkip; /* Number of left-most columns to skip */
117519
+ Vdbe *v = pParse->pVdbe; /* The vm under construction */
117520
+ Index *pIdx; /* The index being used for this loop */
117521
+ WhereTerm *pTerm; /* A single constraint term */
117522
+ WhereLoop *pLoop; /* The WhereLoop object */
117523
+ int j; /* Loop counter */
117524
+ int regBase; /* Base register */
117525
+ int nReg; /* Number of registers to allocate */
117526
+ char *zAff; /* Affinity string to return */
117527
+
117528
+ /* This module is only called on query plans that use an index. */
117529
+ pLoop = pLevel->pWLoop;
117530
+ assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
117531
+ nEq = pLoop->u.btree.nEq;
117532
+ nSkip = pLoop->nSkip;
117533
+ pIdx = pLoop->u.btree.pIndex;
117534
+ assert( pIdx!=0 );
117535
+
117536
+ /* Figure out how many memory cells we will need then allocate them.
117537
+ */
117538
+ regBase = pParse->nMem + 1;
117539
+ nReg = pLoop->u.btree.nEq + nExtraReg;
117540
+ pParse->nMem += nReg;
117541
+
117542
+ zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
117543
+ if( !zAff ){
117544
+ pParse->db->mallocFailed = 1;
117545
+ }
117546
+
117547
+ if( nSkip ){
117548
+ int iIdxCur = pLevel->iIdxCur;
117549
+ sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
117550
+ VdbeCoverageIf(v, bRev==0);
117551
+ VdbeCoverageIf(v, bRev!=0);
117552
+ VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
117553
+ j = sqlite3VdbeAddOp0(v, OP_Goto);
117554
+ pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
117555
+ iIdxCur, 0, regBase, nSkip);
117556
+ VdbeCoverageIf(v, bRev==0);
117557
+ VdbeCoverageIf(v, bRev!=0);
117558
+ sqlite3VdbeJumpHere(v, j);
117559
+ for(j=0; j<nSkip; j++){
117560
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
117561
+ assert( pIdx->aiColumn[j]>=0 );
117562
+ VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
117563
+ }
117564
+ }
117565
+
117566
+ /* Evaluate the equality constraints
117567
+ */
117568
+ assert( zAff==0 || (int)strlen(zAff)>=nEq );
117569
+ for(j=nSkip; j<nEq; j++){
117570
+ int r1;
117571
+ pTerm = pLoop->aLTerm[j];
117572
+ assert( pTerm!=0 );
117573
+ /* The following testcase is true for indices with redundant columns.
117574
+ ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
117575
+ testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
117576
+ testcase( pTerm->wtFlags & TERM_VIRTUAL );
117577
+ r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
117578
+ if( r1!=regBase+j ){
117579
+ if( nReg==1 ){
117580
+ sqlite3ReleaseTempReg(pParse, regBase);
117581
+ regBase = r1;
117582
+ }else{
117583
+ sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
117584
+ }
117585
+ }
117586
+ testcase( pTerm->eOperator & WO_ISNULL );
117587
+ testcase( pTerm->eOperator & WO_IN );
117588
+ if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
117589
+ Expr *pRight = pTerm->pExpr->pRight;
117590
+ if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
117591
+ sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
117592
+ VdbeCoverage(v);
117593
+ }
117594
+ if( zAff ){
117595
+ if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
117596
+ zAff[j] = SQLITE_AFF_BLOB;
117597
+ }
117598
+ if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
117599
+ zAff[j] = SQLITE_AFF_BLOB;
117600
+ }
117601
+ }
117602
+ }
117603
+ }
117604
+ *pzAff = zAff;
117605
+ return regBase;
117606
+}
117607
+
117608
+/*
117609
+** If the most recently coded instruction is a constant range contraint
117610
+** that originated from the LIKE optimization, then change the P3 to be
117611
+** pLoop->iLikeRepCntr and set P5.
117612
+**
117613
+** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
117614
+** expression: "x>='ABC' AND x<'abd'". But this requires that the range
117615
+** scan loop run twice, once for strings and a second time for BLOBs.
117616
+** The OP_String opcodes on the second pass convert the upper and lower
117617
+** bound string contants to blobs. This routine makes the necessary changes
117618
+** to the OP_String opcodes for that to happen.
117619
+*/
117620
+static void whereLikeOptimizationStringFixup(
117621
+ Vdbe *v, /* prepared statement under construction */
117622
+ WhereLevel *pLevel, /* The loop that contains the LIKE operator */
117623
+ WhereTerm *pTerm /* The upper or lower bound just coded */
117624
+){
117625
+ if( pTerm->wtFlags & TERM_LIKEOPT ){
117626
+ VdbeOp *pOp;
117627
+ assert( pLevel->iLikeRepCntr>0 );
117628
+ pOp = sqlite3VdbeGetOp(v, -1);
117629
+ assert( pOp!=0 );
117630
+ assert( pOp->opcode==OP_String8
117631
+ || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
117632
+ pOp->p3 = pLevel->iLikeRepCntr;
117633
+ pOp->p5 = 1;
117634
+ }
117635
+}
117636
+
117637
+
117638
+/*
117639
+** Generate code for the start of the iLevel-th loop in the WHERE clause
117640
+** implementation described by pWInfo.
117641
+*/
117642
+SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
117643
+ WhereInfo *pWInfo, /* Complete information about the WHERE clause */
117644
+ int iLevel, /* Which level of pWInfo->a[] should be coded */
117645
+ Bitmask notReady /* Which tables are currently available */
117646
+){
117647
+ int j, k; /* Loop counters */
117648
+ int iCur; /* The VDBE cursor for the table */
117649
+ int addrNxt; /* Where to jump to continue with the next IN case */
117650
+ int omitTable; /* True if we use the index only */
117651
+ int bRev; /* True if we need to scan in reverse order */
117652
+ WhereLevel *pLevel; /* The where level to be coded */
117653
+ WhereLoop *pLoop; /* The WhereLoop object being coded */
117654
+ WhereClause *pWC; /* Decomposition of the entire WHERE clause */
117655
+ WhereTerm *pTerm; /* A WHERE clause term */
117656
+ Parse *pParse; /* Parsing context */
117657
+ sqlite3 *db; /* Database connection */
117658
+ Vdbe *v; /* The prepared stmt under constructions */
117659
+ struct SrcList_item *pTabItem; /* FROM clause term being coded */
117660
+ int addrBrk; /* Jump here to break out of the loop */
117661
+ int addrCont; /* Jump here to continue with next cycle */
117662
+ int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
117663
+ int iReleaseReg = 0; /* Temp register to free before returning */
117664
+
117665
+ pParse = pWInfo->pParse;
117666
+ v = pParse->pVdbe;
117667
+ pWC = &pWInfo->sWC;
117668
+ db = pParse->db;
117669
+ pLevel = &pWInfo->a[iLevel];
117670
+ pLoop = pLevel->pWLoop;
117671
+ pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
117672
+ iCur = pTabItem->iCursor;
117673
+ pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
117674
+ bRev = (pWInfo->revMask>>iLevel)&1;
117675
+ omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
117676
+ && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
117677
+ VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
117678
+
117679
+ /* Create labels for the "break" and "continue" instructions
117680
+ ** for the current loop. Jump to addrBrk to break out of a loop.
117681
+ ** Jump to cont to go immediately to the next iteration of the
117682
+ ** loop.
117683
+ **
117684
+ ** When there is an IN operator, we also have a "addrNxt" label that
117685
+ ** means to continue with the next IN value combination. When
117686
+ ** there are no IN operators in the constraints, the "addrNxt" label
117687
+ ** is the same as "addrBrk".
117688
+ */
117689
+ addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
117690
+ addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
117691
+
117692
+ /* If this is the right table of a LEFT OUTER JOIN, allocate and
117693
+ ** initialize a memory cell that records if this table matches any
117694
+ ** row of the left table of the join.
117695
+ */
117696
+ if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
117697
+ pLevel->iLeftJoin = ++pParse->nMem;
117698
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
117699
+ VdbeComment((v, "init LEFT JOIN no-match flag"));
117700
+ }
117701
+
117702
+ /* Special case of a FROM clause subquery implemented as a co-routine */
117703
+ if( pTabItem->viaCoroutine ){
117704
+ int regYield = pTabItem->regReturn;
117705
+ sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
117706
+ pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
117707
+ VdbeCoverage(v);
117708
+ VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
117709
+ pLevel->op = OP_Goto;
117710
+ }else
117711
+
117712
+#ifndef SQLITE_OMIT_VIRTUALTABLE
117713
+ if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
117714
+ /* Case 1: The table is a virtual-table. Use the VFilter and VNext
117715
+ ** to access the data.
117716
+ */
117717
+ int iReg; /* P3 Value for OP_VFilter */
117718
+ int addrNotFound;
117719
+ int nConstraint = pLoop->nLTerm;
117720
+
117721
+ sqlite3ExprCachePush(pParse);
117722
+ iReg = sqlite3GetTempRange(pParse, nConstraint+2);
117723
+ addrNotFound = pLevel->addrBrk;
117724
+ for(j=0; j<nConstraint; j++){
117725
+ int iTarget = iReg+j+2;
117726
+ pTerm = pLoop->aLTerm[j];
117727
+ if( pTerm==0 ) continue;
117728
+ if( pTerm->eOperator & WO_IN ){
117729
+ codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
117730
+ addrNotFound = pLevel->addrNxt;
117731
+ }else{
117732
+ sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
117733
+ }
117734
+ }
117735
+ sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
117736
+ sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
117737
+ sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
117738
+ pLoop->u.vtab.idxStr,
117739
+ pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
117740
+ VdbeCoverage(v);
117741
+ pLoop->u.vtab.needFree = 0;
117742
+ for(j=0; j<nConstraint && j<16; j++){
117743
+ if( (pLoop->u.vtab.omitMask>>j)&1 ){
117744
+ disableTerm(pLevel, pLoop->aLTerm[j]);
117745
+ }
117746
+ }
117747
+ pLevel->op = OP_VNext;
117748
+ pLevel->p1 = iCur;
117749
+ pLevel->p2 = sqlite3VdbeCurrentAddr(v);
117750
+ sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
117751
+ sqlite3ExprCachePop(pParse);
117752
+ }else
117753
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
117754
+
117755
+ if( (pLoop->wsFlags & WHERE_IPK)!=0
117756
+ && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
117757
+ ){
117758
+ /* Case 2: We can directly reference a single row using an
117759
+ ** equality comparison against the ROWID field. Or
117760
+ ** we reference multiple rows using a "rowid IN (...)"
117761
+ ** construct.
117762
+ */
117763
+ assert( pLoop->u.btree.nEq==1 );
117764
+ pTerm = pLoop->aLTerm[0];
117765
+ assert( pTerm!=0 );
117766
+ assert( pTerm->pExpr!=0 );
117767
+ assert( omitTable==0 );
117768
+ testcase( pTerm->wtFlags & TERM_VIRTUAL );
117769
+ iReleaseReg = ++pParse->nMem;
117770
+ iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
117771
+ if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
117772
+ addrNxt = pLevel->addrNxt;
117773
+ sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
117774
+ sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
117775
+ VdbeCoverage(v);
117776
+ sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
117777
+ sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
117778
+ VdbeComment((v, "pk"));
117779
+ pLevel->op = OP_Noop;
117780
+ }else if( (pLoop->wsFlags & WHERE_IPK)!=0
117781
+ && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
117782
+ ){
117783
+ /* Case 3: We have an inequality comparison against the ROWID field.
117784
+ */
117785
+ int testOp = OP_Noop;
117786
+ int start;
117787
+ int memEndValue = 0;
117788
+ WhereTerm *pStart, *pEnd;
117789
+
117790
+ assert( omitTable==0 );
117791
+ j = 0;
117792
+ pStart = pEnd = 0;
117793
+ if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
117794
+ if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
117795
+ assert( pStart!=0 || pEnd!=0 );
117796
+ if( bRev ){
117797
+ pTerm = pStart;
117798
+ pStart = pEnd;
117799
+ pEnd = pTerm;
117800
+ }
117801
+ if( pStart ){
117802
+ Expr *pX; /* The expression that defines the start bound */
117803
+ int r1, rTemp; /* Registers for holding the start boundary */
117804
+
117805
+ /* The following constant maps TK_xx codes into corresponding
117806
+ ** seek opcodes. It depends on a particular ordering of TK_xx
117807
+ */
117808
+ const u8 aMoveOp[] = {
117809
+ /* TK_GT */ OP_SeekGT,
117810
+ /* TK_LE */ OP_SeekLE,
117811
+ /* TK_LT */ OP_SeekLT,
117812
+ /* TK_GE */ OP_SeekGE
117813
+ };
117814
+ assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
117815
+ assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
117816
+ assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
117817
+
117818
+ assert( (pStart->wtFlags & TERM_VNULL)==0 );
117819
+ testcase( pStart->wtFlags & TERM_VIRTUAL );
117820
+ pX = pStart->pExpr;
117821
+ assert( pX!=0 );
117822
+ testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
117823
+ r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
117824
+ sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
117825
+ VdbeComment((v, "pk"));
117826
+ VdbeCoverageIf(v, pX->op==TK_GT);
117827
+ VdbeCoverageIf(v, pX->op==TK_LE);
117828
+ VdbeCoverageIf(v, pX->op==TK_LT);
117829
+ VdbeCoverageIf(v, pX->op==TK_GE);
117830
+ sqlite3ExprCacheAffinityChange(pParse, r1, 1);
117831
+ sqlite3ReleaseTempReg(pParse, rTemp);
117832
+ disableTerm(pLevel, pStart);
117833
+ }else{
117834
+ sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
117835
+ VdbeCoverageIf(v, bRev==0);
117836
+ VdbeCoverageIf(v, bRev!=0);
117837
+ }
117838
+ if( pEnd ){
117839
+ Expr *pX;
117840
+ pX = pEnd->pExpr;
117841
+ assert( pX!=0 );
117842
+ assert( (pEnd->wtFlags & TERM_VNULL)==0 );
117843
+ testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
117844
+ testcase( pEnd->wtFlags & TERM_VIRTUAL );
117845
+ memEndValue = ++pParse->nMem;
117846
+ sqlite3ExprCode(pParse, pX->pRight, memEndValue);
117847
+ if( pX->op==TK_LT || pX->op==TK_GT ){
117848
+ testOp = bRev ? OP_Le : OP_Ge;
117849
+ }else{
117850
+ testOp = bRev ? OP_Lt : OP_Gt;
117851
+ }
117852
+ disableTerm(pLevel, pEnd);
117853
+ }
117854
+ start = sqlite3VdbeCurrentAddr(v);
117855
+ pLevel->op = bRev ? OP_Prev : OP_Next;
117856
+ pLevel->p1 = iCur;
117857
+ pLevel->p2 = start;
117858
+ assert( pLevel->p5==0 );
117859
+ if( testOp!=OP_Noop ){
117860
+ iRowidReg = ++pParse->nMem;
117861
+ sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
117862
+ sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
117863
+ sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
117864
+ VdbeCoverageIf(v, testOp==OP_Le);
117865
+ VdbeCoverageIf(v, testOp==OP_Lt);
117866
+ VdbeCoverageIf(v, testOp==OP_Ge);
117867
+ VdbeCoverageIf(v, testOp==OP_Gt);
117868
+ sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
117869
+ }
117870
+ }else if( pLoop->wsFlags & WHERE_INDEXED ){
117871
+ /* Case 4: A scan using an index.
117872
+ **
117873
+ ** The WHERE clause may contain zero or more equality
117874
+ ** terms ("==" or "IN" operators) that refer to the N
117875
+ ** left-most columns of the index. It may also contain
117876
+ ** inequality constraints (>, <, >= or <=) on the indexed
117877
+ ** column that immediately follows the N equalities. Only
117878
+ ** the right-most column can be an inequality - the rest must
117879
+ ** use the "==" and "IN" operators. For example, if the
117880
+ ** index is on (x,y,z), then the following clauses are all
117881
+ ** optimized:
117882
+ **
117883
+ ** x=5
117884
+ ** x=5 AND y=10
117885
+ ** x=5 AND y<10
117886
+ ** x=5 AND y>5 AND y<10
117887
+ ** x=5 AND y=5 AND z<=10
117888
+ **
117889
+ ** The z<10 term of the following cannot be used, only
117890
+ ** the x=5 term:
117891
+ **
117892
+ ** x=5 AND z<10
117893
+ **
117894
+ ** N may be zero if there are inequality constraints.
117895
+ ** If there are no inequality constraints, then N is at
117896
+ ** least one.
117897
+ **
117898
+ ** This case is also used when there are no WHERE clause
117899
+ ** constraints but an index is selected anyway, in order
117900
+ ** to force the output order to conform to an ORDER BY.
117901
+ */
117902
+ static const u8 aStartOp[] = {
117903
+ 0,
117904
+ 0,
117905
+ OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
117906
+ OP_Last, /* 3: (!start_constraints && startEq && bRev) */
117907
+ OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
117908
+ OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
117909
+ OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
117910
+ OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
117911
+ };
117912
+ static const u8 aEndOp[] = {
117913
+ OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
117914
+ OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
117915
+ OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
117916
+ OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
117917
+ };
117918
+ u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
117919
+ int regBase; /* Base register holding constraint values */
117920
+ WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
117921
+ WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
117922
+ int startEq; /* True if range start uses ==, >= or <= */
117923
+ int endEq; /* True if range end uses ==, >= or <= */
117924
+ int start_constraints; /* Start of range is constrained */
117925
+ int nConstraint; /* Number of constraint terms */
117926
+ Index *pIdx; /* The index we will be using */
117927
+ int iIdxCur; /* The VDBE cursor for the index */
117928
+ int nExtraReg = 0; /* Number of extra registers needed */
117929
+ int op; /* Instruction opcode */
117930
+ char *zStartAff; /* Affinity for start of range constraint */
117931
+ char cEndAff = 0; /* Affinity for end of range constraint */
117932
+ u8 bSeekPastNull = 0; /* True to seek past initial nulls */
117933
+ u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
117934
+
117935
+ pIdx = pLoop->u.btree.pIndex;
117936
+ iIdxCur = pLevel->iIdxCur;
117937
+ assert( nEq>=pLoop->nSkip );
117938
+
117939
+ /* If this loop satisfies a sort order (pOrderBy) request that
117940
+ ** was passed to this function to implement a "SELECT min(x) ..."
117941
+ ** query, then the caller will only allow the loop to run for
117942
+ ** a single iteration. This means that the first row returned
117943
+ ** should not have a NULL value stored in 'x'. If column 'x' is
117944
+ ** the first one after the nEq equality constraints in the index,
117945
+ ** this requires some special handling.
117946
+ */
117947
+ assert( pWInfo->pOrderBy==0
117948
+ || pWInfo->pOrderBy->nExpr==1
117949
+ || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
117950
+ if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
117951
+ && pWInfo->nOBSat>0
117952
+ && (pIdx->nKeyCol>nEq)
117953
+ ){
117954
+ assert( pLoop->nSkip==0 );
117955
+ bSeekPastNull = 1;
117956
+ nExtraReg = 1;
117957
+ }
117958
+
117959
+ /* Find any inequality constraint terms for the start and end
117960
+ ** of the range.
117961
+ */
117962
+ j = nEq;
117963
+ if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
117964
+ pRangeStart = pLoop->aLTerm[j++];
117965
+ nExtraReg = 1;
117966
+ /* Like optimization range constraints always occur in pairs */
117967
+ assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
117968
+ (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
117969
+ }
117970
+ if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
117971
+ pRangeEnd = pLoop->aLTerm[j++];
117972
+ nExtraReg = 1;
117973
+ if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
117974
+ assert( pRangeStart!=0 ); /* LIKE opt constraints */
117975
+ assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
117976
+ pLevel->iLikeRepCntr = ++pParse->nMem;
117977
+ testcase( bRev );
117978
+ testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
117979
+ sqlite3VdbeAddOp2(v, OP_Integer,
117980
+ bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
117981
+ pLevel->iLikeRepCntr);
117982
+ VdbeComment((v, "LIKE loop counter"));
117983
+ pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
117984
+ }
117985
+ if( pRangeStart==0
117986
+ && (j = pIdx->aiColumn[nEq])>=0
117987
+ && pIdx->pTable->aCol[j].notNull==0
117988
+ ){
117989
+ bSeekPastNull = 1;
117990
+ }
117991
+ }
117992
+ assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
117993
+
117994
+ /* Generate code to evaluate all constraint terms using == or IN
117995
+ ** and store the values of those terms in an array of registers
117996
+ ** starting at regBase.
117997
+ */
117998
+ regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
117999
+ assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
118000
+ if( zStartAff ) cEndAff = zStartAff[nEq];
118001
+ addrNxt = pLevel->addrNxt;
118002
+
118003
+ /* If we are doing a reverse order scan on an ascending index, or
118004
+ ** a forward order scan on a descending index, interchange the
118005
+ ** start and end terms (pRangeStart and pRangeEnd).
118006
+ */
118007
+ if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
118008
+ || (bRev && pIdx->nKeyCol==nEq)
118009
+ ){
118010
+ SWAP(WhereTerm *, pRangeEnd, pRangeStart);
118011
+ SWAP(u8, bSeekPastNull, bStopAtNull);
118012
+ }
118013
+
118014
+ testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
118015
+ testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
118016
+ testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
118017
+ testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
118018
+ startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
118019
+ endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
118020
+ start_constraints = pRangeStart || nEq>0;
118021
+
118022
+ /* Seek the index cursor to the start of the range. */
118023
+ nConstraint = nEq;
118024
+ if( pRangeStart ){
118025
+ Expr *pRight = pRangeStart->pExpr->pRight;
118026
+ sqlite3ExprCode(pParse, pRight, regBase+nEq);
118027
+ whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
118028
+ if( (pRangeStart->wtFlags & TERM_VNULL)==0
118029
+ && sqlite3ExprCanBeNull(pRight)
118030
+ ){
118031
+ sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118032
+ VdbeCoverage(v);
118033
+ }
118034
+ if( zStartAff ){
118035
+ if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
118036
+ /* Since the comparison is to be performed with no conversions
118037
+ ** applied to the operands, set the affinity to apply to pRight to
118038
+ ** SQLITE_AFF_BLOB. */
118039
+ zStartAff[nEq] = SQLITE_AFF_BLOB;
118040
+ }
118041
+ if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
118042
+ zStartAff[nEq] = SQLITE_AFF_BLOB;
118043
+ }
118044
+ }
118045
+ nConstraint++;
118046
+ testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
118047
+ }else if( bSeekPastNull ){
118048
+ sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
118049
+ nConstraint++;
118050
+ startEq = 0;
118051
+ start_constraints = 1;
118052
+ }
118053
+ codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
118054
+ op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
118055
+ assert( op!=0 );
118056
+ sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
118057
+ VdbeCoverage(v);
118058
+ VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
118059
+ VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
118060
+ VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
118061
+ VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
118062
+ VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
118063
+ VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
118064
+
118065
+ /* Load the value for the inequality constraint at the end of the
118066
+ ** range (if any).
118067
+ */
118068
+ nConstraint = nEq;
118069
+ if( pRangeEnd ){
118070
+ Expr *pRight = pRangeEnd->pExpr->pRight;
118071
+ sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
118072
+ sqlite3ExprCode(pParse, pRight, regBase+nEq);
118073
+ whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
118074
+ if( (pRangeEnd->wtFlags & TERM_VNULL)==0
118075
+ && sqlite3ExprCanBeNull(pRight)
118076
+ ){
118077
+ sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118078
+ VdbeCoverage(v);
118079
+ }
118080
+ if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
118081
+ && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
118082
+ ){
118083
+ codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
118084
+ }
118085
+ nConstraint++;
118086
+ testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
118087
+ }else if( bStopAtNull ){
118088
+ sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
118089
+ endEq = 0;
118090
+ nConstraint++;
118091
+ }
118092
+ sqlite3DbFree(db, zStartAff);
118093
+
118094
+ /* Top of the loop body */
118095
+ pLevel->p2 = sqlite3VdbeCurrentAddr(v);
118096
+
118097
+ /* Check if the index cursor is past the end of the range. */
118098
+ if( nConstraint ){
118099
+ op = aEndOp[bRev*2 + endEq];
118100
+ sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
118101
+ testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
118102
+ testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
118103
+ testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
118104
+ testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
118105
+ }
118106
+
118107
+ /* Seek the table cursor, if required */
118108
+ disableTerm(pLevel, pRangeStart);
118109
+ disableTerm(pLevel, pRangeEnd);
118110
+ if( omitTable ){
118111
+ /* pIdx is a covering index. No need to access the main table. */
118112
+ }else if( HasRowid(pIdx->pTable) ){
118113
+ iRowidReg = ++pParse->nMem;
118114
+ sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
118115
+ sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
118116
+ sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
118117
+ }else if( iCur!=iIdxCur ){
118118
+ Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
118119
+ iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
118120
+ for(j=0; j<pPk->nKeyCol; j++){
118121
+ k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
118122
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
118123
+ }
118124
+ sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
118125
+ iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
118126
+ }
118127
+
118128
+ /* Record the instruction used to terminate the loop. Disable
118129
+ ** WHERE clause terms made redundant by the index range scan.
118130
+ */
118131
+ if( pLoop->wsFlags & WHERE_ONEROW ){
118132
+ pLevel->op = OP_Noop;
118133
+ }else if( bRev ){
118134
+ pLevel->op = OP_Prev;
118135
+ }else{
118136
+ pLevel->op = OP_Next;
118137
+ }
118138
+ pLevel->p1 = iIdxCur;
118139
+ pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
118140
+ if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
118141
+ pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
118142
+ }else{
118143
+ assert( pLevel->p5==0 );
118144
+ }
118145
+ }else
118146
+
118147
+#ifndef SQLITE_OMIT_OR_OPTIMIZATION
118148
+ if( pLoop->wsFlags & WHERE_MULTI_OR ){
118149
+ /* Case 5: Two or more separately indexed terms connected by OR
118150
+ **
118151
+ ** Example:
118152
+ **
118153
+ ** CREATE TABLE t1(a,b,c,d);
118154
+ ** CREATE INDEX i1 ON t1(a);
118155
+ ** CREATE INDEX i2 ON t1(b);
118156
+ ** CREATE INDEX i3 ON t1(c);
118157
+ **
118158
+ ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
118159
+ **
118160
+ ** In the example, there are three indexed terms connected by OR.
118161
+ ** The top of the loop looks like this:
118162
+ **
118163
+ ** Null 1 # Zero the rowset in reg 1
118164
+ **
118165
+ ** Then, for each indexed term, the following. The arguments to
118166
+ ** RowSetTest are such that the rowid of the current row is inserted
118167
+ ** into the RowSet. If it is already present, control skips the
118168
+ ** Gosub opcode and jumps straight to the code generated by WhereEnd().
118169
+ **
118170
+ ** sqlite3WhereBegin(<term>)
118171
+ ** RowSetTest # Insert rowid into rowset
118172
+ ** Gosub 2 A
118173
+ ** sqlite3WhereEnd()
118174
+ **
118175
+ ** Following the above, code to terminate the loop. Label A, the target
118176
+ ** of the Gosub above, jumps to the instruction right after the Goto.
118177
+ **
118178
+ ** Null 1 # Zero the rowset in reg 1
118179
+ ** Goto B # The loop is finished.
118180
+ **
118181
+ ** A: <loop body> # Return data, whatever.
118182
+ **
118183
+ ** Return 2 # Jump back to the Gosub
118184
+ **
118185
+ ** B: <after the loop>
118186
+ **
118187
+ ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
118188
+ ** use an ephemeral index instead of a RowSet to record the primary
118189
+ ** keys of the rows we have already seen.
118190
+ **
118191
+ */
118192
+ WhereClause *pOrWc; /* The OR-clause broken out into subterms */
118193
+ SrcList *pOrTab; /* Shortened table list or OR-clause generation */
118194
+ Index *pCov = 0; /* Potential covering index (or NULL) */
118195
+ int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
118196
+
118197
+ int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
118198
+ int regRowset = 0; /* Register for RowSet object */
118199
+ int regRowid = 0; /* Register holding rowid */
118200
+ int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
118201
+ int iRetInit; /* Address of regReturn init */
118202
+ int untestedTerms = 0; /* Some terms not completely tested */
118203
+ int ii; /* Loop counter */
118204
+ u16 wctrlFlags; /* Flags for sub-WHERE clause */
118205
+ Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
118206
+ Table *pTab = pTabItem->pTab;
118207
+
118208
+ pTerm = pLoop->aLTerm[0];
118209
+ assert( pTerm!=0 );
118210
+ assert( pTerm->eOperator & WO_OR );
118211
+ assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
118212
+ pOrWc = &pTerm->u.pOrInfo->wc;
118213
+ pLevel->op = OP_Return;
118214
+ pLevel->p1 = regReturn;
118215
+
118216
+ /* Set up a new SrcList in pOrTab containing the table being scanned
118217
+ ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
118218
+ ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
118219
+ */
118220
+ if( pWInfo->nLevel>1 ){
118221
+ int nNotReady; /* The number of notReady tables */
118222
+ struct SrcList_item *origSrc; /* Original list of tables */
118223
+ nNotReady = pWInfo->nLevel - iLevel - 1;
118224
+ pOrTab = sqlite3StackAllocRaw(db,
118225
+ sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
118226
+ if( pOrTab==0 ) return notReady;
118227
+ pOrTab->nAlloc = (u8)(nNotReady + 1);
118228
+ pOrTab->nSrc = pOrTab->nAlloc;
118229
+ memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
118230
+ origSrc = pWInfo->pTabList->a;
118231
+ for(k=1; k<=nNotReady; k++){
118232
+ memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
118233
+ }
118234
+ }else{
118235
+ pOrTab = pWInfo->pTabList;
118236
+ }
118237
+
118238
+ /* Initialize the rowset register to contain NULL. An SQL NULL is
118239
+ ** equivalent to an empty rowset. Or, create an ephemeral index
118240
+ ** capable of holding primary keys in the case of a WITHOUT ROWID.
118241
+ **
118242
+ ** Also initialize regReturn to contain the address of the instruction
118243
+ ** immediately following the OP_Return at the bottom of the loop. This
118244
+ ** is required in a few obscure LEFT JOIN cases where control jumps
118245
+ ** over the top of the loop into the body of it. In this case the
118246
+ ** correct response for the end-of-loop code (the OP_Return) is to
118247
+ ** fall through to the next instruction, just as an OP_Next does if
118248
+ ** called on an uninitialized cursor.
118249
+ */
118250
+ if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
118251
+ if( HasRowid(pTab) ){
118252
+ regRowset = ++pParse->nMem;
118253
+ sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
118254
+ }else{
118255
+ Index *pPk = sqlite3PrimaryKeyIndex(pTab);
118256
+ regRowset = pParse->nTab++;
118257
+ sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
118258
+ sqlite3VdbeSetP4KeyInfo(pParse, pPk);
118259
+ }
118260
+ regRowid = ++pParse->nMem;
118261
+ }
118262
+ iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
118263
+
118264
+ /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
118265
+ ** Then for every term xN, evaluate as the subexpression: xN AND z
118266
+ ** That way, terms in y that are factored into the disjunction will
118267
+ ** be picked up by the recursive calls to sqlite3WhereBegin() below.
118268
+ **
118269
+ ** Actually, each subexpression is converted to "xN AND w" where w is
118270
+ ** the "interesting" terms of z - terms that did not originate in the
118271
+ ** ON or USING clause of a LEFT JOIN, and terms that are usable as
118272
+ ** indices.
118273
+ **
118274
+ ** This optimization also only applies if the (x1 OR x2 OR ...) term
118275
+ ** is not contained in the ON clause of a LEFT JOIN.
118276
+ ** See ticket http://www.sqlite.org/src/info/f2369304e4
118277
+ */
118278
+ if( pWC->nTerm>1 ){
118279
+ int iTerm;
118280
+ for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
118281
+ Expr *pExpr = pWC->a[iTerm].pExpr;
118282
+ if( &pWC->a[iTerm] == pTerm ) continue;
118283
+ if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
118284
+ if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
118285
+ if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
118286
+ testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
118287
+ pExpr = sqlite3ExprDup(db, pExpr, 0);
118288
+ pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
118289
+ }
118290
+ if( pAndExpr ){
118291
+ pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
118292
+ }
118293
+ }
118294
+
118295
+ /* Run a separate WHERE clause for each term of the OR clause. After
118296
+ ** eliminating duplicates from other WHERE clauses, the action for each
118297
+ ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
118298
+ */
118299
+ wctrlFlags = WHERE_OMIT_OPEN_CLOSE
118300
+ | WHERE_FORCE_TABLE
118301
+ | WHERE_ONETABLE_ONLY
118302
+ | WHERE_NO_AUTOINDEX;
118303
+ for(ii=0; ii<pOrWc->nTerm; ii++){
118304
+ WhereTerm *pOrTerm = &pOrWc->a[ii];
118305
+ if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
118306
+ WhereInfo *pSubWInfo; /* Info for single OR-term scan */
118307
+ Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
118308
+ int j1 = 0; /* Address of jump operation */
118309
+ if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
118310
+ pAndExpr->pLeft = pOrExpr;
118311
+ pOrExpr = pAndExpr;
118312
+ }
118313
+ /* Loop through table entries that match term pOrTerm. */
118314
+ WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
118315
+ pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
118316
+ wctrlFlags, iCovCur);
118317
+ assert( pSubWInfo || pParse->nErr || db->mallocFailed );
118318
+ if( pSubWInfo ){
118319
+ WhereLoop *pSubLoop;
118320
+ int addrExplain = sqlite3WhereExplainOneScan(
118321
+ pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
118322
+ );
118323
+ sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
118324
+
118325
+ /* This is the sub-WHERE clause body. First skip over
118326
+ ** duplicate rows from prior sub-WHERE clauses, and record the
118327
+ ** rowid (or PRIMARY KEY) for the current row so that the same
118328
+ ** row will be skipped in subsequent sub-WHERE clauses.
118329
+ */
118330
+ if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
118331
+ int r;
118332
+ int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
118333
+ if( HasRowid(pTab) ){
118334
+ r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
118335
+ j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
118336
+ VdbeCoverage(v);
118337
+ }else{
118338
+ Index *pPk = sqlite3PrimaryKeyIndex(pTab);
118339
+ int nPk = pPk->nKeyCol;
118340
+ int iPk;
118341
+
118342
+ /* Read the PK into an array of temp registers. */
118343
+ r = sqlite3GetTempRange(pParse, nPk);
118344
+ for(iPk=0; iPk<nPk; iPk++){
118345
+ int iCol = pPk->aiColumn[iPk];
118346
+ sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
118347
+ }
118348
+
118349
+ /* Check if the temp table already contains this key. If so,
118350
+ ** the row has already been included in the result set and
118351
+ ** can be ignored (by jumping past the Gosub below). Otherwise,
118352
+ ** insert the key into the temp table and proceed with processing
118353
+ ** the row.
118354
+ **
118355
+ ** Use some of the same optimizations as OP_RowSetTest: If iSet
118356
+ ** is zero, assume that the key cannot already be present in
118357
+ ** the temp table. And if iSet is -1, assume that there is no
118358
+ ** need to insert the key into the temp table, as it will never
118359
+ ** be tested for. */
118360
+ if( iSet ){
118361
+ j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
118362
+ VdbeCoverage(v);
118363
+ }
118364
+ if( iSet>=0 ){
118365
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
118366
+ sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
118367
+ if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
118368
+ }
118369
+
118370
+ /* Release the array of temp registers */
118371
+ sqlite3ReleaseTempRange(pParse, r, nPk);
118372
+ }
118373
+ }
118374
+
118375
+ /* Invoke the main loop body as a subroutine */
118376
+ sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
118377
+
118378
+ /* Jump here (skipping the main loop body subroutine) if the
118379
+ ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
118380
+ if( j1 ) sqlite3VdbeJumpHere(v, j1);
118381
+
118382
+ /* The pSubWInfo->untestedTerms flag means that this OR term
118383
+ ** contained one or more AND term from a notReady table. The
118384
+ ** terms from the notReady table could not be tested and will
118385
+ ** need to be tested later.
118386
+ */
118387
+ if( pSubWInfo->untestedTerms ) untestedTerms = 1;
118388
+
118389
+ /* If all of the OR-connected terms are optimized using the same
118390
+ ** index, and the index is opened using the same cursor number
118391
+ ** by each call to sqlite3WhereBegin() made by this loop, it may
118392
+ ** be possible to use that index as a covering index.
118393
+ **
118394
+ ** If the call to sqlite3WhereBegin() above resulted in a scan that
118395
+ ** uses an index, and this is either the first OR-connected term
118396
+ ** processed or the index is the same as that used by all previous
118397
+ ** terms, set pCov to the candidate covering index. Otherwise, set
118398
+ ** pCov to NULL to indicate that no candidate covering index will
118399
+ ** be available.
118400
+ */
118401
+ pSubLoop = pSubWInfo->a[0].pWLoop;
118402
+ assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
118403
+ if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
118404
+ && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
118405
+ && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
118406
+ ){
118407
+ assert( pSubWInfo->a[0].iIdxCur==iCovCur );
118408
+ pCov = pSubLoop->u.btree.pIndex;
118409
+ wctrlFlags |= WHERE_REOPEN_IDX;
118410
+ }else{
118411
+ pCov = 0;
118412
+ }
118413
+
118414
+ /* Finish the loop through table entries that match term pOrTerm. */
118415
+ sqlite3WhereEnd(pSubWInfo);
118416
+ }
118417
+ }
118418
+ }
118419
+ pLevel->u.pCovidx = pCov;
118420
+ if( pCov ) pLevel->iIdxCur = iCovCur;
118421
+ if( pAndExpr ){
118422
+ pAndExpr->pLeft = 0;
118423
+ sqlite3ExprDelete(db, pAndExpr);
118424
+ }
118425
+ sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
118426
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
118427
+ sqlite3VdbeResolveLabel(v, iLoopBody);
118428
+
118429
+ if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
118430
+ if( !untestedTerms ) disableTerm(pLevel, pTerm);
118431
+ }else
118432
+#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
118433
+
118434
+ {
118435
+ /* Case 6: There is no usable index. We must do a complete
118436
+ ** scan of the entire table.
118437
+ */
118438
+ static const u8 aStep[] = { OP_Next, OP_Prev };
118439
+ static const u8 aStart[] = { OP_Rewind, OP_Last };
118440
+ assert( bRev==0 || bRev==1 );
118441
+ if( pTabItem->isRecursive ){
118442
+ /* Tables marked isRecursive have only a single row that is stored in
118443
+ ** a pseudo-cursor. No need to Rewind or Next such cursors. */
118444
+ pLevel->op = OP_Noop;
118445
+ }else{
118446
+ pLevel->op = aStep[bRev];
118447
+ pLevel->p1 = iCur;
118448
+ pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
118449
+ VdbeCoverageIf(v, bRev==0);
118450
+ VdbeCoverageIf(v, bRev!=0);
118451
+ pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
118452
+ }
118453
+ }
118454
+
118455
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
118456
+ pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
118457
+#endif
118458
+
118459
+ /* Insert code to test every subexpression that can be completely
118460
+ ** computed using the current set of tables.
118461
+ */
118462
+ for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
118463
+ Expr *pE;
118464
+ int skipLikeAddr = 0;
118465
+ testcase( pTerm->wtFlags & TERM_VIRTUAL );
118466
+ testcase( pTerm->wtFlags & TERM_CODED );
118467
+ if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118468
+ if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
118469
+ testcase( pWInfo->untestedTerms==0
118470
+ && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
118471
+ pWInfo->untestedTerms = 1;
118472
+ continue;
118473
+ }
118474
+ pE = pTerm->pExpr;
118475
+ assert( pE!=0 );
118476
+ if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
118477
+ continue;
118478
+ }
118479
+ if( pTerm->wtFlags & TERM_LIKECOND ){
118480
+ assert( pLevel->iLikeRepCntr>0 );
118481
+ skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
118482
+ VdbeCoverage(v);
118483
+ }
118484
+ sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
118485
+ if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
118486
+ pTerm->wtFlags |= TERM_CODED;
118487
+ }
118488
+
118489
+ /* Insert code to test for implied constraints based on transitivity
118490
+ ** of the "==" operator.
118491
+ **
118492
+ ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
118493
+ ** and we are coding the t1 loop and the t2 loop has not yet coded,
118494
+ ** then we cannot use the "t1.a=t2.b" constraint, but we can code
118495
+ ** the implied "t1.a=123" constraint.
118496
+ */
118497
+ for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
118498
+ Expr *pE, *pEAlt;
118499
+ WhereTerm *pAlt;
118500
+ if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118501
+ if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
118502
+ if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
118503
+ if( pTerm->leftCursor!=iCur ) continue;
118504
+ if( pLevel->iLeftJoin ) continue;
118505
+ pE = pTerm->pExpr;
118506
+ assert( !ExprHasProperty(pE, EP_FromJoin) );
118507
+ assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
118508
+ pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
118509
+ WO_EQ|WO_IN|WO_IS, 0);
118510
+ if( pAlt==0 ) continue;
118511
+ if( pAlt->wtFlags & (TERM_CODED) ) continue;
118512
+ testcase( pAlt->eOperator & WO_EQ );
118513
+ testcase( pAlt->eOperator & WO_IS );
118514
+ testcase( pAlt->eOperator & WO_IN );
118515
+ VdbeModuleComment((v, "begin transitive constraint"));
118516
+ pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
118517
+ if( pEAlt ){
118518
+ *pEAlt = *pAlt->pExpr;
118519
+ pEAlt->pLeft = pE->pLeft;
118520
+ sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
118521
+ sqlite3StackFree(db, pEAlt);
118522
+ }
118523
+ }
118524
+
118525
+ /* For a LEFT OUTER JOIN, generate code that will record the fact that
118526
+ ** at least one row of the right table has matched the left table.
118527
+ */
118528
+ if( pLevel->iLeftJoin ){
118529
+ pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
118530
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
118531
+ VdbeComment((v, "record LEFT JOIN hit"));
118532
+ sqlite3ExprCacheClear(pParse);
118533
+ for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
118534
+ testcase( pTerm->wtFlags & TERM_VIRTUAL );
118535
+ testcase( pTerm->wtFlags & TERM_CODED );
118536
+ if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118537
+ if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
118538
+ assert( pWInfo->untestedTerms );
118539
+ continue;
118540
+ }
118541
+ assert( pTerm->pExpr );
118542
+ sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
118543
+ pTerm->wtFlags |= TERM_CODED;
118544
+ }
118545
+ }
118546
+
118547
+ return pLevel->notReady;
118548
+}
118549
+
118550
+/************** End of wherecode.c *******************************************/
118551
+/************** Begin file whereexpr.c ***************************************/
118552
+/*
118553
+** 2015-06-08
118554
+**
118555
+** The author disclaims copyright to this source code. In place of
118556
+** a legal notice, here is a blessing:
118557
+**
118558
+** May you do good and not evil.
118559
+** May you find forgiveness for yourself and forgive others.
118560
+** May you share freely, never taking more than you give.
118561
+**
118562
+*************************************************************************
118563
+** This module contains C code that generates VDBE code used to process
118564
+** the WHERE clause of SQL statements.
118565
+**
118566
+** This file was originally part of where.c but was split out to improve
118567
+** readability and editabiliity. This file contains utility routines for
118568
+** analyzing Expr objects in the WHERE clause.
118569
+*/
118570
+
118571
+/* Forward declarations */
118572
+static void exprAnalyze(SrcList*, WhereClause*, int);
116438118573
116439118574
/*
116440118575
** Deallocate all memory associated with a WhereOrInfo object.
116441118576
*/
116442118577
static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
116443
- whereClauseClear(&p->wc);
118578
+ sqlite3WhereClauseClear(&p->wc);
116444118579
sqlite3DbFree(db, p);
116445118580
}
116446118581
116447118582
/*
116448118583
** Deallocate all memory associated with a WhereAndInfo object.
116449118584
*/
116450118585
static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
116451
- whereClauseClear(&p->wc);
118586
+ sqlite3WhereClauseClear(&p->wc);
116452118587
sqlite3DbFree(db, p);
116453118588
}
116454118589
116455
-/*
116456
-** Deallocate a WhereClause structure. The WhereClause structure
116457
-** itself is not freed. This routine is the inverse of whereClauseInit().
116458
-*/
116459
-static void whereClauseClear(WhereClause *pWC){
116460
- int i;
116461
- WhereTerm *a;
116462
- sqlite3 *db = pWC->pWInfo->pParse->db;
116463
- for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
116464
- if( a->wtFlags & TERM_DYNAMIC ){
116465
- sqlite3ExprDelete(db, a->pExpr);
116466
- }
116467
- if( a->wtFlags & TERM_ORINFO ){
116468
- whereOrInfoDelete(db, a->u.pOrInfo);
116469
- }else if( a->wtFlags & TERM_ANDINFO ){
116470
- whereAndInfoDelete(db, a->u.pAndInfo);
116471
- }
116472
- }
116473
- if( pWC->a!=pWC->aStatic ){
116474
- sqlite3DbFree(db, pWC->a);
116475
- }
116476
-}
116477
-
116478118590
/*
116479118591
** Add a single new WhereTerm entry to the WhereClause object pWC.
116480118592
** The new WhereTerm object is constructed from Expr p and with wtFlags.
116481118593
** The index in pWC->a[] of the new WhereTerm is returned on success.
116482118594
** 0 is returned if the new WhereTerm could not be added due to a memory
@@ -116527,126 +118639,10 @@
116527118639
pTerm->pWC = pWC;
116528118640
pTerm->iParent = -1;
116529118641
return idx;
116530118642
}
116531118643
116532
-/*
116533
-** This routine identifies subexpressions in the WHERE clause where
116534
-** each subexpression is separated by the AND operator or some other
116535
-** operator specified in the op parameter. The WhereClause structure
116536
-** is filled with pointers to subexpressions. For example:
116537
-**
116538
-** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
116539
-** \________/ \_______________/ \________________/
116540
-** slot[0] slot[1] slot[2]
116541
-**
116542
-** The original WHERE clause in pExpr is unaltered. All this routine
116543
-** does is make slot[] entries point to substructure within pExpr.
116544
-**
116545
-** In the previous sentence and in the diagram, "slot[]" refers to
116546
-** the WhereClause.a[] array. The slot[] array grows as needed to contain
116547
-** all terms of the WHERE clause.
116548
-*/
116549
-static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
116550
- Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
116551
- pWC->op = op;
116552
- if( pE2==0 ) return;
116553
- if( pE2->op!=op ){
116554
- whereClauseInsert(pWC, pExpr, 0);
116555
- }else{
116556
- whereSplit(pWC, pE2->pLeft, op);
116557
- whereSplit(pWC, pE2->pRight, op);
116558
- }
116559
-}
116560
-
116561
-/*
116562
-** Initialize a WhereMaskSet object
116563
-*/
116564
-#define initMaskSet(P) (P)->n=0
116565
-
116566
-/*
116567
-** Return the bitmask for the given cursor number. Return 0 if
116568
-** iCursor is not in the set.
116569
-*/
116570
-static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
116571
- int i;
116572
- assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
116573
- for(i=0; i<pMaskSet->n; i++){
116574
- if( pMaskSet->ix[i]==iCursor ){
116575
- return MASKBIT(i);
116576
- }
116577
- }
116578
- return 0;
116579
-}
116580
-
116581
-/*
116582
-** Create a new mask for cursor iCursor.
116583
-**
116584
-** There is one cursor per table in the FROM clause. The number of
116585
-** tables in the FROM clause is limited by a test early in the
116586
-** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
116587
-** array will never overflow.
116588
-*/
116589
-static void createMask(WhereMaskSet *pMaskSet, int iCursor){
116590
- assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
116591
- pMaskSet->ix[pMaskSet->n++] = iCursor;
116592
-}
116593
-
116594
-/*
116595
-** These routines walk (recursively) an expression tree and generate
116596
-** a bitmask indicating which tables are used in that expression
116597
-** tree.
116598
-*/
116599
-static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
116600
-static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
116601
-static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
116602
- Bitmask mask = 0;
116603
- if( p==0 ) return 0;
116604
- if( p->op==TK_COLUMN ){
116605
- mask = getMask(pMaskSet, p->iTable);
116606
- return mask;
116607
- }
116608
- mask = exprTableUsage(pMaskSet, p->pRight);
116609
- mask |= exprTableUsage(pMaskSet, p->pLeft);
116610
- if( ExprHasProperty(p, EP_xIsSelect) ){
116611
- mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
116612
- }else{
116613
- mask |= exprListTableUsage(pMaskSet, p->x.pList);
116614
- }
116615
- return mask;
116616
-}
116617
-static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
116618
- int i;
116619
- Bitmask mask = 0;
116620
- if( pList ){
116621
- for(i=0; i<pList->nExpr; i++){
116622
- mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
116623
- }
116624
- }
116625
- return mask;
116626
-}
116627
-static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
116628
- Bitmask mask = 0;
116629
- while( pS ){
116630
- SrcList *pSrc = pS->pSrc;
116631
- mask |= exprListTableUsage(pMaskSet, pS->pEList);
116632
- mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
116633
- mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
116634
- mask |= exprTableUsage(pMaskSet, pS->pWhere);
116635
- mask |= exprTableUsage(pMaskSet, pS->pHaving);
116636
- if( ALWAYS(pSrc!=0) ){
116637
- int i;
116638
- for(i=0; i<pSrc->nSrc; i++){
116639
- mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
116640
- mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
116641
- }
116642
- }
116643
- pS = pS->pPrior;
116644
- }
116645
- return mask;
116646
-}
116647
-
116648118644
/*
116649118645
** Return TRUE if the given operator is one of the operators that is
116650118646
** allowed for an indexable WHERE clause term. The allowed operators are
116651118647
** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
116652118648
*/
@@ -116723,203 +118719,10 @@
116723118719
assert( op!=TK_GE || c==WO_GE );
116724118720
assert( op!=TK_IS || c==WO_IS );
116725118721
return c;
116726118722
}
116727118723
116728
-/*
116729
-** Advance to the next WhereTerm that matches according to the criteria
116730
-** established when the pScan object was initialized by whereScanInit().
116731
-** Return NULL if there are no more matching WhereTerms.
116732
-*/
116733
-static WhereTerm *whereScanNext(WhereScan *pScan){
116734
- int iCur; /* The cursor on the LHS of the term */
116735
- int iColumn; /* The column on the LHS of the term. -1 for IPK */
116736
- Expr *pX; /* An expression being tested */
116737
- WhereClause *pWC; /* Shorthand for pScan->pWC */
116738
- WhereTerm *pTerm; /* The term being tested */
116739
- int k = pScan->k; /* Where to start scanning */
116740
-
116741
- while( pScan->iEquiv<=pScan->nEquiv ){
116742
- iCur = pScan->aEquiv[pScan->iEquiv-2];
116743
- iColumn = pScan->aEquiv[pScan->iEquiv-1];
116744
- while( (pWC = pScan->pWC)!=0 ){
116745
- for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
116746
- if( pTerm->leftCursor==iCur
116747
- && pTerm->u.leftColumn==iColumn
116748
- && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
116749
- ){
116750
- if( (pTerm->eOperator & WO_EQUIV)!=0
116751
- && pScan->nEquiv<ArraySize(pScan->aEquiv)
116752
- ){
116753
- int j;
116754
- pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
116755
- assert( pX->op==TK_COLUMN );
116756
- for(j=0; j<pScan->nEquiv; j+=2){
116757
- if( pScan->aEquiv[j]==pX->iTable
116758
- && pScan->aEquiv[j+1]==pX->iColumn ){
116759
- break;
116760
- }
116761
- }
116762
- if( j==pScan->nEquiv ){
116763
- pScan->aEquiv[j] = pX->iTable;
116764
- pScan->aEquiv[j+1] = pX->iColumn;
116765
- pScan->nEquiv += 2;
116766
- }
116767
- }
116768
- if( (pTerm->eOperator & pScan->opMask)!=0 ){
116769
- /* Verify the affinity and collating sequence match */
116770
- if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
116771
- CollSeq *pColl;
116772
- Parse *pParse = pWC->pWInfo->pParse;
116773
- pX = pTerm->pExpr;
116774
- if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
116775
- continue;
116776
- }
116777
- assert(pX->pLeft);
116778
- pColl = sqlite3BinaryCompareCollSeq(pParse,
116779
- pX->pLeft, pX->pRight);
116780
- if( pColl==0 ) pColl = pParse->db->pDfltColl;
116781
- if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
116782
- continue;
116783
- }
116784
- }
116785
- if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
116786
- && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
116787
- && pX->iTable==pScan->aEquiv[0]
116788
- && pX->iColumn==pScan->aEquiv[1]
116789
- ){
116790
- testcase( pTerm->eOperator & WO_IS );
116791
- continue;
116792
- }
116793
- pScan->k = k+1;
116794
- return pTerm;
116795
- }
116796
- }
116797
- }
116798
- pScan->pWC = pScan->pWC->pOuter;
116799
- k = 0;
116800
- }
116801
- pScan->pWC = pScan->pOrigWC;
116802
- k = 0;
116803
- pScan->iEquiv += 2;
116804
- }
116805
- return 0;
116806
-}
116807
-
116808
-/*
116809
-** Initialize a WHERE clause scanner object. Return a pointer to the
116810
-** first match. Return NULL if there are no matches.
116811
-**
116812
-** The scanner will be searching the WHERE clause pWC. It will look
116813
-** for terms of the form "X <op> <expr>" where X is column iColumn of table
116814
-** iCur. The <op> must be one of the operators described by opMask.
116815
-**
116816
-** If the search is for X and the WHERE clause contains terms of the
116817
-** form X=Y then this routine might also return terms of the form
116818
-** "Y <op> <expr>". The number of levels of transitivity is limited,
116819
-** but is enough to handle most commonly occurring SQL statements.
116820
-**
116821
-** If X is not the INTEGER PRIMARY KEY then X must be compatible with
116822
-** index pIdx.
116823
-*/
116824
-static WhereTerm *whereScanInit(
116825
- WhereScan *pScan, /* The WhereScan object being initialized */
116826
- WhereClause *pWC, /* The WHERE clause to be scanned */
116827
- int iCur, /* Cursor to scan for */
116828
- int iColumn, /* Column to scan for */
116829
- u32 opMask, /* Operator(s) to scan for */
116830
- Index *pIdx /* Must be compatible with this index */
116831
-){
116832
- int j;
116833
-
116834
- /* memset(pScan, 0, sizeof(*pScan)); */
116835
- pScan->pOrigWC = pWC;
116836
- pScan->pWC = pWC;
116837
- if( pIdx && iColumn>=0 ){
116838
- pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
116839
- for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
116840
- if( NEVER(j>pIdx->nColumn) ) return 0;
116841
- }
116842
- pScan->zCollName = pIdx->azColl[j];
116843
- }else{
116844
- pScan->idxaff = 0;
116845
- pScan->zCollName = 0;
116846
- }
116847
- pScan->opMask = opMask;
116848
- pScan->k = 0;
116849
- pScan->aEquiv[0] = iCur;
116850
- pScan->aEquiv[1] = iColumn;
116851
- pScan->nEquiv = 2;
116852
- pScan->iEquiv = 2;
116853
- return whereScanNext(pScan);
116854
-}
116855
-
116856
-/*
116857
-** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
116858
-** where X is a reference to the iColumn of table iCur and <op> is one of
116859
-** the WO_xx operator codes specified by the op parameter.
116860
-** Return a pointer to the term. Return 0 if not found.
116861
-**
116862
-** The term returned might by Y=<expr> if there is another constraint in
116863
-** the WHERE clause that specifies that X=Y. Any such constraints will be
116864
-** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
116865
-** aEquiv[] array holds X and all its equivalents, with each SQL variable
116866
-** taking up two slots in aEquiv[]. The first slot is for the cursor number
116867
-** and the second is for the column number. There are 22 slots in aEquiv[]
116868
-** so that means we can look for X plus up to 10 other equivalent values.
116869
-** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
116870
-** and ... and A9=A10 and A10=<expr>.
116871
-**
116872
-** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
116873
-** then try for the one with no dependencies on <expr> - in other words where
116874
-** <expr> is a constant expression of some kind. Only return entries of
116875
-** the form "X <op> Y" where Y is a column in another table if no terms of
116876
-** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
116877
-** exist, try to return a term that does not use WO_EQUIV.
116878
-*/
116879
-static WhereTerm *findTerm(
116880
- WhereClause *pWC, /* The WHERE clause to be searched */
116881
- int iCur, /* Cursor number of LHS */
116882
- int iColumn, /* Column number of LHS */
116883
- Bitmask notReady, /* RHS must not overlap with this mask */
116884
- u32 op, /* Mask of WO_xx values describing operator */
116885
- Index *pIdx /* Must be compatible with this index, if not NULL */
116886
-){
116887
- WhereTerm *pResult = 0;
116888
- WhereTerm *p;
116889
- WhereScan scan;
116890
-
116891
- p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
116892
- op &= WO_EQ|WO_IS;
116893
- while( p ){
116894
- if( (p->prereqRight & notReady)==0 ){
116895
- if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
116896
- testcase( p->eOperator & WO_IS );
116897
- return p;
116898
- }
116899
- if( pResult==0 ) pResult = p;
116900
- }
116901
- p = whereScanNext(&scan);
116902
- }
116903
- return pResult;
116904
-}
116905
-
116906
-/* Forward reference */
116907
-static void exprAnalyze(SrcList*, WhereClause*, int);
116908
-
116909
-/*
116910
-** Call exprAnalyze on all terms in a WHERE clause.
116911
-*/
116912
-static void exprAnalyzeAll(
116913
- SrcList *pTabList, /* the FROM clause */
116914
- WhereClause *pWC /* the WHERE clause to be analyzed */
116915
-){
116916
- int i;
116917
- for(i=pWC->nTerm-1; i>=0; i--){
116918
- exprAnalyze(pTabList, pWC, i);
116919
- }
116920
-}
116921118724
116922118725
#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
116923118726
/*
116924118727
** Check to see if the given expression is a LIKE or GLOB operator that
116925118728
** can be optimized using inequality constraints. Return TRUE if it is
@@ -116970,11 +118773,11 @@
116970118773
pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
116971118774
op = pRight->op;
116972118775
if( op==TK_VARIABLE ){
116973118776
Vdbe *pReprepare = pParse->pReprepare;
116974118777
int iCol = pRight->iColumn;
116975
- pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
118778
+ pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
116976118779
if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
116977118780
z = (char *)sqlite3_value_text(pVal);
116978118781
}
116979118782
sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
116980118783
assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
@@ -117181,11 +118984,11 @@
117181118984
**
117182118985
** x IN (expr1,expr2,expr3)
117183118986
**
117184118987
** CASE 2:
117185118988
**
117186
-** If there are exactly two disjuncts one side has x>A and the other side
118989
+** If there are exactly two disjuncts and one side has x>A and the other side
117187118990
** has x=A (for the same x and A) then add a new virtual conjunct term to the
117188118991
** WHERE clause of the form "x>=A". Example:
117189118992
**
117190118993
** x>A OR (x=A AND y>B) adds: x>=A
117191118994
**
@@ -117210,26 +119013,26 @@
117210119013
** potentially be used with an index if an appropriate index exists.
117211119014
** This analysis does not consider whether or not the index exists; that
117212119015
** is decided elsewhere. This analysis only looks at whether subterms
117213119016
** appropriate for indexing exist.
117214119017
**
117215
-** All examples A through E above satisfy case 2. But if a term
119018
+** All examples A through E above satisfy case 3. But if a term
117216119019
** also satisfies case 1 (such as B) we know that the optimizer will
117217
-** always prefer case 1, so in that case we pretend that case 2 is not
119020
+** always prefer case 1, so in that case we pretend that case 3 is not
117218119021
** satisfied.
117219119022
**
117220119023
** It might be the case that multiple tables are indexable. For example,
117221119024
** (E) above is indexable on tables P, Q, and R.
117222119025
**
117223
-** Terms that satisfy case 2 are candidates for lookup by using
119026
+** Terms that satisfy case 3 are candidates for lookup by using
117224119027
** separate indices to find rowids for each subterm and composing
117225119028
** the union of all rowids using a RowSet object. This is similar
117226119029
** to "bitmap indices" in other database engines.
117227119030
**
117228119031
** OTHERWISE:
117229119032
**
117230
-** If neither case 1 nor case 2 apply, then leave the eOperator set to
119033
+** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
117231119034
** zero. This term is not useful for search.
117232119035
*/
117233119036
static void exprAnalyzeOrTerm(
117234119037
SrcList *pSrc, /* the FROM clause */
117235119038
WhereClause *pWC, /* the complete WHERE clause */
@@ -117256,18 +119059,18 @@
117256119059
assert( pExpr->op==TK_OR );
117257119060
pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
117258119061
if( pOrInfo==0 ) return;
117259119062
pTerm->wtFlags |= TERM_ORINFO;
117260119063
pOrWc = &pOrInfo->wc;
117261
- whereClauseInit(pOrWc, pWInfo);
117262
- whereSplit(pOrWc, pExpr, TK_OR);
117263
- exprAnalyzeAll(pSrc, pOrWc);
119064
+ sqlite3WhereClauseInit(pOrWc, pWInfo);
119065
+ sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
119066
+ sqlite3WhereExprAnalyze(pSrc, pOrWc);
117264119067
if( db->mallocFailed ) return;
117265119068
assert( pOrWc->nTerm>=2 );
117266119069
117267119070
/*
117268
- ** Compute the set of tables that might satisfy cases 1 or 2.
119071
+ ** Compute the set of tables that might satisfy cases 1 or 3.
117269119072
*/
117270119073
indexable = ~(Bitmask)0;
117271119074
chngToIN = ~(Bitmask)0;
117272119075
for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
117273119076
if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
@@ -117282,20 +119085,20 @@
117282119085
Bitmask b = 0;
117283119086
pOrTerm->u.pAndInfo = pAndInfo;
117284119087
pOrTerm->wtFlags |= TERM_ANDINFO;
117285119088
pOrTerm->eOperator = WO_AND;
117286119089
pAndWC = &pAndInfo->wc;
117287
- whereClauseInit(pAndWC, pWC->pWInfo);
117288
- whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
117289
- exprAnalyzeAll(pSrc, pAndWC);
119090
+ sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
119091
+ sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
119092
+ sqlite3WhereExprAnalyze(pSrc, pAndWC);
117290119093
pAndWC->pOuter = pWC;
117291119094
testcase( db->mallocFailed );
117292119095
if( !db->mallocFailed ){
117293119096
for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
117294119097
assert( pAndTerm->pExpr );
117295119098
if( allowedOp(pAndTerm->pExpr->op) ){
117296
- b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
119099
+ b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
117297119100
}
117298119101
}
117299119102
}
117300119103
indexable &= b;
117301119104
}
@@ -117302,14 +119105,14 @@
117302119105
}else if( pOrTerm->wtFlags & TERM_COPIED ){
117303119106
/* Skip this term for now. We revisit it when we process the
117304119107
** corresponding TERM_VIRTUAL term */
117305119108
}else{
117306119109
Bitmask b;
117307
- b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
119110
+ b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
117308119111
if( pOrTerm->wtFlags & TERM_VIRTUAL ){
117309119112
WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
117310
- b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
119113
+ b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
117311119114
}
117312119115
indexable &= b;
117313119116
if( (pOrTerm->eOperator & WO_EQ)==0 ){
117314119117
chngToIN = 0;
117315119118
}else{
@@ -117381,11 +119184,12 @@
117381119184
/* This is the 2-bit case and we are on the second iteration and
117382119185
** current term is from the first iteration. So skip this term. */
117383119186
assert( j==1 );
117384119187
continue;
117385119188
}
117386
- if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
119189
+ if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
119190
+ pOrTerm->leftCursor))==0 ){
117387119191
/* This term must be of the form t1.a==t2.b where t2 is in the
117388119192
** chngToIN set but t1 is not. This term will be either preceded
117389119193
** or follwed by an inverted copy (t2.b==t1.a). Skip this term
117390119194
** and use its inversion. */
117391119195
testcase( pOrTerm->wtFlags & TERM_COPIED );
@@ -117400,11 +119204,11 @@
117400119204
if( i<0 ){
117401119205
/* No candidate table+column was found. This can only occur
117402119206
** on the second iteration */
117403119207
assert( j==1 );
117404119208
assert( IsPowerOfTwo(chngToIN) );
117405
- assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
119209
+ assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
117406119210
break;
117407119211
}
117408119212
testcase( j==1 );
117409119213
117410119214
/* We have found a candidate table and column. Check to see if that
@@ -117478,11 +119282,11 @@
117478119282
** We already know that pExpr is a binary operator where both operands are
117479119283
** column references. This routine checks to see if pExpr is an equivalence
117480119284
** relation:
117481119285
** 1. The SQLITE_Transitive optimization must be enabled
117482119286
** 2. Must be either an == or an IS operator
117483
-** 3. Not originating the ON clause of an OUTER JOIN
119287
+** 3. Not originating in the ON clause of an OUTER JOIN
117484119288
** 4. The affinities of A and B must be compatible
117485119289
** 5a. Both operands use the same collating sequence OR
117486119290
** 5b. The overall collating sequence is BINARY
117487119291
** If this routine returns TRUE, that means that the RHS can be substituted
117488119292
** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
@@ -117511,10 +119315,36 @@
117511119315
zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
117512119316
pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
117513119317
zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
117514119318
return sqlite3StrICmp(zColl1, zColl2)==0;
117515119319
}
119320
+
119321
+/*
119322
+** Recursively walk the expressions of a SELECT statement and generate
119323
+** a bitmask indicating which tables are used in that expression
119324
+** tree.
119325
+*/
119326
+static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
119327
+ Bitmask mask = 0;
119328
+ while( pS ){
119329
+ SrcList *pSrc = pS->pSrc;
119330
+ mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
119331
+ mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
119332
+ mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
119333
+ mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
119334
+ mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
119335
+ if( ALWAYS(pSrc!=0) ){
119336
+ int i;
119337
+ for(i=0; i<pSrc->nSrc; i++){
119338
+ mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
119339
+ mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
119340
+ }
119341
+ }
119342
+ pS = pS->pPrior;
119343
+ }
119344
+ return mask;
119345
+}
117516119346
117517119347
/*
117518119348
** The input to this routine is an WhereTerm structure with only the
117519119349
** "pExpr" field filled in. The job of this routine is to analyze the
117520119350
** subexpression and populate all the other fields of the WhereTerm
@@ -117556,27 +119386,27 @@
117556119386
}
117557119387
pTerm = &pWC->a[idxTerm];
117558119388
pMaskSet = &pWInfo->sMaskSet;
117559119389
pExpr = pTerm->pExpr;
117560119390
assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
117561
- prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
119391
+ prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
117562119392
op = pExpr->op;
117563119393
if( op==TK_IN ){
117564119394
assert( pExpr->pRight==0 );
117565119395
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
117566
- pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
119396
+ pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
117567119397
}else{
117568
- pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
119398
+ pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
117569119399
}
117570119400
}else if( op==TK_ISNULL ){
117571119401
pTerm->prereqRight = 0;
117572119402
}else{
117573
- pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
119403
+ pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
117574119404
}
117575
- prereqAll = exprTableUsage(pMaskSet, pExpr);
119405
+ prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
117576119406
if( ExprHasProperty(pExpr, EP_FromJoin) ){
117577
- Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
119407
+ Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
117578119408
prereqAll |= x;
117579119409
extraRight = x-1; /* ON clause terms may not be used with an index
117580119410
** on left table of a LEFT JOIN. Ticket #3015 */
117581119411
}
117582119412
pTerm->prereqAll = prereqAll;
@@ -117777,12 +119607,12 @@
117777119607
WhereTerm *pNewTerm;
117778119608
Bitmask prereqColumn, prereqExpr;
117779119609
117780119610
pRight = pExpr->x.pList->a[0].pExpr;
117781119611
pLeft = pExpr->x.pList->a[1].pExpr;
117782
- prereqExpr = exprTableUsage(pMaskSet, pRight);
117783
- prereqColumn = exprTableUsage(pMaskSet, pLeft);
119612
+ prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
119613
+ prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
117784119614
if( (prereqExpr & prereqColumn)==0 ){
117785119615
Expr *pNewExpr;
117786119616
pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
117787119617
0, sqlite3ExprDup(db, pRight, 0), 0);
117788119618
idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
@@ -117841,10 +119671,477 @@
117841119671
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
117842119672
** an index for tables to the left of the join.
117843119673
*/
117844119674
pTerm->prereqRight |= extraRight;
117845119675
}
119676
+
119677
+/***************************************************************************
119678
+** Routines with file scope above. Interface to the rest of the where.c
119679
+** subsystem follows.
119680
+***************************************************************************/
119681
+
119682
+/*
119683
+** This routine identifies subexpressions in the WHERE clause where
119684
+** each subexpression is separated by the AND operator or some other
119685
+** operator specified in the op parameter. The WhereClause structure
119686
+** is filled with pointers to subexpressions. For example:
119687
+**
119688
+** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
119689
+** \________/ \_______________/ \________________/
119690
+** slot[0] slot[1] slot[2]
119691
+**
119692
+** The original WHERE clause in pExpr is unaltered. All this routine
119693
+** does is make slot[] entries point to substructure within pExpr.
119694
+**
119695
+** In the previous sentence and in the diagram, "slot[]" refers to
119696
+** the WhereClause.a[] array. The slot[] array grows as needed to contain
119697
+** all terms of the WHERE clause.
119698
+*/
119699
+SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
119700
+ Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
119701
+ pWC->op = op;
119702
+ if( pE2==0 ) return;
119703
+ if( pE2->op!=op ){
119704
+ whereClauseInsert(pWC, pExpr, 0);
119705
+ }else{
119706
+ sqlite3WhereSplit(pWC, pE2->pLeft, op);
119707
+ sqlite3WhereSplit(pWC, pE2->pRight, op);
119708
+ }
119709
+}
119710
+
119711
+/*
119712
+** Initialize a preallocated WhereClause structure.
119713
+*/
119714
+SQLITE_PRIVATE void sqlite3WhereClauseInit(
119715
+ WhereClause *pWC, /* The WhereClause to be initialized */
119716
+ WhereInfo *pWInfo /* The WHERE processing context */
119717
+){
119718
+ pWC->pWInfo = pWInfo;
119719
+ pWC->pOuter = 0;
119720
+ pWC->nTerm = 0;
119721
+ pWC->nSlot = ArraySize(pWC->aStatic);
119722
+ pWC->a = pWC->aStatic;
119723
+}
119724
+
119725
+/*
119726
+** Deallocate a WhereClause structure. The WhereClause structure
119727
+** itself is not freed. This routine is the inverse of sqlite3WhereClauseInit().
119728
+*/
119729
+SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
119730
+ int i;
119731
+ WhereTerm *a;
119732
+ sqlite3 *db = pWC->pWInfo->pParse->db;
119733
+ for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
119734
+ if( a->wtFlags & TERM_DYNAMIC ){
119735
+ sqlite3ExprDelete(db, a->pExpr);
119736
+ }
119737
+ if( a->wtFlags & TERM_ORINFO ){
119738
+ whereOrInfoDelete(db, a->u.pOrInfo);
119739
+ }else if( a->wtFlags & TERM_ANDINFO ){
119740
+ whereAndInfoDelete(db, a->u.pAndInfo);
119741
+ }
119742
+ }
119743
+ if( pWC->a!=pWC->aStatic ){
119744
+ sqlite3DbFree(db, pWC->a);
119745
+ }
119746
+}
119747
+
119748
+
119749
+/*
119750
+** These routines walk (recursively) an expression tree and generate
119751
+** a bitmask indicating which tables are used in that expression
119752
+** tree.
119753
+*/
119754
+SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
119755
+ Bitmask mask = 0;
119756
+ if( p==0 ) return 0;
119757
+ if( p->op==TK_COLUMN ){
119758
+ mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
119759
+ return mask;
119760
+ }
119761
+ mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
119762
+ mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
119763
+ if( ExprHasProperty(p, EP_xIsSelect) ){
119764
+ mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
119765
+ }else{
119766
+ mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
119767
+ }
119768
+ return mask;
119769
+}
119770
+SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
119771
+ int i;
119772
+ Bitmask mask = 0;
119773
+ if( pList ){
119774
+ for(i=0; i<pList->nExpr; i++){
119775
+ mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
119776
+ }
119777
+ }
119778
+ return mask;
119779
+}
119780
+
119781
+
119782
+/*
119783
+** Call exprAnalyze on all terms in a WHERE clause.
119784
+**
119785
+** Note that exprAnalyze() might add new virtual terms onto the
119786
+** end of the WHERE clause. We do not want to analyze these new
119787
+** virtual terms, so start analyzing at the end and work forward
119788
+** so that the added virtual terms are never processed.
119789
+*/
119790
+SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
119791
+ SrcList *pTabList, /* the FROM clause */
119792
+ WhereClause *pWC /* the WHERE clause to be analyzed */
119793
+){
119794
+ int i;
119795
+ for(i=pWC->nTerm-1; i>=0; i--){
119796
+ exprAnalyze(pTabList, pWC, i);
119797
+ }
119798
+}
119799
+
119800
+/************** End of whereexpr.c *******************************************/
119801
+/************** Begin file where.c *******************************************/
119802
+/*
119803
+** 2001 September 15
119804
+**
119805
+** The author disclaims copyright to this source code. In place of
119806
+** a legal notice, here is a blessing:
119807
+**
119808
+** May you do good and not evil.
119809
+** May you find forgiveness for yourself and forgive others.
119810
+** May you share freely, never taking more than you give.
119811
+**
119812
+*************************************************************************
119813
+** This module contains C code that generates VDBE code used to process
119814
+** the WHERE clause of SQL statements. This module is responsible for
119815
+** generating the code that loops through a table looking for applicable
119816
+** rows. Indices are selected and used to speed the search when doing
119817
+** so is applicable. Because this module is responsible for selecting
119818
+** indices, you might also think of this module as the "query optimizer".
119819
+*/
119820
+
119821
+/* Forward declaration of methods */
119822
+static int whereLoopResize(sqlite3*, WhereLoop*, int);
119823
+
119824
+/* Test variable that can be set to enable WHERE tracing */
119825
+#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
119826
+/***/ int sqlite3WhereTrace = 0;
119827
+#endif
119828
+
119829
+
119830
+/*
119831
+** Return the estimated number of output rows from a WHERE clause
119832
+*/
119833
+SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
119834
+ return sqlite3LogEstToInt(pWInfo->nRowOut);
119835
+}
119836
+
119837
+/*
119838
+** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
119839
+** WHERE clause returns outputs for DISTINCT processing.
119840
+*/
119841
+SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
119842
+ return pWInfo->eDistinct;
119843
+}
119844
+
119845
+/*
119846
+** Return TRUE if the WHERE clause returns rows in ORDER BY order.
119847
+** Return FALSE if the output needs to be sorted.
119848
+*/
119849
+SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
119850
+ return pWInfo->nOBSat;
119851
+}
119852
+
119853
+/*
119854
+** Return the VDBE address or label to jump to in order to continue
119855
+** immediately with the next row of a WHERE clause.
119856
+*/
119857
+SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
119858
+ assert( pWInfo->iContinue!=0 );
119859
+ return pWInfo->iContinue;
119860
+}
119861
+
119862
+/*
119863
+** Return the VDBE address or label to jump to in order to break
119864
+** out of a WHERE loop.
119865
+*/
119866
+SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
119867
+ return pWInfo->iBreak;
119868
+}
119869
+
119870
+/*
119871
+** Return TRUE if an UPDATE or DELETE statement can operate directly on
119872
+** the rowids returned by a WHERE clause. Return FALSE if doing an
119873
+** UPDATE or DELETE might change subsequent WHERE clause results.
119874
+**
119875
+** If the ONEPASS optimization is used (if this routine returns true)
119876
+** then also write the indices of open cursors used by ONEPASS
119877
+** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
119878
+** table and iaCur[1] gets the cursor used by an auxiliary index.
119879
+** Either value may be -1, indicating that cursor is not used.
119880
+** Any cursors returned will have been opened for writing.
119881
+**
119882
+** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
119883
+** unable to use the ONEPASS optimization.
119884
+*/
119885
+SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
119886
+ memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
119887
+ return pWInfo->okOnePass;
119888
+}
119889
+
119890
+/*
119891
+** Move the content of pSrc into pDest
119892
+*/
119893
+static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
119894
+ pDest->n = pSrc->n;
119895
+ memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
119896
+}
119897
+
119898
+/*
119899
+** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
119900
+**
119901
+** The new entry might overwrite an existing entry, or it might be
119902
+** appended, or it might be discarded. Do whatever is the right thing
119903
+** so that pSet keeps the N_OR_COST best entries seen so far.
119904
+*/
119905
+static int whereOrInsert(
119906
+ WhereOrSet *pSet, /* The WhereOrSet to be updated */
119907
+ Bitmask prereq, /* Prerequisites of the new entry */
119908
+ LogEst rRun, /* Run-cost of the new entry */
119909
+ LogEst nOut /* Number of outputs for the new entry */
119910
+){
119911
+ u16 i;
119912
+ WhereOrCost *p;
119913
+ for(i=pSet->n, p=pSet->a; i>0; i--, p++){
119914
+ if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
119915
+ goto whereOrInsert_done;
119916
+ }
119917
+ if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
119918
+ return 0;
119919
+ }
119920
+ }
119921
+ if( pSet->n<N_OR_COST ){
119922
+ p = &pSet->a[pSet->n++];
119923
+ p->nOut = nOut;
119924
+ }else{
119925
+ p = pSet->a;
119926
+ for(i=1; i<pSet->n; i++){
119927
+ if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
119928
+ }
119929
+ if( p->rRun<=rRun ) return 0;
119930
+ }
119931
+whereOrInsert_done:
119932
+ p->prereq = prereq;
119933
+ p->rRun = rRun;
119934
+ if( p->nOut>nOut ) p->nOut = nOut;
119935
+ return 1;
119936
+}
119937
+
119938
+/*
119939
+** Return the bitmask for the given cursor number. Return 0 if
119940
+** iCursor is not in the set.
119941
+*/
119942
+SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
119943
+ int i;
119944
+ assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
119945
+ for(i=0; i<pMaskSet->n; i++){
119946
+ if( pMaskSet->ix[i]==iCursor ){
119947
+ return MASKBIT(i);
119948
+ }
119949
+ }
119950
+ return 0;
119951
+}
119952
+
119953
+/*
119954
+** Create a new mask for cursor iCursor.
119955
+**
119956
+** There is one cursor per table in the FROM clause. The number of
119957
+** tables in the FROM clause is limited by a test early in the
119958
+** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
119959
+** array will never overflow.
119960
+*/
119961
+static void createMask(WhereMaskSet *pMaskSet, int iCursor){
119962
+ assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
119963
+ pMaskSet->ix[pMaskSet->n++] = iCursor;
119964
+}
119965
+
119966
+/*
119967
+** Advance to the next WhereTerm that matches according to the criteria
119968
+** established when the pScan object was initialized by whereScanInit().
119969
+** Return NULL if there are no more matching WhereTerms.
119970
+*/
119971
+static WhereTerm *whereScanNext(WhereScan *pScan){
119972
+ int iCur; /* The cursor on the LHS of the term */
119973
+ int iColumn; /* The column on the LHS of the term. -1 for IPK */
119974
+ Expr *pX; /* An expression being tested */
119975
+ WhereClause *pWC; /* Shorthand for pScan->pWC */
119976
+ WhereTerm *pTerm; /* The term being tested */
119977
+ int k = pScan->k; /* Where to start scanning */
119978
+
119979
+ while( pScan->iEquiv<=pScan->nEquiv ){
119980
+ iCur = pScan->aEquiv[pScan->iEquiv-2];
119981
+ iColumn = pScan->aEquiv[pScan->iEquiv-1];
119982
+ while( (pWC = pScan->pWC)!=0 ){
119983
+ for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
119984
+ if( pTerm->leftCursor==iCur
119985
+ && pTerm->u.leftColumn==iColumn
119986
+ && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
119987
+ ){
119988
+ if( (pTerm->eOperator & WO_EQUIV)!=0
119989
+ && pScan->nEquiv<ArraySize(pScan->aEquiv)
119990
+ ){
119991
+ int j;
119992
+ pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
119993
+ assert( pX->op==TK_COLUMN );
119994
+ for(j=0; j<pScan->nEquiv; j+=2){
119995
+ if( pScan->aEquiv[j]==pX->iTable
119996
+ && pScan->aEquiv[j+1]==pX->iColumn ){
119997
+ break;
119998
+ }
119999
+ }
120000
+ if( j==pScan->nEquiv ){
120001
+ pScan->aEquiv[j] = pX->iTable;
120002
+ pScan->aEquiv[j+1] = pX->iColumn;
120003
+ pScan->nEquiv += 2;
120004
+ }
120005
+ }
120006
+ if( (pTerm->eOperator & pScan->opMask)!=0 ){
120007
+ /* Verify the affinity and collating sequence match */
120008
+ if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
120009
+ CollSeq *pColl;
120010
+ Parse *pParse = pWC->pWInfo->pParse;
120011
+ pX = pTerm->pExpr;
120012
+ if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
120013
+ continue;
120014
+ }
120015
+ assert(pX->pLeft);
120016
+ pColl = sqlite3BinaryCompareCollSeq(pParse,
120017
+ pX->pLeft, pX->pRight);
120018
+ if( pColl==0 ) pColl = pParse->db->pDfltColl;
120019
+ if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
120020
+ continue;
120021
+ }
120022
+ }
120023
+ if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
120024
+ && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
120025
+ && pX->iTable==pScan->aEquiv[0]
120026
+ && pX->iColumn==pScan->aEquiv[1]
120027
+ ){
120028
+ testcase( pTerm->eOperator & WO_IS );
120029
+ continue;
120030
+ }
120031
+ pScan->k = k+1;
120032
+ return pTerm;
120033
+ }
120034
+ }
120035
+ }
120036
+ pScan->pWC = pScan->pWC->pOuter;
120037
+ k = 0;
120038
+ }
120039
+ pScan->pWC = pScan->pOrigWC;
120040
+ k = 0;
120041
+ pScan->iEquiv += 2;
120042
+ }
120043
+ return 0;
120044
+}
120045
+
120046
+/*
120047
+** Initialize a WHERE clause scanner object. Return a pointer to the
120048
+** first match. Return NULL if there are no matches.
120049
+**
120050
+** The scanner will be searching the WHERE clause pWC. It will look
120051
+** for terms of the form "X <op> <expr>" where X is column iColumn of table
120052
+** iCur. The <op> must be one of the operators described by opMask.
120053
+**
120054
+** If the search is for X and the WHERE clause contains terms of the
120055
+** form X=Y then this routine might also return terms of the form
120056
+** "Y <op> <expr>". The number of levels of transitivity is limited,
120057
+** but is enough to handle most commonly occurring SQL statements.
120058
+**
120059
+** If X is not the INTEGER PRIMARY KEY then X must be compatible with
120060
+** index pIdx.
120061
+*/
120062
+static WhereTerm *whereScanInit(
120063
+ WhereScan *pScan, /* The WhereScan object being initialized */
120064
+ WhereClause *pWC, /* The WHERE clause to be scanned */
120065
+ int iCur, /* Cursor to scan for */
120066
+ int iColumn, /* Column to scan for */
120067
+ u32 opMask, /* Operator(s) to scan for */
120068
+ Index *pIdx /* Must be compatible with this index */
120069
+){
120070
+ int j;
120071
+
120072
+ /* memset(pScan, 0, sizeof(*pScan)); */
120073
+ pScan->pOrigWC = pWC;
120074
+ pScan->pWC = pWC;
120075
+ if( pIdx && iColumn>=0 ){
120076
+ pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
120077
+ for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
120078
+ if( NEVER(j>pIdx->nColumn) ) return 0;
120079
+ }
120080
+ pScan->zCollName = pIdx->azColl[j];
120081
+ }else{
120082
+ pScan->idxaff = 0;
120083
+ pScan->zCollName = 0;
120084
+ }
120085
+ pScan->opMask = opMask;
120086
+ pScan->k = 0;
120087
+ pScan->aEquiv[0] = iCur;
120088
+ pScan->aEquiv[1] = iColumn;
120089
+ pScan->nEquiv = 2;
120090
+ pScan->iEquiv = 2;
120091
+ return whereScanNext(pScan);
120092
+}
120093
+
120094
+/*
120095
+** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
120096
+** where X is a reference to the iColumn of table iCur and <op> is one of
120097
+** the WO_xx operator codes specified by the op parameter.
120098
+** Return a pointer to the term. Return 0 if not found.
120099
+**
120100
+** The term returned might by Y=<expr> if there is another constraint in
120101
+** the WHERE clause that specifies that X=Y. Any such constraints will be
120102
+** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
120103
+** aEquiv[] array holds X and all its equivalents, with each SQL variable
120104
+** taking up two slots in aEquiv[]. The first slot is for the cursor number
120105
+** and the second is for the column number. There are 22 slots in aEquiv[]
120106
+** so that means we can look for X plus up to 10 other equivalent values.
120107
+** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
120108
+** and ... and A9=A10 and A10=<expr>.
120109
+**
120110
+** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
120111
+** then try for the one with no dependencies on <expr> - in other words where
120112
+** <expr> is a constant expression of some kind. Only return entries of
120113
+** the form "X <op> Y" where Y is a column in another table if no terms of
120114
+** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
120115
+** exist, try to return a term that does not use WO_EQUIV.
120116
+*/
120117
+SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
120118
+ WhereClause *pWC, /* The WHERE clause to be searched */
120119
+ int iCur, /* Cursor number of LHS */
120120
+ int iColumn, /* Column number of LHS */
120121
+ Bitmask notReady, /* RHS must not overlap with this mask */
120122
+ u32 op, /* Mask of WO_xx values describing operator */
120123
+ Index *pIdx /* Must be compatible with this index, if not NULL */
120124
+){
120125
+ WhereTerm *pResult = 0;
120126
+ WhereTerm *p;
120127
+ WhereScan scan;
120128
+
120129
+ p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
120130
+ op &= WO_EQ|WO_IS;
120131
+ while( p ){
120132
+ if( (p->prereqRight & notReady)==0 ){
120133
+ if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
120134
+ testcase( p->eOperator & WO_IS );
120135
+ return p;
120136
+ }
120137
+ if( pResult==0 ) pResult = p;
120138
+ }
120139
+ p = whereScanNext(&scan);
120140
+ }
120141
+ return pResult;
120142
+}
117846120143
117847120144
/*
117848120145
** This function searches pList for an entry that matches the iCol-th column
117849120146
** of index pIdx.
117850120147
**
@@ -117879,12 +120176,12 @@
117879120176
117880120177
/*
117881120178
** Return true if the DISTINCT expression-list passed as the third argument
117882120179
** is redundant.
117883120180
**
117884
-** A DISTINCT list is redundant if the database contains some subset of
117885
-** columns that are unique and non-null.
120181
+** A DISTINCT list is redundant if any subset of the columns in the
120182
+** DISTINCT list are collectively unique and individually non-null.
117886120183
*/
117887120184
static int isDistinctRedundant(
117888120185
Parse *pParse, /* Parsing context */
117889120186
SrcList *pTabList, /* The FROM clause */
117890120187
WhereClause *pWC, /* The WHERE clause */
@@ -117926,11 +120223,11 @@
117926120223
*/
117927120224
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117928120225
if( !IsUniqueIndex(pIdx) ) continue;
117929120226
for(i=0; i<pIdx->nKeyCol; i++){
117930120227
i16 iCol = pIdx->aiColumn[i];
117931
- if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
120228
+ if( 0==sqlite3WhereFindTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
117932120229
int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
117933120230
if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
117934120231
break;
117935120232
}
117936120233
}
@@ -118257,10 +120554,11 @@
118257120554
** by passing the pointer returned by this function to sqlite3_free().
118258120555
*/
118259120556
static sqlite3_index_info *allocateIndexInfo(
118260120557
Parse *pParse,
118261120558
WhereClause *pWC,
120559
+ Bitmask mUnusable, /* Ignore terms with these prereqs */
118262120560
struct SrcList_item *pSrc,
118263120561
ExprList *pOrderBy
118264120562
){
118265120563
int i, j;
118266120564
int nTerm;
@@ -118273,10 +120571,11 @@
118273120571
118274120572
/* Count the number of possible WHERE clause constraints referring
118275120573
** to this virtual table */
118276120574
for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118277120575
if( pTerm->leftCursor != pSrc->iCursor ) continue;
120576
+ if( pTerm->prereqRight & mUnusable ) continue;
118278120577
assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118279120578
testcase( pTerm->eOperator & WO_IN );
118280120579
testcase( pTerm->eOperator & WO_ISNULL );
118281120580
testcase( pTerm->eOperator & WO_IS );
118282120581
testcase( pTerm->eOperator & WO_ALL );
@@ -118327,10 +120626,11 @@
118327120626
pUsage;
118328120627
118329120628
for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118330120629
u8 op;
118331120630
if( pTerm->leftCursor != pSrc->iCursor ) continue;
120631
+ if( pTerm->prereqRight & mUnusable ) continue;
118332120632
assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118333120633
testcase( pTerm->eOperator & WO_IN );
118334120634
testcase( pTerm->eOperator & WO_IS );
118335120635
testcase( pTerm->eOperator & WO_ISNULL );
118336120636
testcase( pTerm->eOperator & WO_ALL );
@@ -119048,1491 +121348,10 @@
119048121348
assert( pBuilder->nRecValid==nRecValid );
119049121349
return rc;
119050121350
}
119051121351
#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
119052121352
119053
-/*
119054
-** Disable a term in the WHERE clause. Except, do not disable the term
119055
-** if it controls a LEFT OUTER JOIN and it did not originate in the ON
119056
-** or USING clause of that join.
119057
-**
119058
-** Consider the term t2.z='ok' in the following queries:
119059
-**
119060
-** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
119061
-** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
119062
-** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
119063
-**
119064
-** The t2.z='ok' is disabled in the in (2) because it originates
119065
-** in the ON clause. The term is disabled in (3) because it is not part
119066
-** of a LEFT OUTER JOIN. In (1), the term is not disabled.
119067
-**
119068
-** Disabling a term causes that term to not be tested in the inner loop
119069
-** of the join. Disabling is an optimization. When terms are satisfied
119070
-** by indices, we disable them to prevent redundant tests in the inner
119071
-** loop. We would get the correct results if nothing were ever disabled,
119072
-** but joins might run a little slower. The trick is to disable as much
119073
-** as we can without disabling too much. If we disabled in (1), we'd get
119074
-** the wrong answer. See ticket #813.
119075
-**
119076
-** If all the children of a term are disabled, then that term is also
119077
-** automatically disabled. In this way, terms get disabled if derived
119078
-** virtual terms are tested first. For example:
119079
-**
119080
-** x GLOB 'abc*' AND x>='abc' AND x<'acd'
119081
-** \___________/ \______/ \_____/
119082
-** parent child1 child2
119083
-**
119084
-** Only the parent term was in the original WHERE clause. The child1
119085
-** and child2 terms were added by the LIKE optimization. If both of
119086
-** the virtual child terms are valid, then testing of the parent can be
119087
-** skipped.
119088
-**
119089
-** Usually the parent term is marked as TERM_CODED. But if the parent
119090
-** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
119091
-** The TERM_LIKECOND marking indicates that the term should be coded inside
119092
-** a conditional such that is only evaluated on the second pass of a
119093
-** LIKE-optimization loop, when scanning BLOBs instead of strings.
119094
-*/
119095
-static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
119096
- int nLoop = 0;
119097
- while( pTerm
119098
- && (pTerm->wtFlags & TERM_CODED)==0
119099
- && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
119100
- && (pLevel->notReady & pTerm->prereqAll)==0
119101
- ){
119102
- if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
119103
- pTerm->wtFlags |= TERM_LIKECOND;
119104
- }else{
119105
- pTerm->wtFlags |= TERM_CODED;
119106
- }
119107
- if( pTerm->iParent<0 ) break;
119108
- pTerm = &pTerm->pWC->a[pTerm->iParent];
119109
- pTerm->nChild--;
119110
- if( pTerm->nChild!=0 ) break;
119111
- nLoop++;
119112
- }
119113
-}
119114
-
119115
-/*
119116
-** Code an OP_Affinity opcode to apply the column affinity string zAff
119117
-** to the n registers starting at base.
119118
-**
119119
-** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
119120
-** beginning and end of zAff are ignored. If all entries in zAff are
119121
-** SQLITE_AFF_NONE, then no code gets generated.
119122
-**
119123
-** This routine makes its own copy of zAff so that the caller is free
119124
-** to modify zAff after this routine returns.
119125
-*/
119126
-static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
119127
- Vdbe *v = pParse->pVdbe;
119128
- if( zAff==0 ){
119129
- assert( pParse->db->mallocFailed );
119130
- return;
119131
- }
119132
- assert( v!=0 );
119133
-
119134
- /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
119135
- ** and end of the affinity string.
119136
- */
119137
- while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
119138
- n--;
119139
- base++;
119140
- zAff++;
119141
- }
119142
- while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
119143
- n--;
119144
- }
119145
-
119146
- /* Code the OP_Affinity opcode if there is anything left to do. */
119147
- if( n>0 ){
119148
- sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
119149
- sqlite3VdbeChangeP4(v, -1, zAff, n);
119150
- sqlite3ExprCacheAffinityChange(pParse, base, n);
119151
- }
119152
-}
119153
-
119154
-
119155
-/*
119156
-** Generate code for a single equality term of the WHERE clause. An equality
119157
-** term can be either X=expr or X IN (...). pTerm is the term to be
119158
-** coded.
119159
-**
119160
-** The current value for the constraint is left in register iReg.
119161
-**
119162
-** For a constraint of the form X=expr, the expression is evaluated and its
119163
-** result is left on the stack. For constraints of the form X IN (...)
119164
-** this routine sets up a loop that will iterate over all values of X.
119165
-*/
119166
-static int codeEqualityTerm(
119167
- Parse *pParse, /* The parsing context */
119168
- WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
119169
- WhereLevel *pLevel, /* The level of the FROM clause we are working on */
119170
- int iEq, /* Index of the equality term within this level */
119171
- int bRev, /* True for reverse-order IN operations */
119172
- int iTarget /* Attempt to leave results in this register */
119173
-){
119174
- Expr *pX = pTerm->pExpr;
119175
- Vdbe *v = pParse->pVdbe;
119176
- int iReg; /* Register holding results */
119177
-
119178
- assert( iTarget>0 );
119179
- if( pX->op==TK_EQ || pX->op==TK_IS ){
119180
- iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
119181
- }else if( pX->op==TK_ISNULL ){
119182
- iReg = iTarget;
119183
- sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
119184
-#ifndef SQLITE_OMIT_SUBQUERY
119185
- }else{
119186
- int eType;
119187
- int iTab;
119188
- struct InLoop *pIn;
119189
- WhereLoop *pLoop = pLevel->pWLoop;
119190
-
119191
- if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
119192
- && pLoop->u.btree.pIndex!=0
119193
- && pLoop->u.btree.pIndex->aSortOrder[iEq]
119194
- ){
119195
- testcase( iEq==0 );
119196
- testcase( bRev );
119197
- bRev = !bRev;
119198
- }
119199
- assert( pX->op==TK_IN );
119200
- iReg = iTarget;
119201
- eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
119202
- if( eType==IN_INDEX_INDEX_DESC ){
119203
- testcase( bRev );
119204
- bRev = !bRev;
119205
- }
119206
- iTab = pX->iTable;
119207
- sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
119208
- VdbeCoverageIf(v, bRev);
119209
- VdbeCoverageIf(v, !bRev);
119210
- assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
119211
- pLoop->wsFlags |= WHERE_IN_ABLE;
119212
- if( pLevel->u.in.nIn==0 ){
119213
- pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
119214
- }
119215
- pLevel->u.in.nIn++;
119216
- pLevel->u.in.aInLoop =
119217
- sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
119218
- sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
119219
- pIn = pLevel->u.in.aInLoop;
119220
- if( pIn ){
119221
- pIn += pLevel->u.in.nIn - 1;
119222
- pIn->iCur = iTab;
119223
- if( eType==IN_INDEX_ROWID ){
119224
- pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
119225
- }else{
119226
- pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
119227
- }
119228
- pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
119229
- sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
119230
- }else{
119231
- pLevel->u.in.nIn = 0;
119232
- }
119233
-#endif
119234
- }
119235
- disableTerm(pLevel, pTerm);
119236
- return iReg;
119237
-}
119238
-
119239
-/*
119240
-** Generate code that will evaluate all == and IN constraints for an
119241
-** index scan.
119242
-**
119243
-** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
119244
-** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
119245
-** The index has as many as three equality constraints, but in this
119246
-** example, the third "c" value is an inequality. So only two
119247
-** constraints are coded. This routine will generate code to evaluate
119248
-** a==5 and b IN (1,2,3). The current values for a and b will be stored
119249
-** in consecutive registers and the index of the first register is returned.
119250
-**
119251
-** In the example above nEq==2. But this subroutine works for any value
119252
-** of nEq including 0. If nEq==0, this routine is nearly a no-op.
119253
-** The only thing it does is allocate the pLevel->iMem memory cell and
119254
-** compute the affinity string.
119255
-**
119256
-** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
119257
-** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
119258
-** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
119259
-** occurs after the nEq quality constraints.
119260
-**
119261
-** This routine allocates a range of nEq+nExtraReg memory cells and returns
119262
-** the index of the first memory cell in that range. The code that
119263
-** calls this routine will use that memory range to store keys for
119264
-** start and termination conditions of the loop.
119265
-** key value of the loop. If one or more IN operators appear, then
119266
-** this routine allocates an additional nEq memory cells for internal
119267
-** use.
119268
-**
119269
-** Before returning, *pzAff is set to point to a buffer containing a
119270
-** copy of the column affinity string of the index allocated using
119271
-** sqlite3DbMalloc(). Except, entries in the copy of the string associated
119272
-** with equality constraints that use NONE affinity are set to
119273
-** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
119274
-**
119275
-** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
119276
-** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
119277
-**
119278
-** In the example above, the index on t1(a) has TEXT affinity. But since
119279
-** the right hand side of the equality constraint (t2.b) has NONE affinity,
119280
-** no conversion should be attempted before using a t2.b value as part of
119281
-** a key to search the index. Hence the first byte in the returned affinity
119282
-** string in this example would be set to SQLITE_AFF_NONE.
119283
-*/
119284
-static int codeAllEqualityTerms(
119285
- Parse *pParse, /* Parsing context */
119286
- WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
119287
- int bRev, /* Reverse the order of IN operators */
119288
- int nExtraReg, /* Number of extra registers to allocate */
119289
- char **pzAff /* OUT: Set to point to affinity string */
119290
-){
119291
- u16 nEq; /* The number of == or IN constraints to code */
119292
- u16 nSkip; /* Number of left-most columns to skip */
119293
- Vdbe *v = pParse->pVdbe; /* The vm under construction */
119294
- Index *pIdx; /* The index being used for this loop */
119295
- WhereTerm *pTerm; /* A single constraint term */
119296
- WhereLoop *pLoop; /* The WhereLoop object */
119297
- int j; /* Loop counter */
119298
- int regBase; /* Base register */
119299
- int nReg; /* Number of registers to allocate */
119300
- char *zAff; /* Affinity string to return */
119301
-
119302
- /* This module is only called on query plans that use an index. */
119303
- pLoop = pLevel->pWLoop;
119304
- assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
119305
- nEq = pLoop->u.btree.nEq;
119306
- nSkip = pLoop->nSkip;
119307
- pIdx = pLoop->u.btree.pIndex;
119308
- assert( pIdx!=0 );
119309
-
119310
- /* Figure out how many memory cells we will need then allocate them.
119311
- */
119312
- regBase = pParse->nMem + 1;
119313
- nReg = pLoop->u.btree.nEq + nExtraReg;
119314
- pParse->nMem += nReg;
119315
-
119316
- zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
119317
- if( !zAff ){
119318
- pParse->db->mallocFailed = 1;
119319
- }
119320
-
119321
- if( nSkip ){
119322
- int iIdxCur = pLevel->iIdxCur;
119323
- sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
119324
- VdbeCoverageIf(v, bRev==0);
119325
- VdbeCoverageIf(v, bRev!=0);
119326
- VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
119327
- j = sqlite3VdbeAddOp0(v, OP_Goto);
119328
- pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
119329
- iIdxCur, 0, regBase, nSkip);
119330
- VdbeCoverageIf(v, bRev==0);
119331
- VdbeCoverageIf(v, bRev!=0);
119332
- sqlite3VdbeJumpHere(v, j);
119333
- for(j=0; j<nSkip; j++){
119334
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
119335
- assert( pIdx->aiColumn[j]>=0 );
119336
- VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
119337
- }
119338
- }
119339
-
119340
- /* Evaluate the equality constraints
119341
- */
119342
- assert( zAff==0 || (int)strlen(zAff)>=nEq );
119343
- for(j=nSkip; j<nEq; j++){
119344
- int r1;
119345
- pTerm = pLoop->aLTerm[j];
119346
- assert( pTerm!=0 );
119347
- /* The following testcase is true for indices with redundant columns.
119348
- ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
119349
- testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
119350
- testcase( pTerm->wtFlags & TERM_VIRTUAL );
119351
- r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
119352
- if( r1!=regBase+j ){
119353
- if( nReg==1 ){
119354
- sqlite3ReleaseTempReg(pParse, regBase);
119355
- regBase = r1;
119356
- }else{
119357
- sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
119358
- }
119359
- }
119360
- testcase( pTerm->eOperator & WO_ISNULL );
119361
- testcase( pTerm->eOperator & WO_IN );
119362
- if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
119363
- Expr *pRight = pTerm->pExpr->pRight;
119364
- if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
119365
- sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
119366
- VdbeCoverage(v);
119367
- }
119368
- if( zAff ){
119369
- if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
119370
- zAff[j] = SQLITE_AFF_NONE;
119371
- }
119372
- if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
119373
- zAff[j] = SQLITE_AFF_NONE;
119374
- }
119375
- }
119376
- }
119377
- }
119378
- *pzAff = zAff;
119379
- return regBase;
119380
-}
119381
-
119382
-#ifndef SQLITE_OMIT_EXPLAIN
119383
-/*
119384
-** This routine is a helper for explainIndexRange() below
119385
-**
119386
-** pStr holds the text of an expression that we are building up one term
119387
-** at a time. This routine adds a new term to the end of the expression.
119388
-** Terms are separated by AND so add the "AND" text for second and subsequent
119389
-** terms only.
119390
-*/
119391
-static void explainAppendTerm(
119392
- StrAccum *pStr, /* The text expression being built */
119393
- int iTerm, /* Index of this term. First is zero */
119394
- const char *zColumn, /* Name of the column */
119395
- const char *zOp /* Name of the operator */
119396
-){
119397
- if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
119398
- sqlite3StrAccumAppendAll(pStr, zColumn);
119399
- sqlite3StrAccumAppend(pStr, zOp, 1);
119400
- sqlite3StrAccumAppend(pStr, "?", 1);
119401
-}
119402
-
119403
-/*
119404
-** Argument pLevel describes a strategy for scanning table pTab. This
119405
-** function appends text to pStr that describes the subset of table
119406
-** rows scanned by the strategy in the form of an SQL expression.
119407
-**
119408
-** For example, if the query:
119409
-**
119410
-** SELECT * FROM t1 WHERE a=1 AND b>2;
119411
-**
119412
-** is run and there is an index on (a, b), then this function returns a
119413
-** string similar to:
119414
-**
119415
-** "a=? AND b>?"
119416
-*/
119417
-static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
119418
- Index *pIndex = pLoop->u.btree.pIndex;
119419
- u16 nEq = pLoop->u.btree.nEq;
119420
- u16 nSkip = pLoop->nSkip;
119421
- int i, j;
119422
- Column *aCol = pTab->aCol;
119423
- i16 *aiColumn = pIndex->aiColumn;
119424
-
119425
- if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
119426
- sqlite3StrAccumAppend(pStr, " (", 2);
119427
- for(i=0; i<nEq; i++){
119428
- char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
119429
- if( i>=nSkip ){
119430
- explainAppendTerm(pStr, i, z, "=");
119431
- }else{
119432
- if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
119433
- sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
119434
- }
119435
- }
119436
-
119437
- j = i;
119438
- if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
119439
- char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
119440
- explainAppendTerm(pStr, i++, z, ">");
119441
- }
119442
- if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
119443
- char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
119444
- explainAppendTerm(pStr, i, z, "<");
119445
- }
119446
- sqlite3StrAccumAppend(pStr, ")", 1);
119447
-}
119448
-
119449
-/*
119450
-** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
119451
-** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
119452
-** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
119453
-** is added to the output to describe the table scan strategy in pLevel.
119454
-**
119455
-** If an OP_Explain opcode is added to the VM, its address is returned.
119456
-** Otherwise, if no OP_Explain is coded, zero is returned.
119457
-*/
119458
-static int explainOneScan(
119459
- Parse *pParse, /* Parse context */
119460
- SrcList *pTabList, /* Table list this loop refers to */
119461
- WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
119462
- int iLevel, /* Value for "level" column of output */
119463
- int iFrom, /* Value for "from" column of output */
119464
- u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
119465
-){
119466
- int ret = 0;
119467
-#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
119468
- if( pParse->explain==2 )
119469
-#endif
119470
- {
119471
- struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
119472
- Vdbe *v = pParse->pVdbe; /* VM being constructed */
119473
- sqlite3 *db = pParse->db; /* Database handle */
119474
- int iId = pParse->iSelectId; /* Select id (left-most output column) */
119475
- int isSearch; /* True for a SEARCH. False for SCAN. */
119476
- WhereLoop *pLoop; /* The controlling WhereLoop object */
119477
- u32 flags; /* Flags that describe this loop */
119478
- char *zMsg; /* Text to add to EQP output */
119479
- StrAccum str; /* EQP output string */
119480
- char zBuf[100]; /* Initial space for EQP output string */
119481
-
119482
- pLoop = pLevel->pWLoop;
119483
- flags = pLoop->wsFlags;
119484
- if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
119485
-
119486
- isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
119487
- || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
119488
- || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
119489
-
119490
- sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
119491
- sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
119492
- if( pItem->pSelect ){
119493
- sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
119494
- }else{
119495
- sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
119496
- }
119497
-
119498
- if( pItem->zAlias ){
119499
- sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
119500
- }
119501
- if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
119502
- const char *zFmt = 0;
119503
- Index *pIdx;
119504
-
119505
- assert( pLoop->u.btree.pIndex!=0 );
119506
- pIdx = pLoop->u.btree.pIndex;
119507
- assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
119508
- if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
119509
- if( isSearch ){
119510
- zFmt = "PRIMARY KEY";
119511
- }
119512
- }else if( flags & WHERE_PARTIALIDX ){
119513
- zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
119514
- }else if( flags & WHERE_AUTO_INDEX ){
119515
- zFmt = "AUTOMATIC COVERING INDEX";
119516
- }else if( flags & WHERE_IDX_ONLY ){
119517
- zFmt = "COVERING INDEX %s";
119518
- }else{
119519
- zFmt = "INDEX %s";
119520
- }
119521
- if( zFmt ){
119522
- sqlite3StrAccumAppend(&str, " USING ", 7);
119523
- sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
119524
- explainIndexRange(&str, pLoop, pItem->pTab);
119525
- }
119526
- }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
119527
- const char *zRange;
119528
- if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
119529
- zRange = "(rowid=?)";
119530
- }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
119531
- zRange = "(rowid>? AND rowid<?)";
119532
- }else if( flags&WHERE_BTM_LIMIT ){
119533
- zRange = "(rowid>?)";
119534
- }else{
119535
- assert( flags&WHERE_TOP_LIMIT);
119536
- zRange = "(rowid<?)";
119537
- }
119538
- sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
119539
- sqlite3StrAccumAppendAll(&str, zRange);
119540
- }
119541
-#ifndef SQLITE_OMIT_VIRTUALTABLE
119542
- else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
119543
- sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
119544
- pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
119545
- }
119546
-#endif
119547
-#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
119548
- if( pLoop->nOut>=10 ){
119549
- sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
119550
- }else{
119551
- sqlite3StrAccumAppend(&str, " (~1 row)", 9);
119552
- }
119553
-#endif
119554
- zMsg = sqlite3StrAccumFinish(&str);
119555
- ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
119556
- }
119557
- return ret;
119558
-}
119559
-#else
119560
-# define explainOneScan(u,v,w,x,y,z) 0
119561
-#endif /* SQLITE_OMIT_EXPLAIN */
119562
-
119563
-#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
119564
-/*
119565
-** Configure the VM passed as the first argument with an
119566
-** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
119567
-** implement level pLvl. Argument pSrclist is a pointer to the FROM
119568
-** clause that the scan reads data from.
119569
-**
119570
-** If argument addrExplain is not 0, it must be the address of an
119571
-** OP_Explain instruction that describes the same loop.
119572
-*/
119573
-static void addScanStatus(
119574
- Vdbe *v, /* Vdbe to add scanstatus entry to */
119575
- SrcList *pSrclist, /* FROM clause pLvl reads data from */
119576
- WhereLevel *pLvl, /* Level to add scanstatus() entry for */
119577
- int addrExplain /* Address of OP_Explain (or 0) */
119578
-){
119579
- const char *zObj = 0;
119580
- WhereLoop *pLoop = pLvl->pWLoop;
119581
- if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
119582
- zObj = pLoop->u.btree.pIndex->zName;
119583
- }else{
119584
- zObj = pSrclist->a[pLvl->iFrom].zName;
119585
- }
119586
- sqlite3VdbeScanStatus(
119587
- v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
119588
- );
119589
-}
119590
-#else
119591
-# define addScanStatus(a, b, c, d) ((void)d)
119592
-#endif
119593
-
119594
-/*
119595
-** If the most recently coded instruction is a constant range contraint
119596
-** that originated from the LIKE optimization, then change the P3 to be
119597
-** pLoop->iLikeRepCntr and set P5.
119598
-**
119599
-** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
119600
-** expression: "x>='ABC' AND x<'abd'". But this requires that the range
119601
-** scan loop run twice, once for strings and a second time for BLOBs.
119602
-** The OP_String opcodes on the second pass convert the upper and lower
119603
-** bound string contants to blobs. This routine makes the necessary changes
119604
-** to the OP_String opcodes for that to happen.
119605
-*/
119606
-static void whereLikeOptimizationStringFixup(
119607
- Vdbe *v, /* prepared statement under construction */
119608
- WhereLevel *pLevel, /* The loop that contains the LIKE operator */
119609
- WhereTerm *pTerm /* The upper or lower bound just coded */
119610
-){
119611
- if( pTerm->wtFlags & TERM_LIKEOPT ){
119612
- VdbeOp *pOp;
119613
- assert( pLevel->iLikeRepCntr>0 );
119614
- pOp = sqlite3VdbeGetOp(v, -1);
119615
- assert( pOp!=0 );
119616
- assert( pOp->opcode==OP_String8
119617
- || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
119618
- pOp->p3 = pLevel->iLikeRepCntr;
119619
- pOp->p5 = 1;
119620
- }
119621
-}
119622
-
119623
-/*
119624
-** Generate code for the start of the iLevel-th loop in the WHERE clause
119625
-** implementation described by pWInfo.
119626
-*/
119627
-static Bitmask codeOneLoopStart(
119628
- WhereInfo *pWInfo, /* Complete information about the WHERE clause */
119629
- int iLevel, /* Which level of pWInfo->a[] should be coded */
119630
- Bitmask notReady /* Which tables are currently available */
119631
-){
119632
- int j, k; /* Loop counters */
119633
- int iCur; /* The VDBE cursor for the table */
119634
- int addrNxt; /* Where to jump to continue with the next IN case */
119635
- int omitTable; /* True if we use the index only */
119636
- int bRev; /* True if we need to scan in reverse order */
119637
- WhereLevel *pLevel; /* The where level to be coded */
119638
- WhereLoop *pLoop; /* The WhereLoop object being coded */
119639
- WhereClause *pWC; /* Decomposition of the entire WHERE clause */
119640
- WhereTerm *pTerm; /* A WHERE clause term */
119641
- Parse *pParse; /* Parsing context */
119642
- sqlite3 *db; /* Database connection */
119643
- Vdbe *v; /* The prepared stmt under constructions */
119644
- struct SrcList_item *pTabItem; /* FROM clause term being coded */
119645
- int addrBrk; /* Jump here to break out of the loop */
119646
- int addrCont; /* Jump here to continue with next cycle */
119647
- int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
119648
- int iReleaseReg = 0; /* Temp register to free before returning */
119649
-
119650
- pParse = pWInfo->pParse;
119651
- v = pParse->pVdbe;
119652
- pWC = &pWInfo->sWC;
119653
- db = pParse->db;
119654
- pLevel = &pWInfo->a[iLevel];
119655
- pLoop = pLevel->pWLoop;
119656
- pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
119657
- iCur = pTabItem->iCursor;
119658
- pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
119659
- bRev = (pWInfo->revMask>>iLevel)&1;
119660
- omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
119661
- && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
119662
- VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
119663
-
119664
- /* Create labels for the "break" and "continue" instructions
119665
- ** for the current loop. Jump to addrBrk to break out of a loop.
119666
- ** Jump to cont to go immediately to the next iteration of the
119667
- ** loop.
119668
- **
119669
- ** When there is an IN operator, we also have a "addrNxt" label that
119670
- ** means to continue with the next IN value combination. When
119671
- ** there are no IN operators in the constraints, the "addrNxt" label
119672
- ** is the same as "addrBrk".
119673
- */
119674
- addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
119675
- addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
119676
-
119677
- /* If this is the right table of a LEFT OUTER JOIN, allocate and
119678
- ** initialize a memory cell that records if this table matches any
119679
- ** row of the left table of the join.
119680
- */
119681
- if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
119682
- pLevel->iLeftJoin = ++pParse->nMem;
119683
- sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
119684
- VdbeComment((v, "init LEFT JOIN no-match flag"));
119685
- }
119686
-
119687
- /* Special case of a FROM clause subquery implemented as a co-routine */
119688
- if( pTabItem->viaCoroutine ){
119689
- int regYield = pTabItem->regReturn;
119690
- sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
119691
- pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
119692
- VdbeCoverage(v);
119693
- VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
119694
- pLevel->op = OP_Goto;
119695
- }else
119696
-
119697
-#ifndef SQLITE_OMIT_VIRTUALTABLE
119698
- if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
119699
- /* Case 1: The table is a virtual-table. Use the VFilter and VNext
119700
- ** to access the data.
119701
- */
119702
- int iReg; /* P3 Value for OP_VFilter */
119703
- int addrNotFound;
119704
- int nConstraint = pLoop->nLTerm;
119705
-
119706
- sqlite3ExprCachePush(pParse);
119707
- iReg = sqlite3GetTempRange(pParse, nConstraint+2);
119708
- addrNotFound = pLevel->addrBrk;
119709
- for(j=0; j<nConstraint; j++){
119710
- int iTarget = iReg+j+2;
119711
- pTerm = pLoop->aLTerm[j];
119712
- if( pTerm==0 ) continue;
119713
- if( pTerm->eOperator & WO_IN ){
119714
- codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
119715
- addrNotFound = pLevel->addrNxt;
119716
- }else{
119717
- sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
119718
- }
119719
- }
119720
- sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
119721
- sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
119722
- sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
119723
- pLoop->u.vtab.idxStr,
119724
- pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
119725
- VdbeCoverage(v);
119726
- pLoop->u.vtab.needFree = 0;
119727
- for(j=0; j<nConstraint && j<16; j++){
119728
- if( (pLoop->u.vtab.omitMask>>j)&1 ){
119729
- disableTerm(pLevel, pLoop->aLTerm[j]);
119730
- }
119731
- }
119732
- pLevel->op = OP_VNext;
119733
- pLevel->p1 = iCur;
119734
- pLevel->p2 = sqlite3VdbeCurrentAddr(v);
119735
- sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
119736
- sqlite3ExprCachePop(pParse);
119737
- }else
119738
-#endif /* SQLITE_OMIT_VIRTUALTABLE */
119739
-
119740
- if( (pLoop->wsFlags & WHERE_IPK)!=0
119741
- && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
119742
- ){
119743
- /* Case 2: We can directly reference a single row using an
119744
- ** equality comparison against the ROWID field. Or
119745
- ** we reference multiple rows using a "rowid IN (...)"
119746
- ** construct.
119747
- */
119748
- assert( pLoop->u.btree.nEq==1 );
119749
- pTerm = pLoop->aLTerm[0];
119750
- assert( pTerm!=0 );
119751
- assert( pTerm->pExpr!=0 );
119752
- assert( omitTable==0 );
119753
- testcase( pTerm->wtFlags & TERM_VIRTUAL );
119754
- iReleaseReg = ++pParse->nMem;
119755
- iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
119756
- if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
119757
- addrNxt = pLevel->addrNxt;
119758
- sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
119759
- sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
119760
- VdbeCoverage(v);
119761
- sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
119762
- sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
119763
- VdbeComment((v, "pk"));
119764
- pLevel->op = OP_Noop;
119765
- }else if( (pLoop->wsFlags & WHERE_IPK)!=0
119766
- && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
119767
- ){
119768
- /* Case 3: We have an inequality comparison against the ROWID field.
119769
- */
119770
- int testOp = OP_Noop;
119771
- int start;
119772
- int memEndValue = 0;
119773
- WhereTerm *pStart, *pEnd;
119774
-
119775
- assert( omitTable==0 );
119776
- j = 0;
119777
- pStart = pEnd = 0;
119778
- if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
119779
- if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
119780
- assert( pStart!=0 || pEnd!=0 );
119781
- if( bRev ){
119782
- pTerm = pStart;
119783
- pStart = pEnd;
119784
- pEnd = pTerm;
119785
- }
119786
- if( pStart ){
119787
- Expr *pX; /* The expression that defines the start bound */
119788
- int r1, rTemp; /* Registers for holding the start boundary */
119789
-
119790
- /* The following constant maps TK_xx codes into corresponding
119791
- ** seek opcodes. It depends on a particular ordering of TK_xx
119792
- */
119793
- const u8 aMoveOp[] = {
119794
- /* TK_GT */ OP_SeekGT,
119795
- /* TK_LE */ OP_SeekLE,
119796
- /* TK_LT */ OP_SeekLT,
119797
- /* TK_GE */ OP_SeekGE
119798
- };
119799
- assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
119800
- assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
119801
- assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
119802
-
119803
- assert( (pStart->wtFlags & TERM_VNULL)==0 );
119804
- testcase( pStart->wtFlags & TERM_VIRTUAL );
119805
- pX = pStart->pExpr;
119806
- assert( pX!=0 );
119807
- testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
119808
- r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
119809
- sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
119810
- VdbeComment((v, "pk"));
119811
- VdbeCoverageIf(v, pX->op==TK_GT);
119812
- VdbeCoverageIf(v, pX->op==TK_LE);
119813
- VdbeCoverageIf(v, pX->op==TK_LT);
119814
- VdbeCoverageIf(v, pX->op==TK_GE);
119815
- sqlite3ExprCacheAffinityChange(pParse, r1, 1);
119816
- sqlite3ReleaseTempReg(pParse, rTemp);
119817
- disableTerm(pLevel, pStart);
119818
- }else{
119819
- sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
119820
- VdbeCoverageIf(v, bRev==0);
119821
- VdbeCoverageIf(v, bRev!=0);
119822
- }
119823
- if( pEnd ){
119824
- Expr *pX;
119825
- pX = pEnd->pExpr;
119826
- assert( pX!=0 );
119827
- assert( (pEnd->wtFlags & TERM_VNULL)==0 );
119828
- testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
119829
- testcase( pEnd->wtFlags & TERM_VIRTUAL );
119830
- memEndValue = ++pParse->nMem;
119831
- sqlite3ExprCode(pParse, pX->pRight, memEndValue);
119832
- if( pX->op==TK_LT || pX->op==TK_GT ){
119833
- testOp = bRev ? OP_Le : OP_Ge;
119834
- }else{
119835
- testOp = bRev ? OP_Lt : OP_Gt;
119836
- }
119837
- disableTerm(pLevel, pEnd);
119838
- }
119839
- start = sqlite3VdbeCurrentAddr(v);
119840
- pLevel->op = bRev ? OP_Prev : OP_Next;
119841
- pLevel->p1 = iCur;
119842
- pLevel->p2 = start;
119843
- assert( pLevel->p5==0 );
119844
- if( testOp!=OP_Noop ){
119845
- iRowidReg = ++pParse->nMem;
119846
- sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
119847
- sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
119848
- sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
119849
- VdbeCoverageIf(v, testOp==OP_Le);
119850
- VdbeCoverageIf(v, testOp==OP_Lt);
119851
- VdbeCoverageIf(v, testOp==OP_Ge);
119852
- VdbeCoverageIf(v, testOp==OP_Gt);
119853
- sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
119854
- }
119855
- }else if( pLoop->wsFlags & WHERE_INDEXED ){
119856
- /* Case 4: A scan using an index.
119857
- **
119858
- ** The WHERE clause may contain zero or more equality
119859
- ** terms ("==" or "IN" operators) that refer to the N
119860
- ** left-most columns of the index. It may also contain
119861
- ** inequality constraints (>, <, >= or <=) on the indexed
119862
- ** column that immediately follows the N equalities. Only
119863
- ** the right-most column can be an inequality - the rest must
119864
- ** use the "==" and "IN" operators. For example, if the
119865
- ** index is on (x,y,z), then the following clauses are all
119866
- ** optimized:
119867
- **
119868
- ** x=5
119869
- ** x=5 AND y=10
119870
- ** x=5 AND y<10
119871
- ** x=5 AND y>5 AND y<10
119872
- ** x=5 AND y=5 AND z<=10
119873
- **
119874
- ** The z<10 term of the following cannot be used, only
119875
- ** the x=5 term:
119876
- **
119877
- ** x=5 AND z<10
119878
- **
119879
- ** N may be zero if there are inequality constraints.
119880
- ** If there are no inequality constraints, then N is at
119881
- ** least one.
119882
- **
119883
- ** This case is also used when there are no WHERE clause
119884
- ** constraints but an index is selected anyway, in order
119885
- ** to force the output order to conform to an ORDER BY.
119886
- */
119887
- static const u8 aStartOp[] = {
119888
- 0,
119889
- 0,
119890
- OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
119891
- OP_Last, /* 3: (!start_constraints && startEq && bRev) */
119892
- OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
119893
- OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
119894
- OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
119895
- OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
119896
- };
119897
- static const u8 aEndOp[] = {
119898
- OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
119899
- OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
119900
- OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
119901
- OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
119902
- };
119903
- u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
119904
- int regBase; /* Base register holding constraint values */
119905
- WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
119906
- WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
119907
- int startEq; /* True if range start uses ==, >= or <= */
119908
- int endEq; /* True if range end uses ==, >= or <= */
119909
- int start_constraints; /* Start of range is constrained */
119910
- int nConstraint; /* Number of constraint terms */
119911
- Index *pIdx; /* The index we will be using */
119912
- int iIdxCur; /* The VDBE cursor for the index */
119913
- int nExtraReg = 0; /* Number of extra registers needed */
119914
- int op; /* Instruction opcode */
119915
- char *zStartAff; /* Affinity for start of range constraint */
119916
- char cEndAff = 0; /* Affinity for end of range constraint */
119917
- u8 bSeekPastNull = 0; /* True to seek past initial nulls */
119918
- u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
119919
-
119920
- pIdx = pLoop->u.btree.pIndex;
119921
- iIdxCur = pLevel->iIdxCur;
119922
- assert( nEq>=pLoop->nSkip );
119923
-
119924
- /* If this loop satisfies a sort order (pOrderBy) request that
119925
- ** was passed to this function to implement a "SELECT min(x) ..."
119926
- ** query, then the caller will only allow the loop to run for
119927
- ** a single iteration. This means that the first row returned
119928
- ** should not have a NULL value stored in 'x'. If column 'x' is
119929
- ** the first one after the nEq equality constraints in the index,
119930
- ** this requires some special handling.
119931
- */
119932
- assert( pWInfo->pOrderBy==0
119933
- || pWInfo->pOrderBy->nExpr==1
119934
- || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
119935
- if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
119936
- && pWInfo->nOBSat>0
119937
- && (pIdx->nKeyCol>nEq)
119938
- ){
119939
- assert( pLoop->nSkip==0 );
119940
- bSeekPastNull = 1;
119941
- nExtraReg = 1;
119942
- }
119943
-
119944
- /* Find any inequality constraint terms for the start and end
119945
- ** of the range.
119946
- */
119947
- j = nEq;
119948
- if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
119949
- pRangeStart = pLoop->aLTerm[j++];
119950
- nExtraReg = 1;
119951
- /* Like optimization range constraints always occur in pairs */
119952
- assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
119953
- (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
119954
- }
119955
- if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
119956
- pRangeEnd = pLoop->aLTerm[j++];
119957
- nExtraReg = 1;
119958
- if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
119959
- assert( pRangeStart!=0 ); /* LIKE opt constraints */
119960
- assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
119961
- pLevel->iLikeRepCntr = ++pParse->nMem;
119962
- testcase( bRev );
119963
- testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
119964
- sqlite3VdbeAddOp2(v, OP_Integer,
119965
- bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
119966
- pLevel->iLikeRepCntr);
119967
- VdbeComment((v, "LIKE loop counter"));
119968
- pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
119969
- }
119970
- if( pRangeStart==0
119971
- && (j = pIdx->aiColumn[nEq])>=0
119972
- && pIdx->pTable->aCol[j].notNull==0
119973
- ){
119974
- bSeekPastNull = 1;
119975
- }
119976
- }
119977
- assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
119978
-
119979
- /* Generate code to evaluate all constraint terms using == or IN
119980
- ** and store the values of those terms in an array of registers
119981
- ** starting at regBase.
119982
- */
119983
- regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
119984
- assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
119985
- if( zStartAff ) cEndAff = zStartAff[nEq];
119986
- addrNxt = pLevel->addrNxt;
119987
-
119988
- /* If we are doing a reverse order scan on an ascending index, or
119989
- ** a forward order scan on a descending index, interchange the
119990
- ** start and end terms (pRangeStart and pRangeEnd).
119991
- */
119992
- if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
119993
- || (bRev && pIdx->nKeyCol==nEq)
119994
- ){
119995
- SWAP(WhereTerm *, pRangeEnd, pRangeStart);
119996
- SWAP(u8, bSeekPastNull, bStopAtNull);
119997
- }
119998
-
119999
- testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
120000
- testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
120001
- testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
120002
- testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
120003
- startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
120004
- endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
120005
- start_constraints = pRangeStart || nEq>0;
120006
-
120007
- /* Seek the index cursor to the start of the range. */
120008
- nConstraint = nEq;
120009
- if( pRangeStart ){
120010
- Expr *pRight = pRangeStart->pExpr->pRight;
120011
- sqlite3ExprCode(pParse, pRight, regBase+nEq);
120012
- whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
120013
- if( (pRangeStart->wtFlags & TERM_VNULL)==0
120014
- && sqlite3ExprCanBeNull(pRight)
120015
- ){
120016
- sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
120017
- VdbeCoverage(v);
120018
- }
120019
- if( zStartAff ){
120020
- if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
120021
- /* Since the comparison is to be performed with no conversions
120022
- ** applied to the operands, set the affinity to apply to pRight to
120023
- ** SQLITE_AFF_NONE. */
120024
- zStartAff[nEq] = SQLITE_AFF_NONE;
120025
- }
120026
- if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
120027
- zStartAff[nEq] = SQLITE_AFF_NONE;
120028
- }
120029
- }
120030
- nConstraint++;
120031
- testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
120032
- }else if( bSeekPastNull ){
120033
- sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
120034
- nConstraint++;
120035
- startEq = 0;
120036
- start_constraints = 1;
120037
- }
120038
- codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
120039
- op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
120040
- assert( op!=0 );
120041
- sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
120042
- VdbeCoverage(v);
120043
- VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
120044
- VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
120045
- VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
120046
- VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
120047
- VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
120048
- VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
120049
-
120050
- /* Load the value for the inequality constraint at the end of the
120051
- ** range (if any).
120052
- */
120053
- nConstraint = nEq;
120054
- if( pRangeEnd ){
120055
- Expr *pRight = pRangeEnd->pExpr->pRight;
120056
- sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
120057
- sqlite3ExprCode(pParse, pRight, regBase+nEq);
120058
- whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
120059
- if( (pRangeEnd->wtFlags & TERM_VNULL)==0
120060
- && sqlite3ExprCanBeNull(pRight)
120061
- ){
120062
- sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
120063
- VdbeCoverage(v);
120064
- }
120065
- if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
120066
- && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
120067
- ){
120068
- codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
120069
- }
120070
- nConstraint++;
120071
- testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
120072
- }else if( bStopAtNull ){
120073
- sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
120074
- endEq = 0;
120075
- nConstraint++;
120076
- }
120077
- sqlite3DbFree(db, zStartAff);
120078
-
120079
- /* Top of the loop body */
120080
- pLevel->p2 = sqlite3VdbeCurrentAddr(v);
120081
-
120082
- /* Check if the index cursor is past the end of the range. */
120083
- if( nConstraint ){
120084
- op = aEndOp[bRev*2 + endEq];
120085
- sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
120086
- testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
120087
- testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
120088
- testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
120089
- testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
120090
- }
120091
-
120092
- /* Seek the table cursor, if required */
120093
- disableTerm(pLevel, pRangeStart);
120094
- disableTerm(pLevel, pRangeEnd);
120095
- if( omitTable ){
120096
- /* pIdx is a covering index. No need to access the main table. */
120097
- }else if( HasRowid(pIdx->pTable) ){
120098
- iRowidReg = ++pParse->nMem;
120099
- sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
120100
- sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
120101
- sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
120102
- }else if( iCur!=iIdxCur ){
120103
- Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
120104
- iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
120105
- for(j=0; j<pPk->nKeyCol; j++){
120106
- k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
120107
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
120108
- }
120109
- sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
120110
- iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
120111
- }
120112
-
120113
- /* Record the instruction used to terminate the loop. Disable
120114
- ** WHERE clause terms made redundant by the index range scan.
120115
- */
120116
- if( pLoop->wsFlags & WHERE_ONEROW ){
120117
- pLevel->op = OP_Noop;
120118
- }else if( bRev ){
120119
- pLevel->op = OP_Prev;
120120
- }else{
120121
- pLevel->op = OP_Next;
120122
- }
120123
- pLevel->p1 = iIdxCur;
120124
- pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
120125
- if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
120126
- pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
120127
- }else{
120128
- assert( pLevel->p5==0 );
120129
- }
120130
- }else
120131
-
120132
-#ifndef SQLITE_OMIT_OR_OPTIMIZATION
120133
- if( pLoop->wsFlags & WHERE_MULTI_OR ){
120134
- /* Case 5: Two or more separately indexed terms connected by OR
120135
- **
120136
- ** Example:
120137
- **
120138
- ** CREATE TABLE t1(a,b,c,d);
120139
- ** CREATE INDEX i1 ON t1(a);
120140
- ** CREATE INDEX i2 ON t1(b);
120141
- ** CREATE INDEX i3 ON t1(c);
120142
- **
120143
- ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
120144
- **
120145
- ** In the example, there are three indexed terms connected by OR.
120146
- ** The top of the loop looks like this:
120147
- **
120148
- ** Null 1 # Zero the rowset in reg 1
120149
- **
120150
- ** Then, for each indexed term, the following. The arguments to
120151
- ** RowSetTest are such that the rowid of the current row is inserted
120152
- ** into the RowSet. If it is already present, control skips the
120153
- ** Gosub opcode and jumps straight to the code generated by WhereEnd().
120154
- **
120155
- ** sqlite3WhereBegin(<term>)
120156
- ** RowSetTest # Insert rowid into rowset
120157
- ** Gosub 2 A
120158
- ** sqlite3WhereEnd()
120159
- **
120160
- ** Following the above, code to terminate the loop. Label A, the target
120161
- ** of the Gosub above, jumps to the instruction right after the Goto.
120162
- **
120163
- ** Null 1 # Zero the rowset in reg 1
120164
- ** Goto B # The loop is finished.
120165
- **
120166
- ** A: <loop body> # Return data, whatever.
120167
- **
120168
- ** Return 2 # Jump back to the Gosub
120169
- **
120170
- ** B: <after the loop>
120171
- **
120172
- ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
120173
- ** use an ephemeral index instead of a RowSet to record the primary
120174
- ** keys of the rows we have already seen.
120175
- **
120176
- */
120177
- WhereClause *pOrWc; /* The OR-clause broken out into subterms */
120178
- SrcList *pOrTab; /* Shortened table list or OR-clause generation */
120179
- Index *pCov = 0; /* Potential covering index (or NULL) */
120180
- int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
120181
-
120182
- int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
120183
- int regRowset = 0; /* Register for RowSet object */
120184
- int regRowid = 0; /* Register holding rowid */
120185
- int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
120186
- int iRetInit; /* Address of regReturn init */
120187
- int untestedTerms = 0; /* Some terms not completely tested */
120188
- int ii; /* Loop counter */
120189
- u16 wctrlFlags; /* Flags for sub-WHERE clause */
120190
- Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
120191
- Table *pTab = pTabItem->pTab;
120192
-
120193
- pTerm = pLoop->aLTerm[0];
120194
- assert( pTerm!=0 );
120195
- assert( pTerm->eOperator & WO_OR );
120196
- assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
120197
- pOrWc = &pTerm->u.pOrInfo->wc;
120198
- pLevel->op = OP_Return;
120199
- pLevel->p1 = regReturn;
120200
-
120201
- /* Set up a new SrcList in pOrTab containing the table being scanned
120202
- ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
120203
- ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
120204
- */
120205
- if( pWInfo->nLevel>1 ){
120206
- int nNotReady; /* The number of notReady tables */
120207
- struct SrcList_item *origSrc; /* Original list of tables */
120208
- nNotReady = pWInfo->nLevel - iLevel - 1;
120209
- pOrTab = sqlite3StackAllocRaw(db,
120210
- sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
120211
- if( pOrTab==0 ) return notReady;
120212
- pOrTab->nAlloc = (u8)(nNotReady + 1);
120213
- pOrTab->nSrc = pOrTab->nAlloc;
120214
- memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
120215
- origSrc = pWInfo->pTabList->a;
120216
- for(k=1; k<=nNotReady; k++){
120217
- memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
120218
- }
120219
- }else{
120220
- pOrTab = pWInfo->pTabList;
120221
- }
120222
-
120223
- /* Initialize the rowset register to contain NULL. An SQL NULL is
120224
- ** equivalent to an empty rowset. Or, create an ephemeral index
120225
- ** capable of holding primary keys in the case of a WITHOUT ROWID.
120226
- **
120227
- ** Also initialize regReturn to contain the address of the instruction
120228
- ** immediately following the OP_Return at the bottom of the loop. This
120229
- ** is required in a few obscure LEFT JOIN cases where control jumps
120230
- ** over the top of the loop into the body of it. In this case the
120231
- ** correct response for the end-of-loop code (the OP_Return) is to
120232
- ** fall through to the next instruction, just as an OP_Next does if
120233
- ** called on an uninitialized cursor.
120234
- */
120235
- if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
120236
- if( HasRowid(pTab) ){
120237
- regRowset = ++pParse->nMem;
120238
- sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
120239
- }else{
120240
- Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120241
- regRowset = pParse->nTab++;
120242
- sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
120243
- sqlite3VdbeSetP4KeyInfo(pParse, pPk);
120244
- }
120245
- regRowid = ++pParse->nMem;
120246
- }
120247
- iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
120248
-
120249
- /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
120250
- ** Then for every term xN, evaluate as the subexpression: xN AND z
120251
- ** That way, terms in y that are factored into the disjunction will
120252
- ** be picked up by the recursive calls to sqlite3WhereBegin() below.
120253
- **
120254
- ** Actually, each subexpression is converted to "xN AND w" where w is
120255
- ** the "interesting" terms of z - terms that did not originate in the
120256
- ** ON or USING clause of a LEFT JOIN, and terms that are usable as
120257
- ** indices.
120258
- **
120259
- ** This optimization also only applies if the (x1 OR x2 OR ...) term
120260
- ** is not contained in the ON clause of a LEFT JOIN.
120261
- ** See ticket http://www.sqlite.org/src/info/f2369304e4
120262
- */
120263
- if( pWC->nTerm>1 ){
120264
- int iTerm;
120265
- for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
120266
- Expr *pExpr = pWC->a[iTerm].pExpr;
120267
- if( &pWC->a[iTerm] == pTerm ) continue;
120268
- if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
120269
- if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
120270
- if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
120271
- testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
120272
- pExpr = sqlite3ExprDup(db, pExpr, 0);
120273
- pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
120274
- }
120275
- if( pAndExpr ){
120276
- pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
120277
- }
120278
- }
120279
-
120280
- /* Run a separate WHERE clause for each term of the OR clause. After
120281
- ** eliminating duplicates from other WHERE clauses, the action for each
120282
- ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
120283
- */
120284
- wctrlFlags = WHERE_OMIT_OPEN_CLOSE
120285
- | WHERE_FORCE_TABLE
120286
- | WHERE_ONETABLE_ONLY
120287
- | WHERE_NO_AUTOINDEX;
120288
- for(ii=0; ii<pOrWc->nTerm; ii++){
120289
- WhereTerm *pOrTerm = &pOrWc->a[ii];
120290
- if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
120291
- WhereInfo *pSubWInfo; /* Info for single OR-term scan */
120292
- Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
120293
- int j1 = 0; /* Address of jump operation */
120294
- if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
120295
- pAndExpr->pLeft = pOrExpr;
120296
- pOrExpr = pAndExpr;
120297
- }
120298
- /* Loop through table entries that match term pOrTerm. */
120299
- WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
120300
- pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
120301
- wctrlFlags, iCovCur);
120302
- assert( pSubWInfo || pParse->nErr || db->mallocFailed );
120303
- if( pSubWInfo ){
120304
- WhereLoop *pSubLoop;
120305
- int addrExplain = explainOneScan(
120306
- pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
120307
- );
120308
- addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
120309
-
120310
- /* This is the sub-WHERE clause body. First skip over
120311
- ** duplicate rows from prior sub-WHERE clauses, and record the
120312
- ** rowid (or PRIMARY KEY) for the current row so that the same
120313
- ** row will be skipped in subsequent sub-WHERE clauses.
120314
- */
120315
- if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
120316
- int r;
120317
- int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
120318
- if( HasRowid(pTab) ){
120319
- r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
120320
- j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
120321
- VdbeCoverage(v);
120322
- }else{
120323
- Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120324
- int nPk = pPk->nKeyCol;
120325
- int iPk;
120326
-
120327
- /* Read the PK into an array of temp registers. */
120328
- r = sqlite3GetTempRange(pParse, nPk);
120329
- for(iPk=0; iPk<nPk; iPk++){
120330
- int iCol = pPk->aiColumn[iPk];
120331
- sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
120332
- }
120333
-
120334
- /* Check if the temp table already contains this key. If so,
120335
- ** the row has already been included in the result set and
120336
- ** can be ignored (by jumping past the Gosub below). Otherwise,
120337
- ** insert the key into the temp table and proceed with processing
120338
- ** the row.
120339
- **
120340
- ** Use some of the same optimizations as OP_RowSetTest: If iSet
120341
- ** is zero, assume that the key cannot already be present in
120342
- ** the temp table. And if iSet is -1, assume that there is no
120343
- ** need to insert the key into the temp table, as it will never
120344
- ** be tested for. */
120345
- if( iSet ){
120346
- j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
120347
- VdbeCoverage(v);
120348
- }
120349
- if( iSet>=0 ){
120350
- sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
120351
- sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
120352
- if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
120353
- }
120354
-
120355
- /* Release the array of temp registers */
120356
- sqlite3ReleaseTempRange(pParse, r, nPk);
120357
- }
120358
- }
120359
-
120360
- /* Invoke the main loop body as a subroutine */
120361
- sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
120362
-
120363
- /* Jump here (skipping the main loop body subroutine) if the
120364
- ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
120365
- if( j1 ) sqlite3VdbeJumpHere(v, j1);
120366
-
120367
- /* The pSubWInfo->untestedTerms flag means that this OR term
120368
- ** contained one or more AND term from a notReady table. The
120369
- ** terms from the notReady table could not be tested and will
120370
- ** need to be tested later.
120371
- */
120372
- if( pSubWInfo->untestedTerms ) untestedTerms = 1;
120373
-
120374
- /* If all of the OR-connected terms are optimized using the same
120375
- ** index, and the index is opened using the same cursor number
120376
- ** by each call to sqlite3WhereBegin() made by this loop, it may
120377
- ** be possible to use that index as a covering index.
120378
- **
120379
- ** If the call to sqlite3WhereBegin() above resulted in a scan that
120380
- ** uses an index, and this is either the first OR-connected term
120381
- ** processed or the index is the same as that used by all previous
120382
- ** terms, set pCov to the candidate covering index. Otherwise, set
120383
- ** pCov to NULL to indicate that no candidate covering index will
120384
- ** be available.
120385
- */
120386
- pSubLoop = pSubWInfo->a[0].pWLoop;
120387
- assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
120388
- if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
120389
- && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
120390
- && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
120391
- ){
120392
- assert( pSubWInfo->a[0].iIdxCur==iCovCur );
120393
- pCov = pSubLoop->u.btree.pIndex;
120394
- wctrlFlags |= WHERE_REOPEN_IDX;
120395
- }else{
120396
- pCov = 0;
120397
- }
120398
-
120399
- /* Finish the loop through table entries that match term pOrTerm. */
120400
- sqlite3WhereEnd(pSubWInfo);
120401
- }
120402
- }
120403
- }
120404
- pLevel->u.pCovidx = pCov;
120405
- if( pCov ) pLevel->iIdxCur = iCovCur;
120406
- if( pAndExpr ){
120407
- pAndExpr->pLeft = 0;
120408
- sqlite3ExprDelete(db, pAndExpr);
120409
- }
120410
- sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
120411
- sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
120412
- sqlite3VdbeResolveLabel(v, iLoopBody);
120413
-
120414
- if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
120415
- if( !untestedTerms ) disableTerm(pLevel, pTerm);
120416
- }else
120417
-#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
120418
-
120419
- {
120420
- /* Case 6: There is no usable index. We must do a complete
120421
- ** scan of the entire table.
120422
- */
120423
- static const u8 aStep[] = { OP_Next, OP_Prev };
120424
- static const u8 aStart[] = { OP_Rewind, OP_Last };
120425
- assert( bRev==0 || bRev==1 );
120426
- if( pTabItem->isRecursive ){
120427
- /* Tables marked isRecursive have only a single row that is stored in
120428
- ** a pseudo-cursor. No need to Rewind or Next such cursors. */
120429
- pLevel->op = OP_Noop;
120430
- }else{
120431
- pLevel->op = aStep[bRev];
120432
- pLevel->p1 = iCur;
120433
- pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
120434
- VdbeCoverageIf(v, bRev==0);
120435
- VdbeCoverageIf(v, bRev!=0);
120436
- pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
120437
- }
120438
- }
120439
-
120440
-#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
120441
- pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
120442
-#endif
120443
-
120444
- /* Insert code to test every subexpression that can be completely
120445
- ** computed using the current set of tables.
120446
- */
120447
- for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
120448
- Expr *pE;
120449
- int skipLikeAddr = 0;
120450
- testcase( pTerm->wtFlags & TERM_VIRTUAL );
120451
- testcase( pTerm->wtFlags & TERM_CODED );
120452
- if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120453
- if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
120454
- testcase( pWInfo->untestedTerms==0
120455
- && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
120456
- pWInfo->untestedTerms = 1;
120457
- continue;
120458
- }
120459
- pE = pTerm->pExpr;
120460
- assert( pE!=0 );
120461
- if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
120462
- continue;
120463
- }
120464
- if( pTerm->wtFlags & TERM_LIKECOND ){
120465
- assert( pLevel->iLikeRepCntr>0 );
120466
- skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
120467
- VdbeCoverage(v);
120468
- }
120469
- sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
120470
- if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
120471
- pTerm->wtFlags |= TERM_CODED;
120472
- }
120473
-
120474
- /* Insert code to test for implied constraints based on transitivity
120475
- ** of the "==" operator.
120476
- **
120477
- ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
120478
- ** and we are coding the t1 loop and the t2 loop has not yet coded,
120479
- ** then we cannot use the "t1.a=t2.b" constraint, but we can code
120480
- ** the implied "t1.a=123" constraint.
120481
- */
120482
- for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
120483
- Expr *pE, *pEAlt;
120484
- WhereTerm *pAlt;
120485
- if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120486
- if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
120487
- if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
120488
- if( pTerm->leftCursor!=iCur ) continue;
120489
- if( pLevel->iLeftJoin ) continue;
120490
- pE = pTerm->pExpr;
120491
- assert( !ExprHasProperty(pE, EP_FromJoin) );
120492
- assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
120493
- pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
120494
- WO_EQ|WO_IN|WO_IS, 0);
120495
- if( pAlt==0 ) continue;
120496
- if( pAlt->wtFlags & (TERM_CODED) ) continue;
120497
- testcase( pAlt->eOperator & WO_EQ );
120498
- testcase( pAlt->eOperator & WO_IS );
120499
- testcase( pAlt->eOperator & WO_IN );
120500
- VdbeModuleComment((v, "begin transitive constraint"));
120501
- pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
120502
- if( pEAlt ){
120503
- *pEAlt = *pAlt->pExpr;
120504
- pEAlt->pLeft = pE->pLeft;
120505
- sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
120506
- sqlite3StackFree(db, pEAlt);
120507
- }
120508
- }
120509
-
120510
- /* For a LEFT OUTER JOIN, generate code that will record the fact that
120511
- ** at least one row of the right table has matched the left table.
120512
- */
120513
- if( pLevel->iLeftJoin ){
120514
- pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
120515
- sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
120516
- VdbeComment((v, "record LEFT JOIN hit"));
120517
- sqlite3ExprCacheClear(pParse);
120518
- for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
120519
- testcase( pTerm->wtFlags & TERM_VIRTUAL );
120520
- testcase( pTerm->wtFlags & TERM_CODED );
120521
- if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120522
- if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
120523
- assert( pWInfo->untestedTerms );
120524
- continue;
120525
- }
120526
- assert( pTerm->pExpr );
120527
- sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
120528
- pTerm->wtFlags |= TERM_CODED;
120529
- }
120530
- }
120531
-
120532
- return pLevel->notReady;
120533
-}
120534121353
120535121354
#ifdef WHERETRACE_ENABLED
120536121355
/*
120537121356
** Print the content of a WhereTerm object
120538121357
*/
@@ -120695,11 +121514,11 @@
120695121514
WhereLevel *pLevel = &pWInfo->a[i];
120696121515
if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
120697121516
sqlite3DbFree(db, pLevel->u.in.aInLoop);
120698121517
}
120699121518
}
120700
- whereClauseClear(&pWInfo->sWC);
121519
+ sqlite3WhereClauseClear(&pWInfo->sWC);
120701121520
while( pWInfo->pLoops ){
120702121521
WhereLoop *p = pWInfo->pLoops;
120703121522
pWInfo->pLoops = p->pNextLoop;
120704121523
whereLoopDelete(db, p);
120705121524
}
@@ -121647,14 +122466,36 @@
121647122466
121648122467
#ifndef SQLITE_OMIT_VIRTUALTABLE
121649122468
/*
121650122469
** Add all WhereLoop objects for a table of the join identified by
121651122470
** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
122471
+**
122472
+** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and
122473
+** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause
122474
+** entries that occur before the virtual table in the FROM clause and are
122475
+** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
122476
+** mUnusable mask contains all FROM clause entries that occur after the
122477
+** virtual table and are separated from it by at least one LEFT or
122478
+** CROSS JOIN.
122479
+**
122480
+** For example, if the query were:
122481
+**
122482
+** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
122483
+**
122484
+** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6).
122485
+**
122486
+** All the tables in mExtra must be scanned before the current virtual
122487
+** table. So any terms for which all prerequisites are satisfied by
122488
+** mExtra may be specified as "usable" in all calls to xBestIndex.
122489
+** Conversely, all tables in mUnusable must be scanned after the current
122490
+** virtual table, so any terms for which the prerequisites overlap with
122491
+** mUnusable should always be configured as "not-usable" for xBestIndex.
121652122492
*/
121653122493
static int whereLoopAddVirtual(
121654122494
WhereLoopBuilder *pBuilder, /* WHERE clause information */
121655
- Bitmask mExtra
122495
+ Bitmask mExtra, /* Tables that must be scanned before this one */
122496
+ Bitmask mUnusable /* Tables that must be scanned after this one */
121656122497
){
121657122498
WhereInfo *pWInfo; /* WHERE analysis context */
121658122499
Parse *pParse; /* The parsing context */
121659122500
WhereClause *pWC; /* The WHERE clause */
121660122501
struct SrcList_item *pSrc; /* The FROM clause term to search */
@@ -121671,19 +122512,20 @@
121671122512
int seenVar = 0; /* True if a non-constant constraint is seen */
121672122513
int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */
121673122514
WhereLoop *pNew;
121674122515
int rc = SQLITE_OK;
121675122516
122517
+ assert( (mExtra & mUnusable)==0 );
121676122518
pWInfo = pBuilder->pWInfo;
121677122519
pParse = pWInfo->pParse;
121678122520
db = pParse->db;
121679122521
pWC = pBuilder->pWC;
121680122522
pNew = pBuilder->pNew;
121681122523
pSrc = &pWInfo->pTabList->a[pNew->iTab];
121682122524
pTab = pSrc->pTab;
121683122525
assert( IsVirtual(pTab) );
121684
- pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
122526
+ pIdxInfo = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy);
121685122527
if( pIdxInfo==0 ) return SQLITE_NOMEM;
121686122528
pNew->prereq = 0;
121687122529
pNew->rSetup = 0;
121688122530
pNew->wsFlags = WHERE_VIRTUALTABLE;
121689122531
pNew->nLTerm = 0;
@@ -121709,19 +122551,19 @@
121709122551
case 0: /* Constants without IN operator */
121710122552
pIdxCons->usable = 0;
121711122553
if( (pTerm->eOperator & WO_IN)!=0 ){
121712122554
seenIn = 1;
121713122555
}
121714
- if( pTerm->prereqRight!=0 ){
122556
+ if( (pTerm->prereqRight & ~mExtra)!=0 ){
121715122557
seenVar = 1;
121716122558
}else if( (pTerm->eOperator & WO_IN)==0 ){
121717122559
pIdxCons->usable = 1;
121718122560
}
121719122561
break;
121720122562
case 1: /* Constants with IN operators */
121721122563
assert( seenIn );
121722
- pIdxCons->usable = (pTerm->prereqRight==0);
122564
+ pIdxCons->usable = (pTerm->prereqRight & ~mExtra)==0;
121723122565
break;
121724122566
case 2: /* Variables without IN */
121725122567
assert( seenVar );
121726122568
pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
121727122569
break;
@@ -121816,11 +122658,15 @@
121816122658
121817122659
/*
121818122660
** Add WhereLoop entries to handle OR terms. This works for either
121819122661
** btrees or virtual tables.
121820122662
*/
121821
-static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
122663
+static int whereLoopAddOr(
122664
+ WhereLoopBuilder *pBuilder,
122665
+ Bitmask mExtra,
122666
+ Bitmask mUnusable
122667
+){
121822122668
WhereInfo *pWInfo = pBuilder->pWInfo;
121823122669
WhereClause *pWC;
121824122670
WhereLoop *pNew;
121825122671
WhereTerm *pTerm, *pWCEnd;
121826122672
int rc = SQLITE_OK;
@@ -121875,18 +122721,18 @@
121875122721
}
121876122722
}
121877122723
#endif
121878122724
#ifndef SQLITE_OMIT_VIRTUALTABLE
121879122725
if( IsVirtual(pItem->pTab) ){
121880
- rc = whereLoopAddVirtual(&sSubBuild, mExtra);
122726
+ rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable);
121881122727
}else
121882122728
#endif
121883122729
{
121884122730
rc = whereLoopAddBtree(&sSubBuild, mExtra);
121885122731
}
121886122732
if( rc==SQLITE_OK ){
121887
- rc = whereLoopAddOr(&sSubBuild, mExtra);
122733
+ rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable);
121888122734
}
121889122735
assert( rc==SQLITE_OK || sCur.n==0 );
121890122736
if( sCur.n==0 ){
121891122737
sSum.n = 0;
121892122738
break;
@@ -121944,37 +122790,47 @@
121944122790
Bitmask mExtra = 0;
121945122791
Bitmask mPrior = 0;
121946122792
int iTab;
121947122793
SrcList *pTabList = pWInfo->pTabList;
121948122794
struct SrcList_item *pItem;
122795
+ struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
121949122796
sqlite3 *db = pWInfo->pParse->db;
121950
- int nTabList = pWInfo->nLevel;
121951122797
int rc = SQLITE_OK;
121952
- u8 priorJoinType = 0;
121953122798
WhereLoop *pNew;
122799
+ u8 priorJointype = 0;
121954122800
121955122801
/* Loop over the tables in the join, from left to right */
121956122802
pNew = pBuilder->pNew;
121957122803
whereLoopInit(pNew);
121958
- for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
122804
+ for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
122805
+ Bitmask mUnusable = 0;
121959122806
pNew->iTab = iTab;
121960
- pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
121961
- if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
122807
+ pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
122808
+ if( ((pItem->jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
122809
+ /* This condition is true when pItem is the FROM clause term on the
122810
+ ** right-hand-side of a LEFT or CROSS JOIN. */
121962122811
mExtra = mPrior;
121963122812
}
121964
- priorJoinType = pItem->jointype;
122813
+ priorJointype = pItem->jointype;
121965122814
if( IsVirtual(pItem->pTab) ){
121966
- rc = whereLoopAddVirtual(pBuilder, mExtra);
122815
+ struct SrcList_item *p;
122816
+ for(p=&pItem[1]; p<pEnd; p++){
122817
+ if( mUnusable || (p->jointype & (JT_LEFT|JT_CROSS)) ){
122818
+ mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
122819
+ }
122820
+ }
122821
+ rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable);
121967122822
}else{
121968122823
rc = whereLoopAddBtree(pBuilder, mExtra);
121969122824
}
121970122825
if( rc==SQLITE_OK ){
121971
- rc = whereLoopAddOr(pBuilder, mExtra);
122826
+ rc = whereLoopAddOr(pBuilder, mExtra, mUnusable);
121972122827
}
121973122828
mPrior |= pNew->maskSelf;
121974122829
if( rc || db->mallocFailed ) break;
121975122830
}
122831
+
121976122832
whereLoopClear(db, pNew);
121977122833
return rc;
121978122834
}
121979122835
121980122836
/*
@@ -122076,11 +122932,11 @@
122076122932
for(i=0; i<nOrderBy; i++){
122077122933
if( MASKBIT(i) & obSat ) continue;
122078122934
pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
122079122935
if( pOBExpr->op!=TK_COLUMN ) continue;
122080122936
if( pOBExpr->iTable!=iCur ) continue;
122081
- pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
122937
+ pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
122082122938
~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
122083122939
if( pTerm==0 ) continue;
122084122940
if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
122085122941
const char *z1, *z2;
122086122942
pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
@@ -122213,11 +123069,11 @@
122213123069
for(i=0; i<nOrderBy; i++){
122214123070
Expr *p;
122215123071
Bitmask mTerm;
122216123072
if( MASKBIT(i) & obSat ) continue;
122217123073
p = pOrderBy->a[i].pExpr;
122218
- mTerm = exprTableUsage(&pWInfo->sMaskSet,p);
123074
+ mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
122219123075
if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
122220123076
if( (mTerm&~orderDistinctMask)==0 ){
122221123077
obSat |= MASKBIT(i);
122222123078
}
122223123079
}
@@ -122686,17 +123542,17 @@
122686123542
if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
122687123543
assert( pWInfo->pTabList->nSrc>=1 );
122688123544
pItem = pWInfo->pTabList->a;
122689123545
pTab = pItem->pTab;
122690123546
if( IsVirtual(pTab) ) return 0;
122691
- if( pItem->zIndex ) return 0;
123547
+ if( pItem->zIndexedBy ) return 0;
122692123548
iCur = pItem->iCursor;
122693123549
pWC = &pWInfo->sWC;
122694123550
pLoop = pBuilder->pNew;
122695123551
pLoop->wsFlags = 0;
122696123552
pLoop->nSkip = 0;
122697
- pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
123553
+ pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
122698123554
if( pTerm ){
122699123555
testcase( pTerm->eOperator & WO_IS );
122700123556
pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
122701123557
pLoop->aLTerm[0] = pTerm;
122702123558
pLoop->nLTerm = 1;
@@ -122711,11 +123567,11 @@
122711123567
|| pIdx->pPartIdxWhere!=0
122712123568
|| pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
122713123569
) continue;
122714123570
opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
122715123571
for(j=0; j<pIdx->nKeyCol; j++){
122716
- pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
123572
+ pTerm = sqlite3WhereFindTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
122717123573
if( pTerm==0 ) break;
122718123574
testcase( pTerm->eOperator & WO_IS );
122719123575
pLoop->aLTerm[j] = pTerm;
122720123576
}
122721123577
if( j!=pIdx->nKeyCol ) continue;
@@ -122732,11 +123588,11 @@
122732123588
}
122733123589
}
122734123590
if( pLoop->wsFlags ){
122735123591
pLoop->nOut = (LogEst)1;
122736123592
pWInfo->a[0].pWLoop = pLoop;
122737
- pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
123593
+ pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
122738123594
pWInfo->a[0].iTabCur = iCur;
122739123595
pWInfo->nRowOut = 1;
122740123596
if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
122741123597
if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
122742123598
pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
@@ -122926,12 +123782,12 @@
122926123782
122927123783
/* Split the WHERE clause into separate subexpressions where each
122928123784
** subexpression is separated by an AND operator.
122929123785
*/
122930123786
initMaskSet(pMaskSet);
122931
- whereClauseInit(&pWInfo->sWC, pWInfo);
122932
- whereSplit(&pWInfo->sWC, pWhere, TK_AND);
123787
+ sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
123788
+ sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
122933123789
122934123790
/* Special case: a WHERE clause that is constant. Evaluate the
122935123791
** expression and either jump over all of the code or fall thru.
122936123792
*/
122937123793
for(ii=0; ii<sWLB.pWC->nTerm; ii++){
@@ -122972,26 +123828,20 @@
122972123828
}
122973123829
#ifndef NDEBUG
122974123830
{
122975123831
Bitmask toTheLeft = 0;
122976123832
for(ii=0; ii<pTabList->nSrc; ii++){
122977
- Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
123833
+ Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
122978123834
assert( (m-1)==toTheLeft );
122979123835
toTheLeft |= m;
122980123836
}
122981123837
}
122982123838
#endif
122983123839
122984
- /* Analyze all of the subexpressions. Note that exprAnalyze() might
122985
- ** add new virtual terms onto the end of the WHERE clause. We do not
122986
- ** want to analyze these virtual terms, so start analyzing at the end
122987
- ** and work forward so that the added virtual terms are never processed.
122988
- */
122989
- exprAnalyzeAll(pTabList, &pWInfo->sWC);
122990
- if( db->mallocFailed ){
122991
- goto whereBeginError;
122992
- }
123840
+ /* Analyze all of the subexpressions. */
123841
+ sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
123842
+ if( db->mallocFailed ) goto whereBeginError;
122993123843
122994123844
if( wctrlFlags & WHERE_WANT_DISTINCT ){
122995123845
if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
122996123846
/* The DISTINCT marking is pointless. Ignore it. */
122997123847
pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
@@ -123003,12 +123853,11 @@
123003123853
}
123004123854
123005123855
/* Construct the WhereLoop objects */
123006123856
WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
123007123857
#if defined(WHERETRACE_ENABLED)
123008
- /* Display all terms of the WHERE clause */
123009
- if( sqlite3WhereTrace & 0x100 ){
123858
+ if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
123010123859
int i;
123011123860
for(i=0; i<sWLB.pWC->nTerm; i++){
123012123861
whereTermPrint(&sWLB.pWC->a[i], i);
123013123862
}
123014123863
}
@@ -123016,17 +123865,16 @@
123016123865
123017123866
if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
123018123867
rc = whereLoopAddAll(&sWLB);
123019123868
if( rc ) goto whereBeginError;
123020123869
123021
- /* Display all of the WhereLoop objects if wheretrace is enabled */
123022
-#ifdef WHERETRACE_ENABLED /* !=0 */
123023
- if( sqlite3WhereTrace ){
123870
+#ifdef WHERETRACE_ENABLED
123871
+ if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
123024123872
WhereLoop *p;
123025123873
int i;
123026
- static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
123027
- "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
123874
+ static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
123875
+ "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
123028123876
for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
123029123877
p->cId = zLabel[i%sizeof(zLabel)];
123030123878
whereLoopPrint(p, sWLB.pWC);
123031123879
}
123032123880
}
@@ -123043,11 +123891,11 @@
123043123891
pWInfo->revMask = (Bitmask)(-1);
123044123892
}
123045123893
if( pParse->nErr || NEVER(db->mallocFailed) ){
123046123894
goto whereBeginError;
123047123895
}
123048
-#ifdef WHERETRACE_ENABLED /* !=0 */
123896
+#ifdef WHERETRACE_ENABLED
123049123897
if( sqlite3WhereTrace ){
123050123898
sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
123051123899
if( pWInfo->nOBSat>0 ){
123052123900
sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
123053123901
}
@@ -123074,12 +123922,14 @@
123074123922
/* Attempt to omit tables from the join that do not effect the result */
123075123923
if( pWInfo->nLevel>=2
123076123924
&& pResultSet!=0
123077123925
&& OptimizationEnabled(db, SQLITE_OmitNoopJoin)
123078123926
){
123079
- Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
123080
- if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
123927
+ Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
123928
+ if( sWLB.pOrderBy ){
123929
+ tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
123930
+ }
123081123931
while( pWInfo->nLevel>=2 ){
123082123932
WhereTerm *pTerm, *pEnd;
123083123933
pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
123084123934
if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
123085123935
if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
@@ -123106,11 +123956,11 @@
123106123956
pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
123107123957
123108123958
/* If the caller is an UPDATE or DELETE statement that is requesting
123109123959
** to use a one-pass algorithm, determine if this is appropriate.
123110123960
** The one-pass algorithm only works if the WHERE clause constrains
123111
- ** the statement to update a single row.
123961
+ ** the statement to update or delete a single row.
123112123962
*/
123113123963
assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
123114123964
if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
123115123965
&& (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
123116123966
pWInfo->okOnePass = 1;
@@ -123120,11 +123970,10 @@
123120123970
}
123121123971
123122123972
/* Open all tables in the pTabList and any indices selected for
123123123973
** searching those tables.
123124123974
*/
123125
- notReady = ~(Bitmask)0;
123126123975
for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
123127123976
Table *pTab; /* Table to open */
123128123977
int iDb; /* Index of database containing table/index */
123129123978
struct SrcList_item *pTabItem;
123130123979
@@ -123161,10 +124010,14 @@
123161124010
for(; b; b=b>>1, n++){}
123162124011
sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
123163124012
SQLITE_INT_TO_PTR(n), P4_INT32);
123164124013
assert( n<=pTab->nCol );
123165124014
}
124015
+#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
124016
+ sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
124017
+ (const u8*)&pTabItem->colUsed, P4_INT64);
124018
+#endif
123166124019
}else{
123167124020
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
123168124021
}
123169124022
if( pLoop->wsFlags & WHERE_INDEXED ){
123170124023
Index *pIx = pLoop->u.btree.pIndex;
@@ -123206,14 +124059,28 @@
123206124059
&& (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
123207124060
){
123208124061
sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
123209124062
}
123210124063
VdbeComment((v, "%s", pIx->zName));
124064
+#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
124065
+ {
124066
+ u64 colUsed = 0;
124067
+ int ii, jj;
124068
+ for(ii=0; ii<pIx->nColumn; ii++){
124069
+ jj = pIx->aiColumn[ii];
124070
+ if( jj<0 ) continue;
124071
+ if( jj>63 ) jj = 63;
124072
+ if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
124073
+ colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
124074
+ }
124075
+ sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
124076
+ (u8*)&colUsed, P4_INT64);
124077
+ }
124078
+#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
123211124079
}
123212124080
}
123213124081
if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
123214
- notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
123215124082
}
123216124083
pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
123217124084
if( db->mallocFailed ) goto whereBeginError;
123218124085
123219124086
/* Generate the code to do the search. Each iteration of the for
@@ -123231,18 +124098,18 @@
123231124098
constructAutomaticIndex(pParse, &pWInfo->sWC,
123232124099
&pTabList->a[pLevel->iFrom], notReady, pLevel);
123233124100
if( db->mallocFailed ) goto whereBeginError;
123234124101
}
123235124102
#endif
123236
- addrExplain = explainOneScan(
124103
+ addrExplain = sqlite3WhereExplainOneScan(
123237124104
pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
123238124105
);
123239124106
pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
123240
- notReady = codeOneLoopStart(pWInfo, ii, notReady);
124107
+ notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
123241124108
pWInfo->iContinue = pLevel->addrCont;
123242124109
if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
123243
- addScanStatus(v, pTabList, pLevel, addrExplain);
124110
+ sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
123244124111
}
123245124112
}
123246124113
123247124114
/* Done. */
123248124115
VdbeModuleComment((v, "Begin WHERE-core"));
@@ -124924,11 +125791,11 @@
124924125791
YYCODETYPE yymajor;
124925125792
yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
124926125793
124927125794
/* There is no mechanism by which the parser stack can be popped below
124928125795
** empty in SQLite. */
124929
- if( NEVER(pParser->yyidx<0) ) return 0;
125796
+ assert( pParser->yyidx>=0 );
124930125797
#ifndef NDEBUG
124931125798
if( yyTraceFILE && pParser->yyidx>=0 ){
124932125799
fprintf(yyTraceFILE,"%sPopping %s\n",
124933125800
yyTracePrompt,
124934125801
yyTokenName[yytos->major]);
@@ -127396,11 +128263,15 @@
127396128263
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
127397128264
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
127398128265
};
127399128266
#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
127400128267
#endif
128268
+
128269
+/* Make the IdChar function accessible from ctime.c */
128270
+#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
127401128271
SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
128272
+#endif
127402128273
127403128274
127404128275
/*
127405128276
** Return the length of the token that begins at z[0].
127406128277
** Store the token type in *tokenType before returning.
@@ -128104,11 +128975,11 @@
128104128975
rc = sqlite3_complete(zSql8);
128105128976
}else{
128106128977
rc = SQLITE_NOMEM;
128107128978
}
128108128979
sqlite3ValueFree(pVal);
128109
- return sqlite3ApiExit(0, rc);
128980
+ return rc & 0xff;
128110128981
}
128111128982
#endif /* SQLITE_OMIT_UTF16 */
128112128983
#endif /* SQLITE_OMIT_COMPLETE */
128113128984
128114128985
/************** End of complete.c ********************************************/
@@ -130282,13 +131153,15 @@
130282131153
#endif
130283131154
#if SQLITE_TEMP_STORE==2
130284131155
return ( db->temp_store!=1 );
130285131156
#endif
130286131157
#if SQLITE_TEMP_STORE==3
131158
+ UNUSED_PARAMETER(db);
130287131159
return 1;
130288131160
#endif
130289131161
#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
131162
+ UNUSED_PARAMETER(db);
130290131163
return 0;
130291131164
#endif
130292131165
}
130293131166
130294131167
/*
@@ -131126,11 +131999,11 @@
131126131999
/* Opening a db handle. Fourth parameter is passed 0. */
131127132000
void *pArg = sqlite3GlobalConfig.pSqllogArg;
131128132001
sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
131129132002
}
131130132003
#endif
131131
- return sqlite3ApiExit(0, rc);
132004
+ return rc & 0xff;
131132132005
}
131133132006
131134132007
/*
131135132008
** Open a new database handle.
131136132009
*/
@@ -131184,11 +132057,11 @@
131184132057
}else{
131185132058
rc = SQLITE_NOMEM;
131186132059
}
131187132060
sqlite3ValueFree(pVal);
131188132061
131189
- return sqlite3ApiExit(0, rc);
132062
+ return rc & 0xff;
131190132063
}
131191132064
#endif /* SQLITE_OMIT_UTF16 */
131192132065
131193132066
/*
131194132067
** Register a new collation sequence with the database handle db.
@@ -131556,11 +132429,13 @@
131556132429
/*
131557132430
** Interface to the testing logic.
131558132431
*/
131559132432
SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
131560132433
int rc = 0;
131561
-#ifndef SQLITE_OMIT_BUILTIN_TEST
132434
+#ifdef SQLITE_OMIT_BUILTIN_TEST
132435
+ UNUSED_PARAMETER(op);
132436
+#else
131562132437
va_list ap;
131563132438
va_start(ap, op);
131564132439
switch( op ){
131565132440
131566132441
/*
@@ -154904,11 +155779,10 @@
154904155779
while( zPattern[iPattern]!=0 ){
154905155780
154906155781
/* Read (and consume) the next character from the input pattern. */
154907155782
UChar32 uPattern;
154908155783
U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
154909
- assert(uPattern!=0);
154910155784
154911155785
/* There are now 4 possibilities:
154912155786
**
154913155787
** 1. uPattern is an unescaped match-all character "%",
154914155788
** 2. uPattern is an unescaped match-one character "_",
@@ -155243,10 +156117,11 @@
155243156117
const char *zName; /* SQL Collation sequence name (eg. "japanese") */
155244156118
UCollator *pUCollator; /* ICU library collation object */
155245156119
int rc; /* Return code from sqlite3_create_collation_x() */
155246156120
155247156121
assert(nArg==2);
156122
+ (void)nArg; /* Unused parameter */
155248156123
zLocale = (const char *)sqlite3_value_text(apArg[0]);
155249156124
zName = (const char *)sqlite3_value_text(apArg[1]);
155250156125
155251156126
if( !zLocale || !zName ){
155252156127
return;
@@ -155566,16 +156441,17 @@
155566156441
155567156442
/*
155568156443
** The set of routines that implement the simple tokenizer
155569156444
*/
155570156445
static const sqlite3_tokenizer_module icuTokenizerModule = {
155571
- 0, /* iVersion */
155572
- icuCreate, /* xCreate */
155573
- icuDestroy, /* xCreate */
155574
- icuOpen, /* xOpen */
155575
- icuClose, /* xClose */
155576
- icuNext, /* xNext */
156446
+ 0, /* iVersion */
156447
+ icuCreate, /* xCreate */
156448
+ icuDestroy, /* xCreate */
156449
+ icuOpen, /* xOpen */
156450
+ icuClose, /* xClose */
156451
+ icuNext, /* xNext */
156452
+ 0, /* xLanguageid */
155577156453
};
155578156454
155579156455
/*
155580156456
** Set *ppModule to point at the implementation of the ICU tokenizer.
155581156457
*/
@@ -159131,12 +160007,12 @@
159131160007
static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
159132160008
ota_file *p = (ota_file *)pFile;
159133160009
int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
159134160010
int rc;
159135160011
159136
- assert( p->openFlags &
159137
- (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB|SQLITE_OPEN_TRANSIENT_DB)
160012
+ assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160013
+ || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
159138160014
);
159139160015
if( op==SQLITE_FCNTL_OTA ){
159140160016
sqlite3ota *pOta = (sqlite3ota*)pArg;
159141160017
159142160018
/* First try to find another OTA vfs lower down in the vfs stack. If
159143160019
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -155,10 +155,17 @@
155 # ifndef _FILE_OFFSET_BITS
156 # define _FILE_OFFSET_BITS 64
157 # endif
158 # define _LARGEFILE_SOURCE 1
159 #endif
 
 
 
 
 
 
 
160
161 /* Needed for various definitions... */
162 #if defined(__GNUC__) && !defined(_GNU_SOURCE)
163 # define _GNU_SOURCE
164 #endif
@@ -228,11 +235,11 @@
228 ** to experimental interfaces but reserve the right to make minor changes
229 ** if experience from use "in the wild" suggest such changes are prudent.
230 **
231 ** The official C-language API documentation for SQLite is derived
232 ** from comments in this file. This file is the authoritative source
233 ** on how SQLite interfaces are suppose to operate.
234 **
235 ** The name of this file under configuration management is "sqlite.h.in".
236 ** The makefile makes some minor changes to this file (such as inserting
237 ** the version number) and changes its name to "sqlite3.h" as
238 ** part of the build process.
@@ -318,11 +325,11 @@
318 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
319 ** [sqlite_version()] and [sqlite_source_id()].
320 */
321 #define SQLITE_VERSION "3.8.11"
322 #define SQLITE_VERSION_NUMBER 3008011
323 #define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417"
324
325 /*
326 ** CAPI3REF: Run-Time Library Version Numbers
327 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
328 **
@@ -1161,11 +1168,11 @@
1161 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1162 ** opcode causes the xFileControl method to swap the file handle with the one
1163 ** pointed to by the pArg argument. This capability is used during testing
1164 ** and only needs to be supported when SQLITE_TEST is defined.
1165 **
1166 * <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1167 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1168 ** be advantageous to block on the next WAL lock if the lock is not immediately
1169 ** available. The WAL subsystem issues this signal during rare
1170 ** circumstances in order to fix a problem with priority inversion.
1171 ** Applications should <em>not</em> use this file-control.
@@ -9689,17 +9696,18 @@
9689 u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */
9690 u8 p5; /* Fifth parameter is an unsigned character */
9691 int p1; /* First operand */
9692 int p2; /* Second parameter (often the jump destination) */
9693 int p3; /* The third parameter */
9694 union { /* fourth parameter */
9695 int i; /* Integer value if p4type==P4_INT32 */
9696 void *p; /* Generic pointer */
9697 char *z; /* Pointer to data for string (char array) types */
9698 i64 *pI64; /* Used when p4type is P4_INT64 */
9699 double *pReal; /* Used when p4type is P4_REAL */
9700 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
 
9701 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
9702 Mem *pMem; /* Used when p4type is P4_MEM */
9703 VTable *pVtab; /* Used when p4type is P4_VTAB */
9704 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
9705 int *ai; /* Used when p4type is P4_INTARRAY */
@@ -9762,10 +9770,11 @@
9762 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
9763 #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
9764 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
9765 #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
9766 #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
 
9767
9768 /* Error message codes for OP_Halt */
9769 #define P5_ConstraintNotNull 1
9770 #define P5_ConstraintUnique 2
9771 #define P5_ConstraintCheck 3
@@ -9804,46 +9813,46 @@
9804 */
9805 /************** Include opcodes.h in the middle of vdbe.h ********************/
9806 /************** Begin file opcodes.h *****************************************/
9807 /* Automatically generated. Do not edit */
9808 /* See the mkopcodeh.awk script for details */
9809 #define OP_Function 1 /* synopsis: r[P3]=func(r[P2@P5]) */
9810 #define OP_Savepoint 2
9811 #define OP_AutoCommit 3
9812 #define OP_Transaction 4
9813 #define OP_SorterNext 5
9814 #define OP_PrevIfOpen 6
9815 #define OP_NextIfOpen 7
9816 #define OP_Prev 8
9817 #define OP_Next 9
9818 #define OP_AggStep 10 /* synopsis: accum=r[P3] step(r[P2@P5]) */
9819 #define OP_Checkpoint 11
9820 #define OP_JournalMode 12
9821 #define OP_Vacuum 13
9822 #define OP_VFilter 14 /* synopsis: iplan=r[P3] zplan='P4' */
9823 #define OP_VUpdate 15 /* synopsis: data=r[P3@P2] */
9824 #define OP_Goto 16
9825 #define OP_Gosub 17
9826 #define OP_Return 18
9827 #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
9828 #define OP_InitCoroutine 20
9829 #define OP_EndCoroutine 21
9830 #define OP_Yield 22
9831 #define OP_HaltIfNull 23 /* synopsis: if r[P3]=null halt */
9832 #define OP_Halt 24
9833 #define OP_Integer 25 /* synopsis: r[P2]=P1 */
9834 #define OP_Int64 26 /* synopsis: r[P2]=P4 */
9835 #define OP_String 27 /* synopsis: r[P2]='P4' (len=P1) */
9836 #define OP_Null 28 /* synopsis: r[P2..P3]=NULL */
9837 #define OP_SoftNull 29 /* synopsis: r[P1]=NULL */
9838 #define OP_Blob 30 /* synopsis: r[P2]=P4 (len=P1) */
9839 #define OP_Variable 31 /* synopsis: r[P2]=parameter(P1,P4) */
9840 #define OP_Move 32 /* synopsis: r[P2@P3]=r[P1@P3] */
9841 #define OP_Copy 33 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
9842 #define OP_SCopy 34 /* synopsis: r[P2]=r[P1] */
9843 #define OP_ResultRow 35 /* synopsis: output=r[P1@P2] */
9844 #define OP_CollSeq 36
9845 #define OP_AddImm 37 /* synopsis: r[P1]=r[P1]+P2 */
9846 #define OP_MustBeInt 38
9847 #define OP_RealAffinity 39
9848 #define OP_Cast 40 /* synopsis: affinity(r[P1]) */
9849 #define OP_Permutation 41
@@ -9865,33 +9874,33 @@
9865 #define OP_OpenEphemeral 57 /* synopsis: nColumn=P2 */
9866 #define OP_SorterOpen 58
9867 #define OP_SequenceTest 59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
9868 #define OP_OpenPseudo 60 /* synopsis: P3 columns in r[P2] */
9869 #define OP_Close 61
9870 #define OP_SeekLT 62 /* synopsis: key=r[P3@P4] */
9871 #define OP_SeekLE 63 /* synopsis: key=r[P3@P4] */
9872 #define OP_SeekGE 64 /* synopsis: key=r[P3@P4] */
9873 #define OP_SeekGT 65 /* synopsis: key=r[P3@P4] */
9874 #define OP_Seek 66 /* synopsis: intkey=r[P2] */
9875 #define OP_NoConflict 67 /* synopsis: key=r[P3@P4] */
9876 #define OP_NotFound 68 /* synopsis: key=r[P3@P4] */
9877 #define OP_Found 69 /* synopsis: key=r[P3@P4] */
9878 #define OP_NotExists 70 /* synopsis: intkey=r[P3] */
9879 #define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
9880 #define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9881 #define OP_Sequence 73 /* synopsis: r[P2]=cursor[P1].ctr++ */
9882 #define OP_NewRowid 74 /* synopsis: r[P2]=rowid */
9883 #define OP_Insert 75 /* synopsis: intkey=r[P3] data=r[P2] */
9884 #define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
9885 #define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9886 #define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9887 #define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9888 #define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9889 #define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9890 #define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
9891 #define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9892 #define OP_InsertInt 84 /* synopsis: intkey=P3 data=r[P2] */
9893 #define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9894 #define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9895 #define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9896 #define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9897 #define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -9898,73 +9907,76 @@
9898 #define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9899 #define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9900 #define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9901 #define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
9902 #define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9903 #define OP_Delete 95
9904 #define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9905 #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
9906 #define OP_ResetCount 98
9907 #define OP_SorterCompare 99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
9908 #define OP_SorterData 100 /* synopsis: r[P2]=data */
9909 #define OP_RowKey 101 /* synopsis: r[P2]=key */
9910 #define OP_RowData 102 /* synopsis: r[P2]=data */
9911 #define OP_Rowid 103 /* synopsis: r[P2]=rowid */
9912 #define OP_NullRow 104
9913 #define OP_Last 105
9914 #define OP_SorterSort 106
9915 #define OP_Sort 107
9916 #define OP_Rewind 108
9917 #define OP_SorterInsert 109
9918 #define OP_IdxInsert 110 /* synopsis: key=r[P2] */
9919 #define OP_IdxDelete 111 /* synopsis: key=r[P2@P3] */
9920 #define OP_IdxRowid 112 /* synopsis: r[P2]=rowid */
9921 #define OP_IdxLE 113 /* synopsis: key=r[P3@P4] */
9922 #define OP_IdxGT 114 /* synopsis: key=r[P3@P4] */
9923 #define OP_IdxLT 115 /* synopsis: key=r[P3@P4] */
9924 #define OP_IdxGE 116 /* synopsis: key=r[P3@P4] */
9925 #define OP_Destroy 117
9926 #define OP_Clear 118
9927 #define OP_ResetSorter 119
9928 #define OP_CreateIndex 120 /* synopsis: r[P2]=root iDb=P1 */
9929 #define OP_CreateTable 121 /* synopsis: r[P2]=root iDb=P1 */
9930 #define OP_ParseSchema 122
9931 #define OP_LoadAnalysis 123
9932 #define OP_DropTable 124
9933 #define OP_DropIndex 125
9934 #define OP_DropTrigger 126
9935 #define OP_IntegrityCk 127
9936 #define OP_RowSetAdd 128 /* synopsis: rowset(P1)=r[P2] */
9937 #define OP_RowSetRead 129 /* synopsis: r[P3]=rowset(P1) */
9938 #define OP_RowSetTest 130 /* synopsis: if r[P3] in rowset(P1) goto P2 */
9939 #define OP_Program 131
9940 #define OP_Param 132
9941 #define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
9942 #define OP_FkCounter 134 /* synopsis: fkctr[P1]+=P2 */
9943 #define OP_FkIfZero 135 /* synopsis: if fkctr[P1]==0 goto P2 */
9944 #define OP_MemMax 136 /* synopsis: r[P1]=max(r[P1],r[P2]) */
9945 #define OP_IfPos 137 /* synopsis: if r[P1]>0 goto P2 */
9946 #define OP_IfNeg 138 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */
9947 #define OP_IfNotZero 139 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */
9948 #define OP_DecrJumpZero 140 /* synopsis: if (--r[P1])==0 goto P2 */
9949 #define OP_JumpZeroIncr 141 /* synopsis: if (r[P1]++)==0 ) goto P2 */
9950 #define OP_AggFinal 142 /* synopsis: accum=r[P1] N=P2 */
9951 #define OP_IncrVacuum 143
9952 #define OP_Expire 144
9953 #define OP_TableLock 145 /* synopsis: iDb=P1 root=P2 write=P3 */
9954 #define OP_VBegin 146
9955 #define OP_VCreate 147
9956 #define OP_VDestroy 148
9957 #define OP_VOpen 149
9958 #define OP_VColumn 150 /* synopsis: r[P3]=vcolumn(P2) */
9959 #define OP_VNext 151
9960 #define OP_VRename 152
9961 #define OP_Pagecount 153
9962 #define OP_MaxPgcnt 154
9963 #define OP_Init 155 /* synopsis: Start at P2 */
9964 #define OP_Noop 156
9965 #define OP_Explain 157
 
 
 
9966
9967
9968 /* Properties such as "out2" or "jump" that are specified in
9969 ** comments following the "case" for each opcode in the vdbe.c
9970 ** are encoded into bitvectors as follows:
@@ -9974,30 +9986,31 @@
9974 #define OPFLG_IN2 0x0004 /* in2: P2 is an input */
9975 #define OPFLG_IN3 0x0008 /* in3: P3 is an input */
9976 #define OPFLG_OUT2 0x0010 /* out2: P2 is an output */
9977 #define OPFLG_OUT3 0x0020 /* out3: P3 is an output */
9978 #define OPFLG_INITIALIZER {\
9979 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
9980 /* 8 */ 0x01, 0x01, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
9981 /* 16 */ 0x01, 0x01, 0x02, 0x12, 0x01, 0x02, 0x03, 0x08,\
9982 /* 24 */ 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10,\
9983 /* 32 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x03, 0x02,\
9984 /* 40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
9985 /* 48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\
9986 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09,\
9987 /* 64 */ 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x26,\
9988 /* 72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
9989 /* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
9990 /* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
9991 /* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
9992 /* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
9993 /* 112 */ 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00, 0x00,\
9994 /* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
9995 /* 128 */ 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00, 0x01,\
9996 /* 136 */ 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x01,\
9997 /* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\
9998 /* 152 */ 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,}
 
9999
10000 /************** End of opcodes.h *********************************************/
10001 /************** Continuing where we left off in vdbe.h ***********************/
10002
10003 /*
@@ -10008,10 +10021,11 @@
10008 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
10009 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
10010 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
10011 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
10012 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
 
10013 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
10014 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
10015 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
10016 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
10017 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
@@ -10400,18 +10414,18 @@
10400 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
10401 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
10402 };
10403
10404 /* Bit values for PgHdr.flags */
10405 #define PGHDR_DIRTY 0x002 /* Page has changed */
10406 #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
10407 ** writing this page to the database */
10408 #define PGHDR_NEED_READ 0x008 /* Content is unread */
10409 #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
10410 #define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
10411
10412 #define PGHDR_MMAP 0x040 /* This is an mmap page object */
10413
10414 /* Initialize and shutdown the page cache subsystem */
10415 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
10416 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
10417
@@ -11439,13 +11453,13 @@
11439 ** But rather than start with 0 or 1, we begin with 'A'. That way,
11440 ** when multiple affinity types are concatenated into a string and
11441 ** used as the P4 operand, they will be more readable.
11442 **
11443 ** Note also that the numeric types are grouped together so that testing
11444 ** for a numeric type is a single comparison. And the NONE type is first.
11445 */
11446 #define SQLITE_AFF_NONE 'A'
11447 #define SQLITE_AFF_TEXT 'B'
11448 #define SQLITE_AFF_NUMERIC 'C'
11449 #define SQLITE_AFF_INTEGER 'D'
11450 #define SQLITE_AFF_REAL 'E'
11451
@@ -12198,11 +12212,11 @@
12198 #endif
12199 int iCursor; /* The VDBE cursor number used to access this table */
12200 Expr *pOn; /* The ON clause of a join */
12201 IdList *pUsing; /* The USING clause of a join */
12202 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
12203 char *zIndex; /* Identifier from "INDEXED BY <zIndex>" clause */
12204 Index *pIndex; /* Index structure corresponding to zIndex, if any */
12205 } a[1]; /* One entry for each identifier on the list */
12206 };
12207
12208 /*
@@ -13004,11 +13018,13 @@
13004 # define sqlite3Isalpha(x) isalpha((unsigned char)(x))
13005 # define sqlite3Isdigit(x) isdigit((unsigned char)(x))
13006 # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
13007 # define sqlite3Tolower(x) tolower((unsigned char)(x))
13008 #endif
 
13009 SQLITE_PRIVATE int sqlite3IsIdChar(u8);
 
13010
13011 /*
13012 ** Internal function prototypes
13013 */
13014 #define sqlite3StrICmp sqlite3_stricmp
@@ -13032,11 +13048,13 @@
13032 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
13033 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
13034 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
13035 SQLITE_PRIVATE void sqlite3PageFree(void*);
13036 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
 
13037 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
 
13038 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
13039
13040 /*
13041 ** On systems with ample stack space and that support alloca(), make
13042 ** use of alloca() to obtain space for large automatic objects. By default,
@@ -13108,14 +13126,10 @@
13108 #if defined(SQLITE_TEST)
13109 SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
13110 #endif
13111
13112 #if defined(SQLITE_DEBUG)
13113 SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView*,u8);
13114 SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView*);
13115 SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView*, const char*, ...);
13116 SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView*, const char*, u8);
13117 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
13118 SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
13119 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
13120 #endif
13121
@@ -13176,15 +13190,18 @@
13176 SQLITE_PRIVATE int sqlite3FaultSim(int);
13177 #endif
13178
13179 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
13180 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
 
13181 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
13182 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
13183 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
13184 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
 
13185 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
 
13186
13187 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
13188 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
13189 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
13190 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
@@ -13268,10 +13285,11 @@
13268 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
13269 #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
13270 #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
13271 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
13272 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
 
13273 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
13274 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
13275 SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
13276 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
13277 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
@@ -13284,12 +13302,14 @@
13284 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
13285 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
13286 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
13287 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
13288 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 
13289 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
13290 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
 
13291 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
13292 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
13293 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
13294 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
13295 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
@@ -13503,10 +13523,11 @@
13503 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
13504 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
13505 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
13506 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
13507 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 
13508 SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
13509 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
13510 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
13511 SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
13512 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
@@ -14620,10 +14641,13 @@
14620 Pgno pgnoRoot; /* Root page of the open btree cursor */
14621 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
14622 i64 seqCount; /* Sequence counter */
14623 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
14624 VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
 
 
 
14625
14626 /* Cached information about the header for the data record that the
14627 ** cursor is currently pointing to. Only valid if cacheStatus matches
14628 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
14629 ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
@@ -14813,18 +14837,20 @@
14813 **
14814 ** This structure is defined inside of vdbeInt.h because it uses substructures
14815 ** (Mem) which are only defined there.
14816 */
14817 struct sqlite3_context {
14818 Mem *pOut; /* The return value is stored here */
14819 FuncDef *pFunc; /* Pointer to function information */
14820 Mem *pMem; /* Memory cell used to store aggregate context */
14821 Vdbe *pVdbe; /* The VM that owns this context */
14822 int iOp; /* Instruction number of OP_Function */
14823 int isError; /* Error code returned by the function. */
14824 u8 skipFlag; /* Skip accumulator loading if true */
14825 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
 
 
14826 };
14827
14828 /*
14829 ** An Explain object accumulates indented output which is helpful
14830 ** in describing recursive data structures.
@@ -19193,13 +19219,18 @@
19193 if( sqlite3GlobalConfig.bCoreMutex ){
19194 pFrom = sqlite3DefaultMutex();
19195 }else{
19196 pFrom = sqlite3NoopMutex();
19197 }
19198 memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
19199 memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
19200 sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
 
 
 
 
 
19201 pTo->xMutexAlloc = pFrom->xMutexAlloc;
19202 }
19203 rc = sqlite3GlobalConfig.mutex.xMutexInit();
19204
19205 #ifdef SQLITE_DEBUG
@@ -21353,21 +21384,20 @@
21353 **
21354 ** The returned value is normally a copy of the second argument to this
21355 ** function. However, if a malloc() failure has occurred since the previous
21356 ** invocation SQLITE_NOMEM is returned instead.
21357 **
21358 ** If the first argument, db, is not NULL and a malloc() error has occurred,
21359 ** then the connection error-code (the value returned by sqlite3_errcode())
21360 ** is set to SQLITE_NOMEM.
21361 */
21362 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
21363 /* If the db handle is not NULL, then we must hold the connection handle
21364 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
21365 ** is unsafe, as is the call to sqlite3Error().
21366 */
21367 assert( !db || sqlite3_mutex_held(db->mutex) );
21368 if( db==0 ) return rc & 0xff;
21369 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
21370 return apiOomError(db);
21371 }
21372 return rc & db->errMask;
21373 }
@@ -21374,21 +21404,18 @@
21374
21375 /************** End of malloc.c **********************************************/
21376 /************** Begin file printf.c ******************************************/
21377 /*
21378 ** The "printf" code that follows dates from the 1980's. It is in
21379 ** the public domain. The original comments are included here for
21380 ** completeness. They are very out-of-date but might be useful as
21381 ** an historical reference. Most of the "enhancements" have been backed
21382 ** out so that the functionality is now the same as standard printf().
21383 **
21384 **************************************************************************
21385 **
21386 ** This file contains code for a set of "printf"-like routines. These
21387 ** routines format strings much like the printf() from the standard C
21388 ** library, though the implementation here has enhancements to support
21389 ** SQLlite.
21390 */
21391
21392 /*
21393 ** Conversion types fall into various categories as defined by the
21394 ** following enumeration.
@@ -22431,26 +22458,49 @@
22431 fprintf(stdout,"%s", zBuf);
22432 fflush(stdout);
22433 }
22434 #endif
22435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22436 #ifdef SQLITE_DEBUG
22437 /*************************************************************************
22438 ** Routines for implementing the "TreeView" display of hierarchical
22439 ** data structures for debugging.
22440 **
22441 ** The main entry points (coded elsewhere) are:
22442 ** sqlite3TreeViewExpr(0, pExpr, 0);
22443 ** sqlite3TreeViewExprList(0, pList, 0, 0);
22444 ** sqlite3TreeViewSelect(0, pSelect, 0);
22445 ** Insert calls to those routines while debugging in order to display
22446 ** a diagram of Expr, ExprList, and Select objects.
22447 **
22448 */
22449 /* Add a new subitem to the tree. The moreToFollow flag indicates that this
22450 ** is not the last item in the tree. */
22451 SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
22452 if( p==0 ){
22453 p = sqlite3_malloc64( sizeof(*p) );
22454 if( p==0 ) return 0;
22455 memset(p, 0, sizeof(*p));
22456 }else{
@@ -22458,19 +22508,25 @@
22458 }
22459 assert( moreToFollow==0 || moreToFollow==1 );
22460 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
22461 return p;
22462 }
22463 /* Finished with one layer of the tree */
22464 SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView *p){
 
 
 
22465 if( p==0 ) return;
22466 p->iLevel--;
22467 if( p->iLevel<0 ) sqlite3_free(p);
22468 }
22469 /* Generate a single line of output for the tree, with a prefix that contains
22470 ** all the appropriate tree lines */
22471 SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
 
 
 
22472 va_list ap;
22473 int i;
22474 StrAccum acc;
22475 char zBuf[500];
22476 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
@@ -22486,28 +22542,371 @@
22486 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
22487 sqlite3StrAccumFinish(&acc);
22488 fprintf(stdout,"%s", zBuf);
22489 fflush(stdout);
22490 }
22491 /* Shorthand for starting a new tree item that consists of a single label */
22492 SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
22493 p = sqlite3TreeViewPush(p, moreToFollow);
 
 
 
22494 sqlite3TreeViewLine(p, "%s", zLabel);
22495 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22496 #endif /* SQLITE_DEBUG */
22497
22498 /*
22499 ** variable-argument wrapper around sqlite3VXPrintf().
22500 */
22501 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
22502 va_list ap;
22503 va_start(ap,zFormat);
22504 sqlite3VXPrintf(p, bFlags, zFormat, ap);
22505 va_end(ap);
22506 }
22507
22508 /************** End of printf.c **********************************************/
22509 /************** Begin file random.c ******************************************/
22510 /*
22511 ** 2001 September 15
22512 **
22513 ** The author disclaims copyright to this source code. In place of
@@ -23544,14 +23943,12 @@
23544 ** The value returned will never be negative. Nor will it ever be greater
23545 ** than the actual length of the string. For very long strings (greater
23546 ** than 1GiB) the value returned might be less than the true string length.
23547 */
23548 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
23549 const char *z2 = z;
23550 if( z==0 ) return 0;
23551 while( *z2 ){ z2++; }
23552 return 0x3fffffff & (int)(z2 - z);
23553 }
23554
23555 /*
23556 ** Set the current error code to err_code and clear any prior error message.
23557 */
@@ -24519,18 +24916,35 @@
24519
24520 /*
24521 ** Read or write a four-byte big-endian integer value.
24522 */
24523 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
 
 
 
 
 
 
 
 
 
24524 testcase( p[0]&0x80 );
24525 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
 
24526 }
24527 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
 
 
 
 
 
 
24528 p[0] = (u8)(v>>24);
24529 p[1] = (u8)(v>>16);
24530 p[2] = (u8)(v>>8);
24531 p[3] = (u8)v;
 
24532 }
24533
24534
24535
24536 /*
@@ -25094,46 +25508,46 @@
25094 #else
25095 # define OpHelp(X)
25096 #endif
25097 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
25098 static const char *const azName[] = { "?",
25099 /* 1 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
25100 /* 2 */ "Savepoint" OpHelp(""),
25101 /* 3 */ "AutoCommit" OpHelp(""),
25102 /* 4 */ "Transaction" OpHelp(""),
25103 /* 5 */ "SorterNext" OpHelp(""),
25104 /* 6 */ "PrevIfOpen" OpHelp(""),
25105 /* 7 */ "NextIfOpen" OpHelp(""),
25106 /* 8 */ "Prev" OpHelp(""),
25107 /* 9 */ "Next" OpHelp(""),
25108 /* 10 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
25109 /* 11 */ "Checkpoint" OpHelp(""),
25110 /* 12 */ "JournalMode" OpHelp(""),
25111 /* 13 */ "Vacuum" OpHelp(""),
25112 /* 14 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
25113 /* 15 */ "VUpdate" OpHelp("data=r[P3@P2]"),
25114 /* 16 */ "Goto" OpHelp(""),
25115 /* 17 */ "Gosub" OpHelp(""),
25116 /* 18 */ "Return" OpHelp(""),
25117 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
25118 /* 20 */ "InitCoroutine" OpHelp(""),
25119 /* 21 */ "EndCoroutine" OpHelp(""),
25120 /* 22 */ "Yield" OpHelp(""),
25121 /* 23 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
25122 /* 24 */ "Halt" OpHelp(""),
25123 /* 25 */ "Integer" OpHelp("r[P2]=P1"),
25124 /* 26 */ "Int64" OpHelp("r[P2]=P4"),
25125 /* 27 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
25126 /* 28 */ "Null" OpHelp("r[P2..P3]=NULL"),
25127 /* 29 */ "SoftNull" OpHelp("r[P1]=NULL"),
25128 /* 30 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
25129 /* 31 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
25130 /* 32 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
25131 /* 33 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
25132 /* 34 */ "SCopy" OpHelp("r[P2]=r[P1]"),
25133 /* 35 */ "ResultRow" OpHelp("output=r[P1@P2]"),
25134 /* 36 */ "CollSeq" OpHelp(""),
25135 /* 37 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
25136 /* 38 */ "MustBeInt" OpHelp(""),
25137 /* 39 */ "RealAffinity" OpHelp(""),
25138 /* 40 */ "Cast" OpHelp("affinity(r[P1])"),
25139 /* 41 */ "Permutation" OpHelp(""),
@@ -25155,33 +25569,33 @@
25155 /* 57 */ "OpenEphemeral" OpHelp("nColumn=P2"),
25156 /* 58 */ "SorterOpen" OpHelp(""),
25157 /* 59 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
25158 /* 60 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
25159 /* 61 */ "Close" OpHelp(""),
25160 /* 62 */ "SeekLT" OpHelp("key=r[P3@P4]"),
25161 /* 63 */ "SeekLE" OpHelp("key=r[P3@P4]"),
25162 /* 64 */ "SeekGE" OpHelp("key=r[P3@P4]"),
25163 /* 65 */ "SeekGT" OpHelp("key=r[P3@P4]"),
25164 /* 66 */ "Seek" OpHelp("intkey=r[P2]"),
25165 /* 67 */ "NoConflict" OpHelp("key=r[P3@P4]"),
25166 /* 68 */ "NotFound" OpHelp("key=r[P3@P4]"),
25167 /* 69 */ "Found" OpHelp("key=r[P3@P4]"),
25168 /* 70 */ "NotExists" OpHelp("intkey=r[P3]"),
25169 /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
25170 /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
25171 /* 73 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
25172 /* 74 */ "NewRowid" OpHelp("r[P2]=rowid"),
25173 /* 75 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
25174 /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
25175 /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
25176 /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
25177 /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
25178 /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
25179 /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
25180 /* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
25181 /* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
25182 /* 84 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
25183 /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
25184 /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
25185 /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
25186 /* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
25187 /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -25188,73 +25602,76 @@
25188 /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
25189 /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
25190 /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
25191 /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
25192 /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
25193 /* 95 */ "Delete" OpHelp(""),
25194 /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
25195 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
25196 /* 98 */ "ResetCount" OpHelp(""),
25197 /* 99 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
25198 /* 100 */ "SorterData" OpHelp("r[P2]=data"),
25199 /* 101 */ "RowKey" OpHelp("r[P2]=key"),
25200 /* 102 */ "RowData" OpHelp("r[P2]=data"),
25201 /* 103 */ "Rowid" OpHelp("r[P2]=rowid"),
25202 /* 104 */ "NullRow" OpHelp(""),
25203 /* 105 */ "Last" OpHelp(""),
25204 /* 106 */ "SorterSort" OpHelp(""),
25205 /* 107 */ "Sort" OpHelp(""),
25206 /* 108 */ "Rewind" OpHelp(""),
25207 /* 109 */ "SorterInsert" OpHelp(""),
25208 /* 110 */ "IdxInsert" OpHelp("key=r[P2]"),
25209 /* 111 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
25210 /* 112 */ "IdxRowid" OpHelp("r[P2]=rowid"),
25211 /* 113 */ "IdxLE" OpHelp("key=r[P3@P4]"),
25212 /* 114 */ "IdxGT" OpHelp("key=r[P3@P4]"),
25213 /* 115 */ "IdxLT" OpHelp("key=r[P3@P4]"),
25214 /* 116 */ "IdxGE" OpHelp("key=r[P3@P4]"),
25215 /* 117 */ "Destroy" OpHelp(""),
25216 /* 118 */ "Clear" OpHelp(""),
25217 /* 119 */ "ResetSorter" OpHelp(""),
25218 /* 120 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
25219 /* 121 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
25220 /* 122 */ "ParseSchema" OpHelp(""),
25221 /* 123 */ "LoadAnalysis" OpHelp(""),
25222 /* 124 */ "DropTable" OpHelp(""),
25223 /* 125 */ "DropIndex" OpHelp(""),
25224 /* 126 */ "DropTrigger" OpHelp(""),
25225 /* 127 */ "IntegrityCk" OpHelp(""),
25226 /* 128 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
25227 /* 129 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
25228 /* 130 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
25229 /* 131 */ "Program" OpHelp(""),
25230 /* 132 */ "Param" OpHelp(""),
25231 /* 133 */ "Real" OpHelp("r[P2]=P4"),
25232 /* 134 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
25233 /* 135 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
25234 /* 136 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
25235 /* 137 */ "IfPos" OpHelp("if r[P1]>0 goto P2"),
25236 /* 138 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
25237 /* 139 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"),
25238 /* 140 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
25239 /* 141 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"),
25240 /* 142 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
25241 /* 143 */ "IncrVacuum" OpHelp(""),
25242 /* 144 */ "Expire" OpHelp(""),
25243 /* 145 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
25244 /* 146 */ "VBegin" OpHelp(""),
25245 /* 147 */ "VCreate" OpHelp(""),
25246 /* 148 */ "VDestroy" OpHelp(""),
25247 /* 149 */ "VOpen" OpHelp(""),
25248 /* 150 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
25249 /* 151 */ "VNext" OpHelp(""),
25250 /* 152 */ "VRename" OpHelp(""),
25251 /* 153 */ "Pagecount" OpHelp(""),
25252 /* 154 */ "MaxPgcnt" OpHelp(""),
25253 /* 155 */ "Init" OpHelp("Start at P2"),
25254 /* 156 */ "Noop" OpHelp(""),
25255 /* 157 */ "Explain" OpHelp(""),
 
 
 
25256 };
25257 return azName[i];
25258 }
25259 #endif
25260
@@ -38989,14 +39406,14 @@
38989 /*
38990 ** Check to see if the i-th bit is set. Return true or false.
38991 ** If p is NULL (if the bitmap has not been created) or if
38992 ** i is out of range, then return false.
38993 */
38994 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
38995 if( p==0 ) return 0;
38996 if( i>p->iSize || i==0 ) return 0;
38997 i--;
 
38998 while( p->iDivisor ){
38999 u32 bin = i/p->iDivisor;
39000 i = i%p->iDivisor;
39001 p = p->u.apSub[bin];
39002 if (!p) {
@@ -39011,10 +39428,13 @@
39011 if( p->u.aHash[h]==i ) return 1;
39012 h = (h+1) % BITVEC_NINT;
39013 }
39014 return 0;
39015 }
 
 
 
39016 }
39017
39018 /*
39019 ** Set the i-th bit. Return 0 on success and an error code if
39020 ** anything goes wrong.
@@ -39300,11 +39720,10 @@
39300 u8 bPurgeable; /* True if pages are on backing store */
39301 u8 eCreate; /* eCreate value for for xFetch() */
39302 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
39303 void *pStress; /* Argument to xStress */
39304 sqlite3_pcache *pCache; /* Pluggable cache module */
39305 PgHdr *pPage1; /* Reference to page 1 */
39306 };
39307
39308 /********************************** Linked List Management ********************/
39309
39310 /* Allowed values for second argument to pcacheManageDirtyList() */
@@ -39378,13 +39797,10 @@
39378 ** Wrapper around the pluggable caches xUnpin method. If the cache is
39379 ** being used for an in-memory database, this function is a no-op.
39380 */
39381 static void pcacheUnpin(PgHdr *p){
39382 if( p->pCache->bPurgeable ){
39383 if( p->pgno==1 ){
39384 p->pCache->pPage1 = 0;
39385 }
39386 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
39387 }
39388 }
39389
39390 /*
@@ -39473,11 +39889,10 @@
39473 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
39474 if( pCache->pCache ){
39475 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
39476 }
39477 pCache->pCache = pNew;
39478 pCache->pPage1 = 0;
39479 pCache->szPage = szPage;
39480 }
39481 return SQLITE_OK;
39482 }
39483
@@ -39598,17 +40013,18 @@
39598 ){
39599 PgHdr *pPgHdr;
39600 assert( pPage!=0 );
39601 pPgHdr = (PgHdr*)pPage->pExtra;
39602 assert( pPgHdr->pPage==0 );
39603 memset(pPgHdr, 0, sizeof(PgHdr));
39604 pPgHdr->pPage = pPage;
39605 pPgHdr->pData = pPage->pBuf;
39606 pPgHdr->pExtra = (void *)&pPgHdr[1];
39607 memset(pPgHdr->pExtra, 0, pCache->szExtra);
39608 pPgHdr->pCache = pCache;
39609 pPgHdr->pgno = pgno;
 
39610 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
39611 }
39612
39613 /*
39614 ** This routine converts the sqlite3_pcache_page object returned by
@@ -39621,23 +40037,20 @@
39621 Pgno pgno, /* Page number obtained */
39622 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
39623 ){
39624 PgHdr *pPgHdr;
39625
39626 if( pPage==0 ) return 0;
39627 pPgHdr = (PgHdr *)pPage->pExtra;
39628
39629 if( !pPgHdr->pPage ){
39630 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
39631 }
39632 if( 0==pPgHdr->nRef ){
39633 pCache->nRef++;
39634 }
39635 pPgHdr->nRef++;
39636 if( pgno==1 ){
39637 pCache->pPage1 = pPgHdr;
39638 }
39639 return pPgHdr;
39640 }
39641
39642 /*
39643 ** Decrement the reference count on a page. If the page is clean and the
@@ -39646,11 +40059,11 @@
39646 SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
39647 assert( p->nRef>0 );
39648 p->nRef--;
39649 if( p->nRef==0 ){
39650 p->pCache->nRef--;
39651 if( (p->flags&PGHDR_DIRTY)==0 ){
39652 pcacheUnpin(p);
39653 }else if( p->pDirtyPrev!=0 ){
39654 /* Move the page to the head of the dirty list. */
39655 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
39656 }
@@ -39674,37 +40087,39 @@
39674 assert( p->nRef==1 );
39675 if( p->flags&PGHDR_DIRTY ){
39676 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
39677 }
39678 p->pCache->nRef--;
39679 if( p->pgno==1 ){
39680 p->pCache->pPage1 = 0;
39681 }
39682 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
39683 }
39684
39685 /*
39686 ** Make sure the page is marked as dirty. If it isn't dirty already,
39687 ** make it so.
39688 */
39689 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
39690 p->flags &= ~PGHDR_DONT_WRITE;
39691 assert( p->nRef>0 );
39692 if( 0==(p->flags & PGHDR_DIRTY) ){
39693 p->flags |= PGHDR_DIRTY;
39694 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
 
 
 
 
39695 }
39696 }
39697
39698 /*
39699 ** Make sure the page is marked as clean. If it isn't clean already,
39700 ** make it so.
39701 */
39702 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
39703 if( (p->flags & PGHDR_DIRTY) ){
 
39704 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
39705 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
 
39706 if( p->nRef==0 ){
39707 pcacheUnpin(p);
39708 }
39709 }
39710 }
@@ -39767,13 +40182,18 @@
39767 if( ALWAYS(p->pgno>pgno) ){
39768 assert( p->flags&PGHDR_DIRTY );
39769 sqlite3PcacheMakeClean(p);
39770 }
39771 }
39772 if( pgno==0 && pCache->pPage1 ){
39773 memset(pCache->pPage1->pData, 0, pCache->szPage);
39774 pgno = 1;
 
 
 
 
 
39775 }
39776 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
39777 }
39778 }
39779
@@ -40092,12 +40512,19 @@
40092 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
40093
40094 /*
40095 ** Macros to enter and leave the PCache LRU mutex.
40096 */
40097 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
40098 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
 
 
 
 
 
 
 
40099
40100 /******************************************************************************/
40101 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
40102
40103 /*
@@ -40369,45 +40796,45 @@
40369 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
40370 ** LRU list, then this function is a no-op.
40371 **
40372 ** The PGroup mutex must be held when this function is called.
40373 */
40374 static void pcache1PinPage(PgHdr1 *pPage){
40375 PCache1 *pCache;
40376 PGroup *pGroup;
40377
40378 assert( pPage!=0 );
40379 assert( pPage->isPinned==0 );
40380 pCache = pPage->pCache;
40381 pGroup = pCache->pGroup;
40382 assert( pPage->pLruNext || pPage==pGroup->pLruTail );
40383 assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
40384 assert( sqlite3_mutex_held(pGroup->mutex) );
40385 if( pPage->pLruPrev ){
40386 pPage->pLruPrev->pLruNext = pPage->pLruNext;
40387 }else{
40388 pGroup->pLruHead = pPage->pLruNext;
40389 }
40390 if( pPage->pLruNext ){
40391 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
40392 }else{
40393 pGroup->pLruTail = pPage->pLruPrev;
40394 }
40395 pPage->pLruNext = 0;
40396 pPage->pLruPrev = 0;
40397 pPage->isPinned = 1;
40398 pCache->nRecyclable--;
 
40399 }
40400
40401
40402 /*
40403 ** Remove the page supplied as an argument from the hash table
40404 ** (PCache1.apHash structure) that it is currently stored in.
 
40405 **
40406 ** The PGroup mutex must be held when this function is called.
40407 */
40408 static void pcache1RemoveFromHash(PgHdr1 *pPage){
40409 unsigned int h;
40410 PCache1 *pCache = pPage->pCache;
40411 PgHdr1 **pp;
40412
40413 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
@@ -40414,10 +40841,11 @@
40414 h = pPage->iKey % pCache->nHash;
40415 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
40416 *pp = (*pp)->pNext;
40417
40418 pCache->nPage--;
 
40419 }
40420
40421 /*
40422 ** If there are currently more than nMaxPage pages allocated, try
40423 ** to recycle pages to reduce the number allocated to nMaxPage.
@@ -40427,12 +40855,11 @@
40427 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
40428 PgHdr1 *p = pGroup->pLruTail;
40429 assert( p->pCache->pGroup==pGroup );
40430 assert( p->isPinned==0 );
40431 pcache1PinPage(p);
40432 pcache1RemoveFromHash(p);
40433 pcache1FreePage(p);
40434 }
40435 }
40436
40437 /*
40438 ** Discard all pages from cache pCache with a page number (key value)
@@ -40474,14 +40901,16 @@
40474 */
40475 static int pcache1Init(void *NotUsed){
40476 UNUSED_PARAMETER(NotUsed);
40477 assert( pcache1.isInit==0 );
40478 memset(&pcache1, 0, sizeof(pcache1));
 
40479 if( sqlite3GlobalConfig.bCoreMutex ){
40480 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
40481 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
40482 }
 
40483 pcache1.grp.mxPinned = 10;
40484 pcache1.isInit = 1;
40485 return SQLITE_OK;
40486 }
40487
@@ -40650,11 +41079,11 @@
40650 || pcache1UnderMemoryPressure(pCache)
40651 )){
40652 PCache1 *pOther;
40653 pPage = pGroup->pLruTail;
40654 assert( pPage->isPinned==0 );
40655 pcache1RemoveFromHash(pPage);
40656 pcache1PinPage(pPage);
40657 pOther = pPage->pCache;
40658
40659 /* We want to verify that szPage and szExtra are the same for pOther
40660 ** and pCache. Assert that we can verify this by comparing sums. */
@@ -40673,13 +41102,13 @@
40673
40674 /* Step 5. If a usable page buffer has still not been found,
40675 ** attempt to allocate a new one.
40676 */
40677 if( !pPage ){
40678 if( createFlag==1 ) sqlite3BeginBenignMalloc();
40679 pPage = pcache1AllocPage(pCache);
40680 if( createFlag==1 ) sqlite3EndBenignMalloc();
40681 }
40682
40683 if( pPage ){
40684 unsigned int h = iKey % pCache->nHash;
40685 pCache->nPage++;
@@ -40749,41 +41178,81 @@
40749 ** then attempt to recycle a page from the LRU list. If it is the right
40750 ** size, return the recycled buffer. Otherwise, free the buffer and
40751 ** proceed to step 5.
40752 **
40753 ** 5. Otherwise, allocate and return a new page buffer.
 
 
 
 
 
40754 */
40755 static sqlite3_pcache_page *pcache1Fetch(
40756 sqlite3_pcache *p,
40757 unsigned int iKey,
40758 int createFlag
40759 ){
40760 PCache1 *pCache = (PCache1 *)p;
40761 PgHdr1 *pPage = 0;
40762
40763 assert( offsetof(PgHdr1,page)==0 );
40764 assert( pCache->bPurgeable || createFlag!=1 );
40765 assert( pCache->bPurgeable || pCache->nMin==0 );
40766 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
40767 assert( pCache->nMin==0 || pCache->bPurgeable );
40768 assert( pCache->nHash>0 );
40769 pcache1EnterMutex(pCache->pGroup);
40770
40771 /* Step 1: Search the hash table for an existing entry. */
40772 pPage = pCache->apHash[iKey % pCache->nHash];
40773 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
40774
40775 /* Step 2: Abort if no existing page is found and createFlag is 0 */
40776 if( pPage ){
40777 if( !pPage->isPinned ) pcache1PinPage(pPage);
 
 
 
 
40778 }else if( createFlag ){
40779 /* Steps 3, 4, and 5 implemented by this subroutine */
40780 pPage = pcache1FetchStage2(pCache, iKey, createFlag);
 
 
40781 }
 
 
 
 
 
 
 
 
 
 
 
 
40782 assert( pPage==0 || pCache->iMaxKey>=iKey );
40783 pcache1LeaveMutex(pCache->pGroup);
40784 return (sqlite3_pcache_page*)pPage;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40785 }
40786
40787
40788 /*
40789 ** Implementation of the sqlite3_pcache.xUnpin method.
@@ -40808,12 +41277,11 @@
40808 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
40809 assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
40810 assert( pPage->isPinned==1 );
40811
40812 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
40813 pcache1RemoveFromHash(pPage);
40814 pcache1FreePage(pPage);
40815 }else{
40816 /* Add the page to the PGroup LRU list. */
40817 if( pGroup->pLruHead ){
40818 pGroup->pLruHead->pLruPrev = pPage;
40819 pPage->pLruNext = pGroup->pLruHead;
@@ -40963,12 +41431,11 @@
40963 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
40964 nFree += sqlite3MemSize(p);
40965 #endif
40966 assert( p->isPinned==0 );
40967 pcache1PinPage(p);
40968 pcache1RemoveFromHash(p);
40969 pcache1FreePage(p);
40970 }
40971 pcache1LeaveMutex(&pcache1.grp);
40972 }
40973 return nFree;
40974 }
@@ -42105,13 +42572,13 @@
42105 };
42106
42107 /*
42108 ** Bits of the Pager.doNotSpill flag. See further description below.
42109 */
42110 #define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
42111 #define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
42112 #define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
42113
42114 /*
42115 ** An open page cache is an instance of struct Pager. A description of
42116 ** some of the more important member variables follows:
42117 **
@@ -42189,15 +42656,15 @@
42189 ** comes up during savepoint rollback that requires the pcache module
42190 ** to allocate a new page to prevent the journal file from being written
42191 ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
42192 ** case is a user preference.
42193 **
42194 ** If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress()
42195 ** is permitted, but syncing the journal file is not. This flag is set
42196 ** by sqlite3PagerWrite() when the file-system sector-size is larger than
42197 ** the database page-size in order to prevent a journal sync from happening
42198 ** in between the journalling of two pages on the same sector.
42199 **
42200 ** subjInMemory
42201 **
42202 ** This is a boolean variable. If true, then any required sub-journal
42203 ** is opened as an in-memory journal file. If false, then in-memory
@@ -42296,11 +42763,11 @@
42296 u8 changeCountDone; /* Set after incrementing the change-counter */
42297 u8 setMaster; /* True if a m-j name has been written to jrnl */
42298 u8 doNotSpill; /* Do not spill the cache when non-zero */
42299 u8 subjInMemory; /* True to use in-memory sub-journals */
42300 u8 bUseFetch; /* True to use xFetch() */
42301 u8 hasBeenUsed; /* True if any content previously read from this pager*/
42302 Pgno dbSize; /* Number of pages in the database */
42303 Pgno dbOrigSize; /* dbSize before the current transaction */
42304 Pgno dbFileSize; /* Number of pages in the database file */
42305 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
42306 int errCode; /* One of several kinds of errors */
@@ -42457,11 +42924,11 @@
42457 **
42458 ** instead of
42459 **
42460 ** if( pPager->jfd->pMethods ){ ...
42461 */
42462 #define isOpen(pFd) ((pFd)->pMethods)
42463
42464 /*
42465 ** Return true if this pager uses a write-ahead log instead of the usual
42466 ** rollback journal. Otherwise false.
42467 */
@@ -42680,23 +43147,25 @@
42680 PagerSavepoint *p;
42681 Pgno pgno = pPg->pgno;
42682 int i;
42683 for(i=0; i<pPager->nSavepoint; i++){
42684 p = &pPager->aSavepoint[i];
42685 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
42686 return 1;
42687 }
42688 }
42689 return 0;
42690 }
42691
 
42692 /*
42693 ** Return true if the page is already in the journal file.
42694 */
42695 static int pageInJournal(Pager *pPager, PgHdr *pPg){
42696 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
42697 }
 
42698
42699 /*
42700 ** Read a 32-bit integer from the given file descriptor. Store the integer
42701 ** that is read in *pRes. Return SQLITE_OK if everything worked, or an
42702 ** error code is something goes wrong.
@@ -43304,11 +43773,12 @@
43304 */
43305 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
43306 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
43307 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
43308 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
43309 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
 
43310 ){
43311 return rc;
43312 }
43313 pPager->journalOff += (nMaster+20);
43314
@@ -43864,11 +44334,11 @@
43864 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
43865 return SQLITE_DONE;
43866 }
43867 }
43868
43869 /* If this page has already been played by before during the current
43870 ** rollback, then don't bother to play it back again.
43871 */
43872 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
43873 return rc;
43874 }
@@ -45966,12 +46436,10 @@
45966 return rc;
45967 }
45968
45969 /*
45970 ** Append a record of the current state of page pPg to the sub-journal.
45971 ** It is the callers responsibility to use subjRequiresPage() to check
45972 ** that it is really required before calling this function.
45973 **
45974 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
45975 ** for all open savepoints before returning.
45976 **
45977 ** This function returns SQLITE_OK if everything is successful, an IO
@@ -46013,10 +46481,17 @@
46013 pPager->nSubRec++;
46014 assert( pPager->nSavepoint>0 );
46015 rc = addToSavepointBitvecs(pPager, pPg->pgno);
46016 }
46017 return rc;
 
 
 
 
 
 
 
46018 }
46019
46020 /*
46021 ** This function is called by the pcache layer when it has reached some
46022 ** soft memory limit. The first argument is a pointer to a Pager object
@@ -46071,13 +46546,11 @@
46071 }
46072
46073 pPg->pDirty = 0;
46074 if( pagerUseWal(pPager) ){
46075 /* Write a single frame for this page to the log. */
46076 if( subjRequiresPage(pPg) ){
46077 rc = subjournalPage(pPg);
46078 }
46079 if( rc==SQLITE_OK ){
46080 rc = pagerWalFrames(pPager, pPg, 0, 0);
46081 }
46082 }else{
46083
@@ -46086,43 +46559,10 @@
46086 || pPager->eState==PAGER_WRITER_CACHEMOD
46087 ){
46088 rc = syncJournal(pPager, 1);
46089 }
46090
46091 /* If the page number of this page is larger than the current size of
46092 ** the database image, it may need to be written to the sub-journal.
46093 ** This is because the call to pager_write_pagelist() below will not
46094 ** actually write data to the file in this case.
46095 **
46096 ** Consider the following sequence of events:
46097 **
46098 ** BEGIN;
46099 ** <journal page X>
46100 ** <modify page X>
46101 ** SAVEPOINT sp;
46102 ** <shrink database file to Y pages>
46103 ** pagerStress(page X)
46104 ** ROLLBACK TO sp;
46105 **
46106 ** If (X>Y), then when pagerStress is called page X will not be written
46107 ** out to the database file, but will be dropped from the cache. Then,
46108 ** following the "ROLLBACK TO sp" statement, reading page X will read
46109 ** data from the database file. This will be the copy of page X as it
46110 ** was when the transaction started, not as it was when "SAVEPOINT sp"
46111 ** was executed.
46112 **
46113 ** The solution is to write the current data for page X into the
46114 ** sub-journal file now (if it is not already there), so that it will
46115 ** be restored to its current value when the "ROLLBACK TO sp" is
46116 ** executed.
46117 */
46118 if( NEVER(
46119 rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
46120 ) ){
46121 rc = subjournalPage(pPg);
46122 }
46123
46124 /* Write the contents of the page out to the database file. */
46125 if( rc==SQLITE_OK ){
46126 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
46127 rc = pager_write_pagelist(pPager, pPg);
46128 }
@@ -46374,11 +46814,11 @@
46374 ** This branch also runs for files marked as immutable.
46375 */
46376 act_like_temp_file:
46377 tempFile = 1;
46378 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
46379 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE locking mode */
46380 pPager->noLock = 1; /* Do no locking */
46381 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
46382 }
46383
46384 /* The following call to PagerSetPagesize() serves to set the value of
@@ -46393,11 +46833,11 @@
46393 /* Initialize the PCache object. */
46394 if( rc==SQLITE_OK ){
46395 assert( nExtra<1000 );
46396 nExtra = ROUND8(nExtra);
46397 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
46398 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
46399 }
46400
46401 /* If an error occurred above, free the Pager structure and close the file.
46402 */
46403 if( rc!=SQLITE_OK ){
@@ -46780,11 +47220,11 @@
46780 ** see if the database has been modified. If the database has changed,
46781 ** flush the cache. The pPager->hasBeenUsed flag prevents this from
46782 ** occurring on the very first access to a file, in order to save a
46783 ** single unnecessary sqlite3OsRead() call at the start-up.
46784 **
46785 ** Database changes is detected by looking at 15 bytes beginning
46786 ** at offset 24 into the file. The first 4 of these 16 bytes are
46787 ** a 32-bit counter that is incremented with each change. The
46788 ** other bytes change randomly with each file change when
46789 ** a codec is in use.
46790 **
@@ -46988,13 +47428,18 @@
46988 sqlite3_pcache_page *pBase;
46989 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
46990 if( pBase==0 ){
46991 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
46992 if( rc!=SQLITE_OK ) goto pager_acquire_err;
 
 
 
 
 
46993 }
46994 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
46995 if( pPg==0 ) rc = SQLITE_NOMEM;
46996 }
46997 }
46998
46999 if( rc!=SQLITE_OK ){
47000 /* Either the call to sqlite3PcacheFetch() returned an error or the
@@ -47001,14 +47446,15 @@
47001 ** pager was already in the error-state when this function was called.
47002 ** Set pPg to 0 and jump to the exception handler. */
47003 pPg = 0;
47004 goto pager_acquire_err;
47005 }
47006 assert( (*ppPage)->pgno==pgno );
47007 assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
 
47008
47009 if( (*ppPage)->pPager && !noContent ){
47010 /* In this case the pcache already contains an initialized copy of
47011 ** the page. Return without further ado. */
47012 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
47013 pPager->aStat[PAGER_STAT_HIT]++;
47014 return SQLITE_OK;
@@ -47015,11 +47461,10 @@
47015
47016 }else{
47017 /* The pager cache has created a new page. Its content needs to
47018 ** be initialized. */
47019
47020 pPg = *ppPage;
47021 pPg->pPager = pPager;
47022
47023 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
47024 ** number greater than this, or the unused locking-page, is requested. */
47025 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
@@ -47094,10 +47539,11 @@
47094 assert( pPager!=0 );
47095 assert( pgno!=0 );
47096 assert( pPager->pPCache!=0 );
47097 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
47098 assert( pPage==0 || pPager->hasBeenUsed );
 
47099 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
47100 }
47101
47102 /*
47103 ** Release a page reference.
@@ -47296,10 +47742,63 @@
47296 }
47297
47298 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
47299 return rc;
47300 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47301
47302 /*
47303 ** Mark a single data page as writeable. The page is written into the
47304 ** main journal or sub-journal as required. If the page is written into
47305 ** one of the journals, the corresponding bit is set in the
@@ -47307,11 +47806,10 @@
47307 ** of any open savepoints as appropriate.
47308 */
47309 static int pager_write(PgHdr *pPg){
47310 Pager *pPager = pPg->pPager;
47311 int rc = SQLITE_OK;
47312 int inJournal;
47313
47314 /* This routine is not called unless a write-transaction has already
47315 ** been started. The journal file may or may not be open at this point.
47316 ** It is never called in the ERROR state.
47317 */
@@ -47320,11 +47818,10 @@
47320 || pPager->eState==PAGER_WRITER_DBMOD
47321 );
47322 assert( assert_pager_state(pPager) );
47323 assert( pPager->errCode==0 );
47324 assert( pPager->readOnly==0 );
47325
47326 CHECK_PAGE(pPg);
47327
47328 /* The journal file needs to be opened. Higher level routines have already
47329 ** obtained the necessary locks to begin the write-transaction, but the
47330 ** rollback journal might not yet be open. Open it now if this is the case.
@@ -47339,95 +47836,52 @@
47339 if( rc!=SQLITE_OK ) return rc;
47340 }
47341 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
47342 assert( assert_pager_state(pPager) );
47343
47344 /* Mark the page as dirty. If the page has already been written
47345 ** to the journal then we can return right away.
47346 */
47347 sqlite3PcacheMakeDirty(pPg);
47348 inJournal = pageInJournal(pPager, pPg);
47349 if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
47350 assert( !pagerUseWal(pPager) );
47351 }else{
47352
47353 /* The transaction journal now exists and we have a RESERVED or an
47354 ** EXCLUSIVE lock on the main database file. Write the current page to
47355 ** the transaction journal if it is not there already.
47356 */
47357 if( !inJournal && !pagerUseWal(pPager) ){
47358 assert( pagerUseWal(pPager)==0 );
47359 if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
47360 u32 cksum;
47361 char *pData2;
47362 i64 iOff = pPager->journalOff;
47363
47364 /* We should never write to the journal file the page that
47365 ** contains the database locks. The following assert verifies
47366 ** that we do not. */
47367 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
47368
47369 assert( pPager->journalHdr<=pPager->journalOff );
47370 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
47371 cksum = pager_cksum(pPager, (u8*)pData2);
47372
47373 /* Even if an IO or diskfull error occurs while journalling the
47374 ** page in the block above, set the need-sync flag for the page.
47375 ** Otherwise, when the transaction is rolled back, the logic in
47376 ** playback_one_page() will think that the page needs to be restored
47377 ** in the database file. And if an IO error occurs while doing so,
47378 ** then corruption may follow.
47379 */
47380 pPg->flags |= PGHDR_NEED_SYNC;
47381
47382 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
47383 if( rc!=SQLITE_OK ) return rc;
47384 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
47385 if( rc!=SQLITE_OK ) return rc;
47386 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
47387 if( rc!=SQLITE_OK ) return rc;
47388
47389 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
47390 pPager->journalOff, pPager->pageSize));
47391 PAGER_INCR(sqlite3_pager_writej_count);
47392 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
47393 PAGERID(pPager), pPg->pgno,
47394 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
47395
47396 pPager->journalOff += 8 + pPager->pageSize;
47397 pPager->nRec++;
47398 assert( pPager->pInJournal!=0 );
47399 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
47400 testcase( rc==SQLITE_NOMEM );
47401 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
47402 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
47403 if( rc!=SQLITE_OK ){
47404 assert( rc==SQLITE_NOMEM );
47405 return rc;
47406 }
47407 }else{
47408 if( pPager->eState!=PAGER_WRITER_DBMOD ){
47409 pPg->flags |= PGHDR_NEED_SYNC;
47410 }
47411 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
47412 PAGERID(pPager), pPg->pgno,
47413 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
47414 }
47415 }
47416
47417 /* If the statement journal is open and the page is not in it,
47418 ** then write the current page to the statement journal. Note that
47419 ** the statement journal format differs from the standard journal format
47420 ** in that it omits the checksums and the header.
47421 */
47422 if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
47423 rc = subjournalPage(pPg);
47424 }
47425 }
47426
47427 /* Update the database size and return.
47428 */
47429 if( pPager->dbSize<pPg->pgno ){
47430 pPager->dbSize = pPg->pgno;
47431 }
47432 return rc;
47433 }
@@ -47438,21 +47892,21 @@
47438 ** all bytes of a sector are written together by hardware. Hence, all bytes of
47439 ** a sector need to be journalled in case of a power loss in the middle of
47440 ** a write.
47441 **
47442 ** Usually, the sector size is less than or equal to the page size, in which
47443 ** case pages can be individually written. This routine only runs in the exceptional
47444 ** case where the page size is smaller than the sector size.
47445 */
47446 static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
47447 int rc = SQLITE_OK; /* Return code */
47448 Pgno nPageCount; /* Total number of pages in database file */
47449 Pgno pg1; /* First page of the sector pPg is located on. */
47450 int nPage = 0; /* Number of pages starting at pg1 to journal */
47451 int ii; /* Loop counter */
47452 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
47453 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
47454 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
47455
47456 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
47457 ** a journal header to be written between the pages journaled by
47458 ** this function.
@@ -47536,15 +47990,19 @@
47536 **
47537 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
47538 ** as appropriate. Otherwise, SQLITE_OK.
47539 */
47540 SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
 
47541 assert( (pPg->flags & PGHDR_MMAP)==0 );
47542 assert( pPg->pPager->eState>=PAGER_WRITER_LOCKED );
47543 assert( pPg->pPager->eState!=PAGER_ERROR );
47544 assert( assert_pager_state(pPg->pPager) );
47545 if( pPg->pPager->sectorSize > (u32)pPg->pPager->pageSize ){
 
 
 
47546 return pagerWriteLargeSector(pPg);
47547 }else{
47548 return pager_write(pPg);
47549 }
47550 }
@@ -47554,11 +48012,11 @@
47554 ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
47555 ** to change the content of the page.
47556 */
47557 #ifndef NDEBUG
47558 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
47559 return pPg->flags&PGHDR_DIRTY;
47560 }
47561 #endif
47562
47563 /*
47564 ** A call to this routine tells the pager that it is not necessary to
@@ -47578,10 +48036,11 @@
47578 Pager *pPager = pPg->pPager;
47579 if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
47580 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
47581 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
47582 pPg->flags |= PGHDR_DONT_WRITE;
 
47583 pager_set_pagehash(pPg);
47584 }
47585 }
47586
47587 /*
@@ -48132,58 +48591,66 @@
48132 **
48133 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
48134 ** occurs while opening the sub-journal file, then an IO error code is
48135 ** returned. Otherwise, SQLITE_OK.
48136 */
48137 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
48138 int rc = SQLITE_OK; /* Return code */
48139 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
 
 
48140
48141 assert( pPager->eState>=PAGER_WRITER_LOCKED );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48142 assert( assert_pager_state(pPager) );
48143
48144 if( nSavepoint>nCurrent && pPager->useJournal ){
48145 int ii; /* Iterator variable */
48146 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
48147
48148 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
48149 ** if the allocation fails. Otherwise, zero the new portion in case a
48150 ** malloc failure occurs while populating it in the for(...) loop below.
48151 */
48152 aNew = (PagerSavepoint *)sqlite3Realloc(
48153 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
48154 );
48155 if( !aNew ){
48156 return SQLITE_NOMEM;
48157 }
48158 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
48159 pPager->aSavepoint = aNew;
48160
48161 /* Populate the PagerSavepoint structures just allocated. */
48162 for(ii=nCurrent; ii<nSavepoint; ii++){
48163 aNew[ii].nOrig = pPager->dbSize;
48164 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
48165 aNew[ii].iOffset = pPager->journalOff;
48166 }else{
48167 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
48168 }
48169 aNew[ii].iSubRec = pPager->nSubRec;
48170 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
48171 if( !aNew[ii].pInSavepoint ){
48172 return SQLITE_NOMEM;
48173 }
48174 if( pagerUseWal(pPager) ){
48175 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
48176 }
48177 pPager->nSavepoint = ii+1;
48178 }
48179 assert( pPager->nSavepoint==nSavepoint );
48180 assertTruncateConstraint(pPager);
48181 }
48182
48183 return rc;
48184 }
48185
48186 /*
48187 ** This function is called to rollback or release (commit) a savepoint.
48188 ** The savepoint to release or rollback need not be the most recently
48189 ** created savepoint.
@@ -48410,13 +48877,12 @@
48410 **
48411 ** subjournalPage() may need to allocate space to store pPg->pgno into
48412 ** one or more savepoint bitvecs. This is the reason this function
48413 ** may return SQLITE_NOMEM.
48414 */
48415 if( pPg->flags&PGHDR_DIRTY
48416 && subjRequiresPage(pPg)
48417 && SQLITE_OK!=(rc = subjournalPage(pPg))
48418 ){
48419 return rc;
48420 }
48421
48422 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
@@ -52330,10 +52796,11 @@
52330 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
52331
52332 /* Forward declarations */
52333 typedef struct MemPage MemPage;
52334 typedef struct BtLock BtLock;
 
52335
52336 /*
52337 ** This is a magic string that appears at the beginning of every
52338 ** SQLite database in order to identify the file as a real database.
52339 **
@@ -52393,11 +52860,14 @@
52393 u8 *apOvfl[5]; /* Pointers to the body of overflow cells */
52394 BtShared *pBt; /* Pointer to BtShared that this page is part of */
52395 u8 *aData; /* Pointer to disk image of the page data */
52396 u8 *aDataEnd; /* One byte past the end of usable data */
52397 u8 *aCellIdx; /* The cell index area */
 
52398 DbPage *pDbPage; /* Pager page handle */
 
 
52399 Pgno pgno; /* Page number for this page */
52400 };
52401
52402 /*
52403 ** The in-memory image of a disk page has the auxiliary information appended
@@ -52449,10 +52919,11 @@
52449 sqlite3 *db; /* The database connection holding this btree */
52450 BtShared *pBt; /* Sharable content of this btree */
52451 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
52452 u8 sharable; /* True if we can share pBt with another db */
52453 u8 locked; /* True if db currently has pBt locked */
 
52454 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
52455 int nBackup; /* Number of backup operations reading this btree */
52456 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
52457 Btree *pNext; /* List of other sharable Btrees from the same db */
52458 Btree *pPrev; /* Back pointer of the same list */
@@ -52559,11 +53030,10 @@
52559 /*
52560 ** An instance of the following structure is used to hold information
52561 ** about a cell. The parseCellPtr() function fills in this structure
52562 ** based on information extract from the raw disk page.
52563 */
52564 typedef struct CellInfo CellInfo;
52565 struct CellInfo {
52566 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
52567 u8 *pPayload; /* Pointer to the start of payload */
52568 u32 nPayload; /* Bytes of payload */
52569 u16 nLocal; /* Amount of payload held locally, not on overflow */
@@ -52602,24 +53072,30 @@
52602 ** eState==FAULT: Cursor fault with skipNext as error code.
52603 */
52604 struct BtCursor {
52605 Btree *pBtree; /* The Btree to which this cursor belongs */
52606 BtShared *pBt; /* The BtShared this cursor points to */
52607 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
52608 struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
52609 Pgno *aOverflow; /* Cache of overflow page locations */
52610 CellInfo info; /* A parse of the cell we are pointing at */
52611 i64 nKey; /* Size of pKey, or last integer key */
52612 void *pKey; /* Saved key that was cursor last known position */
52613 Pgno pgnoRoot; /* The root page of this tree */
52614 int nOvflAlloc; /* Allocated size of aOverflow[] array */
52615 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
52616 ** Error code if eState==CURSOR_FAULT */
52617 u8 curFlags; /* zero or more BTCF_* flags defined below */
 
52618 u8 eState; /* One of the CURSOR_XXX constants (see below) */
52619 u8 hints; /* As configured by CursorSetHints() */
52620 i16 iPage; /* Index of current page in apPage */
 
 
 
 
 
 
52621 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */
52622 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
52623 };
52624
52625 /*
@@ -52628,10 +53104,11 @@
52628 #define BTCF_WriteFlag 0x01 /* True if a write cursor */
52629 #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
52630 #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
52631 #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */
52632 #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
 
52633
52634 /*
52635 ** Potential values for BtCursor.eState.
52636 **
52637 ** CURSOR_INVALID:
@@ -52780,10 +53257,23 @@
52780 #define get2byte(x) ((x)[0]<<8 | (x)[1])
52781 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
52782 #define get4byte sqlite3Get4byte
52783 #define put4byte sqlite3Put4byte
52784
 
 
 
 
 
 
 
 
 
 
 
 
 
52785 /************** End of btreeInt.h ********************************************/
52786 /************** Continuing where we left off in btmutex.c ********************/
52787 #ifndef SQLITE_OMIT_SHARED_CACHE
52788 #if SQLITE_THREADSAFE
52789
@@ -53559,17 +54049,19 @@
53559 Btree *pBtree, /* The database file to check */
53560 i64 iRow, /* The rowid that might be changing */
53561 int isClearTable /* True if all rows are being deleted */
53562 ){
53563 BtCursor *p;
53564 BtShared *pBt = pBtree->pBt;
53565 assert( sqlite3BtreeHoldsMutex(pBtree) );
53566 for(p=pBt->pCursor; p; p=p->pNext){
53567 if( (p->curFlags & BTCF_Incrblob)!=0
53568 && (isClearTable || p->info.nKey==iRow)
53569 ){
53570 p->eState = CURSOR_INVALID;
 
 
53571 }
53572 }
53573 }
53574
53575 #else
@@ -53687,11 +54179,11 @@
53687 ** stores the integer key in pCur->nKey. In this case this value is
53688 ** all that is required. Otherwise, if pCur is not open on an intKey
53689 ** table, then malloc space for and store the pCur->nKey bytes of key
53690 ** data.
53691 */
53692 if( 0==pCur->apPage[0]->intKey ){
53693 void *pKey = sqlite3Malloc( pCur->nKey );
53694 if( pKey ){
53695 rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
53696 if( rc==SQLITE_OK ){
53697 pCur->pKey = pKey;
@@ -53700,11 +54192,11 @@
53700 }
53701 }else{
53702 rc = SQLITE_NOMEM;
53703 }
53704 }
53705 assert( !pCur->apPage[0]->intKey || !pCur->pKey );
53706
53707 if( rc==SQLITE_OK ){
53708 btreeReleaseAllCursorPages(pCur);
53709 pCur->eState = CURSOR_REQUIRESEEK;
53710 }
@@ -53721,10 +54213,19 @@
53721 ** the table with root-page iRoot. "Saving the cursor position" means that
53722 ** the location in the btree is remembered in such a way that it can be
53723 ** moved back to the same spot after the btree has been modified. This
53724 ** routine is called just before cursor pExcept is used to modify the
53725 ** table, for example in BtreeDelete() or BtreeInsert().
 
 
 
 
 
 
 
 
 
53726 **
53727 ** Implementation note: This routine merely checks to see if any cursors
53728 ** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
53729 ** event that cursors are in need to being saved.
53730 */
@@ -53733,11 +54234,13 @@
53733 assert( sqlite3_mutex_held(pBt->mutex) );
53734 assert( pExcept==0 || pExcept->pBt==pBt );
53735 for(p=pBt->pCursor; p; p=p->pNext){
53736 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
53737 }
53738 return p ? saveCursorsOnList(p, iRoot, pExcept) : SQLITE_OK;
 
 
53739 }
53740
53741 /* This helper routine to saveAllCursors does the actual work of saving
53742 ** the cursors if and when a cursor is found that actually requires saving.
53743 ** The common case is that no cursors need to be saved, so this routine is
@@ -54020,71 +54523,184 @@
54020
54021 /*
54022 ** Given a btree page and a cell index (0 means the first cell on
54023 ** the page, 1 means the second cell, and so forth) return a pointer
54024 ** to the cell content.
 
 
 
54025 **
54026 ** This routine works only for pages that do not contain overflow cells.
54027 */
54028 #define findCell(P,I) \
54029 ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
54030 #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
 
54031
54032
54033 /*
54034 ** This a more complex version of findCell() that works for
54035 ** pages that do contain overflow cells.
 
 
54036 */
54037 static u8 *findOverflowCell(MemPage *pPage, int iCell){
54038 int i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54039 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54040 for(i=pPage->nOverflow-1; i>=0; i--){
54041 int k;
54042 k = pPage->aiOvfl[i];
54043 if( k<=iCell ){
54044 if( k==iCell ){
54045 return pPage->apOvfl[i];
54046 }
54047 iCell--;
54048 }
54049 }
54050 return findCell(pPage, iCell);
54051 }
54052
54053 /*
54054 ** Parse a cell content block and fill in the CellInfo structure. There
54055 ** are two versions of this function. btreeParseCell() takes a
54056 ** cell index as the second argument and btreeParseCellPtr()
54057 ** takes a pointer to the body of the cell as its second argument.
54058 */
54059 static void btreeParseCellPtr(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54060 MemPage *pPage, /* Page containing the cell */
54061 u8 *pCell, /* Pointer to the cell text. */
54062 CellInfo *pInfo /* Fill in this structure */
54063 ){
54064 u8 *pIter; /* For scanning through pCell */
54065 u32 nPayload; /* Number of bytes of cell payload */
54066
54067 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54068 assert( pPage->leaf==0 || pPage->leaf==1 );
54069 if( pPage->intKeyLeaf ){
54070 assert( pPage->childPtrSize==0 );
54071 pIter = pCell + getVarint32(pCell, nPayload);
54072 pIter += getVarint(pIter, (u64*)&pInfo->nKey);
54073 }else if( pPage->noPayload ){
54074 assert( pPage->childPtrSize==4 );
54075 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
54076 pInfo->nPayload = 0;
54077 pInfo->nLocal = 0;
54078 pInfo->iOverflow = 0;
54079 pInfo->pPayload = 0;
54080 return;
54081 }else{
54082 pIter = pCell + pPage->childPtrSize;
54083 pIter += getVarint32(pIter, nPayload);
54084 pInfo->nKey = nPayload;
54085 }
54086 pInfo->nPayload = nPayload;
54087 pInfo->pPayload = pIter;
54088 testcase( nPayload==pPage->maxLocal );
54089 testcase( nPayload==pPage->maxLocal+1 );
54090 if( nPayload<=pPage->maxLocal ){
@@ -54094,50 +54710,32 @@
54094 pInfo->nSize = nPayload + (u16)(pIter - pCell);
54095 if( pInfo->nSize<4 ) pInfo->nSize = 4;
54096 pInfo->nLocal = (u16)nPayload;
54097 pInfo->iOverflow = 0;
54098 }else{
54099 /* If the payload will not fit completely on the local page, we have
54100 ** to decide how much to store locally and how much to spill onto
54101 ** overflow pages. The strategy is to minimize the amount of unused
54102 ** space on overflow pages while keeping the amount of local storage
54103 ** in between minLocal and maxLocal.
54104 **
54105 ** Warning: changing the way overflow payload is distributed in any
54106 ** way will result in an incompatible file format.
54107 */
54108 int minLocal; /* Minimum amount of payload held locally */
54109 int maxLocal; /* Maximum amount of payload held locally */
54110 int surplus; /* Overflow payload available for local storage */
54111
54112 minLocal = pPage->minLocal;
54113 maxLocal = pPage->maxLocal;
54114 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
54115 testcase( surplus==maxLocal );
54116 testcase( surplus==maxLocal+1 );
54117 if( surplus <= maxLocal ){
54118 pInfo->nLocal = (u16)surplus;
54119 }else{
54120 pInfo->nLocal = (u16)minLocal;
54121 }
54122 pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
54123 pInfo->nSize = pInfo->iOverflow + 4;
54124 }
54125 }
54126 static void btreeParseCell(
54127 MemPage *pPage, /* Page containing the cell */
54128 int iCell, /* The cell index. First cell is 0 */
54129 CellInfo *pInfo /* Fill in this structure */
54130 ){
54131 btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo);
54132 }
54133
54134 /*
 
 
 
54135 ** Compute the total number of bytes that a Cell needs in the cell
54136 ** data area of the btree-page. The return number includes the cell
54137 ** data header and the local payload, but not any overflow page or
54138 ** the space used by the cell pointer.
 
 
 
54139 */
54140 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
54141 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
54142 u8 *pEnd; /* End mark for a varint */
54143 u32 nSize; /* Size value to return */
@@ -54146,22 +54744,17 @@
54146 /* The value returned by this function should always be the same as
54147 ** the (CellInfo.nSize) value found by doing a full parse of the
54148 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
54149 ** this function verifies that this invariant is not violated. */
54150 CellInfo debuginfo;
54151 btreeParseCellPtr(pPage, pCell, &debuginfo);
54152 #endif
54153
54154 if( pPage->noPayload ){
54155 pEnd = &pIter[9];
54156 while( (*pIter++)&0x80 && pIter<pEnd );
54157 assert( pPage->childPtrSize==4 );
54158 return (u16)(pIter - pCell);
54159 }
54160 nSize = *pIter;
54161 if( nSize>=0x80 ){
54162 pEnd = &pIter[9];
54163 nSize &= 0x7f;
54164 do{
54165 nSize = (nSize<<7) | (*++pIter & 0x7f);
54166 }while( *(pIter)>=0x80 && pIter<pEnd );
54167 }
@@ -54189,16 +54782,36 @@
54189 nSize += 4 + (u16)(pIter - pCell);
54190 }
54191 assert( nSize==debuginfo.nSize || CORRUPT_DB );
54192 return (u16)nSize;
54193 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54194
54195 #ifdef SQLITE_DEBUG
54196 /* This variation on cellSizePtr() is used inside of assert() statements
54197 ** only. */
54198 static u16 cellSize(MemPage *pPage, int iCell){
54199 return cellSizePtr(pPage, findCell(pPage, iCell));
54200 }
54201 #endif
54202
54203 #ifndef SQLITE_OMIT_AUTOVACUUM
54204 /*
@@ -54208,11 +54821,11 @@
54208 */
54209 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
54210 CellInfo info;
54211 if( *pRC ) return;
54212 assert( pCell!=0 );
54213 btreeParseCellPtr(pPage, pCell, &info);
54214 if( info.iOverflow ){
54215 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
54216 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
54217 }
54218 }
@@ -54272,11 +54885,11 @@
54272 */
54273 if( pc<iCellFirst || pc>iCellLast ){
54274 return SQLITE_CORRUPT_BKPT;
54275 }
54276 assert( pc>=iCellFirst && pc<=iCellLast );
54277 size = cellSizePtr(pPage, &src[pc]);
54278 cbrk -= size;
54279 if( cbrk<iCellFirst || pc+size>usableSize ){
54280 return SQLITE_CORRUPT_BKPT;
54281 }
54282 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
@@ -54314,22 +54927,24 @@
54314 ** If no suitable space can be found on the free-list, return NULL.
54315 **
54316 ** This function may detect corruption within pPg. If corruption is
54317 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
54318 **
54319 ** If a slot of at least nByte bytes is found but cannot be used because
54320 ** there are already at least 60 fragmented bytes on the page, return NULL.
54321 ** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true.
54322 */
54323 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){
54324 const int hdr = pPg->hdrOffset;
54325 u8 * const aData = pPg->aData;
54326 int iAddr;
54327 int pc;
 
54328 int usableSize = pPg->pBt->usableSize;
54329
54330 for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){
 
54331 int size; /* Size of the free slot */
54332 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
54333 ** increasing offset. */
54334 if( pc>usableSize-4 || pc<iAddr+4 ){
54335 *pRc = SQLITE_CORRUPT_BKPT;
@@ -54337,36 +54952,35 @@
54337 }
54338 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
54339 ** freeblock form a big-endian integer which is the size of the freeblock
54340 ** in bytes, including the 4-byte header. */
54341 size = get2byte(&aData[pc+2]);
54342 if( size>=nByte ){
54343 int x = size - nByte;
54344 testcase( x==4 );
54345 testcase( x==3 );
54346 if( x<4 ){
 
 
 
54347 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
54348 ** number of bytes in fragments may not exceed 60. */
54349 if( aData[hdr+7]>=60 ){
54350 if( pbDefrag ) *pbDefrag = 1;
54351 return 0;
54352 }
54353 /* Remove the slot from the free-list. Update the number of
54354 ** fragmented bytes within the page. */
54355 memcpy(&aData[iAddr], &aData[pc], 2);
54356 aData[hdr+7] += (u8)x;
54357 }else if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
54358 *pRc = SQLITE_CORRUPT_BKPT;
54359 return 0;
54360 }else{
54361 /* The slot remains on the free-list. Reduce its size to account
54362 ** for the portion used by the new allocation. */
54363 put2byte(&aData[pc+2], x);
54364 }
54365 return &aData[pc + x];
54366 }
54367 }
 
 
54368
54369 return 0;
54370 }
54371
54372 /*
@@ -54403,42 +55017,43 @@
54403 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
54404 ** and the reserved space is zero (the usual value for reserved space)
54405 ** then the cell content offset of an empty page wants to be 65536.
54406 ** However, that integer is too large to be stored in a 2-byte unsigned
54407 ** integer, so a value of 0 is used in its place. */
54408 top = get2byteNotZero(&data[hdr+5]);
54409 if( gap>top || NEVER((u32)top>pPage->pBt->usableSize) ){
54410 /* The NEVER() is because a oversize "top" value will be blocked from
54411 ** reaching this point by btreeInitPage() or btreeGetUnusedPage() */
54412 return SQLITE_CORRUPT_BKPT;
 
 
 
54413 }
54414
54415 /* If there is enough space between gap and top for one more cell pointer
54416 ** array entry offset, and if the freelist is not empty, then search the
54417 ** freelist looking for a free slot big enough to satisfy the request.
54418 */
54419 testcase( gap+2==top );
54420 testcase( gap+1==top );
54421 testcase( gap==top );
54422 if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){
54423 int bDefrag = 0;
54424 u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag);
54425 if( rc ) return rc;
54426 if( bDefrag ) goto defragment_page;
54427 if( pSpace ){
54428 assert( pSpace>=data && (pSpace - data)<65536 );
54429 *pIdx = (int)(pSpace - data);
54430 return SQLITE_OK;
 
 
54431 }
54432 }
54433
54434 /* The request could not be fulfilled using a freelist slot. Check
54435 ** to see if defragmentation is necessary.
54436 */
54437 testcase( gap+2+nByte==top );
54438 if( gap+2+nByte>top ){
54439 defragment_page:
54440 assert( pPage->nCell>0 || CORRUPT_DB );
54441 rc = defragmentPage(pPage);
54442 if( rc ) return rc;
54443 top = get2byteNotZero(&data[hdr+5]);
54444 assert( gap+nByte<=top );
@@ -54510,18 +55125,19 @@
54510 if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
54511 assert( iFreeBlk>iPtr || iFreeBlk==0 );
54512
54513 /* At this point:
54514 ** iFreeBlk: First freeblock after iStart, or zero if none
54515 ** iPtr: The address of a pointer iFreeBlk
54516 **
54517 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
54518 */
54519 if( iFreeBlk && iEnd+3>=iFreeBlk ){
54520 nFrag = iFreeBlk - iEnd;
54521 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
54522 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
 
54523 iSize = iEnd - iStart;
54524 iFreeBlk = get2byte(&data[iFreeBlk]);
54525 }
54526
54527 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
@@ -54575,21 +55191,30 @@
54575 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
54576 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54577 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
54578 flagByte &= ~PTF_LEAF;
54579 pPage->childPtrSize = 4-4*pPage->leaf;
 
54580 pBt = pPage->pBt;
54581 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
54582 /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
54583 ** table b-tree page. */
54584 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
54585 /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
54586 ** table b-tree page. */
54587 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
54588 pPage->intKey = 1;
54589 pPage->intKeyLeaf = pPage->leaf;
54590 pPage->noPayload = !pPage->leaf;
 
 
 
 
 
 
 
 
54591 pPage->maxLocal = pBt->maxLeaf;
54592 pPage->minLocal = pBt->minLeaf;
54593 }else if( flagByte==PTF_ZERODATA ){
54594 /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
54595 ** index b-tree page. */
@@ -54598,10 +55223,11 @@
54598 ** index b-tree page. */
54599 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
54600 pPage->intKey = 0;
54601 pPage->intKeyLeaf = 0;
54602 pPage->noPayload = 0;
 
54603 pPage->maxLocal = pBt->maxLocal;
54604 pPage->minLocal = pBt->minLocal;
54605 }else{
54606 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
54607 ** an error. */
@@ -54653,10 +55279,11 @@
54653 pPage->nOverflow = 0;
54654 usableSize = pBt->usableSize;
54655 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
54656 pPage->aDataEnd = &data[usableSize];
54657 pPage->aCellIdx = &data[cellOffset];
 
54658 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
54659 ** the start of the cell content area. A zero value for this integer is
54660 ** interpreted as 65536. */
54661 top = get2byteNotZero(&data[hdr+5]);
54662 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
@@ -54686,17 +55313,17 @@
54686 int i; /* Index into the cell pointer array */
54687 int sz; /* Size of a cell */
54688
54689 if( !pPage->leaf ) iCellLast--;
54690 for(i=0; i<pPage->nCell; i++){
54691 pc = get2byte(&data[cellOffset+i*2]);
54692 testcase( pc==iCellFirst );
54693 testcase( pc==iCellLast );
54694 if( pc<iCellFirst || pc>iCellLast ){
54695 return SQLITE_CORRUPT_BKPT;
54696 }
54697 sz = cellSizePtr(pPage, &data[pc]);
54698 testcase( pc+sz==usableSize );
54699 if( pc+sz>usableSize ){
54700 return SQLITE_CORRUPT_BKPT;
54701 }
54702 }
@@ -54772,10 +55399,11 @@
54772 pPage->nFree = (u16)(pBt->usableSize - first);
54773 decodeFlags(pPage, flags);
54774 pPage->cellOffset = first;
54775 pPage->aDataEnd = &data[pBt->usableSize];
54776 pPage->aCellIdx = &data[first];
 
54777 pPage->nOverflow = 0;
54778 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
54779 pPage->maskPage = (u16)(pBt->pageSize - 1);
54780 pPage->nCell = 0;
54781 pPage->isInit = 1;
@@ -54790,11 +55418,11 @@
54790 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
54791 pPage->aData = sqlite3PagerGetData(pDbPage);
54792 pPage->pDbPage = pDbPage;
54793 pPage->pBt = pBt;
54794 pPage->pgno = pgno;
54795 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
54796 return pPage;
54797 }
54798
54799 /*
54800 ** Get a page from the pager. Initialize the MemPage.pBt and
@@ -54851,58 +55479,86 @@
54851 assert( ((p->pBt->nPage)&0x8000000)==0 );
54852 return btreePagecount(p->pBt);
54853 }
54854
54855 /*
54856 ** Get a page from the pager and initialize it. This routine is just a
54857 ** convenience wrapper around separate calls to btreeGetPage() and
54858 ** btreeInitPage().
54859 **
54860 ** If an error occurs, then the value *ppPage is set to is undefined. It
 
 
 
 
 
 
 
54861 ** may remain unchanged, or it may be set to an invalid value.
54862 */
54863 static int getAndInitPage(
54864 BtShared *pBt, /* The database file */
54865 Pgno pgno, /* Number of the page to get */
54866 MemPage **ppPage, /* Write the page pointer here */
54867 int bReadonly /* PAGER_GET_READONLY or 0 */
 
54868 ){
54869 int rc;
 
54870 assert( sqlite3_mutex_held(pBt->mutex) );
54871 assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
 
 
54872
54873 if( pgno>btreePagecount(pBt) ){
54874 rc = SQLITE_CORRUPT_BKPT;
54875 }else{
54876 rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
54877 if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
54878 rc = btreeInitPage(*ppPage);
54879 if( rc!=SQLITE_OK ){
54880 releasePage(*ppPage);
54881 }
 
 
 
 
 
54882 }
54883 }
54884
 
 
 
 
 
 
 
 
 
 
 
 
 
54885 testcase( pgno==0 );
54886 assert( pgno!=0 || rc==SQLITE_CORRUPT );
54887 return rc;
54888 }
54889
54890 /*
54891 ** Release a MemPage. This should be called once for each prior
54892 ** call to btreeGetPage.
54893 */
 
 
 
 
 
 
 
 
 
54894 static void releasePage(MemPage *pPage){
54895 if( pPage ){
54896 assert( pPage->aData );
54897 assert( pPage->pBt );
54898 assert( pPage->pDbPage!=0 );
54899 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
54900 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
54901 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54902 sqlite3PagerUnrefNotNull(pPage->pDbPage);
54903 }
54904 }
54905
54906 /*
54907 ** Get an unused page.
54908 **
@@ -55873,11 +56529,11 @@
55873 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
55874 MemPage *pPage1 = pBt->pPage1;
55875 assert( pPage1->aData );
55876 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
55877 pBt->pPage1 = 0;
55878 releasePage(pPage1);
55879 }
55880 }
55881
55882 /*
55883 ** If pBt points to an empty file then convert that empty file
@@ -56188,11 +56844,11 @@
56188
56189 for(i=0; i<nCell; i++){
56190 u8 *pCell = findCell(pPage, i);
56191 if( eType==PTRMAP_OVERFLOW1 ){
56192 CellInfo info;
56193 btreeParseCellPtr(pPage, pCell, &info);
56194 if( info.iOverflow
56195 && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
56196 && iFrom==get4byte(&pCell[info.iOverflow])
56197 ){
56198 put4byte(&pCell[info.iOverflow], iTo);
@@ -56929,10 +57585,11 @@
56929 int wrFlag, /* 1 to write. 0 read-only */
56930 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
56931 BtCursor *pCur /* Space for new cursor */
56932 ){
56933 BtShared *pBt = p->pBt; /* Shared b-tree handle */
 
56934
56935 assert( sqlite3BtreeHoldsMutex(p) );
56936 assert( wrFlag==0 || wrFlag==1 );
56937
56938 /* The following assert statements verify that if this is a sharable
@@ -56944,14 +57601,12 @@
56944
56945 /* Assert that the caller has opened the required transaction. */
56946 assert( p->inTrans>TRANS_NONE );
56947 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
56948 assert( pBt->pPage1 && pBt->pPage1->aData );
 
56949
56950 if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
56951 return SQLITE_READONLY;
56952 }
56953 if( wrFlag ){
56954 allocateTempSpace(pBt);
56955 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
56956 }
56957 if( iTable==1 && btreePagecount(pBt)==0 ){
@@ -56966,14 +57621,20 @@
56966 pCur->pKeyInfo = pKeyInfo;
56967 pCur->pBtree = p;
56968 pCur->pBt = pBt;
56969 assert( wrFlag==0 || wrFlag==BTCF_WriteFlag );
56970 pCur->curFlags = wrFlag;
 
 
 
 
 
 
 
 
 
56971 pCur->pNext = pBt->pCursor;
56972 if( pCur->pNext ){
56973 pCur->pNext->pPrev = pCur;
56974 }
56975 pBt->pCursor = pCur;
56976 pCur->eState = CURSOR_INVALID;
56977 return SQLITE_OK;
56978 }
56979 SQLITE_PRIVATE int sqlite3BtreeCursor(
@@ -57027,17 +57688,22 @@
57027 if( pBtree ){
57028 int i;
57029 BtShared *pBt = pCur->pBt;
57030 sqlite3BtreeEnter(pBtree);
57031 sqlite3BtreeClearCursor(pCur);
57032 if( pCur->pPrev ){
57033 pCur->pPrev->pNext = pCur->pNext;
57034 }else{
57035 pBt->pCursor = pCur->pNext;
57036 }
57037 if( pCur->pNext ){
57038 pCur->pNext->pPrev = pCur->pPrev;
 
 
 
 
 
 
57039 }
57040 for(i=0; i<=pCur->iPage; i++){
57041 releasePage(pCur->apPage[i]);
57042 }
57043 unlockBtreeIfUnused(pBt);
@@ -57053,17 +57719,10 @@
57053 ** BtCursor.info structure. If it is not already valid, call
57054 ** btreeParseCell() to fill it in.
57055 **
57056 ** BtCursor.info is a cache of the information in the current cell.
57057 ** Using this cache reduces the number of calls to btreeParseCell().
57058 **
57059 ** 2007-06-25: There is a bug in some versions of MSVC that cause the
57060 ** compiler to crash when getCellInfo() is implemented as a macro.
57061 ** But there is a measureable speed advantage to using the macro on gcc
57062 ** (when less compiler optimizations like -Os or -O0 are used and the
57063 ** compiler is not doing aggressive inlining.) So we use a real function
57064 ** for MSVC and a macro for everything else. Ticket #2457.
57065 */
57066 #ifndef NDEBUG
57067 static void assertCellInfo(BtCursor *pCur){
57068 CellInfo info;
57069 int iPage = pCur->iPage;
@@ -57072,32 +57731,19 @@
57072 assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
57073 }
57074 #else
57075 #define assertCellInfo(x)
57076 #endif
57077 #ifdef _MSC_VER
57078 /* Use a real function in MSVC to work around bugs in that compiler. */
57079 static void getCellInfo(BtCursor *pCur){
57080 if( pCur->info.nSize==0 ){
57081 int iPage = pCur->iPage;
57082 btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
57083 pCur->curFlags |= BTCF_ValidNKey;
57084 }else{
57085 assertCellInfo(pCur);
57086 }
57087 }
57088 #else /* if not _MSC_VER */
57089 /* Use a macro in all other compilers so that the function is inlined */
57090 #define getCellInfo(pCur) \
57091 if( pCur->info.nSize==0 ){ \
57092 int iPage = pCur->iPage; \
57093 btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
57094 pCur->curFlags |= BTCF_ValidNKey; \
57095 }else{ \
57096 assertCellInfo(pCur); \
57097 }
57098 #endif /* _MSC_VER */
57099
57100 #ifndef NDEBUG /* The next routine used only within assert() statements */
57101 /*
57102 ** Return true if the given BtCursor is valid. A valid cursor is one
57103 ** that is currently pointing to a row in a (non-empty) table.
@@ -57599,35 +58245,25 @@
57599 ** the new child page does not match the flags field of the parent (i.e.
57600 ** if an intkey page appears to be the parent of a non-intkey page, or
57601 ** vice-versa).
57602 */
57603 static int moveToChild(BtCursor *pCur, u32 newPgno){
57604 int rc;
57605 int i = pCur->iPage;
57606 MemPage *pNewPage;
57607 BtShared *pBt = pCur->pBt;
57608
57609 assert( cursorHoldsMutex(pCur) );
57610 assert( pCur->eState==CURSOR_VALID );
57611 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
57612 assert( pCur->iPage>=0 );
57613 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
57614 return SQLITE_CORRUPT_BKPT;
57615 }
57616 rc = getAndInitPage(pBt, newPgno, &pNewPage,
57617 (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
57618 if( rc ) return rc;
57619 pCur->apPage[i+1] = pNewPage;
57620 pCur->aiIdx[i+1] = 0;
57621 pCur->iPage++;
57622
57623 pCur->info.nSize = 0;
57624 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
57625 if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
57626 return SQLITE_CORRUPT_BKPT;
57627 }
57628 return SQLITE_OK;
57629 }
57630
57631 #if SQLITE_DEBUG
57632 /*
57633 ** Page pParent is an internal (non-leaf) tree page. This function
@@ -57667,15 +58303,13 @@
57667 pCur->apPage[pCur->iPage-1],
57668 pCur->aiIdx[pCur->iPage-1],
57669 pCur->apPage[pCur->iPage]->pgno
57670 );
57671 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
57672
57673 releasePage(pCur->apPage[pCur->iPage]);
57674 pCur->iPage--;
57675 pCur->info.nSize = 0;
57676 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
 
57677 }
57678
57679 /*
57680 ** Move the cursor to point to the root page of its b-tree structure.
57681 **
@@ -57712,22 +58346,27 @@
57712 }
57713 sqlite3BtreeClearCursor(pCur);
57714 }
57715
57716 if( pCur->iPage>=0 ){
57717 while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
 
 
 
57718 }else if( pCur->pgnoRoot==0 ){
57719 pCur->eState = CURSOR_INVALID;
57720 return SQLITE_OK;
57721 }else{
 
57722 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
57723 (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
57724 if( rc!=SQLITE_OK ){
57725 pCur->eState = CURSOR_INVALID;
57726 return rc;
57727 }
57728 pCur->iPage = 0;
 
57729 }
57730 pRoot = pCur->apPage[0];
57731 assert( pRoot->pgno==pCur->pgnoRoot );
57732
57733 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
@@ -57926,11 +58565,11 @@
57926 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
57927
57928 /* If the cursor is already positioned at the point we are trying
57929 ** to move to, then just return without doing any work */
57930 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
57931 && pCur->apPage[0]->intKey
57932 ){
57933 if( pCur->info.nKey==intKey ){
57934 *pRes = 0;
57935 return SQLITE_OK;
57936 }
@@ -57961,11 +58600,12 @@
57961 if( pCur->eState==CURSOR_INVALID ){
57962 *pRes = -1;
57963 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
57964 return SQLITE_OK;
57965 }
57966 assert( pCur->apPage[0]->intKey || pIdxKey );
 
57967 for(;;){
57968 int lwr, upr, idx, c;
57969 Pgno chldPg;
57970 MemPage *pPage = pCur->apPage[pCur->iPage];
57971 u8 *pCell; /* Pointer to current cell in pPage */
@@ -57984,11 +58624,11 @@
57984 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
57985 pCur->aiIdx[pCur->iPage] = (u16)idx;
57986 if( xRecordCompare==0 ){
57987 for(;;){
57988 i64 nCellKey;
57989 pCell = findCell(pPage, idx) + pPage->childPtrSize;
57990 if( pPage->intKeyLeaf ){
57991 while( 0x80 <= *(pCell++) ){
57992 if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
57993 }
57994 }
@@ -58017,11 +58657,11 @@
58017 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
58018 }
58019 }else{
58020 for(;;){
58021 int nCell; /* Size of the pCell cell in bytes */
58022 pCell = findCell(pPage, idx) + pPage->childPtrSize;
58023
58024 /* The maximum supported page-size is 65536 bytes. This means that
58025 ** the maximum number of record bytes stored on an index B-Tree
58026 ** page is less than 16384 bytes and may be stored as a 2-byte
58027 ** varint. This information is used to attempt to avoid parsing
@@ -58053,11 +58693,11 @@
58053 ** up to two varints past the end of the buffer. An extra 18
58054 ** bytes of padding is allocated at the end of the buffer in
58055 ** case this happens. */
58056 void *pCellKey;
58057 u8 * const pCellBody = pCell - pPage->childPtrSize;
58058 btreeParseCellPtr(pPage, pCellBody, &pCur->info);
58059 nCell = (int)pCur->info.nKey;
58060 testcase( nCell<0 ); /* True if key size is 2^32 or more */
58061 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
58062 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
58063 testcase( nCell==2 ); /* Minimum legal index key size */
@@ -58356,12 +58996,11 @@
58356 ** has already been called on the new page.) The new page has also
58357 ** been referenced and the calling routine is responsible for calling
58358 ** sqlite3PagerUnref() on the new page when it is done.
58359 **
58360 ** SQLITE_OK is returned on success. Any other return value indicates
58361 ** an error. *ppPage and *pPgno are undefined in the event of an error.
58362 ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
58363 **
58364 ** If the "nearby" parameter is not 0, then an effort is made to
58365 ** locate a page close to the page number "nearby". This can be used in an
58366 ** attempt to keep related pages close to each other in the database file,
58367 ** which in turn can make database access faster.
@@ -58400,10 +59039,11 @@
58400 }
58401 if( n>0 ){
58402 /* There are pages on the freelist. Reuse one of those pages. */
58403 Pgno iTrunk;
58404 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
 
58405
58406 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
58407 ** shows that the page 'nearby' is somewhere on the free-list, then
58408 ** the entire-list will be searched for that page.
58409 */
@@ -58448,11 +59088,11 @@
58448 ** stores the page number of the first page of the freelist, or zero if
58449 ** the freelist is empty. */
58450 iTrunk = get4byte(&pPage1->aData[32]);
58451 }
58452 testcase( iTrunk==mxPage );
58453 if( iTrunk>mxPage ){
58454 rc = SQLITE_CORRUPT_BKPT;
58455 }else{
58456 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
58457 }
58458 if( rc ){
@@ -58601,10 +59241,11 @@
58601 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
58602 if( rc==SQLITE_OK ){
58603 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
58604 if( rc!=SQLITE_OK ){
58605 releasePage(*ppPage);
 
58606 }
58607 }
58608 searchList = 0;
58609 }
58610 }
@@ -58842,11 +59483,11 @@
58842 int rc;
58843 int nOvfl;
58844 u32 ovflPageSize;
58845
58846 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
58847 btreeParseCellPtr(pPage, pCell, &info);
58848 *pnSize = info.nSize;
58849 if( info.iOverflow==0 ){
58850 return SQLITE_OK; /* No overflow pages. Return without doing anything */
58851 }
58852 if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
@@ -58954,13 +59595,11 @@
58954 if( pPage->intKey ){
58955 pSrc = pData;
58956 nSrc = nData;
58957 nData = 0;
58958 }else{
58959 if( NEVER(nKey>0x7fffffff || pKey==0) ){
58960 return SQLITE_CORRUPT_BKPT;
58961 }
58962 nPayload = (int)nKey;
58963 pSrc = pKey;
58964 nSrc = (int)nKey;
58965 }
58966 if( nPayload<=pPage->maxLocal ){
@@ -58996,11 +59635,11 @@
58996 ** were computed correctly.
58997 */
58998 #if SQLITE_DEBUG
58999 {
59000 CellInfo info;
59001 btreeParseCellPtr(pPage, pCell, &info);
59002 assert( nHeader=(int)(info.pPayload - pCell) );
59003 assert( info.nKey==nKey );
59004 assert( *pnSize == info.nSize );
59005 assert( spaceLeft == info.nLocal );
59006 assert( pPrior == &pCell[info.iOverflow] );
@@ -59166,14 +59805,12 @@
59166 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
59167 int *pRC /* Read and write return code from here */
59168 ){
59169 int idx = 0; /* Where to write new cell content in data[] */
59170 int j; /* Loop counter */
59171 int end; /* First byte past the last cell pointer in data[] */
59172 int ins; /* Index in data[] where new cell pointer is inserted */
59173 int cellOffset; /* Address of first cell pointer in data[] */
59174 u8 *data; /* The content of the whole page */
 
59175
59176 if( *pRC ) return;
59177
59178 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
59179 assert( MX_CELL(pPage->pBt)<=10921 );
@@ -59184,11 +59821,11 @@
59184 /* The cell should normally be sized correctly. However, when moving a
59185 ** malformed cell from a leaf page to an interior page, if the cell size
59186 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
59187 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
59188 ** the term after the || in the following assert(). */
59189 assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
59190 if( pPage->nOverflow || sz+2>pPage->nFree ){
59191 if( pTemp ){
59192 memcpy(pTemp, pCell, sz);
59193 pCell = pTemp;
59194 }
@@ -59197,36 +59834,46 @@
59197 }
59198 j = pPage->nOverflow++;
59199 assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
59200 pPage->apOvfl[j] = pCell;
59201 pPage->aiOvfl[j] = (u16)i;
 
 
 
 
 
 
 
 
59202 }else{
59203 int rc = sqlite3PagerWrite(pPage->pDbPage);
59204 if( rc!=SQLITE_OK ){
59205 *pRC = rc;
59206 return;
59207 }
59208 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
59209 data = pPage->aData;
59210 cellOffset = pPage->cellOffset;
59211 end = cellOffset + 2*pPage->nCell;
59212 ins = cellOffset + 2*i;
59213 rc = allocateSpace(pPage, sz, &idx);
59214 if( rc ){ *pRC = rc; return; }
59215 /* The allocateSpace() routine guarantees the following two properties
59216 ** if it returns success */
59217 assert( idx >= end+2 );
 
59218 assert( idx+sz <= (int)pPage->pBt->usableSize );
59219 pPage->nCell++;
59220 pPage->nFree -= (u16)(2 + sz);
59221 memcpy(&data[idx], pCell, sz);
59222 if( iChild ){
59223 put4byte(&data[idx], iChild);
59224 }
59225 memmove(&data[ins+2], &data[ins], end-ins);
59226 put2byte(&data[ins], idx);
59227 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
 
 
 
 
59228 #ifndef SQLITE_OMIT_AUTOVACUUM
59229 if( pPage->pBt->autoVacuum ){
59230 /* The cell may contain a pointer to an overflow page. If so, write
59231 ** the entry for the overflow page into the pointer map.
59232 */
@@ -59233,10 +59880,56 @@
59233 ptrmapPutOvflPtr(pPage, pCell, pRC);
59234 }
59235 #endif
59236 }
59237 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59238
59239 /*
59240 ** Array apCell[] contains pointers to nCell b-tree page cells. The
59241 ** szCell[] array contains the size in bytes of each cell. This function
59242 ** replaces the current contents of page pPg with the contents of the cell
@@ -59247,11 +59940,11 @@
59247 ** such cells before overwriting the page data.
59248 **
59249 ** The MemPage.nFree field is invalidated by this function. It is the
59250 ** responsibility of the caller to set it correctly.
59251 */
59252 static void rebuildPage(
59253 MemPage *pPg, /* Edit this page */
59254 int nCell, /* Final number of cells on page */
59255 u8 **apCell, /* Array of cells */
59256 u16 *szCell /* Array of cell sizes */
59257 ){
@@ -59272,15 +59965,16 @@
59272 u8 *pCell = apCell[i];
59273 if( pCell>aData && pCell<pEnd ){
59274 pCell = &pTmp[pCell - aData];
59275 }
59276 pData -= szCell[i];
59277 memcpy(pData, pCell, szCell[i]);
59278 put2byte(pCellptr, (pData - aData));
59279 pCellptr += 2;
59280 assert( szCell[i]==cellSizePtr(pPg, pCell) || CORRUPT_DB );
59281 testcase( szCell[i]==cellSizePtr(pPg,pCell) );
 
 
59282 }
59283
59284 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
59285 pPg->nCell = nCell;
59286 pPg->nOverflow = 0;
@@ -59287,10 +59981,11 @@
59287
59288 put2byte(&aData[hdr+1], 0);
59289 put2byte(&aData[hdr+3], pPg->nCell);
59290 put2byte(&aData[hdr+5], pData - aData);
59291 aData[hdr+7] = 0x00;
 
59292 }
59293
59294 /*
59295 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
59296 ** contains the size in bytes of each such cell. This function attempts to
@@ -59319,29 +60014,29 @@
59319 static int pageInsertArray(
59320 MemPage *pPg, /* Page to add cells to */
59321 u8 *pBegin, /* End of cell-pointer array */
59322 u8 **ppData, /* IN/OUT: Page content -area pointer */
59323 u8 *pCellptr, /* Pointer to cell-pointer area */
 
59324 int nCell, /* Number of cells to add to pPg */
59325 u8 **apCell, /* Array of cells */
59326 u16 *szCell /* Array of cell sizes */
59327 ){
59328 int i;
59329 u8 *aData = pPg->aData;
59330 u8 *pData = *ppData;
59331 const int bFreelist = aData[1] || aData[2];
59332 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
59333 for(i=0; i<nCell; i++){
59334 int sz = szCell[i];
59335 int rc;
59336 u8 *pSlot;
59337 if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){
 
59338 pData -= sz;
59339 if( pData<pBegin ) return 1;
59340 pSlot = pData;
59341 }
59342 memcpy(pSlot, apCell[i], sz);
59343 put2byte(pCellptr, (pSlot - aData));
59344 pCellptr += 2;
59345 }
59346 *ppData = pData;
59347 return 0;
@@ -59356,26 +60051,31 @@
59356 **
59357 ** This function returns the total number of cells added to the free-list.
59358 */
59359 static int pageFreeArray(
59360 MemPage *pPg, /* Page to edit */
 
59361 int nCell, /* Cells to delete */
59362 u8 **apCell, /* Array of cells */
59363 u16 *szCell /* Array of cell sizes */
59364 ){
59365 u8 * const aData = pPg->aData;
59366 u8 * const pEnd = &aData[pPg->pBt->usableSize];
59367 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
59368 int nRet = 0;
59369 int i;
 
59370 u8 *pFree = 0;
59371 int szFree = 0;
59372
59373 for(i=0; i<nCell; i++){
59374 u8 *pCell = apCell[i];
59375 if( pCell>=pStart && pCell<pEnd ){
59376 int sz = szCell[i];
 
 
 
 
59377 if( pFree!=(pCell + sz) ){
59378 if( pFree ){
59379 assert( pFree>aData && (pFree - aData)<65536 );
59380 freeSpace(pPg, (u16)(pFree - aData), szFree);
59381 }
@@ -59406,17 +60106,16 @@
59406 ** the correct cells after being balanced.
59407 **
59408 ** The pPg->nFree field is invalid when this function returns. It is the
59409 ** responsibility of the caller to set it correctly.
59410 */
59411 static void editPage(
59412 MemPage *pPg, /* Edit this page */
59413 int iOld, /* Index of first cell currently on page */
59414 int iNew, /* Index of new first cell on page */
59415 int nNew, /* Final number of cells on page */
59416 u8 **apCell, /* Array of cells */
59417 u16 *szCell /* Array of cell sizes */
59418 ){
59419 u8 * const aData = pPg->aData;
59420 const int hdr = pPg->hdrOffset;
59421 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
59422 int nCell = pPg->nCell; /* Cells stored on pPg */
@@ -59431,20 +60130,16 @@
59431 memcpy(pTmp, aData, pPg->pBt->usableSize);
59432 #endif
59433
59434 /* Remove cells from the start and end of the page */
59435 if( iOld<iNew ){
59436 int nShift = pageFreeArray(
59437 pPg, iNew-iOld, &apCell[iOld], &szCell[iOld]
59438 );
59439 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
59440 nCell -= nShift;
59441 }
59442 if( iNewEnd < iOldEnd ){
59443 nCell -= pageFreeArray(
59444 pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd]
59445 );
59446 }
59447
59448 pData = &aData[get2byteNotZero(&aData[hdr+5])];
59449 if( pData<pBegin ) goto editpage_fail;
59450
@@ -59454,11 +60149,11 @@
59454 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
59455 pCellptr = pPg->aCellIdx;
59456 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
59457 if( pageInsertArray(
59458 pPg, pBegin, &pData, pCellptr,
59459 nAdd, &apCell[iNew], &szCell[iNew]
59460 ) ) goto editpage_fail;
59461 nCell += nAdd;
59462 }
59463
59464 /* Add any overflow cells */
@@ -59468,20 +60163,20 @@
59468 pCellptr = &pPg->aCellIdx[iCell * 2];
59469 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
59470 nCell++;
59471 if( pageInsertArray(
59472 pPg, pBegin, &pData, pCellptr,
59473 1, &apCell[iCell + iNew], &szCell[iCell + iNew]
59474 ) ) goto editpage_fail;
59475 }
59476 }
59477
59478 /* Append cells to the end of the page */
59479 pCellptr = &pPg->aCellIdx[nCell*2];
59480 if( pageInsertArray(
59481 pPg, pBegin, &pData, pCellptr,
59482 nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell]
59483 ) ) goto editpage_fail;
59484
59485 pPg->nCell = nNew;
59486 pPg->nOverflow = 0;
59487
@@ -59488,23 +60183,25 @@
59488 put2byte(&aData[hdr+3], pPg->nCell);
59489 put2byte(&aData[hdr+5], pData - aData);
59490
59491 #ifdef SQLITE_DEBUG
59492 for(i=0; i<nNew && !CORRUPT_DB; i++){
59493 u8 *pCell = apCell[i+iNew];
59494 int iOff = get2byte(&pPg->aCellIdx[i*2]);
59495 if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
59496 pCell = &pTmp[pCell - aData];
59497 }
59498 assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) );
 
59499 }
59500 #endif
59501
59502 return;
59503 editpage_fail:
59504 /* Unable to edit this page. Rebuild it from scratch instead. */
59505 rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]);
 
59506 }
59507
59508 /*
59509 ** The following parameters determine how many adjacent pages get involved
59510 ** in a balancing operation. NN is the number of neighbors on either side
@@ -59566,17 +60263,18 @@
59566
59567 if( rc==SQLITE_OK ){
59568
59569 u8 *pOut = &pSpace[4];
59570 u8 *pCell = pPage->apOvfl[0];
59571 u16 szCell = cellSizePtr(pPage, pCell);
59572 u8 *pStop;
59573
59574 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
59575 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
59576 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
59577 rebuildPage(pNew, 1, &pCell, &szCell);
 
59578 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
59579
59580 /* If this is an auto-vacuum database, update the pointer map
59581 ** with entries for the new page, and any pointer from the
59582 ** cell on the page to an overflow page. If either of these
@@ -59645,11 +60343,11 @@
59645 for(j=0; j<pPage->nCell; j++){
59646 CellInfo info;
59647 u8 *z;
59648
59649 z = findCell(pPage, j);
59650 btreeParseCellPtr(pPage, z, &info);
59651 if( info.iOverflow ){
59652 Pgno ovfl = get4byte(&z[info.iOverflow]);
59653 ptrmapGet(pBt, ovfl, &e, &n);
59654 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
59655 }
@@ -59776,11 +60474,10 @@
59776 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
59777 int isRoot, /* True if pParent is a root-page */
59778 int bBulk /* True if this call is part of a bulk load */
59779 ){
59780 BtShared *pBt; /* The whole database */
59781 int nCell = 0; /* Number of cells in apCell[] */
59782 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
59783 int nNew = 0; /* Number of pages in apNew[] */
59784 int nOld; /* Number of pages in apOld[] */
59785 int i, j, k; /* Loop counters */
59786 int nxDiv; /* Next divider slot in pParent->aCell[] */
@@ -59787,31 +60484,31 @@
59787 int rc = SQLITE_OK; /* The return code */
59788 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
59789 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
59790 int usableSpace; /* Bytes in pPage beyond the header */
59791 int pageFlags; /* Value of pPage->aData[0] */
59792 int subtotal; /* Subtotal of bytes in cells on one page */
59793 int iSpace1 = 0; /* First unused byte of aSpace1[] */
59794 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
59795 int szScratch; /* Size of scratch memory requested */
59796 MemPage *apOld[NB]; /* pPage and up to two siblings */
59797 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
59798 u8 *pRight; /* Location in parent of right-sibling pointer */
59799 u8 *apDiv[NB-1]; /* Divider cells in pParent */
59800 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
59801 int cntOld[NB+2]; /* Old index in aCell[] after i-th page */
59802 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
59803 u8 **apCell = 0; /* All cells begin balanced */
59804 u16 *szCell; /* Local size of all cells in apCell[] */
59805 u8 *aSpace1; /* Space for copies of dividers cells */
59806 Pgno pgno; /* Temp var to store a page number in */
59807 u8 abDone[NB+2]; /* True after i'th new page is populated */
59808 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
59809 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
59810 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
 
59811
59812 memset(abDone, 0, sizeof(abDone));
 
 
59813 pBt = pParent->pBt;
59814 assert( sqlite3_mutex_held(pBt->mutex) );
59815 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
59816
59817 #if 0
@@ -59861,11 +60558,11 @@
59861 }else{
59862 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
59863 }
59864 pgno = get4byte(pRight);
59865 while( 1 ){
59866 rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
59867 if( rc ){
59868 memset(apOld, 0, (i+1)*sizeof(MemPage*));
59869 goto balance_cleanup;
59870 }
59871 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
@@ -59872,16 +60569,16 @@
59872 if( (i--)==0 ) break;
59873
59874 if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
59875 apDiv[i] = pParent->apOvfl[0];
59876 pgno = get4byte(apDiv[i]);
59877 szNew[i] = cellSizePtr(pParent, apDiv[i]);
59878 pParent->nOverflow = 0;
59879 }else{
59880 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
59881 pgno = get4byte(apDiv[i]);
59882 szNew[i] = cellSizePtr(pParent, apDiv[i]);
59883
59884 /* Drop the cell from the parent page. apDiv[i] still points to
59885 ** the cell within the parent, even though it has been dropped.
59886 ** This is safe because dropping a cell only overwrites the first
59887 ** four bytes of it, and this function does not need the first
@@ -59916,142 +60613,205 @@
59916
59917 /*
59918 ** Allocate space for memory structures
59919 */
59920 szScratch =
59921 nMaxCells*sizeof(u8*) /* apCell */
59922 + nMaxCells*sizeof(u16) /* szCell */
59923 + pBt->pageSize; /* aSpace1 */
59924
59925 /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
59926 ** that is more than 6 times the database page size. */
59927 assert( szScratch<=6*(int)pBt->pageSize );
59928 apCell = sqlite3ScratchMalloc( szScratch );
59929 if( apCell==0 ){
59930 rc = SQLITE_NOMEM;
59931 goto balance_cleanup;
59932 }
59933 szCell = (u16*)&apCell[nMaxCells];
59934 aSpace1 = (u8*)&szCell[nMaxCells];
59935 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
59936
59937 /*
59938 ** Load pointers to all cells on sibling pages and the divider cells
59939 ** into the local apCell[] array. Make copies of the divider cells
59940 ** into space obtained from aSpace1[]. The divider cells have already
59941 ** been removed from pParent.
59942 **
59943 ** If the siblings are on leaf pages, then the child pointers of the
59944 ** divider cells are stripped from the cells before they are copied
59945 ** into aSpace1[]. In this way, all cells in apCell[] are without
59946 ** child pointers. If siblings are not leaves, then all cell in
59947 ** apCell[] include child pointers. Either way, all cells in apCell[]
59948 ** are alike.
59949 **
59950 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
59951 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
59952 */
59953 leafCorrection = apOld[0]->leaf*4;
59954 leafData = apOld[0]->intKeyLeaf;
 
59955 for(i=0; i<nOld; i++){
59956 int limit;
59957 MemPage *pOld = apOld[i];
 
 
 
 
 
59958
59959 /* Verify that all sibling pages are of the same "type" (table-leaf,
59960 ** table-interior, index-leaf, or index-interior).
59961 */
59962 if( pOld->aData[0]!=apOld[0]->aData[0] ){
59963 rc = SQLITE_CORRUPT_BKPT;
59964 goto balance_cleanup;
59965 }
59966
59967 limit = pOld->nCell+pOld->nOverflow;
59968 if( pOld->nOverflow>0 ){
59969 for(j=0; j<limit; j++){
59970 assert( nCell<nMaxCells );
59971 apCell[nCell] = findOverflowCell(pOld, j);
59972 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
59973 nCell++;
59974 }
59975 }else{
59976 u8 *aData = pOld->aData;
59977 u16 maskPage = pOld->maskPage;
59978 u16 cellOffset = pOld->cellOffset;
59979 for(j=0; j<limit; j++){
59980 assert( nCell<nMaxCells );
59981 apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
59982 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
59983 nCell++;
59984 }
59985 }
59986 cntOld[i] = nCell;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59987 if( i<nOld-1 && !leafData){
59988 u16 sz = (u16)szNew[i];
59989 u8 *pTemp;
59990 assert( nCell<nMaxCells );
59991 szCell[nCell] = sz;
59992 pTemp = &aSpace1[iSpace1];
59993 iSpace1 += sz;
59994 assert( sz<=pBt->maxLocal+23 );
59995 assert( iSpace1 <= (int)pBt->pageSize );
59996 memcpy(pTemp, apDiv[i], sz);
59997 apCell[nCell] = pTemp+leafCorrection;
59998 assert( leafCorrection==0 || leafCorrection==4 );
59999 szCell[nCell] = szCell[nCell] - leafCorrection;
60000 if( !pOld->leaf ){
60001 assert( leafCorrection==0 );
60002 assert( pOld->hdrOffset==0 );
60003 /* The right pointer of the child page pOld becomes the left
60004 ** pointer of the divider cell */
60005 memcpy(apCell[nCell], &pOld->aData[8], 4);
60006 }else{
60007 assert( leafCorrection==4 );
60008 while( szCell[nCell]<4 ){
60009 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
60010 ** does exist, pad it with 0x00 bytes. */
60011 assert( szCell[nCell]==3 || CORRUPT_DB );
60012 assert( apCell[nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
60013 aSpace1[iSpace1++] = 0x00;
60014 szCell[nCell]++;
60015 }
60016 }
60017 nCell++;
60018 }
60019 }
60020
60021 /*
60022 ** Figure out the number of pages needed to hold all nCell cells.
60023 ** Store this number in "k". Also compute szNew[] which is the total
60024 ** size of all cells on the i-th page and cntNew[] which is the index
60025 ** in apCell[] of the cell that divides page i from page i+1.
60026 ** cntNew[k] should equal nCell.
60027 **
60028 ** Values computed by this block:
60029 **
60030 ** k: The total number of sibling pages
60031 ** szNew[i]: Spaced used on the i-th sibling page.
60032 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
60033 ** the right of the i-th sibling page.
60034 ** usableSpace: Number of bytes of space available on each sibling.
60035 **
60036 */
60037 usableSpace = pBt->usableSize - 12 + leafCorrection;
60038 for(subtotal=k=i=0; i<nCell; i++){
60039 assert( i<nMaxCells );
60040 subtotal += szCell[i] + 2;
60041 if( subtotal > usableSpace ){
60042 szNew[k] = subtotal - szCell[i] - 2;
60043 cntNew[k] = i;
60044 if( leafData ){ i--; }
60045 subtotal = 0;
60046 k++;
60047 if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
60048 }
60049 }
60050 szNew[k] = subtotal;
60051 cntNew[k] = nCell;
60052 k++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60053
60054 /*
60055 ** The packing computed by the previous block is biased toward the siblings
60056 ** on the left side (siblings with smaller keys). The left siblings are
60057 ** always nearly full, while the right-most sibling might be nearly empty.
@@ -60068,23 +60828,31 @@
60068 int r; /* Index of right-most cell in left sibling */
60069 int d; /* Index of first cell to the left of right sibling */
60070
60071 r = cntNew[i-1] - 1;
60072 d = r + 1 - leafData;
60073 assert( d<nMaxCells );
60074 assert( r<nMaxCells );
60075 while( szRight==0
60076 || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2))
60077 ){
60078 szRight += szCell[d] + 2;
60079 szLeft -= szCell[r] + 2;
60080 cntNew[i-1]--;
60081 r = cntNew[i-1] - 1;
60082 d = r + 1 - leafData;
60083 }
 
 
 
 
60084 szNew[i] = szRight;
60085 szNew[i-1] = szLeft;
 
 
 
 
60086 }
60087
60088 /* Sanity check: For a non-corrupt database file one of the follwing
60089 ** must be true:
60090 ** (1) We found one or more cells (cntNew[0])>0), or
@@ -60116,11 +60884,11 @@
60116 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
60117 if( rc ) goto balance_cleanup;
60118 zeroPage(pNew, pageFlags);
60119 apNew[i] = pNew;
60120 nNew++;
60121 cntOld[i] = nCell;
60122
60123 /* Set the pointer-map entry for the new sibling page. */
60124 if( ISAUTOVACUUM ){
60125 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
60126 if( rc!=SQLITE_OK ){
@@ -60221,12 +60989,12 @@
60221 int cntOldNext = pNew->nCell + pNew->nOverflow;
60222 int usableSize = pBt->usableSize;
60223 int iNew = 0;
60224 int iOld = 0;
60225
60226 for(i=0; i<nCell; i++){
60227 u8 *pCell = apCell[i];
60228 if( i==cntOldNext ){
60229 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
60230 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
60231 aOld = pOld->aData;
60232 }
@@ -60247,13 +61015,14 @@
60247 || pCell>=&aOld[usableSize]
60248 ){
60249 if( !leafCorrection ){
60250 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
60251 }
60252 if( szCell[i]>pNew->minLocal ){
60253 ptrmapPutOvflPtr(pNew, pCell, &rc);
60254 }
 
60255 }
60256 }
60257 }
60258
60259 /* Insert new divider cells into pParent. */
@@ -60263,24 +61032,25 @@
60263 int sz;
60264 MemPage *pNew = apNew[i];
60265 j = cntNew[i];
60266
60267 assert( j<nMaxCells );
60268 pCell = apCell[j];
60269 sz = szCell[j] + leafCorrection;
 
60270 pTemp = &aOvflSpace[iOvflSpace];
60271 if( !pNew->leaf ){
60272 memcpy(&pNew->aData[8], pCell, 4);
60273 }else if( leafData ){
60274 /* If the tree is a leaf-data tree, and the siblings are leaves,
60275 ** then there is no divider cell in apCell[]. Instead, the divider
60276 ** cell consists of the integer key for the right-most cell of
60277 ** the sibling-page assembled above only.
60278 */
60279 CellInfo info;
60280 j--;
60281 btreeParseCellPtr(pNew, apCell[j], &info);
60282 pCell = pTemp;
60283 sz = 4 + putVarint(&pCell[4], info.nKey);
60284 pTemp = 0;
60285 }else{
60286 pCell -= 4;
@@ -60293,13 +61063,13 @@
60293 **
60294 ** Note that this can never happen in an SQLite data file, as all
60295 ** cells are at least 4 bytes. It only happens in b-trees used
60296 ** to evaluate "IN (SELECT ...)" and similar clauses.
60297 */
60298 if( szCell[j]==4 ){
60299 assert(leafCorrection==4);
60300 sz = cellSizePtr(pParent, pCell);
60301 }
60302 }
60303 iOvflSpace += sz;
60304 assert( sz<=pBt->maxLocal+23 );
60305 assert( iOvflSpace <= (int)pBt->pageSize );
@@ -60351,16 +61121,17 @@
60351
60352 if( iPg==0 ){
60353 iNew = iOld = 0;
60354 nNewCell = cntNew[0];
60355 }else{
60356 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell;
60357 iNew = cntNew[iPg-1] + !leafData;
60358 nNewCell = cntNew[iPg] - iNew;
60359 }
60360
60361 editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell);
 
60362 abDone[iPg]++;
60363 apNew[iPg]->nFree = usableSpace-szNew[iPg];
60364 assert( apNew[iPg]->nOverflow==0 );
60365 assert( apNew[iPg]->nCell==nNewCell );
60366 }
@@ -60407,11 +61178,11 @@
60407 }
60408 }
60409
60410 assert( pParent->isInit );
60411 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
60412 nOld, nNew, nCell));
60413
60414 /* Free any old pages that were not reused as new pages.
60415 */
60416 for(i=nNew; i<nOld; i++){
60417 freePage(apOld[i], &rc);
@@ -60430,11 +61201,11 @@
60430
60431 /*
60432 ** Cleanup before returning.
60433 */
60434 balance_cleanup:
60435 sqlite3ScratchFree(apCell);
60436 for(i=0; i<nOld; i++){
60437 releasePage(apOld[i]);
60438 }
60439 for(i=0; i<nNew; i++){
60440 releasePage(apNew[i]);
@@ -60705,28 +61476,32 @@
60705 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
60706 ** that the cursor is already where it needs to be and returns without
60707 ** doing any work. To avoid thwarting these optimizations, it is important
60708 ** not to clear the cursor here.
60709 */
60710 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
60711 if( rc ) return rc;
 
 
60712
60713 if( pCur->pKeyInfo==0 ){
 
60714 /* If this is an insert into a table b-tree, invalidate any incrblob
60715 ** cursors open on the row being replaced */
60716 invalidateIncrblobCursors(p, nKey, 0);
60717
60718 /* If the cursor is currently on the last row and we are appending a
60719 ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
60720 ** call */
60721 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
60722 && pCur->info.nKey==nKey-1 ){
60723 loc = -1;
 
 
 
60724 }
60725 }
60726
60727 if( !loc ){
60728 rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
60729 if( rc ) return rc;
60730 }
60731 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
60732
@@ -60740,11 +61515,11 @@
60740 assert( pPage->isInit );
60741 newCell = pBt->pTmpSpace;
60742 assert( newCell!=0 );
60743 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
60744 if( rc ) goto end_insert;
60745 assert( szNew==cellSizePtr(pPage, newCell) );
60746 assert( szNew <= MX_CELL_SIZE(pBt) );
60747 idx = pCur->aiIdx[pCur->iPage];
60748 if( loc==0 ){
60749 u16 szOld;
60750 assert( idx<pPage->nCell );
@@ -60824,16 +61599,12 @@
60824 assert( pBt->inTransaction==TRANS_WRITE );
60825 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
60826 assert( pCur->curFlags & BTCF_WriteFlag );
60827 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
60828 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
60829
60830 if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
60831 || NEVER(pCur->eState!=CURSOR_VALID)
60832 ){
60833 return SQLITE_ERROR; /* Something has gone awry. */
60834 }
60835
60836 iCellDepth = pCur->iPage;
60837 iCellIdx = pCur->aiIdx[iCellDepth];
60838 pPage = pCur->apPage[iCellDepth];
60839 pCell = findCell(pPage, iCellIdx);
@@ -60854,12 +61625,14 @@
60854 /* Save the positions of any other cursors open on this table before
60855 ** making any modifications. Make the page containing the entry to be
60856 ** deleted writable. Then free any overflow pages associated with the
60857 ** entry and finally remove the cell itself from within the page.
60858 */
60859 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
60860 if( rc ) return rc;
 
 
60861
60862 /* If this is a delete operation to remove a row from a table b-tree,
60863 ** invalidate any incrblob cursors open on the row being deleted. */
60864 if( pCur->pKeyInfo==0 ){
60865 invalidateIncrblobCursors(p, pCur->info.nKey, 0);
@@ -60882,11 +61655,11 @@
60882 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
60883 unsigned char *pTmp;
60884
60885 pCell = findCell(pLeaf, pLeaf->nCell-1);
60886 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
60887 nCell = cellSizePtr(pLeaf, pCell);
60888 assert( MX_CELL_SIZE(pBt) >= nCell );
60889 pTmp = pBt->pTmpSpace;
60890 assert( pTmp!=0 );
60891 rc = sqlite3PagerWrite(pLeaf->pDbPage);
60892 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
@@ -61104,11 +61877,11 @@
61104
61105 assert( sqlite3_mutex_held(pBt->mutex) );
61106 if( pgno>btreePagecount(pBt) ){
61107 return SQLITE_CORRUPT_BKPT;
61108 }
61109 rc = getAndInitPage(pBt, pgno, &pPage, 0);
61110 if( rc ) return rc;
61111 if( pPage->bBusy ){
61112 rc = SQLITE_CORRUPT_BKPT;
61113 goto cleardatabasepage_out;
61114 }
@@ -61776,11 +62549,11 @@
61776 */
61777 pCheck->zPfx = "On tree page %d cell %d: ";
61778 pCheck->v1 = iPage;
61779 pCheck->v2 = i;
61780 pCell = findCell(pPage,i);
61781 btreeParseCellPtr(pPage, pCell, &info);
61782 sz = info.nPayload;
61783 /* For intKey pages, check that the keys are in order.
61784 */
61785 if( pPage->intKey ){
61786 if( i==0 ){
@@ -61891,14 +62664,14 @@
61891 ** immediately follows the b-tree page header. */
61892 cellStart = hdr + 12 - 4*pPage->leaf;
61893 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
61894 ** integer offsets to the cell contents. */
61895 for(i=0; i<nCell; i++){
61896 int pc = get2byte(&data[cellStart+i*2]);
61897 u32 size = 65536;
61898 if( pc<=usableSize-4 ){
61899 size = cellSizePtr(pPage, &data[pc]);
61900 }
61901 if( (int)(pc+size-1)>=usableSize ){
61902 pCheck->zPfx = 0;
61903 checkAppendMsg(pCheck,
61904 "Corruption detected in cell %d on page %d",i,iPage);
@@ -62292,10 +63065,11 @@
62292 /*
62293 ** Mark this cursor as an incremental blob cursor.
62294 */
62295 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
62296 pCur->curFlags |= BTCF_Incrblob;
 
62297 }
62298 #endif
62299
62300 /*
62301 ** Set both the "read version" (single byte at byte offset 18) and
@@ -63739,11 +64513,11 @@
63739 ** used (for example) to implement the SQL "cast()" operator.
63740 */
63741 SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
63742 if( pMem->flags & MEM_Null ) return;
63743 switch( aff ){
63744 case SQLITE_AFF_NONE: { /* Really a cast to BLOB */
63745 if( (pMem->flags & MEM_Blob)==0 ){
63746 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
63747 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
63748 MemSetTypeFlag(pMem, MEM_Blob);
63749 }else{
@@ -63928,14 +64702,19 @@
63928 ** Make an shallow copy of pFrom into pTo. Prior contents of
63929 ** pTo are freed. The pFrom->z field is not duplicated. If
63930 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
63931 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
63932 */
 
 
 
 
 
63933 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
63934 assert( (pFrom->flags & MEM_RowSet)==0 );
63935 assert( pTo->db==pFrom->db );
63936 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
63937 memcpy(pTo, pFrom, MEMCELLSIZE);
63938 if( (pFrom->flags&MEM_Static)==0 ){
63939 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
63940 assert( srcType==MEM_Ephem || srcType==MEM_Static );
63941 pTo->flags |= srcType;
@@ -64097,10 +64876,36 @@
64097 ** destroyed.
64098 **
64099 ** If this routine fails for any reason (malloc returns NULL or unable
64100 ** to read from the disk) then the pMem is left in an inconsistent state.
64101 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64102 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
64103 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
64104 u32 offset, /* Offset from the start of data to return bytes from. */
64105 u32 amt, /* Number of bytes to return. */
64106 int key, /* If true, retrieve from the btree key, not data. */
@@ -64126,26 +64931,11 @@
64126 if( offset+amt<=available ){
64127 pMem->z = &zData[offset];
64128 pMem->flags = MEM_Blob|MEM_Ephem;
64129 pMem->n = (int)amt;
64130 }else{
64131 pMem->flags = MEM_Null;
64132 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
64133 if( key ){
64134 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
64135 }else{
64136 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
64137 }
64138 if( rc==SQLITE_OK ){
64139 pMem->z[amt] = 0;
64140 pMem->z[amt+1] = 0;
64141 pMem->flags = MEM_Blob|MEM_Term;
64142 pMem->n = (int)amt;
64143 }else{
64144 sqlite3VdbeMemRelease(pMem);
64145 }
64146 }
64147 }
64148
64149 return rc;
64150 }
64151
@@ -64462,11 +65252,11 @@
64462 }else{
64463 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
64464 if( zVal==0 ) goto no_mem;
64465 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
64466 }
64467 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
64468 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
64469 }else{
64470 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
64471 }
64472 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
@@ -64829,23 +65619,32 @@
64829 sqlite3VdbeMemRelease((Mem *)v);
64830 sqlite3DbFree(((Mem*)v)->db, v);
64831 }
64832
64833 /*
64834 ** Return the number of bytes in the sqlite3_value object assuming
64835 ** that it uses the encoding "enc"
 
64836 */
 
 
 
64837 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
64838 Mem *p = (Mem*)pVal;
64839 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
 
 
 
 
64840 if( p->flags & MEM_Zero ){
64841 return p->n + p->u.nZero;
64842 }else{
64843 return p->n;
64844 }
64845 }
64846 return 0;
 
64847 }
64848
64849 /************** End of vdbemem.c *********************************************/
64850 /************** Begin file vdbeaux.c *****************************************/
64851 /*
@@ -65078,10 +65877,27 @@
65078 ){
65079 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
65080 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
65081 return addr;
65082 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65083
65084 /*
65085 ** Add an OP_ParseSchema opcode. This routine is broken out from
65086 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
65087 ** as having been used.
@@ -65243,10 +66059,11 @@
65243 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
65244 ** * OP_Destroy
65245 ** * OP_VUpdate
65246 ** * OP_VRename
65247 ** * OP_FkCounter with P2==0 (immediate foreign key constraint)
 
65248 **
65249 ** Then check that the value of Parse.mayAbort is true if an
65250 ** ABORT may be thrown, or false otherwise. Return true if it does
65251 ** match, or false otherwise. This function is intended to be used as
65252 ** part of an assert statement in the compiler. Similar to:
@@ -65254,10 +66071,12 @@
65254 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
65255 */
65256 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
65257 int hasAbort = 0;
65258 int hasFkCounter = 0;
 
 
65259 Op *pOp;
65260 VdbeOpIter sIter;
65261 memset(&sIter, 0, sizeof(sIter));
65262 sIter.v = v;
65263
@@ -65268,10 +66087,12 @@
65268 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
65269 ){
65270 hasAbort = 1;
65271 break;
65272 }
 
 
65273 #ifndef SQLITE_OMIT_FOREIGN_KEY
65274 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
65275 hasFkCounter = 1;
65276 }
65277 #endif
@@ -65281,11 +66102,12 @@
65281 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
65282 ** If malloc failed, then the while() loop above may not have iterated
65283 ** through all opcodes and hasAbort may be set incorrectly. Return
65284 ** true for this case to prevent the assert() in the callers frame
65285 ** from failing. */
65286 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter );
 
65287 }
65288 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
65289
65290 /*
65291 ** Loop through the program looking for P2 values that are negative
@@ -65312,15 +66134,10 @@
65312 u8 opcode = pOp->opcode;
65313
65314 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
65315 ** cases from this switch! */
65316 switch( opcode ){
65317 case OP_Function:
65318 case OP_AggStep: {
65319 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
65320 break;
65321 }
65322 case OP_Transaction: {
65323 if( pOp->p2!=0 ) p->readOnly = 0;
65324 /* fall thru */
65325 }
65326 case OP_AutoCommit:
@@ -65560,10 +66377,14 @@
65560 */
65561 static void freeP4(sqlite3 *db, int p4type, void *p4){
65562 if( p4 ){
65563 assert( db );
65564 switch( p4type ){
 
 
 
 
65565 case P4_REAL:
65566 case P4_INT64:
65567 case P4_DYNAMIC:
65568 case P4_INTARRAY: {
65569 sqlite3DbFree(db, p4);
@@ -65944,10 +66765,17 @@
65944 case P4_FUNCDEF: {
65945 FuncDef *pDef = pOp->p4.pFunc;
65946 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
65947 break;
65948 }
 
 
 
 
 
 
 
65949 case P4_INT64: {
65950 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
65951 break;
65952 }
65953 case P4_INT32: {
@@ -66064,24 +66892,27 @@
66064
66065 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
66066 /*
66067 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
66068 */
66069 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
66070 int i;
66071 sqlite3 *db;
66072 Db *aDb;
66073 int nDb;
66074 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
66075 db = p->db;
66076 aDb = db->aDb;
66077 nDb = db->nDb;
66078 for(i=0; i<nDb; i++){
66079 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
66080 sqlite3BtreeLeave(aDb[i].pBt);
66081 }
66082 }
 
 
 
 
66083 }
66084 #endif
66085
66086 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
66087 /*
@@ -67775,19 +68606,25 @@
67775 n += pMem->u.nZero;
67776 }
67777 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
67778 }
67779
 
 
 
 
 
 
 
67780 /*
67781 ** Return the length of the data corresponding to the supplied serial-type.
67782 */
67783 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
67784 if( serial_type>=12 ){
67785 return (serial_type-12)/2;
67786 }else{
67787 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
67788 return aSize[serial_type];
67789 }
67790 }
67791
67792 /*
67793 ** If we are on an architecture with mixed-endian floating
@@ -67867,11 +68704,11 @@
67867 memcpy(&v, &pMem->u.r, sizeof(v));
67868 swapMixedEndianFloat(v);
67869 }else{
67870 v = pMem->u.i;
67871 }
67872 len = i = sqlite3VdbeSerialTypeLen(serial_type);
67873 assert( i>0 );
67874 do{
67875 buf[--i] = (u8)(v&0xFF);
67876 v >>= 8;
67877 }while( i );
@@ -68896,11 +69733,11 @@
68896 testcase( typeRowid==8 );
68897 testcase( typeRowid==9 );
68898 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
68899 goto idx_rowid_corruption;
68900 }
68901 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
68902 testcase( (u32)m.n==szHdr+lenRowid );
68903 if( unlikely((u32)m.n<szHdr+lenRowid) ){
68904 goto idx_rowid_corruption;
68905 }
68906
@@ -71122,11 +71959,11 @@
71122 ** an integer representation is more space efficient on disk.
71123 **
71124 ** SQLITE_AFF_TEXT:
71125 ** Convert pRec to a text representation.
71126 **
71127 ** SQLITE_AFF_NONE:
71128 ** No-op. pRec is unchanged.
71129 */
71130 static void applyAffinity(
71131 Mem *pRec, /* The value to apply affinity to */
71132 char affinity, /* The affinity to be applied */
@@ -71523,17 +72360,13 @@
71523 db->busyHandler.nBusy = 0;
71524 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
71525 sqlite3VdbeIOTraceSql(p);
71526 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
71527 if( db->xProgress ){
 
71528 assert( 0 < db->nProgressOps );
71529 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
71530 if( nProgressLimit==0 ){
71531 nProgressLimit = db->nProgressOps;
71532 }else{
71533 nProgressLimit %= (unsigned)db->nProgressOps;
71534 }
71535 }
71536 #endif
71537 #ifdef SQLITE_DEBUG
71538 sqlite3BeginBenignMalloc();
71539 if( p->pc==0
@@ -72491,14 +73324,14 @@
72491 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
72492 }
72493 break;
72494 }
72495
72496 /* Opcode: Function P1 P2 P3 P4 P5
72497 ** Synopsis: r[P3]=func(r[P2@P5])
72498 **
72499 ** Invoke a user function (P4 is a pointer to a Function structure that
72500 ** defines the function) with P5 arguments taken from register P2 and
72501 ** successors. The result of the function is stored in register P3.
72502 ** Register P3 must not be one of the function inputs.
72503 **
72504 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
@@ -72506,63 +73339,104 @@
72506 ** argument was constant then bit 0 of P1 is set. This is used to determine
72507 ** whether meta data associated with a user function argument using the
72508 ** sqlite3_set_auxdata() API may be safely retained until the next
72509 ** invocation of this opcode.
72510 **
72511 ** See also: AggStep and AggFinal
72512 */
72513 case OP_Function: {
72514 int i;
72515 Mem *pArg;
72516 sqlite3_context ctx;
72517 sqlite3_value **apVal;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72518 int n;
72519
72520 n = pOp->p5;
72521 apVal = p->apArg;
72522 assert( apVal || n==0 );
72523 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
72524 ctx.pOut = &aMem[pOp->p3];
72525 memAboutToChange(p, ctx.pOut);
72526
72527 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
72528 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
72529 pArg = &aMem[pOp->p2];
72530 for(i=0; i<n; i++, pArg++){
72531 assert( memIsValid(pArg) );
72532 apVal[i] = pArg;
72533 Deephemeralize(pArg);
72534 REGISTER_TRACE(pOp->p2+i, pArg);
72535 }
72536
72537 assert( pOp->p4type==P4_FUNCDEF );
72538 ctx.pFunc = pOp->p4.pFunc;
72539 ctx.iOp = (int)(pOp - aOp);
72540 ctx.pVdbe = p;
72541 MemSetTypeFlag(ctx.pOut, MEM_Null);
72542 ctx.fErrorOrAux = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72543 db->lastRowid = lastRowid;
72544 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
72545 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
72546
72547 /* If the function returned an error, throw an exception */
72548 if( ctx.fErrorOrAux ){
72549 if( ctx.isError ){
72550 sqlite3VdbeError(p, "%s", sqlite3_value_text(ctx.pOut));
72551 rc = ctx.isError;
72552 }
72553 sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
72554 }
72555
72556 /* Copy the result of the function into register P3 */
72557 sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
72558 if( sqlite3VdbeMemTooBig(ctx.pOut) ){
72559 goto too_big;
72560 }
72561
72562 REGISTER_TRACE(pOp->p3, ctx.pOut);
72563 UPDATE_MAX_BLOBSIZE(ctx.pOut);
72564 break;
72565 }
72566
72567 /* Opcode: BitAnd P1 P2 P3 * *
72568 ** Synopsis: r[P3]=r[P1]&r[P2]
@@ -72721,13 +73595,13 @@
72721 ** </ul>
72722 **
72723 ** A NULL value is not changed by this routine. It remains NULL.
72724 */
72725 case OP_Cast: { /* in1 */
72726 assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL );
72727 testcase( pOp->p2==SQLITE_AFF_TEXT );
72728 testcase( pOp->p2==SQLITE_AFF_NONE );
72729 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
72730 testcase( pOp->p2==SQLITE_AFF_INTEGER );
72731 testcase( pOp->p2==SQLITE_AFF_REAL );
72732 pIn1 = &aMem[pOp->p1];
72733 memAboutToChange(p, pIn1);
@@ -73534,11 +74408,11 @@
73534 ** field of the index key.
73535 **
73536 ** The mapping from character to affinity is given by the SQLITE_AFF_
73537 ** macros defined in sqliteInt.h.
73538 **
73539 ** If P4 is NULL then all index fields have the affinity NONE.
73540 */
73541 case OP_MakeRecord: {
73542 u8 *zNewRecord; /* A buffer to hold the data for the new record */
73543 Mem *pRec; /* The new record */
73544 u64 nData; /* Number of bytes of data space */
@@ -74451,10 +75325,30 @@
74451 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
74452 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
74453 p->apCsr[pOp->p1] = 0;
74454 break;
74455 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74456
74457 /* Opcode: SeekGE P1 P2 P3 P4 *
74458 ** Synopsis: key=r[P3@P4]
74459 **
74460 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -74940,13 +75834,12 @@
74940 res = 0;
74941 pOut = out2Prerelease(p, pOp);
74942 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
74943 pC = p->apCsr[pOp->p1];
74944 assert( pC!=0 );
74945 if( NEVER(pC->pCursor==0) ){
74946 /* The zero initialization above is all that is needed */
74947 }else{
74948 /* The next rowid or record number (different terms for the same
74949 ** thing) is obtained in a two-step algorithm.
74950 **
74951 ** First we attempt to find the largest existing rowid and add one
74952 ** to that. But if the largest existing rowid is already the maximum
@@ -75681,32 +76574,30 @@
75681 ** for tables is OP_Insert.
75682 */
75683 case OP_SorterInsert: /* in2 */
75684 case OP_IdxInsert: { /* in2 */
75685 VdbeCursor *pC;
75686 BtCursor *pCrsr;
75687 int nKey;
75688 const char *zKey;
75689
75690 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
75691 pC = p->apCsr[pOp->p1];
75692 assert( pC!=0 );
75693 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
75694 pIn2 = &aMem[pOp->p2];
75695 assert( pIn2->flags & MEM_Blob );
75696 pCrsr = pC->pCursor;
75697 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
75698 assert( pCrsr!=0 );
75699 assert( pC->isTable==0 );
75700 rc = ExpandBlob(pIn2);
75701 if( rc==SQLITE_OK ){
75702 if( isSorter(pC) ){
75703 rc = sqlite3VdbeSorterWrite(pC, pIn2);
75704 }else{
75705 nKey = pIn2->n;
75706 zKey = pIn2->z;
75707 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
75708 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
75709 );
75710 assert( pC->deferredMoveto==0 );
75711 pC->cacheStatus = CACHE_STALE;
75712 }
@@ -76622,61 +77513,105 @@
76622 VdbeBranchTaken(pIn1->u.i==0, 2);
76623 if( (pIn1->u.i++)==0 ) goto jump_to_p2;
76624 break;
76625 }
76626
76627 /* Opcode: AggStep * P2 P3 P4 P5
76628 ** Synopsis: accum=r[P3] step(r[P2@P5])
76629 **
76630 ** Execute the step function for an aggregate. The
76631 ** function has P5 arguments. P4 is a pointer to the FuncDef
76632 ** structure that specifies the function. Use register
76633 ** P3 as the accumulator.
 
 
 
 
 
 
 
 
 
 
 
76634 **
76635 ** The P5 arguments are taken from register P2 and its
76636 ** successors.
 
 
 
 
 
 
76637 */
76638 case OP_AggStep: {
76639 int n;
76640 int i;
76641 Mem *pMem;
76642 Mem *pRec;
76643 Mem t;
76644 sqlite3_context ctx;
76645 sqlite3_value **apVal;
76646
 
76647 n = pOp->p5;
76648 assert( n>=0 );
76649 pRec = &aMem[pOp->p2];
76650 apVal = p->apArg;
76651 assert( apVal || n==0 );
76652 for(i=0; i<n; i++, pRec++){
76653 assert( memIsValid(pRec) );
76654 apVal[i] = pRec;
76655 memAboutToChange(p, pRec);
76656 }
76657 ctx.pFunc = pOp->p4.pFunc;
76658 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
76659 ctx.pMem = pMem = &aMem[pOp->p3];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76660 pMem->n++;
76661 sqlite3VdbeMemInit(&t, db, MEM_Null);
76662 ctx.pOut = &t;
76663 ctx.isError = 0;
76664 ctx.pVdbe = p;
76665 ctx.iOp = (int)(pOp - aOp);
76666 ctx.skipFlag = 0;
76667 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
76668 if( ctx.isError ){
76669 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
76670 rc = ctx.isError;
76671 }
76672 if( ctx.skipFlag ){
 
 
 
76673 assert( pOp[-1].opcode==OP_CollSeq );
76674 i = pOp[-1].p1;
76675 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
76676 }
76677 sqlite3VdbeMemRelease(&t);
76678 break;
76679 }
76680
76681 /* Opcode: AggFinal P1 P2 * P4 *
76682 ** Synopsis: accum=r[P1] N=P2
@@ -82721,10 +83656,17 @@
82721 "the GROUP BY clause");
82722 return WRC_Abort;
82723 }
82724 }
82725 }
 
 
 
 
 
 
 
82726
82727 /* Advance to the next term of the compound
82728 */
82729 p = p->pPrior;
82730 nCompound++;
@@ -83089,17 +84031,17 @@
83089 ** affinity, use that. Otherwise use no affinity.
83090 */
83091 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
83092 return SQLITE_AFF_NUMERIC;
83093 }else{
83094 return SQLITE_AFF_NONE;
83095 }
83096 }else if( !aff1 && !aff2 ){
83097 /* Neither side of the comparison is a column. Compare the
83098 ** results directly.
83099 */
83100 return SQLITE_AFF_NONE;
83101 }else{
83102 /* One side is a column, the other is not. Use the columns affinity. */
83103 assert( aff1==0 || aff2==0 );
83104 return (aff1 + aff2);
83105 }
@@ -83119,11 +84061,11 @@
83119 if( pExpr->pRight ){
83120 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
83121 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
83122 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
83123 }else if( !aff ){
83124 aff = SQLITE_AFF_NONE;
83125 }
83126 return aff;
83127 }
83128
83129 /*
@@ -83133,11 +84075,11 @@
83133 ** the comparison in pExpr.
83134 */
83135 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
83136 char aff = comparisonAffinity(pExpr);
83137 switch( aff ){
83138 case SQLITE_AFF_NONE:
83139 return 1;
83140 case SQLITE_AFF_TEXT:
83141 return idx_affinity==SQLITE_AFF_TEXT;
83142 default:
83143 return sqlite3IsNumericAffinity(idx_affinity);
@@ -83939,11 +84881,11 @@
83939 pNewItem->addrFillSub = pOldItem->addrFillSub;
83940 pNewItem->regReturn = pOldItem->regReturn;
83941 pNewItem->isCorrelated = pOldItem->isCorrelated;
83942 pNewItem->viaCoroutine = pOldItem->viaCoroutine;
83943 pNewItem->isRecursive = pOldItem->isRecursive;
83944 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
83945 pNewItem->notIndexed = pOldItem->notIndexed;
83946 pNewItem->pIndex = pOldItem->pIndex;
83947 pTab = pNewItem->pTab = pOldItem->pTab;
83948 if( pTab ){
83949 pTab->nRef++;
@@ -84166,11 +85108,11 @@
84166 **
84167 ** These callback routines are used to implement the following:
84168 **
84169 ** sqlite3ExprIsConstant() pWalker->eCode==1
84170 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
84171 ** sqlite3ExprRefOneTableOnly() pWalker->eCode==3
84172 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
84173 **
84174 ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
84175 ** is found to not be a constant.
84176 **
@@ -84274,11 +85216,11 @@
84274 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
84275 return exprIsConst(p, 2, 0);
84276 }
84277
84278 /*
84279 ** Walk an expression tree. Return non-zero if the expression constant
84280 ** for any single row of the table with cursor iCur. In other words, the
84281 ** expression must not refer to any non-deterministic function nor any
84282 ** table other than iCur.
84283 */
84284 SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
@@ -84380,11 +85322,11 @@
84380 ** is harmless. A false positive, however, can result in the wrong
84381 ** answer.
84382 */
84383 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
84384 u8 op;
84385 if( aff==SQLITE_AFF_NONE ) return 1;
84386 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
84387 op = p->op;
84388 if( op==TK_REGISTER ) op = p->op2;
84389 switch( op ){
84390 case TK_INTEGER: {
@@ -84831,11 +85773,11 @@
84831 ExprList *pList = pExpr->x.pList;
84832 struct ExprList_item *pItem;
84833 int r1, r2, r3;
84834
84835 if( !affinity ){
84836 affinity = SQLITE_AFF_NONE;
84837 }
84838 if( pKeyInfo ){
84839 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
84840 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
84841 }
@@ -85106,21 +86048,10 @@
85106 sqlite3ExprCachePop(pParse);
85107 VdbeComment((v, "end IN expr"));
85108 }
85109 #endif /* SQLITE_OMIT_SUBQUERY */
85110
85111 /*
85112 ** Duplicate an 8-byte value
85113 */
85114 static char *dup8bytes(Vdbe *v, const char *in){
85115 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
85116 if( out ){
85117 memcpy(out, in, 8);
85118 }
85119 return out;
85120 }
85121
85122 #ifndef SQLITE_OMIT_FLOATING_POINT
85123 /*
85124 ** Generate an instruction that will put the floating point
85125 ** value described by z[0..n-1] into register iMem.
85126 **
@@ -85129,16 +86060,14 @@
85129 ** like the continuation of the number.
85130 */
85131 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
85132 if( ALWAYS(z!=0) ){
85133 double value;
85134 char *zV;
85135 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
85136 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
85137 if( negateFlag ) value = -value;
85138 zV = dup8bytes(v, (char*)&value);
85139 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
85140 }
85141 }
85142 #endif
85143
85144
@@ -85160,14 +86089,12 @@
85160 i64 value;
85161 const char *z = pExpr->u.zToken;
85162 assert( z!=0 );
85163 c = sqlite3DecOrHexToI64(z, &value);
85164 if( c==0 || (c==2 && negFlag) ){
85165 char *zV;
85166 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
85167 zV = dup8bytes(v, (char*)&value);
85168 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
85169 }else{
85170 #ifdef SQLITE_OMIT_FLOATING_POINT
85171 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
85172 #else
85173 #ifndef SQLITE_OMIT_HEX_INTEGER
@@ -85768,11 +86695,11 @@
85768 /* The UNLIKELY() function is a no-op. The result is the value
85769 ** of the first argument.
85770 */
85771 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
85772 assert( nFarg>=1 );
85773 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
85774 break;
85775 }
85776
85777 for(i=0; i<nFarg; i++){
85778 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
@@ -85838,11 +86765,11 @@
85838 #endif
85839 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
85840 if( !pColl ) pColl = db->pDfltColl;
85841 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
85842 }
85843 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
85844 (char*)pDef, P4_FUNCDEF);
85845 sqlite3VdbeChangeP5(v, (u8)nFarg);
85846 if( nFarg && constMask==0 ){
85847 sqlite3ReleaseTempRange(pParse, r1, nFarg);
85848 }
@@ -86209,272 +87136,10 @@
86209 iMem = ++pParse->nMem;
86210 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
86211 exprToRegister(pExpr, iMem);
86212 }
86213
86214 #ifdef SQLITE_DEBUG
86215 /*
86216 ** Generate a human-readable explanation of an expression tree.
86217 */
86218 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
86219 const char *zBinOp = 0; /* Binary operator */
86220 const char *zUniOp = 0; /* Unary operator */
86221 pView = sqlite3TreeViewPush(pView, moreToFollow);
86222 if( pExpr==0 ){
86223 sqlite3TreeViewLine(pView, "nil");
86224 sqlite3TreeViewPop(pView);
86225 return;
86226 }
86227 switch( pExpr->op ){
86228 case TK_AGG_COLUMN: {
86229 sqlite3TreeViewLine(pView, "AGG{%d:%d}",
86230 pExpr->iTable, pExpr->iColumn);
86231 break;
86232 }
86233 case TK_COLUMN: {
86234 if( pExpr->iTable<0 ){
86235 /* This only happens when coding check constraints */
86236 sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
86237 }else{
86238 sqlite3TreeViewLine(pView, "{%d:%d}",
86239 pExpr->iTable, pExpr->iColumn);
86240 }
86241 break;
86242 }
86243 case TK_INTEGER: {
86244 if( pExpr->flags & EP_IntValue ){
86245 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
86246 }else{
86247 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
86248 }
86249 break;
86250 }
86251 #ifndef SQLITE_OMIT_FLOATING_POINT
86252 case TK_FLOAT: {
86253 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
86254 break;
86255 }
86256 #endif
86257 case TK_STRING: {
86258 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
86259 break;
86260 }
86261 case TK_NULL: {
86262 sqlite3TreeViewLine(pView,"NULL");
86263 break;
86264 }
86265 #ifndef SQLITE_OMIT_BLOB_LITERAL
86266 case TK_BLOB: {
86267 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
86268 break;
86269 }
86270 #endif
86271 case TK_VARIABLE: {
86272 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
86273 pExpr->u.zToken, pExpr->iColumn);
86274 break;
86275 }
86276 case TK_REGISTER: {
86277 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
86278 break;
86279 }
86280 case TK_AS: {
86281 sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
86282 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
86283 break;
86284 }
86285 case TK_ID: {
86286 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
86287 break;
86288 }
86289 #ifndef SQLITE_OMIT_CAST
86290 case TK_CAST: {
86291 /* Expressions of the form: CAST(pLeft AS token) */
86292 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
86293 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
86294 break;
86295 }
86296 #endif /* SQLITE_OMIT_CAST */
86297 case TK_LT: zBinOp = "LT"; break;
86298 case TK_LE: zBinOp = "LE"; break;
86299 case TK_GT: zBinOp = "GT"; break;
86300 case TK_GE: zBinOp = "GE"; break;
86301 case TK_NE: zBinOp = "NE"; break;
86302 case TK_EQ: zBinOp = "EQ"; break;
86303 case TK_IS: zBinOp = "IS"; break;
86304 case TK_ISNOT: zBinOp = "ISNOT"; break;
86305 case TK_AND: zBinOp = "AND"; break;
86306 case TK_OR: zBinOp = "OR"; break;
86307 case TK_PLUS: zBinOp = "ADD"; break;
86308 case TK_STAR: zBinOp = "MUL"; break;
86309 case TK_MINUS: zBinOp = "SUB"; break;
86310 case TK_REM: zBinOp = "REM"; break;
86311 case TK_BITAND: zBinOp = "BITAND"; break;
86312 case TK_BITOR: zBinOp = "BITOR"; break;
86313 case TK_SLASH: zBinOp = "DIV"; break;
86314 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
86315 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
86316 case TK_CONCAT: zBinOp = "CONCAT"; break;
86317 case TK_DOT: zBinOp = "DOT"; break;
86318
86319 case TK_UMINUS: zUniOp = "UMINUS"; break;
86320 case TK_UPLUS: zUniOp = "UPLUS"; break;
86321 case TK_BITNOT: zUniOp = "BITNOT"; break;
86322 case TK_NOT: zUniOp = "NOT"; break;
86323 case TK_ISNULL: zUniOp = "ISNULL"; break;
86324 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
86325
86326 case TK_COLLATE: {
86327 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
86328 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
86329 break;
86330 }
86331
86332 case TK_AGG_FUNCTION:
86333 case TK_FUNCTION: {
86334 ExprList *pFarg; /* List of function arguments */
86335 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
86336 pFarg = 0;
86337 }else{
86338 pFarg = pExpr->x.pList;
86339 }
86340 if( pExpr->op==TK_AGG_FUNCTION ){
86341 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
86342 pExpr->op2, pExpr->u.zToken);
86343 }else{
86344 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
86345 }
86346 if( pFarg ){
86347 sqlite3TreeViewExprList(pView, pFarg, 0, 0);
86348 }
86349 break;
86350 }
86351 #ifndef SQLITE_OMIT_SUBQUERY
86352 case TK_EXISTS: {
86353 sqlite3TreeViewLine(pView, "EXISTS-expr");
86354 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
86355 break;
86356 }
86357 case TK_SELECT: {
86358 sqlite3TreeViewLine(pView, "SELECT-expr");
86359 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
86360 break;
86361 }
86362 case TK_IN: {
86363 sqlite3TreeViewLine(pView, "IN");
86364 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
86365 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
86366 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
86367 }else{
86368 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
86369 }
86370 break;
86371 }
86372 #endif /* SQLITE_OMIT_SUBQUERY */
86373
86374 /*
86375 ** x BETWEEN y AND z
86376 **
86377 ** This is equivalent to
86378 **
86379 ** x>=y AND x<=z
86380 **
86381 ** X is stored in pExpr->pLeft.
86382 ** Y is stored in pExpr->pList->a[0].pExpr.
86383 ** Z is stored in pExpr->pList->a[1].pExpr.
86384 */
86385 case TK_BETWEEN: {
86386 Expr *pX = pExpr->pLeft;
86387 Expr *pY = pExpr->x.pList->a[0].pExpr;
86388 Expr *pZ = pExpr->x.pList->a[1].pExpr;
86389 sqlite3TreeViewLine(pView, "BETWEEN");
86390 sqlite3TreeViewExpr(pView, pX, 1);
86391 sqlite3TreeViewExpr(pView, pY, 1);
86392 sqlite3TreeViewExpr(pView, pZ, 0);
86393 break;
86394 }
86395 case TK_TRIGGER: {
86396 /* If the opcode is TK_TRIGGER, then the expression is a reference
86397 ** to a column in the new.* or old.* pseudo-tables available to
86398 ** trigger programs. In this case Expr.iTable is set to 1 for the
86399 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
86400 ** is set to the column of the pseudo-table to read, or to -1 to
86401 ** read the rowid field.
86402 */
86403 sqlite3TreeViewLine(pView, "%s(%d)",
86404 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
86405 break;
86406 }
86407 case TK_CASE: {
86408 sqlite3TreeViewLine(pView, "CASE");
86409 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
86410 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
86411 break;
86412 }
86413 #ifndef SQLITE_OMIT_TRIGGER
86414 case TK_RAISE: {
86415 const char *zType = "unk";
86416 switch( pExpr->affinity ){
86417 case OE_Rollback: zType = "rollback"; break;
86418 case OE_Abort: zType = "abort"; break;
86419 case OE_Fail: zType = "fail"; break;
86420 case OE_Ignore: zType = "ignore"; break;
86421 }
86422 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
86423 break;
86424 }
86425 #endif
86426 default: {
86427 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
86428 break;
86429 }
86430 }
86431 if( zBinOp ){
86432 sqlite3TreeViewLine(pView, "%s", zBinOp);
86433 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
86434 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
86435 }else if( zUniOp ){
86436 sqlite3TreeViewLine(pView, "%s", zUniOp);
86437 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
86438 }
86439 sqlite3TreeViewPop(pView);
86440 }
86441 #endif /* SQLITE_DEBUG */
86442
86443 #ifdef SQLITE_DEBUG
86444 /*
86445 ** Generate a human-readable explanation of an expression list.
86446 */
86447 SQLITE_PRIVATE void sqlite3TreeViewExprList(
86448 TreeView *pView,
86449 const ExprList *pList,
86450 u8 moreToFollow,
86451 const char *zLabel
86452 ){
86453 int i;
86454 pView = sqlite3TreeViewPush(pView, moreToFollow);
86455 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
86456 if( pList==0 ){
86457 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
86458 }else{
86459 sqlite3TreeViewLine(pView, "%s", zLabel);
86460 for(i=0; i<pList->nExpr; i++){
86461 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
86462 #if 0
86463 if( pList->a[i].zName ){
86464 sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
86465 }
86466 if( pList->a[i].bSpanIsTab ){
86467 sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
86468 }
86469 #endif
86470 }
86471 }
86472 sqlite3TreeViewPop(pView);
86473 }
86474 #endif /* SQLITE_DEBUG */
86475
86476 /*
86477 ** Generate code that pushes the value of every element of the given
86478 ** expression list into a sequence of registers beginning at target.
86479 **
86480 ** Return the number of elements evaluated.
@@ -86861,10 +87526,25 @@
86861 }
86862 }
86863 sqlite3ReleaseTempReg(pParse, regFree1);
86864 sqlite3ReleaseTempReg(pParse, regFree2);
86865 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86866
86867 /*
86868 ** Do a deep comparison of two expression trees. Return 0 if the two
86869 ** expressions are completely identical. Return 1 if they differ only
86870 ** by a COLLATE operator at the top level. Return 2 if there are differences
@@ -88014,11 +88694,11 @@
88014 ** can handle (i.e. not CURRENT_TIME etc.)
88015 */
88016 if( pDflt ){
88017 sqlite3_value *pVal = 0;
88018 int rc;
88019 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal);
88020 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
88021 if( rc!=SQLITE_OK ){
88022 db->mallocFailed = 1;
88023 return;
88024 }
@@ -89099,11 +89779,11 @@
89099 #elif SQLITE_DEBUG
89100 assert( iParam==STAT_GET_STAT1 );
89101 #else
89102 UNUSED_PARAMETER( iParam );
89103 #endif
89104 sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
89105 sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
89106 sqlite3VdbeChangeP5(v, 1 + IsStat34);
89107 }
89108
89109 /*
@@ -89254,11 +89934,11 @@
89254 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89255 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
89256 #endif
89257 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
89258 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
89259 sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
89260 sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
89261 sqlite3VdbeChangeP5(v, 2+IsStat34);
89262
89263 /* Implementation of the following:
89264 **
@@ -89350,11 +90030,11 @@
89350 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
89351 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
89352 }
89353 #endif
89354 assert( regChng==(regStat4+1) );
89355 sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
89356 sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
89357 sqlite3VdbeChangeP5(v, 2+IsStat34);
89358 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
89359
89360 /* Add the entry to the stat1 table. */
@@ -90409,11 +91089,11 @@
90409 sqlite3ExprCode(pParse, pDbname, regArgs+1);
90410 sqlite3ExprCode(pParse, pKey, regArgs+2);
90411
90412 assert( v || db->mallocFailed );
90413 if( v ){
90414 sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
90415 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
90416 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
90417 sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
90418
90419 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
@@ -91874,11 +92554,11 @@
91874 */
91875 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
91876 int j1;
91877 int fileFormat;
91878 int reg1, reg2, reg3;
91879 sqlite3BeginWriteOperation(pParse, 0, iDb);
91880
91881 #ifndef SQLITE_OMIT_VIRTUALTABLE
91882 if( isVirtual ){
91883 sqlite3VdbeAddOp0(v, OP_VBegin);
91884 }
@@ -91990,14 +92670,14 @@
91990 pCol = &p->aCol[p->nCol];
91991 memset(pCol, 0, sizeof(p->aCol[0]));
91992 pCol->zName = z;
91993
91994 /* If there is no type specified, columns have the default affinity
91995 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
91996 ** be called next to set pCol->affinity correctly.
91997 */
91998 pCol->affinity = SQLITE_AFF_NONE;
91999 pCol->szEst = 1;
92000 p->nCol++;
92001 }
92002
92003 /*
@@ -92028,11 +92708,11 @@
92028 ** --------------------------------
92029 ** 'INT' | SQLITE_AFF_INTEGER
92030 ** 'CHAR' | SQLITE_AFF_TEXT
92031 ** 'CLOB' | SQLITE_AFF_TEXT
92032 ** 'TEXT' | SQLITE_AFF_TEXT
92033 ** 'BLOB' | SQLITE_AFF_NONE
92034 ** 'REAL' | SQLITE_AFF_REAL
92035 ** 'FLOA' | SQLITE_AFF_REAL
92036 ** 'DOUB' | SQLITE_AFF_REAL
92037 **
92038 ** If none of the substrings in the above table are found,
@@ -92054,11 +92734,11 @@
92054 aff = SQLITE_AFF_TEXT;
92055 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
92056 aff = SQLITE_AFF_TEXT;
92057 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
92058 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
92059 aff = SQLITE_AFF_NONE;
92060 if( zIn[0]=='(' ) zChar = zIn;
92061 #ifndef SQLITE_OMIT_FLOATING_POINT
92062 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
92063 && aff==SQLITE_AFF_NUMERIC ){
92064 aff = SQLITE_AFF_REAL;
@@ -92446,11 +93126,11 @@
92446 k = sqlite3Strlen30(zStmt);
92447 identPut(zStmt, &k, p->zName);
92448 zStmt[k++] = '(';
92449 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
92450 static const char * const azType[] = {
92451 /* SQLITE_AFF_NONE */ "",
92452 /* SQLITE_AFF_TEXT */ " TEXT",
92453 /* SQLITE_AFF_NUMERIC */ " NUM",
92454 /* SQLITE_AFF_INTEGER */ " INT",
92455 /* SQLITE_AFF_REAL */ " REAL"
92456 };
@@ -92459,21 +93139,21 @@
92459
92460 sqlite3_snprintf(n-k, &zStmt[k], zSep);
92461 k += sqlite3Strlen30(&zStmt[k]);
92462 zSep = zSep2;
92463 identPut(zStmt, &k, pCol->zName);
92464 assert( pCol->affinity-SQLITE_AFF_NONE >= 0 );
92465 assert( pCol->affinity-SQLITE_AFF_NONE < ArraySize(azType) );
92466 testcase( pCol->affinity==SQLITE_AFF_NONE );
92467 testcase( pCol->affinity==SQLITE_AFF_TEXT );
92468 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
92469 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
92470 testcase( pCol->affinity==SQLITE_AFF_REAL );
92471
92472 zType = azType[pCol->affinity - SQLITE_AFF_NONE];
92473 len = sqlite3Strlen30(zType);
92474 assert( pCol->affinity==SQLITE_AFF_NONE
92475 || pCol->affinity==sqlite3AffinityType(zType, 0) );
92476 memcpy(&zStmt[k], zType, len);
92477 k += len;
92478 assert( k<=n );
92479 }
@@ -92822,10 +93502,11 @@
92822
92823 regYield = ++pParse->nMem;
92824 regRec = ++pParse->nMem;
92825 regRowid = ++pParse->nMem;
92826 assert(pParse->nTab==1);
 
92827 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
92828 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
92829 pParse->nTab = 2;
92830 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
92831 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
@@ -94599,11 +95280,11 @@
94599 if( pList==0 ) return;
94600 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
94601 sqlite3DbFree(db, pItem->zDatabase);
94602 sqlite3DbFree(db, pItem->zName);
94603 sqlite3DbFree(db, pItem->zAlias);
94604 sqlite3DbFree(db, pItem->zIndex);
94605 sqlite3DeleteTable(db, pItem->pTab);
94606 sqlite3SelectDelete(db, pItem->pSelect);
94607 sqlite3ExprDelete(db, pItem->pOn);
94608 sqlite3IdListDelete(db, pItem->pUsing);
94609 }
@@ -94672,17 +95353,17 @@
94672 */
94673 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
94674 assert( pIndexedBy!=0 );
94675 if( p && ALWAYS(p->nSrc>0) ){
94676 struct SrcList_item *pItem = &p->a[p->nSrc-1];
94677 assert( pItem->notIndexed==0 && pItem->zIndex==0 );
94678 if( pIndexedBy->n==1 && !pIndexedBy->z ){
94679 /* A "NOT INDEXED" clause was supplied. See parse.y
94680 ** construct "indexed_opt" for details. */
94681 pItem->notIndexed = 1;
94682 }else{
94683 pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
94684 }
94685 }
94686 }
94687
94688 /*
@@ -96502,12 +97183,12 @@
96502 if( piPartIdxLabel ){
96503 if( pIdx->pPartIdxWhere ){
96504 *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
96505 pParse->iPartIdxTab = iDataCur;
96506 sqlite3ExprCachePush(pParse);
96507 sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
96508 SQLITE_JUMPIFNULL);
96509 }else{
96510 *piPartIdxLabel = 0;
96511 }
96512 }
96513 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
@@ -97119,21 +97800,19 @@
97119 u8 noCase;
97120 };
97121
97122 /*
97123 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
97124 ** character is exactly one byte in size. Also, all characters are
97125 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
97126 ** whereas only characters less than 0x80 do in ASCII.
97127 */
97128 #if defined(SQLITE_EBCDIC)
97129 # define sqlite3Utf8Read(A) (*((*A)++))
97130 # define GlobUpperToLower(A) A = sqlite3UpperToLower[A]
97131 # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A]
97132 #else
97133 # define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; }
97134 # define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A]
97135 #endif
97136
97137 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
97138 /* The correct SQL-92 behavior is for the LIKE operator to ignore
97139 ** case. Thus 'a' LIKE 'A' would be true. */
@@ -97171,11 +97850,11 @@
97171 *** '_' Matches any one character
97172 **
97173 ** Ec Where E is the "esc" character and c is any other
97174 ** character, including '%', '_', and esc, match exactly c.
97175 **
97176 ** The comments through this routine usually assume glob matching.
97177 **
97178 ** This routine is usually quick, but can be N**2 in the worst case.
97179 */
97180 static int patternCompare(
97181 const u8 *zPattern, /* The glob pattern */
@@ -97195,17 +97874,16 @@
97195 ** the other, never both. Hence the single variable matchOther is used
97196 ** to store the one we have to look for.
97197 */
97198 matchOther = esc ? esc : pInfo->matchSet;
97199
97200 while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
97201 if( c==matchAll ){ /* Match "*" */
97202 /* Skip over multiple "*" characters in the pattern. If there
97203 ** are also "?" characters, skip those as well, but consume a
97204 ** single character of the input string for each "?" skipped */
97205 while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
97206 || c == matchOne ){
97207 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
97208 return 0;
97209 }
97210 }
97211 if( c==0 ){
@@ -97246,11 +97924,11 @@
97246 while( (c2 = *(zString++))!=0 ){
97247 if( c2!=c && c2!=cx ) continue;
97248 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
97249 }
97250 }else{
97251 while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
97252 if( c2!=c ) continue;
97253 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
97254 }
97255 }
97256 return 0;
@@ -97292,11 +97970,11 @@
97292 return 0;
97293 }
97294 continue;
97295 }
97296 }
97297 c2 = sqlite3Utf8Read(&zString);
97298 if( c==c2 ) continue;
97299 if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
97300 continue;
97301 }
97302 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
@@ -99806,11 +100484,11 @@
99806 ** pIdx. A column affinity string has one character for each column in
99807 ** the table, according to the affinity of the column:
99808 **
99809 ** Character Column affinity
99810 ** ------------------------------
99811 ** 'A' NONE
99812 ** 'B' TEXT
99813 ** 'C' NUMERIC
99814 ** 'D' INTEGER
99815 ** 'F' REAL
99816 **
@@ -99849,23 +100527,23 @@
99849 return pIdx->zColAff;
99850 }
99851
99852 /*
99853 ** Compute the affinity string for table pTab, if it has not already been
99854 ** computed. As an optimization, omit trailing SQLITE_AFF_NONE affinities.
99855 **
99856 ** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and
99857 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
99858 ** for register iReg and following. Or if affinities exists and iReg==0,
99859 ** then just set the P4 operand of the previous opcode (which should be
99860 ** an OP_MakeRecord) to the affinity string.
99861 **
99862 ** A column affinity string has one character per column:
99863 **
99864 ** Character Column affinity
99865 ** ------------------------------
99866 ** 'A' NONE
99867 ** 'B' TEXT
99868 ** 'C' NUMERIC
99869 ** 'D' INTEGER
99870 ** 'E' REAL
99871 */
@@ -99883,11 +100561,11 @@
99883 for(i=0; i<pTab->nCol; i++){
99884 zColAff[i] = pTab->aCol[i].affinity;
99885 }
99886 do{
99887 zColAff[i--] = 0;
99888 }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
99889 pTab->zColAff = zColAff;
99890 }
99891 i = sqlite3Strlen30(zColAff);
99892 if( i ){
99893 if( iReg ){
@@ -101131,12 +101809,12 @@
101131
101132 /* Skip partial indices for which the WHERE clause is not true */
101133 if( pIdx->pPartIdxWhere ){
101134 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
101135 pParse->ckBase = regNewData+1;
101136 sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
101137 SQLITE_JUMPIFNULL);
101138 pParse->ckBase = 0;
101139 }
101140
101141 /* Create a record for this index entry as it should appear after
101142 ** the insert or update. Store that record in the aRegIdx[ix] register
@@ -102242,11 +102920,12 @@
102242 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
102243 void(*)(void*));
102244 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
102245 void(*)(void*), unsigned char);
102246 int (*strglob)(const char*,const char*);
102247 sqlite3_value (*value_dup)(const sqlite3_value*);
 
102248 void (*value_free)(sqlite3_value*);
102249 };
102250
102251 /*
102252 ** The following macros redefine the API routines so that they are
@@ -102882,11 +103561,14 @@
102882 sqlite3_msize,
102883 sqlite3_realloc64,
102884 sqlite3_reset_auto_extension,
102885 sqlite3_result_blob64,
102886 sqlite3_result_text64,
102887 sqlite3_strglob
 
 
 
102888 };
102889
102890 /*
102891 ** Attempt to load an SQLite extension library contained in the file
102892 ** zFile. The entry point is zProc. zProc may be 0 in which case a
@@ -106617,11 +107299,12 @@
106617 */
106618 #if SELECTTRACE_ENABLED
106619 /***/ int sqlite3SelectTrace = 0;
106620 # define SELECTTRACE(K,P,S,X) \
106621 if(sqlite3SelectTrace&(K)) \
106622 sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\
 
106623 sqlite3DebugPrintf X
106624 #else
106625 # define SELECTTRACE(K,P,S,X)
106626 #endif
106627
@@ -106961,10 +107644,16 @@
106961 while( p ){
106962 ExprSetProperty(p, EP_FromJoin);
106963 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
106964 ExprSetVVAProperty(p, EP_NoReduce);
106965 p->iRightJoinTable = (i16)iTable;
 
 
 
 
 
 
106966 setJoinExpr(p->pLeft, iTable);
106967 p = p->pRight;
106968 }
106969 }
106970
@@ -107370,11 +108059,12 @@
107370 break;
107371 }
107372
107373 default: {
107374 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
107375 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
 
107376 break;
107377 }
107378 }
107379 if( pSort==0 ){
107380 codeOffset(v, p->iOffset, iContinue);
@@ -107423,11 +108113,12 @@
107423 ** on an ephemeral index. If the current row is already present
107424 ** in the index, do not write it to the output. If not, add the
107425 ** current row to the index and proceed with writing it to the
107426 ** output table as well. */
107427 int addr = sqlite3VdbeCurrentAddr(v) + 4;
107428 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
 
107429 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
107430 assert( pSort==0 );
107431 }
107432 #endif
107433 if( pSort ){
@@ -107906,32 +108597,31 @@
107906 ** This routine has either 3 or 6 parameters depending on whether or not
107907 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
107908 */
107909 #ifdef SQLITE_ENABLE_COLUMN_METADATA
107910 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
 
 
 
107911 static const char *columnTypeImpl(
107912 NameContext *pNC,
107913 Expr *pExpr,
 
107914 const char **pzOrigDb,
107915 const char **pzOrigTab,
107916 const char **pzOrigCol,
 
107917 u8 *pEstWidth
107918 ){
 
 
 
 
107919 char const *zOrigDb = 0;
107920 char const *zOrigTab = 0;
107921 char const *zOrigCol = 0;
107922 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
107923 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
107924 static const char *columnTypeImpl(
107925 NameContext *pNC,
107926 Expr *pExpr,
107927 u8 *pEstWidth
107928 ){
107929 #endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
107930 char const *zType = 0;
107931 int j;
107932 u8 estWidth = 1;
107933
107934 if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
107935 switch( pExpr->op ){
107936 case TK_AGG_COLUMN:
107937 case TK_COLUMN: {
@@ -107980,14 +108670,17 @@
107980 if( pS ){
107981 /* The "table" is actually a sub-select or a view in the FROM clause
107982 ** of the SELECT statement. Return the declaration type and origin
107983 ** data for the result-set column of the sub-select.
107984 */
107985 if( iCol>=0 && iCol<pS->pEList->nExpr ){
107986 /* If iCol is less than zero, then the expression requests the
107987 ** rowid of the sub-select or view. This expression is legal (see
107988 ** test case misc2.2.2) - it always evaluates to NULL.
 
 
 
107989 */
107990 NameContext sNC;
107991 Expr *p = pS->pEList->a[iCol].pExpr;
107992 sNC.pSrcList = pS->pSrc;
107993 sNC.pNext = pNC;
@@ -108301,15 +108994,16 @@
108301 sNC.pSrcList = pSelect->pSrc;
108302 a = pSelect->pEList->a;
108303 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
108304 p = a[i].pExpr;
108305 if( pCol->zType==0 ){
108306 pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
 
108307 }
108308 szAll += pCol->szEst;
108309 pCol->affinity = sqlite3ExprAffinity(p);
108310 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
108311 pColl = sqlite3ExprCollSeq(pParse, p);
108312 if( pColl && pCol->zColl==0 ){
108313 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
108314 }
108315 }
@@ -108461,11 +109155,14 @@
108461 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
108462 }else{
108463 pRet = 0;
108464 }
108465 assert( iCol>=0 );
108466 if( pRet==0 && iCol<p->pEList->nExpr ){
 
 
 
108467 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
108468 }
108469 return pRet;
108470 }
108471
@@ -108680,11 +109377,11 @@
108680
108681 /*
108682 ** Error message for when two or more terms of a compound select have different
108683 ** size result sets.
108684 */
108685 static void selectWrongNumTermsError(Parse *pParse, Select *p){
108686 if( p->selFlags & SF_Values ){
108687 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
108688 }else{
108689 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
108690 " do not have the same number of result columns", selectOpName(p->op));
@@ -108706,23 +109403,19 @@
108706 Parse *pParse, /* Parsing context */
108707 Select *p, /* The right-most of SELECTs to be coded */
108708 SelectDest *pDest /* What to do with query results */
108709 ){
108710 Select *pPrior;
108711 int nExpr = p->pEList->nExpr;
108712 int nRow = 1;
108713 int rc = 0;
108714 assert( p->selFlags & SF_MultiValue );
108715 do{
108716 assert( p->selFlags & SF_Values );
108717 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
108718 assert( p->pLimit==0 );
108719 assert( p->pOffset==0 );
108720 if( p->pEList->nExpr!=nExpr ){
108721 selectWrongNumTermsError(pParse, p);
108722 return 1;
108723 }
108724 if( p->pPrior==0 ) break;
108725 assert( p->pPrior->pNext==p );
108726 p = p->pPrior;
108727 nRow++;
108728 }while(1);
@@ -108827,15 +109520,11 @@
108827
108828 /* Make sure all SELECTs in the statement have the same number of elements
108829 ** in their result sets.
108830 */
108831 assert( p->pEList && pPrior->pEList );
108832 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
108833 selectWrongNumTermsError(pParse, p);
108834 rc = 1;
108835 goto multi_select_end;
108836 }
108837
108838 #ifndef SQLITE_OMIT_CTE
108839 if( p->selFlags & SF_Recursive ){
108840 generateWithRecursiveQuery(pParse, p, &dest);
108841 }else
@@ -109450,13 +110139,11 @@
109450 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
109451 if( aPermute ){
109452 struct ExprList_item *pItem;
109453 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
109454 assert( pItem->u.x.iOrderByCol>0 );
109455 /* assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ) is also true
109456 ** but only for well-formed SELECT statements. */
109457 testcase( pItem->u.x.iOrderByCol > p->pEList->nExpr );
109458 aPermute[i] = pItem->u.x.iOrderByCol - 1;
109459 }
109460 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
109461 }else{
109462 pKeyMerge = 0;
@@ -109811,12 +110498,12 @@
109811 ** (9) The subquery does not use LIMIT or the outer query does not use
109812 ** aggregates.
109813 **
109814 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
109815 ** accidently carried the comment forward until 2014-09-15. Original
109816 ** text: "The subquery does not use aggregates or the outer query does not
109817 ** use LIMIT."
109818 **
109819 ** (11) The subquery and the outer query do not both have ORDER BY clauses.
109820 **
109821 ** (**) Not implemented. Subsumed into restriction (3). Was previously
109822 ** a separate restriction deriving from ticket #350.
@@ -110022,14 +110709,14 @@
110022 }
110023 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
110024 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
110025 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
110026 assert( pSub->pSrc!=0 );
 
110027 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
110028 || (pSub1->pPrior && pSub1->op!=TK_ALL)
110029 || pSub1->pSrc->nSrc<1
110030 || pSub->pEList->nExpr!=pSub1->pEList->nExpr
110031 ){
110032 return 0;
110033 }
110034 testcase( pSub1->pSrc->nSrc>1 );
110035 }
@@ -110305,16 +110992,83 @@
110305 */
110306 sqlite3SelectDelete(db, pSub1);
110307
110308 #if SELECTTRACE_ENABLED
110309 if( sqlite3SelectTrace & 0x100 ){
110310 sqlite3DebugPrintf("After flattening:\n");
110311 sqlite3TreeViewSelect(0, p, 0);
110312 }
110313 #endif
110314
110315 return 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110316 }
110317 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
110318
110319 /*
110320 ** Based on the contents of the AggInfo structure indicated by the first
@@ -110397,20 +111151,20 @@
110397 ** was such a clause and the named index cannot be found, return
110398 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
110399 ** pFrom->pIndex and return SQLITE_OK.
110400 */
110401 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
110402 if( pFrom->pTab && pFrom->zIndex ){
110403 Table *pTab = pFrom->pTab;
110404 char *zIndex = pFrom->zIndex;
110405 Index *pIdx;
110406 for(pIdx=pTab->pIndex;
110407 pIdx && sqlite3StrICmp(pIdx->zName, zIndex);
110408 pIdx=pIdx->pNext
110409 );
110410 if( !pIdx ){
110411 sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
110412 pParse->checkSchema = 1;
110413 return SQLITE_ERROR;
110414 }
110415 pFrom->pIndex = pIdx;
110416 }
@@ -111210,11 +111964,11 @@
111210 pColl = pParse->db->pDfltColl;
111211 }
111212 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
111213 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
111214 }
111215 sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
111216 (void*)pF->pFunc, P4_FUNCDEF);
111217 sqlite3VdbeChangeP5(v, (u8)nArg);
111218 sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
111219 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
111220 if( addrNext ){
@@ -111343,47 +112097,84 @@
111343 }
111344 sqlite3SelectPrep(pParse, p, 0);
111345 memset(&sSort, 0, sizeof(sSort));
111346 sSort.pOrderBy = p->pOrderBy;
111347 pTabList = p->pSrc;
111348 pEList = p->pEList;
111349 if( pParse->nErr || db->mallocFailed ){
111350 goto select_end;
111351 }
 
111352 isAgg = (p->selFlags & SF_Aggregate)!=0;
111353 assert( pEList!=0 );
111354 #if SELECTTRACE_ENABLED
111355 if( sqlite3SelectTrace & 0x100 ){
111356 SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
111357 sqlite3TreeViewSelect(0, p, 0);
111358 }
111359 #endif
111360
111361
111362 /* Begin generating code.
111363 */
111364 v = sqlite3GetVdbe(pParse);
111365 if( v==0 ) goto select_end;
111366
111367 /* If writing to memory or generating a set
111368 ** only a single column may be output.
111369 */
111370 #ifndef SQLITE_OMIT_SUBQUERY
111371 if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
111372 goto select_end;
111373 }
111374 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111375
111376 /* Generate code for all sub-queries in the FROM clause
111377 */
111378 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
111379 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
111380 struct SrcList_item *pItem = &pTabList->a[i];
111381 SelectDest dest;
111382 Select *pSub = pItem->pSelect;
111383 int isAggSub;
111384
111385 if( pSub==0 ) continue;
111386
111387 /* Sometimes the code for a subquery will be generated more than
111388 ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
111389 ** for example. In that case, do not regenerate the code to manifest
@@ -111404,21 +112195,29 @@
111404 ** more conservative than necessary, but much easier than enforcing
111405 ** an exact limit.
111406 */
111407 pParse->nHeight += sqlite3SelectExprHeight(p);
111408
111409 isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
111410 if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
111411 /* This subquery can be absorbed into its parent. */
111412 if( isAggSub ){
111413 isAgg = 1;
111414 p->selFlags |= SF_Aggregate;
111415 }
111416 i = -1;
111417 }else if( pTabList->nSrc==1
111418 && (p->selFlags & SF_All)==0
111419 && OptimizationEnabled(db, SQLITE_SubqCoroutine)
 
 
 
 
 
 
 
 
111420 ){
111421 /* Implement a co-routine that will return a single row of the result
111422 ** set on each invocation.
111423 */
111424 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
@@ -111465,37 +112264,27 @@
111465 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
111466 VdbeComment((v, "end %s", pItem->pTab->zName));
111467 sqlite3VdbeChangeP1(v, topAddr, retAddr);
111468 sqlite3ClearTempRegCache(pParse);
111469 }
111470 if( /*pParse->nErr ||*/ db->mallocFailed ){
111471 goto select_end;
111472 }
111473 pParse->nHeight -= sqlite3SelectExprHeight(p);
111474 pTabList = p->pSrc;
111475 if( !IgnorableOrderby(pDest) ){
111476 sSort.pOrderBy = p->pOrderBy;
111477 }
111478 }
 
 
 
 
111479 pEList = p->pEList;
111480 #endif
111481 pWhere = p->pWhere;
111482 pGroupBy = p->pGroupBy;
111483 pHaving = p->pHaving;
111484 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
111485
111486 #ifndef SQLITE_OMIT_COMPOUND_SELECT
111487 /* If there is are a sequence of queries, do the earlier ones first.
111488 */
111489 if( p->pPrior ){
111490 rc = multiSelect(pParse, p, pDest);
111491 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
111492 #if SELECTTRACE_ENABLED
111493 SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
111494 pParse->nSelectIndent--;
111495 #endif
111496 return rc;
111497 }
111498 #endif
111499
111500 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
111501 ** if the select-list is the same as the ORDER BY list, then this query
@@ -111511,27 +112300,27 @@
111511 ** used for both the ORDER BY and DISTINCT processing. As originally
111512 ** written the query must use a temp-table for at least one of the ORDER
111513 ** BY and DISTINCT, and an index or separate temp-table for the other.
111514 */
111515 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
111516 && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0
111517 ){
111518 p->selFlags &= ~SF_Distinct;
111519 p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
111520 pGroupBy = p->pGroupBy;
111521 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
111522 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
111523 ** original setting of the SF_Distinct flag, not the current setting */
111524 assert( sDistinct.isTnct );
111525 }
111526
111527 /* If there is an ORDER BY clause, then this sorting
111528 ** index might end up being unused if the data can be
111529 ** extracted in pre-sorted order. If that is the case, then the
111530 ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
111531 ** we figure out that the sorting index is not needed. The addrSortIndex
111532 ** variable is used to facilitate that change.
 
111533 */
111534 if( sSort.pOrderBy ){
111535 KeyInfo *pKeyInfo;
111536 pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
111537 sSort.iECursor = pParse->nTab++;
@@ -111558,18 +112347,18 @@
111558 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
111559 sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
111560 sSort.sortFlags |= SORTFLAG_UseSorter;
111561 }
111562
111563 /* Open a virtual index to use for the distinct set.
111564 */
111565 if( p->selFlags & SF_Distinct ){
111566 sDistinct.tabTnct = pParse->nTab++;
111567 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
111568 sDistinct.tabTnct, 0, 0,
111569 (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
111570 P4_KEYINFO);
111571 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
111572 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
111573 }else{
111574 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
111575 }
@@ -111643,15 +112432,14 @@
111643 if( p->nSelectRow>100 ) p->nSelectRow = 100;
111644 }else{
111645 p->nSelectRow = 1;
111646 }
111647
111648
111649 /* If there is both a GROUP BY and an ORDER BY clause and they are
111650 ** identical, then it may be possible to disable the ORDER BY clause
111651 ** on the grounds that the GROUP BY will cause elements to come out
111652 ** in the correct order. It also may not - the GROUP BY may use a
111653 ** database index that causes rows to be grouped together as required
111654 ** but not actually sorted. Either way, record the fact that the
111655 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
111656 ** variable. */
111657 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
@@ -111825,11 +112613,12 @@
111825 ** from the previous row currently stored in a0, a1, a2...
111826 */
111827 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
111828 sqlite3ExprCacheClear(pParse);
111829 if( groupBySort ){
111830 sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab);
 
111831 }
111832 for(j=0; j<pGroupBy->nExpr; j++){
111833 if( groupBySort ){
111834 sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
111835 }else{
@@ -111897,11 +112686,12 @@
111897 sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
111898 VdbeComment((v, "set abort flag"));
111899 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
111900 sqlite3VdbeResolveLabel(v, addrOutputRow);
111901 addrOutputRow = sqlite3VdbeCurrentAddr(v);
111902 sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
 
111903 VdbeComment((v, "Groupby result generator entry point"));
111904 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
111905 finalizeAggFunctions(pParse, &sAggInfo);
111906 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
111907 selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
@@ -112061,11 +112851,12 @@
112061
112062 /* If there is an ORDER BY clause, then we need to sort the results
112063 ** and send them to the callback one by one.
112064 */
112065 if( sSort.pOrderBy ){
112066 explainTempTable(pParse, sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
 
112067 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
112068 }
112069
112070 /* Jump here to skip this query
112071 */
@@ -112094,104 +112885,10 @@
112094 pParse->nSelectIndent--;
112095 #endif
112096 return rc;
112097 }
112098
112099 #ifdef SQLITE_DEBUG
112100 /*
112101 ** Generate a human-readable description of a the Select object.
112102 */
112103 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
112104 int n = 0;
112105 pView = sqlite3TreeViewPush(pView, moreToFollow);
112106 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
112107 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
112108 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
112109 );
112110 if( p->pSrc && p->pSrc->nSrc ) n++;
112111 if( p->pWhere ) n++;
112112 if( p->pGroupBy ) n++;
112113 if( p->pHaving ) n++;
112114 if( p->pOrderBy ) n++;
112115 if( p->pLimit ) n++;
112116 if( p->pOffset ) n++;
112117 if( p->pPrior ) n++;
112118 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
112119 if( p->pSrc && p->pSrc->nSrc ){
112120 int i;
112121 pView = sqlite3TreeViewPush(pView, (n--)>0);
112122 sqlite3TreeViewLine(pView, "FROM");
112123 for(i=0; i<p->pSrc->nSrc; i++){
112124 struct SrcList_item *pItem = &p->pSrc->a[i];
112125 StrAccum x;
112126 char zLine[100];
112127 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
112128 sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
112129 if( pItem->zDatabase ){
112130 sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
112131 }else if( pItem->zName ){
112132 sqlite3XPrintf(&x, 0, " %s", pItem->zName);
112133 }
112134 if( pItem->pTab ){
112135 sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
112136 }
112137 if( pItem->zAlias ){
112138 sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
112139 }
112140 if( pItem->jointype & JT_LEFT ){
112141 sqlite3XPrintf(&x, 0, " LEFT-JOIN");
112142 }
112143 sqlite3StrAccumFinish(&x);
112144 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
112145 if( pItem->pSelect ){
112146 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
112147 }
112148 sqlite3TreeViewPop(pView);
112149 }
112150 sqlite3TreeViewPop(pView);
112151 }
112152 if( p->pWhere ){
112153 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
112154 sqlite3TreeViewExpr(pView, p->pWhere, 0);
112155 sqlite3TreeViewPop(pView);
112156 }
112157 if( p->pGroupBy ){
112158 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
112159 }
112160 if( p->pHaving ){
112161 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
112162 sqlite3TreeViewExpr(pView, p->pHaving, 0);
112163 sqlite3TreeViewPop(pView);
112164 }
112165 if( p->pOrderBy ){
112166 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
112167 }
112168 if( p->pLimit ){
112169 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
112170 sqlite3TreeViewExpr(pView, p->pLimit, 0);
112171 sqlite3TreeViewPop(pView);
112172 }
112173 if( p->pOffset ){
112174 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
112175 sqlite3TreeViewExpr(pView, p->pOffset, 0);
112176 sqlite3TreeViewPop(pView);
112177 }
112178 if( p->pPrior ){
112179 const char *zOp = "UNION";
112180 switch( p->op ){
112181 case TK_ALL: zOp = "UNION ALL"; break;
112182 case TK_INTERSECT: zOp = "INTERSECT"; break;
112183 case TK_EXCEPT: zOp = "EXCEPT"; break;
112184 }
112185 sqlite3TreeViewItem(pView, zOp, (n--)>0);
112186 sqlite3TreeViewSelect(pView, p->pPrior, 0);
112187 sqlite3TreeViewPop(pView);
112188 }
112189 sqlite3TreeViewPop(pView);
112190 }
112191 #endif /* SQLITE_DEBUG */
112192
112193 /************** End of select.c **********************************************/
112194 /************** Begin file table.c *******************************************/
112195 /*
112196 ** 2001 September 15
112197 **
@@ -115499,24 +116196,25 @@
115499 ** The array is cleared after invoking the callbacks.
115500 */
115501 static void callFinaliser(sqlite3 *db, int offset){
115502 int i;
115503 if( db->aVTrans ){
 
 
115504 for(i=0; i<db->nVTrans; i++){
115505 VTable *pVTab = db->aVTrans[i];
115506 sqlite3_vtab *p = pVTab->pVtab;
115507 if( p ){
115508 int (*x)(sqlite3_vtab *);
115509 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
115510 if( x ) x(p);
115511 }
115512 pVTab->iSavepoint = 0;
115513 sqlite3VtabUnlock(pVTab);
115514 }
115515 sqlite3DbFree(db, db->aVTrans);
115516 db->nVTrans = 0;
115517 db->aVTrans = 0;
115518 }
115519 }
115520
115521 /*
115522 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
@@ -115812,13 +116510,13 @@
115812 }
115813
115814 #endif /* SQLITE_OMIT_VIRTUALTABLE */
115815
115816 /************** End of vtab.c ************************************************/
115817 /************** Begin file where.c *******************************************/
115818 /*
115819 ** 2001 September 15
115820 **
115821 ** The author disclaims copyright to this source code. In place of
115822 ** a legal notice, here is a blessing:
115823 **
115824 ** May you do good and not evil.
@@ -115825,17 +116523,18 @@
115825 ** May you find forgiveness for yourself and forgive others.
115826 ** May you share freely, never taking more than you give.
115827 **
115828 *************************************************************************
115829 ** This module contains C code that generates VDBE code used to process
115830 ** the WHERE clause of SQL statements. This module is responsible for
115831 ** generating the code that loops through a table looking for applicable
115832 ** rows. Indices are selected and used to speed the search when doing
115833 ** so is applicable. Because this module is responsible for selecting
115834 ** indices, you might also think of this module as the "query optimizer".
 
115835 */
115836 /************** Include whereInt.h in the middle of where.c ******************/
115837 /************** Begin file whereInt.h ****************************************/
115838 /*
115839 ** 2013-11-12
115840 **
115841 ** The author disclaims copyright to this source code. In place of
@@ -115854,11 +116553,11 @@
115854
115855 /*
115856 ** Trace output macros
115857 */
115858 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
115859 /***/ int sqlite3WhereTrace = 0;
115860 #endif
115861 #if defined(SQLITE_DEBUG) \
115862 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
115863 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
115864 # define WHERETRACE_ENABLED 1
@@ -115996,14 +116695,10 @@
115996 struct WhereOrSet {
115997 u16 n; /* Number of valid a[] entries */
115998 WhereOrCost a[N_OR_COST]; /* Set of best costs */
115999 };
116000
116001
116002 /* Forward declaration of methods */
116003 static int whereLoopResize(sqlite3*, WhereLoop*, int);
116004
116005 /*
116006 ** Each instance of this object holds a sequence of WhereLoop objects
116007 ** that implement some or all of a query plan.
116008 **
116009 ** Think of each WhereLoop object as a node in a graph with arcs
@@ -116207,10 +116902,15 @@
116207 struct WhereMaskSet {
116208 int n; /* Number of assigned cursor values */
116209 int ix[BMS]; /* Cursor assigned to each bit */
116210 };
116211
 
 
 
 
 
116212 /*
116213 ** This object is a convenience wrapper holding all information needed
116214 ** to construct WhereLoop objects for a particular query.
116215 */
116216 struct WhereLoopBuilder {
@@ -116257,10 +116957,66 @@
116257 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
116258 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
116259 WhereClause sWC; /* Decomposition of the WHERE clause */
116260 WhereLevel a[1]; /* Information about each nest loop in WHERE */
116261 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116262
116263 /*
116264 ** Bitmasks for the operators on WhereTerm objects. These are all
116265 ** operators that are of interest to the query planner. An
116266 ** OR-ed combination of these values can be used when searching for
@@ -116307,176 +117063,1532 @@
116307 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
116308 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
116309 #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
116310
116311 /************** End of whereInt.h ********************************************/
116312 /************** Continuing where we left off in where.c **********************/
116313
116314 /*
116315 ** Return the estimated number of output rows from a WHERE clause
116316 */
116317 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
116318 return sqlite3LogEstToInt(pWInfo->nRowOut);
116319 }
116320
116321 /*
116322 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
116323 ** WHERE clause returns outputs for DISTINCT processing.
116324 */
116325 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
116326 return pWInfo->eDistinct;
116327 }
116328
116329 /*
116330 ** Return TRUE if the WHERE clause returns rows in ORDER BY order.
116331 ** Return FALSE if the output needs to be sorted.
116332 */
116333 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
116334 return pWInfo->nOBSat;
116335 }
116336
116337 /*
116338 ** Return the VDBE address or label to jump to in order to continue
116339 ** immediately with the next row of a WHERE clause.
116340 */
116341 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
116342 assert( pWInfo->iContinue!=0 );
116343 return pWInfo->iContinue;
116344 }
116345
116346 /*
116347 ** Return the VDBE address or label to jump to in order to break
116348 ** out of a WHERE loop.
116349 */
116350 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
116351 return pWInfo->iBreak;
116352 }
116353
116354 /*
116355 ** Return TRUE if an UPDATE or DELETE statement can operate directly on
116356 ** the rowids returned by a WHERE clause. Return FALSE if doing an
116357 ** UPDATE or DELETE might change subsequent WHERE clause results.
116358 **
116359 ** If the ONEPASS optimization is used (if this routine returns true)
116360 ** then also write the indices of open cursors used by ONEPASS
116361 ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
116362 ** table and iaCur[1] gets the cursor used by an auxiliary index.
116363 ** Either value may be -1, indicating that cursor is not used.
116364 ** Any cursors returned will have been opened for writing.
116365 **
116366 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
116367 ** unable to use the ONEPASS optimization.
116368 */
116369 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
116370 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
116371 return pWInfo->okOnePass;
116372 }
116373
116374 /*
116375 ** Move the content of pSrc into pDest
116376 */
116377 static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
116378 pDest->n = pSrc->n;
116379 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
116380 }
116381
116382 /*
116383 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
116384 **
116385 ** The new entry might overwrite an existing entry, or it might be
116386 ** appended, or it might be discarded. Do whatever is the right thing
116387 ** so that pSet keeps the N_OR_COST best entries seen so far.
116388 */
116389 static int whereOrInsert(
116390 WhereOrSet *pSet, /* The WhereOrSet to be updated */
116391 Bitmask prereq, /* Prerequisites of the new entry */
116392 LogEst rRun, /* Run-cost of the new entry */
116393 LogEst nOut /* Number of outputs for the new entry */
116394 ){
116395 u16 i;
116396 WhereOrCost *p;
116397 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
116398 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
116399 goto whereOrInsert_done;
116400 }
116401 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
116402 return 0;
116403 }
116404 }
116405 if( pSet->n<N_OR_COST ){
116406 p = &pSet->a[pSet->n++];
116407 p->nOut = nOut;
116408 }else{
116409 p = pSet->a;
116410 for(i=1; i<pSet->n; i++){
116411 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
116412 }
116413 if( p->rRun<=rRun ) return 0;
116414 }
116415 whereOrInsert_done:
116416 p->prereq = prereq;
116417 p->rRun = rRun;
116418 if( p->nOut>nOut ) p->nOut = nOut;
116419 return 1;
116420 }
116421
116422 /*
116423 ** Initialize a preallocated WhereClause structure.
116424 */
116425 static void whereClauseInit(
116426 WhereClause *pWC, /* The WhereClause to be initialized */
116427 WhereInfo *pWInfo /* The WHERE processing context */
116428 ){
116429 pWC->pWInfo = pWInfo;
116430 pWC->pOuter = 0;
116431 pWC->nTerm = 0;
116432 pWC->nSlot = ArraySize(pWC->aStatic);
116433 pWC->a = pWC->aStatic;
116434 }
116435
116436 /* Forward reference */
116437 static void whereClauseClear(WhereClause*);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116438
116439 /*
116440 ** Deallocate all memory associated with a WhereOrInfo object.
116441 */
116442 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
116443 whereClauseClear(&p->wc);
116444 sqlite3DbFree(db, p);
116445 }
116446
116447 /*
116448 ** Deallocate all memory associated with a WhereAndInfo object.
116449 */
116450 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
116451 whereClauseClear(&p->wc);
116452 sqlite3DbFree(db, p);
116453 }
116454
116455 /*
116456 ** Deallocate a WhereClause structure. The WhereClause structure
116457 ** itself is not freed. This routine is the inverse of whereClauseInit().
116458 */
116459 static void whereClauseClear(WhereClause *pWC){
116460 int i;
116461 WhereTerm *a;
116462 sqlite3 *db = pWC->pWInfo->pParse->db;
116463 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
116464 if( a->wtFlags & TERM_DYNAMIC ){
116465 sqlite3ExprDelete(db, a->pExpr);
116466 }
116467 if( a->wtFlags & TERM_ORINFO ){
116468 whereOrInfoDelete(db, a->u.pOrInfo);
116469 }else if( a->wtFlags & TERM_ANDINFO ){
116470 whereAndInfoDelete(db, a->u.pAndInfo);
116471 }
116472 }
116473 if( pWC->a!=pWC->aStatic ){
116474 sqlite3DbFree(db, pWC->a);
116475 }
116476 }
116477
116478 /*
116479 ** Add a single new WhereTerm entry to the WhereClause object pWC.
116480 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
116481 ** The index in pWC->a[] of the new WhereTerm is returned on success.
116482 ** 0 is returned if the new WhereTerm could not be added due to a memory
@@ -116527,126 +118639,10 @@
116527 pTerm->pWC = pWC;
116528 pTerm->iParent = -1;
116529 return idx;
116530 }
116531
116532 /*
116533 ** This routine identifies subexpressions in the WHERE clause where
116534 ** each subexpression is separated by the AND operator or some other
116535 ** operator specified in the op parameter. The WhereClause structure
116536 ** is filled with pointers to subexpressions. For example:
116537 **
116538 ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
116539 ** \________/ \_______________/ \________________/
116540 ** slot[0] slot[1] slot[2]
116541 **
116542 ** The original WHERE clause in pExpr is unaltered. All this routine
116543 ** does is make slot[] entries point to substructure within pExpr.
116544 **
116545 ** In the previous sentence and in the diagram, "slot[]" refers to
116546 ** the WhereClause.a[] array. The slot[] array grows as needed to contain
116547 ** all terms of the WHERE clause.
116548 */
116549 static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
116550 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
116551 pWC->op = op;
116552 if( pE2==0 ) return;
116553 if( pE2->op!=op ){
116554 whereClauseInsert(pWC, pExpr, 0);
116555 }else{
116556 whereSplit(pWC, pE2->pLeft, op);
116557 whereSplit(pWC, pE2->pRight, op);
116558 }
116559 }
116560
116561 /*
116562 ** Initialize a WhereMaskSet object
116563 */
116564 #define initMaskSet(P) (P)->n=0
116565
116566 /*
116567 ** Return the bitmask for the given cursor number. Return 0 if
116568 ** iCursor is not in the set.
116569 */
116570 static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
116571 int i;
116572 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
116573 for(i=0; i<pMaskSet->n; i++){
116574 if( pMaskSet->ix[i]==iCursor ){
116575 return MASKBIT(i);
116576 }
116577 }
116578 return 0;
116579 }
116580
116581 /*
116582 ** Create a new mask for cursor iCursor.
116583 **
116584 ** There is one cursor per table in the FROM clause. The number of
116585 ** tables in the FROM clause is limited by a test early in the
116586 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
116587 ** array will never overflow.
116588 */
116589 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
116590 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
116591 pMaskSet->ix[pMaskSet->n++] = iCursor;
116592 }
116593
116594 /*
116595 ** These routines walk (recursively) an expression tree and generate
116596 ** a bitmask indicating which tables are used in that expression
116597 ** tree.
116598 */
116599 static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
116600 static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
116601 static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
116602 Bitmask mask = 0;
116603 if( p==0 ) return 0;
116604 if( p->op==TK_COLUMN ){
116605 mask = getMask(pMaskSet, p->iTable);
116606 return mask;
116607 }
116608 mask = exprTableUsage(pMaskSet, p->pRight);
116609 mask |= exprTableUsage(pMaskSet, p->pLeft);
116610 if( ExprHasProperty(p, EP_xIsSelect) ){
116611 mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
116612 }else{
116613 mask |= exprListTableUsage(pMaskSet, p->x.pList);
116614 }
116615 return mask;
116616 }
116617 static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
116618 int i;
116619 Bitmask mask = 0;
116620 if( pList ){
116621 for(i=0; i<pList->nExpr; i++){
116622 mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
116623 }
116624 }
116625 return mask;
116626 }
116627 static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
116628 Bitmask mask = 0;
116629 while( pS ){
116630 SrcList *pSrc = pS->pSrc;
116631 mask |= exprListTableUsage(pMaskSet, pS->pEList);
116632 mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
116633 mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
116634 mask |= exprTableUsage(pMaskSet, pS->pWhere);
116635 mask |= exprTableUsage(pMaskSet, pS->pHaving);
116636 if( ALWAYS(pSrc!=0) ){
116637 int i;
116638 for(i=0; i<pSrc->nSrc; i++){
116639 mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
116640 mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
116641 }
116642 }
116643 pS = pS->pPrior;
116644 }
116645 return mask;
116646 }
116647
116648 /*
116649 ** Return TRUE if the given operator is one of the operators that is
116650 ** allowed for an indexable WHERE clause term. The allowed operators are
116651 ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
116652 */
@@ -116723,203 +118719,10 @@
116723 assert( op!=TK_GE || c==WO_GE );
116724 assert( op!=TK_IS || c==WO_IS );
116725 return c;
116726 }
116727
116728 /*
116729 ** Advance to the next WhereTerm that matches according to the criteria
116730 ** established when the pScan object was initialized by whereScanInit().
116731 ** Return NULL if there are no more matching WhereTerms.
116732 */
116733 static WhereTerm *whereScanNext(WhereScan *pScan){
116734 int iCur; /* The cursor on the LHS of the term */
116735 int iColumn; /* The column on the LHS of the term. -1 for IPK */
116736 Expr *pX; /* An expression being tested */
116737 WhereClause *pWC; /* Shorthand for pScan->pWC */
116738 WhereTerm *pTerm; /* The term being tested */
116739 int k = pScan->k; /* Where to start scanning */
116740
116741 while( pScan->iEquiv<=pScan->nEquiv ){
116742 iCur = pScan->aEquiv[pScan->iEquiv-2];
116743 iColumn = pScan->aEquiv[pScan->iEquiv-1];
116744 while( (pWC = pScan->pWC)!=0 ){
116745 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
116746 if( pTerm->leftCursor==iCur
116747 && pTerm->u.leftColumn==iColumn
116748 && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
116749 ){
116750 if( (pTerm->eOperator & WO_EQUIV)!=0
116751 && pScan->nEquiv<ArraySize(pScan->aEquiv)
116752 ){
116753 int j;
116754 pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
116755 assert( pX->op==TK_COLUMN );
116756 for(j=0; j<pScan->nEquiv; j+=2){
116757 if( pScan->aEquiv[j]==pX->iTable
116758 && pScan->aEquiv[j+1]==pX->iColumn ){
116759 break;
116760 }
116761 }
116762 if( j==pScan->nEquiv ){
116763 pScan->aEquiv[j] = pX->iTable;
116764 pScan->aEquiv[j+1] = pX->iColumn;
116765 pScan->nEquiv += 2;
116766 }
116767 }
116768 if( (pTerm->eOperator & pScan->opMask)!=0 ){
116769 /* Verify the affinity and collating sequence match */
116770 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
116771 CollSeq *pColl;
116772 Parse *pParse = pWC->pWInfo->pParse;
116773 pX = pTerm->pExpr;
116774 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
116775 continue;
116776 }
116777 assert(pX->pLeft);
116778 pColl = sqlite3BinaryCompareCollSeq(pParse,
116779 pX->pLeft, pX->pRight);
116780 if( pColl==0 ) pColl = pParse->db->pDfltColl;
116781 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
116782 continue;
116783 }
116784 }
116785 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
116786 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
116787 && pX->iTable==pScan->aEquiv[0]
116788 && pX->iColumn==pScan->aEquiv[1]
116789 ){
116790 testcase( pTerm->eOperator & WO_IS );
116791 continue;
116792 }
116793 pScan->k = k+1;
116794 return pTerm;
116795 }
116796 }
116797 }
116798 pScan->pWC = pScan->pWC->pOuter;
116799 k = 0;
116800 }
116801 pScan->pWC = pScan->pOrigWC;
116802 k = 0;
116803 pScan->iEquiv += 2;
116804 }
116805 return 0;
116806 }
116807
116808 /*
116809 ** Initialize a WHERE clause scanner object. Return a pointer to the
116810 ** first match. Return NULL if there are no matches.
116811 **
116812 ** The scanner will be searching the WHERE clause pWC. It will look
116813 ** for terms of the form "X <op> <expr>" where X is column iColumn of table
116814 ** iCur. The <op> must be one of the operators described by opMask.
116815 **
116816 ** If the search is for X and the WHERE clause contains terms of the
116817 ** form X=Y then this routine might also return terms of the form
116818 ** "Y <op> <expr>". The number of levels of transitivity is limited,
116819 ** but is enough to handle most commonly occurring SQL statements.
116820 **
116821 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with
116822 ** index pIdx.
116823 */
116824 static WhereTerm *whereScanInit(
116825 WhereScan *pScan, /* The WhereScan object being initialized */
116826 WhereClause *pWC, /* The WHERE clause to be scanned */
116827 int iCur, /* Cursor to scan for */
116828 int iColumn, /* Column to scan for */
116829 u32 opMask, /* Operator(s) to scan for */
116830 Index *pIdx /* Must be compatible with this index */
116831 ){
116832 int j;
116833
116834 /* memset(pScan, 0, sizeof(*pScan)); */
116835 pScan->pOrigWC = pWC;
116836 pScan->pWC = pWC;
116837 if( pIdx && iColumn>=0 ){
116838 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
116839 for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
116840 if( NEVER(j>pIdx->nColumn) ) return 0;
116841 }
116842 pScan->zCollName = pIdx->azColl[j];
116843 }else{
116844 pScan->idxaff = 0;
116845 pScan->zCollName = 0;
116846 }
116847 pScan->opMask = opMask;
116848 pScan->k = 0;
116849 pScan->aEquiv[0] = iCur;
116850 pScan->aEquiv[1] = iColumn;
116851 pScan->nEquiv = 2;
116852 pScan->iEquiv = 2;
116853 return whereScanNext(pScan);
116854 }
116855
116856 /*
116857 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
116858 ** where X is a reference to the iColumn of table iCur and <op> is one of
116859 ** the WO_xx operator codes specified by the op parameter.
116860 ** Return a pointer to the term. Return 0 if not found.
116861 **
116862 ** The term returned might by Y=<expr> if there is another constraint in
116863 ** the WHERE clause that specifies that X=Y. Any such constraints will be
116864 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
116865 ** aEquiv[] array holds X and all its equivalents, with each SQL variable
116866 ** taking up two slots in aEquiv[]. The first slot is for the cursor number
116867 ** and the second is for the column number. There are 22 slots in aEquiv[]
116868 ** so that means we can look for X plus up to 10 other equivalent values.
116869 ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
116870 ** and ... and A9=A10 and A10=<expr>.
116871 **
116872 ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
116873 ** then try for the one with no dependencies on <expr> - in other words where
116874 ** <expr> is a constant expression of some kind. Only return entries of
116875 ** the form "X <op> Y" where Y is a column in another table if no terms of
116876 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
116877 ** exist, try to return a term that does not use WO_EQUIV.
116878 */
116879 static WhereTerm *findTerm(
116880 WhereClause *pWC, /* The WHERE clause to be searched */
116881 int iCur, /* Cursor number of LHS */
116882 int iColumn, /* Column number of LHS */
116883 Bitmask notReady, /* RHS must not overlap with this mask */
116884 u32 op, /* Mask of WO_xx values describing operator */
116885 Index *pIdx /* Must be compatible with this index, if not NULL */
116886 ){
116887 WhereTerm *pResult = 0;
116888 WhereTerm *p;
116889 WhereScan scan;
116890
116891 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
116892 op &= WO_EQ|WO_IS;
116893 while( p ){
116894 if( (p->prereqRight & notReady)==0 ){
116895 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
116896 testcase( p->eOperator & WO_IS );
116897 return p;
116898 }
116899 if( pResult==0 ) pResult = p;
116900 }
116901 p = whereScanNext(&scan);
116902 }
116903 return pResult;
116904 }
116905
116906 /* Forward reference */
116907 static void exprAnalyze(SrcList*, WhereClause*, int);
116908
116909 /*
116910 ** Call exprAnalyze on all terms in a WHERE clause.
116911 */
116912 static void exprAnalyzeAll(
116913 SrcList *pTabList, /* the FROM clause */
116914 WhereClause *pWC /* the WHERE clause to be analyzed */
116915 ){
116916 int i;
116917 for(i=pWC->nTerm-1; i>=0; i--){
116918 exprAnalyze(pTabList, pWC, i);
116919 }
116920 }
116921
116922 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
116923 /*
116924 ** Check to see if the given expression is a LIKE or GLOB operator that
116925 ** can be optimized using inequality constraints. Return TRUE if it is
@@ -116970,11 +118773,11 @@
116970 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
116971 op = pRight->op;
116972 if( op==TK_VARIABLE ){
116973 Vdbe *pReprepare = pParse->pReprepare;
116974 int iCol = pRight->iColumn;
116975 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
116976 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
116977 z = (char *)sqlite3_value_text(pVal);
116978 }
116979 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
116980 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
@@ -117181,11 +118984,11 @@
117181 **
117182 ** x IN (expr1,expr2,expr3)
117183 **
117184 ** CASE 2:
117185 **
117186 ** If there are exactly two disjuncts one side has x>A and the other side
117187 ** has x=A (for the same x and A) then add a new virtual conjunct term to the
117188 ** WHERE clause of the form "x>=A". Example:
117189 **
117190 ** x>A OR (x=A AND y>B) adds: x>=A
117191 **
@@ -117210,26 +119013,26 @@
117210 ** potentially be used with an index if an appropriate index exists.
117211 ** This analysis does not consider whether or not the index exists; that
117212 ** is decided elsewhere. This analysis only looks at whether subterms
117213 ** appropriate for indexing exist.
117214 **
117215 ** All examples A through E above satisfy case 2. But if a term
117216 ** also satisfies case 1 (such as B) we know that the optimizer will
117217 ** always prefer case 1, so in that case we pretend that case 2 is not
117218 ** satisfied.
117219 **
117220 ** It might be the case that multiple tables are indexable. For example,
117221 ** (E) above is indexable on tables P, Q, and R.
117222 **
117223 ** Terms that satisfy case 2 are candidates for lookup by using
117224 ** separate indices to find rowids for each subterm and composing
117225 ** the union of all rowids using a RowSet object. This is similar
117226 ** to "bitmap indices" in other database engines.
117227 **
117228 ** OTHERWISE:
117229 **
117230 ** If neither case 1 nor case 2 apply, then leave the eOperator set to
117231 ** zero. This term is not useful for search.
117232 */
117233 static void exprAnalyzeOrTerm(
117234 SrcList *pSrc, /* the FROM clause */
117235 WhereClause *pWC, /* the complete WHERE clause */
@@ -117256,18 +119059,18 @@
117256 assert( pExpr->op==TK_OR );
117257 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
117258 if( pOrInfo==0 ) return;
117259 pTerm->wtFlags |= TERM_ORINFO;
117260 pOrWc = &pOrInfo->wc;
117261 whereClauseInit(pOrWc, pWInfo);
117262 whereSplit(pOrWc, pExpr, TK_OR);
117263 exprAnalyzeAll(pSrc, pOrWc);
117264 if( db->mallocFailed ) return;
117265 assert( pOrWc->nTerm>=2 );
117266
117267 /*
117268 ** Compute the set of tables that might satisfy cases 1 or 2.
117269 */
117270 indexable = ~(Bitmask)0;
117271 chngToIN = ~(Bitmask)0;
117272 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
117273 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
@@ -117282,20 +119085,20 @@
117282 Bitmask b = 0;
117283 pOrTerm->u.pAndInfo = pAndInfo;
117284 pOrTerm->wtFlags |= TERM_ANDINFO;
117285 pOrTerm->eOperator = WO_AND;
117286 pAndWC = &pAndInfo->wc;
117287 whereClauseInit(pAndWC, pWC->pWInfo);
117288 whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
117289 exprAnalyzeAll(pSrc, pAndWC);
117290 pAndWC->pOuter = pWC;
117291 testcase( db->mallocFailed );
117292 if( !db->mallocFailed ){
117293 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
117294 assert( pAndTerm->pExpr );
117295 if( allowedOp(pAndTerm->pExpr->op) ){
117296 b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
117297 }
117298 }
117299 }
117300 indexable &= b;
117301 }
@@ -117302,14 +119105,14 @@
117302 }else if( pOrTerm->wtFlags & TERM_COPIED ){
117303 /* Skip this term for now. We revisit it when we process the
117304 ** corresponding TERM_VIRTUAL term */
117305 }else{
117306 Bitmask b;
117307 b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
117308 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
117309 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
117310 b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
117311 }
117312 indexable &= b;
117313 if( (pOrTerm->eOperator & WO_EQ)==0 ){
117314 chngToIN = 0;
117315 }else{
@@ -117381,11 +119184,12 @@
117381 /* This is the 2-bit case and we are on the second iteration and
117382 ** current term is from the first iteration. So skip this term. */
117383 assert( j==1 );
117384 continue;
117385 }
117386 if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
 
117387 /* This term must be of the form t1.a==t2.b where t2 is in the
117388 ** chngToIN set but t1 is not. This term will be either preceded
117389 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
117390 ** and use its inversion. */
117391 testcase( pOrTerm->wtFlags & TERM_COPIED );
@@ -117400,11 +119204,11 @@
117400 if( i<0 ){
117401 /* No candidate table+column was found. This can only occur
117402 ** on the second iteration */
117403 assert( j==1 );
117404 assert( IsPowerOfTwo(chngToIN) );
117405 assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
117406 break;
117407 }
117408 testcase( j==1 );
117409
117410 /* We have found a candidate table and column. Check to see if that
@@ -117478,11 +119282,11 @@
117478 ** We already know that pExpr is a binary operator where both operands are
117479 ** column references. This routine checks to see if pExpr is an equivalence
117480 ** relation:
117481 ** 1. The SQLITE_Transitive optimization must be enabled
117482 ** 2. Must be either an == or an IS operator
117483 ** 3. Not originating the ON clause of an OUTER JOIN
117484 ** 4. The affinities of A and B must be compatible
117485 ** 5a. Both operands use the same collating sequence OR
117486 ** 5b. The overall collating sequence is BINARY
117487 ** If this routine returns TRUE, that means that the RHS can be substituted
117488 ** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
@@ -117511,10 +119315,36 @@
117511 zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
117512 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
117513 zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
117514 return sqlite3StrICmp(zColl1, zColl2)==0;
117515 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117516
117517 /*
117518 ** The input to this routine is an WhereTerm structure with only the
117519 ** "pExpr" field filled in. The job of this routine is to analyze the
117520 ** subexpression and populate all the other fields of the WhereTerm
@@ -117556,27 +119386,27 @@
117556 }
117557 pTerm = &pWC->a[idxTerm];
117558 pMaskSet = &pWInfo->sMaskSet;
117559 pExpr = pTerm->pExpr;
117560 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
117561 prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
117562 op = pExpr->op;
117563 if( op==TK_IN ){
117564 assert( pExpr->pRight==0 );
117565 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
117566 pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
117567 }else{
117568 pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
117569 }
117570 }else if( op==TK_ISNULL ){
117571 pTerm->prereqRight = 0;
117572 }else{
117573 pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
117574 }
117575 prereqAll = exprTableUsage(pMaskSet, pExpr);
117576 if( ExprHasProperty(pExpr, EP_FromJoin) ){
117577 Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
117578 prereqAll |= x;
117579 extraRight = x-1; /* ON clause terms may not be used with an index
117580 ** on left table of a LEFT JOIN. Ticket #3015 */
117581 }
117582 pTerm->prereqAll = prereqAll;
@@ -117777,12 +119607,12 @@
117777 WhereTerm *pNewTerm;
117778 Bitmask prereqColumn, prereqExpr;
117779
117780 pRight = pExpr->x.pList->a[0].pExpr;
117781 pLeft = pExpr->x.pList->a[1].pExpr;
117782 prereqExpr = exprTableUsage(pMaskSet, pRight);
117783 prereqColumn = exprTableUsage(pMaskSet, pLeft);
117784 if( (prereqExpr & prereqColumn)==0 ){
117785 Expr *pNewExpr;
117786 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
117787 0, sqlite3ExprDup(db, pRight, 0), 0);
117788 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
@@ -117841,10 +119671,477 @@
117841 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
117842 ** an index for tables to the left of the join.
117843 */
117844 pTerm->prereqRight |= extraRight;
117845 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117846
117847 /*
117848 ** This function searches pList for an entry that matches the iCol-th column
117849 ** of index pIdx.
117850 **
@@ -117879,12 +120176,12 @@
117879
117880 /*
117881 ** Return true if the DISTINCT expression-list passed as the third argument
117882 ** is redundant.
117883 **
117884 ** A DISTINCT list is redundant if the database contains some subset of
117885 ** columns that are unique and non-null.
117886 */
117887 static int isDistinctRedundant(
117888 Parse *pParse, /* Parsing context */
117889 SrcList *pTabList, /* The FROM clause */
117890 WhereClause *pWC, /* The WHERE clause */
@@ -117926,11 +120223,11 @@
117926 */
117927 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117928 if( !IsUniqueIndex(pIdx) ) continue;
117929 for(i=0; i<pIdx->nKeyCol; i++){
117930 i16 iCol = pIdx->aiColumn[i];
117931 if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
117932 int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
117933 if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
117934 break;
117935 }
117936 }
@@ -118257,10 +120554,11 @@
118257 ** by passing the pointer returned by this function to sqlite3_free().
118258 */
118259 static sqlite3_index_info *allocateIndexInfo(
118260 Parse *pParse,
118261 WhereClause *pWC,
 
118262 struct SrcList_item *pSrc,
118263 ExprList *pOrderBy
118264 ){
118265 int i, j;
118266 int nTerm;
@@ -118273,10 +120571,11 @@
118273
118274 /* Count the number of possible WHERE clause constraints referring
118275 ** to this virtual table */
118276 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118277 if( pTerm->leftCursor != pSrc->iCursor ) continue;
 
118278 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118279 testcase( pTerm->eOperator & WO_IN );
118280 testcase( pTerm->eOperator & WO_ISNULL );
118281 testcase( pTerm->eOperator & WO_IS );
118282 testcase( pTerm->eOperator & WO_ALL );
@@ -118327,10 +120626,11 @@
118327 pUsage;
118328
118329 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
118330 u8 op;
118331 if( pTerm->leftCursor != pSrc->iCursor ) continue;
 
118332 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
118333 testcase( pTerm->eOperator & WO_IN );
118334 testcase( pTerm->eOperator & WO_IS );
118335 testcase( pTerm->eOperator & WO_ISNULL );
118336 testcase( pTerm->eOperator & WO_ALL );
@@ -119048,1491 +121348,10 @@
119048 assert( pBuilder->nRecValid==nRecValid );
119049 return rc;
119050 }
119051 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
119052
119053 /*
119054 ** Disable a term in the WHERE clause. Except, do not disable the term
119055 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
119056 ** or USING clause of that join.
119057 **
119058 ** Consider the term t2.z='ok' in the following queries:
119059 **
119060 ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
119061 ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
119062 ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
119063 **
119064 ** The t2.z='ok' is disabled in the in (2) because it originates
119065 ** in the ON clause. The term is disabled in (3) because it is not part
119066 ** of a LEFT OUTER JOIN. In (1), the term is not disabled.
119067 **
119068 ** Disabling a term causes that term to not be tested in the inner loop
119069 ** of the join. Disabling is an optimization. When terms are satisfied
119070 ** by indices, we disable them to prevent redundant tests in the inner
119071 ** loop. We would get the correct results if nothing were ever disabled,
119072 ** but joins might run a little slower. The trick is to disable as much
119073 ** as we can without disabling too much. If we disabled in (1), we'd get
119074 ** the wrong answer. See ticket #813.
119075 **
119076 ** If all the children of a term are disabled, then that term is also
119077 ** automatically disabled. In this way, terms get disabled if derived
119078 ** virtual terms are tested first. For example:
119079 **
119080 ** x GLOB 'abc*' AND x>='abc' AND x<'acd'
119081 ** \___________/ \______/ \_____/
119082 ** parent child1 child2
119083 **
119084 ** Only the parent term was in the original WHERE clause. The child1
119085 ** and child2 terms were added by the LIKE optimization. If both of
119086 ** the virtual child terms are valid, then testing of the parent can be
119087 ** skipped.
119088 **
119089 ** Usually the parent term is marked as TERM_CODED. But if the parent
119090 ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
119091 ** The TERM_LIKECOND marking indicates that the term should be coded inside
119092 ** a conditional such that is only evaluated on the second pass of a
119093 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
119094 */
119095 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
119096 int nLoop = 0;
119097 while( pTerm
119098 && (pTerm->wtFlags & TERM_CODED)==0
119099 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
119100 && (pLevel->notReady & pTerm->prereqAll)==0
119101 ){
119102 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
119103 pTerm->wtFlags |= TERM_LIKECOND;
119104 }else{
119105 pTerm->wtFlags |= TERM_CODED;
119106 }
119107 if( pTerm->iParent<0 ) break;
119108 pTerm = &pTerm->pWC->a[pTerm->iParent];
119109 pTerm->nChild--;
119110 if( pTerm->nChild!=0 ) break;
119111 nLoop++;
119112 }
119113 }
119114
119115 /*
119116 ** Code an OP_Affinity opcode to apply the column affinity string zAff
119117 ** to the n registers starting at base.
119118 **
119119 ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
119120 ** beginning and end of zAff are ignored. If all entries in zAff are
119121 ** SQLITE_AFF_NONE, then no code gets generated.
119122 **
119123 ** This routine makes its own copy of zAff so that the caller is free
119124 ** to modify zAff after this routine returns.
119125 */
119126 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
119127 Vdbe *v = pParse->pVdbe;
119128 if( zAff==0 ){
119129 assert( pParse->db->mallocFailed );
119130 return;
119131 }
119132 assert( v!=0 );
119133
119134 /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
119135 ** and end of the affinity string.
119136 */
119137 while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
119138 n--;
119139 base++;
119140 zAff++;
119141 }
119142 while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
119143 n--;
119144 }
119145
119146 /* Code the OP_Affinity opcode if there is anything left to do. */
119147 if( n>0 ){
119148 sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
119149 sqlite3VdbeChangeP4(v, -1, zAff, n);
119150 sqlite3ExprCacheAffinityChange(pParse, base, n);
119151 }
119152 }
119153
119154
119155 /*
119156 ** Generate code for a single equality term of the WHERE clause. An equality
119157 ** term can be either X=expr or X IN (...). pTerm is the term to be
119158 ** coded.
119159 **
119160 ** The current value for the constraint is left in register iReg.
119161 **
119162 ** For a constraint of the form X=expr, the expression is evaluated and its
119163 ** result is left on the stack. For constraints of the form X IN (...)
119164 ** this routine sets up a loop that will iterate over all values of X.
119165 */
119166 static int codeEqualityTerm(
119167 Parse *pParse, /* The parsing context */
119168 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
119169 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
119170 int iEq, /* Index of the equality term within this level */
119171 int bRev, /* True for reverse-order IN operations */
119172 int iTarget /* Attempt to leave results in this register */
119173 ){
119174 Expr *pX = pTerm->pExpr;
119175 Vdbe *v = pParse->pVdbe;
119176 int iReg; /* Register holding results */
119177
119178 assert( iTarget>0 );
119179 if( pX->op==TK_EQ || pX->op==TK_IS ){
119180 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
119181 }else if( pX->op==TK_ISNULL ){
119182 iReg = iTarget;
119183 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
119184 #ifndef SQLITE_OMIT_SUBQUERY
119185 }else{
119186 int eType;
119187 int iTab;
119188 struct InLoop *pIn;
119189 WhereLoop *pLoop = pLevel->pWLoop;
119190
119191 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
119192 && pLoop->u.btree.pIndex!=0
119193 && pLoop->u.btree.pIndex->aSortOrder[iEq]
119194 ){
119195 testcase( iEq==0 );
119196 testcase( bRev );
119197 bRev = !bRev;
119198 }
119199 assert( pX->op==TK_IN );
119200 iReg = iTarget;
119201 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
119202 if( eType==IN_INDEX_INDEX_DESC ){
119203 testcase( bRev );
119204 bRev = !bRev;
119205 }
119206 iTab = pX->iTable;
119207 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
119208 VdbeCoverageIf(v, bRev);
119209 VdbeCoverageIf(v, !bRev);
119210 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
119211 pLoop->wsFlags |= WHERE_IN_ABLE;
119212 if( pLevel->u.in.nIn==0 ){
119213 pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
119214 }
119215 pLevel->u.in.nIn++;
119216 pLevel->u.in.aInLoop =
119217 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
119218 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
119219 pIn = pLevel->u.in.aInLoop;
119220 if( pIn ){
119221 pIn += pLevel->u.in.nIn - 1;
119222 pIn->iCur = iTab;
119223 if( eType==IN_INDEX_ROWID ){
119224 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
119225 }else{
119226 pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
119227 }
119228 pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
119229 sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
119230 }else{
119231 pLevel->u.in.nIn = 0;
119232 }
119233 #endif
119234 }
119235 disableTerm(pLevel, pTerm);
119236 return iReg;
119237 }
119238
119239 /*
119240 ** Generate code that will evaluate all == and IN constraints for an
119241 ** index scan.
119242 **
119243 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
119244 ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
119245 ** The index has as many as three equality constraints, but in this
119246 ** example, the third "c" value is an inequality. So only two
119247 ** constraints are coded. This routine will generate code to evaluate
119248 ** a==5 and b IN (1,2,3). The current values for a and b will be stored
119249 ** in consecutive registers and the index of the first register is returned.
119250 **
119251 ** In the example above nEq==2. But this subroutine works for any value
119252 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
119253 ** The only thing it does is allocate the pLevel->iMem memory cell and
119254 ** compute the affinity string.
119255 **
119256 ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
119257 ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
119258 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
119259 ** occurs after the nEq quality constraints.
119260 **
119261 ** This routine allocates a range of nEq+nExtraReg memory cells and returns
119262 ** the index of the first memory cell in that range. The code that
119263 ** calls this routine will use that memory range to store keys for
119264 ** start and termination conditions of the loop.
119265 ** key value of the loop. If one or more IN operators appear, then
119266 ** this routine allocates an additional nEq memory cells for internal
119267 ** use.
119268 **
119269 ** Before returning, *pzAff is set to point to a buffer containing a
119270 ** copy of the column affinity string of the index allocated using
119271 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
119272 ** with equality constraints that use NONE affinity are set to
119273 ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
119274 **
119275 ** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
119276 ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
119277 **
119278 ** In the example above, the index on t1(a) has TEXT affinity. But since
119279 ** the right hand side of the equality constraint (t2.b) has NONE affinity,
119280 ** no conversion should be attempted before using a t2.b value as part of
119281 ** a key to search the index. Hence the first byte in the returned affinity
119282 ** string in this example would be set to SQLITE_AFF_NONE.
119283 */
119284 static int codeAllEqualityTerms(
119285 Parse *pParse, /* Parsing context */
119286 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
119287 int bRev, /* Reverse the order of IN operators */
119288 int nExtraReg, /* Number of extra registers to allocate */
119289 char **pzAff /* OUT: Set to point to affinity string */
119290 ){
119291 u16 nEq; /* The number of == or IN constraints to code */
119292 u16 nSkip; /* Number of left-most columns to skip */
119293 Vdbe *v = pParse->pVdbe; /* The vm under construction */
119294 Index *pIdx; /* The index being used for this loop */
119295 WhereTerm *pTerm; /* A single constraint term */
119296 WhereLoop *pLoop; /* The WhereLoop object */
119297 int j; /* Loop counter */
119298 int regBase; /* Base register */
119299 int nReg; /* Number of registers to allocate */
119300 char *zAff; /* Affinity string to return */
119301
119302 /* This module is only called on query plans that use an index. */
119303 pLoop = pLevel->pWLoop;
119304 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
119305 nEq = pLoop->u.btree.nEq;
119306 nSkip = pLoop->nSkip;
119307 pIdx = pLoop->u.btree.pIndex;
119308 assert( pIdx!=0 );
119309
119310 /* Figure out how many memory cells we will need then allocate them.
119311 */
119312 regBase = pParse->nMem + 1;
119313 nReg = pLoop->u.btree.nEq + nExtraReg;
119314 pParse->nMem += nReg;
119315
119316 zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
119317 if( !zAff ){
119318 pParse->db->mallocFailed = 1;
119319 }
119320
119321 if( nSkip ){
119322 int iIdxCur = pLevel->iIdxCur;
119323 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
119324 VdbeCoverageIf(v, bRev==0);
119325 VdbeCoverageIf(v, bRev!=0);
119326 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
119327 j = sqlite3VdbeAddOp0(v, OP_Goto);
119328 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
119329 iIdxCur, 0, regBase, nSkip);
119330 VdbeCoverageIf(v, bRev==0);
119331 VdbeCoverageIf(v, bRev!=0);
119332 sqlite3VdbeJumpHere(v, j);
119333 for(j=0; j<nSkip; j++){
119334 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
119335 assert( pIdx->aiColumn[j]>=0 );
119336 VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
119337 }
119338 }
119339
119340 /* Evaluate the equality constraints
119341 */
119342 assert( zAff==0 || (int)strlen(zAff)>=nEq );
119343 for(j=nSkip; j<nEq; j++){
119344 int r1;
119345 pTerm = pLoop->aLTerm[j];
119346 assert( pTerm!=0 );
119347 /* The following testcase is true for indices with redundant columns.
119348 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
119349 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
119350 testcase( pTerm->wtFlags & TERM_VIRTUAL );
119351 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
119352 if( r1!=regBase+j ){
119353 if( nReg==1 ){
119354 sqlite3ReleaseTempReg(pParse, regBase);
119355 regBase = r1;
119356 }else{
119357 sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
119358 }
119359 }
119360 testcase( pTerm->eOperator & WO_ISNULL );
119361 testcase( pTerm->eOperator & WO_IN );
119362 if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
119363 Expr *pRight = pTerm->pExpr->pRight;
119364 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
119365 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
119366 VdbeCoverage(v);
119367 }
119368 if( zAff ){
119369 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
119370 zAff[j] = SQLITE_AFF_NONE;
119371 }
119372 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
119373 zAff[j] = SQLITE_AFF_NONE;
119374 }
119375 }
119376 }
119377 }
119378 *pzAff = zAff;
119379 return regBase;
119380 }
119381
119382 #ifndef SQLITE_OMIT_EXPLAIN
119383 /*
119384 ** This routine is a helper for explainIndexRange() below
119385 **
119386 ** pStr holds the text of an expression that we are building up one term
119387 ** at a time. This routine adds a new term to the end of the expression.
119388 ** Terms are separated by AND so add the "AND" text for second and subsequent
119389 ** terms only.
119390 */
119391 static void explainAppendTerm(
119392 StrAccum *pStr, /* The text expression being built */
119393 int iTerm, /* Index of this term. First is zero */
119394 const char *zColumn, /* Name of the column */
119395 const char *zOp /* Name of the operator */
119396 ){
119397 if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
119398 sqlite3StrAccumAppendAll(pStr, zColumn);
119399 sqlite3StrAccumAppend(pStr, zOp, 1);
119400 sqlite3StrAccumAppend(pStr, "?", 1);
119401 }
119402
119403 /*
119404 ** Argument pLevel describes a strategy for scanning table pTab. This
119405 ** function appends text to pStr that describes the subset of table
119406 ** rows scanned by the strategy in the form of an SQL expression.
119407 **
119408 ** For example, if the query:
119409 **
119410 ** SELECT * FROM t1 WHERE a=1 AND b>2;
119411 **
119412 ** is run and there is an index on (a, b), then this function returns a
119413 ** string similar to:
119414 **
119415 ** "a=? AND b>?"
119416 */
119417 static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
119418 Index *pIndex = pLoop->u.btree.pIndex;
119419 u16 nEq = pLoop->u.btree.nEq;
119420 u16 nSkip = pLoop->nSkip;
119421 int i, j;
119422 Column *aCol = pTab->aCol;
119423 i16 *aiColumn = pIndex->aiColumn;
119424
119425 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
119426 sqlite3StrAccumAppend(pStr, " (", 2);
119427 for(i=0; i<nEq; i++){
119428 char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
119429 if( i>=nSkip ){
119430 explainAppendTerm(pStr, i, z, "=");
119431 }else{
119432 if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
119433 sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
119434 }
119435 }
119436
119437 j = i;
119438 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
119439 char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
119440 explainAppendTerm(pStr, i++, z, ">");
119441 }
119442 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
119443 char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
119444 explainAppendTerm(pStr, i, z, "<");
119445 }
119446 sqlite3StrAccumAppend(pStr, ")", 1);
119447 }
119448
119449 /*
119450 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
119451 ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
119452 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
119453 ** is added to the output to describe the table scan strategy in pLevel.
119454 **
119455 ** If an OP_Explain opcode is added to the VM, its address is returned.
119456 ** Otherwise, if no OP_Explain is coded, zero is returned.
119457 */
119458 static int explainOneScan(
119459 Parse *pParse, /* Parse context */
119460 SrcList *pTabList, /* Table list this loop refers to */
119461 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
119462 int iLevel, /* Value for "level" column of output */
119463 int iFrom, /* Value for "from" column of output */
119464 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
119465 ){
119466 int ret = 0;
119467 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
119468 if( pParse->explain==2 )
119469 #endif
119470 {
119471 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
119472 Vdbe *v = pParse->pVdbe; /* VM being constructed */
119473 sqlite3 *db = pParse->db; /* Database handle */
119474 int iId = pParse->iSelectId; /* Select id (left-most output column) */
119475 int isSearch; /* True for a SEARCH. False for SCAN. */
119476 WhereLoop *pLoop; /* The controlling WhereLoop object */
119477 u32 flags; /* Flags that describe this loop */
119478 char *zMsg; /* Text to add to EQP output */
119479 StrAccum str; /* EQP output string */
119480 char zBuf[100]; /* Initial space for EQP output string */
119481
119482 pLoop = pLevel->pWLoop;
119483 flags = pLoop->wsFlags;
119484 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
119485
119486 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
119487 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
119488 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
119489
119490 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
119491 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
119492 if( pItem->pSelect ){
119493 sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
119494 }else{
119495 sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
119496 }
119497
119498 if( pItem->zAlias ){
119499 sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
119500 }
119501 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
119502 const char *zFmt = 0;
119503 Index *pIdx;
119504
119505 assert( pLoop->u.btree.pIndex!=0 );
119506 pIdx = pLoop->u.btree.pIndex;
119507 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
119508 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
119509 if( isSearch ){
119510 zFmt = "PRIMARY KEY";
119511 }
119512 }else if( flags & WHERE_PARTIALIDX ){
119513 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
119514 }else if( flags & WHERE_AUTO_INDEX ){
119515 zFmt = "AUTOMATIC COVERING INDEX";
119516 }else if( flags & WHERE_IDX_ONLY ){
119517 zFmt = "COVERING INDEX %s";
119518 }else{
119519 zFmt = "INDEX %s";
119520 }
119521 if( zFmt ){
119522 sqlite3StrAccumAppend(&str, " USING ", 7);
119523 sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
119524 explainIndexRange(&str, pLoop, pItem->pTab);
119525 }
119526 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
119527 const char *zRange;
119528 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
119529 zRange = "(rowid=?)";
119530 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
119531 zRange = "(rowid>? AND rowid<?)";
119532 }else if( flags&WHERE_BTM_LIMIT ){
119533 zRange = "(rowid>?)";
119534 }else{
119535 assert( flags&WHERE_TOP_LIMIT);
119536 zRange = "(rowid<?)";
119537 }
119538 sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
119539 sqlite3StrAccumAppendAll(&str, zRange);
119540 }
119541 #ifndef SQLITE_OMIT_VIRTUALTABLE
119542 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
119543 sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
119544 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
119545 }
119546 #endif
119547 #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
119548 if( pLoop->nOut>=10 ){
119549 sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
119550 }else{
119551 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
119552 }
119553 #endif
119554 zMsg = sqlite3StrAccumFinish(&str);
119555 ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
119556 }
119557 return ret;
119558 }
119559 #else
119560 # define explainOneScan(u,v,w,x,y,z) 0
119561 #endif /* SQLITE_OMIT_EXPLAIN */
119562
119563 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
119564 /*
119565 ** Configure the VM passed as the first argument with an
119566 ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
119567 ** implement level pLvl. Argument pSrclist is a pointer to the FROM
119568 ** clause that the scan reads data from.
119569 **
119570 ** If argument addrExplain is not 0, it must be the address of an
119571 ** OP_Explain instruction that describes the same loop.
119572 */
119573 static void addScanStatus(
119574 Vdbe *v, /* Vdbe to add scanstatus entry to */
119575 SrcList *pSrclist, /* FROM clause pLvl reads data from */
119576 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
119577 int addrExplain /* Address of OP_Explain (or 0) */
119578 ){
119579 const char *zObj = 0;
119580 WhereLoop *pLoop = pLvl->pWLoop;
119581 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
119582 zObj = pLoop->u.btree.pIndex->zName;
119583 }else{
119584 zObj = pSrclist->a[pLvl->iFrom].zName;
119585 }
119586 sqlite3VdbeScanStatus(
119587 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
119588 );
119589 }
119590 #else
119591 # define addScanStatus(a, b, c, d) ((void)d)
119592 #endif
119593
119594 /*
119595 ** If the most recently coded instruction is a constant range contraint
119596 ** that originated from the LIKE optimization, then change the P3 to be
119597 ** pLoop->iLikeRepCntr and set P5.
119598 **
119599 ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
119600 ** expression: "x>='ABC' AND x<'abd'". But this requires that the range
119601 ** scan loop run twice, once for strings and a second time for BLOBs.
119602 ** The OP_String opcodes on the second pass convert the upper and lower
119603 ** bound string contants to blobs. This routine makes the necessary changes
119604 ** to the OP_String opcodes for that to happen.
119605 */
119606 static void whereLikeOptimizationStringFixup(
119607 Vdbe *v, /* prepared statement under construction */
119608 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
119609 WhereTerm *pTerm /* The upper or lower bound just coded */
119610 ){
119611 if( pTerm->wtFlags & TERM_LIKEOPT ){
119612 VdbeOp *pOp;
119613 assert( pLevel->iLikeRepCntr>0 );
119614 pOp = sqlite3VdbeGetOp(v, -1);
119615 assert( pOp!=0 );
119616 assert( pOp->opcode==OP_String8
119617 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
119618 pOp->p3 = pLevel->iLikeRepCntr;
119619 pOp->p5 = 1;
119620 }
119621 }
119622
119623 /*
119624 ** Generate code for the start of the iLevel-th loop in the WHERE clause
119625 ** implementation described by pWInfo.
119626 */
119627 static Bitmask codeOneLoopStart(
119628 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
119629 int iLevel, /* Which level of pWInfo->a[] should be coded */
119630 Bitmask notReady /* Which tables are currently available */
119631 ){
119632 int j, k; /* Loop counters */
119633 int iCur; /* The VDBE cursor for the table */
119634 int addrNxt; /* Where to jump to continue with the next IN case */
119635 int omitTable; /* True if we use the index only */
119636 int bRev; /* True if we need to scan in reverse order */
119637 WhereLevel *pLevel; /* The where level to be coded */
119638 WhereLoop *pLoop; /* The WhereLoop object being coded */
119639 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
119640 WhereTerm *pTerm; /* A WHERE clause term */
119641 Parse *pParse; /* Parsing context */
119642 sqlite3 *db; /* Database connection */
119643 Vdbe *v; /* The prepared stmt under constructions */
119644 struct SrcList_item *pTabItem; /* FROM clause term being coded */
119645 int addrBrk; /* Jump here to break out of the loop */
119646 int addrCont; /* Jump here to continue with next cycle */
119647 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
119648 int iReleaseReg = 0; /* Temp register to free before returning */
119649
119650 pParse = pWInfo->pParse;
119651 v = pParse->pVdbe;
119652 pWC = &pWInfo->sWC;
119653 db = pParse->db;
119654 pLevel = &pWInfo->a[iLevel];
119655 pLoop = pLevel->pWLoop;
119656 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
119657 iCur = pTabItem->iCursor;
119658 pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
119659 bRev = (pWInfo->revMask>>iLevel)&1;
119660 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
119661 && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
119662 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
119663
119664 /* Create labels for the "break" and "continue" instructions
119665 ** for the current loop. Jump to addrBrk to break out of a loop.
119666 ** Jump to cont to go immediately to the next iteration of the
119667 ** loop.
119668 **
119669 ** When there is an IN operator, we also have a "addrNxt" label that
119670 ** means to continue with the next IN value combination. When
119671 ** there are no IN operators in the constraints, the "addrNxt" label
119672 ** is the same as "addrBrk".
119673 */
119674 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
119675 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
119676
119677 /* If this is the right table of a LEFT OUTER JOIN, allocate and
119678 ** initialize a memory cell that records if this table matches any
119679 ** row of the left table of the join.
119680 */
119681 if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
119682 pLevel->iLeftJoin = ++pParse->nMem;
119683 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
119684 VdbeComment((v, "init LEFT JOIN no-match flag"));
119685 }
119686
119687 /* Special case of a FROM clause subquery implemented as a co-routine */
119688 if( pTabItem->viaCoroutine ){
119689 int regYield = pTabItem->regReturn;
119690 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
119691 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
119692 VdbeCoverage(v);
119693 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
119694 pLevel->op = OP_Goto;
119695 }else
119696
119697 #ifndef SQLITE_OMIT_VIRTUALTABLE
119698 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
119699 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
119700 ** to access the data.
119701 */
119702 int iReg; /* P3 Value for OP_VFilter */
119703 int addrNotFound;
119704 int nConstraint = pLoop->nLTerm;
119705
119706 sqlite3ExprCachePush(pParse);
119707 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
119708 addrNotFound = pLevel->addrBrk;
119709 for(j=0; j<nConstraint; j++){
119710 int iTarget = iReg+j+2;
119711 pTerm = pLoop->aLTerm[j];
119712 if( pTerm==0 ) continue;
119713 if( pTerm->eOperator & WO_IN ){
119714 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
119715 addrNotFound = pLevel->addrNxt;
119716 }else{
119717 sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
119718 }
119719 }
119720 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
119721 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
119722 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
119723 pLoop->u.vtab.idxStr,
119724 pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
119725 VdbeCoverage(v);
119726 pLoop->u.vtab.needFree = 0;
119727 for(j=0; j<nConstraint && j<16; j++){
119728 if( (pLoop->u.vtab.omitMask>>j)&1 ){
119729 disableTerm(pLevel, pLoop->aLTerm[j]);
119730 }
119731 }
119732 pLevel->op = OP_VNext;
119733 pLevel->p1 = iCur;
119734 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
119735 sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
119736 sqlite3ExprCachePop(pParse);
119737 }else
119738 #endif /* SQLITE_OMIT_VIRTUALTABLE */
119739
119740 if( (pLoop->wsFlags & WHERE_IPK)!=0
119741 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
119742 ){
119743 /* Case 2: We can directly reference a single row using an
119744 ** equality comparison against the ROWID field. Or
119745 ** we reference multiple rows using a "rowid IN (...)"
119746 ** construct.
119747 */
119748 assert( pLoop->u.btree.nEq==1 );
119749 pTerm = pLoop->aLTerm[0];
119750 assert( pTerm!=0 );
119751 assert( pTerm->pExpr!=0 );
119752 assert( omitTable==0 );
119753 testcase( pTerm->wtFlags & TERM_VIRTUAL );
119754 iReleaseReg = ++pParse->nMem;
119755 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
119756 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
119757 addrNxt = pLevel->addrNxt;
119758 sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
119759 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
119760 VdbeCoverage(v);
119761 sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
119762 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
119763 VdbeComment((v, "pk"));
119764 pLevel->op = OP_Noop;
119765 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
119766 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
119767 ){
119768 /* Case 3: We have an inequality comparison against the ROWID field.
119769 */
119770 int testOp = OP_Noop;
119771 int start;
119772 int memEndValue = 0;
119773 WhereTerm *pStart, *pEnd;
119774
119775 assert( omitTable==0 );
119776 j = 0;
119777 pStart = pEnd = 0;
119778 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
119779 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
119780 assert( pStart!=0 || pEnd!=0 );
119781 if( bRev ){
119782 pTerm = pStart;
119783 pStart = pEnd;
119784 pEnd = pTerm;
119785 }
119786 if( pStart ){
119787 Expr *pX; /* The expression that defines the start bound */
119788 int r1, rTemp; /* Registers for holding the start boundary */
119789
119790 /* The following constant maps TK_xx codes into corresponding
119791 ** seek opcodes. It depends on a particular ordering of TK_xx
119792 */
119793 const u8 aMoveOp[] = {
119794 /* TK_GT */ OP_SeekGT,
119795 /* TK_LE */ OP_SeekLE,
119796 /* TK_LT */ OP_SeekLT,
119797 /* TK_GE */ OP_SeekGE
119798 };
119799 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
119800 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
119801 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
119802
119803 assert( (pStart->wtFlags & TERM_VNULL)==0 );
119804 testcase( pStart->wtFlags & TERM_VIRTUAL );
119805 pX = pStart->pExpr;
119806 assert( pX!=0 );
119807 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
119808 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
119809 sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
119810 VdbeComment((v, "pk"));
119811 VdbeCoverageIf(v, pX->op==TK_GT);
119812 VdbeCoverageIf(v, pX->op==TK_LE);
119813 VdbeCoverageIf(v, pX->op==TK_LT);
119814 VdbeCoverageIf(v, pX->op==TK_GE);
119815 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
119816 sqlite3ReleaseTempReg(pParse, rTemp);
119817 disableTerm(pLevel, pStart);
119818 }else{
119819 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
119820 VdbeCoverageIf(v, bRev==0);
119821 VdbeCoverageIf(v, bRev!=0);
119822 }
119823 if( pEnd ){
119824 Expr *pX;
119825 pX = pEnd->pExpr;
119826 assert( pX!=0 );
119827 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
119828 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
119829 testcase( pEnd->wtFlags & TERM_VIRTUAL );
119830 memEndValue = ++pParse->nMem;
119831 sqlite3ExprCode(pParse, pX->pRight, memEndValue);
119832 if( pX->op==TK_LT || pX->op==TK_GT ){
119833 testOp = bRev ? OP_Le : OP_Ge;
119834 }else{
119835 testOp = bRev ? OP_Lt : OP_Gt;
119836 }
119837 disableTerm(pLevel, pEnd);
119838 }
119839 start = sqlite3VdbeCurrentAddr(v);
119840 pLevel->op = bRev ? OP_Prev : OP_Next;
119841 pLevel->p1 = iCur;
119842 pLevel->p2 = start;
119843 assert( pLevel->p5==0 );
119844 if( testOp!=OP_Noop ){
119845 iRowidReg = ++pParse->nMem;
119846 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
119847 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
119848 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
119849 VdbeCoverageIf(v, testOp==OP_Le);
119850 VdbeCoverageIf(v, testOp==OP_Lt);
119851 VdbeCoverageIf(v, testOp==OP_Ge);
119852 VdbeCoverageIf(v, testOp==OP_Gt);
119853 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
119854 }
119855 }else if( pLoop->wsFlags & WHERE_INDEXED ){
119856 /* Case 4: A scan using an index.
119857 **
119858 ** The WHERE clause may contain zero or more equality
119859 ** terms ("==" or "IN" operators) that refer to the N
119860 ** left-most columns of the index. It may also contain
119861 ** inequality constraints (>, <, >= or <=) on the indexed
119862 ** column that immediately follows the N equalities. Only
119863 ** the right-most column can be an inequality - the rest must
119864 ** use the "==" and "IN" operators. For example, if the
119865 ** index is on (x,y,z), then the following clauses are all
119866 ** optimized:
119867 **
119868 ** x=5
119869 ** x=5 AND y=10
119870 ** x=5 AND y<10
119871 ** x=5 AND y>5 AND y<10
119872 ** x=5 AND y=5 AND z<=10
119873 **
119874 ** The z<10 term of the following cannot be used, only
119875 ** the x=5 term:
119876 **
119877 ** x=5 AND z<10
119878 **
119879 ** N may be zero if there are inequality constraints.
119880 ** If there are no inequality constraints, then N is at
119881 ** least one.
119882 **
119883 ** This case is also used when there are no WHERE clause
119884 ** constraints but an index is selected anyway, in order
119885 ** to force the output order to conform to an ORDER BY.
119886 */
119887 static const u8 aStartOp[] = {
119888 0,
119889 0,
119890 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
119891 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
119892 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
119893 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
119894 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
119895 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
119896 };
119897 static const u8 aEndOp[] = {
119898 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
119899 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
119900 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
119901 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
119902 };
119903 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
119904 int regBase; /* Base register holding constraint values */
119905 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
119906 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
119907 int startEq; /* True if range start uses ==, >= or <= */
119908 int endEq; /* True if range end uses ==, >= or <= */
119909 int start_constraints; /* Start of range is constrained */
119910 int nConstraint; /* Number of constraint terms */
119911 Index *pIdx; /* The index we will be using */
119912 int iIdxCur; /* The VDBE cursor for the index */
119913 int nExtraReg = 0; /* Number of extra registers needed */
119914 int op; /* Instruction opcode */
119915 char *zStartAff; /* Affinity for start of range constraint */
119916 char cEndAff = 0; /* Affinity for end of range constraint */
119917 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
119918 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
119919
119920 pIdx = pLoop->u.btree.pIndex;
119921 iIdxCur = pLevel->iIdxCur;
119922 assert( nEq>=pLoop->nSkip );
119923
119924 /* If this loop satisfies a sort order (pOrderBy) request that
119925 ** was passed to this function to implement a "SELECT min(x) ..."
119926 ** query, then the caller will only allow the loop to run for
119927 ** a single iteration. This means that the first row returned
119928 ** should not have a NULL value stored in 'x'. If column 'x' is
119929 ** the first one after the nEq equality constraints in the index,
119930 ** this requires some special handling.
119931 */
119932 assert( pWInfo->pOrderBy==0
119933 || pWInfo->pOrderBy->nExpr==1
119934 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
119935 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
119936 && pWInfo->nOBSat>0
119937 && (pIdx->nKeyCol>nEq)
119938 ){
119939 assert( pLoop->nSkip==0 );
119940 bSeekPastNull = 1;
119941 nExtraReg = 1;
119942 }
119943
119944 /* Find any inequality constraint terms for the start and end
119945 ** of the range.
119946 */
119947 j = nEq;
119948 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
119949 pRangeStart = pLoop->aLTerm[j++];
119950 nExtraReg = 1;
119951 /* Like optimization range constraints always occur in pairs */
119952 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
119953 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
119954 }
119955 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
119956 pRangeEnd = pLoop->aLTerm[j++];
119957 nExtraReg = 1;
119958 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
119959 assert( pRangeStart!=0 ); /* LIKE opt constraints */
119960 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
119961 pLevel->iLikeRepCntr = ++pParse->nMem;
119962 testcase( bRev );
119963 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
119964 sqlite3VdbeAddOp2(v, OP_Integer,
119965 bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
119966 pLevel->iLikeRepCntr);
119967 VdbeComment((v, "LIKE loop counter"));
119968 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
119969 }
119970 if( pRangeStart==0
119971 && (j = pIdx->aiColumn[nEq])>=0
119972 && pIdx->pTable->aCol[j].notNull==0
119973 ){
119974 bSeekPastNull = 1;
119975 }
119976 }
119977 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
119978
119979 /* Generate code to evaluate all constraint terms using == or IN
119980 ** and store the values of those terms in an array of registers
119981 ** starting at regBase.
119982 */
119983 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
119984 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
119985 if( zStartAff ) cEndAff = zStartAff[nEq];
119986 addrNxt = pLevel->addrNxt;
119987
119988 /* If we are doing a reverse order scan on an ascending index, or
119989 ** a forward order scan on a descending index, interchange the
119990 ** start and end terms (pRangeStart and pRangeEnd).
119991 */
119992 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
119993 || (bRev && pIdx->nKeyCol==nEq)
119994 ){
119995 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
119996 SWAP(u8, bSeekPastNull, bStopAtNull);
119997 }
119998
119999 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
120000 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
120001 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
120002 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
120003 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
120004 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
120005 start_constraints = pRangeStart || nEq>0;
120006
120007 /* Seek the index cursor to the start of the range. */
120008 nConstraint = nEq;
120009 if( pRangeStart ){
120010 Expr *pRight = pRangeStart->pExpr->pRight;
120011 sqlite3ExprCode(pParse, pRight, regBase+nEq);
120012 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
120013 if( (pRangeStart->wtFlags & TERM_VNULL)==0
120014 && sqlite3ExprCanBeNull(pRight)
120015 ){
120016 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
120017 VdbeCoverage(v);
120018 }
120019 if( zStartAff ){
120020 if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
120021 /* Since the comparison is to be performed with no conversions
120022 ** applied to the operands, set the affinity to apply to pRight to
120023 ** SQLITE_AFF_NONE. */
120024 zStartAff[nEq] = SQLITE_AFF_NONE;
120025 }
120026 if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
120027 zStartAff[nEq] = SQLITE_AFF_NONE;
120028 }
120029 }
120030 nConstraint++;
120031 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
120032 }else if( bSeekPastNull ){
120033 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
120034 nConstraint++;
120035 startEq = 0;
120036 start_constraints = 1;
120037 }
120038 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
120039 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
120040 assert( op!=0 );
120041 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
120042 VdbeCoverage(v);
120043 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
120044 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
120045 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
120046 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
120047 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
120048 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
120049
120050 /* Load the value for the inequality constraint at the end of the
120051 ** range (if any).
120052 */
120053 nConstraint = nEq;
120054 if( pRangeEnd ){
120055 Expr *pRight = pRangeEnd->pExpr->pRight;
120056 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
120057 sqlite3ExprCode(pParse, pRight, regBase+nEq);
120058 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
120059 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
120060 && sqlite3ExprCanBeNull(pRight)
120061 ){
120062 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
120063 VdbeCoverage(v);
120064 }
120065 if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
120066 && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
120067 ){
120068 codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
120069 }
120070 nConstraint++;
120071 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
120072 }else if( bStopAtNull ){
120073 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
120074 endEq = 0;
120075 nConstraint++;
120076 }
120077 sqlite3DbFree(db, zStartAff);
120078
120079 /* Top of the loop body */
120080 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
120081
120082 /* Check if the index cursor is past the end of the range. */
120083 if( nConstraint ){
120084 op = aEndOp[bRev*2 + endEq];
120085 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
120086 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
120087 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
120088 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
120089 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
120090 }
120091
120092 /* Seek the table cursor, if required */
120093 disableTerm(pLevel, pRangeStart);
120094 disableTerm(pLevel, pRangeEnd);
120095 if( omitTable ){
120096 /* pIdx is a covering index. No need to access the main table. */
120097 }else if( HasRowid(pIdx->pTable) ){
120098 iRowidReg = ++pParse->nMem;
120099 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
120100 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
120101 sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
120102 }else if( iCur!=iIdxCur ){
120103 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
120104 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
120105 for(j=0; j<pPk->nKeyCol; j++){
120106 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
120107 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
120108 }
120109 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
120110 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
120111 }
120112
120113 /* Record the instruction used to terminate the loop. Disable
120114 ** WHERE clause terms made redundant by the index range scan.
120115 */
120116 if( pLoop->wsFlags & WHERE_ONEROW ){
120117 pLevel->op = OP_Noop;
120118 }else if( bRev ){
120119 pLevel->op = OP_Prev;
120120 }else{
120121 pLevel->op = OP_Next;
120122 }
120123 pLevel->p1 = iIdxCur;
120124 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
120125 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
120126 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
120127 }else{
120128 assert( pLevel->p5==0 );
120129 }
120130 }else
120131
120132 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
120133 if( pLoop->wsFlags & WHERE_MULTI_OR ){
120134 /* Case 5: Two or more separately indexed terms connected by OR
120135 **
120136 ** Example:
120137 **
120138 ** CREATE TABLE t1(a,b,c,d);
120139 ** CREATE INDEX i1 ON t1(a);
120140 ** CREATE INDEX i2 ON t1(b);
120141 ** CREATE INDEX i3 ON t1(c);
120142 **
120143 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
120144 **
120145 ** In the example, there are three indexed terms connected by OR.
120146 ** The top of the loop looks like this:
120147 **
120148 ** Null 1 # Zero the rowset in reg 1
120149 **
120150 ** Then, for each indexed term, the following. The arguments to
120151 ** RowSetTest are such that the rowid of the current row is inserted
120152 ** into the RowSet. If it is already present, control skips the
120153 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
120154 **
120155 ** sqlite3WhereBegin(<term>)
120156 ** RowSetTest # Insert rowid into rowset
120157 ** Gosub 2 A
120158 ** sqlite3WhereEnd()
120159 **
120160 ** Following the above, code to terminate the loop. Label A, the target
120161 ** of the Gosub above, jumps to the instruction right after the Goto.
120162 **
120163 ** Null 1 # Zero the rowset in reg 1
120164 ** Goto B # The loop is finished.
120165 **
120166 ** A: <loop body> # Return data, whatever.
120167 **
120168 ** Return 2 # Jump back to the Gosub
120169 **
120170 ** B: <after the loop>
120171 **
120172 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
120173 ** use an ephemeral index instead of a RowSet to record the primary
120174 ** keys of the rows we have already seen.
120175 **
120176 */
120177 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
120178 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
120179 Index *pCov = 0; /* Potential covering index (or NULL) */
120180 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
120181
120182 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
120183 int regRowset = 0; /* Register for RowSet object */
120184 int regRowid = 0; /* Register holding rowid */
120185 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
120186 int iRetInit; /* Address of regReturn init */
120187 int untestedTerms = 0; /* Some terms not completely tested */
120188 int ii; /* Loop counter */
120189 u16 wctrlFlags; /* Flags for sub-WHERE clause */
120190 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
120191 Table *pTab = pTabItem->pTab;
120192
120193 pTerm = pLoop->aLTerm[0];
120194 assert( pTerm!=0 );
120195 assert( pTerm->eOperator & WO_OR );
120196 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
120197 pOrWc = &pTerm->u.pOrInfo->wc;
120198 pLevel->op = OP_Return;
120199 pLevel->p1 = regReturn;
120200
120201 /* Set up a new SrcList in pOrTab containing the table being scanned
120202 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
120203 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
120204 */
120205 if( pWInfo->nLevel>1 ){
120206 int nNotReady; /* The number of notReady tables */
120207 struct SrcList_item *origSrc; /* Original list of tables */
120208 nNotReady = pWInfo->nLevel - iLevel - 1;
120209 pOrTab = sqlite3StackAllocRaw(db,
120210 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
120211 if( pOrTab==0 ) return notReady;
120212 pOrTab->nAlloc = (u8)(nNotReady + 1);
120213 pOrTab->nSrc = pOrTab->nAlloc;
120214 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
120215 origSrc = pWInfo->pTabList->a;
120216 for(k=1; k<=nNotReady; k++){
120217 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
120218 }
120219 }else{
120220 pOrTab = pWInfo->pTabList;
120221 }
120222
120223 /* Initialize the rowset register to contain NULL. An SQL NULL is
120224 ** equivalent to an empty rowset. Or, create an ephemeral index
120225 ** capable of holding primary keys in the case of a WITHOUT ROWID.
120226 **
120227 ** Also initialize regReturn to contain the address of the instruction
120228 ** immediately following the OP_Return at the bottom of the loop. This
120229 ** is required in a few obscure LEFT JOIN cases where control jumps
120230 ** over the top of the loop into the body of it. In this case the
120231 ** correct response for the end-of-loop code (the OP_Return) is to
120232 ** fall through to the next instruction, just as an OP_Next does if
120233 ** called on an uninitialized cursor.
120234 */
120235 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
120236 if( HasRowid(pTab) ){
120237 regRowset = ++pParse->nMem;
120238 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
120239 }else{
120240 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120241 regRowset = pParse->nTab++;
120242 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
120243 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
120244 }
120245 regRowid = ++pParse->nMem;
120246 }
120247 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
120248
120249 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
120250 ** Then for every term xN, evaluate as the subexpression: xN AND z
120251 ** That way, terms in y that are factored into the disjunction will
120252 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
120253 **
120254 ** Actually, each subexpression is converted to "xN AND w" where w is
120255 ** the "interesting" terms of z - terms that did not originate in the
120256 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
120257 ** indices.
120258 **
120259 ** This optimization also only applies if the (x1 OR x2 OR ...) term
120260 ** is not contained in the ON clause of a LEFT JOIN.
120261 ** See ticket http://www.sqlite.org/src/info/f2369304e4
120262 */
120263 if( pWC->nTerm>1 ){
120264 int iTerm;
120265 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
120266 Expr *pExpr = pWC->a[iTerm].pExpr;
120267 if( &pWC->a[iTerm] == pTerm ) continue;
120268 if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
120269 if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
120270 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
120271 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
120272 pExpr = sqlite3ExprDup(db, pExpr, 0);
120273 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
120274 }
120275 if( pAndExpr ){
120276 pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
120277 }
120278 }
120279
120280 /* Run a separate WHERE clause for each term of the OR clause. After
120281 ** eliminating duplicates from other WHERE clauses, the action for each
120282 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
120283 */
120284 wctrlFlags = WHERE_OMIT_OPEN_CLOSE
120285 | WHERE_FORCE_TABLE
120286 | WHERE_ONETABLE_ONLY
120287 | WHERE_NO_AUTOINDEX;
120288 for(ii=0; ii<pOrWc->nTerm; ii++){
120289 WhereTerm *pOrTerm = &pOrWc->a[ii];
120290 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
120291 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
120292 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
120293 int j1 = 0; /* Address of jump operation */
120294 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
120295 pAndExpr->pLeft = pOrExpr;
120296 pOrExpr = pAndExpr;
120297 }
120298 /* Loop through table entries that match term pOrTerm. */
120299 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
120300 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
120301 wctrlFlags, iCovCur);
120302 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
120303 if( pSubWInfo ){
120304 WhereLoop *pSubLoop;
120305 int addrExplain = explainOneScan(
120306 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
120307 );
120308 addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
120309
120310 /* This is the sub-WHERE clause body. First skip over
120311 ** duplicate rows from prior sub-WHERE clauses, and record the
120312 ** rowid (or PRIMARY KEY) for the current row so that the same
120313 ** row will be skipped in subsequent sub-WHERE clauses.
120314 */
120315 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
120316 int r;
120317 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
120318 if( HasRowid(pTab) ){
120319 r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
120320 j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
120321 VdbeCoverage(v);
120322 }else{
120323 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120324 int nPk = pPk->nKeyCol;
120325 int iPk;
120326
120327 /* Read the PK into an array of temp registers. */
120328 r = sqlite3GetTempRange(pParse, nPk);
120329 for(iPk=0; iPk<nPk; iPk++){
120330 int iCol = pPk->aiColumn[iPk];
120331 sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
120332 }
120333
120334 /* Check if the temp table already contains this key. If so,
120335 ** the row has already been included in the result set and
120336 ** can be ignored (by jumping past the Gosub below). Otherwise,
120337 ** insert the key into the temp table and proceed with processing
120338 ** the row.
120339 **
120340 ** Use some of the same optimizations as OP_RowSetTest: If iSet
120341 ** is zero, assume that the key cannot already be present in
120342 ** the temp table. And if iSet is -1, assume that there is no
120343 ** need to insert the key into the temp table, as it will never
120344 ** be tested for. */
120345 if( iSet ){
120346 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
120347 VdbeCoverage(v);
120348 }
120349 if( iSet>=0 ){
120350 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
120351 sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
120352 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
120353 }
120354
120355 /* Release the array of temp registers */
120356 sqlite3ReleaseTempRange(pParse, r, nPk);
120357 }
120358 }
120359
120360 /* Invoke the main loop body as a subroutine */
120361 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
120362
120363 /* Jump here (skipping the main loop body subroutine) if the
120364 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
120365 if( j1 ) sqlite3VdbeJumpHere(v, j1);
120366
120367 /* The pSubWInfo->untestedTerms flag means that this OR term
120368 ** contained one or more AND term from a notReady table. The
120369 ** terms from the notReady table could not be tested and will
120370 ** need to be tested later.
120371 */
120372 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
120373
120374 /* If all of the OR-connected terms are optimized using the same
120375 ** index, and the index is opened using the same cursor number
120376 ** by each call to sqlite3WhereBegin() made by this loop, it may
120377 ** be possible to use that index as a covering index.
120378 **
120379 ** If the call to sqlite3WhereBegin() above resulted in a scan that
120380 ** uses an index, and this is either the first OR-connected term
120381 ** processed or the index is the same as that used by all previous
120382 ** terms, set pCov to the candidate covering index. Otherwise, set
120383 ** pCov to NULL to indicate that no candidate covering index will
120384 ** be available.
120385 */
120386 pSubLoop = pSubWInfo->a[0].pWLoop;
120387 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
120388 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
120389 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
120390 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
120391 ){
120392 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
120393 pCov = pSubLoop->u.btree.pIndex;
120394 wctrlFlags |= WHERE_REOPEN_IDX;
120395 }else{
120396 pCov = 0;
120397 }
120398
120399 /* Finish the loop through table entries that match term pOrTerm. */
120400 sqlite3WhereEnd(pSubWInfo);
120401 }
120402 }
120403 }
120404 pLevel->u.pCovidx = pCov;
120405 if( pCov ) pLevel->iIdxCur = iCovCur;
120406 if( pAndExpr ){
120407 pAndExpr->pLeft = 0;
120408 sqlite3ExprDelete(db, pAndExpr);
120409 }
120410 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
120411 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
120412 sqlite3VdbeResolveLabel(v, iLoopBody);
120413
120414 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
120415 if( !untestedTerms ) disableTerm(pLevel, pTerm);
120416 }else
120417 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
120418
120419 {
120420 /* Case 6: There is no usable index. We must do a complete
120421 ** scan of the entire table.
120422 */
120423 static const u8 aStep[] = { OP_Next, OP_Prev };
120424 static const u8 aStart[] = { OP_Rewind, OP_Last };
120425 assert( bRev==0 || bRev==1 );
120426 if( pTabItem->isRecursive ){
120427 /* Tables marked isRecursive have only a single row that is stored in
120428 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
120429 pLevel->op = OP_Noop;
120430 }else{
120431 pLevel->op = aStep[bRev];
120432 pLevel->p1 = iCur;
120433 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
120434 VdbeCoverageIf(v, bRev==0);
120435 VdbeCoverageIf(v, bRev!=0);
120436 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
120437 }
120438 }
120439
120440 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
120441 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
120442 #endif
120443
120444 /* Insert code to test every subexpression that can be completely
120445 ** computed using the current set of tables.
120446 */
120447 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
120448 Expr *pE;
120449 int skipLikeAddr = 0;
120450 testcase( pTerm->wtFlags & TERM_VIRTUAL );
120451 testcase( pTerm->wtFlags & TERM_CODED );
120452 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120453 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
120454 testcase( pWInfo->untestedTerms==0
120455 && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
120456 pWInfo->untestedTerms = 1;
120457 continue;
120458 }
120459 pE = pTerm->pExpr;
120460 assert( pE!=0 );
120461 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
120462 continue;
120463 }
120464 if( pTerm->wtFlags & TERM_LIKECOND ){
120465 assert( pLevel->iLikeRepCntr>0 );
120466 skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
120467 VdbeCoverage(v);
120468 }
120469 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
120470 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
120471 pTerm->wtFlags |= TERM_CODED;
120472 }
120473
120474 /* Insert code to test for implied constraints based on transitivity
120475 ** of the "==" operator.
120476 **
120477 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
120478 ** and we are coding the t1 loop and the t2 loop has not yet coded,
120479 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
120480 ** the implied "t1.a=123" constraint.
120481 */
120482 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
120483 Expr *pE, *pEAlt;
120484 WhereTerm *pAlt;
120485 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120486 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
120487 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
120488 if( pTerm->leftCursor!=iCur ) continue;
120489 if( pLevel->iLeftJoin ) continue;
120490 pE = pTerm->pExpr;
120491 assert( !ExprHasProperty(pE, EP_FromJoin) );
120492 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
120493 pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
120494 WO_EQ|WO_IN|WO_IS, 0);
120495 if( pAlt==0 ) continue;
120496 if( pAlt->wtFlags & (TERM_CODED) ) continue;
120497 testcase( pAlt->eOperator & WO_EQ );
120498 testcase( pAlt->eOperator & WO_IS );
120499 testcase( pAlt->eOperator & WO_IN );
120500 VdbeModuleComment((v, "begin transitive constraint"));
120501 pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
120502 if( pEAlt ){
120503 *pEAlt = *pAlt->pExpr;
120504 pEAlt->pLeft = pE->pLeft;
120505 sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
120506 sqlite3StackFree(db, pEAlt);
120507 }
120508 }
120509
120510 /* For a LEFT OUTER JOIN, generate code that will record the fact that
120511 ** at least one row of the right table has matched the left table.
120512 */
120513 if( pLevel->iLeftJoin ){
120514 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
120515 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
120516 VdbeComment((v, "record LEFT JOIN hit"));
120517 sqlite3ExprCacheClear(pParse);
120518 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
120519 testcase( pTerm->wtFlags & TERM_VIRTUAL );
120520 testcase( pTerm->wtFlags & TERM_CODED );
120521 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
120522 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
120523 assert( pWInfo->untestedTerms );
120524 continue;
120525 }
120526 assert( pTerm->pExpr );
120527 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
120528 pTerm->wtFlags |= TERM_CODED;
120529 }
120530 }
120531
120532 return pLevel->notReady;
120533 }
120534
120535 #ifdef WHERETRACE_ENABLED
120536 /*
120537 ** Print the content of a WhereTerm object
120538 */
@@ -120695,11 +121514,11 @@
120695 WhereLevel *pLevel = &pWInfo->a[i];
120696 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
120697 sqlite3DbFree(db, pLevel->u.in.aInLoop);
120698 }
120699 }
120700 whereClauseClear(&pWInfo->sWC);
120701 while( pWInfo->pLoops ){
120702 WhereLoop *p = pWInfo->pLoops;
120703 pWInfo->pLoops = p->pNextLoop;
120704 whereLoopDelete(db, p);
120705 }
@@ -121647,14 +122466,36 @@
121647
121648 #ifndef SQLITE_OMIT_VIRTUALTABLE
121649 /*
121650 ** Add all WhereLoop objects for a table of the join identified by
121651 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121652 */
121653 static int whereLoopAddVirtual(
121654 WhereLoopBuilder *pBuilder, /* WHERE clause information */
121655 Bitmask mExtra
 
121656 ){
121657 WhereInfo *pWInfo; /* WHERE analysis context */
121658 Parse *pParse; /* The parsing context */
121659 WhereClause *pWC; /* The WHERE clause */
121660 struct SrcList_item *pSrc; /* The FROM clause term to search */
@@ -121671,19 +122512,20 @@
121671 int seenVar = 0; /* True if a non-constant constraint is seen */
121672 int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */
121673 WhereLoop *pNew;
121674 int rc = SQLITE_OK;
121675
 
121676 pWInfo = pBuilder->pWInfo;
121677 pParse = pWInfo->pParse;
121678 db = pParse->db;
121679 pWC = pBuilder->pWC;
121680 pNew = pBuilder->pNew;
121681 pSrc = &pWInfo->pTabList->a[pNew->iTab];
121682 pTab = pSrc->pTab;
121683 assert( IsVirtual(pTab) );
121684 pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
121685 if( pIdxInfo==0 ) return SQLITE_NOMEM;
121686 pNew->prereq = 0;
121687 pNew->rSetup = 0;
121688 pNew->wsFlags = WHERE_VIRTUALTABLE;
121689 pNew->nLTerm = 0;
@@ -121709,19 +122551,19 @@
121709 case 0: /* Constants without IN operator */
121710 pIdxCons->usable = 0;
121711 if( (pTerm->eOperator & WO_IN)!=0 ){
121712 seenIn = 1;
121713 }
121714 if( pTerm->prereqRight!=0 ){
121715 seenVar = 1;
121716 }else if( (pTerm->eOperator & WO_IN)==0 ){
121717 pIdxCons->usable = 1;
121718 }
121719 break;
121720 case 1: /* Constants with IN operators */
121721 assert( seenIn );
121722 pIdxCons->usable = (pTerm->prereqRight==0);
121723 break;
121724 case 2: /* Variables without IN */
121725 assert( seenVar );
121726 pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
121727 break;
@@ -121816,11 +122658,15 @@
121816
121817 /*
121818 ** Add WhereLoop entries to handle OR terms. This works for either
121819 ** btrees or virtual tables.
121820 */
121821 static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
 
 
 
 
121822 WhereInfo *pWInfo = pBuilder->pWInfo;
121823 WhereClause *pWC;
121824 WhereLoop *pNew;
121825 WhereTerm *pTerm, *pWCEnd;
121826 int rc = SQLITE_OK;
@@ -121875,18 +122721,18 @@
121875 }
121876 }
121877 #endif
121878 #ifndef SQLITE_OMIT_VIRTUALTABLE
121879 if( IsVirtual(pItem->pTab) ){
121880 rc = whereLoopAddVirtual(&sSubBuild, mExtra);
121881 }else
121882 #endif
121883 {
121884 rc = whereLoopAddBtree(&sSubBuild, mExtra);
121885 }
121886 if( rc==SQLITE_OK ){
121887 rc = whereLoopAddOr(&sSubBuild, mExtra);
121888 }
121889 assert( rc==SQLITE_OK || sCur.n==0 );
121890 if( sCur.n==0 ){
121891 sSum.n = 0;
121892 break;
@@ -121944,37 +122790,47 @@
121944 Bitmask mExtra = 0;
121945 Bitmask mPrior = 0;
121946 int iTab;
121947 SrcList *pTabList = pWInfo->pTabList;
121948 struct SrcList_item *pItem;
 
121949 sqlite3 *db = pWInfo->pParse->db;
121950 int nTabList = pWInfo->nLevel;
121951 int rc = SQLITE_OK;
121952 u8 priorJoinType = 0;
121953 WhereLoop *pNew;
 
121954
121955 /* Loop over the tables in the join, from left to right */
121956 pNew = pBuilder->pNew;
121957 whereLoopInit(pNew);
121958 for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
 
121959 pNew->iTab = iTab;
121960 pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
121961 if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
 
 
121962 mExtra = mPrior;
121963 }
121964 priorJoinType = pItem->jointype;
121965 if( IsVirtual(pItem->pTab) ){
121966 rc = whereLoopAddVirtual(pBuilder, mExtra);
 
 
 
 
 
 
121967 }else{
121968 rc = whereLoopAddBtree(pBuilder, mExtra);
121969 }
121970 if( rc==SQLITE_OK ){
121971 rc = whereLoopAddOr(pBuilder, mExtra);
121972 }
121973 mPrior |= pNew->maskSelf;
121974 if( rc || db->mallocFailed ) break;
121975 }
 
121976 whereLoopClear(db, pNew);
121977 return rc;
121978 }
121979
121980 /*
@@ -122076,11 +122932,11 @@
122076 for(i=0; i<nOrderBy; i++){
122077 if( MASKBIT(i) & obSat ) continue;
122078 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
122079 if( pOBExpr->op!=TK_COLUMN ) continue;
122080 if( pOBExpr->iTable!=iCur ) continue;
122081 pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
122082 ~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
122083 if( pTerm==0 ) continue;
122084 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
122085 const char *z1, *z2;
122086 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
@@ -122213,11 +123069,11 @@
122213 for(i=0; i<nOrderBy; i++){
122214 Expr *p;
122215 Bitmask mTerm;
122216 if( MASKBIT(i) & obSat ) continue;
122217 p = pOrderBy->a[i].pExpr;
122218 mTerm = exprTableUsage(&pWInfo->sMaskSet,p);
122219 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
122220 if( (mTerm&~orderDistinctMask)==0 ){
122221 obSat |= MASKBIT(i);
122222 }
122223 }
@@ -122686,17 +123542,17 @@
122686 if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
122687 assert( pWInfo->pTabList->nSrc>=1 );
122688 pItem = pWInfo->pTabList->a;
122689 pTab = pItem->pTab;
122690 if( IsVirtual(pTab) ) return 0;
122691 if( pItem->zIndex ) return 0;
122692 iCur = pItem->iCursor;
122693 pWC = &pWInfo->sWC;
122694 pLoop = pBuilder->pNew;
122695 pLoop->wsFlags = 0;
122696 pLoop->nSkip = 0;
122697 pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
122698 if( pTerm ){
122699 testcase( pTerm->eOperator & WO_IS );
122700 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
122701 pLoop->aLTerm[0] = pTerm;
122702 pLoop->nLTerm = 1;
@@ -122711,11 +123567,11 @@
122711 || pIdx->pPartIdxWhere!=0
122712 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
122713 ) continue;
122714 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
122715 for(j=0; j<pIdx->nKeyCol; j++){
122716 pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
122717 if( pTerm==0 ) break;
122718 testcase( pTerm->eOperator & WO_IS );
122719 pLoop->aLTerm[j] = pTerm;
122720 }
122721 if( j!=pIdx->nKeyCol ) continue;
@@ -122732,11 +123588,11 @@
122732 }
122733 }
122734 if( pLoop->wsFlags ){
122735 pLoop->nOut = (LogEst)1;
122736 pWInfo->a[0].pWLoop = pLoop;
122737 pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
122738 pWInfo->a[0].iTabCur = iCur;
122739 pWInfo->nRowOut = 1;
122740 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
122741 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
122742 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
@@ -122926,12 +123782,12 @@
122926
122927 /* Split the WHERE clause into separate subexpressions where each
122928 ** subexpression is separated by an AND operator.
122929 */
122930 initMaskSet(pMaskSet);
122931 whereClauseInit(&pWInfo->sWC, pWInfo);
122932 whereSplit(&pWInfo->sWC, pWhere, TK_AND);
122933
122934 /* Special case: a WHERE clause that is constant. Evaluate the
122935 ** expression and either jump over all of the code or fall thru.
122936 */
122937 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
@@ -122972,26 +123828,20 @@
122972 }
122973 #ifndef NDEBUG
122974 {
122975 Bitmask toTheLeft = 0;
122976 for(ii=0; ii<pTabList->nSrc; ii++){
122977 Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
122978 assert( (m-1)==toTheLeft );
122979 toTheLeft |= m;
122980 }
122981 }
122982 #endif
122983
122984 /* Analyze all of the subexpressions. Note that exprAnalyze() might
122985 ** add new virtual terms onto the end of the WHERE clause. We do not
122986 ** want to analyze these virtual terms, so start analyzing at the end
122987 ** and work forward so that the added virtual terms are never processed.
122988 */
122989 exprAnalyzeAll(pTabList, &pWInfo->sWC);
122990 if( db->mallocFailed ){
122991 goto whereBeginError;
122992 }
122993
122994 if( wctrlFlags & WHERE_WANT_DISTINCT ){
122995 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
122996 /* The DISTINCT marking is pointless. Ignore it. */
122997 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
@@ -123003,12 +123853,11 @@
123003 }
123004
123005 /* Construct the WhereLoop objects */
123006 WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
123007 #if defined(WHERETRACE_ENABLED)
123008 /* Display all terms of the WHERE clause */
123009 if( sqlite3WhereTrace & 0x100 ){
123010 int i;
123011 for(i=0; i<sWLB.pWC->nTerm; i++){
123012 whereTermPrint(&sWLB.pWC->a[i], i);
123013 }
123014 }
@@ -123016,17 +123865,16 @@
123016
123017 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
123018 rc = whereLoopAddAll(&sWLB);
123019 if( rc ) goto whereBeginError;
123020
123021 /* Display all of the WhereLoop objects if wheretrace is enabled */
123022 #ifdef WHERETRACE_ENABLED /* !=0 */
123023 if( sqlite3WhereTrace ){
123024 WhereLoop *p;
123025 int i;
123026 static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
123027 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
123028 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
123029 p->cId = zLabel[i%sizeof(zLabel)];
123030 whereLoopPrint(p, sWLB.pWC);
123031 }
123032 }
@@ -123043,11 +123891,11 @@
123043 pWInfo->revMask = (Bitmask)(-1);
123044 }
123045 if( pParse->nErr || NEVER(db->mallocFailed) ){
123046 goto whereBeginError;
123047 }
123048 #ifdef WHERETRACE_ENABLED /* !=0 */
123049 if( sqlite3WhereTrace ){
123050 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
123051 if( pWInfo->nOBSat>0 ){
123052 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
123053 }
@@ -123074,12 +123922,14 @@
123074 /* Attempt to omit tables from the join that do not effect the result */
123075 if( pWInfo->nLevel>=2
123076 && pResultSet!=0
123077 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
123078 ){
123079 Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
123080 if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
 
 
123081 while( pWInfo->nLevel>=2 ){
123082 WhereTerm *pTerm, *pEnd;
123083 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
123084 if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
123085 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
@@ -123106,11 +123956,11 @@
123106 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
123107
123108 /* If the caller is an UPDATE or DELETE statement that is requesting
123109 ** to use a one-pass algorithm, determine if this is appropriate.
123110 ** The one-pass algorithm only works if the WHERE clause constrains
123111 ** the statement to update a single row.
123112 */
123113 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
123114 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
123115 && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
123116 pWInfo->okOnePass = 1;
@@ -123120,11 +123970,10 @@
123120 }
123121
123122 /* Open all tables in the pTabList and any indices selected for
123123 ** searching those tables.
123124 */
123125 notReady = ~(Bitmask)0;
123126 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
123127 Table *pTab; /* Table to open */
123128 int iDb; /* Index of database containing table/index */
123129 struct SrcList_item *pTabItem;
123130
@@ -123161,10 +124010,14 @@
123161 for(; b; b=b>>1, n++){}
123162 sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
123163 SQLITE_INT_TO_PTR(n), P4_INT32);
123164 assert( n<=pTab->nCol );
123165 }
 
 
 
 
123166 }else{
123167 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
123168 }
123169 if( pLoop->wsFlags & WHERE_INDEXED ){
123170 Index *pIx = pLoop->u.btree.pIndex;
@@ -123206,14 +124059,28 @@
123206 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
123207 ){
123208 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
123209 }
123210 VdbeComment((v, "%s", pIx->zName));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123211 }
123212 }
123213 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
123214 notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
123215 }
123216 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
123217 if( db->mallocFailed ) goto whereBeginError;
123218
123219 /* Generate the code to do the search. Each iteration of the for
@@ -123231,18 +124098,18 @@
123231 constructAutomaticIndex(pParse, &pWInfo->sWC,
123232 &pTabList->a[pLevel->iFrom], notReady, pLevel);
123233 if( db->mallocFailed ) goto whereBeginError;
123234 }
123235 #endif
123236 addrExplain = explainOneScan(
123237 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
123238 );
123239 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
123240 notReady = codeOneLoopStart(pWInfo, ii, notReady);
123241 pWInfo->iContinue = pLevel->addrCont;
123242 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
123243 addScanStatus(v, pTabList, pLevel, addrExplain);
123244 }
123245 }
123246
123247 /* Done. */
123248 VdbeModuleComment((v, "Begin WHERE-core"));
@@ -124924,11 +125791,11 @@
124924 YYCODETYPE yymajor;
124925 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
124926
124927 /* There is no mechanism by which the parser stack can be popped below
124928 ** empty in SQLite. */
124929 if( NEVER(pParser->yyidx<0) ) return 0;
124930 #ifndef NDEBUG
124931 if( yyTraceFILE && pParser->yyidx>=0 ){
124932 fprintf(yyTraceFILE,"%sPopping %s\n",
124933 yyTracePrompt,
124934 yyTokenName[yytos->major]);
@@ -127396,11 +128263,15 @@
127396 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
127397 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
127398 };
127399 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
127400 #endif
 
 
 
127401 SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
 
127402
127403
127404 /*
127405 ** Return the length of the token that begins at z[0].
127406 ** Store the token type in *tokenType before returning.
@@ -128104,11 +128975,11 @@
128104 rc = sqlite3_complete(zSql8);
128105 }else{
128106 rc = SQLITE_NOMEM;
128107 }
128108 sqlite3ValueFree(pVal);
128109 return sqlite3ApiExit(0, rc);
128110 }
128111 #endif /* SQLITE_OMIT_UTF16 */
128112 #endif /* SQLITE_OMIT_COMPLETE */
128113
128114 /************** End of complete.c ********************************************/
@@ -130282,13 +131153,15 @@
130282 #endif
130283 #if SQLITE_TEMP_STORE==2
130284 return ( db->temp_store!=1 );
130285 #endif
130286 #if SQLITE_TEMP_STORE==3
 
130287 return 1;
130288 #endif
130289 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
 
130290 return 0;
130291 #endif
130292 }
130293
130294 /*
@@ -131126,11 +131999,11 @@
131126 /* Opening a db handle. Fourth parameter is passed 0. */
131127 void *pArg = sqlite3GlobalConfig.pSqllogArg;
131128 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
131129 }
131130 #endif
131131 return sqlite3ApiExit(0, rc);
131132 }
131133
131134 /*
131135 ** Open a new database handle.
131136 */
@@ -131184,11 +132057,11 @@
131184 }else{
131185 rc = SQLITE_NOMEM;
131186 }
131187 sqlite3ValueFree(pVal);
131188
131189 return sqlite3ApiExit(0, rc);
131190 }
131191 #endif /* SQLITE_OMIT_UTF16 */
131192
131193 /*
131194 ** Register a new collation sequence with the database handle db.
@@ -131556,11 +132429,13 @@
131556 /*
131557 ** Interface to the testing logic.
131558 */
131559 SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
131560 int rc = 0;
131561 #ifndef SQLITE_OMIT_BUILTIN_TEST
 
 
131562 va_list ap;
131563 va_start(ap, op);
131564 switch( op ){
131565
131566 /*
@@ -154904,11 +155779,10 @@
154904 while( zPattern[iPattern]!=0 ){
154905
154906 /* Read (and consume) the next character from the input pattern. */
154907 UChar32 uPattern;
154908 U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
154909 assert(uPattern!=0);
154910
154911 /* There are now 4 possibilities:
154912 **
154913 ** 1. uPattern is an unescaped match-all character "%",
154914 ** 2. uPattern is an unescaped match-one character "_",
@@ -155243,10 +156117,11 @@
155243 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
155244 UCollator *pUCollator; /* ICU library collation object */
155245 int rc; /* Return code from sqlite3_create_collation_x() */
155246
155247 assert(nArg==2);
 
155248 zLocale = (const char *)sqlite3_value_text(apArg[0]);
155249 zName = (const char *)sqlite3_value_text(apArg[1]);
155250
155251 if( !zLocale || !zName ){
155252 return;
@@ -155566,16 +156441,17 @@
155566
155567 /*
155568 ** The set of routines that implement the simple tokenizer
155569 */
155570 static const sqlite3_tokenizer_module icuTokenizerModule = {
155571 0, /* iVersion */
155572 icuCreate, /* xCreate */
155573 icuDestroy, /* xCreate */
155574 icuOpen, /* xOpen */
155575 icuClose, /* xClose */
155576 icuNext, /* xNext */
 
155577 };
155578
155579 /*
155580 ** Set *ppModule to point at the implementation of the ICU tokenizer.
155581 */
@@ -159131,12 +160007,12 @@
159131 static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
159132 ota_file *p = (ota_file *)pFile;
159133 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
159134 int rc;
159135
159136 assert( p->openFlags &
159137 (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB|SQLITE_OPEN_TRANSIENT_DB)
159138 );
159139 if( op==SQLITE_FCNTL_OTA ){
159140 sqlite3ota *pOta = (sqlite3ota*)pArg;
159141
159142 /* First try to find another OTA vfs lower down in the vfs stack. If
159143
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -155,10 +155,17 @@
155 # ifndef _FILE_OFFSET_BITS
156 # define _FILE_OFFSET_BITS 64
157 # endif
158 # define _LARGEFILE_SOURCE 1
159 #endif
160
161 /* What version of GCC is being used. 0 means GCC is not being used */
162 #ifdef __GNUC__
163 # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
164 #else
165 # define GCC_VERSION 0
166 #endif
167
168 /* Needed for various definitions... */
169 #if defined(__GNUC__) && !defined(_GNU_SOURCE)
170 # define _GNU_SOURCE
171 #endif
@@ -228,11 +235,11 @@
235 ** to experimental interfaces but reserve the right to make minor changes
236 ** if experience from use "in the wild" suggest such changes are prudent.
237 **
238 ** The official C-language API documentation for SQLite is derived
239 ** from comments in this file. This file is the authoritative source
240 ** on how SQLite interfaces are supposed to operate.
241 **
242 ** The name of this file under configuration management is "sqlite.h.in".
243 ** The makefile makes some minor changes to this file (such as inserting
244 ** the version number) and changes its name to "sqlite3.h" as
245 ** part of the build process.
@@ -318,11 +325,11 @@
325 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326 ** [sqlite_version()] and [sqlite_source_id()].
327 */
328 #define SQLITE_VERSION "3.8.11"
329 #define SQLITE_VERSION_NUMBER 3008011
330 #define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899"
331
332 /*
333 ** CAPI3REF: Run-Time Library Version Numbers
334 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
335 **
@@ -1161,11 +1168,11 @@
1168 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1169 ** opcode causes the xFileControl method to swap the file handle with the one
1170 ** pointed to by the pArg argument. This capability is used during testing
1171 ** and only needs to be supported when SQLITE_TEST is defined.
1172 **
1173 ** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1174 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1175 ** be advantageous to block on the next WAL lock if the lock is not immediately
1176 ** available. The WAL subsystem issues this signal during rare
1177 ** circumstances in order to fix a problem with priority inversion.
1178 ** Applications should <em>not</em> use this file-control.
@@ -9689,17 +9696,18 @@
9696 u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */
9697 u8 p5; /* Fifth parameter is an unsigned character */
9698 int p1; /* First operand */
9699 int p2; /* Second parameter (often the jump destination) */
9700 int p3; /* The third parameter */
9701 union p4union { /* fourth parameter */
9702 int i; /* Integer value if p4type==P4_INT32 */
9703 void *p; /* Generic pointer */
9704 char *z; /* Pointer to data for string (char array) types */
9705 i64 *pI64; /* Used when p4type is P4_INT64 */
9706 double *pReal; /* Used when p4type is P4_REAL */
9707 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
9708 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
9709 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
9710 Mem *pMem; /* Used when p4type is P4_MEM */
9711 VTable *pVtab; /* Used when p4type is P4_VTAB */
9712 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
9713 int *ai; /* Used when p4type is P4_INTARRAY */
@@ -9762,10 +9770,11 @@
9770 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
9771 #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
9772 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
9773 #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
9774 #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
9775 #define P4_FUNCCTX (-20) /* P4 is a pointer to an sqlite3_context object */
9776
9777 /* Error message codes for OP_Halt */
9778 #define P5_ConstraintNotNull 1
9779 #define P5_ConstraintUnique 2
9780 #define P5_ConstraintCheck 3
@@ -9804,46 +9813,46 @@
9813 */
9814 /************** Include opcodes.h in the middle of vdbe.h ********************/
9815 /************** Begin file opcodes.h *****************************************/
9816 /* Automatically generated. Do not edit */
9817 /* See the mkopcodeh.awk script for details */
9818 #define OP_Savepoint 1
9819 #define OP_AutoCommit 2
9820 #define OP_Transaction 3
9821 #define OP_SorterNext 4
9822 #define OP_PrevIfOpen 5
9823 #define OP_NextIfOpen 6
9824 #define OP_Prev 7
9825 #define OP_Next 8
9826 #define OP_Checkpoint 9
9827 #define OP_JournalMode 10
9828 #define OP_Vacuum 11
9829 #define OP_VFilter 12 /* synopsis: iplan=r[P3] zplan='P4' */
9830 #define OP_VUpdate 13 /* synopsis: data=r[P3@P2] */
9831 #define OP_Goto 14
9832 #define OP_Gosub 15
9833 #define OP_Return 16
9834 #define OP_InitCoroutine 17
9835 #define OP_EndCoroutine 18
9836 #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
9837 #define OP_Yield 20
9838 #define OP_HaltIfNull 21 /* synopsis: if r[P3]=null halt */
9839 #define OP_Halt 22
9840 #define OP_Integer 23 /* synopsis: r[P2]=P1 */
9841 #define OP_Int64 24 /* synopsis: r[P2]=P4 */
9842 #define OP_String 25 /* synopsis: r[P2]='P4' (len=P1) */
9843 #define OP_Null 26 /* synopsis: r[P2..P3]=NULL */
9844 #define OP_SoftNull 27 /* synopsis: r[P1]=NULL */
9845 #define OP_Blob 28 /* synopsis: r[P2]=P4 (len=P1) */
9846 #define OP_Variable 29 /* synopsis: r[P2]=parameter(P1,P4) */
9847 #define OP_Move 30 /* synopsis: r[P2@P3]=r[P1@P3] */
9848 #define OP_Copy 31 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
9849 #define OP_SCopy 32 /* synopsis: r[P2]=r[P1] */
9850 #define OP_ResultRow 33 /* synopsis: output=r[P1@P2] */
9851 #define OP_CollSeq 34
9852 #define OP_Function0 35 /* synopsis: r[P3]=func(r[P2@P5]) */
9853 #define OP_Function 36 /* synopsis: r[P3]=func(r[P2@P5]) */
9854 #define OP_AddImm 37 /* synopsis: r[P1]=r[P1]+P2 */
9855 #define OP_MustBeInt 38
9856 #define OP_RealAffinity 39
9857 #define OP_Cast 40 /* synopsis: affinity(r[P1]) */
9858 #define OP_Permutation 41
@@ -9865,33 +9874,33 @@
9874 #define OP_OpenEphemeral 57 /* synopsis: nColumn=P2 */
9875 #define OP_SorterOpen 58
9876 #define OP_SequenceTest 59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
9877 #define OP_OpenPseudo 60 /* synopsis: P3 columns in r[P2] */
9878 #define OP_Close 61
9879 #define OP_ColumnsUsed 62
9880 #define OP_SeekLT 63 /* synopsis: key=r[P3@P4] */
9881 #define OP_SeekLE 64 /* synopsis: key=r[P3@P4] */
9882 #define OP_SeekGE 65 /* synopsis: key=r[P3@P4] */
9883 #define OP_SeekGT 66 /* synopsis: key=r[P3@P4] */
9884 #define OP_Seek 67 /* synopsis: intkey=r[P2] */
9885 #define OP_NoConflict 68 /* synopsis: key=r[P3@P4] */
9886 #define OP_NotFound 69 /* synopsis: key=r[P3@P4] */
9887 #define OP_Found 70 /* synopsis: key=r[P3@P4] */
9888 #define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
9889 #define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9890 #define OP_NotExists 73 /* synopsis: intkey=r[P3] */
9891 #define OP_Sequence 74 /* synopsis: r[P2]=cursor[P1].ctr++ */
9892 #define OP_NewRowid 75 /* synopsis: r[P2]=rowid */
9893 #define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
9894 #define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9895 #define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9896 #define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9897 #define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9898 #define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9899 #define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
9900 #define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9901 #define OP_Insert 84 /* synopsis: intkey=r[P3] data=r[P2] */
9902 #define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9903 #define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9904 #define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9905 #define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9906 #define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -9898,73 +9907,76 @@
9907 #define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9908 #define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9909 #define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9910 #define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
9911 #define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9912 #define OP_InsertInt 95 /* synopsis: intkey=P3 data=r[P2] */
9913 #define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9914 #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
9915 #define OP_Delete 98
9916 #define OP_ResetCount 99
9917 #define OP_SorterCompare 100 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
9918 #define OP_SorterData 101 /* synopsis: r[P2]=data */
9919 #define OP_RowKey 102 /* synopsis: r[P2]=key */
9920 #define OP_RowData 103 /* synopsis: r[P2]=data */
9921 #define OP_Rowid 104 /* synopsis: r[P2]=rowid */
9922 #define OP_NullRow 105
9923 #define OP_Last 106
9924 #define OP_SorterSort 107
9925 #define OP_Sort 108
9926 #define OP_Rewind 109
9927 #define OP_SorterInsert 110
9928 #define OP_IdxInsert 111 /* synopsis: key=r[P2] */
9929 #define OP_IdxDelete 112 /* synopsis: key=r[P2@P3] */
9930 #define OP_IdxRowid 113 /* synopsis: r[P2]=rowid */
9931 #define OP_IdxLE 114 /* synopsis: key=r[P3@P4] */
9932 #define OP_IdxGT 115 /* synopsis: key=r[P3@P4] */
9933 #define OP_IdxLT 116 /* synopsis: key=r[P3@P4] */
9934 #define OP_IdxGE 117 /* synopsis: key=r[P3@P4] */
9935 #define OP_Destroy 118
9936 #define OP_Clear 119
9937 #define OP_ResetSorter 120
9938 #define OP_CreateIndex 121 /* synopsis: r[P2]=root iDb=P1 */
9939 #define OP_CreateTable 122 /* synopsis: r[P2]=root iDb=P1 */
9940 #define OP_ParseSchema 123
9941 #define OP_LoadAnalysis 124
9942 #define OP_DropTable 125
9943 #define OP_DropIndex 126
9944 #define OP_DropTrigger 127
9945 #define OP_IntegrityCk 128
9946 #define OP_RowSetAdd 129 /* synopsis: rowset(P1)=r[P2] */
9947 #define OP_RowSetRead 130 /* synopsis: r[P3]=rowset(P1) */
9948 #define OP_RowSetTest 131 /* synopsis: if r[P3] in rowset(P1) goto P2 */
9949 #define OP_Program 132
9950 #define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
9951 #define OP_Param 134
9952 #define OP_FkCounter 135 /* synopsis: fkctr[P1]+=P2 */
9953 #define OP_FkIfZero 136 /* synopsis: if fkctr[P1]==0 goto P2 */
9954 #define OP_MemMax 137 /* synopsis: r[P1]=max(r[P1],r[P2]) */
9955 #define OP_IfPos 138 /* synopsis: if r[P1]>0 goto P2 */
9956 #define OP_IfNeg 139 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */
9957 #define OP_IfNotZero 140 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */
9958 #define OP_DecrJumpZero 141 /* synopsis: if (--r[P1])==0 goto P2 */
9959 #define OP_JumpZeroIncr 142 /* synopsis: if (r[P1]++)==0 ) goto P2 */
9960 #define OP_AggStep0 143 /* synopsis: accum=r[P3] step(r[P2@P5]) */
9961 #define OP_AggStep 144 /* synopsis: accum=r[P3] step(r[P2@P5]) */
9962 #define OP_AggFinal 145 /* synopsis: accum=r[P1] N=P2 */
9963 #define OP_IncrVacuum 146
9964 #define OP_Expire 147
9965 #define OP_TableLock 148 /* synopsis: iDb=P1 root=P2 write=P3 */
9966 #define OP_VBegin 149
9967 #define OP_VCreate 150
9968 #define OP_VDestroy 151
9969 #define OP_VOpen 152
9970 #define OP_VColumn 153 /* synopsis: r[P3]=vcolumn(P2) */
9971 #define OP_VNext 154
9972 #define OP_VRename 155
9973 #define OP_Pagecount 156
9974 #define OP_MaxPgcnt 157
9975 #define OP_Init 158 /* synopsis: Start at P2 */
9976 #define OP_Noop 159
9977 #define OP_Explain 160
9978
9979
9980 /* Properties such as "out2" or "jump" that are specified in
9981 ** comments following the "case" for each opcode in the vdbe.c
9982 ** are encoded into bitvectors as follows:
@@ -9974,30 +9986,31 @@
9986 #define OPFLG_IN2 0x0004 /* in2: P2 is an input */
9987 #define OPFLG_IN3 0x0008 /* in3: P3 is an input */
9988 #define OPFLG_OUT2 0x0010 /* out2: P2 is an output */
9989 #define OPFLG_OUT3 0x0020 /* out3: P3 is an output */
9990 #define OPFLG_INITIALIZER {\
9991 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,\
9992 /* 8 */ 0x01, 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01,\
9993 /* 16 */ 0x02, 0x01, 0x02, 0x12, 0x03, 0x08, 0x00, 0x10,\
9994 /* 24 */ 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00,\
9995 /* 32 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02,\
9996 /* 40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
9997 /* 48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\
9998 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\
9999 /* 64 */ 0x09, 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x26,\
10000 /* 72 */ 0x26, 0x09, 0x10, 0x10, 0x03, 0x03, 0x0b, 0x0b,\
10001 /* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
10002 /* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
10003 /* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
10004 /* 104 */ 0x10, 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04,\
10005 /* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\
10006 /* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
10007 /* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\
10008 /* 136 */ 0x01, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00,\
10009 /* 144 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
10010 /* 152 */ 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01, 0x00,\
10011 /* 160 */ 0x00,}
10012
10013 /************** End of opcodes.h *********************************************/
10014 /************** Continuing where we left off in vdbe.h ***********************/
10015
10016 /*
@@ -10008,10 +10021,11 @@
10021 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
10022 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
10023 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
10024 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
10025 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
10026 SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
10027 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
10028 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
10029 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
10030 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
10031 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
@@ -10400,18 +10414,18 @@
10414 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
10415 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
10416 };
10417
10418 /* Bit values for PgHdr.flags */
10419 #define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
10420 #define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
10421 #define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
10422 #define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
10423 ** writing this page to the database */
10424 #define PGHDR_NEED_READ 0x010 /* Content is unread */
10425 #define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
10426 #define PGHDR_MMAP 0x040 /* This is an mmap page object */
10427
10428 /* Initialize and shutdown the page cache subsystem */
10429 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
10430 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
10431
@@ -11439,13 +11453,13 @@
11453 ** But rather than start with 0 or 1, we begin with 'A'. That way,
11454 ** when multiple affinity types are concatenated into a string and
11455 ** used as the P4 operand, they will be more readable.
11456 **
11457 ** Note also that the numeric types are grouped together so that testing
11458 ** for a numeric type is a single comparison. And the BLOB type is first.
11459 */
11460 #define SQLITE_AFF_BLOB 'A'
11461 #define SQLITE_AFF_TEXT 'B'
11462 #define SQLITE_AFF_NUMERIC 'C'
11463 #define SQLITE_AFF_INTEGER 'D'
11464 #define SQLITE_AFF_REAL 'E'
11465
@@ -12198,11 +12212,11 @@
12212 #endif
12213 int iCursor; /* The VDBE cursor number used to access this table */
12214 Expr *pOn; /* The ON clause of a join */
12215 IdList *pUsing; /* The USING clause of a join */
12216 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
12217 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
12218 Index *pIndex; /* Index structure corresponding to zIndex, if any */
12219 } a[1]; /* One entry for each identifier on the list */
12220 };
12221
12222 /*
@@ -13004,11 +13018,13 @@
13018 # define sqlite3Isalpha(x) isalpha((unsigned char)(x))
13019 # define sqlite3Isdigit(x) isdigit((unsigned char)(x))
13020 # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
13021 # define sqlite3Tolower(x) tolower((unsigned char)(x))
13022 #endif
13023 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
13024 SQLITE_PRIVATE int sqlite3IsIdChar(u8);
13025 #endif
13026
13027 /*
13028 ** Internal function prototypes
13029 */
13030 #define sqlite3StrICmp sqlite3_stricmp
@@ -13032,11 +13048,13 @@
13048 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
13049 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
13050 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
13051 SQLITE_PRIVATE void sqlite3PageFree(void*);
13052 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
13053 #ifndef SQLITE_OMIT_BUILTIN_TEST
13054 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
13055 #endif
13056 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
13057
13058 /*
13059 ** On systems with ample stack space and that support alloca(), make
13060 ** use of alloca() to obtain space for large automatic objects. By default,
@@ -13108,14 +13126,10 @@
13126 #if defined(SQLITE_TEST)
13127 SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
13128 #endif
13129
13130 #if defined(SQLITE_DEBUG)
 
 
 
 
13131 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
13132 SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
13133 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
13134 #endif
13135
@@ -13176,15 +13190,18 @@
13190 SQLITE_PRIVATE int sqlite3FaultSim(int);
13191 #endif
13192
13193 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
13194 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
13195 SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
13196 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
13197 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
13198 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
13199 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
13200 #ifndef SQLITE_OMIT_BUILTIN_TEST
13201 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
13202 #endif
13203
13204 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
13205 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
13206 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
13207 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
@@ -13268,10 +13285,11 @@
13285 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
13286 #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
13287 #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
13288 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
13289 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
13290 SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
13291 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
13292 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
13293 SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
13294 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
13295 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
@@ -13284,12 +13302,14 @@
13302 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
13303 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
13304 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
13305 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
13306 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
13307 #ifndef SQLITE_OMIT_BUILTIN_TEST
13308 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
13309 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
13310 #endif
13311 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
13312 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
13313 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
13314 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
13315 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
@@ -13503,10 +13523,11 @@
13523 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
13524 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
13525 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
13526 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
13527 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
13528 SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
13529 SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
13530 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
13531 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
13532 SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
13533 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
@@ -14620,10 +14641,13 @@
14641 Pgno pgnoRoot; /* Root page of the open btree cursor */
14642 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
14643 i64 seqCount; /* Sequence counter */
14644 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
14645 VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
14646 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
14647 u64 maskUsed; /* Mask of columns used by this cursor */
14648 #endif
14649
14650 /* Cached information about the header for the data record that the
14651 ** cursor is currently pointing to. Only valid if cacheStatus matches
14652 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
14653 ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
@@ -14813,18 +14837,20 @@
14837 **
14838 ** This structure is defined inside of vdbeInt.h because it uses substructures
14839 ** (Mem) which are only defined there.
14840 */
14841 struct sqlite3_context {
14842 Mem *pOut; /* The return value is stored here */
14843 FuncDef *pFunc; /* Pointer to function information */
14844 Mem *pMem; /* Memory cell used to store aggregate context */
14845 Vdbe *pVdbe; /* The VM that owns this context */
14846 int iOp; /* Instruction number of OP_Function */
14847 int isError; /* Error code returned by the function. */
14848 u8 skipFlag; /* Skip accumulator loading if true */
14849 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
14850 u8 argc; /* Number of arguments */
14851 sqlite3_value *argv[1]; /* Argument set */
14852 };
14853
14854 /*
14855 ** An Explain object accumulates indented output which is helpful
14856 ** in describing recursive data structures.
@@ -19193,13 +19219,18 @@
19219 if( sqlite3GlobalConfig.bCoreMutex ){
19220 pFrom = sqlite3DefaultMutex();
19221 }else{
19222 pFrom = sqlite3NoopMutex();
19223 }
19224 pTo->xMutexInit = pFrom->xMutexInit;
19225 pTo->xMutexEnd = pFrom->xMutexEnd;
19226 pTo->xMutexFree = pFrom->xMutexFree;
19227 pTo->xMutexEnter = pFrom->xMutexEnter;
19228 pTo->xMutexTry = pFrom->xMutexTry;
19229 pTo->xMutexLeave = pFrom->xMutexLeave;
19230 pTo->xMutexHeld = pFrom->xMutexHeld;
19231 pTo->xMutexNotheld = pFrom->xMutexNotheld;
19232 pTo->xMutexAlloc = pFrom->xMutexAlloc;
19233 }
19234 rc = sqlite3GlobalConfig.mutex.xMutexInit();
19235
19236 #ifdef SQLITE_DEBUG
@@ -21353,21 +21384,20 @@
21384 **
21385 ** The returned value is normally a copy of the second argument to this
21386 ** function. However, if a malloc() failure has occurred since the previous
21387 ** invocation SQLITE_NOMEM is returned instead.
21388 **
21389 ** If an OOM as occurred, then the connection error-code (the value
21390 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
 
21391 */
21392 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
21393 /* If the db handle must hold the connection handle mutex here.
21394 ** Otherwise the read (and possible write) of db->mallocFailed
21395 ** is unsafe, as is the call to sqlite3Error().
21396 */
21397 assert( db!=0 );
21398 assert( sqlite3_mutex_held(db->mutex) );
21399 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
21400 return apiOomError(db);
21401 }
21402 return rc & db->errMask;
21403 }
@@ -21374,21 +21404,18 @@
21404
21405 /************** End of malloc.c **********************************************/
21406 /************** Begin file printf.c ******************************************/
21407 /*
21408 ** The "printf" code that follows dates from the 1980's. It is in
21409 ** the public domain.
 
 
 
21410 **
21411 **************************************************************************
21412 **
21413 ** This file contains code for a set of "printf"-like routines. These
21414 ** routines format strings much like the printf() from the standard C
21415 ** library, though the implementation here has enhancements to support
21416 ** SQLite.
21417 */
21418
21419 /*
21420 ** Conversion types fall into various categories as defined by the
21421 ** following enumeration.
@@ -22431,26 +22458,49 @@
22458 fprintf(stdout,"%s", zBuf);
22459 fflush(stdout);
22460 }
22461 #endif
22462
22463
22464 /*
22465 ** variable-argument wrapper around sqlite3VXPrintf().
22466 */
22467 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
22468 va_list ap;
22469 va_start(ap,zFormat);
22470 sqlite3VXPrintf(p, bFlags, zFormat, ap);
22471 va_end(ap);
22472 }
22473
22474 /************** End of printf.c **********************************************/
22475 /************** Begin file treeview.c ****************************************/
22476 /*
22477 ** 2015-06-08
22478 **
22479 ** The author disclaims copyright to this source code. In place of
22480 ** a legal notice, here is a blessing:
22481 **
22482 ** May you do good and not evil.
22483 ** May you find forgiveness for yourself and forgive others.
22484 ** May you share freely, never taking more than you give.
22485 **
22486 *************************************************************************
22487 **
22488 ** This file contains C code to implement the TreeView debugging routines.
22489 ** These routines print a parse tree to standard output for debugging and
22490 ** analysis.
22491 **
22492 ** The interfaces in this file is only available when compiling
22493 ** with SQLITE_DEBUG.
22494 */
22495 #ifdef SQLITE_DEBUG
22496
22497 /*
22498 ** Add a new subitem to the tree. The moreToFollow flag indicates that this
22499 ** is not the last item in the tree.
 
 
 
 
 
 
 
22500 */
22501 static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
 
 
22502 if( p==0 ){
22503 p = sqlite3_malloc64( sizeof(*p) );
22504 if( p==0 ) return 0;
22505 memset(p, 0, sizeof(*p));
22506 }else{
@@ -22458,19 +22508,25 @@
22508 }
22509 assert( moreToFollow==0 || moreToFollow==1 );
22510 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
22511 return p;
22512 }
22513
22514 /*
22515 ** Finished with one layer of the tree
22516 */
22517 static void sqlite3TreeViewPop(TreeView *p){
22518 if( p==0 ) return;
22519 p->iLevel--;
22520 if( p->iLevel<0 ) sqlite3_free(p);
22521 }
22522
22523 /*
22524 ** Generate a single line of output for the tree, with a prefix that contains
22525 ** all the appropriate tree lines
22526 */
22527 static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
22528 va_list ap;
22529 int i;
22530 StrAccum acc;
22531 char zBuf[500];
22532 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
@@ -22486,28 +22542,371 @@
22542 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
22543 sqlite3StrAccumFinish(&acc);
22544 fprintf(stdout,"%s", zBuf);
22545 fflush(stdout);
22546 }
22547
22548 /*
22549 ** Shorthand for starting a new tree item that consists of a single label
22550 */
22551 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
22552 p = sqlite3TreeViewPush(p, moreFollows);
22553 sqlite3TreeViewLine(p, "%s", zLabel);
22554 }
22555
22556
22557 /*
22558 ** Generate a human-readable description of a the Select object.
22559 */
22560 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
22561 int n = 0;
22562 pView = sqlite3TreeViewPush(pView, moreToFollow);
22563 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
22564 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
22565 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
22566 );
22567 if( p->pSrc && p->pSrc->nSrc ) n++;
22568 if( p->pWhere ) n++;
22569 if( p->pGroupBy ) n++;
22570 if( p->pHaving ) n++;
22571 if( p->pOrderBy ) n++;
22572 if( p->pLimit ) n++;
22573 if( p->pOffset ) n++;
22574 if( p->pPrior ) n++;
22575 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
22576 if( p->pSrc && p->pSrc->nSrc ){
22577 int i;
22578 pView = sqlite3TreeViewPush(pView, (n--)>0);
22579 sqlite3TreeViewLine(pView, "FROM");
22580 for(i=0; i<p->pSrc->nSrc; i++){
22581 struct SrcList_item *pItem = &p->pSrc->a[i];
22582 StrAccum x;
22583 char zLine[100];
22584 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
22585 sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
22586 if( pItem->zDatabase ){
22587 sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
22588 }else if( pItem->zName ){
22589 sqlite3XPrintf(&x, 0, " %s", pItem->zName);
22590 }
22591 if( pItem->pTab ){
22592 sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
22593 }
22594 if( pItem->zAlias ){
22595 sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
22596 }
22597 if( pItem->jointype & JT_LEFT ){
22598 sqlite3XPrintf(&x, 0, " LEFT-JOIN");
22599 }
22600 sqlite3StrAccumFinish(&x);
22601 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
22602 if( pItem->pSelect ){
22603 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
22604 }
22605 sqlite3TreeViewPop(pView);
22606 }
22607 sqlite3TreeViewPop(pView);
22608 }
22609 if( p->pWhere ){
22610 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
22611 sqlite3TreeViewExpr(pView, p->pWhere, 0);
22612 sqlite3TreeViewPop(pView);
22613 }
22614 if( p->pGroupBy ){
22615 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
22616 }
22617 if( p->pHaving ){
22618 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
22619 sqlite3TreeViewExpr(pView, p->pHaving, 0);
22620 sqlite3TreeViewPop(pView);
22621 }
22622 if( p->pOrderBy ){
22623 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
22624 }
22625 if( p->pLimit ){
22626 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
22627 sqlite3TreeViewExpr(pView, p->pLimit, 0);
22628 sqlite3TreeViewPop(pView);
22629 }
22630 if( p->pOffset ){
22631 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
22632 sqlite3TreeViewExpr(pView, p->pOffset, 0);
22633 sqlite3TreeViewPop(pView);
22634 }
22635 if( p->pPrior ){
22636 const char *zOp = "UNION";
22637 switch( p->op ){
22638 case TK_ALL: zOp = "UNION ALL"; break;
22639 case TK_INTERSECT: zOp = "INTERSECT"; break;
22640 case TK_EXCEPT: zOp = "EXCEPT"; break;
22641 }
22642 sqlite3TreeViewItem(pView, zOp, (n--)>0);
22643 sqlite3TreeViewSelect(pView, p->pPrior, 0);
22644 sqlite3TreeViewPop(pView);
22645 }
22646 sqlite3TreeViewPop(pView);
22647 }
22648
22649 /*
22650 ** Generate a human-readable explanation of an expression tree.
22651 */
22652 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
22653 const char *zBinOp = 0; /* Binary operator */
22654 const char *zUniOp = 0; /* Unary operator */
22655 char zFlgs[30];
22656 pView = sqlite3TreeViewPush(pView, moreToFollow);
22657 if( pExpr==0 ){
22658 sqlite3TreeViewLine(pView, "nil");
22659 sqlite3TreeViewPop(pView);
22660 return;
22661 }
22662 if( pExpr->flags ){
22663 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
22664 }else{
22665 zFlgs[0] = 0;
22666 }
22667 switch( pExpr->op ){
22668 case TK_AGG_COLUMN: {
22669 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
22670 pExpr->iTable, pExpr->iColumn, zFlgs);
22671 break;
22672 }
22673 case TK_COLUMN: {
22674 if( pExpr->iTable<0 ){
22675 /* This only happens when coding check constraints */
22676 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
22677 }else{
22678 sqlite3TreeViewLine(pView, "{%d:%d}%s",
22679 pExpr->iTable, pExpr->iColumn, zFlgs);
22680 }
22681 break;
22682 }
22683 case TK_INTEGER: {
22684 if( pExpr->flags & EP_IntValue ){
22685 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
22686 }else{
22687 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
22688 }
22689 break;
22690 }
22691 #ifndef SQLITE_OMIT_FLOATING_POINT
22692 case TK_FLOAT: {
22693 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
22694 break;
22695 }
22696 #endif
22697 case TK_STRING: {
22698 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
22699 break;
22700 }
22701 case TK_NULL: {
22702 sqlite3TreeViewLine(pView,"NULL");
22703 break;
22704 }
22705 #ifndef SQLITE_OMIT_BLOB_LITERAL
22706 case TK_BLOB: {
22707 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
22708 break;
22709 }
22710 #endif
22711 case TK_VARIABLE: {
22712 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
22713 pExpr->u.zToken, pExpr->iColumn);
22714 break;
22715 }
22716 case TK_REGISTER: {
22717 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
22718 break;
22719 }
22720 case TK_AS: {
22721 sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
22722 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22723 break;
22724 }
22725 case TK_ID: {
22726 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
22727 break;
22728 }
22729 #ifndef SQLITE_OMIT_CAST
22730 case TK_CAST: {
22731 /* Expressions of the form: CAST(pLeft AS token) */
22732 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
22733 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22734 break;
22735 }
22736 #endif /* SQLITE_OMIT_CAST */
22737 case TK_LT: zBinOp = "LT"; break;
22738 case TK_LE: zBinOp = "LE"; break;
22739 case TK_GT: zBinOp = "GT"; break;
22740 case TK_GE: zBinOp = "GE"; break;
22741 case TK_NE: zBinOp = "NE"; break;
22742 case TK_EQ: zBinOp = "EQ"; break;
22743 case TK_IS: zBinOp = "IS"; break;
22744 case TK_ISNOT: zBinOp = "ISNOT"; break;
22745 case TK_AND: zBinOp = "AND"; break;
22746 case TK_OR: zBinOp = "OR"; break;
22747 case TK_PLUS: zBinOp = "ADD"; break;
22748 case TK_STAR: zBinOp = "MUL"; break;
22749 case TK_MINUS: zBinOp = "SUB"; break;
22750 case TK_REM: zBinOp = "REM"; break;
22751 case TK_BITAND: zBinOp = "BITAND"; break;
22752 case TK_BITOR: zBinOp = "BITOR"; break;
22753 case TK_SLASH: zBinOp = "DIV"; break;
22754 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
22755 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
22756 case TK_CONCAT: zBinOp = "CONCAT"; break;
22757 case TK_DOT: zBinOp = "DOT"; break;
22758
22759 case TK_UMINUS: zUniOp = "UMINUS"; break;
22760 case TK_UPLUS: zUniOp = "UPLUS"; break;
22761 case TK_BITNOT: zUniOp = "BITNOT"; break;
22762 case TK_NOT: zUniOp = "NOT"; break;
22763 case TK_ISNULL: zUniOp = "ISNULL"; break;
22764 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
22765
22766 case TK_COLLATE: {
22767 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
22768 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22769 break;
22770 }
22771
22772 case TK_AGG_FUNCTION:
22773 case TK_FUNCTION: {
22774 ExprList *pFarg; /* List of function arguments */
22775 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
22776 pFarg = 0;
22777 }else{
22778 pFarg = pExpr->x.pList;
22779 }
22780 if( pExpr->op==TK_AGG_FUNCTION ){
22781 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
22782 pExpr->op2, pExpr->u.zToken);
22783 }else{
22784 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
22785 }
22786 if( pFarg ){
22787 sqlite3TreeViewExprList(pView, pFarg, 0, 0);
22788 }
22789 break;
22790 }
22791 #ifndef SQLITE_OMIT_SUBQUERY
22792 case TK_EXISTS: {
22793 sqlite3TreeViewLine(pView, "EXISTS-expr");
22794 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22795 break;
22796 }
22797 case TK_SELECT: {
22798 sqlite3TreeViewLine(pView, "SELECT-expr");
22799 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22800 break;
22801 }
22802 case TK_IN: {
22803 sqlite3TreeViewLine(pView, "IN");
22804 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22805 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
22806 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22807 }else{
22808 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
22809 }
22810 break;
22811 }
22812 #endif /* SQLITE_OMIT_SUBQUERY */
22813
22814 /*
22815 ** x BETWEEN y AND z
22816 **
22817 ** This is equivalent to
22818 **
22819 ** x>=y AND x<=z
22820 **
22821 ** X is stored in pExpr->pLeft.
22822 ** Y is stored in pExpr->pList->a[0].pExpr.
22823 ** Z is stored in pExpr->pList->a[1].pExpr.
22824 */
22825 case TK_BETWEEN: {
22826 Expr *pX = pExpr->pLeft;
22827 Expr *pY = pExpr->x.pList->a[0].pExpr;
22828 Expr *pZ = pExpr->x.pList->a[1].pExpr;
22829 sqlite3TreeViewLine(pView, "BETWEEN");
22830 sqlite3TreeViewExpr(pView, pX, 1);
22831 sqlite3TreeViewExpr(pView, pY, 1);
22832 sqlite3TreeViewExpr(pView, pZ, 0);
22833 break;
22834 }
22835 case TK_TRIGGER: {
22836 /* If the opcode is TK_TRIGGER, then the expression is a reference
22837 ** to a column in the new.* or old.* pseudo-tables available to
22838 ** trigger programs. In this case Expr.iTable is set to 1 for the
22839 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
22840 ** is set to the column of the pseudo-table to read, or to -1 to
22841 ** read the rowid field.
22842 */
22843 sqlite3TreeViewLine(pView, "%s(%d)",
22844 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
22845 break;
22846 }
22847 case TK_CASE: {
22848 sqlite3TreeViewLine(pView, "CASE");
22849 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22850 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
22851 break;
22852 }
22853 #ifndef SQLITE_OMIT_TRIGGER
22854 case TK_RAISE: {
22855 const char *zType = "unk";
22856 switch( pExpr->affinity ){
22857 case OE_Rollback: zType = "rollback"; break;
22858 case OE_Abort: zType = "abort"; break;
22859 case OE_Fail: zType = "fail"; break;
22860 case OE_Ignore: zType = "ignore"; break;
22861 }
22862 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
22863 break;
22864 }
22865 #endif
22866 default: {
22867 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
22868 break;
22869 }
22870 }
22871 if( zBinOp ){
22872 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
22873 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22874 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
22875 }else if( zUniOp ){
22876 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
22877 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22878 }
22879 sqlite3TreeViewPop(pView);
22880 }
22881
22882 /*
22883 ** Generate a human-readable explanation of an expression list.
22884 */
22885 SQLITE_PRIVATE void sqlite3TreeViewExprList(
22886 TreeView *pView,
22887 const ExprList *pList,
22888 u8 moreToFollow,
22889 const char *zLabel
22890 ){
22891 int i;
22892 pView = sqlite3TreeViewPush(pView, moreToFollow);
22893 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
22894 if( pList==0 ){
22895 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
22896 }else{
22897 sqlite3TreeViewLine(pView, "%s", zLabel);
22898 for(i=0; i<pList->nExpr; i++){
22899 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
22900 }
22901 }
22902 sqlite3TreeViewPop(pView);
22903 }
22904
22905 #endif /* SQLITE_DEBUG */
22906
22907 /************** End of treeview.c ********************************************/
 
 
 
 
 
 
 
 
 
 
22908 /************** Begin file random.c ******************************************/
22909 /*
22910 ** 2001 September 15
22911 **
22912 ** The author disclaims copyright to this source code. In place of
@@ -23544,14 +23943,12 @@
23943 ** The value returned will never be negative. Nor will it ever be greater
23944 ** than the actual length of the string. For very long strings (greater
23945 ** than 1GiB) the value returned might be less than the true string length.
23946 */
23947 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
 
23948 if( z==0 ) return 0;
23949 return 0x3fffffff & (int)strlen(z);
 
23950 }
23951
23952 /*
23953 ** Set the current error code to err_code and clear any prior error message.
23954 */
@@ -24519,18 +24916,35 @@
24916
24917 /*
24918 ** Read or write a four-byte big-endian integer value.
24919 */
24920 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
24921 #if SQLITE_BYTEORDER==4321
24922 u32 x;
24923 memcpy(&x,p,4);
24924 return x;
24925 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
24926 u32 x;
24927 memcpy(&x,p,4);
24928 return __builtin_bswap32(x);
24929 #else
24930 testcase( p[0]&0x80 );
24931 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
24932 #endif
24933 }
24934 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
24935 #if SQLITE_BYTEORDER==4321
24936 memcpy(p,&v,4);
24937 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__)
24938 u32 x = __builtin_bswap32(v);
24939 memcpy(p,&x,4);
24940 #else
24941 p[0] = (u8)(v>>24);
24942 p[1] = (u8)(v>>16);
24943 p[2] = (u8)(v>>8);
24944 p[3] = (u8)v;
24945 #endif
24946 }
24947
24948
24949
24950 /*
@@ -25094,46 +25508,46 @@
25508 #else
25509 # define OpHelp(X)
25510 #endif
25511 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
25512 static const char *const azName[] = { "?",
25513 /* 1 */ "Savepoint" OpHelp(""),
25514 /* 2 */ "AutoCommit" OpHelp(""),
25515 /* 3 */ "Transaction" OpHelp(""),
25516 /* 4 */ "SorterNext" OpHelp(""),
25517 /* 5 */ "PrevIfOpen" OpHelp(""),
25518 /* 6 */ "NextIfOpen" OpHelp(""),
25519 /* 7 */ "Prev" OpHelp(""),
25520 /* 8 */ "Next" OpHelp(""),
25521 /* 9 */ "Checkpoint" OpHelp(""),
25522 /* 10 */ "JournalMode" OpHelp(""),
25523 /* 11 */ "Vacuum" OpHelp(""),
25524 /* 12 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
25525 /* 13 */ "VUpdate" OpHelp("data=r[P3@P2]"),
25526 /* 14 */ "Goto" OpHelp(""),
25527 /* 15 */ "Gosub" OpHelp(""),
25528 /* 16 */ "Return" OpHelp(""),
25529 /* 17 */ "InitCoroutine" OpHelp(""),
25530 /* 18 */ "EndCoroutine" OpHelp(""),
25531 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
25532 /* 20 */ "Yield" OpHelp(""),
25533 /* 21 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
25534 /* 22 */ "Halt" OpHelp(""),
25535 /* 23 */ "Integer" OpHelp("r[P2]=P1"),
25536 /* 24 */ "Int64" OpHelp("r[P2]=P4"),
25537 /* 25 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
25538 /* 26 */ "Null" OpHelp("r[P2..P3]=NULL"),
25539 /* 27 */ "SoftNull" OpHelp("r[P1]=NULL"),
25540 /* 28 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
25541 /* 29 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
25542 /* 30 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
25543 /* 31 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
25544 /* 32 */ "SCopy" OpHelp("r[P2]=r[P1]"),
25545 /* 33 */ "ResultRow" OpHelp("output=r[P1@P2]"),
25546 /* 34 */ "CollSeq" OpHelp(""),
25547 /* 35 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
25548 /* 36 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
25549 /* 37 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
25550 /* 38 */ "MustBeInt" OpHelp(""),
25551 /* 39 */ "RealAffinity" OpHelp(""),
25552 /* 40 */ "Cast" OpHelp("affinity(r[P1])"),
25553 /* 41 */ "Permutation" OpHelp(""),
@@ -25155,33 +25569,33 @@
25569 /* 57 */ "OpenEphemeral" OpHelp("nColumn=P2"),
25570 /* 58 */ "SorterOpen" OpHelp(""),
25571 /* 59 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
25572 /* 60 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
25573 /* 61 */ "Close" OpHelp(""),
25574 /* 62 */ "ColumnsUsed" OpHelp(""),
25575 /* 63 */ "SeekLT" OpHelp("key=r[P3@P4]"),
25576 /* 64 */ "SeekLE" OpHelp("key=r[P3@P4]"),
25577 /* 65 */ "SeekGE" OpHelp("key=r[P3@P4]"),
25578 /* 66 */ "SeekGT" OpHelp("key=r[P3@P4]"),
25579 /* 67 */ "Seek" OpHelp("intkey=r[P2]"),
25580 /* 68 */ "NoConflict" OpHelp("key=r[P3@P4]"),
25581 /* 69 */ "NotFound" OpHelp("key=r[P3@P4]"),
25582 /* 70 */ "Found" OpHelp("key=r[P3@P4]"),
25583 /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
25584 /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
25585 /* 73 */ "NotExists" OpHelp("intkey=r[P3]"),
25586 /* 74 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
25587 /* 75 */ "NewRowid" OpHelp("r[P2]=rowid"),
25588 /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
25589 /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
25590 /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
25591 /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
25592 /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
25593 /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
25594 /* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
25595 /* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
25596 /* 84 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
25597 /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
25598 /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
25599 /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
25600 /* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
25601 /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -25188,73 +25602,76 @@
25602 /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
25603 /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
25604 /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
25605 /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
25606 /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
25607 /* 95 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
25608 /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
25609 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
25610 /* 98 */ "Delete" OpHelp(""),
25611 /* 99 */ "ResetCount" OpHelp(""),
25612 /* 100 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
25613 /* 101 */ "SorterData" OpHelp("r[P2]=data"),
25614 /* 102 */ "RowKey" OpHelp("r[P2]=key"),
25615 /* 103 */ "RowData" OpHelp("r[P2]=data"),
25616 /* 104 */ "Rowid" OpHelp("r[P2]=rowid"),
25617 /* 105 */ "NullRow" OpHelp(""),
25618 /* 106 */ "Last" OpHelp(""),
25619 /* 107 */ "SorterSort" OpHelp(""),
25620 /* 108 */ "Sort" OpHelp(""),
25621 /* 109 */ "Rewind" OpHelp(""),
25622 /* 110 */ "SorterInsert" OpHelp(""),
25623 /* 111 */ "IdxInsert" OpHelp("key=r[P2]"),
25624 /* 112 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
25625 /* 113 */ "IdxRowid" OpHelp("r[P2]=rowid"),
25626 /* 114 */ "IdxLE" OpHelp("key=r[P3@P4]"),
25627 /* 115 */ "IdxGT" OpHelp("key=r[P3@P4]"),
25628 /* 116 */ "IdxLT" OpHelp("key=r[P3@P4]"),
25629 /* 117 */ "IdxGE" OpHelp("key=r[P3@P4]"),
25630 /* 118 */ "Destroy" OpHelp(""),
25631 /* 119 */ "Clear" OpHelp(""),
25632 /* 120 */ "ResetSorter" OpHelp(""),
25633 /* 121 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
25634 /* 122 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
25635 /* 123 */ "ParseSchema" OpHelp(""),
25636 /* 124 */ "LoadAnalysis" OpHelp(""),
25637 /* 125 */ "DropTable" OpHelp(""),
25638 /* 126 */ "DropIndex" OpHelp(""),
25639 /* 127 */ "DropTrigger" OpHelp(""),
25640 /* 128 */ "IntegrityCk" OpHelp(""),
25641 /* 129 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
25642 /* 130 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
25643 /* 131 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
25644 /* 132 */ "Program" OpHelp(""),
25645 /* 133 */ "Real" OpHelp("r[P2]=P4"),
25646 /* 134 */ "Param" OpHelp(""),
25647 /* 135 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
25648 /* 136 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
25649 /* 137 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
25650 /* 138 */ "IfPos" OpHelp("if r[P1]>0 goto P2"),
25651 /* 139 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
25652 /* 140 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"),
25653 /* 141 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
25654 /* 142 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"),
25655 /* 143 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
25656 /* 144 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
25657 /* 145 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
25658 /* 146 */ "IncrVacuum" OpHelp(""),
25659 /* 147 */ "Expire" OpHelp(""),
25660 /* 148 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
25661 /* 149 */ "VBegin" OpHelp(""),
25662 /* 150 */ "VCreate" OpHelp(""),
25663 /* 151 */ "VDestroy" OpHelp(""),
25664 /* 152 */ "VOpen" OpHelp(""),
25665 /* 153 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
25666 /* 154 */ "VNext" OpHelp(""),
25667 /* 155 */ "VRename" OpHelp(""),
25668 /* 156 */ "Pagecount" OpHelp(""),
25669 /* 157 */ "MaxPgcnt" OpHelp(""),
25670 /* 158 */ "Init" OpHelp("Start at P2"),
25671 /* 159 */ "Noop" OpHelp(""),
25672 /* 160 */ "Explain" OpHelp(""),
25673 };
25674 return azName[i];
25675 }
25676 #endif
25677
@@ -38989,14 +39406,14 @@
39406 /*
39407 ** Check to see if the i-th bit is set. Return true or false.
39408 ** If p is NULL (if the bitmap has not been created) or if
39409 ** i is out of range, then return false.
39410 */
39411 SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
39412 assert( p!=0 );
 
39413 i--;
39414 if( i>=p->iSize ) return 0;
39415 while( p->iDivisor ){
39416 u32 bin = i/p->iDivisor;
39417 i = i%p->iDivisor;
39418 p = p->u.apSub[bin];
39419 if (!p) {
@@ -39011,10 +39428,13 @@
39428 if( p->u.aHash[h]==i ) return 1;
39429 h = (h+1) % BITVEC_NINT;
39430 }
39431 return 0;
39432 }
39433 }
39434 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
39435 return p!=0 && sqlite3BitvecTestNotNull(p,i);
39436 }
39437
39438 /*
39439 ** Set the i-th bit. Return 0 on success and an error code if
39440 ** anything goes wrong.
@@ -39300,11 +39720,10 @@
39720 u8 bPurgeable; /* True if pages are on backing store */
39721 u8 eCreate; /* eCreate value for for xFetch() */
39722 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
39723 void *pStress; /* Argument to xStress */
39724 sqlite3_pcache *pCache; /* Pluggable cache module */
 
39725 };
39726
39727 /********************************** Linked List Management ********************/
39728
39729 /* Allowed values for second argument to pcacheManageDirtyList() */
@@ -39378,13 +39797,10 @@
39797 ** Wrapper around the pluggable caches xUnpin method. If the cache is
39798 ** being used for an in-memory database, this function is a no-op.
39799 */
39800 static void pcacheUnpin(PgHdr *p){
39801 if( p->pCache->bPurgeable ){
 
 
 
39802 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
39803 }
39804 }
39805
39806 /*
@@ -39473,11 +39889,10 @@
39889 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
39890 if( pCache->pCache ){
39891 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
39892 }
39893 pCache->pCache = pNew;
 
39894 pCache->szPage = szPage;
39895 }
39896 return SQLITE_OK;
39897 }
39898
@@ -39598,17 +40013,18 @@
40013 ){
40014 PgHdr *pPgHdr;
40015 assert( pPage!=0 );
40016 pPgHdr = (PgHdr*)pPage->pExtra;
40017 assert( pPgHdr->pPage==0 );
40018 memset(pPgHdr, 0, sizeof(PgHdr));
40019 pPgHdr->pPage = pPage;
40020 pPgHdr->pData = pPage->pBuf;
40021 pPgHdr->pExtra = (void *)&pPgHdr[1];
40022 memset(pPgHdr->pExtra, 0, pCache->szExtra);
40023 pPgHdr->pCache = pCache;
40024 pPgHdr->pgno = pgno;
40025 pPgHdr->flags = PGHDR_CLEAN;
40026 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
40027 }
40028
40029 /*
40030 ** This routine converts the sqlite3_pcache_page object returned by
@@ -39621,23 +40037,20 @@
40037 Pgno pgno, /* Page number obtained */
40038 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
40039 ){
40040 PgHdr *pPgHdr;
40041
40042 assert( pPage!=0 );
40043 pPgHdr = (PgHdr *)pPage->pExtra;
40044
40045 if( !pPgHdr->pPage ){
40046 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
40047 }
40048 if( 0==pPgHdr->nRef ){
40049 pCache->nRef++;
40050 }
40051 pPgHdr->nRef++;
 
 
 
40052 return pPgHdr;
40053 }
40054
40055 /*
40056 ** Decrement the reference count on a page. If the page is clean and the
@@ -39646,11 +40059,11 @@
40059 SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
40060 assert( p->nRef>0 );
40061 p->nRef--;
40062 if( p->nRef==0 ){
40063 p->pCache->nRef--;
40064 if( p->flags&PGHDR_CLEAN ){
40065 pcacheUnpin(p);
40066 }else if( p->pDirtyPrev!=0 ){
40067 /* Move the page to the head of the dirty list. */
40068 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
40069 }
@@ -39674,37 +40087,39 @@
40087 assert( p->nRef==1 );
40088 if( p->flags&PGHDR_DIRTY ){
40089 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
40090 }
40091 p->pCache->nRef--;
 
 
 
40092 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
40093 }
40094
40095 /*
40096 ** Make sure the page is marked as dirty. If it isn't dirty already,
40097 ** make it so.
40098 */
40099 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
 
40100 assert( p->nRef>0 );
40101 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){
40102 p->flags &= ~PGHDR_DONT_WRITE;
40103 if( p->flags & PGHDR_CLEAN ){
40104 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
40105 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
40106 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
40107 }
40108 }
40109 }
40110
40111 /*
40112 ** Make sure the page is marked as clean. If it isn't clean already,
40113 ** make it so.
40114 */
40115 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
40116 if( (p->flags & PGHDR_DIRTY) ){
40117 assert( (p->flags & PGHDR_CLEAN)==0 );
40118 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
40119 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
40120 p->flags |= PGHDR_CLEAN;
40121 if( p->nRef==0 ){
40122 pcacheUnpin(p);
40123 }
40124 }
40125 }
@@ -39767,13 +40182,18 @@
40182 if( ALWAYS(p->pgno>pgno) ){
40183 assert( p->flags&PGHDR_DIRTY );
40184 sqlite3PcacheMakeClean(p);
40185 }
40186 }
40187 if( pgno==0 && pCache->nRef ){
40188 sqlite3_pcache_page *pPage1;
40189 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
40190 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
40191 ** pCache->nRef>0 */
40192 memset(pPage1->pBuf, 0, pCache->szPage);
40193 pgno = 1;
40194 }
40195 }
40196 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
40197 }
40198 }
40199
@@ -40092,12 +40512,19 @@
40512 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
40513
40514 /*
40515 ** Macros to enter and leave the PCache LRU mutex.
40516 */
40517 #if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
40518 # define pcache1EnterMutex(X) assert((X)->mutex==0)
40519 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
40520 # define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
40521 #else
40522 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
40523 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
40524 # define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
40525 #endif
40526
40527 /******************************************************************************/
40528 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
40529
40530 /*
@@ -40369,45 +40796,45 @@
40796 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
40797 ** LRU list, then this function is a no-op.
40798 **
40799 ** The PGroup mutex must be held when this function is called.
40800 */
40801 static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
40802 PCache1 *pCache;
 
40803
40804 assert( pPage!=0 );
40805 assert( pPage->isPinned==0 );
40806 pCache = pPage->pCache;
40807 assert( pPage->pLruNext || pPage==pCache->pGroup->pLruTail );
40808 assert( pPage->pLruPrev || pPage==pCache->pGroup->pLruHead );
40809 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 
40810 if( pPage->pLruPrev ){
40811 pPage->pLruPrev->pLruNext = pPage->pLruNext;
40812 }else{
40813 pCache->pGroup->pLruHead = pPage->pLruNext;
40814 }
40815 if( pPage->pLruNext ){
40816 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
40817 }else{
40818 pCache->pGroup->pLruTail = pPage->pLruPrev;
40819 }
40820 pPage->pLruNext = 0;
40821 pPage->pLruPrev = 0;
40822 pPage->isPinned = 1;
40823 pCache->nRecyclable--;
40824 return pPage;
40825 }
40826
40827
40828 /*
40829 ** Remove the page supplied as an argument from the hash table
40830 ** (PCache1.apHash structure) that it is currently stored in.
40831 ** Also free the page if freePage is true.
40832 **
40833 ** The PGroup mutex must be held when this function is called.
40834 */
40835 static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
40836 unsigned int h;
40837 PCache1 *pCache = pPage->pCache;
40838 PgHdr1 **pp;
40839
40840 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
@@ -40414,10 +40841,11 @@
40841 h = pPage->iKey % pCache->nHash;
40842 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
40843 *pp = (*pp)->pNext;
40844
40845 pCache->nPage--;
40846 if( freeFlag ) pcache1FreePage(pPage);
40847 }
40848
40849 /*
40850 ** If there are currently more than nMaxPage pages allocated, try
40851 ** to recycle pages to reduce the number allocated to nMaxPage.
@@ -40427,12 +40855,11 @@
40855 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
40856 PgHdr1 *p = pGroup->pLruTail;
40857 assert( p->pCache->pGroup==pGroup );
40858 assert( p->isPinned==0 );
40859 pcache1PinPage(p);
40860 pcache1RemoveFromHash(p, 1);
 
40861 }
40862 }
40863
40864 /*
40865 ** Discard all pages from cache pCache with a page number (key value)
@@ -40474,14 +40901,16 @@
40901 */
40902 static int pcache1Init(void *NotUsed){
40903 UNUSED_PARAMETER(NotUsed);
40904 assert( pcache1.isInit==0 );
40905 memset(&pcache1, 0, sizeof(pcache1));
40906 #if SQLITE_THREADSAFE
40907 if( sqlite3GlobalConfig.bCoreMutex ){
40908 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
40909 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
40910 }
40911 #endif
40912 pcache1.grp.mxPinned = 10;
40913 pcache1.isInit = 1;
40914 return SQLITE_OK;
40915 }
40916
@@ -40650,11 +41079,11 @@
41079 || pcache1UnderMemoryPressure(pCache)
41080 )){
41081 PCache1 *pOther;
41082 pPage = pGroup->pLruTail;
41083 assert( pPage->isPinned==0 );
41084 pcache1RemoveFromHash(pPage, 0);
41085 pcache1PinPage(pPage);
41086 pOther = pPage->pCache;
41087
41088 /* We want to verify that szPage and szExtra are the same for pOther
41089 ** and pCache. Assert that we can verify this by comparing sums. */
@@ -40673,13 +41102,13 @@
41102
41103 /* Step 5. If a usable page buffer has still not been found,
41104 ** attempt to allocate a new one.
41105 */
41106 if( !pPage ){
41107 if( createFlag==1 ){ sqlite3BeginBenignMalloc(); }
41108 pPage = pcache1AllocPage(pCache);
41109 if( createFlag==1 ){ sqlite3EndBenignMalloc(); }
41110 }
41111
41112 if( pPage ){
41113 unsigned int h = iKey % pCache->nHash;
41114 pCache->nPage++;
@@ -40749,41 +41178,81 @@
41178 ** then attempt to recycle a page from the LRU list. If it is the right
41179 ** size, return the recycled buffer. Otherwise, free the buffer and
41180 ** proceed to step 5.
41181 **
41182 ** 5. Otherwise, allocate and return a new page buffer.
41183 **
41184 ** There are two versions of this routine. pcache1FetchWithMutex() is
41185 ** the general case. pcache1FetchNoMutex() is a faster implementation for
41186 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
41187 ** invokes the appropriate routine.
41188 */
41189 static PgHdr1 *pcache1FetchNoMutex(
41190 sqlite3_pcache *p,
41191 unsigned int iKey,
41192 int createFlag
41193 ){
41194 PCache1 *pCache = (PCache1 *)p;
41195 PgHdr1 *pPage = 0;
41196
 
 
 
 
 
 
 
 
41197 /* Step 1: Search the hash table for an existing entry. */
41198 pPage = pCache->apHash[iKey % pCache->nHash];
41199 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
41200
41201 /* Step 2: Abort if no existing page is found and createFlag is 0 */
41202 if( pPage ){
41203 if( !pPage->isPinned ){
41204 return pcache1PinPage(pPage);
41205 }else{
41206 return pPage;
41207 }
41208 }else if( createFlag ){
41209 /* Steps 3, 4, and 5 implemented by this subroutine */
41210 return pcache1FetchStage2(pCache, iKey, createFlag);
41211 }else{
41212 return 0;
41213 }
41214 }
41215 #if PCACHE1_MIGHT_USE_GROUP_MUTEX
41216 static PgHdr1 *pcache1FetchWithMutex(
41217 sqlite3_pcache *p,
41218 unsigned int iKey,
41219 int createFlag
41220 ){
41221 PCache1 *pCache = (PCache1 *)p;
41222 PgHdr1 *pPage;
41223
41224 pcache1EnterMutex(pCache->pGroup);
41225 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
41226 assert( pPage==0 || pCache->iMaxKey>=iKey );
41227 pcache1LeaveMutex(pCache->pGroup);
41228 return pPage;
41229 }
41230 #endif
41231 static sqlite3_pcache_page *pcache1Fetch(
41232 sqlite3_pcache *p,
41233 unsigned int iKey,
41234 int createFlag
41235 ){
41236 #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
41237 PCache1 *pCache = (PCache1 *)p;
41238 #endif
41239
41240 assert( offsetof(PgHdr1,page)==0 );
41241 assert( pCache->bPurgeable || createFlag!=1 );
41242 assert( pCache->bPurgeable || pCache->nMin==0 );
41243 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
41244 assert( pCache->nMin==0 || pCache->bPurgeable );
41245 assert( pCache->nHash>0 );
41246 #if PCACHE1_MIGHT_USE_GROUP_MUTEX
41247 if( pCache->pGroup->mutex ){
41248 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
41249 }else
41250 #endif
41251 {
41252 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
41253 }
41254 }
41255
41256
41257 /*
41258 ** Implementation of the sqlite3_pcache.xUnpin method.
@@ -40808,12 +41277,11 @@
41277 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
41278 assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
41279 assert( pPage->isPinned==1 );
41280
41281 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
41282 pcache1RemoveFromHash(pPage, 1);
 
41283 }else{
41284 /* Add the page to the PGroup LRU list. */
41285 if( pGroup->pLruHead ){
41286 pGroup->pLruHead->pLruPrev = pPage;
41287 pPage->pLruNext = pGroup->pLruHead;
@@ -40963,12 +41431,11 @@
41431 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
41432 nFree += sqlite3MemSize(p);
41433 #endif
41434 assert( p->isPinned==0 );
41435 pcache1PinPage(p);
41436 pcache1RemoveFromHash(p, 1);
 
41437 }
41438 pcache1LeaveMutex(&pcache1.grp);
41439 }
41440 return nFree;
41441 }
@@ -42105,13 +42572,13 @@
42572 };
42573
42574 /*
42575 ** Bits of the Pager.doNotSpill flag. See further description below.
42576 */
42577 #define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
42578 #define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
42579 #define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
42580
42581 /*
42582 ** An open page cache is an instance of struct Pager. A description of
42583 ** some of the more important member variables follows:
42584 **
@@ -42189,15 +42656,15 @@
42656 ** comes up during savepoint rollback that requires the pcache module
42657 ** to allocate a new page to prevent the journal file from being written
42658 ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
42659 ** case is a user preference.
42660 **
42661 ** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
42662 ** pagerStress() is permitted, but syncing the journal file is not.
42663 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
42664 ** is larger than the database page-size in order to prevent a journal sync
42665 ** from happening in between the journalling of two pages on the same sector.
42666 **
42667 ** subjInMemory
42668 **
42669 ** This is a boolean variable. If true, then any required sub-journal
42670 ** is opened as an in-memory journal file. If false, then in-memory
@@ -42296,11 +42763,11 @@
42763 u8 changeCountDone; /* Set after incrementing the change-counter */
42764 u8 setMaster; /* True if a m-j name has been written to jrnl */
42765 u8 doNotSpill; /* Do not spill the cache when non-zero */
42766 u8 subjInMemory; /* True to use in-memory sub-journals */
42767 u8 bUseFetch; /* True to use xFetch() */
42768 u8 hasBeenUsed; /* True if any content previously read */
42769 Pgno dbSize; /* Number of pages in the database */
42770 Pgno dbOrigSize; /* dbSize before the current transaction */
42771 Pgno dbFileSize; /* Number of pages in the database file */
42772 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
42773 int errCode; /* One of several kinds of errors */
@@ -42457,11 +42924,11 @@
42924 **
42925 ** instead of
42926 **
42927 ** if( pPager->jfd->pMethods ){ ...
42928 */
42929 #define isOpen(pFd) ((pFd)->pMethods!=0)
42930
42931 /*
42932 ** Return true if this pager uses a write-ahead log instead of the usual
42933 ** rollback journal. Otherwise false.
42934 */
@@ -42680,23 +43147,25 @@
43147 PagerSavepoint *p;
43148 Pgno pgno = pPg->pgno;
43149 int i;
43150 for(i=0; i<pPager->nSavepoint; i++){
43151 p = &pPager->aSavepoint[i];
43152 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
43153 return 1;
43154 }
43155 }
43156 return 0;
43157 }
43158
43159 #ifdef SQLITE_DEBUG
43160 /*
43161 ** Return true if the page is already in the journal file.
43162 */
43163 static int pageInJournal(Pager *pPager, PgHdr *pPg){
43164 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
43165 }
43166 #endif
43167
43168 /*
43169 ** Read a 32-bit integer from the given file descriptor. Store the integer
43170 ** that is read in *pRes. Return SQLITE_OK if everything worked, or an
43171 ** error code is something goes wrong.
@@ -43304,11 +43773,12 @@
43773 */
43774 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
43775 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
43776 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
43777 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
43778 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
43779 iHdrOff+4+nMaster+8)))
43780 ){
43781 return rc;
43782 }
43783 pPager->journalOff += (nMaster+20);
43784
@@ -43864,11 +44334,11 @@
44334 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
44335 return SQLITE_DONE;
44336 }
44337 }
44338
44339 /* If this page has already been played back before during the current
44340 ** rollback, then don't bother to play it back again.
44341 */
44342 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
44343 return rc;
44344 }
@@ -45966,12 +46436,10 @@
46436 return rc;
46437 }
46438
46439 /*
46440 ** Append a record of the current state of page pPg to the sub-journal.
 
 
46441 **
46442 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
46443 ** for all open savepoints before returning.
46444 **
46445 ** This function returns SQLITE_OK if everything is successful, an IO
@@ -46013,10 +46481,17 @@
46481 pPager->nSubRec++;
46482 assert( pPager->nSavepoint>0 );
46483 rc = addToSavepointBitvecs(pPager, pPg->pgno);
46484 }
46485 return rc;
46486 }
46487 static int subjournalPageIfRequired(PgHdr *pPg){
46488 if( subjRequiresPage(pPg) ){
46489 return subjournalPage(pPg);
46490 }else{
46491 return SQLITE_OK;
46492 }
46493 }
46494
46495 /*
46496 ** This function is called by the pcache layer when it has reached some
46497 ** soft memory limit. The first argument is a pointer to a Pager object
@@ -46071,13 +46546,11 @@
46546 }
46547
46548 pPg->pDirty = 0;
46549 if( pagerUseWal(pPager) ){
46550 /* Write a single frame for this page to the log. */
46551 rc = subjournalPageIfRequired(pPg);
 
 
46552 if( rc==SQLITE_OK ){
46553 rc = pagerWalFrames(pPager, pPg, 0, 0);
46554 }
46555 }else{
46556
@@ -46086,43 +46559,10 @@
46559 || pPager->eState==PAGER_WRITER_CACHEMOD
46560 ){
46561 rc = syncJournal(pPager, 1);
46562 }
46563
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46564 /* Write the contents of the page out to the database file. */
46565 if( rc==SQLITE_OK ){
46566 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
46567 rc = pager_write_pagelist(pPager, pPg);
46568 }
@@ -46374,11 +46814,11 @@
46814 ** This branch also runs for files marked as immutable.
46815 */
46816 act_like_temp_file:
46817 tempFile = 1;
46818 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
46819 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
46820 pPager->noLock = 1; /* Do no locking */
46821 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
46822 }
46823
46824 /* The following call to PagerSetPagesize() serves to set the value of
@@ -46393,11 +46833,11 @@
46833 /* Initialize the PCache object. */
46834 if( rc==SQLITE_OK ){
46835 assert( nExtra<1000 );
46836 nExtra = ROUND8(nExtra);
46837 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
46838 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
46839 }
46840
46841 /* If an error occurred above, free the Pager structure and close the file.
46842 */
46843 if( rc!=SQLITE_OK ){
@@ -46780,11 +47220,11 @@
47220 ** see if the database has been modified. If the database has changed,
47221 ** flush the cache. The pPager->hasBeenUsed flag prevents this from
47222 ** occurring on the very first access to a file, in order to save a
47223 ** single unnecessary sqlite3OsRead() call at the start-up.
47224 **
47225 ** Database changes are detected by looking at 15 bytes beginning
47226 ** at offset 24 into the file. The first 4 of these 16 bytes are
47227 ** a 32-bit counter that is incremented with each change. The
47228 ** other bytes change randomly with each file change when
47229 ** a codec is in use.
47230 **
@@ -46988,13 +47428,18 @@
47428 sqlite3_pcache_page *pBase;
47429 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
47430 if( pBase==0 ){
47431 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
47432 if( rc!=SQLITE_OK ) goto pager_acquire_err;
47433 if( pBase==0 ){
47434 pPg = *ppPage = 0;
47435 rc = SQLITE_NOMEM;
47436 goto pager_acquire_err;
47437 }
47438 }
47439 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
47440 assert( pPg!=0 );
47441 }
47442 }
47443
47444 if( rc!=SQLITE_OK ){
47445 /* Either the call to sqlite3PcacheFetch() returned an error or the
@@ -47001,14 +47446,15 @@
47446 ** pager was already in the error-state when this function was called.
47447 ** Set pPg to 0 and jump to the exception handler. */
47448 pPg = 0;
47449 goto pager_acquire_err;
47450 }
47451 assert( pPg==(*ppPage) );
47452 assert( pPg->pgno==pgno );
47453 assert( pPg->pPager==pPager || pPg->pPager==0 );
47454
47455 if( pPg->pPager && !noContent ){
47456 /* In this case the pcache already contains an initialized copy of
47457 ** the page. Return without further ado. */
47458 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
47459 pPager->aStat[PAGER_STAT_HIT]++;
47460 return SQLITE_OK;
@@ -47015,11 +47461,10 @@
47461
47462 }else{
47463 /* The pager cache has created a new page. Its content needs to
47464 ** be initialized. */
47465
 
47466 pPg->pPager = pPager;
47467
47468 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
47469 ** number greater than this, or the unused locking-page, is requested. */
47470 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
@@ -47094,10 +47539,11 @@
47539 assert( pPager!=0 );
47540 assert( pgno!=0 );
47541 assert( pPager->pPCache!=0 );
47542 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
47543 assert( pPage==0 || pPager->hasBeenUsed );
47544 if( pPage==0 ) return 0;
47545 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
47546 }
47547
47548 /*
47549 ** Release a page reference.
@@ -47296,10 +47742,63 @@
47742 }
47743
47744 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
47745 return rc;
47746 }
47747
47748 /*
47749 ** Write page pPg onto the end of the rollback journal.
47750 */
47751 static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
47752 Pager *pPager = pPg->pPager;
47753 int rc;
47754 u32 cksum;
47755 char *pData2;
47756 i64 iOff = pPager->journalOff;
47757
47758 /* We should never write to the journal file the page that
47759 ** contains the database locks. The following assert verifies
47760 ** that we do not. */
47761 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
47762
47763 assert( pPager->journalHdr<=pPager->journalOff );
47764 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
47765 cksum = pager_cksum(pPager, (u8*)pData2);
47766
47767 /* Even if an IO or diskfull error occurs while journalling the
47768 ** page in the block above, set the need-sync flag for the page.
47769 ** Otherwise, when the transaction is rolled back, the logic in
47770 ** playback_one_page() will think that the page needs to be restored
47771 ** in the database file. And if an IO error occurs while doing so,
47772 ** then corruption may follow.
47773 */
47774 pPg->flags |= PGHDR_NEED_SYNC;
47775
47776 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
47777 if( rc!=SQLITE_OK ) return rc;
47778 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
47779 if( rc!=SQLITE_OK ) return rc;
47780 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
47781 if( rc!=SQLITE_OK ) return rc;
47782
47783 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
47784 pPager->journalOff, pPager->pageSize));
47785 PAGER_INCR(sqlite3_pager_writej_count);
47786 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
47787 PAGERID(pPager), pPg->pgno,
47788 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
47789
47790 pPager->journalOff += 8 + pPager->pageSize;
47791 pPager->nRec++;
47792 assert( pPager->pInJournal!=0 );
47793 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
47794 testcase( rc==SQLITE_NOMEM );
47795 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
47796 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
47797 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
47798 return rc;
47799 }
47800
47801 /*
47802 ** Mark a single data page as writeable. The page is written into the
47803 ** main journal or sub-journal as required. If the page is written into
47804 ** one of the journals, the corresponding bit is set in the
@@ -47307,11 +47806,10 @@
47806 ** of any open savepoints as appropriate.
47807 */
47808 static int pager_write(PgHdr *pPg){
47809 Pager *pPager = pPg->pPager;
47810 int rc = SQLITE_OK;
 
47811
47812 /* This routine is not called unless a write-transaction has already
47813 ** been started. The journal file may or may not be open at this point.
47814 ** It is never called in the ERROR state.
47815 */
@@ -47320,11 +47818,10 @@
47818 || pPager->eState==PAGER_WRITER_DBMOD
47819 );
47820 assert( assert_pager_state(pPager) );
47821 assert( pPager->errCode==0 );
47822 assert( pPager->readOnly==0 );
 
47823 CHECK_PAGE(pPg);
47824
47825 /* The journal file needs to be opened. Higher level routines have already
47826 ** obtained the necessary locks to begin the write-transaction, but the
47827 ** rollback journal might not yet be open. Open it now if this is the case.
@@ -47339,95 +47836,52 @@
47836 if( rc!=SQLITE_OK ) return rc;
47837 }
47838 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
47839 assert( assert_pager_state(pPager) );
47840
47841 /* Mark the page that is about to be modified as dirty. */
47842 sqlite3PcacheMakeDirty(pPg);
47843
47844 /* If a rollback journal is in use, them make sure the page that is about
47845 ** to change is in the rollback journal, or if the page is a new page off
47846 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
47847 */
47848 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
47849 if( pPager->pInJournal!=0
47850 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
47851 ){
47852 assert( pagerUseWal(pPager)==0 );
47853 if( pPg->pgno<=pPager->dbOrigSize ){
47854 rc = pagerAddPageToRollbackJournal(pPg);
47855 if( rc!=SQLITE_OK ){
47856 return rc;
47857 }
47858 }else{
47859 if( pPager->eState!=PAGER_WRITER_DBMOD ){
47860 pPg->flags |= PGHDR_NEED_SYNC;
47861 }
47862 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
47863 PAGERID(pPager), pPg->pgno,
47864 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
47865 }
47866 }
47867
47868 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
47869 ** and before writing the page into the rollback journal. Wait until now,
47870 ** after the page has been successfully journalled, before setting the
47871 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
47872 */
47873 pPg->flags |= PGHDR_WRITEABLE;
47874
47875 /* If the statement journal is open and the page is not in it,
47876 ** then write the page into the statement journal.
47877 */
47878 if( pPager->nSavepoint>0 ){
47879 rc = subjournalPageIfRequired(pPg);
47880 }
47881
47882 /* Update the database size and return. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47883 if( pPager->dbSize<pPg->pgno ){
47884 pPager->dbSize = pPg->pgno;
47885 }
47886 return rc;
47887 }
@@ -47438,21 +47892,21 @@
47892 ** all bytes of a sector are written together by hardware. Hence, all bytes of
47893 ** a sector need to be journalled in case of a power loss in the middle of
47894 ** a write.
47895 **
47896 ** Usually, the sector size is less than or equal to the page size, in which
47897 ** case pages can be individually written. This routine only runs in the
47898 ** exceptional case where the page size is smaller than the sector size.
47899 */
47900 static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
47901 int rc = SQLITE_OK; /* Return code */
47902 Pgno nPageCount; /* Total number of pages in database file */
47903 Pgno pg1; /* First page of the sector pPg is located on. */
47904 int nPage = 0; /* Number of pages starting at pg1 to journal */
47905 int ii; /* Loop counter */
47906 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
47907 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
47908 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
47909
47910 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
47911 ** a journal header to be written between the pages journaled by
47912 ** this function.
@@ -47536,15 +47990,19 @@
47990 **
47991 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
47992 ** as appropriate. Otherwise, SQLITE_OK.
47993 */
47994 SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
47995 Pager *pPager = pPg->pPager;
47996 assert( (pPg->flags & PGHDR_MMAP)==0 );
47997 assert( pPager->eState>=PAGER_WRITER_LOCKED );
47998 assert( pPager->eState!=PAGER_ERROR );
47999 assert( assert_pager_state(pPager) );
48000 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
48001 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
48002 return SQLITE_OK;
48003 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
48004 return pagerWriteLargeSector(pPg);
48005 }else{
48006 return pager_write(pPg);
48007 }
48008 }
@@ -47554,11 +48012,11 @@
48012 ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
48013 ** to change the content of the page.
48014 */
48015 #ifndef NDEBUG
48016 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
48017 return pPg->flags & PGHDR_WRITEABLE;
48018 }
48019 #endif
48020
48021 /*
48022 ** A call to this routine tells the pager that it is not necessary to
@@ -47578,10 +48036,11 @@
48036 Pager *pPager = pPg->pPager;
48037 if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
48038 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
48039 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
48040 pPg->flags |= PGHDR_DONT_WRITE;
48041 pPg->flags &= ~PGHDR_WRITEABLE;
48042 pager_set_pagehash(pPg);
48043 }
48044 }
48045
48046 /*
@@ -48132,58 +48591,66 @@
48591 **
48592 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
48593 ** occurs while opening the sub-journal file, then an IO error code is
48594 ** returned. Otherwise, SQLITE_OK.
48595 */
48596 static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
48597 int rc = SQLITE_OK; /* Return code */
48598 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
48599 int ii; /* Iterator variable */
48600 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
48601
48602 assert( pPager->eState>=PAGER_WRITER_LOCKED );
48603 assert( assert_pager_state(pPager) );
48604 assert( nSavepoint>nCurrent && pPager->useJournal );
48605
48606 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
48607 ** if the allocation fails. Otherwise, zero the new portion in case a
48608 ** malloc failure occurs while populating it in the for(...) loop below.
48609 */
48610 aNew = (PagerSavepoint *)sqlite3Realloc(
48611 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
48612 );
48613 if( !aNew ){
48614 return SQLITE_NOMEM;
48615 }
48616 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
48617 pPager->aSavepoint = aNew;
48618
48619 /* Populate the PagerSavepoint structures just allocated. */
48620 for(ii=nCurrent; ii<nSavepoint; ii++){
48621 aNew[ii].nOrig = pPager->dbSize;
48622 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
48623 aNew[ii].iOffset = pPager->journalOff;
48624 }else{
48625 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
48626 }
48627 aNew[ii].iSubRec = pPager->nSubRec;
48628 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
48629 if( !aNew[ii].pInSavepoint ){
48630 return SQLITE_NOMEM;
48631 }
48632 if( pagerUseWal(pPager) ){
48633 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
48634 }
48635 pPager->nSavepoint = ii+1;
48636 }
48637 assert( pPager->nSavepoint==nSavepoint );
48638 assertTruncateConstraint(pPager);
48639 return rc;
48640 }
48641 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
48642 assert( pPager->eState>=PAGER_WRITER_LOCKED );
48643 assert( assert_pager_state(pPager) );
48644
48645 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
48646 return pagerOpenSavepoint(pPager, nSavepoint);
48647 }else{
48648 return SQLITE_OK;
48649 }
48650 }
48651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48652
48653 /*
48654 ** This function is called to rollback or release (commit) a savepoint.
48655 ** The savepoint to release or rollback need not be the most recently
48656 ** created savepoint.
@@ -48410,13 +48877,12 @@
48877 **
48878 ** subjournalPage() may need to allocate space to store pPg->pgno into
48879 ** one or more savepoint bitvecs. This is the reason this function
48880 ** may return SQLITE_NOMEM.
48881 */
48882 if( (pPg->flags & PGHDR_DIRTY)!=0
48883 && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
 
48884 ){
48885 return rc;
48886 }
48887
48888 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
@@ -52330,10 +52796,11 @@
52796 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
52797
52798 /* Forward declarations */
52799 typedef struct MemPage MemPage;
52800 typedef struct BtLock BtLock;
52801 typedef struct CellInfo CellInfo;
52802
52803 /*
52804 ** This is a magic string that appears at the beginning of every
52805 ** SQLite database in order to identify the file as a real database.
52806 **
@@ -52393,11 +52860,14 @@
52860 u8 *apOvfl[5]; /* Pointers to the body of overflow cells */
52861 BtShared *pBt; /* Pointer to BtShared that this page is part of */
52862 u8 *aData; /* Pointer to disk image of the page data */
52863 u8 *aDataEnd; /* One byte past the end of usable data */
52864 u8 *aCellIdx; /* The cell index area */
52865 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
52866 DbPage *pDbPage; /* Pager page handle */
52867 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
52868 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
52869 Pgno pgno; /* Page number for this page */
52870 };
52871
52872 /*
52873 ** The in-memory image of a disk page has the auxiliary information appended
@@ -52449,10 +52919,11 @@
52919 sqlite3 *db; /* The database connection holding this btree */
52920 BtShared *pBt; /* Sharable content of this btree */
52921 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
52922 u8 sharable; /* True if we can share pBt with another db */
52923 u8 locked; /* True if db currently has pBt locked */
52924 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
52925 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
52926 int nBackup; /* Number of backup operations reading this btree */
52927 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
52928 Btree *pNext; /* List of other sharable Btrees from the same db */
52929 Btree *pPrev; /* Back pointer of the same list */
@@ -52559,11 +53030,10 @@
53030 /*
53031 ** An instance of the following structure is used to hold information
53032 ** about a cell. The parseCellPtr() function fills in this structure
53033 ** based on information extract from the raw disk page.
53034 */
 
53035 struct CellInfo {
53036 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
53037 u8 *pPayload; /* Pointer to the start of payload */
53038 u32 nPayload; /* Bytes of payload */
53039 u16 nLocal; /* Amount of payload held locally, not on overflow */
@@ -52602,24 +53072,30 @@
53072 ** eState==FAULT: Cursor fault with skipNext as error code.
53073 */
53074 struct BtCursor {
53075 Btree *pBtree; /* The Btree to which this cursor belongs */
53076 BtShared *pBt; /* The BtShared this cursor points to */
53077 BtCursor *pNext; /* Forms a linked list of all cursors */
 
53078 Pgno *aOverflow; /* Cache of overflow page locations */
53079 CellInfo info; /* A parse of the cell we are pointing at */
53080 i64 nKey; /* Size of pKey, or last integer key */
53081 void *pKey; /* Saved key that was cursor last known position */
53082 Pgno pgnoRoot; /* The root page of this tree */
53083 int nOvflAlloc; /* Allocated size of aOverflow[] array */
53084 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
53085 ** Error code if eState==CURSOR_FAULT */
53086 u8 curFlags; /* zero or more BTCF_* flags defined below */
53087 u8 curPagerFlags; /* Flags to send to sqlite3PagerAcquire() */
53088 u8 eState; /* One of the CURSOR_XXX constants (see below) */
53089 u8 hints; /* As configured by CursorSetHints() */
53090 /* All fields above are zeroed when the cursor is allocated. See
53091 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
53092 ** initialized. */
53093 i8 iPage; /* Index of current page in apPage */
53094 u8 curIntKey; /* Value of apPage[0]->intKey */
53095 struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
53096 void *padding1; /* Make object size a multiple of 16 */
53097 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */
53098 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */
53099 };
53100
53101 /*
@@ -52628,10 +53104,11 @@
53104 #define BTCF_WriteFlag 0x01 /* True if a write cursor */
53105 #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
53106 #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
53107 #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */
53108 #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
53109 #define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */
53110
53111 /*
53112 ** Potential values for BtCursor.eState.
53113 **
53114 ** CURSOR_INVALID:
@@ -52780,10 +53257,23 @@
53257 #define get2byte(x) ((x)[0]<<8 | (x)[1])
53258 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
53259 #define get4byte sqlite3Get4byte
53260 #define put4byte sqlite3Put4byte
53261
53262 /*
53263 ** get2byteAligned(), unlike get2byte(), requires that its argument point to a
53264 ** two-byte aligned address. get2bytea() is only used for accessing the
53265 ** cell addresses in a btree header.
53266 */
53267 #if SQLITE_BYTEORDER==4321
53268 # define get2byteAligned(x) (*(u16*)(x))
53269 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
53270 # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
53271 #else
53272 # define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
53273 #endif
53274
53275 /************** End of btreeInt.h ********************************************/
53276 /************** Continuing where we left off in btmutex.c ********************/
53277 #ifndef SQLITE_OMIT_SHARED_CACHE
53278 #if SQLITE_THREADSAFE
53279
@@ -53559,17 +54049,19 @@
54049 Btree *pBtree, /* The database file to check */
54050 i64 iRow, /* The rowid that might be changing */
54051 int isClearTable /* True if all rows are being deleted */
54052 ){
54053 BtCursor *p;
54054 if( pBtree->hasIncrblobCur==0 ) return;
54055 assert( sqlite3BtreeHoldsMutex(pBtree) );
54056 pBtree->hasIncrblobCur = 0;
54057 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
54058 if( (p->curFlags & BTCF_Incrblob)!=0 ){
54059 pBtree->hasIncrblobCur = 1;
54060 if( isClearTable || p->info.nKey==iRow ){
54061 p->eState = CURSOR_INVALID;
54062 }
54063 }
54064 }
54065 }
54066
54067 #else
@@ -53687,11 +54179,11 @@
54179 ** stores the integer key in pCur->nKey. In this case this value is
54180 ** all that is required. Otherwise, if pCur is not open on an intKey
54181 ** table, then malloc space for and store the pCur->nKey bytes of key
54182 ** data.
54183 */
54184 if( 0==pCur->curIntKey ){
54185 void *pKey = sqlite3Malloc( pCur->nKey );
54186 if( pKey ){
54187 rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
54188 if( rc==SQLITE_OK ){
54189 pCur->pKey = pKey;
@@ -53700,11 +54192,11 @@
54192 }
54193 }else{
54194 rc = SQLITE_NOMEM;
54195 }
54196 }
54197 assert( !pCur->curIntKey || !pCur->pKey );
54198
54199 if( rc==SQLITE_OK ){
54200 btreeReleaseAllCursorPages(pCur);
54201 pCur->eState = CURSOR_REQUIRESEEK;
54202 }
@@ -53721,10 +54213,19 @@
54213 ** the table with root-page iRoot. "Saving the cursor position" means that
54214 ** the location in the btree is remembered in such a way that it can be
54215 ** moved back to the same spot after the btree has been modified. This
54216 ** routine is called just before cursor pExcept is used to modify the
54217 ** table, for example in BtreeDelete() or BtreeInsert().
54218 **
54219 ** If there are two or more cursors on the same btree, then all such
54220 ** cursors should have their BTCF_Multiple flag set. The btreeCursor()
54221 ** routine enforces that rule. This routine only needs to be called in
54222 ** the uncommon case when pExpect has the BTCF_Multiple flag set.
54223 **
54224 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
54225 ** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
54226 ** pointless call to this routine.
54227 **
54228 ** Implementation note: This routine merely checks to see if any cursors
54229 ** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
54230 ** event that cursors are in need to being saved.
54231 */
@@ -53733,11 +54234,13 @@
54234 assert( sqlite3_mutex_held(pBt->mutex) );
54235 assert( pExcept==0 || pExcept->pBt==pBt );
54236 for(p=pBt->pCursor; p; p=p->pNext){
54237 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
54238 }
54239 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
54240 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
54241 return SQLITE_OK;
54242 }
54243
54244 /* This helper routine to saveAllCursors does the actual work of saving
54245 ** the cursors if and when a cursor is found that actually requires saving.
54246 ** The common case is that no cursors need to be saved, so this routine is
@@ -54020,71 +54523,184 @@
54523
54524 /*
54525 ** Given a btree page and a cell index (0 means the first cell on
54526 ** the page, 1 means the second cell, and so forth) return a pointer
54527 ** to the cell content.
54528 **
54529 ** findCellPastPtr() does the same except it skips past the initial
54530 ** 4-byte child pointer found on interior pages, if there is one.
54531 **
54532 ** This routine works only for pages that do not contain overflow cells.
54533 */
54534 #define findCell(P,I) \
54535 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
54536 #define findCellPastPtr(P,I) \
54537 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
54538
54539
54540 /*
54541 ** This is common tail processing for btreeParseCellPtr() and
54542 ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
54543 ** on a single B-tree page. Make necessary adjustments to the CellInfo
54544 ** structure.
54545 */
54546 static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
54547 MemPage *pPage, /* Page containing the cell */
54548 u8 *pCell, /* Pointer to the cell text. */
54549 CellInfo *pInfo /* Fill in this structure */
54550 ){
54551 /* If the payload will not fit completely on the local page, we have
54552 ** to decide how much to store locally and how much to spill onto
54553 ** overflow pages. The strategy is to minimize the amount of unused
54554 ** space on overflow pages while keeping the amount of local storage
54555 ** in between minLocal and maxLocal.
54556 **
54557 ** Warning: changing the way overflow payload is distributed in any
54558 ** way will result in an incompatible file format.
54559 */
54560 int minLocal; /* Minimum amount of payload held locally */
54561 int maxLocal; /* Maximum amount of payload held locally */
54562 int surplus; /* Overflow payload available for local storage */
54563
54564 minLocal = pPage->minLocal;
54565 maxLocal = pPage->maxLocal;
54566 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
54567 testcase( surplus==maxLocal );
54568 testcase( surplus==maxLocal+1 );
54569 if( surplus <= maxLocal ){
54570 pInfo->nLocal = (u16)surplus;
54571 }else{
54572 pInfo->nLocal = (u16)minLocal;
54573 }
54574 pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
54575 pInfo->nSize = pInfo->iOverflow + 4;
54576 }
54577
54578 /*
54579 ** The following routines are implementations of the MemPage.xParseCell()
54580 ** method.
54581 **
54582 ** Parse a cell content block and fill in the CellInfo structure.
54583 **
54584 ** btreeParseCellPtr() => table btree leaf nodes
54585 ** btreeParseCellNoPayload() => table btree internal nodes
54586 ** btreeParseCellPtrIndex() => index btree nodes
54587 **
54588 ** There is also a wrapper function btreeParseCell() that works for
54589 ** all MemPage types and that references the cell by index rather than
54590 ** by pointer.
54591 */
54592 static void btreeParseCellPtrNoPayload(
54593 MemPage *pPage, /* Page containing the cell */
54594 u8 *pCell, /* Pointer to the cell text. */
54595 CellInfo *pInfo /* Fill in this structure */
54596 ){
54597 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54598 assert( pPage->leaf==0 );
54599 assert( pPage->noPayload );
54600 assert( pPage->childPtrSize==4 );
54601 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
54602 pInfo->nPayload = 0;
54603 pInfo->nLocal = 0;
54604 pInfo->iOverflow = 0;
54605 pInfo->pPayload = 0;
54606 return;
54607 }
 
 
 
 
 
 
 
 
 
54608 static void btreeParseCellPtr(
54609 MemPage *pPage, /* Page containing the cell */
54610 u8 *pCell, /* Pointer to the cell text. */
54611 CellInfo *pInfo /* Fill in this structure */
54612 ){
54613 u8 *pIter; /* For scanning through pCell */
54614 u32 nPayload; /* Number of bytes of cell payload */
54615 u64 iKey; /* Extracted Key value */
54616
54617 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54618 assert( pPage->leaf==0 || pPage->leaf==1 );
54619 assert( pPage->intKeyLeaf || pPage->noPayload );
54620 assert( pPage->noPayload==0 );
54621 assert( pPage->intKeyLeaf );
54622 assert( pPage->childPtrSize==0 );
54623 pIter = pCell;
54624
54625 /* The next block of code is equivalent to:
54626 **
54627 ** pIter += getVarint32(pIter, nPayload);
54628 **
54629 ** The code is inlined to avoid a function call.
54630 */
54631 nPayload = *pIter;
54632 if( nPayload>=0x80 ){
54633 u8 *pEnd = &pIter[8];
54634 nPayload &= 0x7f;
54635 do{
54636 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
54637 }while( (*pIter)>=0x80 && pIter<pEnd );
54638 }
54639 pIter++;
54640
54641 /* The next block of code is equivalent to:
54642 **
54643 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
54644 **
54645 ** The code is inlined to avoid a function call.
54646 */
54647 iKey = *pIter;
54648 if( iKey>=0x80 ){
54649 u8 *pEnd = &pIter[7];
54650 iKey &= 0x7f;
54651 while(1){
54652 iKey = (iKey<<7) | (*++pIter & 0x7f);
54653 if( (*pIter)<0x80 ) break;
54654 if( pIter>=pEnd ){
54655 iKey = (iKey<<8) | *++pIter;
54656 break;
54657 }
54658 }
54659 }
54660 pIter++;
54661
54662 pInfo->nKey = *(i64*)&iKey;
54663 pInfo->nPayload = nPayload;
54664 pInfo->pPayload = pIter;
54665 testcase( nPayload==pPage->maxLocal );
54666 testcase( nPayload==pPage->maxLocal+1 );
54667 if( nPayload<=pPage->maxLocal ){
54668 /* This is the (easy) common case where the entire payload fits
54669 ** on the local page. No overflow is required.
54670 */
54671 pInfo->nSize = nPayload + (u16)(pIter - pCell);
54672 if( pInfo->nSize<4 ) pInfo->nSize = 4;
54673 pInfo->nLocal = (u16)nPayload;
54674 pInfo->iOverflow = 0;
54675 }else{
54676 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
54677 }
54678 }
54679 static void btreeParseCellPtrIndex(
54680 MemPage *pPage, /* Page containing the cell */
54681 u8 *pCell, /* Pointer to the cell text. */
54682 CellInfo *pInfo /* Fill in this structure */
54683 ){
54684 u8 *pIter; /* For scanning through pCell */
54685 u32 nPayload; /* Number of bytes of cell payload */
54686
54687 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54688 assert( pPage->leaf==0 || pPage->leaf==1 );
54689 assert( pPage->intKeyLeaf==0 );
54690 assert( pPage->noPayload==0 );
54691 pIter = pCell + pPage->childPtrSize;
54692 nPayload = *pIter;
54693 if( nPayload>=0x80 ){
54694 u8 *pEnd = &pIter[8];
54695 nPayload &= 0x7f;
54696 do{
54697 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
54698 }while( *(pIter)>=0x80 && pIter<pEnd );
54699 }
54700 pIter++;
54701 pInfo->nKey = nPayload;
 
 
 
 
54702 pInfo->nPayload = nPayload;
54703 pInfo->pPayload = pIter;
54704 testcase( nPayload==pPage->maxLocal );
54705 testcase( nPayload==pPage->maxLocal+1 );
54706 if( nPayload<=pPage->maxLocal ){
@@ -54094,50 +54710,32 @@
54710 pInfo->nSize = nPayload + (u16)(pIter - pCell);
54711 if( pInfo->nSize<4 ) pInfo->nSize = 4;
54712 pInfo->nLocal = (u16)nPayload;
54713 pInfo->iOverflow = 0;
54714 }else{
54715 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54716 }
54717 }
54718 static void btreeParseCell(
54719 MemPage *pPage, /* Page containing the cell */
54720 int iCell, /* The cell index. First cell is 0 */
54721 CellInfo *pInfo /* Fill in this structure */
54722 ){
54723 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
54724 }
54725
54726 /*
54727 ** The following routines are implementations of the MemPage.xCellSize
54728 ** method.
54729 **
54730 ** Compute the total number of bytes that a Cell needs in the cell
54731 ** data area of the btree-page. The return number includes the cell
54732 ** data header and the local payload, but not any overflow page or
54733 ** the space used by the cell pointer.
54734 **
54735 ** cellSizePtrNoPayload() => table internal nodes
54736 ** cellSizePtr() => all index nodes & table leaf nodes
54737 */
54738 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
54739 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
54740 u8 *pEnd; /* End mark for a varint */
54741 u32 nSize; /* Size value to return */
@@ -54146,22 +54744,17 @@
54744 /* The value returned by this function should always be the same as
54745 ** the (CellInfo.nSize) value found by doing a full parse of the
54746 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
54747 ** this function verifies that this invariant is not violated. */
54748 CellInfo debuginfo;
54749 pPage->xParseCell(pPage, pCell, &debuginfo);
54750 #endif
54751
54752 assert( pPage->noPayload==0 );
 
 
 
 
 
54753 nSize = *pIter;
54754 if( nSize>=0x80 ){
54755 pEnd = &pIter[8];
54756 nSize &= 0x7f;
54757 do{
54758 nSize = (nSize<<7) | (*++pIter & 0x7f);
54759 }while( *(pIter)>=0x80 && pIter<pEnd );
54760 }
@@ -54189,16 +54782,36 @@
54782 nSize += 4 + (u16)(pIter - pCell);
54783 }
54784 assert( nSize==debuginfo.nSize || CORRUPT_DB );
54785 return (u16)nSize;
54786 }
54787 static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
54788 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
54789 u8 *pEnd; /* End mark for a varint */
54790
54791 #ifdef SQLITE_DEBUG
54792 /* The value returned by this function should always be the same as
54793 ** the (CellInfo.nSize) value found by doing a full parse of the
54794 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
54795 ** this function verifies that this invariant is not violated. */
54796 CellInfo debuginfo;
54797 pPage->xParseCell(pPage, pCell, &debuginfo);
54798 #endif
54799
54800 assert( pPage->childPtrSize==4 );
54801 pEnd = pIter + 9;
54802 while( (*pIter++)&0x80 && pIter<pEnd );
54803 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
54804 return (u16)(pIter - pCell);
54805 }
54806
54807
54808 #ifdef SQLITE_DEBUG
54809 /* This variation on cellSizePtr() is used inside of assert() statements
54810 ** only. */
54811 static u16 cellSize(MemPage *pPage, int iCell){
54812 return pPage->xCellSize(pPage, findCell(pPage, iCell));
54813 }
54814 #endif
54815
54816 #ifndef SQLITE_OMIT_AUTOVACUUM
54817 /*
@@ -54208,11 +54821,11 @@
54821 */
54822 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
54823 CellInfo info;
54824 if( *pRC ) return;
54825 assert( pCell!=0 );
54826 pPage->xParseCell(pPage, pCell, &info);
54827 if( info.iOverflow ){
54828 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
54829 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
54830 }
54831 }
@@ -54272,11 +54885,11 @@
54885 */
54886 if( pc<iCellFirst || pc>iCellLast ){
54887 return SQLITE_CORRUPT_BKPT;
54888 }
54889 assert( pc>=iCellFirst && pc<=iCellLast );
54890 size = pPage->xCellSize(pPage, &src[pc]);
54891 cbrk -= size;
54892 if( cbrk<iCellFirst || pc+size>usableSize ){
54893 return SQLITE_CORRUPT_BKPT;
54894 }
54895 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
@@ -54314,22 +54927,24 @@
54927 ** If no suitable space can be found on the free-list, return NULL.
54928 **
54929 ** This function may detect corruption within pPg. If corruption is
54930 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
54931 **
54932 ** Slots on the free list that are between 1 and 3 bytes larger than nByte
54933 ** will be ignored if adding the extra space to the fragmentation count
54934 ** causes the fragmentation count to exceed 60.
54935 */
54936 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
54937 const int hdr = pPg->hdrOffset;
54938 u8 * const aData = pPg->aData;
54939 int iAddr = hdr + 1;
54940 int pc = get2byte(&aData[iAddr]);
54941 int x;
54942 int usableSize = pPg->pBt->usableSize;
54943
54944 assert( pc>0 );
54945 do{
54946 int size; /* Size of the free slot */
54947 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
54948 ** increasing offset. */
54949 if( pc>usableSize-4 || pc<iAddr+4 ){
54950 *pRc = SQLITE_CORRUPT_BKPT;
@@ -54337,36 +54952,35 @@
54952 }
54953 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
54954 ** freeblock form a big-endian integer which is the size of the freeblock
54955 ** in bytes, including the 4-byte header. */
54956 size = get2byte(&aData[pc+2]);
54957 if( (x = size - nByte)>=0 ){
 
54958 testcase( x==4 );
54959 testcase( x==3 );
54960 if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
54961 *pRc = SQLITE_CORRUPT_BKPT;
54962 return 0;
54963 }else if( x<4 ){
54964 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
54965 ** number of bytes in fragments may not exceed 60. */
54966 if( aData[hdr+7]>57 ) return 0;
54967
 
 
54968 /* Remove the slot from the free-list. Update the number of
54969 ** fragmented bytes within the page. */
54970 memcpy(&aData[iAddr], &aData[pc], 2);
54971 aData[hdr+7] += (u8)x;
 
 
 
54972 }else{
54973 /* The slot remains on the free-list. Reduce its size to account
54974 ** for the portion used by the new allocation. */
54975 put2byte(&aData[pc+2], x);
54976 }
54977 return &aData[pc + x];
54978 }
54979 iAddr = pc;
54980 pc = get2byte(&aData[pc]);
54981 }while( pc );
54982
54983 return 0;
54984 }
54985
54986 /*
@@ -54403,42 +55017,43 @@
55017 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
55018 ** and the reserved space is zero (the usual value for reserved space)
55019 ** then the cell content offset of an empty page wants to be 65536.
55020 ** However, that integer is too large to be stored in a 2-byte unsigned
55021 ** integer, so a value of 0 is used in its place. */
55022 top = get2byte(&data[hdr+5]);
55023 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
55024 if( gap>top ){
55025 if( top==0 && pPage->pBt->usableSize==65536 ){
55026 top = 65536;
55027 }else{
55028 return SQLITE_CORRUPT_BKPT;
55029 }
55030 }
55031
55032 /* If there is enough space between gap and top for one more cell pointer
55033 ** array entry offset, and if the freelist is not empty, then search the
55034 ** freelist looking for a free slot big enough to satisfy the request.
55035 */
55036 testcase( gap+2==top );
55037 testcase( gap+1==top );
55038 testcase( gap==top );
55039 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
55040 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
 
 
 
55041 if( pSpace ){
55042 assert( pSpace>=data && (pSpace - data)<65536 );
55043 *pIdx = (int)(pSpace - data);
55044 return SQLITE_OK;
55045 }else if( rc ){
55046 return rc;
55047 }
55048 }
55049
55050 /* The request could not be fulfilled using a freelist slot. Check
55051 ** to see if defragmentation is necessary.
55052 */
55053 testcase( gap+2+nByte==top );
55054 if( gap+2+nByte>top ){
 
55055 assert( pPage->nCell>0 || CORRUPT_DB );
55056 rc = defragmentPage(pPage);
55057 if( rc ) return rc;
55058 top = get2byteNotZero(&data[hdr+5]);
55059 assert( gap+nByte<=top );
@@ -54510,18 +55125,19 @@
55125 if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
55126 assert( iFreeBlk>iPtr || iFreeBlk==0 );
55127
55128 /* At this point:
55129 ** iFreeBlk: First freeblock after iStart, or zero if none
55130 ** iPtr: The address of a pointer to iFreeBlk
55131 **
55132 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
55133 */
55134 if( iFreeBlk && iEnd+3>=iFreeBlk ){
55135 nFrag = iFreeBlk - iEnd;
55136 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
55137 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
55138 if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT;
55139 iSize = iEnd - iStart;
55140 iFreeBlk = get2byte(&data[iFreeBlk]);
55141 }
55142
55143 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
@@ -54575,21 +55191,30 @@
55191 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
55192 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55193 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
55194 flagByte &= ~PTF_LEAF;
55195 pPage->childPtrSize = 4-4*pPage->leaf;
55196 pPage->xCellSize = cellSizePtr;
55197 pBt = pPage->pBt;
55198 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
55199 /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
55200 ** table b-tree page. */
55201 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
55202 /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
55203 ** table b-tree page. */
55204 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
55205 pPage->intKey = 1;
55206 if( pPage->leaf ){
55207 pPage->intKeyLeaf = 1;
55208 pPage->noPayload = 0;
55209 pPage->xParseCell = btreeParseCellPtr;
55210 }else{
55211 pPage->intKeyLeaf = 0;
55212 pPage->noPayload = 1;
55213 pPage->xCellSize = cellSizePtrNoPayload;
55214 pPage->xParseCell = btreeParseCellPtrNoPayload;
55215 }
55216 pPage->maxLocal = pBt->maxLeaf;
55217 pPage->minLocal = pBt->minLeaf;
55218 }else if( flagByte==PTF_ZERODATA ){
55219 /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
55220 ** index b-tree page. */
@@ -54598,10 +55223,11 @@
55223 ** index b-tree page. */
55224 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
55225 pPage->intKey = 0;
55226 pPage->intKeyLeaf = 0;
55227 pPage->noPayload = 0;
55228 pPage->xParseCell = btreeParseCellPtrIndex;
55229 pPage->maxLocal = pBt->maxLocal;
55230 pPage->minLocal = pBt->minLocal;
55231 }else{
55232 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
55233 ** an error. */
@@ -54653,10 +55279,11 @@
55279 pPage->nOverflow = 0;
55280 usableSize = pBt->usableSize;
55281 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
55282 pPage->aDataEnd = &data[usableSize];
55283 pPage->aCellIdx = &data[cellOffset];
55284 pPage->aDataOfst = &data[pPage->childPtrSize];
55285 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
55286 ** the start of the cell content area. A zero value for this integer is
55287 ** interpreted as 65536. */
55288 top = get2byteNotZero(&data[hdr+5]);
55289 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
@@ -54686,17 +55313,17 @@
55313 int i; /* Index into the cell pointer array */
55314 int sz; /* Size of a cell */
55315
55316 if( !pPage->leaf ) iCellLast--;
55317 for(i=0; i<pPage->nCell; i++){
55318 pc = get2byteAligned(&data[cellOffset+i*2]);
55319 testcase( pc==iCellFirst );
55320 testcase( pc==iCellLast );
55321 if( pc<iCellFirst || pc>iCellLast ){
55322 return SQLITE_CORRUPT_BKPT;
55323 }
55324 sz = pPage->xCellSize(pPage, &data[pc]);
55325 testcase( pc+sz==usableSize );
55326 if( pc+sz>usableSize ){
55327 return SQLITE_CORRUPT_BKPT;
55328 }
55329 }
@@ -54772,10 +55399,11 @@
55399 pPage->nFree = (u16)(pBt->usableSize - first);
55400 decodeFlags(pPage, flags);
55401 pPage->cellOffset = first;
55402 pPage->aDataEnd = &data[pBt->usableSize];
55403 pPage->aCellIdx = &data[first];
55404 pPage->aDataOfst = &data[pPage->childPtrSize];
55405 pPage->nOverflow = 0;
55406 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
55407 pPage->maskPage = (u16)(pBt->pageSize - 1);
55408 pPage->nCell = 0;
55409 pPage->isInit = 1;
@@ -54790,11 +55418,11 @@
55418 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
55419 pPage->aData = sqlite3PagerGetData(pDbPage);
55420 pPage->pDbPage = pDbPage;
55421 pPage->pBt = pBt;
55422 pPage->pgno = pgno;
55423 pPage->hdrOffset = pgno==1 ? 100 : 0;
55424 return pPage;
55425 }
55426
55427 /*
55428 ** Get a page from the pager. Initialize the MemPage.pBt and
@@ -54851,58 +55479,86 @@
55479 assert( ((p->pBt->nPage)&0x8000000)==0 );
55480 return btreePagecount(p->pBt);
55481 }
55482
55483 /*
55484 ** Get a page from the pager and initialize it.
 
 
55485 **
55486 ** If pCur!=0 then the page is being fetched as part of a moveToChild()
55487 ** call. Do additional sanity checking on the page in this case.
55488 ** And if the fetch fails, this routine must decrement pCur->iPage.
55489 **
55490 ** The page is fetched as read-write unless pCur is not NULL and is
55491 ** a read-only cursor.
55492 **
55493 ** If an error occurs, then *ppPage is undefined. It
55494 ** may remain unchanged, or it may be set to an invalid value.
55495 */
55496 static int getAndInitPage(
55497 BtShared *pBt, /* The database file */
55498 Pgno pgno, /* Number of the page to get */
55499 MemPage **ppPage, /* Write the page pointer here */
55500 BtCursor *pCur, /* Cursor to receive the page, or NULL */
55501 int bReadOnly /* True for a read-only page */
55502 ){
55503 int rc;
55504 DbPage *pDbPage;
55505 assert( sqlite3_mutex_held(pBt->mutex) );
55506 assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] );
55507 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
55508 assert( pCur==0 || pCur->iPage>0 );
55509
55510 if( pgno>btreePagecount(pBt) ){
55511 rc = SQLITE_CORRUPT_BKPT;
55512 goto getAndInitPage_error;
55513 }
55514 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
55515 if( rc ){
55516 goto getAndInitPage_error;
55517 }
55518 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
55519 if( (*ppPage)->isInit==0 ){
55520 rc = btreeInitPage(*ppPage);
55521 if( rc!=SQLITE_OK ){
55522 releasePage(*ppPage);
55523 goto getAndInitPage_error;
55524 }
55525 }
55526
55527 /* If obtaining a child page for a cursor, we must verify that the page is
55528 ** compatible with the root page. */
55529 if( pCur
55530 && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey)
55531 ){
55532 rc = SQLITE_CORRUPT_BKPT;
55533 releasePage(*ppPage);
55534 goto getAndInitPage_error;
55535 }
55536 return SQLITE_OK;
55537
55538 getAndInitPage_error:
55539 if( pCur ) pCur->iPage--;
55540 testcase( pgno==0 );
55541 assert( pgno!=0 || rc==SQLITE_CORRUPT );
55542 return rc;
55543 }
55544
55545 /*
55546 ** Release a MemPage. This should be called once for each prior
55547 ** call to btreeGetPage.
55548 */
55549 static void releasePageNotNull(MemPage *pPage){
55550 assert( pPage->aData );
55551 assert( pPage->pBt );
55552 assert( pPage->pDbPage!=0 );
55553 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
55554 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
55555 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55556 sqlite3PagerUnrefNotNull(pPage->pDbPage);
55557 }
55558 static void releasePage(MemPage *pPage){
55559 if( pPage ) releasePageNotNull(pPage);
 
 
 
 
 
 
 
 
55560 }
55561
55562 /*
55563 ** Get an unused page.
55564 **
@@ -55873,11 +56529,11 @@
56529 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
56530 MemPage *pPage1 = pBt->pPage1;
56531 assert( pPage1->aData );
56532 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
56533 pBt->pPage1 = 0;
56534 releasePageNotNull(pPage1);
56535 }
56536 }
56537
56538 /*
56539 ** If pBt points to an empty file then convert that empty file
@@ -56188,11 +56844,11 @@
56844
56845 for(i=0; i<nCell; i++){
56846 u8 *pCell = findCell(pPage, i);
56847 if( eType==PTRMAP_OVERFLOW1 ){
56848 CellInfo info;
56849 pPage->xParseCell(pPage, pCell, &info);
56850 if( info.iOverflow
56851 && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
56852 && iFrom==get4byte(&pCell[info.iOverflow])
56853 ){
56854 put4byte(&pCell[info.iOverflow], iTo);
@@ -56929,10 +57585,11 @@
57585 int wrFlag, /* 1 to write. 0 read-only */
57586 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
57587 BtCursor *pCur /* Space for new cursor */
57588 ){
57589 BtShared *pBt = p->pBt; /* Shared b-tree handle */
57590 BtCursor *pX; /* Looping over other all cursors */
57591
57592 assert( sqlite3BtreeHoldsMutex(p) );
57593 assert( wrFlag==0 || wrFlag==1 );
57594
57595 /* The following assert statements verify that if this is a sharable
@@ -56944,14 +57601,12 @@
57601
57602 /* Assert that the caller has opened the required transaction. */
57603 assert( p->inTrans>TRANS_NONE );
57604 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
57605 assert( pBt->pPage1 && pBt->pPage1->aData );
57606 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
57607
 
 
 
57608 if( wrFlag ){
57609 allocateTempSpace(pBt);
57610 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
57611 }
57612 if( iTable==1 && btreePagecount(pBt)==0 ){
@@ -56966,14 +57621,20 @@
57621 pCur->pKeyInfo = pKeyInfo;
57622 pCur->pBtree = p;
57623 pCur->pBt = pBt;
57624 assert( wrFlag==0 || wrFlag==BTCF_WriteFlag );
57625 pCur->curFlags = wrFlag;
57626 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
57627 /* If there are two or more cursors on the same btree, then all such
57628 ** cursors *must* have the BTCF_Multiple flag set. */
57629 for(pX=pBt->pCursor; pX; pX=pX->pNext){
57630 if( pX->pgnoRoot==(Pgno)iTable ){
57631 pX->curFlags |= BTCF_Multiple;
57632 pCur->curFlags |= BTCF_Multiple;
57633 }
57634 }
57635 pCur->pNext = pBt->pCursor;
 
 
 
57636 pBt->pCursor = pCur;
57637 pCur->eState = CURSOR_INVALID;
57638 return SQLITE_OK;
57639 }
57640 SQLITE_PRIVATE int sqlite3BtreeCursor(
@@ -57027,17 +57688,22 @@
57688 if( pBtree ){
57689 int i;
57690 BtShared *pBt = pCur->pBt;
57691 sqlite3BtreeEnter(pBtree);
57692 sqlite3BtreeClearCursor(pCur);
57693 assert( pBt->pCursor!=0 );
57694 if( pBt->pCursor==pCur ){
 
57695 pBt->pCursor = pCur->pNext;
57696 }else{
57697 BtCursor *pPrev = pBt->pCursor;
57698 do{
57699 if( pPrev->pNext==pCur ){
57700 pPrev->pNext = pCur->pNext;
57701 break;
57702 }
57703 pPrev = pPrev->pNext;
57704 }while( ALWAYS(pPrev) );
57705 }
57706 for(i=0; i<=pCur->iPage; i++){
57707 releasePage(pCur->apPage[i]);
57708 }
57709 unlockBtreeIfUnused(pBt);
@@ -57053,17 +57719,10 @@
57719 ** BtCursor.info structure. If it is not already valid, call
57720 ** btreeParseCell() to fill it in.
57721 **
57722 ** BtCursor.info is a cache of the information in the current cell.
57723 ** Using this cache reduces the number of calls to btreeParseCell().
 
 
 
 
 
 
 
57724 */
57725 #ifndef NDEBUG
57726 static void assertCellInfo(BtCursor *pCur){
57727 CellInfo info;
57728 int iPage = pCur->iPage;
@@ -57072,32 +57731,19 @@
57731 assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
57732 }
57733 #else
57734 #define assertCellInfo(x)
57735 #endif
57736 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
57737 if( pCur->info.nSize==0 ){
57738 int iPage = pCur->iPage;
57739 pCur->curFlags |= BTCF_ValidNKey;
57740 btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
57741 }else{
57742 assertCellInfo(pCur);
57743 }
57744 }
 
 
 
 
 
 
 
 
 
 
 
 
 
57745
57746 #ifndef NDEBUG /* The next routine used only within assert() statements */
57747 /*
57748 ** Return true if the given BtCursor is valid. A valid cursor is one
57749 ** that is currently pointing to a row in a (non-empty) table.
@@ -57599,35 +58245,25 @@
58245 ** the new child page does not match the flags field of the parent (i.e.
58246 ** if an intkey page appears to be the parent of a non-intkey page, or
58247 ** vice-versa).
58248 */
58249 static int moveToChild(BtCursor *pCur, u32 newPgno){
 
 
 
58250 BtShared *pBt = pCur->pBt;
58251
58252 assert( cursorHoldsMutex(pCur) );
58253 assert( pCur->eState==CURSOR_VALID );
58254 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
58255 assert( pCur->iPage>=0 );
58256 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
58257 return SQLITE_CORRUPT_BKPT;
58258 }
 
 
 
 
 
 
 
58259 pCur->info.nSize = 0;
58260 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
58261 pCur->iPage++;
58262 pCur->aiIdx[pCur->iPage] = 0;
58263 return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
58264 pCur, pCur->curPagerFlags);
58265 }
58266
58267 #if SQLITE_DEBUG
58268 /*
58269 ** Page pParent is an internal (non-leaf) tree page. This function
@@ -57667,15 +58303,13 @@
58303 pCur->apPage[pCur->iPage-1],
58304 pCur->aiIdx[pCur->iPage-1],
58305 pCur->apPage[pCur->iPage]->pgno
58306 );
58307 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
 
 
 
58308 pCur->info.nSize = 0;
58309 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
58310 releasePageNotNull(pCur->apPage[pCur->iPage--]);
58311 }
58312
58313 /*
58314 ** Move the cursor to point to the root page of its b-tree structure.
58315 **
@@ -57712,22 +58346,27 @@
58346 }
58347 sqlite3BtreeClearCursor(pCur);
58348 }
58349
58350 if( pCur->iPage>=0 ){
58351 while( pCur->iPage ){
58352 assert( pCur->apPage[pCur->iPage]!=0 );
58353 releasePageNotNull(pCur->apPage[pCur->iPage--]);
58354 }
58355 }else if( pCur->pgnoRoot==0 ){
58356 pCur->eState = CURSOR_INVALID;
58357 return SQLITE_OK;
58358 }else{
58359 assert( pCur->iPage==(-1) );
58360 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
58361 0, pCur->curPagerFlags);
58362 if( rc!=SQLITE_OK ){
58363 pCur->eState = CURSOR_INVALID;
58364 return rc;
58365 }
58366 pCur->iPage = 0;
58367 pCur->curIntKey = pCur->apPage[0]->intKey;
58368 }
58369 pRoot = pCur->apPage[0];
58370 assert( pRoot->pgno==pCur->pgnoRoot );
58371
58372 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
@@ -57926,11 +58565,11 @@
58565 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
58566
58567 /* If the cursor is already positioned at the point we are trying
58568 ** to move to, then just return without doing any work */
58569 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
58570 && pCur->curIntKey
58571 ){
58572 if( pCur->info.nKey==intKey ){
58573 *pRes = 0;
58574 return SQLITE_OK;
58575 }
@@ -57961,11 +58600,12 @@
58600 if( pCur->eState==CURSOR_INVALID ){
58601 *pRes = -1;
58602 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
58603 return SQLITE_OK;
58604 }
58605 assert( pCur->apPage[0]->intKey==pCur->curIntKey );
58606 assert( pCur->curIntKey || pIdxKey );
58607 for(;;){
58608 int lwr, upr, idx, c;
58609 Pgno chldPg;
58610 MemPage *pPage = pCur->apPage[pCur->iPage];
58611 u8 *pCell; /* Pointer to current cell in pPage */
@@ -57984,11 +58624,11 @@
58624 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
58625 pCur->aiIdx[pCur->iPage] = (u16)idx;
58626 if( xRecordCompare==0 ){
58627 for(;;){
58628 i64 nCellKey;
58629 pCell = findCellPastPtr(pPage, idx);
58630 if( pPage->intKeyLeaf ){
58631 while( 0x80 <= *(pCell++) ){
58632 if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
58633 }
58634 }
@@ -58017,11 +58657,11 @@
58657 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
58658 }
58659 }else{
58660 for(;;){
58661 int nCell; /* Size of the pCell cell in bytes */
58662 pCell = findCellPastPtr(pPage, idx);
58663
58664 /* The maximum supported page-size is 65536 bytes. This means that
58665 ** the maximum number of record bytes stored on an index B-Tree
58666 ** page is less than 16384 bytes and may be stored as a 2-byte
58667 ** varint. This information is used to attempt to avoid parsing
@@ -58053,11 +58693,11 @@
58693 ** up to two varints past the end of the buffer. An extra 18
58694 ** bytes of padding is allocated at the end of the buffer in
58695 ** case this happens. */
58696 void *pCellKey;
58697 u8 * const pCellBody = pCell - pPage->childPtrSize;
58698 pPage->xParseCell(pPage, pCellBody, &pCur->info);
58699 nCell = (int)pCur->info.nKey;
58700 testcase( nCell<0 ); /* True if key size is 2^32 or more */
58701 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
58702 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
58703 testcase( nCell==2 ); /* Minimum legal index key size */
@@ -58356,12 +58996,11 @@
58996 ** has already been called on the new page.) The new page has also
58997 ** been referenced and the calling routine is responsible for calling
58998 ** sqlite3PagerUnref() on the new page when it is done.
58999 **
59000 ** SQLITE_OK is returned on success. Any other return value indicates
59001 ** an error. *ppPage is set to NULL in the event of an error.
 
59002 **
59003 ** If the "nearby" parameter is not 0, then an effort is made to
59004 ** locate a page close to the page number "nearby". This can be used in an
59005 ** attempt to keep related pages close to each other in the database file,
59006 ** which in turn can make database access faster.
@@ -58400,10 +59039,11 @@
59039 }
59040 if( n>0 ){
59041 /* There are pages on the freelist. Reuse one of those pages. */
59042 Pgno iTrunk;
59043 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
59044 u32 nSearch = 0; /* Count of the number of search attempts */
59045
59046 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
59047 ** shows that the page 'nearby' is somewhere on the free-list, then
59048 ** the entire-list will be searched for that page.
59049 */
@@ -58448,11 +59088,11 @@
59088 ** stores the page number of the first page of the freelist, or zero if
59089 ** the freelist is empty. */
59090 iTrunk = get4byte(&pPage1->aData[32]);
59091 }
59092 testcase( iTrunk==mxPage );
59093 if( iTrunk>mxPage || nSearch++ > n ){
59094 rc = SQLITE_CORRUPT_BKPT;
59095 }else{
59096 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
59097 }
59098 if( rc ){
@@ -58601,10 +59241,11 @@
59241 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
59242 if( rc==SQLITE_OK ){
59243 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
59244 if( rc!=SQLITE_OK ){
59245 releasePage(*ppPage);
59246 *ppPage = 0;
59247 }
59248 }
59249 searchList = 0;
59250 }
59251 }
@@ -58842,11 +59483,11 @@
59483 int rc;
59484 int nOvfl;
59485 u32 ovflPageSize;
59486
59487 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
59488 pPage->xParseCell(pPage, pCell, &info);
59489 *pnSize = info.nSize;
59490 if( info.iOverflow==0 ){
59491 return SQLITE_OK; /* No overflow pages. Return without doing anything */
59492 }
59493 if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
@@ -58954,13 +59595,11 @@
59595 if( pPage->intKey ){
59596 pSrc = pData;
59597 nSrc = nData;
59598 nData = 0;
59599 }else{
59600 assert( nKey<=0x7fffffff && pKey!=0 );
 
 
59601 nPayload = (int)nKey;
59602 pSrc = pKey;
59603 nSrc = (int)nKey;
59604 }
59605 if( nPayload<=pPage->maxLocal ){
@@ -58996,11 +59635,11 @@
59635 ** were computed correctly.
59636 */
59637 #if SQLITE_DEBUG
59638 {
59639 CellInfo info;
59640 pPage->xParseCell(pPage, pCell, &info);
59641 assert( nHeader=(int)(info.pPayload - pCell) );
59642 assert( info.nKey==nKey );
59643 assert( *pnSize == info.nSize );
59644 assert( spaceLeft == info.nLocal );
59645 assert( pPrior == &pCell[info.iOverflow] );
@@ -59166,14 +59805,12 @@
59805 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
59806 int *pRC /* Read and write return code from here */
59807 ){
59808 int idx = 0; /* Where to write new cell content in data[] */
59809 int j; /* Loop counter */
 
 
 
59810 u8 *data; /* The content of the whole page */
59811 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
59812
59813 if( *pRC ) return;
59814
59815 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
59816 assert( MX_CELL(pPage->pBt)<=10921 );
@@ -59184,11 +59821,11 @@
59821 /* The cell should normally be sized correctly. However, when moving a
59822 ** malformed cell from a leaf page to an interior page, if the cell size
59823 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
59824 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
59825 ** the term after the || in the following assert(). */
59826 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
59827 if( pPage->nOverflow || sz+2>pPage->nFree ){
59828 if( pTemp ){
59829 memcpy(pTemp, pCell, sz);
59830 pCell = pTemp;
59831 }
@@ -59197,36 +59834,46 @@
59834 }
59835 j = pPage->nOverflow++;
59836 assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
59837 pPage->apOvfl[j] = pCell;
59838 pPage->aiOvfl[j] = (u16)i;
59839
59840 /* When multiple overflows occur, they are always sequential and in
59841 ** sorted order. This invariants arise because multiple overflows can
59842 ** only occur when inserting divider cells into the parent page during
59843 ** balancing, and the dividers are adjacent and sorted.
59844 */
59845 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
59846 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
59847 }else{
59848 int rc = sqlite3PagerWrite(pPage->pDbPage);
59849 if( rc!=SQLITE_OK ){
59850 *pRC = rc;
59851 return;
59852 }
59853 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
59854 data = pPage->aData;
59855 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
 
 
59856 rc = allocateSpace(pPage, sz, &idx);
59857 if( rc ){ *pRC = rc; return; }
59858 /* The allocateSpace() routine guarantees the following properties
59859 ** if it returns successfully */
59860 assert( idx >= 0 );
59861 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
59862 assert( idx+sz <= (int)pPage->pBt->usableSize );
 
59863 pPage->nFree -= (u16)(2 + sz);
59864 memcpy(&data[idx], pCell, sz);
59865 if( iChild ){
59866 put4byte(&data[idx], iChild);
59867 }
59868 pIns = pPage->aCellIdx + i*2;
59869 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
59870 put2byte(pIns, idx);
59871 pPage->nCell++;
59872 /* increment the cell count */
59873 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
59874 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
59875 #ifndef SQLITE_OMIT_AUTOVACUUM
59876 if( pPage->pBt->autoVacuum ){
59877 /* The cell may contain a pointer to an overflow page. If so, write
59878 ** the entry for the overflow page into the pointer map.
59879 */
@@ -59233,10 +59880,56 @@
59880 ptrmapPutOvflPtr(pPage, pCell, pRC);
59881 }
59882 #endif
59883 }
59884 }
59885
59886 /*
59887 ** A CellArray object contains a cache of pointers and sizes for a
59888 ** consecutive sequence of cells that might be held multiple pages.
59889 */
59890 typedef struct CellArray CellArray;
59891 struct CellArray {
59892 int nCell; /* Number of cells in apCell[] */
59893 MemPage *pRef; /* Reference page */
59894 u8 **apCell; /* All cells begin balanced */
59895 u16 *szCell; /* Local size of all cells in apCell[] */
59896 };
59897
59898 /*
59899 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
59900 ** computed.
59901 */
59902 static void populateCellCache(CellArray *p, int idx, int N){
59903 assert( idx>=0 && idx+N<=p->nCell );
59904 while( N>0 ){
59905 assert( p->apCell[idx]!=0 );
59906 if( p->szCell[idx]==0 ){
59907 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
59908 }else{
59909 assert( CORRUPT_DB ||
59910 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
59911 }
59912 idx++;
59913 N--;
59914 }
59915 }
59916
59917 /*
59918 ** Return the size of the Nth element of the cell array
59919 */
59920 static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
59921 assert( N>=0 && N<p->nCell );
59922 assert( p->szCell[N]==0 );
59923 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
59924 return p->szCell[N];
59925 }
59926 static u16 cachedCellSize(CellArray *p, int N){
59927 assert( N>=0 && N<p->nCell );
59928 if( p->szCell[N] ) return p->szCell[N];
59929 return computeCellSize(p, N);
59930 }
59931
59932 /*
59933 ** Array apCell[] contains pointers to nCell b-tree page cells. The
59934 ** szCell[] array contains the size in bytes of each cell. This function
59935 ** replaces the current contents of page pPg with the contents of the cell
@@ -59247,11 +59940,11 @@
59940 ** such cells before overwriting the page data.
59941 **
59942 ** The MemPage.nFree field is invalidated by this function. It is the
59943 ** responsibility of the caller to set it correctly.
59944 */
59945 static int rebuildPage(
59946 MemPage *pPg, /* Edit this page */
59947 int nCell, /* Final number of cells on page */
59948 u8 **apCell, /* Array of cells */
59949 u16 *szCell /* Array of cell sizes */
59950 ){
@@ -59272,15 +59965,16 @@
59965 u8 *pCell = apCell[i];
59966 if( pCell>aData && pCell<pEnd ){
59967 pCell = &pTmp[pCell - aData];
59968 }
59969 pData -= szCell[i];
 
59970 put2byte(pCellptr, (pData - aData));
59971 pCellptr += 2;
59972 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
59973 memcpy(pData, pCell, szCell[i]);
59974 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
59975 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
59976 }
59977
59978 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
59979 pPg->nCell = nCell;
59980 pPg->nOverflow = 0;
@@ -59287,10 +59981,11 @@
59981
59982 put2byte(&aData[hdr+1], 0);
59983 put2byte(&aData[hdr+3], pPg->nCell);
59984 put2byte(&aData[hdr+5], pData - aData);
59985 aData[hdr+7] = 0x00;
59986 return SQLITE_OK;
59987 }
59988
59989 /*
59990 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
59991 ** contains the size in bytes of each such cell. This function attempts to
@@ -59319,29 +60014,29 @@
60014 static int pageInsertArray(
60015 MemPage *pPg, /* Page to add cells to */
60016 u8 *pBegin, /* End of cell-pointer array */
60017 u8 **ppData, /* IN/OUT: Page content -area pointer */
60018 u8 *pCellptr, /* Pointer to cell-pointer area */
60019 int iFirst, /* Index of first cell to add */
60020 int nCell, /* Number of cells to add to pPg */
60021 CellArray *pCArray /* Array of cells */
 
60022 ){
60023 int i;
60024 u8 *aData = pPg->aData;
60025 u8 *pData = *ppData;
60026 int iEnd = iFirst + nCell;
60027 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
60028 for(i=iFirst; i<iEnd; i++){
60029 int sz, rc;
 
60030 u8 *pSlot;
60031 sz = cachedCellSize(pCArray, i);
60032 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
60033 pData -= sz;
60034 if( pData<pBegin ) return 1;
60035 pSlot = pData;
60036 }
60037 memcpy(pSlot, pCArray->apCell[i], sz);
60038 put2byte(pCellptr, (pSlot - aData));
60039 pCellptr += 2;
60040 }
60041 *ppData = pData;
60042 return 0;
@@ -59356,26 +60051,31 @@
60051 **
60052 ** This function returns the total number of cells added to the free-list.
60053 */
60054 static int pageFreeArray(
60055 MemPage *pPg, /* Page to edit */
60056 int iFirst, /* First cell to delete */
60057 int nCell, /* Cells to delete */
60058 CellArray *pCArray /* Array of cells */
 
60059 ){
60060 u8 * const aData = pPg->aData;
60061 u8 * const pEnd = &aData[pPg->pBt->usableSize];
60062 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
60063 int nRet = 0;
60064 int i;
60065 int iEnd = iFirst + nCell;
60066 u8 *pFree = 0;
60067 int szFree = 0;
60068
60069 for(i=iFirst; i<iEnd; i++){
60070 u8 *pCell = pCArray->apCell[i];
60071 if( pCell>=pStart && pCell<pEnd ){
60072 int sz;
60073 /* No need to use cachedCellSize() here. The sizes of all cells that
60074 ** are to be freed have already been computing while deciding which
60075 ** cells need freeing */
60076 sz = pCArray->szCell[i]; assert( sz>0 );
60077 if( pFree!=(pCell + sz) ){
60078 if( pFree ){
60079 assert( pFree>aData && (pFree - aData)<65536 );
60080 freeSpace(pPg, (u16)(pFree - aData), szFree);
60081 }
@@ -59406,17 +60106,16 @@
60106 ** the correct cells after being balanced.
60107 **
60108 ** The pPg->nFree field is invalid when this function returns. It is the
60109 ** responsibility of the caller to set it correctly.
60110 */
60111 static int editPage(
60112 MemPage *pPg, /* Edit this page */
60113 int iOld, /* Index of first cell currently on page */
60114 int iNew, /* Index of new first cell on page */
60115 int nNew, /* Final number of cells on page */
60116 CellArray *pCArray /* Array of cells and sizes */
 
60117 ){
60118 u8 * const aData = pPg->aData;
60119 const int hdr = pPg->hdrOffset;
60120 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
60121 int nCell = pPg->nCell; /* Cells stored on pPg */
@@ -59431,20 +60130,16 @@
60130 memcpy(pTmp, aData, pPg->pBt->usableSize);
60131 #endif
60132
60133 /* Remove cells from the start and end of the page */
60134 if( iOld<iNew ){
60135 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
 
 
60136 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
60137 nCell -= nShift;
60138 }
60139 if( iNewEnd < iOldEnd ){
60140 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
 
 
60141 }
60142
60143 pData = &aData[get2byteNotZero(&aData[hdr+5])];
60144 if( pData<pBegin ) goto editpage_fail;
60145
@@ -59454,11 +60149,11 @@
60149 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
60150 pCellptr = pPg->aCellIdx;
60151 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
60152 if( pageInsertArray(
60153 pPg, pBegin, &pData, pCellptr,
60154 iNew, nAdd, pCArray
60155 ) ) goto editpage_fail;
60156 nCell += nAdd;
60157 }
60158
60159 /* Add any overflow cells */
@@ -59468,20 +60163,20 @@
60163 pCellptr = &pPg->aCellIdx[iCell * 2];
60164 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
60165 nCell++;
60166 if( pageInsertArray(
60167 pPg, pBegin, &pData, pCellptr,
60168 iCell+iNew, 1, pCArray
60169 ) ) goto editpage_fail;
60170 }
60171 }
60172
60173 /* Append cells to the end of the page */
60174 pCellptr = &pPg->aCellIdx[nCell*2];
60175 if( pageInsertArray(
60176 pPg, pBegin, &pData, pCellptr,
60177 iNew+nCell, nNew-nCell, pCArray
60178 ) ) goto editpage_fail;
60179
60180 pPg->nCell = nNew;
60181 pPg->nOverflow = 0;
60182
@@ -59488,23 +60183,25 @@
60183 put2byte(&aData[hdr+3], pPg->nCell);
60184 put2byte(&aData[hdr+5], pData - aData);
60185
60186 #ifdef SQLITE_DEBUG
60187 for(i=0; i<nNew && !CORRUPT_DB; i++){
60188 u8 *pCell = pCArray->apCell[i+iNew];
60189 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
60190 if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
60191 pCell = &pTmp[pCell - aData];
60192 }
60193 assert( 0==memcmp(pCell, &aData[iOff],
60194 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
60195 }
60196 #endif
60197
60198 return SQLITE_OK;
60199 editpage_fail:
60200 /* Unable to edit this page. Rebuild it from scratch instead. */
60201 populateCellCache(pCArray, iNew, nNew);
60202 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
60203 }
60204
60205 /*
60206 ** The following parameters determine how many adjacent pages get involved
60207 ** in a balancing operation. NN is the number of neighbors on either side
@@ -59566,17 +60263,18 @@
60263
60264 if( rc==SQLITE_OK ){
60265
60266 u8 *pOut = &pSpace[4];
60267 u8 *pCell = pPage->apOvfl[0];
60268 u16 szCell = pPage->xCellSize(pPage, pCell);
60269 u8 *pStop;
60270
60271 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
60272 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
60273 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
60274 rc = rebuildPage(pNew, 1, &pCell, &szCell);
60275 if( NEVER(rc) ) return rc;
60276 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
60277
60278 /* If this is an auto-vacuum database, update the pointer map
60279 ** with entries for the new page, and any pointer from the
60280 ** cell on the page to an overflow page. If either of these
@@ -59645,11 +60343,11 @@
60343 for(j=0; j<pPage->nCell; j++){
60344 CellInfo info;
60345 u8 *z;
60346
60347 z = findCell(pPage, j);
60348 pPage->xParseCell(pPage, z, &info);
60349 if( info.iOverflow ){
60350 Pgno ovfl = get4byte(&z[info.iOverflow]);
60351 ptrmapGet(pBt, ovfl, &e, &n);
60352 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
60353 }
@@ -59776,11 +60474,10 @@
60474 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
60475 int isRoot, /* True if pParent is a root-page */
60476 int bBulk /* True if this call is part of a bulk load */
60477 ){
60478 BtShared *pBt; /* The whole database */
 
60479 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
60480 int nNew = 0; /* Number of pages in apNew[] */
60481 int nOld; /* Number of pages in apOld[] */
60482 int i, j, k; /* Loop counters */
60483 int nxDiv; /* Next divider slot in pParent->aCell[] */
@@ -59787,31 +60484,31 @@
60484 int rc = SQLITE_OK; /* The return code */
60485 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
60486 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
60487 int usableSpace; /* Bytes in pPage beyond the header */
60488 int pageFlags; /* Value of pPage->aData[0] */
 
60489 int iSpace1 = 0; /* First unused byte of aSpace1[] */
60490 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
60491 int szScratch; /* Size of scratch memory requested */
60492 MemPage *apOld[NB]; /* pPage and up to two siblings */
60493 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
60494 u8 *pRight; /* Location in parent of right-sibling pointer */
60495 u8 *apDiv[NB-1]; /* Divider cells in pParent */
60496 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
60497 int cntOld[NB+2]; /* Old index in b.apCell[] */
60498 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
 
 
60499 u8 *aSpace1; /* Space for copies of dividers cells */
60500 Pgno pgno; /* Temp var to store a page number in */
60501 u8 abDone[NB+2]; /* True after i'th new page is populated */
60502 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
60503 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
60504 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
60505 CellArray b; /* Parsed information on cells being balanced */
60506
60507 memset(abDone, 0, sizeof(abDone));
60508 b.nCell = 0;
60509 b.apCell = 0;
60510 pBt = pParent->pBt;
60511 assert( sqlite3_mutex_held(pBt->mutex) );
60512 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
60513
60514 #if 0
@@ -59861,11 +60558,11 @@
60558 }else{
60559 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
60560 }
60561 pgno = get4byte(pRight);
60562 while( 1 ){
60563 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
60564 if( rc ){
60565 memset(apOld, 0, (i+1)*sizeof(MemPage*));
60566 goto balance_cleanup;
60567 }
60568 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
@@ -59872,16 +60569,16 @@
60569 if( (i--)==0 ) break;
60570
60571 if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
60572 apDiv[i] = pParent->apOvfl[0];
60573 pgno = get4byte(apDiv[i]);
60574 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
60575 pParent->nOverflow = 0;
60576 }else{
60577 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
60578 pgno = get4byte(apDiv[i]);
60579 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
60580
60581 /* Drop the cell from the parent page. apDiv[i] still points to
60582 ** the cell within the parent, even though it has been dropped.
60583 ** This is safe because dropping a cell only overwrites the first
60584 ** four bytes of it, and this function does not need the first
@@ -59916,142 +60613,205 @@
60613
60614 /*
60615 ** Allocate space for memory structures
60616 */
60617 szScratch =
60618 nMaxCells*sizeof(u8*) /* b.apCell */
60619 + nMaxCells*sizeof(u16) /* b.szCell */
60620 + pBt->pageSize; /* aSpace1 */
60621
60622 /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
60623 ** that is more than 6 times the database page size. */
60624 assert( szScratch<=6*(int)pBt->pageSize );
60625 b.apCell = sqlite3ScratchMalloc( szScratch );
60626 if( b.apCell==0 ){
60627 rc = SQLITE_NOMEM;
60628 goto balance_cleanup;
60629 }
60630 b.szCell = (u16*)&b.apCell[nMaxCells];
60631 aSpace1 = (u8*)&b.szCell[nMaxCells];
60632 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
60633
60634 /*
60635 ** Load pointers to all cells on sibling pages and the divider cells
60636 ** into the local b.apCell[] array. Make copies of the divider cells
60637 ** into space obtained from aSpace1[]. The divider cells have already
60638 ** been removed from pParent.
60639 **
60640 ** If the siblings are on leaf pages, then the child pointers of the
60641 ** divider cells are stripped from the cells before they are copied
60642 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
60643 ** child pointers. If siblings are not leaves, then all cell in
60644 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
60645 ** are alike.
60646 **
60647 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
60648 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
60649 */
60650 b.pRef = apOld[0];
60651 leafCorrection = b.pRef->leaf*4;
60652 leafData = b.pRef->intKeyLeaf;
60653 for(i=0; i<nOld; i++){
 
60654 MemPage *pOld = apOld[i];
60655 int limit = pOld->nCell;
60656 u8 *aData = pOld->aData;
60657 u16 maskPage = pOld->maskPage;
60658 u8 *piCell = aData + pOld->cellOffset;
60659 u8 *piEnd;
60660
60661 /* Verify that all sibling pages are of the same "type" (table-leaf,
60662 ** table-interior, index-leaf, or index-interior).
60663 */
60664 if( pOld->aData[0]!=apOld[0]->aData[0] ){
60665 rc = SQLITE_CORRUPT_BKPT;
60666 goto balance_cleanup;
60667 }
60668
60669 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
60670 ** constains overflow cells, include them in the b.apCell[] array
60671 ** in the correct spot.
60672 **
60673 ** Note that when there are multiple overflow cells, it is always the
60674 ** case that they are sequential and adjacent. This invariant arises
60675 ** because multiple overflows can only occurs when inserting divider
60676 ** cells into a parent on a prior balance, and divider cells are always
60677 ** adjacent and are inserted in order. There is an assert() tagged
60678 ** with "NOTE 1" in the overflow cell insertion loop to prove this
60679 ** invariant.
60680 **
60681 ** This must be done in advance. Once the balance starts, the cell
60682 ** offset section of the btree page will be overwritten and we will no
60683 ** long be able to find the cells if a pointer to each cell is not saved
60684 ** first.
60685 */
60686 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit);
60687 if( pOld->nOverflow>0 ){
60688 memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow);
60689 limit = pOld->aiOvfl[0];
60690 for(j=0; j<limit; j++){
60691 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
60692 piCell += 2;
60693 b.nCell++;
60694 }
60695 for(k=0; k<pOld->nOverflow; k++){
60696 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
60697 b.apCell[b.nCell] = pOld->apOvfl[k];
60698 b.nCell++;
60699 }
60700 }
60701 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
60702 while( piCell<piEnd ){
60703 assert( b.nCell<nMaxCells );
60704 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
60705 piCell += 2;
60706 b.nCell++;
60707 }
60708
60709 cntOld[i] = b.nCell;
60710 if( i<nOld-1 && !leafData){
60711 u16 sz = (u16)szNew[i];
60712 u8 *pTemp;
60713 assert( b.nCell<nMaxCells );
60714 b.szCell[b.nCell] = sz;
60715 pTemp = &aSpace1[iSpace1];
60716 iSpace1 += sz;
60717 assert( sz<=pBt->maxLocal+23 );
60718 assert( iSpace1 <= (int)pBt->pageSize );
60719 memcpy(pTemp, apDiv[i], sz);
60720 b.apCell[b.nCell] = pTemp+leafCorrection;
60721 assert( leafCorrection==0 || leafCorrection==4 );
60722 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
60723 if( !pOld->leaf ){
60724 assert( leafCorrection==0 );
60725 assert( pOld->hdrOffset==0 );
60726 /* The right pointer of the child page pOld becomes the left
60727 ** pointer of the divider cell */
60728 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
60729 }else{
60730 assert( leafCorrection==4 );
60731 while( b.szCell[b.nCell]<4 ){
60732 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
60733 ** does exist, pad it with 0x00 bytes. */
60734 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
60735 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
60736 aSpace1[iSpace1++] = 0x00;
60737 b.szCell[b.nCell]++;
60738 }
60739 }
60740 b.nCell++;
60741 }
60742 }
60743
60744 /*
60745 ** Figure out the number of pages needed to hold all b.nCell cells.
60746 ** Store this number in "k". Also compute szNew[] which is the total
60747 ** size of all cells on the i-th page and cntNew[] which is the index
60748 ** in b.apCell[] of the cell that divides page i from page i+1.
60749 ** cntNew[k] should equal b.nCell.
60750 **
60751 ** Values computed by this block:
60752 **
60753 ** k: The total number of sibling pages
60754 ** szNew[i]: Spaced used on the i-th sibling page.
60755 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
60756 ** the right of the i-th sibling page.
60757 ** usableSpace: Number of bytes of space available on each sibling.
60758 **
60759 */
60760 usableSpace = pBt->usableSize - 12 + leafCorrection;
60761 for(i=0; i<nOld; i++){
60762 MemPage *p = apOld[i];
60763 szNew[i] = usableSpace - p->nFree;
60764 if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
60765 for(j=0; j<p->nOverflow; j++){
60766 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
60767 }
60768 cntNew[i] = cntOld[i];
60769 }
60770 k = nOld;
60771 for(i=0; i<k; i++){
60772 int sz;
60773 while( szNew[i]>usableSpace ){
60774 if( i+1>=k ){
60775 k = i+2;
60776 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
60777 szNew[k-1] = 0;
60778 cntNew[k-1] = b.nCell;
60779 }
60780 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
60781 szNew[i] -= sz;
60782 if( !leafData ){
60783 if( cntNew[i]<b.nCell ){
60784 sz = 2 + cachedCellSize(&b, cntNew[i]);
60785 }else{
60786 sz = 0;
60787 }
60788 }
60789 szNew[i+1] += sz;
60790 cntNew[i]--;
60791 }
60792 while( cntNew[i]<b.nCell ){
60793 sz = 2 + cachedCellSize(&b, cntNew[i]);
60794 if( szNew[i]+sz>usableSpace ) break;
60795 szNew[i] += sz;
60796 cntNew[i]++;
60797 if( !leafData ){
60798 if( cntNew[i]<b.nCell ){
60799 sz = 2 + cachedCellSize(&b, cntNew[i]);
60800 }else{
60801 sz = 0;
60802 }
60803 }
60804 szNew[i+1] -= sz;
60805 }
60806 if( cntNew[i]>=b.nCell ){
60807 k = i+1;
60808 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
60809 rc = SQLITE_CORRUPT_BKPT;
60810 goto balance_cleanup;
60811 }
60812 }
60813
60814 /*
60815 ** The packing computed by the previous block is biased toward the siblings
60816 ** on the left side (siblings with smaller keys). The left siblings are
60817 ** always nearly full, while the right-most sibling might be nearly empty.
@@ -60068,23 +60828,31 @@
60828 int r; /* Index of right-most cell in left sibling */
60829 int d; /* Index of first cell to the left of right sibling */
60830
60831 r = cntNew[i-1] - 1;
60832 d = r + 1 - leafData;
60833 (void)cachedCellSize(&b, d);
60834 do{
60835 assert( d<nMaxCells );
60836 assert( r<nMaxCells );
60837 (void)cachedCellSize(&b, r);
60838 if( szRight!=0
60839 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+2)) ){
60840 break;
60841 }
60842 szRight += b.szCell[d] + 2;
60843 szLeft -= b.szCell[r] + 2;
60844 cntNew[i-1] = r;
60845 r--;
60846 d--;
60847 }while( r>=0 );
60848 szNew[i] = szRight;
60849 szNew[i-1] = szLeft;
60850 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
60851 rc = SQLITE_CORRUPT_BKPT;
60852 goto balance_cleanup;
60853 }
60854 }
60855
60856 /* Sanity check: For a non-corrupt database file one of the follwing
60857 ** must be true:
60858 ** (1) We found one or more cells (cntNew[0])>0), or
@@ -60116,11 +60884,11 @@
60884 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
60885 if( rc ) goto balance_cleanup;
60886 zeroPage(pNew, pageFlags);
60887 apNew[i] = pNew;
60888 nNew++;
60889 cntOld[i] = b.nCell;
60890
60891 /* Set the pointer-map entry for the new sibling page. */
60892 if( ISAUTOVACUUM ){
60893 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
60894 if( rc!=SQLITE_OK ){
@@ -60221,12 +60989,12 @@
60989 int cntOldNext = pNew->nCell + pNew->nOverflow;
60990 int usableSize = pBt->usableSize;
60991 int iNew = 0;
60992 int iOld = 0;
60993
60994 for(i=0; i<b.nCell; i++){
60995 u8 *pCell = b.apCell[i];
60996 if( i==cntOldNext ){
60997 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
60998 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
60999 aOld = pOld->aData;
61000 }
@@ -60247,13 +61015,14 @@
61015 || pCell>=&aOld[usableSize]
61016 ){
61017 if( !leafCorrection ){
61018 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
61019 }
61020 if( cachedCellSize(&b,i)>pNew->minLocal ){
61021 ptrmapPutOvflPtr(pNew, pCell, &rc);
61022 }
61023 if( rc ) goto balance_cleanup;
61024 }
61025 }
61026 }
61027
61028 /* Insert new divider cells into pParent. */
@@ -60263,24 +61032,25 @@
61032 int sz;
61033 MemPage *pNew = apNew[i];
61034 j = cntNew[i];
61035
61036 assert( j<nMaxCells );
61037 assert( b.apCell[j]!=0 );
61038 pCell = b.apCell[j];
61039 sz = b.szCell[j] + leafCorrection;
61040 pTemp = &aOvflSpace[iOvflSpace];
61041 if( !pNew->leaf ){
61042 memcpy(&pNew->aData[8], pCell, 4);
61043 }else if( leafData ){
61044 /* If the tree is a leaf-data tree, and the siblings are leaves,
61045 ** then there is no divider cell in b.apCell[]. Instead, the divider
61046 ** cell consists of the integer key for the right-most cell of
61047 ** the sibling-page assembled above only.
61048 */
61049 CellInfo info;
61050 j--;
61051 pNew->xParseCell(pNew, b.apCell[j], &info);
61052 pCell = pTemp;
61053 sz = 4 + putVarint(&pCell[4], info.nKey);
61054 pTemp = 0;
61055 }else{
61056 pCell -= 4;
@@ -60293,13 +61063,13 @@
61063 **
61064 ** Note that this can never happen in an SQLite data file, as all
61065 ** cells are at least 4 bytes. It only happens in b-trees used
61066 ** to evaluate "IN (SELECT ...)" and similar clauses.
61067 */
61068 if( b.szCell[j]==4 ){
61069 assert(leafCorrection==4);
61070 sz = pParent->xCellSize(pParent, pCell);
61071 }
61072 }
61073 iOvflSpace += sz;
61074 assert( sz<=pBt->maxLocal+23 );
61075 assert( iOvflSpace <= (int)pBt->pageSize );
@@ -60351,16 +61121,17 @@
61121
61122 if( iPg==0 ){
61123 iNew = iOld = 0;
61124 nNewCell = cntNew[0];
61125 }else{
61126 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
61127 iNew = cntNew[iPg-1] + !leafData;
61128 nNewCell = cntNew[iPg] - iNew;
61129 }
61130
61131 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
61132 if( rc ) goto balance_cleanup;
61133 abDone[iPg]++;
61134 apNew[iPg]->nFree = usableSpace-szNew[iPg];
61135 assert( apNew[iPg]->nOverflow==0 );
61136 assert( apNew[iPg]->nCell==nNewCell );
61137 }
@@ -60407,11 +61178,11 @@
61178 }
61179 }
61180
61181 assert( pParent->isInit );
61182 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
61183 nOld, nNew, b.nCell));
61184
61185 /* Free any old pages that were not reused as new pages.
61186 */
61187 for(i=nNew; i<nOld; i++){
61188 freePage(apOld[i], &rc);
@@ -60430,11 +61201,11 @@
61201
61202 /*
61203 ** Cleanup before returning.
61204 */
61205 balance_cleanup:
61206 sqlite3ScratchFree(b.apCell);
61207 for(i=0; i<nOld; i++){
61208 releasePage(apOld[i]);
61209 }
61210 for(i=0; i<nNew; i++){
61211 releasePage(apNew[i]);
@@ -60705,28 +61476,32 @@
61476 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
61477 ** that the cursor is already where it needs to be and returns without
61478 ** doing any work. To avoid thwarting these optimizations, it is important
61479 ** not to clear the cursor here.
61480 */
61481 if( pCur->curFlags & BTCF_Multiple ){
61482 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
61483 if( rc ) return rc;
61484 }
61485
61486 if( pCur->pKeyInfo==0 ){
61487 assert( pKey==0 );
61488 /* If this is an insert into a table b-tree, invalidate any incrblob
61489 ** cursors open on the row being replaced */
61490 invalidateIncrblobCursors(p, nKey, 0);
61491
61492 /* If the cursor is currently on the last row and we are appending a
61493 ** new row onto the end, set the "loc" to avoid an unnecessary
61494 ** btreeMoveto() call */
61495 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
61496 && pCur->info.nKey==nKey-1 ){
61497 loc = -1;
61498 }else if( loc==0 ){
61499 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc);
61500 if( rc ) return rc;
61501 }
61502 }else if( loc==0 ){
 
 
61503 rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
61504 if( rc ) return rc;
61505 }
61506 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
61507
@@ -60740,11 +61515,11 @@
61515 assert( pPage->isInit );
61516 newCell = pBt->pTmpSpace;
61517 assert( newCell!=0 );
61518 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
61519 if( rc ) goto end_insert;
61520 assert( szNew==pPage->xCellSize(pPage, newCell) );
61521 assert( szNew <= MX_CELL_SIZE(pBt) );
61522 idx = pCur->aiIdx[pCur->iPage];
61523 if( loc==0 ){
61524 u16 szOld;
61525 assert( idx<pPage->nCell );
@@ -60824,16 +61599,12 @@
61599 assert( pBt->inTransaction==TRANS_WRITE );
61600 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
61601 assert( pCur->curFlags & BTCF_WriteFlag );
61602 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
61603 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
61604 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
61605 assert( pCur->eState==CURSOR_VALID );
 
 
 
 
61606
61607 iCellDepth = pCur->iPage;
61608 iCellIdx = pCur->aiIdx[iCellDepth];
61609 pPage = pCur->apPage[iCellDepth];
61610 pCell = findCell(pPage, iCellIdx);
@@ -60854,12 +61625,14 @@
61625 /* Save the positions of any other cursors open on this table before
61626 ** making any modifications. Make the page containing the entry to be
61627 ** deleted writable. Then free any overflow pages associated with the
61628 ** entry and finally remove the cell itself from within the page.
61629 */
61630 if( pCur->curFlags & BTCF_Multiple ){
61631 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
61632 if( rc ) return rc;
61633 }
61634
61635 /* If this is a delete operation to remove a row from a table b-tree,
61636 ** invalidate any incrblob cursors open on the row being deleted. */
61637 if( pCur->pKeyInfo==0 ){
61638 invalidateIncrblobCursors(p, pCur->info.nKey, 0);
@@ -60882,11 +61655,11 @@
61655 Pgno n = pCur->apPage[iCellDepth+1]->pgno;
61656 unsigned char *pTmp;
61657
61658 pCell = findCell(pLeaf, pLeaf->nCell-1);
61659 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
61660 nCell = pLeaf->xCellSize(pLeaf, pCell);
61661 assert( MX_CELL_SIZE(pBt) >= nCell );
61662 pTmp = pBt->pTmpSpace;
61663 assert( pTmp!=0 );
61664 rc = sqlite3PagerWrite(pLeaf->pDbPage);
61665 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
@@ -61104,11 +61877,11 @@
61877
61878 assert( sqlite3_mutex_held(pBt->mutex) );
61879 if( pgno>btreePagecount(pBt) ){
61880 return SQLITE_CORRUPT_BKPT;
61881 }
61882 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
61883 if( rc ) return rc;
61884 if( pPage->bBusy ){
61885 rc = SQLITE_CORRUPT_BKPT;
61886 goto cleardatabasepage_out;
61887 }
@@ -61776,11 +62549,11 @@
62549 */
62550 pCheck->zPfx = "On tree page %d cell %d: ";
62551 pCheck->v1 = iPage;
62552 pCheck->v2 = i;
62553 pCell = findCell(pPage,i);
62554 pPage->xParseCell(pPage, pCell, &info);
62555 sz = info.nPayload;
62556 /* For intKey pages, check that the keys are in order.
62557 */
62558 if( pPage->intKey ){
62559 if( i==0 ){
@@ -61891,14 +62664,14 @@
62664 ** immediately follows the b-tree page header. */
62665 cellStart = hdr + 12 - 4*pPage->leaf;
62666 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
62667 ** integer offsets to the cell contents. */
62668 for(i=0; i<nCell; i++){
62669 int pc = get2byteAligned(&data[cellStart+i*2]);
62670 u32 size = 65536;
62671 if( pc<=usableSize-4 ){
62672 size = pPage->xCellSize(pPage, &data[pc]);
62673 }
62674 if( (int)(pc+size-1)>=usableSize ){
62675 pCheck->zPfx = 0;
62676 checkAppendMsg(pCheck,
62677 "Corruption detected in cell %d on page %d",i,iPage);
@@ -62292,10 +63065,11 @@
63065 /*
63066 ** Mark this cursor as an incremental blob cursor.
63067 */
63068 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
63069 pCur->curFlags |= BTCF_Incrblob;
63070 pCur->pBtree->hasIncrblobCur = 1;
63071 }
63072 #endif
63073
63074 /*
63075 ** Set both the "read version" (single byte at byte offset 18) and
@@ -63739,11 +64513,11 @@
64513 ** used (for example) to implement the SQL "cast()" operator.
64514 */
64515 SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
64516 if( pMem->flags & MEM_Null ) return;
64517 switch( aff ){
64518 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
64519 if( (pMem->flags & MEM_Blob)==0 ){
64520 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
64521 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
64522 MemSetTypeFlag(pMem, MEM_Blob);
64523 }else{
@@ -63928,14 +64702,19 @@
64702 ** Make an shallow copy of pFrom into pTo. Prior contents of
64703 ** pTo are freed. The pFrom->z field is not duplicated. If
64704 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
64705 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
64706 */
64707 static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
64708 vdbeMemClearExternAndSetNull(pTo);
64709 assert( !VdbeMemDynamic(pTo) );
64710 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
64711 }
64712 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
64713 assert( (pFrom->flags & MEM_RowSet)==0 );
64714 assert( pTo->db==pFrom->db );
64715 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
64716 memcpy(pTo, pFrom, MEMCELLSIZE);
64717 if( (pFrom->flags&MEM_Static)==0 ){
64718 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
64719 assert( srcType==MEM_Ephem || srcType==MEM_Static );
64720 pTo->flags |= srcType;
@@ -64097,10 +64876,36 @@
64876 ** destroyed.
64877 **
64878 ** If this routine fails for any reason (malloc returns NULL or unable
64879 ** to read from the disk) then the pMem is left in an inconsistent state.
64880 */
64881 static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
64882 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
64883 u32 offset, /* Offset from the start of data to return bytes from. */
64884 u32 amt, /* Number of bytes to return. */
64885 int key, /* If true, retrieve from the btree key, not data. */
64886 Mem *pMem /* OUT: Return data in this Mem structure. */
64887 ){
64888 int rc;
64889 pMem->flags = MEM_Null;
64890 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
64891 if( key ){
64892 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
64893 }else{
64894 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
64895 }
64896 if( rc==SQLITE_OK ){
64897 pMem->z[amt] = 0;
64898 pMem->z[amt+1] = 0;
64899 pMem->flags = MEM_Blob|MEM_Term;
64900 pMem->n = (int)amt;
64901 }else{
64902 sqlite3VdbeMemRelease(pMem);
64903 }
64904 }
64905 return rc;
64906 }
64907 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
64908 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
64909 u32 offset, /* Offset from the start of data to return bytes from. */
64910 u32 amt, /* Number of bytes to return. */
64911 int key, /* If true, retrieve from the btree key, not data. */
@@ -64126,26 +64931,11 @@
64931 if( offset+amt<=available ){
64932 pMem->z = &zData[offset];
64933 pMem->flags = MEM_Blob|MEM_Ephem;
64934 pMem->n = (int)amt;
64935 }else{
64936 rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64937 }
64938
64939 return rc;
64940 }
64941
@@ -64462,11 +65252,11 @@
65252 }else{
65253 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
65254 if( zVal==0 ) goto no_mem;
65255 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
65256 }
65257 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
65258 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
65259 }else{
65260 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
65261 }
65262 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
@@ -64829,23 +65619,32 @@
65619 sqlite3VdbeMemRelease((Mem *)v);
65620 sqlite3DbFree(((Mem*)v)->db, v);
65621 }
65622
65623 /*
65624 ** The sqlite3ValueBytes() routine returns the number of bytes in the
65625 ** sqlite3_value object assuming that it uses the encoding "enc".
65626 ** The valueBytes() routine is a helper function.
65627 */
65628 static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
65629 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
65630 }
65631 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
65632 Mem *p = (Mem*)pVal;
65633 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
65634 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
65635 return p->n;
65636 }
65637 if( (p->flags & MEM_Blob)!=0 ){
65638 if( p->flags & MEM_Zero ){
65639 return p->n + p->u.nZero;
65640 }else{
65641 return p->n;
65642 }
65643 }
65644 if( p->flags & MEM_Null ) return 0;
65645 return valueBytes(pVal, enc);
65646 }
65647
65648 /************** End of vdbemem.c *********************************************/
65649 /************** Begin file vdbeaux.c *****************************************/
65650 /*
@@ -65078,10 +65877,27 @@
65877 ){
65878 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
65879 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
65880 return addr;
65881 }
65882
65883 /*
65884 ** Add an opcode that includes the p4 value with a P4_INT64 type.
65885 */
65886 SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
65887 Vdbe *p, /* Add the opcode to this VM */
65888 int op, /* The new opcode */
65889 int p1, /* The P1 operand */
65890 int p2, /* The P2 operand */
65891 int p3, /* The P3 operand */
65892 const u8 *zP4, /* The P4 operand */
65893 int p4type /* P4 operand type */
65894 ){
65895 char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
65896 if( p4copy ) memcpy(p4copy, zP4, 8);
65897 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
65898 }
65899
65900 /*
65901 ** Add an OP_ParseSchema opcode. This routine is broken out from
65902 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
65903 ** as having been used.
@@ -65243,10 +66059,11 @@
66059 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
66060 ** * OP_Destroy
66061 ** * OP_VUpdate
66062 ** * OP_VRename
66063 ** * OP_FkCounter with P2==0 (immediate foreign key constraint)
66064 ** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
66065 **
66066 ** Then check that the value of Parse.mayAbort is true if an
66067 ** ABORT may be thrown, or false otherwise. Return true if it does
66068 ** match, or false otherwise. This function is intended to be used as
66069 ** part of an assert statement in the compiler. Similar to:
@@ -65254,10 +66071,12 @@
66071 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
66072 */
66073 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
66074 int hasAbort = 0;
66075 int hasFkCounter = 0;
66076 int hasCreateTable = 0;
66077 int hasInitCoroutine = 0;
66078 Op *pOp;
66079 VdbeOpIter sIter;
66080 memset(&sIter, 0, sizeof(sIter));
66081 sIter.v = v;
66082
@@ -65268,10 +66087,12 @@
66087 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
66088 ){
66089 hasAbort = 1;
66090 break;
66091 }
66092 if( opcode==OP_CreateTable ) hasCreateTable = 1;
66093 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
66094 #ifndef SQLITE_OMIT_FOREIGN_KEY
66095 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
66096 hasFkCounter = 1;
66097 }
66098 #endif
@@ -65281,11 +66102,12 @@
66102 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
66103 ** If malloc failed, then the while() loop above may not have iterated
66104 ** through all opcodes and hasAbort may be set incorrectly. Return
66105 ** true for this case to prevent the assert() in the callers frame
66106 ** from failing. */
66107 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
66108 || (hasCreateTable && hasInitCoroutine) );
66109 }
66110 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
66111
66112 /*
66113 ** Loop through the program looking for P2 values that are negative
@@ -65312,15 +66134,10 @@
66134 u8 opcode = pOp->opcode;
66135
66136 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
66137 ** cases from this switch! */
66138 switch( opcode ){
 
 
 
 
 
66139 case OP_Transaction: {
66140 if( pOp->p2!=0 ) p->readOnly = 0;
66141 /* fall thru */
66142 }
66143 case OP_AutoCommit:
@@ -65560,10 +66377,14 @@
66377 */
66378 static void freeP4(sqlite3 *db, int p4type, void *p4){
66379 if( p4 ){
66380 assert( db );
66381 switch( p4type ){
66382 case P4_FUNCCTX: {
66383 freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
66384 /* Fall through into the next case */
66385 }
66386 case P4_REAL:
66387 case P4_INT64:
66388 case P4_DYNAMIC:
66389 case P4_INTARRAY: {
66390 sqlite3DbFree(db, p4);
@@ -65944,10 +66765,17 @@
66765 case P4_FUNCDEF: {
66766 FuncDef *pDef = pOp->p4.pFunc;
66767 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
66768 break;
66769 }
66770 #ifdef SQLITE_DEBUG
66771 case P4_FUNCCTX: {
66772 FuncDef *pDef = pOp->p4.pCtx->pFunc;
66773 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
66774 break;
66775 }
66776 #endif
66777 case P4_INT64: {
66778 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
66779 break;
66780 }
66781 case P4_INT32: {
@@ -66064,24 +66892,27 @@
66892
66893 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
66894 /*
66895 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
66896 */
66897 static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
66898 int i;
66899 sqlite3 *db;
66900 Db *aDb;
66901 int nDb;
 
66902 db = p->db;
66903 aDb = db->aDb;
66904 nDb = db->nDb;
66905 for(i=0; i<nDb; i++){
66906 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
66907 sqlite3BtreeLeave(aDb[i].pBt);
66908 }
66909 }
66910 }
66911 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
66912 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
66913 vdbeLeave(p);
66914 }
66915 #endif
66916
66917 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
66918 /*
@@ -67775,19 +68606,25 @@
68606 n += pMem->u.nZero;
68607 }
68608 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
68609 }
68610
68611 /*
68612 ** The sizes for serial types less than 12
68613 */
68614 static const u8 sqlite3SmallTypeSizes[] = {
68615 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0
68616 };
68617
68618 /*
68619 ** Return the length of the data corresponding to the supplied serial-type.
68620 */
68621 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
68622 if( serial_type>=12 ){
68623 return (serial_type-12)/2;
68624 }else{
68625 return sqlite3SmallTypeSizes[serial_type];
 
68626 }
68627 }
68628
68629 /*
68630 ** If we are on an architecture with mixed-endian floating
@@ -67867,11 +68704,11 @@
68704 memcpy(&v, &pMem->u.r, sizeof(v));
68705 swapMixedEndianFloat(v);
68706 }else{
68707 v = pMem->u.i;
68708 }
68709 len = i = sqlite3SmallTypeSizes[serial_type];
68710 assert( i>0 );
68711 do{
68712 buf[--i] = (u8)(v&0xFF);
68713 v >>= 8;
68714 }while( i );
@@ -68896,11 +69733,11 @@
69733 testcase( typeRowid==8 );
69734 testcase( typeRowid==9 );
69735 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
69736 goto idx_rowid_corruption;
69737 }
69738 lenRowid = sqlite3SmallTypeSizes[typeRowid];
69739 testcase( (u32)m.n==szHdr+lenRowid );
69740 if( unlikely((u32)m.n<szHdr+lenRowid) ){
69741 goto idx_rowid_corruption;
69742 }
69743
@@ -71122,11 +71959,11 @@
71959 ** an integer representation is more space efficient on disk.
71960 **
71961 ** SQLITE_AFF_TEXT:
71962 ** Convert pRec to a text representation.
71963 **
71964 ** SQLITE_AFF_BLOB:
71965 ** No-op. pRec is unchanged.
71966 */
71967 static void applyAffinity(
71968 Mem *pRec, /* The value to apply affinity to */
71969 char affinity, /* The affinity to be applied */
@@ -71523,17 +72360,13 @@
72360 db->busyHandler.nBusy = 0;
72361 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
72362 sqlite3VdbeIOTraceSql(p);
72363 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
72364 if( db->xProgress ){
72365 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
72366 assert( 0 < db->nProgressOps );
72367 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
 
 
 
 
 
72368 }
72369 #endif
72370 #ifdef SQLITE_DEBUG
72371 sqlite3BeginBenignMalloc();
72372 if( p->pc==0
@@ -72491,14 +73324,14 @@
73324 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
73325 }
73326 break;
73327 }
73328
73329 /* Opcode: Function0 P1 P2 P3 P4 P5
73330 ** Synopsis: r[P3]=func(r[P2@P5])
73331 **
73332 ** Invoke a user function (P4 is a pointer to a FuncDef object that
73333 ** defines the function) with P5 arguments taken from register P2 and
73334 ** successors. The result of the function is stored in register P3.
73335 ** Register P3 must not be one of the function inputs.
73336 **
73337 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
@@ -72506,63 +73339,104 @@
73339 ** argument was constant then bit 0 of P1 is set. This is used to determine
73340 ** whether meta data associated with a user function argument using the
73341 ** sqlite3_set_auxdata() API may be safely retained until the next
73342 ** invocation of this opcode.
73343 **
73344 ** See also: Function, AggStep, AggFinal
73345 */
73346 /* Opcode: Function P1 P2 P3 P4 P5
73347 ** Synopsis: r[P3]=func(r[P2@P5])
73348 **
73349 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
73350 ** contains a pointer to the function to be run) with P5 arguments taken
73351 ** from register P2 and successors. The result of the function is stored
73352 ** in register P3. Register P3 must not be one of the function inputs.
73353 **
73354 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
73355 ** function was determined to be constant at compile time. If the first
73356 ** argument was constant then bit 0 of P1 is set. This is used to determine
73357 ** whether meta data associated with a user function argument using the
73358 ** sqlite3_set_auxdata() API may be safely retained until the next
73359 ** invocation of this opcode.
73360 **
73361 ** SQL functions are initially coded as OP_Function0 with P4 pointing
73362 ** to a FuncDef object. But on first evaluation, the P4 operand is
73363 ** automatically converted into an sqlite3_context object and the operation
73364 ** changed to this OP_Function opcode. In this way, the initialization of
73365 ** the sqlite3_context object occurs only once, rather than once for each
73366 ** evaluation of the function.
73367 **
73368 ** See also: Function0, AggStep, AggFinal
73369 */
73370 case OP_Function0: {
73371 int n;
73372 sqlite3_context *pCtx;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73373
73374 assert( pOp->p4type==P4_FUNCDEF );
73375 n = pOp->p5;
73376 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
73377 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
73378 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
73379 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
73380 if( pCtx==0 ) goto no_mem;
73381 pCtx->pOut = 0;
73382 pCtx->pFunc = pOp->p4.pFunc;
73383 pCtx->iOp = (int)(pOp - aOp);
73384 pCtx->pVdbe = p;
73385 pCtx->argc = n;
73386 pOp->p4type = P4_FUNCCTX;
73387 pOp->p4.pCtx = pCtx;
73388 pOp->opcode = OP_Function;
73389 /* Fall through into OP_Function */
73390 }
73391 case OP_Function: {
73392 int i;
73393 sqlite3_context *pCtx;
73394
73395 assert( pOp->p4type==P4_FUNCCTX );
73396 pCtx = pOp->p4.pCtx;
73397
73398 /* If this function is inside of a trigger, the register array in aMem[]
73399 ** might change from one evaluation to the next. The next block of code
73400 ** checks to see if the register array has changed, and if so it
73401 ** reinitializes the relavant parts of the sqlite3_context object */
73402 pOut = &aMem[pOp->p3];
73403 if( pCtx->pOut != pOut ){
73404 pCtx->pOut = pOut;
73405 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
73406 }
73407
73408 memAboutToChange(p, pCtx->pOut);
73409 #ifdef SQLITE_DEBUG
73410 for(i=0; i<pCtx->argc; i++){
73411 assert( memIsValid(pCtx->argv[i]) );
73412 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
73413 }
73414 #endif
73415 MemSetTypeFlag(pCtx->pOut, MEM_Null);
73416 pCtx->fErrorOrAux = 0;
73417 db->lastRowid = lastRowid;
73418 (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */
73419 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
73420
73421 /* If the function returned an error, throw an exception */
73422 if( pCtx->fErrorOrAux ){
73423 if( pCtx->isError ){
73424 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
73425 rc = pCtx->isError;
73426 }
73427 sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1);
73428 }
73429
73430 /* Copy the result of the function into register P3 */
73431 if( pOut->flags & (MEM_Str|MEM_Blob) ){
73432 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
73433 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
73434 }
73435
73436 REGISTER_TRACE(pOp->p3, pCtx->pOut);
73437 UPDATE_MAX_BLOBSIZE(pCtx->pOut);
73438 break;
73439 }
73440
73441 /* Opcode: BitAnd P1 P2 P3 * *
73442 ** Synopsis: r[P3]=r[P1]&r[P2]
@@ -72721,13 +73595,13 @@
73595 ** </ul>
73596 **
73597 ** A NULL value is not changed by this routine. It remains NULL.
73598 */
73599 case OP_Cast: { /* in1 */
73600 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
73601 testcase( pOp->p2==SQLITE_AFF_TEXT );
73602 testcase( pOp->p2==SQLITE_AFF_BLOB );
73603 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
73604 testcase( pOp->p2==SQLITE_AFF_INTEGER );
73605 testcase( pOp->p2==SQLITE_AFF_REAL );
73606 pIn1 = &aMem[pOp->p1];
73607 memAboutToChange(p, pIn1);
@@ -73534,11 +74408,11 @@
74408 ** field of the index key.
74409 **
74410 ** The mapping from character to affinity is given by the SQLITE_AFF_
74411 ** macros defined in sqliteInt.h.
74412 **
74413 ** If P4 is NULL then all index fields have the affinity BLOB.
74414 */
74415 case OP_MakeRecord: {
74416 u8 *zNewRecord; /* A buffer to hold the data for the new record */
74417 Mem *pRec; /* The new record */
74418 u64 nData; /* Number of bytes of data space */
@@ -74451,10 +75325,30 @@
75325 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
75326 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
75327 p->apCsr[pOp->p1] = 0;
75328 break;
75329 }
75330
75331 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
75332 /* Opcode: ColumnsUsed P1 * * P4 *
75333 **
75334 ** This opcode (which only exists if SQLite was compiled with
75335 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
75336 ** table or index for cursor P1 are used. P4 is a 64-bit integer
75337 ** (P4_INT64) in which the first 63 bits are one for each of the
75338 ** first 63 columns of the table or index that are actually used
75339 ** by the cursor. The high-order bit is set if any column after
75340 ** the 64th is used.
75341 */
75342 case OP_ColumnsUsed: {
75343 VdbeCursor *pC;
75344 pC = p->apCsr[pOp->p1];
75345 assert( pC->pCursor );
75346 pC->maskUsed = *(u64*)pOp->p4.pI64;
75347 break;
75348 }
75349 #endif
75350
75351 /* Opcode: SeekGE P1 P2 P3 P4 *
75352 ** Synopsis: key=r[P3@P4]
75353 **
75354 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -74940,13 +75834,12 @@
75834 res = 0;
75835 pOut = out2Prerelease(p, pOp);
75836 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
75837 pC = p->apCsr[pOp->p1];
75838 assert( pC!=0 );
75839 assert( pC->pCursor!=0 );
75840 {
 
75841 /* The next rowid or record number (different terms for the same
75842 ** thing) is obtained in a two-step algorithm.
75843 **
75844 ** First we attempt to find the largest existing rowid and add one
75845 ** to that. But if the largest existing rowid is already the maximum
@@ -75681,32 +76574,30 @@
76574 ** for tables is OP_Insert.
76575 */
76576 case OP_SorterInsert: /* in2 */
76577 case OP_IdxInsert: { /* in2 */
76578 VdbeCursor *pC;
 
76579 int nKey;
76580 const char *zKey;
76581
76582 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76583 pC = p->apCsr[pOp->p1];
76584 assert( pC!=0 );
76585 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
76586 pIn2 = &aMem[pOp->p2];
76587 assert( pIn2->flags & MEM_Blob );
 
76588 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
76589 assert( pC->pCursor!=0 );
76590 assert( pC->isTable==0 );
76591 rc = ExpandBlob(pIn2);
76592 if( rc==SQLITE_OK ){
76593 if( pOp->opcode==OP_SorterInsert ){
76594 rc = sqlite3VdbeSorterWrite(pC, pIn2);
76595 }else{
76596 nKey = pIn2->n;
76597 zKey = pIn2->z;
76598 rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, "", 0, 0, pOp->p3,
76599 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
76600 );
76601 assert( pC->deferredMoveto==0 );
76602 pC->cacheStatus = CACHE_STALE;
76603 }
@@ -76622,61 +77513,105 @@
77513 VdbeBranchTaken(pIn1->u.i==0, 2);
77514 if( (pIn1->u.i++)==0 ) goto jump_to_p2;
77515 break;
77516 }
77517
77518 /* Opcode: AggStep0 * P2 P3 P4 P5
77519 ** Synopsis: accum=r[P3] step(r[P2@P5])
77520 **
77521 ** Execute the step function for an aggregate. The
77522 ** function has P5 arguments. P4 is a pointer to the FuncDef
77523 ** structure that specifies the function. Register P3 is the
77524 ** accumulator.
77525 **
77526 ** The P5 arguments are taken from register P2 and its
77527 ** successors.
77528 */
77529 /* Opcode: AggStep * P2 P3 P4 P5
77530 ** Synopsis: accum=r[P3] step(r[P2@P5])
77531 **
77532 ** Execute the step function for an aggregate. The
77533 ** function has P5 arguments. P4 is a pointer to an sqlite3_context
77534 ** object that is used to run the function. Register P3 is
77535 ** as the accumulator.
77536 **
77537 ** The P5 arguments are taken from register P2 and its
77538 ** successors.
77539 **
77540 ** This opcode is initially coded as OP_AggStep0. On first evaluation,
77541 ** the FuncDef stored in P4 is converted into an sqlite3_context and
77542 ** the opcode is changed. In this way, the initialization of the
77543 ** sqlite3_context only happens once, instead of on each call to the
77544 ** step function.
77545 */
77546 case OP_AggStep0: {
77547 int n;
77548 sqlite3_context *pCtx;
 
 
 
 
 
77549
77550 assert( pOp->p4type==P4_FUNCDEF );
77551 n = pOp->p5;
 
 
 
 
 
 
 
 
 
 
77552 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
77553 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
77554 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
77555 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
77556 if( pCtx==0 ) goto no_mem;
77557 pCtx->pMem = 0;
77558 pCtx->pFunc = pOp->p4.pFunc;
77559 pCtx->iOp = (int)(pOp - aOp);
77560 pCtx->pVdbe = p;
77561 pCtx->argc = n;
77562 pOp->p4type = P4_FUNCCTX;
77563 pOp->p4.pCtx = pCtx;
77564 pOp->opcode = OP_AggStep;
77565 /* Fall through into OP_AggStep */
77566 }
77567 case OP_AggStep: {
77568 int i;
77569 sqlite3_context *pCtx;
77570 Mem *pMem;
77571 Mem t;
77572
77573 assert( pOp->p4type==P4_FUNCCTX );
77574 pCtx = pOp->p4.pCtx;
77575 pMem = &aMem[pOp->p3];
77576
77577 /* If this function is inside of a trigger, the register array in aMem[]
77578 ** might change from one evaluation to the next. The next block of code
77579 ** checks to see if the register array has changed, and if so it
77580 ** reinitializes the relavant parts of the sqlite3_context object */
77581 if( pCtx->pMem != pMem ){
77582 pCtx->pMem = pMem;
77583 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
77584 }
77585
77586 #ifdef SQLITE_DEBUG
77587 for(i=0; i<pCtx->argc; i++){
77588 assert( memIsValid(pCtx->argv[i]) );
77589 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
77590 }
77591 #endif
77592
77593 pMem->n++;
77594 sqlite3VdbeMemInit(&t, db, MEM_Null);
77595 pCtx->pOut = &t;
77596 pCtx->fErrorOrAux = 0;
77597 pCtx->skipFlag = 0;
77598 (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
77599 if( pCtx->fErrorOrAux ){
77600 if( pCtx->isError ){
77601 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
77602 rc = pCtx->isError;
77603 }
77604 sqlite3VdbeMemRelease(&t);
77605 }else{
77606 assert( t.flags==MEM_Null );
77607 }
77608 if( pCtx->skipFlag ){
77609 assert( pOp[-1].opcode==OP_CollSeq );
77610 i = pOp[-1].p1;
77611 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
77612 }
 
77613 break;
77614 }
77615
77616 /* Opcode: AggFinal P1 P2 * P4 *
77617 ** Synopsis: accum=r[P1] N=P2
@@ -82721,10 +83656,17 @@
83656 "the GROUP BY clause");
83657 return WRC_Abort;
83658 }
83659 }
83660 }
83661
83662 /* If this is part of a compound SELECT, check that it has the right
83663 ** number of expressions in the select list. */
83664 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
83665 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
83666 return WRC_Abort;
83667 }
83668
83669 /* Advance to the next term of the compound
83670 */
83671 p = p->pPrior;
83672 nCompound++;
@@ -83089,17 +84031,17 @@
84031 ** affinity, use that. Otherwise use no affinity.
84032 */
84033 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
84034 return SQLITE_AFF_NUMERIC;
84035 }else{
84036 return SQLITE_AFF_BLOB;
84037 }
84038 }else if( !aff1 && !aff2 ){
84039 /* Neither side of the comparison is a column. Compare the
84040 ** results directly.
84041 */
84042 return SQLITE_AFF_BLOB;
84043 }else{
84044 /* One side is a column, the other is not. Use the columns affinity. */
84045 assert( aff1==0 || aff2==0 );
84046 return (aff1 + aff2);
84047 }
@@ -83119,11 +84061,11 @@
84061 if( pExpr->pRight ){
84062 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
84063 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
84064 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
84065 }else if( !aff ){
84066 aff = SQLITE_AFF_BLOB;
84067 }
84068 return aff;
84069 }
84070
84071 /*
@@ -83133,11 +84075,11 @@
84075 ** the comparison in pExpr.
84076 */
84077 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
84078 char aff = comparisonAffinity(pExpr);
84079 switch( aff ){
84080 case SQLITE_AFF_BLOB:
84081 return 1;
84082 case SQLITE_AFF_TEXT:
84083 return idx_affinity==SQLITE_AFF_TEXT;
84084 default:
84085 return sqlite3IsNumericAffinity(idx_affinity);
@@ -83939,11 +84881,11 @@
84881 pNewItem->addrFillSub = pOldItem->addrFillSub;
84882 pNewItem->regReturn = pOldItem->regReturn;
84883 pNewItem->isCorrelated = pOldItem->isCorrelated;
84884 pNewItem->viaCoroutine = pOldItem->viaCoroutine;
84885 pNewItem->isRecursive = pOldItem->isRecursive;
84886 pNewItem->zIndexedBy = sqlite3DbStrDup(db, pOldItem->zIndexedBy);
84887 pNewItem->notIndexed = pOldItem->notIndexed;
84888 pNewItem->pIndex = pOldItem->pIndex;
84889 pTab = pNewItem->pTab = pOldItem->pTab;
84890 if( pTab ){
84891 pTab->nRef++;
@@ -84166,11 +85108,11 @@
85108 **
85109 ** These callback routines are used to implement the following:
85110 **
85111 ** sqlite3ExprIsConstant() pWalker->eCode==1
85112 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
85113 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
85114 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
85115 **
85116 ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
85117 ** is found to not be a constant.
85118 **
@@ -84274,11 +85216,11 @@
85216 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
85217 return exprIsConst(p, 2, 0);
85218 }
85219
85220 /*
85221 ** Walk an expression tree. Return non-zero if the expression is constant
85222 ** for any single row of the table with cursor iCur. In other words, the
85223 ** expression must not refer to any non-deterministic function nor any
85224 ** table other than iCur.
85225 */
85226 SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
@@ -84380,11 +85322,11 @@
85322 ** is harmless. A false positive, however, can result in the wrong
85323 ** answer.
85324 */
85325 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
85326 u8 op;
85327 if( aff==SQLITE_AFF_BLOB ) return 1;
85328 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
85329 op = p->op;
85330 if( op==TK_REGISTER ) op = p->op2;
85331 switch( op ){
85332 case TK_INTEGER: {
@@ -84831,11 +85773,11 @@
85773 ExprList *pList = pExpr->x.pList;
85774 struct ExprList_item *pItem;
85775 int r1, r2, r3;
85776
85777 if( !affinity ){
85778 affinity = SQLITE_AFF_BLOB;
85779 }
85780 if( pKeyInfo ){
85781 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
85782 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
85783 }
@@ -85106,21 +86048,10 @@
86048 sqlite3ExprCachePop(pParse);
86049 VdbeComment((v, "end IN expr"));
86050 }
86051 #endif /* SQLITE_OMIT_SUBQUERY */
86052
 
 
 
 
 
 
 
 
 
 
 
86053 #ifndef SQLITE_OMIT_FLOATING_POINT
86054 /*
86055 ** Generate an instruction that will put the floating point
86056 ** value described by z[0..n-1] into register iMem.
86057 **
@@ -85129,16 +86060,14 @@
86060 ** like the continuation of the number.
86061 */
86062 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
86063 if( ALWAYS(z!=0) ){
86064 double value;
 
86065 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
86066 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
86067 if( negateFlag ) value = -value;
86068 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
 
86069 }
86070 }
86071 #endif
86072
86073
@@ -85160,14 +86089,12 @@
86089 i64 value;
86090 const char *z = pExpr->u.zToken;
86091 assert( z!=0 );
86092 c = sqlite3DecOrHexToI64(z, &value);
86093 if( c==0 || (c==2 && negFlag) ){
 
86094 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
86095 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
 
86096 }else{
86097 #ifdef SQLITE_OMIT_FLOATING_POINT
86098 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
86099 #else
86100 #ifndef SQLITE_OMIT_HEX_INTEGER
@@ -85768,11 +86695,11 @@
86695 /* The UNLIKELY() function is a no-op. The result is the value
86696 ** of the first argument.
86697 */
86698 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
86699 assert( nFarg>=1 );
86700 inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
86701 break;
86702 }
86703
86704 for(i=0; i<nFarg; i++){
86705 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
@@ -85838,11 +86765,11 @@
86765 #endif
86766 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
86767 if( !pColl ) pColl = db->pDfltColl;
86768 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
86769 }
86770 sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
86771 (char*)pDef, P4_FUNCDEF);
86772 sqlite3VdbeChangeP5(v, (u8)nFarg);
86773 if( nFarg && constMask==0 ){
86774 sqlite3ReleaseTempRange(pParse, r1, nFarg);
86775 }
@@ -86209,272 +87136,10 @@
87136 iMem = ++pParse->nMem;
87137 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
87138 exprToRegister(pExpr, iMem);
87139 }
87140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87141 /*
87142 ** Generate code that pushes the value of every element of the given
87143 ** expression list into a sequence of registers beginning at target.
87144 **
87145 ** Return the number of elements evaluated.
@@ -86861,10 +87526,25 @@
87526 }
87527 }
87528 sqlite3ReleaseTempReg(pParse, regFree1);
87529 sqlite3ReleaseTempReg(pParse, regFree2);
87530 }
87531
87532 /*
87533 ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
87534 ** code generation, and that copy is deleted after code generation. This
87535 ** ensures that the original pExpr is unchanged.
87536 */
87537 SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
87538 sqlite3 *db = pParse->db;
87539 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
87540 if( db->mallocFailed==0 ){
87541 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
87542 }
87543 sqlite3ExprDelete(db, pCopy);
87544 }
87545
87546
87547 /*
87548 ** Do a deep comparison of two expression trees. Return 0 if the two
87549 ** expressions are completely identical. Return 1 if they differ only
87550 ** by a COLLATE operator at the top level. Return 2 if there are differences
@@ -88014,11 +88694,11 @@
88694 ** can handle (i.e. not CURRENT_TIME etc.)
88695 */
88696 if( pDflt ){
88697 sqlite3_value *pVal = 0;
88698 int rc;
88699 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
88700 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
88701 if( rc!=SQLITE_OK ){
88702 db->mallocFailed = 1;
88703 return;
88704 }
@@ -89099,11 +89779,11 @@
89779 #elif SQLITE_DEBUG
89780 assert( iParam==STAT_GET_STAT1 );
89781 #else
89782 UNUSED_PARAMETER( iParam );
89783 #endif
89784 sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4, regOut);
89785 sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
89786 sqlite3VdbeChangeP5(v, 1 + IsStat34);
89787 }
89788
89789 /*
@@ -89254,11 +89934,11 @@
89934 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89935 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
89936 #endif
89937 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
89938 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
89939 sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4+1, regStat4);
89940 sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
89941 sqlite3VdbeChangeP5(v, 2+IsStat34);
89942
89943 /* Implementation of the following:
89944 **
@@ -89350,11 +90030,11 @@
90030 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
90031 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
90032 }
90033 #endif
90034 assert( regChng==(regStat4+1) );
90035 sqlite3VdbeAddOp3(v, OP_Function0, 1, regStat4, regTemp);
90036 sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
90037 sqlite3VdbeChangeP5(v, 2+IsStat34);
90038 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
90039
90040 /* Add the entry to the stat1 table. */
@@ -90409,11 +91089,11 @@
91089 sqlite3ExprCode(pParse, pDbname, regArgs+1);
91090 sqlite3ExprCode(pParse, pKey, regArgs+2);
91091
91092 assert( v || db->mallocFailed );
91093 if( v ){
91094 sqlite3VdbeAddOp3(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3);
91095 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
91096 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
91097 sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
91098
91099 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
@@ -91874,11 +92554,11 @@
92554 */
92555 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
92556 int j1;
92557 int fileFormat;
92558 int reg1, reg2, reg3;
92559 sqlite3BeginWriteOperation(pParse, 1, iDb);
92560
92561 #ifndef SQLITE_OMIT_VIRTUALTABLE
92562 if( isVirtual ){
92563 sqlite3VdbeAddOp0(v, OP_VBegin);
92564 }
@@ -91990,14 +92670,14 @@
92670 pCol = &p->aCol[p->nCol];
92671 memset(pCol, 0, sizeof(p->aCol[0]));
92672 pCol->zName = z;
92673
92674 /* If there is no type specified, columns have the default affinity
92675 ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will
92676 ** be called next to set pCol->affinity correctly.
92677 */
92678 pCol->affinity = SQLITE_AFF_BLOB;
92679 pCol->szEst = 1;
92680 p->nCol++;
92681 }
92682
92683 /*
@@ -92028,11 +92708,11 @@
92708 ** --------------------------------
92709 ** 'INT' | SQLITE_AFF_INTEGER
92710 ** 'CHAR' | SQLITE_AFF_TEXT
92711 ** 'CLOB' | SQLITE_AFF_TEXT
92712 ** 'TEXT' | SQLITE_AFF_TEXT
92713 ** 'BLOB' | SQLITE_AFF_BLOB
92714 ** 'REAL' | SQLITE_AFF_REAL
92715 ** 'FLOA' | SQLITE_AFF_REAL
92716 ** 'DOUB' | SQLITE_AFF_REAL
92717 **
92718 ** If none of the substrings in the above table are found,
@@ -92054,11 +92734,11 @@
92734 aff = SQLITE_AFF_TEXT;
92735 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
92736 aff = SQLITE_AFF_TEXT;
92737 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
92738 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
92739 aff = SQLITE_AFF_BLOB;
92740 if( zIn[0]=='(' ) zChar = zIn;
92741 #ifndef SQLITE_OMIT_FLOATING_POINT
92742 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
92743 && aff==SQLITE_AFF_NUMERIC ){
92744 aff = SQLITE_AFF_REAL;
@@ -92446,11 +93126,11 @@
93126 k = sqlite3Strlen30(zStmt);
93127 identPut(zStmt, &k, p->zName);
93128 zStmt[k++] = '(';
93129 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
93130 static const char * const azType[] = {
93131 /* SQLITE_AFF_BLOB */ "",
93132 /* SQLITE_AFF_TEXT */ " TEXT",
93133 /* SQLITE_AFF_NUMERIC */ " NUM",
93134 /* SQLITE_AFF_INTEGER */ " INT",
93135 /* SQLITE_AFF_REAL */ " REAL"
93136 };
@@ -92459,21 +93139,21 @@
93139
93140 sqlite3_snprintf(n-k, &zStmt[k], zSep);
93141 k += sqlite3Strlen30(&zStmt[k]);
93142 zSep = zSep2;
93143 identPut(zStmt, &k, pCol->zName);
93144 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
93145 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
93146 testcase( pCol->affinity==SQLITE_AFF_BLOB );
93147 testcase( pCol->affinity==SQLITE_AFF_TEXT );
93148 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
93149 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
93150 testcase( pCol->affinity==SQLITE_AFF_REAL );
93151
93152 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
93153 len = sqlite3Strlen30(zType);
93154 assert( pCol->affinity==SQLITE_AFF_BLOB
93155 || pCol->affinity==sqlite3AffinityType(zType, 0) );
93156 memcpy(&zStmt[k], zType, len);
93157 k += len;
93158 assert( k<=n );
93159 }
@@ -92822,10 +93502,11 @@
93502
93503 regYield = ++pParse->nMem;
93504 regRec = ++pParse->nMem;
93505 regRowid = ++pParse->nMem;
93506 assert(pParse->nTab==1);
93507 sqlite3MayAbort(pParse);
93508 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
93509 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
93510 pParse->nTab = 2;
93511 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
93512 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
@@ -94599,11 +95280,11 @@
95280 if( pList==0 ) return;
95281 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
95282 sqlite3DbFree(db, pItem->zDatabase);
95283 sqlite3DbFree(db, pItem->zName);
95284 sqlite3DbFree(db, pItem->zAlias);
95285 sqlite3DbFree(db, pItem->zIndexedBy);
95286 sqlite3DeleteTable(db, pItem->pTab);
95287 sqlite3SelectDelete(db, pItem->pSelect);
95288 sqlite3ExprDelete(db, pItem->pOn);
95289 sqlite3IdListDelete(db, pItem->pUsing);
95290 }
@@ -94672,17 +95353,17 @@
95353 */
95354 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
95355 assert( pIndexedBy!=0 );
95356 if( p && ALWAYS(p->nSrc>0) ){
95357 struct SrcList_item *pItem = &p->a[p->nSrc-1];
95358 assert( pItem->notIndexed==0 && pItem->zIndexedBy==0 );
95359 if( pIndexedBy->n==1 && !pIndexedBy->z ){
95360 /* A "NOT INDEXED" clause was supplied. See parse.y
95361 ** construct "indexed_opt" for details. */
95362 pItem->notIndexed = 1;
95363 }else{
95364 pItem->zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
95365 }
95366 }
95367 }
95368
95369 /*
@@ -96502,12 +97183,12 @@
97183 if( piPartIdxLabel ){
97184 if( pIdx->pPartIdxWhere ){
97185 *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
97186 pParse->iPartIdxTab = iDataCur;
97187 sqlite3ExprCachePush(pParse);
97188 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
97189 SQLITE_JUMPIFNULL);
97190 }else{
97191 *piPartIdxLabel = 0;
97192 }
97193 }
97194 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
@@ -97119,21 +97800,19 @@
97800 u8 noCase;
97801 };
97802
97803 /*
97804 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
97805 ** character is exactly one byte in size. Also, provde the Utf8Read()
97806 ** macro for fast reading of the next character in the common case where
97807 ** the next character is ASCII.
97808 */
97809 #if defined(SQLITE_EBCDIC)
97810 # define sqlite3Utf8Read(A) (*((*A)++))
97811 # define Utf8Read(A) (*(A++))
 
97812 #else
97813 # define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
 
97814 #endif
97815
97816 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
97817 /* The correct SQL-92 behavior is for the LIKE operator to ignore
97818 ** case. Thus 'a' LIKE 'A' would be true. */
@@ -97171,11 +97850,11 @@
97850 *** '_' Matches any one character
97851 **
97852 ** Ec Where E is the "esc" character and c is any other
97853 ** character, including '%', '_', and esc, match exactly c.
97854 **
97855 ** The comments within this routine usually assume glob matching.
97856 **
97857 ** This routine is usually quick, but can be N**2 in the worst case.
97858 */
97859 static int patternCompare(
97860 const u8 *zPattern, /* The glob pattern */
@@ -97195,17 +97874,16 @@
97874 ** the other, never both. Hence the single variable matchOther is used
97875 ** to store the one we have to look for.
97876 */
97877 matchOther = esc ? esc : pInfo->matchSet;
97878
97879 while( (c = Utf8Read(zPattern))!=0 ){
97880 if( c==matchAll ){ /* Match "*" */
97881 /* Skip over multiple "*" characters in the pattern. If there
97882 ** are also "?" characters, skip those as well, but consume a
97883 ** single character of the input string for each "?" skipped */
97884 while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
 
97885 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
97886 return 0;
97887 }
97888 }
97889 if( c==0 ){
@@ -97246,11 +97924,11 @@
97924 while( (c2 = *(zString++))!=0 ){
97925 if( c2!=c && c2!=cx ) continue;
97926 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
97927 }
97928 }else{
97929 while( (c2 = Utf8Read(zString))!=0 ){
97930 if( c2!=c ) continue;
97931 if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
97932 }
97933 }
97934 return 0;
@@ -97292,11 +97970,11 @@
97970 return 0;
97971 }
97972 continue;
97973 }
97974 }
97975 c2 = Utf8Read(zString);
97976 if( c==c2 ) continue;
97977 if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
97978 continue;
97979 }
97980 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
@@ -99806,11 +100484,11 @@
100484 ** pIdx. A column affinity string has one character for each column in
100485 ** the table, according to the affinity of the column:
100486 **
100487 ** Character Column affinity
100488 ** ------------------------------
100489 ** 'A' BLOB
100490 ** 'B' TEXT
100491 ** 'C' NUMERIC
100492 ** 'D' INTEGER
100493 ** 'F' REAL
100494 **
@@ -99849,23 +100527,23 @@
100527 return pIdx->zColAff;
100528 }
100529
100530 /*
100531 ** Compute the affinity string for table pTab, if it has not already been
100532 ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
100533 **
100534 ** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
100535 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
100536 ** for register iReg and following. Or if affinities exists and iReg==0,
100537 ** then just set the P4 operand of the previous opcode (which should be
100538 ** an OP_MakeRecord) to the affinity string.
100539 **
100540 ** A column affinity string has one character per column:
100541 **
100542 ** Character Column affinity
100543 ** ------------------------------
100544 ** 'A' BLOB
100545 ** 'B' TEXT
100546 ** 'C' NUMERIC
100547 ** 'D' INTEGER
100548 ** 'E' REAL
100549 */
@@ -99883,11 +100561,11 @@
100561 for(i=0; i<pTab->nCol; i++){
100562 zColAff[i] = pTab->aCol[i].affinity;
100563 }
100564 do{
100565 zColAff[i--] = 0;
100566 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
100567 pTab->zColAff = zColAff;
100568 }
100569 i = sqlite3Strlen30(zColAff);
100570 if( i ){
100571 if( iReg ){
@@ -101131,12 +101809,12 @@
101809
101810 /* Skip partial indices for which the WHERE clause is not true */
101811 if( pIdx->pPartIdxWhere ){
101812 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
101813 pParse->ckBase = regNewData+1;
101814 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
101815 SQLITE_JUMPIFNULL);
101816 pParse->ckBase = 0;
101817 }
101818
101819 /* Create a record for this index entry as it should appear after
101820 ** the insert or update. Store that record in the aRegIdx[ix] register
@@ -102242,11 +102920,12 @@
102920 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
102921 void(*)(void*));
102922 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
102923 void(*)(void*), unsigned char);
102924 int (*strglob)(const char*,const char*);
102925 /* Version 3.8.11 and later */
102926 sqlite3_value *(*value_dup)(const sqlite3_value*);
102927 void (*value_free)(sqlite3_value*);
102928 };
102929
102930 /*
102931 ** The following macros redefine the API routines so that they are
@@ -102882,11 +103561,14 @@
103561 sqlite3_msize,
103562 sqlite3_realloc64,
103563 sqlite3_reset_auto_extension,
103564 sqlite3_result_blob64,
103565 sqlite3_result_text64,
103566 sqlite3_strglob,
103567 /* Version 3.8.11 and later */
103568 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
103569 sqlite3_value_free
103570 };
103571
103572 /*
103573 ** Attempt to load an SQLite extension library contained in the file
103574 ** zFile. The entry point is zProc. zProc may be 0 in which case a
@@ -106617,11 +107299,12 @@
107299 */
107300 #if SELECTTRACE_ENABLED
107301 /***/ int sqlite3SelectTrace = 0;
107302 # define SELECTTRACE(K,P,S,X) \
107303 if(sqlite3SelectTrace&(K)) \
107304 sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
107305 (S)->zSelName,(S)),\
107306 sqlite3DebugPrintf X
107307 #else
107308 # define SELECTTRACE(K,P,S,X)
107309 #endif
107310
@@ -106961,10 +107644,16 @@
107644 while( p ){
107645 ExprSetProperty(p, EP_FromJoin);
107646 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
107647 ExprSetVVAProperty(p, EP_NoReduce);
107648 p->iRightJoinTable = (i16)iTable;
107649 if( p->op==TK_FUNCTION && p->x.pList ){
107650 int i;
107651 for(i=0; i<p->x.pList->nExpr; i++){
107652 setJoinExpr(p->x.pList->a[i].pExpr, iTable);
107653 }
107654 }
107655 setJoinExpr(p->pLeft, iTable);
107656 p = p->pRight;
107657 }
107658 }
107659
@@ -107370,11 +108059,12 @@
108059 break;
108060 }
108061
108062 default: {
108063 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
108064 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
108065 regResult);
108066 break;
108067 }
108068 }
108069 if( pSort==0 ){
108070 codeOffset(v, p->iOffset, iContinue);
@@ -107423,11 +108113,12 @@
108113 ** on an ephemeral index. If the current row is already present
108114 ** in the index, do not write it to the output. If not, add the
108115 ** current row to the index and proceed with writing it to the
108116 ** output table as well. */
108117 int addr = sqlite3VdbeCurrentAddr(v) + 4;
108118 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
108119 VdbeCoverage(v);
108120 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
108121 assert( pSort==0 );
108122 }
108123 #endif
108124 if( pSort ){
@@ -107906,32 +108597,31 @@
108597 ** This routine has either 3 or 6 parameters depending on whether or not
108598 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
108599 */
108600 #ifdef SQLITE_ENABLE_COLUMN_METADATA
108601 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
108602 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
108603 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
108604 #endif
108605 static const char *columnTypeImpl(
108606 NameContext *pNC,
108607 Expr *pExpr,
108608 #ifdef SQLITE_ENABLE_COLUMN_METADATA
108609 const char **pzOrigDb,
108610 const char **pzOrigTab,
108611 const char **pzOrigCol,
108612 #endif
108613 u8 *pEstWidth
108614 ){
108615 char const *zType = 0;
108616 int j;
108617 u8 estWidth = 1;
108618 #ifdef SQLITE_ENABLE_COLUMN_METADATA
108619 char const *zOrigDb = 0;
108620 char const *zOrigTab = 0;
108621 char const *zOrigCol = 0;
108622 #endif
 
 
 
 
 
 
 
 
 
 
108623
108624 if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
108625 switch( pExpr->op ){
108626 case TK_AGG_COLUMN:
108627 case TK_COLUMN: {
@@ -107980,14 +108670,17 @@
108670 if( pS ){
108671 /* The "table" is actually a sub-select or a view in the FROM clause
108672 ** of the SELECT statement. Return the declaration type and origin
108673 ** data for the result-set column of the sub-select.
108674 */
108675 if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
108676 /* If iCol is less than zero, then the expression requests the
108677 ** rowid of the sub-select or view. This expression is legal (see
108678 ** test case misc2.2.2) - it always evaluates to NULL.
108679 **
108680 ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been
108681 ** caught already by name resolution.
108682 */
108683 NameContext sNC;
108684 Expr *p = pS->pEList->a[iCol].pExpr;
108685 sNC.pSrcList = pS->pSrc;
108686 sNC.pNext = pNC;
@@ -108301,15 +108994,16 @@
108994 sNC.pSrcList = pSelect->pSrc;
108995 a = pSelect->pEList->a;
108996 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
108997 p = a[i].pExpr;
108998 if( pCol->zType==0 ){
108999 pCol->zType = sqlite3DbStrDup(db,
109000 columnType(&sNC, p,0,0,0, &pCol->szEst));
109001 }
109002 szAll += pCol->szEst;
109003 pCol->affinity = sqlite3ExprAffinity(p);
109004 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
109005 pColl = sqlite3ExprCollSeq(pParse, p);
109006 if( pColl && pCol->zColl==0 ){
109007 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
109008 }
109009 }
@@ -108461,11 +109155,14 @@
109155 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
109156 }else{
109157 pRet = 0;
109158 }
109159 assert( iCol>=0 );
109160 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
109161 ** have been thrown during name resolution and we would not have gotten
109162 ** this far */
109163 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
109164 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
109165 }
109166 return pRet;
109167 }
109168
@@ -108680,11 +109377,11 @@
109377
109378 /*
109379 ** Error message for when two or more terms of a compound select have different
109380 ** size result sets.
109381 */
109382 SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
109383 if( p->selFlags & SF_Values ){
109384 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
109385 }else{
109386 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
109387 " do not have the same number of result columns", selectOpName(p->op));
@@ -108706,23 +109403,19 @@
109403 Parse *pParse, /* Parsing context */
109404 Select *p, /* The right-most of SELECTs to be coded */
109405 SelectDest *pDest /* What to do with query results */
109406 ){
109407 Select *pPrior;
 
109408 int nRow = 1;
109409 int rc = 0;
109410 assert( p->selFlags & SF_MultiValue );
109411 do{
109412 assert( p->selFlags & SF_Values );
109413 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
109414 assert( p->pLimit==0 );
109415 assert( p->pOffset==0 );
109416 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
 
 
 
109417 if( p->pPrior==0 ) break;
109418 assert( p->pPrior->pNext==p );
109419 p = p->pPrior;
109420 nRow++;
109421 }while(1);
@@ -108827,15 +109520,11 @@
109520
109521 /* Make sure all SELECTs in the statement have the same number of elements
109522 ** in their result sets.
109523 */
109524 assert( p->pEList && pPrior->pEList );
109525 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
 
 
 
 
109526
109527 #ifndef SQLITE_OMIT_CTE
109528 if( p->selFlags & SF_Recursive ){
109529 generateWithRecursiveQuery(pParse, p, &dest);
109530 }else
@@ -109450,13 +110139,11 @@
110139 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
110140 if( aPermute ){
110141 struct ExprList_item *pItem;
110142 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
110143 assert( pItem->u.x.iOrderByCol>0 );
110144 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
 
 
110145 aPermute[i] = pItem->u.x.iOrderByCol - 1;
110146 }
110147 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
110148 }else{
110149 pKeyMerge = 0;
@@ -109811,12 +110498,12 @@
110498 ** (9) The subquery does not use LIMIT or the outer query does not use
110499 ** aggregates.
110500 **
110501 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
110502 ** accidently carried the comment forward until 2014-09-15. Original
110503 ** text: "The subquery does not use aggregates or the outer query
110504 ** does not use LIMIT."
110505 **
110506 ** (11) The subquery and the outer query do not both have ORDER BY clauses.
110507 **
110508 ** (**) Not implemented. Subsumed into restriction (3). Was previously
110509 ** a separate restriction deriving from ticket #350.
@@ -110022,14 +110709,14 @@
110709 }
110710 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
110711 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
110712 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
110713 assert( pSub->pSrc!=0 );
110714 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
110715 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
110716 || (pSub1->pPrior && pSub1->op!=TK_ALL)
110717 || pSub1->pSrc->nSrc<1
 
110718 ){
110719 return 0;
110720 }
110721 testcase( pSub1->pSrc->nSrc>1 );
110722 }
@@ -110305,16 +110992,83 @@
110992 */
110993 sqlite3SelectDelete(db, pSub1);
110994
110995 #if SELECTTRACE_ENABLED
110996 if( sqlite3SelectTrace & 0x100 ){
110997 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
110998 sqlite3TreeViewSelect(0, p, 0);
110999 }
111000 #endif
111001
111002 return 1;
111003 }
111004 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
111005
111006
111007
111008 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
111009 /*
111010 ** Make copies of relevant WHERE clause terms of the outer query into
111011 ** the WHERE clause of subquery. Example:
111012 **
111013 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
111014 **
111015 ** Transformed into:
111016 **
111017 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
111018 ** WHERE x=5 AND y=10;
111019 **
111020 ** The hope is that the terms added to the inner query will make it more
111021 ** efficient.
111022 **
111023 ** Do not attempt this optimization if:
111024 **
111025 ** (1) The inner query is an aggregate. (In that case, we'd really want
111026 ** to copy the outer WHERE-clause terms onto the HAVING clause of the
111027 ** inner query. But they probably won't help there so do not bother.)
111028 **
111029 ** (2) The inner query is the recursive part of a common table expression.
111030 **
111031 ** (3) The inner query has a LIMIT clause (since the changes to the WHERE
111032 ** close would change the meaning of the LIMIT).
111033 **
111034 ** (4) The inner query is the right operand of a LEFT JOIN. (The caller
111035 ** enforces this restriction since this routine does not have enough
111036 ** information to know.)
111037 **
111038 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
111039 ** terms are duplicated into the subquery.
111040 */
111041 static int pushDownWhereTerms(
111042 sqlite3 *db, /* The database connection (for malloc()) */
111043 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
111044 Expr *pWhere, /* The WHERE clause of the outer query */
111045 int iCursor /* Cursor number of the subquery */
111046 ){
111047 Expr *pNew;
111048 int nChng = 0;
111049 if( pWhere==0 ) return 0;
111050 if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
111051 return 0; /* restrictions (1) and (2) */
111052 }
111053 if( pSubq->pLimit!=0 ){
111054 return 0; /* restriction (3) */
111055 }
111056 while( pWhere->op==TK_AND ){
111057 nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor);
111058 pWhere = pWhere->pLeft;
111059 }
111060 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
111061 nChng++;
111062 while( pSubq ){
111063 pNew = sqlite3ExprDup(db, pWhere, 0);
111064 pNew = substExpr(db, pNew, iCursor, pSubq->pEList);
111065 pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew);
111066 pSubq = pSubq->pPrior;
111067 }
111068 }
111069 return nChng;
111070 }
111071 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
111072
111073 /*
111074 ** Based on the contents of the AggInfo structure indicated by the first
@@ -110397,20 +111151,20 @@
111151 ** was such a clause and the named index cannot be found, return
111152 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
111153 ** pFrom->pIndex and return SQLITE_OK.
111154 */
111155 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
111156 if( pFrom->pTab && pFrom->zIndexedBy ){
111157 Table *pTab = pFrom->pTab;
111158 char *zIndexedBy = pFrom->zIndexedBy;
111159 Index *pIdx;
111160 for(pIdx=pTab->pIndex;
111161 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
111162 pIdx=pIdx->pNext
111163 );
111164 if( !pIdx ){
111165 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
111166 pParse->checkSchema = 1;
111167 return SQLITE_ERROR;
111168 }
111169 pFrom->pIndex = pIdx;
111170 }
@@ -111210,11 +111964,11 @@
111964 pColl = pParse->db->pDfltColl;
111965 }
111966 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
111967 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
111968 }
111969 sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem,
111970 (void*)pF->pFunc, P4_FUNCDEF);
111971 sqlite3VdbeChangeP5(v, (u8)nArg);
111972 sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
111973 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
111974 if( addrNext ){
@@ -111343,47 +112097,84 @@
112097 }
112098 sqlite3SelectPrep(pParse, p, 0);
112099 memset(&sSort, 0, sizeof(sSort));
112100 sSort.pOrderBy = p->pOrderBy;
112101 pTabList = p->pSrc;
 
112102 if( pParse->nErr || db->mallocFailed ){
112103 goto select_end;
112104 }
112105 assert( p->pEList!=0 );
112106 isAgg = (p->selFlags & SF_Aggregate)!=0;
 
112107 #if SELECTTRACE_ENABLED
112108 if( sqlite3SelectTrace & 0x100 ){
112109 SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
112110 sqlite3TreeViewSelect(0, p, 0);
112111 }
112112 #endif
112113
112114
 
 
 
 
 
112115 /* If writing to memory or generating a set
112116 ** only a single column may be output.
112117 */
112118 #ifndef SQLITE_OMIT_SUBQUERY
112119 if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){
112120 goto select_end;
112121 }
112122 #endif
112123
112124 /* Try to flatten subqueries in the FROM clause up into the main query
112125 */
112126 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
112127 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
112128 struct SrcList_item *pItem = &pTabList->a[i];
112129 Select *pSub = pItem->pSelect;
112130 int isAggSub;
112131 if( pSub==0 ) continue;
112132 isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
112133 if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
112134 /* This subquery can be absorbed into its parent. */
112135 if( isAggSub ){
112136 isAgg = 1;
112137 p->selFlags |= SF_Aggregate;
112138 }
112139 i = -1;
112140 }
112141 pTabList = p->pSrc;
112142 if( db->mallocFailed ) goto select_end;
112143 if( !IgnorableOrderby(pDest) ){
112144 sSort.pOrderBy = p->pOrderBy;
112145 }
112146 }
112147 #endif
112148
112149 /* Get a pointer the VDBE under construction, allocating a new VDBE if one
112150 ** does not already exist */
112151 v = sqlite3GetVdbe(pParse);
112152 if( v==0 ) goto select_end;
112153
112154 #ifndef SQLITE_OMIT_COMPOUND_SELECT
112155 /* Handle compound SELECT statements using the separate multiSelect()
112156 ** procedure.
112157 */
112158 if( p->pPrior ){
112159 rc = multiSelect(pParse, p, pDest);
112160 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
112161 #if SELECTTRACE_ENABLED
112162 SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
112163 pParse->nSelectIndent--;
112164 #endif
112165 return rc;
112166 }
112167 #endif
112168
112169 /* Generate code for all sub-queries in the FROM clause
112170 */
112171 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
112172 for(i=0; i<pTabList->nSrc; i++){
112173 struct SrcList_item *pItem = &pTabList->a[i];
112174 SelectDest dest;
112175 Select *pSub = pItem->pSelect;
 
 
112176 if( pSub==0 ) continue;
112177
112178 /* Sometimes the code for a subquery will be generated more than
112179 ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
112180 ** for example. In that case, do not regenerate the code to manifest
@@ -111404,21 +112195,29 @@
112195 ** more conservative than necessary, but much easier than enforcing
112196 ** an exact limit.
112197 */
112198 pParse->nHeight += sqlite3SelectExprHeight(p);
112199
112200 /* Make copies of constant WHERE-clause terms in the outer query down
112201 ** inside the subquery. This can help the subquery to run more efficiently.
112202 */
112203 if( (pItem->jointype & JT_OUTER)==0
112204 && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
112205 ){
112206 #if SELECTTRACE_ENABLED
112207 if( sqlite3SelectTrace & 0x100 ){
112208 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
112209 sqlite3TreeViewSelect(0, p, 0);
112210 }
112211 #endif
112212 }
112213
112214 /* Generate code to implement the subquery
112215 */
112216 if( pTabList->nSrc==1
112217 && (p->selFlags & SF_All)==0
112218 && OptimizationEnabled(db, SQLITE_SubqCoroutine)
112219 ){
112220 /* Implement a co-routine that will return a single row of the result
112221 ** set on each invocation.
112222 */
112223 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
@@ -111465,37 +112264,27 @@
112264 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
112265 VdbeComment((v, "end %s", pItem->pTab->zName));
112266 sqlite3VdbeChangeP1(v, topAddr, retAddr);
112267 sqlite3ClearTempRegCache(pParse);
112268 }
112269 if( db->mallocFailed ) goto select_end;
 
 
112270 pParse->nHeight -= sqlite3SelectExprHeight(p);
 
 
 
 
112271 }
112272 #endif
112273
112274 /* Various elements of the SELECT copied into local variables for
112275 ** convenience */
112276 pEList = p->pEList;
 
112277 pWhere = p->pWhere;
112278 pGroupBy = p->pGroupBy;
112279 pHaving = p->pHaving;
112280 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
112281
 
 
 
 
 
 
112282 #if SELECTTRACE_ENABLED
112283 if( sqlite3SelectTrace & 0x400 ){
112284 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
112285 sqlite3TreeViewSelect(0, p, 0);
 
112286 }
112287 #endif
112288
112289 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
112290 ** if the select-list is the same as the ORDER BY list, then this query
@@ -111511,27 +112300,27 @@
112300 ** used for both the ORDER BY and DISTINCT processing. As originally
112301 ** written the query must use a temp-table for at least one of the ORDER
112302 ** BY and DISTINCT, and an index or separate temp-table for the other.
112303 */
112304 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
112305 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
112306 ){
112307 p->selFlags &= ~SF_Distinct;
112308 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
 
112309 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
112310 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
112311 ** original setting of the SF_Distinct flag, not the current setting */
112312 assert( sDistinct.isTnct );
112313 }
112314
112315 /* If there is an ORDER BY clause, then create an ephemeral index to
112316 ** do the sorting. But this sorting ephemeral index might end up
112317 ** being unused if the data can be extracted in pre-sorted order.
112318 ** If that is the case, then the OP_OpenEphemeral instruction will be
112319 ** changed to an OP_Noop once we figure out that the sorting index is
112320 ** not needed. The sSort.addrSortIndex variable is used to facilitate
112321 ** that change.
112322 */
112323 if( sSort.pOrderBy ){
112324 KeyInfo *pKeyInfo;
112325 pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
112326 sSort.iECursor = pParse->nTab++;
@@ -111558,18 +112347,18 @@
112347 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
112348 sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
112349 sSort.sortFlags |= SORTFLAG_UseSorter;
112350 }
112351
112352 /* Open an ephemeral index to use for the distinct set.
112353 */
112354 if( p->selFlags & SF_Distinct ){
112355 sDistinct.tabTnct = pParse->nTab++;
112356 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
112357 sDistinct.tabTnct, 0, 0,
112358 (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
112359 P4_KEYINFO);
112360 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
112361 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
112362 }else{
112363 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
112364 }
@@ -111643,15 +112432,14 @@
112432 if( p->nSelectRow>100 ) p->nSelectRow = 100;
112433 }else{
112434 p->nSelectRow = 1;
112435 }
112436
 
112437 /* If there is both a GROUP BY and an ORDER BY clause and they are
112438 ** identical, then it may be possible to disable the ORDER BY clause
112439 ** on the grounds that the GROUP BY will cause elements to come out
112440 ** in the correct order. It also may not - the GROUP BY might use a
112441 ** database index that causes rows to be grouped together as required
112442 ** but not actually sorted. Either way, record the fact that the
112443 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
112444 ** variable. */
112445 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
@@ -111825,11 +112613,12 @@
112613 ** from the previous row currently stored in a0, a1, a2...
112614 */
112615 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
112616 sqlite3ExprCacheClear(pParse);
112617 if( groupBySort ){
112618 sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
112619 sortOut, sortPTab);
112620 }
112621 for(j=0; j<pGroupBy->nExpr; j++){
112622 if( groupBySort ){
112623 sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
112624 }else{
@@ -111897,11 +112686,12 @@
112686 sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
112687 VdbeComment((v, "set abort flag"));
112688 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
112689 sqlite3VdbeResolveLabel(v, addrOutputRow);
112690 addrOutputRow = sqlite3VdbeCurrentAddr(v);
112691 sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
112692 VdbeCoverage(v);
112693 VdbeComment((v, "Groupby result generator entry point"));
112694 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
112695 finalizeAggFunctions(pParse, &sAggInfo);
112696 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
112697 selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
@@ -112061,11 +112851,12 @@
112851
112852 /* If there is an ORDER BY clause, then we need to sort the results
112853 ** and send them to the callback one by one.
112854 */
112855 if( sSort.pOrderBy ){
112856 explainTempTable(pParse,
112857 sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
112858 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
112859 }
112860
112861 /* Jump here to skip this query
112862 */
@@ -112094,104 +112885,10 @@
112885 pParse->nSelectIndent--;
112886 #endif
112887 return rc;
112888 }
112889
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112890 /************** End of select.c **********************************************/
112891 /************** Begin file table.c *******************************************/
112892 /*
112893 ** 2001 September 15
112894 **
@@ -115499,24 +116196,25 @@
116196 ** The array is cleared after invoking the callbacks.
116197 */
116198 static void callFinaliser(sqlite3 *db, int offset){
116199 int i;
116200 if( db->aVTrans ){
116201 VTable **aVTrans = db->aVTrans;
116202 db->aVTrans = 0;
116203 for(i=0; i<db->nVTrans; i++){
116204 VTable *pVTab = aVTrans[i];
116205 sqlite3_vtab *p = pVTab->pVtab;
116206 if( p ){
116207 int (*x)(sqlite3_vtab *);
116208 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
116209 if( x ) x(p);
116210 }
116211 pVTab->iSavepoint = 0;
116212 sqlite3VtabUnlock(pVTab);
116213 }
116214 sqlite3DbFree(db, aVTrans);
116215 db->nVTrans = 0;
 
116216 }
116217 }
116218
116219 /*
116220 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
@@ -115812,13 +116510,13 @@
116510 }
116511
116512 #endif /* SQLITE_OMIT_VIRTUALTABLE */
116513
116514 /************** End of vtab.c ************************************************/
116515 /************** Begin file wherecode.c ***************************************/
116516 /*
116517 ** 2015-06-06
116518 **
116519 ** The author disclaims copyright to this source code. In place of
116520 ** a legal notice, here is a blessing:
116521 **
116522 ** May you do good and not evil.
@@ -115825,17 +116523,18 @@
116523 ** May you find forgiveness for yourself and forgive others.
116524 ** May you share freely, never taking more than you give.
116525 **
116526 *************************************************************************
116527 ** This module contains C code that generates VDBE code used to process
116528 ** the WHERE clause of SQL statements.
116529 **
116530 ** This file was split off from where.c on 2015-06-06 in order to reduce the
116531 ** size of where.c and make it easier to edit. This file contains the routines
116532 ** that actually generate the bulk of the WHERE loop code. The original where.c
116533 ** file retains the code that does query planning and analysis.
116534 */
116535 /************** Include whereInt.h in the middle of wherecode.c **************/
116536 /************** Begin file whereInt.h ****************************************/
116537 /*
116538 ** 2013-11-12
116539 **
116540 ** The author disclaims copyright to this source code. In place of
@@ -115854,11 +116553,11 @@
116553
116554 /*
116555 ** Trace output macros
116556 */
116557 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
116558 /***/ int sqlite3WhereTrace;
116559 #endif
116560 #if defined(SQLITE_DEBUG) \
116561 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
116562 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
116563 # define WHERETRACE_ENABLED 1
@@ -115996,14 +116695,10 @@
116695 struct WhereOrSet {
116696 u16 n; /* Number of valid a[] entries */
116697 WhereOrCost a[N_OR_COST]; /* Set of best costs */
116698 };
116699
 
 
 
 
116700 /*
116701 ** Each instance of this object holds a sequence of WhereLoop objects
116702 ** that implement some or all of a query plan.
116703 **
116704 ** Think of each WhereLoop object as a node in a graph with arcs
@@ -116207,10 +116902,15 @@
116902 struct WhereMaskSet {
116903 int n; /* Number of assigned cursor values */
116904 int ix[BMS]; /* Cursor assigned to each bit */
116905 };
116906
116907 /*
116908 ** Initialize a WhereMaskSet object
116909 */
116910 #define initMaskSet(P) (P)->n=0
116911
116912 /*
116913 ** This object is a convenience wrapper holding all information needed
116914 ** to construct WhereLoop objects for a particular query.
116915 */
116916 struct WhereLoopBuilder {
@@ -116257,10 +116957,66 @@
116957 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
116958 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
116959 WhereClause sWC; /* Decomposition of the WHERE clause */
116960 WhereLevel a[1]; /* Information about each nest loop in WHERE */
116961 };
116962
116963 /*
116964 ** Private interfaces - callable only by other where.c routines.
116965 **
116966 ** where.c:
116967 */
116968 SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
116969 SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
116970 WhereClause *pWC, /* The WHERE clause to be searched */
116971 int iCur, /* Cursor number of LHS */
116972 int iColumn, /* Column number of LHS */
116973 Bitmask notReady, /* RHS must not overlap with this mask */
116974 u32 op, /* Mask of WO_xx values describing operator */
116975 Index *pIdx /* Must be compatible with this index, if not NULL */
116976 );
116977
116978 /* wherecode.c: */
116979 #ifndef SQLITE_OMIT_EXPLAIN
116980 SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
116981 Parse *pParse, /* Parse context */
116982 SrcList *pTabList, /* Table list this loop refers to */
116983 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
116984 int iLevel, /* Value for "level" column of output */
116985 int iFrom, /* Value for "from" column of output */
116986 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
116987 );
116988 #else
116989 # define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
116990 #endif /* SQLITE_OMIT_EXPLAIN */
116991 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
116992 SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
116993 Vdbe *v, /* Vdbe to add scanstatus entry to */
116994 SrcList *pSrclist, /* FROM clause pLvl reads data from */
116995 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
116996 int addrExplain /* Address of OP_Explain (or 0) */
116997 );
116998 #else
116999 # define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
117000 #endif
117001 SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
117002 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
117003 int iLevel, /* Which level of pWInfo->a[] should be coded */
117004 Bitmask notReady /* Which tables are currently available */
117005 );
117006
117007 /* whereexpr.c: */
117008 SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
117009 SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
117010 SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
117011 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
117012 SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
117013 SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
117014
117015
117016
117017
117018
117019 /*
117020 ** Bitmasks for the operators on WhereTerm objects. These are all
117021 ** operators that are of interest to the query planner. An
117022 ** OR-ed combination of these values can be used when searching for
@@ -116307,176 +117063,1532 @@
117063 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
117064 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
117065 #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
117066
117067 /************** End of whereInt.h ********************************************/
117068 /************** Continuing where we left off in wherecode.c ******************/
117069
117070 #ifndef SQLITE_OMIT_EXPLAIN
117071 /*
117072 ** This routine is a helper for explainIndexRange() below
117073 **
117074 ** pStr holds the text of an expression that we are building up one term
117075 ** at a time. This routine adds a new term to the end of the expression.
117076 ** Terms are separated by AND so add the "AND" text for second and subsequent
117077 ** terms only.
117078 */
117079 static void explainAppendTerm(
117080 StrAccum *pStr, /* The text expression being built */
117081 int iTerm, /* Index of this term. First is zero */
117082 const char *zColumn, /* Name of the column */
117083 const char *zOp /* Name of the operator */
117084 ){
117085 if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
117086 sqlite3StrAccumAppendAll(pStr, zColumn);
117087 sqlite3StrAccumAppend(pStr, zOp, 1);
117088 sqlite3StrAccumAppend(pStr, "?", 1);
117089 }
117090
117091 /*
117092 ** Argument pLevel describes a strategy for scanning table pTab. This
117093 ** function appends text to pStr that describes the subset of table
117094 ** rows scanned by the strategy in the form of an SQL expression.
117095 **
117096 ** For example, if the query:
117097 **
117098 ** SELECT * FROM t1 WHERE a=1 AND b>2;
117099 **
117100 ** is run and there is an index on (a, b), then this function returns a
117101 ** string similar to:
117102 **
117103 ** "a=? AND b>?"
117104 */
117105 static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
117106 Index *pIndex = pLoop->u.btree.pIndex;
117107 u16 nEq = pLoop->u.btree.nEq;
117108 u16 nSkip = pLoop->nSkip;
117109 int i, j;
117110 Column *aCol = pTab->aCol;
117111 i16 *aiColumn = pIndex->aiColumn;
117112
117113 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
117114 sqlite3StrAccumAppend(pStr, " (", 2);
117115 for(i=0; i<nEq; i++){
117116 char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
117117 if( i>=nSkip ){
117118 explainAppendTerm(pStr, i, z, "=");
117119 }else{
117120 if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
117121 sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
117122 }
117123 }
117124
117125 j = i;
117126 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
117127 char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
117128 explainAppendTerm(pStr, i++, z, ">");
117129 }
117130 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
117131 char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
117132 explainAppendTerm(pStr, i, z, "<");
117133 }
117134 sqlite3StrAccumAppend(pStr, ")", 1);
117135 }
117136
117137 /*
117138 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
117139 ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
117140 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
117141 ** is added to the output to describe the table scan strategy in pLevel.
117142 **
117143 ** If an OP_Explain opcode is added to the VM, its address is returned.
117144 ** Otherwise, if no OP_Explain is coded, zero is returned.
117145 */
117146 SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
117147 Parse *pParse, /* Parse context */
117148 SrcList *pTabList, /* Table list this loop refers to */
117149 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
117150 int iLevel, /* Value for "level" column of output */
117151 int iFrom, /* Value for "from" column of output */
117152 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
117153 ){
117154 int ret = 0;
117155 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
117156 if( pParse->explain==2 )
117157 #endif
117158 {
117159 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
117160 Vdbe *v = pParse->pVdbe; /* VM being constructed */
117161 sqlite3 *db = pParse->db; /* Database handle */
117162 int iId = pParse->iSelectId; /* Select id (left-most output column) */
117163 int isSearch; /* True for a SEARCH. False for SCAN. */
117164 WhereLoop *pLoop; /* The controlling WhereLoop object */
117165 u32 flags; /* Flags that describe this loop */
117166 char *zMsg; /* Text to add to EQP output */
117167 StrAccum str; /* EQP output string */
117168 char zBuf[100]; /* Initial space for EQP output string */
117169
117170 pLoop = pLevel->pWLoop;
117171 flags = pLoop->wsFlags;
117172 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
117173
117174 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
117175 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
117176 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
117177
117178 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
117179 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
117180 if( pItem->pSelect ){
117181 sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
117182 }else{
117183 sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
117184 }
117185
117186 if( pItem->zAlias ){
117187 sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
117188 }
117189 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
117190 const char *zFmt = 0;
117191 Index *pIdx;
117192
117193 assert( pLoop->u.btree.pIndex!=0 );
117194 pIdx = pLoop->u.btree.pIndex;
117195 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
117196 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
117197 if( isSearch ){
117198 zFmt = "PRIMARY KEY";
117199 }
117200 }else if( flags & WHERE_PARTIALIDX ){
117201 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
117202 }else if( flags & WHERE_AUTO_INDEX ){
117203 zFmt = "AUTOMATIC COVERING INDEX";
117204 }else if( flags & WHERE_IDX_ONLY ){
117205 zFmt = "COVERING INDEX %s";
117206 }else{
117207 zFmt = "INDEX %s";
117208 }
117209 if( zFmt ){
117210 sqlite3StrAccumAppend(&str, " USING ", 7);
117211 sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
117212 explainIndexRange(&str, pLoop, pItem->pTab);
117213 }
117214 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
117215 const char *zRange;
117216 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
117217 zRange = "(rowid=?)";
117218 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
117219 zRange = "(rowid>? AND rowid<?)";
117220 }else if( flags&WHERE_BTM_LIMIT ){
117221 zRange = "(rowid>?)";
117222 }else{
117223 assert( flags&WHERE_TOP_LIMIT);
117224 zRange = "(rowid<?)";
117225 }
117226 sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
117227 sqlite3StrAccumAppendAll(&str, zRange);
117228 }
117229 #ifndef SQLITE_OMIT_VIRTUALTABLE
117230 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
117231 sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
117232 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
117233 }
117234 #endif
117235 #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
117236 if( pLoop->nOut>=10 ){
117237 sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
117238 }else{
117239 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
117240 }
117241 #endif
117242 zMsg = sqlite3StrAccumFinish(&str);
117243 ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
117244 }
117245 return ret;
117246 }
117247 #endif /* SQLITE_OMIT_EXPLAIN */
117248
117249 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
117250 /*
117251 ** Configure the VM passed as the first argument with an
117252 ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
117253 ** implement level pLvl. Argument pSrclist is a pointer to the FROM
117254 ** clause that the scan reads data from.
117255 **
117256 ** If argument addrExplain is not 0, it must be the address of an
117257 ** OP_Explain instruction that describes the same loop.
117258 */
117259 SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
117260 Vdbe *v, /* Vdbe to add scanstatus entry to */
117261 SrcList *pSrclist, /* FROM clause pLvl reads data from */
117262 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
117263 int addrExplain /* Address of OP_Explain (or 0) */
117264 ){
117265 const char *zObj = 0;
117266 WhereLoop *pLoop = pLvl->pWLoop;
117267 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
117268 zObj = pLoop->u.btree.pIndex->zName;
117269 }else{
117270 zObj = pSrclist->a[pLvl->iFrom].zName;
117271 }
117272 sqlite3VdbeScanStatus(
117273 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
117274 );
117275 }
117276 #endif
117277
117278
117279 /*
117280 ** Disable a term in the WHERE clause. Except, do not disable the term
117281 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
117282 ** or USING clause of that join.
117283 **
117284 ** Consider the term t2.z='ok' in the following queries:
117285 **
117286 ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
117287 ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
117288 ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
117289 **
117290 ** The t2.z='ok' is disabled in the in (2) because it originates
117291 ** in the ON clause. The term is disabled in (3) because it is not part
117292 ** of a LEFT OUTER JOIN. In (1), the term is not disabled.
117293 **
117294 ** Disabling a term causes that term to not be tested in the inner loop
117295 ** of the join. Disabling is an optimization. When terms are satisfied
117296 ** by indices, we disable them to prevent redundant tests in the inner
117297 ** loop. We would get the correct results if nothing were ever disabled,
117298 ** but joins might run a little slower. The trick is to disable as much
117299 ** as we can without disabling too much. If we disabled in (1), we'd get
117300 ** the wrong answer. See ticket #813.
117301 **
117302 ** If all the children of a term are disabled, then that term is also
117303 ** automatically disabled. In this way, terms get disabled if derived
117304 ** virtual terms are tested first. For example:
117305 **
117306 ** x GLOB 'abc*' AND x>='abc' AND x<'acd'
117307 ** \___________/ \______/ \_____/
117308 ** parent child1 child2
117309 **
117310 ** Only the parent term was in the original WHERE clause. The child1
117311 ** and child2 terms were added by the LIKE optimization. If both of
117312 ** the virtual child terms are valid, then testing of the parent can be
117313 ** skipped.
117314 **
117315 ** Usually the parent term is marked as TERM_CODED. But if the parent
117316 ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
117317 ** The TERM_LIKECOND marking indicates that the term should be coded inside
117318 ** a conditional such that is only evaluated on the second pass of a
117319 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
117320 */
117321 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
117322 int nLoop = 0;
117323 while( pTerm
117324 && (pTerm->wtFlags & TERM_CODED)==0
117325 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
117326 && (pLevel->notReady & pTerm->prereqAll)==0
117327 ){
117328 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
117329 pTerm->wtFlags |= TERM_LIKECOND;
117330 }else{
117331 pTerm->wtFlags |= TERM_CODED;
117332 }
117333 if( pTerm->iParent<0 ) break;
117334 pTerm = &pTerm->pWC->a[pTerm->iParent];
117335 pTerm->nChild--;
117336 if( pTerm->nChild!=0 ) break;
117337 nLoop++;
117338 }
117339 }
117340
117341 /*
117342 ** Code an OP_Affinity opcode to apply the column affinity string zAff
117343 ** to the n registers starting at base.
117344 **
117345 ** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
117346 ** beginning and end of zAff are ignored. If all entries in zAff are
117347 ** SQLITE_AFF_BLOB, then no code gets generated.
117348 **
117349 ** This routine makes its own copy of zAff so that the caller is free
117350 ** to modify zAff after this routine returns.
117351 */
117352 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
117353 Vdbe *v = pParse->pVdbe;
117354 if( zAff==0 ){
117355 assert( pParse->db->mallocFailed );
117356 return;
117357 }
117358 assert( v!=0 );
117359
117360 /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
117361 ** and end of the affinity string.
117362 */
117363 while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
117364 n--;
117365 base++;
117366 zAff++;
117367 }
117368 while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
117369 n--;
117370 }
117371
117372 /* Code the OP_Affinity opcode if there is anything left to do. */
117373 if( n>0 ){
117374 sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
117375 sqlite3VdbeChangeP4(v, -1, zAff, n);
117376 sqlite3ExprCacheAffinityChange(pParse, base, n);
117377 }
117378 }
117379
117380
117381 /*
117382 ** Generate code for a single equality term of the WHERE clause. An equality
117383 ** term can be either X=expr or X IN (...). pTerm is the term to be
117384 ** coded.
117385 **
117386 ** The current value for the constraint is left in register iReg.
117387 **
117388 ** For a constraint of the form X=expr, the expression is evaluated and its
117389 ** result is left on the stack. For constraints of the form X IN (...)
117390 ** this routine sets up a loop that will iterate over all values of X.
117391 */
117392 static int codeEqualityTerm(
117393 Parse *pParse, /* The parsing context */
117394 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
117395 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
117396 int iEq, /* Index of the equality term within this level */
117397 int bRev, /* True for reverse-order IN operations */
117398 int iTarget /* Attempt to leave results in this register */
117399 ){
117400 Expr *pX = pTerm->pExpr;
117401 Vdbe *v = pParse->pVdbe;
117402 int iReg; /* Register holding results */
117403
117404 assert( iTarget>0 );
117405 if( pX->op==TK_EQ || pX->op==TK_IS ){
117406 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
117407 }else if( pX->op==TK_ISNULL ){
117408 iReg = iTarget;
117409 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
117410 #ifndef SQLITE_OMIT_SUBQUERY
117411 }else{
117412 int eType;
117413 int iTab;
117414 struct InLoop *pIn;
117415 WhereLoop *pLoop = pLevel->pWLoop;
117416
117417 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
117418 && pLoop->u.btree.pIndex!=0
117419 && pLoop->u.btree.pIndex->aSortOrder[iEq]
117420 ){
117421 testcase( iEq==0 );
117422 testcase( bRev );
117423 bRev = !bRev;
117424 }
117425 assert( pX->op==TK_IN );
117426 iReg = iTarget;
117427 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
117428 if( eType==IN_INDEX_INDEX_DESC ){
117429 testcase( bRev );
117430 bRev = !bRev;
117431 }
117432 iTab = pX->iTable;
117433 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
117434 VdbeCoverageIf(v, bRev);
117435 VdbeCoverageIf(v, !bRev);
117436 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
117437 pLoop->wsFlags |= WHERE_IN_ABLE;
117438 if( pLevel->u.in.nIn==0 ){
117439 pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
117440 }
117441 pLevel->u.in.nIn++;
117442 pLevel->u.in.aInLoop =
117443 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
117444 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
117445 pIn = pLevel->u.in.aInLoop;
117446 if( pIn ){
117447 pIn += pLevel->u.in.nIn - 1;
117448 pIn->iCur = iTab;
117449 if( eType==IN_INDEX_ROWID ){
117450 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
117451 }else{
117452 pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
117453 }
117454 pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
117455 sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
117456 }else{
117457 pLevel->u.in.nIn = 0;
117458 }
117459 #endif
117460 }
117461 disableTerm(pLevel, pTerm);
117462 return iReg;
117463 }
117464
117465 /*
117466 ** Generate code that will evaluate all == and IN constraints for an
117467 ** index scan.
117468 **
117469 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
117470 ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
117471 ** The index has as many as three equality constraints, but in this
117472 ** example, the third "c" value is an inequality. So only two
117473 ** constraints are coded. This routine will generate code to evaluate
117474 ** a==5 and b IN (1,2,3). The current values for a and b will be stored
117475 ** in consecutive registers and the index of the first register is returned.
117476 **
117477 ** In the example above nEq==2. But this subroutine works for any value
117478 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
117479 ** The only thing it does is allocate the pLevel->iMem memory cell and
117480 ** compute the affinity string.
117481 **
117482 ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
117483 ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
117484 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
117485 ** occurs after the nEq quality constraints.
117486 **
117487 ** This routine allocates a range of nEq+nExtraReg memory cells and returns
117488 ** the index of the first memory cell in that range. The code that
117489 ** calls this routine will use that memory range to store keys for
117490 ** start and termination conditions of the loop.
117491 ** key value of the loop. If one or more IN operators appear, then
117492 ** this routine allocates an additional nEq memory cells for internal
117493 ** use.
117494 **
117495 ** Before returning, *pzAff is set to point to a buffer containing a
117496 ** copy of the column affinity string of the index allocated using
117497 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
117498 ** with equality constraints that use BLOB or NONE affinity are set to
117499 ** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
117500 **
117501 ** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
117502 ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
117503 **
117504 ** In the example above, the index on t1(a) has TEXT affinity. But since
117505 ** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
117506 ** no conversion should be attempted before using a t2.b value as part of
117507 ** a key to search the index. Hence the first byte in the returned affinity
117508 ** string in this example would be set to SQLITE_AFF_BLOB.
117509 */
117510 static int codeAllEqualityTerms(
117511 Parse *pParse, /* Parsing context */
117512 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
117513 int bRev, /* Reverse the order of IN operators */
117514 int nExtraReg, /* Number of extra registers to allocate */
117515 char **pzAff /* OUT: Set to point to affinity string */
117516 ){
117517 u16 nEq; /* The number of == or IN constraints to code */
117518 u16 nSkip; /* Number of left-most columns to skip */
117519 Vdbe *v = pParse->pVdbe; /* The vm under construction */
117520 Index *pIdx; /* The index being used for this loop */
117521 WhereTerm *pTerm; /* A single constraint term */
117522 WhereLoop *pLoop; /* The WhereLoop object */
117523 int j; /* Loop counter */
117524 int regBase; /* Base register */
117525 int nReg; /* Number of registers to allocate */
117526 char *zAff; /* Affinity string to return */
117527
117528 /* This module is only called on query plans that use an index. */
117529 pLoop = pLevel->pWLoop;
117530 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
117531 nEq = pLoop->u.btree.nEq;
117532 nSkip = pLoop->nSkip;
117533 pIdx = pLoop->u.btree.pIndex;
117534 assert( pIdx!=0 );
117535
117536 /* Figure out how many memory cells we will need then allocate them.
117537 */
117538 regBase = pParse->nMem + 1;
117539 nReg = pLoop->u.btree.nEq + nExtraReg;
117540 pParse->nMem += nReg;
117541
117542 zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
117543 if( !zAff ){
117544 pParse->db->mallocFailed = 1;
117545 }
117546
117547 if( nSkip ){
117548 int iIdxCur = pLevel->iIdxCur;
117549 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
117550 VdbeCoverageIf(v, bRev==0);
117551 VdbeCoverageIf(v, bRev!=0);
117552 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
117553 j = sqlite3VdbeAddOp0(v, OP_Goto);
117554 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
117555 iIdxCur, 0, regBase, nSkip);
117556 VdbeCoverageIf(v, bRev==0);
117557 VdbeCoverageIf(v, bRev!=0);
117558 sqlite3VdbeJumpHere(v, j);
117559 for(j=0; j<nSkip; j++){
117560 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
117561 assert( pIdx->aiColumn[j]>=0 );
117562 VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
117563 }
117564 }
117565
117566 /* Evaluate the equality constraints
117567 */
117568 assert( zAff==0 || (int)strlen(zAff)>=nEq );
117569 for(j=nSkip; j<nEq; j++){
117570 int r1;
117571 pTerm = pLoop->aLTerm[j];
117572 assert( pTerm!=0 );
117573 /* The following testcase is true for indices with redundant columns.
117574 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
117575 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
117576 testcase( pTerm->wtFlags & TERM_VIRTUAL );
117577 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
117578 if( r1!=regBase+j ){
117579 if( nReg==1 ){
117580 sqlite3ReleaseTempReg(pParse, regBase);
117581 regBase = r1;
117582 }else{
117583 sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
117584 }
117585 }
117586 testcase( pTerm->eOperator & WO_ISNULL );
117587 testcase( pTerm->eOperator & WO_IN );
117588 if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
117589 Expr *pRight = pTerm->pExpr->pRight;
117590 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
117591 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
117592 VdbeCoverage(v);
117593 }
117594 if( zAff ){
117595 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
117596 zAff[j] = SQLITE_AFF_BLOB;
117597 }
117598 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
117599 zAff[j] = SQLITE_AFF_BLOB;
117600 }
117601 }
117602 }
117603 }
117604 *pzAff = zAff;
117605 return regBase;
117606 }
117607
117608 /*
117609 ** If the most recently coded instruction is a constant range contraint
117610 ** that originated from the LIKE optimization, then change the P3 to be
117611 ** pLoop->iLikeRepCntr and set P5.
117612 **
117613 ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
117614 ** expression: "x>='ABC' AND x<'abd'". But this requires that the range
117615 ** scan loop run twice, once for strings and a second time for BLOBs.
117616 ** The OP_String opcodes on the second pass convert the upper and lower
117617 ** bound string contants to blobs. This routine makes the necessary changes
117618 ** to the OP_String opcodes for that to happen.
117619 */
117620 static void whereLikeOptimizationStringFixup(
117621 Vdbe *v, /* prepared statement under construction */
117622 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
117623 WhereTerm *pTerm /* The upper or lower bound just coded */
117624 ){
117625 if( pTerm->wtFlags & TERM_LIKEOPT ){
117626 VdbeOp *pOp;
117627 assert( pLevel->iLikeRepCntr>0 );
117628 pOp = sqlite3VdbeGetOp(v, -1);
117629 assert( pOp!=0 );
117630 assert( pOp->opcode==OP_String8
117631 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
117632 pOp->p3 = pLevel->iLikeRepCntr;
117633 pOp->p5 = 1;
117634 }
117635 }
117636
117637
117638 /*
117639 ** Generate code for the start of the iLevel-th loop in the WHERE clause
117640 ** implementation described by pWInfo.
117641 */
117642 SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
117643 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
117644 int iLevel, /* Which level of pWInfo->a[] should be coded */
117645 Bitmask notReady /* Which tables are currently available */
117646 ){
117647 int j, k; /* Loop counters */
117648 int iCur; /* The VDBE cursor for the table */
117649 int addrNxt; /* Where to jump to continue with the next IN case */
117650 int omitTable; /* True if we use the index only */
117651 int bRev; /* True if we need to scan in reverse order */
117652 WhereLevel *pLevel; /* The where level to be coded */
117653 WhereLoop *pLoop; /* The WhereLoop object being coded */
117654 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
117655 WhereTerm *pTerm; /* A WHERE clause term */
117656 Parse *pParse; /* Parsing context */
117657 sqlite3 *db; /* Database connection */
117658 Vdbe *v; /* The prepared stmt under constructions */
117659 struct SrcList_item *pTabItem; /* FROM clause term being coded */
117660 int addrBrk; /* Jump here to break out of the loop */
117661 int addrCont; /* Jump here to continue with next cycle */
117662 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
117663 int iReleaseReg = 0; /* Temp register to free before returning */
117664
117665 pParse = pWInfo->pParse;
117666 v = pParse->pVdbe;
117667 pWC = &pWInfo->sWC;
117668 db = pParse->db;
117669 pLevel = &pWInfo->a[iLevel];
117670 pLoop = pLevel->pWLoop;
117671 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
117672 iCur = pTabItem->iCursor;
117673 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
117674 bRev = (pWInfo->revMask>>iLevel)&1;
117675 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
117676 && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
117677 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
117678
117679 /* Create labels for the "break" and "continue" instructions
117680 ** for the current loop. Jump to addrBrk to break out of a loop.
117681 ** Jump to cont to go immediately to the next iteration of the
117682 ** loop.
117683 **
117684 ** When there is an IN operator, we also have a "addrNxt" label that
117685 ** means to continue with the next IN value combination. When
117686 ** there are no IN operators in the constraints, the "addrNxt" label
117687 ** is the same as "addrBrk".
117688 */
117689 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
117690 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
117691
117692 /* If this is the right table of a LEFT OUTER JOIN, allocate and
117693 ** initialize a memory cell that records if this table matches any
117694 ** row of the left table of the join.
117695 */
117696 if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
117697 pLevel->iLeftJoin = ++pParse->nMem;
117698 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
117699 VdbeComment((v, "init LEFT JOIN no-match flag"));
117700 }
117701
117702 /* Special case of a FROM clause subquery implemented as a co-routine */
117703 if( pTabItem->viaCoroutine ){
117704 int regYield = pTabItem->regReturn;
117705 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
117706 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
117707 VdbeCoverage(v);
117708 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
117709 pLevel->op = OP_Goto;
117710 }else
117711
117712 #ifndef SQLITE_OMIT_VIRTUALTABLE
117713 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
117714 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
117715 ** to access the data.
117716 */
117717 int iReg; /* P3 Value for OP_VFilter */
117718 int addrNotFound;
117719 int nConstraint = pLoop->nLTerm;
117720
117721 sqlite3ExprCachePush(pParse);
117722 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
117723 addrNotFound = pLevel->addrBrk;
117724 for(j=0; j<nConstraint; j++){
117725 int iTarget = iReg+j+2;
117726 pTerm = pLoop->aLTerm[j];
117727 if( pTerm==0 ) continue;
117728 if( pTerm->eOperator & WO_IN ){
117729 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
117730 addrNotFound = pLevel->addrNxt;
117731 }else{
117732 sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
117733 }
117734 }
117735 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
117736 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
117737 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
117738 pLoop->u.vtab.idxStr,
117739 pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
117740 VdbeCoverage(v);
117741 pLoop->u.vtab.needFree = 0;
117742 for(j=0; j<nConstraint && j<16; j++){
117743 if( (pLoop->u.vtab.omitMask>>j)&1 ){
117744 disableTerm(pLevel, pLoop->aLTerm[j]);
117745 }
117746 }
117747 pLevel->op = OP_VNext;
117748 pLevel->p1 = iCur;
117749 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
117750 sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
117751 sqlite3ExprCachePop(pParse);
117752 }else
117753 #endif /* SQLITE_OMIT_VIRTUALTABLE */
117754
117755 if( (pLoop->wsFlags & WHERE_IPK)!=0
117756 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
117757 ){
117758 /* Case 2: We can directly reference a single row using an
117759 ** equality comparison against the ROWID field. Or
117760 ** we reference multiple rows using a "rowid IN (...)"
117761 ** construct.
117762 */
117763 assert( pLoop->u.btree.nEq==1 );
117764 pTerm = pLoop->aLTerm[0];
117765 assert( pTerm!=0 );
117766 assert( pTerm->pExpr!=0 );
117767 assert( omitTable==0 );
117768 testcase( pTerm->wtFlags & TERM_VIRTUAL );
117769 iReleaseReg = ++pParse->nMem;
117770 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
117771 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
117772 addrNxt = pLevel->addrNxt;
117773 sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
117774 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
117775 VdbeCoverage(v);
117776 sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
117777 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
117778 VdbeComment((v, "pk"));
117779 pLevel->op = OP_Noop;
117780 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
117781 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
117782 ){
117783 /* Case 3: We have an inequality comparison against the ROWID field.
117784 */
117785 int testOp = OP_Noop;
117786 int start;
117787 int memEndValue = 0;
117788 WhereTerm *pStart, *pEnd;
117789
117790 assert( omitTable==0 );
117791 j = 0;
117792 pStart = pEnd = 0;
117793 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
117794 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
117795 assert( pStart!=0 || pEnd!=0 );
117796 if( bRev ){
117797 pTerm = pStart;
117798 pStart = pEnd;
117799 pEnd = pTerm;
117800 }
117801 if( pStart ){
117802 Expr *pX; /* The expression that defines the start bound */
117803 int r1, rTemp; /* Registers for holding the start boundary */
117804
117805 /* The following constant maps TK_xx codes into corresponding
117806 ** seek opcodes. It depends on a particular ordering of TK_xx
117807 */
117808 const u8 aMoveOp[] = {
117809 /* TK_GT */ OP_SeekGT,
117810 /* TK_LE */ OP_SeekLE,
117811 /* TK_LT */ OP_SeekLT,
117812 /* TK_GE */ OP_SeekGE
117813 };
117814 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
117815 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
117816 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
117817
117818 assert( (pStart->wtFlags & TERM_VNULL)==0 );
117819 testcase( pStart->wtFlags & TERM_VIRTUAL );
117820 pX = pStart->pExpr;
117821 assert( pX!=0 );
117822 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
117823 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
117824 sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
117825 VdbeComment((v, "pk"));
117826 VdbeCoverageIf(v, pX->op==TK_GT);
117827 VdbeCoverageIf(v, pX->op==TK_LE);
117828 VdbeCoverageIf(v, pX->op==TK_LT);
117829 VdbeCoverageIf(v, pX->op==TK_GE);
117830 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
117831 sqlite3ReleaseTempReg(pParse, rTemp);
117832 disableTerm(pLevel, pStart);
117833 }else{
117834 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
117835 VdbeCoverageIf(v, bRev==0);
117836 VdbeCoverageIf(v, bRev!=0);
117837 }
117838 if( pEnd ){
117839 Expr *pX;
117840 pX = pEnd->pExpr;
117841 assert( pX!=0 );
117842 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
117843 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
117844 testcase( pEnd->wtFlags & TERM_VIRTUAL );
117845 memEndValue = ++pParse->nMem;
117846 sqlite3ExprCode(pParse, pX->pRight, memEndValue);
117847 if( pX->op==TK_LT || pX->op==TK_GT ){
117848 testOp = bRev ? OP_Le : OP_Ge;
117849 }else{
117850 testOp = bRev ? OP_Lt : OP_Gt;
117851 }
117852 disableTerm(pLevel, pEnd);
117853 }
117854 start = sqlite3VdbeCurrentAddr(v);
117855 pLevel->op = bRev ? OP_Prev : OP_Next;
117856 pLevel->p1 = iCur;
117857 pLevel->p2 = start;
117858 assert( pLevel->p5==0 );
117859 if( testOp!=OP_Noop ){
117860 iRowidReg = ++pParse->nMem;
117861 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
117862 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
117863 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
117864 VdbeCoverageIf(v, testOp==OP_Le);
117865 VdbeCoverageIf(v, testOp==OP_Lt);
117866 VdbeCoverageIf(v, testOp==OP_Ge);
117867 VdbeCoverageIf(v, testOp==OP_Gt);
117868 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
117869 }
117870 }else if( pLoop->wsFlags & WHERE_INDEXED ){
117871 /* Case 4: A scan using an index.
117872 **
117873 ** The WHERE clause may contain zero or more equality
117874 ** terms ("==" or "IN" operators) that refer to the N
117875 ** left-most columns of the index. It may also contain
117876 ** inequality constraints (>, <, >= or <=) on the indexed
117877 ** column that immediately follows the N equalities. Only
117878 ** the right-most column can be an inequality - the rest must
117879 ** use the "==" and "IN" operators. For example, if the
117880 ** index is on (x,y,z), then the following clauses are all
117881 ** optimized:
117882 **
117883 ** x=5
117884 ** x=5 AND y=10
117885 ** x=5 AND y<10
117886 ** x=5 AND y>5 AND y<10
117887 ** x=5 AND y=5 AND z<=10
117888 **
117889 ** The z<10 term of the following cannot be used, only
117890 ** the x=5 term:
117891 **
117892 ** x=5 AND z<10
117893 **
117894 ** N may be zero if there are inequality constraints.
117895 ** If there are no inequality constraints, then N is at
117896 ** least one.
117897 **
117898 ** This case is also used when there are no WHERE clause
117899 ** constraints but an index is selected anyway, in order
117900 ** to force the output order to conform to an ORDER BY.
117901 */
117902 static const u8 aStartOp[] = {
117903 0,
117904 0,
117905 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
117906 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
117907 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
117908 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
117909 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
117910 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
117911 };
117912 static const u8 aEndOp[] = {
117913 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
117914 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
117915 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
117916 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
117917 };
117918 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
117919 int regBase; /* Base register holding constraint values */
117920 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
117921 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
117922 int startEq; /* True if range start uses ==, >= or <= */
117923 int endEq; /* True if range end uses ==, >= or <= */
117924 int start_constraints; /* Start of range is constrained */
117925 int nConstraint; /* Number of constraint terms */
117926 Index *pIdx; /* The index we will be using */
117927 int iIdxCur; /* The VDBE cursor for the index */
117928 int nExtraReg = 0; /* Number of extra registers needed */
117929 int op; /* Instruction opcode */
117930 char *zStartAff; /* Affinity for start of range constraint */
117931 char cEndAff = 0; /* Affinity for end of range constraint */
117932 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
117933 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
117934
117935 pIdx = pLoop->u.btree.pIndex;
117936 iIdxCur = pLevel->iIdxCur;
117937 assert( nEq>=pLoop->nSkip );
117938
117939 /* If this loop satisfies a sort order (pOrderBy) request that
117940 ** was passed to this function to implement a "SELECT min(x) ..."
117941 ** query, then the caller will only allow the loop to run for
117942 ** a single iteration. This means that the first row returned
117943 ** should not have a NULL value stored in 'x'. If column 'x' is
117944 ** the first one after the nEq equality constraints in the index,
117945 ** this requires some special handling.
117946 */
117947 assert( pWInfo->pOrderBy==0
117948 || pWInfo->pOrderBy->nExpr==1
117949 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
117950 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
117951 && pWInfo->nOBSat>0
117952 && (pIdx->nKeyCol>nEq)
117953 ){
117954 assert( pLoop->nSkip==0 );
117955 bSeekPastNull = 1;
117956 nExtraReg = 1;
117957 }
117958
117959 /* Find any inequality constraint terms for the start and end
117960 ** of the range.
117961 */
117962 j = nEq;
117963 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
117964 pRangeStart = pLoop->aLTerm[j++];
117965 nExtraReg = 1;
117966 /* Like optimization range constraints always occur in pairs */
117967 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
117968 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
117969 }
117970 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
117971 pRangeEnd = pLoop->aLTerm[j++];
117972 nExtraReg = 1;
117973 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
117974 assert( pRangeStart!=0 ); /* LIKE opt constraints */
117975 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
117976 pLevel->iLikeRepCntr = ++pParse->nMem;
117977 testcase( bRev );
117978 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
117979 sqlite3VdbeAddOp2(v, OP_Integer,
117980 bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
117981 pLevel->iLikeRepCntr);
117982 VdbeComment((v, "LIKE loop counter"));
117983 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
117984 }
117985 if( pRangeStart==0
117986 && (j = pIdx->aiColumn[nEq])>=0
117987 && pIdx->pTable->aCol[j].notNull==0
117988 ){
117989 bSeekPastNull = 1;
117990 }
117991 }
117992 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
117993
117994 /* Generate code to evaluate all constraint terms using == or IN
117995 ** and store the values of those terms in an array of registers
117996 ** starting at regBase.
117997 */
117998 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
117999 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
118000 if( zStartAff ) cEndAff = zStartAff[nEq];
118001 addrNxt = pLevel->addrNxt;
118002
118003 /* If we are doing a reverse order scan on an ascending index, or
118004 ** a forward order scan on a descending index, interchange the
118005 ** start and end terms (pRangeStart and pRangeEnd).
118006 */
118007 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
118008 || (bRev && pIdx->nKeyCol==nEq)
118009 ){
118010 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
118011 SWAP(u8, bSeekPastNull, bStopAtNull);
118012 }
118013
118014 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
118015 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
118016 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
118017 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
118018 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
118019 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
118020 start_constraints = pRangeStart || nEq>0;
118021
118022 /* Seek the index cursor to the start of the range. */
118023 nConstraint = nEq;
118024 if( pRangeStart ){
118025 Expr *pRight = pRangeStart->pExpr->pRight;
118026 sqlite3ExprCode(pParse, pRight, regBase+nEq);
118027 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
118028 if( (pRangeStart->wtFlags & TERM_VNULL)==0
118029 && sqlite3ExprCanBeNull(pRight)
118030 ){
118031 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118032 VdbeCoverage(v);
118033 }
118034 if( zStartAff ){
118035 if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
118036 /* Since the comparison is to be performed with no conversions
118037 ** applied to the operands, set the affinity to apply to pRight to
118038 ** SQLITE_AFF_BLOB. */
118039 zStartAff[nEq] = SQLITE_AFF_BLOB;
118040 }
118041 if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
118042 zStartAff[nEq] = SQLITE_AFF_BLOB;
118043 }
118044 }
118045 nConstraint++;
118046 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
118047 }else if( bSeekPastNull ){
118048 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
118049 nConstraint++;
118050 startEq = 0;
118051 start_constraints = 1;
118052 }
118053 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
118054 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
118055 assert( op!=0 );
118056 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
118057 VdbeCoverage(v);
118058 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
118059 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
118060 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
118061 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
118062 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
118063 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
118064
118065 /* Load the value for the inequality constraint at the end of the
118066 ** range (if any).
118067 */
118068 nConstraint = nEq;
118069 if( pRangeEnd ){
118070 Expr *pRight = pRangeEnd->pExpr->pRight;
118071 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
118072 sqlite3ExprCode(pParse, pRight, regBase+nEq);
118073 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
118074 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
118075 && sqlite3ExprCanBeNull(pRight)
118076 ){
118077 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118078 VdbeCoverage(v);
118079 }
118080 if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
118081 && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
118082 ){
118083 codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
118084 }
118085 nConstraint++;
118086 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
118087 }else if( bStopAtNull ){
118088 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
118089 endEq = 0;
118090 nConstraint++;
118091 }
118092 sqlite3DbFree(db, zStartAff);
118093
118094 /* Top of the loop body */
118095 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
118096
118097 /* Check if the index cursor is past the end of the range. */
118098 if( nConstraint ){
118099 op = aEndOp[bRev*2 + endEq];
118100 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
118101 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
118102 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
118103 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
118104 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
118105 }
118106
118107 /* Seek the table cursor, if required */
118108 disableTerm(pLevel, pRangeStart);
118109 disableTerm(pLevel, pRangeEnd);
118110 if( omitTable ){
118111 /* pIdx is a covering index. No need to access the main table. */
118112 }else if( HasRowid(pIdx->pTable) ){
118113 iRowidReg = ++pParse->nMem;
118114 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
118115 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
118116 sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
118117 }else if( iCur!=iIdxCur ){
118118 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
118119 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
118120 for(j=0; j<pPk->nKeyCol; j++){
118121 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
118122 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
118123 }
118124 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
118125 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
118126 }
118127
118128 /* Record the instruction used to terminate the loop. Disable
118129 ** WHERE clause terms made redundant by the index range scan.
118130 */
118131 if( pLoop->wsFlags & WHERE_ONEROW ){
118132 pLevel->op = OP_Noop;
118133 }else if( bRev ){
118134 pLevel->op = OP_Prev;
118135 }else{
118136 pLevel->op = OP_Next;
118137 }
118138 pLevel->p1 = iIdxCur;
118139 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
118140 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
118141 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
118142 }else{
118143 assert( pLevel->p5==0 );
118144 }
118145 }else
118146
118147 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
118148 if( pLoop->wsFlags & WHERE_MULTI_OR ){
118149 /* Case 5: Two or more separately indexed terms connected by OR
118150 **
118151 ** Example:
118152 **
118153 ** CREATE TABLE t1(a,b,c,d);
118154 ** CREATE INDEX i1 ON t1(a);
118155 ** CREATE INDEX i2 ON t1(b);
118156 ** CREATE INDEX i3 ON t1(c);
118157 **
118158 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
118159 **
118160 ** In the example, there are three indexed terms connected by OR.
118161 ** The top of the loop looks like this:
118162 **
118163 ** Null 1 # Zero the rowset in reg 1
118164 **
118165 ** Then, for each indexed term, the following. The arguments to
118166 ** RowSetTest are such that the rowid of the current row is inserted
118167 ** into the RowSet. If it is already present, control skips the
118168 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
118169 **
118170 ** sqlite3WhereBegin(<term>)
118171 ** RowSetTest # Insert rowid into rowset
118172 ** Gosub 2 A
118173 ** sqlite3WhereEnd()
118174 **
118175 ** Following the above, code to terminate the loop. Label A, the target
118176 ** of the Gosub above, jumps to the instruction right after the Goto.
118177 **
118178 ** Null 1 # Zero the rowset in reg 1
118179 ** Goto B # The loop is finished.
118180 **
118181 ** A: <loop body> # Return data, whatever.
118182 **
118183 ** Return 2 # Jump back to the Gosub
118184 **
118185 ** B: <after the loop>
118186 **
118187 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
118188 ** use an ephemeral index instead of a RowSet to record the primary
118189 ** keys of the rows we have already seen.
118190 **
118191 */
118192 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
118193 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
118194 Index *pCov = 0; /* Potential covering index (or NULL) */
118195 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
118196
118197 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
118198 int regRowset = 0; /* Register for RowSet object */
118199 int regRowid = 0; /* Register holding rowid */
118200 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
118201 int iRetInit; /* Address of regReturn init */
118202 int untestedTerms = 0; /* Some terms not completely tested */
118203 int ii; /* Loop counter */
118204 u16 wctrlFlags; /* Flags for sub-WHERE clause */
118205 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
118206 Table *pTab = pTabItem->pTab;
118207
118208 pTerm = pLoop->aLTerm[0];
118209 assert( pTerm!=0 );
118210 assert( pTerm->eOperator & WO_OR );
118211 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
118212 pOrWc = &pTerm->u.pOrInfo->wc;
118213 pLevel->op = OP_Return;
118214 pLevel->p1 = regReturn;
118215
118216 /* Set up a new SrcList in pOrTab containing the table being scanned
118217 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
118218 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
118219 */
118220 if( pWInfo->nLevel>1 ){
118221 int nNotReady; /* The number of notReady tables */
118222 struct SrcList_item *origSrc; /* Original list of tables */
118223 nNotReady = pWInfo->nLevel - iLevel - 1;
118224 pOrTab = sqlite3StackAllocRaw(db,
118225 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
118226 if( pOrTab==0 ) return notReady;
118227 pOrTab->nAlloc = (u8)(nNotReady + 1);
118228 pOrTab->nSrc = pOrTab->nAlloc;
118229 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
118230 origSrc = pWInfo->pTabList->a;
118231 for(k=1; k<=nNotReady; k++){
118232 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
118233 }
118234 }else{
118235 pOrTab = pWInfo->pTabList;
118236 }
118237
118238 /* Initialize the rowset register to contain NULL. An SQL NULL is
118239 ** equivalent to an empty rowset. Or, create an ephemeral index
118240 ** capable of holding primary keys in the case of a WITHOUT ROWID.
118241 **
118242 ** Also initialize regReturn to contain the address of the instruction
118243 ** immediately following the OP_Return at the bottom of the loop. This
118244 ** is required in a few obscure LEFT JOIN cases where control jumps
118245 ** over the top of the loop into the body of it. In this case the
118246 ** correct response for the end-of-loop code (the OP_Return) is to
118247 ** fall through to the next instruction, just as an OP_Next does if
118248 ** called on an uninitialized cursor.
118249 */
118250 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
118251 if( HasRowid(pTab) ){
118252 regRowset = ++pParse->nMem;
118253 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
118254 }else{
118255 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
118256 regRowset = pParse->nTab++;
118257 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
118258 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
118259 }
118260 regRowid = ++pParse->nMem;
118261 }
118262 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
118263
118264 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
118265 ** Then for every term xN, evaluate as the subexpression: xN AND z
118266 ** That way, terms in y that are factored into the disjunction will
118267 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
118268 **
118269 ** Actually, each subexpression is converted to "xN AND w" where w is
118270 ** the "interesting" terms of z - terms that did not originate in the
118271 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
118272 ** indices.
118273 **
118274 ** This optimization also only applies if the (x1 OR x2 OR ...) term
118275 ** is not contained in the ON clause of a LEFT JOIN.
118276 ** See ticket http://www.sqlite.org/src/info/f2369304e4
118277 */
118278 if( pWC->nTerm>1 ){
118279 int iTerm;
118280 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
118281 Expr *pExpr = pWC->a[iTerm].pExpr;
118282 if( &pWC->a[iTerm] == pTerm ) continue;
118283 if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
118284 if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
118285 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
118286 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
118287 pExpr = sqlite3ExprDup(db, pExpr, 0);
118288 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
118289 }
118290 if( pAndExpr ){
118291 pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
118292 }
118293 }
118294
118295 /* Run a separate WHERE clause for each term of the OR clause. After
118296 ** eliminating duplicates from other WHERE clauses, the action for each
118297 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
118298 */
118299 wctrlFlags = WHERE_OMIT_OPEN_CLOSE
118300 | WHERE_FORCE_TABLE
118301 | WHERE_ONETABLE_ONLY
118302 | WHERE_NO_AUTOINDEX;
118303 for(ii=0; ii<pOrWc->nTerm; ii++){
118304 WhereTerm *pOrTerm = &pOrWc->a[ii];
118305 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
118306 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
118307 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
118308 int j1 = 0; /* Address of jump operation */
118309 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
118310 pAndExpr->pLeft = pOrExpr;
118311 pOrExpr = pAndExpr;
118312 }
118313 /* Loop through table entries that match term pOrTerm. */
118314 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
118315 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
118316 wctrlFlags, iCovCur);
118317 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
118318 if( pSubWInfo ){
118319 WhereLoop *pSubLoop;
118320 int addrExplain = sqlite3WhereExplainOneScan(
118321 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
118322 );
118323 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
118324
118325 /* This is the sub-WHERE clause body. First skip over
118326 ** duplicate rows from prior sub-WHERE clauses, and record the
118327 ** rowid (or PRIMARY KEY) for the current row so that the same
118328 ** row will be skipped in subsequent sub-WHERE clauses.
118329 */
118330 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
118331 int r;
118332 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
118333 if( HasRowid(pTab) ){
118334 r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
118335 j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
118336 VdbeCoverage(v);
118337 }else{
118338 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
118339 int nPk = pPk->nKeyCol;
118340 int iPk;
118341
118342 /* Read the PK into an array of temp registers. */
118343 r = sqlite3GetTempRange(pParse, nPk);
118344 for(iPk=0; iPk<nPk; iPk++){
118345 int iCol = pPk->aiColumn[iPk];
118346 sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
118347 }
118348
118349 /* Check if the temp table already contains this key. If so,
118350 ** the row has already been included in the result set and
118351 ** can be ignored (by jumping past the Gosub below). Otherwise,
118352 ** insert the key into the temp table and proceed with processing
118353 ** the row.
118354 **
118355 ** Use some of the same optimizations as OP_RowSetTest: If iSet
118356 ** is zero, assume that the key cannot already be present in
118357 ** the temp table. And if iSet is -1, assume that there is no
118358 ** need to insert the key into the temp table, as it will never
118359 ** be tested for. */
118360 if( iSet ){
118361 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
118362 VdbeCoverage(v);
118363 }
118364 if( iSet>=0 ){
118365 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
118366 sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
118367 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
118368 }
118369
118370 /* Release the array of temp registers */
118371 sqlite3ReleaseTempRange(pParse, r, nPk);
118372 }
118373 }
118374
118375 /* Invoke the main loop body as a subroutine */
118376 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
118377
118378 /* Jump here (skipping the main loop body subroutine) if the
118379 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
118380 if( j1 ) sqlite3VdbeJumpHere(v, j1);
118381
118382 /* The pSubWInfo->untestedTerms flag means that this OR term
118383 ** contained one or more AND term from a notReady table. The
118384 ** terms from the notReady table could not be tested and will
118385 ** need to be tested later.
118386 */
118387 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
118388
118389 /* If all of the OR-connected terms are optimized using the same
118390 ** index, and the index is opened using the same cursor number
118391 ** by each call to sqlite3WhereBegin() made by this loop, it may
118392 ** be possible to use that index as a covering index.
118393 **
118394 ** If the call to sqlite3WhereBegin() above resulted in a scan that
118395 ** uses an index, and this is either the first OR-connected term
118396 ** processed or the index is the same as that used by all previous
118397 ** terms, set pCov to the candidate covering index. Otherwise, set
118398 ** pCov to NULL to indicate that no candidate covering index will
118399 ** be available.
118400 */
118401 pSubLoop = pSubWInfo->a[0].pWLoop;
118402 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
118403 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
118404 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
118405 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
118406 ){
118407 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
118408 pCov = pSubLoop->u.btree.pIndex;
118409 wctrlFlags |= WHERE_REOPEN_IDX;
118410 }else{
118411 pCov = 0;
118412 }
118413
118414 /* Finish the loop through table entries that match term pOrTerm. */
118415 sqlite3WhereEnd(pSubWInfo);
118416 }
118417 }
118418 }
118419 pLevel->u.pCovidx = pCov;
118420 if( pCov ) pLevel->iIdxCur = iCovCur;
118421 if( pAndExpr ){
118422 pAndExpr->pLeft = 0;
118423 sqlite3ExprDelete(db, pAndExpr);
118424 }
118425 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
118426 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
118427 sqlite3VdbeResolveLabel(v, iLoopBody);
118428
118429 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
118430 if( !untestedTerms ) disableTerm(pLevel, pTerm);
118431 }else
118432 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
118433
118434 {
118435 /* Case 6: There is no usable index. We must do a complete
118436 ** scan of the entire table.
118437 */
118438 static const u8 aStep[] = { OP_Next, OP_Prev };
118439 static const u8 aStart[] = { OP_Rewind, OP_Last };
118440 assert( bRev==0 || bRev==1 );
118441 if( pTabItem->isRecursive ){
118442 /* Tables marked isRecursive have only a single row that is stored in
118443 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
118444 pLevel->op = OP_Noop;
118445 }else{
118446 pLevel->op = aStep[bRev];
118447 pLevel->p1 = iCur;
118448 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
118449 VdbeCoverageIf(v, bRev==0);
118450 VdbeCoverageIf(v, bRev!=0);
118451 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
118452 }
118453 }
118454
118455 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
118456 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
118457 #endif
118458
118459 /* Insert code to test every subexpression that can be completely
118460 ** computed using the current set of tables.
118461 */
118462 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
118463 Expr *pE;
118464 int skipLikeAddr = 0;
118465 testcase( pTerm->wtFlags & TERM_VIRTUAL );
118466 testcase( pTerm->wtFlags & TERM_CODED );
118467 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118468 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
118469 testcase( pWInfo->untestedTerms==0
118470 && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
118471 pWInfo->untestedTerms = 1;
118472 continue;
118473 }
118474 pE = pTerm->pExpr;
118475 assert( pE!=0 );
118476 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
118477 continue;
118478 }
118479 if( pTerm->wtFlags & TERM_LIKECOND ){
118480 assert( pLevel->iLikeRepCntr>0 );
118481 skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
118482 VdbeCoverage(v);
118483 }
118484 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
118485 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
118486 pTerm->wtFlags |= TERM_CODED;
118487 }
118488
118489 /* Insert code to test for implied constraints based on transitivity
118490 ** of the "==" operator.
118491 **
118492 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
118493 ** and we are coding the t1 loop and the t2 loop has not yet coded,
118494 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
118495 ** the implied "t1.a=123" constraint.
118496 */
118497 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
118498 Expr *pE, *pEAlt;
118499 WhereTerm *pAlt;
118500 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118501 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
118502 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
118503 if( pTerm->leftCursor!=iCur ) continue;
118504 if( pLevel->iLeftJoin ) continue;
118505 pE = pTerm->pExpr;
118506 assert( !ExprHasProperty(pE, EP_FromJoin) );
118507 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
118508 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
118509 WO_EQ|WO_IN|WO_IS, 0);
118510 if( pAlt==0 ) continue;
118511 if( pAlt->wtFlags & (TERM_CODED) ) continue;
118512 testcase( pAlt->eOperator & WO_EQ );
118513 testcase( pAlt->eOperator & WO_IS );
118514 testcase( pAlt->eOperator & WO_IN );
118515 VdbeModuleComment((v, "begin transitive constraint"));
118516 pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
118517 if( pEAlt ){
118518 *pEAlt = *pAlt->pExpr;
118519 pEAlt->pLeft = pE->pLeft;
118520 sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
118521 sqlite3StackFree(db, pEAlt);
118522 }
118523 }
118524
118525 /* For a LEFT OUTER JOIN, generate code that will record the fact that
118526 ** at least one row of the right table has matched the left table.
118527 */
118528 if( pLevel->iLeftJoin ){
118529 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
118530 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
118531 VdbeComment((v, "record LEFT JOIN hit"));
118532 sqlite3ExprCacheClear(pParse);
118533 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
118534 testcase( pTerm->wtFlags & TERM_VIRTUAL );
118535 testcase( pTerm->wtFlags & TERM_CODED );
118536 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118537 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
118538 assert( pWInfo->untestedTerms );
118539 continue;
118540 }
118541 assert( pTerm->pExpr );
118542 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
118543 pTerm->wtFlags |= TERM_CODED;
118544 }
118545 }
118546
118547 return pLevel->notReady;
118548 }
118549
118550 /************** End of wherecode.c *******************************************/
118551 /************** Begin file whereexpr.c ***************************************/
118552 /*
118553 ** 2015-06-08
118554 **
118555 ** The author disclaims copyright to this source code. In place of
118556 ** a legal notice, here is a blessing:
118557 **
118558 ** May you do good and not evil.
118559 ** May you find forgiveness for yourself and forgive others.
118560 ** May you share freely, never taking more than you give.
118561 **
118562 *************************************************************************
118563 ** This module contains C code that generates VDBE code used to process
118564 ** the WHERE clause of SQL statements.
118565 **
118566 ** This file was originally part of where.c but was split out to improve
118567 ** readability and editabiliity. This file contains utility routines for
118568 ** analyzing Expr objects in the WHERE clause.
118569 */
118570
118571 /* Forward declarations */
118572 static void exprAnalyze(SrcList*, WhereClause*, int);
118573
118574 /*
118575 ** Deallocate all memory associated with a WhereOrInfo object.
118576 */
118577 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
118578 sqlite3WhereClauseClear(&p->wc);
118579 sqlite3DbFree(db, p);
118580 }
118581
118582 /*
118583 ** Deallocate all memory associated with a WhereAndInfo object.
118584 */
118585 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
118586 sqlite3WhereClauseClear(&p->wc);
118587 sqlite3DbFree(db, p);
118588 }
118589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118590 /*
118591 ** Add a single new WhereTerm entry to the WhereClause object pWC.
118592 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
118593 ** The index in pWC->a[] of the new WhereTerm is returned on success.
118594 ** 0 is returned if the new WhereTerm could not be added due to a memory
@@ -116527,126 +118639,10 @@
118639 pTerm->pWC = pWC;
118640 pTerm->iParent = -1;
118641 return idx;
118642 }
118643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118644 /*
118645 ** Return TRUE if the given operator is one of the operators that is
118646 ** allowed for an indexable WHERE clause term. The allowed operators are
118647 ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
118648 */
@@ -116723,203 +118719,10 @@
118719 assert( op!=TK_GE || c==WO_GE );
118720 assert( op!=TK_IS || c==WO_IS );
118721 return c;
118722 }
118723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118724
118725 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
118726 /*
118727 ** Check to see if the given expression is a LIKE or GLOB operator that
118728 ** can be optimized using inequality constraints. Return TRUE if it is
@@ -116970,11 +118773,11 @@
118773 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
118774 op = pRight->op;
118775 if( op==TK_VARIABLE ){
118776 Vdbe *pReprepare = pParse->pReprepare;
118777 int iCol = pRight->iColumn;
118778 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
118779 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
118780 z = (char *)sqlite3_value_text(pVal);
118781 }
118782 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
118783 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
@@ -117181,11 +118984,11 @@
118984 **
118985 ** x IN (expr1,expr2,expr3)
118986 **
118987 ** CASE 2:
118988 **
118989 ** If there are exactly two disjuncts and one side has x>A and the other side
118990 ** has x=A (for the same x and A) then add a new virtual conjunct term to the
118991 ** WHERE clause of the form "x>=A". Example:
118992 **
118993 ** x>A OR (x=A AND y>B) adds: x>=A
118994 **
@@ -117210,26 +119013,26 @@
119013 ** potentially be used with an index if an appropriate index exists.
119014 ** This analysis does not consider whether or not the index exists; that
119015 ** is decided elsewhere. This analysis only looks at whether subterms
119016 ** appropriate for indexing exist.
119017 **
119018 ** All examples A through E above satisfy case 3. But if a term
119019 ** also satisfies case 1 (such as B) we know that the optimizer will
119020 ** always prefer case 1, so in that case we pretend that case 3 is not
119021 ** satisfied.
119022 **
119023 ** It might be the case that multiple tables are indexable. For example,
119024 ** (E) above is indexable on tables P, Q, and R.
119025 **
119026 ** Terms that satisfy case 3 are candidates for lookup by using
119027 ** separate indices to find rowids for each subterm and composing
119028 ** the union of all rowids using a RowSet object. This is similar
119029 ** to "bitmap indices" in other database engines.
119030 **
119031 ** OTHERWISE:
119032 **
119033 ** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
119034 ** zero. This term is not useful for search.
119035 */
119036 static void exprAnalyzeOrTerm(
119037 SrcList *pSrc, /* the FROM clause */
119038 WhereClause *pWC, /* the complete WHERE clause */
@@ -117256,18 +119059,18 @@
119059 assert( pExpr->op==TK_OR );
119060 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
119061 if( pOrInfo==0 ) return;
119062 pTerm->wtFlags |= TERM_ORINFO;
119063 pOrWc = &pOrInfo->wc;
119064 sqlite3WhereClauseInit(pOrWc, pWInfo);
119065 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
119066 sqlite3WhereExprAnalyze(pSrc, pOrWc);
119067 if( db->mallocFailed ) return;
119068 assert( pOrWc->nTerm>=2 );
119069
119070 /*
119071 ** Compute the set of tables that might satisfy cases 1 or 3.
119072 */
119073 indexable = ~(Bitmask)0;
119074 chngToIN = ~(Bitmask)0;
119075 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
119076 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
@@ -117282,20 +119085,20 @@
119085 Bitmask b = 0;
119086 pOrTerm->u.pAndInfo = pAndInfo;
119087 pOrTerm->wtFlags |= TERM_ANDINFO;
119088 pOrTerm->eOperator = WO_AND;
119089 pAndWC = &pAndInfo->wc;
119090 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
119091 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
119092 sqlite3WhereExprAnalyze(pSrc, pAndWC);
119093 pAndWC->pOuter = pWC;
119094 testcase( db->mallocFailed );
119095 if( !db->mallocFailed ){
119096 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
119097 assert( pAndTerm->pExpr );
119098 if( allowedOp(pAndTerm->pExpr->op) ){
119099 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
119100 }
119101 }
119102 }
119103 indexable &= b;
119104 }
@@ -117302,14 +119105,14 @@
119105 }else if( pOrTerm->wtFlags & TERM_COPIED ){
119106 /* Skip this term for now. We revisit it when we process the
119107 ** corresponding TERM_VIRTUAL term */
119108 }else{
119109 Bitmask b;
119110 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
119111 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
119112 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
119113 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
119114 }
119115 indexable &= b;
119116 if( (pOrTerm->eOperator & WO_EQ)==0 ){
119117 chngToIN = 0;
119118 }else{
@@ -117381,11 +119184,12 @@
119184 /* This is the 2-bit case and we are on the second iteration and
119185 ** current term is from the first iteration. So skip this term. */
119186 assert( j==1 );
119187 continue;
119188 }
119189 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
119190 pOrTerm->leftCursor))==0 ){
119191 /* This term must be of the form t1.a==t2.b where t2 is in the
119192 ** chngToIN set but t1 is not. This term will be either preceded
119193 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
119194 ** and use its inversion. */
119195 testcase( pOrTerm->wtFlags & TERM_COPIED );
@@ -117400,11 +119204,11 @@
119204 if( i<0 ){
119205 /* No candidate table+column was found. This can only occur
119206 ** on the second iteration */
119207 assert( j==1 );
119208 assert( IsPowerOfTwo(chngToIN) );
119209 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
119210 break;
119211 }
119212 testcase( j==1 );
119213
119214 /* We have found a candidate table and column. Check to see if that
@@ -117478,11 +119282,11 @@
119282 ** We already know that pExpr is a binary operator where both operands are
119283 ** column references. This routine checks to see if pExpr is an equivalence
119284 ** relation:
119285 ** 1. The SQLITE_Transitive optimization must be enabled
119286 ** 2. Must be either an == or an IS operator
119287 ** 3. Not originating in the ON clause of an OUTER JOIN
119288 ** 4. The affinities of A and B must be compatible
119289 ** 5a. Both operands use the same collating sequence OR
119290 ** 5b. The overall collating sequence is BINARY
119291 ** If this routine returns TRUE, that means that the RHS can be substituted
119292 ** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
@@ -117511,10 +119315,36 @@
119315 zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
119316 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
119317 zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
119318 return sqlite3StrICmp(zColl1, zColl2)==0;
119319 }
119320
119321 /*
119322 ** Recursively walk the expressions of a SELECT statement and generate
119323 ** a bitmask indicating which tables are used in that expression
119324 ** tree.
119325 */
119326 static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
119327 Bitmask mask = 0;
119328 while( pS ){
119329 SrcList *pSrc = pS->pSrc;
119330 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
119331 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
119332 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
119333 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
119334 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
119335 if( ALWAYS(pSrc!=0) ){
119336 int i;
119337 for(i=0; i<pSrc->nSrc; i++){
119338 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
119339 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
119340 }
119341 }
119342 pS = pS->pPrior;
119343 }
119344 return mask;
119345 }
119346
119347 /*
119348 ** The input to this routine is an WhereTerm structure with only the
119349 ** "pExpr" field filled in. The job of this routine is to analyze the
119350 ** subexpression and populate all the other fields of the WhereTerm
@@ -117556,27 +119386,27 @@
119386 }
119387 pTerm = &pWC->a[idxTerm];
119388 pMaskSet = &pWInfo->sMaskSet;
119389 pExpr = pTerm->pExpr;
119390 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
119391 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
119392 op = pExpr->op;
119393 if( op==TK_IN ){
119394 assert( pExpr->pRight==0 );
119395 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
119396 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
119397 }else{
119398 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
119399 }
119400 }else if( op==TK_ISNULL ){
119401 pTerm->prereqRight = 0;
119402 }else{
119403 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
119404 }
119405 prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
119406 if( ExprHasProperty(pExpr, EP_FromJoin) ){
119407 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
119408 prereqAll |= x;
119409 extraRight = x-1; /* ON clause terms may not be used with an index
119410 ** on left table of a LEFT JOIN. Ticket #3015 */
119411 }
119412 pTerm->prereqAll = prereqAll;
@@ -117777,12 +119607,12 @@
119607 WhereTerm *pNewTerm;
119608 Bitmask prereqColumn, prereqExpr;
119609
119610 pRight = pExpr->x.pList->a[0].pExpr;
119611 pLeft = pExpr->x.pList->a[1].pExpr;
119612 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
119613 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
119614 if( (prereqExpr & prereqColumn)==0 ){
119615 Expr *pNewExpr;
119616 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
119617 0, sqlite3ExprDup(db, pRight, 0), 0);
119618 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
@@ -117841,10 +119671,477 @@
119671 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
119672 ** an index for tables to the left of the join.
119673 */
119674 pTerm->prereqRight |= extraRight;
119675 }
119676
119677 /***************************************************************************
119678 ** Routines with file scope above. Interface to the rest of the where.c
119679 ** subsystem follows.
119680 ***************************************************************************/
119681
119682 /*
119683 ** This routine identifies subexpressions in the WHERE clause where
119684 ** each subexpression is separated by the AND operator or some other
119685 ** operator specified in the op parameter. The WhereClause structure
119686 ** is filled with pointers to subexpressions. For example:
119687 **
119688 ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
119689 ** \________/ \_______________/ \________________/
119690 ** slot[0] slot[1] slot[2]
119691 **
119692 ** The original WHERE clause in pExpr is unaltered. All this routine
119693 ** does is make slot[] entries point to substructure within pExpr.
119694 **
119695 ** In the previous sentence and in the diagram, "slot[]" refers to
119696 ** the WhereClause.a[] array. The slot[] array grows as needed to contain
119697 ** all terms of the WHERE clause.
119698 */
119699 SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
119700 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
119701 pWC->op = op;
119702 if( pE2==0 ) return;
119703 if( pE2->op!=op ){
119704 whereClauseInsert(pWC, pExpr, 0);
119705 }else{
119706 sqlite3WhereSplit(pWC, pE2->pLeft, op);
119707 sqlite3WhereSplit(pWC, pE2->pRight, op);
119708 }
119709 }
119710
119711 /*
119712 ** Initialize a preallocated WhereClause structure.
119713 */
119714 SQLITE_PRIVATE void sqlite3WhereClauseInit(
119715 WhereClause *pWC, /* The WhereClause to be initialized */
119716 WhereInfo *pWInfo /* The WHERE processing context */
119717 ){
119718 pWC->pWInfo = pWInfo;
119719 pWC->pOuter = 0;
119720 pWC->nTerm = 0;
119721 pWC->nSlot = ArraySize(pWC->aStatic);
119722 pWC->a = pWC->aStatic;
119723 }
119724
119725 /*
119726 ** Deallocate a WhereClause structure. The WhereClause structure
119727 ** itself is not freed. This routine is the inverse of sqlite3WhereClauseInit().
119728 */
119729 SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
119730 int i;
119731 WhereTerm *a;
119732 sqlite3 *db = pWC->pWInfo->pParse->db;
119733 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
119734 if( a->wtFlags & TERM_DYNAMIC ){
119735 sqlite3ExprDelete(db, a->pExpr);
119736 }
119737 if( a->wtFlags & TERM_ORINFO ){
119738 whereOrInfoDelete(db, a->u.pOrInfo);
119739 }else if( a->wtFlags & TERM_ANDINFO ){
119740 whereAndInfoDelete(db, a->u.pAndInfo);
119741 }
119742 }
119743 if( pWC->a!=pWC->aStatic ){
119744 sqlite3DbFree(db, pWC->a);
119745 }
119746 }
119747
119748
119749 /*
119750 ** These routines walk (recursively) an expression tree and generate
119751 ** a bitmask indicating which tables are used in that expression
119752 ** tree.
119753 */
119754 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
119755 Bitmask mask = 0;
119756 if( p==0 ) return 0;
119757 if( p->op==TK_COLUMN ){
119758 mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
119759 return mask;
119760 }
119761 mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
119762 mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
119763 if( ExprHasProperty(p, EP_xIsSelect) ){
119764 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
119765 }else{
119766 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
119767 }
119768 return mask;
119769 }
119770 SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
119771 int i;
119772 Bitmask mask = 0;
119773 if( pList ){
119774 for(i=0; i<pList->nExpr; i++){
119775 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
119776 }
119777 }
119778 return mask;
119779 }
119780
119781
119782 /*
119783 ** Call exprAnalyze on all terms in a WHERE clause.
119784 **
119785 ** Note that exprAnalyze() might add new virtual terms onto the
119786 ** end of the WHERE clause. We do not want to analyze these new
119787 ** virtual terms, so start analyzing at the end and work forward
119788 ** so that the added virtual terms are never processed.
119789 */
119790 SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
119791 SrcList *pTabList, /* the FROM clause */
119792 WhereClause *pWC /* the WHERE clause to be analyzed */
119793 ){
119794 int i;
119795 for(i=pWC->nTerm-1; i>=0; i--){
119796 exprAnalyze(pTabList, pWC, i);
119797 }
119798 }
119799
119800 /************** End of whereexpr.c *******************************************/
119801 /************** Begin file where.c *******************************************/
119802 /*
119803 ** 2001 September 15
119804 **
119805 ** The author disclaims copyright to this source code. In place of
119806 ** a legal notice, here is a blessing:
119807 **
119808 ** May you do good and not evil.
119809 ** May you find forgiveness for yourself and forgive others.
119810 ** May you share freely, never taking more than you give.
119811 **
119812 *************************************************************************
119813 ** This module contains C code that generates VDBE code used to process
119814 ** the WHERE clause of SQL statements. This module is responsible for
119815 ** generating the code that loops through a table looking for applicable
119816 ** rows. Indices are selected and used to speed the search when doing
119817 ** so is applicable. Because this module is responsible for selecting
119818 ** indices, you might also think of this module as the "query optimizer".
119819 */
119820
119821 /* Forward declaration of methods */
119822 static int whereLoopResize(sqlite3*, WhereLoop*, int);
119823
119824 /* Test variable that can be set to enable WHERE tracing */
119825 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
119826 /***/ int sqlite3WhereTrace = 0;
119827 #endif
119828
119829
119830 /*
119831 ** Return the estimated number of output rows from a WHERE clause
119832 */
119833 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
119834 return sqlite3LogEstToInt(pWInfo->nRowOut);
119835 }
119836
119837 /*
119838 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
119839 ** WHERE clause returns outputs for DISTINCT processing.
119840 */
119841 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
119842 return pWInfo->eDistinct;
119843 }
119844
119845 /*
119846 ** Return TRUE if the WHERE clause returns rows in ORDER BY order.
119847 ** Return FALSE if the output needs to be sorted.
119848 */
119849 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
119850 return pWInfo->nOBSat;
119851 }
119852
119853 /*
119854 ** Return the VDBE address or label to jump to in order to continue
119855 ** immediately with the next row of a WHERE clause.
119856 */
119857 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
119858 assert( pWInfo->iContinue!=0 );
119859 return pWInfo->iContinue;
119860 }
119861
119862 /*
119863 ** Return the VDBE address or label to jump to in order to break
119864 ** out of a WHERE loop.
119865 */
119866 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
119867 return pWInfo->iBreak;
119868 }
119869
119870 /*
119871 ** Return TRUE if an UPDATE or DELETE statement can operate directly on
119872 ** the rowids returned by a WHERE clause. Return FALSE if doing an
119873 ** UPDATE or DELETE might change subsequent WHERE clause results.
119874 **
119875 ** If the ONEPASS optimization is used (if this routine returns true)
119876 ** then also write the indices of open cursors used by ONEPASS
119877 ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
119878 ** table and iaCur[1] gets the cursor used by an auxiliary index.
119879 ** Either value may be -1, indicating that cursor is not used.
119880 ** Any cursors returned will have been opened for writing.
119881 **
119882 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
119883 ** unable to use the ONEPASS optimization.
119884 */
119885 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
119886 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
119887 return pWInfo->okOnePass;
119888 }
119889
119890 /*
119891 ** Move the content of pSrc into pDest
119892 */
119893 static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
119894 pDest->n = pSrc->n;
119895 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
119896 }
119897
119898 /*
119899 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
119900 **
119901 ** The new entry might overwrite an existing entry, or it might be
119902 ** appended, or it might be discarded. Do whatever is the right thing
119903 ** so that pSet keeps the N_OR_COST best entries seen so far.
119904 */
119905 static int whereOrInsert(
119906 WhereOrSet *pSet, /* The WhereOrSet to be updated */
119907 Bitmask prereq, /* Prerequisites of the new entry */
119908 LogEst rRun, /* Run-cost of the new entry */
119909 LogEst nOut /* Number of outputs for the new entry */
119910 ){
119911 u16 i;
119912 WhereOrCost *p;
119913 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
119914 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
119915 goto whereOrInsert_done;
119916 }
119917 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
119918 return 0;
119919 }
119920 }
119921 if( pSet->n<N_OR_COST ){
119922 p = &pSet->a[pSet->n++];
119923 p->nOut = nOut;
119924 }else{
119925 p = pSet->a;
119926 for(i=1; i<pSet->n; i++){
119927 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
119928 }
119929 if( p->rRun<=rRun ) return 0;
119930 }
119931 whereOrInsert_done:
119932 p->prereq = prereq;
119933 p->rRun = rRun;
119934 if( p->nOut>nOut ) p->nOut = nOut;
119935 return 1;
119936 }
119937
119938 /*
119939 ** Return the bitmask for the given cursor number. Return 0 if
119940 ** iCursor is not in the set.
119941 */
119942 SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
119943 int i;
119944 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
119945 for(i=0; i<pMaskSet->n; i++){
119946 if( pMaskSet->ix[i]==iCursor ){
119947 return MASKBIT(i);
119948 }
119949 }
119950 return 0;
119951 }
119952
119953 /*
119954 ** Create a new mask for cursor iCursor.
119955 **
119956 ** There is one cursor per table in the FROM clause. The number of
119957 ** tables in the FROM clause is limited by a test early in the
119958 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
119959 ** array will never overflow.
119960 */
119961 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
119962 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
119963 pMaskSet->ix[pMaskSet->n++] = iCursor;
119964 }
119965
119966 /*
119967 ** Advance to the next WhereTerm that matches according to the criteria
119968 ** established when the pScan object was initialized by whereScanInit().
119969 ** Return NULL if there are no more matching WhereTerms.
119970 */
119971 static WhereTerm *whereScanNext(WhereScan *pScan){
119972 int iCur; /* The cursor on the LHS of the term */
119973 int iColumn; /* The column on the LHS of the term. -1 for IPK */
119974 Expr *pX; /* An expression being tested */
119975 WhereClause *pWC; /* Shorthand for pScan->pWC */
119976 WhereTerm *pTerm; /* The term being tested */
119977 int k = pScan->k; /* Where to start scanning */
119978
119979 while( pScan->iEquiv<=pScan->nEquiv ){
119980 iCur = pScan->aEquiv[pScan->iEquiv-2];
119981 iColumn = pScan->aEquiv[pScan->iEquiv-1];
119982 while( (pWC = pScan->pWC)!=0 ){
119983 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
119984 if( pTerm->leftCursor==iCur
119985 && pTerm->u.leftColumn==iColumn
119986 && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
119987 ){
119988 if( (pTerm->eOperator & WO_EQUIV)!=0
119989 && pScan->nEquiv<ArraySize(pScan->aEquiv)
119990 ){
119991 int j;
119992 pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
119993 assert( pX->op==TK_COLUMN );
119994 for(j=0; j<pScan->nEquiv; j+=2){
119995 if( pScan->aEquiv[j]==pX->iTable
119996 && pScan->aEquiv[j+1]==pX->iColumn ){
119997 break;
119998 }
119999 }
120000 if( j==pScan->nEquiv ){
120001 pScan->aEquiv[j] = pX->iTable;
120002 pScan->aEquiv[j+1] = pX->iColumn;
120003 pScan->nEquiv += 2;
120004 }
120005 }
120006 if( (pTerm->eOperator & pScan->opMask)!=0 ){
120007 /* Verify the affinity and collating sequence match */
120008 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
120009 CollSeq *pColl;
120010 Parse *pParse = pWC->pWInfo->pParse;
120011 pX = pTerm->pExpr;
120012 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
120013 continue;
120014 }
120015 assert(pX->pLeft);
120016 pColl = sqlite3BinaryCompareCollSeq(pParse,
120017 pX->pLeft, pX->pRight);
120018 if( pColl==0 ) pColl = pParse->db->pDfltColl;
120019 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
120020 continue;
120021 }
120022 }
120023 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
120024 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
120025 && pX->iTable==pScan->aEquiv[0]
120026 && pX->iColumn==pScan->aEquiv[1]
120027 ){
120028 testcase( pTerm->eOperator & WO_IS );
120029 continue;
120030 }
120031 pScan->k = k+1;
120032 return pTerm;
120033 }
120034 }
120035 }
120036 pScan->pWC = pScan->pWC->pOuter;
120037 k = 0;
120038 }
120039 pScan->pWC = pScan->pOrigWC;
120040 k = 0;
120041 pScan->iEquiv += 2;
120042 }
120043 return 0;
120044 }
120045
120046 /*
120047 ** Initialize a WHERE clause scanner object. Return a pointer to the
120048 ** first match. Return NULL if there are no matches.
120049 **
120050 ** The scanner will be searching the WHERE clause pWC. It will look
120051 ** for terms of the form "X <op> <expr>" where X is column iColumn of table
120052 ** iCur. The <op> must be one of the operators described by opMask.
120053 **
120054 ** If the search is for X and the WHERE clause contains terms of the
120055 ** form X=Y then this routine might also return terms of the form
120056 ** "Y <op> <expr>". The number of levels of transitivity is limited,
120057 ** but is enough to handle most commonly occurring SQL statements.
120058 **
120059 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with
120060 ** index pIdx.
120061 */
120062 static WhereTerm *whereScanInit(
120063 WhereScan *pScan, /* The WhereScan object being initialized */
120064 WhereClause *pWC, /* The WHERE clause to be scanned */
120065 int iCur, /* Cursor to scan for */
120066 int iColumn, /* Column to scan for */
120067 u32 opMask, /* Operator(s) to scan for */
120068 Index *pIdx /* Must be compatible with this index */
120069 ){
120070 int j;
120071
120072 /* memset(pScan, 0, sizeof(*pScan)); */
120073 pScan->pOrigWC = pWC;
120074 pScan->pWC = pWC;
120075 if( pIdx && iColumn>=0 ){
120076 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
120077 for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
120078 if( NEVER(j>pIdx->nColumn) ) return 0;
120079 }
120080 pScan->zCollName = pIdx->azColl[j];
120081 }else{
120082 pScan->idxaff = 0;
120083 pScan->zCollName = 0;
120084 }
120085 pScan->opMask = opMask;
120086 pScan->k = 0;
120087 pScan->aEquiv[0] = iCur;
120088 pScan->aEquiv[1] = iColumn;
120089 pScan->nEquiv = 2;
120090 pScan->iEquiv = 2;
120091 return whereScanNext(pScan);
120092 }
120093
120094 /*
120095 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
120096 ** where X is a reference to the iColumn of table iCur and <op> is one of
120097 ** the WO_xx operator codes specified by the op parameter.
120098 ** Return a pointer to the term. Return 0 if not found.
120099 **
120100 ** The term returned might by Y=<expr> if there is another constraint in
120101 ** the WHERE clause that specifies that X=Y. Any such constraints will be
120102 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
120103 ** aEquiv[] array holds X and all its equivalents, with each SQL variable
120104 ** taking up two slots in aEquiv[]. The first slot is for the cursor number
120105 ** and the second is for the column number. There are 22 slots in aEquiv[]
120106 ** so that means we can look for X plus up to 10 other equivalent values.
120107 ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
120108 ** and ... and A9=A10 and A10=<expr>.
120109 **
120110 ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
120111 ** then try for the one with no dependencies on <expr> - in other words where
120112 ** <expr> is a constant expression of some kind. Only return entries of
120113 ** the form "X <op> Y" where Y is a column in another table if no terms of
120114 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
120115 ** exist, try to return a term that does not use WO_EQUIV.
120116 */
120117 SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
120118 WhereClause *pWC, /* The WHERE clause to be searched */
120119 int iCur, /* Cursor number of LHS */
120120 int iColumn, /* Column number of LHS */
120121 Bitmask notReady, /* RHS must not overlap with this mask */
120122 u32 op, /* Mask of WO_xx values describing operator */
120123 Index *pIdx /* Must be compatible with this index, if not NULL */
120124 ){
120125 WhereTerm *pResult = 0;
120126 WhereTerm *p;
120127 WhereScan scan;
120128
120129 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
120130 op &= WO_EQ|WO_IS;
120131 while( p ){
120132 if( (p->prereqRight & notReady)==0 ){
120133 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
120134 testcase( p->eOperator & WO_IS );
120135 return p;
120136 }
120137 if( pResult==0 ) pResult = p;
120138 }
120139 p = whereScanNext(&scan);
120140 }
120141 return pResult;
120142 }
120143
120144 /*
120145 ** This function searches pList for an entry that matches the iCol-th column
120146 ** of index pIdx.
120147 **
@@ -117879,12 +120176,12 @@
120176
120177 /*
120178 ** Return true if the DISTINCT expression-list passed as the third argument
120179 ** is redundant.
120180 **
120181 ** A DISTINCT list is redundant if any subset of the columns in the
120182 ** DISTINCT list are collectively unique and individually non-null.
120183 */
120184 static int isDistinctRedundant(
120185 Parse *pParse, /* Parsing context */
120186 SrcList *pTabList, /* The FROM clause */
120187 WhereClause *pWC, /* The WHERE clause */
@@ -117926,11 +120223,11 @@
120223 */
120224 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120225 if( !IsUniqueIndex(pIdx) ) continue;
120226 for(i=0; i<pIdx->nKeyCol; i++){
120227 i16 iCol = pIdx->aiColumn[i];
120228 if( 0==sqlite3WhereFindTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
120229 int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
120230 if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
120231 break;
120232 }
120233 }
@@ -118257,10 +120554,11 @@
120554 ** by passing the pointer returned by this function to sqlite3_free().
120555 */
120556 static sqlite3_index_info *allocateIndexInfo(
120557 Parse *pParse,
120558 WhereClause *pWC,
120559 Bitmask mUnusable, /* Ignore terms with these prereqs */
120560 struct SrcList_item *pSrc,
120561 ExprList *pOrderBy
120562 ){
120563 int i, j;
120564 int nTerm;
@@ -118273,10 +120571,11 @@
120571
120572 /* Count the number of possible WHERE clause constraints referring
120573 ** to this virtual table */
120574 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
120575 if( pTerm->leftCursor != pSrc->iCursor ) continue;
120576 if( pTerm->prereqRight & mUnusable ) continue;
120577 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
120578 testcase( pTerm->eOperator & WO_IN );
120579 testcase( pTerm->eOperator & WO_ISNULL );
120580 testcase( pTerm->eOperator & WO_IS );
120581 testcase( pTerm->eOperator & WO_ALL );
@@ -118327,10 +120626,11 @@
120626 pUsage;
120627
120628 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
120629 u8 op;
120630 if( pTerm->leftCursor != pSrc->iCursor ) continue;
120631 if( pTerm->prereqRight & mUnusable ) continue;
120632 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
120633 testcase( pTerm->eOperator & WO_IN );
120634 testcase( pTerm->eOperator & WO_IS );
120635 testcase( pTerm->eOperator & WO_ISNULL );
120636 testcase( pTerm->eOperator & WO_ALL );
@@ -119048,1491 +121348,10 @@
121348 assert( pBuilder->nRecValid==nRecValid );
121349 return rc;
121350 }
121351 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
121352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121353
121354 #ifdef WHERETRACE_ENABLED
121355 /*
121356 ** Print the content of a WhereTerm object
121357 */
@@ -120695,11 +121514,11 @@
121514 WhereLevel *pLevel = &pWInfo->a[i];
121515 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
121516 sqlite3DbFree(db, pLevel->u.in.aInLoop);
121517 }
121518 }
121519 sqlite3WhereClauseClear(&pWInfo->sWC);
121520 while( pWInfo->pLoops ){
121521 WhereLoop *p = pWInfo->pLoops;
121522 pWInfo->pLoops = p->pNextLoop;
121523 whereLoopDelete(db, p);
121524 }
@@ -121647,14 +122466,36 @@
122466
122467 #ifndef SQLITE_OMIT_VIRTUALTABLE
122468 /*
122469 ** Add all WhereLoop objects for a table of the join identified by
122470 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
122471 **
122472 ** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and
122473 ** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause
122474 ** entries that occur before the virtual table in the FROM clause and are
122475 ** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
122476 ** mUnusable mask contains all FROM clause entries that occur after the
122477 ** virtual table and are separated from it by at least one LEFT or
122478 ** CROSS JOIN.
122479 **
122480 ** For example, if the query were:
122481 **
122482 ** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
122483 **
122484 ** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6).
122485 **
122486 ** All the tables in mExtra must be scanned before the current virtual
122487 ** table. So any terms for which all prerequisites are satisfied by
122488 ** mExtra may be specified as "usable" in all calls to xBestIndex.
122489 ** Conversely, all tables in mUnusable must be scanned after the current
122490 ** virtual table, so any terms for which the prerequisites overlap with
122491 ** mUnusable should always be configured as "not-usable" for xBestIndex.
122492 */
122493 static int whereLoopAddVirtual(
122494 WhereLoopBuilder *pBuilder, /* WHERE clause information */
122495 Bitmask mExtra, /* Tables that must be scanned before this one */
122496 Bitmask mUnusable /* Tables that must be scanned after this one */
122497 ){
122498 WhereInfo *pWInfo; /* WHERE analysis context */
122499 Parse *pParse; /* The parsing context */
122500 WhereClause *pWC; /* The WHERE clause */
122501 struct SrcList_item *pSrc; /* The FROM clause term to search */
@@ -121671,19 +122512,20 @@
122512 int seenVar = 0; /* True if a non-constant constraint is seen */
122513 int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */
122514 WhereLoop *pNew;
122515 int rc = SQLITE_OK;
122516
122517 assert( (mExtra & mUnusable)==0 );
122518 pWInfo = pBuilder->pWInfo;
122519 pParse = pWInfo->pParse;
122520 db = pParse->db;
122521 pWC = pBuilder->pWC;
122522 pNew = pBuilder->pNew;
122523 pSrc = &pWInfo->pTabList->a[pNew->iTab];
122524 pTab = pSrc->pTab;
122525 assert( IsVirtual(pTab) );
122526 pIdxInfo = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy);
122527 if( pIdxInfo==0 ) return SQLITE_NOMEM;
122528 pNew->prereq = 0;
122529 pNew->rSetup = 0;
122530 pNew->wsFlags = WHERE_VIRTUALTABLE;
122531 pNew->nLTerm = 0;
@@ -121709,19 +122551,19 @@
122551 case 0: /* Constants without IN operator */
122552 pIdxCons->usable = 0;
122553 if( (pTerm->eOperator & WO_IN)!=0 ){
122554 seenIn = 1;
122555 }
122556 if( (pTerm->prereqRight & ~mExtra)!=0 ){
122557 seenVar = 1;
122558 }else if( (pTerm->eOperator & WO_IN)==0 ){
122559 pIdxCons->usable = 1;
122560 }
122561 break;
122562 case 1: /* Constants with IN operators */
122563 assert( seenIn );
122564 pIdxCons->usable = (pTerm->prereqRight & ~mExtra)==0;
122565 break;
122566 case 2: /* Variables without IN */
122567 assert( seenVar );
122568 pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
122569 break;
@@ -121816,11 +122658,15 @@
122658
122659 /*
122660 ** Add WhereLoop entries to handle OR terms. This works for either
122661 ** btrees or virtual tables.
122662 */
122663 static int whereLoopAddOr(
122664 WhereLoopBuilder *pBuilder,
122665 Bitmask mExtra,
122666 Bitmask mUnusable
122667 ){
122668 WhereInfo *pWInfo = pBuilder->pWInfo;
122669 WhereClause *pWC;
122670 WhereLoop *pNew;
122671 WhereTerm *pTerm, *pWCEnd;
122672 int rc = SQLITE_OK;
@@ -121875,18 +122721,18 @@
122721 }
122722 }
122723 #endif
122724 #ifndef SQLITE_OMIT_VIRTUALTABLE
122725 if( IsVirtual(pItem->pTab) ){
122726 rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable);
122727 }else
122728 #endif
122729 {
122730 rc = whereLoopAddBtree(&sSubBuild, mExtra);
122731 }
122732 if( rc==SQLITE_OK ){
122733 rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable);
122734 }
122735 assert( rc==SQLITE_OK || sCur.n==0 );
122736 if( sCur.n==0 ){
122737 sSum.n = 0;
122738 break;
@@ -121944,37 +122790,47 @@
122790 Bitmask mExtra = 0;
122791 Bitmask mPrior = 0;
122792 int iTab;
122793 SrcList *pTabList = pWInfo->pTabList;
122794 struct SrcList_item *pItem;
122795 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
122796 sqlite3 *db = pWInfo->pParse->db;
 
122797 int rc = SQLITE_OK;
 
122798 WhereLoop *pNew;
122799 u8 priorJointype = 0;
122800
122801 /* Loop over the tables in the join, from left to right */
122802 pNew = pBuilder->pNew;
122803 whereLoopInit(pNew);
122804 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
122805 Bitmask mUnusable = 0;
122806 pNew->iTab = iTab;
122807 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
122808 if( ((pItem->jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
122809 /* This condition is true when pItem is the FROM clause term on the
122810 ** right-hand-side of a LEFT or CROSS JOIN. */
122811 mExtra = mPrior;
122812 }
122813 priorJointype = pItem->jointype;
122814 if( IsVirtual(pItem->pTab) ){
122815 struct SrcList_item *p;
122816 for(p=&pItem[1]; p<pEnd; p++){
122817 if( mUnusable || (p->jointype & (JT_LEFT|JT_CROSS)) ){
122818 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
122819 }
122820 }
122821 rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable);
122822 }else{
122823 rc = whereLoopAddBtree(pBuilder, mExtra);
122824 }
122825 if( rc==SQLITE_OK ){
122826 rc = whereLoopAddOr(pBuilder, mExtra, mUnusable);
122827 }
122828 mPrior |= pNew->maskSelf;
122829 if( rc || db->mallocFailed ) break;
122830 }
122831
122832 whereLoopClear(db, pNew);
122833 return rc;
122834 }
122835
122836 /*
@@ -122076,11 +122932,11 @@
122932 for(i=0; i<nOrderBy; i++){
122933 if( MASKBIT(i) & obSat ) continue;
122934 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
122935 if( pOBExpr->op!=TK_COLUMN ) continue;
122936 if( pOBExpr->iTable!=iCur ) continue;
122937 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
122938 ~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
122939 if( pTerm==0 ) continue;
122940 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
122941 const char *z1, *z2;
122942 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
@@ -122213,11 +123069,11 @@
123069 for(i=0; i<nOrderBy; i++){
123070 Expr *p;
123071 Bitmask mTerm;
123072 if( MASKBIT(i) & obSat ) continue;
123073 p = pOrderBy->a[i].pExpr;
123074 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
123075 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
123076 if( (mTerm&~orderDistinctMask)==0 ){
123077 obSat |= MASKBIT(i);
123078 }
123079 }
@@ -122686,17 +123542,17 @@
123542 if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
123543 assert( pWInfo->pTabList->nSrc>=1 );
123544 pItem = pWInfo->pTabList->a;
123545 pTab = pItem->pTab;
123546 if( IsVirtual(pTab) ) return 0;
123547 if( pItem->zIndexedBy ) return 0;
123548 iCur = pItem->iCursor;
123549 pWC = &pWInfo->sWC;
123550 pLoop = pBuilder->pNew;
123551 pLoop->wsFlags = 0;
123552 pLoop->nSkip = 0;
123553 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
123554 if( pTerm ){
123555 testcase( pTerm->eOperator & WO_IS );
123556 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
123557 pLoop->aLTerm[0] = pTerm;
123558 pLoop->nLTerm = 1;
@@ -122711,11 +123567,11 @@
123567 || pIdx->pPartIdxWhere!=0
123568 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
123569 ) continue;
123570 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
123571 for(j=0; j<pIdx->nKeyCol; j++){
123572 pTerm = sqlite3WhereFindTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
123573 if( pTerm==0 ) break;
123574 testcase( pTerm->eOperator & WO_IS );
123575 pLoop->aLTerm[j] = pTerm;
123576 }
123577 if( j!=pIdx->nKeyCol ) continue;
@@ -122732,11 +123588,11 @@
123588 }
123589 }
123590 if( pLoop->wsFlags ){
123591 pLoop->nOut = (LogEst)1;
123592 pWInfo->a[0].pWLoop = pLoop;
123593 pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
123594 pWInfo->a[0].iTabCur = iCur;
123595 pWInfo->nRowOut = 1;
123596 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
123597 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
123598 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
@@ -122926,12 +123782,12 @@
123782
123783 /* Split the WHERE clause into separate subexpressions where each
123784 ** subexpression is separated by an AND operator.
123785 */
123786 initMaskSet(pMaskSet);
123787 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
123788 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
123789
123790 /* Special case: a WHERE clause that is constant. Evaluate the
123791 ** expression and either jump over all of the code or fall thru.
123792 */
123793 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
@@ -122972,26 +123828,20 @@
123828 }
123829 #ifndef NDEBUG
123830 {
123831 Bitmask toTheLeft = 0;
123832 for(ii=0; ii<pTabList->nSrc; ii++){
123833 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
123834 assert( (m-1)==toTheLeft );
123835 toTheLeft |= m;
123836 }
123837 }
123838 #endif
123839
123840 /* Analyze all of the subexpressions. */
123841 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
123842 if( db->mallocFailed ) goto whereBeginError;
 
 
 
 
 
 
123843
123844 if( wctrlFlags & WHERE_WANT_DISTINCT ){
123845 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
123846 /* The DISTINCT marking is pointless. Ignore it. */
123847 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
@@ -123003,12 +123853,11 @@
123853 }
123854
123855 /* Construct the WhereLoop objects */
123856 WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
123857 #if defined(WHERETRACE_ENABLED)
123858 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
 
123859 int i;
123860 for(i=0; i<sWLB.pWC->nTerm; i++){
123861 whereTermPrint(&sWLB.pWC->a[i], i);
123862 }
123863 }
@@ -123016,17 +123865,16 @@
123865
123866 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
123867 rc = whereLoopAddAll(&sWLB);
123868 if( rc ) goto whereBeginError;
123869
123870 #ifdef WHERETRACE_ENABLED
123871 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
 
123872 WhereLoop *p;
123873 int i;
123874 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
123875 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
123876 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
123877 p->cId = zLabel[i%sizeof(zLabel)];
123878 whereLoopPrint(p, sWLB.pWC);
123879 }
123880 }
@@ -123043,11 +123891,11 @@
123891 pWInfo->revMask = (Bitmask)(-1);
123892 }
123893 if( pParse->nErr || NEVER(db->mallocFailed) ){
123894 goto whereBeginError;
123895 }
123896 #ifdef WHERETRACE_ENABLED
123897 if( sqlite3WhereTrace ){
123898 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
123899 if( pWInfo->nOBSat>0 ){
123900 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
123901 }
@@ -123074,12 +123922,14 @@
123922 /* Attempt to omit tables from the join that do not effect the result */
123923 if( pWInfo->nLevel>=2
123924 && pResultSet!=0
123925 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
123926 ){
123927 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
123928 if( sWLB.pOrderBy ){
123929 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
123930 }
123931 while( pWInfo->nLevel>=2 ){
123932 WhereTerm *pTerm, *pEnd;
123933 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
123934 if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
123935 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
@@ -123106,11 +123956,11 @@
123956 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
123957
123958 /* If the caller is an UPDATE or DELETE statement that is requesting
123959 ** to use a one-pass algorithm, determine if this is appropriate.
123960 ** The one-pass algorithm only works if the WHERE clause constrains
123961 ** the statement to update or delete a single row.
123962 */
123963 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
123964 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
123965 && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
123966 pWInfo->okOnePass = 1;
@@ -123120,11 +123970,10 @@
123970 }
123971
123972 /* Open all tables in the pTabList and any indices selected for
123973 ** searching those tables.
123974 */
 
123975 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
123976 Table *pTab; /* Table to open */
123977 int iDb; /* Index of database containing table/index */
123978 struct SrcList_item *pTabItem;
123979
@@ -123161,10 +124010,14 @@
124010 for(; b; b=b>>1, n++){}
124011 sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
124012 SQLITE_INT_TO_PTR(n), P4_INT32);
124013 assert( n<=pTab->nCol );
124014 }
124015 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
124016 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
124017 (const u8*)&pTabItem->colUsed, P4_INT64);
124018 #endif
124019 }else{
124020 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
124021 }
124022 if( pLoop->wsFlags & WHERE_INDEXED ){
124023 Index *pIx = pLoop->u.btree.pIndex;
@@ -123206,14 +124059,28 @@
124059 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
124060 ){
124061 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
124062 }
124063 VdbeComment((v, "%s", pIx->zName));
124064 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
124065 {
124066 u64 colUsed = 0;
124067 int ii, jj;
124068 for(ii=0; ii<pIx->nColumn; ii++){
124069 jj = pIx->aiColumn[ii];
124070 if( jj<0 ) continue;
124071 if( jj>63 ) jj = 63;
124072 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
124073 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
124074 }
124075 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
124076 (u8*)&colUsed, P4_INT64);
124077 }
124078 #endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
124079 }
124080 }
124081 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
 
124082 }
124083 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
124084 if( db->mallocFailed ) goto whereBeginError;
124085
124086 /* Generate the code to do the search. Each iteration of the for
@@ -123231,18 +124098,18 @@
124098 constructAutomaticIndex(pParse, &pWInfo->sWC,
124099 &pTabList->a[pLevel->iFrom], notReady, pLevel);
124100 if( db->mallocFailed ) goto whereBeginError;
124101 }
124102 #endif
124103 addrExplain = sqlite3WhereExplainOneScan(
124104 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
124105 );
124106 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
124107 notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
124108 pWInfo->iContinue = pLevel->addrCont;
124109 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
124110 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
124111 }
124112 }
124113
124114 /* Done. */
124115 VdbeModuleComment((v, "Begin WHERE-core"));
@@ -124924,11 +125791,11 @@
125791 YYCODETYPE yymajor;
125792 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
125793
125794 /* There is no mechanism by which the parser stack can be popped below
125795 ** empty in SQLite. */
125796 assert( pParser->yyidx>=0 );
125797 #ifndef NDEBUG
125798 if( yyTraceFILE && pParser->yyidx>=0 ){
125799 fprintf(yyTraceFILE,"%sPopping %s\n",
125800 yyTracePrompt,
125801 yyTokenName[yytos->major]);
@@ -127396,11 +128263,15 @@
128263 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
128264 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
128265 };
128266 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
128267 #endif
128268
128269 /* Make the IdChar function accessible from ctime.c */
128270 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
128271 SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
128272 #endif
128273
128274
128275 /*
128276 ** Return the length of the token that begins at z[0].
128277 ** Store the token type in *tokenType before returning.
@@ -128104,11 +128975,11 @@
128975 rc = sqlite3_complete(zSql8);
128976 }else{
128977 rc = SQLITE_NOMEM;
128978 }
128979 sqlite3ValueFree(pVal);
128980 return rc & 0xff;
128981 }
128982 #endif /* SQLITE_OMIT_UTF16 */
128983 #endif /* SQLITE_OMIT_COMPLETE */
128984
128985 /************** End of complete.c ********************************************/
@@ -130282,13 +131153,15 @@
131153 #endif
131154 #if SQLITE_TEMP_STORE==2
131155 return ( db->temp_store!=1 );
131156 #endif
131157 #if SQLITE_TEMP_STORE==3
131158 UNUSED_PARAMETER(db);
131159 return 1;
131160 #endif
131161 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
131162 UNUSED_PARAMETER(db);
131163 return 0;
131164 #endif
131165 }
131166
131167 /*
@@ -131126,11 +131999,11 @@
131999 /* Opening a db handle. Fourth parameter is passed 0. */
132000 void *pArg = sqlite3GlobalConfig.pSqllogArg;
132001 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
132002 }
132003 #endif
132004 return rc & 0xff;
132005 }
132006
132007 /*
132008 ** Open a new database handle.
132009 */
@@ -131184,11 +132057,11 @@
132057 }else{
132058 rc = SQLITE_NOMEM;
132059 }
132060 sqlite3ValueFree(pVal);
132061
132062 return rc & 0xff;
132063 }
132064 #endif /* SQLITE_OMIT_UTF16 */
132065
132066 /*
132067 ** Register a new collation sequence with the database handle db.
@@ -131556,11 +132429,13 @@
132429 /*
132430 ** Interface to the testing logic.
132431 */
132432 SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
132433 int rc = 0;
132434 #ifdef SQLITE_OMIT_BUILTIN_TEST
132435 UNUSED_PARAMETER(op);
132436 #else
132437 va_list ap;
132438 va_start(ap, op);
132439 switch( op ){
132440
132441 /*
@@ -154904,11 +155779,10 @@
155779 while( zPattern[iPattern]!=0 ){
155780
155781 /* Read (and consume) the next character from the input pattern. */
155782 UChar32 uPattern;
155783 U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
 
155784
155785 /* There are now 4 possibilities:
155786 **
155787 ** 1. uPattern is an unescaped match-all character "%",
155788 ** 2. uPattern is an unescaped match-one character "_",
@@ -155243,10 +156117,11 @@
156117 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
156118 UCollator *pUCollator; /* ICU library collation object */
156119 int rc; /* Return code from sqlite3_create_collation_x() */
156120
156121 assert(nArg==2);
156122 (void)nArg; /* Unused parameter */
156123 zLocale = (const char *)sqlite3_value_text(apArg[0]);
156124 zName = (const char *)sqlite3_value_text(apArg[1]);
156125
156126 if( !zLocale || !zName ){
156127 return;
@@ -155566,16 +156441,17 @@
156441
156442 /*
156443 ** The set of routines that implement the simple tokenizer
156444 */
156445 static const sqlite3_tokenizer_module icuTokenizerModule = {
156446 0, /* iVersion */
156447 icuCreate, /* xCreate */
156448 icuDestroy, /* xCreate */
156449 icuOpen, /* xOpen */
156450 icuClose, /* xClose */
156451 icuNext, /* xNext */
156452 0, /* xLanguageid */
156453 };
156454
156455 /*
156456 ** Set *ppModule to point at the implementation of the ICU tokenizer.
156457 */
@@ -159131,12 +160007,12 @@
160007 static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160008 ota_file *p = (ota_file *)pFile;
160009 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
160010 int rc;
160011
160012 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160013 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
160014 );
160015 if( op==SQLITE_FCNTL_OTA ){
160016 sqlite3ota *pOta = (sqlite3ota*)pArg;
160017
160018 /* First try to find another OTA vfs lower down in the vfs stack. If
160019
+3 -3
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -21,11 +21,11 @@
2121
** to experimental interfaces but reserve the right to make minor changes
2222
** if experience from use "in the wild" suggest such changes are prudent.
2323
**
2424
** The official C-language API documentation for SQLite is derived
2525
** from comments in this file. This file is the authoritative source
26
-** on how SQLite interfaces are suppose to operate.
26
+** on how SQLite interfaces are supposed to operate.
2727
**
2828
** The name of this file under configuration management is "sqlite.h.in".
2929
** The makefile makes some minor changes to this file (such as inserting
3030
** the version number) and changes its name to "sqlite3.h" as
3131
** part of the build process.
@@ -111,11 +111,11 @@
111111
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112112
** [sqlite_version()] and [sqlite_source_id()].
113113
*/
114114
#define SQLITE_VERSION "3.8.11"
115115
#define SQLITE_VERSION_NUMBER 3008011
116
-#define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417"
116
+#define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899"
117117
118118
/*
119119
** CAPI3REF: Run-Time Library Version Numbers
120120
** KEYWORDS: sqlite3_version, sqlite3_sourceid
121121
**
@@ -954,11 +954,11 @@
954954
** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
955955
** opcode causes the xFileControl method to swap the file handle with the one
956956
** pointed to by the pArg argument. This capability is used during testing
957957
** and only needs to be supported when SQLITE_TEST is defined.
958958
**
959
-* <li>[[SQLITE_FCNTL_WAL_BLOCK]]
959
+** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
960960
** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
961961
** be advantageous to block on the next WAL lock if the lock is not immediately
962962
** available. The WAL subsystem issues this signal during rare
963963
** circumstances in order to fix a problem with priority inversion.
964964
** Applications should <em>not</em> use this file-control.
965965
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -21,11 +21,11 @@
21 ** to experimental interfaces but reserve the right to make minor changes
22 ** if experience from use "in the wild" suggest such changes are prudent.
23 **
24 ** The official C-language API documentation for SQLite is derived
25 ** from comments in this file. This file is the authoritative source
26 ** on how SQLite interfaces are suppose to operate.
27 **
28 ** The name of this file under configuration management is "sqlite.h.in".
29 ** The makefile makes some minor changes to this file (such as inserting
30 ** the version number) and changes its name to "sqlite3.h" as
31 ** part of the build process.
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -954,11 +954,11 @@
954 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
955 ** opcode causes the xFileControl method to swap the file handle with the one
956 ** pointed to by the pArg argument. This capability is used during testing
957 ** and only needs to be supported when SQLITE_TEST is defined.
958 **
959 * <li>[[SQLITE_FCNTL_WAL_BLOCK]]
960 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
961 ** be advantageous to block on the next WAL lock if the lock is not immediately
962 ** available. The WAL subsystem issues this signal during rare
963 ** circumstances in order to fix a problem with priority inversion.
964 ** Applications should <em>not</em> use this file-control.
965
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -21,11 +21,11 @@
21 ** to experimental interfaces but reserve the right to make minor changes
22 ** if experience from use "in the wild" suggest such changes are prudent.
23 **
24 ** The official C-language API documentation for SQLite is derived
25 ** from comments in this file. This file is the authoritative source
26 ** on how SQLite interfaces are supposed to operate.
27 **
28 ** The name of this file under configuration management is "sqlite.h.in".
29 ** The makefile makes some minor changes to this file (such as inserting
30 ** the version number) and changes its name to "sqlite3.h" as
31 ** part of the build process.
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -954,11 +954,11 @@
954 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
955 ** opcode causes the xFileControl method to swap the file handle with the one
956 ** pointed to by the pArg argument. This capability is used during testing
957 ** and only needs to be supported when SQLITE_TEST is defined.
958 **
959 ** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
960 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
961 ** be advantageous to block on the next WAL lock if the lock is not immediately
962 ** available. The WAL subsystem issues this signal during rare
963 ** circumstances in order to fix a problem with priority inversion.
964 ** Applications should <em>not</em> use this file-control.
965

Keyboard Shortcuts

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